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))
189 g_warning ("Ignoring non-UTF-8 constant string \"%s\"", yytext + 1);
191 g_free($$->const_string);
192 $$->const_string = NULL;
198 char *strings, *string2;
200 yytext[strlen (yytext) - 1] = '\0';
201 string2 = g_strcompress (yytext + 1);
202 strings = g_strconcat ($$->const_string, string2, NULL);
203 g_free ($$->const_string);
205 $$->const_string = strings;
212 $$ = g_strdup (yytext);
216 identifier_or_typedef_name
223 | postfix_expression '[' expression ']'
225 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
227 | postfix_expression '(' argument_expression_list ')'
229 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
231 | postfix_expression '(' ')'
233 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
235 | postfix_expression '.' identifier_or_typedef_name
237 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
239 | postfix_expression ARROW identifier_or_typedef_name
241 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
243 | postfix_expression PLUSPLUS
245 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
247 | postfix_expression MINUSMINUS
249 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
253 argument_expression_list
254 : assignment_expression
255 | argument_expression_list ',' assignment_expression
260 | PLUSPLUS unary_expression
262 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
264 | MINUSMINUS unary_expression
266 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
268 | unary_operator cast_expression
276 $$->const_int = -$2->const_int;
278 case UNARY_BITWISE_COMPLEMENT:
280 $$->const_int = ~$2->const_int;
282 case UNARY_LOGICAL_NEGATION:
284 $$->const_int = !gi_source_symbol_get_const_boolean ($2);
287 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
291 | SIZEOF unary_expression
293 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
295 | SIZEOF '(' type_name ')'
298 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
305 $$ = UNARY_ADDRESS_OF;
309 $$ = UNARY_POINTER_INDIRECTION;
321 $$ = UNARY_BITWISE_COMPLEMENT;
325 $$ = UNARY_LOGICAL_NEGATION;
331 | '(' type_name ')' cast_expression
338 multiplicative_expression
340 | multiplicative_expression '*' cast_expression
342 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
343 $$->const_int_set = TRUE;
344 $$->const_int = $1->const_int * $3->const_int;
346 | multiplicative_expression '/' cast_expression
348 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
349 $$->const_int_set = TRUE;
350 if ($3->const_int != 0) {
351 $$->const_int = $1->const_int / $3->const_int;
354 | multiplicative_expression '%' cast_expression
356 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
357 $$->const_int_set = TRUE;
358 if ($3->const_int != 0) {
359 $$->const_int = $1->const_int % $3->const_int;
365 : multiplicative_expression
366 | additive_expression '+' multiplicative_expression
368 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
369 $$->const_int_set = TRUE;
370 $$->const_int = $1->const_int + $3->const_int;
372 | additive_expression '-' multiplicative_expression
374 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
375 $$->const_int_set = TRUE;
376 $$->const_int = $1->const_int - $3->const_int;
381 : additive_expression
382 | shift_expression SL additive_expression
384 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
385 $$->const_int_set = TRUE;
386 $$->const_int = $1->const_int << $3->const_int;
388 /* assume this is a bitfield/flags declaration
389 * if a left shift operator is sued in an enum value
390 * This mimics the glib-mkenum behavior.
394 | shift_expression SR additive_expression
396 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
397 $$->const_int_set = TRUE;
398 $$->const_int = $1->const_int >> $3->const_int;
402 relational_expression
404 | relational_expression '<' shift_expression
406 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
407 $$->const_int_set = TRUE;
408 $$->const_int = $1->const_int < $3->const_int;
410 | relational_expression '>' shift_expression
412 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
413 $$->const_int_set = TRUE;
414 $$->const_int = $1->const_int > $3->const_int;
416 | relational_expression LTEQ shift_expression
418 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
419 $$->const_int_set = TRUE;
420 $$->const_int = $1->const_int <= $3->const_int;
422 | relational_expression GTEQ shift_expression
424 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
425 $$->const_int_set = TRUE;
426 $$->const_int = $1->const_int >= $3->const_int;
431 : relational_expression
432 | equality_expression EQ relational_expression
434 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
435 $$->const_int_set = TRUE;
436 $$->const_int = $1->const_int == $3->const_int;
438 | equality_expression NOTEQ relational_expression
440 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
441 $$->const_int_set = TRUE;
442 $$->const_int = $1->const_int != $3->const_int;
447 : equality_expression
448 | and_expression '&' equality_expression
450 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
451 $$->const_int_set = TRUE;
452 $$->const_int = $1->const_int & $3->const_int;
456 exclusive_or_expression
458 | exclusive_or_expression '^' and_expression
460 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
461 $$->const_int_set = TRUE;
462 $$->const_int = $1->const_int ^ $3->const_int;
466 inclusive_or_expression
467 : exclusive_or_expression
468 | inclusive_or_expression '|' exclusive_or_expression
470 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
471 $$->const_int_set = TRUE;
472 $$->const_int = $1->const_int | $3->const_int;
476 logical_and_expression
477 : inclusive_or_expression
478 | logical_and_expression ANDAND inclusive_or_expression
480 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
481 $$->const_int_set = TRUE;
483 gi_source_symbol_get_const_boolean ($1) &&
484 gi_source_symbol_get_const_boolean ($3);
488 logical_or_expression
489 : logical_and_expression
490 | logical_or_expression OROR logical_and_expression
492 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
493 $$->const_int_set = TRUE;
495 gi_source_symbol_get_const_boolean ($1) ||
496 gi_source_symbol_get_const_boolean ($3);
500 conditional_expression
501 : logical_or_expression
502 | logical_or_expression '?' expression ':' conditional_expression
504 $$ = gi_source_symbol_get_const_boolean ($1) ? $3 : $5;
508 assignment_expression
509 : conditional_expression
510 | unary_expression assignment_operator assignment_expression
512 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
531 : assignment_expression
532 | expression ',' assignment_expression
534 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
539 : conditional_expression
542 /* A.2.2 Declarations. */
545 : declaration_specifiers init_declarator_list ';'
548 for (l = $2; l != NULL; l = l->next) {
549 GISourceSymbol *sym = l->data;
550 gi_source_symbol_merge_type (sym, gi_source_type_copy ($1));
551 if ($1->storage_class_specifier & STORAGE_CLASS_TYPEDEF) {
552 sym->type = CSYMBOL_TYPE_TYPEDEF;
553 } else if (sym->base_type->type == CTYPE_FUNCTION) {
554 sym->type = CSYMBOL_TYPE_FUNCTION;
556 sym->type = CSYMBOL_TYPE_OBJECT;
558 gi_source_scanner_add_symbol (scanner, sym);
559 gi_source_symbol_unref (sym);
563 | declaration_specifiers ';'
569 declaration_specifiers
570 : storage_class_specifier declaration_specifiers
573 $$->storage_class_specifier |= $1;
575 | storage_class_specifier
577 $$ = gi_source_type_new (CTYPE_INVALID);
578 $$->storage_class_specifier |= $1;
580 | type_specifier declaration_specifiers
583 /* combine basic types like unsigned int and long long */
584 if ($$->type == CTYPE_BASIC_TYPE && $2->type == CTYPE_BASIC_TYPE) {
585 char *name = g_strdup_printf ("%s %s", $$->name, $2->name);
594 | type_qualifier declaration_specifiers
597 $$->type_qualifier |= $1;
601 $$ = gi_source_type_new (CTYPE_INVALID);
602 $$->type_qualifier |= $1;
604 | function_specifier declaration_specifiers
607 $$->function_specifier |= $1;
611 $$ = gi_source_type_new (CTYPE_INVALID);
612 $$->function_specifier |= $1;
619 $$ = g_list_append (NULL, $1);
621 | init_declarator_list ',' init_declarator
623 $$ = g_list_append ($1, $3);
629 | declarator '=' initializer
632 storage_class_specifier
635 $$ = STORAGE_CLASS_TYPEDEF;
639 $$ = STORAGE_CLASS_EXTERN;
643 $$ = STORAGE_CLASS_STATIC;
647 $$ = STORAGE_CLASS_AUTO;
651 $$ = STORAGE_CLASS_REGISTER;
658 $$ = gi_source_type_new (CTYPE_VOID);
662 $$ = gi_source_basic_type_new ("char");
666 $$ = gi_source_basic_type_new ("short");
670 $$ = gi_source_basic_type_new ("int");
674 $$ = gi_source_basic_type_new ("long");
678 $$ = gi_source_basic_type_new ("float");
682 $$ = gi_source_basic_type_new ("double");
686 $$ = gi_source_basic_type_new ("signed");
690 $$ = gi_source_basic_type_new ("unsigned");
694 $$ = gi_source_basic_type_new ("bool");
696 | struct_or_union_specifier
700 $$ = gi_source_typedef_new ($1);
705 struct_or_union_specifier
706 : struct_or_union identifier_or_typedef_name '{' struct_declaration_list '}'
712 GISourceSymbol *sym = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
713 if ($$->type == CTYPE_STRUCT) {
714 sym->type = CSYMBOL_TYPE_STRUCT;
715 } else if ($$->type == CTYPE_UNION) {
716 sym->type = CSYMBOL_TYPE_UNION;
718 g_assert_not_reached ();
720 sym->ident = g_strdup ($$->name);
721 sym->base_type = gi_source_type_copy ($$);
722 gi_source_scanner_add_symbol (scanner, sym);
723 gi_source_symbol_unref (sym);
725 | struct_or_union '{' struct_declaration_list '}'
730 | struct_or_union identifier_or_typedef_name
740 $$ = gi_source_struct_new (NULL);
744 $$ = gi_source_union_new (NULL);
748 struct_declaration_list
750 | struct_declaration_list struct_declaration
752 $$ = g_list_concat ($1, $2);
757 : specifier_qualifier_list struct_declarator_list ';'
761 for (l = $2; l != NULL; l = l->next)
763 GISourceSymbol *sym = l->data;
764 if ($1->storage_class_specifier & STORAGE_CLASS_TYPEDEF)
765 sym->type = CSYMBOL_TYPE_TYPEDEF;
767 sym->type = CSYMBOL_TYPE_MEMBER;
768 gi_source_symbol_merge_type (sym, gi_source_type_copy ($1));
769 $$ = g_list_append ($$, sym);
775 specifier_qualifier_list
776 : type_specifier specifier_qualifier_list
782 | type_qualifier specifier_qualifier_list
785 $$->type_qualifier |= $1;
789 $$ = gi_source_type_new (CTYPE_INVALID);
790 $$->type_qualifier |= $1;
794 struct_declarator_list
797 $$ = g_list_append (NULL, $1);
799 | struct_declarator_list ',' struct_declarator
801 $$ = g_list_append ($1, $3);
806 : /* empty, support for anonymous structs and unions */
808 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
811 | ':' constant_expression
813 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
815 | declarator ':' constant_expression
818 if ($3->const_int_set) {
819 $$->const_int_set = TRUE;
820 $$->const_int = $3->const_int;
826 : ENUM identifier_or_typedef_name '{' enumerator_list '}'
828 $$ = gi_source_enum_new ($2);
830 $$->is_bitfield = is_bitfield;
831 last_enum_value = -1;
833 | ENUM '{' enumerator_list '}'
835 $$ = gi_source_enum_new (NULL);
837 $$->is_bitfield = is_bitfield;
838 last_enum_value = -1;
840 | ENUM identifier_or_typedef_name '{' enumerator_list ',' '}'
842 $$ = gi_source_enum_new ($2);
844 $$->is_bitfield = is_bitfield;
845 last_enum_value = -1;
847 | ENUM '{' enumerator_list ',' '}'
849 $$ = gi_source_enum_new (NULL);
851 $$->is_bitfield = is_bitfield;
852 last_enum_value = -1;
854 | ENUM identifier_or_typedef_name
856 $$ = gi_source_enum_new ($2);
863 /* reset flag before the first enum value */
868 $$ = g_list_append (NULL, $2);
870 | enumerator_list ',' enumerator
872 $$ = g_list_append ($1, $3);
879 $$ = gi_source_symbol_new (CSYMBOL_TYPE_OBJECT);
881 $$->const_int_set = TRUE;
882 $$->const_int = ++last_enum_value;
883 g_hash_table_insert (const_table, g_strdup ($$->ident), gi_source_symbol_ref ($$));
885 | identifier '=' constant_expression
887 $$ = gi_source_symbol_new (CSYMBOL_TYPE_OBJECT);
889 $$->const_int_set = TRUE;
890 $$->const_int = $3->const_int;
891 last_enum_value = $$->const_int;
892 g_hash_table_insert (const_table, g_strdup ($$->ident), gi_source_symbol_ref ($$));
899 $$ = TYPE_QUALIFIER_CONST;
903 $$ = TYPE_QUALIFIER_RESTRICT;
907 $$ = TYPE_QUALIFIER_EXTENSION;
911 $$ = TYPE_QUALIFIER_VOLATILE;
918 $$ = FUNCTION_INLINE;
923 : pointer direct_declarator
926 gi_source_symbol_merge_type ($$, $1);
934 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
941 | direct_declarator '[' assignment_expression ']'
944 gi_source_symbol_merge_type ($$, gi_source_array_new ($3));
946 | direct_declarator '[' ']'
949 gi_source_symbol_merge_type ($$, gi_source_array_new (NULL));
951 | direct_declarator '(' parameter_list ')'
953 GISourceType *func = gi_source_function_new ();
954 // ignore (void) parameter list
955 if ($3 != NULL && ($3->next != NULL || ((GISourceSymbol *) $3->data)->base_type->type != CTYPE_VOID)) {
956 func->child_list = $3;
959 gi_source_symbol_merge_type ($$, func);
961 | direct_declarator '(' identifier_list ')'
963 GISourceType *func = gi_source_function_new ();
964 func->child_list = $3;
966 gi_source_symbol_merge_type ($$, func);
968 | direct_declarator '(' ')'
970 GISourceType *func = gi_source_function_new ();
972 gi_source_symbol_merge_type ($$, func);
977 : '*' type_qualifier_list
979 $$ = gi_source_pointer_new (NULL);
980 $$->type_qualifier = $2;
984 $$ = gi_source_pointer_new (NULL);
986 | '*' type_qualifier_list pointer
988 $$ = gi_source_pointer_new ($3);
989 $$->type_qualifier = $2;
993 $$ = gi_source_pointer_new ($2);
999 | type_qualifier_list type_qualifier
1006 : parameter_declaration
1008 $$ = g_list_append (NULL, $1);
1010 | parameter_list ',' parameter_declaration
1012 $$ = g_list_append ($1, $3);
1016 parameter_declaration
1017 : declaration_specifiers declarator
1020 gi_source_symbol_merge_type ($$, $1);
1022 | declaration_specifiers abstract_declarator
1025 gi_source_symbol_merge_type ($$, $1);
1027 | declaration_specifiers
1029 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
1034 $$ = gi_source_symbol_new (CSYMBOL_TYPE_ELLIPSIS);
1041 GISourceSymbol *sym = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
1043 $$ = g_list_append (NULL, sym);
1045 | identifier_list ',' identifier
1047 GISourceSymbol *sym = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
1049 $$ = g_list_append ($1, sym);
1054 : specifier_qualifier_list
1055 | specifier_qualifier_list abstract_declarator
1061 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
1062 gi_source_symbol_merge_type ($$, $1);
1064 | direct_abstract_declarator
1065 | pointer direct_abstract_declarator
1068 gi_source_symbol_merge_type ($$, $1);
1072 direct_abstract_declarator
1073 : '(' abstract_declarator ')'
1079 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
1080 gi_source_symbol_merge_type ($$, gi_source_array_new (NULL));
1082 | '[' assignment_expression ']'
1084 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
1085 gi_source_symbol_merge_type ($$, gi_source_array_new ($2));
1087 | direct_abstract_declarator '[' ']'
1090 gi_source_symbol_merge_type ($$, gi_source_array_new (NULL));
1092 | direct_abstract_declarator '[' assignment_expression ']'
1095 gi_source_symbol_merge_type ($$, gi_source_array_new ($3));
1099 GISourceType *func = gi_source_function_new ();
1100 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
1101 gi_source_symbol_merge_type ($$, func);
1103 | '(' parameter_list ')'
1105 GISourceType *func = gi_source_function_new ();
1106 // ignore (void) parameter list
1107 if ($2 != NULL && ($2->next != NULL || ((GISourceSymbol *) $2->data)->base_type->type != CTYPE_VOID)) {
1108 func->child_list = $2;
1110 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
1111 gi_source_symbol_merge_type ($$, func);
1113 | direct_abstract_declarator '(' ')'
1115 GISourceType *func = gi_source_function_new ();
1117 gi_source_symbol_merge_type ($$, func);
1119 | direct_abstract_declarator '(' parameter_list ')'
1121 GISourceType *func = gi_source_function_new ();
1122 // ignore (void) parameter list
1123 if ($3 != NULL && ($3->next != NULL || ((GISourceSymbol *) $3->data)->base_type->type != CTYPE_VOID)) {
1124 func->child_list = $3;
1127 gi_source_symbol_merge_type ($$, func);
1134 $$ = g_strdup (yytext);
1139 : assignment_expression
1140 | '{' initializer_list '}'
1141 | '{' initializer_list ',' '}'
1146 | initializer_list ',' initializer
1149 /* A.2.3 Statements. */
1153 | compound_statement
1154 | expression_statement
1155 | selection_statement
1156 | iteration_statement
1161 : identifier_or_typedef_name ':' statement
1162 | CASE constant_expression ':' statement
1163 | DEFAULT ':' statement
1168 | '{' block_item_list '}'
1173 | block_item_list block_item
1181 expression_statement
1187 : IF '(' expression ')' statement
1188 | IF '(' expression ')' statement ELSE statement
1189 | SWITCH '(' expression ')' statement
1193 : WHILE '(' expression ')' statement
1194 | DO statement WHILE '(' expression ')' ';'
1195 | FOR '(' ';' ';' ')' statement
1196 | FOR '(' expression ';' ';' ')' statement
1197 | FOR '(' ';' expression ';' ')' statement
1198 | FOR '(' expression ';' expression ';' ')' statement
1199 | FOR '(' ';' ';' expression ')' statement
1200 | FOR '(' expression ';' ';' expression ')' statement
1201 | FOR '(' ';' expression ';' expression ')' statement
1202 | FOR '(' expression ';' expression ';' expression ')' statement
1206 : GOTO identifier_or_typedef_name ';'
1210 | RETURN expression ';'
1213 /* A.2.4 External definitions. */
1216 : external_declaration
1217 | translation_unit external_declaration
1220 external_declaration
1221 : function_definition
1227 : declaration_specifiers declarator declaration_list compound_statement
1228 | declaration_specifiers declarator compound_statement
1233 | declaration_list declaration
1241 $$ = g_strdup (yytext + strlen ("#define "));
1248 $$ = g_strdup (yytext + strlen ("#define "));
1252 function_macro_define
1253 : function_macro '(' identifier_list ')'
1257 : object_macro constant_expression
1259 if ($2->const_int_set || $2->const_string != NULL) {
1261 gi_source_scanner_add_symbol (scanner, $2);
1262 gi_source_symbol_unref ($2);
1268 : function_macro_define
1269 | object_macro_define
1275 yyerror (GISourceScanner *scanner, const char *s)
1277 /* ignore errors while doing a macro scan as not all object macros
1278 * have valid expressions */
1279 if (!scanner->macro_scan)
1281 fprintf(stderr, "%s:%d: %s\n",
1282 scanner->current_filename, lineno, s);
1287 eat_hspace (FILE * f)
1294 while (c == ' ' || c == '\t');
1299 eat_line (FILE * f, int c)
1301 while (c != EOF && c != '\n')
1308 if (c == ' ' || c == '\t')
1317 read_identifier (FILE * f, int c, char **identifier)
1319 GString *id = g_string_new ("");
1320 while (g_ascii_isalnum (c) || c == '_')
1322 g_string_append_c (id, c);
1325 *identifier = g_string_free (id, FALSE);
1330 gi_source_scanner_parse_macros (GISourceScanner *scanner, GList *filenames)
1332 GError *error = NULL;
1333 char *tmp_name = NULL;
1335 fdopen (g_file_open_tmp ("gen-introspect-XXXXXX.h", &tmp_name, &error),
1337 g_unlink (tmp_name);
1340 for (l = filenames; l != NULL; l = l->next)
1342 FILE *f = fopen (l->data, "r");
1345 GString *define_line;
1347 gboolean error_line = FALSE;
1348 int c = eat_hspace (f);
1354 c = eat_line (f, c);
1359 /* print current location */
1360 str = g_strescape (l->data, "");
1361 fprintf (fmacros, "# %d \"%s\"\n", line, str);
1365 c = read_identifier (f, c, &str);
1366 if (strcmp (str, "define") != 0 || (c != ' ' && c != '\t'))
1370 c = eat_line (f, c);
1376 c = read_identifier (f, c, &str);
1377 if (strlen (str) == 0 || (c != ' ' && c != '\t' && c != '('))
1381 c = eat_line (f, c);
1385 define_line = g_string_new ("#define ");
1386 g_string_append (define_line, str);
1392 g_string_append_c (define_line, c);
1394 if (c == EOF || c == '\n')
1402 g_string_free (define_line, TRUE);
1404 c = eat_line (f, c);
1409 g_assert (c == ')');
1410 g_string_append_c (define_line, c);
1413 /* found function-like macro */
1414 fprintf (fmacros, "%s\n", define_line->str);
1416 g_string_free (define_line, TRUE);
1417 /* ignore rest of line */
1418 c = eat_line (f, c);
1422 if (c != ' ' && c != '\t')
1424 g_string_free (define_line, TRUE);
1426 c = eat_line (f, c);
1430 while (c != EOF && c != '\n')
1432 g_string_append_c (define_line, c);
1439 /* fold lines when seeing backslash new-line sequence */
1444 g_string_append_c (define_line, '\\');
1449 /* found object-like macro */
1450 fprintf (fmacros, "%s\n", define_line->str);
1452 c = eat_line (f, c);
1460 gi_source_scanner_parse_file (scanner, fmacros);
1464 gi_source_scanner_parse_file (GISourceScanner *scanner, FILE *file)
1466 g_return_val_if_fail (file != NULL, FALSE);
1468 const_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1469 g_free, (GDestroyNotify)gi_source_symbol_unref);
1475 g_hash_table_destroy (const_table);
1484 gi_source_scanner_lex_filename (GISourceScanner *scanner, const gchar *filename)
1486 yyin = fopen (filename, "r");
1488 while (yylex (scanner) != YYEOF)