2 /* -*- Mode: C; c-file-style: "gnu"; -*- */
3 /* GObject introspection: IDL generator
5 * Copyright (C) 2005 Matthias Clasen
6 * Copyright (C) 2008,2009 Red Hat, Inc.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
28 #include <glib-object.h>
29 #include <glib/gstdio.h>
31 #include "girepository.h"
34 /* FIXME: Avoid global */
35 static gchar *output = NULL;
36 gchar **includedirs = NULL;
37 static gboolean show_all = FALSE;
46 guint has_children : 1;
50 xml_element_new (const char *name)
54 elem = g_new (XmlElement, 1);
55 elem->name = g_strdup (name);
56 elem->has_children = FALSE;
61 xml_element_free (XmlElement *elem)
68 xml_printf (Xml *xml, const char *fmt, ...)
74 s = g_markup_vprintf_escaped (fmt, ap);
81 xml_start_element (Xml *xml, const char *element_name)
83 XmlElement *parent = NULL;
87 parent = xml->stack->data;
89 if (!parent->has_children)
90 xml_printf (xml, ">\n");
92 parent->has_children = TRUE;
95 xml_printf (xml, "%*s<%s", g_slist_length(xml->stack)*2, "", element_name);
97 xml->stack = g_slist_prepend (xml->stack, xml_element_new (element_name));
101 xml_end_element (Xml *xml, const char *name)
105 g_assert (xml->stack != NULL);
107 elem = xml->stack->data;
108 xml->stack = g_slist_delete_link (xml->stack, xml->stack);
111 g_assert_cmpstr (name, ==, elem->name);
113 if (elem->has_children)
114 xml_printf (xml, "%*s</%s>\n", g_slist_length (xml->stack)*2, "", elem->name);
116 xml_printf (xml, "/>\n");
118 xml_element_free (elem);
122 xml_end_element_unchecked (Xml *xml)
124 xml_end_element (xml, NULL);
128 xml_open (FILE *file)
132 xml = g_new (Xml, 1);
142 g_assert (xml->stack == NULL);
143 if (xml->file != NULL)
146 if (xml->file != stdout)
161 check_unresolved (GIBaseInfo *info)
163 if (g_base_info_get_type (info) != GI_INFO_TYPE_UNRESOLVED)
166 g_critical ("Found unresolved type '%s' '%s'\n",
167 g_base_info_get_name (info), g_base_info_get_namespace (info));
171 write_type_name (const gchar *namespace,
175 if (strcmp (namespace, g_base_info_get_namespace (info)) != 0)
176 xml_printf (file, "%s.", g_base_info_get_namespace (info));
178 xml_printf (file, "%s", g_base_info_get_name (info));
182 write_type_name_attribute (const gchar *namespace,
184 const char *attr_name,
187 xml_printf (file, " %s=\"", attr_name);
188 write_type_name (namespace, info, file);
189 xml_printf (file, "\"");
193 write_type_info (const gchar *namespace,
202 check_unresolved ((GIBaseInfo*)info);
204 tag = g_type_info_get_tag (info);
205 is_pointer = g_type_info_is_pointer (info);
207 if (tag == GI_TYPE_TAG_VOID)
209 xml_start_element (file, "type");
211 xml_printf (file, " name=\"%s\"", is_pointer ? "any" : "none");
213 xml_end_element (file, "type");
215 else if (G_TYPE_TAG_IS_BASIC (tag))
217 xml_start_element (file, "type");
218 xml_printf (file, " name=\"%s\"", g_type_tag_to_string (tag));
219 xml_end_element (file, "type");
221 else if (tag == GI_TYPE_TAG_ARRAY)
225 xml_start_element (file, "array");
227 type = g_type_info_get_param_type (info, 0);
229 length = g_type_info_get_array_length (info);
231 xml_printf (file, " length=\"%d\"", length);
233 size = g_type_info_get_array_fixed_size (info);
235 xml_printf (file, " fixed-size=\"%d\"", size);
237 if (g_type_info_is_zero_terminated (info))
238 xml_printf (file, " zero-terminated=\"1\"");
240 write_type_info (namespace, type, file);
242 g_base_info_unref ((GIBaseInfo *)type);
244 xml_end_element (file, "array");
246 else if (tag == GI_TYPE_TAG_INTERFACE)
248 GIBaseInfo *iface = g_type_info_get_interface (info);
249 xml_start_element (file, "type");
250 write_type_name_attribute (namespace, iface, "name", file);
251 xml_end_element (file, "type");
252 g_base_info_unref (iface);
254 else if (tag == GI_TYPE_TAG_GLIST)
256 xml_start_element (file, "type");
257 xml_printf (file, " name=\"GLib.List\"");
258 type = g_type_info_get_param_type (info, 0);
261 write_type_info (namespace, type, file);
262 g_base_info_unref ((GIBaseInfo *)type);
264 xml_end_element (file, "type");
266 else if (tag == GI_TYPE_TAG_GSLIST)
268 xml_start_element (file, "type");
269 xml_printf (file, " name=\"GLib.SList\"");
270 type = g_type_info_get_param_type (info, 0);
273 write_type_info (namespace, type, file);
274 g_base_info_unref ((GIBaseInfo *)type);
276 xml_end_element (file, "type");
278 else if (tag == GI_TYPE_TAG_GHASH)
280 xml_start_element (file, "type");
281 xml_printf (file, " name=\"GLib.HashTable\"");
282 type = g_type_info_get_param_type (info, 0);
285 write_type_info (namespace, type, file);
286 g_base_info_unref ((GIBaseInfo *)type);
287 type = g_type_info_get_param_type (info, 1);
288 write_type_info (namespace, type, file);
289 g_base_info_unref ((GIBaseInfo *)type);
291 xml_end_element (file, "type");
293 else if (tag == GI_TYPE_TAG_ERROR)
297 xml_start_element (file, "type");
298 xml_printf (file, " name=\"GLib.Error\"");
300 n = g_type_info_get_n_error_domains (info);
303 for (i = 0; i < n; i++)
305 GIErrorDomainInfo *ed = g_type_info_get_error_domain (info, i);
306 xml_start_element (file, "type");
307 write_type_name_attribute (namespace, (GIBaseInfo *)ed, "name", file);
308 xml_end_element (file, "type");
309 g_base_info_unref ((GIBaseInfo *)ed);
313 xml_end_element (file, "type");
317 g_printerr ("Unhandled type tag %d\n", tag);
318 g_assert_not_reached ();
323 write_attributes (Xml *file,
326 GIAttributeIter iter = { 0, };
329 while (g_base_info_iterate_attributes (info, &iter, &name, &value))
331 xml_start_element (file, "attribute");
332 xml_printf (file, " name=\"%s\" value=\"%s\"", name, value);
333 xml_end_element (file, "attribute");
338 write_constant_value (const gchar *namespace,
344 write_callback_info (const gchar *namespace,
345 GICallbackInfo *info,
349 write_field_info (const gchar *namespace,
351 GIConstantInfo *branch,
355 GIFieldInfoFlags flags;
359 GIBaseInfo *interface;
362 name = g_base_info_get_name ((GIBaseInfo *)info);
363 flags = g_field_info_get_flags (info);
364 size = g_field_info_get_size (info);
365 offset = g_field_info_get_offset (info);
367 xml_start_element (file, "field");
368 xml_printf (file, " name=\"%s\"", name);
370 /* Fields are assumed to be read-only
371 * (see also girwriter.py and girparser.c)
373 if (!(flags & GI_FIELD_IS_READABLE))
374 xml_printf (file, " readable=\"0\"");
375 if (flags & GI_FIELD_IS_WRITABLE)
376 xml_printf (file, " writable=\"1\"");
379 xml_printf (file, " bits=\"%d\"", size);
381 write_attributes (file, (GIBaseInfo*) info);
383 type = g_field_info_get_type (info);
387 xml_printf (file, " branch=\"");
388 type = g_constant_info_get_type (branch);
389 g_constant_info_get_value (branch, &value);
390 write_constant_value (namespace, type, &value, file);
391 xml_printf (file, "\"");
397 xml_printf (file, "offset=\"%d\"", offset);
400 interface = g_type_info_get_interface (type);
401 if (interface && g_base_info_get_type(interface) == GI_INFO_TYPE_CALLBACK)
402 write_callback_info (namespace, (GICallbackInfo *)interface, file);
404 write_type_info (namespace, type, file);
407 g_base_info_unref (interface);
409 g_base_info_unref ((GIBaseInfo *)type);
411 xml_end_element (file, "field");
415 write_callable_info (const gchar *namespace,
416 GICallableInfo *info,
422 write_attributes (file, (GIBaseInfo*) info);
424 type = g_callable_info_get_return_type (info);
426 xml_start_element (file, "return-value");
428 switch (g_callable_info_get_caller_owns (info))
430 case GI_TRANSFER_NOTHING:
431 xml_printf (file, " transfer-ownership=\"none\"");
433 case GI_TRANSFER_CONTAINER:
434 xml_printf (file, " transfer-ownership=\"container\"");
436 case GI_TRANSFER_EVERYTHING:
437 xml_printf (file, " transfer-ownership=\"full\"");
440 g_assert_not_reached ();
443 if (g_callable_info_may_return_null (info))
444 xml_printf (file, " allow-none=\"1\"");
446 write_type_info (namespace, type, file);
448 xml_end_element (file, "return-value");
450 if (g_callable_info_get_n_args (info) <= 0)
453 xml_start_element (file, "parameters");
454 for (i = 0; i < g_callable_info_get_n_args (info); i++)
456 GIArgInfo *arg = g_callable_info_get_arg (info, i);
458 xml_start_element (file, "parameter");
459 xml_printf (file, " name=\"%s\"",
460 g_base_info_get_name ((GIBaseInfo *) arg));
462 switch (g_arg_info_get_ownership_transfer (arg))
464 case GI_TRANSFER_NOTHING:
465 xml_printf (file, " transfer-ownership=\"none\"");
467 case GI_TRANSFER_CONTAINER:
468 xml_printf (file, " transfer-ownership=\"container\"");
470 case GI_TRANSFER_EVERYTHING:
471 xml_printf (file, " transfer-ownership=\"full\"");
474 g_assert_not_reached ();
477 switch (g_arg_info_get_direction (arg))
479 case GI_DIRECTION_IN:
481 case GI_DIRECTION_OUT:
482 xml_printf (file, " direction=\"out\"");
484 case GI_DIRECTION_INOUT:
485 xml_printf (file, " direction=\"inout\"");
489 if (g_arg_info_may_be_null (arg))
490 xml_printf (file, " allow-none=\"1\"");
492 if (g_arg_info_is_dipper (arg))
493 xml_printf (file, " dipper=\"1\"");
495 if (g_arg_info_is_return_value (arg))
496 xml_printf (file, " retval=\"1\"");
498 if (g_arg_info_is_optional (arg))
499 xml_printf (file, " optional=\"1\"");
501 switch (g_arg_info_get_scope (arg))
503 case GI_SCOPE_TYPE_INVALID:
505 case GI_SCOPE_TYPE_CALL:
506 xml_printf (file, " scope=\"call\"");
508 case GI_SCOPE_TYPE_ASYNC:
509 xml_printf (file, " scope=\"async\"");
511 case GI_SCOPE_TYPE_NOTIFIED:
512 xml_printf (file, " scope=\"notified\"");
516 if (g_arg_info_get_closure (arg) >= 0)
517 xml_printf (file, " closure=\"%d\"", g_arg_info_get_closure (arg));
519 if (g_arg_info_get_destroy (arg) >= 0)
520 xml_printf (file, " destroy=\"%d\"", g_arg_info_get_destroy (arg));
522 type = g_arg_info_get_type (arg);
523 write_type_info (namespace, type, file);
525 xml_end_element (file, "parameter");
527 g_base_info_unref ((GIBaseInfo *)arg);
530 xml_end_element (file, "parameters");
531 g_base_info_unref ((GIBaseInfo *)type);
535 write_function_info (const gchar *namespace,
536 GIFunctionInfo *info,
539 GIFunctionInfoFlags flags;
546 flags = g_function_info_get_flags (info);
547 name = g_base_info_get_name ((GIBaseInfo *)info);
548 symbol = g_function_info_get_symbol (info);
549 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
550 throws = flags & GI_FUNCTION_THROWS;
552 if (flags & GI_FUNCTION_IS_CONSTRUCTOR)
554 else if (flags & GI_FUNCTION_IS_METHOD)
559 xml_start_element (file, tag);
560 xml_printf (file, " name=\"%s\" c:identifier=\"%s\"",
563 if (flags & GI_FUNCTION_IS_SETTER)
564 xml_printf (file, " type=\"setter\"");
565 else if (flags & GI_FUNCTION_IS_GETTER)
566 xml_printf (file, " type=\"getter\"");
569 xml_printf (file, " deprecated=\"1\"");
572 xml_printf (file, " throws=\"1\"");
574 write_callable_info (namespace, (GICallableInfo*)info, file);
575 xml_end_element (file, tag);
579 write_callback_info (const gchar *namespace,
580 GICallbackInfo *info,
586 name = g_base_info_get_name ((GIBaseInfo *)info);
587 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
589 xml_start_element (file, "callback");
590 xml_printf (file, " name=\"%s\"", name);
593 xml_printf (file, " deprecated=\"1\"");
595 write_callable_info (namespace, (GICallableInfo*)info, file);
596 xml_end_element (file, "callback");
600 write_struct_info (const gchar *namespace,
605 const gchar *type_name;
606 const gchar *type_init;
608 gboolean is_gtype_struct;
614 name = g_base_info_get_name ((GIBaseInfo *)info);
615 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
617 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
618 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
620 if (g_base_info_get_type ((GIBaseInfo *)info) == GI_INFO_TYPE_BOXED)
622 xml_start_element (file, "glib:boxed");
623 xml_printf (file, " glib:name=\"%s\"", name);
627 xml_start_element (file, "record");
628 xml_printf (file, " name=\"%s\"", name);
631 if (type_name != NULL)
632 xml_printf (file, " glib:type-name=\"%s\" glib:get-type=\"%s\"", type_name, type_init);
635 xml_printf (file, " deprecated=\"1\"");
637 is_gtype_struct = g_struct_info_is_gtype_struct (info);
639 xml_printf (file, " glib:is-gtype-struct=\"1\"");
641 write_attributes (file, (GIBaseInfo*) info);
643 size = g_struct_info_get_size (info);
644 if (show_all && size >= 0)
645 xml_printf (file, " size=\"%d\"", size);
647 foreign = g_struct_info_is_foreign (info);
649 xml_printf (file, " foreign=\"1\"");
651 n_elts = g_struct_info_get_n_fields (info) + g_struct_info_get_n_methods (info);
654 for (i = 0; i < g_struct_info_get_n_fields (info); i++)
656 GIFieldInfo *field = g_struct_info_get_field (info, i);
657 write_field_info (namespace, field, NULL, file);
658 g_base_info_unref ((GIBaseInfo *)field);
661 for (i = 0; i < g_struct_info_get_n_methods (info); i++)
663 GIFunctionInfo *function = g_struct_info_get_method (info, i);
664 write_function_info (namespace, function, file);
665 g_base_info_unref ((GIBaseInfo *)function);
670 xml_end_element_unchecked (file);
674 write_value_info (const gchar *namespace,
682 name = g_base_info_get_name ((GIBaseInfo *)info);
683 value = g_value_info_get_value (info);
684 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
686 xml_start_element (file, "member");
687 xml_printf (file, " name=\"%s\" value=\"%ld\"", name, value);
690 xml_printf (file, " deprecated=\"1\"");
692 write_attributes (file, (GIBaseInfo*) info);
694 xml_end_element (file, "member");
698 write_constant_value (const gchar *namespace,
703 switch (g_type_info_get_tag (type))
705 case GI_TYPE_TAG_BOOLEAN:
706 xml_printf (file, "%d", value->v_boolean);
708 case GI_TYPE_TAG_INT8:
709 xml_printf (file, "%d", value->v_int8);
711 case GI_TYPE_TAG_UINT8:
712 xml_printf (file, "%d", value->v_uint8);
714 case GI_TYPE_TAG_INT16:
715 xml_printf (file, "%" G_GINT16_FORMAT, value->v_int16);
717 case GI_TYPE_TAG_UINT16:
718 xml_printf (file, "%" G_GUINT16_FORMAT, value->v_uint16);
720 case GI_TYPE_TAG_INT32:
721 xml_printf (file, "%" G_GINT32_FORMAT, value->v_int32);
723 case GI_TYPE_TAG_UINT32:
724 xml_printf (file, "%" G_GUINT32_FORMAT, value->v_uint32);
726 case GI_TYPE_TAG_INT64:
727 xml_printf (file, "%" G_GINT64_FORMAT, value->v_int64);
729 case GI_TYPE_TAG_UINT64:
730 xml_printf (file, "%" G_GUINT64_FORMAT, value->v_uint64);
732 case GI_TYPE_TAG_INT:
733 xml_printf (file, "%d", value->v_int);
735 case GI_TYPE_TAG_UINT:
736 xml_printf (file, "%d", value->v_uint);
738 case GI_TYPE_TAG_LONG:
739 xml_printf (file, "%ld", value->v_long);
741 case GI_TYPE_TAG_ULONG:
742 xml_printf (file, "%ld", value->v_ulong);
744 case GI_TYPE_TAG_SSIZE:
745 xml_printf (file, "%zd", value->v_ssize);
747 case GI_TYPE_TAG_SIZE:
748 xml_printf (file, "%zd", value->v_size);
750 case GI_TYPE_TAG_FLOAT:
751 xml_printf (file, "%f", value->v_float);
753 case GI_TYPE_TAG_DOUBLE:
754 xml_printf (file, "%f", value->v_double);
756 case GI_TYPE_TAG_UTF8:
757 case GI_TYPE_TAG_FILENAME:
758 xml_printf (file, "%s", value->v_string);
761 g_assert_not_reached ();
766 write_constant_info (const gchar *namespace,
767 GIConstantInfo *info,
775 name = g_base_info_get_name ((GIBaseInfo *)info);
776 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
778 xml_start_element (file, "constant");
779 xml_printf (file, " name=\"%s\"", name);
781 type = g_constant_info_get_type (info);
782 xml_printf (file, " value=\"");
784 g_constant_info_get_value (info, &value);
785 write_constant_value (namespace, type, &value, file);
786 xml_printf (file, "\"");
788 write_type_info (namespace, type, file);
790 write_attributes (file, (GIBaseInfo*) info);
792 xml_end_element (file, "constant");
794 g_base_info_unref ((GIBaseInfo *)type);
799 write_enum_info (const gchar *namespace,
804 const gchar *type_name;
805 const gchar *type_init;
809 name = g_base_info_get_name ((GIBaseInfo *)info);
810 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
812 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
813 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
815 if (g_base_info_get_type ((GIBaseInfo *)info) == GI_INFO_TYPE_ENUM)
816 xml_start_element (file, "enumeration");
818 xml_start_element (file, "bitfield");
819 xml_printf (file, " name=\"%s\"", name);
822 xml_printf (file, " glib:type-name=\"%s\" glib:get-type=\"%s\"", type_name, type_init);
825 xml_printf (file, " deprecated=\"1\"");
827 write_attributes (file, (GIBaseInfo*) info);
829 for (i = 0; i < g_enum_info_get_n_values (info); i++)
831 GIValueInfo *value = g_enum_info_get_value (info, i);
832 write_value_info (namespace, value, file);
833 g_base_info_unref ((GIBaseInfo *)value);
836 xml_end_element_unchecked (file);
840 write_signal_info (const gchar *namespace,
848 name = g_base_info_get_name ((GIBaseInfo *)info);
849 flags = g_signal_info_get_flags (info);
850 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
852 xml_start_element (file, "glib:signal");
853 xml_printf (file, " name=\"%s\"", name);
856 xml_printf (file, " deprecated=\"1\"");
858 if (flags & G_SIGNAL_RUN_FIRST)
859 xml_printf (file, " when=\"FIRST\"");
860 else if (flags & G_SIGNAL_RUN_LAST)
861 xml_printf (file, " when=\"LAST\"");
862 else if (flags & G_SIGNAL_RUN_CLEANUP)
863 xml_printf (file, " when=\"CLEANUP\"");
865 if (flags & G_SIGNAL_NO_RECURSE)
866 xml_printf (file, " no-recurse=\"1\"");
868 if (flags & G_SIGNAL_DETAILED)
869 xml_printf (file, " detailed=\"1\"");
871 if (flags & G_SIGNAL_ACTION)
872 xml_printf (file, " action=\"1\"");
874 if (flags & G_SIGNAL_NO_HOOKS)
875 xml_printf (file, " no-hooks=\"1\"");
877 write_callable_info (namespace, (GICallableInfo*)info, file);
879 xml_end_element (file, "glib:signal");
883 write_vfunc_info (const gchar *namespace,
887 GIVFuncInfoFlags flags;
889 GIFunctionInfo *invoker;
893 name = g_base_info_get_name ((GIBaseInfo *)info);
894 flags = g_vfunc_info_get_flags (info);
895 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
896 offset = g_vfunc_info_get_offset (info);
897 invoker = g_vfunc_info_get_invoker (info);
899 xml_start_element (file, "virtual-method");
900 xml_printf (file, " name=\"%s\"", name);
903 xml_printf (file, " deprecated=\"1\"");
905 if (flags & GI_VFUNC_MUST_CHAIN_UP)
906 xml_printf (file, " must-chain-up=\"1\"");
908 if (flags & GI_VFUNC_MUST_OVERRIDE)
909 xml_printf (file, " override=\"always\"");
910 else if (flags & GI_VFUNC_MUST_NOT_OVERRIDE)
911 xml_printf (file, " override=\"never\"");
913 xml_printf (file, " offset=\"%d\"", offset);
916 xml_printf (file, " invoker=\"%s\"", g_base_info_get_name ((GIBaseInfo*)invoker));
918 write_callable_info (namespace, (GICallableInfo*)info, file);
920 xml_end_element (file, "virtual-method");
924 write_property_info (const gchar *namespace,
925 GIPropertyInfo *info,
933 name = g_base_info_get_name ((GIBaseInfo *)info);
934 flags = g_property_info_get_flags (info);
935 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
937 xml_start_element (file, "property");
938 xml_printf (file, " name=\"%s\"", name);
941 xml_printf (file, " deprecated=\"1\"");
943 /* Properties are assumed to be read-only (see also girwriter.py) */
944 if (!(flags & G_PARAM_READABLE))
945 xml_printf (file, " readable=\"0\"");
946 if (flags & G_PARAM_WRITABLE)
947 xml_printf (file, " writable=\"1\"");
949 if (flags & G_PARAM_CONSTRUCT)
950 xml_printf (file, " construct=\"1\"");
952 if (flags & G_PARAM_CONSTRUCT_ONLY)
953 xml_printf (file, " construct-only=\"1\"");
955 write_attributes (file, (GIBaseInfo*) info);
957 type = g_property_info_get_type (info);
959 write_type_info (namespace, type, file);
961 xml_end_element (file, "property");
965 write_object_info (const gchar *namespace,
970 const gchar *type_name;
971 const gchar *type_init;
973 gboolean is_abstract;
975 GIStructInfo *class_struct;
978 name = g_base_info_get_name ((GIBaseInfo *)info);
979 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
980 is_abstract = g_object_info_get_abstract (info);
982 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
983 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
984 xml_start_element (file, "class");
985 xml_printf (file, " name=\"%s\"", name);
987 pnode = g_object_info_get_parent (info);
990 write_type_name_attribute (namespace, (GIBaseInfo *)pnode, "parent", file);
991 g_base_info_unref ((GIBaseInfo *)pnode);
994 class_struct = g_object_info_get_class_struct (info);
997 write_type_name_attribute (namespace, (GIBaseInfo*) class_struct, "glib:type-struct", file);
998 g_base_info_unref ((GIBaseInfo*)class_struct);
1002 xml_printf (file, " abstract=\"1\"");
1004 xml_printf (file, " glib:type-name=\"%s\" glib:get-type=\"%s\"", type_name, type_init);
1007 xml_printf (file, " deprecated=\"1\"");
1009 write_attributes (file, (GIBaseInfo*) info);
1011 if (g_object_info_get_n_interfaces (info) > 0)
1013 for (i = 0; i < g_object_info_get_n_interfaces (info); i++)
1015 GIInterfaceInfo *imp = g_object_info_get_interface (info, i);
1016 xml_start_element (file, "implements");
1017 write_type_name_attribute (namespace, (GIBaseInfo *)imp, "name", file);
1018 xml_end_element (file, "implements");
1019 g_base_info_unref ((GIBaseInfo*)imp);
1023 for (i = 0; i < g_object_info_get_n_fields (info); i++)
1025 GIFieldInfo *field = g_object_info_get_field (info, i);
1026 write_field_info (namespace, field, NULL, file);
1027 g_base_info_unref ((GIBaseInfo *)field);
1030 for (i = 0; i < g_object_info_get_n_methods (info); i++)
1032 GIFunctionInfo *function = g_object_info_get_method (info, i);
1033 write_function_info (namespace, function, file);
1034 g_base_info_unref ((GIBaseInfo *)function);
1037 for (i = 0; i < g_object_info_get_n_properties (info); i++)
1039 GIPropertyInfo *prop = g_object_info_get_property (info, i);
1040 write_property_info (namespace, prop, file);
1041 g_base_info_unref ((GIBaseInfo *)prop);
1044 for (i = 0; i < g_object_info_get_n_signals (info); i++)
1046 GISignalInfo *signal = g_object_info_get_signal (info, i);
1047 write_signal_info (namespace, signal, file);
1048 g_base_info_unref ((GIBaseInfo *)signal);
1051 for (i = 0; i < g_object_info_get_n_vfuncs (info); i++)
1053 GIVFuncInfo *vfunc = g_object_info_get_vfunc (info, i);
1054 write_vfunc_info (namespace, vfunc, file);
1055 g_base_info_unref ((GIBaseInfo *)vfunc);
1058 for (i = 0; i < g_object_info_get_n_constants (info); i++)
1060 GIConstantInfo *constant = g_object_info_get_constant (info, i);
1061 write_constant_info (namespace, constant, file);
1062 g_base_info_unref ((GIBaseInfo *)constant);
1065 xml_end_element (file, "class");
1069 write_interface_info (const gchar *namespace,
1070 GIInterfaceInfo *info,
1074 const gchar *type_name;
1075 const gchar *type_init;
1076 GIStructInfo *class_struct;
1077 gboolean deprecated;
1080 name = g_base_info_get_name ((GIBaseInfo *)info);
1081 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
1083 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
1084 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
1085 xml_start_element (file, "interface");
1086 xml_printf (file, " name=\"%s\" glib:type-name=\"%s\" glib:get-type=\"%s\"",
1087 name, type_name, type_init);
1089 class_struct = g_interface_info_get_iface_struct (info);
1092 write_type_name_attribute (namespace, (GIBaseInfo*) class_struct, "glib:type-struct", file);
1093 g_base_info_unref ((GIBaseInfo*)class_struct);
1097 xml_printf (file, " deprecated=\"1\"");
1099 write_attributes (file, (GIBaseInfo*) info);
1101 if (g_interface_info_get_n_prerequisites (info) > 0)
1103 for (i = 0; i < g_interface_info_get_n_prerequisites (info); i++)
1105 GIBaseInfo *req = g_interface_info_get_prerequisite (info, i);
1107 xml_start_element (file, "prerequisite");
1108 write_type_name_attribute (namespace, req, "name", file);
1110 xml_end_element_unchecked (file);
1111 g_base_info_unref (req);
1115 for (i = 0; i < g_interface_info_get_n_methods (info); i++)
1117 GIFunctionInfo *function = g_interface_info_get_method (info, i);
1118 write_function_info (namespace, function, file);
1119 g_base_info_unref ((GIBaseInfo *)function);
1122 for (i = 0; i < g_interface_info_get_n_properties (info); i++)
1124 GIPropertyInfo *prop = g_interface_info_get_property (info, i);
1125 write_property_info (namespace, prop, file);
1126 g_base_info_unref ((GIBaseInfo *)prop);
1129 for (i = 0; i < g_interface_info_get_n_signals (info); i++)
1131 GISignalInfo *signal = g_interface_info_get_signal (info, i);
1132 write_signal_info (namespace, signal, file);
1133 g_base_info_unref ((GIBaseInfo *)signal);
1136 for (i = 0; i < g_interface_info_get_n_vfuncs (info); i++)
1138 GIVFuncInfo *vfunc = g_interface_info_get_vfunc (info, i);
1139 write_vfunc_info (namespace, vfunc, file);
1140 g_base_info_unref ((GIBaseInfo *)vfunc);
1143 for (i = 0; i < g_interface_info_get_n_constants (info); i++)
1145 GIConstantInfo *constant = g_interface_info_get_constant (info, i);
1146 write_constant_info (namespace, constant, file);
1147 g_base_info_unref ((GIBaseInfo *)constant);
1150 xml_end_element (file, "interface");
1154 write_error_domain_info (const gchar *namespace,
1155 GIErrorDomainInfo *info,
1159 const gchar *name, *quark;
1161 name = g_base_info_get_name ((GIBaseInfo *)info);
1162 quark = g_error_domain_info_get_quark (info);
1163 enum_ = (GIBaseInfo *)g_error_domain_info_get_codes (info);
1164 xml_start_element (file, "errordomain");
1165 xml_printf (file, " name=\"%s\" get-quark=\"%s\"",
1167 write_type_name_attribute (namespace, enum_, "codes", file);
1168 xml_end_element (file, "errordomain");
1169 g_base_info_unref (enum_);
1173 write_union_info (const gchar *namespace,
1178 const gchar *type_name;
1179 const gchar *type_init;
1180 gboolean deprecated;
1184 name = g_base_info_get_name ((GIBaseInfo *)info);
1185 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
1187 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
1188 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
1190 xml_start_element (file, "union");
1191 xml_printf (file, " name=\"%s\"", name);
1194 xml_printf (file, " type-name=\"%s\" get-type=\"%s\"", type_name, type_init);
1197 xml_printf (file, " deprecated=\"1\"");
1199 size = g_union_info_get_size (info);
1200 if (show_all && size >= 0)
1201 xml_printf (file, " size=\"%d\"", size);
1203 write_attributes (file, (GIBaseInfo*) info);
1205 if (g_union_info_is_discriminated (info))
1210 offset = g_union_info_get_discriminator_offset (info);
1211 type = g_union_info_get_discriminator_type (info);
1213 xml_start_element (file, "discriminator");
1214 xml_printf (file, " offset=\"%d\" type=\"", offset);
1215 write_type_info (namespace, type, file);
1216 xml_end_element (file, "discriminator");
1217 g_base_info_unref ((GIBaseInfo *)type);
1220 for (i = 0; i < g_union_info_get_n_fields (info); i++)
1222 GIFieldInfo *field = g_union_info_get_field (info, i);
1223 GIConstantInfo *constant = g_union_info_get_discriminator (info, i);
1224 write_field_info (namespace, field, constant, file);
1225 g_base_info_unref ((GIBaseInfo *)field);
1227 g_base_info_unref ((GIBaseInfo *)constant);
1230 for (i = 0; i < g_union_info_get_n_methods (info); i++)
1232 GIFunctionInfo *function = g_union_info_get_method (info, i);
1233 write_function_info (namespace, function, file);
1234 g_base_info_unref ((GIBaseInfo *)function);
1237 xml_end_element (file, "union");
1241 write_repository (const char *namespace,
1242 gboolean needs_prefix)
1246 char **dependencies;
1247 GIRepository *repository;
1250 repository = g_irepository_get_default ();
1259 filename = g_strdup_printf ("%s-%s", namespace, output);
1261 filename = g_strdup (output);
1262 ofile = g_fopen (filename, "w");
1266 g_fprintf (stderr, "failed to open '%s': %s\n",
1267 filename, g_strerror (errno));
1276 xml = xml_open (ofile);
1278 xml_printf (xml, "<?xml version=\"1.0\"?>\n");
1279 xml_start_element (xml, "repository");
1280 xml_printf (xml, " version=\"1.0\"\n"
1281 " xmlns=\"http://www.gtk.org/introspection/core/1.0\"\n"
1282 " xmlns:c=\"http://www.gtk.org/introspection/c/1.0\"\n"
1283 " xmlns:glib=\"http://www.gtk.org/introspection/glib/1.0\"");
1285 dependencies = g_irepository_get_dependencies (repository,
1287 if (dependencies != NULL)
1289 for (i = 0; dependencies[i]; i++)
1291 char **parts = g_strsplit (dependencies[i], "-", 2);
1292 xml_start_element (xml, "include");
1293 xml_printf (xml, " name=\"%s\" version=\"%s\"", parts[0], parts[1]);
1294 xml_end_element (xml, "include");
1301 const gchar *shared_library;
1302 const gchar *c_prefix;
1303 const char *ns = namespace;
1304 const char *version;
1306 version = g_irepository_get_version (repository, ns);
1308 shared_library = g_irepository_get_shared_library (repository, ns);
1309 c_prefix = g_irepository_get_c_prefix (repository, ns);
1310 xml_start_element (xml, "namespace");
1311 xml_printf (xml, " name=\"%s\" version=\"%s\"", ns, version);
1313 xml_printf (xml, " shared-library=\"%s\"", shared_library);
1315 xml_printf (xml, " c:prefix=\"%s\"", c_prefix);
1317 for (j = 0; j < g_irepository_get_n_infos (repository, ns); j++)
1319 GIBaseInfo *info = g_irepository_get_info (repository, ns, j);
1320 switch (g_base_info_get_type (info))
1322 case GI_INFO_TYPE_FUNCTION:
1323 write_function_info (ns, (GIFunctionInfo *)info, xml);
1326 case GI_INFO_TYPE_CALLBACK:
1327 write_callback_info (ns, (GICallbackInfo *)info, xml);
1330 case GI_INFO_TYPE_STRUCT:
1331 case GI_INFO_TYPE_BOXED:
1332 write_struct_info (ns, (GIStructInfo *)info, xml);
1335 case GI_INFO_TYPE_UNION:
1336 write_union_info (ns, (GIUnionInfo *)info, xml);
1339 case GI_INFO_TYPE_ENUM:
1340 case GI_INFO_TYPE_FLAGS:
1341 write_enum_info (ns, (GIEnumInfo *)info, xml);
1344 case GI_INFO_TYPE_CONSTANT:
1345 write_constant_info (ns, (GIConstantInfo *)info, xml);
1348 case GI_INFO_TYPE_OBJECT:
1349 write_object_info (ns, (GIObjectInfo *)info, xml);
1352 case GI_INFO_TYPE_INTERFACE:
1353 write_interface_info (ns, (GIInterfaceInfo *)info, xml);
1356 case GI_INFO_TYPE_ERROR_DOMAIN:
1357 write_error_domain_info (ns, (GIErrorDomainInfo *)info, xml);
1361 g_error ("unknown info type %d\n", g_base_info_get_type (info));
1364 g_base_info_unref (info);
1367 xml_end_element (xml, "namespace");
1370 xml_end_element (xml, "repository");
1375 static const guchar *
1376 load_typelib (const gchar *filename,
1381 gsize *typelib_size;
1384 handle = g_module_open (filename, G_MODULE_BIND_LOCAL|G_MODULE_BIND_LAZY);
1387 g_printerr ("Could not load typelib from '%s': %s\n",
1388 filename, g_module_error ());
1392 if (!g_module_symbol (handle, "_G_TYPELIB", (gpointer *) &typelib))
1394 g_printerr ("Could not load typelib from '%s': %s\n",
1395 filename, g_module_error ());
1399 if (!g_module_symbol (handle, "_G_TYPELIB_SIZE", (gpointer *) &typelib_size))
1401 g_printerr ("Could not load typelib from '%s': %s\n",
1402 filename, g_module_error ());
1406 *len = *typelib_size;
1415 main (int argc, char *argv[])
1417 gboolean shlib = FALSE;
1418 gchar **input = NULL;
1419 GOptionContext *context;
1420 GError *error = NULL;
1421 gboolean needs_prefix;
1424 GOptionEntry options[] =
1426 { "shlib", 0, 0, G_OPTION_ARG_NONE, &shlib, "handle typelib embedded in shlib", NULL },
1427 { "output", 'o', 0, G_OPTION_ARG_FILENAME, &output, "output file", "FILE" },
1428 { "includedir", 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &includedirs, "include directories in GIR search path", NULL },
1429 { "all", 0, 0, G_OPTION_ARG_NONE, &show_all, "show all available information", NULL, },
1430 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &input, NULL, NULL },
1434 g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL);
1438 g_typelib_check_sanity ();
1440 context = g_option_context_new ("");
1441 g_option_context_add_main_entries (context, options, NULL);
1442 g_option_context_parse (context, &argc, &argv, &error);
1446 g_fprintf (stderr, "no input files\n");
1451 if (includedirs != NULL)
1452 for (i = 0; includedirs[i]; i++)
1453 g_irepository_prepend_search_path (includedirs[i]);
1455 for (i = 0; input[i]; i++)
1457 GModule *dlhandle = NULL;
1458 const guchar *typelib;
1460 const char *namespace;
1464 if (!g_file_get_contents (input[i], (gchar **)&typelib, &len, &error))
1466 g_fprintf (stderr, "failed to read '%s': %s\n",
1467 input[i], error->message);
1468 g_clear_error (&error);
1474 typelib = load_typelib (input[i], &dlhandle, &len);
1477 g_fprintf (stderr, "failed to load typelib from '%s'\n",
1483 if (input[i + 1] && output)
1484 needs_prefix = TRUE;
1486 needs_prefix = FALSE;
1488 data = g_typelib_new_from_const_memory (typelib, len);
1490 GError *error = NULL;
1491 if (!g_typelib_validate (data, &error)) {
1492 g_printerr ("typelib not valid: %s\n", error->message);
1493 g_clear_error (&error);
1497 namespace = g_irepository_load_typelib (g_irepository_get_default (), data, 0,
1499 if (namespace == NULL)
1501 g_printerr ("failed to load typelib: %s\n", error->message);
1505 write_repository (namespace, needs_prefix);
1509 g_module_close (dlhandle);
1513 /* when writing to stdout, stop after the first module */
1514 if (input[i + 1] && !output)
1516 g_fprintf (stderr, "warning, %d modules omitted\n",
1517 g_strv_length (input) - 1);