Search in sources :

Example 16 with TypeDescriptor

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

the class ClassOrInterfaceDeclarationImpl method memberApply.

@SuppressWarnings("rawtypes")
@TypeInfo("ceylon.language.meta.model::Member<Container,ceylon.language.meta.model::ClassOrInterface<Type>>&ceylon.language.meta.model::ClassOrInterface<Type>")
@TypeParameters({ @TypeParameter("Container"), @TypeParameter("Type") })
@Override
public <Container, Type extends Object> java.lang.Object memberApply(@Ignore TypeDescriptor $reifiedContainer, @Ignore TypeDescriptor $reifiedType, @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.Member<? extends Container, ceylon.language.meta.model.ClassOrInterface<?>> member = 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;
    if (member instanceof MemberClassImpl)
        actualReifiedContainer = ((MemberClassImpl) member).$reifiedContainer;
    else
        actualReifiedContainer = ((MemberInterfaceImpl) member).$reifiedContainer;
    org.eclipse.ceylon.model.typechecker.model.Type actualType = Metamodel.getModel((ceylon.language.meta.model.Type<?>) member);
    Metamodel.checkReifiedTypeArgument("memberApply", "Member<$1,ClassOrInterface<$2>>&ClassOrInterface<$2>", Variance.IN, Metamodel.getProducedType(actualReifiedContainer), $reifiedContainer, Variance.OUT, actualType, $reifiedType);
    return member;
}
Also used : ClassOrInterface(ceylon.language.meta.model.ClassOrInterface) TypeDescriptor(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor) MemberInterfaceImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.MemberInterfaceImpl) Type(org.eclipse.ceylon.model.typechecker.model.Type) Metamodel(org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel) MemberClassImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.MemberClassImpl) TypeParameters(org.eclipse.ceylon.compiler.java.metadata.TypeParameters) TypeInfo(org.eclipse.ceylon.compiler.java.metadata.TypeInfo)

Example 17 with TypeDescriptor

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

the class FunctionDeclarationImpl method apply.

@Override
@TypeInfo("ceylon.language.meta.model::Function<Return,Arguments>")
@TypeParameters({ @TypeParameter("Return"), @TypeParameter(value = "Arguments", satisfies = "ceylon.language::Sequential<ceylon.language::Anything>") })
public <Return extends Object, Arguments extends Sequential<? extends Object>> ceylon.language.meta.model.Function<Return, Arguments> apply(@Ignore TypeDescriptor $reifiedReturn, @Ignore TypeDescriptor $reifiedArguments, @Name("typeArguments") @TypeInfo("ceylon.language::Sequential<ceylon.language.meta.model::Type<ceylon.language::Anything>>") @Sequenced Sequential<? extends ceylon.language.meta.model.Type<?>> typeArguments) {
    if (!getToplevel()) {
        String msg;
        if (getStatic()) {
            msg = "Cannot apply a static declaration with no container type: use staticApply";
        } else {
            msg = "Cannot apply a member declaration with no container type: use memberApply";
        }
        throw new ceylon.language.meta.model.TypeApplicationException(msg);
    }
    List<org.eclipse.ceylon.model.typechecker.model.Type> producedTypes = Metamodel.getProducedTypes(typeArguments);
    Metamodel.checkTypeArguments(null, declaration, producedTypes);
    org.eclipse.ceylon.model.typechecker.model.Reference appliedFunction = declaration.appliedReference(null, producedTypes);
    TypeDescriptor reifiedType = Metamodel.getTypeDescriptorForFunction(appliedFunction);
    TypeDescriptor reifiedArguments = Metamodel.getTypeDescriptorForArguments(declaration.getUnit(), (Functional) declaration, appliedFunction);
    Metamodel.checkReifiedTypeArgument("apply", "Function<$1,$2>", Variance.OUT, declaration.getUnit().getCallableReturnType(appliedFunction.getFullType()), $reifiedReturn, Variance.IN, Metamodel.getProducedTypeForArguments(declaration.getUnit(), (Functional) declaration, appliedFunction), $reifiedArguments);
    return new FunctionImpl<Return, Arguments>(reifiedType, reifiedArguments, appliedFunction, this, null, null);
}
Also used : Functional(org.eclipse.ceylon.model.typechecker.model.Functional) OpenType(ceylon.language.meta.declaration.OpenType) TypeDescriptor(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor) FunctionImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.FunctionImpl) 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 18 with TypeDescriptor

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

the class FunctionDeclarationImpl method staticApply.

@Override
public <Return extends Object, Arguments extends Sequential<? extends Object>> ceylon.language.meta.model.Function<Return, Arguments> staticApply(@Ignore TypeDescriptor $reifiedReturn, @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 (!getStatic())
        throw new ceylon.language.meta.model.TypeApplicationException("Cannot apply a " + (getToplevel() ? "toplevel" : "member") + " declaration to a container type: use " + (getToplevel() ? "apply" : "memberApply"));
    List<org.eclipse.ceylon.model.typechecker.model.Type> producedTypes = Metamodel.getProducedTypes(typeArguments);
    Metamodel.checkTypeArguments(null, declaration, producedTypes);
    org.eclipse.ceylon.model.typechecker.model.Reference appliedFunction = declaration.appliedReference(((ClassOrInterfaceImpl<?>) containerType).producedType, producedTypes);
    TypeDescriptor reifiedType = Metamodel.getTypeDescriptorForFunction(appliedFunction);
    TypeDescriptor reifiedArguments = Metamodel.getTypeDescriptorForArguments(declaration.getUnit(), (Functional) declaration, appliedFunction);
    if (getStatic()) {
        producedTypes.addAll(0, Metamodel.getModel(containerType).getTypeArgumentList());
    }
    Metamodel.checkReifiedTypeArgument("apply", "Function<$1,$2>", Variance.OUT, declaration.getUnit().getCallableReturnType(appliedFunction.getFullType()), $reifiedReturn, Variance.IN, Metamodel.getProducedTypeForArguments(declaration.getUnit(), (Functional) declaration, appliedFunction), $reifiedArguments);
    return new FunctionImpl<Return, Arguments>(reifiedType, reifiedArguments, appliedFunction, this, containerType, null);
}
Also used : Functional(org.eclipse.ceylon.model.typechecker.model.Functional) OpenType(ceylon.language.meta.declaration.OpenType) TypeDescriptor(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor) FunctionImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.FunctionImpl) Metamodel(org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel)

Example 19 with TypeDescriptor

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

the class PartialImpl method initializeObject.

protected <Id> void initializeObject(TypeDescriptor $reified$Id, DeserializationContextImpl<Id> context, Serializable instance) {
    NativeMap<ReachableReference, Id> state = (NativeMap<ReachableReference, Id>) getState();
    // TODO If it were a map of java.lang.String we'd avoid pointless extra boxing
    java.util.Collection<ReachableReference> reachables = instance.$references$();
    int numLate = 0;
    for (ReachableReference r : reachables) {
        if (r instanceof Member && ((Member) r).getAttribute().getLate()) {
            numLate++;
        } else if (r instanceof Outer) {
            numLate++;
        }
    }
    if (state.getSize() < reachables.size() - numLate) {
        HashSet<ReachableReference> missingNames = new HashSet<ReachableReference>();
        java.util.Iterator<ReachableReference> it = reachables.iterator();
        while (it.hasNext()) {
            missingNames.add(it.next());
        }
        ceylon.language.Iterator<? extends ReachableReference> it2 = state.getKeys().iterator();
        Object next;
        while (((next = it2.next()) instanceof ReachableReference)) {
            missingNames.remove(next);
        }
        throw insufficiantState(missingNames);
    }
    for (ReachableReference reference : reachables) {
        if (reference instanceof Member) {
            Member member = (Member) reference;
            if (member.getAttribute().getLate() && !state.contains(member) || state.get(member) == uninitializedLateValue_.get_()) {
                continue;
            }
            TypeDescriptor.Class classTypeDescriptor = getClassTypeDescriptor();
            Entry<TypeDescriptor.Class, String> cacheKey = new Entry<TypeDescriptor.Class, String>(TypeDescriptor.klass(TypeDescriptor.Class.class), String.$TypeDescriptor$, classTypeDescriptor, String.instance(member.getAttribute().getQualifiedName()));
            Type memberType = (Type) context.getMemberTypeCache().get(cacheKey);
            if (memberType == null) {
                Type pt = Metamodel.getModuleManager().getCachedType(classTypeDescriptor);
                while (!pt.getDeclaration().getQualifiedNameString().equals(((ClassDeclaration) member.getAttribute().getContainer()).getQualifiedName())) {
                    pt = pt.getExtendedType();
                }
                FunctionOrValue attributeDeclaration = (FunctionOrValue) ((TypeDeclaration) pt.getDeclaration()).getMember(member.getAttribute().getName(), null, false);
                TypedReference attributeType = pt.getTypedMember(attributeDeclaration, Collections.<Type>emptyList(), true);
                memberType = attributeType.getType();
                context.getMemberTypeCache().put(cacheKey, memberType);
            }
            Object referredInstance = getReferredInstance(context, state, member);
            if (referredInstance instanceof Tuple) {
                // Because tuples are special wrt reified types...
                Id referredId = state.get(member);
                Object r = context.leakInstance(referredId);
                if (r instanceof PartialImpl) {
                    ((PartialImpl) r).initialize($reified$Id, context);
                }
            }
            Type instanceType = Metamodel.getModuleManager().getCachedType(Metamodel.getTypeDescriptor(referredInstance));
            if (!instanceType.isSubtypeOf(memberType)) {
                throw notAssignable(member, memberType, instanceType);
            }
            instance.$set$(member, referredInstance);
        // the JVM will check the assignability, but we need to
        // check assignability at the ceylon level, so we need to know
        // / type of the attribute an the type that we're assigning.
        // XXX this check is really expensive!
        // we should cache the attribute type on the context
        // when can we avoid this check.
        // XXX we can cache MethodHandle setters on the context!
        } else if (reference instanceof Outer) {
            // instantiating member classes
            continue;
        } else {
            throw new AssertionError("unexpected ReachableReference " + reference);
        }
    }
}
Also used : TypedReference(org.eclipse.ceylon.model.typechecker.model.TypedReference) String(ceylon.language.String) Entry(ceylon.language.Entry) AssertionError(ceylon.language.AssertionError) HashSet(java.util.HashSet) Type(org.eclipse.ceylon.model.typechecker.model.Type) ReifiedType(org.eclipse.ceylon.compiler.java.runtime.model.ReifiedType) ClassDeclaration(ceylon.language.meta.declaration.ClassDeclaration) TypeDescriptor(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) Tuple(ceylon.language.Tuple)

Example 20 with TypeDescriptor

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

the class Predicates method isDeclarationOfKind.

/**
 * Returns a Predicate for Declarations being of the given kind
 * (Class, Interface, Function, Value etc)
 * @param kind A TypeDescriptor for the sought declaration kind.
 */
public static Predicate<Declaration> isDeclarationOfKind(TypeDescriptor kind) {
    if (kind instanceof TypeDescriptor.Class) {
        Class<?> declarationClass = (Class<?>) ((TypeDescriptor.Class) kind).getKlass();
        if (declarationClass == ceylon.language.meta.declaration.ValueDeclaration.class) {
            return DECLARATION_IS_VALUE;
        } else if (declarationClass == ceylon.language.meta.declaration.FunctionDeclaration.class) {
            return DECLARATION_IS_FUNCTION;
        } else if (declarationClass == ceylon.language.meta.declaration.FunctionOrValueDeclaration.class) {
            return DECLARATION_IS_FUNCTION_OR_VALUE;
        } else if (declarationClass == ceylon.language.meta.declaration.ClassDeclaration.class) {
            return DECLARATION_IS_CLASS;
        } else if (declarationClass == ceylon.language.meta.declaration.ClassWithInitializerDeclaration.class) {
            return DECLARATION_IS_CLASS_WITH_INIT;
        } else if (declarationClass == ceylon.language.meta.declaration.ClassWithConstructorsDeclaration.class) {
            return DECLARATION_IS_CLASS_WITH_CTORS;
        } else if (declarationClass == ceylon.language.meta.declaration.InterfaceDeclaration.class) {
            return DECLARATION_IS_INTERFACE;
        } else if (declarationClass == ceylon.language.meta.declaration.ClassOrInterfaceDeclaration.class) {
            return DECLARATION_IS_CLASS_OR_INTERFACE;
        } else if (declarationClass == ceylon.language.meta.declaration.AliasDeclaration.class) {
            return DECLARATION_IS_ALIAS;
        } else if (declarationClass == ceylon.language.meta.declaration.ConstructorDeclaration.class) {
            return DECLARATION_IS_CONSTRUCTOR;
        } else if (declarationClass == ceylon.language.meta.declaration.CallableConstructorDeclaration.class) {
            return DECLARATION_IS_CALLABLE_CONSTRUCTOR;
        } else if (declarationClass == ceylon.language.meta.declaration.ValueConstructorDeclaration.class) {
            return DECLARATION_IS_VALUE_CONSTRUCTOR;
        } else if (declarationClass == ceylon.language.meta.declaration.NestableDeclaration.class) {
            return true_();
        }
        throw new EnumeratedTypeError("Supposedly exhaustive switch was not exhaustive");
    } else if (kind instanceof TypeDescriptor.Union) {
        TypeDescriptor[] members = ((TypeDescriptor.Union) kind).getMembers();
        @SuppressWarnings("unchecked") Predicate<Declaration>[] preds = new Predicate[members.length];
        int ii = 0;
        for (TypeDescriptor member : members) {
            preds[ii++] = isDeclarationOfKind(member);
        }
        return or(preds);
    } else if (kind instanceof TypeDescriptor.Intersection) {
        TypeDescriptor[] members = ((TypeDescriptor.Intersection) kind).getMembers();
        @SuppressWarnings("unchecked") Predicate<Declaration>[] preds = new Predicate[members.length];
        int ii = 0;
        for (TypeDescriptor member : members) {
            preds[ii++] = isDeclarationOfKind(member);
        }
        return and(preds);
    } else if (kind == TypeDescriptor.NothingType) {
        return false_();
    }
    throw new EnumeratedTypeError("Supposedly exhaustive switch was not exhaustive");
}
Also used : TypeDescriptor(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor) EnumeratedTypeError(org.eclipse.ceylon.compiler.java.language.EnumeratedTypeError) AnnotatedDeclaration(ceylon.language.meta.declaration.AnnotatedDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration)

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