Search in sources :

Example 11 with PrefixExpression

use of com.google.devtools.j2objc.ast.PrefixExpression 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 12 with PrefixExpression

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

the class StaticVarRewriter method rewriteStaticAccess.

private void rewriteStaticAccess(Expression node) {
    VariableElement var = TreeUtil.getVariableElement(node);
    if (var == null || !needsStaticLoad(node, var)) {
        return;
    }
    TypeElement declaringClass = ElementUtil.getDeclaringClass(var);
    boolean assignable = TranslationUtil.isAssigned(node);
    StringBuilder code = new StringBuilder(ElementUtil.isEnumConstant(var) ? "JreLoadEnum" : "JreLoadStatic");
    TypeMirror exprType = var.asType();
    if (assignable) {
        code.append("Ref");
        exprType = new PointerType(exprType);
    }
    code.append("(");
    code.append(nameTable.getFullName(declaringClass));
    code.append(", ");
    code.append(nameTable.getVariableShortName(var));
    code.append(")");
    NativeExpression nativeExpr = new NativeExpression(code.toString(), exprType);
    nativeExpr.addImportType(declaringClass.asType());
    Expression newNode = nativeExpr;
    if (assignable) {
        newNode = new PrefixExpression(var.asType(), PrefixExpression.Operator.DEREFERENCE, newNode);
    }
    node.replaceWith(newNode);
}
Also used : NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) TypeMirror(javax.lang.model.type.TypeMirror) 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) TypeElement(javax.lang.model.element.TypeElement) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) PointerType(com.google.devtools.j2objc.types.PointerType) VariableElement(javax.lang.model.element.VariableElement)

Example 13 with PrefixExpression

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

the class OperatorRewriter method rewriteCompoundAssign.

private void rewriteCompoundAssign(Assignment node) {
    if (!shouldRewriteCompoundAssign(node)) {
        return;
    }
    Expression lhs = node.getLeftHandSide();
    Expression rhs = node.getRightHandSide();
    TypeMirror lhsType = lhs.getTypeMirror();
    TypeMirror lhsPointerType = new PointerType(lhsType);
    String funcName = "Jre" + node.getOperator().getName() + (isVolatile(lhs) ? "Volatile" : "") + NameTable.capitalize(lhsType.toString()) + getPromotionSuffix(node);
    FunctionElement element = new FunctionElement(funcName, lhsType, null).addParameters(lhsPointerType, rhs.getTypeMirror());
    FunctionInvocation invocation = new FunctionInvocation(element, lhsType);
    List<Expression> args = invocation.getArguments();
    args.add(new PrefixExpression(lhsPointerType, PrefixExpression.Operator.ADDRESS_OF, TreeUtil.remove(lhs)));
    args.add(TreeUtil.remove(rhs));
    node.replaceWith(invocation);
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) 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) TypeMirror(javax.lang.model.type.TypeMirror) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) PointerType(com.google.devtools.j2objc.types.PointerType)

Example 14 with PrefixExpression

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

the class OperatorRewriter method rewriteVolatileLoad.

private void rewriteVolatileLoad(Expression node) {
    VariableElement var = TreeUtil.getVariableElement(node);
    if (var != null && ElementUtil.isVolatile(var) && !TranslationUtil.isAssigned(node)) {
        TypeMirror type = node.getTypeMirror();
        TypeMirror declaredType = type.getKind().isPrimitive() ? type : TypeUtil.ID_TYPE;
        String funcName = "JreLoadVolatile" + NameTable.capitalize(declaredType.toString());
        FunctionElement element = new FunctionElement(funcName, declaredType, null).addParameters(TypeUtil.ID_PTR_TYPE);
        FunctionInvocation invocation = new FunctionInvocation(element, type);
        node.replaceWith(invocation);
        invocation.addArgument(new PrefixExpression(new PointerType(type), PrefixExpression.Operator.ADDRESS_OF, node));
    }
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) TypeMirror(javax.lang.model.type.TypeMirror) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) PointerType(com.google.devtools.j2objc.types.PointerType) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement)

Example 15 with PrefixExpression

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

Aggregations

PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)18 PointerType (com.google.devtools.j2objc.types.PointerType)12 TypeMirror (javax.lang.model.type.TypeMirror)12 Expression (com.google.devtools.j2objc.ast.Expression)11 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)9 FunctionElement (com.google.devtools.j2objc.types.FunctionElement)9 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)8 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)7 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)6 VariableElement (javax.lang.model.element.VariableElement)6 ThisExpression (com.google.devtools.j2objc.ast.ThisExpression)5 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)4 ParenthesizedExpression (com.google.devtools.j2objc.ast.ParenthesizedExpression)4 SimpleName (com.google.devtools.j2objc.ast.SimpleName)4 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)4 CastExpression (com.google.devtools.j2objc.ast.CastExpression)3 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)3 TreeNode (com.google.devtools.j2objc.ast.TreeNode)3 Assignment (com.google.devtools.j2objc.ast.Assignment)2 Block (com.google.devtools.j2objc.ast.Block)2