1 /* GObject introspection: C parser
3 * Copyright (c) 1997 Sandro Sigala <ssigala@globalnet.it>
4 * Copyright (c) 2007-2008 Jürg Billeter <j@bitron.ch>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <glib/gstdio.h>
36 #include "sourcescanner.h"
37 #include "scannerparser.h"
41 extern char linebuf[2000];
44 extern int yylex (GISourceScanner *scanner);
45 static void yyerror (GISourceScanner *scanner, const char *str);
47 extern void ctype_free (GISourceType * type);
49 static int last_enum_value = -1;
50 static gboolean is_bitfield;
51 static GHashTable *const_table = NULL;
54 * parse_c_string_literal:
55 * @str: A string containing a C string literal
57 * Based on g_strcompress(), but also handles
58 * hexadecimal escapes.
61 parse_c_string_literal (const char *str)
63 const gchar *p = str, *num;
64 gchar *dest = g_malloc (strlen (str) + 1);
75 g_warning ("parse_c_string_literal: trailing \\");
77 case '0': case '1': case '2': case '3': case '4':
78 case '5': case '6': case '7':
81 while ((p < num + 3) && (*p >= '0') && (*p <= '7'))
83 *q = (*q * 8) + (*p - '0');
93 while ((p < num + 2) && (g_ascii_isxdigit(*p)))
95 *q = (*q * 16) + g_ascii_xdigit_value(*p);
116 default: /* Also handles \" and \\ */
137 GISourceSymbol *symbol;
139 StorageClassSpecifier storage_class_specifier;
140 TypeQualifier type_qualifier;
141 FunctionSpecifier function_specifier;
142 UnaryOperator unary_operator;
145 %parse-param { GISourceScanner* scanner }
146 %lex-param { GISourceScanner* scanner }
148 %token <str> IDENTIFIER "identifier"
149 %token <str> TYPEDEF_NAME "typedef-name"
151 %token INTEGER FLOATING CHARACTER STRING
153 %token INTL_CONST INTUL_CONST
154 %token ELLIPSIS ADDEQ SUBEQ MULEQ DIVEQ MODEQ XOREQ ANDEQ OREQ SL SR
155 %token SLEQ SREQ EQ NOTEQ LTEQ GTEQ ANDAND OROR PLUSPLUS MINUSMINUS ARROW
157 %token AUTO BOOL BREAK CASE CHAR CONST CONTINUE DEFAULT DO DOUBLE ELSE ENUM
158 %token EXTENSION EXTERN FLOAT FOR GOTO IF INLINE INT LONG REGISTER RESTRICT
159 %token RETURN SHORT SIGNED SIZEOF STATIC STRUCT SWITCH TYPEDEF UNION UNSIGNED
160 %token VOID VOLATILE WHILE
162 %token FUNCTION_MACRO OBJECT_MACRO
164 %start translation_unit
166 %type <ctype> declaration_specifiers
167 %type <ctype> enum_specifier
168 %type <ctype> pointer
169 %type <ctype> specifier_qualifier_list
170 %type <ctype> type_name
171 %type <ctype> struct_or_union
172 %type <ctype> struct_or_union_specifier
173 %type <ctype> type_specifier
174 %type <str> identifier
175 %type <str> typedef_name
176 %type <str> identifier_or_typedef_name
177 %type <symbol> abstract_declarator
178 %type <symbol> init_declarator
179 %type <symbol> declarator
180 %type <symbol> enumerator
181 %type <symbol> direct_abstract_declarator
182 %type <symbol> direct_declarator
183 %type <symbol> parameter_declaration
184 %type <symbol> struct_declarator
185 %type <list> enumerator_list
186 %type <list> identifier_list
187 %type <list> init_declarator_list
188 %type <list> parameter_list
189 %type <list> struct_declaration
190 %type <list> struct_declaration_list
191 %type <list> struct_declarator_list
192 %type <storage_class_specifier> storage_class_specifier
193 %type <type_qualifier> type_qualifier
194 %type <type_qualifier> type_qualifier_list
195 %type <function_specifier> function_specifier
196 %type <symbol> expression
197 %type <symbol> constant_expression
198 %type <symbol> conditional_expression
199 %type <symbol> logical_and_expression
200 %type <symbol> logical_or_expression
201 %type <symbol> inclusive_or_expression
202 %type <symbol> exclusive_or_expression
203 %type <symbol> multiplicative_expression
204 %type <symbol> additive_expression
205 %type <symbol> shift_expression
206 %type <symbol> relational_expression
207 %type <symbol> equality_expression
208 %type <symbol> and_expression
209 %type <symbol> cast_expression
210 %type <symbol> assignment_expression
211 %type <symbol> unary_expression
212 %type <symbol> postfix_expression
213 %type <symbol> primary_expression
214 %type <unary_operator> unary_operator
215 %type <str> function_macro
216 %type <str> object_macro
217 %type <symbol> strings
221 /* A.2.1 Expressions. */
226 $$ = g_hash_table_lookup (const_table, $1);
228 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, scanner->current_filename, lineno);
230 $$ = gi_source_symbol_ref ($$);
235 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_filename, lineno);
236 $$->const_int_set = TRUE;
237 if (g_str_has_prefix (yytext, "0x") && strlen (yytext) > 2) {
238 $$->const_int = g_ascii_strtoll (yytext + 2, NULL, 16);
239 } else if (g_str_has_prefix (yytext, "0") && strlen (yytext) > 1) {
240 $$->const_int = g_ascii_strtoll (yytext + 1, NULL, 8);
242 $$->const_int = g_ascii_strtoll (yytext, NULL, 10);
247 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_filename, lineno);
248 $$->const_int_set = TRUE;
249 $$->const_int = g_utf8_get_char(yytext + 1);
253 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_filename, lineno);
254 $$->const_double_set = TRUE;
255 $$->const_double = 0.0;
256 sscanf (yytext, "%lf", &($$->const_double));
263 | EXTENSION '(' '{' block_item_list '}' ')'
265 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, scanner->current_filename, lineno);
269 /* concatenate adjacent string literal tokens */
273 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_filename, lineno);
274 yytext[strlen (yytext) - 1] = '\0';
275 $$->const_string = parse_c_string_literal (yytext + 1);
276 if (!g_utf8_validate ($$->const_string, -1, NULL))
279 g_warning ("Ignoring non-UTF-8 constant string \"%s\"", yytext + 1);
281 g_free($$->const_string);
282 $$->const_string = NULL;
288 char *strings, *string2;
290 yytext[strlen (yytext) - 1] = '\0';
291 string2 = parse_c_string_literal (yytext + 1);
292 strings = g_strconcat ($$->const_string, string2, NULL);
293 g_free ($$->const_string);
295 $$->const_string = strings;
302 $$ = g_strdup (yytext);
306 identifier_or_typedef_name
313 | postfix_expression '[' expression ']'
315 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, scanner->current_filename, lineno);
317 | postfix_expression '(' argument_expression_list ')'
319 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, scanner->current_filename, lineno);
321 | postfix_expression '(' ')'
323 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, scanner->current_filename, lineno);
325 | postfix_expression '.' identifier_or_typedef_name
327 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, scanner->current_filename, lineno);
329 | postfix_expression ARROW identifier_or_typedef_name
331 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, scanner->current_filename, lineno);
333 | postfix_expression PLUSPLUS
335 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, scanner->current_filename, lineno);
337 | postfix_expression MINUSMINUS
339 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, scanner->current_filename, lineno);
343 argument_expression_list
344 : assignment_expression
345 | argument_expression_list ',' assignment_expression
350 | PLUSPLUS unary_expression
352 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, scanner->current_filename, lineno);
354 | MINUSMINUS unary_expression
356 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, scanner->current_filename, lineno);
358 | unary_operator cast_expression
366 $$->const_int = -$2->const_int;
368 case UNARY_BITWISE_COMPLEMENT:
370 $$->const_int = ~$2->const_int;
372 case UNARY_LOGICAL_NEGATION:
374 $$->const_int = !gi_source_symbol_get_const_boolean ($2);
377 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, scanner->current_filename, lineno);
381 | INTL_CONST '(' unary_expression ')'
384 if ($$->const_int_set) {
385 $$->base_type = gi_source_basic_type_new ("gint64");
388 | INTUL_CONST '(' unary_expression ')'
391 if ($$->const_int_set) {
392 $$->base_type = gi_source_basic_type_new ("guint64");
395 | SIZEOF unary_expression
397 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, scanner->current_filename, lineno);
399 | SIZEOF '(' type_name ')'
402 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, scanner->current_filename, lineno);
409 $$ = UNARY_ADDRESS_OF;
413 $$ = UNARY_POINTER_INDIRECTION;
425 $$ = UNARY_BITWISE_COMPLEMENT;
429 $$ = UNARY_LOGICAL_NEGATION;
435 | '(' type_name ')' cast_expression
438 if ($$->const_int_set || $$->const_double_set || $$->const_string != NULL) {
446 multiplicative_expression
448 | multiplicative_expression '*' cast_expression
450 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_filename, lineno);
451 $$->const_int_set = TRUE;
452 $$->const_int = $1->const_int * $3->const_int;
454 | multiplicative_expression '/' cast_expression
456 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_filename, lineno);
457 $$->const_int_set = TRUE;
458 if ($3->const_int != 0) {
459 $$->const_int = $1->const_int / $3->const_int;
462 | multiplicative_expression '%' cast_expression
464 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_filename, lineno);
465 $$->const_int_set = TRUE;
466 if ($3->const_int != 0) {
467 $$->const_int = $1->const_int % $3->const_int;
473 : multiplicative_expression
474 | additive_expression '+' multiplicative_expression
476 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_filename, lineno);
477 $$->const_int_set = TRUE;
478 $$->const_int = $1->const_int + $3->const_int;
480 | additive_expression '-' multiplicative_expression
482 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_filename, lineno);
483 $$->const_int_set = TRUE;
484 $$->const_int = $1->const_int - $3->const_int;
489 : additive_expression
490 | shift_expression SL additive_expression
492 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_filename, lineno);
493 $$->const_int_set = TRUE;
494 $$->const_int = $1->const_int << $3->const_int;
496 /* assume this is a bitfield/flags declaration
497 * if a left shift operator is sued in an enum value
498 * This mimics the glib-mkenum behavior.
502 | shift_expression SR additive_expression
504 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_filename, lineno);
505 $$->const_int_set = TRUE;
506 $$->const_int = $1->const_int >> $3->const_int;
510 relational_expression
512 | relational_expression '<' shift_expression
514 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_filename, lineno);
515 $$->const_int_set = TRUE;
516 $$->const_int = $1->const_int < $3->const_int;
518 | relational_expression '>' shift_expression
520 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_filename, lineno);
521 $$->const_int_set = TRUE;
522 $$->const_int = $1->const_int > $3->const_int;
524 | relational_expression LTEQ shift_expression
526 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_filename, lineno);
527 $$->const_int_set = TRUE;
528 $$->const_int = $1->const_int <= $3->const_int;
530 | relational_expression GTEQ shift_expression
532 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_filename, lineno);
533 $$->const_int_set = TRUE;
534 $$->const_int = $1->const_int >= $3->const_int;
539 : relational_expression
540 | equality_expression EQ relational_expression
542 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_filename, lineno);
543 $$->const_int_set = TRUE;
544 $$->const_int = $1->const_int == $3->const_int;
546 | equality_expression NOTEQ relational_expression
548 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_filename, lineno);
549 $$->const_int_set = TRUE;
550 $$->const_int = $1->const_int != $3->const_int;
555 : equality_expression
556 | and_expression '&' equality_expression
558 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_filename, lineno);
559 $$->const_int_set = TRUE;
560 $$->const_int = $1->const_int & $3->const_int;
564 exclusive_or_expression
566 | exclusive_or_expression '^' and_expression
568 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_filename, lineno);
569 $$->const_int_set = TRUE;
570 $$->const_int = $1->const_int ^ $3->const_int;
574 inclusive_or_expression
575 : exclusive_or_expression
576 | inclusive_or_expression '|' exclusive_or_expression
578 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_filename, lineno);
579 $$->const_int_set = TRUE;
580 $$->const_int = $1->const_int | $3->const_int;
584 logical_and_expression
585 : inclusive_or_expression
586 | logical_and_expression ANDAND inclusive_or_expression
588 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_filename, lineno);
589 $$->const_int_set = TRUE;
591 gi_source_symbol_get_const_boolean ($1) &&
592 gi_source_symbol_get_const_boolean ($3);
596 logical_or_expression
597 : logical_and_expression
598 | logical_or_expression OROR logical_and_expression
600 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_filename, lineno);
601 $$->const_int_set = TRUE;
603 gi_source_symbol_get_const_boolean ($1) ||
604 gi_source_symbol_get_const_boolean ($3);
608 conditional_expression
609 : logical_or_expression
610 | logical_or_expression '?' expression ':' expression
612 $$ = gi_source_symbol_get_const_boolean ($1) ? $3 : $5;
616 assignment_expression
617 : conditional_expression
618 | unary_expression assignment_operator assignment_expression
620 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, scanner->current_filename, lineno);
639 : assignment_expression
640 | expression ',' assignment_expression
641 | EXTENSION expression
643 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, scanner->current_filename, lineno);
648 : conditional_expression
651 /* A.2.2 Declarations. */
654 : declaration_specifiers init_declarator_list ';'
657 for (l = $2; l != NULL; l = l->next) {
658 GISourceSymbol *sym = l->data;
659 gi_source_symbol_merge_type (sym, gi_source_type_copy ($1));
660 if ($1->storage_class_specifier & STORAGE_CLASS_TYPEDEF) {
661 sym->type = CSYMBOL_TYPE_TYPEDEF;
662 } else if (sym->base_type->type == CTYPE_FUNCTION) {
663 sym->type = CSYMBOL_TYPE_FUNCTION;
665 sym->type = CSYMBOL_TYPE_OBJECT;
667 gi_source_scanner_add_symbol (scanner, sym);
668 gi_source_symbol_unref (sym);
672 | declaration_specifiers ';'
678 declaration_specifiers
679 : storage_class_specifier declaration_specifiers
682 $$->storage_class_specifier |= $1;
684 | storage_class_specifier
686 $$ = gi_source_type_new (CTYPE_INVALID);
687 $$->storage_class_specifier |= $1;
689 | type_specifier declaration_specifiers
692 /* combine basic types like unsigned int and long long */
693 if ($$->type == CTYPE_BASIC_TYPE && $2->type == CTYPE_BASIC_TYPE) {
694 char *name = g_strdup_printf ("%s %s", $$->name, $2->name);
703 | type_qualifier declaration_specifiers
706 $$->type_qualifier |= $1;
710 $$ = gi_source_type_new (CTYPE_INVALID);
711 $$->type_qualifier |= $1;
713 | function_specifier declaration_specifiers
716 $$->function_specifier |= $1;
720 $$ = gi_source_type_new (CTYPE_INVALID);
721 $$->function_specifier |= $1;
728 $$ = g_list_append (NULL, $1);
730 | init_declarator_list ',' init_declarator
732 $$ = g_list_append ($1, $3);
738 | declarator '=' initializer
741 storage_class_specifier
744 $$ = STORAGE_CLASS_TYPEDEF;
748 $$ = STORAGE_CLASS_EXTERN;
752 $$ = STORAGE_CLASS_STATIC;
756 $$ = STORAGE_CLASS_AUTO;
760 $$ = STORAGE_CLASS_REGISTER;
767 $$ = gi_source_type_new (CTYPE_VOID);
771 $$ = gi_source_basic_type_new ("char");
775 $$ = gi_source_basic_type_new ("short");
779 $$ = gi_source_basic_type_new ("int");
783 $$ = gi_source_basic_type_new ("long");
787 $$ = gi_source_basic_type_new ("float");
791 $$ = gi_source_basic_type_new ("double");
795 $$ = gi_source_basic_type_new ("signed");
799 $$ = gi_source_basic_type_new ("unsigned");
803 $$ = gi_source_basic_type_new ("bool");
805 | struct_or_union_specifier
809 $$ = gi_source_typedef_new ($1);
814 struct_or_union_specifier
815 : struct_or_union identifier_or_typedef_name '{' struct_declaration_list '}'
822 sym = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, scanner->current_filename, lineno);
823 if ($$->type == CTYPE_STRUCT) {
824 sym->type = CSYMBOL_TYPE_STRUCT;
825 } else if ($$->type == CTYPE_UNION) {
826 sym->type = CSYMBOL_TYPE_UNION;
828 g_assert_not_reached ();
830 sym->ident = g_strdup ($$->name);
831 sym->base_type = gi_source_type_copy ($$);
832 gi_source_scanner_add_symbol (scanner, sym);
833 gi_source_symbol_unref (sym);
835 | struct_or_union '{' struct_declaration_list '}'
840 | struct_or_union identifier_or_typedef_name
850 scanner->private = FALSE;
851 $$ = gi_source_struct_new (NULL);
855 scanner->private = FALSE;
856 $$ = gi_source_union_new (NULL);
860 struct_declaration_list
862 | struct_declaration_list struct_declaration
864 $$ = g_list_concat ($1, $2);
869 : specifier_qualifier_list struct_declarator_list ';'
873 for (l = $2; l != NULL; l = l->next)
875 GISourceSymbol *sym = l->data;
876 if ($1->storage_class_specifier & STORAGE_CLASS_TYPEDEF)
877 sym->type = CSYMBOL_TYPE_TYPEDEF;
879 sym->type = CSYMBOL_TYPE_MEMBER;
880 gi_source_symbol_merge_type (sym, gi_source_type_copy ($1));
881 sym->private = scanner->private;
882 $$ = g_list_append ($$, sym);
888 specifier_qualifier_list
889 : type_specifier specifier_qualifier_list
895 | type_qualifier specifier_qualifier_list
898 $$->type_qualifier |= $1;
902 $$ = gi_source_type_new (CTYPE_INVALID);
903 $$->type_qualifier |= $1;
907 struct_declarator_list
910 $$ = g_list_append (NULL, $1);
912 | struct_declarator_list ',' struct_declarator
914 $$ = g_list_append ($1, $3);
919 : /* empty, support for anonymous structs and unions */
921 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, scanner->current_filename, lineno);
924 | ':' constant_expression
926 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, scanner->current_filename, lineno);
928 | declarator ':' constant_expression
931 if ($3->const_int_set) {
932 $$->const_int_set = TRUE;
933 $$->const_int = $3->const_int;
939 : enum_keyword identifier_or_typedef_name '{' enumerator_list '}'
941 $$ = gi_source_enum_new ($2);
943 $$->is_bitfield = is_bitfield || scanner->flags;
944 last_enum_value = -1;
946 | enum_keyword '{' enumerator_list '}'
948 $$ = gi_source_enum_new (NULL);
950 $$->is_bitfield = is_bitfield || scanner->flags;
951 last_enum_value = -1;
953 | enum_keyword identifier_or_typedef_name '{' enumerator_list ',' '}'
955 $$ = gi_source_enum_new ($2);
957 $$->is_bitfield = is_bitfield || scanner->flags;
958 last_enum_value = -1;
960 | enum_keyword '{' enumerator_list ',' '}'
962 $$ = gi_source_enum_new (NULL);
964 $$->is_bitfield = is_bitfield || scanner->flags;
965 last_enum_value = -1;
967 | enum_keyword identifier_or_typedef_name
969 $$ = gi_source_enum_new ($2);
976 scanner->flags = FALSE;
977 scanner->private = FALSE;
984 /* reset flag before the first enum value */
989 $2->private = scanner->private;
990 $$ = g_list_append (NULL, $2);
992 | enumerator_list ',' enumerator
994 $3->private = scanner->private;
995 $$ = g_list_append ($1, $3);
1002 $$ = gi_source_symbol_new (CSYMBOL_TYPE_OBJECT, scanner->current_filename, lineno);
1004 $$->const_int_set = TRUE;
1005 $$->const_int = ++last_enum_value;
1006 g_hash_table_insert (const_table, g_strdup ($$->ident), gi_source_symbol_ref ($$));
1008 | identifier '=' constant_expression
1010 $$ = gi_source_symbol_new (CSYMBOL_TYPE_OBJECT, scanner->current_filename, lineno);
1012 $$->const_int_set = TRUE;
1013 $$->const_int = $3->const_int;
1014 last_enum_value = $$->const_int;
1015 g_hash_table_insert (const_table, g_strdup ($$->ident), gi_source_symbol_ref ($$));
1022 $$ = TYPE_QUALIFIER_CONST;
1026 $$ = TYPE_QUALIFIER_RESTRICT;
1030 $$ = TYPE_QUALIFIER_EXTENSION;
1034 $$ = TYPE_QUALIFIER_VOLATILE;
1041 $$ = FUNCTION_INLINE;
1046 : pointer direct_declarator
1049 gi_source_symbol_merge_type ($$, $1);
1057 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, scanner->current_filename, lineno);
1060 | '(' declarator ')'
1064 | direct_declarator '[' assignment_expression ']'
1067 gi_source_symbol_merge_type ($$, gi_source_array_new ($3));
1069 | direct_declarator '[' ']'
1072 gi_source_symbol_merge_type ($$, gi_source_array_new (NULL));
1074 | direct_declarator '(' parameter_list ')'
1076 GISourceType *func = gi_source_function_new ();
1077 // ignore (void) parameter list
1078 if ($3 != NULL && ($3->next != NULL || ((GISourceSymbol *) $3->data)->base_type->type != CTYPE_VOID)) {
1079 func->child_list = $3;
1082 gi_source_symbol_merge_type ($$, func);
1084 | direct_declarator '(' identifier_list ')'
1086 GISourceType *func = gi_source_function_new ();
1087 func->child_list = $3;
1089 gi_source_symbol_merge_type ($$, func);
1091 | direct_declarator '(' ')'
1093 GISourceType *func = gi_source_function_new ();
1095 gi_source_symbol_merge_type ($$, func);
1100 : '*' type_qualifier_list
1102 $$ = gi_source_pointer_new (NULL);
1103 $$->type_qualifier = $2;
1107 $$ = gi_source_pointer_new (NULL);
1109 | '*' type_qualifier_list pointer
1111 GISourceType **base = &($3->base_type);
1113 while (*base != NULL) {
1114 base = &((*base)->base_type);
1116 *base = gi_source_pointer_new (NULL);
1117 (*base)->type_qualifier = $2;
1122 GISourceType **base = &($2->base_type);
1124 while (*base != NULL) {
1125 base = &((*base)->base_type);
1127 *base = gi_source_pointer_new (NULL);
1134 | type_qualifier_list type_qualifier
1141 : parameter_declaration
1143 $$ = g_list_append (NULL, $1);
1145 | parameter_list ',' parameter_declaration
1147 $$ = g_list_append ($1, $3);
1151 parameter_declaration
1152 : declaration_specifiers declarator
1155 gi_source_symbol_merge_type ($$, $1);
1157 | declaration_specifiers abstract_declarator
1160 gi_source_symbol_merge_type ($$, $1);
1162 | declaration_specifiers
1164 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, scanner->current_filename, lineno);
1169 $$ = gi_source_symbol_new (CSYMBOL_TYPE_ELLIPSIS, scanner->current_filename, lineno);
1176 GISourceSymbol *sym = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, scanner->current_filename, lineno);
1178 $$ = g_list_append (NULL, sym);
1180 | identifier_list ',' identifier
1182 GISourceSymbol *sym = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, scanner->current_filename, lineno);
1184 $$ = g_list_append ($1, sym);
1189 : specifier_qualifier_list
1190 | specifier_qualifier_list abstract_declarator
1196 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, scanner->current_filename, lineno);
1197 gi_source_symbol_merge_type ($$, $1);
1199 | direct_abstract_declarator
1200 | pointer direct_abstract_declarator
1203 gi_source_symbol_merge_type ($$, $1);
1207 direct_abstract_declarator
1208 : '(' abstract_declarator ')'
1214 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, scanner->current_filename, lineno);
1215 gi_source_symbol_merge_type ($$, gi_source_array_new (NULL));
1217 | '[' assignment_expression ']'
1219 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, scanner->current_filename, lineno);
1220 gi_source_symbol_merge_type ($$, gi_source_array_new ($2));
1222 | direct_abstract_declarator '[' ']'
1225 gi_source_symbol_merge_type ($$, gi_source_array_new (NULL));
1227 | direct_abstract_declarator '[' assignment_expression ']'
1230 gi_source_symbol_merge_type ($$, gi_source_array_new ($3));
1234 GISourceType *func = gi_source_function_new ();
1235 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, scanner->current_filename, lineno);
1236 gi_source_symbol_merge_type ($$, func);
1238 | '(' parameter_list ')'
1240 GISourceType *func = gi_source_function_new ();
1241 // ignore (void) parameter list
1242 if ($2 != NULL && ($2->next != NULL || ((GISourceSymbol *) $2->data)->base_type->type != CTYPE_VOID)) {
1243 func->child_list = $2;
1245 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, scanner->current_filename, lineno);
1246 gi_source_symbol_merge_type ($$, func);
1248 | direct_abstract_declarator '(' ')'
1250 GISourceType *func = gi_source_function_new ();
1252 gi_source_symbol_merge_type ($$, func);
1254 | direct_abstract_declarator '(' parameter_list ')'
1256 GISourceType *func = gi_source_function_new ();
1257 // ignore (void) parameter list
1258 if ($3 != NULL && ($3->next != NULL || ((GISourceSymbol *) $3->data)->base_type->type != CTYPE_VOID)) {
1259 func->child_list = $3;
1262 gi_source_symbol_merge_type ($$, func);
1269 $$ = g_strdup (yytext);
1274 : assignment_expression
1275 | '{' initializer_list '}'
1276 | '{' initializer_list ',' '}'
1281 | initializer_list ',' initializer
1284 /* A.2.3 Statements. */
1288 | compound_statement
1289 | expression_statement
1290 | selection_statement
1291 | iteration_statement
1296 : identifier_or_typedef_name ':' statement
1297 | CASE constant_expression ':' statement
1298 | DEFAULT ':' statement
1303 | '{' block_item_list '}'
1308 | block_item_list block_item
1316 expression_statement
1322 : IF '(' expression ')' statement
1323 | IF '(' expression ')' statement ELSE statement
1324 | SWITCH '(' expression ')' statement
1328 : WHILE '(' expression ')' statement
1329 | DO statement WHILE '(' expression ')' ';'
1330 | FOR '(' ';' ';' ')' statement
1331 | FOR '(' expression ';' ';' ')' statement
1332 | FOR '(' ';' expression ';' ')' statement
1333 | FOR '(' expression ';' expression ';' ')' statement
1334 | FOR '(' ';' ';' expression ')' statement
1335 | FOR '(' expression ';' ';' expression ')' statement
1336 | FOR '(' ';' expression ';' expression ')' statement
1337 | FOR '(' expression ';' expression ';' expression ')' statement
1341 : GOTO identifier_or_typedef_name ';'
1345 | RETURN expression ';'
1348 /* A.2.4 External definitions. */
1351 : external_declaration
1352 | translation_unit external_declaration
1355 external_declaration
1356 : function_definition
1362 : declaration_specifiers declarator declaration_list compound_statement
1363 | declaration_specifiers declarator compound_statement
1368 | declaration_list declaration
1376 $$ = g_strdup (yytext + strlen ("#define "));
1383 $$ = g_strdup (yytext + strlen ("#define "));
1387 function_macro_define
1388 : function_macro '(' identifier_list ')'
1392 : object_macro constant_expression
1394 if ($2->const_int_set || $2->const_double_set || $2->const_string != NULL) {
1396 gi_source_scanner_add_symbol (scanner, $2);
1397 gi_source_symbol_unref ($2);
1403 : function_macro_define
1404 | object_macro_define
1410 yyerror (GISourceScanner *scanner, const char *s)
1412 /* ignore errors while doing a macro scan as not all object macros
1413 * have valid expressions */
1414 if (!scanner->macro_scan)
1416 fprintf(stderr, "%s:%d: %s in '%s' at '%s'\n",
1417 scanner->current_filename, lineno, s, linebuf, yytext);
1422 eat_hspace (FILE * f)
1429 while (c == ' ' || c == '\t');
1434 eat_line (FILE * f, int c)
1436 while (c != EOF && c != '\n')
1443 if (c == ' ' || c == '\t')
1452 read_identifier (FILE * f, int c, char **identifier)
1454 GString *id = g_string_new ("");
1455 while (g_ascii_isalnum (c) || c == '_')
1457 g_string_append_c (id, c);
1460 *identifier = g_string_free (id, FALSE);
1465 gi_source_scanner_parse_macros (GISourceScanner *scanner, GList *filenames)
1467 GError *error = NULL;
1468 char *tmp_name = NULL;
1470 fdopen (g_file_open_tmp ("gen-introspect-XXXXXX.h", &tmp_name, &error),
1473 g_unlink (tmp_name);
1475 for (l = filenames; l != NULL; l = l->next)
1477 FILE *f = fopen (l->data, "r");
1480 GString *define_line;
1482 gboolean error_line = FALSE;
1483 int c = eat_hspace (f);
1489 c = eat_line (f, c);
1494 /* print current location */
1495 str = g_strescape (l->data, "");
1496 fprintf (fmacros, "# %d \"%s\"\n", line, str);
1500 c = read_identifier (f, c, &str);
1501 if (strcmp (str, "define") != 0 || (c != ' ' && c != '\t'))
1505 c = eat_line (f, c);
1511 c = read_identifier (f, c, &str);
1512 if (strlen (str) == 0 || (c != ' ' && c != '\t' && c != '('))
1516 c = eat_line (f, c);
1520 define_line = g_string_new ("#define ");
1521 g_string_append (define_line, str);
1527 g_string_append_c (define_line, c);
1529 if (c == EOF || c == '\n')
1537 g_string_free (define_line, TRUE);
1539 c = eat_line (f, c);
1544 g_assert (c == ')');
1545 g_string_append_c (define_line, c);
1548 /* found function-like macro */
1549 fprintf (fmacros, "%s\n", define_line->str);
1551 g_string_free (define_line, TRUE);
1552 /* ignore rest of line */
1553 c = eat_line (f, c);
1557 if (c != ' ' && c != '\t')
1559 g_string_free (define_line, TRUE);
1561 c = eat_line (f, c);
1565 while (c != EOF && c != '\n')
1567 g_string_append_c (define_line, c);
1574 /* fold lines when seeing backslash new-line sequence */
1579 g_string_append_c (define_line, '\\');
1584 /* found object-like macro */
1585 fprintf (fmacros, "%s\n", define_line->str);
1587 c = eat_line (f, c);
1595 gi_source_scanner_parse_file (scanner, fmacros);
1599 gi_source_scanner_parse_file (GISourceScanner *scanner, FILE *file)
1601 g_return_val_if_fail (file != NULL, FALSE);
1603 const_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1604 g_free, (GDestroyNotify)gi_source_symbol_unref);
1610 g_hash_table_destroy (const_table);
1619 gi_source_scanner_lex_filename (GISourceScanner *scanner, const gchar *filename)
1622 yyin = fopen (filename, "r");
1624 while (yylex (scanner) != YYEOF)