Search in sources :

Example 1 with FunctionElement

use of com.google.devtools.j2objc.types.FunctionElement 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 2 with FunctionElement

use of com.google.devtools.j2objc.types.FunctionElement in project j2objc by google.

the class Functionizer method newFunctionElement.

private FunctionElement newFunctionElement(ExecutableElement method) {
    TypeElement declaringClass = ElementUtil.getDeclaringClass(method);
    FunctionElement element = new FunctionElement(nameTable.getFullFunctionName(method), method.getReturnType(), declaringClass);
    if (ElementUtil.isConstructor(method) || !ElementUtil.isStatic(method)) {
        element.addParameters(declaringClass.asType());
    }
    transferParams(method, element, declaringClass);
    return element;
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) TypeElement(javax.lang.model.element.TypeElement)

Example 3 with FunctionElement

use of com.google.devtools.j2objc.types.FunctionElement in project j2objc by google.

the class Functionizer method makeFunction.

/**
   * Create an equivalent function declaration for a given method.
   */
private FunctionDeclaration makeFunction(MethodDeclaration method) {
    ExecutableElement elem = method.getExecutableElement();
    TypeElement declaringClass = ElementUtil.getDeclaringClass(elem);
    boolean isInstanceMethod = !ElementUtil.isStatic(elem) && !ElementUtil.isConstructor(elem);
    FunctionDeclaration function = new FunctionDeclaration(nameTable.getFullFunctionName(elem), elem.getReturnType());
    function.setJniSignature(signatureGenerator.createJniFunctionSignature(elem));
    function.setLineNumber(method.getLineNumber());
    if (!ElementUtil.isStatic(elem)) {
        VariableElement var = GeneratedVariableElement.newParameter(NameTable.SELF_NAME, declaringClass.asType(), null);
        function.addParameter(new SingleVariableDeclaration(var));
    }
    TreeUtil.copyList(method.getParameters(), function.getParameters());
    function.setModifiers(method.getModifiers() & Modifier.STATIC);
    if (ElementUtil.isPrivate(elem) || (isInstanceMethod && !ElementUtil.isDefault(elem))) {
        function.addModifiers(Modifier.PRIVATE);
    } else {
        function.addModifiers(Modifier.PUBLIC);
    }
    if (Modifier.isNative(method.getModifiers())) {
        function.addModifiers(Modifier.NATIVE);
        return function;
    }
    function.setBody(TreeUtil.remove(method.getBody()));
    if (ElementUtil.isStatic(elem)) {
        // Add class initialization invocation, since this may be the first use of this class.
        String initName = UnicodeUtils.format("%s_initialize", nameTable.getFullName(declaringClass));
        TypeMirror voidType = typeUtil.getVoid();
        FunctionElement initElement = new FunctionElement(initName, voidType, declaringClass);
        FunctionInvocation initCall = new FunctionInvocation(initElement, voidType);
        function.getBody().addStatement(0, new ExpressionStatement(initCall));
    } else {
        FunctionConverter.convert(function);
    }
    return function;
}
Also used : FunctionDeclaration(com.google.devtools.j2objc.ast.FunctionDeclaration) FunctionElement(com.google.devtools.j2objc.types.FunctionElement) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) VariableElement(javax.lang.model.element.VariableElement)

Example 4 with FunctionElement

use of com.google.devtools.j2objc.types.FunctionElement in project j2objc by google.

the class Functionizer method newAllocatingConstructorElement.

private FunctionElement newAllocatingConstructorElement(ExecutableElement method) {
    TypeElement declaringClass = ElementUtil.getDeclaringClass(method);
    FunctionElement element = new FunctionElement(nameTable.getReleasingConstructorName(method), nameTable.getAllocatingConstructorName(method), declaringClass.asType(), declaringClass);
    transferParams(method, element, declaringClass);
    return element;
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) TypeElement(javax.lang.model.element.TypeElement)

Example 5 with FunctionElement

use of com.google.devtools.j2objc.types.FunctionElement in project j2objc by google.

the class Functionizer method endVisit.

@Override
public void endVisit(ClassInstanceCreation node) {
    ExecutableElement element = node.getExecutableElement();
    TypeElement type = ElementUtil.getDeclaringClass(element);
    FunctionElement funcElement = newAllocatingConstructorElement(element);
    FunctionInvocation invocation = new FunctionInvocation(funcElement, node.getTypeMirror());
    invocation.setHasRetainedResult(node.hasRetainedResult() || options.useARC());
    List<Expression> args = invocation.getArguments();
    Expression outerExpr = node.getExpression();
    if (outerExpr != null) {
        args.add(TreeUtil.remove(outerExpr));
    } else if (captureInfo.needsOuterParam(type)) {
        args.add(new ThisExpression(ElementUtil.getDeclaringClass(type).asType()));
    }
    Expression superOuterArg = node.getSuperOuterArg();
    if (superOuterArg != null) {
        args.add(TreeUtil.remove(superOuterArg));
    }
    TreeUtil.moveList(node.getCaptureArgs(), args);
    TreeUtil.moveList(node.getArguments(), args);
    node.replaceWith(invocation);
    assert funcElement.getParameterTypes().size() == args.size();
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) Expression(com.google.devtools.j2objc.ast.Expression) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) TypeElement(javax.lang.model.element.TypeElement) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement)

Aggregations

FunctionElement (com.google.devtools.j2objc.types.FunctionElement)27 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)25 TypeMirror (javax.lang.model.type.TypeMirror)20 Expression (com.google.devtools.j2objc.ast.Expression)14 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)12 TypeElement (javax.lang.model.element.TypeElement)11 ThisExpression (com.google.devtools.j2objc.ast.ThisExpression)10 PointerType (com.google.devtools.j2objc.types.PointerType)9 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)8 ExecutableElement (javax.lang.model.element.ExecutableElement)8 VariableElement (javax.lang.model.element.VariableElement)8 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)7 SimpleName (com.google.devtools.j2objc.ast.SimpleName)7 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)6 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)6 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)5 NativeExpression (com.google.devtools.j2objc.ast.NativeExpression)3 Assignment (com.google.devtools.j2objc.ast.Assignment)2 CastExpression (com.google.devtools.j2objc.ast.CastExpression)2 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)2