Search in sources :

Example 1 with NativeExpression

use of com.google.devtools.j2objc.ast.NativeExpression 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 NativeExpression

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

the class SuperMethodInvocationRewriter method endVisit.

@Override
public void endVisit(SuperMethodInvocation node) {
    Expression receiver = node.getReceiver();
    ExecutableElement method = node.getExecutableElement();
    TypeMirror exprType = node.getTypeMirror();
    assert !ElementUtil.isDefault(method) : "Default methods are handled in Functionizer.java";
    if (receiver == null) {
        return;
    }
    VariableElement var = TreeUtil.getVariableElement(receiver);
    assert var != null : "Expected receiver to be a variable";
    TypeMirror receiverType = var.asType();
    TypeElement receiverElem = TypeUtil.asTypeElement(receiverType);
    SuperMethodElementPair superMethod = new SuperMethodElementPair(receiverElem, method);
    superMethods.add(superMethod);
    FunctionElement element = new FunctionElement(getSuperFunctionName(superMethod), exprType, receiverElem).addParameters(receiverType, TypeUtil.ID_TYPE).addParameters(ElementUtil.asTypes(method.getParameters()));
    FunctionInvocation invocation = new FunctionInvocation(element, exprType);
    List<Expression> args = invocation.getArguments();
    args.add(TreeUtil.remove(receiver));
    String selectorExpr = UnicodeUtils.format("@selector(%s)", nameTable.getMethodSelector(method));
    args.add(new NativeExpression(selectorExpr, TypeUtil.ID_TYPE));
    TreeUtil.copyList(node.getArguments(), args);
    node.replaceWith(invocation);
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) Expression(com.google.devtools.j2objc.ast.Expression) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement)

Example 3 with NativeExpression

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

the class StaticVarRewriter method rewriteStaticAccess.

private void rewriteStaticAccess(Expression node) {
    VariableElement var = TreeUtil.getVariableElement(node);
    if (var == null || !needsStaticLoad(node, var)) {
        return;
    }
    TypeElement declaringClass = ElementUtil.getDeclaringClass(var);
    boolean assignable = TranslationUtil.isAssigned(node);
    StringBuilder code = new StringBuilder(ElementUtil.isEnumConstant(var) ? "JreLoadEnum" : "JreLoadStatic");
    TypeMirror exprType = var.asType();
    if (assignable) {
        code.append("Ref");
        exprType = new PointerType(exprType);
    }
    code.append("(");
    code.append(nameTable.getFullName(declaringClass));
    code.append(", ");
    code.append(nameTable.getVariableShortName(var));
    code.append(")");
    NativeExpression nativeExpr = new NativeExpression(code.toString(), exprType);
    nativeExpr.addImportType(declaringClass.asType());
    Expression newNode = nativeExpr;
    if (assignable) {
        newNode = new PrefixExpression(var.asType(), PrefixExpression.Operator.DEREFERENCE, newNode);
    }
    node.replaceWith(newNode);
}
Also used : NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) TypeMirror(javax.lang.model.type.TypeMirror) Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) CommaExpression(com.google.devtools.j2objc.ast.CommaExpression) TypeElement(javax.lang.model.element.TypeElement) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) PointerType(com.google.devtools.j2objc.types.PointerType) VariableElement(javax.lang.model.element.VariableElement)

Example 4 with NativeExpression

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

the class SwitchRewriter method endVisit.

@Override
public void endVisit(SwitchCase node) {
    Expression expr = node.getExpression();
    VariableElement var = expr != null ? TreeUtil.getVariableElement(expr) : null;
    if (var == null) {
        return;
    }
    TypeMirror type = var.asType();
    if (TypeUtil.isEnum(type)) {
        String enumValue = NameTable.getNativeEnumName(nameTable.getFullName(TypeUtil.asTypeElement(type))) + "_" + nameTable.getVariableBaseName(var);
        node.setExpression(new NativeExpression(enumValue, typeUtil.getInt()));
    } else if (type.getKind().isPrimitive() && var.getKind() == ElementKind.LOCAL_VARIABLE) {
        Object value = var.getConstantValue();
        if (value != null) {
            node.setExpression(TreeUtil.newLiteral(value, typeUtil));
        }
    }
}
Also used : NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) Expression(com.google.devtools.j2objc.ast.Expression) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) TypeMirror(javax.lang.model.type.TypeMirror) VariableElement(javax.lang.model.element.VariableElement)

Aggregations

NativeExpression (com.google.devtools.j2objc.ast.NativeExpression)4 VariableElement (javax.lang.model.element.VariableElement)4 TypeMirror (javax.lang.model.type.TypeMirror)4 Expression (com.google.devtools.j2objc.ast.Expression)3 TypeElement (javax.lang.model.element.TypeElement)3 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)2 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)2 FunctionElement (com.google.devtools.j2objc.types.FunctionElement)2 ExecutableElement (javax.lang.model.element.ExecutableElement)2 Assignment (com.google.devtools.j2objc.ast.Assignment)1 CastExpression (com.google.devtools.j2objc.ast.CastExpression)1 EnumConstantDeclaration (com.google.devtools.j2objc.ast.EnumConstantDeclaration)1 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)1 ForStatement (com.google.devtools.j2objc.ast.ForStatement)1 NativeStatement (com.google.devtools.j2objc.ast.NativeStatement)1 NumberLiteral (com.google.devtools.j2objc.ast.NumberLiteral)1 ParenthesizedExpression (com.google.devtools.j2objc.ast.ParenthesizedExpression)1 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)1 SimpleName (com.google.devtools.j2objc.ast.SimpleName)1 Statement (com.google.devtools.j2objc.ast.Statement)1