Search in sources :

Example 1 with LazyContainer

use of org.eclipse.ceylon.model.loader.model.LazyContainer in project ceylon by eclipse.

the class AbstractModelLoader method setTypeParametersFromAnnotations.

// from our annotation
private void setTypeParametersFromAnnotations(Scope scope, List<TypeParameter> params, AnnotatedMirror mirror, List<AnnotationMirror> typeParameterAnnotations, List<TypeParameterMirror> typeParameterMirrors) {
    // We must first add every type param, before we resolve the bounds, which can
    // refer to type params.
    String selfTypeName = getSelfTypeFromAnnotations(mirror);
    int i = 0;
    for (AnnotationMirror typeParamAnnotation : typeParameterAnnotations) {
        TypeParameter param = new TypeParameter();
        param.setUnit(scope.getUnit());
        param.setContainer(scope);
        param.setScope(scope);
        ModelUtil.setVisibleScope(param);
        param.setDeclaration((Declaration) scope);
        // let's not trigger the lazy-loading if we're completing a LazyClass/LazyInterface
        if (scope instanceof LazyContainer)
            ((LazyContainer) scope).addMember(param);
        else
            // must be a method
            scope.addMember(param);
        param.setName((String) typeParamAnnotation.getValue("value"));
        param.setExtendedType(typeFactory.getAnythingType());
        if (i < typeParameterMirrors.size()) {
            TypeParameterMirror typeParameterMirror = typeParameterMirrors.get(i);
            param.setNonErasedBounds(hasNonErasedBounds(typeParameterMirror));
        }
        String varianceName = (String) typeParamAnnotation.getValue("variance");
        if (varianceName != null) {
            if (varianceName.equals("IN")) {
                param.setContravariant(true);
            } else if (varianceName.equals("OUT"))
                param.setCovariant(true);
        }
        // If this is a self type param then link it to its type's declaration
        if (param.getName().equals(selfTypeName)) {
            param.setSelfTypedDeclaration((TypeDeclaration) scope);
        }
        params.add(param);
        i++;
    }
    Module moduleScope = ModelUtil.getModuleContainer(scope);
    // Now all type params have been set, we can resolve the references parts
    Iterator<TypeParameter> paramsIterator = params.iterator();
    for (AnnotationMirror typeParamAnnotation : typeParameterAnnotations) {
        TypeParameter param = paramsIterator.next();
        @SuppressWarnings("unchecked") List<String> satisfiesAttribute = (List<String>) typeParamAnnotation.getValue("satisfies");
        setListOfTypes(param.getSatisfiedTypes(), satisfiesAttribute, scope, moduleScope, "type parameter '" + param.getName() + "' satisfied types");
        @SuppressWarnings("unchecked") List<String> caseTypesAttribute = (List<String>) typeParamAnnotation.getValue("caseTypes");
        if (caseTypesAttribute != null && !caseTypesAttribute.isEmpty())
            param.setCaseTypes(new LinkedList<Type>());
        setListOfTypes(param.getCaseTypes(), caseTypesAttribute, scope, moduleScope, "type parameter '" + param.getName() + "' case types");
        String defaultValueAttribute = (String) typeParamAnnotation.getValue("defaultValue");
        if (defaultValueAttribute != null && !defaultValueAttribute.isEmpty()) {
            Type decodedType = decodeType(defaultValueAttribute, scope, moduleScope, "type parameter '" + param.getName() + "' defaultValue");
            param.setDefaultTypeArgument(decodedType);
            param.setDefaulted(true);
        }
    }
}
Also used : TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) LinkedList(java.util.LinkedList) AnnotationMirror(org.eclipse.ceylon.model.loader.mirror.AnnotationMirror) LazyContainer(org.eclipse.ceylon.model.loader.model.LazyContainer) Type(org.eclipse.ceylon.model.typechecker.model.Type) UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) FunctionalInterfaceType(org.eclipse.ceylon.model.loader.mirror.FunctionalInterfaceType) TypeParameterMirror(org.eclipse.ceylon.model.loader.mirror.TypeParameterMirror) List(java.util.List) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Module(org.eclipse.ceylon.model.typechecker.model.Module) LazyModule(org.eclipse.ceylon.model.loader.model.LazyModule)

Example 2 with LazyContainer

use of org.eclipse.ceylon.model.loader.model.LazyContainer in project ceylon by eclipse.

the class AbstractModelLoader method setContainer.

private void setContainer(ClassMirror classMirror, Declaration d, LazyPackage pkg) {
    // add it to its package if it's not an inner class
    if (!classMirror.isInnerClass() && !classMirror.isLocalClass()) {
        d.setContainer(pkg);
        d.setScope(pkg);
        pkg.addCompiledMember(d);
        if (d instanceof LazyInterface && ((LazyInterface) d).isCeylon()) {
            setInterfaceCompanionClass(d, null, pkg);
        }
        ModelUtil.setVisibleScope(d);
    } else if (classMirror.isLocalClass() && !classMirror.isInnerClass()) {
        // set its container to the package for now, but don't add it to the package as a member because it's not
        Scope localContainer = getLocalContainer(pkg, classMirror, d);
        if (localContainer != null) {
            d.setContainer(localContainer);
            d.setScope(localContainer);
        // do not add it as member, it has already been registered by getLocalContainer
        } else {
            d.setContainer(pkg);
            d.setScope(pkg);
        }
        ((LazyElement) d).setLocal(true);
    } else if (d instanceof ClassOrInterface || d instanceof TypeAlias) {
        // we have to set that one first
        if (d instanceof Class == false || !((Class) d).isOverloaded()) {
            ClassOrInterface container = getContainer(pkg.getModule(), classMirror);
            if (d.isNativeHeader() && container.isNative()) {
                container = (ClassOrInterface) ModelUtil.getNativeHeader(container);
            } else if (d.isNativeImplementation() && // for every Java declaration, who don't have native headers
            container.isNativeHeader()) {
                container = (ClassOrInterface) ModelUtil.getNativeDeclaration(container, Backend.Java);
            }
            d.setContainer(container);
            d.setScope(container);
            if (d instanceof LazyInterface && ((LazyInterface) d).isCeylon()) {
                setInterfaceCompanionClass(d, container, pkg);
            }
            // let's not trigger lazy-loading
            ((LazyContainer) container).addMember(d);
            ModelUtil.setVisibleScope(d);
            // now we can do overloads
            if (d instanceof Class && ((Class) d).getOverloads() != null) {
                for (Declaration overload : ((Class) d).getOverloads()) {
                    overload.setContainer(container);
                    overload.setScope(container);
                    // let's not trigger lazy-loading
                    ((LazyContainer) container).addMember(overload);
                    ModelUtil.setVisibleScope(overload);
                }
            }
            // Adds extra members for annotation interop.
            if (d instanceof LazyInterface && !((LazyInterface) d).isCeylon() && ((LazyInterface) d).isAnnotationType()) {
                for (Declaration decl : makeInteropAnnotation((LazyInterface) d, container)) {
                    container.addMember(decl);
                }
            }
        }
    }
}
Also used : ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) LazyContainer(org.eclipse.ceylon.model.loader.model.LazyContainer) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) LazyInterface(org.eclipse.ceylon.model.loader.model.LazyInterface) LazyTypeAlias(org.eclipse.ceylon.model.loader.model.LazyTypeAlias) TypeAlias(org.eclipse.ceylon.model.typechecker.model.TypeAlias) AnnotationProxyClass(org.eclipse.ceylon.model.loader.model.AnnotationProxyClass) LazyClass(org.eclipse.ceylon.model.loader.model.LazyClass) Class(org.eclipse.ceylon.model.typechecker.model.Class) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration)

Example 3 with LazyContainer

use of org.eclipse.ceylon.model.loader.model.LazyContainer in project ceylon by eclipse.

the class AbstractModelLoader method setTypeParameters.

// from java type info
private void setTypeParameters(Scope scope, List<TypeParameter> params, List<TypeParameterMirror> typeParameters, boolean isCeylon) {
    // refer to type params.
    for (TypeParameterMirror typeParam : typeParameters) {
        TypeParameter param = new TypeParameter();
        param.setUnit(scope.getUnit());
        param.setContainer(scope);
        param.setScope(scope);
        ModelUtil.setVisibleScope(param);
        param.setDeclaration((Declaration) scope);
        // let's not trigger the lazy-loading if we're completing a LazyClass/LazyInterface
        if (scope instanceof LazyContainer)
            ((LazyContainer) scope).addMember(param);
        else
            // must be a method
            scope.addMember(param);
        param.setName(typeParam.getName());
        param.setExtendedType(typeFactory.getAnythingType());
        params.add(param);
    }
    boolean needsObjectBounds = !isCeylon && scope instanceof Function;
    // Now all type params have been set, we can resolve the references parts
    Iterator<TypeParameter> paramsIterator = params.iterator();
    for (TypeParameterMirror typeParam : typeParameters) {
        TypeParameter param = paramsIterator.next();
        List<TypeMirror> bounds = typeParam.getBounds();
        for (TypeMirror bound : bounds) {
            Type boundType;
            // we turn java's default upper bound java.lang.Object into ceylon.language.Object
            if (sameType(bound, OBJECT_TYPE)) {
                // especially since we do not want it for types
                if (bounds.size() == 1)
                    break;
                boundType = getNonPrimitiveType(getLanguageModule(), CEYLON_OBJECT_TYPE, scope);
            } else
                boundType = getNonPrimitiveType(ModelUtil.getModuleContainer(scope), bound, scope);
            param.getSatisfiedTypes().add(boundType);
        }
        if (needsObjectBounds && param.getSatisfiedTypes().isEmpty()) {
            Type boundType = getNonPrimitiveType(getLanguageModule(), CEYLON_OBJECT_TYPE, scope);
            param.getSatisfiedTypes().add(boundType);
        }
    }
}
Also used : LazyContainer(org.eclipse.ceylon.model.loader.model.LazyContainer) Function(org.eclipse.ceylon.model.typechecker.model.Function) LazyFunction(org.eclipse.ceylon.model.loader.model.LazyFunction) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) Type(org.eclipse.ceylon.model.typechecker.model.Type) UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) FunctionalInterfaceType(org.eclipse.ceylon.model.loader.mirror.FunctionalInterfaceType) TypeMirror(org.eclipse.ceylon.model.loader.mirror.TypeMirror) TypeParameterMirror(org.eclipse.ceylon.model.loader.mirror.TypeParameterMirror)

Aggregations

LazyContainer (org.eclipse.ceylon.model.loader.model.LazyContainer)3 FunctionalInterfaceType (org.eclipse.ceylon.model.loader.mirror.FunctionalInterfaceType)2 TypeParameterMirror (org.eclipse.ceylon.model.loader.mirror.TypeParameterMirror)2 Type (org.eclipse.ceylon.model.typechecker.model.Type)2 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)2 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 AnnotationMirror (org.eclipse.ceylon.model.loader.mirror.AnnotationMirror)1 TypeMirror (org.eclipse.ceylon.model.loader.mirror.TypeMirror)1 AnnotationProxyClass (org.eclipse.ceylon.model.loader.model.AnnotationProxyClass)1 LazyClass (org.eclipse.ceylon.model.loader.model.LazyClass)1 LazyFunction (org.eclipse.ceylon.model.loader.model.LazyFunction)1 LazyInterface (org.eclipse.ceylon.model.loader.model.LazyInterface)1 LazyModule (org.eclipse.ceylon.model.loader.model.LazyModule)1 LazyTypeAlias (org.eclipse.ceylon.model.loader.model.LazyTypeAlias)1 Class (org.eclipse.ceylon.model.typechecker.model.Class)1 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)1 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)1