neo_doxygen: Unlink `name` and `full_name`.
[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 location=(location: nullable Location) do
54 super
55 class_def.location = location
56 end
57
58 redef fun set_mdoc do
59 super
60 class_def["mdoc"] = doc
61 end
62
63 redef fun declare_super(id: String, full_name: String, prot: String, virt: String) do
64 class_def.declare_super(id, full_name, prot, virt)
65 end
66
67 redef fun declare_member(member: Member) do
68 class_def.declare_member(member)
69 end
70
71 # Append the specified type parameter.
72 fun add_type_parameter(parameter: TypeParameter) do
73 class_type.arguments.add(parameter)
74 end
75
76 redef fun put_in_graph do
77 super
78 class_type.put_in_graph
79 class_def.put_in_graph
80 end
81
82 redef fun put_edges do
83 super
84 graph.add_edge(self, "CLASSTYPE", class_type)
85 if arity > 0 then
86 var names = new JsonArray
87
88 for p in class_type.arguments do
89 names.add(p.name)
90 end
91 self["parameter_names"] = names
92 end
93 end
94 end
95
96 # The `MClassDef` node of a class.
97 class ClassDef
98 super CodeBlock
99
100 # The defined class.
101 var class_compound: ClassCompound
102
103 # The `model_id` of the base classes.
104 var supers: SimpleCollection[String] = new Array[String]
105
106 # The set of the introduced/redefined members.
107 #
108 # Includes inner classes.
109 #
110 # Filled by `declare_member` and `declare_class`.
111 #
112 # Note: `declare_class` is defined by the `inner_class` module.
113 #
114 # SEE: `declare_member`
115 # SEE: `declare_class`
116 var members: SimpleCollection[MemberOrInner] = new Array[MemberOrInner]
117
118 init do
119 super
120 self.labels.add("MClassDef")
121 self["is_intro"] = true
122 end
123
124 # Declare a base compound (usually, a base class).
125 #
126 # Parameters:
127 #
128 # * `id`: `model_id` of the base compound. May be empty.
129 # * `full_name`: qualified name of the base compound. May be empty.
130 # * `prot`: visibility (proctection) of the relationship.
131 # * `virt`: level of virtuality of the relationship.
132 fun declare_super(id: String, full_name: String, prot: String,
133 virt: String) do
134 # TODO prot, virt, full_name
135 if "" != id then
136 supers.add(id)
137 end
138 end
139
140 # Append the specified member.
141 fun declare_member(member: Member) do
142 members.add(member)
143 end
144
145 redef fun put_edges do
146 super
147 graph.add_edge(self, "BOUNDTYPE", class_compound.class_type)
148 graph.add_edge(self, "MCLASS", class_compound)
149 for s in supers do
150 graph.add_edge(self, "INHERITS", graph.by_id[s].as(ClassCompound).class_type)
151 end
152 for m in members do
153 if m.is_intro then
154 var intro = m.introducer.as(not null)
155 graph.add_edge(self, "INTRODUCES", intro)
156 graph.add_edge(intro, "INTRO_CLASSDEF", self)
157 end
158 graph.add_edge(self, "DECLARES", m)
159 end
160 end
161 end
162
163 # A type defined by a class.
164 class ClassType
165 super TypeEntity
166
167 # The associated class.
168 #
169 # You may use this attribute or `class_compound_id` to specify the class.
170 var class_compound: nullable ClassCompound = null is writable
171
172 # The `model_id` of the associated class.
173 #
174 # You may use this attribute or `class_compound` to specify the class.
175 var class_compound_id: String = "" is writable
176
177 # The type arguments or the type parameters.
178 var arguments = new Array[TypeEntity]
179
180 init do
181 super
182 self.labels.add("MClassType")
183 end
184
185 # Return the number of arguments.
186 fun arity: Int do return arguments.length
187
188 # Is the class generic?
189 fun is_generic: Bool do return arity > 0
190
191 redef fun put_in_graph do
192 super
193 if is_generic then
194 self.labels.add("MGenericType")
195 else
196 var i = self.labels.index_of("MGenericType")
197 if i >= 0 then self.labels.remove_at(i)
198 end
199 end
200
201 redef fun put_edges do
202 var cls = class_compound
203
204 if cls == null and class_compound_id != "" then
205 cls = graph.by_id[class_compound_id].as(ClassCompound)
206 end
207 assert cls != null
208
209 super
210 graph.add_edge(self, "CLASS", cls)
211 assert cls.arity == self.arity
212 for i in [0..arguments.length[ do
213 var a = arguments[i]
214 if cls.class_type != self then
215 a.name = cls.class_type.arguments[i].name
216 end
217 if a isa TypeParameter then
218 a.rank = i
219 graph.add_edge(a, "CLASS", cls)
220 end
221 graph.add_edge(self, "ARGUMENT", a)
222 end
223 end
224 end