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