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