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;
58 GISourceSymbol *symbol;
60 StorageClassSpecifier storage_class_specifier;
61 TypeQualifier type_qualifier;
62 FunctionSpecifier function_specifier;
63 UnaryOperator unary_operator;
66 %parse-param { GISourceScanner* scanner }
67 %lex-param { GISourceScanner* scanner }
69 %token <str> IDENTIFIER "identifier"
70 %token <str> TYPEDEF_NAME "typedef-name"
72 %token INTEGER FLOATING CHARACTER STRING
74 %token ELLIPSIS ADDEQ SUBEQ MULEQ DIVEQ MODEQ XOREQ ANDEQ OREQ SL SR
75 %token SLEQ SREQ EQ NOTEQ LTEQ GTEQ ANDAND OROR PLUSPLUS MINUSMINUS ARROW
77 %token AUTO BOOL BREAK CASE CHAR CONST CONTINUE DEFAULT DO DOUBLE ELSE ENUM
78 %token EXTENSION EXTERN FLOAT FOR GOTO IF INLINE INT LONG REGISTER RESTRICT
79 %token RETURN SHORT SIGNED SIZEOF STATIC STRUCT SWITCH TYPEDEF UNION UNSIGNED
80 %token VOID VOLATILE WHILE
82 %token FUNCTION_MACRO OBJECT_MACRO
84 %start translation_unit
86 %type <ctype> declaration_specifiers
87 %type <ctype> enum_specifier
89 %type <ctype> specifier_qualifier_list
90 %type <ctype> type_name
91 %type <ctype> struct_or_union
92 %type <ctype> struct_or_union_specifier
93 %type <ctype> type_specifier
94 %type <str> identifier
95 %type <str> typedef_name
96 %type <str> identifier_or_typedef_name
97 %type <symbol> abstract_declarator
98 %type <symbol> init_declarator
99 %type <symbol> declarator
100 %type <symbol> enumerator
101 %type <symbol> direct_abstract_declarator
102 %type <symbol> direct_declarator
103 %type <symbol> parameter_declaration
104 %type <symbol> struct_declarator
105 %type <list> enumerator_list
106 %type <list> identifier_list
107 %type <list> init_declarator_list
108 %type <list> parameter_list
109 %type <list> struct_declaration
110 %type <list> struct_declaration_list
111 %type <list> struct_declarator_list
112 %type <storage_class_specifier> storage_class_specifier
113 %type <type_qualifier> type_qualifier
114 %type <type_qualifier> type_qualifier_list
115 %type <function_specifier> function_specifier
116 %type <symbol> expression
117 %type <symbol> constant_expression
118 %type <symbol> conditional_expression
119 %type <symbol> logical_and_expression
120 %type <symbol> logical_or_expression
121 %type <symbol> inclusive_or_expression
122 %type <symbol> exclusive_or_expression
123 %type <symbol> multiplicative_expression
124 %type <symbol> additive_expression
125 %type <symbol> shift_expression
126 %type <symbol> relational_expression
127 %type <symbol> equality_expression
128 %type <symbol> and_expression
129 %type <symbol> cast_expression
130 %type <symbol> assignment_expression
131 %type <symbol> unary_expression
132 %type <symbol> postfix_expression
133 %type <symbol> primary_expression
134 %type <unary_operator> unary_operator
135 %type <str> function_macro
136 %type <str> object_macro
137 %type <symbol> strings
141 /* A.2.1 Expressions. */
146 $$ = g_hash_table_lookup (const_table, $1);
148 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
150 $$ = gi_source_symbol_ref ($$);
155 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
156 $$->const_int_set = TRUE;
157 if (g_str_has_prefix (yytext, "0x") && strlen (yytext) > 2) {
158 $$->const_int = strtol (yytext + 2, NULL, 16);
159 } else if (g_str_has_prefix (yytext, "0") && strlen (yytext) > 1) {
160 $$->const_int = strtol (yytext + 1, NULL, 8);
162 $$->const_int = atoi (yytext);
167 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
171 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
172 $$->const_double_set = TRUE;
173 $$->const_double = 0.0;
174 sscanf (yytext, "%lf", &($$->const_double));
183 /* concatenate adjacent string literal tokens */
187 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
188 yytext[strlen (yytext) - 1] = '\0';
189 $$->const_string = g_strcompress (yytext + 1);
190 if (!g_utf8_validate ($$->const_string, -1, NULL))
193 g_warning ("Ignoring non-UTF-8 constant string \"%s\"", yytext + 1);
195 g_free($$->const_string);
196 $$->const_string = NULL;
202 char *strings, *string2;
204 yytext[strlen (yytext) - 1] = '\0';
205 string2 = g_strcompress (yytext + 1);
206 strings = g_strconcat ($$->const_string, string2, NULL);
207 g_free ($$->const_string);
209 $$->const_string = strings;
216 $$ = g_strdup (yytext);
220 identifier_or_typedef_name
227 | postfix_expression '[' expression ']'
229 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
231 | postfix_expression '(' argument_expression_list ')'
233 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
235 | postfix_expression '(' ')'
237 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
239 | postfix_expression '.' identifier_or_typedef_name
241 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
243 | postfix_expression ARROW identifier_or_typedef_name
245 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
247 | postfix_expression PLUSPLUS
249 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
251 | postfix_expression MINUSMINUS
253 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
257 argument_expression_list
258 : assignment_expression
259 | argument_expression_list ',' assignment_expression
264 | PLUSPLUS unary_expression
266 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
268 | MINUSMINUS unary_expression
270 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
272 | unary_operator cast_expression
280 $$->const_int = -$2->const_int;
282 case UNARY_BITWISE_COMPLEMENT:
284 $$->const_int = ~$2->const_int;
286 case UNARY_LOGICAL_NEGATION:
288 $$->const_int = !gi_source_symbol_get_const_boolean ($2);
291 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
295 | SIZEOF unary_expression
297 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
299 | SIZEOF '(' type_name ')'
302 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
309 $$ = UNARY_ADDRESS_OF;
313 $$ = UNARY_POINTER_INDIRECTION;
325 $$ = UNARY_BITWISE_COMPLEMENT;
329 $$ = UNARY_LOGICAL_NEGATION;
335 | '(' type_name ')' cast_expression
342 multiplicative_expression
344 | multiplicative_expression '*' cast_expression
346 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
347 $$->const_int_set = TRUE;
348 $$->const_int = $1->const_int * $3->const_int;
350 | multiplicative_expression '/' cast_expression
352 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
353 $$->const_int_set = TRUE;
354 if ($3->const_int != 0) {
355 $$->const_int = $1->const_int / $3->const_int;
358 | multiplicative_expression '%' cast_expression
360 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
361 $$->const_int_set = TRUE;
362 if ($3->const_int != 0) {
363 $$->const_int = $1->const_int % $3->const_int;
369 : multiplicative_expression
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;
376 | additive_expression '-' multiplicative_expression
378 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
379 $$->const_int_set = TRUE;
380 $$->const_int = $1->const_int - $3->const_int;
385 : additive_expression
386 | shift_expression SL additive_expression
388 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
389 $$->const_int_set = TRUE;
390 $$->const_int = $1->const_int << $3->const_int;
392 /* assume this is a bitfield/flags declaration
393 * if a left shift operator is sued in an enum value
394 * This mimics the glib-mkenum behavior.
398 | shift_expression SR additive_expression
400 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
401 $$->const_int_set = TRUE;
402 $$->const_int = $1->const_int >> $3->const_int;
406 relational_expression
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 '>' 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 LTEQ shift_expression
422 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
423 $$->const_int_set = TRUE;
424 $$->const_int = $1->const_int <= $3->const_int;
426 | relational_expression GTEQ shift_expression
428 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
429 $$->const_int_set = TRUE;
430 $$->const_int = $1->const_int >= $3->const_int;
435 : relational_expression
436 | equality_expression EQ relational_expression
438 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
439 $$->const_int_set = TRUE;
440 $$->const_int = $1->const_int == $3->const_int;
442 | equality_expression NOTEQ relational_expression
444 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
445 $$->const_int_set = TRUE;
446 $$->const_int = $1->const_int != $3->const_int;
451 : equality_expression
452 | and_expression '&' equality_expression
454 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
455 $$->const_int_set = TRUE;
456 $$->const_int = $1->const_int & $3->const_int;
460 exclusive_or_expression
462 | exclusive_or_expression '^' and_expression
464 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
465 $$->const_int_set = TRUE;
466 $$->const_int = $1->const_int ^ $3->const_int;
470 inclusive_or_expression
471 : exclusive_or_expression
472 | inclusive_or_expression '|' exclusive_or_expression
474 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
475 $$->const_int_set = TRUE;
476 $$->const_int = $1->const_int | $3->const_int;
480 logical_and_expression
481 : inclusive_or_expression
482 | logical_and_expression ANDAND inclusive_or_expression
484 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
485 $$->const_int_set = TRUE;
487 gi_source_symbol_get_const_boolean ($1) &&
488 gi_source_symbol_get_const_boolean ($3);
492 logical_or_expression
493 : logical_and_expression
494 | logical_or_expression OROR logical_and_expression
496 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
497 $$->const_int_set = TRUE;
499 gi_source_symbol_get_const_boolean ($1) ||
500 gi_source_symbol_get_const_boolean ($3);
504 conditional_expression
505 : logical_or_expression
506 | logical_or_expression '?' expression ':' conditional_expression
508 $$ = gi_source_symbol_get_const_boolean ($1) ? $3 : $5;
512 assignment_expression
513 : conditional_expression
514 | unary_expression assignment_operator assignment_expression
516 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
535 : assignment_expression
536 | expression ',' assignment_expression
538 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
543 : conditional_expression
546 /* A.2.2 Declarations. */
549 : declaration_specifiers init_declarator_list ';'
552 for (l = $2; l != NULL; l = l->next) {
553 GISourceSymbol *sym = l->data;
554 gi_source_symbol_merge_type (sym, gi_source_type_copy ($1));
555 if ($1->storage_class_specifier & STORAGE_CLASS_TYPEDEF) {
556 sym->type = CSYMBOL_TYPE_TYPEDEF;
557 } else if (sym->base_type->type == CTYPE_FUNCTION) {
558 sym->type = CSYMBOL_TYPE_FUNCTION;
560 sym->type = CSYMBOL_TYPE_OBJECT;
562 gi_source_scanner_add_symbol (scanner, sym);
563 gi_source_symbol_unref (sym);
567 | declaration_specifiers ';'
573 declaration_specifiers
574 : storage_class_specifier declaration_specifiers
577 $$->storage_class_specifier |= $1;
579 | storage_class_specifier
581 $$ = gi_source_type_new (CTYPE_INVALID);
582 $$->storage_class_specifier |= $1;
584 | type_specifier declaration_specifiers
587 /* combine basic types like unsigned int and long long */
588 if ($$->type == CTYPE_BASIC_TYPE && $2->type == CTYPE_BASIC_TYPE) {
589 char *name = g_strdup_printf ("%s %s", $$->name, $2->name);
598 | type_qualifier declaration_specifiers
601 $$->type_qualifier |= $1;
605 $$ = gi_source_type_new (CTYPE_INVALID);
606 $$->type_qualifier |= $1;
608 | function_specifier declaration_specifiers
611 $$->function_specifier |= $1;
615 $$ = gi_source_type_new (CTYPE_INVALID);
616 $$->function_specifier |= $1;
623 $$ = g_list_append (NULL, $1);
625 | init_declarator_list ',' init_declarator
627 $$ = g_list_append ($1, $3);
633 | declarator '=' initializer
636 storage_class_specifier
639 $$ = STORAGE_CLASS_TYPEDEF;
643 $$ = STORAGE_CLASS_EXTERN;
647 $$ = STORAGE_CLASS_STATIC;
651 $$ = STORAGE_CLASS_AUTO;
655 $$ = STORAGE_CLASS_REGISTER;
662 $$ = gi_source_type_new (CTYPE_VOID);
666 $$ = gi_source_basic_type_new ("char");
670 $$ = gi_source_basic_type_new ("short");
674 $$ = gi_source_basic_type_new ("int");
678 $$ = gi_source_basic_type_new ("long");
682 $$ = gi_source_basic_type_new ("float");
686 $$ = gi_source_basic_type_new ("double");
690 $$ = gi_source_basic_type_new ("signed");
694 $$ = gi_source_basic_type_new ("unsigned");
698 $$ = gi_source_basic_type_new ("bool");
700 | struct_or_union_specifier
704 $$ = gi_source_typedef_new ($1);
709 struct_or_union_specifier
710 : struct_or_union identifier_or_typedef_name '{' struct_declaration_list '}'
716 GISourceSymbol *sym = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
717 if ($$->type == CTYPE_STRUCT) {
718 sym->type = CSYMBOL_TYPE_STRUCT;
719 } else if ($$->type == CTYPE_UNION) {
720 sym->type = CSYMBOL_TYPE_UNION;
722 g_assert_not_reached ();
724 sym->ident = g_strdup ($$->name);
725 sym->base_type = gi_source_type_copy ($$);
726 gi_source_scanner_add_symbol (scanner, sym);
727 gi_source_symbol_unref (sym);
729 | struct_or_union '{' struct_declaration_list '}'
734 | struct_or_union identifier_or_typedef_name
744 $$ = gi_source_struct_new (NULL);
748 $$ = gi_source_union_new (NULL);
752 struct_declaration_list
754 | struct_declaration_list struct_declaration
756 $$ = g_list_concat ($1, $2);
761 : specifier_qualifier_list struct_declarator_list ';'
765 for (l = $2; l != NULL; l = l->next)
767 GISourceSymbol *sym = l->data;
768 if ($1->storage_class_specifier & STORAGE_CLASS_TYPEDEF)
769 sym->type = CSYMBOL_TYPE_TYPEDEF;
771 sym->type = CSYMBOL_TYPE_MEMBER;
772 gi_source_symbol_merge_type (sym, gi_source_type_copy ($1));
773 $$ = g_list_append ($$, sym);
779 specifier_qualifier_list
780 : type_specifier specifier_qualifier_list
786 | type_qualifier specifier_qualifier_list
789 $$->type_qualifier |= $1;
793 $$ = gi_source_type_new (CTYPE_INVALID);
794 $$->type_qualifier |= $1;
798 struct_declarator_list
801 $$ = g_list_append (NULL, $1);
803 | struct_declarator_list ',' struct_declarator
805 $$ = g_list_append ($1, $3);
810 : /* empty, support for anonymous structs and unions */
812 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
815 | ':' constant_expression
817 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
819 | declarator ':' constant_expression
822 if ($3->const_int_set) {
823 $$->const_int_set = TRUE;
824 $$->const_int = $3->const_int;
830 : ENUM identifier_or_typedef_name '{' enumerator_list '}'
832 $$ = gi_source_enum_new ($2);
834 $$->is_bitfield = is_bitfield;
835 last_enum_value = -1;
837 | ENUM '{' enumerator_list '}'
839 $$ = gi_source_enum_new (NULL);
841 $$->is_bitfield = is_bitfield;
842 last_enum_value = -1;
844 | ENUM identifier_or_typedef_name '{' enumerator_list ',' '}'
846 $$ = gi_source_enum_new ($2);
848 $$->is_bitfield = is_bitfield;
849 last_enum_value = -1;
851 | ENUM '{' enumerator_list ',' '}'
853 $$ = gi_source_enum_new (NULL);
855 $$->is_bitfield = is_bitfield;
856 last_enum_value = -1;
858 | ENUM identifier_or_typedef_name
860 $$ = gi_source_enum_new ($2);
867 /* reset flag before the first enum value */
872 $$ = g_list_append (NULL, $2);
874 | enumerator_list ',' enumerator
876 $$ = g_list_append ($1, $3);
883 $$ = gi_source_symbol_new (CSYMBOL_TYPE_OBJECT);
885 $$->const_int_set = TRUE;
886 $$->const_int = ++last_enum_value;
887 g_hash_table_insert (const_table, g_strdup ($$->ident), gi_source_symbol_ref ($$));
889 | identifier '=' constant_expression
891 $$ = gi_source_symbol_new (CSYMBOL_TYPE_OBJECT);
893 $$->const_int_set = TRUE;
894 $$->const_int = $3->const_int;
895 last_enum_value = $$->const_int;
896 g_hash_table_insert (const_table, g_strdup ($$->ident), gi_source_symbol_ref ($$));
903 $$ = TYPE_QUALIFIER_CONST;
907 $$ = TYPE_QUALIFIER_RESTRICT;
911 $$ = TYPE_QUALIFIER_EXTENSION;
915 $$ = TYPE_QUALIFIER_VOLATILE;
922 $$ = FUNCTION_INLINE;
927 : pointer direct_declarator
930 gi_source_symbol_merge_type ($$, $1);
938 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
945 | direct_declarator '[' assignment_expression ']'
948 gi_source_symbol_merge_type ($$, gi_source_array_new ($3));
950 | direct_declarator '[' ']'
953 gi_source_symbol_merge_type ($$, gi_source_array_new (NULL));
955 | direct_declarator '(' parameter_list ')'
957 GISourceType *func = gi_source_function_new ();
958 // ignore (void) parameter list
959 if ($3 != NULL && ($3->next != NULL || ((GISourceSymbol *) $3->data)->base_type->type != CTYPE_VOID)) {
960 func->child_list = $3;
963 gi_source_symbol_merge_type ($$, func);
965 | direct_declarator '(' identifier_list ')'
967 GISourceType *func = gi_source_function_new ();
968 func->child_list = $3;
970 gi_source_symbol_merge_type ($$, func);
972 | direct_declarator '(' ')'
974 GISourceType *func = gi_source_function_new ();
976 gi_source_symbol_merge_type ($$, func);
981 : '*' type_qualifier_list
983 $$ = gi_source_pointer_new (NULL);
984 $$->type_qualifier = $2;
988 $$ = gi_source_pointer_new (NULL);
990 | '*' type_qualifier_list pointer
992 $$ = gi_source_pointer_new ($3);
993 $$->type_qualifier = $2;
997 $$ = gi_source_pointer_new ($2);
1003 | type_qualifier_list type_qualifier
1010 : parameter_declaration
1012 $$ = g_list_append (NULL, $1);
1014 | parameter_list ',' parameter_declaration
1016 $$ = g_list_append ($1, $3);
1020 parameter_declaration
1021 : declaration_specifiers declarator
1024 gi_source_symbol_merge_type ($$, $1);
1026 | declaration_specifiers abstract_declarator
1029 gi_source_symbol_merge_type ($$, $1);
1031 | declaration_specifiers
1033 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
1038 $$ = gi_source_symbol_new (CSYMBOL_TYPE_ELLIPSIS);
1045 GISourceSymbol *sym = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
1047 $$ = g_list_append (NULL, sym);
1049 | identifier_list ',' identifier
1051 GISourceSymbol *sym = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
1053 $$ = g_list_append ($1, sym);
1058 : specifier_qualifier_list
1059 | specifier_qualifier_list abstract_declarator
1065 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
1066 gi_source_symbol_merge_type ($$, $1);
1068 | direct_abstract_declarator
1069 | pointer direct_abstract_declarator
1072 gi_source_symbol_merge_type ($$, $1);
1076 direct_abstract_declarator
1077 : '(' abstract_declarator ')'
1083 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
1084 gi_source_symbol_merge_type ($$, gi_source_array_new (NULL));
1086 | '[' assignment_expression ']'
1088 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
1089 gi_source_symbol_merge_type ($$, gi_source_array_new ($2));
1091 | direct_abstract_declarator '[' ']'
1094 gi_source_symbol_merge_type ($$, gi_source_array_new (NULL));
1096 | direct_abstract_declarator '[' assignment_expression ']'
1099 gi_source_symbol_merge_type ($$, gi_source_array_new ($3));
1103 GISourceType *func = gi_source_function_new ();
1104 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
1105 gi_source_symbol_merge_type ($$, func);
1107 | '(' parameter_list ')'
1109 GISourceType *func = gi_source_function_new ();
1110 // ignore (void) parameter list
1111 if ($2 != NULL && ($2->next != NULL || ((GISourceSymbol *) $2->data)->base_type->type != CTYPE_VOID)) {
1112 func->child_list = $2;
1114 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
1115 gi_source_symbol_merge_type ($$, func);
1117 | direct_abstract_declarator '(' ')'
1119 GISourceType *func = gi_source_function_new ();
1121 gi_source_symbol_merge_type ($$, func);
1123 | direct_abstract_declarator '(' parameter_list ')'
1125 GISourceType *func = gi_source_function_new ();
1126 // ignore (void) parameter list
1127 if ($3 != NULL && ($3->next != NULL || ((GISourceSymbol *) $3->data)->base_type->type != CTYPE_VOID)) {
1128 func->child_list = $3;
1131 gi_source_symbol_merge_type ($$, func);
1138 $$ = g_strdup (yytext);
1143 : assignment_expression
1144 | '{' initializer_list '}'
1145 | '{' initializer_list ',' '}'
1150 | initializer_list ',' initializer
1153 /* A.2.3 Statements. */
1157 | compound_statement
1158 | expression_statement
1159 | selection_statement
1160 | iteration_statement
1165 : identifier_or_typedef_name ':' statement
1166 | CASE constant_expression ':' statement
1167 | DEFAULT ':' statement
1172 | '{' block_item_list '}'
1177 | block_item_list block_item
1185 expression_statement
1191 : IF '(' expression ')' statement
1192 | IF '(' expression ')' statement ELSE statement
1193 | SWITCH '(' expression ')' statement
1197 : WHILE '(' expression ')' statement
1198 | DO statement WHILE '(' expression ')' ';'
1199 | FOR '(' ';' ';' ')' statement
1200 | FOR '(' expression ';' ';' ')' statement
1201 | FOR '(' ';' expression ';' ')' statement
1202 | FOR '(' expression ';' expression ';' ')' statement
1203 | FOR '(' ';' ';' expression ')' statement
1204 | FOR '(' expression ';' ';' expression ')' statement
1205 | FOR '(' ';' expression ';' expression ')' statement
1206 | FOR '(' expression ';' expression ';' expression ')' statement
1210 : GOTO identifier_or_typedef_name ';'
1214 | RETURN expression ';'
1217 /* A.2.4 External definitions. */
1220 : external_declaration
1221 | translation_unit external_declaration
1224 external_declaration
1225 : function_definition
1231 : declaration_specifiers declarator declaration_list compound_statement
1232 | declaration_specifiers declarator compound_statement
1237 | declaration_list declaration
1245 $$ = g_strdup (yytext + strlen ("#define "));
1252 $$ = g_strdup (yytext + strlen ("#define "));
1256 function_macro_define
1257 : function_macro '(' identifier_list ')'
1261 : object_macro constant_expression
1263 if ($2->const_int_set || $2->const_double_set || $2->const_string != NULL) {
1265 gi_source_scanner_add_symbol (scanner, $2);
1266 gi_source_symbol_unref ($2);
1272 : function_macro_define
1273 | object_macro_define
1279 yyerror (GISourceScanner *scanner, const char *s)
1281 /* ignore errors while doing a macro scan as not all object macros
1282 * have valid expressions */
1283 if (!scanner->macro_scan)
1285 fprintf(stderr, "%s:%d: %s in '%s' at '%s'\n",
1286 scanner->current_filename, lineno, s, linebuf, yytext);
1291 eat_hspace (FILE * f)
1298 while (c == ' ' || c == '\t');
1303 eat_line (FILE * f, int c)
1305 while (c != EOF && c != '\n')
1312 if (c == ' ' || c == '\t')
1321 read_identifier (FILE * f, int c, char **identifier)
1323 GString *id = g_string_new ("");
1324 while (g_ascii_isalnum (c) || c == '_')
1326 g_string_append_c (id, c);
1329 *identifier = g_string_free (id, FALSE);
1334 gi_source_scanner_parse_macros (GISourceScanner *scanner, GList *filenames)
1336 GError *error = NULL;
1337 char *tmp_name = NULL;
1339 fdopen (g_file_open_tmp ("gen-introspect-XXXXXX.h", &tmp_name, &error),
1341 g_unlink (tmp_name);
1344 for (l = filenames; l != NULL; l = l->next)
1346 FILE *f = fopen (l->data, "r");
1349 GString *define_line;
1351 gboolean error_line = FALSE;
1352 int c = eat_hspace (f);
1358 c = eat_line (f, c);
1363 /* print current location */
1364 str = g_strescape (l->data, "");
1365 fprintf (fmacros, "# %d \"%s\"\n", line, str);
1369 c = read_identifier (f, c, &str);
1370 if (strcmp (str, "define") != 0 || (c != ' ' && c != '\t'))
1374 c = eat_line (f, c);
1380 c = read_identifier (f, c, &str);
1381 if (strlen (str) == 0 || (c != ' ' && c != '\t' && c != '('))
1385 c = eat_line (f, c);
1389 define_line = g_string_new ("#define ");
1390 g_string_append (define_line, str);
1396 g_string_append_c (define_line, c);
1398 if (c == EOF || c == '\n')
1406 g_string_free (define_line, TRUE);
1408 c = eat_line (f, c);
1413 g_assert (c == ')');
1414 g_string_append_c (define_line, c);
1417 /* found function-like macro */
1418 fprintf (fmacros, "%s\n", define_line->str);
1420 g_string_free (define_line, TRUE);
1421 /* ignore rest of line */
1422 c = eat_line (f, c);
1426 if (c != ' ' && c != '\t')
1428 g_string_free (define_line, TRUE);
1430 c = eat_line (f, c);
1434 while (c != EOF && c != '\n')
1436 g_string_append_c (define_line, c);
1443 /* fold lines when seeing backslash new-line sequence */
1448 g_string_append_c (define_line, '\\');
1453 /* found object-like macro */
1454 fprintf (fmacros, "%s\n", define_line->str);
1456 c = eat_line (f, c);
1464 gi_source_scanner_parse_file (scanner, fmacros);
1468 gi_source_scanner_parse_file (GISourceScanner *scanner, FILE *file)
1470 g_return_val_if_fail (file != NULL, FALSE);
1472 const_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1473 g_free, (GDestroyNotify)gi_source_symbol_unref);
1479 g_hash_table_destroy (const_table);
1488 gi_source_scanner_lex_filename (GISourceScanner *scanner, const gchar *filename)
1490 yyin = fopen (filename, "r");
1492 while (yylex (scanner) != YYEOF)