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