Search in sources :

Example 1 with SourcePosition

use of com.google.devtools.j2objc.ast.SourcePosition in project j2objc by google.

the class TreeConverter method convertMemberReference.

private TreeNode convertMemberReference(JCTree.JCMemberReference node) {
    Element element = node.sym;
    SourcePosition pos = getPosition(node);
    if (ElementUtil.isConstructor(element)) {
        CreationReference newNode = new CreationReference();
        convertMethodReference(node, newNode);
        return newNode.setType(Type.newType(nameType(node.expr)));
    }
    if (node.hasKind(JCTree.JCMemberReference.ReferenceKind.SUPER)) {
        SuperMethodReference newNode = new SuperMethodReference();
        convertMethodReference(node, newNode);
        if (node.getQualifierExpression().getKind() == Kind.IDENTIFIER) {
            // super::foo
            return newNode;
        } else {
            // Qualifier expression is <name>."super", so it's always a JCFieldAccess.
            JCTree.JCFieldAccess expr = (JCTree.JCFieldAccess) node.getQualifierExpression();
            return newNode.setQualifier(convertSimpleName(nameSymbol(expr.selected), expr.type, getPosition(expr.selected)));
        }
    }
    if (node.hasKind(JCTree.JCMemberReference.ReferenceKind.UNBOUND) || node.hasKind(JCTree.JCMemberReference.ReferenceKind.STATIC)) {
        TypeMethodReference newNode = new TypeMethodReference();
        convertMethodReference(node, newNode);
        return newNode.setName(convertSimpleName(node.sym, node.type, pos)).setType(convertType(node.type, pos, false));
    }
    ExpressionMethodReference newNode = new ExpressionMethodReference();
    convertMethodReference(node, newNode);
    return newNode.setName(convertSimpleName(node.sym, node.type, pos)).setExpression((Expression) convert(node.getQualifierExpression()));
}
Also used : ExpressionMethodReference(com.google.devtools.j2objc.ast.ExpressionMethodReference) Element(javax.lang.model.element.Element) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) SourcePosition(com.google.devtools.j2objc.ast.SourcePosition) CreationReference(com.google.devtools.j2objc.ast.CreationReference) JCTree(com.sun.tools.javac.tree.JCTree) TypeMethodReference(com.google.devtools.j2objc.ast.TypeMethodReference) SuperMethodReference(com.google.devtools.j2objc.ast.SuperMethodReference)

Example 2 with SourcePosition

use of com.google.devtools.j2objc.ast.SourcePosition in project j2objc by google.

the class TreeConverter method convertSingleVariable.

private TreeNode convertSingleVariable(JCTree.JCVariableDecl node) {
    VarSymbol var = node.sym;
    SourcePosition pos = getPosition(node);
    boolean isVarargs = (node.sym.flags() & Flags.VARARGS) > 0;
    Type newType = convertType(var.asType(), pos, isVarargs);
    return new SingleVariableDeclaration().setType(newType).setIsVarargs(isVarargs).setAnnotations(convertAnnotations(node.getModifiers())).setVariableElement(var).setInitializer((Expression) convert(node.getInitializer()));
}
Also used : ParameterizedType(com.google.devtools.j2objc.ast.ParameterizedType) Type(com.google.devtools.j2objc.ast.Type) SimpleType(com.google.devtools.j2objc.ast.SimpleType) DeclaredType(javax.lang.model.type.DeclaredType) ArrayType(com.google.devtools.j2objc.ast.ArrayType) PrimitiveType(com.google.devtools.j2objc.ast.PrimitiveType) UnionType(com.google.devtools.j2objc.ast.UnionType) ExecutableType(javax.lang.model.type.ExecutableType) SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) SourcePosition(com.google.devtools.j2objc.ast.SourcePosition) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Example 3 with SourcePosition

use of com.google.devtools.j2objc.ast.SourcePosition in project j2objc by google.

the class TreeConverter method convertFieldAccess.

private TreeNode convertFieldAccess(JCTree.JCFieldAccess node) {
    String fieldName = node.name.toString();
    SourcePosition pos = getPosition(node);
    JCTree.JCExpression selected = node.getExpression();
    if (fieldName.equals("this")) {
        return new ThisExpression().setQualifier((Name) convert(selected)).setTypeMirror(node.sym.asType());
    }
    if ("super".equals(getMemberName(selected))) {
        SuperFieldAccess newNode = new SuperFieldAccess().setVariableElement((VariableElement) node.sym).setName(convertSimpleName(node.sym, node.type, pos));
        if (selected.getKind() == Kind.MEMBER_SELECT) {
            newNode.setQualifier((Name) convert(((JCTree.JCFieldAccess) selected).getExpression()));
        }
        return newNode;
    }
    if (node.getIdentifier().toString().equals("class")) {
        return new TypeLiteral(node.type).setType((Type) convertType(selected.type, pos, false).setPosition(getPosition(node)));
    }
    if (selected.getKind() == Kind.IDENTIFIER && (!node.sym.getKind().isField() || ElementUtil.isConstant((VariableElement) node.sym))) {
        if (selected.toString().equals("this")) {
            // Just return the constant.
            return new SimpleName(node.sym);
        }
        JCIdent ident = (JCTree.JCIdent) selected;
        return new QualifiedName().setName(convertSimpleName(node.sym, node.type, pos)).setQualifier(convertSimpleName(ident.sym, ident.type, pos)).setElement(node.sym);
    }
    if (selected.getKind() == Kind.MEMBER_SELECT) {
        TreeNode newSelected = convertFieldAccess((JCTree.JCFieldAccess) selected).setPosition(pos);
        if (newSelected.getKind() == TreeNode.Kind.QUALIFIED_NAME) {
            return new QualifiedName().setName(convertSimpleName(node.sym, node.type, pos)).setQualifier((QualifiedName) newSelected).setElement(node.sym);
        }
    }
    if (ElementUtil.isConstant((VariableElement) node.sym) && ElementUtil.isStatic(node.sym) && !(selected.getKind() == Kind.METHOD_INVOCATION)) {
        return new QualifiedName().setName(convertSimpleName(node.sym, node.type, pos)).setQualifier((Name) convert(selected)).setElement(node.sym);
    }
    return new FieldAccess().setVariableElement((VariableElement) node.sym).setExpression((Expression) convert(selected)).setName(convertSimpleName(node.sym, node.type, pos).setTypeMirror(node.type));
}
Also used : JCIdent(com.sun.tools.javac.tree.JCTree.JCIdent) SimpleName(com.google.devtools.j2objc.ast.SimpleName) QualifiedName(com.google.devtools.j2objc.ast.QualifiedName) JCTree(com.sun.tools.javac.tree.JCTree) SuperFieldAccess(com.google.devtools.j2objc.ast.SuperFieldAccess) VariableElement(javax.lang.model.element.VariableElement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) Name(com.google.devtools.j2objc.ast.Name) QualifiedName(com.google.devtools.j2objc.ast.QualifiedName) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) TypeLiteral(com.google.devtools.j2objc.ast.TypeLiteral) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) InstanceofExpression(com.google.devtools.j2objc.ast.InstanceofExpression) Expression(com.google.devtools.j2objc.ast.Expression) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) LambdaExpression(com.google.devtools.j2objc.ast.LambdaExpression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) FunctionalExpression(com.google.devtools.j2objc.ast.FunctionalExpression) CastExpression(com.google.devtools.j2objc.ast.CastExpression) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) TreeNode(com.google.devtools.j2objc.ast.TreeNode) SourcePosition(com.google.devtools.j2objc.ast.SourcePosition) FieldAccess(com.google.devtools.j2objc.ast.FieldAccess) SuperFieldAccess(com.google.devtools.j2objc.ast.SuperFieldAccess)

Example 4 with SourcePosition

use of com.google.devtools.j2objc.ast.SourcePosition in project j2objc by google.

the class TreeConverter method getPosition.

private static SourcePosition getPosition(ASTNode jdtNode) {
    int startPosition = jdtNode.getStartPosition();
    int length = jdtNode.getLength();
    ASTNode root = jdtNode.getRoot();
    if (root instanceof org.eclipse.jdt.core.dom.CompilationUnit) {
        int line = ((org.eclipse.jdt.core.dom.CompilationUnit) root).getLineNumber(startPosition);
        return new SourcePosition(startPosition, length, line);
    } else {
        return new SourcePosition(startPosition, length);
    }
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) SourcePosition(com.google.devtools.j2objc.ast.SourcePosition) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Aggregations

SourcePosition (com.google.devtools.j2objc.ast.SourcePosition)4 JCTree (com.sun.tools.javac.tree.JCTree)2 VariableElement (javax.lang.model.element.VariableElement)2 ArrayType (com.google.devtools.j2objc.ast.ArrayType)1 CastExpression (com.google.devtools.j2objc.ast.CastExpression)1 CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)1 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)1 CreationReference (com.google.devtools.j2objc.ast.CreationReference)1 Expression (com.google.devtools.j2objc.ast.Expression)1 ExpressionMethodReference (com.google.devtools.j2objc.ast.ExpressionMethodReference)1 FieldAccess (com.google.devtools.j2objc.ast.FieldAccess)1 FunctionalExpression (com.google.devtools.j2objc.ast.FunctionalExpression)1 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)1 InstanceofExpression (com.google.devtools.j2objc.ast.InstanceofExpression)1 LambdaExpression (com.google.devtools.j2objc.ast.LambdaExpression)1 Name (com.google.devtools.j2objc.ast.Name)1 ParameterizedType (com.google.devtools.j2objc.ast.ParameterizedType)1 ParenthesizedExpression (com.google.devtools.j2objc.ast.ParenthesizedExpression)1 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)1 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)1