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 private fun force_alive
(classname
: String)
86 var classes
= self.modelbuilder
.model
.get_mclasses_by_name
(classname
)
87 if classes
!= null then for c
in classes
do self.add_new
(c
.mclass_type
, c
.mclass_type
)
90 # Run the analysis until all visitable method definitions are visited.
93 var maintype
= mainmodule
.sys_type
94 if maintype
== null then return # No entry point
95 add_new
(maintype
, maintype
)
96 var initprop
= mainmodule
.try_get_primitive_method
("init", maintype
.mclass
)
97 if initprop
!= null then
98 add_send
(maintype
, initprop
)
100 var mainprop
= mainmodule
.try_get_primitive_method
("main", maintype
.mclass
)
101 if mainprop
!= null then
102 add_send
(maintype
, mainprop
)
105 # Force primitive types
111 while not todo
.is_empty
do
112 var mmethoddef
= todo
.shift
113 #print "# visit {mmethoddef}"
114 var v
= new RapidTypeVisitor(self, mmethoddef
.mclassdef
.bound_mtype
, mmethoddef
)
116 var vararg_rank
= mmethoddef
.msignature
.vararg_rank
117 if vararg_rank
> -1 then
118 var node
= self.modelbuilder
.mpropdef2npropdef
[mmethoddef
]
119 var elttype
= mmethoddef
.msignature
.mparameters
[vararg_rank
].mtype
120 #elttype = elttype.anchor_to(self.mainmodule, v.receiver)
121 var vararg
= self.mainmodule
.get_primitive_class
("Array").get_mtype
([elttype
])
123 var native
= self.mainmodule
.get_primitive_class
("NativeArray").get_mtype
([elttype
])
125 v
.add_monomorphic_send
(vararg
, self.modelbuilder
.force_get_primitive_method
(node
, "with_native", vararg
.mclass
, self.mainmodule
))
129 for i
in [0..mmethoddef
.msignature
.arity
[ do
130 var origtype
= mmethoddef
.mproperty
.intro
.msignature
.mparameters
[i
].mtype
131 if not origtype
.need_anchor
then continue # skip non covariant stuff
132 var paramtype
= mmethoddef
.msignature
.mparameters
[i
].mtype
133 #paramtype = v.cleanup_type(paramtype).as(not null)
137 if not modelbuilder
.mpropdef2npropdef
.has_key
(mmethoddef
) then
138 # It is an init for a class?
139 if mmethoddef
.mproperty
.name
== "init" then
140 var nclassdef
= self.modelbuilder
.mclassdef2nclassdef
[mmethoddef
.mclassdef
]
141 var super_inits
= nclassdef
.super_inits
142 if super_inits
!= null then
143 #assert args.length == 1
144 for su
in super_inits
do
145 v
.add_monomorphic_send
(v
.receiver
, su
)
155 var npropdef
= modelbuilder
.mpropdef2npropdef
[mmethoddef
]
157 if npropdef
isa AConcreteMethPropdef then
158 var auto_super_inits
= npropdef
.auto_super_inits
159 if auto_super_inits
!= null then
160 for auto_super_init
in auto_super_inits
do
161 v
.add_monomorphic_send
(v
.receiver
, auto_super_init
)
164 else if npropdef
isa AInternMethPropdef or
165 (npropdef
isa AExternMethPropdef and npropdef
.n_extern
!= null) then
166 # UGLY: We force the "instantation" of the concrete return type if any
167 var ret
= mmethoddef
.msignature
.return_mtype
168 if ret
!= null and ret
isa MClassType and ret
.mclass
.kind
!= abstract_kind
and ret
.mclass
.kind
!= interface_kind
then
171 else if npropdef
isa AExternMethPropdef then
172 var nclassdef
= npropdef
.parent
.as(AClassdef)
173 v
.enter_visit
(npropdef
)
174 else if npropdef
isa AExternInitPropdef then
175 v
.add_type
(v
.receiver
)
180 v
.enter_visit
(npropdef
)
183 #print "MMethod {live_methods.length}: {live_methods.join(", ")}"
184 #print "MMethodDef {live_methoddefs.length}: {live_methoddefs.join(", ")}"
186 #print "open MType {live_open_types.length}: {live_open_types.join(", ")}"
187 var todo_types
= new List[MClassType]
188 todo_types
.add_all
(live_types
)
189 while not todo_types
.is_empty
do
190 var t
= todo_types
.shift
191 for ot
in live_open_types
do
193 if not ot
.can_resolve_for
(t
, t
, mainmodule
) then continue
194 var rt
= ot
.anchor_to
(mainmodule
, t
)
195 if live_types
.has
(rt
) then continue
196 #print "{ot}/{t} -> {rt}"
202 #print "MType {live_types.length}: {live_types.join(", ")}"
204 #print "open cast MType {live_open_cast_types.length}: {live_open_cast_types.join(", ")}"
205 for ot
in live_open_cast_types
do
206 #print "live_open_cast_type: {ot}"
207 for t
in live_types
do
208 if not ot
.can_resolve_for
(t
, t
, mainmodule
) then continue
209 var rt
= ot
.anchor_to
(mainmodule
, t
)
210 if rt
isa MNullableType then rt
= rt
.mtype
211 assert rt
isa MClassType
212 live_cast_types
.add
(rt
)
213 #print " {ot}/{t} -> {rt}"
216 #print "cast MType {live_cast_types.length}: {live_cast_types.join(", ")}"
219 private fun check_depth
(mtype
: MClassType)
223 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}.")
227 fun add_new
(recv
: MClassType, mtype
: MClassType)
229 assert not recv
.need_anchor
230 if mtype
.need_anchor
then
231 if live_open_types
.has
(mtype
) then return
232 live_open_types
.add
(mtype
)
234 if live_types
.has
(mtype
) then return
235 live_types
.add
(mtype
)
238 var mclass
= mtype
.mclass
239 if live_classes
.has
(mclass
) then return
240 live_classes
.add
(mclass
)
242 for p
in totry_methods
do try_send
(mtype
, p
)
243 for p
in live_super_sends
do try_super_send
(mtype
, p
)
245 var bound_mtype
= mtype
.anchor_to
(mainmodule
, recv
)
246 for cd
in bound_mtype
.collect_mclassdefs
(mainmodule
)
248 if not self.modelbuilder
.mclassdef2nclassdef
.has_key
(cd
) then continue
249 var nclassdef
= self.modelbuilder
.mclassdef2nclassdef
[cd
]
250 for npropdef
in nclassdef
.n_propdefs
do
251 if not npropdef
isa AAttrPropdef then continue
252 var nexpr
= npropdef
.n_expr
253 if nexpr
== null then continue
254 var mpropdef
= npropdef
.mpropdef
.as(not null)
255 var v
= new RapidTypeVisitor(self, bound_mtype
, mpropdef
)
262 fun add_cast
(mtype
: MType)
264 if mtype
isa MNullableType then mtype
= mtype
.mtype
265 if mtype
.need_anchor
then
266 live_open_cast_types
.add
(mtype
)
268 assert mtype
isa MClassType
269 live_cast_types
.add
(mtype
)
273 fun try_send
(recv
: MClassType, mproperty
: MMethod)
275 recv
= recv
.mclass
.intro
.bound_mtype
276 if not recv
.has_mproperty
(mainmodule
, mproperty
) then return
277 var d
= mproperty
.lookup_first_definition
(mainmodule
, recv
)
281 fun add_call
(mpropdef
: MMethodDef)
283 if live_methoddefs
.has
(mpropdef
) then return
284 live_methoddefs
.add
(mpropdef
)
287 var mproperty
= mpropdef
.mproperty
288 if mproperty
.mpropdefs
.length
<= 1 then return
289 # If all definitions of a method are live, we can remove the definition of the totry set
290 for d
in mproperty
.mpropdefs
do
291 if d
.is_abstract
then continue
292 if not live_methoddefs
.has
(d
) then return
294 #print "full property: {mpropdef.mproperty} for {mpropdef.mproperty.mpropdefs.length} definitions"
295 totry_methods
.remove
(mpropdef
.mproperty
)
298 fun add_send
(recv
: MType, mproperty
: MMethod)
300 if live_methods
.has
(mproperty
) then return
301 #print "new prop: {mproperty}"
302 live_methods
.add
(mproperty
)
303 if mproperty
.mpropdefs
.length
== 1 then
304 # If there is only one definition, just add the definition and do not try again the property
305 var d
= mproperty
.mpropdefs
.first
309 # Else, the property is potentially called with various reciever
310 # So just try the methods with existing receiver and register it for future receiver
311 totry_methods
.add
(mproperty
)
312 for c
in live_classes
do
313 try_send
(c
.intro
.bound_mtype
, mproperty
)
317 fun try_super_send
(recv
: MClassType, mpropdef
: MMethodDef)
319 recv
= recv
.mclass
.intro
.bound_mtype
320 if not recv
.collect_mclassdefs
(mainmodule
).has
(mpropdef
.mclassdef
) then return
321 var d
= mpropdef
.lookup_next_definition
(mainmodule
, recv
)
325 fun add_super_send
(recv
: MType, mpropdef
: MMethodDef)
327 if live_super_sends
.has
(mpropdef
) then return
328 #print "new super prop: {mpropdef}"
329 live_super_sends
.add
(mpropdef
)
330 for t
in live_types
do
331 try_super_send
(t
, mpropdef
)
336 class RapidTypeVisitor
339 var analysis
: RapidTypeAnalysis
340 var receiver
: MClassType
341 var mpropdef
: MPropDef
343 init(analysis
: RapidTypeAnalysis, receiver
: MClassType, mpropdef
: MPropDef)
345 self.analysis
= analysis
346 self.receiver
= receiver
347 self.mpropdef
= mpropdef
348 assert not receiver
.need_anchor
353 n
.accept_rapid_type_visitor
(self)
355 var implicit_cast_to
= n
.implicit_cast_to
356 if implicit_cast_to
!= null then self.add_cast_type
(implicit_cast_to
)
359 # RTA does not enter in AAnnotations
360 if not n
isa AAnnotations then
365 fun cleanup_type
(mtype
: MType): nullable MClassType
367 mtype
= mtype
.anchor_to
(self.analysis
.mainmodule
, self.receiver
)
368 if mtype
isa MNullType then return null
369 if mtype
isa MNullableType then mtype
= mtype
.mtype
370 assert mtype
isa MClassType
371 assert not mtype
.need_anchor
375 fun get_class
(name
: String): MClass
377 return analysis
.mainmodule
.get_primitive_class
(name
)
380 fun get_method
(recv
: MType, name
: String): MMethod
382 var mtype
= cleanup_type
(recv
)
384 return self.analysis
.modelbuilder
.force_get_primitive_method
(self.current_node
.as(not null), name
, mtype
.mclass
, self.analysis
.mainmodule
)
387 fun add_type
(mtype
: MClassType) do analysis
.add_new
(receiver
, mtype
)
389 fun add_monomorphic_send
(mtype
: MType, mproperty
: MMethod) do analysis
.try_send
(mtype
.as(MClassType), mproperty
)
391 fun add_send
(mtype
: MType, mproperty
: MMethod) do analysis
.add_send
(mtype
, mproperty
)
393 fun add_cast_type
(mtype
: MType) do analysis
.add_cast
(mtype
)
399 private fun accept_rapid_type_visitor
(v
: RapidTypeVisitor)
405 redef fun accept_rapid_type_visitor
(v
)
407 v
.add_type
(self.mtype
.as(MClassType))
411 redef class AFloatExpr
412 redef fun accept_rapid_type_visitor
(v
)
414 v
.add_type
(self.mtype
.as(MClassType))
418 redef class ACharExpr
419 redef fun accept_rapid_type_visitor
(v
)
421 v
.add_type
(self.mtype
.as(MClassType))
425 redef class AArrayExpr
426 redef fun accept_rapid_type_visitor
(v
)
428 var mtype
= self.mtype
.as(MClassType)
430 var native
= v
.analysis
.mainmodule
.get_primitive_class
("NativeArray").get_mtype
([mtype
.arguments
.first
])
432 mtype
= v
.cleanup_type
(mtype
).as(not null)
433 var prop
= v
.get_method
(mtype
, "with_native")
434 v
.add_monomorphic_send
(mtype
, prop
)
438 redef class AStringFormExpr
439 redef fun accept_rapid_type_visitor
(v
)
441 var native
= v
.get_class
("NativeString").mclass_type
443 var prop
= v
.get_method
(native
, "to_s_with_length")
444 v
.add_monomorphic_send
(native
, prop
)
448 redef class ASuperstringExpr
449 redef fun accept_rapid_type_visitor
(v
)
451 var arraytype
= v
.get_class
("Array").get_mtype
([v
.get_class
("Object").mclass_type
])
452 v
.add_type
(arraytype
)
453 v
.add_type
(v
.get_class
("NativeArray").get_mtype
([v
.get_class
("Object").mclass_type
]))
454 var prop
= v
.get_method
(arraytype
, "join")
455 v
.add_monomorphic_send
(arraytype
, prop
)
456 var prop2
= v
.get_method
(arraytype
, "with_native")
457 v
.add_monomorphic_send
(arraytype
, prop2
)
461 redef class ACrangeExpr
462 redef fun accept_rapid_type_visitor
(v
)
464 var mtype
= self.mtype
.as(MClassType)
466 var prop
= v
.get_method
(mtype
, "init")
467 v
.add_monomorphic_send
(mtype
, prop
)
471 redef class AOrangeExpr
472 redef fun accept_rapid_type_visitor
(v
)
474 var mtype
= self.mtype
.as(MClassType)
476 var prop
= v
.get_method
(mtype
, "without_last")
477 v
.add_monomorphic_send
(mtype
, prop
)
481 redef class ATrueExpr
482 redef fun accept_rapid_type_visitor
(v
)
484 v
.add_type
(self.mtype
.as(MClassType))
488 redef class AFalseExpr
489 redef fun accept_rapid_type_visitor
(v
)
491 v
.add_type
(self.mtype
.as(MClassType))
496 redef fun accept_rapid_type_visitor
(v
)
498 v
.add_cast_type
(self.cast_type
.as(not null))
502 redef class AAsCastExpr
503 redef fun accept_rapid_type_visitor
(v
)
505 v
.add_cast_type
(self.mtype
.as(not null))
509 redef class ASendExpr
510 redef fun accept_rapid_type_visitor
(v
)
512 var mproperty
= self.mproperty
.as(not null)
513 var recvtype
= self.n_expr
.mtype
.as(not null)
514 v
.add_send
(recvtype
, mproperty
)
519 redef class ASendReassignFormExpr
520 redef fun accept_rapid_type_visitor
(v
)
522 v
.add_send
(self.read_type
.as(not null), self.reassign_property
.mproperty
)
523 var mproperty
= self.mproperty
.as(not null)
524 var write_mproperty
= self.write_mproperty
.as(not null)
525 if n_expr
isa ASelfExpr then
526 v
.add_monomorphic_send
(v
.receiver
, mproperty
)
527 v
.add_monomorphic_send
(v
.receiver
, write_mproperty
)
529 var recvtype
= self.n_expr
.mtype
.as(not null)
530 v
.add_send
(recvtype
, mproperty
)
531 v
.add_send
(recvtype
, write_mproperty
)
536 redef class AVarReassignExpr
537 redef fun accept_rapid_type_visitor
(v
)
539 v
.add_send
(self.read_type
.as(not null), self.reassign_property
.mproperty
)
543 redef class AAttrReassignExpr
544 redef fun accept_rapid_type_visitor
(v
)
546 v
.add_send
(self.read_type
.as(not null), self.reassign_property
.mproperty
)
550 redef class ASuperExpr
551 redef fun accept_rapid_type_visitor
(v
)
553 var mproperty
= self.mproperty
554 if mproperty
!= null then
555 v
.add_monomorphic_send
(v
.receiver
, mproperty
)
559 v
.analysis
.add_super_send
(v
.receiver
, v
.mpropdef
.as(MMethodDef))
564 redef fun accept_rapid_type_visitor
(v
)
566 var recvtype
= self.n_expr
.mtype
.as(not null)
567 var colltype
= self.coltype
.as(not null)
568 var itmeth
= v
.get_method
(colltype
, "iterator")
569 v
.add_send
(recvtype
, itmeth
)
570 var iteratortype
= itmeth
.intro
.msignature
.return_mtype
.as(MClassType).mclass
.intro
.bound_mtype
571 var objtype
= v
.get_class
("Object").mclass_type
572 v
.add_send
(objtype
, v
.get_method
(iteratortype
, "is_ok"))
573 if self.variables
.length
== 1 then
574 v
.add_send
(objtype
, v
.get_method
(iteratortype
, "item"))
575 else if self.variables
.length
== 2 then
576 v
.add_send
(objtype
, v
.get_method
(iteratortype
, "key"))
577 v
.add_send
(objtype
, v
.get_method
(iteratortype
, "item"))
581 v
.add_send
(objtype
, v
.get_method
(iteratortype
, "next"))
586 redef fun accept_rapid_type_visitor
(v
)
588 var mtype
= self.mtype
.as(MClassType)
590 var mproperty
= self.mproperty
.as(not null)
591 v
.add_monomorphic_send
(mtype
, mproperty
)