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 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 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 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 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
|
/* valagirwriter.vala
*
* Copyright (C) 2008-2011 Jürg Billeter
*
* 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>
*/
using GLib;
/**
* Code visitor generating .gir file for the public interface.
*/
public class Vala.GIRWriter : CodeVisitor {
private CodeContext context;
private string directory;
private string gir_namespace;
private string gir_version;
StringBuilder buffer = new StringBuilder();
FileStream stream;
Vala.HashSet<Namespace> unannotated_namespaces = new Vala.HashSet<Namespace>();
Vala.HashSet<Namespace> our_namespaces = new Vala.HashSet<Namespace>();
Vala.ArrayList<Vala.Symbol> hierarchy = new Vala.ArrayList<Vala.Symbol>();
Vala.ArrayList<Vala.CodeNode> deferred = new Vala.ArrayList<Vala.CodeNode>();
int indent;
private TypeSymbol gobject_type;
private struct GIRNamespace {
public GIRNamespace (string ns, string version) {
this.ns = ns; this.version = version;
}
public string ns;
public string version;
public bool equal (GIRNamespace g) {
return ((ns == g.ns) && (version == g.version));
}
}
private ArrayList<GIRNamespace?> externals = new ArrayList<GIRNamespace?> ((EqualFunc) GIRNamespace.equal);
public void write_includes() {
foreach (var i in externals) {
if (i.ns != this.gir_namespace) {
write_indent_stream ();
stream.printf ("<include name=\"%s\" version=\"%s\"/>\n", i.ns, i.version);
}
}
}
/**
* Writes the public interface of the specified code context into the
* specified file.
*
* @param context a code context
* @param filename a relative or absolute filename
*/
public void write_file (CodeContext context, string directory, string gir_namespace, string gir_version, string package) {
this.context = context;
this.directory = directory;
this.gir_namespace = gir_namespace;
this.gir_version = gir_version;
var root_symbol = context.root;
var glib_ns = root_symbol.scope.lookup ("GLib");
gobject_type = (TypeSymbol) glib_ns.scope.lookup ("Object");
write_package (package);
context.accept (this);
indent--;
buffer.append_printf ("</repository>\n");
string filename = "%s%c%s-%s.gir".printf (directory, Path.DIR_SEPARATOR, gir_namespace, gir_version);
stream = FileStream.open (filename, "w");
if (stream == null) {
Report.error (null, "unable to open `%s' for writing".printf (filename));
return;
}
stream.printf ("<?xml version=\"1.0\"?>\n");
stream.printf ("<repository version=\"1.2\"");
stream.printf (" xmlns=\"http://www.gtk.org/introspection/core/1.0\"");
stream.printf (" xmlns:c=\"http://www.gtk.org/introspection/c/1.0\"");
stream.printf (" xmlns:glib=\"http://www.gtk.org/introspection/glib/1.0\"");
stream.printf (">\n");
indent++;
write_includes();
indent--;
stream.puts (buffer.str);
stream = null;
foreach (var ns in unannotated_namespaces) {
if (!our_namespaces.contains(ns)) {
Report.warning (ns.source_reference, "Namespace %s does not have a GIR namespace and version annotation".printf (ns.name));
}
}
foreach (var ns in our_namespaces) {
ns.source_reference.file.gir_namespace = gir_namespace;
ns.source_reference.file.gir_version = gir_version;
}
}
private void write_package (string package) {
write_indent ();
buffer.append_printf ("<package name=\"%s\"/>\n", package);
}
private void write_c_includes (Namespace ns) {
// Collect C header filenames
Set<string> header_filenames = new HashSet<string> (str_hash, str_equal);
foreach (string c_header_filename in CCodeBaseModule.get_ccode_header_filenames (ns).split (",")) {
header_filenames.add (c_header_filename);
}
foreach (Symbol symbol in ns.scope.get_symbol_table ().get_values ()) {
foreach (string c_header_filename in CCodeBaseModule.get_ccode_header_filenames (symbol).split (",")) {
header_filenames.add (c_header_filename);
}
}
// Generate c:include tags
foreach (string c_header_filename in header_filenames) {
write_c_include (c_header_filename);
}
}
private void write_c_include (string name) {
write_indent ();
buffer.append_printf ("<c:include name=\"%s\"/>\n", name);
}
public override void visit_namespace (Namespace ns) {
if (ns.external_package) {
return;
}
if (ns.name == null) {
// global namespace
hierarchy.insert (0, ns);
ns.accept_children (this);
hierarchy.remove_at (0);
return;
}
if (ns.parent_symbol.name != null) {
ns.accept_children (this);
return;
}
write_c_includes (ns);
write_indent ();
buffer.append_printf ("<namespace name=\"%s\" version=\"%s\"", gir_namespace, gir_version);
string? cprefix = CCodeBaseModule.get_ccode_prefix (ns);
if (cprefix != null) {
buffer.append_printf (" c:prefix=\"%s\"", cprefix);
}
buffer.append_printf (">\n");
indent++;
write_annotations (ns);
hierarchy.insert (0, ns);
ns.accept_children (this);
hierarchy.remove_at (0);
indent--;
write_indent ();
buffer.append_printf ("</namespace>\n");
our_namespaces.add(ns);
visit_deferred ();
}
private void write_symbol_attributes (Symbol symbol) {
if (symbol.deprecated) {
buffer.append_printf (" deprecated=\"%s\"", (symbol.replacement == null) ? "" : "Use %s".printf (symbol.replacement));
if (symbol.deprecated_since != null) {
buffer.append_printf (" deprecated-version=\"%s\"", symbol.deprecated_since);
}
}
}
public override void visit_class (Class cl) {
if (cl.external_package) {
return;
}
if (!check_accessibility (cl)) {
return;
}
if (!(hierarchy[0] is Namespace)) {
deferred.add (cl);
return;
}
if (cl.is_subtype_of (gobject_type)) {
string gtype_struct_name = cl.name + "Class";
write_indent ();
buffer.append_printf ("<class name=\"%s\"", get_gir_name (cl));
write_gtype_attributes (cl);
buffer.append_printf (" glib:type-struct=\"%s\"", gtype_struct_name);
buffer.append_printf (" parent=\"%s\"", gi_type_name (cl.base_class));
if (cl.is_abstract) {
buffer.append_printf (" abstract=\"1\"");
}
write_symbol_attributes (cl);
buffer.append_printf (">\n");
indent++;
// write implemented interfaces
foreach (DataType base_type in cl.get_base_types ()) {
var object_type = (ObjectType) base_type;
if (object_type.type_symbol is Interface) {
write_indent ();
buffer.append_printf ("<implements name=\"%s\"/>\n", gi_type_name (object_type.type_symbol));
}
}
write_annotations (cl);
write_indent ();
buffer.append_printf ("<field name=\"parent_instance\">\n");
indent++;
write_indent ();
buffer.append_printf ("<type name=\"%s\" c:type=\"%s\"/>\n", gi_type_name (cl.base_class), CCodeBaseModule.get_ccode_name (cl.base_class));
indent--;
write_indent ();
buffer.append_printf("</field>\n");
write_indent ();
buffer.append_printf ("<field name=\"priv\">\n");
indent++;
write_indent ();
buffer.append_printf ("<type name=\"%sPrivate\" c:type=\"%sPrivate*\"/>\n", cl.name, CCodeBaseModule.get_ccode_name (cl));
indent--;
write_indent ();
buffer.append_printf("</field>\n");
hierarchy.insert (0, cl);
cl.accept_children (this);
hierarchy.remove_at (0);
indent--;
write_indent ();
buffer.append_printf ("</class>\n");
write_indent ();
buffer.append_printf ("<record name=\"%s\"", gtype_struct_name);
write_ctype_attributes (cl, "Class");
buffer.append_printf (" glib:is-gtype-struct-for=\"%s\"", cl.name);
buffer.append_printf (">\n");
indent++;
write_indent ();
buffer.append_printf ("<field name=\"parent_class\">\n");
indent++;
write_indent ();
buffer.append_printf ("<type name=\"%sClass\" c:type=\"%sClass\"/>\n", gi_type_name (cl.base_class), CCodeBaseModule.get_ccode_name (cl.base_class));
indent--;
write_indent ();
buffer.append_printf ("</field>\n");
foreach (Method m in cl.get_methods ()) {
if (m.is_abstract || m.is_virtual) {
write_indent ();
if (m.coroutine) {
string finish_name = m.name;
if (finish_name.has_suffix ("_async")) {
finish_name = finish_name.substring (0, finish_name.length - "_async".length);
}
finish_name += "_finish";
write_indent ();
buffer.append_printf("<field name=\"%s\">\n", m.name);
indent++;
do_write_signature (m, "callback", true, m.name, CCodeBaseModule.get_ccode_name (m), m.get_async_begin_parameters (), new VoidType (), false);
indent--;
write_indent ();
buffer.append_printf ("</field>\n");
write_indent ();
buffer.append_printf("<field name=\"%s\">\n", finish_name);
indent++;
do_write_signature (m, "callback", true, finish_name, CCodeBaseModule.get_ccode_finish_name (m), m.get_async_end_parameters (), m.return_type, m.tree_can_fail);
indent--;
write_indent ();
buffer.append_printf ("</field>\n");
} else {
write_indent ();
buffer.append_printf("<field name=\"%s\">\n", m.name);
indent++;
do_write_signature (m, "callback", true, m.name, CCodeBaseModule.get_ccode_name (m), m.get_parameters (), m.return_type, m.tree_can_fail);
indent--;
write_indent ();
buffer.append_printf ("</field>\n");
}
}
}
foreach (Signal sig in cl.get_signals ()) {
if (sig.default_handler != null) {
write_indent ();
buffer.append_printf ("<field name=\"%s\">\n", sig.name);
indent++;
write_signature (sig.default_handler, "callback", true);
indent--;
write_indent ();
buffer.append_printf ("</field>\n");
}
}
indent--;
write_indent ();
buffer.append_printf ("</record>\n");
write_indent ();
buffer.append_printf ("<record name=\"%sPrivate\" c:type=\"%sPrivate\" disguised=\"1\"/>\n", cl.name, CCodeBaseModule.get_ccode_name (cl));
} else {
write_indent ();
buffer.append_printf ("<record name=\"%s\"", get_gir_name (cl));
write_symbol_attributes (cl);
buffer.append_printf (">\n");
indent++;
write_annotations (cl);
hierarchy.insert (0, cl);
cl.accept_children (this);
hierarchy.remove_at (0);
indent--;
write_indent ();
buffer.append_printf ("</record>\n");
}
visit_deferred ();
}
public override void visit_struct (Struct st) {
if (st.external_package) {
return;
}
if (!check_accessibility (st)) {
return;
}
if (!(hierarchy[0] is Namespace)) {
deferred.add (st);
return;
}
write_indent ();
buffer.append_printf ("<record name=\"%s\"", get_gir_name (st));
write_symbol_attributes (st);
buffer.append_printf (">\n");
indent++;
write_annotations (st);
hierarchy.insert (0, st);
st.accept_children (this);
hierarchy.remove_at (0);
indent--;
write_indent ();
buffer.append_printf ("</record>\n");
visit_deferred ();
}
public override void visit_interface (Interface iface) {
if (iface.external_package) {
return;
}
if (!check_accessibility (iface)) {
return;
}
if (!(hierarchy[0] is Namespace)) {
deferred.add (iface);
return;
}
string gtype_struct_name = iface.name + "Iface";
write_indent ();
buffer.append_printf ("<interface name=\"%s\"", get_gir_name (iface));
write_gtype_attributes (iface);
buffer.append_printf (" glib:type-struct=\"%s\"", gtype_struct_name);
write_symbol_attributes (iface);
buffer.append_printf (">\n");
indent++;
// write prerequisites
if (iface.get_prerequisites ().size > 0) {
foreach (DataType base_type in iface.get_prerequisites ()) {
write_indent ();
buffer.append_printf ("<prerequisite name=\"%s\"/>\n", gi_type_name (((ObjectType) base_type).type_symbol));
}
}
write_annotations (iface);
hierarchy.insert (0, iface);
iface.accept_children (this);
hierarchy.remove_at (0);
indent--;
write_indent ();
buffer.append_printf ("</interface>\n");
write_indent ();
buffer.append_printf ("<record name=\"%s\"", gtype_struct_name);
write_ctype_attributes (iface, "Iface");
buffer.append_printf (" glib:is-gtype-struct-for=\"%s\"", iface.name);
buffer.append_printf (">\n");
indent++;
write_indent ();
buffer.append_printf ("<field name=\"parent_iface\">\n");
indent++;
write_indent ();
buffer.append_printf ("<type name=\"GObject.TypeInterface\" c:type=\"GTypeInterface\"/>\n");
indent--;
write_indent ();
buffer.append_printf ("</field>\n");
foreach (Method m in iface.get_methods ()) {
if (m.is_abstract || m.is_virtual) {
if (m.coroutine) {
string finish_name = m.name;
if (finish_name.has_suffix ("_async")) {
finish_name = finish_name.substring (0, finish_name.length - "_async".length);
}
finish_name += "_finish";
write_indent ();
buffer.append_printf("<field name=\"%s\">\n", m.name);
indent++;
do_write_signature (m, "callback", true, m.name, CCodeBaseModule.get_ccode_name (m), m.get_async_begin_parameters (), new VoidType (), false);
indent--;
write_indent ();
buffer.append_printf ("</field>\n");
write_indent ();
buffer.append_printf("<field name=\"%s\">\n", finish_name);
indent++;
do_write_signature (m, "callback", true, finish_name, CCodeBaseModule.get_ccode_finish_name (m), m.get_async_end_parameters (), m.return_type, m.tree_can_fail);
indent--;
write_indent ();
buffer.append_printf ("</field>\n");
} else {
write_indent ();
buffer.append_printf("<field name=\"%s\">\n", m.name);
indent++;
do_write_signature (m, "callback", true, m.name, CCodeBaseModule.get_ccode_name (m), m.get_parameters (), m.return_type, m.tree_can_fail);
indent--;
write_indent ();
buffer.append_printf ("</field>\n");
}
}
}
indent--;
write_indent ();
buffer.append_printf ("</record>\n");
visit_deferred ();
}
private void visit_deferred () {
var nodes = this.deferred;
this.deferred = new Vala.ArrayList<Vala.CodeNode>();
foreach (var node in nodes) {
node.accept (this);
}
}
private string? get_gir_name (Symbol symbol) {
string? gir_name = null;
var h0 = hierarchy[0];
for (Symbol? cur_sym = symbol ; cur_sym != null ; cur_sym = cur_sym.parent_symbol) {
if (cur_sym == h0) {
break;
}
var cur_name = cur_sym.get_attribute_string ("GIR", "name");
if (cur_name == null) {
cur_name = cur_sym.name;
}
gir_name = cur_name.concat (gir_name);
}
return gir_name;
}
public override void visit_enum (Enum en) {
if (en.external_package) {
return;
}
if (!check_accessibility (en)) {
return;
}
if (!(hierarchy[0] is Namespace)) {
deferred.add (en);
return;
}
string element_name = (en.is_flags) ? "bitfield" : "enumeration";
write_indent ();
buffer.append_printf ("<%s name=\"%s\"", element_name, get_gir_name (en));
write_gtype_attributes (en);
write_symbol_attributes (en);
buffer.append_printf (">\n");
indent++;
write_annotations (en);
enum_value = 0;
hierarchy.insert (0, en);
en.accept_children (this);
hierarchy.remove_at (0);
indent--;
write_indent ();
buffer.append_printf ("</%s>\n", element_name);
visit_deferred ();
}
private int enum_value;
public override void visit_enum_value (EnumValue ev) {
write_indent ();
var en = (Enum) hierarchy[0];
buffer.append_printf ("<member name=\"%s\" c:identifier=\"%s\"", ev.name.down (), CCodeBaseModule.get_ccode_name (ev));
if (ev.value != null) {
string value = literal_expression_to_value_string (ev.value);
buffer.append_printf (" value=\"%s\"", value);
} else {
if (en.is_flags) {
buffer.append_printf (" value=\"%d\"", 1 << enum_value++);
} else {
buffer.append_printf (" value=\"%d\"", enum_value++);
}
}
write_symbol_attributes (ev);
buffer.append_printf ("/>\n");
}
public override void visit_error_domain (ErrorDomain edomain) {
if (edomain.external_package) {
return;
}
if (!check_accessibility (edomain)) {
return;
}
write_indent ();
buffer.append_printf ("<errordomain name=\"%s\"", edomain.name);
buffer.append_printf (" get-quark=\"%squark\"", CCodeBaseModule.get_ccode_lower_case_prefix (edomain));
buffer.append_printf (" codes=\"%s\"", edomain.name);
write_symbol_attributes (edomain);
buffer.append_printf (">\n");
write_annotations (edomain);
buffer.append_printf ("</errordomain>\n");
write_indent ();
buffer.append_printf ("<enumeration name=\"%s\"", edomain.name);
write_ctype_attributes (edomain);
buffer.append_printf (">\n");
indent++;
enum_value = 0;
hierarchy.insert (0, edomain);
edomain.accept_children (this);
hierarchy.remove_at (0);
indent--;
write_indent ();
buffer.append_printf ("</enumeration>\n");
visit_deferred ();
}
public override void visit_error_code (ErrorCode ecode) {
write_indent ();
buffer.append_printf ("<member name=\"%s\" c:identifier=\"%s\"", ecode.name.down (), CCodeBaseModule.get_ccode_name (ecode));
if (ecode.value != null) {
string value = literal_expression_to_value_string (ecode.value);
buffer.append_printf (" value=\"%s\"", value);
} else {
buffer.append_printf (" value=\"%d\"", enum_value++);
}
write_symbol_attributes (ecode);
buffer.append_printf ("/>\n");
}
public override void visit_constant (Constant c) {
if (c.external_package) {
return;
}
if (!check_accessibility (c)) {
return;
}
//TODO Add better constant evaluation
var initializer = c.value;
string value = literal_expression_to_value_string (initializer);
write_indent ();
buffer.append_printf ("<constant name=\"%s\" c:identifier=\"%s\"", c.name, CCodeBaseModule.get_ccode_name (c));
buffer.append_printf (" value=\"%s\"", value);
write_symbol_attributes (c);
buffer.append_printf (">\n");
indent++;
write_type (initializer.value_type);
indent--;
write_indent ();
buffer.append_printf ("</constant>\n");
}
public override void visit_field (Field f) {
if (f.external_package) {
return;
}
if (!check_accessibility (f)) {
return;
}
write_indent ();
buffer.append_printf ("<field name=\"%s\"", CCodeBaseModule.get_ccode_name (f));
if (f.variable_type.nullable) {
buffer.append_printf (" allow-none=\"1\"");
}
write_symbol_attributes (f);
buffer.append_printf (">\n");
indent++;
write_annotations (f);
write_type (f.variable_type);
indent--;
write_indent ();
buffer.append_printf ("</field>\n");
}
private void write_implicit_params (DataType type, ref int index, bool has_array_length, string name, ParameterDirection direction) {
if (type is ArrayType && has_array_length) {
var int_type = new IntegerType (CodeContext.get ().root.scope.lookup ("int") as Struct);
write_param_or_return (int_type, true, ref index, has_array_length, "%s_length1".printf (name), direction);
} else if (type is DelegateType) {
var data_type = new PointerType (new VoidType ());
write_param_or_return (data_type, true, ref index, false, "%s_target".printf (name), direction);
if (type.value_owned) {
var notify_type = new DelegateType (CodeContext.get ().root.scope.lookup ("GLib").scope.lookup ("DestroyNotify") as Delegate);
write_param_or_return (notify_type, true, ref index, false, "%s_target_destroy_notify".printf (name), direction);
}
}
}
private void write_params_and_return (List<Parameter> params, DataType? return_type, bool return_array_length, bool constructor = false, DataType? instance_type = null, bool user_data = false) {
int last_index = 0;
bool ret_is_struct = return_type != null && return_type.is_real_non_null_struct_type ();
if (params.size != 0 || instance_type != null || (return_type is ArrayType && return_array_length) || (return_type is DelegateType) || ret_is_struct) {
write_indent ();
buffer.append_printf ("<parameters>\n");
indent++;
int index = 0;
if (instance_type != null) {
write_param_or_return (instance_type, true, ref index, false, "self");
}
foreach (Parameter param in params) {
write_param_or_return (param.variable_type, true, ref index, CCodeBaseModule.get_ccode_array_length (param), param.name, param.direction);
write_implicit_params (param.variable_type, ref index, CCodeBaseModule.get_ccode_array_length (param), param.name, param.direction);
}
if (ret_is_struct) {
// struct returns are converted to parameters
write_param_or_return (return_type, true, ref index, false, "result", ParameterDirection.OUT, constructor, true);
} else {
write_implicit_params (return_type, ref index, return_array_length, "result", ParameterDirection.OUT);
}
last_index = index - 1;
if (user_data) {
write_indent ();
buffer.append_printf ("<parameter name=\"user_data\" transfer-ownership=\"none\" closure=\"%d\">\n", index);
indent++;
write_indent ();
buffer.append_printf ("<type name=\"gpointer\" c:type=\"void*\"/>\n");
indent--;
write_indent ();
buffer.append_printf ("</parameter>\n");
}
indent--;
write_indent ();
buffer.append_printf ("</parameters>\n");
}
if (return_type != null && !ret_is_struct) {
write_param_or_return (return_type, false, ref last_index, return_array_length, null, ParameterDirection.IN, constructor);
} else if (ret_is_struct) {
write_param_or_return (new VoidType (), false, ref last_index, false, null, ParameterDirection.IN);
}
}
public override void visit_delegate (Delegate cb) {
if (cb.external_package) {
return;
}
if (!check_accessibility (cb)) {
return;
}
write_indent ();
buffer.append_printf ("<callback name=\"%s\"", cb.name);
buffer.append_printf (" c:type=\"%s\"", CCodeBaseModule.get_ccode_name (cb));
if (cb.tree_can_fail) {
buffer.append_printf (" throws=\"1\"");
}
write_symbol_attributes (cb);
buffer.append_printf (">\n");
indent++;
write_annotations (cb);
write_params_and_return (cb.get_parameters (), cb.return_type, CCodeBaseModule.get_ccode_array_length (cb), false, null, cb.has_target);
indent--;
write_indent ();
buffer.append_printf ("</callback>\n");
}
public override void visit_method (Method m) {
if (m.external_package) {
return;
}
// don't write interface implementation unless it's an abstract or virtual method
if (!check_accessibility (m) || m.overrides || (m.base_interface_method != null && !m.is_abstract && !m.is_virtual)) {
return;
}
// check for unsupported types
if (!check_signature (m)) {
return;
}
string tag_name = "method";
var parent = this.hierarchy.get (0);
if (parent is Enum) {
deferred.add (m);
return;
}
if (parent is Namespace || m.binding == MemberBinding.STATIC || parent != m.parent_symbol) {
tag_name = "function";
}
write_signature (m, tag_name);
if (m.is_abstract || m.is_virtual) {
write_signature (m, "virtual-method", false);
}
}
bool check_type (DataType type) {
// gobject-introspection does not currently support va_list parameters
if (CCodeBaseModule.get_ccode_name (type) == "va_list") {
return false;
}
return true;
}
bool check_signature (Method m) {
if (!check_type (m.return_type)) {
return false;
}
foreach (var param in m.get_parameters ()) {
if (param.variable_type == null || !check_type (param.variable_type)) {
return false;
}
}
return true;
}
private void write_signature (Method m, string tag_name, bool instance = false) {
var parent = this.hierarchy.get (0);
string name;
if (m.parent_symbol != parent) {
instance = false;
name = CCodeBaseModule.get_ccode_name (m);
var parent_prefix = CCodeBaseModule.get_ccode_lower_case_prefix (parent);
if (name.has_prefix (parent_prefix)) {
name = name.substring (parent_prefix.length);
}
} else {
name = m.name;
}
if (m.coroutine) {
string finish_name = name;
if (finish_name.has_suffix ("_async")) {
finish_name = finish_name.substring (0, finish_name.length - "_async".length);
}
finish_name += "_finish";
do_write_signature (m, tag_name, instance, name, CCodeBaseModule.get_ccode_name (m), m.get_async_begin_parameters (), new VoidType (), false);
do_write_signature (m, tag_name, instance, finish_name, CCodeBaseModule.get_ccode_finish_name (m), m.get_async_end_parameters (), m.return_type, m.tree_can_fail);
} else {
do_write_signature (m, tag_name, instance, name, CCodeBaseModule.get_ccode_name (m), m.get_parameters (), m.return_type, m.tree_can_fail);
}
}
private void do_write_signature (Method m, string tag_name, bool instance, string name, string cname, List<Vala.Parameter> params, DataType return_type, bool can_fail) {
write_indent ();
buffer.append_printf ("<%s name=\"%s\"", tag_name, name);
if (tag_name == "virtual-method") {
buffer.append_printf (" invoker=\"%s\"", name);
} else if (tag_name == "callback") {
/* this is only used for vfuncs */
buffer.append_printf (" c:type=\"%s\"", name);
} else {
buffer.append_printf (" c:identifier=\"%s\"", cname);
}
if (can_fail) {
buffer.append_printf (" throws=\"1\"");
}
write_symbol_attributes (m);
buffer.append_printf (">\n");
indent++;
write_annotations (m);
DataType instance_type = null;
if (instance) {
instance_type = CCodeBaseModule.get_data_type_for_symbol ((TypeSymbol) m.parent_symbol);
}
write_params_and_return (params, return_type, CCodeBaseModule.get_ccode_array_length (m), false, instance_type);
indent--;
write_indent ();
buffer.append_printf ("</%s>\n", tag_name);
}
public override void visit_creation_method (CreationMethod m) {
if (m.external_package) {
return;
}
if (!check_accessibility (m)) {
return;
}
if (m.parent_symbol is Class && ((Class) m.parent_symbol).is_abstract) {
return;
}
write_indent ();
bool is_struct = m.parent_symbol is Struct;
// GI doesn't like constructors that return void type
string tag_name = is_struct ? "function" : "constructor";
if (m.parent_symbol is Class && m == ((Class)m.parent_symbol).default_construction_method ||
m.parent_symbol is Struct && m == ((Struct)m.parent_symbol).default_construction_method) {
string m_name = is_struct ? "init" : "new";
buffer.append_printf ("<%s name=\"%s\" c:identifier=\"%s\"", tag_name, m_name, CCodeBaseModule.get_ccode_name (m));
} else {
buffer.append_printf ("<%s name=\"%s\" c:identifier=\"%s\"", tag_name, m.name, CCodeBaseModule.get_ccode_name (m));
}
if (m.tree_can_fail) {
buffer.append_printf (" throws=\"1\"");
}
buffer.append_printf (">\n");
indent++;
write_annotations (m);
var datatype = CCodeBaseModule.get_data_type_for_symbol ((TypeSymbol) m.parent_symbol);
write_params_and_return (m.get_parameters (), datatype, false, true);
indent--;
write_indent ();
buffer.append_printf ("</%s>\n", tag_name);
}
public override void visit_property (Property prop) {
if (!check_accessibility (prop) || prop.overrides || (prop.base_interface_property != null && !prop.is_abstract && !prop.is_virtual)) {
return;
}
write_indent ();
buffer.append_printf ("<property name=\"%s\"", prop.name.replace ("_", "-"));
if (prop.get_accessor == null) {
buffer.append_printf (" readable=\"0\"");
}
if (prop.set_accessor != null) {
buffer.append_printf (" writable=\"1\"");
if (prop.set_accessor.construction) {
if (!prop.set_accessor.writable) {
buffer.append_printf (" construct-only=\"1\"");
} else {
buffer.append_printf (" construct=\"1\"");
}
}
}
write_symbol_attributes (prop);
buffer.append_printf (">\n");
indent++;
write_annotations (prop);
write_type (prop.property_type);
indent--;
write_indent ();
buffer.append_printf ("</property>\n");
}
public override void visit_signal (Signal sig) {
if (!check_accessibility (sig)) {
return;
}
write_indent ();
buffer.append_printf ("<glib:signal name=\"%s\"", CCodeBaseModule.get_ccode_name (sig));
write_symbol_attributes (sig);
buffer.append_printf (">\n");
indent++;
write_annotations (sig);
write_params_and_return (sig.get_parameters (), sig.return_type, false);
indent--;
write_indent ();
buffer.append_printf ("</glib:signal>\n");
}
private void write_indent () {
int i;
for (i = 0; i < indent; i++) {
buffer.append_c ('\t');
}
}
private void write_indent_stream () {
int i;
for (i = 0; i < indent; i++) {
stream.putc ('\t');
}
}
private void write_param_or_return (DataType type, bool is_parameter, ref int index, bool has_array_length, string? name = null, ParameterDirection direction = ParameterDirection.IN, bool constructor = false, bool caller_allocates = false) {
write_indent ();
string tag = is_parameter ? "parameter" : "return-value";
buffer.append_printf ("<%s", tag);
if (name != null) {
buffer.append_printf (" name=\"%s\"", name);
}
if (direction == ParameterDirection.REF) {
buffer.append_printf (" direction=\"inout\"");
} else if (direction == ParameterDirection.OUT) {
buffer.append_printf (" direction=\"out\"");
}
DelegateType delegate_type = type as DelegateType;
if ((type.value_owned && delegate_type == null) || constructor) {
buffer.append_printf (" transfer-ownership=\"full\"");
} else {
buffer.append_printf (" transfer-ownership=\"none\"");
}
if (caller_allocates) {
buffer.append_printf (" caller-allocates=\"1\"");
}
if (type.nullable) {
buffer.append_printf (" allow-none=\"1\"");
}
if (delegate_type != null && delegate_type.delegate_symbol.has_target) {
int closure_index = is_parameter ?
index + 1 : (type.value_owned ? index - 1 : index);
buffer.append_printf (" closure=\"%i\"", closure_index);
if (type.value_owned) {
buffer.append_printf (" destroy=\"%i\"", closure_index + 1);
}
if (delegate_type.is_called_once) {
buffer.append (" scope=\"async\"");
}
}
buffer.append_printf (">\n");
indent++;
int length_param_index = -1;
if (has_array_length) {
length_param_index = is_parameter ? index + 1 : index;
}
write_type (type, length_param_index);
indent--;
write_indent ();
buffer.append_printf ("</%s>\n", tag);
index++;
}
private void write_ctype_attributes (TypeSymbol symbol, string suffix = "") {
buffer.append_printf (" c:type=\"%s%s\"", CCodeBaseModule.get_ccode_name (symbol), suffix);
}
private void write_gtype_attributes (TypeSymbol symbol) {
write_ctype_attributes(symbol);
buffer.append_printf (" glib:type-name=\"%s\"", CCodeBaseModule.get_ccode_name (symbol));
buffer.append_printf (" glib:get-type=\"%sget_type\"", CCodeBaseModule.get_ccode_lower_case_prefix (symbol));
}
private void write_type (DataType type, int index = -1) {
if (type is ArrayType) {
var array_type = (ArrayType) type;
write_indent ();
buffer.append_printf ("<array");
if (array_type.fixed_length) {
buffer.append_printf (" fixed-size=\"%i\"", array_type.length);
} else if (index != -1) {
buffer.append_printf (" length=\"%i\"", index);
}
buffer.append_printf (">\n");
indent++;
write_type (array_type.element_type);
indent--;
write_indent ();
buffer.append_printf ("</array>\n");
} else if (type is VoidType) {
write_indent ();
buffer.append_printf ("<type name=\"none\"/>\n");
} else if (type is PointerType) {
write_indent ();
buffer.append_printf ("<type name=\"gpointer\" c:type=\"%s\"/>\n", CCodeBaseModule.get_ccode_name (type));
} else if (type.data_type != null) {
write_indent ();
buffer.append_printf ("<type name=\"%s\" c:type=\"%s\"", gi_type_name (type.data_type), CCodeBaseModule.get_ccode_name (type));
List<DataType> type_arguments = type.get_type_arguments ();
if (type_arguments.size == 0) {
buffer.append_printf ("/>\n");
} else {
buffer.append_printf (">\n");
indent++;
foreach (DataType type_argument in type_arguments) {
write_type (type_argument);
}
indent--;
write_indent ();
buffer.append_printf ("</type>\n");
}
} else if (type is DelegateType) {
var deleg_type = (DelegateType) type;
write_indent ();
buffer.append_printf ("<type name=\"%s\" c:type=\"%s\"/>\n", gi_type_name (deleg_type.delegate_symbol), CCodeBaseModule.get_ccode_name (type));
} else if (type is GenericType) {
// generic type parameters not supported in GIR
write_indent ();
buffer.append ("<type name=\"gpointer\" c:type=\"gpointer\"/>\n");
} else {
write_indent ();
buffer.append_printf ("<type name=\"%s\"/>\n", type.to_string ());
}
}
private void write_annotations (CodeNode node) {
foreach (Attribute attr in node.attributes) {
string name = camel_case_to_canonical (attr.name);
foreach (string arg_name in attr.args.get_keys ()) {
string value = attr.args.get (arg_name);
if (value.has_prefix ("\"")) {
// eval string
value = attr.get_string (arg_name);
}
write_indent ();
buffer.append_printf ("<annotation key=\"%s.%s\" value=\"%s\"/>\n",
name, camel_case_to_canonical (arg_name), value);
}
}
}
private string? get_full_gir_name (Symbol sym) {
var gir_name = sym.get_attribute_string ("GIR", "name") ?? sym.name;
if (sym.parent_symbol == null) {
return gir_name;
}
if (sym.name == null) {
return get_full_gir_name (sym.parent_symbol);
}
string parent_gir_name = get_full_gir_name (sym.parent_symbol);
if (parent_gir_name == null) {
return gir_name;
}
string self_gir_name = gir_name.has_prefix (".") ? gir_name.substring (1) : gir_name;
if ("." in parent_gir_name) {
return "%s%s".printf (parent_gir_name, self_gir_name);
} else {
return "%s.%s".printf (parent_gir_name, self_gir_name);
}
}
private string gi_type_name (TypeSymbol type_symbol) {
Symbol parent = type_symbol.parent_symbol;
if (parent is Namespace) {
Namespace ns = parent as Namespace;
var ns_gir_name = ns.get_attribute_string ("GIR", "name") ?? ns.name;
if (ns_gir_name != null) {
if (type_symbol.source_reference.file.gir_namespace != null) {
GIRNamespace external = GIRNamespace (type_symbol.source_reference.file.gir_namespace, type_symbol.source_reference.file.gir_version);
if (!externals.contains (external)) {
externals.add (external);
}
var type_name = type_symbol.get_attribute_string ("GIR", "name") ?? type_symbol.name;
return "%s.%s".printf (type_symbol.source_reference.file.gir_namespace, type_name);
} else {
unannotated_namespaces.add(ns);
}
}
}
return get_full_gir_name (type_symbol);
}
private string? literal_expression_to_value_string (Expression literal) {
if (literal is StringLiteral) {
var lit = literal as StringLiteral;
if (lit != null) {
return Markup.escape_text (lit.eval ());
}
} else if (literal is CharacterLiteral) {
return "%c".printf ((char) ((CharacterLiteral) literal).get_char ());
} else if (literal is BooleanLiteral) {
return ((BooleanLiteral) literal).value ? "true" : "false";
} else if (literal is RealLiteral) {
return ((RealLiteral) literal).value;
} else if (literal is IntegerLiteral) {
return ((IntegerLiteral) literal).value;
} else if (literal is UnaryExpression) {
var unary = (UnaryExpression) literal;
if (unary.operator == UnaryOperator.MINUS) {
if (unary.inner is RealLiteral) {
return "-" + ((RealLiteral) unary.inner).value;
} else if (unary.inner is IntegerLiteral) {
return "-" + ((IntegerLiteral) unary.inner).value;
}
}
}
return null;
}
private string camel_case_to_canonical (string name) {
string[] parts = Symbol.camel_case_to_lower_case (name).split ("_");
return string.joinv ("-", parts);
}
private bool check_accessibility (Symbol sym) {
if (sym.access == SymbolAccessibility.PUBLIC ||
sym.access == SymbolAccessibility.PROTECTED) {
return true;
}
return false;
}
}
|