valaccodeattribute.vala 40.5 KB
Newer Older
Luca Bruno's avatar
Luca Bruno committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/* valaccodeattribute.vala
 *
 * Copyright (C) 2011  Luca Bruno
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 * Author:
 * 	Luca Bruno <lucabru@src.gnome.org>
 */


/**
 * Cache for the CCode attribute
 */
public class Vala.CCodeAttribute : AttributeCache {
	private weak CodeNode node;
29
	private weak Symbol? sym;
Luca Bruno's avatar
Luca Bruno committed
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
	private Attribute ccode;

	public string name {
		get {
			if (_name == null) {
				if (ccode != null) {
					_name = ccode.get_string ("cname");
				}
				if (_name == null) {
					_name = get_default_name ();
				}
			}
			return _name;
		}
	}

	public string const_name {
		get {
			if (_const_name == null) {
				if (ccode != null) {
					_const_name = ccode.get_string ("const_cname");
				}
				if (_const_name == null) {
					_const_name = get_default_const_name ();
				}
			}
			return _const_name;
		}
	}

	public string type_name {
		get {
			if (_type_name == null) {
				if (ccode != null) {
					_type_name = ccode.get_string ("type_cname");
				}
				if (_type_name == null) {
67
					_type_name = "%sIface".printf (get_ccode_name (sym));
Luca Bruno's avatar
Luca Bruno committed
68 69 70 71 72 73
				}
			}
			return _type_name;
		}
	}

74 75 76 77 78 79 80 81 82 83 84 85 86 87
	public string feature_test_macros {
		get {
			if (_feature_test_macros == null) {
				if (ccode != null) {
					_feature_test_macros = ccode.get_string ("feature_test_macro");
				}
				if (_feature_test_macros == null) {
					_feature_test_macros = "";
				}
			}
			return _feature_test_macros;
		}
	}

Luca Bruno's avatar
Luca Bruno committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
	public string header_filenames {
		get {
			if (_header_filenames == null) {
				if (ccode != null) {
					_header_filenames = ccode.get_string ("cheader_filename");
				}
				if (_header_filenames == null) {
					_header_filenames = get_default_header_filenames ();
				}
			}
			return _header_filenames;
		}
	}

	public string prefix {
		get {
			if (_prefix == null) {
				if (ccode != null) {
					_prefix = ccode.get_string ("cprefix");
				}
				if (_prefix == null) {
					_prefix = get_default_prefix ();
				}
			}
			return _prefix;
		}
	}

	public string lower_case_prefix {
		get {
			if (_lower_case_prefix == null) {
				if (ccode != null) {
					_lower_case_prefix = ccode.get_string ("lower_case_cprefix");
					if (_lower_case_prefix == null && (sym is ObjectTypeSymbol || sym is Struct)) {
						_lower_case_prefix = ccode.get_string ("cprefix");
					}
				}
				if (_lower_case_prefix == null) {
					_lower_case_prefix = get_default_lower_case_prefix ();
				}
			}
			return _lower_case_prefix;
		}
	}

	public string lower_case_suffix {
		get {
			if (_lower_case_suffix == null) {
				if (ccode != null) {
					_lower_case_suffix = ccode.get_string ("lower_case_csuffix");
				}
				if (_lower_case_suffix == null) {
					_lower_case_suffix = get_default_lower_case_suffix ();
				}
			}
			return _lower_case_suffix;
		}
	}

	public string ref_function {
		get {
			if (!ref_function_set) {
				if (ccode != null) {
					_ref_function = ccode.get_string ("ref_function");
				}
				if (_ref_function == null) {
					_ref_function = get_default_ref_function ();
				}
				ref_function_set = true;
			}
			return _ref_function;
		}
	}

	public bool ref_function_void {
		get {
			if (_ref_function_void == null) {
				if (ccode != null && ccode.has_argument ("ref_function_void")) {
					_ref_function_void = ccode.get_bool ("ref_function_void");
				} else {
					var cl = (Class) sym;
					if (cl.base_class != null) {
170
						_ref_function_void = get_ccode_ref_function_void (cl.base_class);
Luca Bruno's avatar
Luca Bruno committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
					} else {
						_ref_function_void = false;
					}
				}
			}
			return _ref_function_void;
		}
	}

	public string unref_function {
		get {
			if (!unref_function_set) {
				if (ccode != null) {
					_unref_function = ccode.get_string ("unref_function");
				}
				if (_unref_function == null) {
					_unref_function = get_default_unref_function ();
				}
				unref_function_set = true;
			}
			return _unref_function;
		}
	}

	public string ref_sink_function {
		get {
			if (_ref_sink_function == null) {
				if (ccode != null) {
					_ref_sink_function = ccode.get_string ("ref_sink_function");
				}
				if (_ref_sink_function == null) {
					_ref_sink_function = get_default_ref_sink_function ();
				}
			}
			return _ref_sink_function;
		}
	}

	public string copy_function {
		get {
			if (!copy_function_set) {
				if (ccode != null) {
					_copy_function = ccode.get_string ("copy_function");
				}
				if (_copy_function == null && sym is Struct) {
216
					_copy_function = "%scopy".printf (lower_case_prefix);
Luca Bruno's avatar
Luca Bruno committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230
				}
				copy_function_set = true;
			}
			return _copy_function;
		}
	}

	public string destroy_function {
		get {
			if (!destroy_function_set) {
				if (ccode != null) {
					_destroy_function = ccode.get_string ("destroy_function");
				}
				if (_destroy_function == null && sym is Struct) {
231
					_destroy_function = "%sdestroy".printf (lower_case_prefix);
Luca Bruno's avatar
Luca Bruno committed
232 233 234 235 236 237 238
				}
				destroy_function_set = true;
			}
			return _destroy_function;
		}
	}

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
	public string dup_function {
		get {
			if (!dup_function_set) {
				if (ccode != null) {
					_dup_function = ccode.get_string ("dup_function");
				}
				if (_dup_function == null && !sym.external_package && sym is Struct) {
					_dup_function = "%sdup".printf (lower_case_prefix);
				}
				dup_function_set = true;
			}
			return _dup_function;
		}
	}

Luca Bruno's avatar
Luca Bruno committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
	public string free_function {
		get {
			if (!free_function_set) {
				if (ccode != null) {
					_free_function = ccode.get_string ("free_function");
				}
				if (_free_function == null) {
					_free_function = get_default_free_function ();
				}
				free_function_set = true;
			}
			return _free_function;
		}
	}

269 270 271 272 273 274
	public bool free_function_address_of {
		get {
			if (_free_function_address_of == null) {
				if (ccode != null && ccode.has_argument ("free_function_address_of")) {
					_free_function_address_of = ccode.get_bool ("free_function_address_of");
				} else {
275
					unowned Class cl = (Class) sym;
276
					if (cl.base_class != null) {
277
						_free_function_address_of = get_ccode_free_function_address_of (cl.base_class);
278 279 280 281 282 283 284 285 286
					} else {
						_free_function_address_of = false;
					}
				}
			}
			return _free_function_address_of;
		}
	}

287 288 289 290 291 292 293
	public string ctype {
		get {
			if (!ctype_set) {
				if (ccode != null) {
					_ctype = ccode.get_string ("type");
					if (_ctype == null) {
						_ctype = ccode.get_string ("ctype");
294 295 296
						if (_ctype != null) {
							Report.deprecated (node.source_reference, "[CCode (ctype = \"...\")] is deprecated, use [CCode (type = \"...\")] instead.");
						}
297 298 299 300 301 302 303 304
					}
				}
				ctype_set = true;
			}
			return _ctype;
		}
	}

Luca Bruno's avatar
Luca Bruno committed
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
	public string type_id {
		get {
			if (_type_id == null) {
				if (ccode != null) {
					_type_id = ccode.get_string ("type_id");
				}
				if (_type_id == null) {
					_type_id = get_default_type_id ();
				}
			}
			return _type_id;
		}
	}

	public string marshaller_type_name {
		get {
			if (_marshaller_type_name == null) {
				if (ccode != null) {
					_marshaller_type_name = ccode.get_string ("marshaller_type_name");
				}
				if (_marshaller_type_name == null) {
					_marshaller_type_name = get_default_marshaller_type_name ();
				}
			}
			return _marshaller_type_name;
		}
	}

	public string get_value_function {
		get {
			if (_get_value_function == null) {
				if (ccode != null) {
					_get_value_function = ccode.get_string ("get_value_function");
				}
				if (_get_value_function == null) {
					_get_value_function = get_default_get_value_function ();
				}
			}
			return _get_value_function;
		}
	}

	public string set_value_function {
		get {
			if (_set_value_function == null) {
				if (ccode != null) {
					_set_value_function = ccode.get_string ("set_value_function");
				}
				if (_set_value_function == null) {
					_set_value_function = get_default_set_value_function ();
				}
			}
			return _set_value_function;
		}
	}

	public string take_value_function {
		get {
			if (_take_value_function == null) {
				if (ccode != null) {
					_take_value_function = ccode.get_string ("take_value_function");
				}
				if (_take_value_function == null) {
					_take_value_function = get_default_take_value_function ();
				}
			}
			return _take_value_function;
		}
	}

	public string param_spec_function {
		get {
			if (_param_spec_function == null) {
				if (ccode != null) {
					_param_spec_function = ccode.get_string ("param_spec_function");
				}
				if (_param_spec_function == null) {
					_param_spec_function = get_default_param_spec_function ();
				}
			}
			return _param_spec_function;
		}
	}

	public string default_value {
		get {
			if (_default_value == null) {
				if (ccode != null) {
					_default_value = ccode.get_string ("default_value");
				}
				if (_default_value == null) {
					_default_value = get_default_default_value ();
				}
			}
			return _default_value;
		}
	}

403 404 405 406 407 408 409 410 411 412 413 414 415 416
	public string default_value_on_error {
		get {
			if (_default_value_on_error == null) {
				if (ccode != null) {
					_default_value_on_error = ccode.get_string ("default_value_on_error");
				}
				if (_default_value_on_error == null) {
					_default_value_on_error = default_value;
				}
			}
			return _default_value_on_error;
		}
	}

Luca Bruno's avatar
Luca Bruno committed
417 418 419 420 421 422
	public double pos {
		get {
			if (_pos == null) {
				if (ccode != null && ccode.has_argument ("pos")) {
					_pos = ccode.get_double ("pos");
				} else {
423 424 425 426
					unowned Parameter param = (Parameter) node;
					unowned Callable? callable = param.parent_symbol as Callable;
					if (callable != null) {
						_pos = callable.get_parameters ().index_of (param) + 1.0;
Luca Bruno's avatar
Luca Bruno committed
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
					} else {
						_pos = 0.0;
					}
				}
			}
			return _pos;
		}
	}

	public string real_name {
		get {
			if (_real_name == null) {
				if (ccode != null && sym is CreationMethod) {
					_real_name = ccode.get_string ("construct_function");
				}
				if (_real_name == null) {
					_real_name = get_default_real_name ();
				}
			}
			return _real_name;
		}
	}

	public string vfunc_name {
		get {
			if (_vfunc_name == null) {
				if (ccode != null) {
					_vfunc_name = ccode.get_string ("vfunc_name");
				}
				if (_vfunc_name == null) {
457
					unowned Method? m = node as Method;
458
					if (m != null && m.signal_reference != null) {
459
						_vfunc_name = get_ccode_lower_case_name (m.signal_reference);
460 461 462
					} else {
						_vfunc_name = sym.name;
					}
Luca Bruno's avatar
Luca Bruno committed
463 464 465 466 467 468 469 470 471 472 473
				}
			}
			return _vfunc_name;
		}
	}

	public string finish_name {
		get {
			if (_finish_name == null) {
				if (ccode != null) {
					_finish_name = ccode.get_string ("finish_name");
474 475
					if (_finish_name == null) {
						_finish_name = ccode.get_string ("finish_function");
476 477 478
						if (_finish_name != null) {
							Report.deprecated (node.source_reference, "[CCode (finish_function = \"...\")] is deprecated, use [CCode (finish_name = \"...\")] instead.");
						}
479
					}
480
				}
Luca Bruno's avatar
Luca Bruno committed
481 482 483 484 485 486 487 488 489 490 491
				if (_finish_name == null) {
					_finish_name = get_finish_name_for_basename (name);
				}
			}
			return _finish_name;
		}
	}

	public string finish_vfunc_name {
		get {
			if (_finish_vfunc_name == null) {
492 493 494 495 496 497
				if (ccode != null) {
					_finish_vfunc_name = ccode.get_string ("finish_vfunc_name");
				}
				if (_finish_vfunc_name == null) {
					_finish_vfunc_name = get_finish_name_for_basename (vfunc_name);
				}
Luca Bruno's avatar
Luca Bruno committed
498 499 500 501 502 503 504 505 506 507 508 509 510 511
			}
			return _finish_vfunc_name;
		}
	}

	public string finish_real_name {
		get {
			if (_finish_real_name == null) {
				_finish_real_name = get_finish_name_for_basename (real_name);
			}
			return _finish_real_name;
		}
	}

512 513 514
	public bool finish_instance {
		get {
			if (_finish_instance == null) {
515
				unowned Method? m = node as Method;
516 517 518 519 520 521 522 523 524 525 526
				bool is_creation_method = m is CreationMethod;
				if (ccode == null || m == null || m.is_abstract || m.is_virtual) {
					_finish_instance = !is_creation_method;
				} else {
					_finish_instance = ccode.get_bool ("finish_instance", !is_creation_method);
				}
			}
			return _finish_instance;
		}
	}

527 528 529 530 531 532 533
	public string delegate_target_name {
		get {
			if (_delegate_target_name == null) {
				if (ccode != null) {
					_delegate_target_name = ccode.get_string ("delegate_target_cname");
				}
				if (_delegate_target_name == null) {
534
					_delegate_target_name = "%s_target".printf (name);
535 536 537 538 539 540
				}
			}
			return _delegate_target_name;
		}
	}

541 542 543 544
	public bool array_length {
		get {
			if (_array_length == null) {
				if (node.get_attribute ("NoArrayLength") != null) {
545
					Report.deprecated (node.source_reference, "[NoArrayLength] is deprecated, use [CCode (array_length = false)] instead.");
546 547 548 549 550 551 552 553 554 555 556 557 558 559
					_array_length = false;
				} else if (ccode != null && ccode.has_argument ("array_length")) {
					_array_length = ccode.get_bool ("array_length");
				} else {
					_array_length = get_default_array_length ();
				}
			}
			return _array_length;
		}
	}

	public bool array_null_terminated {
		get {
			if (_array_null_terminated == null) {
560 561 562 563
				// If arrays claim to have an array-length and also are null-terminated then rely on the given length
				if (ccode != null && ccode.has_argument ("array_length") && ccode.get_bool ("array_length")) {
					_array_null_terminated = false;
				} else if (ccode != null && ccode.has_argument ("array_null_terminated")) {
564 565 566 567 568 569 570 571 572
					_array_null_terminated = ccode.get_bool ("array_null_terminated");
				} else {
					_array_null_terminated = get_default_array_null_terminated ();
				}
			}
			return _array_null_terminated;
		}
	}

Luca Bruno's avatar
Luca Bruno committed
573 574 575 576 577 578 579 580 581
	public string? array_length_type { get; private set; }
	public string? array_length_name { get; private set; }
	public string? array_length_expr { get; private set; }
	public bool delegate_target { get; private set; }
	public string sentinel { get; private set; }

	private string _name;
	private string _const_name;
	private string _type_name;
582
	private string _feature_test_macros;
Luca Bruno's avatar
Luca Bruno committed
583 584 585 586 587 588 589 590 591 592 593 594 595 596
	private string _header_filenames;
	private string _prefix;
	private string _lower_case_prefix;
	private string _lower_case_suffix;
	private string? _ref_function;
	private bool ref_function_set;
	private bool? _ref_function_void;
	private string? _unref_function;
	private bool unref_function_set;
	private string _ref_sink_function;
	private string? _copy_function;
	private bool copy_function_set;
	private string? _destroy_function;
	private bool destroy_function_set;
597 598
	private string? _dup_function;
	private bool dup_function_set;
Luca Bruno's avatar
Luca Bruno committed
599 600
	private string? _free_function;
	private bool free_function_set;
601
	private bool? _free_function_address_of;
Luca Bruno's avatar
Luca Bruno committed
602 603 604 605 606 607 608
	private string _type_id;
	private string _marshaller_type_name;
	private string _get_value_function;
	private string _set_value_function;
	private string _take_value_function;
	private string _param_spec_function;
	private string _default_value;
609
	private string _default_value_on_error;
Luca Bruno's avatar
Luca Bruno committed
610 611 612 613 614
	private double? _pos;
	private string _vfunc_name;
	private string _finish_name;
	private string _finish_vfunc_name;
	private string _finish_real_name;
615
	private bool? _finish_instance;
Luca Bruno's avatar
Luca Bruno committed
616
	private string _real_name;
617
	private string _delegate_target_name;
618 619
	private string _ctype;
	private bool ctype_set = false;
620 621
	private bool? _array_length;
	private bool? _array_null_terminated;
Luca Bruno's avatar
Luca Bruno committed
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648

	private static int dynamic_method_id;

	public CCodeAttribute (CodeNode node) {
		this.node = node;
		this.sym = node as Symbol;

		delegate_target = true;
		ccode = node.get_attribute ("CCode");
		if (ccode != null) {
			array_length_type = ccode.get_string ("array_length_type");
			array_length_name = ccode.get_string ("array_length_cname");
			array_length_expr = ccode.get_string ("array_length_cexpr");
			if (ccode.has_argument ("pos")) {
				_pos = ccode.get_double ("pos");
			}
			delegate_target = ccode.get_bool ("delegate_target", true);
			sentinel = ccode.get_string ("sentinel");
		}
		if (sentinel == null) {
			sentinel = "NULL";
		}
	}

	private string get_default_name () {
		if (sym != null) {
			if (sym is Constant && !(sym is EnumValue)) {
649 650 651 652
				if (sym.parent_symbol is Block) {
					// local constant
					return sym.name;
				}
653
				return "%s%s".printf (get_ccode_lower_case_prefix (sym.parent_symbol).ascii_up (), sym.name);
Luca Bruno's avatar
Luca Bruno committed
654
			} else if (sym is Field) {
655
				var cname = sym.name;
Luca Bruno's avatar
Luca Bruno committed
656
				if (((Field) sym).binding == MemberBinding.STATIC) {
657
					cname = "%s%s".printf (get_ccode_lower_case_prefix (sym.parent_symbol), sym.name);
658 659 660 661
				}
				if (cname[0].isdigit ()) {
					Report.error (node.source_reference, "Field name starts with a digit. Use the `cname' attribute to provide a valid C name if intended");
					return "";
Luca Bruno's avatar
Luca Bruno committed
662
				}
663
				return cname;
Luca Bruno's avatar
Luca Bruno committed
664
			} else if (sym is CreationMethod) {
665
				unowned CreationMethod m = (CreationMethod) sym;
Luca Bruno's avatar
Luca Bruno committed
666 667 668 669 670 671 672
				string infix;
				if (m.parent_symbol is Struct) {
					infix = "init";
				} else {
					infix = "new";
				}
				if (m.name == ".new") {
673
					return "%s%s".printf (get_ccode_lower_case_prefix (m.parent_symbol), infix);
Luca Bruno's avatar
Luca Bruno committed
674
				} else {
675
					return "%s%s_%s".printf (get_ccode_lower_case_prefix (m.parent_symbol), infix, m.name);
Luca Bruno's avatar
Luca Bruno committed
676 677 678 679
				}
			} else if (sym is DynamicMethod) {
				return "_dynamic_%s%d".printf (sym.name, dynamic_method_id++);
			} else if (sym is Method) {
680
				unowned Method m = (Method) sym;
Luca Bruno's avatar
Luca Bruno committed
681
				if (m.is_async_callback) {
682
					return "%s_co".printf (get_ccode_real_name ((Method) m.parent_symbol));
Luca Bruno's avatar
Luca Bruno committed
683
				}
684
				if (m.signal_reference != null) {
685
					return "%s%s".printf (get_ccode_lower_case_prefix (m.parent_symbol), get_ccode_lower_case_name (m.signal_reference));
686
				}
Luca Bruno's avatar
Luca Bruno committed
687 688 689 690
				if (sym.name == "main" && sym.parent_symbol.name == null) {
					// avoid conflict with generated main function
					return "_vala_main";
				} else if (sym.name.has_prefix ("_")) {
691
					return "_%s%s".printf (get_ccode_lower_case_prefix (sym.parent_symbol), sym.name.substring (1));
Luca Bruno's avatar
Luca Bruno committed
692
				} else {
693
					return "%s%s".printf (get_ccode_lower_case_prefix (sym.parent_symbol), sym.name);
Luca Bruno's avatar
Luca Bruno committed
694
				}
695 696
			} else if (sym is Property) {
				return sym.name.replace ("_", "-");
Luca Bruno's avatar
Luca Bruno committed
697
			} else if (sym is PropertyAccessor) {
698
				unowned PropertyAccessor acc = (PropertyAccessor) sym;
Luca Bruno's avatar
Luca Bruno committed
699 700 701
				var t = (TypeSymbol) acc.prop.parent_symbol;

				if (acc.readable) {
702
					return "%sget_%s".printf (get_ccode_lower_case_prefix (t), acc.prop.name);
Luca Bruno's avatar
Luca Bruno committed
703
				} else {
704
					return "%sset_%s".printf (get_ccode_lower_case_prefix (t), acc.prop.name);
Luca Bruno's avatar
Luca Bruno committed
705 706
				}
			} else if (sym is Signal) {
707
				return Symbol.camel_case_to_lower_case (sym.name).replace ("_", "-");;
708 709
			} else if (sym is LocalVariable || sym is Parameter) {
				return sym.name;
Luca Bruno's avatar
Luca Bruno committed
710
			} else {
711
				return "%s%s".printf (get_ccode_prefix (sym.parent_symbol), sym.name);
Luca Bruno's avatar
Luca Bruno committed
712 713 714 715 716 717
			}
		} else if (node is ObjectType) {
			var type = (ObjectType) node;

			string cname;
			if (!type.value_owned) {
718
				cname = get_ccode_const_name (type.type_symbol);
Luca Bruno's avatar
Luca Bruno committed
719
			} else {
720
				cname = get_ccode_name (type.type_symbol);
Luca Bruno's avatar
Luca Bruno committed
721 722 723 724
			}
			return "%s*".printf (cname);
		} else if (node is ArrayType) {
			var type = (ArrayType) node;
725
			var cname = get_ccode_name (type.element_type);
Luca Bruno's avatar
Luca Bruno committed
726 727 728
			if (type.inline_allocated) {
				return cname;
			} else {
Jürg Billeter's avatar
Jürg Billeter committed
729
				return "%s*".printf (cname);
Luca Bruno's avatar
Luca Bruno committed
730 731 732
			}
		} else if (node is DelegateType) {
			var type = (DelegateType) node;
733
			return get_ccode_name (type.delegate_symbol);
Luca Bruno's avatar
Luca Bruno committed
734 735 736
		} else if (node is ErrorType) {
			return "GError*";
		} else if (node is GenericType) {
Jürg Billeter's avatar
Jürg Billeter committed
737 738
			var type = (GenericType) node;
			if (type.value_owned) {
739 740 741 742 743
				if (CodeContext.get ().profile == Profile.GOBJECT) {
					return "gpointer";
				} else {
					return "void *";
				}
Luca Bruno's avatar
Luca Bruno committed
744
			} else {
745 746 747 748 749
				if (CodeContext.get ().profile == Profile.GOBJECT) {
					return "gconstpointer";
				} else {
					return "const void *";
				}
Luca Bruno's avatar
Luca Bruno committed
750 751
			}
		} else if (node is MethodType) {
752 753 754 755 756
			if (CodeContext.get ().profile == Profile.GOBJECT) {
				return "gpointer";
			} else {
				return "void *";
			}
Luca Bruno's avatar
Luca Bruno committed
757
		} else if (node is NullType) {
758 759 760 761 762
			if (CodeContext.get ().profile == Profile.GOBJECT) {
				return "gpointer";
			} else {
				return "void *";
			}
Luca Bruno's avatar
Luca Bruno committed
763 764 765
		} else if (node is PointerType) {
			var type = (PointerType) node;
			if (type.base_type.data_type != null && type.base_type.data_type.is_reference_type ()) {
766
				return get_ccode_name (type.base_type);
Luca Bruno's avatar
Luca Bruno committed
767
			} else {
768
				return "%s*".printf (get_ccode_name (type.base_type));
Luca Bruno's avatar
Luca Bruno committed
769 770 771 772 773
			}
		} else if (node is VoidType) {
			return "void";
		} else if (node is ClassType) {
			var type = (ClassType) node;
774
			return "%sClass*".printf (get_ccode_name (type.class_symbol));
Luca Bruno's avatar
Luca Bruno committed
775 776
		} else if (node is InterfaceType) {
			var type = (InterfaceType) node;
777
			return "%s*".printf (get_ccode_type_name (type.interface_symbol));
Luca Bruno's avatar
Luca Bruno committed
778 779
		} else if (node is ValueType) {
			var type = (ValueType) node;
780
			var cname = get_ccode_name (type.type_symbol);
Luca Bruno's avatar
Luca Bruno committed
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
			if (type.nullable) {
				return "%s*".printf (cname);
			} else {
				return cname;
			}
		} else if (node is CType) {
			return ((CType) node).ctype_name;
		} else {
			Report.error (node.source_reference, "Unresolved type reference");
			return "";
		}
	}

	private string get_default_header_filenames () {
		if (sym is DynamicProperty || sym is DynamicMethod) {
			return "";
797 798
		}
		if (sym.parent_symbol != null) {
799
			var parent_headers = get_ccode_header_filenames (sym.parent_symbol);
800 801 802 803 804
			if (parent_headers.length > 0) {
				return parent_headers;
			}
		}
		if (sym.source_reference != null && !sym.external_package) {
Luca Bruno's avatar
Luca Bruno committed
805 806 807 808 809 810 811 812 813 814
			// don't add default include directives for VAPI files
			return sym.source_reference.file.get_cinclude_filename ();
		}
		return "";
	}

	private string get_default_prefix () {
		if (sym is ObjectTypeSymbol) {
			return name;
		} else if (sym is Enum || sym is ErrorDomain) {
815
			return "%s_".printf (get_ccode_upper_case_name (sym));
Luca Bruno's avatar
Luca Bruno committed
816 817 818 819
		} else if (sym is Namespace) {
			if (sym.name != null) {
				var parent_prefix = "";
				if (sym.parent_symbol != null) {
820
					parent_prefix = get_ccode_prefix (sym.parent_symbol);
Luca Bruno's avatar
Luca Bruno committed
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
				}
				return "%s%s".printf (parent_prefix, sym.name);
			} else {
				return "";
			}
		} else if (sym.name != null) {
			return sym.name;
		}
		return "";
	}

	private string get_default_lower_case_prefix () {
		if (sym is Namespace) {
			if (sym.name == null) {
				return "";
			} else {
837
				return "%s%s_".printf (get_ccode_lower_case_prefix (sym.parent_symbol), Symbol.camel_case_to_lower_case (sym.name));
Luca Bruno's avatar
Luca Bruno committed
838 839 840 841 842
			}
		} else if (sym is Method) {
			// for lambda expressions
			return "";
		} else {
843
			return "%s_".printf (get_ccode_lower_case_name (sym));
Luca Bruno's avatar
Luca Bruno committed
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
		}
	}

	private string get_default_lower_case_suffix () {
		if (sym is ObjectTypeSymbol) {
			var csuffix = Symbol.camel_case_to_lower_case (sym.name);

			// remove underscores in some cases to avoid conflicts of type macros
			if (csuffix.has_prefix ("type_")) {
				csuffix = "type" + csuffix.substring ("type_".length);
			} else if (csuffix.has_prefix ("is_")) {
				csuffix = "is" + csuffix.substring ("is_".length);
			}
			if (csuffix.has_suffix ("_class")) {
				csuffix = csuffix.substring (0, csuffix.length - "_class".length) + "class";
			}
			return csuffix;
861
		} else if (sym is Signal) {
862
			return get_ccode_attribute (sym).name.replace ("-", "_");
Luca Bruno's avatar
Luca Bruno committed
863 864 865 866 867 868 869 870
		} else if (sym.name != null) {
			return Symbol.camel_case_to_lower_case (sym.name);
		}
		return "";
	}

	private string? get_default_ref_function () {
		if (sym is Class) {
871
			unowned Class cl = (Class) sym;
Luca Bruno's avatar
Luca Bruno committed
872
			if (cl.is_fundamental ()) {
873
				return "%sref".printf (lower_case_prefix);
Luca Bruno's avatar
Luca Bruno committed
874
			} else if (cl.base_class != null) {
875
				return get_ccode_ref_function (cl.base_class);
Luca Bruno's avatar
Luca Bruno committed
876 877 878
			}
		} else if (sym is Interface) {
			foreach (var prereq in ((Interface) sym).get_prerequisites ()) {
879
				var ref_func = get_ccode_ref_function ((ObjectTypeSymbol) prereq.data_type);
Luca Bruno's avatar
Luca Bruno committed
880 881 882 883 884 885 886 887 888 889
				if (ref_func != null) {
					return ref_func;
				}
			}
		}
		return null;
	}

	private string? get_default_unref_function () {
		if (sym is Class) {
890
			unowned Class cl = (Class) sym;
Luca Bruno's avatar
Luca Bruno committed
891
			if (cl.is_fundamental ()) {
892
				return "%sunref".printf (lower_case_prefix);
Luca Bruno's avatar
Luca Bruno committed
893
			} else if (cl.base_class != null) {
894
				return get_ccode_unref_function (cl.base_class);
Luca Bruno's avatar
Luca Bruno committed
895 896 897
			}
		} else if (sym is Interface) {
			foreach (var prereq in ((Interface) sym).get_prerequisites ()) {
898
				string unref_func = get_ccode_unref_function ((ObjectTypeSymbol) prereq.data_type);
Luca Bruno's avatar
Luca Bruno committed
899 900 901 902 903 904 905 906 907 908
				if (unref_func != null) {
					return unref_func;
				}
			}
		}
		return null;
	}

	private string get_default_ref_sink_function () {
		if (sym is Class) {
909 910 911 912
			unowned Class? base_class = ((Class) sym).base_class;
			if (base_class != null) {
				return get_ccode_ref_sink_function (base_class);
			}
Luca Bruno's avatar
Luca Bruno committed
913 914
		} else if (sym is Interface) {
			foreach (var prereq in ((Interface) sym).get_prerequisites ()) {
915
				string ref_sink_func = get_ccode_ref_sink_function ((ObjectTypeSymbol) prereq.data_type);
Luca Bruno's avatar
Luca Bruno committed
916 917 918 919 920 921 922 923 924 925
				if (ref_sink_func != "") {
					return ref_sink_func;
				}
			}
		}
		return "";
	}

	private string? get_default_free_function () {
		if (sym is Class) {
926
			unowned Class cl = (Class) sym;
Luca Bruno's avatar
Luca Bruno committed
927
			if (cl.base_class != null) {
928
				return get_ccode_free_function (cl.base_class);
Luca Bruno's avatar
Luca Bruno committed
929
			}
930
			return "%sfree".printf (lower_case_prefix);
Luca Bruno's avatar
Luca Bruno committed
931 932
		} else if (sym is Struct) {
			if (!sym.external_package) {
933
				return "%sfree".printf (lower_case_prefix);
Luca Bruno's avatar
Luca Bruno committed
934 935 936 937 938 939 940 941
			}
		}
		return null;
	}

	private string get_default_type_id () {
		if (sym != null) {
			if (sym is Class && !((Class) sym).is_compact || sym is Interface) {
942
				return get_ccode_upper_case_name (sym, "TYPE_");
943
			} else if (sym is ErrorType) {
Luca Bruno's avatar
Luca Bruno committed
944 945
				return "G_TYPE_ERROR";
			} else if (sym is Struct) {
946
				unowned Struct st = (Struct) sym;
947
				if (!get_ccode_has_type_id (st)) {
948
					unowned Struct? base_struct = st.base_struct;
Luca Bruno's avatar
Luca Bruno committed
949
					if (base_struct != null) {
950
						return get_ccode_type_id (base_struct);
Luca Bruno's avatar
Luca Bruno committed
951 952 953 954 955
					}
					if (!st.is_simple_type ()) {
						return "G_TYPE_POINTER";
					}
				} else {
956
					return get_ccode_upper_case_name (st, "TYPE_");
Luca Bruno's avatar
Luca Bruno committed
957 958
				}
			} else if (sym is Enum) {
959
				unowned Enum en = (Enum) sym;
960 961
				if (get_ccode_has_type_id (en)) {
					return get_ccode_upper_case_name (en, "TYPE_");
Luca Bruno's avatar
Luca Bruno committed
962 963 964 965 966 967 968 969 970 971 972
				} else {
					return en.is_flags ? "G_TYPE_UINT" : "G_TYPE_INT";
				}
			} else {
				return "G_TYPE_POINTER";
			}
		} else if (node is ArrayType && ((ArrayType) node).element_type.data_type.get_full_name () == "string") {
			return "G_TYPE_STRV";
		} else if (node is PointerType || node is DelegateType) {
			return "G_TYPE_POINTER";
		} else if (node is ErrorType) {
973
			return "G_TYPE_ERROR";
Luca Bruno's avatar
Luca Bruno committed
974 975 976 977 978
		} else if (node is VoidType) {
			return "G_TYPE_NONE";
		} else {
			var type = (DataType) node;
			if (type.data_type != null) {
979
				return get_ccode_type_id (type.data_type);
Luca Bruno's avatar
Luca Bruno committed
980 981 982 983 984 985 986 987
			}
		}
		return "";
	}

	private string get_default_marshaller_type_name () {
		if (sym != null) {
			if (sym is Class) {
988
				unowned Class cl = (Class) sym;
Luca Bruno's avatar
Luca Bruno committed
989
				if (cl.base_class != null) {
990
					return get_ccode_marshaller_type_name (cl.base_class);
Luca Bruno's avatar
Luca Bruno committed
991
				} else if (!cl.is_compact) {
992
					return get_ccode_upper_case_name (cl);
Luca Bruno's avatar
Luca Bruno committed
993 994 995 996 997 998
				} else if (type_id == "G_TYPE_POINTER") {
					return "POINTER";
				} else {
					return "BOXED";
				}
			} else if (sym is Enum) {
999
				unowned Enum en = (Enum) sym;
1000
				if (get_ccode_has_type_id (en)) {
Luca Bruno's avatar
Luca Bruno committed
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
					if (en.is_flags) {
						return "FLAGS";
					} else {
						return "ENUM";
					}
				} else {
					if (en.is_flags) {
						return "UINT";
					} else {
						return "INT";
					}
				}
			} else if (sym is Interface) {
				foreach (var prereq in ((Interface) sym).get_prerequisites ()) {
1015
					var type_name = get_ccode_marshaller_type_name (prereq.data_type);
Luca Bruno's avatar
Luca Bruno committed
1016 1017 1018 1019 1020 1021
					if (type_name != "") {
						return type_name;
					}
				}
				return "POINTER";
			} else if (sym is Struct) {
1022 1023
				unowned Struct st = (Struct) sym;
				unowned Struct? base_st = st.base_struct;
1024
				while (base_st != null) {
1025 1026
					if (get_ccode_has_type_id (base_st)) {
						return get_ccode_marshaller_type_name (base_st);
1027 1028 1029
					} else {
						base_st = base_st.base_struct;
					}
Luca Bruno's avatar
Luca Bruno committed
1030 1031
				}
				if (st.is_simple_type ()) {
1032
					Report.error (st.source_reference, "The type `%s' doesn't declare a marshaller type name".printf (st.get_full_name ()));
1033
				} else if (get_ccode_has_type_id (st)) {
Luca Bruno's avatar
Luca Bruno committed
1034 1035 1036 1037 1038
					return "BOXED";
				} else {
					return "POINTER";
				}
			} else if (sym is Parameter) {
1039
				unowned Parameter param = (Parameter) sym;
Luca Bruno's avatar
Luca Bruno committed
1040 1041 1042
				if (param.direction != ParameterDirection.IN) {
					return "POINTER";
				} else {
1043
					return get_ccode_marshaller_type_name (param.variable_type);
Luca Bruno's avatar
Luca Bruno committed
1044 1045 1046 1047
				}
			} else {
				return "POINTER";
			}
1048 1049
		} else if (node is ValueType && ((ValueType) node).nullable) {
			return "POINTER";
1050
		} else if (node is PointerType || node is GenericType) {
Luca Bruno's avatar
Luca Bruno committed
1051 1052 1053 1054
			return "POINTER";
		} else if (node is ErrorType) {
			return "POINTER";
		} else if (node is ArrayType) {
1055 1056 1057
			unowned ArrayType array_type = (ArrayType) node;
			if (array_type.element_type.data_type.get_full_name () == "string") {
				return "BOXED,%s".printf (get_ccode_marshaller_type_name (array_type.length_type.data_type));
Luca Bruno's avatar
Luca Bruno committed
1058
			} else {
1059
				var ret = "POINTER";
1060 1061 1062
				var length_marshaller_type_name = get_ccode_marshaller_type_name (array_type.length_type.data_type);
				for (var i = 0; i < array_type.rank; i++) {
					ret = "%s,%s".printf (ret, length_marshaller_type_name);
1063 1064
				}
				return ret;
Luca Bruno's avatar
Luca Bruno committed
1065
			}
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
		} else if (node is DelegateType) {
			unowned DelegateType delegate_type = (DelegateType) node;
			var ret = "POINTER";
			if (delegate_type.delegate_symbol.has_target) {
				ret = "%s,POINTER".printf (ret);
				if (delegate_type.is_disposable ()) {
					ret = "%s,POINTER".printf (ret);
				}
			}
			return ret;
Luca Bruno's avatar
Luca Bruno committed
1076 1077 1078
		} else if (node is VoidType) {
			return "VOID";
		} else {
1079
			return get_ccode_marshaller_type_name (((DataType) node).data_type);
Luca Bruno's avatar
Luca Bruno committed
1080 1081 1082 1083 1084 1085
		}
		return "";
	}

	private string get_default_get_value_function () {
		if (sym is Class) {
1086
			unowned Class cl = (Class) sym;
Luca Bruno's avatar
Luca Bruno committed
1087
			if (cl.is_fundamental ()) {
1088
				return get_ccode_lower_case_name (cl, "value_get_");
Luca Bruno's avatar
Luca Bruno committed
1089
			} else if (cl.base_class != null) {
1090
				return get_ccode_get_value_function (cl.base_class);
Luca Bruno's avatar
Luca Bruno committed
1091 1092 1093 1094 1095 1096
			} else if (type_id == "G_TYPE_POINTER") {
				return "g_value_get_pointer";
			} else {
				return "g_value_get_boxed";
			}
		} else if (sym is Enum) {
1097
			unowned Enum en = (Enum) sym;
1098
			if (get_ccode_has_type_id (en)) {
Luca Bruno's avatar
Luca Bruno committed
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
				if (en.is_flags) {
					return "g_value_get_flags";
				} else {
					return "g_value_get_enum";
				}
			} else {
				if (en.is_flags) {
					return "g_value_get_uint";
				} else {
					return "g_value_get_int";
				}
			}
		} else if (sym is Interface) {
			foreach (var prereq in ((Interface) sym).get_prerequisites ()) {
1113
				var type_name = get_ccode_get_value_function (prereq.data_type);
Luca Bruno's avatar
Luca Bruno committed
1114 1115 1116 1117 1118 1119
				if (type_name != "") {
					return type_name;
				}
			}
			return "g_value_get_pointer";
		} else if (sym is Struct) {
1120 1121
			unowned Struct st = (Struct) sym;
			unowned Struct? base_st = st.base_struct;
1122
			while (base_st != null) {
1123 1124
				if (get_ccode_has_type_id (base_st)) {
					return get_ccode_get_value_function (base_st);
1125 1126 1127
				} else {
					base_st = base_st.base_struct;
				}
Luca Bruno's avatar
Luca Bruno committed
1128 1129
			}
			if (st.is_simple_type ()) {
1130
				Report.error (st.source_reference, "The type `%s' doesn't declare a GValue get function".printf (st.get_full_name ()));
1131
			} else if (get_ccode_has_type_id (st)) {
Luca Bruno's avatar
Luca Bruno committed
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
				return "g_value_get_boxed";
			} else {
				return "g_value_get_pointer";
			}
		} else {
			return "g_value_get_pointer";
		}
		return "";
	}

	private string get_default_set_value_function () {
		if (sym is Class) {
1144
			unowned Class cl = (Class) sym;
Luca Bruno's avatar
Luca Bruno committed
1145
			if (cl.is_fundamental ()) {
1146
				return get_ccode_lower_case_name (cl, "value_set_");
Luca Bruno's avatar
Luca Bruno committed
1147
			} else if (cl.base_class != null) {
1148
				return get_ccode_set_value_function (cl.base_class);
Luca Bruno's avatar
Luca Bruno committed
1149 1150 1151 1152 1153 1154
			} else if (type_id == "G_TYPE_POINTER") {
				return "g_value_set_pointer";
			} else {
				return "g_value_set_boxed";
			}
		} else if (sym is Enum) {
1155
			unowned Enum en = (Enum) sym;
1156
			if (get_ccode_has_type_id (en)) {
Luca Bruno's avatar
Luca Bruno committed
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
				if (en.is_flags) {
					return "g_value_set_flags";
				} else {
					return "g_value_set_enum";
				}
			} else {
				if (en.is_flags) {
					return "g_value_set_uint";
				} else {
					return "g_value_set_int";
				}
			}
		} else if (sym is Interface) {
			foreach (var prereq in ((Interface) sym).get_prerequisites ()) {
1171
				var type_name = get_ccode_set_value_function (prereq.data_type);
Luca Bruno's avatar
Luca Bruno committed
1172 1173 1174 1175 1176 1177
				if (type_name != "") {
					return type_name;
				}
			}
			return "g_value_set_pointer";
		} else if (sym is Struct) {
1178 1179
			unowned Struct st = (Struct) sym;
			unowned Struct? base_st = st.base_struct;
1180
			while (base_st != null) {
1181 1182
				if (get_ccode_has_type_id (base_st)) {
					return get_ccode_set_value_function (base_st);
1183 1184 1185
				} else {
					base_st = base_st.base_struct;
				}
Luca Bruno's avatar
Luca Bruno committed
1186 1187
			}
			if (st.is_simple_type ()) {
1188
				Report.error (st.source_reference, "The type `%s' doesn't declare a GValue set function".printf (st.get_full_name ()));
1189
			} else if (get_ccode_has_type_id (st)) {
Luca Bruno's avatar
Luca Bruno committed
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
				return "g_value_set_boxed";
			} else {
				return "g_value_set_pointer";
			}
		} else {
			return "g_value_set_pointer";
		}
		return "";
	}

	private string get_default_take_value_function () {
		if (sym is Class) {
1202
			unowned Class cl = (Class) sym;
Luca Bruno's avatar
Luca Bruno committed
1203
			if (cl.is_fundamental ()) {
1204
				return get_ccode_lower_case_name (cl, "value_take_");
Luca Bruno's avatar
Luca Bruno committed
1205
			} else if (cl.base_class != null) {
1206
				return get_ccode_take_value_function (cl.base_class);
Luca Bruno's avatar
Luca Bruno committed
1207 1208 1209 1210 1211 1212
			} else if (type_id == "G_TYPE_POINTER") {
				return "g_value_set_pointer";
			} else {
				return "g_value_take_boxed";
			}
		} else if (sym is Enum) {
1213
			unowned Enum en = (Enum) sym;
1214
			if (get_ccode_has_type_id (en)) {
Luca Bruno's avatar
Luca Bruno committed
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
				if (en.is_flags) {
					return "g_value_take_flags";
				} else {
					return "g_value_take_enum";
				}
			} else {
				if (en.is_flags) {
					return "g_value_take_uint";
				} else {
					return "g_value_take_int";
				}
			}
		} else if (sym is Interface) {
			foreach (var prereq in ((Interface) sym).get_prerequisites ()) {
1229
				var func = get_ccode_take_value_function (prereq.data_type);
Luca Bruno's avatar
Luca Bruno committed
1230 1231 1232 1233 1234 1235
				if (func != "") {
					return func;
				}
			}
			return "g_value_set_pointer";
		} else if (sym is Struct) {
1236 1237
			unowned Struct st = (Struct) sym;
			unowned Struct? base_st = st.base_struct;
1238
			while (base_st != null) {
1239 1240
				if (get_ccode_has_type_id (base_st)) {
					return get_ccode_take_value_function (base_st);
1241 1242 1243
				} else {
					base_st = base_st.base_struct;
				}
Luca Bruno's avatar
Luca Bruno committed
1244 1245
			}
			if (st.is_simple_type ()) {
1246
				Report.error (st.source_reference, "The type `%s' doesn't declare a GValue take function".printf (st.get_full_name ()));
1247
			} else if (get_ccode_has_type_id (st)) {
Luca Bruno's avatar
Luca Bruno committed
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
				return "g_value_take_boxed";
			} else {
				return "g_value_set_pointer";
			}
		} else {
			return "g_value_set_pointer";
		}
		return "";
	}

	private string get_default_param_spec_function () {
1259 1260
		if (node is Symbol) {
			if (sym is Class) {
1261
				unowned Class cl = (Class) sym;
1262
				if (cl.is_fundamental ()) {
1263
					return get_ccode_lower_case_name (cl, "param_spec_");
1264
				} else if (cl.base_class != null) {
1265
					return get_ccode_param_spec_function (cl.base_class);
1266 1267 1268 1269 1270 1271 1272
				} else if (type_id == "G_TYPE_POINTER") {
					return "g_param_spec_pointer";
				} else {
					return "g_param_spec_boxed";
				}
			} else if (sym is Interface) {
				foreach (var prereq in ((Interface) sym).get_prerequisites ()) {
1273
					var func = get_ccode_param_spec_function (prereq.data_type);
1274 1275 1276 1277
					if (func != "") {
						return func;
					}
				}
Luca Bruno's avatar
Luca Bruno committed
1278
				return "g_param_spec_pointer";
1279
			} else if (sym is Enum) {
1280
				unowned Enum e = (Enum) sym;
1281
				if (get_ccode_has_type_id (e)) {
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
					if (e.is_flags) {
						return "g_param_spec_flags";
					} else {
						return "g_param_spec_enum";
					}
				} else {
					if (e.is_flags) {
						return "g_param_spec_uint";
					} else {
						return "g_param_spec_int";
					}
				}
			} else if (sym is Struct) {
1295
				var type_id = get_ccode_type_id (sym);
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
				if (type_id == "G_TYPE_INT") {
					return "g_param_spec_int";
				} else if (type_id == "G_TYPE_UINT") {
					return "g_param_spec_uint";
				} else if (type_id == "G_TYPE_INT64") {
					return "g_param_spec_int64";
				} else if (type_id == "G_TYPE_UINT64") {
					return "g_param_spec_uint64";
				} else if (type_id == "G_TYPE_LONG") {
					return "g_param_spec_long";
				} else if (type_id == "G_TYPE_ULONG") {
					return "g_param_spec_ulong";
				} else if (type_id == "G_TYPE_BOOLEAN") {
					return "g_param_spec_boolean";
				} else if (type_id == "G_TYPE_CHAR") {
					return "g_param_spec_char";
				} else if (type_id == "G_TYPE_UCHAR") {
					return "g_param_spec_uchar";
				}else if (type_id == "G_TYPE_FLOAT") {
					return "g_param_spec_float";
				} else if (type_id == "G_TYPE_DOUBLE") {
					return "g_param_spec_double";
				} else if (type_id == "G_TYPE_GTYPE") {
					return "g_param_spec_gtype";
				} else {
					return "g_param_spec_boxed";
Luca Bruno's avatar
Luca Bruno committed
1322 1323
				}
			}
1324 1325 1326
		} else if (node is ArrayType && ((ArrayType)node).element_type.data_type == CodeContext.get().analyzer.string_type.data_type) {
			return "g_param_spec_boxed";
		} else if (node is DataType && ((DataType) node).data_type != null) {
1327
			return get_ccode_param_spec_function (((DataType) node).data_type);
Luca Bruno's avatar
Luca Bruno committed
1328
		}
1329 1330

		return "g_param_spec_pointer";
Luca Bruno's avatar
Luca Bruno committed
1331 1332 1333 1334 1335 1336
	}

	private string get_default_default_value () {
		if (sym is Enum) {
			return "0";
		} else if (sym is Struct) {
1337 1338
			unowned Struct st = (Struct) sym;
			unowned Struct? base_st = st.base_struct;
Luca Bruno's avatar
Luca Bruno committed
1339
			if (base_st != null) {
1340
				return get_ccode_default_value (base_st);
Luca Bruno's avatar
Luca Bruno committed
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
			}
		}
		return "";
	}

	private string get_finish_name_for_basename (string basename) {
		string result = basename;
		if (result.has_suffix ("_async")) {
			result = result.substring (0, result.length - "_async".length);
		}
1351
		return "%s_finish".printf (result);
Luca Bruno's avatar
Luca Bruno committed
1352 1353 1354
	}

	private string get_default_real_name () {
1355
		if (sym is CreationMethod) {
1356 1357
			unowned CreationMethod m = (CreationMethod) sym;
			unowned Class? parent = m.parent_symbol as Class;
Luca Bruno's avatar
Luca Bruno committed
1358 1359 1360 1361 1362 1363 1364 1365

			if (parent == null || parent.is_compact) {
				return name;
			}

			string infix = "construct";

			if (m.name == ".new") {
1366
				return "%s%s".printf (get_ccode_lower_case_prefix (parent), infix);
Luca Bruno's avatar
Luca Bruno committed
1367
			} else {
1368
				return "%s%s_%s".printf (get_ccode_lower_case_prefix (parent), infix, m.name);
Luca Bruno's avatar
Luca Bruno committed
1369
			}
1370
		} else if (sym is Method) {
1371
			unowned Method m = (Method) sym;
Luca Bruno's avatar
Luca Bruno committed
1372
			if (m.base_method != null || m.base_interface_method != null) {
1373 1374
				string m_name;
				if (m.signal_reference != null) {
1375
					m_name = get_ccode_lower_case_name (m.signal_reference);
1376 1377 1378
				} else {
					m_name = m.name;
				}
1379
				if (m.base_interface_type != null) {
1380 1381
					return "%sreal_%s%s".printf (get_ccode_lower_case_prefix (m.parent_symbol),
												 get_ccode_lower_case_prefix (m.base_interface_type.data_type),
1382
												 m_name);
1383
				} else {
1384
					return "%sreal_%s".printf (get_ccode_lower_case_prefix (m.parent_symbol), m_name);
1385
				}
Luca Bruno's avatar
Luca Bruno committed
1386 1387 1388
			} else {
				return name;
			}
1389
		} else if (sym is PropertyAccessor) {
1390 1391
			unowned PropertyAccessor acc = (PropertyAccessor) sym;
			unowned Property prop = (Property) acc.prop;
1392 1393
			if (prop.base_property != null || prop.base_interface_property != null) {
				if (acc.readable) {
1394
					return "%sreal_get_%s".printf (get_ccode_lower_case_prefix (prop.parent_symbol), prop.name);
1395
				} else {
1396
					return "%sreal_set_%s".printf (get_ccode_lower_case_prefix (prop.parent_symbol), prop.name);
1397 1398 1399 1400
				}
			} else {
				return name;
			}
Luca Bruno's avatar
Luca Bruno committed
1401
		}
1402
		assert_not_reached ();
Luca Bruno's avatar
Luca Bruno committed
1403 1404 1405 1406
	}

	private string get_default_const_name () {
		if (node is DataType) {
1407
			unowned DataType type = (DataType) node;
Luca Bruno's avatar
Luca Bruno committed
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
			string ptr;
			TypeSymbol t;
			// FIXME: workaround to make constant arrays possible
			if (type is ArrayType) {
				t = ((ArrayType) type).element_type.data_type;
			} else {
				t = type.data_type;
			}
			if (!t.is_reference_type ()) {
				ptr = "";
			} else {
				ptr = "*";
			}

1422
			return "const %s%s".printf (get_ccode_name (t), ptr);
Luca Bruno's avatar
Luca Bruno committed
1423 1424 1425 1426 1427 1428 1429 1430
		} else {
			if (node is Class && ((Class) node).is_immutable) {
				return "const %s".printf (name);
			} else {
				return name;
			}
		}
	}
1431 1432 1433

	private bool get_default_array_length () {
		if (node is Parameter) {
1434
			unowned Parameter param = (Parameter) node;
1435
			if (param.base_parameter != null) {
1436
				return get_ccode_array_length (param.base_parameter);
1437
			}
1438
		} else if (node is Method) {
1439
			unowned Method method = (Method) node;
1440
			if (method.base_method != null && method.base_method != method) {
1441
				return get_ccode_array_length (method.base_method);
1442
			} else if (method.base_interface_method != null && method.base_interface_method != method) {
1443
				return get_ccode_array_length (method.base_interface_method);
1444
			}
1445 1446 1447 1448 1449 1450
		}
		return true;
	}

	private bool get_default_array_null_terminated () {
		if (node is Parameter) {
1451
			unowned Parameter param = (Parameter) node;
1452
			if (param.base_parameter != null) {
1453
				return get_ccode_array_null_terminated (param.base_parameter);
1454
			}
1455
		} else if (node is Method) {
1456
			unowned Method method = (Method) node;
1457
			if (method.base_method != null && method.base_method != method) {
1458
				return get_ccode_array_null_terminated (method.base_method);
1459
			} else if (method.base_interface_method != null && method.base_interface_method != method) {
1460
				return get_ccode_array_null_terminated (method.base_interface_method);
1461
			}
1462 1463 1464
		}
		return false;
	}
Luca Bruno's avatar
Luca Bruno committed
1465
}