1 /* $NetBSD: init.c,v 1.20 2006/10/15 15:08:20 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1994, 1995 Jochen Pohl 5 * All Rights Reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Jochen Pohl for 18 * The NetBSD Project. 19 * 4. The name of the author may not be used to endorse or promote products 20 * derived from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #if HAVE_NBTOOL_CONFIG_H 35 #include "nbtool_config.h" 36 #endif 37 38 #include <sys/cdefs.h> 39 #if defined(__RCSID) && !defined(lint) 40 __RCSID("$NetBSD: init.c,v 1.20 2006/10/15 15:08:20 christos Exp $"); 41 #endif 42 43 #include <stdlib.h> 44 #include <string.h> 45 #include <ctype.h> 46 47 #include "lint1.h" 48 49 /* 50 * initerr is set as soon as a fatal error occurred in an initialisation. 51 * The effect is that the rest of the initialisation is ignored (parsed 52 * by yacc, expression trees built, but no initialisation takes place). 53 */ 54 int initerr; 55 56 /* Pointer to the symbol which is to be initialized. */ 57 sym_t *initsym; 58 59 /* Points to the top element of the initialisation stack. */ 60 istk_t *initstk; 61 62 typedef struct namlist { 63 const char *n_name; 64 struct namlist *n_prev; 65 struct namlist *n_next; 66 } namlist_t; 67 68 /* Points to a c9x named member; */ 69 namlist_t *namedmem = NULL; 70 71 72 static void popi2(void); 73 static void popinit(int); 74 static void pushinit(void); 75 static void testinit(void); 76 static void nextinit(int); 77 static int strginit(tnode_t *); 78 static void memberpop(void); 79 80 #ifndef DEBUG 81 #define DPRINTF(a) 82 #else 83 #define DPRINTF(a) printf a 84 #endif 85 86 void 87 memberpush(sb) 88 sbuf_t *sb; 89 { 90 namlist_t *nam = xcalloc(1, sizeof (namlist_t)); 91 nam->n_name = sb->sb_name; 92 DPRINTF(("memberpush = %s\n", nam->n_name)); 93 if (namedmem == NULL) { 94 nam->n_prev = nam->n_next = nam; 95 namedmem = nam; 96 } else { 97 namedmem->n_prev->n_next = nam; 98 nam->n_prev = namedmem->n_prev; 99 nam->n_next = namedmem; 100 namedmem->n_prev = nam; 101 } 102 #if 0 103 nam->n_next = namedmem; 104 namedmem = nam; 105 #endif 106 } 107 108 static void 109 memberpop() 110 { 111 DPRINTF(("memberpop = %s\n", namedmem->n_name)); 112 if (namedmem->n_next == namedmem) { 113 free(namedmem); 114 namedmem = NULL; 115 } else { 116 namlist_t *nam = namedmem; 117 namedmem = namedmem->n_next; 118 free(nam); 119 } 120 #if 0 121 namedmem = namedmem->n_next; 122 free(nam); 123 #endif 124 } 125 126 127 /* 128 * Initialize the initialisation stack by putting an entry for the variable 129 * which is to be initialized on it. 130 */ 131 void 132 prepinit(void) 133 { 134 istk_t *istk; 135 136 if (initerr) 137 return; 138 139 /* free memory used in last initialisation */ 140 while ((istk = initstk) != NULL) { 141 initstk = istk->i_nxt; 142 free(istk); 143 } 144 145 /* 146 * If the type which is to be initialized is an incomplete type, 147 * it must be duplicated. 148 */ 149 if (initsym->s_type->t_tspec == ARRAY && incompl(initsym->s_type)) 150 initsym->s_type = duptyp(initsym->s_type); 151 152 istk = initstk = xcalloc(1, sizeof (istk_t)); 153 istk->i_subt = initsym->s_type; 154 istk->i_cnt = 1; 155 156 } 157 158 static void 159 popi2(void) 160 { 161 #ifdef DEBUG 162 char buf[64]; 163 #endif 164 istk_t *istk; 165 sym_t *m; 166 167 DPRINTF(("popi2(%s): brace=%d count=%d namedmem %d\n", 168 tyname(buf, sizeof(buf), 169 initstk->i_type ? initstk->i_type : initstk->i_subt), 170 initstk->i_brace, initstk->i_cnt, initstk->i_namedmem)); 171 initstk = (istk = initstk)->i_nxt; 172 if (initstk == NULL) 173 LERROR("popi2()"); 174 free(istk); 175 176 istk = initstk; 177 178 istk->i_cnt--; 179 if (istk->i_cnt < 0) 180 LERROR("popi2()"); 181 182 DPRINTF(("popi2(): %d %s\n", istk->i_cnt, 183 namedmem ? namedmem->n_name : "*null*")); 184 if (istk->i_cnt >= 0 && namedmem != NULL) { 185 DPRINTF(("popi2(): %d %s %s\n", istk->i_cnt, 186 tyname(buf, sizeof(buf), istk->i_type), namedmem->n_name)); 187 for (m = istk->i_type->t_str->memb; m != NULL; m = m->s_nxt) { 188 if (m->s_field && m->s_name == unnamed) 189 continue; 190 if (strcmp(m->s_name, namedmem->n_name) == 0) { 191 istk->i_subt = m->s_type; 192 istk->i_cnt++; 193 memberpop(); 194 return; 195 } 196 } 197 error(101, namedmem->n_name); 198 memberpop(); 199 istk->i_namedmem = 1; 200 return; 201 } 202 /* 203 * If the removed element was a structure member, we must go 204 * to the next structure member. 205 */ 206 if (istk->i_cnt > 0 && istk->i_type->t_tspec == STRUCT && 207 !istk->i_namedmem) { 208 do { 209 m = istk->i_mem = istk->i_mem->s_nxt; 210 if (m == NULL) 211 LERROR("popi2()"); 212 } while (m->s_field && m->s_name == unnamed); 213 istk->i_subt = m->s_type; 214 } 215 } 216 217 static void 218 popinit(int brace) 219 { 220 DPRINTF(("popinit(%d)\n", brace)); 221 222 if (brace) { 223 /* 224 * Take all entries, including the first which requires 225 * a closing brace, from the stack. 226 */ 227 do { 228 brace = initstk->i_brace; 229 popi2(); 230 } while (!brace); 231 } else { 232 /* 233 * Take all entries which cannot be used for further 234 * initializers from the stack, but do this only if 235 * they do not require a closing brace. 236 */ 237 while (!initstk->i_brace && 238 initstk->i_cnt == 0 && !initstk->i_nolimit) { 239 popi2(); 240 } 241 } 242 } 243 244 static void 245 pushinit(void) 246 { 247 #ifdef DEBUG 248 char buf[64]; 249 #endif 250 istk_t *istk; 251 int cnt; 252 sym_t *m; 253 254 istk = initstk; 255 256 /* Extend an incomplete array type by one element */ 257 if (istk->i_cnt == 0) { 258 DPRINTF(("pushinit(extend) %s\n", tyname(buf, sizeof(buf), 259 istk->i_type))); 260 /* 261 * Inside of other aggregate types must not be an incomplete 262 * type. 263 */ 264 if (istk->i_nxt->i_nxt != NULL) 265 LERROR("pushinit()"); 266 istk->i_cnt = 1; 267 if (istk->i_type->t_tspec != ARRAY) 268 LERROR("pushinit()"); 269 istk->i_type->t_dim++; 270 /* from now its an complete type */ 271 setcompl(istk->i_type, 0); 272 } 273 274 if (istk->i_cnt <= 0) 275 LERROR("pushinit()"); 276 if (istk->i_type != NULL && issclt(istk->i_type->t_tspec)) 277 LERROR("pushinit()"); 278 279 initstk = xcalloc(1, sizeof (istk_t)); 280 initstk->i_nxt = istk; 281 initstk->i_type = istk->i_subt; 282 if (initstk->i_type->t_tspec == FUNC) 283 LERROR("pushinit()"); 284 285 again: 286 istk = initstk; 287 288 DPRINTF(("pushinit(%s)\n", tyname(buf, sizeof(buf), istk->i_type))); 289 switch (istk->i_type->t_tspec) { 290 case ARRAY: 291 if (namedmem) { 292 DPRINTF(("pushinit ARRAY %s brace=%d\n", 293 namedmem->n_name, istk->i_brace)); 294 goto pop; 295 } else if (istk->i_nxt->i_namedmem) { 296 DPRINTF(("pushinit ARRAY brace=%d, namedmem=%d\n", 297 istk->i_brace, istk->i_nxt->i_namedmem)); 298 istk->i_brace = 1; 299 } 300 301 if (incompl(istk->i_type) && istk->i_nxt->i_nxt != NULL) { 302 /* initialisation of an incomplete type */ 303 error(175); 304 initerr = 1; 305 return; 306 } 307 istk->i_subt = istk->i_type->t_subt; 308 istk->i_nolimit = incompl(istk->i_type); 309 istk->i_cnt = istk->i_type->t_dim; 310 DPRINTF(("elements array %s[%d] %s\n", 311 tyname(buf, sizeof(buf), istk->i_subt), istk->i_cnt, 312 namedmem ? namedmem->n_name : "*none*")); 313 break; 314 case UNION: 315 if (tflag) 316 /* initialisation of union is illegal in trad. C */ 317 warning(238); 318 /* FALLTHROUGH */ 319 case STRUCT: 320 if (incompl(istk->i_type)) { 321 /* initialisation of an incomplete type */ 322 error(175); 323 initerr = 1; 324 return; 325 } 326 cnt = 0; 327 DPRINTF(("2. member lookup %s %s\n", 328 tyname(buf, sizeof(buf), istk->i_type), 329 namedmem ? namedmem->n_name : "*none*")); 330 for (m = istk->i_type->t_str->memb; m != NULL; m = m->s_nxt) { 331 if (m->s_field && m->s_name == unnamed) 332 continue; 333 if (namedmem != NULL) { 334 DPRINTF(("pushinit():[member:%s, looking:%s]\n", 335 m->s_name, namedmem->n_name)); 336 if (strcmp(m->s_name, namedmem->n_name) == 0) { 337 cnt++; 338 break; 339 } else 340 continue; 341 } 342 if (++cnt == 1) { 343 istk->i_mem = m; 344 istk->i_subt = m->s_type; 345 } 346 } 347 if (namedmem != NULL) { 348 if (m == NULL) { 349 DPRINTF(("pushinit(): struct pop\n")); 350 goto pop; 351 } else { 352 istk->i_mem = m; 353 istk->i_subt = m->s_type; 354 } 355 istk->i_namedmem = 1; 356 memberpop(); 357 cnt = istk->i_type->t_tspec == STRUCT ? 2 : 1; 358 } 359 if (cnt == 0) { 360 /* cannot init. struct/union with no named member */ 361 error(179); 362 initerr = 1; 363 return; 364 } 365 istk->i_cnt = istk->i_type->t_tspec == STRUCT ? cnt : 1; 366 break; 367 default: 368 if (namedmem) { 369 DPRINTF(("pushinit(): pop\n")); 370 pop: 371 free(istk); 372 initstk = initstk->i_nxt; 373 goto again; 374 } 375 istk->i_cnt = 1; 376 break; 377 } 378 } 379 380 static void 381 testinit(void) 382 { 383 istk_t *istk; 384 385 istk = initstk; 386 387 /* 388 * If a closing brace is expected we have at least one initializer 389 * too much. 390 */ 391 if (istk->i_cnt == 0 && !istk->i_nolimit && !istk->i_namedmem) { 392 switch (istk->i_type->t_tspec) { 393 case ARRAY: 394 /* too many array initializers */ 395 error(173); 396 break; 397 case STRUCT: 398 case UNION: 399 /* too many struct/union initializers */ 400 error(172); 401 break; 402 default: 403 /* too many initializers */ 404 error(174); 405 break; 406 } 407 initerr = 1; 408 } 409 } 410 411 static void 412 nextinit(int brace) 413 { 414 char buf[64]; 415 416 DPRINTF(("nextinit(%d)\n", brace)); 417 if (!brace) { 418 if (initstk->i_type == NULL && 419 !issclt(initstk->i_subt->t_tspec)) { 420 /* {}-enclosed initializer required */ 421 error(181); 422 } 423 /* 424 * Make sure an entry with a scalar type is at the top 425 * of the stack. 426 */ 427 if (!initerr) 428 testinit(); 429 while (!initerr && (initstk->i_type == NULL || 430 !issclt(initstk->i_type->t_tspec))) { 431 if (!initerr) 432 pushinit(); 433 } 434 } else { 435 if (initstk->i_type != NULL && 436 issclt(initstk->i_type->t_tspec)) { 437 /* invalid initializer */ 438 error(176, tyname(buf, sizeof(buf), initstk->i_type)); 439 initerr = 1; 440 } 441 if (!initerr) 442 testinit(); 443 if (!initerr) 444 pushinit(); 445 if (!initerr) 446 initstk->i_brace = 1; 447 } 448 } 449 450 void 451 initlbr(void) 452 { 453 DPRINTF(("initlbr\n")); 454 455 if (initerr) 456 return; 457 458 if ((initsym->s_scl == AUTO || initsym->s_scl == REG) && 459 initstk->i_nxt == NULL) { 460 if (tflag && !issclt(initstk->i_subt->t_tspec)) 461 /* no automatic aggregate initialization in trad. C*/ 462 warning(188); 463 } 464 465 /* 466 * Remove all entries which cannot be used for further initializers 467 * and do not expect a closing brace. 468 */ 469 popinit(0); 470 471 nextinit(1); 472 } 473 474 void 475 initrbr(void) 476 { 477 DPRINTF(("initrbr\n")); 478 479 if (initerr) 480 return; 481 482 popinit(1); 483 } 484 485 void 486 mkinit(tnode_t *tn) 487 { 488 ptrdiff_t offs; 489 sym_t *sym; 490 tspec_t lt, rt; 491 tnode_t *ln; 492 struct mbl *tmem; 493 scl_t sc; 494 #ifdef DEBUG 495 char buf[64], sbuf[64]; 496 #endif 497 498 DPRINTF(("mkinit(%s %s)\n", tyname(buf, sizeof(buf), tn->tn_type), 499 prtnode(sbuf, sizeof(sbuf), tn))); 500 if (initerr || tn == NULL) 501 goto end; 502 503 sc = initsym->s_scl; 504 505 /* 506 * Do not test for automatic aggregate initialisation. If the 507 * initializer starts with a brace we have the warning already. 508 * If not, an error will be printed that the initializer must 509 * be enclosed by braces. 510 */ 511 512 /* 513 * Local initialisation of non-array-types with only one expression 514 * without braces is done by ASSIGN 515 */ 516 if ((sc == AUTO || sc == REG) && 517 initsym->s_type->t_tspec != ARRAY && initstk->i_nxt == NULL) { 518 ln = getnnode(initsym, 0); 519 ln->tn_type = tduptyp(ln->tn_type); 520 ln->tn_type->t_const = 0; 521 tn = build(ASSIGN, ln, tn); 522 expr(tn, 0, 0, 1); 523 goto end; 524 } 525 526 /* 527 * Remove all entries which cannot be used for further initializers 528 * and do not require a closing brace. 529 */ 530 popinit(0); 531 532 /* Initialisations by strings are done in strginit(). */ 533 if (strginit(tn)) 534 goto end; 535 536 nextinit(0); 537 if (initerr || tn == NULL) 538 goto end; 539 540 initstk->i_cnt--; 541 DPRINTF(("mkinit() cnt=%d tn=%p\n", initstk->i_cnt, tn)); 542 /* Create a temporary node for the left side. */ 543 ln = tgetblk(sizeof (tnode_t)); 544 ln->tn_op = NAME; 545 ln->tn_type = tduptyp(initstk->i_type); 546 ln->tn_type->t_const = 0; 547 ln->tn_lvalue = 1; 548 ln->tn_sym = initsym; /* better than nothing */ 549 550 tn = cconv(tn); 551 552 lt = ln->tn_type->t_tspec; 553 rt = tn->tn_type->t_tspec; 554 555 if (!issclt(lt)) 556 LERROR("mkinit()"); 557 558 if (!typeok(INIT, 0, ln, tn)) 559 goto end; 560 561 /* 562 * Store the tree memory. This is nessesary because otherwise 563 * expr() would free it. 564 */ 565 tmem = tsave(); 566 expr(tn, 1, 0, 1); 567 trestor(tmem); 568 569 if (isityp(lt) && ln->tn_type->t_isfield && !isityp(rt)) { 570 /* 571 * Bit-fields can be initialized in trad. C only by integer 572 * constants. 573 */ 574 if (tflag) 575 /* bit-field initialisation is illegal in trad. C */ 576 warning(186); 577 } 578 579 if (lt != rt || (initstk->i_type->t_isfield && tn->tn_op == CON)) 580 tn = convert(INIT, 0, initstk->i_type, tn); 581 582 if (tn != NULL && tn->tn_op != CON) { 583 sym = NULL; 584 offs = 0; 585 if (conaddr(tn, &sym, &offs) == -1) { 586 if (sc == AUTO || sc == REG) { 587 /* non-constant initializer */ 588 (void)gnuism(177); 589 } else { 590 /* non-constant initializer */ 591 error(177); 592 } 593 } 594 } 595 596 end: 597 /* 598 * We only free the block, if we are not a compound declaration 599 * We know that the only symbols that start with a digit are the 600 * ones we allocate with mktempsym() for compound declarations 601 */ 602 if (!isdigit((unsigned char)initsym->s_name[0])) 603 tfreeblk(); 604 } 605 606 607 static int 608 strginit(tnode_t *tn) 609 { 610 tspec_t t; 611 istk_t *istk; 612 int len; 613 strg_t *strg; 614 615 if (tn->tn_op != STRING) 616 return (0); 617 618 istk = initstk; 619 strg = tn->tn_strg; 620 621 /* 622 * Check if we have an array type which can be initialized by 623 * the string. 624 */ 625 if (istk->i_subt != NULL && istk->i_subt->t_tspec == ARRAY) { 626 DPRINTF(("strginit subt array\n")); 627 t = istk->i_subt->t_subt->t_tspec; 628 if (!((strg->st_tspec == CHAR && 629 (t == CHAR || t == UCHAR || t == SCHAR)) || 630 (strg->st_tspec == WCHAR && t == WCHAR))) { 631 return (0); 632 } 633 /* Put the array at top of stack */ 634 pushinit(); 635 istk = initstk; 636 } else if (istk->i_type != NULL && istk->i_type->t_tspec == ARRAY) { 637 DPRINTF(("strginit type array\n")); 638 t = istk->i_type->t_subt->t_tspec; 639 if (!((strg->st_tspec == CHAR && 640 (t == CHAR || t == UCHAR || t == SCHAR)) || 641 (strg->st_tspec == WCHAR && t == WCHAR))) { 642 return (0); 643 } 644 /* 645 * If the array is already partly initialized, we are 646 * wrong here. 647 */ 648 if (istk->i_cnt != istk->i_type->t_dim) 649 return (0); 650 } else { 651 return (0); 652 } 653 654 /* Get length without trailing NUL character. */ 655 len = strg->st_len; 656 657 if (istk->i_nolimit) { 658 istk->i_nolimit = 0; 659 istk->i_type->t_dim = len + 1; 660 /* from now complete type */ 661 setcompl(istk->i_type, 0); 662 } else { 663 if (istk->i_type->t_dim < len) { 664 /* non-null byte ignored in string initializer */ 665 warning(187); 666 } 667 } 668 669 /* In every case the array is initialized completely. */ 670 istk->i_cnt = 0; 671 672 return (1); 673 } 674