1 %{ 2 /* $NetBSD: cgram.y,v 1.424 2022/10/01 09:42:40 rillig 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) 38 __RCSID("$NetBSD: cgram.y,v 1.424 2022/10/01 09:42:40 rillig Exp $"); 39 #endif 40 41 #include <limits.h> 42 #include <stdlib.h> 43 #include <string.h> 44 45 #include "lint1.h" 46 47 extern char *yytext; 48 49 /* 50 * Contains the level of current declaration, used for symbol table entries. 51 * 0 is the top-level, > 0 is inside a function body. 52 */ 53 int block_level; 54 55 /* 56 * level for memory allocation. Normally the same as block_level. 57 * An exception is the declaration of arguments in prototypes. Memory 58 * for these can't be freed after the declaration, but symbols must 59 * be removed from the symbol table after the declaration. 60 */ 61 size_t mem_block_level; 62 63 /* 64 * Save the no-warns state and restore it to avoid the problem where 65 * if (expr) { stmt } / * NOLINT * / stmt; 66 */ 67 #define LWARN_NOTHING_SAVED (-3) 68 static int saved_lwarn = LWARN_NOTHING_SAVED; 69 70 static void cgram_declare(sym_t *, bool, sbuf_t *); 71 static void read_until_rparen(void); 72 static sym_t *symbolrename(sym_t *, sbuf_t *); 73 74 75 /* ARGSUSED */ 76 static void 77 clear_warning_flags_loc(const char *file, size_t line) 78 { 79 debug_step("%s:%zu: clearing flags", file, line); 80 clear_warn_flags(); 81 saved_lwarn = LWARN_NOTHING_SAVED; 82 } 83 84 /* ARGSUSED */ 85 static void 86 save_warning_flags_loc(const char *file, size_t line) 87 { 88 /* 89 * There used to be an assertion that saved_lwarn is 90 * LWARN_NOTHING_SAVED here, but that triggered for the following 91 * code: 92 * 93 * void function(int x) { if (x > 0) if (x > 1) return; } 94 * 95 * It didn't trigger if the inner 'if' was enclosed in braces though. 96 * 97 * TODO: If actually needed, add support for nested suppression of 98 * warnings. 99 */ 100 debug_step("%s:%zu: saving flags %d", file, line, lwarn); 101 saved_lwarn = lwarn; 102 } 103 104 /* ARGSUSED */ 105 static void 106 restore_warning_flags_loc(const char *file, size_t line) 107 { 108 if (saved_lwarn != LWARN_NOTHING_SAVED) { 109 lwarn = saved_lwarn; 110 debug_step("%s:%zu: restoring flags %d", file, line, lwarn); 111 /* 112 * Do not set 'saved_lwarn = LWARN_NOTHING_SAVED' here, to 113 * avoid triggering the assertion in save_warning_flags_loc. 114 */ 115 } else 116 clear_warning_flags_loc(file, line); 117 } 118 119 #define clear_warning_flags() clear_warning_flags_loc(__FILE__, __LINE__) 120 #define save_warning_flags() save_warning_flags_loc(__FILE__, __LINE__) 121 #define restore_warning_flags() restore_warning_flags_loc(__FILE__, __LINE__) 122 123 /* unbind the anonymous struct members from the struct */ 124 static void 125 anonymize(sym_t *s) 126 { 127 for ( ; s != NULL; s = s->s_next) 128 s->u.s_member.sm_sou_type = NULL; 129 } 130 131 static bool 132 is_either(const char *s, const char *a, const char *b) 133 { 134 return strcmp(s, a) == 0 || strcmp(s, b) == 0; 135 } 136 137 #if YYDEBUG && (YYBYACC || YYBISON) 138 #define YYSTYPE_TOSTRING cgram_to_string 139 #endif 140 #if YYDEBUG && YYBISON 141 #define YYPRINT cgram_print 142 #endif 143 144 %} 145 146 %expect 129 147 148 %union { 149 val_t *y_val; 150 sbuf_t *y_name; 151 sym_t *y_sym; 152 op_t y_op; 153 scl_t y_scl; 154 tspec_t y_tspec; 155 tqual_t y_tqual; 156 type_t *y_type; 157 tnode_t *y_tnode; 158 range_t y_range; 159 strg_t *y_string; 160 qual_ptr *y_qual_ptr; 161 bool y_seen_statement; 162 struct generic_association *y_generic; 163 struct array_size y_array_size; 164 bool y_in_system_header; 165 }; 166 167 %token T_LBRACE T_RBRACE T_LBRACK T_RBRACK T_LPAREN T_RPAREN 168 %token T_POINT T_ARROW 169 %token T_COMPLEMENT T_LOGNOT 170 %token <y_op> T_INCDEC 171 %token T_SIZEOF 172 %token T_BUILTIN_OFFSETOF 173 %token T_TYPEOF 174 %token T_EXTENSION 175 %token T_ALIGNAS 176 %token T_ALIGNOF 177 %token T_ASTERISK 178 %token <y_op> T_MULTIPLICATIVE 179 %token <y_op> T_ADDITIVE 180 %token <y_op> T_SHIFT 181 %token <y_op> T_RELATIONAL 182 %token <y_op> T_EQUALITY 183 %token T_AMPER 184 %token T_BITXOR 185 %token T_BITOR 186 %token T_LOGAND 187 %token T_LOGOR 188 %token T_QUEST 189 %token T_COLON 190 %token T_ASSIGN 191 %token <y_op> T_OPASSIGN 192 %token T_COMMA 193 %token T_SEMI 194 %token T_ELLIPSIS 195 %token T_REAL 196 %token T_IMAG 197 %token T_GENERIC 198 %token T_NORETURN 199 200 /* storage classes (extern, static, auto, register and typedef) */ 201 %token <y_scl> T_SCLASS 202 203 /* 204 * predefined type keywords (char, int, short, long, unsigned, signed, 205 * float, double, void); see T_TYPENAME for types from typedef 206 */ 207 %token <y_tspec> T_TYPE 208 209 /* qualifiers (const, volatile, restrict, _Thread_local) */ 210 %token <y_tqual> T_QUAL 211 212 /* struct or union */ 213 %token <y_tspec> T_STRUCT_OR_UNION 214 215 /* remaining keywords */ 216 %token T_ASM 217 %token T_BREAK 218 %token T_CASE 219 %token T_CONTINUE 220 %token T_DEFAULT 221 %token T_DO 222 %token T_ELSE 223 %token T_ENUM 224 %token T_FOR 225 %token T_GOTO 226 %token T_IF 227 %token T_PACKED 228 %token T_RETURN 229 %token T_SWITCH 230 %token T_SYMBOLRENAME 231 %token T_WHILE 232 %token T_STATIC_ASSERT 233 234 %token T_ATTRIBUTE 235 236 %left T_THEN 237 %left T_ELSE 238 %right T_QUEST T_COLON 239 %left T_LOGOR 240 %left T_LOGAND 241 %left T_BITOR 242 %left T_BITXOR 243 %left T_AMPER 244 %left T_EQUALITY 245 %left T_RELATIONAL 246 %left T_SHIFT 247 %left T_ADDITIVE 248 %left T_ASTERISK T_MULTIPLICATIVE 249 250 %token <y_name> T_NAME 251 %token <y_name> T_TYPENAME 252 %token <y_val> T_CON 253 %token <y_string> T_STRING 254 255 %type <y_sym> identifier_sym 256 %type <y_name> identifier 257 %type <y_string> string 258 259 %type <y_tnode> primary_expression 260 %type <y_tnode> generic_selection 261 %type <y_generic> generic_assoc_list 262 %type <y_generic> generic_association 263 %type <y_tnode> postfix_expression 264 %type <y_tnode> gcc_statement_expr_list 265 %type <y_tnode> gcc_statement_expr_item 266 %type <y_op> point_or_arrow 267 %type <y_tnode> argument_expression_list 268 %type <y_tnode> unary_expression 269 %type <y_tnode> cast_expression 270 %type <y_tnode> conditional_expression 271 %type <y_tnode> assignment_expression 272 %type <y_tnode> expression_opt 273 %type <y_tnode> expression 274 %type <y_tnode> constant_expr 275 276 %type <y_type> begin_type_typespec 277 %type <y_type> type_specifier 278 %type <y_type> notype_type_specifier 279 %type <y_type> struct_or_union_specifier 280 %type <y_tspec> struct_or_union 281 %type <y_sym> braced_struct_declaration_list 282 %type <y_sym> struct_declaration_list_with_rbrace 283 %type <y_sym> struct_declaration_list 284 %type <y_sym> struct_declaration 285 %type <y_sym> notype_struct_declarators 286 %type <y_sym> type_struct_declarators 287 %type <y_sym> notype_struct_declarator 288 %type <y_sym> type_struct_declarator 289 %type <y_type> enum_specifier 290 %type <y_sym> enum_declaration 291 %type <y_sym> enums_with_opt_comma 292 %type <y_sym> enumerator_list 293 %type <y_sym> enumerator 294 %type <y_qual_ptr> type_qualifier 295 %type <y_qual_ptr> pointer 296 %type <y_qual_ptr> asterisk 297 %type <y_qual_ptr> type_qualifier_list_opt 298 %type <y_qual_ptr> type_qualifier_list 299 %type <y_sym> notype_declarator 300 %type <y_sym> type_declarator 301 %type <y_sym> notype_direct_declarator 302 %type <y_sym> type_direct_declarator 303 %type <y_sym> type_param_declarator 304 %type <y_sym> notype_param_declarator 305 %type <y_sym> direct_param_declarator 306 %type <y_sym> direct_notype_param_declarator 307 %type <y_sym> param_list 308 %type <y_array_size> array_size_opt 309 %type <y_tnode> array_size 310 %type <y_sym> identifier_list 311 %type <y_type> type_name 312 %type <y_sym> abstract_declaration 313 %type <y_sym> abstract_declarator 314 %type <y_sym> direct_abstract_declarator 315 %type <y_sym> abstract_decl_param_list 316 %type <y_sym> vararg_parameter_type_list 317 %type <y_sym> parameter_type_list 318 %type <y_sym> parameter_declaration 319 %type <y_range> range 320 %type <y_name> asm_or_symbolrename_opt 321 322 %type <y_seen_statement> block_item_list 323 %type <y_seen_statement> block_item 324 %type <y_tnode> do_while_expr 325 %type <y_sym> func_declarator 326 %type <y_in_system_header> sys 327 328 %{ 329 #if YYDEBUG && YYBISON 330 static inline void cgram_print(FILE *, int, YYSTYPE); 331 #endif 332 %} 333 334 %% 335 336 program: 337 /* empty */ { 338 /* TODO: Make this an error in C99 mode as well. */ 339 if (!allow_trad && !allow_c99) { 340 /* empty translation unit */ 341 error(272); 342 } else if (allow_c90) { 343 /* empty translation unit */ 344 warning(272); 345 } 346 } 347 | translation_unit 348 ; 349 350 identifier_sym: /* helper for struct/union/enum */ 351 identifier { 352 $$ = getsym($1); 353 } 354 ; 355 356 /* K&R ???, C90 ???, C99 6.4.2.1, C11 ??? */ 357 identifier: 358 T_NAME { 359 debug_step("cgram: name '%s'", $1->sb_name); 360 $$ = $1; 361 } 362 | T_TYPENAME { 363 debug_step("cgram: typename '%s'", $1->sb_name); 364 $$ = $1; 365 } 366 ; 367 368 /* see C99 6.4.5, string literals are joined by 5.1.1.2 */ 369 string: 370 T_STRING 371 | string T_STRING { 372 if (!allow_c90) { 373 /* concatenated strings are illegal in traditional C */ 374 warning(219); 375 } 376 $$ = cat_strings($1, $2); 377 } 378 ; 379 380 /* K&R 7.1, C90 ???, C99 6.5.1, C11 6.5.1 */ 381 primary_expression: 382 T_NAME { 383 bool sys_name, sys_next; 384 sys_name = in_system_header; 385 if (yychar < 0) 386 yychar = yylex(); 387 sys_next = in_system_header; 388 in_system_header = sys_name; 389 $$ = build_name(getsym($1), yychar == T_LPAREN); 390 in_system_header = sys_next; 391 } 392 | T_CON { 393 $$ = build_constant(gettyp($1->v_tspec), $1); 394 } 395 | string { 396 $$ = build_string($1); 397 } 398 | T_LPAREN expression T_RPAREN { 399 if ($2 != NULL) 400 $2->tn_parenthesized = true; 401 $$ = $2; 402 } 403 | generic_selection 404 /* GCC primary-expression, see c_parser_postfix_expression */ 405 | T_BUILTIN_OFFSETOF T_LPAREN type_name T_COMMA identifier T_RPAREN { 406 symtyp = FMEMBER; 407 $$ = build_offsetof($3, getsym($5)); 408 } 409 ; 410 411 /* K&R ---, C90 ---, C99 ---, C11 6.5.1.1 */ 412 generic_selection: 413 T_GENERIC T_LPAREN assignment_expression T_COMMA 414 generic_assoc_list T_RPAREN { 415 /* generic selection requires C11 or later */ 416 c11ism(345); 417 $$ = build_generic_selection($3, $5); 418 } 419 ; 420 421 /* K&R ---, C90 ---, C99 ---, C11 6.5.1.1 */ 422 generic_assoc_list: 423 generic_association 424 | generic_assoc_list T_COMMA generic_association { 425 $3->ga_prev = $1; 426 $$ = $3; 427 } 428 ; 429 430 /* K&R ---, C90 ---, C99 ---, C11 6.5.1.1 */ 431 generic_association: 432 type_name T_COLON assignment_expression { 433 $$ = block_zero_alloc(sizeof(*$$)); 434 $$->ga_arg = $1; 435 $$->ga_result = $3; 436 } 437 | T_DEFAULT T_COLON assignment_expression { 438 $$ = block_zero_alloc(sizeof(*$$)); 439 $$->ga_arg = NULL; 440 $$->ga_result = $3; 441 } 442 ; 443 444 /* K&R 7.1, C90 ???, C99 6.5.2, C11 6.5.2 */ 445 postfix_expression: 446 primary_expression 447 | postfix_expression T_LBRACK sys expression T_RBRACK { 448 $$ = build_unary(INDIR, $3, build_binary($1, PLUS, $3, $4)); 449 } 450 | postfix_expression T_LPAREN sys T_RPAREN { 451 $$ = build_function_call($1, $3, NULL); 452 } 453 | postfix_expression T_LPAREN sys argument_expression_list T_RPAREN { 454 $$ = build_function_call($1, $3, $4); 455 } 456 | postfix_expression point_or_arrow sys T_NAME { 457 $$ = build_member_access($1, $2, $3, $4); 458 } 459 | postfix_expression T_INCDEC sys { 460 $$ = build_unary($2 == INC ? INCAFT : DECAFT, $3, $1); 461 } 462 | T_LPAREN type_name T_RPAREN { /* C99 6.5.2.5 "Compound literals" */ 463 sym_t *tmp = mktempsym($2); 464 begin_initialization(tmp); 465 cgram_declare(tmp, true, NULL); 466 } init_lbrace initializer_list comma_opt init_rbrace { 467 if (!allow_c99) 468 /* compound literals are a C99/GCC extension */ 469 gnuism(319); 470 $$ = build_name(*current_initsym(), false); 471 end_initialization(); 472 } 473 | T_LPAREN compound_statement_lbrace { 474 begin_statement_expr(); 475 } gcc_statement_expr_list { 476 do_statement_expr($4); 477 } compound_statement_rbrace T_RPAREN { 478 $$ = end_statement_expr(); 479 } 480 ; 481 482 comma_opt: /* helper for 'postfix_expression' */ 483 /* empty */ 484 | T_COMMA 485 ; 486 487 /* 488 * The inner part of a GCC statement-expression of the form ({ ... }). 489 * 490 * https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html 491 */ 492 gcc_statement_expr_list: 493 gcc_statement_expr_item 494 | gcc_statement_expr_list gcc_statement_expr_item { 495 $$ = $2; 496 } 497 ; 498 499 gcc_statement_expr_item: 500 declaration_or_error { 501 clear_warning_flags(); 502 $$ = NULL; 503 } 504 | non_expr_statement { 505 $$ = expr_alloc_tnode(); 506 $$->tn_type = gettyp(VOID); 507 } 508 | expression T_SEMI { 509 if ($1 == NULL) { /* in case of syntax errors */ 510 $$ = expr_alloc_tnode(); 511 $$->tn_type = gettyp(VOID); 512 } else { 513 /* XXX: do that only on the last name */ 514 if ($1->tn_op == NAME) 515 $1->tn_sym->s_used = true; 516 expr($1, false, false, false, false); 517 seen_fallthrough = false; 518 $$ = $1; 519 } 520 } 521 ; 522 523 point_or_arrow: /* helper for 'postfix_expression' */ 524 T_POINT { 525 symtyp = FMEMBER; 526 $$ = POINT; 527 } 528 | T_ARROW { 529 symtyp = FMEMBER; 530 $$ = ARROW; 531 } 532 ; 533 534 /* K&R 7.1, C90 ???, C99 6.5.2, C11 6.5.2 */ 535 argument_expression_list: 536 assignment_expression { 537 $$ = build_function_argument(NULL, $1); 538 } 539 | argument_expression_list T_COMMA assignment_expression { 540 $$ = build_function_argument($1, $3); 541 } 542 ; 543 544 /* K&R 7.2, C90 ???, C99 6.5.3, C11 6.5.3 */ 545 unary_expression: 546 postfix_expression 547 | T_INCDEC sys unary_expression { 548 $$ = build_unary($1 == INC ? INCBEF : DECBEF, $2, $3); 549 } 550 | T_AMPER sys cast_expression { 551 $$ = build_unary(ADDR, $2, $3); 552 } 553 | T_ASTERISK sys cast_expression { 554 $$ = build_unary(INDIR, $2, $3); 555 } 556 | T_ADDITIVE sys cast_expression { 557 if (!allow_c90 && $1 == PLUS) { 558 /* unary '+' is illegal in traditional C */ 559 warning(100); 560 } 561 $$ = build_unary($1 == PLUS ? UPLUS : UMINUS, $2, $3); 562 } 563 | T_COMPLEMENT sys cast_expression { 564 $$ = build_unary(COMPL, $2, $3); 565 } 566 | T_LOGNOT sys cast_expression { 567 $$ = build_unary(NOT, $2, $3); 568 } 569 | T_REAL sys cast_expression { /* GCC c_parser_unary_expression */ 570 $$ = build_unary(REAL, $2, $3); 571 } 572 | T_IMAG sys cast_expression { /* GCC c_parser_unary_expression */ 573 $$ = build_unary(IMAG, $2, $3); 574 } 575 | T_EXTENSION cast_expression { /* GCC c_parser_unary_expression */ 576 $$ = $2; 577 } 578 | T_SIZEOF unary_expression { 579 $$ = $2 == NULL ? NULL : build_sizeof($2->tn_type); 580 if ($$ != NULL) 581 check_expr_misc($2, 582 false, false, false, false, false, true); 583 } 584 | T_SIZEOF T_LPAREN type_name T_RPAREN { 585 $$ = build_sizeof($3); 586 } 587 | T_ALIGNOF unary_expression { 588 /* non type argument to alignof is a GCC extension */ 589 gnuism(349); 590 lint_assert($2 != NULL); 591 $$ = build_alignof($2->tn_type); 592 } 593 /* K&R ---, C90 ---, C99 ---, C11 6.5.3 */ 594 | T_ALIGNOF T_LPAREN type_name T_RPAREN { 595 /* TODO: c11ism */ 596 $$ = build_alignof($3); 597 } 598 ; 599 600 /* The rule 'unary_operator' is inlined into unary_expression. */ 601 602 /* K&R 7.2, C90 ???, C99 6.5.4, C11 6.5.4 */ 603 cast_expression: 604 unary_expression 605 | T_LPAREN type_name T_RPAREN cast_expression { 606 $$ = cast($4, $2); 607 } 608 ; 609 610 expression_opt: 611 /* empty */ { 612 $$ = NULL; 613 } 614 | expression 615 ; 616 617 /* 'conditional_expression' also implements 'multiplicative_expression'. */ 618 /* 'conditional_expression' also implements 'additive_expression'. */ 619 /* 'conditional_expression' also implements 'shift_expression'. */ 620 /* 'conditional_expression' also implements 'relational_expression'. */ 621 /* 'conditional_expression' also implements 'equality_expression'. */ 622 /* 'conditional_expression' also implements 'AND_expression'. */ 623 /* 'conditional_expression' also implements 'exclusive_OR_expression'. */ 624 /* 'conditional_expression' also implements 'inclusive_OR_expression'. */ 625 /* 'conditional_expression' also implements 'logical_AND_expression'. */ 626 /* 'conditional_expression' also implements 'logical_OR_expression'. */ 627 /* K&R ???, C90 ???, C99 6.5.5 to 6.5.15, C11 6.5.5 to 6.5.15 */ 628 conditional_expression: 629 cast_expression 630 | conditional_expression T_ASTERISK sys conditional_expression { 631 $$ = build_binary($1, MULT, $3, $4); 632 } 633 | conditional_expression T_MULTIPLICATIVE sys conditional_expression { 634 $$ = build_binary($1, $2, $3, $4); 635 } 636 | conditional_expression T_ADDITIVE sys conditional_expression { 637 $$ = build_binary($1, $2, $3, $4); 638 } 639 | conditional_expression T_SHIFT sys conditional_expression { 640 $$ = build_binary($1, $2, $3, $4); 641 } 642 | conditional_expression T_RELATIONAL sys conditional_expression { 643 $$ = build_binary($1, $2, $3, $4); 644 } 645 | conditional_expression T_EQUALITY sys conditional_expression { 646 $$ = build_binary($1, $2, $3, $4); 647 } 648 | conditional_expression T_AMPER sys conditional_expression { 649 $$ = build_binary($1, BITAND, $3, $4); 650 } 651 | conditional_expression T_BITXOR sys conditional_expression { 652 $$ = build_binary($1, BITXOR, $3, $4); 653 } 654 | conditional_expression T_BITOR sys conditional_expression { 655 $$ = build_binary($1, BITOR, $3, $4); 656 } 657 | conditional_expression T_LOGAND sys conditional_expression { 658 $$ = build_binary($1, LOGAND, $3, $4); 659 } 660 | conditional_expression T_LOGOR sys conditional_expression { 661 $$ = build_binary($1, LOGOR, $3, $4); 662 } 663 | conditional_expression T_QUEST sys 664 expression T_COLON sys conditional_expression { 665 $$ = build_binary($1, QUEST, $3, 666 build_binary($4, COLON, $6, $7)); 667 } 668 ; 669 670 /* K&R ???, C90 ???, C99 6.5.16, C11 6.5.16 */ 671 assignment_expression: 672 conditional_expression 673 | unary_expression T_ASSIGN sys assignment_expression { 674 $$ = build_binary($1, ASSIGN, $3, $4); 675 } 676 | unary_expression T_OPASSIGN sys assignment_expression { 677 $$ = build_binary($1, $2, $3, $4); 678 } 679 ; 680 681 /* K&R ???, C90 ???, C99 6.5.17, C11 6.5.17 */ 682 expression: 683 assignment_expression 684 | expression T_COMMA sys assignment_expression { 685 $$ = build_binary($1, COMMA, $3, $4); 686 } 687 ; 688 689 constant_expr: /* C99 6.6 */ 690 conditional_expression 691 ; 692 693 declaration_or_error: 694 declaration 695 | error T_SEMI 696 ; 697 698 declaration: /* C99 6.7 */ 699 begin_type_declmods end_type T_SEMI { 700 if (dcs->d_scl == TYPEDEF) { 701 /* typedef declares no type name */ 702 warning(72); 703 } else { 704 /* empty declaration */ 705 warning(2); 706 } 707 } 708 | begin_type_declmods end_type notype_init_declarators T_SEMI { 709 if (dcs->d_scl == TYPEDEF) { 710 /* syntax error '%s' */ 711 error(249, "missing base type for typedef"); 712 } else { 713 /* old-style declaration; add 'int' */ 714 error(1); 715 } 716 } 717 | begin_type_declaration_specifiers end_type T_SEMI { 718 if (dcs->d_scl == TYPEDEF) { 719 /* typedef declares no type name */ 720 warning(72); 721 } else if (!dcs->d_nonempty_decl) { 722 /* empty declaration */ 723 warning(2); 724 } 725 } 726 | begin_type_declaration_specifiers end_type 727 type_init_declarators T_SEMI 728 | static_assert_declaration 729 ; 730 731 begin_type_declaration_specifiers: /* see C99 6.7 */ 732 begin_type_typespec { 733 dcs_add_type($1); 734 } 735 | begin_type_declmods type_specifier { 736 dcs_add_type($2); 737 } 738 | type_attribute begin_type_declaration_specifiers 739 | begin_type_declaration_specifiers declmod 740 | begin_type_declaration_specifiers notype_type_specifier { 741 dcs_add_type($2); 742 } 743 ; 744 745 begin_type_declmods: /* see C99 6.7 */ 746 begin_type T_QUAL { 747 dcs_add_qualifier($2); 748 } 749 | begin_type T_SCLASS { 750 dcs_add_storage_class($2); 751 } 752 | begin_type_declmods declmod 753 ; 754 755 begin_type_specifier_qualifier_list: /* see C11 6.7.2.1 */ 756 begin_type_specifier_qualifier_list_postfix 757 | type_attribute_list begin_type_specifier_qualifier_list_postfix 758 ; 759 760 begin_type_specifier_qualifier_list_postfix: 761 begin_type_typespec { 762 dcs_add_type($1); 763 } 764 | begin_type_qualifier_list type_specifier { 765 dcs_add_type($2); 766 } 767 | begin_type_specifier_qualifier_list_postfix T_QUAL { 768 dcs_add_qualifier($2); 769 } 770 | begin_type_specifier_qualifier_list_postfix notype_type_specifier { 771 dcs_add_type($2); 772 } 773 | begin_type_specifier_qualifier_list_postfix type_attribute 774 ; 775 776 begin_type_typespec: 777 begin_type notype_type_specifier { 778 $$ = $2; 779 } 780 | begin_type T_TYPENAME { 781 $$ = getsym($2)->s_type; 782 } 783 ; 784 785 begin_type_qualifier_list: 786 begin_type T_QUAL { 787 dcs_add_qualifier($2); 788 } 789 | begin_type_qualifier_list T_QUAL { 790 dcs_add_qualifier($2); 791 } 792 ; 793 794 declmod: 795 T_QUAL { 796 dcs_add_qualifier($1); 797 } 798 | T_SCLASS { 799 dcs_add_storage_class($1); 800 } 801 | type_attribute_list 802 ; 803 804 type_attribute_list_opt: 805 /* empty */ 806 | type_attribute_list 807 ; 808 809 type_attribute_list: 810 type_attribute 811 | type_attribute_list type_attribute 812 ; 813 814 type_attribute_opt: 815 /* empty */ 816 | type_attribute 817 ; 818 819 type_attribute: /* See C11 6.7 declaration-specifiers */ 820 gcc_attribute_specifier 821 | T_ALIGNAS T_LPAREN type_specifier T_RPAREN /* C11 6.7.5 */ 822 | T_ALIGNAS T_LPAREN constant_expr T_RPAREN /* C11 6.7.5 */ 823 | T_PACKED { 824 dcs_add_packed(); 825 } 826 | T_NORETURN 827 ; 828 829 begin_type: 830 /* empty */ { 831 dcs_begin_type(); 832 } 833 ; 834 835 end_type: 836 /* empty */ { 837 dcs_end_type(); 838 } 839 ; 840 841 type_specifier: /* C99 6.7.2 */ 842 notype_type_specifier 843 | T_TYPENAME { 844 $$ = getsym($1)->s_type; 845 } 846 ; 847 848 notype_type_specifier: /* see C99 6.7.2 */ 849 T_TYPE { 850 $$ = gettyp($1); 851 } 852 | T_TYPEOF T_LPAREN expression T_RPAREN { /* GCC extension */ 853 $$ = block_dup_type($3->tn_type); 854 $$->t_typeof = true; 855 } 856 | struct_or_union_specifier { 857 end_declaration_level(); 858 $$ = $1; 859 } 860 | enum_specifier { 861 end_declaration_level(); 862 $$ = $1; 863 } 864 ; 865 866 struct_or_union_specifier: /* C99 6.7.2.1 */ 867 struct_or_union identifier_sym { 868 /* 869 * STDC requires that "struct a;" always introduces 870 * a new tag if "a" is not declared at current level 871 * 872 * yychar is valid because otherwise the parser would not 873 * have been able to decide if it must shift or reduce 874 */ 875 $$ = make_tag_type($2, $1, false, yychar == T_SEMI); 876 } 877 | struct_or_union identifier_sym { 878 dcs->d_tagtyp = make_tag_type($2, $1, true, false); 879 } braced_struct_declaration_list { 880 $$ = complete_tag_struct_or_union(dcs->d_tagtyp, $4); 881 } 882 | struct_or_union { 883 dcs->d_tagtyp = make_tag_type(NULL, $1, true, false); 884 } braced_struct_declaration_list { 885 $$ = complete_tag_struct_or_union(dcs->d_tagtyp, $3); 886 } 887 | struct_or_union error { 888 symtyp = FVFT; 889 $$ = gettyp(INT); 890 } 891 ; 892 893 struct_or_union: /* C99 6.7.2.1 */ 894 T_STRUCT_OR_UNION { 895 symtyp = FTAG; 896 begin_declaration_level($1 == STRUCT ? DK_MOS : DK_MOU); 897 dcs->d_offset_in_bits = 0; 898 dcs->d_sou_align_in_bits = CHAR_SIZE; 899 $$ = $1; 900 } 901 | struct_or_union type_attribute 902 ; 903 904 braced_struct_declaration_list: /* see C99 6.7.2.1 */ 905 struct_declaration_lbrace struct_declaration_list_with_rbrace { 906 $$ = $2; 907 } 908 ; 909 910 struct_declaration_lbrace: /* see C99 6.7.2.1 */ 911 T_LBRACE { 912 symtyp = FVFT; 913 } 914 ; 915 916 struct_declaration_list_with_rbrace: /* see C99 6.7.2.1 */ 917 struct_declaration_list T_RBRACE 918 | T_RBRACE { 919 /* XXX: This is not allowed by any C standard. */ 920 $$ = NULL; 921 } 922 ; 923 924 struct_declaration_list: /* C99 6.7.2.1 */ 925 struct_declaration 926 | struct_declaration_list struct_declaration { 927 $$ = concat_lists($1, $2); 928 } 929 ; 930 931 struct_declaration: /* C99 6.7.2.1 */ 932 begin_type_qualifier_list end_type { 933 /* ^^ There is no check for the missing type-specifier. */ 934 /* too late, i know, but getsym() compensates it */ 935 symtyp = FMEMBER; 936 } notype_struct_declarators type_attribute_opt T_SEMI { 937 symtyp = FVFT; 938 $$ = $4; 939 } 940 | begin_type_specifier_qualifier_list end_type { 941 symtyp = FMEMBER; 942 } type_struct_declarators type_attribute_opt T_SEMI { 943 symtyp = FVFT; 944 $$ = $4; 945 } 946 | begin_type_qualifier_list end_type type_attribute_opt T_SEMI { 947 /* syntax error '%s' */ 948 error(249, "member without type"); 949 $$ = NULL; 950 } 951 | begin_type_specifier_qualifier_list end_type type_attribute_opt 952 T_SEMI { 953 symtyp = FVFT; 954 if (!allow_c11 && !allow_gcc) 955 /* anonymous struct/union members is a C11 feature */ 956 warning(49); 957 if (is_struct_or_union(dcs->d_type->t_tspec)) { 958 $$ = dcs->d_type->t_str->sou_first_member; 959 /* add all the members of the anonymous struct/union */ 960 anonymize($$); 961 } else { 962 /* syntax error '%s' */ 963 error(249, "unnamed member"); 964 $$ = NULL; 965 } 966 } 967 | static_assert_declaration { 968 $$ = NULL; 969 } 970 | error T_SEMI { 971 symtyp = FVFT; 972 $$ = NULL; 973 } 974 ; 975 976 notype_struct_declarators: 977 notype_struct_declarator { 978 $$ = declarator_1_struct_union($1); 979 } 980 | notype_struct_declarators { 981 symtyp = FMEMBER; 982 } T_COMMA type_struct_declarator { 983 $$ = concat_lists($1, declarator_1_struct_union($4)); 984 } 985 ; 986 987 type_struct_declarators: 988 type_struct_declarator { 989 $$ = declarator_1_struct_union($1); 990 } 991 | type_struct_declarators { 992 symtyp = FMEMBER; 993 } T_COMMA type_struct_declarator { 994 $$ = concat_lists($1, declarator_1_struct_union($4)); 995 } 996 ; 997 998 notype_struct_declarator: 999 notype_declarator 1000 | notype_declarator T_COLON constant_expr { /* C99 6.7.2.1 */ 1001 $$ = set_bit_field_width($1, to_int_constant($3, true)); 1002 } 1003 | { 1004 symtyp = FVFT; 1005 } T_COLON constant_expr { /* C99 6.7.2.1 */ 1006 $$ = set_bit_field_width(NULL, to_int_constant($3, true)); 1007 } 1008 ; 1009 1010 type_struct_declarator: 1011 type_declarator 1012 | type_declarator T_COLON constant_expr { 1013 $$ = set_bit_field_width($1, to_int_constant($3, true)); 1014 } 1015 | { 1016 symtyp = FVFT; 1017 } T_COLON constant_expr { 1018 $$ = set_bit_field_width(NULL, to_int_constant($3, true)); 1019 } 1020 ; 1021 1022 /* K&R ---, C90 6.5.2.2, C99 6.7.2.2, C11 6.7.2.2 */ 1023 enum_specifier: /* C99 6.7.2.2 */ 1024 enum gcc_attribute_specifier_list_opt identifier_sym { 1025 $$ = make_tag_type($3, ENUM, false, false); 1026 } 1027 | enum gcc_attribute_specifier_list_opt identifier_sym { 1028 dcs->d_tagtyp = make_tag_type($3, ENUM, true, false); 1029 } enum_declaration /*gcc_attribute_specifier_list_opt*/ { 1030 $$ = complete_tag_enum(dcs->d_tagtyp, $5); 1031 } 1032 | enum gcc_attribute_specifier_list_opt { 1033 dcs->d_tagtyp = make_tag_type(NULL, ENUM, true, false); 1034 } enum_declaration /*gcc_attribute_specifier_list_opt*/ { 1035 $$ = complete_tag_enum(dcs->d_tagtyp, $4); 1036 } 1037 | enum error { 1038 symtyp = FVFT; 1039 $$ = gettyp(INT); 1040 } 1041 ; 1042 1043 enum: /* helper for C99 6.7.2.2 */ 1044 T_ENUM { 1045 symtyp = FTAG; 1046 begin_declaration_level(DK_ENUM_CONST); 1047 } 1048 ; 1049 1050 enum_declaration: /* helper for C99 6.7.2.2 */ 1051 enum_decl_lbrace enums_with_opt_comma T_RBRACE { 1052 $$ = $2; 1053 } 1054 ; 1055 1056 enum_decl_lbrace: /* helper for C99 6.7.2.2 */ 1057 T_LBRACE { 1058 symtyp = FVFT; 1059 enumval = 0; 1060 } 1061 ; 1062 1063 enums_with_opt_comma: /* helper for C99 6.7.2.2 */ 1064 enumerator_list 1065 | enumerator_list T_COMMA { 1066 if (!allow_c99 && !allow_trad) { 1067 /* trailing ',' prohibited in enum declaration */ 1068 error(54); 1069 } else { 1070 /* trailing ',' prohibited in enum declaration */ 1071 c99ism(54); 1072 } 1073 $$ = $1; 1074 } 1075 ; 1076 1077 enumerator_list: /* C99 6.7.2.2 */ 1078 enumerator 1079 | enumerator_list T_COMMA enumerator { 1080 $$ = concat_lists($1, $3); 1081 } 1082 | error { 1083 $$ = NULL; 1084 } 1085 ; 1086 1087 enumerator: /* C99 6.7.2.2 */ 1088 identifier_sym gcc_attribute_specifier_list_opt { 1089 $$ = enumeration_constant($1, enumval, true); 1090 } 1091 | identifier_sym gcc_attribute_specifier_list_opt 1092 T_ASSIGN constant_expr { 1093 $$ = enumeration_constant($1, to_int_constant($4, true), 1094 false); 1095 } 1096 ; 1097 1098 type_qualifier: /* C99 6.7.3 */ 1099 T_QUAL { 1100 $$ = xcalloc(1, sizeof(*$$)); 1101 if ($1 == CONST) 1102 $$->p_const = true; 1103 if ($1 == VOLATILE) 1104 $$->p_volatile = true; 1105 } 1106 ; 1107 1108 pointer: /* C99 6.7.5 */ 1109 asterisk type_qualifier_list_opt { 1110 $$ = merge_qualified_pointer($1, $2); 1111 } 1112 | asterisk type_qualifier_list_opt pointer { 1113 $$ = merge_qualified_pointer($1, $2); 1114 $$ = merge_qualified_pointer($$, $3); 1115 } 1116 ; 1117 1118 asterisk: /* helper for 'pointer' */ 1119 T_ASTERISK { 1120 $$ = xcalloc(1, sizeof(*$$)); 1121 $$->p_pointer = true; 1122 } 1123 ; 1124 1125 type_qualifier_list_opt: /* see C99 6.7.5 */ 1126 /* empty */ { 1127 $$ = NULL; 1128 } 1129 | type_qualifier_list 1130 ; 1131 1132 type_qualifier_list: /* C99 6.7.5 */ 1133 type_qualifier 1134 | type_qualifier_list type_qualifier { 1135 $$ = merge_qualified_pointer($1, $2); 1136 } 1137 ; 1138 1139 /* 1140 * For an explanation of 'notype' in the following rules, see 1141 * https://www.gnu.org/software/bison/manual/bison.html#Semantic-Tokens. 1142 */ 1143 1144 notype_init_declarators: 1145 notype_init_declarator 1146 | notype_init_declarators T_COMMA type_init_declarator 1147 ; 1148 1149 type_init_declarators: 1150 type_init_declarator 1151 | type_init_declarators T_COMMA type_init_declarator 1152 ; 1153 1154 notype_init_declarator: 1155 notype_declarator asm_or_symbolrename_opt { 1156 cgram_declare($1, false, $2); 1157 check_size($1); 1158 } 1159 | notype_declarator asm_or_symbolrename_opt { 1160 begin_initialization($1); 1161 cgram_declare($1, true, $2); 1162 } T_ASSIGN initializer { 1163 check_size($1); 1164 end_initialization(); 1165 } 1166 ; 1167 1168 type_init_declarator: 1169 type_declarator asm_or_symbolrename_opt { 1170 cgram_declare($1, false, $2); 1171 check_size($1); 1172 } 1173 | type_declarator asm_or_symbolrename_opt { 1174 begin_initialization($1); 1175 cgram_declare($1, true, $2); 1176 } T_ASSIGN initializer { 1177 check_size($1); 1178 end_initialization(); 1179 } 1180 ; 1181 1182 notype_declarator: 1183 notype_direct_declarator 1184 | pointer notype_direct_declarator { 1185 $$ = add_pointer($2, $1); 1186 } 1187 ; 1188 1189 type_declarator: 1190 type_direct_declarator 1191 | pointer type_direct_declarator { 1192 $$ = add_pointer($2, $1); 1193 } 1194 ; 1195 1196 notype_direct_declarator: 1197 type_attribute_list_opt T_NAME { 1198 $$ = declarator_name(getsym($2)); 1199 } 1200 | type_attribute_list_opt T_LPAREN type_declarator T_RPAREN { 1201 $$ = $3; 1202 } 1203 | notype_direct_declarator T_LBRACK array_size_opt T_RBRACK { 1204 $$ = add_array($1, $3.has_dim, $3.dim); 1205 } 1206 | notype_direct_declarator param_list asm_or_symbolrename_opt { 1207 $$ = add_function(symbolrename($1, $3), $2); 1208 end_declaration_level(); 1209 block_level--; 1210 } 1211 | notype_direct_declarator type_attribute 1212 ; 1213 1214 type_direct_declarator: 1215 type_attribute_list_opt identifier { 1216 $$ = declarator_name(getsym($2)); 1217 } 1218 | type_attribute_list_opt T_LPAREN type_declarator T_RPAREN { 1219 $$ = $3; 1220 } 1221 | type_direct_declarator T_LBRACK array_size_opt T_RBRACK { 1222 $$ = add_array($1, $3.has_dim, $3.dim); 1223 } 1224 | type_direct_declarator param_list asm_or_symbolrename_opt { 1225 $$ = add_function(symbolrename($1, $3), $2); 1226 end_declaration_level(); 1227 block_level--; 1228 } 1229 | type_direct_declarator type_attribute 1230 ; 1231 1232 /* 1233 * The two distinct rules type_param_declarator and notype_param_declarator 1234 * avoid a conflict in argument lists. A typename enclosed in parentheses is 1235 * always treated as a typename, not an argument name. For example, after 1236 * "typedef double a;", the declaration "f(int (a));" is interpreted as 1237 * "f(int (double));", not "f(int a);". 1238 */ 1239 type_param_declarator: 1240 direct_param_declarator 1241 | pointer direct_param_declarator { 1242 $$ = add_pointer($2, $1); 1243 } 1244 ; 1245 1246 notype_param_declarator: 1247 direct_notype_param_declarator 1248 | pointer direct_notype_param_declarator { 1249 $$ = add_pointer($2, $1); 1250 } 1251 ; 1252 1253 direct_param_declarator: 1254 identifier type_attribute_list { 1255 $$ = declarator_name(getsym($1)); 1256 } 1257 | identifier { 1258 $$ = declarator_name(getsym($1)); 1259 } 1260 | T_LPAREN notype_param_declarator T_RPAREN { 1261 $$ = $2; 1262 } 1263 | direct_param_declarator T_LBRACK array_size_opt T_RBRACK 1264 gcc_attribute_specifier_list_opt { 1265 $$ = add_array($1, $3.has_dim, $3.dim); 1266 } 1267 | direct_param_declarator param_list asm_or_symbolrename_opt { 1268 $$ = add_function(symbolrename($1, $3), $2); 1269 end_declaration_level(); 1270 block_level--; 1271 } 1272 ; 1273 1274 direct_notype_param_declarator: 1275 identifier { 1276 $$ = declarator_name(getsym($1)); 1277 } 1278 | T_LPAREN notype_param_declarator T_RPAREN { 1279 $$ = $2; 1280 } 1281 | direct_notype_param_declarator T_LBRACK array_size_opt T_RBRACK { 1282 $$ = add_array($1, $3.has_dim, $3.dim); 1283 } 1284 | direct_notype_param_declarator param_list asm_or_symbolrename_opt { 1285 $$ = add_function(symbolrename($1, $3), $2); 1286 end_declaration_level(); 1287 block_level--; 1288 } 1289 ; 1290 1291 param_list: 1292 id_list_lparen identifier_list T_RPAREN { 1293 $$ = $2; 1294 } 1295 | abstract_decl_param_list 1296 ; 1297 1298 id_list_lparen: 1299 T_LPAREN { 1300 block_level++; 1301 begin_declaration_level(DK_PROTO_ARG); 1302 } 1303 ; 1304 1305 array_size_opt: 1306 /* empty */ { 1307 $$.has_dim = false; 1308 $$.dim = 0; 1309 } 1310 | T_ASTERISK { 1311 /* since C99; variable length array of unspecified size */ 1312 $$.has_dim = false; /* TODO: maybe change to true */ 1313 $$.dim = 0; /* just as a placeholder */ 1314 } 1315 | array_size { 1316 $$.has_dim = true; 1317 $$.dim = $1 == NULL ? 0 : to_int_constant($1, false); 1318 } 1319 ; 1320 1321 array_size: 1322 type_qualifier_list_opt T_SCLASS constant_expr { 1323 /* C11 6.7.6.3p7 */ 1324 if ($2 != STATIC) 1325 yyerror("Bad attribute"); 1326 /* static array size is a C11 extension */ 1327 c11ism(343); 1328 $$ = $3; 1329 } 1330 | T_QUAL { 1331 /* C11 6.7.6.2 */ 1332 if ($1 != RESTRICT) 1333 yyerror("Bad attribute"); 1334 $$ = NULL; 1335 } 1336 | constant_expr 1337 ; 1338 1339 identifier_list: /* C99 6.7.5 */ 1340 T_NAME { 1341 $$ = old_style_function_name(getsym($1)); 1342 } 1343 | identifier_list T_COMMA T_NAME { 1344 $$ = concat_lists($1, old_style_function_name(getsym($3))); 1345 } 1346 | identifier_list error 1347 ; 1348 1349 /* XXX: C99 requires an additional specifier-qualifier-list. */ 1350 type_name: /* C99 6.7.6 */ 1351 { 1352 begin_declaration_level(DK_ABSTRACT); 1353 } abstract_declaration { 1354 end_declaration_level(); 1355 $$ = $2->s_type; 1356 } 1357 ; 1358 1359 abstract_declaration: /* specific to lint */ 1360 begin_type_qualifier_list end_type { 1361 $$ = declare_1_abstract(abstract_name()); 1362 } 1363 | begin_type_specifier_qualifier_list end_type { 1364 $$ = declare_1_abstract(abstract_name()); 1365 } 1366 | begin_type_qualifier_list end_type abstract_declarator { 1367 $$ = declare_1_abstract($3); 1368 } 1369 | begin_type_specifier_qualifier_list end_type abstract_declarator { 1370 $$ = declare_1_abstract($3); 1371 } 1372 ; 1373 1374 /* K&R 8.7, C90 ???, C99 6.7.6, C11 6.7.7 */ 1375 /* In K&R, abstract-declarator could be empty and was still simpler. */ 1376 abstract_declarator: 1377 pointer { 1378 $$ = add_pointer(abstract_name(), $1); 1379 } 1380 | direct_abstract_declarator 1381 | pointer direct_abstract_declarator { 1382 $$ = add_pointer($2, $1); 1383 } 1384 | type_attribute_list direct_abstract_declarator { 1385 $$ = $2; 1386 } 1387 | pointer type_attribute_list direct_abstract_declarator { 1388 $$ = add_pointer($3, $1); 1389 } 1390 ; 1391 1392 /* K&R ---, C90 ???, C99 6.7.6, C11 6.7.7 */ 1393 direct_abstract_declarator: 1394 /* TODO: sort rules according to C99 */ 1395 T_LPAREN abstract_declarator T_RPAREN { 1396 $$ = $2; 1397 } 1398 | T_LBRACK array_size_opt T_RBRACK { 1399 $$ = add_array(abstract_name(), $2.has_dim, $2.dim); 1400 } 1401 | direct_abstract_declarator T_LBRACK array_size_opt T_RBRACK { 1402 $$ = add_array($1, $3.has_dim, $3.dim); 1403 } 1404 | abstract_decl_param_list asm_or_symbolrename_opt { 1405 $$ = add_function(symbolrename(abstract_name(), $2), $1); 1406 end_declaration_level(); 1407 block_level--; 1408 } 1409 | direct_abstract_declarator abstract_decl_param_list 1410 asm_or_symbolrename_opt { 1411 $$ = add_function(symbolrename($1, $3), $2); 1412 end_declaration_level(); 1413 block_level--; 1414 } 1415 | direct_abstract_declarator type_attribute_list 1416 ; 1417 1418 abstract_decl_param_list: /* specific to lint */ 1419 abstract_decl_lparen T_RPAREN type_attribute_opt { 1420 $$ = NULL; 1421 } 1422 | abstract_decl_lparen vararg_parameter_type_list T_RPAREN 1423 type_attribute_opt { 1424 dcs->d_proto = true; 1425 $$ = $2; 1426 } 1427 | abstract_decl_lparen error T_RPAREN type_attribute_opt { 1428 $$ = NULL; 1429 } 1430 ; 1431 1432 abstract_decl_lparen: /* specific to lint */ 1433 T_LPAREN { 1434 block_level++; 1435 begin_declaration_level(DK_PROTO_ARG); 1436 } 1437 ; 1438 1439 vararg_parameter_type_list: /* specific to lint */ 1440 parameter_type_list 1441 | parameter_type_list T_COMMA T_ELLIPSIS { 1442 dcs->d_vararg = true; 1443 $$ = $1; 1444 } 1445 | T_ELLIPSIS { 1446 /* TODO: C99 6.7.5 makes this an error as well. */ 1447 if (!allow_trad && !allow_c99) { 1448 /* ANSI C requires formal parameter before '...' */ 1449 error(84); 1450 } else if (allow_c90) { 1451 /* ANSI C requires formal parameter before '...' */ 1452 warning(84); 1453 } 1454 dcs->d_vararg = true; 1455 $$ = NULL; 1456 } 1457 ; 1458 1459 /* XXX: C99 6.7.5 defines the same name, but it looks different. */ 1460 parameter_type_list: 1461 parameter_declaration 1462 | parameter_type_list T_COMMA parameter_declaration { 1463 $$ = concat_lists($1, $3); 1464 } 1465 ; 1466 1467 /* XXX: C99 6.7.5 defines the same name, but it looks completely different. */ 1468 parameter_declaration: 1469 begin_type_declmods end_type { 1470 /* ^^ There is no check for the missing type-specifier. */ 1471 $$ = declare_argument(abstract_name(), false); 1472 } 1473 | begin_type_declaration_specifiers end_type { 1474 $$ = declare_argument(abstract_name(), false); 1475 } 1476 | begin_type_declmods end_type notype_param_declarator { 1477 /* ^^ There is no check for the missing type-specifier. */ 1478 $$ = declare_argument($3, false); 1479 } 1480 /* 1481 * type_param_declarator is needed because of following conflict: 1482 * "typedef int a; f(int (a));" could be parsed as 1483 * "function with argument a of type int", or 1484 * "function with an abstract argument of type function". 1485 * This grammar realizes the second case. 1486 */ 1487 | begin_type_declaration_specifiers end_type type_param_declarator { 1488 $$ = declare_argument($3, false); 1489 } 1490 | begin_type_declmods end_type abstract_declarator { 1491 /* ^^ There is no check for the missing type-specifier. */ 1492 $$ = declare_argument($3, false); 1493 } 1494 | begin_type_declaration_specifiers end_type abstract_declarator { 1495 $$ = declare_argument($3, false); 1496 } 1497 ; 1498 1499 initializer: /* C99 6.7.8 "Initialization" */ 1500 assignment_expression { 1501 init_expr($1); 1502 } 1503 | init_lbrace init_rbrace { 1504 /* XXX: Empty braces are not covered by C99 6.7.8. */ 1505 } 1506 | init_lbrace initializer_list comma_opt init_rbrace 1507 /* XXX: What is this error handling for? */ 1508 | error 1509 ; 1510 1511 initializer_list: /* C99 6.7.8 "Initialization" */ 1512 initializer_list_item 1513 | initializer_list T_COMMA initializer_list_item 1514 ; 1515 1516 initializer_list_item: /* helper */ 1517 designation initializer 1518 | initializer 1519 ; 1520 1521 designation: /* C99 6.7.8 "Initialization" */ 1522 begin_designation designator_list T_ASSIGN 1523 | identifier T_COLON { 1524 /* GCC style struct or union member name in initializer */ 1525 gnuism(315); 1526 begin_designation(); 1527 add_designator_member($1); 1528 } 1529 ; 1530 1531 begin_designation: /* lint-specific helper */ 1532 /* empty */ { 1533 begin_designation(); 1534 } 1535 ; 1536 1537 designator_list: /* C99 6.7.8 "Initialization" */ 1538 designator 1539 | designator_list designator 1540 ; 1541 1542 designator: /* C99 6.7.8 "Initialization" */ 1543 T_LBRACK range T_RBRACK { 1544 if (!allow_c99) 1545 /* array initializer with designators is a C99 ... */ 1546 warning(321); 1547 add_designator_subscript($2); 1548 } 1549 | T_POINT identifier { 1550 if (!allow_c99) 1551 /* struct or union member name in initializer is ... */ 1552 warning(313); 1553 add_designator_member($2); 1554 } 1555 ; 1556 1557 static_assert_declaration: 1558 T_STATIC_ASSERT T_LPAREN constant_expr T_COMMA T_STRING T_RPAREN T_SEMI /* C11 */ 1559 | T_STATIC_ASSERT T_LPAREN constant_expr T_RPAREN T_SEMI /* C23 */ 1560 ; 1561 1562 range: 1563 constant_expr { 1564 $$.lo = to_int_constant($1, true); 1565 $$.hi = $$.lo; 1566 } 1567 | constant_expr T_ELLIPSIS constant_expr { 1568 $$.lo = to_int_constant($1, true); 1569 $$.hi = to_int_constant($3, true); 1570 /* initialization with '[a...b]' is a GCC extension */ 1571 gnuism(340); 1572 } 1573 ; 1574 1575 init_lbrace: /* helper */ 1576 T_LBRACE { 1577 init_lbrace(); 1578 } 1579 ; 1580 1581 init_rbrace: /* helper */ 1582 T_RBRACE { 1583 init_rbrace(); 1584 } 1585 ; 1586 1587 asm_or_symbolrename_opt: /* GCC extensions */ 1588 /* empty */ { 1589 $$ = NULL; 1590 } 1591 | T_ASM T_LPAREN T_STRING T_RPAREN gcc_attribute_specifier_list_opt { 1592 freeyyv(&$3, T_STRING); 1593 $$ = NULL; 1594 } 1595 | T_SYMBOLRENAME T_LPAREN T_NAME T_RPAREN 1596 gcc_attribute_specifier_list_opt { 1597 $$ = $3; 1598 } 1599 ; 1600 1601 statement: /* C99 6.8 */ 1602 expression_statement 1603 | non_expr_statement 1604 ; 1605 1606 non_expr_statement: /* helper for C99 6.8 */ 1607 gcc_attribute_specifier /* ((__fallthrough__)) */ T_SEMI 1608 | labeled_statement 1609 | compound_statement 1610 | selection_statement 1611 | iteration_statement 1612 | jump_statement { 1613 seen_fallthrough = false; 1614 } 1615 | asm_statement 1616 ; 1617 1618 labeled_statement: /* C99 6.8.1 */ 1619 label gcc_attribute_specifier_list_opt statement 1620 ; 1621 1622 label: 1623 T_NAME T_COLON { 1624 symtyp = FLABEL; 1625 named_label(getsym($1)); 1626 } 1627 | T_CASE constant_expr T_COLON { 1628 case_label($2); 1629 seen_fallthrough = true; 1630 } 1631 | T_CASE constant_expr T_ELLIPSIS constant_expr T_COLON { 1632 /* XXX: We don't fill all cases */ 1633 case_label($2); 1634 seen_fallthrough = true; 1635 } 1636 | T_DEFAULT T_COLON { 1637 default_label(); 1638 seen_fallthrough = true; 1639 } 1640 ; 1641 1642 compound_statement: /* C99 6.8.2 */ 1643 compound_statement_lbrace compound_statement_rbrace 1644 | compound_statement_lbrace block_item_list compound_statement_rbrace 1645 ; 1646 1647 compound_statement_lbrace: 1648 T_LBRACE { 1649 block_level++; 1650 mem_block_level++; 1651 begin_declaration_level(DK_AUTO); 1652 } 1653 ; 1654 1655 compound_statement_rbrace: 1656 T_RBRACE { 1657 end_declaration_level(); 1658 level_free_all(mem_block_level); 1659 mem_block_level--; 1660 block_level--; 1661 seen_fallthrough = false; 1662 } 1663 ; 1664 1665 block_item_list: /* C99 6.8.2 */ 1666 block_item 1667 | block_item_list block_item { 1668 if ($1 && !$2) 1669 /* declarations after statements is a C99 feature */ 1670 c99ism(327); 1671 $$ = $1 || $2; 1672 } 1673 ; 1674 1675 block_item: /* C99 6.8.2 */ 1676 declaration_or_error { 1677 $$ = false; 1678 restore_warning_flags(); 1679 } 1680 | statement { 1681 $$ = true; 1682 restore_warning_flags(); 1683 } 1684 ; 1685 1686 expression_statement: /* C99 6.8.3 */ 1687 expression T_SEMI { 1688 expr($1, false, false, false, false); 1689 seen_fallthrough = false; 1690 } 1691 | T_SEMI { 1692 check_statement_reachable(); 1693 seen_fallthrough = false; 1694 } 1695 ; 1696 1697 selection_statement: /* C99 6.8.4 */ 1698 if_without_else %prec T_THEN { 1699 save_warning_flags(); 1700 if2(); 1701 if3(false); 1702 } 1703 | if_without_else T_ELSE { 1704 save_warning_flags(); 1705 if2(); 1706 } statement { 1707 clear_warning_flags(); 1708 if3(true); 1709 } 1710 | if_without_else T_ELSE error { 1711 clear_warning_flags(); 1712 if3(false); 1713 } 1714 | switch_expr statement { 1715 clear_warning_flags(); 1716 switch2(); 1717 } 1718 | switch_expr error { 1719 clear_warning_flags(); 1720 switch2(); 1721 } 1722 ; 1723 1724 if_without_else: /* see C99 6.8.4 */ 1725 if_expr statement 1726 | if_expr error 1727 ; 1728 1729 if_expr: /* see C99 6.8.4 */ 1730 T_IF T_LPAREN expression T_RPAREN { 1731 if1($3); 1732 clear_warning_flags(); 1733 } 1734 ; 1735 1736 switch_expr: /* see C99 6.8.4 */ 1737 T_SWITCH T_LPAREN expression T_RPAREN { 1738 switch1($3); 1739 clear_warning_flags(); 1740 } 1741 ; 1742 1743 iteration_statement: /* C99 6.8.5 */ 1744 while_expr statement { 1745 clear_warning_flags(); 1746 while2(); 1747 } 1748 | while_expr error { 1749 clear_warning_flags(); 1750 while2(); 1751 } 1752 | do_statement do_while_expr { 1753 do2($2); 1754 seen_fallthrough = false; 1755 } 1756 | do error { 1757 clear_warning_flags(); 1758 do2(NULL); 1759 } 1760 | for_exprs statement { 1761 clear_warning_flags(); 1762 for2(); 1763 end_declaration_level(); 1764 block_level--; 1765 } 1766 | for_exprs error { 1767 clear_warning_flags(); 1768 for2(); 1769 end_declaration_level(); 1770 block_level--; 1771 } 1772 ; 1773 1774 while_expr: /* see C99 6.8.5 */ 1775 T_WHILE T_LPAREN expression T_RPAREN { 1776 while1($3); 1777 clear_warning_flags(); 1778 } 1779 ; 1780 1781 do_statement: /* see C99 6.8.5 */ 1782 do statement { 1783 clear_warning_flags(); 1784 } 1785 ; 1786 1787 do: /* see C99 6.8.5 */ 1788 T_DO { 1789 do1(); 1790 } 1791 ; 1792 1793 do_while_expr: /* see C99 6.8.5 */ 1794 T_WHILE T_LPAREN expression T_RPAREN T_SEMI { 1795 $$ = $3; 1796 } 1797 ; 1798 1799 for_start: /* see C99 6.8.5 */ 1800 T_FOR T_LPAREN { 1801 begin_declaration_level(DK_AUTO); 1802 block_level++; 1803 } 1804 ; 1805 1806 for_exprs: /* see C99 6.8.5 */ 1807 for_start 1808 begin_type_declaration_specifiers end_type 1809 notype_init_declarators T_SEMI 1810 expression_opt T_SEMI 1811 expression_opt T_RPAREN { 1812 /* variable declaration in for loop */ 1813 c99ism(325); 1814 for1(NULL, $6, $8); 1815 clear_warning_flags(); 1816 } 1817 | for_start 1818 expression_opt T_SEMI 1819 expression_opt T_SEMI 1820 expression_opt T_RPAREN { 1821 for1($2, $4, $6); 1822 clear_warning_flags(); 1823 } 1824 ; 1825 1826 jump_statement: /* C99 6.8.6 */ 1827 goto identifier T_SEMI { 1828 do_goto(getsym($2)); 1829 } 1830 | goto error T_SEMI { 1831 symtyp = FVFT; 1832 } 1833 | T_CONTINUE T_SEMI { 1834 do_continue(); 1835 } 1836 | T_BREAK T_SEMI { 1837 do_break(); 1838 } 1839 | T_RETURN sys T_SEMI { 1840 do_return($2, NULL); 1841 } 1842 | T_RETURN sys expression T_SEMI { 1843 do_return($2, $3); 1844 } 1845 ; 1846 1847 goto: /* see C99 6.8.6 */ 1848 T_GOTO { 1849 symtyp = FLABEL; 1850 } 1851 ; 1852 1853 asm_statement: /* GCC extension */ 1854 T_ASM T_LPAREN read_until_rparen T_SEMI { 1855 dcs_set_asm(); 1856 } 1857 | T_ASM T_QUAL T_LPAREN read_until_rparen T_SEMI { 1858 dcs_set_asm(); 1859 } 1860 | T_ASM error 1861 ; 1862 1863 read_until_rparen: /* helper for 'asm_statement' */ 1864 /* empty */ { 1865 read_until_rparen(); 1866 } 1867 ; 1868 1869 translation_unit: /* C99 6.9 */ 1870 external_declaration 1871 | translation_unit external_declaration 1872 ; 1873 1874 external_declaration: /* C99 6.9 */ 1875 function_definition { 1876 global_clean_up_decl(false); 1877 clear_warning_flags(); 1878 } 1879 | top_level_declaration { 1880 global_clean_up_decl(false); 1881 clear_warning_flags(); 1882 } 1883 | asm_statement /* GCC extension */ 1884 | T_SEMI { /* GCC extension */ 1885 /* 1886 * TODO: Only allow this in GCC mode, not in plain C99. 1887 * This is one of the top 10 warnings in the NetBSD build. 1888 */ 1889 if (!allow_trad && !allow_c99) { 1890 /* empty declaration */ 1891 error(0); 1892 } else if (allow_c90) { 1893 /* empty declaration */ 1894 warning(0); 1895 } 1896 } 1897 ; 1898 1899 /* 1900 * On the top level, lint allows several forms of declarations that it doesn't 1901 * allow in functions. For example, a single ';' is an empty declaration and 1902 * is supported by some compilers, but in a function it would be an empty 1903 * statement, not a declaration. This makes a difference in C90 mode, where 1904 * a statement must not be followed by a declaration. 1905 * 1906 * See 'declaration' for all other declarations. 1907 */ 1908 top_level_declaration: /* C99 6.9 calls this 'declaration' */ 1909 begin_type end_type notype_init_declarators T_SEMI { 1910 /* TODO: Make this an error in C99 mode as well. */ 1911 if (!allow_trad && !allow_c99) { 1912 /* old-style declaration; add 'int' */ 1913 error(1); 1914 } else if (allow_c90) { 1915 /* old-style declaration; add 'int' */ 1916 warning(1); 1917 } 1918 } 1919 | declaration 1920 | error T_SEMI { 1921 global_clean_up(); 1922 } 1923 | error T_RBRACE { 1924 global_clean_up(); 1925 } 1926 ; 1927 1928 function_definition: /* C99 6.9.1 */ 1929 func_declarator { 1930 if ($1->s_type->t_tspec != FUNC) { 1931 /* syntax error '%s' */ 1932 error(249, yytext); 1933 YYERROR; 1934 } 1935 if ($1->s_type->t_typedef) { 1936 /* ()-less function definition */ 1937 error(64); 1938 YYERROR; 1939 } 1940 funcdef($1); 1941 block_level++; 1942 begin_declaration_level(DK_OLD_STYLE_ARG); 1943 if (lwarn == LWARN_NONE) 1944 $1->s_used = true; 1945 } arg_declaration_list_opt { 1946 end_declaration_level(); 1947 block_level--; 1948 check_func_lint_directives(); 1949 check_func_old_style_arguments(); 1950 begin_control_statement(CS_FUNCTION_BODY); 1951 } compound_statement { 1952 funcend(); 1953 end_control_statement(CS_FUNCTION_BODY); 1954 } 1955 ; 1956 1957 func_declarator: 1958 begin_type end_type notype_declarator { 1959 if (!allow_trad) { 1960 /* old-style declaration; add 'int' */ 1961 error(1); 1962 } 1963 $$ = $3; 1964 } 1965 | begin_type_declmods end_type notype_declarator { 1966 if (!allow_trad) { 1967 /* old-style declaration; add 'int' */ 1968 error(1); 1969 } 1970 $$ = $3; 1971 } 1972 | begin_type_declaration_specifiers end_type type_declarator { 1973 $$ = $3; 1974 } 1975 ; 1976 1977 arg_declaration_list_opt: /* C99 6.9.1p13 example 1 */ 1978 /* empty */ 1979 | arg_declaration_list 1980 ; 1981 1982 arg_declaration_list: /* C99 6.9.1p13 example 1 */ 1983 arg_declaration 1984 | arg_declaration_list arg_declaration 1985 /* XXX or better "arg_declaration error" ? */ 1986 | error 1987 ; 1988 1989 /* 1990 * "arg_declaration" is separated from "declaration" because it 1991 * needs other error handling. 1992 */ 1993 arg_declaration: 1994 begin_type_declmods end_type T_SEMI { 1995 /* empty declaration */ 1996 warning(2); 1997 } 1998 | begin_type_declmods end_type notype_init_declarators T_SEMI 1999 | begin_type_declaration_specifiers end_type T_SEMI { 2000 if (!dcs->d_nonempty_decl) { 2001 /* empty declaration */ 2002 warning(2); 2003 } else { 2004 /* '%s' declared in argument declaration list */ 2005 warning(3, type_name(dcs->d_type)); 2006 } 2007 } 2008 | begin_type_declaration_specifiers end_type 2009 type_init_declarators T_SEMI { 2010 if (dcs->d_nonempty_decl) { 2011 /* '%s' declared in argument declaration list */ 2012 warning(3, type_name(dcs->d_type)); 2013 } 2014 } 2015 | begin_type_declmods error 2016 | begin_type_declaration_specifiers error 2017 ; 2018 2019 /* https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html */ 2020 gcc_attribute_specifier_list_opt: 2021 /* empty */ 2022 | gcc_attribute_specifier_list 2023 ; 2024 2025 gcc_attribute_specifier_list: 2026 gcc_attribute_specifier 2027 | gcc_attribute_specifier_list gcc_attribute_specifier 2028 ; 2029 2030 gcc_attribute_specifier: 2031 T_ATTRIBUTE T_LPAREN T_LPAREN { 2032 in_gcc_attribute = true; 2033 } gcc_attribute_list { 2034 in_gcc_attribute = false; 2035 } T_RPAREN T_RPAREN 2036 ; 2037 2038 gcc_attribute_list: 2039 gcc_attribute 2040 | gcc_attribute_list T_COMMA gcc_attribute 2041 ; 2042 2043 gcc_attribute: 2044 /* empty */ 2045 | T_NAME { 2046 const char *name = $1->sb_name; 2047 if (is_either(name, "packed", "__packed__")) 2048 dcs_add_packed(); 2049 else if (is_either(name, "used", "__used__") || 2050 is_either(name, "unused", "__unused__")) 2051 dcs_set_used(); 2052 else if (is_either(name, "fallthrough", 2053 "__fallthrough__")) 2054 fallthru(1); 2055 } 2056 | T_NAME T_LPAREN T_RPAREN 2057 | T_NAME T_LPAREN gcc_attribute_parameters T_RPAREN 2058 | T_QUAL { 2059 if ($1 != CONST) 2060 yyerror("Bad attribute"); 2061 } 2062 ; 2063 2064 gcc_attribute_parameters: 2065 constant_expr 2066 | gcc_attribute_parameters T_COMMA constant_expr 2067 ; 2068 2069 sys: 2070 /* empty */ { 2071 $$ = in_system_header; 2072 } 2073 ; 2074 2075 %% 2076 2077 /* ARGSUSED */ 2078 int 2079 yyerror(const char *msg) 2080 { 2081 /* syntax error '%s' */ 2082 error(249, yytext); 2083 if (++sytxerr >= 5) 2084 norecover(); 2085 return 0; 2086 } 2087 2088 #if YYDEBUG && (YYBYACC || YYBISON) 2089 static const char * 2090 cgram_to_string(int token, YYSTYPE val) 2091 { 2092 2093 switch (token) { 2094 case T_INCDEC: 2095 case T_MULTIPLICATIVE: 2096 case T_ADDITIVE: 2097 case T_SHIFT: 2098 case T_RELATIONAL: 2099 case T_EQUALITY: 2100 case T_OPASSIGN: 2101 return modtab[val.y_op].m_name; 2102 case T_SCLASS: 2103 return scl_name(val.y_scl); 2104 case T_TYPE: 2105 case T_STRUCT_OR_UNION: 2106 return tspec_name(val.y_tspec); 2107 case T_QUAL: 2108 return tqual_name(val.y_tqual); 2109 case T_NAME: 2110 return val.y_name->sb_name; 2111 default: 2112 return "<none>"; 2113 } 2114 } 2115 #endif 2116 2117 #if YYDEBUG && YYBISON 2118 static inline void 2119 cgram_print(FILE *output, int token, YYSTYPE val) 2120 { 2121 (void)fprintf(output, "%s", cgram_to_string(token, val)); 2122 } 2123 #endif 2124 2125 static void 2126 cgram_declare(sym_t *decl, bool initflg, sbuf_t *renaming) 2127 { 2128 declare(decl, initflg, renaming); 2129 if (renaming != NULL) 2130 freeyyv(&renaming, T_NAME); 2131 } 2132 2133 /* 2134 * Discard all input tokens up to and including the next 2135 * unmatched right paren 2136 */ 2137 static void 2138 read_until_rparen(void) 2139 { 2140 int level; 2141 2142 if (yychar < 0) 2143 yychar = yylex(); 2144 freeyyv(&yylval, yychar); 2145 2146 level = 1; 2147 while (yychar != T_RPAREN || --level > 0) { 2148 if (yychar == T_LPAREN) { 2149 level++; 2150 } else if (yychar <= 0) { 2151 break; 2152 } 2153 freeyyv(&yylval, yychar = yylex()); 2154 } 2155 2156 yyclearin; 2157 } 2158 2159 static sym_t * 2160 symbolrename(sym_t *s, sbuf_t *sb) 2161 { 2162 if (sb != NULL) 2163 s->s_rename = sb->sb_name; 2164 return s; 2165 } 2166