sdl2: intro minimal bindings to SDL2 mixer
[nit.git] / src / model / model.nit
index 3733566..8fb09ee 100644 (file)
@@ -103,6 +103,9 @@ redef class Model
        # The only null type
        var null_type = new MNullType(self)
 
+       # The only bottom type
+       var bottom_type: MBottomType = null_type.as_notnull
+
        # Build an ordered tree with from `concerns`
        fun concerns_tree(mconcerns: Collection[MConcern]): ConcernsTree do
                var seen = new HashSet[MConcern]
@@ -151,6 +154,14 @@ redef class MModule
        # (introduction and refinement)
        var mclassdefs = new Array[MClassDef]
 
+       private var mclassdef_sorter: MClassDefSorter is lazy do
+               return new MClassDefSorter(self)
+       end
+
+       private var mpropdef_sorter: MPropDefSorter is lazy do
+               return new MPropDefSorter(self)
+       end
+
        # Does the current module has a given class `mclass`?
        # Return true if the mmodule introduces, refines or imports a class.
        # Visibility is not considered.
@@ -198,8 +209,7 @@ redef class MModule
        # 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)
+               mclassdef_sorter.sort(mclassdefs)
        end
 
        # Sort a given array of property definitions using the linearization order of the module
@@ -207,8 +217,7 @@ redef class MModule
        # 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)
+               mpropdef_sorter.sort(mpropdefs)
        end
 
        private var flatten_mclass_hierarchy_cache: nullable POSet[MClass] = null
@@ -252,8 +261,8 @@ redef class MModule
        # The primitive type `String`
        var string_type: MClassType = self.get_primitive_class("String").mclass_type is lazy
 
-       # The primitive type `NativeString`
-       var native_string_type: MClassType = self.get_primitive_class("NativeString").mclass_type is lazy
+       # The primitive type `CString`
+       var c_string_type: MClassType = self.get_primitive_class("CString").mclass_type is lazy
 
        # A primitive type of `Array`
        fun array_type(elt_type: MType): MClassType do return array_class.get_mtype([elt_type])
@@ -352,14 +361,12 @@ private class MPropDefSorter
        super Comparator
        redef type COMPARED: 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)
+               return mmodule.mclassdef_sorter.compare(a, b)
        end
 end
 
@@ -566,7 +573,12 @@ class MClass
        # Is `self` and abstract class?
        var is_abstract: Bool is lazy do return kind == abstract_kind
 
-       redef fun mdoc_or_fallback do return intro.mdoc_or_fallback
+       redef fun mdoc_or_fallback
+       do
+               # Don’t use `intro.mdoc_or_fallback` because it would create an infinite
+               # recursion.
+               return intro.mdoc
+       end
 end
 
 
@@ -725,6 +737,8 @@ class MClassDef
 
        # All property introductions and redefinitions (not inheritance) in `self` by its associated property.
        var mpropdefs_by_property = new HashMap[MProperty, MPropDef]
+
+       redef fun mdoc_or_fallback do return mdoc or else mclass.mdoc_or_fallback
 end
 
 # A global static type
@@ -898,15 +912,16 @@ abstract class MType
        #
        # Explanation of the example:
        # In H, T is set to B, because "H super G[B]", and U is bound to Y,
-        # because "redef type U: Y". Therefore, Map[T, U] is bound to
+       # because "redef type U: Y". Therefore, Map[T, U] is bound to
        # Map[B, Y]
        #
+       # REQUIRE: `self.need_anchor implies anchor != null`
        # ENSURE: `not self.need_anchor implies result == self`
        # ENSURE: `not result.need_anchor`
-       fun anchor_to(mmodule: MModule, anchor: MClassType): MType
+       fun anchor_to(mmodule: MModule, anchor: nullable MClassType): MType
        do
                if not need_anchor then return self
-               assert not anchor.need_anchor
+               assert anchor != null and not anchor.need_anchor
                # Just resolve to the anchor and clear all the virtual types
                var res = self.resolve_for(anchor, null, mmodule, true)
                assert not res.need_anchor
@@ -1136,6 +1151,7 @@ abstract class MType
        # * H[G[A], B] -> 3
        #
        # Formal types have a depth of 1.
+       # Only `MClassType` and `MFormalType` nodes are counted.
        fun depth: Int
        do
                return 1
@@ -1149,6 +1165,7 @@ abstract class MType
        # * H[G[A], B] -> 4
        #
        # Formal types have a length of 1.
+       # Only `MClassType` and `MFormalType` nodes are counted.
        fun length: Int
        do
                return 1
@@ -1215,7 +1232,7 @@ class MClassType
 
        redef fun need_anchor do return false
 
-       redef fun anchor_to(mmodule: MModule, anchor: MClassType): MClassType
+       redef fun anchor_to(mmodule, anchor): MClassType
        do
                return super.as(MClassType)
        end
@@ -1295,6 +1312,7 @@ class MClassType
        private var collect_mclasses_cache = new HashMap[MModule, Set[MClass]]
        private var collect_mtypes_cache = new HashMap[MModule, Set[MClassType]]
 
+       redef fun mdoc_or_fallback do return mclass.mdoc_or_fallback
 end
 
 # A type based on a generic class.
@@ -1525,6 +1543,8 @@ class MVirtualType
        redef fun full_name do return self.mproperty.full_name
 
        redef fun c_name do return self.mproperty.c_name
+
+       redef fun mdoc_or_fallback do return mproperty.mdoc_or_fallback
 end
 
 # The type associated to a formal parameter generic type of a class
@@ -1809,7 +1829,7 @@ class MNullType
        redef fun c_name do return "null"
        redef fun as_nullable do return self
 
-       redef var as_notnull = new MBottomType(model) is lazy
+       redef var as_notnull: MBottomType = new MBottomType(model) is lazy
        redef fun need_anchor do return false
        redef fun resolve_for(mtype, anchor, mmodule, cleanup_virtual) do return self
        redef fun can_resolve_for(mtype, anchor, mmodule) do return true
@@ -2049,7 +2069,12 @@ abstract class MProperty
 
        redef var location
 
-       redef fun mdoc_or_fallback do return intro.mdoc_or_fallback
+       redef fun mdoc_or_fallback
+       do
+               # Don’t use `intro.mdoc_or_fallback` because it would create an infinite
+               # recursion.
+               return intro.mdoc
+       end
 
        # The canonical name of the property.
        #
@@ -2471,6 +2496,8 @@ abstract class MPropDef
                assert has_next_property: i.is_ok
                return i.item
        end
+
+       redef fun mdoc_or_fallback do return mdoc or else mproperty.mdoc_or_fallback
 end
 
 # A local definition of a method