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