Prevent statements to be used as expressions.
[nit.git] / src / syntax / syntax_base.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2008 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 # Common syntax structures for syntax analysis of NIT AST.
18 package syntax_base
19
20 import parser
21 import mmloader
22
23 # Concrete NIT source module
24 class MMSrcModule
25 special MMModule
26 # The related AST node
27 readable attr _node: AModule
28
29 # Concrete NIT source local classs by name
30 readable attr _src_local_classes: Map[Symbol, MMSrcLocalClass]
31
32 init(c: MMContext, source: AModule, dir: MMDirectory, name: Symbol)
33 do
34 super(name, dir, c)
35 _node = source
36 _src_local_classes = new HashMap[Symbol, MMSrcLocalClass]
37 end
38 end
39
40 redef class MMGlobalClass
41 # Check that a module can access a class
42 meth check_visibility(v: AbsSyntaxVisitor, n: PNode, cm: MMSrcModule): Bool do
43 var pm = intro.module
44 assert pm isa MMSrcModule
45 var vpm = cm.visibility_for(pm)
46 if vpm == 3 then
47 return true
48 else if vpm == 0 then
49 v.error(n, "Visibility error: Class {self} comes from the hidden module {cm}.") # TODO: should not occur
50 return false
51 else if visibility_level >= 3 then
52 v.error(n, "Visibility error: Class {self} is private.")
53 return false
54 end
55 return true
56 end
57 end
58
59 # Concrete NIT source local classes
60 class MMSrcLocalClass
61 special MMConcreteClass
62 # The related AST nodes
63 readable attr _nodes: Array[PClassdef]
64
65 # Concrete NIT source generic formal parameter by name
66 readable writable attr _formal_dict: Map[Symbol, MMTypeFormalParameter]
67
68 # Concrete NIT source properties by name
69 readable attr _src_local_properties: Map[Symbol, MMLocalProperty]
70
71 init(n: Symbol, cla: PClassdef, a: Int)
72 do
73 super(n, a)
74 _nodes = [cla]
75 _src_local_properties = new HashMap[Symbol, MMLocalProperty]
76 end
77 end
78
79 redef class MMGlobalProperty
80 # Check that a module can access a property
81 meth check_visibility(v: AbsSyntaxVisitor, n: PNode, cm: MMSrcModule, allows_protected: Bool): Bool do
82 var pm = local_class.module
83 assert pm isa MMSrcModule
84 var vpm = cm.visibility_for(pm)
85 if vpm == 3 then
86 return true
87 else if vpm == 0 then
88 # TODO: should not occurs
89 v.error(n, "Visibility error: Property {self} comes from the hidden module {cm}.")
90 return false
91 else if visibility_level >= 3 then
92 v.error(n, "Visibility error: Property {self} is private.")
93 return false
94 else if visibility_level >= 2 and not allows_protected then
95 v.error(n, "Visibility error: Property {self} is protected and can only acceded by self.")
96 return false
97 end
98 return true
99 end
100 end
101
102 redef class MMLocalProperty
103 # The attached node (if any)
104 meth node: PNode do return null
105
106 # Is the concrete method defined as init
107 meth is_init: Bool do return false
108 end
109
110 # Concrete NIT source attribute
111 class MMSrcAttribute
112 special MMAttribute
113 redef readable attr _node: AAttrPropdef
114 init(name: Symbol, cla: MMLocalClass, n: AAttrPropdef)
115 do
116 super(name, cla)
117 _node = n
118 end
119 end
120
121 # Concrete NIT source method
122 class MMSrcMethod
123 special MMMethod
124 end
125
126 # Concrete NIT source method for an automatic accesor
127 class MMAttrImplementationMethod
128 special MMSrcMethod
129 redef readable attr _node: AAttrPropdef
130 init(name: Symbol, cla: MMLocalClass, n: AAttrPropdef)
131 do
132 super(name, cla)
133 _node = n
134 end
135 end
136
137 # Concrete NIT source method for an automatic read accesor
138 class MMReadImplementationMethod
139 special MMAttrImplementationMethod
140 init(name: Symbol, cla: MMLocalClass, n: AAttrPropdef)
141 do
142 super(name, cla, n)
143 end
144 end
145
146 # Concrete NIT source method for an automatic write accesor
147 class MMWriteImplementationMethod
148 special MMAttrImplementationMethod
149 init(name: Symbol, cla: MMLocalClass, n: AAttrPropdef)
150 do
151 super(name, cla, n)
152 end
153 end
154
155 # Concrete NIT source method for an explicit method
156 class MMMethSrcMethod
157 special MMSrcMethod
158 redef meth is_init do return _node isa AConcreteInitPropdef
159 redef readable attr _node: AMethPropdef
160 init(name: Symbol, cla: MMLocalClass, n: AMethPropdef)
161 do
162 super(name, cla)
163 _node = n
164 end
165 end
166
167 # Concrete NIT source virtual type
168 class MMSrcTypeProperty
169 special MMLocalProperty
170 special MMTypeProperty
171 redef readable attr _node: ATypePropdef
172 init(name: Symbol, cla: MMLocalClass, n: ATypePropdef)
173 do
174 super(name, cla)
175 _node = n
176 end
177 end
178
179 # Concrete NIT implicit constructor
180 class MMImplicitInit
181 special MMMethSrcMethod
182 redef meth is_init do return true
183 readable attr _unassigned_attributes: Array[MMSrcAttribute]
184 readable attr _super_inits: Array[MMLocalProperty]
185 init(cla: MMLocalClass, unassigned_attributes: Array[MMSrcAttribute], super_inits: Array[MMLocalProperty])
186 do
187 super(once "init".to_symbol, cla, null)
188 _unassigned_attributes = unassigned_attributes
189 _super_inits = super_inits
190 end
191 end
192
193 # Local variables
194 abstract class Variable
195 # Name of the variable
196 readable attr _name: Symbol
197
198 # Declaration AST node
199 readable attr _decl: PNode
200
201 # Static type
202 readable writable attr _stype: MMType
203
204 redef meth to_s do return _name.to_s
205
206 meth kind: String is abstract
207
208 init(n: Symbol, d: PNode)
209 do
210 assert n != null
211 assert d != null
212 _name = n
213 _decl = d
214 end
215 end
216
217 # Variable declared with 'var'
218 class VarVariable
219 special Variable
220 redef meth kind do return once "variable"
221 init(n: Symbol, d: PNode) do super
222 end
223
224 # Parameter of method (declared in signature)
225 class ParamVariable
226 special Variable
227 redef meth kind do return once "parameter"
228 init(n: Symbol, d: PNode) do super
229 end
230
231 # Automatic variable (like in the 'for' statement)
232 class AutoVariable
233 special Variable
234 redef meth kind do return once "automatic variable"
235 init(n: Symbol, d: PNode) do super
236 end
237
238 ###############################################################################
239
240 # Visitor used during the syntax analysis
241 class AbsSyntaxVisitor
242 special Visitor
243 # The primitive type Bool
244 meth type_bool: MMType
245 do
246 return _module.class_by_name(once ("Bool".to_symbol)).get_type
247 end
248
249 # The primitive type Int
250 meth type_int: MMType
251 do
252 return _module.class_by_name(once ("Int".to_symbol)).get_type
253 end
254
255 # The primitive type Float
256 meth type_float: MMType
257 do
258 return _module.class_by_name(once ("Float".to_symbol)).get_type
259 end
260
261 # The primitive type Char
262 meth type_char: MMType
263 do
264 return _module.class_by_name(once ("Char".to_symbol)).get_type
265 end
266
267 # The primitive type String
268 meth type_string: MMType
269 do
270 return _module.class_by_name(once ("String".to_symbol)).get_type
271 end
272
273 # The primitive type Collection[Object]
274 meth type_collection: MMType
275 do
276 return _module.class_by_name(once ("Collection".to_symbol)).get_type
277 end
278
279 # The primitive type Array[?]
280 meth type_array(stype: MMType): MMType
281 do
282 return _module.class_by_name(once ("Array".to_symbol)).get_instantiate_type([stype])
283 end
284
285 # The primitive type Discrete
286 meth type_discrete: MMType
287 do
288 return _module.class_by_name(once ("Discrete".to_symbol)).get_type
289 end
290
291 # The primitive type Range[?]
292 meth type_range(stype: MMType): MMType
293 do
294 return _module.class_by_name(once ("Range".to_symbol)).get_instantiate_type([stype])
295 end
296
297 # The primitive type of null
298 meth type_none: MMType
299 do
300 return _module.type_none
301 end
302
303 # The current module
304 readable writable attr _module: MMSrcModule
305
306 # The current class
307 readable writable attr _local_class: MMSrcLocalClass
308
309 # The current property
310 readable writable attr _local_property: MMLocalProperty
311
312 # The current tool configuration/status
313 readable attr _tc: ToolContext
314
315 # Display an error for a given syntax node
316 meth error(n: PNode, s: String)
317 do
318 _tc.error("{locate(n)}: {s}")
319 end
320
321 # Display a warning for a given syntax node
322 meth warning(n: PNode, s: String)
323 do
324 _tc.warning("{locate(n)}: {s}")
325 end
326
327 #
328 meth locate(n: PNode): String
329 do
330 if n != null then return n.locate
331 return _module.filename
332 end
333
334 # Check conformity and display error
335 meth check_conform(n: PNode, subtype: MMType, stype: MMType): Bool
336 do
337 if stype == null or subtype == null then
338 return false
339 end
340 if subtype < stype then
341 return true
342 end
343 #error(n, "Type error: expected {stype}'{stype.module}, got {subtype}'{subtype.module}")
344 #abort
345 error(n, "Type error: expected {stype}, got {subtype}")
346 return false
347 end
348
349 # Check that an expression has a static type and that
350 # Display an error and return false if n is a statement
351 # Require that the static type of n is known
352 meth check_expr(n: PExpr): Bool
353 do
354 # FIXME: The tc.error_count is a workaround since currently there is no way
355 # to distingate statements from buggy expressions: both have a null stype
356 if tc.error_count == 0 and n.stype == null then
357 error(n, "Type error: expected expression.")
358 return false
359 end
360 return true
361 end
362
363 # Combine check_conform and check_expr
364 meth check_conform_expr(n: PExpr, stype: MMType): Bool
365 do
366 if check_expr(n) then return check_conform(n, n.stype, stype) else return false
367 end
368
369
370 protected init(tc: ToolContext, module: MMSrcModule)
371 do
372 _tc = tc
373 _module = module
374 end
375 end
376
377 ###############################################################################
378
379 redef class PNode
380 protected meth accept_abs_syntax_visitor(v: AbsSyntaxVisitor) do visit_all(v)
381 end
382
383 redef class Token
384 attr _symbol: Symbol
385
386 # Symbol associated with the text
387 # Lazily computed
388 meth to_symbol: Symbol
389 do
390 var s = _symbol
391 if s == null then
392 s = text.to_symbol
393 _symbol = s
394 end
395 return s
396 end
397 end
398
399 redef class PClassdef
400 # Associated class (MM entity)
401 meth local_class: MMSrcLocalClass is abstract
402 end
403
404 redef class AAttrPropdef
405 # Associated attribute (MM entity)
406 meth prop: MMSrcAttribute is abstract
407
408 # Associated read accessor (MM entity)
409 meth readmethod: MMSrcMethod is abstract
410
411 # Associated write accessor (MM entity)
412 meth writemethod: MMSrcMethod is abstract
413 end
414
415 redef class AMethPropdef
416 # Associated method (MM entity)
417 meth method: MMMethSrcMethod is abstract
418 end
419
420 redef class ATypePropdef
421 # Associated formal type (MM entity)
422 meth prop: MMSrcTypeProperty is abstract
423 end
424
425 redef class PParam
426 # Position in the signature
427 meth position: Int is abstract
428
429 # Associated local variable
430 meth variable: ParamVariable is abstract
431 end
432
433 redef class PType
434 # Retrieve the local class corresponding to the type.
435 # Display an error and return null if there is no class
436 # Display an error and return null if the type is not class based (formal one)
437 meth get_local_class(v: AbsSyntaxVisitor): MMLocalClass is abstract
438
439 # Retrieve corresponding static type.
440 # Display an error and return null if there is a problem
441 meth get_stype(v: AbsSyntaxVisitor): MMType is abstract
442
443 # Retrieve corresponding static type.
444 # Display an error and return null if there is a problem
445 # But do not performs any subtype check.
446 # get_unchecked_stype should be called to check that the static type is fully valid
447 meth get_unchecked_stype(v: AbsSyntaxVisitor): MMType is abstract
448
449 # Check that a static definition type is conform with regard to formal types
450 # Useful with get_unchecked_stype
451 # Remember that conformance check need that ancestors are totaly computed
452 meth check_conform(v: AbsSyntaxVisitor) is abstract
453 end
454
455 redef class AType
456 attr _stype_cache: MMType
457 attr _stype_cached: Bool
458
459 redef meth get_local_class(v)
460 do
461 var name = n_id.to_symbol
462 var mod = v.module
463 var cla = v.local_class
464
465 if (cla.formal_dict != null and cla.formal_dict.has_key(name)) or (cla.global_properties != null and cla.has_global_property_by_name(name)) then
466 v.error(n_id, "Type error: {name} is a formal type")
467 _stype_cached = true
468 return null
469 end
470
471 if not mod.has_global_class_named(name) then
472 v.error(n_id, "Type error: class {name} not found in module {mod}.")
473 _stype_cached = true
474 return null
475 end
476
477 var local_class = mod.class_by_name(name)
478 local_class.global.check_visibility(v, self, mod)
479 return local_class
480 end
481
482 redef meth get_unchecked_stype(v)
483 do
484 if _stype_cached then return _stype_cache
485 _stype_cached = true
486
487 var name = n_id.to_symbol
488 var mod = v.module
489 var cla = v.local_class
490
491 if cla.formal_dict.has_key(name) then
492 if n_types.length > 0 then
493 v.error(self, "Type error: formal type {name} cannot have formal parameters.")
494 return null
495 end
496 var formal = cla.formal_dict[name]
497 _stype_cache = formal
498 return formal
499 end
500
501 if cla.global_properties != null and cla.has_global_property_by_name(name) then
502 if n_types.length > 0 then
503 v.error(self, "Type error: formal type {name} cannot have formal parameters.")
504 return null
505 end
506 var t = cla.get_type.local_class.select_virtual_type(name).stype_for(cla.get_type)
507 if t == null then
508 v.error(self, "Type error: circular definition in formal type {name}.")
509 return null
510 end
511 _stype_cache = t
512 return t
513 end
514
515 var local_class = get_local_class(v)
516 if local_class == null then return null
517
518 var arity = n_types.length
519 if local_class.arity != arity then
520 v.error(self, "Type error: '{local_class}' has {local_class.arity} parameters which differs from the {arity} params.")
521 return null
522 end
523
524 if arity > 0 then
525 var tab = new Array[MMType]
526 for p in n_types do
527 tab.add(p.get_unchecked_stype(v))
528 end
529 var t = local_class.get_instantiate_type(tab)
530 _stype_cache = t
531 return t
532 else
533 var t = local_class.get_type
534 _stype_cache = t
535 return t
536 end
537 end
538
539 redef meth get_stype(v)
540 do
541 var t = get_unchecked_stype(v)
542 if t != null then check_conform(v)
543 return t
544 end
545
546 redef meth check_conform(v)
547 do
548 var st = get_unchecked_stype(v)
549 if st == null then return
550 var local_class = st.local_class
551 var arity = n_types.length
552 if arity > 0 then
553 for i in [0..arity[ do
554 var p = n_types[i]
555 var pt = p.get_stype(v)
556 var bt = local_class.get_formal(i).bound
557 if bt == null then return
558 bt = bt.adapt_to(st) # We need to abapt because of F-genericity
559 v.check_conform(p, pt, bt)
560 end
561 end
562 end
563 end
564
565 redef class PExpr
566 # Static type
567 # Is null for statement and for erronus expression
568 meth stype: MMType is abstract
569 end
570
571 redef class AVardeclExpr
572 # Assiociated local variable
573 readable writable attr _variable: VarVariable
574 end
575
576 redef class AForVardeclExpr
577 # Associated automatic local variable
578 readable writable attr _variable: AutoVariable
579 end
580
581 redef class AVarFormExpr
582 # Associated local variable
583 readable writable attr _variable: Variable
584 end
585