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