1 /* -*- Mode: C; c-file-style: "gnu"; -*- */
2 /* GObject introspection: IDL generator
4 * Copyright (C) 2005 Matthias Clasen
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
26 #include <glib-object.h>
27 #include <glib/gstdio.h>
29 #include "girepository.h"
32 /* FIXME: Avoid global */
33 static gchar *output = NULL;
34 gchar **includedirs = NULL;
43 guint has_children : 1;
47 xml_element_new (const char *name)
51 elem = g_new (XmlElement, 1);
52 elem->name = g_strdup (name);
53 elem->has_children = FALSE;
58 xml_element_free (XmlElement *elem)
65 xml_printf (Xml *xml, const char *fmt, ...)
71 s = g_markup_vprintf_escaped (fmt, ap);
78 xml_start_element (Xml *xml, const char *element_name)
80 XmlElement *parent = NULL;
84 parent = xml->stack->data;
86 if (!parent->has_children)
87 xml_printf (xml, ">\n");
89 parent->has_children = TRUE;
92 xml_printf (xml, "%*s<%s", g_slist_length(xml->stack)*2, "", element_name);
94 xml->stack = g_slist_prepend (xml->stack, xml_element_new (element_name));
98 xml_end_element (Xml *xml, const char *name)
102 g_assert (xml->stack != NULL);
104 elem = xml->stack->data;
105 xml->stack = g_slist_delete_link (xml->stack, xml->stack);
108 g_assert_cmpstr (name, ==, elem->name);
110 if (elem->has_children)
111 xml_printf (xml, "%*s</%s>\n", g_slist_length (xml->stack)*2, "", elem->name);
113 xml_printf (xml, "/>\n");
115 xml_element_free (elem);
119 xml_end_element_unchecked (Xml *xml)
121 xml_end_element (xml, NULL);
125 xml_open (FILE *file)
129 xml = g_new (Xml, 1);
139 g_assert (xml->stack == NULL);
140 if (xml->file != NULL)
143 if (xml->file != stdout)
158 check_unresolved (GIBaseInfo *info)
160 if (g_base_info_get_type (info) != GI_INFO_TYPE_UNRESOLVED)
163 g_critical ("Found unresolved type '%s' '%s'\n",
164 g_base_info_get_name (info), g_base_info_get_namespace (info));
168 write_type_name (const gchar *namespace,
172 if (strcmp (namespace, g_base_info_get_namespace (info)) != 0)
173 xml_printf (file, "%s.", g_base_info_get_namespace (info));
175 xml_printf (file, "%s", g_base_info_get_name (info));
179 write_type_name_attribute (const gchar *namespace,
181 const char *attr_name,
184 xml_printf (file, " %s=\"", attr_name);
185 write_type_name (namespace, info, file);
186 xml_printf (file, "\"");
190 write_type_info (const gchar *namespace,
199 check_unresolved ((GIBaseInfo*)info);
201 tag = g_type_info_get_tag (info);
202 is_pointer = g_type_info_is_pointer (info);
204 if (tag == GI_TYPE_TAG_VOID)
206 xml_start_element (file, "type");
208 xml_printf (file, " name=\"%s\"", is_pointer ? "any" : "none");
210 xml_end_element (file, "type");
212 else if (G_TYPE_TAG_IS_BASIC (tag))
214 xml_start_element (file, "type");
215 xml_printf (file, " name=\"%s\"", g_type_tag_to_string (tag));
216 xml_end_element (file, "type");
218 else if (tag == GI_TYPE_TAG_ARRAY)
222 xml_start_element (file, "array");
224 type = g_type_info_get_param_type (info, 0);
226 length = g_type_info_get_array_length (info);
228 xml_printf (file, " length=\"%d\"", length);
230 size = g_type_info_get_array_fixed_size (info);
232 xml_printf (file, " fixed-size=\"%d\"", size);
234 if (g_type_info_is_zero_terminated (info))
235 xml_printf (file, " zero-terminated=\"1\"");
237 write_type_info (namespace, type, file);
239 g_base_info_unref ((GIBaseInfo *)type);
241 xml_end_element (file, "array");
243 else if (tag == GI_TYPE_TAG_INTERFACE)
245 GIBaseInfo *iface = g_type_info_get_interface (info);
246 xml_start_element (file, "type");
247 write_type_name_attribute (namespace, iface, "name", file);
248 xml_end_element (file, "type");
249 g_base_info_unref (iface);
251 else if (tag == GI_TYPE_TAG_GLIST)
253 xml_start_element (file, "type");
254 xml_printf (file, " name=\"GLib.List\"");
255 type = g_type_info_get_param_type (info, 0);
258 write_type_info (namespace, type, file);
259 g_base_info_unref ((GIBaseInfo *)type);
261 xml_end_element (file, "type");
263 else if (tag == GI_TYPE_TAG_GSLIST)
265 xml_start_element (file, "type");
266 xml_printf (file, " name=\"GLib.SList\"");
267 type = g_type_info_get_param_type (info, 0);
270 write_type_info (namespace, type, file);
271 g_base_info_unref ((GIBaseInfo *)type);
273 xml_end_element (file, "type");
275 else if (tag == GI_TYPE_TAG_GHASH)
277 xml_start_element (file, "type");
278 xml_printf (file, " name=\"GLib.HashTable\"");
279 type = g_type_info_get_param_type (info, 0);
282 write_type_info (namespace, type, file);
283 g_base_info_unref ((GIBaseInfo *)type);
284 type = g_type_info_get_param_type (info, 1);
285 write_type_info (namespace, type, file);
286 g_base_info_unref ((GIBaseInfo *)type);
288 xml_end_element (file, "type");
290 else if (tag == GI_TYPE_TAG_ERROR)
294 xml_start_element (file, "type");
295 xml_printf (file, " name=\"GLib.Error\"");
297 n = g_type_info_get_n_error_domains (info);
300 for (i = 0; i < n; i++)
302 GIErrorDomainInfo *ed = g_type_info_get_error_domain (info, i);
303 xml_start_element (file, "type");
304 write_type_name_attribute (namespace, (GIBaseInfo *)ed, "name", file);
305 xml_end_element (file, "type");
306 g_base_info_unref ((GIBaseInfo *)ed);
310 xml_end_element (file, "type");
314 g_printerr ("Unhandled type tag %d\n", tag);
315 g_assert_not_reached ();
320 write_constant_value (const gchar *namespace,
326 write_field_info (const gchar *namespace,
328 GIConstantInfo *branch,
332 GIFieldInfoFlags flags;
338 name = g_base_info_get_name ((GIBaseInfo *)info);
339 flags = g_field_info_get_flags (info);
340 size = g_field_info_get_size (info);
341 offset = g_field_info_get_offset (info);
343 xml_start_element (file, "field");
344 xml_printf (file, " name=\"%s\"", name);
346 /* Fields are assumed to be read-only
347 * (see also girwriter.py and girparser.c)
349 if (!(flags & GI_FIELD_IS_READABLE))
350 xml_printf (file, " readable=\"0\"");
351 if (flags & GI_FIELD_IS_WRITABLE)
352 xml_printf (file, " writable=\"1\"");
355 xml_printf (file, " bits=\"%d\"", size);
357 xml_printf (file, " offset=\"%d\"", offset);
359 type = g_field_info_get_type (info);
363 xml_printf (file, " branch=\"");
364 type = g_constant_info_get_type (branch);
365 g_constant_info_get_value (branch, &value);
366 write_constant_value (namespace, type, &value, file);
367 xml_printf (file, "\"");
370 write_type_info (namespace, type, file);
371 g_base_info_unref ((GIBaseInfo *)type);
373 xml_end_element (file, "field");
377 write_callable_info (const gchar *namespace,
378 GICallableInfo *info,
384 type = g_callable_info_get_return_type (info);
386 xml_start_element (file, "return-value");
388 switch (g_callable_info_get_caller_owns (info))
390 case GI_TRANSFER_NOTHING:
391 xml_printf (file, " transfer-ownership=\"none\"");
393 case GI_TRANSFER_CONTAINER:
394 xml_printf (file, " transfer-ownership=\"container\"");
396 case GI_TRANSFER_EVERYTHING:
397 xml_printf (file, " transfer-ownership=\"full\"");
400 g_assert_not_reached ();
403 if (g_callable_info_may_return_null (info))
404 xml_printf (file, " allow-none=\"1\"");
406 write_type_info (namespace, type, file);
408 xml_end_element (file, "return-value");
410 if (g_callable_info_get_n_args (info) <= 0)
413 xml_start_element (file, "parameters");
414 for (i = 0; i < g_callable_info_get_n_args (info); i++)
416 GIArgInfo *arg = g_callable_info_get_arg (info, i);
418 xml_start_element (file, "parameter");
419 xml_printf (file, " name=\"%s\"",
420 g_base_info_get_name ((GIBaseInfo *) arg));
422 switch (g_arg_info_get_ownership_transfer (arg))
424 case GI_TRANSFER_NOTHING:
425 xml_printf (file, " transfer-ownership=\"none\"");
427 case GI_TRANSFER_CONTAINER:
428 xml_printf (file, " transfer-ownership=\"container\"");
430 case GI_TRANSFER_EVERYTHING:
431 xml_printf (file, " transfer-ownership=\"full\"");
434 g_assert_not_reached ();
437 switch (g_arg_info_get_direction (arg))
439 case GI_DIRECTION_IN:
441 case GI_DIRECTION_OUT:
442 xml_printf (file, " direction=\"out\"");
444 case GI_DIRECTION_INOUT:
445 xml_printf (file, " direction=\"inout\"");
449 if (g_arg_info_may_be_null (arg))
450 xml_printf (file, " allow-none=\"1\"");
452 if (g_arg_info_is_dipper (arg))
453 xml_printf (file, " dipper=\"1\"");
455 if (g_arg_info_is_return_value (arg))
456 xml_printf (file, " retval=\"1\"");
458 if (g_arg_info_is_optional (arg))
459 xml_printf (file, " optional=\"1\"");
461 type = g_arg_info_get_type (arg);
462 write_type_info (namespace, type, file);
464 xml_end_element (file, "parameter");
466 g_base_info_unref ((GIBaseInfo *)arg);
469 xml_end_element (file, "parameters");
470 g_base_info_unref ((GIBaseInfo *)type);
474 write_function_info (const gchar *namespace,
475 GIFunctionInfo *info,
478 GIFunctionInfoFlags flags;
485 flags = g_function_info_get_flags (info);
486 name = g_base_info_get_name ((GIBaseInfo *)info);
487 symbol = g_function_info_get_symbol (info);
488 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
489 throws = flags & GI_FUNCTION_THROWS;
491 if (flags & GI_FUNCTION_IS_CONSTRUCTOR)
493 else if (flags & GI_FUNCTION_IS_METHOD)
498 xml_start_element (file, tag);
499 xml_printf (file, " name=\"%s\" c:identifier=\"%s\"",
502 if (flags & GI_FUNCTION_IS_SETTER)
503 xml_printf (file, " type=\"setter\"");
504 else if (flags & GI_FUNCTION_IS_GETTER)
505 xml_printf (file, " type=\"getter\"");
508 xml_printf (file, " deprecated=\"1\"");
511 xml_printf (file, " throws=\"1\"");
513 write_callable_info (namespace, (GICallableInfo*)info, file);
514 xml_end_element (file, tag);
518 write_callback_info (const gchar *namespace,
519 GICallbackInfo *info,
525 name = g_base_info_get_name ((GIBaseInfo *)info);
526 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
528 xml_start_element (file, "callback");
529 xml_printf (file, " name=\"%s\"", name);
532 xml_printf (file, " deprecated=\"1\"");
534 write_callable_info (namespace, (GICallableInfo*)info, file);
535 xml_end_element (file, "callback");
539 write_struct_info (const gchar *namespace,
544 const gchar *type_name;
545 const gchar *type_init;
550 name = g_base_info_get_name ((GIBaseInfo *)info);
551 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
553 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
554 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
556 if (g_base_info_get_type ((GIBaseInfo *)info) == GI_INFO_TYPE_BOXED)
558 xml_start_element (file, "glib:boxed");
559 xml_printf (file, " glib:name=\"%s\"", name);
563 xml_start_element (file, "record");
564 xml_printf (file, " name=\"%s\"", name);
567 if (type_name != NULL)
568 xml_printf (file, " glib:type-name=\"%s\" glib:get-type=\"%s\"", type_name, type_init);
571 xml_printf (file, " deprecated=\"1\"");
573 n_elts = g_struct_info_get_n_fields (info) + g_struct_info_get_n_methods (info);
576 for (i = 0; i < g_struct_info_get_n_fields (info); i++)
578 GIFieldInfo *field = g_struct_info_get_field (info, i);
579 write_field_info (namespace, field, NULL, file);
580 g_base_info_unref ((GIBaseInfo *)field);
583 for (i = 0; i < g_struct_info_get_n_methods (info); i++)
585 GIFunctionInfo *function = g_struct_info_get_method (info, i);
586 write_function_info (namespace, function, file);
587 g_base_info_unref ((GIBaseInfo *)function);
592 xml_end_element_unchecked (file);
596 write_value_info (const gchar *namespace,
604 name = g_base_info_get_name ((GIBaseInfo *)info);
605 value = g_value_info_get_value (info);
606 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
608 xml_start_element (file, "member");
609 xml_printf (file, " name=\"%s\" value=\"%ld\"", name, value);
612 xml_printf (file, " deprecated=\"1\"");
614 xml_end_element (file, "member");
618 write_constant_value (const gchar *namespace,
623 switch (g_type_info_get_tag (type))
625 case GI_TYPE_TAG_BOOLEAN:
626 xml_printf (file, "%d", value->v_boolean);
628 case GI_TYPE_TAG_INT8:
629 xml_printf (file, "%d", value->v_int8);
631 case GI_TYPE_TAG_UINT8:
632 xml_printf (file, "%d", value->v_uint8);
634 case GI_TYPE_TAG_INT16:
635 xml_printf (file, "%" G_GINT16_FORMAT, value->v_int16);
637 case GI_TYPE_TAG_UINT16:
638 xml_printf (file, "%" G_GUINT16_FORMAT, value->v_uint16);
640 case GI_TYPE_TAG_INT32:
641 xml_printf (file, "%" G_GINT32_FORMAT, value->v_int32);
643 case GI_TYPE_TAG_UINT32:
644 xml_printf (file, "%" G_GUINT32_FORMAT, value->v_uint32);
646 case GI_TYPE_TAG_INT64:
647 xml_printf (file, "%" G_GINT64_FORMAT, value->v_int64);
649 case GI_TYPE_TAG_UINT64:
650 xml_printf (file, "%" G_GUINT64_FORMAT, value->v_uint64);
652 case GI_TYPE_TAG_INT:
653 xml_printf (file, "%d", value->v_int);
655 case GI_TYPE_TAG_UINT:
656 xml_printf (file, "%d", value->v_uint);
658 case GI_TYPE_TAG_LONG:
659 xml_printf (file, "%ld", value->v_long);
661 case GI_TYPE_TAG_ULONG:
662 xml_printf (file, "%ld", value->v_ulong);
664 case GI_TYPE_TAG_SSIZE:
665 xml_printf (file, "%zd", value->v_ssize);
667 case GI_TYPE_TAG_SIZE:
668 xml_printf (file, "%zd", value->v_size);
670 case GI_TYPE_TAG_FLOAT:
671 xml_printf (file, "%f", value->v_float);
673 case GI_TYPE_TAG_DOUBLE:
674 xml_printf (file, "%f", value->v_double);
676 case GI_TYPE_TAG_UTF8:
677 case GI_TYPE_TAG_FILENAME:
678 xml_printf (file, "%s", value->v_string);
681 g_assert_not_reached ();
686 write_constant_info (const gchar *namespace,
687 GIConstantInfo *info,
695 name = g_base_info_get_name ((GIBaseInfo *)info);
696 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
698 xml_start_element (file, "constant");
699 xml_printf (file, " name=\"%s\"", name);
701 type = g_constant_info_get_type (info);
702 xml_printf (file, " value=\"");
704 g_constant_info_get_value (info, &value);
705 write_constant_value (namespace, type, &value, file);
706 xml_printf (file, "\"");
708 write_type_info (namespace, type, file);
710 xml_end_element (file, "constant");
712 g_base_info_unref ((GIBaseInfo *)type);
717 write_enum_info (const gchar *namespace,
722 const gchar *type_name;
723 const gchar *type_init;
727 name = g_base_info_get_name ((GIBaseInfo *)info);
728 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
730 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
731 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
733 if (g_base_info_get_type ((GIBaseInfo *)info) == GI_INFO_TYPE_ENUM)
734 xml_start_element (file, "enumeration");
736 xml_start_element (file, "bitfield");
737 xml_printf (file, " name=\"%s\"", name);
740 xml_printf (file, " glib:type-name=\"%s\" glib:get-type=\"%s\"", type_name, type_init);
743 xml_printf (file, " deprecated=\"1\"");
746 for (i = 0; i < g_enum_info_get_n_values (info); i++)
748 GIValueInfo *value = g_enum_info_get_value (info, i);
749 write_value_info (namespace, value, file);
750 g_base_info_unref ((GIBaseInfo *)value);
753 xml_end_element_unchecked (file);
757 write_signal_info (const gchar *namespace,
765 name = g_base_info_get_name ((GIBaseInfo *)info);
766 flags = g_signal_info_get_flags (info);
767 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
769 xml_start_element (file, "glib:signal");
770 xml_printf (file, " name=\"%s\"", name);
773 xml_printf (file, " deprecated=\"1\"");
775 if (flags & G_SIGNAL_RUN_FIRST)
776 xml_printf (file, " when=\"FIRST\"");
777 else if (flags & G_SIGNAL_RUN_LAST)
778 xml_printf (file, " when=\"LAST\"");
779 else if (flags & G_SIGNAL_RUN_CLEANUP)
780 xml_printf (file, " when=\"CLEANUP\"");
782 if (flags & G_SIGNAL_NO_RECURSE)
783 xml_printf (file, " no-recurse=\"1\"");
785 if (flags & G_SIGNAL_DETAILED)
786 xml_printf (file, " detailed=\"1\"");
788 if (flags & G_SIGNAL_ACTION)
789 xml_printf (file, " action=\"1\"");
791 if (flags & G_SIGNAL_NO_HOOKS)
792 xml_printf (file, " no-hooks=\"1\"");
794 write_callable_info (namespace, (GICallableInfo*)info, file);
796 xml_end_element (file, "glib:signal");
800 write_vfunc_info (const gchar *namespace,
804 GIVFuncInfoFlags flags;
809 name = g_base_info_get_name ((GIBaseInfo *)info);
810 flags = g_vfunc_info_get_flags (info);
811 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
812 offset = g_vfunc_info_get_offset (info);
814 xml_start_element (file, "vfunc");
815 xml_printf (file, " name=\"%s\"", name);
818 xml_printf (file, " deprecated=\"1\"");
820 if (flags & GI_VFUNC_MUST_CHAIN_UP)
821 xml_printf (file, " must-chain-up=\"1\"");
823 if (flags & GI_VFUNC_MUST_OVERRIDE)
824 xml_printf (file, " override=\"always\"");
825 else if (flags & GI_VFUNC_MUST_NOT_OVERRIDE)
826 xml_printf (file, " override=\"never\"");
828 xml_printf (file, " offset=\"%d\"", offset);
830 write_callable_info (namespace, (GICallableInfo*)info, file);
832 xml_end_element (file, "vfunc");
836 write_property_info (const gchar *namespace,
837 GIPropertyInfo *info,
845 name = g_base_info_get_name ((GIBaseInfo *)info);
846 flags = g_property_info_get_flags (info);
847 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
849 xml_start_element (file, "property");
850 xml_printf (file, " name=\"%s\"", name);
853 xml_printf (file, " deprecated=\"1\"");
855 /* Properties are assumed to be read-only (see also girwriter.py) */
856 if (!(flags & G_PARAM_READABLE))
857 xml_printf (file, " readable=\"0\"");
858 if (flags & G_PARAM_WRITABLE)
859 xml_printf (file, " writable=\"1\"");
861 if (flags & G_PARAM_CONSTRUCT)
862 xml_printf (file, " construct=\"1\"");
864 if (flags & G_PARAM_CONSTRUCT_ONLY)
865 xml_printf (file, " construct-only=\"1\"");
867 type = g_property_info_get_type (info);
869 write_type_info (namespace, type, file);
871 xml_end_element (file, "property");
875 write_object_info (const gchar *namespace,
880 const gchar *type_name;
881 const gchar *type_init;
883 gboolean is_abstract;
887 name = g_base_info_get_name ((GIBaseInfo *)info);
888 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
889 is_abstract = g_object_info_get_abstract (info);
891 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
892 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
893 xml_start_element (file, "class");
894 xml_printf (file, " name=\"%s\"", name);
896 pnode = g_object_info_get_parent (info);
899 write_type_name_attribute (namespace, (GIBaseInfo *)pnode, "parent", file);
900 g_base_info_unref ((GIBaseInfo *)pnode);
904 xml_printf (file, " abstract=\"1\"");
906 xml_printf (file, " glib:type-name=\"%s\" glib:get-type=\"%s\"", type_name, type_init);
909 xml_printf (file, " deprecated=\"1\"");
912 if (g_object_info_get_n_interfaces (info) > 0)
914 for (i = 0; i < g_object_info_get_n_interfaces (info); i++)
916 GIInterfaceInfo *imp = g_object_info_get_interface (info, i);
917 xml_start_element (file, "implements");
918 write_type_name_attribute (namespace, (GIBaseInfo *)imp, "name", file);
919 xml_end_element (file, "implements");
920 g_base_info_unref ((GIBaseInfo*)imp);
924 for (i = 0; i < g_object_info_get_n_fields (info); i++)
926 GIFieldInfo *field = g_object_info_get_field (info, i);
927 write_field_info (namespace, field, NULL, file);
928 g_base_info_unref ((GIBaseInfo *)field);
931 for (i = 0; i < g_object_info_get_n_methods (info); i++)
933 GIFunctionInfo *function = g_object_info_get_method (info, i);
934 write_function_info (namespace, function, file);
935 g_base_info_unref ((GIBaseInfo *)function);
938 for (i = 0; i < g_object_info_get_n_properties (info); i++)
940 GIPropertyInfo *prop = g_object_info_get_property (info, i);
941 write_property_info (namespace, prop, file);
942 g_base_info_unref ((GIBaseInfo *)prop);
945 for (i = 0; i < g_object_info_get_n_signals (info); i++)
947 GISignalInfo *signal = g_object_info_get_signal (info, i);
948 write_signal_info (namespace, signal, file);
949 g_base_info_unref ((GIBaseInfo *)signal);
952 for (i = 0; i < g_object_info_get_n_vfuncs (info); i++)
954 GIVFuncInfo *vfunc = g_object_info_get_vfunc (info, i);
955 write_vfunc_info (namespace, vfunc, file);
956 g_base_info_unref ((GIBaseInfo *)vfunc);
959 for (i = 0; i < g_object_info_get_n_constants (info); i++)
961 GIConstantInfo *constant = g_object_info_get_constant (info, i);
962 write_constant_info (namespace, constant, file);
963 g_base_info_unref ((GIBaseInfo *)constant);
966 xml_end_element (file, "class");
970 write_interface_info (const gchar *namespace,
971 GIInterfaceInfo *info,
975 const gchar *type_name;
976 const gchar *type_init;
980 name = g_base_info_get_name ((GIBaseInfo *)info);
981 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
983 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
984 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
985 xml_start_element (file, "interface");
986 xml_printf (file, " name=\"%s\" glib:type-name=\"%s\" glib:get-type=\"%s\"",
987 name, type_name, type_init);
990 xml_printf (file, " deprecated=\"1\"");
993 if (g_interface_info_get_n_prerequisites (info) > 0)
995 xml_start_element (file, "requires");
996 for (i = 0; i < g_interface_info_get_n_prerequisites (info); i++)
998 GIBaseInfo *req = g_interface_info_get_prerequisite (info, i);
1000 if (g_base_info_get_type (req) == GI_INFO_TYPE_INTERFACE)
1001 xml_start_element (file, "interface");
1003 xml_start_element (file, "object");
1004 write_type_name_attribute (namespace, req, "name", file);
1005 xml_end_element_unchecked (file);
1006 g_base_info_unref (req);
1008 xml_end_element (file, "requires");
1011 for (i = 0; i < g_interface_info_get_n_methods (info); i++)
1013 GIFunctionInfo *function = g_interface_info_get_method (info, i);
1014 write_function_info (namespace, function, file);
1015 g_base_info_unref ((GIBaseInfo *)function);
1018 for (i = 0; i < g_interface_info_get_n_properties (info); i++)
1020 GIPropertyInfo *prop = g_interface_info_get_property (info, i);
1021 write_property_info (namespace, prop, file);
1022 g_base_info_unref ((GIBaseInfo *)prop);
1025 for (i = 0; i < g_interface_info_get_n_signals (info); i++)
1027 GISignalInfo *signal = g_interface_info_get_signal (info, i);
1028 write_signal_info (namespace, signal, file);
1029 g_base_info_unref ((GIBaseInfo *)signal);
1032 for (i = 0; i < g_interface_info_get_n_vfuncs (info); i++)
1034 GIVFuncInfo *vfunc = g_interface_info_get_vfunc (info, i);
1035 write_vfunc_info (namespace, vfunc, file);
1036 g_base_info_unref ((GIBaseInfo *)vfunc);
1039 for (i = 0; i < g_interface_info_get_n_constants (info); i++)
1041 GIConstantInfo *constant = g_interface_info_get_constant (info, i);
1042 write_constant_info (namespace, constant, file);
1043 g_base_info_unref ((GIBaseInfo *)constant);
1046 xml_end_element (file, "interface");
1050 write_error_domain_info (const gchar *namespace,
1051 GIErrorDomainInfo *info,
1055 const gchar *name, *quark;
1057 name = g_base_info_get_name ((GIBaseInfo *)info);
1058 quark = g_error_domain_info_get_quark (info);
1059 enum_ = (GIBaseInfo *)g_error_domain_info_get_codes (info);
1060 xml_start_element (file, "errordomain");
1061 xml_printf (file, " name=\"%s\" get-quark=\"%s\"",
1063 write_type_name_attribute (namespace, enum_, "codes", file);
1064 xml_end_element (file, "errordomain");
1065 g_base_info_unref (enum_);
1069 write_union_info (const gchar *namespace,
1074 const gchar *type_name;
1075 const gchar *type_init;
1076 gboolean deprecated;
1079 name = g_base_info_get_name ((GIBaseInfo *)info);
1080 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
1082 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
1083 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
1085 xml_start_element (file, "union");
1086 xml_printf (file, " name=\"%s\"", name);
1089 xml_printf (file, " type-name=\"%s\" get-type=\"%s\"", type_name, type_init);
1092 xml_printf (file, " deprecated=\"1\"");
1095 if (g_union_info_is_discriminated (info))
1100 offset = g_union_info_get_discriminator_offset (info);
1101 type = g_union_info_get_discriminator_type (info);
1103 xml_start_element (file, "discriminator");
1104 xml_printf (file, " offset=\"%d\" type=\"", offset);
1105 write_type_info (namespace, type, file);
1106 xml_end_element (file, "discriminator");
1107 g_base_info_unref ((GIBaseInfo *)type);
1110 for (i = 0; i < g_union_info_get_n_fields (info); i++)
1112 GIFieldInfo *field = g_union_info_get_field (info, i);
1113 GIConstantInfo *constant = g_union_info_get_discriminator (info, i);
1114 write_field_info (namespace, field, constant, file);
1115 g_base_info_unref ((GIBaseInfo *)field);
1117 g_base_info_unref ((GIBaseInfo *)constant);
1120 for (i = 0; i < g_union_info_get_n_methods (info); i++)
1122 GIFunctionInfo *function = g_union_info_get_method (info, i);
1123 write_function_info (namespace, function, file);
1124 g_base_info_unref ((GIBaseInfo *)function);
1127 xml_end_element (file, "union");
1131 write_repository (const char *namespace,
1132 gboolean needs_prefix)
1136 char **dependencies;
1137 GIRepository *repository;
1140 repository = g_irepository_get_default ();
1149 filename = g_strdup_printf ("%s-%s", namespace, output);
1151 filename = g_strdup (output);
1152 ofile = g_fopen (filename, "w");
1156 g_fprintf (stderr, "failed to open '%s': %s\n",
1157 filename, g_strerror (errno));
1166 xml = xml_open (ofile);
1168 xml_printf (xml, "<?xml version=\"1.0\"?>\n");
1169 xml_start_element (xml, "repository");
1170 xml_printf (xml, " version=\"1.0\"\n"
1171 " xmlns=\"http://www.gtk.org/introspection/core/1.0\"\n"
1172 " xmlns:c=\"http://www.gtk.org/introspection/c/1.0\"\n"
1173 " xmlns:glib=\"http://www.gtk.org/introspection/glib/1.0\"");
1175 dependencies = g_irepository_get_dependencies (repository,
1177 if (dependencies != NULL)
1179 for (i = 0; dependencies[i]; i++)
1181 char **parts = g_strsplit (dependencies[i], "-", 2);
1182 xml_start_element (xml, "include");
1183 xml_printf (xml, " name=\"%s\" version=\"%s\"", parts[0], parts[1]);
1184 xml_end_element (xml, "include");
1191 const gchar *shared_library;
1192 const char *ns = namespace;
1193 const char *version;
1195 version = g_irepository_get_version (repository, ns);
1197 shared_library = g_irepository_get_shared_library (repository, ns);
1198 xml_start_element (xml, "namespace");
1199 xml_printf (xml, " name=\"%s\" version=\"%s\"", ns, version);
1201 xml_printf (xml, " shared-library=\"%s\"", shared_library);
1203 for (j = 0; j < g_irepository_get_n_infos (repository, ns); j++)
1205 GIBaseInfo *info = g_irepository_get_info (repository, ns, j);
1206 switch (g_base_info_get_type (info))
1208 case GI_INFO_TYPE_FUNCTION:
1209 write_function_info (ns, (GIFunctionInfo *)info, xml);
1212 case GI_INFO_TYPE_CALLBACK:
1213 write_callback_info (ns, (GICallbackInfo *)info, xml);
1216 case GI_INFO_TYPE_STRUCT:
1217 case GI_INFO_TYPE_BOXED:
1218 write_struct_info (ns, (GIStructInfo *)info, xml);
1221 case GI_INFO_TYPE_UNION:
1222 write_union_info (ns, (GIUnionInfo *)info, xml);
1225 case GI_INFO_TYPE_ENUM:
1226 case GI_INFO_TYPE_FLAGS:
1227 write_enum_info (ns, (GIEnumInfo *)info, xml);
1230 case GI_INFO_TYPE_CONSTANT:
1231 write_constant_info (ns, (GIConstantInfo *)info, xml);
1234 case GI_INFO_TYPE_OBJECT:
1235 write_object_info (ns, (GIObjectInfo *)info, xml);
1238 case GI_INFO_TYPE_INTERFACE:
1239 write_interface_info (ns, (GIInterfaceInfo *)info, xml);
1242 case GI_INFO_TYPE_ERROR_DOMAIN:
1243 write_error_domain_info (ns, (GIErrorDomainInfo *)info, xml);
1247 g_error ("unknown info type %d\n", g_base_info_get_type (info));
1250 g_base_info_unref (info);
1253 xml_end_element (xml, "namespace");
1256 xml_end_element (xml, "repository");
1261 static const guchar *
1262 load_typelib (const gchar *filename,
1267 gsize *typelib_size;
1270 handle = g_module_open (filename, G_MODULE_BIND_LOCAL|G_MODULE_BIND_LAZY);
1273 g_printerr ("Could not load typelib from '%s': %s\n",
1274 filename, g_module_error ());
1278 if (!g_module_symbol (handle, "_G_TYPELIB", (gpointer *) &typelib))
1280 g_printerr ("Could not load typelib from '%s': %s\n",
1281 filename, g_module_error ());
1285 if (!g_module_symbol (handle, "_G_TYPELIB_SIZE", (gpointer *) &typelib_size))
1287 g_printerr ("Could not load typelib from '%s': %s\n",
1288 filename, g_module_error ());
1292 *len = *typelib_size;
1301 main (int argc, char *argv[])
1303 gboolean shlib = FALSE;
1304 gchar **input = NULL;
1305 GOptionContext *context;
1306 GError *error = NULL;
1307 gboolean needs_prefix;
1310 GOptionEntry options[] =
1312 { "shlib", 0, 0, G_OPTION_ARG_NONE, &shlib, "handle typelib embedded in shlib", NULL },
1313 { "output", 'o', 0, G_OPTION_ARG_FILENAME, &output, "output file", "FILE" },
1314 { "includedir", 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &includedirs, "include directories in GIR search path", NULL },
1315 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &input, NULL, NULL },
1319 g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL);
1323 g_typelib_check_sanity ();
1325 context = g_option_context_new ("");
1326 g_option_context_add_main_entries (context, options, NULL);
1327 g_option_context_parse (context, &argc, &argv, &error);
1331 g_fprintf (stderr, "no input files\n");
1336 if (includedirs != NULL)
1337 for (i = 0; includedirs[i]; i++)
1338 g_irepository_prepend_search_path (includedirs[i]);
1340 for (i = 0; input[i]; i++)
1342 GModule *dlhandle = NULL;
1343 const guchar *typelib;
1345 const char *namespace;
1349 if (!g_file_get_contents (input[i], (gchar **)&typelib, &len, &error))
1351 g_fprintf (stderr, "failed to read '%s': %s\n",
1352 input[i], error->message);
1353 g_clear_error (&error);
1359 typelib = load_typelib (input[i], &dlhandle, &len);
1362 g_fprintf (stderr, "failed to load typelib from '%s'\n",
1368 if (input[i + 1] && output)
1369 needs_prefix = TRUE;
1371 needs_prefix = FALSE;
1373 data = g_typelib_new_from_const_memory (typelib, len);
1375 GError *error = NULL;
1376 if (!g_typelib_validate (data, &error)) {
1377 g_printerr ("typelib not valid: %s\n", error->message);
1378 g_clear_error (&error);
1382 namespace = g_irepository_load_typelib (g_irepository_get_default (), data, 0,
1384 if (namespace == NULL)
1386 g_printerr ("failed to load typelib: %s\n", error->message);
1390 write_repository (namespace, needs_prefix);
1394 g_module_close (dlhandle);
1398 /* when writing to stdout, stop after the first module */
1399 if (input[i + 1] && !output)
1401 g_fprintf (stderr, "warning, %d modules omitted\n",
1402 g_strv_length (input) - 1);