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
23 # The main singleton which knows everything
28 # The module dependence hierarchy
29 readable attr _module_hierarchy
: PartialOrder[MMModule] = new PartialOrder[MMModule]
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]
35 # All known global classes
36 attr _global_classes
: Array[MMGlobalClass] = new Array[MMGlobalClass]
39 readable attr _modules
: Array[MMModule] = new Array[MMModule]
41 # Register a new module with the modules it depends on
42 meth add_module
(module: MMModule, supers
: Array[MMModule])
45 _module_hierarchy
.add
(module, _module_hierarchy
.select_smallests
(supers
))
46 assert module.name
!= null
48 module._mhe
= _module_hierarchy
[module]
51 # Register a global class
52 private meth add_global_class
(c
: MMGlobalClass) do _global_classes
.add
(c
)
54 # Register a local class
55 meth add_local_class
(c
: MMLocalClass, sup
: Array[MMLocalClass])
58 var csup
= new Array[MMLocalClass]
59 var csups
= new Array[String]
61 if s
isa MMConcreteClass then
64 for ss
in s
.che
.direct_greaters
do
65 if csup
.has
(ss
) then continue
70 var che
= _class_hierarchy
.add
(c
, csup
)
75 # Directory of modules
77 # Full name of the directory
78 readable attr _name
: Symbol
81 readable attr _path
: String
85 readable attr _parent
: MMDirectory
87 # The module that introduces the directory if any
88 readable writable attr _owner
: MMModule
90 # Known modules in the directory
91 readable attr _modules
: Map[Symbol, MMModule] = new HashMap[Symbol, MMModule]
93 # Register a new module
94 meth add_module
(module: MMModule)
96 assert not _modules
.has_key
(module.name
)
97 _modules
[module.name
] = module
100 # Directory hierarchy element
101 readable attr _dhe
: PartialOrderElement[MMDirectory]
103 init(name
: Symbol, path
: String, parent
: MMDirectory) do
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
115 # A module is a NIT package
118 readable attr _context
: MMContext
120 # Short name of the module
121 readable attr _name
: Symbol
123 # Full name of the module
124 readable attr _full_name
: Symbol
126 # The directory of the module
127 readable attr _directory
: MMDirectory
129 # Module dependence hierarchy element
130 readable attr _mhe
: PartialOrderElement[MMModule]
132 # All global classes of the module (defined and imported)
133 readable attr _global_classes
: Array[MMGlobalClass] = new Array[MMGlobalClass]
135 # All local classes of the module (defined and imported)
136 readable attr _local_classes
: Array[MMLocalClass] = new Array[MMLocalClass]
138 # Class specialization hierarchy of the module.
139 readable attr _class_specialization_hierarchy
: PartialOrder[MMLocalClass] = new PartialOrder[MMLocalClass]
141 # Modules intruded (directly or not)
142 attr _intrude_modules
: Set[MMModule] = new HashSet[MMModule]
144 # Module publicly imported (directly or not)
145 attr _public_modules
: Set[MMModule] = new HashSet[MMModule]
147 # Module privately imported (directly or not)
148 attr _private_modules
: Set[MMModule] = new HashSet[MMModule]
150 # Explicit imported modules
151 readable attr _explicit_imported_modules
: Set[MMModule] = new HashSet[MMModule]
153 # Association between local class and global classes
154 attr _local_class_by_global
: Map[MMGlobalClass, MMLocalClass] = new HashMap[MMGlobalClass, MMLocalClass]
156 # Dictionary of global classes
157 attr _global_class_by_name
: Map[Symbol, MMGlobalClass] = new HashMap[Symbol, MMGlobalClass]
159 protected init(name
: Symbol, dir
: MMDirectory, context
: MMContext)
168 _full_name
= dir
.full_name_for
(name
)
172 # Register that a module is imported with a visibility
176 meth add_super_module
(m
: MMModule, visibility_level
: Int)
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
)
189 _private_modules
.add
(m
)
190 _private_modules
.add_all
(m
._intrude_modules
)
191 _private_modules
.add_all
(m
._public_modules
)
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
203 if m
== self or _intrude_modules
.has
(m
) then
205 else if _public_modules
.has
(m
) then
207 else if _private_modules
.has
(m
) then
215 # Get the local class associated with a global class
216 meth
[](c
: MMGlobalClass): MMLocalClass
218 assert _local_class_by_global
!= null
220 if _local_class_by_global
.has_key
(c
) then
221 return _local_class_by_global
[c
]
227 # Register a local class to the module
228 meth add_local_class
(c
: MMLocalClass)
231 _local_classes
.add
(c
)
234 # Get a local class by its name
235 meth class_by_name
(n
: Symbol): MMLocalClass
237 return self[_global_class_by_name
[n
]]
240 # Is there a global class with this name
241 meth has_global_class_named
(n
: Symbol): Bool
243 return _global_class_by_name
.has_key
(n
)
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
250 if _global_class_by_name
.has_key
(n
) then
251 return _global_class_by_name
[n
]
257 redef meth to_s
do return name
.to_s
259 # Assign super_classes for a local class
260 meth set_supers_class
(c
: MMLocalClass, supers
: Array[MMLocalClass])
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
267 context
.add_local_class
(c
,tab_all
)
270 # Register a local class and its global class in the module
271 private meth register_global_class
(c
: MMLocalClass)
273 assert c
.global
!= null
274 _local_class_by_global
[c
.global
] = c
279 # The introducing local class
280 readable attr _intro
: MMLocalClass
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]
286 # Create a new global class introduced with a given local class
287 init(c
: MMLocalClass)
290 c
.context
.add_global_class
(self)
293 # The name of the global class
299 # The module that introduces the global class
300 meth
module: MMModule
310 # register a new Local class to local class hierarchy (set the crhe value)
311 private meth register_local_class
(c
: MMLocalClass)
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
321 c
._crhe
= _class_refinement_hierarchy
.add
(c
, sup
)
324 # Is the global class an interface?
325 readable writable attr _is_interface
: Bool = false
327 # Is the global class an abstract class?
328 readable writable attr _is_abstract
: Bool = false
330 # Is the global class a universal class?
331 readable writable attr _is_universal
: Bool = false
333 # Visibility of the global class
336 readable writable attr _visibility_level
: Int = 1 # FIXME: why this should be defined ?
338 # Is the global class a mixin class?
339 # A mixin class inherits all constructors from a superclass
342 return _mixin_of
!= self
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
352 # Local classes are classes defined, refined or imported in a module
354 # The name of the local class
355 readable attr _name
: Symbol
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
361 # The module of the local class
362 readable attr _module
: MMModule
364 # The global class of the local class
365 readable attr _global
: MMGlobalClass
367 # The local class refinement hierarchy element
368 readable attr _crhe
: PartialOrderElement[MMLocalClass]
370 # The local class specialization hierarchy element
371 readable attr _cshe
: PartialOrderElement[MMLocalClass]
373 # The local class full hierarchy element
374 readable attr _che
: PartialOrderElement[MMLocalClass]
376 # Association between local properties and global properties
377 readable attr _local_property_by_global
: Map[MMGlobalProperty, MMLocalProperty]
379 # All known global properties
380 readable attr _global_properties
: Set[MMGlobalProperty]
382 # Dictionnary of global properties
383 readable attr _properties_by_name
: Map[Symbol, Array[MMGlobalProperty]]
385 # Create a new class with a given name and arity
386 protected init(name
: Symbol, arity
: Int)
392 # The corresponding local class in another module
393 meth for_module
(m
: MMModule): MMLocalClass
398 # Introduce a new global class to a new global one and register to hierarchy with no refine
401 var g
= new MMGlobalClass(self)
402 _module
._global_classes
.add
(g
)
403 _module
._global_class_by_name
[name
] = g
407 # Associate this local class to a global one and register to hierarchy
408 # the global class for this class
409 # refined classes for this class
410 meth set_global
(g
: MMGlobalClass)
414 _global
.register_local_class
(self)
415 _module
.register_global_class
(self)
418 # Is there a global propery with a given name
419 # TODO: Will disapear when qualified names will be available
420 meth has_global_property_by_name
(n
: Symbol): Bool
422 return _properties_by_name
.has_key
(n
)
425 # Get a global property by its name
426 # TODO: Will disapear when qualified names will be available
427 meth get_property_by_name
(n
: Symbol): MMGlobalProperty
429 if not has_global_property_by_name
(n
) then return null
430 var props
= _properties_by_name
[n
]
431 if props
.length
> 1 then return null
435 # Get a attribute by its name
436 # TODO: Will disapear when qualified names will be available
437 meth attribute
(a
: Symbol): MMGlobalProperty
439 return get_property_by_name
(a
)
442 # Get a method by its name
443 # TODO: Will disapear when qualified names will be available
444 meth method
(na
: Symbol): MMGlobalProperty
446 assert _properties_by_name
!= null
447 if _properties_by_name
.has_key
(na
) then
448 return _properties_by_name
[na
].first
454 # Select a method from its name
455 # TODO: Will disapear when qualified names will be available
456 meth select_method
(name
: Symbol): MMMethod
459 var gp
= method
(name
)
460 if gp
== null then return null
462 assert res
isa MMMethod
466 # Select an attribute from its name
467 # TODO: Will disapear when qualified names will be available
468 meth select_attribute
(name
: Symbol): MMAttribute
471 var gp
= attribute
(name
)
472 if gp
== null then return null
474 assert res
isa MMAttribute
478 # Look in super-classes (by specialization) and return properties with name
479 # Beware, global property of results is not intended to be the same
480 meth super_methods_named
(n
: Symbol): Array[MMLocalProperty]
482 var classes
= new Array[MMLocalClass]
483 for c
in cshe
.greaters
do
485 if g
== null then continue
488 classes
= cshe
.order
.select_smallests
(classes
)
489 var res
= new Array[MMLocalProperty]
492 #print "found {c[g].full_name}"
498 # Register a local property and associate it with its global property
499 meth register_local_property
(p
: MMLocalProperty)
501 assert p
.global
!= null
503 if not _local_property_by_global
.has_key
(p
.global
) then
504 _local_property_by_global
[p
.global
] = p
508 # Register a global property and associate it with its name
509 meth register_global_property
(glob
: MMGlobalProperty)
511 var prop
= glob
.intro
513 if _properties_by_name
.has_key
(name
) then
514 _properties_by_name
[name
].add
(glob
)
516 _properties_by_name
[name
] = [glob
]
518 _global_properties
.add
(glob
)
519 register_local_property
(prop
)
522 # Get a local proprty by its global property
523 meth
[](glob
: MMGlobalProperty): MMLocalProperty
525 assert _local_property_by_global
!= null
527 if _local_property_by_global
.has_key
(glob
) then
528 return _local_property_by_global
[glob
]
533 # The current MMContext
534 meth context
: MMContext do return module.context
542 # A global property gather local properties that correspond to a same message
543 class MMGlobalProperty
544 # The local property for each class that has the global property
546 # The introducing local property
547 readable attr _intro
: MMLocalProperty
549 # The local class that introduces the global property
550 meth local_class
: MMLocalClass
552 return intro
.local_class
555 # The property redefinition hierarchy
556 readable attr _property_hierarchy
: PartialOrder[MMLocalProperty] = new PartialOrder[MMLocalProperty]
558 # Create a new global property introduced by a local one
559 protected init(p
: MMLocalProperty)
563 _property_hierarchy
= new PartialOrder[MMLocalProperty]
566 add_local_property
(p
, new Array[MMLocalProperty])
569 redef meth to_s
do return intro
.full_name
571 # Register a new local property
572 meth add_local_property
(i
: MMLocalProperty, sup
: Array[MMLocalProperty])
576 i
._prhe
= _property_hierarchy
.add
(i
,sup
)
579 # Is the global property an attribute ?
580 meth is_attribute
: Bool do return intro
isa MMAttribute
582 # Is the global property a method (or a constructor)?
583 meth is_method
: Bool do return intro
isa MMMethod
585 # Is the global property a constructor (thus also a method)?
586 readable writable attr _is_init
: Bool
588 # Is the global property a constructor for a given class?
589 meth is_init_for
(c
: MMLocalClass): Bool
591 if not is_init
then return false
592 var sc
= intro
.local_class
593 var res
= c
.che
<= sc
and c
.global
.mixin_of
== sc
.global
.mixin_of
597 # Visibility of the property
601 readable writable attr _visibility_level
: Int = 1 # FIXME: why this should be defined ?
604 # Local properties are properties defined (explicitely or not) in a local class
605 class MMLocalProperty
606 # The name of the property
607 readable attr _name
: Symbol
609 # The local class who own the local property
610 readable attr _local_class
: MMLocalClass
612 # The global property where belong the local property
613 readable attr _global
: MMGlobalProperty
615 # Redefinition hierarchy of the local property
616 readable attr _prhe
: PartialOrderElement[MMLocalProperty]
618 # The module of the local property
619 meth
module: MMModule do return _local_class
.module
621 # Full expanded name with all qualifications
622 meth full_name
: String
624 if global
== null then
625 return "{local_class.module}::{local_class}::(?::{name})"
626 else if global
.intro
== self then
627 return "{local_class.module}::{local_class}::{name}"
629 return "{local_class.module}::{local_class}::({global.intro.full_name})"
633 # set the global property for this property
634 meth set_global
(g
: MMGlobalProperty)
638 _local_class
.register_local_property
(self)
641 # Introduce a new global property for this local one
644 assert _global
== null
645 _global
= new MMGlobalProperty(self)
646 _local_class
.register_global_property
(_global
)
649 redef meth to_s
do return name
.to_s
651 # Is the concrete property contain a `super' in the body?
652 readable writable attr _need_super
: Bool = false
654 protected init(n
: Symbol, bc
: MMLocalClass)
661 # Attribute local properties
663 special MMLocalProperty
666 # Method local properties
668 special MMLocalProperty
671 # Concrete local classes
672 class MMConcreteClass