# See the License for the specific language governing permissions and
# limitations under the License.
-
# Rapid type analysis on the AST
#
# Rapid type analysis is an analyse that aproximates the set of live classes
module rapid_type_analysis
import semantize
+private import explain_assert_api
-private import csv # for live_types_to_csv
+import csv # for live_types_to_csv
private import ordered_tree # for live_methods_to_tree
private import more_collections
# live_methods to determine new methoddefs to visit
var live_types = new HashSet[MClassType]
- # The pool of undesolved live types
+ # The pool of unresolved live types
# They are globally resolved at the end of the analaysis
var live_open_types = new HashSet[MClassType]
# Live methods.
var live_methods = new HashSet[MMethod]
+ # Live mmodules.
+ # Those with a live method definitions (see `live_methoddefs`)
+ var live_mmodules = new HashSet[MModule]
+
# Live callsites.
var live_callsites = new HashSet[CallSite]
var types = typeset.to_a
(new CachedAlphaComparator).sort(types)
var res = new CsvDocument
- res.format = new CsvFormat('"', ';', "\n")
+ res.separator = ';'
res.header = ["Type", "Resolution", "Liveness", "Cast-liveness"]
for t in types do
var reso
# Force primitive types
force_alive("Bool")
- force_alive("Int")
force_alive("Float")
force_alive("Char")
force_alive("Pointer")
force_alive("Byte")
+ force_alive("Int")
+ force_alive("Int8")
+ force_alive("Int16")
+ force_alive("UInt16")
+ force_alive("Int32")
+ force_alive("UInt32")
while not todo.is_empty do
var mmethoddef = todo.shift
v.add_monomorphic_send(vararg, self.modelbuilder.force_get_primitive_method(node, "with_native", vararg.mclass, self.mainmodule))
end
- # TODO? new_msignature
var sig = msignature
var osig = mmeth.intro.msignature.as(not null)
for i in [0..sig.arity[ do
add_cast(paramtype)
end
+ if mmethoddef.is_abstract then continue
+
var npropdef = modelbuilder.mpropdef2node(mmethoddef)
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)
+ if mmethoddef.mproperty.is_root_init then
+ # Final init call
+ if not mmethoddef.is_intro then
+ self.add_super_send(v.receiver, mmethoddef)
+ end
+ else if mmethoddef.mclassdef.default_init == mmethoddef then
+ # default_init call
+ for i in mmethoddef.initializers do
+ if i isa MMethod then self.add_send(v.receiver, i)
+ end
+ else
+ npropdef.debug "cannot RTA {mmethoddef}"
+ abort
end
continue
else if mmethoddef.constant_value != null then
if not ot.can_resolve_for(t, t, mainmodule) then continue
var rt = ot.anchor_to(mainmodule, t)
if live_types.has(rt) then continue
+ if not rt.is_legal_in(mainmodule) then continue
if not check_depth(rt) then continue
#print "{ot}/{t} -> {rt}"
live_types.add(rt)
- todo_types.add(rt)
+ # unshift means a deep-first visit.
+ # So that the `check_depth` limit is reached sooner.
+ todo_types.unshift(rt)
end
end
#print "MType {live_types.length}: {live_types.join(", ")}"
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 not rt.is_legal_in(mainmodule) then continue
live_cast_types.add(rt)
#print " {ot}/{t} -> {rt}"
end
v.enter_visit(npropdef.n_block)
end
end
-
end
fun add_cast(mtype: MType)
do
if live_methoddefs.has(mpropdef) then return
live_methoddefs.add(mpropdef)
+ live_mmodules.add(mpropdef.mclassdef.mmodule)
todo.add(mpropdef)
var mproperty = mpropdef.mproperty
end
end
-redef class AIntExpr
- redef fun accept_rapid_type_visitor(v)
+redef class AExpr
+ # Make the `mtype` of the expression live
+ # Used by literals and instantiations
+ fun allocate_mtype(v: RapidTypeVisitor)
do
+ var mtype = self.mtype
+ if not mtype isa MClassType then return
v.add_type(self.mtype.as(MClassType))
end
end
-redef class AByteExpr
+redef class AIntegerExpr
redef fun accept_rapid_type_visitor(v)
do
- v.add_type(self.mtype.as(MClassType))
+ allocate_mtype(v)
end
end
redef class AFloatExpr
redef fun accept_rapid_type_visitor(v)
do
- v.add_type(self.mtype.as(MClassType))
+ allocate_mtype(v)
end
end
redef class ACharExpr
redef fun accept_rapid_type_visitor(v)
do
- v.add_type(self.mtype.as(MClassType))
+ allocate_mtype(v)
end
end
redef class AStringFormExpr
redef fun accept_rapid_type_visitor(v)
do
- var native = v.analysis.mainmodule.native_string_type
+ var native = v.analysis.mainmodule.c_string_type
v.add_type(native)
- var prop = v.get_method(native, "to_s_with_length")
+ var prop = v.get_method(native, "to_s_unsafe")
v.add_monomorphic_send(native, prop)
+ v.add_callsite(to_re)
+ v.add_callsite(ignore_case)
+ v.add_callsite(newline)
+ v.add_callsite(extended)
+ v.add_callsite(to_bytes_with_copy)
end
end
redef class ACrangeExpr
redef fun accept_rapid_type_visitor(v)
do
- var mtype = self.mtype.as(MClassType)
+ var mtype = self.mtype
+ if not mtype isa MClassType then return
v.add_type(mtype)
v.add_callsite(init_callsite)
end
redef class AOrangeExpr
redef fun accept_rapid_type_visitor(v)
do
- var mtype = self.mtype.as(MClassType)
+ var mtype = self.mtype
+ if not mtype isa MClassType then return
v.add_type(mtype)
v.add_callsite(init_callsite)
end
redef class ATrueExpr
redef fun accept_rapid_type_visitor(v)
do
- v.add_type(self.mtype.as(MClassType))
+ allocate_mtype(v)
end
end
redef class AFalseExpr
redef fun accept_rapid_type_visitor(v)
do
- v.add_type(self.mtype.as(MClassType))
+ allocate_mtype(v)
end
end
redef class AIsaExpr
redef fun accept_rapid_type_visitor(v)
do
- v.add_cast_type(self.cast_type.as(not null))
+ var cast_type = self.cast_type
+ if cast_type == null then return
+ v.add_cast_type(cast_type)
end
end
redef class AAsCastExpr
redef fun accept_rapid_type_visitor(v)
do
- v.add_cast_type(self.mtype.as(not null))
+ var mtype = self.mtype
+ if mtype == null then return
+ v.add_cast_type(mtype)
+ end
+end
+
+redef class AAssertExpr
+ redef fun accept_rapid_type_visitor(v)
+ do
+ if can_explain_assert(v.analysis.modelbuilder) then
+ var str = explain_assert_str
+ if str != null then str.accept_rapid_type_visitor(v)
+ end
+ end
+
+ # Does `modelbuilder` know the classes to build a superstring to explain a failed assert?
+ private fun can_explain_assert(modelbuilder: ModelBuilder): Bool
+ do
+ var nas = modelbuilder.model.get_mclasses_by_name("NativeArray")
+ if nas == null then return false
+
+ nas = modelbuilder.model.get_mclasses_by_name("Array")
+ if nas == null or nas.is_empty then return false
+
+ nas = modelbuilder.model.get_mclasses_by_name("String")
+ if nas == null or nas.is_empty then return false
+
+ return true
end
end
end
end
+redef class ACallrefExpr
+ redef fun accept_rapid_type_visitor(v)
+ do
+ super
+ v.add_type(mtype.as(MClassType))
+ end
+end
redef class ASendReassignFormExpr
redef fun accept_rapid_type_visitor(v)
end
end
-redef class AForExpr
+redef class AForGroup
redef fun accept_rapid_type_visitor(v)
do
v.add_callsite(self.method_iterator)