1 /* $NetBSD: init.c,v 1.194 2021/04/09 23:03:26 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) && !defined(lint) 41 __RCSID("$NetBSD: init.c,v 1.194 2021/04/09 23:03:26 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 * 56 * int array_of_unknown_size[] = { 111, 222, 333 }; 57 * int array_flat[2][2] = { 11, 12, 21, 22 }; 58 * int array_nested[2][2] = { { 11, 12 }, { 21, 22 } }; 59 * 60 * struct { int x, y; } point = { 3, 4 }; 61 * struct { int x, y; } point = { .y = 4, .x = 3 }; 62 * 63 * Any scalar expression in the initializer may be surrounded by arbitrarily 64 * many extra pairs of braces, like in the example 'number_with_braces' (C99 65 * 6.7.8p11). 66 * 67 * For multi-dimensional arrays, the inner braces may be omitted like in 68 * array_flat or spelled out like in array_nested. This is unusual in 69 * practice and therefore only supported very basically. 70 * 71 * During initialization, the grammar parser calls these functions: 72 * 73 * begin_initialization 74 * init_lbrace for each '{' 75 * add_designator_member for each '.member' before '=' 76 * add_designator_subscript for each '[123]' before '=' 77 * init_expr for each expression 78 * init_rbrace for each '}' 79 * end_initialization 80 * 81 * Each '{' begins a new brace level, each '}' ends the current brace level. 82 * Each brace level has an associated "current object". 83 * 84 * See also: 85 * C99 6.7.8 "Initialization" 86 * d_c99_init.c for more examples 87 */ 88 89 /* 90 * A single component on the path to the sub-object that is initialized by an 91 * initializer expression. Either a struct or union member, or an array 92 * subscript. 93 * 94 * C99 6.7.8p6, 6.7.8p7 95 */ 96 struct designator { 97 const char *dr_name; /* for struct and union */ 98 size_t dr_subscript; /* for array */ 99 struct designator *dr_next; 100 }; 101 102 /* 103 * The optional designation for an initializer, saying which sub-object to 104 * initialize. Examples for designations are '.member' or 105 * '.member[123].member.member[1][1]'. 106 * 107 * C99 6.7.8p6, 6.7.8p7 108 */ 109 struct designation { 110 struct designator *dn_head; 111 struct designator *dn_tail; 112 }; 113 114 /* 115 * Describes a single brace level of an ongoing initialization. 116 * 117 * See C99 6.7.8p17. 118 */ 119 struct brace_level { 120 const type_t *bl_type; /* The type of the current object that 121 * is initialized at this brace 122 * level. */ 123 124 struct designation bl_designation; /* .member[123].member */ 125 126 const sym_t *bl_member; /* for structs and unions */ 127 size_t bl_subscript; /* for arrays */ 128 bool bl_scalar_done: 1; /* for scalars */ 129 bool bl_confused: 1; /* skip further checks */ 130 131 struct brace_level *bl_enclosing; 132 }; 133 134 struct initialization { 135 /* The symbol that is to be initialized. */ 136 sym_t *in_sym; 137 138 /* The innermost brace level. */ 139 struct brace_level *in_brace_level; 140 141 /* 142 * Is set as soon as a fatal error occurred in the initialization. 143 * The effect is that the rest of the initialization is ignored 144 * (parsed by yacc, expression trees built, but no initialization 145 * takes place). 146 */ 147 bool in_err; 148 149 struct initialization *in_enclosing; 150 }; 151 152 153 #ifdef DEBUG 154 static int debug_indentation = 0; 155 #endif 156 157 158 #ifdef DEBUG 159 160 static void __printflike(1, 2) 161 debug_printf(const char *fmt, ...) 162 { 163 va_list va; 164 165 va_start(va, fmt); 166 vfprintf(stdout, fmt, va); 167 va_end(va); 168 } 169 170 static void 171 debug_indent(void) 172 { 173 174 debug_printf("%*s", 2 * debug_indentation, ""); 175 } 176 177 static void 178 debug_enter(const char *func) 179 { 180 181 printf("%*s+ %s\n", 2 * debug_indentation++, "", func); 182 } 183 184 static void __printflike(1, 2) 185 debug_step(const char *fmt, ...) 186 { 187 va_list va; 188 189 debug_indent(); 190 va_start(va, fmt); 191 vfprintf(stdout, fmt, va); 192 va_end(va); 193 printf("\n"); 194 } 195 #define debug_step0 debug_step 196 #define debug_step1 debug_step 197 #define debug_step2 debug_step 198 199 static void 200 debug_leave(const char *func) 201 { 202 203 printf("%*s- %s\n", 2 * --debug_indentation, "", func); 204 } 205 206 #define debug_enter() (debug_enter)(__func__) 207 #define debug_leave() (debug_leave)(__func__) 208 209 #else 210 211 #define debug_indent() do { } while (false) 212 #define debug_enter() do { } while (false) 213 #define debug_step0(msg) do { } while (false) 214 #define debug_step1(fmt, arg0) do { } while (false) 215 #define debug_step2(fmt, arg1, arg2) do { } while (false) 216 #define debug_leave() do { } while (false) 217 218 #endif 219 220 221 static void * 222 unconst_cast(const void *p) 223 { 224 void *r; 225 226 memcpy(&r, &p, sizeof(r)); 227 return r; 228 } 229 230 /* C99 6.7.8p7 */ 231 static bool 232 is_struct_or_union(tspec_t t) 233 { 234 235 return t == STRUCT || t == UNION; 236 } 237 238 static bool 239 has_automatic_storage_duration(const sym_t *sym) 240 { 241 242 return sym->s_scl == AUTO || sym->s_scl == REG; 243 } 244 245 /* C99 6.7.8p14, 6.7.8p15 */ 246 static bool 247 is_string_array(const type_t *tp, tspec_t t) 248 { 249 tspec_t st; 250 251 if (tp == NULL || tp->t_tspec != ARRAY) 252 return false; 253 254 st = tp->t_subt->t_tspec; 255 return t == CHAR 256 ? st == CHAR || st == UCHAR || st == SCHAR 257 : st == WCHAR; 258 } 259 260 /* C99 6.7.8p9 */ 261 static bool 262 is_unnamed(const sym_t *m) 263 { 264 265 return m->s_bitfield && m->s_name == unnamed; 266 } 267 268 /* C99 6.7.8p9 */ 269 static const sym_t * 270 skip_unnamed(const sym_t *m) 271 { 272 273 while (m != NULL && is_unnamed(m)) 274 m = m->s_next; 275 return m; 276 } 277 278 static const sym_t * 279 first_named_member(const type_t *tp) 280 { 281 282 lint_assert(is_struct_or_union(tp->t_tspec)); 283 return skip_unnamed(tp->t_str->sou_first_member); 284 } 285 286 static const sym_t * 287 look_up_member(const type_t *tp, const char *name) 288 { 289 const sym_t *m; 290 291 lint_assert(is_struct_or_union(tp->t_tspec)); 292 for (m = tp->t_str->sou_first_member; m != NULL; m = m->s_next) 293 if (!is_unnamed(m) && strcmp(m->s_name, name) == 0) 294 return m; 295 return NULL; 296 } 297 298 static const type_t * 299 sym_type(const sym_t *sym) 300 { 301 302 return sym != NULL ? sym->s_type : NULL; 303 } 304 305 static const type_t * 306 look_up_member_type(const type_t *tp, const char *name) 307 { 308 const sym_t *member; 309 310 member = look_up_member(tp, name); 311 if (member == NULL) { 312 /* type '%s' does not have member '%s' */ 313 error(101, type_name(tp), name); 314 } 315 316 return sym_type(member); 317 } 318 319 static void 320 update_type_of_array_of_unknown_size(sym_t *sym, size_t size) 321 { 322 type_t *tp; 323 324 tp = dup_type(sym->s_type); 325 tp->t_dim = (int)size; 326 tp->t_incomplete_array = false; 327 sym->s_type = tp; 328 } 329 330 331 /* In traditional C, bit-fields can be initialized only by integer constants. */ 332 static void 333 check_bit_field_init(const tnode_t *ln, tspec_t lt, tspec_t rt) 334 { 335 336 if (tflag && 337 is_integer(lt) && 338 ln->tn_type->t_bitfield && 339 !is_integer(rt)) { 340 /* bit-field initialization is illegal in traditional C */ 341 warning(186); 342 } 343 } 344 345 static void 346 check_non_constant_initializer(const tnode_t *tn, const sym_t *sym) 347 { 348 const sym_t *unused_sym; 349 ptrdiff_t unused_offs; 350 351 if (tn == NULL || tn->tn_op == CON) 352 return; 353 354 if (constant_addr(tn, &unused_sym, &unused_offs)) 355 return; 356 357 if (has_automatic_storage_duration(sym)) { 358 /* non-constant initializer */ 359 c99ism(177); 360 } else { 361 /* non-constant initializer */ 362 error(177); 363 } 364 } 365 366 static void 367 check_no_auto_aggregate(const sym_t *sym) 368 { 369 370 if (tflag && 371 has_automatic_storage_duration(sym) && 372 !is_scalar(sym->s_type->t_tspec)) { 373 /* no automatic aggregate initialization in trad. C */ 374 warning(188); 375 } 376 } 377 378 static void 379 check_init_expr(const type_t *tp, sym_t *sym, tnode_t *tn) 380 { 381 tnode_t *ln; 382 tspec_t lt, rt; 383 struct memory_block *tmem; 384 385 /* Create a temporary node for the left side. */ 386 ln = expr_zalloc(sizeof(*ln)); 387 ln->tn_op = NAME; 388 ln->tn_type = expr_dup_type(tp); 389 ln->tn_type->t_const = false; 390 ln->tn_lvalue = true; 391 ln->tn_sym = sym; 392 393 tn = cconv(tn); 394 395 lt = ln->tn_type->t_tspec; 396 rt = tn->tn_type->t_tspec; 397 398 debug_step2("typeok '%s', '%s'", 399 type_name(ln->tn_type), type_name(tn->tn_type)); 400 if (!typeok(INIT, 0, ln, tn)) 401 return; 402 403 /* 404 * Preserve the tree memory. This is necessary because otherwise 405 * expr() would free it. 406 */ 407 tmem = expr_save_memory(); 408 expr(tn, true, false, true, false); 409 expr_restore_memory(tmem); 410 411 check_bit_field_init(ln, lt, rt); 412 413 /* 414 * XXX: Is it correct to do this conversion _after_ the typeok above? 415 */ 416 if (lt != rt || (tp->t_bitfield && tn->tn_op == CON)) 417 tn = convert(INIT, 0, unconst_cast(tp), tn); 418 419 check_non_constant_initializer(tn, sym); 420 } 421 422 423 static struct designator * 424 designator_new(const char *name, size_t subscript) 425 { 426 struct designator *dr; 427 428 dr = xcalloc(1, sizeof(*dr)); 429 dr->dr_name = name; 430 dr->dr_subscript = subscript; 431 return dr; 432 } 433 434 static void 435 designator_free(struct designator *dr) 436 { 437 438 free(dr); 439 } 440 441 442 static const type_t * 443 designator_look_up(const struct designator *dr, const type_t *tp) 444 { 445 switch (tp->t_tspec) { 446 case STRUCT: 447 case UNION: 448 if (dr->dr_name == NULL) { 449 /* syntax error '%s' */ 450 error(249, "designator '[...]' is only for arrays"); 451 return sym_type(first_named_member(tp)); 452 } 453 454 return look_up_member_type(tp, dr->dr_name); 455 case ARRAY: 456 if (dr->dr_name != NULL) { 457 /* syntax error '%s' */ 458 error(249, 459 "designator '.member' is only for struct/union"); 460 } 461 if (!tp->t_incomplete_array && 462 dr->dr_subscript >= (size_t)tp->t_dim) { 463 /* array subscript cannot be > %d: %ld */ 464 error(168, tp->t_dim - 1, (long)dr->dr_subscript); 465 } 466 return tp->t_subt; 467 default: 468 /* syntax error '%s' */ 469 error(249, "scalar type cannot use designator"); 470 return tp; 471 } 472 } 473 474 475 #ifdef DEBUG 476 static void 477 designation_debug(const struct designation *dn) 478 { 479 const struct designator *dr; 480 481 if (dn->dn_head == NULL) 482 return; 483 484 debug_indent(); 485 debug_printf("designation: "); 486 for (dr = dn->dn_head; dr != NULL; dr = dr->dr_next) { 487 if (dr->dr_name != NULL) { 488 debug_printf(".%s", dr->dr_name); 489 lint_assert(dr->dr_subscript == 0); 490 } else 491 debug_printf("[%zu]", dr->dr_subscript); 492 } 493 debug_printf("\n"); 494 } 495 #else 496 #define designation_debug(dn) do { } while (false) 497 #endif 498 499 static void 500 designation_add(struct designation *dn, const char *name, size_t subscript) 501 { 502 struct designator *dr; 503 504 dr = designator_new(name, subscript); 505 506 if (dn->dn_head != NULL) { 507 dn->dn_tail->dr_next = dr; 508 dn->dn_tail = dr; 509 } else { 510 dn->dn_head = dr; 511 dn->dn_tail = dr; 512 } 513 } 514 515 /* 516 * Starting at the type of the current object, resolve the type of the 517 * sub-object by following each designator in the list. 518 */ 519 static const type_t * 520 designation_look_up(const struct designation *dn, const type_t *tp) 521 { 522 const struct designator *dr; 523 524 for (dr = dn->dn_head; dr != NULL && tp != NULL; dr = dr->dr_next) 525 tp = designator_look_up(dr, tp); 526 return tp; 527 } 528 529 static void 530 designation_reset(struct designation *dn) 531 { 532 struct designator *dr, *next; 533 534 for (dr = dn->dn_head; dr != NULL; dr = next) { 535 next = dr->dr_next; 536 designator_free(dr); 537 } 538 539 dn->dn_head = NULL; 540 dn->dn_tail = NULL; 541 } 542 543 544 static struct brace_level * 545 brace_level_new(const type_t *tp, struct brace_level *enclosing) 546 { 547 struct brace_level *bl; 548 549 bl = xcalloc(1, sizeof(*bl)); 550 bl->bl_type = tp; 551 bl->bl_enclosing = enclosing; 552 if (is_struct_or_union(tp->t_tspec)) 553 bl->bl_member = first_named_member(tp); 554 555 return bl; 556 } 557 558 static void 559 brace_level_free(struct brace_level *bl) 560 { 561 562 designation_reset(&bl->bl_designation); 563 free(bl); 564 } 565 566 #ifdef DEBUG 567 static void 568 brace_level_debug(const struct brace_level *bl) 569 { 570 571 lint_assert(bl->bl_type != NULL); 572 lint_assert(bl->bl_member == NULL || !is_unnamed(bl->bl_member)); 573 574 debug_printf("type '%s'", type_name(bl->bl_type)); 575 576 if (is_struct_or_union(bl->bl_type->t_tspec) && bl->bl_member != NULL) 577 debug_printf(", member '%s'", bl->bl_member->s_name); 578 if (bl->bl_type->t_tspec == ARRAY) 579 debug_printf(", subscript %zu", bl->bl_subscript); 580 581 debug_printf("\n"); 582 } 583 #else 584 #define brace_level_debug(level) do { } while (false) 585 #endif 586 587 static const type_t * 588 brace_level_sub_type_struct_or_union(const struct brace_level *bl) 589 { 590 591 if (bl->bl_member == NULL) { 592 /* too many struct/union initializers */ 593 error(172); 594 return NULL; 595 } 596 597 lint_assert(!is_unnamed(bl->bl_member)); 598 return sym_type(bl->bl_member); 599 } 600 601 static const type_t * 602 brace_level_sub_type_array(const struct brace_level *bl, bool is_string) 603 { 604 605 if (!bl->bl_confused && !bl->bl_type->t_incomplete_array && 606 bl->bl_subscript >= (size_t)bl->bl_type->t_dim) { 607 /* too many array initializers, expected %d */ 608 error(173, bl->bl_type->t_dim); 609 } 610 611 if (is_string && bl->bl_subscript == 0 && 612 bl->bl_type->t_subt->t_tspec != ARRAY) 613 return bl->bl_type; 614 615 return bl->bl_type->t_subt; 616 } 617 618 static const type_t * 619 brace_level_sub_type_scalar(const struct brace_level *bl) 620 { 621 622 if (bl->bl_scalar_done) { 623 /* too many initializers */ 624 error(174); 625 } 626 627 return bl->bl_type; 628 } 629 630 /* Return the type of the sub-object that is currently being initialized. */ 631 static const type_t * 632 brace_level_sub_type(const struct brace_level *bl, bool is_string) 633 { 634 635 if (bl->bl_designation.dn_head != NULL) 636 return designation_look_up(&bl->bl_designation, bl->bl_type); 637 638 switch (bl->bl_type->t_tspec) { 639 case STRUCT: 640 case UNION: 641 return brace_level_sub_type_struct_or_union(bl); 642 case ARRAY: 643 return brace_level_sub_type_array(bl, is_string); 644 default: 645 return brace_level_sub_type_scalar(bl); 646 } 647 } 648 649 /* C99 6.7.8p17 */ 650 static void 651 brace_level_apply_designation(struct brace_level *bl) 652 { 653 const struct designator *dr = bl->bl_designation.dn_head; 654 655 if (dr == NULL) 656 return; 657 658 designation_debug(&bl->bl_designation); 659 660 switch (bl->bl_type->t_tspec) { 661 case STRUCT: 662 case UNION: 663 if (dr->dr_name == NULL) 664 return; /* error, silently ignored */ 665 bl->bl_member = look_up_member(bl->bl_type, dr->dr_name); 666 break; 667 case ARRAY: 668 if (dr->dr_name != NULL) 669 return; /* error, silently ignored */ 670 bl->bl_subscript = dr->dr_subscript; 671 break; 672 default: 673 break; /* error, silently ignored */ 674 } 675 } 676 677 /* 678 * After initializing a sub-object, advance to the next sub-object. 679 * 680 * C99 6.7.8p17 681 */ 682 static void 683 brace_level_advance(struct brace_level *bl) 684 { 685 686 switch (bl->bl_type->t_tspec) { 687 case STRUCT: 688 lint_assert(bl->bl_member != NULL); 689 bl->bl_member = skip_unnamed(bl->bl_member->s_next); 690 break; 691 case UNION: 692 bl->bl_member = NULL; 693 break; 694 case ARRAY: 695 bl->bl_subscript++; 696 break; 697 default: 698 bl->bl_scalar_done = true; 699 break; 700 } 701 } 702 703 704 static struct initialization * 705 initialization_new(sym_t *sym) 706 { 707 struct initialization *in; 708 709 in = xcalloc(1, sizeof(*in)); 710 in->in_sym = sym; 711 712 return in; 713 } 714 715 static void 716 initialization_free(struct initialization *in) 717 { 718 struct brace_level *bl, *next; 719 720 for (bl = in->in_brace_level; bl != NULL; bl = next) { 721 next = bl->bl_enclosing; 722 brace_level_free(bl); 723 } 724 725 free(in); 726 } 727 728 #ifdef DEBUG 729 static void 730 initialization_debug(const struct initialization *in) 731 { 732 size_t i; 733 const struct brace_level *bl; 734 735 if (in->in_brace_level == NULL) { 736 debug_step("no brace level"); 737 return; 738 } 739 740 i = 0; 741 for (bl = in->in_brace_level; bl != NULL; bl = bl->bl_enclosing) { 742 debug_indent(); 743 debug_printf("brace level %zu: ", i); 744 brace_level_debug(bl); 745 i++; 746 } 747 } 748 #else 749 #define initialization_debug(in) do { } while (false) 750 #endif 751 752 /* 753 * Return the type of the object or sub-object that is currently being 754 * initialized. 755 */ 756 static const type_t * 757 initialization_sub_type(struct initialization *in, bool is_string) 758 { 759 const type_t *tp; 760 761 tp = in->in_brace_level != NULL 762 ? brace_level_sub_type(in->in_brace_level, is_string) 763 : in->in_sym->s_type; 764 if (tp == NULL) 765 in->in_err = true; 766 return tp; 767 } 768 769 static void 770 initialization_begin_brace_level(struct initialization *in) 771 { 772 const type_t *tp; 773 774 if (in->in_err) 775 return; 776 777 debug_enter(); 778 779 tp = initialization_sub_type(in, false); 780 if (tp == NULL) { 781 in->in_err = true; 782 goto done; 783 } 784 785 if (tflag && in->in_brace_level == NULL) 786 check_no_auto_aggregate(in->in_sym); 787 788 if (tflag && tp->t_tspec == UNION) { 789 /* initialization of union is illegal in traditional C */ 790 warning(238); 791 } 792 793 if (tp->t_tspec == STRUCT && tp->t_str->sou_incomplete) { 794 /* initialization of incomplete type '%s' */ 795 error(175, type_name(tp)); 796 in->in_err = true; 797 goto done; 798 } 799 800 if (in->in_brace_level != NULL) 801 brace_level_apply_designation(in->in_brace_level); 802 803 in->in_brace_level = brace_level_new(tp, in->in_brace_level); 804 805 done: 806 initialization_debug(in); 807 debug_leave(); 808 } 809 810 /* C99 6.7.8p22 */ 811 static void 812 initialization_set_size_of_unknown_array(struct initialization *in) 813 { 814 815 if (in->in_sym->s_type->t_incomplete_array && 816 in->in_brace_level->bl_enclosing == NULL) 817 update_type_of_array_of_unknown_size(in->in_sym, 818 in->in_brace_level->bl_subscript); 819 } 820 821 static void 822 initialization_end_brace_level(struct initialization *in) 823 { 824 struct brace_level *bl; 825 826 if (in->in_err) 827 return; 828 829 debug_enter(); 830 831 initialization_set_size_of_unknown_array(in); 832 833 bl = in->in_brace_level; 834 in->in_brace_level = bl->bl_enclosing; 835 brace_level_free(bl); 836 bl = in->in_brace_level; 837 838 if (bl != NULL) 839 brace_level_advance(bl); 840 if (bl != NULL) 841 designation_reset(&bl->bl_designation); 842 843 initialization_debug(in); 844 debug_leave(); 845 } 846 847 static void 848 initialization_add_designator(struct initialization *in, 849 const char *name, size_t subscript) 850 { 851 852 if (in->in_err) 853 return; 854 855 lint_assert(in->in_brace_level != NULL); 856 designation_add(&in->in_brace_level->bl_designation, name, subscript); 857 } 858 859 /* 860 * An object with automatic storage duration that has a single initializer 861 * expression without braces and is not an array is initialized by delegating 862 * to the ASSIGN operator. 863 */ 864 static bool 865 initialization_expr_using_assign(struct initialization *in, tnode_t *rn) 866 { 867 tnode_t *ln, *tn; 868 869 if (!has_automatic_storage_duration(in->in_sym)) 870 return false; 871 if (in->in_brace_level != NULL) 872 return false; 873 if (in->in_sym->s_type->t_tspec == ARRAY) 874 return false; 875 876 debug_step0("handing over to ASSIGN"); 877 878 ln = new_name_node(in->in_sym, 0); 879 ln->tn_type = expr_dup_type(ln->tn_type); 880 ln->tn_type->t_const = false; 881 882 tn = build(ASSIGN, ln, rn); 883 expr(tn, false, false, false, false); 884 885 return true; 886 } 887 888 /* Initialize a character array or wchar_t array with a string literal. */ 889 static bool 890 initialization_init_array_using_string(struct initialization *in, tnode_t *tn) 891 { 892 struct brace_level *bl; 893 const type_t *tp; 894 strg_t *strg; 895 896 if (tn->tn_op != STRING) 897 return false; 898 899 bl = in->in_brace_level; 900 tp = initialization_sub_type(in, true); 901 strg = tn->tn_string; 902 903 if (!is_string_array(tp, strg->st_tspec)) 904 return false; 905 if (bl != NULL && tp->t_tspec != ARRAY && bl->bl_subscript != 0) 906 return false; 907 908 if (!tp->t_incomplete_array && tp->t_dim < (int)strg->st_len) { 909 /* non-null byte ignored in string initializer */ 910 warning(187); 911 } 912 913 if (tp == in->in_sym->s_type && tp->t_incomplete_array) { 914 if (bl != NULL) { 915 bl->bl_subscript = strg->st_len; 916 /* see brace_level_advance for the +1 */ 917 /* see initialization_set_size_of_unknown_array */ 918 } else 919 update_type_of_array_of_unknown_size(in->in_sym, 920 strg->st_len + 1); 921 } 922 923 return true; 924 } 925 926 /* 927 * Initialize a single sub-object as part of the currently ongoing 928 * initialization. 929 */ 930 static void 931 initialization_expr(struct initialization *in, tnode_t *tn) 932 { 933 struct brace_level *bl; 934 const type_t *tp; 935 936 if (in->in_err) 937 return; 938 939 bl = in->in_brace_level; 940 if (bl != NULL && bl->bl_confused) 941 return; 942 943 debug_enter(); 944 945 if (tn == NULL) 946 goto advance; 947 if (initialization_expr_using_assign(in, tn)) 948 goto done; 949 if (initialization_init_array_using_string(in, tn)) 950 goto advance; 951 952 if (bl != NULL) 953 brace_level_apply_designation(bl); 954 tp = initialization_sub_type(in, false); 955 if (tp == NULL) 956 goto done; 957 958 if (bl == NULL && !is_scalar(tp->t_tspec)) { 959 /* {}-enclosed initializer required */ 960 error(181); 961 goto done; 962 } 963 964 /* 965 * Hack to accept initializations with omitted braces, see 966 * c99_6_7_8_p28_example5 in test d_c99_init.c. Since both GCC and 967 * Clang already warn about this at level -Wall, there is no point 968 * in repeating the same check in lint. If needed, support for these 969 * edge cases could be added, but that would increase the complexity. 970 */ 971 if (is_scalar(tn->tn_type->t_tspec) && 972 (tp->t_tspec == ARRAY || is_struct_or_union(tp->t_tspec)) && 973 bl != NULL) { 974 bl->bl_confused = true; 975 goto done; 976 } 977 978 debug_step2("expecting '%s', expression has '%s'", 979 type_name(tp), type_name(tn->tn_type)); 980 check_init_expr(tp, in->in_sym, tn); 981 982 advance: 983 if (bl != NULL) 984 brace_level_advance(bl); 985 done: 986 if (bl != NULL) 987 designation_reset(&bl->bl_designation); 988 989 initialization_debug(in); 990 debug_leave(); 991 } 992 993 994 static struct initialization *init; 995 996 997 static struct initialization * 998 current_init(void) 999 { 1000 1001 lint_assert(init != NULL); 1002 return init; 1003 } 1004 1005 sym_t ** 1006 current_initsym(void) 1007 { 1008 1009 return ¤t_init()->in_sym; 1010 } 1011 1012 void 1013 begin_initialization(sym_t *sym) 1014 { 1015 struct initialization *in; 1016 1017 debug_step1("begin initialization of '%s'", type_name(sym->s_type)); 1018 #ifdef DEBUG 1019 debug_indentation++; 1020 #endif 1021 1022 in = initialization_new(sym); 1023 in->in_enclosing = init; 1024 init = in; 1025 } 1026 1027 void 1028 end_initialization(void) 1029 { 1030 struct initialization *in; 1031 1032 in = init; 1033 init = in->in_enclosing; 1034 initialization_free(in); 1035 1036 #ifdef DEBUG 1037 debug_indentation--; 1038 #endif 1039 debug_step0("end initialization"); 1040 } 1041 1042 void 1043 add_designator_member(sbuf_t *sb) 1044 { 1045 1046 initialization_add_designator(current_init(), sb->sb_name, 0); 1047 } 1048 1049 void 1050 add_designator_subscript(range_t range) 1051 { 1052 1053 initialization_add_designator(current_init(), NULL, range.hi); 1054 } 1055 1056 void 1057 init_lbrace(void) 1058 { 1059 1060 initialization_begin_brace_level(current_init()); 1061 } 1062 1063 void 1064 init_expr(tnode_t *tn) 1065 { 1066 1067 initialization_expr(current_init(), tn); 1068 } 1069 1070 void 1071 init_rbrace(void) 1072 { 1073 1074 initialization_end_brace_level(current_init()); 1075 } 1076