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
39 var old_var
= self[v
.name
]
40 if old_var
!= null then
41 _visitor
.error
(v
.decl
, "Error: '{v}' already defined at {old_var.decl.location.relative_to(v.decl.location)}.")
47 fun mark_is_set
(v
: Variable)
52 fun check_is_set
(n
: ANode, v
: Variable)
54 if v
.must_be_set
and not is_set
(v
) then
55 _visitor
.error
(n
, "Error: variable '{v}' is possibly unset.")
56 else if v
.is_typed
and stype
(v
) == null then
57 _visitor
.error
(n
, "Error: variable '{v}' is untyped.")
61 # The effective static type of a given variable
62 # May be different from the declaration static type
63 fun stype
(v
: Variable): nullable MMType
65 if _stypes
.has_key
(v
) then
72 # Set effective static type of a given variable
73 # May be different from the declaration static type
74 fun stype
=(v
: Variable, t
: nullable MMType)
79 # Variables by name (in the current context only)
80 var _dico
: Map[Symbol, Variable]
82 # All variables in all contextes
83 var _all_variables
: Set[Variable]
85 # Updated static type of variables
86 var _stypes
: Map[Variable, nullable MMType] = new HashMap[Variable, nullable MMType]
88 # Build a new VariableContext
89 fun sub
(node
: ANode): SubVariableContext
91 return new SubVariableContext.with_prev
(self, node
)
94 # Build a nested VariableContext with new variable information
95 fun sub_with
(node
: ANode, v
: Variable, t
: MMType): SubVariableContext
102 # The visitor of the context (used to display error)
103 var _visitor
: AbsSyntaxVisitor
105 # The syntax node that introduced the context
106 readable var _node
: ANode
108 init(visitor
: AbsSyntaxVisitor, node
: ANode)
112 _dico
= new HashMap[Symbol, Variable]
115 # Is a control flow break met? (return, break, continue)
116 readable writable var _unreash
: Bool = false
118 # Is a control flow already broken?
119 # Used to avoid repeating the same error message
120 readable writable var _already_unreash
: Bool = false
122 # Set of variable that are set (assigned)
123 readable var _set_variables
: HashSet[Variable] = new HashSet[Variable]
126 fun is_set
(v
: Variable): Bool
128 return _set_variables
.has
(v
)
131 # Merge back one flow context information
132 fun merge
(ctx
: VariableContext)
136 if ctx
.already_unreash
then already_unreash
= true
139 for v
in _all_variables
do
140 if not is_set
(v
) and ctx
.is_set
(v
) then
144 var s1
= ctx
.stype
(v
)
145 if s1
!= s
then stype
(v
) = s1
149 # Merge back two alternative flow context informations
150 fun merge2
(ctx1
, ctx2
, basectx
: VariableContext)
154 else if ctx2
.unreash
then
157 for v
in _all_variables
do
158 if not is_set
(v
) and ctx1
.is_set
(v
) and ctx2
.is_set
(v
) then
163 var s1
= ctx1
.stype
(v
)
164 var s2
= ctx2
.stype
(v
)
165 if s1
== s
and s2
== s
then
167 else if s1
== s2
then
169 else if s2
== null or s1
< s2
then
171 else if s1
== null or s2
< s1
then
174 stype
(v
) = basectx
.stype
(v
)
182 s
.append
(node
.location
.to_s
)
183 for v
in _all_variables
do
185 if t
== null then continue
192 class RootVariableContext
193 special VariableContext
194 init(visitor
: AbsSyntaxVisitor, node
: ANode)
197 _all_variables
= new HashSet[Variable]
201 class SubVariableContext
202 special VariableContext
203 readable var _prev
: VariableContext
207 if _dico
.has_key
(s
) then
216 if _stypes
.has_key
(v
) then
223 init with_prev
(p
: VariableContext, node
: ANode)
225 init(p
._visitor
, node
)
227 _all_variables
= p
._all_variables
232 return _set_variables
.has
(v
) or _prev
.is_set
(v
)
237 # Is the variable must be set before being used ?
238 fun must_be_set
: Bool do return false
241 redef class VarVariable
242 redef fun must_be_set
do return true