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
22 private import more_collections
24 # The container class of a Nit object-oriented model.
25 # A model knows modules, classes and properties and can retrieve them.
28 var mmodules
= new Array[MModule]
30 # placebo for old module nesting hierarchy.
31 # where mainmodule < mainmodule::nestedmodule
33 # TODO REMOVE, rely on mgroup instead
34 var mmodule_nesting_hierarchy
= new POSet[MModule]
36 # Full module importation hierarchy including private or nested links.
37 var mmodule_importation_hierarchy
= new POSet[MModule]
39 # Collections of modules grouped by their short names
40 private var mmodules_by_name
= new MultiHashMap[String, MModule]
42 # Return all module named `name`
43 # If such a module does not exist, null is returned (instead of an empty array)
45 # Visibility or modules are not considered
46 fun get_mmodules_by_name
(name
: String): nullable Array[MModule]
48 if mmodules_by_name
.has_key
(name
) then
49 return mmodules_by_name
[name
]
57 # The loaded modules of this group
58 var mmodules
= new Array[MModule]
60 # The default module of a group (if any, and if loaded)
62 # The default module of a group is the one that has the same name.
63 # Return `null` if the group has no default module or if the default
64 # module is not loaded.
65 var default_mmodule
: nullable MModule = null
67 redef fun mdoc_or_fallback
69 if mdoc
!= null then return mdoc
70 if default_mmodule
== null then return null
71 return default_mmodule
.mdoc_or_fallback
75 # A Nit module is usually associated with a Nit source file.
79 # The model considered
80 redef var model
: Model
82 # The group of module in the project if any
83 var mgroup
: nullable MGroup
85 # The short name of the module
86 redef var name
: String
88 # The origin of the definition
89 var location
: Location
92 redef fun to_s
do return self.name
94 # placebo for old module nesting hierarchy
95 # The view of the module in the `model.mmodule_nesting_hierarchy`
97 # TODO REMOVE, rely on mgroup instead
98 var in_nesting
: POSetElement[MModule] is noinit
100 # The view of the module in the `model.mmodule_importation_hierarchy`
101 var in_importation
: POSetElement[MModule] is noinit
103 # The canonical name of the module
104 # Example: `"project::name"`
105 fun full_name
: String
107 var mgroup
= self.mgroup
108 if mgroup
== null or mgroup
.mproject
.name
== self.name
then
111 return "{mgroup.mproject.name}::{self.name}"
115 # Create a new empty module and register it to a model
118 model
.mmodules_by_name
.add_one
(name
, self)
119 model
.mmodules
.add
(self)
120 self.in_nesting
= model
.mmodule_nesting_hierarchy
.add_node
(self)
121 if mgroup
!= null then
122 mgroup
.mmodules
.add
(self)
123 if mgroup
.name
== name
then
124 assert mgroup
.default_mmodule
== null
125 mgroup
.default_mmodule
= self
127 # placebo for old module nesting hierarchy
128 var direct_owner
= mgroup
.default_mmodule
129 if direct_owner
== self then
130 # The module is the new owner of its own group, thus adopt the other modules
131 for m
in mgroup
.mmodules
do
132 if m
== self then continue
133 model
.mmodule_nesting_hierarchy
.add_edge
(self, m
)
135 # The potential owner is the default_mmodule of the parent group
136 if mgroup
.parent
!= null then direct_owner
= mgroup
.parent
.default_mmodule
138 if direct_owner
!= self and direct_owner
!= null then
139 model
.mmodule_nesting_hierarchy
.add_edge
(direct_owner
, self)
142 self.in_importation
= model
.mmodule_importation_hierarchy
.add_node
(self)
145 # Register the imported modules (ie "import some_module")
146 # This function can only invoked once by mmodule.
147 # The visibility must be set with `set_visibility_for`.
148 fun set_imported_mmodules
(imported_mmodules
: Array[MModule])
150 assert unique_invocation
: self.in_importation
.direct_greaters
.is_empty
151 for m
in imported_mmodules
do
152 self.model
.mmodule_importation_hierarchy
.add_edge
(self, m
)
156 private var intrude_mmodules
= new HashSet[MModule]
157 private var public_mmodules
= new HashSet[MModule]
158 private var private_mmodules
= new HashSet[MModule]
160 # Return the visibility level of an imported module `m`
161 fun visibility_for
(m
: MModule): MVisibility
163 if m
== self then return intrude_visibility
164 if self.intrude_mmodules
.has
(m
) then return intrude_visibility
165 if self.public_mmodules
.has
(m
) then return public_visibility
166 if self.private_mmodules
.has
(m
) then return private_visibility
167 return none_visibility
170 # Set the visibility of an imported module
171 # REQUIRE: the visibility of the modules imported by `m` are already set for `m`
172 fun set_visibility_for
(m
: MModule, v
: MVisibility)
174 if v
== intrude_visibility
then
175 self.intrude_mmodules
.add
(m
)
176 self.intrude_mmodules
.add_all
(m
.intrude_mmodules
)
177 self.public_mmodules
.add_all
(m
.public_mmodules
)
178 self.private_mmodules
.add_all
(m
.private_mmodules
)
179 else if v
== public_visibility
then
180 self.public_mmodules
.add
(m
)
181 self.public_mmodules
.add_all
(m
.intrude_mmodules
)
182 self.public_mmodules
.add_all
(m
.public_mmodules
)
183 else if v
== private_visibility
then
184 self.private_mmodules
.add
(m
)
185 self.private_mmodules
.add_all
(m
.intrude_mmodules
)
186 self.private_mmodules
.add_all
(m
.public_mmodules
)
188 print
"{self} visibility for {m} = {v}"
189 abort # invalid visibility
193 # Return true if a class or a property introduced in `intro_mmodule` with a visibility of `visibility` is visible in self.
194 fun is_visible
(intro_mmodule
: MModule, visibility
: MVisibility): Bool
196 var v
= visibility_for
(intro_mmodule
)
197 if v
== intrude_visibility
then
198 return visibility
>= private_visibility
199 else if v
== public_visibility
then
200 return visibility
> private_visibility
201 else if v
== private_visibility
then
202 return visibility
> private_visibility
203 else if v
== none_visibility
then
210 # Is `self` created for internal purpose?
211 # Fictive modules are instantiated internally but they should not be
212 # exposed to the final user.
213 var is_fictive
: Bool = false is writable
215 redef fun parent_concern
do return mgroup