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