use of com.google.devtools.j2objc.ast.FieldAccess in project j2objc by google.
the class EnhancedForRewriter method handleArrayIteration.
private void handleArrayIteration(EnhancedForStatement node) {
Expression expression = node.getExpression();
ArrayType expressionType = (ArrayType) expression.getTypeMirror();
VariableElement loopVariable = node.getParameter().getVariableElement();
TypeMirror componentType = expressionType.getComponentType();
TypeElement iosArrayType = typeUtil.getIosArray(componentType);
TypeMirror bufferType = new PointerType(componentType);
VariableElement arrayVariable = GeneratedVariableElement.newLocalVar("a__", expressionType, null);
VariableElement bufferVariable = GeneratedVariableElement.newLocalVar("b__", bufferType, null).setTypeQualifiers("const*");
VariableElement endVariable = GeneratedVariableElement.newLocalVar("e__", bufferType, null).setTypeQualifiers("const*");
VariableElement bufferField = GeneratedVariableElement.newField("buffer", bufferType, iosArrayType).addModifiers(Modifier.PUBLIC);
VariableElement sizeField = GeneratedVariableElement.newField("size", typeUtil.getInt(), iosArrayType).addModifiers(Modifier.PUBLIC);
VariableDeclarationStatement arrayDecl = new VariableDeclarationStatement(arrayVariable, TreeUtil.remove(expression));
FieldAccess bufferAccess = new FieldAccess(bufferField, new SimpleName(arrayVariable));
VariableDeclarationStatement bufferDecl = new VariableDeclarationStatement(bufferVariable, bufferAccess);
InfixExpression endInit = new InfixExpression(bufferType, InfixExpression.Operator.PLUS, new SimpleName(bufferVariable), new FieldAccess(sizeField, new SimpleName(arrayVariable)));
VariableDeclarationStatement endDecl = new VariableDeclarationStatement(endVariable, endInit);
WhileStatement loop = new WhileStatement();
loop.setExpression(new InfixExpression(typeUtil.getBoolean(), InfixExpression.Operator.LESS, new SimpleName(bufferVariable), new SimpleName(endVariable)));
Block newLoopBody = makeBlock(TreeUtil.remove(node.getBody()));
loop.setBody(newLoopBody);
newLoopBody.addStatement(0, new VariableDeclarationStatement(loopVariable, new PrefixExpression(componentType, PrefixExpression.Operator.DEREFERENCE, new PostfixExpression(bufferVariable, PostfixExpression.Operator.INCREMENT))));
Block block = new Block();
List<Statement> stmts = block.getStatements();
stmts.add(arrayDecl);
stmts.add(bufferDecl);
stmts.add(endDecl);
stmts.add(loop);
replaceLoop(node, block, loop);
}
use of com.google.devtools.j2objc.ast.FieldAccess 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);
}
use of com.google.devtools.j2objc.ast.FieldAccess in project j2objc by google.
the class TreeConverter method convertFieldAccess.
private TreeNode convertFieldAccess(JCTree.JCFieldAccess node) {
String fieldName = node.name.toString();
SourcePosition pos = getPosition(node);
JCTree.JCExpression selected = node.getExpression();
if (fieldName.equals("this")) {
return new ThisExpression().setQualifier((Name) convert(selected)).setTypeMirror(node.sym.asType());
}
if ("super".equals(getMemberName(selected))) {
SuperFieldAccess newNode = new SuperFieldAccess().setVariableElement((VariableElement) node.sym).setName(convertSimpleName(node.sym, node.type, pos));
if (selected.getKind() == Kind.MEMBER_SELECT) {
newNode.setQualifier((Name) convert(((JCTree.JCFieldAccess) selected).getExpression()));
}
return newNode;
}
if (node.getIdentifier().toString().equals("class")) {
return new TypeLiteral(node.type).setType((Type) convertType(selected.type, pos, false).setPosition(getPosition(node)));
}
if (selected.getKind() == Kind.IDENTIFIER && (!node.sym.getKind().isField() || ElementUtil.isConstant((VariableElement) node.sym))) {
if (selected.toString().equals("this")) {
// Just return the constant.
return new SimpleName(node.sym);
}
JCIdent ident = (JCTree.JCIdent) selected;
return new QualifiedName().setName(convertSimpleName(node.sym, node.type, pos)).setQualifier(convertSimpleName(ident.sym, ident.type, pos)).setElement(node.sym);
}
if (selected.getKind() == Kind.MEMBER_SELECT) {
TreeNode newSelected = convertFieldAccess((JCTree.JCFieldAccess) selected).setPosition(pos);
if (newSelected.getKind() == TreeNode.Kind.QUALIFIED_NAME) {
return new QualifiedName().setName(convertSimpleName(node.sym, node.type, pos)).setQualifier((QualifiedName) newSelected).setElement(node.sym);
}
}
if (ElementUtil.isConstant((VariableElement) node.sym) && ElementUtil.isStatic(node.sym) && !(selected.getKind() == Kind.METHOD_INVOCATION)) {
return new QualifiedName().setName(convertSimpleName(node.sym, node.type, pos)).setQualifier((Name) convert(selected)).setElement(node.sym);
}
return new FieldAccess().setVariableElement((VariableElement) node.sym).setExpression((Expression) convert(selected)).setName(convertSimpleName(node.sym, node.type, pos).setTypeMirror(node.type));
}
use of com.google.devtools.j2objc.ast.FieldAccess in project j2objc by google.
the class JavaCloneWriter method createVolatileCloneStatement.
private Statement createVolatileCloneStatement(VariableElement var, VariableElement originalVar, boolean isWeak) {
TypeMirror voidType = typeUtil.getVoid();
TypeMirror pointerType = new PointerType(var.asType());
String funcName = "JreCloneVolatile" + (isWeak ? "" : "Strong");
FunctionElement element = new FunctionElement(funcName, voidType, null).addParameters(pointerType, pointerType);
FunctionInvocation invocation = new FunctionInvocation(element, voidType);
invocation.addArgument(new PrefixExpression(pointerType, PrefixExpression.Operator.ADDRESS_OF, new SimpleName(var)));
invocation.addArgument(new PrefixExpression(pointerType, PrefixExpression.Operator.ADDRESS_OF, new FieldAccess(var, new SimpleName(originalVar))));
return new ExpressionStatement(invocation);
}
use of com.google.devtools.j2objc.ast.FieldAccess in project j2objc by google.
the class Rewriter method visit.
@Override
public boolean visit(QualifiedName node) {
VariableElement var = TreeUtil.getVariableElement(node);
Expression qualifier = node.getQualifier();
if (var != null && var.getKind().isField() && TreeUtil.getVariableElement(qualifier) != null) {
// FieldAccess nodes are more easily mutated than QualifiedName.
FieldAccess fieldAccess = new FieldAccess(var, node.getTypeMirror(), TreeUtil.remove(qualifier));
node.replaceWith(fieldAccess);
fieldAccess.accept(this);
return false;
}
return true;
}
Aggregations