Search in sources :

Example 6 with TypeLiteral

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

the class ArrayRewriter method newInitializedArrayInvocation.

private MethodInvocation newInitializedArrayInvocation(ArrayType arrayType, List<Expression> elements, boolean retainedResult) {
    TypeMirror componentType = arrayType.getComponentType();
    TypeElement iosArrayElement = typeUtil.getIosArray(componentType);
    GeneratedExecutableElement methodElement = GeneratedExecutableElement.newMethodWithSelector(getInitializeSelector(componentType, retainedResult), iosArrayElement.asType(), iosArrayElement).addModifiers(Modifier.PUBLIC, Modifier.STATIC);
    methodElement.addParameter(GeneratedVariableElement.newParameter("values", new PointerType(componentType), methodElement));
    methodElement.addParameter(GeneratedVariableElement.newParameter("count", typeUtil.getInt(), methodElement));
    if (!componentType.getKind().isPrimitive()) {
        methodElement.addParameter(GeneratedVariableElement.newParameter("type", TypeUtil.IOS_CLASS.asType(), methodElement));
    }
    MethodInvocation invocation = new MethodInvocation(new ExecutablePair(methodElement), arrayType, new SimpleName(iosArrayElement));
    // Create the array initializer and add it as the first parameter.
    ArrayInitializer arrayInit = new ArrayInitializer(arrayType);
    for (Expression element : elements) {
        arrayInit.addExpression(element.copy());
    }
    invocation.addArgument(arrayInit);
    // Add the array size parameter.
    invocation.addArgument(NumberLiteral.newIntLiteral(arrayInit.getExpressions().size(), typeUtil));
    // Add the type argument for object arrays.
    if (!componentType.getKind().isPrimitive()) {
        invocation.addArgument(new TypeLiteral(componentType, typeUtil));
    }
    return invocation;
}
Also used : GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) TypeLiteral(com.google.devtools.j2objc.ast.TypeLiteral) TypeMirror(javax.lang.model.type.TypeMirror) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) InstanceofExpression(com.google.devtools.j2objc.ast.InstanceofExpression) Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) TypeElement(javax.lang.model.element.TypeElement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) PointerType(com.google.devtools.j2objc.types.PointerType) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation) ArrayInitializer(com.google.devtools.j2objc.ast.ArrayInitializer)

Example 7 with TypeLiteral

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

the class TreeConverter method convertTypeLiteral.

private static TreeNode convertTypeLiteral(org.eclipse.jdt.core.dom.TypeLiteral node) {
    TypeLiteral newNode = new TypeLiteral(BindingConverter.getType(node.resolveTypeBinding()));
    convertExpression(node, newNode);
    return newNode.setType((Type) TreeConverter.convert(node.getType()));
}
Also used : TypeLiteral(com.google.devtools.j2objc.ast.TypeLiteral)

Example 8 with TypeLiteral

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

the class ArrayRewriter method newMultiDimensionArrayInvocation.

private MethodInvocation newMultiDimensionArrayInvocation(ArrayType arrayType, List<Expression> dimensions, boolean retainedResult) {
    assert dimensions.size() > 1;
    TypeMirror componentType = arrayType;
    for (int i = 0; i < dimensions.size(); i++) {
        assert TypeUtil.isArray(componentType);
        componentType = ((ArrayType) componentType).getComponentType();
    }
    TypeElement iosArrayElement = typeUtil.getIosArray(componentType);
    ExecutableElement methodElement = getMultiDimensionMethod(componentType, iosArrayElement, retainedResult);
    MethodInvocation invocation = new MethodInvocation(new ExecutablePair(methodElement), arrayType, new SimpleName(iosArrayElement));
    // Add the dimension count argument.
    invocation.addArgument(NumberLiteral.newIntLiteral(dimensions.size(), typeUtil));
    // Create the dimensions array.
    ArrayInitializer dimensionsArg = new ArrayInitializer(typeUtil.getArrayType(typeUtil.getInt()));
    for (Expression e : dimensions) {
        dimensionsArg.addExpression(e.copy());
    }
    invocation.addArgument(dimensionsArg);
    if (!componentType.getKind().isPrimitive()) {
        invocation.addArgument(new TypeLiteral(componentType, typeUtil));
    }
    return invocation;
}
Also used : TypeLiteral(com.google.devtools.j2objc.ast.TypeLiteral) TypeMirror(javax.lang.model.type.TypeMirror) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) InstanceofExpression(com.google.devtools.j2objc.ast.InstanceofExpression) Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) 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) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation) ArrayInitializer(com.google.devtools.j2objc.ast.ArrayInitializer)

Example 9 with TypeLiteral

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

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

the class TreeConverter method convertFieldAccess.

private TreeNode convertFieldAccess(JCTree.JCFieldAccess node) {
    String fieldName = node.name.toString();
    SourcePosition pos = getPosition(node);
    JCTree.JCExpression selected = node.getExpression();
    if (fieldName.equals("this")) {
        return new ThisExpression().setQualifier((Name) convert(selected)).setTypeMirror(node.sym.asType());
    }
    if ("super".equals(getMemberName(selected))) {
        SuperFieldAccess newNode = new SuperFieldAccess().setVariableElement((VariableElement) node.sym).setName(convertSimpleName(node.sym, node.type, pos));
        if (selected.getKind() == Kind.MEMBER_SELECT) {
            newNode.setQualifier((Name) convert(((JCTree.JCFieldAccess) selected).getExpression()));
        }
        return newNode;
    }
    if (node.getIdentifier().toString().equals("class")) {
        return new TypeLiteral(node.type).setType((Type) convertType(selected.type, pos, false).setPosition(getPosition(node)));
    }
    if (selected.getKind() == Kind.IDENTIFIER && (!node.sym.getKind().isField() || ElementUtil.isConstant((VariableElement) node.sym))) {
        if (selected.toString().equals("this")) {
            // Just return the constant.
            return new SimpleName(node.sym);
        }
        JCIdent ident = (JCTree.JCIdent) selected;
        return new QualifiedName().setName(convertSimpleName(node.sym, node.type, pos)).setQualifier(convertSimpleName(ident.sym, ident.type, pos)).setElement(node.sym);
    }
    if (selected.getKind() == Kind.MEMBER_SELECT) {
        TreeNode newSelected = convertFieldAccess((JCTree.JCFieldAccess) selected).setPosition(pos);
        if (newSelected.getKind() == TreeNode.Kind.QUALIFIED_NAME) {
            return new QualifiedName().setName(convertSimpleName(node.sym, node.type, pos)).setQualifier((QualifiedName) newSelected).setElement(node.sym);
        }
    }
    if (ElementUtil.isConstant((VariableElement) node.sym) && ElementUtil.isStatic(node.sym) && !(selected.getKind() == Kind.METHOD_INVOCATION)) {
        return new QualifiedName().setName(convertSimpleName(node.sym, node.type, pos)).setQualifier((Name) convert(selected)).setElement(node.sym);
    }
    return new FieldAccess().setVariableElement((VariableElement) node.sym).setExpression((Expression) convert(selected)).setName(convertSimpleName(node.sym, node.type, pos).setTypeMirror(node.type));
}
Also used : JCIdent(com.sun.tools.javac.tree.JCTree.JCIdent) SimpleName(com.google.devtools.j2objc.ast.SimpleName) QualifiedName(com.google.devtools.j2objc.ast.QualifiedName) JCTree(com.sun.tools.javac.tree.JCTree) SuperFieldAccess(com.google.devtools.j2objc.ast.SuperFieldAccess) VariableElement(javax.lang.model.element.VariableElement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) Name(com.google.devtools.j2objc.ast.Name) QualifiedName(com.google.devtools.j2objc.ast.QualifiedName) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) TypeLiteral(com.google.devtools.j2objc.ast.TypeLiteral) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) InstanceofExpression(com.google.devtools.j2objc.ast.InstanceofExpression) Expression(com.google.devtools.j2objc.ast.Expression) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) LambdaExpression(com.google.devtools.j2objc.ast.LambdaExpression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) FunctionalExpression(com.google.devtools.j2objc.ast.FunctionalExpression) CastExpression(com.google.devtools.j2objc.ast.CastExpression) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) TreeNode(com.google.devtools.j2objc.ast.TreeNode) SourcePosition(com.google.devtools.j2objc.ast.SourcePosition) FieldAccess(com.google.devtools.j2objc.ast.FieldAccess) SuperFieldAccess(com.google.devtools.j2objc.ast.SuperFieldAccess)

Aggregations

TypeLiteral (com.google.devtools.j2objc.ast.TypeLiteral)10 SimpleName (com.google.devtools.j2objc.ast.SimpleName)6 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)6 TypeMirror (javax.lang.model.type.TypeMirror)6 MethodInvocation (com.google.devtools.j2objc.ast.MethodInvocation)5 ExecutablePair (com.google.devtools.j2objc.types.ExecutablePair)5 TypeElement (javax.lang.model.element.TypeElement)5 Expression (com.google.devtools.j2objc.ast.Expression)4 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)4 InstanceofExpression (com.google.devtools.j2objc.ast.InstanceofExpression)3 ExecutableElement (javax.lang.model.element.ExecutableElement)3 ArrayInitializer (com.google.devtools.j2objc.ast.ArrayInitializer)2 Block (com.google.devtools.j2objc.ast.Block)2 CastExpression (com.google.devtools.j2objc.ast.CastExpression)2 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)2 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)2 ParenthesizedExpression (com.google.devtools.j2objc.ast.ParenthesizedExpression)2 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)2 ThisExpression (com.google.devtools.j2objc.ast.ThisExpression)2 VariableElement (javax.lang.model.element.VariableElement)2