Search in sources :

Example 46 with Expression

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

the class TreeConverter method convertSynchronized.

private TreeNode convertSynchronized(SynchronizedTree node, TreePath parent) {
    TreePath path = getTreePath(parent, node);
    Expression expr = convertWithoutParens(node.getExpression(), path);
    expr.setPosition(getPosition(node));
    return new SynchronizedStatement().setExpression(expr).setBody((Block) convert(node.getBlock(), path));
}
Also used : TreePath(com.sun.source.util.TreePath) 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) JCFunctionalExpression(com.sun.tools.javac.tree.JCTree.JCFunctionalExpression) LambdaExpression(com.google.devtools.j2objc.ast.LambdaExpression) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) 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) SynchronizedStatement(com.google.devtools.j2objc.ast.SynchronizedStatement)

Example 47 with Expression

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

the class ComplexExpressionExtractor method handleNode.

private void handleNode(Expression node, Collection<Expression> children) {
    if (node.getParent() instanceof Statement) {
        return;
    }
    int depth = 0;
    for (Expression child : children) {
        Integer childDepth = depths.get(child);
        depth = Math.max(depth, childDepth != null ? childDepth : 1);
    }
    if (depth >= maxDepth) {
        VariableElement newVar = GeneratedVariableElement.newLocalVar("complex$" + count++, node.getTypeMirror(), currentMethod);
        Statement newStmt = new VariableDeclarationStatement(newVar, node.copy());
        assert currentStatement != null;
        TreeUtil.insertBefore(currentStatement, newStmt);
        node.replaceWith(new SimpleName(newVar));
    } else {
        depths.put(node, depth + 1);
    }
}
Also used : Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) DoStatement(com.google.devtools.j2objc.ast.DoStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) WhileStatement(com.google.devtools.j2objc.ast.WhileStatement) IfStatement(com.google.devtools.j2objc.ast.IfStatement) Statement(com.google.devtools.j2objc.ast.Statement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement)

Example 48 with Expression

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

the class MetadataWriter method createAnnotations.

private Expression createAnnotations(List<Annotation> annotations) {
    // Group repeated annotations.
    LinkedHashMap<DeclaredType, List<Annotation>> groupedAnnotations = new LinkedHashMap<>();
    for (Annotation annotation : annotations) {
        DeclaredType type = annotation.getAnnotationMirror().getAnnotationType();
        groupedAnnotations.computeIfAbsent(type, k -> new ArrayList<>()).add(annotation);
    }
    List<Expression> expressions = new ArrayList<>();
    for (List<Annotation> group : groupedAnnotations.values()) {
        if (group.size() == 1) {
            expressions.add(translationUtil.createAnnotation(group.get(0).getAnnotationMirror()));
        } else {
            expressions.add(createContainerAnnotation(group));
        }
    }
    return translationUtil.createObjectArray(expressions, annotationArray);
}
Also used : Iterables(com.google.common.collect.Iterables) UnitTreeVisitor(com.google.devtools.j2objc.ast.UnitTreeVisitor) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) EnumDeclaration(com.google.devtools.j2objc.ast.EnumDeclaration) GeneratedTypeElement(com.google.devtools.j2objc.types.GeneratedTypeElement) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) TreeUtil(com.google.devtools.j2objc.ast.TreeUtil) Modifier(javax.lang.model.element.Modifier) FunctionElement(com.google.devtools.j2objc.types.FunctionElement) VariableElement(javax.lang.model.element.VariableElement) CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) TypeElement(javax.lang.model.element.TypeElement) EnumConstantDeclaration(com.google.devtools.j2objc.ast.EnumConstantDeclaration) AnnotationTypeMemberDeclaration(com.google.devtools.j2objc.ast.AnnotationTypeMemberDeclaration) AnnotationTypeDeclaration(com.google.devtools.j2objc.ast.AnnotationTypeDeclaration) Expression(com.google.devtools.j2objc.ast.Expression) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Strings(com.google.common.base.Strings) BodyDeclaration(com.google.devtools.j2objc.ast.BodyDeclaration) UnicodeUtils(com.google.devtools.j2objc.util.UnicodeUtils) Annotation(com.google.devtools.j2objc.ast.Annotation) FieldDeclaration(com.google.devtools.j2objc.ast.FieldDeclaration) Repeatable(java.lang.annotation.Repeatable) DeclaredType(javax.lang.model.type.DeclaredType) ElementUtil(com.google.devtools.j2objc.util.ElementUtil) ArrayType(javax.lang.model.type.ArrayType) TypeDeclaration(com.google.devtools.j2objc.ast.TypeDeclaration) NativeType(com.google.devtools.j2objc.types.NativeType) ExecutableElement(javax.lang.model.element.ExecutableElement) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) Element(javax.lang.model.element.Element) Block(com.google.devtools.j2objc.ast.Block) FunctionDeclaration(com.google.devtools.j2objc.ast.FunctionDeclaration) SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) Statement(com.google.devtools.j2objc.ast.Statement) Collectors(java.util.stream.Collectors) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) List(java.util.List) TypeMirror(javax.lang.model.type.TypeMirror) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration) TypeUtil(com.google.devtools.j2objc.util.TypeUtil) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) ErrorUtil(com.google.devtools.j2objc.util.ErrorUtil) Joiner(com.google.common.base.Joiner) CodeReferenceMap(com.google.devtools.j2objc.util.CodeReferenceMap) Expression(com.google.devtools.j2objc.ast.Expression) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Annotation(com.google.devtools.j2objc.ast.Annotation) DeclaredType(javax.lang.model.type.DeclaredType) LinkedHashMap(java.util.LinkedHashMap)

Example 49 with Expression

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

the class CastResolver method endVisit.

@Override
public void endVisit(CastExpression node) {
    TypeMirror type = node.getType().getTypeMirror();
    Expression expr = node.getExpression();
    TypeMirror exprType = expr.getTypeMirror();
    if (TypeUtil.isFloatingPoint(exprType)) {
        // Java wouldn't allow a cast from primitive to non-primitive.
        assert type.getKind().isPrimitive();
        switch(type.getKind()) {
            case LONG:
                node.replaceWith(rewriteFloatToIntegralCast(type, expr, "JreFpToLong", type));
                return;
            case CHAR:
                node.replaceWith(rewriteFloatToIntegralCast(type, expr, "JreFpToChar", type));
                return;
            case BYTE:
            case SHORT:
            case INT:
                node.replaceWith(rewriteFloatToIntegralCast(type, expr, "JreFpToInt", typeUtil.getInt()));
                return;
            // Fall through.
            default:
        }
    }
    // Lean on Java's type-checking.
    if (!type.getKind().isPrimitive() && typeUtil.isAssignable(exprType, typeUtil.erasure(type))) {
        node.replaceWith(TreeUtil.remove(expr));
        return;
    }
    FunctionInvocation castCheck = createCastCheck(type, expr);
    if (castCheck != null) {
        node.setExpression(castCheck);
    }
}
Also used : FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) TypeMirror(javax.lang.model.type.TypeMirror) CastExpression(com.google.devtools.j2objc.ast.CastExpression) InstanceofExpression(com.google.devtools.j2objc.ast.InstanceofExpression) Expression(com.google.devtools.j2objc.ast.Expression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression)

Example 50 with Expression

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

the class CastResolver method visit.

@Override
public boolean visit(IfStatement node) {
    // Adds a narrowed type to the narrowing map for a statement with
    // an instanceof expression where the body would have a redundant
    // cast check (since the instanceof test shows it's not necessary).
    Expression expr = node.getExpression();
    VariableElement var = getInstanceofVar(expr);
    if (var != null) {
        TypeMirror instanceofType = ((InstanceofExpression) expr).getRightOperand().getTypeMirror();
        narrowingMap.put(var, instanceofType);
    }
    return true;
}
Also used : CastExpression(com.google.devtools.j2objc.ast.CastExpression) InstanceofExpression(com.google.devtools.j2objc.ast.InstanceofExpression) Expression(com.google.devtools.j2objc.ast.Expression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) TypeMirror(javax.lang.model.type.TypeMirror) VariableElement(javax.lang.model.element.VariableElement)

Aggregations

Expression (com.google.devtools.j2objc.ast.Expression)139 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)103 ParenthesizedExpression (com.google.devtools.j2objc.ast.ParenthesizedExpression)95 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)89 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)78 CastExpression (com.google.devtools.j2objc.ast.CastExpression)70 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)70 ThisExpression (com.google.devtools.j2objc.ast.ThisExpression)64 VariableDeclarationExpression (com.google.devtools.j2objc.ast.VariableDeclarationExpression)58 InstanceofExpression (com.google.devtools.j2objc.ast.InstanceofExpression)45 TypeMirror (javax.lang.model.type.TypeMirror)41 VariableElement (javax.lang.model.element.VariableElement)40 LambdaExpression (com.google.devtools.j2objc.ast.LambdaExpression)36 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)34 SimpleName (com.google.devtools.j2objc.ast.SimpleName)29 FunctionalExpression (com.google.devtools.j2objc.ast.FunctionalExpression)24 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)22 NativeExpression (com.google.devtools.j2objc.ast.NativeExpression)22 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)22 TypeElement (javax.lang.model.element.TypeElement)21