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