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,
776 name = g_base_info_get_name ((GIBaseInfo *)info);
778 xml_start_element (file, "constant");
779 xml_printf (file, " name=\"%s\"", name);
781 type = g_constant_info_get_type (info);
782 xml_printf (file, " value=\"");
784 g_constant_info_get_value (info, &value);
785 write_constant_value (namespace, type, &value, file);
786 xml_printf (file, "\"");
788 write_type_info (namespace, type, file);
790 write_attributes (file, (GIBaseInfo*) info);
792 xml_end_element (file, "constant");
794 g_base_info_unref ((GIBaseInfo *)type);
799 write_enum_info (const gchar *namespace,
804 const gchar *type_name;
805 const gchar *type_init;
806 const gchar *error_domain;
810 name = g_base_info_get_name ((GIBaseInfo *)info);
811 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
813 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
814 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
815 error_domain = g_enum_info_get_error_domain (info);
817 if (g_base_info_get_type ((GIBaseInfo *)info) == GI_INFO_TYPE_ENUM)
818 xml_start_element (file, "enumeration");
820 xml_start_element (file, "bitfield");
821 xml_printf (file, " name=\"%s\"", name);
824 xml_printf (file, " glib:type-name=\"%s\" glib:get-type=\"%s\"", type_name, type_init);
826 xml_printf (file, " glib:error-domain=\"%s\"", error_domain);
829 xml_printf (file, " deprecated=\"1\"");
831 write_attributes (file, (GIBaseInfo*) info);
833 for (i = 0; i < g_enum_info_get_n_values (info); i++)
835 GIValueInfo *value = g_enum_info_get_value (info, i);
836 write_value_info (namespace, value, file);
837 g_base_info_unref ((GIBaseInfo *)value);
840 xml_end_element_unchecked (file);
844 write_signal_info (const gchar *namespace,
852 name = g_base_info_get_name ((GIBaseInfo *)info);
853 flags = g_signal_info_get_flags (info);
854 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
856 xml_start_element (file, "glib:signal");
857 xml_printf (file, " name=\"%s\"", name);
860 xml_printf (file, " deprecated=\"1\"");
862 if (flags & G_SIGNAL_RUN_FIRST)
863 xml_printf (file, " when=\"FIRST\"");
864 else if (flags & G_SIGNAL_RUN_LAST)
865 xml_printf (file, " when=\"LAST\"");
866 else if (flags & G_SIGNAL_RUN_CLEANUP)
867 xml_printf (file, " when=\"CLEANUP\"");
869 if (flags & G_SIGNAL_NO_RECURSE)
870 xml_printf (file, " no-recurse=\"1\"");
872 if (flags & G_SIGNAL_DETAILED)
873 xml_printf (file, " detailed=\"1\"");
875 if (flags & G_SIGNAL_ACTION)
876 xml_printf (file, " action=\"1\"");
878 if (flags & G_SIGNAL_NO_HOOKS)
879 xml_printf (file, " no-hooks=\"1\"");
881 write_callable_info (namespace, (GICallableInfo*)info, file);
883 xml_end_element (file, "glib:signal");
887 write_vfunc_info (const gchar *namespace,
891 GIVFuncInfoFlags flags;
893 GIFunctionInfo *invoker;
897 name = g_base_info_get_name ((GIBaseInfo *)info);
898 flags = g_vfunc_info_get_flags (info);
899 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
900 offset = g_vfunc_info_get_offset (info);
901 invoker = g_vfunc_info_get_invoker (info);
903 xml_start_element (file, "virtual-method");
904 xml_printf (file, " name=\"%s\"", name);
907 xml_printf (file, " deprecated=\"1\"");
909 if (flags & GI_VFUNC_MUST_CHAIN_UP)
910 xml_printf (file, " must-chain-up=\"1\"");
912 if (flags & GI_VFUNC_MUST_OVERRIDE)
913 xml_printf (file, " override=\"always\"");
914 else if (flags & GI_VFUNC_MUST_NOT_OVERRIDE)
915 xml_printf (file, " override=\"never\"");
917 if (flags & GI_VFUNC_THROWS)
918 xml_printf (file, " throws=\"1\"");
920 xml_printf (file, " offset=\"%d\"", offset);
923 xml_printf (file, " invoker=\"%s\"", g_base_info_get_name ((GIBaseInfo*)invoker));
925 write_callable_info (namespace, (GICallableInfo*)info, file);
927 xml_end_element (file, "virtual-method");
931 write_property_info (const gchar *namespace,
932 GIPropertyInfo *info,
940 name = g_base_info_get_name ((GIBaseInfo *)info);
941 flags = g_property_info_get_flags (info);
942 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
944 xml_start_element (file, "property");
945 xml_printf (file, " name=\"%s\"", name);
948 xml_printf (file, " deprecated=\"1\"");
950 /* Properties are assumed to be read-only (see also girwriter.py) */
951 if (!(flags & G_PARAM_READABLE))
952 xml_printf (file, " readable=\"0\"");
953 if (flags & G_PARAM_WRITABLE)
954 xml_printf (file, " writable=\"1\"");
956 if (flags & G_PARAM_CONSTRUCT)
957 xml_printf (file, " construct=\"1\"");
959 if (flags & G_PARAM_CONSTRUCT_ONLY)
960 xml_printf (file, " construct-only=\"1\"");
962 write_ownership_transfer (g_property_info_get_ownership_transfer (info), file);
964 write_attributes (file, (GIBaseInfo*) info);
966 type = g_property_info_get_type (info);
968 write_type_info (namespace, type, file);
970 xml_end_element (file, "property");
974 write_object_info (const gchar *namespace,
979 const gchar *type_name;
980 const gchar *type_init;
983 gboolean is_abstract;
984 gboolean is_fundamental;
986 GIStructInfo *class_struct;
989 name = g_base_info_get_name ((GIBaseInfo *)info);
990 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
991 is_abstract = g_object_info_get_abstract (info);
992 is_fundamental = g_object_info_get_fundamental (info);
994 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
995 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
996 xml_start_element (file, "class");
997 xml_printf (file, " name=\"%s\"", name);
999 pnode = g_object_info_get_parent (info);
1002 write_type_name_attribute (namespace, (GIBaseInfo *)pnode, "parent", file);
1003 g_base_info_unref ((GIBaseInfo *)pnode);
1006 class_struct = g_object_info_get_class_struct (info);
1009 write_type_name_attribute (namespace, (GIBaseInfo*) class_struct, "glib:type-struct", file);
1010 g_base_info_unref ((GIBaseInfo*)class_struct);
1014 xml_printf (file, " abstract=\"1\"");
1016 xml_printf (file, " glib:type-name=\"%s\" glib:get-type=\"%s\"", type_name, type_init);
1019 xml_printf (file, " glib:fundamental=\"1\"");
1021 func = g_object_info_get_unref_function (info);
1023 xml_printf (file, " glib:unref-function=\"%s\"", func);
1025 func = g_object_info_get_ref_function (info);
1027 xml_printf (file, " glib:ref-function=\"%s\"", func);
1029 func = g_object_info_get_set_value_function (info);
1031 xml_printf (file, " glib:set-value-function=\"%s\"", func);
1033 func = g_object_info_get_get_value_function (info);
1035 xml_printf (file, " glib:get-value-function=\"%s\"", func);
1038 xml_printf (file, " deprecated=\"1\"");
1040 write_attributes (file, (GIBaseInfo*) info);
1042 if (g_object_info_get_n_interfaces (info) > 0)
1044 for (i = 0; i < g_object_info_get_n_interfaces (info); i++)
1046 GIInterfaceInfo *imp = g_object_info_get_interface (info, i);
1047 xml_start_element (file, "implements");
1048 write_type_name_attribute (namespace, (GIBaseInfo *)imp, "name", file);
1049 xml_end_element (file, "implements");
1050 g_base_info_unref ((GIBaseInfo*)imp);
1054 for (i = 0; i < g_object_info_get_n_fields (info); i++)
1056 GIFieldInfo *field = g_object_info_get_field (info, i);
1057 write_field_info (namespace, field, NULL, file);
1058 g_base_info_unref ((GIBaseInfo *)field);
1061 for (i = 0; i < g_object_info_get_n_methods (info); i++)
1063 GIFunctionInfo *function = g_object_info_get_method (info, i);
1064 write_function_info (namespace, function, file);
1065 g_base_info_unref ((GIBaseInfo *)function);
1068 for (i = 0; i < g_object_info_get_n_properties (info); i++)
1070 GIPropertyInfo *prop = g_object_info_get_property (info, i);
1071 write_property_info (namespace, prop, file);
1072 g_base_info_unref ((GIBaseInfo *)prop);
1075 for (i = 0; i < g_object_info_get_n_signals (info); i++)
1077 GISignalInfo *signal = g_object_info_get_signal (info, i);
1078 write_signal_info (namespace, signal, file);
1079 g_base_info_unref ((GIBaseInfo *)signal);
1082 for (i = 0; i < g_object_info_get_n_vfuncs (info); i++)
1084 GIVFuncInfo *vfunc = g_object_info_get_vfunc (info, i);
1085 write_vfunc_info (namespace, vfunc, file);
1086 g_base_info_unref ((GIBaseInfo *)vfunc);
1089 for (i = 0; i < g_object_info_get_n_constants (info); i++)
1091 GIConstantInfo *constant = g_object_info_get_constant (info, i);
1092 write_constant_info (namespace, constant, file);
1093 g_base_info_unref ((GIBaseInfo *)constant);
1096 xml_end_element (file, "class");
1100 write_interface_info (const gchar *namespace,
1101 GIInterfaceInfo *info,
1105 const gchar *type_name;
1106 const gchar *type_init;
1107 GIStructInfo *class_struct;
1108 gboolean deprecated;
1111 name = g_base_info_get_name ((GIBaseInfo *)info);
1112 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
1114 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
1115 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
1116 xml_start_element (file, "interface");
1117 xml_printf (file, " name=\"%s\" glib:type-name=\"%s\" glib:get-type=\"%s\"",
1118 name, type_name, type_init);
1120 class_struct = g_interface_info_get_iface_struct (info);
1123 write_type_name_attribute (namespace, (GIBaseInfo*) class_struct, "glib:type-struct", file);
1124 g_base_info_unref ((GIBaseInfo*)class_struct);
1128 xml_printf (file, " deprecated=\"1\"");
1130 write_attributes (file, (GIBaseInfo*) info);
1132 if (g_interface_info_get_n_prerequisites (info) > 0)
1134 for (i = 0; i < g_interface_info_get_n_prerequisites (info); i++)
1136 GIBaseInfo *req = g_interface_info_get_prerequisite (info, i);
1138 xml_start_element (file, "prerequisite");
1139 write_type_name_attribute (namespace, req, "name", file);
1141 xml_end_element_unchecked (file);
1142 g_base_info_unref (req);
1146 for (i = 0; i < g_interface_info_get_n_methods (info); i++)
1148 GIFunctionInfo *function = g_interface_info_get_method (info, i);
1149 write_function_info (namespace, function, file);
1150 g_base_info_unref ((GIBaseInfo *)function);
1153 for (i = 0; i < g_interface_info_get_n_properties (info); i++)
1155 GIPropertyInfo *prop = g_interface_info_get_property (info, i);
1156 write_property_info (namespace, prop, file);
1157 g_base_info_unref ((GIBaseInfo *)prop);
1160 for (i = 0; i < g_interface_info_get_n_signals (info); i++)
1162 GISignalInfo *signal = g_interface_info_get_signal (info, i);
1163 write_signal_info (namespace, signal, file);
1164 g_base_info_unref ((GIBaseInfo *)signal);
1167 for (i = 0; i < g_interface_info_get_n_vfuncs (info); i++)
1169 GIVFuncInfo *vfunc = g_interface_info_get_vfunc (info, i);
1170 write_vfunc_info (namespace, vfunc, file);
1171 g_base_info_unref ((GIBaseInfo *)vfunc);
1174 for (i = 0; i < g_interface_info_get_n_constants (info); i++)
1176 GIConstantInfo *constant = g_interface_info_get_constant (info, i);
1177 write_constant_info (namespace, constant, file);
1178 g_base_info_unref ((GIBaseInfo *)constant);
1181 xml_end_element (file, "interface");
1185 write_union_info (const gchar *namespace,
1190 const gchar *type_name;
1191 const gchar *type_init;
1192 gboolean deprecated;
1196 name = g_base_info_get_name ((GIBaseInfo *)info);
1197 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
1199 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
1200 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
1202 xml_start_element (file, "union");
1203 xml_printf (file, " name=\"%s\"", name);
1206 xml_printf (file, " type-name=\"%s\" get-type=\"%s\"", type_name, type_init);
1209 xml_printf (file, " deprecated=\"1\"");
1211 size = g_union_info_get_size (info);
1212 if (file->show_all && size >= 0)
1213 xml_printf (file, " size=\"%d\"", size);
1215 write_attributes (file, (GIBaseInfo*) info);
1217 if (g_union_info_is_discriminated (info))
1222 offset = g_union_info_get_discriminator_offset (info);
1223 type = g_union_info_get_discriminator_type (info);
1225 xml_start_element (file, "discriminator");
1226 xml_printf (file, " offset=\"%d\" type=\"", offset);
1227 write_type_info (namespace, type, file);
1228 xml_end_element (file, "discriminator");
1229 g_base_info_unref ((GIBaseInfo *)type);
1232 for (i = 0; i < g_union_info_get_n_fields (info); i++)
1234 GIFieldInfo *field = g_union_info_get_field (info, i);
1235 GIConstantInfo *constant = g_union_info_get_discriminator (info, i);
1236 write_field_info (namespace, field, constant, file);
1237 g_base_info_unref ((GIBaseInfo *)field);
1239 g_base_info_unref ((GIBaseInfo *)constant);
1242 for (i = 0; i < g_union_info_get_n_methods (info); i++)
1244 GIFunctionInfo *function = g_union_info_get_method (info, i);
1245 write_function_info (namespace, function, file);
1246 g_base_info_unref ((GIBaseInfo *)function);
1249 xml_end_element (file, "union");
1255 * @filename: filename to write to
1256 * @namespace: GIR namespace to write
1257 * @needs_prefix: if the filename needs prefixing
1258 * @show_all: if field size calculations should be included
1260 * Writes the output of a typelib represented by @namespace
1261 * into a GIR xml file named @filename.
1264 gir_writer_write (const char *filename,
1265 const char *namespace,
1266 gboolean needs_prefix,
1271 char **dependencies;
1272 GIRepository *repository;
1275 repository = g_irepository_get_default ();
1277 if (filename == NULL)
1281 gchar *full_filename;
1284 full_filename = g_strdup_printf ("%s-%s", namespace, filename);
1286 full_filename = g_strdup (filename);
1287 ofile = g_fopen (filename, "w");
1291 g_fprintf (stderr, "failed to open '%s': %s\n",
1292 full_filename, g_strerror (errno));
1293 g_free (full_filename);
1298 g_free (full_filename);
1301 xml = xml_open (ofile);
1302 xml->show_all = show_all;
1303 xml_printf (xml, "<?xml version=\"1.0\"?>\n");
1304 xml_start_element (xml, "repository");
1305 xml_printf (xml, " version=\"1.0\"\n"
1306 " xmlns=\"http://www.gtk.org/introspection/core/1.0\"\n"
1307 " xmlns:c=\"http://www.gtk.org/introspection/c/1.0\"\n"
1308 " xmlns:glib=\"http://www.gtk.org/introspection/glib/1.0\"");
1310 dependencies = g_irepository_get_immediate_dependencies (repository,
1312 if (dependencies != NULL)
1314 for (i = 0; dependencies[i]; i++)
1316 char **parts = g_strsplit (dependencies[i], "-", 2);
1317 xml_start_element (xml, "include");
1318 xml_printf (xml, " name=\"%s\" version=\"%s\"", parts[0], parts[1]);
1319 xml_end_element (xml, "include");
1326 const gchar *shared_library;
1327 const gchar *c_prefix;
1328 const char *ns = namespace;
1329 const char *version;
1332 version = g_irepository_get_version (repository, ns);
1334 shared_library = g_irepository_get_shared_library (repository, ns);
1335 c_prefix = g_irepository_get_c_prefix (repository, ns);
1336 xml_start_element (xml, "namespace");
1337 xml_printf (xml, " name=\"%s\" version=\"%s\"", ns, version);
1339 xml_printf (xml, " shared-library=\"%s\"", shared_library);
1341 xml_printf (xml, " c:prefix=\"%s\"", c_prefix);
1343 n_infos = g_irepository_get_n_infos (repository, ns);
1344 for (j = 0; j < n_infos; j++)
1346 GIBaseInfo *info = g_irepository_get_info (repository, ns, j);
1347 switch (g_base_info_get_type (info))
1349 case GI_INFO_TYPE_FUNCTION:
1350 write_function_info (ns, (GIFunctionInfo *)info, xml);
1353 case GI_INFO_TYPE_CALLBACK:
1354 write_callback_info (ns, (GICallbackInfo *)info, xml);
1357 case GI_INFO_TYPE_STRUCT:
1358 case GI_INFO_TYPE_BOXED:
1359 write_struct_info (ns, (GIStructInfo *)info, xml);
1362 case GI_INFO_TYPE_UNION:
1363 write_union_info (ns, (GIUnionInfo *)info, xml);
1366 case GI_INFO_TYPE_ENUM:
1367 case GI_INFO_TYPE_FLAGS:
1368 write_enum_info (ns, (GIEnumInfo *)info, xml);
1371 case GI_INFO_TYPE_CONSTANT:
1372 write_constant_info (ns, (GIConstantInfo *)info, xml);
1375 case GI_INFO_TYPE_OBJECT:
1376 write_object_info (ns, (GIObjectInfo *)info, xml);
1379 case GI_INFO_TYPE_INTERFACE:
1380 write_interface_info (ns, (GIInterfaceInfo *)info, xml);
1384 g_error ("unknown info type %d\n", g_base_info_get_type (info));
1387 g_base_info_unref (info);
1390 xml_end_element (xml, "namespace");
1393 xml_end_element (xml, "repository");