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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 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 170 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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 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 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
|
/* valaccodeclassbinding.vala
*
* Copyright (C) 2006-2008 Jürg Billeter, Raffaele Sandrini
*
* 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:
* Jürg Billeter <j@bitron.ch>
* Raffaele Sandrini <raffaele@sandrini.ch>
*/
using GLib;
public class Vala.CCodeClassBinding : CCodeObjectTypeSymbolBinding {
public Class cl { get; set; }
public CCodeClassBinding (CCodeGenerator codegen, Class cl) {
this.cl = cl;
this.codegen = codegen;
}
public override void emit () {
var old_symbol = codegen.current_symbol;
var old_type_symbol = codegen.current_type_symbol;
var old_class = codegen.current_class;
var old_instance_struct = codegen.instance_struct;
var old_type_struct = codegen.type_struct;
var old_instance_priv_struct = codegen.instance_priv_struct;
var old_prop_enum = codegen.prop_enum;
var old_class_init_fragment = codegen.class_init_fragment;
var old_instance_init_fragment = codegen.instance_init_fragment;
var old_instance_dispose_fragment = codegen.instance_dispose_fragment;
codegen.current_symbol = cl;
codegen.current_type_symbol = cl;
codegen.current_class = cl;
bool is_gtypeinstance = !cl.is_compact && !cl.is_static;
bool is_gobject = cl.is_subtype_of (codegen.gobject_type);
bool is_fundamental = is_gtypeinstance && cl.base_class == null;
if (cl.get_cname().len () < 3) {
cl.error = true;
Report.error (cl.source_reference, "Class name `%s' is too short".printf (cl.get_cname ()));
return;
}
if (!cl.is_static) {
codegen.instance_struct = new CCodeStruct ("_%s".printf (cl.get_cname ()));
codegen.type_struct = new CCodeStruct ("_%sClass".printf (cl.get_cname ()));
codegen.instance_priv_struct = new CCodeStruct ("_%sPrivate".printf (cl.get_cname ()));
codegen.prop_enum = new CCodeEnum ();
codegen.prop_enum.add_value (new CCodeEnumValue ("%s_DUMMY_PROPERTY".printf (cl.get_upper_case_cname (null))));
codegen.class_init_fragment = new CCodeFragment ();
codegen.instance_init_fragment = new CCodeFragment ();
codegen.instance_dispose_fragment = new CCodeFragment ();
}
CCodeFragment decl_frag;
CCodeFragment def_frag;
if (cl.access != SymbolAccessibility.PRIVATE) {
decl_frag = codegen.header_type_declaration;
def_frag = codegen.header_type_definition;
} else {
decl_frag = codegen.source_type_declaration;
def_frag = codegen.source_type_definition;
}
if (is_gtypeinstance) {
decl_frag.append (new CCodeNewline ());
var macro = "(%s_get_type ())".printf (cl.get_lower_case_cname (null));
decl_frag.append (new CCodeMacroReplacement (cl.get_type_id (), macro));
macro = "(G_TYPE_CHECK_INSTANCE_CAST ((obj), %s, %s))".printf (cl.get_type_id (), cl.get_cname ());
decl_frag.append (new CCodeMacroReplacement ("%s(obj)".printf (cl.get_upper_case_cname (null)), macro));
macro = "(G_TYPE_CHECK_CLASS_CAST ((klass), %s, %sClass))".printf (cl.get_type_id (), cl.get_cname ());
decl_frag.append (new CCodeMacroReplacement ("%s_CLASS(klass)".printf (cl.get_upper_case_cname (null)), macro));
macro = "(G_TYPE_CHECK_INSTANCE_TYPE ((obj), %s))".printf (cl.get_type_id ());
decl_frag.append (new CCodeMacroReplacement ("%s(obj)".printf (cl.get_upper_case_cname ("IS_")), macro));
macro = "(G_TYPE_CHECK_CLASS_TYPE ((klass), %s))".printf (cl.get_type_id ());
decl_frag.append (new CCodeMacroReplacement ("%s_CLASS(klass)".printf (cl.get_upper_case_cname ("IS_")), macro));
macro = "(G_TYPE_INSTANCE_GET_CLASS ((obj), %s, %sClass))".printf (cl.get_type_id (), cl.get_cname ());
decl_frag.append (new CCodeMacroReplacement ("%s_GET_CLASS(obj)".printf (cl.get_upper_case_cname (null)), macro));
decl_frag.append (new CCodeNewline ());
}
if (!cl.is_static && cl.source_reference.file.cycle == null) {
decl_frag.append (new CCodeTypeDefinition ("struct %s".printf (codegen.instance_struct.name), new CCodeVariableDeclarator (cl.get_cname ())));
}
if (cl.base_class != null) {
codegen.instance_struct.add_field (cl.base_class.get_cname (), "parent_instance");
} else if (is_fundamental) {
codegen.instance_struct.add_field ("GTypeInstance", "parent_instance");
codegen.instance_struct.add_field ("volatile int", "ref_count");
}
if (cl.is_compact && cl.base_class == null && cl.get_fields ().size == 0) {
// add dummy member, C doesn't allow empty structs
codegen.instance_struct.add_field ("int", "dummy");
}
if (is_gtypeinstance) {
if (cl.source_reference.file.cycle == null) {
decl_frag.append (new CCodeTypeDefinition ("struct %s".printf (codegen.type_struct.name), new CCodeVariableDeclarator ("%sClass".printf (cl.get_cname ()))));
}
decl_frag.append (new CCodeTypeDefinition ("struct %s".printf (codegen.instance_priv_struct.name), new CCodeVariableDeclarator ("%sPrivate".printf (cl.get_cname ()))));
codegen.instance_struct.add_field ("%sPrivate *".printf (cl.get_cname ()), "priv");
if (is_fundamental) {
codegen.type_struct.add_field ("GTypeClass", "parent_class");
codegen.type_struct.add_field ("void", "(*finalize) (%s *self)".printf (cl.get_cname ()));
} else {
codegen.type_struct.add_field ("%sClass".printf (cl.base_class.get_cname ()), "parent_class");
}
}
if (!cl.is_static) {
if (cl.source_reference.comment != null) {
def_frag.append (new CCodeComment (cl.source_reference.comment));
}
def_frag.append (codegen.instance_struct);
}
if (is_gtypeinstance) {
def_frag.append (codegen.type_struct);
/* only add the *Private struct if it is not empty, i.e. we actually have private data */
if (cl.has_private_fields || cl.get_type_parameters ().size > 0) {
codegen.source_type_member_declaration.append (codegen.instance_priv_struct);
var macro = "(G_TYPE_INSTANCE_GET_PRIVATE ((o), %s, %sPrivate))".printf (cl.get_type_id (), cl.get_cname ());
codegen.source_type_member_declaration.append (new CCodeMacroReplacement ("%s_GET_PRIVATE(o)".printf (cl.get_upper_case_cname (null)), macro));
}
codegen.source_type_member_declaration.append (codegen.prop_enum);
}
cl.accept_children (codegen);
if (is_gtypeinstance) {
if (is_fundamental) {
var ref_count = new CCodeAssignment (new CCodeMemberAccess.pointer (new CCodeIdentifier ("self"), "ref_count"), new CCodeConstant ("1"));
codegen.instance_init_fragment.append (new CCodeExpressionStatement (ref_count));
} else if (is_gobject) {
if (class_has_readable_properties (cl) || cl.get_type_parameters ().size > 0) {
add_get_property_function (cl);
}
if (class_has_writable_properties (cl) || cl.get_type_parameters ().size > 0) {
add_set_property_function (cl);
}
}
add_class_init_function (cl);
foreach (DataType base_type in cl.get_base_types ()) {
if (base_type.data_type is Interface) {
add_interface_init_function (cl, (Interface) base_type.data_type);
}
}
add_instance_init_function (cl);
if (is_gobject) {
if (cl.get_fields ().size > 0 || cl.destructor != null) {
add_dispose_function (cl);
}
}
var type_fun = new ClassRegisterFunction (cl);
type_fun.init_from_type (codegen.in_plugin);
if (cl.access != SymbolAccessibility.PRIVATE) {
codegen.header_type_member_declaration.append (type_fun.get_declaration ());
} else {
codegen.source_type_member_declaration.append (type_fun.get_declaration ());
}
codegen.source_type_member_definition.append (type_fun.get_definition ());
if (codegen.in_plugin) {
// FIXME resolve potential dependency issues, i.e. base types have to be registered before derived types
var register_call = new CCodeFunctionCall (new CCodeIdentifier ("%s_register_type".printf (cl.get_lower_case_cname (null))));
register_call.add_argument (new CCodeIdentifier (codegen.module_init_param_name));
codegen.module_init_fragment.append (new CCodeExpressionStatement (register_call));
}
if (is_fundamental) {
var ref_fun = new CCodeFunction (cl.get_lower_case_cprefix () + "ref", "gpointer");
var unref_fun = new CCodeFunction (cl.get_lower_case_cprefix () + "unref", "void");
if (cl.access == SymbolAccessibility.PRIVATE) {
ref_fun.modifiers = CCodeModifiers.STATIC;
unref_fun.modifiers = CCodeModifiers.STATIC;
}
ref_fun.add_parameter (new CCodeFormalParameter ("instance", "gpointer"));
unref_fun.add_parameter (new CCodeFormalParameter ("instance", "gpointer"));
if (cl.access != SymbolAccessibility.PRIVATE) {
codegen.header_type_member_declaration.append (ref_fun.copy ());
codegen.header_type_member_declaration.append (unref_fun.copy ());
} else {
codegen.source_type_member_declaration.append (ref_fun.copy ());
codegen.source_type_member_declaration.append (unref_fun.copy ());
}
var ref_block = new CCodeBlock ();
var unref_block = new CCodeBlock ();
var cdecl = new CCodeDeclaration (cl.get_cname () + "*");
cdecl.add_declarator (new CCodeVariableDeclarator.with_initializer ("self", new CCodeIdentifier ("instance")));
ref_block.add_statement (cdecl);
unref_block.add_statement (cdecl);
var ref_count = new CCodeMemberAccess.pointer (new CCodeIdentifier ("self"), "ref_count");
var ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_atomic_int_inc"));
ccall.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, ref_count));
ref_block.add_statement (new CCodeExpressionStatement (ccall));
ref_block.add_statement (new CCodeReturnStatement (new CCodeIdentifier ("instance")));
var destroy_block = new CCodeBlock ();
var get_class = new CCodeFunctionCall (new CCodeIdentifier ("%s_GET_CLASS".printf (cl.get_upper_case_cname (null))));
get_class.add_argument (new CCodeIdentifier ("self"));
var finalize = new CCodeMemberAccess.pointer (get_class, "finalize");
var finalize_call = new CCodeFunctionCall (finalize);
finalize_call.add_argument (new CCodeIdentifier ("self"));
//destroy_block.add_statement (new CCodeExpressionStatement (finalize_call));
var free = new CCodeFunctionCall (new CCodeIdentifier ("g_type_free_instance"));
free.add_argument (new CCodeCastExpression (new CCodeIdentifier ("self"), "GTypeInstance *"));
destroy_block.add_statement (new CCodeExpressionStatement (free));
ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_atomic_int_dec_and_test"));
ccall.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, ref_count));
unref_block.add_statement (new CCodeIfStatement (ccall, destroy_block));
ref_fun.block = ref_block;
unref_fun.block = unref_block;
codegen.source_type_member_definition.append (ref_fun);
codegen.source_type_member_definition.append (unref_fun);
}
} else if (!cl.is_static) {
var function = new CCodeFunction (cl.get_lower_case_cprefix () + "free", "void");
if (cl.access == SymbolAccessibility.PRIVATE) {
function.modifiers = CCodeModifiers.STATIC;
}
function.add_parameter (new CCodeFormalParameter ("self", cl.get_cname () + "*"));
if (cl.access != SymbolAccessibility.PRIVATE) {
codegen.header_type_member_declaration.append (function.copy ());
} else {
codegen.source_type_member_declaration.append (function.copy ());
}
var cblock = new CCodeBlock ();
cblock.add_statement (codegen.instance_dispose_fragment);
if (cl.destructor != null) {
cblock.add_statement (cl.destructor.ccodenode);
}
var ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_slice_free"));
ccall.add_argument (new CCodeIdentifier (cl.get_cname ()));
ccall.add_argument (new CCodeIdentifier ("self"));
cblock.add_statement (new CCodeExpressionStatement (ccall));
function.block = cblock;
codegen.source_type_member_definition.append (function);
}
codegen.current_type_symbol = old_type_symbol;
codegen.current_class = old_class;
codegen.instance_struct = old_instance_struct;
codegen.type_struct = old_type_struct;
codegen.instance_priv_struct = old_instance_priv_struct;
codegen.prop_enum = old_prop_enum;
codegen.class_init_fragment = old_class_init_fragment;
codegen.instance_init_fragment = old_instance_init_fragment;
codegen.instance_dispose_fragment = old_instance_dispose_fragment;
}
private void add_class_init_function (Class cl) {
var class_init = new CCodeFunction ("%s_class_init".printf (cl.get_lower_case_cname (null)), "void");
class_init.add_parameter (new CCodeFormalParameter ("klass", "%sClass *".printf (cl.get_cname ())));
class_init.modifiers = CCodeModifiers.STATIC;
var init_block = new CCodeBlock ();
class_init.block = init_block;
CCodeFunctionCall ccall;
/* save pointer to parent class */
var parent_decl = new CCodeDeclaration ("gpointer");
var parent_var_decl = new CCodeVariableDeclarator ("%s_parent_class".printf (cl.get_lower_case_cname (null)));
parent_var_decl.initializer = new CCodeConstant ("NULL");
parent_decl.add_declarator (parent_var_decl);
parent_decl.modifiers = CCodeModifiers.STATIC;
codegen.source_type_member_declaration.append (parent_decl);
ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_type_class_peek_parent"));
ccall.add_argument (new CCodeIdentifier ("klass"));
var parent_assignment = new CCodeAssignment (new CCodeIdentifier ("%s_parent_class".printf (cl.get_lower_case_cname (null))), ccall);
init_block.add_statement (new CCodeExpressionStatement (parent_assignment));
/* add struct for private fields */
if (cl.has_private_fields || cl.get_type_parameters ().size > 0) {
ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_type_class_add_private"));
ccall.add_argument (new CCodeIdentifier ("klass"));
ccall.add_argument (new CCodeConstant ("sizeof (%sPrivate)".printf (cl.get_cname ())));
init_block.add_statement (new CCodeExpressionStatement (ccall));
}
if (cl.is_subtype_of (codegen.gobject_type)) {
/* set property handlers */
ccall = new CCodeFunctionCall (new CCodeIdentifier ("G_OBJECT_CLASS"));
ccall.add_argument (new CCodeIdentifier ("klass"));
if (class_has_readable_properties (cl) || cl.get_type_parameters ().size > 0) {
init_block.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeMemberAccess.pointer (ccall, "get_property"), new CCodeIdentifier ("%s_get_property".printf (cl.get_lower_case_cname (null))))));
}
if (class_has_writable_properties (cl) || cl.get_type_parameters ().size > 0) {
init_block.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeMemberAccess.pointer (ccall, "set_property"), new CCodeIdentifier ("%s_set_property".printf (cl.get_lower_case_cname (null))))));
}
/* set constructor */
if (cl.constructor != null) {
var ccast = new CCodeFunctionCall (new CCodeIdentifier ("G_OBJECT_CLASS"));
ccast.add_argument (new CCodeIdentifier ("klass"));
init_block.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeMemberAccess.pointer (ccast, "constructor"), new CCodeIdentifier ("%s_constructor".printf (cl.get_lower_case_cname (null))))));
}
/* set dispose function */
if (cl.get_fields ().size > 0 || cl.destructor != null) {
var ccast = new CCodeFunctionCall (new CCodeIdentifier ("G_OBJECT_CLASS"));
ccast.add_argument (new CCodeIdentifier ("klass"));
init_block.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeMemberAccess.pointer (ccast, "dispose"), new CCodeIdentifier ("%s_dispose".printf (cl.get_lower_case_cname (null))))));
}
}
/* connect overridden methods */
foreach (Method m in cl.get_methods ()) {
if (m.base_method == null) {
continue;
}
var base_type = m.base_method.parent_symbol;
var ccast = new CCodeFunctionCall (new CCodeIdentifier ("%s_CLASS".printf (((Class) base_type).get_upper_case_cname (null))));
ccast.add_argument (new CCodeIdentifier ("klass"));
init_block.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeMemberAccess.pointer (ccast, m.base_method.vfunc_name), new CCodeIdentifier (m.get_real_cname ()))));
}
if (cl.is_subtype_of (codegen.gobject_type)) {
/* create type, dup_func, and destroy_func properties for generic types */
foreach (TypeParameter type_param in cl.get_type_parameters ()) {
string func_name, enum_value;
CCodeConstant func_name_constant;
CCodeFunctionCall cinst, cspec;
func_name = "%s_type".printf (type_param.name.down ());
func_name_constant = new CCodeConstant ("\"%s-type\"".printf (type_param.name.down ()));
enum_value = "%s_%s".printf (cl.get_lower_case_cname (null), func_name).up ();
cinst = new CCodeFunctionCall (new CCodeIdentifier ("g_object_class_install_property"));
cinst.add_argument (ccall);
cinst.add_argument (new CCodeConstant (enum_value));
cspec = new CCodeFunctionCall (new CCodeIdentifier ("g_param_spec_gtype"));
cspec.add_argument (func_name_constant);
cspec.add_argument (new CCodeConstant ("\"type\""));
cspec.add_argument (new CCodeConstant ("\"type\""));
cspec.add_argument (new CCodeIdentifier ("G_TYPE_NONE"));
cspec.add_argument (new CCodeConstant ("G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY"));
cinst.add_argument (cspec);
init_block.add_statement (new CCodeExpressionStatement (cinst));
codegen.prop_enum.add_value (new CCodeEnumValue (enum_value));
codegen.instance_priv_struct.add_field ("GType", func_name);
func_name = "%s_dup_func".printf (type_param.name.down ());
func_name_constant = new CCodeConstant ("\"%s-dup-func\"".printf (type_param.name.down ()));
enum_value = "%s_%s".printf (cl.get_lower_case_cname (null), func_name).up ();
cinst = new CCodeFunctionCall (new CCodeIdentifier ("g_object_class_install_property"));
cinst.add_argument (ccall);
cinst.add_argument (new CCodeConstant (enum_value));
cspec = new CCodeFunctionCall (new CCodeIdentifier ("g_param_spec_pointer"));
cspec.add_argument (func_name_constant);
cspec.add_argument (new CCodeConstant ("\"dup func\""));
cspec.add_argument (new CCodeConstant ("\"dup func\""));
cspec.add_argument (new CCodeConstant ("G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY"));
cinst.add_argument (cspec);
init_block.add_statement (new CCodeExpressionStatement (cinst));
codegen.prop_enum.add_value (new CCodeEnumValue (enum_value));
codegen.instance_priv_struct.add_field ("GBoxedCopyFunc", func_name);
func_name = "%s_destroy_func".printf (type_param.name.down ());
func_name_constant = new CCodeConstant ("\"%s-destroy-func\"".printf (type_param.name.down ()));
enum_value = "%s_%s".printf (cl.get_lower_case_cname (null), func_name).up ();
cinst = new CCodeFunctionCall (new CCodeIdentifier ("g_object_class_install_property"));
cinst.add_argument (ccall);
cinst.add_argument (new CCodeConstant (enum_value));
cspec = new CCodeFunctionCall (new CCodeIdentifier ("g_param_spec_pointer"));
cspec.add_argument (func_name_constant);
cspec.add_argument (new CCodeConstant ("\"destroy func\""));
cspec.add_argument (new CCodeConstant ("\"destroy func\""));
cspec.add_argument (new CCodeConstant ("G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY"));
cinst.add_argument (cspec);
init_block.add_statement (new CCodeExpressionStatement (cinst));
codegen.prop_enum.add_value (new CCodeEnumValue (enum_value));
codegen.instance_priv_struct.add_field ("GDestroyNotify", func_name);
}
/* initialize class fields */
var fields = cl.get_fields ();
foreach (Field field in fields) {
if (field.binding != MemberBinding.CLASS || field.initializer == null) {
continue;
}
CCodeExpression left = new CCodeMemberAccess (new CCodeIdentifier ("klass"),
field.get_cname (), true);
CCodeExpression right = (CCodeExpression)field.initializer.ccodenode;
CCodeAssignment assign = new CCodeAssignment (left, right);
init_block.add_statement (new CCodeExpressionStatement (assign));
}
/* create properties */
var props = cl.get_properties ();
foreach (Property prop in props) {
// FIXME: omit real struct types for now since they cannot be expressed as gobject property yet
if (prop.property_type.is_real_struct_type ()) {
continue;
}
if (prop.access == SymbolAccessibility.PRIVATE) {
// don't register private properties
continue;
}
if (prop.overrides || prop.base_interface_property != null) {
var cinst = new CCodeFunctionCall (new CCodeIdentifier ("g_object_class_override_property"));
cinst.add_argument (ccall);
cinst.add_argument (new CCodeConstant (prop.get_upper_case_cname ()));
cinst.add_argument (prop.get_canonical_cconstant ());
init_block.add_statement (new CCodeExpressionStatement (cinst));
} else {
var cinst = new CCodeFunctionCall (new CCodeIdentifier ("g_object_class_install_property"));
cinst.add_argument (ccall);
cinst.add_argument (new CCodeConstant (prop.get_upper_case_cname ()));
cinst.add_argument (get_param_spec (prop));
init_block.add_statement (new CCodeExpressionStatement (cinst));
}
}
/* create signals */
foreach (Signal sig in cl.get_signals ()) {
init_block.add_statement (new CCodeExpressionStatement (get_signal_creation (sig, cl)));
}
} else if (!cl.is_compact) {
/* create type, dup_func, and destroy_func fields for generic types */
foreach (TypeParameter type_param in cl.get_type_parameters ()) {
string func_name;
func_name = "%s_type".printf (type_param.name.down ());
codegen.instance_priv_struct.add_field ("GType", func_name);
func_name = "%s_dup_func".printf (type_param.name.down ());
codegen.instance_priv_struct.add_field ("GBoxedCopyFunc", func_name);
func_name = "%s_destroy_func".printf (type_param.name.down ());
codegen.instance_priv_struct.add_field ("GDestroyNotify", func_name);
}
}
init_block.add_statement (register_dbus_info (cl));
init_block.add_statement (codegen.class_init_fragment);
codegen.source_type_member_definition.append (class_init);
}
private void add_interface_init_function (Class cl, Interface iface) {
var iface_init = new CCodeFunction ("%s_%s_interface_init".printf (cl.get_lower_case_cname (null), iface.get_lower_case_cname (null)), "void");
iface_init.add_parameter (new CCodeFormalParameter ("iface", "%s *".printf (iface.get_type_cname ())));
iface_init.modifiers = CCodeModifiers.STATIC;
var init_block = new CCodeBlock ();
iface_init.block = init_block;
CCodeFunctionCall ccall;
/* save pointer to parent vtable */
string parent_iface_var = "%s_%s_parent_iface".printf (cl.get_lower_case_cname (null), iface.get_lower_case_cname (null));
var parent_decl = new CCodeDeclaration (iface.get_type_cname () + "*");
var parent_var_decl = new CCodeVariableDeclarator (parent_iface_var);
parent_var_decl.initializer = new CCodeConstant ("NULL");
parent_decl.add_declarator (parent_var_decl);
parent_decl.modifiers = CCodeModifiers.STATIC;
codegen.source_type_member_declaration.append (parent_decl);
ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_type_interface_peek_parent"));
ccall.add_argument (new CCodeIdentifier ("iface"));
var parent_assignment = new CCodeAssignment (new CCodeIdentifier (parent_iface_var), ccall);
init_block.add_statement (new CCodeExpressionStatement (parent_assignment));
foreach (Method m in cl.get_methods ()) {
if (m.base_interface_method == null) {
continue;
}
var base_type = m.base_interface_method.parent_symbol;
if (base_type != iface) {
continue;
}
var ciface = new CCodeIdentifier ("iface");
var cname = m.get_real_cname ();
if (m.is_abstract || m.is_virtual) {
// FIXME results in C compiler warning
cname = m.get_cname ();
}
init_block.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeMemberAccess.pointer (ciface, m.base_interface_method.vfunc_name), new CCodeIdentifier (cname))));
}
// connect inherited implementations
foreach (Method m in iface.get_methods ()) {
if (m.is_abstract) {
Method cl_method = null;
var base_class = cl;
while (cl_method == null) {
cl_method = base_class.scope.lookup (m.name) as Method;
base_class = base_class.base_class;
}
if (cl_method.parent_symbol != cl) {
// method inherited from base class
var ciface = new CCodeIdentifier ("iface");
init_block.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeMemberAccess.pointer (ciface, m.vfunc_name), new CCodeIdentifier (cl_method.get_cname ()))));
}
}
}
codegen.source_type_member_definition.append (iface_init);
}
private void add_instance_init_function (Class cl) {
var instance_init = new CCodeFunction ("%s_instance_init".printf (cl.get_lower_case_cname (null)), "void");
instance_init.add_parameter (new CCodeFormalParameter ("self", "%s *".printf (cl.get_cname ())));
instance_init.modifiers = CCodeModifiers.STATIC;
var init_block = new CCodeBlock ();
instance_init.block = init_block;
if (cl.has_private_fields || cl.get_type_parameters ().size > 0) {
var ccall = new CCodeFunctionCall (new CCodeIdentifier ("%s_GET_PRIVATE".printf (cl.get_upper_case_cname (null))));
ccall.add_argument (new CCodeIdentifier ("self"));
init_block.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeMemberAccess.pointer (new CCodeIdentifier ("self"), "priv"), ccall)));
}
init_block.add_statement (codegen.instance_init_fragment);
codegen.source_type_member_definition.append (instance_init);
}
private void add_dispose_function (Class cl) {
var function = new CCodeFunction ("%s_dispose".printf (cl.get_lower_case_cname (null)), "void");
function.modifiers = CCodeModifiers.STATIC;
function.add_parameter (new CCodeFormalParameter ("obj", "GObject *"));
codegen.source_type_member_declaration.append (function.copy ());
var cblock = new CCodeBlock ();
CCodeFunctionCall ccall = new InstanceCast (new CCodeIdentifier ("obj"), cl);
var cdecl = new CCodeDeclaration ("%s *".printf (cl.get_cname ()));
cdecl.add_declarator (new CCodeVariableDeclarator.with_initializer ("self", ccall));
cblock.add_statement (cdecl);
if (cl.destructor != null) {
cblock.add_statement (cl.destructor.ccodenode);
}
cblock.add_statement (codegen.instance_dispose_fragment);
// chain up to dispose function of the base class
var ccast = new CCodeFunctionCall (new CCodeIdentifier ("G_OBJECT_CLASS"));
ccast.add_argument (new CCodeIdentifier ("%s_parent_class".printf (cl.get_lower_case_cname (null))));
ccall = new CCodeFunctionCall (new CCodeMemberAccess.pointer (ccast, "dispose"));
ccall.add_argument (new CCodeIdentifier ("obj"));
cblock.add_statement (new CCodeExpressionStatement (ccall));
function.block = cblock;
codegen.source_type_member_definition.append (function);
}
private bool class_has_readable_properties (Class cl) {
foreach (Property prop in cl.get_properties ()) {
if (prop.get_accessor != null) {
return true;
}
}
return false;
}
private bool class_has_writable_properties (Class cl) {
foreach (Property prop in cl.get_properties ()) {
if (prop.set_accessor != null) {
return true;
}
}
return false;
}
private void add_get_property_function (Class cl) {
var get_prop = new CCodeFunction ("%s_get_property".printf (cl.get_lower_case_cname (null)), "void");
get_prop.modifiers = CCodeModifiers.STATIC;
get_prop.add_parameter (new CCodeFormalParameter ("object", "GObject *"));
get_prop.add_parameter (new CCodeFormalParameter ("property_id", "guint"));
get_prop.add_parameter (new CCodeFormalParameter ("value", "GValue *"));
get_prop.add_parameter (new CCodeFormalParameter ("pspec", "GParamSpec *"));
var block = new CCodeBlock ();
var ccall = new InstanceCast (new CCodeIdentifier ("object"), cl);
var cdecl = new CCodeDeclaration ("%s *".printf (cl.get_cname ()));
cdecl.add_declarator (new CCodeVariableDeclarator.with_initializer ("self", ccall));
block.add_statement (cdecl);
var cswitch = new CCodeSwitchStatement (new CCodeIdentifier ("property_id"));
var props = cl.get_properties ();
foreach (Property prop in props) {
// FIXME: omit real struct types for now since they cannot be expressed as gobject property yet
if (prop.get_accessor == null || prop.is_abstract || prop.property_type.is_real_struct_type ()) {
continue;
}
if (prop.access == SymbolAccessibility.PRIVATE) {
// don't register private properties
continue;
}
bool is_virtual = prop.base_property != null || prop.base_interface_property != null;
string prefix = cl.get_lower_case_cname (null);
if (is_virtual) {
prefix += "_real";
}
var ccase = new CCodeCaseStatement (new CCodeIdentifier (prop.get_upper_case_cname ()));
var ccall = new CCodeFunctionCall (new CCodeIdentifier ("%s_get_%s".printf (prefix, prop.name)));
ccall.add_argument (new CCodeIdentifier ("self"));
var csetcall = new CCodeFunctionCall ();
csetcall.call = get_value_setter_function (prop.property_type);
csetcall.add_argument (new CCodeIdentifier ("value"));
csetcall.add_argument (ccall);
ccase.add_statement (new CCodeExpressionStatement (csetcall));
ccase.add_statement (new CCodeBreakStatement ());
cswitch.add_case (ccase);
}
cswitch.add_default_statement (get_invalid_property_id_warn_statement ());
cswitch.add_default_statement (new CCodeBreakStatement ());
block.add_statement (cswitch);
get_prop.block = block;
codegen.source_type_member_definition.append (get_prop);
}
private void add_set_property_function (Class cl) {
var set_prop = new CCodeFunction ("%s_set_property".printf (cl.get_lower_case_cname (null)), "void");
set_prop.modifiers = CCodeModifiers.STATIC;
set_prop.add_parameter (new CCodeFormalParameter ("object", "GObject *"));
set_prop.add_parameter (new CCodeFormalParameter ("property_id", "guint"));
set_prop.add_parameter (new CCodeFormalParameter ("value", "const GValue *"));
set_prop.add_parameter (new CCodeFormalParameter ("pspec", "GParamSpec *"));
var block = new CCodeBlock ();
var ccall = new InstanceCast (new CCodeIdentifier ("object"), cl);
var cdecl = new CCodeDeclaration ("%s *".printf (cl.get_cname ()));
cdecl.add_declarator (new CCodeVariableDeclarator.with_initializer ("self", ccall));
block.add_statement (cdecl);
var cswitch = new CCodeSwitchStatement (new CCodeIdentifier ("property_id"));
var props = cl.get_properties ();
foreach (Property prop in props) {
// FIXME: omit real struct types for now since they cannot be expressed as gobject property yet
if (prop.set_accessor == null || prop.is_abstract || prop.property_type.is_real_struct_type ()) {
continue;
}
if (prop.access == SymbolAccessibility.PRIVATE) {
// don't register private properties
continue;
}
bool is_virtual = prop.base_property != null || prop.base_interface_property != null;
string prefix = cl.get_lower_case_cname (null);
if (is_virtual) {
prefix += "_real";
}
var ccase = new CCodeCaseStatement (new CCodeIdentifier (prop.get_upper_case_cname ()));
var ccall = new CCodeFunctionCall (new CCodeIdentifier ("%s_set_%s".printf (prefix, prop.name)));
ccall.add_argument (new CCodeIdentifier ("self"));
var cgetcall = new CCodeFunctionCall ();
if (prop.property_type.data_type != null) {
cgetcall.call = new CCodeIdentifier (prop.property_type.data_type.get_get_value_function ());
} else {
cgetcall.call = new CCodeIdentifier ("g_value_get_pointer");
}
cgetcall.add_argument (new CCodeIdentifier ("value"));
ccall.add_argument (cgetcall);
ccase.add_statement (new CCodeExpressionStatement (ccall));
ccase.add_statement (new CCodeBreakStatement ());
cswitch.add_case (ccase);
}
cswitch.add_default_statement (get_invalid_property_id_warn_statement ());
cswitch.add_default_statement (new CCodeBreakStatement ());
block.add_statement (cswitch);
/* type, dup func, and destroy func properties for generic types */
foreach (TypeParameter type_param in cl.get_type_parameters ()) {
string func_name, enum_value;
CCodeCaseStatement ccase;
CCodeMemberAccess cfield;
CCodeFunctionCall cgetcall;
func_name = "%s_type".printf (type_param.name.down ());
enum_value = "%s_%s".printf (cl.get_lower_case_cname (null), func_name).up ();
ccase = new CCodeCaseStatement (new CCodeIdentifier (enum_value));
cfield = new CCodeMemberAccess.pointer (new CCodeMemberAccess.pointer (new CCodeIdentifier ("self"), "priv"), func_name);
cgetcall = new CCodeFunctionCall (new CCodeIdentifier ("g_value_get_gtype"));
cgetcall.add_argument (new CCodeIdentifier ("value"));
ccase.add_statement (new CCodeExpressionStatement (new CCodeAssignment (cfield, cgetcall)));
ccase.add_statement (new CCodeBreakStatement ());
cswitch.add_case (ccase);
func_name = "%s_dup_func".printf (type_param.name.down ());
enum_value = "%s_%s".printf (cl.get_lower_case_cname (null), func_name).up ();
ccase = new CCodeCaseStatement (new CCodeIdentifier (enum_value));
cfield = new CCodeMemberAccess.pointer (new CCodeMemberAccess.pointer (new CCodeIdentifier ("self"), "priv"), func_name);
cgetcall = new CCodeFunctionCall (new CCodeIdentifier ("g_value_get_pointer"));
cgetcall.add_argument (new CCodeIdentifier ("value"));
ccase.add_statement (new CCodeExpressionStatement (new CCodeAssignment (cfield, cgetcall)));
ccase.add_statement (new CCodeBreakStatement ());
cswitch.add_case (ccase);
func_name = "%s_destroy_func".printf (type_param.name.down ());
enum_value = "%s_%s".printf (cl.get_lower_case_cname (null), func_name).up ();
ccase = new CCodeCaseStatement (new CCodeIdentifier (enum_value));
cfield = new CCodeMemberAccess.pointer (new CCodeMemberAccess.pointer (new CCodeIdentifier ("self"), "priv"), func_name);
cgetcall = new CCodeFunctionCall (new CCodeIdentifier ("g_value_get_pointer"));
cgetcall.add_argument (new CCodeIdentifier ("value"));
ccase.add_statement (new CCodeExpressionStatement (new CCodeAssignment (cfield, cgetcall)));
ccase.add_statement (new CCodeBreakStatement ());
cswitch.add_case (ccase);
}
set_prop.block = block;
codegen.source_type_member_definition.append (set_prop);
}
private CCodeStatement get_invalid_property_id_warn_statement () {
// warn on invalid property id
var cwarn = new CCodeFunctionCall (new CCodeIdentifier ("G_OBJECT_WARN_INVALID_PROPERTY_ID"));
cwarn.add_argument (new CCodeIdentifier ("object"));
cwarn.add_argument (new CCodeIdentifier ("property_id"));
cwarn.add_argument (new CCodeIdentifier ("pspec"));
return new CCodeExpressionStatement (cwarn);
}
}
|