58c25a4b5e40ae96041b619de1f332c2684572dd
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.
25 # The node used to represent the `MProperty` node.
27 # Only defined if `self` is at the root of a reimplementation graph, and
28 # only once `put_in_graph` is called.
29 var introducer
: nullable MemberIntroducer = null
31 # Members that this member redefines/reimplements.
32 var reimplemented
: SimpleCollection[String] = new Array[String]
36 self.labels
.add
("MPropDef")
39 # Set the static type.
40 fun static_type
=(static_type
: nullable TypeEntity) is abstract
42 # Get the static type.
43 fun static_type
: nullable TypeEntity is abstract
45 # Append the specified parameter to the signature.
46 fun add_parameter
(parameter
: MemberParameter) do end
48 # Append a member that is reimplemeneted by `self`.
49 fun reimplement
(parent
: String) do
50 reimplemented
.add
(parent
)
53 # Does this member introduce the property?
55 return reimplemented
.length
<= 0
58 redef fun put_in_graph
do
60 self["is_intro"] = is_intro
62 var visibility
= self["visibility"]
63 var full_name
= self["full_name"]
64 var name
= self["name"]
66 introducer
= create_introducer
67 if full_name
isa String then
68 introducer
.full_name
= full_name
69 else if name
isa String then
70 introducer
.name
= name
72 if visibility
isa String then
73 introducer
.visibility
= visibility
75 introducer
.put_in_graph
79 redef fun put_edges
do
81 var intro
= resolve_introducer
84 graph
.add_edge
(self, "DEFINES", intro
)
88 fun visibility
=(visibility
: String) do
89 self["visibility"] = visibility
90 if introducer
!= null then
91 introducer
.as(not null).visibility
= visibility
97 # Return `""` by default.
98 fun visibility
: String do
99 var visibility
= self["visibility"]
100 if visibility
isa String then return visibility
104 redef fun name
=(name
: String) do
106 if introducer
!= null then
107 introducer
.as(not null).name
= name
111 redef fun full_name
=(full_name
: String) do
113 if introducer
!= null then
114 introducer
.as(not null).full_name
= full_name
118 redef fun parent_name
=(parent_name
: String) do
120 if introducer
!= null then
121 introducer
.as(not null).parent_name
= parent_name
125 # Is the member abstract?
126 fun is_abstract
=(is_abstract
: Bool) do
127 self["is_abstract"] = is_abstract
130 # Create an instance of `MemberIntroducer` that will be linked to `self`.
131 protected fun create_introducer
: MemberIntroducer is abstract
133 # Find the nearest reimplementation root.
135 # var g = new ProjectGraph("foo")
136 # var m1 = new Attribute(g)
137 # var m2 = new Attribute(g)
138 # var m3 = new Attribute(g)
142 # m2.reimplement("1")
144 # assert m1.resolve_introducer == m1.introducer
145 # assert m2.resolve_introducer == m1.introducer
148 # m3.reimplement("3")
150 # assert m3.resolve_introducer == null
151 fun resolve_introducer
: nullable MemberIntroducer do
152 if introducer
== null then
153 var member_queue
= new List[String]
154 var visited
= new HashSet[Member]
157 member_queue
.add_all
(reimplemented
)
158 while not member_queue
.is_empty
do
159 member
= graph
.by_id
[member_queue
.shift
].as(Member)
160 if visited
.has
(member
) then
162 else if member
.is_intro
then
163 return member
.introducer
166 member_queue
.add_all
(member
.reimplemented
)
176 # An unrecognized member.
178 # Used to simplify the handling of ignored entities.
182 redef fun put_in_graph
do end
183 redef fun put_edges
do end
186 # A local definition of a method.
190 # The method’s signature.
191 var signature
: Signature is noinit
, writable
195 self.labels
.add
("MMethodDef")
196 self["is_intern"] = false # TODO
197 self["is_extern"] = false # TODO
198 signature
= new Signature(graph
)
202 # Set the return type.
203 redef fun static_type
=(static_type
: nullable TypeEntity) do
204 signature
.return_type
= static_type
207 # Get the return type.
208 redef fun static_type
: nullable TypeEntity do return signature
.return_type
210 redef fun add_parameter
(parameter
: MemberParameter) do
211 signature
.parameters
.add
(parameter
)
214 redef fun create_introducer
: MemberIntroducer do
215 return new MethodIntroducer(graph
)
218 redef fun put_in_graph
do
220 signature
.put_in_graph
223 redef fun put_edges
do
225 graph
.add_edge
(self, "SIGNATURE", signature
)
229 # A local definition of an attribute.
234 redef var static_type
: nullable TypeEntity = null is writable
238 self.labels
.add
("MAttributeDef")
241 redef fun create_introducer
: MemberIntroducer do
242 return new AttributeIntroducer(graph
)
245 redef fun put_in_graph
do
247 if static_type
!= null then
248 static_type
.as(not null).put_in_graph
252 redef fun put_edges
do
254 if static_type
!= null then
255 graph
.add_edge
(self, "TYPE", static_type
.as(not null))
260 # The `MProperty` node of a root of a reimplementation graph.
261 abstract class MemberIntroducer
266 self.labels
.add
("MProperty")
267 self["visibility"] = "public"
270 # Set the visibility.
271 fun visibility
=(visibility
: String) do
272 self["visibility"] = visibility
275 # Get the visibility.
277 # Return `""` by default.
278 fun visibility
: String do
279 var visibility
= self["visibility"]
280 if visibility
isa String then return visibility
285 # A `MProperty` node for a method.
286 class MethodIntroducer
287 super MemberIntroducer
291 self.labels
.add
("MMethod")
292 self["is_init"] = false # TODO
296 # A `MProperty` node for an attribute.
297 class AttributeIntroducer
298 super MemberIntroducer
302 self.labels
.add
("MAttribute")
307 # Append the specified member.
308 fun declare_member
(member
: Member) do end