d2d7268f082029b0648f3bff8407d2d9a34c09dc
[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 mproject
22 private import more_collections
23
24 # The container class of a Nit object-oriented model.
25 # A model knows modules, classes and properties and can retrieve them.
26 redef class Model
27 # All known modules
28 var mmodules: Array[MModule] = new Array[MModule]
29
30 # placebo for old module nesting hierarchy.
31 # where mainmodule < mainmodule::nestedmodule
32 #
33 # TODO REMOVE, rely on mgroup instead
34 var mmodule_nesting_hierarchy: POSet[MModule] = new POSet[MModule]
35
36 # Full module importation hierarchy including private or nested links.
37 var mmodule_importation_hierarchy: POSet[MModule] = new POSet[MModule]
38
39 # Collections of modules grouped by their short names
40 private var mmodules_by_name: MultiHashMap[String, MModule] = new MultiHashMap[String, MModule]
41
42 # Return all module named `name`
43 # If such a module does not exist, null is returned (instead of an empty array)
44 #
45 # Visibility or modules are not considered
46 fun get_mmodules_by_name(name: String): nullable Array[MModule]
47 do
48 if mmodules_by_name.has_key(name) then
49 return mmodules_by_name[name]
50 else
51 return null
52 end
53 end
54 end
55
56 redef class MGroup
57 # The loaded modules of this group
58 var mmodules = new Array[MModule]
59
60 # The default module of a group (if any, and if loaded)
61 #
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
66 end
67
68 # A Nit module is usually associated with a Nit source file.
69 class MModule
70 super MConcern
71
72 # The model considered
73 redef var model: Model
74
75 # placebo for old module nesting hierarchy
76 # return null if self is not nested (ie. is a top-level module)
77 #
78 # TODO REMOVE, rely on mgroup instead
79 var direct_owner: nullable MModule
80
81 # The group of module in the project if any
82 var mgroup: nullable MGroup
83
84 # The short name of the module
85 redef var name: String
86
87 # The origin of the definition
88 var location: Location
89
90 # Alias for `name`
91 redef fun to_s do return self.name
92
93 # placebo for old module nesting hierarchy
94 # The view of the module in the `model.mmodule_nesting_hierarchy`
95 #
96 # TODO REMOVE, rely on mgroup instead
97 var in_nesting: POSetElement[MModule]
98
99 # The view of the module in the `model.mmodule_importation_hierarchy`
100 var in_importation: POSetElement[MModule]
101
102 # The canonical name of the module
103 # Example: `"project::name"`
104 fun full_name: String
105 do
106 var mgroup = self.mgroup
107 if mgroup == null or mgroup.mproject.name == self.name then
108 return self.name
109 else
110 return "{mgroup.mproject.name}::{self.name}"
111 end
112 end
113
114 # Create a new empty module and register it to a model
115 init(model: Model, mgroup: nullable MGroup, name: String, location: Location)
116 do
117 self.model = model
118 self.name = name
119 self.location = location
120 model.mmodules_by_name.add_one(name, self)
121 model.mmodules.add(self)
122 self.in_nesting = model.mmodule_nesting_hierarchy.add_node(self)
123 self.mgroup = mgroup
124 if mgroup != null then
125 mgroup.mmodules.add(self)
126 if mgroup.name == name then
127 assert mgroup.default_mmodule == null
128 mgroup.default_mmodule = self
129 end
130 # placebo for old module nesting hierarchy
131 var direct_owner = mgroup.default_mmodule
132 if direct_owner == self then
133 # The module is the new owner of its own group, thus adopt the other modules
134 for m in mgroup.mmodules do
135 if m == self then continue
136 m.direct_owner = self
137 model.mmodule_nesting_hierarchy.add_edge(self, m)
138 end
139 # The potential owner is the default_mmodule of the parent group
140 if mgroup.parent != null then direct_owner = mgroup.parent.default_mmodule
141 end
142 if direct_owner != self and direct_owner != null then
143 self.direct_owner = direct_owner
144 model.mmodule_nesting_hierarchy.add_edge(direct_owner, self)
145 end
146 end
147 self.in_importation = model.mmodule_importation_hierarchy.add_node(self)
148 end
149
150 # Register the imported modules (ie "import some_module")
151 # This function can only invoked once by mmodule.
152 # The visibility must be set with `set_visibility_for`.
153 fun set_imported_mmodules(imported_mmodules: Array[MModule])
154 do
155 assert unique_invocation: self.in_importation.direct_greaters.is_empty
156 for m in imported_mmodules do
157 self.model.mmodule_importation_hierarchy.add_edge(self, m)
158 end
159 end
160
161 private var intrude_mmodules: HashSet[MModule] = new HashSet[MModule]
162 private var public_mmodules: HashSet[MModule] = new HashSet[MModule]
163 private var private_mmodules: HashSet[MModule] = new HashSet[MModule]
164
165 # Return the visibility level of an imported module `m`
166 fun visibility_for(m: MModule): MVisibility
167 do
168 if m == self then return intrude_visibility
169 if self.intrude_mmodules.has(m) then return intrude_visibility
170 if self.public_mmodules.has(m) then return public_visibility
171 if self.private_mmodules.has(m) then return private_visibility
172 return none_visibility
173 end
174
175 # Set the visibility of an imported module
176 # REQUIRE: the visibility of the modules imported by `m` are already set for `m`
177 fun set_visibility_for(m: MModule, v: MVisibility)
178 do
179 if v == intrude_visibility then
180 self.intrude_mmodules.add(m)
181 self.intrude_mmodules.add_all(m.intrude_mmodules)
182 self.public_mmodules.add_all(m.public_mmodules)
183 self.private_mmodules.add_all(m.private_mmodules)
184 else if v == public_visibility then
185 self.public_mmodules.add(m)
186 self.public_mmodules.add_all(m.intrude_mmodules)
187 self.public_mmodules.add_all(m.public_mmodules)
188 else if v == private_visibility then
189 self.private_mmodules.add(m)
190 self.private_mmodules.add_all(m.intrude_mmodules)
191 self.private_mmodules.add_all(m.public_mmodules)
192 else
193 print "{self} visibility for {m} = {v}"
194 abort # invalid visibility
195 end
196 end
197
198 # Return true if a class or a property introduced in `intro_mmodule` with a visibility of `visibility` is visible in self.
199 fun is_visible(intro_mmodule: MModule, visibility: MVisibility): Bool
200 do
201 var v = visibility_for(intro_mmodule)
202 if v == intrude_visibility then
203 return visibility >= private_visibility
204 else if v == public_visibility then
205 return visibility > private_visibility
206 else if v == private_visibility then
207 return visibility > private_visibility
208 else if v == none_visibility then
209 return false
210 else
211 abort
212 end
213 end
214
215 # Is the mmodule created for internal purpose?
216 # Fictive module are instantied internally but they should not be
217 # exposed to the final user
218 var is_fictive: Bool = false is writable
219
220 redef fun parent_concern do return mgroup
221 end