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