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