Search in sources :

Example 1 with FunctionOrValueInterface

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

the class Metamodel method getTypeDescriptorForProducedType.

public static TypeDescriptor getTypeDescriptorForProducedType(org.eclipse.ceylon.model.typechecker.model.Type type) {
    TypeDeclaration declaration = type.getDeclaration();
    if (type.isNothing()) {
        return TypeDescriptor.NothingType;
    }
    if (type.isUnion()) {
        TypeDescriptor[] tdArgs = getTypeDescriptorsForProducedTypes(type.getCaseTypes());
        return TypeDescriptor.union(tdArgs);
    }
    if (type.isIntersection()) {
        TypeDescriptor[] tdArgs = getTypeDescriptorsForProducedTypes(type.getSatisfiedTypes());
        return TypeDescriptor.intersection(tdArgs);
    }
    if (declaration instanceof LazyClass) {
        ReflectionClass classMirror = (ReflectionClass) ((LazyClass) declaration).classMirror;
        TypeDescriptor[] tdArgs = getTypeDescriptorsForProducedTypes(type.getTypeArgumentList());
        TypeDescriptor ret = TypeDescriptor.klass(classMirror.klass, tdArgs);
        if (type.getQualifyingType() != null)
            return TypeDescriptor.member(getTypeDescriptorForProducedType(type.getQualifyingType()), ret);
        return ret;
    }
    if (declaration instanceof LazyInterface) {
        ReflectionClass classMirror = (ReflectionClass) ((LazyInterface) declaration).classMirror;
        TypeDescriptor[] tdArgs = getTypeDescriptorsForProducedTypes(type.getTypeArgumentList());
        TypeDescriptor ret = TypeDescriptor.klass(classMirror.klass, tdArgs);
        if (type.getQualifyingType() != null)
            return TypeDescriptor.member(getTypeDescriptorForProducedType(type.getQualifyingType()), ret);
        return ret;
    }
    if (declaration instanceof FunctionOrValueInterface) {
        TypedDeclaration underlyingDeclaration = ((FunctionOrValueInterface) declaration).getUnderlyingDeclaration();
        TypeDescriptor[] tdArgs = getTypeDescriptorsForProducedTypes(type.getTypeArgumentList());
        TypeDescriptor ret;
        if (underlyingDeclaration.isToplevel()) {
            ReflectionClass classMirror;
            // type arguments
            if (underlyingDeclaration instanceof Setter)
                underlyingDeclaration = ((Setter) underlyingDeclaration).getGetter();
            if (underlyingDeclaration instanceof LazyValue)
                classMirror = (ReflectionClass) ((LazyValue) underlyingDeclaration).classMirror;
            else if (underlyingDeclaration instanceof LazyFunction)
                classMirror = (ReflectionClass) ((LazyFunction) underlyingDeclaration).classMirror;
            else
                throw Metamodel.newModelError("Unsupported underlying declaration type: " + underlyingDeclaration);
            ret = TypeDescriptor.functionOrValue(classMirror.klass, tdArgs);
        } else
            ret = TypeDescriptor.functionOrValue(underlyingDeclaration.getPrefixedName(), tdArgs);
        if (type.getQualifyingType() != null)
            return TypeDescriptor.member(getTypeDescriptorForProducedType(type.getQualifyingType()), ret);
        return ret;
    }
    if (declaration instanceof UnknownType) {
        ((UnknownType) declaration).reportErrors();
    }
    throw Metamodel.newModelError("Unsupported declaration type: " + (declaration == null ? "null" : declaration.getClass()));
}
Also used : FunctionOrValueInterface(org.eclipse.ceylon.model.loader.model.FunctionOrValueInterface) UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) LazyValue(org.eclipse.ceylon.model.loader.model.LazyValue) TypeDescriptor(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor) ReflectionClass(org.eclipse.ceylon.model.loader.impl.reflect.mirror.ReflectionClass) LazyInterface(org.eclipse.ceylon.model.loader.model.LazyInterface) Setter(org.eclipse.ceylon.model.typechecker.model.Setter) LazyFunction(org.eclipse.ceylon.model.loader.model.LazyFunction) LazyClass(org.eclipse.ceylon.model.loader.model.LazyClass) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)

Example 2 with FunctionOrValueInterface

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

the class TypeParser method loadType.

private Type loadType(String pkg, String fullName, Part part, Type qualifyingType) {
    // try to find a qualified type
    try {
        Declaration newDeclaration;
        if (qualifyingType == null) {
            // FIXME: this only works for packages not contained in multiple modules
            Package foundPackage = moduleScope.getPackage(pkg);
            if (foundPackage != null)
                newDeclaration = loader.getDeclaration(foundPackage.getModule(), pkg, fullName, scope);
            else if (scope != null && !pkg.isEmpty() && loader.isDynamicMetamodel()) {
                // try the default module, damnit
                newDeclaration = loader.getDeclaration(loader.getLoadedModule(Module.DEFAULT_MODULE_NAME, null), pkg, fullName, scope);
            } else if (scope != null) {
                // if we did not find any package and the scope is null, chances are we're after a type variable
                // or a relative type, so use the module scope
                newDeclaration = loader.getDeclaration(moduleScope, pkg, fullName, scope);
            } else
                newDeclaration = null;
        } else {
            // look it up via its qualifying type or decl
            Declaration qualifyingDeclaration = qualifyingType.getDeclaration();
            if (qualifyingType.isUnion() || qualifyingType.isIntersection()) {
                newDeclaration = qualifyingDeclaration.getMember(part.name, null, false);
            } else {
                if (qualifyingDeclaration instanceof FunctionOrValueInterface)
                    qualifyingDeclaration = ((FunctionOrValueInterface) qualifyingDeclaration).getUnderlyingDeclaration();
                newDeclaration = AbstractModelLoader.getDirectMember((Scope) qualifyingDeclaration, part.name);
            }
            if (newDeclaration == null)
                throw new ModelResolutionException("Failed to resolve inner type or declaration " + part.name + " in " + qualifyingDeclaration.getQualifiedNameString());
        }
        if (newDeclaration == null)
            return null;
        TypeDeclaration newTypeDeclaration;
        if (newDeclaration instanceof TypeDeclaration)
            newTypeDeclaration = (TypeDeclaration) newDeclaration;
        else
            newTypeDeclaration = new FunctionOrValueInterface((TypedDeclaration) newDeclaration);
        Type ret = newTypeDeclaration.appliedType(qualifyingType, part.getParameters());
        // set the use-site variance if required, now that we know the TypeParameter declarations
        if (!part.getVariance().isEmpty()) {
            List<TypeParameter> tps = newTypeDeclaration.getTypeParameters();
            List<SiteVariance> variance = part.getVariance();
            for (int i = 0, l1 = tps.size(), l2 = variance.size(); i < l1 && i < l2; i++) {
                SiteVariance siteVariance = variance.get(i);
                if (siteVariance != null) {
                    ret.setVariance(tps.get(i), siteVariance);
                }
            }
        }
        return ret;
    } catch (ModelResolutionException x) {
        // - if we have type parameters we must have a type
        if (qualifyingType != null || (part.parameters != null && !part.parameters.isEmpty()))
            throw x;
        return null;
    }
}
Also used : FunctionOrValueInterface(org.eclipse.ceylon.model.loader.model.FunctionOrValueInterface) Type(org.eclipse.ceylon.model.typechecker.model.Type) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) SiteVariance(org.eclipse.ceylon.model.typechecker.model.SiteVariance) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) Package(org.eclipse.ceylon.model.typechecker.model.Package) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)

Aggregations

FunctionOrValueInterface (org.eclipse.ceylon.model.loader.model.FunctionOrValueInterface)2 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)2 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)2 TypeDescriptor (org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor)1 ReflectionClass (org.eclipse.ceylon.model.loader.impl.reflect.mirror.ReflectionClass)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 LazyValue (org.eclipse.ceylon.model.loader.model.LazyValue)1 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)1 Package (org.eclipse.ceylon.model.typechecker.model.Package)1 Scope (org.eclipse.ceylon.model.typechecker.model.Scope)1 Setter (org.eclipse.ceylon.model.typechecker.model.Setter)1 SiteVariance (org.eclipse.ceylon.model.typechecker.model.SiteVariance)1 Type (org.eclipse.ceylon.model.typechecker.model.Type)1 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)1 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)1