3 /* enable chatty error messages */
4 #define YYERROR_VERBOSE 1
6 /* figure out the location of a non-terminal. We discard extent information
7 * but this isn't necessarily disastrous for our purpose. */
8 #define YYLLOC_DEFAULT(Current, Rhs, N) do { \
16 /* various bits of parsing infrastructure. increasingly i'm thinking some of
17 * this belongs in its own file. still... */
19 /* hang on to both ends of a declarator while it is being parsed. This idiom
20 * is used a lot with anonymous structures below, but we need an actual name
21 * for the type for this particular case. */
22 struct parsing_declarator {
23 struct declarator_type *first, **end;
24 char *name; /* pick up name */
27 /* we need to know what declaration specifiers apply to the current
28 * declarator. This means we need a stack of them to cope with nested
30 struct declaration_specifiers_stack {
31 struct declaration_specifiers_stack *next;
32 struct declaration_specifiers *ds;
35 static struct declaration_specifiers_stack *declaration_specifiers_stack;
37 static void push_declaration_specifiers(struct declaration_specifiers *ds) {
38 struct declaration_specifiers_stack *n;
41 n->next = declaration_specifiers_stack;
43 declaration_specifiers_stack = n;
46 static void pop_declaration_specifiers(void) {
47 /* can we have ->= and .= operators? */
48 declaration_specifiers_stack = declaration_specifiers_stack->next;
51 static struct declaration_specifiers *top_declaration_specifiers(void) {
52 return declaration_specifiers_stack->ds;
55 static void check_top_declaration_specifier(struct declaration_specifiers *ds) {
56 assert(declaration_specifiers_stack->ds == ds);
59 /* make up an actual declarator from a completed parse */
60 static struct declarator *get_declarator(struct parsing_declarator *pd,
61 const struct location *where) {
65 d->declarator_type = pd->first;
66 d->declaration_specifiers = top_declaration_specifiers();
72 static struct expression expr_star; /* kludge */
74 static void warn_old_style_function(const struct location *where) {
75 inputwarning(where, warn_obsolete,
76 "old-style function declarator");
79 void parser_init(FILE *fp) {
99 struct declarator *declarator;
101 struct declaration_specifiers *declaration_specifiers;
102 struct declarator *declarator;
103 struct parsing_declarator *declarator_parse;
104 struct declaration *declaration;
105 struct identifier_list *identifier_list;
106 struct expression *expression;
107 struct expression_list *expression_list;
108 struct initializer *initializer;
109 struct designator *designator;
110 struct external_declaration *external_declaration;
111 struct function_definition *function_definition;
112 struct enumerator *enumerator;
113 struct statement *statement;
114 /* transients for parsing lists */
115 struct { struct declarator *first, **end; } *declarator_list;
116 struct { struct declaration *first, **end; } *declaration_list;
117 struct { struct identifier_list *first, **end; } *identifier_list_parse;
118 struct { struct expression_list *first, **end; } *parsing_expression_list;
119 struct { struct initializer *first, **end; } *initializer_list;
120 struct { struct designator *first, **end; } *designator_list;
121 struct { struct external_declaration *first, **end; } *external_declaration_list;
122 struct { struct enumerator *first, **end; } *enumerator_list;
123 struct { struct statement *first, **end; } *statement_list;
126 %token <i> MEMBER "->"
137 %token <i> MULEQ "*="
138 %token <i> DIVEQ "/="
139 %token <i> MODEQ "%="
140 %token <i> ADDEQ "+="
141 %token <i> SUBEQ "-="
142 %token <i> SLEQ "<<="
143 %token <i> SREQ ">>="
144 %token <i> ANDEQ "&="
145 %token <i> XOREQ "^="
148 %token <i> VARARG "..."
166 %token <u> AUTO "auto"
169 %token <u> CHAR "char"
170 %token <u> CONST "const"
171 %token CONTINUE "continue"
172 %token DEFAULT "default"
174 %token <u> DOUBLE "double"
177 %token <u> EXTERN "extern"
178 %token <u> FLOAT "float"
182 %token <u> INLINE "inline"
184 %token <u> LONG "long"
185 %token <u> REGISTER "register"
186 %token <u> RESTRICT "restrict"
187 %token RETURN "return"
188 %token <u> SHORT "short"
189 %token <u> SIGNED "signed"
190 %token SIZEOF "sizeof"
191 %token <u> STATIC "static"
192 %token <u> STRUCT "struct"
193 %token SWITCH "switch"
194 %token <u> TYPEDEF "typedef"
195 %token <u> UNION "union"
196 %token <u> UNSIGNED "unsigned"
197 %token <u> VOID "void"
198 %token <u> VOLATILE "volatile"
200 %token <u> BOOL "_Bool"
201 %token <u> COMPLEX "_Complex"
202 %token <u> IMAGINARY "_Imaginary"
203 %token ATTRIBUTE "__attribute__"
204 %token <u> GCC_VA_LIST "__builtin_va_list"
205 %token GCC_VA_ARG "__builtin_va_arg"
206 %token GCC_EXPECT "__builtin_expect"
208 %token <name> TYPEDEF_NAME
213 %token <s> WSTRINGLIT
216 %type <u> storage_class_specifier
217 %type <u> type_qualifier
218 %type <u> struct_or_union
219 %type <u> type_qualifier_list
220 %type <u> type_qualifier_list_opt
221 %type <u> function_specifier
222 %type <u> basic_type_specifier
224 %type <name> identifier
225 %type <name> identifier_opt
227 %type <declaration_specifiers> type_specifier
228 %type <declaration_specifiers> struct_or_union_specifier
229 %type <declaration_specifiers> enum_specifier
231 %type <declaration_specifiers> declaration_specifiers
232 %type <declaration_specifiers> declaration_specifiers_definition
233 %type <declaration_specifiers> declaration_specifiers_opt
234 %type <declaration_specifiers> parameter_declaration_specifiers
235 %type <declaration_specifiers> specifier_qualifier_list
236 %type <declaration_specifiers> specifier_qualifier_list_definition
237 %type <declaration_specifiers> specifier_qualifier_list_opt
238 %type <declaration_specifiers> array_specifiers
239 %type <declarator> declarator_opt
240 %type <declarator> init_declarator_list_opt
241 %type <declarator> attributed_declarator
242 %type <declarator> attributed_init_declarator
243 %type <declarator> struct_declarator
244 %type <declarator_list> init_declarator_list
245 %type <declarator_list> struct_declarator_list
247 %type <declarator_parse> declarator pointer
248 %type <declarator_parse> direct_declarator
249 %type <declarator_parse> abstract_declarator
250 %type <declarator_parse> pointer_opt
251 %type <declarator_parse> direct_abstract_declarator
252 %type <declarator_parse> direct_abstract_declarator_opt
253 %type <declarator_parse> abstract_declarator_opt
255 %type <declaration> declaration
256 %type <declaration> struct_declaration
257 %type <declaration> parameter_declaration
258 %type <declaration> type_name
259 %type <declaration> main
260 %type <external_declaration_list> translation_unit
261 %type <external_declaration> external_declaration
262 %type <declaration_list> struct_declaration_list
263 %type <declaration_list> parameter_list
264 %type <declaration_list> declaration_list_opt
266 %type <identifier_list_parse> ID_list
267 %type <identifier_list> ID_list_opt
269 %type <expression> expression
270 %type <expression> expression_opt
271 %type <expression> primary_expression
272 %type <expression> postfix_expression
273 %type <expression> unary_expression
274 %type <expression> cast_expression
275 %type <expression> multiplicative_expression
276 %type <expression> additive_expression
277 %type <expression> shift_expression
278 %type <expression> relational_expression
279 %type <expression> equality_expression
280 %type <expression> AND_expression
281 %type <expression> exclusive_OR_expression
282 %type <expression> inclusive_OR_expression
283 %type <expression> logical_AND_expression
284 %type <expression> logical_OR_expression
285 %type <expression> conditional_expression
286 %type <expression> assignment_expression
287 %type <expression> constant_expression
289 %type <expression> array_size
291 %type <expression_list> argument_expression_list_opt
292 %type <parsing_expression_list> argument_expression_list
294 %type <initializer> initializer
295 %type <initializer> designated_initializer
296 %type <initializer_list> initializer_list
297 %type <designator_list> designator_list
298 %type <designator> designator
302 %type <i> member_operator
303 %type <i> unary_operator
304 %type <i> multiplicative_operator
305 %type <i> additive_operator
306 %type <i> shift_operator
307 %type <i> relational_operator
308 %type <i> equality_operator
309 %type <i> assignment_operator
313 %type <function_definition> function_definition
314 %type <enumerator> enumerator
315 %type <enumerator> registered_enumerator
316 %type <enumerator_list> enumerator_list
318 %type <statement> statement
319 %type <statement> function_body
320 %type <statement> block_item
321 %type <statement> else_part_opt
322 %type <statement_list> block_item_list_opt
328 translation_unit = $1->first;
346 /* C99 6.5 expressions */
350 struct declarator *d;
351 $$ = expr(ex_id, &@1);
352 $$->u.name = $1.name;
355 inputerror(&@1, "undeclared identifier '%s'", $1.name);
356 } else if(d->declaration_specifiers
357 && (d->declaration_specifiers->storage_class_specifiers
359 inputerror(&@1, "typedef-name '%s' used in expression", $1);
361 $$->valuetype = resolve_typedefs(d);
364 $$ = expr(ex_number, &@1);
366 $$->valuetype = numbertype($$);
369 $$ = stringexpr(ex_char, $1, &@1);
372 $$ = stringexpr(ex_string, $1, &@1);
375 $$ = stringexpr(ex_wchar, $1, &@1);
378 $$ = stringexpr(ex_wstring, $1, &@1);
380 | '(' expression ')' {
387 | postfix_expression '[' expression ']' {
388 $$ = binary('[', $1, $3, &@2);
390 | postfix_expression '(' argument_expression_list_opt ')' {
391 $$ = fncallexpr($1, $3, &@2);
393 | GCC_VA_ARG '(' assignment_expression ',' type_name ')' {
394 $$ = expr(ex_gcc_va_arg, &@2);
395 $$->u.gcc_va_arg.arg = $3;
396 $$->u.gcc_va_arg.type = $5;
397 $$->valuetype = resolve_typedefs($5->declarator_list);
399 | GCC_EXPECT '(' assignment_expression ',' assignment_expression ')' {
400 /* we can't hack this in as a function as the return type is that of
402 $$ = expr(ex_gcc_expect, &@1);
403 /* XXX does the LH arg undergo any implicit conversions? */
406 $$->valuetype = $3->valuetype;
408 | postfix_expression member_operator identifier {
409 struct expression *e = expr(ex_id, &@2);
411 $$ = binary($2, $1, e, &@2);
413 | postfix_expression incrdecr {
414 $$ = postfix($2, $1, &@2);
416 | '(' type_name ')' '{' initializer_list comma_opt '}' {
417 $$ = expr(ex_compound_literal, &@1);
418 $$->u.compound_literal.type = $2;
419 $$->u.compound_literal.value = $5->first;
420 declaration_constraints($2, dc_compound_literal);
421 $$->valuetype = resolve_typedefs($2->declarator_list);
425 member_operator: '.' | "->" ;
427 incrdecr: "++" | "--" ;
429 argument_expression_list:
430 assignment_expression {
434 $$->end = &$$->first->next;
436 | argument_expression_list ',' assignment_expression {
437 struct expression_list *e;
445 argument_expression_list_opt:
446 argument_expression_list { $$ = $1->first; }
452 | unary_operator cast_expression {
453 $$ = prefix($1, $2, &@1);
455 | "sizeof" unary_expression {
456 $$ = prefix(SIZEOF, $2, &@1);
458 | "sizeof" '(' type_name ')' {
459 $$ = expr(ex_sizeof_type, &@1);
461 declaration_constraints($3, dc_sizeof);
465 /* putting sizeof in here would conflict with sizeof(type) */
478 | '(' type_name ')' cast_expression {
479 $$ = expr(ex_cast, &@1);
480 $$->valuetype = $2->declarator_list;
482 declaration_constraints($2, dc_cast);
486 multiplicative_expression:
488 | multiplicative_expression multiplicative_operator cast_expression {
489 $$ = binary($2, $1, $3, &@2);
493 multiplicative_operator:
500 multiplicative_expression
501 | additive_expression additive_operator multiplicative_expression {
502 $$ = binary($2, $1, $3, &@2);
513 | shift_expression shift_operator additive_expression {
514 $$ = binary($2, $1, $3, &@2);
523 relational_expression:
525 | relational_expression relational_operator shift_expression {
526 $$ = binary($2, $1, $3, &@2);
538 relational_expression
539 | equality_expression equality_operator relational_expression {
540 $$ = binary($2, $1, $3, &@2);
551 | AND_expression '&' equality_expression {
552 $$ = binary($2, $1, $3, &@2);
556 exclusive_OR_expression:
558 | exclusive_OR_expression '^' AND_expression {
559 $$ = binary($2, $1, $3, &@2);
563 inclusive_OR_expression:
564 exclusive_OR_expression
565 | inclusive_OR_expression '|' exclusive_OR_expression {
566 $$ = binary($2, $1, $3, &@2);
570 logical_AND_expression:
571 inclusive_OR_expression
572 | logical_AND_expression "&&" inclusive_OR_expression {
573 $$ = binary($2, $1, $3, &@2);
577 logical_OR_expression:
578 logical_AND_expression
579 | logical_OR_expression "||" logical_AND_expression {
580 $$ = binary($2, $1, $3, &@2);
584 conditional_expression:
585 logical_OR_expression
586 | logical_OR_expression '?' expression ':' conditional_expression {
587 $$ = binary('?', $1, binary(':', $3, $5, &@4), &@2);
591 assignment_expression:
592 conditional_expression
593 | unary_expression assignment_operator assignment_expression {
594 $$ = binary($2, $1, $3, &@2);
613 assignment_expression
614 | expression ',' assignment_expression {
615 $$ = binary($2, $1, $3, &@2);
625 conditional_expression
628 /* C99 6.7 declarations */
631 declaration_specifiers init_declarator_list_opt ';' {
632 struct declaration_specifiers *ds = $1;
633 struct declarator *decl = $2;
635 check_top_declaration_specifier(ds);
636 pop_declaration_specifiers();
637 /* will have already done add_declarator() */
639 $$->declaration_specifiers = ds;
640 $$->declarator_list = decl;
642 /* constraints checked by calling production */
646 declaration_specifiers:
647 declaration_specifiers_definition {
648 push_declaration_specifiers($1);
653 declaration_specifiers_definition:
654 storage_class_specifier declaration_specifiers_opt {
655 struct declaration_specifiers ds = {
656 .type_specifiers = 0,
657 .storage_class_specifiers = $1,
658 .type_qualifiers = 0,
659 .function_specifiers = 0,
664 .enum_compat_type = 0
666 $$ = merge_declaration_specifiers($2, &ds);
668 | type_specifier declaration_specifiers_opt {
669 $$ = merge_declaration_specifiers($2, $1);
671 | type_qualifier declaration_specifiers_opt {
672 struct declaration_specifiers ds = {
673 .type_specifiers = 0,
674 .storage_class_specifiers = 0,
675 .type_qualifiers = $1,
676 .function_specifiers = 0,
681 .enum_compat_type = 0
683 $$ = merge_declaration_specifiers($2, &ds);
685 | function_specifier declaration_specifiers_opt {
686 struct declaration_specifiers ds = {
687 .type_specifiers = 0,
688 .storage_class_specifiers = 0,
689 .type_qualifiers = 0,
690 .function_specifiers = $1,
695 .enum_compat_type = 0
697 $$ = merge_declaration_specifiers($2, &ds);
699 | attribute_specifier declaration_specifiers_opt {
704 declaration_specifiers_opt:
705 declaration_specifiers_definition
711 init_declarator_list_opt:
712 init_declarator_list { $$ = $1->first; }
716 init_declarator_list:
717 attributed_init_declarator {
722 | init_declarator_list ',' attribute_specifier_list attributed_init_declarator {
729 attributed_declarator:
730 declarator attribute_specifier_list { $$ = get_declarator($1, &@1); }
733 attributed_init_declarator:
734 attributed_declarator {
735 add_declaration($$ = $1);
737 | attributed_declarator '=' {
738 /* need to do this before we parse the initializer */
742 $$->initializer = $4;
746 /* C99 6.7.1 storage-class specifiers */
748 storage_class_specifier:
756 /* C99 6.7.2 type specifiers */
759 basic_type_specifier {
761 $$->type_specifiers = $1;
763 | struct_or_union_specifier { $$ = $1; }
767 $$->type_specifiers = TS_TYPEDEF;
769 $$->type = $1.declarator;
773 basic_type_specifier:
789 /* C99 6.2.7.1 structure and union specifiers */
791 /* shift/reduce conflict between
795 resolved by shifting (which is right)
797 struct_or_union_specifier:
798 struct_or_union identifier_opt '{' struct_declaration_list '}' attribute_specifier_list {
800 $$->type_specifiers = $1 | TS_DEFINITION;
802 $$->structure = $4->first;
804 | struct_or_union identifier {
806 $$->type_specifiers = $1;
809 | struct_or_union { /* shift/reduce conflict */
810 inputerror(&@1, "structure must have a tag or a definition");
812 $$->type_specifiers = $1;
817 "struct" attribute_specifier_list { $$ = $1; }
818 | "union" attribute_specifier_list { $$ = $1; }
821 struct_declaration_list:
827 | struct_declaration_list struct_declaration {
835 specifier_qualifier_list struct_declarator_list ';' {
836 check_top_declaration_specifier($1);
837 pop_declaration_specifiers();
839 $$->declaration_specifiers = $1;
840 $$->declarator_list = $2->first;
841 declaration_constraints($$, dc_struct_member);
842 /* XXX perhaps add to a dictionary of struct/union declarations? */
847 specifier_qualifier_list:
848 specifier_qualifier_list_definition {
849 push_declaration_specifiers($1);
854 specifier_qualifier_list_definition:
855 type_specifier specifier_qualifier_list_opt {
856 $$ = merge_declaration_specifiers($2, $1);
858 | type_qualifier specifier_qualifier_list_opt {
859 struct declaration_specifiers ds = {
860 .type_specifiers = 0,
861 .storage_class_specifiers = 0,
862 .type_qualifiers = $1,
863 .function_specifiers = 0,
868 .enum_compat_type = 0
870 $$ = merge_declaration_specifiers($2, &ds);
872 | storage_class_specifier specifier_qualifier_list_opt {
873 inputerror(&@1, "storage class specifiers not allowed here");
876 | function_specifier specifier_qualifier_list_opt {
877 inputerror(&@1, "function specifiers not allowed here");
880 | attribute_specifier specifier_qualifier_list_opt {
885 specifier_qualifier_list_opt:
886 specifier_qualifier_list_definition
892 struct_declarator_list:
893 struct_declarator attribute_specifier_list {
898 | struct_declarator_list ',' attribute_specifier_list struct_declarator attribute_specifier_list {
906 attributed_declarator
907 | declarator_opt ':' constant_expression {
910 $$->declaration_specifiers = top_declaration_specifiers();
917 /* C99 6.7.2.2 enumeration specifiers */
920 enum identifier_opt '{' enumerator_list comma_opt '}' attribute_specifier_list {
922 $$->type_specifiers = TS_ENUM | TS_DEFINITION;
924 $$->enumerators = $4->first;
925 $$->enum_compat_type = TS_INT; /* XXX */
927 inputwarning(&@5, warn_compat,
928 "enumerator-list trailing comma not supported in C89 or C++");
929 /* XXX pick type and fix up the declarators of all the enumerators */
933 $$->type_specifiers = TS_ENUM;
939 "enum" attribute_specifier_list
943 registered_enumerator {
948 | enumerator_list ',' registered_enumerator {
949 *($$ = $1)->end = $3;
954 registered_enumerator:
956 struct declarator *d;
959 d->where = $$->where;
960 NEW(d->declaration_specifiers);
961 d->declaration_specifiers->type_specifiers = TS_INT;
974 | identifier '=' constant_expression {
982 /* C99 6.7.3 type qualifiers */
990 /* C99 6.7.4 function specifiers */
996 /* C99 6.7.5 declarators */
999 declarator { $$ = get_declarator($1, &@1); }
1004 pointer direct_declarator {
1006 *$$->end = $1->first;
1015 $$->end = &$$->first;
1017 /* XXX warn about redeclaration */
1019 | '(' attribute_specifier_list declarator ')' {
1022 | direct_declarator '[' array_specifiers array_size ']' {
1023 struct declarator_type *t;
1028 t->type_qualifiers = $3->type_qualifiers;
1029 t->storage_class_specifiers = $3->storage_class_specifiers;
1030 if($4 == &expr_star) {
1031 t->u.array.size = 0;
1032 t->u.array.flags = AF_STAR;
1034 t->u.array.size = $4;
1039 | direct_declarator '(' ID_list_opt ')' {
1040 struct declarator_type *t;
1044 t->type = dt_old_function;
1045 t->u.old_function.args = $3;
1049 warn_old_style_function(&@2);
1051 | direct_declarator '(' parameter_list variadic ')' {
1052 struct declarator_type *t;
1056 t->type = dt_function;
1057 t->u.function.args = $3->first;
1058 t->u.function.variadic = $4;
1066 assignment_expression
1067 | '*' { $$ = &expr_star; }
1072 type_qualifier_list_opt {
1075 | "static" type_qualifier_list_opt {
1077 $$->type_qualifiers = $2;
1078 $$->storage_class_specifiers = SCS_STATIC;
1080 | type_qualifier_list "static" {
1082 $$->type_qualifiers = $1;
1083 $$->storage_class_specifiers = SCS_STATIC;
1088 ',' "..." { $$ = 1; }
1097 $$->end = &$$->first;
1102 '*' type_qualifier_list_opt {
1103 struct declarator_type *t;
1106 t->type = dt_pointer;
1107 t->type_qualifiers = $2;
1113 | '*' type_qualifier_list_opt pointer {
1114 struct declarator_type *t;
1117 t->type = dt_pointer;
1118 t->type_qualifiers = $2;
1126 type_qualifier_list:
1128 | type_qualifier_list type_qualifier { $$ = $1 | $2; }
1129 | type_qualifier_list attribute_specifier
1132 type_qualifier_list_opt:
1138 parameter_declaration {
1141 $$->end = &$1->next;
1143 | parameter_list ',' parameter_declaration {
1145 $$->end = &$3->next;
1149 parameter_declaration_specifiers:
1150 declaration_specifiers {
1155 /* declaration_constraints will be checked recursively once we know what
1156 * context we're really in (i.e. function-definition or not) */
1157 parameter_declaration:
1158 parameter_declaration_specifiers declarator {
1159 struct declarator *d = get_declarator($2, &@2);
1160 check_top_declaration_specifier($1);
1161 pop_declaration_specifiers();
1163 $$->declaration_specifiers = $1;
1164 $$->declarator_list = d;
1167 | parameter_declaration_specifiers abstract_declarator_opt {
1168 struct declarator *d = get_declarator($2, $2->first ? &@2 : &@1);
1169 check_top_declaration_specifier($1);
1170 pop_declaration_specifiers();
1172 $$->declaration_specifiers = $1;
1173 $$->declarator_list = d;
1178 /* an old-style function declaration where all the parameter names match
1179 * typedef names is ambiguous - it could be a parameter list where all the
1180 * declarators are abstract. We resolve the ambiguity by assumiong that it is
1181 * the latter, modern form. */
1183 ID_list { $$ = $1->first; }
1189 struct identifier_list *i;
1193 /* XXX warn about redeclaration */
1199 struct identifier_list *i;
1203 /* XXX warn about redeclaration */
1204 *($$ = $1)->end = i;
1209 /* C99 6.7.6 type names */
1212 specifier_qualifier_list abstract_declarator_opt {
1213 struct declarator *d = get_declarator($2, $2->first ? &@2 : &@1);
1214 check_top_declaration_specifier($1);
1215 pop_declaration_specifiers();
1217 $$->declaration_specifiers = $1;
1218 $$->declarator_list = d;
1220 /* declaration_constraints checked by calling production */
1224 direct_abstract_declarator:
1225 '(' attribute_specifier_list abstract_declarator ')' {
1228 | direct_abstract_declarator_opt '[' array_specifiers array_size ']' {
1229 struct declarator_type *t;
1234 t->type_qualifiers = $3->type_qualifiers;
1235 t->storage_class_specifiers = $3->storage_class_specifiers;
1236 if($4 == &expr_star) {
1237 t->u.array.size = 0;
1238 t->u.array.flags = AF_STAR;
1240 t->u.array.size = $4;
1245 | direct_abstract_declarator_opt '(' ')' {
1246 struct declarator_type *t;
1250 t->type = dt_old_function;
1251 t->u.old_function.args = 0;
1255 warn_old_style_function(&@2);
1257 | direct_abstract_declarator_opt '(' parameter_list variadic ')' {
1258 struct declarator_type *t;
1262 t->type = dt_function;
1263 t->u.function.args = $3->first;
1264 t->u.function.variadic = $4;
1271 direct_abstract_declarator_opt:
1272 direct_abstract_declarator
1275 $$->end = &$$->first;
1276 @$ = (struct location){path, line};
1280 abstract_declarator_opt:
1284 $$->end = &$$->first;
1285 @$ = (struct location){path, line};
1289 abstract_declarator:
1291 | pointer_opt direct_abstract_declarator {
1293 *$$->end = $1->first;
1298 /* C99 6.7.8 initializers */
1301 assignment_expression {
1306 | '{' initializer_list comma_opt '}' {
1309 $$->u.list = $2->first;
1319 designated_initializer {
1322 $$->end = &$1->next;
1324 | initializer_list ',' designated_initializer {
1325 *($$ = $1)->end = $3;
1326 $$->end = &$3->next;
1330 designated_initializer:
1331 designator_list '=' initializer {
1332 ($$ = $3)->designator = $1->first;
1333 $$->syntax = des_c99;
1335 | designator initializer {
1336 ($$ = $2)->designator = $1;
1337 $$->syntax = des_gcc_raw;
1338 inputwarning(&@1, warn_compat,
1339 "non-portable GNU C initializer designator");
1341 | ID ':' initializer {
1342 struct designator *d;
1345 d->type = des_field;
1346 d->u.name = $1.name;
1348 ($$ = $3)->designator = d;
1349 $$->syntax = des_gcc_colon;
1350 inputwarning(&@1, warn_compat,
1351 "non-portable GNU C initializer designator");
1360 $$->end = &$1->next;
1362 | designator_list designator {
1363 *($$ = $1)->end = $2;
1364 $$->end = &$2->next;
1369 '[' constant_expression ']' {
1371 $$->type = des_expr;
1377 $$->type = des_field;
1378 $$->u.name = $2.name;
1383 /* C99 6.8 statements and blocks */
1386 identifier ':' attribute_specifier_list statement {
1388 $$->type = st_label;
1389 $$->u.label.label = $1.name;
1390 /* XXX warn about redeclaration */
1391 $$->u.label.body = $4;
1394 | "case" constant_expression ':' statement {
1397 $$->u.case_.value = $2;
1398 $$->u.case_.body = $4;
1401 | "default" ':' statement {
1403 $$->type = st_default;
1404 $$->u.default_ = $3;
1407 | "if" '(' expression ')' statement else_part_opt {
1410 $$->u.if_.cond = $3;
1411 $$->u.if_.true = $5;
1412 $$->u.if_.false = $6;
1415 | "switch" '(' expression ')' statement {
1417 $$->type = st_switch;
1418 $$->u.switch_.cond = $3;
1419 $$->u.switch_.body = $5;
1422 | expression_opt ';' {
1424 $$->type = st_expression;
1425 $$->u.expression = $1;
1426 $$->where = $1 ? @1 : @2;
1428 | "while" '(' expression ')' statement {
1430 $$->type = st_while;
1431 $$->u.while_.cond = $3;
1432 $$->u.while_.body = $5;
1435 | "do" statement "while" '(' expression ')' ';' {
1438 $$->u.while_.cond = $5;
1439 $$->u.while_.body = $2;
1442 | "for" '(' expression_opt ';'
1444 expression_opt ')' statement {
1447 $$->u.for_.init = $3;
1448 $$->u.for_.cond = $5;
1449 $$->u.for_.iter = $7;
1450 $$->u.for_.body = $9;
1457 expression_opt ')' statement {
1459 $$->type = st_for_declaration;
1460 $$->u.for_declaration.init = $4;
1461 $$->u.for_declaration.cond = $5;
1462 $$->u.for_declaration.iter = $7;
1463 $$->u.for_declaration.body = $9;
1467 | "goto" identifier ';' {
1470 $$->u.goto_ = $2.name;
1475 $$->type = st_continue;
1480 $$->type = st_break;
1483 | "return" expression_opt ';' {
1485 $$->type = st_return;
1486 $$->u.expression = $2;
1489 | '{' { enter_scope(); } block_item_list_opt '}' {
1491 $$->type = st_compound;
1492 $$->u.compound.body = $3->first;
1494 $$->u.compound.endwhere = @4;
1495 /*$$->u.compound.scope = scope;*/
1500 /* scope handling for function bodies is done by function_definition */
1502 '{' block_item_list_opt '}' {
1504 $$->type = st_compound;
1505 $$->u.compound.body = $2->first;
1507 $$->u.compound.endwhere = @3;
1508 /*$$->u.compound.scope = scope;*/
1512 block_item_list_opt:
1513 block_item_list_opt block_item {
1514 *($$ = $1)->end = $2;
1515 $$->end = &$2->next;
1519 $$->end = &$$->first;
1526 $$->type = st_declaration;
1527 $$->u.declaration = $1;
1529 declaration_constraints($1, dc_block_scope);
1534 /* shift/reduce conflict here */
1536 "else" statement { $$ = $2; }
1540 /* C99 6.9 external definitions */
1543 external_declaration {
1545 translation_unit = $$->first = $1;
1546 $$->end = &$1->next;
1548 | translation_unit external_declaration {
1551 $$->end = &$2->next;
1553 | translation_unit ';' {
1554 inputerror(&@2, "redundant semicolon at top level");
1558 external_declaration:
1561 $$->type = ed_declaration;
1562 $$->u.declaration = $1;
1563 declaration_constraints($1, dc_file_scope);
1565 | function_definition {
1567 $$->type = ed_function_definition;
1568 $$->u.function_definition = $1;
1569 /* constraints checked in function_definition (i.e. before errors
1570 * from the function body) */
1574 /* C99 6.9.1 function definitions */
1576 function_definition:
1577 declaration_specifiers attributed_declarator {
1578 check_top_declaration_specifier($1);
1579 pop_declaration_specifiers();
1580 declarator_constraints($1, $2, dc_function_definition, 0);
1581 NEW($<declaration>$);
1582 $<declaration>$->declaration_specifiers = $1;
1583 $<declaration>$->declarator_list = $2;
1584 $<declaration>$->where = @1;
1585 add_declaration($2);
1588 struct declarator *d;
1590 NEW(d->declaration_specifiers);
1591 NEW(d->declarator_type);
1592 d->name = xstrdup("__PRETTY_FUNCTION__");
1593 d->declaration_specifiers->type_qualifiers = TQ_CONST;
1594 d->declaration_specifiers->type_specifiers = TS_CHAR;
1595 d->declarator_type->type = dt_pointer;
1598 /* we had better be in the right scope before parsing the declaration
1599 * list for old style functions */
1600 } declaration_list_opt {
1601 struct declaration *decl;
1602 struct declarator *d = 0; /* quieten compiler */
1604 /* inject function args into scope */
1605 if($2->declarator_type) switch($2->declarator_type->type) {
1607 if(!is_void_args($2->declarator_type)) {
1608 for(decl = $2->declarator_type->u.function.args;
1610 decl = decl->next) {
1611 decl->declarator_list->flags |= DF_PARAM;
1612 add_declaration(decl->declarator_list);
1616 inputerror(&@4, "declaration-list not required for new-style function definitions");
1618 case dt_old_function:
1619 if($2->declarator_type->u.old_function.args) {
1620 struct identifier_list *i;
1622 for(i = $2->declarator_type->u.old_function.args;
1625 for(decl = $4->first;
1628 for(d = decl->declarator_list;
1631 if(!strcmp(d->name, i->id))
1634 d->flags |= DF_PARAM;
1637 /* default to int */
1640 NEW(d->declaration_specifiers);
1641 d->declaration_specifiers->type_specifiers = TS_INT;
1642 d->flags = DF_PARAM;
1647 /* check that all the identifiers in the list are actually
1649 for(decl = $4->first; decl; decl = decl->next)
1650 for(d = decl->declarator_list;
1653 struct identifier_list *i;
1655 for(i = $2->declarator_type->u.old_function.args; i; i = i->next)
1656 if(!strcmp(d->name, i->id))
1659 inputerror(&d->where, "'%s' is not a parameter", d->name);
1663 default: /* quieten compiler */
1668 $$->declaration = $<declaration>3;
1669 $$->args = $4->first;
1675 declaration_list_opt:
1676 declaration_list_opt declaration {
1677 *($$ = $1)->end = $2;
1678 $$->end = &$2->next;
1682 $$->end = &$$->first;
1686 /* gcc.info attribute syntax */
1688 attribute_specifier_list:
1689 attribute_specifier_list attribute_specifier
1693 attribute_specifier:
1694 ATTRIBUTE '(' '(' attribute_list ')' ')' {
1695 static int attr_warning;
1697 inputwarning(&@1, warn_compat, "GNU C attributes are ignored");
1702 attribute_list attribute
1710 { suppress_errors(); }
1711 argument_expression_list_opt
1712 { restore_errors(); }