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