Search in sources :

Example 16 with ExpressionStatement

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

the class DestructorGenerator method createRelease.

private Statement createRelease(VariableElement var) {
    TypeMirror varType = var.asType();
    if (ElementUtil.isStatic(var) || varType.getKind().isPrimitive() || ElementUtil.isWeakReference(var)) {
        return null;
    }
    boolean isVolatile = ElementUtil.isVolatile(var);
    boolean isRetainedWith = ElementUtil.isRetainedWithField(var);
    String funcName = null;
    if (isRetainedWith) {
        funcName = isVolatile ? "JreVolatileRetainedWithRelease" : "JreRetainedWithRelease";
    } else if (isVolatile) {
        funcName = "JreReleaseVolatile";
    } else if (options.useReferenceCounting()) {
        funcName = "RELEASE_";
    }
    if (funcName == null) {
        return null;
    }
    TypeMirror voidType = typeUtil.getVoid();
    TypeMirror idType = TypeUtil.ID_TYPE;
    FunctionElement element = new FunctionElement(funcName, voidType, null);
    FunctionInvocation releaseInvocation = new FunctionInvocation(element, voidType);
    if (isRetainedWith) {
        element.addParameters(idType);
        releaseInvocation.addArgument(new ThisExpression(ElementUtil.getDeclaringClass(var).asType()));
    }
    element.addParameters(isVolatile ? TypeUtil.ID_PTR_TYPE : idType);
    Expression arg = new SimpleName(var);
    if (isVolatile) {
        arg = new PrefixExpression(new PointerType(varType), PrefixExpression.Operator.ADDRESS_OF, arg);
    }
    releaseInvocation.addArgument(arg);
    return new ExpressionStatement(releaseInvocation);
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) TypeMirror(javax.lang.model.type.TypeMirror) Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) SimpleName(com.google.devtools.j2objc.ast.SimpleName) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) PointerType(com.google.devtools.j2objc.types.PointerType)

Example 17 with ExpressionStatement

use of com.google.devtools.j2objc.ast.ExpressionStatement 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)

Aggregations

ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)17 SimpleName (com.google.devtools.j2objc.ast.SimpleName)13 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)9 VariableElement (javax.lang.model.element.VariableElement)9 Expression (com.google.devtools.j2objc.ast.Expression)8 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)8 TypeMirror (javax.lang.model.type.TypeMirror)8 Statement (com.google.devtools.j2objc.ast.Statement)7 FunctionElement (com.google.devtools.j2objc.types.FunctionElement)7 ExecutableElement (javax.lang.model.element.ExecutableElement)7 TypeElement (javax.lang.model.element.TypeElement)7 Block (com.google.devtools.j2objc.ast.Block)6 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)6 Assignment (com.google.devtools.j2objc.ast.Assignment)5 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)5 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)5 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)4 NativeStatement (com.google.devtools.j2objc.ast.NativeStatement)4 ThisExpression (com.google.devtools.j2objc.ast.ThisExpression)4 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)3