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