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