Search in sources :

Example 91 with Declaration

use of org.eclipse.ceylon.model.typechecker.model.Declaration in project ceylon by eclipse.

the class JsonPackage method parseTypeParameters.

/**
 * Creates a list of TypeParameter from a list of maps.
 * @param typeParams The list of maps to create the TypeParameters.
 * @param container The declaration which owns the resulting type parameters.
 * @param existing A list of type parameters declared in the parent scopes which can be referenced from
 * the ones that have to be parsed.
 */
private List<TypeParameter> parseTypeParameters(List<Map<String, Object>> typeParams, final Declaration container, List<TypeParameter> existing) {
    if (typeParams == null)
        return Collections.emptyList();
    // New array with existing parms to avoid modifying that one
    List<TypeParameter> allparms = new ArrayList<>((existing == null ? 0 : existing.size()) + typeParams.size());
    if (existing != null && !existing.isEmpty()) {
        allparms.addAll(existing);
    }
    List<TypeParameter> tparms = new ArrayList<>(typeParams.size());
    // First create the type parameters
    for (Map<String, Object> tp : typeParams) {
        final Declaration maybe;
        if (tp.get(KEY_METATYPE) instanceof TypeParameter) {
            maybe = (TypeParameter) tp.get(KEY_METATYPE);
        } else {
            maybe = container.getDirectMember((String) tp.get(KEY_NAME), null, false);
        }
        if (maybe instanceof TypeParameter) {
            // we already had it (from partial loading elsewhere)
            allparms.add((TypeParameter) maybe);
            tparms.add((TypeParameter) maybe);
            tp.put(KEY_METATYPE, maybe);
        } else {
            TypeParameter tparm = new TypeParameter();
            tparm.setUnit(container.getUnit());
            tparm.setDeclaration(container);
            container.getMembers().add(tparm);
            if (tp.containsKey(KEY_NAME)) {
                tparm.setName((String) tp.get(KEY_NAME));
            } else if (!tp.containsKey(KEY_TYPES)) {
                throw new IllegalArgumentException("Invalid type parameter map " + tp);
            }
            String variance = (String) tp.get(KEY_DS_VARIANCE);
            if ("out".equals(variance)) {
                tparm.setCovariant(true);
            } else if ("in".equals(variance)) {
                tparm.setContravariant(true);
            }
            if (container instanceof Scope) {
                Scope scope = (Scope) container;
                tparm.setContainer(scope);
                tparm.setScope(scope);
            }
            tparm.setDefaulted(tp.containsKey(KEY_DEFAULT));
            tparms.add(tparm);
            allparms.add(tparm);
            tp.put(KEY_METATYPE, tparm);
        }
    }
    if (container instanceof Generic) {
        ((Generic) container).setTypeParameters(tparms);
    }
    // Second, add defaults and heritage
    for (Map<String, Object> tp : typeParams) {
        TypeParameter tparm = (TypeParameter) tp.get(KEY_METATYPE);
        if (tparm.getExtendedType() == null) {
            if (tp.containsKey(KEY_PACKAGE)) {
                // Looks like this never happens but...
                Type subtype = getTypeFromJson(tp, container, allparms);
                tparm.setExtendedType(subtype);
            } else if (tp.containsKey(KEY_TYPES)) {
                if (!("u".equals(tp.get("comp")) || "i".equals(tp.get("comp")))) {
                    throw new IllegalArgumentException("Only union or intersection types are allowed as 'comp'");
                }
                Type subtype = getTypeFromJson(tp, container, allparms);
                tparm.setName(subtype.asString());
                tparm.setExtendedType(subtype);
            } else {
                tparm.setExtendedType(getTypeFromJson(voidclass, container, null));
            }
        }
        if (tparm.isDefaulted()) {
            @SuppressWarnings("unchecked") final Map<String, Object> deftype = (Map<String, Object>) tp.get(KEY_DEFAULT);
            tparm.setDefaultTypeArgument(getTypeFromJson(deftype, container, existing));
        }
        if (tp.containsKey(KEY_SATISFIES)) {
            @SuppressWarnings("unchecked") final List<Map<String, Object>> stypes = (List<Map<String, Object>>) tp.get(KEY_SATISFIES);
            tparm.setSatisfiedTypes(parseTypeList(stypes, allparms));
            tparm.setConstrained(true);
        } else if (tp.containsKey("of")) {
            @SuppressWarnings("unchecked") final List<Map<String, Object>> oftype = (List<Map<String, Object>>) tp.get("of");
            tparm.setCaseTypes(parseTypeList(oftype, allparms));
            tparm.setConstrained(true);
        }
    }
    return tparms;
}
Also used : TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) Generic(org.eclipse.ceylon.model.typechecker.model.Generic) ArrayList(java.util.ArrayList) IntersectionType(org.eclipse.ceylon.model.typechecker.model.IntersectionType) NothingType(org.eclipse.ceylon.model.typechecker.model.NothingType) UnionType(org.eclipse.ceylon.model.typechecker.model.UnionType) Type(org.eclipse.ceylon.model.typechecker.model.Type) UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) List(java.util.List) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) ArrayList(java.util.ArrayList) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) Map(java.util.Map) HashMap(java.util.HashMap)

Example 92 with Declaration

use of org.eclipse.ceylon.model.typechecker.model.Declaration in project ceylon by eclipse.

the class JsonPackage method loadNestedType.

/**
 * Load a nested type that hasn't been loaded yet
 */
private TypeDeclaration loadNestedType(final String fqn, List<TypeParameter> typeParams) {
    try {
        String[] path = fqn.split("\\.");
        @SuppressWarnings("unchecked") Map<String, Object> typeMap = (Map<String, Object>) model.get(path[0]);
        if (typeMap.get(KEY_METATYPE) instanceof TypeDeclaration == false) {
            load(path[0], typeParams);
        }
        TypeDeclaration td = (TypeDeclaration) typeMap.get(KEY_METATYPE);
        for (int i = 1; i < path.length; i++) {
            @SuppressWarnings("unchecked") Map<String, Object> subtypes = (Map<String, Object>) typeMap.get(KEY_INTERFACES);
            Map<String, Object> childMap = null;
            int type = 0;
            if (subtypes != null) {
                childMap = (Map<String, Object>) subtypes.get(path[i]);
                type = 1;
            }
            if (childMap == null) {
                subtypes = (Map<String, Object>) typeMap.get(KEY_CLASSES);
                if (subtypes != null) {
                    childMap = (Map<String, Object>) subtypes.get(path[i]);
                    type = 2;
                }
            }
            Declaration member = td.getDirectMember(path[i], null, false);
            TypeDeclaration child;
            if (member instanceof Value && ((Value) member).getTypeDeclaration() instanceof Constructor)
                child = ((Value) member).getTypeDeclaration().getExtendedType().getDeclaration();
            else
                child = (TypeDeclaration) member;
            if (child == null) {
                switch(type) {
                    case 1:
                        child = loadInterface(path[i], childMap, td, typeParams);
                        break;
                    case 2:
                        child = loadClass(path[i], childMap, td, typeParams);
                        break;
                }
            }
            td = child;
        }
        return td;
    } catch (RuntimeException x) {
        throw new RuntimeException("Failed to load inner type " + fqn + " in package " + getQualifiedNameString(), x);
    }
}
Also used : Constructor(org.eclipse.ceylon.model.typechecker.model.Constructor) Value(org.eclipse.ceylon.model.typechecker.model.Value) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) Map(java.util.Map) HashMap(java.util.HashMap) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)

Example 93 with Declaration

use of org.eclipse.ceylon.model.typechecker.model.Declaration in project ceylon by eclipse.

the class JsonPackage method refineMembers.

/**
 * Sets the refined declarations for the type's members.
 */
private void refineMembers(ClassOrInterface coi) {
    // fill refined declarations
    for (Declaration d : coi.getMembers()) {
        if (d.isActual()) {
            Declaration refined = coi.getRefinedMember(d.getName(), getSignature(d), isVariadic(d));
            if (refined == null)
                refined = d;
            d.setRefinedDeclaration(refined);
        }
        if (d instanceof ClassOrInterface) {
            refineMembers((ClassOrInterface) d);
        }
    }
}
Also used : ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration)

Example 94 with Declaration

use of org.eclipse.ceylon.model.typechecker.model.Declaration in project ceylon by eclipse.

the class TypeUtils method metamodelTypeNameOrList.

/**
 * Prints out an object with a type constructor under the property "t" and its type arguments under
 * the property "a", or a union/intersection type with "u" or "i" under property "t" and the list
 * of types that compose it in an array under the property "l", or a type parameter as a reference to
 * already existing params.
 * @param resolveTargsFromScope Indicates whether to resolve a type argument if it's within reach in the
 * node's scope. This is useful for parameters of JsCallables but must be disabled for metamodel functions.
 * @param node The node to use as starting point for resolution of other references.
 * @param pkg The package of the current declaration
 * @param pt The produced type for which a name must be output.
 * @param gen The generator to use for output.
 */
static void metamodelTypeNameOrList(final boolean resolveTargsFromScope, final Node node, final org.eclipse.ceylon.model.typechecker.model.Package pkg, Type pt, SiteVariance useSiteVariance, GenerateJsVisitor gen) {
    if (pt == null) {
        // In dynamic blocks we sometimes get a null producedType
        gen.out("'$U'");
        return;
    }
    if (!outputMetamodelTypeList(resolveTargsFromScope, node, pkg, pt, gen)) {
        TypeDeclaration type = pt.getDeclaration();
        if (pt.isTypeParameter()) {
            final TypeParameter tparm = (TypeParameter) type;
            final Declaration tpowner = tparm.getDeclaration();
            final boolean nodeIsDecl = node instanceof Tree.Declaration;
            boolean rtafs = tpowner instanceof TypeDeclaration == false && (nodeIsDecl ? ((Tree.Declaration) node).getDeclarationModel() != tpowner : true);
            if (rtafs && ModelUtil.contains((Scope) tpowner, node.getScope())) {
                // Attempt to resolve this to an argument if the scope allows for it
                if (tpowner instanceof TypeDeclaration) {
                    gen.out(gen.getNames().self((TypeDeclaration) tpowner), ".$$targs$$.", gen.getNames().typeParameterName(tparm));
                } else if (tpowner instanceof Function) {
                    gen.out(gen.getNames().typeArgsParamName((Function) tpowner), ".", gen.getNames().typeParameterName(tparm));
                }
            } else if (resolveTargsFromScope && tpowner instanceof TypeDeclaration && (nodeIsDecl ? ((Tree.Declaration) node).getDeclarationModel() == tpowner : true) && ModelUtil.contains((Scope) tpowner, node.getScope())) {
                typeNameOrList(node, tparm.getType(), gen, false);
            } else {
                gen.out("'", gen.getNames().typeParameterName(tparm), "'");
            }
        } else if (pt.isTypeAlias()) {
            outputQualifiedTypename(node, gen.isImported(pkg, type), pt, gen, false);
        } else {
            gen.out("{t:");
            // For constructors, output the type of the class
            final Type qt = type instanceof Constructor ? pt.getQualifyingType() : pt;
            outputQualifiedTypename(node, gen.isImported(pkg, type), qt, gen, false);
            // Type Parameters
            if (!pt.getTypeArguments().isEmpty()) {
                gen.out(",a:{");
                boolean first = true;
                for (Map.Entry<TypeParameter, Type> e : pt.getTypeArguments().entrySet()) {
                    if (first)
                        first = false;
                    else
                        gen.out(",");
                    gen.out(gen.getNames().typeParameterName(e.getKey()), ":");
                    metamodelTypeNameOrList(resolveTargsFromScope, node, pkg, e.getValue(), pt.getVarianceOverrides().get(e.getKey()), gen);
                }
                gen.out("}");
            }
            printSiteVariance(useSiteVariance, gen);
            gen.out("}");
        }
    }
}
Also used : Function(org.eclipse.ceylon.model.typechecker.model.Function) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) Type(org.eclipse.ceylon.model.typechecker.model.Type) UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) NothingType(org.eclipse.ceylon.model.typechecker.model.NothingType) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) Constructor(org.eclipse.ceylon.model.typechecker.model.Constructor) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)

Example 95 with Declaration

use of org.eclipse.ceylon.model.typechecker.model.Declaration in project ceylon by eclipse.

the class TypeUtils method getToplevel.

public static Declaration getToplevel(Declaration d) {
    while (d != null && !d.isToplevel()) {
        Scope s = d.getContainer();
        // Skip any non-declaration elements
        while (s != null && !(s instanceof Declaration)) {
            s = s.getContainer();
        }
        d = (Declaration) s;
    }
    return d;
}
Also used : Scope(org.eclipse.ceylon.model.typechecker.model.Scope) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration)

Aggregations

Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)370 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)309 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)264 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)129 Type (org.eclipse.ceylon.model.typechecker.model.Type)100 Class (org.eclipse.ceylon.model.typechecker.model.Class)78 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)74 Value (org.eclipse.ceylon.model.typechecker.model.Value)73 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)72 Function (org.eclipse.ceylon.model.typechecker.model.Function)71 FunctionOrValue (org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)71 AnalyzerUtil.getTypedDeclaration (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypedDeclaration)63 ArrayList (java.util.ArrayList)61 AnalyzerUtil.getPackageTypeDeclaration (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getPackageTypeDeclaration)60 AnalyzerUtil.getTypeDeclaration (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypeDeclaration)60 ModelUtil.getNativeDeclaration (org.eclipse.ceylon.model.typechecker.model.ModelUtil.getNativeDeclaration)57 AnalyzerUtil.getPackageTypedDeclaration (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getPackageTypedDeclaration)51 Scope (org.eclipse.ceylon.model.typechecker.model.Scope)50 Constructor (org.eclipse.ceylon.model.typechecker.model.Constructor)48 CustomTree (org.eclipse.ceylon.compiler.typechecker.tree.CustomTree)45