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