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