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);
}
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;
}
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);
}
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);
}
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));
}
}
}
Aggregations