cgram.y revision 1.78 1 %{
2 /* $NetBSD: cgram.y,v 1.78 2016/07/20 18:14:12 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.78 2016/07/20 18:14:12 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 type_attribute {
1024 $$ = dname(getsym($1));
1025 }
1026 | identifier {
1027 $$ = dname(getsym($1));
1028 }
1029 | T_LPARN notype_param_decl T_RPARN {
1030 $$ = $2;
1031 }
1032 | direct_param_decl T_LBRACK T_RBRACK {
1033 $$ = addarray($1, 0, 0);
1034 }
1035 | direct_param_decl T_LBRACK constant T_RBRACK {
1036 $$ = addarray($1, 1, toicon($3, 0));
1037 }
1038 | direct_param_decl param_list opt_asm_or_symbolrename {
1039 $$ = addfunc(symbolrename($1, $3), $2);
1040 popdecl();
1041 blklev--;
1042 }
1043 ;
1044
1045 notype_param_decl:
1046 direct_notype_param_decl {
1047 $$ = $1;
1048 }
1049 | pointer direct_notype_param_decl {
1050 $$ = addptr($2, $1);
1051 }
1052 ;
1053
1054 direct_notype_param_decl:
1055 identifier {
1056 $$ = dname(getsym($1));
1057 }
1058 | T_LPARN notype_param_decl T_RPARN {
1059 $$ = $2;
1060 }
1061 | direct_notype_param_decl T_LBRACK T_RBRACK {
1062 $$ = addarray($1, 0, 0);
1063 }
1064 | direct_notype_param_decl T_LBRACK constant T_RBRACK {
1065 $$ = addarray($1, 1, toicon($3, 0));
1066 }
1067 | direct_notype_param_decl param_list opt_asm_or_symbolrename {
1068 $$ = addfunc(symbolrename($1, $3), $2);
1069 popdecl();
1070 blklev--;
1071 }
1072 ;
1073
1074 pointer:
1075 asterisk {
1076 $$ = $1;
1077 }
1078 | asterisk type_qualifier_list {
1079 $$ = mergepq($1, $2);
1080 }
1081 | asterisk pointer {
1082 $$ = mergepq($1, $2);
1083 }
1084 | asterisk type_qualifier_list pointer {
1085 $$ = mergepq(mergepq($1, $2), $3);
1086 }
1087 ;
1088
1089 asterisk:
1090 T_MULT {
1091 $$ = xcalloc(1, sizeof (pqinf_t));
1092 $$->p_pcnt = 1;
1093 }
1094 ;
1095
1096 type_qualifier_list:
1097 type_qualifier {
1098 $$ = $1;
1099 }
1100 | type_qualifier_list type_qualifier {
1101 $$ = mergepq($1, $2);
1102 }
1103 ;
1104
1105 type_qualifier:
1106 T_QUAL {
1107 $$ = xcalloc(1, sizeof (pqinf_t));
1108 if ($1 == CONST) {
1109 $$->p_const = 1;
1110 } else {
1111 $$->p_volatile = 1;
1112 }
1113 }
1114 ;
1115
1116 param_list:
1117 id_list_lparn identifier_list T_RPARN {
1118 $$ = $2;
1119 }
1120 | abs_decl_param_list {
1121 $$ = $1;
1122 }
1123 ;
1124
1125 id_list_lparn:
1126 T_LPARN {
1127 blklev++;
1128 pushdecl(PARG);
1129 }
1130 ;
1131
1132 identifier_list:
1133 T_NAME {
1134 $$ = iname(getsym($1));
1135 }
1136 | identifier_list T_COMMA T_NAME {
1137 $$ = lnklst($1, iname(getsym($3)));
1138 }
1139 | identifier_list error {
1140 $$ = $1;
1141 }
1142 ;
1143
1144 abs_decl_param_list:
1145 abs_decl_lparn T_RPARN {
1146 $$ = NULL;
1147 }
1148 | abs_decl_lparn vararg_parameter_type_list T_RPARN {
1149 dcs->d_proto = 1;
1150 $$ = $2;
1151 }
1152 | abs_decl_lparn error T_RPARN {
1153 $$ = NULL;
1154 }
1155 ;
1156
1157 abs_decl_lparn:
1158 T_LPARN {
1159 blklev++;
1160 pushdecl(PARG);
1161 }
1162 ;
1163
1164 vararg_parameter_type_list:
1165 parameter_type_list {
1166 $$ = $1;
1167 }
1168 | parameter_type_list T_COMMA T_ELLIPSE {
1169 dcs->d_vararg = 1;
1170 $$ = $1;
1171 }
1172 | T_ELLIPSE {
1173 if (sflag) {
1174 /* ANSI C requires formal parameter before "..." */
1175 error(84);
1176 } else if (!tflag) {
1177 /* ANSI C requires formal parameter before "..." */
1178 warning(84);
1179 }
1180 dcs->d_vararg = 1;
1181 $$ = NULL;
1182 }
1183 ;
1184
1185 parameter_type_list:
1186 parameter_declaration {
1187 $$ = $1;
1188 }
1189 | parameter_type_list T_COMMA parameter_declaration {
1190 $$ = lnklst($1, $3);
1191 }
1192 ;
1193
1194 parameter_declaration:
1195 declmods deftyp {
1196 $$ = decl1arg(aname(), 0);
1197 }
1198 | declspecs deftyp {
1199 $$ = decl1arg(aname(), 0);
1200 }
1201 | declmods deftyp notype_param_decl {
1202 $$ = decl1arg($3, 0);
1203 }
1204 /*
1205 * param_decl is needed because of following conflict:
1206 * "typedef int a; f(int (a));" could be parsed as
1207 * "function with argument a of type int", or
1208 * "function with an abstract argument of type function".
1209 * This grammar realizes the second case.
1210 */
1211 | declspecs deftyp param_decl {
1212 $$ = decl1arg($3, 0);
1213 }
1214 | declmods deftyp abs_decl {
1215 $$ = decl1arg($3, 0);
1216 }
1217 | declspecs deftyp abs_decl {
1218 $$ = decl1arg($3, 0);
1219 }
1220 ;
1221
1222 opt_asm_or_symbolrename: /* expect only one */
1223 /* empty */ {
1224 $$ = NULL;
1225 }
1226 | T_ASM T_LPARN T_STRING T_RPARN {
1227 freeyyv(&$3, T_STRING);
1228 $$ = NULL;
1229 }
1230 | T_SYMBOLRENAME T_LPARN T_NAME T_RPARN {
1231 $$ = $3;
1232 }
1233 ;
1234
1235 initializer:
1236 init_expr
1237 ;
1238
1239 init_expr:
1240 | expr %prec T_COMMA {
1241 mkinit($1);
1242 }
1243 | init_by_name init_expr %prec T_COMMA
1244 | init_lbrace init_rbrace
1245 | init_lbrace init_expr_list init_rbrace
1246 | init_lbrace init_expr_list T_COMMA init_rbrace
1247 | error
1248 ;
1249
1250 init_expr_list:
1251 init_expr %prec T_COMMA
1252 | init_expr_list T_COMMA init_expr
1253 ;
1254
1255 lorange:
1256 constant T_ELLIPSE {
1257 $$.lo = toicon($1, 1);
1258 }
1259 ;
1260 range:
1261 constant {
1262 $$.lo = toicon($1, 1);
1263 $$.hi = $$.lo + 1;
1264 }
1265 | lorange constant {
1266 $$.lo = $1.lo;
1267 $$.hi = toicon($2, 1);
1268 }
1269 ;
1270
1271 init_field:
1272 T_LBRACK range T_RBRACK {
1273 if (!Sflag)
1274 warning(321);
1275 }
1276 | point identifier {
1277 if (!Sflag)
1278 warning(313);
1279 memberpush($2);
1280 }
1281 ;
1282
1283 init_field_list:
1284 init_field
1285 | init_field_list init_field
1286 ;
1287
1288 init_by_name:
1289 init_field_list T_ASSIGN
1290 | identifier T_COLON {
1291 gnuism(315);
1292 memberpush($1);
1293 }
1294 ;
1295
1296 init_lbrace:
1297 T_LBRACE {
1298 initlbr();
1299 }
1300 ;
1301
1302 init_rbrace:
1303 T_RBRACE {
1304 initrbr();
1305 }
1306 ;
1307
1308 type_name:
1309 {
1310 pushdecl(ABSTRACT);
1311 } abstract_declaration {
1312 popdecl();
1313 $$ = $2->s_type;
1314 }
1315 ;
1316
1317 abstract_declaration:
1318 noclass_declmods deftyp {
1319 $$ = decl1abs(aname());
1320 }
1321 | noclass_declspecs deftyp {
1322 $$ = decl1abs(aname());
1323 }
1324 | noclass_declmods deftyp abs_decl {
1325 $$ = decl1abs($3);
1326 }
1327 | noclass_declspecs deftyp abs_decl {
1328 $$ = decl1abs($3);
1329 }
1330 ;
1331
1332 abs_decl:
1333 pointer {
1334 $$ = addptr(aname(), $1);
1335 }
1336 | direct_abs_decl {
1337 $$ = $1;
1338 }
1339 | pointer direct_abs_decl {
1340 $$ = addptr($2, $1);
1341 }
1342 ;
1343
1344 direct_abs_decl:
1345 T_LPARN abs_decl T_RPARN {
1346 $$ = $2;
1347 }
1348 | T_LBRACK T_RBRACK {
1349 $$ = addarray(aname(), 0, 0);
1350 }
1351 | T_LBRACK constant T_RBRACK {
1352 $$ = addarray(aname(), 1, toicon($2, 0));
1353 }
1354 | type_attribute direct_abs_decl {
1355 $$ = $2;
1356 }
1357 | direct_abs_decl T_LBRACK T_RBRACK {
1358 $$ = addarray($1, 0, 0);
1359 }
1360 | direct_abs_decl T_LBRACK constant T_RBRACK {
1361 $$ = addarray($1, 1, toicon($3, 0));
1362 }
1363 | abs_decl_param_list opt_asm_or_symbolrename {
1364 $$ = addfunc(symbolrename(aname(), $2), $1);
1365 popdecl();
1366 blklev--;
1367 }
1368 | direct_abs_decl abs_decl_param_list opt_asm_or_symbolrename {
1369 $$ = addfunc(symbolrename($1, $3), $2);
1370 popdecl();
1371 blklev--;
1372 }
1373 | direct_abs_decl type_attribute
1374 ;
1375
1376 non_expr_stmnt:
1377 labeled_stmnt
1378 | comp_stmnt
1379 | selection_stmnt
1380 | iteration_stmnt
1381 | jump_stmnt {
1382 ftflg = 0;
1383 }
1384 | asm_stmnt
1385
1386 stmnt:
1387 expr_stmnt
1388 | non_expr_stmnt
1389 ;
1390
1391 labeled_stmnt:
1392 label stmnt
1393 ;
1394
1395 label:
1396 T_NAME T_COLON {
1397 symtyp = FLAB;
1398 label(T_NAME, getsym($1), NULL);
1399 }
1400 | T_CASE constant T_COLON {
1401 label(T_CASE, NULL, $2);
1402 ftflg = 1;
1403 }
1404 | T_CASE constant T_ELLIPSE constant T_COLON {
1405 /* XXX: We don't fill all cases */
1406 label(T_CASE, NULL, $2);
1407 ftflg = 1;
1408 }
1409 | T_DEFAULT T_COLON {
1410 label(T_DEFAULT, NULL, NULL);
1411 ftflg = 1;
1412 }
1413 ;
1414
1415 stmnt_d_list:
1416 stmnt_list
1417 | stmnt_d_list declaration_list stmnt_list {
1418 if (!Sflag)
1419 c99ism(327);
1420 }
1421 ;
1422
1423 comp_stmnt:
1424 comp_stmnt_lbrace comp_stmnt_rbrace
1425 | comp_stmnt_lbrace stmnt_d_list comp_stmnt_rbrace
1426 | comp_stmnt_lbrace declaration_list comp_stmnt_rbrace
1427 | comp_stmnt_lbrace declaration_list stmnt_d_list comp_stmnt_rbrace
1428 ;
1429
1430 comp_stmnt_lbrace:
1431 T_LBRACE {
1432 blklev++;
1433 mblklev++;
1434 pushdecl(AUTO);
1435 }
1436 ;
1437
1438 comp_stmnt_rbrace:
1439 T_RBRACE {
1440 popdecl();
1441 freeblk();
1442 mblklev--;
1443 blklev--;
1444 ftflg = 0;
1445 }
1446 ;
1447
1448 stmnt_list:
1449 stmnt
1450 | stmnt_list stmnt {
1451 RESTORE(__FILE__, __LINE__);
1452 }
1453 | stmnt_list error T_SEMI
1454 ;
1455
1456 expr_stmnt:
1457 expr T_SEMI {
1458 expr($1, 0, 0, 1);
1459 ftflg = 0;
1460 }
1461 | T_SEMI {
1462 ftflg = 0;
1463 }
1464 ;
1465
1466 /*
1467 * The following two productions are used to implement
1468 * ({ [[decl-list] stmt-list] }).
1469 * XXX: This is not well tested.
1470 */
1471 expr_stmnt_val:
1472 expr T_SEMI {
1473 /* XXX: We should really do that only on the last name */
1474 if ($1->tn_op == NAME)
1475 $1->tn_sym->s_used = 1;
1476 $$ = $1;
1477 expr($1, 0, 0, 0);
1478 ftflg = 0;
1479 }
1480 | non_expr_stmnt {
1481 $$ = getnode();
1482 $$->tn_type = gettyp(VOID);
1483 }
1484 ;
1485
1486 expr_stmnt_list:
1487 expr_stmnt_val
1488 | expr_stmnt_list expr_stmnt_val {
1489 $$ = $2;
1490 }
1491 ;
1492
1493 selection_stmnt:
1494 if_without_else {
1495 SAVE(__FILE__, __LINE__);
1496 if2();
1497 if3(0);
1498 }
1499 | if_without_else T_ELSE {
1500 SAVE(__FILE__, __LINE__);
1501 if2();
1502 } stmnt {
1503 CLRWFLGS(__FILE__, __LINE__);
1504 if3(1);
1505 }
1506 | if_without_else T_ELSE error {
1507 CLRWFLGS(__FILE__, __LINE__);
1508 if3(0);
1509 }
1510 | switch_expr stmnt {
1511 CLRWFLGS(__FILE__, __LINE__);
1512 switch2();
1513 }
1514 | switch_expr error {
1515 CLRWFLGS(__FILE__, __LINE__);
1516 switch2();
1517 }
1518 ;
1519
1520 if_without_else:
1521 if_expr stmnt
1522 | if_expr error
1523 ;
1524
1525 if_expr:
1526 T_IF T_LPARN expr T_RPARN {
1527 if1($3);
1528 CLRWFLGS(__FILE__, __LINE__);
1529 }
1530 ;
1531
1532 switch_expr:
1533 T_SWITCH T_LPARN expr T_RPARN {
1534 switch1($3);
1535 CLRWFLGS(__FILE__, __LINE__);
1536 }
1537 ;
1538
1539 do_stmnt:
1540 do stmnt {
1541 CLRWFLGS(__FILE__, __LINE__);
1542 }
1543 ;
1544
1545 iteration_stmnt:
1546 while_expr stmnt {
1547 CLRWFLGS(__FILE__, __LINE__);
1548 while2();
1549 }
1550 | while_expr error {
1551 CLRWFLGS(__FILE__, __LINE__);
1552 while2();
1553 }
1554 | do_stmnt do_while_expr {
1555 do2($2);
1556 ftflg = 0;
1557 }
1558 | do error {
1559 CLRWFLGS(__FILE__, __LINE__);
1560 do2(NULL);
1561 }
1562 | for_exprs stmnt {
1563 CLRWFLGS(__FILE__, __LINE__);
1564 for2();
1565 popdecl();
1566 blklev--;
1567 }
1568 | for_exprs error {
1569 CLRWFLGS(__FILE__, __LINE__);
1570 for2();
1571 popdecl();
1572 blklev--;
1573 }
1574 ;
1575
1576 while_expr:
1577 T_WHILE T_LPARN expr T_RPARN {
1578 while1($3);
1579 CLRWFLGS(__FILE__, __LINE__);
1580 }
1581 ;
1582
1583 do:
1584 T_DO {
1585 do1();
1586 }
1587 ;
1588
1589 do_while_expr:
1590 T_WHILE T_LPARN expr T_RPARN T_SEMI {
1591 $$ = $3;
1592 }
1593 ;
1594
1595 for_start:
1596 T_FOR T_LPARN {
1597 pushdecl(AUTO);
1598 blklev++;
1599 }
1600 ;
1601 for_exprs:
1602 for_start declspecs deftyp notype_init_decls T_SEMI opt_expr
1603 T_SEMI opt_expr T_RPARN {
1604 c99ism(325);
1605 for1(NULL, $6, $8);
1606 CLRWFLGS(__FILE__, __LINE__);
1607 }
1608 | for_start opt_expr T_SEMI opt_expr T_SEMI opt_expr T_RPARN {
1609 for1($2, $4, $6);
1610 CLRWFLGS(__FILE__, __LINE__);
1611 }
1612 ;
1613
1614 opt_expr:
1615 /* empty */ {
1616 $$ = NULL;
1617 }
1618 | expr {
1619 $$ = $1;
1620 }
1621 ;
1622
1623 jump_stmnt:
1624 goto identifier T_SEMI {
1625 dogoto(getsym($2));
1626 }
1627 | goto error T_SEMI {
1628 symtyp = FVFT;
1629 }
1630 | T_CONTINUE T_SEMI {
1631 docont();
1632 }
1633 | T_BREAK T_SEMI {
1634 dobreak();
1635 }
1636 | T_RETURN T_SEMI {
1637 doreturn(NULL);
1638 }
1639 | T_RETURN expr T_SEMI {
1640 doreturn($2);
1641 }
1642 ;
1643
1644 goto:
1645 T_GOTO {
1646 symtyp = FLAB;
1647 }
1648 ;
1649
1650 asm_stmnt:
1651 T_ASM T_LPARN read_until_rparn T_SEMI {
1652 setasm();
1653 }
1654 | T_ASM T_QUAL T_LPARN read_until_rparn T_SEMI {
1655 setasm();
1656 }
1657 | T_ASM error
1658 ;
1659
1660 read_until_rparn:
1661 /* empty */ {
1662 ignuptorp();
1663 }
1664 ;
1665
1666 declaration_list:
1667 declaration {
1668 CLRWFLGS(__FILE__, __LINE__);
1669 }
1670 | declaration_list declaration {
1671 CLRWFLGS(__FILE__, __LINE__);
1672 }
1673 ;
1674
1675 constant:
1676 expr %prec T_COMMA {
1677 $$ = $1;
1678 }
1679 ;
1680
1681 expr:
1682 expr T_MULT expr {
1683 $$ = build(MULT, $1, $3);
1684 }
1685 | expr T_DIVOP expr {
1686 $$ = build($2, $1, $3);
1687 }
1688 | expr T_ADDOP expr {
1689 $$ = build($2, $1, $3);
1690 }
1691 | expr T_SHFTOP expr {
1692 $$ = build($2, $1, $3);
1693 }
1694 | expr T_RELOP expr {
1695 $$ = build($2, $1, $3);
1696 }
1697 | expr T_EQOP expr {
1698 $$ = build($2, $1, $3);
1699 }
1700 | expr T_AND expr {
1701 $$ = build(AND, $1, $3);
1702 }
1703 | expr T_XOR expr {
1704 $$ = build(XOR, $1, $3);
1705 }
1706 | expr T_OR expr {
1707 $$ = build(OR, $1, $3);
1708 }
1709 | expr T_LOGAND expr {
1710 $$ = build(LOGAND, $1, $3);
1711 }
1712 | expr T_LOGOR expr {
1713 $$ = build(LOGOR, $1, $3);
1714 }
1715 | expr T_QUEST expr T_COLON expr {
1716 $$ = build(QUEST, $1, build(COLON, $3, $5));
1717 }
1718 | expr T_ASSIGN expr {
1719 $$ = build(ASSIGN, $1, $3);
1720 }
1721 | expr T_OPASS expr {
1722 $$ = build($2, $1, $3);
1723 }
1724 | expr T_COMMA expr {
1725 $$ = build(COMMA, $1, $3);
1726 }
1727 | term {
1728 $$ = $1;
1729 }
1730 ;
1731
1732 term:
1733 T_NAME {
1734 /* XXX really necessary? */
1735 if (yychar < 0)
1736 yychar = yylex();
1737 $$ = getnnode(getsym($1), yychar);
1738 }
1739 | string {
1740 $$ = getsnode($1);
1741 }
1742 | T_CON {
1743 $$ = getcnode(gettyp($1->v_tspec), $1);
1744 }
1745 | T_LPARN expr T_RPARN {
1746 if ($2 != NULL)
1747 $2->tn_parn = 1;
1748 $$ = $2;
1749 }
1750 | T_LPARN comp_stmnt_lbrace declaration_list expr_stmnt_list {
1751 blklev--;
1752 mblklev--;
1753 initsym = mktempsym(duptyp($4->tn_type));
1754 mblklev++;
1755 blklev++;
1756 gnuism(320);
1757 } comp_stmnt_rbrace T_RPARN {
1758 $$ = getnnode(initsym, 0);
1759 }
1760 | T_LPARN comp_stmnt_lbrace expr_stmnt_list {
1761 blklev--;
1762 mblklev--;
1763 initsym = mktempsym($3->tn_type);
1764 mblklev++;
1765 blklev++;
1766 gnuism(320);
1767 } comp_stmnt_rbrace T_RPARN {
1768 $$ = getnnode(initsym, 0);
1769 }
1770 | term T_INCDEC {
1771 $$ = build($2 == INC ? INCAFT : DECAFT, $1, NULL);
1772 }
1773 | T_INCDEC term {
1774 $$ = build($1 == INC ? INCBEF : DECBEF, $2, NULL);
1775 }
1776 | T_MULT term {
1777 $$ = build(STAR, $2, NULL);
1778 }
1779 | T_AND term {
1780 $$ = build(AMPER, $2, NULL);
1781 }
1782 | T_UNOP term {
1783 $$ = build($1, $2, NULL);
1784 }
1785 | T_ADDOP term {
1786 if (tflag && $1 == PLUS) {
1787 /* unary + is illegal in traditional C */
1788 warning(100);
1789 }
1790 $$ = build($1 == PLUS ? UPLUS : UMINUS, $2, NULL);
1791 }
1792 | term T_LBRACK expr T_RBRACK {
1793 $$ = build(STAR, build(PLUS, $1, $3), NULL);
1794 }
1795 | term T_LPARN T_RPARN {
1796 $$ = funccall($1, NULL);
1797 }
1798 | term T_LPARN func_arg_list T_RPARN {
1799 $$ = funccall($1, $3);
1800 }
1801 | term point_or_arrow T_NAME {
1802 if ($1 != NULL) {
1803 sym_t *msym;
1804 /* XXX strmemb should be integrated in build() */
1805 if ($2 == ARROW) {
1806 /* must to this before strmemb is called */
1807 $1 = cconv($1);
1808 }
1809 msym = strmemb($1, $2, getsym($3));
1810 $$ = build($2, $1, getnnode(msym, 0));
1811 } else {
1812 $$ = NULL;
1813 }
1814 }
1815 | T_REAL term {
1816 $$ = build(REAL, $2, NULL);
1817 }
1818 | T_IMAG term {
1819 $$ = build(IMAG, $2, NULL);
1820 }
1821 | T_EXTENSION term {
1822 $$ = $2;
1823 }
1824 | T_REAL T_LPARN term T_RPARN {
1825 $$ = build(REAL, $3, NULL);
1826 }
1827 | T_IMAG T_LPARN term T_RPARN {
1828 $$ = build(IMAG, $3, NULL);
1829 }
1830 | T_SIZEOF term %prec T_SIZEOF {
1831 if (($$ = $2 == NULL ? NULL : bldszof($2->tn_type)) != NULL)
1832 chkmisc($2, 0, 0, 0, 0, 0, 1);
1833 }
1834 | T_SIZEOF T_LPARN type_name T_RPARN %prec T_SIZEOF {
1835 $$ = bldszof($3);
1836 }
1837 | T_ALIGNOF T_LPARN type_name T_RPARN %prec T_ALIGNOF {
1838 $$ = bldalof($3);
1839 }
1840 | T_LPARN type_name T_RPARN term %prec T_UNOP {
1841 $$ = cast($4, $2);
1842 }
1843 | T_LPARN type_name T_RPARN %prec T_UNOP {
1844 sym_t *tmp = mktempsym($2);
1845 idecl(tmp, 1, NULL);
1846 } init_lbrace init_expr_list init_rbrace {
1847 if (!Sflag)
1848 gnuism(319);
1849 $$ = getnnode(initsym, 0);
1850 }
1851 ;
1852
1853 string:
1854 T_STRING {
1855 $$ = $1;
1856 }
1857 | T_STRING string2 {
1858 $$ = catstrg($1, $2);
1859 }
1860 ;
1861
1862 string2:
1863 T_STRING {
1864 if (tflag) {
1865 /* concatenated strings are illegal in traditional C */
1866 warning(219);
1867 }
1868 $$ = $1;
1869 }
1870 | string2 T_STRING {
1871 $$ = catstrg($1, $2);
1872 }
1873 ;
1874
1875 func_arg_list:
1876 expr %prec T_COMMA {
1877 $$ = funcarg(NULL, $1);
1878 }
1879 | func_arg_list T_COMMA expr {
1880 $$ = funcarg($1, $3);
1881 }
1882 ;
1883
1884 point_or_arrow:
1885 T_STROP {
1886 symtyp = FMOS;
1887 $$ = $1;
1888 }
1889 ;
1890
1891 point:
1892 T_STROP {
1893 if ($1 != POINT) {
1894 error(249, yytext);
1895 }
1896 }
1897 ;
1898
1899 identifier:
1900 T_NAME {
1901 $$ = $1;
1902 }
1903 | T_TYPENAME {
1904 $$ = $1;
1905 }
1906 ;
1907
1908 %%
1909
1910 /* ARGSUSED */
1911 int
1912 yyerror(const char *msg)
1913 {
1914 error(249, yytext);
1915 if (++sytxerr >= 5)
1916 norecover();
1917 return (0);
1918 }
1919
1920 static __inline int uq_gt(uint64_t, uint64_t);
1921 static __inline int q_gt(int64_t, int64_t);
1922
1923 static __inline int
1924 uq_gt(uint64_t a, uint64_t b)
1925 {
1926
1927 return (a > b);
1928 }
1929
1930 static __inline int
1931 q_gt(int64_t a, int64_t b)
1932 {
1933
1934 return (a > b);
1935 }
1936
1937 #define q_lt(a, b) q_gt(b, a)
1938
1939 /*
1940 * Gets a node for a constant and returns the value of this constant
1941 * as integer.
1942 * Is the node not constant or too large for int or of type float,
1943 * a warning will be printed.
1944 *
1945 * toicon() should be used only inside declarations. If it is used in
1946 * expressions, it frees the memory used for the expression.
1947 */
1948 static int
1949 toicon(tnode_t *tn, int required)
1950 {
1951 int i;
1952 tspec_t t;
1953 val_t *v;
1954
1955 v = constant(tn, required);
1956
1957 /*
1958 * Abstract declarations are used inside expression. To free
1959 * the memory would be a fatal error.
1960 * We don't free blocks that are inside casts because these
1961 * will be used later to match types.
1962 */
1963 if (tn->tn_op != CON && dcs->d_ctx != ABSTRACT)
1964 tfreeblk();
1965
1966 if ((t = v->v_tspec) == FLOAT || t == DOUBLE || t == LDOUBLE) {
1967 i = (int)v->v_ldbl;
1968 /* integral constant expression expected */
1969 error(55);
1970 } else {
1971 i = (int)v->v_quad;
1972 if (isutyp(t)) {
1973 if (uq_gt((uint64_t)v->v_quad,
1974 (uint64_t)TARG_INT_MAX)) {
1975 /* integral constant too large */
1976 warning(56);
1977 }
1978 } else {
1979 if (q_gt(v->v_quad, (int64_t)TARG_INT_MAX) ||
1980 q_lt(v->v_quad, (int64_t)TARG_INT_MIN)) {
1981 /* integral constant too large */
1982 warning(56);
1983 }
1984 }
1985 }
1986 free(v);
1987 return (i);
1988 }
1989
1990 static void
1991 idecl(sym_t *decl, int initflg, sbuf_t *renaming)
1992 {
1993 char *s;
1994
1995 initerr = 0;
1996 initsym = decl;
1997
1998 switch (dcs->d_ctx) {
1999 case EXTERN:
2000 if (renaming != NULL) {
2001 if (decl->s_rename != NULL)
2002 LERROR("idecl(rename)");
2003
2004 s = getlblk(1, renaming->sb_len + 1);
2005 (void)memcpy(s, renaming->sb_name, renaming->sb_len + 1);
2006 decl->s_rename = s;
2007 freeyyv(&renaming, T_NAME);
2008 }
2009 decl1ext(decl, initflg);
2010 break;
2011 case ARG:
2012 if (renaming != NULL) {
2013 /* symbol renaming can't be used on function arguments */
2014 error(310);
2015 freeyyv(&renaming, T_NAME);
2016 break;
2017 }
2018 (void)decl1arg(decl, initflg);
2019 break;
2020 case AUTO:
2021 if (renaming != NULL) {
2022 /* symbol renaming can't be used on automatic variables */
2023 error(311);
2024 freeyyv(&renaming, T_NAME);
2025 break;
2026 }
2027 decl1loc(decl, initflg);
2028 break;
2029 default:
2030 LERROR("idecl(%d)", dcs->d_ctx);
2031 }
2032
2033 if (initflg && !initerr)
2034 prepinit();
2035 }
2036
2037 /*
2038 * Discard all input tokens up to and including the next
2039 * unmatched right paren
2040 */
2041 static void
2042 ignuptorp(void)
2043 {
2044 int level;
2045
2046 if (yychar < 0)
2047 yychar = yylex();
2048 freeyyv(&yylval, yychar);
2049
2050 level = 1;
2051 while (yychar != T_RPARN || --level > 0) {
2052 if (yychar == T_LPARN) {
2053 level++;
2054 } else if (yychar <= 0) {
2055 break;
2056 }
2057 freeyyv(&yylval, yychar = yylex());
2058 }
2059
2060 yyclearin;
2061 }
2062
2063 static sym_t *
2064 symbolrename(sym_t *s, sbuf_t *sb)
2065 {
2066 if (sb)
2067 s->s_rename = sb->sb_name;
2068 return s;
2069 }
2070