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 # Full module importation hierarchy including private or nested links.
31 var mmodule_importation_hierarchy
= new POSet[MModule]
33 # Collections of modules grouped by their short names
34 private var mmodules_by_name
= new MultiHashMap[String, MModule]
36 # Return all module named `name`
37 # If such a module does not exist, null is returned (instead of an empty array)
39 # Visibility or modules are not considered
40 fun get_mmodules_by_name
(name
: String): nullable Array[MModule]
42 if mmodules_by_name
.has_key
(name
) then
43 return mmodules_by_name
[name
]
51 # The loaded modules of this group
52 var mmodules
= new Array[MModule]
54 # The default module of a group (if any, and if loaded)
56 # The default module of a group is the one that has the same name.
57 # Return `null` if the group has no default module or if the default
58 # module is not loaded.
59 var default_mmodule
: nullable MModule = null
61 redef fun mdoc_or_fallback
63 if mdoc
!= null then return mdoc
64 if default_mmodule
== null then return null
65 return default_mmodule
.mdoc_or_fallback
69 # A Nit module is usually associated with a Nit source file.
73 # The model considered
74 redef var model
: Model
76 # The group of module in the project if any
77 var mgroup
: nullable MGroup
79 # The short name of the module
80 redef var name
: String
82 # The origin of the definition
83 var location
: Location
86 redef fun to_s
do return self.name
88 # The view of the module in the `model.mmodule_importation_hierarchy`
89 var in_importation
: POSetElement[MModule] is noinit
91 # The canonical name of the module.
93 # It is usually the `name` prefixed by the project's name.
94 # Example: `"project::name"`
96 # If both names are the same (of if the module is project-less), then
97 # the short-name is used alone.
98 redef var full_name
is lazy
do
99 var mgroup
= self.mgroup
100 if mgroup
== null or mgroup
.mproject
.name
== self.name
then
103 return "{mgroup.mproject.name}::{self.name}"
107 # Return the name of the global C identifier associated to `self`.
108 # This name is used to prefix files and other C identifiers associated with `self`.
109 redef var c_name
: String is lazy
do
112 if g
!= null and g
.mproject
.name
!= name
then
113 res
= g
.mproject
.name
.to_cmangle
+ "__" + name
.to_cmangle
115 res
= name
.to_cmangle
121 # Create a new empty module and register it to a model
124 model
.mmodules_by_name
.add_one
(name
, self)
125 model
.mmodules
.add
(self)
126 if mgroup
!= null then
127 mgroup
.mmodules
.add
(self)
128 if mgroup
.name
== name
then
129 assert mgroup
.default_mmodule
== null
130 mgroup
.default_mmodule
= self
133 self.in_importation
= model
.mmodule_importation_hierarchy
.add_node
(self)
136 # Register the imported modules (ie "import some_module")
137 # This function can only invoked once by mmodule.
138 # The visibility must be set with `set_visibility_for`.
139 fun set_imported_mmodules
(imported_mmodules
: Array[MModule])
141 assert unique_invocation
: self.in_importation
.direct_greaters
.is_empty
142 for m
in imported_mmodules
do
143 self.model
.mmodule_importation_hierarchy
.add_edge
(self, m
)
147 private var intrude_mmodules
= new HashSet[MModule]
148 private var public_mmodules
= new HashSet[MModule]
149 private var private_mmodules
= new HashSet[MModule]
151 # Return the visibility level of an imported module `m`
152 fun visibility_for
(m
: MModule): MVisibility
154 if m
== self then return intrude_visibility
155 if self.intrude_mmodules
.has
(m
) then return intrude_visibility
156 if self.public_mmodules
.has
(m
) then return public_visibility
157 if self.private_mmodules
.has
(m
) then return private_visibility
158 return none_visibility
161 # Set the visibility of an imported module
162 # REQUIRE: the visibility of the modules imported by `m` are already set for `m`
163 fun set_visibility_for
(m
: MModule, v
: MVisibility)
165 if v
== intrude_visibility
then
166 self.intrude_mmodules
.add
(m
)
167 self.intrude_mmodules
.add_all
(m
.intrude_mmodules
)
168 self.public_mmodules
.add_all
(m
.public_mmodules
)
169 self.private_mmodules
.add_all
(m
.private_mmodules
)
170 else if v
== public_visibility
then
171 self.public_mmodules
.add
(m
)
172 self.public_mmodules
.add_all
(m
.intrude_mmodules
)
173 self.public_mmodules
.add_all
(m
.public_mmodules
)
174 else if v
== private_visibility
then
175 self.private_mmodules
.add
(m
)
176 self.private_mmodules
.add_all
(m
.intrude_mmodules
)
177 self.private_mmodules
.add_all
(m
.public_mmodules
)
179 print
"{self} visibility for {m} = {v}"
180 abort # invalid visibility
184 # Return true if a class or a property introduced in `intro_mmodule` with a visibility of `visibility` is visible in self.
185 fun is_visible
(intro_mmodule
: MModule, visibility
: MVisibility): Bool
187 var v
= visibility_for
(intro_mmodule
)
188 if v
== intrude_visibility
then
189 return visibility
>= private_visibility
190 else if v
== public_visibility
then
191 return visibility
> private_visibility
192 else if v
== private_visibility
then
193 return visibility
> private_visibility
194 else if v
== none_visibility
then
201 # Is `self` created for internal purpose?
202 # Fictive modules are instantiated internally but they should not be
203 # exposed to the final user.
204 var is_fictive
: Bool = false is writable
206 redef fun parent_concern
do return mgroup