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 # The related AST node
27 readable attr _node
: AModule
29 # Concrete NIT source local classs by name
30 readable attr _src_local_classes
: Map[Symbol, MMSrcLocalClass]
32 init(c
: MMContext, source
: AModule, dir
: MMDirectory, name
: Symbol, filename
: String)
34 super(name
, dir
, c
, filename
)
36 _src_local_classes
= new HashMap[Symbol, MMSrcLocalClass]
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
44 assert pm
isa MMSrcModule
45 var vpm
= cm
.visibility_for
(pm
)
49 v
.error
(n
, "Visibility error: Class {self} comes from the hidden module {cm}.") # TODO: should not occur
51 else if visibility_level
>= 3 then
52 v
.error
(n
, "Visibility error: Class {self} is private.")
59 # Concrete NIT source local classes
61 special MMConcreteClass
62 # The related AST nodes
63 readable attr _nodes
: Array[PClassdef]
65 # Concrete NIT source generic formal parameter by name
66 readable attr _formal_dict
: Map[Symbol, MMTypeFormalParameter] = new HashMap[Symbol, MMTypeFormalParameter]
68 # Concrete NIT source properties by name
69 readable attr _src_local_properties
: Map[Symbol, MMLocalProperty]
71 init(mod
: MMSrcModule, n
: Symbol, cla
: PClassdef, a
: Int)
75 _src_local_properties
= new HashMap[Symbol, MMLocalProperty]
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
)
88 # TODO: should not occurs
89 v
.error
(n
, "Visibility error: Property {self} comes from the hidden module {cm}.")
91 else if visibility_level
>= 3 then
92 v
.error
(n
, "Visibility error: Property {self} is private.")
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.")
102 redef class MMLocalProperty
103 # The attached node (if any)
104 meth node
: nullable PNode do return null
106 # Is the concrete method defined as init
107 meth is_init
: Bool do return false
110 # Concrete NIT source attribute
113 redef readable attr _node
: AAttrPropdef
114 init(name
: Symbol, cla
: MMLocalClass, n
: AAttrPropdef)
121 # Concrete NIT source method
126 # Concrete NIT source method for an automatic accesor
127 class MMAttrImplementationMethod
129 redef readable attr _node
: AAttrPropdef
130 init(name
: Symbol, cla
: MMLocalClass, n
: AAttrPropdef)
137 # Concrete NIT source method for an automatic read accesor
138 class MMReadImplementationMethod
139 special MMAttrImplementationMethod
140 init(name
: Symbol, cla
: MMLocalClass, n
: AAttrPropdef)
146 # Concrete NIT source method for an automatic write accesor
147 class MMWriteImplementationMethod
148 special MMAttrImplementationMethod
149 init(name
: Symbol, cla
: MMLocalClass, n
: AAttrPropdef)
155 # Concrete NIT source method for an explicit method
156 class MMMethSrcMethod
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)
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)
179 # Concrete NIT implicit constructor
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])
187 super(once
"init".to_symbol
, cla
, null)
188 _unassigned_attributes
= unassigned_attributes
189 _super_inits
= super_inits
194 abstract class Variable
195 # Name of the variable
196 readable attr _name
: Symbol
198 # Declaration AST node
199 readable attr _decl
: nullable PNode
202 readable writable attr _stype
: nullable MMType
204 redef meth to_s
do return _name
.to_s
206 meth kind
: String is abstract
208 init(n
: Symbol, d
: nullable PNode)
215 # Variable declared with 'var'
218 redef meth kind
do return once
"variable"
219 init(n
: Symbol, d
: PNode) do super
222 # Parameter of method (declared in signature)
225 redef meth kind
do return once
"parameter"
226 init(n
: Symbol, d
: nullable PNode) do super
229 # Automatic variable (like in the 'for' statement)
232 redef meth kind
do return once
"automatic variable"
233 init(n
: Symbol, d
: PNode) do super
236 # False variable corresponding to closures declared in signatures
237 # Lives in the same namespace than variables
238 class ClosureVariable
240 redef meth kind
do return once
"closure"
242 # The signature of the closure
243 readable attr _closure
: MMClosure
245 init(n
: Symbol, d
: PNode, c
: MMClosure)
252 ###############################################################################
254 # Visitor used during the syntax analysis
255 class AbsSyntaxVisitor
257 # The root type Object
258 meth type_object
: MMType
260 return _module
.class_by_name
(once
("Object".to_symbol
)).get_type
263 # The primitive type Bool
264 meth type_bool
: MMType
266 return _module
.class_by_name
(once
("Bool".to_symbol
)).get_type
269 # The primitive type Int
270 meth type_int
: MMType
272 return _module
.class_by_name
(once
("Int".to_symbol
)).get_type
275 # The primitive type Float
276 meth type_float
: MMType
278 return _module
.class_by_name
(once
("Float".to_symbol
)).get_type
281 # The primitive type Char
282 meth type_char
: MMType
284 return _module
.class_by_name
(once
("Char".to_symbol
)).get_type
287 # The primitive type String
288 meth type_string
: MMType
290 return _module
.class_by_name
(once
("String".to_symbol
)).get_type
293 # The primitive type Collection[nullable Object]
294 meth type_collection
: MMType
296 return _module
.class_by_name
(once
("Collection".to_symbol
)).get_instantiate_type
([type_object
.as_nullable
])
299 # The primitive type Array[?]
300 meth type_array
(stype
: MMType): MMType
302 return _module
.class_by_name
(once
("Array".to_symbol
)).get_instantiate_type
([stype
])
305 # The primitive type Discrete
306 meth type_discrete
: MMType
308 return _module
.class_by_name
(once
("Discrete".to_symbol
)).get_type
311 # The primitive type Range[?]
312 meth type_range
(stype
: MMType): MMType
314 return _module
.class_by_name
(once
("Range".to_symbol
)).get_instantiate_type
([stype
])
317 # The primitive type of null
318 meth type_none
: MMType
320 return _module
.type_none
324 readable attr _module
: MMSrcModule
327 meth local_class
: MMSrcLocalClass do return _local_class
.as(not null)
328 writable attr _local_class
: nullable MMSrcLocalClass
330 # The current property
331 meth local_property
: MMLocalProperty do return _local_property
.as(not null)
332 writable attr _local_property
: nullable MMLocalProperty
334 # The current tool configuration/status
335 readable attr _tc
: ToolContext
337 # Display an error for a given syntax node
338 meth error
(n
: nullable PNode, s
: String)
340 _tc
.error
("{locate(n)}: {s}")
343 # Display a warning for a given syntax node
344 meth warning
(n
: nullable PNode, s
: String)
346 _tc
.warning
("{locate(n)}: {s}")
350 meth locate
(n
: nullable PNode): String
352 if n
!= null then return n
.locate
353 return _module
.filename
356 # Check conformity and display error
357 meth check_conform
(n
: PNode, subtype
: nullable MMType, stype
: nullable MMType): Bool
359 if stype
== null or subtype
== null then
362 if subtype
< stype
then
365 # Do not enforce nullable subtype rules yet
366 if subtype
isa MMTypeNone or subtype
.as_notnull
< stype
.as_notnull
then
367 warning
(n
, "Nullable type warning: expected {stype}, got {subtype}")
370 error
(n
, "Type error: expected {stype}, got {subtype}")
374 # Check that an expression has a static type and that
375 # Display an error and return false if n is a statement
376 # Require that the static type of n is known
377 meth check_expr
(n
: PExpr): Bool
379 if not n
.is_typed
then
380 if tc
.error_count
== 0 then
381 print
("{n.locate} not typed but not error")
384 # An error occured in a sub node,
385 # sillently cascade fail
387 else if n
.is_statement
then
388 error
(n
, "Type error: expected expression.")
394 # Combine check_conform and check_expr
395 meth check_conform_expr
(n
: PExpr, stype
: nullable MMType): Bool
397 if stype
== null then return false
398 if check_expr
(n
) then return check_conform
(n
, n
.stype
, stype
) else return false
401 # Check conformance between multiple expressions and a static type
402 # Conformance is granted if among them there is a most general type
403 # Return the most general type if a conformance is found
404 # Display an error and return null if no conformance is found
405 # The only allowed combinaison is with the nullable marker
406 # @param stype is a possible additional type (without node)
408 # Int, Int, Object => return Object
409 # Int, Float => display error, return null
410 # nullable Int, Object => return nullable Object
411 meth check_conform_multiexpr
(stype
: nullable MMType, nodes
: Collection[PExpr]): nullable MMType
413 var node
: nullable PExpr = null # candidate node
415 if not check_expr
(n
) then return null
417 if stype
!= null and stype
.is_nullable
!= ntype
.is_nullable
then
418 # nullable combinaison: if one of them is nulable, considers that both are
419 stype
= stype
.as_nullable
420 ntype
= ntype
.as_nullable
422 if stype
== null or stype
< ntype
then
428 if not n
.stype
< stype
.as(not null) then
430 error
(n
, "Type error: no most general type. Got {n.stype} and {stype}.")
432 error
(n
, "Type error: no most general type. Got {n.stype} and {stype} at {node.locate}.")
440 protected init(tc
: ToolContext, module: MMSrcModule)
447 ###############################################################################
450 protected meth accept_abs_syntax_visitor
(v
: AbsSyntaxVisitor) do visit_all
(v
)
454 attr _symbol_cache
: nullable Symbol
456 # Symbol associated with the text
458 meth to_symbol
: Symbol
460 var s
= _symbol_cache
469 redef class PClassdef
470 # Associated class (MM entity)
471 meth local_class
: MMSrcLocalClass is abstract
474 redef class AAttrPropdef
475 # Associated attribute (MM entity)
476 meth prop
: MMSrcAttribute is abstract
478 # Associated read accessor (MM entity)
479 meth readmethod
: nullable MMSrcMethod is abstract
481 # Associated write accessor (MM entity)
482 meth writemethod
: nullable MMSrcMethod is abstract
485 redef class AMethPropdef
486 # Associated method (MM entity)
487 meth method
: MMMethSrcMethod is abstract
489 # Associated 'self' variable
490 meth self_var
: ParamVariable is abstract
493 redef class ATypePropdef
494 # Associated formal type (MM entity)
495 meth prop
: MMSrcTypeProperty is abstract
499 # Position in the signature
500 meth position
: Int is abstract
502 # Associated local variable
503 meth variable
: ParamVariable is abstract
506 redef class PClosureDecl
507 # Associated closure variable
508 meth variable
: ClosureVariable is abstract
512 # Retrieve the local class corresponding to the type.
513 # Display an error and return null if there is no class
514 # Display an error and return null if the type is not class based (formal one)
515 meth get_local_class
(v
: AbsSyntaxVisitor): nullable MMLocalClass is abstract
517 # Retrieve corresponding static type.
518 # Display an error and return null if there is a problem
519 meth get_stype
(v
: AbsSyntaxVisitor): nullable MMType is abstract
521 # Retrieve corresponding static type.
522 # Display an error and return null if there is a problem
523 # But do not performs any subtype check.
524 # get_unchecked_stype should be called to check that the static type is fully valid
525 meth get_unchecked_stype
(v
: AbsSyntaxVisitor): nullable MMType is abstract
527 # Check that a static definition type is conform with regard to formal types
528 # Useful with get_unchecked_stype
529 # Remember that conformance check need that ancestors are totaly computed
530 meth check_conform
(v
: AbsSyntaxVisitor) is abstract
534 attr _stype_cache
: nullable MMType = null
535 attr _stype_cached
: Bool = false
537 redef meth get_local_class
(v
)
539 var name
= n_id
.to_symbol
541 var cla
= v
.local_class
543 if cla
.formal_dict
.has_key
(name
) or cla
.has_global_property_by_name
(name
) then
544 v
.error
(n_id
, "Type error: {name} is a formal type")
549 if not mod
.has_global_class_named
(name
) then
550 v
.error
(n_id
, "Type error: class {name} not found in module {mod}.")
555 var local_class
= mod
.class_by_name
(name
)
556 local_class
.global
.check_visibility
(v
, self, mod
)
560 redef meth get_unchecked_stype
(v
)
562 if _stype_cached
then return _stype_cache
565 var name
= n_id
.to_symbol
567 var cla
= v
.local_class
568 var t
: nullable MMType
570 if cla
.formal_dict
.has_key
(name
) then
571 if n_types
.length
> 0 then
572 v
.error
(self, "Type error: formal type {name} cannot have formal parameters.")
575 t
= cla
.formal_dict
[name
]
576 if n_kwnullable
!= null then t
= t
.as_nullable
581 if cla
.has_global_property_by_name
(name
) then
582 if n_types
.length
> 0 then
583 v
.error
(self, "Type error: formal type {name} cannot have formal parameters.")
586 t
= cla
.get_type
.local_class
.select_virtual_type
(name
).stype_for
(cla
.get_type
)
588 v
.error
(self, "Type error: circular definition in formal type {name}.")
591 if n_kwnullable
!= null then t
= t
.as_nullable
596 var local_class
= get_local_class
(v
)
597 if local_class
== null then return null
599 var arity
= n_types
.length
600 if local_class
.arity
!= arity
then
601 v
.error
(self, "Type error: '{local_class}' has {local_class.arity} parameters which differs from the {arity} params.")
606 var tab
= new Array[MMType]
608 var t2
= p
.get_unchecked_stype
(v
)
609 if t2
== null then return null
612 t
= local_class
.get_instantiate_type
(tab
)
614 t
= local_class
.get_type
616 if n_kwnullable
!= null then t
= t
.as_nullable
621 redef meth get_stype
(v
)
623 var t
= get_unchecked_stype
(v
)
624 if t
== null then return null
625 if not t
.is_valid
then return null
630 redef meth check_conform
(v
)
632 var st
= get_unchecked_stype
(v
)
633 if st
== null then return
634 var local_class
= st
.local_class
635 var arity
= n_types
.length
637 for i
in [0..arity
[ do
639 var pt
= p
.get_stype
(v
)
640 var b
= local_class
.get_formal
(i
)
641 if not b
.is_valid
then return
643 bt
= bt
.adapt_to
(st
) # We need to abapt because of F-genericity
644 v
.check_conform
(p
, pt
, bt
)
651 # Is the expression node correcly typed
652 # Return false if typed was not yet computed or
653 # if an error occured during the typing computation
654 meth is_typed
: Bool is abstract
656 # Is the expression node a statement? (ie has no return value)
658 meth is_statement
: Bool is abstract
660 # The static type of the expression
661 # require: is_typed and not is_statement
662 meth stype
: MMType is abstract
665 redef class AVardeclExpr
666 # Assiociated local variable
667 meth variable
: VarVariable is abstract
668 #readable writable attr _variable: nullable VarVariable
672 # Associated automatic local variable
673 meth variable
: AutoVariable is abstract
674 #readable writable attr _variable: nullable AutoVariable
677 redef class ASelfExpr
678 # Associated local variable
679 meth variable
: ParamVariable is abstract
680 #readable writable attr _variable: nullable ParamVariable
683 redef class AVarFormExpr
684 # Associated local variable
685 meth variable
: Variable is abstract
686 #readable writable attr _variable: nullable Variable
689 redef class AClosureCallExpr
690 # Associated closure variable
691 meth variable
: ClosureVariable is abstract
692 #readable writable attr _variable: nullable ClosureVariable
695 redef class PClosureDef
697 #readable writable attr _closure: nullable MMClosure
698 meth closure
: MMClosure is abstract
700 # Automatic variables
701 readable writable attr _variables
: nullable Array[AutoVariable]