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