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
537 | EXTENSION expression
539 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
544 : conditional_expression
547 /* A.2.2 Declarations. */
550 : declaration_specifiers init_declarator_list ';'
553 for (l = $2; l != NULL; l = l->next) {
554 GISourceSymbol *sym = l->data;
555 gi_source_symbol_merge_type (sym, gi_source_type_copy ($1));
556 if ($1->storage_class_specifier & STORAGE_CLASS_TYPEDEF) {
557 sym->type = CSYMBOL_TYPE_TYPEDEF;
558 } else if (sym->base_type->type == CTYPE_FUNCTION) {
559 sym->type = CSYMBOL_TYPE_FUNCTION;
561 sym->type = CSYMBOL_TYPE_OBJECT;
563 gi_source_scanner_add_symbol (scanner, sym);
564 gi_source_symbol_unref (sym);
568 | declaration_specifiers ';'
574 declaration_specifiers
575 : storage_class_specifier declaration_specifiers
578 $$->storage_class_specifier |= $1;
580 | storage_class_specifier
582 $$ = gi_source_type_new (CTYPE_INVALID);
583 $$->storage_class_specifier |= $1;
585 | type_specifier declaration_specifiers
588 /* combine basic types like unsigned int and long long */
589 if ($$->type == CTYPE_BASIC_TYPE && $2->type == CTYPE_BASIC_TYPE) {
590 char *name = g_strdup_printf ("%s %s", $$->name, $2->name);
599 | type_qualifier declaration_specifiers
602 $$->type_qualifier |= $1;
606 $$ = gi_source_type_new (CTYPE_INVALID);
607 $$->type_qualifier |= $1;
609 | function_specifier declaration_specifiers
612 $$->function_specifier |= $1;
616 $$ = gi_source_type_new (CTYPE_INVALID);
617 $$->function_specifier |= $1;
624 $$ = g_list_append (NULL, $1);
626 | init_declarator_list ',' init_declarator
628 $$ = g_list_append ($1, $3);
634 | declarator '=' initializer
637 storage_class_specifier
640 $$ = STORAGE_CLASS_TYPEDEF;
644 $$ = STORAGE_CLASS_EXTERN;
648 $$ = STORAGE_CLASS_STATIC;
652 $$ = STORAGE_CLASS_AUTO;
656 $$ = STORAGE_CLASS_REGISTER;
663 $$ = gi_source_type_new (CTYPE_VOID);
667 $$ = gi_source_basic_type_new ("char");
671 $$ = gi_source_basic_type_new ("short");
675 $$ = gi_source_basic_type_new ("int");
679 $$ = gi_source_basic_type_new ("long");
683 $$ = gi_source_basic_type_new ("float");
687 $$ = gi_source_basic_type_new ("double");
691 $$ = gi_source_basic_type_new ("signed");
695 $$ = gi_source_basic_type_new ("unsigned");
699 $$ = gi_source_basic_type_new ("bool");
701 | struct_or_union_specifier
705 $$ = gi_source_typedef_new ($1);
710 struct_or_union_specifier
711 : struct_or_union identifier_or_typedef_name '{' struct_declaration_list '}'
717 GISourceSymbol *sym = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
718 if ($$->type == CTYPE_STRUCT) {
719 sym->type = CSYMBOL_TYPE_STRUCT;
720 } else if ($$->type == CTYPE_UNION) {
721 sym->type = CSYMBOL_TYPE_UNION;
723 g_assert_not_reached ();
725 sym->ident = g_strdup ($$->name);
726 sym->base_type = gi_source_type_copy ($$);
727 gi_source_scanner_add_symbol (scanner, sym);
728 gi_source_symbol_unref (sym);
730 | struct_or_union '{' struct_declaration_list '}'
735 | struct_or_union identifier_or_typedef_name
745 $$ = gi_source_struct_new (NULL);
749 $$ = gi_source_union_new (NULL);
753 struct_declaration_list
755 | struct_declaration_list struct_declaration
757 $$ = g_list_concat ($1, $2);
762 : specifier_qualifier_list struct_declarator_list ';'
766 for (l = $2; l != NULL; l = l->next)
768 GISourceSymbol *sym = l->data;
769 if ($1->storage_class_specifier & STORAGE_CLASS_TYPEDEF)
770 sym->type = CSYMBOL_TYPE_TYPEDEF;
772 sym->type = CSYMBOL_TYPE_MEMBER;
773 gi_source_symbol_merge_type (sym, gi_source_type_copy ($1));
774 $$ = g_list_append ($$, sym);
780 specifier_qualifier_list
781 : type_specifier specifier_qualifier_list
787 | type_qualifier specifier_qualifier_list
790 $$->type_qualifier |= $1;
794 $$ = gi_source_type_new (CTYPE_INVALID);
795 $$->type_qualifier |= $1;
799 struct_declarator_list
802 $$ = g_list_append (NULL, $1);
804 | struct_declarator_list ',' struct_declarator
806 $$ = g_list_append ($1, $3);
811 : /* empty, support for anonymous structs and unions */
813 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
816 | ':' constant_expression
818 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
820 | declarator ':' constant_expression
823 if ($3->const_int_set) {
824 $$->const_int_set = TRUE;
825 $$->const_int = $3->const_int;
831 : ENUM identifier_or_typedef_name '{' enumerator_list '}'
833 $$ = gi_source_enum_new ($2);
835 $$->is_bitfield = is_bitfield;
836 last_enum_value = -1;
838 | ENUM '{' enumerator_list '}'
840 $$ = gi_source_enum_new (NULL);
842 $$->is_bitfield = is_bitfield;
843 last_enum_value = -1;
845 | ENUM identifier_or_typedef_name '{' enumerator_list ',' '}'
847 $$ = gi_source_enum_new ($2);
849 $$->is_bitfield = is_bitfield;
850 last_enum_value = -1;
852 | ENUM '{' enumerator_list ',' '}'
854 $$ = gi_source_enum_new (NULL);
856 $$->is_bitfield = is_bitfield;
857 last_enum_value = -1;
859 | ENUM identifier_or_typedef_name
861 $$ = gi_source_enum_new ($2);
868 /* reset flag before the first enum value */
873 $$ = g_list_append (NULL, $2);
875 | enumerator_list ',' enumerator
877 $$ = g_list_append ($1, $3);
884 $$ = gi_source_symbol_new (CSYMBOL_TYPE_OBJECT);
886 $$->const_int_set = TRUE;
887 $$->const_int = ++last_enum_value;
888 g_hash_table_insert (const_table, g_strdup ($$->ident), gi_source_symbol_ref ($$));
890 | identifier '=' constant_expression
892 $$ = gi_source_symbol_new (CSYMBOL_TYPE_OBJECT);
894 $$->const_int_set = TRUE;
895 $$->const_int = $3->const_int;
896 last_enum_value = $$->const_int;
897 g_hash_table_insert (const_table, g_strdup ($$->ident), gi_source_symbol_ref ($$));
904 $$ = TYPE_QUALIFIER_CONST;
908 $$ = TYPE_QUALIFIER_RESTRICT;
912 $$ = TYPE_QUALIFIER_EXTENSION;
916 $$ = TYPE_QUALIFIER_VOLATILE;
923 $$ = FUNCTION_INLINE;
928 : pointer direct_declarator
931 gi_source_symbol_merge_type ($$, $1);
939 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
946 | direct_declarator '[' assignment_expression ']'
949 gi_source_symbol_merge_type ($$, gi_source_array_new ($3));
951 | direct_declarator '[' ']'
954 gi_source_symbol_merge_type ($$, gi_source_array_new (NULL));
956 | direct_declarator '(' parameter_list ')'
958 GISourceType *func = gi_source_function_new ();
959 // ignore (void) parameter list
960 if ($3 != NULL && ($3->next != NULL || ((GISourceSymbol *) $3->data)->base_type->type != CTYPE_VOID)) {
961 func->child_list = $3;
964 gi_source_symbol_merge_type ($$, func);
966 | direct_declarator '(' identifier_list ')'
968 GISourceType *func = gi_source_function_new ();
969 func->child_list = $3;
971 gi_source_symbol_merge_type ($$, func);
973 | direct_declarator '(' ')'
975 GISourceType *func = gi_source_function_new ();
977 gi_source_symbol_merge_type ($$, func);
982 : '*' type_qualifier_list
984 $$ = gi_source_pointer_new (NULL);
985 $$->type_qualifier = $2;
989 $$ = gi_source_pointer_new (NULL);
991 | '*' type_qualifier_list pointer
993 $$ = gi_source_pointer_new ($3);
994 $$->type_qualifier = $2;
998 $$ = gi_source_pointer_new ($2);
1004 | type_qualifier_list type_qualifier
1011 : parameter_declaration
1013 $$ = g_list_append (NULL, $1);
1015 | parameter_list ',' parameter_declaration
1017 $$ = g_list_append ($1, $3);
1021 parameter_declaration
1022 : declaration_specifiers declarator
1025 gi_source_symbol_merge_type ($$, $1);
1027 | declaration_specifiers abstract_declarator
1030 gi_source_symbol_merge_type ($$, $1);
1032 | declaration_specifiers
1034 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
1039 $$ = gi_source_symbol_new (CSYMBOL_TYPE_ELLIPSIS);
1046 GISourceSymbol *sym = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
1048 $$ = g_list_append (NULL, sym);
1050 | identifier_list ',' identifier
1052 GISourceSymbol *sym = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
1054 $$ = g_list_append ($1, sym);
1059 : specifier_qualifier_list
1060 | specifier_qualifier_list abstract_declarator
1066 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
1067 gi_source_symbol_merge_type ($$, $1);
1069 | direct_abstract_declarator
1070 | pointer direct_abstract_declarator
1073 gi_source_symbol_merge_type ($$, $1);
1077 direct_abstract_declarator
1078 : '(' abstract_declarator ')'
1084 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
1085 gi_source_symbol_merge_type ($$, gi_source_array_new (NULL));
1087 | '[' assignment_expression ']'
1089 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
1090 gi_source_symbol_merge_type ($$, gi_source_array_new ($2));
1092 | direct_abstract_declarator '[' ']'
1095 gi_source_symbol_merge_type ($$, gi_source_array_new (NULL));
1097 | direct_abstract_declarator '[' assignment_expression ']'
1100 gi_source_symbol_merge_type ($$, gi_source_array_new ($3));
1104 GISourceType *func = gi_source_function_new ();
1105 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
1106 gi_source_symbol_merge_type ($$, func);
1108 | '(' parameter_list ')'
1110 GISourceType *func = gi_source_function_new ();
1111 // ignore (void) parameter list
1112 if ($2 != NULL && ($2->next != NULL || ((GISourceSymbol *) $2->data)->base_type->type != CTYPE_VOID)) {
1113 func->child_list = $2;
1115 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
1116 gi_source_symbol_merge_type ($$, func);
1118 | direct_abstract_declarator '(' ')'
1120 GISourceType *func = gi_source_function_new ();
1122 gi_source_symbol_merge_type ($$, func);
1124 | direct_abstract_declarator '(' parameter_list ')'
1126 GISourceType *func = gi_source_function_new ();
1127 // ignore (void) parameter list
1128 if ($3 != NULL && ($3->next != NULL || ((GISourceSymbol *) $3->data)->base_type->type != CTYPE_VOID)) {
1129 func->child_list = $3;
1132 gi_source_symbol_merge_type ($$, func);
1139 $$ = g_strdup (yytext);
1144 : assignment_expression
1145 | '{' initializer_list '}'
1146 | '{' initializer_list ',' '}'
1151 | initializer_list ',' initializer
1154 /* A.2.3 Statements. */
1158 | compound_statement
1159 | expression_statement
1160 | selection_statement
1161 | iteration_statement
1166 : identifier_or_typedef_name ':' statement
1167 | CASE constant_expression ':' statement
1168 | DEFAULT ':' statement
1173 | '{' block_item_list '}'
1178 | block_item_list block_item
1186 expression_statement
1192 : IF '(' expression ')' statement
1193 | IF '(' expression ')' statement ELSE statement
1194 | SWITCH '(' expression ')' statement
1198 : WHILE '(' expression ')' statement
1199 | DO statement WHILE '(' expression ')' ';'
1200 | FOR '(' ';' ';' ')' statement
1201 | FOR '(' expression ';' ';' ')' statement
1202 | FOR '(' ';' expression ';' ')' statement
1203 | FOR '(' expression ';' expression ';' ')' statement
1204 | FOR '(' ';' ';' expression ')' statement
1205 | FOR '(' expression ';' ';' expression ')' statement
1206 | FOR '(' ';' expression ';' expression ')' statement
1207 | FOR '(' expression ';' expression ';' expression ')' statement
1211 : GOTO identifier_or_typedef_name ';'
1215 | RETURN expression ';'
1218 /* A.2.4 External definitions. */
1221 : external_declaration
1222 | translation_unit external_declaration
1225 external_declaration
1226 : function_definition
1232 : declaration_specifiers declarator declaration_list compound_statement
1233 | declaration_specifiers declarator compound_statement
1238 | declaration_list declaration
1246 $$ = g_strdup (yytext + strlen ("#define "));
1253 $$ = g_strdup (yytext + strlen ("#define "));
1257 function_macro_define
1258 : function_macro '(' identifier_list ')'
1262 : object_macro constant_expression
1264 if ($2->const_int_set || $2->const_double_set || $2->const_string != NULL) {
1266 gi_source_scanner_add_symbol (scanner, $2);
1267 gi_source_symbol_unref ($2);
1273 : function_macro_define
1274 | object_macro_define
1280 yyerror (GISourceScanner *scanner, const char *s)
1282 /* ignore errors while doing a macro scan as not all object macros
1283 * have valid expressions */
1284 if (!scanner->macro_scan)
1286 fprintf(stderr, "%s:%d: %s in '%s' at '%s'\n",
1287 scanner->current_filename, lineno, s, linebuf, yytext);
1292 eat_hspace (FILE * f)
1299 while (c == ' ' || c == '\t');
1304 eat_line (FILE * f, int c)
1306 while (c != EOF && c != '\n')
1313 if (c == ' ' || c == '\t')
1322 read_identifier (FILE * f, int c, char **identifier)
1324 GString *id = g_string_new ("");
1325 while (g_ascii_isalnum (c) || c == '_')
1327 g_string_append_c (id, c);
1330 *identifier = g_string_free (id, FALSE);
1335 gi_source_scanner_parse_macros (GISourceScanner *scanner, GList *filenames)
1337 GError *error = NULL;
1338 char *tmp_name = NULL;
1340 fdopen (g_file_open_tmp ("gen-introspect-XXXXXX.h", &tmp_name, &error),
1342 g_unlink (tmp_name);
1345 for (l = filenames; l != NULL; l = l->next)
1347 FILE *f = fopen (l->data, "r");
1350 GString *define_line;
1352 gboolean error_line = FALSE;
1353 int c = eat_hspace (f);
1359 c = eat_line (f, c);
1364 /* print current location */
1365 str = g_strescape (l->data, "");
1366 fprintf (fmacros, "# %d \"%s\"\n", line, str);
1370 c = read_identifier (f, c, &str);
1371 if (strcmp (str, "define") != 0 || (c != ' ' && c != '\t'))
1375 c = eat_line (f, c);
1381 c = read_identifier (f, c, &str);
1382 if (strlen (str) == 0 || (c != ' ' && c != '\t' && c != '('))
1386 c = eat_line (f, c);
1390 define_line = g_string_new ("#define ");
1391 g_string_append (define_line, str);
1397 g_string_append_c (define_line, c);
1399 if (c == EOF || c == '\n')
1407 g_string_free (define_line, TRUE);
1409 c = eat_line (f, c);
1414 g_assert (c == ')');
1415 g_string_append_c (define_line, c);
1418 /* found function-like macro */
1419 fprintf (fmacros, "%s\n", define_line->str);
1421 g_string_free (define_line, TRUE);
1422 /* ignore rest of line */
1423 c = eat_line (f, c);
1427 if (c != ' ' && c != '\t')
1429 g_string_free (define_line, TRUE);
1431 c = eat_line (f, c);
1435 while (c != EOF && c != '\n')
1437 g_string_append_c (define_line, c);
1444 /* fold lines when seeing backslash new-line sequence */
1449 g_string_append_c (define_line, '\\');
1454 /* found object-like macro */
1455 fprintf (fmacros, "%s\n", define_line->str);
1457 c = eat_line (f, c);
1465 gi_source_scanner_parse_file (scanner, fmacros);
1469 gi_source_scanner_parse_file (GISourceScanner *scanner, FILE *file)
1471 g_return_val_if_fail (file != NULL, FALSE);
1473 const_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1474 g_free, (GDestroyNotify)gi_source_symbol_unref);
1480 g_hash_table_destroy (const_table);
1489 gi_source_scanner_lex_filename (GISourceScanner *scanner, const gchar *filename)
1491 yyin = fopen (filename, "r");
1493 while (yylex (scanner) != YYEOF)