Search in sources :

Example 96 with Expression

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

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

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

the class SwitchRewriter method fixEnumValue.

private void fixEnumValue(SwitchStatement node) {
    Expression expr = node.getExpression();
    TypeMirror type = expr.getTypeMirror();
    if (!TypeUtil.isEnum(type)) {
        return;
    }
    DeclaredType enumType = typeUtil.getSuperclass(type);
    ExecutablePair ordinalMethod = typeUtil.findMethod(enumType, "ordinal");
    MethodInvocation invocation = new MethodInvocation(ordinalMethod, TreeUtil.remove(expr));
    node.setExpression(invocation);
}
Also used : Expression(com.google.devtools.j2objc.ast.Expression) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) TypeMirror(javax.lang.model.type.TypeMirror) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation) DeclaredType(javax.lang.model.type.DeclaredType)

Example 99 with Expression

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

the class SwitchRewriter method fixStringValue.

private void fixStringValue(SwitchStatement node) {
    Expression expr = node.getExpression();
    TypeMirror type = expr.getTypeMirror();
    if (!typeUtil.isString(type)) {
        return;
    }
    ArrayType arrayType = typeUtil.getArrayType(type);
    ArrayInitializer arrayInit = new ArrayInitializer(arrayType);
    int idx = 0;
    for (Statement stmt : node.getStatements()) {
        if (stmt instanceof SwitchCase) {
            SwitchCase caseStmt = (SwitchCase) stmt;
            if (!caseStmt.isDefault()) {
                arrayInit.addExpression(TreeUtil.remove(caseStmt.getExpression()));
                caseStmt.setExpression(NumberLiteral.newIntLiteral(idx++, typeUtil));
            }
        }
    }
    TypeMirror intType = typeUtil.getInt();
    FunctionElement indexOfFunc = new FunctionElement("JreIndexOfStr", intType, null).addParameters(type, arrayType, intType);
    FunctionInvocation invocation = new FunctionInvocation(indexOfFunc, intType);
    invocation.addArgument(TreeUtil.remove(expr)).addArgument(arrayInit).addArgument(NumberLiteral.newIntLiteral(idx, typeUtil));
    node.setExpression(invocation);
}
Also used : ArrayType(javax.lang.model.type.ArrayType) FunctionElement(com.google.devtools.j2objc.types.FunctionElement) SwitchCase(com.google.devtools.j2objc.ast.SwitchCase) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) Expression(com.google.devtools.j2objc.ast.Expression) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) TypeMirror(javax.lang.model.type.TypeMirror) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Statement(com.google.devtools.j2objc.ast.Statement) EmptyStatement(com.google.devtools.j2objc.ast.EmptyStatement) SwitchStatement(com.google.devtools.j2objc.ast.SwitchStatement) ArrayInitializer(com.google.devtools.j2objc.ast.ArrayInitializer)

Example 100 with Expression

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

the class SwitchRewriter method endVisit.

@Override
public void endVisit(SwitchCase node) {
    Expression expr = node.getExpression();
    VariableElement var = expr != null ? TreeUtil.getVariableElement(expr) : null;
    if (var == null) {
        return;
    }
    TypeMirror type = var.asType();
    if (TypeUtil.isEnum(type)) {
        String enumValue = NameTable.getNativeEnumName(nameTable.getFullName(TypeUtil.asTypeElement(type))) + "_" + nameTable.getVariableBaseName(var);
        node.setExpression(new NativeExpression(enumValue, typeUtil.getInt()));
    } else if (type.getKind().isPrimitive() && var.getKind() == ElementKind.LOCAL_VARIABLE) {
        Object value = var.getConstantValue();
        if (value != null) {
            node.setExpression(TreeUtil.newLiteral(value, typeUtil));
        }
    }
}
Also used : NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) Expression(com.google.devtools.j2objc.ast.Expression) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) TypeMirror(javax.lang.model.type.TypeMirror) VariableElement(javax.lang.model.element.VariableElement)

Aggregations

Expression (com.google.devtools.j2objc.ast.Expression)139 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)103 ParenthesizedExpression (com.google.devtools.j2objc.ast.ParenthesizedExpression)95 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)89 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)78 CastExpression (com.google.devtools.j2objc.ast.CastExpression)70 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)70 ThisExpression (com.google.devtools.j2objc.ast.ThisExpression)64 VariableDeclarationExpression (com.google.devtools.j2objc.ast.VariableDeclarationExpression)58 InstanceofExpression (com.google.devtools.j2objc.ast.InstanceofExpression)45 TypeMirror (javax.lang.model.type.TypeMirror)41 VariableElement (javax.lang.model.element.VariableElement)40 LambdaExpression (com.google.devtools.j2objc.ast.LambdaExpression)36 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)34 SimpleName (com.google.devtools.j2objc.ast.SimpleName)29 FunctionalExpression (com.google.devtools.j2objc.ast.FunctionalExpression)24 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)22 NativeExpression (com.google.devtools.j2objc.ast.NativeExpression)22 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)22 TypeElement (javax.lang.model.element.TypeElement)21