Search in sources :

Example 51 with TypeDeclaration

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

the class LinkRenderer method processAnnotationParam.

private String processAnnotationParam(String text) {
    if (text.equals("module")) {
        Module mod = getCurrentModule();
        if (mod != null) {
            return processModule(mod);
        }
    }
    if (text.startsWith("module ")) {
        String modName = text.substring(7);
        for (Module m : ceylonDocTool.getTypeChecker().getContext().getModules().getListOfModules()) {
            if (m.getNameAsString().equals(modName)) {
                return processModule(m);
            }
        }
    }
    if (text.equals("package")) {
        Package pkg = getCurrentPackage();
        if (pkg != null) {
            return processPackage(pkg);
        }
    }
    if (text.startsWith("package ")) {
        String pkgName = text.substring(8);
        for (Module m : ceylonDocTool.getTypeChecker().getContext().getModules().getListOfModules()) {
            if (pkgName.startsWith(m.getNameAsString() + ".")) {
                Package pkg = m.getPackage(pkgName);
                if (pkg != null) {
                    return processPackage(pkg);
                }
            }
        }
    }
    if (text.equals("interface")) {
        Interface interf = getCurrentInterface();
        if (interf != null) {
            return processProducedType(interf.getType());
        }
    }
    if (text.equals("class")) {
        Class clazz = getCurrentClass();
        if (clazz != null) {
            return processProducedType(clazz.getType());
        }
    }
    String declName;
    Scope currentScope;
    int pkgSeparatorIndex = text.indexOf("::");
    if (pkgSeparatorIndex == -1) {
        declName = text;
        currentScope = resolveScope(scope);
    } else {
        String pkgName = text.substring(0, pkgSeparatorIndex);
        declName = text.substring(pkgSeparatorIndex + 2, text.length());
        currentScope = ceylonDocTool.getCurrentModule().getPackage(pkgName);
    }
    String[] declNames = declName.split("\\.");
    Declaration currentDecl = null;
    boolean isNested = false;
    for (String currentDeclName : declNames) {
        currentDecl = resolveDeclaration(currentScope, currentDeclName, isNested);
        if (currentDecl != null) {
            if (isValueWithTypeObject(currentDecl)) {
                TypeDeclaration objectType = ((Value) currentDecl).getTypeDeclaration();
                currentScope = objectType;
                currentDecl = objectType;
            } else {
                currentScope = resolveScope(currentDecl);
            }
            isNested = true;
        } else {
            break;
        }
    }
    // we can't link to parameters yet, unless they're toplevel
    if (currentDecl != null && !isParameter(currentDecl)) {
        if (currentDecl instanceof TypeDeclaration) {
            return processProducedType(((TypeDeclaration) currentDecl).getType());
        } else {
            return processTypedDeclaration((TypedDeclaration) currentDecl);
        }
    } else {
        return getUnresolvableLink(text);
    }
}
Also used : Scope(com.redhat.ceylon.model.typechecker.model.Scope) FunctionOrValue(com.redhat.ceylon.model.typechecker.model.FunctionOrValue) Value(com.redhat.ceylon.model.typechecker.model.Value) Class(com.redhat.ceylon.model.typechecker.model.Class) Package(com.redhat.ceylon.model.typechecker.model.Package) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) Module(com.redhat.ceylon.model.typechecker.model.Module) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) Interface(com.redhat.ceylon.model.typechecker.model.Interface) ClassOrInterface(com.redhat.ceylon.model.typechecker.model.ClassOrInterface)

Example 52 with TypeDeclaration

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

the class Util method getSuperInterfaces.

public static List<TypeDeclaration> getSuperInterfaces(TypeDeclaration decl) {
    Set<TypeDeclaration> superInterfaces = new HashSet<TypeDeclaration>();
    for (Type satisfiedType : decl.getSatisfiedTypes()) {
        superInterfaces.add(satisfiedType.getDeclaration());
        superInterfaces.addAll(getSuperInterfaces(satisfiedType.getDeclaration()));
    }
    List<TypeDeclaration> list = new ArrayList<TypeDeclaration>();
    list.addAll(superInterfaces);
    removeDuplicates(list);
    return list;
}
Also used : Type(com.redhat.ceylon.model.typechecker.model.Type) ArrayList(java.util.ArrayList) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) HashSet(java.util.HashSet)

Example 53 with TypeDeclaration

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

the class AbstractTransformer method collectRefinedMembers.

private void collectRefinedMembers(Type currentType, String name, java.util.List<Type> signature, boolean ellipsis, java.util.Set<TypeDeclaration> visited, Set<TypedDeclaration> ret, boolean ignoreFirst) {
    TypeDeclaration decl = currentType.getDeclaration();
    if (visited.contains(decl)) {
        return;
    } else {
        visited.add(decl);
        Type et = currentType.getExtendedType();
        if (et != null) {
            collectRefinedMembers(et, name, signature, ellipsis, visited, ret, false);
        }
        for (Type st : currentType.getSatisfiedTypes()) {
            collectRefinedMembers(st, name, signature, ellipsis, visited, ret, false);
        }
        // we're collecting refined members, not the refining one
        if (!ignoreFirst) {
            TypedDeclaration found = (TypedDeclaration) decl.getDirectMember(name, signature, ellipsis);
            if (found instanceof Function) {
                // do not trust getDirectMember because if you ask it for [Integer,String] and it has [Integer,E] it does not
                // know that E=String and will not make it match, and will just return any member when there is overloading,
                // including one with signature [String] when you asked for [Integer,String]
                java.util.List<Type> typedSignature = getTypedSignature(currentType, found);
                if (typedSignature != null && hasMatchingSignature(signature, typedSignature))
                    ret.add(found);
            }
        }
    }
}
Also used : TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Function(com.redhat.ceylon.model.typechecker.model.Function) 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 54 with TypeDeclaration

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

the class AbstractTransformer method hasDependentCovariantTypeParameters.

boolean hasDependentCovariantTypeParameters(Type type) {
    if (type.isUnion()) {
        for (Type m : type.getCaseTypes()) if (hasDependentCovariantTypeParameters(m))
            return true;
        return false;
    }
    if (type.isIntersection()) {
        for (Type m : type.getSatisfiedTypes()) if (hasDependentCovariantTypeParameters(m))
            return true;
        return false;
    }
    // check its type arguments
    // special case for Callable which has only a single type param in Java
    boolean isCallable = isCeylonCallable(type);
    // check if any type parameter is dependent on and covariant
    TypeDeclaration declaration = type.getDeclaration();
    java.util.List<TypeParameter> typeParams = declaration.getTypeParameters();
    Map<TypeParameter, Type> typeArguments = type.getTypeArguments();
    for (TypeParameter typeParam : typeParams) {
        Type typeArg = typeArguments.get(typeParam);
        if (type.isCovariant(typeParam) && hasDependentTypeParameters(typeParams, typeParam)) {
            // see if the type argument in question contains type parameters and is erased to Object
            if (containsTypeParameter(typeArg) && willEraseToObject(typeArg))
                return true;
        }
        // now check if we the type argument has the same problem
        if (hasDependentCovariantTypeParameters(typeArg))
            return true;
        // stop after the first type arg for Callable
        if (isCallable)
            break;
    }
    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 55 with TypeDeclaration

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

the class AbstractTransformer method isJavaEnumType.

boolean isJavaEnumType(Type type) {
    Module jdkBaseModule = loader().getJDKBaseModule();
    Package javaLang = jdkBaseModule.getPackage("java.lang");
    TypeDeclaration enumDecl = (TypeDeclaration) javaLang.getDirectMember("Enum", null, false);
    if (type.isClass() && type.getDeclaration().isAnonymous()) {
        type = type.getExtendedType();
    }
    return type.isSubtypeOf(enumDecl.appliedType(null, Collections.singletonList(type)));
}
Also used : Package(com.redhat.ceylon.model.typechecker.model.Package) Module(com.redhat.ceylon.model.typechecker.model.Module) 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