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