Search in sources :

Example 36 with JCExpression

use of com.sun.tools.javac.tree.JCTree.JCExpression in project error-prone by google.

the class UTypeVarIdent method defaultAction.

@Override
protected Choice<Unifier> defaultAction(Tree target, Unifier unifier) {
    JCExpression expr = (JCExpression) target;
    Type targetType = expr.type;
    if (targetType == null) {
        return Choice.none();
    }
    @Nullable TypeWithExpression boundType = unifier.getBinding(key());
    if (boundType == null) {
        unifier.putBinding(key(), expr.accept(QUALIFIED_FROM_PACKAGE, null) ? TypeWithExpression.create(targetType) : /* use the ImportPolicy to refer to this type */
        TypeWithExpression.create(targetType, expr));
        return Choice.of(unifier);
    } else if (unifier.types().isSameType(targetType, boundType.type())) {
        return Choice.of(unifier);
    }
    return Choice.none();
}
Also used : Type(com.sun.tools.javac.code.Type) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) TypeWithExpression(com.google.errorprone.refaster.UTypeVar.TypeWithExpression) Nullable(javax.annotation.Nullable)

Example 37 with JCExpression

use of com.sun.tools.javac.tree.JCTree.JCExpression in project error-prone by google.

the class UUnary method inline.

@Override
public JCExpression inline(Inliner inliner) throws CouldNotResolveImportException {
    JCExpression expr = getExpression().inline(inliner);
    final TreeMaker maker = inliner.maker();
    if (getKind() == Kind.LOGICAL_COMPLEMENT) {
        return new TreeCopier<Void>(maker) {

            @SuppressWarnings("unchecked")
            // essentially depends on T being a superclass of JCExpression
            @Override
            public <T extends JCTree> T copy(T t, Void v) {
                if (t instanceof BinaryTree || t instanceof UnaryTree || t instanceof ConditionalExpressionTree) {
                    return super.copy(t, v);
                } else {
                    return (T) defaultNegation(t);
                }
            }

            public JCExpression defaultNegation(Tree expr) {
                return maker.Unary(JCTree.Tag.NOT, (JCExpression) expr);
            }

            @Override
            public JCExpression visitBinary(BinaryTree tree, Void v) {
                if (UBinary.DEMORGAN.containsKey(tree.getKind())) {
                    JCExpression negLeft = copy((JCExpression) tree.getLeftOperand());
                    JCExpression negRight = copy((JCExpression) tree.getRightOperand());
                    return maker.Binary(UBinary.OP_CODES.get(UBinary.DEMORGAN.get(tree.getKind())), negLeft, negRight);
                } else if (UBinary.NEGATION.containsKey(tree.getKind())) {
                    JCExpression left = (JCExpression) tree.getLeftOperand();
                    JCExpression right = (JCExpression) tree.getRightOperand();
                    return maker.Binary(UBinary.OP_CODES.get(UBinary.NEGATION.get(tree.getKind())), left, right);
                } else {
                    return defaultNegation(tree);
                }
            }

            @Override
            public JCExpression visitUnary(UnaryTree tree, Void v) {
                if (tree.getKind() == Kind.LOGICAL_COMPLEMENT) {
                    return (JCExpression) tree.getExpression();
                } else {
                    return defaultNegation(tree);
                }
            }

            @Override
            public JCConditional visitConditionalExpression(ConditionalExpressionTree tree, Void v) {
                return maker.Conditional((JCExpression) tree.getCondition(), copy((JCExpression) tree.getTrueExpression()), copy((JCExpression) tree.getFalseExpression()));
            }
        }.copy(expr);
    } else {
        return inliner.maker().Unary(UNARY_OP_CODES.get(getKind()), getExpression().inline(inliner));
    }
}
Also used : TreeMaker(com.sun.tools.javac.tree.TreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCConditional(com.sun.tools.javac.tree.JCTree.JCConditional) BinaryTree(com.sun.source.tree.BinaryTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) BinaryTree(com.sun.source.tree.BinaryTree) UnaryTree(com.sun.source.tree.UnaryTree) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.sun.source.tree.Tree) UnaryTree(com.sun.source.tree.UnaryTree)

Example 38 with JCExpression

use of com.sun.tools.javac.tree.JCTree.JCExpression in project error-prone by google.

the class Template method expectedTypes.

/**
   * Returns a list of the expected types to be matched.  This consists of the argument types from
   * the @BeforeTemplate method, concatenated with the return types of expression placeholders, 
   * sorted by the name of the placeholder method.
   *
   * @throws CouldNotResolveImportException if a referenced type could not be resolved
   */
protected List<Type> expectedTypes(Inliner inliner) throws CouldNotResolveImportException {
    ArrayList<Type> result = new ArrayList<>();
    ImmutableList<UType> types = expressionArgumentTypes().values().asList();
    ImmutableList<String> argNames = expressionArgumentTypes().keySet().asList();
    for (int i = 0; i < argNames.size(); i++) {
        String argName = argNames.get(i);
        Optional<JCExpression> singleBinding = inliner.getOptionalBinding(new UFreeIdent.Key(argName));
        if (!singleBinding.isPresent()) {
            Optional<java.util.List<JCExpression>> exprs = inliner.getOptionalBinding(new URepeated.Key(argName));
            if (!exprs.isPresent() || exprs.get().isEmpty()) {
                // It is a repeated template variable and matches no expressions.
                continue;
            }
        }
        result.add(types.get(i).inline(inliner));
    }
    for (PlaceholderExpressionKey key : Ordering.natural().immutableSortedCopy(Iterables.filter(inliner.bindings.keySet(), PlaceholderExpressionKey.class))) {
        result.add(key.method.returnType().inline(inliner));
    }
    return List.from(result);
}
Also used : ArrayList(java.util.ArrayList) MethodType(com.sun.tools.javac.code.Type.MethodType) Type(com.sun.tools.javac.code.Type) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) List(com.sun.tools.javac.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) PlaceholderExpressionKey(com.google.errorprone.refaster.PlaceholderMethod.PlaceholderExpressionKey)

Example 39 with JCExpression

use of com.sun.tools.javac.tree.JCTree.JCExpression in project error-prone by google.

the class Template method actualTypes.

/**
   * Returns a list of the actual types to be matched.  This consists of the types of the 
   * expressions bound to the @BeforeTemplate method parameters, concatenated with the types
   * of the expressions bound to expression placeholders, sorted by the name of the placeholder 
   * method.
   */
protected List<Type> actualTypes(Inliner inliner) {
    ArrayList<Type> result = new ArrayList<>();
    ImmutableList<String> argNames = expressionArgumentTypes().keySet().asList();
    for (int i = 0; i < expressionArgumentTypes().size(); i++) {
        String argName = argNames.get(i);
        Optional<JCExpression> singleBinding = inliner.getOptionalBinding(new UFreeIdent.Key(argName));
        if (singleBinding.isPresent()) {
            result.add(singleBinding.get().type);
        } else {
            Optional<java.util.List<JCExpression>> exprs = inliner.getOptionalBinding(new URepeated.Key(argName));
            if (exprs.isPresent() && !exprs.get().isEmpty()) {
                Type[] exprTys = new Type[exprs.get().size()];
                for (int j = 0; j < exprs.get().size(); j++) {
                    exprTys[j] = exprs.get().get(j).type;
                }
                // Get the least upper bound of the types of all expressions that the argument matches.
                // In the special case where exprs is empty, returns the "bottom" type, which is a
                // subtype of everything.
                result.add(inliner.types().lub(List.from(exprTys)));
            }
        }
    }
    for (PlaceholderExpressionKey key : Ordering.natural().immutableSortedCopy(Iterables.filter(inliner.bindings.keySet(), PlaceholderExpressionKey.class))) {
        result.add(inliner.getBinding(key).type);
    }
    return List.from(result);
}
Also used : ArrayList(java.util.ArrayList) MethodType(com.sun.tools.javac.code.Type.MethodType) Type(com.sun.tools.javac.code.Type) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) List(com.sun.tools.javac.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) PlaceholderExpressionKey(com.google.errorprone.refaster.PlaceholderMethod.PlaceholderExpressionKey)

Example 40 with JCExpression

use of com.sun.tools.javac.tree.JCTree.JCExpression in project error-prone by google.

the class UFreeIdent method defaultAction.

@Override
protected Choice<Unifier> defaultAction(Tree target, final Unifier unifier) {
    if (target instanceof JCExpression) {
        JCExpression expression = (JCExpression) target;
        JCExpression currentBinding = unifier.getBinding(key());
        // Check that the expression does not reference any template-local variables.
        boolean isGood = trueOrNull(new TreeScanner<Boolean, Void>() {

            @Override
            public Boolean reduce(@Nullable Boolean left, @Nullable Boolean right) {
                return trueOrNull(left) && trueOrNull(right);
            }

            @Override
            public Boolean visitIdentifier(IdentifierTree ident, Void v) {
                Symbol identSym = ASTHelpers.getSymbol(ident);
                for (ULocalVarIdent.Key key : Iterables.filter(unifier.getBindings().keySet(), ULocalVarIdent.Key.class)) {
                    if (identSym == unifier.getBinding(key).getSymbol()) {
                        return false;
                    }
                }
                return true;
            }
        }.scan(expression, null));
        if (!isGood) {
            return Choice.none();
        } else if (currentBinding == null) {
            unifier.putBinding(key(), expression);
            return Choice.of(unifier);
        } else if (currentBinding.toString().equals(expression.toString())) {
            // If it's the same code, treat it as the same expression.
            return Choice.of(unifier);
        }
    }
    return Choice.none();
}
Also used : JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) TreeScanner(com.sun.source.util.TreeScanner) Symbol(com.sun.tools.javac.code.Symbol) IdentifierTree(com.sun.source.tree.IdentifierTree) Nullable(javax.annotation.Nullable)

Aggregations

JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)311 JCTree (com.sun.tools.javac.tree.JCTree)95 Type (com.redhat.ceylon.model.typechecker.model.Type)85 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)81 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)67 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)59 ListBuffer (com.sun.tools.javac.util.ListBuffer)54 JCTypeParameter (com.sun.tools.javac.tree.JCTree.JCTypeParameter)39 Name (com.sun.tools.javac.util.Name)39 SyntheticName (com.redhat.ceylon.compiler.java.codegen.Naming.SyntheticName)37 TypeDeclaration (com.redhat.ceylon.model.typechecker.model.TypeDeclaration)35 TypedDeclaration (com.redhat.ceylon.model.typechecker.model.TypedDeclaration)34 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)34 JavacTreeMaker (lombok.javac.JavacTreeMaker)33 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)30 TypeParameter (com.redhat.ceylon.model.typechecker.model.TypeParameter)29 Declaration (com.redhat.ceylon.model.typechecker.model.Declaration)27 Function (com.redhat.ceylon.model.typechecker.model.Function)26 Parameter (com.redhat.ceylon.model.typechecker.model.Parameter)26 JCNewClass (com.sun.tools.javac.tree.JCTree.JCNewClass)26