1 %{ 2 /* $NetBSD: cgram.y,v 1.517 2025/01/03 03:14:47 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.517 2025/01/03 03:14:47 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 parameters 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 balanced_token_sequence read_balanced_token_sequence(void); 73 static sym_t *symbolrename(sym_t *, sbuf_t *); 74 75 76 /* ARGSUSED */ 77 static void 78 clear_warning_flags_loc(const char *file, size_t line) 79 { 80 debug_step("%s:%zu: clearing flags", file, line); 81 reset_suppressions(); 82 saved_lwarn = LWARN_NOTHING_SAVED; 83 } 84 85 /* ARGSUSED */ 86 static void 87 save_warning_flags_loc(const char *file, size_t line) 88 { 89 debug_step("%s:%zu: saving flags %d", file, line, lwarn); 90 saved_lwarn = lwarn; 91 } 92 93 /* ARGSUSED */ 94 static void 95 restore_warning_flags_loc(const char *file, size_t line) 96 { 97 if (saved_lwarn != LWARN_NOTHING_SAVED) { 98 lwarn = saved_lwarn; 99 debug_step("%s:%zu: restoring flags %d", file, line, lwarn); 100 } else 101 clear_warning_flags_loc(file, line); 102 } 103 104 #define clear_warning_flags() clear_warning_flags_loc(__FILE__, __LINE__) 105 #define save_warning_flags() save_warning_flags_loc(__FILE__, __LINE__) 106 #define restore_warning_flags() restore_warning_flags_loc(__FILE__, __LINE__) 107 108 static bool 109 is_either(const char *s, const char *a, const char *b) 110 { 111 return strcmp(s, a) == 0 || strcmp(s, b) == 0; 112 } 113 114 static void 115 attribute_list_add(attribute_list *list, attribute attr) 116 { 117 if (list->len >= list->cap) { 118 attribute *old_attrs = list->attrs; 119 list->cap = 16 + 2 * list->cap; 120 list->attrs = block_zero_alloc( 121 list->cap * sizeof(*list->attrs), "attribute[]"); 122 if (list->len > 0) 123 memcpy(list->attrs, old_attrs, 124 list->len * sizeof(*list->attrs)); 125 } 126 list->attrs[list->len++] = attr; 127 } 128 129 static void 130 attribute_list_add_all(attribute_list *dst, attribute_list src) 131 { 132 for (size_t i = 0, n = src.len; i < n; i++) 133 attribute_list_add(dst, src.attrs[i]); 134 } 135 136 static attribute 137 new_attribute(const sbuf_t *prefix, const sbuf_t *name, 138 const balanced_token_sequence *arg) 139 { 140 attribute attr = { .name = xstrdup(name->sb_name) }; 141 if (prefix != NULL) 142 attr.prefix = xstrdup(prefix->sb_name); 143 if (arg != NULL) { 144 attr.arg = block_zero_alloc(sizeof(*attr.arg), 145 "balanced_token_sequence"); 146 *attr.arg = *arg; 147 } 148 return attr; 149 } 150 151 #if YYDEBUG && YYBYACC 152 #define YYSTYPE_TOSTRING cgram_to_string 153 #endif 154 155 %} 156 157 %expect 110 158 159 %union { 160 val_t *y_val; 161 sbuf_t *y_name; 162 sym_t *y_sym; 163 bool y_inc; 164 op_t y_op; 165 scl_t y_scl; 166 tspec_t y_tspec; 167 type_qualifiers y_type_qualifiers; 168 type_attributes y_type_attributes; 169 function_specifier y_function_specifier; 170 parameter_list y_parameter_list; 171 function_call *y_arguments; 172 type_t *y_type; 173 tnode_t *y_tnode; 174 range_t y_range; 175 buffer *y_string; 176 qual_ptr *y_qual_ptr; 177 bool y_seen_statement; 178 struct generic_association *y_generic; 179 array_size y_array_size; 180 bool y_in_system_header; 181 designation y_designation; 182 named_constant y_named_constant; 183 attribute y_attribute; 184 attribute_list y_attribute_list; 185 balanced_token_sequence y_tokens; 186 }; 187 188 /* for Bison: 189 %printer { 190 if (is_integer($$->v_tspec)) 191 fprintf(yyo, "%lld", (long long)$$->u.integer); 192 else 193 fprintf(yyo, "%Lg", $$->u.floating); 194 } <y_val> 195 %printer { fprintf(yyo, "'%s'", $$ != NULL ? $$->sb_name : "<null>"); } <y_name> 196 %printer { 197 bool indented = debug_push_indented(true); 198 debug_sym("", $$, ""); 199 debug_pop_indented(indented); 200 } <y_sym> 201 %printer { fprintf(yyo, "%s", $$ ? "++" : "--"); } <y_inc> 202 %printer { fprintf(yyo, "%s", op_name($$)); } <y_op> 203 %printer { fprintf(yyo, "%s", scl_name($$)); } <y_scl> 204 %printer { fprintf(yyo, "%s", tspec_name($$)); } <y_tspec> 205 %printer { fprintf(yyo, "%s", type_qualifiers_string($$)); } <y_type_qualifiers> 206 %printer { fprintf(yyo, "%s", type_attributes_string($$)); } <y_type_attributes> 207 %printer { 208 fprintf(yyo, "%s", function_specifier_name($$)); 209 } <y_function_specifier> 210 %printer { 211 size_t n = 0; 212 for (const sym_t *p = $$.first; p != NULL; p = p->s_next) 213 n++; 214 fprintf(yyo, "%zu parameter%s", n, n != 1 ? "s" : ""); 215 } <y_parameter_list> 216 %printer { fprintf(yyo, "%s", type_name($$)); } <y_type> 217 %printer { 218 if ($$ == NULL) 219 fprintf(yyo, "<null>"); 220 else 221 fprintf(yyo, "%s '%s'", 222 op_name($$->tn_op), type_name($$->tn_type)); 223 } <y_tnode> 224 %printer { fprintf(yyo, "%zu to %zu", $$.lo, $$.hi); } <y_range> 225 %printer { fprintf(yyo, "length %zu", $$->len); } <y_string> 226 %printer { 227 fprintf(yyo, "%s *", type_qualifiers_string($$->qualifiers)); 228 } <y_qual_ptr> 229 %printer { fprintf(yyo, "%s", $$ ? "yes" : "no"); } <y_seen_statement> 230 %printer { fprintf(yyo, "%s", type_name($$->ga_arg)); } <y_generic> 231 %printer { fprintf(yyo, "%d", $$.dim); } <y_array_size> 232 %printer { fprintf(yyo, "%s", $$ ? "yes" : "no"); } <y_in_system_header> 233 %printer { 234 if ($$.dn_len == 0) 235 fprintf(yyo, "(empty)"); 236 for (size_t i = 0; i < $$.dn_len; i++) { 237 const designator *dr = $$.dn_items + i; 238 if (dr->dr_kind == DK_MEMBER) 239 fprintf(yyo, ".%s", dr->dr_member->s_name); 240 else if (dr->dr_kind == DK_SUBSCRIPT) 241 fprintf(yyo, "[%zu]", dr->dr_subscript); 242 else 243 fprintf(yyo, "<scalar>"); 244 } 245 } <y_designation> 246 %printer { fprintf(yyo, "%s", named_constant_name($$)); } <y_named_constant> 247 */ 248 249 %token T_LBRACE T_RBRACE T_LBRACK T_RBRACK T_LPAREN T_RPAREN 250 %token T_POINT T_ARROW 251 %token T_COMPLEMENT T_LOGNOT 252 %token <y_inc> T_INCDEC 253 %token T_SIZEOF 254 %token T_BUILTIN_OFFSETOF 255 %token T_TYPEOF 256 %token T_EXTENSION 257 %token T_ALIGNAS 258 %token T_ALIGNOF 259 %token T_ASTERISK 260 %token <y_op> T_MULTIPLICATIVE 261 %token <y_op> T_ADDITIVE 262 %token <y_op> T_SHIFT 263 %token <y_op> T_RELATIONAL 264 %token <y_op> T_EQUALITY 265 %token T_AMPER 266 %token T_BITXOR 267 %token T_BITOR 268 %token T_LOGAND 269 %token T_LOGOR 270 %token T_QUEST 271 %token T_COLON 272 %token T_ASSIGN 273 %token <y_op> T_OPASSIGN 274 %token T_COMMA 275 %token T_SEMI 276 %token T_ELLIPSIS 277 %token T_DCOLON 278 %token T_REAL 279 %token T_IMAG 280 %token T_GENERIC 281 282 /* storage classes (extern, static, auto, register and typedef) */ 283 %token <y_scl> T_SCLASS 284 %token <y_function_specifier> T_FUNCTION_SPECIFIER 285 286 /* 287 * predefined type keywords (char, int, short, long, unsigned, signed, 288 * float, double, void); see T_TYPENAME for types from typedef 289 */ 290 %token <y_tspec> T_TYPE 291 292 %token <y_type_qualifiers> T_QUAL 293 %token <y_type_qualifiers> T_ATOMIC 294 295 /* struct or union */ 296 %token <y_tspec> T_STRUCT_OR_UNION 297 298 /* remaining keywords */ 299 %token T_ASM 300 %token T_BREAK 301 %token T_CASE 302 %token T_CONTINUE 303 %token T_DEFAULT 304 %token T_DO 305 %token T_ELSE 306 %token T_ENUM 307 %token T_FOR 308 %token T_GOTO 309 %token T_IF 310 %token T_PACKED 311 %token T_RETURN 312 %token T_SWITCH 313 %token T_SYMBOLRENAME 314 %token T_WHILE 315 %token T_STATIC_ASSERT 316 317 %token T_ATTRIBUTE 318 319 %left T_THEN 320 %left T_ELSE 321 %right T_QUEST T_COLON 322 %left T_LOGOR 323 %left T_LOGAND 324 %left T_BITOR 325 %left T_BITXOR 326 %left T_AMPER 327 %left T_EQUALITY 328 %left T_RELATIONAL 329 %left T_SHIFT 330 %left T_ADDITIVE 331 %left T_ASTERISK T_MULTIPLICATIVE 332 333 %token <y_name> T_NAME 334 %token <y_name> T_TYPENAME 335 %token <y_val> T_CON 336 %token <y_named_constant> T_NAMED_CONSTANT 337 %token <y_string> T_STRING 338 339 /* No type for program. */ 340 %type <y_sym> identifier_sym 341 %type <y_name> identifier 342 %type <y_string> string 343 %type <y_tnode> primary_expression 344 %type <y_designation> member_designator 345 %type <y_tnode> generic_selection 346 %type <y_generic> generic_assoc_list 347 %type <y_generic> generic_association 348 %type <y_tnode> postfix_expression 349 %type <y_tnode> gcc_statement_expr_list 350 %type <y_tnode> gcc_statement_expr_item 351 %type <y_op> point_or_arrow 352 %type <y_arguments> argument_expression_list 353 %type <y_scl> storage_class_specifiers 354 %type <y_tnode> unary_expression 355 %type <y_tnode> cast_expression 356 %type <y_tnode> expression_opt 357 %type <y_tnode> conditional_expression 358 %type <y_tnode> assignment_expression 359 %type <y_tnode> expression 360 %type <y_tnode> constant_expression 361 /* No type for declaration_or_error. */ 362 /* No type for declaration. */ 363 /* No type for begin_type_declaration_specifiers. */ 364 /* No type for begin_type_declmods. */ 365 /* No type for begin_type_specifier_qualifier_list. */ 366 /* No type for begin_type_specifier_qualifier_list_postfix. */ 367 %type <y_type> begin_type_typespec 368 /* No type for begin_type_qualifier_list. */ 369 /* No type for declmod. */ 370 %type <y_type_attributes> type_attribute_list_opt 371 %type <y_type_attributes> type_attribute_list 372 %type <y_type_attributes> type_attribute_opt 373 %type <y_type_attributes> type_attribute 374 /* No type for begin_type. */ 375 /* No type for end_type. */ 376 /* No type for notype_init_declarator_list. */ 377 /* No type for type_init_declarator_list. */ 378 /* No type for notype_init_declarator. */ 379 /* No type for type_init_declarator. */ 380 %type <y_scl> storage_class_specifier 381 %type <y_type> type_type_specifier 382 %type <y_type> notype_type_specifier 383 %type <y_type> struct_or_union_specifier 384 %type <y_tspec> struct_or_union 385 %type <y_sym> braced_member_declaration_list 386 %type <y_sym> member_declaration_list_with_rbrace 387 %type <y_sym> member_declaration_list 388 %type <y_sym> member_declaration 389 %type <y_sym> notype_member_declarator_list 390 %type <y_sym> type_member_declarator_list 391 %type <y_sym> notype_member_declarator 392 %type <y_sym> type_member_declarator 393 %type <y_type> enum_specifier 394 /* No type for enum. */ 395 %type <y_sym> enum_declaration 396 %type <y_sym> enums_with_opt_comma 397 %type <y_sym> enumerator_list 398 %type <y_sym> enumerator 399 %type <y_type> atomic_type_specifier 400 /* No type for atomic. */ 401 %type <y_type_qualifiers> type_qualifier 402 %type <y_sym> notype_declarator 403 %type <y_sym> type_declarator 404 %type <y_sym> notype_direct_declarator 405 %type <y_sym> type_direct_declarator 406 %type <y_qual_ptr> pointer 407 %type <y_type_qualifiers> type_qualifier_list_opt 408 %type <y_type_qualifiers> type_qualifier_list 409 %type <y_sym> parameter_declaration 410 %type <y_sym> type_param_declarator 411 %type <y_sym> notype_param_declarator 412 %type <y_sym> direct_param_declarator 413 %type <y_sym> direct_notype_param_declarator 414 %type <y_parameter_list> param_list 415 %type <y_array_size> array_size_opt 416 %type <y_sym> identifier_list 417 %type <y_type> type_name 418 %type <y_sym> abstract_declaration 419 %type <y_parameter_list> abstract_decl_param_list 420 /* No type for abstract_decl_lparen. */ 421 %type <y_parameter_list> vararg_parameter_type_list 422 %type <y_parameter_list> parameter_type_list 423 %type <y_sym> abstract_declarator 424 %type <y_sym> direct_abstract_declarator 425 /* No type for braced_initializer. */ 426 /* No type for initializer. */ 427 /* No type for initializer_list. */ 428 /* No type for designation. */ 429 /* No type for designator_list. */ 430 /* No type for designator. */ 431 /* No type for static_assert_declaration. */ 432 %type <y_range> range 433 /* No type for init_lbrace. */ 434 /* No type for init_rbrace. */ 435 %type <y_attribute_list> attribute_specifier_sequence 436 %type <y_attribute_list> attribute_specifier 437 %type <y_attribute_list> attribute_list 438 %type <y_attribute> attribute 439 %type <y_tokens> attribute_argument_clause 440 %type <y_name> asm_or_symbolrename_opt 441 /* No type for statement. */ 442 /* No type for no_attr_statement. */ 443 /* No type for non_expr_statement. */ 444 /* No type for no_attr_non_expr_statement. */ 445 /* No type for label. */ 446 /* No type for labeled_statement. */ 447 /* No type for compound_statement. */ 448 /* No type for compound_statement_lbrace. */ 449 /* No type for compound_statement_rbrace. */ 450 %type <y_seen_statement> block_item_list 451 %type <y_seen_statement> block_item 452 /* No type for expression_statement. */ 453 /* No type for selection_statement. */ 454 /* No type for if_without_else. */ 455 /* No type for if_expr. */ 456 /* No type for switch_expr. */ 457 /* No type for iteration_statement. */ 458 /* No type for while_expr. */ 459 /* No type for do_statement. */ 460 /* No type for do. */ 461 /* No type for for_start. */ 462 /* No type for for_exprs. */ 463 /* No type for jump_statement. */ 464 /* No type for goto. */ 465 /* No type for asm_statement. */ 466 /* No type for read_until_rparen. */ 467 /* No type for translation_unit. */ 468 /* No type for external_declaration. */ 469 /* No type for top_level_declaration. */ 470 /* No type for function_definition. */ 471 %type <y_sym> func_declarator 472 /* No type for arg_declaration_list_opt. */ 473 /* No type for arg_declaration_list. */ 474 /* No type for arg_declaration. */ 475 %type <y_type_attributes> gcc_attribute_specifier_list_opt 476 %type <y_type_attributes> gcc_attribute_specifier_list 477 %type <y_type_attributes> gcc_attribute_specifier 478 %type <y_type_attributes> gcc_attribute_list 479 %type <y_type_attributes> gcc_attribute 480 %type <y_in_system_header> sys 481 482 %% 483 484 program: 485 /* empty */ { 486 /* TODO: Make this an error in C99 mode as well. */ 487 if (!allow_trad && !allow_c99) 488 /* empty translation unit */ 489 error(272); 490 else if (allow_c90) 491 /* empty translation unit */ 492 warning(272); 493 } 494 | translation_unit 495 ; 496 497 identifier_sym: /* helper for struct/union/enum */ 498 identifier { 499 $$ = getsym($1); 500 } 501 ; 502 503 /* K&R ???, C90 ???, C99 6.4.2.1, C11 ??? */ 504 identifier: 505 T_NAME { 506 debug_step("cgram: name '%s'", $1->sb_name); 507 $$ = $1; 508 } 509 | T_TYPENAME { 510 debug_step("cgram: typename '%s'", $1->sb_name); 511 $$ = $1; 512 } 513 ; 514 515 /* see C99 6.4.5, string literals are joined by 5.1.1.2 */ 516 string: 517 T_STRING 518 | string T_STRING { 519 if (!allow_c90) 520 /* concatenated strings are illegal in traditional C */ 521 warning(219); 522 $$ = cat_strings($1, $2); 523 } 524 ; 525 526 /* K&R 7.1, C90 ???, C99 6.5.1, C11 6.5.1, C23 6.5.2 */ 527 primary_expression: 528 T_NAME { 529 bool sys_name, sys_next; 530 sys_name = in_system_header; 531 if (yychar < 0) 532 yychar = yylex(); 533 sys_next = in_system_header; 534 in_system_header = sys_name; 535 $$ = build_name(getsym($1), yychar == T_LPAREN); 536 in_system_header = sys_next; 537 } 538 | T_CON { 539 $$ = build_constant(gettyp($1->v_tspec), $1); 540 } 541 | T_NAMED_CONSTANT { 542 if ($1 == NC_NULLPTR) { 543 tnode_t *zero = expr_alloc_tnode(); 544 zero->tn_op = CON; 545 zero->tn_type = gettyp(INT); 546 zero->u.value.v_tspec = INT; 547 548 type_t *void_ptr = block_derive_type(gettyp(VOID), PTR); 549 $$ = convert(CVT, 0, void_ptr, zero); 550 $$->tn_sys = zero->tn_sys; 551 } else { 552 tnode_t *nc = expr_alloc_tnode(); 553 nc->tn_op = CON; 554 nc->tn_type = gettyp(BOOL); 555 nc->u.value.v_tspec = BOOL; 556 nc->u.value.u.integer = $1 == NC_TRUE ? 1 : 0; 557 $$ = nc; 558 } 559 } 560 | string { 561 $$ = build_string($1); 562 } 563 | T_LPAREN expression T_RPAREN { 564 if ($2 != NULL) 565 $2->tn_parenthesized = true; 566 $$ = $2; 567 } 568 | generic_selection 569 /* GCC primary-expression, see c_parser_postfix_expression */ 570 | T_BUILTIN_OFFSETOF T_LPAREN type_name T_COMMA { 571 set_sym_kind(SK_MEMBER); 572 } member_designator T_RPAREN { 573 $$ = build_offsetof($3, $6); 574 } 575 ; 576 577 /* K&R ---, C90 ---, C99 7.17p3, C11 7.19p3, C23 7.21p4 */ 578 member_designator: 579 identifier { 580 $$ = (designation) { .dn_len = 0 }; 581 designation_push(&$$, DK_MEMBER, getsym($1), 0); 582 } 583 | member_designator T_LBRACK range T_RBRACK { 584 $$ = $1; 585 designation_push(&$$, DK_SUBSCRIPT, NULL, $3.lo); 586 } 587 | member_designator T_POINT { 588 set_sym_kind(SK_MEMBER); 589 } identifier { 590 $$ = $1; 591 designation_push(&$$, DK_MEMBER, getsym($4), 0); 592 } 593 ; 594 595 /* K&R ---, C90 ---, C99 ---, C11 6.5.1.1, C23 6.5.2.1 */ 596 generic_selection: 597 T_GENERIC T_LPAREN assignment_expression T_COMMA 598 generic_assoc_list T_RPAREN { 599 /* generic selection requires C11 or later */ 600 c11ism(345); 601 $$ = build_generic_selection($3, $5); 602 } 603 ; 604 605 /* K&R ---, C90 ---, C99 ---, C11 6.5.1.1, C23 6.5.2.1 */ 606 generic_assoc_list: 607 generic_association 608 | generic_assoc_list T_COMMA generic_association { 609 $3->ga_prev = $1; 610 $$ = $3; 611 } 612 ; 613 614 /* K&R ---, C90 ---, C99 ---, C11 6.5.1.1, C23 6.5.2.1 */ 615 generic_association: 616 type_name T_COLON assignment_expression { 617 $$ = block_zero_alloc(sizeof(*$$), "generic"); 618 $$->ga_arg = $1; 619 $$->ga_result = $3; 620 } 621 | T_DEFAULT T_COLON assignment_expression { 622 $$ = block_zero_alloc(sizeof(*$$), "generic"); 623 $$->ga_arg = NULL; 624 $$->ga_result = $3; 625 } 626 ; 627 628 /* K&R 7.1, C90 ???, C99 6.5.2, C11 6.5.2, C23 6.5.3.1 */ 629 postfix_expression: 630 primary_expression 631 | postfix_expression T_LBRACK sys expression T_RBRACK { 632 $$ = build_unary(INDIR, $3, build_binary($1, PLUS, $3, $4)); 633 } 634 | postfix_expression T_LPAREN sys T_RPAREN { 635 function_call *call = 636 expr_zero_alloc(sizeof(*call), "function_call"); 637 $$ = build_function_call($1, $3, call); 638 } 639 | postfix_expression T_LPAREN sys argument_expression_list T_RPAREN { 640 $$ = build_function_call($1, $3, $4); 641 } 642 | postfix_expression point_or_arrow sys T_NAME { 643 $$ = build_member_access($1, $2, $3, $4); 644 } 645 | postfix_expression T_INCDEC sys { 646 $$ = build_unary($2 ? INCAFT : DECAFT, $3, $1); 647 } 648 /* Rule 'compound_literal' from C99 6.5.2.5. */ 649 | T_LPAREN type_name T_RPAREN { 650 sym_t *tmp = mktempsym($2); 651 begin_initialization(tmp); 652 cgram_declare(tmp, true, NULL); 653 } braced_initializer { 654 if (!allow_c99) 655 /* compound literals are a C99/GCC extension */ 656 gnuism(319); 657 $$ = build_name(current_initsym(), false); 658 end_initialization(); 659 } 660 /* Rule 'compound_literal' with storage classes from C23 6.5.3.6. */ 661 | T_LPAREN storage_class_specifiers type_name T_RPAREN { 662 sym_t *tmp = mktempsym($3); 663 tmp->s_scl = $2; 664 begin_initialization(tmp); 665 cgram_declare(tmp, true, NULL); 666 } braced_initializer { 667 if (!allow_c99) 668 /* compound literals are a C99/GCC extension */ 669 gnuism(319); 670 $$ = build_name(current_initsym(), false); 671 end_initialization(); 672 } 673 | T_LPAREN compound_statement_lbrace { 674 begin_statement_expr(); 675 } gcc_statement_expr_list { 676 do_statement_expr($4); 677 } compound_statement_rbrace T_RPAREN { 678 $$ = end_statement_expr(); 679 } 680 ; 681 682 /* 683 * The inner part of a GCC statement-expression of the form ({ ... }). 684 * 685 * https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html 686 */ 687 gcc_statement_expr_list: 688 gcc_statement_expr_item 689 | gcc_statement_expr_list gcc_statement_expr_item { 690 $$ = $2; 691 } 692 ; 693 694 gcc_statement_expr_item: 695 declaration_or_error { 696 clear_warning_flags(); 697 $$ = NULL; 698 } 699 | non_expr_statement { 700 $$ = expr_alloc_tnode(); 701 $$->tn_type = gettyp(VOID); 702 } 703 | T_SEMI { 704 $$ = expr_alloc_tnode(); 705 $$->tn_type = gettyp(VOID); 706 } 707 | expression T_SEMI { 708 if ($1 == NULL) { /* in case of syntax errors */ 709 $$ = expr_alloc_tnode(); 710 $$->tn_type = gettyp(VOID); 711 } else { 712 /* XXX: do that only on the last name */ 713 if ($1->tn_op == NAME) 714 $1->u.sym->s_used = true; 715 expr($1, true, false, false, false, 716 "statement expression"); 717 suppress_fallthrough = false; 718 $$ = $1; 719 } 720 } 721 ; 722 723 point_or_arrow: /* helper for 'postfix_expression' */ 724 T_POINT { 725 set_sym_kind(SK_MEMBER); 726 $$ = POINT; 727 } 728 | T_ARROW { 729 set_sym_kind(SK_MEMBER); 730 $$ = ARROW; 731 } 732 ; 733 734 /* K&R 7.1, C90 ???, C99 6.5.2, C11 6.5.2, C23 6.5.3.1 */ 735 argument_expression_list: 736 assignment_expression { 737 $$ = expr_zero_alloc(sizeof(*$$), "function_call"); 738 add_function_argument($$, $1); 739 } 740 | argument_expression_list T_COMMA assignment_expression { 741 $$ = $1; 742 add_function_argument($1, $3); 743 } 744 ; 745 746 747 /* C23 6.5.3.6 */ 748 /* The rule 'compound_literal' is inlined into 'postfix_expression'. */ 749 750 /* C23 6.5.3.6 */ 751 storage_class_specifiers: 752 storage_class_specifier 753 | storage_class_specifiers storage_class_specifier { 754 // TODO C23: maybe merge multiple storage class specifiers 755 $$ = $1; 756 } 757 ; 758 759 /* K&R 7.2, C90 ???, C99 6.5.3, C11 6.5.3, C23 6.5.4 */ 760 unary_expression: 761 postfix_expression 762 | T_INCDEC sys unary_expression { 763 $$ = build_unary($1 ? INCBEF : DECBEF, $2, $3); 764 } 765 | T_AMPER sys cast_expression { 766 $$ = build_unary(ADDR, $2, $3); 767 } 768 | T_ASTERISK sys cast_expression { 769 $$ = build_unary(INDIR, $2, $3); 770 } 771 | T_ADDITIVE sys cast_expression { 772 if (!allow_c90 && $1 == PLUS) 773 /* unary '+' is illegal in traditional C */ 774 warning(100); 775 $$ = build_unary($1 == PLUS ? UPLUS : UMINUS, $2, $3); 776 } 777 | T_COMPLEMENT sys cast_expression { 778 $$ = build_unary(COMPL, $2, $3); 779 } 780 | T_LOGNOT sys cast_expression { 781 $$ = build_unary(NOT, $2, $3); 782 } 783 | T_REAL sys cast_expression { /* GCC c_parser_unary_expression */ 784 $$ = build_unary(REAL, $2, $3); 785 } 786 | T_IMAG sys cast_expression { /* GCC c_parser_unary_expression */ 787 $$ = build_unary(IMAG, $2, $3); 788 } 789 | T_EXTENSION cast_expression { /* GCC c_parser_unary_expression */ 790 $$ = $2; 791 } 792 | T_SIZEOF unary_expression { 793 $$ = $2 == NULL ? NULL : build_sizeof($2->tn_type); 794 if ($$ != NULL) 795 check_expr_misc($2, 796 false, false, false, false, false, true); 797 } 798 | T_SIZEOF T_LPAREN type_name T_RPAREN { 799 $$ = build_sizeof($3); 800 } 801 | T_ALIGNOF unary_expression { 802 /* non type argument to alignof is a GCC extension */ 803 gnuism(349); 804 lint_assert($2 != NULL); 805 $$ = build_alignof($2->tn_type); 806 } 807 /* K&R ---, C90 ---, C99 ---, C11 6.5.3, C23 6.5.4.4 */ 808 | T_ALIGNOF T_LPAREN type_name T_RPAREN { 809 /* TODO: c11ism */ 810 $$ = build_alignof($3); 811 } 812 ; 813 814 /* C23 6.5.4 */ 815 /* The rule 'unary_operator' is inlined into unary_expression. */ 816 817 /* K&R 7.2, C90 ???, C99 6.5.4, C11 6.5.4, C23 6.5.5 */ 818 cast_expression: 819 unary_expression 820 | T_LPAREN type_name T_RPAREN sys cast_expression { 821 $$ = cast($5, $4, $2); 822 } 823 ; 824 825 expression_opt: 826 /* empty */ { 827 $$ = NULL; 828 } 829 | expression 830 ; 831 832 /* 'conditional_expression' also implements 'multiplicative_expression'. */ 833 /* 'conditional_expression' also implements 'additive_expression'. */ 834 /* 'conditional_expression' also implements 'shift_expression'. */ 835 /* 'conditional_expression' also implements 'relational_expression'. */ 836 /* 'conditional_expression' also implements 'equality_expression'. */ 837 /* 'conditional_expression' also implements 'AND_expression'. */ 838 /* 'conditional_expression' also implements 'exclusive_OR_expression'. */ 839 /* 'conditional_expression' also implements 'inclusive_OR_expression'. */ 840 /* 'conditional_expression' also implements 'logical_AND_expression'. */ 841 /* 'conditional_expression' also implements 'logical_OR_expression'. */ 842 /* K&R ???, C90 ???, C99 6.5.5 to 6.5.15, C11 6.5.5 to 6.5.15, C23 6.5.6 to 6.5.16 */ 843 conditional_expression: 844 cast_expression 845 | conditional_expression T_ASTERISK sys conditional_expression { 846 $$ = build_binary($1, MULT, $3, $4); 847 } 848 | conditional_expression T_MULTIPLICATIVE sys conditional_expression { 849 $$ = build_binary($1, $2, $3, $4); 850 } 851 | conditional_expression T_ADDITIVE sys conditional_expression { 852 $$ = build_binary($1, $2, $3, $4); 853 } 854 | conditional_expression T_SHIFT sys conditional_expression { 855 $$ = build_binary($1, $2, $3, $4); 856 } 857 | conditional_expression T_RELATIONAL sys conditional_expression { 858 $$ = build_binary($1, $2, $3, $4); 859 } 860 | conditional_expression T_EQUALITY sys conditional_expression { 861 $$ = build_binary($1, $2, $3, $4); 862 } 863 | conditional_expression T_AMPER sys conditional_expression { 864 $$ = build_binary($1, BITAND, $3, $4); 865 } 866 | conditional_expression T_BITXOR sys conditional_expression { 867 $$ = build_binary($1, BITXOR, $3, $4); 868 } 869 | conditional_expression T_BITOR sys conditional_expression { 870 $$ = build_binary($1, BITOR, $3, $4); 871 } 872 | conditional_expression T_LOGAND sys conditional_expression { 873 $$ = build_binary($1, LOGAND, $3, $4); 874 } 875 | conditional_expression T_LOGOR sys conditional_expression { 876 $$ = build_binary($1, LOGOR, $3, $4); 877 } 878 | conditional_expression T_QUEST sys 879 expression T_COLON sys conditional_expression { 880 $$ = build_binary($1, QUEST, $3, 881 build_binary($4, COLON, $6, $7)); 882 } 883 ; 884 885 /* K&R ???, C90 ???, C99 6.5.16, C11 6.5.16, C23 6.5.17.1 */ 886 assignment_expression: 887 conditional_expression 888 | unary_expression T_ASSIGN sys assignment_expression { 889 $$ = build_binary($1, ASSIGN, $3, $4); 890 } 891 | unary_expression T_OPASSIGN sys assignment_expression { 892 $$ = build_binary($1, $2, $3, $4); 893 } 894 ; 895 896 /* C23 6.5.17.1 */ 897 /* The rule 'assignment_operator' is inlined into 'assignment_expression'. */ 898 899 /* K&R ???, C90 ???, C99 6.5.17, C11 6.5.17, C23 6.5.18 */ 900 expression: 901 assignment_expression 902 | expression T_COMMA sys assignment_expression { 903 $$ = build_binary($1, COMMA, $3, $4); 904 } 905 ; 906 907 /* K&R ???, C90 ???, C99 6.6, C11 ???, C23 6.6 */ 908 constant_expression: 909 conditional_expression 910 ; 911 912 declaration_or_error: 913 declaration 914 | error T_SEMI 915 ; 916 917 /* K&R ???, C90 ???, C99 6.7, C11 ???, C23 6.7.1 */ 918 declaration: 919 begin_type_declmods end_type T_SEMI { 920 if (dcs->d_scl == TYPEDEF) 921 /* typedef declares no type name */ 922 warning(72); 923 else 924 /* empty declaration */ 925 warning(2); 926 } 927 | begin_type_declmods end_type notype_init_declarator_list T_SEMI { 928 if (dcs->d_scl == TYPEDEF) 929 /* syntax error '%s' */ 930 error(249, "missing base type for typedef"); 931 else 932 /* old-style declaration; add 'int' */ 933 error(1); 934 } 935 | begin_type_declaration_specifiers end_type T_SEMI { 936 if (dcs->d_scl == TYPEDEF) 937 /* typedef declares no type name */ 938 warning(72); 939 else if (!dcs->d_nonempty_decl) 940 /* empty declaration */ 941 warning(2); 942 } 943 | begin_type_declaration_specifiers end_type 944 type_init_declarator_list T_SEMI 945 | static_assert_declaration 946 ; 947 948 /* TODO: Implement 'declaration_specifiers' from C23 6.7.1. */ 949 950 begin_type_declaration_specifiers: /* see C99 6.7, C23 6.7.1 */ 951 begin_type_typespec { 952 dcs_add_type($1); 953 } 954 | begin_type_declmods type_type_specifier { 955 dcs_add_type($2); 956 } 957 | type_attribute begin_type_declaration_specifiers { 958 if ($1.used) 959 dcs_set_used(); 960 if ($1.noreturn) 961 dcs->d_noreturn = true; 962 } 963 | begin_type_declaration_specifiers declmod 964 | begin_type_declaration_specifiers notype_type_specifier { 965 dcs_add_type($2); 966 } 967 ; 968 969 begin_type_declmods: /* see C99 6.7 */ 970 begin_type type_qualifier { 971 dcs_add_qualifiers($2); 972 } 973 | begin_type T_SCLASS { 974 dcs_add_storage_class($2); 975 } 976 | begin_type T_FUNCTION_SPECIFIER { 977 dcs_add_function_specifier($2); 978 } 979 | begin_type_declmods declmod 980 ; 981 982 begin_type_specifier_qualifier_list: /* see C11 6.7.2.1 */ 983 begin_type_specifier_qualifier_list_postfix 984 | type_attribute_list begin_type_specifier_qualifier_list_postfix 985 ; 986 987 begin_type_specifier_qualifier_list_postfix: 988 begin_type_typespec { 989 dcs_add_type($1); 990 } 991 | begin_type_qualifier_list type_type_specifier { 992 dcs_add_type($2); 993 } 994 | begin_type_specifier_qualifier_list_postfix type_qualifier { 995 dcs_add_qualifiers($2); 996 } 997 | begin_type_specifier_qualifier_list_postfix notype_type_specifier { 998 dcs_add_type($2); 999 } 1000 | begin_type_specifier_qualifier_list_postfix type_attribute 1001 ; 1002 1003 begin_type_typespec: 1004 begin_type notype_type_specifier { 1005 $$ = $2; 1006 } 1007 | begin_type T_TYPENAME { 1008 $$ = getsym($2)->s_type; 1009 } 1010 ; 1011 1012 begin_type_qualifier_list: 1013 begin_type type_qualifier { 1014 dcs_add_qualifiers($2); 1015 } 1016 | begin_type_qualifier_list type_qualifier { 1017 dcs_add_qualifiers($2); 1018 } 1019 ; 1020 1021 declmod: 1022 type_qualifier { 1023 dcs_add_qualifiers($1); 1024 } 1025 | T_SCLASS { 1026 dcs_add_storage_class($1); 1027 } 1028 | T_FUNCTION_SPECIFIER { 1029 dcs_add_function_specifier($1); 1030 } 1031 | type_attribute_list { 1032 if ($1.used) 1033 dcs_set_used(); 1034 if ($1.noreturn) 1035 dcs->d_noreturn = true; 1036 } 1037 ; 1038 1039 type_attribute_list_opt: 1040 /* empty */ { 1041 $$ = (type_attributes){ .used = false }; 1042 } 1043 | type_attribute_list 1044 ; 1045 1046 type_attribute_list: 1047 type_attribute 1048 | type_attribute_list type_attribute { 1049 $$ = (type_attributes){ .used = $1.used || $2.used }; 1050 } 1051 ; 1052 1053 type_attribute_opt: 1054 /* empty */ { 1055 $$ = (type_attributes){ .used = false }; 1056 } 1057 | type_attribute 1058 ; 1059 1060 type_attribute: /* See C11 6.7 declaration-specifiers */ 1061 gcc_attribute_specifier 1062 | T_ALIGNAS T_LPAREN type_type_specifier T_RPAREN { /* C11 6.7.5 */ 1063 dcs_add_alignas(build_sizeof($3)); 1064 $$ = (type_attributes){ .used = false }; 1065 } 1066 | T_ALIGNAS T_LPAREN constant_expression T_RPAREN { /* C11 6.7.5 */ 1067 dcs_add_alignas($3); 1068 $$ = (type_attributes){ .used = false }; 1069 } 1070 | T_PACKED { 1071 dcs_add_packed(); 1072 $$ = (type_attributes){ .used = false }; 1073 } 1074 ; 1075 1076 begin_type: 1077 /* empty */ { 1078 dcs_begin_type(); 1079 } 1080 | attribute_specifier_sequence { 1081 dcs_begin_type(); 1082 dcs->d_used = attributes_contain(&$1, "maybe_unused"); 1083 dcs->d_noreturn = attributes_contain(&$1, "noreturn"); 1084 } 1085 ; 1086 1087 end_type: 1088 /* empty */ { 1089 dcs_end_type(); 1090 } 1091 ; 1092 1093 /* TODO: Implement 'declaration_specifier' from C23 6.7.1. */ 1094 1095 /* 1096 * For an explanation of 'type' and 'notype' prefixes in the following rules, 1097 * see https://www.gnu.org/software/bison/manual/bison.html#Semantic-Tokens. 1098 */ 1099 1100 /* C23 6.7.1 */ 1101 /* The rule 'init_declarator_list' is split into the 'notype' and 'type' variants. */ 1102 1103 notype_init_declarator_list: 1104 notype_init_declarator 1105 | notype_init_declarator_list T_COMMA type_init_declarator 1106 ; 1107 1108 type_init_declarator_list: 1109 type_init_declarator 1110 | type_init_declarator_list T_COMMA type_init_declarator 1111 ; 1112 1113 /* C23 6.7.1 */ 1114 /* The rule 'init_declarator' is split into the 'notype' and 'type' variants. */ 1115 1116 notype_init_declarator: 1117 notype_declarator asm_or_symbolrename_opt { 1118 cgram_declare($1, false, $2); 1119 check_size($1); 1120 } 1121 | notype_declarator asm_or_symbolrename_opt { 1122 begin_initialization($1); 1123 cgram_declare($1, true, $2); 1124 } T_ASSIGN initializer { 1125 check_size($1); 1126 end_initialization(); 1127 } 1128 ; 1129 1130 type_init_declarator: 1131 type_declarator asm_or_symbolrename_opt { 1132 cgram_declare($1, false, $2); 1133 check_size($1); 1134 } 1135 | type_declarator asm_or_symbolrename_opt { 1136 begin_initialization($1); 1137 cgram_declare($1, true, $2); 1138 } T_ASSIGN initializer { 1139 if ($1->s_type->t_tspec != AUTO_TYPE) 1140 check_size($1); 1141 end_initialization(); 1142 } 1143 ; 1144 1145 1146 /* TODO: Implement 'attribute_declaration' from C23 6.7.1. */ 1147 1148 /* K&R ???, C90 ???, C99 ???, C11 ???, C23 6.7.2 */ 1149 storage_class_specifier: 1150 T_SCLASS 1151 ; 1152 1153 /* C99 6.7.2, C23 6.7.3.1 */ 1154 /* The rule 'type_specifier' is split into the 'notype' and 'type' variants. */ 1155 1156 type_type_specifier: 1157 notype_type_specifier 1158 | T_TYPENAME { 1159 $$ = getsym($1)->s_type; 1160 } 1161 ; 1162 1163 notype_type_specifier: /* see C99 6.7.2 */ 1164 T_TYPE { 1165 $$ = gettyp($1); 1166 } 1167 | T_TYPEOF T_LPAREN expression T_RPAREN { /* GCC extension */ 1168 $$ = $3 != NULL ? block_dup_type($3->tn_type) : gettyp(INT); 1169 $$->t_typeof = true; 1170 } 1171 | atomic_type_specifier 1172 | struct_or_union_specifier { 1173 end_declaration_level(); 1174 $$ = $1; 1175 } 1176 | enum_specifier { 1177 end_declaration_level(); 1178 $$ = $1; 1179 } 1180 ; 1181 1182 /* K&R ---, C90 ---, C99 6.7.2.1, C11 ???, C23 6.7.3.2 */ 1183 struct_or_union_specifier: 1184 struct_or_union identifier_sym { 1185 /* 1186 * STDC requires that "struct a;" always introduces 1187 * a new tag if "a" is not declared at current level 1188 * 1189 * yychar is valid because otherwise the parser would not 1190 * have been able to decide if it must shift or reduce 1191 */ 1192 $$ = make_tag_type($2, $1, false, yychar == T_SEMI); 1193 } 1194 | struct_or_union identifier_sym { 1195 dcs->d_tag_type = make_tag_type($2, $1, true, false); 1196 } braced_member_declaration_list { 1197 $$ = complete_struct_or_union($4); 1198 } 1199 | struct_or_union { 1200 dcs->d_tag_type = make_tag_type(NULL, $1, true, false); 1201 } braced_member_declaration_list { 1202 $$ = complete_struct_or_union($3); 1203 } 1204 | struct_or_union error { 1205 set_sym_kind(SK_VCFT); 1206 $$ = gettyp(INT); 1207 } 1208 ; 1209 1210 /* K&R ---, C90 ---, C99 6.7.2.1, C11 ???, C23 6.7.3.2 */ 1211 struct_or_union: 1212 T_STRUCT_OR_UNION { 1213 set_sym_kind(SK_TAG); 1214 begin_declaration_level($1 == STRUCT ? DLK_STRUCT : DLK_UNION); 1215 dcs->d_sou_size_in_bits = 0; 1216 dcs->d_sou_align = 1; 1217 $$ = $1; 1218 } 1219 | struct_or_union type_attribute 1220 ; 1221 1222 braced_member_declaration_list: /* see C99 6.7.2.1 */ 1223 T_LBRACE { 1224 set_sym_kind(SK_VCFT); 1225 } member_declaration_list_with_rbrace { 1226 $$ = $3; 1227 } 1228 ; 1229 1230 member_declaration_list_with_rbrace: /* see C99 6.7.2.1 */ 1231 member_declaration_list T_RBRACE 1232 | T_RBRACE { 1233 /* XXX: Allowed since C23. */ 1234 $$ = NULL; 1235 } 1236 ; 1237 1238 /* K&R ???, C90 ???, C99 6.7.2.1, C11 6.7.2.1, C23 6.7.3.2 */ 1239 /* Was named struct_declaration_list until C11. */ 1240 member_declaration_list: 1241 member_declaration 1242 | member_declaration_list member_declaration { 1243 $$ = concat_symbols($1, $2); 1244 } 1245 ; 1246 1247 /* K&R ???, C90 ???, C99 6.7.2.1, C11 6.7.2.1, C23 6.7.3.2 */ 1248 /* Was named struct_declaration until C11. */ 1249 member_declaration: 1250 begin_type_qualifier_list end_type { 1251 /* ^^ There is no check for the missing type-specifier. */ 1252 /* too late, i know, but getsym() compensates it */ 1253 set_sym_kind(SK_MEMBER); 1254 } notype_member_declarator_list T_SEMI { 1255 set_sym_kind(SK_VCFT); 1256 $$ = $4; 1257 } 1258 | begin_type_specifier_qualifier_list end_type { 1259 set_sym_kind(SK_MEMBER); 1260 } type_member_declarator_list T_SEMI { 1261 set_sym_kind(SK_VCFT); 1262 $$ = $4; 1263 } 1264 | begin_type_qualifier_list end_type type_attribute_opt T_SEMI { 1265 /* syntax error '%s' */ 1266 error(249, "member without type"); 1267 $$ = NULL; 1268 } 1269 | begin_type_specifier_qualifier_list end_type T_SEMI { 1270 set_sym_kind(SK_VCFT); 1271 if (!allow_c11 && !allow_gcc) 1272 /* anonymous struct/union members is a C11 feature */ 1273 warning(49); 1274 if (is_struct_or_union(dcs->d_type->t_tspec)) 1275 $$ = declare_unnamed_member(); 1276 else { 1277 /* syntax error '%s' */ 1278 error(249, "unnamed member"); 1279 $$ = NULL; 1280 } 1281 } 1282 | static_assert_declaration { 1283 $$ = NULL; 1284 } 1285 | error T_SEMI { 1286 set_sym_kind(SK_VCFT); 1287 $$ = NULL; 1288 } 1289 ; 1290 1291 /* TODO: Implement 'specifier_qualifier_list' from C23 6.7.3.2. */ 1292 1293 /* TODO: Implement 'type_specifier_qualifier' from C23 6.7.3.2. */ 1294 1295 /* C23 6.7.3.2 */ 1296 /* The rule 'member_declarator_list' is split into the 'type' and 'notype' variants. */ 1297 /* Was named struct_declarator_list until C11. */ 1298 1299 notype_member_declarator_list: 1300 notype_member_declarator { 1301 $$ = declare_member($1); 1302 } 1303 | notype_member_declarator_list { 1304 set_sym_kind(SK_MEMBER); 1305 } T_COMMA type_member_declarator { 1306 $$ = concat_symbols($1, declare_member($4)); 1307 } 1308 ; 1309 1310 type_member_declarator_list: 1311 type_member_declarator { 1312 $$ = declare_member($1); 1313 } 1314 | type_member_declarator_list { 1315 set_sym_kind(SK_MEMBER); 1316 } T_COMMA type_member_declarator { 1317 $$ = concat_symbols($1, declare_member($4)); 1318 } 1319 ; 1320 1321 /* C23 6.7.3.2 */ 1322 /* The rule 'member_declarator' is split into the 'type' and 'notype' variants. */ 1323 /* Was named struct_declarator until C11. */ 1324 1325 notype_member_declarator: 1326 notype_declarator 1327 /* C99 6.7.2.1 */ 1328 | notype_declarator T_COLON constant_expression { 1329 $$ = set_bit_field_width($1, to_int_constant($3, true)); 1330 } 1331 /* C99 6.7.2.1 */ 1332 | { 1333 set_sym_kind(SK_VCFT); 1334 } T_COLON constant_expression { 1335 $$ = set_bit_field_width(NULL, to_int_constant($3, true)); 1336 } 1337 ; 1338 1339 type_member_declarator: 1340 type_declarator 1341 | type_declarator T_COLON constant_expression type_attribute_list_opt { 1342 $$ = set_bit_field_width($1, to_int_constant($3, true)); 1343 } 1344 | { 1345 set_sym_kind(SK_VCFT); 1346 } T_COLON constant_expression type_attribute_list_opt { 1347 $$ = set_bit_field_width(NULL, to_int_constant($3, true)); 1348 } 1349 ; 1350 1351 /* K&R ---, C90 6.5.2.2, C99 6.7.2.2, C11 6.7.2.2, C23 6.7.3.5 */ 1352 enum_specifier: 1353 enum gcc_attribute_specifier_list_opt identifier_sym { 1354 $$ = make_tag_type($3, ENUM, false, false); 1355 } 1356 | enum gcc_attribute_specifier_list_opt identifier_sym { 1357 dcs->d_tag_type = make_tag_type($3, ENUM, true, false); 1358 } enum_declaration /*gcc_attribute_specifier_list_opt*/ { 1359 $$ = complete_enum($5); 1360 } 1361 | enum gcc_attribute_specifier_list_opt { 1362 dcs->d_tag_type = make_tag_type(NULL, ENUM, true, false); 1363 } enum_declaration /*gcc_attribute_specifier_list_opt*/ { 1364 $$ = complete_enum($4); 1365 } 1366 | enum error { 1367 set_sym_kind(SK_VCFT); 1368 $$ = gettyp(INT); 1369 } 1370 ; 1371 1372 enum: /* helper for C99 6.7.2.2 */ 1373 T_ENUM { 1374 set_sym_kind(SK_TAG); 1375 begin_declaration_level(DLK_ENUM); 1376 } 1377 ; 1378 1379 enum_declaration: /* helper for C99 6.7.2.2 */ 1380 T_LBRACE { 1381 set_sym_kind(SK_VCFT); 1382 enumval = 0; 1383 } enums_with_opt_comma T_RBRACE { 1384 $$ = $3; 1385 } 1386 ; 1387 1388 enums_with_opt_comma: /* helper for C99 6.7.2.2 */ 1389 enumerator_list 1390 | enumerator_list T_COMMA { 1391 if (!allow_c99 && !allow_trad) 1392 /* trailing ',' in enum declaration requires C99 ... */ 1393 error(54); 1394 else 1395 /* trailing ',' in enum declaration requires C99 ... */ 1396 c99ism(54); 1397 $$ = $1; 1398 } 1399 ; 1400 1401 /* C99 6.7.2.2, C23 6.7.3.3 */ 1402 enumerator_list: 1403 enumerator 1404 | enumerator_list T_COMMA enumerator { 1405 $$ = concat_symbols($1, $3); 1406 } 1407 | error { 1408 $$ = NULL; 1409 } 1410 ; 1411 1412 /* C99 6.7.2.2, C23 6.7.3.3 */ 1413 enumerator: 1414 identifier_sym gcc_attribute_specifier_list_opt { 1415 $$ = enumeration_constant($1, enumval, true); 1416 } 1417 | identifier_sym gcc_attribute_specifier_list_opt 1418 T_ASSIGN constant_expression { 1419 $$ = enumeration_constant($1, to_int_constant($4, true), 1420 false); 1421 } 1422 ; 1423 1424 /* TODO: Implement 'enum_type_specifier' from C23 6.7.3.3. */ 1425 1426 /* K&R ---, C90 ---, C99 ---, C11 6.7.2.4, C23 6.7.3.5 */ 1427 atomic_type_specifier: 1428 atomic T_LPAREN type_name T_RPAREN { 1429 $$ = $3; 1430 } 1431 ; 1432 1433 atomic: /* helper */ 1434 T_ATOMIC { 1435 /* TODO: First fix c11ism, then use it here. */ 1436 if (!allow_c11) 1437 /* '_Atomic' requires C11 or later */ 1438 error(350); 1439 } 1440 ; 1441 1442 /* TODO: Implement 'typeof_specifier' from C23 6.7.3.6. */ 1443 1444 /* TODO: Implement 'typeof_specifier_argument' from C23 6.7.3.6. */ 1445 1446 /* C99 6.7.3, C23 6.7.4.1 */ 1447 type_qualifier: 1448 T_QUAL 1449 | atomic { 1450 $$ = (type_qualifiers){ .tq_atomic = true }; 1451 } 1452 ; 1453 1454 /* TODO: Implement 'function_specifier' from C23 6.7.5. */ 1455 1456 /* TODO: Implement 'alignment_specifier' from C23 6.7.6. */ 1457 1458 /* C23 6.7.7.1 */ 1459 /* The rule 'declarator' is split into the 'notype' and 'type' variants. */ 1460 1461 notype_declarator: 1462 notype_direct_declarator 1463 | pointer notype_direct_declarator { 1464 $$ = add_pointer($2, $1); 1465 } 1466 ; 1467 1468 type_declarator: 1469 type_direct_declarator 1470 | pointer type_direct_declarator { 1471 $$ = add_pointer($2, $1); 1472 } 1473 ; 1474 1475 /* C23 6.7.7.1 */ 1476 /* The rule 'direct_declarator' is split into the 'notype' and 'type' variants. */ 1477 1478 notype_direct_declarator: 1479 type_attribute_list_opt T_NAME { 1480 $$ = declarator_name(getsym($2)); 1481 } 1482 | type_attribute_list_opt T_LPAREN type_declarator T_RPAREN { 1483 $$ = $3; 1484 } 1485 | notype_direct_declarator T_LBRACK array_size_opt T_RBRACK { 1486 $$ = add_array($1, $3.has_dim, $3.dim); 1487 } 1488 | notype_direct_declarator param_list asm_or_symbolrename_opt { 1489 $$ = add_function(symbolrename($1, $3), $2); 1490 end_declaration_level(); 1491 block_level--; 1492 } 1493 | notype_direct_declarator type_attribute 1494 ; 1495 1496 type_direct_declarator: 1497 type_attribute_list_opt identifier { 1498 $$ = declarator_name(getsym($2)); 1499 } 1500 | type_attribute_list_opt T_LPAREN type_declarator T_RPAREN { 1501 $$ = $3; 1502 } 1503 | type_direct_declarator T_LBRACK array_size_opt T_RBRACK { 1504 $$ = add_array($1, $3.has_dim, $3.dim); 1505 } 1506 | type_direct_declarator param_list asm_or_symbolrename_opt { 1507 $$ = add_function(symbolrename($1, $3), $2); 1508 end_declaration_level(); 1509 block_level--; 1510 if ($2.used) 1511 $$->s_used = true; 1512 } 1513 | type_direct_declarator type_attribute { 1514 $$ = $1; 1515 if ($2.used) 1516 $$->s_used = true; 1517 } 1518 ; 1519 1520 1521 /* TODO: Implement 'array_declarator' from C23 6.7.7.1. */ 1522 1523 /* TODO: Implement 'function_declarator' from C23 6.7.7.1. */ 1524 1525 /* C99 6.7.5, C23 6.7.7.1 */ 1526 pointer: 1527 T_ASTERISK type_qualifier_list_opt { 1528 $$ = xcalloc(1, sizeof(*$$)); 1529 add_type_qualifiers(&$$->qualifiers, $2); 1530 } 1531 | T_ASTERISK type_qualifier_list_opt pointer { 1532 $$ = xcalloc(1, sizeof(*$$)); 1533 add_type_qualifiers(&$$->qualifiers, $2); 1534 $$ = append_qualified_pointer($$, $3); 1535 } 1536 ; 1537 1538 /* see C99 6.7.5, C23 6.7.7.1 */ 1539 type_qualifier_list_opt: 1540 /* empty */ { 1541 $$ = (type_qualifiers){ .tq_const = false }; 1542 } 1543 | type_qualifier_list 1544 ; 1545 1546 /* C99 6.7.5 */ 1547 type_qualifier_list: 1548 type_qualifier 1549 | type_qualifier_list type_qualifier { 1550 $$ = $1; 1551 add_type_qualifiers(&$$, $2); 1552 } 1553 ; 1554 1555 /* TODO: Implement 'parameter_type_list' from C23 6.7.7.1. */ 1556 1557 /* TODO: Implement 'parameter_list' from C23 6.7.7.1. */ 1558 1559 /* C23 6.7.7.1 */ 1560 /* XXX: C99 6.7.5 defines the same name, but it looks completely different. */ 1561 parameter_declaration: 1562 begin_type_declmods end_type { 1563 /* ^^ There is no check for the missing type-specifier. */ 1564 $$ = declare_parameter(abstract_name(), false); 1565 } 1566 | begin_type_declaration_specifiers end_type { 1567 $$ = declare_parameter(abstract_name(), false); 1568 } 1569 | begin_type_declmods end_type notype_param_declarator { 1570 /* ^^ There is no check for the missing type-specifier. */ 1571 $$ = declare_parameter($3, false); 1572 } 1573 /* 1574 * type_param_declarator is needed because of following conflict: 1575 * "typedef int a; f(int (a));" could be parsed as 1576 * "function with argument a of type int", or 1577 * "function with an unnamed (abstract) argument of type function". 1578 * This grammar realizes the second case. 1579 */ 1580 | begin_type_declaration_specifiers end_type type_param_declarator { 1581 $$ = declare_parameter($3, false); 1582 } 1583 | begin_type_declmods end_type abstract_declarator { 1584 /* ^^ There is no check for the missing type-specifier. */ 1585 $$ = declare_parameter($3, false); 1586 } 1587 | begin_type_declaration_specifiers end_type abstract_declarator { 1588 $$ = declare_parameter($3, false); 1589 } 1590 ; 1591 1592 /* 1593 * The two distinct rules type_param_declarator and notype_param_declarator 1594 * avoid a conflict in parameter lists. A typename enclosed in parentheses is 1595 * always treated as a typename, not an argument name. For example, after 1596 * "typedef double a;", the declaration "f(int (a));" is interpreted as 1597 * "f(int (double));", not "f(int a);". 1598 */ 1599 type_param_declarator: 1600 direct_param_declarator 1601 | pointer direct_param_declarator { 1602 $$ = add_pointer($2, $1); 1603 } 1604 ; 1605 1606 notype_param_declarator: 1607 direct_notype_param_declarator 1608 | pointer direct_notype_param_declarator { 1609 $$ = add_pointer($2, $1); 1610 } 1611 ; 1612 1613 direct_param_declarator: 1614 identifier type_attribute_list { 1615 $$ = declarator_name(getsym($1)); 1616 if ($2.used) 1617 dcs_set_used(); 1618 } 1619 | identifier { 1620 $$ = declarator_name(getsym($1)); 1621 } 1622 | T_LPAREN notype_param_declarator T_RPAREN { 1623 $$ = $2; 1624 } 1625 | direct_param_declarator T_LBRACK array_size_opt T_RBRACK 1626 gcc_attribute_specifier_list_opt { 1627 $$ = add_array($1, $3.has_dim, $3.dim); 1628 if ($5.used) 1629 dcs_set_used(); 1630 } 1631 | direct_param_declarator param_list asm_or_symbolrename_opt { 1632 $$ = add_function(symbolrename($1, $3), $2); 1633 end_declaration_level(); 1634 block_level--; 1635 if ($2.used) 1636 dcs_set_used(); 1637 } 1638 ; 1639 1640 direct_notype_param_declarator: 1641 identifier { 1642 $$ = declarator_name(getsym($1)); 1643 } 1644 | T_LPAREN notype_param_declarator T_RPAREN { 1645 $$ = $2; 1646 } 1647 | direct_notype_param_declarator T_LBRACK array_size_opt T_RBRACK { 1648 $$ = add_array($1, $3.has_dim, $3.dim); 1649 } 1650 | direct_notype_param_declarator param_list asm_or_symbolrename_opt { 1651 $$ = add_function(symbolrename($1, $3), $2); 1652 end_declaration_level(); 1653 block_level--; 1654 } 1655 ; 1656 1657 param_list: 1658 T_LPAREN { 1659 block_level++; 1660 begin_declaration_level(DLK_PROTO_PARAMS); 1661 } identifier_list T_RPAREN { 1662 $$ = (parameter_list){ .first = $3, .identifier = true }; 1663 } 1664 | abstract_decl_param_list 1665 ; 1666 1667 array_size_opt: 1668 /* empty */ { 1669 $$.has_dim = false; 1670 $$.dim = 0; 1671 } 1672 | T_ASTERISK { 1673 /* since C99; variable length array of unspecified size */ 1674 $$.has_dim = false; /* TODO: maybe change to true */ 1675 $$.dim = 0; /* just as a placeholder */ 1676 } 1677 | type_qualifier_list_opt T_SCLASS constant_expression { 1678 /* C11 6.7.6.3p7 */ 1679 if ($2 != STATIC) 1680 yyerror("Bad attribute"); 1681 /* static array size requires C11 or later */ 1682 c11ism(343); 1683 $$.has_dim = true; 1684 $$.dim = $3 == NULL ? 0 : to_int_constant($3, false); 1685 } 1686 | type_qualifier { 1687 /* C11 6.7.6.2 */ 1688 if (!$1.tq_restrict) 1689 yyerror("Bad attribute"); 1690 $$.has_dim = true; 1691 $$.dim = 0; 1692 } 1693 | constant_expression { 1694 $$.has_dim = true; 1695 $$.dim = $1 == NULL ? 0 : to_int_constant($1, false); 1696 } 1697 ; 1698 1699 identifier_list: /* C99 6.7.5 */ 1700 T_NAME { 1701 $$ = old_style_function_parameter_name(getsym($1)); 1702 } 1703 | identifier_list T_COMMA T_NAME { 1704 $$ = concat_symbols($1, 1705 old_style_function_parameter_name(getsym($3))); 1706 } 1707 | identifier_list error 1708 ; 1709 1710 /* C99 6.7.6, C23 6.7.8 */ 1711 /* XXX: C99 requires an additional specifier-qualifier-list. */ 1712 type_name: 1713 { 1714 begin_declaration_level(DLK_ABSTRACT); 1715 } abstract_declaration { 1716 end_declaration_level(); 1717 $$ = $2->s_type; 1718 } 1719 ; 1720 1721 abstract_declaration: /* specific to lint */ 1722 begin_type_qualifier_list end_type { 1723 $$ = declare_abstract_type(abstract_name()); 1724 } 1725 | begin_type_specifier_qualifier_list end_type { 1726 $$ = declare_abstract_type(abstract_name()); 1727 } 1728 | begin_type_qualifier_list end_type abstract_declarator { 1729 $$ = declare_abstract_type($3); 1730 } 1731 | begin_type_specifier_qualifier_list end_type abstract_declarator { 1732 $$ = declare_abstract_type($3); 1733 } 1734 ; 1735 1736 abstract_decl_param_list: /* specific to lint */ 1737 abstract_decl_lparen T_RPAREN type_attribute_list_opt { 1738 $$ = (parameter_list){ .used = $3.used }; 1739 } 1740 | abstract_decl_lparen vararg_parameter_type_list T_RPAREN 1741 type_attribute_list_opt { 1742 $$ = $2; 1743 $$.prototype = true; 1744 $$.used = $4.used; 1745 $$.noreturn = $4.noreturn; 1746 } 1747 | abstract_decl_lparen error T_RPAREN type_attribute_list_opt { 1748 $$ = (parameter_list){ .used = $4.used }; 1749 } 1750 ; 1751 1752 abstract_decl_lparen: /* specific to lint */ 1753 T_LPAREN { 1754 block_level++; 1755 begin_declaration_level(DLK_PROTO_PARAMS); 1756 } 1757 ; 1758 1759 vararg_parameter_type_list: /* specific to lint */ 1760 parameter_type_list 1761 | parameter_type_list T_COMMA T_ELLIPSIS { 1762 $$ = $1; 1763 $$.vararg = true; 1764 } 1765 | T_ELLIPSIS { 1766 /* TODO: C99 6.7.5 makes this an error as well. */ 1767 if (!allow_trad && !allow_c99) 1768 /* C90 to C17 require formal parameter before '...' */ 1769 error(84); 1770 else if (allow_c90) 1771 /* C90 to C17 require formal parameter before '...' */ 1772 warning(84); 1773 $$ = (parameter_list){ .vararg = true }; 1774 } 1775 ; 1776 1777 /* XXX: C99 6.7.5 defines the same name, but it looks different. */ 1778 parameter_type_list: 1779 parameter_declaration { 1780 $$ = (parameter_list){ .first = $1 }; 1781 } 1782 | parameter_type_list T_COMMA parameter_declaration { 1783 $$ = $1; 1784 $$.first = concat_symbols($1.first, $3); 1785 } 1786 ; 1787 1788 /* K&R 8.7, C90 ???, C99 6.7.6, C11 6.7.7, C23 6.7.8 */ 1789 /* In K&R, abstract-declarator could be empty and was still simpler. */ 1790 abstract_declarator: 1791 pointer { 1792 $$ = add_pointer(abstract_name(), $1); 1793 } 1794 | direct_abstract_declarator 1795 | pointer direct_abstract_declarator { 1796 $$ = add_pointer($2, $1); 1797 } 1798 | type_attribute_list direct_abstract_declarator { 1799 $$ = $2; 1800 } 1801 | pointer type_attribute_list direct_abstract_declarator { 1802 $$ = add_pointer($3, $1); 1803 } 1804 ; 1805 1806 /* K&R ---, C90 ???, C99 6.7.6, C11 6.7.7, C23 6.7.8 */ 1807 direct_abstract_declarator: 1808 /* TODO: sort rules according to C99 */ 1809 T_LPAREN abstract_declarator T_RPAREN { 1810 $$ = $2; 1811 } 1812 | T_LBRACK array_size_opt T_RBRACK { 1813 $$ = add_array(abstract_name(), $2.has_dim, $2.dim); 1814 } 1815 | direct_abstract_declarator T_LBRACK array_size_opt T_RBRACK { 1816 $$ = add_array($1, $3.has_dim, $3.dim); 1817 } 1818 | abstract_decl_param_list asm_or_symbolrename_opt { 1819 sym_t *name = abstract_enclosing_name(); 1820 $$ = add_function(symbolrename(name, $2), $1); 1821 end_declaration_level(); 1822 block_level--; 1823 } 1824 | direct_abstract_declarator abstract_decl_param_list 1825 asm_or_symbolrename_opt { 1826 $$ = add_function(symbolrename($1, $3), $2); 1827 end_declaration_level(); 1828 block_level--; 1829 } 1830 | direct_abstract_declarator type_attribute_list 1831 ; 1832 1833 /* TODO: Implement 'array_abstract_declarator' from C23 6.7.8. */ 1834 1835 /* TODO: Implement 'function_abstract_declarator' from C23 6.7.8. */ 1836 1837 /* TODO: Implement 'typedef_name' from C23 6.7.9. */ 1838 1839 /* C23 6.7.11 */ 1840 /* K&R ---, C90 ---, C99 6.7.8, C11 6.7.9, C23 6.7.10 */ 1841 braced_initializer: 1842 init_lbrace init_rbrace { 1843 /* empty initializer braces require C23 or later */ 1844 c23ism(353); 1845 } 1846 | init_lbrace initializer_list init_rbrace 1847 | init_lbrace initializer_list T_COMMA init_rbrace 1848 ; 1849 1850 /* C99 6.7.8, C23 6.7.11 */ 1851 initializer: 1852 assignment_expression { 1853 init_expr($1); 1854 } 1855 | init_lbrace init_rbrace { 1856 /* XXX: Empty braces are not covered by C99 6.7.8. */ 1857 } 1858 | init_lbrace initializer_list init_rbrace 1859 | init_lbrace initializer_list T_COMMA init_rbrace 1860 /* XXX: What is this error handling for? */ 1861 | error 1862 ; 1863 1864 /* C99 6.7.8, C23 6.7.11 */ 1865 initializer_list: 1866 initializer 1867 | designation initializer 1868 | initializer_list T_COMMA initializer 1869 | initializer_list T_COMMA designation initializer 1870 ; 1871 1872 /* C99 6.7.8, C23 6.7.11 */ 1873 designation: 1874 { 1875 begin_designation(); 1876 } designator_list T_ASSIGN 1877 | identifier T_COLON { 1878 /* GCC style struct or union member name in initializer */ 1879 gnuism(315); 1880 begin_designation(); 1881 add_designator_member($1); 1882 } 1883 ; 1884 1885 /* C99 6.7.8, C23 6.7.11 */ 1886 designator_list: 1887 designator 1888 | designator_list designator 1889 ; 1890 1891 /* C99 6.7.8, C23 6.7.11 */ 1892 designator: 1893 T_LBRACK range T_RBRACK { 1894 if (!allow_c99) 1895 /* array initializer with designators is a C99 ... */ 1896 warning(321); 1897 add_designator_subscript($2); 1898 } 1899 | T_POINT identifier { 1900 if (!allow_c99) 1901 /* struct or union member name in initializer is ... */ 1902 warning(313); 1903 add_designator_member($2); 1904 } 1905 ; 1906 1907 /* C23 6.7.12 */ 1908 static_assert_declaration: 1909 T_STATIC_ASSERT T_LPAREN constant_expression T_COMMA T_STRING 1910 T_RPAREN T_SEMI { 1911 /* '_Static_assert' requires C11 or later */ 1912 c11ism(354); 1913 } 1914 | T_STATIC_ASSERT T_LPAREN constant_expression T_RPAREN T_SEMI { 1915 /* '_Static_assert' without message requires C23 or later */ 1916 c23ism(355); 1917 } 1918 ; 1919 1920 range: 1921 constant_expression { 1922 $$.lo = to_int_constant($1, true); 1923 $$.hi = $$.lo; 1924 } 1925 | constant_expression T_ELLIPSIS constant_expression { 1926 $$.lo = to_int_constant($1, true); 1927 $$.hi = to_int_constant($3, true); 1928 /* initialization with '[a...b]' is a GCC extension */ 1929 gnuism(340); 1930 } 1931 ; 1932 1933 init_lbrace: /* helper */ 1934 T_LBRACE { 1935 init_lbrace(); 1936 } 1937 ; 1938 1939 init_rbrace: /* helper */ 1940 T_RBRACE { 1941 init_rbrace(); 1942 } 1943 ; 1944 1945 /* C23 6.7.13.2 */ 1946 attribute_specifier_sequence: 1947 attribute_specifier { 1948 $$ = (attribute_list) { NULL, 0, 0 }; 1949 attribute_list_add_all(&$$, $1); 1950 } 1951 | attribute_specifier_sequence attribute_specifier { 1952 $$ = $1; 1953 attribute_list_add_all(&$$, $2); 1954 } 1955 ; 1956 1957 /* C23 6.7.13.2 */ 1958 attribute_specifier: 1959 T_LBRACK T_LBRACK attribute_list T_RBRACK T_RBRACK { 1960 $$ = $3; 1961 } 1962 ; 1963 1964 /* C23 6.7.13.2 */ 1965 attribute_list: 1966 /* empty */ { 1967 $$ = (attribute_list) { NULL, 0, 0 }; 1968 } 1969 | attribute { 1970 $$ = (attribute_list) { NULL, 0, 0 }; 1971 attribute_list_add(&$$, $1); 1972 } 1973 | attribute_list T_COMMA 1974 | attribute_list T_COMMA attribute { 1975 $$ = $1; 1976 attribute_list_add(&$$, $3); 1977 } 1978 ; 1979 1980 /* C23 6.7.13.2 */ 1981 attribute: 1982 identifier { 1983 $$ = new_attribute(NULL, $1, NULL); 1984 } 1985 | identifier T_DCOLON identifier { 1986 $$ = new_attribute($1, $3, NULL); 1987 } 1988 | identifier attribute_argument_clause { 1989 $$ = new_attribute(NULL, $1, &$2); 1990 } 1991 | identifier T_DCOLON identifier attribute_argument_clause { 1992 $$ = new_attribute($1, $3, &$4); 1993 } 1994 ; 1995 1996 /* The rule 'attribute_token' is inlined into 'attribute'. */ 1997 /* The rule 'standard_attribute' is inlined into 'attribute_token'. */ 1998 /* The rule 'attribute_prefixed_token' is inlined into 'attribute_token'. */ 1999 /* The rule 'attribute_prefix' is inlined into 'attribute_token'. */ 2000 2001 /* C23 6.7.13.2 */ 2002 attribute_argument_clause: 2003 T_LPAREN { 2004 $$ = read_balanced_token_sequence(); 2005 } 2006 ; 2007 2008 /* The rule 'balanced_token_sequence' is inlined into 'attribute_argument_clause'. */ 2009 /* The rule 'balanced_token' is inlined into 'balanced_token_sequence'. */ 2010 2011 asm_or_symbolrename_opt: /* GCC extensions */ 2012 /* empty */ { 2013 $$ = NULL; 2014 } 2015 | T_ASM T_LPAREN T_STRING T_RPAREN gcc_attribute_specifier_list_opt { 2016 freeyyv(&$3, T_STRING); 2017 $$ = NULL; 2018 } 2019 | T_SYMBOLRENAME T_LPAREN T_NAME T_RPAREN 2020 gcc_attribute_specifier_list_opt { 2021 $$ = $3; 2022 } 2023 ; 2024 2025 /* K&R ???, C90 ???, C99 6.8, C11 ???, C23 6.8.1 */ 2026 statement: 2027 expression_statement 2028 | non_expr_statement 2029 ; 2030 2031 /* Helper to avoid shift/reduce conflict in 'label: __attribute__ ;'. */ 2032 no_attr_statement: 2033 expression_statement 2034 | no_attr_non_expr_statement 2035 ; 2036 2037 non_expr_statement: /* helper for C99 6.8 */ 2038 gcc_attribute_specifier /* ((__fallthrough__)) */ T_SEMI 2039 | no_attr_non_expr_statement 2040 ; 2041 2042 /* Helper to avoid shift/reduce conflict in 'label: __attribute__ ;'. */ 2043 no_attr_non_expr_statement: 2044 labeled_statement 2045 | compound_statement 2046 | selection_statement 2047 | iteration_statement 2048 | jump_statement { 2049 suppress_fallthrough = false; 2050 } 2051 | asm_statement 2052 ; 2053 2054 /* TODO: Implement 'unlabeled_statement' from C23 6.8.1. */ 2055 2056 /* TODO: Implement 'primary_block' from C23 6.8.1. */ 2057 2058 /* TODO: Implement 'secondary_block' from C23 6.8.1. */ 2059 2060 /* C23 6.8.2 */ 2061 label: 2062 T_NAME T_COLON { 2063 set_sym_kind(SK_LABEL); 2064 named_label(getsym($1)); 2065 } 2066 | T_CASE constant_expression T_COLON { 2067 case_label($2); 2068 suppress_fallthrough = true; 2069 } 2070 | T_CASE constant_expression T_ELLIPSIS constant_expression T_COLON { 2071 /* XXX: We don't fill all cases */ 2072 case_label($2); 2073 suppress_fallthrough = true; 2074 } 2075 | T_DEFAULT T_COLON { 2076 default_label(); 2077 suppress_fallthrough = true; 2078 } 2079 ; 2080 2081 /* C99 6.8.1, C23 6.8.2 */ 2082 labeled_statement: 2083 label gcc_attribute_specifier_list_opt no_attr_statement 2084 ; 2085 2086 /* C99 6.8.2, C23 6.8.3 */ 2087 compound_statement: 2088 compound_statement_lbrace compound_statement_rbrace 2089 | compound_statement_lbrace block_item_list compound_statement_rbrace 2090 ; 2091 2092 compound_statement_lbrace: 2093 T_LBRACE { 2094 block_level++; 2095 mem_block_level++; 2096 debug_step("%s: mem_block_level = %zu", 2097 "compound_statement_lbrace", mem_block_level); 2098 begin_declaration_level(DLK_AUTO); 2099 } 2100 ; 2101 2102 compound_statement_rbrace: 2103 T_RBRACE { 2104 end_declaration_level(); 2105 if (!in_statement_expr()) 2106 level_free_all(mem_block_level); /* leak */ 2107 mem_block_level--; 2108 debug_step("%s: mem_block_level = %zu", 2109 "compound_statement_rbrace", mem_block_level); 2110 block_level--; 2111 suppress_fallthrough = false; 2112 } 2113 ; 2114 2115 /* C99 6.8.2, C23 6.8.3 */ 2116 block_item_list: 2117 block_item 2118 | block_item_list block_item { 2119 if ($1 && !$2) 2120 /* declarations after statements is a C99 feature */ 2121 c99ism(327); 2122 $$ = $1 || $2; 2123 } 2124 ; 2125 2126 /* C99 6.8.2, C23 6.8.3 */ 2127 block_item: 2128 declaration_or_error { 2129 $$ = false; 2130 restore_warning_flags(); 2131 } 2132 | statement { 2133 $$ = true; 2134 restore_warning_flags(); 2135 } 2136 ; 2137 2138 /* C99 6.8.3, C23 6.8.4 */ 2139 expression_statement: 2140 expression T_SEMI { 2141 /* 2142 * Even though a "call statement" is not a formally defined 2143 * term in the C standards, it occurs so often that it's 2144 * helpful to have a distinguishable term for it. 2145 */ 2146 expr($1, false, false, false, false, 2147 $1 != NULL && $1->tn_op == CALL ? "call" : "expression"); 2148 suppress_fallthrough = false; 2149 if ($1 != NULL && $1->tn_op == CALL 2150 && $1->u.call->func->tn_type->t_subt->t_noreturn) 2151 stmt_call_noreturn(); 2152 } 2153 | T_SEMI { 2154 check_statement_reachable("empty"); 2155 suppress_fallthrough = false; 2156 } 2157 | attribute_specifier_sequence expression T_SEMI { 2158 debug_attribute_list(&$1); 2159 /* 2160 * Even though a "call statement" is not a formally defined 2161 * term in the C standards, it occurs so often that it's 2162 * helpful to have a distinguishable term for it. 2163 */ 2164 expr($2, false, false, false, false, 2165 $2 != NULL && $2->tn_op == CALL ? "call" : "expression"); 2166 suppress_fallthrough = false; 2167 } 2168 | attribute_specifier_sequence T_SEMI { 2169 bool is_fallthrough = attributes_contain(&$1, "fallthrough"); 2170 debug_attribute_list(&$1); 2171 check_statement_reachable( 2172 is_fallthrough ? "fallthrough" : "empty"); 2173 suppress_fallthrough = is_fallthrough; 2174 } 2175 ; 2176 2177 /* C99 6.8.4, C23 6.8.5.1 */ 2178 selection_statement: 2179 if_without_else %prec T_THEN { 2180 save_warning_flags(); 2181 stmt_if_then_stmt(); 2182 stmt_if_else_stmt(false); 2183 } 2184 | if_without_else T_ELSE { 2185 save_warning_flags(); 2186 stmt_if_then_stmt(); 2187 } statement { 2188 restore_warning_flags(); 2189 stmt_if_else_stmt(true); 2190 } 2191 | if_without_else T_ELSE error { 2192 clear_warning_flags(); 2193 stmt_if_else_stmt(false); 2194 } 2195 | switch_expr statement { 2196 clear_warning_flags(); 2197 stmt_switch_expr_stmt(); 2198 } 2199 | switch_expr error { 2200 clear_warning_flags(); 2201 stmt_switch_expr_stmt(); 2202 } 2203 ; 2204 2205 if_without_else: /* see C99 6.8.4 */ 2206 if_expr statement 2207 | if_expr error 2208 ; 2209 2210 if_expr: /* see C99 6.8.4 */ 2211 T_IF T_LPAREN expression T_RPAREN { 2212 stmt_if_expr($3); 2213 clear_warning_flags(); 2214 } 2215 ; 2216 2217 switch_expr: /* see C99 6.8.4 */ 2218 T_SWITCH T_LPAREN expression T_RPAREN { 2219 stmt_switch_expr($3); 2220 clear_warning_flags(); 2221 } 2222 ; 2223 2224 /* C99 6.8.5, C23 6.8.6.1 */ 2225 iteration_statement: 2226 while_expr statement { 2227 clear_warning_flags(); 2228 stmt_while_expr_stmt(); 2229 } 2230 | while_expr error { 2231 clear_warning_flags(); 2232 stmt_while_expr_stmt(); 2233 } 2234 | do_statement T_WHILE T_LPAREN expression T_RPAREN T_SEMI { 2235 stmt_do_while_expr($4); 2236 suppress_fallthrough = false; 2237 } 2238 | do error { 2239 clear_warning_flags(); 2240 stmt_do_while_expr(NULL); 2241 } 2242 | for_exprs statement { 2243 clear_warning_flags(); 2244 stmt_for_exprs_stmt(); 2245 end_declaration_level(); 2246 block_level--; 2247 } 2248 | for_exprs error { 2249 clear_warning_flags(); 2250 stmt_for_exprs_stmt(); 2251 end_declaration_level(); 2252 block_level--; 2253 } 2254 ; 2255 2256 while_expr: /* see C99 6.8.5 */ 2257 T_WHILE T_LPAREN expression T_RPAREN { 2258 stmt_while_expr($3); 2259 clear_warning_flags(); 2260 } 2261 ; 2262 2263 do_statement: /* see C99 6.8.5 */ 2264 do statement { 2265 clear_warning_flags(); 2266 } 2267 ; 2268 2269 do: /* see C99 6.8.5 */ 2270 T_DO { 2271 stmt_do(); 2272 } 2273 ; 2274 2275 for_start: /* see C99 6.8.5 */ 2276 T_FOR T_LPAREN { 2277 begin_declaration_level(DLK_AUTO); 2278 block_level++; 2279 } 2280 ; 2281 2282 for_exprs: /* see C99 6.8.5 */ 2283 for_start 2284 begin_type_declaration_specifiers end_type 2285 notype_init_declarator_list T_SEMI 2286 expression_opt T_SEMI 2287 expression_opt T_RPAREN { 2288 /* variable declaration in for loop */ 2289 c99ism(325); 2290 stmt_for_exprs(NULL, $6, $8); 2291 clear_warning_flags(); 2292 } 2293 | for_start 2294 expression_opt T_SEMI 2295 expression_opt T_SEMI 2296 expression_opt T_RPAREN { 2297 stmt_for_exprs($2, $4, $6); 2298 clear_warning_flags(); 2299 } 2300 ; 2301 2302 /* C99 6.8.6, C23 6.8.7.1 */ 2303 jump_statement: 2304 goto identifier T_SEMI { 2305 stmt_goto(getsym($2)); 2306 } 2307 | goto error T_SEMI { 2308 set_sym_kind(SK_VCFT); 2309 } 2310 | T_CONTINUE T_SEMI { 2311 stmt_continue(); 2312 } 2313 | T_BREAK T_SEMI { 2314 stmt_break(); 2315 } 2316 | T_RETURN sys T_SEMI { 2317 stmt_return($2, NULL); 2318 } 2319 | T_RETURN sys expression T_SEMI { 2320 stmt_return($2, $3); 2321 } 2322 ; 2323 2324 goto: /* see C99 6.8.6 */ 2325 T_GOTO { 2326 set_sym_kind(SK_LABEL); 2327 } 2328 ; 2329 2330 asm_statement: /* GCC extension */ 2331 T_ASM T_LPAREN read_until_rparen T_SEMI { 2332 dcs_set_asm(); 2333 } 2334 | T_ASM type_qualifier T_LPAREN read_until_rparen T_SEMI { 2335 dcs_set_asm(); 2336 } 2337 | T_ASM error 2338 ; 2339 2340 read_until_rparen: /* helper for 'asm_statement' */ 2341 /* empty */ { 2342 read_until_rparen(); 2343 } 2344 ; 2345 2346 /* C99 6.9, C23 6.9.1 */ 2347 translation_unit: 2348 external_declaration 2349 | translation_unit external_declaration 2350 ; 2351 2352 /* C99 6.9, C23 6.9.1 */ 2353 external_declaration: 2354 function_definition { 2355 global_clean_up_decl(false); 2356 clear_warning_flags(); 2357 } 2358 | top_level_declaration { 2359 global_clean_up_decl(false); 2360 clear_warning_flags(); 2361 } 2362 | asm_statement /* GCC extension */ 2363 | T_SEMI { /* GCC extension */ 2364 /* 2365 * TODO: Only allow this in GCC mode, not in plain C99. 2366 * This is one of the top 10 warnings in the NetBSD build. 2367 */ 2368 if (!allow_trad && !allow_c99) 2369 /* empty declaration */ 2370 error(0); 2371 else if (allow_c90) 2372 /* empty declaration */ 2373 warning(0); 2374 } 2375 ; 2376 2377 /* 2378 * On the top level, lint allows several forms of declarations that it doesn't 2379 * allow in functions. For example, a single ';' is an empty declaration and 2380 * is supported by some compilers, but in a function it would be an empty 2381 * statement, not a declaration. This makes a difference in C90 mode, where 2382 * a statement must not be followed by a declaration. 2383 * 2384 * See 'declaration' for all other declarations. 2385 */ 2386 top_level_declaration: /* C99 6.9 calls this 'declaration' */ 2387 begin_type end_type notype_init_declarator_list T_SEMI { 2388 /* TODO: Make this an error in C99 mode as well. */ 2389 if (!allow_trad && !allow_c99) 2390 /* old-style declaration; add 'int' */ 2391 error(1); 2392 else if (allow_c90) 2393 /* old-style declaration; add 'int' */ 2394 warning(1); 2395 } 2396 | declaration 2397 | error T_SEMI { 2398 global_clean_up(); 2399 } 2400 | error T_RBRACE { 2401 global_clean_up(); 2402 } 2403 ; 2404 2405 /* C99 6.9.1, C23 6.9.2 */ 2406 function_definition: 2407 func_declarator { 2408 if ($1->s_type->t_tspec != FUNC) { 2409 /* syntax error '%s' */ 2410 error(249, yytext); 2411 YYERROR; 2412 } 2413 if ($1->s_type->t_typedef) { 2414 /* ()-less function definition */ 2415 error(64); 2416 YYERROR; 2417 } 2418 check_extern_declaration($1); 2419 begin_function($1); 2420 block_level++; 2421 begin_declaration_level(DLK_OLD_STYLE_PARAMS); 2422 if (lwarn == LWARN_NONE) 2423 $1->s_used = true; 2424 } arg_declaration_list_opt { 2425 end_declaration_level(); 2426 block_level--; 2427 check_func_lint_directives(); 2428 check_func_old_style_parameters(); 2429 begin_control_statement(CS_FUNCTION_BODY); 2430 } compound_statement { 2431 end_function(); 2432 end_control_statement(CS_FUNCTION_BODY); 2433 } 2434 ; 2435 2436 func_declarator: 2437 begin_type end_type notype_declarator { 2438 if (!allow_trad) 2439 /* old-style declaration; add 'int' */ 2440 error(1); 2441 $$ = $3; 2442 } 2443 | begin_type_declmods end_type notype_declarator { 2444 if (!allow_trad) 2445 /* old-style declaration; add 'int' */ 2446 error(1); 2447 $$ = $3; 2448 } 2449 | begin_type_declaration_specifiers end_type type_declarator { 2450 $$ = $3; 2451 } 2452 ; 2453 2454 arg_declaration_list_opt: /* C99 6.9.1p13 example 1 */ 2455 /* empty */ 2456 | arg_declaration_list 2457 ; 2458 2459 arg_declaration_list: /* C99 6.9.1p13 example 1 */ 2460 arg_declaration 2461 | arg_declaration_list arg_declaration 2462 /* XXX or better "arg_declaration error" ? */ 2463 | error 2464 ; 2465 2466 /* 2467 * "arg_declaration" is separated from "declaration" because it 2468 * needs other error handling. 2469 */ 2470 arg_declaration: 2471 begin_type_declmods end_type T_SEMI { 2472 /* empty declaration */ 2473 warning(2); 2474 } 2475 | begin_type_declmods end_type notype_init_declarator_list T_SEMI 2476 | begin_type_declaration_specifiers end_type T_SEMI { 2477 if (!dcs->d_nonempty_decl) 2478 /* empty declaration */ 2479 warning(2); 2480 else 2481 /* '%s' declared in parameter declaration list */ 2482 warning(3, type_name(dcs->d_type)); 2483 } 2484 | begin_type_declaration_specifiers end_type 2485 type_init_declarator_list T_SEMI { 2486 if (dcs->d_nonempty_decl) 2487 /* '%s' declared in parameter declaration list */ 2488 warning(3, type_name(dcs->d_type)); 2489 } 2490 | begin_type_declmods error 2491 | begin_type_declaration_specifiers error 2492 ; 2493 2494 /* https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html */ 2495 gcc_attribute_specifier_list_opt: 2496 /* empty */ { 2497 $$ = (type_attributes){ .used = false }; 2498 } 2499 | gcc_attribute_specifier_list 2500 ; 2501 2502 gcc_attribute_specifier_list: 2503 gcc_attribute_specifier 2504 | gcc_attribute_specifier_list gcc_attribute_specifier { 2505 $$ = (type_attributes){ .used = $1.used || $2.used }; 2506 } 2507 ; 2508 2509 gcc_attribute_specifier: 2510 T_ATTRIBUTE T_LPAREN T_LPAREN { 2511 in_gcc_attribute = true; 2512 } gcc_attribute_list { 2513 in_gcc_attribute = false; 2514 } T_RPAREN T_RPAREN { 2515 $$ = $5; 2516 } 2517 ; 2518 2519 gcc_attribute_list: 2520 gcc_attribute 2521 | gcc_attribute_list T_COMMA gcc_attribute { 2522 $$ = (type_attributes){ .used = $1.used || $3.used }; 2523 } 2524 ; 2525 2526 gcc_attribute: 2527 /* empty */ { 2528 $$ = (type_attributes){ .used = false }; 2529 } 2530 | T_NAME { 2531 $$ = (type_attributes){ .used = false }; 2532 const char *name = $1->sb_name; 2533 if (is_either(name, "packed", "__packed__")) 2534 dcs_add_packed(); 2535 else if (is_either(name, "used", "__used__") || 2536 is_either(name, "constructor", "__constructor__") || 2537 is_either(name, "unused", "__unused__")) 2538 $$.used = true; 2539 else if (is_either(name, "fallthrough", "__fallthrough__")) 2540 suppress_fallthrough = true; 2541 else if (is_either(name, "noreturn", "__noreturn__")) 2542 $$.noreturn = true; 2543 } 2544 | T_NAME T_LPAREN T_RPAREN { 2545 $$ = (type_attributes){ .used = false }; 2546 } 2547 | T_NAME T_LPAREN argument_expression_list T_RPAREN { 2548 const char *name = $1->sb_name; 2549 if (is_either(name, "aligned", "__aligned__") 2550 && $3->args_len == 1) 2551 dcs_add_alignas($3->args[0]); 2552 $$ = (type_attributes){ .used = false }; 2553 } 2554 | type_qualifier { 2555 if (!$1.tq_const) 2556 yyerror("Bad attribute"); 2557 $$ = (type_attributes){ .used = false }; 2558 } 2559 ; 2560 2561 /* The rule 'function_body' from C23 6.9.2 is inlined into 'function_definition'. */ 2562 2563 sys: 2564 /* empty */ { 2565 $$ = in_system_header; 2566 } 2567 ; 2568 2569 %% 2570 2571 /* ARGSUSED */ 2572 int 2573 yyerror(const char *msg) 2574 { 2575 /* syntax error '%s' */ 2576 error(249, yytext); 2577 if (++sytxerr >= 5) 2578 norecover(); 2579 return 0; 2580 } 2581 2582 #if YYDEBUG && YYBYACC 2583 static const char * 2584 cgram_to_string(int tok, YYSTYPE val) 2585 { 2586 2587 switch (tok) { 2588 case T_INCDEC: 2589 return val.y_inc ? "++" : "--"; 2590 case T_MULTIPLICATIVE: 2591 case T_ADDITIVE: 2592 case T_SHIFT: 2593 case T_RELATIONAL: 2594 case T_EQUALITY: 2595 case T_OPASSIGN: 2596 return op_name(val.y_op); 2597 case T_SCLASS: 2598 return scl_name(val.y_scl); 2599 case T_TYPE: 2600 case T_STRUCT_OR_UNION: 2601 return tspec_name(val.y_tspec); 2602 case T_QUAL: 2603 return type_qualifiers_string(val.y_type_qualifiers); 2604 case T_FUNCTION_SPECIFIER: 2605 return function_specifier_name(val.y_function_specifier); 2606 case T_NAME: 2607 return val.y_name->sb_name; 2608 default: 2609 return "<none>"; 2610 } 2611 } 2612 #endif 2613 2614 static void 2615 cgram_declare(sym_t *decl, bool has_initializer, sbuf_t *renaming) 2616 { 2617 declare(decl, has_initializer, renaming); 2618 if (renaming != NULL) 2619 freeyyv(&renaming, T_NAME); 2620 } 2621 2622 /* 2623 * Discard all input tokens up to and including the next unmatched right 2624 * parenthesis. 2625 */ 2626 static void 2627 read_until_rparen(void) 2628 { 2629 int level; 2630 2631 if (yychar < 0) 2632 yychar = yylex(); 2633 freeyyv(&yylval, yychar); 2634 2635 level = 1; 2636 while (yychar > 0) { 2637 if (yychar == T_LPAREN) 2638 level++; 2639 if (yychar == T_RPAREN && --level == 0) 2640 break; 2641 freeyyv(&yylval, yychar = yylex()); 2642 } 2643 2644 yyclearin; 2645 } 2646 2647 static balanced_token_sequence 2648 read_balanced_token_sequence(void) 2649 { 2650 lint_assert(yychar < 0); 2651 balanced_token_sequence seq = lex_balanced(); 2652 yyclearin; 2653 return seq; 2654 } 2655 2656 static sym_t * 2657 symbolrename(sym_t *s, sbuf_t *sb) 2658 { 2659 if (sb != NULL) 2660 s->s_rename = sb->sb_name; 2661 return s; 2662 } 2663