cgram.y revision 1.487 1 %{
2 /* $NetBSD: cgram.y,v 1.487 2024/02/05 23:11:22 rillig Exp $ */
3
4 /*
5 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
6 * Copyright (c) 1994, 1995 Jochen Pohl
7 * All Rights Reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Jochen Pohl for
20 * The NetBSD Project.
21 * 4. The name of the author may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #if defined(__RCSID)
38 __RCSID("$NetBSD: cgram.y,v 1.487 2024/02/05 23:11:22 rillig Exp $");
39 #endif
40
41 #include <limits.h>
42 #include <stdlib.h>
43 #include <string.h>
44
45 #include "lint1.h"
46
47 extern char *yytext;
48
49 /*
50 * Contains the level of current declaration, used for symbol table entries.
51 * 0 is the top-level, > 0 is inside a function body.
52 */
53 int block_level;
54
55 /*
56 * level for memory allocation. Normally the same as block_level.
57 * An exception is the declaration of parameters in prototypes. Memory
58 * for these can't be freed after the declaration, but symbols must
59 * be removed from the symbol table after the declaration.
60 */
61 size_t mem_block_level;
62
63 /*
64 * Save the no-warns state and restore it to avoid the problem where
65 * if (expr) { stmt } / * NOLINT * / stmt;
66 */
67 #define LWARN_NOTHING_SAVED (-3)
68 static int saved_lwarn = LWARN_NOTHING_SAVED;
69
70 static void cgram_declare(sym_t *, bool, sbuf_t *);
71 static void read_until_rparen(void);
72 static sym_t *symbolrename(sym_t *, sbuf_t *);
73
74
75 /* ARGSUSED */
76 static void
77 clear_warning_flags_loc(const char *file, size_t line)
78 {
79 debug_step("%s:%zu: clearing flags", file, line);
80 clear_warn_flags();
81 saved_lwarn = LWARN_NOTHING_SAVED;
82 }
83
84 /* ARGSUSED */
85 static void
86 save_warning_flags_loc(const char *file, size_t line)
87 {
88 /*
89 * There used to be an assertion that saved_lwarn is
90 * LWARN_NOTHING_SAVED here, but that triggered for the following
91 * code:
92 *
93 * void function(int x) { if (x > 0) if (x > 1) return; }
94 *
95 * It didn't trigger if the inner 'if' was enclosed in braces though.
96 *
97 * TODO: If actually needed, add support for nested suppression of
98 * warnings.
99 */
100 debug_step("%s:%zu: saving flags %d", file, line, lwarn);
101 saved_lwarn = lwarn;
102 }
103
104 /* ARGSUSED */
105 static void
106 restore_warning_flags_loc(const char *file, size_t line)
107 {
108 if (saved_lwarn != LWARN_NOTHING_SAVED) {
109 lwarn = saved_lwarn;
110 debug_step("%s:%zu: restoring flags %d", file, line, lwarn);
111 /*
112 * Do not set 'saved_lwarn = LWARN_NOTHING_SAVED' here, to
113 * avoid triggering the assertion in save_warning_flags_loc.
114 */
115 } else
116 clear_warning_flags_loc(file, line);
117 }
118
119 #define clear_warning_flags() clear_warning_flags_loc(__FILE__, __LINE__)
120 #define save_warning_flags() save_warning_flags_loc(__FILE__, __LINE__)
121 #define restore_warning_flags() restore_warning_flags_loc(__FILE__, __LINE__)
122
123 static bool
124 is_either(const char *s, const char *a, const char *b)
125 {
126 return strcmp(s, a) == 0 || strcmp(s, b) == 0;
127 }
128
129 #if YYDEBUG && YYBYACC
130 #define YYSTYPE_TOSTRING cgram_to_string
131 #endif
132
133 %}
134
135 %expect 103
136
137 %union {
138 val_t *y_val;
139 sbuf_t *y_name;
140 sym_t *y_sym;
141 bool y_inc;
142 op_t y_op;
143 scl_t y_scl;
144 tspec_t y_tspec;
145 type_qualifiers y_type_qualifiers;
146 function_specifier y_function_specifier;
147 struct parameter_list y_parameter_list;
148 function_call *y_arguments;
149 type_t *y_type;
150 tnode_t *y_tnode;
151 range_t y_range;
152 buffer *y_string;
153 qual_ptr *y_qual_ptr;
154 bool y_seen_statement;
155 struct generic_association *y_generic;
156 struct array_size y_array_size;
157 bool y_in_system_header;
158 designation y_designation;
159 };
160
161 /* for Bison:
162 %printer {
163 if (is_integer($$->v_tspec))
164 fprintf(yyo, "%lld", (long long)$$->u.integer);
165 else
166 fprintf(yyo, "%Lg", $$->u.floating);
167 } <y_val>
168 %printer { fprintf(yyo, "'%s'", $$ != NULL ? $$->sb_name : "<null>"); } <y_name>
169 %printer {
170 bool indented = debug_push_indented(true);
171 debug_sym("", $$, "");
172 debug_pop_indented(indented);
173 } <y_sym>
174 %printer { fprintf(yyo, "%s", $$ ? "++" : "--"); } <y_inc>
175 %printer { fprintf(yyo, "%s", op_name($$)); } <y_op>
176 %printer { fprintf(yyo, "%s", scl_name($$)); } <y_scl>
177 %printer { fprintf(yyo, "%s", tspec_name($$)); } <y_tspec>
178 %printer { fprintf(yyo, "%s", type_qualifiers_string($$)); } <y_type_qualifiers>
179 %printer {
180 fprintf(yyo, "%s", function_specifier_name($$));
181 } <y_function_specifier>
182 %printer { fprintf(yyo, "%s", type_name($$)); } <y_type>
183 %printer {
184 if ($$ == NULL)
185 fprintf(yyo, "<null>");
186 else
187 fprintf(yyo, "%s '%s'",
188 op_name($$->tn_op), type_name($$->tn_type));
189 } <y_tnode>
190 %printer { fprintf(yyo, "%zu to %zu", $$.lo, $$.hi); } <y_range>
191 %printer { fprintf(yyo, "length %zu", $$->len); } <y_string>
192 %printer {
193 fprintf(yyo, "%s *", type_qualifiers_string($$->qualifiers));
194 } <y_qual_ptr>
195 %printer { fprintf(yyo, "%s", $$ ? "yes" : "no"); } <y_seen_statement>
196 %printer { fprintf(yyo, "%s", type_name($$->ga_arg)); } <y_generic>
197 %printer { fprintf(yyo, "%d", $$.dim); } <y_array_size>
198 %printer { fprintf(yyo, "%s", $$ ? "yes" : "no"); } <y_in_system_header>
199 */
200
201 %token T_LBRACE T_RBRACE T_LBRACK T_RBRACK T_LPAREN T_RPAREN
202 %token T_POINT T_ARROW
203 %token T_COMPLEMENT T_LOGNOT
204 %token <y_inc> T_INCDEC
205 %token T_SIZEOF
206 %token T_BUILTIN_OFFSETOF
207 %token T_TYPEOF
208 %token T_EXTENSION
209 %token T_ALIGNAS
210 %token T_ALIGNOF
211 %token T_ASTERISK
212 %token <y_op> T_MULTIPLICATIVE
213 %token <y_op> T_ADDITIVE
214 %token <y_op> T_SHIFT
215 %token <y_op> T_RELATIONAL
216 %token <y_op> T_EQUALITY
217 %token T_AMPER
218 %token T_BITXOR
219 %token T_BITOR
220 %token T_LOGAND
221 %token T_LOGOR
222 %token T_QUEST
223 %token T_COLON
224 %token T_ASSIGN
225 %token <y_op> T_OPASSIGN
226 %token T_COMMA
227 %token T_SEMI
228 %token T_ELLIPSIS
229 %token T_REAL
230 %token T_IMAG
231 %token T_GENERIC
232
233 /* storage classes (extern, static, auto, register and typedef) */
234 %token <y_scl> T_SCLASS
235 %token <y_function_specifier> T_FUNCTION_SPECIFIER
236
237 /*
238 * predefined type keywords (char, int, short, long, unsigned, signed,
239 * float, double, void); see T_TYPENAME for types from typedef
240 */
241 %token <y_tspec> T_TYPE
242
243 %token <y_type_qualifiers> T_QUAL
244 %token <y_type_qualifiers> T_ATOMIC
245
246 /* struct or union */
247 %token <y_tspec> T_STRUCT_OR_UNION
248
249 /* remaining keywords */
250 %token T_ASM
251 %token T_BREAK
252 %token T_CASE
253 %token T_CONTINUE
254 %token T_DEFAULT
255 %token T_DO
256 %token T_ELSE
257 %token T_ENUM
258 %token T_FOR
259 %token T_GOTO
260 %token T_IF
261 %token T_PACKED
262 %token T_RETURN
263 %token T_SWITCH
264 %token T_SYMBOLRENAME
265 %token T_WHILE
266 %token T_STATIC_ASSERT
267
268 %token T_ATTRIBUTE
269
270 %left T_THEN
271 %left T_ELSE
272 %right T_QUEST T_COLON
273 %left T_LOGOR
274 %left T_LOGAND
275 %left T_BITOR
276 %left T_BITXOR
277 %left T_AMPER
278 %left T_EQUALITY
279 %left T_RELATIONAL
280 %left T_SHIFT
281 %left T_ADDITIVE
282 %left T_ASTERISK T_MULTIPLICATIVE
283
284 %token <y_name> T_NAME
285 %token <y_name> T_TYPENAME
286 %token <y_val> T_CON
287 %token <y_string> T_STRING
288
289 /* No type for program. */
290 %type <y_sym> identifier_sym
291 %type <y_name> identifier
292 %type <y_string> string
293 %type <y_tnode> primary_expression
294 %type <y_designation> member_designator
295 %type <y_tnode> generic_selection
296 %type <y_generic> generic_assoc_list
297 %type <y_generic> generic_association
298 %type <y_tnode> postfix_expression
299 %type <y_tnode> gcc_statement_expr_list
300 %type <y_tnode> gcc_statement_expr_item
301 %type <y_op> point_or_arrow
302 %type <y_arguments> argument_expression_list
303 %type <y_tnode> unary_expression
304 %type <y_tnode> cast_expression
305 %type <y_tnode> expression_opt
306 %type <y_tnode> conditional_expression
307 %type <y_tnode> assignment_expression
308 %type <y_tnode> expression
309 %type <y_tnode> constant_expression
310 /* No type for declaration_or_error. */
311 /* No type for declaration. */
312 /* No type for begin_type_declaration_specifiers. */
313 /* No type for begin_type_declmods. */
314 /* No type for begin_type_specifier_qualifier_list. */
315 /* No type for begin_type_specifier_qualifier_list_postfix. */
316 %type <y_type> begin_type_typespec
317 /* No type for begin_type_qualifier_list. */
318 /* No type for declmod. */
319 /* No type for type_attribute_list_opt. */
320 /* No type for type_attribute_list. */
321 /* No type for type_attribute_opt. */
322 /* No type for type_attribute. */
323 /* No type for begin_type. */
324 /* No type for end_type. */
325 %type <y_type> type_specifier
326 %type <y_type> notype_type_specifier
327 %type <y_type> atomic_type_specifier
328 %type <y_type> struct_or_union_specifier
329 %type <y_tspec> struct_or_union
330 %type <y_sym> braced_member_declaration_list
331 %type <y_sym> member_declaration_list_with_rbrace
332 %type <y_sym> member_declaration_list
333 %type <y_sym> member_declaration
334 %type <y_sym> notype_member_declarators
335 %type <y_sym> type_member_declarators
336 %type <y_sym> notype_member_declarator
337 %type <y_sym> type_member_declarator
338 %type <y_type> enum_specifier
339 /* No type for enum. */
340 %type <y_sym> enum_declaration
341 %type <y_sym> enums_with_opt_comma
342 %type <y_sym> enumerator_list
343 %type <y_sym> enumerator
344 %type <y_type_qualifiers> type_qualifier
345 /* No type for atomic. */
346 %type <y_qual_ptr> pointer
347 %type <y_type_qualifiers> type_qualifier_list_opt
348 %type <y_type_qualifiers> type_qualifier_list
349 /* No type for notype_init_declarators. */
350 /* No type for type_init_declarators. */
351 /* No type for notype_init_declarator. */
352 /* No type for type_init_declarator. */
353 %type <y_sym> notype_declarator
354 %type <y_sym> type_declarator
355 %type <y_sym> notype_direct_declarator
356 %type <y_sym> type_direct_declarator
357 %type <y_sym> type_param_declarator
358 %type <y_sym> notype_param_declarator
359 %type <y_sym> direct_param_declarator
360 %type <y_sym> direct_notype_param_declarator
361 %type <y_parameter_list> param_list
362 %type <y_array_size> array_size_opt
363 %type <y_sym> identifier_list
364 %type <y_type> type_name
365 %type <y_sym> abstract_declaration
366 %type <y_sym> abstract_declarator
367 %type <y_sym> direct_abstract_declarator
368 %type <y_parameter_list> abstract_decl_param_list
369 /* No type for abstract_decl_lparen. */
370 %type <y_parameter_list> vararg_parameter_type_list
371 %type <y_parameter_list> parameter_type_list
372 %type <y_sym> parameter_declaration
373 /* No type for braced_initializer. */
374 /* No type for initializer. */
375 /* No type for initializer_list. */
376 /* No type for designation. */
377 /* No type for designator_list. */
378 /* No type for designator. */
379 /* No type for static_assert_declaration. */
380 %type <y_range> range
381 /* No type for init_lbrace. */
382 /* No type for init_rbrace. */
383 %type <y_name> asm_or_symbolrename_opt
384 /* No type for statement. */
385 /* No type for no_attr_statement. */
386 /* No type for non_expr_statement. */
387 /* No type for no_attr_non_expr_statement. */
388 /* No type for labeled_statement. */
389 /* No type for label. */
390 /* No type for compound_statement. */
391 /* No type for compound_statement_lbrace. */
392 /* No type for compound_statement_rbrace. */
393 %type <y_seen_statement> block_item_list
394 %type <y_seen_statement> block_item
395 /* No type for expression_statement. */
396 /* No type for selection_statement. */
397 /* No type for if_without_else. */
398 /* No type for if_expr. */
399 /* No type for switch_expr. */
400 /* No type for iteration_statement. */
401 /* No type for while_expr. */
402 /* No type for do_statement. */
403 /* No type for do. */
404 /* No type for for_start. */
405 /* No type for for_exprs. */
406 /* No type for jump_statement. */
407 /* No type for goto. */
408 /* No type for asm_statement. */
409 /* No type for read_until_rparen. */
410 /* No type for translation_unit. */
411 /* No type for external_declaration. */
412 /* No type for top_level_declaration. */
413 /* No type for function_definition. */
414 %type <y_sym> func_declarator
415 /* No type for arg_declaration_list_opt. */
416 /* No type for arg_declaration_list. */
417 /* No type for arg_declaration. */
418 /* No type for gcc_attribute_specifier_list_opt. */
419 /* No type for gcc_attribute_specifier_list. */
420 /* No type for gcc_attribute_specifier. */
421 /* No type for gcc_attribute_list. */
422 /* No type for gcc_attribute. */
423 /* No type for gcc_attribute_parameters. */
424 %type <y_in_system_header> sys
425
426 %%
427
428 program:
429 /* empty */ {
430 /* TODO: Make this an error in C99 mode as well. */
431 if (!allow_trad && !allow_c99) {
432 /* empty translation unit */
433 error(272);
434 } else if (allow_c90) {
435 /* empty translation unit */
436 warning(272);
437 }
438 }
439 | translation_unit
440 ;
441
442 identifier_sym: /* helper for struct/union/enum */
443 identifier {
444 $$ = getsym($1);
445 }
446 ;
447
448 /* K&R ???, C90 ???, C99 6.4.2.1, C11 ??? */
449 identifier:
450 T_NAME {
451 debug_step("cgram: name '%s'", $1->sb_name);
452 $$ = $1;
453 }
454 | T_TYPENAME {
455 debug_step("cgram: typename '%s'", $1->sb_name);
456 $$ = $1;
457 }
458 ;
459
460 /* see C99 6.4.5, string literals are joined by 5.1.1.2 */
461 string:
462 T_STRING
463 | string T_STRING {
464 if (!allow_c90) {
465 /* concatenated strings are illegal in traditional C */
466 warning(219);
467 }
468 $$ = cat_strings($1, $2);
469 }
470 ;
471
472 /* K&R 7.1, C90 ???, C99 6.5.1, C11 6.5.1 */
473 primary_expression:
474 T_NAME {
475 bool sys_name, sys_next;
476 sys_name = in_system_header;
477 if (yychar < 0)
478 yychar = yylex();
479 sys_next = in_system_header;
480 in_system_header = sys_name;
481 $$ = build_name(getsym($1), yychar == T_LPAREN);
482 in_system_header = sys_next;
483 }
484 | T_CON {
485 $$ = build_constant(gettyp($1->v_tspec), $1);
486 }
487 | string {
488 $$ = build_string($1);
489 }
490 | T_LPAREN expression T_RPAREN {
491 if ($2 != NULL)
492 $2->tn_parenthesized = true;
493 $$ = $2;
494 }
495 | generic_selection
496 /* GCC primary-expression, see c_parser_postfix_expression */
497 | T_BUILTIN_OFFSETOF T_LPAREN type_name T_COMMA {
498 set_sym_kind(SK_MEMBER);
499 } member_designator T_RPAREN {
500 $$ = build_offsetof($3, $6);
501 }
502 ;
503
504 /* K&R ---, C90 ---, C99 7.17p3, C11 7.19p3, C23 7.21p4 */
505 member_designator:
506 identifier {
507 $$ = (designation) { .dn_len = 0 };
508 designation_push(&$$, DK_MEMBER, getsym($1), 0);
509 }
510 | member_designator T_LBRACK range T_RBRACK {
511 $$ = $1;
512 designation_push(&$$, DK_SUBSCRIPT, NULL, $3.lo);
513 }
514 | member_designator T_POINT {
515 set_sym_kind(SK_MEMBER);
516 } identifier {
517 $$ = $1;
518 designation_push(&$$, DK_MEMBER, getsym($4), 0);
519 }
520 ;
521
522 /* K&R ---, C90 ---, C99 ---, C11 6.5.1.1 */
523 generic_selection:
524 T_GENERIC T_LPAREN assignment_expression T_COMMA
525 generic_assoc_list T_RPAREN {
526 /* generic selection requires C11 or later */
527 c11ism(345);
528 $$ = build_generic_selection($3, $5);
529 }
530 ;
531
532 /* K&R ---, C90 ---, C99 ---, C11 6.5.1.1 */
533 generic_assoc_list:
534 generic_association
535 | generic_assoc_list T_COMMA generic_association {
536 $3->ga_prev = $1;
537 $$ = $3;
538 }
539 ;
540
541 /* K&R ---, C90 ---, C99 ---, C11 6.5.1.1 */
542 generic_association:
543 type_name T_COLON assignment_expression {
544 $$ = block_zero_alloc(sizeof(*$$), "generic");
545 $$->ga_arg = $1;
546 $$->ga_result = $3;
547 }
548 | T_DEFAULT T_COLON assignment_expression {
549 $$ = block_zero_alloc(sizeof(*$$), "generic");
550 $$->ga_arg = NULL;
551 $$->ga_result = $3;
552 }
553 ;
554
555 /* K&R 7.1, C90 ???, C99 6.5.2, C11 6.5.2, C23 6.5.2 */
556 postfix_expression:
557 primary_expression
558 | postfix_expression T_LBRACK sys expression T_RBRACK {
559 $$ = build_unary(INDIR, $3, build_binary($1, PLUS, $3, $4));
560 }
561 | postfix_expression T_LPAREN sys T_RPAREN {
562 function_call *call =
563 expr_zero_alloc(sizeof(*call), "function_call");
564 $$ = build_function_call($1, $3, call);
565 }
566 | postfix_expression T_LPAREN sys argument_expression_list T_RPAREN {
567 $$ = build_function_call($1, $3, $4);
568 }
569 | postfix_expression point_or_arrow sys T_NAME {
570 $$ = build_member_access($1, $2, $3, $4);
571 }
572 | postfix_expression T_INCDEC sys {
573 $$ = build_unary($2 ? INCAFT : DECAFT, $3, $1);
574 }
575 | T_LPAREN type_name T_RPAREN { /* C99 6.5.2.5 "Compound literals" */
576 sym_t *tmp = mktempsym($2);
577 begin_initialization(tmp);
578 cgram_declare(tmp, true, NULL);
579 } braced_initializer {
580 if (!allow_c99)
581 /* compound literals are a C99/GCC extension */
582 gnuism(319);
583 $$ = build_name(current_initsym(), false);
584 end_initialization();
585 }
586 | T_LPAREN compound_statement_lbrace {
587 begin_statement_expr();
588 } gcc_statement_expr_list {
589 do_statement_expr($4);
590 } compound_statement_rbrace T_RPAREN {
591 $$ = end_statement_expr();
592 }
593 ;
594
595 /*
596 * The inner part of a GCC statement-expression of the form ({ ... }).
597 *
598 * https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
599 */
600 gcc_statement_expr_list:
601 gcc_statement_expr_item
602 | gcc_statement_expr_list gcc_statement_expr_item {
603 $$ = $2;
604 }
605 ;
606
607 gcc_statement_expr_item:
608 declaration_or_error {
609 clear_warning_flags();
610 $$ = NULL;
611 }
612 | non_expr_statement {
613 $$ = expr_alloc_tnode();
614 $$->tn_type = gettyp(VOID);
615 }
616 | T_SEMI {
617 $$ = expr_alloc_tnode();
618 $$->tn_type = gettyp(VOID);
619 }
620 | expression T_SEMI {
621 if ($1 == NULL) { /* in case of syntax errors */
622 $$ = expr_alloc_tnode();
623 $$->tn_type = gettyp(VOID);
624 } else {
625 /* XXX: do that only on the last name */
626 if ($1->tn_op == NAME)
627 $1->tn_sym->s_used = true;
628 expr($1, false, false, false, false);
629 suppress_fallthrough = false;
630 $$ = $1;
631 }
632 }
633 ;
634
635 point_or_arrow: /* helper for 'postfix_expression' */
636 T_POINT {
637 set_sym_kind(SK_MEMBER);
638 $$ = POINT;
639 }
640 | T_ARROW {
641 set_sym_kind(SK_MEMBER);
642 $$ = ARROW;
643 }
644 ;
645
646 /* K&R 7.1, C90 ???, C99 6.5.2, C11 6.5.2 */
647 argument_expression_list:
648 assignment_expression {
649 $$ = expr_zero_alloc(sizeof(*$$), "function_call");
650 add_function_argument($$, $1);
651 }
652 | argument_expression_list T_COMMA assignment_expression {
653 add_function_argument($1, $3);
654 }
655 ;
656
657 /* K&R 7.2, C90 ???, C99 6.5.3, C11 6.5.3 */
658 unary_expression:
659 postfix_expression
660 | T_INCDEC sys unary_expression {
661 $$ = build_unary($1 ? INCBEF : DECBEF, $2, $3);
662 }
663 | T_AMPER sys cast_expression {
664 $$ = build_unary(ADDR, $2, $3);
665 }
666 | T_ASTERISK sys cast_expression {
667 $$ = build_unary(INDIR, $2, $3);
668 }
669 | T_ADDITIVE sys cast_expression {
670 if (!allow_c90 && $1 == PLUS) {
671 /* unary '+' is illegal in traditional C */
672 warning(100);
673 }
674 $$ = build_unary($1 == PLUS ? UPLUS : UMINUS, $2, $3);
675 }
676 | T_COMPLEMENT sys cast_expression {
677 $$ = build_unary(COMPL, $2, $3);
678 }
679 | T_LOGNOT sys cast_expression {
680 $$ = build_unary(NOT, $2, $3);
681 }
682 | T_REAL sys cast_expression { /* GCC c_parser_unary_expression */
683 $$ = build_unary(REAL, $2, $3);
684 }
685 | T_IMAG sys cast_expression { /* GCC c_parser_unary_expression */
686 $$ = build_unary(IMAG, $2, $3);
687 }
688 | T_EXTENSION cast_expression { /* GCC c_parser_unary_expression */
689 $$ = $2;
690 }
691 | T_SIZEOF unary_expression {
692 $$ = $2 == NULL ? NULL : build_sizeof($2->tn_type);
693 if ($$ != NULL)
694 check_expr_misc($2,
695 false, false, false, false, false, true);
696 }
697 | T_SIZEOF T_LPAREN type_name T_RPAREN {
698 $$ = build_sizeof($3);
699 }
700 | T_ALIGNOF unary_expression {
701 /* non type argument to alignof is a GCC extension */
702 gnuism(349);
703 lint_assert($2 != NULL);
704 $$ = build_alignof($2->tn_type);
705 }
706 /* K&R ---, C90 ---, C99 ---, C11 6.5.3 */
707 | T_ALIGNOF T_LPAREN type_name T_RPAREN {
708 /* TODO: c11ism */
709 $$ = build_alignof($3);
710 }
711 ;
712
713 /* The rule 'unary_operator' is inlined into unary_expression. */
714
715 /* K&R 7.2, C90 ???, C99 6.5.4, C11 6.5.4 */
716 cast_expression:
717 unary_expression
718 | T_LPAREN type_name T_RPAREN sys cast_expression {
719 $$ = cast($5, $4, $2);
720 }
721 ;
722
723 expression_opt:
724 /* empty */ {
725 $$ = NULL;
726 }
727 | expression
728 ;
729
730 /* 'conditional_expression' also implements 'multiplicative_expression'. */
731 /* 'conditional_expression' also implements 'additive_expression'. */
732 /* 'conditional_expression' also implements 'shift_expression'. */
733 /* 'conditional_expression' also implements 'relational_expression'. */
734 /* 'conditional_expression' also implements 'equality_expression'. */
735 /* 'conditional_expression' also implements 'AND_expression'. */
736 /* 'conditional_expression' also implements 'exclusive_OR_expression'. */
737 /* 'conditional_expression' also implements 'inclusive_OR_expression'. */
738 /* 'conditional_expression' also implements 'logical_AND_expression'. */
739 /* 'conditional_expression' also implements 'logical_OR_expression'. */
740 /* K&R ???, C90 ???, C99 6.5.5 to 6.5.15, C11 6.5.5 to 6.5.15 */
741 conditional_expression:
742 cast_expression
743 | conditional_expression T_ASTERISK sys conditional_expression {
744 $$ = build_binary($1, MULT, $3, $4);
745 }
746 | conditional_expression T_MULTIPLICATIVE sys conditional_expression {
747 $$ = build_binary($1, $2, $3, $4);
748 }
749 | conditional_expression T_ADDITIVE sys conditional_expression {
750 $$ = build_binary($1, $2, $3, $4);
751 }
752 | conditional_expression T_SHIFT sys conditional_expression {
753 $$ = build_binary($1, $2, $3, $4);
754 }
755 | conditional_expression T_RELATIONAL sys conditional_expression {
756 $$ = build_binary($1, $2, $3, $4);
757 }
758 | conditional_expression T_EQUALITY sys conditional_expression {
759 $$ = build_binary($1, $2, $3, $4);
760 }
761 | conditional_expression T_AMPER sys conditional_expression {
762 $$ = build_binary($1, BITAND, $3, $4);
763 }
764 | conditional_expression T_BITXOR sys conditional_expression {
765 $$ = build_binary($1, BITXOR, $3, $4);
766 }
767 | conditional_expression T_BITOR sys conditional_expression {
768 $$ = build_binary($1, BITOR, $3, $4);
769 }
770 | conditional_expression T_LOGAND sys conditional_expression {
771 $$ = build_binary($1, LOGAND, $3, $4);
772 }
773 | conditional_expression T_LOGOR sys conditional_expression {
774 $$ = build_binary($1, LOGOR, $3, $4);
775 }
776 | conditional_expression T_QUEST sys
777 expression T_COLON sys conditional_expression {
778 $$ = build_binary($1, QUEST, $3,
779 build_binary($4, COLON, $6, $7));
780 }
781 ;
782
783 /* K&R ???, C90 ???, C99 6.5.16, C11 6.5.16 */
784 assignment_expression:
785 conditional_expression
786 | unary_expression T_ASSIGN sys assignment_expression {
787 $$ = build_binary($1, ASSIGN, $3, $4);
788 }
789 | unary_expression T_OPASSIGN sys assignment_expression {
790 $$ = build_binary($1, $2, $3, $4);
791 }
792 ;
793
794 /* K&R ???, C90 ???, C99 6.5.17, C11 6.5.17 */
795 expression:
796 assignment_expression
797 | expression T_COMMA sys assignment_expression {
798 $$ = build_binary($1, COMMA, $3, $4);
799 }
800 ;
801
802 /* K&R ???, C90 ???, C99 6.6, C11 ???, C23 6.6 */
803 constant_expression:
804 conditional_expression
805 ;
806
807 declaration_or_error:
808 declaration
809 | error T_SEMI
810 ;
811
812 /* K&R ???, C90 ???, C99 6.7, C11 ???, C23 6.7 */
813 declaration:
814 begin_type_declmods end_type T_SEMI {
815 if (dcs->d_scl == TYPEDEF) {
816 /* typedef declares no type name */
817 warning(72);
818 } else {
819 /* empty declaration */
820 warning(2);
821 }
822 }
823 | begin_type_declmods end_type notype_init_declarators T_SEMI {
824 if (dcs->d_scl == TYPEDEF) {
825 /* syntax error '%s' */
826 error(249, "missing base type for typedef");
827 } else {
828 /* old-style declaration; add 'int' */
829 error(1);
830 }
831 }
832 | begin_type_declaration_specifiers end_type T_SEMI {
833 if (dcs->d_scl == TYPEDEF) {
834 /* typedef declares no type name */
835 warning(72);
836 } else if (!dcs->d_nonempty_decl) {
837 /* empty declaration */
838 warning(2);
839 }
840 }
841 | begin_type_declaration_specifiers end_type
842 type_init_declarators T_SEMI
843 | static_assert_declaration
844 ;
845
846 begin_type_declaration_specifiers: /* see C99 6.7 */
847 begin_type_typespec {
848 dcs_add_type($1);
849 }
850 | begin_type_declmods type_specifier {
851 dcs_add_type($2);
852 }
853 | type_attribute begin_type_declaration_specifiers
854 | begin_type_declaration_specifiers declmod
855 | begin_type_declaration_specifiers notype_type_specifier {
856 dcs_add_type($2);
857 }
858 ;
859
860 begin_type_declmods: /* see C99 6.7 */
861 begin_type type_qualifier {
862 dcs_add_qualifiers($2);
863 }
864 | begin_type T_SCLASS {
865 dcs_add_storage_class($2);
866 }
867 | begin_type T_FUNCTION_SPECIFIER {
868 dcs_add_function_specifier($2);
869 }
870 | begin_type_declmods declmod
871 ;
872
873 begin_type_specifier_qualifier_list: /* see C11 6.7.2.1 */
874 begin_type_specifier_qualifier_list_postfix
875 | type_attribute_list begin_type_specifier_qualifier_list_postfix
876 ;
877
878 begin_type_specifier_qualifier_list_postfix:
879 begin_type_typespec {
880 dcs_add_type($1);
881 }
882 | begin_type_qualifier_list type_specifier {
883 dcs_add_type($2);
884 }
885 | begin_type_specifier_qualifier_list_postfix type_qualifier {
886 dcs_add_qualifiers($2);
887 }
888 | begin_type_specifier_qualifier_list_postfix notype_type_specifier {
889 dcs_add_type($2);
890 }
891 | begin_type_specifier_qualifier_list_postfix type_attribute
892 ;
893
894 begin_type_typespec:
895 begin_type notype_type_specifier {
896 $$ = $2;
897 }
898 | begin_type T_TYPENAME {
899 $$ = getsym($2)->s_type;
900 }
901 ;
902
903 begin_type_qualifier_list:
904 begin_type type_qualifier {
905 dcs_add_qualifiers($2);
906 }
907 | begin_type_qualifier_list type_qualifier {
908 dcs_add_qualifiers($2);
909 }
910 ;
911
912 declmod:
913 type_qualifier {
914 dcs_add_qualifiers($1);
915 }
916 | T_SCLASS {
917 dcs_add_storage_class($1);
918 }
919 | T_FUNCTION_SPECIFIER {
920 dcs_add_function_specifier($1);
921 }
922 | type_attribute_list
923 ;
924
925 type_attribute_list_opt:
926 /* empty */
927 | type_attribute_list
928 ;
929
930 type_attribute_list:
931 type_attribute
932 | type_attribute_list type_attribute
933 ;
934
935 type_attribute_opt:
936 /* empty */
937 | type_attribute
938 ;
939
940 type_attribute: /* See C11 6.7 declaration-specifiers */
941 gcc_attribute_specifier
942 | T_ALIGNAS T_LPAREN type_specifier T_RPAREN /* C11 6.7.5 */
943 | T_ALIGNAS T_LPAREN constant_expression T_RPAREN /* C11 6.7.5 */
944 | T_PACKED {
945 dcs_add_packed();
946 }
947 ;
948
949 begin_type:
950 /* empty */ {
951 dcs_begin_type();
952 }
953 ;
954
955 end_type:
956 /* empty */ {
957 dcs_end_type();
958 }
959 ;
960
961 type_specifier: /* C99 6.7.2 */
962 notype_type_specifier
963 | T_TYPENAME {
964 $$ = getsym($1)->s_type;
965 }
966 ;
967
968 notype_type_specifier: /* see C99 6.7.2 */
969 T_TYPE {
970 $$ = gettyp($1);
971 }
972 | T_TYPEOF T_LPAREN expression T_RPAREN { /* GCC extension */
973 $$ = $3 != NULL ? block_dup_type($3->tn_type) : gettyp(INT);
974 $$->t_typeof = true;
975 }
976 | atomic_type_specifier
977 | struct_or_union_specifier {
978 end_declaration_level();
979 $$ = $1;
980 }
981 | enum_specifier {
982 end_declaration_level();
983 $$ = $1;
984 }
985 ;
986
987 /* K&R ---, C90 ---, C99 ---, C11 6.7.2.4 */
988 atomic_type_specifier:
989 atomic T_LPAREN type_name T_RPAREN {
990 $$ = $3;
991 }
992 ;
993
994 /* K&R ---, C90 ---, C99 6.7.2.1, C11 ???, C23 6.7.2.1 */
995 struct_or_union_specifier:
996 struct_or_union identifier_sym {
997 /*
998 * STDC requires that "struct a;" always introduces
999 * a new tag if "a" is not declared at current level
1000 *
1001 * yychar is valid because otherwise the parser would not
1002 * have been able to decide if it must shift or reduce
1003 */
1004 $$ = make_tag_type($2, $1, false, yychar == T_SEMI);
1005 }
1006 | struct_or_union identifier_sym {
1007 dcs->d_tag_type = make_tag_type($2, $1, true, false);
1008 } braced_member_declaration_list {
1009 $$ = complete_struct_or_union($4);
1010 }
1011 | struct_or_union {
1012 dcs->d_tag_type = make_tag_type(NULL, $1, true, false);
1013 } braced_member_declaration_list {
1014 $$ = complete_struct_or_union($3);
1015 }
1016 | struct_or_union error {
1017 set_sym_kind(SK_VCFT);
1018 $$ = gettyp(INT);
1019 }
1020 ;
1021
1022 /* K&R ---, C90 ---, C99 6.7.2.1, C11 ???, C23 6.7.2.1 */
1023 struct_or_union:
1024 T_STRUCT_OR_UNION {
1025 set_sym_kind(SK_TAG);
1026 begin_declaration_level($1 == STRUCT ? DLK_STRUCT : DLK_UNION);
1027 dcs->d_sou_size_in_bits = 0;
1028 dcs->d_sou_align_in_bits = CHAR_SIZE;
1029 $$ = $1;
1030 }
1031 | struct_or_union type_attribute
1032 ;
1033
1034 braced_member_declaration_list: /* see C99 6.7.2.1 */
1035 T_LBRACE {
1036 set_sym_kind(SK_VCFT);
1037 } member_declaration_list_with_rbrace {
1038 $$ = $3;
1039 }
1040 ;
1041
1042 member_declaration_list_with_rbrace: /* see C99 6.7.2.1 */
1043 member_declaration_list T_RBRACE
1044 | T_RBRACE {
1045 /* XXX: This is not allowed by any C standard. */
1046 $$ = NULL;
1047 }
1048 ;
1049
1050 /* K&R ???, C90 ???, C99 6.7.2.1, C11 6.7.2.1, C23 6.7.2.1 */
1051 /* Was named struct_declaration_list until C11. */
1052 member_declaration_list:
1053 member_declaration
1054 | member_declaration_list member_declaration {
1055 $$ = concat_symbols($1, $2);
1056 }
1057 ;
1058
1059 /* Was named struct_declaration until C11. */
1060 /* K&R ???, C90 ???, C99 6.7.2.1, C11 6.7.2.1, C23 6.7.2.1 */
1061 member_declaration:
1062 begin_type_qualifier_list end_type {
1063 /* ^^ There is no check for the missing type-specifier. */
1064 /* too late, i know, but getsym() compensates it */
1065 set_sym_kind(SK_MEMBER);
1066 } notype_member_declarators T_SEMI {
1067 set_sym_kind(SK_VCFT);
1068 $$ = $4;
1069 }
1070 | begin_type_specifier_qualifier_list end_type {
1071 set_sym_kind(SK_MEMBER);
1072 } type_member_declarators T_SEMI {
1073 set_sym_kind(SK_VCFT);
1074 $$ = $4;
1075 }
1076 | begin_type_qualifier_list end_type type_attribute_opt T_SEMI {
1077 /* syntax error '%s' */
1078 error(249, "member without type");
1079 $$ = NULL;
1080 }
1081 | begin_type_specifier_qualifier_list end_type type_attribute_opt
1082 T_SEMI {
1083 set_sym_kind(SK_VCFT);
1084 if (!allow_c11 && !allow_gcc)
1085 /* anonymous struct/union members is a C11 feature */
1086 warning(49);
1087 if (is_struct_or_union(dcs->d_type->t_tspec))
1088 $$ = declare_unnamed_member();
1089 else {
1090 /* syntax error '%s' */
1091 error(249, "unnamed member");
1092 $$ = NULL;
1093 }
1094 }
1095 | static_assert_declaration {
1096 $$ = NULL;
1097 }
1098 | error T_SEMI {
1099 set_sym_kind(SK_VCFT);
1100 $$ = NULL;
1101 }
1102 ;
1103
1104 /* Was named struct_declarators until C11. */
1105 notype_member_declarators:
1106 notype_member_declarator {
1107 $$ = declare_member($1);
1108 }
1109 | notype_member_declarators {
1110 set_sym_kind(SK_MEMBER);
1111 } T_COMMA type_member_declarator {
1112 $$ = concat_symbols($1, declare_member($4));
1113 }
1114 ;
1115
1116 /* Was named struct_declarators until C11. */
1117 type_member_declarators:
1118 type_member_declarator {
1119 $$ = declare_member($1);
1120 }
1121 | type_member_declarators {
1122 set_sym_kind(SK_MEMBER);
1123 } T_COMMA type_member_declarator {
1124 $$ = concat_symbols($1, declare_member($4));
1125 }
1126 ;
1127
1128 /* Was named struct_declarator until C11. */
1129 notype_member_declarator:
1130 notype_declarator
1131 /* C99 6.7.2.1 */
1132 | notype_declarator T_COLON constant_expression {
1133 $$ = set_bit_field_width($1, to_int_constant($3, true));
1134 }
1135 /* C99 6.7.2.1 */
1136 | {
1137 set_sym_kind(SK_VCFT);
1138 } T_COLON constant_expression {
1139 $$ = set_bit_field_width(NULL, to_int_constant($3, true));
1140 }
1141 ;
1142
1143 /* Was named struct_declarator until C11. */
1144 type_member_declarator:
1145 type_declarator
1146 | type_declarator T_COLON constant_expression type_attribute_list_opt {
1147 $$ = set_bit_field_width($1, to_int_constant($3, true));
1148 }
1149 | {
1150 set_sym_kind(SK_VCFT);
1151 } T_COLON constant_expression type_attribute_list_opt {
1152 $$ = set_bit_field_width(NULL, to_int_constant($3, true));
1153 }
1154 ;
1155
1156 /* K&R ---, C90 6.5.2.2, C99 6.7.2.2, C11 6.7.2.2 */
1157 enum_specifier:
1158 enum gcc_attribute_specifier_list_opt identifier_sym {
1159 $$ = make_tag_type($3, ENUM, false, false);
1160 }
1161 | enum gcc_attribute_specifier_list_opt identifier_sym {
1162 dcs->d_tag_type = make_tag_type($3, ENUM, true, false);
1163 } enum_declaration /*gcc_attribute_specifier_list_opt*/ {
1164 $$ = complete_enum($5);
1165 }
1166 | enum gcc_attribute_specifier_list_opt {
1167 dcs->d_tag_type = make_tag_type(NULL, ENUM, true, false);
1168 } enum_declaration /*gcc_attribute_specifier_list_opt*/ {
1169 $$ = complete_enum($4);
1170 }
1171 | enum error {
1172 set_sym_kind(SK_VCFT);
1173 $$ = gettyp(INT);
1174 }
1175 ;
1176
1177 enum: /* helper for C99 6.7.2.2 */
1178 T_ENUM {
1179 set_sym_kind(SK_TAG);
1180 begin_declaration_level(DLK_ENUM);
1181 }
1182 ;
1183
1184 enum_declaration: /* helper for C99 6.7.2.2 */
1185 T_LBRACE {
1186 set_sym_kind(SK_VCFT);
1187 enumval = 0;
1188 } enums_with_opt_comma T_RBRACE {
1189 $$ = $3;
1190 }
1191 ;
1192
1193 enums_with_opt_comma: /* helper for C99 6.7.2.2 */
1194 enumerator_list
1195 | enumerator_list T_COMMA {
1196 if (!allow_c99 && !allow_trad) {
1197 /* trailing ',' in enum declaration requires C99 ... */
1198 error(54);
1199 } else {
1200 /* trailing ',' in enum declaration requires C99 ... */
1201 c99ism(54);
1202 }
1203 $$ = $1;
1204 }
1205 ;
1206
1207 enumerator_list: /* C99 6.7.2.2 */
1208 enumerator
1209 | enumerator_list T_COMMA enumerator {
1210 $$ = concat_symbols($1, $3);
1211 }
1212 | error {
1213 $$ = NULL;
1214 }
1215 ;
1216
1217 enumerator: /* C99 6.7.2.2 */
1218 identifier_sym gcc_attribute_specifier_list_opt {
1219 $$ = enumeration_constant($1, enumval, true);
1220 }
1221 | identifier_sym gcc_attribute_specifier_list_opt
1222 T_ASSIGN constant_expression {
1223 $$ = enumeration_constant($1, to_int_constant($4, true),
1224 false);
1225 }
1226 ;
1227
1228 type_qualifier: /* C99 6.7.3 */
1229 T_QUAL
1230 | atomic {
1231 $$ = (type_qualifiers){ .tq_atomic = true };
1232 }
1233 ;
1234
1235 atomic: /* helper */
1236 T_ATOMIC {
1237 /* TODO: First fix c11ism, then use it here. */
1238 if (!allow_c11)
1239 /* '_Atomic' requires C11 or later */
1240 error(350);
1241 }
1242 ;
1243
1244 pointer: /* C99 6.7.5 */
1245 T_ASTERISK type_qualifier_list_opt {
1246 $$ = xcalloc(1, sizeof(*$$));
1247 add_type_qualifiers(&$$->qualifiers, $2);
1248 }
1249 | T_ASTERISK type_qualifier_list_opt pointer {
1250 $$ = xcalloc(1, sizeof(*$$));
1251 add_type_qualifiers(&$$->qualifiers, $2);
1252 $$ = append_qualified_pointer($$, $3);
1253 }
1254 ;
1255
1256 type_qualifier_list_opt: /* see C99 6.7.5 */
1257 /* empty */ {
1258 $$ = (type_qualifiers){ .tq_const = false };
1259 }
1260 | type_qualifier_list
1261 ;
1262
1263 type_qualifier_list: /* C99 6.7.5 */
1264 type_qualifier
1265 | type_qualifier_list type_qualifier {
1266 $$ = $1;
1267 add_type_qualifiers(&$$, $2);
1268 }
1269 ;
1270
1271 /*
1272 * For an explanation of 'notype' in the following rules, see
1273 * https://www.gnu.org/software/bison/manual/bison.html#Semantic-Tokens.
1274 */
1275
1276 notype_init_declarators:
1277 notype_init_declarator
1278 | notype_init_declarators T_COMMA type_init_declarator
1279 ;
1280
1281 type_init_declarators:
1282 type_init_declarator
1283 | type_init_declarators T_COMMA type_init_declarator
1284 ;
1285
1286 notype_init_declarator:
1287 notype_declarator asm_or_symbolrename_opt {
1288 cgram_declare($1, false, $2);
1289 check_size($1);
1290 }
1291 | notype_declarator asm_or_symbolrename_opt {
1292 begin_initialization($1);
1293 cgram_declare($1, true, $2);
1294 } T_ASSIGN initializer {
1295 check_size($1);
1296 end_initialization();
1297 }
1298 ;
1299
1300 type_init_declarator:
1301 type_declarator asm_or_symbolrename_opt {
1302 cgram_declare($1, false, $2);
1303 check_size($1);
1304 }
1305 | type_declarator asm_or_symbolrename_opt {
1306 begin_initialization($1);
1307 cgram_declare($1, true, $2);
1308 } T_ASSIGN initializer {
1309 check_size($1);
1310 end_initialization();
1311 }
1312 ;
1313
1314 notype_declarator:
1315 notype_direct_declarator
1316 | pointer notype_direct_declarator {
1317 $$ = add_pointer($2, $1);
1318 }
1319 ;
1320
1321 type_declarator:
1322 type_direct_declarator
1323 | pointer type_direct_declarator {
1324 $$ = add_pointer($2, $1);
1325 }
1326 ;
1327
1328 notype_direct_declarator:
1329 type_attribute_list_opt T_NAME {
1330 $$ = declarator_name(getsym($2));
1331 }
1332 | type_attribute_list_opt T_LPAREN type_declarator T_RPAREN {
1333 $$ = $3;
1334 }
1335 | notype_direct_declarator T_LBRACK array_size_opt T_RBRACK {
1336 $$ = add_array($1, $3.has_dim, $3.dim);
1337 }
1338 | notype_direct_declarator param_list asm_or_symbolrename_opt {
1339 $$ = add_function(symbolrename($1, $3), $2);
1340 end_declaration_level();
1341 block_level--;
1342 }
1343 | notype_direct_declarator type_attribute
1344 ;
1345
1346 type_direct_declarator:
1347 type_attribute_list_opt identifier {
1348 $$ = declarator_name(getsym($2));
1349 }
1350 | type_attribute_list_opt T_LPAREN type_declarator T_RPAREN {
1351 $$ = $3;
1352 }
1353 | type_direct_declarator T_LBRACK array_size_opt T_RBRACK {
1354 $$ = add_array($1, $3.has_dim, $3.dim);
1355 }
1356 | type_direct_declarator param_list asm_or_symbolrename_opt {
1357 $$ = add_function(symbolrename($1, $3), $2);
1358 end_declaration_level();
1359 block_level--;
1360 }
1361 | type_direct_declarator type_attribute
1362 ;
1363
1364 /*
1365 * The two distinct rules type_param_declarator and notype_param_declarator
1366 * avoid a conflict in parameter lists. A typename enclosed in parentheses is
1367 * always treated as a typename, not an argument name. For example, after
1368 * "typedef double a;", the declaration "f(int (a));" is interpreted as
1369 * "f(int (double));", not "f(int a);".
1370 */
1371 type_param_declarator:
1372 direct_param_declarator
1373 | pointer direct_param_declarator {
1374 $$ = add_pointer($2, $1);
1375 }
1376 ;
1377
1378 notype_param_declarator:
1379 direct_notype_param_declarator
1380 | pointer direct_notype_param_declarator {
1381 $$ = add_pointer($2, $1);
1382 }
1383 ;
1384
1385 direct_param_declarator:
1386 identifier type_attribute_list {
1387 $$ = declarator_name(getsym($1));
1388 }
1389 | identifier {
1390 $$ = declarator_name(getsym($1));
1391 }
1392 | T_LPAREN notype_param_declarator T_RPAREN {
1393 $$ = $2;
1394 }
1395 | direct_param_declarator T_LBRACK array_size_opt T_RBRACK
1396 gcc_attribute_specifier_list_opt {
1397 $$ = add_array($1, $3.has_dim, $3.dim);
1398 }
1399 | direct_param_declarator param_list asm_or_symbolrename_opt {
1400 $$ = add_function(symbolrename($1, $3), $2);
1401 end_declaration_level();
1402 block_level--;
1403 }
1404 ;
1405
1406 direct_notype_param_declarator:
1407 identifier {
1408 $$ = declarator_name(getsym($1));
1409 }
1410 | T_LPAREN notype_param_declarator T_RPAREN {
1411 $$ = $2;
1412 }
1413 | direct_notype_param_declarator T_LBRACK array_size_opt T_RBRACK {
1414 $$ = add_array($1, $3.has_dim, $3.dim);
1415 }
1416 | direct_notype_param_declarator param_list asm_or_symbolrename_opt {
1417 $$ = add_function(symbolrename($1, $3), $2);
1418 end_declaration_level();
1419 block_level--;
1420 }
1421 ;
1422
1423 param_list:
1424 T_LPAREN {
1425 block_level++;
1426 begin_declaration_level(DLK_PROTO_PARAMS);
1427 } identifier_list T_RPAREN {
1428 $$ = (struct parameter_list){ .first = $3 };
1429 }
1430 | abstract_decl_param_list
1431 ;
1432
1433 array_size_opt:
1434 /* empty */ {
1435 $$.has_dim = false;
1436 $$.dim = 0;
1437 }
1438 | T_ASTERISK {
1439 /* since C99; variable length array of unspecified size */
1440 $$.has_dim = false; /* TODO: maybe change to true */
1441 $$.dim = 0; /* just as a placeholder */
1442 }
1443 | type_qualifier_list_opt T_SCLASS constant_expression {
1444 /* C11 6.7.6.3p7 */
1445 if ($2 != STATIC)
1446 yyerror("Bad attribute");
1447 /* static array size requires C11 or later */
1448 c11ism(343);
1449 $$.has_dim = true;
1450 $$.dim = $3 == NULL ? 0 : to_int_constant($3, false);
1451 }
1452 | type_qualifier {
1453 /* C11 6.7.6.2 */
1454 if (!$1.tq_restrict)
1455 yyerror("Bad attribute");
1456 $$.has_dim = true;
1457 $$.dim = 0;
1458 }
1459 | constant_expression {
1460 $$.has_dim = true;
1461 $$.dim = $1 == NULL ? 0 : to_int_constant($1, false);
1462 }
1463 ;
1464
1465 identifier_list: /* C99 6.7.5 */
1466 T_NAME {
1467 $$ = old_style_function_parameter_name(getsym($1));
1468 }
1469 | identifier_list T_COMMA T_NAME {
1470 $$ = concat_symbols($1,
1471 old_style_function_parameter_name(getsym($3)));
1472 }
1473 | identifier_list error
1474 ;
1475
1476 /* XXX: C99 requires an additional specifier-qualifier-list. */
1477 type_name: /* C99 6.7.6 */
1478 {
1479 begin_declaration_level(DLK_ABSTRACT);
1480 } abstract_declaration {
1481 end_declaration_level();
1482 $$ = $2->s_type;
1483 }
1484 ;
1485
1486 abstract_declaration: /* specific to lint */
1487 begin_type_qualifier_list end_type {
1488 $$ = declare_abstract_type(abstract_name());
1489 }
1490 | begin_type_specifier_qualifier_list end_type {
1491 $$ = declare_abstract_type(abstract_name());
1492 }
1493 | begin_type_qualifier_list end_type abstract_declarator {
1494 $$ = declare_abstract_type($3);
1495 }
1496 | begin_type_specifier_qualifier_list end_type abstract_declarator {
1497 $$ = declare_abstract_type($3);
1498 }
1499 ;
1500
1501 /* K&R 8.7, C90 ???, C99 6.7.6, C11 6.7.7 */
1502 /* In K&R, abstract-declarator could be empty and was still simpler. */
1503 abstract_declarator:
1504 pointer {
1505 $$ = add_pointer(abstract_name(), $1);
1506 }
1507 | direct_abstract_declarator
1508 | pointer direct_abstract_declarator {
1509 $$ = add_pointer($2, $1);
1510 }
1511 | type_attribute_list direct_abstract_declarator {
1512 $$ = $2;
1513 }
1514 | pointer type_attribute_list direct_abstract_declarator {
1515 $$ = add_pointer($3, $1);
1516 }
1517 ;
1518
1519 /* K&R ---, C90 ???, C99 6.7.6, C11 6.7.7 */
1520 direct_abstract_declarator:
1521 /* TODO: sort rules according to C99 */
1522 T_LPAREN abstract_declarator T_RPAREN {
1523 $$ = $2;
1524 }
1525 | T_LBRACK array_size_opt T_RBRACK {
1526 $$ = add_array(abstract_name(), $2.has_dim, $2.dim);
1527 }
1528 | direct_abstract_declarator T_LBRACK array_size_opt T_RBRACK {
1529 $$ = add_array($1, $3.has_dim, $3.dim);
1530 }
1531 | abstract_decl_param_list asm_or_symbolrename_opt {
1532 sym_t *name = abstract_enclosing_name();
1533 $$ = add_function(symbolrename(name, $2), $1);
1534 end_declaration_level();
1535 block_level--;
1536 }
1537 | direct_abstract_declarator abstract_decl_param_list
1538 asm_or_symbolrename_opt {
1539 $$ = add_function(symbolrename($1, $3), $2);
1540 end_declaration_level();
1541 block_level--;
1542 }
1543 | direct_abstract_declarator type_attribute_list
1544 ;
1545
1546 abstract_decl_param_list: /* specific to lint */
1547 abstract_decl_lparen T_RPAREN type_attribute_opt {
1548 $$ = (struct parameter_list){ .first = NULL };
1549 }
1550 | abstract_decl_lparen vararg_parameter_type_list T_RPAREN
1551 type_attribute_opt {
1552 $$ = $2;
1553 $$.prototype = true;
1554 }
1555 | abstract_decl_lparen error T_RPAREN type_attribute_opt {
1556 $$ = (struct parameter_list){ .first = NULL };
1557 }
1558 ;
1559
1560 abstract_decl_lparen: /* specific to lint */
1561 T_LPAREN {
1562 block_level++;
1563 begin_declaration_level(DLK_PROTO_PARAMS);
1564 }
1565 ;
1566
1567 vararg_parameter_type_list: /* specific to lint */
1568 parameter_type_list
1569 | parameter_type_list T_COMMA T_ELLIPSIS {
1570 $$ = $1;
1571 $$.vararg = true;
1572 }
1573 | T_ELLIPSIS {
1574 /* TODO: C99 6.7.5 makes this an error as well. */
1575 if (!allow_trad && !allow_c99) {
1576 /* C90 to C17 require formal parameter before '...' */
1577 error(84);
1578 } else if (allow_c90) {
1579 /* C90 to C17 require formal parameter before '...' */
1580 warning(84);
1581 }
1582 $$ = (struct parameter_list){ .vararg = true };
1583 }
1584 ;
1585
1586 /* XXX: C99 6.7.5 defines the same name, but it looks different. */
1587 parameter_type_list:
1588 parameter_declaration {
1589 $$ = (struct parameter_list){ .first = $1 };
1590 }
1591 | parameter_type_list T_COMMA parameter_declaration {
1592 $$ = $1;
1593 $$.first = concat_symbols($1.first, $3);
1594 }
1595 ;
1596
1597 /* XXX: C99 6.7.5 defines the same name, but it looks completely different. */
1598 parameter_declaration:
1599 begin_type_declmods end_type {
1600 /* ^^ There is no check for the missing type-specifier. */
1601 $$ = declare_parameter(abstract_name(), false);
1602 }
1603 | begin_type_declaration_specifiers end_type {
1604 $$ = declare_parameter(abstract_name(), false);
1605 }
1606 | begin_type_declmods end_type notype_param_declarator {
1607 /* ^^ There is no check for the missing type-specifier. */
1608 $$ = declare_parameter($3, false);
1609 }
1610 /*
1611 * type_param_declarator is needed because of following conflict:
1612 * "typedef int a; f(int (a));" could be parsed as
1613 * "function with argument a of type int", or
1614 * "function with an unnamed (abstract) argument of type function".
1615 * This grammar realizes the second case.
1616 */
1617 | begin_type_declaration_specifiers end_type type_param_declarator {
1618 $$ = declare_parameter($3, false);
1619 }
1620 | begin_type_declmods end_type abstract_declarator {
1621 /* ^^ There is no check for the missing type-specifier. */
1622 $$ = declare_parameter($3, false);
1623 }
1624 | begin_type_declaration_specifiers end_type abstract_declarator {
1625 $$ = declare_parameter($3, false);
1626 }
1627 ;
1628
1629 braced_initializer:
1630 /* K&R ---, C90 ---, C99 ---, C11 ---, C23 6.7.10 */
1631 init_lbrace init_rbrace {
1632 /* empty initializer braces require C23 or later */
1633 c23ism(353);
1634 }
1635 /* K&R ---, C90 ---, C99 6.7.8, C11 6.7.9, C23 6.7.10 */
1636 | init_lbrace initializer_list init_rbrace
1637 | init_lbrace initializer_list T_COMMA init_rbrace
1638 ;
1639
1640 initializer: /* C99 6.7.8 "Initialization" */
1641 assignment_expression {
1642 init_expr($1);
1643 }
1644 | init_lbrace init_rbrace {
1645 /* XXX: Empty braces are not covered by C99 6.7.8. */
1646 }
1647 | init_lbrace initializer_list init_rbrace
1648 | init_lbrace initializer_list T_COMMA init_rbrace
1649 /* XXX: What is this error handling for? */
1650 | error
1651 ;
1652
1653 initializer_list: /* C99 6.7.8 "Initialization" */
1654 initializer
1655 | designation initializer
1656 | initializer_list T_COMMA initializer
1657 | initializer_list T_COMMA designation initializer
1658 ;
1659
1660 designation: /* C99 6.7.8 "Initialization" */
1661 {
1662 begin_designation();
1663 } designator_list T_ASSIGN
1664 | identifier T_COLON {
1665 /* GCC style struct or union member name in initializer */
1666 gnuism(315);
1667 begin_designation();
1668 add_designator_member($1);
1669 }
1670 ;
1671
1672 designator_list: /* C99 6.7.8 "Initialization" */
1673 designator
1674 | designator_list designator
1675 ;
1676
1677 designator: /* C99 6.7.8 "Initialization" */
1678 T_LBRACK range T_RBRACK {
1679 if (!allow_c99)
1680 /* array initializer with designators is a C99 ... */
1681 warning(321);
1682 add_designator_subscript($2);
1683 }
1684 | T_POINT identifier {
1685 if (!allow_c99)
1686 /* struct or union member name in initializer is ... */
1687 warning(313);
1688 add_designator_member($2);
1689 }
1690 ;
1691
1692 static_assert_declaration:
1693 T_STATIC_ASSERT T_LPAREN constant_expression T_COMMA T_STRING
1694 T_RPAREN T_SEMI {
1695 /* '_Static_assert' requires C11 or later */
1696 c11ism(354);
1697 }
1698 | T_STATIC_ASSERT T_LPAREN constant_expression T_RPAREN T_SEMI {
1699 /* '_Static_assert' without message requires C23 or later */
1700 c23ism(355);
1701 }
1702 ;
1703
1704 range:
1705 constant_expression {
1706 $$.lo = to_int_constant($1, true);
1707 $$.hi = $$.lo;
1708 }
1709 | constant_expression T_ELLIPSIS constant_expression {
1710 $$.lo = to_int_constant($1, true);
1711 $$.hi = to_int_constant($3, true);
1712 /* initialization with '[a...b]' is a GCC extension */
1713 gnuism(340);
1714 }
1715 ;
1716
1717 init_lbrace: /* helper */
1718 T_LBRACE {
1719 init_lbrace();
1720 }
1721 ;
1722
1723 init_rbrace: /* helper */
1724 T_RBRACE {
1725 init_rbrace();
1726 }
1727 ;
1728
1729 asm_or_symbolrename_opt: /* GCC extensions */
1730 /* empty */ {
1731 $$ = NULL;
1732 }
1733 | T_ASM T_LPAREN T_STRING T_RPAREN gcc_attribute_specifier_list_opt {
1734 freeyyv(&$3, T_STRING);
1735 $$ = NULL;
1736 }
1737 | T_SYMBOLRENAME T_LPAREN T_NAME T_RPAREN
1738 gcc_attribute_specifier_list_opt {
1739 $$ = $3;
1740 }
1741 ;
1742
1743 /* K&R ???, C90 ???, C99 6.8, C11 ???, C23 6.8 */
1744 statement:
1745 expression_statement
1746 | non_expr_statement
1747 ;
1748
1749 /* Helper to avoid shift/reduce conflict in 'label: __attribute__ ;'. */
1750 no_attr_statement:
1751 expression_statement
1752 | no_attr_non_expr_statement
1753 ;
1754
1755 non_expr_statement: /* helper for C99 6.8 */
1756 gcc_attribute_specifier /* ((__fallthrough__)) */ T_SEMI
1757 | no_attr_non_expr_statement
1758 ;
1759
1760 /* Helper to avoid shift/reduce conflict in 'label: __attribute__ ;'. */
1761 no_attr_non_expr_statement:
1762 labeled_statement
1763 | compound_statement
1764 | selection_statement
1765 | iteration_statement
1766 | jump_statement {
1767 suppress_fallthrough = false;
1768 }
1769 | asm_statement
1770 ;
1771
1772 labeled_statement: /* C99 6.8.1 */
1773 label gcc_attribute_specifier_list_opt no_attr_statement
1774 ;
1775
1776 label:
1777 T_NAME T_COLON {
1778 set_sym_kind(SK_LABEL);
1779 named_label(getsym($1));
1780 }
1781 | T_CASE constant_expression T_COLON {
1782 case_label($2);
1783 suppress_fallthrough = true;
1784 }
1785 | T_CASE constant_expression T_ELLIPSIS constant_expression T_COLON {
1786 /* XXX: We don't fill all cases */
1787 case_label($2);
1788 suppress_fallthrough = true;
1789 }
1790 | T_DEFAULT T_COLON {
1791 default_label();
1792 suppress_fallthrough = true;
1793 }
1794 ;
1795
1796 compound_statement: /* C99 6.8.2 */
1797 compound_statement_lbrace compound_statement_rbrace
1798 | compound_statement_lbrace block_item_list compound_statement_rbrace
1799 ;
1800
1801 compound_statement_lbrace:
1802 T_LBRACE {
1803 block_level++;
1804 mem_block_level++;
1805 debug_step("%s: mem_block_level = %zu",
1806 "compound_statement_lbrace", mem_block_level);
1807 begin_declaration_level(DLK_AUTO);
1808 }
1809 ;
1810
1811 compound_statement_rbrace:
1812 T_RBRACE {
1813 end_declaration_level();
1814 if (!in_statement_expr())
1815 level_free_all(mem_block_level); /* leak */
1816 mem_block_level--;
1817 debug_step("%s: mem_block_level = %zu",
1818 "compound_statement_rbrace", mem_block_level);
1819 block_level--;
1820 suppress_fallthrough = false;
1821 }
1822 ;
1823
1824 block_item_list: /* C99 6.8.2 */
1825 block_item
1826 | block_item_list block_item {
1827 if ($1 && !$2)
1828 /* declarations after statements is a C99 feature */
1829 c99ism(327);
1830 $$ = $1 || $2;
1831 }
1832 ;
1833
1834 block_item: /* C99 6.8.2 */
1835 declaration_or_error {
1836 $$ = false;
1837 restore_warning_flags();
1838 }
1839 | statement {
1840 $$ = true;
1841 restore_warning_flags();
1842 }
1843 ;
1844
1845 expression_statement: /* C99 6.8.3 */
1846 expression T_SEMI {
1847 expr($1, false, false, false, false);
1848 suppress_fallthrough = false;
1849 }
1850 | T_SEMI {
1851 check_statement_reachable();
1852 suppress_fallthrough = false;
1853 }
1854 ;
1855
1856 selection_statement: /* C99 6.8.4 */
1857 if_without_else %prec T_THEN {
1858 save_warning_flags();
1859 stmt_if_then_stmt();
1860 stmt_if_else_stmt(false);
1861 }
1862 | if_without_else T_ELSE {
1863 save_warning_flags();
1864 stmt_if_then_stmt();
1865 } statement {
1866 clear_warning_flags();
1867 stmt_if_else_stmt(true);
1868 }
1869 | if_without_else T_ELSE error {
1870 clear_warning_flags();
1871 stmt_if_else_stmt(false);
1872 }
1873 | switch_expr statement {
1874 clear_warning_flags();
1875 stmt_switch_expr_stmt();
1876 }
1877 | switch_expr error {
1878 clear_warning_flags();
1879 stmt_switch_expr_stmt();
1880 }
1881 ;
1882
1883 if_without_else: /* see C99 6.8.4 */
1884 if_expr statement
1885 | if_expr error
1886 ;
1887
1888 if_expr: /* see C99 6.8.4 */
1889 T_IF T_LPAREN expression T_RPAREN {
1890 stmt_if_expr($3);
1891 clear_warning_flags();
1892 }
1893 ;
1894
1895 switch_expr: /* see C99 6.8.4 */
1896 T_SWITCH T_LPAREN expression T_RPAREN {
1897 stmt_switch_expr($3);
1898 clear_warning_flags();
1899 }
1900 ;
1901
1902 iteration_statement: /* C99 6.8.5 */
1903 while_expr statement {
1904 clear_warning_flags();
1905 stmt_while_expr_stmt();
1906 }
1907 | while_expr error {
1908 clear_warning_flags();
1909 stmt_while_expr_stmt();
1910 }
1911 | do_statement T_WHILE T_LPAREN expression T_RPAREN T_SEMI {
1912 stmt_do_while_expr($4);
1913 suppress_fallthrough = false;
1914 }
1915 | do error {
1916 clear_warning_flags();
1917 stmt_do_while_expr(NULL);
1918 }
1919 | for_exprs statement {
1920 clear_warning_flags();
1921 stmt_for_exprs_stmt();
1922 end_declaration_level();
1923 block_level--;
1924 }
1925 | for_exprs error {
1926 clear_warning_flags();
1927 stmt_for_exprs_stmt();
1928 end_declaration_level();
1929 block_level--;
1930 }
1931 ;
1932
1933 while_expr: /* see C99 6.8.5 */
1934 T_WHILE T_LPAREN expression T_RPAREN {
1935 stmt_while_expr($3);
1936 clear_warning_flags();
1937 }
1938 ;
1939
1940 do_statement: /* see C99 6.8.5 */
1941 do statement {
1942 clear_warning_flags();
1943 }
1944 ;
1945
1946 do: /* see C99 6.8.5 */
1947 T_DO {
1948 stmt_do();
1949 }
1950 ;
1951
1952 for_start: /* see C99 6.8.5 */
1953 T_FOR T_LPAREN {
1954 begin_declaration_level(DLK_AUTO);
1955 block_level++;
1956 }
1957 ;
1958
1959 for_exprs: /* see C99 6.8.5 */
1960 for_start
1961 begin_type_declaration_specifiers end_type
1962 notype_init_declarators T_SEMI
1963 expression_opt T_SEMI
1964 expression_opt T_RPAREN {
1965 /* variable declaration in for loop */
1966 c99ism(325);
1967 stmt_for_exprs(NULL, $6, $8);
1968 clear_warning_flags();
1969 }
1970 | for_start
1971 expression_opt T_SEMI
1972 expression_opt T_SEMI
1973 expression_opt T_RPAREN {
1974 stmt_for_exprs($2, $4, $6);
1975 clear_warning_flags();
1976 }
1977 ;
1978
1979 jump_statement: /* C99 6.8.6 */
1980 goto identifier T_SEMI {
1981 stmt_goto(getsym($2));
1982 }
1983 | goto error T_SEMI {
1984 set_sym_kind(SK_VCFT);
1985 }
1986 | T_CONTINUE T_SEMI {
1987 stmt_continue();
1988 }
1989 | T_BREAK T_SEMI {
1990 stmt_break();
1991 }
1992 | T_RETURN sys T_SEMI {
1993 stmt_return($2, NULL);
1994 }
1995 | T_RETURN sys expression T_SEMI {
1996 stmt_return($2, $3);
1997 }
1998 ;
1999
2000 goto: /* see C99 6.8.6 */
2001 T_GOTO {
2002 set_sym_kind(SK_LABEL);
2003 }
2004 ;
2005
2006 asm_statement: /* GCC extension */
2007 T_ASM T_LPAREN read_until_rparen T_SEMI {
2008 dcs_set_asm();
2009 }
2010 | T_ASM type_qualifier T_LPAREN read_until_rparen T_SEMI {
2011 dcs_set_asm();
2012 }
2013 | T_ASM error
2014 ;
2015
2016 read_until_rparen: /* helper for 'asm_statement' */
2017 /* empty */ {
2018 read_until_rparen();
2019 }
2020 ;
2021
2022 translation_unit: /* C99 6.9 */
2023 external_declaration
2024 | translation_unit external_declaration
2025 ;
2026
2027 external_declaration: /* C99 6.9 */
2028 function_definition {
2029 global_clean_up_decl(false);
2030 clear_warning_flags();
2031 }
2032 | top_level_declaration {
2033 global_clean_up_decl(false);
2034 clear_warning_flags();
2035 }
2036 | asm_statement /* GCC extension */
2037 | T_SEMI { /* GCC extension */
2038 /*
2039 * TODO: Only allow this in GCC mode, not in plain C99.
2040 * This is one of the top 10 warnings in the NetBSD build.
2041 */
2042 if (!allow_trad && !allow_c99) {
2043 /* empty declaration */
2044 error(0);
2045 } else if (allow_c90) {
2046 /* empty declaration */
2047 warning(0);
2048 }
2049 }
2050 ;
2051
2052 /*
2053 * On the top level, lint allows several forms of declarations that it doesn't
2054 * allow in functions. For example, a single ';' is an empty declaration and
2055 * is supported by some compilers, but in a function it would be an empty
2056 * statement, not a declaration. This makes a difference in C90 mode, where
2057 * a statement must not be followed by a declaration.
2058 *
2059 * See 'declaration' for all other declarations.
2060 */
2061 top_level_declaration: /* C99 6.9 calls this 'declaration' */
2062 begin_type end_type notype_init_declarators T_SEMI {
2063 /* TODO: Make this an error in C99 mode as well. */
2064 if (!allow_trad && !allow_c99) {
2065 /* old-style declaration; add 'int' */
2066 error(1);
2067 } else if (allow_c90) {
2068 /* old-style declaration; add 'int' */
2069 warning(1);
2070 }
2071 }
2072 | declaration
2073 | error T_SEMI {
2074 global_clean_up();
2075 }
2076 | error T_RBRACE {
2077 global_clean_up();
2078 }
2079 ;
2080
2081 function_definition: /* C99 6.9.1 */
2082 func_declarator {
2083 if ($1->s_type->t_tspec != FUNC) {
2084 /* syntax error '%s' */
2085 error(249, yytext);
2086 YYERROR;
2087 }
2088 if ($1->s_type->t_typedef) {
2089 /* ()-less function definition */
2090 error(64);
2091 YYERROR;
2092 }
2093 check_extern_declaration($1);
2094 begin_function($1);
2095 block_level++;
2096 begin_declaration_level(DLK_OLD_STYLE_PARAMS);
2097 if (lwarn == LWARN_NONE)
2098 $1->s_used = true;
2099 } arg_declaration_list_opt {
2100 end_declaration_level();
2101 block_level--;
2102 check_func_lint_directives();
2103 check_func_old_style_parameters();
2104 begin_control_statement(CS_FUNCTION_BODY);
2105 } compound_statement {
2106 end_function();
2107 end_control_statement(CS_FUNCTION_BODY);
2108 }
2109 ;
2110
2111 func_declarator:
2112 begin_type end_type notype_declarator {
2113 if (!allow_trad) {
2114 /* old-style declaration; add 'int' */
2115 error(1);
2116 }
2117 $$ = $3;
2118 }
2119 | begin_type_declmods end_type notype_declarator {
2120 if (!allow_trad) {
2121 /* old-style declaration; add 'int' */
2122 error(1);
2123 }
2124 $$ = $3;
2125 }
2126 | begin_type_declaration_specifiers end_type type_declarator {
2127 $$ = $3;
2128 }
2129 ;
2130
2131 arg_declaration_list_opt: /* C99 6.9.1p13 example 1 */
2132 /* empty */
2133 | arg_declaration_list
2134 ;
2135
2136 arg_declaration_list: /* C99 6.9.1p13 example 1 */
2137 arg_declaration
2138 | arg_declaration_list arg_declaration
2139 /* XXX or better "arg_declaration error" ? */
2140 | error
2141 ;
2142
2143 /*
2144 * "arg_declaration" is separated from "declaration" because it
2145 * needs other error handling.
2146 */
2147 arg_declaration:
2148 begin_type_declmods end_type T_SEMI {
2149 /* empty declaration */
2150 warning(2);
2151 }
2152 | begin_type_declmods end_type notype_init_declarators T_SEMI
2153 | begin_type_declaration_specifiers end_type T_SEMI {
2154 if (!dcs->d_nonempty_decl) {
2155 /* empty declaration */
2156 warning(2);
2157 } else {
2158 /* '%s' declared in parameter declaration list */
2159 warning(3, type_name(dcs->d_type));
2160 }
2161 }
2162 | begin_type_declaration_specifiers end_type
2163 type_init_declarators T_SEMI {
2164 if (dcs->d_nonempty_decl) {
2165 /* '%s' declared in parameter declaration list */
2166 warning(3, type_name(dcs->d_type));
2167 }
2168 }
2169 | begin_type_declmods error
2170 | begin_type_declaration_specifiers error
2171 ;
2172
2173 /* https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html */
2174 gcc_attribute_specifier_list_opt:
2175 /* empty */
2176 | gcc_attribute_specifier_list
2177 ;
2178
2179 gcc_attribute_specifier_list:
2180 gcc_attribute_specifier
2181 | gcc_attribute_specifier_list gcc_attribute_specifier
2182 ;
2183
2184 gcc_attribute_specifier:
2185 T_ATTRIBUTE T_LPAREN T_LPAREN {
2186 in_gcc_attribute = true;
2187 } gcc_attribute_list {
2188 in_gcc_attribute = false;
2189 } T_RPAREN T_RPAREN
2190 ;
2191
2192 gcc_attribute_list:
2193 gcc_attribute
2194 | gcc_attribute_list T_COMMA gcc_attribute
2195 ;
2196
2197 gcc_attribute:
2198 /* empty */
2199 | T_NAME {
2200 const char *name = $1->sb_name;
2201 if (is_either(name, "packed", "__packed__"))
2202 dcs_add_packed();
2203 else if (is_either(name, "used", "__used__") ||
2204 is_either(name, "unused", "__unused__"))
2205 dcs_set_used();
2206 else if (is_either(name, "fallthrough", "__fallthrough__"))
2207 suppress_fallthrough = true;
2208 }
2209 | T_NAME T_LPAREN T_RPAREN
2210 | T_NAME T_LPAREN gcc_attribute_parameters T_RPAREN
2211 | type_qualifier {
2212 if (!$1.tq_const)
2213 yyerror("Bad attribute");
2214 }
2215 ;
2216
2217 gcc_attribute_parameters:
2218 constant_expression
2219 | gcc_attribute_parameters T_COMMA constant_expression
2220 ;
2221
2222 sys:
2223 /* empty */ {
2224 $$ = in_system_header;
2225 }
2226 ;
2227
2228 %%
2229
2230 /* ARGSUSED */
2231 int
2232 yyerror(const char *msg)
2233 {
2234 /* syntax error '%s' */
2235 error(249, yytext);
2236 if (++sytxerr >= 5)
2237 norecover();
2238 return 0;
2239 }
2240
2241 #if YYDEBUG && YYBYACC
2242 static const char *
2243 cgram_to_string(int token, YYSTYPE val)
2244 {
2245
2246 switch (token) {
2247 case T_INCDEC:
2248 return val.y_inc ? "++" : "--";
2249 case T_MULTIPLICATIVE:
2250 case T_ADDITIVE:
2251 case T_SHIFT:
2252 case T_RELATIONAL:
2253 case T_EQUALITY:
2254 case T_OPASSIGN:
2255 return op_name(val.y_op);
2256 case T_SCLASS:
2257 return scl_name(val.y_scl);
2258 case T_TYPE:
2259 case T_STRUCT_OR_UNION:
2260 return tspec_name(val.y_tspec);
2261 case T_QUAL:
2262 return type_qualifiers_string(val.y_type_qualifiers);
2263 case T_FUNCTION_SPECIFIER:
2264 return function_specifier_name(val.y_function_specifier);
2265 case T_NAME:
2266 return val.y_name->sb_name;
2267 default:
2268 return "<none>";
2269 }
2270 }
2271 #endif
2272
2273 static void
2274 cgram_declare(sym_t *decl, bool has_initializer, sbuf_t *renaming)
2275 {
2276 declare(decl, has_initializer, renaming);
2277 if (renaming != NULL)
2278 freeyyv(&renaming, T_NAME);
2279 }
2280
2281 /*
2282 * Discard all input tokens up to and including the next unmatched right
2283 * parenthesis.
2284 */
2285 static void
2286 read_until_rparen(void)
2287 {
2288 int level;
2289
2290 if (yychar < 0)
2291 yychar = yylex();
2292 freeyyv(&yylval, yychar);
2293
2294 level = 1;
2295 while (yychar > 0) {
2296 if (yychar == T_LPAREN)
2297 level++;
2298 if (yychar == T_RPAREN && --level == 0)
2299 break;
2300 freeyyv(&yylval, yychar = yylex());
2301 }
2302
2303 yyclearin;
2304 }
2305
2306 static sym_t *
2307 symbolrename(sym_t *s, sbuf_t *sb)
2308 {
2309 if (sb != NULL)
2310 s->s_rename = sb->sb_name;
2311 return s;
2312 }
2313