Merge: neo_doxygen: Be more professional
[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 var class_compound: ClassCompound
113 var supers: SimpleCollection[String] = new Array[String]
114 var members: SimpleCollection[Member] = new Array[Member]
115
116 init do
117 super
118 self.labels.add("MClassDef")
119 self["is_intro"] = true
120 end
121
122 fun declare_super(id: String, full_name: String, prot: String,
123 virt: String) do
124 # TODO prot, virt, full_name
125 if "" != id then
126 supers.add(id)
127 end
128 end
129
130 fun declare_member(member: Member) do
131 var full_name = self["full_name"]
132
133 if full_name != null then
134 member.parent_name = full_name.to_s
135 end
136 members.add(member)
137 end
138
139 redef fun full_name=(full_name: String) do
140 super
141 for m in members do
142 m.parent_name = full_name
143 end
144 end
145
146 redef fun parent_name=(parent_name: String) do
147 super
148 for m in members do
149 m.parent_name = full_name
150 end
151 end
152
153 redef fun put_edges do
154 super
155 graph.add_edge(self, "BOUNDTYPE", class_compound.class_type)
156 graph.add_edge(self, "MCLASS", class_compound)
157 for s in supers do
158 graph.add_edge(self, "INHERITS", graph.by_id[s].as(ClassCompound).class_type)
159 end
160 for m in members do
161 if m.is_intro then
162 var intro = m.introducer.as(not null)
163 graph.add_edge(self, "INTRODUCES", intro)
164 graph.add_edge(intro, "INTRO_CLASSDEF", self)
165 end
166 graph.add_edge(self, "DECLARES", m)
167 end
168 end
169 end
170
171 # A type defined by a class.
172 class ClassType
173 super TypeEntity
174
175 # The associated class.
176 #
177 # You may use this attribute or `class_compound_id` to specify the class.
178 var class_compound: nullable ClassCompound = null is writable
179
180 # The `model_id` of the associated class.
181 #
182 # You may use this attribute or `class_compound` to specify the class.
183 var class_compound_id: String = "" is writable
184
185 # The type arguments or the type parameters.
186 var arguments = new Array[TypeEntity]
187
188 init do
189 super
190 self.labels.add("MClassType")
191 end
192
193 # Return the number of arguments.
194 fun arity: Int do return arguments.length
195
196 fun is_generic: Bool do return arity > 0
197
198 redef fun put_in_graph do
199 super
200 if is_generic then
201 self.labels.add("MGenericType")
202 else
203 var i = self.labels.index_of("MGenericType")
204 if i >= 0 then self.labels.remove_at(i)
205 end
206 end
207
208 redef fun put_edges do
209 var cls = class_compound
210
211 if cls == null and class_compound_id != "" then
212 cls = graph.by_id[class_compound_id].as(ClassCompound)
213 end
214 assert cls != null
215
216 super
217 graph.add_edge(self, "CLASS", cls)
218 assert cls.arity == self.arity
219 for i in [0..arguments.length[ do
220 var a = arguments[i]
221 if cls.class_type != self then
222 a.name = cls.class_type.arguments[i].name
223 end
224 if a isa TypeParameter then
225 a.rank = i
226 graph.add_edge(a, "CLASS", cls)
227 end
228 graph.add_edge(self, "ARGUMENT", a)
229 end
230 end
231 end