Automatic constructors.
[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)
33 do
34 super(name, dir, c)
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 writable attr _formal_dict: Map[Symbol, MMTypeFormalParameter]
67
68 # Concrete NIT source properties by name
69 readable attr _src_local_properties: Map[Symbol, MMLocalProperty]
70
71 init(n: Symbol, cla: PClassdef, a: Int)
72 do
73 super(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: 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: AMethPropdef
160 init(name: Symbol, cla: MMLocalClass, n: 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 variable and method parameter
194 class Variable
195 # Name of the variable
196 readable attr _name: Symbol
197
198 # Declaration AST node
199 readable attr _decl: PNode
200
201 # Static type
202 readable writable attr _stype: MMType
203
204 redef meth to_s do return _name.to_s
205
206 init(n: Symbol, d: PNode)
207 do
208 assert n != null
209 assert d != null
210 _name = n
211 _decl = d
212 end
213 end
214
215 ###############################################################################
216
217 # Visitor used during the syntax analysis
218 class AbsSyntaxVisitor
219 special Visitor
220 # The primitive type Bool
221 meth type_bool: MMType
222 do
223 return _module.class_by_name(once ("Bool".to_symbol)).get_type
224 end
225
226 # The primitive type Int
227 meth type_int: MMType
228 do
229 return _module.class_by_name(once ("Int".to_symbol)).get_type
230 end
231
232 # The primitive type Float
233 meth type_float: MMType
234 do
235 return _module.class_by_name(once ("Float".to_symbol)).get_type
236 end
237
238 # The primitive type Char
239 meth type_char: MMType
240 do
241 return _module.class_by_name(once ("Char".to_symbol)).get_type
242 end
243
244 # The primitive type String
245 meth type_string: MMType
246 do
247 return _module.class_by_name(once ("String".to_symbol)).get_type
248 end
249
250 # The primitive type Collection[Object]
251 meth type_collection: MMType
252 do
253 return _module.class_by_name(once ("Collection".to_symbol)).get_type
254 end
255
256 # The primitive type Array[?]
257 meth type_array(stype: MMType): MMType
258 do
259 return _module.class_by_name(once ("Array".to_symbol)).get_instantiate_type([stype])
260 end
261
262 # The primitive type Discrete
263 meth type_discrete: MMType
264 do
265 return _module.class_by_name(once ("Discrete".to_symbol)).get_type
266 end
267
268 # The primitive type Range[?]
269 meth type_range(stype: MMType): MMType
270 do
271 return _module.class_by_name(once ("Range".to_symbol)).get_instantiate_type([stype])
272 end
273
274 # The primitive type of null
275 meth type_none: MMType
276 do
277 return _module.type_none
278 end
279
280 # The current module
281 readable writable attr _module: MMSrcModule
282
283 # The current class
284 readable writable attr _local_class: MMSrcLocalClass
285
286 # The current property
287 readable writable attr _local_property: MMLocalProperty
288
289 # The current tool configuration/status
290 readable attr _tc: ToolContext
291
292 # Display an error for a given syntax node
293 meth error(n: PNode, s: String)
294 do
295 _tc.error("{locate(n)}: {s}")
296 end
297
298 # Display a warning for a given syntax node
299 meth warning(n: PNode, s: String)
300 do
301 _tc.warning("{locate(n)}: {s}")
302 end
303
304 #
305 meth locate(n: PNode): String
306 do
307 if n != null then return n.locate
308 return _module.filename
309 end
310
311 # Check conformity and display error
312 meth check_conform(n: PNode, subtype: MMType, stype: MMType): Bool
313 do
314 if stype == null or subtype == null then
315 return false
316 end
317 if subtype < stype then
318 return true
319 end
320 #error(n, "Type error: expected {stype}'{stype.module}, got {subtype}'{subtype.module}")
321 #abort
322 error(n, "Type error: expected {stype}, got {subtype}")
323 return false
324 end
325
326
327 protected init(tc: ToolContext, module: MMSrcModule)
328 do
329 _tc = tc
330 _module = module
331 end
332 end
333
334 ###############################################################################
335
336 redef class PNode
337 protected meth accept_abs_syntax_visitor(v: AbsSyntaxVisitor) do visit_all(v)
338 end
339
340 redef class Token
341 attr _symbol: Symbol
342
343 # Symbol associated with the text
344 # Lazily computed
345 meth to_symbol: Symbol
346 do
347 var s = _symbol
348 if s == null then
349 s = text.to_symbol
350 _symbol = s
351 end
352 return s
353 end
354 end
355
356 redef class PClassdef
357 # Associated class (MM entity)
358 meth local_class: MMSrcLocalClass is abstract
359 end
360
361 redef class AAttrPropdef
362 # Associated attribute (MM entity)
363 meth prop: MMSrcAttribute is abstract
364
365 # Associated read accessor (MM entity)
366 meth readmethod: MMSrcMethod is abstract
367
368 # Associated write accessor (MM entity)
369 meth writemethod: MMSrcMethod is abstract
370 end
371
372 redef class AMethPropdef
373 # Associated method (MM entity)
374 meth method: MMMethSrcMethod is abstract
375 end
376
377 redef class ATypePropdef
378 # Associated formal type (MM entity)
379 meth prop: MMSrcTypeProperty is abstract
380 end
381
382 redef class PParam
383 # Position in the signature
384 meth position: Int is abstract
385
386 # Associated local variable
387 meth variable: Variable is abstract
388 end
389
390 redef class PType
391 # Retrieve the local class corresponding to the type.
392 # Display an error and return null if there is no class
393 # Display an error and return null if the type is not class based (formal one)
394 meth get_local_class(v: AbsSyntaxVisitor): MMLocalClass is abstract
395
396 # Retrieve corresponding static type.
397 # Display an error and return null if there is a problem
398 meth get_stype(v: AbsSyntaxVisitor): MMType is abstract
399
400 # Retrieve corresponding static type.
401 # Display an error and return null if there is a problem
402 # But do not performs any subtype check.
403 # get_unchecked_stype should be called to check that the static type is fully valid
404 meth get_unchecked_stype(v: AbsSyntaxVisitor): MMType is abstract
405
406 # Check that a static definition type is conform with regard to formal types
407 # Useful with get_unchecked_stype
408 # Remember that conformance check need that ancestors are totaly computed
409 meth check_conform(v: AbsSyntaxVisitor) is abstract
410 end
411
412 redef class AType
413 attr _stype_cache: MMType
414 attr _stype_cached: Bool
415
416 redef meth get_local_class(v)
417 do
418 var name = n_id.to_symbol
419 var mod = v.module
420 var cla = v.local_class
421
422 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
423 v.error(n_id, "Type error: {name} is a formal type")
424 _stype_cached = true
425 return null
426 end
427
428 if not mod.has_global_class_named(name) then
429 v.error(n_id, "Type error: class {name} not found in module {mod}.")
430 _stype_cached = true
431 return null
432 end
433
434 var local_class = mod.class_by_name(name)
435 local_class.global.check_visibility(v, self, mod)
436 return local_class
437 end
438
439 redef meth get_unchecked_stype(v)
440 do
441 if _stype_cached then return _stype_cache
442 _stype_cached = true
443
444 var name = n_id.to_symbol
445 var mod = v.module
446 var cla = v.local_class
447
448 if cla.formal_dict.has_key(name) then
449 if n_types.length > 0 then
450 v.error(self, "Type error: formal type {name} cannot have formal parameters.")
451 return null
452 end
453 var formal = cla.formal_dict[name]
454 _stype_cache = formal
455 return formal
456 end
457
458 if cla.global_properties != null and cla.has_global_property_by_name(name) then
459 if n_types.length > 0 then
460 v.error(self, "Type error: formal type {name} cannot have formal parameters.")
461 return null
462 end
463 var t = cla.get_type.local_class.select_virtual_type(name).stype_for(cla.get_type)
464 if t == null then
465 v.error(self, "Type error: circular definition in formal type {name}.")
466 return null
467 end
468 _stype_cache = t
469 return t
470 end
471
472 var local_class = get_local_class(v)
473 if local_class == null then return null
474
475 var arity = n_types.length
476 if local_class.arity != arity then
477 v.error(self, "Type error: '{local_class}' has {local_class.arity} parameters which differs from the {arity} params.")
478 return null
479 end
480
481 if arity > 0 then
482 var tab = new Array[MMType]
483 for p in n_types do
484 tab.add(p.get_unchecked_stype(v))
485 end
486 var t = local_class.get_instantiate_type(tab)
487 _stype_cache = t
488 return t
489 else
490 var t = local_class.get_type
491 _stype_cache = t
492 return t
493 end
494 end
495
496 redef meth get_stype(v)
497 do
498 var t = get_unchecked_stype(v)
499 if t != null then check_conform(v)
500 return t
501 end
502
503 redef meth check_conform(v)
504 do
505 var st = get_unchecked_stype(v)
506 if st == null then return
507 var local_class = st.local_class
508 var arity = n_types.length
509 if arity > 0 then
510 for i in [0..arity[ do
511 var p = n_types[i]
512 var pt = p.get_stype(v)
513 var bt = local_class.get_formal(i).bound
514 if bt == null then return
515 bt = bt.adapt_to(st) # We need to abapt because of F-genericity
516 v.check_conform(p, pt, bt)
517 end
518 end
519 end
520 end
521
522 redef class PExpr
523 # Static type
524 # Is null for statement and for erronus expression
525 meth stype: MMType is abstract
526 end
527
528 redef class AVardeclExpr
529 # Assiociated local variable
530 readable writable attr _variable: Variable
531 end
532
533 redef class AForVardeclExpr
534 # Associated automatic local variable
535 readable writable attr _variable: Variable
536 end
537
538 redef class AVarFormExpr
539 # Associated local variable
540 readable writable attr _variable: Variable
541 end
542