Search in sources :

Example 61 with IdentifierTree

use of com.sun.source.tree.IdentifierTree in project st-js by st-js.

the class DefaultMemberSelectTemplate method visit.

@Override
public JS visit(WriterVisitor<JS> visitor, MemberSelectTree tree, GenerationContext<JS> context) {
    // this is only for fields. Methods are handled in MethodInvocationWriter
    TreeWrapper<IdentifierTree, JS> tw = context.getCurrentWrapper();
    Element element = tw.getElement();
    if (element == null || element.getKind() == ElementKind.PACKAGE) {
        // package names are ignored
        return null;
    }
    if (element.getKind() == ElementKind.CLASS) {
        if (tw.isGlobal()) {
            // global classes are ignored
            return null;
        } else {
            // Non global classes however, are not ignored and a translated taking the namespace into account
            return context.js().name(context.getNames().getTypeName(context, element, DependencyType.STATIC));
        }
    }
    JS target = getTarget(visitor, tree, context);
    if (GeneratorConstants.CLASS.equals(tree.getIdentifier().toString())) {
        // When ClassName.class &gt; ClassName
        return target;
    }
    return context.js().property(target, tree.getIdentifier().toString());
}
Also used : Element(javax.lang.model.element.Element) IdentifierTree(com.sun.source.tree.IdentifierTree)

Example 62 with IdentifierTree

use of com.sun.source.tree.IdentifierTree in project st-js by st-js.

the class IdentifierWriter method buildTemplateName.

private String buildTemplateName(IdentifierTree tree, GenerationContext<JS> context) {
    String name = tree.getName().toString();
    if (GeneratorConstants.THIS.equals(name)) {
        return "none";
    }
    TreeWrapper<IdentifierTree, JS> tw = context.getCurrentWrapper();
    Element def = tw.getElement();
    if (def.getKind() == ElementKind.FIELD) {
        String template = tw.getFieldTemplate();
        if (template != null) {
            return template;
        }
        return "none";
    }
    return "none";
}
Also used : Element(javax.lang.model.element.Element) IdentifierTree(com.sun.source.tree.IdentifierTree)

Example 63 with IdentifierTree

use of com.sun.source.tree.IdentifierTree in project checker-framework by typetools.

the class CFGTranslationPhaseOne method visitConditionalExpression.

@Override
public Node visitConditionalExpression(ConditionalExpressionTree tree, Void p) {
    // see JLS 15.25
    TypeMirror exprType = TreeUtils.typeOf(tree);
    Label trueStart = new Label();
    Label falseStart = new Label();
    Label merge = new Label();
    // create a synthetic variable for the value of the conditional expression
    VariableTree condExprVarTree = treeBuilder.buildVariableDecl(exprType, uniqueName("condExpr"), findOwner(), null);
    VariableDeclarationNode condExprVarNode = new VariableDeclarationNode(condExprVarTree);
    condExprVarNode.setInSource(false);
    extendWithNode(condExprVarNode);
    Node condition = unbox(scan(tree.getCondition(), p));
    ConditionalJump cjump = new ConditionalJump(trueStart, falseStart);
    extendWithExtendedNode(cjump);
    addLabelForNextNode(trueStart);
    ExpressionTree trueExprTree = tree.getTrueExpression();
    Node trueExprNode = scan(trueExprTree, p);
    trueExprNode = conditionalExprPromotion(trueExprNode, exprType);
    extendWithAssignmentForConditionalExpr(condExprVarTree, trueExprTree, trueExprNode);
    extendWithExtendedNode(new UnconditionalJump(merge, FlowRule.BOTH_TO_THEN));
    addLabelForNextNode(falseStart);
    ExpressionTree falseExprTree = tree.getFalseExpression();
    Node falseExprNode = scan(falseExprTree, p);
    falseExprNode = conditionalExprPromotion(falseExprNode, exprType);
    extendWithAssignmentForConditionalExpr(condExprVarTree, falseExprTree, falseExprNode);
    extendWithExtendedNode(new UnconditionalJump(merge, FlowRule.BOTH_TO_ELSE));
    addLabelForNextNode(merge);
    Pair<IdentifierTree, LocalVariableNode> treeAndLocalVarNode = extendWithVarUseNode(condExprVarTree);
    Node node = new TernaryExpressionNode(tree, condition, trueExprNode, falseExprNode, treeAndLocalVarNode.second);
    extendWithNode(node);
    return node;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) VariableDeclarationNode(org.checkerframework.dataflow.cfg.node.VariableDeclarationNode) NumericalMultiplicationNode(org.checkerframework.dataflow.cfg.node.NumericalMultiplicationNode) ArrayCreationNode(org.checkerframework.dataflow.cfg.node.ArrayCreationNode) ValueLiteralNode(org.checkerframework.dataflow.cfg.node.ValueLiteralNode) StringConversionNode(org.checkerframework.dataflow.cfg.node.StringConversionNode) UnsignedRightShiftNode(org.checkerframework.dataflow.cfg.node.UnsignedRightShiftNode) LeftShiftNode(org.checkerframework.dataflow.cfg.node.LeftShiftNode) PrimitiveTypeNode(org.checkerframework.dataflow.cfg.node.PrimitiveTypeNode) FloatLiteralNode(org.checkerframework.dataflow.cfg.node.FloatLiteralNode) LessThanNode(org.checkerframework.dataflow.cfg.node.LessThanNode) BitwiseOrNode(org.checkerframework.dataflow.cfg.node.BitwiseOrNode) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode) NarrowingConversionNode(org.checkerframework.dataflow.cfg.node.NarrowingConversionNode) EqualToNode(org.checkerframework.dataflow.cfg.node.EqualToNode) NumericalPlusNode(org.checkerframework.dataflow.cfg.node.NumericalPlusNode) ConditionalAndNode(org.checkerframework.dataflow.cfg.node.ConditionalAndNode) VariableDeclarationNode(org.checkerframework.dataflow.cfg.node.VariableDeclarationNode) ClassDeclarationNode(org.checkerframework.dataflow.cfg.node.ClassDeclarationNode) IntegerDivisionNode(org.checkerframework.dataflow.cfg.node.IntegerDivisionNode) AssertionErrorNode(org.checkerframework.dataflow.cfg.node.AssertionErrorNode) InstanceOfNode(org.checkerframework.dataflow.cfg.node.InstanceOfNode) BooleanLiteralNode(org.checkerframework.dataflow.cfg.node.BooleanLiteralNode) ThisNode(org.checkerframework.dataflow.cfg.node.ThisNode) AssignmentNode(org.checkerframework.dataflow.cfg.node.AssignmentNode) NullLiteralNode(org.checkerframework.dataflow.cfg.node.NullLiteralNode) ArrayTypeNode(org.checkerframework.dataflow.cfg.node.ArrayTypeNode) LambdaResultExpressionNode(org.checkerframework.dataflow.cfg.node.LambdaResultExpressionNode) IntegerRemainderNode(org.checkerframework.dataflow.cfg.node.IntegerRemainderNode) FieldAccessNode(org.checkerframework.dataflow.cfg.node.FieldAccessNode) ConditionalOrNode(org.checkerframework.dataflow.cfg.node.ConditionalOrNode) NotEqualNode(org.checkerframework.dataflow.cfg.node.NotEqualNode) BitwiseXorNode(org.checkerframework.dataflow.cfg.node.BitwiseXorNode) ArrayAccessNode(org.checkerframework.dataflow.cfg.node.ArrayAccessNode) ExplicitThisNode(org.checkerframework.dataflow.cfg.node.ExplicitThisNode) StringConcatenateNode(org.checkerframework.dataflow.cfg.node.StringConcatenateNode) NullChkNode(org.checkerframework.dataflow.cfg.node.NullChkNode) CharacterLiteralNode(org.checkerframework.dataflow.cfg.node.CharacterLiteralNode) FloatingDivisionNode(org.checkerframework.dataflow.cfg.node.FloatingDivisionNode) FunctionalInterfaceNode(org.checkerframework.dataflow.cfg.node.FunctionalInterfaceNode) StringConcatenateAssignmentNode(org.checkerframework.dataflow.cfg.node.StringConcatenateAssignmentNode) TypeCastNode(org.checkerframework.dataflow.cfg.node.TypeCastNode) MethodAccessNode(org.checkerframework.dataflow.cfg.node.MethodAccessNode) WideningConversionNode(org.checkerframework.dataflow.cfg.node.WideningConversionNode) LongLiteralNode(org.checkerframework.dataflow.cfg.node.LongLiteralNode) MarkerNode(org.checkerframework.dataflow.cfg.node.MarkerNode) ImplicitThisNode(org.checkerframework.dataflow.cfg.node.ImplicitThisNode) FloatingRemainderNode(org.checkerframework.dataflow.cfg.node.FloatingRemainderNode) ClassNameNode(org.checkerframework.dataflow.cfg.node.ClassNameNode) ObjectCreationNode(org.checkerframework.dataflow.cfg.node.ObjectCreationNode) PackageNameNode(org.checkerframework.dataflow.cfg.node.PackageNameNode) DoubleLiteralNode(org.checkerframework.dataflow.cfg.node.DoubleLiteralNode) SuperNode(org.checkerframework.dataflow.cfg.node.SuperNode) IntegerLiteralNode(org.checkerframework.dataflow.cfg.node.IntegerLiteralNode) SignedRightShiftNode(org.checkerframework.dataflow.cfg.node.SignedRightShiftNode) ThrowNode(org.checkerframework.dataflow.cfg.node.ThrowNode) GreaterThanOrEqualNode(org.checkerframework.dataflow.cfg.node.GreaterThanOrEqualNode) StringLiteralNode(org.checkerframework.dataflow.cfg.node.StringLiteralNode) TernaryExpressionNode(org.checkerframework.dataflow.cfg.node.TernaryExpressionNode) BitwiseAndNode(org.checkerframework.dataflow.cfg.node.BitwiseAndNode) ParameterizedTypeNode(org.checkerframework.dataflow.cfg.node.ParameterizedTypeNode) CaseNode(org.checkerframework.dataflow.cfg.node.CaseNode) SwitchExpressionNode(org.checkerframework.dataflow.cfg.node.SwitchExpressionNode) NumericalAdditionNode(org.checkerframework.dataflow.cfg.node.NumericalAdditionNode) NumericalSubtractionNode(org.checkerframework.dataflow.cfg.node.NumericalSubtractionNode) BitwiseComplementNode(org.checkerframework.dataflow.cfg.node.BitwiseComplementNode) ConditionalNotNode(org.checkerframework.dataflow.cfg.node.ConditionalNotNode) NumericalMinusNode(org.checkerframework.dataflow.cfg.node.NumericalMinusNode) ReturnNode(org.checkerframework.dataflow.cfg.node.ReturnNode) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) GreaterThanNode(org.checkerframework.dataflow.cfg.node.GreaterThanNode) LessThanOrEqualNode(org.checkerframework.dataflow.cfg.node.LessThanOrEqualNode) SynchronizedNode(org.checkerframework.dataflow.cfg.node.SynchronizedNode) Node(org.checkerframework.dataflow.cfg.node.Node) VariableTree(com.sun.source.tree.VariableTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) ExpressionTree(com.sun.source.tree.ExpressionTree) IdentifierTree(com.sun.source.tree.IdentifierTree) TernaryExpressionNode(org.checkerframework.dataflow.cfg.node.TernaryExpressionNode) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode)

Example 64 with IdentifierTree

use of com.sun.source.tree.IdentifierTree in project checker-framework by typetools.

the class JavaExpression method fromTree.

/**
 * Converts a javac {@link ExpressionTree} to a CF JavaExpression. The result might contain {@link
 * Unknown}.
 *
 * <p>We ignore operations such as widening and narrowing when computing the JavaExpression.
 *
 * @param tree a javac tree
 * @return a JavaExpression for the given javac tree
 */
public static JavaExpression fromTree(ExpressionTree tree) {
    JavaExpression result;
    switch(tree.getKind()) {
        case ARRAY_ACCESS:
            ArrayAccessTree a = (ArrayAccessTree) tree;
            JavaExpression arrayAccessExpression = fromTree(a.getExpression());
            JavaExpression index = fromTree(a.getIndex());
            result = new ArrayAccess(TreeUtils.typeOf(a), arrayAccessExpression, index);
            break;
        case BOOLEAN_LITERAL:
        case CHAR_LITERAL:
        case DOUBLE_LITERAL:
        case FLOAT_LITERAL:
        case INT_LITERAL:
        case LONG_LITERAL:
        case NULL_LITERAL:
        case STRING_LITERAL:
            LiteralTree vn = (LiteralTree) tree;
            result = new ValueLiteral(TreeUtils.typeOf(tree), vn.getValue());
            break;
        case NEW_ARRAY:
            NewArrayTree newArrayTree = (NewArrayTree) tree;
            List<@Nullable JavaExpression> dimensions;
            if (newArrayTree.getDimensions() == null) {
                dimensions = Collections.emptyList();
            } else {
                dimensions = new ArrayList<>(newArrayTree.getDimensions().size());
                for (ExpressionTree dimension : newArrayTree.getDimensions()) {
                    dimensions.add(fromTree(dimension));
                }
            }
            List<JavaExpression> initializers;
            if (newArrayTree.getInitializers() == null) {
                initializers = Collections.emptyList();
            } else {
                initializers = new ArrayList<>(newArrayTree.getInitializers().size());
                for (ExpressionTree initializer : newArrayTree.getInitializers()) {
                    initializers.add(fromTree(initializer));
                }
            }
            result = new ArrayCreation(TreeUtils.typeOf(tree), dimensions, initializers);
            break;
        case METHOD_INVOCATION:
            MethodInvocationTree mn = (MethodInvocationTree) tree;
            assert TreeUtils.isUseOfElement(mn) : "@AssumeAssertion(nullness): tree kind";
            ExecutableElement invokedMethod = TreeUtils.elementFromUse(mn);
            // Note that the method might be nondeterministic.
            List<JavaExpression> parameters = CollectionsPlume.mapList(JavaExpression::fromTree, mn.getArguments());
            JavaExpression methodReceiver;
            if (ElementUtils.isStatic(invokedMethod)) {
                @SuppressWarnings(// enclosingTypeElement(ExecutableElement): @NonNull
                "nullness:assignment") @NonNull TypeElement methodType = ElementUtils.enclosingTypeElement(invokedMethod);
                methodReceiver = new ClassName(methodType.asType());
            } else {
                methodReceiver = getReceiver(mn);
            }
            TypeMirror resultType = TreeUtils.typeOf(mn);
            result = new MethodCall(resultType, invokedMethod, methodReceiver, parameters);
            break;
        case MEMBER_SELECT:
            result = fromMemberSelect((MemberSelectTree) tree);
            break;
        case IDENTIFIER:
            IdentifierTree identifierTree = (IdentifierTree) tree;
            TypeMirror typeOfId = TreeUtils.typeOf(identifierTree);
            Name identifierName = identifierTree.getName();
            if (identifierName.contentEquals("this") || identifierName.contentEquals("super")) {
                result = new ThisReference(typeOfId);
                break;
            }
            assert TreeUtils.isUseOfElement(identifierTree) : "@AssumeAssertion(nullness): tree kind";
            Element ele = TreeUtils.elementFromUse(identifierTree);
            if (ElementUtils.isTypeElement(ele)) {
                result = new ClassName(ele.asType());
                break;
            }
            result = fromVariableElement(typeOfId, ele);
            break;
        case UNARY_PLUS:
            return fromTree(((UnaryTree) tree).getExpression());
        case BITWISE_COMPLEMENT:
        case LOGICAL_COMPLEMENT:
        case POSTFIX_DECREMENT:
        case POSTFIX_INCREMENT:
        case PREFIX_DECREMENT:
        case PREFIX_INCREMENT:
        case UNARY_MINUS:
            JavaExpression operand = fromTree(((UnaryTree) tree).getExpression());
            return new UnaryOperation(TreeUtils.typeOf(tree), tree.getKind(), operand);
        case CONDITIONAL_AND:
        case CONDITIONAL_OR:
        case DIVIDE:
        case EQUAL_TO:
        case GREATER_THAN:
        case GREATER_THAN_EQUAL:
        case LEFT_SHIFT:
        case LESS_THAN:
        case LESS_THAN_EQUAL:
        case MINUS:
        case MULTIPLY:
        case NOT_EQUAL_TO:
        case OR:
        case PLUS:
        case REMAINDER:
        case RIGHT_SHIFT:
        case UNSIGNED_RIGHT_SHIFT:
        case XOR:
            BinaryTree binaryTree = (BinaryTree) tree;
            JavaExpression left = fromTree(binaryTree.getLeftOperand());
            JavaExpression right = fromTree(binaryTree.getRightOperand());
            return new BinaryOperation(TreeUtils.typeOf(tree), tree.getKind(), left, right);
        default:
            result = null;
    }
    if (result == null) {
        result = new Unknown(tree);
    }
    return result;
}
Also used : ArrayAccessTree(com.sun.source.tree.ArrayAccessTree) ExecutableElement(javax.lang.model.element.ExecutableElement) MemberSelectTree(com.sun.source.tree.MemberSelectTree) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) BinaryTree(com.sun.source.tree.BinaryTree) IdentifierTree(com.sun.source.tree.IdentifierTree) LiteralTree(com.sun.source.tree.LiteralTree) Name(javax.lang.model.element.Name) TypeMirror(javax.lang.model.type.TypeMirror) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) NonNull(org.checkerframework.checker.nullness.qual.NonNull) ExpressionTree(com.sun.source.tree.ExpressionTree) TypeElement(javax.lang.model.element.TypeElement) NewArrayTree(com.sun.source.tree.NewArrayTree)

Example 65 with IdentifierTree

use of com.sun.source.tree.IdentifierTree in project checker-framework by typetools.

the class TreeUtils method isMethodAccess.

/**
 * Determine whether {@code tree} refers to a method element, such as.
 *
 * <pre>
 *   <em>m</em>(...)
 *   <em>obj</em> . <em>m</em>(...)
 * </pre>
 *
 * @return true iff if tree is a method access expression (implicit or explicit)
 */
public static boolean isMethodAccess(Tree tree) {
    if (tree.getKind() == Tree.Kind.MEMBER_SELECT) {
        // explicit method access
        MemberSelectTree memberSelect = (MemberSelectTree) tree;
        assert isUseOfElement(memberSelect) : "@AssumeAssertion(nullness): tree kind";
        Element el = TreeUtils.elementFromUse(memberSelect);
        return el.getKind() == ElementKind.METHOD || el.getKind() == ElementKind.CONSTRUCTOR;
    } else if (tree.getKind() == Tree.Kind.IDENTIFIER) {
        // implicit method access
        IdentifierTree ident = (IdentifierTree) tree;
        // The field "super" and "this" are also legal methods
        if (ident.getName().contentEquals("super") || ident.getName().contentEquals("this")) {
            return true;
        }
        assert isUseOfElement(ident) : "@AssumeAssertion(nullness): tree kind";
        Element el = TreeUtils.elementFromUse(ident);
        return el.getKind() == ElementKind.METHOD || el.getKind() == ElementKind.CONSTRUCTOR;
    }
    return false;
}
Also used : MemberSelectTree(com.sun.source.tree.MemberSelectTree) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) IdentifierTree(com.sun.source.tree.IdentifierTree)

Aggregations

IdentifierTree (com.sun.source.tree.IdentifierTree)82 ExpressionTree (com.sun.source.tree.ExpressionTree)41 MemberSelectTree (com.sun.source.tree.MemberSelectTree)36 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)28 Element (javax.lang.model.element.Element)24 Tree (com.sun.source.tree.Tree)21 ExecutableElement (javax.lang.model.element.ExecutableElement)18 VariableTree (com.sun.source.tree.VariableTree)17 TypeElement (javax.lang.model.element.TypeElement)16 MethodTree (com.sun.source.tree.MethodTree)13 VariableElement (javax.lang.model.element.VariableElement)13 ClassTree (com.sun.source.tree.ClassTree)12 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)11 ArrayAccessTree (com.sun.source.tree.ArrayAccessTree)10 NewClassTree (com.sun.source.tree.NewClassTree)10 AssignmentTree (com.sun.source.tree.AssignmentTree)9 BinaryTree (com.sun.source.tree.BinaryTree)9 LiteralTree (com.sun.source.tree.LiteralTree)8 StatementTree (com.sun.source.tree.StatementTree)8 Symbol (com.sun.tools.javac.code.Symbol)8