4cd879b372de8a4ac5235e699c2702414b8e6a8b
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.")
55 # The effective static type of a given variable
56 # May be different from the declaration static type
57 fun stype
(v
: Variable): nullable MMType
59 if _stypes
.has_key
(v
) then
66 # Set effective static type of a given variable
67 # May be different from the declaration static type
68 fun stype
=(v
: Variable, t
: nullable MMType)
73 # Variables by name (in the current context only)
74 var _dico
: Map[Symbol, Variable]
76 # All variables in all contextes
77 var _all_variables
: Set[Variable]
79 # Updated static type of variables
80 var _stypes
: Map[Variable, nullable MMType] = new HashMap[Variable, nullable MMType]
82 # Build a new VariableContext
83 fun sub
(node
: ANode): SubVariableContext
85 return new SubVariableContext.with_prev
(self, node
)
88 # Build a nested VariableContext with new variable information
89 fun sub_with
(node
: ANode, v
: Variable, t
: MMType): SubVariableContext
96 # The visitor of the context (used to display error)
97 var _visitor
: AbsSyntaxVisitor
99 # The syntax node that introduced the context
100 readable var _node
: ANode
102 init(visitor
: AbsSyntaxVisitor, node
: ANode)
106 _dico
= new HashMap[Symbol, Variable]
109 # Is a control flow break met? (return, break, continue)
110 readable writable var _unreash
: Bool = false
112 # Is a control flow already broken?
113 # Used to avoid repeating the same error message
114 readable writable var _already_unreash
: Bool = false
116 # Set of variable that are set (assigned)
117 readable var _set_variables
: HashSet[Variable] = new HashSet[Variable]
120 fun is_set
(v
: Variable): Bool
122 return _set_variables
.has
(v
)
125 # Merge back one flow context information
126 fun merge
(ctx
: VariableContext)
130 if ctx
.already_unreash
then already_unreash
= true
133 for v
in _all_variables
do
134 if not is_set
(v
) and ctx
.is_set
(v
) then
138 var s1
= ctx
.stype
(v
)
139 if s1
!= s
then stype
(v
) = s1
143 # Merge back two alternative flow context informations
144 fun merge2
(ctx1
, ctx2
, basectx
: VariableContext)
148 else if ctx2
.unreash
then
151 for v
in _all_variables
do
152 if not is_set
(v
) and ctx1
.is_set
(v
) and ctx2
.is_set
(v
) then
157 var s1
= ctx1
.stype
(v
)
158 var s2
= ctx2
.stype
(v
)
159 if s1
== s
and s2
== s
then
161 else if s1
== s2
then
163 else if s2
== null or s1
< s2
then
165 else if s1
== null or s2
< s1
then
168 stype
(v
) = basectx
.stype
(v
)
176 s
.append
(node
.location
.to_s
)
177 for v
in _all_variables
do
179 if t
== null then continue
186 class RootVariableContext
187 special VariableContext
188 init(visitor
: AbsSyntaxVisitor, node
: ANode)
191 _all_variables
= new HashSet[Variable]
195 class SubVariableContext
196 special VariableContext
197 readable var _prev
: VariableContext
201 if _dico
.has_key
(s
) then
210 if _stypes
.has_key
(v
) then
217 init with_prev
(p
: VariableContext, node
: ANode)
219 init(p
._visitor
, node
)
221 _all_variables
= p
._all_variables
226 return _set_variables
.has
(v
) or _prev
.is_set
(v
)
231 # Is the variable must be set before being used ?
232 fun must_be_set
: Bool do return false
235 redef class VarVariable
236 redef fun must_be_set
do return true