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