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)
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 writable attr _formal_dict
: Map[Symbol, MMTypeFormalParameter]
68 # Concrete NIT source properties by name
69 readable attr _src_local_properties
: Map[Symbol, MMLocalProperty]
71 init(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
: 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
: AMethPropdef
160 init(name
: Symbol, cla
: MMLocalClass, n
: 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
: PNode
202 readable writable attr _stype
: MMType
204 redef meth to_s
do return _name
.to_s
206 meth kind
: String is abstract
208 init(n
: Symbol, d
: PNode)
217 # Variable declared with 'var'
220 redef meth kind
do return once
"variable"
221 init(n
: Symbol, d
: PNode) do super
224 # Parameter of method (declared in signature)
227 redef meth kind
do return once
"parameter"
228 init(n
: Symbol, d
: PNode) do super
231 # Automatic variable (like in the 'for' statement)
234 redef meth kind
do return once
"automatic variable"
235 init(n
: Symbol, d
: PNode) do super
238 ###############################################################################
240 # Visitor used during the syntax analysis
241 class AbsSyntaxVisitor
243 # The primitive type Bool
244 meth type_bool
: MMType
246 return _module
.class_by_name
(once
("Bool".to_symbol
)).get_type
249 # The primitive type Int
250 meth type_int
: MMType
252 return _module
.class_by_name
(once
("Int".to_symbol
)).get_type
255 # The primitive type Float
256 meth type_float
: MMType
258 return _module
.class_by_name
(once
("Float".to_symbol
)).get_type
261 # The primitive type Char
262 meth type_char
: MMType
264 return _module
.class_by_name
(once
("Char".to_symbol
)).get_type
267 # The primitive type String
268 meth type_string
: MMType
270 return _module
.class_by_name
(once
("String".to_symbol
)).get_type
273 # The primitive type Collection[Object]
274 meth type_collection
: MMType
276 return _module
.class_by_name
(once
("Collection".to_symbol
)).get_type
279 # The primitive type Array[?]
280 meth type_array
(stype
: MMType): MMType
282 return _module
.class_by_name
(once
("Array".to_symbol
)).get_instantiate_type
([stype
])
285 # The primitive type Discrete
286 meth type_discrete
: MMType
288 return _module
.class_by_name
(once
("Discrete".to_symbol
)).get_type
291 # The primitive type Range[?]
292 meth type_range
(stype
: MMType): MMType
294 return _module
.class_by_name
(once
("Range".to_symbol
)).get_instantiate_type
([stype
])
297 # The primitive type of null
298 meth type_none
: MMType
300 return _module
.type_none
304 readable writable attr _module
: MMSrcModule
307 readable writable attr _local_class
: MMSrcLocalClass
309 # The current property
310 readable writable attr _local_property
: MMLocalProperty
312 # The current tool configuration/status
313 readable attr _tc
: ToolContext
315 # Display an error for a given syntax node
316 meth error
(n
: PNode, s
: String)
318 _tc
.error
("{locate(n)}: {s}")
321 # Display a warning for a given syntax node
322 meth warning
(n
: PNode, s
: String)
324 _tc
.warning
("{locate(n)}: {s}")
328 meth locate
(n
: PNode): String
330 if n
!= null then return n
.locate
331 return _module
.filename
334 # Check conformity and display error
335 meth check_conform
(n
: PNode, subtype
: MMType, stype
: MMType): Bool
337 if stype
== null or subtype
== null then
340 if subtype
< stype
then
343 #error(n, "Type error: expected {stype}'{stype.module}, got {subtype}'{subtype.module}")
345 error
(n
, "Type error: expected {stype}, got {subtype}")
350 protected init(tc
: ToolContext, module: MMSrcModule)
357 ###############################################################################
360 protected meth accept_abs_syntax_visitor
(v
: AbsSyntaxVisitor) do visit_all
(v
)
366 # Symbol associated with the text
368 meth to_symbol
: Symbol
379 redef class PClassdef
380 # Associated class (MM entity)
381 meth local_class
: MMSrcLocalClass is abstract
384 redef class AAttrPropdef
385 # Associated attribute (MM entity)
386 meth prop
: MMSrcAttribute is abstract
388 # Associated read accessor (MM entity)
389 meth readmethod
: MMSrcMethod is abstract
391 # Associated write accessor (MM entity)
392 meth writemethod
: MMSrcMethod is abstract
395 redef class AMethPropdef
396 # Associated method (MM entity)
397 meth method
: MMMethSrcMethod is abstract
400 redef class ATypePropdef
401 # Associated formal type (MM entity)
402 meth prop
: MMSrcTypeProperty is abstract
406 # Position in the signature
407 meth position
: Int is abstract
409 # Associated local variable
410 meth variable
: ParamVariable is abstract
414 # Retrieve the local class corresponding to the type.
415 # Display an error and return null if there is no class
416 # Display an error and return null if the type is not class based (formal one)
417 meth get_local_class
(v
: AbsSyntaxVisitor): MMLocalClass is abstract
419 # Retrieve corresponding static type.
420 # Display an error and return null if there is a problem
421 meth get_stype
(v
: AbsSyntaxVisitor): MMType is abstract
423 # Retrieve corresponding static type.
424 # Display an error and return null if there is a problem
425 # But do not performs any subtype check.
426 # get_unchecked_stype should be called to check that the static type is fully valid
427 meth get_unchecked_stype
(v
: AbsSyntaxVisitor): MMType is abstract
429 # Check that a static definition type is conform with regard to formal types
430 # Useful with get_unchecked_stype
431 # Remember that conformance check need that ancestors are totaly computed
432 meth check_conform
(v
: AbsSyntaxVisitor) is abstract
436 attr _stype_cache
: MMType
437 attr _stype_cached
: Bool
439 redef meth get_local_class
(v
)
441 var name
= n_id
.to_symbol
443 var cla
= v
.local_class
445 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
446 v
.error
(n_id
, "Type error: {name} is a formal type")
451 if not mod
.has_global_class_named
(name
) then
452 v
.error
(n_id
, "Type error: class {name} not found in module {mod}.")
457 var local_class
= mod
.class_by_name
(name
)
458 local_class
.global
.check_visibility
(v
, self, mod
)
462 redef meth get_unchecked_stype
(v
)
464 if _stype_cached
then return _stype_cache
467 var name
= n_id
.to_symbol
469 var cla
= v
.local_class
471 if cla
.formal_dict
.has_key
(name
) then
472 if n_types
.length
> 0 then
473 v
.error
(self, "Type error: formal type {name} cannot have formal parameters.")
476 var formal
= cla
.formal_dict
[name
]
477 _stype_cache
= formal
481 if cla
.global_properties
!= null and cla
.has_global_property_by_name
(name
) then
482 if n_types
.length
> 0 then
483 v
.error
(self, "Type error: formal type {name} cannot have formal parameters.")
486 var t
= cla
.get_type
.local_class
.select_virtual_type
(name
).stype_for
(cla
.get_type
)
488 v
.error
(self, "Type error: circular definition in formal type {name}.")
495 var local_class
= get_local_class
(v
)
496 if local_class
== null then return null
498 var arity
= n_types
.length
499 if local_class
.arity
!= arity
then
500 v
.error
(self, "Type error: '{local_class}' has {local_class.arity} parameters which differs from the {arity} params.")
505 var tab
= new Array[MMType]
507 tab
.add
(p
.get_unchecked_stype
(v
))
509 var t
= local_class
.get_instantiate_type
(tab
)
513 var t
= local_class
.get_type
519 redef meth get_stype
(v
)
521 var t
= get_unchecked_stype
(v
)
522 if t
!= null then check_conform
(v
)
526 redef meth check_conform
(v
)
528 var st
= get_unchecked_stype
(v
)
529 if st
== null then return
530 var local_class
= st
.local_class
531 var arity
= n_types
.length
533 for i
in [0..arity
[ do
535 var pt
= p
.get_stype
(v
)
536 var bt
= local_class
.get_formal
(i
).bound
537 if bt
== null then return
538 bt
= bt
.adapt_to
(st
) # We need to abapt because of F-genericity
539 v
.check_conform
(p
, pt
, bt
)
547 # Is null for statement and for erronus expression
548 meth stype
: MMType is abstract
551 redef class AVardeclExpr
552 # Assiociated local variable
553 readable writable attr _variable
: VarVariable
556 redef class AForVardeclExpr
557 # Associated automatic local variable
558 readable writable attr _variable
: AutoVariable
561 redef class AVarFormExpr
562 # Associated local variable
563 readable writable attr _variable
: Variable