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