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);
677 found = strcmp (element_name, "function") == 0;
682 found = (found || strcmp (element_name, "constructor") == 0);
684 case STATE_INTERFACE:
686 strcmp (element_name, "method") == 0 ||
687 strcmp (element_name, "callback") == 0);
696 name = find_attribute ("name", attribute_names, attribute_values);
697 symbol = find_attribute ("c:identifier", attribute_names, attribute_values);
698 deprecated = find_attribute ("deprecated", attribute_names, attribute_values);
699 throws = find_attribute ("throws", attribute_names, attribute_values);
703 MISSING_ATTRIBUTE (context, error, element_name, "name");
706 else if (strcmp (element_name, "callback") != 0 && symbol == NULL)
708 MISSING_ATTRIBUTE (context, error, element_name, "c:identifier");
712 function = (GIrNodeFunction *) g_ir_node_new (G_IR_NODE_FUNCTION);
714 ((GIrNode *)function)->name = g_strdup (name);
715 function->symbol = g_strdup (symbol);
716 function->parameters = NULL;
718 function->deprecated = TRUE;
720 function->deprecated = FALSE;
722 if (strcmp (element_name, "method") == 0 ||
723 strcmp (element_name, "constructor") == 0)
725 function->is_method = TRUE;
727 if (strcmp (element_name, "constructor") == 0)
728 function->is_constructor = TRUE;
730 function->is_constructor = FALSE;
734 function->is_method = FALSE;
735 function->is_setter = FALSE;
736 function->is_getter = FALSE;
737 function->is_constructor = FALSE;
738 if (strcmp (element_name, "callback") == 0)
739 ((GIrNode *)function)->type = G_IR_NODE_CALLBACK;
742 if (throws && strcmp (throws, "1") == 0)
743 function->throws = TRUE;
745 function->throws = FALSE;
747 if (ctx->current_node == NULL)
749 ctx->current_module->entries =
750 g_list_append (ctx->current_module->entries, function);
753 switch (ctx->current_node->type)
755 case G_IR_NODE_INTERFACE:
756 case G_IR_NODE_OBJECT:
758 GIrNodeInterface *iface;
760 iface = (GIrNodeInterface *)ctx->current_node;
761 iface->members = g_list_append (iface->members, function);
764 case G_IR_NODE_BOXED:
768 boxed = (GIrNodeBoxed *)ctx->current_node;
769 boxed->members = g_list_append (boxed->members, function);
772 case G_IR_NODE_STRUCT:
774 GIrNodeStruct *struct_;
776 struct_ = (GIrNodeStruct *)ctx->current_node;
777 struct_->members = g_list_append (struct_->members, function); }
779 case G_IR_NODE_UNION:
781 GIrNodeUnion *union_;
783 union_ = (GIrNodeUnion *)ctx->current_node;
784 union_->members = g_list_append (union_->members, function);
788 g_assert_not_reached ();
791 ctx->current_node = (GIrNode *)function;
792 state_switch (ctx, STATE_FUNCTION);
798 parse_param_transfer (GIrNodeParam *param, const gchar *transfer)
800 if (transfer == NULL)
802 g_warning ("required attribute 'transfer-ownership' missing");
804 else if (strcmp (transfer, "none") == 0)
806 param->transfer = FALSE;
807 param->shallow_transfer = FALSE;
809 else if (strcmp (transfer, "container") == 0)
811 param->transfer = FALSE;
812 param->shallow_transfer = TRUE;
814 else if (strcmp (transfer, "full") == 0)
816 param->transfer = TRUE;
817 param->shallow_transfer = FALSE;
821 g_warning ("Unknown transfer-ownership value: %s", transfer);
826 start_parameter (GMarkupParseContext *context,
827 const gchar *element_name,
828 const gchar **attribute_names,
829 const gchar **attribute_values,
834 const gchar *direction;
837 const gchar *optional;
838 const gchar *allow_none;
839 const gchar *transfer;
842 if (!(strcmp (element_name, "parameter") == 0 &&
843 ctx->state == STATE_FUNCTION_PARAMETERS))
846 name = find_attribute ("name", attribute_names, attribute_values);
847 direction = find_attribute ("direction", attribute_names, attribute_values);
848 retval = find_attribute ("retval", attribute_names, attribute_values);
849 dipper = find_attribute ("dipper", attribute_names, attribute_values);
850 optional = find_attribute ("optional", attribute_names, attribute_values);
851 allow_none = find_attribute ("allow-none", attribute_names, attribute_values);
852 transfer = find_attribute ("transfer-ownership", attribute_names, attribute_values);
857 param = (GIrNodeParam *)g_ir_node_new (G_IR_NODE_PARAM);
859 ctx->current_typed = (GIrNode*) param;
860 ctx->current_typed->name = g_strdup (name);
862 state_switch (ctx, STATE_FUNCTION_PARAMETER);
864 if (direction && strcmp (direction, "out") == 0)
869 else if (direction && strcmp (direction, "inout") == 0)
880 if (retval && strcmp (retval, "1") == 0)
881 param->retval = TRUE;
883 param->retval = FALSE;
885 if (dipper && strcmp (dipper, "1") == 0)
886 param->dipper = TRUE;
888 param->dipper = FALSE;
890 if (optional && strcmp (optional, "1") == 0)
891 param->optional = TRUE;
893 param->optional = FALSE;
895 if (allow_none && strcmp (allow_none, "1") == 0)
896 param->allow_none = TRUE;
898 param->allow_none = FALSE;
900 parse_param_transfer (param, transfer);
902 ((GIrNode *)param)->name = g_strdup (name);
904 switch (ctx->current_node->type)
906 case G_IR_NODE_FUNCTION:
907 case G_IR_NODE_CALLBACK:
909 GIrNodeFunction *func;
911 func = (GIrNodeFunction *)ctx->current_node;
912 func->parameters = g_list_append (func->parameters, param);
915 case G_IR_NODE_SIGNAL:
917 GIrNodeSignal *signal;
919 signal = (GIrNodeSignal *)ctx->current_node;
920 signal->parameters = g_list_append (signal->parameters, param);
923 case G_IR_NODE_VFUNC:
927 vfunc = (GIrNodeVFunc *)ctx->current_node;
928 vfunc->parameters = g_list_append (vfunc->parameters, param);
932 g_assert_not_reached ();
939 start_field (GMarkupParseContext *context,
940 const gchar *element_name,
941 const gchar **attribute_names,
942 const gchar **attribute_values,
947 const gchar *readable;
948 const gchar *writable;
959 case STATE_INTERFACE:
965 if (strcmp (element_name, "field") != 0)
968 name = find_attribute ("name", attribute_names, attribute_values);
969 readable = find_attribute ("readable", attribute_names, attribute_values);
970 writable = find_attribute ("writable", attribute_names, attribute_values);
971 bits = find_attribute ("bits", attribute_names, attribute_values);
972 branch = find_attribute ("branch", attribute_names, attribute_values);
976 MISSING_ATTRIBUTE (context, error, element_name, "name");
980 field = (GIrNodeField *)g_ir_node_new (G_IR_NODE_FIELD);
981 ctx->current_typed = (GIrNode*) field;
982 ((GIrNode *)field)->name = g_strdup (name);
983 /* Fields are assumed to be read-only.
984 * (see also girwriter.py and generate.c)
986 field->readable = readable == NULL || strcmp (readable, "0") == 0;
987 field->writable = writable != NULL && strcmp (writable, "1") == 0;
990 field->bits = atoi (bits);
994 switch (ctx->current_node->type)
996 case G_IR_NODE_OBJECT:
998 GIrNodeInterface *iface;
1000 iface = (GIrNodeInterface *)ctx->current_node;
1001 iface->members = g_list_append (iface->members, field);
1002 state_switch (ctx, STATE_CLASS_FIELD);
1005 case G_IR_NODE_INTERFACE:
1007 GIrNodeInterface *iface;
1009 iface = (GIrNodeInterface *)ctx->current_node;
1010 iface->members = g_list_append (iface->members, field);
1011 state_switch (ctx, STATE_INTERFACE_FIELD);
1014 case G_IR_NODE_BOXED:
1016 GIrNodeBoxed *boxed;
1018 boxed = (GIrNodeBoxed *)ctx->current_node;
1019 boxed->members = g_list_append (boxed->members, field);
1020 state_switch (ctx, STATE_BOXED_FIELD);
1023 case G_IR_NODE_STRUCT:
1025 GIrNodeStruct *struct_;
1027 struct_ = (GIrNodeStruct *)ctx->current_node;
1028 struct_->members = g_list_append (struct_->members, field);
1029 state_switch (ctx, STATE_STRUCT_FIELD);
1032 case G_IR_NODE_UNION:
1034 GIrNodeUnion *union_;
1036 union_ = (GIrNodeUnion *)ctx->current_node;
1037 union_->members = g_list_append (union_->members, field);
1040 GIrNodeConstant *constant;
1042 constant = (GIrNodeConstant *) g_ir_node_new (G_IR_NODE_CONSTANT);
1043 ((GIrNode *)constant)->name = g_strdup (name);
1044 constant->value = g_strdup (branch);
1045 constant->type = union_->discriminator_type;
1046 constant->deprecated = FALSE;
1048 union_->discriminators = g_list_append (union_->discriminators, constant);
1050 state_switch (ctx, STATE_UNION_FIELD);
1054 g_assert_not_reached ();
1061 start_alias (GMarkupParseContext *context,
1062 const gchar *element_name,
1063 const gchar **attribute_names,
1064 const gchar **attribute_values,
1069 const gchar *target;
1073 name = find_attribute ("name", attribute_names, attribute_values);
1076 MISSING_ATTRIBUTE (context, error, element_name, "name");
1080 target = find_attribute ("target", attribute_names, attribute_values);
1083 MISSING_ATTRIBUTE (context, error, element_name, "target");
1087 value = g_strdup (target);
1088 key = g_strdup_printf ("%s.%s", ctx->namespace, name);
1089 if (!strchr (target, '.'))
1091 const BasicTypeInfo *basic = parse_basic (target);
1095 /* For non-basic types, re-qualify the interface */
1096 value = g_strdup_printf ("%s.%s", ctx->namespace, target);
1099 g_hash_table_replace (ctx->aliases, key, value);
1105 start_enum (GMarkupParseContext *context,
1106 const gchar *element_name,
1107 const gchar **attribute_names,
1108 const gchar **attribute_values,
1112 if ((strcmp (element_name, "enumeration") == 0 && ctx->state == STATE_NAMESPACE) ||
1113 (strcmp (element_name, "bitfield") == 0 && ctx->state == STATE_NAMESPACE))
1116 const gchar *typename;
1117 const gchar *typeinit;
1118 const gchar *deprecated;
1120 name = find_attribute ("name", attribute_names, attribute_values);
1121 typename = find_attribute ("glib:type-name", attribute_names, attribute_values);
1122 typeinit = find_attribute ("glib:get-type", attribute_names, attribute_values);
1123 deprecated = find_attribute ("deprecated", attribute_names, attribute_values);
1126 MISSING_ATTRIBUTE (context, error, element_name, "name");
1131 if (strcmp (element_name, "enumeration") == 0)
1132 enum_ = (GIrNodeEnum *) g_ir_node_new (G_IR_NODE_ENUM);
1134 enum_ = (GIrNodeEnum *) g_ir_node_new (G_IR_NODE_FLAGS);
1135 ((GIrNode *)enum_)->name = g_strdup (name);
1136 enum_->gtype_name = g_strdup (typename);
1137 enum_->gtype_init = g_strdup (typeinit);
1139 enum_->deprecated = TRUE;
1141 enum_->deprecated = FALSE;
1143 ctx->current_node = (GIrNode *) enum_;
1144 ctx->current_module->entries =
1145 g_list_append (ctx->current_module->entries, enum_);
1147 state_switch (ctx, STATE_ENUM);
1156 start_property (GMarkupParseContext *context,
1157 const gchar *element_name,
1158 const gchar **attribute_names,
1159 const gchar **attribute_values,
1163 if (strcmp (element_name, "property") == 0 &&
1164 (ctx->state == STATE_CLASS ||
1165 ctx->state == STATE_INTERFACE))
1168 const gchar *readable;
1169 const gchar *writable;
1170 const gchar *construct;
1171 const gchar *construct_only;
1173 name = find_attribute ("name", attribute_names, attribute_values);
1174 readable = find_attribute ("readable", attribute_names, attribute_values);
1175 writable = find_attribute ("writable", attribute_names, attribute_values);
1176 construct = find_attribute ("construct", attribute_names, attribute_values);
1177 construct_only = find_attribute ("construct-only", attribute_names, attribute_values);
1180 MISSING_ATTRIBUTE (context, error, element_name, "name");
1183 GIrNodeProperty *property;
1184 GIrNodeInterface *iface;
1186 property = (GIrNodeProperty *) g_ir_node_new (G_IR_NODE_PROPERTY);
1187 ctx->current_typed = (GIrNode*) property;
1189 ((GIrNode *)property)->name = g_strdup (name);
1191 /* Assume properties are readable */
1192 if (readable == NULL || strcmp (readable, "1") == 0)
1193 property->readable = TRUE;
1195 property->readable = FALSE;
1196 if (writable && strcmp (writable, "1") == 0)
1197 property->writable = TRUE;
1199 property->writable = FALSE;
1200 if (construct && strcmp (construct, "1") == 0)
1201 property->construct = TRUE;
1203 property->construct = FALSE;
1204 if (construct_only && strcmp (construct_only, "1") == 0)
1205 property->construct_only = TRUE;
1207 property->construct_only = FALSE;
1209 iface = (GIrNodeInterface *)ctx->current_node;
1210 iface->members = g_list_append (iface->members, property);
1212 if (ctx->state == STATE_CLASS)
1213 state_switch (ctx, STATE_CLASS_PROPERTY);
1214 else if (ctx->state == STATE_INTERFACE)
1215 state_switch (ctx, STATE_INTERFACE_PROPERTY);
1217 g_assert_not_reached ();
1226 parse_value (const gchar *str)
1230 /* FIXME just a quick hack */
1231 shift_op = strstr (str, "<<");
1237 base = strtol (str, NULL, 10);
1238 shift = strtol (shift_op + 3, NULL, 10);
1240 return base << shift;
1243 return strtol (str, NULL, 10);
1249 start_member (GMarkupParseContext *context,
1250 const gchar *element_name,
1251 const gchar **attribute_names,
1252 const gchar **attribute_values,
1256 if (strcmp (element_name, "member") == 0 &&
1257 ctx->state == STATE_ENUM)
1261 const gchar *deprecated;
1263 name = find_attribute ("name", attribute_names, attribute_values);
1264 value = find_attribute ("value", attribute_names, attribute_values);
1265 deprecated = find_attribute ("deprecated", attribute_names, attribute_values);
1268 MISSING_ATTRIBUTE (context, error, element_name, "name");
1272 GIrNodeValue *value_;
1274 value_ = (GIrNodeValue *) g_ir_node_new (G_IR_NODE_VALUE);
1276 ((GIrNode *)value_)->name = g_strdup (name);
1278 value_->value = parse_value (value);
1281 value_->deprecated = TRUE;
1283 value_->deprecated = FALSE;
1285 enum_ = (GIrNodeEnum *)ctx->current_node;
1286 enum_->values = g_list_append (enum_->values, value_);
1295 start_constant (GMarkupParseContext *context,
1296 const gchar *element_name,
1297 const gchar **attribute_names,
1298 const gchar **attribute_values,
1302 if (strcmp (element_name, "constant") == 0 &&
1303 (ctx->state == STATE_NAMESPACE ||
1304 ctx->state == STATE_CLASS ||
1305 ctx->state == STATE_INTERFACE))
1309 const gchar *deprecated;
1311 name = find_attribute ("name", attribute_names, attribute_values);
1312 value = find_attribute ("value", attribute_names, attribute_values);
1313 deprecated = find_attribute ("deprecated", attribute_names, attribute_values);
1316 MISSING_ATTRIBUTE (context, error, element_name, "name");
1317 else if (value == NULL)
1318 MISSING_ATTRIBUTE (context, error, element_name, "value");
1321 GIrNodeConstant *constant;
1323 constant = (GIrNodeConstant *) g_ir_node_new (G_IR_NODE_CONSTANT);
1325 ((GIrNode *)constant)->name = g_strdup (name);
1326 constant->value = g_strdup (value);
1328 ctx->current_typed = (GIrNode*) constant;
1331 constant->deprecated = TRUE;
1333 constant->deprecated = FALSE;
1335 if (ctx->state == STATE_NAMESPACE)
1337 ctx->current_node = (GIrNode *) constant;
1338 ctx->current_module->entries =
1339 g_list_append (ctx->current_module->entries, constant);
1343 GIrNodeInterface *iface;
1345 iface = (GIrNodeInterface *)ctx->current_node;
1346 iface->members = g_list_append (iface->members, constant);
1351 case STATE_NAMESPACE:
1352 state_switch (ctx, STATE_NAMESPACE_CONSTANT);
1355 state_switch (ctx, STATE_CLASS_CONSTANT);
1357 case STATE_INTERFACE:
1358 state_switch (ctx, STATE_INTERFACE_CONSTANT);
1361 g_assert_not_reached ();
1372 start_errordomain (GMarkupParseContext *context,
1373 const gchar *element_name,
1374 const gchar **attribute_names,
1375 const gchar **attribute_values,
1379 if (strcmp (element_name, "errordomain") == 0 &&
1380 ctx->state == STATE_NAMESPACE)
1383 const gchar *getquark;
1385 const gchar *deprecated;
1387 name = find_attribute ("name", attribute_names, attribute_values);
1388 getquark = find_attribute ("get-quark", attribute_names, attribute_values);
1389 codes = find_attribute ("codes", attribute_names, attribute_values);
1390 deprecated = find_attribute ("deprecated", attribute_names, attribute_values);
1393 MISSING_ATTRIBUTE (context, error, element_name, "name");
1394 else if (getquark == NULL)
1395 MISSING_ATTRIBUTE (context, error, element_name, "getquark");
1396 else if (codes == NULL)
1397 MISSING_ATTRIBUTE (context, error, element_name, "codes");
1400 GIrNodeErrorDomain *domain;
1402 domain = (GIrNodeErrorDomain *) g_ir_node_new (G_IR_NODE_ERROR_DOMAIN);
1404 ((GIrNode *)domain)->name = g_strdup (name);
1405 domain->getquark = g_strdup (getquark);
1406 domain->codes = g_strdup (codes);
1409 domain->deprecated = TRUE;
1411 domain->deprecated = FALSE;
1413 ctx->current_node = (GIrNode *) domain;
1414 ctx->current_module->entries =
1415 g_list_append (ctx->current_module->entries, domain);
1417 state_switch (ctx, STATE_ERRORDOMAIN);
1426 start_interface (GMarkupParseContext *context,
1427 const gchar *element_name,
1428 const gchar **attribute_names,
1429 const gchar **attribute_values,
1433 if (strcmp (element_name, "interface") == 0 &&
1434 ctx->state == STATE_NAMESPACE)
1437 const gchar *typename;
1438 const gchar *typeinit;
1439 const gchar *deprecated;
1441 name = find_attribute ("name", attribute_names, attribute_values);
1442 typename = find_attribute ("glib:type-name", attribute_names, attribute_values);
1443 typeinit = find_attribute ("glib:get-type", attribute_names, attribute_values);
1444 deprecated = find_attribute ("deprecated", attribute_names, attribute_values);
1447 MISSING_ATTRIBUTE (context, error, element_name, "name");
1448 else if (typename == NULL)
1449 MISSING_ATTRIBUTE (context, error, element_name, "glib:type-name");
1450 else if (typeinit == NULL)
1451 MISSING_ATTRIBUTE (context, error, element_name, "glib:get-type");
1454 GIrNodeInterface *iface;
1456 iface = (GIrNodeInterface *) g_ir_node_new (G_IR_NODE_INTERFACE);
1457 ((GIrNode *)iface)->name = g_strdup (name);
1458 iface->gtype_name = g_strdup (typename);
1459 iface->gtype_init = g_strdup (typeinit);
1461 iface->deprecated = TRUE;
1463 iface->deprecated = FALSE;
1465 ctx->current_node = (GIrNode *) iface;
1466 ctx->current_module->entries =
1467 g_list_append (ctx->current_module->entries, iface);
1469 state_switch (ctx, STATE_INTERFACE);
1479 start_class (GMarkupParseContext *context,
1480 const gchar *element_name,
1481 const gchar **attribute_names,
1482 const gchar **attribute_values,
1486 if (strcmp (element_name, "class") == 0 &&
1487 ctx->state == STATE_NAMESPACE)
1490 const gchar *parent;
1491 const gchar *typename;
1492 const gchar *typeinit;
1493 const gchar *deprecated;
1494 const gchar *abstract;
1496 name = find_attribute ("name", attribute_names, attribute_values);
1497 parent = find_attribute ("parent", attribute_names, attribute_values);
1498 typename = find_attribute ("glib:type-name", attribute_names, attribute_values);
1499 typeinit = find_attribute ("glib:get-type", attribute_names, attribute_values);
1500 deprecated = find_attribute ("deprecated", attribute_names, attribute_values);
1501 abstract = find_attribute ("abstract", attribute_names, attribute_values);
1504 MISSING_ATTRIBUTE (context, error, element_name, "name");
1505 else if (typename == NULL)
1506 MISSING_ATTRIBUTE (context, error, element_name, "glib:type-name");
1507 else if (typeinit == NULL && strcmp (typename, "GObject"))
1508 MISSING_ATTRIBUTE (context, error, element_name, "glib:get-type");
1511 GIrNodeInterface *iface;
1513 iface = (GIrNodeInterface *) g_ir_node_new (G_IR_NODE_OBJECT);
1514 ((GIrNode *)iface)->name = g_strdup (name);
1515 iface->gtype_name = g_strdup (typename);
1516 iface->gtype_init = g_strdup (typeinit);
1517 iface->parent = g_strdup (parent);
1519 iface->deprecated = TRUE;
1521 iface->deprecated = FALSE;
1523 iface->abstract = abstract && strcmp (abstract, "1") == 0;
1525 ctx->current_node = (GIrNode *) iface;
1526 ctx->current_module->entries =
1527 g_list_append (ctx->current_module->entries, iface);
1529 state_switch (ctx, STATE_CLASS);
1538 start_type (GMarkupParseContext *context,
1539 const gchar *element_name,
1540 const gchar **attribute_names,
1541 const gchar **attribute_values,
1548 gboolean is_varargs;
1549 GIrNodeType *typenode;
1551 is_array = strcmp (element_name, "array") == 0;
1552 is_varargs = strcmp (element_name, "varargs") == 0;
1554 if (!(is_array || is_varargs || (strcmp (element_name, "type") == 0)))
1557 if (ctx->state == STATE_TYPE)
1560 ctx->type_stack = g_list_prepend (ctx->type_stack, ctx->type_parameters);
1561 ctx->type_parameters = NULL;
1563 else if (ctx->state == STATE_FUNCTION_PARAMETER ||
1564 ctx->state == STATE_FUNCTION_RETURN ||
1565 ctx->state == STATE_STRUCT_FIELD ||
1566 ctx->state == STATE_UNION_FIELD ||
1567 ctx->state == STATE_CLASS_PROPERTY ||
1568 ctx->state == STATE_CLASS_FIELD ||
1569 ctx->state == STATE_INTERFACE_FIELD ||
1570 ctx->state == STATE_INTERFACE_PROPERTY ||
1571 ctx->state == STATE_BOXED_FIELD ||
1572 ctx->state == STATE_NAMESPACE_CONSTANT ||
1573 ctx->state == STATE_CLASS_CONSTANT ||
1574 ctx->state == STATE_INTERFACE_CONSTANT
1577 state_switch (ctx, STATE_TYPE);
1578 ctx->type_depth = 1;
1581 switch (ctx->current_node->type)
1583 case G_IR_NODE_FUNCTION:
1584 case G_IR_NODE_CALLBACK:
1586 GIrNodeFunction *func = (GIrNodeFunction *)ctx->current_node;
1587 func->is_varargs = TRUE;
1590 case G_IR_NODE_VFUNC:
1592 GIrNodeVFunc *vfunc = (GIrNodeVFunc *)ctx->current_node;
1593 vfunc->is_varargs = TRUE;
1596 /* list others individually rather than with default: so that compiler
1597 * warns if new node types are added without adding them to the switch
1599 case G_IR_NODE_INVALID:
1600 case G_IR_NODE_ENUM:
1601 case G_IR_NODE_FLAGS:
1602 case G_IR_NODE_CONSTANT:
1603 case G_IR_NODE_ERROR_DOMAIN:
1604 case G_IR_NODE_PARAM:
1605 case G_IR_NODE_TYPE:
1606 case G_IR_NODE_PROPERTY:
1607 case G_IR_NODE_SIGNAL:
1608 case G_IR_NODE_VALUE:
1609 case G_IR_NODE_FIELD:
1610 case G_IR_NODE_XREF:
1611 case G_IR_NODE_STRUCT:
1612 case G_IR_NODE_BOXED:
1613 case G_IR_NODE_OBJECT:
1614 case G_IR_NODE_INTERFACE:
1615 case G_IR_NODE_UNION:
1616 g_assert_not_reached ();
1620 ctx->type_stack = NULL;
1621 ctx->type_parameters = NULL;
1624 if (!ctx->current_typed)
1628 G_MARKUP_ERROR_INVALID_CONTENT,
1629 "The element <type> is invalid here");
1642 typenode = (GIrNodeType *)g_ir_node_new (G_IR_NODE_TYPE);
1644 typenode->tag = GI_TYPE_TAG_ARRAY;
1645 typenode->is_pointer = TRUE;
1646 typenode->is_array = TRUE;
1648 zero = find_attribute ("zero-terminated", attribute_names, attribute_values);
1649 len = find_attribute ("length", attribute_names, attribute_values);
1650 size = find_attribute ("fixed-size", attribute_names, attribute_values);
1652 typenode->zero_terminated = !(zero && strcmp (zero, "1") != 0);
1653 typenode->has_length = len != NULL;
1654 typenode->length = typenode->has_length ? atoi (len) : -1;
1656 typenode->has_size = size != NULL;
1657 typenode->size = typenode->has_size ? atoi (size) : -1;
1662 name = find_attribute ("name", attribute_names, attribute_values);
1665 MISSING_ATTRIBUTE (context, error, element_name, "name");
1668 ctype = find_attribute ("c:type", attribute_names, attribute_values);
1671 const char *cp = ctype + strlen(ctype) - 1;
1672 while (cp > ctype && *cp-- == '*')
1676 if (ctx->current_typed->type == G_IR_NODE_PARAM &&
1677 ((GIrNodeParam *)ctx->current_typed)->out &&
1681 typenode = parse_type (ctx, name);
1683 /* A 'disguised' structure is one where the c:type is a typedef that
1684 * doesn't look like a pointer, but is internally.
1686 if (typenode->tag == GI_TYPE_TAG_INTERFACE &&
1687 is_disguised_structure (ctx, typenode->interface))
1690 if (pointer_depth > 0)
1691 typenode->is_pointer = TRUE;
1694 ctx->type_parameters = g_list_append (ctx->type_parameters, typenode);
1700 end_type_top (ParseContext *ctx)
1702 GIrNodeType *typenode;
1704 if (!ctx->type_parameters)
1707 typenode = (GIrNodeType*)ctx->type_parameters->data;
1709 /* Default to pointer for unspecified containers */
1710 if (typenode->tag == GI_TYPE_TAG_ARRAY ||
1711 typenode->tag == GI_TYPE_TAG_GLIST ||
1712 typenode->tag == GI_TYPE_TAG_GSLIST)
1714 if (typenode->parameter_type1 == NULL)
1715 typenode->parameter_type1 = parse_type (ctx, "any");
1717 else if (typenode->tag == GI_TYPE_TAG_GHASH)
1719 if (typenode->parameter_type1 == NULL)
1721 typenode->parameter_type1 = parse_type (ctx, "any");
1722 typenode->parameter_type2 = parse_type (ctx, "any");
1726 switch (ctx->current_typed->type)
1728 case G_IR_NODE_PARAM:
1730 GIrNodeParam *param = (GIrNodeParam *)ctx->current_typed;
1731 param->type = typenode;
1734 case G_IR_NODE_FIELD:
1736 GIrNodeField *field = (GIrNodeField *)ctx->current_typed;
1737 field->type = typenode;
1740 case G_IR_NODE_PROPERTY:
1742 GIrNodeProperty *property = (GIrNodeProperty *) ctx->current_typed;
1743 property->type = typenode;
1746 case G_IR_NODE_CONSTANT:
1748 GIrNodeConstant *constant = (GIrNodeConstant *)ctx->current_typed;
1749 constant->type = typenode;
1753 g_printerr("current node is %d\n", ctx->current_node->type);
1754 g_assert_not_reached ();
1756 g_list_free (ctx->type_parameters);
1759 ctx->type_depth = 0;
1760 ctx->type_parameters = NULL;
1761 ctx->current_typed = NULL;
1765 end_type_recurse (ParseContext *ctx)
1767 GIrNodeType *parent;
1768 GIrNodeType *param = NULL;
1770 parent = (GIrNodeType *) ((GList*)ctx->type_stack->data)->data;
1771 if (ctx->type_parameters)
1772 param = (GIrNodeType *) ctx->type_parameters->data;
1774 if (parent->tag == GI_TYPE_TAG_ARRAY ||
1775 parent->tag == GI_TYPE_TAG_GLIST ||
1776 parent->tag == GI_TYPE_TAG_GSLIST)
1778 g_assert (param != NULL);
1780 if (parent->parameter_type1 == NULL)
1781 parent->parameter_type1 = param;
1783 g_assert_not_reached ();
1785 else if (parent->tag == GI_TYPE_TAG_GHASH)
1787 g_assert (param != NULL);
1789 if (parent->parameter_type1 == NULL)
1790 parent->parameter_type1 = param;
1791 else if (parent->parameter_type2 == NULL)
1792 parent->parameter_type2 = param;
1794 g_assert_not_reached ();
1796 g_list_free (ctx->type_parameters);
1797 ctx->type_parameters = (GList *)ctx->type_stack->data;
1798 ctx->type_stack = g_list_delete_link (ctx->type_stack, ctx->type_stack);
1802 end_type (ParseContext *ctx)
1804 if (ctx->type_depth == 1)
1807 state_switch (ctx, ctx->prev_state);
1811 end_type_recurse (ctx);
1817 start_return_value (GMarkupParseContext *context,
1818 const gchar *element_name,
1819 const gchar **attribute_names,
1820 const gchar **attribute_values,
1824 if (strcmp (element_name, "return-value") == 0 &&
1825 ctx->state == STATE_FUNCTION)
1827 GIrNodeParam *param;
1828 const gchar *transfer;
1830 param = (GIrNodeParam *)g_ir_node_new (G_IR_NODE_PARAM);
1833 param->retval = TRUE;
1835 ctx->current_typed = (GIrNode*) param;
1837 state_switch (ctx, STATE_FUNCTION_RETURN);
1839 transfer = find_attribute ("transfer-ownership", attribute_names, attribute_values);
1840 parse_param_transfer (param, transfer);
1842 switch (ctx->current_node->type)
1844 case G_IR_NODE_FUNCTION:
1845 case G_IR_NODE_CALLBACK:
1847 GIrNodeFunction *func = (GIrNodeFunction *)ctx->current_node;
1848 func->result = param;
1851 case G_IR_NODE_SIGNAL:
1853 GIrNodeSignal *signal = (GIrNodeSignal *)ctx->current_node;
1854 signal->result = param;
1857 case G_IR_NODE_VFUNC:
1859 GIrNodeVFunc *vfunc = (GIrNodeVFunc *)ctx->current_node;
1860 vfunc->result = param;
1864 g_assert_not_reached ();
1874 start_implements (GMarkupParseContext *context,
1875 const gchar *element_name,
1876 const gchar **attribute_names,
1877 const gchar **attribute_values,
1881 GIrNodeInterface *iface;
1884 if (strcmp (element_name, "implements") != 0 ||
1885 !(ctx->state == STATE_CLASS))
1888 state_switch (ctx, STATE_IMPLEMENTS);
1890 name = find_attribute ("name", attribute_names, attribute_values);
1893 MISSING_ATTRIBUTE (context, error, element_name, "name");
1897 iface = (GIrNodeInterface *)ctx->current_node;
1898 iface->interfaces = g_list_append (iface->interfaces, g_strdup (name));
1904 start_glib_signal (GMarkupParseContext *context,
1905 const gchar *element_name,
1906 const gchar **attribute_names,
1907 const gchar **attribute_values,
1911 if (strcmp (element_name, "glib:signal") == 0 &&
1912 (ctx->state == STATE_CLASS ||
1913 ctx->state == STATE_INTERFACE))
1917 const gchar *no_recurse;
1918 const gchar *detailed;
1919 const gchar *action;
1920 const gchar *no_hooks;
1921 const gchar *has_class_closure;
1923 name = find_attribute ("name", attribute_names, attribute_values);
1924 when = find_attribute ("when", attribute_names, attribute_values);
1925 no_recurse = find_attribute ("no-recurse", attribute_names, attribute_values);
1926 detailed = find_attribute ("detailed", attribute_names, attribute_values);
1927 action = find_attribute ("action", attribute_names, attribute_values);
1928 no_hooks = find_attribute ("no-hooks", attribute_names, attribute_values);
1929 has_class_closure = find_attribute ("has-class-closure", attribute_names, attribute_values);
1932 MISSING_ATTRIBUTE (context, error, element_name, "name");
1935 GIrNodeInterface *iface;
1936 GIrNodeSignal *signal;
1938 signal = (GIrNodeSignal *)g_ir_node_new (G_IR_NODE_SIGNAL);
1940 ((GIrNode *)signal)->name = g_strdup (name);
1942 signal->run_first = FALSE;
1943 signal->run_last = FALSE;
1944 signal->run_cleanup = FALSE;
1945 if (when == NULL || strcmp (when, "LAST") == 0)
1946 signal->run_last = TRUE;
1947 else if (strcmp (when, "FIRST") == 0)
1948 signal->run_first = TRUE;
1950 signal->run_cleanup = TRUE;
1952 if (no_recurse && strcmp (no_recurse, "1") == 0)
1953 signal->no_recurse = TRUE;
1955 signal->no_recurse = FALSE;
1956 if (detailed && strcmp (detailed, "1") == 0)
1957 signal->detailed = TRUE;
1959 signal->detailed = FALSE;
1960 if (action && strcmp (action, "1") == 0)
1961 signal->action = TRUE;
1963 signal->action = FALSE;
1964 if (no_hooks && strcmp (no_hooks, "1") == 0)
1965 signal->no_hooks = TRUE;
1967 signal->no_hooks = FALSE;
1968 if (has_class_closure && strcmp (has_class_closure, "1") == 0)
1969 signal->has_class_closure = TRUE;
1971 signal->has_class_closure = FALSE;
1973 iface = (GIrNodeInterface *)ctx->current_node;
1974 iface->members = g_list_append (iface->members, signal);
1976 ctx->current_node = (GIrNode *)signal;
1977 state_switch (ctx, STATE_FUNCTION);
1986 start_vfunc (GMarkupParseContext *context,
1987 const gchar *element_name,
1988 const gchar **attribute_names,
1989 const gchar **attribute_values,
1993 if (strcmp (element_name, "vfunc") == 0 &&
1994 (ctx->state == STATE_CLASS ||
1995 ctx->state == STATE_INTERFACE))
1998 const gchar *must_chain_up;
1999 const gchar *override;
2000 const gchar *is_class_closure;
2001 const gchar *offset;
2003 name = find_attribute ("name", attribute_names, attribute_values);
2004 must_chain_up = find_attribute ("must-chain-up", attribute_names, attribute_values);
2005 override = find_attribute ("override", attribute_names, attribute_values);
2006 is_class_closure = find_attribute ("is-class-closure", attribute_names, attribute_values);
2007 offset = find_attribute ("offset", attribute_names, attribute_values);
2010 MISSING_ATTRIBUTE (context, error, element_name, "name");
2013 GIrNodeInterface *iface;
2014 GIrNodeVFunc *vfunc;
2016 vfunc = (GIrNodeVFunc *)g_ir_node_new (G_IR_NODE_VFUNC);
2018 ((GIrNode *)vfunc)->name = g_strdup (name);
2020 if (must_chain_up && strcmp (must_chain_up, "1") == 0)
2021 vfunc->must_chain_up = TRUE;
2023 vfunc->must_chain_up = FALSE;
2025 if (override && strcmp (override, "always") == 0)
2027 vfunc->must_be_implemented = TRUE;
2028 vfunc->must_not_be_implemented = FALSE;
2030 else if (override && strcmp (override, "never") == 0)
2032 vfunc->must_be_implemented = FALSE;
2033 vfunc->must_not_be_implemented = TRUE;
2037 vfunc->must_be_implemented = FALSE;
2038 vfunc->must_not_be_implemented = FALSE;
2041 if (is_class_closure && strcmp (is_class_closure, "1") == 0)
2042 vfunc->is_class_closure = TRUE;
2044 vfunc->is_class_closure = FALSE;
2047 vfunc->offset = atoi (offset);
2051 iface = (GIrNodeInterface *)ctx->current_node;
2052 iface->members = g_list_append (iface->members, vfunc);
2054 ctx->current_node = (GIrNode *)vfunc;
2055 state_switch (ctx, STATE_FUNCTION);
2065 start_struct (GMarkupParseContext *context,
2066 const gchar *element_name,
2067 const gchar **attribute_names,
2068 const gchar **attribute_values,
2072 if (strcmp (element_name, "record") == 0 &&
2073 ctx->state == STATE_NAMESPACE)
2076 const gchar *deprecated;
2077 const gchar *disguised;
2078 const gchar *gtype_name;
2079 const gchar *gtype_init;
2080 GIrNodeStruct *struct_;
2082 name = find_attribute ("name", attribute_names, attribute_values);
2083 deprecated = find_attribute ("deprecated", attribute_names, attribute_values);
2084 disguised = find_attribute ("disguised", attribute_names, attribute_values);
2085 gtype_name = find_attribute ("glib:type-name", attribute_names, attribute_values);
2086 gtype_init = find_attribute ("glib:get-type", attribute_names, attribute_values);
2090 MISSING_ATTRIBUTE (context, error, element_name, "name");
2093 if ((gtype_name == NULL && gtype_init != NULL))
2095 MISSING_ATTRIBUTE (context, error, element_name, "glib:type-name");
2098 if ((gtype_name != NULL && gtype_init == NULL))
2100 MISSING_ATTRIBUTE (context, error, element_name, "glib:get-type");
2104 struct_ = (GIrNodeStruct *) g_ir_node_new (G_IR_NODE_STRUCT);
2106 ((GIrNode *)struct_)->name = g_strdup (name);
2108 struct_->deprecated = TRUE;
2110 struct_->deprecated = FALSE;
2112 if (disguised && strcmp (disguised, "1") == 0)
2113 struct_->disguised = TRUE;
2115 struct_->gtype_name = g_strdup (gtype_name);
2116 struct_->gtype_init = g_strdup (gtype_init);
2118 ctx->current_node = (GIrNode *)struct_;
2119 ctx->current_module->entries =
2120 g_list_append (ctx->current_module->entries, struct_);
2122 state_switch (ctx, STATE_STRUCT);
2130 start_union (GMarkupParseContext *context,
2131 const gchar *element_name,
2132 const gchar **attribute_names,
2133 const gchar **attribute_values,
2137 if (strcmp (element_name, "union") == 0 &&
2138 ctx->state == STATE_NAMESPACE)
2141 const gchar *deprecated;
2142 const gchar *typename;
2143 const gchar *typeinit;
2145 name = find_attribute ("name", attribute_names, attribute_values);
2146 deprecated = find_attribute ("deprecated", attribute_names, attribute_values);
2147 typename = find_attribute ("glib:type-name", attribute_names, attribute_values);
2148 typeinit = find_attribute ("glib:get-type", attribute_names, attribute_values);
2151 MISSING_ATTRIBUTE (context, error, element_name, "name");
2154 GIrNodeUnion *union_;
2156 union_ = (GIrNodeUnion *) g_ir_node_new (G_IR_NODE_UNION);
2158 ((GIrNode *)union_)->name = g_strdup (name);
2159 union_->gtype_name = g_strdup (typename);
2160 union_->gtype_init = g_strdup (typeinit);
2162 union_->deprecated = TRUE;
2164 union_->deprecated = FALSE;
2166 ctx->current_node = (GIrNode *)union_;
2167 ctx->current_module->entries =
2168 g_list_append (ctx->current_module->entries, union_);
2170 state_switch (ctx, STATE_UNION);
2178 start_discriminator (GMarkupParseContext *context,
2179 const gchar *element_name,
2180 const gchar **attribute_names,
2181 const gchar **attribute_values,
2185 if (strcmp (element_name, "discriminator") == 0 &&
2186 ctx->state == STATE_UNION)
2189 const gchar *offset;
2191 type = find_attribute ("type", attribute_names, attribute_values);
2192 offset = find_attribute ("offset", attribute_names, attribute_values);
2194 MISSING_ATTRIBUTE (context, error, element_name, "type");
2195 else if (offset == NULL)
2196 MISSING_ATTRIBUTE (context, error, element_name, "offset");
2198 ((GIrNodeUnion *)ctx->current_node)->discriminator_type
2199 = parse_type (ctx, type);
2200 ((GIrNodeUnion *)ctx->current_node)->discriminator_offset
2211 parse_include (GMarkupParseContext *context,
2214 const char *version,
2220 gboolean success = FALSE;
2224 for (l = ctx->parser->parsed_modules; l; l = l->next)
2226 GIrModule *m = l->data;
2228 if (strcmp (m->name, name) == 0)
2230 if (strcmp (m->version, version) == 0)
2232 ctx->include_modules = g_list_prepend (ctx->include_modules, m);
2240 G_MARKUP_ERROR_INVALID_CONTENT,
2241 "Module '%s' imported with conflicting versions '%s' and '%s'",
2242 name, m->version, version);
2248 girpath = locate_gir (ctx->parser, name, version);
2250 if (girpath == NULL)
2254 G_MARKUP_ERROR_INVALID_CONTENT,
2255 "Could not find GIR file '%s.gir'; check XDG_DATA_DIRS or use --includedir",
2260 g_debug ("Parsing include %s", girpath);
2262 if (!g_file_get_contents (girpath, &buffer, &length, error))
2269 modules = g_ir_parser_parse_string (ctx->parser, name, buffer, length, error);
2270 success = error != NULL;
2272 ctx->include_modules = g_list_concat (ctx->include_modules,
2280 extern GLogLevelFlags logged_levels;
2283 start_element_handler (GMarkupParseContext *context,
2284 const gchar *element_name,
2285 const gchar **attribute_names,
2286 const gchar **attribute_values,
2290 ParseContext *ctx = user_data;
2291 gint line_number, char_number;
2293 if (logged_levels & G_LOG_LEVEL_DEBUG)
2295 GString *tags = g_string_new ("");
2297 for (i = 0; attribute_names[i]; i++)
2298 g_string_append_printf (tags, "%s=\"%s\" ",
2300 attribute_values[i]);
2304 g_string_insert_c (tags, 0, ' ');
2305 g_string_truncate (tags, tags->len - 1);
2307 g_debug ("<%s%s>", element_name, tags->str);
2308 g_string_free (tags, TRUE);
2311 switch (element_name[0])
2314 if (ctx->state == STATE_NAMESPACE && strcmp (element_name, "alias") == 0)
2316 state_switch (ctx, STATE_ALIAS);
2319 if (start_type (context, element_name,
2320 attribute_names, attribute_values,
2325 if (start_enum (context, element_name,
2326 attribute_names, attribute_values,
2331 if (start_function (context, element_name,
2332 attribute_names, attribute_values,
2335 else if (start_constant (context, element_name,
2336 attribute_names, attribute_values,
2339 else if (start_class (context, element_name,
2340 attribute_names, attribute_values,
2346 if (start_discriminator (context, element_name,
2347 attribute_names, attribute_values,
2353 if (start_enum (context, element_name,
2354 attribute_names, attribute_values,
2357 else if (start_errordomain (context, element_name,
2358 attribute_names, attribute_values,
2364 if (start_function (context, element_name,
2365 attribute_names, attribute_values,
2368 else if (start_field (context, element_name,
2369 attribute_names, attribute_values,
2375 if (start_glib_boxed (context, element_name,
2376 attribute_names, attribute_values,
2379 else if (start_glib_signal (context, element_name,
2380 attribute_names, attribute_values,
2386 if (strcmp (element_name, "include") == 0 &&
2387 ctx->state == STATE_REPOSITORY)
2390 const gchar *version;
2392 name = find_attribute ("name", attribute_names, attribute_values);
2393 version = find_attribute ("version", attribute_names, attribute_values);
2397 MISSING_ATTRIBUTE (context, error, element_name, "name");
2400 if (version == NULL)
2402 MISSING_ATTRIBUTE (context, error, element_name, "version");
2406 if (!parse_include (context, ctx, name, version, error))
2409 ctx->dependencies = g_list_prepend (ctx->dependencies,
2410 g_strdup_printf ("%s-%s", name, version));
2413 state_switch (ctx, STATE_INCLUDE);
2416 if (start_interface (context, element_name,
2417 attribute_names, attribute_values,
2420 else if (start_implements (context, element_name,
2421 attribute_names, attribute_values,
2427 if (start_function (context, element_name,
2428 attribute_names, attribute_values,
2431 else if (start_member (context, element_name,
2432 attribute_names, attribute_values,
2438 if (strcmp (element_name, "namespace") == 0 && ctx->state == STATE_REPOSITORY)
2440 const gchar *name, *version, *shared_library;
2442 if (ctx->current_module != NULL)
2446 G_MARKUP_ERROR_INVALID_CONTENT,
2447 "Only one <namespace/> element is currently allowed per <repository/>");
2451 name = find_attribute ("name", attribute_names, attribute_values);
2452 version = find_attribute ("version", attribute_names, attribute_values);
2453 shared_library = find_attribute ("shared-library", attribute_names, attribute_values);
2456 MISSING_ATTRIBUTE (context, error, element_name, "name");
2457 else if (version == NULL)
2458 MISSING_ATTRIBUTE (context, error, element_name, "version");
2463 if (strcmp (name, ctx->namespace) != 0)
2466 G_MARKUP_ERROR_INVALID_CONTENT,
2467 "<namespace/> name element '%s' doesn't match file name '%s'",
2468 name, ctx->namespace);
2470 ctx->current_module = g_ir_module_new (name, version, shared_library);
2472 ctx->current_module->aliases = ctx->aliases;
2473 ctx->aliases = NULL;
2474 ctx->current_module->disguised_structures = ctx->disguised_structures;
2475 ctx->disguised_structures = NULL;
2477 for (l = ctx->include_modules; l; l = l->next)
2478 g_ir_module_add_include_module (ctx->current_module, l->data);
2480 g_list_free (ctx->include_modules);
2481 ctx->include_modules = NULL;
2483 ctx->modules = g_list_append (ctx->modules, ctx->current_module);
2484 ctx->current_module->dependencies = ctx->dependencies;
2486 state_switch (ctx, STATE_NAMESPACE);
2493 if (start_property (context, element_name,
2494 attribute_names, attribute_values,
2497 else if (strcmp (element_name, "parameters") == 0 &&
2498 ctx->state == STATE_FUNCTION)
2500 state_switch (ctx, STATE_FUNCTION_PARAMETERS);
2504 else if (start_parameter (context, element_name,
2505 attribute_names, attribute_values,
2508 else if (strcmp (element_name, "prerequisite") == 0 &&
2509 ctx->state == STATE_INTERFACE)
2513 name = find_attribute ("name", attribute_names, attribute_values);
2515 state_switch (ctx, STATE_PREREQUISITE);
2518 MISSING_ATTRIBUTE (context, error, element_name, "name");
2521 GIrNodeInterface *iface;
2523 iface = (GIrNodeInterface *)ctx->current_node;
2524 iface->prerequisites = g_list_append (iface->prerequisites, g_strdup (name));
2531 if (strcmp (element_name, "repository") == 0 && ctx->state == STATE_START)
2533 const gchar *version;
2535 version = find_attribute ("version", attribute_names, attribute_values);
2537 if (version == NULL)
2538 MISSING_ATTRIBUTE (context, error, element_name, "version");
2539 else if (strcmp (version, "1.0") != 0)
2542 G_MARKUP_ERROR_INVALID_CONTENT,
2543 "Unsupported version '%s'",
2546 state_switch (ctx, STATE_REPOSITORY);
2550 else if (start_return_value (context, element_name,
2551 attribute_names, attribute_values,
2554 else if (start_struct (context, element_name,
2555 attribute_names, attribute_values,
2561 if (start_union (context, element_name,
2562 attribute_names, attribute_values,
2568 if (start_type (context, element_name,
2569 attribute_names, attribute_values,
2575 if (start_vfunc (context, element_name,
2576 attribute_names, attribute_values,
2579 if (start_type (context, element_name,
2580 attribute_names, attribute_values,
2586 g_markup_parse_context_get_position (context, &line_number, &char_number);
2588 if (error && *error == NULL)
2591 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
2592 "Unexpected start tag '%s' on line %d char %d; current state=%d",
2594 line_number, char_number, ctx->state);
2599 g_markup_parse_context_get_position (context, &line_number, &char_number);
2601 fprintf (stderr, "Error at line %d, character %d: %s\n", line_number, char_number, (*error)->message);
2602 backtrace_stderr ();
2607 require_one_of_end_elements (GMarkupParseContext *context,
2609 const char *actual_name,
2614 int line_number, char_number;
2615 const char *expected;
2616 gboolean matched = FALSE;
2618 va_start (args, error);
2620 while ((expected = va_arg (args, const char*)) != NULL)
2622 if (strcmp (expected, actual_name) == 0)
2634 g_markup_parse_context_get_position (context, &line_number, &char_number);
2637 G_MARKUP_ERROR_INVALID_CONTENT,
2638 "Unexpected end tag '%s' on line %d char %d; current state=%d",
2640 line_number, char_number, ctx->state);
2646 require_end_element (GMarkupParseContext *context,
2648 const char *expected_name,
2649 const char *actual_name,
2652 return require_one_of_end_elements (context, ctx, actual_name, error, expected_name, NULL);
2656 end_element_handler (GMarkupParseContext *context,
2657 const gchar *element_name,
2661 ParseContext *ctx = user_data;
2663 g_debug ("</%s>", element_name);
2669 /* no need to GError here, GMarkup already catches this */
2672 case STATE_REPOSITORY:
2673 state_switch (ctx, STATE_END);
2677 if (require_end_element (context, ctx, "include", element_name, error))
2679 state_switch (ctx, STATE_REPOSITORY);
2683 case STATE_NAMESPACE:
2684 if (require_end_element (context, ctx, "namespace", element_name, error))
2686 ctx->current_module = NULL;
2687 state_switch (ctx, STATE_REPOSITORY);
2692 if (require_end_element (context, ctx, "alias", element_name, error))
2694 state_switch (ctx, STATE_NAMESPACE);
2698 case STATE_FUNCTION_RETURN:
2699 if (strcmp ("type", element_name) == 0)
2701 if (require_end_element (context, ctx, "return-value", element_name, error))
2703 state_switch (ctx, STATE_FUNCTION);
2707 case STATE_FUNCTION_PARAMETERS:
2708 if (require_end_element (context, ctx, "parameters", element_name, error))
2710 state_switch (ctx, STATE_FUNCTION);
2714 case STATE_FUNCTION_PARAMETER:
2715 if (strcmp ("type", element_name) == 0)
2717 if (require_end_element (context, ctx, "parameter", element_name, error))
2719 state_switch (ctx, STATE_FUNCTION_PARAMETERS);
2723 case STATE_FUNCTION:
2725 gboolean current_is_toplevel;
2726 GList *last = g_list_last (ctx->current_module->entries);
2728 current_is_toplevel = ctx->current_node == last->data;
2730 if (current_is_toplevel)
2732 ctx->current_node = NULL;
2733 state_switch (ctx, STATE_NAMESPACE);
2737 ctx->current_node = g_list_last (ctx->current_module->entries)->data;
2738 if (ctx->current_node->type == G_IR_NODE_INTERFACE)
2739 state_switch (ctx, STATE_INTERFACE);
2740 else if (ctx->current_node->type == G_IR_NODE_OBJECT)
2741 state_switch (ctx, STATE_CLASS);
2742 else if (ctx->current_node->type == G_IR_NODE_BOXED)
2743 state_switch (ctx, STATE_BOXED);
2744 else if (ctx->current_node->type == G_IR_NODE_STRUCT)
2745 state_switch (ctx, STATE_STRUCT);
2746 else if (ctx->current_node->type == G_IR_NODE_UNION)
2747 state_switch (ctx, STATE_UNION);
2750 int line_number, char_number;
2751 g_markup_parse_context_get_position (context, &line_number, &char_number);
2754 G_MARKUP_ERROR_INVALID_CONTENT,
2755 "Unexpected end tag '%s' on line %d char %d",
2757 line_number, char_number);
2763 case STATE_CLASS_FIELD:
2764 if (strcmp ("type", element_name) == 0)
2766 if (require_end_element (context, ctx, "field", element_name, error))
2768 state_switch (ctx, STATE_CLASS);
2772 case STATE_CLASS_PROPERTY:
2773 if (strcmp ("type", element_name) == 0)
2775 if (require_end_element (context, ctx, "property", element_name, error))
2777 state_switch (ctx, STATE_CLASS);
2782 if (require_end_element (context, ctx, "class", element_name, error))
2784 ctx->current_node = NULL;
2785 state_switch (ctx, STATE_NAMESPACE);
2789 case STATE_ERRORDOMAIN:
2790 if (require_end_element (context, ctx, "errordomain", element_name, error))
2792 ctx->current_node = NULL;
2793 state_switch (ctx, STATE_NAMESPACE);
2797 case STATE_INTERFACE_PROPERTY:
2798 if (strcmp ("type", element_name) == 0)
2800 if (require_end_element (context, ctx, "property", element_name, error))
2802 state_switch (ctx, STATE_INTERFACE);
2806 case STATE_INTERFACE_FIELD:
2807 if (strcmp ("type", element_name) == 0)
2809 if (require_end_element (context, ctx, "field", element_name, error))
2811 state_switch (ctx, STATE_INTERFACE);
2815 case STATE_INTERFACE:
2816 if (require_end_element (context, ctx, "interface", element_name, error))
2818 ctx->current_node = NULL;
2819 state_switch (ctx, STATE_NAMESPACE);
2824 if (strcmp ("member", element_name) == 0)
2826 else if (require_one_of_end_elements (context, ctx,
2827 element_name, error, "enumeration",
2830 ctx->current_node = NULL;
2831 state_switch (ctx, STATE_NAMESPACE);
2836 if (require_end_element (context, ctx, "glib:boxed", element_name, error))
2838 ctx->current_node = NULL;
2839 state_switch (ctx, STATE_NAMESPACE);
2843 case STATE_BOXED_FIELD:
2844 if (strcmp ("type", element_name) == 0)
2846 if (require_end_element (context, ctx, "field", element_name, error))
2848 state_switch (ctx, STATE_BOXED);
2852 case STATE_STRUCT_FIELD:
2853 if (strcmp ("type", element_name) == 0)
2855 if (require_end_element (context, ctx, "field", element_name, error))
2857 state_switch (ctx, STATE_STRUCT);
2862 if (require_end_element (context, ctx, "record", element_name, error))
2864 ctx->current_node = NULL;
2865 state_switch (ctx, STATE_NAMESPACE);
2869 case STATE_UNION_FIELD:
2870 if (strcmp ("type", element_name) == 0)
2872 if (require_end_element (context, ctx, "field", element_name, error))
2874 state_switch (ctx, STATE_UNION);
2879 if (require_end_element (context, ctx, "union", element_name, error))
2881 ctx->current_node = NULL;
2882 state_switch (ctx, STATE_NAMESPACE);
2885 case STATE_IMPLEMENTS:
2886 if (strcmp ("interface", element_name) == 0)
2888 if (require_end_element (context, ctx, "implements", element_name, error))
2889 state_switch (ctx, STATE_CLASS);
2891 case STATE_PREREQUISITE:
2892 if (require_end_element (context, ctx, "prerequisite", element_name, error))
2893 state_switch (ctx, STATE_INTERFACE);
2895 case STATE_NAMESPACE_CONSTANT:
2896 case STATE_CLASS_CONSTANT:
2897 case STATE_INTERFACE_CONSTANT:
2898 if (strcmp ("type", element_name) == 0)
2900 if (require_end_element (context, ctx, "constant", element_name, error))
2902 ctx->current_node = NULL;
2905 case STATE_NAMESPACE_CONSTANT:
2906 state_switch (ctx, STATE_NAMESPACE);
2908 case STATE_CLASS_CONSTANT:
2909 state_switch (ctx, STATE_CLASS);
2911 case STATE_INTERFACE_CONSTANT:
2912 state_switch (ctx, STATE_INTERFACE);
2915 g_assert_not_reached ();
2921 if ((strcmp ("type", element_name) == 0) || (strcmp ("array", element_name) == 0) ||
2922 (strcmp ("varargs", element_name) == 0))
2928 g_error ("Unhandled state %d in end_element_handler\n", ctx->state);
2933 text_handler (GMarkupParseContext *context,
2939 /* FIXME warn about non-whitespace text */
2943 cleanup (GMarkupParseContext *context,
2947 ParseContext *ctx = user_data;
2950 for (m = ctx->modules; m; m = m->next)
2951 g_ir_module_free (m->data);
2952 g_list_free (ctx->modules);
2953 ctx->modules = NULL;
2955 ctx->current_module = NULL;
2959 post_filter_varargs_functions (GList *list)
2967 GIrNode *node = iter->data;
2971 if (node->type == G_IR_NODE_FUNCTION)
2973 if (((GIrNodeFunction*)node)->is_varargs)
2975 list = g_list_delete_link (list, link);
2983 post_filter (GIrModule *module)
2987 module->entries = post_filter_varargs_functions (module->entries);
2988 iter = module->entries;
2991 GIrNode *node = iter->data;
2995 if (node->type == G_IR_NODE_OBJECT ||
2996 node->type == G_IR_NODE_INTERFACE)
2998 GIrNodeInterface *iface = (GIrNodeInterface*)node;
2999 iface->members = post_filter_varargs_functions (iface->members);
3001 else if (node->type == G_IR_NODE_BOXED)
3003 GIrNodeBoxed *boxed = (GIrNodeBoxed*)node;
3004 boxed->members = post_filter_varargs_functions (boxed->members);
3006 else if (node->type == G_IR_NODE_STRUCT)
3008 GIrNodeStruct *iface = (GIrNodeStruct*)node;
3009 iface->members = post_filter_varargs_functions (iface->members);
3011 else if (node->type == G_IR_NODE_UNION)
3013 GIrNodeUnion *iface = (GIrNodeUnion*)node;
3014 iface->members = post_filter_varargs_functions (iface->members);
3020 * g_ir_parser_parse_string:
3021 * @parser: a #GIrParser
3022 * @error: return location for a #GError, or %NULL
3024 * Parse a string that holds a complete GIR XML file, and return a list of a
3025 * a #GirModule for each <namespace/> element within the file.
3027 * @returns: a newly allocated list of #GIrModule. The modules themselves
3028 * are owned by the #GIrParser and will be freed along with the parser.
3031 g_ir_parser_parse_string (GIrParser *parser,
3032 const gchar *namespace,
3033 const gchar *buffer,
3037 ParseContext ctx = { 0 };
3038 GMarkupParseContext *context;
3040 ctx.parser = parser;
3041 ctx.state = STATE_START;
3042 ctx.namespace = namespace;
3043 ctx.include_modules = NULL;
3044 ctx.aliases = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
3045 ctx.disguised_structures = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
3047 ctx.dependencies = NULL;
3048 ctx.current_module = NULL;
3050 context = g_markup_parse_context_new (&firstpass_parser, 0, &ctx, NULL);
3052 if (!g_markup_parse_context_parse (context, buffer, length, error))
3055 if (!g_markup_parse_context_end_parse (context, error))
3058 g_markup_parse_context_free (context);
3060 context = g_markup_parse_context_new (&markup_parser, 0, &ctx, NULL);
3061 if (!g_markup_parse_context_parse (context, buffer, length, error))
3064 if (!g_markup_parse_context_end_parse (context, error))
3067 parser->parsed_modules = g_list_concat (g_list_copy (ctx.modules),
3068 parser->parsed_modules);
3072 if (ctx.modules == NULL)
3074 /* An error occurred before we created a module, so we haven't
3075 * transferred ownership of these hash tables to the module.
3077 if (ctx.aliases != NULL)
3078 g_hash_table_destroy (ctx.aliases);
3079 if (ctx.disguised_structures != NULL)
3080 g_hash_table_destroy (ctx.disguised_structures);
3081 g_list_free (ctx.include_modules);
3084 g_markup_parse_context_free (context);
3090 * g_ir_parser_parse_file:
3091 * @parser: a #GIrParser
3092 * @error: return location for a #GError, or %NULL
3094 * Parse GIR XML file, and return a list of a a #GirModule for each
3095 * <namespace/> element within the file.
3097 * @returns: a newly allocated list of #GIrModule. The modules themselves
3098 * are owned by the #GIrParser and will be freed along with the parser.
3101 g_ir_parser_parse_file (GIrParser *parser,
3102 const gchar *filename,
3113 if (!g_str_has_suffix (filename, ".gir"))
3117 G_MARKUP_ERROR_INVALID_CONTENT,
3118 "Expected filename to end with '.gir'");
3122 g_debug ("[parsing] filename %s", filename);
3124 slash = g_strrstr (filename, "/");
3126 namespace = g_strdup (filename);
3128 namespace = g_strdup (slash+1);
3129 namespace[strlen(namespace)-4] = '\0';
3131 /* Remove version */
3132 dash = strstr (namespace, "-");
3136 if (!g_file_get_contents (filename, &buffer, &length, error))
3139 modules = g_ir_parser_parse_string (parser, namespace, buffer, length, error);
3141 for (iter = modules; iter; iter = iter->next)
3143 post_filter ((GIrModule*)iter->data);