use of com.google.devtools.j2objc.ast.VariableDeclarationExpression 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);"));
}
}
Aggregations