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