Search in sources :

Example 1 with CommaExpression

use of com.google.devtools.j2objc.ast.CommaExpression 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 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 : 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) NumberLiteral(com.google.devtools.j2objc.ast.NumberLiteral)

Example 2 with CommaExpression

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

the class OperatorRewriter method getRetainedWithTarget.

// Gets the target object for a call to the RetainedWith wrapper.
private Expression getRetainedWithTarget(Assignment node, VariableElement var) {
    Expression lhs = node.getLeftHandSide();
    if (!(lhs instanceof FieldAccess)) {
        return new ThisExpression(ElementUtil.getDeclaringClass(var).asType());
    }
    // To avoid duplicating the target expression we must save the result to a local variable.
    FieldAccess fieldAccess = (FieldAccess) lhs;
    Expression target = fieldAccess.getExpression();
    VariableElement targetVar = GeneratedVariableElement.newLocalVar("__rw$" + rwCount++, target.getTypeMirror(), null);
    TreeUtil.asStatementList(TreeUtil.getOwningStatement(lhs)).add(0, new VariableDeclarationStatement(targetVar, null));
    fieldAccess.setExpression(new SimpleName(targetVar));
    CommaExpression commaExpr = new CommaExpression(new Assignment(new SimpleName(targetVar), target));
    node.replaceWith(commaExpr);
    commaExpr.addExpression(node);
    return new SimpleName(targetVar);
}
Also used : Assignment(com.google.devtools.j2objc.ast.Assignment) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) CommaExpression(com.google.devtools.j2objc.ast.CommaExpression) Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) CommaExpression(com.google.devtools.j2objc.ast.CommaExpression) 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) FieldAccess(com.google.devtools.j2objc.ast.FieldAccess) SuperFieldAccess(com.google.devtools.j2objc.ast.SuperFieldAccess)

Example 3 with CommaExpression

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

the class StaticVarRewriter method visit.

@Override
public boolean visit(FieldAccess node) {
    VariableElement var = node.getVariableElement();
    if (ElementUtil.isInstanceVar(var)) {
        node.getExpression().accept(this);
        return false;
    }
    Expression expr = TreeUtil.remove(node.getExpression());
    Expression varNode = TreeUtil.remove(node.getName());
    if (!TranslationUtil.hasSideEffect(expr)) {
        node.replaceWith(varNode);
        varNode.accept(this);
        return false;
    }
    CommaExpression commaExpr = new CommaExpression(expr);
    if (TranslationUtil.isAssigned(node)) {
        commaExpr.addExpression(new PrefixExpression(new PointerType(var.asType()), PrefixExpression.Operator.ADDRESS_OF, varNode));
        node.replaceWith(new PrefixExpression(var.asType(), PrefixExpression.Operator.DEREFERENCE, commaExpr));
    } else {
        commaExpr.addExpression(varNode);
        node.replaceWith(commaExpr);
    }
    commaExpr.accept(this);
    return false;
}
Also used : CommaExpression(com.google.devtools.j2objc.ast.CommaExpression) Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) CommaExpression(com.google.devtools.j2objc.ast.CommaExpression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) PointerType(com.google.devtools.j2objc.types.PointerType) VariableElement(javax.lang.model.element.VariableElement)

Example 4 with CommaExpression

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

the class StatementGenerator method visit.

@Override
public boolean visit(CommaExpression node) {
    buffer.append('(');
    for (Iterator<Expression> it = node.getExpressions().iterator(); it.hasNext(); ) {
        Expression e = it.next();
        e.accept(this);
        if (it.hasNext()) {
            buffer.append(", ");
        }
    }
    buffer.append(')');
    return false;
}
Also used : LambdaExpression(com.google.devtools.j2objc.ast.LambdaExpression) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) InstanceofExpression(com.google.devtools.j2objc.ast.InstanceofExpression) Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) CommaExpression(com.google.devtools.j2objc.ast.CommaExpression) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) CastExpression(com.google.devtools.j2objc.ast.CastExpression) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression)

Example 5 with CommaExpression

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

Aggregations

CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)5 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)4 Assignment (com.google.devtools.j2objc.ast.Assignment)3 Expression (com.google.devtools.j2objc.ast.Expression)3 NativeExpression (com.google.devtools.j2objc.ast.NativeExpression)3 VariableElement (javax.lang.model.element.VariableElement)3 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)2 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)2 SimpleName (com.google.devtools.j2objc.ast.SimpleName)2 ThisExpression (com.google.devtools.j2objc.ast.ThisExpression)2 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)2 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)2 CastExpression (com.google.devtools.j2objc.ast.CastExpression)1 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)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 ForStatement (com.google.devtools.j2objc.ast.ForStatement)1 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)1 InstanceofExpression (com.google.devtools.j2objc.ast.InstanceofExpression)1