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_INVALID, lineno);
250 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
251 $$->const_double_set = TRUE;
252 $$->const_double = 0.0;
253 sscanf (yytext, "%lf", &($$->const_double));
262 /* concatenate adjacent string literal tokens */
266 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
267 yytext[strlen (yytext) - 1] = '\0';
268 $$->const_string = parse_c_string_literal (yytext + 1);
269 if (!g_utf8_validate ($$->const_string, -1, NULL))
272 g_warning ("Ignoring non-UTF-8 constant string \"%s\"", yytext + 1);
274 g_free($$->const_string);
275 $$->const_string = NULL;
281 char *strings, *string2;
283 yytext[strlen (yytext) - 1] = '\0';
284 string2 = parse_c_string_literal (yytext + 1);
285 strings = g_strconcat ($$->const_string, string2, NULL);
286 g_free ($$->const_string);
288 $$->const_string = strings;
295 $$ = g_strdup (yytext);
299 identifier_or_typedef_name
306 | postfix_expression '[' expression ']'
308 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
310 | postfix_expression '(' argument_expression_list ')'
312 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
314 | postfix_expression '(' ')'
316 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
318 | postfix_expression '.' identifier_or_typedef_name
320 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
322 | postfix_expression ARROW identifier_or_typedef_name
324 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
326 | postfix_expression PLUSPLUS
328 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
330 | postfix_expression MINUSMINUS
332 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
336 argument_expression_list
337 : assignment_expression
338 | argument_expression_list ',' assignment_expression
343 | PLUSPLUS unary_expression
345 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
347 | MINUSMINUS unary_expression
349 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
351 | unary_operator cast_expression
359 $$->const_int = -$2->const_int;
361 case UNARY_BITWISE_COMPLEMENT:
363 $$->const_int = ~$2->const_int;
365 case UNARY_LOGICAL_NEGATION:
367 $$->const_int = !gi_source_symbol_get_const_boolean ($2);
370 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
374 | SIZEOF unary_expression
376 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
378 | SIZEOF '(' type_name ')'
381 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
388 $$ = UNARY_ADDRESS_OF;
392 $$ = UNARY_POINTER_INDIRECTION;
404 $$ = UNARY_BITWISE_COMPLEMENT;
408 $$ = UNARY_LOGICAL_NEGATION;
414 | '(' type_name ')' cast_expression
421 multiplicative_expression
423 | multiplicative_expression '*' cast_expression
425 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
426 $$->const_int_set = TRUE;
427 $$->const_int = $1->const_int * $3->const_int;
429 | multiplicative_expression '/' cast_expression
431 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
432 $$->const_int_set = TRUE;
433 if ($3->const_int != 0) {
434 $$->const_int = $1->const_int / $3->const_int;
437 | multiplicative_expression '%' cast_expression
439 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
440 $$->const_int_set = TRUE;
441 if ($3->const_int != 0) {
442 $$->const_int = $1->const_int % $3->const_int;
448 : multiplicative_expression
449 | additive_expression '+' multiplicative_expression
451 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
452 $$->const_int_set = TRUE;
453 $$->const_int = $1->const_int + $3->const_int;
455 | additive_expression '-' multiplicative_expression
457 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
458 $$->const_int_set = TRUE;
459 $$->const_int = $1->const_int - $3->const_int;
464 : additive_expression
465 | shift_expression SL additive_expression
467 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
468 $$->const_int_set = TRUE;
469 $$->const_int = $1->const_int << $3->const_int;
471 /* assume this is a bitfield/flags declaration
472 * if a left shift operator is sued in an enum value
473 * This mimics the glib-mkenum behavior.
477 | shift_expression SR additive_expression
479 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
480 $$->const_int_set = TRUE;
481 $$->const_int = $1->const_int >> $3->const_int;
485 relational_expression
487 | relational_expression '<' shift_expression
489 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
490 $$->const_int_set = TRUE;
491 $$->const_int = $1->const_int < $3->const_int;
493 | relational_expression '>' shift_expression
495 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
496 $$->const_int_set = TRUE;
497 $$->const_int = $1->const_int > $3->const_int;
499 | relational_expression LTEQ shift_expression
501 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
502 $$->const_int_set = TRUE;
503 $$->const_int = $1->const_int <= $3->const_int;
505 | relational_expression GTEQ shift_expression
507 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
508 $$->const_int_set = TRUE;
509 $$->const_int = $1->const_int >= $3->const_int;
514 : relational_expression
515 | equality_expression EQ relational_expression
517 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
518 $$->const_int_set = TRUE;
519 $$->const_int = $1->const_int == $3->const_int;
521 | equality_expression NOTEQ relational_expression
523 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
524 $$->const_int_set = TRUE;
525 $$->const_int = $1->const_int != $3->const_int;
530 : equality_expression
531 | and_expression '&' equality_expression
533 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
534 $$->const_int_set = TRUE;
535 $$->const_int = $1->const_int & $3->const_int;
539 exclusive_or_expression
541 | exclusive_or_expression '^' and_expression
543 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
544 $$->const_int_set = TRUE;
545 $$->const_int = $1->const_int ^ $3->const_int;
549 inclusive_or_expression
550 : exclusive_or_expression
551 | inclusive_or_expression '|' exclusive_or_expression
553 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
554 $$->const_int_set = TRUE;
555 $$->const_int = $1->const_int | $3->const_int;
559 logical_and_expression
560 : inclusive_or_expression
561 | logical_and_expression ANDAND inclusive_or_expression
563 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
564 $$->const_int_set = TRUE;
566 gi_source_symbol_get_const_boolean ($1) &&
567 gi_source_symbol_get_const_boolean ($3);
571 logical_or_expression
572 : logical_and_expression
573 | logical_or_expression OROR logical_and_expression
575 $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
576 $$->const_int_set = TRUE;
578 gi_source_symbol_get_const_boolean ($1) ||
579 gi_source_symbol_get_const_boolean ($3);
583 conditional_expression
584 : logical_or_expression
585 | logical_or_expression '?' expression ':' conditional_expression
587 $$ = gi_source_symbol_get_const_boolean ($1) ? $3 : $5;
591 assignment_expression
592 : conditional_expression
593 | unary_expression assignment_operator assignment_expression
595 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
614 : assignment_expression
615 | expression ',' assignment_expression
616 | EXTENSION expression
618 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
623 : conditional_expression
626 /* A.2.2 Declarations. */
629 : declaration_specifiers init_declarator_list ';'
632 for (l = $2; l != NULL; l = l->next) {
633 GISourceSymbol *sym = l->data;
634 gi_source_symbol_merge_type (sym, gi_source_type_copy ($1));
635 if ($1->storage_class_specifier & STORAGE_CLASS_TYPEDEF) {
636 sym->type = CSYMBOL_TYPE_TYPEDEF;
637 } else if (sym->base_type->type == CTYPE_FUNCTION) {
638 sym->type = CSYMBOL_TYPE_FUNCTION;
640 sym->type = CSYMBOL_TYPE_OBJECT;
642 gi_source_scanner_add_symbol (scanner, sym);
643 gi_source_symbol_unref (sym);
647 | declaration_specifiers ';'
653 declaration_specifiers
654 : storage_class_specifier declaration_specifiers
657 $$->storage_class_specifier |= $1;
659 | storage_class_specifier
661 $$ = gi_source_type_new (CTYPE_INVALID);
662 $$->storage_class_specifier |= $1;
664 | type_specifier declaration_specifiers
667 /* combine basic types like unsigned int and long long */
668 if ($$->type == CTYPE_BASIC_TYPE && $2->type == CTYPE_BASIC_TYPE) {
669 char *name = g_strdup_printf ("%s %s", $$->name, $2->name);
678 | type_qualifier declaration_specifiers
681 $$->type_qualifier |= $1;
685 $$ = gi_source_type_new (CTYPE_INVALID);
686 $$->type_qualifier |= $1;
688 | function_specifier declaration_specifiers
691 $$->function_specifier |= $1;
695 $$ = gi_source_type_new (CTYPE_INVALID);
696 $$->function_specifier |= $1;
703 $$ = g_list_append (NULL, $1);
705 | init_declarator_list ',' init_declarator
707 $$ = g_list_append ($1, $3);
713 | declarator '=' initializer
716 storage_class_specifier
719 $$ = STORAGE_CLASS_TYPEDEF;
723 $$ = STORAGE_CLASS_EXTERN;
727 $$ = STORAGE_CLASS_STATIC;
731 $$ = STORAGE_CLASS_AUTO;
735 $$ = STORAGE_CLASS_REGISTER;
742 $$ = gi_source_type_new (CTYPE_VOID);
746 $$ = gi_source_basic_type_new ("char");
750 $$ = gi_source_basic_type_new ("short");
754 $$ = gi_source_basic_type_new ("int");
758 $$ = gi_source_basic_type_new ("long");
762 $$ = gi_source_basic_type_new ("float");
766 $$ = gi_source_basic_type_new ("double");
770 $$ = gi_source_basic_type_new ("signed");
774 $$ = gi_source_basic_type_new ("unsigned");
778 $$ = gi_source_basic_type_new ("bool");
780 | struct_or_union_specifier
784 $$ = gi_source_typedef_new ($1);
789 struct_or_union_specifier
790 : struct_or_union identifier_or_typedef_name '{' struct_declaration_list '}'
796 GISourceSymbol *sym = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
797 if ($$->type == CTYPE_STRUCT) {
798 sym->type = CSYMBOL_TYPE_STRUCT;
799 } else if ($$->type == CTYPE_UNION) {
800 sym->type = CSYMBOL_TYPE_UNION;
802 g_assert_not_reached ();
804 sym->ident = g_strdup ($$->name);
805 sym->base_type = gi_source_type_copy ($$);
806 gi_source_scanner_add_symbol (scanner, sym);
807 gi_source_symbol_unref (sym);
809 | struct_or_union '{' struct_declaration_list '}'
814 | struct_or_union identifier_or_typedef_name
824 scanner->private = FALSE;
825 $$ = gi_source_struct_new (NULL);
829 scanner->private = FALSE;
830 $$ = gi_source_union_new (NULL);
834 struct_declaration_list
836 | struct_declaration_list struct_declaration
838 $$ = g_list_concat ($1, $2);
843 : specifier_qualifier_list struct_declarator_list ';'
847 for (l = $2; l != NULL; l = l->next)
849 GISourceSymbol *sym = l->data;
850 if ($1->storage_class_specifier & STORAGE_CLASS_TYPEDEF)
851 sym->type = CSYMBOL_TYPE_TYPEDEF;
853 sym->type = CSYMBOL_TYPE_MEMBER;
854 gi_source_symbol_merge_type (sym, gi_source_type_copy ($1));
855 sym->private = scanner->private;
856 $$ = g_list_append ($$, sym);
862 specifier_qualifier_list
863 : type_specifier specifier_qualifier_list
869 | type_qualifier specifier_qualifier_list
872 $$->type_qualifier |= $1;
876 $$ = gi_source_type_new (CTYPE_INVALID);
877 $$->type_qualifier |= $1;
881 struct_declarator_list
884 $$ = g_list_append (NULL, $1);
886 | struct_declarator_list ',' struct_declarator
888 $$ = g_list_append ($1, $3);
893 : /* empty, support for anonymous structs and unions */
895 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
898 | ':' constant_expression
900 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
902 | declarator ':' constant_expression
905 if ($3->const_int_set) {
906 $$->const_int_set = TRUE;
907 $$->const_int = $3->const_int;
913 : ENUM identifier_or_typedef_name '{' enumerator_list '}'
915 scanner->private = FALSE;
916 $$ = gi_source_enum_new ($2);
918 $$->is_bitfield = is_bitfield;
919 last_enum_value = -1;
921 | ENUM '{' enumerator_list '}'
923 scanner->private = FALSE;
924 $$ = gi_source_enum_new (NULL);
926 $$->is_bitfield = is_bitfield;
927 last_enum_value = -1;
929 | ENUM identifier_or_typedef_name '{' enumerator_list ',' '}'
931 scanner->private = FALSE;
932 $$ = gi_source_enum_new ($2);
934 $$->is_bitfield = is_bitfield;
935 last_enum_value = -1;
937 | ENUM '{' enumerator_list ',' '}'
939 scanner->private = FALSE;
940 $$ = gi_source_enum_new (NULL);
942 $$->is_bitfield = is_bitfield;
943 last_enum_value = -1;
945 | ENUM identifier_or_typedef_name
947 scanner->private = FALSE;
948 $$ = gi_source_enum_new ($2);
955 /* reset flag before the first enum value */
960 $2->private = scanner->private;
961 $$ = g_list_append (NULL, $2);
963 | enumerator_list ',' enumerator
965 $3->private = scanner->private;
966 $$ = g_list_append ($1, $3);
973 $$ = gi_source_symbol_new (CSYMBOL_TYPE_OBJECT, lineno);
975 $$->const_int_set = TRUE;
976 $$->const_int = ++last_enum_value;
977 g_hash_table_insert (const_table, g_strdup ($$->ident), gi_source_symbol_ref ($$));
979 | identifier '=' constant_expression
981 $$ = gi_source_symbol_new (CSYMBOL_TYPE_OBJECT, lineno);
983 $$->const_int_set = TRUE;
984 $$->const_int = $3->const_int;
985 last_enum_value = $$->const_int;
986 g_hash_table_insert (const_table, g_strdup ($$->ident), gi_source_symbol_ref ($$));
993 $$ = TYPE_QUALIFIER_CONST;
997 $$ = TYPE_QUALIFIER_RESTRICT;
1001 $$ = TYPE_QUALIFIER_EXTENSION;
1005 $$ = TYPE_QUALIFIER_VOLATILE;
1012 $$ = FUNCTION_INLINE;
1017 : pointer direct_declarator
1020 gi_source_symbol_merge_type ($$, $1);
1028 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
1031 | '(' declarator ')'
1035 | direct_declarator '[' assignment_expression ']'
1038 gi_source_symbol_merge_type ($$, gi_source_array_new ($3));
1040 | direct_declarator '[' ']'
1043 gi_source_symbol_merge_type ($$, gi_source_array_new (NULL));
1045 | direct_declarator '(' parameter_list ')'
1047 GISourceType *func = gi_source_function_new ();
1048 // ignore (void) parameter list
1049 if ($3 != NULL && ($3->next != NULL || ((GISourceSymbol *) $3->data)->base_type->type != CTYPE_VOID)) {
1050 func->child_list = $3;
1053 gi_source_symbol_merge_type ($$, func);
1055 | direct_declarator '(' identifier_list ')'
1057 GISourceType *func = gi_source_function_new ();
1058 func->child_list = $3;
1060 gi_source_symbol_merge_type ($$, func);
1062 | direct_declarator '(' ')'
1064 GISourceType *func = gi_source_function_new ();
1066 gi_source_symbol_merge_type ($$, func);
1071 : '*' type_qualifier_list
1073 $$ = gi_source_pointer_new (NULL);
1074 $$->type_qualifier = $2;
1078 $$ = gi_source_pointer_new (NULL);
1080 | '*' type_qualifier_list pointer
1082 $$ = gi_source_pointer_new ($3);
1083 $$->type_qualifier = $2;
1087 $$ = gi_source_pointer_new ($2);
1093 | type_qualifier_list type_qualifier
1100 : parameter_declaration
1102 $$ = g_list_append (NULL, $1);
1104 | parameter_list ',' parameter_declaration
1106 $$ = g_list_append ($1, $3);
1110 parameter_declaration
1111 : declaration_specifiers declarator
1114 gi_source_symbol_merge_type ($$, $1);
1116 | declaration_specifiers abstract_declarator
1119 gi_source_symbol_merge_type ($$, $1);
1121 | declaration_specifiers
1123 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
1128 $$ = gi_source_symbol_new (CSYMBOL_TYPE_ELLIPSIS, lineno);
1135 GISourceSymbol *sym = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
1137 $$ = g_list_append (NULL, sym);
1139 | identifier_list ',' identifier
1141 GISourceSymbol *sym = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
1143 $$ = g_list_append ($1, sym);
1148 : specifier_qualifier_list
1149 | specifier_qualifier_list abstract_declarator
1155 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
1156 gi_source_symbol_merge_type ($$, $1);
1158 | direct_abstract_declarator
1159 | pointer direct_abstract_declarator
1162 gi_source_symbol_merge_type ($$, $1);
1166 direct_abstract_declarator
1167 : '(' abstract_declarator ')'
1173 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
1174 gi_source_symbol_merge_type ($$, gi_source_array_new (NULL));
1176 | '[' assignment_expression ']'
1178 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
1179 gi_source_symbol_merge_type ($$, gi_source_array_new ($2));
1181 | direct_abstract_declarator '[' ']'
1184 gi_source_symbol_merge_type ($$, gi_source_array_new (NULL));
1186 | direct_abstract_declarator '[' assignment_expression ']'
1189 gi_source_symbol_merge_type ($$, gi_source_array_new ($3));
1193 GISourceType *func = gi_source_function_new ();
1194 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
1195 gi_source_symbol_merge_type ($$, func);
1197 | '(' parameter_list ')'
1199 GISourceType *func = gi_source_function_new ();
1200 // ignore (void) parameter list
1201 if ($2 != NULL && ($2->next != NULL || ((GISourceSymbol *) $2->data)->base_type->type != CTYPE_VOID)) {
1202 func->child_list = $2;
1204 $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
1205 gi_source_symbol_merge_type ($$, func);
1207 | direct_abstract_declarator '(' ')'
1209 GISourceType *func = gi_source_function_new ();
1211 gi_source_symbol_merge_type ($$, func);
1213 | direct_abstract_declarator '(' parameter_list ')'
1215 GISourceType *func = gi_source_function_new ();
1216 // ignore (void) parameter list
1217 if ($3 != NULL && ($3->next != NULL || ((GISourceSymbol *) $3->data)->base_type->type != CTYPE_VOID)) {
1218 func->child_list = $3;
1221 gi_source_symbol_merge_type ($$, func);
1228 $$ = g_strdup (yytext);
1233 : assignment_expression
1234 | '{' initializer_list '}'
1235 | '{' initializer_list ',' '}'
1240 | initializer_list ',' initializer
1243 /* A.2.3 Statements. */
1247 | compound_statement
1248 | expression_statement
1249 | selection_statement
1250 | iteration_statement
1255 : identifier_or_typedef_name ':' statement
1256 | CASE constant_expression ':' statement
1257 | DEFAULT ':' statement
1262 | '{' block_item_list '}'
1267 | block_item_list block_item
1275 expression_statement
1281 : IF '(' expression ')' statement
1282 | IF '(' expression ')' statement ELSE statement
1283 | SWITCH '(' expression ')' statement
1287 : WHILE '(' expression ')' statement
1288 | DO statement WHILE '(' expression ')' ';'
1289 | FOR '(' ';' ';' ')' statement
1290 | FOR '(' expression ';' ';' ')' statement
1291 | FOR '(' ';' expression ';' ')' statement
1292 | FOR '(' expression ';' expression ';' ')' statement
1293 | FOR '(' ';' ';' expression ')' statement
1294 | FOR '(' expression ';' ';' expression ')' statement
1295 | FOR '(' ';' expression ';' expression ')' statement
1296 | FOR '(' expression ';' expression ';' expression ')' statement
1300 : GOTO identifier_or_typedef_name ';'
1304 | RETURN expression ';'
1307 /* A.2.4 External definitions. */
1310 : external_declaration
1311 | translation_unit external_declaration
1314 external_declaration
1315 : function_definition
1321 : declaration_specifiers declarator declaration_list compound_statement
1322 | declaration_specifiers declarator compound_statement
1327 | declaration_list declaration
1335 $$ = g_strdup (yytext + strlen ("#define "));
1342 $$ = g_strdup (yytext + strlen ("#define "));
1346 function_macro_define
1347 : function_macro '(' identifier_list ')'
1351 : object_macro constant_expression
1353 if ($2->const_int_set || $2->const_double_set || $2->const_string != NULL) {
1355 gi_source_scanner_add_symbol (scanner, $2);
1356 gi_source_symbol_unref ($2);
1362 : function_macro_define
1363 | object_macro_define
1369 yyerror (GISourceScanner *scanner, const char *s)
1371 /* ignore errors while doing a macro scan as not all object macros
1372 * have valid expressions */
1373 if (!scanner->macro_scan)
1375 fprintf(stderr, "%s:%d: %s in '%s' at '%s'\n",
1376 scanner->current_filename, lineno, s, linebuf, yytext);
1381 eat_hspace (FILE * f)
1388 while (c == ' ' || c == '\t');
1393 eat_line (FILE * f, int c)
1395 while (c != EOF && c != '\n')
1402 if (c == ' ' || c == '\t')
1411 read_identifier (FILE * f, int c, char **identifier)
1413 GString *id = g_string_new ("");
1414 while (g_ascii_isalnum (c) || c == '_')
1416 g_string_append_c (id, c);
1419 *identifier = g_string_free (id, FALSE);
1424 gi_source_scanner_parse_macros (GISourceScanner *scanner, GList *filenames)
1426 GError *error = NULL;
1427 char *tmp_name = NULL;
1429 fdopen (g_file_open_tmp ("gen-introspect-XXXXXX.h", &tmp_name, &error),
1431 g_unlink (tmp_name);
1434 for (l = filenames; l != NULL; l = l->next)
1436 FILE *f = fopen (l->data, "r");
1439 GString *define_line;
1441 gboolean error_line = FALSE;
1442 int c = eat_hspace (f);
1448 c = eat_line (f, c);
1453 /* print current location */
1454 str = g_strescape (l->data, "");
1455 fprintf (fmacros, "# %d \"%s\"\n", line, str);
1459 c = read_identifier (f, c, &str);
1460 if (strcmp (str, "define") != 0 || (c != ' ' && c != '\t'))
1464 c = eat_line (f, c);
1470 c = read_identifier (f, c, &str);
1471 if (strlen (str) == 0 || (c != ' ' && c != '\t' && c != '('))
1475 c = eat_line (f, c);
1479 define_line = g_string_new ("#define ");
1480 g_string_append (define_line, str);
1486 g_string_append_c (define_line, c);
1488 if (c == EOF || c == '\n')
1496 g_string_free (define_line, TRUE);
1498 c = eat_line (f, c);
1503 g_assert (c == ')');
1504 g_string_append_c (define_line, c);
1507 /* found function-like macro */
1508 fprintf (fmacros, "%s\n", define_line->str);
1510 g_string_free (define_line, TRUE);
1511 /* ignore rest of line */
1512 c = eat_line (f, c);
1516 if (c != ' ' && c != '\t')
1518 g_string_free (define_line, TRUE);
1520 c = eat_line (f, c);
1524 while (c != EOF && c != '\n')
1526 g_string_append_c (define_line, c);
1533 /* fold lines when seeing backslash new-line sequence */
1538 g_string_append_c (define_line, '\\');
1543 /* found object-like macro */
1544 fprintf (fmacros, "%s\n", define_line->str);
1546 c = eat_line (f, c);
1554 gi_source_scanner_parse_file (scanner, fmacros);
1558 gi_source_scanner_parse_file (GISourceScanner *scanner, FILE *file)
1560 g_return_val_if_fail (file != NULL, FALSE);
1562 const_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1563 g_free, (GDestroyNotify)gi_source_symbol_unref);
1569 g_hash_table_destroy (const_table);
1578 gi_source_scanner_lex_filename (GISourceScanner *scanner, const gchar *filename)
1581 yyin = fopen (filename, "r");
1583 while (yylex (scanner) != YYEOF)