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