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