Search in sources :

Example 1 with NumberLiteral

use of com.google.devtools.j2objc.ast.NumberLiteral 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 CastExpression(voidType, new ParenthesizedExpression(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 : ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) 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) CastExpression(com.google.devtools.j2objc.ast.CastExpression) NumberLiteral(com.google.devtools.j2objc.ast.NumberLiteral)

Example 2 with NumberLiteral

use of com.google.devtools.j2objc.ast.NumberLiteral in project j2objc by google.

the class TreeConverter method convertNumberLiteral.

private static TreeNode convertNumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral node) {
    Object constantValue = node.resolveConstantExpressionValue();
    assert constantValue instanceof Number;
    Number value = (Number) constantValue;
    ITypeBinding typeBinding = node.resolveTypeBinding();
    return convertExpression(node, new NumberLiteral(value, BindingConverter.getType(typeBinding)).setToken(node.getToken()));
}
Also used : ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) NumberLiteral(com.google.devtools.j2objc.ast.NumberLiteral)

Example 3 with NumberLiteral

use of com.google.devtools.j2objc.ast.NumberLiteral 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());
        String stringLiteralName = options.stripEnumConstants() ? ENUM_NAME_STRIPPED : ElementUtil.getName(varElement);
        creation.addArgument(new StringLiteral(stringLiteralName, 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)

Aggregations

NumberLiteral (com.google.devtools.j2objc.ast.NumberLiteral)3 Assignment (com.google.devtools.j2objc.ast.Assignment)2 EnumConstantDeclaration (com.google.devtools.j2objc.ast.EnumConstantDeclaration)2 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)2 ForStatement (com.google.devtools.j2objc.ast.ForStatement)2 NativeStatement (com.google.devtools.j2objc.ast.NativeStatement)2 SimpleName (com.google.devtools.j2objc.ast.SimpleName)2 Statement (com.google.devtools.j2objc.ast.Statement)2 StringLiteral (com.google.devtools.j2objc.ast.StringLiteral)2 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)2 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)2 VariableElement (javax.lang.model.element.VariableElement)2 CastExpression (com.google.devtools.j2objc.ast.CastExpression)1 ClassInstanceCreation (com.google.devtools.j2objc.ast.ClassInstanceCreation)1 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)1 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)1 NativeExpression (com.google.devtools.j2objc.ast.NativeExpression)1 ParenthesizedExpression (com.google.devtools.j2objc.ast.ParenthesizedExpression)1 FunctionElement (com.google.devtools.j2objc.types.FunctionElement)1 GeneratedTypeElement (com.google.devtools.j2objc.types.GeneratedTypeElement)1