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