valagidlparser.vala 102 KB
Newer Older
1 2
/* valagidlparser.vala
 *
3
 * Copyright (C) 2006-2010  Jürg Billeter
4
 * Copyright (C) 2006-2008  Raffaele Sandrini
5 6 7 8
 *
 * 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
9
 * version 2.1 of the License, or (at your option) any later version.
10 11 12 13 14 15 16 17 18 19 20 21

 * 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>
22
 *	Raffaele Sandrini <raffaele@sandrini.ch>
23 24 25 26 27 28 29 30
 */

using GLib;

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

33 34
	private SourceFile current_source_file;

35 36
	private SourceReference current_source_reference;
	
37
	private Namespace current_namespace;
38
	private TypeSymbol current_data_type;
39
	private Map<string,string> codenode_attributes_map;
40
	private Map<PatternSpec*,string> codenode_attributes_patterns;
41
	private Set<string> current_type_symbol_set;
42

43
	private Map<string,TypeSymbol> cname_type_map;
44

45 46
	static GLib.Regex type_from_string_regex;

47 48 49 50 51 52
	/**
	 * Parse all source files in the specified code context and build a
	 * code tree.
	 *
	 * @param context a code context
	 */
53
	public void parse (CodeContext context) {
54
		cname_type_map = new HashMap<string,TypeSymbol> (str_hash, str_equal);
55

56
		this.context = context;
57
		context.accept (this);
58 59 60 61

		cname_type_map = null;
	}

62
	public override void visit_namespace (Namespace ns) {
63 64 65
		ns.accept_children (this);
	}

66
	public override void visit_class (Class cl) {
67 68 69
		visit_type (cl);
	}

70
	public override void visit_struct (Struct st) {
71 72 73
		visit_type (st);
	}

74
	public override void visit_interface (Interface iface) {
75 76 77
		visit_type (iface);
	}

78
	public override void visit_enum (Enum en) {
79 80 81
		visit_type (en);
	}

82 83 84 85
	public override void visit_error_domain (ErrorDomain ed) {
		visit_type (ed);
	}

86
	public override void visit_delegate (Delegate d) {
87
		visit_type (d);
88 89
	}

90
	private void visit_type (TypeSymbol t) {
91 92
		if (!cname_type_map.contains (get_cname (t))) {
			cname_type_map[get_cname (t)] = t;
93
		}
94 95
	}

96
	public override void visit_source_file (SourceFile source_file) {
97
		if (source_file.filename.has_suffix (".gi")) {
98 99 100 101
			parse_file (source_file);
		}
	}
	
102
	private void parse_file (SourceFile source_file) {
103
		string metadata_filename = "%s.metadata".printf (source_file.filename.substring (0, source_file.filename.length - ".gi".length));
104 105 106

		current_source_file = source_file;

107
		codenode_attributes_map = new HashMap<string,string> (str_hash, str_equal);
108
		codenode_attributes_patterns = new HashMap<PatternSpec*,string> (direct_hash, (EqualFunc<PatternSpec>) PatternSpec.equal);
109

110
		if (FileUtils.test (metadata_filename, FileTest.EXISTS)) {
111 112
			try {
				string metadata;
113
				FileUtils.get_contents (metadata_filename, out metadata, null);
114
				
115
				foreach (unowned string line in metadata.split ("\n")) {
116 117 118 119 120
					if (line.has_prefix ("#")) {
						// ignore comment lines
						continue;
					}

121 122 123
					var tokens = line.split (" ", 2);

					if (null == tokens[0]) {
124 125
						continue;
					}
126

127
					if (-1 != tokens[0].index_of_char ('*')) {
128 129
						PatternSpec* pattern = new PatternSpec (tokens[0]);
						codenode_attributes_patterns[pattern] = tokens[0];
130
					}
131
					
132
					codenode_attributes_map[tokens[0]] = tokens[1];
133 134 135
				}
			} catch (FileError e) {
				Report.error (null, "Unable to read metadata file: %s".printf (e.message));
136 137 138
			}
		}
	
139 140 141
		try {
			var modules = Idl.parse_file (source_file.filename);
			
142
			current_source_reference = new SourceReference (source_file, SourceLocation (null, 0, 0), SourceLocation (null, 0, 0));
143
			
144
			foreach (weak IdlModule module in modules) {
145
				var ns = parse_module (module);
146 147 148
				if (ns != null) {
					context.root.add_namespace (ns);
				}
149 150
			}
		} catch (MarkupError e) {
151
			Report.error (null, "Unable to parse GIDL file: %s".printf (e.message));
152 153
		}
	}
154

155
	private string fix_type_name (string type_name, Symbol container) {
156 157 158 159 160 161 162 163 164 165
		var attributes = get_attributes (type_name);
		if (attributes != null) {
			foreach (string attr in attributes) {
				var nv = attr.split ("=", 2);
				if (nv[0] == "name") {
					return eval (nv[1]);
				}
			}
		}

166
		if (type_name.has_prefix (container.name)) {
167
			return type_name.substring (container.name.length);
168 169 170 171
		} else {
			var cprefix = get_cprefix (container);
			if (type_name.has_prefix (cprefix)) {
				return type_name.substring (cprefix.length);;
172
			}
173
		}
174

175 176 177
		return type_name;
	}

178
	private string fix_const_name (string const_name, Symbol container) {
179
		var pref = get_lower_case_cprefix (container).ascii_up ();
180
		if (const_name.has_prefix (pref)) {
181
			return const_name.substring (pref.length);
182 183 184 185
		}
		return const_name;
	}

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
	private string get_cheader_filename (Symbol sym) {
		var cheader_filename = sym.get_attribute_string ("CCode", "cheader_filename");
		if (cheader_filename != null) {
			return cheader_filename;
		}
		if (sym.parent_symbol != null) {
			return get_cheader_filename (sym.parent_symbol);
		} else if (sym.source_reference != null) {
			return sym.source_reference.file.get_cinclude_filename ();
		}
		return "";
	}

	private string get_cname (Symbol sym, Symbol? container = null) {
		if (container == null) {
			container = sym.parent_symbol;
		}
		var cname = sym.get_attribute_string ("CCode", "cname");
		if (cname != null) {
			return cname;
		}
		if (sym is Method) {
			var name = sym.name;
			if (sym is CreationMethod) {
				if (name == null || name == ".new") {
					name = "new";
				} else {
213
					name = "new_%s".printf (name);
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
				}
			}
			if (container != null) {
				return "%s%s".printf (get_lower_case_cprefix (container), name);
			} else {
				return name;
			}
		} else {
			if (container != null) {
				return "%s%s".printf (get_cprefix (container), sym.name);
			} else {
				return sym.name;
			}
		}
	}

	private string get_finish_cname (Method m) {
		var finish_cname = m.get_attribute_string ("CCode", "finish_name");
		if (finish_cname != null) {
			return finish_cname;
		}
		var result = get_cname (m);
		if (result.has_suffix ("_async")) {
			result = result.substring (0, result.length - "_async".length);
		}
		return result + "_finish";
	}

	private string get_lower_case_cname (Symbol sym) {
		var lower_case_csuffix = Symbol.camel_case_to_lower_case (sym.name);
		if (sym is ObjectTypeSymbol) {
			// remove underscores in some cases to avoid conflicts of type macros
			if (lower_case_csuffix.has_prefix ("type_")) {
				lower_case_csuffix = "type" + lower_case_csuffix.substring ("type_".length);
			} else if (lower_case_csuffix.has_prefix ("is_")) {
				lower_case_csuffix = "is" + lower_case_csuffix.substring ("is_".length);
			}
			if (lower_case_csuffix.has_suffix ("_class")) {
				lower_case_csuffix = lower_case_csuffix.substring (0, lower_case_csuffix.length - "_class".length) + "class";
			}
		}
		if (sym.parent_symbol != null) {
			return "%s%s".printf (get_lower_case_cprefix (sym.parent_symbol), lower_case_csuffix);
		} else {
			return lower_case_csuffix;
		}
	}

	private string get_lower_case_cprefix (Symbol sym) {
		if (sym.name == null) {
			return "";
		}
		string cprefix;
267 268
		cprefix = sym.get_attribute_string ("CCode", "lower_case_cprefix");
		if (cprefix == null && (sym is ObjectTypeSymbol || sym is Struct)) {
269 270 271 272 273 274 275 276 277 278 279 280
			cprefix = sym.get_attribute_string ("CCode", "cprefix");
		}
		if (cprefix != null) {
			return cprefix;
		}
		return get_lower_case_cname (sym) + "_";
	}

	public string get_cprefix (Symbol sym) {
		if (sym is ObjectTypeSymbol) {
			return get_cname (sym);
		} else if (sym is Enum || sym is ErrorDomain) {
281
			return "%s_".printf (get_lower_case_cname (sym).ascii_up ());
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
		} else if (sym is Namespace) {
			if (sym.name != null) {
				var cprefix = sym.get_attribute_string ("CCode", "cprefix");
				if (cprefix != null) {
					return cprefix;
				}
				if (sym.parent_symbol != null) {
					return "%s%s".printf (get_cprefix (sym.parent_symbol), sym.name);
				} else {
					return sym.name;
				}
			} else {
				return "";
			}
		} else if (sym.name != null) {
			return sym.name;
		}
		return "";
	}

302 303 304 305 306 307
	private string[] get_attributes_for_node (IdlNode node) {
		string name;

		if (node.type == IdlNodeTypeId.FUNCTION) {
			name = ((IdlNodeFunction) node).symbol;
		} else if (node.type == IdlNodeTypeId.SIGNAL) {
308
			name = "%s::%s".printf (get_cname (current_data_type), node.name);
309
		} else if (node.type == IdlNodeTypeId.PROPERTY) {
310
			name = "%s:%s".printf (get_cname (current_data_type), node.name);
311
		} else if (node.type == IdlNodeTypeId.FIELD) {
312
			name = "%s.%s".printf (get_cname (current_data_type), node.name);
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
		} else {
			name = node.name;
		}

		return get_attributes (name);
	}

	private void add_symbol_to_container (Symbol container, Symbol sym) {
		if (container is Class) {
			unowned Class cl = (Class) container;

			if (sym is Class) {
				cl.add_class ((Class) sym);
			} else if (sym is Constant) {
				cl.add_constant ((Constant) sym);
			} else if (sym is Enum) {
				cl.add_enum ((Enum) sym);
			} else if (sym is Field) {
				cl.add_field ((Field) sym);
			} else if (sym is Method) {
				cl.add_method ((Method) sym);
			} else if (sym is Property) {
				cl.add_property ((Property) sym);
			} else if (sym is Signal) {
				cl.add_signal ((Signal) sym);
			} else if (sym is Struct) {
				cl.add_struct ((Struct) sym);
			}
		} else if (container is Enum) {
			unowned Enum en = (Enum) container;

			if (sym is EnumValue) {
				en.add_value ((EnumValue) sym);
			} else if (sym is Constant) {
				en.add_constant ((Constant) sym);
			} else if (sym is Method) {
				en.add_method ((Method) sym);
			}
		} else if (container is Interface) {
			unowned Interface iface = (Interface) container;

			if (sym is Class) {
				iface.add_class ((Class) sym);
			} else if (sym is Constant) {
				iface.add_constant ((Constant) sym);
			} else if (sym is Enum) {
				iface.add_enum ((Enum) sym);
			} else if (sym is Field) {
				iface.add_field ((Field) sym);
			} else if (sym is Method) {
				iface.add_method ((Method) sym);
			} else if (sym is Property) {
				iface.add_property ((Property) sym);
			} else if (sym is Signal) {
				iface.add_signal ((Signal) sym);
			} else if (sym is Struct) {
				iface.add_struct ((Struct) sym);
			}
		} else if (container is Namespace) {
			unowned Namespace ns = (Namespace) container;

			if (sym is Namespace) {
				ns.add_namespace ((Namespace) sym);
			} else if (sym is Class) {
				ns.add_class ((Class) sym);
			} else if (sym is Constant) {
				ns.add_constant ((Constant) sym);
			} else if (sym is Delegate) {
				ns.add_delegate ((Delegate) sym);
			} else if (sym is Enum) {
				ns.add_enum ((Enum) sym);
			} else if (sym is ErrorDomain) {
				ns.add_error_domain ((ErrorDomain) sym);
			} else if (sym is Field) {
				ns.add_field ((Field) sym);
			} else if (sym is Interface) {
				ns.add_interface ((Interface) sym);
			} else if (sym is Method) {
				ns.add_method ((Method) sym);
			} else if (sym is Namespace) {
				ns.add_namespace ((Namespace) sym);
			} else if (sym is Struct) {
				ns.add_struct ((Struct) sym);
			}
		} else if (container is Struct) {
			unowned Struct st = (Struct) container;

			if (sym is Constant) {
				st.add_constant ((Constant) sym);
			} else if (sym is Field) {
				st.add_field ((Field) sym);
			} else if (sym is Method) {
				st.add_method ((Method) sym);
			} else if (sym is Property) {
				st.add_property ((Property) sym);
			}
		}
410 411 412 413 414

		if (!(sym is Namespace) && container is Namespace) {
			// set C headers
			sym.set_attribute_string ("CCode", "cheader_filename", get_cheader_filename (sym));
		}
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
	}

	private void parse_node (IdlNode node, IdlModule module, Symbol container) {
		if (node.type == IdlNodeTypeId.CALLBACK) {
			var cb = parse_delegate ((IdlNodeFunction) node);
			if (cb == null) {
				return;
			}
			cb.name = fix_type_name (cb.name, container);
			add_symbol_to_container (container, cb);
			current_source_file.add_node (cb);
		} else if (node.type == IdlNodeTypeId.STRUCT) {
			parse_struct ((IdlNodeStruct) node, container, module);
		} else if (node.type == IdlNodeTypeId.UNION) {
			parse_union ((IdlNodeUnion) node, container, module);
		} else if (node.type == IdlNodeTypeId.BOXED) {
			parse_boxed ((IdlNodeBoxed) node, container, module);
		} else if (node.type == IdlNodeTypeId.ENUM) {
			parse_enum ((IdlNodeEnum) node, container, module, false);
		} else if (node.type == IdlNodeTypeId.FLAGS) {
			parse_enum ((IdlNodeEnum) node, container, module, true);
		} else if (node.type == IdlNodeTypeId.OBJECT) {
			parse_object ((IdlNodeInterface) node, container, module);
		} else if (node.type == IdlNodeTypeId.INTERFACE) {
			parse_interface ((IdlNodeInterface) node, container, module);
		} else if (node.type == IdlNodeTypeId.CONSTANT) {
			var c = parse_constant ((IdlNodeConstant) node);
			if (c != null) {
				c.name = fix_const_name (c.name, container);
				add_symbol_to_container (container, c);
				current_source_file.add_node (c);
			}
		} else if (node.type == IdlNodeTypeId.FUNCTION) {
			var m = parse_function ((IdlNodeFunction) node);
			if (m != null) {
				m.binding = MemberBinding.STATIC;
				add_symbol_to_container (container, m);
				current_source_file.add_node (m);
			}
		}
	}

	private Symbol? get_container_from_name (string name) {
		var path = name.split (".");
		Symbol? cp = current_namespace;
460 461 462
		if (cp.parent_symbol != context.root) {
			cp = cp.parent_symbol;
		}
463 464 465 466 467 468 469 470 471 472 473 474 475 476
		Symbol? cc = null;

		foreach ( unowned string tok in path ) {
			cc = cp.scope.lookup (tok) as Symbol;
			if ( cc == null ) {
				cc = new Namespace (tok, current_source_reference);
				add_symbol_to_container (cp, cc);
			}
			cp = cc;
		}

		return cc;
	}

477
	private Namespace? parse_module (IdlModule module) {
478 479 480 481
		Symbol sym = context.root.scope.lookup (module.name);
		Namespace ns;
		if (sym is Namespace) {
			ns = (Namespace) sym;
482
			if (ns.external_package) {
483
				ns.attributes = null;
484 485
				ns.source_reference = current_source_reference;
			}
486 487 488
		} else {
			ns = new Namespace (module.name, current_source_reference);
		}
489 490 491

		current_namespace = ns;

492 493 494 495 496
		var attributes = get_attributes (ns.name);
		if (attributes != null) {
			foreach (string attr in attributes) {
				var nv = attr.split ("=", 2);
				if (nv[0] == "cheader_filename") {
497
					ns.set_attribute_string ("CCode", "cheader_filename", eval (nv[1]));
498
				} else if (nv[0] == "cprefix") {
499
					ns.set_attribute_string ("CCode", "cprefix", eval (nv[1]));
500
				} else if (nv[0] == "lower_case_cprefix") {
501
					ns.set_attribute_string ("CCode", "lower_case_cprefix",  eval (nv[1]));
Rob Taylor's avatar
Rob Taylor committed
502 503
				} else if (nv[0] == "gir_namespace") {
					ns.source_reference.file.gir_namespace = eval (nv[1]);
504
					ns.set_attribute_string ("CCode", "gir_namespace", eval (nv[1]));
Rob Taylor's avatar
Rob Taylor committed
505 506
				} else if (nv[0] == "gir_version") {
					ns.source_reference.file.gir_version = eval (nv[1]);
507
					ns.set_attribute_string ("CCode", "gir_version", eval (nv[1]));
508 509
				} else if (nv[0] == "deprecated") {
					if (eval (nv[1]) == "1") {
510
						ns.set_attribute_bool ("Version", "deprecated", true);
511 512
					}
				} else if (nv[0] == "replacement") {
513
					ns.set_attribute_string ("Version", "replacement", eval (nv[1]));
514
				} else if (nv[0] == "deprecated_since") {
515
					ns.set_attribute_string ("Version", "deprecated_since", eval (nv[1]));
516 517 518
				}
			}
		}
519 520 521

		var deferred = new ArrayList<unowned IdlNode> ();

522
		foreach (weak IdlNode node in module.entries) {
523 524 525 526 527 528 529 530 531
			bool is_deferred = false;
			var child_attributes = get_attributes_for_node (node);
			if (child_attributes != null) {
				foreach (unowned string attr in child_attributes) {
					var nv = attr.split ("=", 2);
					if (nv[0] == "parent") {
						deferred.add (node);
						is_deferred = true;
					}
532
				}
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
			}

			if (!is_deferred) {
				parse_node (node, module, ns);
			}
		}

		foreach (unowned IdlNode node in deferred) {
			Symbol container = ns;
			var child_attributes = get_attributes_for_node (node);
			if (child_attributes != null) {
				foreach (unowned string attr in child_attributes) {
					var nv = attr.split ("=", 2);
					if (nv[0] == "parent") {
						container = get_container_from_name (eval (nv[1]));
					}
549
				}
550
			}
551

552 553 554 555 556
			if (container is Namespace) {
				current_namespace = (Namespace) container;
			} else {
				current_data_type = (TypeSymbol) container;
			}
557
			parse_node (node, module, container);
558 559
			current_namespace = ns;
			current_data_type = null;
560
		}
561 562 563

		current_namespace = null;

564 565 566
		if (sym is Namespace) {
			return null;
		}
567 568 569
		return ns;
	}
	
570
	private Delegate? parse_delegate (IdlNodeFunction f_node) {
571
		weak IdlNode node = (IdlNode) f_node;
572

573 574 575
		var return_type = parse_param (f_node.result);

		var cb = new Delegate (node.name, return_type, current_source_reference);
576
		cb.access = SymbolAccessibility.PUBLIC;
577
		cb.has_target = false;
578

579
		bool check_has_target = true;
580 581
		bool suppress_throws = false;
		string? error_types = null;
582

583 584 585 586 587 588 589 590
		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;
					}
591
				} else if (nv[0] == "cheader_filename") {
592
					cb.set_attribute_string ("CCode", "cheader_filename", eval (nv[1]));
593 594 595
				} else if (nv[0] == "has_target") {
					if (eval (nv[1]) == "0") {
						check_has_target = false;
596 597
					} else if (eval (nv[1]) == "1") {
						cb.has_target = true;
598
					}
599 600 601 602
				} else if (nv[0] == "transfer_ownership") {
					if (eval (nv[1]) == "1") {
						return_type.value_owned = true;
					}
603 604
				} else if (nv[0] == "is_array") {
					if (eval (nv[1]) == "1") {
605
						return_type.value_owned = true;
606 607 608
						return_type = new ArrayType (return_type, 1, return_type.source_reference);
						cb.return_type = return_type;
					}
609 610 611 612 613 614
				} else if (nv[0] == "throws") {
					if (eval (nv[1]) == "0") {
						suppress_throws = true;
					}
				} else if (nv[0] == "error_types") {
					error_types = eval (nv[1]);
615
				} else if (nv[0] == "array_length_type") {
616
					cb.set_attribute_string ("CCode", "array_length_type", eval (nv[1]));
617 618
				} else if (nv[0] == "type_name") {
					cb.return_type = return_type = parse_type_from_string (eval (nv[1]), return_type.value_owned);
619 620
				} else if (nv[0] == "deprecated") {
					if (eval (nv[1]) == "1") {
621
						cb.set_attribute_bool ("Version", "deprecated", true);
622 623
					}
				} else if (nv[0] == "replacement") {
624
					cb.set_attribute_string ("Version", "replacement", eval (nv[1]));
625
				} else if (nv[0] == "deprecated_since") {
626
					cb.set_attribute_string ("Version", "deprecated_since", eval (nv[1]));
627
				} else if (nv[0] == "type_arguments") {
628
					parse_type_arguments_from_string (return_type, eval (nv[1]));
629
				} else if (nv[0] == "instance_pos") {
630
					cb.set_attribute_double ("CCode", "instance_pos", double.parse (eval (nv[1])));
631 632 633 634
				} else if (nv[0] == "type_parameters") {
					foreach (string type_param_name in eval (nv[1]).split (",")) {
						cb.add_type_parameter (new TypeParameter (type_param_name, current_source_reference));
					}
635 636
				} else if (nv[0] == "experimental") {
					if (eval (nv[1]) == "1") {
637
						cb.set_attribute_bool ("Version", "experimental", true);
638
					}
639 640 641
				}
			}
		}
642

643
		uint remaining_params = f_node.parameters.length ();
644
		foreach (weak IdlNodeParam param in f_node.parameters) {
645
			weak IdlNode param_node = (IdlNode) param;
646

647
			if (check_has_target && remaining_params == 1 && (param_node.name == "user_data" || param_node.name == "data")) {
648
				// hide user_data parameter for instance delegates
649
				cb.has_target = true;
650
			} else {
651 652 653 654 655 656 657 658
				// check for GError parameter
				if (suppress_throws == false && param_is_exception (param)) {
					if (error_types == null)
						cb.add_error_type (parse_type (param.type));
					remaining_params--;
					continue;
				}

659 660 661 662
				string param_name = param_node.name;
				if (param_name == "string") {
					// avoid conflict with string type
					param_name = "str";
663 664 665
				} else if (param_name == "self") {
					// avoid conflict with delegate target
					param_name = "_self";
666 667
				}

668
				ParameterDirection direction;
669
				var param_type = parse_param (param, out direction);
670
				var p = new Parameter (param_name, param_type);
671
				p.direction = direction;
672 673 674

				bool hide_param = false;
				bool show_param = false;
675 676
				bool array_requested = false;
				bool out_requested = false;
677 678 679 680 681 682 683 684 685
				attributes = get_attributes ("%s.%s".printf (node.name, param_node.name));
				if (attributes != null) {
					foreach (string attr in attributes) {
						var nv = attr.split ("=", 2);
						if (nv[0] == "hidden") {
							if (eval (nv[1]) == "1") {
								hide_param = true;
							} else if (eval (nv[1]) == "0") {
								show_param = true;
686
							}
687 688
						} else if (nv[0] == "is_array") {
							if (eval (nv[1]) == "1") {
689
								param_type.value_owned = true;
690
								param_type = new ArrayType (param_type, 1, param_type.source_reference);
Jürg Billeter's avatar
Jürg Billeter committed
691
								p.variable_type = param_type;
692 693 694 695 696
								if (!out_requested) {
									p.direction = ParameterDirection.IN;
								}
								array_requested = true;
							}
697 698 699
						} else if (nv[0] == "is_out") {
							if (eval (nv[1]) == "1") {
								p.direction = ParameterDirection.OUT;
700 701 702 703
								out_requested = true;
								if (!array_requested && param_type is ArrayType) {
									var array_type = (ArrayType) param_type;
									param_type = array_type.element_type;
Jürg Billeter's avatar
Jürg Billeter committed
704
									p.variable_type = param_type;
705
								}
706 707 708 709
							}
						} else if (nv[0] == "is_ref") {
							if (eval (nv[1]) == "1") {
								p.direction = ParameterDirection.REF;
710 711 712
								if (!array_requested && param_type is ArrayType) {
									var array_type = (ArrayType) param_type;
									param_type = array_type.element_type;
Jürg Billeter's avatar
Jürg Billeter committed
713
									p.variable_type = param_type;
714
								}
715
							}
716 717 718
						} else if (nv[0] == "takes_ownership") {
							if (eval (nv[1]) == "1") {
								param_type.value_owned = true;
719
							}
720 721 722 723
						} else if (nv[0] == "nullable") {
							if (eval (nv[1]) == "1") {
								param_type.nullable = true;
							}
724
						} else if (nv[0] == "type_arguments") {
725
							parse_type_arguments_from_string (param_type, eval (nv[1]));
726 727
						} else if (nv[0] == "no_array_length") {
							if (eval (nv[1]) == "1") {
728
								p.set_attribute_bool ("CCode", "array_length", false);
729
							}
730
						} else if (nv[0] == "array_length_type") {
731
							p.set_attribute_string ("CCode", "array_length_type", eval (nv[1]));
732 733
						} else if (nv[0] == "array_null_terminated") {
							if (eval (nv[1]) == "1") {
734 735
								p.set_attribute_bool ("CCode", "array_length", false);
								p.set_attribute_bool ("CCode", "array_null_terminated", true);
736
							}
737
						} else if (nv[0] == "type_name") {
738
							p.variable_type = param_type = parse_type_from_string (eval (nv[1]), false);
739 740 741 742 743 744 745
						}
					}
				}

				if (show_param || !hide_param) {
					cb.add_parameter (p);
				}
746
			}
747 748

			remaining_params--;
749
		}
750 751 752

		if (suppress_throws == false && error_types != null) {
			var type_args = eval (error_types).split (",");
753
			foreach (unowned string type_arg in type_args) {
754 755 756 757
				cb.add_error_type (parse_type_from_string (type_arg, true));
			}
		}

758 759
		return cb;
	}
760

761
	private bool is_reference_type (string cname) {
762 763 764 765 766 767 768 769 770 771 772 773
		var st_attributes = get_attributes (cname);
		if (st_attributes != null) {
			foreach (string attr in st_attributes) {
				var nv = attr.split ("=", 2);
				if (nv[0] == "is_value_type" && eval (nv[1]) == "1") {
					return false;
				}
			}
		}
		return true;
	}

774
	private void parse_struct (IdlNodeStruct st_node, Symbol container, IdlModule module) {
775
		weak IdlNode node = (IdlNode) st_node;
776 777
		
		if (st_node.deprecated) {
778
			return;
779 780
		}

781
		string name = fix_type_name (node.name, container);
782

783
		if (!is_reference_type (node.name)) {
784
			var st = container.scope.lookup (name) as Struct;
785 786
			if (st == null) {
				st = new Struct (name, current_source_reference);
787
				st.access = SymbolAccessibility.PUBLIC;
788 789 790 791 792 793

				var st_attributes = get_attributes (node.name);
				if (st_attributes != null) {
					foreach (string attr in st_attributes) {
						var nv = attr.split ("=", 2);
						if (nv[0] == "cheader_filename") {
794
							st.set_attribute_string ("CCode", "cheader_filename", eval (nv[1]));
795 796 797 798
						} else if (nv[0] == "hidden") {
							if (eval (nv[1]) == "1") {
								return;
							}
799 800 801
						} else if (nv[0] == "base_type") {
							st.base_type = parse_type_string (eval (nv[1]));
						} else if (nv[0] == "rank") {
802
							st.rank = int.parse (eval (nv[1]));
803 804 805 806
						} else if (nv[0] == "simple_type") {
							if (eval (nv[1]) == "1") {
								st.set_simple_type (true);
							}
807 808
						} else if (nv[0] == "immutable") {
							if (eval (nv[1]) == "1") {
809
								st.set_attribute ("Immutable", true);
810
							}
811 812
						} else if (nv[0] == "has_type_id") {
							if (eval (nv[1]) == "0") {
813
								st.set_attribute_bool ("CCode", "has_type_id", false);
814 815
							}
						} else if (nv[0] == "type_id") {
816
							st.set_attribute_string ("CCode", "type_id", eval (nv[1]));
817 818
						} else if (nv[0] == "has_copy_function") {
							if (eval (nv[1]) == "0") {
819
								st.set_attribute_bool ("CCode", "has_copy_function", false);
820
							}
821 822
						} else if (nv[0] == "deprecated") {
							if (eval (nv[1]) == "1") {
823
								st.set_attribute_bool ("Version", "deprecated", true);
824 825
							}
						} else if (nv[0] == "replacement") {
826
							st.set_attribute_string ("Version", "replacement", eval (nv[1]));
827
						} else if (nv[0] == "deprecated_since") {
828
							st.set_attribute_string ("Version", "deprecated_since", eval (nv[1]));
829 830
						} else if (nv[0] == "has_destroy_function") {
							if (eval (nv[1]) == "0") {
831
								st.set_attribute_bool ("CCode", "has_destroy_function", false);
832
							}
833 834
						} else if (nv[0] == "experimental") {
							if (eval (nv[1]) == "1") {
835
								st.set_attribute_bool ("Version", "experimental", true);
836
							}
837
						}
Jürg Billeter's avatar
Jürg Billeter committed
838
					}
839
				}
840

841
				add_symbol_to_container (container, st);
842
				current_source_file.add_node (st);
843
			}
844

845
			current_data_type = st;
846

847 848
			foreach (weak IdlNode member in st_node.members) {
				if (member.type == IdlNodeTypeId.FUNCTION) {
849 850 851 852
					var m = parse_function ((IdlNodeFunction) member);
					if (m != null) {
						st.add_method (m);
					}
853 854 855 856 857
				} else if (member.type == IdlNodeTypeId.FIELD) {
					var f = parse_field ((IdlNodeField) member);
					if (f != null) {
						st.add_field (f);
					}
858
				}
859 860 861 862
			}

			current_data_type = null;
		} else {
863 864 865 866 867 868
			bool ref_function_void = false;
			string ref_function = null;
			string unref_function = null;
			string copy_function = null;
			string free_function = null;

869
			var cl = container.scope.lookup (name) as Class;
870
			if (cl == null) {
871
				string base_class = null;
872
				bool is_fundamental = false;
873

874
				cl = new Class (name, current_source_reference);
875
				cl.access = SymbolAccessibility.PUBLIC;
876 877 878 879 880 881

				var cl_attributes = get_attributes (node.name);
				if (cl_attributes != null) {
					foreach (string attr in cl_attributes) {
						var nv = attr.split ("=", 2);
						if (nv[0] == "cheader_filename") {
882
							cl.set_attribute_string ("CCode", "cheader_filename", eval (nv[1]));
883 884
						} else if (nv[0] == "base_class") {
							base_class = eval (nv[1]);
885 886 887 888
						} else if (nv[0] == "hidden") {
							if (eval (nv[1]) == "1") {
								return;
							}
889 890 891 892
						} else if (nv[0] == "is_immutable") {
							if (eval (nv[1]) == "1") {
								cl.is_immutable = true;
							}
893
						} else if (nv[0] == "const_cname") {
894
							cl.set_attribute_string ("CCode", "const_cname", eval (nv[1]));
895 896
						} else if (nv[0] == "is_fundamental") {
							if (eval (nv[1]) == "1") {
897
								is_fundamental = true;
898
							}
899 900 901 902
						} else if (nv[0] == "abstract" && base_class != null) {
							if (eval (nv[1]) == "1") {
								cl.is_abstract = true;
							}
903 904 905 906 907 908 909 910 911 912 913 914
						} else if (nv[0] == "free_function") {
							free_function = eval (nv[1]);
						} else if (nv[0] == "ref_function") {
							ref_function = eval (nv[1]);
						} else if (nv[0] == "unref_function") {
							unref_function = eval (nv[1]);
						} else if (nv[0] == "copy_function") {
							copy_function = eval (nv[1]);
						} else if (nv[0] == "ref_function_void") {
							if (eval (nv[1]) == "1") {
								ref_function_void = true;
							}
915 916
						} else if (nv[0] == "deprecated") {
							if (eval (nv[1]) == "1") {
917
								cl.set_attribute_bool ("Version", "deprecated", true);
918 919
							}
						} else if (nv[0] == "replacement") {
920
							cl.set_attribute_string ("Version", "replacement", eval (nv[1]));
921
						} else if (nv[0] == "deprecated_since") {
922
							cl.set_attribute_string ("Version", "deprecated_since", eval (nv[1]));
923
						} else if (nv[0] == "type_parameters") {
924
							foreach (unowned string type_param_name in eval (nv[1]).split (",")) {
925 926
								cl.add_type_parameter (new TypeParameter (type_param_name, current_source_reference));
							}
927 928
						} else if (nv[0] == "experimental") {
							if (eval (nv[1]) == "1") {
929
								cl.set_attribute_bool ("Version", "experimental", true);
930
							}
931 932
						} else if (nv[0] == "delegate_target_cname") {
							cl.set_attribute_string ("CCode", "delegate_target_cname", eval (nv[1]));
933 934
						}
					}
935
				}
936

937
				add_symbol_to_container (container, cl);
938
				current_source_file.add_node (cl);
939 940

				if (base_class != null) {
941
					var parent = parse_type_string (base_class);
942 943
					cl.add_base_type (parent);
				}
944 945 946
				if (base_class == null && !is_fundamental) {
					cl.is_compact = true;
				}
947 948
			}

949 950 951 952
			current_data_type = cl;

			foreach (weak IdlNode member in st_node.members) {
				if (member.type == IdlNodeTypeId.FUNCTION) {
953
					if ((ref_function == null) && (member.name == "ref")) {
954
						ref_function = ((IdlNodeFunction) member).symbol;
955
						ref_function_void = (parse_type (((IdlNodeFunction) member).result.type) is VoidType);
956
					} else if ((unref_function == null) && (member.name == "unref")) {
957
						unref_function = ((IdlNodeFunction) member).symbol;
958
					} else if ((free_function == null) && (member.name == "free" || member.name == "destroy")) {
959 960
						free_function = ((IdlNodeFunction) member).symbol;
					} else {
961
						if ((copy_function == null) && (member.name == "copy")) {
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
							copy_function = ((IdlNodeFunction) member).symbol;
						}
						var m = parse_function ((IdlNodeFunction) member);
						if (m != null) {
							cl.add_method (m);
						}
					}
				} else if (member.type == IdlNodeTypeId.FIELD) {
					var f = parse_field ((IdlNodeField) member);
					if (f != null) {
						cl.add_field (f);
					}
				}
			}

			if (ref_function != null) {
978 979 980 981 982 983
				cl.set_attribute_string ("CCode", "ref_function", ref_function);
				if (ref_function_void) {
					cl.set_attribute_bool ("CCode", "ref_function_void", ref_function_void);
				}
			} else if (copy_function != null) {
				cl.set_attribute_string ("CCode", "copy_function", copy_function);
984 985
			}
			if (unref_function != null) {
986 987 988
				cl.set_attribute_string ("CCode", "unref_function", unref_function);
			} else if (free_function != null && free_function != "%sfree".printf (get_lower_case_cprefix (cl))) {
				cl.set_attribute_string ("CCode", "free_function", free_function);
989 990 991 992 993 994
			}

			current_data_type = null;
		}
	}

995
	private void parse_union (IdlNodeUnion un_node, Symbol container, IdlModule module) {
996 997 998 999 1000 1001
		weak IdlNode node = (IdlNode) un_node;
		
		if (un_node.deprecated) {
			return;
		}

1002
		string name = fix_type_name (node.name, container);
1003 1004

		if (!is_reference_type (node.name)) {
1005
			var st = container.scope.lookup (name) as Struct;
1006 1007 1008 1009 1010 1011 1012 1013 1014
			if (st == null) {
				st = new Struct (name, current_source_reference);
				st.access = SymbolAccessibility.PUBLIC;

				var st_attributes = get_attributes (node.name);
				if (st_attributes != null) {
					foreach (string attr in st_attributes) {
						var nv = attr.split ("=", 2);
						if (nv[0] == "cheader_filename") {
1015
							st.set_attribute_string ("CCode", "cheader_filename", eval (nv[1]));
1016 1017
						} else if (nv[0] == "deprecated") {
							if (eval (nv[1]) == "1") {
1018
								st.set_attribute_bool ("Version", "deprecated", true);
1019 1020
							}
						} else if (nv[0] == "replacement") {
1021
							st.set_attribute_string ("Version", "replacement", eval (nv[1]));
1022
						} else if (nv[0] == "deprecated_since") {
1023
							st.set_attribute_string ("Version", "deprecated_since", eval (nv[1]));
1024 1025 1026 1027
						} else if (nv[0] == "hidden") {
							if (eval (nv[1]) == "1") {
								return;
							}
1028 1029
						} else if (nv[0] == "experimental") {
							if (eval (nv[1]) == "1") {
1030
								st.set_attribute_bool ("Version", "experimental", true);
1031
							}
1032 1033 1034 1035
						}
					}
				}

1036
				add_symbol_to_container (container, st);
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
				current_source_file.add_node (st);
			}

			current_data_type = st;

			foreach (weak IdlNode member in un_node.members) {
				if (member.type == IdlNodeTypeId.FUNCTION) {
					var m = parse_function ((IdlNodeFunction) member);
					if (m != null) {
						st.add_method (m);
					}
				} else if (member.type == IdlNodeTypeId.FIELD) {
					var f = parse_field ((IdlNodeField) member);
					if (f != null) {
						st.add_field (f);
					}
				}
			}

			current_data_type = null;
		} else {
1058
			var cl = container.scope.lookup (name) as Class;
1059 1060 1061
			if (cl == null) {
				cl = new Class (name, current_source_reference);
				cl.access = SymbolAccessibility.PUBLIC;
1062
				cl.is_compact = true;
1063 1064 1065 1066 1067 1068

				var cl_attributes = get_attributes (node.name);
				if (cl_attributes != null) {
					foreach (string attr in cl_attributes) {
						var nv = attr.split ("=", 2);
						if (nv[0] == "cheader_filename") {
1069
							cl.set_attribute_string ("CCode", "cheader_filename", eval (nv[1]));
1070 1071 1072 1073 1074 1075 1076 1077
						} else if (nv[0] == "hidden") {
							if (eval (nv[1]) == "1") {
								return;
							}
						}
					}
				}

1078
				add_symbol_to_container (container, cl);
1079 1080 1081 1082 1083
				current_source_file.add_node (cl);
			}

			current_data_type = cl;

1084
			bool ref_function_void = false;
1085 1086 1087 1088 1089 1090 1091 1092 1093
			string ref_function = null;
			string unref_function = null;
			string copy_function = null;
			string free_function = null;

			foreach (weak IdlNode member in un_node.members) {
				if (member.type == IdlNodeTypeId.FUNCTION) {
					if (member.name == "ref") {
						ref_function = ((IdlNodeFunction) member).symbol;
1094
						ref_function_void = (parse_type (((IdlNodeFunction) member).result.type) is VoidType);
1095 1096
					} else if (member.name == "unref") {
						unref_function = ((IdlNodeFunction) member).symbol;
1097
					} else if (member.name == "free" || member.name == "destroy") {
1098 1099
						free_function = ((IdlNodeFunction) member).symbol;
					} else {
1100 1101 1102
						if (member.name == "copy") {
							copy_function = ((IdlNodeFunction) member).symbol;
						}
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
						var m = parse_function ((IdlNodeFunction) member);
						if (m != null) {
							cl.add_method (m);
						}
					}
				} else if (member.type == IdlNodeTypeId.FIELD) {
					var f = parse_field ((IdlNodeField) member);
					if (f != null) {
						cl.add_field (f);
					}
				}
			}
1115

1116
			if (ref_function != null) {
1117 1118 1119 1120 1121 1122
				cl.set_attribute_string ("CCode", "ref_function", ref_function);
				if (ref_function_void) {
					cl.set_attribute_bool ("CCode", "ref_function_void", ref_function_void);
				}
			} else if (copy_function != null) {
				cl.set_attribute_string ("CCode", "copy_function", copy_function);
1123
			}
1124
			if (unref_function != null) {
1125 1126 1127
				cl.set_attribute_string ("CCode", "unref_function", unref_function);
			} else if (free_function != null && free_function != "%sfree".printf (get_lower_case_cprefix (cl))) {
				cl.set_attribute_string ("CCode", "free_function", free_function);
1128 1129 1130 1131
			}

			current_data_type = null;
		}
1132
	}
1133

1134
	private void parse_boxed (IdlNodeBoxed boxed_node, Symbol container, IdlModule module) {
1135
		weak IdlNode node = (IdlNode) boxed_node;
1136

1137
		string name = fix_type_name (node.name, container);
1138

1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
		var node_attributes = get_attributes (node.name);
		if (node_attributes != null) {
			foreach (string attr in node_attributes) {
				var nv = attr.split ("=", 2);
				if (nv[0] == "hidden") {
					return;
				}
			}
		}

1149
		if (!is_reference_type (node.name)) {
1150
			var st = container.scope.lookup (name) as Struct;
1151 1152
			if (st == null) {
				st = new Struct (name, current_source_reference);
1153
				st.access = SymbolAccessibility.PUBLIC;
1154 1155 1156 1157 1158 1159

				var st_attributes = get_attributes (node.name);
				if (st_attributes != null) {
					foreach (string attr in st_attributes) {
						var nv = attr.split ("=", 2);
						if (nv[0] == "cheader_filename") {
1160
							st.set_attribute_string ("CCode", "cheader_filename", eval (nv[1]));
1161 1162
						} else if (nv[0] == "deprecated") {
							if (eval (nv[1]) == "1") {
1163
								st.set_attribute_bool ("Version", "deprecated", true);
1164 1165
							}
						} else if (nv[0] == "replacement") {
1166
							st.set_attribute_string ("Version", "replacement", eval (nv[1]));
1167
						} else if (nv[0] == "deprecated_since") {
1168
							st.set_attribute_string ("Version", "deprecated_since", eval (nv[1]));
1169 1170
						} else if (nv[0] == "immutable") {
							if (eval (nv[1]) == "1") {
1171
								st.set_attribute ("Immutable", true);
1172
							}
1173 1174
						} else if (nv[0] == "has_copy_function") {
							if (eval (nv[1]) == "0") {
1175
								st.set_attribute_bool ("CCode", "has_copy_function", false);
1176 1177 1178
							}
						} else if (nv[0] == "has_destroy_function") {
							if (eval (nv[1]) == "0") {
1179
								st.set_attribute_bool ("CCode", "has_destroy_function", false);
1180
							}
1181 1182
						} else if (nv[0] == "experimental") {
							if (eval (nv[1]) == "1") {
1183
								st.set_attribute_bool ("Version", "experimental", true);
1184
							}
1185
						}
1186
					}
1187
				}
1188

1189
				add_symbol_to_container (container, st);
1190 1191
				current_source_file.add_node (st);
			}
1192
		
1193 1194 1195 1196
			current_data_type = st;

			foreach (weak IdlNode member in boxed_node.members) {
				if (member.type == IdlNodeTypeId.FUNCTION) {
1197 1198 1199 1200
					var m = parse_function ((IdlNodeFunction) member);
					if (m != null) {
						st.add_method (m);
					}
1201 1202 1203 1204 1205
				} else if (member.type == IdlNodeTypeId.FIELD) {
					var f = parse_field ((IdlNodeField) member);
					if (f != null) {
						st.add_field (f);
					}
1206
				}
1207 1208 1209 1210
			}

			current_data_type = null;
		} else {
1211 1212 1213 1214 1215 1216
			bool ref_function_void = false;
			string ref_function = null;
			string unref_function = null;
			string copy_function = null;
			string free_function = null;

1217
			var cl = container.scope.lookup (name) as Class;
1218
			if (cl == null) {
1219 1220
				string base_class = null;

1221
				cl = new Class (name, current_source_reference);
1222
				cl.access = SymbolAccessibility.PUBLIC;
1223
				cl.is_compact = true;
1224 1225 1226
				if (boxed_node.gtype_init != null) {
					cl.set_attribute_string ("CCode", "type_id", "%s ()".printf (boxed_node.gtype_init));
				}
1227 1228 1229 1230 1231 1232

				var cl_attributes = get_attributes (node.name);
				if (cl_attributes != null) {
					foreach (string attr in cl_attributes) {
						var nv = attr.split ("=", 2);
						if (nv[0] == "cheader_filename") {
1233
							cl.set_attribute_string ("CCode", "cheader_filename", eval (nv[1]));
1234 1235
						} else if (nv[0] == "base_class") {
							base_class = eval (nv[1]);
1236 1237 1238 1239
						} else if (nv[0] == "is_immutable") {
							if (eval (nv[1]) == "1") {
								cl.is_immutable = true;
							}
1240 1241
						} else if (nv[0] == "deprecated") {
							if (eval (nv[1]) == "1") {
1242
								cl.set_attribute_bool ("Version", "deprecated", true);
1243 1244
							}
						} else if (nv[0] == "replacement") {
1245
							cl.set_attribute_string ("Version", "replacement", eval (nv[1]));
1246
						} else if (nv[0] == "deprecated_since") {
1247
							cl.set_attribute_string ("Version", "deprecated_since", eval (nv[1]));
1248
						} else if (nv[0] == "const_cname") {
1249
							cl.set_attribute_string ("CCode", "const_cname", eval (nv[1]));
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
						} else if (nv[0] == "free_function") {
							free_function = eval (nv[1]);
						} else if (nv[0] == "ref_function") {
							ref_function = eval (nv[1]);
						} else if (nv[0] == "unref_function") {
							unref_function = eval (nv[1]);
						} else if (nv[0] == "copy_function") {
							copy_function = eval (nv[1]);
						} else if (nv[0] == "ref_function_void") {
							if (eval (nv[1]) == "1") {
								ref_function_void = true;
							}
1262