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