1 /* $NetBSD: init.c,v 1.27 2015/07/28 17:55:13 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.27 2015/07/28 17:55:13 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(("%s: %s %p\n", __func__, nam->n_name, nam)); 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 } 103 104 static void 105 memberpop(void) 106 { 107 DPRINTF(("%s: %s %p\n", __func__, namedmem->n_name, namedmem)); 108 if (namedmem->n_next == namedmem) { 109 free(namedmem); 110 namedmem = NULL; 111 } else { 112 namlist_t *nam = namedmem; 113 namedmem = namedmem->n_next; 114 namedmem->n_next = nam->n_next; 115 namedmem->n_prev = nam->n_prev; 116 free(nam); 117 } 118 } 119 120 121 /* 122 * Initialize the initialisation stack by putting an entry for the variable 123 * which is to be initialized on it. 124 */ 125 void 126 prepinit(void) 127 { 128 istk_t *istk; 129 130 if (initerr) 131 return; 132 133 /* free memory used in last initialisation */ 134 while ((istk = initstk) != NULL) { 135 initstk = istk->i_nxt; 136 free(istk); 137 } 138 139 /* 140 * If the type which is to be initialized is an incomplete type, 141 * it must be duplicated. 142 */ 143 if (initsym->s_type->t_tspec == ARRAY && incompl(initsym->s_type)) 144 initsym->s_type = duptyp(initsym->s_type); 145 146 istk = initstk = xcalloc(1, sizeof (istk_t)); 147 istk->i_subt = initsym->s_type; 148 istk->i_cnt = 1; 149 150 } 151 152 static void 153 popi2(void) 154 { 155 #ifdef DEBUG 156 char buf[64]; 157 #endif 158 istk_t *istk; 159 sym_t *m; 160 161 DPRINTF(("%s+(%s): brace=%d count=%d namedmem %d\n", __func__, 162 tyname(buf, sizeof(buf), 163 initstk->i_type ? initstk->i_type : initstk->i_subt), 164 initstk->i_brace, initstk->i_cnt, initstk->i_namedmem)); 165 initstk = (istk = initstk)->i_nxt; 166 free(istk); 167 168 istk = initstk; 169 if (istk == NULL) 170 LERROR("popi2()"); 171 172 DPRINTF(("%s-(%s): brace=%d count=%d namedmem %d\n", __func__, 173 tyname(buf, sizeof(buf), 174 initstk->i_type ? initstk->i_type : initstk->i_subt), 175 initstk->i_brace, initstk->i_cnt, initstk->i_namedmem)); 176 177 istk->i_cnt--; 178 if (istk->i_cnt < 0) 179 LERROR("popi2()"); 180 181 DPRINTF(("%s(): %d %s\n", __func__, istk->i_cnt, 182 namedmem ? namedmem->n_name : "*null*")); 183 if (istk->i_cnt >= 0 && namedmem != NULL) { 184 DPRINTF(("%s(): %d %s %s\n", __func__, istk->i_cnt, 185 tyname(buf, sizeof(buf), istk->i_type), namedmem->n_name)); 186 for (m = istk->i_type->t_str->memb; m != NULL; m = m->s_nxt) { 187 DPRINTF(("%s(): pop [%s %s]\n", __func__, 188 namedmem->n_name, m->s_name)); 189 if (m->s_field && m->s_name == unnamed) 190 continue; 191 if (strcmp(m->s_name, namedmem->n_name) == 0) { 192 istk->i_subt = m->s_type; 193 istk->i_cnt++; 194 memberpop(); 195 return; 196 } 197 } 198 error(101, namedmem->n_name); 199 DPRINTF(("%s(): namedmem %s\n", __func__, namedmem->n_name)); 200 memberpop(); 201 istk->i_namedmem = 1; 202 return; 203 } 204 /* 205 * If the removed element was a structure member, we must go 206 * to the next structure member. 207 */ 208 if (istk->i_cnt > 0 && istk->i_type->t_tspec == STRUCT && 209 !istk->i_namedmem) { 210 do { 211 m = istk->i_mem = istk->i_mem->s_nxt; 212 if (m == NULL) 213 LERROR("popi2()"); 214 DPRINTF(("%s(): pop %s\n", __func__, m->s_name)); 215 } while (m->s_field && m->s_name == unnamed); 216 istk->i_subt = m->s_type; 217 } 218 } 219 220 static void 221 popinit(int brace) 222 { 223 DPRINTF(("%s(%d)\n", __func__, brace)); 224 225 if (brace) { 226 /* 227 * Take all entries, including the first which requires 228 * a closing brace, from the stack. 229 */ 230 DPRINTF(("%s: brace\n", __func__)); 231 do { 232 brace = initstk->i_brace; 233 DPRINTF(("%s: loop brace %d\n", __func__, brace)); 234 popi2(); 235 } while (!brace); 236 DPRINTF(("%s: brace done\n", __func__)); 237 } else { 238 /* 239 * Take all entries which cannot be used for further 240 * initializers from the stack, but do this only if 241 * they do not require a closing brace. 242 */ 243 DPRINTF(("%s: no brace\n", __func__)); 244 while (!initstk->i_brace && 245 initstk->i_cnt == 0 && !initstk->i_nolimit) { 246 popi2(); 247 } 248 DPRINTF(("%s: no brace done\n", __func__)); 249 } 250 } 251 252 static void 253 pushinit(void) 254 { 255 #ifdef DEBUG 256 char buf[64]; 257 #endif 258 istk_t *istk, *inxt; 259 int cnt; 260 sym_t *m; 261 262 istk = initstk; 263 264 /* Extend an incomplete array type by one element */ 265 if (istk->i_cnt == 0) { 266 DPRINTF(("%s(extend) %s\n", __func__, tyname(buf, sizeof(buf), 267 istk->i_type))); 268 /* 269 * Inside of other aggregate types must not be an incomplete 270 * type. 271 */ 272 if (istk->i_nxt->i_nxt != NULL) 273 LERROR("pushinit()"); 274 istk->i_cnt = 1; 275 if (istk->i_type->t_tspec != ARRAY) 276 LERROR("pushinit()"); 277 istk->i_type->t_dim++; 278 /* from now its an complete type */ 279 setcompl(istk->i_type, 0); 280 } 281 282 if (istk->i_cnt <= 0) 283 LERROR("pushinit()"); 284 if (istk->i_type != NULL && issclt(istk->i_type->t_tspec)) 285 LERROR("pushinit()"); 286 287 initstk = xcalloc(1, sizeof (istk_t)); 288 initstk->i_nxt = istk; 289 initstk->i_type = istk->i_subt; 290 if (initstk->i_type->t_tspec == FUNC) 291 LERROR("pushinit()"); 292 293 again: 294 istk = initstk; 295 296 DPRINTF(("%s(%s)\n", __func__, tyname(buf, sizeof(buf), istk->i_type))); 297 switch (istk->i_type->t_tspec) { 298 case ARRAY: 299 if (namedmem) { 300 DPRINTF(("%s: ARRAY %s brace=%d\n", __func__, 301 namedmem->n_name, istk->i_brace)); 302 goto pop; 303 } else if (istk->i_nxt->i_namedmem) { 304 istk->i_brace = 1; 305 DPRINTF(("%s ARRAY brace=%d, namedmem=%d\n", __func__, 306 istk->i_brace, istk->i_nxt->i_namedmem)); 307 } 308 309 if (incompl(istk->i_type) && istk->i_nxt->i_nxt != NULL) { 310 /* initialisation of an incomplete type */ 311 error(175); 312 initerr = 1; 313 return; 314 } 315 istk->i_subt = istk->i_type->t_subt; 316 istk->i_nolimit = incompl(istk->i_type); 317 istk->i_cnt = istk->i_type->t_dim; 318 DPRINTF(("%s: elements array %s[%d] %s\n", __func__, 319 tyname(buf, sizeof(buf), istk->i_subt), istk->i_cnt, 320 namedmem ? namedmem->n_name : "*none*")); 321 break; 322 case UNION: 323 if (tflag) 324 /* initialisation of union is illegal in trad. C */ 325 warning(238); 326 /* FALLTHROUGH */ 327 case STRUCT: 328 if (incompl(istk->i_type)) { 329 /* initialisation of an incomplete type */ 330 error(175); 331 initerr = 1; 332 return; 333 } 334 cnt = 0; 335 DPRINTF(("%s: 2. member lookup %s %s i_namedmem=%d\n", __func__, 336 tyname(buf, sizeof(buf), istk->i_type), 337 namedmem ? namedmem->n_name : "*none*", istk->i_namedmem)); 338 for (m = istk->i_type->t_str->memb; m != NULL; m = m->s_nxt) { 339 if (m->s_field && m->s_name == unnamed) 340 continue; 341 if (namedmem != NULL) { 342 DPRINTF(("%s():[member:%s, looking:%s]\n", 343 __func__, m->s_name, namedmem->n_name)); 344 if (strcmp(m->s_name, namedmem->n_name) == 0) { 345 cnt++; 346 break; 347 } else 348 continue; 349 } 350 if (++cnt == 1) { 351 istk->i_mem = m; 352 istk->i_subt = m->s_type; 353 } 354 } 355 if (namedmem != NULL) { 356 if (m == NULL) { 357 DPRINTF(("%s(): struct pop\n", __func__)); 358 goto pop; 359 } 360 istk->i_mem = m; 361 istk->i_subt = m->s_type; 362 istk->i_namedmem = 1; 363 DPRINTF(("%s(): namedmem %s\n", __func__, 364 namedmem->n_name)); 365 memberpop(); 366 cnt = istk->i_type->t_tspec == STRUCT ? 2 : 1; 367 } 368 istk->i_brace = 1; 369 DPRINTF(("%s(): %s brace=%d\n", __func__, 370 tyname(buf, sizeof(buf), 371 istk->i_type ? istk->i_type : istk->i_subt), 372 istk->i_brace)); 373 if (cnt == 0) { 374 /* cannot init. struct/union with no named member */ 375 error(179); 376 initerr = 1; 377 return; 378 } 379 istk->i_cnt = istk->i_type->t_tspec == STRUCT ? cnt : 1; 380 break; 381 default: 382 if (namedmem) { 383 DPRINTF(("%s(): pop\n", __func__)); 384 pop: 385 inxt = initstk->i_nxt; 386 free(istk); 387 initstk = inxt; 388 goto again; 389 } 390 istk->i_cnt = 1; 391 break; 392 } 393 } 394 395 static void 396 testinit(void) 397 { 398 istk_t *istk; 399 400 istk = initstk; 401 402 /* 403 * If a closing brace is expected we have at least one initializer 404 * too much. 405 */ 406 if (istk->i_cnt == 0 && !istk->i_nolimit && !istk->i_namedmem) { 407 switch (istk->i_type->t_tspec) { 408 case ARRAY: 409 /* too many array initializers */ 410 error(173, istk->i_type->t_dim); 411 break; 412 case STRUCT: 413 case UNION: 414 /* too many struct/union initializers */ 415 error(172); 416 break; 417 default: 418 /* too many initializers */ 419 error(174); 420 break; 421 } 422 initerr = 1; 423 } 424 } 425 426 static void 427 nextinit(int brace) 428 { 429 char buf[64]; 430 431 DPRINTF(("%s(%d)\n", __func__, brace)); 432 if (!brace) { 433 if (initstk->i_type == NULL && 434 !issclt(initstk->i_subt->t_tspec)) { 435 /* {}-enclosed initializer required */ 436 error(181); 437 } 438 /* 439 * Make sure an entry with a scalar type is at the top 440 * of the stack. 441 */ 442 if (!initerr) 443 testinit(); 444 while (!initerr && (initstk->i_type == NULL || 445 !issclt(initstk->i_type->t_tspec))) { 446 if (!initerr) 447 pushinit(); 448 } 449 } else { 450 if (initstk->i_type != NULL && 451 issclt(initstk->i_type->t_tspec)) { 452 /* invalid initializer */ 453 error(176, tyname(buf, sizeof(buf), initstk->i_type)); 454 initerr = 1; 455 } 456 if (!initerr) 457 testinit(); 458 if (!initerr) 459 pushinit(); 460 if (!initerr) { 461 initstk->i_brace = 1; 462 DPRINTF(("%s(): %p %s brace=%d\n", __func__, 463 namedmem, tyname(buf, sizeof(buf), 464 initstk->i_type ? initstk->i_type : 465 initstk->i_subt), initstk->i_brace)); 466 } 467 } 468 } 469 470 void 471 initlbr(void) 472 { 473 DPRINTF(("%s\n", __func__)); 474 475 if (initerr) 476 return; 477 478 if ((initsym->s_scl == AUTO || initsym->s_scl == REG) && 479 initstk->i_nxt == NULL) { 480 if (tflag && !issclt(initstk->i_subt->t_tspec)) 481 /* no automatic aggregate initialization in trad. C*/ 482 warning(188); 483 } 484 485 /* 486 * Remove all entries which cannot be used for further initializers 487 * and do not expect a closing brace. 488 */ 489 popinit(0); 490 491 nextinit(1); 492 } 493 494 void 495 initrbr(void) 496 { 497 DPRINTF(("%s\n", __func__)); 498 499 if (initerr) 500 return; 501 502 popinit(1); 503 } 504 505 void 506 mkinit(tnode_t *tn) 507 { 508 ptrdiff_t offs; 509 sym_t *sym; 510 tspec_t lt, rt; 511 tnode_t *ln; 512 struct mbl *tmem; 513 scl_t sc; 514 #ifdef DEBUG 515 char buf[64], sbuf[64]; 516 #endif 517 518 DPRINTF(("%s(%s %s)\n", __func__, tyname(buf, sizeof(buf), tn->tn_type), 519 prtnode(sbuf, sizeof(sbuf), tn))); 520 if (initerr || tn == NULL) 521 return; 522 523 sc = initsym->s_scl; 524 525 /* 526 * Do not test for automatic aggregate initialisation. If the 527 * initializer starts with a brace we have the warning already. 528 * If not, an error will be printed that the initializer must 529 * be enclosed by braces. 530 */ 531 532 /* 533 * Local initialisation of non-array-types with only one expression 534 * without braces is done by ASSIGN 535 */ 536 if ((sc == AUTO || sc == REG) && 537 initsym->s_type->t_tspec != ARRAY && initstk->i_nxt == NULL) { 538 ln = getnnode(initsym, 0); 539 ln->tn_type = tduptyp(ln->tn_type); 540 ln->tn_type->t_const = 0; 541 tn = build(ASSIGN, ln, tn); 542 expr(tn, 0, 0, 0); 543 return; 544 } 545 546 /* 547 * Remove all entries which cannot be used for further initializers 548 * and do not require a closing brace. 549 */ 550 popinit(0); 551 552 /* Initialisations by strings are done in strginit(). */ 553 if (strginit(tn)) 554 return; 555 556 nextinit(0); 557 if (initerr || tn == NULL) 558 return; 559 560 initstk->i_cnt--; 561 DPRINTF(("%s() cnt=%d tn=%p\n", __func__, initstk->i_cnt, tn)); 562 /* Create a temporary node for the left side. */ 563 ln = tgetblk(sizeof (tnode_t)); 564 ln->tn_op = NAME; 565 ln->tn_type = tduptyp(initstk->i_type); 566 ln->tn_type->t_const = 0; 567 ln->tn_lvalue = 1; 568 ln->tn_sym = initsym; /* better than nothing */ 569 570 tn = cconv(tn); 571 572 lt = ln->tn_type->t_tspec; 573 rt = tn->tn_type->t_tspec; 574 575 if (!issclt(lt)) 576 LERROR("mkinit()"); 577 578 if (!typeok(INIT, 0, ln, tn)) 579 return; 580 581 /* 582 * Store the tree memory. This is nessesary because otherwise 583 * expr() would free it. 584 */ 585 tmem = tsave(); 586 expr(tn, 1, 0, 1); 587 trestor(tmem); 588 589 if (isityp(lt) && ln->tn_type->t_isfield && !isityp(rt)) { 590 /* 591 * Bit-fields can be initialized in trad. C only by integer 592 * constants. 593 */ 594 if (tflag) 595 /* bit-field initialisation is illegal in trad. C */ 596 warning(186); 597 } 598 599 if (lt != rt || (initstk->i_type->t_isfield && tn->tn_op == CON)) 600 tn = convert(INIT, 0, initstk->i_type, tn); 601 602 if (tn != NULL && tn->tn_op != CON) { 603 sym = NULL; 604 offs = 0; 605 if (conaddr(tn, &sym, &offs) == -1) { 606 if (sc == AUTO || sc == REG) { 607 /* non-constant initializer */ 608 (void)c99ism(177); 609 } else { 610 /* non-constant initializer */ 611 error(177); 612 } 613 } 614 } 615 } 616 617 618 static int 619 strginit(tnode_t *tn) 620 { 621 tspec_t t; 622 istk_t *istk; 623 int len; 624 strg_t *strg; 625 626 if (tn->tn_op != STRING) 627 return (0); 628 629 istk = initstk; 630 strg = tn->tn_strg; 631 632 /* 633 * Check if we have an array type which can be initialized by 634 * the string. 635 */ 636 if (istk->i_subt != NULL && istk->i_subt->t_tspec == ARRAY) { 637 DPRINTF(("%s: subt array\n", __func__)); 638 t = istk->i_subt->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 /* Put the array at top of stack */ 645 pushinit(); 646 istk = initstk; 647 } else if (istk->i_type != NULL && istk->i_type->t_tspec == ARRAY) { 648 DPRINTF(("%s: type array\n", __func__)); 649 t = istk->i_type->t_subt->t_tspec; 650 if (!((strg->st_tspec == CHAR && 651 (t == CHAR || t == UCHAR || t == SCHAR)) || 652 (strg->st_tspec == WCHAR && t == WCHAR))) { 653 return (0); 654 } 655 /* 656 * If the array is already partly initialized, we are 657 * wrong here. 658 */ 659 if (istk->i_cnt != istk->i_type->t_dim) 660 return (0); 661 } else { 662 return (0); 663 } 664 665 /* Get length without trailing NUL character. */ 666 len = strg->st_len; 667 668 if (istk->i_nolimit) { 669 istk->i_nolimit = 0; 670 istk->i_type->t_dim = len + 1; 671 /* from now complete type */ 672 setcompl(istk->i_type, 0); 673 } else { 674 if (istk->i_type->t_dim < len) { 675 /* non-null byte ignored in string initializer */ 676 warning(187); 677 } 678 } 679 680 /* In every case the array is initialized completely. */ 681 istk->i_cnt = 0; 682 683 return (1); 684 } 685