1 /* $NetBSD: init.c,v 1.250 2023/07/30 22:27:21 rillig Exp $ */ 2 3 /* 4 * Copyright (c) 1994, 1995 Jochen Pohl 5 * Copyright (c) 2021 Roland Illig 6 * All Rights Reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by Jochen Pohl for 19 * The NetBSD Project. 20 * 4. The name of the author may not be used to endorse or promote products 21 * derived from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #if HAVE_NBTOOL_CONFIG_H 36 #include "nbtool_config.h" 37 #endif 38 39 #include <sys/cdefs.h> 40 #if defined(__RCSID) 41 __RCSID("$NetBSD: init.c,v 1.250 2023/07/30 22:27:21 rillig Exp $"); 42 #endif 43 44 #include <stdlib.h> 45 #include <string.h> 46 47 #include "lint1.h" 48 49 50 /* 51 * Initialization of global or local objects, like in: 52 * 53 * int number = 12345; 54 * int number_with_braces = { 12345 }; 55 * int array_of_unknown_size[] = { 111, 222, 333 }; 56 * struct { int x, y; } point = { .y = 4, .x = 3 }; 57 * 58 * During an initialization, the grammar parser calls these functions: 59 * 60 * begin_initialization 61 * init_lbrace for each '{' 62 * add_designator_member for each '.member' before '=' 63 * add_designator_subscript for each '[123]' before '=' 64 * init_expr for each expression 65 * init_rbrace for each '}' 66 * end_initialization 67 * 68 * Each '{' begins a new brace level, each '}' ends the current brace level. 69 * Each brace level has an associated "current object", which is the starting 70 * point for resolving the optional designations such as '.member[3]'. 71 * 72 * See also: 73 * C99 6.7.8 "Initialization" 74 * C11 6.7.9 "Initialization" 75 * d_c99_init.c for more examples 76 */ 77 78 79 typedef enum designator_kind { 80 DK_STRUCT, /* .member */ 81 DK_UNION, /* .member */ 82 DK_ARRAY, /* [subscript] */ 83 /* TODO: actually necessary? */ 84 DK_SCALAR /* no textual representation, not generated 85 * by the parser */ 86 } designator_kind; 87 88 /* 89 * A single component on the path from the "current object" of a brace level 90 * to the sub-object that is initialized by an expression. 91 * 92 * C99 6.7.8p6, 6.7.8p7 93 */ 94 typedef struct designator { 95 designator_kind dr_kind; 96 const sym_t *dr_member; /* for DK_STRUCT and DK_UNION */ 97 size_t dr_subscript; /* for DK_ARRAY */ 98 bool dr_done; 99 } designator; 100 101 /* 102 * The path from the "current object" of a brace level to the sub-object that 103 * is initialized by an expression. Examples for designations are '.member' 104 * or '.member[123].member.member[1][1]'. 105 * 106 * C99 6.7.8p6, 6.7.8p7 107 */ 108 typedef struct designation { 109 designator *dn_items; 110 size_t dn_len; 111 size_t dn_cap; 112 } designation; 113 114 /* 115 * Everything that happens between a '{' and the corresponding '}', as part 116 * of an initialization. 117 * 118 * Each brace level has a "current object". For the outermost brace level, 119 * it is the same as the object to be initialized. Each nested '{' begins a 120 * nested brace level, for the sub-object pointed to by the designator of the 121 * outer brace level. 122 * 123 * C99 6.7.8p17 124 */ 125 typedef struct brace_level { 126 /* The type of the "current object". */ 127 const type_t *bl_type; 128 129 /* 130 * The path from the "current object" to the sub-object that is 131 * initialized by the next expression. 132 * 133 * Initially, the designation is empty. Before handling an 134 * expression, the designation is updated to point to the 135 * corresponding sub-object to be initialized. After handling an 136 * expression, the designation is marked as done. It is later 137 * advanced as necessary. 138 */ 139 designation bl_designation; 140 141 struct brace_level *bl_enclosing; 142 } brace_level; 143 144 /* 145 * An ongoing initialization. 146 * 147 * In most cases there is only ever a single initialization at a time. See 148 * pointer_to_compound_literal in msg_171.c for a real-life counterexample. 149 */ 150 typedef struct initialization { 151 /* The symbol that is to be initialized. */ 152 sym_t *in_sym; 153 154 /* The innermost brace level. */ 155 brace_level *in_brace_level; 156 157 /* 158 * The maximum subscript that has ever been seen for an array of 159 * unknown size, which can only occur at the outermost brace level. 160 */ 161 size_t in_max_subscript; 162 163 /* 164 * Is set when a structural error occurred in the initialization. 165 * If set, the rest of the initialization is still parsed, but the 166 * initialization assignments are not checked. 167 */ 168 bool in_err; 169 170 struct initialization *in_enclosing; 171 } initialization; 172 173 174 static void * 175 unconst_cast(const void *p) 176 { 177 void *r; 178 179 memcpy(&r, &p, sizeof(r)); 180 return r; 181 } 182 183 static bool 184 has_automatic_storage_duration(const sym_t *sym) 185 { 186 187 return sym->s_scl == AUTO || sym->s_scl == REG; 188 } 189 190 /* 191 * Test whether rn is a string literal that can initialize ltp. 192 * 193 * See also: 194 * C99 6.7.8p14 for plain character strings 195 * C99 6.7.8p15 for wide character strings 196 */ 197 static bool 198 can_init_character_array(const type_t *ltp, const tnode_t *rn) 199 { 200 201 if (!(ltp != NULL && ltp->t_tspec == ARRAY && rn->tn_op == STRING)) 202 return false; 203 204 tspec_t lst = ltp->t_subt->t_tspec; 205 tspec_t rst = rn->tn_type->t_subt->t_tspec; 206 207 return rst == CHAR 208 ? lst == CHAR || lst == UCHAR || lst == SCHAR 209 : lst == WCHAR_TSPEC; 210 } 211 212 /* 213 * C11 6.7.9p9 seems to say that all unnamed members are skipped. C11 6.7.2.1p8 214 * suggests an exception to that rule, and together with C11 6.7.2.1p13, it 215 * says that the members from an anonymous struct/union member are "considered 216 * to be members of the containing structure or union", thereby preventing that 217 * the containing structure or union has only unnamed members. 218 */ 219 static const sym_t * 220 skip_unnamed(const sym_t *m) 221 { 222 223 while (m != NULL && m->s_name == unnamed 224 && !is_struct_or_union(m->s_type->t_tspec)) 225 m = m->s_next; 226 return m; 227 } 228 229 static const sym_t * 230 first_named_member(const type_t *tp) 231 { 232 233 lint_assert(is_struct_or_union(tp->t_tspec)); 234 return skip_unnamed(tp->t_sou->sou_first_member); 235 } 236 237 /* 238 * C99 6.7.8p22 says that the type of an array of unknown size becomes known 239 * at the end of its initializer list. 240 */ 241 static void 242 update_type_of_array_of_unknown_size(sym_t *sym, size_t size) 243 { 244 245 type_t *tp = block_dup_type(sym->s_type); 246 tp->t_dim = (int)size; 247 tp->t_incomplete_array = false; 248 sym->s_type = tp; 249 debug_step("completed array type is '%s'", type_name(sym->s_type)); 250 outsym(sym, sym->s_scl, sym->s_def); 251 } 252 253 254 /* In traditional C, bit-fields can be initialized only by integer constants. */ 255 static void 256 check_bit_field_init(const tnode_t *ln, tspec_t lt, tspec_t rt) 257 { 258 259 if (!allow_c90 && 260 is_integer(lt) && 261 ln->tn_type->t_bitfield && 262 !is_integer(rt)) { 263 /* bit-field initialization is illegal in traditional C */ 264 warning(186); 265 } 266 } 267 268 static void 269 check_non_constant_initializer(const tnode_t *tn, const sym_t *sym) 270 { 271 272 if (tn == NULL || tn->tn_op == CON) 273 return; 274 275 const sym_t *unused_sym; 276 ptrdiff_t unused_offs; 277 if (constant_addr(tn, &unused_sym, &unused_offs)) 278 return; 279 280 if (has_automatic_storage_duration(sym)) { 281 /* non-constant initializer */ 282 c99ism(177); 283 } else { 284 /* non-constant initializer */ 285 error(177); 286 } 287 } 288 289 static void 290 check_trad_no_auto_aggregate(const sym_t *sym) 291 { 292 293 if (has_automatic_storage_duration(sym) && 294 !is_scalar(sym->s_type->t_tspec)) { 295 /* no automatic aggregate initialization in traditional C */ 296 warning(188); 297 } 298 } 299 300 static void 301 check_init_expr(const type_t *ltp, sym_t *lsym, tnode_t *rn) 302 { 303 304 type_t *lutp = expr_unqualified_type(ltp); 305 306 /* Create a temporary node for the left side. */ 307 tnode_t *ln = expr_zero_alloc(sizeof(*ln), "tnode"); 308 ln->tn_op = NAME; 309 ln->tn_type = lutp; 310 ln->tn_lvalue = true; 311 ln->tn_sym = lsym; 312 313 rn = cconv(rn); 314 315 tspec_t lt = ln->tn_type->t_tspec; 316 tspec_t rt = rn->tn_type->t_tspec; 317 318 debug_step("typeok '%s', '%s'", 319 type_name(ln->tn_type), type_name(rn->tn_type)); 320 if (!typeok(INIT, 0, ln, rn)) 321 return; 322 323 /* 324 * Preserve the tree memory. This is necessary because otherwise 325 * expr() would free it. 326 */ 327 memory_pool saved_mem = expr_save_memory(); 328 expr(rn, true, false, true, false); 329 expr_restore_memory(saved_mem); 330 331 check_bit_field_init(ln, lt, rt); 332 333 /* 334 * XXX: Is it correct to do this conversion _after_ the typeok above? 335 */ 336 if (lt != rt || (ltp->t_bitfield && rn->tn_op == CON)) 337 rn = convert(INIT, 0, unconst_cast(ltp), rn); 338 339 check_non_constant_initializer(rn, lsym); 340 } 341 342 343 static const type_t * 344 designator_type(const designator *dr, const type_t *tp) 345 { 346 347 switch (tp->t_tspec) { 348 case STRUCT: 349 case UNION: 350 if (dr->dr_kind != DK_STRUCT && dr->dr_kind != DK_UNION) { 351 const sym_t *fmem = first_named_member(tp); 352 /* syntax error '%s' */ 353 error(249, "designator '[...]' is only for arrays"); 354 return fmem != NULL ? fmem->s_type : NULL; 355 } 356 357 lint_assert(dr->dr_member != NULL); 358 return dr->dr_member->s_type; 359 case ARRAY: 360 if (dr->dr_kind != DK_ARRAY) { 361 /* syntax error '%s' */ 362 error(249, 363 "designator '.member' is only for struct/union"); 364 } 365 if (!tp->t_incomplete_array) 366 lint_assert(dr->dr_subscript < (size_t)tp->t_dim); 367 return tp->t_subt; 368 default: 369 if (dr->dr_kind != DK_SCALAR) { 370 /* syntax error '%s' */ 371 error(249, "scalar type cannot use designator"); 372 } 373 return tp; 374 } 375 } 376 377 378 #ifdef DEBUG 379 static void 380 designator_debug(const designator *dr) 381 { 382 383 if (dr->dr_kind == DK_STRUCT || dr->dr_kind == DK_UNION) { 384 lint_assert(dr->dr_subscript == 0); 385 debug_printf(".%s", 386 dr->dr_member != NULL 387 ? dr->dr_member->s_name 388 : "<end>"); 389 } else if (dr->dr_kind == DK_ARRAY) { 390 lint_assert(dr->dr_member == NULL); 391 debug_printf("[%zu]", dr->dr_subscript); 392 } else { 393 lint_assert(dr->dr_member == NULL); 394 lint_assert(dr->dr_subscript == 0); 395 debug_printf("<scalar>"); 396 } 397 398 if (dr->dr_done) 399 debug_printf(" (done)"); 400 } 401 402 static void 403 designation_debug(const designation *dn) 404 { 405 406 if (dn->dn_len == 0) { 407 debug_step("designation: (empty)"); 408 return; 409 } 410 411 debug_printf("designation: "); 412 for (size_t i = 0; i < dn->dn_len; i++) 413 designator_debug(dn->dn_items + i); 414 debug_printf("\n"); 415 } 416 #else 417 #define designation_debug(dn) do { } while (false) 418 #endif 419 420 static designator * 421 designation_last(designation *dn) 422 { 423 424 lint_assert(dn->dn_len > 0); 425 return &dn->dn_items[dn->dn_len - 1]; 426 } 427 428 static void 429 designation_push(designation *dn, designator_kind kind, 430 const sym_t *member, size_t subscript) 431 { 432 433 if (dn->dn_len == dn->dn_cap) { 434 dn->dn_cap += 4; 435 dn->dn_items = xrealloc(dn->dn_items, 436 dn->dn_cap * sizeof(dn->dn_items[0])); 437 } 438 439 designator *dr = &dn->dn_items[dn->dn_len++]; 440 dr->dr_kind = kind; 441 dr->dr_member = member; 442 dr->dr_subscript = subscript; 443 dr->dr_done = false; 444 designation_debug(dn); 445 } 446 447 /* 448 * Extend the designation as appropriate for the given type. 449 * 450 * C11 6.7.9p17 451 */ 452 static bool 453 designation_descend(designation *dn, const type_t *tp) 454 { 455 456 if (is_struct_or_union(tp->t_tspec)) { 457 const sym_t *member = first_named_member(tp); 458 if (member == NULL) 459 return false; 460 designation_push(dn, 461 tp->t_tspec == STRUCT ? DK_STRUCT : DK_UNION, member, 0); 462 } else if (tp->t_tspec == ARRAY) 463 designation_push(dn, DK_ARRAY, NULL, 0); 464 else 465 designation_push(dn, DK_SCALAR, NULL, 0); 466 return true; 467 } 468 469 /* 470 * Starting at the type of the current object, resolve the type of the 471 * sub-object by following each designator in the list. 472 * 473 * C99 6.7.8p18 474 */ 475 static const type_t * 476 designation_type(const designation *dn, const type_t *tp) 477 { 478 479 for (size_t i = 0; i < dn->dn_len && tp != NULL; i++) 480 tp = designator_type(dn->dn_items + i, tp); 481 return tp; 482 } 483 484 static const type_t * 485 designation_parent_type(const designation *dn, const type_t *tp) 486 { 487 488 for (size_t i = 0; i + 1 < dn->dn_len && tp != NULL; i++) 489 tp = designator_type(dn->dn_items + i, tp); 490 return tp; 491 } 492 493 494 static brace_level * 495 brace_level_new(const type_t *tp, brace_level *enclosing) 496 { 497 498 brace_level *bl = xcalloc(1, sizeof(*bl)); 499 bl->bl_type = tp; 500 bl->bl_enclosing = enclosing; 501 502 return bl; 503 } 504 505 static void 506 brace_level_free(brace_level *bl) 507 { 508 509 free(bl->bl_designation.dn_items); 510 free(bl); 511 } 512 513 #ifdef DEBUG 514 static void 515 brace_level_debug(const brace_level *bl) 516 { 517 518 lint_assert(bl->bl_type != NULL); 519 520 debug_printf("type '%s'\n", type_name(bl->bl_type)); 521 debug_indent_inc(); 522 designation_debug(&bl->bl_designation); 523 debug_indent_dec(); 524 } 525 #else 526 #define brace_level_debug(level) do { } while (false) 527 #endif 528 529 /* Return the type of the sub-object that is currently being initialized. */ 530 static const type_t * 531 brace_level_sub_type(const brace_level *bl) 532 { 533 534 return designation_type(&bl->bl_designation, bl->bl_type); 535 } 536 537 /* 538 * After initializing a sub-object, advance the designation to point after 539 * the sub-object that has just been initialized. 540 * 541 * C99 6.7.8p17 542 * C11 6.7.9p17 543 */ 544 static void 545 brace_level_advance(brace_level *bl, size_t *max_subscript) 546 { 547 548 debug_enter(); 549 designation *dn = &bl->bl_designation; 550 const type_t *tp = designation_parent_type(dn, bl->bl_type); 551 552 if (bl->bl_designation.dn_len == 0) 553 (void)designation_descend(dn, bl->bl_type); 554 555 designator *dr = designation_last(dn); 556 /* TODO: try to switch on dr->dr_kind instead */ 557 switch (tp->t_tspec) { 558 case STRUCT: 559 lint_assert(dr->dr_member != NULL); 560 dr->dr_member = skip_unnamed(dr->dr_member->s_next); 561 if (dr->dr_member == NULL) 562 dr->dr_done = true; 563 break; 564 case UNION: 565 dr->dr_member = NULL; 566 dr->dr_done = true; 567 break; 568 case ARRAY: 569 dr->dr_subscript++; 570 if (tp->t_incomplete_array && 571 dr->dr_subscript > *max_subscript) 572 *max_subscript = dr->dr_subscript; 573 if (!tp->t_incomplete_array && 574 dr->dr_subscript >= (size_t)tp->t_dim) 575 dr->dr_done = true; 576 break; 577 default: 578 dr->dr_done = true; 579 break; 580 } 581 designation_debug(dn); 582 debug_leave(); 583 } 584 585 static void 586 warn_too_many_initializers(designator_kind kind, const type_t *tp) 587 { 588 589 if (kind == DK_STRUCT || kind == DK_UNION) { 590 /* too many struct/union initializers */ 591 error(172); 592 } else if (kind == DK_ARRAY) { 593 lint_assert(tp->t_tspec == ARRAY); 594 lint_assert(!tp->t_incomplete_array); 595 /* too many array initializers, expected %d */ 596 error(173, tp->t_dim); 597 } else { 598 /* too many initializers */ 599 error(174); 600 } 601 602 } 603 604 static bool 605 brace_level_pop_done(brace_level *bl, size_t *max_subscript) 606 { 607 designation *dn = &bl->bl_designation; 608 designator_kind dr_kind = designation_last(dn)->dr_kind; 609 const type_t *sub_type = designation_parent_type(dn, bl->bl_type); 610 611 while (designation_last(dn)->dr_done) { 612 dn->dn_len--; 613 designation_debug(dn); 614 if (dn->dn_len == 0) { 615 warn_too_many_initializers(dr_kind, sub_type); 616 return false; 617 } 618 brace_level_advance(bl, max_subscript); 619 } 620 return true; 621 } 622 623 static void 624 brace_level_pop_final(brace_level *bl, size_t *max_subscript) 625 { 626 designation *dn = &bl->bl_designation; 627 628 while (dn->dn_len > 0 && designation_last(dn)->dr_done) { 629 dn->dn_len--; 630 designation_debug(dn); 631 if (dn->dn_len == 0) 632 return; 633 brace_level_advance(bl, max_subscript); 634 } 635 } 636 637 /* 638 * Make the designation point to the sub-object to be initialized next. 639 * Initially or after a previous expression, the designation is not advanced 640 * yet since the place to stop depends on the next expression, especially for 641 * string literals. 642 */ 643 static bool 644 brace_level_goto(brace_level *bl, const tnode_t *rn, size_t *max_subscript) 645 { 646 647 designation *dn = &bl->bl_designation; 648 if (dn->dn_len == 0 && can_init_character_array(bl->bl_type, rn)) 649 return true; 650 if (dn->dn_len == 0 && !designation_descend(dn, bl->bl_type)) 651 return false; 652 653 again: 654 if (!brace_level_pop_done(bl, max_subscript)) 655 return false; 656 657 const type_t *ltp = brace_level_sub_type(bl); 658 if (types_compatible(ltp, rn->tn_type, true, false, NULL)) 659 return true; 660 661 if (is_struct_or_union(ltp->t_tspec) || ltp->t_tspec == ARRAY) { 662 if (can_init_character_array(ltp, rn)) 663 return true; 664 if (!designation_descend(dn, ltp)) 665 return false; 666 goto again; 667 } 668 669 return true; 670 } 671 672 673 static initialization * 674 initialization_new(sym_t *sym, initialization *enclosing) 675 { 676 677 initialization *in = xcalloc(1, sizeof(*in)); 678 in->in_sym = sym; 679 in->in_enclosing = enclosing; 680 681 return in; 682 } 683 684 static void 685 initialization_free(initialization *in) 686 { 687 brace_level *bl, *next; 688 689 /* TODO: lint_assert(in->in_brace_level == NULL) */ 690 for (bl = in->in_brace_level; bl != NULL; bl = next) { 691 next = bl->bl_enclosing; 692 brace_level_free(bl); 693 } 694 695 free(in); 696 } 697 698 #ifdef DEBUG 699 static void 700 initialization_debug(const initialization *in) 701 { 702 703 if (in->in_err) 704 debug_step("initialization error"); 705 if (in->in_brace_level == NULL) { 706 debug_step("no brace level"); 707 return; 708 } 709 710 const brace_level *bl; 711 size_t i = 0; 712 for (bl = in->in_brace_level; bl != NULL; bl = bl->bl_enclosing) { 713 debug_printf("brace level %zu: ", i); 714 brace_level_debug(bl); 715 i++; 716 } 717 } 718 #else 719 #define initialization_debug(in) do { } while (false) 720 #endif 721 722 /* 723 * Return the type of the object or sub-object that is currently being 724 * initialized. 725 */ 726 static const type_t * 727 initialization_sub_type(initialization *in) 728 { 729 730 if (in->in_brace_level == NULL) 731 return in->in_sym->s_type; 732 733 const type_t *tp = brace_level_sub_type(in->in_brace_level); 734 if (tp == NULL) 735 in->in_err = true; 736 return tp; 737 } 738 739 static void 740 initialization_lbrace(initialization *in) 741 { 742 743 if (in->in_err) 744 return; 745 746 debug_enter(); 747 748 const type_t *tp = initialization_sub_type(in); 749 if (tp == NULL) 750 goto done; 751 752 brace_level *outer_bl = in->in_brace_level; 753 if (!allow_c90 && outer_bl == NULL) 754 check_trad_no_auto_aggregate(in->in_sym); 755 756 if (!allow_c90 && tp->t_tspec == UNION) { 757 /* initialization of union is illegal in traditional C */ 758 warning(238); 759 } 760 761 if (is_struct_or_union(tp->t_tspec) && tp->t_sou->sou_incomplete) { 762 /* initialization of incomplete type '%s' */ 763 error(175, type_name(tp)); 764 in->in_err = true; 765 goto done; 766 } 767 768 if (outer_bl != NULL && outer_bl->bl_designation.dn_len == 0) { 769 designation *dn = &outer_bl->bl_designation; 770 (void)designation_descend(dn, outer_bl->bl_type); 771 tp = designation_type(dn, outer_bl->bl_type); 772 } 773 774 in->in_brace_level = brace_level_new(tp, outer_bl); 775 if (is_struct_or_union(tp->t_tspec) && 776 first_named_member(tp) == NULL) { 777 /* cannot initialize struct/union with no named member */ 778 error(179); 779 in->in_err = true; 780 } 781 782 done: 783 initialization_debug(in); 784 debug_leave(); 785 } 786 787 static void 788 initialization_rbrace(initialization *in) 789 { 790 791 debug_enter(); 792 793 if (in->in_brace_level != NULL) 794 brace_level_pop_final(in->in_brace_level, 795 &in->in_max_subscript); 796 797 /* C99 6.7.8p22 */ 798 if (in->in_sym->s_type->t_incomplete_array && 799 in->in_brace_level->bl_enclosing == NULL) { 800 801 /* prevent "empty array declaration for '%s' [190]" */ 802 size_t dim = in->in_max_subscript; 803 if (dim == 0 && in->in_err) 804 dim = 1; 805 806 update_type_of_array_of_unknown_size(in->in_sym, dim); 807 } 808 809 if (in->in_err) 810 goto done; 811 812 brace_level *inner_bl = in->in_brace_level; 813 brace_level *outer_bl = inner_bl->bl_enclosing; 814 in->in_brace_level = outer_bl; 815 brace_level_free(inner_bl); 816 817 if (outer_bl != NULL) 818 brace_level_advance(outer_bl, &in->in_max_subscript); 819 820 done: 821 initialization_debug(in); 822 debug_leave(); 823 } 824 825 static void 826 initialization_add_designator_member(initialization *in, const char *name) 827 { 828 829 if (in->in_err) 830 return; 831 832 brace_level *bl = in->in_brace_level; 833 lint_assert(bl != NULL); 834 835 const type_t *tp = brace_level_sub_type(bl); 836 if (is_struct_or_union(tp->t_tspec)) 837 goto proceed; 838 else if (tp->t_tspec == ARRAY) 839 /* syntax error '%s' */ 840 error(249, "designator '.member' is only for struct/union"); 841 else 842 /* syntax error '%s' */ 843 error(249, "scalar type cannot use designator"); 844 in->in_err = true; 845 return; 846 847 proceed:; 848 const sym_t *member = find_member(tp->t_sou, name); 849 if (member == NULL) { 850 /* type '%s' does not have member '%s' */ 851 error(101, type_name(tp), name); 852 in->in_err = true; 853 return; 854 } 855 856 designation_push(&bl->bl_designation, 857 tp->t_tspec == STRUCT ? DK_STRUCT : DK_UNION, member, 0); 858 } 859 860 static void 861 initialization_add_designator_subscript(initialization *in, size_t subscript) 862 { 863 864 if (in->in_err) 865 return; 866 867 brace_level *bl = in->in_brace_level; 868 lint_assert(bl != NULL); 869 870 const type_t *tp = brace_level_sub_type(bl); 871 if (tp->t_tspec != ARRAY) { 872 /* syntax error '%s' */ 873 error(249, "designator '[...]' is only for arrays"); 874 in->in_err = true; 875 return; 876 } 877 878 if (!tp->t_incomplete_array && subscript >= (size_t)tp->t_dim) { 879 /* array subscript cannot be > %d: %ld */ 880 error(168, tp->t_dim - 1, (long)subscript); 881 subscript = 0; /* suppress further errors */ 882 } 883 884 if (tp->t_incomplete_array && subscript > in->in_max_subscript) 885 in->in_max_subscript = subscript; 886 887 designation_push(&bl->bl_designation, DK_ARRAY, NULL, subscript); 888 } 889 890 /* 891 * Initialize an object with automatic storage duration that has an 892 * initializer expression without braces. 893 */ 894 static bool 895 initialization_expr_using_op(initialization *in, tnode_t *rn) 896 { 897 898 if (!has_automatic_storage_duration(in->in_sym)) 899 return false; 900 if (in->in_brace_level != NULL) 901 return false; 902 if (in->in_sym->s_type->t_tspec == ARRAY) 903 return false; 904 905 debug_step("handing over to INIT"); 906 907 tnode_t *ln = build_name(in->in_sym, false); 908 ln->tn_type = expr_unqualified_type(ln->tn_type); 909 910 tnode_t *tn = build_binary(ln, INIT, false /* XXX */, rn); 911 expr(tn, false, false, false, false); 912 913 return true; 914 } 915 916 /* Initialize a character array or wchar_t array with a string literal. */ 917 static bool 918 initialization_init_array_from_string(initialization *in, tnode_t *tn) 919 { 920 921 if (tn->tn_op != STRING) 922 return false; 923 924 const type_t *tp = initialization_sub_type(in); 925 926 if (!can_init_character_array(tp, tn)) 927 return false; 928 929 size_t len = tn->tn_string->st_len; 930 if (!tp->t_incomplete_array && (size_t)tp->t_dim < len) { 931 /* string literal too long (%lu) for target array (%lu) */ 932 warning(187, (unsigned long)len, (unsigned long)tp->t_dim); 933 } 934 935 brace_level *bl = in->in_brace_level; 936 if (bl != NULL && bl->bl_designation.dn_len == 0) 937 (void)designation_descend(&bl->bl_designation, bl->bl_type); 938 if (bl != NULL) 939 brace_level_advance(bl, &in->in_max_subscript); 940 941 if (tp->t_incomplete_array) 942 update_type_of_array_of_unknown_size(in->in_sym, len + 1); 943 944 return true; 945 } 946 947 /* 948 * Initialize a single sub-object as part of the currently ongoing 949 * initialization. 950 */ 951 static void 952 initialization_expr(initialization *in, tnode_t *tn) 953 { 954 955 if (in->in_err || tn == NULL) 956 return; 957 958 debug_enter(); 959 960 brace_level *bl = in->in_brace_level; 961 if (bl != NULL && !brace_level_goto(bl, tn, &in->in_max_subscript)) { 962 in->in_err = true; 963 goto done; 964 } 965 if (initialization_expr_using_op(in, tn)) 966 goto done; 967 if (initialization_init_array_from_string(in, tn)) 968 goto done; 969 if (in->in_err) 970 goto done; 971 972 const type_t *tp = initialization_sub_type(in); 973 if (tp == NULL) 974 goto done; 975 976 if (bl == NULL && !is_scalar(tp->t_tspec)) { 977 /* {}-enclosed or constant initializer of type '%s' required */ 978 error(181, type_name(in->in_sym->s_type)); 979 goto done; 980 } 981 982 debug_step("expecting '%s', expression has '%s'", 983 type_name(tp), type_name(tn->tn_type)); 984 check_init_expr(tp, in->in_sym, tn); 985 if (bl != NULL) 986 brace_level_advance(bl, &in->in_max_subscript); 987 988 done: 989 initialization_debug(in); 990 debug_leave(); 991 } 992 993 994 static initialization *init; 995 996 997 sym_t * 998 current_initsym(void) 999 { 1000 1001 return init->in_sym; 1002 } 1003 1004 void 1005 begin_initialization(sym_t *sym) 1006 { 1007 1008 debug_step("begin initialization of '%s'", type_name(sym->s_type)); 1009 debug_indent_inc(); 1010 1011 init = initialization_new(sym, init); 1012 } 1013 1014 void 1015 end_initialization(void) 1016 { 1017 1018 initialization *in = init; 1019 init = in->in_enclosing; 1020 initialization_free(in); 1021 1022 debug_indent_dec(); 1023 debug_step("end initialization"); 1024 } 1025 1026 void 1027 begin_designation(void) 1028 { 1029 1030 initialization *in = init; 1031 if (in->in_err) 1032 return; 1033 1034 brace_level *bl = in->in_brace_level; 1035 lint_assert(bl != NULL); 1036 bl->bl_designation.dn_len = 0; 1037 designation_debug(&bl->bl_designation); 1038 } 1039 1040 void 1041 add_designator_member(sbuf_t *sb) 1042 { 1043 1044 initialization_add_designator_member(init, sb->sb_name); 1045 } 1046 1047 void 1048 add_designator_subscript(range_t range) 1049 { 1050 1051 initialization_add_designator_subscript(init, range.hi); 1052 } 1053 1054 void 1055 init_lbrace(void) 1056 { 1057 1058 initialization_lbrace(init); 1059 } 1060 1061 void 1062 init_expr(tnode_t *tn) 1063 { 1064 1065 initialization_expr(init, tn); 1066 } 1067 1068 void 1069 init_rbrace(void) 1070 { 1071 1072 initialization_rbrace(init); 1073 } 1074