Search in sources :

Example 6 with EnumConstantDeclaration

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)));
    }
}
Also used : ClassInstanceCreation(com.google.devtools.j2objc.ast.ClassInstanceCreation) Assignment(com.google.devtools.j2objc.ast.Assignment) EnumConstantDeclaration(com.google.devtools.j2objc.ast.EnumConstantDeclaration) StringLiteral(com.google.devtools.j2objc.ast.StringLiteral) 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) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) NumberLiteral(com.google.devtools.j2objc.ast.NumberLiteral)

Example 7 with EnumConstantDeclaration

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);"));
}
Also used : GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) 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) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) SimpleName(com.google.devtools.j2objc.ast.SimpleName) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) EnumConstantDeclaration(com.google.devtools.j2objc.ast.EnumConstantDeclaration) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) TypeMirror(javax.lang.model.type.TypeMirror) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) Expression(com.google.devtools.j2objc.ast.Expression) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) CommaExpression(com.google.devtools.j2objc.ast.CommaExpression) VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Block(com.google.devtools.j2objc.ast.Block) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) ForStatement(com.google.devtools.j2objc.ast.ForStatement)

Example 8 with EnumConstantDeclaration

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));
        }
    }
}
Also used : EnumConstantDeclaration(com.google.devtools.j2objc.ast.EnumConstantDeclaration) TypeMirror(javax.lang.model.type.TypeMirror) VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) VariableElement(javax.lang.model.element.VariableElement) EnumDeclaration(com.google.devtools.j2objc.ast.EnumDeclaration)

Example 9 with EnumConstantDeclaration

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);
                }
            }
        }
    }
}
Also used : EnumConstantDeclaration(com.google.devtools.j2objc.ast.EnumConstantDeclaration) VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) VariableElement(javax.lang.model.element.VariableElement) EnumDeclaration(com.google.devtools.j2objc.ast.EnumDeclaration)

Example 10 with EnumConstantDeclaration

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

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