doc/commands: introduce usage commands
[nit.git] / src / doc / commands / commands_usage.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Commands about how mentities are used
16 module commands_usage
17
18 import commands_model
19 import semantize
20
21 # Retrieve all the mproperties using `mentity` as a type for its parameters
22 #
23 # `mentity` must be a MClass or a MClassDef.
24 class CmdParam
25 super CmdEntityList
26
27 redef fun init_results do
28 if results != null then return new CmdSuccess
29
30 var res = super
31 if not res isa CmdSuccess then return res
32 var mentity = self.mentity.as(not null)
33
34 if mentity isa MClassDef then mentity = mentity.mclass
35 if not mentity isa MClass then return new ErrorNotClass(mentity)
36
37 var mentities = new HashSet[MEntity]
38 for mproperty in view.mproperties do
39 if not mproperty isa MMethod then continue
40 var msignature = mproperty.intro.msignature
41 if msignature != null then
42 for mparam in msignature.mparameters do
43 var mtype = mparam.mtype
44 if mtype isa MNullableType then mtype = mtype.mtype
45 if not mtype isa MClassType then continue
46 if mtype.mclass != mentity then continue
47 mentities.add mproperty
48 end
49 end
50 end
51 results = mentities.to_a
52 return res
53 end
54 end
55
56 # Retrieve all the mproperties that return somethinf of the `mentity` type.
57 #
58 # `mentity` must be a MClass or a MClassDef.
59 class CmdReturn
60 super CmdEntityList
61
62 redef fun init_results do
63 if results != null then return new CmdSuccess
64
65 var res = super
66 if not res isa CmdSuccess then return res
67 var mentity = self.mentity.as(not null)
68
69 if mentity isa MClassDef then mentity = mentity.mclass
70 if not mentity isa MClass then return new ErrorNotClass(mentity)
71
72 var mentities = new HashSet[MEntity]
73 for mproperty in view.mproperties do
74 if not mproperty isa MMethod then continue
75 var msignature = mproperty.intro.msignature
76 if msignature != null then
77 var mtype = msignature.return_mtype
78 if mtype == null then continue
79 if mtype isa MNullableType then mtype = mtype.mtype
80 if not mtype isa MClassType then continue
81 if mtype.mclass != mentity then continue
82 mentities.add mproperty
83 end
84 end
85 results = mentities.to_a
86 return res
87 end
88 end
89
90 # Retrieve all the mproperties that initialize `mentity`
91 #
92 # `mentity` must be a MClass or a MClassDef.
93 class CmdNew
94 super CmdEntityList
95
96 autoinit(view, modelbuilder, mentity, mentity_name, limit, page, count, max)
97
98 # ModelBuilder used to retrieve AST nodes
99 var modelbuilder: ModelBuilder
100
101 redef fun init_results do
102 if results != null then return new CmdSuccess
103
104 var res = super
105 if not res isa CmdSuccess then return res
106 var mentity = self.mentity.as(not null)
107
108 if mentity isa MClassDef then mentity = mentity.mclass
109 if not mentity isa MClass then return new ErrorNotClass(mentity)
110
111 var mentities = new HashSet[MEntity]
112 for mpropdef in view.mpropdefs do
113 var visitor = new TypeInitVisitor(mentity)
114 var npropdef = modelbuilder.mpropdef2node(mpropdef)
115 if npropdef == null then continue
116 visitor.enter_visit(npropdef)
117 if visitor.called then
118 mentities.add mpropdef
119 end
120 end
121 results = mentities.to_a
122 return res
123 end
124 end
125
126 # Retrieve all the mproperties that call `mentity`
127 #
128 # `mentity` must be a MProperty or a MPropDef.
129 class CmdCall
130 super CmdEntityList
131
132 autoinit(view, modelbuilder, mentity, mentity_name, limit, page, count, max)
133
134 # ModelBuilder used to retrieve AST nodes
135 var modelbuilder: ModelBuilder
136
137 redef fun init_results do
138 if results != null then return new CmdSuccess
139
140 var res = super
141 if not res isa CmdSuccess then return res
142 var mentity = self.mentity.as(not null)
143
144 if mentity isa MPropDef then mentity = mentity.mproperty
145 if not mentity isa MProperty then return new ErrorNotProperty(mentity)
146
147 var mentities = new HashSet[MEntity]
148 for mpropdef in view.mpropdefs do
149 if mpropdef.mproperty == mentity then continue
150 var visitor = new MPropertyCallVisitor
151 var npropdef = modelbuilder.mpropdef2node(mpropdef)
152 if npropdef == null then continue
153 visitor.enter_visit(npropdef)
154 if visitor.calls.has(mentity) then
155 mentities.add mpropdef
156 end
157 end
158 results = mentities.to_a
159 return res
160 end
161 end
162
163 ## exploration
164
165 # Visitor looking for initialized `MType` (new T).
166 #
167 # See `NewCmd`.
168 private class TypeInitVisitor
169 super Visitor
170
171 var mclass: MClass
172
173 var called = false
174
175 redef fun visit(node)
176 do
177 node.visit_all(self)
178 # look for init
179 if not node isa ANewExpr then return
180 var mtype = node.n_type.mtype
181
182 if mtype == null then return
183 if mtype isa MNullableType then mtype = mtype.mtype
184 if not mtype isa MClassType then return
185 if mtype.mclass != mclass then return
186
187 called = true
188 end
189 end
190
191 # Visitor looking for calls to a `MProperty` (new T).
192 #
193 # See `CallCmd`.
194 private class MPropertyCallVisitor
195 super Visitor
196
197 var calls = new HashSet[MProperty]
198 redef fun visit(node)
199 do
200 node.visit_all(self)
201 if not node isa ASendExpr then return
202 calls.add node.callsite.as(not null).mproperty
203 end
204 end
205
206 # The MEntity is not a MClass or a MClassDef
207 class ErrorNotClass
208 super CmdError
209
210 # MEntity provided
211 var mentity: MEntity
212
213 redef fun to_s do return "`{mentity.full_name}` is not a class"
214 end
215
216 # The MEntity is not a MProperty or a MClassDef
217 class ErrorNotProperty
218 super CmdError
219
220 # MEntity provided
221 var mentity: MEntity
222
223 redef fun to_s do return "`{mentity.full_name}` is not a property"
224 end