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