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