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"
43 extern int yylex (GISourceScanner *scanner);
44 static void yyerror (GISourceScanner *scanner, const char *str);
46 extern void ctype_free (GISourceType * type);
48 static int last_enum_value = -1;
49 static gboolean is_bitfield;
50 static GHashTable *const_table = NULL;
57 GISourceSymbol *symbol;
59 StorageClassSpecifier storage_class_specifier;
60 TypeQualifier type_qualifier;
61 FunctionSpecifier function_specifier;
62 UnaryOperator unary_operator;
65 %parse-param { GISourceScanner* scanner }
66 %lex-param { GISourceScanner* scanner }
68 %token <str> IDENTIFIER "identifier"
69 %token <str> TYPEDEF_NAME "typedef-name"
71 %token INTEGER FLOATING CHARACTER STRING
73 %token ELLIPSIS ADDEQ SUBEQ MULEQ DIVEQ MODEQ XOREQ ANDEQ OREQ SL SR
74 %token SLEQ SREQ EQ NOTEQ LTEQ GTEQ ANDAND OROR PLUSPLUS MINUSMINUS ARROW
76 %token AUTO BOOL BREAK CASE CHAR CONST CONTINUE DEFAULT DO DOUBLE ELSE ENUM
77 %token EXTENSION EXTERN FLOAT FOR GOTO IF INLINE INT LONG REGISTER RESTRICT
78 %token RETURN SHORT SIGNED SIZEOF STATIC STRUCT SWITCH TYPEDEF UNION UNSIGNED
79 %token VOID VOLATILE WHILE
81 %token FUNCTION_MACRO OBJECT_MACRO
83 %start translation_unit
85 %type <ctype> declaration_specifiers
86 %type <ctype> enum_specifier
88 %type <ctype> specifier_qualifier_list
89 %type <ctype> type_name
90 %type <ctype> struct_or_union
91 %type <ctype> struct_or_union_specifier
92 %type <ctype> type_specifier
93 %type <str> identifier
94 %type <str> typedef_name
95 %type <str> identifier_or_typedef_name
96 %type <symbol> abstract_declarator
97 %type <symbol> init_declarator
98 %type <symbol> declarator
99 %type <symbol> enumerator
100 %type <symbol> direct_abstract_declarator
101 %type <symbol> direct_declarator
102 %type <symbol> parameter_declaration
103 %type <symbol> struct_declarator
104 %type <list> enumerator_list
105 %type <list> identifier_list
106 %type <list> init_declarator_list
107 %type <list> parameter_list
108 %type <list> struct_declaration
109 %type <list> struct_declaration_list
110 %type <list> struct_declarator_list
111 %type <storage_class_specifier> storage_class_specifier
112 %type <type_qualifier> type_qualifier
113 %type <type_qualifier> type_qualifier_list
114 %type <function_specifier> function_specifier
115 %type <symbol> expression
116 %type <symbol> constant_expression
117 %type <symbol> conditional_expression
118 %type <symbol> logical_and_expression
119 %type <symbol> logical_or_expression
120 %type <symbol> inclusive_or_expression
121 %type <symbol> exclusive_or_expression
122 %type <symbol> multiplicative_expression
123 %type <symbol> additive_expression
124 %type <symbol> shift_expression
125 %type <symbol> relational_expression
126 %type <symbol> equality_expression
127 %type <symbol> and_expression
128 %type <symbol> cast_expression
129 %type <symbol> assignment_expression
130 %type <symbol> unary_expression
131 %type <symbol> postfix_expression
132 %type <symbol> primary_expression
133 %type <unary_operator> unary_operator
134 %type <str> function_macro
135 %type <str> object_macro
136 %type <symbol> strings
140 /* A.2.1 Expressions. */
145 $$ = g_hash_table_lookup (const_table, $1);
147 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
149 $$ = gi_source_symbol_ref ($$);
154 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
155 $$->const_int_set = TRUE;
156 if (g_str_has_prefix (yytext, "0x") && strlen (yytext) > 2) {
157 $$->const_int = strtol (yytext + 2, NULL, 16);
158 } else if (g_str_has_prefix (yytext, "0") && strlen (yytext) > 1) {
159 $$->const_int = strtol (yytext + 1, NULL, 8);
161 $$->const_int = atoi (yytext);
166 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
170 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
179 /* concatenate adjacent string literal tokens */
183 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
184 yytext[strlen (yytext) - 1] = '\0';
185 $$->const_string = g_strcompress (yytext + 1);
186 if (!g_utf8_validate ($$->const_string, -1, NULL))
188 g_warning ("Ignoring non-UTF-8 constant string \"%s\"", yytext + 1);
189 g_free($$->const_string);
190 $$->const_string = NULL;
196 char *strings, *string2;
198 yytext[strlen (yytext) - 1] = '\0';
199 string2 = g_strcompress (yytext + 1);
200 strings = g_strconcat ($$->const_string, string2, NULL);
201 g_free ($$->const_string);
203 $$->const_string = strings;
210 $$ = g_strdup (yytext);
214 identifier_or_typedef_name
221 | postfix_expression '[' expression ']'
223 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
225 | postfix_expression '(' argument_expression_list ')'
227 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
229 | postfix_expression '(' ')'
231 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
233 | postfix_expression '.' identifier_or_typedef_name
235 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
237 | postfix_expression ARROW identifier_or_typedef_name
239 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
241 | postfix_expression PLUSPLUS
243 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
245 | postfix_expression MINUSMINUS
247 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
251 argument_expression_list
252 : assignment_expression
253 | argument_expression_list ',' assignment_expression
258 | PLUSPLUS unary_expression
260 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
262 | MINUSMINUS unary_expression
264 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
266 | unary_operator cast_expression
274 $$->const_int = -$2->const_int;
276 case UNARY_BITWISE_COMPLEMENT:
278 $$->const_int = ~$2->const_int;
280 case UNARY_LOGICAL_NEGATION:
282 $$->const_int = !gi_source_symbol_get_const_boolean ($2);
285 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
289 | SIZEOF unary_expression
291 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
293 | SIZEOF '(' type_name ')'
296 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
303 $$ = UNARY_ADDRESS_OF;
307 $$ = UNARY_POINTER_INDIRECTION;
319 $$ = UNARY_BITWISE_COMPLEMENT;
323 $$ = UNARY_LOGICAL_NEGATION;
329 | '(' type_name ')' cast_expression
336 multiplicative_expression
338 | multiplicative_expression '*' cast_expression
340 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
341 $$->const_int_set = TRUE;
342 $$->const_int = $1->const_int * $3->const_int;
344 | multiplicative_expression '/' cast_expression
346 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
347 $$->const_int_set = TRUE;
348 if ($3->const_int != 0) {
349 $$->const_int = $1->const_int / $3->const_int;
352 | multiplicative_expression '%' cast_expression
354 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
355 $$->const_int_set = TRUE;
356 if ($3->const_int != 0) {
357 $$->const_int = $1->const_int % $3->const_int;
363 : multiplicative_expression
364 | additive_expression '+' multiplicative_expression
366 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
367 $$->const_int_set = TRUE;
368 $$->const_int = $1->const_int + $3->const_int;
370 | additive_expression '-' multiplicative_expression
372 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
373 $$->const_int_set = TRUE;
374 $$->const_int = $1->const_int - $3->const_int;
379 : additive_expression
380 | shift_expression SL additive_expression
382 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
383 $$->const_int_set = TRUE;
384 $$->const_int = $1->const_int << $3->const_int;
386 /* assume this is a bitfield/flags declaration
387 * if a left shift operator is sued in an enum value
388 * This mimics the glib-mkenum behavior.
392 | shift_expression SR additive_expression
394 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
395 $$->const_int_set = TRUE;
396 $$->const_int = $1->const_int >> $3->const_int;
400 relational_expression
402 | relational_expression '<' shift_expression
404 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
405 $$->const_int_set = TRUE;
406 $$->const_int = $1->const_int < $3->const_int;
408 | relational_expression '>' shift_expression
410 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
411 $$->const_int_set = TRUE;
412 $$->const_int = $1->const_int > $3->const_int;
414 | relational_expression LTEQ shift_expression
416 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
417 $$->const_int_set = TRUE;
418 $$->const_int = $1->const_int <= $3->const_int;
420 | relational_expression GTEQ shift_expression
422 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
423 $$->const_int_set = TRUE;
424 $$->const_int = $1->const_int >= $3->const_int;
429 : relational_expression
430 | equality_expression EQ relational_expression
432 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
433 $$->const_int_set = TRUE;
434 $$->const_int = $1->const_int == $3->const_int;
436 | equality_expression NOTEQ relational_expression
438 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
439 $$->const_int_set = TRUE;
440 $$->const_int = $1->const_int != $3->const_int;
445 : equality_expression
446 | and_expression '&' equality_expression
448 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
449 $$->const_int_set = TRUE;
450 $$->const_int = $1->const_int & $3->const_int;
454 exclusive_or_expression
456 | exclusive_or_expression '^' and_expression
458 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
459 $$->const_int_set = TRUE;
460 $$->const_int = $1->const_int ^ $3->const_int;
464 inclusive_or_expression
465 : exclusive_or_expression
466 | inclusive_or_expression '|' exclusive_or_expression
468 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
469 $$->const_int_set = TRUE;
470 $$->const_int = $1->const_int | $3->const_int;
474 logical_and_expression
475 : inclusive_or_expression
476 | logical_and_expression ANDAND inclusive_or_expression
478 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
479 $$->const_int_set = TRUE;
481 gi_source_symbol_get_const_boolean ($1) &&
482 gi_source_symbol_get_const_boolean ($3);
486 logical_or_expression
487 : logical_and_expression
488 | logical_or_expression OROR logical_and_expression
490 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
491 $$->const_int_set = TRUE;
493 gi_source_symbol_get_const_boolean ($1) ||
494 gi_source_symbol_get_const_boolean ($3);
498 conditional_expression
499 : logical_or_expression
500 | logical_or_expression '?' expression ':' conditional_expression
502 $$ = gi_source_symbol_get_const_boolean ($1) ? $3 : $5;
506 assignment_expression
507 : conditional_expression
508 | unary_expression assignment_operator assignment_expression
510 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
529 : assignment_expression
530 | expression ',' assignment_expression
532 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
537 : conditional_expression
540 /* A.2.2 Declarations. */
543 : declaration_specifiers init_declarator_list ';'
546 for (l = $2; l != NULL; l = l->next) {
547 GISourceSymbol *sym = l->data;
548 gi_source_symbol_merge_type (sym, gi_source_type_copy ($1));
549 if ($1->storage_class_specifier & STORAGE_CLASS_TYPEDEF) {
550 sym->type = CSYMBOL_TYPE_TYPEDEF;
551 } else if (sym->base_type->type == CTYPE_FUNCTION) {
552 sym->type = CSYMBOL_TYPE_FUNCTION;
554 sym->type = CSYMBOL_TYPE_OBJECT;
556 gi_source_scanner_add_symbol (scanner, sym);
557 gi_source_symbol_unref (sym);
561 | declaration_specifiers ';'
567 declaration_specifiers
568 : storage_class_specifier declaration_specifiers
571 $$->storage_class_specifier |= $1;
573 | storage_class_specifier
575 $$ = gi_source_type_new (CTYPE_INVALID);
576 $$->storage_class_specifier |= $1;
578 | type_specifier declaration_specifiers
581 /* combine basic types like unsigned int and long long */
582 if ($$->type == CTYPE_BASIC_TYPE && $2->type == CTYPE_BASIC_TYPE) {
583 char *name = g_strdup_printf ("%s %s", $$->name, $2->name);
592 | type_qualifier declaration_specifiers
595 $$->type_qualifier |= $1;
599 $$ = gi_source_type_new (CTYPE_INVALID);
600 $$->type_qualifier |= $1;
602 | function_specifier declaration_specifiers
605 $$->function_specifier |= $1;
609 $$ = gi_source_type_new (CTYPE_INVALID);
610 $$->function_specifier |= $1;
617 $$ = g_list_append (NULL, $1);
619 | init_declarator_list ',' init_declarator
621 $$ = g_list_append ($1, $3);
627 | declarator '=' initializer
630 storage_class_specifier
633 $$ = STORAGE_CLASS_TYPEDEF;
637 $$ = STORAGE_CLASS_EXTERN;
641 $$ = STORAGE_CLASS_STATIC;
645 $$ = STORAGE_CLASS_AUTO;
649 $$ = STORAGE_CLASS_REGISTER;
656 $$ = gi_source_type_new (CTYPE_VOID);
660 $$ = gi_source_basic_type_new ("char");
664 $$ = gi_source_basic_type_new ("short");
668 $$ = gi_source_basic_type_new ("int");
672 $$ = gi_source_basic_type_new ("long");
676 $$ = gi_source_basic_type_new ("float");
680 $$ = gi_source_basic_type_new ("double");
684 $$ = gi_source_basic_type_new ("signed");
688 $$ = gi_source_basic_type_new ("unsigned");
692 $$ = gi_source_basic_type_new ("bool");
694 | struct_or_union_specifier
698 $$ = gi_source_typedef_new ($1);
703 struct_or_union_specifier
704 : struct_or_union identifier_or_typedef_name '{' struct_declaration_list '}'
710 GISourceSymbol *sym = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
711 if ($$->type == CTYPE_STRUCT) {
712 sym->type = CSYMBOL_TYPE_STRUCT;
713 } else if ($$->type == CTYPE_UNION) {
714 sym->type = CSYMBOL_TYPE_UNION;
716 g_assert_not_reached ();
718 sym->ident = g_strdup ($$->name);
719 sym->base_type = gi_source_type_copy ($$);
720 gi_source_scanner_add_symbol (scanner, sym);
721 gi_source_symbol_unref (sym);
723 | struct_or_union '{' struct_declaration_list '}'
728 | struct_or_union identifier_or_typedef_name
738 $$ = gi_source_struct_new (NULL);
742 $$ = gi_source_union_new (NULL);
746 struct_declaration_list
748 | struct_declaration_list struct_declaration
750 $$ = g_list_concat ($1, $2);
755 : specifier_qualifier_list struct_declarator_list ';'
759 for (l = $2; l != NULL; l = l->next)
761 GISourceSymbol *sym = l->data;
762 if ($1->storage_class_specifier & STORAGE_CLASS_TYPEDEF)
763 sym->type = CSYMBOL_TYPE_TYPEDEF;
765 sym->type = CSYMBOL_TYPE_MEMBER;
766 gi_source_symbol_merge_type (sym, gi_source_type_copy ($1));
767 $$ = g_list_append ($$, sym);
773 specifier_qualifier_list
774 : type_specifier specifier_qualifier_list
780 | type_qualifier specifier_qualifier_list
783 $$->type_qualifier |= $1;
787 $$ = gi_source_type_new (CTYPE_INVALID);
788 $$->type_qualifier |= $1;
792 struct_declarator_list
795 $$ = g_list_append (NULL, $1);
797 | struct_declarator_list ',' struct_declarator
799 $$ = g_list_append ($1, $3);
804 : /* empty, support for anonymous structs and unions */
806 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
809 | ':' constant_expression
811 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
813 | declarator ':' constant_expression
816 if ($3->const_int_set) {
817 $$->const_int_set = TRUE;
818 $$->const_int = $3->const_int;
824 : ENUM identifier_or_typedef_name '{' enumerator_list '}'
826 $$ = gi_source_enum_new ($2);
828 $$->is_bitfield = is_bitfield;
829 last_enum_value = -1;
831 | ENUM '{' enumerator_list '}'
833 $$ = gi_source_enum_new (NULL);
835 $$->is_bitfield = is_bitfield;
836 last_enum_value = -1;
838 | ENUM identifier_or_typedef_name '{' enumerator_list ',' '}'
840 $$ = gi_source_enum_new ($2);
842 $$->is_bitfield = is_bitfield;
843 last_enum_value = -1;
845 | ENUM '{' enumerator_list ',' '}'
847 $$ = gi_source_enum_new (NULL);
849 $$->is_bitfield = is_bitfield;
850 last_enum_value = -1;
852 | ENUM identifier_or_typedef_name
854 $$ = gi_source_enum_new ($2);
861 /* reset flag before the first enum value */
866 $$ = g_list_append (NULL, $2);
868 | enumerator_list ',' enumerator
870 $$ = g_list_append ($1, $3);
877 $$ = gi_source_symbol_new (CSYMBOL_TYPE_OBJECT);
879 $$->const_int_set = TRUE;
880 $$->const_int = ++last_enum_value;
881 g_hash_table_insert (const_table, g_strdup ($$->ident), gi_source_symbol_ref ($$));
883 | identifier '=' constant_expression
885 $$ = gi_source_symbol_new (CSYMBOL_TYPE_OBJECT);
887 $$->const_int_set = TRUE;
888 $$->const_int = $3->const_int;
889 last_enum_value = $$->const_int;
890 g_hash_table_insert (const_table, g_strdup ($$->ident), gi_source_symbol_ref ($$));
897 $$ = TYPE_QUALIFIER_CONST;
901 $$ = TYPE_QUALIFIER_RESTRICT;
905 $$ = TYPE_QUALIFIER_EXTENSION;
909 $$ = TYPE_QUALIFIER_VOLATILE;
916 $$ = FUNCTION_INLINE;
921 : pointer direct_declarator
924 gi_source_symbol_merge_type ($$, $1);
932 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
939 | direct_declarator '[' assignment_expression ']'
942 gi_source_symbol_merge_type ($$, gi_source_array_new ($3));
944 | direct_declarator '[' ']'
947 gi_source_symbol_merge_type ($$, gi_source_array_new (NULL));
949 | direct_declarator '(' parameter_list ')'
951 GISourceType *func = gi_source_function_new ();
952 // ignore (void) parameter list
953 if ($3 != NULL && ($3->next != NULL || ((GISourceSymbol *) $3->data)->base_type->type != CTYPE_VOID)) {
954 func->child_list = $3;
957 gi_source_symbol_merge_type ($$, func);
959 | direct_declarator '(' identifier_list ')'
961 GISourceType *func = gi_source_function_new ();
962 func->child_list = $3;
964 gi_source_symbol_merge_type ($$, func);
966 | direct_declarator '(' ')'
968 GISourceType *func = gi_source_function_new ();
970 gi_source_symbol_merge_type ($$, func);
975 : '*' type_qualifier_list
977 $$ = gi_source_pointer_new (NULL);
978 $$->type_qualifier = $2;
982 $$ = gi_source_pointer_new (NULL);
984 | '*' type_qualifier_list pointer
986 $$ = gi_source_pointer_new ($3);
987 $$->type_qualifier = $2;
991 $$ = gi_source_pointer_new ($2);
997 | type_qualifier_list type_qualifier
1004 : parameter_declaration
1006 $$ = g_list_append (NULL, $1);
1008 | parameter_list ',' parameter_declaration
1010 $$ = g_list_append ($1, $3);
1014 parameter_declaration
1015 : declaration_specifiers declarator
1018 gi_source_symbol_merge_type ($$, $1);
1020 | declaration_specifiers abstract_declarator
1023 gi_source_symbol_merge_type ($$, $1);
1025 | declaration_specifiers
1027 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
1032 $$ = gi_source_symbol_new (CSYMBOL_TYPE_ELLIPSIS);
1039 GISourceSymbol *sym = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
1041 $$ = g_list_append (NULL, sym);
1043 | identifier_list ',' identifier
1045 GISourceSymbol *sym = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
1047 $$ = g_list_append ($1, sym);
1052 : specifier_qualifier_list
1053 | specifier_qualifier_list abstract_declarator
1059 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
1060 gi_source_symbol_merge_type ($$, $1);
1062 | direct_abstract_declarator
1063 | pointer direct_abstract_declarator
1066 gi_source_symbol_merge_type ($$, $1);
1070 direct_abstract_declarator
1071 : '(' abstract_declarator ')'
1077 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
1078 gi_source_symbol_merge_type ($$, gi_source_array_new (NULL));
1080 | '[' assignment_expression ']'
1082 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
1083 gi_source_symbol_merge_type ($$, gi_source_array_new ($2));
1085 | direct_abstract_declarator '[' ']'
1088 gi_source_symbol_merge_type ($$, gi_source_array_new (NULL));
1090 | direct_abstract_declarator '[' assignment_expression ']'
1093 gi_source_symbol_merge_type ($$, gi_source_array_new ($3));
1097 GISourceType *func = gi_source_function_new ();
1098 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
1099 gi_source_symbol_merge_type ($$, func);
1101 | '(' parameter_list ')'
1103 GISourceType *func = gi_source_function_new ();
1104 // ignore (void) parameter list
1105 if ($2 != NULL && ($2->next != NULL || ((GISourceSymbol *) $2->data)->base_type->type != CTYPE_VOID)) {
1106 func->child_list = $2;
1108 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
1109 gi_source_symbol_merge_type ($$, func);
1111 | direct_abstract_declarator '(' ')'
1113 GISourceType *func = gi_source_function_new ();
1115 gi_source_symbol_merge_type ($$, func);
1117 | direct_abstract_declarator '(' parameter_list ')'
1119 GISourceType *func = gi_source_function_new ();
1120 // ignore (void) parameter list
1121 if ($3 != NULL && ($3->next != NULL || ((GISourceSymbol *) $3->data)->base_type->type != CTYPE_VOID)) {
1122 func->child_list = $3;
1125 gi_source_symbol_merge_type ($$, func);
1132 $$ = g_strdup (yytext);
1137 : assignment_expression
1138 | '{' initializer_list '}'
1139 | '{' initializer_list ',' '}'
1144 | initializer_list ',' initializer
1147 /* A.2.3 Statements. */
1151 | compound_statement
1152 | expression_statement
1153 | selection_statement
1154 | iteration_statement
1159 : identifier_or_typedef_name ':' statement
1160 | CASE constant_expression ':' statement
1161 | DEFAULT ':' statement
1166 | '{' block_item_list '}'
1171 | block_item_list block_item
1179 expression_statement
1185 : IF '(' expression ')' statement
1186 | IF '(' expression ')' statement ELSE statement
1187 | SWITCH '(' expression ')' statement
1191 : WHILE '(' expression ')' statement
1192 | DO statement WHILE '(' expression ')' ';'
1193 | FOR '(' ';' ';' ')' statement
1194 | FOR '(' expression ';' ';' ')' statement
1195 | FOR '(' ';' expression ';' ')' statement
1196 | FOR '(' expression ';' expression ';' ')' statement
1197 | FOR '(' ';' ';' expression ')' statement
1198 | FOR '(' expression ';' ';' expression ')' statement
1199 | FOR '(' ';' expression ';' expression ')' statement
1200 | FOR '(' expression ';' expression ';' expression ')' statement
1204 : GOTO identifier_or_typedef_name ';'
1208 | RETURN expression ';'
1211 /* A.2.4 External definitions. */
1214 : external_declaration
1215 | translation_unit external_declaration
1218 external_declaration
1219 : function_definition
1225 : declaration_specifiers declarator declaration_list compound_statement
1226 | declaration_specifiers declarator compound_statement
1231 | declaration_list declaration
1239 $$ = g_strdup (yytext + strlen ("#define "));
1246 $$ = g_strdup (yytext + strlen ("#define "));
1250 function_macro_define
1251 : function_macro '(' identifier_list ')'
1255 : object_macro constant_expression
1257 if ($2->const_int_set || $2->const_string != NULL) {
1259 gi_source_scanner_add_symbol (scanner, $2);
1260 gi_source_symbol_unref ($2);
1266 : function_macro_define
1267 | object_macro_define
1273 yyerror (GISourceScanner *scanner, const char *s)
1275 /* ignore errors while doing a macro scan as not all object macros
1276 * have valid expressions */
1277 if (!scanner->macro_scan)
1279 fprintf(stderr, "%s:%d: %s\n",
1280 scanner->current_filename, lineno, s);
1285 eat_hspace (FILE * f)
1292 while (c == ' ' || c == '\t');
1297 eat_line (FILE * f, int c)
1299 while (c != EOF && c != '\n')
1306 if (c == ' ' || c == '\t')
1315 read_identifier (FILE * f, int c, char **identifier)
1317 GString *id = g_string_new ("");
1318 while (g_ascii_isalnum (c) || c == '_')
1320 g_string_append_c (id, c);
1323 *identifier = g_string_free (id, FALSE);
1328 gi_source_scanner_parse_macros (GISourceScanner *scanner, GList *filenames)
1330 GError *error = NULL;
1331 char *tmp_name = NULL;
1333 fdopen (g_file_open_tmp ("gen-introspect-XXXXXX.h", &tmp_name, &error),
1335 g_unlink (tmp_name);
1338 for (l = filenames; l != NULL; l = l->next)
1340 FILE *f = fopen (l->data, "r");
1343 GString *define_line;
1345 gboolean error_line = FALSE;
1346 int c = eat_hspace (f);
1352 c = eat_line (f, c);
1357 /* print current location */
1358 str = g_strescape (l->data, "");
1359 fprintf (fmacros, "# %d \"%s\"\n", line, str);
1363 c = read_identifier (f, c, &str);
1364 if (strcmp (str, "define") != 0 || (c != ' ' && c != '\t'))
1368 c = eat_line (f, c);
1374 c = read_identifier (f, c, &str);
1375 if (strlen (str) == 0 || (c != ' ' && c != '\t' && c != '('))
1379 c = eat_line (f, c);
1383 define_line = g_string_new ("#define ");
1384 g_string_append (define_line, str);
1390 g_string_append_c (define_line, c);
1392 if (c == EOF || c == '\n')
1400 g_string_free (define_line, TRUE);
1402 c = eat_line (f, c);
1407 g_assert (c == ')');
1408 g_string_append_c (define_line, c);
1411 /* found function-like macro */
1412 fprintf (fmacros, "%s\n", define_line->str);
1414 g_string_free (define_line, TRUE);
1415 /* ignore rest of line */
1416 c = eat_line (f, c);
1420 if (c != ' ' && c != '\t')
1422 g_string_free (define_line, TRUE);
1424 c = eat_line (f, c);
1428 while (c != EOF && c != '\n')
1430 g_string_append_c (define_line, c);
1437 /* fold lines when seeing backslash new-line sequence */
1442 g_string_append_c (define_line, '\\');
1447 /* found object-like macro */
1448 fprintf (fmacros, "%s\n", define_line->str);
1450 c = eat_line (f, c);
1458 gi_source_scanner_parse_file (scanner, fmacros);
1462 gi_source_scanner_parse_file (GISourceScanner *scanner, FILE *file)
1464 g_return_val_if_fail (file != NULL, FALSE);
1466 const_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1467 g_free, (GDestroyNotify)gi_source_symbol_unref);
1473 g_hash_table_destroy (const_table);
1482 gi_source_scanner_lex_filename (GISourceScanner *scanner, const gchar *filename)
1484 yyin = fopen (filename, "r");
1486 while (yylex (scanner) != YYEOF)