cgram.y revision 1.71 1 %{
2 /* $NetBSD: cgram.y,v 1.71 2015/07/28 17:56:56 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.71 2015/07/28 17:56:56 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 80
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 identifier {
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_field:
1245 T_LBRACK range T_RBRACK {
1246 if (!Sflag)
1247 warning(321);
1248 }
1249 | point identifier {
1250 if (!Sflag)
1251 warning(313);
1252 memberpush($2);
1253 }
1254 ;
1255
1256 init_field_list:
1257 init_field
1258 | init_field_list init_field
1259 ;
1260
1261 init_by_name:
1262 init_field_list T_ASSIGN
1263 | identifier T_COLON {
1264 gnuism(315);
1265 memberpush($1);
1266 }
1267 ;
1268
1269 init_lbrace:
1270 T_LBRACE {
1271 initlbr();
1272 }
1273 ;
1274
1275 init_rbrace:
1276 T_RBRACE {
1277 initrbr();
1278 }
1279 ;
1280
1281 type_name:
1282 {
1283 pushdecl(ABSTRACT);
1284 } abstract_declaration {
1285 popdecl();
1286 $$ = $2->s_type;
1287 }
1288 ;
1289
1290 abstract_declaration:
1291 noclass_declmods deftyp {
1292 $$ = decl1abs(aname());
1293 }
1294 | noclass_declspecs deftyp {
1295 $$ = decl1abs(aname());
1296 }
1297 | noclass_declmods deftyp abs_decl {
1298 $$ = decl1abs($3);
1299 }
1300 | noclass_declspecs deftyp abs_decl {
1301 $$ = decl1abs($3);
1302 }
1303 ;
1304
1305 abs_decl:
1306 pointer {
1307 $$ = addptr(aname(), $1);
1308 }
1309 | direct_abs_decl {
1310 $$ = $1;
1311 }
1312 | pointer direct_abs_decl {
1313 $$ = addptr($2, $1);
1314 }
1315 ;
1316
1317 direct_abs_decl:
1318 T_LPARN abs_decl T_RPARN {
1319 $$ = $2;
1320 }
1321 | T_LBRACK T_RBRACK {
1322 $$ = addarray(aname(), 0, 0);
1323 }
1324 | T_LBRACK constant T_RBRACK {
1325 $$ = addarray(aname(), 1, toicon($2, 0));
1326 }
1327 | type_attribute direct_abs_decl {
1328 $$ = $2;
1329 }
1330 | direct_abs_decl T_LBRACK T_RBRACK {
1331 $$ = addarray($1, 0, 0);
1332 }
1333 | direct_abs_decl T_LBRACK constant T_RBRACK {
1334 $$ = addarray($1, 1, toicon($3, 0));
1335 }
1336 | abs_decl_param_list opt_asm_or_symbolrename {
1337 $$ = addfunc(aname(), $1);
1338 popdecl();
1339 blklev--;
1340 }
1341 | direct_abs_decl abs_decl_param_list opt_asm_or_symbolrename {
1342 $$ = addfunc($1, $2);
1343 popdecl();
1344 blklev--;
1345 }
1346 | direct_abs_decl type_attribute
1347 ;
1348
1349 non_expr_stmnt:
1350 labeled_stmnt
1351 | comp_stmnt
1352 | selection_stmnt
1353 | iteration_stmnt
1354 | jump_stmnt {
1355 ftflg = 0;
1356 }
1357 | asm_stmnt
1358
1359 stmnt:
1360 expr_stmnt
1361 | non_expr_stmnt
1362 ;
1363
1364 labeled_stmnt:
1365 label stmnt
1366 ;
1367
1368 label:
1369 T_NAME T_COLON {
1370 symtyp = FLAB;
1371 label(T_NAME, getsym($1), NULL);
1372 }
1373 | T_CASE constant T_COLON {
1374 label(T_CASE, NULL, $2);
1375 ftflg = 1;
1376 }
1377 | T_CASE constant T_ELLIPSE constant T_COLON {
1378 /* XXX: We don't fill all cases */
1379 label(T_CASE, NULL, $2);
1380 ftflg = 1;
1381 }
1382 | T_DEFAULT T_COLON {
1383 label(T_DEFAULT, NULL, NULL);
1384 ftflg = 1;
1385 }
1386 ;
1387
1388 stmnt_d_list:
1389 stmnt_list
1390 | stmnt_d_list declaration_list stmnt_list {
1391 if (!Sflag)
1392 c99ism(327);
1393 }
1394 ;
1395
1396 comp_stmnt:
1397 comp_stmnt_lbrace comp_stmnt_rbrace
1398 | comp_stmnt_lbrace stmnt_d_list comp_stmnt_rbrace
1399 | comp_stmnt_lbrace declaration_list comp_stmnt_rbrace
1400 | comp_stmnt_lbrace declaration_list stmnt_d_list comp_stmnt_rbrace
1401 ;
1402
1403 comp_stmnt_lbrace:
1404 T_LBRACE {
1405 blklev++;
1406 mblklev++;
1407 pushdecl(AUTO);
1408 }
1409 ;
1410
1411 comp_stmnt_rbrace:
1412 T_RBRACE {
1413 popdecl();
1414 freeblk();
1415 mblklev--;
1416 blklev--;
1417 ftflg = 0;
1418 }
1419 ;
1420
1421 stmnt_list:
1422 stmnt
1423 | stmnt_list stmnt {
1424 RESTORE(__FILE__, __LINE__);
1425 }
1426 | stmnt_list error T_SEMI
1427 ;
1428
1429 expr_stmnt:
1430 expr T_SEMI {
1431 expr($1, 0, 0, 1);
1432 ftflg = 0;
1433 }
1434 | T_SEMI {
1435 ftflg = 0;
1436 }
1437 ;
1438
1439 /*
1440 * The following two productions are used to implement
1441 * ({ [[decl-list] stmt-list] }).
1442 * XXX: This is not well tested.
1443 */
1444 expr_stmnt_val:
1445 expr T_SEMI {
1446 /* XXX: We should really do that only on the last name */
1447 if ($1->tn_op == NAME)
1448 $1->tn_sym->s_used = 1;
1449 $$ = $1;
1450 expr($1, 0, 0, 0);
1451 ftflg = 0;
1452 }
1453 | non_expr_stmnt {
1454 $$ = getnode();
1455 $$->tn_type = gettyp(VOID);
1456 }
1457 ;
1458
1459 expr_stmnt_list:
1460 expr_stmnt_val
1461 | expr_stmnt_list expr_stmnt_val {
1462 $$ = $2;
1463 }
1464 ;
1465
1466 selection_stmnt:
1467 if_without_else {
1468 SAVE(__FILE__, __LINE__);
1469 if2();
1470 if3(0);
1471 }
1472 | if_without_else T_ELSE {
1473 SAVE(__FILE__, __LINE__);
1474 if2();
1475 } stmnt {
1476 CLRWFLGS(__FILE__, __LINE__);
1477 if3(1);
1478 }
1479 | if_without_else T_ELSE error {
1480 CLRWFLGS(__FILE__, __LINE__);
1481 if3(0);
1482 }
1483 | switch_expr stmnt {
1484 CLRWFLGS(__FILE__, __LINE__);
1485 switch2();
1486 }
1487 | switch_expr error {
1488 CLRWFLGS(__FILE__, __LINE__);
1489 switch2();
1490 }
1491 ;
1492
1493 if_without_else:
1494 if_expr stmnt
1495 | if_expr error
1496 ;
1497
1498 if_expr:
1499 T_IF T_LPARN expr T_RPARN {
1500 if1($3);
1501 CLRWFLGS(__FILE__, __LINE__);
1502 }
1503 ;
1504
1505 switch_expr:
1506 T_SWITCH T_LPARN expr T_RPARN {
1507 switch1($3);
1508 CLRWFLGS(__FILE__, __LINE__);
1509 }
1510 ;
1511
1512 do_stmnt:
1513 do stmnt {
1514 CLRWFLGS(__FILE__, __LINE__);
1515 }
1516 ;
1517
1518 iteration_stmnt:
1519 while_expr stmnt {
1520 CLRWFLGS(__FILE__, __LINE__);
1521 while2();
1522 }
1523 | while_expr error {
1524 CLRWFLGS(__FILE__, __LINE__);
1525 while2();
1526 }
1527 | do_stmnt do_while_expr {
1528 do2($2);
1529 ftflg = 0;
1530 }
1531 | do error {
1532 CLRWFLGS(__FILE__, __LINE__);
1533 do2(NULL);
1534 }
1535 | for_exprs stmnt {
1536 CLRWFLGS(__FILE__, __LINE__);
1537 for2();
1538 popdecl();
1539 blklev--;
1540 }
1541 | for_exprs error {
1542 CLRWFLGS(__FILE__, __LINE__);
1543 for2();
1544 popdecl();
1545 blklev--;
1546 }
1547 ;
1548
1549 while_expr:
1550 T_WHILE T_LPARN expr T_RPARN {
1551 while1($3);
1552 CLRWFLGS(__FILE__, __LINE__);
1553 }
1554 ;
1555
1556 do:
1557 T_DO {
1558 do1();
1559 }
1560 ;
1561
1562 do_while_expr:
1563 T_WHILE T_LPARN expr T_RPARN T_SEMI {
1564 $$ = $3;
1565 }
1566 ;
1567
1568 for_start:
1569 T_FOR T_LPARN {
1570 pushdecl(AUTO);
1571 blklev++;
1572 }
1573 ;
1574 for_exprs:
1575 for_start declspecs deftyp notype_init_decls T_SEMI opt_expr
1576 T_SEMI opt_expr T_RPARN {
1577 c99ism(325);
1578 for1(NULL, $6, $8);
1579 CLRWFLGS(__FILE__, __LINE__);
1580 }
1581 | for_start opt_expr T_SEMI opt_expr T_SEMI opt_expr T_RPARN {
1582 for1($2, $4, $6);
1583 CLRWFLGS(__FILE__, __LINE__);
1584 }
1585 ;
1586
1587 opt_expr:
1588 /* empty */ {
1589 $$ = NULL;
1590 }
1591 | expr {
1592 $$ = $1;
1593 }
1594 ;
1595
1596 jump_stmnt:
1597 goto identifier T_SEMI {
1598 dogoto(getsym($2));
1599 }
1600 | goto error T_SEMI {
1601 symtyp = FVFT;
1602 }
1603 | T_CONTINUE T_SEMI {
1604 docont();
1605 }
1606 | T_BREAK T_SEMI {
1607 dobreak();
1608 }
1609 | T_RETURN T_SEMI {
1610 doreturn(NULL);
1611 }
1612 | T_RETURN expr T_SEMI {
1613 doreturn($2);
1614 }
1615 ;
1616
1617 goto:
1618 T_GOTO {
1619 symtyp = FLAB;
1620 }
1621 ;
1622
1623 asm_stmnt:
1624 T_ASM T_LPARN read_until_rparn T_SEMI {
1625 setasm();
1626 }
1627 | T_ASM T_QUAL T_LPARN read_until_rparn T_SEMI {
1628 setasm();
1629 }
1630 | T_ASM error
1631 ;
1632
1633 read_until_rparn:
1634 /* empty */ {
1635 ignuptorp();
1636 }
1637 ;
1638
1639 declaration_list:
1640 declaration {
1641 CLRWFLGS(__FILE__, __LINE__);
1642 }
1643 | declaration_list declaration {
1644 CLRWFLGS(__FILE__, __LINE__);
1645 }
1646 ;
1647
1648 constant:
1649 expr %prec T_COMMA {
1650 $$ = $1;
1651 }
1652 ;
1653
1654 expr:
1655 expr T_MULT expr {
1656 $$ = build(MULT, $1, $3);
1657 }
1658 | expr T_DIVOP expr {
1659 $$ = build($2, $1, $3);
1660 }
1661 | expr T_ADDOP expr {
1662 $$ = build($2, $1, $3);
1663 }
1664 | expr T_SHFTOP expr {
1665 $$ = build($2, $1, $3);
1666 }
1667 | expr T_RELOP expr {
1668 $$ = build($2, $1, $3);
1669 }
1670 | expr T_EQOP expr {
1671 $$ = build($2, $1, $3);
1672 }
1673 | expr T_AND expr {
1674 $$ = build(AND, $1, $3);
1675 }
1676 | expr T_XOR expr {
1677 $$ = build(XOR, $1, $3);
1678 }
1679 | expr T_OR expr {
1680 $$ = build(OR, $1, $3);
1681 }
1682 | expr T_LOGAND expr {
1683 $$ = build(LOGAND, $1, $3);
1684 }
1685 | expr T_LOGOR expr {
1686 $$ = build(LOGOR, $1, $3);
1687 }
1688 | expr T_QUEST expr T_COLON expr {
1689 $$ = build(QUEST, $1, build(COLON, $3, $5));
1690 }
1691 | expr T_ASSIGN expr {
1692 $$ = build(ASSIGN, $1, $3);
1693 }
1694 | expr T_OPASS expr {
1695 $$ = build($2, $1, $3);
1696 }
1697 | expr T_COMMA expr {
1698 $$ = build(COMMA, $1, $3);
1699 }
1700 | term {
1701 $$ = $1;
1702 }
1703 ;
1704
1705 term:
1706 T_NAME {
1707 /* XXX really necessary? */
1708 if (yychar < 0)
1709 yychar = yylex();
1710 $$ = getnnode(getsym($1), yychar);
1711 }
1712 | string {
1713 $$ = getsnode($1);
1714 }
1715 | T_CON {
1716 $$ = getcnode(gettyp($1->v_tspec), $1);
1717 }
1718 | T_LPARN expr T_RPARN {
1719 if ($2 != NULL)
1720 $2->tn_parn = 1;
1721 $$ = $2;
1722 }
1723 | T_LPARN comp_stmnt_lbrace declaration_list expr_stmnt_list {
1724 blklev--;
1725 mblklev--;
1726 initsym = mktempsym(duptyp($4->tn_type));
1727 mblklev++;
1728 blklev++;
1729 gnuism(320);
1730 } comp_stmnt_rbrace T_RPARN {
1731 $$ = getnnode(initsym, 0);
1732 }
1733 | T_LPARN comp_stmnt_lbrace expr_stmnt_list {
1734 blklev--;
1735 mblklev--;
1736 initsym = mktempsym($3->tn_type);
1737 mblklev++;
1738 blklev++;
1739 gnuism(320);
1740 } comp_stmnt_rbrace T_RPARN {
1741 $$ = getnnode(initsym, 0);
1742 }
1743 | term T_INCDEC {
1744 $$ = build($2 == INC ? INCAFT : DECAFT, $1, NULL);
1745 }
1746 | T_INCDEC term {
1747 $$ = build($1 == INC ? INCBEF : DECBEF, $2, NULL);
1748 }
1749 | T_MULT term {
1750 $$ = build(STAR, $2, NULL);
1751 }
1752 | T_AND term {
1753 $$ = build(AMPER, $2, NULL);
1754 }
1755 | T_UNOP term {
1756 $$ = build($1, $2, NULL);
1757 }
1758 | T_ADDOP term {
1759 if (tflag && $1 == PLUS) {
1760 /* unary + is illegal in traditional C */
1761 warning(100);
1762 }
1763 $$ = build($1 == PLUS ? UPLUS : UMINUS, $2, NULL);
1764 }
1765 | term T_LBRACK expr T_RBRACK {
1766 $$ = build(STAR, build(PLUS, $1, $3), NULL);
1767 }
1768 | term T_LPARN T_RPARN {
1769 $$ = funccall($1, NULL);
1770 }
1771 | term T_LPARN func_arg_list T_RPARN {
1772 $$ = funccall($1, $3);
1773 }
1774 | term point_or_arrow T_NAME {
1775 if ($1 != NULL) {
1776 sym_t *msym;
1777 /* XXX strmemb should be integrated in build() */
1778 if ($2 == ARROW) {
1779 /* must to this before strmemb is called */
1780 $1 = cconv($1);
1781 }
1782 msym = strmemb($1, $2, getsym($3));
1783 $$ = build($2, $1, getnnode(msym, 0));
1784 } else {
1785 $$ = NULL;
1786 }
1787 }
1788 | T_REAL term {
1789 $$ = build(REAL, $2, NULL);
1790 }
1791 | T_IMAG term {
1792 $$ = build(IMAG, $2, NULL);
1793 }
1794 | T_EXTENSION term {
1795 $$ = $2;
1796 }
1797 | T_REAL T_LPARN term T_RPARN {
1798 $$ = build(REAL, $3, NULL);
1799 }
1800 | T_IMAG T_LPARN term T_RPARN {
1801 $$ = build(IMAG, $3, NULL);
1802 }
1803 | T_SIZEOF term %prec T_SIZEOF {
1804 if (($$ = $2 == NULL ? NULL : bldszof($2->tn_type)) != NULL)
1805 chkmisc($2, 0, 0, 0, 0, 0, 1);
1806 }
1807 | T_SIZEOF T_LPARN type_name T_RPARN %prec T_SIZEOF {
1808 $$ = bldszof($3);
1809 }
1810 | T_ALIGNOF T_LPARN type_name T_RPARN %prec T_ALIGNOF {
1811 $$ = bldalof($3);
1812 }
1813 | T_LPARN type_name T_RPARN term %prec T_UNOP {
1814 $$ = cast($4, $2);
1815 }
1816 | T_LPARN type_name T_RPARN %prec T_UNOP {
1817 sym_t *tmp = mktempsym($2);
1818 idecl(tmp, 1, NULL);
1819 } init_lbrace init_expr_list init_rbrace {
1820 if (!Sflag)
1821 gnuism(319);
1822 $$ = getnnode(initsym, 0);
1823 }
1824 ;
1825
1826 string:
1827 T_STRING {
1828 $$ = $1;
1829 }
1830 | T_STRING string2 {
1831 $$ = catstrg($1, $2);
1832 }
1833 ;
1834
1835 string2:
1836 T_STRING {
1837 if (tflag) {
1838 /* concatenated strings are illegal in traditional C */
1839 warning(219);
1840 }
1841 $$ = $1;
1842 }
1843 | string2 T_STRING {
1844 $$ = catstrg($1, $2);
1845 }
1846 ;
1847
1848 func_arg_list:
1849 expr %prec T_COMMA {
1850 $$ = funcarg(NULL, $1);
1851 }
1852 | func_arg_list T_COMMA expr {
1853 $$ = funcarg($1, $3);
1854 }
1855 ;
1856
1857 point_or_arrow:
1858 T_STROP {
1859 symtyp = FMOS;
1860 $$ = $1;
1861 }
1862 ;
1863
1864 point:
1865 T_STROP {
1866 if ($1 != POINT) {
1867 error(249, yytext);
1868 }
1869 }
1870 ;
1871
1872 identifier:
1873 T_NAME {
1874 $$ = $1;
1875 }
1876 | T_TYPENAME {
1877 $$ = $1;
1878 }
1879 ;
1880
1881 %%
1882
1883 /* ARGSUSED */
1884 int
1885 yyerror(const char *msg)
1886 {
1887 error(249, yytext);
1888 if (++sytxerr >= 5)
1889 norecover();
1890 return (0);
1891 }
1892
1893 static __inline int uq_gt(uint64_t, uint64_t);
1894 static __inline int q_gt(int64_t, int64_t);
1895
1896 static __inline int
1897 uq_gt(uint64_t a, uint64_t b)
1898 {
1899
1900 return (a > b);
1901 }
1902
1903 static __inline int
1904 q_gt(int64_t a, int64_t b)
1905 {
1906
1907 return (a > b);
1908 }
1909
1910 #define q_lt(a, b) q_gt(b, a)
1911
1912 /*
1913 * Gets a node for a constant and returns the value of this constant
1914 * as integer.
1915 * Is the node not constant or too large for int or of type float,
1916 * a warning will be printed.
1917 *
1918 * toicon() should be used only inside declarations. If it is used in
1919 * expressions, it frees the memory used for the expression.
1920 */
1921 static int
1922 toicon(tnode_t *tn, int required)
1923 {
1924 int i;
1925 tspec_t t;
1926 val_t *v;
1927
1928 v = constant(tn, required);
1929
1930 /*
1931 * Abstract declarations are used inside expression. To free
1932 * the memory would be a fatal error.
1933 * We don't free blocks that are inside casts because these
1934 * will be used later to match types.
1935 */
1936 if (tn->tn_op != CON && dcs->d_ctx != ABSTRACT)
1937 tfreeblk();
1938
1939 if ((t = v->v_tspec) == FLOAT || t == DOUBLE || t == LDOUBLE) {
1940 i = (int)v->v_ldbl;
1941 /* integral constant expression expected */
1942 error(55);
1943 } else {
1944 i = (int)v->v_quad;
1945 if (isutyp(t)) {
1946 if (uq_gt((uint64_t)v->v_quad,
1947 (uint64_t)TARG_INT_MAX)) {
1948 /* integral constant too large */
1949 warning(56);
1950 }
1951 } else {
1952 if (q_gt(v->v_quad, (int64_t)TARG_INT_MAX) ||
1953 q_lt(v->v_quad, (int64_t)TARG_INT_MIN)) {
1954 /* integral constant too large */
1955 warning(56);
1956 }
1957 }
1958 }
1959 free(v);
1960 return (i);
1961 }
1962
1963 static void
1964 idecl(sym_t *decl, int initflg, sbuf_t *renaming)
1965 {
1966 char *s;
1967
1968 initerr = 0;
1969 initsym = decl;
1970
1971 switch (dcs->d_ctx) {
1972 case EXTERN:
1973 if (renaming != NULL) {
1974 if (decl->s_rename != NULL)
1975 LERROR("idecl()");
1976
1977 s = getlblk(1, renaming->sb_len + 1);
1978 (void)memcpy(s, renaming->sb_name, renaming->sb_len + 1);
1979 decl->s_rename = s;
1980 freeyyv(&renaming, T_NAME);
1981 }
1982 decl1ext(decl, initflg);
1983 break;
1984 case ARG:
1985 if (renaming != NULL) {
1986 /* symbol renaming can't be used on function arguments */
1987 error(310);
1988 freeyyv(&renaming, T_NAME);
1989 break;
1990 }
1991 (void)decl1arg(decl, initflg);
1992 break;
1993 case AUTO:
1994 if (renaming != NULL) {
1995 /* symbol renaming can't be used on automatic variables */
1996 error(311);
1997 freeyyv(&renaming, T_NAME);
1998 break;
1999 }
2000 decl1loc(decl, initflg);
2001 break;
2002 default:
2003 LERROR("idecl()");
2004 }
2005
2006 if (initflg && !initerr)
2007 prepinit();
2008 }
2009
2010 /*
2011 * Discard all input tokens up to and including the next
2012 * unmatched right paren
2013 */
2014 static void
2015 ignuptorp(void)
2016 {
2017 int level;
2018
2019 if (yychar < 0)
2020 yychar = yylex();
2021 freeyyv(&yylval, yychar);
2022
2023 level = 1;
2024 while (yychar != T_RPARN || --level > 0) {
2025 if (yychar == T_LPARN) {
2026 level++;
2027 } else if (yychar <= 0) {
2028 break;
2029 }
2030 freeyyv(&yylval, yychar = yylex());
2031 }
2032
2033 yyclearin;
2034 }
2035