Search in sources :

Example 11 with TypeDescriptor

use of org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor 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 12 with TypeDescriptor

use of org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor in project ceylon by eclipse.

the class Metamodel method isReified.

/**
 * Implementation of {@code is} operator
 */
public static boolean isReified(java.lang.Object instance, TypeDescriptor type) {
    if (instance == null) {
        return type.containsNull();
    }
    TypeDescriptor instanceType = getTypeDescriptor(instance);
    if (instanceType == null) {
        return false;
    }
    if (instanceType == type) {
        return true;
    }
    boolean result = type.is(instanceType);
    if (!result && // we lack reified types
    !(instance instanceof ReifiedType) && // we're testing for a generic type
    type instanceof TypeDescriptor.Class && ((TypeDescriptor.Class) type).isGeneric() && // the instance is an instance of the base type
    ((TypeDescriptor.Class) type).getKlass().isInstance(instance) && // the type isn't reified by inheritance
    !reifiedByInheritance(instance.getClass(), ((TypeDescriptor.Class) type).getKlass())) {
        // throw when asked if an instance of a Java class is
        // of a generic type and we don't have sufficient information to
        // answer correctly.
        // note we do this only if the isSubtypeOf() test fails
        // that's so that we don't have to worry about type applications
        // such as like <out Anything> which is true even in the absence
        // of reified type arguments
        // throw new ReifiedTypeError("Cannot determine whether " + instance.getClass() + " is a " + type);
        // I can do it in Java, so what the hell ;-)
        result = true;
    }
    return result;
}
Also used : TypeDescriptor(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor) ReifiedType(org.eclipse.ceylon.compiler.java.runtime.model.ReifiedType) AnnotationProxyClass(org.eclipse.ceylon.model.loader.model.AnnotationProxyClass) ReflectionClass(org.eclipse.ceylon.model.loader.impl.reflect.mirror.ReflectionClass) LazyClass(org.eclipse.ceylon.model.loader.model.LazyClass)

Example 13 with TypeDescriptor

use of org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor in project ceylon by eclipse.

the class Metamodel method getConstructors.

public static <Type> Sequential getConstructors(ClassOrInterfaceImpl<Type> cls, boolean justShared, boolean callableConstructors, TypeDescriptor $reified$Arguments, ceylon.language.Sequential<? extends ceylon.language.meta.model.Type<? extends java.lang.annotation.Annotation>> annotations) {
    ArrayList<Object> ctors = new ArrayList<>();
    org.eclipse.ceylon.model.typechecker.model.Type reifiedArguments = $reified$Arguments == null ? null : Metamodel.getProducedType($reified$Arguments);
    TypeDescriptor[] annotationTypeDescriptors = Metamodel.getTypeDescriptors(annotations);
    if (cls.declaration instanceof ClassWithInitializerDeclarationImpl) {
        Reference producedReference = cls.declaration.declaration.appliedReference(cls.producedType, Collections.<org.eclipse.ceylon.model.typechecker.model.Type>emptyList());
        org.eclipse.ceylon.model.typechecker.model.Type argumentsType = Metamodel.getProducedTypeForArguments(cls.declaration.declaration.getUnit(), (Functional) cls.declaration.declaration, producedReference);
        if (reifiedArguments == null || reifiedArguments.isSubtypeOf(argumentsType)) {
            if (hasAllAnnotations(((ClassWithInitializerDeclarationImpl) cls.declaration).getDefaultConstructor(), annotationTypeDescriptors)) {
                // TODO test for arguments too
                // ctors.add(new AppliedInitializer((AppliedClass)cls));
                ctors.add(((ClassModel<?, ?>) cls).getDefaultConstructor());
            }
        }
    } else {
        for (ceylon.language.meta.declaration.Declaration d : ((ClassDeclarationImpl) cls.declaration).constructors()) {
            Declaration dd = null;
            AnnotatedDeclaration annotated;
            if (d instanceof CallableConstructorDeclarationImpl && callableConstructors) {
                dd = ((CallableConstructorDeclarationImpl) d).declaration;
                annotated = (CallableConstructorDeclaration) d;
            } else if (d instanceof ValueConstructorDeclarationImpl && !callableConstructors) {
                dd = ((ValueConstructorDeclarationImpl) d).declaration;
                annotated = (ValueConstructorDeclaration) d;
            } else {
                continue;
            }
            // ATM this is an AND WRT annotation types: all must be present
            if (!hasAllAnnotations(annotated, annotationTypeDescriptors))
                continue;
            if (dd instanceof Functional && reifiedArguments != null) {
                // CallableConstructor need a check on the <Arguments>
                Reference producedReference = dd.appliedReference(cls.producedType, Collections.<org.eclipse.ceylon.model.typechecker.model.Type>emptyList());
                org.eclipse.ceylon.model.typechecker.model.Type argumentsType = Metamodel.getProducedTypeForArguments(dd.getUnit(), (Functional) dd, producedReference);
                if (!reifiedArguments.isSubtypeOf(argumentsType))
                    continue;
            }
            // ATM this is an AND WRT annotation types: all must be present
            if (!Metamodel.hasAllAnnotations((AnnotatedDeclaration) d, annotationTypeDescriptors))
                continue;
            if (dd instanceof Functional && reifiedArguments != null) {
                // CallableConstructor need a check on the <Arguments>
                Reference producedReference = dd.appliedReference(cls.producedType, Collections.<org.eclipse.ceylon.model.typechecker.model.Type>emptyList());
                org.eclipse.ceylon.model.typechecker.model.Type argumentsType = Metamodel.getProducedTypeForArguments(dd.getUnit(), (Functional) dd, producedReference);
                if (!reifiedArguments.isSubtypeOf(argumentsType))
                    continue;
            }
            if (!justShared || (d instanceof NestableDeclaration && ((NestableDeclaration) d).getShared())) {
                Object ctor;
                if (cls instanceof ClassImpl<?, ?>) {
                    ctor = ((ClassImpl<?, ?>) cls).getDeclaredConstructor(TypeDescriptor.NothingType, d.getName());
                } else {
                    // if (cls instanceof AppliedMemberClass<?,?,?>) {
                    ctor = ((MemberClassImpl<?, ?, ?>) cls).getDeclaredConstructor(TypeDescriptor.NothingType, d.getName());
                }
                ctors.add(ctor);
            }
        }
    }
    Object[] array = ctors.toArray(new Object[ctors.size()]);
    ObjectArrayIterable<ceylon.language.meta.declaration.Declaration> iterable = new ObjectArrayIterable<ceylon.language.meta.declaration.Declaration>(TypeDescriptor.union(TypeDescriptor.klass(FunctionModel.class, cls.$reifiedType, TypeDescriptor.NothingType), TypeDescriptor.klass(ValueModel.class, cls.$reifiedType, TypeDescriptor.NothingType)), (Object[]) array);
    return (ceylon.language.Sequential) iterable.sequence();
}
Also used : CallableConstructorDeclarationImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.CallableConstructorDeclarationImpl) MemberClassImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.MemberClassImpl) ClassImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.ClassImpl) ValueConstructorDeclaration(ceylon.language.meta.declaration.ValueConstructorDeclaration) ArrayList(java.util.ArrayList) Type(org.eclipse.ceylon.model.typechecker.model.Type) NestableDeclaration(ceylon.language.meta.declaration.NestableDeclaration) ClassWithInitializerDeclarationImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassWithInitializerDeclarationImpl) AnnotatedDeclaration(ceylon.language.meta.declaration.AnnotatedDeclaration) Sequential(ceylon.language.Sequential) ClassDeclarationImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassDeclarationImpl) Metamodel(org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel) CallableConstructorDeclaration(ceylon.language.meta.declaration.CallableConstructorDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) ValueConstructorDeclaration(ceylon.language.meta.declaration.ValueConstructorDeclaration) NestableDeclaration(ceylon.language.meta.declaration.NestableDeclaration) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) AnnotatedDeclaration(ceylon.language.meta.declaration.AnnotatedDeclaration) Reference(org.eclipse.ceylon.model.typechecker.model.Reference) ObjectArrayIterable(org.eclipse.ceylon.compiler.java.language.ObjectArrayIterable) Functional(org.eclipse.ceylon.model.typechecker.model.Functional) TypeDescriptor(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor) ValueConstructorDeclarationImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ValueConstructorDeclarationImpl)

Example 14 with TypeDescriptor

use of org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor in project ceylon by eclipse.

the class ClassDeclarationImpl method staticClassApply.

@SuppressWarnings("unchecked")
@Override
@TypeInfo("ceylon.language.meta.model::Class<Type,Arguments>")
@TypeParameters({ @TypeParameter("Type"), @TypeParameter(value = "Arguments", satisfies = "ceylon.language::Sequential<ceylon.language::Anything>") })
public <Type, Arguments extends Sequential<? extends Object>> ceylon.language.meta.model.Class<Type, Arguments> staticClassApply(@Ignore TypeDescriptor $reifiedType, @Ignore TypeDescriptor $reifiedArguments, @Name("containerType") @TypeInfo("ceylon.language.meta.model::Type<ceylon.language::Anything>") ceylon.language.meta.model.Type<? extends Object> containerType, @Name("typeArguments") @TypeInfo("ceylon.language::Sequential<ceylon.language.meta.model::Type<ceylon.language::Anything>>") @Sequenced Sequential<? extends ceylon.language.meta.model.Type<?>> typeArguments) {
    if (!getStatic()) {
        throw new ceylon.language.meta.model.TypeApplicationException("Cannot staticClassApply a non-static declaration: use " + (getToplevel() ? "apply" : "memberClassApply"));
    }
    List<org.eclipse.ceylon.model.typechecker.model.Type> producedTypes = Metamodel.getProducedTypes(typeArguments);
    if (getStatic()) {
        producedTypes.addAll(0, Metamodel.getModel(containerType).getTypeArgumentList());
    }
    Metamodel.checkTypeArguments(null, declaration, producedTypes);
    org.eclipse.ceylon.model.typechecker.model.Type ct = Metamodel.getProducedType(Metamodel.getTypeDescriptor(containerType));
    org.eclipse.ceylon.model.typechecker.model.Reference appliedType = declaration.appliedReference(ct, producedTypes);
    // MemberClassImpl<Object, Type, Arguments> ret = (MemberClassImpl<Object, Type, Arguments>) Metamodel.getAppliedMetamodel(appliedType.getType());
    // Metamodel.checkReifiedTypeArgument("staticClassApply", "Class<$1,$2>", Variance.OUT, appliedType.getType(), $reifiedType,
    // Variance.IN, Metamodel.getProducedType(ret.$reifiedArguments), $reifiedArguments);
    // return (ClassImpl)ret.$call$();
    TypeDescriptor reifiedArguments;
    if (!declaration.isAnonymous())
        reifiedArguments = Metamodel.getTypeDescriptorForArguments(declaration.getUnit(), (Functional) declaration, appliedType.getType());
    else
        reifiedArguments = TypeDescriptor.NothingType;
    TypeDescriptor reifiedType = Metamodel.getTypeDescriptorForProducedType(appliedType.getType());
    return new org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.ClassImpl(reifiedType, reifiedArguments, appliedType.getType(), null, null);
}
Also used : MemberClassImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.MemberClassImpl) ClassImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.ClassImpl) TypeDescriptor(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor) Metamodel(org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel) TypeParameters(org.eclipse.ceylon.compiler.java.metadata.TypeParameters) TypeInfo(org.eclipse.ceylon.compiler.java.metadata.TypeInfo)

Example 15 with TypeDescriptor

use of org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor in project ceylon by eclipse.

the class ClassDeclarationImpl method memberClassApply.

@SuppressWarnings({ "unchecked", "rawtypes" })
@TypeInfo("ceylon.language.meta.model::MemberClass<Container,Type,Arguments>")
@TypeParameters({ @TypeParameter("Container"), @TypeParameter("Type"), @TypeParameter(value = "Arguments", satisfies = "ceylon.language::Sequential<ceylon.language::Anything>") })
@Override
public <Container, Type, Arguments extends Sequential<? extends Object>> ceylon.language.meta.model.MemberClass<Container, Type, Arguments> memberClassApply(@Ignore TypeDescriptor $reifiedContainer, @Ignore TypeDescriptor $reifiedType, @Ignore TypeDescriptor $reifiedArguments, @Name("containerType") ceylon.language.meta.model.Type<? extends Object> containerType, @Name("typeArguments") @Sequenced Sequential<? extends ceylon.language.meta.model.Type<?>> typeArguments) {
    if (getToplevel())
        throw new ceylon.language.meta.model.TypeApplicationException("Cannot apply a toplevel declaration to a container type: use apply");
    ceylon.language.meta.model.MemberClass<Container, Type, Arguments> member = (ceylon.language.meta.model.MemberClass) getAppliedClassOrInterface(null, null, typeArguments, containerType);
    // This is all very ugly but we're trying to make it cheaper and friendlier than just checking the full type and showing
    // implementation types to the user, such as AppliedMemberClass
    TypeDescriptor actualReifiedContainer = ((MemberClassImpl) member).$reifiedContainer;
    TypeDescriptor actualReifiedArguments = ((MemberClassImpl) member).$reifiedArguments;
    org.eclipse.ceylon.model.typechecker.model.Type actualType = Metamodel.getModel((ceylon.language.meta.model.Type<?>) member);
    Metamodel.checkReifiedTypeArgument("memberApply", "Member<$1,Class<$2,$3>>&Class<$2,$3>", Variance.IN, Metamodel.getProducedType(actualReifiedContainer), $reifiedContainer, Variance.OUT, actualType, $reifiedType, Variance.IN, Metamodel.getProducedType(actualReifiedArguments), $reifiedArguments);
    return member;
}
Also used : MemberClassImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.MemberClassImpl) TypeDescriptor(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor) Metamodel(org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel) TypeParameters(org.eclipse.ceylon.compiler.java.metadata.TypeParameters) TypeInfo(org.eclipse.ceylon.compiler.java.metadata.TypeInfo)

Aggregations

TypeDescriptor (org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor)46 Metamodel (org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel)23 TypeInfo (org.eclipse.ceylon.compiler.java.metadata.TypeInfo)18 TypeParameters (org.eclipse.ceylon.compiler.java.metadata.TypeParameters)17 Sequential (ceylon.language.Sequential)13 ReifiedType (org.eclipse.ceylon.compiler.java.runtime.model.ReifiedType)12 Type (org.eclipse.ceylon.model.typechecker.model.Type)11 OpenType (ceylon.language.meta.declaration.OpenType)9 ArrayList (java.util.ArrayList)9 ObjectArrayIterable (org.eclipse.ceylon.compiler.java.language.ObjectArrayIterable)9 MemberClassImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.MemberClassImpl)7 ClassDeclaration (ceylon.language.meta.declaration.ClassDeclaration)5 Functional (org.eclipse.ceylon.model.typechecker.model.Functional)5 AssertionError (ceylon.language.AssertionError)4 ClassDeclarationImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassDeclarationImpl)4 ClassImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.ClassImpl)4 ReflectionClass (org.eclipse.ceylon.model.loader.impl.reflect.mirror.ReflectionClass)4 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)4 Metamodel.getTypeDescriptor (org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel.getTypeDescriptor)3 LazyClass (org.eclipse.ceylon.model.loader.model.LazyClass)3