cgram.y revision 1.442 1 %{
2 /* $NetBSD: cgram.y,v 1.442 2023/06/30 21:06:18 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.442 2023/06/30 21:06:18 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
901 ? DK_STRUCT_MEMBER
902 : DK_UNION_MEMBER);
903 dcs->d_offset_in_bits = 0;
904 dcs->d_sou_align_in_bits = CHAR_SIZE;
905 $$ = $1;
906 }
907 | struct_or_union type_attribute
908 ;
909
910 braced_struct_declaration_list: /* see C99 6.7.2.1 */
911 struct_declaration_lbrace struct_declaration_list_with_rbrace {
912 $$ = $2;
913 }
914 ;
915
916 struct_declaration_lbrace: /* see C99 6.7.2.1 */
917 T_LBRACE {
918 symtyp = FVFT;
919 }
920 ;
921
922 struct_declaration_list_with_rbrace: /* see C99 6.7.2.1 */
923 struct_declaration_list T_RBRACE
924 | T_RBRACE {
925 /* XXX: This is not allowed by any C standard. */
926 $$ = NULL;
927 }
928 ;
929
930 struct_declaration_list: /* C99 6.7.2.1 */
931 struct_declaration
932 | struct_declaration_list struct_declaration {
933 $$ = concat_symbols($1, $2);
934 }
935 ;
936
937 struct_declaration: /* C99 6.7.2.1 */
938 begin_type_qualifier_list end_type {
939 /* ^^ There is no check for the missing type-specifier. */
940 /* too late, i know, but getsym() compensates it */
941 symtyp = FMEMBER;
942 } notype_struct_declarators type_attribute_opt T_SEMI {
943 symtyp = FVFT;
944 $$ = $4;
945 }
946 | begin_type_specifier_qualifier_list end_type {
947 symtyp = FMEMBER;
948 } type_struct_declarators type_attribute_opt T_SEMI {
949 symtyp = FVFT;
950 $$ = $4;
951 }
952 | begin_type_qualifier_list end_type type_attribute_opt T_SEMI {
953 /* syntax error '%s' */
954 error(249, "member without type");
955 $$ = NULL;
956 }
957 | begin_type_specifier_qualifier_list end_type type_attribute_opt
958 T_SEMI {
959 symtyp = FVFT;
960 if (!allow_c11 && !allow_gcc)
961 /* anonymous struct/union members is a C11 feature */
962 warning(49);
963 if (is_struct_or_union(dcs->d_type->t_tspec))
964 $$ = declare_unnamed_member();
965 else {
966 /* syntax error '%s' */
967 error(249, "unnamed member");
968 $$ = NULL;
969 }
970 }
971 | static_assert_declaration {
972 $$ = NULL;
973 }
974 | error T_SEMI {
975 symtyp = FVFT;
976 $$ = NULL;
977 }
978 ;
979
980 notype_struct_declarators:
981 notype_struct_declarator {
982 $$ = declare_member($1);
983 }
984 | notype_struct_declarators {
985 symtyp = FMEMBER;
986 } T_COMMA type_struct_declarator {
987 $$ = concat_symbols($1, declare_member($4));
988 }
989 ;
990
991 type_struct_declarators:
992 type_struct_declarator {
993 $$ = declare_member($1);
994 }
995 | type_struct_declarators {
996 symtyp = FMEMBER;
997 } T_COMMA type_struct_declarator {
998 $$ = concat_symbols($1, declare_member($4));
999 }
1000 ;
1001
1002 notype_struct_declarator:
1003 notype_declarator
1004 | notype_declarator T_COLON constant_expr { /* C99 6.7.2.1 */
1005 $$ = set_bit_field_width($1, to_int_constant($3, true));
1006 }
1007 | {
1008 symtyp = FVFT;
1009 } T_COLON constant_expr { /* C99 6.7.2.1 */
1010 $$ = set_bit_field_width(NULL, to_int_constant($3, true));
1011 }
1012 ;
1013
1014 type_struct_declarator:
1015 type_declarator
1016 | type_declarator T_COLON constant_expr {
1017 $$ = set_bit_field_width($1, to_int_constant($3, true));
1018 }
1019 | {
1020 symtyp = FVFT;
1021 } T_COLON constant_expr {
1022 $$ = set_bit_field_width(NULL, to_int_constant($3, true));
1023 }
1024 ;
1025
1026 /* K&R ---, C90 6.5.2.2, C99 6.7.2.2, C11 6.7.2.2 */
1027 enum_specifier: /* C99 6.7.2.2 */
1028 enum gcc_attribute_specifier_list_opt identifier_sym {
1029 $$ = make_tag_type($3, ENUM, false, false);
1030 }
1031 | enum gcc_attribute_specifier_list_opt identifier_sym {
1032 dcs->d_tag_type = make_tag_type($3, ENUM, true, false);
1033 } enum_declaration /*gcc_attribute_specifier_list_opt*/ {
1034 $$ = complete_enum($5);
1035 }
1036 | enum gcc_attribute_specifier_list_opt {
1037 dcs->d_tag_type = make_tag_type(NULL, ENUM, true, false);
1038 } enum_declaration /*gcc_attribute_specifier_list_opt*/ {
1039 $$ = complete_enum($4);
1040 }
1041 | enum error {
1042 symtyp = FVFT;
1043 $$ = gettyp(INT);
1044 }
1045 ;
1046
1047 enum: /* helper for C99 6.7.2.2 */
1048 T_ENUM {
1049 symtyp = FTAG;
1050 begin_declaration_level(DK_ENUM_CONSTANT);
1051 }
1052 ;
1053
1054 enum_declaration: /* helper for C99 6.7.2.2 */
1055 enum_decl_lbrace enums_with_opt_comma T_RBRACE {
1056 $$ = $2;
1057 }
1058 ;
1059
1060 enum_decl_lbrace: /* helper for C99 6.7.2.2 */
1061 T_LBRACE {
1062 symtyp = FVFT;
1063 enumval = 0;
1064 }
1065 ;
1066
1067 enums_with_opt_comma: /* helper for C99 6.7.2.2 */
1068 enumerator_list
1069 | enumerator_list T_COMMA {
1070 if (!allow_c99 && !allow_trad) {
1071 /* trailing ',' prohibited in enum declaration */
1072 error(54);
1073 } else {
1074 /* trailing ',' prohibited in enum declaration */
1075 c99ism(54);
1076 }
1077 $$ = $1;
1078 }
1079 ;
1080
1081 enumerator_list: /* C99 6.7.2.2 */
1082 enumerator
1083 | enumerator_list T_COMMA enumerator {
1084 $$ = concat_symbols($1, $3);
1085 }
1086 | error {
1087 $$ = NULL;
1088 }
1089 ;
1090
1091 enumerator: /* C99 6.7.2.2 */
1092 identifier_sym gcc_attribute_specifier_list_opt {
1093 $$ = enumeration_constant($1, enumval, true);
1094 }
1095 | identifier_sym gcc_attribute_specifier_list_opt
1096 T_ASSIGN constant_expr {
1097 $$ = enumeration_constant($1, to_int_constant($4, true),
1098 false);
1099 }
1100 ;
1101
1102 type_qualifier: /* C99 6.7.3 */
1103 T_QUAL
1104 | atomic {
1105 $$ = ATOMIC;
1106 }
1107 ;
1108
1109 atomic: /* helper */
1110 T_ATOMIC {
1111 /* TODO: First fix c11ism, then use it here. */
1112 if (!allow_c11)
1113 /* '_Atomic' requires C11 or later */
1114 error(350);
1115 }
1116 ;
1117
1118 pointer: /* C99 6.7.5 */
1119 asterisk type_qualifier_list_opt {
1120 $$ = merge_qualified_pointer($1, $2);
1121 }
1122 | asterisk type_qualifier_list_opt pointer {
1123 $$ = merge_qualified_pointer($1, $2);
1124 $$ = merge_qualified_pointer($$, $3);
1125 }
1126 ;
1127
1128 asterisk: /* helper for 'pointer' */
1129 T_ASTERISK {
1130 $$ = xcalloc(1, sizeof(*$$));
1131 $$->p_pointer = true;
1132 }
1133 ;
1134
1135 type_qualifier_list_opt: /* see C99 6.7.5 */
1136 /* empty */ {
1137 $$ = NULL;
1138 }
1139 | type_qualifier_list
1140 ;
1141
1142 type_qualifier_list: /* C99 6.7.5 */
1143 type_qualifier_list_elem
1144 | type_qualifier_list type_qualifier_list_elem {
1145 $$ = merge_qualified_pointer($1, $2);
1146 }
1147 ;
1148
1149 type_qualifier_list_elem: /* helper for 'pointer' */
1150 type_qualifier {
1151 $$ = xcalloc(1, sizeof(*$$));
1152 if ($1 == CONST)
1153 $$->p_const = true;
1154 if ($1 == VOLATILE)
1155 $$->p_volatile = true;
1156 }
1157 ;
1158
1159 /*
1160 * For an explanation of 'notype' in the following rules, see
1161 * https://www.gnu.org/software/bison/manual/bison.html#Semantic-Tokens.
1162 */
1163
1164 notype_init_declarators:
1165 notype_init_declarator
1166 | notype_init_declarators T_COMMA type_init_declarator
1167 ;
1168
1169 type_init_declarators:
1170 type_init_declarator
1171 | type_init_declarators T_COMMA type_init_declarator
1172 ;
1173
1174 notype_init_declarator:
1175 notype_declarator asm_or_symbolrename_opt {
1176 cgram_declare($1, false, $2);
1177 check_size($1);
1178 }
1179 | notype_declarator asm_or_symbolrename_opt {
1180 begin_initialization($1);
1181 cgram_declare($1, true, $2);
1182 } T_ASSIGN initializer {
1183 check_size($1);
1184 end_initialization();
1185 }
1186 ;
1187
1188 type_init_declarator:
1189 type_declarator asm_or_symbolrename_opt {
1190 cgram_declare($1, false, $2);
1191 check_size($1);
1192 }
1193 | type_declarator asm_or_symbolrename_opt {
1194 begin_initialization($1);
1195 cgram_declare($1, true, $2);
1196 } T_ASSIGN initializer {
1197 check_size($1);
1198 end_initialization();
1199 }
1200 ;
1201
1202 notype_declarator:
1203 notype_direct_declarator
1204 | pointer notype_direct_declarator {
1205 $$ = add_pointer($2, $1);
1206 }
1207 ;
1208
1209 type_declarator:
1210 type_direct_declarator
1211 | pointer type_direct_declarator {
1212 $$ = add_pointer($2, $1);
1213 }
1214 ;
1215
1216 notype_direct_declarator:
1217 type_attribute_list_opt T_NAME {
1218 $$ = declarator_name(getsym($2));
1219 }
1220 | type_attribute_list_opt T_LPAREN type_declarator T_RPAREN {
1221 $$ = $3;
1222 }
1223 | notype_direct_declarator T_LBRACK array_size_opt T_RBRACK {
1224 $$ = add_array($1, $3.has_dim, $3.dim);
1225 }
1226 | notype_direct_declarator param_list asm_or_symbolrename_opt {
1227 $$ = add_function(symbolrename($1, $3), $2);
1228 end_declaration_level();
1229 block_level--;
1230 }
1231 | notype_direct_declarator type_attribute
1232 ;
1233
1234 type_direct_declarator:
1235 type_attribute_list_opt identifier {
1236 $$ = declarator_name(getsym($2));
1237 }
1238 | type_attribute_list_opt T_LPAREN type_declarator T_RPAREN {
1239 $$ = $3;
1240 }
1241 | type_direct_declarator T_LBRACK array_size_opt T_RBRACK {
1242 $$ = add_array($1, $3.has_dim, $3.dim);
1243 }
1244 | type_direct_declarator param_list asm_or_symbolrename_opt {
1245 $$ = add_function(symbolrename($1, $3), $2);
1246 end_declaration_level();
1247 block_level--;
1248 }
1249 | type_direct_declarator type_attribute
1250 ;
1251
1252 /*
1253 * The two distinct rules type_param_declarator and notype_param_declarator
1254 * avoid a conflict in argument lists. A typename enclosed in parentheses is
1255 * always treated as a typename, not an argument name. For example, after
1256 * "typedef double a;", the declaration "f(int (a));" is interpreted as
1257 * "f(int (double));", not "f(int a);".
1258 */
1259 type_param_declarator:
1260 direct_param_declarator
1261 | pointer direct_param_declarator {
1262 $$ = add_pointer($2, $1);
1263 }
1264 ;
1265
1266 notype_param_declarator:
1267 direct_notype_param_declarator
1268 | pointer direct_notype_param_declarator {
1269 $$ = add_pointer($2, $1);
1270 }
1271 ;
1272
1273 direct_param_declarator:
1274 identifier type_attribute_list {
1275 $$ = declarator_name(getsym($1));
1276 }
1277 | identifier {
1278 $$ = declarator_name(getsym($1));
1279 }
1280 | T_LPAREN notype_param_declarator T_RPAREN {
1281 $$ = $2;
1282 }
1283 | direct_param_declarator T_LBRACK array_size_opt T_RBRACK
1284 gcc_attribute_specifier_list_opt {
1285 $$ = add_array($1, $3.has_dim, $3.dim);
1286 }
1287 | direct_param_declarator param_list asm_or_symbolrename_opt {
1288 $$ = add_function(symbolrename($1, $3), $2);
1289 end_declaration_level();
1290 block_level--;
1291 }
1292 ;
1293
1294 direct_notype_param_declarator:
1295 identifier {
1296 $$ = declarator_name(getsym($1));
1297 }
1298 | T_LPAREN notype_param_declarator T_RPAREN {
1299 $$ = $2;
1300 }
1301 | direct_notype_param_declarator T_LBRACK array_size_opt T_RBRACK {
1302 $$ = add_array($1, $3.has_dim, $3.dim);
1303 }
1304 | direct_notype_param_declarator param_list asm_or_symbolrename_opt {
1305 $$ = add_function(symbolrename($1, $3), $2);
1306 end_declaration_level();
1307 block_level--;
1308 }
1309 ;
1310
1311 param_list:
1312 id_list_lparen identifier_list T_RPAREN {
1313 $$ = $2;
1314 }
1315 | abstract_decl_param_list
1316 ;
1317
1318 id_list_lparen:
1319 T_LPAREN {
1320 block_level++;
1321 begin_declaration_level(DK_PROTO_ARG);
1322 }
1323 ;
1324
1325 array_size_opt:
1326 /* empty */ {
1327 $$.has_dim = false;
1328 $$.dim = 0;
1329 }
1330 | T_ASTERISK {
1331 /* since C99; variable length array of unspecified size */
1332 $$.has_dim = false; /* TODO: maybe change to true */
1333 $$.dim = 0; /* just as a placeholder */
1334 }
1335 | array_size {
1336 $$.has_dim = true;
1337 $$.dim = $1 == NULL ? 0 : to_int_constant($1, false);
1338 }
1339 ;
1340
1341 array_size:
1342 type_qualifier_list_opt T_SCLASS constant_expr {
1343 /* C11 6.7.6.3p7 */
1344 if ($2 != STATIC)
1345 yyerror("Bad attribute");
1346 /* static array size is a C11 extension */
1347 c11ism(343);
1348 $$ = $3;
1349 }
1350 | type_qualifier {
1351 /* C11 6.7.6.2 */
1352 if ($1 != RESTRICT)
1353 yyerror("Bad attribute");
1354 $$ = NULL;
1355 }
1356 | constant_expr
1357 ;
1358
1359 identifier_list: /* C99 6.7.5 */
1360 T_NAME {
1361 $$ = old_style_function_parameter_name(getsym($1));
1362 }
1363 | identifier_list T_COMMA T_NAME {
1364 $$ = concat_symbols($1,
1365 old_style_function_parameter_name(getsym($3)));
1366 }
1367 | identifier_list error
1368 ;
1369
1370 /* XXX: C99 requires an additional specifier-qualifier-list. */
1371 type_name: /* C99 6.7.6 */
1372 {
1373 begin_declaration_level(DK_ABSTRACT);
1374 } abstract_declaration {
1375 end_declaration_level();
1376 $$ = $2->s_type;
1377 }
1378 ;
1379
1380 abstract_declaration: /* specific to lint */
1381 begin_type_qualifier_list end_type {
1382 $$ = declare_abstract_type(abstract_name());
1383 }
1384 | begin_type_specifier_qualifier_list end_type {
1385 $$ = declare_abstract_type(abstract_name());
1386 }
1387 | begin_type_qualifier_list end_type abstract_declarator {
1388 $$ = declare_abstract_type($3);
1389 }
1390 | begin_type_specifier_qualifier_list end_type abstract_declarator {
1391 $$ = declare_abstract_type($3);
1392 }
1393 ;
1394
1395 /* K&R 8.7, C90 ???, C99 6.7.6, C11 6.7.7 */
1396 /* In K&R, abstract-declarator could be empty and was still simpler. */
1397 abstract_declarator:
1398 pointer {
1399 $$ = add_pointer(abstract_name(), $1);
1400 }
1401 | direct_abstract_declarator
1402 | pointer direct_abstract_declarator {
1403 $$ = add_pointer($2, $1);
1404 }
1405 | type_attribute_list direct_abstract_declarator {
1406 $$ = $2;
1407 }
1408 | pointer type_attribute_list direct_abstract_declarator {
1409 $$ = add_pointer($3, $1);
1410 }
1411 ;
1412
1413 /* K&R ---, C90 ???, C99 6.7.6, C11 6.7.7 */
1414 direct_abstract_declarator:
1415 /* TODO: sort rules according to C99 */
1416 T_LPAREN abstract_declarator T_RPAREN {
1417 $$ = $2;
1418 }
1419 | T_LBRACK array_size_opt T_RBRACK {
1420 $$ = add_array(abstract_name(), $2.has_dim, $2.dim);
1421 }
1422 | direct_abstract_declarator T_LBRACK array_size_opt T_RBRACK {
1423 $$ = add_array($1, $3.has_dim, $3.dim);
1424 }
1425 | abstract_decl_param_list asm_or_symbolrename_opt {
1426 $$ = add_function(symbolrename(abstract_name(), $2), $1);
1427 end_declaration_level();
1428 block_level--;
1429 }
1430 | direct_abstract_declarator abstract_decl_param_list
1431 asm_or_symbolrename_opt {
1432 $$ = add_function(symbolrename($1, $3), $2);
1433 end_declaration_level();
1434 block_level--;
1435 }
1436 | direct_abstract_declarator type_attribute_list
1437 ;
1438
1439 abstract_decl_param_list: /* specific to lint */
1440 abstract_decl_lparen T_RPAREN type_attribute_opt {
1441 $$ = NULL;
1442 }
1443 | abstract_decl_lparen vararg_parameter_type_list T_RPAREN
1444 type_attribute_opt {
1445 dcs->d_prototype = true;
1446 $$ = $2;
1447 }
1448 | abstract_decl_lparen error T_RPAREN type_attribute_opt {
1449 $$ = NULL;
1450 }
1451 ;
1452
1453 abstract_decl_lparen: /* specific to lint */
1454 T_LPAREN {
1455 block_level++;
1456 begin_declaration_level(DK_PROTO_ARG);
1457 }
1458 ;
1459
1460 vararg_parameter_type_list: /* specific to lint */
1461 parameter_type_list
1462 | parameter_type_list T_COMMA T_ELLIPSIS {
1463 dcs->d_vararg = true;
1464 $$ = $1;
1465 }
1466 | T_ELLIPSIS {
1467 /* TODO: C99 6.7.5 makes this an error as well. */
1468 if (!allow_trad && !allow_c99) {
1469 /* ANSI C requires formal parameter before '...' */
1470 error(84);
1471 } else if (allow_c90) {
1472 /* ANSI C requires formal parameter before '...' */
1473 warning(84);
1474 }
1475 dcs->d_vararg = true;
1476 $$ = NULL;
1477 }
1478 ;
1479
1480 /* XXX: C99 6.7.5 defines the same name, but it looks different. */
1481 parameter_type_list:
1482 parameter_declaration
1483 | parameter_type_list T_COMMA parameter_declaration {
1484 $$ = concat_symbols($1, $3);
1485 }
1486 ;
1487
1488 /* XXX: C99 6.7.5 defines the same name, but it looks completely different. */
1489 parameter_declaration:
1490 begin_type_declmods end_type {
1491 /* ^^ There is no check for the missing type-specifier. */
1492 $$ = declare_argument(abstract_name(), false);
1493 }
1494 | begin_type_declaration_specifiers end_type {
1495 $$ = declare_argument(abstract_name(), false);
1496 }
1497 | begin_type_declmods end_type notype_param_declarator {
1498 /* ^^ There is no check for the missing type-specifier. */
1499 $$ = declare_argument($3, false);
1500 }
1501 /*
1502 * type_param_declarator is needed because of following conflict:
1503 * "typedef int a; f(int (a));" could be parsed as
1504 * "function with argument a of type int", or
1505 * "function with an abstract argument of type function".
1506 * This grammar realizes the second case.
1507 */
1508 | begin_type_declaration_specifiers end_type type_param_declarator {
1509 $$ = declare_argument($3, false);
1510 }
1511 | begin_type_declmods end_type abstract_declarator {
1512 /* ^^ There is no check for the missing type-specifier. */
1513 $$ = declare_argument($3, false);
1514 }
1515 | begin_type_declaration_specifiers end_type abstract_declarator {
1516 $$ = declare_argument($3, false);
1517 }
1518 ;
1519
1520 initializer: /* C99 6.7.8 "Initialization" */
1521 assignment_expression {
1522 init_expr($1);
1523 }
1524 | init_lbrace init_rbrace {
1525 /* XXX: Empty braces are not covered by C99 6.7.8. */
1526 }
1527 | init_lbrace initializer_list comma_opt init_rbrace
1528 /* XXX: What is this error handling for? */
1529 | error
1530 ;
1531
1532 initializer_list: /* C99 6.7.8 "Initialization" */
1533 initializer_list_item
1534 | initializer_list T_COMMA initializer_list_item
1535 ;
1536
1537 initializer_list_item: /* helper */
1538 designation initializer
1539 | initializer
1540 ;
1541
1542 designation: /* C99 6.7.8 "Initialization" */
1543 begin_designation designator_list T_ASSIGN
1544 | identifier T_COLON {
1545 /* GCC style struct or union member name in initializer */
1546 gnuism(315);
1547 begin_designation();
1548 add_designator_member($1);
1549 }
1550 ;
1551
1552 begin_designation: /* lint-specific helper */
1553 /* empty */ {
1554 begin_designation();
1555 }
1556 ;
1557
1558 designator_list: /* C99 6.7.8 "Initialization" */
1559 designator
1560 | designator_list designator
1561 ;
1562
1563 designator: /* C99 6.7.8 "Initialization" */
1564 T_LBRACK range T_RBRACK {
1565 if (!allow_c99)
1566 /* array initializer with designators is a C99 ... */
1567 warning(321);
1568 add_designator_subscript($2);
1569 }
1570 | T_POINT identifier {
1571 if (!allow_c99)
1572 /* struct or union member name in initializer is ... */
1573 warning(313);
1574 add_designator_member($2);
1575 }
1576 ;
1577
1578 static_assert_declaration:
1579 T_STATIC_ASSERT T_LPAREN constant_expr T_COMMA T_STRING T_RPAREN T_SEMI /* C11 */
1580 | T_STATIC_ASSERT T_LPAREN constant_expr T_RPAREN T_SEMI /* C23 */
1581 ;
1582
1583 range:
1584 constant_expr {
1585 $$.lo = to_int_constant($1, true);
1586 $$.hi = $$.lo;
1587 }
1588 | constant_expr T_ELLIPSIS constant_expr {
1589 $$.lo = to_int_constant($1, true);
1590 $$.hi = to_int_constant($3, true);
1591 /* initialization with '[a...b]' is a GCC extension */
1592 gnuism(340);
1593 }
1594 ;
1595
1596 init_lbrace: /* helper */
1597 T_LBRACE {
1598 init_lbrace();
1599 }
1600 ;
1601
1602 init_rbrace: /* helper */
1603 T_RBRACE {
1604 init_rbrace();
1605 }
1606 ;
1607
1608 asm_or_symbolrename_opt: /* GCC extensions */
1609 /* empty */ {
1610 $$ = NULL;
1611 }
1612 | T_ASM T_LPAREN T_STRING T_RPAREN gcc_attribute_specifier_list_opt {
1613 freeyyv(&$3, T_STRING);
1614 $$ = NULL;
1615 }
1616 | T_SYMBOLRENAME T_LPAREN T_NAME T_RPAREN
1617 gcc_attribute_specifier_list_opt {
1618 $$ = $3;
1619 }
1620 ;
1621
1622 statement: /* C99 6.8 */
1623 expression_statement
1624 | non_expr_statement
1625 ;
1626
1627 non_expr_statement: /* helper for C99 6.8 */
1628 gcc_attribute_specifier /* ((__fallthrough__)) */ T_SEMI
1629 | labeled_statement
1630 | compound_statement
1631 | selection_statement
1632 | iteration_statement
1633 | jump_statement {
1634 seen_fallthrough = false;
1635 }
1636 | asm_statement
1637 ;
1638
1639 labeled_statement: /* C99 6.8.1 */
1640 label gcc_attribute_specifier_list_opt statement
1641 ;
1642
1643 label:
1644 T_NAME T_COLON {
1645 symtyp = FLABEL;
1646 named_label(getsym($1));
1647 }
1648 | T_CASE constant_expr T_COLON {
1649 case_label($2);
1650 seen_fallthrough = true;
1651 }
1652 | T_CASE constant_expr T_ELLIPSIS constant_expr T_COLON {
1653 /* XXX: We don't fill all cases */
1654 case_label($2);
1655 seen_fallthrough = true;
1656 }
1657 | T_DEFAULT T_COLON {
1658 default_label();
1659 seen_fallthrough = true;
1660 }
1661 ;
1662
1663 compound_statement: /* C99 6.8.2 */
1664 compound_statement_lbrace compound_statement_rbrace
1665 | compound_statement_lbrace block_item_list compound_statement_rbrace
1666 ;
1667
1668 compound_statement_lbrace:
1669 T_LBRACE {
1670 block_level++;
1671 mem_block_level++;
1672 begin_declaration_level(DK_AUTO);
1673 }
1674 ;
1675
1676 compound_statement_rbrace:
1677 T_RBRACE {
1678 end_declaration_level();
1679 level_free_all(mem_block_level);
1680 mem_block_level--;
1681 block_level--;
1682 seen_fallthrough = false;
1683 }
1684 ;
1685
1686 block_item_list: /* C99 6.8.2 */
1687 block_item
1688 | block_item_list block_item {
1689 if ($1 && !$2)
1690 /* declarations after statements is a C99 feature */
1691 c99ism(327);
1692 $$ = $1 || $2;
1693 }
1694 ;
1695
1696 block_item: /* C99 6.8.2 */
1697 declaration_or_error {
1698 $$ = false;
1699 restore_warning_flags();
1700 }
1701 | statement {
1702 $$ = true;
1703 restore_warning_flags();
1704 }
1705 ;
1706
1707 expression_statement: /* C99 6.8.3 */
1708 expression T_SEMI {
1709 expr($1, false, false, false, false);
1710 seen_fallthrough = false;
1711 }
1712 | T_SEMI {
1713 check_statement_reachable();
1714 seen_fallthrough = false;
1715 }
1716 ;
1717
1718 selection_statement: /* C99 6.8.4 */
1719 if_without_else %prec T_THEN {
1720 save_warning_flags();
1721 stmt_if_then_stmt();
1722 stmt_if_else_stmt(false);
1723 }
1724 | if_without_else T_ELSE {
1725 save_warning_flags();
1726 stmt_if_then_stmt();
1727 } statement {
1728 clear_warning_flags();
1729 stmt_if_else_stmt(true);
1730 }
1731 | if_without_else T_ELSE error {
1732 clear_warning_flags();
1733 stmt_if_else_stmt(false);
1734 }
1735 | switch_expr statement {
1736 clear_warning_flags();
1737 stmt_switch_expr_stmt();
1738 }
1739 | switch_expr error {
1740 clear_warning_flags();
1741 stmt_switch_expr_stmt();
1742 }
1743 ;
1744
1745 if_without_else: /* see C99 6.8.4 */
1746 if_expr statement
1747 | if_expr error
1748 ;
1749
1750 if_expr: /* see C99 6.8.4 */
1751 T_IF T_LPAREN expression T_RPAREN {
1752 stmt_if_expr($3);
1753 clear_warning_flags();
1754 }
1755 ;
1756
1757 switch_expr: /* see C99 6.8.4 */
1758 T_SWITCH T_LPAREN expression T_RPAREN {
1759 stmt_switch_expr($3);
1760 clear_warning_flags();
1761 }
1762 ;
1763
1764 iteration_statement: /* C99 6.8.5 */
1765 while_expr statement {
1766 clear_warning_flags();
1767 stmt_while_expr_stmt();
1768 }
1769 | while_expr error {
1770 clear_warning_flags();
1771 stmt_while_expr_stmt();
1772 }
1773 | do_statement do_while_expr {
1774 stmt_do_while_expr($2);
1775 seen_fallthrough = false;
1776 }
1777 | do error {
1778 clear_warning_flags();
1779 stmt_do_while_expr(NULL);
1780 }
1781 | for_exprs statement {
1782 clear_warning_flags();
1783 stmt_for_exprs_stmt();
1784 end_declaration_level();
1785 block_level--;
1786 }
1787 | for_exprs error {
1788 clear_warning_flags();
1789 stmt_for_exprs_stmt();
1790 end_declaration_level();
1791 block_level--;
1792 }
1793 ;
1794
1795 while_expr: /* see C99 6.8.5 */
1796 T_WHILE T_LPAREN expression T_RPAREN {
1797 stmt_while_expr($3);
1798 clear_warning_flags();
1799 }
1800 ;
1801
1802 do_statement: /* see C99 6.8.5 */
1803 do statement {
1804 clear_warning_flags();
1805 }
1806 ;
1807
1808 do: /* see C99 6.8.5 */
1809 T_DO {
1810 stmt_do();
1811 }
1812 ;
1813
1814 do_while_expr: /* see C99 6.8.5 */
1815 T_WHILE T_LPAREN expression T_RPAREN T_SEMI {
1816 $$ = $3;
1817 }
1818 ;
1819
1820 for_start: /* see C99 6.8.5 */
1821 T_FOR T_LPAREN {
1822 begin_declaration_level(DK_AUTO);
1823 block_level++;
1824 }
1825 ;
1826
1827 for_exprs: /* see C99 6.8.5 */
1828 for_start
1829 begin_type_declaration_specifiers end_type
1830 notype_init_declarators T_SEMI
1831 expression_opt T_SEMI
1832 expression_opt T_RPAREN {
1833 /* variable declaration in for loop */
1834 c99ism(325);
1835 stmt_for_exprs(NULL, $6, $8);
1836 clear_warning_flags();
1837 }
1838 | for_start
1839 expression_opt T_SEMI
1840 expression_opt T_SEMI
1841 expression_opt T_RPAREN {
1842 stmt_for_exprs($2, $4, $6);
1843 clear_warning_flags();
1844 }
1845 ;
1846
1847 jump_statement: /* C99 6.8.6 */
1848 goto identifier T_SEMI {
1849 stmt_goto(getsym($2));
1850 }
1851 | goto error T_SEMI {
1852 symtyp = FVFT;
1853 }
1854 | T_CONTINUE T_SEMI {
1855 stmt_continue();
1856 }
1857 | T_BREAK T_SEMI {
1858 stmt_break();
1859 }
1860 | T_RETURN sys T_SEMI {
1861 stmt_return($2, NULL);
1862 }
1863 | T_RETURN sys expression T_SEMI {
1864 stmt_return($2, $3);
1865 }
1866 ;
1867
1868 goto: /* see C99 6.8.6 */
1869 T_GOTO {
1870 symtyp = FLABEL;
1871 }
1872 ;
1873
1874 asm_statement: /* GCC extension */
1875 T_ASM T_LPAREN read_until_rparen T_SEMI {
1876 dcs_set_asm();
1877 }
1878 | T_ASM type_qualifier T_LPAREN read_until_rparen T_SEMI {
1879 dcs_set_asm();
1880 }
1881 | T_ASM error
1882 ;
1883
1884 read_until_rparen: /* helper for 'asm_statement' */
1885 /* empty */ {
1886 read_until_rparen();
1887 }
1888 ;
1889
1890 translation_unit: /* C99 6.9 */
1891 external_declaration
1892 | translation_unit external_declaration
1893 ;
1894
1895 external_declaration: /* C99 6.9 */
1896 function_definition {
1897 global_clean_up_decl(false);
1898 clear_warning_flags();
1899 }
1900 | top_level_declaration {
1901 global_clean_up_decl(false);
1902 clear_warning_flags();
1903 }
1904 | asm_statement /* GCC extension */
1905 | T_SEMI { /* GCC extension */
1906 /*
1907 * TODO: Only allow this in GCC mode, not in plain C99.
1908 * This is one of the top 10 warnings in the NetBSD build.
1909 */
1910 if (!allow_trad && !allow_c99) {
1911 /* empty declaration */
1912 error(0);
1913 } else if (allow_c90) {
1914 /* empty declaration */
1915 warning(0);
1916 }
1917 }
1918 ;
1919
1920 /*
1921 * On the top level, lint allows several forms of declarations that it doesn't
1922 * allow in functions. For example, a single ';' is an empty declaration and
1923 * is supported by some compilers, but in a function it would be an empty
1924 * statement, not a declaration. This makes a difference in C90 mode, where
1925 * a statement must not be followed by a declaration.
1926 *
1927 * See 'declaration' for all other declarations.
1928 */
1929 top_level_declaration: /* C99 6.9 calls this 'declaration' */
1930 begin_type end_type notype_init_declarators T_SEMI {
1931 /* TODO: Make this an error in C99 mode as well. */
1932 if (!allow_trad && !allow_c99) {
1933 /* old-style declaration; add 'int' */
1934 error(1);
1935 } else if (allow_c90) {
1936 /* old-style declaration; add 'int' */
1937 warning(1);
1938 }
1939 }
1940 | declaration
1941 | error T_SEMI {
1942 global_clean_up();
1943 }
1944 | error T_RBRACE {
1945 global_clean_up();
1946 }
1947 ;
1948
1949 function_definition: /* C99 6.9.1 */
1950 func_declarator {
1951 if ($1->s_type->t_tspec != FUNC) {
1952 /* syntax error '%s' */
1953 error(249, yytext);
1954 YYERROR;
1955 }
1956 if ($1->s_type->t_typedef) {
1957 /* ()-less function definition */
1958 error(64);
1959 YYERROR;
1960 }
1961 begin_function($1);
1962 block_level++;
1963 begin_declaration_level(DK_OLD_STYLE_ARG);
1964 if (lwarn == LWARN_NONE)
1965 $1->s_used = true;
1966 } arg_declaration_list_opt {
1967 end_declaration_level();
1968 block_level--;
1969 check_func_lint_directives();
1970 check_func_old_style_arguments();
1971 begin_control_statement(CS_FUNCTION_BODY);
1972 } compound_statement {
1973 end_function();
1974 end_control_statement(CS_FUNCTION_BODY);
1975 }
1976 ;
1977
1978 func_declarator:
1979 begin_type end_type notype_declarator {
1980 if (!allow_trad) {
1981 /* old-style declaration; add 'int' */
1982 error(1);
1983 }
1984 $$ = $3;
1985 }
1986 | begin_type_declmods end_type notype_declarator {
1987 if (!allow_trad) {
1988 /* old-style declaration; add 'int' */
1989 error(1);
1990 }
1991 $$ = $3;
1992 }
1993 | begin_type_declaration_specifiers end_type type_declarator {
1994 $$ = $3;
1995 }
1996 ;
1997
1998 arg_declaration_list_opt: /* C99 6.9.1p13 example 1 */
1999 /* empty */
2000 | arg_declaration_list
2001 ;
2002
2003 arg_declaration_list: /* C99 6.9.1p13 example 1 */
2004 arg_declaration
2005 | arg_declaration_list arg_declaration
2006 /* XXX or better "arg_declaration error" ? */
2007 | error
2008 ;
2009
2010 /*
2011 * "arg_declaration" is separated from "declaration" because it
2012 * needs other error handling.
2013 */
2014 arg_declaration:
2015 begin_type_declmods end_type T_SEMI {
2016 /* empty declaration */
2017 warning(2);
2018 }
2019 | begin_type_declmods end_type notype_init_declarators T_SEMI
2020 | begin_type_declaration_specifiers end_type T_SEMI {
2021 if (!dcs->d_nonempty_decl) {
2022 /* empty declaration */
2023 warning(2);
2024 } else {
2025 /* '%s' declared in argument declaration list */
2026 warning(3, type_name(dcs->d_type));
2027 }
2028 }
2029 | begin_type_declaration_specifiers end_type
2030 type_init_declarators T_SEMI {
2031 if (dcs->d_nonempty_decl) {
2032 /* '%s' declared in argument declaration list */
2033 warning(3, type_name(dcs->d_type));
2034 }
2035 }
2036 | begin_type_declmods error
2037 | begin_type_declaration_specifiers error
2038 ;
2039
2040 /* https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html */
2041 gcc_attribute_specifier_list_opt:
2042 /* empty */
2043 | gcc_attribute_specifier_list
2044 ;
2045
2046 gcc_attribute_specifier_list:
2047 gcc_attribute_specifier
2048 | gcc_attribute_specifier_list gcc_attribute_specifier
2049 ;
2050
2051 gcc_attribute_specifier:
2052 T_ATTRIBUTE T_LPAREN T_LPAREN {
2053 in_gcc_attribute = true;
2054 } gcc_attribute_list {
2055 in_gcc_attribute = false;
2056 } T_RPAREN T_RPAREN
2057 ;
2058
2059 gcc_attribute_list:
2060 gcc_attribute
2061 | gcc_attribute_list T_COMMA gcc_attribute
2062 ;
2063
2064 gcc_attribute:
2065 /* empty */
2066 | T_NAME {
2067 const char *name = $1->sb_name;
2068 if (is_either(name, "packed", "__packed__"))
2069 dcs_add_packed();
2070 else if (is_either(name, "used", "__used__") ||
2071 is_either(name, "unused", "__unused__"))
2072 dcs_set_used();
2073 else if (is_either(name, "fallthrough", "__fallthrough__"))
2074 fallthru(1);
2075 }
2076 | T_NAME T_LPAREN T_RPAREN
2077 | T_NAME T_LPAREN gcc_attribute_parameters T_RPAREN
2078 | type_qualifier {
2079 if ($1 != CONST)
2080 yyerror("Bad attribute");
2081 }
2082 ;
2083
2084 gcc_attribute_parameters:
2085 constant_expr
2086 | gcc_attribute_parameters T_COMMA constant_expr
2087 ;
2088
2089 sys:
2090 /* empty */ {
2091 $$ = in_system_header;
2092 }
2093 ;
2094
2095 %%
2096
2097 /* ARGSUSED */
2098 int
2099 yyerror(const char *msg)
2100 {
2101 /* syntax error '%s' */
2102 error(249, yytext);
2103 if (++sytxerr >= 5)
2104 norecover();
2105 return 0;
2106 }
2107
2108 #if YYDEBUG && (YYBYACC || YYBISON)
2109 static const char *
2110 cgram_to_string(int token, YYSTYPE val)
2111 {
2112
2113 switch (token) {
2114 case T_INCDEC:
2115 case T_MULTIPLICATIVE:
2116 case T_ADDITIVE:
2117 case T_SHIFT:
2118 case T_RELATIONAL:
2119 case T_EQUALITY:
2120 case T_OPASSIGN:
2121 return modtab[val.y_op].m_name;
2122 case T_SCLASS:
2123 return scl_name(val.y_scl);
2124 case T_TYPE:
2125 case T_STRUCT_OR_UNION:
2126 return tspec_name(val.y_tspec);
2127 case T_QUAL:
2128 return tqual_name(val.y_tqual);
2129 case T_NAME:
2130 return val.y_name->sb_name;
2131 default:
2132 return "<none>";
2133 }
2134 }
2135 #endif
2136
2137 #if YYDEBUG && YYBISON
2138 static inline void
2139 cgram_print(FILE *output, int token, YYSTYPE val)
2140 {
2141 (void)fprintf(output, "%s", cgram_to_string(token, val));
2142 }
2143 #endif
2144
2145 static void
2146 cgram_declare(sym_t *decl, bool has_initializer, sbuf_t *renaming)
2147 {
2148 declare(decl, has_initializer, renaming);
2149 if (renaming != NULL)
2150 freeyyv(&renaming, T_NAME);
2151 }
2152
2153 /*
2154 * Discard all input tokens up to and including the next
2155 * unmatched right paren
2156 */
2157 static void
2158 read_until_rparen(void)
2159 {
2160 int level;
2161
2162 if (yychar < 0)
2163 yychar = yylex();
2164 freeyyv(&yylval, yychar);
2165
2166 level = 1;
2167 while (yychar > 0) {
2168 if (yychar == T_LPAREN)
2169 level++;
2170 if (yychar == T_RPAREN && --level == 0)
2171 break;
2172 freeyyv(&yylval, yychar = yylex());
2173 }
2174
2175 yyclearin;
2176 }
2177
2178 static sym_t *
2179 symbolrename(sym_t *s, sbuf_t *sb)
2180 {
2181 if (sb != NULL)
2182 s->s_rename = sb->sb_name;
2183 return s;
2184 }
2185