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, MMConcreteProperty]
71 init(n
: Symbol, cla
: PClassdef, a
: Int)
75 _src_local_properties
= new HashMap[Symbol, MMConcreteProperty]
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 MMConcreteProperty
103 # The attached node (if any)
104 meth node
: PNode do return null
107 # Concrete NIT source attribute
109 special MMConcreteProperty
111 redef readable attr _node
: AAttrPropdef
112 init(name
: Symbol, cla
: MMLocalClass, n
: AAttrPropdef)
114 super(name
, cla
, self)
119 # Concrete NIT source method
121 special MMConcreteProperty
125 # Concrete NIT source method for an automatic accesor
126 class MMAttrImplementationMethod
128 redef readable attr _node
: AAttrPropdef
131 # Concrete NIT source method for an automatic read accesor
132 class MMReadImplementationMethod
133 special MMAttrImplementationMethod
135 init(name
: Symbol, cla
: MMLocalClass, n
: AAttrPropdef)
137 super(name
, cla
, self)
142 # Concrete NIT source method for an automatic write accesor
143 class MMWriteImplementationMethod
144 special MMAttrImplementationMethod
146 init(name
: Symbol, cla
: MMLocalClass, n
: AAttrPropdef)
148 super(name
, cla
, self)
153 # Concrete NIT source method for an explicit method
154 class MMMethSrcMethod
156 redef readable attr _node
: AMethPropdef
157 init(name
: Symbol, cla
: MMLocalClass, n
: AMethPropdef)
159 super(name
, cla
, self)
164 # Concrete NIT source virtual type
165 class MMSrcTypeProperty
166 special MMConcreteProperty
167 special MMTypeProperty
168 redef readable attr _node
: ATypePropdef
169 init(name
: Symbol, cla
: MMLocalClass, n
: ATypePropdef)
171 super(name
, cla
, self)
177 # Local variable and method parameter
179 # Name of the variable
180 readable attr _name
: Symbol
182 # Declaration AST node
183 readable attr _decl
: PNode
186 readable writable attr _stype
: MMType
188 redef meth to_s
do return _name
.to_s
190 init(n
: Symbol, d
: PNode)
199 ###############################################################################
201 # Visitor used during the syntax analysis
202 class AbsSyntaxVisitor
204 # The primitive type Bool
205 meth type_bool
: MMType
207 return _module
.class_by_name
(once
("Bool".to_symbol
)).get_type
210 # The primitive type Int
211 meth type_int
: MMType
213 return _module
.class_by_name
(once
("Int".to_symbol
)).get_type
216 # The primitive type Float
217 meth type_float
: MMType
219 return _module
.class_by_name
(once
("Float".to_symbol
)).get_type
222 # The primitive type Char
223 meth type_char
: MMType
225 return _module
.class_by_name
(once
("Char".to_symbol
)).get_type
228 # The primitive type String
229 meth type_string
: MMType
231 return _module
.class_by_name
(once
("String".to_symbol
)).get_type
234 # The primitive type Collection[Object]
235 meth type_collection
: MMType
237 return _module
.class_by_name
(once
("Collection".to_symbol
)).get_type
240 # The primitive type Array[?]
241 meth type_array
(stype
: MMType): MMType
243 return _module
.class_by_name
(once
("Array".to_symbol
)).get_instantiate_type
([stype
])
246 # The primitive type Discrete
247 meth type_discrete
: MMType
249 return _module
.class_by_name
(once
("Discrete".to_symbol
)).get_type
252 # The primitive type Range[?]
253 meth type_range
(stype
: MMType): MMType
255 return _module
.class_by_name
(once
("Range".to_symbol
)).get_instantiate_type
([stype
])
258 # The primitive type of null
259 meth type_none
: MMType
261 return _module
.type_none
265 readable writable attr _module
: MMSrcModule
268 readable writable attr _local_class
: MMSrcLocalClass
270 # The current property
271 readable writable attr _local_property
: MMConcreteProperty
273 # The current tool configuration/status
274 readable attr _tc
: ToolContext
276 # Display an error for a given syntax node
277 meth error
(n
: PNode, s
: String)
279 _tc
.error
("{n.locate}: {s}")
282 # Display a warning for a given syntax node
283 meth warning
(n
: PNode, s
: String)
285 _tc
.warning
("{n.locate}: {s}")
288 # Check conformity and display error
289 meth check_conform
(n
: PNode, subtype
: MMType, stype
: MMType): Bool
291 if stype
== null or subtype
== null then
294 if subtype
< stype
then
297 error
(n
, "Type error: expected {stype}, got {subtype}")
302 protected init(tc
: ToolContext, module: MMSrcModule)
309 ###############################################################################
312 protected meth accept_abs_syntax_visitor
(v
: AbsSyntaxVisitor) do visit_all
(v
)
318 # Symbol associated with the text
320 meth to_symbol
: Symbol
331 redef class PClassdef
332 # Associated class (MM entity)
333 meth local_class
: MMSrcLocalClass is abstract
336 redef class AAttrPropdef
337 # Associated attribute (MM entity)
338 meth prop
: MMSrcAttribute is abstract
340 # Associated read accessor (MM entity)
341 meth readmethod
: MMSrcMethod is abstract
343 # Associated write accessor (MM entity)
344 meth writemethod
: MMSrcMethod is abstract
347 redef class AMethPropdef
348 # Associated method (MM entity)
349 meth method
: MMMethSrcMethod is abstract
352 redef class ATypePropdef
353 # Associated formal type (MM entity)
354 meth prop
: MMSrcTypeProperty is abstract
358 # Position in the signature
359 meth position
: Int is abstract
361 # Associated local variable
362 meth variable
: Variable is abstract
366 # Retrieve the local class corresponding to the type.
367 # Display an error and return null if there is no class
368 # Display an error and return null if the type is not class based (formal one)
369 meth get_local_class
(v
: AbsSyntaxVisitor): MMLocalClass is abstract
371 # Retrieve corresponding static type.
372 # Display an error and return null if there is a problem
373 meth get_stype
(v
: AbsSyntaxVisitor): MMType is abstract
375 # Retrieve corresponding static type.
376 # Display an error and return null if there is a problem
377 # But do not performs any subtype check.
378 # get_unchecked_stype should be called to check that the static type is fully valid
379 meth get_unchecked_stype
(v
: AbsSyntaxVisitor): MMType is abstract
381 # Check that a static definition type is conform with regard to formal types
382 # Useful with get_unchecked_stype
383 # Remember that conformance check need that ancestors are totaly computed
384 meth check_conform
(v
: AbsSyntaxVisitor) is abstract
388 attr _stype_cache
: MMType
389 attr _stype_cached
: Bool
391 redef meth get_local_class
(v
)
393 var name
= n_id
.to_symbol
395 var cla
= v
.local_class
397 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
398 v
.error
(n_id
, "Type error: {name} is a formal type")
403 if not mod
.has_global_class_named
(name
) then
404 v
.error
(n_id
, "Type error: class {name} not found in module {mod}.")
409 var local_class
= mod
.class_by_name
(name
)
410 local_class
.global
.check_visibility
(v
, self, mod
)
414 redef meth get_unchecked_stype
(v
)
416 if _stype_cached
then return _stype_cache
419 var name
= n_id
.to_symbol
421 var cla
= v
.local_class
423 if cla
.formal_dict
.has_key
(name
) then
424 if n_types
.length
> 0 then
425 v
.error
(self, "Type error: formal type {name} cannot have formal parameters.")
428 var formal
= cla
.formal_dict
[name
]
429 _stype_cache
= formal
433 if cla
.global_properties
!= null and cla
.has_global_property_by_name
(name
) then
434 if n_types
.length
> 0 then
435 v
.error
(self, "Type error: formal type {name} cannot have formal parameters.")
438 var t
= cla
.get_type
.select_virtual_type
(name
).stype
440 v
.error
(self, "Type error: circular definition in formal type {name}.")
447 var local_class
= get_local_class
(v
)
448 if local_class
== null then return null
450 var arity
= n_types
.length
451 if local_class
.arity
!= arity
then
452 v
.error
(self, "Type error: '{local_class}' has {local_class.arity} parameters which differs from the {arity} params.")
457 var tab
= new Array[MMType]
459 tab
.add
(p
.get_unchecked_stype
(v
))
461 var t
= local_class
.get_instantiate_type
(tab
)
465 var t
= local_class
.get_type
471 redef meth get_stype
(v
)
473 var t
= get_unchecked_stype
(v
)
474 if t
!= null then check_conform
(v
)
478 redef meth check_conform
(v
)
480 var st
= get_unchecked_stype
(v
)
481 if st
== null then return
482 var local_class
= st
.local_class
483 var arity
= n_types
.length
485 for i
in [0..arity
[ do
487 var pt
= p
.get_stype
(v
)
488 var bt
= local_class
.get_formal
(i
).bound
489 if bt
== null then return
490 bt
= bt
.adapt_to
(st
) # We need to abapt because of F-genericity
491 v
.check_conform
(p
, pt
, bt
)
499 # Is null for statement and for erronus expression
500 meth stype
: MMType is abstract
503 redef class AVardeclExpr
504 # Assiociated local variable
505 readable writable attr _variable
: Variable
508 redef class AForVardeclExpr
509 # Associated automatic local variable
510 readable writable attr _variable
: Variable
513 redef class AVarFormExpr
514 # Associated local variable
515 readable writable attr _variable
: Variable