1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012 Jean Privat <jean@pryen.org>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
18 # Rapid type analysis on the AST
20 # Rapid type analysis is an analyse that aproximates the set of live classes
21 # and the set of live methods starting from the entry point of the program.
22 # These two sets are interdependant and computed together.
23 # It is quite efficient but the type set is global and pollutes each call site.
24 module rapid_type_analysis
29 import auto_super_init
31 redef class ModelBuilder
32 fun do_rapid_type_analysis
(mainmodule
: MModule): RapidTypeAnalysis
34 var analysis
= new RapidTypeAnalysis(self, mainmodule
)
40 # RapidTypeAnalysis looks for alive rapid types in application.
41 # The entry point of the analysis is the mainmodule of the application.
42 class RapidTypeAnalysis
43 # The modelbuilder used to get the AST.
44 var modelbuilder
: ModelBuilder
46 # The main module of the analysis.
47 # Used to perform types operations.
48 var mainmodule
: MModule
50 # The pool to live types.
51 # During the analysis, new types are added and combined with
52 # live_methods to determine new methoddefs to visit
53 var live_types
= new HashSet[MClassType]
55 # The pool of undesolved live types
56 # They are globally resolved at the end of the analaysis
57 var live_open_types
= new HashSet[MClassType]
59 # Live (instantiated) classes.
60 var live_classes
= new HashSet[MClass]
62 # The pool of types used to perform type checks (isa and as).
63 var live_cast_types
= new HashSet[MClassType]
65 # The pool of undesolved types used to perform type checks (isa and as).
66 # They are globally resolved at the end of the analaysis
67 var live_open_cast_types
= new HashSet[MType]
69 # Live method definitions.
70 var live_methoddefs
= new HashSet[MMethodDef]
73 var live_methods
= new HashSet[MMethod]
76 var live_super_sends
= new HashSet[MMethodDef]
78 # Methods that are are still candidate to the try_send
79 private var totry_methods
= new HashSet[MMethod]
81 # The method definitions that remain to visit
82 private var todo
= new List[MMethodDef]
84 # Run the analysis until all visitable method definitions are visited.
87 var maintype
= mainmodule
.sys_type
88 if maintype
== null then return # No entry point
89 add_new
(maintype
, maintype
)
90 var initprop
= mainmodule
.try_get_primitive_method
("init", maintype
.mclass
)
91 if initprop
!= null then
92 add_send
(maintype
, initprop
)
94 var mainprop
= mainmodule
.try_get_primitive_method
("main", maintype
.mclass
)
95 if mainprop
!= null then
96 add_send
(maintype
, mainprop
)
100 var classes
= self.modelbuilder
.model
.get_mclasses_by_name
("Bool")
101 if classes
!= null then for c
in classes
do self.add_new
(c
.mclass_type
, c
.mclass_type
)
103 while not todo
.is_empty
do
104 var mmethoddef
= todo
.shift
105 #print "# visit {mmethoddef}"
106 var v
= new RapidTypeVisitor(self, mmethoddef
.mclassdef
.bound_mtype
, mmethoddef
)
108 var vararg_rank
= mmethoddef
.msignature
.vararg_rank
109 if vararg_rank
> -1 then
110 var node
= self.modelbuilder
.mpropdef2npropdef
[mmethoddef
]
111 var elttype
= mmethoddef
.msignature
.mparameters
[vararg_rank
].mtype
112 #elttype = elttype.anchor_to(self.mainmodule, v.receiver)
113 var vararg
= self.mainmodule
.get_primitive_class
("Array").get_mtype
([elttype
])
115 var native
= self.mainmodule
.get_primitive_class
("NativeArray").get_mtype
([elttype
])
117 v
.add_monomorphic_send
(vararg
, self.modelbuilder
.force_get_primitive_method
(node
, "with_native", vararg
.mclass
, self.mainmodule
))
121 for i
in [0..mmethoddef
.msignature
.arity
[ do
122 var origtype
= mmethoddef
.mproperty
.intro
.msignature
.mparameters
[i
].mtype
123 if not origtype
.need_anchor
then continue # skip non covariant stuff
124 var paramtype
= mmethoddef
.msignature
.mparameters
[i
].mtype
125 #paramtype = v.cleanup_type(paramtype).as(not null)
129 if not modelbuilder
.mpropdef2npropdef
.has_key
(mmethoddef
) then
130 # It is an init for a class?
131 if mmethoddef
.mproperty
.name
== "init" then
132 var nclassdef
= self.modelbuilder
.mclassdef2nclassdef
[mmethoddef
.mclassdef
]
133 var super_inits
= nclassdef
.super_inits
134 if super_inits
!= null then
135 #assert args.length == 1
136 for su
in super_inits
do
137 v
.add_monomorphic_send
(v
.receiver
, su
)
147 var npropdef
= modelbuilder
.mpropdef2npropdef
[mmethoddef
]
149 if npropdef
isa AConcreteMethPropdef then
150 var auto_super_inits
= npropdef
.auto_super_inits
151 if auto_super_inits
!= null then
152 for auto_super_init
in auto_super_inits
do
153 v
.add_monomorphic_send
(v
.receiver
, auto_super_init
)
156 else if npropdef
isa AInternMethPropdef or
157 (npropdef
isa AExternMethPropdef and npropdef
.n_extern
!= null) then
158 # UGLY: We force the "instantation" of the concrete return type if any
159 var ret
= mmethoddef
.msignature
.return_mtype
160 if ret
!= null and ret
isa MClassType and ret
.mclass
.kind
!= abstract_kind
and ret
.mclass
.kind
!= interface_kind
then
163 else if npropdef
isa AExternMethPropdef then
164 var nclassdef
= npropdef
.parent
.as(AClassdef)
165 v
.enter_visit
(npropdef
)
166 else if npropdef
isa AExternInitPropdef then
167 v
.add_type
(v
.receiver
)
172 v
.enter_visit
(npropdef
)
175 #print "MMethod {live_methods.length}: {live_methods.join(", ")}"
176 #print "MMethodDef {live_methoddefs.length}: {live_methoddefs.join(", ")}"
178 #print "open MType {live_open_types.length}: {live_open_types.join(", ")}"
179 var todo_types
= new List[MClassType]
180 todo_types
.add_all
(live_types
)
181 while not todo_types
.is_empty
do
182 var t
= todo_types
.shift
183 for ot
in live_open_types
do
185 if not ot
.can_resolve_for
(t
, t
, mainmodule
) then continue
186 var rt
= ot
.anchor_to
(mainmodule
, t
)
187 if live_types
.has
(rt
) then continue
188 #print "{ot}/{t} -> {rt}"
194 #print "MType {live_types.length}: {live_types.join(", ")}"
196 #print "open cast MType {live_open_cast_types.length}: {live_open_cast_types.join(", ")}"
197 for ot
in live_open_cast_types
do
198 #print "live_open_cast_type: {ot}"
199 for t
in live_types
do
200 if not ot
.can_resolve_for
(t
, t
, mainmodule
) then continue
201 var rt
= ot
.anchor_to
(mainmodule
, t
)
202 if rt
isa MNullableType then rt
= rt
.mtype
203 assert rt
isa MClassType
204 live_cast_types
.add
(rt
)
205 #print " {ot}/{t} -> {rt}"
208 #print "cast MType {live_cast_types.length}: {live_cast_types.join(", ")}"
211 private fun check_depth
(mtype
: MClassType)
215 self.modelbuilder
.toolcontext
.fatal_error
(null, "Fatal error: limitation in the rapidtype analysis engine: a type depth of {d} is too important, the problematic type is {mtype}.")
219 fun add_new
(recv
: MClassType, mtype
: MClassType)
221 assert not recv
.need_anchor
222 if mtype
.need_anchor
then
223 if live_open_types
.has
(mtype
) then return
224 live_open_types
.add
(mtype
)
226 if live_types
.has
(mtype
) then return
227 live_types
.add
(mtype
)
230 var mclass
= mtype
.mclass
231 if live_classes
.has
(mclass
) then return
232 live_classes
.add
(mclass
)
234 for p
in totry_methods
do try_send
(mtype
, p
)
235 for p
in live_super_sends
do try_super_send
(mtype
, p
)
237 var bound_mtype
= mtype
.anchor_to
(mainmodule
, recv
)
238 for cd
in bound_mtype
.collect_mclassdefs
(mainmodule
)
240 if not self.modelbuilder
.mclassdef2nclassdef
.has_key
(cd
) then continue
241 var nclassdef
= self.modelbuilder
.mclassdef2nclassdef
[cd
]
242 for npropdef
in nclassdef
.n_propdefs
do
243 if not npropdef
isa AAttrPropdef then continue
244 var nexpr
= npropdef
.n_expr
245 if nexpr
== null then continue
246 var mpropdef
= npropdef
.mpropdef
.as(not null)
247 var v
= new RapidTypeVisitor(self, bound_mtype
, mpropdef
)
254 fun add_cast
(mtype
: MType)
256 if mtype
isa MNullableType then mtype
= mtype
.mtype
257 if mtype
.need_anchor
then
258 live_open_cast_types
.add
(mtype
)
260 assert mtype
isa MClassType
261 live_cast_types
.add
(mtype
)
265 fun try_send
(recv
: MClassType, mproperty
: MMethod)
267 recv
= recv
.mclass
.intro
.bound_mtype
268 if not recv
.has_mproperty
(mainmodule
, mproperty
) then return
269 var d
= mproperty
.lookup_first_definition
(mainmodule
, recv
)
273 fun add_call
(mpropdef
: MMethodDef)
275 if live_methoddefs
.has
(mpropdef
) then return
276 live_methoddefs
.add
(mpropdef
)
279 var mproperty
= mpropdef
.mproperty
280 if mproperty
.mpropdefs
.length
<= 1 then return
281 # If all definitions of a method are live, we can remove the definition of the totry set
282 for d
in mproperty
.mpropdefs
do
283 if d
.is_abstract
then continue
284 if not live_methoddefs
.has
(d
) then return
286 #print "full property: {mpropdef.mproperty} for {mpropdef.mproperty.mpropdefs.length} definitions"
287 totry_methods
.remove
(mpropdef
.mproperty
)
290 fun add_send
(recv
: MType, mproperty
: MMethod)
292 if live_methods
.has
(mproperty
) then return
293 #print "new prop: {mproperty}"
294 live_methods
.add
(mproperty
)
295 if mproperty
.mpropdefs
.length
== 1 then
296 # If there is only one definition, just add the definition and do not try again the property
297 var d
= mproperty
.mpropdefs
.first
301 # Else, the property is potentially called with various reciever
302 # So just try the methods with existing receiver and register it for future receiver
303 totry_methods
.add
(mproperty
)
304 for c
in live_classes
do
305 try_send
(c
.intro
.bound_mtype
, mproperty
)
309 fun try_super_send
(recv
: MClassType, mpropdef
: MMethodDef)
311 recv
= recv
.mclass
.intro
.bound_mtype
312 if not recv
.collect_mclassdefs
(mainmodule
).has
(mpropdef
.mclassdef
) then return
313 var d
= mpropdef
.lookup_next_definition
(mainmodule
, recv
)
317 fun add_super_send
(recv
: MType, mpropdef
: MMethodDef)
319 if live_super_sends
.has
(mpropdef
) then return
320 #print "new super prop: {mpropdef}"
321 live_super_sends
.add
(mpropdef
)
322 for t
in live_types
do
323 try_super_send
(t
, mpropdef
)
328 class RapidTypeVisitor
331 var analysis
: RapidTypeAnalysis
332 var receiver
: MClassType
333 var mpropdef
: MPropDef
335 init(analysis
: RapidTypeAnalysis, receiver
: MClassType, mpropdef
: MPropDef)
337 self.analysis
= analysis
338 self.receiver
= receiver
339 self.mpropdef
= mpropdef
340 assert not receiver
.need_anchor
345 n
.accept_rapid_type_visitor
(self)
347 var implicit_cast_to
= n
.implicit_cast_to
348 if implicit_cast_to
!= null then self.add_cast_type
(implicit_cast_to
)
351 # RTA does not enter in AAnnotations
352 if not n
isa AAnnotations then
357 fun cleanup_type
(mtype
: MType): nullable MClassType
359 mtype
= mtype
.anchor_to
(self.analysis
.mainmodule
, self.receiver
)
360 if mtype
isa MNullType then return null
361 if mtype
isa MNullableType then mtype
= mtype
.mtype
362 assert mtype
isa MClassType
363 assert not mtype
.need_anchor
367 fun get_class
(name
: String): MClass
369 return analysis
.mainmodule
.get_primitive_class
(name
)
372 fun get_method
(recv
: MType, name
: String): MMethod
374 var mtype
= cleanup_type
(recv
)
376 return self.analysis
.modelbuilder
.force_get_primitive_method
(self.current_node
.as(not null), name
, mtype
.mclass
, self.analysis
.mainmodule
)
379 fun add_type
(mtype
: MClassType) do analysis
.add_new
(receiver
, mtype
)
381 fun add_monomorphic_send
(mtype
: MType, mproperty
: MMethod) do analysis
.try_send
(mtype
.as(MClassType), mproperty
)
383 fun add_send
(mtype
: MType, mproperty
: MMethod) do analysis
.add_send
(mtype
, mproperty
)
385 fun add_cast_type
(mtype
: MType) do analysis
.add_cast
(mtype
)
387 fun add_callsite
(callsite
: nullable CallSite) do if callsite
!= null then analysis
.add_send
(callsite
.recv
, callsite
.mproperty
)
393 private fun accept_rapid_type_visitor
(v
: RapidTypeVisitor)
399 redef fun accept_rapid_type_visitor
(v
)
401 v
.add_type
(self.mtype
.as(MClassType))
405 redef class AFloatExpr
406 redef fun accept_rapid_type_visitor
(v
)
408 v
.add_type
(self.mtype
.as(MClassType))
412 redef class ACharExpr
413 redef fun accept_rapid_type_visitor
(v
)
415 v
.add_type
(self.mtype
.as(MClassType))
419 redef class AArrayExpr
420 redef fun accept_rapid_type_visitor
(v
)
422 var mtype
= self.mtype
.as(MClassType)
424 var native
= v
.analysis
.mainmodule
.get_primitive_class
("NativeArray").get_mtype
([mtype
.arguments
.first
])
426 mtype
= v
.cleanup_type
(mtype
).as(not null)
427 var prop
= v
.get_method
(mtype
, "with_native")
428 v
.add_monomorphic_send
(mtype
, prop
)
432 redef class AStringFormExpr
433 redef fun accept_rapid_type_visitor
(v
)
435 var native
= v
.get_class
("NativeString").mclass_type
437 var prop
= v
.get_method
(native
, "to_s_with_length")
438 v
.add_monomorphic_send
(native
, prop
)
442 redef class ASuperstringExpr
443 redef fun accept_rapid_type_visitor
(v
)
445 var arraytype
= v
.get_class
("Array").get_mtype
([v
.get_class
("Object").mclass_type
])
446 v
.add_type
(arraytype
)
447 v
.add_type
(v
.get_class
("NativeArray").get_mtype
([v
.get_class
("Object").mclass_type
]))
448 var prop
= v
.get_method
(arraytype
, "join")
449 v
.add_monomorphic_send
(arraytype
, prop
)
450 var prop2
= v
.get_method
(arraytype
, "with_native")
451 v
.add_monomorphic_send
(arraytype
, prop2
)
455 redef class ACrangeExpr
456 redef fun accept_rapid_type_visitor
(v
)
458 var mtype
= self.mtype
.as(MClassType)
460 var prop
= v
.get_method
(mtype
, "init")
461 v
.add_monomorphic_send
(mtype
, prop
)
465 redef class AOrangeExpr
466 redef fun accept_rapid_type_visitor
(v
)
468 var mtype
= self.mtype
.as(MClassType)
470 var prop
= v
.get_method
(mtype
, "without_last")
471 v
.add_monomorphic_send
(mtype
, prop
)
475 redef class ATrueExpr
476 redef fun accept_rapid_type_visitor
(v
)
478 v
.add_type
(self.mtype
.as(MClassType))
482 redef class AFalseExpr
483 redef fun accept_rapid_type_visitor
(v
)
485 v
.add_type
(self.mtype
.as(MClassType))
490 redef fun accept_rapid_type_visitor
(v
)
492 v
.add_cast_type
(self.cast_type
.as(not null))
496 redef class AAsCastExpr
497 redef fun accept_rapid_type_visitor
(v
)
499 v
.add_cast_type
(self.mtype
.as(not null))
503 redef class ASendExpr
504 redef fun accept_rapid_type_visitor
(v
)
506 v
.add_callsite
(callsite
)
511 redef class ASendReassignFormExpr
512 redef fun accept_rapid_type_visitor
(v
)
514 v
.add_callsite
(callsite
)
515 v
.add_callsite
(reassign_callsite
)
516 v
.add_callsite
(write_callsite
)
520 redef class AVarReassignExpr
521 redef fun accept_rapid_type_visitor
(v
)
523 v
.add_callsite
(reassign_callsite
)
527 redef class AAttrReassignExpr
528 redef fun accept_rapid_type_visitor
(v
)
530 v
.add_callsite
(reassign_callsite
)
534 redef class ASuperExpr
535 redef fun accept_rapid_type_visitor
(v
)
537 var callsite
= self.callsite
538 if callsite
!= null then
539 v
.add_callsite
(callsite
)
543 v
.analysis
.add_super_send
(v
.receiver
, v
.mpropdef
.as(MMethodDef))
548 redef fun accept_rapid_type_visitor
(v
)
550 var recvtype
= self.n_expr
.mtype
.as(not null)
551 var colltype
= self.coltype
.as(not null)
552 var itmeth
= v
.get_method
(colltype
, "iterator")
553 v
.add_send
(recvtype
, itmeth
)
554 var iteratortype
= itmeth
.intro
.msignature
.return_mtype
.as(MClassType).mclass
.intro
.bound_mtype
555 var objtype
= v
.get_class
("Object").mclass_type
556 v
.add_send
(objtype
, v
.get_method
(iteratortype
, "is_ok"))
557 if self.variables
.length
== 1 then
558 v
.add_send
(objtype
, v
.get_method
(iteratortype
, "item"))
559 else if self.variables
.length
== 2 then
560 v
.add_send
(objtype
, v
.get_method
(iteratortype
, "key"))
561 v
.add_send
(objtype
, v
.get_method
(iteratortype
, "item"))
565 v
.add_send
(objtype
, v
.get_method
(iteratortype
, "next"))
570 redef fun accept_rapid_type_visitor
(v
)
572 var mtype
= self.mtype
.as(MClassType)
574 v
.add_callsite
(callsite
)