ni: extracted model facilities in their own module
[nit.git] / src / model_utils.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 # Model exploration and traversing facilities
18 module model_utils
19
20 import toolcontext
21 import exprbuilder
22
23 redef class MModule
24 # Get the list of mclasses refined in 'self'.
25 fun redef_mclasses: Set[MClass] do
26 var mclasses = new HashSet[MClass]
27 for c in mclassdefs do
28 if not c.is_intro then mclasses.add(c.mclass)
29 end
30 return mclasses
31 end
32
33 # Get the list of all mclasses imported by 'self'.
34 fun imported_mclasses: Set[MClass] do
35 var mclasses = new HashSet[MClass]
36 for m in in_importation.greaters do
37 for c in m.mclassdefs do mclasses.add(c.mclass)
38 end
39 return mclasses
40 end
41 end
42
43 redef class MClass
44
45 # Get direct parents of 'self'.
46 fun parents: Set[MClass] do
47 var ret = new HashSet[MClass]
48 for mclassdef in mclassdefs do
49 for mclasstype in mclassdef.supertypes do
50 ret.add(mclasstype.mclass)
51 end
52 end
53 return ret
54 end
55
56 # Get all ancestors of 'self'.
57 fun ancestors: Set[MClass] do
58 var lst = new HashSet[MClass]
59 for mclassdef in self.mclassdefs do
60 for super_mclassdef in mclassdef.in_hierarchy.greaters do
61 if super_mclassdef == mclassdef then continue # skip self
62 lst.add(super_mclassdef.mclass)
63 end
64 end
65 return lst
66 end
67
68 # Get the list of constructors available for 'self'.
69 fun constructors: Set[MMethod] do
70 var res = new HashSet[MMethod]
71 for mclassdef in mclassdefs do
72 for mpropdef in mclassdef.mpropdefs do
73 if mpropdef isa MMethodDef then
74 if mpropdef.mproperty.is_init then res.add(mpropdef.mproperty)
75 end
76 end
77 end
78 return res
79 end
80
81 # Get the list of methods introduced in 'self'.
82 fun intro_methods: Set[MMethod] do
83 var res = new HashSet[MMethod]
84 for mclassdef in mclassdefs do
85 for mpropdef in mclassdef.mpropdefs do
86 if mpropdef isa MMethodDef then
87 if mpropdef.is_intro and not mpropdef.mproperty.is_init then res.add(mpropdef.mproperty)
88 end
89 end
90 end
91 return res
92 end
93
94 # Get the list of locally refined methods in 'self'.
95 fun redef_methods: Set[MMethod] do
96 var res = new HashSet[MMethod]
97 for mclassdef in mclassdefs do
98 for mpropdef in mclassdef.mpropdefs do
99 if mpropdef isa MMethodDef then
100 if not mpropdef.is_intro and not mpropdef.mproperty.is_init then res.add(mpropdef.mproperty)
101 end
102 end
103 end
104 return res
105 end
106
107 # Get the list of methods inherited by 'self'.
108 fun inherited_methods: Set[MMethod] do
109 var res = new HashSet[MMethod]
110 for s in ancestors do
111 for m in s.intro_methods do
112 if not self.intro_methods.has(m) and not self.redef_methods.has(m) then res.add(m)
113 end
114 end
115 return res
116 end
117
118 fun is_class: Bool do
119 return self.kind == concrete_kind or self.kind == abstract_kind
120 end
121
122 fun is_interface: Bool do
123 return self.kind == interface_kind
124 end
125
126 fun is_enum: Bool do
127 return self.kind == enum_kind
128 end
129
130 fun is_abstract: Bool do
131 return self.kind == abstract_kind
132 end
133 end