neo_doxygen: Add missing documentation.
[nit.git] / contrib / neo_doxygen / src / model / class_compound.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 # Nodes for classes.
16 module model::class_compound
17
18 import graph
19 import member
20 import type_entity
21
22 # A class.
23 class ClassCompound
24 super Compound
25
26 # The corresponding type.
27 #
28 # In the case of a generic class, defines bounds for type parameters.
29 var class_type: ClassType is noinit
30
31 # The definition.
32 var class_def: ClassDef is noinit
33
34 init do
35 super
36 class_type = new ClassType(graph)
37 class_type.class_compound = self
38 class_def = new ClassDef(graph, self)
39 self.labels.add("MClass")
40 kind = "class"
41 visibility = "public"
42 end
43
44 # Return the number of type parameters.
45 fun arity: Int do return class_type.arity
46
47 redef fun name=(name: String) do
48 super
49 class_type.name = name
50 class_def.name = name
51 end
52
53 redef fun full_name=(full_name: String) do
54 super
55 class_type.full_name = full_name
56 class_def.full_name = full_name
57 end
58
59 redef fun parent_name=(parent_name: String) do
60 super
61 class_type.parent_name = parent_name
62 class_def.parent_name = parent_name
63 end
64
65 redef fun location=(location: nullable Location) do
66 super
67 class_def.location = location
68 end
69
70 redef fun set_mdoc do
71 super
72 class_def["mdoc"] = doc
73 end
74
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)
77 end
78
79 redef fun declare_member(member: Member) do
80 class_def.declare_member(member)
81 end
82
83 # Append the specified type parameter.
84 fun add_type_parameter(parameter: TypeParameter) do
85 class_type.arguments.add(parameter)
86 end
87
88 redef fun put_in_graph do
89 super
90 class_type.put_in_graph
91 class_def.put_in_graph
92 end
93
94 redef fun put_edges do
95 super
96 graph.add_edge(self, "CLASSTYPE", class_type)
97 if arity > 0 then
98 var names = new JsonArray
99
100 for p in class_type.arguments do
101 names.add(p.name)
102 end
103 self["parameter_names"] = names
104 end
105 end
106 end
107
108 # The `MClassDef` node of a class.
109 class ClassDef
110 super CodeBlock
111
112 # The defined class.
113 var class_compound: ClassCompound
114
115 # The `model_id` of the base classes.
116 var supers: SimpleCollection[String] = new Array[String]
117
118 # The set of the introduced/redefined members.
119 #
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]
123
124 init do
125 super
126 self.labels.add("MClassDef")
127 self["is_intro"] = true
128 end
129
130 # Declare a base compound (usually, a base class).
131 #
132 # Parameters:
133 #
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,
139 virt: String) do
140 # TODO prot, virt, full_name
141 if "" != id then
142 supers.add(id)
143 end
144 end
145
146 # Append the specified member.
147 fun declare_member(member: Member) do
148 var full_name = self["full_name"]
149
150 if full_name != null then
151 member.parent_name = full_name.to_s
152 end
153 members.add(member)
154 end
155
156 redef fun full_name=(full_name: String) do
157 super
158 for m in members do
159 m.parent_name = full_name
160 end
161 end
162
163 redef fun parent_name=(parent_name: String) do
164 super
165 for m in members do
166 m.parent_name = full_name
167 end
168 end
169
170 redef fun put_edges do
171 super
172 graph.add_edge(self, "BOUNDTYPE", class_compound.class_type)
173 graph.add_edge(self, "MCLASS", class_compound)
174 for s in supers do
175 graph.add_edge(self, "INHERITS", graph.by_id[s].as(ClassCompound).class_type)
176 end
177 for m in members do
178 if m.is_intro then
179 var intro = m.introducer.as(not null)
180 graph.add_edge(self, "INTRODUCES", intro)
181 graph.add_edge(intro, "INTRO_CLASSDEF", self)
182 end
183 graph.add_edge(self, "DECLARES", m)
184 end
185 end
186 end
187
188 # A type defined by a class.
189 class ClassType
190 super TypeEntity
191
192 # The associated class.
193 #
194 # You may use this attribute or `class_compound_id` to specify the class.
195 var class_compound: nullable ClassCompound = null is writable
196
197 # The `model_id` of the associated class.
198 #
199 # You may use this attribute or `class_compound` to specify the class.
200 var class_compound_id: String = "" is writable
201
202 # The type arguments or the type parameters.
203 var arguments = new Array[TypeEntity]
204
205 init do
206 super
207 self.labels.add("MClassType")
208 end
209
210 # Return the number of arguments.
211 fun arity: Int do return arguments.length
212
213 # Is the class generic?
214 fun is_generic: Bool do return arity > 0
215
216 redef fun put_in_graph do
217 super
218 if is_generic then
219 self.labels.add("MGenericType")
220 else
221 var i = self.labels.index_of("MGenericType")
222 if i >= 0 then self.labels.remove_at(i)
223 end
224 end
225
226 redef fun put_edges do
227 var cls = class_compound
228
229 if cls == null and class_compound_id != "" then
230 cls = graph.by_id[class_compound_id].as(ClassCompound)
231 end
232 assert cls != null
233
234 super
235 graph.add_edge(self, "CLASS", cls)
236 assert cls.arity == self.arity
237 for i in [0..arguments.length[ do
238 var a = arguments[i]
239 if cls.class_type != self then
240 a.name = cls.class_type.arguments[i].name
241 end
242 if a isa TypeParameter then
243 a.rank = i
244 graph.add_edge(a, "CLASS", cls)
245 end
246 graph.add_edge(self, "ARGUMENT", a)
247 end
248 end
249 end