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