1 /* GObject introspection: A parser for the XML GIR format
3 * Copyright (C) 2008 Philip Van Hoof
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
26 #include "girparser.h"
27 #include "girmodule.h"
32 #if defined(HAVE_BACKTRACE) && defined(HAVE_BACKTRACE_SYMBOLS)
33 # include <execinfo.h>
39 GList *parsed_modules; /* All previously parsed modules */
49 STATE_NAMESPACE, /* 5 */
53 STATE_FUNCTION_RETURN,
54 STATE_FUNCTION_PARAMETERS, /* 10 */
55 STATE_FUNCTION_PARAMETER,
59 STATE_INTERFACE, /* 15 */
60 STATE_INTERFACE_PROPERTY,
61 STATE_INTERFACE_FIELD,
71 STATE_NAMESPACE_CONSTANT,
73 STATE_INTERFACE_CONSTANT,
80 typedef struct _ParseContext ParseContext;
87 ParseState prev_state;
90 GList *include_modules;
93 GHashTable *disguised_structures;
95 const char *namespace;
97 GIrModule *current_module;
99 GIrNode *current_typed;
102 GList *type_parameters;
104 gboolean in_embedded_type;
106 #define CURRENT_NODE(ctx) ((GIrNode *)((ctx)->node_stack->data))
108 static void start_element_handler (GMarkupParseContext *context,
109 const gchar *element_name,
110 const gchar **attribute_names,
111 const gchar **attribute_values,
114 static void end_element_handler (GMarkupParseContext *context,
115 const gchar *element_name,
118 static void text_handler (GMarkupParseContext *context,
123 static void cleanup (GMarkupParseContext *context,
127 static GMarkupParser markup_parser =
129 start_element_handler,
137 start_alias (GMarkupParseContext *context,
138 const gchar *element_name,
139 const gchar **attribute_names,
140 const gchar **attribute_values,
144 static const gchar *find_attribute (const gchar *name,
145 const gchar **attribute_names,
146 const gchar **attribute_values);
150 g_ir_parser_new (void)
152 GIrParser *parser = g_slice_new0 (GIrParser);
158 g_ir_parser_free (GIrParser *parser)
162 if (parser->includes)
163 g_strfreev (parser->includes);
165 for (l = parser->parsed_modules; l; l = l->next)
166 g_ir_module_free (l->data);
168 g_slice_free (GIrParser, parser);
172 g_ir_parser_set_includes (GIrParser *parser,
173 const gchar *const *includes)
175 if (parser->includes)
176 g_strfreev (parser->includes);
178 parser->includes = g_strdupv ((char **)includes);
182 firstpass_start_element_handler (GMarkupParseContext *context,
183 const gchar *element_name,
184 const gchar **attribute_names,
185 const gchar **attribute_values,
189 ParseContext *ctx = user_data;
191 if (strcmp (element_name, "alias") == 0)
193 start_alias (context, element_name, attribute_names, attribute_values,
196 else if (strcmp (element_name, "record") == 0)
199 const gchar *disguised;
201 name = find_attribute ("name", attribute_names, attribute_values);
202 disguised = find_attribute ("disguised", attribute_names, attribute_values);
204 if (disguised && strcmp (disguised, "1") == 0)
208 key = g_strdup_printf ("%s.%s", ctx->namespace, name);
209 g_hash_table_replace (ctx->disguised_structures, key, GINT_TO_POINTER (1));
215 firstpass_end_element_handler (GMarkupParseContext *context,
216 const gchar *element_name,
222 static GMarkupParser firstpass_parser =
224 firstpass_start_element_handler,
225 firstpass_end_element_handler,
232 locate_gir (GIrParser *parser,
235 const gchar *const *datadirs;
236 const gchar *const *dir;
239 datadirs = g_get_system_data_dirs ();
241 if (parser->includes != NULL)
243 for (dir = (const gchar *const *)parser->includes; *dir; dir++)
245 path = g_build_filename (*dir, girname, NULL);
246 if (g_file_test (path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))
252 for (dir = datadirs; *dir; dir++)
254 path = g_build_filename (*dir, GIR_SUFFIX, girname, NULL);
255 if (g_file_test (path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))
261 path = g_build_filename (GIR_DIR, girname, NULL);
262 if (g_file_test (path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))
268 #define MISSING_ATTRIBUTE(ctx,error,element,attribute) \
270 int line_number, char_number; \
271 g_markup_parse_context_get_position (context, &line_number, &char_number); \
272 g_set_error (error, \
274 G_MARKUP_ERROR_INVALID_CONTENT, \
275 "Line %d, character %d: The attribute '%s' on the element '%s' must be specified", \
276 line_number, char_number, attribute, element); \
280 backtrace_stderr (void)
282 #if defined(HAVE_BACKTRACE) && defined(HAVE_BACKTRACE_SYMBOLS)
287 size = backtrace (array, 50);
288 strings = (char**) backtrace_symbols (array, size);
290 fprintf (stderr, "--- BACKTRACE (%zd frames) ---\n", size);
292 for (i = 0; i < size; i++)
293 fprintf (stderr, "%s\n", strings[i]);
295 fprintf (stderr, "--- END BACKTRACE ---\n");
303 find_attribute (const gchar *name,
304 const gchar **attribute_names,
305 const gchar **attribute_values)
309 for (i = 0; attribute_names[i] != NULL; i++)
310 if (strcmp (attribute_names[i], name) == 0)
311 return attribute_values[i];
317 state_switch (ParseContext *ctx, ParseState newstate)
319 g_debug ("State: %d", newstate);
320 ctx->prev_state = ctx->state;
321 ctx->state = newstate;
325 pop_node (ParseContext *ctx)
327 g_assert (ctx->node_stack != 0);
329 GSList *top = ctx->node_stack;
330 GIrNode *node = top->data;
332 g_debug ("popping node %d %s", node->type, node->name);
333 ctx->node_stack = top->next;
334 g_slist_free_1 (top);
339 push_node (ParseContext *ctx, GIrNode *node)
341 g_debug ("pushing node %d %s", node->type, node->name);
342 ctx->node_stack = g_slist_prepend (ctx->node_stack, node);
345 static GIrNodeType * parse_type_internal (const gchar *str, gchar **next, gboolean in_glib,
346 gboolean in_gobject);
354 static BasicTypeInfo basic_types[] = {
355 { "none", GI_TYPE_TAG_VOID, 0 },
356 { "any", GI_TYPE_TAG_VOID, 1 },
358 { "bool", GI_TYPE_TAG_BOOLEAN, 0 },
359 { "char", GI_TYPE_TAG_INT8, 0 },
360 { "int8", GI_TYPE_TAG_INT8, 0 },
361 { "uint8", GI_TYPE_TAG_UINT8, 0 },
362 { "int16", GI_TYPE_TAG_INT16, 0 },
363 { "uint16", GI_TYPE_TAG_UINT16, 0 },
364 { "int32", GI_TYPE_TAG_INT32, 0 },
365 { "uint32", GI_TYPE_TAG_UINT32, 0 },
366 { "int64", GI_TYPE_TAG_INT64, 0 },
367 { "uint64", GI_TYPE_TAG_UINT64, 0 },
368 { "short", GI_TYPE_TAG_SHORT, 0 },
369 { "ushort", GI_TYPE_TAG_USHORT, 0 },
370 { "int", GI_TYPE_TAG_INT, 0 },
371 { "uint", GI_TYPE_TAG_UINT, 0 },
372 { "long", GI_TYPE_TAG_LONG, 0 },
373 { "ulong", GI_TYPE_TAG_ULONG, 0 },
374 { "ssize_t", GI_TYPE_TAG_SSIZE, 0 },
375 { "ssize", GI_TYPE_TAG_SSIZE, 0 },
376 { "size_t", GI_TYPE_TAG_SIZE, 0 },
377 { "size", GI_TYPE_TAG_SIZE, 0 },
378 { "float", GI_TYPE_TAG_FLOAT, 0 },
379 { "double", GI_TYPE_TAG_DOUBLE, 0 },
380 { "time_t", GI_TYPE_TAG_TIME_T, 0 },
381 { "GType", GI_TYPE_TAG_GTYPE, 0 },
382 { "utf8", GI_TYPE_TAG_UTF8, 1 },
383 { "filename", GI_TYPE_TAG_FILENAME,1 },
386 static const BasicTypeInfo *
387 parse_basic (const char *str)
390 gint n_basic = G_N_ELEMENTS (basic_types);
392 for (i = 0; i < n_basic; i++)
394 if (g_str_has_prefix (str, basic_types[i].str))
395 return &(basic_types[i]);
401 parse_type_internal (const gchar *str, char **next, gboolean in_glib,
404 const BasicTypeInfo *basic;
406 char *temporary_type = NULL;
408 type = (GIrNodeType *)g_ir_node_new (G_IR_NODE_TYPE);
410 type->unparsed = g_strdup (str);
412 /* See comment below on GLib.List handling */
413 if (in_gobject && strcmp (str, "Type") == 0)
415 temporary_type = g_strdup ("GLib.Type");
416 str = temporary_type;
419 basic = parse_basic (str);
422 type->is_basic = TRUE;
423 type->tag = basic->tag;
424 type->is_pointer = basic->pointer;
426 str += strlen(basic->str);
430 /* If we're inside GLib, handle "List" etc. by prefixing with
431 * "GLib." so the parsing code below doesn't have to get more
434 if (g_str_has_prefix (str, "List<") ||
435 strcmp (str, "List") == 0)
437 temporary_type = g_strdup_printf ("GLib.List%s", str + 4);
438 str = temporary_type;
440 else if (g_str_has_prefix (str, "SList<") ||
441 strcmp (str, "SList") == 0)
443 temporary_type = g_strdup_printf ("GLib.SList%s", str + 5);
444 str = temporary_type;
446 else if (g_str_has_prefix (str, "HashTable<") ||
447 strcmp (str, "HashTable") == 0)
449 temporary_type = g_strdup_printf ("GLib.HashTable%s", str + 9);
450 str = temporary_type;
452 else if (g_str_has_prefix (str, "Error<") ||
453 strcmp (str, "Error") == 0)
455 temporary_type = g_strdup_printf ("GLib.Error%s", str + 5);
456 str = temporary_type;
461 /* found a basic type */;
462 else if (g_str_has_prefix (str, "GLib.List") ||
463 g_str_has_prefix (str, "GLib.SList"))
465 str += strlen ("GLib.");
466 if (g_str_has_prefix (str, "List"))
468 type->tag = GI_TYPE_TAG_GLIST;
469 type->is_glist = TRUE;
470 type->is_pointer = TRUE;
471 str += strlen ("List");
475 type->tag = GI_TYPE_TAG_GSLIST;
476 type->is_gslist = TRUE;
477 type->is_pointer = TRUE;
478 str += strlen ("SList");
481 else if (g_str_has_prefix (str, "GLib.HashTable"))
483 str += strlen ("GLib.");
485 type->tag = GI_TYPE_TAG_GHASH;
486 type->is_ghashtable = TRUE;
487 type->is_pointer = TRUE;
488 str += strlen ("HashTable");
490 else if (g_str_has_prefix (str, "GLib.Error"))
492 str += strlen ("GLib.");
494 type->tag = GI_TYPE_TAG_ERROR;
495 type->is_error = TRUE;
496 type->is_pointer = TRUE;
497 str += strlen ("Error");
504 end = strchr (str, '>');
505 tmp = g_strndup (str, end - str);
506 type->errors = g_strsplit (tmp, ",", 0);
514 type->tag = GI_TYPE_TAG_INTERFACE;
515 type->is_interface = TRUE;
516 const char *start = str;
518 /* must be an interface type */
519 while (g_ascii_isalnum (*str) ||
526 type->interface = g_strndup (start, str - start);
531 g_assert (type->tag >= 0 && type->tag <= GI_TYPE_TAG_ERROR);
532 g_free (temporary_type);
536 g_ir_node_free ((GIrNode *)type);
537 g_free (temporary_type);
542 resolve_aliases (ParseContext *ctx, const gchar *type)
546 GSList *seen_values = NULL;
550 if (strchr (type, '.') == NULL)
552 prefixed = g_strdup_printf ("%s.%s", ctx->namespace, type);
561 seen_values = g_slist_prepend (seen_values, (char*)lookup);
562 while (g_hash_table_lookup_extended (ctx->current_module->aliases, lookup, &orig, &value))
564 g_debug ("Resolved: %s => %s\n", lookup, (char*)value);
566 if (g_slist_find_custom (seen_values, lookup,
567 (GCompareFunc)strcmp) != NULL)
569 seen_values = g_slist_prepend (seen_values, (gchar*)lookup);
571 g_slist_free (seen_values);
573 if (lookup == prefixed)
582 is_disguised_structure (ParseContext *ctx, const gchar *type)
588 if (strchr (type, '.') == NULL)
590 prefixed = g_strdup_printf ("%s.%s", ctx->namespace, type);
599 result = g_hash_table_lookup (ctx->current_module->disguised_structures,
608 parse_type (ParseContext *ctx, const gchar *type)
611 const BasicTypeInfo *basic;
612 gboolean in_glib, in_gobject;
614 in_glib = strcmp (ctx->namespace, "GLib") == 0;
615 in_gobject = strcmp (ctx->namespace, "GObject") == 0;
617 /* Do not search aliases for basic types */
618 basic = parse_basic (type);
620 type = resolve_aliases (ctx, type);
622 node = parse_type_internal (type, NULL, in_glib, in_gobject);
624 g_debug ("Parsed type: %s => %d", type, node->tag);
626 g_critical ("Failed to parse type: '%s'", type);
632 start_glib_boxed (GMarkupParseContext *context,
633 const gchar *element_name,
634 const gchar **attribute_names,
635 const gchar **attribute_values,
640 const gchar *typename;
641 const gchar *typeinit;
642 const gchar *deprecated;
645 if (!(strcmp (element_name, "glib:boxed") == 0 &&
646 ctx->state == STATE_NAMESPACE))
649 name = find_attribute ("glib:name", attribute_names, attribute_values);
650 typename = find_attribute ("glib:type-name", attribute_names, attribute_values);
651 typeinit = find_attribute ("glib:get-type", attribute_names, attribute_values);
652 deprecated = find_attribute ("deprecated", attribute_names, attribute_values);
656 MISSING_ATTRIBUTE (context, error, element_name, "glib:name");
659 else if (typename == NULL)
661 MISSING_ATTRIBUTE (context, error, element_name, "glib:type-name");
664 else if (typeinit == NULL)
666 MISSING_ATTRIBUTE (context, error, element_name, "glib:get-type");
670 boxed = (GIrNodeBoxed *) g_ir_node_new (G_IR_NODE_BOXED);
672 ((GIrNode *)boxed)->name = g_strdup (name);
673 boxed->gtype_name = g_strdup (typename);
674 boxed->gtype_init = g_strdup (typeinit);
676 boxed->deprecated = TRUE;
678 boxed->deprecated = FALSE;
680 push_node (ctx, (GIrNode *)boxed);
681 ctx->current_module->entries =
682 g_list_append (ctx->current_module->entries, boxed);
684 state_switch (ctx, STATE_BOXED);
690 start_function (GMarkupParseContext *context,
691 const gchar *element_name,
692 const gchar **attribute_names,
693 const gchar **attribute_values,
699 const gchar *deprecated;
701 GIrNodeFunction *function;
702 gboolean found = FALSE;
706 case STATE_NAMESPACE:
707 found = (strcmp (element_name, "function") == 0 ||
708 strcmp (element_name, "callback") == 0);
711 found = strcmp (element_name, "function") == 0;
716 found = (found || strcmp (element_name, "constructor") == 0);
718 case STATE_INTERFACE:
720 strcmp (element_name, "method") == 0 ||
721 strcmp (element_name, "callback") == 0);
723 case STATE_STRUCT_FIELD:
724 ctx->in_embedded_type = TRUE;
725 found = (found || strcmp (element_name, "callback") == 0);
734 name = find_attribute ("name", attribute_names, attribute_values);
735 symbol = find_attribute ("c:identifier", attribute_names, attribute_values);
736 deprecated = find_attribute ("deprecated", attribute_names, attribute_values);
737 throws = find_attribute ("throws", attribute_names, attribute_values);
741 MISSING_ATTRIBUTE (context, error, element_name, "name");
744 else if (strcmp (element_name, "callback") != 0 && symbol == NULL)
746 MISSING_ATTRIBUTE (context, error, element_name, "c:identifier");
750 function = (GIrNodeFunction *) g_ir_node_new (G_IR_NODE_FUNCTION);
752 ((GIrNode *)function)->name = g_strdup (name);
753 function->symbol = g_strdup (symbol);
754 function->parameters = NULL;
756 function->deprecated = TRUE;
758 function->deprecated = FALSE;
760 if (strcmp (element_name, "method") == 0 ||
761 strcmp (element_name, "constructor") == 0)
763 function->is_method = TRUE;
765 if (strcmp (element_name, "constructor") == 0)
766 function->is_constructor = TRUE;
768 function->is_constructor = FALSE;
772 function->is_method = FALSE;
773 function->is_setter = FALSE;
774 function->is_getter = FALSE;
775 function->is_constructor = FALSE;
776 if (strcmp (element_name, "callback") == 0)
777 ((GIrNode *)function)->type = G_IR_NODE_CALLBACK;
780 if (throws && strcmp (throws, "1") == 0)
781 function->throws = TRUE;
783 function->throws = FALSE;
785 if (ctx->node_stack == NULL)
787 ctx->current_module->entries =
788 g_list_append (ctx->current_module->entries, function);
790 else if (ctx->current_typed)
794 field = (GIrNodeField *)ctx->current_typed;
795 field->callback = function;
798 switch (CURRENT_NODE (ctx)->type)
800 case G_IR_NODE_INTERFACE:
801 case G_IR_NODE_OBJECT:
803 GIrNodeInterface *iface;
805 iface = (GIrNodeInterface *)CURRENT_NODE (ctx);
806 iface->members = g_list_append (iface->members, function);
809 case G_IR_NODE_BOXED:
813 boxed = (GIrNodeBoxed *)CURRENT_NODE (ctx);
814 boxed->members = g_list_append (boxed->members, function);
817 case G_IR_NODE_STRUCT:
819 GIrNodeStruct *struct_;
821 struct_ = (GIrNodeStruct *)CURRENT_NODE (ctx);
822 struct_->members = g_list_append (struct_->members, function); }
824 case G_IR_NODE_UNION:
826 GIrNodeUnion *union_;
828 union_ = (GIrNodeUnion *)CURRENT_NODE (ctx);
829 union_->members = g_list_append (union_->members, function);
833 g_assert_not_reached ();
836 push_node(ctx, (GIrNode *)function);
837 state_switch (ctx, STATE_FUNCTION);
843 parse_param_transfer (GIrNodeParam *param, const gchar *transfer)
845 if (transfer == NULL)
847 g_warning ("required attribute 'transfer-ownership' missing");
849 else if (strcmp (transfer, "none") == 0)
851 param->transfer = FALSE;
852 param->shallow_transfer = FALSE;
854 else if (strcmp (transfer, "container") == 0)
856 param->transfer = FALSE;
857 param->shallow_transfer = TRUE;
859 else if (strcmp (transfer, "full") == 0)
861 param->transfer = TRUE;
862 param->shallow_transfer = FALSE;
866 g_warning ("Unknown transfer-ownership value: %s", transfer);
871 start_parameter (GMarkupParseContext *context,
872 const gchar *element_name,
873 const gchar **attribute_names,
874 const gchar **attribute_values,
879 const gchar *direction;
882 const gchar *optional;
883 const gchar *allow_none;
884 const gchar *transfer;
886 const gchar *closure;
887 const gchar *destroy;
890 if (!(strcmp (element_name, "parameter") == 0 &&
891 ctx->state == STATE_FUNCTION_PARAMETERS))
894 name = find_attribute ("name", attribute_names, attribute_values);
895 direction = find_attribute ("direction", attribute_names, attribute_values);
896 retval = find_attribute ("retval", attribute_names, attribute_values);
897 dipper = find_attribute ("dipper", attribute_names, attribute_values);
898 optional = find_attribute ("optional", attribute_names, attribute_values);
899 allow_none = find_attribute ("allow-none", attribute_names, attribute_values);
900 transfer = find_attribute ("transfer-ownership", attribute_names, attribute_values);
901 scope = find_attribute ("scope", attribute_names, attribute_values);
902 closure = find_attribute ("closure", attribute_names, attribute_values);
903 destroy = find_attribute ("destroy", attribute_names, attribute_values);
908 param = (GIrNodeParam *)g_ir_node_new (G_IR_NODE_PARAM);
910 ctx->current_typed = (GIrNode*) param;
911 ctx->current_typed->name = g_strdup (name);
913 state_switch (ctx, STATE_FUNCTION_PARAMETER);
915 if (direction && strcmp (direction, "out") == 0)
920 else if (direction && strcmp (direction, "inout") == 0)
931 if (retval && strcmp (retval, "1") == 0)
932 param->retval = TRUE;
934 param->retval = FALSE;
936 if (dipper && strcmp (dipper, "1") == 0)
937 param->dipper = TRUE;
939 param->dipper = FALSE;
941 if (optional && strcmp (optional, "1") == 0)
942 param->optional = TRUE;
944 param->optional = FALSE;
946 if (allow_none && strcmp (allow_none, "1") == 0)
947 param->allow_none = TRUE;
949 param->allow_none = FALSE;
951 parse_param_transfer (param, transfer);
953 if (scope && strcmp (scope, "call") == 0)
954 param->scope = GI_SCOPE_TYPE_CALL;
955 else if (scope && strcmp (scope, "async") == 0)
956 param->scope = GI_SCOPE_TYPE_ASYNC;
957 else if (scope && strcmp (scope, "notified") == 0)
958 param->scope = GI_SCOPE_TYPE_NOTIFIED;
960 param->scope = GI_SCOPE_TYPE_INVALID;
962 param->closure = closure ? atoi (closure) : -1;
963 param->destroy = destroy ? atoi (destroy) : -1;
965 ((GIrNode *)param)->name = g_strdup (name);
967 switch (CURRENT_NODE (ctx)->type)
969 case G_IR_NODE_FUNCTION:
970 case G_IR_NODE_CALLBACK:
972 GIrNodeFunction *func;
974 func = (GIrNodeFunction *)CURRENT_NODE (ctx);
975 func->parameters = g_list_append (func->parameters, param);
978 case G_IR_NODE_SIGNAL:
980 GIrNodeSignal *signal;
982 signal = (GIrNodeSignal *)CURRENT_NODE (ctx);
983 signal->parameters = g_list_append (signal->parameters, param);
986 case G_IR_NODE_VFUNC:
990 vfunc = (GIrNodeVFunc *)CURRENT_NODE (ctx);
991 vfunc->parameters = g_list_append (vfunc->parameters, param);
995 g_assert_not_reached ();
1002 start_field (GMarkupParseContext *context,
1003 const gchar *element_name,
1004 const gchar **attribute_names,
1005 const gchar **attribute_values,
1010 const gchar *readable;
1011 const gchar *writable;
1013 const gchar *branch;
1014 GIrNodeField *field;
1022 case STATE_INTERFACE:
1028 if (strcmp (element_name, "field") != 0)
1031 name = find_attribute ("name", attribute_names, attribute_values);
1032 readable = find_attribute ("readable", attribute_names, attribute_values);
1033 writable = find_attribute ("writable", attribute_names, attribute_values);
1034 bits = find_attribute ("bits", attribute_names, attribute_values);
1035 branch = find_attribute ("branch", attribute_names, attribute_values);
1039 MISSING_ATTRIBUTE (context, error, element_name, "name");
1043 field = (GIrNodeField *)g_ir_node_new (G_IR_NODE_FIELD);
1044 ctx->current_typed = (GIrNode*) field;
1045 ((GIrNode *)field)->name = g_strdup (name);
1046 /* Fields are assumed to be read-only.
1047 * (see also girwriter.py and generate.c)
1049 field->readable = readable == NULL || strcmp (readable, "0") == 0;
1050 field->writable = writable != NULL && strcmp (writable, "1") == 0;
1053 field->bits = atoi (bits);
1057 switch (CURRENT_NODE (ctx)->type)
1059 case G_IR_NODE_OBJECT:
1061 GIrNodeInterface *iface;
1063 iface = (GIrNodeInterface *)CURRENT_NODE (ctx);
1064 iface->members = g_list_append (iface->members, field);
1065 state_switch (ctx, STATE_CLASS_FIELD);
1068 case G_IR_NODE_INTERFACE:
1070 GIrNodeInterface *iface;
1072 iface = (GIrNodeInterface *)CURRENT_NODE (ctx);
1073 iface->members = g_list_append (iface->members, field);
1074 state_switch (ctx, STATE_INTERFACE_FIELD);
1077 case G_IR_NODE_BOXED:
1079 GIrNodeBoxed *boxed;
1081 boxed = (GIrNodeBoxed *)CURRENT_NODE (ctx);
1082 boxed->members = g_list_append (boxed->members, field);
1083 state_switch (ctx, STATE_BOXED_FIELD);
1086 case G_IR_NODE_STRUCT:
1088 GIrNodeStruct *struct_;
1090 struct_ = (GIrNodeStruct *)CURRENT_NODE (ctx);
1091 struct_->members = g_list_append (struct_->members, field);
1092 state_switch (ctx, STATE_STRUCT_FIELD);
1095 case G_IR_NODE_UNION:
1097 GIrNodeUnion *union_;
1099 union_ = (GIrNodeUnion *)CURRENT_NODE (ctx);
1100 union_->members = g_list_append (union_->members, field);
1103 GIrNodeConstant *constant;
1105 constant = (GIrNodeConstant *) g_ir_node_new (G_IR_NODE_CONSTANT);
1106 ((GIrNode *)constant)->name = g_strdup (name);
1107 constant->value = g_strdup (branch);
1108 constant->type = union_->discriminator_type;
1109 constant->deprecated = FALSE;
1111 union_->discriminators = g_list_append (union_->discriminators, constant);
1113 state_switch (ctx, STATE_UNION_FIELD);
1117 g_assert_not_reached ();
1124 start_alias (GMarkupParseContext *context,
1125 const gchar *element_name,
1126 const gchar **attribute_names,
1127 const gchar **attribute_values,
1132 const gchar *target;
1136 name = find_attribute ("name", attribute_names, attribute_values);
1139 MISSING_ATTRIBUTE (context, error, element_name, "name");
1143 target = find_attribute ("target", attribute_names, attribute_values);
1146 MISSING_ATTRIBUTE (context, error, element_name, "target");
1150 value = g_strdup (target);
1151 key = g_strdup_printf ("%s.%s", ctx->namespace, name);
1152 if (!strchr (target, '.'))
1154 const BasicTypeInfo *basic = parse_basic (target);
1158 /* For non-basic types, re-qualify the interface */
1159 value = g_strdup_printf ("%s.%s", ctx->namespace, target);
1162 g_hash_table_replace (ctx->aliases, key, value);
1168 start_enum (GMarkupParseContext *context,
1169 const gchar *element_name,
1170 const gchar **attribute_names,
1171 const gchar **attribute_values,
1175 if ((strcmp (element_name, "enumeration") == 0 && ctx->state == STATE_NAMESPACE) ||
1176 (strcmp (element_name, "bitfield") == 0 && ctx->state == STATE_NAMESPACE))
1179 const gchar *typename;
1180 const gchar *typeinit;
1181 const gchar *deprecated;
1183 name = find_attribute ("name", attribute_names, attribute_values);
1184 typename = find_attribute ("glib:type-name", attribute_names, attribute_values);
1185 typeinit = find_attribute ("glib:get-type", attribute_names, attribute_values);
1186 deprecated = find_attribute ("deprecated", attribute_names, attribute_values);
1189 MISSING_ATTRIBUTE (context, error, element_name, "name");
1194 if (strcmp (element_name, "enumeration") == 0)
1195 enum_ = (GIrNodeEnum *) g_ir_node_new (G_IR_NODE_ENUM);
1197 enum_ = (GIrNodeEnum *) g_ir_node_new (G_IR_NODE_FLAGS);
1198 ((GIrNode *)enum_)->name = g_strdup (name);
1199 enum_->gtype_name = g_strdup (typename);
1200 enum_->gtype_init = g_strdup (typeinit);
1202 enum_->deprecated = TRUE;
1204 enum_->deprecated = FALSE;
1206 push_node (ctx, (GIrNode *) enum_);
1207 ctx->current_module->entries =
1208 g_list_append (ctx->current_module->entries, enum_);
1210 state_switch (ctx, STATE_ENUM);
1219 start_property (GMarkupParseContext *context,
1220 const gchar *element_name,
1221 const gchar **attribute_names,
1222 const gchar **attribute_values,
1226 if (strcmp (element_name, "property") == 0 &&
1227 (ctx->state == STATE_CLASS ||
1228 ctx->state == STATE_INTERFACE))
1231 const gchar *readable;
1232 const gchar *writable;
1233 const gchar *construct;
1234 const gchar *construct_only;
1236 name = find_attribute ("name", attribute_names, attribute_values);
1237 readable = find_attribute ("readable", attribute_names, attribute_values);
1238 writable = find_attribute ("writable", attribute_names, attribute_values);
1239 construct = find_attribute ("construct", attribute_names, attribute_values);
1240 construct_only = find_attribute ("construct-only", attribute_names, attribute_values);
1243 MISSING_ATTRIBUTE (context, error, element_name, "name");
1246 GIrNodeProperty *property;
1247 GIrNodeInterface *iface;
1249 property = (GIrNodeProperty *) g_ir_node_new (G_IR_NODE_PROPERTY);
1250 ctx->current_typed = (GIrNode*) property;
1252 ((GIrNode *)property)->name = g_strdup (name);
1254 /* Assume properties are readable */
1255 if (readable == NULL || strcmp (readable, "1") == 0)
1256 property->readable = TRUE;
1258 property->readable = FALSE;
1259 if (writable && strcmp (writable, "1") == 0)
1260 property->writable = TRUE;
1262 property->writable = FALSE;
1263 if (construct && strcmp (construct, "1") == 0)
1264 property->construct = TRUE;
1266 property->construct = FALSE;
1267 if (construct_only && strcmp (construct_only, "1") == 0)
1268 property->construct_only = TRUE;
1270 property->construct_only = FALSE;
1272 iface = (GIrNodeInterface *)CURRENT_NODE (ctx);
1273 iface->members = g_list_append (iface->members, property);
1275 if (ctx->state == STATE_CLASS)
1276 state_switch (ctx, STATE_CLASS_PROPERTY);
1277 else if (ctx->state == STATE_INTERFACE)
1278 state_switch (ctx, STATE_INTERFACE_PROPERTY);
1280 g_assert_not_reached ();
1289 parse_value (const gchar *str)
1293 /* FIXME just a quick hack */
1294 shift_op = strstr (str, "<<");
1300 base = strtol (str, NULL, 10);
1301 shift = strtol (shift_op + 3, NULL, 10);
1303 return base << shift;
1306 return strtol (str, NULL, 10);
1312 start_member (GMarkupParseContext *context,
1313 const gchar *element_name,
1314 const gchar **attribute_names,
1315 const gchar **attribute_values,
1319 if (strcmp (element_name, "member") == 0 &&
1320 ctx->state == STATE_ENUM)
1324 const gchar *deprecated;
1326 name = find_attribute ("name", attribute_names, attribute_values);
1327 value = find_attribute ("value", attribute_names, attribute_values);
1328 deprecated = find_attribute ("deprecated", attribute_names, attribute_values);
1331 MISSING_ATTRIBUTE (context, error, element_name, "name");
1335 GIrNodeValue *value_;
1337 value_ = (GIrNodeValue *) g_ir_node_new (G_IR_NODE_VALUE);
1339 ((GIrNode *)value_)->name = g_strdup (name);
1341 value_->value = parse_value (value);
1344 value_->deprecated = TRUE;
1346 value_->deprecated = FALSE;
1348 enum_ = (GIrNodeEnum *)CURRENT_NODE (ctx);
1349 enum_->values = g_list_append (enum_->values, value_);
1358 start_constant (GMarkupParseContext *context,
1359 const gchar *element_name,
1360 const gchar **attribute_names,
1361 const gchar **attribute_values,
1365 if (strcmp (element_name, "constant") == 0 &&
1366 (ctx->state == STATE_NAMESPACE ||
1367 ctx->state == STATE_CLASS ||
1368 ctx->state == STATE_INTERFACE))
1372 const gchar *deprecated;
1374 name = find_attribute ("name", attribute_names, attribute_values);
1375 value = find_attribute ("value", attribute_names, attribute_values);
1376 deprecated = find_attribute ("deprecated", attribute_names, attribute_values);
1379 MISSING_ATTRIBUTE (context, error, element_name, "name");
1380 else if (value == NULL)
1381 MISSING_ATTRIBUTE (context, error, element_name, "value");
1384 GIrNodeConstant *constant;
1386 constant = (GIrNodeConstant *) g_ir_node_new (G_IR_NODE_CONSTANT);
1388 ((GIrNode *)constant)->name = g_strdup (name);
1389 constant->value = g_strdup (value);
1391 ctx->current_typed = (GIrNode*) constant;
1394 constant->deprecated = TRUE;
1396 constant->deprecated = FALSE;
1398 if (ctx->state == STATE_NAMESPACE)
1400 push_node (ctx, (GIrNode *) constant);
1401 ctx->current_module->entries =
1402 g_list_append (ctx->current_module->entries, constant);
1406 GIrNodeInterface *iface;
1408 iface = (GIrNodeInterface *)CURRENT_NODE (ctx);
1409 iface->members = g_list_append (iface->members, constant);
1414 case STATE_NAMESPACE:
1415 state_switch (ctx, STATE_NAMESPACE_CONSTANT);
1418 state_switch (ctx, STATE_CLASS_CONSTANT);
1420 case STATE_INTERFACE:
1421 state_switch (ctx, STATE_INTERFACE_CONSTANT);
1424 g_assert_not_reached ();
1435 start_errordomain (GMarkupParseContext *context,
1436 const gchar *element_name,
1437 const gchar **attribute_names,
1438 const gchar **attribute_values,
1442 if (strcmp (element_name, "errordomain") == 0 &&
1443 ctx->state == STATE_NAMESPACE)
1446 const gchar *getquark;
1448 const gchar *deprecated;
1450 name = find_attribute ("name", attribute_names, attribute_values);
1451 getquark = find_attribute ("get-quark", attribute_names, attribute_values);
1452 codes = find_attribute ("codes", attribute_names, attribute_values);
1453 deprecated = find_attribute ("deprecated", attribute_names, attribute_values);
1456 MISSING_ATTRIBUTE (context, error, element_name, "name");
1457 else if (getquark == NULL)
1458 MISSING_ATTRIBUTE (context, error, element_name, "getquark");
1459 else if (codes == NULL)
1460 MISSING_ATTRIBUTE (context, error, element_name, "codes");
1463 GIrNodeErrorDomain *domain;
1465 domain = (GIrNodeErrorDomain *) g_ir_node_new (G_IR_NODE_ERROR_DOMAIN);
1467 ((GIrNode *)domain)->name = g_strdup (name);
1468 domain->getquark = g_strdup (getquark);
1469 domain->codes = g_strdup (codes);
1472 domain->deprecated = TRUE;
1474 domain->deprecated = FALSE;
1476 push_node (ctx, (GIrNode *) domain);
1477 ctx->current_module->entries =
1478 g_list_append (ctx->current_module->entries, domain);
1480 state_switch (ctx, STATE_ERRORDOMAIN);
1489 start_interface (GMarkupParseContext *context,
1490 const gchar *element_name,
1491 const gchar **attribute_names,
1492 const gchar **attribute_values,
1496 if (strcmp (element_name, "interface") == 0 &&
1497 ctx->state == STATE_NAMESPACE)
1500 const gchar *typename;
1501 const gchar *typeinit;
1502 const gchar *deprecated;
1503 const gchar *glib_type_struct;
1505 name = find_attribute ("name", attribute_names, attribute_values);
1506 typename = find_attribute ("glib:type-name", attribute_names, attribute_values);
1507 typeinit = find_attribute ("glib:get-type", attribute_names, attribute_values);
1508 glib_type_struct = find_attribute ("glib:type-struct", attribute_names, attribute_values);
1509 deprecated = find_attribute ("deprecated", attribute_names, attribute_values);
1512 MISSING_ATTRIBUTE (context, error, element_name, "name");
1513 else if (typename == NULL)
1514 MISSING_ATTRIBUTE (context, error, element_name, "glib:type-name");
1515 else if (typeinit == NULL)
1516 MISSING_ATTRIBUTE (context, error, element_name, "glib:get-type");
1519 GIrNodeInterface *iface;
1521 iface = (GIrNodeInterface *) g_ir_node_new (G_IR_NODE_INTERFACE);
1522 ((GIrNode *)iface)->name = g_strdup (name);
1523 iface->gtype_name = g_strdup (typename);
1524 iface->gtype_init = g_strdup (typeinit);
1525 iface->glib_type_struct = g_strdup (glib_type_struct);
1527 iface->deprecated = TRUE;
1529 iface->deprecated = FALSE;
1531 push_node (ctx, (GIrNode *) iface);
1532 ctx->current_module->entries =
1533 g_list_append (ctx->current_module->entries, iface);
1535 state_switch (ctx, STATE_INTERFACE);
1545 start_class (GMarkupParseContext *context,
1546 const gchar *element_name,
1547 const gchar **attribute_names,
1548 const gchar **attribute_values,
1552 if (strcmp (element_name, "class") == 0 &&
1553 ctx->state == STATE_NAMESPACE)
1556 const gchar *parent;
1557 const gchar *glib_type_struct;
1558 const gchar *typename;
1559 const gchar *typeinit;
1560 const gchar *deprecated;
1561 const gchar *abstract;
1563 name = find_attribute ("name", attribute_names, attribute_values);
1564 parent = find_attribute ("parent", attribute_names, attribute_values);
1565 glib_type_struct = find_attribute ("glib:type-struct", attribute_names, attribute_values);
1566 typename = find_attribute ("glib:type-name", attribute_names, attribute_values);
1567 typeinit = find_attribute ("glib:get-type", attribute_names, attribute_values);
1568 deprecated = find_attribute ("deprecated", attribute_names, attribute_values);
1569 abstract = find_attribute ("abstract", attribute_names, attribute_values);
1572 MISSING_ATTRIBUTE (context, error, element_name, "name");
1573 else if (typename == NULL)
1574 MISSING_ATTRIBUTE (context, error, element_name, "glib:type-name");
1575 else if (typeinit == NULL && strcmp (typename, "GObject"))
1576 MISSING_ATTRIBUTE (context, error, element_name, "glib:get-type");
1579 GIrNodeInterface *iface;
1581 iface = (GIrNodeInterface *) g_ir_node_new (G_IR_NODE_OBJECT);
1582 ((GIrNode *)iface)->name = g_strdup (name);
1583 iface->gtype_name = g_strdup (typename);
1584 iface->gtype_init = g_strdup (typeinit);
1585 iface->parent = g_strdup (parent);
1586 iface->glib_type_struct = g_strdup (glib_type_struct);
1588 iface->deprecated = TRUE;
1590 iface->deprecated = FALSE;
1592 iface->abstract = abstract && strcmp (abstract, "1") == 0;
1594 push_node (ctx, (GIrNode *) iface);
1595 ctx->current_module->entries =
1596 g_list_append (ctx->current_module->entries, iface);
1598 state_switch (ctx, STATE_CLASS);
1607 start_type (GMarkupParseContext *context,
1608 const gchar *element_name,
1609 const gchar **attribute_names,
1610 const gchar **attribute_values,
1617 gboolean is_varargs;
1618 GIrNodeType *typenode;
1620 is_array = strcmp (element_name, "array") == 0;
1621 is_varargs = strcmp (element_name, "varargs") == 0;
1623 if (!(is_array || is_varargs || (strcmp (element_name, "type") == 0)))
1626 if (ctx->state == STATE_TYPE)
1629 ctx->type_stack = g_list_prepend (ctx->type_stack, ctx->type_parameters);
1630 ctx->type_parameters = NULL;
1632 else if (ctx->state == STATE_FUNCTION_PARAMETER ||
1633 ctx->state == STATE_FUNCTION_RETURN ||
1634 ctx->state == STATE_STRUCT_FIELD ||
1635 ctx->state == STATE_UNION_FIELD ||
1636 ctx->state == STATE_CLASS_PROPERTY ||
1637 ctx->state == STATE_CLASS_FIELD ||
1638 ctx->state == STATE_INTERFACE_FIELD ||
1639 ctx->state == STATE_INTERFACE_PROPERTY ||
1640 ctx->state == STATE_BOXED_FIELD ||
1641 ctx->state == STATE_NAMESPACE_CONSTANT ||
1642 ctx->state == STATE_CLASS_CONSTANT ||
1643 ctx->state == STATE_INTERFACE_CONSTANT
1646 state_switch (ctx, STATE_TYPE);
1647 ctx->type_depth = 1;
1650 switch (CURRENT_NODE (ctx)->type)
1652 case G_IR_NODE_FUNCTION:
1653 case G_IR_NODE_CALLBACK:
1655 GIrNodeFunction *func = (GIrNodeFunction *)CURRENT_NODE (ctx);
1656 func->is_varargs = TRUE;
1659 case G_IR_NODE_VFUNC:
1661 GIrNodeVFunc *vfunc = (GIrNodeVFunc *)CURRENT_NODE (ctx);
1662 vfunc->is_varargs = TRUE;
1665 /* list others individually rather than with default: so that compiler
1666 * warns if new node types are added without adding them to the switch
1668 case G_IR_NODE_INVALID:
1669 case G_IR_NODE_ENUM:
1670 case G_IR_NODE_FLAGS:
1671 case G_IR_NODE_CONSTANT:
1672 case G_IR_NODE_ERROR_DOMAIN:
1673 case G_IR_NODE_PARAM:
1674 case G_IR_NODE_TYPE:
1675 case G_IR_NODE_PROPERTY:
1676 case G_IR_NODE_SIGNAL:
1677 case G_IR_NODE_VALUE:
1678 case G_IR_NODE_FIELD:
1679 case G_IR_NODE_XREF:
1680 case G_IR_NODE_STRUCT:
1681 case G_IR_NODE_BOXED:
1682 case G_IR_NODE_OBJECT:
1683 case G_IR_NODE_INTERFACE:
1684 case G_IR_NODE_UNION:
1685 g_assert_not_reached ();
1689 ctx->type_stack = NULL;
1690 ctx->type_parameters = NULL;
1693 if (!ctx->current_typed)
1697 G_MARKUP_ERROR_INVALID_CONTENT,
1698 "The element <type> is invalid here");
1711 typenode = (GIrNodeType *)g_ir_node_new (G_IR_NODE_TYPE);
1713 typenode->tag = GI_TYPE_TAG_ARRAY;
1714 typenode->is_pointer = TRUE;
1715 typenode->is_array = TRUE;
1717 zero = find_attribute ("zero-terminated", attribute_names, attribute_values);
1718 len = find_attribute ("length", attribute_names, attribute_values);
1719 size = find_attribute ("fixed-size", attribute_names, attribute_values);
1721 typenode->zero_terminated = !(zero && strcmp (zero, "1") != 0);
1722 typenode->has_length = len != NULL;
1723 typenode->length = typenode->has_length ? atoi (len) : -1;
1725 typenode->has_size = size != NULL;
1726 typenode->size = typenode->has_size ? atoi (size) : -1;
1729 typenode->zero_terminated = strcmp(zero, "1") == 0;
1731 /* If neither zero-terminated nor length nor fixed-size is given, assume zero-terminated. */
1732 typenode->zero_terminated = !(typenode->has_length || typenode->has_size);
1737 name = find_attribute ("name", attribute_names, attribute_values);
1740 MISSING_ATTRIBUTE (context, error, element_name, "name");
1743 ctype = find_attribute ("c:type", attribute_names, attribute_values);
1746 const char *cp = ctype + strlen(ctype) - 1;
1747 while (cp > ctype && *cp-- == '*')
1751 if (ctx->current_typed->type == G_IR_NODE_PARAM &&
1752 ((GIrNodeParam *)ctx->current_typed)->out &&
1756 typenode = parse_type (ctx, name);
1758 /* A 'disguised' structure is one where the c:type is a typedef that
1759 * doesn't look like a pointer, but is internally.
1761 if (typenode->tag == GI_TYPE_TAG_INTERFACE &&
1762 is_disguised_structure (ctx, typenode->interface))
1765 if (pointer_depth > 0)
1766 typenode->is_pointer = TRUE;
1769 ctx->type_parameters = g_list_append (ctx->type_parameters, typenode);
1775 end_type_top (ParseContext *ctx)
1777 GIrNodeType *typenode;
1779 if (!ctx->type_parameters)
1782 typenode = (GIrNodeType*)ctx->type_parameters->data;
1784 /* Default to pointer for unspecified containers */
1785 if (typenode->tag == GI_TYPE_TAG_ARRAY ||
1786 typenode->tag == GI_TYPE_TAG_GLIST ||
1787 typenode->tag == GI_TYPE_TAG_GSLIST)
1789 if (typenode->parameter_type1 == NULL)
1790 typenode->parameter_type1 = parse_type (ctx, "any");
1792 else if (typenode->tag == GI_TYPE_TAG_GHASH)
1794 if (typenode->parameter_type1 == NULL)
1796 typenode->parameter_type1 = parse_type (ctx, "any");
1797 typenode->parameter_type2 = parse_type (ctx, "any");
1801 switch (ctx->current_typed->type)
1803 case G_IR_NODE_PARAM:
1805 GIrNodeParam *param = (GIrNodeParam *)ctx->current_typed;
1806 param->type = typenode;
1809 case G_IR_NODE_FIELD:
1811 GIrNodeField *field = (GIrNodeField *)ctx->current_typed;
1812 field->type = typenode;
1815 case G_IR_NODE_PROPERTY:
1817 GIrNodeProperty *property = (GIrNodeProperty *) ctx->current_typed;
1818 property->type = typenode;
1821 case G_IR_NODE_CONSTANT:
1823 GIrNodeConstant *constant = (GIrNodeConstant *)ctx->current_typed;
1824 constant->type = typenode;
1828 g_printerr("current node is %d\n", CURRENT_NODE (ctx)->type);
1829 g_assert_not_reached ();
1831 g_list_free (ctx->type_parameters);
1834 ctx->type_depth = 0;
1835 ctx->type_parameters = NULL;
1836 ctx->current_typed = NULL;
1840 end_type_recurse (ParseContext *ctx)
1842 GIrNodeType *parent;
1843 GIrNodeType *param = NULL;
1845 parent = (GIrNodeType *) ((GList*)ctx->type_stack->data)->data;
1846 if (ctx->type_parameters)
1847 param = (GIrNodeType *) ctx->type_parameters->data;
1849 if (parent->tag == GI_TYPE_TAG_ARRAY ||
1850 parent->tag == GI_TYPE_TAG_GLIST ||
1851 parent->tag == GI_TYPE_TAG_GSLIST)
1853 g_assert (param != NULL);
1855 if (parent->parameter_type1 == NULL)
1856 parent->parameter_type1 = param;
1858 g_assert_not_reached ();
1860 else if (parent->tag == GI_TYPE_TAG_GHASH)
1862 g_assert (param != NULL);
1864 if (parent->parameter_type1 == NULL)
1865 parent->parameter_type1 = param;
1866 else if (parent->parameter_type2 == NULL)
1867 parent->parameter_type2 = param;
1869 g_assert_not_reached ();
1871 g_list_free (ctx->type_parameters);
1872 ctx->type_parameters = (GList *)ctx->type_stack->data;
1873 ctx->type_stack = g_list_delete_link (ctx->type_stack, ctx->type_stack);
1877 end_type (ParseContext *ctx)
1879 if (ctx->type_depth == 1)
1882 state_switch (ctx, ctx->prev_state);
1886 end_type_recurse (ctx);
1892 start_attribute (GMarkupParseContext *context,
1893 const gchar *element_name,
1894 const gchar **attribute_names,
1895 const gchar **attribute_values,
1903 if (strcmp (element_name, "attribute") != 0 || ctx->node_stack == NULL)
1906 name = find_attribute ("name", attribute_names, attribute_values);
1907 value = find_attribute ("value", attribute_names, attribute_values);
1911 MISSING_ATTRIBUTE (context, error, element_name, "name");
1916 MISSING_ATTRIBUTE (context, error, element_name, "value");
1920 state_switch (ctx, STATE_ATTRIBUTE);
1922 curnode = CURRENT_NODE (ctx);
1924 g_hash_table_insert (curnode->attributes, g_strdup (name), g_strdup (value));
1930 start_return_value (GMarkupParseContext *context,
1931 const gchar *element_name,
1932 const gchar **attribute_names,
1933 const gchar **attribute_values,
1937 if (strcmp (element_name, "return-value") == 0 &&
1938 ctx->state == STATE_FUNCTION)
1940 GIrNodeParam *param;
1941 const gchar *transfer;
1943 param = (GIrNodeParam *)g_ir_node_new (G_IR_NODE_PARAM);
1946 param->retval = TRUE;
1948 ctx->current_typed = (GIrNode*) param;
1950 state_switch (ctx, STATE_FUNCTION_RETURN);
1952 transfer = find_attribute ("transfer-ownership", attribute_names, attribute_values);
1953 parse_param_transfer (param, transfer);
1955 switch (CURRENT_NODE (ctx)->type)
1957 case G_IR_NODE_FUNCTION:
1958 case G_IR_NODE_CALLBACK:
1960 GIrNodeFunction *func = (GIrNodeFunction *)CURRENT_NODE (ctx);
1961 func->result = param;
1964 case G_IR_NODE_SIGNAL:
1966 GIrNodeSignal *signal = (GIrNodeSignal *)CURRENT_NODE (ctx);
1967 signal->result = param;
1970 case G_IR_NODE_VFUNC:
1972 GIrNodeVFunc *vfunc = (GIrNodeVFunc *)CURRENT_NODE (ctx);
1973 vfunc->result = param;
1977 g_assert_not_reached ();
1987 start_implements (GMarkupParseContext *context,
1988 const gchar *element_name,
1989 const gchar **attribute_names,
1990 const gchar **attribute_values,
1994 GIrNodeInterface *iface;
1997 if (strcmp (element_name, "implements") != 0 ||
1998 !(ctx->state == STATE_CLASS))
2001 state_switch (ctx, STATE_IMPLEMENTS);
2003 name = find_attribute ("name", attribute_names, attribute_values);
2006 MISSING_ATTRIBUTE (context, error, element_name, "name");
2010 iface = (GIrNodeInterface *)CURRENT_NODE (ctx);
2011 iface->interfaces = g_list_append (iface->interfaces, g_strdup (name));
2017 start_glib_signal (GMarkupParseContext *context,
2018 const gchar *element_name,
2019 const gchar **attribute_names,
2020 const gchar **attribute_values,
2024 if (strcmp (element_name, "glib:signal") == 0 &&
2025 (ctx->state == STATE_CLASS ||
2026 ctx->state == STATE_INTERFACE))
2030 const gchar *no_recurse;
2031 const gchar *detailed;
2032 const gchar *action;
2033 const gchar *no_hooks;
2034 const gchar *has_class_closure;
2036 name = find_attribute ("name", attribute_names, attribute_values);
2037 when = find_attribute ("when", attribute_names, attribute_values);
2038 no_recurse = find_attribute ("no-recurse", attribute_names, attribute_values);
2039 detailed = find_attribute ("detailed", attribute_names, attribute_values);
2040 action = find_attribute ("action", attribute_names, attribute_values);
2041 no_hooks = find_attribute ("no-hooks", attribute_names, attribute_values);
2042 has_class_closure = find_attribute ("has-class-closure", attribute_names, attribute_values);
2045 MISSING_ATTRIBUTE (context, error, element_name, "name");
2048 GIrNodeInterface *iface;
2049 GIrNodeSignal *signal;
2051 signal = (GIrNodeSignal *)g_ir_node_new (G_IR_NODE_SIGNAL);
2053 ((GIrNode *)signal)->name = g_strdup (name);
2055 signal->run_first = FALSE;
2056 signal->run_last = FALSE;
2057 signal->run_cleanup = FALSE;
2058 if (when == NULL || strcmp (when, "LAST") == 0)
2059 signal->run_last = TRUE;
2060 else if (strcmp (when, "FIRST") == 0)
2061 signal->run_first = TRUE;
2063 signal->run_cleanup = TRUE;
2065 if (no_recurse && strcmp (no_recurse, "1") == 0)
2066 signal->no_recurse = TRUE;
2068 signal->no_recurse = FALSE;
2069 if (detailed && strcmp (detailed, "1") == 0)
2070 signal->detailed = TRUE;
2072 signal->detailed = FALSE;
2073 if (action && strcmp (action, "1") == 0)
2074 signal->action = TRUE;
2076 signal->action = FALSE;
2077 if (no_hooks && strcmp (no_hooks, "1") == 0)
2078 signal->no_hooks = TRUE;
2080 signal->no_hooks = FALSE;
2081 if (has_class_closure && strcmp (has_class_closure, "1") == 0)
2082 signal->has_class_closure = TRUE;
2084 signal->has_class_closure = FALSE;
2086 iface = (GIrNodeInterface *)CURRENT_NODE (ctx);
2087 iface->members = g_list_append (iface->members, signal);
2089 push_node (ctx, (GIrNode *)signal);
2090 state_switch (ctx, STATE_FUNCTION);
2099 start_vfunc (GMarkupParseContext *context,
2100 const gchar *element_name,
2101 const gchar **attribute_names,
2102 const gchar **attribute_values,
2106 if (strcmp (element_name, "virtual-method") == 0 &&
2107 (ctx->state == STATE_CLASS ||
2108 ctx->state == STATE_INTERFACE))
2111 const gchar *must_chain_up;
2112 const gchar *override;
2113 const gchar *is_class_closure;
2114 const gchar *offset;
2115 const gchar *invoker;
2117 name = find_attribute ("name", attribute_names, attribute_values);
2118 must_chain_up = find_attribute ("must-chain-up", attribute_names, attribute_values);
2119 override = find_attribute ("override", attribute_names, attribute_values);
2120 is_class_closure = find_attribute ("is-class-closure", attribute_names, attribute_values);
2121 offset = find_attribute ("offset", attribute_names, attribute_values);
2122 invoker = find_attribute ("invoker", attribute_names, attribute_values);
2125 MISSING_ATTRIBUTE (context, error, element_name, "name");
2128 GIrNodeInterface *iface;
2129 GIrNodeVFunc *vfunc;
2131 vfunc = (GIrNodeVFunc *)g_ir_node_new (G_IR_NODE_VFUNC);
2133 ((GIrNode *)vfunc)->name = g_strdup (name);
2135 if (must_chain_up && strcmp (must_chain_up, "1") == 0)
2136 vfunc->must_chain_up = TRUE;
2138 vfunc->must_chain_up = FALSE;
2140 if (override && strcmp (override, "always") == 0)
2142 vfunc->must_be_implemented = TRUE;
2143 vfunc->must_not_be_implemented = FALSE;
2145 else if (override && strcmp (override, "never") == 0)
2147 vfunc->must_be_implemented = FALSE;
2148 vfunc->must_not_be_implemented = TRUE;
2152 vfunc->must_be_implemented = FALSE;
2153 vfunc->must_not_be_implemented = FALSE;
2156 if (is_class_closure && strcmp (is_class_closure, "1") == 0)
2157 vfunc->is_class_closure = TRUE;
2159 vfunc->is_class_closure = FALSE;
2162 vfunc->offset = atoi (offset);
2166 vfunc->invoker = g_strdup (invoker);
2168 iface = (GIrNodeInterface *)CURRENT_NODE (ctx);
2169 iface->members = g_list_append (iface->members, vfunc);
2171 push_node (ctx, (GIrNode *)vfunc);
2172 state_switch (ctx, STATE_FUNCTION);
2182 start_struct (GMarkupParseContext *context,
2183 const gchar *element_name,
2184 const gchar **attribute_names,
2185 const gchar **attribute_values,
2189 if (strcmp (element_name, "record") == 0 &&
2190 (ctx->state == STATE_NAMESPACE ||
2191 ctx->state == STATE_UNION ||
2192 ctx->state == STATE_STRUCT ||
2193 ctx->state == STATE_CLASS))
2196 const gchar *deprecated;
2197 const gchar *disguised;
2198 const gchar *gtype_name;
2199 const gchar *gtype_init;
2200 const gchar *gtype_struct;
2201 GIrNodeStruct *struct_;
2203 name = find_attribute ("name", attribute_names, attribute_values);
2204 deprecated = find_attribute ("deprecated", attribute_names, attribute_values);
2205 disguised = find_attribute ("disguised", attribute_names, attribute_values);
2206 gtype_name = find_attribute ("glib:type-name", attribute_names, attribute_values);
2207 gtype_init = find_attribute ("glib:get-type", attribute_names, attribute_values);
2208 gtype_struct = find_attribute ("glib:is-gtype-struct-for", attribute_names, attribute_values);
2210 if (name == NULL && ctx->node_stack == NULL)
2212 MISSING_ATTRIBUTE (context, error, element_name, "name");
2215 if ((gtype_name == NULL && gtype_init != NULL))
2217 MISSING_ATTRIBUTE (context, error, element_name, "glib:type-name");
2220 if ((gtype_name != NULL && gtype_init == NULL))
2222 MISSING_ATTRIBUTE (context, error, element_name, "glib:get-type");
2226 struct_ = (GIrNodeStruct *) g_ir_node_new (G_IR_NODE_STRUCT);
2228 ((GIrNode *)struct_)->name = g_strdup (name ? name : "");
2230 struct_->deprecated = TRUE;
2232 struct_->deprecated = FALSE;
2234 if (disguised && strcmp (disguised, "1") == 0)
2235 struct_->disguised = TRUE;
2237 struct_->is_gtype_struct = gtype_struct != NULL;
2239 struct_->gtype_name = g_strdup (gtype_name);
2240 struct_->gtype_init = g_strdup (gtype_init);
2242 if (ctx->node_stack == NULL)
2243 ctx->current_module->entries =
2244 g_list_append (ctx->current_module->entries, struct_);
2245 push_node (ctx, (GIrNode *)struct_);
2247 state_switch (ctx, STATE_STRUCT);
2255 start_union (GMarkupParseContext *context,
2256 const gchar *element_name,
2257 const gchar **attribute_names,
2258 const gchar **attribute_values,
2262 if (strcmp (element_name, "union") == 0 &&
2263 (ctx->state == STATE_NAMESPACE ||
2264 ctx->state == STATE_UNION ||
2265 ctx->state == STATE_STRUCT ||
2266 ctx->state == STATE_CLASS))
2269 const gchar *deprecated;
2270 const gchar *typename;
2271 const gchar *typeinit;
2273 name = find_attribute ("name", attribute_names, attribute_values);
2274 deprecated = find_attribute ("deprecated", attribute_names, attribute_values);
2275 typename = find_attribute ("glib:type-name", attribute_names, attribute_values);
2276 typeinit = find_attribute ("glib:get-type", attribute_names, attribute_values);
2278 if (name == NULL && ctx->node_stack == NULL)
2279 MISSING_ATTRIBUTE (context, error, element_name, "name");
2282 GIrNodeUnion *union_;
2284 union_ = (GIrNodeUnion *) g_ir_node_new (G_IR_NODE_UNION);
2286 ((GIrNode *)union_)->name = g_strdup (name ? name : "");
2287 union_->gtype_name = g_strdup (typename);
2288 union_->gtype_init = g_strdup (typeinit);
2290 union_->deprecated = TRUE;
2292 union_->deprecated = FALSE;
2294 if (ctx->node_stack == NULL)
2295 ctx->current_module->entries =
2296 g_list_append (ctx->current_module->entries, union_);
2297 push_node (ctx, (GIrNode *)union_);
2299 state_switch (ctx, STATE_UNION);
2307 start_discriminator (GMarkupParseContext *context,
2308 const gchar *element_name,
2309 const gchar **attribute_names,
2310 const gchar **attribute_values,
2314 if (strcmp (element_name, "discriminator") == 0 &&
2315 ctx->state == STATE_UNION)
2318 const gchar *offset;
2320 type = find_attribute ("type", attribute_names, attribute_values);
2321 offset = find_attribute ("offset", attribute_names, attribute_values);
2323 MISSING_ATTRIBUTE (context, error, element_name, "type");
2324 else if (offset == NULL)
2325 MISSING_ATTRIBUTE (context, error, element_name, "offset");
2327 ((GIrNodeUnion *)CURRENT_NODE (ctx))->discriminator_type
2328 = parse_type (ctx, type);
2329 ((GIrNodeUnion *)CURRENT_NODE (ctx))->discriminator_offset
2340 parse_include (GMarkupParseContext *context,
2343 const char *version,
2348 gchar *girpath, *girname;
2349 gboolean success = FALSE;
2353 for (l = ctx->parser->parsed_modules; l; l = l->next)
2355 GIrModule *m = l->data;
2357 if (strcmp (m->name, name) == 0)
2359 if (strcmp (m->version, version) == 0)
2361 ctx->include_modules = g_list_prepend (ctx->include_modules, m);
2369 G_MARKUP_ERROR_INVALID_CONTENT,
2370 "Module '%s' imported with conflicting versions '%s' and '%s'",
2371 name, m->version, version);
2377 girname = g_strdup_printf ("%s-%s.gir", name, version);
2378 girpath = locate_gir (ctx->parser, girname);
2380 if (girpath == NULL)
2384 G_MARKUP_ERROR_INVALID_CONTENT,
2385 "Could not find GIR file '%s'; check XDG_DATA_DIRS or use --includedir",
2392 g_debug ("Parsing include %s", girpath);
2394 if (!g_file_get_contents (girpath, &buffer, &length, error))
2401 modules = g_ir_parser_parse_string (ctx->parser, name, buffer, length, error);
2402 success = error != NULL;
2404 ctx->include_modules = g_list_concat (ctx->include_modules,
2412 extern GLogLevelFlags logged_levels;
2415 start_element_handler (GMarkupParseContext *context,
2416 const gchar *element_name,
2417 const gchar **attribute_names,
2418 const gchar **attribute_values,
2422 ParseContext *ctx = user_data;
2423 gint line_number, char_number;
2425 if (logged_levels & G_LOG_LEVEL_DEBUG)
2427 GString *tags = g_string_new ("");
2429 for (i = 0; attribute_names[i]; i++)
2430 g_string_append_printf (tags, "%s=\"%s\" ",
2432 attribute_values[i]);
2436 g_string_insert_c (tags, 0, ' ');
2437 g_string_truncate (tags, tags->len - 1);
2439 g_debug ("<%s%s>", element_name, tags->str);
2440 g_string_free (tags, TRUE);
2443 switch (element_name[0])
2446 if (ctx->state == STATE_NAMESPACE && strcmp (element_name, "alias") == 0)
2448 state_switch (ctx, STATE_ALIAS);
2451 if (start_type (context, element_name,
2452 attribute_names, attribute_values,
2455 else if (start_attribute (context, element_name,
2456 attribute_names, attribute_values,
2461 if (start_enum (context, element_name,
2462 attribute_names, attribute_values,
2467 if (start_function (context, element_name,
2468 attribute_names, attribute_values,
2471 else if (start_constant (context, element_name,
2472 attribute_names, attribute_values,
2475 else if (start_class (context, element_name,
2476 attribute_names, attribute_values,
2482 if (start_discriminator (context, element_name,
2483 attribute_names, attribute_values,
2489 if (start_enum (context, element_name,
2490 attribute_names, attribute_values,
2493 else if (start_errordomain (context, element_name,
2494 attribute_names, attribute_values,
2500 if (start_function (context, element_name,
2501 attribute_names, attribute_values,
2504 else if (start_field (context, element_name,
2505 attribute_names, attribute_values,
2511 if (start_glib_boxed (context, element_name,
2512 attribute_names, attribute_values,
2515 else if (start_glib_signal (context, element_name,
2516 attribute_names, attribute_values,
2522 if (strcmp (element_name, "include") == 0 &&
2523 ctx->state == STATE_REPOSITORY)
2526 const gchar *version;
2528 name = find_attribute ("name", attribute_names, attribute_values);
2529 version = find_attribute ("version", attribute_names, attribute_values);
2533 MISSING_ATTRIBUTE (context, error, element_name, "name");
2536 if (version == NULL)
2538 MISSING_ATTRIBUTE (context, error, element_name, "version");
2542 if (!parse_include (context, ctx, name, version, error))
2545 ctx->dependencies = g_list_prepend (ctx->dependencies,
2546 g_strdup_printf ("%s-%s", name, version));
2549 state_switch (ctx, STATE_INCLUDE);
2552 if (start_interface (context, element_name,
2553 attribute_names, attribute_values,
2556 else if (start_implements (context, element_name,
2557 attribute_names, attribute_values,
2563 if (start_function (context, element_name,
2564 attribute_names, attribute_values,
2567 else if (start_member (context, element_name,
2568 attribute_names, attribute_values,
2574 if (strcmp (element_name, "namespace") == 0 && ctx->state == STATE_REPOSITORY)
2576 const gchar *name, *version, *shared_library, *cprefix;
2578 if (ctx->current_module != NULL)
2582 G_MARKUP_ERROR_INVALID_CONTENT,
2583 "Only one <namespace/> element is currently allowed per <repository/>");
2587 name = find_attribute ("name", attribute_names, attribute_values);
2588 version = find_attribute ("version", attribute_names, attribute_values);
2589 shared_library = find_attribute ("shared-library", attribute_names, attribute_values);
2590 cprefix = find_attribute ("c:prefix", attribute_names, attribute_values);
2593 MISSING_ATTRIBUTE (context, error, element_name, "name");
2594 else if (version == NULL)
2595 MISSING_ATTRIBUTE (context, error, element_name, "version");
2600 if (strcmp (name, ctx->namespace) != 0)
2603 G_MARKUP_ERROR_INVALID_CONTENT,
2604 "<namespace/> name element '%s' doesn't match file name '%s'",
2605 name, ctx->namespace);
2607 ctx->current_module = g_ir_module_new (name, version, shared_library, cprefix);
2609 ctx->current_module->aliases = ctx->aliases;
2610 ctx->aliases = NULL;
2611 ctx->current_module->disguised_structures = ctx->disguised_structures;
2612 ctx->disguised_structures = NULL;
2614 for (l = ctx->include_modules; l; l = l->next)
2615 g_ir_module_add_include_module (ctx->current_module, l->data);
2617 g_list_free (ctx->include_modules);
2618 ctx->include_modules = NULL;
2620 ctx->modules = g_list_append (ctx->modules, ctx->current_module);
2621 ctx->current_module->dependencies = ctx->dependencies;
2623 state_switch (ctx, STATE_NAMESPACE);
2630 if (start_property (context, element_name,
2631 attribute_names, attribute_values,
2634 else if (strcmp (element_name, "parameters") == 0 &&
2635 ctx->state == STATE_FUNCTION)
2637 state_switch (ctx, STATE_FUNCTION_PARAMETERS);
2641 else if (start_parameter (context, element_name,
2642 attribute_names, attribute_values,
2645 else if (strcmp (element_name, "prerequisite") == 0 &&
2646 ctx->state == STATE_INTERFACE)
2650 name = find_attribute ("name", attribute_names, attribute_values);
2652 state_switch (ctx, STATE_PREREQUISITE);
2655 MISSING_ATTRIBUTE (context, error, element_name, "name");
2658 GIrNodeInterface *iface;
2660 iface = (GIrNodeInterface *)CURRENT_NODE(ctx);
2661 iface->prerequisites = g_list_append (iface->prerequisites, g_strdup (name));
2665 else if (strcmp (element_name, "package") == 0 &&
2666 ctx->state == STATE_REPOSITORY)
2668 state_switch (ctx, STATE_PACKAGE);
2674 if (strcmp (element_name, "repository") == 0 && ctx->state == STATE_START)
2676 const gchar *version;
2678 version = find_attribute ("version", attribute_names, attribute_values);
2680 if (version == NULL)
2681 MISSING_ATTRIBUTE (context, error, element_name, "version");
2682 else if (strcmp (version, "1.0") != 0)
2685 G_MARKUP_ERROR_INVALID_CONTENT,
2686 "Unsupported version '%s'",
2689 state_switch (ctx, STATE_REPOSITORY);
2693 else if (start_return_value (context, element_name,
2694 attribute_names, attribute_values,
2697 else if (start_struct (context, element_name,
2698 attribute_names, attribute_values,
2704 if (start_union (context, element_name,
2705 attribute_names, attribute_values,
2711 if (start_type (context, element_name,
2712 attribute_names, attribute_values,
2718 if (start_vfunc (context, element_name,
2719 attribute_names, attribute_values,
2722 if (start_type (context, element_name,
2723 attribute_names, attribute_values,
2729 if (ctx->state != STATE_UNKNOWN)
2731 state_switch (ctx, STATE_UNKNOWN);
2732 ctx->unknown_depth = 1;
2736 ctx->unknown_depth += 1;
2742 g_markup_parse_context_get_position (context, &line_number, &char_number);
2744 fprintf (stderr, "Error at line %d, character %d: %s\n", line_number, char_number, (*error)->message);
2745 backtrace_stderr ();
2750 require_one_of_end_elements (GMarkupParseContext *context,
2752 const char *actual_name,
2757 int line_number, char_number;
2758 const char *expected;
2759 gboolean matched = FALSE;
2761 va_start (args, error);
2763 while ((expected = va_arg (args, const char*)) != NULL)
2765 if (strcmp (expected, actual_name) == 0)
2777 g_markup_parse_context_get_position (context, &line_number, &char_number);
2780 G_MARKUP_ERROR_INVALID_CONTENT,
2781 "Unexpected end tag '%s' on line %d char %d; current state=%d",
2783 line_number, char_number, ctx->state);
2789 state_switch_end_struct_or_union (GMarkupParseContext *context,
2791 const gchar *element_name,
2795 if (ctx->node_stack == NULL)
2797 state_switch (ctx, STATE_NAMESPACE);
2801 if (CURRENT_NODE (ctx)->type == G_IR_NODE_STRUCT)
2802 state_switch (ctx, STATE_STRUCT);
2803 else if (CURRENT_NODE (ctx)->type == G_IR_NODE_UNION)
2804 state_switch (ctx, STATE_UNION);
2805 else if (CURRENT_NODE (ctx)->type == G_IR_NODE_OBJECT)
2806 state_switch (ctx, STATE_CLASS);
2809 int line_number, char_number;
2810 g_markup_parse_context_get_position (context, &line_number, &char_number);
2813 G_MARKUP_ERROR_INVALID_CONTENT,
2814 "Unexpected end tag '%s' on line %d char %d",
2816 line_number, char_number);
2824 require_end_element (GMarkupParseContext *context,
2826 const char *expected_name,
2827 const char *actual_name,
2830 return require_one_of_end_elements (context, ctx, actual_name, error, expected_name, NULL);
2834 end_element_handler (GMarkupParseContext *context,
2835 const gchar *element_name,
2839 ParseContext *ctx = user_data;
2841 g_debug ("</%s>", element_name);
2847 /* no need to GError here, GMarkup already catches this */
2850 case STATE_REPOSITORY:
2851 state_switch (ctx, STATE_END);
2855 if (require_end_element (context, ctx, "include", element_name, error))
2857 state_switch (ctx, STATE_REPOSITORY);
2862 if (require_end_element (context, ctx, "package", element_name, error))
2864 state_switch (ctx, STATE_REPOSITORY);
2868 case STATE_NAMESPACE:
2869 if (require_end_element (context, ctx, "namespace", element_name, error))
2871 ctx->current_module = NULL;
2872 state_switch (ctx, STATE_REPOSITORY);
2877 if (require_end_element (context, ctx, "alias", element_name, error))
2879 state_switch (ctx, STATE_NAMESPACE);
2883 case STATE_FUNCTION_RETURN:
2884 if (strcmp ("type", element_name) == 0)
2886 if (require_end_element (context, ctx, "return-value", element_name, error))
2888 state_switch (ctx, STATE_FUNCTION);
2892 case STATE_FUNCTION_PARAMETERS:
2893 if (require_end_element (context, ctx, "parameters", element_name, error))
2895 state_switch (ctx, STATE_FUNCTION);
2899 case STATE_FUNCTION_PARAMETER:
2900 if (strcmp ("type", element_name) == 0)
2902 if (require_end_element (context, ctx, "parameter", element_name, error))
2904 state_switch (ctx, STATE_FUNCTION_PARAMETERS);
2908 case STATE_FUNCTION:
2911 if (ctx->node_stack == NULL)
2913 state_switch (ctx, STATE_NAMESPACE);
2917 g_debug("case STATE_FUNCTION %d", CURRENT_NODE (ctx)->type);
2918 if (ctx->in_embedded_type)
2920 ctx->in_embedded_type = FALSE;
2921 state_switch (ctx, STATE_STRUCT_FIELD);
2923 else if (CURRENT_NODE (ctx)->type == G_IR_NODE_INTERFACE)
2924 state_switch (ctx, STATE_INTERFACE);
2925 else if (CURRENT_NODE (ctx)->type == G_IR_NODE_OBJECT)
2926 state_switch (ctx, STATE_CLASS);
2927 else if (CURRENT_NODE (ctx)->type == G_IR_NODE_BOXED)
2928 state_switch (ctx, STATE_BOXED);
2929 else if (CURRENT_NODE (ctx)->type == G_IR_NODE_STRUCT)
2930 state_switch (ctx, STATE_STRUCT);
2931 else if (CURRENT_NODE (ctx)->type == G_IR_NODE_UNION)
2932 state_switch (ctx, STATE_UNION);
2935 int line_number, char_number;
2936 g_markup_parse_context_get_position (context, &line_number, &char_number);
2939 G_MARKUP_ERROR_INVALID_CONTENT,
2940 "Unexpected end tag '%s' on line %d char %d",
2942 line_number, char_number);
2948 case STATE_CLASS_FIELD:
2949 if (strcmp ("type", element_name) == 0)
2951 if (require_end_element (context, ctx, "field", element_name, error))
2953 state_switch (ctx, STATE_CLASS);
2957 case STATE_CLASS_PROPERTY:
2958 if (strcmp ("type", element_name) == 0)
2960 if (require_end_element (context, ctx, "property", element_name, error))
2962 state_switch (ctx, STATE_CLASS);
2967 if (require_end_element (context, ctx, "class", element_name, error))
2970 state_switch (ctx, STATE_NAMESPACE);
2974 case STATE_ERRORDOMAIN:
2975 if (require_end_element (context, ctx, "errordomain", element_name, error))
2978 state_switch (ctx, STATE_NAMESPACE);
2982 case STATE_INTERFACE_PROPERTY:
2983 if (strcmp ("type", element_name) == 0)
2985 if (require_end_element (context, ctx, "property", element_name, error))
2987 state_switch (ctx, STATE_INTERFACE);
2991 case STATE_INTERFACE_FIELD:
2992 if (strcmp ("type", element_name) == 0)
2994 if (require_end_element (context, ctx, "field", element_name, error))
2996 state_switch (ctx, STATE_INTERFACE);
3000 case STATE_INTERFACE:
3001 if (require_end_element (context, ctx, "interface", element_name, error))
3004 state_switch (ctx, STATE_NAMESPACE);
3009 if (strcmp ("member", element_name) == 0)
3011 else if (require_one_of_end_elements (context, ctx,
3012 element_name, error, "enumeration",
3016 state_switch (ctx, STATE_NAMESPACE);
3021 if (require_end_element (context, ctx, "glib:boxed", element_name, error))
3024 state_switch (ctx, STATE_NAMESPACE);
3028 case STATE_BOXED_FIELD:
3029 if (strcmp ("type", element_name) == 0)
3031 if (require_end_element (context, ctx, "field", element_name, error))
3033 state_switch (ctx, STATE_BOXED);
3037 case STATE_STRUCT_FIELD:
3038 if (strcmp ("type", element_name) == 0)
3040 if (require_end_element (context, ctx, "field", element_name, error))
3042 state_switch (ctx, STATE_STRUCT);
3047 if (require_end_element (context, ctx, "record", element_name, error))
3049 state_switch_end_struct_or_union (context, ctx, element_name, error);
3053 case STATE_UNION_FIELD:
3054 if (strcmp ("type", element_name) == 0)
3056 if (require_end_element (context, ctx, "field", element_name, error))
3058 state_switch (ctx, STATE_UNION);
3063 if (require_end_element (context, ctx, "union", element_name, error))
3065 state_switch_end_struct_or_union (context, ctx, element_name, error);
3068 case STATE_IMPLEMENTS:
3069 if (strcmp ("interface", element_name) == 0)
3071 if (require_end_element (context, ctx, "implements", element_name, error))
3072 state_switch (ctx, STATE_CLASS);
3074 case STATE_PREREQUISITE:
3075 if (require_end_element (context, ctx, "prerequisite", element_name, error))
3076 state_switch (ctx, STATE_INTERFACE);
3078 case STATE_NAMESPACE_CONSTANT:
3079 case STATE_CLASS_CONSTANT:
3080 case STATE_INTERFACE_CONSTANT:
3081 if (strcmp ("type", element_name) == 0)
3083 if (require_end_element (context, ctx, "constant", element_name, error))
3087 case STATE_NAMESPACE_CONSTANT:
3089 state_switch (ctx, STATE_NAMESPACE);
3091 case STATE_CLASS_CONSTANT:
3092 state_switch (ctx, STATE_CLASS);
3094 case STATE_INTERFACE_CONSTANT:
3095 state_switch (ctx, STATE_INTERFACE);
3098 g_assert_not_reached ();
3104 if ((strcmp ("type", element_name) == 0) || (strcmp ("array", element_name) == 0) ||
3105 (strcmp ("varargs", element_name) == 0))
3110 case STATE_ATTRIBUTE:
3111 if (strcmp ("attribute", element_name) == 0)
3113 state_switch (ctx, ctx->prev_state);
3118 ctx->unknown_depth -= 1;
3119 if (ctx->unknown_depth == 0)
3120 state_switch (ctx, ctx->prev_state);
3123 g_error ("Unhandled state %d in end_element_handler\n", ctx->state);
3128 text_handler (GMarkupParseContext *context,
3134 /* FIXME warn about non-whitespace text */
3138 cleanup (GMarkupParseContext *context,
3142 ParseContext *ctx = user_data;
3145 for (m = ctx->modules; m; m = m->next)
3146 g_ir_module_free (m->data);
3147 g_list_free (ctx->modules);
3148 ctx->modules = NULL;
3150 ctx->current_module = NULL;
3154 post_filter_toplevel_varargs_functions (GList *list,
3155 GList **varargs_callbacks_out)
3158 GList *varargs_callbacks = *varargs_callbacks_out;
3164 GIrNode *node = iter->data;
3168 if (node->type == G_IR_NODE_FUNCTION)
3170 if (((GIrNodeFunction*)node)->is_varargs)
3172 list = g_list_delete_link (list, link);
3175 if (node->type == G_IR_NODE_CALLBACK)
3177 if (((GIrNodeFunction*)node)->is_varargs)
3179 varargs_callbacks = g_list_append (varargs_callbacks,
3181 list = g_list_delete_link (list, link);
3186 *varargs_callbacks_out = varargs_callbacks;
3192 post_filter_varargs_functions (GList *list, GList ** varargs_callbacks_out)
3195 GList *varargs_callbacks;
3197 list = post_filter_toplevel_varargs_functions (list, varargs_callbacks_out);
3199 varargs_callbacks = *varargs_callbacks_out;
3205 GIrNode *node = iter->data;
3209 if (node->type == G_IR_NODE_FUNCTION)
3212 gboolean function_done = FALSE;
3214 for (param = ((GIrNodeFunction *)node)->parameters;
3216 param = param->next)
3218 GIrNodeParam *node = (GIrNodeParam *)param->data;
3223 if (node->type->is_interface)
3226 for (callback = varargs_callbacks;
3228 callback = callback->next)
3230 if (!strcmp (node->type->interface,
3231 ((GIrNode *)varargs_callbacks->data)->name))
3233 list = g_list_delete_link (list, link);
3234 function_done = TRUE;
3243 *varargs_callbacks_out = varargs_callbacks;
3249 post_filter (GIrModule *module)
3252 GList *varargs_callbacks = NULL;
3254 module->entries = post_filter_varargs_functions (module->entries,
3255 &varargs_callbacks);
3256 iter = module->entries;
3259 GIrNode *node = iter->data;
3263 if (node->type == G_IR_NODE_OBJECT ||
3264 node->type == G_IR_NODE_INTERFACE)
3266 GIrNodeInterface *iface = (GIrNodeInterface*)node;
3267 iface->members = post_filter_varargs_functions (iface->members,
3268 &varargs_callbacks);
3270 else if (node->type == G_IR_NODE_BOXED)
3272 GIrNodeBoxed *boxed = (GIrNodeBoxed*)node;
3273 boxed->members = post_filter_varargs_functions (boxed->members,
3274 &varargs_callbacks);
3276 else if (node->type == G_IR_NODE_STRUCT)
3278 GIrNodeStruct *iface = (GIrNodeStruct*)node;
3279 iface->members = post_filter_varargs_functions (iface->members,
3280 &varargs_callbacks);
3282 else if (node->type == G_IR_NODE_UNION)
3284 GIrNodeUnion *iface = (GIrNodeUnion*)node;
3285 iface->members = post_filter_varargs_functions (iface->members,
3286 &varargs_callbacks);
3289 g_list_free (varargs_callbacks);
3293 * g_ir_parser_parse_string:
3294 * @parser: a #GIrParser
3295 * @namespace: the namespace of the string
3296 * @buffer: the data containing the XML
3297 * @length: length of the data
3298 * @error: return location for a #GError, or %NULL
3300 * Parse a string that holds a complete GIR XML file, and return a list of a
3301 * a #GirModule for each <namespace/> element within the file.
3303 * Returns: a newly allocated list of #GIrModule. The modules themselves
3304 * are owned by the #GIrParser and will be freed along with the parser.
3307 g_ir_parser_parse_string (GIrParser *parser,
3308 const gchar *namespace,
3309 const gchar *buffer,
3313 ParseContext ctx = { 0 };
3314 GMarkupParseContext *context;
3316 ctx.parser = parser;
3317 ctx.state = STATE_START;
3318 ctx.namespace = namespace;
3319 ctx.include_modules = NULL;
3320 ctx.aliases = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
3321 ctx.disguised_structures = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
3323 ctx.dependencies = NULL;
3324 ctx.current_module = NULL;
3326 context = g_markup_parse_context_new (&firstpass_parser, 0, &ctx, NULL);
3328 if (!g_markup_parse_context_parse (context, buffer, length, error))
3331 if (!g_markup_parse_context_end_parse (context, error))
3334 g_markup_parse_context_free (context);
3336 context = g_markup_parse_context_new (&markup_parser, 0, &ctx, NULL);
3337 if (!g_markup_parse_context_parse (context, buffer, length, error))
3340 if (!g_markup_parse_context_end_parse (context, error))
3343 parser->parsed_modules = g_list_concat (g_list_copy (ctx.modules),
3344 parser->parsed_modules);
3348 if (ctx.modules == NULL)
3350 /* An error occurred before we created a module, so we haven't
3351 * transferred ownership of these hash tables to the module.
3353 if (ctx.aliases != NULL)
3354 g_hash_table_destroy (ctx.aliases);
3355 if (ctx.disguised_structures != NULL)
3356 g_hash_table_destroy (ctx.disguised_structures);
3357 g_list_free (ctx.include_modules);
3360 g_markup_parse_context_free (context);
3366 * g_ir_parser_parse_file:
3367 * @parser: a #GIrParser
3368 * @filename: filename to parse
3369 * @error: return location for a #GError, or %NULL
3371 * Parse GIR XML file, and return a list of a a #GirModule for each
3372 * <namespace/> element within the file.
3374 * Returns: a newly allocated list of #GIrModule. The modules themselves
3375 * are owned by the #GIrParser and will be freed along with the parser.
3378 g_ir_parser_parse_file (GIrParser *parser,
3379 const gchar *filename,
3390 if (!g_str_has_suffix (filename, ".gir"))
3394 G_MARKUP_ERROR_INVALID_CONTENT,
3395 "Expected filename to end with '.gir'");
3399 g_debug ("[parsing] filename %s", filename);
3401 slash = g_strrstr (filename, "/");
3403 namespace = g_strdup (filename);
3405 namespace = g_strdup (slash+1);
3406 namespace[strlen(namespace)-4] = '\0';
3408 /* Remove version */
3409 dash = strstr (namespace, "-");
3413 if (!g_file_get_contents (filename, &buffer, &length, error))
3416 modules = g_ir_parser_parse_string (parser, namespace, buffer, length, error);
3418 for (iter = modules; iter; iter = iter->next)
3420 post_filter ((GIrModule*)iter->data);