1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
4 # Copyright 2006-2008 Floréal Morandat <morandat@lirmm.fr>
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
10 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 # core NIT metamodel classes featuring the minimal and simpliest entities
19 package abstractmetamodel
24 # The main singleton which knows everything
29 # The module dependence hierarchy
30 readable var _module_hierarchy
: PartialOrder[MMModule] = new PartialOrder[MMModule]
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]
36 # All known global classes
37 var _global_classes
: Array[MMGlobalClass] = new Array[MMGlobalClass]
40 readable var _modules
: Array[MMModule] = new Array[MMModule]
42 # Register a new module with the modules it depends on
43 fun add_module
(module: MMModule, supers
: Array[MMModule])
45 _module_hierarchy
.add
(module, _module_hierarchy
.select_smallests
(supers
))
47 module._mhe
= _module_hierarchy
[module]
50 # Register a global class
51 private fun add_global_class
(c
: MMGlobalClass) do _global_classes
.add
(c
)
53 # Register a local class
54 fun add_local_class
(c
: MMLocalClass, sup
: Array[MMLocalClass])
56 var csup
= new Array[MMLocalClass]
57 var csups
= new Array[String]
59 if s
isa MMConcreteClass then
62 for ss
in s
.che
.direct_greaters
do
63 if csup
.has
(ss
) then continue
68 var che
= _class_hierarchy
.add
(c
, csup
)
73 # Directory of modules
75 # Full name of the directory
76 readable var _name
: Symbol
79 readable var _path
: String
83 readable var _parent
: nullable MMDirectory
85 # The module that introduces the directory if any
86 readable writable var _owner
: nullable MMModule = null
88 # Known modules in the directory
89 readable var _modules
: Map[Symbol, MMModule] = new HashMap[Symbol, MMModule]
91 # Register a new module
92 fun add_module
(module: MMModule)
94 assert not _modules
.has_key
(module.name
)
95 _modules
[module.name
] = module
98 init(name
: Symbol, path
: String, parent
: nullable MMDirectory) do
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
110 # A module is a NIT package
113 readable var _context
: MMContext
115 # Short name of the module
116 readable var _name
: Symbol
118 # Full name of the module
119 readable var _full_name
: Symbol
121 # The directory of the module
122 readable var _directory
: MMDirectory
124 # Location of this module
125 readable var _location
: Location
127 # Module dependence hierarchy element
128 readable var _mhe
: nullable PartialOrderElement[MMModule]
130 # All global classes of the module (defined and imported)
131 readable var _global_classes
: Array[MMGlobalClass] = new Array[MMGlobalClass]
133 # All local classes of the module (defined and imported)
134 readable var _local_classes
: Array[MMLocalClass] = new Array[MMLocalClass]
136 # Class specialization hierarchy of the module.
137 readable var _class_specialization_hierarchy
: PartialOrder[MMLocalClass] = new PartialOrder[MMLocalClass]
139 # Modules intruded (directly or not)
140 var _intrude_modules
: Set[MMModule] = new HashSet[MMModule]
142 # Module publicly imported (directly or not)
143 var _public_modules
: Set[MMModule] = new HashSet[MMModule]
145 # Module privately imported (directly or not)
146 var _private_modules
: Set[MMModule] = new HashSet[MMModule]
148 # Explicit imported modules
149 readable var _explicit_imported_modules
: Set[MMModule] = new HashSet[MMModule]
151 # Association between local class and global classes
152 var _local_class_by_global
: Map[MMGlobalClass, MMLocalClass] = new HashMap[MMGlobalClass, MMLocalClass]
154 # Dictionary of global classes
155 var _global_class_by_name
: Map[Symbol, MMGlobalClass] = new HashMap[Symbol, MMGlobalClass]
157 protected init(name
: Symbol, dir
: MMDirectory, context
: MMContext, loc
: Location)
162 _full_name
= dir
.full_name_for
(name
)
166 # Register that a module is imported with a visibility
170 fun add_super_module
(m
: MMModule, visibility_level
: Int)
172 _explicit_imported_modules
.add
(m
)
173 if visibility_level
== 0 then
174 _intrude_modules
.add
(m
)
175 _intrude_modules
.add_all
(m
._intrude_modules
)
176 _public_modules
.add_all
(m
._public_modules
)
177 _private_modules
.add_all
(m
._private_modules
)
178 else if visibility_level
== 1 then
179 _public_modules
.add
(m
)
180 _public_modules
.add_all
(m
._intrude_modules
)
181 _public_modules
.add_all
(m
._public_modules
)
183 _private_modules
.add
(m
)
184 _private_modules
.add_all
(m
._intrude_modules
)
185 _private_modules
.add_all
(m
._public_modules
)
190 # Return the visibiliy level of a super-module
191 # 3 -> self or intruded => see all
192 # 2 -> public => see public and protected
193 # 1 -> private => see public and protected
194 # 0 -> nothing => see nothing
195 fun visibility_for
(m
: MMModule): Int
197 if m
== self or _intrude_modules
.has
(m
) then
199 else if _public_modules
.has
(m
) then
201 else if _private_modules
.has
(m
) then
209 # Get the local class associated with a global class
210 fun [](c
: MMGlobalClass): MMLocalClass
212 return _local_class_by_global
[c
]
215 # Get a local class by its name
216 fun class_by_name
(n
: Symbol): MMLocalClass
218 return self[_global_class_by_name
[n
]]
221 # Is there a global class with this name
222 fun has_global_class_named
(n
: Symbol): Bool
224 return _global_class_by_name
.has_key
(n
)
227 # Get a global class by its name.
228 # Return null if not class match this name
229 fun global_class_named
(n
: Symbol): MMGlobalClass
231 return _global_class_by_name
[n
]
234 redef fun to_s
do return name
.to_s
236 # Assign super_classes for a local class
237 fun set_supers_class
(c
: MMLocalClass, supers
: Array[MMLocalClass])
239 var tab
= _class_specialization_hierarchy
.select_smallests
(supers
)
240 c
._cshe
= _class_specialization_hierarchy
.add
(c
,tab
)
241 var tab_all
= c
.crhe
.direct_greaters
.to_a
243 context
.add_local_class
(c
,tab_all
)
246 # Register a local class and its global class in the module
247 private fun register_global_class
(c
: MMLocalClass)
249 _local_class_by_global
[c
.global
] = c
254 # The introducing local class
255 readable var _intro
: MMLocalClass
257 # Class refinement hierarchy
258 # It is not the real hierarchy since non concrete classes can only be leaves
259 readable var _class_refinement_hierarchy
: PartialOrder[MMLocalClass] = new PartialOrder[MMLocalClass]
261 # Create a new global class introduced with a given local class
262 init(c
: MMLocalClass)
265 c
.context
.add_global_class
(self)
268 # The name of the global class
274 # The module that introduces the global class
285 # register a new Local class to local class hierarchy (set the crhe value)
286 private fun register_local_class
(c
: MMLocalClass)
288 var sup
= new Array[MMLocalClass]
289 for s
in class_refinement_hierarchy
do
290 if c
.module.mhe
< s
.module and s
isa MMConcreteClass then
294 c
._crhe
= _class_refinement_hierarchy
.add
(c
, sup
)
297 # Is the global class an interface?
298 readable writable var _is_interface
: Bool = false
300 # Is the global class an abstract class?
301 readable writable var _is_abstract
: Bool = false
303 # Is the global class a universal class?
304 readable writable var _is_universal
: Bool = false
306 # Visibility of the global class
309 readable writable var _visibility_level
: Int = 1 # FIXME: why this should be defined ?
311 # Is the global class a mixin class?
312 # A mixin class inherits all constructors from a superclass
315 return _mixin_of
!= self
318 # Indicate the superclass the class is a mixin of.
319 # If mixin_of == self then the class is not a mixin
320 # Is two classes have the same mixin_of value, then they both belong to the same mixing group
321 readable writable var _mixin_of
: MMGlobalClass = self
325 # Local classes are classes defined, refined or imported in a module
327 # The name of the local class
328 readable var _name
: Symbol
330 # Arity of the local class (if generic)
331 # FIXME: How to move this into the generic module in a sane way?
332 readable var _arity
: Int
334 # The module of the local class
335 readable var _module
: MMModule
337 # The global class of the local class
338 fun global
: MMGlobalClass do return _global
.as(not null)
339 var _global
: nullable MMGlobalClass
341 # The local class refinement hierarchy element
342 fun crhe
: PartialOrderElement[MMLocalClass] do return _crhe
.as(not null)
343 var _crhe
: nullable PartialOrderElement[MMLocalClass]
345 # The local class specialization hierarchy element
346 fun cshe
: PartialOrderElement[MMLocalClass] do return _cshe
.as(not null)
347 var _cshe
: nullable PartialOrderElement[MMLocalClass]
349 # The local class full hierarchy element
350 fun che
: PartialOrderElement[MMLocalClass] do return _che
.as(not null)
351 var _che
: nullable PartialOrderElement[MMLocalClass]
353 # Association between local properties and global properties
354 var _local_property_by_global
: Map[MMGlobalProperty, MMLocalProperty] = new HashMap[MMGlobalProperty, MMLocalProperty]
356 # All known global properties
357 readable var _global_properties
: Set[MMGlobalProperty] = new HashSet[MMGlobalProperty]
359 # All locally defined local properties
360 readable var _local_local_properties
: Set[MMLocalProperty] = new HashSet[MMLocalProperty]
362 # Dictionnary of global properties
363 var _properties_by_name
: Map[Symbol, Array[MMGlobalProperty]] = new HashMap[Symbol, Array[MMGlobalProperty]]
365 # Create a new class with a given name and arity
366 protected init(mod
: MMModule, name
: Symbol, arity
: Int)
371 mod
._local_classes
.add
(self)
374 # The corresponding local class in another module
375 fun for_module
(m
: MMModule): MMLocalClass
380 # Introduce a new global class to a new global one and register to hierarchy with no refine
383 var g
= new MMGlobalClass(self)
384 _module
._global_classes
.add
(g
)
385 _module
._global_class_by_name
[name
] = g
389 # Associate this local class to a global one and register to hierarchy
390 # the global class for this class
391 # refined classes for this class
392 fun set_global
(g
: MMGlobalClass)
395 _global
.register_local_class
(self)
396 _module
.register_global_class
(self)
399 # Is there a global propery with a given name
400 # TODO: Will disapear when qualified names will be available
401 fun has_global_property_by_name
(n
: Symbol): Bool
403 return _properties_by_name
.has_key
(n
) and _properties_by_name
[n
].length
== 1
406 # Get a global property by its name
407 # TODO: Will disapear when qualified names will be available
408 fun get_property_by_name
(n
: Symbol): MMGlobalProperty
410 if not has_global_property_by_name
(n
) then abort
411 var props
= _properties_by_name
[n
]
415 # Get a attribute by its name
416 # TODO: Will disapear when qualified names will be available
417 fun attribute
(a
: Symbol): MMGlobalProperty
419 return get_property_by_name
(a
)
422 # Get a method by its name
423 # TODO: Will disapear when qualified names will be available
424 fun method
(na
: Symbol): MMGlobalProperty
426 return _properties_by_name
[na
].first
429 # Select a method from its name
430 # TODO: Will disapear when qualified names will be available
431 fun select_method
(name
: Symbol): MMMethod
433 var gp
= method
(name
)
435 assert res
isa MMMethod
439 # Select an attribute from its name
440 # TODO: Will disapear when qualified names will be available
441 fun select_attribute
(name
: Symbol): MMAttribute
443 var gp
= attribute
(name
)
445 assert res
isa MMAttribute
449 # Look in super-classes (by specialization) and return properties with name
450 # Beware, global property of results is not intended to be the same
451 fun super_methods_named
(n
: Symbol): Array[MMLocalProperty]
453 var classes
= new Array[MMLocalClass]
454 for c
in cshe
.greaters
do
455 if c
.has_global_property_by_name
(n
) then classes
.add
(c
)
457 classes
= cshe
.order
.select_smallests
(classes
)
458 var res
= new Array[MMLocalProperty]
461 #print "found {c[g].full_name}"
467 # Register a local property and associate it with its global property
468 fun register_local_property
(p
: MMLocalProperty)
470 _local_property_by_global
[p
.global
] = p
471 if p
.local_class
== self then
472 _local_local_properties
.add
(p
)
476 # Register a global property and associate it with its name
477 fun register_global_property
(glob
: MMGlobalProperty)
479 var prop
= glob
.intro
481 if _properties_by_name
.has_key
(name
) then
482 _properties_by_name
[name
].add
(glob
)
484 _properties_by_name
[name
] = [glob
]
486 _global_properties
.add
(glob
)
487 register_local_property
(prop
)
490 # Does the global property belong to the class?
491 fun has_global_property
(glob
: MMGlobalProperty): Bool
493 return _global_properties
.has
(glob
)
496 # Get a local proprty by its global property
497 fun [](glob
: MMGlobalProperty): MMLocalProperty
499 return _local_property_by_global
[glob
]
502 # The current MMContext
503 fun context
: MMContext do return module.context
510 # Comparaison in a total order that superset the class refinement and the class specialisation relations
511 fun total_order_compare
(b
: MMLocalClass): Int
516 else if a
.module.mhe
< b
.module then
518 else if b
.module.mhe
< a
.module then
528 return b
.name
.to_s
<=> a
.name
.to_s
533 # A global property gather local properties that correspond to a same message
534 class MMGlobalProperty
535 # The local property for each class that has the global property
537 # The introducing local property
538 readable var _intro
: MMLocalProperty
540 # The local class that introduces the global property
541 fun local_class
: MMLocalClass
543 return intro
.local_class
546 # The property redefinition hierarchy
547 readable var _property_hierarchy
: PartialOrder[MMLocalProperty] = new PartialOrder[MMLocalProperty]
549 # Create a new global property introduced by a local one
550 protected init(p
: MMLocalProperty)
553 add_local_property
(p
, new Array[MMLocalProperty])
556 redef fun to_s
do return intro
.full_name
558 # Register a new local property
559 fun add_local_property
(i
: MMLocalProperty, sup
: Array[MMLocalProperty])
561 i
._prhe
= _property_hierarchy
.add
(i
,sup
)
564 # Is the global property an attribute ?
565 fun is_attribute
: Bool do return intro
isa MMAttribute
567 # Is the global property a method (or a constructor)?
568 fun is_method
: Bool do return intro
isa MMMethod
570 # Is the global property a constructor (thus also a method)?
571 readable writable var _is_init
: Bool = false
573 # Is the global property a constructor for a given class?
574 fun is_init_for
(c
: MMLocalClass): Bool
576 if not is_init
then return false
577 var sc
= intro
.local_class
578 var res
= c
.che
<= sc
and c
.global
.mixin_of
== sc
.global
.mixin_of
582 # Visibility of the property
586 readable writable var _visibility_level
: Int = 1 # FIXME: why this should be defined ?
589 # Local properties are properties defined (explicitely or not) in a local class
590 class MMLocalProperty
591 # The name of the property
592 readable var _name
: Symbol
594 # The local class who own the local property
595 readable var _local_class
: MMLocalClass
597 # The global property where belong the local property
598 var _global
: nullable MMGlobalProperty
600 fun global
: MMGlobalProperty do return _global
.as(not null)
601 fun is_global_set
: Bool do return _global
!= null
603 # Redefinition hierarchy of the local property
604 var _prhe
: nullable PartialOrderElement[MMLocalProperty]
606 fun prhe
: PartialOrderElement[MMLocalProperty] do return _prhe
.as(not null)
608 # The module of the local property
609 fun module: MMModule do return _local_class
.module
611 # Full expanded name with all qualifications
612 fun full_name
: String
614 if _global
== null then
615 return "{local_class.module}::{local_class}::(?::{name})"
616 else if global
.intro
== self then
617 return "{local_class.module}::{local_class}::{name}"
619 return "{local_class.module}::{local_class}::({global.intro.full_name})"
623 # set the global property for this property
624 fun set_global
(g
: MMGlobalProperty)
627 _local_class
.register_local_property
(self)
630 # Introduce a new global property for this local one
633 assert _global
== null
634 var global
= new MMGlobalProperty(self)
636 _local_class
.register_global_property
(global
)
639 redef fun to_s
do return name
.to_s
641 # Is the concrete property contain a `super' in the body?
642 readable writable var _need_super
: Bool = false
644 protected init(n
: Symbol, bc
: MMLocalClass)
651 # Attribute local properties
653 special MMLocalProperty
656 # Method local properties
658 special MMLocalProperty
659 # Is the method defined with intern
660 fun is_intern
: Bool is abstract
662 # Is the method abstract
663 fun is_abstract
: Bool is abstract
666 # Concrete local classes
667 class MMConcreteClass