nitc/scope: attach AReturnExpr to an escape mark of the whole propdef
[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 # The analysed property
75 var propdef: APropdef
76
77 var selfvariable = new Variable("self")
78
79 init
80 do
81 scopes.add(new Scope)
82 end
83
84 # All stacked scope. `scopes.first` is the current scope
85 var scopes = new List[Scope]
86
87 # Shift and check the last scope
88 fun shift_scope
89 do
90 assert not scopes.is_empty
91 var scope = scopes.shift
92 for v in scope.variables.values do
93 if v.warn_unread then
94 toolcontext.advice(v.location, "unread-variable", "Warning: local variable {v.name} is never read.")
95 end
96 end
97 end
98
99 # Register a local variable.
100 # Display an error on toolcontext if a variable with the same name is masked.
101 fun register_variable(node: ANode, variable: Variable): Bool
102 do
103 var name = variable.name
104 var found = search_variable(name)
105 if found != null then
106 self.error(node, "Error: a variable named `{name}` already exists.")
107 return false
108 end
109 scopes.first.variables[name] = variable
110 variable.location = node.location
111 return true
112 end
113
114 # Look for a variable named `name`.
115 # Return null if no such a variable is found.
116 fun search_variable(name: String): nullable Variable
117 do
118 for scope in scopes do
119 var res = scope.get_variable(name)
120 if res != null then
121 return res
122 end
123 end
124 return null
125 end
126
127 redef fun visit(n)
128 do
129 n.accept_scope_visitor(self)
130 end
131
132 # Enter in a statement block `node` as inside a new scope.
133 # The block can be optionally attached to an `escapemark`.
134 fun enter_visit_block(node: nullable AExpr, escapemark: nullable EscapeMark)
135 do
136 if node == null then return
137 var scope = new Scope
138 scope.escapemark = escapemark
139 scopes.unshift(scope)
140 enter_visit(node)
141 shift_scope
142 end
143
144 # Look for a label `name`.
145 # Return null if no such a label is found.
146 fun search_label(name: String): nullable EscapeMark
147 do
148 for scope in scopes do
149 var res = scope.escapemark
150 if res != null and res.name == name then
151 return res
152 end
153 end
154 return null
155 end
156
157 # Create a new escape mark (possibly with a label)
158 # Display an error on toolcontext if a label with the same name is masked.
159 fun make_escape_mark(nlabel: nullable ALabel, for_loop: Bool): EscapeMark
160 do
161 var name: nullable String
162 if nlabel != null then
163 var nid = nlabel.n_id
164 if nid == null then
165 var res = search_label("")
166 if res != null then
167 self.error(nlabel, "Syntax Error: anonymous label already defined.")
168 end
169 name = ""
170 else
171 name = nid.text
172 var found = self.search_label(name)
173 if found != null then
174 self.error(nlabel, "Syntax Error: label `{name}` already defined.")
175 end
176 end
177 else
178 name = null
179 end
180 var res = new EscapeMark(name)
181 if for_loop then res.continue_mark = new EscapeMark(name)
182 return res
183 end
184
185 # Look for an escape mark optionally associated with a label.
186 # If a label is given, the escapemark of this label is returned.
187 # If there is no label, the nearest escapemark that is `for loop` is returned.
188 # If there is no valid escapemark, then an error is displayed ans null is returned.
189 # Return null if no such a label is found.
190 fun get_escapemark(node: ANode, nlabel: nullable ALabel): nullable EscapeMark
191 do
192 if nlabel != null then
193 var nid = nlabel.n_id
194 if nid == null then
195 var res = search_label("")
196 if res == null then
197 self.error(nlabel, "Syntax Error: invalid anonymous label.")
198 node.is_broken = true
199 return null
200 end
201 return res
202 end
203 var name = nid.text
204 var res = search_label(name)
205 if res == null then
206 self.error(nlabel, "Syntax Error: invalid label `{name}`.")
207 node.is_broken = true
208 return null
209 end
210 return res
211 else
212 for scope in scopes do
213 var res = scope.escapemark
214 if res != null then
215 return res
216 end
217 end
218 self.error(node, "Syntax Error: `break` statement outside block.")
219 return null
220 end
221 end
222
223 # Display an error
224 fun error(node: ANode, message: String)
225 do
226 self.toolcontext.error(node.hot_location, message)
227 node.is_broken = true
228 end
229 end
230
231 private class Scope
232 var variables = new HashMap[String, Variable]
233
234 var escapemark: nullable EscapeMark = null
235
236 fun get_variable(name: String): nullable Variable
237 do
238 if self.variables.has_key(name) then
239 return self.variables[name]
240 else
241 return null
242 end
243 end
244 end
245
246 redef class ANode
247 private fun accept_scope_visitor(v: ScopeVisitor)
248 do
249 visit_all(v)
250 end
251 end
252
253 redef class APropdef
254 # The break escape mark associated with the return
255 var return_mark: nullable EscapeMark
256
257 # Entry point of the scope analysis
258 fun do_scope(toolcontext: ToolContext)
259 do
260 var v = new ScopeVisitor(toolcontext, self)
261 v.enter_visit(self)
262 v.shift_scope
263 end
264 end
265
266 redef class AParam
267 # The variable associated with the parameter
268 var variable: nullable Variable
269 redef fun accept_scope_visitor(v)
270 do
271 super
272 var nid = self.n_id
273 var variable = new Variable(nid.text)
274 v.register_variable(nid, variable)
275 self.variable = variable
276 end
277 end
278
279 redef class AVardeclExpr
280 # The variable associated with the variable declaration
281 var variable: nullable Variable
282 redef fun accept_scope_visitor(v)
283 do
284 super
285 var nid = self.n_id
286 var variable = new Variable(nid.text)
287 v.register_variable(nid, variable)
288 variable.warn_unread = true # wait for some read mark.
289 self.variable = variable
290 end
291 end
292
293 redef class ASelfExpr
294 # The variable associated with the self receiver
295 var variable: nullable Variable
296 redef fun accept_scope_visitor(v)
297 do
298 super
299 self.variable = v.selfvariable
300 end
301 end
302
303 redef class AEscapeExpr
304 # The escape mark associated with the break/continue
305 var escapemark: nullable EscapeMark
306 end
307
308 redef class AContinueExpr
309 redef fun accept_scope_visitor(v)
310 do
311 super
312 var escapemark = v.get_escapemark(self, self.n_label)
313 if escapemark == null then return # Skip error
314 escapemark = escapemark.continue_mark
315 if escapemark == null then
316 v.error(self, "Error: cannot 'continue', only 'break'.")
317 return
318 end
319 escapemark.escapes.add(self)
320 self.escapemark = escapemark
321 end
322 end
323
324 redef class ABreakExpr
325 redef fun accept_scope_visitor(v)
326 do
327 super
328 var escapemark = v.get_escapemark(self, self.n_label)
329 if escapemark == null then return # Skip error
330 escapemark.escapes.add(self)
331 self.escapemark = escapemark
332 end
333 end
334
335 redef class AReturnExpr
336 redef fun accept_scope_visitor(v)
337 do
338 super
339
340 var escapemark = v.propdef.return_mark
341 if escapemark == null then
342 escapemark = new EscapeMark
343 v.propdef.return_mark = escapemark
344 end
345
346 escapemark.escapes.add(self)
347 self.escapemark = escapemark
348 end
349 end
350
351 redef class ADoExpr
352 # The break escape mark associated with the 'do' block
353 var break_mark: nullable EscapeMark
354
355 redef fun accept_scope_visitor(v)
356 do
357 self.break_mark = v.make_escape_mark(n_label, false)
358 v.enter_visit_block(n_block, self.break_mark)
359 end
360 end
361
362 redef class AIfExpr
363 redef fun accept_scope_visitor(v)
364 do
365 v.enter_visit(n_expr)
366 v.enter_visit_block(n_then, null)
367 v.enter_visit_block(n_else, null)
368 end
369 end
370
371 redef class AWhileExpr
372 # The break escape mark associated with the 'while'
373 var break_mark: nullable EscapeMark
374
375 # The continue escape mark associated with the 'while'
376 var continue_mark: nullable EscapeMark
377
378 redef fun accept_scope_visitor(v)
379 do
380 var escapemark = v.make_escape_mark(n_label, true)
381 self.break_mark = escapemark
382 self.continue_mark = escapemark.continue_mark
383 v.enter_visit(n_expr)
384 v.enter_visit_block(n_block, escapemark)
385 end
386 end
387
388 redef class ALoopExpr
389 # The break escape mark associated with the 'loop'
390 var break_mark: nullable EscapeMark
391
392 # The continue escape mark associated with the 'loop'
393 var continue_mark: nullable EscapeMark
394
395 redef fun accept_scope_visitor(v)
396 do
397 var escapemark = v.make_escape_mark(n_label, true)
398 self.break_mark = escapemark
399 self.continue_mark = escapemark.continue_mark
400 v.enter_visit_block(n_block, escapemark)
401 end
402 end
403
404 redef class AForExpr
405 # The break escape mark associated with the 'for'
406 var break_mark: nullable EscapeMark
407
408 # The continue escape mark associated with the 'for'
409 var continue_mark: nullable EscapeMark
410
411 redef fun accept_scope_visitor(v)
412 do
413 for g in n_groups do
414 v.enter_visit(g.n_expr)
415 end
416
417 # Protect automatic variables
418 v.scopes.unshift(new Scope)
419
420 for g in n_groups do
421 # Create the automatic variables
422 var variables = new Array[Variable]
423 g.variables = variables
424 for nid in g.n_ids do
425 var va = new Variable(nid.text)
426 v.register_variable(nid, va)
427 variables.add(va)
428 end
429 end
430
431 var escapemark = v.make_escape_mark(n_label, true)
432 self.break_mark = escapemark
433 self.continue_mark = escapemark.continue_mark
434 v.enter_visit_block(n_block, escapemark)
435
436 v.shift_scope
437 end
438 end
439
440 redef class AForGroup
441 # The automatic variables in order
442 var variables: nullable Array[Variable]
443 end
444
445 redef class AWithExpr
446 # The break escape mark associated with the 'with'
447 var break_mark: nullable EscapeMark
448
449 redef fun accept_scope_visitor(v)
450 do
451 v.scopes.unshift(new Scope)
452
453 var escapemark = v.make_escape_mark(n_label, true)
454 self.break_mark = escapemark
455
456 v.enter_visit(n_expr)
457 v.enter_visit_block(n_block, escapemark)
458
459 v.shift_scope
460 end
461 end
462
463 redef class AAssertExpr
464 redef fun accept_scope_visitor(v)
465 do
466 v.enter_visit(n_expr)
467 v.enter_visit_block(n_else, null)
468 end
469 end
470
471 redef class AVarFormExpr
472 # The associated variable
473 var variable: nullable Variable is writable
474 end
475
476 redef class ACallFormExpr
477 redef fun accept_scope_visitor(v)
478 do
479 if n_expr isa AImplicitSelfExpr then
480 var name = n_qid.n_id.text
481 var variable = v.search_variable(name)
482 if variable != null then
483 var n: AExpr
484 if not n_args.n_exprs.is_empty or n_args isa AParExprs then
485 v.error(self, "Error: `{name}` is a variable, not a method.")
486 return
487 end
488 n = variable_create(variable)
489 n.variable = variable
490 replace_with(n)
491 n.accept_scope_visitor(v)
492 return
493 end
494 end
495
496 super
497 end
498
499 # Create a variable access corresponding to the call form
500 private fun variable_create(variable: Variable): AVarFormExpr is abstract
501 end
502
503 redef class ACallExpr
504 redef fun variable_create(variable)
505 do
506 variable.warn_unread = false
507 return new AVarExpr.init_avarexpr(n_qid.n_id)
508 end
509 end
510
511 redef class ACallAssignExpr
512 redef fun variable_create(variable)
513 do
514 return new AVarAssignExpr.init_avarassignexpr(n_qid.n_id, n_assign, n_value)
515 end
516 end
517
518 redef class ACallReassignExpr
519 redef fun variable_create(variable)
520 do
521 variable.warn_unread = false
522 return new AVarReassignExpr.init_avarreassignexpr(n_qid.n_id, n_assign_op, n_value)
523 end
524 end