cgram.y revision 1.117 1 %{
2 /* $NetBSD: cgram.y,v 1.117 2020/12/30 01:02:38 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.117 2020/12/30 01:02:38 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 * Contains the level of current declaration. 0 is extern.
50 * Used for symbol table entries.
51 */
52 int blklev;
53
54 /*
55 * level for memory allocation. Normally the same as blklev.
56 * An exception is the declaration of arguments in prototypes. Memory
57 * for these can't be freed after the declaration, but symbols must
58 * be removed from the symbol table after the declaration.
59 */
60 int mblklev;
61
62 /*
63 * Save the no-warns state and restore it to avoid the problem where
64 * if (expr) { stmt } / * NOLINT * / stmt;
65 */
66 static int olwarn = LWARN_BAD;
67
68 static int toicon(tnode_t *, int);
69 static void idecl(sym_t *, int, sbuf_t *);
70 static void ignuptorp(void);
71 static sym_t *symbolrename(sym_t *, sbuf_t *);
72
73
74 #ifdef DEBUG
75 static inline void CLRWFLGS(const char *file, size_t line);
76 static inline void CLRWFLGS(const char *file, size_t line)
77 {
78 printf("%s, %d: clear flags %s %zu\n", curr_pos.p_file,
79 curr_pos.p_line, file, line);
80 clrwflgs();
81 olwarn = LWARN_BAD;
82 }
83
84 static inline void SAVE(const char *file, size_t line);
85 static inline void SAVE(const char *file, size_t line)
86 {
87 if (olwarn != LWARN_BAD)
88 abort();
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 inline void RESTORE(const char *file, size_t line);
95 static inline void RESTORE(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 CLRWFLGS(file, line);
104 }
105 #define cgram_debug(fmt, args...) printf("cgram_debug: " fmt "\n", ##args)
106 #else
107 #define CLRWFLGS(f, l) clrwflgs(), olwarn = LWARN_BAD
108 #define SAVE(f, l) olwarn = lwarn
109 #define RESTORE(f, l) (void)(olwarn == LWARN_BAD ? (clrwflgs(), 0) : (lwarn = olwarn))
110 #define cgram_debug(fmt, args...) (void)0
111 #endif
112
113 /* unbind the anonymous struct members from the struct */
114 static void
115 anonymize(sym_t *s)
116 {
117 for ( ; s; s = s->s_nxt)
118 s->s_styp = NULL;
119 }
120 %}
121
122 %expect 138
123
124 %union {
125 int y_int;
126 val_t *y_val;
127 sbuf_t *y_sb;
128 sym_t *y_sym;
129 op_t y_op;
130 scl_t y_scl;
131 tspec_t y_tspec;
132 tqual_t y_tqual;
133 type_t *y_type;
134 tnode_t *y_tnode;
135 range_t y_range;
136 strg_t *y_strg;
137 pqinf_t *y_pqinf;
138 };
139
140 %token T_LBRACE T_RBRACE T_LBRACK T_RBRACK T_LPARN T_RPARN
141 %token <y_op> T_STROP
142 %token <y_op> T_UNOP
143 %token <y_op> T_INCDEC
144 %token T_SIZEOF
145 %token T_BUILTIN_OFFSETOF
146 %token T_TYPEOF
147 %token T_EXTENSION
148 %token T_ALIGNOF
149 %token <y_op> T_MULT
150 %token <y_op> T_DIVOP
151 %token <y_op> T_ADDOP
152 %token <y_op> T_SHFTOP
153 %token <y_op> T_RELOP
154 %token <y_op> T_EQOP
155 %token <y_op> T_AND
156 %token <y_op> T_XOR
157 %token <y_op> T_OR
158 %token <y_op> T_LOGAND
159 %token <y_op> T_LOGOR
160 %token T_QUEST
161 %token T_COLON
162 %token <y_op> T_ASSIGN
163 %token <y_op> T_OPASS
164 %token T_COMMA
165 %token T_SEMI
166 %token T_ELLIPSE
167 %token T_REAL
168 %token T_IMAG
169 %token T_GENERIC
170 %token T_NORETURN
171
172 /* storage classes (extern, static, auto, register and typedef) */
173 %token <y_scl> T_SCLASS
174
175 /* types (char, int, short, long, unsigned, signed, float, double, void) */
176 %token <y_tspec> T_TYPE
177
178 /* qualifiers (const, volatile) */
179 %token <y_tqual> T_QUAL
180
181 /* struct or union */
182 %token <y_tspec> T_SOU
183
184 /* enum */
185 %token T_ENUM
186
187 /* remaining keywords */
188 %token T_CASE
189 %token T_DEFAULT
190 %token T_IF
191 %token T_ELSE
192 %token T_SWITCH
193 %token T_DO
194 %token T_WHILE
195 %token T_FOR
196 %token T_GOTO
197 %token T_CONTINUE
198 %token T_BREAK
199 %token T_RETURN
200 %token T_ASM
201 %token T_SYMBOLRENAME
202 %token T_PACKED
203 /* Type Attributes */
204 %token <y_type> T_ATTRIBUTE
205 %token <y_type> T_AT_ALIAS
206 %token <y_type> T_AT_ALLOC_SIZE
207 %token <y_type> T_AT_ALIGNED
208 %token <y_type> T_AT_ALWAYS_INLINE
209 %token <y_type> T_AT_BOUNDED
210 %token <y_type> T_AT_BUFFER
211 %token <y_type> T_AT_COLD
212 %token <y_type> T_AT_CONSTRUCTOR
213 %token <y_type> T_AT_DEPRECATED
214 %token <y_type> T_AT_DESTRUCTOR
215 %token <y_type> T_AT_FORMAT
216 %token <y_type> T_AT_FORMAT_ARG
217 %token <y_type> T_AT_FORMAT_GNU_PRINTF
218 %token <y_type> T_AT_FORMAT_PRINTF
219 %token <y_type> T_AT_FORMAT_SCANF
220 %token <y_type> T_AT_FORMAT_STRFMON
221 %token <y_type> T_AT_FORMAT_STRFTIME
222 %token <y_type> T_AT_FORMAT_SYSLOG
223 %token <y_type> T_AT_GNU_INLINE
224 %token <y_type> T_AT_MALLOC
225 %token <y_type> T_AT_MAY_ALIAS
226 %token <y_type> T_AT_MINBYTES
227 %token <y_type> T_AT_MODE
228 %token <y_type> T_AT_NOINLINE
229 %token <y_type> T_AT_NONNULL
230 %token <y_type> T_AT_NORETURN
231 %token <y_type> T_AT_NOTHROW
232 %token <y_type> T_AT_NO_INSTRUMENT_FUNCTION
233 %token <y_type> T_AT_OPTIMIZE
234 %token <y_type> T_AT_PACKED
235 %token <y_type> T_AT_PCS
236 %token <y_type> T_AT_PURE
237 %token <y_type> T_AT_RETURNS_TWICE
238 %token <y_type> T_AT_SECTION
239 %token <y_type> T_AT_SENTINEL
240 %token <y_type> T_AT_STRING
241 %token <y_type> T_AT_TLS_MODEL
242 %token <y_type> T_AT_TUNION
243 %token <y_type> T_AT_UNUSED
244 %token <y_type> T_AT_USED
245 %token <y_type> T_AT_VISIBILITY
246 %token <y_type> T_AT_WARN_UNUSED_RESULT
247 %token <y_type> T_AT_WEAK
248
249 %left T_COMMA
250 %right T_ASSIGN T_OPASS
251 %right T_QUEST T_COLON
252 %left T_LOGOR
253 %left T_LOGAND
254 %left T_OR
255 %left T_XOR
256 %left T_AND
257 %left T_EQOP
258 %left T_RELOP
259 %left T_SHFTOP
260 %left T_ADDOP
261 %left T_MULT T_DIVOP
262 %right T_UNOP T_INCDEC T_SIZEOF TBUILTIN_SIZEOF T_ALIGNOF T_REAL T_IMAG
263 %left T_LPARN T_LBRACK T_STROP
264
265 %token <y_sb> T_NAME
266 %token <y_sb> T_TYPENAME
267 %token <y_val> T_CON
268 %token <y_strg> T_STRING
269
270 %type <y_sym> func_decl
271 %type <y_sym> notype_decl
272 %type <y_sym> type_decl
273 %type <y_type> typespec
274 %type <y_type> clrtyp_typespec
275 %type <y_type> notype_typespec
276 %type <y_type> struct_spec
277 %type <y_type> enum_spec
278 %type <y_type> type_attribute
279 %type <y_sym> struct_tag
280 %type <y_sym> enum_tag
281 %type <y_tspec> struct
282 %type <y_sym> struct_declaration
283 %type <y_sb> identifier
284 %type <y_sym> member_declaration_list_with_rbrace
285 %type <y_sym> member_declaration_list
286 %type <y_sym> member_declaration
287 %type <y_sym> notype_member_decls
288 %type <y_sym> type_member_decls
289 %type <y_sym> notype_member_decl
290 %type <y_sym> type_member_decl
291 %type <y_tnode> constant
292 %type <y_sym> enum_declaration
293 %type <y_sym> enums_with_opt_comma
294 %type <y_sym> enums
295 %type <y_sym> enumerator
296 %type <y_sym> ename
297 %type <y_sym> notype_direct_decl
298 %type <y_sym> type_direct_decl
299 %type <y_pqinf> pointer
300 %type <y_pqinf> asterisk
301 %type <y_sym> param_decl
302 %type <y_sym> param_list
303 %type <y_sym> abs_decl_param_list
304 %type <y_sym> direct_param_decl
305 %type <y_sym> notype_param_decl
306 %type <y_sym> direct_notype_param_decl
307 %type <y_pqinf> type_qualifier_list
308 %type <y_pqinf> type_qualifier
309 %type <y_sym> identifier_list
310 %type <y_sym> abs_decl
311 %type <y_sym> direct_abs_decl
312 %type <y_sym> vararg_parameter_type_list
313 %type <y_sym> parameter_type_list
314 %type <y_sym> parameter_declaration
315 %type <y_tnode> expr
316 %type <y_tnode> expr_stmnt_val
317 %type <y_tnode> expr_stmnt_list
318 %type <y_tnode> term
319 %type <y_tnode> generic_expr
320 %type <y_tnode> func_arg_list
321 %type <y_op> point_or_arrow
322 %type <y_type> type_name
323 %type <y_sym> abstract_declaration
324 %type <y_tnode> do_while_expr
325 %type <y_tnode> opt_expr
326 %type <y_strg> string
327 %type <y_strg> string2
328 %type <y_sb> opt_asm_or_symbolrename
329 %type <y_range> range
330 %type <y_range> lorange
331
332
333 %%
334
335 program:
336 /* empty */ {
337 if (sflag) {
338 /* empty translation unit */
339 error(272);
340 } else if (!tflag) {
341 /* empty translation unit */
342 warning(272);
343 }
344 }
345 | translation_unit
346 ;
347
348 translation_unit:
349 ext_decl
350 | translation_unit ext_decl
351 ;
352
353 ext_decl:
354 asm_stmnt
355 | func_def {
356 global_clean_up_decl(0);
357 CLRWFLGS(__FILE__, __LINE__);
358 }
359 | data_def {
360 global_clean_up_decl(0);
361 CLRWFLGS(__FILE__, __LINE__);
362 }
363 ;
364
365 data_def:
366 T_SEMI {
367 if (sflag) {
368 /* syntax error: empty declaration */
369 error(0);
370 } else if (!tflag) {
371 /* syntax error: empty declaration */
372 warning(0);
373 }
374 }
375 | clrtyp deftyp notype_init_decls T_SEMI {
376 if (sflag) {
377 /* old style declaration; add "int" */
378 error(1);
379 } else if (!tflag) {
380 /* old style declaration; add "int" */
381 warning(1);
382 }
383 }
384 | declmods deftyp T_SEMI {
385 if (dcs->d_scl == TYPEDEF) {
386 /* typedef declares no type name */
387 warning(72);
388 } else {
389 /* empty declaration */
390 warning(2);
391 }
392 }
393 | declmods deftyp notype_init_decls T_SEMI
394 | declspecs deftyp T_SEMI {
395 if (dcs->d_scl == TYPEDEF) {
396 /* typedef declares no type name */
397 warning(72);
398 } else if (!dcs->d_nedecl) {
399 /* empty declaration */
400 warning(2);
401 }
402 }
403 | declspecs deftyp type_init_decls T_SEMI
404 | error T_SEMI {
405 global_clean_up();
406 }
407 | error T_RBRACE {
408 global_clean_up();
409 }
410 ;
411
412 func_def:
413 func_decl {
414 if ($1->s_type->t_tspec != FUNC) {
415 /* syntax error */
416 error(249, yytext);
417 YYERROR;
418 }
419 if ($1->s_type->t_typedef) {
420 /* ()-less function definition */
421 error(64);
422 YYERROR;
423 }
424 funcdef($1);
425 blklev++;
426 pushdecl(ARG);
427 if (lwarn == LWARN_NONE)
428 $1->s_used = 1;
429 } opt_arg_declaration_list {
430 popdecl();
431 blklev--;
432 cluparg();
433 pushctrl(0);
434 } comp_stmnt {
435 funcend();
436 popctrl(0);
437 }
438 ;
439
440 func_decl:
441 clrtyp deftyp notype_decl {
442 $$ = $3;
443 }
444 | declmods deftyp notype_decl {
445 $$ = $3;
446 }
447 | declspecs deftyp type_decl {
448 $$ = $3;
449 }
450 ;
451
452 opt_arg_declaration_list:
453 /* empty */
454 | arg_declaration_list
455 ;
456
457 arg_declaration_list:
458 arg_declaration
459 | arg_declaration_list arg_declaration
460 /* XXX or better "arg_declaration error" ? */
461 | error
462 ;
463
464 /*
465 * "arg_declaration" is separated from "declaration" because it
466 * needs other error handling.
467 */
468
469 arg_declaration:
470 declmods deftyp T_SEMI {
471 /* empty declaration */
472 warning(2);
473 }
474 | declmods deftyp notype_init_decls T_SEMI
475 | declspecs deftyp T_SEMI {
476 if (!dcs->d_nedecl) {
477 /* empty declaration */
478 warning(2);
479 } else {
480 tspec_t ts = dcs->d_type->t_tspec;
481 /* %s declared in argument declaration list */
482 warning(3, ts == STRUCT ? "struct" :
483 (ts == UNION ? "union" : "enum"));
484 }
485 }
486 | declspecs deftyp type_init_decls T_SEMI {
487 if (dcs->d_nedecl) {
488 tspec_t ts = dcs->d_type->t_tspec;
489 /* %s declared in argument declaration list */
490 warning(3, ts == STRUCT ? "struct" :
491 (ts == UNION ? "union" : "enum"));
492 }
493 }
494 | declmods error
495 | declspecs error
496 ;
497
498 declaration:
499 declmods deftyp T_SEMI {
500 if (dcs->d_scl == TYPEDEF) {
501 /* typedef declares no type name */
502 warning(72);
503 } else {
504 /* empty declaration */
505 warning(2);
506 }
507 }
508 | declmods deftyp notype_init_decls T_SEMI
509 | declspecs deftyp T_SEMI {
510 if (dcs->d_scl == TYPEDEF) {
511 /* typedef declares no type name */
512 warning(72);
513 } else if (!dcs->d_nedecl) {
514 /* empty declaration */
515 warning(2);
516 }
517 }
518 | declspecs deftyp type_init_decls T_SEMI
519 | error T_SEMI
520 ;
521
522 type_attribute_format_type:
523 T_AT_FORMAT_GNU_PRINTF
524 | T_AT_FORMAT_PRINTF
525 | T_AT_FORMAT_SCANF
526 | T_AT_FORMAT_STRFMON
527 | T_AT_FORMAT_STRFTIME
528 | T_AT_FORMAT_SYSLOG
529 ;
530
531 type_attribute_bounded_type:
532 T_AT_MINBYTES
533 | T_AT_STRING
534 | T_AT_BUFFER
535 ;
536
537
538 type_attribute_spec:
539 /* empty */
540 | T_AT_DEPRECATED T_LPARN string T_RPARN
541 | T_AT_DEPRECATED
542 | T_AT_ALIGNED T_LPARN constant T_RPARN
543 | T_AT_ALLOC_SIZE T_LPARN constant T_COMMA constant T_RPARN
544 | T_AT_ALLOC_SIZE T_LPARN constant T_RPARN
545 | T_AT_BOUNDED T_LPARN type_attribute_bounded_type
546 T_COMMA constant T_COMMA constant T_RPARN
547 | T_AT_SENTINEL T_LPARN constant T_RPARN
548 | T_AT_SENTINEL
549 | T_AT_FORMAT_ARG T_LPARN constant T_RPARN
550 | T_AT_NONNULL T_LPARN constant T_RPARN
551 | T_AT_MODE T_LPARN T_NAME T_RPARN
552 | T_AT_ALIAS T_LPARN string T_RPARN
553 | T_AT_OPTIMIZE T_LPARN string T_RPARN
554 | T_AT_PCS T_LPARN string T_RPARN
555 | T_AT_SECTION T_LPARN string T_RPARN
556 | T_AT_TLS_MODEL T_LPARN string T_RPARN
557 | T_AT_ALIGNED
558 | T_AT_CONSTRUCTOR
559 | T_AT_DESTRUCTOR
560 | T_AT_MALLOC
561 | T_AT_MAY_ALIAS
562 | T_AT_NO_INSTRUMENT_FUNCTION
563 | T_AT_NOINLINE
564 | T_AT_NORETURN
565 | T_AT_NOTHROW
566 | T_AT_COLD
567 | T_AT_RETURNS_TWICE
568 | T_AT_PACKED {
569 addpacked();
570 }
571 | T_AT_PURE
572 | T_AT_TUNION
573 | T_AT_GNU_INLINE
574 | T_AT_ALWAYS_INLINE
575 | T_AT_FORMAT T_LPARN type_attribute_format_type T_COMMA
576 constant T_COMMA constant T_RPARN
577 | T_AT_USED {
578 add_attr_used();
579 }
580 | T_AT_UNUSED {
581 add_attr_used();
582 }
583 | T_AT_WARN_UNUSED_RESULT
584 | T_AT_WEAK
585 | T_AT_VISIBILITY T_LPARN constant T_RPARN
586 | T_QUAL {
587 if ($1 != CONST)
588 yyerror("Bad attribute");
589 }
590 ;
591
592 type_attribute_spec_list:
593 type_attribute_spec
594 | type_attribute_spec_list T_COMMA type_attribute_spec
595 ;
596
597 type_attribute:
598 T_ATTRIBUTE T_LPARN T_LPARN {
599 attron = 1;
600 } type_attribute_spec_list {
601 attron = 0;
602 } T_RPARN T_RPARN
603 | T_PACKED {
604 addpacked();
605 }
606 | T_NORETURN {
607 }
608 ;
609
610 type_attribute_list:
611 type_attribute
612 | type_attribute_list type_attribute
613 ;
614
615 clrtyp:
616 /* empty */ {
617 clrtyp();
618 }
619 ;
620
621 deftyp:
622 /* empty */ {
623 deftyp();
624 }
625 ;
626
627 declspecs:
628 clrtyp_typespec {
629 add_type($1);
630 }
631 | declmods typespec {
632 add_type($2);
633 }
634 | type_attribute declspecs
635 | declspecs declmod
636 | declspecs notype_typespec {
637 add_type($2);
638 }
639 ;
640
641 declmods:
642 clrtyp T_QUAL {
643 add_qualifier($2);
644 }
645 | clrtyp T_SCLASS {
646 add_storage_class($2);
647 }
648 | declmods declmod
649 ;
650
651 declmod:
652 T_QUAL {
653 add_qualifier($1);
654 }
655 | T_SCLASS {
656 add_storage_class($1);
657 }
658 | type_attribute_list
659 ;
660
661 clrtyp_typespec:
662 clrtyp notype_typespec {
663 $$ = $2;
664 }
665 | T_TYPENAME clrtyp {
666 $$ = getsym($1)->s_type;
667 }
668 ;
669
670 typespec:
671 notype_typespec {
672 $$ = $1;
673 }
674 | T_TYPENAME {
675 $$ = getsym($1)->s_type;
676 }
677 ;
678
679 notype_typespec:
680 T_TYPE {
681 $$ = gettyp($1);
682 }
683 | T_TYPEOF term {
684 $$ = $2->tn_type;
685 }
686 | struct_spec {
687 popdecl();
688 $$ = $1;
689 }
690 | enum_spec {
691 popdecl();
692 $$ = $1;
693 }
694 ;
695
696 struct_spec:
697 struct struct_tag {
698 /*
699 * STDC requires that "struct a;" always introduces
700 * a new tag if "a" is not declared at current level
701 *
702 * yychar is valid because otherwise the parser would not
703 * have been able to decide if it must shift or reduce
704 */
705 $$ = mktag($2, $1, 0, yychar == T_SEMI);
706 }
707 | struct struct_tag {
708 dcs->d_tagtyp = mktag($2, $1, 1, 0);
709 } struct_declaration {
710 $$ = complete_tag_struct_or_union(dcs->d_tagtyp, $4);
711 }
712 | struct {
713 dcs->d_tagtyp = mktag(NULL, $1, 1, 0);
714 } struct_declaration {
715 $$ = complete_tag_struct_or_union(dcs->d_tagtyp, $3);
716 }
717 | struct error {
718 symtyp = FVFT;
719 $$ = gettyp(INT);
720 }
721 ;
722
723 struct:
724 struct type_attribute
725 | T_SOU {
726 symtyp = FTAG;
727 pushdecl($1 == STRUCT ? MOS : MOU);
728 dcs->d_offset = 0;
729 dcs->d_stralign = CHAR_BIT;
730 $$ = $1;
731 }
732 ;
733
734 struct_tag:
735 identifier {
736 $$ = getsym($1);
737 }
738 ;
739
740 struct_declaration:
741 struct_decl_lbrace member_declaration_list_with_rbrace {
742 $$ = $2;
743 }
744 ;
745
746 struct_decl_lbrace:
747 T_LBRACE {
748 symtyp = FVFT;
749 }
750 ;
751
752 member_declaration_list_with_rbrace:
753 member_declaration_list T_SEMI T_RBRACE {
754 $$ = $1;
755 }
756 | member_declaration_list T_RBRACE {
757 if (sflag) {
758 /* syntax req. ";" after last struct/union member */
759 error(66);
760 } else {
761 /* syntax req. ";" after last struct/union member */
762 warning(66);
763 }
764 $$ = $1;
765 }
766 | T_RBRACE {
767 $$ = NULL;
768 }
769 ;
770
771 opt_type_attribute:
772 /* empty */
773 | type_attribute
774 ;
775
776 member_declaration_list:
777 member_declaration {
778 $$ = $1;
779 }
780 | member_declaration_list T_SEMI member_declaration {
781 $$ = lnklst($1, $3);
782 }
783 ;
784
785 member_declaration:
786 noclass_declmods deftyp {
787 /* too late, i know, but getsym() compensates it */
788 symtyp = FMOS;
789 } notype_member_decls opt_type_attribute {
790 symtyp = FVFT;
791 $$ = $4;
792 }
793 | noclass_declspecs deftyp {
794 symtyp = FMOS;
795 } type_member_decls opt_type_attribute {
796 symtyp = FVFT;
797 $$ = $4;
798 }
799 | noclass_declmods deftyp opt_type_attribute {
800 symtyp = FVFT;
801 /* struct or union member must be named */
802 if (!Sflag)
803 warning(49);
804 /* add all the members of the anonymous struct/union */
805 $$ = dcs->d_type->t_str->memb;
806 anonymize($$);
807 }
808 | noclass_declspecs deftyp opt_type_attribute {
809 symtyp = FVFT;
810 /* struct or union member must be named */
811 if (!Sflag)
812 warning(49);
813 $$ = dcs->d_type->t_str->memb;
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 = FMOS;
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 = FMOS;
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(ENUMCON);
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 ename {
975 $$ = ename($1, enumval, 1);
976 }
977 | ename T_ASSIGN constant {
978 $$ = ename($1, toicon($3, 1), 0);
979 }
980 ;
981
982 ename:
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_LPARN type_decl T_RPARN {
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_LPARN type_decl T_RPARN {
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_LPARN notype_param_decl T_RPARN {
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_LPARN notype_param_decl T_RPARN {
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(
1169 merge_pointers_and_qualifiers($1, $2), $3);
1170 }
1171 ;
1172
1173 asterisk:
1174 T_MULT {
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 = 1;
1194 } else {
1195 $$->p_volatile = 1;
1196 }
1197 }
1198 ;
1199
1200 param_list:
1201 id_list_lparn identifier_list T_RPARN {
1202 $$ = $2;
1203 }
1204 | abs_decl_param_list {
1205 $$ = $1;
1206 }
1207 ;
1208
1209 id_list_lparn:
1210 T_LPARN {
1211 blklev++;
1212 pushdecl(PARG);
1213 }
1214 ;
1215
1216 identifier_list:
1217 T_NAME {
1218 $$ = old_style_function_name(getsym($1));
1219 }
1220 | identifier_list T_COMMA T_NAME {
1221 $$ = lnklst($1, old_style_function_name(getsym($3)));
1222 }
1223 | identifier_list error {
1224 $$ = $1;
1225 }
1226 ;
1227
1228 abs_decl_param_list:
1229 abs_decl_lparn T_RPARN {
1230 $$ = NULL;
1231 }
1232 | abs_decl_lparn vararg_parameter_type_list T_RPARN {
1233 dcs->d_proto = 1;
1234 $$ = $2;
1235 }
1236 | abs_decl_lparn error T_RPARN {
1237 $$ = NULL;
1238 }
1239 ;
1240
1241 abs_decl_lparn:
1242 T_LPARN {
1243 blklev++;
1244 pushdecl(PARG);
1245 }
1246 ;
1247
1248 vararg_parameter_type_list:
1249 parameter_type_list {
1250 $$ = $1;
1251 }
1252 | parameter_type_list T_COMMA T_ELLIPSE {
1253 dcs->d_vararg = 1;
1254 $$ = $1;
1255 }
1256 | T_ELLIPSE {
1257 if (sflag) {
1258 /* ANSI C requires formal parameter before "..." */
1259 error(84);
1260 } else if (!tflag) {
1261 /* ANSI C requires formal parameter before "..." */
1262 warning(84);
1263 }
1264 dcs->d_vararg = 1;
1265 $$ = NULL;
1266 }
1267 ;
1268
1269 parameter_type_list:
1270 parameter_declaration {
1271 $$ = $1;
1272 }
1273 | parameter_type_list T_COMMA parameter_declaration {
1274 $$ = lnklst($1, $3);
1275 }
1276 ;
1277
1278 parameter_declaration:
1279 declmods deftyp {
1280 $$ = decl1arg(abstract_name(), 0);
1281 }
1282 | declspecs deftyp {
1283 $$ = decl1arg(abstract_name(), 0);
1284 }
1285 | declmods deftyp notype_param_decl {
1286 $$ = decl1arg($3, 0);
1287 }
1288 /*
1289 * param_decl is needed because of following conflict:
1290 * "typedef int a; f(int (a));" could be parsed as
1291 * "function with argument a of type int", or
1292 * "function with an abstract argument of type function".
1293 * This grammar realizes the second case.
1294 */
1295 | declspecs deftyp param_decl {
1296 $$ = decl1arg($3, 0);
1297 }
1298 | declmods deftyp abs_decl {
1299 $$ = decl1arg($3, 0);
1300 }
1301 | declspecs deftyp abs_decl {
1302 $$ = decl1arg($3, 0);
1303 }
1304 ;
1305
1306 opt_asm_or_symbolrename: /* expect only one */
1307 /* empty */ {
1308 $$ = NULL;
1309 }
1310 | T_ASM T_LPARN T_STRING T_RPARN {
1311 freeyyv(&$3, T_STRING);
1312 $$ = NULL;
1313 }
1314 | T_SYMBOLRENAME T_LPARN T_NAME T_RPARN {
1315 $$ = $3;
1316 }
1317 ;
1318
1319 initializer:
1320 {
1321 cgram_debug("begin initializer");
1322 } init_assign_expr {
1323 cgram_debug("end initializer");
1324 }
1325 ;
1326
1327 init_assign_expr:
1328 | init_by_name init_base_expr %prec T_COMMA
1329 | init_base_expr
1330
1331 init_base_expr:
1332 expr %prec T_COMMA {
1333 mkinit($1);
1334 }
1335 | init_lbrace init_rbrace
1336 | init_lbrace init_expr_list init_rbrace
1337 | init_lbrace init_expr_list T_COMMA init_rbrace
1338 | error
1339 ;
1340
1341 init_expr_list:
1342 init_assign_expr %prec T_COMMA
1343 | init_expr_list T_COMMA init_assign_expr
1344 ;
1345
1346 lorange:
1347 constant T_ELLIPSE {
1348 $$.lo = toicon($1, 1);
1349 }
1350 ;
1351 range:
1352 constant {
1353 $$.lo = toicon($1, 1);
1354 $$.hi = $$.lo + 1;
1355 }
1356 | lorange constant {
1357 $$.lo = $1.lo;
1358 $$.hi = toicon($2, 1);
1359 }
1360 ;
1361
1362 init_field:
1363 T_LBRACK range T_RBRACK {
1364 if (!Sflag)
1365 warning(321);
1366 }
1367 | point identifier {
1368 if (!Sflag)
1369 warning(313);
1370 push_member($2);
1371 }
1372 ;
1373
1374 init_field_list:
1375 init_field
1376 | init_field_list init_field
1377 ;
1378
1379 init_by_name:
1380 init_field_list T_ASSIGN
1381 | identifier T_COLON {
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 abs_decl {
1416 $$ = declare_1_abstract($3);
1417 }
1418 | noclass_declspecs deftyp abs_decl {
1419 $$ = declare_1_abstract($3);
1420 }
1421 ;
1422
1423 abs_decl:
1424 pointer {
1425 $$ = add_pointer(abstract_name(), $1);
1426 }
1427 | direct_abs_decl {
1428 $$ = $1;
1429 }
1430 | pointer direct_abs_decl {
1431 $$ = add_pointer($2, $1);
1432 }
1433 | T_TYPEOF term {
1434 $$ = mktempsym($2->tn_type);
1435 }
1436 ;
1437
1438 direct_abs_decl:
1439 T_LPARN abs_decl T_RPARN {
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_abs_decl {
1449 $$ = $2;
1450 }
1451 | direct_abs_decl T_LBRACK T_RBRACK {
1452 $$ = add_array($1, 0, 0);
1453 }
1454 | direct_abs_decl T_LBRACK constant T_RBRACK {
1455 $$ = add_array($1, 1, toicon($3, 0));
1456 }
1457 | abs_decl_param_list opt_asm_or_symbolrename {
1458 $$ = add_function(symbolrename(abstract_name(), $2), $1);
1459 popdecl();
1460 blklev--;
1461 }
1462 | direct_abs_decl abs_decl_param_list opt_asm_or_symbolrename {
1463 $$ = add_function(symbolrename($1, $3), $2);
1464 popdecl();
1465 blklev--;
1466 }
1467 | direct_abs_decl type_attribute_list
1468 ;
1469
1470 non_expr_stmnt:
1471 labeled_stmnt
1472 | comp_stmnt
1473 | selection_stmnt
1474 | iteration_stmnt
1475 | jump_stmnt {
1476 ftflg = 0;
1477 }
1478 | asm_stmnt
1479
1480 stmnt:
1481 expr_stmnt
1482 | non_expr_stmnt
1483 ;
1484
1485 labeled_stmnt:
1486 label stmnt
1487 ;
1488
1489 label:
1490 T_NAME T_COLON {
1491 symtyp = FLAB;
1492 label(T_NAME, getsym($1), NULL);
1493 }
1494 | T_CASE constant T_COLON {
1495 label(T_CASE, NULL, $2);
1496 ftflg = 1;
1497 }
1498 | T_CASE constant T_ELLIPSE constant T_COLON {
1499 /* XXX: We don't fill all cases */
1500 label(T_CASE, NULL, $2);
1501 ftflg = 1;
1502 }
1503 | T_DEFAULT T_COLON {
1504 label(T_DEFAULT, NULL, NULL);
1505 ftflg = 1;
1506 }
1507 ;
1508
1509 stmnt_d_list:
1510 stmnt_list
1511 | stmnt_d_list declaration_list stmnt_list {
1512 if (!Sflag)
1513 c99ism(327);
1514 }
1515 ;
1516
1517 comp_stmnt:
1518 comp_stmnt_lbrace comp_stmnt_rbrace
1519 | comp_stmnt_lbrace stmnt_d_list comp_stmnt_rbrace
1520 | comp_stmnt_lbrace declaration_list comp_stmnt_rbrace
1521 | comp_stmnt_lbrace declaration_list stmnt_d_list comp_stmnt_rbrace
1522 ;
1523
1524 comp_stmnt_lbrace:
1525 T_LBRACE {
1526 blklev++;
1527 mblklev++;
1528 pushdecl(AUTO);
1529 }
1530 ;
1531
1532 comp_stmnt_rbrace:
1533 T_RBRACE {
1534 popdecl();
1535 freeblk();
1536 mblklev--;
1537 blklev--;
1538 ftflg = 0;
1539 }
1540 ;
1541
1542 stmnt_list:
1543 stmnt
1544 | stmnt_list stmnt {
1545 RESTORE(__FILE__, __LINE__);
1546 }
1547 | stmnt_list error T_SEMI
1548 ;
1549
1550 expr_stmnt:
1551 expr T_SEMI {
1552 expr($1, 0, 0, 0);
1553 ftflg = 0;
1554 }
1555 | T_SEMI {
1556 ftflg = 0;
1557 }
1558 ;
1559
1560 /*
1561 * The following two productions are used to implement
1562 * ({ [[decl-list] stmt-list] }).
1563 * XXX: This is not well tested.
1564 */
1565 expr_stmnt_val:
1566 expr T_SEMI {
1567 /* XXX: We should really do that only on the last name */
1568 if ($1->tn_op == NAME)
1569 $1->tn_sym->s_used = 1;
1570 $$ = $1;
1571 expr($1, 0, 0, 0);
1572 ftflg = 0;
1573 }
1574 | non_expr_stmnt {
1575 $$ = getnode();
1576 $$->tn_type = gettyp(VOID);
1577 }
1578 ;
1579
1580 expr_stmnt_list:
1581 expr_stmnt_val
1582 | expr_stmnt_list expr_stmnt_val {
1583 $$ = $2;
1584 }
1585 ;
1586
1587 selection_stmnt:
1588 if_without_else {
1589 SAVE(__FILE__, __LINE__);
1590 if2();
1591 if3(0);
1592 }
1593 | if_without_else T_ELSE {
1594 SAVE(__FILE__, __LINE__);
1595 if2();
1596 } stmnt {
1597 CLRWFLGS(__FILE__, __LINE__);
1598 if3(1);
1599 }
1600 | if_without_else T_ELSE error {
1601 CLRWFLGS(__FILE__, __LINE__);
1602 if3(0);
1603 }
1604 | switch_expr stmnt {
1605 CLRWFLGS(__FILE__, __LINE__);
1606 switch2();
1607 }
1608 | switch_expr error {
1609 CLRWFLGS(__FILE__, __LINE__);
1610 switch2();
1611 }
1612 ;
1613
1614 if_without_else:
1615 if_expr stmnt
1616 | if_expr error
1617 ;
1618
1619 if_expr:
1620 T_IF T_LPARN expr T_RPARN {
1621 if1($3);
1622 CLRWFLGS(__FILE__, __LINE__);
1623 }
1624 ;
1625
1626 switch_expr:
1627 T_SWITCH T_LPARN expr T_RPARN {
1628 switch1($3);
1629 CLRWFLGS(__FILE__, __LINE__);
1630 }
1631 ;
1632
1633 association:
1634 type_name T_COLON expr
1635 | T_DEFAULT T_COLON expr
1636 ;
1637
1638 association_list:
1639 association
1640 | association_list T_COMMA association
1641 ;
1642
1643 generic_expr:
1644 T_GENERIC T_LPARN expr T_COMMA association_list T_RPARN {
1645 $$ = $3;
1646 }
1647 ;
1648
1649 do_stmnt:
1650 do stmnt {
1651 CLRWFLGS(__FILE__, __LINE__);
1652 }
1653 ;
1654
1655 iteration_stmnt:
1656 while_expr stmnt {
1657 CLRWFLGS(__FILE__, __LINE__);
1658 while2();
1659 }
1660 | while_expr error {
1661 CLRWFLGS(__FILE__, __LINE__);
1662 while2();
1663 }
1664 | do_stmnt do_while_expr {
1665 do2($2);
1666 ftflg = 0;
1667 }
1668 | do error {
1669 CLRWFLGS(__FILE__, __LINE__);
1670 do2(NULL);
1671 }
1672 | for_exprs stmnt {
1673 CLRWFLGS(__FILE__, __LINE__);
1674 for2();
1675 popdecl();
1676 blklev--;
1677 }
1678 | for_exprs error {
1679 CLRWFLGS(__FILE__, __LINE__);
1680 for2();
1681 popdecl();
1682 blklev--;
1683 }
1684 ;
1685
1686 while_expr:
1687 T_WHILE T_LPARN expr T_RPARN {
1688 while1($3);
1689 CLRWFLGS(__FILE__, __LINE__);
1690 }
1691 ;
1692
1693 do:
1694 T_DO {
1695 do1();
1696 }
1697 ;
1698
1699 do_while_expr:
1700 T_WHILE T_LPARN expr T_RPARN T_SEMI {
1701 $$ = $3;
1702 }
1703 ;
1704
1705 for_start:
1706 T_FOR T_LPARN {
1707 pushdecl(AUTO);
1708 blklev++;
1709 }
1710 ;
1711 for_exprs:
1712 for_start declspecs deftyp notype_init_decls T_SEMI opt_expr
1713 T_SEMI opt_expr T_RPARN {
1714 c99ism(325);
1715 for1(NULL, $6, $8);
1716 CLRWFLGS(__FILE__, __LINE__);
1717 }
1718 | for_start opt_expr T_SEMI opt_expr T_SEMI opt_expr T_RPARN {
1719 for1($2, $4, $6);
1720 CLRWFLGS(__FILE__, __LINE__);
1721 }
1722 ;
1723
1724 opt_expr:
1725 /* empty */ {
1726 $$ = NULL;
1727 }
1728 | expr {
1729 $$ = $1;
1730 }
1731 ;
1732
1733 jump_stmnt:
1734 goto identifier T_SEMI {
1735 dogoto(getsym($2));
1736 }
1737 | goto error T_SEMI {
1738 symtyp = FVFT;
1739 }
1740 | T_CONTINUE T_SEMI {
1741 docont();
1742 }
1743 | T_BREAK T_SEMI {
1744 dobreak();
1745 }
1746 | T_RETURN T_SEMI {
1747 doreturn(NULL);
1748 }
1749 | T_RETURN expr T_SEMI {
1750 doreturn($2);
1751 }
1752 ;
1753
1754 goto:
1755 T_GOTO {
1756 symtyp = FLAB;
1757 }
1758 ;
1759
1760 asm_stmnt:
1761 T_ASM T_LPARN read_until_rparn T_SEMI {
1762 setasm();
1763 }
1764 | T_ASM T_QUAL T_LPARN read_until_rparn T_SEMI {
1765 setasm();
1766 }
1767 | T_ASM error
1768 ;
1769
1770 read_until_rparn:
1771 /* empty */ {
1772 ignuptorp();
1773 }
1774 ;
1775
1776 declaration_list:
1777 declaration {
1778 CLRWFLGS(__FILE__, __LINE__);
1779 }
1780 | declaration_list declaration {
1781 CLRWFLGS(__FILE__, __LINE__);
1782 }
1783 ;
1784
1785 constant:
1786 expr %prec T_COMMA {
1787 $$ = $1;
1788 }
1789 ;
1790
1791 expr:
1792 expr T_MULT expr {
1793 $$ = build(MULT, $1, $3);
1794 }
1795 | expr T_DIVOP expr {
1796 $$ = build($2, $1, $3);
1797 }
1798 | expr T_ADDOP expr {
1799 $$ = build($2, $1, $3);
1800 }
1801 | expr T_SHFTOP expr {
1802 $$ = build($2, $1, $3);
1803 }
1804 | expr T_RELOP expr {
1805 $$ = build($2, $1, $3);
1806 }
1807 | expr T_EQOP expr {
1808 $$ = build($2, $1, $3);
1809 }
1810 | expr T_AND expr {
1811 $$ = build(AND, $1, $3);
1812 }
1813 | expr T_XOR expr {
1814 $$ = build(XOR, $1, $3);
1815 }
1816 | expr T_OR expr {
1817 $$ = build(OR, $1, $3);
1818 }
1819 | expr T_LOGAND expr {
1820 $$ = build(LOGAND, $1, $3);
1821 }
1822 | expr T_LOGOR expr {
1823 $$ = build(LOGOR, $1, $3);
1824 }
1825 | expr T_QUEST expr T_COLON expr {
1826 $$ = build(QUEST, $1, build(COLON, $3, $5));
1827 }
1828 | expr T_ASSIGN expr {
1829 $$ = build(ASSIGN, $1, $3);
1830 }
1831 | expr T_OPASS expr {
1832 $$ = build($2, $1, $3);
1833 }
1834 | expr T_COMMA expr {
1835 $$ = build(COMMA, $1, $3);
1836 }
1837 | term {
1838 $$ = $1;
1839 }
1840 | generic_expr {
1841 $$ = $1;
1842 }
1843 ;
1844
1845 term:
1846 T_NAME {
1847 /* XXX really necessary? */
1848 if (yychar < 0)
1849 yychar = yylex();
1850 $$ = getnnode(getsym($1), yychar);
1851 }
1852 | string {
1853 $$ = getsnode($1);
1854 }
1855 | T_CON {
1856 $$ = getcnode(gettyp($1->v_tspec), $1);
1857 }
1858 | T_LPARN expr T_RPARN {
1859 if ($2 != NULL)
1860 $2->tn_parn = 1;
1861 $$ = $2;
1862 }
1863 | T_LPARN comp_stmnt_lbrace declaration_list expr_stmnt_list {
1864 blklev--;
1865 mblklev--;
1866 initsym = mktempsym(duptyp($4->tn_type));
1867 mblklev++;
1868 blklev++;
1869 gnuism(320);
1870 } comp_stmnt_rbrace T_RPARN {
1871 $$ = getnnode(initsym, 0);
1872 }
1873 | T_LPARN comp_stmnt_lbrace expr_stmnt_list {
1874 blklev--;
1875 mblklev--;
1876 initsym = mktempsym($3->tn_type);
1877 mblklev++;
1878 blklev++;
1879 gnuism(320);
1880 } comp_stmnt_rbrace T_RPARN {
1881 $$ = getnnode(initsym, 0);
1882 }
1883 | term T_INCDEC {
1884 $$ = build($2 == INC ? INCAFT : DECAFT, $1, NULL);
1885 }
1886 | T_INCDEC term {
1887 $$ = build($1 == INC ? INCBEF : DECBEF, $2, NULL);
1888 }
1889 | T_MULT term {
1890 $$ = build(STAR, $2, NULL);
1891 }
1892 | T_AND term {
1893 $$ = build(AMPER, $2, NULL);
1894 }
1895 | T_UNOP term {
1896 $$ = build($1, $2, NULL);
1897 }
1898 | T_ADDOP term {
1899 if (tflag && $1 == PLUS) {
1900 /* unary + is illegal in traditional C */
1901 warning(100);
1902 }
1903 $$ = build($1 == PLUS ? UPLUS : UMINUS, $2, NULL);
1904 }
1905 | term T_LBRACK expr T_RBRACK {
1906 $$ = build(STAR, build(PLUS, $1, $3), NULL);
1907 }
1908 | term T_LPARN T_RPARN {
1909 $$ = funccall($1, NULL);
1910 }
1911 | term T_LPARN func_arg_list T_RPARN {
1912 $$ = funccall($1, $3);
1913 }
1914 | term point_or_arrow T_NAME {
1915 if ($1 != NULL) {
1916 sym_t *msym;
1917 /*
1918 * XXX struct_or_union_member should be integrated
1919 * in build()
1920 */
1921 if ($2 == ARROW) {
1922 /*
1923 * must do this before struct_or_union_member
1924 * is called
1925 */
1926 $1 = cconv($1);
1927 }
1928 msym = struct_or_union_member($1, $2, getsym($3));
1929 $$ = build($2, $1, getnnode(msym, 0));
1930 } else {
1931 $$ = NULL;
1932 }
1933 }
1934 | T_REAL term {
1935 $$ = build(REAL, $2, NULL);
1936 }
1937 | T_IMAG term {
1938 $$ = build(IMAG, $2, NULL);
1939 }
1940 | T_EXTENSION term {
1941 $$ = $2;
1942 }
1943 | T_REAL T_LPARN term T_RPARN {
1944 $$ = build(REAL, $3, NULL);
1945 }
1946 | T_IMAG T_LPARN term T_RPARN {
1947 $$ = build(IMAG, $3, NULL);
1948 }
1949 | T_BUILTIN_OFFSETOF T_LPARN type_name T_COMMA identifier T_RPARN
1950 %prec T_BUILTIN_OFFSETOF {
1951 symtyp = FMOS;
1952 $$ = build_offsetof($3, getsym($5));
1953 }
1954 | T_SIZEOF term %prec T_SIZEOF {
1955 $$ = $2 == NULL ? NULL : build_sizeof($2->tn_type);
1956 if ($$ != NULL)
1957 check_expr_misc($2, 0, 0, 0, 0, 0, 1);
1958 }
1959 | T_SIZEOF T_LPARN type_name T_RPARN %prec T_SIZEOF {
1960 $$ = build_sizeof($3);
1961 }
1962 | T_ALIGNOF T_LPARN type_name T_RPARN %prec T_ALIGNOF {
1963 $$ = build_alignof($3);
1964 }
1965 | T_LPARN type_name T_RPARN term %prec T_UNOP {
1966 $$ = cast($4, $2);
1967 }
1968 | T_LPARN type_name T_RPARN %prec T_UNOP {
1969 sym_t *tmp = mktempsym($2);
1970 idecl(tmp, 1, NULL);
1971 } init_lbrace init_expr_list init_rbrace {
1972 if (!Sflag)
1973 gnuism(319);
1974 $$ = getnnode(initsym, 0);
1975 }
1976 ;
1977
1978 string:
1979 T_STRING {
1980 $$ = $1;
1981 }
1982 | T_STRING string2 {
1983 $$ = cat_strings($1, $2);
1984 }
1985 ;
1986
1987 string2:
1988 T_STRING {
1989 if (tflag) {
1990 /* concatenated strings are illegal in traditional C */
1991 warning(219);
1992 }
1993 $$ = $1;
1994 }
1995 | string2 T_STRING {
1996 $$ = cat_strings($1, $2);
1997 }
1998 ;
1999
2000 func_arg_list:
2001 expr %prec T_COMMA {
2002 $$ = funcarg(NULL, $1);
2003 }
2004 | func_arg_list T_COMMA expr {
2005 $$ = funcarg($1, $3);
2006 }
2007 ;
2008
2009 point_or_arrow:
2010 T_STROP {
2011 symtyp = FMOS;
2012 $$ = $1;
2013 }
2014 ;
2015
2016 point:
2017 T_STROP {
2018 if ($1 != POINT) {
2019 error(249, yytext);
2020 }
2021 }
2022 ;
2023
2024 identifier:
2025 T_NAME {
2026 $$ = $1;
2027 cgram_debug("name '%s'", $$->sb_name);
2028 }
2029 | T_TYPENAME {
2030 $$ = $1;
2031 cgram_debug("typename '%s'", $$->sb_name);
2032 }
2033 ;
2034
2035 %%
2036
2037 /* ARGSUSED */
2038 int
2039 yyerror(const char *msg)
2040 {
2041 error(249, yytext);
2042 if (++sytxerr >= 5)
2043 norecover();
2044 return 0;
2045 }
2046
2047 static __inline int uq_gt(uint64_t, uint64_t);
2048 static __inline int q_gt(int64_t, int64_t);
2049
2050 static __inline int
2051 uq_gt(uint64_t a, uint64_t b)
2052 {
2053
2054 return a > b;
2055 }
2056
2057 static __inline int
2058 q_gt(int64_t a, int64_t b)
2059 {
2060
2061 return a > b;
2062 }
2063
2064 #define q_lt(a, b) q_gt(b, a)
2065
2066 /*
2067 * Gets a node for a constant and returns the value of this constant
2068 * as integer.
2069 * Is the node not constant or too large for int or of type float,
2070 * a warning will be printed.
2071 *
2072 * toicon() should be used only inside declarations. If it is used in
2073 * expressions, it frees the memory used for the expression.
2074 */
2075 static int
2076 toicon(tnode_t *tn, int required)
2077 {
2078 int i;
2079 tspec_t t;
2080 val_t *v;
2081
2082 v = constant(tn, required);
2083
2084 /*
2085 * Abstract declarations are used inside expression. To free
2086 * the memory would be a fatal error.
2087 * We don't free blocks that are inside casts because these
2088 * will be used later to match types.
2089 */
2090 if (tn->tn_op != CON && dcs->d_ctx != ABSTRACT)
2091 tfreeblk();
2092
2093 if ((t = v->v_tspec) == FLOAT || t == DOUBLE || t == LDOUBLE) {
2094 i = (int)v->v_ldbl;
2095 /* integral constant expression expected */
2096 error(55);
2097 } else {
2098 i = (int)v->v_quad;
2099 if (tspec_is_uint(t)) {
2100 if (uq_gt((uint64_t)v->v_quad,
2101 (uint64_t)TARG_INT_MAX)) {
2102 /* integral constant too large */
2103 warning(56);
2104 }
2105 } else {
2106 if (q_gt(v->v_quad, (int64_t)TARG_INT_MAX) ||
2107 q_lt(v->v_quad, (int64_t)TARG_INT_MIN)) {
2108 /* integral constant too large */
2109 warning(56);
2110 }
2111 }
2112 }
2113 free(v);
2114 return i;
2115 }
2116
2117 static void
2118 idecl(sym_t *decl, int initflg, sbuf_t *renaming)
2119 {
2120 char *s;
2121
2122 initerr = 0;
2123 initsym = decl;
2124
2125 switch (dcs->d_ctx) {
2126 case EXTERN:
2127 if (renaming != NULL) {
2128 if (decl->s_rename != NULL)
2129 LERROR("idecl(rename)");
2130
2131 s = getlblk(1, renaming->sb_len + 1);
2132 (void)memcpy(s, renaming->sb_name, renaming->sb_len + 1);
2133 decl->s_rename = s;
2134 freeyyv(&renaming, T_NAME);
2135 }
2136 decl1ext(decl, initflg);
2137 break;
2138 case ARG:
2139 if (renaming != NULL) {
2140 /* symbol renaming can't be used on function arguments */
2141 error(310);
2142 freeyyv(&renaming, T_NAME);
2143 break;
2144 }
2145 (void)decl1arg(decl, initflg);
2146 break;
2147 case AUTO:
2148 if (renaming != NULL) {
2149 /* symbol renaming can't be used on automatic variables */
2150 error(311);
2151 freeyyv(&renaming, T_NAME);
2152 break;
2153 }
2154 decl1loc(decl, initflg);
2155 break;
2156 default:
2157 LERROR("idecl(%d)", dcs->d_ctx);
2158 }
2159
2160 if (initflg && !initerr)
2161 initstack_init();
2162 }
2163
2164 /*
2165 * Discard all input tokens up to and including the next
2166 * unmatched right paren
2167 */
2168 static void
2169 ignuptorp(void)
2170 {
2171 int level;
2172
2173 if (yychar < 0)
2174 yychar = yylex();
2175 freeyyv(&yylval, yychar);
2176
2177 level = 1;
2178 while (yychar != T_RPARN || --level > 0) {
2179 if (yychar == T_LPARN) {
2180 level++;
2181 } else if (yychar <= 0) {
2182 break;
2183 }
2184 freeyyv(&yylval, yychar = yylex());
2185 }
2186
2187 yyclearin;
2188 }
2189
2190 static sym_t *
2191 symbolrename(sym_t *s, sbuf_t *sb)
2192 {
2193 if (sb)
2194 s->s_rename = sb->sb_name;
2195 return s;
2196 }
2197