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