dd73976da6c0d27bad619e956653f197a2a093d3
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.
16 module model
::class_compound
26 # The corresponding type.
28 # In the case of a generic class, defines bounds for type parameters.
29 var class_type
: ClassType is noinit
32 var class_def
: ClassDef is noinit
36 class_type
= new ClassType(graph
)
37 class_type
.class_compound
= self
38 class_def
= new ClassDef(graph
, self)
39 self.labels
.add
("MClass")
44 # Return the number of type parameters.
45 fun arity
: Int do return class_type
.arity
47 redef fun name
=(name
: String) do
49 class_type
.name
= name
53 redef fun full_name
=(full_name
: String) do
55 class_type
.full_name
= full_name
56 class_def
.full_name
= full_name
59 redef fun parent_name
=(parent_name
: String) do
61 class_type
.parent_name
= parent_name
62 class_def
.parent_name
= parent_name
65 redef fun location
=(location
: nullable Location) do
67 class_def
.location
= location
72 class_def
["mdoc"] = doc
75 redef fun declare_super
(id
: String, full_name
: String, prot
: String, virt
: String) do
76 class_def
.declare_super
(id
, full_name
, prot
, virt
)
79 redef fun declare_member
(member
: Member) do
80 class_def
.declare_member
(member
)
83 # Append the specified type parameter.
84 fun add_type_parameter
(parameter
: TypeParameter) do
85 class_type
.arguments
.add
(parameter
)
88 redef fun put_in_graph
do
90 class_type
.put_in_graph
91 class_def
.put_in_graph
94 redef fun put_edges
do
96 graph
.add_edge
(self, "CLASSTYPE", class_type
)
98 var names
= new JsonArray
100 for p
in class_type
.arguments
do
103 self["parameter_names"] = names
108 # The `MClassDef` node of a class.
113 var class_compound
: ClassCompound
115 # The `model_id` of the base classes.
116 var supers
: SimpleCollection[String] = new Array[String]
118 # The set of the introduced/redefined members.
120 # To ensure that the `full_name` of each member is correctly set,
121 # `declare_member` should be used to add each member.
122 var members
: SimpleCollection[Member] = new Array[Member]
126 self.labels
.add
("MClassDef")
127 self["is_intro"] = true
130 # Declare a base compound (usually, a base class).
134 # * `id`: `model_id` of the base compound. May be empty.
135 # * `full_name`: qualified name of the base compound. May be empty.
136 # * `prot`: visibility (proctection) of the relationship.
137 # * `virt`: level of virtuality of the relationship.
138 fun declare_super
(id
: String, full_name
: String, prot
: String,
140 # TODO prot, virt, full_name
146 # Append the specified member.
147 fun declare_member
(member
: Member) do
148 var full_name
= self["full_name"]
150 if full_name
!= null then
151 member
.parent_name
= full_name
.to_s
156 redef fun full_name
=(full_name
: String) do
159 m
.parent_name
= full_name
163 redef fun parent_name
=(parent_name
: String) do
166 m
.parent_name
= full_name
170 redef fun put_edges
do
172 graph
.add_edge
(self, "BOUNDTYPE", class_compound
.class_type
)
173 graph
.add_edge
(self, "MCLASS", class_compound
)
175 graph
.add_edge
(self, "INHERITS", graph
.by_id
[s
].as(ClassCompound).class_type
)
179 var intro
= m
.introducer
.as(not null)
180 graph
.add_edge
(self, "INTRODUCES", intro
)
181 graph
.add_edge
(intro
, "INTRO_CLASSDEF", self)
183 graph
.add_edge
(self, "DECLARES", m
)
188 # A type defined by a class.
192 # The associated class.
194 # You may use this attribute or `class_compound_id` to specify the class.
195 var class_compound
: nullable ClassCompound = null is writable
197 # The `model_id` of the associated class.
199 # You may use this attribute or `class_compound` to specify the class.
200 var class_compound_id
: String = "" is writable
202 # The type arguments or the type parameters.
203 var arguments
= new Array[TypeEntity]
207 self.labels
.add
("MClassType")
210 # Return the number of arguments.
211 fun arity
: Int do return arguments
.length
213 # Is the class generic?
214 fun is_generic
: Bool do return arity
> 0
216 redef fun put_in_graph
do
219 self.labels
.add
("MGenericType")
221 var i
= self.labels
.index_of
("MGenericType")
222 if i
>= 0 then self.labels
.remove_at
(i
)
226 redef fun put_edges
do
227 var cls
= class_compound
229 if cls
== null and class_compound_id
!= "" then
230 cls
= graph
.by_id
[class_compound_id
].as(ClassCompound)
235 graph
.add_edge
(self, "CLASS", cls
)
236 assert cls
.arity
== self.arity
237 for i
in [0..arguments
.length
[ do
239 if cls
.class_type
!= self then
240 a
.name
= cls
.class_type
.arguments
[i
].name
242 if a
isa TypeParameter then
244 graph
.add_edge
(a
, "CLASS", cls
)
246 graph
.add_edge
(self, "ARGUMENT", a
)