Search in sources :

Example 11 with NativeStatement

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

the class AnnotationRewriter method addEqualsMethod.

private void addEqualsMethod(AnnotationTypeDeclaration node) {
    TypeElement typeElement = node.getTypeElement();
    GeneratedExecutableElement equalsElement = GeneratedExecutableElement.newMethodWithSelector("isEqual:", typeUtil.getBoolean(), typeElement);
    GeneratedVariableElement paramElement = GeneratedVariableElement.newParameter("obj", typeUtil.getJavaObject().asType(), equalsElement);
    equalsElement.addParameter(paramElement);
    NativeStatement stmt = new NativeStatement("return JreAnnotationEquals(self, obj);");
    node.addBodyDeclaration(new MethodDeclaration(equalsElement).addParameter(new SingleVariableDeclaration(paramElement)).setBody(new Block().addStatement(stmt)).setModifiers(java.lang.reflect.Modifier.PUBLIC));
}
Also used : GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) TypeElement(javax.lang.model.element.TypeElement) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) Block(com.google.devtools.j2objc.ast.Block)

Example 12 with NativeStatement

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

the class AnnotationRewriter method addConstructor.

private void addConstructor(AnnotationTypeDeclaration node, Map<ExecutableElement, VariableElement> fieldElements) {
    TypeElement type = node.getTypeElement();
    String typeName = nameTable.getFullName(type);
    FunctionDeclaration constructorDecl = new FunctionDeclaration("create_" + typeName, type.asType());
    Block constructorBody = new Block();
    constructorDecl.setBody(constructorBody);
    List<Statement> stmts = constructorBody.getStatements();
    stmts.add(new NativeStatement(UnicodeUtils.format("%s *self = AUTORELEASE([[%s alloc] init]);", typeName, typeName)));
    for (ExecutableElement memberElement : ElementUtil.getSortedAnnotationMembers(type)) {
        TypeMirror memberType = memberElement.getReturnType();
        String propName = NameTable.getAnnotationPropertyName(memberElement);
        String fieldName = nameTable.getVariableShortName(fieldElements.get(memberElement));
        VariableElement param = GeneratedVariableElement.newParameter(propName, memberType, null);
        constructorDecl.addParameter(new SingleVariableDeclaration(param));
        String paramName = nameTable.getVariableShortName(param);
        String rhs = TypeUtil.isReferenceType(memberType) ? "RETAIN_(" + paramName + ")" : paramName;
        stmts.add(new NativeStatement("self->" + fieldName + " = " + rhs + ";"));
    }
    stmts.add(new NativeStatement("return self;"));
    node.addBodyDeclaration(constructorDecl);
}
Also used : FunctionDeclaration(com.google.devtools.j2objc.ast.FunctionDeclaration) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) Statement(com.google.devtools.j2objc.ast.Statement) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Block(com.google.devtools.j2objc.ast.Block) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement)

Example 13 with NativeStatement

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

the class AnnotationRewriter method createDescriptionMethod.

private MethodDeclaration createDescriptionMethod(TypeElement type, List<AnnotationTypeMemberDeclaration> members, Map<ExecutableElement, VariableElement> fieldElements) {
    ExecutableElement descriptionElement = GeneratedExecutableElement.newMethodWithSelector("description", typeUtil.getJavaString().asType(), type);
    MethodDeclaration descriptionMethod = new MethodDeclaration(descriptionElement);
    descriptionMethod.setHasDeclaration(false);
    Block descriptionBody = new Block();
    descriptionMethod.setBody(descriptionBody);
    StringBuilder description = new StringBuilder();
    StringBuilder fields = new StringBuilder();
    if (!members.isEmpty()) {
        description.append("@\"@" + elementUtil.getBinaryName(type) + "(");
        Iterator<AnnotationTypeMemberDeclaration> iter = members.iterator();
        while (iter.hasNext()) {
            AnnotationTypeMemberDeclaration member = iter.next();
            ExecutableElement memberElement = member.getExecutableElement();
            String propName = NameTable.getAnnotationPropertyName(memberElement);
            String fieldName = nameTable.getVariableShortName(fieldElements.get(memberElement));
            description.append(propName + "=%" + TypeUtil.getObjcFormatSpecifier(memberElement.getReturnType()));
            fields.append(fieldName);
            if (iter.hasNext()) {
                description.append(", ");
                fields.append(", ");
            }
        }
        description.append(")\", " + fields);
        descriptionBody.addStatement(new NativeStatement("return [NSString stringWithFormat:" + description + "];"));
    } else {
        descriptionBody.addStatement(new ReturnStatement(new StringLiteral("@" + elementUtil.getBinaryName(type) + "()", typeUtil)));
    }
    return descriptionMethod;
}
Also used : AnnotationTypeMemberDeclaration(com.google.devtools.j2objc.ast.AnnotationTypeMemberDeclaration) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) StringLiteral(com.google.devtools.j2objc.ast.StringLiteral) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) Block(com.google.devtools.j2objc.ast.Block)

Example 14 with NativeStatement

use of com.google.devtools.j2objc.ast.NativeStatement 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));
    // Create a local array of enum names only if reflection is stripped but
    // enum constants are not. For non-stripped classes, enum names are now
    // retrieved from metadata, to avoid duplicates.
    boolean useNamesArray = options.stripReflection() && !options.stripEnumConstants();
    if (useNamesArray) {
        StringBuilder sb = new StringBuilder("id names[] = {\n  ");
        for (EnumConstantDeclaration constant : constants) {
            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("((void)(" + enumClassName + "_values_[i] = e = objc_constructInstance(self, (void *)ptr)), ptr += objSize);"));
    if (useNamesArray) {
        loopBody.addStatement(new NativeStatement(enumClassName + "_initWithNSString_withInt_(e, names[i], i);"));
    } else if (options.stripEnumConstants()) {
        String statementText = enumClassName + "_initWithNSString_withInt_(e, " + ENUM_NAME_STRIPPED + ", i);";
        loopBody.addStatement(new NativeStatement(statementText));
    } else {
        loopBody.addStatement(new NativeStatement(enumClassName + "_initWithNSString_withInt_(e, JreEnumConstantName(" + enumClassName + "_class_(), 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) CastExpression(com.google.devtools.j2objc.ast.CastExpression) 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) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) 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 15 with NativeStatement

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

the class SuperMethodInvocationRewriter method endVisit.

@Override
public void endVisit(CompilationUnit unit) {
    for (SuperMethodElementPair superMethod : superMethods) {
        String funcName = getSuperFunctionName(superMethod);
        String signature = getSuperFunctionSignature(superMethod.method);
        // Add declarations for the function pointers to call.
        unit.addNativeBlock(NativeDeclaration.newOuterDeclaration(null, "static " + UnicodeUtils.format(signature, funcName) + ";"));
        // Look up the implementations in the static initialization.
        AbstractTypeDeclaration typeNode = typeMap.get(superMethod.type);
        assert typeNode != null : "Type is expected to be in this compilation unit";
        String superclassName = nameTable.getFullName(ElementUtil.getSuperclass(superMethod.type));
        typeNode.addClassInitStatement(0, new NativeStatement(UnicodeUtils.format("%s = (%s)[%s instanceMethodForSelector:@selector(%s)];", funcName, UnicodeUtils.format(signature, ""), superclassName, nameTable.getMethodSelector(superMethod.method))));
    }
}
Also used : NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration)

Aggregations

NativeStatement (com.google.devtools.j2objc.ast.NativeStatement)15 Block (com.google.devtools.j2objc.ast.Block)13 TypeElement (javax.lang.model.element.TypeElement)10 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)9 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)8 ExecutableElement (javax.lang.model.element.ExecutableElement)8 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)6 SingleVariableDeclaration (com.google.devtools.j2objc.ast.SingleVariableDeclaration)5 Statement (com.google.devtools.j2objc.ast.Statement)4 VariableElement (javax.lang.model.element.VariableElement)4 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)3 GeneratedTypeElement (com.google.devtools.j2objc.types.GeneratedTypeElement)3 TypeMirror (javax.lang.model.type.TypeMirror)3 CastExpression (com.google.devtools.j2objc.ast.CastExpression)2 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)2 EnumConstantDeclaration (com.google.devtools.j2objc.ast.EnumConstantDeclaration)2 ForStatement (com.google.devtools.j2objc.ast.ForStatement)2 FunctionDeclaration (com.google.devtools.j2objc.ast.FunctionDeclaration)2 NativeExpression (com.google.devtools.j2objc.ast.NativeExpression)2 ParenthesizedExpression (com.google.devtools.j2objc.ast.ParenthesizedExpression)2