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 with heterogenous generics and customization
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.
25 # Heterogenous generics means that each intancied generic class is associated
26 # to a distinct rapid type.
27 # Heterogenous generics has the advantage to resolve the formal generic
28 # parameters types but increase the number of types.
29 # More important, heterogenous generics cannot deal with infinite number of rapid
30 # types since the analyse tries to list them all (so some programs will be badly refused)
32 # Customization means that each method definition is analyzed one per rapid
34 # Customization have the following advantages:
35 # * `self' is monomorphic
36 # * virtual types are all resolved
37 # * live attributes can be determined on each class
38 # But has the disadvantage to explode the number of rapid method: each method
39 # definition for each rapid type that need it
40 module rapid_type_analysis
45 import auto_super_init
47 redef class ModelBuilder
48 fun do_rapid_type_analysis
(mainmodule
: MModule): RapidTypeAnalysis
50 var model
= self.model
51 var analysis
= new RapidTypeAnalysis(self, mainmodule
)
52 var nmodule
= self.nmodules
.first
53 var maintype
= mainmodule
.sys_type
54 if maintype
== null then return analysis
# No entry point
55 analysis
.add_type
(maintype
)
56 var initprop
= mainmodule
.try_get_primitive_method
("init", maintype
)
57 if initprop
!= null then
58 analysis
.add_monomorphic_send
(maintype
, initprop
)
60 var mainprop
= mainmodule
.try_get_primitive_method
("main", maintype
)
61 if mainprop
!= null then
62 analysis
.add_monomorphic_send
(maintype
, mainprop
)
69 # RapidTypeAnalysis looks for alive rapid types in application.
70 # The entry point of the analysis is the mainmodule of the application.
71 class RapidTypeAnalysis
72 # The modelbuilder used to get the AST.
73 var modelbuilder
: ModelBuilder
75 # The main module of the analysis.
76 # Used to perform types operations.
77 var mainmodule
: MModule
79 # The pool to live types.
80 # During the analysis, new types are added and combined with
81 # live_send_sites to determine new customized_methoddefs to visit
82 var live_types
: HashSet[MClassType] = new HashSet[MClassType]
84 # The pool of types used to perform type checks (isa and as).
85 var live_cast_types
: HashSet[MClassType] = new HashSet[MClassType]
87 # Live method definitions.
88 # These method definitions are:
89 # * visited trough a add_send on an already known live_type
90 # * visited trough a add_type for an already known live_send_sites
91 # * visided by a add_monomorphic_send or a add_static_call
92 var live_methoddefs
: HashSet[MMethodDef] = new HashSet[MMethodDef]
94 # The pool of live customized method definitions
95 var live_customized_methoddefs
: HashSet[CustomizedMethodDef] = new HashSet[CustomizedMethodDef]
97 # The pool of live RTA send site
98 # During the analysis, new live_send_sites are added and combined with
99 # live_types to determine new live_customized_methoddefs to visit
100 var live_send_sites
: HashSet[RTASendSite] = new HashSet[RTASendSite]
102 # The customized method definitions that remain to visit
103 private var todo
: List[CustomizedMethodDef] = new List[CustomizedMethodDef]
105 # Add a live type to the pool
107 # If the types is already live, then do nothing.
109 # REQUIRE: not mtype.need_anchor
110 fun add_type
(mtype
: MClassType)
112 if self.live_types
.has
(mtype
) then return
114 assert not mtype
.need_anchor
115 self.live_types
.add
(mtype
)
117 # Collect default attributes
118 for cd
in mtype
.collect_mclassdefs
(self.mainmodule
)
120 var nclassdef
= self.modelbuilder
.mclassdef2nclassdef
[cd
]
121 for npropdef
in nclassdef
.n_propdefs
do
122 if not npropdef
isa AAttrPropdef then continue
123 var nexpr
= npropdef
.n_expr
124 if nexpr
== null then continue
125 var v
= new RapidTypeVisitor(self, nclassdef
, npropdef
.mpropdef
.as(not null), mtype
)
130 for rss
in self.live_send_sites
do
131 if not mtype
.is_subtype
(self.mainmodule
, null, rss
.receiver
) then continue
132 if mtype
.has_mproperty
(self.mainmodule
, rss
.mmethod
) then
133 self.add_monomorphic_send
(mtype
, rss
.mmethod
)
138 # Add a send site to the pool
140 # If the send site is already live, then do nothing.
141 fun add_send
(mtype
: MClassType, mmethod
: MMethod)
143 var rss
= new RTASendSite(mmethod
, mtype
)
144 if self.live_send_sites
.has
(rss
) then return
146 self.live_send_sites
.add
(rss
)
148 for mtype2
in self.live_types
do
149 if not mtype2
.is_subtype
(self.mainmodule
, null, mtype
) then continue
150 if mtype2
.has_mproperty
(self.mainmodule
, mmethod
) then
151 self.add_monomorphic_send
(mtype2
, mmethod
)
156 # Add a monomoprhic send.
157 # The send site is not added to the pool.
158 # The method just determine the associated method definition then
159 # performs a static_call
160 fun add_monomorphic_send
(mtype
: MClassType, mmethod
: MMethod)
162 assert self.live_types
.has
(mtype
)
163 var defs
= mmethod
.lookup_definitions
(self.mainmodule
, mtype
)
164 if defs
.is_empty
then return
165 assert defs
.length
== 1 else print
"conflict on {mtype} for {mmethod}: {defs.join(" ")}"
166 self.add_static_call
(mtype
, defs
.first
)
169 # Add a customized_methoddefs to the pool
170 # Is the customized_methoddefs is already live, then do nothing
171 fun add_static_call
(mtype
: MClassType, mmethoddef
: MMethodDef)
173 assert self.live_types
.has
(mtype
)
174 var rm
= new CustomizedMethodDef(mmethoddef
, mtype
)
175 if self.live_customized_methoddefs
.has
(rm
) then return
176 self.live_customized_methoddefs
.add
(rm
)
178 self.live_methoddefs
.add
(mmethoddef
)
181 # Add mtype to the cast pool.
182 fun add_cast_type
(mtype
: MClassType)
184 if self.live_cast_types
.has
(mtype
) then return
186 assert not mtype
.need_anchor
187 self.live_cast_types
.add
(mtype
)
190 # Run the analysis until all visitable method definitions are visited.
194 var classes
= self.modelbuilder
.model
.get_mclasses_by_name
("Bool")
195 if classes
!= null then for c
in classes
do self.add_type
(c
.mclass_type
)
197 while not todo
.is_empty
do
200 var vararg_rank
= mr
.mmethoddef
.msignature
.vararg_rank
201 if vararg_rank
> -1 then
202 var node
= self.modelbuilder
.mpropdef2npropdef
[mr
.mmethoddef
]
203 var elttype
= mr
.mmethoddef
.msignature
.mparameters
[vararg_rank
].mtype
204 elttype
= elttype
.anchor_to
(self.mainmodule
, mr
.receiver
)
205 var vararg
= self.mainmodule
.get_primitive_class
("Array").get_mtype
([elttype
])
206 self.add_type
(vararg
)
207 self.add_monomorphic_send
(vararg
, self.modelbuilder
.force_get_primitive_method
(node
, "with_native", vararg
, self.mainmodule
))
208 var native
= self.mainmodule
.get_primitive_class
("NativeArray").get_mtype
([elttype
])
209 self.add_type
(native
)
212 if not self.modelbuilder
.mpropdef2npropdef
.has_key
(mr
.mmethoddef
) then
213 # It is an init for a class?
214 if mr
.mmethoddef
.mproperty
.name
== "init" then
215 var nclassdef
= self.modelbuilder
.mclassdef2nclassdef
[mr
.mmethoddef
.mclassdef
]
216 var super_inits
= nclassdef
.super_inits
217 if super_inits
!= null then
218 #assert args.length == 1
219 for su
in super_inits
do
220 self.add_monomorphic_send
(mr
.receiver
, su
)
229 var npropdef
= self.modelbuilder
.mpropdef2npropdef
[mr
.mmethoddef
]
230 if npropdef
isa AConcreteMethPropdef then
231 #npropdef.debug("Visit {mr.mmethoddef} for {mr.receiver}")
232 var nclassdef
= npropdef
.parent
.as(AClassdef)
233 var mmethoddef
= npropdef
.mpropdef
.as(not null)
234 var auto_super_inits
= npropdef
.auto_super_inits
235 if auto_super_inits
!= null then
236 for auto_super_init
in auto_super_inits
do
237 self.add_monomorphic_send
(mr
.receiver
, auto_super_init
)
240 var v
= new RapidTypeVisitor(self, nclassdef
, mmethoddef
, mr
.receiver
)
241 v
.enter_visit
(npropdef
.n_block
)
242 else if npropdef
isa ADeferredMethPropdef then
243 # nothing to do (maybe add a waring?)
244 else if npropdef
isa AAttrPropdef then
246 else if npropdef
isa AInternMethPropdef or npropdef
isa AExternMethPropdef then
247 # UGLY: We force the "instantation" of the concrete return type if any
248 var ret
= mr
.mmethoddef
.msignature
.return_mtype
249 if ret
!= null and ret
isa MClassType and ret
.mclass
.kind
!= abstract_kind
and ret
.mclass
.kind
!= interface_kind
then
250 ret
= ret
.anchor_to
(self.mainmodule
, mr
.receiver
)
253 else if npropdef
isa AExternInitPropdef then
254 self.add_type
(mr
.receiver
)
256 npropdef
.debug
("Not yet implemented")
263 # A method definitions customized to a specific receiver
264 class CustomizedMethodDef
265 var mmethoddef
: MMethodDef
266 var receiver
: MClassType
270 return "{self.mmethoddef}({receiver})"
275 return o
isa CustomizedMethodDef and o
.mmethoddef
== self.mmethoddef
and o
.receiver
== self.receiver
280 return self.mmethoddef
.hash
+ self.receiver
.hash
284 # A method invokation site bounded by a specific receiver
287 var receiver
: MClassType
291 return o
isa RTASendSite and o
.mmethod
== self.mmethod
and o
.receiver
== self.receiver
296 return self.mmethod
.hash
+ self.receiver
.hash
300 private class RapidTypeVisitor
303 var analysis
: RapidTypeAnalysis
305 var nclassdef
: AClassdef
307 var mpropdef
: MPropDef
309 var receiver
: MClassType
311 init(analysis
: RapidTypeAnalysis, nclassdef
: AClassdef, mpropdef
: MPropDef, receiver
: MClassType)
313 self.analysis
= analysis
314 self.nclassdef
= nclassdef
315 self.mpropdef
= mpropdef
316 self.receiver
= receiver
319 # Adapt and remove nullable
320 # return null if we got the null type
321 fun cleanup_type
(mtype
: MType): nullable MClassType
323 mtype
= mtype
.anchor_to
(self.analysis
.mainmodule
, self.receiver
)
324 if mtype
isa MNullType then return null
325 if mtype
isa MNullableType then mtype
= mtype
.mtype
326 assert mtype
isa MClassType
327 assert not mtype
.need_anchor
331 fun add_type
(mtype
: MType)
333 var rapidtype
= cleanup_type
(mtype
)
334 if rapidtype
== null then return # we do not care about null
336 self.analysis
.add_type
(rapidtype
)
337 #self.current_node.debug("add_type {rapidtype}")
340 fun add_send
(mtype
: MType, mproperty
: MMethod)
342 var rapidtype
= cleanup_type
(mtype
)
343 if rapidtype
== null then return # we do not care about null
345 analysis
.add_send
(rapidtype
, mproperty
)
346 #self.current_node.debug("add_send {mproperty}")
349 fun add_monomorphic_send
(mtype
: MType, mproperty
: MMethod)
351 var rapidtype
= cleanup_type
(mtype
)
352 if rapidtype
== null then return # we do not care about null
354 self.analysis
.add_monomorphic_send
(rapidtype
, mproperty
)
355 #self.current_node.debug("add_monomorphic_send {rapidtype} {mproperty}")
358 fun add_cast_type
(mtype
: MType)
360 var rapidtype
= cleanup_type
(mtype
)
361 if rapidtype
== null then return # we do not care about null
363 self.analysis
.add_cast_type
(rapidtype
)
364 #self.current_node.debug("add_cast_type {rapidtype}")
367 redef fun visit
(node
)
369 if node
== null then return
370 node
.accept_rapid_type_vistor
(self)
374 # Force to get the primitive class named `name' or abort
375 fun get_class
(name
: String): MClass
377 return self.analysis
.mainmodule
.get_primitive_class
(name
)
380 # Force to get the primitive property named `name' in the instance `recv' or abort
381 fun get_method
(recv
: MType, name
: String): MMethod
383 var rapidtype
= cleanup_type
(recv
)
384 if rapidtype
== null then abort
386 return self.analysis
.modelbuilder
.force_get_primitive_method
(self.current_node
.as(not null), name
, rapidtype
, self.analysis
.mainmodule
)
393 private fun accept_rapid_type_vistor
(v
: RapidTypeVisitor)
399 redef fun accept_rapid_type_vistor
(v
)
401 v
.add_type
(self.mtype
.as(not null))
405 redef class AFloatExpr
406 redef fun accept_rapid_type_vistor
(v
)
408 v
.add_type
(self.mtype
.as(not null))
412 redef class ACharExpr
413 redef fun accept_rapid_type_vistor
(v
)
415 v
.add_type
(self.mtype
.as(not null))
419 redef class AArrayExpr
420 redef fun accept_rapid_type_vistor
(v
)
422 var mtype
= self.mtype
.as(not null)
424 var native
= v
.get_class
("NativeArray").get_mtype
([mtype
.as(MGenericType).arguments
.first
])
426 var prop
= v
.get_method
(mtype
, "with_native")
427 v
.add_monomorphic_send
(mtype
, prop
)
431 redef class AStringFormExpr
432 redef fun accept_rapid_type_vistor
(v
)
434 var mtype
= self.mtype
.as(not null)
436 var native
= v
.get_class
("NativeString").mclass_type
438 var prop
= v
.get_method
(mtype
, "from_cstring")
439 v
.add_monomorphic_send
(mtype
, prop
)
443 redef class ASuperstringExpr
444 redef fun accept_rapid_type_vistor
(v
)
446 var arraytype
= v
.get_class
("Array").get_mtype
([v
.get_class
("Object").mclass_type
])
447 v
.add_type
(arraytype
)
448 v
.add_type
(v
.get_class
("NativeArray").get_mtype
([v
.get_class
("Object").mclass_type
]))
449 var prop
= v
.get_method
(arraytype
, "join")
450 v
.add_monomorphic_send
(arraytype
, prop
)
454 redef class ACrangeExpr
455 redef fun accept_rapid_type_vistor
(v
)
457 var mtype
= self.mtype
.as(not null)
459 var prop
= v
.get_method
(mtype
, "init")
460 v
.add_monomorphic_send
(mtype
, prop
)
464 redef class AOrangeExpr
465 redef fun accept_rapid_type_vistor
(v
)
467 var mtype
= self.mtype
.as(not null)
469 var prop
= v
.get_method
(mtype
, "without_last")
470 v
.add_monomorphic_send
(mtype
, prop
)
474 redef class ATrueExpr
475 redef fun accept_rapid_type_vistor
(v
)
477 v
.add_type
(self.mtype
.as(not null))
481 redef class AFalseExpr
482 redef fun accept_rapid_type_vistor
(v
)
484 v
.add_type
(self.mtype
.as(not null))
489 redef fun accept_rapid_type_vistor
(v
)
491 v
.add_cast_type
(self.cast_type
.as(not null))
495 redef class AAsCastExpr
496 redef fun accept_rapid_type_vistor
(v
)
498 v
.add_cast_type
(self.mtype
.as(not null))
504 redef class ASendExpr
505 redef fun accept_rapid_type_vistor
(v
)
507 var mproperty
= self.mproperty
.as(not null)
508 if n_expr
isa ASelfExpr then
509 v
.add_monomorphic_send
(v
.receiver
, mproperty
)
511 var recvtype
= self.n_expr
.mtype
.as(not null)
512 v
.add_send
(recvtype
, mproperty
)
517 redef class ASendReassignFormExpr
518 redef fun accept_rapid_type_vistor
(v
)
520 v
.add_send
(self.read_type
.as(not null), self.reassign_property
.mproperty
)
521 var mproperty
= self.mproperty
.as(not null)
522 var write_mproperty
= self.write_mproperty
.as(not null)
523 if n_expr
isa ASelfExpr then
524 v
.add_monomorphic_send
(v
.receiver
, mproperty
)
525 v
.add_monomorphic_send
(v
.receiver
, write_mproperty
)
527 var recvtype
= self.n_expr
.mtype
.as(not null)
528 v
.add_send
(recvtype
, mproperty
)
529 v
.add_send
(recvtype
, write_mproperty
)
534 redef class AVarReassignExpr
535 redef fun accept_rapid_type_vistor
(v
)
537 v
.add_send
(self.read_type
.as(not null), self.reassign_property
.mproperty
)
541 redef class AAttrReassignExpr
542 redef fun accept_rapid_type_vistor
(v
)
544 v
.add_send
(self.read_type
.as(not null), self.reassign_property
.mproperty
)
548 redef class ASuperExpr
549 redef fun accept_rapid_type_vistor
(v
)
551 var mproperty
= self.mproperty
552 if mproperty
!= null then
553 v
.add_monomorphic_send
(v
.receiver
, mproperty
)
557 #FIXME: we do not want an ugly static call!
558 var mpropdef
= v
.mpropdef
559 var mpropdefs
= mpropdef
.mproperty
.lookup_super_definitions
(mpropdef
.mclassdef
.mmodule
, mpropdef
.mclassdef
.bound_mtype
)
560 if mpropdefs
.length
!= 1 then
561 debug
("MPRODFEFS for super {mpropdef} for {v.receiver}: {mpropdefs.join(", ")}")
563 var msuperpropdef
= mpropdefs
.first
564 assert msuperpropdef
isa MMethodDef
565 v
.analysis
.add_static_call
(v
.receiver
, msuperpropdef
)
570 redef fun accept_rapid_type_vistor
(v
)
572 var recvtype
= self.n_expr
.mtype
.as(not null)
573 var colltype
= self.coltype
.as(not null)
574 var itmeth
= v
.get_method
(colltype
, "iterator")
575 v
.add_send
(recvtype
, itmeth
)
576 var iteratortype
= itmeth
.intro
.msignature
.return_mtype
.as(MClassType).mclass
.mclassdefs
.first
.bound_mtype
577 var objtype
= v
.get_class
("Object").mclass_type
578 v
.add_send
(objtype
, v
.get_method
(iteratortype
, "is_ok"))
579 if self.variables
.length
== 1 then
580 v
.add_send
(objtype
, v
.get_method
(iteratortype
, "item"))
581 else if self.variables
.length
== 2 then
582 v
.add_send
(objtype
, v
.get_method
(iteratortype
, "key"))
583 v
.add_send
(objtype
, v
.get_method
(iteratortype
, "item"))
587 v
.add_send
(objtype
, v
.get_method
(iteratortype
, "next"))
592 redef fun accept_rapid_type_vistor
(v
)
594 var recvtype
= self.mtype
.as(not null)
596 v
.add_monomorphic_send
(recvtype
, mproperty
.as(not null))