import poset
import location
import model_base
+private import more_collections
redef class Model
# All known classes
return res
end
+ # Sort a given array of classes using the linerarization order of the module
+ # The most general is first, the most specific is last
+ fun linearize_mclasses(mclasses: Array[MClass])
+ do
+ self.flatten_mclass_hierarchy.sort(mclasses)
+ end
+
+ # Sort a given array of class definitions using the linerarization order of the module
+ # the refinement link is stronger than the specialisation link
+ # The most general is first, the most specific is last
+ fun linearize_mclassdefs(mclassdefs: Array[MClassDef])
+ do
+ var sorter = new MClassDefSorter(self)
+ sorter.sort(mclassdefs)
+ end
+
+ # Sort a given array of property definitions using the linerarization order of the module
+ # the refinement link is stronger than the specialisation link
+ # The most general is first, the most specific is last
+ fun linearize_mpropdefs(mpropdefs: Array[MPropDef])
+ do
+ var sorter = new MPropDefSorter(self)
+ sorter.sort(mpropdefs)
+ end
+
private var flatten_mclass_hierarchy_cache: nullable POSet[MClass] = null
# The primitive type Object, the root of the class hierarchy
end
end
+private class MClassDefSorter
+ super AbstractSorter[MClassDef]
+ var mmodule: MModule
+ redef fun compare(a, b)
+ do
+ var ca = a.mclass
+ var cb = b.mclass
+ if ca != cb then return mmodule.flatten_mclass_hierarchy.compare(ca, cb)
+ return mmodule.model.mclassdef_hierarchy.compare(a, b)
+ end
+end
+
+private class MPropDefSorter
+ super AbstractSorter[MPropDef]
+ var mmodule: MModule
+ redef fun compare(pa, pb)
+ do
+ var a = pa.mclassdef
+ var b = pb.mclassdef
+ var ca = a.mclass
+ var cb = b.mclass
+ if ca != cb then return mmodule.flatten_mclass_hierarchy.compare(ca, cb)
+ return mmodule.model.mclassdef_hierarchy.compare(a, b)
+ end
+end
+
# A named class
#
# MClass are global to the model; it means that a MClass is not bound to a
# Return the nullable version of the type
# If the type is already nullable then self is returned
- #
- # FIXME: DO NOT WORK YET
fun as_nullable: MType
do
var res = self.as_nullable_cache
end
# A type prefixed with "nullable"
-# FIXME Stub implementation
class MNullableType
super MType
# REQUIRE: mtype.has_mproperty(mmodule, self)
fun lookup_first_definition(mmodule: MModule, mtype: MType): MPROPDEF
do
+ return lookup_all_definitions(mmodule, mtype).first
+ end
+
+ # Return all definitions in a linearisation order
+ # Most speficic first, most general last
+ fun lookup_all_definitions(mmodule: MModule, mtype: MType): Array[MPROPDEF]
+ do
assert not mtype.need_anchor
- assert mtype.has_mproperty(mmodule, self)
+ if mtype isa MNullableType then mtype = mtype.mtype
+
+ var cache = self.lookup_all_definitions_cache[mmodule, mtype]
+ if cache != null then return cache
- var candidates = self.lookup_definitions(mmodule, mtype)
- if candidates.length == 1 then return candidates.first
- assert candidates.length > 0
+ #print "select prop {mproperty} for {mtype} in {self}"
+ # First, select all candidates
+ var candidates = new Array[MPROPDEF]
+ for mpropdef in self.mpropdefs do
+ # If the definition is not imported by the module, then skip
+ if not mmodule.in_importation <= mpropdef.mclassdef.mmodule then continue
+ # If the definition is not inherited by the type, then skip
+ if not mtype.is_subtype(mmodule, null, mpropdef.mclassdef.bound_mtype) then continue
+ # Else, we keep it
+ candidates.add(mpropdef)
+ end
+ # Fast track for only one candidate
+ if candidates.length <= 1 then
+ self.lookup_all_definitions_cache[mmodule, mtype] = candidates
+ return candidates
+ end
- print "BADLINEXT chose {candidates.first} in: {candidates.join(", ")}"
- return candidates.first
+ mmodule.linearize_mpropdefs(candidates)
+ candidates = candidates.reversed
+ self.lookup_all_definitions_cache[mmodule, mtype] = candidates
+ return candidates
end
+
+ private var lookup_all_definitions_cache: HashMap2[MModule, MType, Array[MPROPDEF]] = new HashMap2[MModule, MType, Array[MPROPDEF]]
end
# A global method
#
# This method is used to determine what method is called by a super.
#
- # FIXME: IMPLEMENTED AS A static designation, it is ugly
- #
# REQUIRE: not mtype.need_anchor
fun lookup_next_definition(mmodule: MModule, mtype: MType): MPROPDEF
do
assert not mtype.need_anchor
- var mpropdefs = self.mproperty.lookup_super_definitions(self.mclassdef.mmodule, self.mclassdef.bound_mtype)
- assert not mpropdefs.is_empty
- if mpropdefs.length > 1 then
- print "BADLINEXT chose next {mpropdefs.first} in: {mpropdefs.join(", ")}"
- end
- return mpropdefs.first
+ var mpropdefs = self.mproperty.lookup_all_definitions(mmodule, mtype)
+ var i = mpropdefs.iterator
+ while i.is_ok and i.item != self do i.next
+ assert has_property: i.is_ok
+ i.next
+ assert has_next_property: i.is_ok
+ return i.item
end
end