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