rta: do not drop abstract methods. abstract method can be live in Nit.
[nit.git] / src / rapid_type_analysis.nit
index 3d8fb9e..434c0e0 100644 (file)
@@ -31,6 +31,7 @@ private import ordered_tree # for live_methods_to_tree
 private import more_collections
 
 redef class ModelBuilder
+       # Performs a rapid-type-analysis on the program associated with `mainmodule`.
        fun do_rapid_type_analysis(mainmodule: MModule): RapidTypeAnalysis
        do
                var analysis = new RapidTypeAnalysis(self, mainmodule)
@@ -86,8 +87,7 @@ class RapidTypeAnalysis
                var anchor = callsite.anchor
                if anchor != null then mtype = mtype.anchor_to(callsite.mmodule, anchor)
                mtype = mtype.as_notnullable
-               assert mtype isa MClassType
-               mtype = mtype.mclass.intro.bound_mtype
+               if mtype isa MClassType then mtype = mtype.mclass.intro.bound_mtype
                var mproperty = callsite.mproperty
                var res = live_targets_cache[mtype, mproperty]
                if res != null then return res
@@ -110,7 +110,7 @@ class RapidTypeAnalysis
        # 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
+       fun live_types_to_csv: CsvDocument
        do
                # Gather all kind of type
                var typeset = new HashSet[MType]
@@ -120,7 +120,8 @@ class RapidTypeAnalysis
                typeset.add_all(live_open_cast_types)
                var types = typeset.to_a
                (new CachedAlphaComparator).sort(types)
-               var res = new CSVDocument
+               var res = new CsvDocument
+               res.format = new CsvFormat('"', ';', "\n")
                res.header = ["Type", "Resolution", "Liveness", "Cast-liveness"]
                for t in types do
                        var reso
@@ -129,7 +130,7 @@ class RapidTypeAnalysis
                        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)
+                       res.add_record(t, reso, live, cast)
                end
                return res
        end
@@ -214,7 +215,7 @@ class RapidTypeAnalysis
 
                        var vararg_rank = mmethoddef.msignature.vararg_rank
                        if vararg_rank > -1 then
-                               var node = self.modelbuilder.mpropdef2npropdef[mmethoddef]
+                               var node = self.modelbuilder.mpropdef2node(mmethoddef)
                                var elttype = mmethoddef.msignature.mparameters[vararg_rank].mtype
                                #elttype = elttype.anchor_to(self.mainmodule, v.receiver)
                                var vararg = self.mainmodule.get_primitive_class("Array").get_mtype([elttype])
@@ -234,31 +235,25 @@ class RapidTypeAnalysis
                                add_cast(paramtype)
                        end
 
-                       if not modelbuilder.mpropdef2npropdef.has_key(mmethoddef) then
-                               # It is an init for a class?
-                               if mmeth.name == "init" then
-                                       var nclassdef = self.modelbuilder.mclassdef2nclassdef[mmethoddef.mclassdef]
-                                       assert mmethoddef == nclassdef.mfree_init
-                                       var super_inits = nclassdef.super_inits
-                                       if super_inits != null then
-                                               #assert args.length == 1
-                                               for su in super_inits do
-                                                       v.add_monomorphic_send(v.receiver, su)
-                                               end
-                                       end
+                       var npropdef = modelbuilder.mpropdef2node(mmethoddef)
 
-                                       if mmethoddef.mproperty.is_root_init and not mmethoddef.is_intro then
-                                               self.add_super_send(v.receiver, mmethoddef)
-                                       end
-                               else
-                                       abort
+                       if npropdef isa AClassdef then
+                               # It is an init for a class
+                               assert mmethoddef == npropdef.mfree_init
+
+                               if mmethoddef.mproperty.is_root_init and not mmethoddef.is_intro then
+                                       self.add_super_send(v.receiver, mmethoddef)
                                end
                                continue
+                       else if mmethoddef.constant_value != null then
+                               # Make the return type live
+                               v.add_type(mmethoddef.msignature.return_mtype.as(MClassType))
+                               continue
+                       else if npropdef == null then
+                               abort
                        end
 
-                       var npropdef = modelbuilder.mpropdef2npropdef[mmethoddef]
-
-                       if npropdef isa AMethPropdef  then
+                       if npropdef isa AMethPropdef then
                                var auto_super_inits = npropdef.auto_super_inits
                                if auto_super_inits != null then
                                        for auto_super_init in auto_super_inits do
@@ -270,9 +265,7 @@ class RapidTypeAnalysis
                                end
                        end
 
-                       if mmeth.is_new then
-                               v.add_type(v.receiver)
-                       else if mmethoddef.is_intern or mmethoddef.is_extern then
+                       if mmethoddef.is_intern or mmethoddef.is_extern then
                                # UGLY: We force the "instantation" of the concrete return type if any
                                var ret = mmethoddef.msignature.return_mtype
                                if ret != null and ret isa MClassType and ret.mclass.kind != abstract_kind and ret.mclass.kind != interface_kind then
@@ -350,15 +343,13 @@ class RapidTypeAnalysis
                var bound_mtype = mtype.anchor_to(mainmodule, recv)
                for cd in bound_mtype.collect_mclassdefs(mainmodule)
                do
-                       if not self.modelbuilder.mclassdef2nclassdef.has_key(cd) then continue
-                       var nclassdef = self.modelbuilder.mclassdef2nclassdef[cd]
-                       for npropdef in nclassdef.n_propdefs do
-                               if not npropdef isa AAttrPropdef then continue
-                               var nexpr = npropdef.n_expr
-                               if nexpr == null then continue
+                       for npropdef in modelbuilder.collect_attr_propdef(cd) do
+                               if not npropdef.has_value then continue
+
                                var mpropdef = npropdef.mpropdef.as(not null)
                                var v = new RapidTypeVisitor(self, bound_mtype, mpropdef)
-                               v.enter_visit(nexpr)
+                               v.enter_visit(npropdef.n_expr)
+                               v.enter_visit(npropdef.n_block)
                        end
                end
 
@@ -391,7 +382,6 @@ class RapidTypeAnalysis
                if mproperty.mpropdefs.length <= 1 then return
                # If all definitions of a method are live, we can remove the definition of the totry set
                for d in mproperty.mpropdefs do
-                       if d.is_abstract then continue
                        if not live_methoddefs.has(d) then return
                end
                #print "full property: {mpropdef.mproperty} for {mpropdef.mproperty.mpropdefs.length} definitions"
@@ -445,11 +435,8 @@ class RapidTypeVisitor
        var receiver: MClassType
        var mpropdef: MPropDef
 
-       init(analysis: RapidTypeAnalysis, receiver: MClassType, mpropdef: MPropDef)
+       init
        do
-               self.analysis = analysis
-               self.receiver = receiver
-               self.mpropdef = mpropdef
                assert not receiver.need_anchor
        end
 
@@ -681,13 +668,15 @@ redef class AForExpr
                        abort
                end
                v.add_callsite(self.method_next)
+               var mf = self.method_finish
+               if mf != null then v.add_callsite(mf)
        end
 end
 
 redef class ANewExpr
        redef fun accept_rapid_type_visitor(v)
        do
-               var mtype = self.mtype.as(MClassType)
+               var mtype = self.recvtype.as(not null)
                v.add_type(mtype)
                v.add_callsite(callsite)
        end