1 /* -*- Mode: C; c-file-style: "gnu"; -*- */
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.
27 #include <glib-object.h>
28 #include <glib/gstdio.h>
30 #include "girepository.h"
33 /* FIXME: Avoid global */
34 static gchar *output = NULL;
35 gchar **includedirs = NULL;
36 static gboolean show_all = FALSE;
45 guint has_children : 1;
49 xml_element_new (const char *name)
53 elem = g_new (XmlElement, 1);
54 elem->name = g_strdup (name);
55 elem->has_children = FALSE;
60 xml_element_free (XmlElement *elem)
67 xml_printf (Xml *xml, const char *fmt, ...)
73 s = g_markup_vprintf_escaped (fmt, ap);
80 xml_start_element (Xml *xml, const char *element_name)
82 XmlElement *parent = NULL;
86 parent = xml->stack->data;
88 if (!parent->has_children)
89 xml_printf (xml, ">\n");
91 parent->has_children = TRUE;
94 xml_printf (xml, "%*s<%s", g_slist_length(xml->stack)*2, "", element_name);
96 xml->stack = g_slist_prepend (xml->stack, xml_element_new (element_name));
100 xml_end_element (Xml *xml, const char *name)
104 g_assert (xml->stack != NULL);
106 elem = xml->stack->data;
107 xml->stack = g_slist_delete_link (xml->stack, xml->stack);
110 g_assert_cmpstr (name, ==, elem->name);
112 if (elem->has_children)
113 xml_printf (xml, "%*s</%s>\n", g_slist_length (xml->stack)*2, "", elem->name);
115 xml_printf (xml, "/>\n");
117 xml_element_free (elem);
121 xml_end_element_unchecked (Xml *xml)
123 xml_end_element (xml, NULL);
127 xml_open (FILE *file)
131 xml = g_new (Xml, 1);
141 g_assert (xml->stack == NULL);
142 if (xml->file != NULL)
145 if (xml->file != stdout)
160 check_unresolved (GIBaseInfo *info)
162 if (g_base_info_get_type (info) != GI_INFO_TYPE_UNRESOLVED)
165 g_critical ("Found unresolved type '%s' '%s'\n",
166 g_base_info_get_name (info), g_base_info_get_namespace (info));
170 write_type_name (const gchar *namespace,
174 if (strcmp (namespace, g_base_info_get_namespace (info)) != 0)
175 xml_printf (file, "%s.", g_base_info_get_namespace (info));
177 xml_printf (file, "%s", g_base_info_get_name (info));
181 write_type_name_attribute (const gchar *namespace,
183 const char *attr_name,
186 xml_printf (file, " %s=\"", attr_name);
187 write_type_name (namespace, info, file);
188 xml_printf (file, "\"");
192 write_type_info (const gchar *namespace,
201 check_unresolved ((GIBaseInfo*)info);
203 tag = g_type_info_get_tag (info);
204 is_pointer = g_type_info_is_pointer (info);
206 if (tag == GI_TYPE_TAG_VOID)
208 xml_start_element (file, "type");
210 xml_printf (file, " name=\"%s\"", is_pointer ? "any" : "none");
212 xml_end_element (file, "type");
214 else if (G_TYPE_TAG_IS_BASIC (tag))
216 xml_start_element (file, "type");
217 xml_printf (file, " name=\"%s\"", g_type_tag_to_string (tag));
218 xml_end_element (file, "type");
220 else if (tag == GI_TYPE_TAG_ARRAY)
224 xml_start_element (file, "array");
226 type = g_type_info_get_param_type (info, 0);
228 length = g_type_info_get_array_length (info);
230 xml_printf (file, " length=\"%d\"", length);
232 size = g_type_info_get_array_fixed_size (info);
234 xml_printf (file, " fixed-size=\"%d\"", size);
236 if (g_type_info_is_zero_terminated (info))
237 xml_printf (file, " zero-terminated=\"1\"");
239 write_type_info (namespace, type, file);
241 g_base_info_unref ((GIBaseInfo *)type);
243 xml_end_element (file, "array");
245 else if (tag == GI_TYPE_TAG_INTERFACE)
247 GIBaseInfo *iface = g_type_info_get_interface (info);
248 xml_start_element (file, "type");
249 write_type_name_attribute (namespace, iface, "name", file);
250 xml_end_element (file, "type");
251 g_base_info_unref (iface);
253 else if (tag == GI_TYPE_TAG_GLIST)
255 xml_start_element (file, "type");
256 xml_printf (file, " name=\"GLib.List\"");
257 type = g_type_info_get_param_type (info, 0);
260 write_type_info (namespace, type, file);
261 g_base_info_unref ((GIBaseInfo *)type);
263 xml_end_element (file, "type");
265 else if (tag == GI_TYPE_TAG_GSLIST)
267 xml_start_element (file, "type");
268 xml_printf (file, " name=\"GLib.SList\"");
269 type = g_type_info_get_param_type (info, 0);
272 write_type_info (namespace, type, file);
273 g_base_info_unref ((GIBaseInfo *)type);
275 xml_end_element (file, "type");
277 else if (tag == GI_TYPE_TAG_GHASH)
279 xml_start_element (file, "type");
280 xml_printf (file, " name=\"GLib.HashTable\"");
281 type = g_type_info_get_param_type (info, 0);
284 write_type_info (namespace, type, file);
285 g_base_info_unref ((GIBaseInfo *)type);
286 type = g_type_info_get_param_type (info, 1);
287 write_type_info (namespace, type, file);
288 g_base_info_unref ((GIBaseInfo *)type);
290 xml_end_element (file, "type");
292 else if (tag == GI_TYPE_TAG_ERROR)
296 xml_start_element (file, "type");
297 xml_printf (file, " name=\"GLib.Error\"");
299 n = g_type_info_get_n_error_domains (info);
302 for (i = 0; i < n; i++)
304 GIErrorDomainInfo *ed = g_type_info_get_error_domain (info, i);
305 xml_start_element (file, "type");
306 write_type_name_attribute (namespace, (GIBaseInfo *)ed, "name", file);
307 xml_end_element (file, "type");
308 g_base_info_unref ((GIBaseInfo *)ed);
312 xml_end_element (file, "type");
316 g_printerr ("Unhandled type tag %d\n", tag);
317 g_assert_not_reached ();
322 write_attributes (Xml *file,
325 GIAttributeIter iter = { 0, };
328 while (g_base_info_iterate_attributes (info, &iter, &name, &value))
330 xml_start_element (file, "attribute");
331 xml_printf (file, " name=\"%s\" value=\"%s\"", name, value);
332 xml_end_element (file, "attribute");
337 write_constant_value (const gchar *namespace,
343 write_callback_info (const gchar *namespace,
344 GICallbackInfo *info,
348 write_field_info (const gchar *namespace,
350 GIConstantInfo *branch,
354 GIFieldInfoFlags flags;
358 GIBaseInfo *interface;
361 name = g_base_info_get_name ((GIBaseInfo *)info);
362 flags = g_field_info_get_flags (info);
363 size = g_field_info_get_size (info);
364 offset = g_field_info_get_offset (info);
366 xml_start_element (file, "field");
367 xml_printf (file, " name=\"%s\"", name);
369 /* Fields are assumed to be read-only
370 * (see also girwriter.py and girparser.c)
372 if (!(flags & GI_FIELD_IS_READABLE))
373 xml_printf (file, " readable=\"0\"");
374 if (flags & GI_FIELD_IS_WRITABLE)
375 xml_printf (file, " writable=\"1\"");
378 xml_printf (file, " bits=\"%d\"", size);
380 write_attributes (file, (GIBaseInfo*) info);
382 type = g_field_info_get_type (info);
386 xml_printf (file, " branch=\"");
387 type = g_constant_info_get_type (branch);
388 g_constant_info_get_value (branch, &value);
389 write_constant_value (namespace, type, &value, file);
390 xml_printf (file, "\"");
396 xml_printf (file, "offset=\"%d\"", offset);
399 interface = g_type_info_get_interface (type);
400 if (interface && g_base_info_get_type(interface) == GI_INFO_TYPE_CALLBACK)
401 write_callback_info (namespace, (GICallbackInfo *)interface, file);
403 write_type_info (namespace, type, file);
406 g_base_info_unref (interface);
408 g_base_info_unref ((GIBaseInfo *)type);
410 xml_end_element (file, "field");
414 write_callable_info (const gchar *namespace,
415 GICallableInfo *info,
421 write_attributes (file, (GIBaseInfo*) info);
423 type = g_callable_info_get_return_type (info);
425 xml_start_element (file, "return-value");
427 switch (g_callable_info_get_caller_owns (info))
429 case GI_TRANSFER_NOTHING:
430 xml_printf (file, " transfer-ownership=\"none\"");
432 case GI_TRANSFER_CONTAINER:
433 xml_printf (file, " transfer-ownership=\"container\"");
435 case GI_TRANSFER_EVERYTHING:
436 xml_printf (file, " transfer-ownership=\"full\"");
439 g_assert_not_reached ();
442 if (g_callable_info_may_return_null (info))
443 xml_printf (file, " allow-none=\"1\"");
445 write_type_info (namespace, type, file);
447 xml_end_element (file, "return-value");
449 if (g_callable_info_get_n_args (info) <= 0)
452 xml_start_element (file, "parameters");
453 for (i = 0; i < g_callable_info_get_n_args (info); i++)
455 GIArgInfo *arg = g_callable_info_get_arg (info, i);
457 xml_start_element (file, "parameter");
458 xml_printf (file, " name=\"%s\"",
459 g_base_info_get_name ((GIBaseInfo *) arg));
461 switch (g_arg_info_get_ownership_transfer (arg))
463 case GI_TRANSFER_NOTHING:
464 xml_printf (file, " transfer-ownership=\"none\"");
466 case GI_TRANSFER_CONTAINER:
467 xml_printf (file, " transfer-ownership=\"container\"");
469 case GI_TRANSFER_EVERYTHING:
470 xml_printf (file, " transfer-ownership=\"full\"");
473 g_assert_not_reached ();
476 switch (g_arg_info_get_direction (arg))
478 case GI_DIRECTION_IN:
480 case GI_DIRECTION_OUT:
481 xml_printf (file, " direction=\"out\"");
483 case GI_DIRECTION_INOUT:
484 xml_printf (file, " direction=\"inout\"");
488 if (g_arg_info_may_be_null (arg))
489 xml_printf (file, " allow-none=\"1\"");
491 if (g_arg_info_is_dipper (arg))
492 xml_printf (file, " dipper=\"1\"");
494 if (g_arg_info_is_return_value (arg))
495 xml_printf (file, " retval=\"1\"");
497 if (g_arg_info_is_optional (arg))
498 xml_printf (file, " optional=\"1\"");
500 switch (g_arg_info_get_scope (arg))
502 case GI_SCOPE_TYPE_INVALID:
504 case GI_SCOPE_TYPE_CALL:
505 xml_printf (file, " scope=\"call\"");
507 case GI_SCOPE_TYPE_ASYNC:
508 xml_printf (file, " scope=\"async\"");
510 case GI_SCOPE_TYPE_NOTIFIED:
511 xml_printf (file, " scope=\"notified\"");
515 if (g_arg_info_get_closure (arg) >= 0)
516 xml_printf (file, " closure=\"%d\"", g_arg_info_get_closure (arg));
518 if (g_arg_info_get_destroy (arg) >= 0)
519 xml_printf (file, " destroy=\"%d\"", g_arg_info_get_destroy (arg));
521 type = g_arg_info_get_type (arg);
522 write_type_info (namespace, type, file);
524 xml_end_element (file, "parameter");
526 g_base_info_unref ((GIBaseInfo *)arg);
529 xml_end_element (file, "parameters");
530 g_base_info_unref ((GIBaseInfo *)type);
534 write_function_info (const gchar *namespace,
535 GIFunctionInfo *info,
538 GIFunctionInfoFlags flags;
545 flags = g_function_info_get_flags (info);
546 name = g_base_info_get_name ((GIBaseInfo *)info);
547 symbol = g_function_info_get_symbol (info);
548 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
549 throws = flags & GI_FUNCTION_THROWS;
551 if (flags & GI_FUNCTION_IS_CONSTRUCTOR)
553 else if (flags & GI_FUNCTION_IS_METHOD)
558 xml_start_element (file, tag);
559 xml_printf (file, " name=\"%s\" c:identifier=\"%s\"",
562 if (flags & GI_FUNCTION_IS_SETTER)
563 xml_printf (file, " type=\"setter\"");
564 else if (flags & GI_FUNCTION_IS_GETTER)
565 xml_printf (file, " type=\"getter\"");
568 xml_printf (file, " deprecated=\"1\"");
571 xml_printf (file, " throws=\"1\"");
573 write_callable_info (namespace, (GICallableInfo*)info, file);
574 xml_end_element (file, tag);
578 write_callback_info (const gchar *namespace,
579 GICallbackInfo *info,
585 name = g_base_info_get_name ((GIBaseInfo *)info);
586 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
588 xml_start_element (file, "callback");
589 xml_printf (file, " name=\"%s\"", name);
592 xml_printf (file, " deprecated=\"1\"");
594 write_callable_info (namespace, (GICallableInfo*)info, file);
595 xml_end_element (file, "callback");
599 write_struct_info (const gchar *namespace,
604 const gchar *type_name;
605 const gchar *type_init;
607 gboolean is_gtype_struct;
612 name = g_base_info_get_name ((GIBaseInfo *)info);
613 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
615 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
616 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
618 if (g_base_info_get_type ((GIBaseInfo *)info) == GI_INFO_TYPE_BOXED)
620 xml_start_element (file, "glib:boxed");
621 xml_printf (file, " glib:name=\"%s\"", name);
625 xml_start_element (file, "record");
626 xml_printf (file, " name=\"%s\"", name);
629 if (type_name != NULL)
630 xml_printf (file, " glib:type-name=\"%s\" glib:get-type=\"%s\"", type_name, type_init);
633 xml_printf (file, " deprecated=\"1\"");
635 is_gtype_struct = g_struct_info_is_gtype_struct (info);
637 xml_printf (file, " glib:is-gtype-struct=\"1\"");
639 write_attributes (file, (GIBaseInfo*) info);
641 size = g_struct_info_get_size (info);
642 if (show_all && size >= 0)
643 xml_printf (file, " size=\"%d\"", size);
645 n_elts = g_struct_info_get_n_fields (info) + g_struct_info_get_n_methods (info);
648 for (i = 0; i < g_struct_info_get_n_fields (info); i++)
650 GIFieldInfo *field = g_struct_info_get_field (info, i);
651 write_field_info (namespace, field, NULL, file);
652 g_base_info_unref ((GIBaseInfo *)field);
655 for (i = 0; i < g_struct_info_get_n_methods (info); i++)
657 GIFunctionInfo *function = g_struct_info_get_method (info, i);
658 write_function_info (namespace, function, file);
659 g_base_info_unref ((GIBaseInfo *)function);
664 xml_end_element_unchecked (file);
668 write_value_info (const gchar *namespace,
676 name = g_base_info_get_name ((GIBaseInfo *)info);
677 value = g_value_info_get_value (info);
678 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
680 xml_start_element (file, "member");
681 xml_printf (file, " name=\"%s\" value=\"%ld\"", name, value);
684 xml_printf (file, " deprecated=\"1\"");
686 write_attributes (file, (GIBaseInfo*) info);
688 xml_end_element (file, "member");
692 write_constant_value (const gchar *namespace,
697 switch (g_type_info_get_tag (type))
699 case GI_TYPE_TAG_BOOLEAN:
700 xml_printf (file, "%d", value->v_boolean);
702 case GI_TYPE_TAG_INT8:
703 xml_printf (file, "%d", value->v_int8);
705 case GI_TYPE_TAG_UINT8:
706 xml_printf (file, "%d", value->v_uint8);
708 case GI_TYPE_TAG_INT16:
709 xml_printf (file, "%" G_GINT16_FORMAT, value->v_int16);
711 case GI_TYPE_TAG_UINT16:
712 xml_printf (file, "%" G_GUINT16_FORMAT, value->v_uint16);
714 case GI_TYPE_TAG_INT32:
715 xml_printf (file, "%" G_GINT32_FORMAT, value->v_int32);
717 case GI_TYPE_TAG_UINT32:
718 xml_printf (file, "%" G_GUINT32_FORMAT, value->v_uint32);
720 case GI_TYPE_TAG_INT64:
721 xml_printf (file, "%" G_GINT64_FORMAT, value->v_int64);
723 case GI_TYPE_TAG_UINT64:
724 xml_printf (file, "%" G_GUINT64_FORMAT, value->v_uint64);
726 case GI_TYPE_TAG_INT:
727 xml_printf (file, "%d", value->v_int);
729 case GI_TYPE_TAG_UINT:
730 xml_printf (file, "%d", value->v_uint);
732 case GI_TYPE_TAG_LONG:
733 xml_printf (file, "%ld", value->v_long);
735 case GI_TYPE_TAG_ULONG:
736 xml_printf (file, "%ld", value->v_ulong);
738 case GI_TYPE_TAG_SSIZE:
739 xml_printf (file, "%zd", value->v_ssize);
741 case GI_TYPE_TAG_SIZE:
742 xml_printf (file, "%zd", value->v_size);
744 case GI_TYPE_TAG_FLOAT:
745 xml_printf (file, "%f", value->v_float);
747 case GI_TYPE_TAG_DOUBLE:
748 xml_printf (file, "%f", value->v_double);
750 case GI_TYPE_TAG_UTF8:
751 case GI_TYPE_TAG_FILENAME:
752 xml_printf (file, "%s", value->v_string);
755 g_assert_not_reached ();
760 write_constant_info (const gchar *namespace,
761 GIConstantInfo *info,
769 name = g_base_info_get_name ((GIBaseInfo *)info);
770 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
772 xml_start_element (file, "constant");
773 xml_printf (file, " name=\"%s\"", name);
775 type = g_constant_info_get_type (info);
776 xml_printf (file, " value=\"");
778 g_constant_info_get_value (info, &value);
779 write_constant_value (namespace, type, &value, file);
780 xml_printf (file, "\"");
782 write_type_info (namespace, type, file);
784 write_attributes (file, (GIBaseInfo*) info);
786 xml_end_element (file, "constant");
788 g_base_info_unref ((GIBaseInfo *)type);
793 write_enum_info (const gchar *namespace,
798 const gchar *type_name;
799 const gchar *type_init;
803 name = g_base_info_get_name ((GIBaseInfo *)info);
804 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
806 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
807 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
809 if (g_base_info_get_type ((GIBaseInfo *)info) == GI_INFO_TYPE_ENUM)
810 xml_start_element (file, "enumeration");
812 xml_start_element (file, "bitfield");
813 xml_printf (file, " name=\"%s\"", name);
816 xml_printf (file, " glib:type-name=\"%s\" glib:get-type=\"%s\"", type_name, type_init);
819 xml_printf (file, " deprecated=\"1\"");
821 write_attributes (file, (GIBaseInfo*) info);
823 for (i = 0; i < g_enum_info_get_n_values (info); i++)
825 GIValueInfo *value = g_enum_info_get_value (info, i);
826 write_value_info (namespace, value, file);
827 g_base_info_unref ((GIBaseInfo *)value);
830 xml_end_element_unchecked (file);
834 write_signal_info (const gchar *namespace,
842 name = g_base_info_get_name ((GIBaseInfo *)info);
843 flags = g_signal_info_get_flags (info);
844 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
846 xml_start_element (file, "glib:signal");
847 xml_printf (file, " name=\"%s\"", name);
850 xml_printf (file, " deprecated=\"1\"");
852 if (flags & G_SIGNAL_RUN_FIRST)
853 xml_printf (file, " when=\"FIRST\"");
854 else if (flags & G_SIGNAL_RUN_LAST)
855 xml_printf (file, " when=\"LAST\"");
856 else if (flags & G_SIGNAL_RUN_CLEANUP)
857 xml_printf (file, " when=\"CLEANUP\"");
859 if (flags & G_SIGNAL_NO_RECURSE)
860 xml_printf (file, " no-recurse=\"1\"");
862 if (flags & G_SIGNAL_DETAILED)
863 xml_printf (file, " detailed=\"1\"");
865 if (flags & G_SIGNAL_ACTION)
866 xml_printf (file, " action=\"1\"");
868 if (flags & G_SIGNAL_NO_HOOKS)
869 xml_printf (file, " no-hooks=\"1\"");
871 write_callable_info (namespace, (GICallableInfo*)info, file);
873 xml_end_element (file, "glib:signal");
877 write_vfunc_info (const gchar *namespace,
881 GIVFuncInfoFlags flags;
883 GIFunctionInfo *invoker;
887 name = g_base_info_get_name ((GIBaseInfo *)info);
888 flags = g_vfunc_info_get_flags (info);
889 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
890 offset = g_vfunc_info_get_offset (info);
891 invoker = g_vfunc_info_get_invoker (info);
893 xml_start_element (file, "virtual-method");
894 xml_printf (file, " name=\"%s\"", name);
897 xml_printf (file, " deprecated=\"1\"");
899 if (flags & GI_VFUNC_MUST_CHAIN_UP)
900 xml_printf (file, " must-chain-up=\"1\"");
902 if (flags & GI_VFUNC_MUST_OVERRIDE)
903 xml_printf (file, " override=\"always\"");
904 else if (flags & GI_VFUNC_MUST_NOT_OVERRIDE)
905 xml_printf (file, " override=\"never\"");
907 xml_printf (file, " offset=\"%d\"", offset);
910 xml_printf (file, " invoker=\"%s\"", g_base_info_get_name ((GIBaseInfo*)invoker));
912 write_callable_info (namespace, (GICallableInfo*)info, file);
914 xml_end_element (file, "virtual-method");
918 write_property_info (const gchar *namespace,
919 GIPropertyInfo *info,
927 name = g_base_info_get_name ((GIBaseInfo *)info);
928 flags = g_property_info_get_flags (info);
929 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
931 xml_start_element (file, "property");
932 xml_printf (file, " name=\"%s\"", name);
935 xml_printf (file, " deprecated=\"1\"");
937 /* Properties are assumed to be read-only (see also girwriter.py) */
938 if (!(flags & G_PARAM_READABLE))
939 xml_printf (file, " readable=\"0\"");
940 if (flags & G_PARAM_WRITABLE)
941 xml_printf (file, " writable=\"1\"");
943 if (flags & G_PARAM_CONSTRUCT)
944 xml_printf (file, " construct=\"1\"");
946 if (flags & G_PARAM_CONSTRUCT_ONLY)
947 xml_printf (file, " construct-only=\"1\"");
949 write_attributes (file, (GIBaseInfo*) info);
951 type = g_property_info_get_type (info);
953 write_type_info (namespace, type, file);
955 xml_end_element (file, "property");
959 write_object_info (const gchar *namespace,
964 const gchar *type_name;
965 const gchar *type_init;
967 gboolean is_abstract;
969 GIStructInfo *class_struct;
972 name = g_base_info_get_name ((GIBaseInfo *)info);
973 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
974 is_abstract = g_object_info_get_abstract (info);
976 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
977 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
978 xml_start_element (file, "class");
979 xml_printf (file, " name=\"%s\"", name);
981 pnode = g_object_info_get_parent (info);
984 write_type_name_attribute (namespace, (GIBaseInfo *)pnode, "parent", file);
985 g_base_info_unref ((GIBaseInfo *)pnode);
988 class_struct = g_object_info_get_class_struct (info);
991 write_type_name_attribute (namespace, (GIBaseInfo*) class_struct, "glib:type-struct", file);
992 g_base_info_unref ((GIBaseInfo*)class_struct);
996 xml_printf (file, " abstract=\"1\"");
998 xml_printf (file, " glib:type-name=\"%s\" glib:get-type=\"%s\"", type_name, type_init);
1001 xml_printf (file, " deprecated=\"1\"");
1003 write_attributes (file, (GIBaseInfo*) info);
1005 if (g_object_info_get_n_interfaces (info) > 0)
1007 for (i = 0; i < g_object_info_get_n_interfaces (info); i++)
1009 GIInterfaceInfo *imp = g_object_info_get_interface (info, i);
1010 xml_start_element (file, "implements");
1011 write_type_name_attribute (namespace, (GIBaseInfo *)imp, "name", file);
1012 xml_end_element (file, "implements");
1013 g_base_info_unref ((GIBaseInfo*)imp);
1017 for (i = 0; i < g_object_info_get_n_fields (info); i++)
1019 GIFieldInfo *field = g_object_info_get_field (info, i);
1020 write_field_info (namespace, field, NULL, file);
1021 g_base_info_unref ((GIBaseInfo *)field);
1024 for (i = 0; i < g_object_info_get_n_methods (info); i++)
1026 GIFunctionInfo *function = g_object_info_get_method (info, i);
1027 write_function_info (namespace, function, file);
1028 g_base_info_unref ((GIBaseInfo *)function);
1031 for (i = 0; i < g_object_info_get_n_properties (info); i++)
1033 GIPropertyInfo *prop = g_object_info_get_property (info, i);
1034 write_property_info (namespace, prop, file);
1035 g_base_info_unref ((GIBaseInfo *)prop);
1038 for (i = 0; i < g_object_info_get_n_signals (info); i++)
1040 GISignalInfo *signal = g_object_info_get_signal (info, i);
1041 write_signal_info (namespace, signal, file);
1042 g_base_info_unref ((GIBaseInfo *)signal);
1045 for (i = 0; i < g_object_info_get_n_vfuncs (info); i++)
1047 GIVFuncInfo *vfunc = g_object_info_get_vfunc (info, i);
1048 write_vfunc_info (namespace, vfunc, file);
1049 g_base_info_unref ((GIBaseInfo *)vfunc);
1052 for (i = 0; i < g_object_info_get_n_constants (info); i++)
1054 GIConstantInfo *constant = g_object_info_get_constant (info, i);
1055 write_constant_info (namespace, constant, file);
1056 g_base_info_unref ((GIBaseInfo *)constant);
1059 xml_end_element (file, "class");
1063 write_interface_info (const gchar *namespace,
1064 GIInterfaceInfo *info,
1068 const gchar *type_name;
1069 const gchar *type_init;
1070 GIStructInfo *class_struct;
1071 gboolean deprecated;
1074 name = g_base_info_get_name ((GIBaseInfo *)info);
1075 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
1077 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
1078 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
1079 xml_start_element (file, "interface");
1080 xml_printf (file, " name=\"%s\" glib:type-name=\"%s\" glib:get-type=\"%s\"",
1081 name, type_name, type_init);
1083 class_struct = g_interface_info_get_iface_struct (info);
1086 write_type_name_attribute (namespace, (GIBaseInfo*) class_struct, "glib:type-struct", file);
1087 g_base_info_unref ((GIBaseInfo*)class_struct);
1091 xml_printf (file, " deprecated=\"1\"");
1093 write_attributes (file, (GIBaseInfo*) info);
1095 if (g_interface_info_get_n_prerequisites (info) > 0)
1097 for (i = 0; i < g_interface_info_get_n_prerequisites (info); i++)
1099 GIBaseInfo *req = g_interface_info_get_prerequisite (info, i);
1101 xml_start_element (file, "prerequisite");
1102 write_type_name_attribute (namespace, req, "name", file);
1104 xml_end_element_unchecked (file);
1105 g_base_info_unref (req);
1109 for (i = 0; i < g_interface_info_get_n_methods (info); i++)
1111 GIFunctionInfo *function = g_interface_info_get_method (info, i);
1112 write_function_info (namespace, function, file);
1113 g_base_info_unref ((GIBaseInfo *)function);
1116 for (i = 0; i < g_interface_info_get_n_properties (info); i++)
1118 GIPropertyInfo *prop = g_interface_info_get_property (info, i);
1119 write_property_info (namespace, prop, file);
1120 g_base_info_unref ((GIBaseInfo *)prop);
1123 for (i = 0; i < g_interface_info_get_n_signals (info); i++)
1125 GISignalInfo *signal = g_interface_info_get_signal (info, i);
1126 write_signal_info (namespace, signal, file);
1127 g_base_info_unref ((GIBaseInfo *)signal);
1130 for (i = 0; i < g_interface_info_get_n_vfuncs (info); i++)
1132 GIVFuncInfo *vfunc = g_interface_info_get_vfunc (info, i);
1133 write_vfunc_info (namespace, vfunc, file);
1134 g_base_info_unref ((GIBaseInfo *)vfunc);
1137 for (i = 0; i < g_interface_info_get_n_constants (info); i++)
1139 GIConstantInfo *constant = g_interface_info_get_constant (info, i);
1140 write_constant_info (namespace, constant, file);
1141 g_base_info_unref ((GIBaseInfo *)constant);
1144 xml_end_element (file, "interface");
1148 write_error_domain_info (const gchar *namespace,
1149 GIErrorDomainInfo *info,
1153 const gchar *name, *quark;
1155 name = g_base_info_get_name ((GIBaseInfo *)info);
1156 quark = g_error_domain_info_get_quark (info);
1157 enum_ = (GIBaseInfo *)g_error_domain_info_get_codes (info);
1158 xml_start_element (file, "errordomain");
1159 xml_printf (file, " name=\"%s\" get-quark=\"%s\"",
1161 write_type_name_attribute (namespace, enum_, "codes", file);
1162 xml_end_element (file, "errordomain");
1163 g_base_info_unref (enum_);
1167 write_union_info (const gchar *namespace,
1172 const gchar *type_name;
1173 const gchar *type_init;
1174 gboolean deprecated;
1178 name = g_base_info_get_name ((GIBaseInfo *)info);
1179 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
1181 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
1182 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
1184 xml_start_element (file, "union");
1185 xml_printf (file, " name=\"%s\"", name);
1188 xml_printf (file, " type-name=\"%s\" get-type=\"%s\"", type_name, type_init);
1191 xml_printf (file, " deprecated=\"1\"");
1193 size = g_union_info_get_size (info);
1194 if (show_all && size >= 0)
1195 xml_printf (file, " size=\"%d\"", size);
1197 write_attributes (file, (GIBaseInfo*) info);
1199 if (g_union_info_is_discriminated (info))
1204 offset = g_union_info_get_discriminator_offset (info);
1205 type = g_union_info_get_discriminator_type (info);
1207 xml_start_element (file, "discriminator");
1208 xml_printf (file, " offset=\"%d\" type=\"", offset);
1209 write_type_info (namespace, type, file);
1210 xml_end_element (file, "discriminator");
1211 g_base_info_unref ((GIBaseInfo *)type);
1214 for (i = 0; i < g_union_info_get_n_fields (info); i++)
1216 GIFieldInfo *field = g_union_info_get_field (info, i);
1217 GIConstantInfo *constant = g_union_info_get_discriminator (info, i);
1218 write_field_info (namespace, field, constant, file);
1219 g_base_info_unref ((GIBaseInfo *)field);
1221 g_base_info_unref ((GIBaseInfo *)constant);
1224 for (i = 0; i < g_union_info_get_n_methods (info); i++)
1226 GIFunctionInfo *function = g_union_info_get_method (info, i);
1227 write_function_info (namespace, function, file);
1228 g_base_info_unref ((GIBaseInfo *)function);
1231 xml_end_element (file, "union");
1235 write_repository (const char *namespace,
1236 gboolean needs_prefix)
1240 char **dependencies;
1241 GIRepository *repository;
1244 repository = g_irepository_get_default ();
1253 filename = g_strdup_printf ("%s-%s", namespace, output);
1255 filename = g_strdup (output);
1256 ofile = g_fopen (filename, "w");
1260 g_fprintf (stderr, "failed to open '%s': %s\n",
1261 filename, g_strerror (errno));
1270 xml = xml_open (ofile);
1272 xml_printf (xml, "<?xml version=\"1.0\"?>\n");
1273 xml_start_element (xml, "repository");
1274 xml_printf (xml, " version=\"1.0\"\n"
1275 " xmlns=\"http://www.gtk.org/introspection/core/1.0\"\n"
1276 " xmlns:c=\"http://www.gtk.org/introspection/c/1.0\"\n"
1277 " xmlns:glib=\"http://www.gtk.org/introspection/glib/1.0\"");
1279 dependencies = g_irepository_get_dependencies (repository,
1281 if (dependencies != NULL)
1283 for (i = 0; dependencies[i]; i++)
1285 char **parts = g_strsplit (dependencies[i], "-", 2);
1286 xml_start_element (xml, "include");
1287 xml_printf (xml, " name=\"%s\" version=\"%s\"", parts[0], parts[1]);
1288 xml_end_element (xml, "include");
1295 const gchar *shared_library;
1296 const gchar *c_prefix;
1297 const char *ns = namespace;
1298 const char *version;
1300 version = g_irepository_get_version (repository, ns);
1302 shared_library = g_irepository_get_shared_library (repository, ns);
1303 c_prefix = g_irepository_get_c_prefix (repository, ns);
1304 xml_start_element (xml, "namespace");
1305 xml_printf (xml, " name=\"%s\" version=\"%s\"", ns, version);
1307 xml_printf (xml, " shared-library=\"%s\"", shared_library);
1309 xml_printf (xml, " c:prefix=\"%s\"", c_prefix);
1311 for (j = 0; j < g_irepository_get_n_infos (repository, ns); j++)
1313 GIBaseInfo *info = g_irepository_get_info (repository, ns, j);
1314 switch (g_base_info_get_type (info))
1316 case GI_INFO_TYPE_FUNCTION:
1317 write_function_info (ns, (GIFunctionInfo *)info, xml);
1320 case GI_INFO_TYPE_CALLBACK:
1321 write_callback_info (ns, (GICallbackInfo *)info, xml);
1324 case GI_INFO_TYPE_STRUCT:
1325 case GI_INFO_TYPE_BOXED:
1326 write_struct_info (ns, (GIStructInfo *)info, xml);
1329 case GI_INFO_TYPE_UNION:
1330 write_union_info (ns, (GIUnionInfo *)info, xml);
1333 case GI_INFO_TYPE_ENUM:
1334 case GI_INFO_TYPE_FLAGS:
1335 write_enum_info (ns, (GIEnumInfo *)info, xml);
1338 case GI_INFO_TYPE_CONSTANT:
1339 write_constant_info (ns, (GIConstantInfo *)info, xml);
1342 case GI_INFO_TYPE_OBJECT:
1343 write_object_info (ns, (GIObjectInfo *)info, xml);
1346 case GI_INFO_TYPE_INTERFACE:
1347 write_interface_info (ns, (GIInterfaceInfo *)info, xml);
1350 case GI_INFO_TYPE_ERROR_DOMAIN:
1351 write_error_domain_info (ns, (GIErrorDomainInfo *)info, xml);
1355 g_error ("unknown info type %d\n", g_base_info_get_type (info));
1358 g_base_info_unref (info);
1361 xml_end_element (xml, "namespace");
1364 xml_end_element (xml, "repository");
1369 static const guchar *
1370 load_typelib (const gchar *filename,
1375 gsize *typelib_size;
1378 handle = g_module_open (filename, G_MODULE_BIND_LOCAL|G_MODULE_BIND_LAZY);
1381 g_printerr ("Could not load typelib from '%s': %s\n",
1382 filename, g_module_error ());
1386 if (!g_module_symbol (handle, "_G_TYPELIB", (gpointer *) &typelib))
1388 g_printerr ("Could not load typelib from '%s': %s\n",
1389 filename, g_module_error ());
1393 if (!g_module_symbol (handle, "_G_TYPELIB_SIZE", (gpointer *) &typelib_size))
1395 g_printerr ("Could not load typelib from '%s': %s\n",
1396 filename, g_module_error ());
1400 *len = *typelib_size;
1409 main (int argc, char *argv[])
1411 gboolean shlib = FALSE;
1412 gchar **input = NULL;
1413 GOptionContext *context;
1414 GError *error = NULL;
1415 gboolean needs_prefix;
1418 GOptionEntry options[] =
1420 { "shlib", 0, 0, G_OPTION_ARG_NONE, &shlib, "handle typelib embedded in shlib", NULL },
1421 { "output", 'o', 0, G_OPTION_ARG_FILENAME, &output, "output file", "FILE" },
1422 { "includedir", 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &includedirs, "include directories in GIR search path", NULL },
1423 { "all", 0, 0, G_OPTION_ARG_NONE, &show_all, "show all available information", NULL, },
1424 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &input, NULL, NULL },
1428 g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL);
1432 g_typelib_check_sanity ();
1434 context = g_option_context_new ("");
1435 g_option_context_add_main_entries (context, options, NULL);
1436 g_option_context_parse (context, &argc, &argv, &error);
1440 g_fprintf (stderr, "no input files\n");
1445 if (includedirs != NULL)
1446 for (i = 0; includedirs[i]; i++)
1447 g_irepository_prepend_search_path (includedirs[i]);
1449 for (i = 0; input[i]; i++)
1451 GModule *dlhandle = NULL;
1452 const guchar *typelib;
1454 const char *namespace;
1458 if (!g_file_get_contents (input[i], (gchar **)&typelib, &len, &error))
1460 g_fprintf (stderr, "failed to read '%s': %s\n",
1461 input[i], error->message);
1462 g_clear_error (&error);
1468 typelib = load_typelib (input[i], &dlhandle, &len);
1471 g_fprintf (stderr, "failed to load typelib from '%s'\n",
1477 if (input[i + 1] && output)
1478 needs_prefix = TRUE;
1480 needs_prefix = FALSE;
1482 data = g_typelib_new_from_const_memory (typelib, len);
1484 GError *error = NULL;
1485 if (!g_typelib_validate (data, &error)) {
1486 g_printerr ("typelib not valid: %s\n", error->message);
1487 g_clear_error (&error);
1491 namespace = g_irepository_load_typelib (g_irepository_get_default (), data, 0,
1493 if (namespace == NULL)
1495 g_printerr ("failed to load typelib: %s\n", error->message);
1499 write_repository (namespace, needs_prefix);
1503 g_module_close (dlhandle);
1507 /* when writing to stdout, stop after the first module */
1508 if (input[i + 1] && !output)
1510 g_fprintf (stderr, "warning, %d modules omitted\n",
1511 g_strv_length (input) - 1);