1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2 * GObject introspection: IDL generator
4 * Copyright (C) 2005 Matthias Clasen
5 * Copyright (C) 2008,2009 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
28 #include <glib-object.h>
29 #include <glib/gstdio.h>
31 #include "girwriter.h"
32 #include "girepository.h"
33 #include "gitypelib-internal.h"
43 guint has_children : 1;
47 xml_element_new (const char *name)
51 elem = g_slice_new (XmlElement);
52 elem->name = g_strdup (name);
53 elem->has_children = FALSE;
58 xml_element_free (XmlElement *elem)
61 g_slice_free (XmlElement, elem);
65 xml_printf (Xml *xml, const char *fmt, ...)
71 s = g_markup_vprintf_escaped (fmt, ap);
78 xml_start_element (Xml *xml, const char *element_name)
80 XmlElement *parent = NULL;
84 parent = xml->stack->data;
86 if (!parent->has_children)
87 xml_printf (xml, ">\n");
89 parent->has_children = TRUE;
92 xml_printf (xml, "%*s<%s", g_slist_length(xml->stack)*2, "", element_name);
94 xml->stack = g_slist_prepend (xml->stack, xml_element_new (element_name));
98 xml_end_element (Xml *xml, const char *name)
102 g_assert (xml->stack != NULL);
104 elem = xml->stack->data;
105 xml->stack = g_slist_delete_link (xml->stack, xml->stack);
108 g_assert_cmpstr (name, ==, elem->name);
110 if (elem->has_children)
111 xml_printf (xml, "%*s</%s>\n", g_slist_length (xml->stack)*2, "", elem->name);
113 xml_printf (xml, "/>\n");
115 xml_element_free (elem);
119 xml_end_element_unchecked (Xml *xml)
121 xml_end_element (xml, NULL);
125 xml_open (FILE *file)
129 xml = g_slice_new (Xml);
139 g_assert (xml->stack == NULL);
140 if (xml->file != NULL)
143 if (xml->file != stdout)
153 g_slice_free (Xml, xml);
158 check_unresolved (GIBaseInfo *info)
160 if (g_base_info_get_type (info) != GI_INFO_TYPE_UNRESOLVED)
163 g_critical ("Found unresolved type '%s' '%s'\n",
164 g_base_info_get_name (info), g_base_info_get_namespace (info));
168 write_type_name (const gchar *namespace,
172 if (strcmp (namespace, g_base_info_get_namespace (info)) != 0)
173 xml_printf (file, "%s.", g_base_info_get_namespace (info));
175 xml_printf (file, "%s", g_base_info_get_name (info));
179 write_type_name_attribute (const gchar *namespace,
181 const char *attr_name,
184 xml_printf (file, " %s=\"", attr_name);
185 write_type_name (namespace, info, file);
186 xml_printf (file, "\"");
190 write_ownership_transfer (GITransfer transfer,
195 case GI_TRANSFER_NOTHING:
196 xml_printf (file, " transfer-ownership=\"none\"");
198 case GI_TRANSFER_CONTAINER:
199 xml_printf (file, " transfer-ownership=\"container\"");
201 case GI_TRANSFER_EVERYTHING:
202 xml_printf (file, " transfer-ownership=\"full\"");
205 g_assert_not_reached ();
210 write_type_info (const gchar *namespace,
218 check_unresolved ((GIBaseInfo*)info);
220 tag = g_type_info_get_tag (info);
221 is_pointer = g_type_info_is_pointer (info);
223 if (tag == GI_TYPE_TAG_VOID)
225 xml_start_element (file, "type");
227 xml_printf (file, " name=\"%s\"", is_pointer ? "any" : "none");
229 xml_end_element (file, "type");
231 else if (G_TYPE_TAG_IS_BASIC (tag))
233 xml_start_element (file, "type");
234 xml_printf (file, " name=\"%s\"", g_type_tag_to_string (tag));
235 xml_end_element (file, "type");
237 else if (tag == GI_TYPE_TAG_ARRAY)
242 xml_start_element (file, "array");
244 switch (g_type_info_get_array_type (info)) {
245 case GI_ARRAY_TYPE_C:
247 case GI_ARRAY_TYPE_ARRAY:
250 case GI_ARRAY_TYPE_PTR_ARRAY:
251 name = "GLib.PtrArray";
253 case GI_ARRAY_TYPE_BYTE_ARRAY:
254 name = "GLib.ByteArray";
261 xml_printf (file, " name=\"%s\"", name);
263 type = g_type_info_get_param_type (info, 0);
265 length = g_type_info_get_array_length (info);
267 xml_printf (file, " length=\"%d\"", length);
269 size = g_type_info_get_array_fixed_size (info);
271 xml_printf (file, " fixed-size=\"%d\"", size);
273 if (g_type_info_is_zero_terminated (info))
274 xml_printf (file, " zero-terminated=\"1\"");
276 write_type_info (namespace, type, file);
278 g_base_info_unref ((GIBaseInfo *)type);
280 xml_end_element (file, "array");
282 else if (tag == GI_TYPE_TAG_INTERFACE)
284 GIBaseInfo *iface = g_type_info_get_interface (info);
285 xml_start_element (file, "type");
286 write_type_name_attribute (namespace, iface, "name", file);
287 xml_end_element (file, "type");
288 g_base_info_unref (iface);
290 else if (tag == GI_TYPE_TAG_GLIST)
292 xml_start_element (file, "type");
293 xml_printf (file, " name=\"GLib.List\"");
294 type = g_type_info_get_param_type (info, 0);
297 write_type_info (namespace, type, file);
298 g_base_info_unref ((GIBaseInfo *)type);
300 xml_end_element (file, "type");
302 else if (tag == GI_TYPE_TAG_GSLIST)
304 xml_start_element (file, "type");
305 xml_printf (file, " name=\"GLib.SList\"");
306 type = g_type_info_get_param_type (info, 0);
309 write_type_info (namespace, type, file);
310 g_base_info_unref ((GIBaseInfo *)type);
312 xml_end_element (file, "type");
314 else if (tag == GI_TYPE_TAG_GHASH)
316 xml_start_element (file, "type");
317 xml_printf (file, " name=\"GLib.HashTable\"");
318 type = g_type_info_get_param_type (info, 0);
321 write_type_info (namespace, type, file);
322 g_base_info_unref ((GIBaseInfo *)type);
323 type = g_type_info_get_param_type (info, 1);
324 write_type_info (namespace, type, file);
325 g_base_info_unref ((GIBaseInfo *)type);
327 xml_end_element (file, "type");
329 else if (tag == GI_TYPE_TAG_ERROR)
331 xml_start_element (file, "type");
332 xml_printf (file, " name=\"GLib.Error\"");
333 xml_end_element (file, "type");
337 g_printerr ("Unhandled type tag %d\n", tag);
338 g_assert_not_reached ();
343 write_attributes (Xml *file,
346 GIAttributeIter iter = { 0, };
349 while (g_base_info_iterate_attributes (info, &iter, &name, &value))
351 xml_start_element (file, "attribute");
352 xml_printf (file, " name=\"%s\" value=\"%s\"", name, value);
353 xml_end_element (file, "attribute");
358 write_return_value_attributes (Xml *file,
359 GICallableInfo *info)
361 GIAttributeIter iter = { 0, };
364 while (g_callable_info_iterate_return_attributes (info, &iter, &name, &value))
366 xml_start_element (file, "attribute");
367 xml_printf (file, " name=\"%s\" value=\"%s\"", name, value);
368 xml_end_element (file, "attribute");
373 write_constant_value (const gchar *namespace,
375 GIArgument *argument,
379 write_callback_info (const gchar *namespace,
380 GICallbackInfo *info,
384 write_field_info (const gchar *namespace,
386 GIConstantInfo *branch,
390 GIFieldInfoFlags flags;
394 GIBaseInfo *interface;
397 name = g_base_info_get_name ((GIBaseInfo *)info);
398 flags = g_field_info_get_flags (info);
399 size = g_field_info_get_size (info);
400 offset = g_field_info_get_offset (info);
402 xml_start_element (file, "field");
403 xml_printf (file, " name=\"%s\"", name);
405 /* Fields are assumed to be read-only
406 * (see also girwriter.py and girparser.c)
408 if (!(flags & GI_FIELD_IS_READABLE))
409 xml_printf (file, " readable=\"0\"");
410 if (flags & GI_FIELD_IS_WRITABLE)
411 xml_printf (file, " writable=\"1\"");
414 xml_printf (file, " bits=\"%d\"", size);
416 write_attributes (file, (GIBaseInfo*) info);
418 type = g_field_info_get_type (info);
422 xml_printf (file, " branch=\"");
423 type = g_constant_info_get_type (branch);
424 g_constant_info_get_value (branch, &value);
425 write_constant_value (namespace, type, &value, file);
426 xml_printf (file, "\"");
432 xml_printf (file, "offset=\"%d\"", offset);
435 interface = g_type_info_get_interface (type);
436 if (interface && g_base_info_get_type(interface) == GI_INFO_TYPE_CALLBACK)
437 write_callback_info (namespace, (GICallbackInfo *)interface, file);
439 write_type_info (namespace, type, file);
442 g_base_info_unref (interface);
444 g_base_info_unref ((GIBaseInfo *)type);
446 xml_end_element (file, "field");
450 write_callable_info (const gchar *namespace,
451 GICallableInfo *info,
457 write_attributes (file, (GIBaseInfo*) info);
459 type = g_callable_info_get_return_type (info);
461 xml_start_element (file, "return-value");
463 write_ownership_transfer (g_callable_info_get_caller_owns (info), file);
465 if (g_callable_info_may_return_null (info))
466 xml_printf (file, " allow-none=\"1\"");
468 if (g_callable_info_skip_return (info))
469 xml_printf (file, " skip=\"1\"");
471 write_return_value_attributes (file, info);
473 write_type_info (namespace, type, file);
475 xml_end_element (file, "return-value");
477 if (g_callable_info_get_n_args (info) <= 0)
480 xml_start_element (file, "parameters");
481 for (i = 0; i < g_callable_info_get_n_args (info); i++)
483 GIArgInfo *arg = g_callable_info_get_arg (info, i);
485 xml_start_element (file, "parameter");
486 xml_printf (file, " name=\"%s\"",
487 g_base_info_get_name ((GIBaseInfo *) arg));
489 write_ownership_transfer (g_arg_info_get_ownership_transfer (arg), file);
491 switch (g_arg_info_get_direction (arg))
493 case GI_DIRECTION_IN:
495 case GI_DIRECTION_OUT:
496 xml_printf (file, " direction=\"out\" caller-allocates=\"%s\"",
497 g_arg_info_is_caller_allocates (arg) ? "1" : "0");
499 case GI_DIRECTION_INOUT:
500 xml_printf (file, " direction=\"inout\"");
504 if (g_arg_info_may_be_null (arg))
505 xml_printf (file, " allow-none=\"1\"");
507 if (g_arg_info_is_return_value (arg))
508 xml_printf (file, " retval=\"1\"");
510 if (g_arg_info_is_optional (arg))
511 xml_printf (file, " optional=\"1\"");
513 switch (g_arg_info_get_scope (arg))
515 case GI_SCOPE_TYPE_INVALID:
517 case GI_SCOPE_TYPE_CALL:
518 xml_printf (file, " scope=\"call\"");
520 case GI_SCOPE_TYPE_ASYNC:
521 xml_printf (file, " scope=\"async\"");
523 case GI_SCOPE_TYPE_NOTIFIED:
524 xml_printf (file, " scope=\"notified\"");
528 if (g_arg_info_get_closure (arg) >= 0)
529 xml_printf (file, " closure=\"%d\"", g_arg_info_get_closure (arg));
531 if (g_arg_info_get_destroy (arg) >= 0)
532 xml_printf (file, " destroy=\"%d\"", g_arg_info_get_destroy (arg));
534 if (g_arg_info_is_skip (arg))
535 xml_printf (file, " skip=\"1\"");
537 write_attributes (file, (GIBaseInfo*) arg);
539 type = g_arg_info_get_type (arg);
540 write_type_info (namespace, type, file);
542 xml_end_element (file, "parameter");
544 g_base_info_unref ((GIBaseInfo *)arg);
547 xml_end_element (file, "parameters");
548 g_base_info_unref ((GIBaseInfo *)type);
552 write_function_info (const gchar *namespace,
553 GIFunctionInfo *info,
556 GIFunctionInfoFlags flags;
563 flags = g_function_info_get_flags (info);
564 name = g_base_info_get_name ((GIBaseInfo *)info);
565 symbol = g_function_info_get_symbol (info);
566 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
567 throws = flags & GI_FUNCTION_THROWS;
569 if (flags & GI_FUNCTION_IS_CONSTRUCTOR)
571 else if (flags & GI_FUNCTION_IS_METHOD)
576 xml_start_element (file, tag);
577 xml_printf (file, " name=\"%s\" c:identifier=\"%s\"",
580 if (flags & GI_FUNCTION_IS_SETTER)
581 xml_printf (file, " type=\"setter\"");
582 else if (flags & GI_FUNCTION_IS_GETTER)
583 xml_printf (file, " type=\"getter\"");
586 xml_printf (file, " deprecated=\"1\"");
589 xml_printf (file, " throws=\"1\"");
591 write_callable_info (namespace, (GICallableInfo*)info, file);
592 xml_end_element (file, tag);
596 write_callback_info (const gchar *namespace,
597 GICallbackInfo *info,
603 name = g_base_info_get_name ((GIBaseInfo *)info);
604 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
606 xml_start_element (file, "callback");
607 xml_printf (file, " name=\"%s\"", name);
610 xml_printf (file, " deprecated=\"1\"");
612 write_callable_info (namespace, (GICallableInfo*)info, file);
613 xml_end_element (file, "callback");
617 write_struct_info (const gchar *namespace,
622 const gchar *type_name;
623 const gchar *type_init;
625 gboolean is_gtype_struct;
631 name = g_base_info_get_name ((GIBaseInfo *)info);
632 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
634 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
635 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
637 if (g_base_info_get_type ((GIBaseInfo *)info) == GI_INFO_TYPE_BOXED)
639 xml_start_element (file, "glib:boxed");
640 xml_printf (file, " glib:name=\"%s\"", name);
644 xml_start_element (file, "record");
645 xml_printf (file, " name=\"%s\"", name);
648 if (type_name != NULL)
649 xml_printf (file, " glib:type-name=\"%s\" glib:get-type=\"%s\"", type_name, type_init);
652 xml_printf (file, " deprecated=\"1\"");
654 is_gtype_struct = g_struct_info_is_gtype_struct (info);
656 xml_printf (file, " glib:is-gtype-struct=\"1\"");
658 write_attributes (file, (GIBaseInfo*) info);
660 size = g_struct_info_get_size (info);
661 if (file->show_all && size >= 0)
662 xml_printf (file, " size=\"%d\"", size);
664 foreign = g_struct_info_is_foreign (info);
666 xml_printf (file, " foreign=\"1\"");
668 n_elts = g_struct_info_get_n_fields (info) + g_struct_info_get_n_methods (info);
671 for (i = 0; i < g_struct_info_get_n_fields (info); i++)
673 GIFieldInfo *field = g_struct_info_get_field (info, i);
674 write_field_info (namespace, field, NULL, file);
675 g_base_info_unref ((GIBaseInfo *)field);
678 for (i = 0; i < g_struct_info_get_n_methods (info); i++)
680 GIFunctionInfo *function = g_struct_info_get_method (info, i);
681 write_function_info (namespace, function, file);
682 g_base_info_unref ((GIBaseInfo *)function);
687 xml_end_element_unchecked (file);
691 write_value_info (const gchar *namespace,
700 name = g_base_info_get_name ((GIBaseInfo *)info);
701 value = g_value_info_get_value (info);
702 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
704 xml_start_element (file, "member");
705 value_str = g_strdup_printf ("%" G_GINT64_FORMAT, value);
706 xml_printf (file, " name=\"%s\" value=\"%s\"", name, value_str);
710 xml_printf (file, " deprecated=\"1\"");
712 write_attributes (file, (GIBaseInfo*) info);
714 xml_end_element (file, "member");
718 write_constant_value (const gchar *namespace,
723 switch (g_type_info_get_tag (type))
725 case GI_TYPE_TAG_BOOLEAN:
726 xml_printf (file, "%d", value->v_boolean);
728 case GI_TYPE_TAG_INT8:
729 xml_printf (file, "%d", value->v_int8);
731 case GI_TYPE_TAG_UINT8:
732 xml_printf (file, "%d", value->v_uint8);
734 case GI_TYPE_TAG_INT16:
735 xml_printf (file, "%" G_GINT16_FORMAT, value->v_int16);
737 case GI_TYPE_TAG_UINT16:
738 xml_printf (file, "%" G_GUINT16_FORMAT, value->v_uint16);
740 case GI_TYPE_TAG_INT32:
741 xml_printf (file, "%" G_GINT32_FORMAT, value->v_int32);
743 case GI_TYPE_TAG_UINT32:
744 xml_printf (file, "%" G_GUINT32_FORMAT, value->v_uint32);
746 case GI_TYPE_TAG_INT64:
747 xml_printf (file, "%" G_GINT64_FORMAT, value->v_int64);
749 case GI_TYPE_TAG_UINT64:
750 xml_printf (file, "%" G_GUINT64_FORMAT, value->v_uint64);
752 case GI_TYPE_TAG_FLOAT:
753 xml_printf (file, "%f", value->v_float);
755 case GI_TYPE_TAG_DOUBLE:
756 xml_printf (file, "%f", value->v_double);
758 case GI_TYPE_TAG_UTF8:
759 case GI_TYPE_TAG_FILENAME:
760 xml_printf (file, "%s", value->v_string);
763 g_assert_not_reached ();
768 write_constant_info (const gchar *namespace,
769 GIConstantInfo *info,
777 name = g_base_info_get_name ((GIBaseInfo *)info);
778 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
780 xml_start_element (file, "constant");
781 xml_printf (file, " name=\"%s\"", name);
783 type = g_constant_info_get_type (info);
784 xml_printf (file, " value=\"");
786 g_constant_info_get_value (info, &value);
787 write_constant_value (namespace, type, &value, file);
788 xml_printf (file, "\"");
790 write_type_info (namespace, type, file);
792 write_attributes (file, (GIBaseInfo*) info);
794 xml_end_element (file, "constant");
796 g_base_info_unref ((GIBaseInfo *)type);
801 write_enum_info (const gchar *namespace,
806 const gchar *type_name;
807 const gchar *type_init;
808 const gchar *error_domain;
812 name = g_base_info_get_name ((GIBaseInfo *)info);
813 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
815 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
816 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
817 error_domain = g_enum_info_get_error_domain (info);
819 if (g_base_info_get_type ((GIBaseInfo *)info) == GI_INFO_TYPE_ENUM)
820 xml_start_element (file, "enumeration");
822 xml_start_element (file, "bitfield");
823 xml_printf (file, " name=\"%s\"", name);
826 xml_printf (file, " glib:type-name=\"%s\" glib:get-type=\"%s\"", type_name, type_init);
828 xml_printf (file, " glib:error-domain=\"%s\"", error_domain);
831 xml_printf (file, " deprecated=\"1\"");
833 write_attributes (file, (GIBaseInfo*) info);
835 for (i = 0; i < g_enum_info_get_n_values (info); i++)
837 GIValueInfo *value = g_enum_info_get_value (info, i);
838 write_value_info (namespace, value, file);
839 g_base_info_unref ((GIBaseInfo *)value);
842 xml_end_element_unchecked (file);
846 write_signal_info (const gchar *namespace,
854 name = g_base_info_get_name ((GIBaseInfo *)info);
855 flags = g_signal_info_get_flags (info);
856 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
858 xml_start_element (file, "glib:signal");
859 xml_printf (file, " name=\"%s\"", name);
862 xml_printf (file, " deprecated=\"1\"");
864 if (flags & G_SIGNAL_RUN_FIRST)
865 xml_printf (file, " when=\"FIRST\"");
866 else if (flags & G_SIGNAL_RUN_LAST)
867 xml_printf (file, " when=\"LAST\"");
868 else if (flags & G_SIGNAL_RUN_CLEANUP)
869 xml_printf (file, " when=\"CLEANUP\"");
871 if (flags & G_SIGNAL_NO_RECURSE)
872 xml_printf (file, " no-recurse=\"1\"");
874 if (flags & G_SIGNAL_DETAILED)
875 xml_printf (file, " detailed=\"1\"");
877 if (flags & G_SIGNAL_ACTION)
878 xml_printf (file, " action=\"1\"");
880 if (flags & G_SIGNAL_NO_HOOKS)
881 xml_printf (file, " no-hooks=\"1\"");
883 write_callable_info (namespace, (GICallableInfo*)info, file);
885 xml_end_element (file, "glib:signal");
889 write_vfunc_info (const gchar *namespace,
893 GIVFuncInfoFlags flags;
895 GIFunctionInfo *invoker;
899 name = g_base_info_get_name ((GIBaseInfo *)info);
900 flags = g_vfunc_info_get_flags (info);
901 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
902 offset = g_vfunc_info_get_offset (info);
903 invoker = g_vfunc_info_get_invoker (info);
905 xml_start_element (file, "virtual-method");
906 xml_printf (file, " name=\"%s\"", name);
909 xml_printf (file, " deprecated=\"1\"");
911 if (flags & GI_VFUNC_MUST_CHAIN_UP)
912 xml_printf (file, " must-chain-up=\"1\"");
914 if (flags & GI_VFUNC_MUST_OVERRIDE)
915 xml_printf (file, " override=\"always\"");
916 else if (flags & GI_VFUNC_MUST_NOT_OVERRIDE)
917 xml_printf (file, " override=\"never\"");
919 if (flags & GI_VFUNC_THROWS)
920 xml_printf (file, " throws=\"1\"");
922 xml_printf (file, " offset=\"%d\"", offset);
925 xml_printf (file, " invoker=\"%s\"", g_base_info_get_name ((GIBaseInfo*)invoker));
927 write_callable_info (namespace, (GICallableInfo*)info, file);
929 xml_end_element (file, "virtual-method");
933 write_property_info (const gchar *namespace,
934 GIPropertyInfo *info,
942 name = g_base_info_get_name ((GIBaseInfo *)info);
943 flags = g_property_info_get_flags (info);
944 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
946 xml_start_element (file, "property");
947 xml_printf (file, " name=\"%s\"", name);
950 xml_printf (file, " deprecated=\"1\"");
952 /* Properties are assumed to be read-only (see also girwriter.py) */
953 if (!(flags & G_PARAM_READABLE))
954 xml_printf (file, " readable=\"0\"");
955 if (flags & G_PARAM_WRITABLE)
956 xml_printf (file, " writable=\"1\"");
958 if (flags & G_PARAM_CONSTRUCT)
959 xml_printf (file, " construct=\"1\"");
961 if (flags & G_PARAM_CONSTRUCT_ONLY)
962 xml_printf (file, " construct-only=\"1\"");
964 write_ownership_transfer (g_property_info_get_ownership_transfer (info), file);
966 write_attributes (file, (GIBaseInfo*) info);
968 type = g_property_info_get_type (info);
970 write_type_info (namespace, type, file);
972 xml_end_element (file, "property");
976 write_object_info (const gchar *namespace,
981 const gchar *type_name;
982 const gchar *type_init;
985 gboolean is_abstract;
986 gboolean is_fundamental;
988 GIStructInfo *class_struct;
991 name = g_base_info_get_name ((GIBaseInfo *)info);
992 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
993 is_abstract = g_object_info_get_abstract (info);
994 is_fundamental = g_object_info_get_fundamental (info);
996 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
997 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
998 xml_start_element (file, "class");
999 xml_printf (file, " name=\"%s\"", name);
1001 pnode = g_object_info_get_parent (info);
1004 write_type_name_attribute (namespace, (GIBaseInfo *)pnode, "parent", file);
1005 g_base_info_unref ((GIBaseInfo *)pnode);
1008 class_struct = g_object_info_get_class_struct (info);
1011 write_type_name_attribute (namespace, (GIBaseInfo*) class_struct, "glib:type-struct", file);
1012 g_base_info_unref ((GIBaseInfo*)class_struct);
1016 xml_printf (file, " abstract=\"1\"");
1018 xml_printf (file, " glib:type-name=\"%s\" glib:get-type=\"%s\"", type_name, type_init);
1021 xml_printf (file, " glib:fundamental=\"1\"");
1023 func = g_object_info_get_unref_function (info);
1025 xml_printf (file, " glib:unref-function=\"%s\"", func);
1027 func = g_object_info_get_ref_function (info);
1029 xml_printf (file, " glib:ref-function=\"%s\"", func);
1031 func = g_object_info_get_set_value_function (info);
1033 xml_printf (file, " glib:set-value-function=\"%s\"", func);
1035 func = g_object_info_get_get_value_function (info);
1037 xml_printf (file, " glib:get-value-function=\"%s\"", func);
1040 xml_printf (file, " deprecated=\"1\"");
1042 write_attributes (file, (GIBaseInfo*) info);
1044 if (g_object_info_get_n_interfaces (info) > 0)
1046 for (i = 0; i < g_object_info_get_n_interfaces (info); i++)
1048 GIInterfaceInfo *imp = g_object_info_get_interface (info, i);
1049 xml_start_element (file, "implements");
1050 write_type_name_attribute (namespace, (GIBaseInfo *)imp, "name", file);
1051 xml_end_element (file, "implements");
1052 g_base_info_unref ((GIBaseInfo*)imp);
1056 for (i = 0; i < g_object_info_get_n_fields (info); i++)
1058 GIFieldInfo *field = g_object_info_get_field (info, i);
1059 write_field_info (namespace, field, NULL, file);
1060 g_base_info_unref ((GIBaseInfo *)field);
1063 for (i = 0; i < g_object_info_get_n_methods (info); i++)
1065 GIFunctionInfo *function = g_object_info_get_method (info, i);
1066 write_function_info (namespace, function, file);
1067 g_base_info_unref ((GIBaseInfo *)function);
1070 for (i = 0; i < g_object_info_get_n_properties (info); i++)
1072 GIPropertyInfo *prop = g_object_info_get_property (info, i);
1073 write_property_info (namespace, prop, file);
1074 g_base_info_unref ((GIBaseInfo *)prop);
1077 for (i = 0; i < g_object_info_get_n_signals (info); i++)
1079 GISignalInfo *signal = g_object_info_get_signal (info, i);
1080 write_signal_info (namespace, signal, file);
1081 g_base_info_unref ((GIBaseInfo *)signal);
1084 for (i = 0; i < g_object_info_get_n_vfuncs (info); i++)
1086 GIVFuncInfo *vfunc = g_object_info_get_vfunc (info, i);
1087 write_vfunc_info (namespace, vfunc, file);
1088 g_base_info_unref ((GIBaseInfo *)vfunc);
1091 for (i = 0; i < g_object_info_get_n_constants (info); i++)
1093 GIConstantInfo *constant = g_object_info_get_constant (info, i);
1094 write_constant_info (namespace, constant, file);
1095 g_base_info_unref ((GIBaseInfo *)constant);
1098 xml_end_element (file, "class");
1102 write_interface_info (const gchar *namespace,
1103 GIInterfaceInfo *info,
1107 const gchar *type_name;
1108 const gchar *type_init;
1109 GIStructInfo *class_struct;
1110 gboolean deprecated;
1113 name = g_base_info_get_name ((GIBaseInfo *)info);
1114 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
1116 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
1117 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
1118 xml_start_element (file, "interface");
1119 xml_printf (file, " name=\"%s\" glib:type-name=\"%s\" glib:get-type=\"%s\"",
1120 name, type_name, type_init);
1122 class_struct = g_interface_info_get_iface_struct (info);
1125 write_type_name_attribute (namespace, (GIBaseInfo*) class_struct, "glib:type-struct", file);
1126 g_base_info_unref ((GIBaseInfo*)class_struct);
1130 xml_printf (file, " deprecated=\"1\"");
1132 write_attributes (file, (GIBaseInfo*) info);
1134 if (g_interface_info_get_n_prerequisites (info) > 0)
1136 for (i = 0; i < g_interface_info_get_n_prerequisites (info); i++)
1138 GIBaseInfo *req = g_interface_info_get_prerequisite (info, i);
1140 xml_start_element (file, "prerequisite");
1141 write_type_name_attribute (namespace, req, "name", file);
1143 xml_end_element_unchecked (file);
1144 g_base_info_unref (req);
1148 for (i = 0; i < g_interface_info_get_n_methods (info); i++)
1150 GIFunctionInfo *function = g_interface_info_get_method (info, i);
1151 write_function_info (namespace, function, file);
1152 g_base_info_unref ((GIBaseInfo *)function);
1155 for (i = 0; i < g_interface_info_get_n_properties (info); i++)
1157 GIPropertyInfo *prop = g_interface_info_get_property (info, i);
1158 write_property_info (namespace, prop, file);
1159 g_base_info_unref ((GIBaseInfo *)prop);
1162 for (i = 0; i < g_interface_info_get_n_signals (info); i++)
1164 GISignalInfo *signal = g_interface_info_get_signal (info, i);
1165 write_signal_info (namespace, signal, file);
1166 g_base_info_unref ((GIBaseInfo *)signal);
1169 for (i = 0; i < g_interface_info_get_n_vfuncs (info); i++)
1171 GIVFuncInfo *vfunc = g_interface_info_get_vfunc (info, i);
1172 write_vfunc_info (namespace, vfunc, file);
1173 g_base_info_unref ((GIBaseInfo *)vfunc);
1176 for (i = 0; i < g_interface_info_get_n_constants (info); i++)
1178 GIConstantInfo *constant = g_interface_info_get_constant (info, i);
1179 write_constant_info (namespace, constant, file);
1180 g_base_info_unref ((GIBaseInfo *)constant);
1183 xml_end_element (file, "interface");
1187 write_union_info (const gchar *namespace,
1192 const gchar *type_name;
1193 const gchar *type_init;
1194 gboolean deprecated;
1198 name = g_base_info_get_name ((GIBaseInfo *)info);
1199 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
1201 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
1202 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
1204 xml_start_element (file, "union");
1205 xml_printf (file, " name=\"%s\"", name);
1208 xml_printf (file, " type-name=\"%s\" get-type=\"%s\"", type_name, type_init);
1211 xml_printf (file, " deprecated=\"1\"");
1213 size = g_union_info_get_size (info);
1214 if (file->show_all && size >= 0)
1215 xml_printf (file, " size=\"%d\"", size);
1217 write_attributes (file, (GIBaseInfo*) info);
1219 if (g_union_info_is_discriminated (info))
1224 offset = g_union_info_get_discriminator_offset (info);
1225 type = g_union_info_get_discriminator_type (info);
1227 xml_start_element (file, "discriminator");
1228 xml_printf (file, " offset=\"%d\" type=\"", offset);
1229 write_type_info (namespace, type, file);
1230 xml_end_element (file, "discriminator");
1231 g_base_info_unref ((GIBaseInfo *)type);
1234 for (i = 0; i < g_union_info_get_n_fields (info); i++)
1236 GIFieldInfo *field = g_union_info_get_field (info, i);
1237 GIConstantInfo *constant = g_union_info_get_discriminator (info, i);
1238 write_field_info (namespace, field, constant, file);
1239 g_base_info_unref ((GIBaseInfo *)field);
1241 g_base_info_unref ((GIBaseInfo *)constant);
1244 for (i = 0; i < g_union_info_get_n_methods (info); i++)
1246 GIFunctionInfo *function = g_union_info_get_method (info, i);
1247 write_function_info (namespace, function, file);
1248 g_base_info_unref ((GIBaseInfo *)function);
1251 xml_end_element (file, "union");
1257 * @filename: filename to write to
1258 * @namespace: GIR namespace to write
1259 * @needs_prefix: if the filename needs prefixing
1260 * @show_all: if field size calculations should be included
1262 * Writes the output of a typelib represented by @namespace
1263 * into a GIR xml file named @filename.
1266 gir_writer_write (const char *filename,
1267 const char *namespace,
1268 gboolean needs_prefix,
1273 char **dependencies;
1274 GIRepository *repository;
1277 repository = g_irepository_get_default ();
1279 if (filename == NULL)
1283 gchar *full_filename;
1286 full_filename = g_strdup_printf ("%s-%s", namespace, filename);
1288 full_filename = g_strdup (filename);
1289 ofile = g_fopen (filename, "w");
1293 g_fprintf (stderr, "failed to open '%s': %s\n",
1294 full_filename, g_strerror (errno));
1295 g_free (full_filename);
1300 g_free (full_filename);
1303 xml = xml_open (ofile);
1304 xml->show_all = show_all;
1305 xml_printf (xml, "<?xml version=\"1.0\"?>\n");
1306 xml_start_element (xml, "repository");
1307 xml_printf (xml, " version=\"1.0\"\n"
1308 " xmlns=\"http://www.gtk.org/introspection/core/1.0\"\n"
1309 " xmlns:c=\"http://www.gtk.org/introspection/c/1.0\"\n"
1310 " xmlns:glib=\"http://www.gtk.org/introspection/glib/1.0\"");
1312 dependencies = g_irepository_get_dependencies (repository,
1314 if (dependencies != NULL)
1316 for (i = 0; dependencies[i]; i++)
1318 char **parts = g_strsplit (dependencies[i], "-", 2);
1319 xml_start_element (xml, "include");
1320 xml_printf (xml, " name=\"%s\" version=\"%s\"", parts[0], parts[1]);
1321 xml_end_element (xml, "include");
1328 const gchar *shared_library;
1329 const gchar *c_prefix;
1330 const char *ns = namespace;
1331 const char *version;
1334 version = g_irepository_get_version (repository, ns);
1336 shared_library = g_irepository_get_shared_library (repository, ns);
1337 c_prefix = g_irepository_get_c_prefix (repository, ns);
1338 xml_start_element (xml, "namespace");
1339 xml_printf (xml, " name=\"%s\" version=\"%s\"", ns, version);
1341 xml_printf (xml, " shared-library=\"%s\"", shared_library);
1343 xml_printf (xml, " c:prefix=\"%s\"", c_prefix);
1345 n_infos = g_irepository_get_n_infos (repository, ns);
1346 for (j = 0; j < n_infos; j++)
1348 GIBaseInfo *info = g_irepository_get_info (repository, ns, j);
1349 switch (g_base_info_get_type (info))
1351 case GI_INFO_TYPE_FUNCTION:
1352 write_function_info (ns, (GIFunctionInfo *)info, xml);
1355 case GI_INFO_TYPE_CALLBACK:
1356 write_callback_info (ns, (GICallbackInfo *)info, xml);
1359 case GI_INFO_TYPE_STRUCT:
1360 case GI_INFO_TYPE_BOXED:
1361 write_struct_info (ns, (GIStructInfo *)info, xml);
1364 case GI_INFO_TYPE_UNION:
1365 write_union_info (ns, (GIUnionInfo *)info, xml);
1368 case GI_INFO_TYPE_ENUM:
1369 case GI_INFO_TYPE_FLAGS:
1370 write_enum_info (ns, (GIEnumInfo *)info, xml);
1373 case GI_INFO_TYPE_CONSTANT:
1374 write_constant_info (ns, (GIConstantInfo *)info, xml);
1377 case GI_INFO_TYPE_OBJECT:
1378 write_object_info (ns, (GIObjectInfo *)info, xml);
1381 case GI_INFO_TYPE_INTERFACE:
1382 write_interface_info (ns, (GIInterfaceInfo *)info, xml);
1386 g_error ("unknown info type %d\n", g_base_info_get_type (info));
1389 g_base_info_unref (info);
1392 xml_end_element (xml, "namespace");
1395 xml_end_element (xml, "repository");