8927f8cdd34fe8be602ee7df3cd6bfee19253913
[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 var default_mmodule = self.default_mmodule
69 if default_mmodule == null then return null
70 return default_mmodule.mdoc_or_fallback
71 end
72 end
73
74 # A Nit module is usually associated with a Nit source file.
75 class MModule
76 super MConcern
77
78 # The model considered
79 redef var model: Model
80
81 # The group of module in the package if any
82 var mgroup: nullable MGroup
83
84 # The path of the module source, if any
85 var filepath: nullable String = null is writable
86
87 # The package of the module if any
88 # Safe alias for `mgroup.mpackage`
89 fun mpackage: nullable MPackage
90 do
91 var g = mgroup
92 if g == null then return null else return g.mpackage
93 end
94
95 # The short name of the module
96 redef var name: String
97
98 # The origin of the definition
99 var location: Location is writable
100
101 # Alias for `name`
102 redef fun to_s do return self.name
103
104 # The view of the module in the `model.mmodule_importation_hierarchy`
105 var in_importation: POSetElement[MModule] is noinit
106
107 # The canonical name of the module.
108 #
109 # It is usually the `name` prefixed by the package's name.
110 # Example: `"package::name"`
111 #
112 # Default modules use a doubled name to distinguish them from the package name.
113 # E.g.: `"core::core"`
114 #
115 # If the module is package-less, then the short-name is used alone.
116 redef var full_name is lazy do
117 var mgroup = self.mgroup
118 if mgroup == null then
119 return self.name
120 else
121 return "{mgroup.mpackage.name}::{self.name}"
122 end
123 end
124
125 # The namespace used for entities according to their visibility `v`.
126 #
127 # Public entities use only the package as a namespace.
128 # Private entities use the `full_name` (i.e. "package::module")
129 #
130 # This method is used by entities to implement their `full_name`.
131 fun namespace_for(v: MVisibility): String do
132 if v <= private_visibility then return full_name
133 var mgroup = self.mgroup
134 if mgroup == null then
135 return full_name
136 else
137 return mgroup.mpackage.full_name
138 end
139 end
140
141 # Return the name of the global C identifier associated to `self`.
142 # This name is used to prefix files and other C identifiers associated with `self`.
143 redef var c_name: String is lazy do
144 var g = mgroup
145 var res
146 if g != null and g.mpackage.name != name then
147 res = g.mpackage.name.to_cmangle + "__" + name.to_cmangle
148 else
149 res = name.to_cmangle
150 end
151 return res
152 end
153
154 # C identifier version of `namespace_for`.
155 # See `c_name`
156 #
157 # This method is used by entities to implement their `c_name`.
158 fun c_namespace_for(v: MVisibility): String do
159 if v <= private_visibility then return c_name
160 var mgroup = self.mgroup
161 if mgroup == null then
162 return c_name
163 else
164 return mgroup.mpackage.c_name
165 end
166 end
167
168 # Create a new empty module and register it to a model
169 init
170 do
171 model.mmodules_by_name.add_one(name, self)
172 model.mmodules.add(self)
173 var mgroup = self.mgroup
174 if mgroup != null then
175 mgroup.mmodules.add(self)
176 if mgroup.name == name then
177 assert mgroup.default_mmodule == null
178 mgroup.default_mmodule = self
179 end
180 end
181 self.in_importation = model.mmodule_importation_hierarchy.add_node(self)
182 end
183
184 # Register the imported modules (ie "import some_module")
185 # The visibility must be set with `set_visibility_for`.
186 fun set_imported_mmodules(imported_mmodules: Array[MModule])
187 do
188 for m in imported_mmodules do
189 self.model.mmodule_importation_hierarchy.add_edge(self, m)
190 end
191 end
192
193 private var intrude_mmodules = new HashSet[MModule]
194 private var public_mmodules = new HashSet[MModule]
195 private var private_mmodules = new HashSet[MModule]
196
197 # Return the visibility level of an imported module `m`
198 fun visibility_for(m: MModule): MVisibility
199 do
200 if m == self then return intrude_visibility
201 if self.intrude_mmodules.has(m) then return intrude_visibility
202 if self.public_mmodules.has(m) then return public_visibility
203 if self.private_mmodules.has(m) then return private_visibility
204 return none_visibility
205 end
206
207 # Set the visibility of an imported module
208 # REQUIRE: the visibility of the modules imported by `m` are already set for `m`
209 fun set_visibility_for(m: MModule, v: MVisibility)
210 do
211 if v == intrude_visibility then
212 self.intrude_mmodules.add(m)
213 self.intrude_mmodules.add_all(m.intrude_mmodules)
214 self.public_mmodules.add_all(m.public_mmodules)
215 self.private_mmodules.add_all(m.private_mmodules)
216 else if v == public_visibility then
217 self.public_mmodules.add(m)
218 self.public_mmodules.add_all(m.intrude_mmodules)
219 self.public_mmodules.add_all(m.public_mmodules)
220 else if v == private_visibility then
221 self.private_mmodules.add(m)
222 self.private_mmodules.add_all(m.intrude_mmodules)
223 self.private_mmodules.add_all(m.public_mmodules)
224 else
225 print "{self} visibility for {m} = {v}"
226 abort # invalid visibility
227 end
228 end
229
230 # Return true if a class or a property introduced in `intro_mmodule` with a visibility of `visibility` is visible in self.
231 fun is_visible(intro_mmodule: MModule, visibility: MVisibility): Bool
232 do
233 var v = visibility_for(intro_mmodule)
234 if v == intrude_visibility then
235 return visibility >= private_visibility
236 else if v == public_visibility then
237 return visibility > private_visibility
238 else if v == private_visibility then
239 return visibility > private_visibility
240 else if v == none_visibility then
241 return false
242 else
243 abort
244 end
245 end
246
247 # Is `self` a unit test module used by `nitunit`?
248 var is_test_suite: Bool = false is writable
249
250 # Get the first non `is_fictive` module greater than self
251 fun first_real_mmodule: MModule
252 do
253 var mmodule = self
254 while mmodule.is_fictive do
255 mmodule = mmodule.in_importation.direct_greaters.first
256 end
257 return mmodule
258 end
259
260 redef fun parent_concern do return mgroup
261 end