typing: add `ARangeExpr::init_callsite` and use it everywhere
[nit.git] / src / rapid_type_analysis.nit
index 3ee7b28..aad2de0 100644 (file)
@@ -28,6 +28,9 @@ import modelbuilder
 import typing
 import auto_super_init
 
+import csv # for live_types_to_csv
+import ordered_tree # for live_methods_to_tree
+
 redef class ModelBuilder
        fun do_rapid_type_analysis(mainmodule: MModule): RapidTypeAnalysis
        do
@@ -60,7 +63,7 @@ class RapidTypeAnalysis
        var live_classes = new HashSet[MClass]
 
        # The pool of types used to perform type checks (isa and as).
-       var live_cast_types = new HashSet[MClassType]
+       var live_cast_types = new HashSet[MType]
 
        # The pool of undesolved types used to perform type checks (isa and as).
        # They are globally resolved at the end of the analaysis
@@ -75,12 +78,69 @@ class RapidTypeAnalysis
        # Live call-to-super.
        var live_super_sends = new HashSet[MMethodDef]
 
+       # Return a ready-to-save CSV document objet that agregates informations about live types.
+       # Each discovered type is listed in a line, with its status: resolution, liveness, cast-liveness.
+       # Note: types are listed in an alphanumeric order to improve human reading.
+       fun live_types_to_csv: CSVDocument
+       do
+               # Gather all kind of type
+               var typeset = new HashSet[MType]
+               typeset.add_all(live_types)
+               typeset.add_all(live_open_types)
+               typeset.add_all(live_cast_types)
+               typeset.add_all(live_open_cast_types)
+               var types = typeset.to_a
+               (new CachedAlphaComparator).sort(types)
+               var res = new CSVDocument
+               res.header = ["Type", "Resolution", "Liveness", "Cast-liveness"]
+               for t in types do
+                       var reso
+                       if t.need_anchor then reso = "OPEN " else reso = "CLOSED"
+                       var live
+                       if t isa MClassType and (live_types.has(t) or live_open_types.has(t)) then live = "LIVE" else live = "DEAD"
+                       var cast
+                       if live_cast_types.has(t) or live_open_cast_types.has(t) then cast = "CAST LIVE" else cast = "CAST DEAD"
+                       res.add_line(t, reso, live, cast)
+               end
+               return res
+       end
+
+       # Return a ready-to-save OrderedTree object that agregates infomration about live methods.
+       # Note: methods are listed in an alphanumeric order to improve human reading.
+       fun live_methods_to_tree: OrderedTree[Object]
+       do
+               var tree = new OrderedTree[Object]
+               for x in live_methods do
+                       var xn = x.full_name
+                       tree.add(null, xn)
+                       for z in x.mpropdefs do
+                               var zn = z.to_s
+                               if live_methoddefs.has(z) then
+                                       tree.add(xn, zn)
+                                       if live_super_sends.has(z) then
+                                               tree.add(zn, zn + "(super)")
+                                       end
+                               else if live_super_sends.has(z) then
+                                       tree.add(xn, zn + "(super)")
+                               end
+                       end
+               end
+               tree.sort_with(alpha_comparator)
+               return tree
+       end
+
        # Methods that are are still candidate to the try_send
        private var totry_methods = new HashSet[MMethod]
 
        # The method definitions that remain to visit
        private var todo = new List[MMethodDef]
 
+       private fun force_alive(classname: String)
+       do
+               var classes = self.modelbuilder.model.get_mclasses_by_name(classname)
+               if classes != null then for c in classes do self.add_new(c.mclass_type, c.mclass_type)
+       end
+
        # Run the analysis until all visitable method definitions are visited.
        fun run_analysis
        do
@@ -96,9 +156,11 @@ class RapidTypeAnalysis
                        add_send(maintype, mainprop)
                end
 
-               # Force Bool
-               var classes = self.modelbuilder.model.get_mclasses_by_name("Bool")
-               if classes != null then for c in classes do self.add_new(c.mclass_type, c.mclass_type)
+               # Force primitive types
+               force_alive("Bool")
+               force_alive("Int")
+               force_alive("Float")
+               force_alive("Char")
 
                while not todo.is_empty do
                        var mmethoddef = todo.shift
@@ -150,7 +212,7 @@ class RapidTypeAnalysis
                                var auto_super_inits = npropdef.auto_super_inits
                                if auto_super_inits != null then
                                        for auto_super_init in auto_super_inits do
-                                               v.add_monomorphic_send(v.receiver, auto_super_init)
+                                               v.add_callsite(auto_super_init)
                                        end
                                end
                        else if npropdef isa AInternMethPropdef or
@@ -199,8 +261,6 @@ class RapidTypeAnalysis
                        for t in live_types do
                                if not ot.can_resolve_for(t, t, mainmodule) then continue
                                var rt = ot.anchor_to(mainmodule, t)
-                               if rt isa MNullableType then rt = rt.mtype
-                               assert rt isa MClassType
                                live_cast_types.add(rt)
                                #print "  {ot}/{t} -> {rt}"
                        end
@@ -253,11 +313,9 @@ class RapidTypeAnalysis
 
        fun add_cast(mtype: MType)
        do
-               if mtype isa MNullableType then mtype = mtype.mtype
                if mtype.need_anchor then
                        live_open_cast_types.add(mtype)
                else
-                       assert mtype isa MClassType
                        live_cast_types.add(mtype)
                end
        end
@@ -457,8 +515,7 @@ redef class ACrangeExpr
        do
                var mtype = self.mtype.as(MClassType)
                v.add_type(mtype)
-               var prop = v.get_method(mtype, "init")
-               v.add_monomorphic_send(mtype, prop)
+               v.add_callsite(init_callsite)
        end
 end
 
@@ -467,8 +524,7 @@ redef class AOrangeExpr
        do
                var mtype = self.mtype.as(MClassType)
                v.add_type(mtype)
-               var prop = v.get_method(mtype, "without_last")
-               v.add_monomorphic_send(mtype, prop)
+               v.add_callsite(init_callsite)
        end
 end
 
@@ -540,29 +596,24 @@ redef class ASuperExpr
                        return
                end
 
-               v.analysis.add_super_send(v.receiver, v.mpropdef.as(MMethodDef))
+               v.analysis.add_super_send(v.receiver, mpropdef.as(not null))
        end
 end
 
 redef class AForExpr
        redef fun accept_rapid_type_visitor(v)
        do
-               var recvtype = self.n_expr.mtype.as(not null)
-               var colltype = self.coltype.as(not null)
-               var itmeth = v.get_method(colltype, "iterator")
-               v.add_send(recvtype, itmeth)
-               var iteratortype = itmeth.intro.msignature.return_mtype.as(MClassType).mclass.intro.bound_mtype
-               var objtype = v.get_class("Object").mclass_type
-               v.add_send(objtype, v.get_method(iteratortype, "is_ok"))
+               v.add_callsite(self.method_iterator)
+               v.add_callsite(self.method_is_ok)
                if self.variables.length == 1 then
-                       v.add_send(objtype, v.get_method(iteratortype, "item"))
+                       v.add_callsite(self.method_item)
                else if self.variables.length == 2 then
-                       v.add_send(objtype, v.get_method(iteratortype, "key"))
-                       v.add_send(objtype, v.get_method(iteratortype, "item"))
+                       v.add_callsite(self.method_key)
+                       v.add_callsite(self.method_item)
                else
                        abort
                end
-               v.add_send(objtype, v.get_method(iteratortype, "next"))
+               v.add_callsite(self.method_next)
        end
 end