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
: PNode, 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 print
" {x.node.locate}: {x._set_variables.join(", ")} ; {x._dico.join(", ")}"
56 if x0
isa SubVariableContext then
65 # The effective static type of a given variable
66 # May be different from the declaration static type
67 fun stype
(v
: Variable): nullable MMType
69 if _stypes
.has_key
(v
) then
76 # Set effective static type of a given variable
77 # May be different from the declaration static type
78 fun stype
=(v
: Variable, t
: nullable MMType)
83 # Variables by name (in the current context only)
84 var _dico
: Map[Symbol, Variable]
86 # All variables in all contextes
87 var _all_variables
: Set[Variable]
89 # Updated static type of variables
90 var _stypes
: Map[Variable, nullable MMType] = new HashMap[Variable, nullable MMType]
92 # Build a new VariableContext
93 fun sub
(node
: PNode): SubVariableContext
95 return new SubVariableContext.with_prev
(self, node
)
98 # Build a nested VariableContext with new variable information
99 fun sub_with
(node
: PNode, v
: Variable, t
: MMType): SubVariableContext
106 # The visitor of the context (used to display error)
107 var _visitor
: AbsSyntaxVisitor
109 # The syntax node that introduced the context
110 readable var _node
: PNode
112 init(visitor
: AbsSyntaxVisitor, node
: PNode)
116 _dico
= new HashMap[Symbol, Variable]
119 # Is a control flow break met? (return, break, continue)
120 readable writable var _unreash
: Bool = false
122 # Is a control flow already broken?
123 # Used to avoid repeating the same error message
124 readable writable var _already_unreash
: Bool = false
126 # Set of variable that are set (assigned)
127 readable var _set_variables
: HashSet[Variable] = new HashSet[Variable]
130 fun is_set
(v
: Variable): Bool
132 return _set_variables
.has
(v
)
135 # Merge back one flow context information
136 fun merge
(ctx
: VariableContext)
140 if ctx
.already_unreash
then already_unreash
= true
143 for v
in _all_variables
do
144 if not is_set
(v
) and ctx
.is_set
(v
) then
148 var s1
= ctx
.stype
(v
)
149 if s1
!= s
then stype
(v
) = s1
153 # Merge back two alternative flow context informations
154 fun merge2
(ctx1
, ctx2
, basectx
: VariableContext)
158 else if ctx2
.unreash
then
161 for v
in _all_variables
do
162 if not is_set
(v
) and ctx1
.is_set
(v
) and ctx2
.is_set
(v
) then
167 var s1
= ctx1
.stype
(v
)
168 var s2
= ctx2
.stype
(v
)
169 if s1
== s
and s2
== s
then
171 else if s1
== s2
then
173 else if s2
== null or s1
< s2
then
175 else if s1
== null or s2
< s1
then
178 stype
(v
) = basectx
.stype
(v
)
186 s
.append
(node
.locate
)
187 for v
in _all_variables
do
189 if t
== null then continue
196 class RootVariableContext
197 special VariableContext
198 init(visitor
: AbsSyntaxVisitor, node
: PNode)
201 _all_variables
= new HashSet[Variable]
205 class SubVariableContext
206 special VariableContext
207 readable var _prev
: VariableContext
211 if _dico
.has_key
(s
) then
220 if _stypes
.has_key
(v
) then
227 init with_prev
(p
: VariableContext, node
: PNode)
229 init(p
._visitor
, node
)
231 _all_variables
= p
._all_variables
236 return _set_variables
.has
(v
) or _prev
.is_set
(v
)
241 # Is the variable must be set before being used ?
242 fun must_be_set
: Bool do return false
245 redef class VarVariable
246 redef fun must_be_set
do return true