valagidlparser.vala 36.5 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
 *
 * 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
8
 * version 2.1 of the License, or (at your option) any later version.
Jürg Billeter's avatar
Jürg Billeter committed
9 10 11 12 13 14 15 16 17 18 19 20

 * 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
	private Map<string,string> codenode_attributes_map;
40
	private Map<pointer,string> codenode_attributes_patterns;
41
	private Gee.Set<string> current_type_symbol_set;
Jürg Billeter's avatar
Jürg Billeter committed
42

43 44
	private Map<string,DataType> cname_type_map;

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

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

		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
88 89
	}

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

		current_source_file = source_file;

101
		codenode_attributes_map = new HashMap<string,string> (str_hash, str_equal);
102 103
		codenode_attributes_patterns = new HashMap<pointer,string> (direct_hash, PatternSpec.equal);

104
		if (FileUtils.test (metadata_filename, FileTest.EXISTS)) {
105 106
			try {
				string metadata;
Jürg Billeter's avatar
Jürg Billeter committed
107
				ulong metadata_len;
108
				FileUtils.get_contents (metadata_filename, out metadata, out metadata_len);
Jürg Billeter's avatar
Jürg Billeter committed
109
				
110
				foreach (string line in metadata.split ("\n")) {
111 112 113 114 115
					if (line.has_prefix ("#")) {
						// ignore comment lines
						continue;
					}

116 117 118
					var tokens = line.split (" ", 2);

					if (null == tokens[0]) {
119 120
						continue;
					}
121 122 123 124 125

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

150
	private string! fix_type_name (string! type_name, Namespace! ns) {
151 152 153 154 155 156 157 158 159 160
		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]);
				}
			}
		}

161 162 163
		if (type_name.has_prefix (ns.name)) {
			return type_name.offset (ns.name.len ());
		} else if (ns.name == "GLib" && type_name.has_prefix ("G")) {
164
			return type_name.offset (1);
165
		} else  {
166 167 168 169 170
			string best_match = null;
			foreach (string cprefix in ns.get_cprefixes ()) {
				if (type_name.has_prefix (cprefix)) {
					if (best_match == null || cprefix.len () > best_match.len ())
						best_match = cprefix;
171 172
				}
			}
173 174 175 176

			if (best_match != null) {
				return type_name.offset (best_match.len ());;
			}
177
		}
178

179 180 181
		return type_name;
	}

182 183 184 185
	private string! fix_const_name (string! const_name, Namespace! ns) {
		if (const_name.has_prefix (ns.name.up () + "_")) {
			return const_name.offset (ns.name.len () + 1);
		} else if (ns.name == "GLib" && const_name.has_prefix ("G_")) {
186 187 188 189 190 191
			return const_name.offset (2);
		}
		return const_name;
	}

	private Namespace parse_module (IdlModule! module) {
192 193 194 195 196 197 198 199
		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);
		}
200 201 202

		current_namespace = ns;

Jürg Billeter's avatar
Jürg Billeter committed
203 204 205 206 207 208
		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]));
209
				} else if (nv[0] == "cprefix") {
210 211 212 213
					var cprefixes = eval (nv[1]).split (",");
					foreach(string name in cprefixes) {
						ns.add_cprefix (name);
					}
214 215
				} else if (nv[0] == "lower_case_cprefix") {
					ns.set_lower_case_cprefix (eval (nv[1]));
Jürg Billeter's avatar
Jürg Billeter committed
216 217 218 219
				}
			}
		}
		
220
		foreach (weak IdlNode node in module.entries) {
Jürg Billeter's avatar
Jürg Billeter committed
221 222 223 224 225
			if (node.type == IdlNodeTypeId.CALLBACK) {
				var cb = parse_callback ((IdlNodeFunction) node);
				if (cb == null) {
					continue;
				}
226
				cb.name = fix_type_name (cb.name, ns);
Jürg Billeter's avatar
Jürg Billeter committed
227
				ns.add_callback (cb);
228
				current_source_file.add_node (cb);
Jürg Billeter's avatar
Jürg Billeter committed
229
			} else if (node.type == IdlNodeTypeId.STRUCT) {
230
				parse_struct ((IdlNodeStruct) node, ns, module);
Jürg Billeter's avatar
Jürg Billeter committed
231
			} else if (node.type == IdlNodeTypeId.BOXED) {
232
				parse_boxed ((IdlNodeBoxed) node, ns, module);
Jürg Billeter's avatar
Jürg Billeter committed
233 234
			} else if (node.type == IdlNodeTypeId.ENUM) {
				var en = parse_enum ((IdlNodeEnum) node);
235
				en.name = fix_type_name (en.name, ns);
Jürg Billeter's avatar
Jürg Billeter committed
236
				ns.add_enum (en);
237
				current_source_file.add_node (en);
238 239 240 241 242 243
			} else if (node.type == IdlNodeTypeId.FLAGS) {
				var en = parse_enum ((IdlNodeEnum) node);
				en.name = fix_type_name (en.name, ns);
				en.is_flags = true;
				ns.add_enum (en);
				current_source_file.add_node (en);
Jürg Billeter's avatar
Jürg Billeter committed
244
			} else if (node.type == IdlNodeTypeId.OBJECT) {
245
				parse_object ((IdlNodeInterface) node, ns, module);
Jürg Billeter's avatar
Jürg Billeter committed
246
			} else if (node.type == IdlNodeTypeId.INTERFACE) {
247
				parse_interface ((IdlNodeInterface) node, ns, module);
Jürg Billeter's avatar
Jürg Billeter committed
248 249
			} else if (node.type == IdlNodeTypeId.CONSTANT) {
				var c = parse_constant ((IdlNodeConstant) node);
250
				c.name = fix_const_name (c.name, ns);
Jürg Billeter's avatar
Jürg Billeter committed
251
				ns.add_constant (c);
252
				current_source_file.add_node (c);
253 254 255 256 257
			} else if (node.type == IdlNodeTypeId.FUNCTION) {
				var m = parse_function ((IdlNodeFunction) node);
				if (m != null) {
					m.instance = false;
					ns.add_method (m);
258
					current_source_file.add_node (m);
259
				}
Jürg Billeter's avatar
Jürg Billeter committed
260 261
			}
		}
262 263 264

		current_namespace = null;

265 266 267
		if (sym is Namespace) {
			return null;
		}
Jürg Billeter's avatar
Jürg Billeter committed
268 269 270
		return ns;
	}
	
271
	private Callback parse_callback (IdlNodeFunction! f_node) {
272
		weak IdlNode node = (IdlNode) f_node;
Jürg Billeter's avatar
Jürg Billeter committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286

		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);
287
		cb.access = SymbolAccessibility.PUBLIC;
Jürg Billeter's avatar
Jürg Billeter committed
288
		
289
		foreach (weak IdlNodeParam param in f_node.parameters) {
290
			weak IdlNode param_node = (IdlNode) param;
Jürg Billeter's avatar
Jürg Billeter committed
291 292 293 294 295 296 297
			
			var p = new FormalParameter (param_node.name, parse_param (param));
			cb.add_parameter (p);
		}
		
		return cb;
	}
298 299 300 301 302 303 304 305 306 307 308 309 310 311

	private bool is_reference_type (string! cname) {
		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;
	}

312
	private void parse_struct (IdlNodeStruct! st_node, Namespace! ns, IdlModule! module) {
313
		weak IdlNode node = (IdlNode) st_node;
Jürg Billeter's avatar
Jürg Billeter committed
314 315
		
		if (st_node.deprecated) {
316
			return;
Jürg Billeter's avatar
Jürg Billeter committed
317 318
		}

319
		string name = fix_type_name (node.name, ns);
320

321 322 323 324
		if (!is_reference_type (node.name)) {
			var st = ns.scope.lookup (name) as Struct;
			if (st == null) {
				st = new Struct (name, current_source_reference);
325
				st.access = SymbolAccessibility.PUBLIC;
326 327 328 329 330 331 332 333 334 335 336

				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") {
							st.add_cheader_filename (eval (nv[1]));
						} else if (nv[0] == "hidden") {
							if (eval (nv[1]) == "1") {
								return;
							}
337
						}
Jürg Billeter's avatar
Jürg Billeter committed
338
					}
Jürg Billeter's avatar
Jürg Billeter committed
339
				}
340 341 342

				ns.add_struct (st);
				current_source_file.add_node (st);
Jürg Billeter's avatar
Jürg Billeter committed
343
			}
344

345
			current_data_type = st;
346

347 348
			foreach (weak IdlNode member in st_node.members) {
				if (member.type == IdlNodeTypeId.FUNCTION) {
349 350 351 352
					var m = parse_function ((IdlNodeFunction) member);
					if (m != null) {
						st.add_method (m);
					}
353 354 355 356 357
				} 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
358
				}
359 360 361 362 363 364 365
			}

			current_data_type = null;
		} else {
			var cl = ns.scope.lookup (name) as Class;
			if (cl == null) {
				cl = new Class (name, current_source_reference);
366
				cl.access = SymbolAccessibility.PUBLIC;
367 368 369 370 371 372 373 374 375 376 377 378 379

				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") {
							cl.add_cheader_filename (eval (nv[1]));
						} else if (nv[0] == "hidden") {
							if (eval (nv[1]) == "1") {
								return;
							}
						}
					}
Jürg Billeter's avatar
Jürg Billeter committed
380
				}
381 382 383

				ns.add_class (cl);
				current_source_file.add_node (cl);
Jürg Billeter's avatar
Jürg Billeter committed
384 385
			}

386 387 388 389
			current_data_type = cl;

			string ref_function = null;
			string unref_function = null;
390
			string copy_function = null;
391 392 393 394 395 396 397 398
			string free_function = null;

			foreach (weak IdlNode member in st_node.members) {
				if (member.type == IdlNodeTypeId.FUNCTION) {
					if (member.name == "ref") {
						ref_function = ((IdlNodeFunction) member).symbol;
					} else if (member.name == "unref") {
						unref_function = ((IdlNodeFunction) member).symbol;
399
					} else if (member.name == "free" || member.name == "destroy") {
400 401
						free_function = ((IdlNodeFunction) member).symbol;
					} else {
402 403 404
						if (member.name == "copy") {
							copy_function = ((IdlNodeFunction) member).symbol;
						}
405 406 407 408 409 410 411 412 413 414 415 416
						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);
					}
				}
			}
417

418 419 420
			if (ref_function != null) {
				cl.set_ref_function (ref_function);
			}
421 422 423
			if (copy_function != null) {
				cl.set_dup_function (copy_function);
			}
424 425 426 427 428 429 430 431
			if (unref_function != null) {
				cl.set_unref_function (unref_function);
			} else if (free_function != null) {
				cl.set_free_function (free_function);
			}

			current_data_type = null;
		}
Jürg Billeter's avatar
Jürg Billeter committed
432 433
	}
	
434
	private void parse_boxed (IdlNodeBoxed! boxed_node, Namespace! ns, IdlModule! module) {
435
		weak IdlNode node = (IdlNode) boxed_node;
Jürg Billeter's avatar
Jürg Billeter committed
436
	
437
		string name = fix_type_name (node.name, ns);
438

439 440 441 442
		if (!is_reference_type (node.name)) {
			var st = ns.scope.lookup (name) as Struct;
			if (st == null) {
				st = new Struct (name, current_source_reference);
443
				st.access = SymbolAccessibility.PUBLIC;
444 445 446 447 448 449 450 451

				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") {
							st.add_cheader_filename (eval (nv[1]));
						}
452
					}
453
				}
454

455 456 457 458
				ns.add_struct (st);
				st.set_type_id (st.get_upper_case_cname ("TYPE_"));
				current_source_file.add_node (st);
			}
459
		
460 461 462 463
			current_data_type = st;

			foreach (weak IdlNode member in boxed_node.members) {
				if (member.type == IdlNodeTypeId.FUNCTION) {
464 465 466 467
					var m = parse_function ((IdlNodeFunction) member);
					if (m != null) {
						st.add_method (m);
					}
468 469 470 471 472
				} 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
473
				}
474 475 476 477 478 479 480
			}

			current_data_type = null;
		} else {
			var cl = ns.scope.lookup (name) as Class;
			if (cl == null) {
				cl = new Class (name, current_source_reference);
481
				cl.access = SymbolAccessibility.PUBLIC;
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496

				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") {
							cl.add_cheader_filename (eval (nv[1]));
						}
					}
				}

				ns.add_class (cl);
				cl.set_type_id (cl.get_upper_case_cname ("TYPE_"));
				current_source_file.add_node (cl);
			}
497 498 499 500 501 502

			var parent = new TypeReference ();
			parent.namespace_name = "GLib";
			parent.type_name = "Boxed";
			cl.add_base_type (parent);

503 504 505 506
			current_data_type = cl;

			string ref_function = null;
			string unref_function = null;
507
			string copy_function = null;
508 509 510 511 512 513 514 515
			string free_function = null;

			foreach (weak IdlNode member in boxed_node.members) {
				if (member.type == IdlNodeTypeId.FUNCTION) {
					if (member.name == "ref") {
						ref_function = ((IdlNodeFunction) member).symbol;
					} else if (member.name == "unref") {
						unref_function = ((IdlNodeFunction) member).symbol;
516
					} else if (member.name == "free" || member.name == "destroy") {
517 518
						free_function = ((IdlNodeFunction) member).symbol;
					} else {
519 520 521
						if (member.name == "copy") {
							copy_function = ((IdlNodeFunction) member).symbol;
						}
522 523 524 525 526 527 528 529 530 531
						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);
					}
532
				}
Jürg Billeter's avatar
Jürg Billeter committed
533 534
			}

535 536 537
			if (ref_function != null) {
				cl.set_ref_function (ref_function);
			}
538 539 540
			if (copy_function != null) {
				cl.set_dup_function (copy_function);
			}
541 542 543 544 545
			if (unref_function != null) {
				cl.set_unref_function (unref_function);
			} else if (free_function != null) {
				cl.set_free_function (free_function);
			}
546

547 548
			current_data_type = null;
		}
Jürg Billeter's avatar
Jürg Billeter committed
549 550
	}
	
551
	private Enum parse_enum (IdlNodeEnum! en_node) {
552
		weak IdlNode node = (IdlNode) en_node;
Jürg Billeter's avatar
Jürg Billeter committed
553 554
	
		var en = new Enum (node.name, current_source_reference);
555
		en.access = SymbolAccessibility.PUBLIC;
Jürg Billeter's avatar
Jürg Billeter committed
556 557 558
		
		string common_prefix = null;
		
559
		foreach (weak IdlNode value in en_node.values) {
Jürg Billeter's avatar
Jürg Billeter committed
560 561 562 563 564 565 566 567 568 569 570
			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
571 572 573 574 575
			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
576 577 578 579
		}
		
		en.set_cprefix (common_prefix);
		
580
		foreach (weak IdlNode value2 in en_node.values) {
Jürg Billeter's avatar
Jürg Billeter committed
581 582 583 584 585 586 587
			var ev = new EnumValue (value2.name.offset (common_prefix.len ()));
			en.add_value (ev);
		}
		
		return en;
	}
	
588
	private void parse_object (IdlNodeInterface! node, Namespace! ns, IdlModule! module) {
589
		string name = fix_type_name (((IdlNode) node).name, ns);
590

Jürg Billeter's avatar
Jürg Billeter committed
591 592
		string base_class = null;

593 594 595
		var cl = ns.scope.lookup (name) as Class;
		if (cl == null) {
			cl = new Class (name, current_source_reference);
596
			cl.access = SymbolAccessibility.PUBLIC;
597 598 599 600 601 602 603
			
			var attributes = get_attributes (node.gtype_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
604 605
					} else if (nv[0] == "base_class") {
						base_class = eval (nv[1]);
606 607 608 609
					} else if (nv[0] == "hidden") {
						if (eval (nv[1]) == "1") {
							return;
						}
Jürg Billeter's avatar
Jürg Billeter committed
610
					}
611 612
				}
			}
613 614 615

			ns.add_class (cl);
			current_source_file.add_node (cl);
616
		}
617

Jürg Billeter's avatar
Jürg Billeter committed
618 619 620 621 622
		if (base_class != null) {
			var parent = new TypeReference ();
			parse_type_string (parent, base_class);
			cl.add_base_type (parent);
		} else if (node.parent != null) {
Jürg Billeter's avatar
Jürg Billeter committed
623 624 625
			var parent = new TypeReference ();
			parse_type_string (parent, node.parent);
			cl.add_base_type (parent);
626 627 628 629 630
		} else {
			var parent = new TypeReference ();
			parent.namespace_name = "GLib";
			parent.type_name = "Object";
			cl.add_base_type (parent);
Jürg Billeter's avatar
Jürg Billeter committed
631 632 633 634 635 636 637 638 639 640
		}
		
		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;
		
641 642
		current_type_symbol_set = new HashSet<string> (str_hash, str_equal);
		var current_type_vfunc_map = new HashMap<string,string> (str_hash, str_equal);
643
		
644
		foreach (weak IdlNode member in node.members) {
645
			if (member.type == IdlNodeTypeId.VFUNC) {
646
				current_type_vfunc_map.set (member.name, "1");
647 648
			}
		}
Jürg Billeter's avatar
Jürg Billeter committed
649

650
		foreach (weak IdlNode member in node.members) {
Jürg Billeter's avatar
Jürg Billeter committed
651
			if (member.type == IdlNodeTypeId.FUNCTION) {
652
				bool is_virtual = current_type_vfunc_map.get (member.name) != null;
653 654
				
				var m = parse_function ((IdlNodeFunction) member, is_virtual);
Jürg Billeter's avatar
Jürg Billeter committed
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
				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);
				}
			}
		}
		
671
		foreach (weak IdlNode member in node.members) {
Jürg Billeter's avatar
Jürg Billeter committed
672
			if (member.type == IdlNodeTypeId.FIELD) {
673
				if (!current_type_symbol_set.contains (member.name)) {
Jürg Billeter's avatar
Jürg Billeter committed
674 675 676 677 678 679 680 681 682 683 684
					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);
			
685
			if (prop.get_accessor != null && !current_type_symbol_set.contains (getter)) {
Jürg Billeter's avatar
Jürg Billeter committed
686 687 688 689 690
				prop.no_accessor_method = true;
			}
			
			var setter = "set_%s".printf (prop.name);
			
691
			if (prop.set_accessor != null && !current_type_symbol_set.contains (setter)) {
Jürg Billeter's avatar
Jürg Billeter committed
692 693 694 695 696
				prop.no_accessor_method = true;
			}
		}
		
		current_data_type = null;
697
		current_type_symbol_set = null;
Jürg Billeter's avatar
Jürg Billeter committed
698
	}
699

700
	private void parse_interface (IdlNodeInterface! node, Namespace! ns, IdlModule! module) {
701
		string name = fix_type_name (node.gtype_name, ns);
702 703 704 705

		var iface = ns.scope.lookup (name) as Interface;
		if (iface == null) {
			iface = new Interface (name, current_source_reference);
706
			iface.access = SymbolAccessibility.PUBLIC;
707 708 709 710 711 712 713 714
			
			var attributes = get_attributes (node.gtype_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]));
					}
715 716
				}
			}
717 718 719 720 721 722 723 724 725
			
			foreach (string prereq_name in node.prerequisites) {
				var prereq = new TypeReference ();
				parse_type_string (prereq, prereq_name);
				iface.add_prerequisite (prereq);
			}

			ns.add_interface (iface);
			current_source_file.add_node (iface);
Jürg Billeter's avatar
Jürg Billeter committed
726 727 728 729
		}

		current_data_type = iface;

730
		var current_type_vfunc_map = new HashMap<string,string> (str_hash, str_equal);
731
		foreach (weak IdlNode member in node.members) {
732
			if (member.type == IdlNodeTypeId.VFUNC) {
733
				current_type_vfunc_map.set (member.name, "1");
734 735 736
			}
		}

737
		foreach (weak IdlNode member in node.members) {
Jürg Billeter's avatar
Jürg Billeter committed
738
			if (member.type == IdlNodeTypeId.FUNCTION) {
739
				bool is_virtual = current_type_vfunc_map.get (member.name) != null;
740
				
Jürg Billeter's avatar
Jürg Billeter committed
741
				var m = parse_function ((IdlNodeFunction) member, is_virtual, true);
Jürg Billeter's avatar
Jürg Billeter committed
742 743 744
				if (m != null) {
					iface.add_method (m);
				}
745 746 747 748 749
			} 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
750 751 752 753 754 755
			}
		}

		current_data_type = null;
	}
	
756
	private TypeReference parse_type (IdlNodeType! type_node) {
Jürg Billeter's avatar
Jürg Billeter committed
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 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 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
		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";
831 832 833
				if (type_node.unparsed.has_suffix ("**")) {
					type.is_out = true;
				}
Jürg Billeter's avatar
Jürg Billeter committed
834 835 836 837 838 839
			} 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";
840 841 842
				if (type_node.is_pointer) {
					type.array_rank = 1;
				}
Jürg Billeter's avatar
Jürg Billeter committed
843 844 845 846
			} else if (n == "gushort") {
				type.type_name = "ushort";
			} else if (n == "gshort") {
				type.type_name = "short";
847
			} else if (n == "gconstpointer" || n == "void") {
Jürg Billeter's avatar
Jürg Billeter committed
848
				type.type_name = "pointer";
Jürg Billeter's avatar
Jürg Billeter committed
849
			} else if (n == "goffset" || n == "off_t") {
850
				type.type_name = "int64";
Jürg Billeter's avatar
Jürg Billeter committed
851 852 853 854 855
			} 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
856 857
			} else if (n == "socklen_t") {
				type.type_name = "uint32";
858 859
			} else if (n == "mode_t") {
				type.type_name = "uint";
Jürg Billeter's avatar
Jürg Billeter committed
860
			} else if (n == "gint" || n == "pid_t") {
861
				type.type_name = "int";
862 863
			} else if (n == "unsigned" || n == "unsigned-int") {
				type.type_name = "uint";
Jürg Billeter's avatar
Jürg Billeter committed
864 865
			} else if (n == "FILE") {
				type.namespace_name = "GLib";
866
				type.type_name = "FileStream";
Jürg Billeter's avatar
Jürg Billeter committed
867 868
			} else if (n == "struct") {
				type.type_name = "pointer";
869 870
			} else if (n == "iconv_t") {
				type.type_name = "pointer";
871 872 873 874 875 876
			} 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
877 878
			} else {
				parse_type_string (type, n);
879 880 881 882 883
				if (is_value_type (n)) {
					if (type_node.is_pointer) {
						type.is_out = true;
					}
				} else if (type_node.unparsed.has_suffix ("**")) {
884
					type.is_out = true;
885
				}
Jürg Billeter's avatar
Jürg Billeter committed
886 887 888 889 890 891 892
			}
		} else {
			stdout.printf ("%d\n", type_node.tag);
		}
		return type;
	}
	
893 894 895
	private bool is_value_type (string! type_name) {
		var type_attributes = get_attributes (type_name);
		if (type_attributes != null) {
896
			// type in the same package
897 898 899 900 901 902
			foreach (string attr in type_attributes) {
				var nv = attr.split ("=", 2);
				if (nv[0] == "is_value_type" && eval (nv[1]) == "1") {
					return true;
				}
			}
903 904 905 906 907 908
		} else {
			// type in a dependency package
			var dt = cname_type_map[type_name];
			if (dt != null) {
				return !dt.is_reference_type ();
			}
909 910 911 912 913
		}

		return false;
	}
	
Jürg Billeter's avatar
Jürg Billeter committed
914
	private void parse_type_string (TypeReference! type, string! n) {
915 916 917 918 919 920 921
		var dt = cname_type_map[n];
		if (dt != null) {
			type.namespace_name = dt.parent_symbol.name;
			type.type_name = dt.name;
			return;
		}

922 923 924 925 926 927 928 929 930 931 932 933 934
		var type_attributes = get_attributes (n);

		if (null != type_attributes) {
			foreach (string attr in type_attributes) {
				var nv = attr.split ("=", 2);

				if (nv[0] == "cprefix") {
					type.type_name = n.offset (eval (nv[1]).len ());
					return;
				}
			}
		}

935
		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
936 937 938 939 940 941 942 943 944 945 946 947
			// 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";
			}
948 949 950 951 952 953 954 955 956 957 958 959 960
		} 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
961 962 963
		} else if (n == "SoupProtocol") {
			type.namespace_name = "GLib";
			type.type_name = "Quark";
964 965 966 967 968 969
		} 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
970 971
		} else if (n.has_prefix ("G")) {
			type.namespace_name = "GLib";
972
			type.type_name = n.offset (1);
Jürg Billeter's avatar
Jürg Billeter committed
973 974 975 976 977 978 979 980 981 982 983
		} 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];
			}
		}
	}
	
984
	private TypeReference parse_param (IdlNodeParam! param) {
Jürg Billeter's avatar
Jürg Billeter committed
985 986 987 988 989 990 991 992
		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
993
	private Method parse_function (IdlNodeFunction! f, bool is_virtual = false, bool is_interface = false) {
994
		weak IdlNode node = (IdlNode) f;
Jürg Billeter's avatar
Jürg Billeter committed
995 996 997 998 999 1000 1001 1002 1003 1004
		
		if (f.deprecated) {
			return null;
		}
	
		TypeReference return_type = null;
		if (f.result != null) {
			return_type = parse_param (f.result);
		}
		
1005
		Method m;
Jürg Billeter's avatar
Jürg Billeter committed
1006
		if (!is_interface && (f.is_constructor || node.name.has_prefix ("new"))) {
1007
			m = new CreationMethod (null, node.name, current_source_reference);
1008 1009 1010 1011 1012 1013 1014 1015
			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);
		}
1016
		m.access = SymbolAccessibility.PUBLIC;
1017 1018 1019

		m.is_virtual = is_virtual && !is_interface;
		m.is_abstract = is_virtual && is_interface;
1020
		
1021 1022 1023
		// GIDL generator can't provide array parameter information yet
		m.no_array_length = true;
		
1024 1025
		if (current_type_symbol_set != null) {
			current_type_symbol_set.add (node.name);
Jürg Billeter's avatar
Jürg Billeter committed
1026 1027 1028
		}
		
		if (current_data_type != null) {
1029
			var sig_attributes = get_attributes ("%s::%s".printf (current_data_type.get_cname (), node.name));
Jürg Billeter's avatar
Jürg Billeter committed
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
			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);
1046 1047 1048 1049
				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
1050 1051 1052 1053 1054 1055 1056
					if (eval (nv[1]) == "1") {
						return null;
					}
				} else if (nv[0] == "ellipsis") {
					if (eval (nv[1]) == "1") {
						add_ellipsis = true;
					}
1057 1058 1059 1060
				} else if (nv[0] == "transfer_ownership") {
					if (eval (nv[1]) == "1") {
						return_type.transfers_ownership = true;
					}
1061 1062
				} else if (nv[0] == "sentinel") {
					m.sentinel = eval (nv[1]);
1063 1064 1065 1066 1067
				} else if (nv[0] == "is_array") {
					if (eval (nv[1]) == "1") {
						return_type.array_rank = 1;
						return_type.is_out = false;
					}
Jürg Billeter's avatar
Jürg Billeter committed
1068 1069 1070 1071
				}
			}
		}
		
1072 1073
		m.set_cname (f.symbol);
		
Jürg Billeter's avatar
Jürg Billeter committed
1074
		bool first = true;
1075
		FormalParameter last_param = null;
1076
		foreach (weak IdlNodeParam param in f.parameters) {
1077
			weak IdlNode param_node = (IdlNode) param;
Jürg Billeter's avatar
Jürg Billeter committed
1078 1079 1080
			
			if (first) {
				first = false;
1081 1082
				if (!(m is CreationMethod) &&
				    current_data_type != null &&
Jürg Billeter's avatar
Jürg Billeter committed
1083 1084
				    param.type.is_interface &&
				    (param_node.name == "self" ||
1085
				     param.type.@interface.has_suffix (current_data_type.get_cname ()))) {
Jürg Billeter's avatar
Jürg Billeter committed
1086 1087 1088 1089 1090 1091 1092
					// instance method
					continue;
				} else {
					// static method
					m.instance = false;
				}
			}
1093 1094 1095 1096 1097

			if (param.type.is_error) {
				m.add_error_domain (parse_type (param.type));
				continue;
			}
Jürg Billeter's avatar
Jürg Billeter committed
1098 1099 1100
			
			var p = new FormalParameter (param_node.name, parse_param (param));
			m.add_parameter (p);
1101

1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
			var attributes = get_attributes ("%s.%s".printf (f.symbol, param_node.name));
			if (attributes != null) {
				foreach (string attr in attributes) {
					var nv = attr.split ("=", 2);
					if (nv[0] == "is_array") {
						p.type_reference.array_rank = 1;
						p.type_reference.is_out = false;
					}
				}
			}

1113 1114 1115
			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;
1116
				last_param.type_reference.is_out = false;
1117 1118 1119
			}

			last_param = p;
Jürg Billeter's avatar
Jürg Billeter committed
1120 1121
		}
		
1122 1123 1124 1125
		if (first) {
			// no parameters => static method
			m.instance = false;
		}
1126 1127 1128 1129

		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
1130 1131 1132 1133 1134 1135
			m.add_parameter (new FormalParameter.with_ellipsis ());
		}
		
		return m;
	}
	
1136
	private string! fix_prop_name (string name) {
Jürg Billeter's avatar
Jürg Billeter committed
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
		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;
	}

1155
	private Property parse_property (IdlNodeProperty! prop_node) {
1156
		weak IdlNode node = (IdlNode) prop_node;
Jürg Billeter's avatar
Jürg Billeter committed
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
		
		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);
1184
		prop.access = SymbolAccessibility.PUBLIC;
Jürg Billeter's avatar
Jürg Billeter committed
1185
		prop.interface_only = true;
1186

1187
		var attributes = get_attributes ("%s:%s".printf (current_data_type.get_cname (), node.name));
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
		if (attributes != null) {
			foreach (string attr in attributes) {
				var nv = attr.split ("=", 2);
				if (nv[0] == "hidden") {
					if (eval (nv[1]) == "1") {
						return null;
					}
				}
			}
		}

1199 1200
		if (current_type_symbol_set != null) {
			current_type_symbol_set.add (prop.name);
Jürg Billeter's avatar
Jürg Billeter committed
1201 1202 1203 1204 1205
		}
		
		return prop;
	}

1206
	private Constant parse_constant (IdlNodeConstant! const_node) {
1207
		weak IdlNode node = (IdlNode) const_node;
Jürg Billeter's avatar
Jürg Billeter committed
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
		
		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;
	}

1219
	private Field parse_field (IdlNodeField! field_node) {
1220
		weak IdlNode node = (IdlNode) field_node;
Jürg Billeter's avatar
Jürg Billeter committed
1221 1222 1223 1224 1225
		
		var type = parse_type (field_node.type);
		if (type == null) {
			return null;
		}
Jürg Billeter's avatar
Jürg Billeter committed
1226

1227
		var attributes = get_attributes ("%s.%s".printf (current_data_type.get_cname (), node.name));
Jürg Billeter's avatar
Jürg Billeter committed
1228 1229 1230 1231 1232 1233 1234
		if (attributes != null) {
			foreach (string attr in attributes) {
				var nv = attr.split ("=", 2);
				if (nv[0] == "hidden") {
					if (eval (nv[1]) == "1") {
						return null;
					}
1235 1236 1237 1238 1239 1240 1241 1242
				} 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
1243 1244 1245
				}
			}
		}
Jürg Billeter's avatar
Jürg Billeter committed
1246
		
1247 1248
		if (current_type_symbol_set != null) {
			current_type_symbol_set.add (node.name);
Jürg Billeter's avatar
Jürg Billeter committed
1249 1250 1251
		}
		
		var field = new Field (node.name, type, null, current_source_reference);
1252
		field.access = SymbolAccessibility.PUBLIC;
Jürg Billeter's avatar
Jürg Billeter committed
1253 1254 1255
		
		return field;
	}
1256 1257

	[NoArrayLength]
1258
	private string[] get_attributes (string! codenode) {
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
		var attributes = codenode_attributes_map.get (codenode);

		if (attributes == null) {
			var dot_required = (null != codenode.chr (-1, '.'));
			var colon_required = (null != codenode.chr (-1, ':'));

			var pattern_specs = codenode_attributes_patterns.get_keys ();
			foreach (weak PatternSpec pattern in pattern_specs) {
				var pspec = codenode_attributes_patterns[pattern];

				if ((dot_required && null == pspec.chr (-1, '.')) ||
				    (colon_required && null == pspec.chr (-1, ':'))) {
					continue;
				}

				if (pattern.match_string (codenode)) {
					return get_attributes (pspec);
				}
			}
		}

Jürg Billeter's avatar
Jürg Billeter committed
1280 1281 1282 1283 1284 1285 1286
		if (attributes == null) {
			return null;
		}
		
		return attributes.split (" ");
	}