1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2008 Jean Privat <jean@pryen.org>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # Model exploration and traversing facilities
23 fun in_nesting_intro_mclasses
(min_visibility
: MVisibility): Set[MClass] do
24 var res
= new HashSet[MClass]
25 var lst
= in_nesting
.direct_smallers
26 for mmodule
in mmodules
do res
.add_all mmodule
.intro_mclasses
27 for mgrp
in lst
do res
.add_all mgrp
.in_nesting_intro_mclasses
(min_visibility
)
31 fun in_nesting_redef_mclasses
(min_visibility
: MVisibility): Set[MClass] do
32 var res
= new HashSet[MClass]
33 var lst
= in_nesting
.direct_smallers
34 for mmodule
in mmodules
do res
.add_all mmodule
.redef_mclasses
35 for mgrp
in lst
do res
.add_all mgrp
.in_nesting_redef_mclasses
(min_visibility
)
39 fun in_nesting_intro_mclassdefs
(min_visibility
: MVisibility): Set[MClassDef] do
40 var res
= new HashSet[MClassDef]
41 var lst
= in_nesting
.direct_smallers
42 for mmodule
in mmodules
do res
.add_all mmodule
.intro_mclassdefs
(min_visibility
)
43 for mgrp
in lst
do res
.add_all mgrp
.in_nesting_intro_mclassdefs
(min_visibility
)
47 fun in_nesting_redef_mclassdefs
(min_visibility
: MVisibility): Set[MClassDef] do
48 var res
= new HashSet[MClassDef]
49 var lst
= in_nesting
.direct_smallers
50 for mmodule
in mmodules
do res
.add_all mmodule
.redef_mclassdefs
(min_visibility
)
51 for mgrp
in lst
do res
.add_all mgrp
.in_nesting_redef_mclassdefs
(min_visibility
)
58 # The list of intro mclassdef in the module.
59 # with visibility >= to min_visibility
60 fun intro_mclassdefs
(min_visibility
: MVisibility): Set[MClassDef] do
61 var res
= new HashSet[MClassDef]
62 for mclassdef
in mclassdefs
do
63 if not mclassdef
.is_intro
then continue
64 if mclassdef
.mclass
.visibility
< min_visibility
then continue
70 # The list of redef mclassdef in the module.
71 # with visibility >= to min_visibility
72 fun redef_mclassdefs
(min_visibility
: MVisibility): Set[MClassDef] do
73 var res
= new HashSet[MClassDef]
74 for mclassdef
in mclassdefs
do
75 if mclassdef
.is_intro
then continue
76 if mclassdef
.mclass
.visibility
< min_visibility
then continue
82 # Get the list of mclasses refined in 'self'.
83 fun redef_mclasses
: Set[MClass] do
84 var mclasses
= new HashSet[MClass]
85 for c
in mclassdefs
do
86 if not c
.is_intro
then mclasses
.add
(c
.mclass
)
91 # Get the list of all mclasses imported by 'self'.
92 fun imported_mclasses
: Set[MClass] do
93 var mclasses
= new HashSet[MClass]
94 for m
in in_importation
.greaters
do
95 if m
== self then continue
96 for c
in m
.mclassdefs
do mclasses
.add
(c
.mclass
)
101 fun in_nesting_intro_mclasses
(min_visibility
: MVisibility): Set[MClass] do
102 var res
= new HashSet[MClass]
103 for mmodule
in in_nesting
.greaters
do
104 for mclass
in mmodule
.intro_mclasses
do
105 if mclass
.visibility
< min_visibility
then continue
112 fun in_nesting_redef_mclasses
(min_visibility
: MVisibility): Set[MClass] do
113 var res
= new HashSet[MClass]
114 for mmodule
in self.in_nesting
.greaters
do
115 for mclass
in mmodule
.redef_mclasses
do
116 if mclass
.visibility
< min_visibility
then continue
123 fun in_nesting_intro_mclassdefs
(min_visibility
: MVisibility): Set[MClassDef] do
124 var res
= new HashSet[MClassDef]
125 for mmodule
in in_nesting
.greaters
do
126 res
.add_all mmodule
.intro_mclassdefs
(min_visibility
)
131 fun in_nesting_redef_mclassdefs
(min_visibility
: MVisibility): Set[MClassDef] do
132 var res
= new HashSet[MClassDef]
133 for mmodule
in self.in_nesting
.greaters
do
134 res
.add_all mmodule
.redef_mclassdefs
(min_visibility
)
142 # Get the public owner of 'self'.
143 fun public_owner
: MModule do
144 var public_owner
= self.intro_mmodule
.public_owner
145 if public_owner
== null then
146 return self.intro_mmodule
152 # Get direct parents of 'self'.
153 fun parents
: Set[MClass] do
154 var ret
= new HashSet[MClass]
155 for mclassdef
in mclassdefs
do
156 for mclasstype
in mclassdef
.supertypes
do
157 ret
.add
(mclasstype
.mclass
)
163 # Get all ancestors of 'self'.
164 fun ancestors
: Set[MClass] do
165 var lst
= new HashSet[MClass]
166 for mclassdef
in self.mclassdefs
do
167 for super_mclassdef
in mclassdef
.in_hierarchy
.greaters
do
168 if super_mclassdef
== mclassdef
then continue # skip self
169 lst
.add
(super_mclassdef
.mclass
)
175 # Get direct children of 'self'.
176 fun children
: Set[MClass] do
177 var lst
= new HashSet[MClass]
178 for mclassdef
in self.mclassdefs
do
179 for sub_mclassdef
in mclassdef
.in_hierarchy
.direct_smallers
do
180 if sub_mclassdef
== mclassdef
then continue # skip self
181 lst
.add
(sub_mclassdef
.mclass
)
187 # Get all children of 'self'.
188 fun descendants
: Set[MClass] do
189 var lst
= new HashSet[MClass]
190 for mclassdef
in self.mclassdefs
do
191 for sub_mclassdef
in mclassdef
.in_hierarchy
.smallers
do
192 if sub_mclassdef
== mclassdef
then continue # skip self
193 lst
.add
(sub_mclassdef
.mclass
)
199 # Get the list of constructors available for 'self'.
200 fun constructors
: Set[MMethod] do
201 var res
= new HashSet[MMethod]
202 for mclassdef
in mclassdefs
do
203 for mpropdef
in mclassdef
.mpropdefs
do
204 if mpropdef
isa MMethodDef then
205 if mpropdef
.mproperty
.is_init
then res
.add
(mpropdef
.mproperty
)
212 # Get the list of methods introduced in 'self'.
213 fun intro_methods
: Set[MMethod] do
214 var res
= new HashSet[MMethod]
215 for mclassdef
in mclassdefs
do
216 for mpropdef
in mclassdef
.mpropdefs
do
217 if mpropdef
isa MMethodDef then
218 if mpropdef
.is_intro
and not mpropdef
.mproperty
.is_init
then res
.add
(mpropdef
.mproperty
)
225 # the set of properties introduced in 'self'.
226 fun intro_mproperties
(min_visibility
: MVisibility): Set[MProperty] do
227 var set
= new HashSet[MProperty]
228 for mclassdef
in mclassdefs
do
229 for mprop
in mclassdef
.intro_mproperties
do
230 if mprop
.visibility
< min_visibility
then continue
237 fun intro_mpropdefs
(min_visibility
: MVisibility): Set[MPropDef] do
238 var set
= new HashSet[MPropDef]
239 for mclassdef
in mclassdefs
do
240 for mpropdef
in mclassdef
.mpropdefs
do
241 if not mpropdef
.is_intro
then continue
242 if mpropdef
.mproperty
.visibility
< min_visibility
then continue
249 # the set of locally refined properties in 'self'.
250 fun redef_mproperties
(min_visibility
: MVisibility): Set[MProperty] do
251 var set
= new HashSet[MProperty]
252 for mclassdef
in mclassdefs
do
253 for mpropdef
in mclassdef
.mpropdefs
do
254 if mpropdef
.mproperty
.visibility
< min_visibility
then continue
255 if mpropdef
.mproperty
.intro_mclassdef
.mclass
!= self then set
.add
(mpropdef
.mproperty
)
261 fun redef_mpropdefs
(min_visibility
: MVisibility): Set[MPropDef] do
262 var set
= new HashSet[MPropDef]
263 for mclassdef
in mclassdefs
do
264 for mpropdef
in mclassdef
.mpropdefs
do
265 if mpropdef
.is_intro
then continue
266 if mpropdef
.mproperty
.visibility
< min_visibility
then continue
273 # the set of methods inherited by 'self'.
274 fun inherited_mproperties
(mainmodule
: MModule, min_visibility
: MVisibility): Set[MProperty] do
275 var set
= new HashSet[MProperty]
276 for parent
in in_hierarchy
(mainmodule
).direct_greaters
do
277 set
.add_all
(parent
.intro_mproperties
(min_visibility
))
278 set
.add_all
(parent
.inherited_mproperties
(mainmodule
, min_visibility
))
283 # the set of introduced and redefined mproperties
284 fun local_mproperties
(min_visibility
: MVisibility): Set[MProperty] do
285 var set
= new HashSet[MProperty]
286 set
.add_all
(intro_mproperties
(min_visibility
))
287 set
.add_all
(redef_mproperties
(min_visibility
))
291 # the set of all accessible mproperties for this class
292 fun all_mproperties
(mainmodule
: MModule, min_visibility
: MVisibility): Set[MProperty] do
293 var set
= new HashSet[MProperty]
294 set
.add_all
(local_mproperties
(min_visibility
))
295 set
.add_all
(inherited_mproperties
(mainmodule
, min_visibility
))
299 # the set of all accessible mattributes for this class
300 fun all_mattributes
(mainmodule
: MModule, min_visibility
: MVisibility): Set[MAttribute] do
301 var set
= new HashSet[MAttribute]
302 for mprop
in all_mproperties
(mainmodule
, min_visibility
) do
303 if mprop
isa MAttribute then set
.add
(mprop
)
308 # Get the list of locally refined methods in 'self'.
309 fun redef_methods
: Set[MMethod] do
310 var res
= new HashSet[MMethod]
311 for mclassdef
in mclassdefs
do
312 for mpropdef
in mclassdef
.mpropdefs
do
313 if mpropdef
isa MMethodDef then
314 if not mpropdef
.is_intro
and not mpropdef
.mproperty
.is_init
then res
.add
(mpropdef
.mproperty
)
321 fun inherited_methods
: Set[MMethod] do
322 var res
= new HashSet[MMethod]
323 for s
in ancestors
do
324 for m
in s
.intro_methods
do
325 if not self.intro_methods
.has
(m
) and not self.redef_methods
.has
(m
) then res
.add
(m
)
331 # Get the list of all virtual types available in 'self'.
332 fun virtual_types
: Set[MVirtualTypeProp] do
333 var res
= new HashSet[MVirtualTypeProp]
334 for mclassdef
in mclassdefs
do
335 for mpropdef
in mclassdef
.mpropdefs
do
336 if mpropdef
isa MVirtualTypeDef then
337 res
.add
(mpropdef
.mproperty
)
341 for ancestor
in ancestors
do
342 for mclassdef
in ancestor
.mclassdefs
do
343 for mpropdef
in mclassdef
.mpropdefs
do
344 if mpropdef
isa MVirtualTypeDef then
345 res
.add
(mpropdef
.mproperty
)
353 # Get the list of all parameter types in 'self'.
354 fun parameter_types
: Map[String, MType] do
355 var res
= new HashMap[String, MType]
356 for i
in [0..intro
.parameter_names
.length
[ do
357 res
[intro
.parameter_names
[i
]] = intro
.bound_mtype
.arguments
[i
]
362 fun is_class
: Bool do
363 return self.kind
== concrete_kind
or self.kind
== abstract_kind
366 fun is_interface
: Bool do
367 return self.kind
== interface_kind
371 return self.kind
== enum_kind
374 fun is_abstract
: Bool do
375 return self.kind
== abstract_kind
379 redef class MAttribute
380 # Is this attribute nullable for sure?
382 # This mean that its introduction is declarred with a nullable static type
383 # since attributes are invariant this will work on most cases
384 # attributes with static type anchored with a virtual type are not "nullable for-sure"
385 # because this type can be redefined in subclasses
386 fun is_nullable
: Bool do return intro
.static_mtype
isa MNullableType
389 redef class MClassDef
390 # modifiers are keywords like redef, private etc.
391 fun modifiers
: Array[String] do
392 var res
= new Array[String]
396 res
.add mclass
.visibility
.to_s
398 res
.add mclass
.kind
.to_s
404 # modifiers are keywords like redef, private etc.
405 fun modifiers
: Array[String] do
406 var res
= new Array[String]
410 res
.add mproperty
.visibility
.to_s
413 if mprop
isa MVirtualTypeDef then
415 else if mprop
isa MMethodDef then
416 if mprop
.is_abstract
then
418 else if mprop
.is_intern
then
421 if mprop
.mproperty
.is_init
then
434 # Sort mmodules by their name
435 class MModuleNameSorter
436 super AbstractSorter[MModule]
437 redef fun compare
(a
, b
) do return a
.name
<=> b
.name
441 # Sort mclasses by their name
442 class MClassNameSorter
443 super AbstractSorter[MClass]
444 redef fun compare
(a
, b
) do return a
.name
<=> b
.name
448 # Sort mclassdefs by their name
449 class MClassDefNameSorter
450 super AbstractSorter[MClassDef]
451 redef fun compare
(a
, b
) do return a
.mclass
.name
<=> b
.mclass
.name
455 # Sort mproperties by their name
456 class MPropertyNameSorter
457 super AbstractSorter[MProperty]
458 redef fun compare
(a
, b
) do return a
.name
<=> b
.name
462 # Sort mpropdefs by their name
463 class MPropDefNameSorter
464 super AbstractSorter[MPropDef]
465 redef fun compare
(a
, b
) do return a
.mproperty
.name
<=> b
.mproperty
.name