7b3baf49c460a05f41f6512738a326c47a3c0d73
1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2008-2009 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.
17 # Analysis control flow and variable visibility in property bodies, statements and expressions
22 # Associate symbols to variable and variables to type
24 abstract class VariableContext
25 # Look for the variable from its name
26 # Return null if nothing found
27 fun [](s
: Symbol): nullable Variable
29 if _dico
.has_key
(s
) then
36 # Register a new variable with its name
43 fun mark_is_set
(v
: Variable)
48 fun check_is_set
(n
: ANode, v
: Variable)
50 if v
.must_be_set
and not is_set
(v
) then
51 _visitor
.error
(n
, "Error: variable '{v}' is possibly unset.")
54 var loc
= x
.node
.location
55 print
" {if loc != null then loc.to_s else "????"}: {x._set_variables.join(", ")} ; {x._dico.join(", ")}"
57 if x0
isa SubVariableContext then
66 # The effective static type of a given variable
67 # May be different from the declaration static type
68 fun stype
(v
: Variable): nullable MMType
70 if _stypes
.has_key
(v
) then
77 # Set effective static type of a given variable
78 # May be different from the declaration static type
79 fun stype
=(v
: Variable, t
: nullable MMType)
84 # Variables by name (in the current context only)
85 var _dico
: Map[Symbol, Variable]
87 # All variables in all contextes
88 var _all_variables
: Set[Variable]
90 # Updated static type of variables
91 var _stypes
: Map[Variable, nullable MMType] = new HashMap[Variable, nullable MMType]
93 # Build a new VariableContext
94 fun sub
(node
: ANode): SubVariableContext
96 return new SubVariableContext.with_prev
(self, node
)
99 # Build a nested VariableContext with new variable information
100 fun sub_with
(node
: ANode, v
: Variable, t
: MMType): SubVariableContext
107 # The visitor of the context (used to display error)
108 var _visitor
: AbsSyntaxVisitor
110 # The syntax node that introduced the context
111 readable var _node
: ANode
113 init(visitor
: AbsSyntaxVisitor, node
: ANode)
117 _dico
= new HashMap[Symbol, Variable]
120 # Is a control flow break met? (return, break, continue)
121 readable writable var _unreash
: Bool = false
123 # Is a control flow already broken?
124 # Used to avoid repeating the same error message
125 readable writable var _already_unreash
: Bool = false
127 # Set of variable that are set (assigned)
128 readable var _set_variables
: HashSet[Variable] = new HashSet[Variable]
131 fun is_set
(v
: Variable): Bool
133 return _set_variables
.has
(v
)
136 # Merge back one flow context information
137 fun merge
(ctx
: VariableContext)
141 if ctx
.already_unreash
then already_unreash
= true
144 for v
in _all_variables
do
145 if not is_set
(v
) and ctx
.is_set
(v
) then
149 var s1
= ctx
.stype
(v
)
150 if s1
!= s
then stype
(v
) = s1
154 # Merge back two alternative flow context informations
155 fun merge2
(ctx1
, ctx2
, basectx
: VariableContext)
159 else if ctx2
.unreash
then
162 for v
in _all_variables
do
163 if not is_set
(v
) and ctx1
.is_set
(v
) and ctx2
.is_set
(v
) then
168 var s1
= ctx1
.stype
(v
)
169 var s2
= ctx2
.stype
(v
)
170 if s1
== s
and s2
== s
then
172 else if s1
== s2
then
174 else if s2
== null or s1
< s2
then
176 else if s1
== null or s2
< s1
then
179 stype
(v
) = basectx
.stype
(v
)
187 s
.append
(node
.location
.to_s
)
188 for v
in _all_variables
do
190 if t
== null then continue
197 class RootVariableContext
198 special VariableContext
199 init(visitor
: AbsSyntaxVisitor, node
: ANode)
202 _all_variables
= new HashSet[Variable]
206 class SubVariableContext
207 special VariableContext
208 readable var _prev
: VariableContext
212 if _dico
.has_key
(s
) then
221 if _stypes
.has_key
(v
) then
228 init with_prev
(p
: VariableContext, node
: ANode)
230 init(p
._visitor
, node
)
232 _all_variables
= p
._all_variables
237 return _set_variables
.has
(v
) or _prev
.is_set
(v
)
242 # Is the variable must be set before being used ?
243 fun must_be_set
: Bool do return false
246 redef class VarVariable
247 redef fun must_be_set
do return true