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.
23 redef class ToolContext
24 # Run `APropdef::do_scope` on each propdef.
25 var scope_phase
: Phase = new ScopePhase(self, null)
28 private class ScopePhase
30 redef fun process_npropdef
(npropdef
) do npropdef
.do_scope
(toolcontext
)
34 # A local variable (including parameters, automatic variables and self)
36 # The name of the variable (as used in the program)
37 var name
: String is writable
40 redef fun to_s
do return self.name
42 # The declaration of the variable, if any
43 var location
: nullable Location = null is writable
45 # Is the local variable not read and need a warning?
46 var warn_unread
= false is writable
49 # Mark where break and continue will branch.
50 # Marks are either associated with a label of with a for_loop structure
52 # The name of the label (unless the mark is an anonymous loop mark)
53 var name
: nullable String
55 # The associated `continue` mark, if any.
56 # If the mark attached to a loop (loop, while, for), a distinct mark is used.
57 private var continue_mark
: nullable EscapeMark = null
59 # Each break/continue attached to the mark
60 var escapes
= new Array[AEscapeExpr]
63 # Visit a npropdef and:
64 # * Identify variables and labels
65 # * Associate each break and continue to its escapemark
66 # * Transform `ACallFormExpr` that access a variable into `AVarFormExpr`
67 # FIXME: Should the class be private?
68 private class ScopeVisitor
71 # The tool context used to display errors
72 var toolcontext
: ToolContext
74 var selfvariable
= new Variable("self")
81 # All stacked scope. `scopes.first` is the current scope
82 var scopes
= new List[Scope]
84 # Shift and check the last scope
87 assert not scopes
.is_empty
88 var scope
= scopes
.shift
89 for v
in scope
.variables
.values
do
91 toolcontext
.advice
(v
.location
, "unread-variable", "Warning: local variable {v.name} is never read.")
96 # Register a local variable.
97 # Display an error on toolcontext if a variable with the same name is masked.
98 fun register_variable
(node
: ANode, variable
: Variable): Bool
100 var name
= variable
.name
101 var found
= search_variable
(name
)
102 if found
!= null then
103 self.error
(node
, "Error: a variable named `{name}` already exists.")
106 scopes
.first
.variables
[name
] = variable
107 variable
.location
= node
.location
111 # Look for a variable named `name`.
112 # Return null if no such a variable is found.
113 fun search_variable
(name
: String): nullable Variable
115 for scope
in scopes
do
116 var res
= scope
.get_variable
(name
)
126 n
.accept_scope_visitor
(self)
129 # Enter in a statement block `node` as inside a new scope.
130 # The block can be optionally attached to an `escapemark`.
131 fun enter_visit_block
(node
: nullable AExpr, escapemark
: nullable EscapeMark)
133 if node
== null then return
134 var scope
= new Scope
135 scope
.escapemark
= escapemark
136 scopes
.unshift
(scope
)
141 # Look for a label `name`.
142 # Return null if no such a label is found.
143 fun search_label
(name
: String): nullable EscapeMark
145 for scope
in scopes
do
146 var res
= scope
.escapemark
147 if res
!= null and res
.name
== name
then
154 # Create a new escape mark (possibly with a label)
155 # Display an error on toolcontext if a label with the same name is masked.
156 fun make_escape_mark
(nlabel
: nullable ALabel, for_loop
: Bool): EscapeMark
158 var name
: nullable String
159 if nlabel
!= null then
160 var nid
= nlabel
.n_id
162 var res
= search_label
("")
164 self.error
(nlabel
, "Syntax Error: anonymous label already defined.")
169 var found
= self.search_label
(name
)
170 if found
!= null then
171 self.error
(nlabel
, "Syntax Error: label `{name}` already defined.")
177 var res
= new EscapeMark(name
)
178 if for_loop
then res
.continue_mark
= new EscapeMark(name
)
182 # Look for an escape mark optionally associated with a label.
183 # If a label is given, the escapemark of this label is returned.
184 # If there is no label, the nearest escapemark that is `for loop` is returned.
185 # If there is no valid escapemark, then an error is displayed ans null is returned.
186 # Return null if no such a label is found.
187 fun get_escapemark
(node
: ANode, nlabel
: nullable ALabel): nullable EscapeMark
189 if nlabel
!= null then
190 var nid
= nlabel
.n_id
192 var res
= search_label
("")
194 self.error
(nlabel
, "Syntax Error: invalid anonymous label.")
195 node
.is_broken
= true
201 var res
= search_label
(name
)
203 self.error
(nlabel
, "Syntax Error: invalid label `{name}`.")
204 node
.is_broken
= true
209 for scope
in scopes
do
210 var res
= scope
.escapemark
215 self.error
(node
, "Syntax Error: `break` statement outside block.")
221 fun error
(node
: ANode, message
: String)
223 self.toolcontext
.error
(node
.hot_location
, message
)
224 node
.is_broken
= true
229 var variables
= new HashMap[String, Variable]
231 var escapemark
: nullable EscapeMark = null
233 fun get_variable
(name
: String): nullable Variable
235 if self.variables
.has_key
(name
) then
236 return self.variables
[name
]
244 private fun accept_scope_visitor
(v
: ScopeVisitor)
251 # Entry point of the scope analysis
252 fun do_scope
(toolcontext
: ToolContext)
254 var v
= new ScopeVisitor(toolcontext
)
261 # The variable associated with the parameter
262 var variable
: nullable Variable
263 redef fun accept_scope_visitor
(v
)
267 var variable
= new Variable(nid
.text
)
268 v
.register_variable
(nid
, variable
)
269 self.variable
= variable
273 redef class AVardeclExpr
274 # The variable associated with the variable declaration
275 var variable
: nullable Variable
276 redef fun accept_scope_visitor
(v
)
280 var variable
= new Variable(nid
.text
)
281 v
.register_variable
(nid
, variable
)
282 variable
.warn_unread
= true # wait for some read mark.
283 self.variable
= variable
287 redef class ASelfExpr
288 # The variable associated with the self receiver
289 var variable
: nullable Variable
290 redef fun accept_scope_visitor
(v
)
293 self.variable
= v
.selfvariable
297 redef class AEscapeExpr
298 # The escape mark associated with the break/continue
299 var escapemark
: nullable EscapeMark
302 redef class AContinueExpr
303 redef fun accept_scope_visitor
(v
)
306 var escapemark
= v
.get_escapemark
(self, self.n_label
)
307 if escapemark
== null then return # Skip error
308 escapemark
= escapemark
.continue_mark
309 if escapemark
== null then
310 v
.error
(self, "Error: cannot 'continue', only 'break'.")
313 escapemark
.escapes
.add
(self)
314 self.escapemark
= escapemark
318 redef class ABreakExpr
319 redef fun accept_scope_visitor
(v
)
322 var escapemark
= v
.get_escapemark
(self, self.n_label
)
323 if escapemark
== null then return # Skip error
324 escapemark
.escapes
.add
(self)
325 self.escapemark
= escapemark
331 # The break escape mark associated with the 'do' block
332 var break_mark
: nullable EscapeMark
334 redef fun accept_scope_visitor
(v
)
336 self.break_mark
= v
.make_escape_mark
(n_label
, false)
337 v
.enter_visit_block
(n_block
, self.break_mark
)
342 redef fun accept_scope_visitor
(v
)
344 v
.enter_visit
(n_expr
)
345 v
.enter_visit_block
(n_then
, null)
346 v
.enter_visit_block
(n_else
, null)
350 redef class AWhileExpr
351 # The break escape mark associated with the 'while'
352 var break_mark
: nullable EscapeMark
354 # The continue escape mark associated with the 'while'
355 var continue_mark
: nullable EscapeMark
357 redef fun accept_scope_visitor
(v
)
359 var escapemark
= v
.make_escape_mark
(n_label
, true)
360 self.break_mark
= escapemark
361 self.continue_mark
= escapemark
.continue_mark
362 v
.enter_visit
(n_expr
)
363 v
.enter_visit_block
(n_block
, escapemark
)
367 redef class ALoopExpr
368 # The break escape mark associated with the 'loop'
369 var break_mark
: nullable EscapeMark
371 # The continue escape mark associated with the 'loop'
372 var continue_mark
: nullable EscapeMark
374 redef fun accept_scope_visitor
(v
)
376 var escapemark
= v
.make_escape_mark
(n_label
, true)
377 self.break_mark
= escapemark
378 self.continue_mark
= escapemark
.continue_mark
379 v
.enter_visit_block
(n_block
, escapemark
)
384 # The break escape mark associated with the 'for'
385 var break_mark
: nullable EscapeMark
387 # The continue escape mark associated with the 'for'
388 var continue_mark
: nullable EscapeMark
390 redef fun accept_scope_visitor
(v
)
393 v
.enter_visit
(g
.n_expr
)
396 # Protect automatic variables
397 v
.scopes
.unshift
(new Scope)
400 # Create the automatic variables
401 var variables
= new Array[Variable]
402 g
.variables
= variables
403 for nid
in g
.n_ids
do
404 var va
= new Variable(nid
.text
)
405 v
.register_variable
(nid
, va
)
410 var escapemark
= v
.make_escape_mark
(n_label
, true)
411 self.break_mark
= escapemark
412 self.continue_mark
= escapemark
.continue_mark
413 v
.enter_visit_block
(n_block
, escapemark
)
419 redef class AForGroup
420 # The automatic variables in order
421 var variables
: nullable Array[Variable]
424 redef class AWithExpr
425 # The break escape mark associated with the 'with'
426 var break_mark
: nullable EscapeMark
428 redef fun accept_scope_visitor
(v
)
430 v
.scopes
.unshift
(new Scope)
432 var escapemark
= v
.make_escape_mark
(n_label
, true)
433 self.break_mark
= escapemark
435 v
.enter_visit
(n_expr
)
436 v
.enter_visit_block
(n_block
, escapemark
)
442 redef class AAssertExpr
443 redef fun accept_scope_visitor
(v
)
445 v
.enter_visit
(n_expr
)
446 v
.enter_visit_block
(n_else
, null)
450 redef class AVarFormExpr
451 # The associated variable
452 var variable
: nullable Variable is writable
455 redef class ACallFormExpr
456 redef fun accept_scope_visitor
(v
)
458 if n_expr
isa AImplicitSelfExpr then
459 var name
= n_qid
.n_id
.text
460 var variable
= v
.search_variable
(name
)
461 if variable
!= null then
463 if not n_args
.n_exprs
.is_empty
or n_args
isa AParExprs then
464 v
.error
(self, "Error: `{name}` is a variable, not a method.")
467 n
= variable_create
(variable
)
468 n
.variable
= variable
470 n
.accept_scope_visitor
(v
)
478 # Create a variable access corresponding to the call form
479 private fun variable_create
(variable
: Variable): AVarFormExpr is abstract
482 redef class ACallExpr
483 redef fun variable_create
(variable
)
485 variable
.warn_unread
= false
486 return new AVarExpr.init_avarexpr
(n_qid
.n_id
)
490 redef class ACallAssignExpr
491 redef fun variable_create
(variable
)
493 return new AVarAssignExpr.init_avarassignexpr
(n_qid
.n_id
, n_assign
, n_value
)
497 redef class ACallReassignExpr
498 redef fun variable_create
(variable
)
500 variable
.warn_unread
= false
501 return new AVarReassignExpr.init_avarreassignexpr
(n_qid
.n_id
, n_assign_op
, n_value
)