summaryrefslogtreecommitdiff
path: root/libvaladoc/api/class.vala
blob: 667565bbc15736fadd7042c910a3eac43ae2c426 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/* class.vala
 *
 * Copyright (C) 2008-2011  Florian Brosch
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 * Author:
 * 	Florian Brosch <flo.brosch@gmail.com>
 */


using Valadoc.Content;

/**
 * Represents a class declaration.
 */
public class Valadoc.Api.Class : TypeSymbol {
	private Vala.ArrayList<TypeReference> interfaces;

	private string? dbus_name;
	private string? take_value_function_cname;
	private string? get_value_function_cname;
	private string? set_value_function_cname;
	private string? unref_function_name;
	private string? ref_function_name;
	private string? free_function_name;
	private string? param_spec_function_name;
	private string? type_id;
	private string? is_class_type_macro_name;
	private string? class_type_macro_name;
	private string? class_macro_name;
	private string? private_cname;
	private string? cname;

	public Class (Node parent, SourceFile file, string name, Vala.SymbolAccessibility accessibility,
				  SourceComment? comment,
				  Vala.Class data)
	{
		bool is_basic_type = data.base_class == null && data.name == "string";

		base (parent, file, name, accessibility, comment, is_basic_type, data);

		this.interfaces = new Vala.ArrayList<TypeReference> ();

		if (!data.is_compact) {
			this.is_class_type_macro_name = Vala.get_ccode_class_type_check_function (data);
			this.class_type_macro_name = Vala.get_ccode_class_type_function (data);
			this.class_macro_name = Vala.get_ccode_class_get_function (data);
			this.private_cname = _get_private_cname (data);
		}
		this.dbus_name = Vala.GDBusModule.get_dbus_name (data);
		this.type_id = Vala.get_ccode_type_id (data);
		this.cname = Vala.get_ccode_name (data);

		this.param_spec_function_name = Vala.get_ccode_param_spec_function (data);

		this.unref_function_name = Vala.get_ccode_unref_function (data);
		this.ref_function_name = Vala.get_ccode_ref_function (data);
		this.free_function_name = (data.is_compact ? Vala.get_ccode_free_function (data) : null);

		this.take_value_function_cname = Vala.get_ccode_take_value_function (data);
		this.get_value_function_cname = Vala.get_ccode_get_value_function (data);
		this.set_value_function_cname = Vala.get_ccode_set_value_function (data);

		this.is_fundamental = data.is_fundamental ();
		this.is_abstract = data.is_abstract;
		this.is_sealed = data.is_sealed;
	}

	string? _get_private_cname (Vala.Class element) {
		if (element.is_compact) {
			return null;
		}

		string? cname = Vala.get_ccode_name (element);
		return (cname != null ? cname + "Private" : null);
	}

	/**
	 * Specifies the base class.
	 */
	public TypeReference? base_type {
		set;
		get;
	}

	/**
	 * Returns the name of this class as it is used in C.
	 */
	public string? get_cname () {
		return cname;
	}

	/**
	 * Returns the name of this class' private data structure as it is used in C.
	 */
	public string? get_private_cname () {
		return private_cname;
	}

	/**
	 * Returns the C symbol representing the runtime type id for this data type.
	 */
	public string? get_type_id () {
		return type_id;
	}

	/**
	 * Returns the C function name that increments the reference count of
	 * instances of this data type.
	 *
	 * @return the name of the C function or null if this data type does not
	 *         support reference counting
	 */
	public string? get_ref_function_cname () {
		return ref_function_name;
	}

	/**
	 * Returns the C function name that decrements the reference count of
	 * instances of this data type.
	 *
	 * @return the name of the C function or null if this data type does not
	 *         support reference counting
	 */
	public string? get_unref_function_cname () {
		return unref_function_name;
	}

	/**
	 * Returns the C function name that frees the
	 * instances of this data type.
	 *
	 * @return the name of the C function or null
	 */
	public string? get_free_function_name () {
		return free_function_name;
	}

	/**
	 * Returns the cname of the GValue parameter spec function.
	 */
	public string? get_param_spec_function_cname () {
		return param_spec_function_name;
	}

	/**
	 * Returns the cname of the GValue setter function.
	 */
	public string? get_set_value_function_cname () {
		return set_value_function_cname;
	}

	/**
	 * Returns the cname of the GValue getter function.
	 */
	public string? get_get_value_function_cname () {
		return get_value_function_cname;
	}

	/**
	 * Returns the cname of the GValue taker function.
	 */
	public string? get_take_value_function_cname () {
		return take_value_function_cname;
	}

	/**
	 * Returns the dbus-name.
	 */
	public string? get_dbus_name () {
		return dbus_name;
	}

	/**
	 * Gets the name of the GType macro which returns the class struct.
	 */
	public string get_class_macro_name () {
		return class_macro_name;
	}

	/**
	 * Gets the name of the GType macro which returns the type of the class.
	 */
	public string get_class_type_macro_name () {
		return class_type_macro_name;
	}

	/**
	 * Gets the name of the GType macro which returns whether a class instance is of a given type.
	 */
	public string get_is_class_type_macro_name () {
		return is_class_type_macro_name;
	}

	/**
	 * Returns a list of all newly implemented interfaces.
	 *
	 * @see get_full_implemented_interface_list
	 */
	public Vala.Collection<TypeReference> get_implemented_interface_list () {
		return this.interfaces;
	}

	private Vala.Collection<TypeReference> _full_implemented_interfaces = null;

	/**
	 * Returns a list of all implemented interfaces.
	 *
	 * @see get_implemented_interface_list
	 */
	public Vala.Collection<TypeReference> get_full_implemented_interface_list () {
		if (_full_implemented_interfaces == null) {
			_full_implemented_interfaces = new Vala.ArrayList<TypeReference> ();
			_full_implemented_interfaces.add_all (this.interfaces);

			if (base_type != null) {
				_full_implemented_interfaces.add_all (((Class) base_type.data_type).get_full_implemented_interface_list ());
			}
		}

		return _full_implemented_interfaces;
	}

	public void add_interface (TypeReference iface) {
		interfaces.add (iface);
	}

	/**
	 * Specifies whether this class is abstract.
	 */
	public bool is_abstract {
		private set;
		get;
	}

	/**
	 * Specifies whether this class is sealed. Sealed classes may not be
	 * sub-classed.
	 */
	public bool is_sealed {
		private set;
		get;
	}

	/**
	 * Specifies whether this class is fundamental.
	 */
	public bool is_fundamental {
		private set;
		get;
	}

	public bool is_compact {
		get {
			return ((Vala.Class) data).is_compact;
		}
	}

	/**
	 * {@inheritDoc}
	 */
	public override NodeType node_type { get { return NodeType.CLASS; } }

	/**
	 * {@inheritDoc}
	 */
	public override void accept (Visitor visitor) {
		visitor.visit_class (this);
	}

	private Vala.Set<Interface> _known_derived_interfaces = new Vala.HashSet<Interface> ();
	private Vala.Set<Class> _known_child_classes = new Vala.HashSet<Class> ();

	/**
	 * Returns a list of all known classes based on this class
	 */
	public Vala.Collection<Class> get_known_child_classes () {
		return _known_child_classes;
	}

	/**
	 * Returns a list of all known interfaces based on this class
	 */
	public Vala.Collection<Interface> get_known_derived_interfaces () {
		return _known_derived_interfaces;
	}

	public void register_derived_interface (Interface iface) {
		_known_derived_interfaces.add (iface);
	}

	public void register_child_class (Class cl) {
		if (this.base_type != null) {
			((Class) this.base_type.data_type).register_child_class (cl);
		}

		_known_child_classes.add (cl);
	}

	/**
	 * {@inheritDoc}
	 */
	protected override Inline build_signature () {
		var signature = new SignatureBuilder ();

		signature.append_keyword (accessibility.to_string ());
		if (is_abstract) {
			signature.append_keyword ("abstract");
		}
		if (is_sealed) {
			signature.append_keyword ("sealed");
		}
		signature.append_keyword ("class");
		signature.append_symbol (this);

		var type_parameters = get_children_by_type (NodeType.TYPE_PARAMETER, false);
		if (type_parameters.size > 0) {
			signature.append ("<", false);
			bool first = true;
			foreach (Item param in type_parameters) {
				if (!first) {
					signature.append (",", false);
				}
				signature.append_content (param.signature, false);
				first = false;
			}
			signature.append (">", false);
		}

		bool first = true;
		if (base_type != null) {
			signature.append (":");

			signature.append_content (base_type.signature);
			first = false;
		}

		if (interfaces.size > 0) {
			if (first) {
				signature.append (":");
			}

			foreach (Item implemented_interface in interfaces) {
				if (!first) {
					signature.append (",", false);
				}
				signature.append_content (implemented_interface.signature);
				first = false;
			}
		}

		return signature.get ();
	}
}