Search in sources :

Example 1 with Block

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

the class EnhancedForRewriter method convertToJavaIteratorLoop.

private void convertToJavaIteratorLoop(EnhancedForStatement node) {
    Expression expression = node.getExpression();
    TypeMirror expressionType = expression.getTypeMirror();
    VariableElement loopVariable = node.getParameter().getVariableElement();
    DeclaredType iterableType = typeUtil.findSupertype(expressionType, "java.lang.Iterable");
    ExecutablePair iteratorMethod = typeUtil.findMethod(iterableType, "iterator");
    DeclaredType iteratorType = (DeclaredType) iteratorMethod.type().getReturnType();
    ExecutablePair hasNextMethod = typeUtil.findMethod(iteratorType, "hasNext");
    ExecutablePair nextMethod = typeUtil.findMethod(iteratorType, "next");
    assert hasNextMethod != null && nextMethod != null;
    VariableElement iteratorVariable = GeneratedVariableElement.newLocalVar("iter__", iteratorType, null);
    MethodInvocation iteratorInvocation = new MethodInvocation(iteratorMethod, TreeUtil.remove(expression));
    VariableDeclarationStatement iteratorDecl = new VariableDeclarationStatement(iteratorVariable, iteratorInvocation);
    MethodInvocation hasNextInvocation = new MethodInvocation(hasNextMethod, new SimpleName(iteratorVariable));
    MethodInvocation nextInvocation = new MethodInvocation(nextMethod, new SimpleName(iteratorVariable));
    Block newLoopBody = makeBlock(TreeUtil.remove(node.getBody()));
    newLoopBody.addStatement(0, new VariableDeclarationStatement(loopVariable, nextInvocation));
    WhileStatement whileLoop = new WhileStatement();
    whileLoop.setExpression(hasNextInvocation);
    whileLoop.setBody(newLoopBody);
    Block block = new Block();
    List<Statement> stmts = block.getStatements();
    stmts.add(iteratorDecl);
    stmts.add(whileLoop);
    replaceLoop(node, block, whileLoop);
}
Also used : PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) TypeMirror(javax.lang.model.type.TypeMirror) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) EnhancedForStatement(com.google.devtools.j2objc.ast.EnhancedForStatement) LabeledStatement(com.google.devtools.j2objc.ast.LabeledStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) WhileStatement(com.google.devtools.j2objc.ast.WhileStatement) Statement(com.google.devtools.j2objc.ast.Statement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Block(com.google.devtools.j2objc.ast.Block) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) WhileStatement(com.google.devtools.j2objc.ast.WhileStatement) DeclaredType(javax.lang.model.type.DeclaredType)

Example 2 with Block

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

the class EnhancedForRewriter method handleArrayIteration.

private void handleArrayIteration(EnhancedForStatement node) {
    Expression expression = node.getExpression();
    ArrayType expressionType = (ArrayType) expression.getTypeMirror();
    VariableElement loopVariable = node.getParameter().getVariableElement();
    TypeMirror componentType = expressionType.getComponentType();
    TypeElement iosArrayType = typeUtil.getIosArray(componentType);
    TypeMirror bufferType = new PointerType(componentType);
    VariableElement arrayVariable = GeneratedVariableElement.newLocalVar("a__", expressionType, null);
    VariableElement bufferVariable = GeneratedVariableElement.newLocalVar("b__", bufferType, null).setTypeQualifiers("const*");
    VariableElement endVariable = GeneratedVariableElement.newLocalVar("e__", bufferType, null).setTypeQualifiers("const*");
    VariableElement bufferField = GeneratedVariableElement.newField("buffer", bufferType, iosArrayType).addModifiers(Modifier.PUBLIC);
    VariableElement sizeField = GeneratedVariableElement.newField("size", typeUtil.getInt(), iosArrayType).addModifiers(Modifier.PUBLIC);
    VariableDeclarationStatement arrayDecl = new VariableDeclarationStatement(arrayVariable, TreeUtil.remove(expression));
    FieldAccess bufferAccess = new FieldAccess(bufferField, new SimpleName(arrayVariable));
    VariableDeclarationStatement bufferDecl = new VariableDeclarationStatement(bufferVariable, bufferAccess);
    InfixExpression endInit = new InfixExpression(bufferType, InfixExpression.Operator.PLUS, new SimpleName(bufferVariable), new FieldAccess(sizeField, new SimpleName(arrayVariable)));
    VariableDeclarationStatement endDecl = new VariableDeclarationStatement(endVariable, endInit);
    WhileStatement loop = new WhileStatement();
    loop.setExpression(new InfixExpression(typeUtil.getBoolean(), InfixExpression.Operator.LESS, new SimpleName(bufferVariable), new SimpleName(endVariable)));
    Block newLoopBody = makeBlock(TreeUtil.remove(node.getBody()));
    loop.setBody(newLoopBody);
    newLoopBody.addStatement(0, new VariableDeclarationStatement(loopVariable, new PrefixExpression(componentType, PrefixExpression.Operator.DEREFERENCE, new PostfixExpression(bufferVariable, PostfixExpression.Operator.INCREMENT))));
    Block block = new Block();
    List<Statement> stmts = block.getStatements();
    stmts.add(arrayDecl);
    stmts.add(bufferDecl);
    stmts.add(endDecl);
    stmts.add(loop);
    replaceLoop(node, block, loop);
}
Also used : TypeElement(javax.lang.model.element.TypeElement) EnhancedForStatement(com.google.devtools.j2objc.ast.EnhancedForStatement) LabeledStatement(com.google.devtools.j2objc.ast.LabeledStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) WhileStatement(com.google.devtools.j2objc.ast.WhileStatement) Statement(com.google.devtools.j2objc.ast.Statement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) PointerType(com.google.devtools.j2objc.types.PointerType) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) WhileStatement(com.google.devtools.j2objc.ast.WhileStatement) ArrayType(javax.lang.model.type.ArrayType) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) TypeMirror(javax.lang.model.type.TypeMirror) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) 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) FieldAccess(com.google.devtools.j2objc.ast.FieldAccess)

Example 3 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 4 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 5 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)

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