Add default values for some primitive type attributes.
[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 if _local_class_by_global.has_key(c) then
221 return _local_class_by_global[c]
222 else
223 return null
224 end
225 end
226
227 # Register a local class to the module
228 meth add_local_class(c: MMLocalClass)
229 do
230 c._module = self
231 _local_classes.add(c)
232 end
233
234 # Get a local class by its name
235 meth class_by_name(n: Symbol): MMLocalClass
236 do
237 return self[_global_class_by_name[n]]
238 end
239
240 # Is there a global class with this name
241 meth has_global_class_named(n: Symbol): Bool
242 do
243 return _global_class_by_name.has_key(n)
244 end
245
246 # Get a global class by its name.
247 # Return null if not class match this name
248 meth global_class_named(n: Symbol): MMGlobalClass
249 do
250 if _global_class_by_name.has_key(n) then
251 return _global_class_by_name[n]
252 else
253 return null
254 end
255 end
256
257 redef meth to_s do return name.to_s
258
259 # Assign super_classes for a local class
260 meth set_supers_class(c: MMLocalClass, supers: Array[MMLocalClass])
261 do
262 assert supers != null
263 var tab = _class_specialization_hierarchy.select_smallests(supers)
264 c._cshe = _class_specialization_hierarchy.add(c,tab)
265 var tab_all = c.crhe.direct_greaters.to_a
266 tab_all.add_all(tab)
267 context.add_local_class(c,tab_all)
268 end
269
270 # Register a local class and its global class in the module
271 private meth register_global_class(c: MMLocalClass)
272 do
273 assert c.global != null
274 _local_class_by_global[c.global] = c
275 end
276 end
277
278 class MMGlobalClass
279 # The introducing local class
280 readable attr _intro: MMLocalClass
281
282 # Class refinement hierarchy
283 # It is not the real hierarchy since non concrete classes can only be leaves
284 readable attr _class_refinement_hierarchy: PartialOrder[MMLocalClass] = new PartialOrder[MMLocalClass]
285
286 # Create a new global class introduced with a given local class
287 init(c: MMLocalClass)
288 do
289 _intro = c
290 c.context.add_global_class(self)
291 end
292
293 # The name of the global class
294 meth name: Symbol
295 do
296 return intro.name
297 end
298
299 # The module that introduces the global class
300 meth module: MMModule
301 do
302 return intro.module
303 end
304
305 redef meth to_s
306 do
307 return intro.to_s
308 end
309
310 # register a new Local class to local class hierarchy (set the crhe value)
311 private meth register_local_class(c: MMLocalClass)
312 do
313 assert c.module != null
314 assert c.crhe == null
315 var sup = new Array[MMLocalClass]
316 for s in class_refinement_hierarchy do
317 if c.module.mhe < s.module and s isa MMConcreteClass then
318 sup.add(s)
319 end
320 end
321 c._crhe = _class_refinement_hierarchy.add(c, sup)
322 end
323
324 # Is the global class an interface?
325 readable writable attr _is_interface: Bool = false
326
327 # Is the global class an abstract class?
328 readable writable attr _is_abstract: Bool = false
329
330 # Is the global class a universal class?
331 readable writable attr _is_universal: Bool = false
332
333 # Visibility of the global class
334 # 1 -> public
335 # 3 -> private
336 readable writable attr _visibility_level: Int = 1 # FIXME: why this should be defined ?
337
338 # Is the global class a mixin class?
339 # A mixin class inherits all constructors from a superclass
340 meth is_mixin: Bool
341 do
342 return _mixin_of != self
343 end
344
345 # Indicate the superclass the class is a mixin of.
346 # If mixin_of == self then the class is not a mixin
347 # Is two classes have the same mixin_of value, then they both belong to the same mixing group
348 readable writable attr _mixin_of: MMGlobalClass = self
349
350 end
351
352 # Local classes are classes defined, refined or imported in a module
353 class MMLocalClass
354 # The name of the local class
355 readable attr _name: Symbol
356
357 # Arity of the local class (if generic)
358 # FIXME: How to move this into the generic module in a sane way?
359 readable attr _arity : Int
360
361 # The module of the local class
362 readable attr _module: MMModule
363
364 # Is the class abstract
365 readable writable attr _abstract: Bool
366
367 # The global class of the local class
368 readable attr _global: MMGlobalClass
369
370 # The local class refinement hierarchy element
371 readable attr _crhe: PartialOrderElement[MMLocalClass]
372
373 # The local class specialization hierarchy element
374 readable attr _cshe: PartialOrderElement[MMLocalClass]
375
376 # The local class full hierarchy element
377 readable attr _che: PartialOrderElement[MMLocalClass]
378
379 # Association between local properties and global properties
380 readable attr _local_property_by_global: Map[MMGlobalProperty, MMLocalProperty]
381
382 # All known global properties
383 readable attr _global_properties: Set[MMGlobalProperty]
384
385 # Dictionnary of global properties
386 readable attr _properties_by_name: Map[Symbol, Array[MMGlobalProperty]]
387
388 # Create a new class with a given name and arity
389 protected init(name: Symbol, arity: Int)
390 do
391 _name = name
392 _arity = arity
393 end
394
395 # The corresponding local class in another module
396 meth for_module(m: MMModule): MMLocalClass
397 do
398 return m[global]
399 end
400
401 # Introduce a new global class to a new global one and register to hierarchy with no refine
402 meth new_global
403 do
404 var g = new MMGlobalClass(self)
405 _module._global_classes.add(g)
406 _module._global_class_by_name[name] = g
407 set_global(g)
408 end
409
410 # Associate this local class to a global one and register to hierarchy
411 # the global class for this class
412 # refined classes for this class
413 meth set_global(g: MMGlobalClass)
414 do
415 assert g != null
416 _global = g
417 _global.register_local_class(self)
418 _module.register_global_class(self)
419 end
420
421 # Is there a global propery with a given name
422 # TODO: Will disapear when qualified names will be available
423 meth has_global_property_by_name(n: Symbol): Bool
424 do
425 var props = _properties_by_name[n]
426 return props != null
427 end
428
429 # Get a global property by its name
430 # TODO: Will disapear when qualified names will be available
431 meth get_property_by_name(n: Symbol): MMGlobalProperty
432 do
433 var props = _properties_by_name[n]
434 if props == null or props.length > 1 then
435 return null
436 end
437 return props.first
438 end
439
440 # Get a attribute by its name
441 # TODO: Will disapear when qualified names will be available
442 meth attribute(a: Symbol): MMGlobalProperty
443 do
444 return get_property_by_name(a)
445 end
446
447 # Get a method by its name
448 # TODO: Will disapear when qualified names will be available
449 meth method(na: Symbol): MMGlobalProperty
450 do
451 assert _properties_by_name != null
452 var props = _properties_by_name[na]
453 if props != null then
454 return props.first
455 end
456
457 return null
458 end
459
460 # Select a method from its name
461 # TODO: Will disapear when qualified names will be available
462 meth select_method(name: Symbol): MMMethod
463 do
464 assert name != null
465 var gp = method(name)
466 if gp == null then return null
467 var res = self[gp]
468 assert res isa MMMethod
469 return res
470 end
471
472 # Select an attribute from its name
473 # TODO: Will disapear when qualified names will be available
474 meth select_attribute(name: Symbol): MMAttribute
475 do
476 assert name != null
477 var gp = attribute(name)
478 if gp == null then return null
479 var res = self[gp]
480 assert res isa MMAttribute
481 return res
482 end
483
484 # Look in super-classes (by specialization) and return properties with name
485 # Beware, global property of results is not intended to be the same
486 meth super_methods_named(n: Symbol): Array[MMLocalProperty]
487 do
488 var classes = new Array[MMLocalClass]
489 for c in cshe.greaters do
490 var g = c.method(n)
491 if g == null then continue
492 classes.add(c)
493 end
494 classes = cshe.order.select_smallests(classes)
495 var res = new Array[MMLocalProperty]
496 for c in classes do
497 var g = c.method(n)
498 #print "found {c[g].full_name}"
499 res.add(c[g])
500 end
501 return res
502 end
503
504 # Register a local property and associate it with its global property
505 meth register_local_property(p: MMLocalProperty)
506 do
507 assert p.global != null
508 # FIXME: Why a test?
509 if not _local_property_by_global.has_key(p.global) then
510 _local_property_by_global[p.global] = p
511 end
512 end
513
514 # Register a global property and associate it with its name
515 meth register_global_property(glob: MMGlobalProperty)
516 do
517 var prop = glob.intro
518 var name = prop.name
519 var props = _properties_by_name[name]
520 if props == null then
521 _properties_by_name[name] = [glob]
522 else
523 _properties_by_name[name].add(glob)
524 end
525 _global_properties.add(glob)
526 register_local_property(prop)
527 end
528
529 # Get a local proprty by its global property
530 meth [](glob: MMGlobalProperty): MMLocalProperty
531 do
532 assert _local_property_by_global != null
533 assert glob != null
534 if _local_property_by_global.has_key(glob) then
535 return _local_property_by_global[glob]
536 end
537 return null
538 end
539
540 # The current MMContext
541 meth context: MMContext do return module.context
542
543 redef meth to_s
544 do
545 return _name.to_s
546 end
547 end
548
549 # A global property gather local properties that correspond to a same message
550 class MMGlobalProperty
551 # The local property for each class that has the global property
552
553 # The introducing local property
554 readable attr _intro: MMLocalProperty
555
556 # The local class that introduces the global property
557 meth local_class: MMLocalClass
558 do
559 return intro.local_class
560 end
561
562 # The property redefinition hierarchy
563 readable attr _property_hierarchy: PartialOrder[MMLocalProperty] = new PartialOrder[MMLocalProperty]
564
565 # Create a new global property introduced by a local one
566 protected init(p: MMLocalProperty)
567 do
568 assert p != null
569
570 _property_hierarchy = new PartialOrder[MMLocalProperty]
571
572 _intro = p
573 add_local_property(p, new Array[MMLocalProperty])
574 end
575
576 redef meth to_s do return intro.full_name
577
578 # Register a new local property
579 meth add_local_property(i: MMLocalProperty, sup: Array[MMLocalProperty])
580 do
581 assert i != null
582 assert sup != null
583 i._prhe = _property_hierarchy.add(i,sup)
584 end
585
586 # Is the global property an attribute ?
587 meth is_attribute: Bool do return intro isa MMAttribute
588
589 # Is the global property a method (or a constructor)?
590 meth is_method: Bool do return intro isa MMMethod
591
592 # Is the global property a constructor (thus also a method)?
593 readable writable attr _is_init: Bool
594
595 # Is the global property a constructor for a given class?
596 meth is_init_for(c: MMLocalClass): Bool
597 do
598 if not is_init then return false
599 var sc = intro.local_class
600 var res = c.che <= sc and c.global.mixin_of == sc.global.mixin_of
601 return res
602 end
603
604 # Visibility of the property
605 # 1 -> public
606 # 2 -> protected
607 # 3 -> private
608 readable writable attr _visibility_level: Int = 1 # FIXME: why this should be defined ?
609 end
610
611 # Local properties are properties defined (explicitely or not) in a local class
612 class MMLocalProperty
613 # The name of the property
614 readable attr _name: Symbol
615
616 # The local class who own the local property
617 readable attr _local_class: MMLocalClass
618
619 # The global property where belong the local property
620 readable attr _global: MMGlobalProperty
621
622 # Redefinition hierarchy of the local property
623 readable attr _prhe: PartialOrderElement[MMLocalProperty]
624
625 # The module of the local property
626 meth module: MMModule do return _local_class.module
627
628 # Full expanded name with all qualifications
629 meth full_name: String
630 do
631 if global == null then
632 return "{local_class.module}::{local_class}::(?::{name})"
633 else if global.intro == self then
634 return "{local_class.module}::{local_class}::{name}"
635 else
636 return "{local_class.module}::{local_class}::({global.intro.full_name})"
637 end
638 end
639
640 # set the global property for this property
641 meth set_global(g: MMGlobalProperty)
642 do
643 assert g != null
644 _global = g
645 _local_class.register_local_property(self)
646 end
647
648 # Introduce a new global property for this local one
649 meth new_global
650 do
651 assert _global == null
652 _global = new MMGlobalProperty(self)
653 _local_class.register_global_property(_global)
654 end
655
656 redef meth to_s do return name.to_s
657
658 # Is the concrete property contain a `super' in the body?
659 readable writable attr _need_super: Bool = false
660
661 protected init(n: Symbol, bc: MMLocalClass)
662 do
663 _name = n
664 _local_class = bc
665 end
666 end
667
668 # Attribute local properties
669 class MMAttribute
670 special MMLocalProperty
671 end
672
673 # Method local properties
674 class MMMethod
675 special MMLocalProperty
676 end
677
678 # Concrete local classes
679 class MMConcreteClass
680 special MMLocalClass
681 end
682