1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Commands about how mentities are used
21 # Retrieve all the mproperties using `mentity` as a type for its parameters
23 # `mentity` must be a MClass or a MClassDef.
27 redef fun init_results
do
28 if results
!= null then return new CmdSuccess
31 if not res
isa CmdSuccess then return res
32 var mentity
= self.mentity
.as(not null)
34 if mentity
isa MClassDef then mentity
= mentity
.mclass
35 if not mentity
isa MClass then return new ErrorNotClass(mentity
)
37 var mentities
= new HashSet[MEntity]
38 for mproperty
in model
.collect_mproperties
(filter
) 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
51 results
= mentities
.to_a
56 # Retrieve all the mproperties that return somethinf of the `mentity` type.
58 # `mentity` must be a MClass or a MClassDef.
62 redef fun init_results
do
63 if results
!= null then return new CmdSuccess
66 if not res
isa CmdSuccess then return res
67 var mentity
= self.mentity
.as(not null)
69 if mentity
isa MClassDef then mentity
= mentity
.mclass
70 if not mentity
isa MClass then return new ErrorNotClass(mentity
)
72 var mentities
= new HashSet[MEntity]
73 for mproperty
in model
.collect_mproperties
(filter
) 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
85 results
= mentities
.to_a
90 # Retrieve all the mproperties that initialize `mentity`
92 # `mentity` must be a MClass or a MClassDef.
96 autoinit
(model
, modelbuilder
, filter
, mentity
, mentity_name
, limit
, page
, count
, max
)
98 # ModelBuilder used to retrieve AST nodes
99 var modelbuilder
: ModelBuilder
101 redef fun init_results
do
102 if results
!= null then return new CmdSuccess
105 if not res
isa CmdSuccess then return res
106 var mentity
= self.mentity
.as(not null)
108 if mentity
isa MClassDef then mentity
= mentity
.mclass
109 if not mentity
isa MClass then return new ErrorNotClass(mentity
)
111 var mentities
= new HashSet[MEntity]
112 for mpropdef
in model
.collect_mpropdefs
(filter
) 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
121 results
= mentities
.to_a
126 # Retrieve all the mproperties that call `mentity`
128 # `mentity` must be a MProperty or a MPropDef.
132 autoinit
(model
, modelbuilder
, filter
, mentity
, mentity_name
, limit
, page
, count
, max
)
134 # ModelBuilder used to retrieve AST nodes
135 var modelbuilder
: ModelBuilder
137 redef fun init_results
do
138 if results
!= null then return new CmdSuccess
141 if not res
isa CmdSuccess then return res
142 var mentity
= self.mentity
.as(not null)
144 if mentity
isa MPropDef then mentity
= mentity
.mproperty
145 if not mentity
isa MProperty then return new ErrorNotProperty(mentity
)
147 var mentities
= new HashSet[MEntity]
148 for mpropdef
in model
.collect_mpropdefs
(filter
) 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
158 results
= mentities
.to_a
165 # Visitor looking for initialized `MType` (new T).
168 private class TypeInitVisitor
175 redef fun visit
(node
)
179 if not node
isa ANewExpr then return
180 var mtype
= node
.n_type
.mtype
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
191 # Visitor looking for calls to a `MProperty` (new T).
194 private class MPropertyCallVisitor
197 var calls
= new HashSet[MProperty]
198 redef fun visit
(node
)
201 if not node
isa ASendExpr then return
202 calls
.add node
.callsite
.as(not null).mproperty
206 # The MEntity is not a MClass or a MClassDef
213 redef fun to_s
do return "`{mentity.full_name}` is not a class"
216 # The MEntity is not a MProperty or a MClassDef
217 class ErrorNotProperty
223 redef fun to_s
do return "`{mentity.full_name}` is not a property"