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