use of com.google.devtools.j2objc.ast.VariableDeclarationStatement in project j2objc by google.
the class EnumRewriter method addSimpleNonArcInitialization.
private void addSimpleNonArcInitialization(EnumDeclaration node) {
List<EnumConstantDeclaration> constants = node.getEnumConstants();
List<Statement> stmts = node.getClassInitStatements().subList(0, 0);
stmts.add(new NativeStatement("size_t objSize = class_getInstanceSize(self);"));
stmts.add(new NativeStatement(UnicodeUtils.format("size_t allocSize = %s * objSize;", constants.size())));
stmts.add(new NativeStatement("uintptr_t ptr = (uintptr_t)calloc(allocSize, 1);"));
VariableElement localEnum = GeneratedVariableElement.newLocalVar("e", TypeUtil.ID_TYPE, null);
stmts.add(new VariableDeclarationStatement(localEnum, null));
// Create a local array of enum names only if reflection is stripped but
// enum constants are not. For non-stripped classes, enum names are now
// retrieved from metadata, to avoid duplicates.
boolean useNamesArray = options.stripReflection() && !options.stripEnumConstants();
if (useNamesArray) {
StringBuilder sb = new StringBuilder("id names[] = {\n ");
for (EnumConstantDeclaration constant : constants) {
sb.append("@\"" + ElementUtil.getName(constant.getVariableElement()) + "\", ");
}
sb.append("\n};");
stmts.add(new NativeStatement(sb.toString()));
}
TypeMirror intType = typeUtil.getInt();
GeneratedVariableElement loopCounterElement = GeneratedVariableElement.newLocalVar("i", intType, TreeUtil.getEnclosingElement(node));
VariableDeclarationExpression loopCounter = new VariableDeclarationExpression().setType(Type.newType(loopCounterElement.asType())).addFragment(new VariableDeclarationFragment(loopCounterElement, TreeUtil.newLiteral(0, typeUtil)));
Expression loopTest = new InfixExpression().setOperator(InfixExpression.Operator.LESS).setTypeMirror(intType).addOperand(new SimpleName(loopCounterElement)).addOperand(TreeUtil.newLiteral(constants.size(), typeUtil));
Expression loopUpdater = new PostfixExpression(loopCounterElement, PostfixExpression.Operator.INCREMENT);
Block loopBody = new Block();
stmts.add(new ForStatement().addInitializer(loopCounter).setExpression(loopTest).addUpdater(loopUpdater).setBody(loopBody));
String enumClassName = nameTable.getFullName(node.getTypeElement());
loopBody.addStatement(new NativeStatement("((void)(" + enumClassName + "_values_[i] = e = objc_constructInstance(self, (void *)ptr)), ptr += objSize);"));
if (useNamesArray) {
loopBody.addStatement(new NativeStatement(enumClassName + "_initWithNSString_withInt_(e, names[i], i);"));
} else if (options.stripEnumConstants()) {
String statementText = enumClassName + "_initWithNSString_withInt_(e, " + ENUM_NAME_STRIPPED + ", i);";
loopBody.addStatement(new NativeStatement(statementText));
} else {
loopBody.addStatement(new NativeStatement(enumClassName + "_initWithNSString_withInt_(e, JreEnumConstantName(" + enumClassName + "_class_(), i), i);"));
}
}
use of com.google.devtools.j2objc.ast.VariableDeclarationStatement in project j2objc by google.
the class UnsequencedExpressionRewriter method extractConditionalExpression.
private void extractConditionalExpression(List<Statement> stmtList, ConditionalExpression conditional, List<VariableAccess> toExtract) {
Expression condition = conditional.getExpression();
Expression thenExpr = conditional.getThenExpression();
Expression elseExpr = conditional.getElseExpression();
List<VariableAccess> conditionAccesses = Lists.newArrayList();
List<VariableAccess> thenAccesses = Lists.newArrayList();
List<VariableAccess> elseAccesses = Lists.newArrayList();
boolean needsExtraction = false;
for (VariableAccess access : toExtract) {
TreeNode node = access.expression;
while (node.getParent() != conditional) {
node = node.getParent();
}
if (node == condition) {
conditionAccesses.add(access);
} else if (node == thenExpr) {
thenAccesses.add(access);
} else if (node == elseExpr) {
elseAccesses.add(access);
} else {
throw new AssertionError();
}
if (node != condition) {
// We need to extract an if-statement if there is an access that
// executes conditionally.
needsExtraction = true;
}
}
extractOrderedAccesses(stmtList, condition, conditionAccesses);
// The recursive call might replace the condition child.
condition = conditional.getExpression();
if (needsExtraction) {
VariableElement resultVar = GeneratedVariableElement.newLocalVar("unseq$" + count++, conditional.getTypeMirror(), currentMethod);
conditional.replaceWith(new SimpleName(resultVar));
stmtList.add(new VariableDeclarationStatement(resultVar, null));
IfStatement newIf = new IfStatement();
newIf.setExpression(condition.copy());
stmtList.add(newIf);
Block thenBlock = new Block();
newIf.setThenStatement(thenBlock);
List<Statement> thenStmts = thenBlock.getStatements();
extractOrderedAccesses(thenStmts, thenExpr, thenAccesses);
// The recursive call might replace the then expression child.
thenExpr = conditional.getThenExpression();
thenStmts.add(new ExpressionStatement(new Assignment(new SimpleName(resultVar), thenExpr.copy())));
Block elseBlock = new Block();
newIf.setElseStatement(elseBlock);
List<Statement> elseStmts = elseBlock.getStatements();
extractOrderedAccesses(elseStmts, elseExpr, elseAccesses);
// The recursive call might replace the else expression child.
elseExpr = conditional.getElseExpression();
elseStmts.add(new ExpressionStatement(new Assignment(new SimpleName(resultVar), elseExpr.copy())));
} else {
extractOrderedAccesses(stmtList, thenExpr, thenAccesses);
extractOrderedAccesses(stmtList, elseExpr, elseAccesses);
}
}
use of com.google.devtools.j2objc.ast.VariableDeclarationStatement in project j2objc by google.
the class UnsequencedExpressionRewriter method extractVariableDeclarationFragments.
private void extractVariableDeclarationFragments(List<VariableDeclarationFragment> fragments, List<Statement> stmtList) {
for (int i = 0; i < fragments.size(); i++) {
VariableDeclarationFragment frag = fragments.get(i);
Expression init = frag.getInitializer();
if (init == null) {
continue;
}
newExpression(init);
init.accept(this);
List<VariableAccess> toExtract = getUnsequencedAccesses();
if (!toExtract.isEmpty()) {
if (i > 0) {
// Extract all fragments before the current one to preserve ordering.
VariableDeclarationStatement newDecl = new VariableDeclarationStatement(fragments.get(0).copy());
for (int j = 1; j < i; j++) {
newDecl.addFragment(fragments.get(j).copy());
}
stmtList.add(newDecl);
fragments.subList(0, i).clear();
}
extractOrderedAccesses(stmtList, currentTopNode, toExtract);
i = 0;
}
}
}
use of com.google.devtools.j2objc.ast.VariableDeclarationStatement in project j2objc by google.
the class UnsequencedExpressionRewriter method visit.
@Override
public boolean visit(AssertStatement node) {
Expression expr = node.getExpression();
visitAndExtract(expr, node);
Expression msg = node.getMessage();
if (msg != null) {
newExpression(msg);
msg.accept(this);
List<VariableAccess> toExtract = getUnsequencedAccesses();
if (!toExtract.isEmpty()) {
// If the message expression needs any extraction, then we first extract
// the entire boolean expression to preserve ordering between the two.
VariableElement exprVar = GeneratedVariableElement.newLocalVar("unseq$" + count++, expr.getTypeMirror(), currentMethod);
TreeUtil.insertBefore(node, new VariableDeclarationStatement(exprVar, node.getExpression().copy()));
node.setExpression(new SimpleName(exprVar));
extractOrderedAccesses(TreeUtil.asStatementList(node).subList(0, 0), currentTopNode, toExtract);
}
}
return false;
}
use of com.google.devtools.j2objc.ast.VariableDeclarationStatement in project j2objc by google.
the class SwitchRewriterTest method testVariableDeclarationsInSwitchStatement2.
public void testVariableDeclarationsInSwitchStatement2() throws IOException {
CompilationUnit unit = translateType("A", "public class A { public void doSomething(int i) { switch (i) { " + "case 1: int j = i * 2; log(j); break; " + "case 2: log(i); break; " + "case 3: log(i); int k = i, l = 42; break; }}" + "private void log(int i) {}}");
TypeDeclaration testType = (TypeDeclaration) unit.getTypes().get(0);
// First MethodDeclaration is the implicit default constructor.
MethodDeclaration method = TreeUtil.getMethodDeclarationsList(testType).get(1);
List<Statement> stmts = method.getBody().getStatements();
assertEquals(1, stmts.size());
Block block = (Block) stmts.get(0);
stmts = block.getStatements();
assertEquals(4, stmts.size());
assertTrue(stmts.get(0) instanceof VariableDeclarationStatement);
assertTrue(stmts.get(1) instanceof VariableDeclarationStatement);
assertTrue(stmts.get(2) instanceof VariableDeclarationStatement);
assertTrue(stmts.get(3) instanceof SwitchStatement);
}
Aggregations