Search in sources :

Example 36 with SimpleName

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

the class LabelRewriter method visit.

@Override
public boolean visit(MethodDeclaration node) {
    // Rename any labels that have the same names; legal in Java but not C.
    final Map<String, Integer> labelCounts = new HashMap<>();
    node.accept(new TreeVisitor() {

        @Override
        public void endVisit(LabeledStatement labeledStatement) {
            final String name = labeledStatement.getLabel().getIdentifier();
            int value = labelCounts.containsKey(name) ? labelCounts.get(name) + 1 : 1;
            labelCounts.put(name, value);
            if (value > 1) {
                final String newName = name + '_' + value;
                labeledStatement.setLabel(new SimpleName(newName));
                // Update references to this label.
                labeledStatement.accept(new TreeVisitor() {

                    @Override
                    public void endVisit(ContinueStatement node) {
                        if (node.getLabel() != null && node.getLabel().getIdentifier().equals(name)) {
                            node.setLabel(new SimpleName(newName));
                        }
                    }

                    @Override
                    public void endVisit(BreakStatement node) {
                        if (node.getLabel() != null && node.getLabel().getIdentifier().equals(name)) {
                            node.setLabel(new SimpleName(newName));
                        }
                    }
                });
            }
        }
    });
    return true;
}
Also used : TreeVisitor(com.google.devtools.j2objc.ast.TreeVisitor) BreakStatement(com.google.devtools.j2objc.ast.BreakStatement) LabeledStatement(com.google.devtools.j2objc.ast.LabeledStatement) HashMap(java.util.HashMap) SimpleName(com.google.devtools.j2objc.ast.SimpleName) ContinueStatement(com.google.devtools.j2objc.ast.ContinueStatement)

Example 37 with SimpleName

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

the class OuterReferenceResolver method addCaptureArgs.

private void addCaptureArgs(TypeElement type, List<Expression> args) {
    for (VariableElement var : captureInfo.getCapturedVars(type)) {
        Expression path = getPathForLocalVar(var);
        if (path == null) {
            path = new SimpleName(var);
        }
        args.add(path);
    }
}
Also used : LambdaExpression(com.google.devtools.j2objc.ast.LambdaExpression) Expression(com.google.devtools.j2objc.ast.Expression) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) FunctionalExpression(com.google.devtools.j2objc.ast.FunctionalExpression) SimpleName(com.google.devtools.j2objc.ast.SimpleName) VariableElement(javax.lang.model.element.VariableElement)

Example 38 with SimpleName

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

the class CastResolver method createCastCheck.

private FunctionInvocation createCastCheck(TypeMirror type, Expression expr) {
    type = typeUtil.erasure(type);
    TypeMirror idType = TypeUtil.ID_TYPE;
    if (TypeUtil.isInterface(type) || isObjectArray(type)) {
        // Interfaces and object arrays requre a isInstance call.
        FunctionElement element = new FunctionElement("cast_check", idType, null).addParameters(idType, TypeUtil.IOS_CLASS.asType());
        FunctionInvocation invocation = new FunctionInvocation(element, idType);
        invocation.addArgument(TreeUtil.remove(expr));
        invocation.addArgument(new TypeLiteral(type, typeUtil));
        return invocation;
    } else if (TypeUtil.isArray(type) || TypeUtil.isDeclaredType(type)) {
        // Primitive array and non-interface type casts are checked using Objective-C's
        // isKindOfClass:.
        TypeElement objcClass = typeUtil.getObjcClass(type);
        FunctionElement checkFunction = new FunctionElement("cast_chk", idType, null).addParameters(idType, idType);
        FunctionInvocation invocation = new FunctionInvocation(checkFunction, idType);
        invocation.addArgument(TreeUtil.remove(expr));
        ExecutableElement classElement = GeneratedExecutableElement.newMethodWithSelector("class", idType, objcClass).addModifiers(Modifier.STATIC);
        MethodInvocation classInvocation = new MethodInvocation(new ExecutablePair(classElement), new SimpleName(objcClass));
        invocation.addArgument(classInvocation);
        return invocation;
    }
    return null;
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) TypeLiteral(com.google.devtools.j2objc.ast.TypeLiteral) TypeMirror(javax.lang.model.type.TypeMirror) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) TypeElement(javax.lang.model.element.TypeElement) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) SuperMethodInvocation(com.google.devtools.j2objc.ast.SuperMethodInvocation) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation)

Example 39 with SimpleName

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

the class EnumRewriter method addArcInitialization.

// ARC does not allow using "objc_constructInstance" so ARC code doesn't get
// the shared allocation optimization.
private void addArcInitialization(EnumDeclaration node) {
    List<Statement> stmts = node.getClassInitStatements().subList(0, 0);
    int i = 0;
    for (EnumConstantDeclaration constant : node.getEnumConstants()) {
        VariableElement varElement = constant.getVariableElement();
        ClassInstanceCreation creation = new ClassInstanceCreation(constant.getExecutablePair());
        TreeUtil.copyList(constant.getArguments(), creation.getArguments());
        creation.addArgument(new StringLiteral(ElementUtil.getName(varElement), typeUtil));
        creation.addArgument(new NumberLiteral(i++, typeUtil));
        creation.setHasRetainedResult(true);
        stmts.add(new ExpressionStatement(new Assignment(new SimpleName(varElement), creation)));
    }
}
Also used : ClassInstanceCreation(com.google.devtools.j2objc.ast.ClassInstanceCreation) Assignment(com.google.devtools.j2objc.ast.Assignment) EnumConstantDeclaration(com.google.devtools.j2objc.ast.EnumConstantDeclaration) StringLiteral(com.google.devtools.j2objc.ast.StringLiteral) 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) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) NumberLiteral(com.google.devtools.j2objc.ast.NumberLiteral)

Example 40 with SimpleName

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

Aggregations

SimpleName (com.google.devtools.j2objc.ast.SimpleName)48 VariableElement (javax.lang.model.element.VariableElement)26 Expression (com.google.devtools.j2objc.ast.Expression)25 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)16 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)16 TypeMirror (javax.lang.model.type.TypeMirror)15 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)14 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)14 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)13 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)13 ExecutablePair (com.google.devtools.j2objc.types.ExecutablePair)12 Block (com.google.devtools.j2objc.ast.Block)11 Statement (com.google.devtools.j2objc.ast.Statement)11 ExecutableElement (javax.lang.model.element.ExecutableElement)11 TypeElement (javax.lang.model.element.TypeElement)10 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)9 MethodInvocation (com.google.devtools.j2objc.ast.MethodInvocation)9 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)9 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)8 ThisExpression (com.google.devtools.j2objc.ast.ThisExpression)8