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