1 /* -*- Mode: C; c-file-style: "gnu"; -*- */
2 /* GObject introspection: IDL generator
4 * Copyright (C) 2005 Matthias Clasen
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
26 #include <glib-object.h>
27 #include <glib/gstdio.h>
29 #include "girepository.h"
32 /* FIXME: Avoid global */
33 static gchar *output = NULL;
34 gchar **includedirs = NULL;
43 guint has_children : 1;
47 xml_element_new (const char *name)
51 elem = g_new (XmlElement, 1);
52 elem->name = g_strdup (name);
53 elem->has_children = FALSE;
58 xml_element_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_new (Xml, 1);
139 g_assert (xml->stack == NULL);
140 if (xml->file != NULL)
143 if (xml->file != stdout)
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_type_info (const gchar *namespace,
199 check_unresolved ((GIBaseInfo*)info);
201 tag = g_type_info_get_tag (info);
202 is_pointer = g_type_info_is_pointer (info);
204 if (tag == GI_TYPE_TAG_VOID)
206 xml_start_element (file, "type");
208 xml_printf (file, " name=\"%s\"", is_pointer ? "any" : "none");
210 xml_end_element (file, "type");
212 else if (G_TYPE_TAG_IS_BASIC (tag))
214 xml_start_element (file, "type");
215 xml_printf (file, " name=\"%s\"", g_type_tag_to_string (tag));
216 xml_end_element (file, "type");
218 else if (tag == GI_TYPE_TAG_ARRAY)
222 xml_start_element (file, "array");
224 type = g_type_info_get_param_type (info, 0);
226 length = g_type_info_get_array_length (info);
229 xml_printf (file, " length=\"%d\"", length);
231 if (g_type_info_is_zero_terminated (info))
232 xml_printf (file, " zero-terminated=\"1\"");
234 write_type_info (namespace, type, file);
236 g_base_info_unref ((GIBaseInfo *)type);
238 xml_end_element (file, "array");
240 else if (tag == GI_TYPE_TAG_INTERFACE)
242 GIBaseInfo *iface = g_type_info_get_interface (info);
243 xml_start_element (file, "type");
244 write_type_name_attribute (namespace, iface, "name", file);
245 xml_end_element (file, "type");
246 g_base_info_unref (iface);
248 else if (tag == GI_TYPE_TAG_GLIST)
250 xml_start_element (file, "type");
251 xml_printf (file, " name=\"GLib.List\"");
252 type = g_type_info_get_param_type (info, 0);
255 write_type_info (namespace, type, file);
256 g_base_info_unref ((GIBaseInfo *)type);
258 xml_end_element (file, "type");
260 else if (tag == GI_TYPE_TAG_GSLIST)
262 xml_start_element (file, "type");
263 xml_printf (file, " name=\"GLib.SList\"");
264 type = g_type_info_get_param_type (info, 0);
267 write_type_info (namespace, type, file);
268 g_base_info_unref ((GIBaseInfo *)type);
270 xml_end_element (file, "type");
272 else if (tag == GI_TYPE_TAG_GHASH)
274 xml_start_element (file, "type");
275 xml_printf (file, " name=\"GLib.HashTable\"");
276 type = g_type_info_get_param_type (info, 0);
279 write_type_info (namespace, type, file);
280 g_base_info_unref ((GIBaseInfo *)type);
281 type = g_type_info_get_param_type (info, 1);
282 write_type_info (namespace, type, file);
283 g_base_info_unref ((GIBaseInfo *)type);
285 xml_end_element (file, "type");
287 else if (tag == GI_TYPE_TAG_ERROR)
291 xml_start_element (file, "type");
292 xml_printf (file, " name=\"GLib.Error\"");
294 n = g_type_info_get_n_error_domains (info);
297 for (i = 0; i < n; i++)
299 GIErrorDomainInfo *ed = g_type_info_get_error_domain (info, i);
300 xml_start_element (file, "type");
301 write_type_name_attribute (namespace, (GIBaseInfo *)ed, "name", file);
302 xml_end_element (file, "type");
303 g_base_info_unref ((GIBaseInfo *)ed);
307 xml_end_element (file, "type");
311 g_printerr ("Unhandled type tag %d\n", tag);
312 g_assert_not_reached ();
317 write_constant_value (const gchar *namespace,
323 write_field_info (const gchar *namespace,
325 GIConstantInfo *branch,
329 GIFieldInfoFlags flags;
335 name = g_base_info_get_name ((GIBaseInfo *)info);
336 flags = g_field_info_get_flags (info);
337 size = g_field_info_get_size (info);
338 offset = g_field_info_get_offset (info);
340 xml_start_element (file, "field");
341 xml_printf (file, " name=\"%s\" readable=\"%s\" writable=\"%s\"",
343 flags & GI_FIELD_IS_READABLE ? "1" : "0",
344 flags & GI_FIELD_IS_WRITABLE ? "1" : "0");
346 xml_printf (file, " bits=\"%d\"", size);
348 xml_printf (file, " offset=\"%d\"", offset);
350 type = g_field_info_get_type (info);
354 xml_printf (file, " branch=\"");
355 type = g_constant_info_get_type (branch);
356 g_constant_info_get_value (branch, &value);
357 write_constant_value (namespace, type, &value, file);
358 xml_printf (file, "\"");
361 write_type_info (namespace, type, file);
362 g_base_info_unref ((GIBaseInfo *)type);
364 xml_end_element (file, "field");
368 write_callable_info (const gchar *namespace,
369 GICallableInfo *info,
375 type = g_callable_info_get_return_type (info);
377 xml_start_element (file, "return-value");
379 if (g_type_info_is_pointer (type))
381 switch (g_callable_info_get_caller_owns (info))
383 case GI_TRANSFER_NOTHING:
385 case GI_TRANSFER_CONTAINER:
386 xml_printf (file, " transfer-ownership=\"container\"");
388 case GI_TRANSFER_EVERYTHING:
389 xml_printf (file, " transfer-ownership=\"full\"");
392 g_assert_not_reached ();
396 if (g_callable_info_may_return_null (info))
397 xml_printf (file, " null-ok=\"1\"");
399 write_type_info (namespace, type, file);
401 xml_end_element (file, "return-value");
403 if (g_callable_info_get_n_args (info) <= 0)
406 xml_start_element (file, "parameters");
407 for (i = 0; i < g_callable_info_get_n_args (info); i++)
409 GIArgInfo *arg = g_callable_info_get_arg (info, i);
411 xml_start_element (file, "parameter");
412 xml_printf (file, " name=\"%s\"",
413 g_base_info_get_name ((GIBaseInfo *) arg));
415 switch (g_arg_info_get_ownership_transfer (arg))
417 case GI_TRANSFER_NOTHING:
419 case GI_TRANSFER_CONTAINER:
420 xml_printf (file, " transfer=\"container\"");
422 case GI_TRANSFER_EVERYTHING:
423 xml_printf (file, " transfer=\"full\"");
426 g_assert_not_reached ();
429 xml_printf (file, " direction=\"");
430 switch (g_arg_info_get_direction (arg))
432 case GI_DIRECTION_IN:
433 xml_printf (file, "in");
435 case GI_DIRECTION_OUT:
436 xml_printf (file, "out");
438 case GI_DIRECTION_INOUT:
439 xml_printf (file, "inout");
442 xml_printf (file, "\"");
444 if (g_arg_info_may_be_null (arg))
445 xml_printf (file, " null-ok=\"1\"");
447 if (g_arg_info_is_dipper (arg))
448 xml_printf (file, " dipper=\"1\"");
450 if (g_arg_info_is_return_value (arg))
451 xml_printf (file, " retval=\"1\"");
453 if (g_arg_info_is_optional (arg))
454 xml_printf (file, " optional=\"1\"");
456 type = g_arg_info_get_type (arg);
457 write_type_info (namespace, type, file);
459 xml_end_element (file, "parameter");
461 g_base_info_unref ((GIBaseInfo *)arg);
464 xml_end_element (file, "parameters");
465 g_base_info_unref ((GIBaseInfo *)type);
469 write_function_info (const gchar *namespace,
470 GIFunctionInfo *info,
473 GIFunctionInfoFlags flags;
479 flags = g_function_info_get_flags (info);
480 name = g_base_info_get_name ((GIBaseInfo *)info);
481 symbol = g_function_info_get_symbol (info);
482 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
484 if (flags & GI_FUNCTION_IS_CONSTRUCTOR)
486 else if (flags & GI_FUNCTION_IS_METHOD)
491 xml_start_element (file, tag);
492 xml_printf (file, " name=\"%s\" c:identifier=\"%s\"",
495 if (flags & GI_FUNCTION_IS_SETTER)
496 xml_printf (file, " type=\"setter\"");
497 else if (flags & GI_FUNCTION_IS_GETTER)
498 xml_printf (file, " type=\"getter\"");
501 xml_printf (file, " deprecated=\"1\"");
503 write_callable_info (namespace, (GICallableInfo*)info, file);
504 xml_end_element (file, tag);
508 write_callback_info (const gchar *namespace,
509 GICallbackInfo *info,
515 name = g_base_info_get_name ((GIBaseInfo *)info);
516 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
518 xml_start_element (file, "callback");
519 xml_printf (file, " name=\"%s\"", name);
522 xml_printf (file, " deprecated=\"1\"");
524 write_callable_info (namespace, (GICallableInfo*)info, file);
525 xml_end_element (file, "callback");
529 write_struct_info (const gchar *namespace,
534 const gchar *type_name;
535 const gchar *type_init;
540 name = g_base_info_get_name ((GIBaseInfo *)info);
541 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
543 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
544 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
546 if (g_base_info_get_type ((GIBaseInfo *)info) == GI_INFO_TYPE_BOXED)
548 xml_start_element (file, "glib:boxed");
549 xml_printf (file, " glib:name=\"%s\"", name);
553 xml_start_element (file, "record");
554 xml_printf (file, " name=\"%s\"", name);
557 if (type_name != NULL)
558 xml_printf (file, " glib:type-name=\"%s\" glib:get-type=\"%s\"", type_name, type_init);
561 xml_printf (file, " deprecated=\"1\"");
563 n_elts = g_struct_info_get_n_fields (info) + g_struct_info_get_n_methods (info);
566 for (i = 0; i < g_struct_info_get_n_fields (info); i++)
568 GIFieldInfo *field = g_struct_info_get_field (info, i);
569 write_field_info (namespace, field, NULL, file);
570 g_base_info_unref ((GIBaseInfo *)field);
573 for (i = 0; i < g_struct_info_get_n_methods (info); i++)
575 GIFunctionInfo *function = g_struct_info_get_method (info, i);
576 write_function_info (namespace, function, file);
577 g_base_info_unref ((GIBaseInfo *)function);
582 xml_end_element_unchecked (file);
586 write_value_info (const gchar *namespace,
594 name = g_base_info_get_name ((GIBaseInfo *)info);
595 value = g_value_info_get_value (info);
596 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
598 xml_start_element (file, "member");
599 xml_printf (file, " name=\"%s\" value=\"%ld\"", name, value);
602 xml_printf (file, " deprecated=\"1\"");
604 xml_end_element (file, "member");
608 write_constant_value (const gchar *namespace,
613 switch (g_type_info_get_tag (type))
615 case GI_TYPE_TAG_BOOLEAN:
616 xml_printf (file, "%d", value->v_boolean);
618 case GI_TYPE_TAG_INT8:
619 xml_printf (file, "%d", value->v_int8);
621 case GI_TYPE_TAG_UINT8:
622 xml_printf (file, "%d", value->v_uint8);
624 case GI_TYPE_TAG_INT16:
625 xml_printf (file, "%" G_GINT16_FORMAT, value->v_int16);
627 case GI_TYPE_TAG_UINT16:
628 xml_printf (file, "%" G_GUINT16_FORMAT, value->v_uint16);
630 case GI_TYPE_TAG_INT32:
631 xml_printf (file, "%" G_GINT32_FORMAT, value->v_int32);
633 case GI_TYPE_TAG_UINT32:
634 xml_printf (file, "%" G_GUINT32_FORMAT, value->v_uint32);
636 case GI_TYPE_TAG_INT64:
637 xml_printf (file, "%" G_GINT64_FORMAT, value->v_int64);
639 case GI_TYPE_TAG_UINT64:
640 xml_printf (file, "%" G_GUINT64_FORMAT, value->v_uint64);
642 case GI_TYPE_TAG_INT:
643 xml_printf (file, "%d", value->v_int);
645 case GI_TYPE_TAG_UINT:
646 xml_printf (file, "%d", value->v_uint);
648 case GI_TYPE_TAG_LONG:
649 xml_printf (file, "%ld", value->v_long);
651 case GI_TYPE_TAG_ULONG:
652 xml_printf (file, "%ld", value->v_ulong);
654 case GI_TYPE_TAG_SSIZE:
655 xml_printf (file, "%zd", value->v_ssize);
657 case GI_TYPE_TAG_SIZE:
658 xml_printf (file, "%zd", value->v_size);
660 case GI_TYPE_TAG_FLOAT:
661 xml_printf (file, "%f", value->v_float);
663 case GI_TYPE_TAG_DOUBLE:
664 xml_printf (file, "%f", value->v_double);
666 case GI_TYPE_TAG_UTF8:
667 case GI_TYPE_TAG_FILENAME:
668 xml_printf (file, "%s", value->v_string);
671 g_assert_not_reached ();
676 write_constant_info (const gchar *namespace,
677 GIConstantInfo *info,
685 name = g_base_info_get_name ((GIBaseInfo *)info);
686 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
688 xml_start_element (file, "constant");
689 xml_printf (file, " name=\"%s\"", name);
691 type = g_constant_info_get_type (info);
692 xml_printf (file, " value=\"");
694 g_constant_info_get_value (info, &value);
695 write_constant_value (namespace, type, &value, file);
696 xml_printf (file, "\"");
698 write_type_info (namespace, type, file);
700 xml_end_element (file, "constant");
702 g_base_info_unref ((GIBaseInfo *)type);
707 write_enum_info (const gchar *namespace,
712 const gchar *type_name;
713 const gchar *type_init;
717 name = g_base_info_get_name ((GIBaseInfo *)info);
718 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
720 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
721 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
723 if (g_base_info_get_type ((GIBaseInfo *)info) == GI_INFO_TYPE_ENUM)
724 xml_start_element (file, "enumeration");
726 xml_start_element (file, "bitfield");
727 xml_printf (file, " name=\"%s\"", name);
730 xml_printf (file, " glib:type-name=\"%s\" glib:get-type=\"%s\"", type_name, type_init);
733 xml_printf (file, " deprecated=\"1\"");
736 for (i = 0; i < g_enum_info_get_n_values (info); i++)
738 GIValueInfo *value = g_enum_info_get_value (info, i);
739 write_value_info (namespace, value, file);
740 g_base_info_unref ((GIBaseInfo *)value);
743 xml_end_element_unchecked (file);
747 write_signal_info (const gchar *namespace,
755 name = g_base_info_get_name ((GIBaseInfo *)info);
756 flags = g_signal_info_get_flags (info);
757 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
759 xml_start_element (file, "glib:signal");
760 xml_printf (file, " name=\"%s\"", name);
763 xml_printf (file, " deprecated=\"1\"");
765 if (flags & G_SIGNAL_RUN_FIRST)
766 xml_printf (file, " when=\"FIRST\"");
767 else if (flags & G_SIGNAL_RUN_LAST)
768 xml_printf (file, " when=\"LAST\"");
769 else if (flags & G_SIGNAL_RUN_CLEANUP)
770 xml_printf (file, " when=\"CLEANUP\"");
772 if (flags & G_SIGNAL_NO_RECURSE)
773 xml_printf (file, " no-recurse=\"1\"");
775 if (flags & G_SIGNAL_DETAILED)
776 xml_printf (file, " detailed=\"1\"");
778 if (flags & G_SIGNAL_ACTION)
779 xml_printf (file, " action=\"1\"");
781 if (flags & G_SIGNAL_NO_HOOKS)
782 xml_printf (file, " no-hooks=\"1\"");
784 write_callable_info (namespace, (GICallableInfo*)info, file);
786 xml_end_element (file, "glib:signal");
790 write_vfunc_info (const gchar *namespace,
794 GIVFuncInfoFlags flags;
799 name = g_base_info_get_name ((GIBaseInfo *)info);
800 flags = g_vfunc_info_get_flags (info);
801 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
802 offset = g_vfunc_info_get_offset (info);
804 xml_start_element (file, "vfunc");
805 xml_printf (file, " name=\"%s\"", name);
808 xml_printf (file, " deprecated=\"1\"");
810 if (flags & GI_VFUNC_MUST_CHAIN_UP)
811 xml_printf (file, " must-chain-up=\"1\"");
813 if (flags & GI_VFUNC_MUST_OVERRIDE)
814 xml_printf (file, " override=\"always\"");
815 else if (flags & GI_VFUNC_MUST_NOT_OVERRIDE)
816 xml_printf (file, " override=\"never\"");
818 xml_printf (file, " offset=\"%d\"", offset);
820 write_callable_info (namespace, (GICallableInfo*)info, file);
822 xml_end_element (file, "vfunc");
826 write_property_info (const gchar *namespace,
827 GIPropertyInfo *info,
835 name = g_base_info_get_name ((GIBaseInfo *)info);
836 flags = g_property_info_get_flags (info);
837 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
839 xml_start_element (file, "property");
840 xml_printf (file, " name=\"%s\"", name);
843 xml_printf (file, " deprecated=\"1\"");
845 /* Properties are assumed to be read-only (see also girwriter.py) */
846 if (!(flags & G_PARAM_READABLE))
847 xml_printf (file, " readable=\"0\"");
848 if (flags & G_PARAM_WRITABLE)
849 xml_printf (file, " writable=\"1\"");
851 if (flags & G_PARAM_CONSTRUCT)
852 xml_printf (file, " construct=\"1\"");
854 if (flags & G_PARAM_CONSTRUCT_ONLY)
855 xml_printf (file, " construct-only=\"1\"");
857 type = g_property_info_get_type (info);
859 write_type_info (namespace, type, file);
861 xml_end_element (file, "property");
865 write_object_info (const gchar *namespace,
870 const gchar *type_name;
871 const gchar *type_init;
876 name = g_base_info_get_name ((GIBaseInfo *)info);
877 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
879 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
880 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
881 xml_start_element (file, "class");
882 xml_printf (file, " name=\"%s\"", name);
884 pnode = g_object_info_get_parent (info);
887 write_type_name_attribute (namespace, (GIBaseInfo *)pnode, "parent", file);
888 g_base_info_unref ((GIBaseInfo *)pnode);
891 xml_printf (file, " glib:type-name=\"%s\" glib:get-type=\"%s\"", type_name, type_init);
894 xml_printf (file, " deprecated=\"1\"");
897 if (g_object_info_get_n_interfaces (info) > 0)
899 for (i = 0; i < g_object_info_get_n_interfaces (info); i++)
901 GIInterfaceInfo *imp = g_object_info_get_interface (info, i);
902 xml_start_element (file, "implements");
903 write_type_name_attribute (namespace, (GIBaseInfo *)imp, "name", file);
904 xml_end_element (file, "implements");
905 g_base_info_unref ((GIBaseInfo*)imp);
909 for (i = 0; i < g_object_info_get_n_fields (info); i++)
911 GIFieldInfo *field = g_object_info_get_field (info, i);
912 write_field_info (namespace, field, NULL, file);
913 g_base_info_unref ((GIBaseInfo *)field);
916 for (i = 0; i < g_object_info_get_n_methods (info); i++)
918 GIFunctionInfo *function = g_object_info_get_method (info, i);
919 write_function_info (namespace, function, file);
920 g_base_info_unref ((GIBaseInfo *)function);
923 for (i = 0; i < g_object_info_get_n_properties (info); i++)
925 GIPropertyInfo *prop = g_object_info_get_property (info, i);
926 write_property_info (namespace, prop, file);
927 g_base_info_unref ((GIBaseInfo *)prop);
930 for (i = 0; i < g_object_info_get_n_signals (info); i++)
932 GISignalInfo *signal = g_object_info_get_signal (info, i);
933 write_signal_info (namespace, signal, file);
934 g_base_info_unref ((GIBaseInfo *)signal);
937 for (i = 0; i < g_object_info_get_n_vfuncs (info); i++)
939 GIVFuncInfo *vfunc = g_object_info_get_vfunc (info, i);
940 write_vfunc_info (namespace, vfunc, file);
941 g_base_info_unref ((GIBaseInfo *)vfunc);
944 for (i = 0; i < g_object_info_get_n_constants (info); i++)
946 GIConstantInfo *constant = g_object_info_get_constant (info, i);
947 write_constant_info (namespace, constant, file);
948 g_base_info_unref ((GIBaseInfo *)constant);
951 xml_end_element (file, "class");
955 write_interface_info (const gchar *namespace,
956 GIInterfaceInfo *info,
960 const gchar *type_name;
961 const gchar *type_init;
965 name = g_base_info_get_name ((GIBaseInfo *)info);
966 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
968 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
969 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
970 xml_start_element (file, "interface");
971 xml_printf (file, " name=\"%s\" glib:type-name=\"%s\" glib:get-type=\"%s\"",
972 name, type_name, type_init);
975 xml_printf (file, " deprecated=\"1\"");
978 if (g_interface_info_get_n_prerequisites (info) > 0)
980 xml_start_element (file, "requires");
981 for (i = 0; i < g_interface_info_get_n_prerequisites (info); i++)
983 GIBaseInfo *req = g_interface_info_get_prerequisite (info, i);
985 if (g_base_info_get_type (req) == GI_INFO_TYPE_INTERFACE)
986 xml_start_element (file, "interface");
988 xml_start_element (file, "object");
989 write_type_name_attribute (namespace, req, "name", file);
990 xml_end_element_unchecked (file);
991 g_base_info_unref (req);
993 xml_end_element (file, "requires");
996 for (i = 0; i < g_interface_info_get_n_methods (info); i++)
998 GIFunctionInfo *function = g_interface_info_get_method (info, i);
999 write_function_info (namespace, function, file);
1000 g_base_info_unref ((GIBaseInfo *)function);
1003 for (i = 0; i < g_interface_info_get_n_properties (info); i++)
1005 GIPropertyInfo *prop = g_interface_info_get_property (info, i);
1006 write_property_info (namespace, prop, file);
1007 g_base_info_unref ((GIBaseInfo *)prop);
1010 for (i = 0; i < g_interface_info_get_n_signals (info); i++)
1012 GISignalInfo *signal = g_interface_info_get_signal (info, i);
1013 write_signal_info (namespace, signal, file);
1014 g_base_info_unref ((GIBaseInfo *)signal);
1017 for (i = 0; i < g_interface_info_get_n_vfuncs (info); i++)
1019 GIVFuncInfo *vfunc = g_interface_info_get_vfunc (info, i);
1020 write_vfunc_info (namespace, vfunc, file);
1021 g_base_info_unref ((GIBaseInfo *)vfunc);
1024 for (i = 0; i < g_interface_info_get_n_constants (info); i++)
1026 GIConstantInfo *constant = g_interface_info_get_constant (info, i);
1027 write_constant_info (namespace, constant, file);
1028 g_base_info_unref ((GIBaseInfo *)constant);
1031 xml_end_element (file, "interface");
1035 write_error_domain_info (const gchar *namespace,
1036 GIErrorDomainInfo *info,
1040 const gchar *name, *quark;
1042 name = g_base_info_get_name ((GIBaseInfo *)info);
1043 quark = g_error_domain_info_get_quark (info);
1044 enum_ = (GIBaseInfo *)g_error_domain_info_get_codes (info);
1045 xml_start_element (file, "errordomain");
1046 xml_printf (file, " name=\"%s\" get-quark=\"%s\"",
1048 write_type_name_attribute (namespace, enum_, "codes", file);
1049 xml_end_element (file, "errordomain");
1050 g_base_info_unref (enum_);
1054 write_union_info (const gchar *namespace,
1059 const gchar *type_name;
1060 const gchar *type_init;
1061 gboolean deprecated;
1064 name = g_base_info_get_name ((GIBaseInfo *)info);
1065 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
1067 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
1068 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
1070 xml_start_element (file, "union");
1071 xml_printf (file, " name=\"%s\"", name);
1074 xml_printf (file, " type-name=\"%s\" get-type=\"%s\"", type_name, type_init);
1077 xml_printf (file, " deprecated=\"1\"");
1080 if (g_union_info_is_discriminated (info))
1085 offset = g_union_info_get_discriminator_offset (info);
1086 type = g_union_info_get_discriminator_type (info);
1088 xml_start_element (file, "discriminator");
1089 xml_printf (file, " offset=\"%d\" type=\"", offset);
1090 write_type_info (namespace, type, file);
1091 xml_end_element (file, "discriminator");
1092 g_base_info_unref ((GIBaseInfo *)type);
1095 for (i = 0; i < g_union_info_get_n_fields (info); i++)
1097 GIFieldInfo *field = g_union_info_get_field (info, i);
1098 GIConstantInfo *constant = g_union_info_get_discriminator (info, i);
1099 write_field_info (namespace, field, constant, file);
1100 g_base_info_unref ((GIBaseInfo *)field);
1102 g_base_info_unref ((GIBaseInfo *)constant);
1105 for (i = 0; i < g_union_info_get_n_methods (info); i++)
1107 GIFunctionInfo *function = g_union_info_get_method (info, i);
1108 write_function_info (namespace, function, file);
1109 g_base_info_unref ((GIBaseInfo *)function);
1112 xml_end_element (file, "union");
1116 write_repository (const char *namespace,
1117 gboolean needs_prefix)
1121 char **dependencies;
1122 GIRepository *repository;
1125 repository = g_irepository_get_default ();
1134 filename = g_strdup_printf ("%s-%s", namespace, output);
1136 filename = g_strdup (output);
1137 ofile = g_fopen (filename, "w");
1141 g_fprintf (stderr, "failed to open '%s': %s\n",
1142 filename, g_strerror (errno));
1151 xml = xml_open (ofile);
1153 xml_printf (xml, "<?xml version=\"1.0\"?>\n");
1154 xml_start_element (xml, "repository");
1155 xml_printf (xml, " version=\"1.0\"\n"
1156 " xmlns=\"http://www.gtk.org/introspection/core/1.0\"\n"
1157 " xmlns:c=\"http://www.gtk.org/introspection/c/1.0\"\n"
1158 " xmlns:glib=\"http://www.gtk.org/introspection/glib/1.0\"");
1160 dependencies = g_irepository_get_dependencies (repository,
1162 if (dependencies != NULL)
1164 for (i = 0; dependencies[i]; i++)
1166 char **parts = g_strsplit (dependencies[i], "-", 2);
1167 xml_start_element (xml, "include");
1168 xml_printf (xml, " name=\"%s\" version=\"%s\"", parts[0], parts[1]);
1169 xml_end_element (xml, "include");
1176 const gchar *shared_library;
1177 const char *ns = namespace;
1178 const char *version;
1180 version = g_irepository_get_version (repository, ns);
1182 shared_library = g_irepository_get_shared_library (repository, ns);
1183 xml_start_element (xml, "namespace");
1184 xml_printf (xml, " name=\"%s\" version=\"%s\"", ns, version);
1186 xml_printf (xml, " shared-library=\"%s\"", shared_library);
1188 for (j = 0; j < g_irepository_get_n_infos (repository, ns); j++)
1190 GIBaseInfo *info = g_irepository_get_info (repository, ns, j);
1191 switch (g_base_info_get_type (info))
1193 case GI_INFO_TYPE_FUNCTION:
1194 write_function_info (ns, (GIFunctionInfo *)info, xml);
1197 case GI_INFO_TYPE_CALLBACK:
1198 write_callback_info (ns, (GICallbackInfo *)info, xml);
1201 case GI_INFO_TYPE_STRUCT:
1202 case GI_INFO_TYPE_BOXED:
1203 write_struct_info (ns, (GIStructInfo *)info, xml);
1206 case GI_INFO_TYPE_UNION:
1207 write_union_info (ns, (GIUnionInfo *)info, xml);
1210 case GI_INFO_TYPE_ENUM:
1211 case GI_INFO_TYPE_FLAGS:
1212 write_enum_info (ns, (GIEnumInfo *)info, xml);
1215 case GI_INFO_TYPE_CONSTANT:
1216 write_constant_info (ns, (GIConstantInfo *)info, xml);
1219 case GI_INFO_TYPE_OBJECT:
1220 write_object_info (ns, (GIObjectInfo *)info, xml);
1223 case GI_INFO_TYPE_INTERFACE:
1224 write_interface_info (ns, (GIInterfaceInfo *)info, xml);
1227 case GI_INFO_TYPE_ERROR_DOMAIN:
1228 write_error_domain_info (ns, (GIErrorDomainInfo *)info, xml);
1232 g_error ("unknown info type %d\n", g_base_info_get_type (info));
1235 g_base_info_unref (info);
1238 xml_end_element (xml, "namespace");
1241 xml_end_element (xml, "repository");
1246 static const guchar *
1247 load_typelib (const gchar *filename,
1252 gsize *typelib_size;
1255 handle = g_module_open (filename, G_MODULE_BIND_LOCAL|G_MODULE_BIND_LAZY);
1258 g_printerr ("Could not load typelib from '%s': %s\n",
1259 filename, g_module_error ());
1263 if (!g_module_symbol (handle, "_G_TYPELIB", (gpointer *) &typelib))
1265 g_printerr ("Could not load typelib from '%s': %s\n",
1266 filename, g_module_error ());
1270 if (!g_module_symbol (handle, "_G_TYPELIB_SIZE", (gpointer *) &typelib_size))
1272 g_printerr ("Could not load typelib from '%s': %s\n",
1273 filename, g_module_error ());
1277 *len = *typelib_size;
1286 main (int argc, char *argv[])
1288 gboolean shlib = FALSE;
1289 gchar **input = NULL;
1290 GOptionContext *context;
1291 GError *error = NULL;
1292 gboolean needs_prefix;
1295 GOptionEntry options[] =
1297 { "shlib", 0, 0, G_OPTION_ARG_NONE, &shlib, "handle typelib embedded in shlib", NULL },
1298 { "output", 'o', 0, G_OPTION_ARG_FILENAME, &output, "output file", "FILE" },
1299 { "includedir", 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &includedirs, "include directories in GIR search path", NULL },
1300 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &input, NULL, NULL },
1304 g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL);
1308 g_typelib_check_sanity ();
1310 context = g_option_context_new ("");
1311 g_option_context_add_main_entries (context, options, NULL);
1312 g_option_context_parse (context, &argc, &argv, &error);
1316 g_fprintf (stderr, "no input files\n");
1321 if (includedirs != NULL)
1322 for (i = 0; includedirs[i]; i++)
1323 g_irepository_prepend_search_path (includedirs[i]);
1325 for (i = 0; input[i]; i++)
1327 GModule *dlhandle = NULL;
1328 const guchar *typelib;
1330 const char *namespace;
1334 if (!g_file_get_contents (input[i], (gchar **)&typelib, &len, &error))
1336 g_fprintf (stderr, "failed to read '%s': %s\n",
1337 input[i], error->message);
1338 g_clear_error (&error);
1344 typelib = load_typelib (input[i], &dlhandle, &len);
1347 g_fprintf (stderr, "failed to load typelib from '%s'\n",
1353 if (input[i + 1] && output)
1354 needs_prefix = TRUE;
1356 needs_prefix = FALSE;
1358 data = g_typelib_new_from_const_memory (typelib, len);
1360 GError *error = NULL;
1361 if (!g_typelib_validate (data, &error)) {
1362 g_printerr ("typelib not valid: %s\n", error->message);
1363 g_clear_error (&error);
1367 namespace = g_irepository_load_typelib (g_irepository_get_default (), data, 0,
1369 if (namespace == NULL)
1371 g_printerr ("failed to load typelib: %s\n", error->message);
1375 write_repository (namespace, needs_prefix);
1379 g_module_close (dlhandle);
1383 /* when writing to stdout, stop after the first module */
1384 if (input[i + 1] && !output)
1386 g_fprintf (stderr, "warning, %d modules omitted\n",
1387 g_strv_length (input) - 1);