metamodel: fast-fail in getters
[nit.git] / src / metamodel / abstractmetamodel.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
4 # Copyright 2006-2008 Floréal Morandat <morandat@lirmm.fr>
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18 # core NIT metamodel classes featuring the minimal and simpliest entities
19 package abstractmetamodel
20
21 import partial_order
22
23 # The main singleton which knows everything
24 class MMContext
25
26 init do end
27
28 # The module dependence hierarchy
29 readable attr _module_hierarchy: PartialOrder[MMModule] = new PartialOrder[MMModule]
30
31 # The class refinement and specialization hierarchy
32 # It is not the real hierarchy since non concrete classes can only be leaves
33 readable attr _class_hierarchy: PartialOrder[MMLocalClass] = new PartialOrder[MMLocalClass]
34
35 # All known global classes
36 attr _global_classes: Array[MMGlobalClass] = new Array[MMGlobalClass]
37
38 # All known modules
39 readable attr _modules: Array[MMModule] = new Array[MMModule]
40
41 # Register a new module with the modules it depends on
42 meth add_module(module: MMModule, supers: Array[MMModule])
43 do
44 assert supers != null
45 _module_hierarchy.add(module, _module_hierarchy.select_smallests(supers))
46 assert module.name != null
47 _modules.add(module)
48 module._mhe = _module_hierarchy[module]
49 end
50
51 # Register a global class
52 private meth add_global_class(c: MMGlobalClass) do _global_classes.add(c)
53
54 # Register a local class
55 meth add_local_class(c: MMLocalClass, sup: Array[MMLocalClass])
56 do
57 assert sup != null
58 var csup = new Array[MMLocalClass]
59 var csups = new Array[String]
60 for s in sup do
61 if s isa MMConcreteClass then
62 csup.add(s)
63 else
64 for ss in s.che.direct_greaters do
65 if csup.has(ss) then continue
66 csup.add(ss)
67 end
68 end
69 end
70 var che = _class_hierarchy.add(c, csup)
71 c._che = che
72 end
73 end
74
75 # Directory of modules
76 class MMDirectory
77 # Full name of the directory
78 readable attr _name: Symbol
79
80 # Full path
81 readable attr _path: String
82
83 # Parent directory
84 # null if none
85 readable attr _parent: MMDirectory
86
87 # The module that introduces the directory if any
88 readable writable attr _owner: MMModule
89
90 # Known modules in the directory
91 readable attr _modules: Map[Symbol, MMModule] = new HashMap[Symbol, MMModule]
92
93 # Register a new module
94 meth add_module(module: MMModule)
95 do
96 assert not _modules.has_key(module.name)
97 _modules[module.name] = module
98 end
99
100 # Directory hierarchy element
101 readable attr _dhe: PartialOrderElement[MMDirectory]
102
103 init(name: Symbol, path: String, parent: MMDirectory) do
104 _name = name
105 _path = path
106 _parent = parent
107 end
108
109 # The fullname of a a potentiel module in the directory
110 meth full_name_for(module_name: Symbol): Symbol do
111 return "{name}/{module_name}".to_symbol
112 end
113 end
114
115 # A module is a NIT package
116 class MMModule
117 # Global context
118 readable attr _context: MMContext
119
120 # Short name of the module
121 readable attr _name: Symbol
122
123 # Full name of the module
124 readable attr _full_name: Symbol
125
126 # The directory of the module
127 readable attr _directory: MMDirectory
128
129 # Module dependence hierarchy element
130 readable attr _mhe: PartialOrderElement[MMModule]
131
132 # All global classes of the module (defined and imported)
133 readable attr _global_classes: Array[MMGlobalClass] = new Array[MMGlobalClass]
134
135 # All local classes of the module (defined and imported)
136 readable attr _local_classes: Array[MMLocalClass] = new Array[MMLocalClass]
137
138 # Class specialization hierarchy of the module.
139 readable attr _class_specialization_hierarchy: PartialOrder[MMLocalClass] = new PartialOrder[MMLocalClass]
140
141 # Modules intruded (directly or not)
142 attr _intrude_modules: Set[MMModule] = new HashSet[MMModule]
143
144 # Module publicly imported (directly or not)
145 attr _public_modules: Set[MMModule] = new HashSet[MMModule]
146
147 # Module privately imported (directly or not)
148 attr _private_modules: Set[MMModule] = new HashSet[MMModule]
149
150 # Explicit imported modules
151 readable attr _explicit_imported_modules: Set[MMModule] = new HashSet[MMModule]
152
153 # Association between local class and global classes
154 attr _local_class_by_global: Map[MMGlobalClass, MMLocalClass] = new HashMap[MMGlobalClass, MMLocalClass]
155
156 # Dictionary of global classes
157 attr _global_class_by_name: Map[Symbol, MMGlobalClass] = new HashMap[Symbol, MMGlobalClass]
158
159 protected init(name: Symbol, dir: MMDirectory, context: MMContext)
160 do
161 _name = name
162 _directory = dir
163 _context = context
164
165 if dir == null then
166 _full_name = name
167 else
168 _full_name = dir.full_name_for(name)
169 end
170 end
171
172 # Register that a module is imported with a visibility
173 # 0 -> intrude
174 # 1 -> public
175 # 3 -> private
176 meth add_super_module(m: MMModule, visibility_level: Int)
177 do
178 _explicit_imported_modules.add(m)
179 if visibility_level == 0 then
180 _intrude_modules.add(m)
181 _intrude_modules.add_all(m._intrude_modules)
182 _public_modules.add_all(m._public_modules)
183 _private_modules.add_all(m._private_modules)
184 else if visibility_level == 1 then
185 _public_modules.add(m)
186 _public_modules.add_all(m._intrude_modules)
187 _public_modules.add_all(m._public_modules)
188 else
189 _private_modules.add(m)
190 _private_modules.add_all(m._intrude_modules)
191 _private_modules.add_all(m._public_modules)
192 end
193
194 end
195
196 # Return the visibiliy level of a super-module
197 # 3 -> self or intruded => see all
198 # 2 -> public => see public and protected
199 # 1 -> private => see public and protected
200 # 0 -> nothing => see nothing
201 meth visibility_for(m: MMModule): Int
202 do
203 if m == self or _intrude_modules.has(m) then
204 return 3
205 else if _public_modules.has(m) then
206 return 2
207 else if _private_modules.has(m) then
208 return 1
209 else
210 return 0
211 end
212 end
213
214
215 # Get the local class associated with a global class
216 meth [](c: MMGlobalClass): MMLocalClass
217 do
218 assert _local_class_by_global != null
219 assert c != null
220 return _local_class_by_global[c]
221 end
222
223 # Register a local class to the module
224 meth add_local_class(c: MMLocalClass)
225 do
226 c._module = self
227 _local_classes.add(c)
228 end
229
230 # Get a local class by its name
231 meth class_by_name(n: Symbol): MMLocalClass
232 do
233 return self[_global_class_by_name[n]]
234 end
235
236 # Is there a global class with this name
237 meth has_global_class_named(n: Symbol): Bool
238 do
239 return _global_class_by_name.has_key(n)
240 end
241
242 # Get a global class by its name.
243 # Return null if not class match this name
244 meth global_class_named(n: Symbol): MMGlobalClass
245 do
246 return _global_class_by_name[n]
247 end
248
249 redef meth to_s do return name.to_s
250
251 # Assign super_classes for a local class
252 meth set_supers_class(c: MMLocalClass, supers: Array[MMLocalClass])
253 do
254 assert supers != null
255 var tab = _class_specialization_hierarchy.select_smallests(supers)
256 c._cshe = _class_specialization_hierarchy.add(c,tab)
257 var tab_all = c.crhe.direct_greaters.to_a
258 tab_all.add_all(tab)
259 context.add_local_class(c,tab_all)
260 end
261
262 # Register a local class and its global class in the module
263 private meth register_global_class(c: MMLocalClass)
264 do
265 assert c.global != null
266 _local_class_by_global[c.global] = c
267 end
268 end
269
270 class MMGlobalClass
271 # The introducing local class
272 readable attr _intro: MMLocalClass
273
274 # Class refinement hierarchy
275 # It is not the real hierarchy since non concrete classes can only be leaves
276 readable attr _class_refinement_hierarchy: PartialOrder[MMLocalClass] = new PartialOrder[MMLocalClass]
277
278 # Create a new global class introduced with a given local class
279 init(c: MMLocalClass)
280 do
281 _intro = c
282 c.context.add_global_class(self)
283 end
284
285 # The name of the global class
286 meth name: Symbol
287 do
288 return intro.name
289 end
290
291 # The module that introduces the global class
292 meth module: MMModule
293 do
294 return intro.module
295 end
296
297 redef meth to_s
298 do
299 return intro.to_s
300 end
301
302 # register a new Local class to local class hierarchy (set the crhe value)
303 private meth register_local_class(c: MMLocalClass)
304 do
305 assert c.module != null
306 assert c.crhe == null
307 var sup = new Array[MMLocalClass]
308 for s in class_refinement_hierarchy do
309 if c.module.mhe < s.module and s isa MMConcreteClass then
310 sup.add(s)
311 end
312 end
313 c._crhe = _class_refinement_hierarchy.add(c, sup)
314 end
315
316 # Is the global class an interface?
317 readable writable attr _is_interface: Bool = false
318
319 # Is the global class an abstract class?
320 readable writable attr _is_abstract: Bool = false
321
322 # Is the global class a universal class?
323 readable writable attr _is_universal: Bool = false
324
325 # Visibility of the global class
326 # 1 -> public
327 # 3 -> private
328 readable writable attr _visibility_level: Int = 1 # FIXME: why this should be defined ?
329
330 # Is the global class a mixin class?
331 # A mixin class inherits all constructors from a superclass
332 meth is_mixin: Bool
333 do
334 return _mixin_of != self
335 end
336
337 # Indicate the superclass the class is a mixin of.
338 # If mixin_of == self then the class is not a mixin
339 # Is two classes have the same mixin_of value, then they both belong to the same mixing group
340 readable writable attr _mixin_of: MMGlobalClass = self
341
342 end
343
344 # Local classes are classes defined, refined or imported in a module
345 class MMLocalClass
346 # The name of the local class
347 readable attr _name: Symbol
348
349 # Arity of the local class (if generic)
350 # FIXME: How to move this into the generic module in a sane way?
351 readable attr _arity : Int
352
353 # The module of the local class
354 readable attr _module: MMModule
355
356 # The global class of the local class
357 readable attr _global: MMGlobalClass
358
359 # The local class refinement hierarchy element
360 readable attr _crhe: PartialOrderElement[MMLocalClass]
361
362 # The local class specialization hierarchy element
363 readable attr _cshe: PartialOrderElement[MMLocalClass]
364
365 # The local class full hierarchy element
366 readable attr _che: PartialOrderElement[MMLocalClass]
367
368 # Association between local properties and global properties
369 readable attr _local_property_by_global: Map[MMGlobalProperty, MMLocalProperty]
370
371 # All known global properties
372 readable attr _global_properties: Set[MMGlobalProperty]
373
374 # Dictionnary of global properties
375 readable attr _properties_by_name: Map[Symbol, Array[MMGlobalProperty]]
376
377 # Create a new class with a given name and arity
378 protected init(name: Symbol, arity: Int)
379 do
380 _name = name
381 _arity = arity
382 end
383
384 # The corresponding local class in another module
385 meth for_module(m: MMModule): MMLocalClass
386 do
387 return m[global]
388 end
389
390 # Introduce a new global class to a new global one and register to hierarchy with no refine
391 meth new_global
392 do
393 var g = new MMGlobalClass(self)
394 _module._global_classes.add(g)
395 _module._global_class_by_name[name] = g
396 set_global(g)
397 end
398
399 # Associate this local class to a global one and register to hierarchy
400 # the global class for this class
401 # refined classes for this class
402 meth set_global(g: MMGlobalClass)
403 do
404 assert g != null
405 _global = g
406 _global.register_local_class(self)
407 _module.register_global_class(self)
408 end
409
410 # Is there a global propery with a given name
411 # TODO: Will disapear when qualified names will be available
412 meth has_global_property_by_name(n: Symbol): Bool
413 do
414 return _properties_by_name.has_key(n) and _properties_by_name[n].length == 1
415 end
416
417 # Get a global property by its name
418 # TODO: Will disapear when qualified names will be available
419 meth get_property_by_name(n: Symbol): MMGlobalProperty
420 do
421 if not has_global_property_by_name(n) then abort
422 var props = _properties_by_name[n]
423 return props.first
424 end
425
426 # Get a attribute by its name
427 # TODO: Will disapear when qualified names will be available
428 meth attribute(a: Symbol): MMGlobalProperty
429 do
430 return get_property_by_name(a)
431 end
432
433 # Get a method by its name
434 # TODO: Will disapear when qualified names will be available
435 meth method(na: Symbol): MMGlobalProperty
436 do
437 assert _properties_by_name != null
438 return _properties_by_name[na].first
439 end
440
441 # Select a method from its name
442 # TODO: Will disapear when qualified names will be available
443 meth select_method(name: Symbol): MMMethod
444 do
445 assert name != null
446 var gp = method(name)
447 var res = self[gp]
448 assert res isa MMMethod
449 return res
450 end
451
452 # Select an attribute from its name
453 # TODO: Will disapear when qualified names will be available
454 meth select_attribute(name: Symbol): MMAttribute
455 do
456 assert name != null
457 var gp = attribute(name)
458 var res = self[gp]
459 assert res isa MMAttribute
460 return res
461 end
462
463 # Look in super-classes (by specialization) and return properties with name
464 # Beware, global property of results is not intended to be the same
465 meth super_methods_named(n: Symbol): Array[MMLocalProperty]
466 do
467 var classes = new Array[MMLocalClass]
468 for c in cshe.greaters do
469 if c.has_global_property_by_name(n) then classes.add(c)
470 end
471 classes = cshe.order.select_smallests(classes)
472 var res = new Array[MMLocalProperty]
473 for c in classes do
474 var g = c.method(n)
475 #print "found {c[g].full_name}"
476 res.add(c[g])
477 end
478 return res
479 end
480
481 # Register a local property and associate it with its global property
482 meth register_local_property(p: MMLocalProperty)
483 do
484 assert p.global != null
485 # FIXME: Why a test?
486 if not _local_property_by_global.has_key(p.global) then
487 _local_property_by_global[p.global] = p
488 end
489 end
490
491 # Register a global property and associate it with its name
492 meth register_global_property(glob: MMGlobalProperty)
493 do
494 var prop = glob.intro
495 var name = prop.name
496 if _properties_by_name.has_key(name) then
497 _properties_by_name[name].add(glob)
498 else
499 _properties_by_name[name] = [glob]
500 end
501 _global_properties.add(glob)
502 register_local_property(prop)
503 end
504
505 # Get a local proprty by its global property
506 meth [](glob: MMGlobalProperty): MMLocalProperty
507 do
508 return _local_property_by_global[glob]
509 end
510
511 # The current MMContext
512 meth context: MMContext do return module.context
513
514 redef meth to_s
515 do
516 return _name.to_s
517 end
518 end
519
520 # A global property gather local properties that correspond to a same message
521 class MMGlobalProperty
522 # The local property for each class that has the global property
523
524 # The introducing local property
525 readable attr _intro: MMLocalProperty
526
527 # The local class that introduces the global property
528 meth local_class: MMLocalClass
529 do
530 return intro.local_class
531 end
532
533 # The property redefinition hierarchy
534 readable attr _property_hierarchy: PartialOrder[MMLocalProperty] = new PartialOrder[MMLocalProperty]
535
536 # Create a new global property introduced by a local one
537 protected init(p: MMLocalProperty)
538 do
539 assert p != null
540
541 _property_hierarchy = new PartialOrder[MMLocalProperty]
542
543 _intro = p
544 add_local_property(p, new Array[MMLocalProperty])
545 end
546
547 redef meth to_s do return intro.full_name
548
549 # Register a new local property
550 meth add_local_property(i: MMLocalProperty, sup: Array[MMLocalProperty])
551 do
552 assert i != null
553 assert sup != null
554 i._prhe = _property_hierarchy.add(i,sup)
555 end
556
557 # Is the global property an attribute ?
558 meth is_attribute: Bool do return intro isa MMAttribute
559
560 # Is the global property a method (or a constructor)?
561 meth is_method: Bool do return intro isa MMMethod
562
563 # Is the global property a constructor (thus also a method)?
564 readable writable attr _is_init: Bool
565
566 # Is the global property a constructor for a given class?
567 meth is_init_for(c: MMLocalClass): Bool
568 do
569 if not is_init then return false
570 var sc = intro.local_class
571 var res = c.che <= sc and c.global.mixin_of == sc.global.mixin_of
572 return res
573 end
574
575 # Visibility of the property
576 # 1 -> public
577 # 2 -> protected
578 # 3 -> private
579 readable writable attr _visibility_level: Int = 1 # FIXME: why this should be defined ?
580 end
581
582 # Local properties are properties defined (explicitely or not) in a local class
583 class MMLocalProperty
584 # The name of the property
585 readable attr _name: Symbol
586
587 # The local class who own the local property
588 readable attr _local_class: MMLocalClass
589
590 # The global property where belong the local property
591 readable attr _global: MMGlobalProperty
592
593 # Redefinition hierarchy of the local property
594 readable attr _prhe: PartialOrderElement[MMLocalProperty]
595
596 # The module of the local property
597 meth module: MMModule do return _local_class.module
598
599 # Full expanded name with all qualifications
600 meth full_name: String
601 do
602 if global == null then
603 return "{local_class.module}::{local_class}::(?::{name})"
604 else if global.intro == self then
605 return "{local_class.module}::{local_class}::{name}"
606 else
607 return "{local_class.module}::{local_class}::({global.intro.full_name})"
608 end
609 end
610
611 # set the global property for this property
612 meth set_global(g: MMGlobalProperty)
613 do
614 assert g != null
615 _global = g
616 _local_class.register_local_property(self)
617 end
618
619 # Introduce a new global property for this local one
620 meth new_global
621 do
622 assert _global == null
623 _global = new MMGlobalProperty(self)
624 _local_class.register_global_property(_global)
625 end
626
627 redef meth to_s do return name.to_s
628
629 # Is the concrete property contain a `super' in the body?
630 readable writable attr _need_super: Bool = false
631
632 protected init(n: Symbol, bc: MMLocalClass)
633 do
634 _name = n
635 _local_class = bc
636 end
637 end
638
639 # Attribute local properties
640 class MMAttribute
641 special MMLocalProperty
642 end
643
644 # Method local properties
645 class MMMethod
646 special MMLocalProperty
647 end
648
649 # Concrete local classes
650 class MMConcreteClass
651 special MMLocalClass
652 end
653