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