Merge: Multi iterators
[nit.git] / src / semantize / scope.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2012 Jean Privat <jean@pryen.org>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 # Identification and scoping of local variables and labels.
18 module scope
19
20 import phase
21 import modelbuilder
22
23 redef class ToolContext
24 # Run `APropdef::do_scope` on each propdef.
25 var scope_phase: Phase = new ScopePhase(self, null)
26 end
27
28 private class ScopePhase
29 super Phase
30 redef fun process_npropdef(npropdef) do npropdef.do_scope(toolcontext)
31 end
32
33
34 # A local variable (including parameters, automatic variables and self)
35 class Variable
36 # The name of the variable (as used in the program)
37 var name: String is writable
38
39 # Alias of `name`
40 redef fun to_s do return self.name
41
42 # The declaration of the variable, if any
43 var location: nullable Location = null is writable
44
45 # Is the local variable not read and need a warning?
46 var warn_unread = false is writable
47 end
48
49 # Mark where break and continue will branch.
50 # Marks are either associated with a label of with a for_loop structure
51 class EscapeMark
52 # The name of the label (unless the mark is an anonymous loop mark)
53 var name: nullable String
54
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
58
59 # Each break/continue attached to the mark
60 var escapes = new Array[AEscapeExpr]
61 end
62
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
69 super Visitor
70
71 # The tool context used to display errors
72 var toolcontext: ToolContext
73
74 var selfvariable = new Variable("self")
75
76 init
77 do
78 scopes.add(new Scope)
79 end
80
81 # All stacked scope. `scopes.first` is the current scope
82 var scopes = new List[Scope]
83
84 # Shift and check the last scope
85 fun shift_scope
86 do
87 assert not scopes.is_empty
88 var scope = scopes.shift
89 for v in scope.variables.values do
90 if v.warn_unread then
91 toolcontext.advice(v.location, "unread-variable", "Warning: local variable {v.name} is never read.")
92 end
93 end
94 end
95
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
99 do
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.")
104 return false
105 end
106 scopes.first.variables[name] = variable
107 variable.location = node.location
108 return true
109 end
110
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
114 do
115 for scope in scopes do
116 var res = scope.get_variable(name)
117 if res != null then
118 return res
119 end
120 end
121 return null
122 end
123
124 redef fun visit(n)
125 do
126 n.accept_scope_visitor(self)
127 end
128
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)
132 do
133 if node == null then return
134 var scope = new Scope
135 scope.escapemark = escapemark
136 scopes.unshift(scope)
137 enter_visit(node)
138 shift_scope
139 end
140
141 # Look for a label `name`.
142 # Return null if no such a label is found.
143 fun search_label(name: String): nullable EscapeMark
144 do
145 for scope in scopes do
146 var res = scope.escapemark
147 if res != null and res.name == name then
148 return res
149 end
150 end
151 return null
152 end
153
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
157 do
158 var name: nullable String
159 if nlabel != null then
160 var nid = nlabel.n_id
161 if nid == null then
162 var res = search_label("")
163 if res != null then
164 self.error(nlabel, "Syntax Error: anonymous label already defined.")
165 end
166 name = ""
167 else
168 name = nid.text
169 var found = self.search_label(name)
170 if found != null then
171 self.error(nlabel, "Syntax Error: label `{name}` already defined.")
172 end
173 end
174 else
175 name = null
176 end
177 var res = new EscapeMark(name)
178 if for_loop then res.continue_mark = new EscapeMark(name)
179 return res
180 end
181
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
188 do
189 if nlabel != null then
190 var nid = nlabel.n_id
191 if nid == null then
192 var res = search_label("")
193 if res == null then
194 self.error(nlabel, "Syntax Error: invalid anonymous label.")
195 node.is_broken = true
196 return null
197 end
198 return res
199 end
200 var name = nid.text
201 var res = search_label(name)
202 if res == null then
203 self.error(nlabel, "Syntax Error: invalid label `{name}`.")
204 node.is_broken = true
205 return null
206 end
207 return res
208 else
209 for scope in scopes do
210 var res = scope.escapemark
211 if res != null then
212 return res
213 end
214 end
215 self.error(node, "Syntax Error: `break` statement outside block.")
216 return null
217 end
218 end
219
220 # Display an error
221 fun error(node: ANode, message: String)
222 do
223 self.toolcontext.error(node.hot_location, message)
224 node.is_broken = true
225 end
226 end
227
228 private class Scope
229 var variables = new HashMap[String, Variable]
230
231 var escapemark: nullable EscapeMark = null
232
233 fun get_variable(name: String): nullable Variable
234 do
235 if self.variables.has_key(name) then
236 return self.variables[name]
237 else
238 return null
239 end
240 end
241 end
242
243 redef class ANode
244 private fun accept_scope_visitor(v: ScopeVisitor)
245 do
246 visit_all(v)
247 end
248 end
249
250 redef class APropdef
251 # Entry point of the scope analysis
252 fun do_scope(toolcontext: ToolContext)
253 do
254 var v = new ScopeVisitor(toolcontext)
255 v.enter_visit(self)
256 v.shift_scope
257 end
258 end
259
260 redef class AParam
261 # The variable associated with the parameter
262 var variable: nullable Variable
263 redef fun accept_scope_visitor(v)
264 do
265 super
266 var nid = self.n_id
267 var variable = new Variable(nid.text)
268 v.register_variable(nid, variable)
269 self.variable = variable
270 end
271 end
272
273 redef class AVardeclExpr
274 # The variable associated with the variable declaration
275 var variable: nullable Variable
276 redef fun accept_scope_visitor(v)
277 do
278 super
279 var nid = self.n_id
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
284 end
285 end
286
287 redef class ASelfExpr
288 # The variable associated with the self receiver
289 var variable: nullable Variable
290 redef fun accept_scope_visitor(v)
291 do
292 super
293 self.variable = v.selfvariable
294 end
295 end
296
297 redef class AEscapeExpr
298 # The escape mark associated with the break/continue
299 var escapemark: nullable EscapeMark
300 end
301
302 redef class AContinueExpr
303 redef fun accept_scope_visitor(v)
304 do
305 super
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'.")
311 return
312 end
313 escapemark.escapes.add(self)
314 self.escapemark = escapemark
315 end
316 end
317
318 redef class ABreakExpr
319 redef fun accept_scope_visitor(v)
320 do
321 super
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
326 end
327 end
328
329
330 redef class ADoExpr
331 # The break escape mark associated with the 'do' block
332 var break_mark: nullable EscapeMark
333
334 redef fun accept_scope_visitor(v)
335 do
336 self.break_mark = v.make_escape_mark(n_label, false)
337 v.enter_visit_block(n_block, self.break_mark)
338 end
339 end
340
341 redef class AIfExpr
342 redef fun accept_scope_visitor(v)
343 do
344 v.enter_visit(n_expr)
345 v.enter_visit_block(n_then, null)
346 v.enter_visit_block(n_else, null)
347 end
348 end
349
350 redef class AWhileExpr
351 # The break escape mark associated with the 'while'
352 var break_mark: nullable EscapeMark
353
354 # The continue escape mark associated with the 'while'
355 var continue_mark: nullable EscapeMark
356
357 redef fun accept_scope_visitor(v)
358 do
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)
364 end
365 end
366
367 redef class ALoopExpr
368 # The break escape mark associated with the 'loop'
369 var break_mark: nullable EscapeMark
370
371 # The continue escape mark associated with the 'loop'
372 var continue_mark: nullable EscapeMark
373
374 redef fun accept_scope_visitor(v)
375 do
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)
380 end
381 end
382
383 redef class AForExpr
384 # The break escape mark associated with the 'for'
385 var break_mark: nullable EscapeMark
386
387 # The continue escape mark associated with the 'for'
388 var continue_mark: nullable EscapeMark
389
390 redef fun accept_scope_visitor(v)
391 do
392 for g in n_groups do
393 v.enter_visit(g.n_expr)
394 end
395
396 # Protect automatic variables
397 v.scopes.unshift(new Scope)
398
399 for g in n_groups do
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)
406 variables.add(va)
407 end
408 end
409
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)
414
415 v.shift_scope
416 end
417 end
418
419 redef class AForGroup
420 # The automatic variables in order
421 var variables: nullable Array[Variable]
422 end
423
424 redef class AWithExpr
425 # The break escape mark associated with the 'with'
426 var break_mark: nullable EscapeMark
427
428 redef fun accept_scope_visitor(v)
429 do
430 v.scopes.unshift(new Scope)
431
432 var escapemark = v.make_escape_mark(n_label, true)
433 self.break_mark = escapemark
434
435 v.enter_visit(n_expr)
436 v.enter_visit_block(n_block, escapemark)
437
438 v.shift_scope
439 end
440 end
441
442 redef class AAssertExpr
443 redef fun accept_scope_visitor(v)
444 do
445 v.enter_visit(n_expr)
446 v.enter_visit_block(n_else, null)
447 end
448 end
449
450 redef class AVarFormExpr
451 # The associated variable
452 var variable: nullable Variable is writable
453 end
454
455 redef class ACallFormExpr
456 redef fun accept_scope_visitor(v)
457 do
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
462 var n: AExpr
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.")
465 return
466 end
467 n = variable_create(variable)
468 n.variable = variable
469 replace_with(n)
470 n.accept_scope_visitor(v)
471 return
472 end
473 end
474
475 super
476 end
477
478 # Create a variable access corresponding to the call form
479 private fun variable_create(variable: Variable): AVarFormExpr is abstract
480 end
481
482 redef class ACallExpr
483 redef fun variable_create(variable)
484 do
485 variable.warn_unread = false
486 return new AVarExpr.init_avarexpr(n_qid.n_id)
487 end
488 end
489
490 redef class ACallAssignExpr
491 redef fun variable_create(variable)
492 do
493 return new AVarAssignExpr.init_avarassignexpr(n_qid.n_id, n_assign, n_value)
494 end
495 end
496
497 redef class ACallReassignExpr
498 redef fun variable_create(variable)
499 do
500 variable.warn_unread = false
501 return new AVarReassignExpr.init_avarreassignexpr(n_qid.n_id, n_assign_op, n_value)
502 end
503 end