Search in sources :

Example 1 with Array

use of ceylon.language.Array 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 Array

use of ceylon.language.Array in project ceylon by eclipse.

the class Metamodel method namedApply.

public static <Return> Return namedApply(Callable<? extends Return> function, DefaultValueProvider defaultValueProvider, org.eclipse.ceylon.model.typechecker.model.Functional declaration, ceylon.language.Iterable<? extends ceylon.language.Entry<? extends ceylon.language.String, ? extends java.lang.Object>, ? extends java.lang.Object> arguments, List<Type> parameterProducedTypes) {
    // FIXME: throw for Java declarations
    java.util.Map<java.lang.String, java.lang.Object> argumentMap = collectArguments(arguments);
    java.util.List<Parameter> parameters = declaration.getFirstParameterList().getParameters();
    // store the values in an array
    Array<java.lang.Object> values = new Array<java.lang.Object>(Anything.$TypeDescriptor$, parameters.size(), (java.lang.Object) null);
    int parameterIndex = 0;
    for (Parameter parameter : parameters) {
        // get the parameter value and remove it so we can keep track of those we used
        java.lang.Object value;
        if (argumentMap.containsKey(parameter.getName())) {
            value = argumentMap.remove(parameter.getName());
            // we have a value: check the type
            Type argumentType = Metamodel.getProducedType(value);
            Type parameterType = parameterProducedTypes.get(parameterIndex);
            if (!argumentType.isSubtypeOf(parameterType))
                throw new ceylon.language.meta.model.IncompatibleTypeException("Invalid argument " + parameter.getName() + ", expected type " + parameterType + " but got " + argumentType);
        } else {
            // make sure it has a default value
            if (!parameter.isDefaulted())
                throw new InvocationException("Missing value for non-defaulted parameter " + parameter.getName());
            // we need to fetch the default value
            value = defaultValueProvider.getDefaultParameterValue(parameter, values, parameterIndex);
            argumentMap.remove(parameter.getName());
        }
        values.set(parameterIndex++, value);
    }
    // do we have extra unknown/unused parameters left?
    if (!argumentMap.isEmpty()) {
        for (String name : argumentMap.keySet()) {
            throw new InvocationException("No such parameter " + name);
        }
    }
    // FIXME: don't we need to spread any variadic param?
    // now do a regular invocation
    Sequential<? extends Object> argumentSequence = values.sequence();
    // we can trust any variadic or pseudo-variadic since we checked parameter by parameter (no spreading possible)
    return Util.apply(function, argumentSequence, null);
}
Also used : IncompatibleTypeException(ceylon.language.meta.model.IncompatibleTypeException) CharArray(org.eclipse.ceylon.compiler.java.language.CharArray) ShortArray(org.eclipse.ceylon.compiler.java.language.ShortArray) BooleanArray(org.eclipse.ceylon.compiler.java.language.BooleanArray) Array(ceylon.language.Array) LongArray(org.eclipse.ceylon.compiler.java.language.LongArray) FloatArray(org.eclipse.ceylon.compiler.java.language.FloatArray) ByteArray(org.eclipse.ceylon.compiler.java.language.ByteArray) ObjectArray(org.eclipse.ceylon.compiler.java.language.ObjectArray) DoubleArray(org.eclipse.ceylon.compiler.java.language.DoubleArray) IntArray(org.eclipse.ceylon.compiler.java.language.IntArray) ReifiedType(org.eclipse.ceylon.compiler.java.runtime.model.ReifiedType) NothingType(org.eclipse.ceylon.model.typechecker.model.NothingType) OpenClassOrInterfaceType(ceylon.language.meta.declaration.OpenClassOrInterfaceType) OpenType(ceylon.language.meta.declaration.OpenType) DeclarationType(org.eclipse.ceylon.model.loader.ModelLoader.DeclarationType) Type(org.eclipse.ceylon.model.typechecker.model.Type) UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) InvocationException(ceylon.language.meta.model.InvocationException) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) Metamodel(org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel)

Aggregations

Array (ceylon.language.Array)2 Metamodel (org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel)2 ReifiedType (org.eclipse.ceylon.compiler.java.runtime.model.ReifiedType)2 AssertionError (ceylon.language.AssertionError)1 Sequential (ceylon.language.Sequential)1 ClassDeclaration (ceylon.language.meta.declaration.ClassDeclaration)1 ClassOrInterfaceDeclaration (ceylon.language.meta.declaration.ClassOrInterfaceDeclaration)1 OpenClassOrInterfaceType (ceylon.language.meta.declaration.OpenClassOrInterfaceType)1 OpenType (ceylon.language.meta.declaration.OpenType)1 ValueDeclaration (ceylon.language.meta.declaration.ValueDeclaration)1 Attribute (ceylon.language.meta.model.Attribute)1 IncompatibleTypeException (ceylon.language.meta.model.IncompatibleTypeException)1 InvocationException (ceylon.language.meta.model.InvocationException)1 BooleanArray (org.eclipse.ceylon.compiler.java.language.BooleanArray)1 ByteArray (org.eclipse.ceylon.compiler.java.language.ByteArray)1 CharArray (org.eclipse.ceylon.compiler.java.language.CharArray)1 DoubleArray (org.eclipse.ceylon.compiler.java.language.DoubleArray)1 FloatArray (org.eclipse.ceylon.compiler.java.language.FloatArray)1 IntArray (org.eclipse.ceylon.compiler.java.language.IntArray)1 LongArray (org.eclipse.ceylon.compiler.java.language.LongArray)1