1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2008 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 # Common syntax structures for syntax analysis of NIT AST.
23 # Concrete NIT source module
26 # A source module can locate AST nodes of related MM entities
27 # Once a source module AST is no more needed, _nodes is set to null
28 # See ToolContext::keep_ast property in syntax.nit for details
29 var _nodes
: nullable HashMap[Object, nullable ANode] = new HashMap[Object, nullable ANode]
32 fun clear_ast
do _nodes
= null
34 # The related AST node
35 fun node
: AModule do return nodes
(self).as(AModule)
37 # Concrete NIT source local classs by name
38 readable var _src_local_classes
: Map[Symbol, MMSrcLocalClass]
40 init(c
: MMContext, source
: AModule, dir
: MMDirectory, name
: Symbol, loc
: Location)
42 super(name
, dir
, c
, loc
)
44 _src_local_classes
= new HashMap[Symbol, MMSrcLocalClass]
47 redef fun nodes
(o
: Object): nullable ANode
49 if _nodes
!= null and _nodes
.has_key
(o
) then return _nodes
[o
] else return null
51 redef fun nodes
=(o
: Object, n
: nullable ANode)
53 assert not _nodes
.has_key
(o
)
59 # The AST node of some entity
60 private fun nodes
(o
: Object): nullable ANode do return null
61 # The AST node of some entity
62 private fun nodes
=(o
: Object, n
: nullable ANode) do abort
65 redef class MMGlobalClass
66 # Check that a module can access a class
67 fun check_visibility
(v
: AbsSyntaxVisitor, n
: ANode, cm
: MMSrcModule): Bool do
68 var pm
= intro
.mmmodule
69 assert pm
isa MMSrcModule
70 var vpm
= cm
.visibility_for
(pm
)
74 v
.error
(n
, "Visibility error: Class {self} comes from the hidden module {cm}.") # TODO: should not occur
76 else if visibility_level
>= 3 then
77 v
.error
(n
, "Visibility error: Class {self} is private.")
84 # Concrete NIT source local classes
87 # The first related AST node (if any)
88 fun node
: nullable AClassdef do return mmmodule
.nodes
(self).as(nullable AClassdef)
90 # Concrete NIT source generic formal parameter by name
91 readable var _formal_dict
: Map[Symbol, MMTypeFormalParameter] = new HashMap[Symbol, MMTypeFormalParameter]
93 # Concrete NIT source properties by name
94 readable var _src_local_properties
: Map[Symbol, MMLocalProperty]
96 init(mod
: MMSrcModule, n
: Symbol, cla
: nullable AClassdef, a
: Int)
100 _src_local_properties
= new HashMap[Symbol, MMLocalProperty]
104 redef class MMGlobalProperty
105 # Check that a module can access a property
106 fun check_visibility
(v
: AbsSyntaxVisitor, n
: ANode, cm
: MMSrcModule, allows_protected
: Bool): Bool do
107 var pm
= local_class
.mmmodule
108 assert pm
isa MMSrcModule
109 var vpm
= cm
.visibility_for
(pm
)
112 else if vpm
== 0 then
113 # TODO: should not occurs
114 v
.error
(n
, "Visibility error: Property {self} comes from the hidden module {cm}.")
116 else if visibility_level
>= 3 then
117 v
.error
(n
, "Visibility error: Property {self} is private.")
119 else if visibility_level
>= 2 and not allows_protected
then
120 v
.error
(n
, "Visibility error: Property {self} is protected and can only acceded by self.")
127 redef class MMLocalProperty
128 # The attached node (if any)
129 fun node
: nullable ANode do return null
131 # Is the concrete method defined as init
132 fun is_init
: Bool do return false
135 # Concrete NIT source attribute
138 redef fun node
: nullable AAttrPropdef do return mmmodule
.nodes
(self).as(nullable AAttrPropdef)
139 init(name
: Symbol, cla
: MMLocalClass, n
: AAttrPropdef)
142 cla
.mmmodule
.nodes
(self) = n
146 # Concrete NIT source method
149 redef fun is_intern
do return false
150 redef fun is_abstract
do return false
151 redef fun extern_name
do return null
154 # Concrete NIT source method for an automatic accesor
155 class MMAttrImplementationMethod
157 redef fun node
: nullable AAttrPropdef do return mmmodule
.nodes
(self).as(nullable AAttrPropdef)
158 init(name
: Symbol, cla
: MMLocalClass, n
: AAttrPropdef)
161 cla
.mmmodule
.nodes
(self) = n
165 # Concrete NIT source method for an automatic read accesor
166 class MMReadImplementationMethod
167 super MMAttrImplementationMethod
168 init(name
: Symbol, cla
: MMLocalClass, n
: AAttrPropdef)
174 # Concrete NIT source method for an automatic write accesor
175 class MMWriteImplementationMethod
176 super MMAttrImplementationMethod
177 init(name
: Symbol, cla
: MMLocalClass, n
: AAttrPropdef)
183 # Concrete NIT source method for an explicit method
184 class MMMethSrcMethod
186 redef readable var _is_init
: Bool
187 redef readable var _is_intern
: Bool
188 redef readable var _is_abstract
: Bool
189 redef readable writable var _extern_name
: nullable String # Will be computed during MMBuilder
190 redef readable var _explicit_imports
: Set[MMExplicitImport] = new HashSet[MMExplicitImport]
191 redef fun node
: nullable AMethPropdef do return mmmodule
.nodes
(self).as(nullable AMethPropdef)
192 init(name
: Symbol, cla
: MMLocalClass, n
: nullable AMethPropdef)
195 cla
.mmmodule
.nodes
(self) = n
196 _is_init
= node
isa AConcreteInitPropdef
197 _is_intern
= node
isa AInternMethPropdef
198 _is_abstract
= node
isa ADeferredMethPropdef
203 # Concrete NIT source virtual type
204 class MMSrcTypeProperty
205 super MMLocalProperty
207 init(name
: Symbol, cla
: MMLocalClass, n
: ATypePropdef)
213 # Concrete NIT implicit constructor
215 super MMMethSrcMethod
216 fun super_init
: nullable MMLocalProperty is abstract
217 redef fun is_init
do return true
218 readable var _unassigned_attributes
: Array[MMSrcAttribute]
219 readable var _super_inits
: Array[MMLocalProperty]
220 init(cla
: MMLocalClass, unassigned_attributes
: Array[MMSrcAttribute], super_inits
: Array[MMLocalProperty])
222 super(once
"init".to_symbol
, cla
, null)
223 _unassigned_attributes
= unassigned_attributes
224 _super_inits
= super_inits
229 abstract class Variable
230 # Name of the variable
231 readable var _name
: Symbol
233 # Declaration AST node
234 readable var _decl
: nullable ANode
237 readable writable var _stype
: nullable MMType
239 redef fun to_s
do return _name
.to_s
241 fun kind
: String is abstract
243 init(n
: Symbol, d
: nullable ANode)
250 # Variable declared with 'var'
253 redef fun kind
do return once
"variable"
254 init(n
: Symbol, d
: ANode) do super
257 # Parameter of method (declared in signature)
260 redef fun kind
do return once
"parameter"
261 init(n
: Symbol, d
: nullable ANode) do super
264 # Automatic variable (like in the 'for' statement)
267 redef fun kind
do return once
"automatic variable"
268 init(n
: Symbol, d
: ANode) do super
271 # False variable corresponding to closures declared in signatures
272 # Lives in the same namespace than variables
273 class ClosureVariable
275 redef fun kind
do return once
"closure"
277 # The signature of the closure
278 readable var _closure
: MMClosure
280 init(n
: Symbol, d
: ANode, c
: MMClosure)
287 ###############################################################################
289 # Visitor used during the syntax analysis
290 class AbsSyntaxVisitor
292 fun get_type_by_name
(clsname
: Symbol): MMType
294 if not _mmmodule
.has_global_class_named
(clsname
) then _tc
.fatal_error
(_mmmodule
.location
, "Missing necessary class: \"{clsname}\
"")
295 var cls
= _mmmodule
.class_by_name
(clsname
)
299 fun get_instantiated_type_by_name
(clsname
: Symbol, vtype
: Array[MMType]): MMType
301 if not _mmmodule
.has_global_class_named
(clsname
) then _tc
.fatal_error
(_mmmodule
.location
, "Missing necessary class: \"{clsname}\
"")
302 var cls
= _mmmodule
.class_by_name
(clsname
)
303 return cls
.get_instantiate_type
(vtype
)
306 # The root type Object
307 fun type_object
: MMType
309 return get_type_by_name
(once
("Object".to_symbol
))
312 # The primitive type Bool
313 fun type_bool
: MMType
315 return get_type_by_name
(once
("Bool".to_symbol
))
318 # The primitive type Int
321 return get_type_by_name
(once
("Int".to_symbol
))
324 # The primitive type Float
325 fun type_float
: MMType
327 return get_type_by_name
(once
("Float".to_symbol
))
330 # The primitive type Char
331 fun type_char
: MMType
333 return get_type_by_name
(once
("Char".to_symbol
))
336 # The primitive type String
337 fun type_string
: MMType
339 return get_type_by_name
(once
("String".to_symbol
))
342 # The primitive type NativeString
343 fun type_nativestring
: MMType
345 return get_type_by_name
(once
("NativeString".to_symbol
))
348 # The primitive type Array[?]
349 fun type_array
(stype
: MMType): MMType
351 return get_instantiated_type_by_name
(once
("Array".to_symbol
), [stype
])
354 # The primitive type Discrete
355 fun type_discrete
: MMType
357 return get_type_by_name
(once
("Discrete".to_symbol
))
360 # The primitive type Range[?]
361 fun type_range
(stype
: MMType): MMType
363 return get_instantiated_type_by_name
(once
("Range".to_symbol
), [stype
])
366 # The primitive type of null
367 fun type_none
: MMType
369 return _mmmodule
.type_none
372 fun get_method
(recv
: MMType, name
: Symbol): MMMethod
374 if not recv
.local_class
.has_global_property_by_name
(name
) then
375 fatal_error
(current_node
, "Fatal Error: {recv} must have a property named {name}.")
377 return recv
.local_class
.select_method
(name
)
381 readable var _mmmodule
: MMSrcModule
384 fun local_class
: MMSrcLocalClass do return _local_class
.as(not null)
385 writable var _local_class
: nullable MMSrcLocalClass
387 # The current property
388 fun local_property
: MMLocalProperty do return _local_property
.as(not null)
389 writable var _local_property
: nullable MMLocalProperty
391 # The current tool configuration/status
392 readable var _tc
: ToolContext
394 # Display an error for a given syntax node
395 fun error
(n
: nullable ANode, s
: String)
397 _tc
.error
(if n
== null then null else n
.hot_location
, s
)
400 # Add an error, show errors and quit
401 fun fatal_error
(n
: nullable ANode, s
: String)
403 _tc
.fatal_error
(if n
== null then null else n
.hot_location
, s
)
406 # Display a warning for a given syntax node
407 fun warning
(n
: nullable ANode, s
: String)
409 _tc
.warning
(if n
== null then null else n
.hot_location
, s
)
412 # Check conformity and display error
413 fun check_conform
(n
: ANode, subtype
: nullable MMType, stype
: nullable MMType): Bool
415 if stype
== null or subtype
== null then
418 if subtype
< stype
then
421 error
(n
, "Type error: expected {stype}, got {subtype}")
425 # Check that an expression has a static type and that
426 # Display an error and return false if n is a statement
427 # Require that the static type of n is known
428 fun check_expr
(n
: AExpr): Bool
430 if not n
.is_typed
then
431 if tc
.error_count
== 0 then
432 print
("{n.location} not typed but not error")
435 # An error occured in a sub node,
436 # sillently cascade fail
438 else if n
.is_statement
then
439 error
(n
, "Type error: expected expression.")
445 # Combine check_conform and check_expr
446 fun check_conform_expr
(n
: AExpr, stype
: nullable MMType): Bool
448 if stype
== null then return false
449 if check_expr
(n
) then return check_conform
(n
, n
.stype
, stype
) else return false
452 # Check conformance between multiple expressions and a static type
453 # Conformance is granted if among them there is a most general type
454 # Return the most general type if a conformance is found
455 # Display an error and return null if no conformance is found
456 # The only allowed combinaison is with the nullable marker
457 # @param stype is a possible additional type (without node)
459 # Int, Int, Object => return Object
460 # Int, Float => display error, return null
461 # nullable Int, Object => return nullable Object
462 fun check_conform_multiexpr
(stype
: nullable MMType, nodes
: Collection[AExpr]): nullable MMType
464 var node
: nullable AExpr = null # candidate node
466 if not check_expr
(n
) then return null
468 if stype
!= null and stype
.is_nullable
!= ntype
.is_nullable
then
469 # nullable combinaison: if one of them is nulable, considers that both are
470 stype
= stype
.as_nullable
471 ntype
= ntype
.as_nullable
473 if stype
== null or stype
< ntype
then
480 if not n
.stype
< stype
then
482 error
(n
, "Type error: no most general type. Got {n.stype} and {stype}.")
484 error
(n
, "Type error: no most general type. Got {n.stype} and {stype} at {node.location.relative_to(n.location)}.")
492 protected init(tc
: ToolContext, mmmodule
: MMSrcModule)
499 ###############################################################################
502 protected fun accept_abs_syntax_visitor
(v
: AbsSyntaxVisitor) do visit_all
(v
)
506 var _symbol_cache
: nullable Symbol
508 # Symbol associated with the text
510 fun to_symbol
: Symbol
512 var s
= _symbol_cache
521 redef class AClassdef
522 # Associated class (MM entity)
523 fun local_class
: MMSrcLocalClass is abstract
525 # Next AClassdef of the same class (if any)
526 readable writable var _next_node
: nullable AClassdef = null
530 # Associated 'self' variable
531 fun self_var
: ParamVariable is abstract
534 redef class AAttrPropdef
535 # Associated attribute (MM entity)
536 fun prop
: MMSrcAttribute is abstract
538 # Associated read accessor (MM entity)
539 fun readmethod
: nullable MMSrcMethod is abstract
541 # Associated write accessor (MM entity)
542 fun writemethod
: nullable MMSrcMethod is abstract
545 redef class AConcreteInitPropdef
546 readable var _super_init_calls
: Array[MMMethod] = new Array[MMMethod]
547 readable var _explicit_super_init_calls
: Array[MMMethod] = new Array[MMMethod]
550 redef class AMethPropdef
551 # Associated method (MM entity)
552 fun method
: MMMethSrcMethod is abstract
555 redef class ATypePropdef
556 # Associated formal type (MM entity)
557 fun prop
: MMSrcTypeProperty is abstract
561 # Position in the signature
562 fun position
: Int is abstract
564 # Associated local variable
565 fun variable
: ParamVariable is abstract
568 redef class AClosureDecl
569 # Position in the signature
570 fun position
: Int is abstract
572 # Associated closure variable
573 fun variable
: ClosureVariable is abstract
577 # Is the node correcly typed
578 # Return false if typed was not yet computed or
579 # if an error occured during the typing computation
580 fun is_typed
: Bool is abstract
582 # Return corresponding static type. (require is_typed)
583 fun stype
: MMType is abstract
585 var _stype_cache
: nullable MMType = null
586 var _stype_cached
: Bool = false
588 # Retrieve the local class corresponding to the type.
589 # Display an error and return null if there is no class
590 # Display an error and return null if the type is not class based (formal one)
591 fun get_local_class
(v
: AbsSyntaxVisitor): nullable MMLocalClass
593 var name
= n_id
.to_symbol
595 var cla
= v
.local_class
597 if cla
.formal_dict
.has_key
(name
) or cla
.has_global_property_by_name
(name
) then
598 v
.error
(n_id
, "Type error: {name} is a formal type")
603 if not mod
.has_global_class_named
(name
) then
604 v
.error
(n_id
, "Type error: class {name} not found in module {mod}.")
609 var local_class
= mod
.class_by_name
(name
)
610 local_class
.global
.check_visibility
(v
, self, mod
)
614 # Retrieve corresponding static type.
615 # Display an error and return null if there is a problem
616 # But do not performs any subtype check.
617 # get_unchecked_stype should be called to check that the static type is fully valid
618 fun get_unchecked_stype
(v
: AbsSyntaxVisitor): nullable MMType
620 if _stype_cached
then return _stype_cache
623 var name
= n_id
.to_symbol
625 var cla
= v
.local_class
626 var t
: nullable MMType
628 if cla
.formal_dict
.has_key
(name
) then
629 if n_types
.length
> 0 then
630 v
.error
(self, "Type error: formal type {name} cannot have formal parameters.")
633 t
= cla
.formal_dict
[name
]
634 if n_kwnullable
!= null then t
= t
.as_nullable
639 if cla
.has_global_property_by_name
(name
) then
640 if n_types
.length
> 0 then
641 v
.error
(self, "Type error: formal type {name} cannot have formal parameters.")
644 t
= cla
.get_type
.local_class
.select_virtual_type
(name
).stype_for
(cla
.get_type
)
646 v
.error
(self, "Type error: circular definition in formal type {name}.")
649 if n_kwnullable
!= null then t
= t
.as_nullable
654 var local_class
= get_local_class
(v
)
655 if local_class
== null then return null
657 var arity
= n_types
.length
658 if local_class
.arity
!= arity
then
660 v
.error
(self, "Type error: '{local_class}' is a generic class.")
661 else if local_class
.arity
== 0 then
662 v
.error
(self, "Type error: '{local_class}' is not a generic class.")
664 v
.error
(self, "Type error: '{local_class}' has {local_class.arity} parameters ({arity} are provided).")
670 var tab
= new Array[MMType]
672 var t2
= p
.get_unchecked_stype
(v
)
673 if t2
== null then return null
676 t
= local_class
.get_instantiate_type
(tab
)
678 t
= local_class
.get_type
680 if n_kwnullable
!= null then t
= t
.as_nullable
685 # Retrieve corresponding static type.
686 # Display an error and return null if there is a problem
687 fun get_stype
(v
: AbsSyntaxVisitor): nullable MMType
689 var t
= get_unchecked_stype
(v
)
690 if t
== null then return null
691 if not t
.is_valid
then return null
696 # Check that a static definition type is conform with regard to formal types
697 # Useful with get_unchecked_stype
698 # Remember that conformance check need that ancestors are totaly computed
699 fun check_conform
(v
: AbsSyntaxVisitor)
701 var st
= get_unchecked_stype
(v
)
702 if st
== null then return
703 var local_class
= st
.local_class
704 var arity
= n_types
.length
706 for i
in [0..arity
[ do
708 var pt
= p
.get_stype
(v
)
709 var b
= local_class
.get_formal
(i
)
710 if not b
.is_valid
then return
712 bt
= bt
.adapt_to
(st
) # We need to abapt because of F-genericity
713 v
.check_conform
(p
, pt
, bt
)
720 # Is the expression node correcly typed
721 # Return false if typed was not yet computed or
722 # if an error occured during the typing computation
723 fun is_typed
: Bool is abstract
725 # Is the expression node a statement? (ie has no return value)
727 fun is_statement
: Bool is abstract
729 # The static type of the expression
730 # require: is_typed and not is_statement
731 fun stype
: MMType is abstract
734 class AAbsAbsSendExpr
736 # The signature of the called property (require is_typed)
737 fun prop_signature
: MMSignature is abstract
739 # The raw arguments used (without vararg transformation) (require is_typed)
740 fun raw_arguments
: Array[AExpr] is abstract
744 super AAbsAbsSendExpr
745 # The invoked method (require is_typed)
746 fun prop
: MMMethod is abstract
748 # The return type (if any) (once computed)
749 fun return_type
: nullable MMType is abstract
756 redef class ASuperExpr
758 fun init_in_superclass
: nullable MMMethod is abstract
765 redef class ASendExpr
767 # Closure definitions
768 fun closure_defs
: nullable Array[AClosureDef] is abstract
771 redef class AReassignFormExpr
772 # Method used through the reassigment operator (require is_typed)
773 fun assign_method
: MMMethod is abstract
776 class ASendReassignExpr
778 super AReassignFormExpr
779 # The invoked method used to read (require is_typed)
780 # prop is the method used to write
781 fun read_prop
: MMMethod is abstract
784 redef class ACallReassignExpr
785 super ASendReassignExpr
788 redef class ABraReassignExpr
789 super ASendReassignExpr
792 redef class AAttrFormExpr
793 # Attribute accessed (require is_typed)
794 fun prop
: MMAttribute is abstract
796 # Attribute type of the acceded attribute (require is_typed)
797 fun attr_type
: MMType is abstract
800 redef class ASuperstringExpr
801 fun atype
: MMType is abstract
804 redef class AVardeclExpr
805 # Assiociated local variable
806 fun variable
: VarVariable is abstract
807 #readable writable var _variable: nullable VarVariable
811 # Associated automatic local variable
812 fun variables
: Array[AutoVariable] is abstract
815 redef class ASelfExpr
816 # Associated local variable
817 fun variable
: ParamVariable is abstract
820 redef class AVarFormExpr
821 # Associated local variable
822 fun variable
: Variable is abstract
825 redef class AClosureCallExpr
826 super AAbsAbsSendExpr
827 # Associated closure variable
828 fun variable
: ClosureVariable is abstract
831 redef class AClosureDef
833 fun closure
: MMClosure is abstract
835 # Automatic variables
836 readable writable var _variables
: nullable Array[AutoVariable]
841 fun name
: nullable Symbol is abstract