loader: improve documentation
[nit.git] / src / model / mmodule.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2012 Jean Privat <jean@pryen.org>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 # modules and module hierarchies in the metamodel
18 module mmodule
19
20 import location
21 import mpackage
22 private import more_collections
23
24 # The container class of a Nit object-oriented model.
25 #
26 # A model knows modules, classes and properties and can retrieve them.
27 #
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.
30 redef class Model
31 # All known modules
32 var mmodules = new Array[MModule]
33
34 # Full module importation hierarchy including private or nested links.
35 var mmodule_importation_hierarchy = new POSet[MModule]
36
37 # Collections of modules grouped by their short names
38 private var mmodules_by_name = new MultiHashMap[String, MModule]
39
40 # Return all module named `name`
41 # If such a module does not exist, null is returned (instead of an empty array)
42 #
43 # Visibility or modules are not considered
44 fun get_mmodules_by_name(name: String): nullable Array[MModule]
45 do
46 if mmodules_by_name.has_key(name) then
47 return mmodules_by_name[name]
48 else
49 return null
50 end
51 end
52 end
53
54 redef class MGroup
55 # The loaded modules of this group
56 var mmodules = new Array[MModule]
57
58 # The default module of a group (if any, and if loaded)
59 #
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
64
65 redef fun mdoc_or_fallback
66 do
67 if mdoc != null then return mdoc
68 if default_mmodule == null then return null
69 return default_mmodule.mdoc_or_fallback
70 end
71 end
72
73 # A Nit module is usually associated with a Nit source file.
74 class MModule
75 super MConcern
76
77 # The model considered
78 redef var model: Model
79
80 # The group of module in the package if any
81 var mgroup: nullable MGroup
82
83 # The package of the module if any
84 # Safe alias for `mgroup.mpackage`
85 fun mpackage: nullable MPackage
86 do
87 var g = mgroup
88 if g == null then return null else return g.mpackage
89 end
90
91 # The short name of the module
92 redef var name: String
93
94 # The origin of the definition
95 var location: Location
96
97 # Alias for `name`
98 redef fun to_s do return self.name
99
100 # The view of the module in the `model.mmodule_importation_hierarchy`
101 var in_importation: POSetElement[MModule] is noinit
102
103 # The canonical name of the module.
104 #
105 # It is usually the `name` prefixed by the package's name.
106 # Example: `"package::name"`
107 #
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
113 return self.name
114 else
115 return "{mgroup.mpackage.name}::{self.name}"
116 end
117 end
118
119 # The namespace used for entities according to their visibility `v`.
120 #
121 # Public entities use only the package as a namespace.
122 # Private entities use the `full_name` (i.e. "package::module")
123 #
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
129 return full_name
130 else
131 return mgroup.mpackage.full_name
132 end
133 end
134
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
138 var g = mgroup
139 var res
140 if g != null and g.mpackage.name != name then
141 res = g.mpackage.name.to_cmangle + "__" + name.to_cmangle
142 else
143 res = name.to_cmangle
144 end
145 return res
146 end
147
148 # C identifier version of `namespace_for`.
149 # See `c_name`
150 #
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
156 return c_name
157 else
158 return mgroup.mpackage.c_name
159 end
160 end
161
162 # Create a new empty module and register it to a model
163 init
164 do
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
172 end
173 end
174 self.in_importation = model.mmodule_importation_hierarchy.add_node(self)
175 end
176
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])
180 do
181 for m in imported_mmodules do
182 self.model.mmodule_importation_hierarchy.add_edge(self, m)
183 end
184 end
185
186 private var intrude_mmodules = new HashSet[MModule]
187 private var public_mmodules = new HashSet[MModule]
188 private var private_mmodules = new HashSet[MModule]
189
190 # Return the visibility level of an imported module `m`
191 fun visibility_for(m: MModule): MVisibility
192 do
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
198 end
199
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)
203 do
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)
217 else
218 print "{self} visibility for {m} = {v}"
219 abort # invalid visibility
220 end
221 end
222
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
225 do
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
234 return false
235 else
236 abort
237 end
238 end
239
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
244
245 # Is `self` a unit test module used by `nitunit`?
246 var is_test_suite: Bool = false is writable
247
248 # Get the first non `is_fictive` module greater than self
249 fun first_real_mmodule: MModule
250 do
251 var mmodule = self
252 while mmodule.is_fictive do
253 mmodule = mmodule.in_importation.direct_greaters.first
254 end
255 return mmodule
256 end
257
258 redef fun parent_concern do return mgroup
259 end