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