Search in sources :

Example 16 with Statement

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

the class GwtConverter method visit.

@Override
public boolean visit(IfStatement node) {
    if (isGwtTest(node.getExpression())) {
        if (node.getElseStatement() != null) {
            // Replace this node with the else statement.
            Statement replacement = TreeUtil.remove(node.getElseStatement());
            node.replaceWith(replacement);
            replacement.accept(this);
        } else {
            // with an empty statement.
            if (node.getParent() instanceof Block) {
                node.remove();
            } else {
                node.replaceWith(new EmptyStatement());
            }
        }
        return false;
    }
    return true;
}
Also used : IfStatement(com.google.devtools.j2objc.ast.IfStatement) Statement(com.google.devtools.j2objc.ast.Statement) EmptyStatement(com.google.devtools.j2objc.ast.EmptyStatement) EmptyStatement(com.google.devtools.j2objc.ast.EmptyStatement) Block(com.google.devtools.j2objc.ast.Block)

Example 17 with Statement

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

the class JavaCloneWriter method getFieldAdjustments.

private List<Statement> getFieldAdjustments(TypeDeclaration node, VariableElement originalVar) {
    List<Statement> adjustments = Lists.newArrayList();
    for (VariableDeclarationFragment decl : TreeUtil.getAllFields(node)) {
        VariableElement var = decl.getVariableElement();
        if (ElementUtil.isStatic(var) || var.asType().getKind().isPrimitive()) {
            continue;
        }
        boolean isWeak = ElementUtil.isWeakReference(var);
        boolean isVolatile = ElementUtil.isVolatile(var);
        if (isVolatile) {
            adjustments.add(createVolatileCloneStatement(var, originalVar, isWeak));
        } else if (isWeak) {
            adjustments.add(createReleaseStatement(var));
        }
    }
    return adjustments;
}
Also used : ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) Statement(com.google.devtools.j2objc.ast.Statement) VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement)

Example 18 with Statement

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

the class JavaCloneWriter method endVisit.

@Override
public void endVisit(TypeDeclaration node) {
    TypeElement type = node.getTypeElement();
    VariableElement originalVar = GeneratedVariableElement.newParameter("original", type.asType(), null);
    List<Statement> adjustments = getFieldAdjustments(node, originalVar);
    if (adjustments.isEmpty()) {
        return;
    }
    TypeMirror voidType = typeUtil.getVoid();
    ExecutableElement javaCloneElement = GeneratedExecutableElement.newMethodWithSelector(JAVA_CLONE_METHOD, voidType, type).addParameter(originalVar);
    MethodDeclaration declaration = new MethodDeclaration(javaCloneElement);
    declaration.setHasDeclaration(false);
    node.addBodyDeclaration(declaration);
    declaration.addParameter(new SingleVariableDeclaration(originalVar));
    Block body = new Block();
    declaration.setBody(body);
    List<Statement> statements = body.getStatements();
    ExecutableElement javaCloneSuperElement = GeneratedExecutableElement.newMethodWithSelector(JAVA_CLONE_METHOD, voidType, typeUtil.getJavaObject());
    SuperMethodInvocation superCall = new SuperMethodInvocation(new ExecutablePair(javaCloneSuperElement));
    superCall.addArgument(new SimpleName(originalVar));
    statements.add(new ExpressionStatement(superCall));
    statements.addAll(adjustments);
}
Also used : ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) TypeElement(javax.lang.model.element.TypeElement) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) Statement(com.google.devtools.j2objc.ast.Statement) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) SuperMethodInvocation(com.google.devtools.j2objc.ast.SuperMethodInvocation) TypeMirror(javax.lang.model.type.TypeMirror) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) Block(com.google.devtools.j2objc.ast.Block)

Example 19 with Statement

use of com.google.devtools.j2objc.ast.Statement 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 20 with Statement

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

the class ConstantBranchPruner method endVisit.

@Override
public void endVisit(IfStatement node) {
    Expression expr = node.getExpression();
    Boolean value = getKnownValue(expr);
    if (value != null) {
        Statement sideEffects = getSideEffects(expr);
        if (sideEffects != null) {
            TreeUtil.insertBefore(node, sideEffects);
        }
        if (value) {
            node.replaceWith(TreeUtil.remove(node.getThenStatement()));
        } else if (node.getElseStatement() != null) {
            node.replaceWith(TreeUtil.remove(node.getElseStatement()));
        } else {
            node.remove();
        }
    }
}
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) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) IfStatement(com.google.devtools.j2objc.ast.IfStatement) Statement(com.google.devtools.j2objc.ast.Statement) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) WhileStatement(com.google.devtools.j2objc.ast.WhileStatement)

Aggregations

Statement (com.google.devtools.j2objc.ast.Statement)40 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)23 ForStatement (com.google.devtools.j2objc.ast.ForStatement)22 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)22 Block (com.google.devtools.j2objc.ast.Block)21 IfStatement (com.google.devtools.j2objc.ast.IfStatement)20 WhileStatement (com.google.devtools.j2objc.ast.WhileStatement)17 EnhancedForStatement (com.google.devtools.j2objc.ast.EnhancedForStatement)15 ReturnStatement (com.google.devtools.j2objc.ast.ReturnStatement)15 SwitchStatement (com.google.devtools.j2objc.ast.SwitchStatement)15 DoStatement (com.google.devtools.j2objc.ast.DoStatement)14 BreakStatement (com.google.devtools.j2objc.ast.BreakStatement)13 ThrowStatement (com.google.devtools.j2objc.ast.ThrowStatement)13 Expression (com.google.devtools.j2objc.ast.Expression)12 LabeledStatement (com.google.devtools.j2objc.ast.LabeledStatement)12 SimpleName (com.google.devtools.j2objc.ast.SimpleName)12 EmptyStatement (com.google.devtools.j2objc.ast.EmptyStatement)11 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)11 VariableElement (javax.lang.model.element.VariableElement)11 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)9