Search in sources :

Example 86 with Type

use of com.redhat.ceylon.model.typechecker.model.Type in project ceylon-compiler by ceylon.

the class ClassTransformer method getFirstSatisfiedType.

private Type getFirstSatisfiedType(Type currentType, Interface iface) {
    Type found = null;
    TypeDeclaration currentDecl = currentType.getDeclaration();
    if (Decl.equal(currentDecl, iface)) {
        return currentType;
    }
    if (currentType.getExtendedType() != null) {
        Type supertype = currentType.getSupertype(currentType.getExtendedType().getDeclaration());
        found = getFirstSatisfiedType(supertype, iface);
        if (found != null)
            return found;
    }
    for (Type superInterfaceType : currentType.getSatisfiedTypes()) {
        found = getFirstSatisfiedType(superInterfaceType, iface);
        if (found != null)
            return found;
    }
    return null;
}
Also used : Type(com.redhat.ceylon.model.typechecker.model.Type) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration)

Example 87 with Type

use of com.redhat.ceylon.model.typechecker.model.Type in project ceylon-compiler by ceylon.

the class ExpressionTransformer method transform.

public JCTree transform(Tree.TypeLiteral expr) {
    at(expr);
    if (!expr.getWantsDeclaration()) {
        if (expr.getDeclaration() instanceof Constructor) {
            JCExpression classLiteral = makeTypeLiteralCall(expr.getType().getTypeModel().getQualifyingType(), false, expr.getTypeModel());
            TypeDeclaration classModelDeclaration = (TypeDeclaration) typeFact().getLanguageModuleModelDeclaration(expr.getType().getTypeModel().getQualifyingType().getDeclaration().isMember() ? "MemberClass" : "Class");
            JCTypeCast typeCast = make().TypeCast(makeJavaType(classModelDeclaration.appliedType(null, List.of(expr.getType().getTypeModel().getQualifyingType(), typeFact().getNothingType()))), classLiteral);
            Type callableType = expr.getTypeModel().getFullType();
            JCExpression reifiedArgumentsExpr = makeReifiedTypeArgument(typeFact().getCallableTuple(callableType));
            return make().Apply(null, naming.makeQualIdent(typeCast, "getConstructor"), List.<JCExpression>of(reifiedArgumentsExpr, make().Literal(expr.getDeclaration().getName())));
        } else {
            return makeTypeLiteralCall(expr.getType().getTypeModel(), true, expr.getTypeModel());
        }
    } else if (expr.getDeclaration() instanceof TypeParameter) {
        // we must get it from its container
        TypeParameter declaration = (TypeParameter) expr.getDeclaration();
        Node node = expr;
        return makeTypeParameterDeclaration(node, declaration);
    } else if (expr.getDeclaration() instanceof Constructor || expr instanceof Tree.NewLiteral) {
        Constructor ctor;
        if (expr.getDeclaration() instanceof Constructor) {
            ctor = (Constructor) expr.getDeclaration();
        } else {
            ctor = Decl.getDefaultConstructor((Class) expr.getDeclaration());
        }
        JCExpression metamodelCall = makeTypeDeclarationLiteral(Decl.getConstructedClass(ctor));
        metamodelCall = make().TypeCast(makeJavaType(typeFact().getClassDeclarationType(), JT_RAW), metamodelCall);
        metamodelCall = make().Apply(null, naming.makeQualIdent(metamodelCall, "getConstructorDeclaration"), List.<JCExpression>of(make().Literal(ctor.getName() == null ? "" : ctor.getName())));
        if (Decl.isEnumeratedConstructor(ctor)) {
            metamodelCall = make().TypeCast(makeJavaType(typeFact().getValueConstructorDeclarationType(), JT_RAW), metamodelCall);
        } else /*else if (Decl.isDefaultConstructor(ctor)){
                metamodelCall = make().TypeCast(
                        makeJavaType(typeFact().getDefaultConstructorDeclarationType(), JT_RAW), metamodelCall);
            } */
        {
            metamodelCall = make().TypeCast(makeJavaType(typeFact().getCallableConstructorDeclarationType(), JT_RAW), metamodelCall);
        }
        return metamodelCall;
    } else if (expr.getDeclaration() instanceof ClassOrInterface || expr.getDeclaration() instanceof TypeAlias) {
        // use the generated class to get to the declaration literal
        JCExpression metamodelCall = makeTypeDeclarationLiteral((TypeDeclaration) expr.getDeclaration());
        Type exprType = expr.getTypeModel().resolveAliases();
        // now cast if required
        if (!exprType.isExactly(((TypeDeclaration) typeFact().getLanguageModuleDeclarationDeclaration("NestableDeclaration")).getType())) {
            JCExpression type = makeJavaType(exprType, JT_NO_PRIMITIVES);
            return make().TypeCast(type, metamodelCall);
        }
        return metamodelCall;
    } else {
        return makeErroneous(expr, "compiler bug: " + expr.getDeclaration() + " is an unsupported declaration type");
    }
}
Also used : ClassOrInterface(com.redhat.ceylon.model.typechecker.model.ClassOrInterface) Type(com.redhat.ceylon.model.typechecker.model.Type) TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCTypeCast(com.sun.tools.javac.tree.JCTree.JCTypeCast) Constructor(com.redhat.ceylon.model.typechecker.model.Constructor) Node(com.redhat.ceylon.compiler.typechecker.tree.Node) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) Class(com.redhat.ceylon.model.typechecker.model.Class) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) TypeAlias(com.redhat.ceylon.model.typechecker.model.TypeAlias) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration)

Example 88 with Type

use of com.redhat.ceylon.model.typechecker.model.Type in project ceylon-compiler by ceylon.

the class ExpressionTransformer method transformArgumentsForSimpleInvocation.

private List<ExpressionAndType> transformArgumentsForSimpleInvocation(SimpleInvocation invocation, CallBuilder callBuilder) {
    final Constructor superConstructor = invocation.getConstructor();
    CtorDelegation constructorDelegation;
    if (invocation instanceof SuperInvocation) {
        constructorDelegation = ((SuperInvocation) invocation).getDelegation();
    } else {
        constructorDelegation = null;
    }
    List<ExpressionAndType> result = List.<ExpressionAndType>nil();
    if (!(invocation instanceof SuperInvocation) || !((SuperInvocation) invocation).isDelegationDelegation()) {
        int numArguments = invocation.getNumArguments();
        if (invocation.getNumParameters() == 0) {
            // skip transforming arguments
            // (Usually, numArguments would already be null, but it's possible to call a
            //  parameterless function with a *[] argument - see #1593.)
            numArguments = 0;
        }
        boolean wrapIntoArray = false;
        ListBuffer<JCExpression> arrayWrap = new ListBuffer<JCExpression>();
        for (int argIndex = 0; argIndex < numArguments; argIndex++) {
            BoxingStrategy boxingStrategy = invocation.getParameterBoxingStrategy(argIndex);
            Type parameterType = invocation.getParameterType(argIndex);
            // to avoid ambiguity of foo(1,2) for foo(int...) and foo(Object...) methods
            if (!wrapIntoArray && invocation.isParameterSequenced(argIndex) && invocation.isJavaMethod() && boxingStrategy == BoxingStrategy.UNBOXED && willEraseToPrimitive(typeFact().getDefiniteType(parameterType)) && !invocation.isSpread())
                wrapIntoArray = true;
            ExpressionAndType exprAndType;
            if (invocation.isArgumentSpread(argIndex)) {
                if (!invocation.isParameterSequenced(argIndex)) {
                    result = transformSpreadTupleArgument(invocation, callBuilder, result, argIndex);
                    break;
                }
                if (invocation.isJavaMethod()) {
                    // if it's a java method we need a special wrapping
                    exprAndType = transformSpreadArgument(invocation, numArguments, argIndex, boxingStrategy, parameterType);
                    argIndex = numArguments;
                } else {
                    Type argType = invocation.getArgumentType(argIndex);
                    if (argType.getSupertype(typeFact().getSequentialDeclaration()) != null) {
                        exprAndType = transformArgument(invocation, argIndex, boxingStrategy);
                    } else if (argType.getSupertype(typeFact().getIterableDeclaration()) != null) {
                        exprAndType = transformArgument(invocation, argIndex, boxingStrategy);
                        JCExpression sequential = iterableToSequential(exprAndType.expression);
                        if (invocation.isParameterVariadicPlus(argIndex)) {
                            Type iteratedType = typeFact().getIteratedType(argType);
                            sequential = utilInvocation().castSequentialToSequence(sequential, iteratedType);
                        }
                        exprAndType = new ExpressionAndType(sequential, exprAndType.type);
                    } else {
                        exprAndType = new ExpressionAndType(makeErroneous(invocation.getNode(), "compiler bug: unexpected spread argument"), makeErroneous(invocation.getNode(), "compiler bug: unexpected spread argument"));
                    }
                }
            } else if (!invocation.isParameterSequenced(argIndex) || // if it's sequenced, Java and there's no spread at all, pass it along
            (invocation.isParameterSequenced(argIndex) && invocation.isJavaMethod() && !invocation.isSpread())) {
                exprAndType = transformArgument(invocation, argIndex, boxingStrategy);
                // This is not required for primitive arrays since they are not Object[]
                if (numArguments == 1 && invocation.isIndirect()) {
                    Type argumentType = invocation.getArgumentType(0);
                    if (isJavaObjectArray(argumentType) || isNull(argumentType)) {
                        exprAndType = new ExpressionAndType(make().TypeCast(makeJavaType(typeFact().getObjectType()), exprAndType.expression), exprAndType.type);
                    }
                } else if (invocation.isParameterSequenced(argIndex) && invocation.isJavaMethod() && !invocation.isSpread()) {
                    // in fact, the very same problem happens when passing null or object arrays to a java variadic method
                    Type argumentType = invocation.getArgumentType(argIndex);
                    if (isJavaObjectArray(argumentType) || isNull(argumentType)) {
                        // remove any ambiguity
                        exprAndType = new ExpressionAndType(make().TypeCast(makeJavaType(parameterType), exprAndType.expression), exprAndType.type);
                    }
                }
            } else {
                // we must have a sequenced param
                if (invocation.isSpread()) {
                    exprAndType = transformSpreadArgument(invocation, numArguments, argIndex, boxingStrategy, parameterType);
                    argIndex = numArguments;
                } else {
                    exprAndType = transformVariadicArgument(invocation, numArguments, argIndex, parameterType);
                    argIndex = numArguments;
                }
            }
            if (!wrapIntoArray) {
                if (argIndex == 0 && invocation.isCallable() && !invocation.isArgumentSpread(numArguments - 1)) {
                    exprAndType = new ExpressionAndType(make().TypeCast(make().Type(syms().objectType), exprAndType.expression), make().Type(syms().objectType));
                }
                result = result.append(exprAndType);
            } else {
                arrayWrap.append(exprAndType.expression);
            }
        }
        if (invocation.isIndirect() && invocation.isParameterSequenced(numArguments) && !invocation.isArgumentSpread(numArguments - 1) && ((IndirectInvocation) invocation).getNumParameters() > numArguments) {
            // Calling convention for indirect variadic invocation's requires
            // explicit variadic argument (can't use the overloading trick)
            result = result.append(new ExpressionAndType(makeEmptyAsSequential(true), make().Erroneous()));
        }
        if (wrapIntoArray) {
            // must have at least one arg, so take the last one
            Type parameterType = invocation.getParameterType(numArguments - 1);
            JCExpression arrayType = makeJavaType(parameterType, JT_RAW);
            JCNewArray arrayExpr = make().NewArray(arrayType, List.<JCExpression>nil(), arrayWrap.toList());
            JCExpression arrayTypeExpr = make().TypeArray(makeJavaType(parameterType, JT_RAW));
            result = result.append(new ExpressionAndType(arrayExpr, arrayTypeExpr));
        }
    } else {
        for (Parameter p : constructorDelegation.getConstructor().getParameterList().getParameters()) {
            result = result.append(new ExpressionAndType(naming.makeName(p.getModel(), Naming.NA_IDENT | Naming.NA_ALIASED), null));
        }
    }
    boolean concreteDelegation = invocation instanceof SuperInvocation && ((SuperInvocation) invocation).getDelegation().isConcreteSelfDelegation();
    if (superConstructor == null && concreteDelegation) {
        Constructor delegateTo = ((SuperInvocation) invocation).getDelegation().getConstructor();
        result = result.prepend(new ExpressionAndType(naming.makeNamedConstructorName(delegateTo, true), naming.makeNamedConstructorType(delegateTo, true)));
    } else if (superConstructor != null && constructorDelegation != null && constructorDelegation.isSelfDelegation()) {
        result = result.prepend(new ExpressionAndType(naming.makeNamedConstructorName(superConstructor, concreteDelegation), naming.makeNamedConstructorType(superConstructor, concreteDelegation)));
    } else if (superConstructor != null && !Decl.isDefaultConstructor(superConstructor)) {
        result = result.prepend(new ExpressionAndType(naming.makeNamedConstructorName(superConstructor, concreteDelegation), naming.makeNamedConstructorType(superConstructor, concreteDelegation)));
    }
    return result;
}
Also used : Constructor(com.redhat.ceylon.model.typechecker.model.Constructor) ListBuffer(com.sun.tools.javac.util.ListBuffer) Type(com.redhat.ceylon.model.typechecker.model.Type) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) AnalyzerUtil.isIndirectInvocation(com.redhat.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.isIndirectInvocation) TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) Parameter(com.redhat.ceylon.model.typechecker.model.Parameter) JCNewArray(com.sun.tools.javac.tree.JCTree.JCNewArray)

Example 89 with Type

use of com.redhat.ceylon.model.typechecker.model.Type in project ceylon-compiler by ceylon.

the class ExpressionTransformer method transform.

public JCExpression transform(final Tree.BitwiseAssignmentOp op) {
    final AssignmentOperatorTranslation operator = Operators.getAssignmentOperator(op.getClass());
    if (operator == null) {
        return makeErroneous(op, "compiler bug: " + op.getNodeType() + " is not a supported bitwise assignment operator");
    }
    Type valueType = op.getLeftTerm().getTypeModel();
    final Type rightType = getSupertype(op.getRightTerm(), typeFact().getSetDeclaration());
    return transformAssignAndReturnOperation(op, op.getLeftTerm(), false, valueType, valueType, new AssignAndReturnOperationFactory() {

        @Override
        public JCExpression getNewValue(JCExpression previousValue) {
            JCExpression result = transformOverridableBinaryOperator(op.getLeftTerm(), op.getRightTerm(), rightType, operator.binaryOperator, OptimisationStrategy.NONE, previousValue, op.getTypeModel());
            return result;
        }
    });
}
Also used : Type(com.redhat.ceylon.model.typechecker.model.Type) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) AssignmentOperatorTranslation(com.redhat.ceylon.compiler.java.codegen.Operators.AssignmentOperatorTranslation)

Example 90 with Type

use of com.redhat.ceylon.model.typechecker.model.Type in project ceylon-compiler by ceylon.

the class ExpressionTransformer method transformAnonymousAnnotation.

public void transformAnonymousAnnotation(Tree.AnonymousAnnotation annotation, Map<Class, ListBuffer<JCAnnotation>> annos) {
    Type docType = ((TypeDeclaration) typeFact().getLanguageModuleDeclaration("DocAnnotation")).getType();
    JCAnnotation docAnnotation = at(annotation).Annotation(makeJavaType(docType, JT_ANNOTATION), List.<JCExpression>of(make().Assign(naming.makeUnquotedIdent("description"), transform(annotation.getStringLiteral()))));
    putAnnotation(annos, docAnnotation, (Class) docType.getDeclaration());
}
Also used : Type(com.redhat.ceylon.model.typechecker.model.Type) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation)

Aggregations

Type (com.redhat.ceylon.model.typechecker.model.Type)237 TypeDeclaration (com.redhat.ceylon.model.typechecker.model.TypeDeclaration)98 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)87 TypeParameter (com.redhat.ceylon.model.typechecker.model.TypeParameter)56 JCTree (com.sun.tools.javac.tree.JCTree)53 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)51 ModelUtil.appliedType (com.redhat.ceylon.model.typechecker.model.ModelUtil.appliedType)46 Class (com.redhat.ceylon.model.typechecker.model.Class)45 ClassOrInterface (com.redhat.ceylon.model.typechecker.model.ClassOrInterface)43 TypedDeclaration (com.redhat.ceylon.model.typechecker.model.TypedDeclaration)41 IntersectionType (com.redhat.ceylon.model.typechecker.model.IntersectionType)37 UnionType (com.redhat.ceylon.model.typechecker.model.UnionType)37 Test (org.junit.Test)37 TypeParser (com.redhat.ceylon.model.loader.TypeParser)36 Declaration (com.redhat.ceylon.model.typechecker.model.Declaration)34 Function (com.redhat.ceylon.model.typechecker.model.Function)33 Interface (com.redhat.ceylon.model.typechecker.model.Interface)30 JCTypeParameter (com.sun.tools.javac.tree.JCTree.JCTypeParameter)30 TypedReference (com.redhat.ceylon.model.typechecker.model.TypedReference)29 ArrayList (java.util.ArrayList)28