Search in sources :

Example 51 with Value

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

the class JsonPackage method loadObject.

/**
 * Loads an object declaration, creating it if necessary, and returns its type declaration.
 */
@SuppressWarnings("unchecked")
TypeDeclaration loadObject(String name, Map<String, Object> m, Scope parent, List<TypeParameter> existing) {
    Value obj;
    if (m.get(KEY_METATYPE) instanceof Value) {
        obj = (Value) m.get(KEY_METATYPE);
    } else {
        obj = new Value();
        m.put(KEY_METATYPE, obj);
        obj.setName(name);
        obj.setContainer(parent);
        obj.setScope(parent);
        obj.setUnit(u2);
        org.eclipse.ceylon.model.typechecker.model.Class type = new org.eclipse.ceylon.model.typechecker.model.Class();
        type.setName(name);
        type.setAnonymous(true);
        type.setUnit(u2);
        type.setContainer(parent);
        type.setScope(parent);
        if (parent == this) {
            u2.addDeclaration(obj);
            u2.addDeclaration(type);
        }
        parent.addMember(obj);
        obj.setType(type.getType());
        setAnnotations(obj, (Integer) m.get(KEY_PACKED_ANNS), m.get(KEY_ANNOTATIONS));
        setAnnotations(obj.getTypeDeclaration(), (Integer) m.remove(KEY_PACKED_ANNS), m.remove(KEY_ANNOTATIONS));
        if (type.getExtendedType() == null) {
            if (m.containsKey("super")) {
                type.setExtendedType(getTypeFromJson((Map<String, Object>) m.remove("super"), parent instanceof Declaration ? (Declaration) parent : null, existing));
            } else {
                type.setExtendedType(getTypeFromJson(idobj, parent instanceof Declaration ? (Declaration) parent : null, existing));
            }
        }
        if (m.containsKey(KEY_SATISFIES)) {
            List<Map<String, Object>> stypes = (List<Map<String, Object>>) m.remove(KEY_SATISFIES);
            type.setSatisfiedTypes(parseTypeList(stypes, existing));
        }
        if (m.containsKey(KEY_INTERFACES)) {
            for (Map.Entry<String, Map<String, Object>> inner : ((Map<String, Map<String, Object>>) m.remove(KEY_INTERFACES)).entrySet()) {
                loadInterface(inner.getKey(), inner.getValue(), type, existing);
            }
        }
        if (m.containsKey(KEY_CLASSES)) {
            for (Map.Entry<String, Map<String, Object>> inner : ((Map<String, Map<String, Object>>) m.remove(KEY_CLASSES)).entrySet()) {
                loadClass(inner.getKey(), inner.getValue(), type, existing);
            }
        }
        if (m.containsKey(KEY_OBJECTS)) {
            for (Map.Entry<String, Map<String, Object>> inner : ((Map<String, Map<String, Object>>) m.remove(KEY_OBJECTS)).entrySet()) {
                loadObject(inner.getKey(), inner.getValue(), type, existing);
            }
        }
        addAttributesAndMethods(m, type, existing);
    }
    return obj.getTypeDeclaration();
}
Also used : Value(org.eclipse.ceylon.model.typechecker.model.Value) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) 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 52 with Value

use of org.eclipse.ceylon.model.typechecker.model.Value 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 53 with Value

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

the class JsonPackage method loadAttribute.

FunctionOrValue loadAttribute(String name, Map<String, Object> m, Scope parent, List<TypeParameter> typeParameters) {
    String metatype = (String) m.get(KEY_METATYPE);
    Value d = new Value();
    d.setTransient(METATYPE_GETTER.equals(metatype));
    d.setName(name);
    d.setContainer(parent);
    d.setScope(parent);
    d.setUnit(u2);
    if (parent == this) {
        u2.addDeclaration(d);
        addMember(null);
    }
    setAnnotations(d, (Integer) m.remove(KEY_PACKED_ANNS), m.remove(KEY_ANNOTATIONS));
    d.setDynamic(m.remove(KEY_DYNAMIC) != null);
    if (m.containsKey("var")) {
        d.setVariable(true);
    }
    @SuppressWarnings("unchecked") final Map<String, Object> ktype = (Map<String, Object>) m.get(KEY_TYPE);
    d.setType(getTypeFromJson(ktype, parent instanceof Declaration ? (Declaration) parent : null, typeParameters));
    @SuppressWarnings("unchecked") final Map<String, Object> smap = (Map<String, Object>) m.remove("$set");
    if (smap != null) {
        Setter s = new Setter();
        s.setName(name);
        s.setContainer(parent);
        s.setScope(parent);
        s.setUnit(u2);
        s.setGetter(d);
        d.setSetter(s);
        if (parent == this) {
            u2.addDeclaration(s);
            addMember(null);
        }
        setAnnotations(s, (Integer) smap.remove(KEY_PACKED_ANNS), smap.remove(KEY_ANNOTATIONS));
        s.setType(d.getType());
    }
    return d;
}
Also used : Value(org.eclipse.ceylon.model.typechecker.model.Value) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) Setter(org.eclipse.ceylon.model.typechecker.model.Setter) 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 54 with Value

use of org.eclipse.ceylon.model.typechecker.model.Value 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 55 with Value

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

the class MetamodelVisitor method visit.

@Override
public void visit(Tree.SpecifierStatement st) {
    TypedDeclaration d = ((Tree.SpecifierStatement) st).getDeclaration();
    // Just add shared and actual annotations to this declaration
    if (!isNativeHeader(d))
        return;
    if (d != null) {
        Annotation ann = new Annotation();
        ann.setName("shared");
        d.getAnnotations().add(ann);
        ann = new Annotation();
        ann.setName("actual");
        d.getAnnotations().add(ann);
        if (d instanceof Function) {
            gen.encodeMethod((Function) d);
        } else if (d instanceof Value) {
            gen.encodeAttributeOrGetter((Value) d);
        } else {
            throw new RuntimeException("JS compiler doesn't know how to encode " + d.getClass().getName() + " into model");
        }
    }
}
Also used : TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) Function(org.eclipse.ceylon.model.typechecker.model.Function) Value(org.eclipse.ceylon.model.typechecker.model.Value) Annotation(org.eclipse.ceylon.model.typechecker.model.Annotation)

Aggregations

Value (org.eclipse.ceylon.model.typechecker.model.Value)190 FunctionOrValue (org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)135 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)77 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)74 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)70 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)69 Type (org.eclipse.ceylon.model.typechecker.model.Type)68 Function (org.eclipse.ceylon.model.typechecker.model.Function)50 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)47 Class (org.eclipse.ceylon.model.typechecker.model.Class)46 Parameter (org.eclipse.ceylon.model.typechecker.model.Parameter)42 JavaBeanValue (org.eclipse.ceylon.model.loader.model.JavaBeanValue)30 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)30 ArrayList (java.util.ArrayList)29 ParameterList (org.eclipse.ceylon.model.typechecker.model.ParameterList)29 FieldValue (org.eclipse.ceylon.model.loader.model.FieldValue)28 Constructor (org.eclipse.ceylon.model.typechecker.model.Constructor)27 Scope (org.eclipse.ceylon.model.typechecker.model.Scope)26 CustomTree (org.eclipse.ceylon.compiler.typechecker.tree.CustomTree)24 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)23