model_collect: use model_views
[nit.git] / src / model / model_collect.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2008 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 # Collect things from a `Model`.
18 #
19 # **Warning**
20 #
21 # `model_collect` offers a flattened view of the model without considering any
22 # main module.
23 # For this reason, `model_collect` lists all the definitions reachable from all
24 # modules
25 #
26 # This is usefull for tools that need a global view of a model like `nitdoc`,
27 # `nitx` or `nituml`.
28 # It shoul not be used for compiling stuffs like computing VFT, where the listed
29 # entities could not be reachable depending on the modules really imported.
30 module model_collect
31
32 import model_views
33
34 redef class MModule
35
36 # Collect mclassdefs introduced in `self` with `visibility >= to min_visibility`.
37 fun collect_intro_mclassdefs(view: ModelView): Set[MClassDef] do
38 var res = new HashSet[MClassDef]
39 for mclassdef in mclassdefs do
40 if not mclassdef.is_intro then continue
41 if not view.accept_mentity(mclassdef) then continue
42 res.add mclassdef
43 end
44 return res
45 end
46
47 # Collect mclassdefs redefined in `self` with `visibility >= to min_visibility`.
48 fun collect_redef_mclassdefs(view: ModelView): Set[MClassDef] do
49 var res = new HashSet[MClassDef]
50 for mclassdef in mclassdefs do
51 if mclassdef.is_intro then continue
52 if not view.accept_mentity(mclassdef) then continue
53 res.add mclassdef
54 end
55 return res
56 end
57
58 # Collect mclasses introduced in `self` with `visibility >= to min_visibility`.
59 fun collect_intro_mclasses(view: ModelView): Set[MClass] do
60 var res = new HashSet[MClass]
61 for mclass in intro_mclasses do
62 if not view.accept_mentity(mclass) then continue
63 res.add mclass
64 end
65 return res
66 end
67
68 # Collect mclasses redefined in `self` with `visibility >= to min_visibility`.
69 fun collect_redef_mclasses(view: ModelView): Set[MClass] do
70 var mclasses = new HashSet[MClass]
71 for mclassdef in mclassdefs do
72 if not view.accept_mentity(mclassdef) then continue
73 if not mclassdef.is_intro then mclasses.add(mclassdef.mclass)
74 end
75 return mclasses
76 end
77 end
78
79 redef class MClass
80
81 # Collect direct parents of `self` with `visibility >= to min_visibility`.
82 fun collect_parents(view: ModelView): Set[MClass] do
83 var res = new HashSet[MClass]
84 for mclassdef in mclassdefs do
85 for mclasstype in mclassdef.supertypes do
86 var mclass = mclasstype.mclass
87 if not view.accept_mentity(mclass) then continue
88 res.add(mclass)
89 end
90 end
91 return res
92 end
93
94 # Collect all ancestors of `self` with `visibility >= to min_visibility`.
95 fun collect_ancestors(view: ModelView): Set[MClass] do
96 var res = new HashSet[MClass]
97 for mclassdef in self.mclassdefs do
98 for super_mclassdef in mclassdef.in_hierarchy.greaters do
99 if super_mclassdef == mclassdef then continue # skip self
100 var mclass = super_mclassdef.mclass
101 if not view.accept_mentity(mclass) then continue
102 res.add(mclass)
103 end
104 end
105 return res
106 end
107
108 # Collect direct children of `self` with `visibility >= to min_visibility`.
109 fun collect_children(view: ModelView): Set[MClass] do
110 var res = new HashSet[MClass]
111 for mclassdef in self.mclassdefs do
112 for sub_mclassdef in mclassdef.in_hierarchy.direct_smallers do
113 if sub_mclassdef == mclassdef then continue # skip self
114 var mclass = sub_mclassdef.mclass
115 if not view.accept_mentity(mclass) then continue
116 res.add(mclass)
117 end
118 end
119 return res
120 end
121
122 # Collect all descendants of `self` with `visibility >= to min_visibility`.
123 fun collect_descendants(view: ModelView): Set[MClass] do
124 var res = new HashSet[MClass]
125 for mclassdef in self.mclassdefs do
126 for sub_mclassdef in mclassdef.in_hierarchy.smallers do
127 if sub_mclassdef == mclassdef then continue # skip self
128 var mclass = sub_mclassdef.mclass
129 if not view.accept_mentity(mclass) then continue
130 res.add(mclass)
131 end
132 end
133 return res
134 end
135
136 # Collect all mproperties introduced in 'self' with `visibility >= min_visibility`.
137 fun collect_intro_mproperties(view: ModelView): Set[MProperty] do
138 var set = new HashSet[MProperty]
139 for mclassdef in mclassdefs do
140 for mprop in mclassdef.intro_mproperties do
141 if not view.accept_mentity(mprop) then continue
142 set.add(mprop)
143 end
144 end
145 return set
146 end
147
148 # Collect all mproperties redefined in 'self' with `visibility >= min_visibility`.
149 fun collect_redef_mproperties(view: ModelView): Set[MProperty] do
150 var set = new HashSet[MProperty]
151 for mclassdef in mclassdefs do
152 for mpropdef in mclassdef.mpropdefs do
153 if mpropdef.mproperty.intro_mclassdef.mclass == self then continue
154 if not view.accept_mentity(mpropdef) then continue
155 set.add(mpropdef.mproperty)
156 end
157 end
158 return set
159 end
160
161 # Collect mproperties introduced and redefined in 'self' with `visibility >= min_visibility`.
162 fun collect_local_mproperties(view: ModelView): Set[MProperty] do
163 var set = new HashSet[MProperty]
164 set.add_all collect_intro_mproperties(view)
165 set.add_all collect_redef_mproperties(view)
166 return set
167 end
168
169 # Collect all mproperties inehrited by 'self' with `visibility >= min_visibility`.
170 fun collect_inherited_mproperties(view: ModelView): Set[MProperty] do
171 var set = new HashSet[MProperty]
172 for parent in collect_parents(view) do
173 set.add_all(parent.collect_intro_mproperties(view))
174 set.add_all(parent.collect_inherited_mproperties(view))
175 end
176 return set
177 end
178
179 # Collect all mproperties accessible by 'self' with `visibility >= min_visibility`.
180 #
181 # This include introduced, redefined, inherited mproperties.
182 fun collect_accessible_mproperties(view: ModelView): Set[MProperty] do
183 var set = new HashSet[MProperty]
184 set.add_all(collect_intro_mproperties(view))
185 set.add_all(collect_redef_mproperties(view))
186 set.add_all(collect_inherited_mproperties(view))
187 return set
188 end
189
190 # Collect mmethods introduced in 'self' with `visibility >= min_visibility`.
191 fun collect_intro_mmethods(view: ModelView): Set[MMethod] do
192 var res = new HashSet[MMethod]
193 for mproperty in collect_intro_mproperties(view) do
194 if mproperty isa MMethod then res.add(mproperty)
195 end
196 return res
197 end
198
199 # Collect mmethods redefined in 'self' with `visibility >= min_visibility`.
200 fun collect_redef_mmethods(view: ModelView): Set[MMethod] do
201 var res = new HashSet[MMethod]
202 for mproperty in collect_redef_mproperties(view) do
203 if mproperty isa MMethod then res.add(mproperty)
204 end
205 return res
206 end
207
208 # Collect mmethods introduced and redefined in 'self' with `visibility >= min_visibility`.
209 fun collect_local_mmethods(view: ModelView): Set[MMethod] do
210 var set = new HashSet[MMethod]
211 set.add_all collect_intro_mmethods(view)
212 set.add_all collect_redef_mmethods(view)
213 return set
214 end
215
216 # Collect mattributes introduced in 'self' with `visibility >= min_visibility`.
217 fun collect_intro_mattributes(view: ModelView): Set[MAttribute] do
218 var res = new HashSet[MAttribute]
219 for mproperty in collect_intro_mproperties(view) do
220 if mproperty isa MAttribute then res.add(mproperty)
221 end
222 return res
223 end
224
225 # Collect mattributes redefined in 'self' with `visibility >= min_visibility`.
226 fun collect_redef_mattributes(view: ModelView): Set[MAttribute] do
227 var res = new HashSet[MAttribute]
228 for mproperty in collect_redef_mproperties(view) do
229 if mproperty isa MAttribute then res.add(mproperty)
230 end
231 return res
232 end
233
234 # Collect mattributes introduced and redefined in 'self' with `visibility >= min_visibility`.
235 fun collect_local_mattributes(view: ModelView): Set[MAttribute] do
236 var set = new HashSet[MAttribute]
237 set.add_all collect_intro_mattributes(view)
238 set.add_all collect_redef_mattributes(view)
239 return set
240 end
241
242 # Collect mattributes inherited by 'self' with `visibility >= min_visibility`.
243 fun collect_inherited_mattributes(view: ModelView): Set[MAttribute] do
244 var res = new HashSet[MAttribute]
245 for mproperty in collect_inherited_mproperties(view) do
246 if mproperty isa MAttribute then res.add(mproperty)
247 end
248 return res
249 end
250
251 # Collect all mattributes accessible by 'self' with `visibility >= min_visibility`.
252 #
253 # This include introduced, redefined, inherited mattributes.
254 fun collect_accessible_mattributes(view: ModelView): Set[MAttribute] do
255 var set = new HashSet[MAttribute]
256 set.add_all(collect_intro_mattributes(view))
257 set.add_all(collect_redef_mattributes(view))
258 set.add_all(collect_inherited_mattributes(view))
259 return set
260 end
261 end
262
263 redef class MClassDef
264
265 # Collect mpropdefs in 'self' with `visibility >= min_visibility`.
266 fun collect_mpropdefs(view: ModelView): Set[MPropDef] do
267 var res = new HashSet[MPropDef]
268 for mpropdef in mpropdefs do
269 if not view.accept_mentity(mpropdef) then continue
270 res.add mpropdef
271 end
272 return res
273 end
274
275 # Collect mpropdefs introduced in 'self' with `visibility >= min_visibility`.
276 fun collect_intro_mpropdefs(view: ModelView): Set[MPropDef] do
277 var res = new HashSet[MPropDef]
278 for mpropdef in mpropdefs do
279 if not mpropdef.is_intro then continue
280 if not view.accept_mentity(mpropdef) then continue
281 res.add mpropdef
282 end
283 return res
284 end
285
286 # Collect mpropdefs redefined in 'self' with `visibility >= min_visibility`.
287 fun collect_redef_mpropdefs(view: ModelView): Set[MPropDef] do
288 var res = new HashSet[MPropDef]
289 for mpropdef in mpropdefs do
290 if mpropdef.is_intro then continue
291 if not view.accept_mentity(mpropdef) then continue
292 res.add mpropdef
293 end
294 return res
295 end
296
297 # Collect modifiers like redef, private etc.
298 fun collect_modifiers: Array[String] do
299 var res = new Array[String]
300 if not is_intro then
301 res.add "redef"
302 else
303 res.add mclass.visibility.to_s
304 end
305 res.add mclass.kind.to_s
306 return res
307 end
308 end
309
310 redef class MPropDef
311 # Collect modifiers like redef, private, abstract, intern, fun etc.
312 fun collect_modifiers: Array[String] do
313 var res = new Array[String]
314 if not is_intro then
315 res.add "redef"
316 else
317 res.add mproperty.visibility.to_s
318 end
319 var mprop = self
320 if mprop isa MVirtualTypeDef then
321 res.add "type"
322 else if mprop isa MMethodDef then
323 if mprop.is_abstract then
324 res.add "abstract"
325 else if mprop.is_intern then
326 res.add "intern"
327 end
328 if mprop.mproperty.is_init then
329 res.add "init"
330 else
331 res.add "fun"
332 end
333 end
334 return res
335 end
336 end