Search in sources :

Example 1 with PostfixExpression

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

the class TreeConverter method convertPostfixExpression.

private static TreeNode convertPostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression node) {
    PostfixExpression newNode = new PostfixExpression();
    convertExpression(node, newNode);
    return newNode.setOperator(PostfixExpression.Operator.parse(node.getOperator().toString())).setOperand((Expression) TreeConverter.convert(node.getOperand()));
}
Also used : PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression)

Example 2 with PostfixExpression

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

the class EnhancedForRewriter method handleArrayIteration.

private void handleArrayIteration(EnhancedForStatement node) {
    Expression expression = node.getExpression();
    ArrayType expressionType = (ArrayType) expression.getTypeMirror();
    VariableElement loopVariable = node.getParameter().getVariableElement();
    TypeMirror componentType = expressionType.getComponentType();
    TypeElement iosArrayType = typeUtil.getIosArray(componentType);
    TypeMirror bufferType = new PointerType(componentType);
    VariableElement arrayVariable = GeneratedVariableElement.newLocalVar("a__", expressionType, null);
    VariableElement bufferVariable = GeneratedVariableElement.newLocalVar("b__", bufferType, null).setTypeQualifiers("const*");
    VariableElement endVariable = GeneratedVariableElement.newLocalVar("e__", bufferType, null).setTypeQualifiers("const*");
    VariableElement bufferField = GeneratedVariableElement.newField("buffer", bufferType, iosArrayType).addModifiers(Modifier.PUBLIC);
    VariableElement sizeField = GeneratedVariableElement.newField("size", typeUtil.getInt(), iosArrayType).addModifiers(Modifier.PUBLIC);
    VariableDeclarationStatement arrayDecl = new VariableDeclarationStatement(arrayVariable, TreeUtil.remove(expression));
    FieldAccess bufferAccess = new FieldAccess(bufferField, new SimpleName(arrayVariable));
    VariableDeclarationStatement bufferDecl = new VariableDeclarationStatement(bufferVariable, bufferAccess);
    InfixExpression endInit = new InfixExpression(bufferType, InfixExpression.Operator.PLUS, new SimpleName(bufferVariable), new FieldAccess(sizeField, new SimpleName(arrayVariable)));
    VariableDeclarationStatement endDecl = new VariableDeclarationStatement(endVariable, endInit);
    WhileStatement loop = new WhileStatement();
    loop.setExpression(new InfixExpression(typeUtil.getBoolean(), InfixExpression.Operator.LESS, new SimpleName(bufferVariable), new SimpleName(endVariable)));
    Block newLoopBody = makeBlock(TreeUtil.remove(node.getBody()));
    loop.setBody(newLoopBody);
    newLoopBody.addStatement(0, new VariableDeclarationStatement(loopVariable, new PrefixExpression(componentType, PrefixExpression.Operator.DEREFERENCE, new PostfixExpression(bufferVariable, PostfixExpression.Operator.INCREMENT))));
    Block block = new Block();
    List<Statement> stmts = block.getStatements();
    stmts.add(arrayDecl);
    stmts.add(bufferDecl);
    stmts.add(endDecl);
    stmts.add(loop);
    replaceLoop(node, block, loop);
}
Also used : TypeElement(javax.lang.model.element.TypeElement) EnhancedForStatement(com.google.devtools.j2objc.ast.EnhancedForStatement) LabeledStatement(com.google.devtools.j2objc.ast.LabeledStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) WhileStatement(com.google.devtools.j2objc.ast.WhileStatement) Statement(com.google.devtools.j2objc.ast.Statement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) PointerType(com.google.devtools.j2objc.types.PointerType) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) WhileStatement(com.google.devtools.j2objc.ast.WhileStatement) ArrayType(javax.lang.model.type.ArrayType) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) TypeMirror(javax.lang.model.type.TypeMirror) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) 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) FieldAccess(com.google.devtools.j2objc.ast.FieldAccess)

Example 3 with PostfixExpression

use of com.google.devtools.j2objc.ast.PostfixExpression 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));
    // Create a local array of enum names only if reflection is stripped but
    // enum constants are not. For non-stripped classes, enum names are now
    // retrieved from metadata, to avoid duplicates.
    boolean useNamesArray = options.stripReflection() && !options.stripEnumConstants();
    if (useNamesArray) {
        StringBuilder sb = new StringBuilder("id names[] = {\n  ");
        for (EnumConstantDeclaration constant : constants) {
            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("((void)(" + enumClassName + "_values_[i] = e = objc_constructInstance(self, (void *)ptr)), ptr += objSize);"));
    if (useNamesArray) {
        loopBody.addStatement(new NativeStatement(enumClassName + "_initWithNSString_withInt_(e, names[i], i);"));
    } else if (options.stripEnumConstants()) {
        String statementText = enumClassName + "_initWithNSString_withInt_(e, " + ENUM_NAME_STRIPPED + ", i);";
        loopBody.addStatement(new NativeStatement(statementText));
    } else {
        loopBody.addStatement(new NativeStatement(enumClassName + "_initWithNSString_withInt_(e, JreEnumConstantName(" + enumClassName + "_class_(), 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) CastExpression(com.google.devtools.j2objc.ast.CastExpression) 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) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) 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 4 with PostfixExpression

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

the class UnsequencedExpressionRewriter method isUnsequenced.

private boolean isUnsequenced(VariableAccess modification, Set<TreeNode> modificationAncestors, VariableAccess access) {
    TreeNode commonAncestor = currentTopNode;
    TreeNode node = access.expression;
    while (node != currentTopNode) {
        if (modificationAncestors.contains(node)) {
            commonAncestor = node;
            break;
        }
        node = node.getParent();
    }
    // contain the other access, then they are not unsequenced.
    if (isConditional(commonAncestor) && (isWithinConditionalBranch(modification.expression, commonAncestor) || isWithinConditionalBranch(access.expression, commonAncestor))) {
        return false;
    } else if (commonAncestor instanceof CommaExpression) {
        return false;
    } else if (commonAncestor instanceof Assignment && modification.expression == commonAncestor) {
        // "i = 1 + i++;" is unsequenced (according to clang).
        return access.expression instanceof PrefixExpression || access.expression instanceof PostfixExpression;
    }
    return true;
}
Also used : Assignment(com.google.devtools.j2objc.ast.Assignment) CommaExpression(com.google.devtools.j2objc.ast.CommaExpression) TreeNode(com.google.devtools.j2objc.ast.TreeNode) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression)

Example 5 with PostfixExpression

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

the class TranslationUtil method isAssigned.

public static boolean isAssigned(Expression node) {
    TreeNode parent = node.getParent();
    while (parent instanceof ParenthesizedExpression) {
        node = (Expression) parent;
        parent = node.getParent();
    }
    if (parent instanceof PostfixExpression) {
        PostfixExpression.Operator op = ((PostfixExpression) parent).getOperator();
        if (op == PostfixExpression.Operator.INCREMENT || op == PostfixExpression.Operator.DECREMENT) {
            return true;
        }
    } else if (parent instanceof PrefixExpression) {
        PrefixExpression.Operator op = ((PrefixExpression) parent).getOperator();
        if (op == PrefixExpression.Operator.INCREMENT || op == PrefixExpression.Operator.DECREMENT || op == PrefixExpression.Operator.ADDRESS_OF) {
            return true;
        }
    } else if (parent instanceof Assignment) {
        return node == ((Assignment) parent).getLeftHandSide();
    }
    return false;
}
Also used : ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) Assignment(com.google.devtools.j2objc.ast.Assignment) TreeNode(com.google.devtools.j2objc.ast.TreeNode) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression)

Aggregations

PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)6 Expression (com.google.devtools.j2objc.ast.Expression)3 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)3 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)3 VariableElement (javax.lang.model.element.VariableElement)3 Assignment (com.google.devtools.j2objc.ast.Assignment)2 Block (com.google.devtools.j2objc.ast.Block)2 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)2 ParenthesizedExpression (com.google.devtools.j2objc.ast.ParenthesizedExpression)2 SimpleName (com.google.devtools.j2objc.ast.SimpleName)2 Statement (com.google.devtools.j2objc.ast.Statement)2 TreeNode (com.google.devtools.j2objc.ast.TreeNode)2 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)2 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)2 TypeMirror (javax.lang.model.type.TypeMirror)2 CastExpression (com.google.devtools.j2objc.ast.CastExpression)1 EnhancedForStatement (com.google.devtools.j2objc.ast.EnhancedForStatement)1 EnumConstantDeclaration (com.google.devtools.j2objc.ast.EnumConstantDeclaration)1 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)1 FieldAccess (com.google.devtools.j2objc.ast.FieldAccess)1