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_field_info (const gchar *namespace,
345 GIConstantInfo *branch,
349 GIFieldInfoFlags flags;
355 name = g_base_info_get_name ((GIBaseInfo *)info);
356 flags = g_field_info_get_flags (info);
357 size = g_field_info_get_size (info);
358 offset = g_field_info_get_offset (info);
360 xml_start_element (file, "field");
361 xml_printf (file, " name=\"%s\"", name);
363 /* Fields are assumed to be read-only
364 * (see also girwriter.py and girparser.c)
366 if (!(flags & GI_FIELD_IS_READABLE))
367 xml_printf (file, " readable=\"0\"");
368 if (flags & GI_FIELD_IS_WRITABLE)
369 xml_printf (file, " writable=\"1\"");
372 xml_printf (file, " bits=\"%d\"", size);
374 write_attributes (file, (GIBaseInfo*) info);
376 type = g_field_info_get_type (info);
380 xml_printf (file, " branch=\"");
381 type = g_constant_info_get_type (branch);
382 g_constant_info_get_value (branch, &value);
383 write_constant_value (namespace, type, &value, file);
384 xml_printf (file, "\"");
390 xml_printf (file, "offset=\"%d\"", offset);
393 write_type_info (namespace, type, file);
394 g_base_info_unref ((GIBaseInfo *)type);
396 xml_end_element (file, "field");
400 write_callable_info (const gchar *namespace,
401 GICallableInfo *info,
407 write_attributes (file, (GIBaseInfo*) info);
409 type = g_callable_info_get_return_type (info);
411 xml_start_element (file, "return-value");
413 switch (g_callable_info_get_caller_owns (info))
415 case GI_TRANSFER_NOTHING:
416 xml_printf (file, " transfer-ownership=\"none\"");
418 case GI_TRANSFER_CONTAINER:
419 xml_printf (file, " transfer-ownership=\"container\"");
421 case GI_TRANSFER_EVERYTHING:
422 xml_printf (file, " transfer-ownership=\"full\"");
425 g_assert_not_reached ();
428 if (g_callable_info_may_return_null (info))
429 xml_printf (file, " allow-none=\"1\"");
431 write_type_info (namespace, type, file);
433 xml_end_element (file, "return-value");
435 if (g_callable_info_get_n_args (info) <= 0)
438 xml_start_element (file, "parameters");
439 for (i = 0; i < g_callable_info_get_n_args (info); i++)
441 GIArgInfo *arg = g_callable_info_get_arg (info, i);
443 xml_start_element (file, "parameter");
444 xml_printf (file, " name=\"%s\"",
445 g_base_info_get_name ((GIBaseInfo *) arg));
447 switch (g_arg_info_get_ownership_transfer (arg))
449 case GI_TRANSFER_NOTHING:
450 xml_printf (file, " transfer-ownership=\"none\"");
452 case GI_TRANSFER_CONTAINER:
453 xml_printf (file, " transfer-ownership=\"container\"");
455 case GI_TRANSFER_EVERYTHING:
456 xml_printf (file, " transfer-ownership=\"full\"");
459 g_assert_not_reached ();
462 switch (g_arg_info_get_direction (arg))
464 case GI_DIRECTION_IN:
466 case GI_DIRECTION_OUT:
467 xml_printf (file, " direction=\"out\"");
469 case GI_DIRECTION_INOUT:
470 xml_printf (file, " direction=\"inout\"");
474 if (g_arg_info_may_be_null (arg))
475 xml_printf (file, " allow-none=\"1\"");
477 if (g_arg_info_is_dipper (arg))
478 xml_printf (file, " dipper=\"1\"");
480 if (g_arg_info_is_return_value (arg))
481 xml_printf (file, " retval=\"1\"");
483 if (g_arg_info_is_optional (arg))
484 xml_printf (file, " optional=\"1\"");
486 switch (g_arg_info_get_scope (arg))
488 case GI_SCOPE_TYPE_INVALID:
490 case GI_SCOPE_TYPE_CALL:
491 xml_printf (file, " scope=\"call\"");
493 case GI_SCOPE_TYPE_ASYNC:
494 xml_printf (file, " scope=\"async\"");
496 case GI_SCOPE_TYPE_NOTIFIED:
497 xml_printf (file, " scope=\"notified\"");
501 if (g_arg_info_get_closure (arg) >= 0)
502 xml_printf (file, " closure=\"%d\"", g_arg_info_get_closure (arg));
504 if (g_arg_info_get_destroy (arg) >= 0)
505 xml_printf (file, " destroy=\"%d\"", g_arg_info_get_destroy (arg));
507 type = g_arg_info_get_type (arg);
508 write_type_info (namespace, type, file);
510 xml_end_element (file, "parameter");
512 g_base_info_unref ((GIBaseInfo *)arg);
515 xml_end_element (file, "parameters");
516 g_base_info_unref ((GIBaseInfo *)type);
520 write_function_info (const gchar *namespace,
521 GIFunctionInfo *info,
524 GIFunctionInfoFlags flags;
531 flags = g_function_info_get_flags (info);
532 name = g_base_info_get_name ((GIBaseInfo *)info);
533 symbol = g_function_info_get_symbol (info);
534 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
535 throws = flags & GI_FUNCTION_THROWS;
537 if (flags & GI_FUNCTION_IS_CONSTRUCTOR)
539 else if (flags & GI_FUNCTION_IS_METHOD)
544 xml_start_element (file, tag);
545 xml_printf (file, " name=\"%s\" c:identifier=\"%s\"",
548 if (flags & GI_FUNCTION_IS_SETTER)
549 xml_printf (file, " type=\"setter\"");
550 else if (flags & GI_FUNCTION_IS_GETTER)
551 xml_printf (file, " type=\"getter\"");
554 xml_printf (file, " deprecated=\"1\"");
557 xml_printf (file, " throws=\"1\"");
559 write_callable_info (namespace, (GICallableInfo*)info, file);
560 xml_end_element (file, tag);
564 write_callback_info (const gchar *namespace,
565 GICallbackInfo *info,
571 name = g_base_info_get_name ((GIBaseInfo *)info);
572 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
574 xml_start_element (file, "callback");
575 xml_printf (file, " name=\"%s\"", name);
578 xml_printf (file, " deprecated=\"1\"");
580 write_callable_info (namespace, (GICallableInfo*)info, file);
581 xml_end_element (file, "callback");
585 write_struct_info (const gchar *namespace,
590 const gchar *type_name;
591 const gchar *type_init;
593 gboolean is_gtype_struct;
598 name = g_base_info_get_name ((GIBaseInfo *)info);
599 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
601 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
602 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
604 if (g_base_info_get_type ((GIBaseInfo *)info) == GI_INFO_TYPE_BOXED)
606 xml_start_element (file, "glib:boxed");
607 xml_printf (file, " glib:name=\"%s\"", name);
611 xml_start_element (file, "record");
612 xml_printf (file, " name=\"%s\"", name);
615 if (type_name != NULL)
616 xml_printf (file, " glib:type-name=\"%s\" glib:get-type=\"%s\"", type_name, type_init);
619 xml_printf (file, " deprecated=\"1\"");
621 is_gtype_struct = g_struct_info_is_gtype_struct (info);
623 xml_printf (file, " glib:is-gtype-struct=\"1\"");
625 write_attributes (file, (GIBaseInfo*) info);
627 size = g_struct_info_get_size (info);
628 if (show_all && size >= 0)
629 xml_printf (file, " size=\"%d\"", size);
631 n_elts = g_struct_info_get_n_fields (info) + g_struct_info_get_n_methods (info);
634 for (i = 0; i < g_struct_info_get_n_fields (info); i++)
636 GIFieldInfo *field = g_struct_info_get_field (info, i);
637 write_field_info (namespace, field, NULL, file);
638 g_base_info_unref ((GIBaseInfo *)field);
641 for (i = 0; i < g_struct_info_get_n_methods (info); i++)
643 GIFunctionInfo *function = g_struct_info_get_method (info, i);
644 write_function_info (namespace, function, file);
645 g_base_info_unref ((GIBaseInfo *)function);
650 xml_end_element_unchecked (file);
654 write_value_info (const gchar *namespace,
662 name = g_base_info_get_name ((GIBaseInfo *)info);
663 value = g_value_info_get_value (info);
664 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
666 xml_start_element (file, "member");
667 xml_printf (file, " name=\"%s\" value=\"%ld\"", name, value);
670 xml_printf (file, " deprecated=\"1\"");
672 write_attributes (file, (GIBaseInfo*) info);
674 xml_end_element (file, "member");
678 write_constant_value (const gchar *namespace,
683 switch (g_type_info_get_tag (type))
685 case GI_TYPE_TAG_BOOLEAN:
686 xml_printf (file, "%d", value->v_boolean);
688 case GI_TYPE_TAG_INT8:
689 xml_printf (file, "%d", value->v_int8);
691 case GI_TYPE_TAG_UINT8:
692 xml_printf (file, "%d", value->v_uint8);
694 case GI_TYPE_TAG_INT16:
695 xml_printf (file, "%" G_GINT16_FORMAT, value->v_int16);
697 case GI_TYPE_TAG_UINT16:
698 xml_printf (file, "%" G_GUINT16_FORMAT, value->v_uint16);
700 case GI_TYPE_TAG_INT32:
701 xml_printf (file, "%" G_GINT32_FORMAT, value->v_int32);
703 case GI_TYPE_TAG_UINT32:
704 xml_printf (file, "%" G_GUINT32_FORMAT, value->v_uint32);
706 case GI_TYPE_TAG_INT64:
707 xml_printf (file, "%" G_GINT64_FORMAT, value->v_int64);
709 case GI_TYPE_TAG_UINT64:
710 xml_printf (file, "%" G_GUINT64_FORMAT, value->v_uint64);
712 case GI_TYPE_TAG_INT:
713 xml_printf (file, "%d", value->v_int);
715 case GI_TYPE_TAG_UINT:
716 xml_printf (file, "%d", value->v_uint);
718 case GI_TYPE_TAG_LONG:
719 xml_printf (file, "%ld", value->v_long);
721 case GI_TYPE_TAG_ULONG:
722 xml_printf (file, "%ld", value->v_ulong);
724 case GI_TYPE_TAG_SSIZE:
725 xml_printf (file, "%zd", value->v_ssize);
727 case GI_TYPE_TAG_SIZE:
728 xml_printf (file, "%zd", value->v_size);
730 case GI_TYPE_TAG_FLOAT:
731 xml_printf (file, "%f", value->v_float);
733 case GI_TYPE_TAG_DOUBLE:
734 xml_printf (file, "%f", value->v_double);
736 case GI_TYPE_TAG_UTF8:
737 case GI_TYPE_TAG_FILENAME:
738 xml_printf (file, "%s", value->v_string);
741 g_assert_not_reached ();
746 write_constant_info (const gchar *namespace,
747 GIConstantInfo *info,
755 name = g_base_info_get_name ((GIBaseInfo *)info);
756 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
758 xml_start_element (file, "constant");
759 xml_printf (file, " name=\"%s\"", name);
761 type = g_constant_info_get_type (info);
762 xml_printf (file, " value=\"");
764 g_constant_info_get_value (info, &value);
765 write_constant_value (namespace, type, &value, file);
766 xml_printf (file, "\"");
768 write_type_info (namespace, type, file);
770 write_attributes (file, (GIBaseInfo*) info);
772 xml_end_element (file, "constant");
774 g_base_info_unref ((GIBaseInfo *)type);
779 write_enum_info (const gchar *namespace,
784 const gchar *type_name;
785 const gchar *type_init;
789 name = g_base_info_get_name ((GIBaseInfo *)info);
790 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
792 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
793 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
795 if (g_base_info_get_type ((GIBaseInfo *)info) == GI_INFO_TYPE_ENUM)
796 xml_start_element (file, "enumeration");
798 xml_start_element (file, "bitfield");
799 xml_printf (file, " name=\"%s\"", name);
802 xml_printf (file, " glib:type-name=\"%s\" glib:get-type=\"%s\"", type_name, type_init);
805 xml_printf (file, " deprecated=\"1\"");
807 write_attributes (file, (GIBaseInfo*) info);
809 for (i = 0; i < g_enum_info_get_n_values (info); i++)
811 GIValueInfo *value = g_enum_info_get_value (info, i);
812 write_value_info (namespace, value, file);
813 g_base_info_unref ((GIBaseInfo *)value);
816 xml_end_element_unchecked (file);
820 write_signal_info (const gchar *namespace,
828 name = g_base_info_get_name ((GIBaseInfo *)info);
829 flags = g_signal_info_get_flags (info);
830 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
832 xml_start_element (file, "glib:signal");
833 xml_printf (file, " name=\"%s\"", name);
836 xml_printf (file, " deprecated=\"1\"");
838 if (flags & G_SIGNAL_RUN_FIRST)
839 xml_printf (file, " when=\"FIRST\"");
840 else if (flags & G_SIGNAL_RUN_LAST)
841 xml_printf (file, " when=\"LAST\"");
842 else if (flags & G_SIGNAL_RUN_CLEANUP)
843 xml_printf (file, " when=\"CLEANUP\"");
845 if (flags & G_SIGNAL_NO_RECURSE)
846 xml_printf (file, " no-recurse=\"1\"");
848 if (flags & G_SIGNAL_DETAILED)
849 xml_printf (file, " detailed=\"1\"");
851 if (flags & G_SIGNAL_ACTION)
852 xml_printf (file, " action=\"1\"");
854 if (flags & G_SIGNAL_NO_HOOKS)
855 xml_printf (file, " no-hooks=\"1\"");
857 write_callable_info (namespace, (GICallableInfo*)info, file);
859 xml_end_element (file, "glib:signal");
863 write_vfunc_info (const gchar *namespace,
867 GIVFuncInfoFlags flags;
869 GIFunctionInfo *invoker;
873 name = g_base_info_get_name ((GIBaseInfo *)info);
874 flags = g_vfunc_info_get_flags (info);
875 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
876 offset = g_vfunc_info_get_offset (info);
877 invoker = g_vfunc_info_get_invoker (info);
879 xml_start_element (file, "virtual-method");
880 xml_printf (file, " name=\"%s\"", name);
883 xml_printf (file, " deprecated=\"1\"");
885 if (flags & GI_VFUNC_MUST_CHAIN_UP)
886 xml_printf (file, " must-chain-up=\"1\"");
888 if (flags & GI_VFUNC_MUST_OVERRIDE)
889 xml_printf (file, " override=\"always\"");
890 else if (flags & GI_VFUNC_MUST_NOT_OVERRIDE)
891 xml_printf (file, " override=\"never\"");
893 xml_printf (file, " offset=\"%d\"", offset);
896 xml_printf (file, " invoker=\"%s\"", g_base_info_get_name ((GIBaseInfo*)invoker));
898 write_callable_info (namespace, (GICallableInfo*)info, file);
900 xml_end_element (file, "virtual-method");
904 write_property_info (const gchar *namespace,
905 GIPropertyInfo *info,
913 name = g_base_info_get_name ((GIBaseInfo *)info);
914 flags = g_property_info_get_flags (info);
915 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
917 xml_start_element (file, "property");
918 xml_printf (file, " name=\"%s\"", name);
921 xml_printf (file, " deprecated=\"1\"");
923 /* Properties are assumed to be read-only (see also girwriter.py) */
924 if (!(flags & G_PARAM_READABLE))
925 xml_printf (file, " readable=\"0\"");
926 if (flags & G_PARAM_WRITABLE)
927 xml_printf (file, " writable=\"1\"");
929 if (flags & G_PARAM_CONSTRUCT)
930 xml_printf (file, " construct=\"1\"");
932 if (flags & G_PARAM_CONSTRUCT_ONLY)
933 xml_printf (file, " construct-only=\"1\"");
935 write_attributes (file, (GIBaseInfo*) info);
937 type = g_property_info_get_type (info);
939 write_type_info (namespace, type, file);
941 xml_end_element (file, "property");
945 write_object_info (const gchar *namespace,
950 const gchar *type_name;
951 const gchar *type_init;
953 gboolean is_abstract;
955 GIStructInfo *class_struct;
958 name = g_base_info_get_name ((GIBaseInfo *)info);
959 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
960 is_abstract = g_object_info_get_abstract (info);
962 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
963 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
964 xml_start_element (file, "class");
965 xml_printf (file, " name=\"%s\"", name);
967 pnode = g_object_info_get_parent (info);
970 write_type_name_attribute (namespace, (GIBaseInfo *)pnode, "parent", file);
971 g_base_info_unref ((GIBaseInfo *)pnode);
974 class_struct = g_object_info_get_class_struct (info);
977 write_type_name_attribute (namespace, (GIBaseInfo*) class_struct, "glib:type-struct", file);
978 g_base_info_unref ((GIBaseInfo*)class_struct);
982 xml_printf (file, " abstract=\"1\"");
984 xml_printf (file, " glib:type-name=\"%s\" glib:get-type=\"%s\"", type_name, type_init);
987 xml_printf (file, " deprecated=\"1\"");
989 write_attributes (file, (GIBaseInfo*) info);
991 if (g_object_info_get_n_interfaces (info) > 0)
993 for (i = 0; i < g_object_info_get_n_interfaces (info); i++)
995 GIInterfaceInfo *imp = g_object_info_get_interface (info, i);
996 xml_start_element (file, "implements");
997 write_type_name_attribute (namespace, (GIBaseInfo *)imp, "name", file);
998 xml_end_element (file, "implements");
999 g_base_info_unref ((GIBaseInfo*)imp);
1003 for (i = 0; i < g_object_info_get_n_fields (info); i++)
1005 GIFieldInfo *field = g_object_info_get_field (info, i);
1006 write_field_info (namespace, field, NULL, file);
1007 g_base_info_unref ((GIBaseInfo *)field);
1010 for (i = 0; i < g_object_info_get_n_methods (info); i++)
1012 GIFunctionInfo *function = g_object_info_get_method (info, i);
1013 write_function_info (namespace, function, file);
1014 g_base_info_unref ((GIBaseInfo *)function);
1017 for (i = 0; i < g_object_info_get_n_properties (info); i++)
1019 GIPropertyInfo *prop = g_object_info_get_property (info, i);
1020 write_property_info (namespace, prop, file);
1021 g_base_info_unref ((GIBaseInfo *)prop);
1024 for (i = 0; i < g_object_info_get_n_signals (info); i++)
1026 GISignalInfo *signal = g_object_info_get_signal (info, i);
1027 write_signal_info (namespace, signal, file);
1028 g_base_info_unref ((GIBaseInfo *)signal);
1031 for (i = 0; i < g_object_info_get_n_vfuncs (info); i++)
1033 GIVFuncInfo *vfunc = g_object_info_get_vfunc (info, i);
1034 write_vfunc_info (namespace, vfunc, file);
1035 g_base_info_unref ((GIBaseInfo *)vfunc);
1038 for (i = 0; i < g_object_info_get_n_constants (info); i++)
1040 GIConstantInfo *constant = g_object_info_get_constant (info, i);
1041 write_constant_info (namespace, constant, file);
1042 g_base_info_unref ((GIBaseInfo *)constant);
1045 xml_end_element (file, "class");
1049 write_interface_info (const gchar *namespace,
1050 GIInterfaceInfo *info,
1054 const gchar *type_name;
1055 const gchar *type_init;
1056 GIStructInfo *class_struct;
1057 gboolean deprecated;
1060 name = g_base_info_get_name ((GIBaseInfo *)info);
1061 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
1063 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
1064 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
1065 xml_start_element (file, "interface");
1066 xml_printf (file, " name=\"%s\" glib:type-name=\"%s\" glib:get-type=\"%s\"",
1067 name, type_name, type_init);
1069 class_struct = g_interface_info_get_iface_struct (info);
1072 write_type_name_attribute (namespace, (GIBaseInfo*) class_struct, "glib:type-struct", file);
1073 g_base_info_unref ((GIBaseInfo*)class_struct);
1077 xml_printf (file, " deprecated=\"1\"");
1079 write_attributes (file, (GIBaseInfo*) info);
1081 if (g_interface_info_get_n_prerequisites (info) > 0)
1083 for (i = 0; i < g_interface_info_get_n_prerequisites (info); i++)
1085 GIBaseInfo *req = g_interface_info_get_prerequisite (info, i);
1087 xml_start_element (file, "prerequisite");
1088 write_type_name_attribute (namespace, req, "name", file);
1090 xml_end_element_unchecked (file);
1091 g_base_info_unref (req);
1095 for (i = 0; i < g_interface_info_get_n_methods (info); i++)
1097 GIFunctionInfo *function = g_interface_info_get_method (info, i);
1098 write_function_info (namespace, function, file);
1099 g_base_info_unref ((GIBaseInfo *)function);
1102 for (i = 0; i < g_interface_info_get_n_properties (info); i++)
1104 GIPropertyInfo *prop = g_interface_info_get_property (info, i);
1105 write_property_info (namespace, prop, file);
1106 g_base_info_unref ((GIBaseInfo *)prop);
1109 for (i = 0; i < g_interface_info_get_n_signals (info); i++)
1111 GISignalInfo *signal = g_interface_info_get_signal (info, i);
1112 write_signal_info (namespace, signal, file);
1113 g_base_info_unref ((GIBaseInfo *)signal);
1116 for (i = 0; i < g_interface_info_get_n_vfuncs (info); i++)
1118 GIVFuncInfo *vfunc = g_interface_info_get_vfunc (info, i);
1119 write_vfunc_info (namespace, vfunc, file);
1120 g_base_info_unref ((GIBaseInfo *)vfunc);
1123 for (i = 0; i < g_interface_info_get_n_constants (info); i++)
1125 GIConstantInfo *constant = g_interface_info_get_constant (info, i);
1126 write_constant_info (namespace, constant, file);
1127 g_base_info_unref ((GIBaseInfo *)constant);
1130 xml_end_element (file, "interface");
1134 write_error_domain_info (const gchar *namespace,
1135 GIErrorDomainInfo *info,
1139 const gchar *name, *quark;
1141 name = g_base_info_get_name ((GIBaseInfo *)info);
1142 quark = g_error_domain_info_get_quark (info);
1143 enum_ = (GIBaseInfo *)g_error_domain_info_get_codes (info);
1144 xml_start_element (file, "errordomain");
1145 xml_printf (file, " name=\"%s\" get-quark=\"%s\"",
1147 write_type_name_attribute (namespace, enum_, "codes", file);
1148 xml_end_element (file, "errordomain");
1149 g_base_info_unref (enum_);
1153 write_union_info (const gchar *namespace,
1158 const gchar *type_name;
1159 const gchar *type_init;
1160 gboolean deprecated;
1164 name = g_base_info_get_name ((GIBaseInfo *)info);
1165 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
1167 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
1168 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
1170 xml_start_element (file, "union");
1171 xml_printf (file, " name=\"%s\"", name);
1174 xml_printf (file, " type-name=\"%s\" get-type=\"%s\"", type_name, type_init);
1177 xml_printf (file, " deprecated=\"1\"");
1179 size = g_union_info_get_size (info);
1180 if (show_all && size >= 0)
1181 xml_printf (file, " size=\"%d\"", size);
1183 write_attributes (file, (GIBaseInfo*) info);
1185 if (g_union_info_is_discriminated (info))
1190 offset = g_union_info_get_discriminator_offset (info);
1191 type = g_union_info_get_discriminator_type (info);
1193 xml_start_element (file, "discriminator");
1194 xml_printf (file, " offset=\"%d\" type=\"", offset);
1195 write_type_info (namespace, type, file);
1196 xml_end_element (file, "discriminator");
1197 g_base_info_unref ((GIBaseInfo *)type);
1200 for (i = 0; i < g_union_info_get_n_fields (info); i++)
1202 GIFieldInfo *field = g_union_info_get_field (info, i);
1203 GIConstantInfo *constant = g_union_info_get_discriminator (info, i);
1204 write_field_info (namespace, field, constant, file);
1205 g_base_info_unref ((GIBaseInfo *)field);
1207 g_base_info_unref ((GIBaseInfo *)constant);
1210 for (i = 0; i < g_union_info_get_n_methods (info); i++)
1212 GIFunctionInfo *function = g_union_info_get_method (info, i);
1213 write_function_info (namespace, function, file);
1214 g_base_info_unref ((GIBaseInfo *)function);
1217 xml_end_element (file, "union");
1221 write_repository (const char *namespace,
1222 gboolean needs_prefix)
1226 char **dependencies;
1227 GIRepository *repository;
1230 repository = g_irepository_get_default ();
1239 filename = g_strdup_printf ("%s-%s", namespace, output);
1241 filename = g_strdup (output);
1242 ofile = g_fopen (filename, "w");
1246 g_fprintf (stderr, "failed to open '%s': %s\n",
1247 filename, g_strerror (errno));
1256 xml = xml_open (ofile);
1258 xml_printf (xml, "<?xml version=\"1.0\"?>\n");
1259 xml_start_element (xml, "repository");
1260 xml_printf (xml, " version=\"1.0\"\n"
1261 " xmlns=\"http://www.gtk.org/introspection/core/1.0\"\n"
1262 " xmlns:c=\"http://www.gtk.org/introspection/c/1.0\"\n"
1263 " xmlns:glib=\"http://www.gtk.org/introspection/glib/1.0\"");
1265 dependencies = g_irepository_get_dependencies (repository,
1267 if (dependencies != NULL)
1269 for (i = 0; dependencies[i]; i++)
1271 char **parts = g_strsplit (dependencies[i], "-", 2);
1272 xml_start_element (xml, "include");
1273 xml_printf (xml, " name=\"%s\" version=\"%s\"", parts[0], parts[1]);
1274 xml_end_element (xml, "include");
1281 const gchar *shared_library;
1282 const gchar *c_prefix;
1283 const char *ns = namespace;
1284 const char *version;
1286 version = g_irepository_get_version (repository, ns);
1288 shared_library = g_irepository_get_shared_library (repository, ns);
1289 c_prefix = g_irepository_get_c_prefix (repository, ns);
1290 xml_start_element (xml, "namespace");
1291 xml_printf (xml, " name=\"%s\" version=\"%s\"", ns, version);
1293 xml_printf (xml, " shared-library=\"%s\"", shared_library);
1295 xml_printf (xml, " c:prefix=\"%s\"", c_prefix);
1297 for (j = 0; j < g_irepository_get_n_infos (repository, ns); j++)
1299 GIBaseInfo *info = g_irepository_get_info (repository, ns, j);
1300 switch (g_base_info_get_type (info))
1302 case GI_INFO_TYPE_FUNCTION:
1303 write_function_info (ns, (GIFunctionInfo *)info, xml);
1306 case GI_INFO_TYPE_CALLBACK:
1307 write_callback_info (ns, (GICallbackInfo *)info, xml);
1310 case GI_INFO_TYPE_STRUCT:
1311 case GI_INFO_TYPE_BOXED:
1312 write_struct_info (ns, (GIStructInfo *)info, xml);
1315 case GI_INFO_TYPE_UNION:
1316 write_union_info (ns, (GIUnionInfo *)info, xml);
1319 case GI_INFO_TYPE_ENUM:
1320 case GI_INFO_TYPE_FLAGS:
1321 write_enum_info (ns, (GIEnumInfo *)info, xml);
1324 case GI_INFO_TYPE_CONSTANT:
1325 write_constant_info (ns, (GIConstantInfo *)info, xml);
1328 case GI_INFO_TYPE_OBJECT:
1329 write_object_info (ns, (GIObjectInfo *)info, xml);
1332 case GI_INFO_TYPE_INTERFACE:
1333 write_interface_info (ns, (GIInterfaceInfo *)info, xml);
1336 case GI_INFO_TYPE_ERROR_DOMAIN:
1337 write_error_domain_info (ns, (GIErrorDomainInfo *)info, xml);
1341 g_error ("unknown info type %d\n", g_base_info_get_type (info));
1344 g_base_info_unref (info);
1347 xml_end_element (xml, "namespace");
1350 xml_end_element (xml, "repository");
1355 static const guchar *
1356 load_typelib (const gchar *filename,
1361 gsize *typelib_size;
1364 handle = g_module_open (filename, G_MODULE_BIND_LOCAL|G_MODULE_BIND_LAZY);
1367 g_printerr ("Could not load typelib from '%s': %s\n",
1368 filename, g_module_error ());
1372 if (!g_module_symbol (handle, "_G_TYPELIB", (gpointer *) &typelib))
1374 g_printerr ("Could not load typelib from '%s': %s\n",
1375 filename, g_module_error ());
1379 if (!g_module_symbol (handle, "_G_TYPELIB_SIZE", (gpointer *) &typelib_size))
1381 g_printerr ("Could not load typelib from '%s': %s\n",
1382 filename, g_module_error ());
1386 *len = *typelib_size;
1395 main (int argc, char *argv[])
1397 gboolean shlib = FALSE;
1398 gchar **input = NULL;
1399 GOptionContext *context;
1400 GError *error = NULL;
1401 gboolean needs_prefix;
1404 GOptionEntry options[] =
1406 { "shlib", 0, 0, G_OPTION_ARG_NONE, &shlib, "handle typelib embedded in shlib", NULL },
1407 { "output", 'o', 0, G_OPTION_ARG_FILENAME, &output, "output file", "FILE" },
1408 { "includedir", 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &includedirs, "include directories in GIR search path", NULL },
1409 { "all", 0, 0, G_OPTION_ARG_NONE, &show_all, "show all available information", NULL, },
1410 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &input, NULL, NULL },
1414 g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL);
1418 g_typelib_check_sanity ();
1420 context = g_option_context_new ("");
1421 g_option_context_add_main_entries (context, options, NULL);
1422 g_option_context_parse (context, &argc, &argv, &error);
1426 g_fprintf (stderr, "no input files\n");
1431 if (includedirs != NULL)
1432 for (i = 0; includedirs[i]; i++)
1433 g_irepository_prepend_search_path (includedirs[i]);
1435 for (i = 0; input[i]; i++)
1437 GModule *dlhandle = NULL;
1438 const guchar *typelib;
1440 const char *namespace;
1444 if (!g_file_get_contents (input[i], (gchar **)&typelib, &len, &error))
1446 g_fprintf (stderr, "failed to read '%s': %s\n",
1447 input[i], error->message);
1448 g_clear_error (&error);
1454 typelib = load_typelib (input[i], &dlhandle, &len);
1457 g_fprintf (stderr, "failed to load typelib from '%s'\n",
1463 if (input[i + 1] && output)
1464 needs_prefix = TRUE;
1466 needs_prefix = FALSE;
1468 data = g_typelib_new_from_const_memory (typelib, len);
1470 GError *error = NULL;
1471 if (!g_typelib_validate (data, &error)) {
1472 g_printerr ("typelib not valid: %s\n", error->message);
1473 g_clear_error (&error);
1477 namespace = g_irepository_load_typelib (g_irepository_get_default (), data, 0,
1479 if (namespace == NULL)
1481 g_printerr ("failed to load typelib: %s\n", error->message);
1485 write_repository (namespace, needs_prefix);
1489 g_module_close (dlhandle);
1493 /* when writing to stdout, stop after the first module */
1494 if (input[i + 1] && !output)
1496 g_fprintf (stderr, "warning, %d modules omitted\n",
1497 g_strv_length (input) - 1);