use of com.google.devtools.j2objc.ast.VariableDeclarationExpression in project j2objc by google.
the class TreeConverter method convertVariableExpression.
private VariableDeclarationExpression convertVariableExpression(JCTree.JCVariableDecl node) {
VarSymbol var = node.sym;
boolean isVarargs = (node.sym.flags() & Flags.VARARGS) > 0;
Type newType = convertType(var.asType(), getPosition(node), isVarargs);
VariableDeclarationFragment fragment = new VariableDeclarationFragment();
fragment.setVariableElement(var).setInitializer((Expression) convert(node.getInitializer()));
return new VariableDeclarationExpression().setType(newType).addFragment(fragment);
}
use of com.google.devtools.j2objc.ast.VariableDeclarationExpression in project j2objc by google.
the class StatementGenerator method printBasicTryWithResources.
/**
* Print basic try-with-resources, as defined by JLS 14.20.3.1.
*/
private void printBasicTryWithResources(Block body, List<VariableDeclarationExpression> resources) {
VariableDeclarationExpression resource = resources.get(0);
// Resource declaration can only have one fragment.
String resourceName = nameTable.getVariableQualifiedName(resource.getFragment(0).getVariableElement());
String primaryExceptionName = UnicodeUtils.format("__primaryException%d", resources.size());
buffer.append("{\n");
resource.accept(this);
buffer.append(";\n");
buffer.append(UnicodeUtils.format("NSException *%s = nil;\n", primaryExceptionName));
buffer.append("@try ");
List<VariableDeclarationExpression> tail = resources.subList(1, resources.size());
if (tail.isEmpty()) {
body.accept(this);
} else {
printBasicTryWithResources(body, tail);
}
buffer.append(UnicodeUtils.format("@catch (NSException *e) {\n" + "%s = e;\n" + "@throw e;\n" + "}\n", primaryExceptionName));
buffer.append(UnicodeUtils.format(// to compare to the JLS spec.
"@finally {\n" + " if (%s != nil) {\n" + " if (%s != nil) {\n" + " @try {\n" + " [%s close];\n" + " } @catch (NSException *e) {\n" + " [%s addSuppressedWithNSException:e];\n" + " }\n" + " } else {\n" + " [%s close];\n" + " }\n" + " }\n" + "}\n", resourceName, primaryExceptionName, resourceName, primaryExceptionName, resourceName));
buffer.append("}\n");
}
use of com.google.devtools.j2objc.ast.VariableDeclarationExpression in project j2objc by google.
the class TreeConverter method convertVariableDeclarationExpression.
private static TreeNode convertVariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression node) {
VariableDeclarationExpression newNode = new VariableDeclarationExpression();
convertExpression(node, newNode);
for (Object fragment : node.fragments()) {
newNode.addFragment((VariableDeclarationFragment) TreeConverter.convert(fragment));
}
return newNode.setType((Type) TreeConverter.convert(node.getType()));
}
use of com.google.devtools.j2objc.ast.VariableDeclarationExpression in project j2objc by google.
the class Rewriter method endVisit.
@Override
public void endVisit(ForStatement node) {
// nodes in the initializers.
if (node.getInitializers().size() == 1) {
Object initializer = node.getInitializer(0);
if (initializer instanceof VariableDeclarationExpression) {
List<VariableDeclarationFragment> fragments = ((VariableDeclarationExpression) initializer).getFragments();
for (VariableDeclarationFragment fragment : fragments) {
if (ElementUtil.hasAnnotation(fragment.getVariableElement(), AutoreleasePool.class)) {
Statement loopBody = node.getBody();
if (!(loopBody instanceof Block)) {
Block block = new Block();
node.setBody(block);
block.addStatement(loopBody);
}
((Block) node.getBody()).setHasAutoreleasePool(true);
}
}
}
}
}
use of com.google.devtools.j2objc.ast.VariableDeclarationExpression in project j2objc by google.
the class UnsequencedExpressionRewriter method visit.
@Override
public boolean visit(ForStatement node) {
List<Expression> initializers = node.getInitializers();
// expression or a list of initializer expressions.
if (initializers.size() == 1 && initializers.get(0) instanceof VariableDeclarationExpression) {
VariableDeclarationExpression decl = (VariableDeclarationExpression) initializers.get(0);
extractVariableDeclarationFragments(decl.getFragments(), TreeUtil.asStatementList(node).subList(0, 0));
} else {
extractExpressionList(initializers, TreeUtil.asStatementList(node).subList(0, 0), false);
}
Expression expr = node.getExpression();
if (expr != null) {
newExpression(expr);
expr.accept(this);
List<VariableAccess> toExtract = getUnsequencedAccesses();
if (!toExtract.isEmpty()) {
// Convert "if (;cond;)" into "if (;;) { if (!(cond)) break; ...}".
List<Statement> stmtList = TreeUtil.asStatementList(node.getBody()).subList(0, 0);
extractOrderedAccesses(stmtList, currentTopNode, toExtract);
stmtList.add(createLoopTermination(node.getExpression()));
node.setExpression(null);
}
}
extractExpressionList(node.getUpdaters(), TreeUtil.asStatementList(node.getBody()), true);
node.getBody().accept(this);
return false;
}
Aggregations