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.
26 # A model knows modules, classes and properties and can retrieve them.
28 # However, a model is not a program or a library as it can contains modules
29 # found by the system (including broken ones) but not used.
32 var mmodules
= new Array[MModule]
34 # Full module importation hierarchy including private or nested links.
35 var mmodule_importation_hierarchy
= new POSet[MModule]
37 # Collections of modules grouped by their short names
38 private var mmodules_by_name
= new MultiHashMap[String, MModule]
40 # Return all module named `name`
41 # If such a module does not exist, null is returned (instead of an empty array)
43 # Visibility or modules are not considered
44 fun get_mmodules_by_name
(name
: String): nullable Array[MModule]
46 if mmodules_by_name
.has_key
(name
) then
47 return mmodules_by_name
[name
]
55 # The loaded modules of this group
56 var mmodules
= new Array[MModule]
58 # The default module of a group (if any, and if loaded)
60 # The default module of a group is the one that has the same name.
61 # Return `null` if the group has no default module or if the default
62 # module is not loaded.
63 var default_mmodule
: nullable MModule = null
65 redef fun mdoc_or_fallback
67 if mdoc
!= null then return mdoc
68 if default_mmodule
== null then return null
69 return default_mmodule
.mdoc_or_fallback
73 # A Nit module is usually associated with a Nit source file.
77 # The model considered
78 redef var model
: Model
80 # The group of module in the package if any
81 var mgroup
: nullable MGroup
83 # The package of the module if any
84 # Safe alias for `mgroup.mpackage`
85 fun mpackage
: nullable MPackage
88 if g
== null then return null else return g
.mpackage
91 # The short name of the module
92 redef var name
: String
94 # The origin of the definition
95 var location
: Location
98 redef fun to_s
do return self.name
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.
105 # It is usually the `name` prefixed by the package's name.
106 # Example: `"package::name"`
108 # If both names are the same (of if the module is package-less), then
109 # the short-name is used alone.
110 redef var full_name
is lazy
do
111 var mgroup
= self.mgroup
112 if mgroup
== null or mgroup
.mpackage
.name
== self.name
then
115 return "{mgroup.mpackage.name}::{self.name}"
119 # The namespace used for entities according to their visibility `v`.
121 # Public entities use only the package as a namespace.
122 # Private entities use the `full_name` (i.e. "package::module")
124 # This method is used by entities to implement their `full_name`.
125 fun namespace_for
(v
: MVisibility): String do
126 if v
<= private_visibility
then return full_name
127 var mgroup
= self.mgroup
128 if mgroup
== null then
131 return mgroup
.mpackage
.full_name
135 # Return the name of the global C identifier associated to `self`.
136 # This name is used to prefix files and other C identifiers associated with `self`.
137 redef var c_name
: String is lazy
do
140 if g
!= null and g
.mpackage
.name
!= name
then
141 res
= g
.mpackage
.name
.to_cmangle
+ "__" + name
.to_cmangle
143 res
= name
.to_cmangle
148 # C identifier version of `namespace_for`.
151 # This method is used by entities to implement their `c_name`.
152 fun c_namespace_for
(v
: MVisibility): String do
153 if v
<= private_visibility
then return c_name
154 var mgroup
= self.mgroup
155 if mgroup
== null then
158 return mgroup
.mpackage
.c_name
162 # Create a new empty module and register it to a model
165 model
.mmodules_by_name
.add_one
(name
, self)
166 model
.mmodules
.add
(self)
167 if mgroup
!= null then
168 mgroup
.mmodules
.add
(self)
169 if mgroup
.name
== name
then
170 assert mgroup
.default_mmodule
== null
171 mgroup
.default_mmodule
= self
174 self.in_importation
= model
.mmodule_importation_hierarchy
.add_node
(self)
177 # Register the imported modules (ie "import some_module")
178 # The visibility must be set with `set_visibility_for`.
179 fun set_imported_mmodules
(imported_mmodules
: Array[MModule])
181 for m
in imported_mmodules
do
182 self.model
.mmodule_importation_hierarchy
.add_edge
(self, m
)
186 private var intrude_mmodules
= new HashSet[MModule]
187 private var public_mmodules
= new HashSet[MModule]
188 private var private_mmodules
= new HashSet[MModule]
190 # Return the visibility level of an imported module `m`
191 fun visibility_for
(m
: MModule): MVisibility
193 if m
== self then return intrude_visibility
194 if self.intrude_mmodules
.has
(m
) then return intrude_visibility
195 if self.public_mmodules
.has
(m
) then return public_visibility
196 if self.private_mmodules
.has
(m
) then return private_visibility
197 return none_visibility
200 # Set the visibility of an imported module
201 # REQUIRE: the visibility of the modules imported by `m` are already set for `m`
202 fun set_visibility_for
(m
: MModule, v
: MVisibility)
204 if v
== intrude_visibility
then
205 self.intrude_mmodules
.add
(m
)
206 self.intrude_mmodules
.add_all
(m
.intrude_mmodules
)
207 self.public_mmodules
.add_all
(m
.public_mmodules
)
208 self.private_mmodules
.add_all
(m
.private_mmodules
)
209 else if v
== public_visibility
then
210 self.public_mmodules
.add
(m
)
211 self.public_mmodules
.add_all
(m
.intrude_mmodules
)
212 self.public_mmodules
.add_all
(m
.public_mmodules
)
213 else if v
== private_visibility
then
214 self.private_mmodules
.add
(m
)
215 self.private_mmodules
.add_all
(m
.intrude_mmodules
)
216 self.private_mmodules
.add_all
(m
.public_mmodules
)
218 print
"{self} visibility for {m} = {v}"
219 abort # invalid visibility
223 # Return true if a class or a property introduced in `intro_mmodule` with a visibility of `visibility` is visible in self.
224 fun is_visible
(intro_mmodule
: MModule, visibility
: MVisibility): Bool
226 var v
= visibility_for
(intro_mmodule
)
227 if v
== intrude_visibility
then
228 return visibility
>= private_visibility
229 else if v
== public_visibility
then
230 return visibility
> private_visibility
231 else if v
== private_visibility
then
232 return visibility
> private_visibility
233 else if v
== none_visibility
then
240 # Is `self` created for internal purpose?
241 # Fictive modules are instantiated internally but they should not be
242 # exposed to the final user.
243 var is_fictive
: Bool = false is writable
245 # Is `self` a unit test module used by `nitunit`?
246 var is_test_suite
: Bool = false is writable
248 # Get the first non `is_fictive` module greater than self
249 fun first_real_mmodule
: MModule
252 while mmodule
.is_fictive
do
253 mmodule
= mmodule
.in_importation
.direct_greaters
.first
258 redef fun parent_concern
do return mgroup