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