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"
34 GList *parsed_modules; /* All previously parsed modules */
47 STATE_FUNCTION_RETURN,
48 STATE_FUNCTION_PARAMETERS,
49 STATE_FUNCTION_PARAMETER, /* 10 */
54 STATE_INTERFACE_PROPERTY, /* 15 */
55 STATE_INTERFACE_FIELD,
59 STATE_BOXED_FIELD, /* 20 */
64 STATE_UNION_FIELD, /* 25 */
65 STATE_NAMESPACE_CONSTANT,
67 STATE_INTERFACE_CONSTANT,
72 typedef struct _ParseContext ParseContext;
78 ParseState prev_state;
81 GList *include_modules;
84 GHashTable *disguised_structures;
86 const char *namespace;
87 GIrModule *current_module;
88 GIrNode *current_node;
89 GIrNode *current_typed;
92 GList *type_parameters;
96 static void start_element_handler (GMarkupParseContext *context,
97 const gchar *element_name,
98 const gchar **attribute_names,
99 const gchar **attribute_values,
102 static void end_element_handler (GMarkupParseContext *context,
103 const gchar *element_name,
106 static void text_handler (GMarkupParseContext *context,
111 static void cleanup (GMarkupParseContext *context,
115 static GMarkupParser markup_parser =
117 start_element_handler,
125 start_alias (GMarkupParseContext *context,
126 const gchar *element_name,
127 const gchar **attribute_names,
128 const gchar **attribute_values,
132 static const gchar *find_attribute (const gchar *name,
133 const gchar **attribute_names,
134 const gchar **attribute_values);
138 g_ir_parser_new (void)
140 GIrParser *parser = g_slice_new0 (GIrParser);
146 g_ir_parser_free (GIrParser *parser)
150 if (parser->includes)
151 g_strfreev (parser->includes);
153 for (l = parser->parsed_modules; l; l = l->next)
154 g_ir_module_free (l->data);
156 g_slice_free (GIrParser, parser);
160 g_ir_parser_set_includes (GIrParser *parser,
161 const gchar *const *includes)
163 if (parser->includes)
164 g_strfreev (parser->includes);
166 parser->includes = g_strdupv ((char **)includes);
170 firstpass_start_element_handler (GMarkupParseContext *context,
171 const gchar *element_name,
172 const gchar **attribute_names,
173 const gchar **attribute_values,
177 ParseContext *ctx = user_data;
179 if (strcmp (element_name, "alias") == 0)
181 start_alias (context, element_name, attribute_names, attribute_values,
184 else if (strcmp (element_name, "record") == 0)
187 const gchar *disguised;
189 name = find_attribute ("name", attribute_names, attribute_values);
190 disguised = find_attribute ("disguised", attribute_names, attribute_values);
192 if (disguised && strcmp (disguised, "1") == 0)
196 key = g_strdup_printf ("%s.%s", ctx->namespace, name);
197 g_hash_table_replace (ctx->disguised_structures, key, GINT_TO_POINTER (1));
203 firstpass_end_element_handler (GMarkupParseContext *context,
204 const gchar *element_name,
210 static GMarkupParser firstpass_parser =
212 firstpass_start_element_handler,
213 firstpass_end_element_handler,
220 locate_gir (GIrParser *parser,
224 const gchar *const *datadirs;
225 const gchar *const *dir;
229 datadirs = g_get_system_data_dirs ();
231 girname = g_strdup_printf ("%s-%s.gir", name, version);
233 if (parser->includes != NULL)
235 for (dir = (const gchar *const *)parser->includes; *dir; dir++)
237 path = g_build_filename (*dir, girname, NULL);
238 if (g_file_test (path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))
244 for (dir = datadirs; *dir; dir++)
246 path = g_build_filename (*dir, "gir", girname, NULL);
247 if (g_file_test (path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))
256 #define MISSING_ATTRIBUTE(ctx,error,element,attribute) \
258 int line_number, char_number; \
259 g_markup_parse_context_get_position (context, &line_number, &char_number); \
260 g_set_error (error, \
262 G_MARKUP_ERROR_INVALID_CONTENT, \
263 "Line %d, character %d: The attribute '%s' on the element '%s' must be specified", \
264 line_number, char_number, attribute, element); \
268 backtrace_stderr (void)
270 #if defined(HAVE_BACKTRACE) && defined(HAVE_BACKTRACE_SYMBOLS)
276 size = backtrace (array, 50);
277 strings = (char**) backtrace_symbols (array, size);
279 fprintf (stderr, "--- BACKTRACE (%zd frames) ---\n", size);
281 for (i = 0; i < size; i++)
282 fprintf (stderr, "%s\n", strings[i]);
284 fprintf (stderr, "--- END BACKTRACE ---\n", size);
292 find_attribute (const gchar *name,
293 const gchar **attribute_names,
294 const gchar **attribute_values)
298 for (i = 0; attribute_names[i] != NULL; i++)
299 if (strcmp (attribute_names[i], name) == 0)
300 return attribute_values[i];
306 state_switch (ParseContext *ctx, ParseState newstate)
308 g_debug ("State: %d", newstate);
309 ctx->prev_state = ctx->state;
310 ctx->state = newstate;
313 static GIrNodeType * parse_type_internal (const gchar *str, gchar **next, gboolean in_glib,
314 gboolean in_gobject);
322 static BasicTypeInfo basic_types[] = {
323 { "none", GI_TYPE_TAG_VOID, 0 },
324 { "any", GI_TYPE_TAG_VOID, 1 },
326 { "bool", GI_TYPE_TAG_BOOLEAN, 0 },
327 { "char", GI_TYPE_TAG_INT8, 0 },
328 { "int8", GI_TYPE_TAG_INT8, 0 },
329 { "uint8", GI_TYPE_TAG_UINT8, 0 },
330 { "int16", GI_TYPE_TAG_INT16, 0 },
331 { "uint16", GI_TYPE_TAG_UINT16, 0 },
332 { "int32", GI_TYPE_TAG_INT32, 0 },
333 { "uint32", GI_TYPE_TAG_UINT32, 0 },
334 { "int64", GI_TYPE_TAG_INT64, 0 },
335 { "uint64", GI_TYPE_TAG_UINT64, 0 },
336 { "int", GI_TYPE_TAG_INT, 0 },
337 { "uint", GI_TYPE_TAG_UINT, 0 },
338 { "long", GI_TYPE_TAG_LONG, 0 },
339 { "ulong", GI_TYPE_TAG_ULONG, 0 },
340 { "ssize_t", GI_TYPE_TAG_SSIZE, 0 },
341 { "ssize", GI_TYPE_TAG_SSIZE, 0 },
342 { "size_t", GI_TYPE_TAG_SIZE, 0 },
343 { "size", GI_TYPE_TAG_SIZE, 0 },
344 { "float", GI_TYPE_TAG_FLOAT, 0 },
345 { "double", GI_TYPE_TAG_DOUBLE, 0 },
346 { "time_t", GI_TYPE_TAG_TIME_T, 0 },
347 { "GType", GI_TYPE_TAG_GTYPE, 0 },
348 { "utf8", GI_TYPE_TAG_UTF8, 1 },
349 { "filename", GI_TYPE_TAG_FILENAME,1 },
352 static const BasicTypeInfo *
353 parse_basic (const char *str)
356 gint n_basic = G_N_ELEMENTS (basic_types);
358 for (i = 0; i < n_basic; i++)
360 if (g_str_has_prefix (str, basic_types[i].str))
361 return &(basic_types[i]);
367 parse_type_internal (const gchar *str, char **next, gboolean in_glib,
370 const BasicTypeInfo *basic;
372 char *temporary_type = NULL;
374 type = (GIrNodeType *)g_ir_node_new (G_IR_NODE_TYPE);
376 type->unparsed = g_strdup (str);
378 /* See comment below on GLib.List handling */
379 if (in_gobject && strcmp (str, "Type") == 0)
381 temporary_type = g_strdup ("GLib.Type");
382 str = temporary_type;
385 basic = parse_basic (str);
388 type->is_basic = TRUE;
389 type->tag = basic->tag;
390 type->is_pointer = basic->pointer;
392 str += strlen(basic->str);
396 /* If we're inside GLib, handle "List" etc. by prefixing with
397 * "GLib." so the parsing code below doesn't have to get more
400 if (g_str_has_prefix (str, "List<") ||
401 strcmp (str, "List") == 0)
403 temporary_type = g_strdup_printf ("GLib.List%s", str + 4);
404 str = temporary_type;
406 else if (g_str_has_prefix (str, "SList<") ||
407 strcmp (str, "SList") == 0)
409 temporary_type = g_strdup_printf ("GLib.SList%s", str + 5);
410 str = temporary_type;
412 else if (g_str_has_prefix (str, "HashTable<") ||
413 strcmp (str, "HashTable") == 0)
415 temporary_type = g_strdup_printf ("GLib.HashTable%s", str + 9);
416 str = temporary_type;
418 else if (g_str_has_prefix (str, "Error<") ||
419 strcmp (str, "Error") == 0)
421 temporary_type = g_strdup_printf ("GLib.Error%s", str + 5);
422 str = temporary_type;
427 /* found a basic type */;
428 else if (g_str_has_prefix (str, "GLib.List") ||
429 g_str_has_prefix (str, "GLib.SList"))
431 str += strlen ("GLib.");
432 if (g_str_has_prefix (str, "List"))
434 type->tag = GI_TYPE_TAG_GLIST;
435 type->is_glist = TRUE;
436 type->is_pointer = TRUE;
437 str += strlen ("List");
441 type->tag = GI_TYPE_TAG_GSLIST;
442 type->is_gslist = TRUE;
443 type->is_pointer = TRUE;
444 str += strlen ("SList");
447 else if (g_str_has_prefix (str, "GLib.HashTable"))
449 str += strlen ("GLib.");
451 type->tag = GI_TYPE_TAG_GHASH;
452 type->is_ghashtable = TRUE;
453 type->is_pointer = TRUE;
454 str += strlen ("HashTable");
456 else if (g_str_has_prefix (str, "GLib.Error"))
458 str += strlen ("GLib.");
460 type->tag = GI_TYPE_TAG_ERROR;
461 type->is_error = TRUE;
462 type->is_pointer = TRUE;
463 str += strlen ("Error");
470 end = strchr (str, '>');
471 tmp = g_strndup (str, end - str);
472 type->errors = g_strsplit (tmp, ",", 0);
480 type->tag = GI_TYPE_TAG_INTERFACE;
481 type->is_interface = TRUE;
482 const char *start = str;
484 /* must be an interface type */
485 while (g_ascii_isalnum (*str) ||
492 type->interface = g_strndup (start, str - start);
497 g_assert (type->tag >= 0 && type->tag <= GI_TYPE_TAG_ERROR);
498 g_free (temporary_type);
502 g_ir_node_free ((GIrNode *)type);
503 g_free (temporary_type);
508 resolve_aliases (ParseContext *ctx, const gchar *type)
512 GSList *seen_values = NULL;
516 if (strchr (type, '.') == NULL)
518 prefixed = g_strdup_printf ("%s.%s", ctx->namespace, type);
527 seen_values = g_slist_prepend (seen_values, (char*)lookup);
528 while (g_hash_table_lookup_extended (ctx->current_module->aliases, lookup, &orig, &value))
530 g_debug ("Resolved: %s => %s\n", lookup, (char*)value);
532 if (g_slist_find_custom (seen_values, lookup,
533 (GCompareFunc)strcmp) != NULL)
535 seen_values = g_slist_prepend (seen_values, (gchar*)lookup);
537 g_slist_free (seen_values);
539 if (lookup == prefixed)
548 is_disguised_structure (ParseContext *ctx, const gchar *type)
554 if (strchr (type, '.') == NULL)
556 prefixed = g_strdup_printf ("%s.%s", ctx->namespace, type);
565 result = g_hash_table_lookup (ctx->current_module->disguised_structures,
574 parse_type (ParseContext *ctx, const gchar *type)
577 const BasicTypeInfo *basic;
578 gboolean in_glib, in_gobject;
580 in_glib = strcmp (ctx->namespace, "GLib") == 0;
581 in_gobject = strcmp (ctx->namespace, "GObject") == 0;
583 /* Do not search aliases for basic types */
584 basic = parse_basic (type);
586 type = resolve_aliases (ctx, type);
588 node = parse_type_internal (type, NULL, in_glib, in_gobject);
590 g_debug ("Parsed type: %s => %d", type, node->tag);
592 g_critical ("Failed to parse type: '%s'", type);
598 start_glib_boxed (GMarkupParseContext *context,
599 const gchar *element_name,
600 const gchar **attribute_names,
601 const gchar **attribute_values,
606 const gchar *typename;
607 const gchar *typeinit;
608 const gchar *deprecated;
611 if (!(strcmp (element_name, "glib:boxed") == 0 &&
612 ctx->state == STATE_NAMESPACE))
615 name = find_attribute ("glib:name", attribute_names, attribute_values);
616 typename = find_attribute ("glib:type-name", attribute_names, attribute_values);
617 typeinit = find_attribute ("glib:get-type", attribute_names, attribute_values);
618 deprecated = find_attribute ("deprecated", attribute_names, attribute_values);
622 MISSING_ATTRIBUTE (context, error, element_name, "glib:name");
625 else if (typename == NULL)
627 MISSING_ATTRIBUTE (context, error, element_name, "glib:type-name");
630 else if (typeinit == NULL)
632 MISSING_ATTRIBUTE (context, error, element_name, "glib:get-type");
636 boxed = (GIrNodeBoxed *) g_ir_node_new (G_IR_NODE_BOXED);
638 ((GIrNode *)boxed)->name = g_strdup (name);
639 boxed->gtype_name = g_strdup (typename);
640 boxed->gtype_init = g_strdup (typeinit);
642 boxed->deprecated = TRUE;
644 boxed->deprecated = FALSE;
646 ctx->current_node = (GIrNode *)boxed;
647 ctx->current_module->entries =
648 g_list_append (ctx->current_module->entries, boxed);
650 state_switch (ctx, STATE_BOXED);
656 start_function (GMarkupParseContext *context,
657 const gchar *element_name,
658 const gchar **attribute_names,
659 const gchar **attribute_values,
665 const gchar *deprecated;
667 GIrNodeFunction *function;
668 gboolean found = FALSE;
672 case STATE_NAMESPACE:
673 found = (strcmp (element_name, "function") == 0 ||
674 strcmp (element_name, "callback") == 0);
680 found = strcmp (element_name, "constructor") == 0;
682 case STATE_INTERFACE:
684 strcmp (element_name, "method") == 0 ||
685 strcmp (element_name, "callback") == 0);
694 name = find_attribute ("name", attribute_names, attribute_values);
695 symbol = find_attribute ("c:identifier", attribute_names, attribute_values);
696 deprecated = find_attribute ("deprecated", attribute_names, attribute_values);
697 throws = find_attribute ("throws", attribute_names, attribute_values);
701 MISSING_ATTRIBUTE (context, error, element_name, "name");
704 else if (strcmp (element_name, "callback") != 0 && symbol == NULL)
706 MISSING_ATTRIBUTE (context, error, element_name, "c:identifier");
710 function = (GIrNodeFunction *) g_ir_node_new (G_IR_NODE_FUNCTION);
712 ((GIrNode *)function)->name = g_strdup (name);
713 function->symbol = g_strdup (symbol);
714 function->parameters = NULL;
716 function->deprecated = TRUE;
718 function->deprecated = FALSE;
720 if (strcmp (element_name, "method") == 0 ||
721 strcmp (element_name, "constructor") == 0)
723 function->is_method = TRUE;
725 if (strcmp (element_name, "constructor") == 0)
726 function->is_constructor = TRUE;
728 function->is_constructor = FALSE;
732 function->is_method = FALSE;
733 function->is_setter = FALSE;
734 function->is_getter = FALSE;
735 function->is_constructor = FALSE;
736 if (strcmp (element_name, "callback") == 0)
737 ((GIrNode *)function)->type = G_IR_NODE_CALLBACK;
740 if (throws && strcmp (throws, "1") == 0)
741 function->throws = TRUE;
743 function->throws = FALSE;
745 if (ctx->current_node == NULL)
747 ctx->current_module->entries =
748 g_list_append (ctx->current_module->entries, function);
751 switch (ctx->current_node->type)
753 case G_IR_NODE_INTERFACE:
754 case G_IR_NODE_OBJECT:
756 GIrNodeInterface *iface;
758 iface = (GIrNodeInterface *)ctx->current_node;
759 iface->members = g_list_append (iface->members, function);
762 case G_IR_NODE_BOXED:
766 boxed = (GIrNodeBoxed *)ctx->current_node;
767 boxed->members = g_list_append (boxed->members, function);
770 case G_IR_NODE_STRUCT:
772 GIrNodeStruct *struct_;
774 struct_ = (GIrNodeStruct *)ctx->current_node;
775 struct_->members = g_list_append (struct_->members, function); }
777 case G_IR_NODE_UNION:
779 GIrNodeUnion *union_;
781 union_ = (GIrNodeUnion *)ctx->current_node;
782 union_->members = g_list_append (union_->members, function);
786 g_assert_not_reached ();
789 ctx->current_node = (GIrNode *)function;
790 state_switch (ctx, STATE_FUNCTION);
796 parse_param_transfer (GIrNodeParam *param, const gchar *transfer)
798 if (transfer == NULL)
800 g_warning ("required attribute 'transfer-ownership' missing");
802 else if (strcmp (transfer, "none") == 0)
804 param->transfer = FALSE;
805 param->shallow_transfer = FALSE;
807 else if (strcmp (transfer, "container") == 0)
809 param->transfer = FALSE;
810 param->shallow_transfer = TRUE;
812 else if (strcmp (transfer, "full") == 0)
814 param->transfer = TRUE;
815 param->shallow_transfer = FALSE;
819 g_warning ("Unknown transfer-ownership value: %s", transfer);
824 start_parameter (GMarkupParseContext *context,
825 const gchar *element_name,
826 const gchar **attribute_names,
827 const gchar **attribute_values,
832 const gchar *direction;
835 const gchar *optional;
836 const gchar *allow_none;
837 const gchar *transfer;
840 if (!(strcmp (element_name, "parameter") == 0 &&
841 ctx->state == STATE_FUNCTION_PARAMETERS))
844 name = find_attribute ("name", attribute_names, attribute_values);
845 direction = find_attribute ("direction", attribute_names, attribute_values);
846 retval = find_attribute ("retval", attribute_names, attribute_values);
847 dipper = find_attribute ("dipper", attribute_names, attribute_values);
848 optional = find_attribute ("optional", attribute_names, attribute_values);
849 allow_none = find_attribute ("allow-none", attribute_names, attribute_values);
850 transfer = find_attribute ("transfer-ownership", attribute_names, attribute_values);
855 param = (GIrNodeParam *)g_ir_node_new (G_IR_NODE_PARAM);
857 ctx->current_typed = (GIrNode*) param;
858 ctx->current_typed->name = g_strdup (name);
860 state_switch (ctx, STATE_FUNCTION_PARAMETER);
862 if (direction && strcmp (direction, "out") == 0)
867 else if (direction && strcmp (direction, "inout") == 0)
878 if (retval && strcmp (retval, "1") == 0)
879 param->retval = TRUE;
881 param->retval = FALSE;
883 if (dipper && strcmp (dipper, "1") == 0)
884 param->dipper = TRUE;
886 param->dipper = FALSE;
888 if (optional && strcmp (optional, "1") == 0)
889 param->optional = TRUE;
891 param->optional = FALSE;
893 if (allow_none && strcmp (allow_none, "1") == 0)
894 param->allow_none = TRUE;
896 param->allow_none = FALSE;
898 parse_param_transfer (param, transfer);
900 ((GIrNode *)param)->name = g_strdup (name);
902 switch (ctx->current_node->type)
904 case G_IR_NODE_FUNCTION:
905 case G_IR_NODE_CALLBACK:
907 GIrNodeFunction *func;
909 func = (GIrNodeFunction *)ctx->current_node;
910 func->parameters = g_list_append (func->parameters, param);
913 case G_IR_NODE_SIGNAL:
915 GIrNodeSignal *signal;
917 signal = (GIrNodeSignal *)ctx->current_node;
918 signal->parameters = g_list_append (signal->parameters, param);
921 case G_IR_NODE_VFUNC:
925 vfunc = (GIrNodeVFunc *)ctx->current_node;
926 vfunc->parameters = g_list_append (vfunc->parameters, param);
930 g_assert_not_reached ();
937 start_field (GMarkupParseContext *context,
938 const gchar *element_name,
939 const gchar **attribute_names,
940 const gchar **attribute_values,
945 const gchar *readable;
946 const gchar *writable;
957 case STATE_INTERFACE:
963 if (strcmp (element_name, "field") != 0)
966 name = find_attribute ("name", attribute_names, attribute_values);
967 readable = find_attribute ("readable", attribute_names, attribute_values);
968 writable = find_attribute ("writable", attribute_names, attribute_values);
969 bits = find_attribute ("bits", attribute_names, attribute_values);
970 branch = find_attribute ("branch", attribute_names, attribute_values);
974 MISSING_ATTRIBUTE (context, error, element_name, "name");
978 field = (GIrNodeField *)g_ir_node_new (G_IR_NODE_FIELD);
979 ctx->current_typed = (GIrNode*) field;
980 ((GIrNode *)field)->name = g_strdup (name);
981 /* Fields are assumed to be read-only.
982 * (see also girwriter.py and generate.c)
984 field->readable = readable == NULL || strcmp (readable, "0") == 0;
985 field->writable = writable != NULL && strcmp (writable, "1") == 0;
988 field->bits = atoi (bits);
992 switch (ctx->current_node->type)
994 case G_IR_NODE_OBJECT:
996 GIrNodeInterface *iface;
998 iface = (GIrNodeInterface *)ctx->current_node;
999 iface->members = g_list_append (iface->members, field);
1000 state_switch (ctx, STATE_CLASS_FIELD);
1003 case G_IR_NODE_INTERFACE:
1005 GIrNodeInterface *iface;
1007 iface = (GIrNodeInterface *)ctx->current_node;
1008 iface->members = g_list_append (iface->members, field);
1009 state_switch (ctx, STATE_INTERFACE_FIELD);
1012 case G_IR_NODE_BOXED:
1014 GIrNodeBoxed *boxed;
1016 boxed = (GIrNodeBoxed *)ctx->current_node;
1017 boxed->members = g_list_append (boxed->members, field);
1018 state_switch (ctx, STATE_BOXED_FIELD);
1021 case G_IR_NODE_STRUCT:
1023 GIrNodeStruct *struct_;
1025 struct_ = (GIrNodeStruct *)ctx->current_node;
1026 struct_->members = g_list_append (struct_->members, field);
1027 state_switch (ctx, STATE_STRUCT_FIELD);
1030 case G_IR_NODE_UNION:
1032 GIrNodeUnion *union_;
1034 union_ = (GIrNodeUnion *)ctx->current_node;
1035 union_->members = g_list_append (union_->members, field);
1038 GIrNodeConstant *constant;
1040 constant = (GIrNodeConstant *) g_ir_node_new (G_IR_NODE_CONSTANT);
1041 ((GIrNode *)constant)->name = g_strdup (name);
1042 constant->value = g_strdup (branch);
1043 constant->type = union_->discriminator_type;
1044 constant->deprecated = FALSE;
1046 union_->discriminators = g_list_append (union_->discriminators, constant);
1048 state_switch (ctx, STATE_UNION_FIELD);
1052 g_assert_not_reached ();
1059 start_alias (GMarkupParseContext *context,
1060 const gchar *element_name,
1061 const gchar **attribute_names,
1062 const gchar **attribute_values,
1067 const gchar *target;
1071 name = find_attribute ("name", attribute_names, attribute_values);
1074 MISSING_ATTRIBUTE (context, error, element_name, "name");
1078 target = find_attribute ("target", attribute_names, attribute_values);
1081 MISSING_ATTRIBUTE (context, error, element_name, "target");
1085 value = g_strdup (target);
1086 key = g_strdup_printf ("%s.%s", ctx->namespace, name);
1087 if (!strchr (target, '.'))
1089 const BasicTypeInfo *basic = parse_basic (target);
1093 /* For non-basic types, re-qualify the interface */
1094 value = g_strdup_printf ("%s.%s", ctx->namespace, target);
1097 g_hash_table_replace (ctx->aliases, key, value);
1103 start_enum (GMarkupParseContext *context,
1104 const gchar *element_name,
1105 const gchar **attribute_names,
1106 const gchar **attribute_values,
1110 if ((strcmp (element_name, "enumeration") == 0 && ctx->state == STATE_NAMESPACE) ||
1111 (strcmp (element_name, "bitfield") == 0 && ctx->state == STATE_NAMESPACE))
1114 const gchar *typename;
1115 const gchar *typeinit;
1116 const gchar *deprecated;
1118 name = find_attribute ("name", attribute_names, attribute_values);
1119 typename = find_attribute ("glib:type-name", attribute_names, attribute_values);
1120 typeinit = find_attribute ("glib:get-type", attribute_names, attribute_values);
1121 deprecated = find_attribute ("deprecated", attribute_names, attribute_values);
1124 MISSING_ATTRIBUTE (context, error, element_name, "name");
1129 if (strcmp (element_name, "enumeration") == 0)
1130 enum_ = (GIrNodeEnum *) g_ir_node_new (G_IR_NODE_ENUM);
1132 enum_ = (GIrNodeEnum *) g_ir_node_new (G_IR_NODE_FLAGS);
1133 ((GIrNode *)enum_)->name = g_strdup (name);
1134 enum_->gtype_name = g_strdup (typename);
1135 enum_->gtype_init = g_strdup (typeinit);
1137 enum_->deprecated = TRUE;
1139 enum_->deprecated = FALSE;
1141 ctx->current_node = (GIrNode *) enum_;
1142 ctx->current_module->entries =
1143 g_list_append (ctx->current_module->entries, enum_);
1145 state_switch (ctx, STATE_ENUM);
1154 start_property (GMarkupParseContext *context,
1155 const gchar *element_name,
1156 const gchar **attribute_names,
1157 const gchar **attribute_values,
1161 if (strcmp (element_name, "property") == 0 &&
1162 (ctx->state == STATE_CLASS ||
1163 ctx->state == STATE_INTERFACE))
1166 const gchar *readable;
1167 const gchar *writable;
1168 const gchar *construct;
1169 const gchar *construct_only;
1171 name = find_attribute ("name", attribute_names, attribute_values);
1172 readable = find_attribute ("readable", attribute_names, attribute_values);
1173 writable = find_attribute ("writable", attribute_names, attribute_values);
1174 construct = find_attribute ("construct", attribute_names, attribute_values);
1175 construct_only = find_attribute ("construct-only", attribute_names, attribute_values);
1178 MISSING_ATTRIBUTE (context, error, element_name, "name");
1181 GIrNodeProperty *property;
1182 GIrNodeInterface *iface;
1184 property = (GIrNodeProperty *) g_ir_node_new (G_IR_NODE_PROPERTY);
1185 ctx->current_typed = (GIrNode*) property;
1187 ((GIrNode *)property)->name = g_strdup (name);
1189 /* Assume properties are readable */
1190 if (readable == NULL || strcmp (readable, "1") == 0)
1191 property->readable = TRUE;
1193 property->readable = FALSE;
1194 if (writable && strcmp (writable, "1") == 0)
1195 property->writable = TRUE;
1197 property->writable = FALSE;
1198 if (construct && strcmp (construct, "1") == 0)
1199 property->construct = TRUE;
1201 property->construct = FALSE;
1202 if (construct_only && strcmp (construct_only, "1") == 0)
1203 property->construct_only = TRUE;
1205 property->construct_only = FALSE;
1207 iface = (GIrNodeInterface *)ctx->current_node;
1208 iface->members = g_list_append (iface->members, property);
1210 if (ctx->state == STATE_CLASS)
1211 state_switch (ctx, STATE_CLASS_PROPERTY);
1212 else if (ctx->state == STATE_INTERFACE)
1213 state_switch (ctx, STATE_INTERFACE_PROPERTY);
1215 g_assert_not_reached ();
1224 parse_value (const gchar *str)
1228 /* FIXME just a quick hack */
1229 shift_op = strstr (str, "<<");
1235 base = strtol (str, NULL, 10);
1236 shift = strtol (shift_op + 3, NULL, 10);
1238 return base << shift;
1241 return strtol (str, NULL, 10);
1247 start_member (GMarkupParseContext *context,
1248 const gchar *element_name,
1249 const gchar **attribute_names,
1250 const gchar **attribute_values,
1254 if (strcmp (element_name, "member") == 0 &&
1255 ctx->state == STATE_ENUM)
1259 const gchar *deprecated;
1261 name = find_attribute ("name", attribute_names, attribute_values);
1262 value = find_attribute ("value", attribute_names, attribute_values);
1263 deprecated = find_attribute ("deprecated", attribute_names, attribute_values);
1266 MISSING_ATTRIBUTE (context, error, element_name, "name");
1270 GIrNodeValue *value_;
1272 value_ = (GIrNodeValue *) g_ir_node_new (G_IR_NODE_VALUE);
1274 ((GIrNode *)value_)->name = g_strdup (name);
1276 value_->value = parse_value (value);
1279 value_->deprecated = TRUE;
1281 value_->deprecated = FALSE;
1283 enum_ = (GIrNodeEnum *)ctx->current_node;
1284 enum_->values = g_list_append (enum_->values, value_);
1293 start_constant (GMarkupParseContext *context,
1294 const gchar *element_name,
1295 const gchar **attribute_names,
1296 const gchar **attribute_values,
1300 if (strcmp (element_name, "constant") == 0 &&
1301 (ctx->state == STATE_NAMESPACE ||
1302 ctx->state == STATE_CLASS ||
1303 ctx->state == STATE_INTERFACE))
1307 const gchar *deprecated;
1309 name = find_attribute ("name", attribute_names, attribute_values);
1310 value = find_attribute ("value", attribute_names, attribute_values);
1311 deprecated = find_attribute ("deprecated", attribute_names, attribute_values);
1314 MISSING_ATTRIBUTE (context, error, element_name, "name");
1315 else if (value == NULL)
1316 MISSING_ATTRIBUTE (context, error, element_name, "value");
1319 GIrNodeConstant *constant;
1321 constant = (GIrNodeConstant *) g_ir_node_new (G_IR_NODE_CONSTANT);
1323 ((GIrNode *)constant)->name = g_strdup (name);
1324 constant->value = g_strdup (value);
1326 ctx->current_typed = (GIrNode*) constant;
1329 constant->deprecated = TRUE;
1331 constant->deprecated = FALSE;
1333 if (ctx->state == STATE_NAMESPACE)
1335 ctx->current_node = (GIrNode *) constant;
1336 ctx->current_module->entries =
1337 g_list_append (ctx->current_module->entries, constant);
1341 GIrNodeInterface *iface;
1343 iface = (GIrNodeInterface *)ctx->current_node;
1344 iface->members = g_list_append (iface->members, constant);
1349 case STATE_NAMESPACE:
1350 state_switch (ctx, STATE_NAMESPACE_CONSTANT);
1353 state_switch (ctx, STATE_CLASS_CONSTANT);
1355 case STATE_INTERFACE:
1356 state_switch (ctx, STATE_INTERFACE_CONSTANT);
1359 g_assert_not_reached ();
1370 start_errordomain (GMarkupParseContext *context,
1371 const gchar *element_name,
1372 const gchar **attribute_names,
1373 const gchar **attribute_values,
1377 if (strcmp (element_name, "errordomain") == 0 &&
1378 ctx->state == STATE_NAMESPACE)
1381 const gchar *getquark;
1383 const gchar *deprecated;
1385 name = find_attribute ("name", attribute_names, attribute_values);
1386 getquark = find_attribute ("get-quark", attribute_names, attribute_values);
1387 codes = find_attribute ("codes", attribute_names, attribute_values);
1388 deprecated = find_attribute ("deprecated", attribute_names, attribute_values);
1391 MISSING_ATTRIBUTE (context, error, element_name, "name");
1392 else if (getquark == NULL)
1393 MISSING_ATTRIBUTE (context, error, element_name, "getquark");
1394 else if (codes == NULL)
1395 MISSING_ATTRIBUTE (context, error, element_name, "codes");
1398 GIrNodeErrorDomain *domain;
1400 domain = (GIrNodeErrorDomain *) g_ir_node_new (G_IR_NODE_ERROR_DOMAIN);
1402 ((GIrNode *)domain)->name = g_strdup (name);
1403 domain->getquark = g_strdup (getquark);
1404 domain->codes = g_strdup (codes);
1407 domain->deprecated = TRUE;
1409 domain->deprecated = FALSE;
1411 ctx->current_node = (GIrNode *) domain;
1412 ctx->current_module->entries =
1413 g_list_append (ctx->current_module->entries, domain);
1415 state_switch (ctx, STATE_ERRORDOMAIN);
1424 start_interface (GMarkupParseContext *context,
1425 const gchar *element_name,
1426 const gchar **attribute_names,
1427 const gchar **attribute_values,
1431 if (strcmp (element_name, "interface") == 0 &&
1432 ctx->state == STATE_NAMESPACE)
1435 const gchar *typename;
1436 const gchar *typeinit;
1437 const gchar *deprecated;
1439 name = find_attribute ("name", attribute_names, attribute_values);
1440 typename = find_attribute ("glib:type-name", attribute_names, attribute_values);
1441 typeinit = find_attribute ("glib:get-type", attribute_names, attribute_values);
1442 deprecated = find_attribute ("deprecated", attribute_names, attribute_values);
1445 MISSING_ATTRIBUTE (context, error, element_name, "name");
1446 else if (typename == NULL)
1447 MISSING_ATTRIBUTE (context, error, element_name, "glib:type-name");
1448 else if (typeinit == NULL)
1449 MISSING_ATTRIBUTE (context, error, element_name, "glib:get-type");
1452 GIrNodeInterface *iface;
1454 iface = (GIrNodeInterface *) g_ir_node_new (G_IR_NODE_INTERFACE);
1455 ((GIrNode *)iface)->name = g_strdup (name);
1456 iface->gtype_name = g_strdup (typename);
1457 iface->gtype_init = g_strdup (typeinit);
1459 iface->deprecated = TRUE;
1461 iface->deprecated = FALSE;
1463 ctx->current_node = (GIrNode *) iface;
1464 ctx->current_module->entries =
1465 g_list_append (ctx->current_module->entries, iface);
1467 state_switch (ctx, STATE_INTERFACE);
1477 start_class (GMarkupParseContext *context,
1478 const gchar *element_name,
1479 const gchar **attribute_names,
1480 const gchar **attribute_values,
1484 if (strcmp (element_name, "class") == 0 &&
1485 ctx->state == STATE_NAMESPACE)
1488 const gchar *parent;
1489 const gchar *typename;
1490 const gchar *typeinit;
1491 const gchar *deprecated;
1492 const gchar *abstract;
1494 name = find_attribute ("name", attribute_names, attribute_values);
1495 parent = find_attribute ("parent", attribute_names, attribute_values);
1496 typename = find_attribute ("glib:type-name", attribute_names, attribute_values);
1497 typeinit = find_attribute ("glib:get-type", attribute_names, attribute_values);
1498 deprecated = find_attribute ("deprecated", attribute_names, attribute_values);
1499 abstract = find_attribute ("abstract", attribute_names, attribute_values);
1502 MISSING_ATTRIBUTE (context, error, element_name, "name");
1503 else if (typename == NULL)
1504 MISSING_ATTRIBUTE (context, error, element_name, "glib:type-name");
1505 else if (typeinit == NULL && strcmp (typename, "GObject"))
1506 MISSING_ATTRIBUTE (context, error, element_name, "glib:get-type");
1509 GIrNodeInterface *iface;
1511 iface = (GIrNodeInterface *) g_ir_node_new (G_IR_NODE_OBJECT);
1512 ((GIrNode *)iface)->name = g_strdup (name);
1513 iface->gtype_name = g_strdup (typename);
1514 iface->gtype_init = g_strdup (typeinit);
1515 iface->parent = g_strdup (parent);
1517 iface->deprecated = TRUE;
1519 iface->deprecated = FALSE;
1521 iface->abstract = abstract && strcmp (abstract, "1") == 0;
1523 ctx->current_node = (GIrNode *) iface;
1524 ctx->current_module->entries =
1525 g_list_append (ctx->current_module->entries, iface);
1527 state_switch (ctx, STATE_CLASS);
1536 start_type (GMarkupParseContext *context,
1537 const gchar *element_name,
1538 const gchar **attribute_names,
1539 const gchar **attribute_values,
1546 gboolean is_varargs;
1547 GIrNodeType *typenode;
1549 is_array = strcmp (element_name, "array") == 0;
1550 is_varargs = strcmp (element_name, "varargs") == 0;
1552 if (!(is_array || is_varargs || (strcmp (element_name, "type") == 0)))
1555 if (ctx->state == STATE_TYPE)
1558 ctx->type_stack = g_list_prepend (ctx->type_stack, ctx->type_parameters);
1559 ctx->type_parameters = NULL;
1561 else if (ctx->state == STATE_FUNCTION_PARAMETER ||
1562 ctx->state == STATE_FUNCTION_RETURN ||
1563 ctx->state == STATE_STRUCT_FIELD ||
1564 ctx->state == STATE_UNION_FIELD ||
1565 ctx->state == STATE_CLASS_PROPERTY ||
1566 ctx->state == STATE_CLASS_FIELD ||
1567 ctx->state == STATE_INTERFACE_FIELD ||
1568 ctx->state == STATE_INTERFACE_PROPERTY ||
1569 ctx->state == STATE_BOXED_FIELD ||
1570 ctx->state == STATE_NAMESPACE_CONSTANT ||
1571 ctx->state == STATE_CLASS_CONSTANT ||
1572 ctx->state == STATE_INTERFACE_CONSTANT
1575 state_switch (ctx, STATE_TYPE);
1576 ctx->type_depth = 1;
1579 switch (ctx->current_node->type)
1581 case G_IR_NODE_FUNCTION:
1582 case G_IR_NODE_CALLBACK:
1584 GIrNodeFunction *func = (GIrNodeFunction *)ctx->current_node;
1585 func->is_varargs = TRUE;
1588 case G_IR_NODE_VFUNC:
1590 GIrNodeVFunc *vfunc = (GIrNodeVFunc *)ctx->current_node;
1591 vfunc->is_varargs = TRUE;
1594 /* list others individually rather than with default: so that compiler
1595 * warns if new node types are added without adding them to the switch
1597 case G_IR_NODE_INVALID:
1598 case G_IR_NODE_ENUM:
1599 case G_IR_NODE_FLAGS:
1600 case G_IR_NODE_CONSTANT:
1601 case G_IR_NODE_ERROR_DOMAIN:
1602 case G_IR_NODE_PARAM:
1603 case G_IR_NODE_TYPE:
1604 case G_IR_NODE_PROPERTY:
1605 case G_IR_NODE_SIGNAL:
1606 case G_IR_NODE_VALUE:
1607 case G_IR_NODE_FIELD:
1608 case G_IR_NODE_XREF:
1609 case G_IR_NODE_STRUCT:
1610 case G_IR_NODE_BOXED:
1611 case G_IR_NODE_OBJECT:
1612 case G_IR_NODE_INTERFACE:
1613 case G_IR_NODE_UNION:
1614 g_assert_not_reached ();
1618 ctx->type_stack = NULL;
1619 ctx->type_parameters = NULL;
1622 if (!ctx->current_typed)
1626 G_MARKUP_ERROR_INVALID_CONTENT,
1627 "The element <type> is invalid here");
1640 typenode = (GIrNodeType *)g_ir_node_new (G_IR_NODE_TYPE);
1642 typenode->tag = GI_TYPE_TAG_ARRAY;
1643 typenode->is_pointer = TRUE;
1644 typenode->is_array = TRUE;
1646 zero = find_attribute ("zero-terminated", attribute_names, attribute_values);
1647 len = find_attribute ("length", attribute_names, attribute_values);
1648 size = find_attribute ("fixed-size", attribute_names, attribute_values);
1650 typenode->zero_terminated = !(zero && strcmp (zero, "1") != 0);
1651 typenode->has_length = len != NULL;
1652 typenode->length = typenode->has_length ? atoi (len) : -1;
1654 typenode->has_size = size != NULL;
1655 typenode->size = typenode->has_size ? atoi (size) : -1;
1660 name = find_attribute ("name", attribute_names, attribute_values);
1663 MISSING_ATTRIBUTE (context, error, element_name, "name");
1666 ctype = find_attribute ("c:type", attribute_names, attribute_values);
1669 const char *cp = ctype + strlen(ctype) - 1;
1670 while (cp > ctype && *cp-- == '*')
1674 if (ctx->current_typed->type == G_IR_NODE_PARAM &&
1675 ((GIrNodeParam *)ctx->current_typed)->out &&
1679 typenode = parse_type (ctx, name);
1681 /* A 'disguised' structure is one where the c:type is a typedef that
1682 * doesn't look like a pointer, but is internally.
1684 if (typenode->tag == GI_TYPE_TAG_INTERFACE &&
1685 is_disguised_structure (ctx, typenode->interface))
1688 if (pointer_depth > 0)
1689 typenode->is_pointer = TRUE;
1692 ctx->type_parameters = g_list_append (ctx->type_parameters, typenode);
1698 end_type_top (ParseContext *ctx)
1700 GIrNodeType *typenode;
1702 if (!ctx->type_parameters)
1705 typenode = (GIrNodeType*)ctx->type_parameters->data;
1707 /* Default to pointer for unspecified containers */
1708 if (typenode->tag == GI_TYPE_TAG_ARRAY ||
1709 typenode->tag == GI_TYPE_TAG_GLIST ||
1710 typenode->tag == GI_TYPE_TAG_GSLIST)
1712 if (typenode->parameter_type1 == NULL)
1713 typenode->parameter_type1 = parse_type (ctx, "any");
1715 else if (typenode->tag == GI_TYPE_TAG_GHASH)
1717 if (typenode->parameter_type1 == NULL)
1719 typenode->parameter_type1 = parse_type (ctx, "any");
1720 typenode->parameter_type2 = parse_type (ctx, "any");
1724 switch (ctx->current_typed->type)
1726 case G_IR_NODE_PARAM:
1728 GIrNodeParam *param = (GIrNodeParam *)ctx->current_typed;
1729 param->type = typenode;
1732 case G_IR_NODE_FIELD:
1734 GIrNodeField *field = (GIrNodeField *)ctx->current_typed;
1735 field->type = typenode;
1738 case G_IR_NODE_PROPERTY:
1740 GIrNodeProperty *property = (GIrNodeProperty *) ctx->current_typed;
1741 property->type = typenode;
1744 case G_IR_NODE_CONSTANT:
1746 GIrNodeConstant *constant = (GIrNodeConstant *)ctx->current_typed;
1747 constant->type = typenode;
1751 g_printerr("current node is %d\n", ctx->current_node->type);
1752 g_assert_not_reached ();
1754 g_list_free (ctx->type_parameters);
1757 ctx->type_depth = 0;
1758 ctx->type_parameters = NULL;
1759 ctx->current_typed = NULL;
1763 end_type_recurse (ParseContext *ctx)
1765 GIrNodeType *parent;
1766 GIrNodeType *param = NULL;
1768 parent = (GIrNodeType *) ((GList*)ctx->type_stack->data)->data;
1769 if (ctx->type_parameters)
1770 param = (GIrNodeType *) ctx->type_parameters->data;
1772 if (parent->tag == GI_TYPE_TAG_ARRAY ||
1773 parent->tag == GI_TYPE_TAG_GLIST ||
1774 parent->tag == GI_TYPE_TAG_GSLIST)
1776 g_assert (param != NULL);
1778 if (parent->parameter_type1 == NULL)
1779 parent->parameter_type1 = param;
1781 g_assert_not_reached ();
1783 else if (parent->tag == GI_TYPE_TAG_GHASH)
1785 g_assert (param != NULL);
1787 if (parent->parameter_type1 == NULL)
1788 parent->parameter_type1 = param;
1789 else if (parent->parameter_type2 == NULL)
1790 parent->parameter_type2 = param;
1792 g_assert_not_reached ();
1794 g_list_free (ctx->type_parameters);
1795 ctx->type_parameters = (GList *)ctx->type_stack->data;
1796 ctx->type_stack = g_list_delete_link (ctx->type_stack, ctx->type_stack);
1800 end_type (ParseContext *ctx)
1802 if (ctx->type_depth == 1)
1805 state_switch (ctx, ctx->prev_state);
1809 end_type_recurse (ctx);
1815 start_return_value (GMarkupParseContext *context,
1816 const gchar *element_name,
1817 const gchar **attribute_names,
1818 const gchar **attribute_values,
1822 if (strcmp (element_name, "return-value") == 0 &&
1823 ctx->state == STATE_FUNCTION)
1825 GIrNodeParam *param;
1826 const gchar *transfer;
1828 param = (GIrNodeParam *)g_ir_node_new (G_IR_NODE_PARAM);
1831 param->retval = TRUE;
1833 ctx->current_typed = (GIrNode*) param;
1835 state_switch (ctx, STATE_FUNCTION_RETURN);
1837 transfer = find_attribute ("transfer-ownership", attribute_names, attribute_values);
1838 parse_param_transfer (param, transfer);
1840 switch (ctx->current_node->type)
1842 case G_IR_NODE_FUNCTION:
1843 case G_IR_NODE_CALLBACK:
1845 GIrNodeFunction *func = (GIrNodeFunction *)ctx->current_node;
1846 func->result = param;
1849 case G_IR_NODE_SIGNAL:
1851 GIrNodeSignal *signal = (GIrNodeSignal *)ctx->current_node;
1852 signal->result = param;
1855 case G_IR_NODE_VFUNC:
1857 GIrNodeVFunc *vfunc = (GIrNodeVFunc *)ctx->current_node;
1858 vfunc->result = param;
1862 g_assert_not_reached ();
1872 start_implements (GMarkupParseContext *context,
1873 const gchar *element_name,
1874 const gchar **attribute_names,
1875 const gchar **attribute_values,
1879 GIrNodeInterface *iface;
1882 if (strcmp (element_name, "implements") != 0 ||
1883 !(ctx->state == STATE_CLASS))
1886 state_switch (ctx, STATE_IMPLEMENTS);
1888 name = find_attribute ("name", attribute_names, attribute_values);
1891 MISSING_ATTRIBUTE (context, error, element_name, "name");
1895 iface = (GIrNodeInterface *)ctx->current_node;
1896 iface->interfaces = g_list_append (iface->interfaces, g_strdup (name));
1902 start_glib_signal (GMarkupParseContext *context,
1903 const gchar *element_name,
1904 const gchar **attribute_names,
1905 const gchar **attribute_values,
1909 if (strcmp (element_name, "glib:signal") == 0 &&
1910 (ctx->state == STATE_CLASS ||
1911 ctx->state == STATE_INTERFACE))
1915 const gchar *no_recurse;
1916 const gchar *detailed;
1917 const gchar *action;
1918 const gchar *no_hooks;
1919 const gchar *has_class_closure;
1921 name = find_attribute ("name", attribute_names, attribute_values);
1922 when = find_attribute ("when", attribute_names, attribute_values);
1923 no_recurse = find_attribute ("no-recurse", attribute_names, attribute_values);
1924 detailed = find_attribute ("detailed", attribute_names, attribute_values);
1925 action = find_attribute ("action", attribute_names, attribute_values);
1926 no_hooks = find_attribute ("no-hooks", attribute_names, attribute_values);
1927 has_class_closure = find_attribute ("has-class-closure", attribute_names, attribute_values);
1930 MISSING_ATTRIBUTE (context, error, element_name, "name");
1933 GIrNodeInterface *iface;
1934 GIrNodeSignal *signal;
1936 signal = (GIrNodeSignal *)g_ir_node_new (G_IR_NODE_SIGNAL);
1938 ((GIrNode *)signal)->name = g_strdup (name);
1940 signal->run_first = FALSE;
1941 signal->run_last = FALSE;
1942 signal->run_cleanup = FALSE;
1943 if (when == NULL || strcmp (when, "LAST") == 0)
1944 signal->run_last = TRUE;
1945 else if (strcmp (when, "FIRST") == 0)
1946 signal->run_first = TRUE;
1948 signal->run_cleanup = TRUE;
1950 if (no_recurse && strcmp (no_recurse, "1") == 0)
1951 signal->no_recurse = TRUE;
1953 signal->no_recurse = FALSE;
1954 if (detailed && strcmp (detailed, "1") == 0)
1955 signal->detailed = TRUE;
1957 signal->detailed = FALSE;
1958 if (action && strcmp (action, "1") == 0)
1959 signal->action = TRUE;
1961 signal->action = FALSE;
1962 if (no_hooks && strcmp (no_hooks, "1") == 0)
1963 signal->no_hooks = TRUE;
1965 signal->no_hooks = FALSE;
1966 if (has_class_closure && strcmp (has_class_closure, "1") == 0)
1967 signal->has_class_closure = TRUE;
1969 signal->has_class_closure = FALSE;
1971 iface = (GIrNodeInterface *)ctx->current_node;
1972 iface->members = g_list_append (iface->members, signal);
1974 ctx->current_node = (GIrNode *)signal;
1975 state_switch (ctx, STATE_FUNCTION);
1984 start_vfunc (GMarkupParseContext *context,
1985 const gchar *element_name,
1986 const gchar **attribute_names,
1987 const gchar **attribute_values,
1991 if (strcmp (element_name, "vfunc") == 0 &&
1992 (ctx->state == STATE_CLASS ||
1993 ctx->state == STATE_INTERFACE))
1996 const gchar *must_chain_up;
1997 const gchar *override;
1998 const gchar *is_class_closure;
1999 const gchar *offset;
2001 name = find_attribute ("name", attribute_names, attribute_values);
2002 must_chain_up = find_attribute ("must-chain-up", attribute_names, attribute_values);
2003 override = find_attribute ("override", attribute_names, attribute_values);
2004 is_class_closure = find_attribute ("is-class-closure", attribute_names, attribute_values);
2005 offset = find_attribute ("offset", attribute_names, attribute_values);
2008 MISSING_ATTRIBUTE (context, error, element_name, "name");
2011 GIrNodeInterface *iface;
2012 GIrNodeVFunc *vfunc;
2014 vfunc = (GIrNodeVFunc *)g_ir_node_new (G_IR_NODE_VFUNC);
2016 ((GIrNode *)vfunc)->name = g_strdup (name);
2018 if (must_chain_up && strcmp (must_chain_up, "1") == 0)
2019 vfunc->must_chain_up = TRUE;
2021 vfunc->must_chain_up = FALSE;
2023 if (override && strcmp (override, "always") == 0)
2025 vfunc->must_be_implemented = TRUE;
2026 vfunc->must_not_be_implemented = FALSE;
2028 else if (override && strcmp (override, "never") == 0)
2030 vfunc->must_be_implemented = FALSE;
2031 vfunc->must_not_be_implemented = TRUE;
2035 vfunc->must_be_implemented = FALSE;
2036 vfunc->must_not_be_implemented = FALSE;
2039 if (is_class_closure && strcmp (is_class_closure, "1") == 0)
2040 vfunc->is_class_closure = TRUE;
2042 vfunc->is_class_closure = FALSE;
2045 vfunc->offset = atoi (offset);
2049 iface = (GIrNodeInterface *)ctx->current_node;
2050 iface->members = g_list_append (iface->members, vfunc);
2052 ctx->current_node = (GIrNode *)vfunc;
2053 state_switch (ctx, STATE_FUNCTION);
2063 start_struct (GMarkupParseContext *context,
2064 const gchar *element_name,
2065 const gchar **attribute_names,
2066 const gchar **attribute_values,
2070 if (strcmp (element_name, "record") == 0 &&
2071 ctx->state == STATE_NAMESPACE)
2074 const gchar *deprecated;
2075 const gchar *disguised;
2076 const gchar *gtype_name;
2077 const gchar *gtype_init;
2078 GIrNodeStruct *struct_;
2080 name = find_attribute ("name", attribute_names, attribute_values);
2081 deprecated = find_attribute ("deprecated", attribute_names, attribute_values);
2082 disguised = find_attribute ("disguised", attribute_names, attribute_values);
2083 gtype_name = find_attribute ("glib:type-name", attribute_names, attribute_values);
2084 gtype_init = find_attribute ("glib:get-type", attribute_names, attribute_values);
2088 MISSING_ATTRIBUTE (context, error, element_name, "name");
2091 if ((gtype_name == NULL && gtype_init != NULL))
2093 MISSING_ATTRIBUTE (context, error, element_name, "glib:type-name");
2096 if ((gtype_name != NULL && gtype_init == NULL))
2098 MISSING_ATTRIBUTE (context, error, element_name, "glib:get-type");
2102 struct_ = (GIrNodeStruct *) g_ir_node_new (G_IR_NODE_STRUCT);
2104 ((GIrNode *)struct_)->name = g_strdup (name);
2106 struct_->deprecated = TRUE;
2108 struct_->deprecated = FALSE;
2110 if (disguised && strcmp (disguised, "1") == 0)
2111 struct_->disguised = TRUE;
2113 struct_->gtype_name = g_strdup (gtype_name);
2114 struct_->gtype_init = g_strdup (gtype_init);
2116 ctx->current_node = (GIrNode *)struct_;
2117 ctx->current_module->entries =
2118 g_list_append (ctx->current_module->entries, struct_);
2120 state_switch (ctx, STATE_STRUCT);
2128 start_union (GMarkupParseContext *context,
2129 const gchar *element_name,
2130 const gchar **attribute_names,
2131 const gchar **attribute_values,
2135 if (strcmp (element_name, "union") == 0 &&
2136 ctx->state == STATE_NAMESPACE)
2139 const gchar *deprecated;
2140 const gchar *typename;
2141 const gchar *typeinit;
2143 name = find_attribute ("name", attribute_names, attribute_values);
2144 deprecated = find_attribute ("deprecated", attribute_names, attribute_values);
2145 typename = find_attribute ("glib:type-name", attribute_names, attribute_values);
2146 typeinit = find_attribute ("glib:get-type", attribute_names, attribute_values);
2149 MISSING_ATTRIBUTE (context, error, element_name, "name");
2152 GIrNodeUnion *union_;
2154 union_ = (GIrNodeUnion *) g_ir_node_new (G_IR_NODE_UNION);
2156 ((GIrNode *)union_)->name = g_strdup (name);
2157 union_->gtype_name = g_strdup (typename);
2158 union_->gtype_init = g_strdup (typeinit);
2160 union_->deprecated = TRUE;
2162 union_->deprecated = FALSE;
2164 ctx->current_node = (GIrNode *)union_;
2165 ctx->current_module->entries =
2166 g_list_append (ctx->current_module->entries, union_);
2168 state_switch (ctx, STATE_UNION);
2176 start_discriminator (GMarkupParseContext *context,
2177 const gchar *element_name,
2178 const gchar **attribute_names,
2179 const gchar **attribute_values,
2183 if (strcmp (element_name, "discriminator") == 0 &&
2184 ctx->state == STATE_UNION)
2187 const gchar *offset;
2189 type = find_attribute ("type", attribute_names, attribute_values);
2190 offset = find_attribute ("offset", attribute_names, attribute_values);
2192 MISSING_ATTRIBUTE (context, error, element_name, "type");
2193 else if (offset == NULL)
2194 MISSING_ATTRIBUTE (context, error, element_name, "offset");
2196 ((GIrNodeUnion *)ctx->current_node)->discriminator_type
2197 = parse_type (ctx, type);
2198 ((GIrNodeUnion *)ctx->current_node)->discriminator_offset
2209 parse_include (GMarkupParseContext *context,
2212 const char *version,
2218 gboolean success = FALSE;
2222 for (l = ctx->parser->parsed_modules; l; l = l->next)
2224 GIrModule *m = l->data;
2226 if (strcmp (m->name, name) == 0)
2228 if (strcmp (m->version, version) == 0)
2230 ctx->include_modules = g_list_prepend (ctx->include_modules, m);
2238 G_MARKUP_ERROR_INVALID_CONTENT,
2239 "Module '%s' imported with conflicting versions '%s' and '%s'",
2240 name, m->version, version);
2246 girpath = locate_gir (ctx->parser, name, version);
2248 if (girpath == NULL)
2252 G_MARKUP_ERROR_INVALID_CONTENT,
2253 "Could not find GIR file '%s.gir'; check XDG_DATA_DIRS or use --includedir",
2258 g_debug ("Parsing include %s", girpath);
2260 if (!g_file_get_contents (girpath, &buffer, &length, error))
2267 modules = g_ir_parser_parse_string (ctx->parser, name, buffer, length, error);
2268 success = error != NULL;
2270 ctx->include_modules = g_list_concat (ctx->include_modules,
2278 extern GLogLevelFlags logged_levels;
2281 start_element_handler (GMarkupParseContext *context,
2282 const gchar *element_name,
2283 const gchar **attribute_names,
2284 const gchar **attribute_values,
2288 ParseContext *ctx = user_data;
2289 gint line_number, char_number;
2291 if (logged_levels & G_LOG_LEVEL_DEBUG)
2293 GString *tags = g_string_new ("");
2295 for (i = 0; attribute_names[i]; i++)
2296 g_string_append_printf (tags, "%s=\"%s\" ",
2298 attribute_values[i]);
2302 g_string_insert_c (tags, 0, ' ');
2303 g_string_truncate (tags, tags->len - 1);
2305 g_debug ("<%s%s>", element_name, tags->str);
2306 g_string_free (tags, TRUE);
2309 switch (element_name[0])
2312 if (ctx->state == STATE_NAMESPACE && strcmp (element_name, "alias") == 0)
2314 state_switch (ctx, STATE_ALIAS);
2317 if (start_type (context, element_name,
2318 attribute_names, attribute_values,
2323 if (start_enum (context, element_name,
2324 attribute_names, attribute_values,
2329 if (start_function (context, element_name,
2330 attribute_names, attribute_values,
2333 else if (start_constant (context, element_name,
2334 attribute_names, attribute_values,
2337 else if (start_class (context, element_name,
2338 attribute_names, attribute_values,
2344 if (start_discriminator (context, element_name,
2345 attribute_names, attribute_values,
2351 if (start_enum (context, element_name,
2352 attribute_names, attribute_values,
2355 else if (start_errordomain (context, element_name,
2356 attribute_names, attribute_values,
2362 if (start_function (context, element_name,
2363 attribute_names, attribute_values,
2366 else if (start_field (context, element_name,
2367 attribute_names, attribute_values,
2373 if (start_glib_boxed (context, element_name,
2374 attribute_names, attribute_values,
2377 else if (start_glib_signal (context, element_name,
2378 attribute_names, attribute_values,
2384 if (strcmp (element_name, "include") == 0 &&
2385 ctx->state == STATE_REPOSITORY)
2388 const gchar *version;
2390 name = find_attribute ("name", attribute_names, attribute_values);
2391 version = find_attribute ("version", attribute_names, attribute_values);
2395 MISSING_ATTRIBUTE (context, error, element_name, "name");
2398 if (version == NULL)
2400 MISSING_ATTRIBUTE (context, error, element_name, "version");
2404 if (!parse_include (context, ctx, name, version, error))
2407 ctx->dependencies = g_list_prepend (ctx->dependencies,
2408 g_strdup_printf ("%s-%s", name, version));
2411 state_switch (ctx, STATE_INCLUDE);
2414 if (start_interface (context, element_name,
2415 attribute_names, attribute_values,
2418 else if (start_implements (context, element_name,
2419 attribute_names, attribute_values,
2425 if (start_function (context, element_name,
2426 attribute_names, attribute_values,
2429 else if (start_member (context, element_name,
2430 attribute_names, attribute_values,
2436 if (strcmp (element_name, "namespace") == 0 && ctx->state == STATE_REPOSITORY)
2438 const gchar *name, *version, *shared_library;
2440 if (ctx->current_module != NULL)
2444 G_MARKUP_ERROR_INVALID_CONTENT,
2445 "Only one <namespace/> element is currently allowed per <repository/>");
2449 name = find_attribute ("name", attribute_names, attribute_values);
2450 version = find_attribute ("version", attribute_names, attribute_values);
2451 shared_library = find_attribute ("shared-library", attribute_names, attribute_values);
2454 MISSING_ATTRIBUTE (context, error, element_name, "name");
2455 else if (version == NULL)
2456 MISSING_ATTRIBUTE (context, error, element_name, "version");
2461 if (strcmp (name, ctx->namespace) != 0)
2464 G_MARKUP_ERROR_INVALID_CONTENT,
2465 "<namespace/> name element '%s' doesn't match file name '%s'",
2466 name, ctx->namespace);
2468 ctx->current_module = g_ir_module_new (name, version, shared_library);
2470 ctx->current_module->aliases = ctx->aliases;
2471 ctx->aliases = NULL;
2472 ctx->current_module->disguised_structures = ctx->disguised_structures;
2473 ctx->disguised_structures = NULL;
2475 for (l = ctx->include_modules; l; l = l->next)
2476 g_ir_module_add_include_module (ctx->current_module, l->data);
2478 g_list_free (ctx->include_modules);
2479 ctx->include_modules = NULL;
2481 ctx->modules = g_list_append (ctx->modules, ctx->current_module);
2482 ctx->current_module->dependencies = ctx->dependencies;
2484 state_switch (ctx, STATE_NAMESPACE);
2491 if (start_property (context, element_name,
2492 attribute_names, attribute_values,
2495 else if (strcmp (element_name, "parameters") == 0 &&
2496 ctx->state == STATE_FUNCTION)
2498 state_switch (ctx, STATE_FUNCTION_PARAMETERS);
2502 else if (start_parameter (context, element_name,
2503 attribute_names, attribute_values,
2506 else if (strcmp (element_name, "prerequisite") == 0 &&
2507 ctx->state == STATE_INTERFACE)
2511 name = find_attribute ("name", attribute_names, attribute_values);
2513 state_switch (ctx, STATE_PREREQUISITE);
2516 MISSING_ATTRIBUTE (context, error, element_name, "name");
2519 GIrNodeInterface *iface;
2521 iface = (GIrNodeInterface *)ctx->current_node;
2522 iface ->prerequisites = g_list_append (iface->prerequisites, g_strdup (name));
2529 if (strcmp (element_name, "repository") == 0 && ctx->state == STATE_START)
2531 const gchar *version;
2533 version = find_attribute ("version", attribute_names, attribute_values);
2535 if (version == NULL)
2536 MISSING_ATTRIBUTE (context, error, element_name, "version");
2537 else if (strcmp (version, "1.0") != 0)
2540 G_MARKUP_ERROR_INVALID_CONTENT,
2541 "Unsupported version '%s'",
2544 state_switch (ctx, STATE_REPOSITORY);
2548 else if (start_return_value (context, element_name,
2549 attribute_names, attribute_values,
2552 else if (start_struct (context, element_name,
2553 attribute_names, attribute_values,
2559 if (start_union (context, element_name,
2560 attribute_names, attribute_values,
2566 if (start_type (context, element_name,
2567 attribute_names, attribute_values,
2573 if (start_vfunc (context, element_name,
2574 attribute_names, attribute_values,
2577 if (start_type (context, element_name,
2578 attribute_names, attribute_values,
2584 g_markup_parse_context_get_position (context, &line_number, &char_number);
2586 if (error && *error == NULL)
2589 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
2590 "Unexpected start tag '%s' on line %d char %d; current state=%d",
2592 line_number, char_number, ctx->state);
2597 g_markup_parse_context_get_position (context, &line_number, &char_number);
2599 fprintf (stderr, "Error at line %d, character %d: %s\n", line_number, char_number, (*error)->message);
2600 backtrace_stderr ();
2605 require_one_of_end_elements (GMarkupParseContext *context,
2607 const char *actual_name,
2612 int line_number, char_number;
2613 const char *expected;
2614 gboolean matched = FALSE;
2616 va_start (args, error);
2618 while ((expected = va_arg (args, const char*)) != NULL)
2620 if (strcmp (expected, actual_name) == 0)
2632 g_markup_parse_context_get_position (context, &line_number, &char_number);
2635 G_MARKUP_ERROR_INVALID_CONTENT,
2636 "Unexpected end tag '%s' on line %d char %d; current state=%d",
2638 line_number, char_number, ctx->state);
2644 require_end_element (GMarkupParseContext *context,
2646 const char *expected_name,
2647 const char *actual_name,
2650 return require_one_of_end_elements (context, ctx, actual_name, error, expected_name, NULL);
2654 end_element_handler (GMarkupParseContext *context,
2655 const gchar *element_name,
2659 ParseContext *ctx = user_data;
2661 g_debug ("</%s>", element_name);
2667 /* no need to GError here, GMarkup already catches this */
2670 case STATE_REPOSITORY:
2671 state_switch (ctx, STATE_END);
2675 if (require_end_element (context, ctx, "include", element_name, error))
2677 state_switch (ctx, STATE_REPOSITORY);
2681 case STATE_NAMESPACE:
2682 if (require_end_element (context, ctx, "namespace", element_name, error))
2684 ctx->current_module = NULL;
2685 state_switch (ctx, STATE_REPOSITORY);
2690 if (require_end_element (context, ctx, "alias", element_name, error))
2692 state_switch (ctx, STATE_NAMESPACE);
2696 case STATE_FUNCTION_RETURN:
2697 if (strcmp ("type", element_name) == 0)
2699 if (require_end_element (context, ctx, "return-value", element_name, error))
2701 state_switch (ctx, STATE_FUNCTION);
2705 case STATE_FUNCTION_PARAMETERS:
2706 if (require_end_element (context, ctx, "parameters", element_name, error))
2708 state_switch (ctx, STATE_FUNCTION);
2712 case STATE_FUNCTION_PARAMETER:
2713 if (strcmp ("type", element_name) == 0)
2715 if (require_end_element (context, ctx, "parameter", element_name, error))
2717 state_switch (ctx, STATE_FUNCTION_PARAMETERS);
2721 case STATE_FUNCTION:
2723 gboolean current_is_toplevel;
2724 GList *last = g_list_last (ctx->current_module->entries);
2726 current_is_toplevel = ctx->current_node == last->data;
2728 if (current_is_toplevel)
2730 ctx->current_node = NULL;
2731 state_switch (ctx, STATE_NAMESPACE);
2735 ctx->current_node = g_list_last (ctx->current_module->entries)->data;
2736 if (ctx->current_node->type == G_IR_NODE_INTERFACE)
2737 state_switch (ctx, STATE_INTERFACE);
2738 else if (ctx->current_node->type == G_IR_NODE_OBJECT)
2739 state_switch (ctx, STATE_CLASS);
2740 else if (ctx->current_node->type == G_IR_NODE_BOXED)
2741 state_switch (ctx, STATE_BOXED);
2742 else if (ctx->current_node->type == G_IR_NODE_STRUCT)
2743 state_switch (ctx, STATE_STRUCT);
2744 else if (ctx->current_node->type == G_IR_NODE_UNION)
2745 state_switch (ctx, STATE_UNION);
2748 int line_number, char_number;
2749 g_markup_parse_context_get_position (context, &line_number, &char_number);
2752 G_MARKUP_ERROR_INVALID_CONTENT,
2753 "Unexpected end tag '%s' on line %d char %d",
2755 line_number, char_number);
2761 case STATE_CLASS_FIELD:
2762 if (strcmp ("type", element_name) == 0)
2764 if (require_end_element (context, ctx, "field", element_name, error))
2766 state_switch (ctx, STATE_CLASS);
2770 case STATE_CLASS_PROPERTY:
2771 if (strcmp ("type", element_name) == 0)
2773 if (require_end_element (context, ctx, "property", element_name, error))
2775 state_switch (ctx, STATE_CLASS);
2780 if (require_end_element (context, ctx, "class", element_name, error))
2782 ctx->current_node = NULL;
2783 state_switch (ctx, STATE_NAMESPACE);
2787 case STATE_ERRORDOMAIN:
2788 if (require_end_element (context, ctx, "errordomain", element_name, error))
2790 ctx->current_node = NULL;
2791 state_switch (ctx, STATE_NAMESPACE);
2795 case STATE_INTERFACE_PROPERTY:
2796 if (strcmp ("type", element_name) == 0)
2798 if (require_end_element (context, ctx, "property", element_name, error))
2800 state_switch (ctx, STATE_INTERFACE);
2804 case STATE_INTERFACE_FIELD:
2805 if (strcmp ("type", element_name) == 0)
2807 if (require_end_element (context, ctx, "field", element_name, error))
2809 state_switch (ctx, STATE_INTERFACE);
2813 case STATE_INTERFACE:
2814 if (require_end_element (context, ctx, "interface", element_name, error))
2816 ctx->current_node = NULL;
2817 state_switch (ctx, STATE_NAMESPACE);
2822 if (strcmp ("member", element_name) == 0)
2824 else if (require_one_of_end_elements (context, ctx,
2825 element_name, error, "enumeration",
2828 ctx->current_node = NULL;
2829 state_switch (ctx, STATE_NAMESPACE);
2834 if (require_end_element (context, ctx, "glib:boxed", element_name, error))
2836 ctx->current_node = NULL;
2837 state_switch (ctx, STATE_NAMESPACE);
2841 case STATE_BOXED_FIELD:
2842 if (strcmp ("type", element_name) == 0)
2844 if (require_end_element (context, ctx, "field", element_name, error))
2846 state_switch (ctx, STATE_BOXED);
2850 case STATE_STRUCT_FIELD:
2851 if (strcmp ("type", element_name) == 0)
2853 if (require_end_element (context, ctx, "field", element_name, error))
2855 state_switch (ctx, STATE_STRUCT);
2860 if (require_end_element (context, ctx, "record", element_name, error))
2862 ctx->current_node = NULL;
2863 state_switch (ctx, STATE_NAMESPACE);
2867 case STATE_UNION_FIELD:
2868 if (strcmp ("type", element_name) == 0)
2870 if (require_end_element (context, ctx, "field", element_name, error))
2872 state_switch (ctx, STATE_UNION);
2877 if (require_end_element (context, ctx, "union", element_name, error))
2879 ctx->current_node = NULL;
2880 state_switch (ctx, STATE_NAMESPACE);
2883 case STATE_IMPLEMENTS:
2884 if (strcmp ("interface", element_name) == 0)
2886 if (require_end_element (context, ctx, "implements", element_name, error))
2887 state_switch (ctx, STATE_CLASS);
2889 case STATE_PREREQUISITE:
2890 if (require_end_element (context, ctx, "prerequisite", element_name, error))
2891 state_switch (ctx, STATE_INTERFACE);
2893 case STATE_NAMESPACE_CONSTANT:
2894 case STATE_CLASS_CONSTANT:
2895 case STATE_INTERFACE_CONSTANT:
2896 if (strcmp ("type", element_name) == 0)
2898 if (require_end_element (context, ctx, "constant", element_name, error))
2900 ctx->current_node = NULL;
2903 case STATE_NAMESPACE_CONSTANT:
2904 state_switch (ctx, STATE_NAMESPACE);
2906 case STATE_CLASS_CONSTANT:
2907 state_switch (ctx, STATE_CLASS);
2909 case STATE_INTERFACE_CONSTANT:
2910 state_switch (ctx, STATE_INTERFACE);
2913 g_assert_not_reached ();
2919 if ((strcmp ("type", element_name) == 0) || (strcmp ("array", element_name) == 0) ||
2920 (strcmp ("varargs", element_name) == 0))
2926 g_error ("Unhandled state %d in end_element_handler\n", ctx->state);
2931 text_handler (GMarkupParseContext *context,
2937 /* FIXME warn about non-whitespace text */
2941 cleanup (GMarkupParseContext *context,
2945 ParseContext *ctx = user_data;
2948 for (m = ctx->modules; m; m = m->next)
2949 g_ir_module_free (m->data);
2950 g_list_free (ctx->modules);
2951 ctx->modules = NULL;
2953 ctx->current_module = NULL;
2957 post_filter_varargs_functions (GList *list)
2965 GIrNode *node = iter->data;
2969 if (node->type == G_IR_NODE_FUNCTION)
2971 if (((GIrNodeFunction*)node)->is_varargs)
2973 list = g_list_delete_link (list, link);
2981 post_filter (GIrModule *module)
2985 module->entries = post_filter_varargs_functions (module->entries);
2986 iter = module->entries;
2989 GIrNode *node = iter->data;
2993 if (node->type == G_IR_NODE_OBJECT ||
2994 node->type == G_IR_NODE_INTERFACE)
2996 GIrNodeInterface *iface = (GIrNodeInterface*)node;
2997 iface->members = post_filter_varargs_functions (iface->members);
2999 else if (node->type == G_IR_NODE_BOXED)
3001 GIrNodeBoxed *boxed = (GIrNodeBoxed*)node;
3002 boxed->members = post_filter_varargs_functions (boxed->members);
3004 else if (node->type == G_IR_NODE_STRUCT)
3006 GIrNodeStruct *iface = (GIrNodeStruct*)node;
3007 iface->members = post_filter_varargs_functions (iface->members);
3009 else if (node->type == G_IR_NODE_UNION)
3011 GIrNodeUnion *iface = (GIrNodeUnion*)node;
3012 iface->members = post_filter_varargs_functions (iface->members);
3018 * g_ir_parser_parse_string:
3019 * @parser: a #GIrParser
3020 * @error: return location for a #GError, or %NULL
3022 * Parse a string that holds a complete GIR XML file, and return a list of a
3023 * a #GirModule for each <namespace/> element within the file.
3025 * @returns: a newly allocated list of #GIrModule. The modules themselves
3026 * are owned by the #GIrParser and will be freed along with the parser.
3029 g_ir_parser_parse_string (GIrParser *parser,
3030 const gchar *namespace,
3031 const gchar *buffer,
3035 ParseContext ctx = { 0 };
3036 GMarkupParseContext *context;
3038 ctx.parser = parser;
3039 ctx.state = STATE_START;
3040 ctx.namespace = namespace;
3041 ctx.include_modules = NULL;
3042 ctx.aliases = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
3043 ctx.disguised_structures = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
3045 ctx.dependencies = NULL;
3046 ctx.current_module = NULL;
3048 context = g_markup_parse_context_new (&firstpass_parser, 0, &ctx, NULL);
3050 if (!g_markup_parse_context_parse (context, buffer, length, error))
3053 if (!g_markup_parse_context_end_parse (context, error))
3056 g_markup_parse_context_free (context);
3058 context = g_markup_parse_context_new (&markup_parser, 0, &ctx, NULL);
3059 if (!g_markup_parse_context_parse (context, buffer, length, error))
3062 if (!g_markup_parse_context_end_parse (context, error))
3065 parser->parsed_modules = g_list_concat (g_list_copy (ctx.modules),
3066 parser->parsed_modules);
3070 if (ctx.modules == NULL)
3072 /* An error occurred before we created a module, so we haven't
3073 * transferred ownership of these hash tables to the module.
3075 if (ctx.aliases != NULL)
3076 g_hash_table_destroy (ctx.aliases);
3077 if (ctx.disguised_structures != NULL)
3078 g_hash_table_destroy (ctx.disguised_structures);
3079 g_list_free (ctx.include_modules);
3082 g_markup_parse_context_free (context);
3088 * g_ir_parser_parse_file:
3089 * @parser: a #GIrParser
3090 * @error: return location for a #GError, or %NULL
3092 * Parse GIR XML file, and return a list of a a #GirModule for each
3093 * <namespace/> element within the file.
3095 * @returns: a newly allocated list of #GIrModule. The modules themselves
3096 * are owned by the #GIrParser and will be freed along with the parser.
3099 g_ir_parser_parse_file (GIrParser *parser,
3100 const gchar *filename,
3111 if (!g_str_has_suffix (filename, ".gir"))
3115 G_MARKUP_ERROR_INVALID_CONTENT,
3116 "Expected filename to end with '.gir'");
3120 g_debug ("[parsing] filename %s", filename);
3122 slash = g_strrstr (filename, "/");
3124 namespace = g_strdup (filename);
3126 namespace = g_strdup (slash+1);
3127 namespace[strlen(namespace)-4] = '\0';
3129 /* Remove version */
3130 dash = strstr (namespace, "-");
3134 if (!g_file_get_contents (filename, &buffer, &length, error))
3137 modules = g_ir_parser_parse_string (parser, namespace, buffer, length, error);
3139 for (iter = modules; iter; iter = iter->next)
3141 post_filter ((GIrModule*)iter->data);