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
return c
end
print("Fatal Error: no primitive class {name}")
- abort
+ exit(1)
end
assert cla.length == 1 else print cla.join(", ")
return cla.first
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
# Warning: the introduction is the first `MClassDef' object associated
# to self. If self is just created without having any associated
# definition, this method will abort
- private fun intro: MClassDef
+ fun intro: MClassDef
do
assert has_a_first_definition: not mclassdefs.is_empty
return mclassdefs.first
end
+ # Return the class `self' in the class hierarchy of the module `mmodule'.
+ #
+ # SEE: MModule::flatten_mclass_hierarchy
+ # REQUIRE: mmodule.has_mclass(self)
+ fun in_hierarchy(mmodule: MModule): POSetElement[MClass]
+ do
+ return mmodule.flatten_mclass_hierarchy[self]
+ end
+
# The principal static type of the class.
#
# For non-generic class, mclass_type is the only MClassType based
# FIXME: quite ugly but not better idea yet
var supertypes: Array[MClassType] = new Array[MClassType]
- # Register the super-types for the class (ie "super SomeType")
- # This function can only invoked once by class
+ # Register some super-types for the class (ie "super SomeType")
+ #
+ # The hierarchy must not already be set
+ # REQUIRE: self.in_hierarchy == null
fun set_supertypes(supertypes: Array[MClassType])
do
assert unique_invocation: self.in_hierarchy == null
var mmodule = self.mmodule
var model = mmodule.model
- var res = model.mclassdef_hierarchy.add_node(self)
- self.in_hierarchy = res
var mtype = self.bound_mtype
for supertype in supertypes do
end
end
+ end
+
+ # Collect the super-types (set by set_supertypes) to build the hierarchy
+ #
+ # This function can only invoked once by class
+ # REQUIRE: self.in_hierarchy == null
+ # ENSURE: self.in_hierarchy != null
+ fun add_in_hierarchy
+ do
+ assert unique_invocation: self.in_hierarchy == null
+ var model = mmodule.model
+ var res = model.mclassdef_hierarchy.add_node(self)
+ self.in_hierarchy = res
+ var mtype = self.bound_mtype
+
+ # Here we need to connect the mclassdef to its pairs in the mclassdef_hierarchy
+ # The simpliest way is to attach it to collect_mclassdefs
for mclassdef in mtype.collect_mclassdefs(mmodule) do
res.poset.add_edge(self, mclassdef)
end
assert not sub.need_anchor
assert not sup.need_anchor
end
- # First, resolve the types
+
+ # First, resolve the formal types to a common version in the receiver
+ # The trick here is that fixed formal type will be associed to the bound
+ # And unfixed formal types will be associed to a canonical formal type.
if sub isa MParameterType or sub isa MVirtualType then
assert anchor != null
- sub = sub.resolve_for(anchor, anchor, mmodule, false)
+ sub = sub.resolve_for(anchor.mclass.mclass_type, anchor, mmodule, false)
end
if sup isa MParameterType or sup isa MVirtualType then
assert anchor != null
- sup = sup.resolve_for(anchor, anchor, mmodule, false)
+ sup = sup.resolve_for(anchor.mclass.mclass_type, anchor, mmodule, false)
+ end
+
+ # Does `sup` accept null or not?
+ # Discard the nullable marker if it exists
+ var sup_accept_null = false
+ if sup isa MNullableType then
+ sup_accept_null = true
+ sup = sup.mtype
+ else if sup isa MNullType then
+ sup_accept_null = true
+ end
+
+ # Can `sub` provide null or not?
+ # Thus we can match with `sup_accept_null`
+ # Also discard the nullable marker if it exists
+ if sub isa MNullableType then
+ if not sup_accept_null then return false
+ sub = sub.mtype
+ else if sub isa MNullType then
+ return sup_accept_null
end
+ # Now the case of direct null and nullable is over.
- if sup isa MParameterType or sup isa MVirtualType or sup isa MNullType then
+ # A unfixed formal type can only accept itself
+ if sup isa MParameterType or sup isa MVirtualType then
return sub == sup
end
+
+ # If `sub` is a formal type, then it is accepted if its bound is accepted
if sub isa MParameterType or sub isa MVirtualType then
assert anchor != null
sub = sub.anchor_to(mmodule, anchor)
- end
- if sup isa MNullableType then
- if sub isa MNullType then
- return true
- else if sub isa MNullableType then
- return sub.mtype.is_subtype(mmodule, anchor, sup.mtype)
- else if sub isa MClassType then
- return sub.is_subtype(mmodule, anchor, sup.mtype)
- else
- abort
+
+ # Manage the second layer of null/nullable
+ if sub isa MNullableType then
+ if not sup_accept_null then return false
+ sub = sub.mtype
+ else if sub isa MNullType then
+ return sup_accept_null
end
end
- assert sup isa MClassType # It is the only remaining type
- if sub isa MNullableType or sub isa MNullType then
+ assert sub isa MClassType # It is the only remaining type
+
+ if sup isa MNullType then
+ # `sup` accepts only null
return false
end
+ assert sup isa MClassType # It is the only remaining type
+
+ # Now both are MClassType, we need to dig
+
if sub == sup then return true
- assert sub isa MClassType # It is the only remaining type
if anchor == null then anchor = sub # UGLY: any anchor will work
var resolved_sub = sub.anchor_to(mmodule, anchor)
var res = resolved_sub.collect_mclasses(mmodule).has(sup.mclass)
if not sup isa MGenericType then return true
var sub2 = sub.supertype_to(mmodule, anchor, sup.mclass)
assert sub2.mclass == sup.mclass
- assert sub2 isa MGenericType
for i in [0..sup.mclass.arity[ do
var sub_arg = sub2.arguments[i]
var sup_arg = sup.arguments[i]
# 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
private var as_nullable_cache: nullable MType = null
+
+ # The deph of the type seen as a tree.
+ #
+ # A -> 1
+ # G[A] -> 2
+ # H[A, B] -> 2
+ # H[G[A], B] -> 3
+ #
+ # Formal types have a depth of 1.
+ fun depth: Int
+ do
+ return 1
+ end
+
# Compute all the classdefs inherited/imported.
# The returned set contains:
# * the class definitions from `mmodule` and its imported modules
self.mclass = mclass
end
+ # The formal arguments of the type
+ # ENSURE: return.length == self.mclass.arity
+ var arguments: Array[MType] = new Array[MType]
+
redef fun to_s do return mclass.to_s
redef fun need_anchor do return false
end
end
- # The formal arguments of the type
- # ENSURE: return.length == self.mclass.arity
- var arguments: Array[MType]
-
# Recursively print the type of the arguments within brackets.
- # Example: "Map[String,List[Int]]"
+ # Example: "Map[String, List[Int]]"
redef fun to_s
do
- return "{mclass}[{arguments.join(",")}]"
+ return "{mclass}[{arguments.join(", ")}]"
end
redef var need_anchor: Bool
end
return mclass.get_mtype(types)
end
+
+ redef fun depth
+ do
+ var dmax = 0
+ for a in self.arguments do
+ var d = a.depth
+ if d > dmax then dmax = d
+ end
+ return dmax + 1
+ end
end
# A virtual formal type.
redef fun resolve_for(mtype, anchor, mmodule, cleanup_virtual)
do
- if not cleanup_virtual then return self
# self is a virtual type declared (or inherited) in mtype
# The point of the function it to get the bound of the virtual type that make sense for mtype
# But because mtype is maybe a virtual/formal type, we need to get a real receiver first
# Now, we can get the bound
var verbatim_bound = lookup_bound(mmodule, resolved_reciever)
# The bound is exactly as declared in the "type" property, so we must resolve it again
- var res = verbatim_bound.resolve_for(mtype, anchor, mmodule, true)
+ var res = verbatim_bound.resolve_for(mtype, anchor, mmodule, cleanup_virtual)
#print "{class_name}: {self}/{mtype}/{anchor} -> {self}/{resolved_reciever}/{anchor} -> {verbatim_bound}/{mtype}/{anchor} -> {res}"
- return res
+
+ # What to return here? There is a bunch a special cases:
+ # If 'cleanup_virtual' we must return the resolved type, since we cannot return self
+ if cleanup_virtual then return res
+ # If the reciever is a intern class, then the virtual type cannot be redefined since there is no possible subclass. self is just fixed. so simply return the resolution
+ if resolved_reciever isa MNullableType then resolved_reciever = resolved_reciever.mtype
+ if resolved_reciever.as(MClassType).mclass.kind == enum_kind then return res
+ # If the resolved type isa MVirtualType, it means that self was bound to it, and cannot be unbound. self is just fixed. so return the resolution.
+ if res isa MVirtualType then return res
+ # It the resolved type isa intern class, then there is no possible valid redefinition is any potentiel subclass. self is just fixed. so simply return the resolution
+ if res isa MClassType and res.mclass.kind == enum_kind then return res
+ # TODO: Add 'fixed' virtual type in the specification.
+ # TODO: What if bound to a MParameterType?
+ # Note that Nullable types can always be redefined by the non nullable version, so there is no specific case on it.
+
+ # If anything apply, then `self' cannot be resolved, so return self
+ return self
end
redef fun to_s do return self.mproperty.to_s
if t.mclass == goalclass then
# Yeah! c specialize goalclass with a "super `t'". So the question is what is the argument of f
# FIXME: Here, we stop on the first goal. Should we check others and detect inconsistencies?
- assert t isa MGenericType
var res = t.arguments[self.rank]
return res
end
if resolved_receiver isa MNullableType then resolved_receiver = resolved_receiver.mtype
if resolved_receiver isa MParameterType then
assert resolved_receiver.mclass == anchor.mclass
- resolved_receiver = anchor.as(MGenericType).arguments[resolved_receiver.rank]
+ resolved_receiver = anchor.arguments[resolved_receiver.rank]
if resolved_receiver isa MNullableType then resolved_receiver = resolved_receiver.mtype
end
assert resolved_receiver isa MClassType else print "{class_name}: {self}/{mtype}/{anchor}? {resolved_receiver}"
# Eh! The parameter is in the current class.
# So we return the corresponding argument, no mater what!
if resolved_receiver.mclass == self.mclass then
- assert resolved_receiver isa MGenericType
var res = resolved_receiver.arguments[self.rank]
#print "{class_name}: {self}/{mtype}/{anchor} -> direct {res}"
return res
end
# A type prefixed with "nullable"
-# FIXME Stub implementation
class MNullableType
super MType
return res.as_nullable
end
+ redef fun depth do return self.mtype.depth
+
redef fun collect_mclassdefs(mmodule)
do
assert not self.need_anchor
# The return type (null for a procedure)
var return_mtype: nullable MType
+ redef fun depth
+ do
+ var dmax = 0
+ var t = self.return_mtype
+ if t != null then dmax = t.depth
+ for p in mparameters do
+ var d = p.mtype.depth
+ if d > dmax then dmax = d
+ end
+ for p in mclosures do
+ var d = p.mtype.depth
+ if d > dmax then dmax = d
+ end
+ return dmax + 1
+ end
+
# REQUIRE: 1 <= mparameters.count p -> p.is_vararg
init(mparameters: Array[MParameter], return_mtype: nullable MType)
do
end
# Return the most specific definition in the linearization of `mtype`.
- # If mtype does not know mproperty then null is returned.
#
# If you want to know the next properties in the linearization,
# look at `MPropDef::lookup_next_definition`.
#
- # FIXME: NOT YET IMPLEMENTED
+ # FIXME: the linearisation is still unspecified
#
# REQUIRE: not mtype.need_anchor
- fun lookup_first_definition(mmodule: MModule, mtype: MType): nullable MPROPDEF
+ # 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
- return null
+ if mtype isa MNullableType then mtype = mtype.mtype
+
+ var cache = self.lookup_all_definitions_cache[mmodule, mtype]
+ if cache != null then return cache
+
+ #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
+
+ 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
fun is_intro: Bool do return mproperty.intro == self
# Return the next definition in linearization of `mtype`.
- # If there is no next method then null is returned.
#
# This method is used to determine what method is called by a super.
#
- # FIXME: NOT YET IMPLEMENTED
- #
# REQUIRE: not mtype.need_anchor
- fun lookup_next_definition(mmodule: MModule, mtype: MType): nullable MPROPDEF
+ fun lookup_next_definition(mmodule: MModule, mtype: MType): MPROPDEF
do
assert not mtype.need_anchor
- return null
+
+ 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