1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012 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 # Identification and scoping of local variables and labels.
22 redef class ToolContext
23 var scope_phase
: Phase = new ScopePhase(self, null)
26 private class ScopePhase
28 redef fun process_npropdef
(npropdef
) do npropdef
.do_scope
(toolcontext
)
32 # A local variable (including parameters, automatic variables and self)
34 # The name of the variable (as used in the program)
38 redef fun to_s
do return self.name
40 # The declaration of the variable, if any
41 var location
: nullable Location = null
43 # Is the local variable not read and need a warning?
44 var warn_unread
= false is writable
47 # Mark where break and continue will branch.
48 # Marks are either associated with a label of with a for_loop structure
50 # The name of the label (unless the mark is an anonymous loop mark)
51 var name
: nullable String
53 # The associated `continue` mark, if any.
54 # If the mark attached to a loop (loop, while, for), a distinct mark is used.
55 private var continue_mark
: nullable EscapeMark = null
57 # Each break/continue attached to the mark
58 var escapes
= new Array[AEscapeExpr]
61 # Visit a npropdef and:
62 # * Identify variables and labels
63 # * Associate each break and continue to its escapemark
64 # * Transform `ACallFormExpr` that access a variable into `AVarFormExpr`
65 # FIXME: Should the class be private?
66 private class ScopeVisitor
69 # The tool context used to display errors
70 var toolcontext
: ToolContext
72 var selfvariable
= new Variable("self")
79 # All stacked scope. `scopes.first` is the current scope
80 var scopes
= new List[Scope]
82 # Shift and check the last scope
85 assert not scopes
.is_empty
86 var scope
= scopes
.shift
87 for v
in scope
.variables
.values
do
89 toolcontext
.advice
(v
.location
, "unread-variable", "Warning: local variable {v.name} is never read.")
94 # Register a local variable.
95 # Display an error on toolcontext if a variable with the same name is masked.
96 fun register_variable
(node
: ANode, variable
: Variable): Bool
98 var name
= variable
.name
99 var found
= search_variable
(name
)
100 if found
!= null then
101 self.error
(node
, "Error: A variable named `{name}' already exists")
104 scopes
.first
.variables
[name
] = variable
105 variable
.location
= node
.location
109 # Look for a variable named `name`.
110 # Return null if no such a variable is found.
111 fun search_variable
(name
: String): nullable Variable
113 for scope
in scopes
do
114 var res
= scope
.get_variable
(name
)
124 n
.accept_scope_visitor
(self)
127 # Enter in a statement block `node` as inside a new scope.
128 # The block can be optionally attached to an `escapemark`.
129 fun enter_visit_block
(node
: nullable AExpr, escapemark
: nullable EscapeMark)
131 if node
== null then return
132 var scope
= new Scope
133 scope
.escapemark
= escapemark
134 scopes
.unshift
(scope
)
139 # Look for a label `name`.
140 # Return null if no such a label is found.
141 fun search_label
(name
: String): nullable EscapeMark
143 for scope
in scopes
do
144 var res
= scope
.escapemark
145 if res
!= null and res
.name
== name
then
152 # Create a new escape mark (possibly with a label)
153 # Display an error on toolcontext if a label with the same name is masked.
154 fun make_escape_mark
(nlabel
: nullable ALabel, for_loop
: Bool): EscapeMark
156 var name
: nullable String
157 if nlabel
!= null then
158 var nid
= nlabel
.n_id
160 var res
= search_label
("")
162 self.error
(nlabel
, "Syntax error: anonymous label already defined.")
167 var found
= self.search_label
(name
)
168 if found
!= null then
169 self.error
(nlabel
, "Syntax error: label {name} already defined.")
175 var res
= new EscapeMark(name
)
176 if for_loop
then res
.continue_mark
= new EscapeMark(name
)
180 # Look for an escape mark optionally associated with a label.
181 # If a label is given, the escapemark of this label is returned.
182 # If there is no label, the nearest escapemark that is `for loop` is returned.
183 # If there is no valid escapemark, then an error is displayed ans null is returned.
184 # Return null if no such a label is found.
185 fun get_escapemark
(node
: ANode, nlabel
: nullable ALabel): nullable EscapeMark
187 if nlabel
!= null then
188 var nid
= nlabel
.n_id
190 var res
= search_label
("")
192 self.error
(nlabel
, "Syntax error: invalid anonymous label.")
198 var res
= search_label
(name
)
200 self.error
(nlabel
, "Syntax error: invalid label {name}.")
205 for scope
in scopes
do
206 var res
= scope
.escapemark
211 self.error
(node
, "Syntax Error: 'break' statement outside block.")
217 fun error
(node
: ANode, message
: String)
219 self.toolcontext
.error
(node
.hot_location
, message
)
224 var variables
= new HashMap[String, Variable]
226 var escapemark
: nullable EscapeMark = null
228 fun get_variable
(name
: String): nullable Variable
230 if self.variables
.has_key
(name
) then
231 return self.variables
[name
]
239 private fun accept_scope_visitor
(v
: ScopeVisitor)
246 # Entry point of the scope analysis
247 fun do_scope
(toolcontext
: ToolContext)
249 var v
= new ScopeVisitor(toolcontext
)
256 # The variable associated with the parameter
257 var variable
: nullable Variable
258 redef fun accept_scope_visitor
(v
)
262 var variable
= new Variable(nid
.text
)
263 v
.register_variable
(nid
, variable
)
264 self.variable
= variable
268 redef class AVardeclExpr
269 # The variable associated with the variable declaration
270 var variable
: nullable Variable
271 redef fun accept_scope_visitor
(v
)
275 var variable
= new Variable(nid
.text
)
276 v
.register_variable
(nid
, variable
)
277 variable
.warn_unread
= true # wait for some read mark.
278 self.variable
= variable
282 redef class ASelfExpr
283 # The variable associated with the self receiver
284 var variable
: nullable Variable
285 redef fun accept_scope_visitor
(v
)
288 self.variable
= v
.selfvariable
292 redef class AEscapeExpr
293 # The escape mark associated with the break/continue
294 var escapemark
: nullable EscapeMark
297 redef class AContinueExpr
298 redef fun accept_scope_visitor
(v
)
301 var escapemark
= v
.get_escapemark
(self, self.n_label
)
302 if escapemark
== null then return # Skip error
303 escapemark
= escapemark
.continue_mark
304 if escapemark
== null then
305 v
.error
(self, "Error: cannot 'continue', only 'break'.")
308 escapemark
.escapes
.add
(self)
309 self.escapemark
= escapemark
313 redef class ABreakExpr
314 redef fun accept_scope_visitor
(v
)
317 var escapemark
= v
.get_escapemark
(self, self.n_label
)
318 if escapemark
== null then return # Skip error
319 escapemark
.escapes
.add
(self)
320 self.escapemark
= escapemark
326 # The break escape mark associated with the 'do' block
327 var break_mark
: nullable EscapeMark
329 redef fun accept_scope_visitor
(v
)
331 self.break_mark
= v
.make_escape_mark
(n_label
, false)
332 v
.enter_visit_block
(n_block
, self.break_mark
)
337 redef fun accept_scope_visitor
(v
)
339 v
.enter_visit
(n_expr
)
340 v
.enter_visit_block
(n_then
, null)
341 v
.enter_visit_block
(n_else
, null)
345 redef class AWhileExpr
346 # The break escape mark associated with the 'while'
347 var break_mark
: nullable EscapeMark
349 # The continue escape mark associated with the 'while'
350 var continue_mark
: nullable EscapeMark
352 redef fun accept_scope_visitor
(v
)
354 var escapemark
= v
.make_escape_mark
(n_label
, true)
355 self.break_mark
= escapemark
356 self.continue_mark
= escapemark
.continue_mark
357 v
.enter_visit
(n_expr
)
358 v
.enter_visit_block
(n_block
, escapemark
)
362 redef class ALoopExpr
363 # The break escape mark associated with the 'loop'
364 var break_mark
: nullable EscapeMark
366 # The continue escape mark associated with the 'loop'
367 var continue_mark
: nullable EscapeMark
369 redef fun accept_scope_visitor
(v
)
371 var escapemark
= v
.make_escape_mark
(n_label
, true)
372 self.break_mark
= escapemark
373 self.continue_mark
= escapemark
.continue_mark
374 v
.enter_visit_block
(n_block
, escapemark
)
379 # The automatic variables in order
380 var variables
: nullable Array[Variable]
382 # The break escape mark associated with the 'for'
383 var break_mark
: nullable EscapeMark
385 # The continue escape mark associated with the 'for'
386 var continue_mark
: nullable EscapeMark
388 redef fun accept_scope_visitor
(v
)
390 v
.enter_visit
(n_expr
)
392 # Protect automatic variables
393 v
.scopes
.unshift
(new Scope)
395 # Create the automatic variables
396 var variables
= new Array[Variable]
397 self.variables
= variables
399 var va
= new Variable(nid
.text
)
400 v
.register_variable
(nid
, va
)
404 var escapemark
= v
.make_escape_mark
(n_label
, true)
405 self.break_mark
= escapemark
406 self.continue_mark
= escapemark
.continue_mark
407 v
.enter_visit_block
(n_block
, escapemark
)
413 redef class AVarFormExpr
414 # The associated variable
415 var variable
: nullable Variable
418 redef class ACallFormExpr
419 redef fun accept_scope_visitor
(v
)
421 if n_expr
isa AImplicitSelfExpr then
423 var variable
= v
.search_variable
(name
)
424 if variable
!= null then
426 if not n_args
.n_exprs
.is_empty
or n_args
isa AParExprs then
427 v
.error
(self, "Error: {name} is variable, not a function.")
430 n
= variable_create
(variable
)
431 n
.variable
= variable
433 n
.accept_scope_visitor
(v
)
441 # Create a variable access corresponding to the call form
442 private fun variable_create
(variable
: Variable): AVarFormExpr is abstract
445 redef class ACallExpr
446 redef fun variable_create
(variable
)
448 variable
.warn_unread
= false
449 return new AVarExpr.init_avarexpr
(n_id
)
453 redef class ACallAssignExpr
454 redef fun variable_create
(variable
)
456 return new AVarAssignExpr.init_avarassignexpr
(n_id
, n_assign
, n_value
)
460 redef class ACallReassignExpr
461 redef fun variable_create
(variable
)
463 variable
.warn_unread
= false
464 return new AVarReassignExpr.init_avarreassignexpr
(n_id
, n_assign_op
, n_value
)