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