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