use of org.eclipse.ceylon.model.typechecker.model.Declaration in project ceylon by eclipse.
the class JsonPackage method loadTypeAlias.
/**
* Load a type alias, creating it if necessary.
* @return The TypeAlias declaration.
*/
@SuppressWarnings("unchecked")
private TypeAlias loadTypeAlias(String name, Map<String, Object> m, Scope parent, List<TypeParameter> existing) {
TypeAlias alias;
if (m.get(KEY_METATYPE) instanceof TypeAlias) {
// It's been loaded already
alias = (TypeAlias) m.get(KEY_METATYPE);
if (m.size() == 1) {
return alias;
}
} else {
Declaration maybe = parent.getDirectMember(name, null, false);
if (maybe == null) {
alias = new TypeAlias();
alias.setContainer(parent);
alias.setScope(parent);
alias.setName(name);
alias.setUnit(u2);
setAnnotations(alias, (Integer) m.remove(KEY_PACKED_ANNS), m.remove(KEY_ANNOTATIONS));
m.put(KEY_METATYPE, alias);
} else if (maybe instanceof TypeAlias) {
alias = (TypeAlias) maybe;
} else {
throw new IllegalStateException(maybe + " should be an TypeAlias");
}
}
// Gather available type parameters
List<Map<String, Object>> listOfMaps = (List<Map<String, Object>>) m.get(KEY_TYPE_PARAMS);
final List<TypeParameter> tparms;
if (listOfMaps != null && alias.getTypeParameters().size() < listOfMaps.size()) {
tparms = parseTypeParameters(listOfMaps, alias, existing);
alias.setTypeParameters(tparms);
} else {
tparms = alias.getTypeParameters();
}
final List<TypeParameter> allparms = JsonPackage.merge(tparms, existing);
// All interfaces extend Object, except aliases
if (alias.getExtendedType() == null) {
alias.setExtendedType(getTypeFromJson((Map<String, Object>) m.get("$alias"), parent instanceof Declaration ? (Declaration) parent : null, allparms));
}
if (m.containsKey(KEY_SELF_TYPE)) {
for (TypeParameter _tp : tparms) {
if (_tp.getName().equals(m.get(KEY_SELF_TYPE))) {
alias.setSelfType(_tp.getType());
}
}
}
if (m.containsKey("of")) {
alias.setCaseTypes(parseTypeList((List<Map<String, Object>>) m.remove("of"), allparms));
}
if (m.containsKey(KEY_SATISFIES)) {
List<Map<String, Object>> stypes = (List<Map<String, Object>>) m.remove(KEY_SATISFIES);
alias.setSatisfiedTypes(parseTypeList(stypes, allparms));
}
m.clear();
m.put(KEY_METATYPE, alias);
if (parent == this) {
u2.addDeclaration(alias);
}
parent.addMember(alias);
return alias;
}
use of org.eclipse.ceylon.model.typechecker.model.Declaration in project ceylon by eclipse.
the class JsonPackage method loadInterface.
@SuppressWarnings("unchecked")
Interface loadInterface(String name, Map<String, Object> m, Scope parent, final List<TypeParameter> existing) {
// Check if it's been loaded first
// It hasn't been loaded, so create it
Interface t;
m.remove(KEY_NAME);
if (m.get(KEY_METATYPE) instanceof Interface) {
t = (Interface) m.get(KEY_METATYPE);
if (m.size() <= 3) {
// it's been loaded
return t;
}
} else {
if (m.containsKey("$alias")) {
t = new InterfaceAlias();
} else {
t = new Interface();
}
t.setContainer(parent);
t.setScope(parent);
t.setName(name);
t.setUnit(u2);
if (parent == this) {
u2.addDeclaration(t);
}
parent.addMember(t);
m.put(KEY_METATYPE, t);
setAnnotations(t, (Integer) m.remove(KEY_PACKED_ANNS), m.remove(KEY_ANNOTATIONS));
}
if (m.remove(KEY_DYNAMIC) != null) {
t.setDynamic(true);
}
List<TypeParameter> tparms = t.getTypeParameters();
List<Map<String, Object>> listOfMaps = (List<Map<String, Object>>) m.get(KEY_TYPE_PARAMS);
if (listOfMaps != null && (tparms == null || tparms.size() < listOfMaps.size())) {
tparms = parseTypeParameters(listOfMaps, t, existing);
m.remove(KEY_TYPE_PARAMS);
}
final List<TypeParameter> allparms = JsonPackage.merge(tparms, existing);
// All interfaces extend Object, except aliases
if (t.getExtendedType() == null) {
if (t.isAlias()) {
t.setExtendedType(getTypeFromJson((Map<String, Object>) m.remove("$alias"), parent instanceof Declaration ? (Declaration) parent : null, allparms));
} else {
t.setExtendedType(getTypeFromJson(objclass, parent instanceof Declaration ? (Declaration) parent : null, null));
}
}
if (m.containsKey(KEY_SELF_TYPE)) {
for (TypeParameter _tp : tparms) {
if (_tp.getName().equals(m.get(KEY_SELF_TYPE))) {
t.setSelfType(_tp.getType());
_tp.setSelfTypedDeclaration(t);
}
}
m.remove(KEY_SELF_TYPE);
}
if (m.containsKey("of") && t.getCaseTypes() == null) {
t.setCaseTypes(parseTypeList((List<Map<String, Object>>) m.remove("of"), allparms));
}
if (m.containsKey(KEY_SATISFIES)) {
for (Type s : parseTypeList((List<Map<String, Object>>) m.remove(KEY_SATISFIES), allparms)) {
t.getSatisfiedTypes().add(s);
}
}
addAttributesAndMethods(m, t, allparms);
if (m.containsKey(KEY_INTERFACES)) {
Map<String, Map<String, Object>> cdefs = (Map<String, Map<String, Object>>) m.remove(KEY_INTERFACES);
for (Map.Entry<String, Map<String, Object>> cdef : cdefs.entrySet()) {
loadInterface(cdef.getKey(), cdef.getValue(), t, allparms);
}
}
if (m.containsKey(KEY_CLASSES)) {
Map<String, Map<String, Object>> cdefs = (Map<String, Map<String, Object>>) m.remove(KEY_CLASSES);
for (Map.Entry<String, Map<String, Object>> cdef : cdefs.entrySet()) {
loadClass(cdef.getKey(), cdef.getValue(), t, allparms);
}
}
if (t.isDynamic() && (getModule().getJsMajor() < 9 || (getModule().getJsMajor() == 9 && getModule().getJsMinor() < 1))) {
// previous versions did not set dynamic flag on members
t.makeMembersDynamic();
}
return t;
}
use of org.eclipse.ceylon.model.typechecker.model.Declaration 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();
}
use of org.eclipse.ceylon.model.typechecker.model.Declaration 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;
}
use of org.eclipse.ceylon.model.typechecker.model.Declaration 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;
}
Aggregations