1 /* $NetBSD: tree.c,v 1.36 2003/05/30 13:37:49 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 #include <sys/cdefs.h> 35 #if defined(__RCSID) && !defined(lint) 36 __RCSID("$NetBSD: tree.c,v 1.36 2003/05/30 13:37:49 christos Exp $"); 37 #endif 38 39 #include <stdlib.h> 40 #include <string.h> 41 #include <float.h> 42 #include <limits.h> 43 #include <math.h> 44 45 #include "lint1.h" 46 #include "cgram.h" 47 48 /* Various flags for each operator. */ 49 static mod_t modtab[NOPS]; 50 51 static tnode_t *getinode(tspec_t, int64_t); 52 static void ptrcmpok(op_t, tnode_t *, tnode_t *); 53 static int asgntypok(op_t, int, tnode_t *, tnode_t *); 54 static void chkbeop(op_t, tnode_t *, tnode_t *); 55 static void chkeop2(op_t, int, tnode_t *, tnode_t *); 56 static void chkeop1(op_t, int, tnode_t *, tnode_t *); 57 static tnode_t *mktnode(op_t, type_t *, tnode_t *, tnode_t *); 58 static void balance(op_t, tnode_t **, tnode_t **); 59 static void incompat(op_t, tspec_t, tspec_t); 60 static void illptrc(mod_t *, type_t *, type_t *); 61 static void mrgqual(type_t **, type_t *, type_t *); 62 static int conmemb(type_t *); 63 static void ptconv(int, tspec_t, tspec_t, type_t *, tnode_t *); 64 static void iiconv(op_t, int, tspec_t, tspec_t, type_t *, tnode_t *); 65 static void piconv(op_t, tspec_t, type_t *, tnode_t *); 66 static void ppconv(op_t, tnode_t *, type_t *); 67 static tnode_t *bldstr(op_t, tnode_t *, tnode_t *); 68 static tnode_t *bldincdec(op_t, tnode_t *); 69 static tnode_t *bldamper(tnode_t *, int); 70 static tnode_t *bldplmi(op_t, tnode_t *, tnode_t *); 71 static tnode_t *bldshft(op_t, tnode_t *, tnode_t *); 72 static tnode_t *bldcol(tnode_t *, tnode_t *); 73 static tnode_t *bldasgn(op_t, tnode_t *, tnode_t *); 74 static tnode_t *plength(type_t *); 75 static tnode_t *fold(tnode_t *); 76 static tnode_t *foldtst(tnode_t *); 77 static tnode_t *foldflt(tnode_t *); 78 static tnode_t *chkfarg(type_t *, tnode_t *); 79 static tnode_t *parg(int, type_t *, tnode_t *); 80 static void nulleff(tnode_t *); 81 static void displexpr(tnode_t *, int); 82 static void chkaidx(tnode_t *, int); 83 static void chkcomp(op_t, tnode_t *, tnode_t *); 84 static void precconf(tnode_t *); 85 86 /* 87 * Initialize mods of operators. 88 */ 89 void 90 initmtab(void) 91 { 92 static struct { 93 op_t op; 94 mod_t m; 95 } imods[] = { 96 { ARROW, { 1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0, 97 "->" } }, 98 { POINT, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 99 "." } }, 100 { NOT, { 0,1,0,1,0,1,0,1,0,0,0,0,0,0,0,1,0, 101 "!" } }, 102 { COMPL, { 0,0,1,0,0,1,1,0,0,0,0,0,0,0,0,1,1, 103 "~" } }, 104 { INCBEF, { 0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0, 105 "prefix++" } }, 106 { DECBEF, { 0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0, 107 "prefix--" } }, 108 { INCAFT, { 0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0, 109 "postfix++" } }, 110 { DECAFT, { 0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0, 111 "postfix--" } }, 112 { UPLUS, { 0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,1, 113 "unary +" } }, 114 { UMINUS, { 0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,1,1, 115 "unary -" } }, 116 { STAR, { 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0, 117 "unary *" } }, 118 { AMPER, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 119 "unary &" } }, 120 { MULT, { 1,0,0,0,1,1,1,0,1,0,0,1,0,0,0,1,1, 121 "*" } }, 122 { DIV, { 1,0,0,0,1,1,1,0,1,0,1,1,0,0,0,1,1, 123 "/" } }, 124 { MOD, { 1,0,1,0,0,1,1,0,1,0,1,1,0,0,0,1,1, 125 "%" } }, 126 { PLUS, { 1,0,0,1,0,1,1,0,1,0,0,0,0,0,0,1,0, 127 "+" } }, 128 { MINUS, { 1,0,0,1,0,1,1,0,1,0,0,0,0,0,0,1,0, 129 "-" } }, 130 { SHL, { 1,0,1,0,0,1,1,0,0,0,0,0,1,0,0,1,1, 131 "<<" } }, 132 { SHR, { 1,0,1,0,0,1,1,0,0,0,1,0,1,0,0,1,1, 133 ">>" } }, 134 { LT, { 1,1,0,1,0,1,1,0,1,0,1,1,0,1,1,0,1, 135 "<" } }, 136 { LE, { 1,1,0,1,0,1,1,0,1,0,1,1,0,1,1,0,1, 137 "<=" } }, 138 { GT, { 1,1,0,1,0,1,1,0,1,0,1,1,0,1,1,0,1, 139 ">" } }, 140 { GE, { 1,1,0,1,0,1,1,0,1,0,1,1,0,1,1,0,1, 141 ">=" } }, 142 { EQ, { 1,1,0,1,0,1,1,0,1,0,0,0,0,1,1,0,1, 143 "==" } }, 144 { NE, { 1,1,0,1,0,1,1,0,1,0,0,0,0,1,1,0,1, 145 "!=" } }, 146 { AND, { 1,0,1,0,0,1,1,0,1,0,0,0,1,0,0,1,0, 147 "&" } }, 148 { XOR, { 1,0,1,0,0,1,1,0,1,0,0,0,1,0,0,1,0, 149 "^" } }, 150 { OR, { 1,0,1,0,0,1,1,0,1,0,0,0,1,0,0,1,0, 151 "|" } }, 152 { LOGAND, { 1,1,0,1,0,1,0,1,0,0,0,0,0,0,0,1,0, 153 "&&" } }, 154 { LOGOR, { 1,1,0,1,0,1,0,1,0,0,0,0,1,0,0,1,0, 155 "||" } }, 156 { QUEST, { 1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0, 157 "?" } }, 158 { COLON, { 1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0, 159 ":" } }, 160 { ASSIGN, { 1,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0, 161 "=" } }, 162 { MULASS, { 1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,1,0, 163 "*=" } }, 164 { DIVASS, { 1,0,0,0,1,0,0,0,0,1,0,1,0,0,0,1,0, 165 "/=" } }, 166 { MODASS, { 1,0,1,0,0,0,0,0,0,1,0,1,0,0,0,1,0, 167 "%=" } }, 168 { ADDASS, { 1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0, 169 "+=" } }, 170 { SUBASS, { 1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0, 171 "-=" } }, 172 { SHLASS, { 1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0, 173 "<<=" } }, 174 { SHRASS, { 1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0, 175 ">>=" } }, 176 { ANDASS, { 1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0, 177 "&=" } }, 178 { XORASS, { 1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0, 179 "^=" } }, 180 { ORASS, { 1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0, 181 "|=" } }, 182 { NAME, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 183 "NAME" } }, 184 { CON, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 185 "CON" } }, 186 { STRING, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 187 "STRING" } }, 188 { FSEL, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 189 "FSEL" } }, 190 { CALL, { 1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0, 191 "CALL" } }, 192 { COMMA, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 193 "," } }, 194 { CVT, { 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0, 195 "CVT" } }, 196 { ICALL, { 1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0, 197 "ICALL" } }, 198 { LOAD, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 199 "LOAD" } }, 200 { PUSH, { 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0, 201 "PUSH" } }, 202 { RETURN, { 1,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0, 203 "RETURN" } }, 204 { INIT, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0, 205 "INIT" } }, 206 { FARG, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0, 207 "FARG" } }, 208 { NOOP } 209 }; 210 int i; 211 212 for (i = 0; imods[i].op != NOOP; i++) 213 STRUCT_ASSIGN(modtab[imods[i].op], imods[i].m); 214 } 215 216 /* 217 * Increase degree of reference. 218 * This is most often used to change type "T" in type "pointer to T". 219 */ 220 type_t * 221 incref(type_t *tp, tspec_t t) 222 { 223 type_t *tp2; 224 225 tp2 = getblk(sizeof (type_t)); 226 tp2->t_tspec = t; 227 tp2->t_subt = tp; 228 return (tp2); 229 } 230 231 /* 232 * same for use in expressions 233 */ 234 type_t * 235 tincref(type_t *tp, tspec_t t) 236 { 237 type_t *tp2; 238 239 tp2 = tgetblk(sizeof (type_t)); 240 tp2->t_tspec = t; 241 tp2->t_subt = tp; 242 return (tp2); 243 } 244 245 /* 246 * Create a node for a constant. 247 */ 248 tnode_t * 249 getcnode(type_t *tp, val_t *v) 250 { 251 tnode_t *n; 252 253 n = getnode(); 254 n->tn_op = CON; 255 n->tn_type = tp; 256 n->tn_val = tgetblk(sizeof (val_t)); 257 n->tn_val->v_tspec = tp->t_tspec; 258 n->tn_val->v_ansiu = v->v_ansiu; 259 n->tn_val->v_u = v->v_u; 260 free(v); 261 return (n); 262 } 263 264 /* 265 * Create a node for a integer constant. 266 */ 267 static tnode_t * 268 getinode(tspec_t t, int64_t q) 269 { 270 tnode_t *n; 271 272 n = getnode(); 273 n->tn_op = CON; 274 n->tn_type = gettyp(t); 275 n->tn_val = tgetblk(sizeof (val_t)); 276 n->tn_val->v_tspec = t; 277 n->tn_val->v_quad = q; 278 return (n); 279 } 280 281 /* 282 * Create a node for a name (symbol table entry). 283 * ntok is the token which follows the name. 284 */ 285 tnode_t * 286 getnnode(sym_t *sym, int ntok) 287 { 288 tnode_t *n; 289 290 if (sym->s_scl == NOSCL) { 291 sym->s_scl = EXTERN; 292 sym->s_def = DECL; 293 if (ntok == T_LPARN) { 294 if (sflag) { 295 /* function implicitly declared to ... */ 296 warning(215); 297 } 298 /* 299 * XXX if tflag is set the symbol should be 300 * exported to level 0 301 */ 302 sym->s_type = incref(sym->s_type, FUNC); 303 } else { 304 if (!blklev) { 305 /* %s undefined */ 306 error(99, sym->s_name); 307 } else { 308 int fixtype; 309 if (strcmp(sym->s_name, "__FUNCTION__") == 0) { 310 gnuism(316); 311 fixtype = 1; 312 } else if (strcmp(sym->s_name, "__func__") == 0) { 313 if (!Sflag) 314 warning(317); 315 fixtype = 1; 316 } else { 317 error(99, sym->s_name); 318 fixtype = 0; 319 } 320 if (fixtype) { 321 sym->s_type = incref(gettyp(CHAR), PTR); 322 sym->s_type->t_const = 1; 323 } 324 } 325 } 326 } 327 328 if (sym->s_kind != FVFT && sym->s_kind != FMOS) 329 LERROR("getnnode()"); 330 331 n = getnode(); 332 n->tn_type = sym->s_type; 333 if (sym->s_scl != ENUMCON) { 334 n->tn_op = NAME; 335 n->tn_sym = sym; 336 if (sym->s_kind == FVFT && sym->s_type->t_tspec != FUNC) 337 n->tn_lvalue = 1; 338 } else { 339 n->tn_op = CON; 340 n->tn_val = tgetblk(sizeof (val_t)); 341 *n->tn_val = sym->s_value; 342 } 343 344 return (n); 345 } 346 347 /* 348 * Create a node for a string. 349 */ 350 tnode_t * 351 getsnode(strg_t *strg) 352 { 353 size_t len; 354 tnode_t *n; 355 356 len = strg->st_len; 357 358 n = getnode(); 359 360 n->tn_op = STRING; 361 n->tn_type = tincref(gettyp(strg->st_tspec), ARRAY); 362 n->tn_type->t_dim = len + 1; 363 n->tn_lvalue = 1; 364 365 n->tn_strg = tgetblk(sizeof (strg_t)); 366 n->tn_strg->st_tspec = strg->st_tspec; 367 n->tn_strg->st_len = len; 368 369 if (strg->st_tspec == CHAR) { 370 n->tn_strg->st_cp = tgetblk(len + 1); 371 (void)memcpy(n->tn_strg->st_cp, strg->st_cp, len + 1); 372 free(strg->st_cp); 373 } else { 374 n->tn_strg->st_wcp = tgetblk((len + 1) * sizeof (wchar_t)); 375 (void)memcpy(n->tn_strg->st_wcp, strg->st_wcp, 376 (len + 1) * sizeof (wchar_t)); 377 free(strg->st_wcp); 378 } 379 free(strg); 380 381 return (n); 382 } 383 384 /* 385 * Returns a symbol which has the same name as the msym argument and is a 386 * member of the struct or union specified by the tn argument. 387 */ 388 sym_t * 389 strmemb(tnode_t *tn, op_t op, sym_t *msym) 390 { 391 str_t *str; 392 type_t *tp; 393 sym_t *sym, *csym; 394 int eq; 395 tspec_t t; 396 397 /* 398 * Remove the member if it was unknown until now (Which means 399 * that no defined struct or union has a member with the same name). 400 */ 401 if (msym->s_scl == NOSCL) { 402 /* undefined struct/union member: %s */ 403 error(101, msym->s_name); 404 rmsym(msym); 405 msym->s_kind = FMOS; 406 msym->s_scl = MOS; 407 msym->s_styp = tgetblk(sizeof (str_t)); 408 msym->s_styp->stag = tgetblk(sizeof (sym_t)); 409 msym->s_styp->stag->s_name = unnamed; 410 msym->s_value.v_tspec = INT; 411 return (msym); 412 } 413 414 /* Set str to the tag of which msym is expected to be a member. */ 415 str = NULL; 416 t = (tp = tn->tn_type)->t_tspec; 417 if (op == POINT) { 418 if (t == STRUCT || t == UNION) 419 str = tp->t_str; 420 } else if (op == ARROW && t == PTR) { 421 t = (tp = tp->t_subt)->t_tspec; 422 if (t == STRUCT || t == UNION) 423 str = tp->t_str; 424 } 425 426 /* 427 * If this struct/union has a member with the name of msym, return 428 * return this it. 429 */ 430 if (str != NULL) { 431 for (sym = msym; sym != NULL; sym = sym->s_link) { 432 if (sym->s_scl != MOS && sym->s_scl != MOU) 433 continue; 434 if (sym->s_styp != str) 435 continue; 436 if (strcmp(sym->s_name, msym->s_name) != 0) 437 continue; 438 return (sym); 439 } 440 } 441 442 /* 443 * Set eq to 0 if there are struct/union members with the same name 444 * and different types and/or offsets. 445 */ 446 eq = 1; 447 for (csym = msym; csym != NULL; csym = csym->s_link) { 448 if (csym->s_scl != MOS && csym->s_scl != MOU) 449 continue; 450 if (strcmp(msym->s_name, csym->s_name) != 0) 451 continue; 452 for (sym = csym->s_link ; sym != NULL; sym = sym->s_link) { 453 int w; 454 455 if (sym->s_scl != MOS && sym->s_scl != MOU) 456 continue; 457 if (strcmp(csym->s_name, sym->s_name) != 0) 458 continue; 459 if (csym->s_value.v_quad != sym->s_value.v_quad) { 460 eq = 0; 461 break; 462 } 463 w = 0; 464 eq = eqtype(csym->s_type, sym->s_type, 0, 0, &w) && !w; 465 if (!eq) 466 break; 467 if (csym->s_field != sym->s_field) { 468 eq = 0; 469 break; 470 } 471 if (csym->s_field) { 472 type_t *tp1, *tp2; 473 474 tp1 = csym->s_type; 475 tp2 = sym->s_type; 476 if (tp1->t_flen != tp2->t_flen) { 477 eq = 0; 478 break; 479 } 480 if (tp1->t_foffs != tp2->t_foffs) { 481 eq = 0; 482 break; 483 } 484 } 485 } 486 if (!eq) 487 break; 488 } 489 490 /* 491 * Now handle the case in which the left operand refers really 492 * to a struct/union, but the right operand is not member of it. 493 */ 494 if (str != NULL) { 495 /* illegal member use: %s */ 496 if (eq && tflag) { 497 warning(102, msym->s_name); 498 } else { 499 error(102, msym->s_name); 500 } 501 return (msym); 502 } 503 504 /* 505 * Now the left operand of ARROW does not point to a struct/union 506 * or the left operand of POINT is no struct/union. 507 */ 508 if (eq) { 509 if (op == POINT) { 510 /* left operand of "." must be struct/union object */ 511 if (tflag) { 512 warning(103); 513 } else { 514 error(103); 515 } 516 } else { 517 /* left operand of "->" must be pointer to ... */ 518 if (tflag && tn->tn_type->t_tspec == PTR) { 519 warning(104); 520 } else { 521 error(104); 522 } 523 } 524 } else { 525 if (tflag) { 526 /* non-unique member requires struct/union %s */ 527 error(105, op == POINT ? "object" : "pointer"); 528 } else { 529 /* unacceptable operand of %s */ 530 error(111, modtab[op].m_name); 531 } 532 } 533 534 return (msym); 535 } 536 537 /* 538 * Create a tree node. Called for most operands except function calls, 539 * sizeof and casts. 540 * 541 * op operator 542 * ln left operand 543 * rn if not NULL, right operand 544 */ 545 tnode_t * 546 build(op_t op, tnode_t *ln, tnode_t *rn) 547 { 548 mod_t *mp; 549 tnode_t *ntn; 550 type_t *rtp; 551 552 mp = &modtab[op]; 553 554 /* If there was an error in one of the operands, return. */ 555 if (ln == NULL || (mp->m_binary && rn == NULL)) 556 return (NULL); 557 558 /* 559 * Apply class conversions to the left operand, but only if its 560 * value is needed or it is compaired with null. 561 */ 562 if (mp->m_vctx || mp->m_tctx) 563 ln = cconv(ln); 564 /* 565 * The right operand is almost always in a test or value context, 566 * except if it is a struct or union member. 567 */ 568 if (mp->m_binary && op != ARROW && op != POINT) 569 rn = cconv(rn); 570 571 /* 572 * Print some warnings for comparisons of unsigned values with 573 * constants lower than or equal to null. This must be done 574 * before promote() because otherwise unsigned char and unsigned 575 * short would be promoted to int. Also types are tested to be 576 * CHAR, which would also become int. 577 */ 578 if (mp->m_comp) 579 chkcomp(op, ln, rn); 580 581 /* 582 * Promote the left operand if it is in a test or value context 583 */ 584 if (mp->m_vctx || mp->m_tctx) 585 ln = promote(op, 0, ln); 586 /* 587 * Promote the right operand, but only if it is no struct or 588 * union member, or if it is not to be assigned to the left operand 589 */ 590 if (mp->m_binary && op != ARROW && op != POINT && 591 op != ASSIGN && op != RETURN) { 592 rn = promote(op, 0, rn); 593 } 594 595 /* 596 * If the result of the operation is different for signed or 597 * unsigned operands and one of the operands is signed only in 598 * ANSI C, print a warning. 599 */ 600 if (mp->m_tlansiu && ln->tn_op == CON && ln->tn_val->v_ansiu) { 601 /* ANSI C treats constant as unsigned, op %s */ 602 warning(218, mp->m_name); 603 ln->tn_val->v_ansiu = 0; 604 } 605 if (mp->m_transiu && rn->tn_op == CON && rn->tn_val->v_ansiu) { 606 /* ANSI C treats constant as unsigned, op %s */ 607 warning(218, mp->m_name); 608 rn->tn_val->v_ansiu = 0; 609 } 610 611 /* Make sure both operands are of the same type */ 612 if (mp->m_balance || (tflag && (op == SHL || op == SHR))) 613 balance(op, &ln, &rn); 614 615 /* 616 * Check types for compatibility with the operation and mutual 617 * compatibility. Return if there are serios problems. 618 */ 619 if (!typeok(op, 0, ln, rn)) 620 return (NULL); 621 622 /* And now create the node. */ 623 switch (op) { 624 case POINT: 625 case ARROW: 626 ntn = bldstr(op, ln, rn); 627 break; 628 case INCAFT: 629 case DECAFT: 630 case INCBEF: 631 case DECBEF: 632 ntn = bldincdec(op, ln); 633 break; 634 case AMPER: 635 ntn = bldamper(ln, 0); 636 break; 637 case STAR: 638 ntn = mktnode(STAR, ln->tn_type->t_subt, ln, NULL); 639 break; 640 case PLUS: 641 case MINUS: 642 ntn = bldplmi(op, ln, rn); 643 break; 644 case SHL: 645 case SHR: 646 ntn = bldshft(op, ln, rn); 647 break; 648 case COLON: 649 ntn = bldcol(ln, rn); 650 break; 651 case ASSIGN: 652 case MULASS: 653 case DIVASS: 654 case MODASS: 655 case ADDASS: 656 case SUBASS: 657 case SHLASS: 658 case SHRASS: 659 case ANDASS: 660 case XORASS: 661 case ORASS: 662 case RETURN: 663 ntn = bldasgn(op, ln, rn); 664 break; 665 case COMMA: 666 case QUEST: 667 ntn = mktnode(op, rn->tn_type, ln, rn); 668 break; 669 default: 670 rtp = mp->m_logop ? gettyp(INT) : ln->tn_type; 671 if (!mp->m_binary && rn != NULL) 672 LERROR("build()"); 673 ntn = mktnode(op, rtp, ln, rn); 674 break; 675 } 676 677 /* Return if an error occurred. */ 678 if (ntn == NULL) 679 return (NULL); 680 681 /* Print a warning if precedence confusion is possible */ 682 if (mp->m_tpconf) 683 precconf(ntn); 684 685 /* 686 * Print a warning if one of the operands is in a context where 687 * it is compared with null and if this operand is a constant. 688 */ 689 if (mp->m_tctx) { 690 if (ln->tn_op == CON || 691 ((mp->m_binary && op != QUEST) && rn->tn_op == CON)) { 692 if (hflag && !ccflg) 693 /* constant in conditional context */ 694 warning(161); 695 } 696 } 697 698 /* Fold if the operator requires it */ 699 if (mp->m_fold) { 700 if (ln->tn_op == CON && (!mp->m_binary || rn->tn_op == CON)) { 701 if (mp->m_tctx) { 702 ntn = foldtst(ntn); 703 } else if (isftyp(ntn->tn_type->t_tspec)) { 704 ntn = foldflt(ntn); 705 } else { 706 ntn = fold(ntn); 707 } 708 } else if (op == QUEST && ln->tn_op == CON) { 709 ntn = ln->tn_val->v_quad ? rn->tn_left : rn->tn_right; 710 } 711 } 712 713 return (ntn); 714 } 715 716 /* 717 * Perform class conversions. 718 * 719 * Arrays of type T are converted into pointers to type T. 720 * Functions are converted to pointers to functions. 721 * Lvalues are converted to rvalues. 722 */ 723 tnode_t * 724 cconv(tnode_t *tn) 725 { 726 type_t *tp; 727 728 /* 729 * Array-lvalue (array of type T) is converted into rvalue 730 * (pointer to type T) 731 */ 732 if (tn->tn_type->t_tspec == ARRAY) { 733 if (!tn->tn_lvalue) { 734 /* %soperand of '%s' must be lvalue */ 735 /* XXX print correct operator */ 736 (void)gnuism(114, "", modtab[AMPER].m_name); 737 } 738 tn = mktnode(AMPER, tincref(tn->tn_type->t_subt, PTR), 739 tn, NULL); 740 } 741 742 /* 743 * Expression of type function (function with return value of type T) 744 * in rvalue-expression (pointer to function with return value 745 * of type T) 746 */ 747 if (tn->tn_type->t_tspec == FUNC) 748 tn = bldamper(tn, 1); 749 750 /* lvalue to rvalue */ 751 if (tn->tn_lvalue) { 752 tp = tduptyp(tn->tn_type); 753 tp->t_const = tp->t_volatile = 0; 754 tn = mktnode(LOAD, tp, tn, NULL); 755 } 756 757 return (tn); 758 } 759 760 /* 761 * Perform most type checks. First the types are checked using 762 * informations from modtab[]. After that it is done by hand for 763 * more complicated operators and type combinations. 764 * 765 * If the types are ok, typeok() returns 1, otherwise 0. 766 */ 767 int 768 typeok(op_t op, int arg, tnode_t *ln, tnode_t *rn) 769 { 770 mod_t *mp; 771 tspec_t lt, rt = NOTSPEC, lst = NOTSPEC, rst = NOTSPEC, olt = NOTSPEC, 772 ort = NOTSPEC; 773 type_t *ltp, *rtp = NULL, *lstp = NULL, *rstp = NULL; 774 tnode_t *tn; 775 776 mp = &modtab[op]; 777 778 if ((ltp = ln->tn_type) == NULL) 779 LERROR("typeok()"); 780 781 if ((lt = ltp->t_tspec) == PTR) 782 lst = (lstp = ltp->t_subt)->t_tspec; 783 if (mp->m_binary) { 784 if ((rtp = rn->tn_type) == NULL) 785 LERROR("typeok()"); 786 if ((rt = rtp->t_tspec) == PTR) 787 rst = (rstp = rtp->t_subt)->t_tspec; 788 } 789 790 if (mp->m_rqint) { 791 /* integertypes required */ 792 if (!isityp(lt) || (mp->m_binary && !isityp(rt))) { 793 incompat(op, lt, rt); 794 return (0); 795 } 796 } else if (mp->m_rqsclt) { 797 /* scalar types required */ 798 if (!issclt(lt) || (mp->m_binary && !issclt(rt))) { 799 incompat(op, lt, rt); 800 return (0); 801 } 802 } else if (mp->m_rqatyp) { 803 /* arithmetic types required */ 804 if (!isatyp(lt) || (mp->m_binary && !isatyp(rt))) { 805 incompat(op, lt, rt); 806 return (0); 807 } 808 } 809 810 if (op == SHL || op == SHR || op == SHLASS || op == SHRASS) { 811 /* 812 * For these operations we need the types before promotion 813 * and balancing. 814 */ 815 for (tn=ln; tn->tn_op==CVT && !tn->tn_cast; tn=tn->tn_left) 816 continue; 817 olt = tn->tn_type->t_tspec; 818 for (tn=rn; tn->tn_op==CVT && !tn->tn_cast; tn=tn->tn_left) 819 continue; 820 ort = tn->tn_type->t_tspec; 821 } 822 823 switch (op) { 824 case POINT: 825 /* 826 * Most errors required by ANSI C are reported in strmemb(). 827 * Here we only must check for totaly wrong things. 828 */ 829 if (lt == FUNC || lt == VOID || ltp->t_isfield || 830 ((lt != STRUCT && lt != UNION) && !ln->tn_lvalue)) { 831 /* Without tflag we got already an error */ 832 if (tflag) 833 /* unacceptable operand of %s */ 834 error(111, mp->m_name); 835 return (0); 836 } 837 /* Now we have an object we can create a pointer to */ 838 break; 839 case ARROW: 840 if (lt != PTR && !(tflag && isityp(lt))) { 841 /* Without tflag we got already an error */ 842 if (tflag) 843 /* unacceptabel operand of %s */ 844 error(111, mp->m_name); 845 return (0); 846 } 847 break; 848 case INCAFT: 849 case DECAFT: 850 case INCBEF: 851 case DECBEF: 852 /* operands have scalar types (checked above) */ 853 if (!ln->tn_lvalue) { 854 if (ln->tn_op == CVT && ln->tn_cast && 855 ln->tn_left->tn_op == LOAD) { 856 /* a cast to non-ptr does not yield an lvalue */ 857 if (ln->tn_type->t_tspec == PTR) 858 break; 859 error(163); 860 } 861 /* %soperand of %s must be lvalue */ 862 error(114, "", mp->m_name); 863 return (0); 864 } else if (ltp->t_const) { 865 /* %soperand of %s must be modifiable lvalue */ 866 if (!tflag) 867 warning(115, "", mp->m_name); 868 } 869 break; 870 case AMPER: 871 if (lt == ARRAY || lt == FUNC) { 872 /* ok, a warning comes later (in bldamper()) */ 873 } else if (!ln->tn_lvalue) { 874 if (ln->tn_op == CVT && ln->tn_cast && 875 ln->tn_left->tn_op == LOAD) { 876 /* a cast to non-ptr does not yield an lvalue */ 877 if (ln->tn_type->t_tspec == PTR) 878 break; 879 error(163); 880 } 881 /* %soperand of %s must be lvalue */ 882 error(114, "", mp->m_name); 883 return (0); 884 } else if (issclt(lt)) { 885 if (ltp->t_isfield) { 886 /* cannot take address of bit-field */ 887 error(112); 888 return (0); 889 } 890 } else if (lt != STRUCT && lt != UNION) { 891 /* unacceptable operand of %s */ 892 error(111, mp->m_name); 893 return (0); 894 } 895 if (ln->tn_op == NAME && ln->tn_sym->s_reg) { 896 /* cannot take address of register %s */ 897 error(113, ln->tn_sym->s_name); 898 return (0); 899 } 900 break; 901 case STAR: 902 /* until now there were no type checks for this operator */ 903 if (lt != PTR) { 904 /* cannot dereference non-pointer type */ 905 error(96); 906 return (0); 907 } 908 break; 909 case PLUS: 910 /* operands have scalar types (checked above) */ 911 if ((lt == PTR && !isityp(rt)) || (rt == PTR && !isityp(lt))) { 912 incompat(op, lt, rt); 913 return (0); 914 } 915 break; 916 case MINUS: 917 /* operands have scalar types (checked above) */ 918 if (lt == PTR && (!isityp(rt) && rt != PTR)) { 919 incompat(op, lt, rt); 920 return (0); 921 } else if (rt == PTR && lt != PTR) { 922 incompat(op, lt, rt); 923 return (0); 924 } 925 if (lt == PTR && rt == PTR) { 926 if (!eqtype(lstp, rstp, 1, 0, NULL)) { 927 /* illegal pointer subtraction */ 928 error(116); 929 } 930 } 931 break; 932 case SHR: 933 /* operands have integer types (checked above) */ 934 if (pflag && !isutyp(lt)) { 935 /* 936 * The left operand is signed. This means that 937 * the operation is (possibly) nonportable. 938 */ 939 /* bitwise operation on signed value nonportable */ 940 if (ln->tn_op != CON) { 941 /* possibly nonportable */ 942 warning(117); 943 } else if (ln->tn_val->v_quad < 0) { 944 warning(120); 945 } 946 } else if (!tflag && !sflag && !isutyp(olt) && isutyp(ort)) { 947 /* 948 * The left operand would become unsigned in 949 * traditional C. 950 */ 951 if (hflag && 952 (ln->tn_op != CON || ln->tn_val->v_quad < 0)) { 953 /* semantics of %s change in ANSI C; use ... */ 954 warning(118, mp->m_name); 955 } 956 } else if (!tflag && !sflag && !isutyp(olt) && !isutyp(ort) && 957 psize(lt) < psize(rt)) { 958 /* 959 * In traditional C the left operand would be extended, 960 * possibly with 1, and then shifted. 961 */ 962 if (hflag && 963 (ln->tn_op != CON || ln->tn_val->v_quad < 0)) { 964 /* semantics of %s change in ANSI C; use ... */ 965 warning(118, mp->m_name); 966 } 967 } 968 goto shift; 969 case SHL: 970 /* 971 * ANSI C does not perform balancing for shift operations, 972 * but traditional C does. If the width of the right operand 973 * is greather than the width of the left operand, than in 974 * traditional C the left operand would be extendet to the 975 * width of the right operand. For SHL this may result in 976 * different results. 977 */ 978 if (psize(lt) < psize(rt)) { 979 /* 980 * XXX If both operands are constant make sure 981 * that there is really a differencs between 982 * ANSI C and traditional C. 983 */ 984 if (hflag) 985 /* semantics of %s change in ANSI C; use ... */ 986 warning(118, mp->m_name); 987 } 988 shift: 989 if (rn->tn_op == CON) { 990 if (!isutyp(rt) && rn->tn_val->v_quad < 0) { 991 /* negative shift */ 992 warning(121); 993 } else if ((uint64_t)rn->tn_val->v_quad == size(lt)) { 994 /* shift equal to size fo object */ 995 warning(267); 996 } else if ((uint64_t)rn->tn_val->v_quad > size(lt)) { 997 /* shift greater than size of object */ 998 warning(122); 999 } 1000 } 1001 break; 1002 case EQ: 1003 case NE: 1004 /* 1005 * Accept some things which are allowed with EQ and NE, 1006 * but not with ordered comparisons. 1007 */ 1008 if (lt == PTR && ((rt == PTR && rst == VOID) || isityp(rt))) { 1009 if (rn->tn_op == CON && rn->tn_val->v_quad == 0) 1010 break; 1011 } 1012 if (rt == PTR && ((lt == PTR && lst == VOID) || isityp(lt))) { 1013 if (ln->tn_op == CON && ln->tn_val->v_quad == 0) 1014 break; 1015 } 1016 /* FALLTHROUGH */ 1017 case LT: 1018 case GT: 1019 case LE: 1020 case GE: 1021 if ((lt == PTR || rt == PTR) && lt != rt) { 1022 if (isityp(lt) || isityp(rt)) { 1023 /* illegal comb. of pointer and int., op %s */ 1024 warning(123, mp->m_name); 1025 } else { 1026 incompat(op, lt, rt); 1027 return (0); 1028 } 1029 } else if (lt == PTR && rt == PTR) { 1030 ptrcmpok(op, ln, rn); 1031 } 1032 break; 1033 case QUEST: 1034 if (!issclt(lt)) { 1035 /* first operand must have scalar type, op ? : */ 1036 error(170); 1037 return (0); 1038 } 1039 while (rn->tn_op == CVT) 1040 rn = rn->tn_left; 1041 if (rn->tn_op != COLON) 1042 LERROR("typeok()"); 1043 break; 1044 case COLON: 1045 1046 if (isatyp(lt) && isatyp(rt)) 1047 break; 1048 1049 if (lt == STRUCT && rt == STRUCT && ltp->t_str == rtp->t_str) 1050 break; 1051 if (lt == UNION && rt == UNION && ltp->t_str == rtp->t_str) 1052 break; 1053 1054 /* combination of any pointer and 0, 0L or (void *)0 is ok */ 1055 if (lt == PTR && ((rt == PTR && rst == VOID) || isityp(rt))) { 1056 if (rn->tn_op == CON && rn->tn_val->v_quad == 0) 1057 break; 1058 } 1059 if (rt == PTR && ((lt == PTR && lst == VOID) || isityp(lt))) { 1060 if (ln->tn_op == CON && ln->tn_val->v_quad == 0) 1061 break; 1062 } 1063 1064 if ((lt == PTR && isityp(rt)) || (isityp(lt) && rt == PTR)) { 1065 /* illegal comb. of ptr. and int., op %s */ 1066 warning(123, mp->m_name); 1067 break; 1068 } 1069 1070 if (lt == VOID || rt == VOID) { 1071 if (lt != VOID || rt != VOID) 1072 /* incompatible types in conditional */ 1073 warning(126); 1074 break; 1075 } 1076 1077 if (lt == PTR && rt == PTR && ((lst == VOID && rst == FUNC) || 1078 (lst == FUNC && rst == VOID))) { 1079 /* (void *)0 handled above */ 1080 if (sflag) 1081 /* ANSI C forbids conv. of %s to %s, op %s */ 1082 warning(305, "function pointer", "'void *'", 1083 mp->m_name); 1084 break; 1085 } 1086 1087 if (rt == PTR && lt == PTR) { 1088 if (!eqtype(lstp, rstp, 1, 0, NULL)) 1089 illptrc(mp, ltp, rtp); 1090 break; 1091 } 1092 1093 /* incompatible types in conditional */ 1094 error(126); 1095 return (0); 1096 1097 case ASSIGN: 1098 case INIT: 1099 case FARG: 1100 case RETURN: 1101 if (!asgntypok(op, arg, ln, rn)) 1102 return (0); 1103 goto assign; 1104 case MULASS: 1105 case DIVASS: 1106 case MODASS: 1107 goto assign; 1108 case ADDASS: 1109 case SUBASS: 1110 /* operands have scalar types (checked above) */ 1111 if ((lt == PTR && !isityp(rt)) || rt == PTR) { 1112 incompat(op, lt, rt); 1113 return (0); 1114 } 1115 goto assign; 1116 case SHLASS: 1117 goto assign; 1118 case SHRASS: 1119 if (pflag && !isutyp(lt) && !(tflag && isutyp(rt))) { 1120 /* bitwise operation on s.v. possibly nonportabel */ 1121 warning(117); 1122 } 1123 goto assign; 1124 case ANDASS: 1125 case XORASS: 1126 case ORASS: 1127 goto assign; 1128 assign: 1129 if (!ln->tn_lvalue) { 1130 if (ln->tn_op == CVT && ln->tn_cast && 1131 ln->tn_left->tn_op == LOAD) { 1132 /* a cast to non-ptr does not yield an lvalue */ 1133 if (ln->tn_type->t_tspec == PTR) 1134 break; 1135 error(163); 1136 } 1137 /* %soperand of %s must be lvalue */ 1138 error(114, "left ", mp->m_name); 1139 return (0); 1140 } else if (ltp->t_const || ((lt == STRUCT || lt == UNION) && 1141 conmemb(ltp))) { 1142 /* %soperand of %s must be modifiable lvalue */ 1143 if (!tflag) 1144 warning(115, "left ", mp->m_name); 1145 } 1146 break; 1147 case COMMA: 1148 if (!modtab[ln->tn_op].m_sideeff) 1149 nulleff(ln); 1150 break; 1151 /* LINTED (enumeration values not handled in switch) */ 1152 case CON: 1153 case CASE: 1154 case PUSH: 1155 case LOAD: 1156 case ICALL: 1157 case CVT: 1158 case CALL: 1159 case FSEL: 1160 case STRING: 1161 case NAME: 1162 case LOGOR: 1163 case LOGAND: 1164 case OR: 1165 case XOR: 1166 case AND: 1167 case MOD: 1168 case DIV: 1169 case MULT: 1170 case UMINUS: 1171 case UPLUS: 1172 case DEC: 1173 case INC: 1174 case COMPL: 1175 case NOT: 1176 case NOOP: 1177 break; 1178 } 1179 1180 if (mp->m_badeop && 1181 (ltp->t_isenum || (mp->m_binary && rtp->t_isenum))) { 1182 chkbeop(op, ln, rn); 1183 } else if (mp->m_enumop && (ltp->t_isenum && rtp->t_isenum)) { 1184 chkeop2(op, arg, ln, rn); 1185 } else if (mp->m_enumop && (ltp->t_isenum || rtp->t_isenum)) { 1186 chkeop1(op, arg, ln, rn); 1187 } 1188 1189 return (1); 1190 } 1191 1192 static void 1193 ptrcmpok(op_t op, tnode_t *ln, tnode_t *rn) 1194 { 1195 type_t *ltp, *rtp; 1196 tspec_t lt, rt; 1197 const char *lts, *rts; 1198 1199 lt = (ltp = ln->tn_type)->t_subt->t_tspec; 1200 rt = (rtp = rn->tn_type)->t_subt->t_tspec; 1201 1202 if (lt == VOID || rt == VOID) { 1203 if (sflag && (lt == FUNC || rt == FUNC)) { 1204 /* (void *)0 already handled in typeok() */ 1205 *(lt == FUNC ? <s : &rts) = "function pointer"; 1206 *(lt == VOID ? <s : &rts) = "'void *'"; 1207 /* ANSI C forbids comparison of %s with %s */ 1208 warning(274, lts, rts); 1209 } 1210 return; 1211 } 1212 1213 if (!eqtype(ltp->t_subt, rtp->t_subt, 1, 0, NULL)) { 1214 illptrc(&modtab[op], ltp, rtp); 1215 return; 1216 } 1217 1218 if (lt == FUNC && rt == FUNC) { 1219 if (sflag && op != EQ && op != NE) 1220 /* ANSI C forbids ordered comp. of func ptr */ 1221 warning(125); 1222 } 1223 } 1224 1225 /* 1226 * Checks type compatibility for ASSIGN, INIT, FARG and RETURN 1227 * and prints warnings/errors if necessary. 1228 * If the types are (almost) compatible, 1 is returned, otherwise 0. 1229 */ 1230 static int 1231 asgntypok(op_t op, int arg, tnode_t *ln, tnode_t *rn) 1232 { 1233 tspec_t lt, rt, lst = NOTSPEC, rst = NOTSPEC; 1234 type_t *ltp, *rtp, *lstp = NULL, *rstp = NULL; 1235 mod_t *mp; 1236 const char *lts, *rts; 1237 1238 if ((lt = (ltp = ln->tn_type)->t_tspec) == PTR) 1239 lst = (lstp = ltp->t_subt)->t_tspec; 1240 if ((rt = (rtp = rn->tn_type)->t_tspec) == PTR) 1241 rst = (rstp = rtp->t_subt)->t_tspec; 1242 mp = &modtab[op]; 1243 1244 if (isatyp(lt) && isatyp(rt)) 1245 return (1); 1246 1247 if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION)) 1248 /* both are struct or union */ 1249 return (ltp->t_str == rtp->t_str); 1250 1251 /* 0, 0L and (void *)0 may be assigned to any pointer */ 1252 if (lt == PTR && ((rt == PTR && rst == VOID) || isityp(rt))) { 1253 if (rn->tn_op == CON && rn->tn_val->v_quad == 0) 1254 return (1); 1255 } 1256 1257 if (lt == PTR && rt == PTR && (lst == VOID || rst == VOID)) { 1258 /* two pointers, at least one pointer to void */ 1259 if (sflag && (lst == FUNC || rst == FUNC)) { 1260 /* comb. of ptr to func and ptr to void */ 1261 *(lst == FUNC ? <s : &rts) = "function pointer"; 1262 *(lst == VOID ? <s : &rts) = "'void *'"; 1263 switch (op) { 1264 case INIT: 1265 case RETURN: 1266 /* ANSI C forbids conversion of %s to %s */ 1267 warning(303, rts, lts); 1268 break; 1269 case FARG: 1270 /* ANSI C forbids conv. of %s to %s, arg #%d */ 1271 warning(304, rts, lts, arg); 1272 break; 1273 default: 1274 /* ANSI C forbids conv. of %s to %s, op %s */ 1275 warning(305, rts, lts, mp->m_name); 1276 break; 1277 } 1278 } 1279 } 1280 1281 if (lt == PTR && rt == PTR && (lst == VOID || rst == VOID || 1282 eqtype(lstp, rstp, 1, 0, NULL))) { 1283 /* compatible pointer types (qualifiers ignored) */ 1284 if (!tflag && 1285 ((!lstp->t_const && rstp->t_const) || 1286 (!lstp->t_volatile && rstp->t_volatile))) { 1287 /* left side has not all qualifiers of right */ 1288 switch (op) { 1289 case INIT: 1290 case RETURN: 1291 /* incompatible pointer types */ 1292 warning(182); 1293 break; 1294 case FARG: 1295 /* argument has incompat. ptr. type, arg #%d */ 1296 warning(153, arg); 1297 break; 1298 default: 1299 /* operands have incompat. ptr. types, op %s */ 1300 warning(128, mp->m_name); 1301 break; 1302 } 1303 } 1304 return (1); 1305 } 1306 1307 if ((lt == PTR && isityp(rt)) || (isityp(lt) && rt == PTR)) { 1308 switch (op) { 1309 case INIT: 1310 case RETURN: 1311 /* illegal combination of pointer and integer */ 1312 warning(183); 1313 break; 1314 case FARG: 1315 /* illegal comb. of ptr. and int., arg #%d */ 1316 warning(154, arg); 1317 break; 1318 default: 1319 /* illegal comb. of ptr. and int., op %s */ 1320 warning(123, mp->m_name); 1321 break; 1322 } 1323 return (1); 1324 } 1325 1326 if (lt == PTR && rt == PTR) { 1327 switch (op) { 1328 case INIT: 1329 case RETURN: 1330 illptrc(NULL, ltp, rtp); 1331 break; 1332 case FARG: 1333 /* argument has incompatible pointer type, arg #%d */ 1334 warning(153, arg); 1335 break; 1336 default: 1337 illptrc(mp, ltp, rtp); 1338 break; 1339 } 1340 return (1); 1341 } 1342 1343 switch (op) { 1344 case INIT: 1345 /* initialisation type mismatch */ 1346 error(185); 1347 break; 1348 case RETURN: 1349 /* return value type mismatch */ 1350 error(211); 1351 break; 1352 case FARG: 1353 /* argument is incompatible with prototype, arg #%d */ 1354 warning(155, arg); 1355 break; 1356 default: 1357 incompat(op, lt, rt); 1358 break; 1359 } 1360 1361 return (0); 1362 } 1363 1364 /* 1365 * Prints a warning if an operator, which should be senseless for an 1366 * enum type, is applied to an enum type. 1367 */ 1368 static void 1369 chkbeop(op_t op, tnode_t *ln, tnode_t *rn) 1370 { 1371 mod_t *mp; 1372 1373 if (!eflag) 1374 return; 1375 1376 mp = &modtab[op]; 1377 1378 if (!(ln->tn_type->t_isenum || 1379 (mp->m_binary && rn->tn_type->t_isenum))) { 1380 return; 1381 } 1382 1383 /* 1384 * Enum as offset to a pointer is an exception (otherwise enums 1385 * could not be used as array indizes). 1386 */ 1387 if (op == PLUS && 1388 ((ln->tn_type->t_isenum && rn->tn_type->t_tspec == PTR) || 1389 (rn->tn_type->t_isenum && ln->tn_type->t_tspec == PTR))) { 1390 return; 1391 } 1392 1393 /* dubious operation on enum, op %s */ 1394 warning(241, mp->m_name); 1395 1396 } 1397 1398 /* 1399 * Prints a warning if an operator is applied to two different enum types. 1400 */ 1401 static void 1402 chkeop2(op_t op, int arg, tnode_t *ln, tnode_t *rn) 1403 { 1404 mod_t *mp; 1405 1406 mp = &modtab[op]; 1407 1408 if (ln->tn_type->t_enum != rn->tn_type->t_enum) { 1409 switch (op) { 1410 case INIT: 1411 /* enum type mismatch in initialisation */ 1412 warning(210); 1413 break; 1414 case FARG: 1415 /* enum type mismatch, arg #%d */ 1416 warning(156, arg); 1417 break; 1418 case RETURN: 1419 /* return value type mismatch */ 1420 warning(211); 1421 break; 1422 default: 1423 /* enum type mismatch, op %s */ 1424 warning(130, mp->m_name); 1425 break; 1426 } 1427 #if 0 1428 } else if (mp->m_comp && op != EQ && op != NE) { 1429 if (eflag) 1430 /* dubious comparisons of enums */ 1431 warning(243, mp->m_name); 1432 #endif 1433 } 1434 } 1435 1436 /* 1437 * Prints a warning if an operator has both enum end other integer 1438 * types. 1439 */ 1440 static void 1441 chkeop1(op_t op, int arg, tnode_t *ln, tnode_t *rn) 1442 { 1443 char lbuf[64], rbuf[64]; 1444 1445 if (!eflag) 1446 return; 1447 1448 switch (op) { 1449 case INIT: 1450 /* 1451 * Initializations with 0 should be allowed. Otherwise, 1452 * we should complain about all uninitialized enums, 1453 * consequently. 1454 */ 1455 if (!rn->tn_type->t_isenum && rn->tn_op == CON && 1456 isityp(rn->tn_type->t_tspec) && rn->tn_val->v_quad == 0) { 1457 return; 1458 } 1459 /* initialisation of '%s' with '%s' */ 1460 warning(277, tyname(lbuf, sizeof(lbuf), ln->tn_type), 1461 tyname(rbuf, sizeof(rbuf), rn->tn_type)); 1462 break; 1463 case FARG: 1464 /* combination of '%s' and '%s', arg #%d */ 1465 warning(278, tyname(lbuf, sizeof(lbuf), ln->tn_type), 1466 tyname(rbuf, sizeof(rbuf), rn->tn_type), arg); 1467 break; 1468 case RETURN: 1469 /* combination of '%s' and '%s' in return */ 1470 warning(279, tyname(lbuf, sizeof(lbuf), ln->tn_type), 1471 tyname(rbuf, sizeof(rbuf), rn->tn_type)); 1472 break; 1473 default: 1474 /* combination of '%s' and %s, op %s */ 1475 warning(242, tyname(lbuf, sizeof(lbuf), ln->tn_type), 1476 tyname(rbuf, sizeof(rbuf), rn->tn_type), 1477 modtab[op].m_name); 1478 break; 1479 } 1480 } 1481 1482 /* 1483 * Build and initialize a new node. 1484 */ 1485 static tnode_t * 1486 mktnode(op_t op, type_t *type, tnode_t *ln, tnode_t *rn) 1487 { 1488 tnode_t *ntn; 1489 tspec_t t; 1490 1491 ntn = getnode(); 1492 1493 ntn->tn_op = op; 1494 ntn->tn_type = type; 1495 ntn->tn_left = ln; 1496 ntn->tn_right = rn; 1497 1498 if (op == STAR || op == FSEL) { 1499 if (ln->tn_type->t_tspec == PTR) { 1500 t = ln->tn_type->t_subt->t_tspec; 1501 if (t != FUNC && t != VOID) 1502 ntn->tn_lvalue = 1; 1503 } else { 1504 LERROR("mktnode()"); 1505 } 1506 } 1507 1508 return (ntn); 1509 } 1510 1511 /* 1512 * Performs usual conversion of operands to (unsigned) int. 1513 * 1514 * If tflag is set or the operand is a function argument with no 1515 * type information (no prototype or variable # of args), convert 1516 * float to double. 1517 */ 1518 tnode_t * 1519 promote(op_t op, int farg, tnode_t *tn) 1520 { 1521 tspec_t t; 1522 type_t *ntp; 1523 int len; 1524 1525 t = tn->tn_type->t_tspec; 1526 1527 if (!isatyp(t)) 1528 return (tn); 1529 1530 if (!tflag) { 1531 /* 1532 * ANSI C requires that the result is always of type INT 1533 * if INT can represent all possible values of the previous 1534 * type. 1535 */ 1536 if (tn->tn_type->t_isfield) { 1537 len = tn->tn_type->t_flen; 1538 if (size(INT) > len) { 1539 t = INT; 1540 } else { 1541 if (size(INT) != len) 1542 LERROR("promote()"); 1543 if (isutyp(t)) { 1544 t = UINT; 1545 } else { 1546 t = INT; 1547 } 1548 } 1549 } else if (t == CHAR || t == UCHAR || t == SCHAR) { 1550 t = (size(CHAR) < size(INT) || t != UCHAR) ? 1551 INT : UINT; 1552 } else if (t == SHORT || t == USHORT) { 1553 t = (size(SHORT) < size(INT) || t == SHORT) ? 1554 INT : UINT; 1555 } else if (t == ENUM) { 1556 t = INT; 1557 } else if (farg && t == FLOAT) { 1558 t = DOUBLE; 1559 } 1560 } else { 1561 /* 1562 * In traditional C, keep unsigned and promote FLOAT 1563 * to DOUBLE. 1564 */ 1565 if (t == UCHAR || t == USHORT) { 1566 t = UINT; 1567 } else if (t == CHAR || t == SCHAR || t == SHORT) { 1568 t = INT; 1569 } else if (t == FLOAT) { 1570 t = DOUBLE; 1571 } else if (t == ENUM) { 1572 t = INT; 1573 } 1574 } 1575 1576 if (t != tn->tn_type->t_tspec) { 1577 ntp = tduptyp(tn->tn_type); 1578 ntp->t_tspec = t; 1579 /* 1580 * Keep t_isenum so we are later able to check compatibility 1581 * of enum types. 1582 */ 1583 tn = convert(op, 0, ntp, tn); 1584 } 1585 1586 return (tn); 1587 } 1588 1589 /* 1590 * Insert conversions which are necessary to give both operands the same 1591 * type. This is done in different ways for traditional C and ANIS C. 1592 */ 1593 static void 1594 balance(op_t op, tnode_t **lnp, tnode_t **rnp) 1595 { 1596 tspec_t lt, rt, t; 1597 int i, u; 1598 type_t *ntp; 1599 static tspec_t tl[] = { 1600 LDOUBLE, DOUBLE, FLOAT, UQUAD, QUAD, ULONG, LONG, UINT, INT, 1601 }; 1602 1603 lt = (*lnp)->tn_type->t_tspec; 1604 rt = (*rnp)->tn_type->t_tspec; 1605 1606 if (!isatyp(lt) || !isatyp(rt)) 1607 return; 1608 1609 if (!tflag) { 1610 if (lt == rt) { 1611 t = lt; 1612 } else if (lt == LDOUBLE || rt == LDOUBLE) { 1613 t = LDOUBLE; 1614 } else if (lt == DOUBLE || rt == DOUBLE) { 1615 t = DOUBLE; 1616 } else if (lt == FLOAT || rt == FLOAT) { 1617 t = FLOAT; 1618 } else { 1619 /* 1620 * If type A has more bits than type B it should 1621 * be able to hold all possible values of type B. 1622 */ 1623 if (size(lt) > size(rt)) { 1624 t = lt; 1625 } else if (size(lt) < size(rt)) { 1626 t = rt; 1627 } else { 1628 for (i = 3; tl[i] != INT; i++) { 1629 if (tl[i] == lt || tl[i] == rt) 1630 break; 1631 } 1632 if ((isutyp(lt) || isutyp(rt)) && 1633 !isutyp(tl[i])) { 1634 i--; 1635 } 1636 t = tl[i]; 1637 } 1638 } 1639 } else { 1640 /* Keep unsigned in traditional C */ 1641 u = isutyp(lt) || isutyp(rt); 1642 for (i = 0; tl[i] != INT; i++) { 1643 if (lt == tl[i] || rt == tl[i]) 1644 break; 1645 } 1646 t = tl[i]; 1647 if (u && isityp(t) && !isutyp(t)) 1648 t = utyp(t); 1649 } 1650 1651 if (t != lt) { 1652 ntp = tduptyp((*lnp)->tn_type); 1653 ntp->t_tspec = t; 1654 *lnp = convert(op, 0, ntp, *lnp); 1655 } 1656 if (t != rt) { 1657 ntp = tduptyp((*rnp)->tn_type); 1658 ntp->t_tspec = t; 1659 *rnp = convert(op, 0, ntp, *rnp); 1660 } 1661 } 1662 1663 /* 1664 * Insert a conversion operator, which converts the type of the node 1665 * to another given type. 1666 * If op is FARG, arg is the number of the argument (used for warnings). 1667 */ 1668 tnode_t * 1669 convert(op_t op, int arg, type_t *tp, tnode_t *tn) 1670 { 1671 tnode_t *ntn; 1672 tspec_t nt, ot, ost = NOTSPEC; 1673 1674 if (tn->tn_lvalue) 1675 LERROR("convert()"); 1676 1677 nt = tp->t_tspec; 1678 if ((ot = tn->tn_type->t_tspec) == PTR) 1679 ost = tn->tn_type->t_subt->t_tspec; 1680 1681 if (!tflag && !sflag && op == FARG) 1682 ptconv(arg, nt, ot, tp, tn); 1683 if (isityp(nt) && isityp(ot)) { 1684 iiconv(op, arg, nt, ot, tp, tn); 1685 } else if (nt == PTR && ((ot == PTR && ost == VOID) || isityp(ot)) && 1686 tn->tn_op == CON && tn->tn_val->v_quad == 0) { 1687 /* 0, 0L and (void *)0 may be assigned to any pointer. */ 1688 } else if (isityp(nt) && ot == PTR) { 1689 piconv(op, nt, tp, tn); 1690 } else if (nt == PTR && ot == PTR) { 1691 ppconv(op, tn, tp); 1692 } 1693 1694 ntn = getnode(); 1695 ntn->tn_op = CVT; 1696 ntn->tn_type = tp; 1697 ntn->tn_cast = op == CVT; 1698 if (tn->tn_op != CON || nt == VOID) { 1699 ntn->tn_left = tn; 1700 } else { 1701 ntn->tn_op = CON; 1702 ntn->tn_val = tgetblk(sizeof (val_t)); 1703 cvtcon(op, arg, ntn->tn_type, ntn->tn_val, tn->tn_val); 1704 } 1705 1706 return (ntn); 1707 } 1708 1709 /* 1710 * Print a warning if a prototype causes a type conversion that is 1711 * different from what would happen to the same argument in the 1712 * absence of a prototype. 1713 * 1714 * Errors/Warnings about illegal type combinations are already printed 1715 * in asgntypok(). 1716 */ 1717 static void 1718 ptconv(int arg, tspec_t nt, tspec_t ot, type_t *tp, tnode_t *tn) 1719 { 1720 tnode_t *ptn; 1721 char buf[64]; 1722 1723 if (!isatyp(nt) || !isatyp(ot)) 1724 return; 1725 1726 /* 1727 * If the type of the formal parameter is char/short, a warning 1728 * would be useless, because functions declared the old style 1729 * can't expect char/short arguments. 1730 */ 1731 if (nt == CHAR || nt == UCHAR || nt == SHORT || nt == USHORT) 1732 return; 1733 1734 /* get default promotion */ 1735 ptn = promote(NOOP, 1, tn); 1736 ot = ptn->tn_type->t_tspec; 1737 1738 /* return if types are the same with and without prototype */ 1739 if (nt == ot || (nt == ENUM && ot == INT)) 1740 return; 1741 1742 if (isftyp(nt) != isftyp(ot) || psize(nt) != psize(ot)) { 1743 /* representation and/or width change */ 1744 if (styp(nt) != SHORT || !isityp(ot) || psize(ot) > psize(INT)) 1745 /* conversion to '%s' due to prototype, arg #%d */ 1746 warning(259, tyname(buf, sizeof(buf), tp), arg); 1747 } else if (hflag) { 1748 /* 1749 * they differ in sign or base type (char, short, int, 1750 * long, long long, float, double, long double) 1751 * 1752 * if they differ only in sign and the argument is a constant 1753 * and the msb of the argument is not set, print no warning 1754 */ 1755 if (ptn->tn_op == CON && isityp(nt) && styp(nt) == styp(ot) && 1756 msb(ptn->tn_val->v_quad, ot, -1) == 0) { 1757 /* ok */ 1758 } else { 1759 /* conversion to '%s' due to prototype, arg #%d */ 1760 warning(259, tyname(buf, sizeof(buf), tp), arg); 1761 } 1762 } 1763 } 1764 1765 /* 1766 * Print warnings for conversions of integer types which my cause 1767 * problems. 1768 */ 1769 /* ARGSUSED */ 1770 static void 1771 iiconv(op_t op, int arg, tspec_t nt, tspec_t ot, type_t *tp, tnode_t *tn) 1772 { 1773 char buf[64]; 1774 if (tn->tn_op == CON) 1775 return; 1776 1777 if (op == CVT) 1778 return; 1779 1780 #if 0 1781 if (psize(nt) > psize(ot) && isutyp(nt) != isutyp(ot)) { 1782 /* conversion to %s may sign-extend incorrectly (, arg #%d) */ 1783 if (aflag && pflag) { 1784 if (op == FARG) { 1785 warning(297, tyname(buf, sizeof(buf), tp), arg); 1786 } else { 1787 warning(131, tyname(buf, sizeof(buf), tp)); 1788 } 1789 } 1790 } 1791 #endif 1792 1793 if (psize(nt) < psize(ot) && 1794 (ot == LONG || ot == ULONG || ot == QUAD || ot == UQUAD || 1795 aflag > 1)) { 1796 /* conversion from '%s' may lose accuracy */ 1797 if (aflag) { 1798 if (op == FARG) { 1799 warning(298, tyname(buf, sizeof(buf), tn->tn_type), arg); 1800 } else { 1801 warning(132, tyname(buf, sizeof(buf), tn->tn_type)); 1802 } 1803 } 1804 } 1805 } 1806 1807 /* 1808 * Print warnings for dubious conversions of pointer to integer. 1809 */ 1810 static void 1811 piconv(op_t op, tspec_t nt, type_t *tp, tnode_t *tn) 1812 { 1813 char buf[64]; 1814 1815 if (tn->tn_op == CON) 1816 return; 1817 1818 if (op != CVT) { 1819 /* We got already an error. */ 1820 return; 1821 } 1822 1823 if (psize(nt) < psize(PTR)) { 1824 if (pflag && size(nt) >= size(PTR)) { 1825 /* conv. of pointer to %s may lose bits */ 1826 warning(134, tyname(buf, sizeof(buf), tp)); 1827 } else { 1828 /* conv. of pointer to %s loses bits */ 1829 warning(133, tyname(buf, sizeof(buf), tp)); 1830 } 1831 } 1832 } 1833 1834 /* 1835 * Print warnings for questionable pointer conversions. 1836 */ 1837 static void 1838 ppconv(op_t op, tnode_t *tn, type_t *tp) 1839 { 1840 tspec_t nt, ot; 1841 const char *nts, *ots; 1842 1843 /* 1844 * We got already an error (pointers of different types 1845 * without a cast) or we will not get a warning. 1846 */ 1847 if (op != CVT) 1848 return; 1849 1850 nt = tp->t_subt->t_tspec; 1851 ot = tn->tn_type->t_subt->t_tspec; 1852 1853 if (nt == VOID || ot == VOID) { 1854 if (sflag && (nt == FUNC || ot == FUNC)) { 1855 /* (void *)0 already handled in convert() */ 1856 *(nt == FUNC ? &nts : &ots) = "function pointer"; 1857 *(nt == VOID ? &nts : &ots) = "'void *'"; 1858 /* ANSI C forbids conversion of %s to %s */ 1859 warning(303, ots, nts); 1860 } 1861 return; 1862 } else if (nt == FUNC && ot == FUNC) { 1863 return; 1864 } else if (nt == FUNC || ot == FUNC) { 1865 /* questionable conversion of function pointer */ 1866 warning(229); 1867 return; 1868 } 1869 1870 if (getbound(tp->t_subt) > getbound(tn->tn_type->t_subt)) { 1871 if (hflag) 1872 /* possible pointer alignment problem */ 1873 warning(135); 1874 } 1875 if (((nt == STRUCT || nt == UNION) && 1876 tp->t_subt->t_str != tn->tn_type->t_subt->t_str) || 1877 psize(nt) != psize(ot)) { 1878 if (cflag) { 1879 /* pointer casts may be troublesome */ 1880 warning(247); 1881 } 1882 } 1883 } 1884 1885 /* 1886 * Converts a typed constant in a constant of another type. 1887 * 1888 * op operator which requires conversion 1889 * arg if op is FARG, # of argument 1890 * tp type in which to convert the constant 1891 * nv new constant 1892 * v old constant 1893 */ 1894 void 1895 cvtcon(op_t op, int arg, type_t *tp, val_t *nv, val_t *v) 1896 { 1897 char lbuf[64], rbuf[64]; 1898 tspec_t ot, nt; 1899 ldbl_t max = 0.0, min = 0.0; 1900 int sz, rchk; 1901 int64_t xmask, xmsk1; 1902 int osz, nsz; 1903 1904 ot = v->v_tspec; 1905 nt = nv->v_tspec = tp->t_tspec; 1906 rchk = 0; 1907 1908 if (ot == FLOAT || ot == DOUBLE || ot == LDOUBLE) { 1909 switch (nt) { 1910 case CHAR: 1911 max = CHAR_MAX; min = CHAR_MIN; break; 1912 case UCHAR: 1913 max = UCHAR_MAX; min = 0; break; 1914 case SCHAR: 1915 max = SCHAR_MAX; min = SCHAR_MIN; break; 1916 case SHORT: 1917 max = SHRT_MAX; min = SHRT_MIN; break; 1918 case USHORT: 1919 max = USHRT_MAX; min = 0; break; 1920 case ENUM: 1921 case INT: 1922 max = INT_MAX; min = INT_MIN; break; 1923 case UINT: 1924 max = (u_int)UINT_MAX; min = 0; break; 1925 case LONG: 1926 max = LONG_MAX; min = LONG_MIN; break; 1927 case ULONG: 1928 max = (u_long)ULONG_MAX; min = 0; break; 1929 case QUAD: 1930 max = QUAD_MAX; min = QUAD_MIN; break; 1931 case UQUAD: 1932 max = (uint64_t)UQUAD_MAX; min = 0; break; 1933 case FLOAT: 1934 max = FLT_MAX; min = -FLT_MAX; break; 1935 case DOUBLE: 1936 max = DBL_MAX; min = -DBL_MAX; break; 1937 case PTR: 1938 /* Got already an error because of float --> ptr */ 1939 case LDOUBLE: 1940 max = LDBL_MAX; min = -LDBL_MAX; break; 1941 default: 1942 LERROR("cvtcon()"); 1943 } 1944 if (v->v_ldbl > max || v->v_ldbl < min) { 1945 if (nt == LDOUBLE) 1946 LERROR("cvtcon()"); 1947 if (op == FARG) { 1948 /* conv. of %s to %s is out of rng., arg #%d */ 1949 warning(295, tyname(lbuf, sizeof(lbuf), 1950 gettyp(ot)), tyname(rbuf, sizeof(rbuf), tp), 1951 arg); 1952 } else { 1953 /* conversion of %s to %s is out of range */ 1954 warning(119, tyname(lbuf, sizeof(lbuf), 1955 gettyp(ot)), 1956 tyname(rbuf, sizeof(rbuf), tp)); 1957 } 1958 v->v_ldbl = v->v_ldbl > 0 ? max : min; 1959 } 1960 if (nt == FLOAT) { 1961 nv->v_ldbl = (float)v->v_ldbl; 1962 } else if (nt == DOUBLE) { 1963 nv->v_ldbl = (double)v->v_ldbl; 1964 } else if (nt == LDOUBLE) { 1965 nv->v_ldbl = v->v_ldbl; 1966 } else { 1967 nv->v_quad = (nt == PTR || isutyp(nt)) ? 1968 (uint64_t)v->v_ldbl : (int64_t)v->v_ldbl; 1969 } 1970 } else { 1971 if (nt == FLOAT) { 1972 nv->v_ldbl = (ot == PTR || isutyp(ot)) ? 1973 (float)(uint64_t)v->v_quad : (float)v->v_quad; 1974 } else if (nt == DOUBLE) { 1975 nv->v_ldbl = (ot == PTR || isutyp(ot)) ? 1976 (double)(uint64_t)v->v_quad : (double)v->v_quad; 1977 } else if (nt == LDOUBLE) { 1978 nv->v_ldbl = (ot == PTR || isutyp(ot)) ? 1979 (ldbl_t)(uint64_t)v->v_quad : (ldbl_t)v->v_quad; 1980 } else { 1981 rchk = 1; /* Check for lost precision. */ 1982 nv->v_quad = v->v_quad; 1983 } 1984 } 1985 1986 if (v->v_ansiu && isftyp(nt)) { 1987 /* ANSI C treats constant as unsigned */ 1988 warning(157); 1989 v->v_ansiu = 0; 1990 } else if (v->v_ansiu && (isityp(nt) && !isutyp(nt) && 1991 psize(nt) > psize(ot))) { 1992 /* ANSI C treats constant as unsigned */ 1993 warning(157); 1994 v->v_ansiu = 0; 1995 } 1996 1997 if (nt != FLOAT && nt != DOUBLE && nt != LDOUBLE) { 1998 sz = tp->t_isfield ? tp->t_flen : size(nt); 1999 nv->v_quad = xsign(nv->v_quad, nt, sz); 2000 } 2001 2002 if (rchk && op != CVT) { 2003 osz = size(ot); 2004 nsz = tp->t_isfield ? tp->t_flen : size(nt); 2005 xmask = qlmasks[nsz] ^ qlmasks[osz]; 2006 xmsk1 = qlmasks[nsz] ^ qlmasks[osz - 1]; 2007 /* 2008 * For bitwise operations we are not interested in the 2009 * value, but in the bits itself. 2010 */ 2011 if (op == ORASS || op == OR || op == XOR) { 2012 /* 2013 * Print a warning if bits which were set are 2014 * lost due to the conversion. 2015 * This can happen with operator ORASS only. 2016 */ 2017 if (nsz < osz && (v->v_quad & xmask) != 0) { 2018 /* constant truncated by conv., op %s */ 2019 warning(306, modtab[op].m_name); 2020 } 2021 } else if (op == ANDASS || op == AND) { 2022 /* 2023 * Print a warning if additional bits are not all 1 2024 * and the most significant bit of the old value is 1, 2025 * or if at least one (but not all) removed bit was 0. 2026 */ 2027 if (nsz > osz && 2028 (nv->v_quad & qbmasks[osz - 1]) != 0 && 2029 (nv->v_quad & xmask) != xmask) { 2030 /* 2031 * extra bits set to 0 in conversion 2032 * of '%s' to '%s', op %s 2033 */ 2034 warning(309, tyname(lbuf, sizeof(lbuf), 2035 gettyp(ot)), tyname(rbuf, sizeof(rbuf), tp), 2036 modtab[op].m_name); 2037 } else if (nsz < osz && 2038 (v->v_quad & xmask) != xmask && 2039 (v->v_quad & xmask) != 0) { 2040 /* const. truncated by conv., op %s */ 2041 warning(306, modtab[op].m_name); 2042 } 2043 } else if ((nt != PTR && isutyp(nt)) && 2044 (ot != PTR && !isutyp(ot)) && v->v_quad < 0) { 2045 if (op == ASSIGN) { 2046 /* assignment of negative constant to ... */ 2047 warning(164); 2048 } else if (op == INIT) { 2049 /* initialisation of unsigned with neg. ... */ 2050 warning(221); 2051 } else if (op == FARG) { 2052 /* conversion of neg. const. to ..., arg #%d */ 2053 warning(296, arg); 2054 } else if (modtab[op].m_comp) { 2055 /* we get this warning already in chkcomp() */ 2056 } else { 2057 /* conversion of negative constant to ... */ 2058 warning(222); 2059 } 2060 } else if (nv->v_quad != v->v_quad && nsz <= osz && 2061 (v->v_quad & xmask) != 0 && 2062 (isutyp(ot) || (v->v_quad & xmsk1) != xmsk1)) { 2063 /* 2064 * Loss of significant bit(s). All truncated bits 2065 * of unsigned types or all truncated bits plus the 2066 * msb of the target for signed types are considered 2067 * to be significant bits. Loss of significant bits 2068 * means that at least on of the bits was set in an 2069 * unsigned type or that at least one, but not all of 2070 * the bits was set in an signed type. 2071 * Loss of significant bits means that it is not 2072 * possible, also not with necessary casts, to convert 2073 * back to the original type. A example for a 2074 * necessary cast is: 2075 * char c; int i; c = 128; 2076 * i = c; ** yields -128 ** 2077 * i = (unsigned char)c; ** yields 128 ** 2078 */ 2079 if (op == ASSIGN && tp->t_isfield) { 2080 /* precision lost in bit-field assignment */ 2081 warning(166); 2082 } else if (op == ASSIGN) { 2083 /* constant truncated by assignment */ 2084 warning(165); 2085 } else if (op == INIT && tp->t_isfield) { 2086 /* bit-field initializer does not fit */ 2087 warning(180); 2088 } else if (op == INIT) { 2089 /* initializer does not fit */ 2090 warning(178); 2091 } else if (op == CASE) { 2092 /* case label affected by conversion */ 2093 warning(196); 2094 } else if (op == FARG) { 2095 /* conv. of %s to %s is out of rng., arg #%d */ 2096 warning(295, tyname(lbuf, sizeof(lbuf), 2097 gettyp(ot)), tyname(rbuf, sizeof(rbuf), tp), 2098 arg); 2099 } else { 2100 /* conversion of %s to %s is out of range */ 2101 warning(119, tyname(lbuf, sizeof(lbuf), 2102 gettyp(ot)), 2103 tyname(rbuf, sizeof(rbuf), tp)); 2104 } 2105 } else if (nv->v_quad != v->v_quad) { 2106 if (op == ASSIGN && tp->t_isfield) { 2107 /* precision lost in bit-field assignment */ 2108 warning(166); 2109 } else if (op == INIT && tp->t_isfield) { 2110 /* bit-field initializer out of range */ 2111 warning(11); 2112 } else if (op == CASE) { 2113 /* case label affected by conversion */ 2114 warning(196); 2115 } else if (op == FARG) { 2116 /* conv. of %s to %s is out of rng., arg #%d */ 2117 warning(295, tyname(lbuf, sizeof(lbuf), 2118 gettyp(ot)), tyname(rbuf, sizeof(rbuf), tp), 2119 arg); 2120 } else { 2121 /* conversion of %s to %s is out of range */ 2122 warning(119, tyname(lbuf, sizeof(lbuf), 2123 gettyp(ot)), 2124 tyname(rbuf, sizeof(rbuf), tp)); 2125 } 2126 } 2127 } 2128 } 2129 2130 /* 2131 * Called if incompatible types were detected. 2132 * Prints a appropriate warning. 2133 */ 2134 static void 2135 incompat(op_t op, tspec_t lt, tspec_t rt) 2136 { 2137 mod_t *mp; 2138 2139 mp = &modtab[op]; 2140 2141 if (lt == VOID || (mp->m_binary && rt == VOID)) { 2142 /* void type illegal in expression */ 2143 error(109); 2144 } else if (op == ASSIGN) { 2145 if ((lt == STRUCT || lt == UNION) && 2146 (rt == STRUCT || rt == UNION)) { 2147 /* assignment of different structures */ 2148 error(240); 2149 } else { 2150 /* assignment type mismatch */ 2151 error(171); 2152 } 2153 } else if (mp->m_binary) { 2154 /* operands of %s have incompatible types */ 2155 error(107, mp->m_name); 2156 } else { 2157 /* operand of %s has incompatible type */ 2158 error(108, mp->m_name); 2159 } 2160 } 2161 2162 /* 2163 * Called if incompatible pointer types are detected. 2164 * Print an appropriate warning. 2165 */ 2166 static void 2167 illptrc(mod_t *mp, type_t *ltp, type_t *rtp) 2168 { 2169 tspec_t lt, rt; 2170 2171 if (ltp->t_tspec != PTR || rtp->t_tspec != PTR) 2172 LERROR("illptrc()"); 2173 2174 lt = ltp->t_subt->t_tspec; 2175 rt = rtp->t_subt->t_tspec; 2176 2177 if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION)) { 2178 if (mp == NULL) { 2179 /* illegal structure pointer combination */ 2180 warning(244); 2181 } else { 2182 /* illegal structure pointer combination, op %s */ 2183 warning(245, mp->m_name); 2184 } 2185 } else { 2186 if (mp == NULL) { 2187 /* illegal pointer combination */ 2188 warning(184); 2189 } else { 2190 /* illegal pointer combination, op %s */ 2191 warning(124, mp->m_name); 2192 } 2193 } 2194 } 2195 2196 /* 2197 * Make sure type (*tpp)->t_subt has at least the qualifiers 2198 * of tp1->t_subt and tp2->t_subt. 2199 */ 2200 static void 2201 mrgqual(type_t **tpp, type_t *tp1, type_t *tp2) 2202 { 2203 2204 if ((*tpp)->t_tspec != PTR || 2205 tp1->t_tspec != PTR || tp2->t_tspec != PTR) { 2206 LERROR("mrgqual()"); 2207 } 2208 2209 if ((*tpp)->t_subt->t_const == 2210 (tp1->t_subt->t_const | tp2->t_subt->t_const) && 2211 (*tpp)->t_subt->t_volatile == 2212 (tp1->t_subt->t_volatile | tp2->t_subt->t_volatile)) { 2213 return; 2214 } 2215 2216 *tpp = tduptyp(*tpp); 2217 (*tpp)->t_subt = tduptyp((*tpp)->t_subt); 2218 (*tpp)->t_subt->t_const = 2219 tp1->t_subt->t_const | tp2->t_subt->t_const; 2220 (*tpp)->t_subt->t_volatile = 2221 tp1->t_subt->t_volatile | tp2->t_subt->t_volatile; 2222 } 2223 2224 /* 2225 * Returns 1 if the given structure or union has a constant member 2226 * (maybe recursively). 2227 */ 2228 static int 2229 conmemb(type_t *tp) 2230 { 2231 sym_t *m; 2232 tspec_t t; 2233 2234 if ((t = tp->t_tspec) != STRUCT && t != UNION) 2235 LERROR("conmemb()"); 2236 for (m = tp->t_str->memb; m != NULL; m = m->s_nxt) { 2237 tp = m->s_type; 2238 if (tp->t_const) 2239 return (1); 2240 if ((t = tp->t_tspec) == STRUCT || t == UNION) { 2241 if (conmemb(m->s_type)) 2242 return (1); 2243 } 2244 } 2245 return (0); 2246 } 2247 2248 const char * 2249 basictyname(tspec_t t) 2250 { 2251 switch (t) { 2252 case CHAR: return "char"; 2253 case UCHAR: return "unsigned char"; 2254 case SCHAR: return "signed char"; 2255 case SHORT: return "short"; 2256 case USHORT: return "unsigned short"; 2257 case INT: return "int"; 2258 case UINT: return "unsigned int"; 2259 case LONG: return "long"; 2260 case ULONG: return "unsigned long"; 2261 case QUAD: return "long long"; 2262 case UQUAD: return "unsigned long long"; 2263 case FLOAT: return "float"; 2264 case DOUBLE: return "double"; 2265 case LDOUBLE: return "long double"; 2266 case PTR: return "pointer"; 2267 case ENUM: return "enum"; 2268 case STRUCT: return "struct"; 2269 case UNION: return "union"; 2270 case FUNC: return "function"; 2271 case ARRAY: return "array"; 2272 default: 2273 LERROR("basictyname()"); 2274 return NULL; 2275 } 2276 } 2277 2278 const char * 2279 tyname(char *buf, size_t bufsiz, type_t *tp) 2280 { 2281 tspec_t t; 2282 const char *s; 2283 char lbuf[64]; 2284 2285 if ((t = tp->t_tspec) == INT && tp->t_isenum) 2286 t = ENUM; 2287 2288 s = basictyname(t); 2289 2290 2291 switch (t) { 2292 case CHAR: 2293 case UCHAR: 2294 case SCHAR: 2295 case SHORT: 2296 case USHORT: 2297 case INT: 2298 case UINT: 2299 case LONG: 2300 case ULONG: 2301 case QUAD: 2302 case UQUAD: 2303 case FLOAT: 2304 case DOUBLE: 2305 case LDOUBLE: 2306 case FUNC: 2307 (void)snprintf(buf, bufsiz, "%s", s); 2308 break; 2309 case PTR: 2310 (void)snprintf(buf, bufsiz, "%s to %s", s, 2311 tyname(lbuf, sizeof(lbuf), tp->t_subt)); 2312 break; 2313 case ENUM: 2314 (void)snprintf(buf, bufsiz, "%s %s", s, 2315 tp->t_enum->etag->s_name); 2316 break; 2317 case STRUCT: 2318 case UNION: 2319 (void)snprintf(buf, bufsiz, "%s %s", s, 2320 tp->t_str->stag->s_name); 2321 break; 2322 case ARRAY: 2323 (void)snprintf(buf, bufsiz, "%s of %s[%d]", s, 2324 tyname(lbuf, sizeof(lbuf), tp->t_subt), tp->t_dim); 2325 break; 2326 default: 2327 LERROR("tyname()"); 2328 } 2329 return (buf); 2330 } 2331 2332 /* 2333 * Create a new node for one of the operators POINT and ARROW. 2334 */ 2335 static tnode_t * 2336 bldstr(op_t op, tnode_t *ln, tnode_t *rn) 2337 { 2338 tnode_t *ntn, *ctn; 2339 int nolval; 2340 2341 if (rn->tn_op != NAME) 2342 LERROR("bldstr()"); 2343 if (rn->tn_sym->s_value.v_tspec != INT) 2344 LERROR("bldstr()"); 2345 if (rn->tn_sym->s_scl != MOS && rn->tn_sym->s_scl != MOU) 2346 LERROR("bldstr()"); 2347 2348 /* 2349 * Remember if the left operand is an lvalue (structure members 2350 * are lvalues if and only if the structure itself is an lvalue). 2351 */ 2352 nolval = op == POINT && !ln->tn_lvalue; 2353 2354 if (op == POINT) { 2355 ln = bldamper(ln, 1); 2356 } else if (ln->tn_type->t_tspec != PTR) { 2357 if (!tflag || !isityp(ln->tn_type->t_tspec)) 2358 LERROR("bldstr()"); 2359 ln = convert(NOOP, 0, tincref(gettyp(VOID), PTR), ln); 2360 } 2361 2362 #if PTRDIFF_IS_LONG 2363 ctn = getinode(LONG, rn->tn_sym->s_value.v_quad / CHAR_BIT); 2364 #else 2365 ctn = getinode(INT, rn->tn_sym->s_value.v_quad / CHAR_BIT); 2366 #endif 2367 2368 ntn = mktnode(PLUS, tincref(rn->tn_type, PTR), ln, ctn); 2369 if (ln->tn_op == CON) 2370 ntn = fold(ntn); 2371 2372 if (rn->tn_type->t_isfield) { 2373 ntn = mktnode(FSEL, ntn->tn_type->t_subt, ntn, NULL); 2374 } else { 2375 ntn = mktnode(STAR, ntn->tn_type->t_subt, ntn, NULL); 2376 } 2377 2378 if (nolval) 2379 ntn->tn_lvalue = 0; 2380 2381 return (ntn); 2382 } 2383 2384 /* 2385 * Create a node for INCAFT, INCBEF, DECAFT and DECBEF. 2386 */ 2387 static tnode_t * 2388 bldincdec(op_t op, tnode_t *ln) 2389 { 2390 tnode_t *cn, *ntn; 2391 2392 if (ln == NULL) 2393 LERROR("bldincdec()"); 2394 2395 if (ln->tn_type->t_tspec == PTR) { 2396 cn = plength(ln->tn_type); 2397 } else { 2398 cn = getinode(INT, (int64_t)1); 2399 } 2400 ntn = mktnode(op, ln->tn_type, ln, cn); 2401 2402 return (ntn); 2403 } 2404 2405 /* 2406 * Create a tree node for the & operator 2407 */ 2408 static tnode_t * 2409 bldamper(tnode_t *tn, int noign) 2410 { 2411 tnode_t *ntn; 2412 tspec_t t; 2413 2414 if (!noign && ((t = tn->tn_type->t_tspec) == ARRAY || t == FUNC)) { 2415 /* & before array or function: ignored */ 2416 if (tflag) 2417 warning(127); 2418 return (tn); 2419 } 2420 2421 /* eliminate &* */ 2422 if (tn->tn_op == STAR && 2423 tn->tn_left->tn_type->t_tspec == PTR && 2424 tn->tn_left->tn_type->t_subt == tn->tn_type) { 2425 return (tn->tn_left); 2426 } 2427 2428 ntn = mktnode(AMPER, tincref(tn->tn_type, PTR), tn, NULL); 2429 2430 return (ntn); 2431 } 2432 2433 /* 2434 * Create a node for operators PLUS and MINUS. 2435 */ 2436 static tnode_t * 2437 bldplmi(op_t op, tnode_t *ln, tnode_t *rn) 2438 { 2439 tnode_t *ntn, *ctn; 2440 type_t *tp; 2441 2442 /* If pointer and integer, then pointer to the lhs. */ 2443 if (rn->tn_type->t_tspec == PTR && isityp(ln->tn_type->t_tspec)) { 2444 ntn = ln; 2445 ln = rn; 2446 rn = ntn; 2447 } 2448 2449 if (ln->tn_type->t_tspec == PTR && rn->tn_type->t_tspec != PTR) { 2450 2451 if (!isityp(rn->tn_type->t_tspec)) 2452 LERROR("bldplmi()"); 2453 2454 ctn = plength(ln->tn_type); 2455 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec) 2456 rn = convert(NOOP, 0, ctn->tn_type, rn); 2457 rn = mktnode(MULT, rn->tn_type, rn, ctn); 2458 if (rn->tn_left->tn_op == CON) 2459 rn = fold(rn); 2460 ntn = mktnode(op, ln->tn_type, ln, rn); 2461 2462 } else if (rn->tn_type->t_tspec == PTR) { 2463 2464 if (ln->tn_type->t_tspec != PTR || op != MINUS) 2465 LERROR("bldplmi()"); 2466 #if PTRDIFF_IS_LONG 2467 tp = gettyp(LONG); 2468 #else 2469 tp = gettyp(INT); 2470 #endif 2471 ntn = mktnode(op, tp, ln, rn); 2472 if (ln->tn_op == CON && rn->tn_op == CON) 2473 ntn = fold(ntn); 2474 ctn = plength(ln->tn_type); 2475 balance(NOOP, &ntn, &ctn); 2476 ntn = mktnode(DIV, tp, ntn, ctn); 2477 2478 } else { 2479 2480 ntn = mktnode(op, ln->tn_type, ln, rn); 2481 2482 } 2483 return (ntn); 2484 } 2485 2486 /* 2487 * Create a node for operators SHL and SHR. 2488 */ 2489 static tnode_t * 2490 bldshft(op_t op, tnode_t *ln, tnode_t *rn) 2491 { 2492 tspec_t t; 2493 tnode_t *ntn; 2494 2495 if ((t = rn->tn_type->t_tspec) != INT && t != UINT) 2496 rn = convert(CVT, 0, gettyp(INT), rn); 2497 ntn = mktnode(op, ln->tn_type, ln, rn); 2498 return (ntn); 2499 } 2500 2501 /* 2502 * Create a node for COLON. 2503 */ 2504 static tnode_t * 2505 bldcol(tnode_t *ln, tnode_t *rn) 2506 { 2507 tspec_t lt, rt, pdt; 2508 type_t *rtp; 2509 tnode_t *ntn; 2510 2511 lt = ln->tn_type->t_tspec; 2512 rt = rn->tn_type->t_tspec; 2513 #if PTRDIFF_IS_LONG 2514 pdt = LONG; 2515 #else 2516 pdt = INT; 2517 #endif 2518 2519 /* 2520 * Arithmetic types are balanced, all other type combinations 2521 * still need to be handled. 2522 */ 2523 if (isatyp(lt) && isatyp(rt)) { 2524 rtp = ln->tn_type; 2525 } else if (lt == VOID || rt == VOID) { 2526 rtp = gettyp(VOID); 2527 } else if (lt == STRUCT || lt == UNION) { 2528 /* Both types must be identical. */ 2529 if (rt != STRUCT && rt != UNION) 2530 LERROR("bldcol()"); 2531 if (ln->tn_type->t_str != rn->tn_type->t_str) 2532 LERROR("bldcol()"); 2533 if (incompl(ln->tn_type)) { 2534 /* unknown operand size, op %s */ 2535 error(138, modtab[COLON].m_name); 2536 return (NULL); 2537 } 2538 rtp = ln->tn_type; 2539 } else if (lt == PTR && isityp(rt)) { 2540 if (rt != pdt) { 2541 rn = convert(NOOP, 0, gettyp(pdt), rn); 2542 rt = pdt; 2543 } 2544 rtp = ln->tn_type; 2545 } else if (rt == PTR && isityp(lt)) { 2546 if (lt != pdt) { 2547 ln = convert(NOOP, 0, gettyp(pdt), ln); 2548 lt = pdt; 2549 } 2550 rtp = rn->tn_type; 2551 } else if (lt == PTR && ln->tn_type->t_subt->t_tspec == VOID) { 2552 if (rt != PTR) 2553 LERROR("bldcol()"); 2554 rtp = ln->tn_type; 2555 mrgqual(&rtp, ln->tn_type, rn->tn_type); 2556 } else if (rt == PTR && rn->tn_type->t_subt->t_tspec == VOID) { 2557 if (lt != PTR) 2558 LERROR("bldcol()"); 2559 rtp = rn->tn_type; 2560 mrgqual(&rtp, ln->tn_type, rn->tn_type); 2561 } else { 2562 if (lt != PTR || rt != PTR) 2563 LERROR("bldcol()"); 2564 /* 2565 * XXX For now we simply take the left type. This is 2566 * probably wrong, if one type contains a functionprototype 2567 * and the other one, at the same place, only an old style 2568 * declaration. 2569 */ 2570 rtp = ln->tn_type; 2571 mrgqual(&rtp, ln->tn_type, rn->tn_type); 2572 } 2573 2574 ntn = mktnode(COLON, rtp, ln, rn); 2575 2576 return (ntn); 2577 } 2578 2579 /* 2580 * Create a node for an assignment operator (both = and op= ). 2581 */ 2582 static tnode_t * 2583 bldasgn(op_t op, tnode_t *ln, tnode_t *rn) 2584 { 2585 tspec_t lt, rt; 2586 tnode_t *ntn, *ctn; 2587 2588 if (ln == NULL || rn == NULL) 2589 LERROR("bldasgn()"); 2590 2591 lt = ln->tn_type->t_tspec; 2592 rt = rn->tn_type->t_tspec; 2593 2594 if ((op == ADDASS || op == SUBASS) && lt == PTR) { 2595 if (!isityp(rt)) 2596 LERROR("bldasgn()"); 2597 ctn = plength(ln->tn_type); 2598 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec) 2599 rn = convert(NOOP, 0, ctn->tn_type, rn); 2600 rn = mktnode(MULT, rn->tn_type, rn, ctn); 2601 if (rn->tn_left->tn_op == CON) 2602 rn = fold(rn); 2603 } 2604 2605 if ((op == ASSIGN || op == RETURN) && (lt == STRUCT || rt == STRUCT)) { 2606 if (rt != lt || ln->tn_type->t_str != rn->tn_type->t_str) 2607 LERROR("bldasgn()"); 2608 if (incompl(ln->tn_type)) { 2609 if (op == RETURN) { 2610 /* cannot return incomplete type */ 2611 error(212); 2612 } else { 2613 /* unknown operand size, op %s */ 2614 error(138, modtab[op].m_name); 2615 } 2616 return (NULL); 2617 } 2618 } 2619 2620 if (op == SHLASS || op == SHRASS) { 2621 if (rt != INT) { 2622 rn = convert(NOOP, 0, gettyp(INT), rn); 2623 rt = INT; 2624 } 2625 } else { 2626 if (op == ASSIGN || lt != PTR) { 2627 if (lt != rt || 2628 (ln->tn_type->t_isfield && rn->tn_op == CON)) { 2629 rn = convert(op, 0, ln->tn_type, rn); 2630 rt = lt; 2631 } 2632 } 2633 } 2634 2635 ntn = mktnode(op, ln->tn_type, ln, rn); 2636 2637 return (ntn); 2638 } 2639 2640 /* 2641 * Get length of type tp->t_subt. 2642 */ 2643 static tnode_t * 2644 plength(type_t *tp) 2645 { 2646 int elem, elsz; 2647 tspec_t st; 2648 2649 if (tp->t_tspec != PTR) 2650 LERROR("plength()"); 2651 tp = tp->t_subt; 2652 2653 elem = 1; 2654 elsz = 0; 2655 2656 while (tp->t_tspec == ARRAY) { 2657 elem *= tp->t_dim; 2658 tp = tp->t_subt; 2659 } 2660 2661 switch (tp->t_tspec) { 2662 case FUNC: 2663 /* pointer to function is not allowed here */ 2664 error(110); 2665 break; 2666 case VOID: 2667 /* cannot do pointer arithmetic on operand of ... */ 2668 (void)gnuism(136); 2669 break; 2670 case STRUCT: 2671 case UNION: 2672 if ((elsz = tp->t_str->size) == 0) 2673 /* cannot do pointer arithmetic on operand of ... */ 2674 error(136); 2675 break; 2676 case ENUM: 2677 if (incompl(tp)) { 2678 /* cannot do pointer arithmetic on operand of ... */ 2679 warning(136); 2680 } 2681 /* FALLTHROUGH */ 2682 default: 2683 if ((elsz = size(tp->t_tspec)) == 0) { 2684 /* cannot do pointer arithmetic on operand of ... */ 2685 error(136); 2686 } else if (elsz == -1) { 2687 LERROR("plength()"); 2688 } 2689 break; 2690 } 2691 2692 if (elem == 0 && elsz != 0) { 2693 /* cannot do pointer arithmetic on operand of ... */ 2694 error(136); 2695 } 2696 2697 if (elsz == 0) 2698 elsz = CHAR_BIT; 2699 2700 #if PTRDIFF_IS_LONG 2701 st = LONG; 2702 #else 2703 st = INT; 2704 #endif 2705 2706 return (getinode(st, (int64_t)(elem * elsz / CHAR_BIT))); 2707 } 2708 2709 /* 2710 * XXX 2711 * Note: There appear to be a number of bugs in detecting overflow in 2712 * this function. An audit and a set of proper regression tests are needed. 2713 * --Perry Metzger, Nov. 16, 2001 2714 */ 2715 /* 2716 * Do only as much as necessary to compute constant expressions. 2717 * Called only if the operator allows folding and (both) operands 2718 * are constants. 2719 */ 2720 static tnode_t * 2721 fold(tnode_t *tn) 2722 { 2723 val_t *v; 2724 tspec_t t; 2725 int utyp, ovfl; 2726 int64_t sl, sr = 0, q = 0, mask; 2727 uint64_t ul, ur = 0; 2728 tnode_t *cn; 2729 2730 v = xcalloc(1, sizeof (val_t)); 2731 v->v_tspec = t = tn->tn_type->t_tspec; 2732 2733 utyp = t == PTR || isutyp(t); 2734 ul = sl = tn->tn_left->tn_val->v_quad; 2735 if (modtab[tn->tn_op].m_binary) 2736 ur = sr = tn->tn_right->tn_val->v_quad; 2737 2738 mask = qlmasks[size(t)]; 2739 ovfl = 0; 2740 2741 switch (tn->tn_op) { 2742 case UPLUS: 2743 q = sl; 2744 break; 2745 case UMINUS: 2746 q = -sl; 2747 if (msb(q, t, -1) == msb(sl, t, -1)) 2748 ovfl = 1; 2749 break; 2750 case COMPL: 2751 q = ~sl; 2752 break; 2753 case MULT: 2754 if (utyp) { 2755 q = ul * ur; 2756 if (q != (q & mask)) 2757 ovfl = 1; 2758 else if ((ul != 0) && ((q / ul) != ur)) 2759 ovfl = 1; 2760 } else { 2761 q = sl * sr; 2762 if (msb(q, t, -1) != (msb(sl, t, -1) ^ msb(sr, t, -1))) 2763 ovfl = 1; 2764 } 2765 break; 2766 case DIV: 2767 if (sr == 0) { 2768 /* division by 0 */ 2769 error(139); 2770 q = utyp ? UQUAD_MAX : QUAD_MAX; 2771 } else { 2772 q = utyp ? ul / ur : sl / sr; 2773 } 2774 break; 2775 case MOD: 2776 if (sr == 0) { 2777 /* modulus by 0 */ 2778 error(140); 2779 q = 0; 2780 } else { 2781 q = utyp ? ul % ur : sl % sr; 2782 } 2783 break; 2784 case PLUS: 2785 q = utyp ? ul + ur : sl + sr; 2786 if (msb(sl, t, -1) != 0 && msb(sr, t, -1) != 0) { 2787 if (msb(q, t, -1) == 0) 2788 ovfl = 1; 2789 } else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) == 0) { 2790 if (msb(q, t, -1) != 0) 2791 ovfl = 1; 2792 } 2793 break; 2794 case MINUS: 2795 q = utyp ? ul - ur : sl - sr; 2796 if (msb(sl, t, -1) != 0 && msb(sr, t, -1) == 0) { 2797 if (msb(q, t, -1) == 0) 2798 ovfl = 1; 2799 } else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) != 0) { 2800 if (msb(q, t, -1) != 0) 2801 ovfl = 1; 2802 } 2803 break; 2804 case SHL: 2805 q = utyp ? ul << sr : sl << sr; 2806 break; 2807 case SHR: 2808 /* 2809 * The sign must be explicitly extended because 2810 * shifts of signed values are implementation dependent. 2811 */ 2812 q = ul >> sr; 2813 q = xsign(q, t, size(t) - (int)sr); 2814 break; 2815 case LT: 2816 q = utyp ? ul < ur : sl < sr; 2817 break; 2818 case LE: 2819 q = utyp ? ul <= ur : sl <= sr; 2820 break; 2821 case GE: 2822 q = utyp ? ul >= ur : sl >= sr; 2823 break; 2824 case GT: 2825 q = utyp ? ul > ur : sl > sr; 2826 break; 2827 case EQ: 2828 q = utyp ? ul == ur : sl == sr; 2829 break; 2830 case NE: 2831 q = utyp ? ul != ur : sl != sr; 2832 break; 2833 case AND: 2834 q = utyp ? ul & ur : sl & sr; 2835 break; 2836 case XOR: 2837 q = utyp ? ul ^ ur : sl ^ sr; 2838 break; 2839 case OR: 2840 q = utyp ? ul | ur : sl | sr; 2841 break; 2842 default: 2843 LERROR("fold()"); 2844 } 2845 2846 /* XXX does not work for quads. */ 2847 if (ovfl || ((q | mask) != ~(uint64_t)0 && (q & ~mask) != 0)) { 2848 if (hflag) 2849 /* integer overflow detected, op %s */ 2850 warning(141, modtab[tn->tn_op].m_name); 2851 } 2852 2853 v->v_quad = xsign(q, t, -1); 2854 2855 cn = getcnode(tn->tn_type, v); 2856 2857 return (cn); 2858 } 2859 2860 /* 2861 * Same for operators whose operands are compared with 0 (test context). 2862 */ 2863 static tnode_t * 2864 foldtst(tnode_t *tn) 2865 { 2866 int l, r = 0; 2867 val_t *v; 2868 2869 v = xcalloc(1, sizeof (val_t)); 2870 v->v_tspec = tn->tn_type->t_tspec; 2871 if (tn->tn_type->t_tspec != INT) 2872 LERROR("foldtst()"); 2873 2874 if (isftyp(tn->tn_left->tn_type->t_tspec)) { 2875 l = tn->tn_left->tn_val->v_ldbl != 0.0; 2876 } else { 2877 l = tn->tn_left->tn_val->v_quad != 0; 2878 } 2879 2880 if (modtab[tn->tn_op].m_binary) { 2881 if (isftyp(tn->tn_right->tn_type->t_tspec)) { 2882 r = tn->tn_right->tn_val->v_ldbl != 0.0; 2883 } else { 2884 r = tn->tn_right->tn_val->v_quad != 0; 2885 } 2886 } 2887 2888 switch (tn->tn_op) { 2889 case NOT: 2890 if (hflag) 2891 /* constant argument to NOT */ 2892 warning(239); 2893 v->v_quad = !l; 2894 break; 2895 case LOGAND: 2896 v->v_quad = l && r; 2897 break; 2898 case LOGOR: 2899 v->v_quad = l || r; 2900 break; 2901 default: 2902 LERROR("foldtst()"); 2903 } 2904 2905 return (getcnode(tn->tn_type, v)); 2906 } 2907 2908 /* 2909 * Same for operands with floating point type. 2910 */ 2911 static tnode_t * 2912 foldflt(tnode_t *tn) 2913 { 2914 val_t *v; 2915 tspec_t t; 2916 ldbl_t l, r = 0; 2917 2918 v = xcalloc(1, sizeof (val_t)); 2919 v->v_tspec = t = tn->tn_type->t_tspec; 2920 2921 if (!isftyp(t)) 2922 LERROR("foldflt()"); 2923 2924 if (t != tn->tn_left->tn_type->t_tspec) 2925 LERROR("foldflt()"); 2926 if (modtab[tn->tn_op].m_binary && t != tn->tn_right->tn_type->t_tspec) 2927 LERROR("foldflt()"); 2928 2929 l = tn->tn_left->tn_val->v_ldbl; 2930 if (modtab[tn->tn_op].m_binary) 2931 r = tn->tn_right->tn_val->v_ldbl; 2932 2933 switch (tn->tn_op) { 2934 case UPLUS: 2935 v->v_ldbl = l; 2936 break; 2937 case UMINUS: 2938 v->v_ldbl = -l; 2939 break; 2940 case MULT: 2941 v->v_ldbl = l * r; 2942 break; 2943 case DIV: 2944 if (r == 0.0) { 2945 /* division by 0 */ 2946 error(139); 2947 if (t == FLOAT) { 2948 v->v_ldbl = l < 0 ? -FLT_MAX : FLT_MAX; 2949 } else if (t == DOUBLE) { 2950 v->v_ldbl = l < 0 ? -DBL_MAX : DBL_MAX; 2951 } else { 2952 v->v_ldbl = l < 0 ? -LDBL_MAX : LDBL_MAX; 2953 } 2954 } else { 2955 v->v_ldbl = l / r; 2956 } 2957 break; 2958 case PLUS: 2959 v->v_ldbl = l + r; 2960 break; 2961 case MINUS: 2962 v->v_ldbl = l - r; 2963 break; 2964 case LT: 2965 v->v_quad = l < r; 2966 break; 2967 case LE: 2968 v->v_quad = l <= r; 2969 break; 2970 case GE: 2971 v->v_quad = l >= r; 2972 break; 2973 case GT: 2974 v->v_quad = l > r; 2975 break; 2976 case EQ: 2977 v->v_quad = l == r; 2978 break; 2979 case NE: 2980 v->v_quad = l != r; 2981 break; 2982 default: 2983 LERROR("foldflt()"); 2984 } 2985 2986 if (isnan((double)v->v_ldbl)) 2987 LERROR("foldflt()"); 2988 if (!finite((double)v->v_ldbl) || 2989 (t == FLOAT && 2990 (v->v_ldbl > FLT_MAX || v->v_ldbl < -FLT_MAX)) || 2991 (t == DOUBLE && 2992 (v->v_ldbl > DBL_MAX || v->v_ldbl < -DBL_MAX))) { 2993 /* floating point overflow detected, op %s */ 2994 warning(142, modtab[tn->tn_op].m_name); 2995 if (t == FLOAT) { 2996 v->v_ldbl = v->v_ldbl < 0 ? -FLT_MAX : FLT_MAX; 2997 } else if (t == DOUBLE) { 2998 v->v_ldbl = v->v_ldbl < 0 ? -DBL_MAX : DBL_MAX; 2999 } else { 3000 v->v_ldbl = v->v_ldbl < 0 ? -LDBL_MAX: LDBL_MAX; 3001 } 3002 } 3003 3004 return (getcnode(tn->tn_type, v)); 3005 } 3006 3007 /* 3008 * Create a constant node for sizeof. 3009 */ 3010 tnode_t * 3011 bldszof(type_t *tp) 3012 { 3013 int elem, elsz; 3014 tspec_t st; 3015 3016 elem = 1; 3017 while (tp->t_tspec == ARRAY) { 3018 elem *= tp->t_dim; 3019 tp = tp->t_subt; 3020 } 3021 if (elem == 0) { 3022 /* cannot take size of incomplete type */ 3023 error(143); 3024 elem = 1; 3025 } 3026 switch (tp->t_tspec) { 3027 case FUNC: 3028 /* cannot take size of function */ 3029 error(144); 3030 elsz = 1; 3031 break; 3032 case STRUCT: 3033 case UNION: 3034 if (incompl(tp)) { 3035 /* cannot take size of incomplete type */ 3036 error(143); 3037 elsz = 1; 3038 } else { 3039 elsz = tp->t_str->size; 3040 } 3041 break; 3042 case ENUM: 3043 if (incompl(tp)) { 3044 /* cannot take size of incomplete type */ 3045 warning(143); 3046 } 3047 /* FALLTHROUGH */ 3048 default: 3049 if (tp->t_isfield) { 3050 /* cannot take size of bit-field */ 3051 error(145); 3052 } 3053 if (tp->t_tspec == VOID) { 3054 /* cannot take size of void */ 3055 error(146); 3056 elsz = 1; 3057 } else { 3058 elsz = size(tp->t_tspec); 3059 if (elsz <= 0) 3060 LERROR("bldszof()"); 3061 } 3062 break; 3063 } 3064 3065 #if SIZEOF_IS_ULONG 3066 st = ULONG; 3067 #else 3068 st = UINT; 3069 #endif 3070 3071 return (getinode(st, (int64_t)(elem * elsz / CHAR_BIT))); 3072 } 3073 3074 /* 3075 * Type casts. 3076 */ 3077 tnode_t * 3078 cast(tnode_t *tn, type_t *tp) 3079 { 3080 tspec_t nt, ot; 3081 3082 if (tn == NULL) 3083 return (NULL); 3084 3085 tn = cconv(tn); 3086 3087 nt = tp->t_tspec; 3088 ot = tn->tn_type->t_tspec; 3089 3090 if (nt == VOID) { 3091 /* 3092 * XXX ANSI C requires scalar types or void (Plauger&Brodie). 3093 * But this seams really questionable. 3094 */ 3095 } else if (nt == STRUCT || nt == UNION || nt == ARRAY || nt == FUNC) { 3096 /* invalid cast expression */ 3097 error(147); 3098 return (NULL); 3099 } else if (ot == STRUCT || ot == UNION) { 3100 /* invalid cast expression */ 3101 error(147); 3102 return (NULL); 3103 } else if (ot == VOID) { 3104 /* improper cast of void expression */ 3105 error(148); 3106 return (NULL); 3107 } else if (isityp(nt) && issclt(ot)) { 3108 /* ok */ 3109 } else if (isftyp(nt) && isatyp(ot)) { 3110 /* ok */ 3111 } else if (nt == PTR && isityp(ot)) { 3112 /* ok */ 3113 } else if (nt == PTR && ot == PTR) { 3114 if (!tp->t_subt->t_const && tn->tn_type->t_subt->t_const) { 3115 if (hflag) 3116 /* cast discards 'const' from ... */ 3117 warning(275); 3118 } 3119 } else { 3120 /* invalid cast expression */ 3121 error(147); 3122 return (NULL); 3123 } 3124 3125 tn = convert(CVT, 0, tp, tn); 3126 tn->tn_cast = 1; 3127 3128 return (tn); 3129 } 3130 3131 /* 3132 * Create the node for a function argument. 3133 * All necessary conversions and type checks are done in funccall(), because 3134 * in funcarg() we have no information about expected argument types. 3135 */ 3136 tnode_t * 3137 funcarg(tnode_t *args, tnode_t *arg) 3138 { 3139 tnode_t *ntn; 3140 3141 /* 3142 * If there was a serious error in the expression for the argument, 3143 * create a dummy argument so the positions of the remaining arguments 3144 * will not change. 3145 */ 3146 if (arg == NULL) 3147 arg = getinode(INT, (int64_t)0); 3148 3149 ntn = mktnode(PUSH, arg->tn_type, arg, args); 3150 3151 return (ntn); 3152 } 3153 3154 /* 3155 * Create the node for a function call. Also check types of 3156 * function arguments and insert conversions, if necessary. 3157 */ 3158 tnode_t * 3159 funccall(tnode_t *func, tnode_t *args) 3160 { 3161 tnode_t *ntn; 3162 op_t fcop; 3163 3164 if (func == NULL) 3165 return (NULL); 3166 3167 if (func->tn_op == NAME && func->tn_type->t_tspec == FUNC) { 3168 fcop = CALL; 3169 } else { 3170 fcop = ICALL; 3171 } 3172 3173 /* 3174 * after cconv() func will always be a pointer to a function 3175 * if it is a valid function designator. 3176 */ 3177 func = cconv(func); 3178 3179 if (func->tn_type->t_tspec != PTR || 3180 func->tn_type->t_subt->t_tspec != FUNC) { 3181 /* illegal function */ 3182 error(149); 3183 return (NULL); 3184 } 3185 3186 args = chkfarg(func->tn_type->t_subt, args); 3187 3188 ntn = mktnode(fcop, func->tn_type->t_subt->t_subt, func, args); 3189 3190 return (ntn); 3191 } 3192 3193 /* 3194 * Check types of all function arguments and insert conversions, 3195 * if necessary. 3196 */ 3197 static tnode_t * 3198 chkfarg(type_t *ftp, tnode_t *args) 3199 { 3200 tnode_t *arg; 3201 sym_t *asym; 3202 tspec_t at; 3203 int narg, npar, n, i; 3204 3205 /* get # of args in the prototype */ 3206 npar = 0; 3207 for (asym = ftp->t_args; asym != NULL; asym = asym->s_nxt) 3208 npar++; 3209 3210 /* get # of args in function call */ 3211 narg = 0; 3212 for (arg = args; arg != NULL; arg = arg->tn_right) 3213 narg++; 3214 3215 asym = ftp->t_args; 3216 if (ftp->t_proto && npar != narg && !(ftp->t_vararg && npar < narg)) { 3217 /* argument mismatch: %d arg%s passed, %d expected */ 3218 error(150, narg, narg > 1 ? "s" : "", npar); 3219 asym = NULL; 3220 } 3221 3222 for (n = 1; n <= narg; n++) { 3223 3224 /* 3225 * The rightmost argument is at the top of the argument 3226 * subtree. 3227 */ 3228 for (i = narg, arg = args; i > n; i--, arg = arg->tn_right) 3229 continue; 3230 3231 /* some things which are always not allowd */ 3232 if ((at = arg->tn_left->tn_type->t_tspec) == VOID) { 3233 /* void expressions may not be arguments, arg #%d */ 3234 error(151, n); 3235 return (NULL); 3236 } else if ((at == STRUCT || at == UNION) && 3237 incompl(arg->tn_left->tn_type)) { 3238 /* argument cannot have unknown size, arg #%d */ 3239 error(152, n); 3240 return (NULL); 3241 } else if (isityp(at) && arg->tn_left->tn_type->t_isenum && 3242 incompl(arg->tn_left->tn_type)) { 3243 /* argument cannot have unknown size, arg #%d */ 3244 warning(152, n); 3245 } 3246 3247 /* class conversions (arg in value context) */ 3248 arg->tn_left = cconv(arg->tn_left); 3249 3250 if (asym != NULL) { 3251 arg->tn_left = parg(n, asym->s_type, arg->tn_left); 3252 } else { 3253 arg->tn_left = promote(NOOP, 1, arg->tn_left); 3254 } 3255 arg->tn_type = arg->tn_left->tn_type; 3256 3257 if (asym != NULL) 3258 asym = asym->s_nxt; 3259 } 3260 3261 return (args); 3262 } 3263 3264 /* 3265 * Compare the type of an argument with the corresponding type of a 3266 * prototype parameter. If it is a valid combination, but both types 3267 * are not the same, insert a conversion to convert the argument into 3268 * the type of the parameter. 3269 */ 3270 static tnode_t * 3271 parg( int n, /* pos of arg */ 3272 type_t *tp, /* expected type (from prototype) */ 3273 tnode_t *tn) /* argument */ 3274 { 3275 tnode_t *ln; 3276 int warn; 3277 3278 ln = xcalloc(1, sizeof (tnode_t)); 3279 ln->tn_type = tduptyp(tp); 3280 ln->tn_type->t_const = 0; 3281 ln->tn_lvalue = 1; 3282 if (typeok(FARG, n, ln, tn)) { 3283 if (!eqtype(tp, tn->tn_type, 1, 0, (warn = 0, &warn)) || warn) 3284 tn = convert(FARG, n, tp, tn); 3285 } 3286 free(ln); 3287 return (tn); 3288 } 3289 3290 /* 3291 * Return the value of an integral constant expression. 3292 * If the expression is not constant or its type is not an integer 3293 * type, an error message is printed. 3294 */ 3295 val_t * 3296 constant(tnode_t *tn, int required) 3297 { 3298 val_t *v; 3299 3300 if (tn != NULL) 3301 tn = cconv(tn); 3302 if (tn != NULL) 3303 tn = promote(NOOP, 0, tn); 3304 3305 v = xcalloc(1, sizeof (val_t)); 3306 3307 if (tn == NULL) { 3308 if (nerr == 0) 3309 LERROR("constant()"); 3310 v->v_tspec = INT; 3311 v->v_quad = 1; 3312 return (v); 3313 } 3314 3315 v->v_tspec = tn->tn_type->t_tspec; 3316 3317 if (tn->tn_op == CON) { 3318 if (tn->tn_type->t_tspec != tn->tn_val->v_tspec) 3319 LERROR("constant()"); 3320 if (isityp(tn->tn_val->v_tspec)) { 3321 v->v_ansiu = tn->tn_val->v_ansiu; 3322 v->v_quad = tn->tn_val->v_quad; 3323 return (v); 3324 } 3325 v->v_quad = tn->tn_val->v_ldbl; 3326 } else { 3327 v->v_quad = 1; 3328 } 3329 3330 /* integral constant expression expected */ 3331 if (required) 3332 error(55); 3333 else 3334 gnuism(318); 3335 3336 if (!isityp(v->v_tspec)) 3337 v->v_tspec = INT; 3338 3339 return (v); 3340 } 3341 3342 /* 3343 * Perform some tests on expressions which can't be done in build() and 3344 * functions called by build(). These tests must be done here because 3345 * we need some information about the context in which the operations 3346 * are performed. 3347 * After all tests are performed, expr() frees the memory which is used 3348 * for the expression. 3349 */ 3350 void 3351 expr(tnode_t *tn, int vctx, int tctx, int freeblk) 3352 { 3353 3354 if (tn == NULL && nerr == 0) 3355 LERROR("expr()"); 3356 3357 if (tn == NULL) { 3358 tfreeblk(); 3359 return; 3360 } 3361 3362 /* expr() is also called in global initialisations */ 3363 if (dcs->d_ctx != EXTERN) 3364 chkreach(); 3365 3366 chkmisc(tn, vctx, tctx, !tctx, 0, 0, 0); 3367 if (tn->tn_op == ASSIGN) { 3368 if (hflag && tctx) 3369 /* assignment in conditional context */ 3370 warning(159); 3371 } else if (tn->tn_op == CON) { 3372 if (hflag && tctx && !ccflg) 3373 /* constant in conditional context */ 3374 warning(161); 3375 } 3376 if (!modtab[tn->tn_op].m_sideeff) { 3377 /* 3378 * for left operands of COMMA this warning is already 3379 * printed 3380 */ 3381 if (tn->tn_op != COMMA && !vctx && !tctx) 3382 nulleff(tn); 3383 } 3384 if (dflag) 3385 displexpr(tn, 0); 3386 3387 /* free the tree memory */ 3388 if (freeblk) 3389 tfreeblk(); 3390 } 3391 3392 static void 3393 nulleff(tnode_t *tn) 3394 { 3395 3396 if (!hflag) 3397 return; 3398 3399 while (!modtab[tn->tn_op].m_sideeff) { 3400 if (tn->tn_op == CVT && tn->tn_type->t_tspec == VOID) { 3401 tn = tn->tn_left; 3402 } else if (tn->tn_op == LOGAND || tn->tn_op == LOGOR) { 3403 /* 3404 * && and || have a side effect if the right operand 3405 * has a side effect. 3406 */ 3407 tn = tn->tn_right; 3408 } else if (tn->tn_op == QUEST) { 3409 /* 3410 * ? has a side effect if at least one of its right 3411 * operands has a side effect 3412 */ 3413 tn = tn->tn_right; 3414 } else if (tn->tn_op == COLON || tn->tn_op == COMMA) { 3415 /* 3416 * : has a side effect if at least one of its operands 3417 * has a side effect 3418 */ 3419 if (modtab[tn->tn_left->tn_op].m_sideeff) { 3420 tn = tn->tn_left; 3421 } else if (modtab[tn->tn_right->tn_op].m_sideeff) { 3422 tn = tn->tn_right; 3423 } else { 3424 break; 3425 } 3426 } else { 3427 break; 3428 } 3429 } 3430 if (!modtab[tn->tn_op].m_sideeff) 3431 /* expression has null effect */ 3432 warning(129); 3433 } 3434 3435 /* 3436 * Dump an expression to stdout 3437 * only used for debugging 3438 */ 3439 static void 3440 displexpr(tnode_t *tn, int offs) 3441 { 3442 uint64_t uq; 3443 3444 if (tn == NULL) { 3445 (void)printf("%*s%s\n", offs, "", "NULL"); 3446 return; 3447 } 3448 (void)printf("%*sop %s ", offs, "", modtab[tn->tn_op].m_name); 3449 3450 if (tn->tn_op == NAME) { 3451 (void)printf("%s: %s ", 3452 tn->tn_sym->s_name, scltoa(tn->tn_sym->s_scl)); 3453 } else if (tn->tn_op == CON && isftyp(tn->tn_type->t_tspec)) { 3454 (void)printf("%#g ", (double)tn->tn_val->v_ldbl); 3455 } else if (tn->tn_op == CON && isityp(tn->tn_type->t_tspec)) { 3456 uq = tn->tn_val->v_quad; 3457 (void)printf("0x %08lx %08lx ", (long)(uq >> 32) & 0xffffffffl, 3458 (long)uq & 0xffffffffl); 3459 } else if (tn->tn_op == CON) { 3460 if (tn->tn_type->t_tspec != PTR) 3461 LERROR("displexpr()"); 3462 (void)printf("0x%0*lx ", (int)(sizeof (void *) * CHAR_BIT / 4), 3463 (u_long)tn->tn_val->v_quad); 3464 } else if (tn->tn_op == STRING) { 3465 if (tn->tn_strg->st_tspec == CHAR) { 3466 (void)printf("\"%s\"", tn->tn_strg->st_cp); 3467 } else { 3468 char *s; 3469 size_t n; 3470 n = MB_CUR_MAX * (tn->tn_strg->st_len + 1); 3471 s = xmalloc(n); 3472 (void)wcstombs(s, tn->tn_strg->st_wcp, n); 3473 (void)printf("L\"%s\"", s); 3474 free(s); 3475 } 3476 (void)printf(" "); 3477 } else if (tn->tn_op == FSEL) { 3478 (void)printf("o=%d, l=%d ", tn->tn_type->t_foffs, 3479 tn->tn_type->t_flen); 3480 } 3481 (void)printf("%s\n", ttos(tn->tn_type)); 3482 if (tn->tn_op == NAME || tn->tn_op == CON || tn->tn_op == STRING) 3483 return; 3484 displexpr(tn->tn_left, offs + 2); 3485 if (modtab[tn->tn_op].m_binary || 3486 (tn->tn_op == PUSH && tn->tn_right != NULL)) { 3487 displexpr(tn->tn_right, offs + 2); 3488 } 3489 } 3490 3491 /* 3492 * Called by expr() to recursively perform some tests. 3493 */ 3494 /* ARGSUSED */ 3495 void 3496 chkmisc(tnode_t *tn, int vctx, int tctx, int eqwarn, int fcall, int rvdisc, 3497 int szof) 3498 { 3499 tnode_t *ln, *rn; 3500 mod_t *mp; 3501 int nrvdisc, cvctx, ctctx; 3502 op_t op; 3503 scl_t sc; 3504 dinfo_t *di; 3505 3506 if (tn == NULL) 3507 return; 3508 3509 ln = tn->tn_left; 3510 rn = tn->tn_right; 3511 mp = &modtab[op = tn->tn_op]; 3512 3513 switch (op) { 3514 case AMPER: 3515 if (ln->tn_op == NAME && (reached || rchflg)) { 3516 if (!szof) 3517 setsflg(ln->tn_sym); 3518 setuflg(ln->tn_sym, fcall, szof); 3519 } 3520 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS) 3521 /* check the range of array indices */ 3522 chkaidx(ln->tn_left, 1); 3523 break; 3524 case LOAD: 3525 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS) 3526 /* check the range of array indices */ 3527 chkaidx(ln->tn_left, 0); 3528 /* FALLTHROUGH */ 3529 case PUSH: 3530 case INCBEF: 3531 case DECBEF: 3532 case INCAFT: 3533 case DECAFT: 3534 case ADDASS: 3535 case SUBASS: 3536 case MULASS: 3537 case DIVASS: 3538 case MODASS: 3539 case ANDASS: 3540 case ORASS: 3541 case XORASS: 3542 case SHLASS: 3543 case SHRASS: 3544 if (ln->tn_op == NAME && (reached || rchflg)) { 3545 sc = ln->tn_sym->s_scl; 3546 /* 3547 * Look if there was a asm statement in one of the 3548 * compound statements we are in. If not, we don't 3549 * print a warning. 3550 */ 3551 for (di = dcs; di != NULL; di = di->d_nxt) { 3552 if (di->d_asm) 3553 break; 3554 } 3555 if (sc != EXTERN && sc != STATIC && 3556 !ln->tn_sym->s_set && !szof && di == NULL) { 3557 /* %s may be used before set */ 3558 warning(158, ln->tn_sym->s_name); 3559 setsflg(ln->tn_sym); 3560 } 3561 setuflg(ln->tn_sym, 0, 0); 3562 } 3563 break; 3564 case ASSIGN: 3565 if (ln->tn_op == NAME && !szof && (reached || rchflg)) { 3566 setsflg(ln->tn_sym); 3567 if (ln->tn_sym->s_scl == EXTERN) 3568 outusg(ln->tn_sym); 3569 } 3570 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS) 3571 /* check the range of array indices */ 3572 chkaidx(ln->tn_left, 0); 3573 break; 3574 case CALL: 3575 if (ln->tn_op != AMPER || ln->tn_left->tn_op != NAME) 3576 LERROR("chkmisc()"); 3577 if (!szof) 3578 outcall(tn, vctx || tctx, rvdisc); 3579 break; 3580 case EQ: 3581 /* equality operator "==" found where "=" was exp. */ 3582 if (hflag && eqwarn) 3583 warning(160); 3584 break; 3585 case CON: 3586 case NAME: 3587 case STRING: 3588 return; 3589 /* LINTED (enumeration values not handled in switch) */ 3590 case OR: 3591 case XOR: 3592 case NE: 3593 case GE: 3594 case GT: 3595 case LE: 3596 case LT: 3597 case SHR: 3598 case SHL: 3599 case MINUS: 3600 case PLUS: 3601 case MOD: 3602 case DIV: 3603 case MULT: 3604 case STAR: 3605 case UMINUS: 3606 case UPLUS: 3607 case DEC: 3608 case INC: 3609 case COMPL: 3610 case NOT: 3611 case POINT: 3612 case ARROW: 3613 case NOOP: 3614 case AND: 3615 case FARG: 3616 case CASE: 3617 case INIT: 3618 case RETURN: 3619 case ICALL: 3620 case CVT: 3621 case COMMA: 3622 case FSEL: 3623 case COLON: 3624 case QUEST: 3625 case LOGOR: 3626 case LOGAND: 3627 break; 3628 } 3629 3630 cvctx = mp->m_vctx; 3631 ctctx = mp->m_tctx; 3632 /* 3633 * values of operands of ':' are not used if the type of at least 3634 * one of the operands (for gcc compatibility) is void 3635 * XXX test/value context of QUEST should probably be used as 3636 * context for both operands of COLON 3637 */ 3638 if (op == COLON && tn->tn_type->t_tspec == VOID) 3639 cvctx = ctctx = 0; 3640 nrvdisc = op == CVT && tn->tn_type->t_tspec == VOID; 3641 chkmisc(ln, cvctx, ctctx, mp->m_eqwarn, op == CALL, nrvdisc, szof); 3642 3643 switch (op) { 3644 case PUSH: 3645 if (rn != NULL) 3646 chkmisc(rn, 0, 0, mp->m_eqwarn, 0, 0, szof); 3647 break; 3648 case LOGAND: 3649 case LOGOR: 3650 chkmisc(rn, 0, 1, mp->m_eqwarn, 0, 0, szof); 3651 break; 3652 case COLON: 3653 chkmisc(rn, cvctx, ctctx, mp->m_eqwarn, 0, 0, szof); 3654 break; 3655 case COMMA: 3656 chkmisc(rn, vctx, tctx, mp->m_eqwarn, 0, 0, szof); 3657 break; 3658 default: 3659 if (mp->m_binary) 3660 chkmisc(rn, 1, 0, mp->m_eqwarn, 0, 0, szof); 3661 break; 3662 } 3663 3664 } 3665 3666 /* 3667 * Checks the range of array indices, if possible. 3668 * amper is set if only the address of the element is used. This 3669 * means that the index is allowd to refere to the first element 3670 * after the array. 3671 */ 3672 static void 3673 chkaidx(tnode_t *tn, int amper) 3674 { 3675 int dim; 3676 tnode_t *ln, *rn; 3677 int elsz; 3678 int64_t con; 3679 3680 ln = tn->tn_left; 3681 rn = tn->tn_right; 3682 3683 /* We can only check constant indices. */ 3684 if (rn->tn_op != CON) 3685 return; 3686 3687 /* Return if the left node does not stem from an array. */ 3688 if (ln->tn_op != AMPER) 3689 return; 3690 if (ln->tn_left->tn_op != STRING && ln->tn_left->tn_op != NAME) 3691 return; 3692 if (ln->tn_left->tn_type->t_tspec != ARRAY) 3693 return; 3694 3695 /* 3696 * For incomplete array types, we can print a warning only if 3697 * the index is negative. 3698 */ 3699 if (incompl(ln->tn_left->tn_type) && rn->tn_val->v_quad >= 0) 3700 return; 3701 3702 /* Get the size of one array element */ 3703 if ((elsz = length(ln->tn_type->t_subt, NULL)) == 0) 3704 return; 3705 elsz /= CHAR_BIT; 3706 3707 /* Change the unit of the index from bytes to element size. */ 3708 if (isutyp(rn->tn_type->t_tspec)) { 3709 con = (uint64_t)rn->tn_val->v_quad / elsz; 3710 } else { 3711 con = rn->tn_val->v_quad / elsz; 3712 } 3713 3714 dim = ln->tn_left->tn_type->t_dim + (amper ? 1 : 0); 3715 3716 if (!isutyp(rn->tn_type->t_tspec) && con < 0) { 3717 /* array subscript cannot be negative: %ld */ 3718 warning(167, (long)con); 3719 } else if (dim > 0 && (uint64_t)con >= dim) { 3720 /* array subscript cannot be > %d: %ld */ 3721 warning(168, dim - 1, (long)con); 3722 } 3723 } 3724 3725 /* 3726 * Check for ordered comparisons of unsigned values with 0. 3727 */ 3728 static void 3729 chkcomp(op_t op, tnode_t *ln, tnode_t *rn) 3730 { 3731 char buf[64]; 3732 tspec_t lt, rt; 3733 mod_t *mp; 3734 3735 lt = ln->tn_type->t_tspec; 3736 rt = rn->tn_type->t_tspec; 3737 mp = &modtab[op]; 3738 3739 if (ln->tn_op != CON && rn->tn_op != CON) 3740 return; 3741 3742 if (!isityp(lt) || !isityp(rt)) 3743 return; 3744 3745 if ((hflag || pflag) && lt == CHAR && rn->tn_op == CON && 3746 (rn->tn_val->v_quad < 0 || 3747 rn->tn_val->v_quad > ~(~0 << (CHAR_BIT - 1)))) { 3748 /* nonportable character comparison, op %s */ 3749 warning(230, mp->m_name); 3750 return; 3751 } 3752 if ((hflag || pflag) && rt == CHAR && ln->tn_op == CON && 3753 (ln->tn_val->v_quad < 0 || 3754 ln->tn_val->v_quad > ~(~0 << (CHAR_BIT - 1)))) { 3755 /* nonportable character comparison, op %s */ 3756 warning(230, mp->m_name); 3757 return; 3758 } 3759 if (isutyp(lt) && !isutyp(rt) && 3760 rn->tn_op == CON && rn->tn_val->v_quad <= 0) { 3761 if (rn->tn_val->v_quad < 0) { 3762 /* comparison of %s with %s, op %s */ 3763 warning(162, tyname(buf, sizeof(buf), ln->tn_type), 3764 "negative constant", mp->m_name); 3765 } else if (op == LT || op == GE || (hflag && op == LE)) { 3766 /* comparison of %s with %s, op %s */ 3767 warning(162, tyname(buf, sizeof(buf), ln->tn_type), 3768 "0", mp->m_name); 3769 } 3770 return; 3771 } 3772 if (isutyp(rt) && !isutyp(lt) && 3773 ln->tn_op == CON && ln->tn_val->v_quad <= 0) { 3774 if (ln->tn_val->v_quad < 0) { 3775 /* comparison of %s with %s, op %s */ 3776 warning(162, "negative constant", 3777 tyname(buf, sizeof(buf), rn->tn_type), mp->m_name); 3778 } else if (op == GT || op == LE || (hflag && op == GE)) { 3779 /* comparison of %s with %s, op %s */ 3780 warning(162, "0", tyname(buf, sizeof(buf), rn->tn_type), 3781 mp->m_name); 3782 } 3783 return; 3784 } 3785 } 3786 3787 /* 3788 * Takes an expression an returns 0 if this expression can be used 3789 * for static initialisation, otherwise -1. 3790 * 3791 * Constant initialisation expressions must be constant or an address 3792 * of a static object with an optional offset. In the first case, 3793 * the result is returned in *offsp. In the second case, the static 3794 * object is returned in *symp and the offset in *offsp. 3795 * 3796 * The expression can consist of PLUS, MINUS, AMPER, NAME, STRING and 3797 * CON. Type conversions are allowed if they do not change binary 3798 * representation (including width). 3799 */ 3800 int 3801 conaddr(tnode_t *tn, sym_t **symp, ptrdiff_t *offsp) 3802 { 3803 sym_t *sym; 3804 ptrdiff_t offs1, offs2; 3805 tspec_t t, ot; 3806 3807 switch (tn->tn_op) { 3808 case MINUS: 3809 if (tn->tn_right->tn_op == CVT) 3810 return conaddr(tn->tn_right, symp, offsp); 3811 else if (tn->tn_right->tn_op != CON) 3812 return (-1); 3813 /* FALLTHROUGH */ 3814 case PLUS: 3815 offs1 = offs2 = 0; 3816 if (tn->tn_left->tn_op == CON) { 3817 offs1 = (ptrdiff_t)tn->tn_left->tn_val->v_quad; 3818 if (conaddr(tn->tn_right, &sym, &offs2) == -1) 3819 return (-1); 3820 } else if (tn->tn_right->tn_op == CON) { 3821 offs2 = (ptrdiff_t)tn->tn_right->tn_val->v_quad; 3822 if (tn->tn_op == MINUS) 3823 offs2 = -offs2; 3824 if (conaddr(tn->tn_left, &sym, &offs1) == -1) 3825 return (-1); 3826 } else { 3827 return (-1); 3828 } 3829 *symp = sym; 3830 *offsp = offs1 + offs2; 3831 break; 3832 case AMPER: 3833 if (tn->tn_left->tn_op == NAME) { 3834 *symp = tn->tn_left->tn_sym; 3835 *offsp = 0; 3836 } else if (tn->tn_left->tn_op == STRING) { 3837 /* 3838 * If this would be the front end of a compiler we 3839 * would return a label instead of 0. 3840 */ 3841 *offsp = 0; 3842 } 3843 break; 3844 case CVT: 3845 t = tn->tn_type->t_tspec; 3846 ot = tn->tn_left->tn_type->t_tspec; 3847 if ((!isityp(t) && t != PTR) || (!isityp(ot) && ot != PTR)) 3848 return (-1); 3849 #ifdef notdef 3850 /* 3851 * consider: 3852 * struct foo { 3853 * unsigned char a; 3854 * } f = { 3855 * (u_char)(u_long)(&(((struct foo *)0)->a)) 3856 * }; 3857 * since psize(u_long) != psize(u_char) this fails. 3858 */ 3859 else if (psize(t) != psize(ot)) 3860 return (-1); 3861 #endif 3862 if (conaddr(tn->tn_left, symp, offsp) == -1) 3863 return (-1); 3864 break; 3865 default: 3866 return (-1); 3867 } 3868 return (0); 3869 } 3870 3871 /* 3872 * Concatenate two string constants. 3873 */ 3874 strg_t * 3875 catstrg(strg_t *strg1, strg_t *strg2) 3876 { 3877 size_t len1, len2, len; 3878 3879 if (strg1->st_tspec != strg2->st_tspec) { 3880 /* cannot concatenate wide and regular string literals */ 3881 error(292); 3882 return (strg1); 3883 } 3884 3885 len = (len1 = strg1->st_len) + (len2 = strg2->st_len); 3886 3887 if (strg1->st_tspec == CHAR) { 3888 strg1->st_cp = xrealloc(strg1->st_cp, len + 1); 3889 (void)memcpy(strg1->st_cp + len1, strg2->st_cp, len2 + 1); 3890 free(strg2->st_cp); 3891 } else { 3892 strg1->st_wcp = xrealloc(strg1->st_wcp, 3893 (len + 1) * sizeof (wchar_t)); 3894 (void)memcpy(strg1->st_wcp + len1, strg2->st_wcp, 3895 (len2 + 1) * sizeof (wchar_t)); 3896 free(strg2->st_wcp); 3897 } 3898 free(strg2); 3899 3900 return (strg1); 3901 } 3902 3903 /* 3904 * Print a warning if the given node has operands which should be 3905 * parenthesized. 3906 * 3907 * XXX Does not work if an operand is a constant expression. Constant 3908 * expressions are already folded. 3909 */ 3910 static void 3911 precconf(tnode_t *tn) 3912 { 3913 tnode_t *ln, *rn; 3914 op_t lop, rop = NOOP; 3915 int lparn, rparn = 0; 3916 mod_t *mp; 3917 int warn; 3918 3919 if (!hflag) 3920 return; 3921 3922 mp = &modtab[tn->tn_op]; 3923 3924 lparn = 0; 3925 for (ln = tn->tn_left; ln->tn_op == CVT; ln = ln->tn_left) 3926 lparn |= ln->tn_parn; 3927 lparn |= ln->tn_parn; 3928 lop = ln->tn_op; 3929 3930 if (mp->m_binary) { 3931 rparn = 0; 3932 for (rn = tn->tn_right; tn->tn_op == CVT; rn = rn->tn_left) 3933 rparn |= rn->tn_parn; 3934 rparn |= rn->tn_parn; 3935 rop = rn->tn_op; 3936 } 3937 3938 warn = 0; 3939 3940 switch (tn->tn_op) { 3941 case SHL: 3942 case SHR: 3943 if (!lparn && (lop == PLUS || lop == MINUS)) { 3944 warn = 1; 3945 } else if (!rparn && (rop == PLUS || rop == MINUS)) { 3946 warn = 1; 3947 } 3948 break; 3949 case LOGOR: 3950 if (!lparn && lop == LOGAND) { 3951 warn = 1; 3952 } else if (!rparn && rop == LOGAND) { 3953 warn = 1; 3954 } 3955 break; 3956 case AND: 3957 case XOR: 3958 case OR: 3959 if (!lparn && lop != tn->tn_op) { 3960 if (lop == PLUS || lop == MINUS) { 3961 warn = 1; 3962 } else if (lop == AND || lop == XOR) { 3963 warn = 1; 3964 } 3965 } 3966 if (!warn && !rparn && rop != tn->tn_op) { 3967 if (rop == PLUS || rop == MINUS) { 3968 warn = 1; 3969 } else if (rop == AND || rop == XOR) { 3970 warn = 1; 3971 } 3972 } 3973 break; 3974 /* LINTED (enumeration values not handled in switch) */ 3975 case DECAFT: 3976 case XORASS: 3977 case SHLASS: 3978 case NOOP: 3979 case ARROW: 3980 case ORASS: 3981 case POINT: 3982 case NAME: 3983 case NOT: 3984 case COMPL: 3985 case CON: 3986 case INC: 3987 case STRING: 3988 case DEC: 3989 case INCBEF: 3990 case DECBEF: 3991 case INCAFT: 3992 case FSEL: 3993 case CALL: 3994 case COMMA: 3995 case CVT: 3996 case ICALL: 3997 case LOAD: 3998 case PUSH: 3999 case RETURN: 4000 case INIT: 4001 case CASE: 4002 case FARG: 4003 case SUBASS: 4004 case ADDASS: 4005 case MODASS: 4006 case DIVASS: 4007 case MULASS: 4008 case ASSIGN: 4009 case COLON: 4010 case QUEST: 4011 case LOGAND: 4012 case NE: 4013 case EQ: 4014 case GE: 4015 case GT: 4016 case LE: 4017 case LT: 4018 case MINUS: 4019 case PLUS: 4020 case MOD: 4021 case DIV: 4022 case MULT: 4023 case AMPER: 4024 case STAR: 4025 case UMINUS: 4026 case SHRASS: 4027 case UPLUS: 4028 case ANDASS: 4029 break; 4030 } 4031 4032 if (warn) { 4033 /* precedence confusion possible: parenthesize! */ 4034 warning(169); 4035 } 4036 4037 } 4038