cgram.y revision 1.446 1 %{
2 /* $NetBSD: cgram.y,v 1.446 2023/07/02 23:40:23 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.446 2023/07/02 23:40:23 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 } braced_initializer {
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 braced_initializer:
1523 /* K&R ---, C90 ---, C99 ---, C11 ---, C23 6.7.10 */
1524 init_lbrace init_rbrace {
1525 /* empty initializer braces require C23 or later */
1526 c23ism(353);
1527 }
1528 /* K&R ---, C90 ---, C99 6.7.8, C11 6.7.9, C23 6.7.10 */
1529 | init_lbrace initializer_list comma_opt init_rbrace
1530 ;
1531
1532 initializer: /* C99 6.7.8 "Initialization" */
1533 assignment_expression {
1534 init_expr($1);
1535 }
1536 | init_lbrace init_rbrace {
1537 /* XXX: Empty braces are not covered by C99 6.7.8. */
1538 }
1539 | init_lbrace initializer_list comma_opt init_rbrace
1540 /* XXX: What is this error handling for? */
1541 | error
1542 ;
1543
1544 initializer_list: /* C99 6.7.8 "Initialization" */
1545 initializer_list_item
1546 | initializer_list T_COMMA initializer_list_item
1547 ;
1548
1549 initializer_list_item: /* helper */
1550 designation initializer
1551 | initializer
1552 ;
1553
1554 designation: /* C99 6.7.8 "Initialization" */
1555 begin_designation designator_list T_ASSIGN
1556 | identifier T_COLON {
1557 /* GCC style struct or union member name in initializer */
1558 gnuism(315);
1559 begin_designation();
1560 add_designator_member($1);
1561 }
1562 ;
1563
1564 begin_designation: /* lint-specific helper */
1565 /* empty */ {
1566 begin_designation();
1567 }
1568 ;
1569
1570 designator_list: /* C99 6.7.8 "Initialization" */
1571 designator
1572 | designator_list designator
1573 ;
1574
1575 designator: /* C99 6.7.8 "Initialization" */
1576 T_LBRACK range T_RBRACK {
1577 if (!allow_c99)
1578 /* array initializer with designators is a C99 ... */
1579 warning(321);
1580 add_designator_subscript($2);
1581 }
1582 | T_POINT identifier {
1583 if (!allow_c99)
1584 /* struct or union member name in initializer is ... */
1585 warning(313);
1586 add_designator_member($2);
1587 }
1588 ;
1589
1590 static_assert_declaration:
1591 T_STATIC_ASSERT T_LPAREN constant_expr T_COMMA T_STRING T_RPAREN T_SEMI /* C11 */
1592 | T_STATIC_ASSERT T_LPAREN constant_expr T_RPAREN T_SEMI /* C23 */
1593 ;
1594
1595 range:
1596 constant_expr {
1597 $$.lo = to_int_constant($1, true);
1598 $$.hi = $$.lo;
1599 }
1600 | constant_expr T_ELLIPSIS constant_expr {
1601 $$.lo = to_int_constant($1, true);
1602 $$.hi = to_int_constant($3, true);
1603 /* initialization with '[a...b]' is a GCC extension */
1604 gnuism(340);
1605 }
1606 ;
1607
1608 init_lbrace: /* helper */
1609 T_LBRACE {
1610 init_lbrace();
1611 }
1612 ;
1613
1614 init_rbrace: /* helper */
1615 T_RBRACE {
1616 init_rbrace();
1617 }
1618 ;
1619
1620 asm_or_symbolrename_opt: /* GCC extensions */
1621 /* empty */ {
1622 $$ = NULL;
1623 }
1624 | T_ASM T_LPAREN T_STRING T_RPAREN gcc_attribute_specifier_list_opt {
1625 freeyyv(&$3, T_STRING);
1626 $$ = NULL;
1627 }
1628 | T_SYMBOLRENAME T_LPAREN T_NAME T_RPAREN
1629 gcc_attribute_specifier_list_opt {
1630 $$ = $3;
1631 }
1632 ;
1633
1634 statement: /* C99 6.8 */
1635 expression_statement
1636 | non_expr_statement
1637 ;
1638
1639 non_expr_statement: /* helper for C99 6.8 */
1640 gcc_attribute_specifier /* ((__fallthrough__)) */ T_SEMI
1641 | labeled_statement
1642 | compound_statement
1643 | selection_statement
1644 | iteration_statement
1645 | jump_statement {
1646 seen_fallthrough = false;
1647 }
1648 | asm_statement
1649 ;
1650
1651 labeled_statement: /* C99 6.8.1 */
1652 label gcc_attribute_specifier_list_opt statement
1653 ;
1654
1655 label:
1656 T_NAME T_COLON {
1657 symtyp = FLABEL;
1658 named_label(getsym($1));
1659 }
1660 | T_CASE constant_expr T_COLON {
1661 case_label($2);
1662 seen_fallthrough = true;
1663 }
1664 | T_CASE constant_expr T_ELLIPSIS constant_expr T_COLON {
1665 /* XXX: We don't fill all cases */
1666 case_label($2);
1667 seen_fallthrough = true;
1668 }
1669 | T_DEFAULT T_COLON {
1670 default_label();
1671 seen_fallthrough = true;
1672 }
1673 ;
1674
1675 compound_statement: /* C99 6.8.2 */
1676 compound_statement_lbrace compound_statement_rbrace
1677 | compound_statement_lbrace block_item_list compound_statement_rbrace
1678 ;
1679
1680 compound_statement_lbrace:
1681 T_LBRACE {
1682 block_level++;
1683 mem_block_level++;
1684 begin_declaration_level(DLK_AUTO);
1685 }
1686 ;
1687
1688 compound_statement_rbrace:
1689 T_RBRACE {
1690 end_declaration_level();
1691 level_free_all(mem_block_level);
1692 mem_block_level--;
1693 block_level--;
1694 seen_fallthrough = false;
1695 }
1696 ;
1697
1698 block_item_list: /* C99 6.8.2 */
1699 block_item
1700 | block_item_list block_item {
1701 if ($1 && !$2)
1702 /* declarations after statements is a C99 feature */
1703 c99ism(327);
1704 $$ = $1 || $2;
1705 }
1706 ;
1707
1708 block_item: /* C99 6.8.2 */
1709 declaration_or_error {
1710 $$ = false;
1711 restore_warning_flags();
1712 }
1713 | statement {
1714 $$ = true;
1715 restore_warning_flags();
1716 }
1717 ;
1718
1719 expression_statement: /* C99 6.8.3 */
1720 expression T_SEMI {
1721 expr($1, false, false, false, false);
1722 seen_fallthrough = false;
1723 }
1724 | T_SEMI {
1725 check_statement_reachable();
1726 seen_fallthrough = false;
1727 }
1728 ;
1729
1730 selection_statement: /* C99 6.8.4 */
1731 if_without_else %prec T_THEN {
1732 save_warning_flags();
1733 stmt_if_then_stmt();
1734 stmt_if_else_stmt(false);
1735 }
1736 | if_without_else T_ELSE {
1737 save_warning_flags();
1738 stmt_if_then_stmt();
1739 } statement {
1740 clear_warning_flags();
1741 stmt_if_else_stmt(true);
1742 }
1743 | if_without_else T_ELSE error {
1744 clear_warning_flags();
1745 stmt_if_else_stmt(false);
1746 }
1747 | switch_expr statement {
1748 clear_warning_flags();
1749 stmt_switch_expr_stmt();
1750 }
1751 | switch_expr error {
1752 clear_warning_flags();
1753 stmt_switch_expr_stmt();
1754 }
1755 ;
1756
1757 if_without_else: /* see C99 6.8.4 */
1758 if_expr statement
1759 | if_expr error
1760 ;
1761
1762 if_expr: /* see C99 6.8.4 */
1763 T_IF T_LPAREN expression T_RPAREN {
1764 stmt_if_expr($3);
1765 clear_warning_flags();
1766 }
1767 ;
1768
1769 switch_expr: /* see C99 6.8.4 */
1770 T_SWITCH T_LPAREN expression T_RPAREN {
1771 stmt_switch_expr($3);
1772 clear_warning_flags();
1773 }
1774 ;
1775
1776 iteration_statement: /* C99 6.8.5 */
1777 while_expr statement {
1778 clear_warning_flags();
1779 stmt_while_expr_stmt();
1780 }
1781 | while_expr error {
1782 clear_warning_flags();
1783 stmt_while_expr_stmt();
1784 }
1785 | do_statement do_while_expr {
1786 stmt_do_while_expr($2);
1787 seen_fallthrough = false;
1788 }
1789 | do error {
1790 clear_warning_flags();
1791 stmt_do_while_expr(NULL);
1792 }
1793 | for_exprs statement {
1794 clear_warning_flags();
1795 stmt_for_exprs_stmt();
1796 end_declaration_level();
1797 block_level--;
1798 }
1799 | for_exprs error {
1800 clear_warning_flags();
1801 stmt_for_exprs_stmt();
1802 end_declaration_level();
1803 block_level--;
1804 }
1805 ;
1806
1807 while_expr: /* see C99 6.8.5 */
1808 T_WHILE T_LPAREN expression T_RPAREN {
1809 stmt_while_expr($3);
1810 clear_warning_flags();
1811 }
1812 ;
1813
1814 do_statement: /* see C99 6.8.5 */
1815 do statement {
1816 clear_warning_flags();
1817 }
1818 ;
1819
1820 do: /* see C99 6.8.5 */
1821 T_DO {
1822 stmt_do();
1823 }
1824 ;
1825
1826 do_while_expr: /* see C99 6.8.5 */
1827 T_WHILE T_LPAREN expression T_RPAREN T_SEMI {
1828 $$ = $3;
1829 }
1830 ;
1831
1832 for_start: /* see C99 6.8.5 */
1833 T_FOR T_LPAREN {
1834 begin_declaration_level(DLK_AUTO);
1835 block_level++;
1836 }
1837 ;
1838
1839 for_exprs: /* see C99 6.8.5 */
1840 for_start
1841 begin_type_declaration_specifiers end_type
1842 notype_init_declarators T_SEMI
1843 expression_opt T_SEMI
1844 expression_opt T_RPAREN {
1845 /* variable declaration in for loop */
1846 c99ism(325);
1847 stmt_for_exprs(NULL, $6, $8);
1848 clear_warning_flags();
1849 }
1850 | for_start
1851 expression_opt T_SEMI
1852 expression_opt T_SEMI
1853 expression_opt T_RPAREN {
1854 stmt_for_exprs($2, $4, $6);
1855 clear_warning_flags();
1856 }
1857 ;
1858
1859 jump_statement: /* C99 6.8.6 */
1860 goto identifier T_SEMI {
1861 stmt_goto(getsym($2));
1862 }
1863 | goto error T_SEMI {
1864 symtyp = FVFT;
1865 }
1866 | T_CONTINUE T_SEMI {
1867 stmt_continue();
1868 }
1869 | T_BREAK T_SEMI {
1870 stmt_break();
1871 }
1872 | T_RETURN sys T_SEMI {
1873 stmt_return($2, NULL);
1874 }
1875 | T_RETURN sys expression T_SEMI {
1876 stmt_return($2, $3);
1877 }
1878 ;
1879
1880 goto: /* see C99 6.8.6 */
1881 T_GOTO {
1882 symtyp = FLABEL;
1883 }
1884 ;
1885
1886 asm_statement: /* GCC extension */
1887 T_ASM T_LPAREN read_until_rparen T_SEMI {
1888 dcs_set_asm();
1889 }
1890 | T_ASM type_qualifier T_LPAREN read_until_rparen T_SEMI {
1891 dcs_set_asm();
1892 }
1893 | T_ASM error
1894 ;
1895
1896 read_until_rparen: /* helper for 'asm_statement' */
1897 /* empty */ {
1898 read_until_rparen();
1899 }
1900 ;
1901
1902 translation_unit: /* C99 6.9 */
1903 external_declaration
1904 | translation_unit external_declaration
1905 ;
1906
1907 external_declaration: /* C99 6.9 */
1908 function_definition {
1909 global_clean_up_decl(false);
1910 clear_warning_flags();
1911 }
1912 | top_level_declaration {
1913 global_clean_up_decl(false);
1914 clear_warning_flags();
1915 }
1916 | asm_statement /* GCC extension */
1917 | T_SEMI { /* GCC extension */
1918 /*
1919 * TODO: Only allow this in GCC mode, not in plain C99.
1920 * This is one of the top 10 warnings in the NetBSD build.
1921 */
1922 if (!allow_trad && !allow_c99) {
1923 /* empty declaration */
1924 error(0);
1925 } else if (allow_c90) {
1926 /* empty declaration */
1927 warning(0);
1928 }
1929 }
1930 ;
1931
1932 /*
1933 * On the top level, lint allows several forms of declarations that it doesn't
1934 * allow in functions. For example, a single ';' is an empty declaration and
1935 * is supported by some compilers, but in a function it would be an empty
1936 * statement, not a declaration. This makes a difference in C90 mode, where
1937 * a statement must not be followed by a declaration.
1938 *
1939 * See 'declaration' for all other declarations.
1940 */
1941 top_level_declaration: /* C99 6.9 calls this 'declaration' */
1942 begin_type end_type notype_init_declarators T_SEMI {
1943 /* TODO: Make this an error in C99 mode as well. */
1944 if (!allow_trad && !allow_c99) {
1945 /* old-style declaration; add 'int' */
1946 error(1);
1947 } else if (allow_c90) {
1948 /* old-style declaration; add 'int' */
1949 warning(1);
1950 }
1951 }
1952 | declaration
1953 | error T_SEMI {
1954 global_clean_up();
1955 }
1956 | error T_RBRACE {
1957 global_clean_up();
1958 }
1959 ;
1960
1961 function_definition: /* C99 6.9.1 */
1962 func_declarator {
1963 if ($1->s_type->t_tspec != FUNC) {
1964 /* syntax error '%s' */
1965 error(249, yytext);
1966 YYERROR;
1967 }
1968 if ($1->s_type->t_typedef) {
1969 /* ()-less function definition */
1970 error(64);
1971 YYERROR;
1972 }
1973 begin_function($1);
1974 block_level++;
1975 begin_declaration_level(DLK_OLD_STYLE_ARGS);
1976 if (lwarn == LWARN_NONE)
1977 $1->s_used = true;
1978 } arg_declaration_list_opt {
1979 end_declaration_level();
1980 block_level--;
1981 check_func_lint_directives();
1982 check_func_old_style_arguments();
1983 begin_control_statement(CS_FUNCTION_BODY);
1984 } compound_statement {
1985 end_function();
1986 end_control_statement(CS_FUNCTION_BODY);
1987 }
1988 ;
1989
1990 func_declarator:
1991 begin_type end_type notype_declarator {
1992 if (!allow_trad) {
1993 /* old-style declaration; add 'int' */
1994 error(1);
1995 }
1996 $$ = $3;
1997 }
1998 | begin_type_declmods end_type notype_declarator {
1999 if (!allow_trad) {
2000 /* old-style declaration; add 'int' */
2001 error(1);
2002 }
2003 $$ = $3;
2004 }
2005 | begin_type_declaration_specifiers end_type type_declarator {
2006 $$ = $3;
2007 }
2008 ;
2009
2010 arg_declaration_list_opt: /* C99 6.9.1p13 example 1 */
2011 /* empty */
2012 | arg_declaration_list
2013 ;
2014
2015 arg_declaration_list: /* C99 6.9.1p13 example 1 */
2016 arg_declaration
2017 | arg_declaration_list arg_declaration
2018 /* XXX or better "arg_declaration error" ? */
2019 | error
2020 ;
2021
2022 /*
2023 * "arg_declaration" is separated from "declaration" because it
2024 * needs other error handling.
2025 */
2026 arg_declaration:
2027 begin_type_declmods end_type T_SEMI {
2028 /* empty declaration */
2029 warning(2);
2030 }
2031 | begin_type_declmods end_type notype_init_declarators T_SEMI
2032 | begin_type_declaration_specifiers end_type T_SEMI {
2033 if (!dcs->d_nonempty_decl) {
2034 /* empty declaration */
2035 warning(2);
2036 } else {
2037 /* '%s' declared in argument declaration list */
2038 warning(3, type_name(dcs->d_type));
2039 }
2040 }
2041 | begin_type_declaration_specifiers end_type
2042 type_init_declarators T_SEMI {
2043 if (dcs->d_nonempty_decl) {
2044 /* '%s' declared in argument declaration list */
2045 warning(3, type_name(dcs->d_type));
2046 }
2047 }
2048 | begin_type_declmods error
2049 | begin_type_declaration_specifiers error
2050 ;
2051
2052 /* https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html */
2053 gcc_attribute_specifier_list_opt:
2054 /* empty */
2055 | gcc_attribute_specifier_list
2056 ;
2057
2058 gcc_attribute_specifier_list:
2059 gcc_attribute_specifier
2060 | gcc_attribute_specifier_list gcc_attribute_specifier
2061 ;
2062
2063 gcc_attribute_specifier:
2064 T_ATTRIBUTE T_LPAREN T_LPAREN {
2065 in_gcc_attribute = true;
2066 } gcc_attribute_list {
2067 in_gcc_attribute = false;
2068 } T_RPAREN T_RPAREN
2069 ;
2070
2071 gcc_attribute_list:
2072 gcc_attribute
2073 | gcc_attribute_list T_COMMA gcc_attribute
2074 ;
2075
2076 gcc_attribute:
2077 /* empty */
2078 | T_NAME {
2079 const char *name = $1->sb_name;
2080 if (is_either(name, "packed", "__packed__"))
2081 dcs_add_packed();
2082 else if (is_either(name, "used", "__used__") ||
2083 is_either(name, "unused", "__unused__"))
2084 dcs_set_used();
2085 else if (is_either(name, "fallthrough", "__fallthrough__"))
2086 fallthru(1);
2087 }
2088 | T_NAME T_LPAREN T_RPAREN
2089 | T_NAME T_LPAREN gcc_attribute_parameters T_RPAREN
2090 | type_qualifier {
2091 if ($1 != CONST)
2092 yyerror("Bad attribute");
2093 }
2094 ;
2095
2096 gcc_attribute_parameters:
2097 constant_expr
2098 | gcc_attribute_parameters T_COMMA constant_expr
2099 ;
2100
2101 sys:
2102 /* empty */ {
2103 $$ = in_system_header;
2104 }
2105 ;
2106
2107 %%
2108
2109 /* ARGSUSED */
2110 int
2111 yyerror(const char *msg)
2112 {
2113 /* syntax error '%s' */
2114 error(249, yytext);
2115 if (++sytxerr >= 5)
2116 norecover();
2117 return 0;
2118 }
2119
2120 #if YYDEBUG && (YYBYACC || YYBISON)
2121 static const char *
2122 cgram_to_string(int token, YYSTYPE val)
2123 {
2124
2125 switch (token) {
2126 case T_INCDEC:
2127 case T_MULTIPLICATIVE:
2128 case T_ADDITIVE:
2129 case T_SHIFT:
2130 case T_RELATIONAL:
2131 case T_EQUALITY:
2132 case T_OPASSIGN:
2133 return modtab[val.y_op].m_name;
2134 case T_SCLASS:
2135 return scl_name(val.y_scl);
2136 case T_TYPE:
2137 case T_STRUCT_OR_UNION:
2138 return tspec_name(val.y_tspec);
2139 case T_QUAL:
2140 return tqual_name(val.y_tqual);
2141 case T_NAME:
2142 return val.y_name->sb_name;
2143 default:
2144 return "<none>";
2145 }
2146 }
2147 #endif
2148
2149 #if YYDEBUG && YYBISON
2150 static inline void
2151 cgram_print(FILE *output, int token, YYSTYPE val)
2152 {
2153 (void)fprintf(output, "%s", cgram_to_string(token, val));
2154 }
2155 #endif
2156
2157 static void
2158 cgram_declare(sym_t *decl, bool has_initializer, sbuf_t *renaming)
2159 {
2160 declare(decl, has_initializer, renaming);
2161 if (renaming != NULL)
2162 freeyyv(&renaming, T_NAME);
2163 }
2164
2165 /*
2166 * Discard all input tokens up to and including the next
2167 * unmatched right paren
2168 */
2169 static void
2170 read_until_rparen(void)
2171 {
2172 int level;
2173
2174 if (yychar < 0)
2175 yychar = yylex();
2176 freeyyv(&yylval, yychar);
2177
2178 level = 1;
2179 while (yychar > 0) {
2180 if (yychar == T_LPAREN)
2181 level++;
2182 if (yychar == T_RPAREN && --level == 0)
2183 break;
2184 freeyyv(&yylval, yychar = yylex());
2185 }
2186
2187 yyclearin;
2188 }
2189
2190 static sym_t *
2191 symbolrename(sym_t *s, sbuf_t *sb)
2192 {
2193 if (sb != NULL)
2194 s->s_rename = sb->sb_name;
2195 return s;
2196 }
2197