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