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