Search in sources :

Example 11 with SingleVariableDeclaration

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

the class StatementGenerator method printMultiCatch.

private void printMultiCatch(CatchClause node) {
    SingleVariableDeclaration exception = node.getException();
    for (Type exceptionType : ((UnionType) exception.getType()).getTypes()) {
        buffer.append("@catch (");
        exceptionType.accept(this);
        buffer.append(" *");
        buffer.append(nameTable.getVariableQualifiedName(exception.getVariableElement()));
        buffer.append(") {\n");
        printStatements(node.getBody().getStatements());
        buffer.append("}\n");
    }
}
Also used : UnionType(com.google.devtools.j2objc.ast.UnionType) QualifiedType(com.google.devtools.j2objc.ast.QualifiedType) PrimitiveType(com.google.devtools.j2objc.ast.PrimitiveType) IntersectionType(com.google.devtools.j2objc.ast.IntersectionType) UnionType(com.google.devtools.j2objc.ast.UnionType) Type(com.google.devtools.j2objc.ast.Type) NameQualifiedType(com.google.devtools.j2objc.ast.NameQualifiedType) SimpleType(com.google.devtools.j2objc.ast.SimpleType) ArrayType(com.google.devtools.j2objc.ast.ArrayType) SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration)

Example 12 with SingleVariableDeclaration

use of com.google.devtools.j2objc.ast.SingleVariableDeclaration 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 rhs = TypeUtil.isReferenceType(memberType) ? "RETAIN_(" + propName + ")" : propName;
        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 SingleVariableDeclaration

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

the class TreeConverter method convertSingleVariableDeclaration.

private static TreeNode convertSingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration node) {
    SingleVariableDeclaration newNode = new SingleVariableDeclaration();
    convertVariableDeclaration(node, newNode);
    for (Object modifier : node.modifiers()) {
        if (modifier instanceof org.eclipse.jdt.core.dom.Annotation) {
            newNode.addAnnotation((Annotation) TreeConverter.convert(modifier));
        }
    }
    return newNode.setType((Type) TreeConverter.convert(node.getType())).setIsVarargs(node.isVarargs());
}
Also used : ParameterizedType(com.google.devtools.j2objc.ast.ParameterizedType) QualifiedType(com.google.devtools.j2objc.ast.QualifiedType) AnnotatableType(com.google.devtools.j2objc.ast.AnnotatableType) Type(com.google.devtools.j2objc.ast.Type) NameQualifiedType(com.google.devtools.j2objc.ast.NameQualifiedType) SimpleType(com.google.devtools.j2objc.ast.SimpleType) DeclaredType(javax.lang.model.type.DeclaredType) ArrayType(com.google.devtools.j2objc.ast.ArrayType) PrimitiveType(com.google.devtools.j2objc.ast.PrimitiveType) IntersectionType(com.google.devtools.j2objc.ast.IntersectionType) UnionType(com.google.devtools.j2objc.ast.UnionType) ExecutableType(javax.lang.model.type.ExecutableType) SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) NormalAnnotation(com.google.devtools.j2objc.ast.NormalAnnotation) PropertyAnnotation(com.google.devtools.j2objc.ast.PropertyAnnotation) Annotation(com.google.devtools.j2objc.ast.Annotation) SingleMemberAnnotation(com.google.devtools.j2objc.ast.SingleMemberAnnotation) MarkerAnnotation(com.google.devtools.j2objc.ast.MarkerAnnotation)

Example 14 with SingleVariableDeclaration

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

the class Functionizer method setFunctionCaller.

/**
   *  Replace method block statements with single statement that invokes function.
   */
private void setFunctionCaller(MethodDeclaration method, ExecutableElement methodElement) {
    TypeMirror returnType = methodElement.getReturnType();
    TypeElement declaringClass = ElementUtil.getDeclaringClass(methodElement);
    Block body = new Block();
    method.setBody(body);
    method.removeModifiers(Modifier.NATIVE);
    List<Statement> stmts = body.getStatements();
    FunctionInvocation invocation = new FunctionInvocation(newFunctionElement(methodElement), returnType);
    List<Expression> args = invocation.getArguments();
    if (!ElementUtil.isStatic(methodElement)) {
        args.add(new ThisExpression(declaringClass.asType()));
    }
    for (SingleVariableDeclaration param : method.getParameters()) {
        args.add(new SimpleName(param.getVariableElement()));
    }
    if (TypeUtil.isVoid(returnType)) {
        stmts.add(new ExpressionStatement(invocation));
        if (ElementUtil.isConstructor(methodElement)) {
            stmts.add(new ReturnStatement(new ThisExpression(declaringClass.asType())));
        }
    } else {
        stmts.add(new ReturnStatement(invocation));
    }
}
Also used : FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) TypeElement(javax.lang.model.element.TypeElement) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) Statement(com.google.devtools.j2objc.ast.Statement) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) SimpleName(com.google.devtools.j2objc.ast.SimpleName) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) TypeMirror(javax.lang.model.type.TypeMirror) Expression(com.google.devtools.j2objc.ast.Expression) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) Block(com.google.devtools.j2objc.ast.Block)

Example 15 with SingleVariableDeclaration

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

the class Functionizer method addDisallowedConstructors.

/**
   * Declare any inherited constructors that aren't allowed to be accessed in Java
   * with a NS_UNAVAILABLE macro, so that clang will flag such access from native
   * code as an error.
   */
private void addDisallowedConstructors(TypeDeclaration node) {
    TypeElement typeElement = node.getTypeElement();
    TypeElement superClass = ElementUtil.getSuperclass(typeElement);
    if (ElementUtil.isPrivateInnerType(typeElement) || ElementUtil.isAbstract(typeElement) || superClass == null) {
        return;
    }
    Set<String> constructors = new HashSet<>();
    for (ExecutableElement constructor : ElementUtil.getConstructors(typeElement)) {
        constructors.add(nameTable.getMethodSelector(constructor));
    }
    Map<String, ExecutableElement> inheritedConstructors = new HashMap<>();
    // Add super constructors that have unique parameter lists.
    for (ExecutableElement superC : ElementUtil.getConstructors(superClass)) {
        if (ElementUtil.isPrivate(superC)) {
            // Skip private super constructors since they're already unavailable.
            continue;
        }
        String selector = nameTable.getMethodSelector(superC);
        if (!constructors.contains(selector)) {
            inheritedConstructors.put(selector, superC);
        }
    }
    for (Map.Entry<String, ExecutableElement> entry : inheritedConstructors.entrySet()) {
        ExecutableElement oldConstructor = entry.getValue();
        GeneratedExecutableElement newConstructor = GeneratedExecutableElement.newConstructorWithSelector(entry.getKey(), typeElement, typeUtil);
        MethodDeclaration decl = new MethodDeclaration(newConstructor).setUnavailable(true);
        decl.addModifiers(Modifier.ABSTRACT);
        int count = 0;
        for (VariableElement param : oldConstructor.getParameters()) {
            VariableElement newParam = GeneratedVariableElement.newParameter("arg" + count++, param.asType(), newConstructor);
            newConstructor.addParameter(newParam);
            decl.addParameter(new SingleVariableDeclaration(newParam));
        }
        addImplicitParameters(decl, ElementUtil.getDeclaringClass(oldConstructor));
        node.addBodyDeclaration(decl);
    }
}
Also used : HashMap(java.util.HashMap) TypeElement(javax.lang.model.element.TypeElement) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) VariableElement(javax.lang.model.element.VariableElement) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Aggregations

SingleVariableDeclaration (com.google.devtools.j2objc.ast.SingleVariableDeclaration)19 VariableElement (javax.lang.model.element.VariableElement)13 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)10 ExecutableElement (javax.lang.model.element.ExecutableElement)8 Block (com.google.devtools.j2objc.ast.Block)7 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)7 TypeElement (javax.lang.model.element.TypeElement)7 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)6 TypeMirror (javax.lang.model.type.TypeMirror)6 NativeStatement (com.google.devtools.j2objc.ast.NativeStatement)4 SimpleName (com.google.devtools.j2objc.ast.SimpleName)4 ArrayType (com.google.devtools.j2objc.ast.ArrayType)3 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)3 FunctionDeclaration (com.google.devtools.j2objc.ast.FunctionDeclaration)3 PrimitiveType (com.google.devtools.j2objc.ast.PrimitiveType)3 ReturnStatement (com.google.devtools.j2objc.ast.ReturnStatement)3 SimpleType (com.google.devtools.j2objc.ast.SimpleType)3 Statement (com.google.devtools.j2objc.ast.Statement)3 Type (com.google.devtools.j2objc.ast.Type)3 UnionType (com.google.devtools.j2objc.ast.UnionType)3