Search in sources :

Example 11 with PointerType

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

use of com.google.devtools.j2objc.types.PointerType 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 13 with PointerType

use of com.google.devtools.j2objc.types.PointerType 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 14 with PointerType

use of com.google.devtools.j2objc.types.PointerType 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

PointerType (com.google.devtools.j2objc.types.PointerType)14 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)13 TypeMirror (javax.lang.model.type.TypeMirror)13 Expression (com.google.devtools.j2objc.ast.Expression)9 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)9 FunctionElement (com.google.devtools.j2objc.types.FunctionElement)9 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)5 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)5 VariableElement (javax.lang.model.element.VariableElement)5 SimpleName (com.google.devtools.j2objc.ast.SimpleName)4 ThisExpression (com.google.devtools.j2objc.ast.ThisExpression)4 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)3 TypeElement (javax.lang.model.element.TypeElement)3 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)2 FieldAccess (com.google.devtools.j2objc.ast.FieldAccess)2 NativeExpression (com.google.devtools.j2objc.ast.NativeExpression)2 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)2 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)2 ArrayInitializer (com.google.devtools.j2objc.ast.ArrayInitializer)1 Block (com.google.devtools.j2objc.ast.Block)1