valagidlparser.vala 24.6 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 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
 */

using GLib;

/**
 * Code visitor parsing all GIDL files.
 */
public class Vala.GIdlParser : CodeVisitor {
	private SourceReference current_source_reference;
	
	private DataType current_data_type;
	private HashTable<string,string> codenode_attributes_map;
	private HashTable<string,string> current_type_symbol_map;

	/**
	 * Parse all source files in the specified code context and build a
	 * code tree.
	 *
	 * @param context a code context
	 */
	public void parse (CodeContext! context) {
		context.accept (this);
	}

	public override void visit_begin_source_file (SourceFile! source_file) {
		if (source_file.filename.has_suffix (".gidl")) {
			parse_file (source_file);
		}
	}
	
	private void parse_file (SourceFile! source_file) {
		Error error = null;
		
		string metadata_filename = "%s.metadata".printf (source_file.filename.ndup (source_file.filename.size () - ".gidl".size ()));
		
		codenode_attributes_map = new HashTable.full (str_hash, str_equal, g_free, g_free);
		
		if (File.test (metadata_filename, FileTest.EXISTS)) {
			string metadata;
			File.get_contents (metadata_filename, out metadata, null, out error);
			
			foreach (string line in metadata.split ("\n")) {
				var line_parts = line.split (" ", 2);
				if (line_parts[0] == null) {
					continue;
				}
				
				codenode_attributes_map.insert (line_parts[0], line_parts[1]);
			}
		}
	
		var modules = Idl.parse_file (source_file.filename, out error);
		
		if (error != null) {
			stdout.printf ("error parsing GIDL file: %s\n", error.message);
		}
		
		current_source_reference = new SourceReference (source_file);
		
		foreach (IdlModule module in modules) {
			var ns = parse_module (module);
			source_file.add_namespace (ns);
		}
	}
	
	private ref Namespace parse_module (IdlModule! module) {
		var ns = new Namespace (module.name, current_source_reference);
		
		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]));
				}
			}
		}
		
		foreach (IdlNode node in module.entries) {
			if (node.type == IdlNodeTypeId.CALLBACK) {
				var cb = parse_callback ((IdlNodeFunction) node);
				if (cb == null) {
					continue;
				}
				if (cb.name.has_prefix (module.name)) {
					cb.name = cb.name.offset (module.name.len ());
				}
				ns.add_callback (cb);
			} else if (node.type == IdlNodeTypeId.STRUCT) {
				var st = parse_struct ((IdlNodeStruct) node);
				if (st == null) {
					continue;
				}
				if (st.name.has_prefix (module.name)) {
					st.name = st.name.offset (module.name.len ());
				}
				ns.add_struct (st);
			} else if (node.type == IdlNodeTypeId.BOXED) {
				var st = parse_boxed ((IdlNodeBoxed) node);
				if (st.name.has_prefix (module.name)) {
					st.name = st.name.offset (module.name.len ());
				}
				ns.add_struct (st);
125
				st.set_type_id (st.get_upper_case_cname ("TYPE_"));
Jürg Billeter's avatar
Jürg Billeter committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
			} else if (node.type == IdlNodeTypeId.ENUM) {
				var en = parse_enum ((IdlNodeEnum) node);
				if (en.name.has_prefix (module.name)) {
					en.name = en.name.offset (module.name.len ());
				}
				ns.add_enum (en);
			} else if (node.type == IdlNodeTypeId.OBJECT) {
				var cl = parse_object ((IdlNodeInterface) node);
				if (cl.name.has_prefix (module.name)) {
					cl.name = cl.name.offset (module.name.len ());
				}
				ns.add_class (cl);
			} else if (node.type == IdlNodeTypeId.INTERFACE) {
				var iface = parse_interface ((IdlNodeInterface) node);
				if (iface.name.has_prefix (module.name)) {
					iface.name = iface.name.offset (module.name.len ());
				}
				ns.add_interface (iface);
			} else if (node.type == IdlNodeTypeId.CONSTANT) {
				var c = parse_constant ((IdlNodeConstant) node);
				if (c.name.has_prefix (module.name.up ())) {
					c.name = c.name.offset (module.name.len () + 1);
				}
				ns.add_constant (c);
150 151 152 153 154 155
			} else if (node.type == IdlNodeTypeId.FUNCTION) {
				var m = parse_function ((IdlNodeFunction) node);
				if (m != null) {
					m.instance = false;
					ns.add_method (m);
				}
Jürg Billeter's avatar
Jürg Billeter committed
156 157 158 159 160 161 162
			}
		}
		
		return ns;
	}
	
	private ref Callback parse_callback (IdlNodeFunction! f_node) {
163
		weak IdlNode node = (IdlNode) f_node;
Jürg Billeter's avatar
Jürg Billeter committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

		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;
		
		foreach (IdlNodeParam param in f_node.parameters) {
181
			weak IdlNode param_node = (IdlNode) param;
Jürg Billeter's avatar
Jürg Billeter committed
182 183 184 185 186 187 188 189 190
			
			var p = new FormalParameter (param_node.name, parse_param (param));
			cb.add_parameter (p);
		}
		
		return cb;
	}
	
	private ref Struct parse_struct (IdlNodeStruct! st_node) {
191
		weak IdlNode node = (IdlNode) st_node;
Jürg Billeter's avatar
Jürg Billeter committed
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
		
		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);
				if (nv[0] == "is_value_type" && eval (nv[1]) == "1") {
					st.set_is_reference_type (false);
				}
			}
		}
		
		current_data_type = st;
		
		foreach (IdlNode member in st_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;
		
		return st;
	}
	
	private ref Struct parse_boxed (IdlNodeBoxed! boxed_node) {
234
		weak IdlNode node = (IdlNode) boxed_node;
Jürg Billeter's avatar
Jürg Billeter committed
235 236 237
	
		var st = new Struct (node.name, current_source_reference);
		st.access = MemberAccessibility.PUBLIC;
238 239 240

		st.set_is_reference_type (true);

241 242 243 244
		var st_attributes = get_attributes (node.name);
		if (st_attributes != null) {
			foreach (string attr in st_attributes) {
				var nv = attr.split ("=", 2);
245 246
				if (nv[0] == "is_value_type" && eval (nv[1]) == "1") {
					st.set_is_reference_type (false);
247 248 249 250
				}
			}
		}
		
Jürg Billeter's avatar
Jürg Billeter committed
251 252 253 254 255 256 257 258
		current_data_type = st;
		
		foreach (IdlNode member in boxed_node.members) {
			if (member.type == IdlNodeTypeId.FUNCTION) {
				var m = parse_function ((IdlNodeFunction) member);
				if (m != null) {
					st.add_method (m);
				}
259 260 261 262 263
			} 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
264 265 266 267 268 269 270 271 272
			}
		}

		current_data_type = null;
		
		return st;
	}
	
	private ref Enum parse_enum (IdlNodeEnum! en_node) {
273
		weak IdlNode node = (IdlNode) en_node;
Jürg Billeter's avatar
Jürg Billeter committed
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
	
		var en = new Enum (node.name, current_source_reference);
		en.access = MemberAccessibility.PUBLIC;
		
		string common_prefix = null;
		
		foreach (IdlNode value in en_node.values) {
			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);
				}
			}
		}
		
		en.set_cprefix (common_prefix);
		
		foreach (IdlNode value2 in en_node.values) {
			var ev = new EnumValue (value2.name.offset (common_prefix.len ()));
			en.add_value (ev);
		}
		
		return en;
	}
	
	private ref Class parse_object (IdlNodeInterface! node) {
		var cl = new Class (node.gtype_name, current_source_reference);
		cl.access = MemberAccessibility.PUBLIC;
		
308 309 310 311 312 313 314 315 316 317
		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
318 319 320 321 322 323 324 325 326 327 328 329 330 331
		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;
		
332 333 334 335 336 337 338 339
		current_type_symbol_map = new HashTable<string,string>.full (str_hash, str_equal, g_free, g_free);
		var current_type_vfunc_map = new HashTable<string,string>.full (str_hash, str_equal, g_free, g_free);
		
		foreach (IdlNode member in node.members) {
			if (member.type == IdlNodeTypeId.VFUNC) {
				current_type_vfunc_map.insert (member.name, "1");
			}
		}
Jürg Billeter's avatar
Jürg Billeter committed
340 341 342

		foreach (IdlNode member in node.members) {
			if (member.type == IdlNodeTypeId.FUNCTION) {
343 344 345
				bool is_virtual = current_type_vfunc_map.lookup (member.name) != null;
				
				var m = parse_function ((IdlNodeFunction) member, is_virtual);
Jürg Billeter's avatar
Jürg Billeter committed
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
				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);
				}
			}
		}
		
		foreach (IdlNode member in node.members) {
			if (member.type == IdlNodeTypeId.FIELD) {
				if (current_type_symbol_map.lookup (member.name) == null) {
					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);
			
			if (prop.get_accessor != null && current_type_symbol_map.lookup (getter) == null) {
				prop.no_accessor_method = true;
			}
			
			var setter = "set_%s".printf (prop.name);
			
			if (prop.set_accessor != null && current_type_symbol_map.lookup (setter) == null) {
				prop.no_accessor_method = true;
			}
		}
		
		current_data_type = null;
		current_type_symbol_map = null;
		
		return cl;
	}
	
	private ref Interface parse_interface (IdlNodeInterface! node) {
		var iface = new Interface (node.gtype_name, current_source_reference);
		iface.access = MemberAccessibility.PUBLIC;
		
		foreach (string prereq_name in node.prerequisites) {
			var prereq = new TypeReference ();
			parse_type_string (prereq, prereq_name);
400
			iface.add_prerequisite (prereq);
Jürg Billeter's avatar
Jürg Billeter committed
401 402 403 404
		}

		current_data_type = iface;

405 406 407 408 409 410 411
		var current_type_vfunc_map = new HashTable<string,string>.full (str_hash, str_equal, g_free, g_free);
		foreach (IdlNode member in node.members) {
			if (member.type == IdlNodeTypeId.VFUNC) {
				current_type_vfunc_map.insert (member.name, "1");
			}
		}

Jürg Billeter's avatar
Jürg Billeter committed
412 413
		foreach (IdlNode member in node.members) {
			if (member.type == IdlNodeTypeId.FUNCTION) {
414 415 416
				bool is_virtual = current_type_vfunc_map.lookup (member.name) != null;
				
				var m = parse_function ((IdlNodeFunction) member, is_virtual);
Jürg Billeter's avatar
Jürg Billeter committed
417 418 419
				if (m != null) {
					iface.add_method (m);
				}
420 421 422 423 424
			} 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
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
			}
		}

		current_data_type = null;
		
		return iface;
	}
	
	private ref TypeReference parse_type (IdlNodeType! type_node) {
		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";
514 515 516
				if (type_node.is_pointer) {
					type.array_rank = 1;
				}
Jürg Billeter's avatar
Jürg Billeter committed
517 518 519 520 521 522 523 524 525 526 527
			} else if (n == "gushort") {
				type.type_name = "ushort";
			} else if (n == "gshort") {
				type.type_name = "short";
			} else if (n == "gconstpointer") {
				type.type_name = "pointer";
			} else if (n == "value_array") {
				type.namespace_name = "GLib";
				type.type_name = "ValueArray";
			} else if (n == "time_t") {
				type.type_name = "ulong";
528 529
			} else if (n == "pid_t") {
				type.type_name = "int";
Jürg Billeter's avatar
Jürg Billeter committed
530 531 532 533 534
			} else if (n == "FILE") {
				type.namespace_name = "GLib";
				type.type_name = "File";
			} else {
				parse_type_string (type, n);
535 536 537
				if (type_node.is_pointer && is_value_type (n)) {
					type.reference_to_value_type = true;
				}
Jürg Billeter's avatar
Jürg Billeter committed
538 539 540 541 542 543 544
			}
		} else {
			stdout.printf ("%d\n", type_node.tag);
		}
		return type;
	}
	
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
	private bool is_value_type (string! type_name) {
		// FIXME only works if both types are in current package, e.g. doesn't work when Gtk uses GdkRectangle
		var type_attributes = get_attributes (type_name);
		if (type_attributes != null) {
			foreach (string attr in type_attributes) {
				var nv = attr.split ("=", 2);
				if (nv[0] == "is_value_type" && eval (nv[1]) == "1") {
					return true;
				}
			}
		}

		return false;
	}
	
Jürg Billeter's avatar
Jürg Billeter committed
560 561 562
	private void parse_type_string (TypeReference! type, string! n) {
		// Generated GIDL misses explicit namespace specifier,
		// so try to guess namespace
563
		if (n.has_prefix ("H") || n.has_suffix ("Class") || n == "va_list" || n.has_prefix ("LOGFONT")) {
Jürg Billeter's avatar
Jürg Billeter committed
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
			// 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";
			}
		} else if (n.has_prefix ("Atk")) {
			type.namespace_name = "Atk";
			type.type_name = n.offset ("Atk".len ());
			if (type.type_name == "AttributeSet") {
				type.namespace_name = "GLib";
				type.type_name = "SList";
			}
		} else if (n.has_prefix ("Gtk")) {
			type.namespace_name = "Gtk";
			type.type_name = n.offset ("Gtk".len ());
586
			if (type.type_name == "TextLayout") {
Jürg Billeter's avatar
Jürg Billeter committed
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
				type.namespace_name = null;
				type.type_name = "pointer";
			}
		} else if (n.has_prefix ("Gdk")) {
			type.namespace_name = "Gdk";
			type.type_name = n.offset ("Gdk".len ());
			if (type.type_name == "NativeWindow" || type.type_name == "XEvent") {
				type.namespace_name = null;
				type.type_name = "pointer";
			}
		} else if (n.has_prefix ("Pango")) {
			type.namespace_name = "Pango";
			type.type_name = n.offset ("Pango".len ());
			if (type.type_name == "Glyph") {
				type.namespace_name = null;
				type.type_name = "uint";
			} else if (type.type_name == "GlyphUnit") {
				type.namespace_name = null;
				type.type_name = "int";
			}
607 608 609
		} else if (n.has_prefix ("Vte")) {
			type.namespace_name = "Vte";
			type.type_name = n.offset ("Vte".len ());
Jürg Billeter's avatar
Jürg Billeter committed
610 611 612
		} else if (n.has_prefix ("Goo")) {
			type.namespace_name = "Goo";
			type.type_name = n.offset ("Goo".len ());
Jürg Billeter's avatar
Jürg Billeter committed
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
		} else if (n.has_prefix ("G")) {
			type.namespace_name = "GLib";
			type.type_name = n.offset ("G".len ());
			if (type.type_name == "Strv") {
				type.namespace_name = null;
				type.type_name = "string";
				type.array_rank = 1;
			}
		} 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];
			}
		}
	}
	
	private ref TypeReference parse_param (IdlNodeParam! param) {
		var type = parse_type (param.type);

		// disable for now as null_ok not yet correctly set
		// type.non_null = !param.null_ok;
		
		return type;
	}
	
641
	private ref Method parse_function (IdlNodeFunction! f, bool is_virtual = false) {
642
		weak IdlNode node = (IdlNode) f;
Jürg Billeter's avatar
Jürg Billeter committed
643 644 645 646 647 648 649 650 651 652
		
		if (f.deprecated) {
			return null;
		}
	
		TypeReference return_type = null;
		if (f.result != null) {
			return_type = parse_param (f.result);
		}
		
653 654 655 656 657 658 659 660 661 662 663
		Method m;
		if (f.is_constructor || node.name.has_prefix ("new")) {
			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
664 665
		m.access = MemberAccessibility.PUBLIC;
		
666 667
		m.is_virtual = is_virtual;
		
668 669 670
		// GIDL generator can't provide array parameter information yet
		m.no_array_length = true;
		
Jürg Billeter's avatar
Jürg Billeter committed
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
		if (current_type_symbol_map != null) {
			current_type_symbol_map.insert (node.name, "1");
		}
		
		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);
				if (nv[0] == "hidden") {
					if (eval (nv[1]) == "1") {
						return null;
					}
				} else if (nv[0] == "ellipsis") {
					if (eval (nv[1]) == "1") {
						add_ellipsis = true;
					}
				}
			}
		}
		
705 706
		m.set_cname (f.symbol);
		
Jürg Billeter's avatar
Jürg Billeter committed
707
		bool first = true;
708
		FormalParameter last_param = null;
Jürg Billeter's avatar
Jürg Billeter committed
709
		foreach (IdlNodeParam param in f.parameters) {
710
			weak IdlNode param_node = (IdlNode) param;
Jürg Billeter's avatar
Jürg Billeter committed
711 712 713 714 715 716 717 718
			
			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
719 720 721 722 723 724
					
					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
725 726 727 728 729 730 731 732 733
					continue;
				} else {
					// static method
					m.instance = false;
				}
			}
			
			var p = new FormalParameter (param_node.name, parse_param (param));
			m.add_parameter (p);
734 735 736 737 738 739 740 741

			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;
				last_param.type_reference.reference_to_value_type = false;
			}

			last_param = p;
Jürg Billeter's avatar
Jürg Billeter committed
742 743
		}
		
744 745 746 747 748
		if (first) {
			// no parameters => static method
			m.instance = false;
		}
		
Jürg Billeter's avatar
Jürg Billeter committed
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
		if (add_ellipsis) {
			m.add_parameter (new FormalParameter.with_ellipsis ());
		}
		
		return m;
	}
	
	private ref string! fix_prop_name (string name) {
		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;
	}

	private ref Property parse_property (IdlNodeProperty! prop_node) {
776
		weak IdlNode node = (IdlNode) prop_node;
Jürg Billeter's avatar
Jürg Billeter committed
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
		
		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;
		
		if (current_type_symbol_map != null) {
			current_type_symbol_map.insert (prop.name, "1");
		}
		
		return prop;
	}

	private ref Constant parse_constant (IdlNodeConstant! const_node) {
815
		weak IdlNode node = (IdlNode) const_node;
Jürg Billeter's avatar
Jürg Billeter committed
816 817 818 819 820 821 822 823 824 825 826 827
		
		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;
	}

	private ref Field parse_field (IdlNodeField! field_node) {
828
		weak IdlNode node = (IdlNode) field_node;
Jürg Billeter's avatar
Jürg Billeter committed
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
		
		var type = parse_type (field_node.type);
		if (type == null) {
			return null;
		}
		
		if (!field_node.readable) {
			return null;
		}
		
		if (current_type_symbol_map != null) {
			current_type_symbol_map.insert (node.name, "1");
		}
		
		var field = new Field (node.name, type, null, current_source_reference);
		field.access = MemberAccessibility.PUBLIC;
		
		return field;
	}
	
	private ref string[] get_attributes (string! codenode) {
		string attributes = codenode_attributes_map.lookup (codenode);
		if (attributes == null) {
			return null;
		}
		
		return attributes.split (" ");
	}
	
	private ref string eval (string! s) {
		return s.offset (1).ndup (s.size () - 2);
	}

	private ref Signal parse_signal (IdlNodeSignal! sig_node) {
863
		weak IdlNode node = (IdlNode) sig_node;
Jürg Billeter's avatar
Jürg Billeter committed
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
		
		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;
				}
			}
		}
		
		bool first = true;
		
		foreach (IdlNodeParam param in sig_node.parameters) {
			if (first) {
				// ignore implicit first signal parameter (sender)
				first = false;
				continue;
			}
		
894
			weak IdlNode param_node = (IdlNode) param;
Jürg Billeter's avatar
Jürg Billeter committed
895 896 897 898 899 900 901 902
			
			var p = new FormalParameter (param_node.name, parse_param (param));
			sig.add_parameter (p);
		}
		
		return sig;
	}
}