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