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