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