Search in sources :

Example 1 with Generic

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

the class ClassOrPackageDoc method doc.

protected final void doc(String name, Declaration d) throws IOException {
    String declarationName = Util.getDeclarationName(d);
    boolean alias = Util.nullSafeCompare(name, declarationName) != 0;
    // put the id on the td because IE8 doesn't support id attributes on tr (yeah right)
    open("tr");
    open("td id='" + name + "' nowrap");
    writeIcon(d);
    if (!(d instanceof Constructor)) {
        around("code class='decl-label'", name);
        close("td");
        open("td");
    }
    writeLinkOneSelf(d);
    if (alias) {
        writeTagged(d);
        writeAlias(d);
    } else {
        writeLinkSource(d);
        writeTagged(d);
        if (d instanceof Functional) {
            writeParameterLinksIfRequired((Functional) d);
        }
        open("code class='signature'");
        around("span class='modifiers'", getModifiers(d));
        write(" ");
        if (!ModelUtil.isConstructor(d)) {
            if (d instanceof Functional && ((Functional) d).isDeclaredVoid()) {
                around("span class='void'", "void");
            } else if (d instanceof TypedDeclaration) {
                linkRenderer().to(((TypedDeclaration) d).getType()).useScope(d).write();
            } else {
                linkRenderer().to(d).useScope(d).write();
            }
        }
        write(" ");
        open("span class='identifier'");
        write(name);
        close("span");
        if (isConstantValue(d)) {
            writeConstantValue((Value) d);
        }
        if (d instanceof Generic) {
            Generic f = (Generic) d;
            writeTypeParameters(f.getTypeParameters(), d);
        }
        if (d instanceof Functional) {
            writeParameterList((Functional) d, d);
        }
        if (d instanceof Generic) {
            Generic f = (Generic) d;
            writeTypeParametersConstraints(f.getTypeParameters(), d);
        }
        if (d instanceof Value) {
            Setter setter = ((Value) d).getSetter();
            if (setter != null && Util.getAnnotation(setter.getUnit(), setter.getAnnotations(), "doc") != null) {
                tool.warningSetterDoc(d.getQualifiedNameString(), d);
            }
        }
        close("code");
        writeDescription(d);
    }
    close("td");
    close("tr");
}
Also used : Functional(com.redhat.ceylon.model.typechecker.model.Functional) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Constructor(com.redhat.ceylon.model.typechecker.model.Constructor) Generic(com.redhat.ceylon.model.typechecker.model.Generic) FunctionOrValue(com.redhat.ceylon.model.typechecker.model.FunctionOrValue) Value(com.redhat.ceylon.model.typechecker.model.Value) Setter(com.redhat.ceylon.model.typechecker.model.Setter)

Example 2 with Generic

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

the class AbstractTransformer method canOptimiseReifiedTypeTest.

/**
     * Determine whether we can use a plain {@code instanceof} instead of 
     * a full {@code Util.isReified()} for a {@code is} test
     */
private boolean canOptimiseReifiedTypeTest(Type type) {
    if (isJavaArray(type)) {
        if (isJavaObjectArray(type)) {
            MultidimensionalArray multiArray = getMultiDimensionalArrayInfo(type);
            // we can test, even if not fully reified in Java
            return multiArray.type.getDeclaration() instanceof ClassOrInterface;
        } else {
            // primitive array we can test
            return true;
        }
    }
    // we can optimise it if we've got a ClassOrInterface with only Anything type parameters
    if (type.getDeclaration() instanceof ClassOrInterface == false)
        return false;
    for (Entry<TypeParameter, Type> entry : type.getTypeArguments().entrySet()) {
        TypeParameter tp = entry.getKey();
        if (!type.isCovariant(tp)) {
            return false;
        }
        java.util.List<Type> bounds = tp.getSatisfiedTypes();
        Type ta = entry.getValue();
        if ((bounds == null || bounds.isEmpty()) && !isAnything(ta)) {
            return false;
        }
        for (Type bound : bounds) {
            if (!ta.isSupertypeOf(bound)) {
                return false;
            }
        }
    }
    // they're all Anything (or supertypes of their upper bound) we can optimise, unless we have a container with type arguments
    Type qualifyingType = type.getQualifyingType();
    if (qualifyingType == null && // ignore qualifying types of static java declarations
    (Decl.isCeylon(type.getDeclaration()) || !type.getDeclaration().isStaticallyImportable())) {
        Declaration declaration = type.getDeclaration();
        do {
            // it may be contained in a function or value, and we want its type
            Declaration enclosingDeclaration = getDeclarationContainer(declaration);
            if (enclosingDeclaration instanceof TypedDeclaration) {
                // must be in scope
                if (enclosingDeclaration instanceof Generic && !((Generic) enclosingDeclaration).getTypeParameters().isEmpty())
                    return false;
                // look up the containers
                declaration = enclosingDeclaration;
            } else if (enclosingDeclaration instanceof TypeDeclaration) {
                // we can't optimise if that container has type arguments as they are not provided
                if (enclosingDeclaration instanceof Generic && !((Generic) enclosingDeclaration).getTypeParameters().isEmpty())
                    return false;
                // look up the containers
                declaration = enclosingDeclaration;
            } else {
                // that's fucked up
                break;
            }
        // go up every containing typed declaration
        } while (declaration != null);
        // we can optimise!
        return true;
    } else if (qualifyingType != null) {
        // we can only optimise if the qualifying type can also be optimised
        return canOptimiseReifiedTypeTest(qualifyingType);
    } else {
        // we can optimise!
        return true;
    }
}
Also used : ClassOrInterface(com.redhat.ceylon.model.typechecker.model.ClassOrInterface) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) 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) Generic(com.redhat.ceylon.model.typechecker.model.Generic) 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)

Example 3 with Generic

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

the class ClassTransformer method typeParametersOfAllContainers.

private java.util.List<TypeParameter> typeParametersOfAllContainers(final ClassOrInterface model, boolean includeModelTypeParameters) {
    java.util.List<java.util.List<TypeParameter>> r = new ArrayList<java.util.List<TypeParameter>>(1);
    Scope s = model.getContainer();
    while (!(s instanceof Package)) {
        if (s instanceof Generic) {
            r.add(0, ((Generic) s).getTypeParameters());
        }
        s = s.getContainer();
    }
    Set<String> names = new HashSet<String>();
    for (TypeParameter tp : model.getTypeParameters()) {
        names.add(tp.getName());
    }
    java.util.List<TypeParameter> result = new ArrayList<TypeParameter>(1);
    for (java.util.List<TypeParameter> tps : r) {
        for (TypeParameter tp : tps) {
            if (names.add(tp.getName())) {
                result.add(tp);
            }
        }
    }
    if (includeModelTypeParameters)
        result.addAll(model.getTypeParameters());
    return result;
}
Also used : TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) Generic(com.redhat.ceylon.model.typechecker.model.Generic) ArrayList(java.util.ArrayList) Scope(com.redhat.ceylon.model.typechecker.model.Scope) ArrayList(java.util.ArrayList) AnnotationList(com.redhat.ceylon.compiler.typechecker.tree.Tree.AnnotationList) List(com.sun.tools.javac.util.List) ParameterList(com.redhat.ceylon.model.typechecker.model.ParameterList) Package(com.redhat.ceylon.model.typechecker.model.Package) HashSet(java.util.HashSet)

Example 4 with Generic

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

the class ClassTransformer method typeParametersForInstantiator.

/**
     * When generating an instantiator method if the inner class has a type 
     * parameter with the same name as a type parameter of an outer type, then the 
     * instantiator method shouldn't declare its own type parameter of that 
     * name -- it should use the captured one. This method filters out the
     * type parameters of the inner class which are the same as type parameters 
     * of the outer class so that they can be captured.
     */
private java.util.List<TypeParameter> typeParametersForInstantiator(final Class model) {
    java.util.List<TypeParameter> filtered = new ArrayList<TypeParameter>();
    java.util.List<TypeParameter> tps = model.getTypeParameters();
    if (tps != null) {
        for (TypeParameter tp : tps) {
            boolean omit = false;
            Scope s = model.getContainer();
            while (!(s instanceof Package)) {
                if (s instanceof Generic) {
                    for (TypeParameter outerTp : ((Generic) s).getTypeParameters()) {
                        if (tp.getName().equals(outerTp.getName())) {
                            omit = true;
                        }
                    }
                }
                s = s.getContainer();
            }
            if (!omit) {
                filtered.add(tp);
            }
        }
    }
    return filtered;
}
Also used : TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) Scope(com.redhat.ceylon.model.typechecker.model.Scope) Generic(com.redhat.ceylon.model.typechecker.model.Generic) ArrayList(java.util.ArrayList) Package(com.redhat.ceylon.model.typechecker.model.Package)

Example 5 with Generic

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

the class ClassTransformer method addMissingUnrefinedMembers.

/** 
     * Recover from members not being refined in the class hierarchy 
     * by generating a stub method that throws.
     */
private void addMissingUnrefinedMembers(Node def, Class classModel, ClassDefinitionBuilder classBuilder) {
    for (Reference unrefined : classModel.getUnimplementedFormals()) {
        //classModel.getMember(memberName, null, false);
        Declaration formalMember = unrefined.getDeclaration();
        String errorMessage = "formal member '" + formalMember.getName() + "' of '" + ((TypeDeclaration) formalMember.getContainer()).getName() + "' not implemented in class hierarchy";
        java.util.List<Type> params = new java.util.ArrayList<Type>();
        if (formalMember instanceof Generic) {
            for (TypeParameter tp : ((Generic) formalMember).getTypeParameters()) {
                params.add(tp.getType());
            }
        }
        if (formalMember instanceof Value) {
            addRefinedThrowerAttribute(classBuilder, errorMessage, classModel, (Value) formalMember);
        } else if (formalMember instanceof Function) {
            addRefinedThrowerMethod(classBuilder, errorMessage, classModel, (Function) formalMember);
        } else if (formalMember instanceof Class && formalMember.isClassMember()) {
            addRefinedThrowerInstantiatorMethod(classBuilder, errorMessage, classModel, (Class) formalMember, unrefined);
        }
    // formal member class of interface handled in
    // makeDelegateToCompanion()
    }
}
Also used : TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) Reference(com.redhat.ceylon.model.typechecker.model.Reference) TypedReference(com.redhat.ceylon.model.typechecker.model.TypedReference) Generic(com.redhat.ceylon.model.typechecker.model.Generic) ArrayList(java.util.ArrayList) Function(com.redhat.ceylon.model.typechecker.model.Function) Type(com.redhat.ceylon.model.typechecker.model.Type) FunctionOrValue(com.redhat.ceylon.model.typechecker.model.FunctionOrValue) JavaBeanValue(com.redhat.ceylon.model.loader.model.JavaBeanValue) Value(com.redhat.ceylon.model.typechecker.model.Value) Class(com.redhat.ceylon.model.typechecker.model.Class) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) MethodDeclaration(com.redhat.ceylon.compiler.typechecker.tree.Tree.MethodDeclaration) AttributeDeclaration(com.redhat.ceylon.compiler.typechecker.tree.Tree.AttributeDeclaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration)

Aggregations

Generic (com.redhat.ceylon.model.typechecker.model.Generic)12 TypeParameter (com.redhat.ceylon.model.typechecker.model.TypeParameter)9 Type (com.redhat.ceylon.model.typechecker.model.Type)8 TypeDeclaration (com.redhat.ceylon.model.typechecker.model.TypeDeclaration)7 TypedDeclaration (com.redhat.ceylon.model.typechecker.model.TypedDeclaration)7 Declaration (com.redhat.ceylon.model.typechecker.model.Declaration)6 ModelUtil.appliedType (com.redhat.ceylon.model.typechecker.model.ModelUtil.appliedType)4 ArrayList (java.util.ArrayList)4 ClassOrInterface (com.redhat.ceylon.model.typechecker.model.ClassOrInterface)3 Package (com.redhat.ceylon.model.typechecker.model.Package)3 Scope (com.redhat.ceylon.model.typechecker.model.Scope)3 JCTypeParameter (com.sun.tools.javac.tree.JCTree.JCTypeParameter)3 Class (com.redhat.ceylon.model.typechecker.model.Class)2 Constructor (com.redhat.ceylon.model.typechecker.model.Constructor)2 Function (com.redhat.ceylon.model.typechecker.model.Function)2 FunctionOrValue (com.redhat.ceylon.model.typechecker.model.FunctionOrValue)2 Reference (com.redhat.ceylon.model.typechecker.model.Reference)2 TypedReference (com.redhat.ceylon.model.typechecker.model.TypedReference)2 Value (com.redhat.ceylon.model.typechecker.model.Value)2 JCNewClass (com.sun.tools.javac.tree.JCTree.JCNewClass)2