metamodel: rename 'universal' to 'enum'
[nit.git] / src / analysis / reachable_as_init_impl.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
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 # This module introduces an analysis that flags all initializers called as new A.x
18 package reachable_as_init_impl
19
20 import reachable_method_analysis
21 import reachable_as_init
22
23 class ReachableAsInitBuilder
24 readable var _context: ReachableAsInitAnalysisImpl = new ReachableAsInitAnalysisImpl
25 readable var _program: Program
26
27 fun work do
28 program.with_each_iroutines !action(i, m) do
29 if program.rma.is_iroutine_reachable(i) then
30 var v = new RAIVisitor(self)
31 v.visit_iroutine(i)
32 end
33 end
34 end
35
36 init(p: Program) do
37 _program = p
38 end
39 end
40
41 # Visitor will add only initializers in the _methods list.
42 # If the checked method is in this list, it is reachable as init !
43 class ReachableAsInitAnalysisImpl
44 super ReachableAsInitAnalysis
45 var _methods: HashMap[MMLocalClass, List[MMMethod]] = new HashMap[MMLocalClass, List[MMMethod]]
46
47 redef fun is_method_reachable_as_init(method: MMMethod, c: MMLocalClass): Bool do
48 if _methods.has_key(c) then return _methods[c].has(method)
49 return false
50 end
51
52 init do end
53 end
54
55 class RAIVisitor
56 super ICodeVisitor
57 readable var _builder: ReachableAsInitBuilder
58
59 redef fun visit_icode(ic)
60 do
61 if ic isa INew then
62 # FIXME: take only the last property on the redef. hierarchie
63 var t = ic.stype
64 var cls = t.for_module(builder.program.main_module).local_class
65 var m = cls[ic.property.global].as(MMMethod)
66 assert m.global.is_init
67 if not builder.context._methods.has_key(cls) then builder.context._methods[cls] = new List[MMMethod]
68 if not builder.context._methods[cls].has(m) then builder.context._methods[cls].add(m)
69 end
70 super
71 end
72
73 init(b: ReachableAsInitBuilder)
74 do
75 _builder = b
76 end
77 end