Search in sources :

Example 31 with SimpleName

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

the class UnsequencedExpressionRewriter method visit.

@Override
public boolean visit(AssertStatement node) {
    Expression expr = node.getExpression();
    visitAndExtract(expr, node);
    Expression msg = node.getMessage();
    if (msg != null) {
        newExpression(msg);
        msg.accept(this);
        List<VariableAccess> toExtract = getUnsequencedAccesses();
        if (!toExtract.isEmpty()) {
            // If the message expression needs any extraction, then we first extract
            // the entire boolean expression to preserve ordering between the two.
            VariableElement exprVar = GeneratedVariableElement.newLocalVar("unseq$" + count++, expr.getTypeMirror(), currentMethod);
            TreeUtil.insertBefore(node, new VariableDeclarationStatement(exprVar, node.getExpression().copy()));
            node.setExpression(new SimpleName(exprVar));
            extractOrderedAccesses(TreeUtil.asStatementList(node).subList(0, 0), currentTopNode, toExtract);
        }
    }
    return false;
}
Also used : PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) CommaExpression(com.google.devtools.j2objc.ast.CommaExpression) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) 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 32 with SimpleName

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

the class UnsequencedExpressionRewriter method extractConditionalExpression.

private void extractConditionalExpression(List<Statement> stmtList, ConditionalExpression conditional, List<VariableAccess> toExtract) {
    Expression condition = conditional.getExpression();
    Expression thenExpr = conditional.getThenExpression();
    Expression elseExpr = conditional.getElseExpression();
    List<VariableAccess> conditionAccesses = Lists.newArrayList();
    List<VariableAccess> thenAccesses = Lists.newArrayList();
    List<VariableAccess> elseAccesses = Lists.newArrayList();
    boolean needsExtraction = false;
    for (VariableAccess access : toExtract) {
        TreeNode node = access.expression;
        while (node.getParent() != conditional) {
            node = node.getParent();
        }
        if (node == condition) {
            conditionAccesses.add(access);
        } else if (node == thenExpr) {
            thenAccesses.add(access);
        } else if (node == elseExpr) {
            elseAccesses.add(access);
        } else {
            throw new AssertionError();
        }
        if (node != condition) {
            // We need to extract an if-statement if there is an access that
            // executes conditionally.
            needsExtraction = true;
        }
    }
    extractOrderedAccesses(stmtList, condition, conditionAccesses);
    // The recursive call might replace the condition child.
    condition = conditional.getExpression();
    if (needsExtraction) {
        VariableElement resultVar = GeneratedVariableElement.newLocalVar("unseq$" + count++, conditional.getTypeMirror(), currentMethod);
        conditional.replaceWith(new SimpleName(resultVar));
        stmtList.add(new VariableDeclarationStatement(resultVar, null));
        IfStatement newIf = new IfStatement();
        newIf.setExpression(condition.copy());
        stmtList.add(newIf);
        Block thenBlock = new Block();
        newIf.setThenStatement(thenBlock);
        List<Statement> thenStmts = thenBlock.getStatements();
        extractOrderedAccesses(thenStmts, thenExpr, thenAccesses);
        // The recursive call might replace the then expression child.
        thenExpr = conditional.getThenExpression();
        thenStmts.add(new ExpressionStatement(new Assignment(new SimpleName(resultVar), thenExpr.copy())));
        Block elseBlock = new Block();
        newIf.setElseStatement(elseBlock);
        List<Statement> elseStmts = elseBlock.getStatements();
        extractOrderedAccesses(elseStmts, elseExpr, elseAccesses);
        // The recursive call might replace the else expression child.
        elseExpr = conditional.getElseExpression();
        elseStmts.add(new ExpressionStatement(new Assignment(new SimpleName(resultVar), elseExpr.copy())));
    } else {
        extractOrderedAccesses(stmtList, thenExpr, thenAccesses);
        extractOrderedAccesses(stmtList, elseExpr, elseAccesses);
    }
}
Also used : ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) AssertStatement(com.google.devtools.j2objc.ast.AssertStatement) ForStatement(com.google.devtools.j2objc.ast.ForStatement) SynchronizedStatement(com.google.devtools.j2objc.ast.SynchronizedStatement) DoStatement(com.google.devtools.j2objc.ast.DoStatement) EnhancedForStatement(com.google.devtools.j2objc.ast.EnhancedForStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) WhileStatement(com.google.devtools.j2objc.ast.WhileStatement) BreakStatement(com.google.devtools.j2objc.ast.BreakStatement) IfStatement(com.google.devtools.j2objc.ast.IfStatement) Statement(com.google.devtools.j2objc.ast.Statement) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) ThrowStatement(com.google.devtools.j2objc.ast.ThrowStatement) SwitchStatement(com.google.devtools.j2objc.ast.SwitchStatement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) Assignment(com.google.devtools.j2objc.ast.Assignment) IfStatement(com.google.devtools.j2objc.ast.IfStatement) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) CommaExpression(com.google.devtools.j2objc.ast.CommaExpression) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) TreeNode(com.google.devtools.j2objc.ast.TreeNode) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Block(com.google.devtools.j2objc.ast.Block)

Example 33 with SimpleName

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

the class GwtConverter method visit.

@Override
public boolean visit(MethodInvocation node) {
    ExecutableElement method = node.getExecutableElement();
    List<Expression> args = node.getArguments();
    if (ElementUtil.getName(method).equals("create") && ElementUtil.getQualifiedName(ElementUtil.getDeclaringClass(method)).equals(GWT_CLASS) && args.size() == 1) {
        // Convert GWT.create(Foo.class) to Foo.class.newInstance().
        ExecutableElement newMethod = ElementUtil.findMethod(typeUtil.getJavaClass(), "newInstance");
        node.setName(new SimpleName(newMethod));
        Expression clazz = args.remove(0);
        node.setExpression(clazz);
        node.setExecutablePair(new ExecutablePair(newMethod));
    } else if (isGwtTest(node)) {
        node.replaceWith(new BooleanLiteral(false, typeUtil));
    }
    return true;
}
Also used : Expression(com.google.devtools.j2objc.ast.Expression) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) BooleanLiteral(com.google.devtools.j2objc.ast.BooleanLiteral) ExecutableElement(javax.lang.model.element.ExecutableElement) SimpleName(com.google.devtools.j2objc.ast.SimpleName)

Example 34 with SimpleName

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

the class JavaCloneWriter method createVolatileCloneStatement.

private Statement createVolatileCloneStatement(VariableElement var, VariableElement originalVar, boolean isWeak) {
    TypeMirror voidType = typeUtil.getVoid();
    TypeMirror pointerType = new PointerType(var.asType());
    String funcName = "JreCloneVolatile" + (isWeak ? "" : "Strong");
    FunctionElement element = new FunctionElement(funcName, voidType, null).addParameters(pointerType, pointerType);
    FunctionInvocation invocation = new FunctionInvocation(element, voidType);
    invocation.addArgument(new PrefixExpression(pointerType, PrefixExpression.Operator.ADDRESS_OF, new SimpleName(var)));
    invocation.addArgument(new PrefixExpression(pointerType, PrefixExpression.Operator.ADDRESS_OF, new FieldAccess(var, new SimpleName(originalVar))));
    return new ExpressionStatement(invocation);
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) TypeMirror(javax.lang.model.type.TypeMirror) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) SimpleName(com.google.devtools.j2objc.ast.SimpleName) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) PointerType(com.google.devtools.j2objc.types.PointerType) FieldAccess(com.google.devtools.j2objc.ast.FieldAccess)

Example 35 with SimpleName

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

the class LabelRewriter method endVisit.

@Override
public void endVisit(LabeledStatement node) {
    Statement loopBody = getLoopBody(node.getBody());
    final String labelIdentifier = node.getLabel().getIdentifier();
    final boolean[] hasContinue = new boolean[1];
    final boolean[] hasBreak = new boolean[1];
    node.accept(new TreeVisitor() {

        @Override
        public void endVisit(ContinueStatement node) {
            if (node.getLabel() != null && node.getLabel().getIdentifier().equals(labelIdentifier)) {
                hasContinue[0] = true;
                node.setLabel(new SimpleName("continue_" + labelIdentifier));
            }
        }

        @Override
        public void endVisit(BreakStatement node) {
            if (node.getLabel() != null && node.getLabel().getIdentifier().equals(labelIdentifier)) {
                hasBreak[0] = true;
                node.setLabel(new SimpleName("break_" + labelIdentifier));
            }
        }
    });
    if (hasContinue[0]) {
        assert loopBody != null : "Continue statements must be inside a loop.";
        LabeledStatement newLabelStmt = new LabeledStatement("continue_" + labelIdentifier);
        newLabelStmt.setBody(new EmptyStatement());
        // Put the loop body into an inner block so the continue label is outside
        // the scope of any variable initializations.
        Block newBlock = new Block();
        loopBody.replaceWith(newBlock);
        newBlock.addStatement(loopBody);
        newBlock.addStatement(newLabelStmt);
    }
    if (hasBreak[0]) {
        LabeledStatement newLabelStmt = new LabeledStatement("break_" + labelIdentifier);
        newLabelStmt.setBody(new EmptyStatement());
        TreeUtil.insertAfter(node, newLabelStmt);
    }
    if (hasContinue[0] || hasBreak[0]) {
        // Replace this node with its statement, thus deleting the label.
        node.replaceWith(TreeUtil.remove(node.getBody()));
    }
}
Also used : TreeVisitor(com.google.devtools.j2objc.ast.TreeVisitor) BreakStatement(com.google.devtools.j2objc.ast.BreakStatement) LabeledStatement(com.google.devtools.j2objc.ast.LabeledStatement) Statement(com.google.devtools.j2objc.ast.Statement) ContinueStatement(com.google.devtools.j2objc.ast.ContinueStatement) ForStatement(com.google.devtools.j2objc.ast.ForStatement) DoStatement(com.google.devtools.j2objc.ast.DoStatement) EnhancedForStatement(com.google.devtools.j2objc.ast.EnhancedForStatement) LabeledStatement(com.google.devtools.j2objc.ast.LabeledStatement) WhileStatement(com.google.devtools.j2objc.ast.WhileStatement) BreakStatement(com.google.devtools.j2objc.ast.BreakStatement) EmptyStatement(com.google.devtools.j2objc.ast.EmptyStatement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) EmptyStatement(com.google.devtools.j2objc.ast.EmptyStatement) Block(com.google.devtools.j2objc.ast.Block) ContinueStatement(com.google.devtools.j2objc.ast.ContinueStatement)

Aggregations

SimpleName (com.google.devtools.j2objc.ast.SimpleName)48 VariableElement (javax.lang.model.element.VariableElement)26 Expression (com.google.devtools.j2objc.ast.Expression)25 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)16 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)16 TypeMirror (javax.lang.model.type.TypeMirror)15 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)14 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)14 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)13 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)13 ExecutablePair (com.google.devtools.j2objc.types.ExecutablePair)12 Block (com.google.devtools.j2objc.ast.Block)11 Statement (com.google.devtools.j2objc.ast.Statement)11 ExecutableElement (javax.lang.model.element.ExecutableElement)11 TypeElement (javax.lang.model.element.TypeElement)10 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)9 MethodInvocation (com.google.devtools.j2objc.ast.MethodInvocation)9 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)9 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)8 ThisExpression (com.google.devtools.j2objc.ast.ThisExpression)8