Search in sources :

Example 6 with Block

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

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

the class EnumRewriter method addValuesMethod.

private void addValuesMethod(EnumDeclaration node) {
    TypeElement type = node.getTypeElement();
    ExecutableElement method = ElementUtil.findMethod(type, "values");
    assert method != null : "Can't find values method on enum type.";
    String typeName = nameTable.getFullName(type);
    MethodDeclaration methodDecl = new MethodDeclaration(method);
    Block body = new Block();
    methodDecl.setBody(body);
    body.addStatement(new NativeStatement(UnicodeUtils.format("  return [IOSObjectArray arrayWithObjects:%s_values_ count:%s type:%s_class_()];", typeName, node.getEnumConstants().size(), typeName)));
    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) ExecutableElement(javax.lang.model.element.ExecutableElement) Block(com.google.devtools.j2objc.ast.Block)

Example 8 with Block

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

Example 9 with Block

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

the class DestructorGenerator method addDeallocMethod.

private void addDeallocMethod(AbstractTypeDeclaration node) {
    TypeElement type = node.getTypeElement();
    boolean hasFinalize = hasFinalizeMethod(type);
    List<Statement> releaseStatements = createReleaseStatements(node);
    if (releaseStatements.isEmpty() && !hasFinalize) {
        return;
    }
    ExecutableElement deallocElement = GeneratedExecutableElement.newMethodWithSelector(NameTable.DEALLOC_METHOD, typeUtil.getVoid(), type).addModifiers(Modifier.PUBLIC);
    MethodDeclaration deallocDecl = new MethodDeclaration(deallocElement);
    deallocDecl.setHasDeclaration(false);
    Block block = new Block();
    deallocDecl.setBody(block);
    List<Statement> stmts = block.getStatements();
    if (hasFinalize) {
        String clsName = nameTable.getFullName(type);
        stmts.add(new NativeStatement("JreCheckFinalize(self, [" + clsName + " class]);"));
    }
    stmts.addAll(releaseStatements);
    if (options.useReferenceCounting()) {
        stmts.add(new ExpressionStatement(new SuperMethodInvocation(new ExecutablePair(superDeallocElement))));
    }
    node.addBodyDeclaration(deallocDecl);
}
Also used : NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) TypeElement(javax.lang.model.element.TypeElement) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) Statement(com.google.devtools.j2objc.ast.Statement) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) Block(com.google.devtools.j2objc.ast.Block) SuperMethodInvocation(com.google.devtools.j2objc.ast.SuperMethodInvocation)

Example 10 with Block

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

the class SwitchRewriter method fixVariableDeclarations.

/**
   * Moves all variable declarations above the first case statement.
   */
private void fixVariableDeclarations(SwitchStatement node) {
    List<Statement> statements = node.getStatements();
    Block block = new Block();
    List<Statement> blockStmts = block.getStatements();
    for (int i = 0; i < statements.size(); i++) {
        Statement stmt = statements.get(i);
        if (stmt instanceof VariableDeclarationStatement) {
            VariableDeclarationStatement declStmt = (VariableDeclarationStatement) stmt;
            statements.remove(i--);
            List<VariableDeclarationFragment> fragments = declStmt.getFragments();
            for (VariableDeclarationFragment decl : fragments) {
                Expression initializer = decl.getInitializer();
                if (initializer != null) {
                    Assignment assignment = new Assignment(new SimpleName(decl.getVariableElement()), TreeUtil.remove(initializer));
                    statements.add(++i, new ExpressionStatement(assignment));
                }
            }
            blockStmts.add(declStmt);
        }
    }
    if (blockStmts.size() > 0) {
        // There is at least one variable declaration, so copy this switch
        // statement into the new block and replace it in the parent list.
        node.replaceWith(block);
        blockStmts.add(node);
    }
}
Also used : Assignment(com.google.devtools.j2objc.ast.Assignment) Expression(com.google.devtools.j2objc.ast.Expression) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Statement(com.google.devtools.j2objc.ast.Statement) EmptyStatement(com.google.devtools.j2objc.ast.EmptyStatement) SwitchStatement(com.google.devtools.j2objc.ast.SwitchStatement) VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) SimpleName(com.google.devtools.j2objc.ast.SimpleName) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) Block(com.google.devtools.j2objc.ast.Block) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement)

Aggregations

Block (com.google.devtools.j2objc.ast.Block)38 Statement (com.google.devtools.j2objc.ast.Statement)18 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)13 ExecutableElement (javax.lang.model.element.ExecutableElement)13 TypeElement (javax.lang.model.element.TypeElement)12 SimpleName (com.google.devtools.j2objc.ast.SimpleName)11 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)11 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)10 VariableElement (javax.lang.model.element.VariableElement)10 NativeStatement (com.google.devtools.j2objc.ast.NativeStatement)9 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)9 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)8 ForStatement (com.google.devtools.j2objc.ast.ForStatement)8 ReturnStatement (com.google.devtools.j2objc.ast.ReturnStatement)8 TypeMirror (javax.lang.model.type.TypeMirror)8 Expression (com.google.devtools.j2objc.ast.Expression)7 SingleVariableDeclaration (com.google.devtools.j2objc.ast.SingleVariableDeclaration)7 EmptyStatement (com.google.devtools.j2objc.ast.EmptyStatement)6 IfStatement (com.google.devtools.j2objc.ast.IfStatement)6 LabeledStatement (com.google.devtools.j2objc.ast.LabeledStatement)6