Search in sources :

Example 1 with ValueDeclaration

use of ceylon.language.meta.declaration.ValueDeclaration in project ceylon by eclipse.

the class ClassOrInterfaceImpl method getCaseValues.

@Override
@TypeInfo("ceylon.language::Sequential<Type>")
public ceylon.language.Sequential<? extends Type> getCaseValues() {
    if (!((ClassOrInterface) declaration.declaration).isAbstract()) {
        // optimization: a concrete class cannot have cases
        return (Sequential) empty_.get_();
    }
    Sequential<? extends ceylon.language.meta.declaration.OpenType> caseTypeDeclarations = getDeclaration().getCaseTypes();
    Iterator<? extends ceylon.language.meta.declaration.OpenType> iterator = caseTypeDeclarations.iterator();
    Object it;
    Array<Type> ret = new Array<Type>($reifiedType, (int) caseTypeDeclarations.getSize(), (Type) null);
    int count = 0;
    while ((it = iterator.next()) != finished_.get_()) {
        if (it instanceof ceylon.language.meta.declaration.OpenClassType == false)
            continue;
        ceylon.language.meta.declaration.OpenClassType caseClassType = (ceylon.language.meta.declaration.OpenClassType) it;
        ceylon.language.meta.declaration.ClassDeclaration caseClass = caseClassType.getDeclaration();
        if (!caseClass.getAnonymous())
            continue;
        Type value = null;
        Object container = caseClass.getContainer();
        while (true) {
            if (container instanceof ceylon.language.meta.declaration.Package) {
                ValueDeclaration valueDeclaration = ((ceylon.language.meta.declaration.Package) container).getValue(caseClass.getName());
                ceylon.language.meta.model.Value<? extends Type, ? super Object> valueModel = valueDeclaration.<Type, Object>apply($reifiedType, TypeDescriptor.NothingType);
                value = valueModel.get();
                break;
            } else {
                if (container instanceof ClassOrInterfaceDeclaration) {
                    ValueDeclaration valueDeclaration = ((ClassOrInterfaceDeclaration) container).getMemberDeclaration(ValueDeclaration.$TypeDescriptor$, caseClass.getName());
                    Attribute a = valueDeclaration.memberApply($reifiedType, $reifiedType, Nothing.NothingType, this);
                    value = (Type) a.bind(null).get();
                }
                // other nestable decls can't contain members, so keep looking up scopes
                container = ((NestableDeclaration) container).getContainer();
            }
            if (value != null) {
                break;
            }
        }
        if (value == null && !producedType.isNull()) {
            throw new AssertionError("case " + caseClassType + " of " + this + " not found");
        }
        ret.set(count++, value);
    }
    return ret.take(count).sequence();
}
Also used : ClassOrInterfaceDeclaration(ceylon.language.meta.declaration.ClassOrInterfaceDeclaration) Attribute(ceylon.language.meta.model.Attribute) ValueDeclaration(ceylon.language.meta.declaration.ValueDeclaration) ClassDeclaration(ceylon.language.meta.declaration.ClassDeclaration) Array(ceylon.language.Array) ReifiedType(org.eclipse.ceylon.compiler.java.runtime.model.ReifiedType) Sequential(ceylon.language.Sequential) AssertionError(ceylon.language.AssertionError) Metamodel(org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel) TypeInfo(org.eclipse.ceylon.compiler.java.metadata.TypeInfo)

Example 2 with ValueDeclaration

use of ceylon.language.meta.declaration.ValueDeclaration in project ceylon by eclipse.

the class ClassOrInterfaceImpl method getAttributes.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
@TypeParameters({ @TypeParameter(value = "Container"), @TypeParameter(value = "Get"), @TypeParameter(value = "Set") })
@TypeInfo("ceylon.language::Sequential<ceylon.language.meta.model::Attribute<Container,Get,Set>>")
public <Container, Get, Set> ceylon.language.Sequential<? extends ceylon.language.meta.model.Attribute<? super Container, ? extends Get, ? super Set>> getAttributes(@Ignore TypeDescriptor $reifiedContainer, @Ignore TypeDescriptor $reifiedGet, @Ignore TypeDescriptor $reifiedSet, @Sequenced ceylon.language.Sequential<? extends ceylon.language.meta.model.Type<? extends java.lang.annotation.Annotation>> annotations) {
    checkInit();
    Sequential<? extends ValueDeclaration> declaredDeclarations = declaration.<ValueDeclaration>memberDeclarations(ValueDeclaration.$TypeDescriptor$);
    if (declaredDeclarations.getEmpty())
        return (ceylon.language.Sequential) empty_.get_();
    org.eclipse.ceylon.model.typechecker.model.Type reifiedContainer = Metamodel.getProducedType($reifiedContainer);
    org.eclipse.ceylon.model.typechecker.model.Type reifiedGet = Metamodel.getProducedType($reifiedGet);
    org.eclipse.ceylon.model.typechecker.model.Type reifiedSet = Metamodel.getProducedType($reifiedSet);
    Iterator<?> iterator = declaredDeclarations.iterator();
    Object it;
    TypeDescriptor[] annotationTypeDescriptors = Metamodel.getTypeDescriptors(annotations);
    TypeDescriptor reifiedKind = TypeDescriptor.klass(ceylon.language.meta.model.Attribute.class, $reifiedContainer, $reifiedGet, $reifiedSet);
    ArrayList<ceylon.language.meta.model.Attribute<? super Container, ? extends Get, ? super Set>> members = new ArrayList<ceylon.language.meta.model.Attribute<? super Container, ? extends Get, ? super Set>>((int) declaredDeclarations.getSize());
    while ((it = iterator.next()) != finished_.get_()) {
        ValueDeclarationImpl decl = (ValueDeclarationImpl) it;
        MemberLookup<ValueDeclarationImpl, Container> lookup = lookupMember(ValueDeclarationImpl.$TypeDescriptor$, $reifiedContainer, reifiedContainer, decl);
        if (lookup == null)
            continue;
        // ATM this is an AND WRT annotation types: all must be present
        if (!Metamodel.hasAllAnnotations(lookup.declaration, annotationTypeDescriptors))
            continue;
        addAttributeIfCompatible($reifiedContainer, $reifiedGet, $reifiedSet, members, lookup.declaration, lookup.qualifyingType, lookup.containerMetamodel, reifiedGet, reifiedSet);
    }
    Attribute[] array = members.toArray(new ceylon.language.meta.model.Attribute[members.size()]);
    ObjectArrayIterable<Attribute> iterable = new ObjectArrayIterable<ceylon.language.meta.model.Attribute>(reifiedKind, array);
    return (ceylon.language.Sequential) iterable.sequence();
}
Also used : ValueDeclarationImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ValueDeclarationImpl) Attribute(ceylon.language.meta.model.Attribute) ValueDeclaration(ceylon.language.meta.declaration.ValueDeclaration) ArrayList(java.util.ArrayList) ObjectArrayIterable(org.eclipse.ceylon.compiler.java.language.ObjectArrayIterable) TypeDescriptor(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor) Sequential(ceylon.language.Sequential) 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 3 with ValueDeclaration

use of ceylon.language.meta.declaration.ValueDeclaration in project ceylon by eclipse.

the class PartialImpl method initializeTuple.

protected <Id> void initializeTuple(TypeDescriptor $reified$Id, DeserializationContextImpl<Id> context, ceylon.language.Tuple<?, ?, ?> instance) {
    NativeMap<ReachableReference, Id> state = (NativeMap<ReachableReference, Id>) getState();
    ValueDeclaration firstAttribute = Util.assertExists((ValueDeclaration) ((ClassDeclaration) Metamodel.getOrCreateMetamodel(Tuple.class)).getMemberDeclaration(ValueDeclaration.$TypeDescriptor$, "first"));
    MemberImpl firstMember = new MemberImpl(firstAttribute);
    java.lang.Object first = getReferredInstance(context, state, firstMember);
    ValueDeclaration restAttribute = Util.assertExists((ValueDeclaration) ((ClassDeclaration) Metamodel.getOrCreateMetamodel(Tuple.class)).getMemberDeclaration(ValueDeclaration.$TypeDescriptor$, "rest"));
    MemberImpl restMember = new MemberImpl(restAttribute);
    Id restId = state.get(restMember);
    java.lang.Object referredRest = context.leakInstance(restId);
    if (referredRest instanceof Partial && !((PartialImpl) referredRest).getInitialized()) {
        // Safe because tuples are immutable => no cycles
        ((PartialImpl) referredRest).initialize($reified$Id, context);
    }
    java.lang.Object rest = getReferredInstance(context, state, restMember);
    ((Tuple<?, ?, ?>) instance).$completeInit$(first, rest);
    // now check compatibility (do this after initialization
    // because Tuple$getType$ requires the tuple is initialized!
    Type firstMemberType = Metamodel.getModuleManager().getCachedType(getClassTypeDescriptor().getTypeArgument(1));
    Type firstInstanceType = Metamodel.getModuleManager().getCachedType(Metamodel.getTypeDescriptor(first));
    if (!firstInstanceType.isSubtypeOf(firstMemberType)) {
        throw notAssignable(firstMember, firstMemberType, firstInstanceType);
    }
    Type restInstanceType = Metamodel.getModuleManager().getCachedType(Metamodel.getTypeDescriptor(rest));
    Type restMemberType = Metamodel.getModuleManager().getCachedType(getClassTypeDescriptor().getTypeArgument(2));
    if (!restInstanceType.isSubtypeOf(restMemberType)) {
        throw notAssignable(restMember, restMemberType, restInstanceType);
    }
}
Also used : MemberImpl(ceylon.language.impl.MemberImpl) ValueDeclaration(ceylon.language.meta.declaration.ValueDeclaration) Type(org.eclipse.ceylon.model.typechecker.model.Type) ReifiedType(org.eclipse.ceylon.compiler.java.runtime.model.ReifiedType) ClassDeclaration(ceylon.language.meta.declaration.ClassDeclaration) Tuple(ceylon.language.Tuple)

Example 4 with ValueDeclaration

use of ceylon.language.meta.declaration.ValueDeclaration in project ceylon by eclipse.

the class ClassOrInterfaceImpl method getDeclaredAttributes.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
@TypeParameters({ @TypeParameter(value = "Container"), @TypeParameter(value = "Get"), @TypeParameter(value = "Set") })
@TypeInfo("ceylon.language::Sequential<ceylon.language.meta.model::Attribute<Container,Get,Set>>")
public <Container, Get, Set> ceylon.language.Sequential<? extends ceylon.language.meta.model.Attribute<? super Container, ? extends Get, ? super Set>> getDeclaredAttributes(@Ignore TypeDescriptor $reifiedContainer, @Ignore TypeDescriptor $reifiedGet, @Ignore TypeDescriptor $reifiedSet, @Sequenced ceylon.language.Sequential<? extends ceylon.language.meta.model.Type<? extends java.lang.annotation.Annotation>> annotations) {
    checkInit();
    // check the container type first
    org.eclipse.ceylon.model.typechecker.model.Type reifiedContainer = Metamodel.getProducedType($reifiedContainer);
    if (!reifiedContainer.isSubtypeOf(this.producedType))
        return (ceylon.language.Sequential) empty_.get_();
    Sequential<? extends ValueDeclaration> declaredDeclarations = declaration.<ValueDeclaration>declaredMemberDeclarations(ValueDeclaration.$TypeDescriptor$);
    if (declaredDeclarations.getEmpty())
        return (ceylon.language.Sequential) empty_.get_();
    org.eclipse.ceylon.model.typechecker.model.Type reifiedGet = Metamodel.getProducedType($reifiedGet);
    org.eclipse.ceylon.model.typechecker.model.Type reifiedSet = Metamodel.getProducedType($reifiedSet);
    Iterator<?> iterator = declaredDeclarations.iterator();
    Object it;
    TypeDescriptor[] annotationTypeDescriptors = Metamodel.getTypeDescriptors(annotations);
    TypeDescriptor reifiedKind = TypeDescriptor.klass(ceylon.language.meta.model.Attribute.class, $reifiedType, $reifiedGet, $reifiedSet);
    ArrayList<ceylon.language.meta.model.Attribute<? super Container, ? extends Get, ? super Set>> members = new ArrayList<ceylon.language.meta.model.Attribute<? super Container, ? extends Get, ? super Set>>((int) declaredDeclarations.getSize());
    while ((it = iterator.next()) != finished_.get_()) {
        ValueDeclarationImpl decl = (ValueDeclarationImpl) it;
        // ATM this is an AND WRT annotation types: all must be present
        if (!Metamodel.hasAllAnnotations(decl, annotationTypeDescriptors))
            continue;
        addAttributeIfCompatible($reifiedContainer, $reifiedGet, $reifiedSet, members, decl, this.producedType, (ceylon.language.meta.model.Type<Container>) this, reifiedGet, reifiedSet);
    }
    Attribute[] array = members.toArray(new ceylon.language.meta.model.Attribute[0]);
    ObjectArrayIterable<Attribute> iterable = new ObjectArrayIterable<ceylon.language.meta.model.Attribute>(reifiedKind, array);
    return (ceylon.language.Sequential) iterable.sequence();
}
Also used : ValueDeclarationImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ValueDeclarationImpl) Attribute(ceylon.language.meta.model.Attribute) ValueDeclaration(ceylon.language.meta.declaration.ValueDeclaration) ArrayList(java.util.ArrayList) ObjectArrayIterable(org.eclipse.ceylon.compiler.java.language.ObjectArrayIterable) TypeDescriptor(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor) Sequential(ceylon.language.Sequential) 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

ValueDeclaration (ceylon.language.meta.declaration.ValueDeclaration)4 Sequential (ceylon.language.Sequential)3 Attribute (ceylon.language.meta.model.Attribute)3 TypeInfo (org.eclipse.ceylon.compiler.java.metadata.TypeInfo)3 Metamodel (org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel)3 ClassDeclaration (ceylon.language.meta.declaration.ClassDeclaration)2 ArrayList (java.util.ArrayList)2 ObjectArrayIterable (org.eclipse.ceylon.compiler.java.language.ObjectArrayIterable)2 TypeParameters (org.eclipse.ceylon.compiler.java.metadata.TypeParameters)2 ValueDeclarationImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ValueDeclarationImpl)2 ReifiedType (org.eclipse.ceylon.compiler.java.runtime.model.ReifiedType)2 TypeDescriptor (org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor)2 Array (ceylon.language.Array)1 AssertionError (ceylon.language.AssertionError)1 Tuple (ceylon.language.Tuple)1 MemberImpl (ceylon.language.impl.MemberImpl)1 ClassOrInterfaceDeclaration (ceylon.language.meta.declaration.ClassOrInterfaceDeclaration)1 Type (org.eclipse.ceylon.model.typechecker.model.Type)1