1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
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.
17 # This module introduces an analysis that flags all initializers called as new A.x
18 package reachable_as_init_impl
20 import reachable_method_analysis
21 import reachable_as_init
23 class ReachableAsInitBuilder
24 readable var _context
: ReachableAsInitAnalysisImpl = new ReachableAsInitAnalysisImpl
25 readable var _program
: Program
28 program
.with_each_iroutines
!action
(i
, m
) do
29 if program
.rma
.is_iroutine_reachable
(i
) then
30 var v
= new RAIVisitor(self)
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]]
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
)
57 readable var _builder
: ReachableAsInitBuilder
59 redef fun visit_icode
(ic
)
62 # FIXME: take only the last property on the redef. hierarchie
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
)
73 init(b
: ReachableAsInitBuilder)