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