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