vala: Don't return null if no rank attribute was found
[vala-gnome.git] / codegen / valagirwriter.vala
blobd0de350524523c5ecb683dd2ac82dea6c69652a1
1 /* valagirwriter.vala
3 * Copyright (C) 2008-2012 Jürg Billeter
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 * Author:
20 * Jürg Billeter <j@bitron.ch>
23 using GLib;
25 /**
26 * Code visitor generating .gir file for the public interface.
28 public class Vala.GIRWriter : CodeVisitor {
29 private CodeContext context;
30 private string directory;
31 private string gir_namespace;
32 private string gir_version;
33 private string gir_shared_library;
35 protected virtual string? get_interface_comment (Interface iface) {
36 return null;
39 protected virtual string? get_struct_comment (Struct st) {
40 return null;
43 protected virtual string? get_enum_comment (Enum en) {
44 return null;
47 protected virtual string? get_class_comment (Class c) {
48 return null;
51 protected virtual string? get_error_code_comment (ErrorCode ecode) {
52 return null;
55 protected virtual string? get_enum_value_comment (EnumValue ev) {
56 return null;
59 protected virtual string? get_constant_comment (Constant c) {
60 return null;
63 protected virtual string? get_error_domain_comment (ErrorDomain edomain) {
64 return null;
67 protected virtual string? get_field_comment (Field f) {
68 return null;
71 protected virtual string? get_delegate_comment (Delegate cb) {
72 return null;
75 protected virtual string? get_method_comment (Method m) {
76 return null;
79 protected virtual string? get_property_comment (Property prop) {
80 return null;
83 protected virtual string? get_delegate_return_comment (Delegate cb) {
84 return null;
87 protected virtual string? get_signal_return_comment (Signal sig) {
88 return null;
91 protected virtual string? get_method_return_comment (Method m) {
92 return null;
95 protected virtual string? get_signal_comment (Signal sig) {
96 return null;
99 protected virtual string? get_parameter_comment (Parameter param) {
100 return null;
103 StringBuilder buffer = new StringBuilder();
104 FileStream stream;
105 Vala.HashSet<Namespace> unannotated_namespaces = new Vala.HashSet<Namespace>();
106 Vala.HashSet<Namespace> our_namespaces = new Vala.HashSet<Namespace>();
107 Vala.ArrayList<Vala.Symbol> hierarchy = new Vala.ArrayList<Vala.Symbol>();
108 Vala.ArrayList<Vala.CodeNode> deferred = new Vala.ArrayList<Vala.CodeNode>();
110 int indent;
112 private TypeSymbol gobject_type;
113 private TypeSymbol ginitiallyunowned_type;
115 private struct GIRNamespace {
116 public GIRNamespace (string ns, string version) {
117 this.ns = ns; this.version = version;
119 public string ns;
120 public string version;
121 public bool equal (GIRNamespace g) {
122 return ((ns == g.ns) && (version == g.version));
126 private ArrayList<GIRNamespace?> externals = new ArrayList<GIRNamespace?> ((EqualFunc<GIRNamespace>) GIRNamespace.equal);
128 public void write_includes() {
129 foreach (var i in externals) {
130 if (i.ns != this.gir_namespace) {
131 write_indent_stream ();
132 stream.printf ("<include name=\"%s\" version=\"%s\"/>\n", i.ns, i.version);
139 * Writes the public interface of the specified code context into the
140 * specified file.
142 * @param context a code context
143 * @param gir_filename a relative or absolute filename
145 public void write_file (CodeContext context, string directory, string gir_filename, string gir_namespace, string gir_version, string package, string? gir_shared_library = null) {
146 this.context = context;
147 this.directory = directory;
148 this.gir_namespace = gir_namespace;
149 this.gir_version = gir_version;
150 this.gir_shared_library = gir_shared_library;
152 var root_symbol = context.root;
153 var glib_ns = root_symbol.scope.lookup ("GLib");
154 gobject_type = (TypeSymbol) glib_ns.scope.lookup ("Object");
155 ginitiallyunowned_type = (TypeSymbol) glib_ns.scope.lookup ("InitiallyUnowned");
157 write_package (package);
159 context.accept (this);
161 indent--;
162 buffer.append_printf ("</repository>\n");
164 string filename = "%s%c%s".printf (directory, Path.DIR_SEPARATOR, gir_filename);
165 stream = FileStream.open (filename, "w");
166 if (stream == null) {
167 Report.error (null, "unable to open `%s' for writing".printf (filename));
168 this.context = null;
169 return;
172 stream.printf ("<?xml version=\"1.0\"?>\n");
174 stream.printf ("<repository version=\"1.2\"");
175 stream.printf (" xmlns=\"http://www.gtk.org/introspection/core/1.0\"");
176 stream.printf (" xmlns:c=\"http://www.gtk.org/introspection/c/1.0\"");
177 stream.printf (" xmlns:glib=\"http://www.gtk.org/introspection/glib/1.0\"");
178 stream.printf (">\n");
179 indent++;
181 write_includes();
182 indent--;
184 stream.puts (buffer.str);
185 stream = null;
187 foreach (var ns in unannotated_namespaces) {
188 if (!our_namespaces.contains(ns)) {
189 Report.warning (ns.source_reference, "Namespace %s does not have a GIR namespace and version annotation".printf (ns.name));
192 foreach (var ns in our_namespaces) {
193 ns.source_reference.file.gir_namespace = gir_namespace;
194 ns.source_reference.file.gir_version = gir_version;
197 if (our_namespaces.size == 0) {
198 Report.error (null, "No suitable namespace found to export for GIR");
201 this.context = null;
204 private void write_doc (string? comment) {
205 if (comment != null) {
206 write_indent ();
207 buffer.append ("<doc xml:whitespace=\"preserve\">");
208 buffer.append (comment);
209 buffer.append ("</doc>\n");
213 private void write_package (string package) {
214 write_indent ();
215 buffer.append_printf ("<package name=\"%s\"/>\n", package);
218 private void write_c_includes (Namespace ns) {
219 // Collect C header filenames
220 Set<string> header_filenames = new HashSet<string> (str_hash, str_equal);
221 foreach (unowned string c_header_filename in get_ccode_header_filenames (ns).split (",")) {
222 header_filenames.add (c_header_filename);
224 foreach (Symbol symbol in ns.scope.get_symbol_table ().get_values ()) {
225 foreach (unowned string c_header_filename in get_ccode_header_filenames (symbol).split (",")) {
226 header_filenames.add (c_header_filename);
230 // Generate c:include tags
231 foreach (string c_header_filename in header_filenames) {
232 write_c_include (c_header_filename);
236 private void write_c_include (string name) {
237 write_indent ();
238 buffer.append_printf ("<c:include name=\"%s\"/>\n", name);
241 public override void visit_namespace (Namespace ns) {
242 if (ns.external_package) {
243 return;
246 if (ns.name == null) {
247 // global namespace
248 hierarchy.insert (0, ns);
249 ns.accept_children (this);
250 hierarchy.remove_at (0);
251 return;
254 if (ns.parent_symbol.name != null) {
255 ns.accept_children (this);
256 return;
259 write_c_includes (ns);
261 write_indent ();
262 buffer.append_printf ("<namespace name=\"%s\" version=\"%s\"", gir_namespace, gir_version);
263 string? cprefix = get_ccode_prefix (ns);
264 if (gir_shared_library != null) {
265 buffer.append_printf(" shared-library=\"%s\"", gir_shared_library);
267 if (cprefix != null) {
268 buffer.append_printf (" c:prefix=\"%s\"", cprefix);
270 buffer.append_printf (">\n");
271 indent++;
273 write_annotations (ns);
275 hierarchy.insert (0, ns);
276 ns.accept_children (this);
277 hierarchy.remove_at (0);
279 indent--;
280 write_indent ();
281 buffer.append_printf ("</namespace>\n");
282 our_namespaces.add(ns);
284 visit_deferred ();
287 private void write_symbol_attributes (Symbol symbol) {
288 if (symbol.version.deprecated) {
289 buffer.append_printf (" deprecated=\"%s\"", (symbol.version.replacement == null) ? "" : "Use %s".printf (symbol.version.replacement));
290 if (symbol.version.deprecated_since != null) {
291 buffer.append_printf (" deprecated-version=\"%s\"", symbol.version.deprecated_since);
294 if (symbol.version.since != null) {
295 buffer.append_printf (" version=\"%s\"", symbol.version.since);
299 public override void visit_class (Class cl) {
300 if (cl.external_package) {
301 return;
304 if (!check_accessibility (cl)) {
305 return;
308 if (!(hierarchy[0] is Namespace)) {
309 deferred.add (cl);
310 return;
313 if (cl.is_subtype_of (gobject_type)) {
314 string gtype_struct_name = get_gir_name (cl) + "Class";
316 write_indent ();
317 buffer.append_printf ("<class name=\"%s\"", get_gir_name (cl));
318 write_gtype_attributes (cl);
319 buffer.append_printf (" glib:type-struct=\"%s\"", gtype_struct_name);
320 buffer.append_printf (" parent=\"%s\"", gi_type_name (cl.base_class));
321 if (cl.is_abstract) {
322 buffer.append_printf (" abstract=\"1\"");
324 write_symbol_attributes (cl);
325 buffer.append_printf (">\n");
326 indent++;
328 write_doc (get_class_comment (cl));
330 // write implemented interfaces
331 foreach (DataType base_type in cl.get_base_types ()) {
332 var object_type = (ObjectType) base_type;
333 if (object_type.type_symbol is Interface) {
334 write_indent ();
335 buffer.append_printf ("<implements name=\"%s\"/>\n", gi_type_name (object_type.type_symbol));
339 write_annotations (cl);
341 write_indent ();
342 buffer.append_printf ("<field name=\"parent_instance\">\n");
343 indent++;
344 write_indent ();
345 buffer.append_printf ("<type name=\"%s\" c:type=\"%s\"/>\n", gi_type_name (cl.base_class), get_ccode_name (cl.base_class));
346 indent--;
347 write_indent ();
348 buffer.append_printf("</field>\n");
350 write_indent ();
351 buffer.append_printf ("<field name=\"priv\">\n");
352 indent++;
353 write_indent ();
354 buffer.append_printf ("<type name=\"%sPrivate\" c:type=\"%sPrivate*\"/>\n", get_gir_name (cl), get_ccode_name (cl));
355 indent--;
356 write_indent ();
357 buffer.append_printf("</field>\n");
359 hierarchy.insert (0, cl);
360 cl.accept_children (this);
361 hierarchy.remove_at (0);
363 indent--;
364 write_indent ();
365 buffer.append_printf ("</class>\n");
367 write_indent ();
368 buffer.append_printf ("<record name=\"%s\"", gtype_struct_name);
369 write_ctype_attributes (cl, "Class");
370 buffer.append_printf (" glib:is-gtype-struct-for=\"%s\"", cl.name);
371 buffer.append_printf (">\n");
372 indent++;
374 write_indent ();
375 buffer.append_printf ("<field name=\"parent_class\">\n");
376 indent++;
377 write_indent ();
378 buffer.append_printf ("<type name=\"%sClass\" c:type=\"%sClass\"/>\n", gi_type_name (cl.base_class), get_ccode_name (cl.base_class));
379 indent--;
380 write_indent ();
381 buffer.append_printf ("</field>\n");
383 foreach (Method m in cl.get_methods ()) {
384 if (m.is_abstract || m.is_virtual) {
385 write_indent ();
386 if (m.coroutine) {
387 string finish_name = m.name;
388 if (finish_name.has_suffix ("_async")) {
389 finish_name = finish_name.substring (0, finish_name.length - "_async".length);
391 finish_name += "_finish";
393 write_indent ();
394 buffer.append_printf("<field name=\"%s\">\n", m.name);
395 indent++;
396 do_write_signature (m, "callback", true, m.name, get_ccode_name (m), m.get_async_begin_parameters (), new VoidType (), false, false);
397 indent--;
398 write_indent ();
399 buffer.append_printf ("</field>\n");
401 write_indent ();
402 buffer.append_printf("<field name=\"%s\">\n", finish_name);
403 indent++;
404 do_write_signature (m, "callback", true, finish_name, get_ccode_finish_name (m), m.get_async_end_parameters (), m.return_type, m.tree_can_fail, false);
405 indent--;
406 write_indent ();
407 buffer.append_printf ("</field>\n");
408 } else {
409 write_indent ();
410 buffer.append_printf("<field name=\"%s\">\n", m.name);
411 indent++;
412 do_write_signature (m, "callback", true, m.name, get_ccode_name (m), m.get_parameters (), m.return_type, m.tree_can_fail, false);
413 indent--;
414 write_indent ();
415 buffer.append_printf ("</field>\n");
420 foreach (Signal sig in cl.get_signals ()) {
421 if (sig.default_handler != null) {
422 write_indent ();
423 buffer.append_printf ("<field name=\"%s\">\n", get_ccode_lower_case_name (sig));
424 indent++;
425 write_signature (sig.default_handler, "callback", false, true);
426 indent--;
427 write_indent ();
428 buffer.append_printf ("</field>\n");
432 indent--;
433 write_indent ();
434 buffer.append_printf ("</record>\n");
436 write_indent ();
437 buffer.append_printf ("<record name=\"%sPrivate\" c:type=\"%sPrivate\" disguised=\"1\"/>\n", get_gir_name (cl), get_ccode_name (cl));
438 } else {
439 write_indent ();
440 buffer.append_printf ("<record name=\"%s\"", get_gir_name (cl));
441 write_symbol_attributes (cl);
442 buffer.append_printf (">\n");
443 indent++;
445 write_doc (get_class_comment (cl));
447 write_annotations (cl);
449 hierarchy.insert (0, cl);
450 cl.accept_children (this);
451 hierarchy.remove_at (0);
453 indent--;
454 write_indent ();
455 buffer.append_printf ("</record>\n");
458 visit_deferred ();
461 public override void visit_struct (Struct st) {
462 if (st.external_package) {
463 return;
466 if (!check_accessibility (st)) {
467 return;
470 if (!(hierarchy[0] is Namespace)) {
471 deferred.add (st);
472 return;
475 write_indent ();
476 buffer.append_printf ("<record name=\"%s\"", get_gir_name (st));
477 write_symbol_attributes (st);
478 buffer.append_printf (">\n");
479 indent++;
481 write_doc (get_struct_comment (st));
483 write_annotations (st);
485 hierarchy.insert (0, st);
486 st.accept_children (this);
487 hierarchy.remove_at (0);
489 indent--;
490 write_indent ();
491 buffer.append_printf ("</record>\n");
493 visit_deferred ();
496 public override void visit_interface (Interface iface) {
497 if (iface.external_package) {
498 return;
501 if (!check_accessibility (iface)) {
502 return;
505 if (!(hierarchy[0] is Namespace)) {
506 deferred.add (iface);
507 return;
510 string gtype_struct_name = iface.name + "Iface";
512 write_indent ();
513 buffer.append_printf ("<interface name=\"%s\"", get_gir_name (iface));
514 write_gtype_attributes (iface);
515 buffer.append_printf (" glib:type-struct=\"%s\"", gtype_struct_name);
516 write_symbol_attributes (iface);
517 buffer.append_printf (">\n");
518 indent++;
520 write_doc (get_interface_comment (iface));
522 // write prerequisites
523 if (iface.get_prerequisites ().size > 0) {
524 foreach (DataType base_type in iface.get_prerequisites ()) {
525 write_indent ();
526 buffer.append_printf ("<prerequisite name=\"%s\"/>\n", gi_type_name (((ObjectType) base_type).type_symbol));
530 write_annotations (iface);
532 hierarchy.insert (0, iface);
533 iface.accept_children (this);
534 hierarchy.remove_at (0);
536 indent--;
537 write_indent ();
538 buffer.append_printf ("</interface>\n");
540 write_indent ();
541 buffer.append_printf ("<record name=\"%s\"", gtype_struct_name);
542 write_ctype_attributes (iface, "Iface");
543 buffer.append_printf (" glib:is-gtype-struct-for=\"%s\"", iface.name);
544 buffer.append_printf (">\n");
545 indent++;
547 write_indent ();
548 buffer.append_printf ("<field name=\"parent_iface\">\n");
549 indent++;
550 write_indent ();
551 buffer.append_printf ("<type name=\"GObject.TypeInterface\" c:type=\"GTypeInterface\"/>\n");
552 indent--;
553 write_indent ();
554 buffer.append_printf ("</field>\n");
556 foreach (Method m in iface.get_methods ()) {
557 if (m.is_abstract || m.is_virtual) {
558 if (m.coroutine) {
559 string finish_name = m.name;
560 if (finish_name.has_suffix ("_async")) {
561 finish_name = finish_name.substring (0, finish_name.length - "_async".length);
563 finish_name += "_finish";
565 write_indent ();
566 buffer.append_printf("<field name=\"%s\">\n", m.name);
567 indent++;
568 do_write_signature (m, "callback", true, m.name, get_ccode_name (m), m.get_async_begin_parameters (), new VoidType (), false, false);
569 indent--;
570 write_indent ();
571 buffer.append_printf ("</field>\n");
573 write_indent ();
574 buffer.append_printf("<field name=\"%s\">\n", finish_name);
575 indent++;
576 do_write_signature (m, "callback", true, finish_name, get_ccode_finish_name (m), m.get_async_end_parameters (), m.return_type, m.tree_can_fail, false);
577 indent--;
578 write_indent ();
579 buffer.append_printf ("</field>\n");
580 } else {
581 write_indent ();
582 buffer.append_printf("<field name=\"%s\">\n", m.name);
583 indent++;
584 do_write_signature (m, "callback", true, m.name, get_ccode_name (m), m.get_parameters (), m.return_type, m.tree_can_fail, false);
585 indent--;
586 write_indent ();
587 buffer.append_printf ("</field>\n");
592 foreach (var prop in iface.get_properties ()) {
593 if (prop.is_abstract || prop.is_virtual) {
594 if (prop.get_accessor != null) {
595 var m = prop.get_accessor.get_method ();
596 write_indent ();
597 buffer.append_printf("<field name=\"%s\">\n", m.name);
598 indent++;
599 do_write_signature (m, "callback", true, m.name, get_ccode_name (m), m.get_parameters (), m.return_type, m.tree_can_fail, false);
600 indent--;
601 write_indent ();
602 buffer.append_printf ("</field>\n");
605 if (prop.set_accessor != null) {
606 var m = prop.set_accessor.get_method ();
607 write_indent ();
608 buffer.append_printf("<field name=\"%s\">\n", m.name);
609 indent++;
610 do_write_signature (m, "callback", true, m.name, get_ccode_name (m), m.get_parameters (), m.return_type, m.tree_can_fail, false);
611 indent--;
612 write_indent ();
613 buffer.append_printf ("</field>\n");
618 indent--;
619 write_indent ();
620 buffer.append_printf ("</record>\n");
622 visit_deferred ();
625 private void visit_deferred () {
626 var nodes = this.deferred;
627 this.deferred = new Vala.ArrayList<Vala.CodeNode>();
629 foreach (var node in nodes) {
630 node.accept (this);
634 private string? get_gir_name (Symbol symbol) {
635 string? gir_name = null;
636 var h0 = hierarchy[0];
638 for (Symbol? cur_sym = symbol ; cur_sym != null ; cur_sym = cur_sym.parent_symbol) {
639 if (cur_sym == h0) {
640 break;
643 var cur_name = cur_sym.get_attribute_string ("GIR", "name");
644 if (cur_name == null) {
645 cur_name = cur_sym.name;
647 gir_name = cur_name.concat (gir_name);
650 return gir_name;
653 public override void visit_enum (Enum en) {
654 if (en.external_package) {
655 return;
658 if (!check_accessibility (en)) {
659 return;
662 if (!(hierarchy[0] is Namespace)) {
663 deferred.add (en);
664 return;
667 string element_name = (en.is_flags) ? "bitfield" : "enumeration";
669 write_indent ();
670 buffer.append_printf ("<%s name=\"%s\"", element_name, get_gir_name (en));
671 write_gtype_attributes (en);
672 write_symbol_attributes (en);
673 buffer.append_printf (">\n");
674 indent++;
676 write_doc (get_enum_comment (en));
678 write_annotations (en);
680 enum_value = 0;
681 hierarchy.insert (0, en);
682 en.accept_children (this);
683 hierarchy.remove_at (0);
685 indent--;
686 write_indent ();
687 buffer.append_printf ("</%s>\n", element_name);
689 visit_deferred ();
692 private int enum_value;
694 public override void visit_enum_value (EnumValue ev) {
695 write_indent ();
696 var en = (Enum) hierarchy[0];
697 buffer.append_printf ("<member name=\"%s\" c:identifier=\"%s\"", ev.name.down (), get_ccode_name (ev));
698 if (ev.value != null) {
699 string value = literal_expression_to_value_string (ev.value);
700 buffer.append_printf (" value=\"%s\"", value);
701 } else {
702 if (en.is_flags) {
703 buffer.append_printf (" value=\"%d\"", 1 << enum_value++);
704 } else {
705 buffer.append_printf (" value=\"%d\"", enum_value++);
708 write_symbol_attributes (ev);
710 string? comment = get_enum_value_comment (ev);
711 if (comment == null) {
712 buffer.append_printf ("/>\n");
713 } else {
714 buffer.append_printf (">\n");
715 indent++;
717 write_doc (comment);
719 indent--;
720 write_indent ();
721 buffer.append_printf ("</member>\n");
725 public override void visit_error_domain (ErrorDomain edomain) {
726 if (edomain.external_package) {
727 return;
730 if (!check_accessibility (edomain)) {
731 return;
734 write_indent ();
735 buffer.append_printf ("<enumeration name=\"%s\"", edomain.name);
736 write_ctype_attributes (edomain);
737 buffer.append_printf (" glib:error-domain=\"%s\"", get_ccode_quark_name (edomain));
738 buffer.append_printf (">\n");
739 indent++;
741 write_doc (get_error_domain_comment (edomain));
743 enum_value = 0;
744 hierarchy.insert (0, edomain);
745 edomain.accept_children (this);
746 hierarchy.remove_at (0);
748 indent--;
749 write_indent ();
750 buffer.append_printf ("</enumeration>\n");
752 visit_deferred ();
755 public override void visit_error_code (ErrorCode ecode) {
756 write_indent ();
757 buffer.append_printf ("<member name=\"%s\" c:identifier=\"%s\"", ecode.name.down (), get_ccode_name (ecode));
758 if (ecode.value != null) {
759 string value = literal_expression_to_value_string (ecode.value);
760 buffer.append_printf (" value=\"%s\"", value);
761 } else {
762 buffer.append_printf (" value=\"%d\"", enum_value++);
764 write_symbol_attributes (ecode);
766 string? comment = get_error_code_comment (ecode);
767 if (comment == null) {
768 buffer.append_printf ("/>\n");
769 } else {
770 buffer.append_printf (">\n");
771 indent++;
773 write_doc (comment);
775 indent--;
776 write_indent ();
777 buffer.append_printf ("</member>\n");
781 public override void visit_constant (Constant c) {
782 if (c.external_package) {
783 return;
786 if (!check_accessibility (c)) {
787 return;
790 //TODO Add better constant evaluation
791 var initializer = c.value;
792 string value = literal_expression_to_value_string (initializer);
794 write_indent ();
795 buffer.append_printf ("<constant name=\"%s\" c:identifier=\"%s\"", c.name, get_ccode_name (c));
796 buffer.append_printf (" value=\"%s\"", value);
797 write_symbol_attributes (c);
798 buffer.append_printf (">\n");
799 indent++;
801 write_doc (get_constant_comment (c));
803 write_type (initializer.value_type);
805 indent--;
806 write_indent ();
807 buffer.append_printf ("</constant>\n");
810 public override void visit_field (Field f) {
811 if (f.external_package) {
812 return;
815 if (!check_accessibility (f)) {
816 return;
819 write_indent ();
820 buffer.append_printf ("<field name=\"%s\"", get_ccode_name (f));
821 if (f.variable_type.nullable) {
822 buffer.append_printf (" allow-none=\"1\"");
824 write_symbol_attributes (f);
825 buffer.append_printf (">\n");
826 indent++;
828 write_doc (get_field_comment (f));
830 write_annotations (f);
832 write_type (f.variable_type);
834 indent--;
835 write_indent ();
836 buffer.append_printf ("</field>\n");
839 private void write_implicit_params (DataType type, ref int index, bool has_array_length, string name, ParameterDirection direction) {
840 if (type is ArrayType && has_array_length) {
841 var int_type = new IntegerType (context.root.scope.lookup ("int") as Struct);
842 for (var i = 0; i < ((ArrayType) type).rank; i++) {
843 write_param_or_return (int_type, true, ref index, has_array_length, "%s_length%i".printf (name, i + 1), null, direction);
845 } else if (type is DelegateType) {
846 var deleg_type = (DelegateType) type;
847 if (deleg_type.delegate_symbol.has_target) {
848 var data_type = new PointerType (new VoidType ());
849 write_param_or_return (data_type, true, ref index, false, "%s_target".printf (name), null, direction);
850 if (deleg_type.is_disposable ()) {
851 var notify_type = new DelegateType (context.root.scope.lookup ("GLib").scope.lookup ("DestroyNotify") as Delegate);
852 write_param_or_return (notify_type, true, ref index, false, "%s_target_destroy_notify".printf (name), null, direction);
858 void skip_implicit_params (DataType type, ref int index, bool has_array_length) {
859 if (type is ArrayType && has_array_length) {
860 index += ((ArrayType) type).rank;
861 } else if (type is DelegateType) {
862 index++;
863 var deleg_type = (DelegateType) type;
864 if (deleg_type.is_disposable ()) {
865 index++;
870 private void write_params_and_return (List<Parameter> params, DataType? return_type, bool return_array_length, string? return_comment = null, bool constructor = false, DataType? instance_type = null, bool user_data = false) {
871 int last_index = 0;
872 bool ret_is_struct = return_type != null && return_type.is_real_non_null_struct_type ();
874 if (params.size != 0 || instance_type != null || (return_type is ArrayType && return_array_length) || (return_type is DelegateType) || ret_is_struct) {
875 int index = 0;
877 if (instance_type != null) {
878 index++;
881 foreach (Parameter param in params) {
882 index++;
884 skip_implicit_params (param.variable_type, ref index, get_ccode_array_length (param));
887 if (ret_is_struct) {
888 index++;
889 } else {
890 skip_implicit_params (return_type, ref index, return_array_length);
891 if (return_type is ArrayType && return_array_length) {
892 index -= ((ArrayType) return_type).rank - 1;
896 last_index = index - 1;
899 if (return_type != null && !ret_is_struct) {
900 write_param_or_return (return_type, false, ref last_index, return_array_length, null, return_comment, ParameterDirection.IN, constructor);
901 } else if (ret_is_struct) {
902 write_param_or_return (new VoidType (), false, ref last_index, false, null, return_comment, ParameterDirection.IN);
905 if (params.size != 0 || instance_type != null || (return_type is ArrayType && return_array_length) || (return_type is DelegateType) || ret_is_struct) {
906 write_indent ();
907 buffer.append_printf ("<parameters>\n");
908 indent++;
909 int index = 0;
911 if (instance_type != null) {
912 write_param_or_return (instance_type, true, ref index, false, "self");
915 foreach (Parameter param in params) {
916 write_param_or_return (param.variable_type, true, ref index, get_ccode_array_length (param), param.name, get_parameter_comment (param), param.direction);
918 write_implicit_params (param.variable_type, ref index, get_ccode_array_length (param), param.name, param.direction);
921 if (ret_is_struct) {
922 // struct returns are converted to parameters
923 write_param_or_return (return_type, true, ref index, false, "result", return_comment, ParameterDirection.OUT, constructor, true);
924 } else {
925 write_implicit_params (return_type, ref index, return_array_length, "result", ParameterDirection.OUT);
928 if (user_data) {
929 write_indent ();
930 buffer.append_printf ("<parameter name=\"user_data\" transfer-ownership=\"none\" closure=\"%d\">\n", index);
931 indent++;
932 write_indent ();
933 buffer.append_printf ("<type name=\"gpointer\" c:type=\"void*\"/>\n");
934 indent--;
935 write_indent ();
936 buffer.append_printf ("</parameter>\n");
939 indent--;
940 write_indent ();
941 buffer.append_printf ("</parameters>\n");
945 public override void visit_delegate (Delegate cb) {
946 if (cb.external_package) {
947 return;
950 if (!check_accessibility (cb)) {
951 return;
954 write_indent ();
955 buffer.append_printf ("<callback name=\"%s\"", cb.name);
956 buffer.append_printf (" c:type=\"%s\"", get_ccode_name (cb));
957 if (cb.tree_can_fail) {
958 buffer.append_printf (" throws=\"1\"");
960 write_symbol_attributes (cb);
961 buffer.append_printf (">\n");
962 indent++;
964 write_doc (get_delegate_comment (cb));
966 write_annotations (cb);
968 write_params_and_return (cb.get_parameters (), cb.return_type, get_ccode_array_length (cb), get_delegate_return_comment (cb), false, null, cb.has_target);
970 indent--;
971 write_indent ();
972 buffer.append_printf ("</callback>\n");
975 public override void visit_method (Method m) {
976 if (m.external_package) {
977 return;
980 // don't write interface implementation unless it's an abstract or virtual method
981 if (!check_accessibility (m) || m.overrides || (m.base_interface_method != null && !m.is_abstract && !m.is_virtual)) {
982 return;
985 // check for unsupported types
986 if (!check_signature (m)) {
987 return;
990 string tag_name = "method";
991 var parent = this.hierarchy.get (0);
992 if (parent is Enum) {
993 deferred.add (m);
994 return;
997 if (parent is Namespace || m.binding == MemberBinding.STATIC || parent != m.parent_symbol) {
998 tag_name = "function";
1001 write_signature (m, tag_name, true);
1003 if (m.is_abstract || m.is_virtual) {
1004 write_signature (m, "virtual-method", true, false);
1008 bool check_type (DataType type) {
1009 // gobject-introspection does not currently support va_list parameters
1010 if (get_ccode_name (type) == "va_list") {
1011 return false;
1014 return true;
1017 bool check_signature (Method m) {
1018 if (!check_type (m.return_type)) {
1019 return false;
1021 foreach (var param in m.get_parameters ()) {
1022 if (param.variable_type == null || !check_type (param.variable_type)) {
1023 return false;
1026 return true;
1029 private void write_signature (Method m, string tag_name, bool write_doc, bool instance = false) {
1030 var parent = this.hierarchy.get (0);
1031 string name;
1032 if (m.parent_symbol != parent) {
1033 instance = false;
1034 name = get_ccode_name (m);
1035 var parent_prefix = get_ccode_lower_case_prefix (parent);
1036 if (name.has_prefix (parent_prefix)) {
1037 name = name.substring (parent_prefix.length);
1039 } else {
1040 name = m.name;
1043 if (m.coroutine) {
1044 string finish_name = name;
1045 if (finish_name.has_suffix ("_async")) {
1046 finish_name = finish_name.substring (0, finish_name.length - "_async".length);
1048 finish_name += "_finish";
1049 do_write_signature (m, tag_name, instance, name, get_ccode_name (m), m.get_async_begin_parameters (), new VoidType (), false, true);
1050 do_write_signature (m, tag_name, instance, finish_name, get_ccode_finish_name (m), m.get_async_end_parameters (), m.return_type, m.tree_can_fail, false);
1051 } else {
1052 do_write_signature (m, tag_name, instance, name, get_ccode_name (m), m.get_parameters (), m.return_type, m.tree_can_fail, true);
1056 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, bool write_comment) {
1057 write_indent ();
1058 buffer.append_printf ("<%s name=\"%s\"", tag_name, name);
1059 if (tag_name == "virtual-method") {
1060 buffer.append_printf (" invoker=\"%s\"", name);
1061 } else if (tag_name == "callback") {
1062 /* this is only used for vfuncs */
1063 buffer.append_printf (" c:type=\"%s\"", name);
1064 } else {
1065 buffer.append_printf (" c:identifier=\"%s\"", cname);
1067 if (can_fail) {
1068 buffer.append_printf (" throws=\"1\"");
1070 write_symbol_attributes (m);
1071 buffer.append_printf (">\n");
1072 indent++;
1074 string? return_comment = null;
1075 if (write_comment) {
1076 return_comment = get_method_return_comment (m);
1077 write_doc (get_method_comment (m));
1080 write_annotations (m);
1082 DataType instance_type = null;
1083 if (instance) {
1084 instance_type = CCodeBaseModule.get_data_type_for_symbol ((TypeSymbol) m.parent_symbol);
1087 write_params_and_return (params, return_type, get_ccode_array_length (m), return_comment, false, instance_type);
1089 indent--;
1090 write_indent ();
1091 buffer.append_printf ("</%s>\n", tag_name);
1094 public override void visit_creation_method (CreationMethod m) {
1095 if (m.external_package) {
1096 return;
1099 if (!check_accessibility (m)) {
1100 return;
1103 if (m.parent_symbol is Class && ((Class) m.parent_symbol).is_abstract) {
1104 return;
1107 write_indent ();
1109 bool is_struct = m.parent_symbol is Struct;
1110 // GI doesn't like constructors that return void type
1111 string tag_name = is_struct ? "function" : "constructor";
1113 if (m.parent_symbol is Class && m == ((Class)m.parent_symbol).default_construction_method ||
1114 m.parent_symbol is Struct && m == ((Struct)m.parent_symbol).default_construction_method) {
1115 string m_name = is_struct ? "init" : "new";
1116 buffer.append_printf ("<%s name=\"%s\" c:identifier=\"%s\"", tag_name, m_name, get_ccode_name (m));
1117 } else {
1118 buffer.append_printf ("<%s name=\"%s\" c:identifier=\"%s\"", tag_name, m.name, get_ccode_name (m));
1121 if (m.tree_can_fail) {
1122 buffer.append_printf (" throws=\"1\"");
1124 buffer.append_printf (">\n");
1125 indent++;
1127 write_doc (get_method_comment (m));
1129 write_annotations (m);
1132 var datatype = CCodeBaseModule.get_data_type_for_symbol ((TypeSymbol) m.parent_symbol);
1133 write_params_and_return (m.get_parameters (), datatype, false, get_method_return_comment (m), true);
1135 indent--;
1136 write_indent ();
1137 buffer.append_printf ("</%s>\n", tag_name);
1140 public override void visit_property (Property prop) {
1141 if (!check_accessibility (prop) || prop.overrides || (prop.base_interface_property != null && !prop.is_abstract && !prop.is_virtual)) {
1142 return;
1145 write_indent ();
1146 buffer.append_printf ("<property name=\"%s\"", prop.name.replace ("_", "-"));
1147 if (prop.get_accessor == null) {
1148 buffer.append_printf (" readable=\"0\"");
1150 if (prop.set_accessor != null) {
1151 buffer.append_printf (" writable=\"1\"");
1152 if (prop.set_accessor.construction) {
1153 if (!prop.set_accessor.writable) {
1154 buffer.append_printf (" construct-only=\"1\"");
1155 } else {
1156 buffer.append_printf (" construct=\"1\"");
1160 write_symbol_attributes (prop);
1161 buffer.append_printf (">\n");
1162 indent++;
1164 write_doc (get_property_comment (prop));
1166 write_annotations (prop);
1168 write_type (prop.property_type);
1170 indent--;
1171 write_indent ();
1172 buffer.append_printf ("</property>\n");
1174 if (prop.get_accessor != null) {
1175 var m = prop.get_accessor.get_method ();
1176 if (m != null) {
1177 visit_method (m);
1181 if (prop.set_accessor != null) {
1182 var m = prop.set_accessor.get_method ();
1183 if (m != null) {
1184 visit_method (m);
1189 public override void visit_signal (Signal sig) {
1190 if (!check_accessibility (sig)) {
1191 return;
1194 if (sig.emitter != null) {
1195 sig.emitter.accept (this);
1198 write_indent ();
1199 buffer.append_printf ("<glib:signal name=\"%s\"", get_ccode_name (sig));
1200 write_symbol_attributes (sig);
1201 buffer.append_printf (">\n");
1202 indent++;
1204 write_doc (get_signal_comment (sig));
1206 write_annotations (sig);
1208 write_params_and_return (sig.get_parameters (), sig.return_type, false, get_signal_return_comment (sig));
1210 indent--;
1211 write_indent ();
1212 buffer.append_printf ("</glib:signal>\n");
1215 private void write_indent () {
1216 int i;
1218 for (i = 0; i < indent; i++) {
1219 buffer.append_c ('\t');
1223 private void write_indent_stream () {
1224 int i;
1226 for (i = 0; i < indent; i++) {
1227 stream.putc ('\t');
1232 private void write_param_or_return (DataType type, bool is_parameter, ref int index, bool has_array_length, string? name = null, string? comment = null, ParameterDirection direction = ParameterDirection.IN, bool constructor = false, bool caller_allocates = false) {
1233 write_indent ();
1234 string tag = is_parameter ? "parameter" : "return-value";
1235 buffer.append_printf ("<%s", tag);
1236 if (name != null) {
1237 buffer.append_printf (" name=\"%s\"", name);
1239 if (direction == ParameterDirection.REF) {
1240 buffer.append_printf (" direction=\"inout\"");
1241 } else if (direction == ParameterDirection.OUT) {
1242 buffer.append_printf (" direction=\"out\"");
1245 DelegateType delegate_type = type as DelegateType;
1247 if ((type.value_owned && delegate_type == null) || (constructor && !type.data_type.is_subtype_of (ginitiallyunowned_type))) {
1248 var any_owned = false;
1249 foreach (var generic_arg in type.get_type_arguments ()) {
1250 any_owned |= generic_arg.value_owned;
1252 if (type.has_type_arguments () && !any_owned) {
1253 buffer.append_printf (" transfer-ownership=\"container\"");
1254 } else {
1255 buffer.append_printf (" transfer-ownership=\"full\"");
1257 } else {
1258 buffer.append_printf (" transfer-ownership=\"none\"");
1260 if (caller_allocates) {
1261 buffer.append_printf (" caller-allocates=\"1\"");
1263 if (type.nullable) {
1264 buffer.append_printf (" allow-none=\"1\"");
1267 if (delegate_type != null && delegate_type.delegate_symbol.has_target) {
1268 int closure_index = is_parameter ?
1269 index + 1 : (type.value_owned ? index - 1 : index);
1270 buffer.append_printf (" closure=\"%i\"", closure_index);
1271 if (delegate_type.is_called_once) {
1272 buffer.append (" scope=\"async\"");
1273 } else if (type.value_owned) {
1274 buffer.append_printf (" scope=\"notified\" destroy=\"%i\"", closure_index + 1);
1275 } else {
1276 buffer.append (" scope=\"call\"");
1278 } else if (delegate_type != null) {
1279 buffer.append (" scope=\"call\"");
1282 buffer.append_printf (">\n");
1283 indent++;
1285 write_doc (comment);
1287 int length_param_index = -1;
1288 if (has_array_length) {
1289 length_param_index = is_parameter ? index + 1 : index;
1291 write_type (type, length_param_index, direction);
1293 indent--;
1294 write_indent ();
1295 buffer.append_printf ("</%s>\n", tag);
1296 index++;
1299 private void write_ctype_attributes (TypeSymbol symbol, string suffix = "") {
1300 buffer.append_printf (" c:type=\"%s%s\"", get_ccode_name (symbol), suffix);
1303 private void write_gtype_attributes (TypeSymbol symbol) {
1304 write_ctype_attributes(symbol);
1305 buffer.append_printf (" glib:type-name=\"%s\"", get_ccode_name (symbol));
1306 buffer.append_printf (" glib:get-type=\"%sget_type\"", get_ccode_lower_case_prefix (symbol));
1309 private void write_type (DataType type, int index = -1, ParameterDirection direction = ParameterDirection.IN) {
1310 if (type is ArrayType) {
1311 var array_type = (ArrayType) type;
1313 write_indent ();
1314 buffer.append_printf ("<array");
1315 if (array_type.fixed_length && array_type.length is IntegerLiteral) {
1316 var lit = (IntegerLiteral) array_type.length;
1317 buffer.append_printf (" fixed-size=\"%i\"", int.parse (lit.value));
1318 } else if (index != -1) {
1319 buffer.append_printf (" length=\"%i\"", index);
1321 buffer.append_printf (">\n");
1322 indent++;
1324 write_type (array_type.element_type);
1326 indent--;
1327 write_indent ();
1328 buffer.append_printf ("</array>\n");
1329 } else if (type is VoidType) {
1330 write_indent ();
1331 buffer.append_printf ("<type name=\"none\"/>\n");
1332 } else if (type is PointerType) {
1333 write_indent ();
1334 buffer.append_printf ("<type name=\"gpointer\" c:type=\"%s\"/>\n", get_ccode_name (type));
1335 } else if (type.data_type != null) {
1336 write_indent ();
1337 string type_name = gi_type_name (type.data_type);
1338 bool is_array = false;
1339 if ((type_name == "GLib.Array") || (type_name == "GLib.PtrArray")) {
1340 is_array = true;
1342 buffer.append_printf ("<%s name=\"%s\" c:type=\"%s%s\"", is_array ? "array" : "type", gi_type_name (type.data_type), get_ccode_name (type), direction == ParameterDirection.IN ? "" : "*");
1344 List<DataType> type_arguments = type.get_type_arguments ();
1345 if (type_arguments.size == 0) {
1346 buffer.append_printf ("/>\n");
1347 } else {
1348 buffer.append_printf (">\n");
1349 indent++;
1351 foreach (DataType type_argument in type_arguments) {
1352 write_type (type_argument);
1355 indent--;
1356 write_indent ();
1357 buffer.append_printf ("</%s>\n", is_array ? "array" : "type");
1359 } else if (type is DelegateType) {
1360 var deleg_type = (DelegateType) type;
1361 write_indent ();
1362 buffer.append_printf ("<type name=\"%s\" c:type=\"%s\"/>\n", gi_type_name (deleg_type.delegate_symbol), get_ccode_name (type));
1363 } else if (type is GenericType) {
1364 // generic type parameters not supported in GIR
1365 write_indent ();
1366 buffer.append ("<type name=\"gpointer\" c:type=\"gpointer\"/>\n");
1367 } else {
1368 write_indent ();
1369 buffer.append_printf ("<type name=\"%s\"/>\n", type.to_string ());
1373 private void write_annotations (CodeNode node) {
1374 foreach (Attribute attr in node.attributes) {
1375 string name = camel_case_to_canonical (attr.name);
1376 foreach (string arg_name in attr.args.get_keys ()) {
1377 string value = attr.args.get (arg_name);
1378 if (value.has_prefix ("\"")) {
1379 // eval string
1380 value = attr.get_string (arg_name);
1383 write_indent ();
1384 buffer.append_printf ("<attribute name=\"%s.%s\" value=\"%s\"/>\n",
1385 name, camel_case_to_canonical (arg_name), value);
1390 private string? get_full_gir_name (Symbol sym) {
1391 string? gir_fullname = sym.get_attribute_string ("GIR", "fullname");
1392 if (gir_fullname != null) {
1393 return gir_fullname;
1396 string? gir_name = sym.get_attribute_string ("GIR", "name");
1398 if (gir_name == null && sym is Namespace) {
1399 gir_name = sym.get_attribute_string ("CCode", "gir_namespace");
1401 if (gir_name == null) {
1402 gir_name = sym.name;
1405 if (sym.parent_symbol == null) {
1406 return gir_name;
1409 if (sym.name == null) {
1410 return get_full_gir_name (sym.parent_symbol);
1413 string parent_gir_name = get_full_gir_name (sym.parent_symbol);
1414 if (parent_gir_name == null) {
1415 return gir_name;
1418 string self_gir_name = gir_name.has_prefix (".") ? gir_name.substring (1) : gir_name;
1419 if ("." in parent_gir_name) {
1420 return "%s%s".printf (parent_gir_name, self_gir_name);
1421 } else {
1422 return "%s.%s".printf (parent_gir_name, self_gir_name);
1426 private string gi_type_name (TypeSymbol type_symbol) {
1427 Symbol parent = type_symbol.parent_symbol;
1428 if (parent is Namespace) {
1429 Namespace ns = parent as Namespace;
1430 var ns_gir_name = ns.get_attribute_string ("GIR", "name") ?? ns.name;
1431 if (ns_gir_name != null) {
1432 if (type_symbol.source_reference.file.gir_namespace != null) {
1433 GIRNamespace external = GIRNamespace (type_symbol.source_reference.file.gir_namespace, type_symbol.source_reference.file.gir_version);
1434 if (!externals.contains (external)) {
1435 externals.add (external);
1437 string? gir_fullname = type_symbol.get_attribute_string ("GIR", "fullname");
1438 if (gir_fullname != null) {
1439 return gir_fullname;
1441 var type_name = type_symbol.get_attribute_string ("GIR", "name") ?? type_symbol.name;
1442 return "%s.%s".printf (type_symbol.source_reference.file.gir_namespace, type_name);
1443 } else {
1444 unannotated_namespaces.add(ns);
1449 return get_full_gir_name (type_symbol);
1452 private string? literal_expression_to_value_string (Expression literal) {
1453 if (literal is StringLiteral) {
1454 var lit = literal as StringLiteral;
1455 if (lit != null) {
1456 return Markup.escape_text (lit.eval ());
1458 } else if (literal is CharacterLiteral) {
1459 return "%c".printf ((char) ((CharacterLiteral) literal).get_char ());
1460 } else if (literal is BooleanLiteral) {
1461 return ((BooleanLiteral) literal).value ? "true" : "false";
1462 } else if (literal is RealLiteral) {
1463 return ((RealLiteral) literal).value;
1464 } else if (literal is IntegerLiteral) {
1465 return ((IntegerLiteral) literal).value;
1466 } else if (literal is UnaryExpression) {
1467 var unary = (UnaryExpression) literal;
1468 if (unary.operator == UnaryOperator.MINUS) {
1469 if (unary.inner is RealLiteral) {
1470 return "-" + ((RealLiteral) unary.inner).value;
1471 } else if (unary.inner is IntegerLiteral) {
1472 return "-" + ((IntegerLiteral) unary.inner).value;
1476 return null;
1479 private string camel_case_to_canonical (string name) {
1480 string[] parts = Symbol.camel_case_to_lower_case (name).split ("_");
1481 return string.joinv ("-", parts);
1484 private bool check_accessibility (Symbol sym) {
1485 if (sym.access == SymbolAccessibility.PUBLIC ||
1486 sym.access == SymbolAccessibility.PROTECTED) {
1487 return true;
1490 return false;