1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012 Jean Privat <jean@pryen.org>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # modules and module hierarchies in the metamodel
23 private import more_collections
25 # The container class of a Nit object-oriented model.
26 # A model knows modules, classes and properties and can retrieve them.
29 var mmodules
: Array[MModule] = new Array[MModule]
31 # placebo for old module nesting hierarchy.
32 # where mainmodule < mainmodule::nestedmodule
34 # TODO REMOVE, rely on mgroup instead
35 var mmodule_nesting_hierarchy
: POSet[MModule] = new POSet[MModule]
37 # Full module importation hierarchy including private or nested links.
38 var mmodule_importation_hierarchy
: POSet[MModule] = new POSet[MModule]
40 # Collections of modules grouped by their short names
41 private var mmodules_by_name
: MultiHashMap[String, MModule] = new MultiHashMap[String, MModule]
43 # Return all module named `name`
44 # If such a module does not exist, null is returned (instead of an empty array)
46 # Visibility or modules are not considered
47 fun get_mmodules_by_name
(name
: String): nullable Array[MModule]
49 if mmodules_by_name
.has_key
(name
) then
50 return mmodules_by_name
[name
]
58 # The loaded modules of this group
59 var mmodules
= new Array[MModule]
61 # Placebo stuff to find the owner (module with same name)
62 # null is returned if there is no owner, or if it is not loaded yet
63 fun fuzzy_owner
: nullable MModule
65 for m
in mmodules
do if m
.name
== name
then return m
70 # A Nit module is usually associated with a Nit source file.
72 # The model considered
75 # placebo for old module nesting hierarchy
76 # return null if self is not nested (ie. is a top-level module)
78 # TODO REMOVE, rely on mgroup instead
79 var direct_owner
: nullable MModule
81 # The group of module in the project if any
82 var mgroup
: nullable MGroup
84 # The short name of the module
87 # The origin of the definition
88 var location
: Location
91 redef fun to_s
do return self.name
93 # placebo for old module nesting hierarchy
94 # The view of the module in the `model.mmodule_nesting_hierarchy`
96 # TODO REMOVE, rely on mgroup instead
97 var in_nesting
: POSetElement[MModule]
99 # The view of the module in the `model.mmodule_importation_hierarchy`
100 var in_importation
: POSetElement[MModule]
102 # The canonical name of the module
103 # Example: `"project::name"`
104 fun full_name
: String
106 var mgroup
= self.mgroup
107 if mgroup
== null or mgroup
.mproject
.name
== self.name
then
110 return "{mgroup.mproject.name}::{self.name}"
114 # Create a new empty module and register it to a model
115 init(model
: Model, mgroup
: nullable MGroup, name
: String, location
: Location)
119 self.location
= location
120 model
.mmodules_by_name
.add_one
(name
, self)
121 model
.mmodules
.add
(self)
122 self.in_nesting
= model
.mmodule_nesting_hierarchy
.add_node
(self)
124 if mgroup
!= null then
125 mgroup
.mmodules
.add
(self)
126 # placebo for old module nesting hierarchy
127 var direct_owner
= mgroup
.fuzzy_owner
128 if direct_owner
== self then
129 # The module is the new owner of its own group, thus adopt the other modules
130 for m
in mgroup
.mmodules
do
131 if m
== self then continue
132 m
.direct_owner
= self
133 model
.mmodule_nesting_hierarchy
.add_edge
(self, m
)
135 # The potential owner is the the fuzzy_owner of the parent group
136 if mgroup
.parent
!= null then direct_owner
= mgroup
.parent
.fuzzy_owner
138 if direct_owner
!= self and direct_owner
!= null then
139 self.direct_owner
= direct_owner
140 model
.mmodule_nesting_hierarchy
.add_edge
(direct_owner
, self)
143 self.in_importation
= model
.mmodule_importation_hierarchy
.add_node
(self)
146 # Register the imported modules (ie "import some_module")
147 # This function can only invoked once by mmodule.
148 # The visibility must be set with `set_visibility_for`.
149 fun set_imported_mmodules
(imported_mmodules
: Array[MModule])
151 assert unique_invocation
: self.in_importation
.direct_greaters
.is_empty
152 for m
in imported_mmodules
do
153 self.model
.mmodule_importation_hierarchy
.add_edge
(self, m
)
157 private var intrude_mmodules
: HashSet[MModule] = new HashSet[MModule]
158 private var public_mmodules
: HashSet[MModule] = new HashSet[MModule]
159 private var private_mmodules
: HashSet[MModule] = new HashSet[MModule]
161 # Return the visibility level of an imported module `m`
162 fun visibility_for
(m
: MModule): MVisibility
164 if m
== self then return intrude_visibility
165 if self.intrude_mmodules
.has
(m
) then return intrude_visibility
166 if self.public_mmodules
.has
(m
) then return public_visibility
167 if self.private_mmodules
.has
(m
) then return private_visibility
168 return none_visibility
171 # Set the visibility of an imported module
172 # REQUIRE: the visibility of the modules imported by `m` are already set for `m`
173 fun set_visibility_for
(m
: MModule, v
: MVisibility)
175 if v
== intrude_visibility
then
176 self.intrude_mmodules
.add
(m
)
177 self.intrude_mmodules
.add_all
(m
.intrude_mmodules
)
178 self.public_mmodules
.add_all
(m
.public_mmodules
)
179 self.private_mmodules
.add_all
(m
.private_mmodules
)
180 else if v
== public_visibility
then
181 self.public_mmodules
.add
(m
)
182 self.public_mmodules
.add_all
(m
.intrude_mmodules
)
183 self.public_mmodules
.add_all
(m
.public_mmodules
)
184 else if v
== private_visibility
then
185 self.private_mmodules
.add
(m
)
186 self.private_mmodules
.add_all
(m
.intrude_mmodules
)
187 self.private_mmodules
.add_all
(m
.public_mmodules
)
189 print
"{self} visibility for {m} = {v}"
190 abort # invalid visibility
194 # placebo for old module nesting hierarchy
195 fun public_owner
: nullable MModule
197 var mgroup
= self.mgroup
198 if mgroup
== null then return null
199 mgroup
= mgroup
.mproject
.root
200 if mgroup
.mmodules
.is_empty
then return null
201 var res
= mgroup
.fuzzy_owner
202 if res
== self then return null
206 # Return true if a class or a property introduced in `intro_mmodule` with a visibility of `visibility` is visible in self.
207 fun is_visible
(intro_mmodule
: MModule, visibility
: MVisibility): Bool
209 var v
= visibility_for
(intro_mmodule
)
210 if v
== intrude_visibility
then
211 return visibility
>= private_visibility
212 else if v
== public_visibility
then
213 return visibility
> private_visibility
214 else if v
== private_visibility
then
215 return visibility
> private_visibility
216 else if v
== none_visibility
then