1 /* GObject introspection: C parser
3 * Copyright (c) 1997 Sandro Sigala <ssigala@globalnet.it>
4 * Copyright (c) 2007-2008 Jürg Billeter <j@bitron.ch>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <glib/gstdio.h>
36 #include "sourcescanner.h"
37 #include "scannerparser.h"
41 extern char linebuf[2000];
44 extern int yylex (GISourceScanner *scanner);
45 static void yyerror (GISourceScanner *scanner, const char *str);
47 extern void ctype_free (GISourceType * type);
49 static int last_enum_value = -1;
50 static gboolean is_bitfield;
51 static GHashTable *const_table = NULL;
54 * parse_c_string_literal:
55 * @str: A string containing a C string literal
57 * Based on g_strcompress(), but also handles
58 * hexadecimal escapes.
61 parse_c_string_literal (const char *str)
63 const gchar *p = str, *num;
64 gchar *dest = g_malloc (strlen (str) + 1);
75 g_warning ("parse_c_string_literal: trailing \\");
77 case '0': case '1': case '2': case '3': case '4':
78 case '5': case '6': case '7':
81 while ((p < num + 3) && (*p >= '0') && (*p <= '7'))
83 *q = (*q * 8) + (*p - '0');
93 while ((p < num + 2) && (g_ascii_isxdigit(*p)))
95 *q = (*q * 16) + g_ascii_xdigit_value(*p);
116 default: /* Also handles \" and \\ */
137 GISourceSymbol *symbol;
139 StorageClassSpecifier storage_class_specifier;
140 TypeQualifier type_qualifier;
141 FunctionSpecifier function_specifier;
142 UnaryOperator unary_operator;
145 %parse-param { GISourceScanner* scanner }
146 %lex-param { GISourceScanner* scanner }
148 %token <str> IDENTIFIER "identifier"
149 %token <str> TYPEDEF_NAME "typedef-name"
151 %token INTEGER FLOATING CHARACTER STRING
153 %token ELLIPSIS ADDEQ SUBEQ MULEQ DIVEQ MODEQ XOREQ ANDEQ OREQ SL SR
154 %token SLEQ SREQ EQ NOTEQ LTEQ GTEQ ANDAND OROR PLUSPLUS MINUSMINUS ARROW
156 %token AUTO BOOL BREAK CASE CHAR CONST CONTINUE DEFAULT DO DOUBLE ELSE ENUM
157 %token EXTENSION EXTERN FLOAT FOR GOTO IF INLINE INT LONG REGISTER RESTRICT
158 %token RETURN SHORT SIGNED SIZEOF STATIC STRUCT SWITCH TYPEDEF UNION UNSIGNED
159 %token VOID VOLATILE WHILE
161 %token FUNCTION_MACRO OBJECT_MACRO
163 %start translation_unit
165 %type <ctype> declaration_specifiers
166 %type <ctype> enum_specifier
167 %type <ctype> pointer
168 %type <ctype> specifier_qualifier_list
169 %type <ctype> type_name
170 %type <ctype> struct_or_union
171 %type <ctype> struct_or_union_specifier
172 %type <ctype> type_specifier
173 %type <str> identifier
174 %type <str> typedef_name
175 %type <str> identifier_or_typedef_name
176 %type <symbol> abstract_declarator
177 %type <symbol> init_declarator
178 %type <symbol> declarator
179 %type <symbol> enumerator
180 %type <symbol> direct_abstract_declarator
181 %type <symbol> direct_declarator
182 %type <symbol> parameter_declaration
183 %type <symbol> struct_declarator
184 %type <list> enumerator_list
185 %type <list> identifier_list
186 %type <list> init_declarator_list
187 %type <list> parameter_list
188 %type <list> struct_declaration
189 %type <list> struct_declaration_list
190 %type <list> struct_declarator_list
191 %type <storage_class_specifier> storage_class_specifier
192 %type <type_qualifier> type_qualifier
193 %type <type_qualifier> type_qualifier_list
194 %type <function_specifier> function_specifier
195 %type <symbol> expression
196 %type <symbol> constant_expression
197 %type <symbol> conditional_expression
198 %type <symbol> logical_and_expression
199 %type <symbol> logical_or_expression
200 %type <symbol> inclusive_or_expression
201 %type <symbol> exclusive_or_expression
202 %type <symbol> multiplicative_expression
203 %type <symbol> additive_expression
204 %type <symbol> shift_expression
205 %type <symbol> relational_expression
206 %type <symbol> equality_expression
207 %type <symbol> and_expression
208 %type <symbol> cast_expression
209 %type <symbol> assignment_expression
210 %type <symbol> unary_expression
211 %type <symbol> postfix_expression
212 %type <symbol> primary_expression
213 %type <unary_operator> unary_operator
214 %type <str> function_macro
215 %type <str> object_macro
216 %type <symbol> strings
220 /* A.2.1 Expressions. */
225 $$ = g_hash_table_lookup (const_table, $1);
227 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
229 $$ = gi_source_symbol_ref ($$);
234 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
235 $$->const_int_set = TRUE;
236 if (g_str_has_prefix (yytext, "0x") && strlen (yytext) > 2) {
237 $$->const_int = g_ascii_strtoll (yytext + 2, NULL, 16);
238 } else if (g_str_has_prefix (yytext, "0") && strlen (yytext) > 1) {
239 $$->const_int = g_ascii_strtoll (yytext + 1, NULL, 8);
241 $$->const_int = g_ascii_strtoll (yytext, NULL, 10);
246 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
247 $$->const_int_set = TRUE;
248 $$->const_int = g_utf8_get_char(yytext + 1);
252 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
253 $$->const_double_set = TRUE;
254 $$->const_double = 0.0;
255 sscanf (yytext, "%lf", &($$->const_double));
264 /* concatenate adjacent string literal tokens */
268 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
269 yytext[strlen (yytext) - 1] = '\0';
270 $$->const_string = parse_c_string_literal (yytext + 1);
271 if (!g_utf8_validate ($$->const_string, -1, NULL))
274 g_warning ("Ignoring non-UTF-8 constant string \"%s\"", yytext + 1);
276 g_free($$->const_string);
277 $$->const_string = NULL;
283 char *strings, *string2;
285 yytext[strlen (yytext) - 1] = '\0';
286 string2 = parse_c_string_literal (yytext + 1);
287 strings = g_strconcat ($$->const_string, string2, NULL);
288 g_free ($$->const_string);
290 $$->const_string = strings;
297 $$ = g_strdup (yytext);
301 identifier_or_typedef_name
308 | postfix_expression '[' expression ']'
310 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
312 | postfix_expression '(' argument_expression_list ')'
314 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
316 | postfix_expression '(' ')'
318 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
320 | postfix_expression '.' identifier_or_typedef_name
322 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
324 | postfix_expression ARROW identifier_or_typedef_name
326 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
328 | postfix_expression PLUSPLUS
330 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
332 | postfix_expression MINUSMINUS
334 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
338 argument_expression_list
339 : assignment_expression
340 | argument_expression_list ',' assignment_expression
345 | PLUSPLUS unary_expression
347 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
349 | MINUSMINUS unary_expression
351 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
353 | unary_operator cast_expression
361 $$->const_int = -$2->const_int;
363 case UNARY_BITWISE_COMPLEMENT:
365 $$->const_int = ~$2->const_int;
367 case UNARY_LOGICAL_NEGATION:
369 $$->const_int = !gi_source_symbol_get_const_boolean ($2);
372 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
376 | SIZEOF unary_expression
378 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
380 | SIZEOF '(' type_name ')'
383 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
390 $$ = UNARY_ADDRESS_OF;
394 $$ = UNARY_POINTER_INDIRECTION;
406 $$ = UNARY_BITWISE_COMPLEMENT;
410 $$ = UNARY_LOGICAL_NEGATION;
416 | '(' type_name ')' cast_expression
423 multiplicative_expression
425 | multiplicative_expression '*' cast_expression
427 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
428 $$->const_int_set = TRUE;
429 $$->const_int = $1->const_int * $3->const_int;
431 | multiplicative_expression '/' cast_expression
433 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
434 $$->const_int_set = TRUE;
435 if ($3->const_int != 0) {
436 $$->const_int = $1->const_int / $3->const_int;
439 | multiplicative_expression '%' cast_expression
441 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
442 $$->const_int_set = TRUE;
443 if ($3->const_int != 0) {
444 $$->const_int = $1->const_int % $3->const_int;
450 : multiplicative_expression
451 | additive_expression '+' multiplicative_expression
453 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
454 $$->const_int_set = TRUE;
455 $$->const_int = $1->const_int + $3->const_int;
457 | additive_expression '-' multiplicative_expression
459 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
460 $$->const_int_set = TRUE;
461 $$->const_int = $1->const_int - $3->const_int;
466 : additive_expression
467 | shift_expression SL additive_expression
469 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
470 $$->const_int_set = TRUE;
471 $$->const_int = $1->const_int << $3->const_int;
473 /* assume this is a bitfield/flags declaration
474 * if a left shift operator is sued in an enum value
475 * This mimics the glib-mkenum behavior.
479 | shift_expression SR additive_expression
481 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
482 $$->const_int_set = TRUE;
483 $$->const_int = $1->const_int >> $3->const_int;
487 relational_expression
489 | relational_expression '<' shift_expression
491 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
492 $$->const_int_set = TRUE;
493 $$->const_int = $1->const_int < $3->const_int;
495 | relational_expression '>' shift_expression
497 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
498 $$->const_int_set = TRUE;
499 $$->const_int = $1->const_int > $3->const_int;
501 | relational_expression LTEQ shift_expression
503 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
504 $$->const_int_set = TRUE;
505 $$->const_int = $1->const_int <= $3->const_int;
507 | relational_expression GTEQ shift_expression
509 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
510 $$->const_int_set = TRUE;
511 $$->const_int = $1->const_int >= $3->const_int;
516 : relational_expression
517 | equality_expression EQ relational_expression
519 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
520 $$->const_int_set = TRUE;
521 $$->const_int = $1->const_int == $3->const_int;
523 | equality_expression NOTEQ relational_expression
525 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
526 $$->const_int_set = TRUE;
527 $$->const_int = $1->const_int != $3->const_int;
532 : equality_expression
533 | and_expression '&' equality_expression
535 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
536 $$->const_int_set = TRUE;
537 $$->const_int = $1->const_int & $3->const_int;
541 exclusive_or_expression
543 | exclusive_or_expression '^' and_expression
545 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
546 $$->const_int_set = TRUE;
547 $$->const_int = $1->const_int ^ $3->const_int;
551 inclusive_or_expression
552 : exclusive_or_expression
553 | inclusive_or_expression '|' exclusive_or_expression
555 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
556 $$->const_int_set = TRUE;
557 $$->const_int = $1->const_int | $3->const_int;
561 logical_and_expression
562 : inclusive_or_expression
563 | logical_and_expression ANDAND inclusive_or_expression
565 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
566 $$->const_int_set = TRUE;
568 gi_source_symbol_get_const_boolean ($1) &&
569 gi_source_symbol_get_const_boolean ($3);
573 logical_or_expression
574 : logical_and_expression
575 | logical_or_expression OROR logical_and_expression
577 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
578 $$->const_int_set = TRUE;
580 gi_source_symbol_get_const_boolean ($1) ||
581 gi_source_symbol_get_const_boolean ($3);
585 conditional_expression
586 : logical_or_expression
587 | logical_or_expression '?' expression ':' conditional_expression
589 $$ = gi_source_symbol_get_const_boolean ($1) ? $3 : $5;
593 assignment_expression
594 : conditional_expression
595 | unary_expression assignment_operator assignment_expression
597 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
616 : assignment_expression
617 | expression ',' assignment_expression
618 | EXTENSION expression
620 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
625 : conditional_expression
628 /* A.2.2 Declarations. */
631 : declaration_specifiers init_declarator_list ';'
634 for (l = $2; l != NULL; l = l->next) {
635 GISourceSymbol *sym = l->data;
636 gi_source_symbol_merge_type (sym, gi_source_type_copy ($1));
637 if ($1->storage_class_specifier & STORAGE_CLASS_TYPEDEF) {
638 sym->type = CSYMBOL_TYPE_TYPEDEF;
639 } else if (sym->base_type->type == CTYPE_FUNCTION) {
640 sym->type = CSYMBOL_TYPE_FUNCTION;
642 sym->type = CSYMBOL_TYPE_OBJECT;
644 gi_source_scanner_add_symbol (scanner, sym);
645 gi_source_symbol_unref (sym);
649 | declaration_specifiers ';'
655 declaration_specifiers
656 : storage_class_specifier declaration_specifiers
659 $$->storage_class_specifier |= $1;
661 | storage_class_specifier
663 $$ = gi_source_type_new (CTYPE_INVALID);
664 $$->storage_class_specifier |= $1;
666 | type_specifier declaration_specifiers
669 /* combine basic types like unsigned int and long long */
670 if ($$->type == CTYPE_BASIC_TYPE && $2->type == CTYPE_BASIC_TYPE) {
671 char *name = g_strdup_printf ("%s %s", $$->name, $2->name);
680 | type_qualifier declaration_specifiers
683 $$->type_qualifier |= $1;
687 $$ = gi_source_type_new (CTYPE_INVALID);
688 $$->type_qualifier |= $1;
690 | function_specifier declaration_specifiers
693 $$->function_specifier |= $1;
697 $$ = gi_source_type_new (CTYPE_INVALID);
698 $$->function_specifier |= $1;
705 $$ = g_list_append (NULL, $1);
707 | init_declarator_list ',' init_declarator
709 $$ = g_list_append ($1, $3);
715 | declarator '=' initializer
718 storage_class_specifier
721 $$ = STORAGE_CLASS_TYPEDEF;
725 $$ = STORAGE_CLASS_EXTERN;
729 $$ = STORAGE_CLASS_STATIC;
733 $$ = STORAGE_CLASS_AUTO;
737 $$ = STORAGE_CLASS_REGISTER;
744 $$ = gi_source_type_new (CTYPE_VOID);
748 $$ = gi_source_basic_type_new ("char");
752 $$ = gi_source_basic_type_new ("short");
756 $$ = gi_source_basic_type_new ("int");
760 $$ = gi_source_basic_type_new ("long");
764 $$ = gi_source_basic_type_new ("float");
768 $$ = gi_source_basic_type_new ("double");
772 $$ = gi_source_basic_type_new ("signed");
776 $$ = gi_source_basic_type_new ("unsigned");
780 $$ = gi_source_basic_type_new ("bool");
782 | struct_or_union_specifier
786 $$ = gi_source_typedef_new ($1);
791 struct_or_union_specifier
792 : struct_or_union identifier_or_typedef_name '{' struct_declaration_list '}'
798 GISourceSymbol *sym = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
799 if ($$->type == CTYPE_STRUCT) {
800 sym->type = CSYMBOL_TYPE_STRUCT;
801 } else if ($$->type == CTYPE_UNION) {
802 sym->type = CSYMBOL_TYPE_UNION;
804 g_assert_not_reached ();
806 sym->ident = g_strdup ($$->name);
807 sym->base_type = gi_source_type_copy ($$);
808 gi_source_scanner_add_symbol (scanner, sym);
809 gi_source_symbol_unref (sym);
811 | struct_or_union '{' struct_declaration_list '}'
816 | struct_or_union identifier_or_typedef_name
826 scanner->private = FALSE;
827 $$ = gi_source_struct_new (NULL);
831 scanner->private = FALSE;
832 $$ = gi_source_union_new (NULL);
836 struct_declaration_list
838 | struct_declaration_list struct_declaration
840 $$ = g_list_concat ($1, $2);
845 : specifier_qualifier_list struct_declarator_list ';'
849 for (l = $2; l != NULL; l = l->next)
851 GISourceSymbol *sym = l->data;
852 if ($1->storage_class_specifier & STORAGE_CLASS_TYPEDEF)
853 sym->type = CSYMBOL_TYPE_TYPEDEF;
855 sym->type = CSYMBOL_TYPE_MEMBER;
856 gi_source_symbol_merge_type (sym, gi_source_type_copy ($1));
857 sym->private = scanner->private;
858 $$ = g_list_append ($$, sym);
864 specifier_qualifier_list
865 : type_specifier specifier_qualifier_list
871 | type_qualifier specifier_qualifier_list
874 $$->type_qualifier |= $1;
878 $$ = gi_source_type_new (CTYPE_INVALID);
879 $$->type_qualifier |= $1;
883 struct_declarator_list
886 $$ = g_list_append (NULL, $1);
888 | struct_declarator_list ',' struct_declarator
890 $$ = g_list_append ($1, $3);
895 : /* empty, support for anonymous structs and unions */
897 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
900 | ':' constant_expression
902 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
904 | declarator ':' constant_expression
907 if ($3->const_int_set) {
908 $$->const_int_set = TRUE;
909 $$->const_int = $3->const_int;
915 : enum_keyword identifier_or_typedef_name '{' enumerator_list '}'
917 $$ = gi_source_enum_new ($2);
919 $$->is_bitfield = is_bitfield || scanner->flags;
920 last_enum_value = -1;
922 | enum_keyword '{' enumerator_list '}'
924 $$ = gi_source_enum_new (NULL);
926 $$->is_bitfield = is_bitfield || scanner->flags;
927 last_enum_value = -1;
929 | enum_keyword identifier_or_typedef_name '{' enumerator_list ',' '}'
931 $$ = gi_source_enum_new ($2);
933 $$->is_bitfield = is_bitfield || scanner->flags;
934 last_enum_value = -1;
936 | enum_keyword '{' enumerator_list ',' '}'
938 $$ = gi_source_enum_new (NULL);
940 $$->is_bitfield = is_bitfield || scanner->flags;
941 last_enum_value = -1;
943 | enum_keyword identifier_or_typedef_name
945 $$ = gi_source_enum_new ($2);
952 scanner->flags = FALSE;
953 scanner->private = FALSE;
960 /* reset flag before the first enum value */
965 $2->private = scanner->private;
966 $$ = g_list_append (NULL, $2);
968 | enumerator_list ',' enumerator
970 $3->private = scanner->private;
971 $$ = g_list_append ($1, $3);
978 $$ = gi_source_symbol_new (CSYMBOL_TYPE_OBJECT, lineno);
980 $$->const_int_set = TRUE;
981 $$->const_int = ++last_enum_value;
982 g_hash_table_insert (const_table, g_strdup ($$->ident), gi_source_symbol_ref ($$));
984 | identifier '=' constant_expression
986 $$ = gi_source_symbol_new (CSYMBOL_TYPE_OBJECT, lineno);
988 $$->const_int_set = TRUE;
989 $$->const_int = $3->const_int;
990 last_enum_value = $$->const_int;
991 g_hash_table_insert (const_table, g_strdup ($$->ident), gi_source_symbol_ref ($$));
998 $$ = TYPE_QUALIFIER_CONST;
1002 $$ = TYPE_QUALIFIER_RESTRICT;
1006 $$ = TYPE_QUALIFIER_EXTENSION;
1010 $$ = TYPE_QUALIFIER_VOLATILE;
1017 $$ = FUNCTION_INLINE;
1022 : pointer direct_declarator
1025 gi_source_symbol_merge_type ($$, $1);
1033 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
1036 | '(' declarator ')'
1040 | direct_declarator '[' assignment_expression ']'
1043 gi_source_symbol_merge_type ($$, gi_source_array_new ($3));
1045 | direct_declarator '[' ']'
1048 gi_source_symbol_merge_type ($$, gi_source_array_new (NULL));
1050 | direct_declarator '(' parameter_list ')'
1052 GISourceType *func = gi_source_function_new ();
1053 // ignore (void) parameter list
1054 if ($3 != NULL && ($3->next != NULL || ((GISourceSymbol *) $3->data)->base_type->type != CTYPE_VOID)) {
1055 func->child_list = $3;
1058 gi_source_symbol_merge_type ($$, func);
1060 | direct_declarator '(' identifier_list ')'
1062 GISourceType *func = gi_source_function_new ();
1063 func->child_list = $3;
1065 gi_source_symbol_merge_type ($$, func);
1067 | direct_declarator '(' ')'
1069 GISourceType *func = gi_source_function_new ();
1071 gi_source_symbol_merge_type ($$, func);
1076 : '*' type_qualifier_list
1078 $$ = gi_source_pointer_new (NULL);
1079 $$->type_qualifier = $2;
1083 $$ = gi_source_pointer_new (NULL);
1085 | '*' type_qualifier_list pointer
1087 $$ = gi_source_pointer_new ($3);
1088 $$->type_qualifier = $2;
1092 $$ = gi_source_pointer_new ($2);
1098 | type_qualifier_list type_qualifier
1105 : parameter_declaration
1107 $$ = g_list_append (NULL, $1);
1109 | parameter_list ',' parameter_declaration
1111 $$ = g_list_append ($1, $3);
1115 parameter_declaration
1116 : declaration_specifiers declarator
1119 gi_source_symbol_merge_type ($$, $1);
1121 | declaration_specifiers abstract_declarator
1124 gi_source_symbol_merge_type ($$, $1);
1126 | declaration_specifiers
1128 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
1133 $$ = gi_source_symbol_new (CSYMBOL_TYPE_ELLIPSIS, lineno);
1140 GISourceSymbol *sym = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
1142 $$ = g_list_append (NULL, sym);
1144 | identifier_list ',' identifier
1146 GISourceSymbol *sym = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
1148 $$ = g_list_append ($1, sym);
1153 : specifier_qualifier_list
1154 | specifier_qualifier_list abstract_declarator
1160 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
1161 gi_source_symbol_merge_type ($$, $1);
1163 | direct_abstract_declarator
1164 | pointer direct_abstract_declarator
1167 gi_source_symbol_merge_type ($$, $1);
1171 direct_abstract_declarator
1172 : '(' abstract_declarator ')'
1178 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
1179 gi_source_symbol_merge_type ($$, gi_source_array_new (NULL));
1181 | '[' assignment_expression ']'
1183 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
1184 gi_source_symbol_merge_type ($$, gi_source_array_new ($2));
1186 | direct_abstract_declarator '[' ']'
1189 gi_source_symbol_merge_type ($$, gi_source_array_new (NULL));
1191 | direct_abstract_declarator '[' assignment_expression ']'
1194 gi_source_symbol_merge_type ($$, gi_source_array_new ($3));
1198 GISourceType *func = gi_source_function_new ();
1199 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
1200 gi_source_symbol_merge_type ($$, func);
1202 | '(' parameter_list ')'
1204 GISourceType *func = gi_source_function_new ();
1205 // ignore (void) parameter list
1206 if ($2 != NULL && ($2->next != NULL || ((GISourceSymbol *) $2->data)->base_type->type != CTYPE_VOID)) {
1207 func->child_list = $2;
1209 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
1210 gi_source_symbol_merge_type ($$, func);
1212 | direct_abstract_declarator '(' ')'
1214 GISourceType *func = gi_source_function_new ();
1216 gi_source_symbol_merge_type ($$, func);
1218 | direct_abstract_declarator '(' parameter_list ')'
1220 GISourceType *func = gi_source_function_new ();
1221 // ignore (void) parameter list
1222 if ($3 != NULL && ($3->next != NULL || ((GISourceSymbol *) $3->data)->base_type->type != CTYPE_VOID)) {
1223 func->child_list = $3;
1226 gi_source_symbol_merge_type ($$, func);
1233 $$ = g_strdup (yytext);
1238 : assignment_expression
1239 | '{' initializer_list '}'
1240 | '{' initializer_list ',' '}'
1245 | initializer_list ',' initializer
1248 /* A.2.3 Statements. */
1252 | compound_statement
1253 | expression_statement
1254 | selection_statement
1255 | iteration_statement
1260 : identifier_or_typedef_name ':' statement
1261 | CASE constant_expression ':' statement
1262 | DEFAULT ':' statement
1267 | '{' block_item_list '}'
1272 | block_item_list block_item
1280 expression_statement
1286 : IF '(' expression ')' statement
1287 | IF '(' expression ')' statement ELSE statement
1288 | SWITCH '(' expression ')' statement
1292 : WHILE '(' expression ')' statement
1293 | DO statement WHILE '(' expression ')' ';'
1294 | FOR '(' ';' ';' ')' statement
1295 | FOR '(' expression ';' ';' ')' statement
1296 | FOR '(' ';' expression ';' ')' statement
1297 | FOR '(' expression ';' expression ';' ')' statement
1298 | FOR '(' ';' ';' expression ')' statement
1299 | FOR '(' expression ';' ';' expression ')' statement
1300 | FOR '(' ';' expression ';' expression ')' statement
1301 | FOR '(' expression ';' expression ';' expression ')' statement
1305 : GOTO identifier_or_typedef_name ';'
1309 | RETURN expression ';'
1312 /* A.2.4 External definitions. */
1315 : external_declaration
1316 | translation_unit external_declaration
1319 external_declaration
1320 : function_definition
1326 : declaration_specifiers declarator declaration_list compound_statement
1327 | declaration_specifiers declarator compound_statement
1332 | declaration_list declaration
1340 $$ = g_strdup (yytext + strlen ("#define "));
1347 $$ = g_strdup (yytext + strlen ("#define "));
1351 function_macro_define
1352 : function_macro '(' identifier_list ')'
1356 : object_macro constant_expression
1358 if ($2->const_int_set || $2->const_double_set || $2->const_string != NULL) {
1360 gi_source_scanner_add_symbol (scanner, $2);
1361 gi_source_symbol_unref ($2);
1367 : function_macro_define
1368 | object_macro_define
1374 yyerror (GISourceScanner *scanner, const char *s)
1376 /* ignore errors while doing a macro scan as not all object macros
1377 * have valid expressions */
1378 if (!scanner->macro_scan)
1380 fprintf(stderr, "%s:%d: %s in '%s' at '%s'\n",
1381 scanner->current_filename, lineno, s, linebuf, yytext);
1386 eat_hspace (FILE * f)
1393 while (c == ' ' || c == '\t');
1398 eat_line (FILE * f, int c)
1400 while (c != EOF && c != '\n')
1407 if (c == ' ' || c == '\t')
1416 read_identifier (FILE * f, int c, char **identifier)
1418 GString *id = g_string_new ("");
1419 while (g_ascii_isalnum (c) || c == '_')
1421 g_string_append_c (id, c);
1424 *identifier = g_string_free (id, FALSE);
1429 gi_source_scanner_parse_macros (GISourceScanner *scanner, GList *filenames)
1431 GError *error = NULL;
1432 char *tmp_name = NULL;
1434 fdopen (g_file_open_tmp ("gen-introspect-XXXXXX.h", &tmp_name, &error),
1436 g_unlink (tmp_name);
1439 for (l = filenames; l != NULL; l = l->next)
1441 FILE *f = fopen (l->data, "r");
1444 GString *define_line;
1446 gboolean error_line = FALSE;
1447 int c = eat_hspace (f);
1453 c = eat_line (f, c);
1458 /* print current location */
1459 str = g_strescape (l->data, "");
1460 fprintf (fmacros, "# %d \"%s\"\n", line, str);
1464 c = read_identifier (f, c, &str);
1465 if (strcmp (str, "define") != 0 || (c != ' ' && c != '\t'))
1469 c = eat_line (f, c);
1475 c = read_identifier (f, c, &str);
1476 if (strlen (str) == 0 || (c != ' ' && c != '\t' && c != '('))
1480 c = eat_line (f, c);
1484 define_line = g_string_new ("#define ");
1485 g_string_append (define_line, str);
1491 g_string_append_c (define_line, c);
1493 if (c == EOF || c == '\n')
1501 g_string_free (define_line, TRUE);
1503 c = eat_line (f, c);
1508 g_assert (c == ')');
1509 g_string_append_c (define_line, c);
1512 /* found function-like macro */
1513 fprintf (fmacros, "%s\n", define_line->str);
1515 g_string_free (define_line, TRUE);
1516 /* ignore rest of line */
1517 c = eat_line (f, c);
1521 if (c != ' ' && c != '\t')
1523 g_string_free (define_line, TRUE);
1525 c = eat_line (f, c);
1529 while (c != EOF && c != '\n')
1531 g_string_append_c (define_line, c);
1538 /* fold lines when seeing backslash new-line sequence */
1543 g_string_append_c (define_line, '\\');
1548 /* found object-like macro */
1549 fprintf (fmacros, "%s\n", define_line->str);
1551 c = eat_line (f, c);
1559 gi_source_scanner_parse_file (scanner, fmacros);
1563 gi_source_scanner_parse_file (GISourceScanner *scanner, FILE *file)
1565 g_return_val_if_fail (file != NULL, FALSE);
1567 const_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1568 g_free, (GDestroyNotify)gi_source_symbol_unref);
1574 g_hash_table_destroy (const_table);
1583 gi_source_scanner_lex_filename (GISourceScanner *scanner, const gchar *filename)
1586 yyin = fopen (filename, "r");
1588 while (yylex (scanner) != YYEOF)