nullable: enforce static and dynamic rules.
[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, filename: String)
33 do
34 super(name, dir, c, filename)
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 attr _formal_dict: Map[Symbol, MMTypeFormalParameter] = new HashMap[Symbol, MMTypeFormalParameter]
67
68 # Concrete NIT source properties by name
69 readable attr _src_local_properties: Map[Symbol, MMLocalProperty]
70
71 init(mod: MMSrcModule, n: Symbol, cla: PClassdef, a: Int)
72 do
73 super(mod, 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: nullable 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: nullable AMethPropdef
160 init(name: Symbol, cla: MMLocalClass, n: nullable 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: nullable PNode
200
201 # Static type
202 readable writable attr _stype: nullable 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: nullable PNode)
209 do
210 _name = n
211 _decl = d
212 end
213 end
214
215 # Variable declared with 'var'
216 class VarVariable
217 special Variable
218 redef meth kind do return once "variable"
219 init(n: Symbol, d: PNode) do super
220 end
221
222 # Parameter of method (declared in signature)
223 class ParamVariable
224 special Variable
225 redef meth kind do return once "parameter"
226 init(n: Symbol, d: nullable PNode) do super
227 end
228
229 # Automatic variable (like in the 'for' statement)
230 class AutoVariable
231 special Variable
232 redef meth kind do return once "automatic variable"
233 init(n: Symbol, d: PNode) do super
234 end
235
236 # False variable corresponding to closures declared in signatures
237 # Lives in the same namespace than variables
238 class ClosureVariable
239 special Variable
240 redef meth kind do return once "closure"
241
242 # The signature of the closure
243 readable attr _closure: MMClosure
244
245 init(n: Symbol, d: PNode, c: MMClosure)
246 do
247 super(n, d)
248 _closure = c
249 end
250 end
251
252 ###############################################################################
253
254 # Visitor used during the syntax analysis
255 class AbsSyntaxVisitor
256 special Visitor
257 # The root type Object
258 meth type_object: MMType
259 do
260 return _module.class_by_name(once ("Object".to_symbol)).get_type
261 end
262
263 # The primitive type Bool
264 meth type_bool: MMType
265 do
266 return _module.class_by_name(once ("Bool".to_symbol)).get_type
267 end
268
269 # The primitive type Int
270 meth type_int: MMType
271 do
272 return _module.class_by_name(once ("Int".to_symbol)).get_type
273 end
274
275 # The primitive type Float
276 meth type_float: MMType
277 do
278 return _module.class_by_name(once ("Float".to_symbol)).get_type
279 end
280
281 # The primitive type Char
282 meth type_char: MMType
283 do
284 return _module.class_by_name(once ("Char".to_symbol)).get_type
285 end
286
287 # The primitive type String
288 meth type_string: MMType
289 do
290 return _module.class_by_name(once ("String".to_symbol)).get_type
291 end
292
293 # The primitive type Collection[nullable Object]
294 meth type_collection: MMType
295 do
296 return _module.class_by_name(once ("Collection".to_symbol)).get_instantiate_type([type_object.as_nullable])
297 end
298
299 # The primitive type Array[?]
300 meth type_array(stype: MMType): MMType
301 do
302 return _module.class_by_name(once ("Array".to_symbol)).get_instantiate_type([stype])
303 end
304
305 # The primitive type Discrete
306 meth type_discrete: MMType
307 do
308 return _module.class_by_name(once ("Discrete".to_symbol)).get_type
309 end
310
311 # The primitive type Range[?]
312 meth type_range(stype: MMType): MMType
313 do
314 return _module.class_by_name(once ("Range".to_symbol)).get_instantiate_type([stype])
315 end
316
317 # The primitive type of null
318 meth type_none: MMType
319 do
320 return _module.type_none
321 end
322
323 # The current module
324 readable attr _module: MMSrcModule
325
326 # The current class
327 meth local_class: MMSrcLocalClass do return _local_class.as(not null)
328 writable attr _local_class: nullable MMSrcLocalClass
329
330 # The current property
331 meth local_property: MMLocalProperty do return _local_property.as(not null)
332 writable attr _local_property: nullable MMLocalProperty
333
334 # The current tool configuration/status
335 readable attr _tc: ToolContext
336
337 # Display an error for a given syntax node
338 meth error(n: nullable PNode, s: String)
339 do
340 _tc.error("{locate(n)}: {s}")
341 end
342
343 # Display a warning for a given syntax node
344 meth warning(n: nullable PNode, s: String)
345 do
346 _tc.warning("{locate(n)}: {s}")
347 end
348
349 #
350 meth locate(n: nullable PNode): String
351 do
352 if n != null then return n.locate
353 return _module.filename
354 end
355
356 # Check conformity and display error
357 meth check_conform(n: PNode, subtype: nullable MMType, stype: nullable MMType): Bool
358 do
359 if stype == null or subtype == null then
360 return false
361 end
362 if subtype < stype then
363 return true
364 end
365 error(n, "Type error: expected {stype}, got {subtype}")
366 return false
367 end
368
369 # Check that an expression has a static type and that
370 # Display an error and return false if n is a statement
371 # Require that the static type of n is known
372 meth check_expr(n: PExpr): Bool
373 do
374 if not n.is_typed then
375 if tc.error_count == 0 then
376 print("{n.locate} not typed but not error")
377 abort
378 end
379 # An error occured in a sub node,
380 # sillently cascade fail
381 return false
382 else if n.is_statement then
383 error(n, "Type error: expected expression.")
384 return false
385 end
386 return true
387 end
388
389 # Combine check_conform and check_expr
390 meth check_conform_expr(n: PExpr, stype: nullable MMType): Bool
391 do
392 if stype == null then return false
393 if check_expr(n) then return check_conform(n, n.stype, stype) else return false
394 end
395
396 # Check conformance between multiple expressions and a static type
397 # Conformance is granted if among them there is a most general type
398 # Return the most general type if a conformance is found
399 # Display an error and return null if no conformance is found
400 # The only allowed combinaison is with the nullable marker
401 # @param stype is a possible additional type (without node)
402 # Examples:
403 # Int, Int, Object => return Object
404 # Int, Float => display error, return null
405 # nullable Int, Object => return nullable Object
406 meth check_conform_multiexpr(stype: nullable MMType, nodes: Collection[PExpr]): nullable MMType
407 do
408 var node: nullable PExpr = null # candidate node
409 for n in nodes do
410 if not check_expr(n) then return null
411 var ntype = n.stype
412 if stype != null and stype.is_nullable != ntype.is_nullable then
413 # nullable combinaison: if one of them is nulable, considers that both are
414 stype = stype.as_nullable
415 ntype = ntype.as_nullable
416 end
417 if stype == null or stype < ntype then
418 stype = ntype
419 node = n
420 end
421 end
422 for n in nodes do
423 if not n.stype < stype.as(not null) then
424 if node == null then
425 error(n, "Type error: no most general type. Got {n.stype} and {stype}.")
426 else
427 error(n, "Type error: no most general type. Got {n.stype} and {stype} at {node.locate}.")
428 end
429 return null
430 end
431 end
432 return stype
433 end
434
435 protected init(tc: ToolContext, module: MMSrcModule)
436 do
437 _tc = tc
438 _module = module
439 end
440 end
441
442 ###############################################################################
443
444 redef class PNode
445 protected meth accept_abs_syntax_visitor(v: AbsSyntaxVisitor) do visit_all(v)
446 end
447
448 redef class Token
449 attr _symbol_cache: nullable Symbol
450
451 # Symbol associated with the text
452 # Lazily computed
453 meth to_symbol: Symbol
454 do
455 var s = _symbol_cache
456 if s == null then
457 s = text.to_symbol
458 _symbol_cache = s
459 end
460 return s
461 end
462 end
463
464 redef class PClassdef
465 # Associated class (MM entity)
466 meth local_class: MMSrcLocalClass is abstract
467 end
468
469 redef class AAttrPropdef
470 # Associated attribute (MM entity)
471 meth prop: MMSrcAttribute is abstract
472
473 # Associated read accessor (MM entity)
474 meth readmethod: nullable MMSrcMethod is abstract
475
476 # Associated write accessor (MM entity)
477 meth writemethod: nullable MMSrcMethod is abstract
478 end
479
480 redef class AMethPropdef
481 # Associated method (MM entity)
482 meth method: MMMethSrcMethod is abstract
483
484 # Associated 'self' variable
485 meth self_var: ParamVariable is abstract
486 end
487
488 redef class ATypePropdef
489 # Associated formal type (MM entity)
490 meth prop: MMSrcTypeProperty is abstract
491 end
492
493 redef class PParam
494 # Position in the signature
495 meth position: Int is abstract
496
497 # Associated local variable
498 meth variable: ParamVariable is abstract
499 end
500
501 redef class PClosureDecl
502 # Associated closure variable
503 meth variable: ClosureVariable is abstract
504 end
505
506 redef class PType
507 # Retrieve the local class corresponding to the type.
508 # Display an error and return null if there is no class
509 # Display an error and return null if the type is not class based (formal one)
510 meth get_local_class(v: AbsSyntaxVisitor): nullable MMLocalClass is abstract
511
512 # Retrieve corresponding static type.
513 # Display an error and return null if there is a problem
514 meth get_stype(v: AbsSyntaxVisitor): nullable MMType is abstract
515
516 # Retrieve corresponding static type.
517 # Display an error and return null if there is a problem
518 # But do not performs any subtype check.
519 # get_unchecked_stype should be called to check that the static type is fully valid
520 meth get_unchecked_stype(v: AbsSyntaxVisitor): nullable MMType is abstract
521
522 # Check that a static definition type is conform with regard to formal types
523 # Useful with get_unchecked_stype
524 # Remember that conformance check need that ancestors are totaly computed
525 meth check_conform(v: AbsSyntaxVisitor) is abstract
526 end
527
528 redef class AType
529 attr _stype_cache: nullable MMType = null
530 attr _stype_cached: Bool = false
531
532 redef meth get_local_class(v)
533 do
534 var name = n_id.to_symbol
535 var mod = v.module
536 var cla = v.local_class
537
538 if cla.formal_dict.has_key(name) or cla.has_global_property_by_name(name) then
539 v.error(n_id, "Type error: {name} is a formal type")
540 _stype_cached = true
541 return null
542 end
543
544 if not mod.has_global_class_named(name) then
545 v.error(n_id, "Type error: class {name} not found in module {mod}.")
546 _stype_cached = true
547 return null
548 end
549
550 var local_class = mod.class_by_name(name)
551 local_class.global.check_visibility(v, self, mod)
552 return local_class
553 end
554
555 redef meth get_unchecked_stype(v)
556 do
557 if _stype_cached then return _stype_cache
558 _stype_cached = true
559
560 var name = n_id.to_symbol
561 var mod = v.module
562 var cla = v.local_class
563 var t: nullable MMType
564
565 if cla.formal_dict.has_key(name) then
566 if n_types.length > 0 then
567 v.error(self, "Type error: formal type {name} cannot have formal parameters.")
568 return null
569 end
570 t = cla.formal_dict[name]
571 if n_kwnullable != null then t = t.as_nullable
572 _stype_cache = t
573 return t
574 end
575
576 if cla.has_global_property_by_name(name) then
577 if n_types.length > 0 then
578 v.error(self, "Type error: formal type {name} cannot have formal parameters.")
579 return null
580 end
581 t = cla.get_type.local_class.select_virtual_type(name).stype_for(cla.get_type)
582 if t == null then
583 v.error(self, "Type error: circular definition in formal type {name}.")
584 return null
585 end
586 if n_kwnullable != null then t = t.as_nullable
587 _stype_cache = t
588 return t
589 end
590
591 var local_class = get_local_class(v)
592 if local_class == null then return null
593
594 var arity = n_types.length
595 if local_class.arity != arity then
596 v.error(self, "Type error: '{local_class}' has {local_class.arity} parameters which differs from the {arity} params.")
597 return null
598 end
599
600 if arity > 0 then
601 var tab = new Array[MMType]
602 for p in n_types do
603 var t2 = p.get_unchecked_stype(v)
604 if t2 == null then return null
605 tab.add(t2)
606 end
607 t = local_class.get_instantiate_type(tab)
608 else
609 t = local_class.get_type
610 end
611 if n_kwnullable != null then t = t.as_nullable
612 _stype_cache = t
613 return t
614 end
615
616 redef meth get_stype(v)
617 do
618 var t = get_unchecked_stype(v)
619 if t == null then return null
620 if not t.is_valid then return null
621 check_conform(v)
622 return t
623 end
624
625 redef meth check_conform(v)
626 do
627 var st = get_unchecked_stype(v)
628 if st == null then return
629 var local_class = st.local_class
630 var arity = n_types.length
631 if arity > 0 then
632 for i in [0..arity[ do
633 var p = n_types[i]
634 var pt = p.get_stype(v)
635 var b = local_class.get_formal(i)
636 if not b.is_valid then return
637 var bt = b.bound
638 bt = bt.adapt_to(st) # We need to abapt because of F-genericity
639 v.check_conform(p, pt, bt)
640 end
641 end
642 end
643 end
644
645 redef class PExpr
646 # Is the expression node correcly typed
647 # Return false if typed was not yet computed or
648 # if an error occured during the typing computation
649 meth is_typed: Bool is abstract
650
651 # Is the expression node a statement? (ie has no return value)
652 # require: is_typed
653 meth is_statement: Bool is abstract
654
655 # The static type of the expression
656 # require: is_typed and not is_statement
657 meth stype: MMType is abstract
658 end
659
660 redef class AVardeclExpr
661 # Assiociated local variable
662 meth variable: VarVariable is abstract
663 #readable writable attr _variable: nullable VarVariable
664 end
665
666 redef class AForExpr
667 # Associated automatic local variable
668 meth variable: AutoVariable is abstract
669 #readable writable attr _variable: nullable AutoVariable
670 end
671
672 redef class ASelfExpr
673 # Associated local variable
674 meth variable: ParamVariable is abstract
675 #readable writable attr _variable: nullable ParamVariable
676 end
677
678 redef class AVarFormExpr
679 # Associated local variable
680 meth variable: Variable is abstract
681 #readable writable attr _variable: nullable Variable
682 end
683
684 redef class AClosureCallExpr
685 # Associated closure variable
686 meth variable: ClosureVariable is abstract
687 #readable writable attr _variable: nullable ClosureVariable
688 end
689
690 redef class PClosureDef
691 # Associated closure
692 #readable writable attr _closure: nullable MMClosure
693 meth closure: MMClosure is abstract
694
695 # Automatic variables
696 readable writable attr _variables: nullable Array[AutoVariable]
697 end