Search in sources :

Example 1 with EnumConstantDeclaration

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);
}
Also used : ClassInstanceCreation(com.google.devtools.j2objc.ast.ClassInstanceCreation) EnumConstantDeclaration(com.google.devtools.j2objc.ast.EnumConstantDeclaration) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) FieldDeclaration(com.google.devtools.j2objc.ast.FieldDeclaration)

Example 2 with EnumConstantDeclaration

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");
    }
}
Also used : EnumConstantDeclaration(com.google.devtools.j2objc.ast.EnumConstantDeclaration) EnumDeclaration(com.google.devtools.j2objc.ast.EnumDeclaration)

Example 3 with EnumConstantDeclaration

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;
}
Also used : EnumConstantDeclaration(com.google.devtools.j2objc.ast.EnumConstantDeclaration) GeneratedTypeElement(com.google.devtools.j2objc.types.GeneratedTypeElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 4 with EnumConstantDeclaration

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);
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) GeneratedTypeElement(com.google.devtools.j2objc.types.GeneratedTypeElement) TypeElement(javax.lang.model.element.TypeElement) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) ForStatement(com.google.devtools.j2objc.ast.ForStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Statement(com.google.devtools.j2objc.ast.Statement) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) ExecutableElement(javax.lang.model.element.ExecutableElement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) ArrayList(java.util.ArrayList) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) Assignment(com.google.devtools.j2objc.ast.Assignment) EnumConstantDeclaration(com.google.devtools.j2objc.ast.EnumConstantDeclaration) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) CommaExpression(com.google.devtools.j2objc.ast.CommaExpression) StringLiteral(com.google.devtools.j2objc.ast.StringLiteral) TypeMirror(javax.lang.model.type.TypeMirror) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) NumberLiteral(com.google.devtools.j2objc.ast.NumberLiteral)

Example 5 with EnumConstantDeclaration

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;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) EnumConstantDeclaration(com.google.devtools.j2objc.ast.EnumConstantDeclaration) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair)

Aggregations

EnumConstantDeclaration (com.google.devtools.j2objc.ast.EnumConstantDeclaration)10 VariableElement (javax.lang.model.element.VariableElement)5 EnumDeclaration (com.google.devtools.j2objc.ast.EnumDeclaration)4 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)4 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)3 ForStatement (com.google.devtools.j2objc.ast.ForStatement)3 NativeStatement (com.google.devtools.j2objc.ast.NativeStatement)3 SimpleName (com.google.devtools.j2objc.ast.SimpleName)3 Statement (com.google.devtools.j2objc.ast.Statement)3 VariableDeclarationFragment (com.google.devtools.j2objc.ast.VariableDeclarationFragment)3 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)3 TypeMirror (javax.lang.model.type.TypeMirror)3 Assignment (com.google.devtools.j2objc.ast.Assignment)2 ClassInstanceCreation (com.google.devtools.j2objc.ast.ClassInstanceCreation)2 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)2 NativeExpression (com.google.devtools.j2objc.ast.NativeExpression)2 NumberLiteral (com.google.devtools.j2objc.ast.NumberLiteral)2 StringLiteral (com.google.devtools.j2objc.ast.StringLiteral)2 GeneratedTypeElement (com.google.devtools.j2objc.types.GeneratedTypeElement)2 ExecutableElement (javax.lang.model.element.ExecutableElement)2