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