Search in sources :

Example 1 with SingleVariableDeclaration

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

the class EnhancedForRewriter method boxLoopVariable.

private void boxLoopVariable(EnhancedForStatement node, TypeMirror expressionType, VariableElement loopVariable) {
    DeclaredType iterableType = typeUtil.findSupertype(expressionType, "java.lang.Iterable");
    List<? extends TypeMirror> typeArgs = iterableType.getTypeArguments();
    assert typeArgs.size() == 1 && typeUtil.isBoxedType(typeArgs.get(0));
    VariableElement boxVariable = GeneratedVariableElement.newLocalVar("boxed__", typeArgs.get(0), null);
    node.setParameter(new SingleVariableDeclaration(boxVariable));
    makeBlock(node.getBody()).addStatement(0, new VariableDeclarationStatement(loopVariable, new SimpleName(boxVariable)));
}
Also used : SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) SimpleName(com.google.devtools.j2objc.ast.SimpleName) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) DeclaredType(javax.lang.model.type.DeclaredType)

Example 2 with SingleVariableDeclaration

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

the class EnumRewriter method addValueOfMethod.

private void addValueOfMethod(EnumDeclaration node) {
    TypeElement type = node.getTypeElement();
    ExecutableElement method = ElementUtil.findMethod(type, "valueOf", "java.lang.String");
    assert method != null : "Can't find valueOf method on enum type.";
    String typeName = nameTable.getFullName(type);
    int numConstants = node.getEnumConstants().size();
    VariableElement nameParam = GeneratedVariableElement.newParameter("name", method.getParameters().get(0).asType(), method);
    MethodDeclaration methodDecl = new MethodDeclaration(method);
    methodDecl.addParameter(new SingleVariableDeclaration(nameParam));
    Block body = new Block();
    methodDecl.setBody(body);
    StringBuilder impl = new StringBuilder();
    if (numConstants > 0) {
        impl.append(UnicodeUtils.format("  for (int i = 0; i < %s; i++) {\n" + "    %s *e = %s_values_[i];\n" + "    if ([name isEqual:[e name]]) {\n" + "      return e;\n" + "    }\n" + "  }\n", numConstants, typeName, typeName));
    }
    impl.append("  @throw create_JavaLangIllegalArgumentException_initWithNSString_(name);\n" + "  return nil;");
    body.addStatement(new NativeStatement(impl.toString()));
    node.addBodyDeclaration(methodDecl);
}
Also used : NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) GeneratedTypeElement(com.google.devtools.j2objc.types.GeneratedTypeElement) TypeElement(javax.lang.model.element.TypeElement) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) 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 3 with SingleVariableDeclaration

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

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

the class Functionizer method addImplicitParameters.

private void addImplicitParameters(MethodDeclaration node, TypeElement type) {
    List<SingleVariableDeclaration> methodParams = node.getParameters().subList(0, 0);
    for (VariableElement param : captureInfo.getImplicitPrefixParams(type)) {
        methodParams.add(new SingleVariableDeclaration(param));
    }
    methodParams = node.getParameters();
    for (VariableElement param : captureInfo.getImplicitPostfixParams(type)) {
        methodParams.add(new SingleVariableDeclaration(param));
    }
}
Also used : SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) VariableElement(javax.lang.model.element.VariableElement)

Example 5 with SingleVariableDeclaration

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

the class Functionizer method makeAllocatingConstructor.

/**
   * Create a wrapper for a constructor that does the object allocation.
   */
private FunctionDeclaration makeAllocatingConstructor(MethodDeclaration method, boolean releasing) {
    assert method.isConstructor();
    ExecutableElement element = method.getExecutableElement();
    TypeElement declaringClass = ElementUtil.getDeclaringClass(element);
    String name = releasing ? nameTable.getReleasingConstructorName(element) : nameTable.getAllocatingConstructorName(element);
    FunctionDeclaration function = new FunctionDeclaration(name, declaringClass.asType());
    function.setLineNumber(method.getLineNumber());
    function.setModifiers(ElementUtil.isPrivate(element) ? Modifier.PRIVATE : Modifier.PUBLIC);
    function.setReturnsRetained(!releasing);
    TreeUtil.copyList(method.getParameters(), function.getParameters());
    Block body = new Block();
    function.setBody(body);
    StringBuilder sb = new StringBuilder(releasing ? "J2OBJC_CREATE_IMPL(" : "J2OBJC_NEW_IMPL(");
    sb.append(nameTable.getFullName(declaringClass));
    sb.append(", ").append(nameTable.getFunctionName(element));
    for (SingleVariableDeclaration param : function.getParameters()) {
        sb.append(", ").append(nameTable.getVariableQualifiedName(param.getVariableElement()));
    }
    sb.append(")");
    body.addStatement(new NativeStatement(sb.toString()));
    return function;
}
Also used : FunctionDeclaration(com.google.devtools.j2objc.ast.FunctionDeclaration) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) 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) Block(com.google.devtools.j2objc.ast.Block)

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