nullable: convert lib, tools and tests
[nit.git] / src / metamodel / inheritance.nit
index 0eba770..2b2275b 100644 (file)
@@ -47,7 +47,7 @@ redef class MMModule
        meth import_local_classes
        do
                for g in _global_classes do
-                       if self[g] != null then continue
+                       if _local_class_by_global.has_key(g) then continue
                        var impl = new MMImplicitLocalClass(self, g) 
                end
        end
@@ -59,7 +59,7 @@ redef class MMLocalClass
 
        # Is the class computing super.
        # Used to detect specialization loops.
-       attr _computing_super: Bool 
+       attr _computing_super: Bool = false 
 
        # Compute super classes of a class
        meth compute_super_classes
@@ -106,14 +106,13 @@ redef class MMLocalClass
                end
        end
 
+       attr _are_global_properties_inherited: Bool = false
+
        # Inherit global properties for a class
        meth inherit_global_properties
        do
-               if _global_properties != null then return
-
-               _global_properties = new HashSet[MMGlobalProperty]
-               _properties_by_name = new HashMap[Symbol, Array[MMGlobalProperty]]
-               _local_property_by_global = new HashMap[MMGlobalProperty, MMLocalProperty]
+               if _are_global_properties_inherited then return
+               _are_global_properties_inherited = true
 
                var names = _properties_by_name
                var set = _global_properties
@@ -130,19 +129,26 @@ redef class MMLocalClass
                                        continue
                                end
 
-                               var gname = glob.intro.name
-                               var conf_set: Array[MMGlobalProperty]
-                               if names.has_key(gname) then
-                                       conf_set = names[gname]
-                               else
-                                       conf_set = new Array[MMGlobalProperty]
-                                       names[gname] = conf_set
-                               end
-                               conf_set.add(glob)
+                               make_visible_an_inherited_global_property(glob)
                        end
                end
        end
 
+       # Make the name of a global property meaningful in the class
+       meth make_visible_an_inherited_global_property(glob: MMGlobalProperty)
+       do
+               var names = _properties_by_name
+               var gname = glob.intro.name
+               var conf_set: Array[MMGlobalProperty]
+               if names.has_key(gname) then
+                       conf_set = names[gname]
+               else
+                       conf_set = new Array[MMGlobalProperty]
+                       names[gname] = conf_set
+               end
+               conf_set.add(glob)
+       end
+
        # Add super stype of this current local class
        meth add_direct_parent(p: MMAncestor)
        do
@@ -186,11 +192,13 @@ redef class MMLocalClass
 
        redef meth [](glob)
        do
-               var prop = super(glob)
-               if prop == null and _global_properties.has(glob) then
-                       prop = inherit_local_property(glob)
+               if _local_property_by_global.has_key(glob) then
+                       return _local_property_by_global[glob]
+               else if has_global_property(glob) then
+                       return inherit_local_property(glob)
+               else
+                       abort
                end
-               return prop
        end
 
        # Add default super class in direct parent and in super classes if this is not the Object class
@@ -209,10 +217,8 @@ redef class MMLocalClass
        do
                assert _crhe != null
                for ref in _crhe.direct_greaters do
-                       assert ref.cshe != null
                        for sup in ref.cshe.direct_greaters do
                                var cla = sup.for_module(_module)
-                               assert cla != null
                                supers.add(cla)
                        end
                end
@@ -230,7 +236,6 @@ redef class MMLocalClass
        private meth compute_super_parents(supers: Array[MMLocalClass])
        do
                for p in supers do
-                       assert p != null
                        p.compute_super_classes
                end
        end
@@ -314,62 +319,50 @@ redef class MMLocalClass
        private meth inherit_local_property(glob: MMGlobalProperty): MMLocalProperty
        do
                assert not _local_property_by_global.has_key(glob)
-               assert glob != null
-
-               var impls = glob.get_compatible_concrete_properties_for(self)
-               if impls.length != 1 then
-                       stderr.write("Fatal error: inherit_local_property error\n")
-                       print("------- {module}::{self} {glob.intro.full_name}")
-                       for i in impls do
-                               print("   {i.full_name}")
-                       end
-                       print("------- {glob.concrete_property_hierarchy.first}")
-                       print("------- {glob.concrete_property_hierarchy.to_dot}")
-                       exit(1)
-               end
-               var impl = impls.first
-               var ac = ancestor_for(impl.local_class)
-               ac.local_class.inherit_global_properties
-               var a = ac.stype
-               
-               assert a.local_class != self
-               var sup = a.select_property(glob) # Select super prop
-               assert sup != null
-               return sup
-       end
-end
 
-redef class MMConcreteProperty
-       # FIXME: use this
-       meth is_deferred: Bool do return false
-end
+               var impl: MMLocalProperty
 
-redef class MMGlobalProperty
-       # Get the most specific local properties defined in superclasses of c
-       private meth get_compatible_concrete_properties_for(cla: MMLocalClass): Array[MMConcreteProperty]
-       do
-               var impl_hier = _concrete_property_hierarchy
-               if impl_hier.length == 1 then return [intro]
-               var impls = new ArraySet[MMConcreteProperty]
-               var clache = cla.che
-               if true then
-                       #print "{cla.module}::{cla} get glob {intro.local_class}::{intro.name}"
-               for sc in cla.che.direct_greaters do
-                       #print "   try {sc.module}::{sc} {sc.che != null} {sc.crhe != null} {sc.che != null}"
-                       var p = sc[self]
-                       if p == null then continue
-                       var impl = p.concrete_property
-                       impls.add(impl)
-               end
+               var ghier = glob.property_hierarchy
+               var supers = che.direct_greaters
+               if ghier.length == 1 then
+                       # Unredefined property
+                       impl = glob.intro
+               else if supers.length == 1 then
+                       # Single inheritance
+                       impl = supers.first[glob]
                else
-               for impl in impl_hier do
-                       var bc = impl.local_class
-                       if clache < bc then
-                               impls.add(impl)
+                       # Hard multiple inheritance
+                       # First compute the set of bottom properties
+                       var impls = new ArraySet[MMLocalProperty]
+                       for sc in supers do
+                               if sc.has_global_property(glob) then impls.add(sc[glob])
                        end
+                       # Second, extract most specific
+                       var impls2 = ghier.select_smallests(impls)
+                       # Conflict case (FIXME)
+                       if impls2.length != 1 then
+                               stderr.write("Fatal error: inherit_local_property error\n")
+                               print("------- {module}::{self} {glob.intro.full_name}")
+                               for i in impls2 do
+                                       print("   {i.full_name}")
+                               end
+                               print("------- {glob.property_hierarchy.first}")
+                               print("------- {glob.property_hierarchy.to_dot}")
+                               exit(1)
+                       end
+                       impl = impls2.first
                end
-               end
-               return impl_hier.select_smallests(impls)
+
+               # FIXME: Why these 3 lines ?
+               #var ac = ancestor_for(impl.local_class)
+               #ac.local_class.inherit_global_properties
+               #var a = ac.stype
+               #assert a.local_class != self
+
+               # Register the local property
+               _local_property_by_global[glob] = impl
+               
+               return impl
        end
 end
 
@@ -378,8 +371,12 @@ redef class MMLocalProperty
        meth inherit_global(g: MMGlobalProperty)
        do
                set_global(g)
-               # What the next line used for?
-               g.add_concrete_property(concrete_property, g.get_compatible_concrete_properties_for(local_class))
+               var impls = new Array[MMLocalProperty]
+               for sc in local_class.che.direct_greaters do
+                       if not sc.has_global_property(g) then continue
+                       impls.add(sc[g])
+               end
+               g.add_local_property(self, impls)
        end
 end
 
@@ -387,11 +384,9 @@ redef class MMAncestor
        # Add this ancestor and it's super one in tab
        private meth add_in(tab: Array[MMAncestor])
        do
-               assert ancestor: stype != null
-               assert local_class: stype.local_class != null
                tab.add(self)
                stype.local_class.compute_ancestors
-               for anc in stype.local_class.ancestors do
+               for anc in stype.local_class.ancestors.as(not null) do
                        var aaa = anc.stype.for_module(stype.module)
                        var a = aaa.adapt_to(stype).for_module(inheriter.module)
                        if a.local_class != inheriter.local_class then
@@ -417,8 +412,7 @@ special MMLocalClass
        init(mod: MMModule, g: MMGlobalClass)
        do
                var cla = g.intro
-               super(cla.name, cla.arity)
-               mod.add_local_class(self)
+               super(mod, cla.name, cla.arity)
                set_global(g)
        end
 end
@@ -453,9 +447,6 @@ special MMAncestor
 
        init(b: MMLocalClass, anc: MMType)
        do
-               assert b != null
-               assert b.module != null
-               assert anc != null
                inheriter = b.get_type
                stype = anc
        end