Search in sources :

Example 16 with ParameterList

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

the class Strategy method hasNoRequiredParameters.

/**
 * Returns true if the given functional takes no parameters or accepts only defaulted params
 */
private static boolean hasNoRequiredParameters(Functional model) {
    List<ParameterList> parameterLists = model.getParameterLists();
    if (parameterLists == null || parameterLists.size() != 1)
        return false;
    ParameterList parameterList = parameterLists.get(0);
    if (parameterList == null)
        return false;
    List<Parameter> parameters = parameterList.getParameters();
    if (parameters == null)
        return false;
    if (parameters.isEmpty())
        return true;
    // if the first one is optional, all others have to be too
    return parameters.get(0).isDefaulted();
}
Also used : ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter)

Example 17 with ParameterList

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

the class JsonPackage method loadMethod.

@SuppressWarnings("unchecked")
Function loadMethod(String name, Map<String, Object> m, Scope parent, final List<TypeParameter> existing) {
    Function md = new Function();
    md.setName(name);
    m.remove(KEY_NAME);
    md.setContainer(parent);
    md.setScope(parent);
    setAnnotations(md, (Integer) m.remove(KEY_PACKED_ANNS), m.remove(KEY_ANNOTATIONS));
    md.setUnit(u2);
    if (parent == this) {
        // Top-level declarations are directly added to the unit
        u2.addDeclaration(md);
        addMember(null);
    }
    if (m.containsKey(KEY_FLAGS)) {
        int flags = (int) m.remove(KEY_FLAGS);
        md.setDeclaredVoid((flags & 1) > 0);
        md.setDeferred((flags & 2) > 0);
    }
    md.setDynamic(m.remove(KEY_DYNAMIC) != null);
    final List<TypeParameter> tparms = parseTypeParameters((List<Map<String, Object>>) m.get(KEY_TYPE_PARAMS), md, existing);
    final List<TypeParameter> allparms = JsonPackage.merge(tparms, existing);
    md.setType(getTypeFromJson((Map<String, Object>) m.remove(KEY_TYPE), parent instanceof Declaration ? (Declaration) parent : null, allparms));
    List<List<Map<String, Object>>> paramLists = (List<List<Map<String, Object>>>) m.remove(KEY_PARAMS);
    if (paramLists == null) {
        md.addParameterList(new ParameterList());
    } else {
        boolean first = true;
        for (List<Map<String, Object>> plist : paramLists) {
            ParameterList _params = parseParameters(plist, md, allparms);
            _params.setNamedParametersSupported(first);
            first = false;
            md.addParameterList(_params);
            for (Parameter p : _params.getParameters()) {
                md.addMember(p.getModel());
            }
        }
    }
    return md;
}
Also used : TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) Function(org.eclipse.ceylon.model.typechecker.model.Function) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) 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 18 with ParameterList

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

the class JsonPackage method loadClass.

/**
 * Loads a class from the specified map. To avoid circularities, when the class is being created it is
 * added to the map, and once it's been fully loaded, all other keys are removed.
 */
@SuppressWarnings("unchecked")
org.eclipse.ceylon.model.typechecker.model.Class loadClass(String name, Map<String, Object> m, Scope parent, final List<TypeParameter> existing) {
    org.eclipse.ceylon.model.typechecker.model.Class cls;
    m.remove(KEY_NAME);
    if (m.get(KEY_METATYPE) instanceof org.eclipse.ceylon.model.typechecker.model.Class) {
        cls = (org.eclipse.ceylon.model.typechecker.model.Class) m.get(KEY_METATYPE);
        if (m.size() <= 3) {
            // It's been fully loaded
            return cls;
        }
    } else {
        // It's not there, so create it
        if (m.containsKey("$alias")) {
            cls = new org.eclipse.ceylon.model.typechecker.model.ClassAlias();
        } else {
            cls = new org.eclipse.ceylon.model.typechecker.model.Class();
        }
        cls.setAbstract(m.remove("abstract") != null);
        cls.setAnonymous(m.remove("$anon") != null);
        cls.setDynamic(m.remove(KEY_DYNAMIC) != null);
        cls.setContainer(parent);
        cls.setScope(parent);
        cls.setName(name);
        cls.setUnit(u2);
        if (parent == this) {
            u2.addDeclaration(cls);
        }
        parent.addMember(cls);
        m.put(KEY_METATYPE, cls);
        setAnnotations(cls, (Integer) m.remove(KEY_PACKED_ANNS), m.remove(KEY_ANNOTATIONS));
    }
    // Type parameters are about the first thing we need to load
    final List<TypeParameter> tparms = parseTypeParameters((List<Map<String, Object>>) m.remove(KEY_TYPE_PARAMS), cls, existing);
    final List<TypeParameter> allparms = JsonPackage.merge(tparms, existing);
    if (m.containsKey(KEY_SELF_TYPE)) {
        for (TypeParameter t : tparms) {
            if (t.getName().equals(m.get(KEY_SELF_TYPE))) {
                cls.setSelfType(t.getType());
            }
        }
    }
    // This is to avoid circularity
    if (!(isLanguagePackage() && ("Nothing".equals(name) || "Anything".equals(name)))) {
        if (cls.getExtendedType() == null) {
            if (m.containsKey("super")) {
                Type father = getTypeFromJson((Map<String, Object>) m.get("super"), parent instanceof Declaration ? (Declaration) parent : null, allparms);
                if (father != null) {
                    m.remove("super");
                    cls.setExtendedType(father);
                }
            } else {
                cls.setExtendedType(getTypeFromJson(idobj, parent instanceof Declaration ? (Declaration) parent : null, allparms));
            }
        }
    }
    if (cls instanceof ClassAlias) {
        ClassAlias ca = (ClassAlias) cls;
        if (m.containsKey(KEY_CONSTRUCTOR)) {
            String constructorName = (String) m.get(KEY_CONSTRUCTOR);
            Function ctorFn = (Function) ca.getExtendedType().getDeclaration().getDirectMember(constructorName, null, false);
            ca.setConstructor(ctorFn.getType().getDeclaration());
        } else {
            ca.setConstructor(ca.getExtendedType().getDeclaration());
        }
    }
    if (m.containsKey(KEY_CONSTRUCTORS)) {
        final Map<String, Map<String, Object>> constructors = (Map<String, Map<String, Object>>) m.remove(KEY_CONSTRUCTORS);
        for (Map.Entry<String, Map<String, Object>> cons : constructors.entrySet()) {
            Constructor cnst = new Constructor();
            cnst.setName("$def".equals(cons.getKey()) ? null : cons.getKey());
            cnst.setContainer(cls);
            cnst.setScope(cls);
            cnst.setUnit(cls.getUnit());
            cnst.setExtendedType(cls.getType());
            cnst.setDynamic(cons.getValue().remove(KEY_DYNAMIC) != null);
            setAnnotations(cnst, (Integer) cons.getValue().remove(KEY_PACKED_ANNS), cons.getValue().remove(KEY_ANNOTATIONS));
            final List<Map<String, Object>> modelPlist = (List<Map<String, Object>>) cons.getValue().remove(KEY_PARAMS);
            cls.addMember(cnst);
            if (modelPlist == null) {
                // It's a value constructor
                cls.setEnumerated(true);
                Value cv = new Value();
                cv.setName(cnst.getName());
                cv.setType(cnst.getType());
                cv.setContainer(cls);
                cv.setScope(cls);
                cv.setUnit(cls.getUnit());
                cv.setVisibleScope(cls.getVisibleScope());
                cv.setShared(cls.isShared());
                cv.setDeprecated(cls.isDeprecated());
                cls.addMember(cv);
            } else {
                cls.setConstructors(true);
                final ParameterList plist = parseParameters(modelPlist, cnst, allparms);
                cnst.addParameterList(plist);
                plist.setNamedParametersSupported(true);
                Function cf = new Function();
                cf.setName(cnst.getName());
                final Type ft = cnst.appliedType(cnst.getExtendedType(), Collections.<Type>emptyList());
                cf.setType(ft);
                cf.addParameterList(plist);
                cf.setContainer(cls);
                cf.setScope(cls);
                cf.setUnit(cls.getUnit());
                cf.setVisibleScope(cnst.getVisibleScope());
                cf.setShared(cnst.isShared());
                cf.setDeprecated(cnst.isDeprecated());
                cf.setDynamic(cnst.isDynamic());
                cls.addMember(cf);
            }
            if (cons.getValue().containsKey(KEY_JS_TSENUM)) {
                cnst.setTypescriptEnum((String) cons.getValue().get(KEY_JS_TSENUM));
            }
        }
    } else {
        ParameterList plist = parseParameters((List<Map<String, Object>>) m.remove(KEY_PARAMS), cls, allparms);
        plist.setNamedParametersSupported(true);
        cls.setParameterList(plist);
    }
    if (m.containsKey("of") && cls.getCaseTypes() == null) {
        cls.setCaseTypes(parseTypeList((List<Map<String, Object>>) m.get("of"), allparms));
        m.remove("of");
    }
    if (m.containsKey(KEY_SATISFIES)) {
        List<Map<String, Object>> stypes = (List<Map<String, Object>>) m.remove(KEY_SATISFIES);
        cls.setSatisfiedTypes(parseTypeList(stypes, allparms));
    }
    if (m.containsKey(KEY_OBJECTS)) {
        for (Map.Entry<String, Map<String, Object>> inner : ((Map<String, Map<String, Object>>) m.get(KEY_OBJECTS)).entrySet()) {
            loadObject(inner.getKey(), inner.getValue(), cls, allparms);
        }
        m.remove(KEY_OBJECTS);
    }
    addAttributesAndMethods(m, cls, allparms);
    if (m.containsKey(KEY_INTERFACES)) {
        Map<String, Map<String, Object>> cdefs = (Map<String, Map<String, Object>>) m.get(KEY_INTERFACES);
        for (Map.Entry<String, Map<String, Object>> cdef : cdefs.entrySet()) {
            loadInterface(cdef.getKey(), cdef.getValue(), cls, allparms);
        }
        m.remove(KEY_INTERFACES);
    }
    if (m.containsKey(KEY_CLASSES)) {
        Map<String, Map<String, Object>> cdefs = (Map<String, Map<String, Object>>) m.get(KEY_CLASSES);
        for (Map.Entry<String, Map<String, Object>> cdef : cdefs.entrySet()) {
            loadClass(cdef.getKey(), cdef.getValue(), cls, allparms);
        }
        m.remove(KEY_CLASSES);
    }
    if (cls.isDynamic() && (getModule().getJsMajor() < 9 || (getModule().getJsMajor() == 9 && getModule().getJsMinor() < 1))) {
        // previous versions did not set dynamic flag on members
        cls.makeMembersDynamic();
    }
    return cls;
}
Also used : TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) ClassAlias(org.eclipse.ceylon.model.typechecker.model.ClassAlias) Constructor(org.eclipse.ceylon.model.typechecker.model.Constructor) Function(org.eclipse.ceylon.model.typechecker.model.Function) 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) Value(org.eclipse.ceylon.model.typechecker.model.Value) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) ClassAlias(org.eclipse.ceylon.model.typechecker.model.ClassAlias) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) 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 19 with ParameterList

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

the class MetamodelGenerator method encodeMethod.

@SuppressWarnings("unchecked")
public Map<String, Object> encodeMethod(Function d) {
    final Map<String, Object> m = new HashMap<>();
    m.put(KEY_METATYPE, METATYPE_METHOD);
    m.put(KEY_NAME, d.getName());
    if (d.isDynamic()) {
        m.put(KEY_DYNAMIC, 1);
    }
    List<Map<String, Object>> tpl = typeParameters(d.getTypeParameters(), d);
    if (tpl != null) {
        m.put(KEY_TYPE_PARAMS, tpl);
    }
    m.put(KEY_TYPE, typeMap(d.getType(), d));
    List<List<Map<String, Object>>> paramLists = new ArrayList<>(d.getParameterLists().size());
    for (ParameterList plist : d.getParameterLists()) {
        List<Map<String, Object>> params = parameterListMap(plist, d);
        if (params == null) {
            params = Collections.emptyList();
        }
        paramLists.add(params);
    }
    if (paramLists.size() > 1 || !paramLists.get(0).isEmpty()) {
        m.put(KEY_PARAMS, paramLists);
    }
    int ff = 0;
    if (d.isDeclaredVoid()) {
        ff |= 1;
    }
    if (d.isDeferred()) {
        ff |= 2;
    }
    if (ff > 0) {
        m.put(KEY_FLAGS, ff);
    }
    // Annotations
    encodeAnnotations(d.getAnnotations(), d, m);
    Map<String, Object> parent = findParent(d);
    if (parent != null) {
        if (parent != getPackageMap(d.getUnit().getPackage())) {
            if (!parent.containsKey(KEY_METHODS)) {
                parent.put(KEY_METHODS, new HashMap<>());
            }
            parent = (Map<String, Object>) parent.get(KEY_METHODS);
        }
        if (parent != null) {
            parent.put(TypeUtils.modelName(d), m);
        }
    }
    return m;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 20 with ParameterList

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

the class MetamodelGenerator method encodeConstructor.

public Map<String, Object> encodeConstructor(final Constructor d) {
    // First of all, find the class this thing belongs to
    Map<String, Object> c = findParent(d);
    final String mname = TypeUtils.modelName(d);
    if (c == null) {
        System.out.println("WTF no parent for Constructor " + d);
        return null;
    }
    Map<String, Object> m = new HashMap<>();
    if (d.getName() != null) {
        m.put(KEY_NAME, mname);
    }
    final ParameterList plist = d.getFirstParameterList();
    if (plist != null) {
        m.put(KEY_PARAMS, plist.getParameters().isEmpty() ? Collections.EMPTY_LIST : parameterListMap(plist, d));
    }
    encodeAnnotations(d.getAnnotations(), d, m);
    if (c.get(KEY_CONSTRUCTORS) == null) {
        c.put(KEY_CONSTRUCTORS, new HashMap<>());
    }
    @SuppressWarnings("unchecked") Map<String, Object> consmap = (Map<String, Object>) c.get(KEY_CONSTRUCTORS);
    consmap.put(d.getName() == null ? "$def" : mname, m);
    return null;
}
Also used : HashMap(java.util.HashMap) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ParameterList (org.eclipse.ceylon.model.typechecker.model.ParameterList)69 Parameter (org.eclipse.ceylon.model.typechecker.model.Parameter)47 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)44 Type (org.eclipse.ceylon.model.typechecker.model.Type)25 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)24 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)24 Functional (org.eclipse.ceylon.model.typechecker.model.Functional)23 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)22 Function (org.eclipse.ceylon.model.typechecker.model.Function)21 FunctionOrValue (org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)20 Value (org.eclipse.ceylon.model.typechecker.model.Value)20 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)19 ArrayList (java.util.ArrayList)16 AnalyzerUtil.getMatchingParameter (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getMatchingParameter)12 AnalyzerUtil.getUnspecifiedParameter (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getUnspecifiedParameter)12 CustomTree (org.eclipse.ceylon.compiler.typechecker.tree.CustomTree)12 FieldValue (org.eclipse.ceylon.model.loader.model.FieldValue)12 Class (org.eclipse.ceylon.model.typechecker.model.Class)11 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)10 HashMap (java.util.HashMap)9