Search in sources :

Example 66 with TypeDeclaration

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

the class Naming method appendTypeDeclaration.

private void appendTypeDeclaration(final TypeDeclaration decl, EnumSet<DeclNameFlag> flags, TypeDeclarationBuilder<?> typeDeclarationBuilder, Scope scope, final boolean last) {
    if (scope instanceof Class || scope instanceof TypeAlias || (scope instanceof Constructor && (scope.equals(decl) || !Decl.isLocalNotInitializerScope(scope)))) {
        TypeDeclaration klass = (TypeDeclaration) scope;
        if (klass.isAnonymous() && !klass.isNamed())
            typeDeclarationBuilder.clear();
        typeDeclarationBuilder.append(escapeClassName(klass.getName() != null ? klass.getName() : ""));
        if (Decl.isCeylon(klass)) {
            if (flags.contains(DeclNameFlag.COMPANION) && Decl.isLocalNotInitializer(klass) && last) {
                typeDeclarationBuilder.append(IMPL_POSTFIX);
            } else if (flags.contains(DeclNameFlag.ANNOTATION) && last) {
                typeDeclarationBuilder.append(ANNO_POSTFIX);
            } else if (flags.contains(DeclNameFlag.ANNOTATIONS) && last) {
                typeDeclarationBuilder.append(ANNOS_POSTFIX);
            } else if (flags.contains(DeclNameFlag.DELEGATION) && last) {
                typeDeclarationBuilder.append(DELEGATION_POSTFIX);
            }
        }
    } else if (scope instanceof Interface) {
        Interface iface = (Interface) scope;
        typeDeclarationBuilder.append(iface.getName());
        if (Decl.isCeylon(iface) && ((decl instanceof Class || decl instanceof Constructor || decl instanceof TypeAlias || scope instanceof Constructor) || flags.contains(DeclNameFlag.COMPANION))) {
            typeDeclarationBuilder.append(IMPL_POSTFIX);
        }
    } else if (Decl.isLocalNotInitializerScope(scope)) {
        if (flags.contains(DeclNameFlag.COMPANION) || !(decl instanceof Interface)) {
            typeDeclarationBuilder.clear();
        } else if (flags.contains(DeclNameFlag.QUALIFIED) || (decl instanceof Interface)) {
            Scope nonLocal = scope;
            while (!(nonLocal instanceof Declaration)) {
                nonLocal = nonLocal.getContainer();
            }
            typeDeclarationBuilder.append(((Declaration) nonLocal).getPrefixedName());
            if (!Decl.equalScopes(scope, nonLocal)) {
                typeDeclarationBuilder.append('$');
                typeDeclarationBuilder.append(getLocalId(scope));
            }
            if (decl instanceof Interface) {
                typeDeclarationBuilder.append('$');
            } else {
                if (flags.contains(DeclNameFlag.QUALIFIED)) {
                    typeDeclarationBuilder.selectAppended();
                } else {
                    typeDeclarationBuilder.clear();
                }
            }
        }
        return;
    } else if (scope instanceof TypedDeclaration && ((Declaration) scope).isToplevel()) {
    // nothing? that's just weird
    }
    if (!last) {
        if (decl instanceof Interface && Decl.isCeylon((TypeDeclaration) decl) && !flags.contains(DeclNameFlag.COMPANION)) {
            typeDeclarationBuilder.append('$');
        } else if (decl instanceof Constructor && ((Class) decl.getContainer()).isMember() && decl.getContainer().equals(scope)) {
            typeDeclarationBuilder.append('$');
        } else {
            if (flags.contains(DeclNameFlag.QUALIFIED)) {
                typeDeclarationBuilder.selectAppended();
            } else {
                typeDeclarationBuilder.clear();
            }
        }
    } else {
        typeDeclarationBuilder.selectAppended();
    }
    return;
}
Also used : TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Scope(com.redhat.ceylon.model.typechecker.model.Scope) Constructor(com.redhat.ceylon.model.typechecker.model.Constructor) Class(com.redhat.ceylon.model.typechecker.model.Class) LazyClass(com.redhat.ceylon.model.loader.model.LazyClass) TypeAlias(com.redhat.ceylon.model.typechecker.model.TypeAlias) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) LazyInterface(com.redhat.ceylon.model.loader.model.LazyInterface) Interface(com.redhat.ceylon.model.typechecker.model.Interface) ClassOrInterface(com.redhat.ceylon.model.typechecker.model.ClassOrInterface)

Example 67 with TypeDeclaration

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

the class AbstractTransformer method isTurnedToRawResolved.

private boolean isTurnedToRawResolved(Type type) {
    // if we don't have type arguments we can't be raw
    if (type.getTypeArguments().isEmpty())
        return false;
    // we only go raw if any type param is an erased union/intersection
    // start with type but consider ever qualifying type
    Type singleType = type;
    do {
        // special case for Callable where we stop after the first type param
        boolean isCallable = isCeylonCallable(singleType);
        TypeDeclaration declaration = singleType.getDeclaration();
        Map<TypeParameter, Type> typeArguments = singleType.getTypeArguments();
        for (TypeParameter tp : declaration.getTypeParameters()) {
            Type ta = typeArguments.get(tp);
            // skip invalid input
            if (tp == null || ta == null)
                return false;
            // see makeTypeArgs: Nothing in contravariant position causes a raw type
            if (singleType.isContravariant(tp) && ta.isNothing())
                return true;
            if (isErasedUnionOrIntersection(ta)) {
                return true;
            }
            // Callable really has a single type arg in Java
            if (isCallable)
                break;
        // don't recurse
        }
    // move on to next qualifying type
    } while ((singleType = singleType.getQualifyingType()) != null);
    // we're only raw if every type param is an erased union/intersection
    return false;
}
Also used : Type(com.redhat.ceylon.model.typechecker.model.Type) ModelUtil.appliedType(com.redhat.ceylon.model.typechecker.model.ModelUtil.appliedType) TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration)

Example 68 with TypeDeclaration

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

the class AbstractTransformer method hasSubstitutedBounds.

boolean hasSubstitutedBounds(Type pt) {
    TypeDeclaration declaration = pt.getDeclaration();
    java.util.List<TypeParameter> tps = declaration.getTypeParameters();
    final Map<TypeParameter, Type> tas = pt.getTypeArguments();
    boolean isCallable = isCeylonCallable(pt);
    for (TypeParameter tp : tps) {
        Type ta = tas.get(tp);
        // error recovery
        if (ta == null)
            continue;
        if (!tp.getSatisfiedTypes().isEmpty()) {
            for (Type bound : tp.getSatisfiedTypes()) {
                bound = bound.substitute(pt);
                if (expressionGen().needsCast(ta, bound, false, false, false))
                    return true;
            }
        }
        if (hasSubstitutedBounds(ta))
            return true;
        // Callable ignores type parameters after the first
        if (isCallable)
            break;
    }
    return false;
}
Also used : TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) Type(com.redhat.ceylon.model.typechecker.model.Type) ModelUtil.appliedType(com.redhat.ceylon.model.typechecker.model.ModelUtil.appliedType) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration)

Example 69 with TypeDeclaration

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

the class AbstractTransformer method makeParameterisedType.

public JCExpression makeParameterisedType(Type type, Type generalType, final int flags, JCExpression qualifyingExpression, java.util.List<Reference> qualifyingTypes, int firstQualifyingTypeWithTypeParameters, int index) {
    JCExpression baseType;
    TypeDeclaration tdecl = type.getDeclaration();
    ListBuffer<JCExpression> typeArgs = null;
    if (index >= firstQualifyingTypeWithTypeParameters) {
        int taFlags = flags;
        if (qualifyingTypes != null && index < qualifyingTypes.size()) {
            // The qualifying types before the main one should
            // have type parameters with proper variance
            taFlags &= ~(JT_EXTENDS | JT_SATISFIES);
        }
        typeArgs = makeTypeArgs(type, taFlags);
    }
    if (isCeylonCallable(generalType) && (flags & JT_CLASS_NEW) != 0) {
        baseType = makeIdent(syms().ceylonAbstractCallableType);
    } else if (index == 0) {
        // qualified type is static
        if (tdecl instanceof Interface && qualifyingTypes != null && qualifyingTypes.size() > 1 && firstQualifyingTypeWithTypeParameters == 0 && (flags & JT_NON_QUALIFIED) == 0) {
            baseType = naming.makeCompanionClassName(tdecl);
        } else {
            baseType = naming.makeDeclarationName(tdecl, jtFlagsToDeclNameOpts(flags));
        }
    } else {
        baseType = naming.makeTypeDeclarationExpression(qualifyingExpression, tdecl, jtFlagsToDeclNameOpts(flags | JT_NON_QUALIFIED | (type.getDeclaration() instanceof Interface ? JT_COMPANION : 0)));
    }
    if (typeArgs != null && typeArgs.size() > 0) {
        qualifyingExpression = make().TypeApply(baseType, typeArgs.toList());
    } else {
        qualifyingExpression = baseType;
    }
    return qualifyingExpression;
}
Also used : JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) ClassOrInterface(com.redhat.ceylon.model.typechecker.model.ClassOrInterface) Interface(com.redhat.ceylon.model.typechecker.model.Interface)

Example 70 with TypeDeclaration

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

the class Util method getAncestors.

public static List<TypeDeclaration> getAncestors(TypeDeclaration decl) {
    List<TypeDeclaration> ancestors = new ArrayList<TypeDeclaration>();
    Type ancestor = decl.getExtendedType();
    while (ancestor != null) {
        ancestors.add(ancestor.getDeclaration());
        ancestor = ancestor.getExtendedType();
    }
    return ancestors;
}
Also used : Type(com.redhat.ceylon.model.typechecker.model.Type) ArrayList(java.util.ArrayList) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration)

Aggregations

TypeDeclaration (com.redhat.ceylon.model.typechecker.model.TypeDeclaration)140 Type (com.redhat.ceylon.model.typechecker.model.Type)85 Test (org.junit.Test)51 Class (com.redhat.ceylon.model.typechecker.model.Class)40 ClassOrInterface (com.redhat.ceylon.model.typechecker.model.ClassOrInterface)37 TypedDeclaration (com.redhat.ceylon.model.typechecker.model.TypedDeclaration)36 IntersectionType (com.redhat.ceylon.model.typechecker.model.IntersectionType)33 UnionType (com.redhat.ceylon.model.typechecker.model.UnionType)33 TypeParser (com.redhat.ceylon.model.loader.TypeParser)32 Declaration (com.redhat.ceylon.model.typechecker.model.Declaration)32 Interface (com.redhat.ceylon.model.typechecker.model.Interface)27 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)24 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)20 ModelUtil.appliedType (com.redhat.ceylon.model.typechecker.model.ModelUtil.appliedType)19 TypeParameter (com.redhat.ceylon.model.typechecker.model.TypeParameter)19 Function (com.redhat.ceylon.model.typechecker.model.Function)18 ArrayList (java.util.ArrayList)17 JCTree (com.sun.tools.javac.tree.JCTree)16 FunctionOrValue (com.redhat.ceylon.model.typechecker.model.FunctionOrValue)14 JCNewClass (com.sun.tools.javac.tree.JCTree.JCNewClass)14