valagidlparser.vala 30.1 KB
Newer Older
Jürg Billeter's avatar
Jürg Billeter committed
1
2
/* valagidlparser.vala
 *
3
 * Copyright (C) 2006-2007  Jürg Billeter, Raffaele Sandrini
Jürg Billeter's avatar
Jürg Billeter committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 *
 * 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 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>
21
 *	Raffaele Sandrini <rasa@gmx.ch>
Jürg Billeter's avatar
Jürg Billeter committed
22
23
24
 */

using GLib;
25
using Gee;
Jürg Billeter's avatar
Jürg Billeter committed
26
27
28
29
30

/**
 * Code visitor parsing all GIDL files.
 */
public class Vala.GIdlParser : CodeVisitor {
31
32
	private CodeContext context;

33
34
	private SourceFile current_source_file;

Jürg Billeter's avatar
Jürg Billeter committed
35
36
	private SourceReference current_source_reference;
	
37
	private Namespace current_namespace;
Jürg Billeter's avatar
Jürg Billeter committed
38
	private DataType current_data_type;
39
40
	private Map<string,string> codenode_attributes_map;
	private Gee.Set<string> current_type_symbol_set;
Jürg Billeter's avatar
Jürg Billeter committed
41

42
43
	private Map<string,DataType> cname_type_map;

Jürg Billeter's avatar
Jürg Billeter committed
44
45
46
47
48
49
50
	/**
	 * Parse all source files in the specified code context and build a
	 * code tree.
	 *
	 * @param context a code context
	 */
	public void parse (CodeContext! context) {
51
52
		cname_type_map = new HashMap<string,DataType> (str_hash, str_equal);

53
		this.context = context;
Jürg Billeter's avatar
Jürg Billeter committed
54
		context.accept (this);
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

		cname_type_map = null;
	}

	public override void visit_namespace (Namespace! ns) {
		ns.accept_children (this);
	}

	public override void visit_class (Class! cl) {
		visit_type (cl);
	}

	public override void visit_struct (Struct! st) {
		visit_type (st);
	}

	public override void visit_interface (Interface! iface) {
		visit_type (iface);
	}

	public override void visit_enum (Enum! en) {
		visit_type (en);
	}

	public override void visit_callback (Callback! cb) {
		visit_type (cb);
	}

	private void visit_type (DataType! t) {
		if (!cname_type_map.contains (t.get_cname ())) {
			cname_type_map[t.get_cname ()] = t;
		}
Jürg Billeter's avatar
Jürg Billeter committed
87
88
	}

89
	public override void visit_source_file (SourceFile! source_file) {
Jürg Billeter's avatar
Jürg Billeter committed
90
91
92
93
94
95
96
		if (source_file.filename.has_suffix (".gidl")) {
			parse_file (source_file);
		}
	}
	
	private void parse_file (SourceFile! source_file) {
		string metadata_filename = "%s.metadata".printf (source_file.filename.ndup (source_file.filename.size () - ".gidl".size ()));
97
98
99

		current_source_file = source_file;

100
		codenode_attributes_map = new HashMap<string,string> (str_hash, str_equal);
Jürg Billeter's avatar
Jürg Billeter committed
101
		
102
		if (FileUtils.test (metadata_filename, FileTest.EXISTS)) {
103
104
			try {
				string metadata;
Jürg Billeter's avatar
Jürg Billeter committed
105
				ulong metadata_len;
106
				FileUtils.get_contents (metadata_filename, out metadata, out metadata_len);
Jürg Billeter's avatar
Jürg Billeter committed
107
				
108
109
110
111
112
113
				foreach (string line in metadata.split ("\n")) {
					var line_parts = line.split (" ", 2);
					if (line_parts[0] == null) {
						continue;
					}
					
114
					codenode_attributes_map.set (line_parts[0], line_parts[1]);
115
116
117
				}
			} catch (FileError e) {
				Report.error (null, "Unable to read metadata file: %s".printf (e.message));
Jürg Billeter's avatar
Jürg Billeter committed
118
119
120
			}
		}
	
121
122
123
124
125
		try {
			var modules = Idl.parse_file (source_file.filename);
			
			current_source_reference = new SourceReference (source_file);
			
126
			foreach (weak IdlModule module in modules) {
127
				var ns = parse_module (module);
128
129
130
				if (ns != null) {
					context.root.add_namespace (ns);
				}
131
132
133
			}
		} catch (MarkupError e) {
			stdout.printf ("error parsing GIDL file: %s\n", e.message);
Jürg Billeter's avatar
Jürg Billeter committed
134
135
		}
	}
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155

	private string! fix_type_name (string! type_name, IdlModule! module) {
		if (type_name.has_prefix (module.name)) {
			return type_name.offset (module.name.len ());
		} else if (module.name == "GLib" && type_name.has_prefix ("G")) {
			return type_name.offset (1);
		}
		return type_name;
	}

	private string! fix_const_name (string! const_name, IdlModule! module) {
		if (const_name.has_prefix (module.name.up () + "_")) {
			return const_name.offset (module.name.len () + 1);
		} else if (module.name == "GLib" && const_name.has_prefix ("G_")) {
			return const_name.offset (2);
		}
		return const_name;
	}

	private Namespace parse_module (IdlModule! module) {
156
157
158
159
160
161
162
163
		Symbol sym = context.root.scope.lookup (module.name);
		Namespace ns;
		if (sym is Namespace) {
			ns = (Namespace) sym;
			ns.pkg = false;
		} else {
			ns = new Namespace (module.name, current_source_reference);
		}
164
165
166

		current_namespace = ns;

Jürg Billeter's avatar
Jürg Billeter committed
167
168
169
170
171
172
		var attributes = get_attributes (ns.name);
		if (attributes != null) {
			foreach (string attr in attributes) {
				var nv = attr.split ("=", 2);
				if (nv[0] == "cheader_filename") {
					ns.set_cheader_filename (eval (nv[1]));
173
174
175
176
				} else if (nv[0] == "cprefix") {
					ns.set_cprefix (eval (nv[1]));
				} else if (nv[0] == "lower_case_cprefix") {
					ns.set_lower_case_cprefix (eval (nv[1]));
Jürg Billeter's avatar
Jürg Billeter committed
177
178
179
180
				}
			}
		}
		
181
		foreach (weak IdlNode node in module.entries) {
Jürg Billeter's avatar
Jürg Billeter committed
182
183
184
185
186
			if (node.type == IdlNodeTypeId.CALLBACK) {
				var cb = parse_callback ((IdlNodeFunction) node);
				if (cb == null) {
					continue;
				}
187
				cb.name = fix_type_name (cb.name, module);
Jürg Billeter's avatar
Jürg Billeter committed
188
				ns.add_callback (cb);
189
				current_source_file.add_node (cb);
Jürg Billeter's avatar
Jürg Billeter committed
190
191
192
193
194
			} else if (node.type == IdlNodeTypeId.STRUCT) {
				var st = parse_struct ((IdlNodeStruct) node);
				if (st == null) {
					continue;
				}
195
				st.name = fix_type_name (st.name, module);
Jürg Billeter's avatar
Jürg Billeter committed
196
				ns.add_struct (st);
197
				current_source_file.add_node (st);
Jürg Billeter's avatar
Jürg Billeter committed
198
199
			} else if (node.type == IdlNodeTypeId.BOXED) {
				var st = parse_boxed ((IdlNodeBoxed) node);
200
				st.name = fix_type_name (st.name, module);
Jürg Billeter's avatar
Jürg Billeter committed
201
				ns.add_struct (st);
202
				st.set_type_id (st.get_upper_case_cname ("TYPE_"));
203
				current_source_file.add_node (st);
Jürg Billeter's avatar
Jürg Billeter committed
204
205
			} else if (node.type == IdlNodeTypeId.ENUM) {
				var en = parse_enum ((IdlNodeEnum) node);
206
				en.name = fix_type_name (en.name, module);
Jürg Billeter's avatar
Jürg Billeter committed
207
				ns.add_enum (en);
208
				current_source_file.add_node (en);
Jürg Billeter's avatar
Jürg Billeter committed
209
210
			} else if (node.type == IdlNodeTypeId.OBJECT) {
				var cl = parse_object ((IdlNodeInterface) node);
Jürg Billeter's avatar
Jürg Billeter committed
211
212
213
				if (cl == null) {
					continue;
				}
214
				cl.name = fix_type_name (cl.name, module);
Jürg Billeter's avatar
Jürg Billeter committed
215
				ns.add_class (cl);
216
				current_source_file.add_node (cl);
Jürg Billeter's avatar
Jürg Billeter committed
217
218
			} else if (node.type == IdlNodeTypeId.INTERFACE) {
				var iface = parse_interface ((IdlNodeInterface) node);
219
				iface.name = fix_type_name (iface.name, module);
Jürg Billeter's avatar
Jürg Billeter committed
220
				ns.add_interface (iface);
221
				current_source_file.add_node (iface);
Jürg Billeter's avatar
Jürg Billeter committed
222
223
			} else if (node.type == IdlNodeTypeId.CONSTANT) {
				var c = parse_constant ((IdlNodeConstant) node);
224
				c.name = fix_const_name (c.name, module);
Jürg Billeter's avatar
Jürg Billeter committed
225
				ns.add_constant (c);
226
				current_source_file.add_node (c);
227
228
229
230
231
			} else if (node.type == IdlNodeTypeId.FUNCTION) {
				var m = parse_function ((IdlNodeFunction) node);
				if (m != null) {
					m.instance = false;
					ns.add_method (m);
232
					current_source_file.add_node (m);
233
				}
Jürg Billeter's avatar
Jürg Billeter committed
234
235
			}
		}
236
237
238

		current_namespace = null;

239
240
241
		if (sym is Namespace) {
			return null;
		}
Jürg Billeter's avatar
Jürg Billeter committed
242
243
244
		return ns;
	}
	
245
	private Callback parse_callback (IdlNodeFunction! f_node) {
246
		weak IdlNode node = (IdlNode) f_node;
Jürg Billeter's avatar
Jürg Billeter committed
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262

		var attributes = get_attributes (node.name);
		if (attributes != null) {
			foreach (string attr in attributes) {
				var nv = attr.split ("=", 2);
				if (nv[0] == "hidden") {
					if (eval (nv[1]) == "1") {
						return null;
					}
				}
			}
		}
	
		var cb = new Callback (node.name, parse_param (f_node.result), current_source_reference);
		cb.access = MemberAccessibility.PUBLIC;
		
263
		foreach (weak IdlNodeParam param in f_node.parameters) {
264
			weak IdlNode param_node = (IdlNode) param;
Jürg Billeter's avatar
Jürg Billeter committed
265
266
267
268
269
270
271
272
			
			var p = new FormalParameter (param_node.name, parse_param (param));
			cb.add_parameter (p);
		}
		
		return cb;
	}
	
273
	private Struct parse_struct (IdlNodeStruct! st_node) {
274
		weak IdlNode node = (IdlNode) st_node;
Jürg Billeter's avatar
Jürg Billeter committed
275
276
277
278
279
280
281
282
283
284
285
286
287
288
		
		if (st_node.deprecated) {
			return null;
		}
	
		var st = new Struct (node.name, current_source_reference);
		st.access = MemberAccessibility.PUBLIC;

		st.set_is_reference_type (true);
		
		var st_attributes = get_attributes (node.name);
		if (st_attributes != null) {
			foreach (string attr in st_attributes) {
				var nv = attr.split ("=", 2);
289
290
291
				if (nv[0] == "cheader_filename") {
					st.add_cheader_filename (eval (nv[1]));
				} else if (nv[0] == "is_value_type" && eval (nv[1]) == "1") {
Jürg Billeter's avatar
Jürg Billeter committed
292
					st.set_is_reference_type (false);
Jürg Billeter's avatar
Jürg Billeter committed
293
294
295
296
				} else if (nv[0] == "hidden") {
					if (eval (nv[1]) == "1") {
						return null;
					}
Jürg Billeter's avatar
Jürg Billeter committed
297
298
299
300
301
				}
			}
		}
		
		current_data_type = st;
302
303
304
305
306

		string ref_function = null;
		string unref_function = null;
		string free_function = null;

307
		foreach (weak IdlNode member in st_node.members) {
Jürg Billeter's avatar
Jürg Billeter committed
308
			if (member.type == IdlNodeTypeId.FUNCTION) {
309
310
311
312
313
314
315
316
317
318
319
				if (member.name == "ref") {
					ref_function = ((IdlNodeFunction) member).symbol;
				} else if (member.name == "unref") {
					unref_function = ((IdlNodeFunction) member).symbol;
				} else if (member.name == "free") {
					free_function = ((IdlNodeFunction) member).symbol;
				} else {
					var m = parse_function ((IdlNodeFunction) member);
					if (m != null) {
						st.add_method (m);
					}
Jürg Billeter's avatar
Jürg Billeter committed
320
321
322
323
324
325
326
327
328
				}
			} else if (member.type == IdlNodeTypeId.FIELD) {
				var f = parse_field ((IdlNodeField) member);
				if (f != null) {
					st.add_field (f);
				}
			}
		}

329
330
331
332
333
334
335
336
337
		if (ref_function != null) {
			st.set_dup_function (ref_function);
		}
		if (unref_function != null) {
			st.set_free_function (unref_function);
		} else if (free_function != null) {
			st.set_free_function (free_function);
		}

Jürg Billeter's avatar
Jürg Billeter committed
338
339
340
341
342
		current_data_type = null;
		
		return st;
	}
	
343
	private Struct parse_boxed (IdlNodeBoxed! boxed_node) {
344
		weak IdlNode node = (IdlNode) boxed_node;
Jürg Billeter's avatar
Jürg Billeter committed
345
346
347
	
		var st = new Struct (node.name, current_source_reference);
		st.access = MemberAccessibility.PUBLIC;
348
349
350

		st.set_is_reference_type (true);

351
352
353
354
		var st_attributes = get_attributes (node.name);
		if (st_attributes != null) {
			foreach (string attr in st_attributes) {
				var nv = attr.split ("=", 2);
355
356
357
				if (nv[0] == "cheader_filename") {
					st.add_cheader_filename (eval (nv[1]));
				} else if (nv[0] == "is_value_type" && eval (nv[1]) == "1") {
358
					st.set_is_reference_type (false);
359
360
361
362
				}
			}
		}
		
Jürg Billeter's avatar
Jürg Billeter committed
363
		current_data_type = st;
364
365
366
367
368

		string ref_function = null;
		string unref_function = null;
		string free_function = null;

369
		foreach (weak IdlNode member in boxed_node.members) {
Jürg Billeter's avatar
Jürg Billeter committed
370
			if (member.type == IdlNodeTypeId.FUNCTION) {
371
372
373
374
375
376
377
378
379
380
381
				if (member.name == "ref") {
					ref_function = ((IdlNodeFunction) member).symbol;
				} else if (member.name == "unref") {
					unref_function = ((IdlNodeFunction) member).symbol;
				} else if (member.name == "free") {
					free_function = ((IdlNodeFunction) member).symbol;
				} else {
					var m = parse_function ((IdlNodeFunction) member);
					if (m != null) {
						st.add_method (m);
					}
Jürg Billeter's avatar
Jürg Billeter committed
382
				}
383
384
385
386
387
			} else if (member.type == IdlNodeTypeId.FIELD) {
				var f = parse_field ((IdlNodeField) member);
				if (f != null) {
					st.add_field (f);
				}
Jürg Billeter's avatar
Jürg Billeter committed
388
389
390
			}
		}

391
392
393
394
395
396
397
398
399
		if (ref_function != null) {
			st.set_dup_function (ref_function);
		}
		if (unref_function != null) {
			st.set_free_function (unref_function);
		} else if (free_function != null) {
			st.set_free_function (free_function);
		}

Jürg Billeter's avatar
Jürg Billeter committed
400
401
402
403
404
		current_data_type = null;
		
		return st;
	}
	
405
	private Enum parse_enum (IdlNodeEnum! en_node) {
406
		weak IdlNode node = (IdlNode) en_node;
Jürg Billeter's avatar
Jürg Billeter committed
407
408
409
410
411
412
	
		var en = new Enum (node.name, current_source_reference);
		en.access = MemberAccessibility.PUBLIC;
		
		string common_prefix = null;
		
413
		foreach (weak IdlNode value in en_node.values) {
Jürg Billeter's avatar
Jürg Billeter committed
414
415
416
417
418
419
420
421
422
423
424
			if (common_prefix == null) {
				common_prefix = value.name;
				while (common_prefix.len () > 0 && !common_prefix.has_suffix ("_")) {
					// FIXME: could easily be made faster
					common_prefix = common_prefix.ndup (common_prefix.size () - 1);
				}
			} else {
				while (!value.name.has_prefix (common_prefix)) {
					common_prefix = common_prefix.ndup (common_prefix.size () - 1);
				}
			}
Jürg Billeter's avatar
Jürg Billeter committed
425
426
427
428
429
			while (common_prefix.len () > 0 && (!common_prefix.has_suffix ("_") ||
			       (value.name.offset (common_prefix.size ()).get_char ().isdigit ()) && (value.name.len () - common_prefix.len ()) <= 1)) {
				// enum values may not consist solely of digits
				common_prefix = common_prefix.ndup (common_prefix.size () - 1);
			}
Jürg Billeter's avatar
Jürg Billeter committed
430
431
432
433
		}
		
		en.set_cprefix (common_prefix);
		
434
		foreach (weak IdlNode value2 in en_node.values) {
Jürg Billeter's avatar
Jürg Billeter committed
435
436
437
438
439
440
441
			var ev = new EnumValue (value2.name.offset (common_prefix.len ()));
			en.add_value (ev);
		}
		
		return en;
	}
	
442
	private Class parse_object (IdlNodeInterface! node) {
Jürg Billeter's avatar
Jürg Billeter committed
443
444
445
		var cl = new Class (node.gtype_name, current_source_reference);
		cl.access = MemberAccessibility.PUBLIC;
		
446
447
448
449
450
451
		var attributes = get_attributes (cl.name);
		if (attributes != null) {
			foreach (string attr in attributes) {
				var nv = attr.split ("=", 2);
				if (nv[0] == "cheader_filename") {
					cl.add_cheader_filename (eval (nv[1]));
Jürg Billeter's avatar
Jürg Billeter committed
452
453
454
455
				} else if (nv[0] == "hidden") {
					if (eval (nv[1]) == "1") {
						return null;
					}
456
457
458
459
				}
			}
		}
		
Jürg Billeter's avatar
Jürg Billeter committed
460
461
462
463
464
465
466
467
468
469
470
471
472
473
		if (node.parent != null) {
			var parent = new TypeReference ();
			parse_type_string (parent, node.parent);
			cl.add_base_type (parent);
		}
		
		foreach (string iface_name in node.interfaces) {
			var iface = new TypeReference ();
			parse_type_string (iface, iface_name);
			cl.add_base_type (iface);
		}
		
		current_data_type = cl;
		
474
475
		current_type_symbol_set = new HashSet<string> (str_hash, str_equal);
		var current_type_vfunc_map = new HashMap<string,string> (str_hash, str_equal);
476
		
477
		foreach (weak IdlNode member in node.members) {
478
			if (member.type == IdlNodeTypeId.VFUNC) {
479
				current_type_vfunc_map.set (member.name, "1");
480
481
			}
		}
Jürg Billeter's avatar
Jürg Billeter committed
482

483
		foreach (weak IdlNode member in node.members) {
Jürg Billeter's avatar
Jürg Billeter committed
484
			if (member.type == IdlNodeTypeId.FUNCTION) {
485
				bool is_virtual = current_type_vfunc_map.get (member.name) != null;
486
487
				
				var m = parse_function ((IdlNodeFunction) member, is_virtual);
Jürg Billeter's avatar
Jürg Billeter committed
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
				if (m != null) {
					cl.add_method (m);
				}
			} else if (member.type == IdlNodeTypeId.PROPERTY) {
				var prop = parse_property ((IdlNodeProperty) member);
				if (prop != null) {
					cl.add_property (prop);
				}
			} else if (member.type == IdlNodeTypeId.SIGNAL) {
				var sig = parse_signal ((IdlNodeSignal) member);
				if (sig != null) {
					cl.add_signal (sig);
				}
			}
		}
		
504
		foreach (weak IdlNode member in node.members) {
Jürg Billeter's avatar
Jürg Billeter committed
505
			if (member.type == IdlNodeTypeId.FIELD) {
506
				if (!current_type_symbol_set.contains (member.name)) {
Jürg Billeter's avatar
Jürg Billeter committed
507
508
509
510
511
512
513
514
515
516
517
					var f = parse_field ((IdlNodeField) member);
					if (f != null) {
						cl.add_field (f);
					}
				}
			}
		}
		
		foreach (Property prop in cl.get_properties ()) {
			var getter = "get_%s".printf (prop.name);
			
518
			if (prop.get_accessor != null && !current_type_symbol_set.contains (getter)) {
Jürg Billeter's avatar
Jürg Billeter committed
519
520
521
522
523
				prop.no_accessor_method = true;
			}
			
			var setter = "set_%s".printf (prop.name);
			
524
			if (prop.set_accessor != null && !current_type_symbol_set.contains (setter)) {
Jürg Billeter's avatar
Jürg Billeter committed
525
526
527
528
529
				prop.no_accessor_method = true;
			}
		}
		
		current_data_type = null;
530
		current_type_symbol_set = null;
Jürg Billeter's avatar
Jürg Billeter committed
531
532
533
		
		return cl;
	}
534
535

	private Interface parse_interface (IdlNodeInterface! node) {
Jürg Billeter's avatar
Jürg Billeter committed
536
537
538
		var iface = new Interface (node.gtype_name, current_source_reference);
		iface.access = MemberAccessibility.PUBLIC;
		
539
540
541
542
543
544
545
546
547
548
		var attributes = get_attributes (iface.name);
		if (attributes != null) {
			foreach (string attr in attributes) {
				var nv = attr.split ("=", 2);
				if (nv[0] == "cheader_filename") {
					iface.add_cheader_filename (eval (nv[1]));
				}
			}
		}
		
Jürg Billeter's avatar
Jürg Billeter committed
549
550
551
		foreach (string prereq_name in node.prerequisites) {
			var prereq = new TypeReference ();
			parse_type_string (prereq, prereq_name);
552
			iface.add_prerequisite (prereq);
Jürg Billeter's avatar
Jürg Billeter committed
553
554
555
556
		}

		current_data_type = iface;

557
		var current_type_vfunc_map = new HashMap<string,string> (str_hash, str_equal);
558
		foreach (weak IdlNode member in node.members) {
559
			if (member.type == IdlNodeTypeId.VFUNC) {
560
				current_type_vfunc_map.set (member.name, "1");
561
562
563
			}
		}

564
		foreach (weak IdlNode member in node.members) {
Jürg Billeter's avatar
Jürg Billeter committed
565
			if (member.type == IdlNodeTypeId.FUNCTION) {
566
				bool is_virtual = current_type_vfunc_map.get (member.name) != null;
567
				
Jürg Billeter's avatar
Jürg Billeter committed
568
				var m = parse_function ((IdlNodeFunction) member, is_virtual, true);
Jürg Billeter's avatar
Jürg Billeter committed
569
570
571
				if (m != null) {
					iface.add_method (m);
				}
572
573
574
575
576
			} else if (member.type == IdlNodeTypeId.SIGNAL) {
				var sig = parse_signal ((IdlNodeSignal) member);
				if (sig != null) {
					iface.add_signal (sig);
				}
Jürg Billeter's avatar
Jürg Billeter committed
577
578
579
580
581
582
583
584
			}
		}

		current_data_type = null;
		
		return iface;
	}
	
585
	private TypeReference parse_type (IdlNodeType! type_node) {
Jürg Billeter's avatar
Jürg Billeter committed
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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
		var type = new TypeReference ();
		if (type_node.tag == TypeTag.VOID) {
			if (type_node.is_pointer) {
				type.type_name = "pointer";
			} else {
				type.type_name = "void";
			}
		} else if (type_node.tag == TypeTag.BOOLEAN) {
			type.type_name = "bool";
		} else if (type_node.tag == TypeTag.INT8) {
			type.type_name = "char";
		} else if (type_node.tag == TypeTag.UINT8) {
			type.type_name = "uchar";
		} else if (type_node.tag == TypeTag.INT16) {
			type.type_name = "short";
		} else if (type_node.tag == TypeTag.UINT16) {
			type.type_name = "ushort";
		} else if (type_node.tag == TypeTag.INT32) {
			type.type_name = "int";
		} else if (type_node.tag == TypeTag.UINT32) {
			type.type_name = "uint";
		} else if (type_node.tag == TypeTag.INT64) {
			type.type_name = "int64";
		} else if (type_node.tag == TypeTag.UINT64) {
			type.type_name = "uint64";
		} else if (type_node.tag == TypeTag.INT) {
			type.type_name = "int";
		} else if (type_node.tag == TypeTag.UINT) {
			type.type_name = "uint";
		} else if (type_node.tag == TypeTag.LONG) {
			type.type_name = "long";
		} else if (type_node.tag == TypeTag.ULONG) {
			type.type_name = "ulong";
		} else if (type_node.tag == TypeTag.SSIZE) {
			type.type_name = "long";
		} else if (type_node.tag == TypeTag.SIZE) {
			type.type_name = "ulong";
		} else if (type_node.tag == TypeTag.FLOAT) {
			type.type_name = "float";
		} else if (type_node.tag == TypeTag.DOUBLE) {
			type.type_name = "double";
		} else if (type_node.tag == TypeTag.UTF8) {
			type.type_name = "string";
		} else if (type_node.tag == TypeTag.FILENAME) {
			type.type_name = "string";
		} else if (type_node.tag == TypeTag.ARRAY) {
			type = parse_type (type_node.parameter_type1);
			type.array_rank = 1;
		} else if (type_node.tag == TypeTag.LIST) {
			type.namespace_name = "GLib";
			type.type_name = "List";
		} else if (type_node.tag == TypeTag.SLIST) {
			type.namespace_name = "GLib";
			type.type_name = "SList";
		} else if (type_node.tag == TypeTag.HASH) {
			type.namespace_name = "GLib";
			type.type_name = "HashTable";
		} else if (type_node.tag == TypeTag.ERROR) {
			type.namespace_name = "GLib";
			type.type_name = "Error";
		} else if (type_node.is_interface) {
			var n = type_node.@interface;
			
			if (n == "") {
				return null;
			}
			
			if (n.has_prefix ("const-")) {
				n = n.offset ("const-".len ());
			}

			if (type_node.is_pointer &&
			    (n == "gchar" || n == "char")) {
				type.type_name = "string";
			} else if (n == "gunichar") {
				type.type_name = "unichar";
			} else if (n == "gchar") {
				type.type_name = "char";
			} else if (n == "guchar" || n == "guint8") {
				type.type_name = "uchar";
666
667
668
				if (type_node.is_pointer) {
					type.array_rank = 1;
				}
Jürg Billeter's avatar
Jürg Billeter committed
669
670
671
672
			} else if (n == "gushort") {
				type.type_name = "ushort";
			} else if (n == "gshort") {
				type.type_name = "short";
673
			} else if (n == "gconstpointer" || n == "void") {
Jürg Billeter's avatar
Jürg Billeter committed
674
				type.type_name = "pointer";
675
676
			} else if (n == "goffset") {
				type.type_name = "int64";
Jürg Billeter's avatar
Jürg Billeter committed
677
678
679
680
681
			} else if (n == "value_array") {
				type.namespace_name = "GLib";
				type.type_name = "ValueArray";
			} else if (n == "time_t") {
				type.type_name = "ulong";
Jürg Billeter's avatar
Jürg Billeter committed
682
			} else if (n == "gint" || n == "pid_t") {
683
				type.type_name = "int";
684
685
			} else if (n == "unsigned" || n == "unsigned-int") {
				type.type_name = "uint";
Jürg Billeter's avatar
Jürg Billeter committed
686
687
			} else if (n == "FILE") {
				type.namespace_name = "GLib";
688
				type.type_name = "FileStream";
689
690
691
692
693
694
			} else if (n == "GType") {
				type.namespace_name = "GLib";
				type.type_name = "Type";
				if (type_node.is_pointer) {
					type.array_rank = 1;
				}
Jürg Billeter's avatar
Jürg Billeter committed
695
696
			} else {
				parse_type_string (type, n);
697
				if (type_node.is_pointer && is_value_type (n)) {
698
					type.is_out = true;
699
				}
Jürg Billeter's avatar
Jürg Billeter committed
700
701
702
703
704
705
706
			}
		} else {
			stdout.printf ("%d\n", type_node.tag);
		}
		return type;
	}
	
707
708
709
	private bool is_value_type (string! type_name) {
		var type_attributes = get_attributes (type_name);
		if (type_attributes != null) {
710
			// type in the same package
711
712
713
714
715
716
			foreach (string attr in type_attributes) {
				var nv = attr.split ("=", 2);
				if (nv[0] == "is_value_type" && eval (nv[1]) == "1") {
					return true;
				}
			}
717
718
719
720
721
722
		} else {
			// type in a dependency package
			var dt = cname_type_map[type_name];
			if (dt != null) {
				return !dt.is_reference_type ();
			}
723
724
725
726
727
		}

		return false;
	}
	
Jürg Billeter's avatar
Jürg Billeter committed
728
	private void parse_type_string (TypeReference! type, string! n) {
729
730
731
732
733
734
735
736
		var dt = cname_type_map[n];
		if (dt != null) {
			type.namespace_name = dt.parent_symbol.name;
			type.type_name = dt.name;
			return;
		}

		if (n == "HFONT" || n == "HGLOBAL" || n == "GStaticRecMutex" || n.has_suffix ("Class") || n == "va_list" || n.has_prefix ("LOGFONT") || n.has_prefix ("xml") || n == "GdkNativeWindow" || n == "GdkXEvent" || n == "GtkTextLayout" || n == "GstClockID" || n.has_prefix ("GstXml")) {
Jürg Billeter's avatar
Jürg Billeter committed
737
738
739
740
741
742
743
744
745
746
747
748
			// unsupported
			type.type_name = "pointer";
		} else if (n.has_prefix ("cairo")) {
			type.namespace_name = "Cairo";
			if (n == "cairo_t") {
				type.type_name = "Context";
			} else if (n == "cairo_surface_t") {
				type.type_name = "Surface";
			} else {
				type.namespace_name = null;
				type.type_name = "pointer";
			}
749
750
751
752
753
754
755
756
757
758
759
760
761
		} else if (n == "AtkAttributeSet") {
			type.namespace_name = "GLib";
			type.type_name = "SList";
		} else if (n == "GstClockTime") {
			type.type_name = "uint64";
		} else if (n == "GstClockTimeDiff") {
			type.type_name = "int64";
		} else if (n == "PangoGlyph") {
			type.type_name = "uint";
		} else if (n == "PangoGlyphUnit") {
			type.type_name = "int";
		} else if (n == "ClutterFixed" || n == "ClutterUnit" || n == "ClutterAngle") {
			type.type_name = "int32";
Jürg Billeter's avatar
Jürg Billeter committed
762
763
764
		} else if (n == "SoupProtocol") {
			type.namespace_name = "GLib";
			type.type_name = "Quark";
765
766
767
768
769
770
		} else if (n == "GStrv") {
			type.type_name = "string";
			type.array_rank = 1;
		} else if (n.has_prefix (current_namespace.name)) {
			type.namespace_name = current_namespace.name;
			type.type_name = n.offset (current_namespace.name.len ());
Jürg Billeter's avatar
Jürg Billeter committed
771
772
		} else if (n.has_prefix ("G")) {
			type.namespace_name = "GLib";
773
			type.type_name = n.offset (1);
Jürg Billeter's avatar
Jürg Billeter committed
774
775
776
777
778
779
780
781
782
783
784
		} else {
			var name_parts = n.split (".", 2);
			if (name_parts[1] == null) {
				type.type_name = name_parts[0];
			} else {
				type.namespace_name = name_parts[0];
				type.type_name = name_parts[1];
			}
		}
	}
	
785
	private TypeReference parse_param (IdlNodeParam! param) {
Jürg Billeter's avatar
Jürg Billeter committed
786
787
788
789
790
791
792
793
		var type = parse_type (param.type);

		// disable for now as null_ok not yet correctly set
		// type.non_null = !param.null_ok;
		
		return type;
	}
	
Jürg Billeter's avatar
Jürg Billeter committed
794
	private Method parse_function (IdlNodeFunction! f, bool is_virtual = false, bool is_interface = false) {
795
		weak IdlNode node = (IdlNode) f;
Jürg Billeter's avatar
Jürg Billeter committed
796
797
798
799
800
801
802
803
804
805
		
		if (f.deprecated) {
			return null;
		}
	
		TypeReference return_type = null;
		if (f.result != null) {
			return_type = parse_param (f.result);
		}
		
806
		Method m;
Jürg Billeter's avatar
Jürg Billeter committed
807
		if (!is_interface && (f.is_constructor || node.name.has_prefix ("new"))) {
808
809
810
811
812
813
814
815
816
			m = new CreationMethod (node.name, current_source_reference);
			if (m.name == "new") {
				m.name = null;
			} else if (m.name.has_prefix ("new_")) {
				m.name = m.name.offset ("new_".len ());
			}
		} else {
			m = new Method (node.name, return_type, current_source_reference);
		}
Jürg Billeter's avatar
Jürg Billeter committed
817
		m.access = MemberAccessibility.PUBLIC;
818
819
820

		m.is_virtual = is_virtual && !is_interface;
		m.is_abstract = is_virtual && is_interface;
821
		
822
823
824
		// GIDL generator can't provide array parameter information yet
		m.no_array_length = true;
		
825
826
		if (current_type_symbol_set != null) {
			current_type_symbol_set.add (node.name);
Jürg Billeter's avatar
Jürg Billeter committed
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
		}
		
		if (current_data_type != null) {
			var sig_attributes = get_attributes ("%s::%s".printf (current_data_type.name, node.name));
			if (sig_attributes != null) {
				foreach (string attr in sig_attributes) {
					var nv = attr.split ("=", 2);
					if (nv[0] == "has_emitter" && eval (nv[1]) == "1") {
						return null;
					}
				}
			}
		}
		
		bool add_ellipsis = false;

		var attributes = get_attributes (f.symbol);
		if (attributes != null) {
			foreach (string attr in attributes) {
				var nv = attr.split ("=", 2);
847
848
849
850
				if (nv[0] == "name") {
					m.set_cname (m.name);
					m.name = eval (nv[1]);
				} else if (nv[0] == "hidden") {
Jürg Billeter's avatar
Jürg Billeter committed
851
852
853
854
855
856
857
					if (eval (nv[1]) == "1") {
						return null;
					}
				} else if (nv[0] == "ellipsis") {
					if (eval (nv[1]) == "1") {
						add_ellipsis = true;
					}
858
859
860
861
				} else if (nv[0] == "transfer_ownership") {
					if (eval (nv[1]) == "1") {
						return_type.transfers_ownership = true;
					}
862
863
				} else if (nv[0] == "sentinel") {
					m.sentinel = eval (nv[1]);
Jürg Billeter's avatar
Jürg Billeter committed
864
865
866
867
				}
			}
		}
		
868
869
		m.set_cname (f.symbol);
		
Jürg Billeter's avatar
Jürg Billeter committed
870
		bool first = true;
871
		FormalParameter last_param = null;
872
		foreach (weak IdlNodeParam param in f.parameters) {
873
			weak IdlNode param_node = (IdlNode) param;
Jürg Billeter's avatar
Jürg Billeter committed
874
875
876
877
878
879
880
881
			
			if (first) {
				first = false;
				if (current_data_type != null &&
				    param.type.is_interface &&
				    (param_node.name == "self" ||
				     param.type.@interface.has_suffix (current_data_type.name))) {
					// instance method
882
883
884
885
886
887
					
					if (!current_data_type.is_reference_type () &&
					    param.type.is_pointer) {
						m.instance_by_reference = true;
					}
					
Jürg Billeter's avatar
Jürg Billeter committed
888
889
890
891
892
893
894
895
896
					continue;
				} else {
					// static method
					m.instance = false;
				}
			}
			
			var p = new FormalParameter (param_node.name, parse_param (param));
			m.add_parameter (p);
897
898
899
900

			if (last_param != null && p.name == "n_" + last_param.name) {
				// last_param is array, p is array length
				last_param.type_reference.array_rank = 1;
901
				last_param.type_reference.is_out = false;
902
903
904
			}

			last_param = p;
Jürg Billeter's avatar
Jürg Billeter committed
905
906
		}
		
907
908
909
910
		if (first) {
			// no parameters => static method
			m.instance = false;
		}
911
912
913
914

		if (last_param != null && last_param.name.has_prefix ("first_")) {
			last_param.ellipsis = true;
		} else if (add_ellipsis) {
Jürg Billeter's avatar
Jürg Billeter committed
915
916
917
918
919
920
			m.add_parameter (new FormalParameter.with_ellipsis ());
		}
		
		return m;
	}
	
921
	private string! fix_prop_name (string name) {
Jürg Billeter's avatar
Jürg Billeter committed
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
		var str = new String ();
		
		string i = name;
		
		while (i.len () > 0) {
			unichar c = i.get_char ();
			if (c == '-') {
				str.append_c ('_');
			} else {
				str.append_unichar (c);
			}
			
			i = i.next_char ();
		}
		
		return str.str;
	}

940
	private Property parse_property (IdlNodeProperty! prop_node) {
941
		weak IdlNode node = (IdlNode) prop_node;
Jürg Billeter's avatar
Jürg Billeter committed
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
		
		if (prop_node.deprecated) {
			return null;
		}
		
		if (!prop_node.readable && !prop_node.writable) {
			// buggy GIDL definition
			prop_node.readable = true;
			prop_node.writable = true;
		}
		
		PropertyAccessor get_acc = null;
		PropertyAccessor set_acc = null;
		if (prop_node.readable) {
			get_acc = new PropertyAccessor (true, false, false, null, null);
		}
		if (prop_node.writable) {
			set_acc = new PropertyAccessor (false, false, false, null, null);
			if (prop_node.construct_only) {
				set_acc.construction = true;
			} else {
				set_acc.writable = true;
				set_acc.construction = prop_node.@construct;
			}
		}
		
		var prop = new Property (fix_prop_name (node.name), parse_type (prop_node.type), get_acc, set_acc, current_source_reference);
		prop.access = MemberAccessibility.PUBLIC;
		prop.interface_only = true;
971
972
973
974
975
976
977
978
979
980
981
982
983

		var attributes = get_attributes ("%s:%s".printf (current_data_type.name, node.name));
		if (attributes != null) {
			foreach (string attr in attributes) {
				var nv = attr.split ("=", 2);
				if (nv[0] == "hidden") {
					if (eval (nv[1]) == "1") {
						return null;
					}
				}
			}
		}

984
985
		if (current_type_symbol_set != null) {
			current_type_symbol_set.add (prop.name);
Jürg Billeter's avatar
Jürg Billeter committed
986
987
988
989
990
		}
		
		return prop;
	}

991
	private Constant parse_constant (IdlNodeConstant! const_node) {
992
		weak IdlNode node = (IdlNode) const_node;
Jürg Billeter's avatar
Jürg Billeter committed
993
994
995
996
997
998
999
1000
1001
1002
1003
		
		var type = parse_type (const_node.type);
		if (type == null) {
			return null;
		}
		
		var c = new Constant (node.name, type, null, current_source_reference);
		
		return c;
	}

1004
	private Field parse_field (IdlNodeField! field_node) {
1005
		weak IdlNode node = (IdlNode) field_node;
Jürg Billeter's avatar
Jürg Billeter committed
1006
1007
1008
1009
1010
1011
1012
1013
1014
		
		var type = parse_type (field_node.type);
		if (type == null) {
			return null;
		}
		
		if (!field_node.readable) {
			return null;
		}
Jürg Billeter's avatar
Jürg Billeter committed
1015
1016
1017
1018
1019
1020
1021
1022
1023

		var attributes = get_attributes ("%s.%s".printf (current_data_type.name, node.name));
		if (attributes != null) {
			foreach (string attr in attributes) {
				var nv = attr.split ("=", 2);
				if (nv[0] == "hidden") {
					if (eval (nv[1]) == "1") {
						return null;
					}
1024
1025
1026
1027
1028
1029
1030
1031
				} else if (nv[0] == "is_array") {
					if (eval (nv[1]) == "1") {
						type.array_rank = 1;
					}
				} else if (nv[0] == "weak") {
					if (eval (nv[1]) == "0") {
						type.takes_ownership = true;
					}
Jürg Billeter's avatar
Jürg Billeter committed
1032
1033
1034
				}
			}
		}
Jürg Billeter's avatar
Jürg Billeter committed
1035
		
1036
1037
		if (current_type_symbol_set != null) {
			current_type_symbol_set.add (node.name);
Jürg Billeter's avatar
Jürg Billeter committed
1038
1039
1040
1041
1042
1043
1044
		}
		
		var field = new Field (node.name, type, null, current_source_reference);
		field.access = MemberAccessibility.PUBLIC;
		
		return field;
	}
1045
1046

	[NoArrayLength]
1047
	private string[] get_attributes (string! codenode) {
1048
		string attributes = codenode_attributes_map.get (codenode);
Jürg Billeter's avatar
Jürg Billeter committed
1049
1050
1051
1052
1053
1054
1055
		if (attributes == null) {
			return null;
		}
		
		return attributes.split (" ");
	}
	
1056
	private string eval (string! s) {
Jürg Billeter's avatar
Jürg Billeter committed
1057
1058
1059
		return s.offset (1).ndup (s.size () - 2);
	}

1060
	private Signal parse_signal (IdlNodeSignal! sig_node) {
1061
		weak IdlNode node = (IdlNode) sig_node;
Jürg Billeter's avatar
Jürg Billeter committed
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
		
		if (sig_node.deprecated || sig_node.result == null) {
			return null;
		}
		
		var sig = new Signal (fix_prop_name (node.name), parse_param (sig_node.result), current_source_reference);
		sig.access = MemberAccessibility.PUBLIC;
		
		var attributes = get_attributes ("%s::%s".printf (current_data_type.name, sig.name));
		if (attributes != null) {
			foreach (string attr in attributes) {
				var nv = attr.split ("=", 2);
				if (nv[0] == "name") {
					sig.set_cname (sig.name);
					sig.name = eval (nv[1]);
				} else if (nv[0] == "has_emitter" && eval (nv[1]) == "1") {
					sig.has_emitter = true;
1079
1080
1081
1082
				} else if (nv[0] == "hidden") {
					if (eval (nv[1]) == "1") {
						return null;
					}
Jürg Billeter's avatar
Jürg Billeter committed
1083
1084
1085
1086
1087
1088
				}
			}
		}
		
		bool first = true;
		
1089
		foreach (weak IdlNodeParam param in sig_node.parameters) {
Jürg Billeter's avatar
Jürg Billeter committed
1090
1091
1092
1093
1094
1095
			if (first) {
				// ignore implicit first signal parameter (sender)
				first = false;
				continue;
			}
		
1096
			weak IdlNode param_node = (IdlNode) param;
Jürg Billeter's avatar
Jürg Billeter committed
1097
1098
1099
1100
1101
1102
1103
1104
			
			var p = new FormalParameter (param_node.name, parse_param (param));
			sig.add_parameter (p);
		}
		
		return sig;
	}
}