Search in sources :

Example 16 with VariableDeclarationFragment

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

the class Rewriter method endVisit.

@Override
public void endVisit(VariableDeclarationStatement node) {
    if (options.isJDT()) {
        ListMultimap<Integer, VariableDeclarationFragment> newDeclarations = rewriteExtraDimensions(node.getFragments());
        if (newDeclarations != null) {
            List<Statement> statements = ((Block) node.getParent()).getStatements();
            int location = 0;
            while (location < statements.size() && !node.equals(statements.get(location))) {
                location++;
            }
            for (Integer dimensions : newDeclarations.keySet()) {
                List<VariableDeclarationFragment> fragments = newDeclarations.get(dimensions);
                VariableDeclarationStatement newDecl = new VariableDeclarationStatement(fragments.get(0));
                newDecl.getFragments().addAll(fragments.subList(1, fragments.size()));
                statements.add(++location, newDecl);
            }
        }
    }
}
Also used : VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) ForStatement(com.google.devtools.j2objc.ast.ForStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Statement(com.google.devtools.j2objc.ast.Statement) Block(com.google.devtools.j2objc.ast.Block) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement)

Example 17 with VariableDeclarationFragment

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

the class Rewriter method rewriteExtraDimensions.

private ListMultimap<Integer, VariableDeclarationFragment> rewriteExtraDimensions(List<VariableDeclarationFragment> fragments) {
    // Removes extra dimensions on variable declaration fragments and creates extra field
    // declaration nodes if necessary.
    // eg. "int i1, i2[], i3[][];" becomes "int i1; int[] i2; int[][] i3".
    ListMultimap<Integer, VariableDeclarationFragment> newDeclarations = null;
    int masterDimensions = -1;
    Iterator<VariableDeclarationFragment> iter = fragments.iterator();
    while (iter.hasNext()) {
        VariableDeclarationFragment frag = iter.next();
        int dimensions = frag.getExtraDimensions();
        if (masterDimensions == -1) {
            masterDimensions = dimensions;
        } else if (dimensions != masterDimensions) {
            if (newDeclarations == null) {
                newDeclarations = LinkedListMultimap.create();
            }
            VariableDeclarationFragment newFrag = new VariableDeclarationFragment(frag.getVariableElement(), TreeUtil.remove(frag.getInitializer()));
            newDeclarations.put(dimensions, newFrag);
            iter.remove();
        } else {
            frag.setExtraDimensions(0);
        }
    }
    return newDeclarations;
}
Also used : VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment)

Example 18 with VariableDeclarationFragment

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

the class Rewriter method endVisit.

@Override
public void endVisit(FieldDeclaration node) {
    if (options.isJDT()) {
        ListMultimap<Integer, VariableDeclarationFragment> newDeclarations = rewriteExtraDimensions(node.getFragments());
        if (newDeclarations != null) {
            List<BodyDeclaration> bodyDecls = TreeUtil.asDeclarationSublist(node);
            for (Integer dimensions : newDeclarations.keySet()) {
                List<VariableDeclarationFragment> fragments = newDeclarations.get(dimensions);
                FieldDeclaration newDecl = new FieldDeclaration(fragments.get(0));
                newDecl.getFragments().addAll(fragments.subList(1, fragments.size()));
                bodyDecls.add(newDecl);
            }
        }
    }
}
Also used : VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) BodyDeclaration(com.google.devtools.j2objc.ast.BodyDeclaration) FieldDeclaration(com.google.devtools.j2objc.ast.FieldDeclaration)

Example 19 with VariableDeclarationFragment

use of com.google.devtools.j2objc.ast.VariableDeclarationFragment 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));
    StringBuffer sb = new StringBuffer("id names[] = {\n  ");
    for (EnumConstantDeclaration constant : node.getEnumConstants()) {
        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("(" + enumClassName + "_values_[i] = e = objc_constructInstance(self, (void *)ptr), ptr += objSize);"));
    loopBody.addStatement(new NativeStatement(enumClassName + "_initWithNSString_withInt_(e, names[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) 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) 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 20 with VariableDeclarationFragment

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

the class StatementGenerator method visit.

@Override
public boolean visit(VariableDeclarationStatement node) {
    List<VariableDeclarationFragment> vars = node.getFragments();
    assert !vars.isEmpty();
    VariableElement element = vars.get(0).getVariableElement();
    if (ElementUtil.suppressesWarning("unused", element)) {
        buffer.append("__unused ");
    }
    String objcType = nameTable.getObjCType(element);
    String objcTypePointers = " ";
    int idx = objcType.indexOf(" *");
    if (idx != -1) {
        // Split the type at the first pointer. The second part of the type is
        // applied to each fragment. (eg. Foo *one, *two)
        objcTypePointers = objcType.substring(idx);
        objcType = objcType.substring(0, idx);
    }
    buffer.append(objcType);
    for (Iterator<VariableDeclarationFragment> it = vars.iterator(); it.hasNext(); ) {
        VariableDeclarationFragment f = it.next();
        buffer.append(objcTypePointers);
        f.accept(this);
        if (it.hasNext()) {
            buffer.append(",");
        }
    }
    buffer.append(";\n");
    return false;
}
Also used : VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) VariableElement(javax.lang.model.element.VariableElement)

Aggregations

VariableDeclarationFragment (com.google.devtools.j2objc.ast.VariableDeclarationFragment)24 VariableElement (javax.lang.model.element.VariableElement)11 FieldDeclaration (com.google.devtools.j2objc.ast.FieldDeclaration)6 Statement (com.google.devtools.j2objc.ast.Statement)5 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)5 TypeMirror (javax.lang.model.type.TypeMirror)5 Block (com.google.devtools.j2objc.ast.Block)4 Expression (com.google.devtools.j2objc.ast.Expression)4 VariableDeclarationExpression (com.google.devtools.j2objc.ast.VariableDeclarationExpression)4 AbstractTypeDeclaration (com.google.devtools.j2objc.ast.AbstractTypeDeclaration)3 CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)3 EnumConstantDeclaration (com.google.devtools.j2objc.ast.EnumConstantDeclaration)3 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)3 ForStatement (com.google.devtools.j2objc.ast.ForStatement)3 NameTable (com.google.devtools.j2objc.util.NameTable)3 BodyDeclaration (com.google.devtools.j2objc.ast.BodyDeclaration)2 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)2 EnumDeclaration (com.google.devtools.j2objc.ast.EnumDeclaration)2 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)2 NativeExpression (com.google.devtools.j2objc.ast.NativeExpression)2