girwriter: Fix indices for delegate parameters
[vala-lang.git] / codegen / valagirwriter.vala
blob60ab49e52bcafe93c5e62024c66beae612f02e6a
1 /* valagirwriter.vala
3 * Copyright (C) 2008-2010 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;
34 StringBuilder buffer = new StringBuilder();
35 FileStream stream;
36 Vala.HashSet<Namespace> unannotated_namespaces = new Vala.HashSet<Namespace>();
37 Vala.HashSet<Namespace> our_namespaces = new Vala.HashSet<Namespace>();
38 Vala.ArrayList<Vala.Symbol> hierarchy = new Vala.ArrayList<Vala.Symbol>();
39 Vala.ArrayList<Vala.CodeNode> deferred = new Vala.ArrayList<Vala.CodeNode>();
41 int indent;
43 private TypeSymbol gobject_type;
45 private struct GIRNamespace {
46 public GIRNamespace (string ns, string version) {
47 this.ns = ns; this.version = version;
49 public string ns;
50 public string version;
51 public bool equal (GIRNamespace g) {
52 return ((ns == g.ns) && (version == g.version));
56 private ArrayList<GIRNamespace?> externals = new ArrayList<GIRNamespace?> ((EqualFunc) GIRNamespace.equal);
58 public void write_includes() {
59 foreach (var i in externals) {
60 write_indent_stream ();
61 stream.printf ("<include name=\"%s\" version=\"%s\"/>\n", i.ns, i.version);
66 /**
67 * Writes the public interface of the specified code context into the
68 * specified file.
70 * @param context a code context
71 * @param filename a relative or absolute filename
73 public void write_file (CodeContext context, string directory, string gir_namespace, string gir_version, string package) {
74 this.context = context;
75 this.directory = directory;
76 this.gir_namespace = gir_namespace;
77 this.gir_version = gir_version;
79 var root_symbol = context.root;
80 var glib_ns = root_symbol.scope.lookup ("GLib");
81 gobject_type = (TypeSymbol) glib_ns.scope.lookup ("Object");
83 write_package (package);
85 context.accept (this);
87 indent--;
88 buffer.append_printf ("</repository>\n");
90 string filename = "%s%c%s-%s.gir".printf (directory, Path.DIR_SEPARATOR, gir_namespace, gir_version);
91 stream = FileStream.open (filename, "w");
92 if (stream == null) {
93 Report.error (null, "unable to open `%s' for writing".printf (filename));
94 return;
97 stream.printf ("<?xml version=\"1.0\"?>\n");
99 stream.printf ("<repository version=\"1.2\"");
100 stream.printf (" xmlns=\"http://www.gtk.org/introspection/core/1.0\"");
101 stream.printf (" xmlns:c=\"http://www.gtk.org/introspection/c/1.0\"");
102 stream.printf (" xmlns:glib=\"http://www.gtk.org/introspection/glib/1.0\"");
103 stream.printf (">\n");
104 indent++;
106 write_includes();
107 indent--;
109 stream.puts (buffer.str);
110 stream = null;
112 foreach (var ns in unannotated_namespaces) {
113 if (!our_namespaces.contains(ns)) {
114 Report.warning (ns.source_reference, "Namespace %s does not have a GIR namespace and version annotation".printf (ns.name));
117 foreach (var ns in our_namespaces) {
118 ns.source_reference.file.gir_namespace = gir_namespace;
119 ns.source_reference.file.gir_version = gir_version;
123 private void write_package (string package) {
124 write_indent ();
125 buffer.append_printf ("<package name=\"%s\"/>\n", package);
128 private void write_c_includes (Namespace ns) {
129 // Collect C header filenames
130 Set<string> header_filenames = new HashSet<string> (str_hash, str_equal);
131 foreach (string c_header_filename in ns.get_cheader_filenames ()) {
132 header_filenames.add (c_header_filename);
134 foreach (Symbol symbol in ns.scope.get_symbol_table ().get_values ()) {
135 foreach (string c_header_filename in symbol.get_cheader_filenames ()) {
136 header_filenames.add (c_header_filename);
140 // Generate c:include tags
141 foreach (string c_header_filename in header_filenames) {
142 write_c_include (c_header_filename);
146 private void write_c_include (string name) {
147 write_indent ();
148 buffer.append_printf ("<c:include name=\"%s\"/>\n", name);
151 public override void visit_namespace (Namespace ns) {
152 if (ns.external_package) {
153 return;
156 if (ns.name == null) {
157 // global namespace
158 hierarchy.insert (0, ns);
159 ns.accept_children (this);
160 hierarchy.remove_at (0);
161 return;
164 if (ns.parent_symbol.name != null) {
165 ns.accept_children (this);
166 return;
169 write_c_includes (ns);
171 write_indent ();
172 buffer.append_printf ("<namespace name=\"%s\" version=\"%s\"", gir_namespace, gir_version);
173 string? cprefix = ns.get_cprefix ();
174 if (cprefix != null) {
175 buffer.append_printf (" c:prefix=\"%s\"", cprefix);
177 buffer.append_printf (">\n");
178 indent++;
180 write_annotations (ns);
182 hierarchy.insert (0, ns);
183 ns.accept_children (this);
184 hierarchy.remove_at (0);
186 indent--;
187 write_indent ();
188 buffer.append_printf ("</namespace>\n");
189 our_namespaces.add(ns);
191 visit_deferred ();
194 private void write_symbol_attributes (Symbol symbol) {
195 if (symbol.deprecated) {
196 buffer.append_printf (" deprecated=\"%s\"", (symbol.replacement == null) ? "" : "Use %s".printf (symbol.replacement));
197 if (symbol.deprecated_since != null) {
198 buffer.append_printf (" deprecated-version=\"%s\"", symbol.deprecated_since);
203 public override void visit_class (Class cl) {
204 if (cl.external_package) {
205 return;
208 if (!check_accessibility (cl)) {
209 return;
212 if (!(hierarchy[0] is Namespace)) {
213 deferred.add (cl);
214 return;
217 if (cl.is_subtype_of (gobject_type)) {
218 string gtype_struct_name = cl.name + "Class";
220 write_indent ();
221 buffer.append_printf ("<class name=\"%s\"", get_gir_name (cl));
222 write_gtype_attributes (cl);
223 buffer.append_printf (" glib:type-struct=\"%s\"", gtype_struct_name);
224 buffer.append_printf (" parent=\"%s\"", gi_type_name (cl.base_class));
225 if (cl.is_abstract) {
226 buffer.append_printf (" abstract=\"1\"");
228 write_symbol_attributes (cl);
229 buffer.append_printf (">\n");
230 indent++;
232 // write implemented interfaces
233 foreach (DataType base_type in cl.get_base_types ()) {
234 var object_type = (ObjectType) base_type;
235 if (object_type.type_symbol is Interface) {
236 write_indent ();
237 buffer.append_printf ("<implements name=\"%s\"/>\n", gi_type_name (object_type.type_symbol));
241 write_annotations (cl);
243 write_indent ();
244 buffer.append_printf ("<field name=\"parent_instance\">\n");
245 indent++;
246 write_indent ();
247 buffer.append_printf ("<type name=\"%s\" c:type=\"%s\"/>\n", gi_type_name (cl.base_class), cl.base_class.get_cname ());
248 indent--;
249 write_indent ();
250 buffer.append_printf("</field>\n");
252 write_indent ();
253 buffer.append_printf ("<field name=\"priv\">\n");
254 indent++;
255 write_indent ();
256 buffer.append_printf ("<type name=\"%sPrivate\" c:type=\"%sPrivate*\"/>\n", cl.name, cl.get_cname ());
257 indent--;
258 write_indent ();
259 buffer.append_printf("</field>\n");
261 hierarchy.insert (0, cl);
262 cl.accept_children (this);
263 hierarchy.remove_at (0);
265 indent--;
266 write_indent ();
267 buffer.append_printf ("</class>\n");
269 write_indent ();
270 buffer.append_printf ("<record name=\"%s\"", gtype_struct_name);
271 write_ctype_attributes (cl, "Class");
272 buffer.append_printf (" glib:is-gtype-struct-for=\"%s\"", cl.name);
273 buffer.append_printf (">\n");
274 indent++;
276 write_indent ();
277 buffer.append_printf ("<field name=\"parent_class\">\n");
278 indent++;
279 write_indent ();
280 buffer.append_printf ("<type name=\"%sClass\" c:type=\"%sClass\"/>\n", gi_type_name (cl.base_class), cl.base_class.get_cname ());
281 indent--;
282 write_indent ();
283 buffer.append_printf ("</field>\n");
285 foreach (Method m in cl.get_methods ()) {
286 if (m.is_abstract || m.is_virtual) {
287 write_indent ();
288 buffer.append_printf("<field name=\"%s\">\n", m.name);
289 indent++;
290 write_signature(m, "callback", true);
291 indent--;
292 write_indent ();
293 buffer.append_printf ("</field>\n");
297 foreach (Signal sig in cl.get_signals ()) {
298 if (sig.default_handler != null) {
299 write_indent ();
300 buffer.append_printf ("<field name=\"%s\">\n", sig.name);
301 indent++;
302 write_signature (sig.default_handler, "callback", true);
303 indent--;
304 write_indent ();
305 buffer.append_printf ("</field>\n");
310 indent--;
311 write_indent ();
312 buffer.append_printf ("</record>\n");
314 write_indent ();
315 buffer.append_printf ("<record name=\"%sPrivate\" c:type=\"%sPrivate\" disguised=\"1\"/>\n", cl.name, cl.get_cname ());
316 } else {
317 write_indent ();
318 buffer.append_printf ("<record name=\"%s\"", get_gir_name (cl));
319 write_symbol_attributes (cl);
320 buffer.append_printf (">\n");
321 indent++;
323 write_annotations (cl);
325 hierarchy.insert (0, cl);
326 cl.accept_children (this);
327 hierarchy.remove_at (0);
329 indent--;
330 write_indent ();
331 buffer.append_printf ("</record>\n");
334 visit_deferred ();
337 public override void visit_struct (Struct st) {
338 if (st.external_package) {
339 return;
342 if (!check_accessibility (st)) {
343 return;
346 if (!(hierarchy[0] is Namespace)) {
347 deferred.add (st);
348 return;
351 write_indent ();
352 buffer.append_printf ("<record name=\"%s\"", get_gir_name (st));
353 write_symbol_attributes (st);
354 buffer.append_printf (">\n");
355 indent++;
357 write_annotations (st);
359 hierarchy.insert (0, st);
360 st.accept_children (this);
361 hierarchy.remove_at (0);
363 indent--;
364 write_indent ();
365 buffer.append_printf ("</record>\n");
367 visit_deferred ();
370 public override void visit_interface (Interface iface) {
371 if (iface.external_package) {
372 return;
375 if (!check_accessibility (iface)) {
376 return;
379 if (!(hierarchy[0] is Namespace)) {
380 deferred.add (iface);
381 return;
384 string gtype_struct_name = iface.name + "Iface";
386 write_indent ();
387 buffer.append_printf ("<interface name=\"%s\"", get_gir_name (iface));
388 write_gtype_attributes (iface);
389 buffer.append_printf (" glib:type-struct=\"%s\"", gtype_struct_name);
390 write_symbol_attributes (iface);
391 buffer.append_printf (">\n");
392 indent++;
394 // write prerequisites
395 if (iface.get_prerequisites ().size > 0) {
396 foreach (DataType base_type in iface.get_prerequisites ()) {
397 write_indent ();
398 buffer.append_printf ("<prerequisite name=\"%s\"/>\n", gi_type_name (((ObjectType) base_type).type_symbol));
402 write_annotations (iface);
404 hierarchy.insert (0, iface);
405 iface.accept_children (this);
406 hierarchy.remove_at (0);
408 indent--;
409 write_indent ();
410 buffer.append_printf ("</interface>\n");
412 write_indent ();
413 buffer.append_printf ("<record name=\"%s\"", gtype_struct_name);
414 write_ctype_attributes (iface, "Iface");
415 buffer.append_printf (" glib:is-gtype-struct-for=\"%s\"", iface.name);
416 buffer.append_printf (">\n");
417 indent++;
419 foreach (Method m in iface.get_methods ()) {
420 if (m.is_abstract || m.is_virtual) {
421 write_signature(m, "callback", true);
425 indent--;
426 write_indent ();
427 buffer.append_printf ("</record>\n");
429 visit_deferred ();
432 private void visit_deferred () {
433 var nodes = this.deferred;
434 this.deferred = new Vala.ArrayList<Vala.CodeNode>();
436 foreach (var node in nodes) {
437 node.accept (this);
441 private string? get_gir_name (Symbol symbol) {
442 string? gir_name = null;
443 var h0 = hierarchy[0];
445 for (Symbol? cur_sym = symbol ; cur_sym != null ; cur_sym = cur_sym.parent_symbol) {
446 if (cur_sym == h0) {
447 break;
450 gir_name = cur_sym.gir_name.concat (gir_name);
453 return gir_name;
456 public override void visit_enum (Enum en) {
457 if (en.external_package) {
458 return;
461 if (!check_accessibility (en)) {
462 return;
465 if (!(hierarchy[0] is Namespace)) {
466 deferred.add (en);
467 return;
470 string element_name = (en.is_flags) ? "bitfield" : "enumeration";
472 write_indent ();
473 buffer.append_printf ("<%s name=\"%s\"", element_name, get_gir_name (en));
474 write_gtype_attributes (en);
475 write_symbol_attributes (en);
476 buffer.append_printf (">\n");
477 indent++;
479 write_annotations (en);
481 enum_value = 0;
482 hierarchy.insert (0, en);
483 en.accept_children (this);
484 hierarchy.remove_at (0);
486 indent--;
487 write_indent ();
488 buffer.append_printf ("</%s>\n", element_name);
490 visit_deferred ();
493 private int enum_value;
495 public override void visit_enum_value (EnumValue ev) {
496 write_indent ();
497 var en = (Enum) hierarchy[0];
498 buffer.append_printf ("<member name=\"%s\" c:identifier=\"%s\"", ev.name.down (), ev.get_cname ());
499 if (ev.value != null) {
500 string value = literal_expression_to_value_string (ev.value);
501 buffer.append_printf (" value=\"%s\"", value);
502 } else {
503 if (en.is_flags) {
504 buffer.append_printf (" value=\"%d\"", 1 << enum_value++);
505 } else {
506 buffer.append_printf (" value=\"%d\"", enum_value++);
509 write_symbol_attributes (ev);
510 buffer.append_printf ("/>\n");
513 public override void visit_error_domain (ErrorDomain edomain) {
514 if (edomain.external_package) {
515 return;
518 if (!check_accessibility (edomain)) {
519 return;
522 write_indent ();
523 buffer.append_printf ("<errordomain name=\"%s\"", edomain.name);
524 buffer.append_printf (" get-quark=\"%squark\"", edomain.get_lower_case_cprefix ());
525 buffer.append_printf (" codes=\"%s\"", edomain.name);
526 write_symbol_attributes (edomain);
527 buffer.append_printf (">\n");
529 write_annotations (edomain);
531 buffer.append_printf ("</errordomain>\n");
533 write_indent ();
534 buffer.append_printf ("<enumeration name=\"%s\"", edomain.name);
535 write_ctype_attributes (edomain);
536 buffer.append_printf (">\n");
537 indent++;
539 enum_value = 0;
540 hierarchy.insert (0, edomain);
541 edomain.accept_children (this);
542 hierarchy.remove_at (0);
544 indent--;
545 write_indent ();
546 buffer.append_printf ("</enumeration>\n");
548 visit_deferred ();
551 public override void visit_error_code (ErrorCode ecode) {
552 write_indent ();
553 buffer.append_printf ("<member name=\"%s\" c:identifier=\"%s\"", ecode.name.down (), ecode.get_cname ());
554 if (ecode.value != null) {
555 string value = literal_expression_to_value_string (ecode.value);
556 buffer.append_printf (" value=\"%s\"", value);
557 } else {
558 buffer.append_printf (" value=\"%d\"", enum_value++);
560 write_symbol_attributes (ecode);
561 buffer.append_printf ("/>\n");
564 public override void visit_constant (Constant c) {
565 if (c.external_package) {
566 return;
569 if (!check_accessibility (c)) {
570 return;
573 //TODO Add better constant evaluation
574 var initializer = c.value;
575 string value = literal_expression_to_value_string (initializer);
577 write_indent ();
578 buffer.append_printf ("<constant name=\"%s\" c:identifier=\"%s\"", c.name, c.get_cname ());
579 buffer.append_printf (" value=\"%s\"", value);
580 write_symbol_attributes (c);
581 buffer.append_printf (">\n");
582 indent++;
584 write_type (initializer.value_type);
586 indent--;
587 write_indent ();
588 buffer.append_printf ("</constant>\n");
591 public override void visit_field (Field f) {
592 if (f.external_package) {
593 return;
596 if (!check_accessibility (f)) {
597 return;
600 write_indent ();
601 buffer.append_printf ("<field name=\"%s\"", f.get_cname ());
602 if (f.variable_type.nullable) {
603 buffer.append_printf (" allow-none=\"1\"");
605 write_symbol_attributes (f);
606 buffer.append_printf (">\n");
607 indent++;
609 write_annotations (f);
611 write_type (f.variable_type);
613 indent--;
614 write_indent ();
615 buffer.append_printf ("</field>\n");
618 private void write_implicit_params (DataType type, ref int index, bool has_array_length, string name, ParameterDirection direction) {
619 if (type is ArrayType && has_array_length) {
620 var int_type = new IntegerType (CodeContext.get ().root.scope.lookup ("int") as Struct);
621 write_param_or_return (int_type, true, ref index, has_array_length, "%s_length1".printf (name), direction);
622 } else if (type is DelegateType) {
623 var data_type = new PointerType (new VoidType ());
624 write_param_or_return (data_type, true, ref index, false, "%s_target".printf (name), direction);
625 if (type.value_owned) {
626 var notify_type = new DelegateType (CodeContext.get ().root.scope.lookup ("GLib").scope.lookup ("DestroyNotify") as Delegate);
627 write_param_or_return (notify_type, true, ref index, false, "%s_target_destroy_notify".printf (name), direction);
632 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) {
633 int last_index = 0;
634 bool ret_is_struct = return_type != null && return_type.is_real_non_null_struct_type ();
635 if (params.size != 0 || instance_type != null || (return_type is ArrayType && return_array_length) || (return_type is DelegateType) || ret_is_struct) {
636 write_indent ();
637 buffer.append_printf ("<parameters>\n");
638 indent++;
639 int index = 0;
641 if (instance_type != null) {
642 write_param_or_return (instance_type, true, ref index, false, "self");
645 foreach (Parameter param in params) {
646 write_param_or_return (param.variable_type, true, ref index, !param.no_array_length, param.name, param.direction);
648 write_implicit_params (param.variable_type, ref index, !param.no_array_length, param.name, param.direction);
651 if (ret_is_struct) {
652 // struct returns are converted to parameters
653 write_param_or_return (return_type, true, ref index, false, "result", ParameterDirection.OUT, constructor, true);
654 } else {
655 write_implicit_params (return_type, ref index, return_array_length, "result", ParameterDirection.OUT);
658 last_index = index - 1;
660 if (user_data) {
661 write_indent ();
662 buffer.append_printf ("<parameter name=\"user_data\" transfer-ownership=\"none\" closure=\"%d\">\n", index);
663 indent++;
664 write_indent ();
665 buffer.append_printf ("<type name=\"gpointer\" c:type=\"void*\"/>\n");
666 indent--;
667 write_indent ();
668 buffer.append_printf ("</parameter>\n");
671 indent--;
672 write_indent ();
673 buffer.append_printf ("</parameters>\n");
676 if (return_type != null && !ret_is_struct) {
677 write_param_or_return (return_type, false, ref last_index, return_array_length, null, ParameterDirection.IN, constructor);
678 } else if (ret_is_struct) {
679 write_param_or_return (new VoidType (), false, ref last_index, false, null, ParameterDirection.IN);
683 public override void visit_delegate (Delegate cb) {
684 if (cb.external_package) {
685 return;
688 if (!check_accessibility (cb)) {
689 return;
692 write_indent ();
693 buffer.append_printf ("<callback name=\"%s\"", cb.name);
694 buffer.append_printf (" c:type=\"%s\"", cb.get_cname ());
695 if (cb.tree_can_fail) {
696 buffer.append_printf (" throws=\"1\"");
698 write_symbol_attributes (cb);
699 buffer.append_printf (">\n");
700 indent++;
702 write_annotations (cb);
704 write_params_and_return (cb.get_parameters (), cb.return_type, !cb.no_array_length, false, null, cb.has_target);
706 indent--;
707 write_indent ();
708 buffer.append_printf ("</callback>\n");
711 public override void visit_method (Method m) {
712 if (m.external_package) {
713 return;
716 // don't write interface implementation unless it's an abstract or virtual method
717 if (!check_accessibility (m) || m.overrides || (m.base_interface_method != null && !m.is_abstract && !m.is_virtual)) {
718 return;
721 string tag_name = "method";
722 var parent = this.hierarchy.get (0);
723 if (parent is Enum) {
724 deferred.add (m);
725 return;
728 if (parent is Namespace || m.binding == MemberBinding.STATIC || parent != m.parent_symbol) {
729 tag_name = "function";
732 write_signature (m, tag_name);
734 if (m.is_abstract || m.is_virtual) {
735 write_signature (m, "virtual-method", false);
739 private void write_signature (Method m, string tag_name, bool instance = false) {
740 var parent = this.hierarchy.get (0);
741 string name;
742 if (m.parent_symbol != parent) {
743 instance = false;
744 name = m.get_cname ();
745 var parent_prefix = parent.get_lower_case_cprefix ();
746 if (name.has_prefix (parent_prefix)) {
747 name = name.substring (parent_prefix.length);
749 } else {
750 name = m.name;
753 if (m.coroutine) {
754 string finish_name = name;
755 if (finish_name.has_suffix ("_async")) {
756 finish_name = finish_name.substring (0, finish_name.length - "_async".length);
758 finish_name += "_finish";
759 do_write_signature (m, tag_name, instance, name, m.get_cname (), m.get_async_begin_parameters (), new VoidType (), false);
760 do_write_signature (m, tag_name, instance, finish_name, m.get_finish_cname (), m.get_async_end_parameters (), m.return_type, m.tree_can_fail);
761 } else {
762 do_write_signature (m, tag_name, instance, name, m.get_cname (), m.get_parameters (), m.return_type, m.tree_can_fail);
766 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) {
767 write_indent ();
768 buffer.append_printf ("<%s name=\"%s\"", tag_name, name);
769 if (tag_name == "virtual-method") {
770 buffer.append_printf (" invoker=\"%s\"", name);
771 } else if (tag_name == "callback") {
772 /* this is only used for vfuncs */
773 buffer.append_printf (" c:type=\"%s\"", name);
774 } else {
775 buffer.append_printf (" c:identifier=\"%s\"", cname);
777 if (can_fail) {
778 buffer.append_printf (" throws=\"1\"");
780 write_symbol_attributes (m);
781 buffer.append_printf (">\n");
782 indent++;
784 write_annotations (m);
786 DataType instance_type = null;
787 if (instance) {
788 instance_type = CCodeBaseModule.get_data_type_for_symbol ((TypeSymbol) m.parent_symbol);
791 write_params_and_return (params, return_type, !m.no_array_length, false, instance_type);
793 indent--;
794 write_indent ();
795 buffer.append_printf ("</%s>\n", tag_name);
798 public override void visit_creation_method (CreationMethod m) {
799 if (m.external_package) {
800 return;
803 if (!check_accessibility (m)) {
804 return;
807 write_indent ();
809 if (m.parent_symbol is Class && m == ((Class)m.parent_symbol).default_construction_method ||
810 m.parent_symbol is Struct && m == ((Struct)m.parent_symbol).default_construction_method) {
811 buffer.append_printf ("<constructor name=\"new\" c:identifier=\"%s\"", m.get_cname ());
812 } else {
813 buffer.append_printf ("<constructor name=\"%s\" c:identifier=\"%s\"", m.name, m.get_cname ());
816 if (m.tree_can_fail) {
817 buffer.append_printf (" throws=\"1\"");
819 buffer.append_printf (">\n");
820 indent++;
822 write_annotations (m);
825 var datatype = CCodeBaseModule.get_data_type_for_symbol ((TypeSymbol) m.parent_symbol);
826 write_params_and_return (m.get_parameters (), datatype, false, true);
828 indent--;
829 write_indent ();
830 buffer.append_printf ("</constructor>\n");
833 public override void visit_property (Property prop) {
834 if (!check_accessibility (prop) || prop.overrides || (prop.base_interface_property != null && !prop.is_abstract && !prop.is_virtual)) {
835 return;
838 write_indent ();
839 buffer.append_printf ("<property name=\"%s\"", prop.get_canonical_name ());
840 if (prop.get_accessor == null) {
841 buffer.append_printf (" readable=\"0\"");
843 if (prop.set_accessor != null) {
844 buffer.append_printf (" writable=\"1\"");
845 if (prop.set_accessor.construction) {
846 if (!prop.set_accessor.writable) {
847 buffer.append_printf (" construct-only=\"1\"");
848 } else {
849 buffer.append_printf (" construct=\"1\"");
853 write_symbol_attributes (prop);
854 buffer.append_printf (">\n");
855 indent++;
857 write_annotations (prop);
859 write_type (prop.property_type);
861 indent--;
862 write_indent ();
863 buffer.append_printf ("</property>\n");
866 public override void visit_signal (Signal sig) {
867 if (!check_accessibility (sig)) {
868 return;
871 write_indent ();
872 buffer.append_printf ("<glib:signal name=\"%s\"", sig.get_cname ());
873 write_symbol_attributes (sig);
874 buffer.append_printf (">\n");
875 indent++;
877 write_annotations (sig);
879 write_params_and_return (sig.get_parameters (), sig.return_type, false);
881 indent--;
882 write_indent ();
883 buffer.append_printf ("</glib:signal>\n");
886 private void write_indent () {
887 int i;
889 for (i = 0; i < indent; i++) {
890 buffer.append_c ('\t');
894 private void write_indent_stream () {
895 int i;
897 for (i = 0; i < indent; i++) {
898 stream.putc ('\t');
903 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) {
904 write_indent ();
905 string tag = is_parameter ? "parameter" : "return-value";
906 buffer.append_printf ("<%s", tag);
907 if (name != null) {
908 buffer.append_printf (" name=\"%s\"", name);
910 if (direction == ParameterDirection.REF) {
911 buffer.append_printf (" direction=\"inout\"");
912 } else if (direction == ParameterDirection.OUT) {
913 buffer.append_printf (" direction=\"out\"");
916 DelegateType delegate_type = type as DelegateType;
918 if ((type.value_owned && delegate_type == null) || constructor) {
919 buffer.append_printf (" transfer-ownership=\"full\"");
920 } else {
921 buffer.append_printf (" transfer-ownership=\"none\"");
923 if (caller_allocates) {
924 buffer.append_printf (" caller-allocates=\"1\"");
926 if (type.nullable) {
927 buffer.append_printf (" allow-none=\"1\"");
930 if (delegate_type != null && delegate_type.delegate_symbol.has_target) {
931 int closure_index = is_parameter ?
932 index + 1 : (type.value_owned ? index - 1 : index);
933 buffer.append_printf (" closure=\"%i\"", closure_index);
934 if (type.value_owned) {
935 buffer.append_printf (" destroy=\"%i\"", closure_index + 1);
939 buffer.append_printf (">\n");
940 indent++;
942 int length_param_index = -1;
943 if (has_array_length) {
944 length_param_index = is_parameter ? index + 1 : index;
946 write_type (type, length_param_index);
948 indent--;
949 write_indent ();
950 buffer.append_printf ("</%s>\n", tag);
951 index++;
954 private void write_ctype_attributes (TypeSymbol symbol, string suffix = "") {
955 buffer.append_printf (" c:type=\"%s%s\"", symbol.get_cname (), suffix);
958 private void write_gtype_attributes (TypeSymbol symbol) {
959 write_ctype_attributes(symbol);
960 buffer.append_printf (" glib:type-name=\"%s\"", symbol.get_cname ());
961 buffer.append_printf (" glib:get-type=\"%sget_type\"", symbol.get_lower_case_cprefix ());
964 private void write_type (DataType type, int index = -1) {
965 if (type is ArrayType) {
966 var array_type = (ArrayType) type;
968 write_indent ();
969 buffer.append_printf ("<array");
970 if (array_type.fixed_length) {
971 buffer.append_printf (" fixed-size=\"%i\"", array_type.length);
972 } else if (index != -1) {
973 buffer.append_printf (" length=\"%i\"", index);
975 buffer.append_printf (">\n");
976 indent++;
978 write_type (array_type.element_type);
980 indent--;
981 write_indent ();
982 buffer.append_printf ("</array>\n");
983 } else if (type is VoidType) {
984 write_indent ();
985 buffer.append_printf ("<type name=\"none\"/>\n");
986 } else if (type is PointerType) {
987 write_indent ();
988 buffer.append_printf ("<type name=\"gpointer\" c:type=\"%s\"/>\n", type.get_cname ());
989 } else if (type.data_type != null) {
990 write_indent ();
991 buffer.append_printf ("<type name=\"%s\" c:type=\"%s\"", gi_type_name (type.data_type), type.get_cname ());
993 List<DataType> type_arguments = type.get_type_arguments ();
994 if (type_arguments.size == 0) {
995 buffer.append_printf ("/>\n");
996 } else {
997 buffer.append_printf (">\n");
998 indent++;
1000 foreach (DataType type_argument in type_arguments) {
1001 write_type (type_argument);
1004 indent--;
1005 write_indent ();
1006 buffer.append_printf ("</type>\n");
1008 } else if (type is DelegateType) {
1009 var deleg_type = (DelegateType) type;
1010 write_indent ();
1011 buffer.append_printf ("<type name=\"%s\" c:type=\"%s\"/>\n", gi_type_name (deleg_type.delegate_symbol), type.get_cname ());
1012 } else if (type is GenericType) {
1013 // generic type parameters not supported in GIR
1014 write_indent ();
1015 buffer.append ("<type name=\"gpointer\" c:type=\"gpointer\"/>\n");
1016 } else {
1017 write_indent ();
1018 buffer.append_printf ("<type name=\"%s\"/>\n", type.to_string ());
1022 private void write_annotations (CodeNode node) {
1023 foreach (Attribute attr in node.attributes) {
1024 string name = camel_case_to_canonical (attr.name);
1025 foreach (string arg_name in attr.args.get_keys ()) {
1026 string value = attr.args.get (arg_name);
1027 if (value.has_prefix ("\"")) {
1028 // eval string
1029 value = attr.get_string (arg_name);
1032 write_indent ();
1033 buffer.append_printf ("<annotation key=\"%s.%s\" value=\"%s\"/>\n",
1034 name, camel_case_to_canonical (arg_name), value);
1039 private string gi_type_name (TypeSymbol type_symbol) {
1040 Symbol parent = type_symbol.parent_symbol;
1041 if (parent is Namespace) {
1042 Namespace ns = parent as Namespace;
1043 if (ns.gir_name != null) {
1044 if (type_symbol.source_reference.file.gir_namespace != null) {
1045 GIRNamespace external = GIRNamespace (type_symbol.source_reference.file.gir_namespace, type_symbol.source_reference.file.gir_version);
1046 if (!externals.contains (external)) {
1047 externals.add (external);
1049 return "%s.%s".printf (type_symbol.source_reference.file.gir_namespace, type_symbol.gir_name);
1050 } else {
1051 unannotated_namespaces.add(ns);
1056 return type_symbol.get_full_gir_name();
1059 private string? literal_expression_to_value_string (Expression literal) {
1060 if (literal is StringLiteral) {
1061 var lit = literal as StringLiteral;
1062 if (lit != null) {
1063 return Markup.escape_text (lit.eval ());
1065 } else if (literal is CharacterLiteral) {
1066 return "%c".printf ((char) ((CharacterLiteral) literal).get_char ());
1067 } else if (literal is BooleanLiteral) {
1068 return ((BooleanLiteral) literal).value ? "true" : "false";
1069 } else if (literal is RealLiteral) {
1070 return ((RealLiteral) literal).value;
1071 } else if (literal is IntegerLiteral) {
1072 return ((IntegerLiteral) literal).value;
1073 } else if (literal is UnaryExpression) {
1074 var unary = (UnaryExpression) literal;
1075 if (unary.operator == UnaryOperator.MINUS) {
1076 if (unary.inner is RealLiteral) {
1077 return "-" + ((RealLiteral) unary.inner).value;
1078 } else if (unary.inner is IntegerLiteral) {
1079 return "-" + ((IntegerLiteral) unary.inner).value;
1083 return null;
1086 private string camel_case_to_canonical (string name) {
1087 string[] parts = Symbol.camel_case_to_lower_case (name).split ("_");
1088 return string.joinv ("-", parts);
1091 private bool check_accessibility (Symbol sym) {
1092 if (sym.access == SymbolAccessibility.PUBLIC ||
1093 sym.access == SymbolAccessibility.PROTECTED) {
1094 return true;
1097 return false;