use of com.google.devtools.j2objc.ast.EnumConstantDeclaration in project j2objc by google.
the class TreeConverter method convertVariableDeclaration.
private TreeNode convertVariableDeclaration(JCTree.JCVariableDecl node) {
VarSymbol var = node.sym;
if (var.getKind() == ElementKind.FIELD) {
FieldDeclaration newNode = new FieldDeclaration(var, (Expression) convert(node.getInitializer()));
convertBodyDeclaration(node, node.getModifiers(), newNode, var);
return newNode;
}
if (var.getKind() == ElementKind.LOCAL_VARIABLE) {
return new VariableDeclarationStatement(var, (Expression) convert(node.getInitializer()));
}
if (var.getKind() == ElementKind.ENUM_CONSTANT) {
EnumConstantDeclaration newNode = new EnumConstantDeclaration().setVariableElement(var);
convertBodyDeclaration(node, node.getModifiers(), newNode, var);
ClassInstanceCreation init = (ClassInstanceCreation) convert(node.getInitializer());
TreeUtil.moveList(init.getArguments(), newNode.getArguments());
if (init.getAnonymousClassDeclaration() != null) {
newNode.setAnonymousClassDeclaration(TreeUtil.remove(init.getAnonymousClassDeclaration()));
}
return newNode.setExecutablePair(init.getExecutablePair()).setVarargsType(init.getVarargsType());
}
return convertSingleVariable(node);
}
use of com.google.devtools.j2objc.ast.EnumConstantDeclaration in project j2objc by google.
the class TypeDeclarationGenerator method printNativeEnum.
private void printNativeEnum() {
if (!(typeNode instanceof EnumDeclaration)) {
return;
}
List<EnumConstantDeclaration> constants = ((EnumDeclaration) typeNode).getEnumConstants();
String nativeName = NameTable.getNativeEnumName(typeName);
// C enum declaration and generate the type declaration.
if (!constants.isEmpty()) {
newline();
printf("typedef NS_ENUM(NSUInteger, %s) {\n", nativeName);
// Print C enum typedef.
indent();
int ordinal = 0;
for (EnumConstantDeclaration constant : constants) {
printIndent();
printf("%s_%s = %d,\n", nativeName, nameTable.getVariableBaseName(constant.getVariableElement()), ordinal++);
}
unindent();
print("};\n");
}
}
use of com.google.devtools.j2objc.ast.EnumConstantDeclaration in project j2objc by google.
the class EnumRewriter method isSimpleEnum.
/**
* Returns true if an enum doesn't have custom or renamed constructors,
* vararg constructors or constants with anonymous class extensions.
*/
private boolean isSimpleEnum(EnumDeclaration node) {
TypeElement type = node.getTypeElement();
for (EnumConstantDeclaration constant : node.getEnumConstants()) {
ExecutableElement method = constant.getExecutableElement();
if (method.getParameters().size() > 0 || method.isVarArgs()) {
return false;
}
if (ElementUtil.hasAnnotation(method, ObjectiveCName.class)) {
return false;
}
TypeElement valueType = ElementUtil.getDeclaringClass(method);
if (valueType != type) {
return false;
}
}
return true;
}
use of com.google.devtools.j2objc.ast.EnumConstantDeclaration in project j2objc by google.
the class EnumRewriter method addNonArcInitialization.
private void addNonArcInitialization(EnumDeclaration node) {
TypeElement type = node.getTypeElement();
int baseTypeCount = 0;
List<Statement> sizeStatements = new ArrayList<>();
List<Statement> initStatements = new ArrayList<>();
TypeMirror voidType = typeUtil.getVoid();
VariableElement localEnum = GeneratedVariableElement.newLocalVar("e", TypeUtil.ID_TYPE, null);
int i = 0;
for (EnumConstantDeclaration constant : node.getEnumConstants()) {
VariableElement varElement = constant.getVariableElement();
String varName = ElementUtil.getName(varElement);
ExecutableElement methodElement = constant.getExecutableElement();
TypeElement valueType = ElementUtil.getDeclaringClass(methodElement);
boolean isAnonymous = valueType != type;
String classExpr = isAnonymous ? "[" + nameTable.getFullName(valueType) + " class]" : "self";
String sizeName = "objSize" + (isAnonymous ? "_" + varName : "");
if (isAnonymous) {
sizeStatements.add(new NativeStatement(UnicodeUtils.format("size_t %s = class_getInstanceSize(%s);", sizeName, classExpr)));
sizeStatements.add(new NativeStatement(UnicodeUtils.format("allocSize += %s;", sizeName)));
} else {
baseTypeCount++;
}
initStatements.add(new ExpressionStatement(new CommaExpression(new Assignment(new SimpleName(varElement), new Assignment(new SimpleName(localEnum), new NativeExpression(UnicodeUtils.format("objc_constructInstance(%s, (void *)ptr)", classExpr), type.asType()))), new NativeExpression("ptr += " + sizeName, voidType))));
String initName = nameTable.getFullFunctionName(methodElement);
FunctionElement initElement = new FunctionElement(initName, voidType, valueType).addParameters(valueType.asType()).addParameters(ElementUtil.asTypes(methodElement.getParameters()));
FunctionInvocation initFunc = new FunctionInvocation(initElement, voidType);
initFunc.addArgument(new SimpleName(localEnum));
TreeUtil.copyList(constant.getArguments(), initFunc.getArguments());
initFunc.addArgument(new StringLiteral(varName, typeUtil));
initFunc.addArgument(new NumberLiteral(i++, typeUtil));
initStatements.add(new ExpressionStatement(initFunc));
}
List<Statement> stmts = node.getClassInitStatements().subList(0, 0);
if (baseTypeCount == 0) {
stmts.add(new NativeStatement("size_t allocSize = 0;"));
} else {
stmts.add(new NativeStatement("size_t objSize = class_getInstanceSize(self);"));
stmts.add(new NativeStatement(UnicodeUtils.format("size_t allocSize = %s * objSize;", baseTypeCount)));
}
stmts.addAll(sizeStatements);
stmts.add(new NativeStatement("uintptr_t ptr = (uintptr_t)calloc(allocSize, 1);"));
stmts.add(new VariableDeclarationStatement(localEnum, null));
stmts.addAll(initStatements);
}
use of com.google.devtools.j2objc.ast.EnumConstantDeclaration in project j2objc by google.
the class TreeConverter method convertEnumConstantDeclaration.
private static TreeNode convertEnumConstantDeclaration(org.eclipse.jdt.core.dom.EnumConstantDeclaration node) {
EnumConstantDeclaration newNode = new EnumConstantDeclaration();
convertBodyDeclaration(node, newNode);
IMethodBinding methodBinding = node.resolveConstructorBinding();
JdtExecutableElement element = (JdtExecutableElement) BindingConverter.getExecutableElement(methodBinding);
newNode.setVariableElement(BindingConverter.getVariableElement(node.resolveVariable())).setExecutablePair(new ExecutablePair(element, BindingConverter.getType(methodBinding))).setVarargsType(getVarargsType(methodBinding, node.arguments()));
for (Object argument : node.arguments()) {
newNode.addArgument((Expression) convert(argument));
}
org.eclipse.jdt.core.dom.AnonymousClassDeclaration anonymousClassDecl = node.getAnonymousClassDeclaration();
if (anonymousClassDecl != null) {
newNode.setAnonymousClassDeclaration(convertAnonymousClassDeclaration(anonymousClassDecl, element, methodBinding));
}
return newNode;
}
Aggregations