1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Static Analysis Framework base
17 # A lot of TODOs still missing:
18 # * flow breaks (AReturnExpr, AEscapeExpr, AAbortExpr)
19 # * other conditionnals (AIfexprExpr, AAssertExpr, ABonBoolExpr)
20 # * flow through AAttrPropDef
25 # The base of all analysis performed statically.
26 abstract class StaticAnalysis
29 # Type of FlowSet representation used by the StaticAnalysis.
32 # "in" set for the currently visited node.
33 var current_inset
: FLOW is noinit
, writable
35 # 'out' set for the currently visited node.
36 var current_outset
: FLOW is noinit
, writable
38 # Sets at the entry of each node.
40 # Each node is associated with the `current_inset` it got.
41 var insets
= new HashMap[ANode, FLOW]
43 # Sets at the exit of each node.
45 # Each node is associated with the `current_outset` it got.
46 var outsets
= new HashMap[ANode, FLOW]
49 current_inset
= new_initial_flow
50 current_outset
= new_initial_flow
53 # Initial flow set to use.
55 # Because the initial flow set depends on the analysis performed, the
56 # implementation of this method is the responsability the subclass.
57 fun new_initial_flow
: FLOW is abstract
59 # The merge operation on sets for confluence.
61 # Depends on the analysis performed.
62 fun merge
(s1
, s2
: FLOW): FLOW is abstract
64 # ModelBuilder used to lookup AST nodes.
65 var modelbuilder
: ModelBuilder
67 # Run `self` on an AST `node`.
68 fun start_analysis
(node
: ANode) do visit
(node
)
70 # Pretty print the outsets of this analysis.
72 # Mainly used for debug and testing.
73 fun pretty_print
is abstract
76 # An analysis go forward from the entry point to the exit point.
78 # With a forward analysis, output properties are determined by the inputs.
82 redef fun visit
(n
) do n
.accept_forward_analysis
(self)
85 # FlowSet are used to represent data at the entry and exit of a statement.
89 # Merge `self` with another flow set.
90 fun flow_union
(o
: SELF): SELF is abstract
93 # A FlowSet based on a HashMap.
94 class FlowHashMap[K
, V
]
98 # Init `self` with the content of `map`.
99 init from
(map
: Map[K
, V
]) do
101 for k
, v
in map
do self[k
] = v
105 redef fun clone
do return new FlowHashMap[K
, V
].from
(self)
108 # A FlowSet based on a HashSet.
113 redef fun clone
do return new FlowHashSet[E
].from
(self)
115 # Remove all items found in `other` from `self`.
116 fun remove_from
(other
: Collection[E
]) do
117 for e
in other
do remove
(e
)
120 redef fun flow_union
(o
) do return new FlowHashSet[E
].from
(union
(o
))
125 # Apply the forward analysis `v` to `self`.
126 fun accept_forward_analysis
(v
: ForwardAnalysis) do
127 v
.current_inset
= v
.current_outset
.clone
128 v
.current_outset
= v
.current_inset
.clone
129 v
.insets
[self] = v
.current_inset
131 v
.outsets
[self] = v
.current_outset
137 # Merge flow on if .. else constructs.
138 redef fun accept_forward_analysis
(v
) do
139 v
.enter_visit
(n_expr
)
140 var inset
= v
.current_inset
141 var outset
= v
.current_outset
143 if n_then
!= null then v
.enter_visit
(n_then
)
144 var then_outset
= v
.current_outset
146 v
.current_inset
= inset
147 v
.current_outset
= outset
149 if n_else
!= null then
150 v
.enter_visit
(n_else
)
151 outset
= v
.merge
(then_outset
, v
.current_outset
)
153 outset
= v
.merge
(then_outset
, v
.current_inset
)
155 v
.current_inset
= inset
156 v
.current_outset
= outset
160 # Represent all kind of `do .. end` blocks.
162 # Used to factorize implementations across do blocks, whiles, fors and loops.
164 # This factorization makes sense since all these contructs can be flow managed
165 # through contine and breack statements.
167 # TODO move this up in the module hierarchy
168 interface ADoBlockHelper
169 # Lookup fix point for this loop.
170 fun loop_fix_point
(v
: StaticAnalysis, node
: ANode) do
171 var inset
= v
.current_inset
.clone
172 var last
: nullable FlowSet = null
173 while v
.current_outset
!= last
do
175 v
.current_inset
= v
.merge
(inset
, v
.current_outset
)
176 v
.current_outset
= v
.current_inset
.clone
177 last
= v
.current_outset
.clone
179 v
.current_inset
= inset
180 v
.current_outset
= v
.merge
(inset
, v
.current_outset
)
183 # Factorize loop forward analysis.
184 fun accept_loop_forward_analysis
(v
: StaticAnalysis) do
185 var n_block
= loop_block
186 if not n_block
== null then loop_fix_point
(v
, n_block
)
189 # The block contained by this loop.
190 fun loop_block
: nullable ANode is abstract
196 redef fun loop_block
do return self.n_block
197 redef fun accept_forward_analysis
(v
) do accept_loop_forward_analysis
(v
)
200 redef class ALoopExpr
203 redef fun loop_block
do return self.n_block
204 redef fun accept_forward_analysis
(v
) do accept_loop_forward_analysis
(v
)
207 redef class AWhileExpr
210 redef fun loop_block
do return self.n_block
212 redef fun accept_forward_analysis
(v
) do
213 v
.enter_visit
(n_expr
)
214 accept_loop_forward_analysis
(v
)
221 redef fun loop_block
do return self.n_block
223 redef fun accept_forward_analysis
(v
) do
224 for n_group
in n_groups
do
225 v
.enter_visit
(n_group
.n_expr
)
227 accept_loop_forward_analysis
(v
)