use of com.google.devtools.j2objc.ast.EnumConstantDeclaration in project j2objc by google.
the class EnumRewriter method addArcInitialization.
// ARC does not allow using "objc_constructInstance" so ARC code doesn't get
// the shared allocation optimization.
private void addArcInitialization(EnumDeclaration node) {
List<Statement> stmts = node.getClassInitStatements().subList(0, 0);
int i = 0;
for (EnumConstantDeclaration constant : node.getEnumConstants()) {
VariableElement varElement = constant.getVariableElement();
ClassInstanceCreation creation = new ClassInstanceCreation(constant.getExecutablePair());
TreeUtil.copyList(constant.getArguments(), creation.getArguments());
creation.addArgument(new StringLiteral(ElementUtil.getName(varElement), typeUtil));
creation.addArgument(new NumberLiteral(i++, typeUtil));
creation.setHasRetainedResult(true);
stmts.add(new ExpressionStatement(new Assignment(new SimpleName(varElement), creation)));
}
}
use of com.google.devtools.j2objc.ast.EnumConstantDeclaration 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));
StringBuffer sb = new StringBuffer("id names[] = {\n ");
for (EnumConstantDeclaration constant : node.getEnumConstants()) {
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("(" + enumClassName + "_values_[i] = e = objc_constructInstance(self, (void *)ptr), ptr += objSize);"));
loopBody.addStatement(new NativeStatement(enumClassName + "_initWithNSString_withInt_(e, names[i], i);"));
}
use of com.google.devtools.j2objc.ast.EnumConstantDeclaration in project j2objc by google.
the class TypeImplementationGenerator method printStaticAccessors.
/**
* Prints the list of static variable and/or enum constant accessor methods.
*/
protected void printStaticAccessors() {
if (!options.staticAccessorMethods()) {
return;
}
for (VariableDeclarationFragment fragment : getStaticFields()) {
if (!((FieldDeclaration) fragment.getParent()).hasPrivateDeclaration()) {
VariableElement varElement = fragment.getVariableElement();
TypeMirror type = varElement.asType();
boolean isVolatile = ElementUtil.isVolatile(varElement);
boolean isPrimitive = type.getKind().isPrimitive();
String accessorName = nameTable.getStaticAccessorName(varElement);
String varName = nameTable.getVariableQualifiedName(varElement);
String objcType = nameTable.getObjCType(type);
String typeSuffix = isPrimitive ? NameTable.capitalize(TypeUtil.getName(type)) : "Id";
if (isVolatile) {
printf("\n+ (%s)%s {\n return JreLoadVolatile%s(&%s);\n}\n", objcType, accessorName, typeSuffix, varName);
} else {
printf("\n+ (%s)%s {\n return %s;\n}\n", objcType, accessorName, varName);
}
if (!ElementUtil.isFinal(varElement)) {
String setterFunc = isVolatile ? (isPrimitive ? "JreAssignVolatile" + typeSuffix : "JreVolatileStrongAssign") : (isPrimitive | options.useARC() ? null : "JreStrongAssign");
if (setterFunc == null) {
printf("\n+ (void)set%s:(%s)value {\n %s = value;\n}\n", NameTable.capitalize(accessorName), objcType, varName);
} else {
printf("\n+ (void)set%s:(%s)value {\n %s(&%s, value);\n}\n", NameTable.capitalize(accessorName), objcType, setterFunc, varName);
}
}
}
}
if (typeNode instanceof EnumDeclaration) {
for (EnumConstantDeclaration constant : ((EnumDeclaration) typeNode).getEnumConstants()) {
VariableElement varElement = constant.getVariableElement();
printf("\n+ (%s *)%s {\n return %s;\n}\n", typeName, nameTable.getStaticAccessorName(varElement), nameTable.getVariableQualifiedName(varElement));
}
}
}
use of com.google.devtools.j2objc.ast.EnumConstantDeclaration in project j2objc by google.
the class TypeDeclarationGenerator method printStaticAccessors.
/**
* Prints the list of static variable and/or enum constant accessor methods.
*/
protected void printStaticAccessors() {
if (options.staticAccessorMethods()) {
for (VariableDeclarationFragment fragment : getStaticFields()) {
VariableElement var = fragment.getVariableElement();
String accessorName = nameTable.getStaticAccessorName(var);
String objcType = nameTable.getObjCType(var.asType());
printf("\n+ (%s)%s;\n", objcType, accessorName);
if (!ElementUtil.isFinal(var)) {
printf("\n+ (void)set%s:(%s)value;\n", NameTable.capitalize(accessorName), objcType);
}
}
if (typeNode instanceof EnumDeclaration) {
for (EnumConstantDeclaration constant : ((EnumDeclaration) typeNode).getEnumConstants()) {
String accessorName = nameTable.getStaticAccessorName(constant.getVariableElement());
if (options.nullability()) {
printf("\n+ (%s * __nonnull)%s;\n", typeName, accessorName);
} else {
printf("\n+ (%s *)%s;\n", typeName, accessorName);
}
}
}
}
}
use of com.google.devtools.j2objc.ast.EnumConstantDeclaration in project j2objc by google.
the class TypeDeclarationGenerator method printEnumConstants.
private void printEnumConstants() {
if (typeNode instanceof EnumDeclaration) {
newline();
println("/*! INTERNAL ONLY - Use enum accessors declared below. */");
printf("FOUNDATION_EXPORT %s *%s_values_[];\n", typeName, typeName);
for (EnumConstantDeclaration constant : ((EnumDeclaration) typeNode).getEnumConstants()) {
String varName = nameTable.getVariableBaseName(constant.getVariableElement());
newline();
JavadocGenerator.printDocComment(getBuilder(), constant.getJavadoc());
printf("inline %s *%s_get_%s();\n", typeName, typeName, varName);
printf("J2OBJC_ENUM_CONSTANT(%s, %s)\n", typeName, varName);
}
}
}
Aggregations