src: use `ASuperExpr::mpropdef` instead of asking the frame or visitors
[nit.git] / src / rapid_type_analysis.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2012 Jean Privat <jean@pryen.org>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17
18 # Rapid type analysis on the AST
19 #
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
25
26 import model
27 import modelbuilder
28 import typing
29 import auto_super_init
30
31 redef class ModelBuilder
32 fun do_rapid_type_analysis(mainmodule: MModule): RapidTypeAnalysis
33 do
34 var analysis = new RapidTypeAnalysis(self, mainmodule)
35 analysis.run_analysis
36 return analysis
37 end
38 end
39
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
45
46 # The main module of the analysis.
47 # Used to perform types operations.
48 var mainmodule: MModule
49
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]
54
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]
58
59 # Live (instantiated) classes.
60 var live_classes = new HashSet[MClass]
61
62 # The pool of types used to perform type checks (isa and as).
63 var live_cast_types = new HashSet[MType]
64
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]
68
69 # Live method definitions.
70 var live_methoddefs = new HashSet[MMethodDef]
71
72 # Live methods.
73 var live_methods = new HashSet[MMethod]
74
75 # Live call-to-super.
76 var live_super_sends = new HashSet[MMethodDef]
77
78 # Methods that are are still candidate to the try_send
79 private var totry_methods = new HashSet[MMethod]
80
81 # The method definitions that remain to visit
82 private var todo = new List[MMethodDef]
83
84 private fun force_alive(classname: String)
85 do
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)
88 end
89
90 # Run the analysis until all visitable method definitions are visited.
91 fun run_analysis
92 do
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)
99 end
100 var mainprop = mainmodule.try_get_primitive_method("main", maintype.mclass)
101 if mainprop != null then
102 add_send(maintype, mainprop)
103 end
104
105 # Force primitive types
106 force_alive("Bool")
107 force_alive("Int")
108 force_alive("Float")
109 force_alive("Char")
110
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)
115
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])
122 v.add_type(vararg)
123 var native = self.mainmodule.get_primitive_class("NativeArray").get_mtype([elttype])
124 v.add_type(native)
125 v.add_monomorphic_send(vararg, self.modelbuilder.force_get_primitive_method(node, "with_native", vararg.mclass, self.mainmodule))
126 end
127
128
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)
134 add_cast(paramtype)
135 end
136
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)
146 end
147 end
148
149 else
150 abort
151 end
152 continue
153 end
154
155 var npropdef = modelbuilder.mpropdef2npropdef[mmethoddef]
156
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_callsite(auto_super_init)
162 end
163 end
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
169 v.add_type(ret)
170 end
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)
176 else
177
178 end
179
180 v.enter_visit(npropdef)
181 end
182
183 #print "MMethod {live_methods.length}: {live_methods.join(", ")}"
184 #print "MMethodDef {live_methoddefs.length}: {live_methoddefs.join(", ")}"
185
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
192 #print "{ot}/{t} ?"
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}"
197 live_types.add(rt)
198 todo_types.add(rt)
199 check_depth(rt)
200 end
201 end
202 #print "MType {live_types.length}: {live_types.join(", ")}"
203
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 live_cast_types.add(rt)
211 #print " {ot}/{t} -> {rt}"
212 end
213 end
214 #print "cast MType {live_cast_types.length}: {live_cast_types.join(", ")}"
215 end
216
217 private fun check_depth(mtype: MClassType)
218 do
219 var d = mtype.length
220 if d > 255 then
221 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}.")
222 end
223 end
224
225 fun add_new(recv: MClassType, mtype: MClassType)
226 do
227 assert not recv.need_anchor
228 if mtype.need_anchor then
229 if live_open_types.has(mtype) then return
230 live_open_types.add(mtype)
231 else
232 if live_types.has(mtype) then return
233 live_types.add(mtype)
234 end
235
236 var mclass = mtype.mclass
237 if live_classes.has(mclass) then return
238 live_classes.add(mclass)
239
240 for p in totry_methods do try_send(mtype, p)
241 for p in live_super_sends do try_super_send(mtype, p)
242
243 var bound_mtype = mtype.anchor_to(mainmodule, recv)
244 for cd in bound_mtype.collect_mclassdefs(mainmodule)
245 do
246 if not self.modelbuilder.mclassdef2nclassdef.has_key(cd) then continue
247 var nclassdef = self.modelbuilder.mclassdef2nclassdef[cd]
248 for npropdef in nclassdef.n_propdefs do
249 if not npropdef isa AAttrPropdef then continue
250 var nexpr = npropdef.n_expr
251 if nexpr == null then continue
252 var mpropdef = npropdef.mpropdef.as(not null)
253 var v = new RapidTypeVisitor(self, bound_mtype, mpropdef)
254 v.enter_visit(nexpr)
255 end
256 end
257
258 end
259
260 fun add_cast(mtype: MType)
261 do
262 if mtype.need_anchor then
263 live_open_cast_types.add(mtype)
264 else
265 live_cast_types.add(mtype)
266 end
267 end
268
269 fun try_send(recv: MClassType, mproperty: MMethod)
270 do
271 recv = recv.mclass.intro.bound_mtype
272 if not recv.has_mproperty(mainmodule, mproperty) then return
273 var d = mproperty.lookup_first_definition(mainmodule, recv)
274 add_call(d)
275 end
276
277 fun add_call(mpropdef: MMethodDef)
278 do
279 if live_methoddefs.has(mpropdef) then return
280 live_methoddefs.add(mpropdef)
281 todo.add(mpropdef)
282
283 var mproperty = mpropdef.mproperty
284 if mproperty.mpropdefs.length <= 1 then return
285 # If all definitions of a method are live, we can remove the definition of the totry set
286 for d in mproperty.mpropdefs do
287 if d.is_abstract then continue
288 if not live_methoddefs.has(d) then return
289 end
290 #print "full property: {mpropdef.mproperty} for {mpropdef.mproperty.mpropdefs.length} definitions"
291 totry_methods.remove(mpropdef.mproperty)
292 end
293
294 fun add_send(recv: MType, mproperty: MMethod)
295 do
296 if live_methods.has(mproperty) then return
297 #print "new prop: {mproperty}"
298 live_methods.add(mproperty)
299 if mproperty.mpropdefs.length == 1 then
300 # If there is only one definition, just add the definition and do not try again the property
301 var d = mproperty.mpropdefs.first
302 add_call(d)
303 return
304 end
305 # Else, the property is potentially called with various reciever
306 # So just try the methods with existing receiver and register it for future receiver
307 totry_methods.add(mproperty)
308 for c in live_classes do
309 try_send(c.intro.bound_mtype, mproperty)
310 end
311 end
312
313 fun try_super_send(recv: MClassType, mpropdef: MMethodDef)
314 do
315 recv = recv.mclass.intro.bound_mtype
316 if not recv.collect_mclassdefs(mainmodule).has(mpropdef.mclassdef) then return
317 var d = mpropdef.lookup_next_definition(mainmodule, recv)
318 add_call(d)
319 end
320
321 fun add_super_send(recv: MType, mpropdef: MMethodDef)
322 do
323 if live_super_sends.has(mpropdef) then return
324 #print "new super prop: {mpropdef}"
325 live_super_sends.add(mpropdef)
326 for t in live_types do
327 try_super_send(t, mpropdef)
328 end
329 end
330 end
331
332 class RapidTypeVisitor
333 super Visitor
334
335 var analysis: RapidTypeAnalysis
336 var receiver: MClassType
337 var mpropdef: MPropDef
338
339 init(analysis: RapidTypeAnalysis, receiver: MClassType, mpropdef: MPropDef)
340 do
341 self.analysis = analysis
342 self.receiver = receiver
343 self.mpropdef = mpropdef
344 assert not receiver.need_anchor
345 end
346
347 redef fun visit(n)
348 do
349 n.accept_rapid_type_visitor(self)
350 if n isa AExpr then
351 var implicit_cast_to = n.implicit_cast_to
352 if implicit_cast_to != null then self.add_cast_type(implicit_cast_to)
353 end
354
355 # RTA does not enter in AAnnotations
356 if not n isa AAnnotations then
357 n.visit_all(self)
358 end
359 end
360
361 fun cleanup_type(mtype: MType): nullable MClassType
362 do
363 mtype = mtype.anchor_to(self.analysis.mainmodule, self.receiver)
364 if mtype isa MNullType then return null
365 if mtype isa MNullableType then mtype = mtype.mtype
366 assert mtype isa MClassType
367 assert not mtype.need_anchor
368 return mtype
369 end
370
371 fun get_class(name: String): MClass
372 do
373 return analysis.mainmodule.get_primitive_class(name)
374 end
375
376 fun get_method(recv: MType, name: String): MMethod
377 do
378 var mtype = cleanup_type(recv)
379 assert mtype != null
380 return self.analysis.modelbuilder.force_get_primitive_method(self.current_node.as(not null), name, mtype.mclass, self.analysis.mainmodule)
381 end
382
383 fun add_type(mtype: MClassType) do analysis.add_new(receiver, mtype)
384
385 fun add_monomorphic_send(mtype: MType, mproperty: MMethod) do analysis.try_send(mtype.as(MClassType), mproperty)
386
387 fun add_send(mtype: MType, mproperty: MMethod) do analysis.add_send(mtype, mproperty)
388
389 fun add_cast_type(mtype: MType) do analysis.add_cast(mtype)
390
391 fun add_callsite(callsite: nullable CallSite) do if callsite != null then analysis.add_send(callsite.recv, callsite.mproperty)
392 end
393
394 ###
395
396 redef class ANode
397 private fun accept_rapid_type_visitor(v: RapidTypeVisitor)
398 do
399 end
400 end
401
402 redef class AIntExpr
403 redef fun accept_rapid_type_visitor(v)
404 do
405 v.add_type(self.mtype.as(MClassType))
406 end
407 end
408
409 redef class AFloatExpr
410 redef fun accept_rapid_type_visitor(v)
411 do
412 v.add_type(self.mtype.as(MClassType))
413 end
414 end
415
416 redef class ACharExpr
417 redef fun accept_rapid_type_visitor(v)
418 do
419 v.add_type(self.mtype.as(MClassType))
420 end
421 end
422
423 redef class AArrayExpr
424 redef fun accept_rapid_type_visitor(v)
425 do
426 var mtype = self.mtype.as(MClassType)
427 v.add_type(mtype)
428 var native = v.analysis.mainmodule.get_primitive_class("NativeArray").get_mtype([mtype.arguments.first])
429 v.add_type(native)
430 mtype = v.cleanup_type(mtype).as(not null)
431 var prop = v.get_method(mtype, "with_native")
432 v.add_monomorphic_send(mtype, prop)
433 end
434 end
435
436 redef class AStringFormExpr
437 redef fun accept_rapid_type_visitor(v)
438 do
439 var native = v.get_class("NativeString").mclass_type
440 v.add_type(native)
441 var prop = v.get_method(native, "to_s_with_length")
442 v.add_monomorphic_send(native, prop)
443 end
444 end
445
446 redef class ASuperstringExpr
447 redef fun accept_rapid_type_visitor(v)
448 do
449 var arraytype = v.get_class("Array").get_mtype([v.get_class("Object").mclass_type])
450 v.add_type(arraytype)
451 v.add_type(v.get_class("NativeArray").get_mtype([v.get_class("Object").mclass_type]))
452 var prop = v.get_method(arraytype, "join")
453 v.add_monomorphic_send(arraytype, prop)
454 var prop2 = v.get_method(arraytype, "with_native")
455 v.add_monomorphic_send(arraytype, prop2)
456 end
457 end
458
459 redef class ACrangeExpr
460 redef fun accept_rapid_type_visitor(v)
461 do
462 var mtype = self.mtype.as(MClassType)
463 v.add_type(mtype)
464 var prop = v.get_method(mtype, "init")
465 v.add_monomorphic_send(mtype, prop)
466 end
467 end
468
469 redef class AOrangeExpr
470 redef fun accept_rapid_type_visitor(v)
471 do
472 var mtype = self.mtype.as(MClassType)
473 v.add_type(mtype)
474 var prop = v.get_method(mtype, "without_last")
475 v.add_monomorphic_send(mtype, prop)
476 end
477 end
478
479 redef class ATrueExpr
480 redef fun accept_rapid_type_visitor(v)
481 do
482 v.add_type(self.mtype.as(MClassType))
483 end
484 end
485
486 redef class AFalseExpr
487 redef fun accept_rapid_type_visitor(v)
488 do
489 v.add_type(self.mtype.as(MClassType))
490 end
491 end
492
493 redef class AIsaExpr
494 redef fun accept_rapid_type_visitor(v)
495 do
496 v.add_cast_type(self.cast_type.as(not null))
497 end
498 end
499
500 redef class AAsCastExpr
501 redef fun accept_rapid_type_visitor(v)
502 do
503 v.add_cast_type(self.mtype.as(not null))
504 end
505 end
506
507 redef class ASendExpr
508 redef fun accept_rapid_type_visitor(v)
509 do
510 v.add_callsite(callsite)
511 end
512 end
513
514
515 redef class ASendReassignFormExpr
516 redef fun accept_rapid_type_visitor(v)
517 do
518 v.add_callsite(callsite)
519 v.add_callsite(reassign_callsite)
520 v.add_callsite(write_callsite)
521 end
522 end
523
524 redef class AVarReassignExpr
525 redef fun accept_rapid_type_visitor(v)
526 do
527 v.add_callsite(reassign_callsite)
528 end
529 end
530
531 redef class AAttrReassignExpr
532 redef fun accept_rapid_type_visitor(v)
533 do
534 v.add_callsite(reassign_callsite)
535 end
536 end
537
538 redef class ASuperExpr
539 redef fun accept_rapid_type_visitor(v)
540 do
541 var callsite = self.callsite
542 if callsite != null then
543 v.add_callsite(callsite)
544 return
545 end
546
547 v.analysis.add_super_send(v.receiver, mpropdef.as(not null))
548 end
549 end
550
551 redef class AForExpr
552 redef fun accept_rapid_type_visitor(v)
553 do
554 var recvtype = self.n_expr.mtype.as(not null)
555 var colltype = self.coltype.as(not null)
556 var itmeth = v.get_method(colltype, "iterator")
557 v.add_send(recvtype, itmeth)
558 var iteratortype = itmeth.intro.msignature.return_mtype.as(MClassType).mclass.intro.bound_mtype
559 var objtype = v.get_class("Object").mclass_type
560 v.add_send(objtype, v.get_method(iteratortype, "is_ok"))
561 if self.variables.length == 1 then
562 v.add_send(objtype, v.get_method(iteratortype, "item"))
563 else if self.variables.length == 2 then
564 v.add_send(objtype, v.get_method(iteratortype, "key"))
565 v.add_send(objtype, v.get_method(iteratortype, "item"))
566 else
567 abort
568 end
569 v.add_send(objtype, v.get_method(iteratortype, "next"))
570 end
571 end
572
573 redef class ANewExpr
574 redef fun accept_rapid_type_visitor(v)
575 do
576 var mtype = self.mtype.as(MClassType)
577 v.add_type(mtype)
578 v.add_callsite(callsite)
579 end
580 end