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