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