Search in sources :

Example 6 with ForStatement

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

the class EnumRewriter method addSimpleNonArcInitialization.

private void addSimpleNonArcInitialization(EnumDeclaration node) {
    List<EnumConstantDeclaration> constants = node.getEnumConstants();
    List<Statement> stmts = node.getClassInitStatements().subList(0, 0);
    stmts.add(new NativeStatement("size_t objSize = class_getInstanceSize(self);"));
    stmts.add(new NativeStatement(UnicodeUtils.format("size_t allocSize = %s * objSize;", constants.size())));
    stmts.add(new NativeStatement("uintptr_t ptr = (uintptr_t)calloc(allocSize, 1);"));
    VariableElement localEnum = GeneratedVariableElement.newLocalVar("e", TypeUtil.ID_TYPE, null);
    stmts.add(new VariableDeclarationStatement(localEnum, null));
    StringBuffer sb = new StringBuffer("id names[] = {\n  ");
    for (EnumConstantDeclaration constant : node.getEnumConstants()) {
        sb.append("@\"" + ElementUtil.getName(constant.getVariableElement()) + "\", ");
    }
    sb.append("\n};");
    stmts.add(new NativeStatement(sb.toString()));
    TypeMirror intType = typeUtil.getInt();
    GeneratedVariableElement loopCounterElement = GeneratedVariableElement.newLocalVar("i", intType, TreeUtil.getEnclosingElement(node));
    VariableDeclarationExpression loopCounter = new VariableDeclarationExpression().setType(Type.newType(loopCounterElement.asType())).addFragment(new VariableDeclarationFragment(loopCounterElement, TreeUtil.newLiteral(0, typeUtil)));
    Expression loopTest = new InfixExpression().setOperator(InfixExpression.Operator.LESS).setTypeMirror(intType).addOperand(new SimpleName(loopCounterElement)).addOperand(TreeUtil.newLiteral(constants.size(), typeUtil));
    Expression loopUpdater = new PostfixExpression(loopCounterElement, PostfixExpression.Operator.INCREMENT);
    Block loopBody = new Block();
    stmts.add(new ForStatement().addInitializer(loopCounter).setExpression(loopTest).addUpdater(loopUpdater).setBody(loopBody));
    String enumClassName = nameTable.getFullName(node.getTypeElement());
    loopBody.addStatement(new NativeStatement("(" + enumClassName + "_values_[i] = e = objc_constructInstance(self, (void *)ptr), ptr += objSize);"));
    loopBody.addStatement(new NativeStatement(enumClassName + "_initWithNSString_withInt_(e, names[i], i);"));
}
Also used : GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) 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) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) SimpleName(com.google.devtools.j2objc.ast.SimpleName) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) EnumConstantDeclaration(com.google.devtools.j2objc.ast.EnumConstantDeclaration) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) TypeMirror(javax.lang.model.type.TypeMirror) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) Expression(com.google.devtools.j2objc.ast.Expression) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) CommaExpression(com.google.devtools.j2objc.ast.CommaExpression) VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Block(com.google.devtools.j2objc.ast.Block) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) ForStatement(com.google.devtools.j2objc.ast.ForStatement)

Example 7 with ForStatement

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

the class TreeConverter method convertForLoop.

private TreeNode convertForLoop(JCTree.JCForLoop node) {
    ForStatement newNode = new ForStatement().setExpression((Expression) convert(node.getCondition())).setBody((Statement) convert(node.getStatement()));
    VariableDeclarationExpression lastVar = null;
    for (JCTree.JCStatement initializer : node.getInitializer()) {
        if (initializer.getKind() == Kind.VARIABLE) {
            JCTree.JCVariableDecl var = (JCTree.JCVariableDecl) initializer;
            VariableDeclarationExpression newVar = convertVariableExpression(var);
            if (lastVar == null) {
                newNode.addInitializer(newVar);
                lastVar = newVar;
            } else {
                lastVar.addFragment(TreeUtil.remove(newVar.getFragment(0)));
            }
        } else {
            assert initializer.getKind() == Kind.EXPRESSION_STATEMENT;
            newNode.addInitializer((Expression) convert(((JCTree.JCExpressionStatement) initializer).getExpression()));
        }
    }
    for (JCTree.JCExpressionStatement updater : node.getUpdate()) {
        newNode.addUpdater((Expression) convert(updater.getExpression()));
    }
    return newNode;
}
Also used : 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) LambdaExpression(com.google.devtools.j2objc.ast.LambdaExpression) 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) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) JCTree(com.sun.tools.javac.tree.JCTree) ForStatement(com.google.devtools.j2objc.ast.ForStatement) EnhancedForStatement(com.google.devtools.j2objc.ast.EnhancedForStatement) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Aggregations

ForStatement (com.google.devtools.j2objc.ast.ForStatement)7 Statement (com.google.devtools.j2objc.ast.Statement)6 Block (com.google.devtools.j2objc.ast.Block)5 IfStatement (com.google.devtools.j2objc.ast.IfStatement)4 VariableDeclarationExpression (com.google.devtools.j2objc.ast.VariableDeclarationExpression)4 EmptyStatement (com.google.devtools.j2objc.ast.EmptyStatement)3 Expression (com.google.devtools.j2objc.ast.Expression)3 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)3 LabeledStatement (com.google.devtools.j2objc.ast.LabeledStatement)3 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)3 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)3 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)2 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)2 EnhancedForStatement (com.google.devtools.j2objc.ast.EnhancedForStatement)2 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)2 ParenthesizedExpression (com.google.devtools.j2objc.ast.ParenthesizedExpression)2 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)2 VariableDeclarationFragment (com.google.devtools.j2objc.ast.VariableDeclarationFragment)2 AssertStatement (com.google.devtools.j2objc.ast.AssertStatement)1 BreakStatement (com.google.devtools.j2objc.ast.BreakStatement)1