summaryrefslogtreecommitdiff
path: root/vala/valastruct.vala
blob: 74d8297dae9212ea38a8a313a486d0e075041dff (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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
/* valastruct.vala
 *
 * Copyright (C) 2006-2008  Jürg Billeter
 *
 * 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:
 * 	Jürg Billeter <j@bitron.ch>
 */

using GLib;
using Gee;

/**
 * Represents a struct declaration in the source code.
 */
public class Vala.Struct : TypeSymbol {
	private Gee.List<TypeParameter> type_parameters = new ArrayList<TypeParameter> ();
	private Gee.List<Constant> constants = new ArrayList<Constant> ();
	private Gee.List<Field> fields = new ArrayList<Field> ();
	private Gee.List<Method> methods = new ArrayList<Method> ();

	private Gee.List<DataType> base_types = new ArrayList<DataType> ();
	
	private string cname;
	private string const_cname;
	private string dup_function;
	private string free_function;
	private string type_id;
	private string lower_case_cprefix;
	private string lower_case_csuffix;
	private bool integer_type;
	private bool floating_type;
	private int rank;
	private string marshaller_type_name;
	private string get_value_function;
	private string set_value_function;
	private string default_value = null;
	private bool simple_type;
	private string? type_signature;
	private string copy_function;
	private string destroy_function;

	/**
	 * Specifies the default construction method.
	 */
	public Method default_construction_method { get; set; }
	
	/**
	 * Creates a new struct.
	 *
	 * @param name             type name
	 * @param source_reference reference to source code
	 * @return                 newly created struct
	 */
	public Struct (string name, SourceReference? source_reference = null) {
		base.init ();
		this.source_reference = source_reference;
		this.name = name;
	}

	/**
	 * Appends the specified parameter to the list of type parameters.
	 *
	 * @param p a type parameter
	 */
	public void add_type_parameter (TypeParameter p) {
		type_parameters.add (p);
		p.type = this;
		scope.add (p.name, p);
	}
	
	/**
	 * Returns a copy of the type parameter list.
	 *
	 * @return list of type parameters
	 */
	public Gee.List<TypeParameter> get_type_parameters () {
		return new ReadOnlyList<TypeParameter> (type_parameters);
	}

	/**
	 * Adds the specified constant as a member to this struct.
	 *
	 * @param c a constant
	 */
	public void add_constant (Constant c) {
		constants.add (c);
		scope.add (c.name, c);
	}
	
	/**
	 * Adds the specified field as a member to this struct.
	 *
	 * @param f a field
	 */
	public void add_field (Field f) {
		fields.add (f);
		scope.add (f.name, f);
	}
	
	/**
	 * Returns a copy of the list of fields.
	 *
	 * @return list of fields
	 */
	public Gee.List<Field> get_fields () {
		return new ReadOnlyList<Field> (fields);
	}

	/**
	 * Returns a copy of the list of constants.
	 *
	 * @return list of constants
	 */
	public Gee.List<Constant> get_constants () {
		return new ReadOnlyList<Constant> (constants);
	}

	/**
	 * Adds the specified method as a member to this struct.
	 *
	 * @param m a method
	 */
	public void add_method (Method m) {
		return_if_fail (m != null);
		
		if (m.binding == MemberBinding.INSTANCE || m is CreationMethod) {
			m.this_parameter = new FormalParameter ("this", new ValueType (this));
			m.scope.add (m.this_parameter.name, m.this_parameter);
		}
		if (!(m.return_type is VoidType) && m.get_postconditions ().size > 0) {
			m.result_var = new LocalVariable (m.return_type.copy (), "result");
			m.scope.add (m.result_var.name, m.result_var);
		}
		if (m is CreationMethod) {
			if (m.name == null) {
				default_construction_method = m;
				m.name = ".new";
			} else {
				m.name = ".new." + m.name;
			}

			var cm = (CreationMethod) m;
			if (cm.type_name != null && cm.type_name != name) {
				// type_name is null for constructors generated by GIdlParser
				Report.error (m.source_reference, "missing return type in method `%s.%s´".printf (get_full_name (), cm.type_name));
				m.error = true;
				return;
			}
		}

		methods.add (m);
		scope.add (m.name, m);
	}
	
	/**
	 * Returns a copy of the list of methods.
	 *
	 * @return list of methods
	 */
	public Gee.List<Method> get_methods () {
		return new ReadOnlyList<Method> (methods);
	}

	public override void accept (CodeVisitor visitor) {
		visitor.visit_struct (this);
	}

	public override void accept_children (CodeVisitor visitor) {
		foreach (DataType type in base_types) {
			type.accept (visitor);
		}

		foreach (TypeParameter p in type_parameters) {
			p.accept (visitor);
		}
		
		foreach (Field f in fields) {
			f.accept (visitor);
		}
		
		foreach (Constant c in constants) {
			c.accept (visitor);
		}
		
		foreach (Method m in methods) {
			m.accept (visitor);
		}
	}

	public override string get_cname (bool const_type = false) {
		if (const_type && const_cname != null) {
			return const_cname;
		}
		
		if (cname == null) {
			cname = get_default_cname ();
		}
		return cname;
	}

	/**
	 * Returns the default name of this struct as it is used in C code.
	 *
	 * @return the name to be used in C code by default
	 */
	public string get_default_cname () {
		return "%s%s".printf (parent_symbol.get_cprefix (), name);
	}

	private void set_cname (string cname) {
		this.cname = cname;
	}
	
	private void set_const_cname (string cname) {
		this.const_cname = cname;
	}
	
	public override string get_lower_case_cprefix () {
		if (lower_case_cprefix == null) {
			lower_case_cprefix = "%s_".printf (get_lower_case_cname (null));
		}
		return lower_case_cprefix;
	}
	
	private string get_lower_case_csuffix () {
		if (lower_case_csuffix == null) {
			lower_case_csuffix = camel_case_to_lower_case (name);
		}
		return lower_case_csuffix;
	}

	public override string? get_lower_case_cname (string? infix) {
		if (infix == null) {
			infix = "";
		}
		return "%s%s%s".printf (parent_symbol.get_lower_case_cprefix (), infix, get_lower_case_csuffix ());
	}
	
	public override string? get_upper_case_cname (string? infix) {
		return get_lower_case_cname (infix).up ();
	}

	public override string? get_type_signature () {
		if (type_signature == null) {
			var str = new StringBuilder ();
			str.append_c ('(');
			foreach (Field f in fields) {
				if (f.binding == MemberBinding.INSTANCE) {
					str.append (f.field_type.get_type_signature ());
				}
			}
			str.append_c (')');
			return str.str;
		}
		return type_signature;
	}

	/**
	 * Returns whether this is an integer type.
	 *
	 * @return true if this is an integer type, false otherwise
	 */
	public bool is_integer_type () {
		foreach (DataType type in base_types) {
			var st = type.data_type as Struct;
			if (st != null && st.is_integer_type ()) {
				return true;
			}
		}
		return integer_type;
	}
	
	/**
	 * Returns whether this is a floating point type.
	 *
	 * @return true if this is a floating point type, false otherwise
	 */
	public bool is_floating_type () {
		foreach (DataType type in base_types) {
			var st = type.data_type as Struct;
			if (st != null && st.is_floating_type ()) {
				return true;
			}
		}
		return floating_type;
	}
	
	/**
	 * Returns the rank of this integer or floating point type.
	 *
	 * @return the rank if this is an integer or floating point type
	 */
	public int get_rank () {
		return rank;
	}

	private void process_ccode_attribute (Attribute a) {
		if (a.has_argument ("cname")) {
			set_cname (a.get_string ("cname"));
		}
		if (a.has_argument ("const_cname")) {
			set_const_cname (a.get_string ("const_cname"));
		}
		if (a.has_argument ("cprefix")) {
			lower_case_cprefix = a.get_string ("cprefix");
		}
		if (a.has_argument ("cheader_filename")) {
			var val = a.get_string ("cheader_filename");
			foreach (string filename in val.split (",")) {
				add_cheader_filename (filename);
			}
		}
		if (a.has_argument ("type_id")) {
			set_type_id (a.get_string ("type_id"));
		}
		if (a.has_argument ("marshaller_type_name")) {
			set_marshaller_type_name (a.get_string ("marshaller_type_name"));
		}
		if (a.has_argument ("get_value_function")) {
			set_get_value_function (a.get_string ("get_value_function"));
		}
		if (a.has_argument ("set_value_function")) {
			set_set_value_function (a.get_string ("set_value_function"));
		}
		if (a.has_argument ("default_value")) {
			set_default_value (a.get_string ("default_value"));
		}
		if (a.has_argument ("type_signature")) {
			type_signature = a.get_string ("type_signature");
		}
		if (a.has_argument ("copy_function")) {
			set_copy_function (a.get_string ("copy_function"));
		}
		if (a.has_argument ("destroy_function")) {
			set_destroy_function (a.get_string ("destroy_function"));
		}
	}

	private void process_integer_type_attribute (Attribute a) {
		integer_type = true;
		if (a.has_argument ("rank")) {
			rank = a.get_integer ("rank");
		}
	}
	
	private void process_floating_type_attribute (Attribute a) {
		floating_type = true;
		if (a.has_argument ("rank")) {
			rank = a.get_integer ("rank");
		}
	}
	
	/**
	 * Process all associated attributes.
	 */
	public void process_attributes () {
		foreach (Attribute a in attributes) {
			if (a.name == "CCode") {
				process_ccode_attribute (a);
			} else if (a.name == "SimpleType") {
				simple_type = true;
			} else if (a.name == "IntegerType") {
				process_integer_type_attribute (a);
			} else if (a.name == "FloatingType") {
				process_floating_type_attribute (a);
			}
		}
	}

	public override string? get_type_id () {
		if (type_id == null) {
			foreach (DataType type in base_types) {
				var st = type.data_type as Struct;
				if (st != null) {
					return st.get_type_id ();;
				}
			}
			if (simple_type) {
				Report.error (source_reference, "The type `%s` doesn't declare a type id".printf (get_full_name ()));
			} else {
				return "G_TYPE_POINTER";
			}
		}
		return type_id;
	}
	
	public void set_type_id (string? name) {
		this.type_id = name;
	}

	public override string? get_marshaller_type_name () {
		if (marshaller_type_name == null) {
			foreach (DataType type in base_types) {
				var st = type.data_type as Struct;
				if (st != null) {
					return st.get_marshaller_type_name ();
				}
			}
			if (simple_type) {
				Report.error (source_reference, "The type `%s` doesn't declare a marshaller type name".printf (get_full_name ()));
			} else {
				return "POINTER";
			}
		}
		return marshaller_type_name;
	}
	
	private void set_marshaller_type_name (string? name) {
		this.marshaller_type_name = name;
	}
	
	public override string? get_get_value_function () {
		if (get_value_function == null) {
			foreach (DataType type in base_types) {
				var st = type.data_type as Struct;
				if (st != null) {
					return st.get_get_value_function ();
				}
			}
			if (simple_type) {
				Report.error (source_reference, "The value type `%s` doesn't declare a GValue get function".printf (get_full_name ()));
				return null;
			} else {
				return "g_value_get_pointer";
			}
		} else {
			return get_value_function;
		}
	}
	
	public override string? get_set_value_function () {
		if (set_value_function == null) {
			foreach (DataType type in base_types) {
				var st = type.data_type as Struct;
				if (st != null) {
					return st.get_set_value_function ();
				}
			}
			if (simple_type) {
				Report.error (source_reference, "The value type `%s` doesn't declare a GValue set function".printf (get_full_name ()));
				return null;
			} else {
				return "g_value_set_pointer";
			}
		} else {
			return set_value_function;
		}
	}
	
	private void set_get_value_function (string? function) {
		get_value_function = function;
	}
	
	private void set_set_value_function (string? function) {
		set_value_function = function;
	}

	public override string? get_default_value () {
		if (default_value != null) {
			return default_value;
		}

		// inherit default value from base type
		foreach (DataType type in base_types) {
			var st = type.data_type as Struct;
			if (st != null) {
				return st.get_default_value ();
			}
		}
		return null;
	}

	private void set_default_value (string? value) {
		default_value = value;
	}

	/**
	 * Adds the specified struct to the list of base types of this struct.
	 *
	 * @param type a class or interface reference
	 */
	public void add_base_type (DataType type) {
		base_types.add (type);
		type.parent_node = this;
	}

	/**
	 * Returns a copy of the base type list.
	 *
	 * @return list of base types
	 */
	public Gee.List<DataType> get_base_types () {
		return new ReadOnlyList<DataType> (base_types);
	}
	
	public override int get_type_parameter_index (string name) {
		int i = 0;
		
		foreach (TypeParameter p in type_parameters) {
			if (p.name == name) {
				return (i);
			}
			i++;
		}
		
		return -1;
	}

	/**
	 * Returns whether this struct is a simple type, i.e. whether
	 * instances are passed by value.
	 */
	public bool is_simple_type () {
		foreach (DataType type in base_types) {
			var st = type.data_type as Struct;
			if (st != null && st.is_simple_type ()) {
				return true;
			}
		}
		return simple_type;
	}

	/**
	 * Marks this struct as simple type, i.e. instances will be passed by
	 * value.
	 */
	public void set_simple_type (bool simple_type) {
		this.simple_type = simple_type;
	}

	public override void replace_type (DataType old_type, DataType new_type) {
		for (int i = 0; i < base_types.size; i++) {
			if (base_types[i] == old_type) {
				base_types[i] = new_type;
				return;
			}
		}
	}

	public override bool is_subtype_of (TypeSymbol t) {
		if (this == t) {
			return true;
		}

		foreach (DataType base_type in base_types) {
			if (base_type.data_type != null && base_type.data_type.is_subtype_of (t)) {
				return true;
			}
		}
		
		return false;
	}

	public string get_default_copy_function () {
		return get_lower_case_cprefix () + "copy";
	}

	public override string? get_copy_function () {
		if (copy_function == null) {
			copy_function = get_default_copy_function ();
		}
		return copy_function;
	}

	public void set_copy_function (string name) {
		this.copy_function = name;
	}

	public string get_default_destroy_function () {
		return get_lower_case_cprefix () + "destroy";
	}

	public override string? get_destroy_function () {
		if (destroy_function == null) {
			destroy_function = get_default_destroy_function ();
		}
		return destroy_function;
	}

	public void set_destroy_function (string name) {
		this.destroy_function = name;
	}

	public bool is_disposable () {
		if (destroy_function != null) {
			return true;
		}

		foreach (Field f in fields) {
			if (f.binding == MemberBinding.INSTANCE
			    && f.field_type.is_disposable ()) {
				return true;
			}
		}

		return false;
	}
}