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 # Adapt and remove nullable
106 # return null if we got the null type
107 fun cleanup_type
(mtype
: MType, recvtype
: MClassType): nullable MClassType
109 mtype
= mtype
.anchor_to
(self.mainmodule
, recvtype
)
110 if mtype
isa MNullType then return null
111 if mtype
isa MNullableType then mtype
= mtype
.mtype
112 assert mtype
isa MClassType
113 assert not mtype
.need_anchor
117 # Add a live type to the pool
119 # If the types is already live, then do nothing.
121 # REQUIRE: not mtype.need_anchor
122 fun add_type
(mtype
: MClassType)
124 if self.live_types
.has
(mtype
) then return
126 assert not mtype
.need_anchor
127 self.live_types
.add
(mtype
)
128 self.check_depth
(mtype
)
130 # Collect default attributes
131 for cd
in mtype
.collect_mclassdefs
(self.mainmodule
)
133 var nclassdef
= self.modelbuilder
.mclassdef2nclassdef
[cd
]
134 for npropdef
in nclassdef
.n_propdefs
do
135 if not npropdef
isa AAttrPropdef then continue
136 var nexpr
= npropdef
.n_expr
137 if nexpr
== null then continue
138 var v
= new RapidTypeVisitor(self, nclassdef
, npropdef
.mpropdef
.as(not null), mtype
)
143 for rss
in self.live_send_sites
do
144 if not mtype
.is_subtype
(self.mainmodule
, null, rss
.receiver
) then continue
145 if mtype
.has_mproperty
(self.mainmodule
, rss
.mmethod
) then
146 self.add_monomorphic_send
(mtype
, rss
.mmethod
)
151 # Add a send site to the pool
153 # If the send site is already live, then do nothing.
154 fun add_send
(mtype
: MClassType, mmethod
: MMethod)
156 var rss
= new RTASendSite(mmethod
, mtype
)
157 if self.live_send_sites
.has
(rss
) then return
159 self.live_send_sites
.add
(rss
)
161 for mtype2
in self.live_types
do
162 if not mtype2
.is_subtype
(self.mainmodule
, null, mtype
) then continue
163 if mtype2
.has_mproperty
(self.mainmodule
, mmethod
) then
164 self.add_monomorphic_send
(mtype2
, mmethod
)
169 # Add a monomoprhic send.
170 # The send site is not added to the pool.
171 # The method just determine the associated method definition then
172 # performs a static_call
173 fun add_monomorphic_send
(mtype
: MClassType, mmethod
: MMethod)
175 assert self.live_types
.has
(mtype
)
176 var defs
= mmethod
.lookup_definitions
(self.mainmodule
, mtype
)
177 if defs
.is_empty
then return
178 assert defs
.length
== 1 else print
"conflict on {mtype} for {mmethod}: {defs.join(" ")}"
179 self.add_static_call
(mtype
, defs
.first
)
182 # Add a customized_methoddefs to the pool
183 # Is the customized_methoddefs is already live, then do nothing
184 fun add_static_call
(mtype
: MClassType, mmethoddef
: MMethodDef)
186 assert self.live_types
.has
(mtype
)
187 var rm
= new CustomizedMethodDef(mmethoddef
, mtype
)
188 if self.live_customized_methoddefs
.has
(rm
) then return
189 self.live_customized_methoddefs
.add
(rm
)
191 self.live_methoddefs
.add
(mmethoddef
)
194 # Add mtype to the cast pool.
195 fun add_cast_type
(mtype
: MClassType)
197 if self.live_cast_types
.has
(mtype
) then return
199 assert not mtype
.need_anchor
200 self.live_cast_types
.add
(mtype
)
201 self.check_depth
(mtype
)
204 fun check_depth
(mtype
: MClassType)
208 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}.")
212 # Run the analysis until all visitable method definitions are visited.
216 var classes
= self.modelbuilder
.model
.get_mclasses_by_name
("Bool")
217 if classes
!= null then for c
in classes
do self.add_type
(c
.mclass_type
)
219 while not todo
.is_empty
do
222 var vararg_rank
= mr
.mmethoddef
.msignature
.vararg_rank
223 if vararg_rank
> -1 then
224 var node
= self.modelbuilder
.mpropdef2npropdef
[mr
.mmethoddef
]
225 var elttype
= mr
.mmethoddef
.msignature
.mparameters
[vararg_rank
].mtype
226 elttype
= elttype
.anchor_to
(self.mainmodule
, mr
.receiver
)
227 var vararg
= self.mainmodule
.get_primitive_class
("Array").get_mtype
([elttype
])
228 self.add_type
(vararg
)
229 self.add_monomorphic_send
(vararg
, self.modelbuilder
.force_get_primitive_method
(node
, "with_native", vararg
, self.mainmodule
))
230 var native
= self.mainmodule
.get_primitive_class
("NativeArray").get_mtype
([elttype
])
231 self.add_type
(native
)
234 for i
in [0..mr
.mmethoddef
.msignature
.arity
[ do
235 var origtype
= mr
.mmethoddef
.mproperty
.intro
.msignature
.mparameters
[i
].mtype
236 if not origtype
.need_anchor
then continue # skip non covariant stuff
237 var paramtype
= mr
.mmethoddef
.msignature
.mparameters
[i
].mtype
238 paramtype
= self.cleanup_type
(paramtype
, mr
.receiver
).as(not null)
239 self.add_cast_type
(paramtype
)
242 if not self.modelbuilder
.mpropdef2npropdef
.has_key
(mr
.mmethoddef
) then
243 # It is an init for a class?
244 if mr
.mmethoddef
.mproperty
.name
== "init" then
245 var nclassdef
= self.modelbuilder
.mclassdef2nclassdef
[mr
.mmethoddef
.mclassdef
]
246 var super_inits
= nclassdef
.super_inits
247 if super_inits
!= null then
248 #assert args.length == 1
249 for su
in super_inits
do
250 self.add_monomorphic_send
(mr
.receiver
, su
)
259 var npropdef
= self.modelbuilder
.mpropdef2npropdef
[mr
.mmethoddef
]
260 if npropdef
isa AConcreteMethPropdef then
261 #npropdef.debug("Visit {mr.mmethoddef} for {mr.receiver}")
262 var nclassdef
= npropdef
.parent
.as(AClassdef)
263 var mmethoddef
= npropdef
.mpropdef
.as(not null)
264 var auto_super_inits
= npropdef
.auto_super_inits
265 if auto_super_inits
!= null then
266 for auto_super_init
in auto_super_inits
do
267 self.add_monomorphic_send
(mr
.receiver
, auto_super_init
)
270 var v
= new RapidTypeVisitor(self, nclassdef
, mmethoddef
, mr
.receiver
)
271 v
.enter_visit
(npropdef
.n_block
)
272 else if npropdef
isa ADeferredMethPropdef then
273 # nothing to do (maybe add a waring?)
274 else if npropdef
isa AAttrPropdef then
276 else if npropdef
isa AInternMethPropdef or npropdef
isa AExternMethPropdef then
277 # UGLY: We force the "instantation" of the concrete return type if any
278 var ret
= mr
.mmethoddef
.msignature
.return_mtype
279 if ret
!= null and ret
isa MClassType and ret
.mclass
.kind
!= abstract_kind
and ret
.mclass
.kind
!= interface_kind
then
280 ret
= ret
.anchor_to
(self.mainmodule
, mr
.receiver
)
283 else if npropdef
isa AExternInitPropdef then
284 self.add_type
(mr
.receiver
)
286 npropdef
.debug
("Not yet implemented")
293 # A method definitions customized to a specific receiver
294 class CustomizedMethodDef
295 var mmethoddef
: MMethodDef
296 var receiver
: MClassType
300 return "{self.mmethoddef}({receiver})"
305 return o
isa CustomizedMethodDef and o
.mmethoddef
== self.mmethoddef
and o
.receiver
== self.receiver
310 return self.mmethoddef
.hash
+ self.receiver
.hash
314 # A method invokation site bounded by a specific receiver
317 var receiver
: MClassType
321 return o
isa RTASendSite and o
.mmethod
== self.mmethod
and o
.receiver
== self.receiver
326 return self.mmethod
.hash
+ self.receiver
.hash
330 private class RapidTypeVisitor
333 var analysis
: RapidTypeAnalysis
335 var nclassdef
: AClassdef
337 var mpropdef
: MPropDef
339 var receiver
: MClassType
341 init(analysis
: RapidTypeAnalysis, nclassdef
: AClassdef, mpropdef
: MPropDef, receiver
: MClassType)
343 self.analysis
= analysis
344 self.nclassdef
= nclassdef
345 self.mpropdef
= mpropdef
346 self.receiver
= receiver
349 # Adapt and remove nullable
350 # return null if we got the null type
351 fun cleanup_type
(mtype
: MType): nullable MClassType
353 mtype
= mtype
.anchor_to
(self.analysis
.mainmodule
, self.receiver
)
354 if mtype
isa MNullType then return null
355 if mtype
isa MNullableType then mtype
= mtype
.mtype
356 assert mtype
isa MClassType
357 assert not mtype
.need_anchor
361 fun add_type
(mtype
: MType)
363 var rapidtype
= cleanup_type
(mtype
)
364 if rapidtype
== null then return # we do not care about null
366 self.analysis
.add_type
(rapidtype
)
367 #self.current_node.debug("add_type {rapidtype}")
370 fun add_send
(mtype
: MType, mproperty
: MMethod)
372 var rapidtype
= cleanup_type
(mtype
)
373 if rapidtype
== null then return # we do not care about null
375 analysis
.add_send
(rapidtype
, mproperty
)
376 #self.current_node.debug("add_send {mproperty}")
379 fun add_monomorphic_send
(mtype
: MType, mproperty
: MMethod)
381 var rapidtype
= cleanup_type
(mtype
)
382 if rapidtype
== null then return # we do not care about null
384 self.analysis
.add_monomorphic_send
(rapidtype
, mproperty
)
385 #self.current_node.debug("add_monomorphic_send {rapidtype} {mproperty}")
388 fun add_cast_type
(mtype
: MType)
390 var rapidtype
= cleanup_type
(mtype
)
391 if rapidtype
== null then return # we do not care about null
393 self.analysis
.add_cast_type
(rapidtype
)
394 #self.current_node.debug("add_cast_type {rapidtype}")
397 redef fun visit
(node
)
399 if node
== null then return
400 node
.accept_rapid_type_vistor
(self)
404 # Force to get the primitive class named `name' or abort
405 fun get_class
(name
: String): MClass
407 return self.analysis
.mainmodule
.get_primitive_class
(name
)
410 # Force to get the primitive property named `name' in the instance `recv' or abort
411 fun get_method
(recv
: MType, name
: String): MMethod
413 var rapidtype
= cleanup_type
(recv
)
414 if rapidtype
== null then abort
416 return self.analysis
.modelbuilder
.force_get_primitive_method
(self.current_node
.as(not null), name
, rapidtype
, self.analysis
.mainmodule
)
423 private fun accept_rapid_type_vistor
(v
: RapidTypeVisitor)
429 redef fun accept_rapid_type_vistor
(v
)
431 v
.add_type
(self.mtype
.as(not null))
435 redef class AFloatExpr
436 redef fun accept_rapid_type_vistor
(v
)
438 v
.add_type
(self.mtype
.as(not null))
442 redef class ACharExpr
443 redef fun accept_rapid_type_vistor
(v
)
445 v
.add_type
(self.mtype
.as(not null))
449 redef class AArrayExpr
450 redef fun accept_rapid_type_vistor
(v
)
452 var mtype
= self.mtype
.as(MClassType)
454 var native
= v
.get_class
("NativeArray").get_mtype
([mtype
.arguments
.first
])
456 var prop
= v
.get_method
(mtype
, "with_native")
457 v
.add_monomorphic_send
(mtype
, prop
)
461 redef class AStringFormExpr
462 redef fun accept_rapid_type_vistor
(v
)
464 var mtype
= self.mtype
.as(not null)
466 var native
= v
.get_class
("NativeString").mclass_type
468 var prop
= v
.get_method
(mtype
, "from_cstring")
469 v
.add_monomorphic_send
(mtype
, prop
)
473 redef class ASuperstringExpr
474 redef fun accept_rapid_type_vistor
(v
)
476 var arraytype
= v
.get_class
("Array").get_mtype
([v
.get_class
("Object").mclass_type
])
477 v
.add_type
(arraytype
)
478 v
.add_type
(v
.get_class
("NativeArray").get_mtype
([v
.get_class
("Object").mclass_type
]))
479 var prop
= v
.get_method
(arraytype
, "join")
480 v
.add_monomorphic_send
(arraytype
, prop
)
481 var prop2
= v
.get_method
(arraytype
, "with_native")
482 v
.add_monomorphic_send
(arraytype
, prop2
)
486 redef class ACrangeExpr
487 redef fun accept_rapid_type_vistor
(v
)
489 var mtype
= self.mtype
.as(not null)
491 var prop
= v
.get_method
(mtype
, "init")
492 v
.add_monomorphic_send
(mtype
, prop
)
496 redef class AOrangeExpr
497 redef fun accept_rapid_type_vistor
(v
)
499 var mtype
= self.mtype
.as(not null)
501 var prop
= v
.get_method
(mtype
, "without_last")
502 v
.add_monomorphic_send
(mtype
, prop
)
506 redef class ATrueExpr
507 redef fun accept_rapid_type_vistor
(v
)
509 v
.add_type
(self.mtype
.as(not null))
513 redef class AFalseExpr
514 redef fun accept_rapid_type_vistor
(v
)
516 v
.add_type
(self.mtype
.as(not null))
521 redef fun accept_rapid_type_vistor
(v
)
523 v
.add_cast_type
(self.cast_type
.as(not null))
527 redef class AAsCastExpr
528 redef fun accept_rapid_type_vistor
(v
)
530 v
.add_cast_type
(self.mtype
.as(not null))
536 redef class ASendExpr
537 redef fun accept_rapid_type_vistor
(v
)
539 var mproperty
= self.mproperty
.as(not null)
540 if n_expr
isa ASelfExpr then
541 v
.add_monomorphic_send
(v
.receiver
, mproperty
)
543 var recvtype
= self.n_expr
.mtype
.as(not null)
544 v
.add_send
(recvtype
, mproperty
)
549 redef class ASendReassignFormExpr
550 redef fun accept_rapid_type_vistor
(v
)
552 v
.add_send
(self.read_type
.as(not null), self.reassign_property
.mproperty
)
553 var mproperty
= self.mproperty
.as(not null)
554 var write_mproperty
= self.write_mproperty
.as(not null)
555 if n_expr
isa ASelfExpr then
556 v
.add_monomorphic_send
(v
.receiver
, mproperty
)
557 v
.add_monomorphic_send
(v
.receiver
, write_mproperty
)
559 var recvtype
= self.n_expr
.mtype
.as(not null)
560 v
.add_send
(recvtype
, mproperty
)
561 v
.add_send
(recvtype
, write_mproperty
)
566 redef class AVarReassignExpr
567 redef fun accept_rapid_type_vistor
(v
)
569 v
.add_send
(self.read_type
.as(not null), self.reassign_property
.mproperty
)
573 redef class AAttrReassignExpr
574 redef fun accept_rapid_type_vistor
(v
)
576 v
.add_send
(self.read_type
.as(not null), self.reassign_property
.mproperty
)
580 redef class ASuperExpr
581 redef fun accept_rapid_type_vistor
(v
)
583 var mproperty
= self.mproperty
584 if mproperty
!= null then
585 v
.add_monomorphic_send
(v
.receiver
, mproperty
)
589 #FIXME: we do not want an ugly static call!
590 var mpropdef
= v
.mpropdef
591 var mpropdefs
= mpropdef
.mproperty
.lookup_super_definitions
(mpropdef
.mclassdef
.mmodule
, mpropdef
.mclassdef
.bound_mtype
)
592 if mpropdefs
.length
!= 1 then
593 debug
("MPRODFEFS for super {mpropdef} for {v.receiver}: {mpropdefs.join(", ")}")
595 var msuperpropdef
= mpropdefs
.first
596 assert msuperpropdef
isa MMethodDef
597 v
.analysis
.add_static_call
(v
.receiver
, msuperpropdef
)
602 redef fun accept_rapid_type_vistor
(v
)
604 var recvtype
= self.n_expr
.mtype
.as(not null)
605 var colltype
= self.coltype
.as(not null)
606 var itmeth
= v
.get_method
(colltype
, "iterator")
607 v
.add_send
(recvtype
, itmeth
)
608 var iteratortype
= itmeth
.intro
.msignature
.return_mtype
.as(MClassType).mclass
.intro
.bound_mtype
609 var objtype
= v
.get_class
("Object").mclass_type
610 v
.add_send
(objtype
, v
.get_method
(iteratortype
, "is_ok"))
611 if self.variables
.length
== 1 then
612 v
.add_send
(objtype
, v
.get_method
(iteratortype
, "item"))
613 else if self.variables
.length
== 2 then
614 v
.add_send
(objtype
, v
.get_method
(iteratortype
, "key"))
615 v
.add_send
(objtype
, v
.get_method
(iteratortype
, "item"))
619 v
.add_send
(objtype
, v
.get_method
(iteratortype
, "next"))
624 redef fun accept_rapid_type_vistor
(v
)
626 var recvtype
= self.mtype
.as(not null)
628 v
.add_monomorphic_send
(recvtype
, mproperty
.as(not null))