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