Merge: CallSite on AFor and ARange
[nit.git] / src / model_utils.nit
index 44cf8e8..5347128 100644 (file)
@@ -17,8 +17,7 @@
 # Model exploration and traversing facilities
 module model_utils
 
-import toolcontext
-import exprbuilder
+import modelbuilder
 
 redef class MModule
        # Get the list of mclasses refined in 'self'.
@@ -43,6 +42,16 @@ end
 
 redef class MClass
 
+       # Get the public owner of 'self'.
+       fun public_owner: MModule do
+               var public_owner = self.intro_mmodule.public_owner
+               if public_owner == null then
+                       return self.intro_mmodule
+               else
+                       return public_owner
+               end
+       end
+
        # Get direct parents of 'self'.
        fun parents: Set[MClass] do
                var ret = new HashSet[MClass]
@@ -66,6 +75,30 @@ redef class MClass
                return lst
        end
 
+       # Get direct children of 'self'.
+       fun children: Set[MClass] do
+               var lst = new HashSet[MClass]
+               for mclassdef in self.mclassdefs do
+                       for sub_mclassdef in mclassdef.in_hierarchy.direct_smallers do
+                               if sub_mclassdef == mclassdef then continue  # skip self
+                               lst.add(sub_mclassdef.mclass)
+                       end
+               end
+               return lst
+       end
+
+       # Get all children of 'self'.
+       fun descendants: Set[MClass] do
+               var lst = new HashSet[MClass]
+               for mclassdef in self.mclassdefs do
+                       for sub_mclassdef in mclassdef.in_hierarchy.smallers do
+                               if sub_mclassdef == mclassdef then continue  # skip self
+                               lst.add(sub_mclassdef.mclass)
+                       end
+               end
+               return lst
+       end
+
        # Get the list of constructors available for 'self'.
        fun constructors: Set[MMethod] do
                var res = new HashSet[MMethod]
@@ -92,6 +125,57 @@ redef class MClass
                return res
        end
 
+       # the set of properties introduced in 'self'.
+       fun intro_mproperties(min_visibility: MVisibility): Set[MProperty] do
+               var set = new HashSet[MProperty]
+               for mclassdef in mclassdefs do
+                       for mprop in mclassdef.intro_mproperties do
+                               if mprop.visibility < min_visibility then continue
+                               set.add(mprop)
+                       end
+               end
+               return set
+       end
+
+       # the set of locally refined properties in 'self'.
+       fun redef_mproperties(min_visibility: MVisibility): Set[MProperty] do
+               var set = new HashSet[MProperty]
+               for mclassdef in mclassdefs do
+                       for mpropdef in mclassdef.mpropdefs do
+                               if mpropdef.mproperty.visibility < min_visibility then continue
+                               if mpropdef.mproperty.intro_mclassdef.mclass != self then set.add(mpropdef.mproperty)
+                       end
+               end
+               return set
+       end
+
+       # the set of methods inherited by 'self'.
+       fun inherited_mproperties(mainmodule: MModule, min_visibility: MVisibility): Set[MProperty] do
+               var set = new HashSet[MProperty]
+               for parent in in_hierarchy(mainmodule).direct_greaters do
+                       set.add_all(parent.intro_mproperties(min_visibility))
+                       set.add_all(parent.inherited_mproperties(mainmodule, min_visibility))
+               end
+               return set
+       end
+
+       # the set of introduced and redefined mproperties
+       fun local_mproperties(min_visibility: MVisibility): Set[MProperty] do
+               var set = new HashSet[MProperty]
+               set.add_all(intro_mproperties(min_visibility))
+               set.add_all(redef_mproperties(min_visibility))
+               return set
+       end
+
+       # the set of all accessible mproperties for this class
+       fun all_mproperties(mainmodule: MModule, min_visibility: MVisibility): Set[MProperty] do
+               var set = new HashSet[MProperty]
+               set.add_all(local_mproperties(min_visibility))
+               set.add_all(inherited_mproperties(mainmodule, min_visibility))
+               return set
+       end
+
+
        # Get the list of locally refined methods in 'self'.
        fun redef_methods: Set[MMethod] do
                var res = new HashSet[MMethod]
@@ -105,7 +189,6 @@ redef class MClass
                return res
        end
 
-       # Get the list of methods inherited by 'self'.
        fun inherited_methods: Set[MMethod] do
                var res = new HashSet[MMethod]
                for s in ancestors do
@@ -163,3 +246,40 @@ redef class MClass
                return self.kind == abstract_kind
        end
 end
+
+# Sorters
+
+# Sort mmodules by their name
+class MModuleNameSorter
+       super AbstractSorter[MModule]
+       redef fun compare(a, b) do return a.name <=> b.name
+       init do end
+end
+
+# Sort mclasses by their name
+class MClassNameSorter
+       super AbstractSorter[MClass]
+       redef fun compare(a, b) do return a.name <=> b.name
+       init do end
+end
+
+# Sort mclassdefs by their name
+class MClassDefNameSorter
+       super AbstractSorter[MClassDef]
+       redef fun compare(a, b) do return a.mclass.name <=> b.mclass.name
+       init do end
+end
+
+# Sort mproperties by their name
+class MPropertyNameSorter
+       super AbstractSorter[MProperty]
+       redef fun compare(a, b) do return a.name <=> b.name
+       init do end
+end
+
+# Sort mpropdefs by their name
+class MPropDefNameSorter
+       super AbstractSorter[MPropDef]
+       redef fun compare(a, b) do return a.mproperty.name <=> b.mproperty.name
+       init do end
+end