Search in sources :

Example 1 with ExpressionStatement

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

the class UnsequencedExpressionRewriter method extractInfixConditional.

private void extractInfixConditional(List<Statement> stmtList, InfixExpression conditional, List<VariableAccess> toExtract) {
    InfixExpression.Operator op = conditional.getOperator();
    List<Expression> branches = conditional.getOperands();
    int lastIfExtractIdx = 0;
    VariableElement conditionalVar = null;
    int lastExtracted = 0;
    Expression lastBranch = null;
    for (int i = 0; i < toExtract.size(); i++) {
        VariableAccess access = toExtract.get(i);
        TreeNode node = access.expression;
        while (node.getParent() != conditional) {
            node = node.getParent();
        }
        assert node instanceof Expression;
        Expression branch = (Expression) node;
        // Extract all accesses from the previous branch.
        if (lastBranch != null && branch != lastBranch) {
            extractOrderedAccesses(stmtList, lastBranch, toExtract.subList(lastExtracted, i));
            lastExtracted = i;
        }
        lastBranch = branch;
        // If there's a new access in a new branch, then we extract an if-statement.
        if (branch != branches.get(lastIfExtractIdx)) {
            TypeMirror boolType = typeUtil.getBoolean();
            if (conditionalVar == null) {
                conditionalVar = GeneratedVariableElement.newLocalVar("unseq$" + count++, boolType, currentMethod);
                conditional.replaceWith(new SimpleName(conditionalVar));
                stmtList.add(new VariableDeclarationStatement(conditionalVar, null));
            }
            List<Expression> subBranches = branches.subList(lastIfExtractIdx, branches.indexOf(branch));
            IfStatement newIf = new IfStatement();
            Expression ifExpr = new Assignment(new SimpleName(conditionalVar), conditionalFromSubBranches(subBranches, op));
            if (op == InfixExpression.Operator.CONDITIONAL_OR) {
                ifExpr = new PrefixExpression(boolType, PrefixExpression.Operator.NOT, ParenthesizedExpression.parenthesize(ifExpr));
            }
            newIf.setExpression(ifExpr);
            stmtList.add(newIf);
            Block thenBlock = new Block();
            stmtList = thenBlock.getStatements();
            newIf.setThenStatement(thenBlock);
            lastIfExtractIdx = branches.indexOf(branch);
        }
    }
    extractOrderedAccesses(stmtList, lastBranch, toExtract.subList(lastExtracted, toExtract.size()));
    if (conditionalVar != null) {
        List<Expression> remainingBranches = Lists.newArrayList();
        remainingBranches.add(new SimpleName(conditionalVar));
        remainingBranches.addAll(branches.subList(lastIfExtractIdx, branches.size()));
        stmtList.add(new ExpressionStatement(new Assignment(new SimpleName(conditionalVar), conditionalFromSubBranches(remainingBranches, op))));
    }
}
Also used : 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) TypeMirror(javax.lang.model.type.TypeMirror) TreeNode(com.google.devtools.j2objc.ast.TreeNode) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Block(com.google.devtools.j2objc.ast.Block)

Example 2 with ExpressionStatement

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

the class UnsequencedExpressionRewriter method extractExpressionList.

private void extractExpressionList(List<Expression> expressions, List<Statement> stmtList, boolean extractModifiedExpression) {
    for (int i = 0; i < expressions.size(); i++) {
        Expression expr = expressions.get(i);
        newExpression(expr);
        expr.accept(this);
        List<VariableAccess> unsequencedAccesses = getUnsequencedAccesses();
        if (!unsequencedAccesses.isEmpty()) {
            for (int j = 0; j < i; j++) {
                stmtList.add(new ExpressionStatement(expressions.get(j).copy()));
            }
            expressions.subList(0, i).clear();
            extractOrderedAccesses(stmtList, currentTopNode, unsequencedAccesses);
            i = 0;
            if (extractModifiedExpression) {
                stmtList.add(new ExpressionStatement(expressions.get(0).copy()));
                expressions.remove(0);
                i = -1;
            }
        }
    }
}
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) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement)

Example 3 with ExpressionStatement

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

the class SwitchRewriter method fixVariableDeclarations.

/**
 * Moves all variable declarations above the first case statement.
 */
private void fixVariableDeclarations(SwitchStatement node) {
    List<Statement> statements = node.getStatements();
    Block block = new Block();
    List<Statement> blockStmts = block.getStatements();
    for (int i = 0; i < statements.size(); i++) {
        Statement stmt = statements.get(i);
        if (stmt instanceof VariableDeclarationStatement) {
            VariableDeclarationStatement declStmt = (VariableDeclarationStatement) stmt;
            statements.remove(i--);
            List<VariableDeclarationFragment> fragments = declStmt.getFragments();
            for (VariableDeclarationFragment decl : fragments) {
                Expression initializer = decl.getInitializer();
                if (initializer != null) {
                    Assignment assignment = new Assignment(new SimpleName(decl.getVariableElement()), TreeUtil.remove(initializer));
                    statements.add(++i, new ExpressionStatement(assignment));
                }
            }
            blockStmts.add(declStmt);
        }
    }
    if (blockStmts.size() > 0) {
        // There is at least one variable declaration, so copy this switch
        // statement into the new block and replace it in the parent list.
        node.replaceWith(block);
        blockStmts.add(node);
    }
}
Also used : Assignment(com.google.devtools.j2objc.ast.Assignment) Expression(com.google.devtools.j2objc.ast.Expression) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Statement(com.google.devtools.j2objc.ast.Statement) EmptyStatement(com.google.devtools.j2objc.ast.EmptyStatement) SwitchStatement(com.google.devtools.j2objc.ast.SwitchStatement) VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) SimpleName(com.google.devtools.j2objc.ast.SimpleName) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) Block(com.google.devtools.j2objc.ast.Block) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement)

Example 4 with ExpressionStatement

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

the class DestructorGenerator method addDeallocMethod.

private void addDeallocMethod(AbstractTypeDeclaration node) {
    TypeElement type = node.getTypeElement();
    boolean hasFinalize = hasFinalizeMethod(type);
    List<Statement> releaseStatements = options.useARC() ? ImmutableList.of() : createReleaseStatements(node);
    MethodDeclaration onDeallocMethodDeclaration = getOnDeallocMethodDeclaration(node);
    if (releaseStatements.isEmpty() && !hasFinalize && onDeallocMethodDeclaration == null) {
        return;
    }
    ExecutableElement deallocElement = GeneratedExecutableElement.newMethodWithSelector(NameTable.DEALLOC_METHOD, typeUtil.getVoid(), type).addModifiers(Modifier.PUBLIC);
    MethodDeclaration deallocDecl = new MethodDeclaration(deallocElement);
    deallocDecl.setHasDeclaration(false);
    Block block = new Block();
    deallocDecl.setBody(block);
    List<Statement> stmts = block.getStatements();
    if (onDeallocMethodDeclaration != null) {
        stmts.add(new ExpressionStatement(new MethodInvocation(new ExecutablePair(onDeallocMethodDeclaration.getExecutableElement()), null)));
    }
    if (hasFinalize) {
        String clsName = nameTable.getFullName(type);
        stmts.add(new NativeStatement("JreCheckFinalize(self, [" + clsName + " class]);"));
    }
    stmts.addAll(releaseStatements);
    if (options.useReferenceCounting()) {
        stmts.add(new ExpressionStatement(new SuperMethodInvocation(new ExecutablePair(superDeallocElement))));
    }
    node.addBodyDeclaration(deallocDecl);
}
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) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) SuperMethodInvocation(com.google.devtools.j2objc.ast.SuperMethodInvocation) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation) SuperMethodInvocation(com.google.devtools.j2objc.ast.SuperMethodInvocation) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) Block(com.google.devtools.j2objc.ast.Block)

Example 5 with ExpressionStatement

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

the class EnumRewriter method addNonArcInitialization.

private void addNonArcInitialization(EnumDeclaration node) {
    TypeElement type = node.getTypeElement();
    int baseTypeCount = 0;
    List<Statement> sizeStatements = new ArrayList<>();
    List<Statement> initStatements = new ArrayList<>();
    TypeMirror voidType = typeUtil.getVoid();
    VariableElement localEnum = GeneratedVariableElement.newLocalVar("e", TypeUtil.ID_TYPE, null);
    int i = 0;
    for (EnumConstantDeclaration constant : node.getEnumConstants()) {
        VariableElement varElement = constant.getVariableElement();
        String varName = ElementUtil.getName(varElement);
        ExecutableElement methodElement = constant.getExecutableElement();
        TypeElement valueType = ElementUtil.getDeclaringClass(methodElement);
        boolean isAnonymous = valueType != type;
        String classExpr = isAnonymous ? "[" + nameTable.getFullName(valueType) + " class]" : "self";
        String sizeName = "objSize" + (isAnonymous ? "_" + varName : "");
        if (isAnonymous) {
            sizeStatements.add(new NativeStatement(UnicodeUtils.format("size_t %s = class_getInstanceSize(%s);", sizeName, classExpr)));
            sizeStatements.add(new NativeStatement(UnicodeUtils.format("allocSize += %s;", sizeName)));
        } else {
            baseTypeCount++;
        }
        initStatements.add(new ExpressionStatement(new CommaExpression(new CastExpression(voidType, new ParenthesizedExpression(new Assignment(new SimpleName(varElement), new Assignment(new SimpleName(localEnum), new NativeExpression(UnicodeUtils.format("objc_constructInstance(%s, (void *)ptr)", classExpr), type.asType()))))), new NativeExpression("ptr += " + sizeName, voidType))));
        String initName = nameTable.getFullFunctionName(methodElement);
        FunctionElement initElement = new FunctionElement(initName, voidType, valueType).addParameters(valueType.asType()).addParameters(ElementUtil.asTypes(methodElement.getParameters()));
        FunctionInvocation initFunc = new FunctionInvocation(initElement, voidType);
        initFunc.addArgument(new SimpleName(localEnum));
        TreeUtil.copyList(constant.getArguments(), initFunc.getArguments());
        initFunc.addArgument(new StringLiteral(varName, typeUtil));
        initFunc.addArgument(new NumberLiteral(i++, typeUtil));
        initStatements.add(new ExpressionStatement(initFunc));
    }
    List<Statement> stmts = node.getClassInitStatements().subList(0, 0);
    if (baseTypeCount == 0) {
        stmts.add(new NativeStatement("size_t allocSize = 0;"));
    } else {
        stmts.add(new NativeStatement("size_t objSize = class_getInstanceSize(self);"));
        stmts.add(new NativeStatement(UnicodeUtils.format("size_t allocSize = %s * objSize;", baseTypeCount)));
    }
    stmts.addAll(sizeStatements);
    stmts.add(new NativeStatement("uintptr_t ptr = (uintptr_t)calloc(allocSize, 1);"));
    stmts.add(new VariableDeclarationStatement(localEnum, null));
    stmts.addAll(initStatements);
}
Also used : ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) FunctionElement(com.google.devtools.j2objc.types.FunctionElement) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) GeneratedTypeElement(com.google.devtools.j2objc.types.GeneratedTypeElement) TypeElement(javax.lang.model.element.TypeElement) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) ForStatement(com.google.devtools.j2objc.ast.ForStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Statement(com.google.devtools.j2objc.ast.Statement) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) ExecutableElement(javax.lang.model.element.ExecutableElement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) ArrayList(java.util.ArrayList) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) Assignment(com.google.devtools.j2objc.ast.Assignment) EnumConstantDeclaration(com.google.devtools.j2objc.ast.EnumConstantDeclaration) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) CommaExpression(com.google.devtools.j2objc.ast.CommaExpression) StringLiteral(com.google.devtools.j2objc.ast.StringLiteral) TypeMirror(javax.lang.model.type.TypeMirror) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) CastExpression(com.google.devtools.j2objc.ast.CastExpression) NumberLiteral(com.google.devtools.j2objc.ast.NumberLiteral)

Aggregations

ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)19 SimpleName (com.google.devtools.j2objc.ast.SimpleName)14 VariableElement (javax.lang.model.element.VariableElement)10 Expression (com.google.devtools.j2objc.ast.Expression)9 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)9 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)9 Statement (com.google.devtools.j2objc.ast.Statement)8 TypeMirror (javax.lang.model.type.TypeMirror)8 Block (com.google.devtools.j2objc.ast.Block)7 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)7 FunctionElement (com.google.devtools.j2objc.types.FunctionElement)7 ExecutableElement (javax.lang.model.element.ExecutableElement)7 TypeElement (javax.lang.model.element.TypeElement)7 Assignment (com.google.devtools.j2objc.ast.Assignment)6 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)6 ForStatement (com.google.devtools.j2objc.ast.ForStatement)5 ParenthesizedExpression (com.google.devtools.j2objc.ast.ParenthesizedExpression)5 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)5 VariableDeclarationExpression (com.google.devtools.j2objc.ast.VariableDeclarationExpression)5 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)4