com.sun.tools.javac.tree.JCTree.JCBinary Java Examples
The following examples show how to use
com.sun.tools.javac.tree.JCTree.JCBinary.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Lower.java From lua-for-android with BSD 3-Clause "New" or "Revised" License | 6 votes |
private Boolean expValue(JCTree exp) { while (exp.hasTag(PARENS)) exp = ((JCParens)exp).expr; boolean eq; switch (exp.getTag()) { case EQ: eq = true; break; case NE: eq = false; break; default: return null; } // we have a JCBinary(EQ|NE) // check if we have two literals (constants or null) JCBinary b = (JCBinary)exp; if (b.lhs.type.hasTag(BOT)) return expValueIsNull(eq, b.rhs); if (b.rhs.type.hasTag(BOT)) return expValueIsNull(eq, b.lhs); return null; }
Example #2
Source File: JavacBinder.java From manifold with Apache License 2.0 | 6 votes |
@Override protected Node<JCExpression, Tag> makeBinaryExpression( Node<JCExpression, Tag> left, Node<JCExpression, Tag> right, Symbol.MethodSymbol binderMethod ) { JCBinary binary = _make.Binary( right._operatorLeft == null ? Tag.MUL : right._operatorLeft, left._expr, right._expr ); binary.pos = left._expr.pos; boolean rightToLeft = binderMethod instanceof OverloadOperatorSymbol && ((OverloadOperatorSymbol)binderMethod).isSwapped() || binderMethod.name.toString().equals( "postfixBind" ); IDynamicJdk.instance().setOperator( binary, new OverloadOperatorSymbol( binderMethod, rightToLeft ) ); binary.type = rightToLeft ? memberType( right._expr.type, left._expr.type, binderMethod ) : memberType( left._expr.type, right._expr.type, binderMethod ); return new Node<>( binary, left._operatorLeft ); }
Example #3
Source File: TwrAvoidNullCheck.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
@Override public List<JCTree> translateTopLevelClass(Env<AttrContext> env, JCTree cdef, TreeMaker make) { List<JCTree> result = super.translateTopLevelClass(env, cdef, make); new TreeScanner() { @Override public void visitBinary(JCBinary tree) { hasNullCheck |= tree.operator.getSimpleName().contentEquals("!=") && "resource".equals(String.valueOf(TreeInfo.name(tree.lhs))) && TreeInfo.isNull(tree.rhs); super.visitBinary(tree); } }.scan(result); return result; }
Example #4
Source File: LambdaToMethod.java From lua-for-android with BSD 3-Clause "New" or "Revised" License | 5 votes |
private JCExpression deserTest(JCExpression prev, String func, String lit) { MethodType eqmt = new MethodType(List.of(syms.objectType), syms.booleanType, List.nil(), syms.methodClass); Symbol eqsym = rs.resolveQualifiedMethod(null, attrEnv, syms.objectType, names.equals, List.of(syms.objectType), List.nil()); JCMethodInvocation eqtest = make.Apply( List.nil(), make.Select(deserGetter(func, syms.stringType), eqsym).setType(eqmt), List.of(make.Literal(lit))); eqtest.setType(syms.booleanType); JCBinary compound = make.Binary(JCTree.Tag.AND, prev, eqtest); compound.operator = operators.resolveBinary(compound, JCTree.Tag.AND, syms.booleanType, syms.booleanType); compound.setType(syms.booleanType); return compound; }
Example #5
Source File: PrettyCommentsPrinter.java From EasyMPermission with MIT License | 5 votes |
public void visitBinary(JCBinary tree) { try { int ownprec = isOwnPrec(tree); String opname = operatorName(treeTag(tree)); open(prec, ownprec); printExpr(tree.lhs, ownprec); print(" " + opname + " "); printExpr(tree.rhs, ownprec + 1); close(prec, ownprec); } catch (IOException e) { throw new UncheckedIOException(e); } }
Example #6
Source File: HandleEqualsAndHashCode.java From EasyMPermission with MIT License | 5 votes |
public JCStatement generateCompareFloatOrDouble(JCExpression thisDotField, JCExpression otherDotField, JavacTreeMaker maker, JavacNode node, boolean isDouble) { /* if (Float.compare(fieldName, other.fieldName) != 0) return false; */ JCExpression clazz = genJavaLangTypeRef(node, isDouble ? "Double" : "Float"); List<JCExpression> args = List.of(thisDotField, otherDotField); JCBinary compareCallEquals0 = maker.Binary(CTC_NOT_EQUAL, maker.Apply( List.<JCExpression>nil(), maker.Select(clazz, node.toName("compare")), args), maker.Literal(0)); return maker.If(compareCallEquals0, returnBool(maker, false), null); }
Example #7
Source File: ManAttr.java From manifold with Apache License 2.0 | 5 votes |
default ArrayList<Node<JCExpression, Tag>> getBindingOperands( JCExpression tree, ArrayList<Node<JCExpression, Tag>> operands ) { if( tree instanceof JCBinary && tree.getTag() == Tag.APPLY ) { // Binding expr getBindingOperands( ((JCBinary)tree).lhs, operands ); getBindingOperands( ((JCBinary)tree).rhs, operands ); } else if( tree instanceof JCBinary ) { JCBinary binExpr = (JCBinary)tree; Tag opcode = (Tag)ReflectUtil.field( tree, "opcode" ).get(); getBindingOperands( binExpr.lhs, operands ); int index = operands.size(); getBindingOperands( binExpr.rhs, operands ); Node<JCExpression, Tag> rhsNode = operands.get( index ); rhsNode._operatorLeft = opcode; } else { ReflectUtil.LiveMethodRef checkNonVoid = ReflectUtil.method( chk(), "checkNonVoid", JCDiagnostic.DiagnosticPosition.class, Type.class ); ReflectUtil.LiveMethodRef attribExpr = ReflectUtil.method( this, "attribExpr", JCTree.class, Env.class ); checkNonVoid.invoke( tree.pos(), attribExpr.invoke( tree, getEnv() ) ); operands.add( new JavacBinder.Node<>( tree ) ); } return operands; }
Example #8
Source File: ManAttr.java From manifold with Apache License 2.0 | 5 votes |
default void visitBindingExpression( JCTree.JCBinary tree ) { Type owntype; if( IDynamicJdk.instance().getOperator( tree ) == null ) { // replace the tree with JCBinary expressions reflecting the correct associativity and bindingOperator JCTree.JCBinary newTree = new JavacBinder( types() ).bind( getBindingOperands( tree, new ArrayList<>() ) ); if( newTree == null ) { getLogger().error( tree.lhs.pos, "proc.messager", "No reaction defined for types '" + tree.lhs.type + "' and '" + tree.rhs.type + "'" ); return; } ReflectUtil.field( tree, "opcode" ).set( ReflectUtil.field( newTree, "opcode" ).get() ); tree.lhs = newTree.lhs; tree.rhs = newTree.rhs; tree.type = newTree.type; IDynamicJdk.instance().setOperator( tree, (Symbol.OperatorSymbol)IDynamicJdk.instance().getOperator( newTree ) ); owntype = newTree.type; } else { Symbol operator = IDynamicJdk.instance().getOperator( tree ); owntype = operator.type.isErroneous() ? operator.type : operator.type.getReturnType(); } setResult( tree, owntype ); }
Example #9
Source File: ManAttr.java From manifold with Apache License 2.0 | 5 votes |
default boolean handleOperatorOverloading( JCBinary tree, Type left, Type right ) { // Handle operator overloading boolean swapped = false; Symbol.MethodSymbol overloadOperator = ManAttr.resolveOperatorMethod( types(), tree.getTag(), left, right ); if( overloadOperator == null && ManAttr.isCommutative( tree.getTag() ) ) { overloadOperator = ManAttr.resolveOperatorMethod( types(), tree.getTag(), right, left ); swapped = true; } if( overloadOperator != null ) { if( overloadOperator.name.toString().equals( COMPARE_TO ) ) { // pose with boolean return to satisfy type checker, this call will be transformed in ext transformer Type.MethodType typePoseWithBooleanReturn = new Type.MethodType( overloadOperator.type.getParameterTypes(), syms().booleanType, overloadOperator.type.getThrownTypes(), syms().methodClass ); overloadOperator = new OverloadOperatorSymbol( overloadOperator, typePoseWithBooleanReturn, swapped ); } else { overloadOperator = new OverloadOperatorSymbol( overloadOperator, swapped ); } IDynamicJdk.instance().setOperator( tree, (Symbol.OperatorSymbol)overloadOperator ); Type owntype = overloadOperator.type.isErroneous() ? overloadOperator.type : swapped ? types().memberType( right, overloadOperator ).getReturnType() : types().memberType( left, overloadOperator ).getReturnType(); setResult( tree, owntype ); return true; } return false; }
Example #10
Source File: CompilationUnitBuilder.java From j2cl with Apache License 2.0 | 5 votes |
private BinaryExpression convertBinary(JCBinary expression) { return BinaryExpression.newBuilder() .setLeftOperand(convertExpression(expression.getLeftOperand())) .setOperator(JavaEnvironment.getBinaryOperator(expression.getKind())) .setRightOperand(convertExpression(expression.getRightOperand())) .build(); }
Example #11
Source File: MemberEnter.java From lua-for-android with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public void visitBinary(JCBinary that) { if (!ALLOWED_OPERATORS.contains(that.getTag())) { result = false; return ; } that.lhs.accept(this); that.rhs.accept(this); }
Example #12
Source File: HandleNonNull.java From EasyMPermission with MIT License | 5 votes |
/** * Checks if the statement is of the form 'if (x == null) {throw WHATEVER;}, * where the block braces are optional. If it is of this form, returns "x". * If it is not of this form, returns null. */ public String returnVarNameIfNullCheck(JCStatement stat) { if (!(stat instanceof JCIf)) return null; /* Check that the if's statement is a throw statement, possibly in a block. */ { JCStatement then = ((JCIf) stat).thenpart; if (then instanceof JCBlock) { List<JCStatement> stats = ((JCBlock) then).stats; if (stats.length() == 0) return null; then = stats.get(0); } if (!(then instanceof JCThrow)) return null; } /* Check that the if's conditional is like 'x == null'. Return from this method (don't generate a nullcheck) if 'x' is equal to our own variable's name: There's already a nullcheck here. */ { JCExpression cond = ((JCIf) stat).cond; while (cond instanceof JCParens) cond = ((JCParens) cond).expr; if (!(cond instanceof JCBinary)) return null; JCBinary bin = (JCBinary) cond; if (!CTC_EQUAL.equals(treeTag(bin))) return null; if (!(bin.lhs instanceof JCIdent)) return null; if (!(bin.rhs instanceof JCLiteral)) return null; if (!CTC_BOT.equals(typeTag(bin.rhs))) return null; return ((JCIdent) bin.lhs).name.toString(); } }
Example #13
Source File: Lower.java From lua-for-android with BSD 3-Clause "New" or "Revised" License | 5 votes |
public void visitBinary(JCBinary tree) { List<Type> formals = tree.operator.type.getParameterTypes(); JCTree lhs = tree.lhs = translate(tree.lhs, formals.head); switch (tree.getTag()) { case OR: if (isTrue(lhs)) { result = lhs; return; } if (isFalse(lhs)) { result = translate(tree.rhs, formals.tail.head); return; } break; case AND: if (isFalse(lhs)) { result = lhs; return; } if (isTrue(lhs)) { result = translate(tree.rhs, formals.tail.head); return; } break; } tree.rhs = translate(tree.rhs, formals.tail.head); result = tree; }
Example #14
Source File: Lower.java From lua-for-android with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** Make an attributed binary expression. * @param optag The operators tree tag. * @param lhs The operator's left argument. * @param rhs The operator's right argument. */ JCBinary makeBinary(JCTree.Tag optag, JCExpression lhs, JCExpression rhs) { JCBinary tree = make.Binary(optag, lhs, rhs); tree.operator = operators.resolveBinary(tree, optag, lhs.type, rhs.type); tree.type = tree.operator.type.getReturnType(); return tree; }
Example #15
Source File: Flow.java From lua-for-android with BSD 3-Clause "New" or "Revised" License | 5 votes |
public void visitBinary(JCBinary tree) { switch (tree.getTag()) { case AND: scanCond(tree.lhs); final Bits initsWhenFalseLeft = new Bits(initsWhenFalse); final Bits uninitsWhenFalseLeft = new Bits(uninitsWhenFalse); inits.assign(initsWhenTrue); uninits.assign(uninitsWhenTrue); scanCond(tree.rhs); initsWhenFalse.andSet(initsWhenFalseLeft); uninitsWhenFalse.andSet(uninitsWhenFalseLeft); break; case OR: scanCond(tree.lhs); final Bits initsWhenTrueLeft = new Bits(initsWhenTrue); final Bits uninitsWhenTrueLeft = new Bits(uninitsWhenTrue); inits.assign(initsWhenFalse); uninits.assign(uninitsWhenFalse); scanCond(tree.rhs); initsWhenTrue.andSet(initsWhenTrueLeft); uninitsWhenTrue.andSet(uninitsWhenTrueLeft); break; default: scanExpr(tree.lhs); scanExpr(tree.rhs); } }
Example #16
Source File: UBinary.java From Refaster with Apache License 2.0 | 5 votes |
@Override public JCBinary inline(Inliner inliner) throws CouldNotResolveImportException { return inliner.maker().Binary( OP_CODES.get(getKind()), getLeftOperand().inline(inliner), getRightOperand().inline(inliner)); }
Example #17
Source File: ExpressionTemplate.java From Refaster with Apache License 2.0 | 4 votes |
/** * Returns the precedence level appropriate for unambiguously printing * leaf as a subexpression of its parent. */ private static int getPrecedence(JCTree leaf, Context context) { JCCompilationUnit comp = context.get(JCCompilationUnit.class); JCTree parent = TreeInfo.pathFor(leaf, comp).get(1); // In general, this should match the logic in com.sun.tools.javac.tree.Pretty. // // TODO(mdempsky): There are probably cases where we could omit parentheses // by tweaking the returned precedence, but they need careful review. // For example, consider a template to replace "add(a, b)" with "a + b", // which applied to "x + add(y, z)" would result in "x + (y + z)". // In most cases, we'd likely prefer "x + y + z" instead, but those aren't // always equivalent: "0L + (Integer.MIN_VALUE + Integer.MIN_VALUE)" yields // a different value than "0L + Integer.MIN_VALUE + Integer.MIN_VALUE" due // to integer promotion rules. if (parent instanceof JCConditional) { // This intentionally differs from Pretty, because Pretty appears buggy: // http://mail.openjdk.java.net/pipermail/compiler-dev/2013-September/007303.html JCConditional conditional = (JCConditional) parent; return TreeInfo.condPrec + ((conditional.cond == leaf) ? 1 : 0); } else if (parent instanceof JCAssign) { JCAssign assign = (JCAssign) parent; return TreeInfo.assignPrec + ((assign.lhs == leaf) ? 1 : 0); } else if (parent instanceof JCAssignOp) { JCAssignOp assignOp = (JCAssignOp) parent; return TreeInfo.assignopPrec + ((assignOp.lhs == leaf) ? 1 : 0); } else if (parent instanceof JCUnary) { return TreeInfo.opPrec(parent.getTag()); } else if (parent instanceof JCBinary) { JCBinary binary = (JCBinary) parent; return TreeInfo.opPrec(parent.getTag()) + ((binary.rhs == leaf) ? 1 : 0); } else if (parent instanceof JCTypeCast) { JCTypeCast typeCast = (JCTypeCast) parent; return (typeCast.expr == leaf) ? TreeInfo.prefixPrec : TreeInfo.noPrec; } else if (parent instanceof JCInstanceOf) { JCInstanceOf instanceOf = (JCInstanceOf) parent; return TreeInfo.ordPrec + ((instanceOf.clazz == leaf) ? 1 : 0); } else if (parent instanceof JCArrayAccess) { JCArrayAccess arrayAccess = (JCArrayAccess) parent; return (arrayAccess.indexed == leaf) ? TreeInfo.postfixPrec : TreeInfo.noPrec; } else if (parent instanceof JCFieldAccess) { JCFieldAccess fieldAccess = (JCFieldAccess) parent; return (fieldAccess.selected == leaf) ? TreeInfo.postfixPrec : TreeInfo.noPrec; } else { return TreeInfo.noPrec; } }
Example #18
Source File: HandleCleanup.java From EasyMPermission with MIT License | 4 votes |
@Override public void handle(AnnotationValues<Cleanup> annotation, JCAnnotation ast, JavacNode annotationNode) { handleFlagUsage(annotationNode, ConfigurationKeys.CLEANUP_FLAG_USAGE, "@Cleanup"); if (inNetbeansEditor(annotationNode)) return; deleteAnnotationIfNeccessary(annotationNode, Cleanup.class); String cleanupName = annotation.getInstance().value(); if (cleanupName.length() == 0) { annotationNode.addError("cleanupName cannot be the empty string."); return; } if (annotationNode.up().getKind() != Kind.LOCAL) { annotationNode.addError("@Cleanup is legal only on local variable declarations."); return; } JCVariableDecl decl = (JCVariableDecl)annotationNode.up().get(); if (decl.init == null) { annotationNode.addError("@Cleanup variable declarations need to be initialized."); return; } JavacNode ancestor = annotationNode.up().directUp(); JCTree blockNode = ancestor.get(); final List<JCStatement> statements; if (blockNode instanceof JCBlock) { statements = ((JCBlock)blockNode).stats; } else if (blockNode instanceof JCCase) { statements = ((JCCase)blockNode).stats; } else if (blockNode instanceof JCMethodDecl) { statements = ((JCMethodDecl)blockNode).body.stats; } else { annotationNode.addError("@Cleanup is legal only on a local variable declaration inside a block."); return; } boolean seenDeclaration = false; ListBuffer<JCStatement> newStatements = new ListBuffer<JCStatement>(); ListBuffer<JCStatement> tryBlock = new ListBuffer<JCStatement>(); for (JCStatement statement : statements) { if (!seenDeclaration) { if (statement == decl) seenDeclaration = true; newStatements.append(statement); } else { tryBlock.append(statement); } } if (!seenDeclaration) { annotationNode.addError("LOMBOK BUG: Can't find this local variable declaration inside its parent."); return; } doAssignmentCheck(annotationNode, tryBlock.toList(), decl.name); JavacTreeMaker maker = annotationNode.getTreeMaker(); JCFieldAccess cleanupMethod = maker.Select(maker.Ident(decl.name), annotationNode.toName(cleanupName)); List<JCStatement> cleanupCall = List.<JCStatement>of(maker.Exec( maker.Apply(List.<JCExpression>nil(), cleanupMethod, List.<JCExpression>nil()))); JCExpression preventNullAnalysis = preventNullAnalysis(maker, annotationNode, maker.Ident(decl.name)); JCBinary isNull = maker.Binary(CTC_NOT_EQUAL, preventNullAnalysis, maker.Literal(CTC_BOT, null)); JCIf ifNotNullCleanup = maker.If(isNull, maker.Block(0, cleanupCall), null); Context context = annotationNode.getContext(); JCBlock finalizer = recursiveSetGeneratedBy(maker.Block(0, List.<JCStatement>of(ifNotNullCleanup)), ast, context); newStatements.append(setGeneratedBy(maker.Try(setGeneratedBy(maker.Block(0, tryBlock.toList()), ast, context), List.<JCCatch>nil(), finalizer), ast, context)); if (blockNode instanceof JCBlock) { ((JCBlock)blockNode).stats = newStatements.toList(); } else if (blockNode instanceof JCCase) { ((JCCase)blockNode).stats = newStatements.toList(); } else if (blockNode instanceof JCMethodDecl) { ((JCMethodDecl)blockNode).body.stats = newStatements.toList(); } else throw new AssertionError("Should not get here"); ancestor.rebuild(); }
Example #19
Source File: CRTable.java From lua-for-android with BSD 3-Clause "New" or "Revised" License | 4 votes |
public void visitBinary(JCBinary tree) { SourceRange sr = new SourceRange(startPos(tree), endPos(tree)); sr.mergeWith(csp(tree.lhs)); sr.mergeWith(csp(tree.rhs)); result = sr; }
Example #20
Source File: JavacTreeMaker.java From EasyMPermission with MIT License | 4 votes |
public JCBinary Binary(TreeTag opcode, JCExpression lhs, JCExpression rhs) { return invoke(Binary, opcode.value, lhs, rhs); }
Example #21
Source File: LambdaToMethod.java From lua-for-android with BSD 3-Clause "New" or "Revised" License | 4 votes |
private JCExpression eqTest(Type argType, JCExpression arg1, JCExpression arg2) { JCBinary testExpr = make.Binary(JCTree.Tag.EQ, arg1, arg2); testExpr.operator = operators.resolveBinary(testExpr, JCTree.Tag.EQ, argType, argType); testExpr.setType(syms.booleanType); return testExpr; }
Example #22
Source File: Lower.java From lua-for-android with BSD 3-Clause "New" or "Revised" License | 4 votes |
/** * A statement of the form * * <pre> * for ( T v : arrayexpr ) stmt; * </pre> * * (where arrayexpr is of an array type) gets translated to * * <pre>{@code * for ( { arraytype #arr = arrayexpr; * int #len = array.length; * int #i = 0; }; * #i < #len; i$++ ) { * T v = arr$[#i]; * stmt; * } * }</pre> * * where #arr, #len, and #i are freshly named synthetic local variables. */ private void visitArrayForeachLoop(JCEnhancedForLoop tree) { make_at(tree.expr.pos()); VarSymbol arraycache = new VarSymbol(SYNTHETIC, names.fromString("arr" + target.syntheticNameChar()), tree.expr.type, currentMethodSym); JCStatement arraycachedef = make.VarDef(arraycache, tree.expr); VarSymbol lencache = new VarSymbol(SYNTHETIC, names.fromString("len" + target.syntheticNameChar()), syms.intType, currentMethodSym); JCStatement lencachedef = make. VarDef(lencache, make.Select(make.Ident(arraycache), syms.lengthVar)); VarSymbol index = new VarSymbol(SYNTHETIC, names.fromString("i" + target.syntheticNameChar()), syms.intType, currentMethodSym); JCVariableDecl indexdef = make.VarDef(index, make.Literal(INT, 0)); indexdef.init.type = indexdef.type = syms.intType.constType(0); List<JCStatement> loopinit = List.of(arraycachedef, lencachedef, indexdef); JCBinary cond = makeBinary(LT, make.Ident(index), make.Ident(lencache)); JCExpressionStatement step = make.Exec(makeUnary(PREINC, make.Ident(index))); Type elemtype = types.elemtype(tree.expr.type); JCExpression loopvarinit = make.Indexed(make.Ident(arraycache), make.Ident(index)).setType(elemtype); JCVariableDecl loopvardef = (JCVariableDecl)make.VarDef(tree.var.mods, tree.var.name, tree.var.vartype, loopvarinit).setType(tree.var.type); loopvardef.sym = tree.var.sym; JCBlock body = make. Block(0, List.of(loopvardef, tree.body)); result = translate(make. ForLoop(loopinit, cond, List.of(step), body)); patchTargets(body, tree, result); }
Example #23
Source File: TransTypes.java From lua-for-android with BSD 3-Clause "New" or "Revised" License | 4 votes |
public void visitBinary(JCBinary tree) { tree.lhs = translate(tree.lhs, tree.operator.type.getParameterTypes().head); tree.rhs = translate(tree.rhs, tree.operator.type.getParameterTypes().tail.head); result = tree; }
Example #24
Source File: JavacProcessingEnvironment.java From lua-for-android with BSD 3-Clause "New" or "Revised" License | 4 votes |
public void visitBinary(JCBinary node) { node.operator = null; super.visitBinary(node); }