1 /* $NetBSD: tree.c,v 1.34 2002/10/22 22:50:11 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.34 2002/10/22 22:50:11 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 if (rn->tn_op != COLON) 1040 LERROR("typeok()"); 1041 break; 1042 case COLON: 1043 1044 if (isatyp(lt) && isatyp(rt)) 1045 break; 1046 1047 if (lt == STRUCT && rt == STRUCT && ltp->t_str == rtp->t_str) 1048 break; 1049 if (lt == UNION && rt == UNION && ltp->t_str == rtp->t_str) 1050 break; 1051 1052 /* combination of any pointer and 0, 0L or (void *)0 is ok */ 1053 if (lt == PTR && ((rt == PTR && rst == VOID) || isityp(rt))) { 1054 if (rn->tn_op == CON && rn->tn_val->v_quad == 0) 1055 break; 1056 } 1057 if (rt == PTR && ((lt == PTR && lst == VOID) || isityp(lt))) { 1058 if (ln->tn_op == CON && ln->tn_val->v_quad == 0) 1059 break; 1060 } 1061 1062 if ((lt == PTR && isityp(rt)) || (isityp(lt) && rt == PTR)) { 1063 /* illegal comb. of ptr. and int., op %s */ 1064 warning(123, mp->m_name); 1065 break; 1066 } 1067 1068 if (lt == VOID || rt == VOID) { 1069 if (lt != VOID || rt != VOID) 1070 /* incompatible types in conditional */ 1071 warning(126); 1072 break; 1073 } 1074 1075 if (lt == PTR && rt == PTR && ((lst == VOID && rst == FUNC) || 1076 (lst == FUNC && rst == VOID))) { 1077 /* (void *)0 handled above */ 1078 if (sflag) 1079 /* ANSI C forbids conv. of %s to %s, op %s */ 1080 warning(305, "function pointer", "'void *'", 1081 mp->m_name); 1082 break; 1083 } 1084 1085 if (rt == PTR && lt == PTR) { 1086 if (!eqtype(lstp, rstp, 1, 0, NULL)) 1087 illptrc(mp, ltp, rtp); 1088 break; 1089 } 1090 1091 /* incompatible types in conditional */ 1092 error(126); 1093 return (0); 1094 1095 case ASSIGN: 1096 case INIT: 1097 case FARG: 1098 case RETURN: 1099 if (!asgntypok(op, arg, ln, rn)) 1100 return (0); 1101 goto assign; 1102 case MULASS: 1103 case DIVASS: 1104 case MODASS: 1105 goto assign; 1106 case ADDASS: 1107 case SUBASS: 1108 /* operands have scalar types (checked above) */ 1109 if ((lt == PTR && !isityp(rt)) || rt == PTR) { 1110 incompat(op, lt, rt); 1111 return (0); 1112 } 1113 goto assign; 1114 case SHLASS: 1115 goto assign; 1116 case SHRASS: 1117 if (pflag && !isutyp(lt) && !(tflag && isutyp(rt))) { 1118 /* bitwise operation on s.v. possibly nonportabel */ 1119 warning(117); 1120 } 1121 goto assign; 1122 case ANDASS: 1123 case XORASS: 1124 case ORASS: 1125 goto assign; 1126 assign: 1127 if (!ln->tn_lvalue) { 1128 if (ln->tn_op == CVT && ln->tn_cast && 1129 ln->tn_left->tn_op == LOAD) { 1130 /* a cast to non-ptr does not yield an lvalue */ 1131 if (ln->tn_type->t_tspec == PTR) 1132 break; 1133 error(163); 1134 } 1135 /* %soperand of %s must be lvalue */ 1136 error(114, "left ", mp->m_name); 1137 return (0); 1138 } else if (ltp->t_const || ((lt == STRUCT || lt == UNION) && 1139 conmemb(ltp))) { 1140 /* %soperand of %s must be modifiable lvalue */ 1141 if (!tflag) 1142 warning(115, "left ", mp->m_name); 1143 } 1144 break; 1145 case COMMA: 1146 if (!modtab[ln->tn_op].m_sideeff) 1147 nulleff(ln); 1148 break; 1149 /* LINTED (enumeration values not handled in switch) */ 1150 case CON: 1151 case CASE: 1152 case PUSH: 1153 case LOAD: 1154 case ICALL: 1155 case CVT: 1156 case CALL: 1157 case FSEL: 1158 case STRING: 1159 case NAME: 1160 case LOGOR: 1161 case LOGAND: 1162 case OR: 1163 case XOR: 1164 case AND: 1165 case MOD: 1166 case DIV: 1167 case MULT: 1168 case UMINUS: 1169 case UPLUS: 1170 case DEC: 1171 case INC: 1172 case COMPL: 1173 case NOT: 1174 case NOOP: 1175 break; 1176 } 1177 1178 if (mp->m_badeop && 1179 (ltp->t_isenum || (mp->m_binary && rtp->t_isenum))) { 1180 chkbeop(op, ln, rn); 1181 } else if (mp->m_enumop && (ltp->t_isenum && rtp->t_isenum)) { 1182 chkeop2(op, arg, ln, rn); 1183 } else if (mp->m_enumop && (ltp->t_isenum || rtp->t_isenum)) { 1184 chkeop1(op, arg, ln, rn); 1185 } 1186 1187 return (1); 1188 } 1189 1190 static void 1191 ptrcmpok(op_t op, tnode_t *ln, tnode_t *rn) 1192 { 1193 type_t *ltp, *rtp; 1194 tspec_t lt, rt; 1195 const char *lts, *rts; 1196 1197 lt = (ltp = ln->tn_type)->t_subt->t_tspec; 1198 rt = (rtp = rn->tn_type)->t_subt->t_tspec; 1199 1200 if (lt == VOID || rt == VOID) { 1201 if (sflag && (lt == FUNC || rt == FUNC)) { 1202 /* (void *)0 already handled in typeok() */ 1203 *(lt == FUNC ? <s : &rts) = "function pointer"; 1204 *(lt == VOID ? <s : &rts) = "'void *'"; 1205 /* ANSI C forbids comparison of %s with %s */ 1206 warning(274, lts, rts); 1207 } 1208 return; 1209 } 1210 1211 if (!eqtype(ltp->t_subt, rtp->t_subt, 1, 0, NULL)) { 1212 illptrc(&modtab[op], ltp, rtp); 1213 return; 1214 } 1215 1216 if (lt == FUNC && rt == FUNC) { 1217 if (sflag && op != EQ && op != NE) 1218 /* ANSI C forbids ordered comp. of func ptr */ 1219 warning(125); 1220 } 1221 } 1222 1223 /* 1224 * Checks type compatibility for ASSIGN, INIT, FARG and RETURN 1225 * and prints warnings/errors if necessary. 1226 * If the types are (almost) compatible, 1 is returned, otherwise 0. 1227 */ 1228 static int 1229 asgntypok(op_t op, int arg, tnode_t *ln, tnode_t *rn) 1230 { 1231 tspec_t lt, rt, lst = NOTSPEC, rst = NOTSPEC; 1232 type_t *ltp, *rtp, *lstp = NULL, *rstp = NULL; 1233 mod_t *mp; 1234 const char *lts, *rts; 1235 1236 if ((lt = (ltp = ln->tn_type)->t_tspec) == PTR) 1237 lst = (lstp = ltp->t_subt)->t_tspec; 1238 if ((rt = (rtp = rn->tn_type)->t_tspec) == PTR) 1239 rst = (rstp = rtp->t_subt)->t_tspec; 1240 mp = &modtab[op]; 1241 1242 if (isatyp(lt) && isatyp(rt)) 1243 return (1); 1244 1245 if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION)) 1246 /* both are struct or union */ 1247 return (ltp->t_str == rtp->t_str); 1248 1249 /* 0, 0L and (void *)0 may be assigned to any pointer */ 1250 if (lt == PTR && ((rt == PTR && rst == VOID) || isityp(rt))) { 1251 if (rn->tn_op == CON && rn->tn_val->v_quad == 0) 1252 return (1); 1253 } 1254 1255 if (lt == PTR && rt == PTR && (lst == VOID || rst == VOID)) { 1256 /* two pointers, at least one pointer to void */ 1257 if (sflag && (lst == FUNC || rst == FUNC)) { 1258 /* comb. of ptr to func and ptr to void */ 1259 *(lst == FUNC ? <s : &rts) = "function pointer"; 1260 *(lst == VOID ? <s : &rts) = "'void *'"; 1261 switch (op) { 1262 case INIT: 1263 case RETURN: 1264 /* ANSI C forbids conversion of %s to %s */ 1265 warning(303, rts, lts); 1266 break; 1267 case FARG: 1268 /* ANSI C forbids conv. of %s to %s, arg #%d */ 1269 warning(304, rts, lts, arg); 1270 break; 1271 default: 1272 /* ANSI C forbids conv. of %s to %s, op %s */ 1273 warning(305, rts, lts, mp->m_name); 1274 break; 1275 } 1276 } 1277 } 1278 1279 if (lt == PTR && rt == PTR && (lst == VOID || rst == VOID || 1280 eqtype(lstp, rstp, 1, 0, NULL))) { 1281 /* compatible pointer types (qualifiers ignored) */ 1282 if (!tflag && 1283 ((!lstp->t_const && rstp->t_const) || 1284 (!lstp->t_volatile && rstp->t_volatile))) { 1285 /* left side has not all qualifiers of right */ 1286 switch (op) { 1287 case INIT: 1288 case RETURN: 1289 /* incompatible pointer types */ 1290 warning(182); 1291 break; 1292 case FARG: 1293 /* argument has incompat. ptr. type, arg #%d */ 1294 warning(153, arg); 1295 break; 1296 default: 1297 /* operands have incompat. ptr. types, op %s */ 1298 warning(128, mp->m_name); 1299 break; 1300 } 1301 } 1302 return (1); 1303 } 1304 1305 if ((lt == PTR && isityp(rt)) || (isityp(lt) && rt == PTR)) { 1306 switch (op) { 1307 case INIT: 1308 case RETURN: 1309 /* illegal combination of pointer and integer */ 1310 warning(183); 1311 break; 1312 case FARG: 1313 /* illegal comb. of ptr. and int., arg #%d */ 1314 warning(154, arg); 1315 break; 1316 default: 1317 /* illegal comb. of ptr. and int., op %s */ 1318 warning(123, mp->m_name); 1319 break; 1320 } 1321 return (1); 1322 } 1323 1324 if (lt == PTR && rt == PTR) { 1325 switch (op) { 1326 case INIT: 1327 case RETURN: 1328 illptrc(NULL, ltp, rtp); 1329 break; 1330 case FARG: 1331 /* argument has incompatible pointer type, arg #%d */ 1332 warning(153, arg); 1333 break; 1334 default: 1335 illptrc(mp, ltp, rtp); 1336 break; 1337 } 1338 return (1); 1339 } 1340 1341 switch (op) { 1342 case INIT: 1343 /* initialisation type mismatch */ 1344 error(185); 1345 break; 1346 case RETURN: 1347 /* return value type mismatch */ 1348 error(211); 1349 break; 1350 case FARG: 1351 /* argument is incompatible with prototype, arg #%d */ 1352 warning(155, arg); 1353 break; 1354 default: 1355 incompat(op, lt, rt); 1356 break; 1357 } 1358 1359 return (0); 1360 } 1361 1362 /* 1363 * Prints a warning if an operator, which should be senseless for an 1364 * enum type, is applied to an enum type. 1365 */ 1366 static void 1367 chkbeop(op_t op, tnode_t *ln, tnode_t *rn) 1368 { 1369 mod_t *mp; 1370 1371 if (!eflag) 1372 return; 1373 1374 mp = &modtab[op]; 1375 1376 if (!(ln->tn_type->t_isenum || 1377 (mp->m_binary && rn->tn_type->t_isenum))) { 1378 return; 1379 } 1380 1381 /* 1382 * Enum as offset to a pointer is an exception (otherwise enums 1383 * could not be used as array indizes). 1384 */ 1385 if (op == PLUS && 1386 ((ln->tn_type->t_isenum && rn->tn_type->t_tspec == PTR) || 1387 (rn->tn_type->t_isenum && ln->tn_type->t_tspec == PTR))) { 1388 return; 1389 } 1390 1391 /* dubious operation on enum, op %s */ 1392 warning(241, mp->m_name); 1393 1394 } 1395 1396 /* 1397 * Prints a warning if an operator is applied to two different enum types. 1398 */ 1399 static void 1400 chkeop2(op_t op, int arg, tnode_t *ln, tnode_t *rn) 1401 { 1402 mod_t *mp; 1403 1404 mp = &modtab[op]; 1405 1406 if (ln->tn_type->t_enum != rn->tn_type->t_enum) { 1407 switch (op) { 1408 case INIT: 1409 /* enum type mismatch in initialisation */ 1410 warning(210); 1411 break; 1412 case FARG: 1413 /* enum type mismatch, arg #%d */ 1414 warning(156, arg); 1415 break; 1416 case RETURN: 1417 /* return value type mismatch */ 1418 warning(211); 1419 break; 1420 default: 1421 /* enum type mismatch, op %s */ 1422 warning(130, mp->m_name); 1423 break; 1424 } 1425 #if 0 1426 } else if (mp->m_comp && op != EQ && op != NE) { 1427 if (eflag) 1428 /* dubious comparisons of enums */ 1429 warning(243, mp->m_name); 1430 #endif 1431 } 1432 } 1433 1434 /* 1435 * Prints a warning if an operator has both enum end other integer 1436 * types. 1437 */ 1438 static void 1439 chkeop1(op_t op, int arg, tnode_t *ln, tnode_t *rn) 1440 { 1441 char lbuf[64], rbuf[64]; 1442 1443 if (!eflag) 1444 return; 1445 1446 switch (op) { 1447 case INIT: 1448 /* 1449 * Initializations with 0 should be allowed. Otherwise, 1450 * we should complain about all uninitialized enums, 1451 * consequently. 1452 */ 1453 if (!rn->tn_type->t_isenum && rn->tn_op == CON && 1454 isityp(rn->tn_type->t_tspec) && rn->tn_val->v_quad == 0) { 1455 return; 1456 } 1457 /* initialisation of '%s' with '%s' */ 1458 warning(277, tyname(lbuf, sizeof(lbuf), ln->tn_type), 1459 tyname(rbuf, sizeof(rbuf), rn->tn_type)); 1460 break; 1461 case FARG: 1462 /* combination of '%s' and '%s', arg #%d */ 1463 warning(278, tyname(lbuf, sizeof(lbuf), ln->tn_type), 1464 tyname(rbuf, sizeof(rbuf), rn->tn_type), arg); 1465 break; 1466 case RETURN: 1467 /* combination of '%s' and '%s' in return */ 1468 warning(279, tyname(lbuf, sizeof(lbuf), ln->tn_type), 1469 tyname(rbuf, sizeof(rbuf), rn->tn_type)); 1470 break; 1471 default: 1472 /* combination of '%s' and %s, op %s */ 1473 warning(242, tyname(lbuf, sizeof(lbuf), ln->tn_type), 1474 tyname(rbuf, sizeof(rbuf), rn->tn_type), 1475 modtab[op].m_name); 1476 break; 1477 } 1478 } 1479 1480 /* 1481 * Build and initialize a new node. 1482 */ 1483 static tnode_t * 1484 mktnode(op_t op, type_t *type, tnode_t *ln, tnode_t *rn) 1485 { 1486 tnode_t *ntn; 1487 tspec_t t; 1488 1489 ntn = getnode(); 1490 1491 ntn->tn_op = op; 1492 ntn->tn_type = type; 1493 ntn->tn_left = ln; 1494 ntn->tn_right = rn; 1495 1496 if (op == STAR || op == FSEL) { 1497 if (ln->tn_type->t_tspec == PTR) { 1498 t = ln->tn_type->t_subt->t_tspec; 1499 if (t != FUNC && t != VOID) 1500 ntn->tn_lvalue = 1; 1501 } else { 1502 LERROR("mktnode()"); 1503 } 1504 } 1505 1506 return (ntn); 1507 } 1508 1509 /* 1510 * Performs usual conversion of operands to (unsigned) int. 1511 * 1512 * If tflag is set or the operand is a function argument with no 1513 * type information (no prototype or variable # of args), convert 1514 * float to double. 1515 */ 1516 tnode_t * 1517 promote(op_t op, int farg, tnode_t *tn) 1518 { 1519 tspec_t t; 1520 type_t *ntp; 1521 int len; 1522 1523 t = tn->tn_type->t_tspec; 1524 1525 if (!isatyp(t)) 1526 return (tn); 1527 1528 if (!tflag) { 1529 /* 1530 * ANSI C requires that the result is always of type INT 1531 * if INT can represent all possible values of the previous 1532 * type. 1533 */ 1534 if (tn->tn_type->t_isfield) { 1535 len = tn->tn_type->t_flen; 1536 if (size(INT) > len) { 1537 t = INT; 1538 } else { 1539 if (size(INT) != len) 1540 LERROR("promote()"); 1541 if (isutyp(t)) { 1542 t = UINT; 1543 } else { 1544 t = INT; 1545 } 1546 } 1547 } else if (t == CHAR || t == UCHAR || t == SCHAR) { 1548 t = (size(CHAR) < size(INT) || t != UCHAR) ? 1549 INT : UINT; 1550 } else if (t == SHORT || t == USHORT) { 1551 t = (size(SHORT) < size(INT) || t == SHORT) ? 1552 INT : UINT; 1553 } else if (t == ENUM) { 1554 t = INT; 1555 } else if (farg && t == FLOAT) { 1556 t = DOUBLE; 1557 } 1558 } else { 1559 /* 1560 * In traditional C, keep unsigned and promote FLOAT 1561 * to DOUBLE. 1562 */ 1563 if (t == UCHAR || t == USHORT) { 1564 t = UINT; 1565 } else if (t == CHAR || t == SCHAR || t == SHORT) { 1566 t = INT; 1567 } else if (t == FLOAT) { 1568 t = DOUBLE; 1569 } else if (t == ENUM) { 1570 t = INT; 1571 } 1572 } 1573 1574 if (t != tn->tn_type->t_tspec) { 1575 ntp = tduptyp(tn->tn_type); 1576 ntp->t_tspec = t; 1577 /* 1578 * Keep t_isenum so we are later able to check compatibility 1579 * of enum types. 1580 */ 1581 tn = convert(op, 0, ntp, tn); 1582 } 1583 1584 return (tn); 1585 } 1586 1587 /* 1588 * Insert conversions which are necessary to give both operands the same 1589 * type. This is done in different ways for traditional C and ANIS C. 1590 */ 1591 static void 1592 balance(op_t op, tnode_t **lnp, tnode_t **rnp) 1593 { 1594 tspec_t lt, rt, t; 1595 int i, u; 1596 type_t *ntp; 1597 static tspec_t tl[] = { 1598 LDOUBLE, DOUBLE, FLOAT, UQUAD, QUAD, ULONG, LONG, UINT, INT, 1599 }; 1600 1601 lt = (*lnp)->tn_type->t_tspec; 1602 rt = (*rnp)->tn_type->t_tspec; 1603 1604 if (!isatyp(lt) || !isatyp(rt)) 1605 return; 1606 1607 if (!tflag) { 1608 if (lt == rt) { 1609 t = lt; 1610 } else if (lt == LDOUBLE || rt == LDOUBLE) { 1611 t = LDOUBLE; 1612 } else if (lt == DOUBLE || rt == DOUBLE) { 1613 t = DOUBLE; 1614 } else if (lt == FLOAT || rt == FLOAT) { 1615 t = FLOAT; 1616 } else { 1617 /* 1618 * If type A has more bits than type B it should 1619 * be able to hold all possible values of type B. 1620 */ 1621 if (size(lt) > size(rt)) { 1622 t = lt; 1623 } else if (size(lt) < size(rt)) { 1624 t = rt; 1625 } else { 1626 for (i = 3; tl[i] != INT; i++) { 1627 if (tl[i] == lt || tl[i] == rt) 1628 break; 1629 } 1630 if ((isutyp(lt) || isutyp(rt)) && 1631 !isutyp(tl[i])) { 1632 i--; 1633 } 1634 t = tl[i]; 1635 } 1636 } 1637 } else { 1638 /* Keep unsigned in traditional C */ 1639 u = isutyp(lt) || isutyp(rt); 1640 for (i = 0; tl[i] != INT; i++) { 1641 if (lt == tl[i] || rt == tl[i]) 1642 break; 1643 } 1644 t = tl[i]; 1645 if (u && isityp(t) && !isutyp(t)) 1646 t = utyp(t); 1647 } 1648 1649 if (t != lt) { 1650 ntp = tduptyp((*lnp)->tn_type); 1651 ntp->t_tspec = t; 1652 *lnp = convert(op, 0, ntp, *lnp); 1653 } 1654 if (t != rt) { 1655 ntp = tduptyp((*rnp)->tn_type); 1656 ntp->t_tspec = t; 1657 *rnp = convert(op, 0, ntp, *rnp); 1658 } 1659 } 1660 1661 /* 1662 * Insert a conversion operator, which converts the type of the node 1663 * to another given type. 1664 * If op is FARG, arg is the number of the argument (used for warnings). 1665 */ 1666 tnode_t * 1667 convert(op_t op, int arg, type_t *tp, tnode_t *tn) 1668 { 1669 tnode_t *ntn; 1670 tspec_t nt, ot, ost = NOTSPEC; 1671 1672 if (tn->tn_lvalue) 1673 LERROR("convert()"); 1674 1675 nt = tp->t_tspec; 1676 if ((ot = tn->tn_type->t_tspec) == PTR) 1677 ost = tn->tn_type->t_subt->t_tspec; 1678 1679 if (!tflag && !sflag && op == FARG) 1680 ptconv(arg, nt, ot, tp, tn); 1681 if (isityp(nt) && isityp(ot)) { 1682 iiconv(op, arg, nt, ot, tp, tn); 1683 } else if (nt == PTR && ((ot == PTR && ost == VOID) || isityp(ot)) && 1684 tn->tn_op == CON && tn->tn_val->v_quad == 0) { 1685 /* 0, 0L and (void *)0 may be assigned to any pointer. */ 1686 } else if (isityp(nt) && ot == PTR) { 1687 piconv(op, nt, tp, tn); 1688 } else if (nt == PTR && ot == PTR) { 1689 ppconv(op, tn, tp); 1690 } 1691 1692 ntn = getnode(); 1693 ntn->tn_op = CVT; 1694 ntn->tn_type = tp; 1695 ntn->tn_cast = op == CVT; 1696 if (tn->tn_op != CON || nt == VOID) { 1697 ntn->tn_left = tn; 1698 } else { 1699 ntn->tn_op = CON; 1700 ntn->tn_val = tgetblk(sizeof (val_t)); 1701 cvtcon(op, arg, ntn->tn_type, ntn->tn_val, tn->tn_val); 1702 } 1703 1704 return (ntn); 1705 } 1706 1707 /* 1708 * Print a warning if a prototype causes a type conversion that is 1709 * different from what would happen to the same argument in the 1710 * absence of a prototype. 1711 * 1712 * Errors/Warnings about illegal type combinations are already printed 1713 * in asgntypok(). 1714 */ 1715 static void 1716 ptconv(int arg, tspec_t nt, tspec_t ot, type_t *tp, tnode_t *tn) 1717 { 1718 tnode_t *ptn; 1719 char buf[64]; 1720 1721 if (!isatyp(nt) || !isatyp(ot)) 1722 return; 1723 1724 /* 1725 * If the type of the formal parameter is char/short, a warning 1726 * would be useless, because functions declared the old style 1727 * can't expect char/short arguments. 1728 */ 1729 if (nt == CHAR || nt == UCHAR || nt == SHORT || nt == USHORT) 1730 return; 1731 1732 /* get default promotion */ 1733 ptn = promote(NOOP, 1, tn); 1734 ot = ptn->tn_type->t_tspec; 1735 1736 /* return if types are the same with and without prototype */ 1737 if (nt == ot || (nt == ENUM && ot == INT)) 1738 return; 1739 1740 if (isftyp(nt) != isftyp(ot) || psize(nt) != psize(ot)) { 1741 /* representation and/or width change */ 1742 if (styp(nt) != SHORT || !isityp(ot) || psize(ot) > psize(INT)) 1743 /* conversion to '%s' due to prototype, arg #%d */ 1744 warning(259, tyname(buf, sizeof(buf), tp), arg); 1745 } else if (hflag) { 1746 /* 1747 * they differ in sign or base type (char, short, int, 1748 * long, long long, float, double, long double) 1749 * 1750 * if they differ only in sign and the argument is a constant 1751 * and the msb of the argument is not set, print no warning 1752 */ 1753 if (ptn->tn_op == CON && isityp(nt) && styp(nt) == styp(ot) && 1754 msb(ptn->tn_val->v_quad, ot, -1) == 0) { 1755 /* ok */ 1756 } else { 1757 /* conversion to '%s' due to prototype, arg #%d */ 1758 warning(259, tyname(buf, sizeof(buf), tp), arg); 1759 } 1760 } 1761 } 1762 1763 /* 1764 * Print warnings for conversions of integer types which my cause 1765 * problems. 1766 */ 1767 /* ARGSUSED */ 1768 static void 1769 iiconv(op_t op, int arg, tspec_t nt, tspec_t ot, type_t *tp, tnode_t *tn) 1770 { 1771 char buf[64]; 1772 if (tn->tn_op == CON) 1773 return; 1774 1775 if (op == CVT) 1776 return; 1777 1778 #if 0 1779 if (psize(nt) > psize(ot) && isutyp(nt) != isutyp(ot)) { 1780 /* conversion to %s may sign-extend incorrectly (, arg #%d) */ 1781 if (aflag && pflag) { 1782 if (op == FARG) { 1783 warning(297, tyname(buf, sizeof(buf), tp), arg); 1784 } else { 1785 warning(131, tyname(buf, sizeof(buf), tp)); 1786 } 1787 } 1788 } 1789 #endif 1790 1791 if (psize(nt) < psize(ot) && 1792 (ot == LONG || ot == ULONG || ot == QUAD || ot == UQUAD || 1793 aflag > 1)) { 1794 /* conversion from '%s' may lose accuracy */ 1795 if (aflag) { 1796 if (op == FARG) { 1797 warning(298, tyname(buf, sizeof(buf), tn->tn_type), arg); 1798 } else { 1799 warning(132, tyname(buf, sizeof(buf), tn->tn_type)); 1800 } 1801 } 1802 } 1803 } 1804 1805 /* 1806 * Print warnings for dubious conversions of pointer to integer. 1807 */ 1808 static void 1809 piconv(op_t op, tspec_t nt, type_t *tp, tnode_t *tn) 1810 { 1811 char buf[64]; 1812 1813 if (tn->tn_op == CON) 1814 return; 1815 1816 if (op != CVT) { 1817 /* We got already an error. */ 1818 return; 1819 } 1820 1821 if (psize(nt) < psize(PTR)) { 1822 if (pflag && size(nt) >= size(PTR)) { 1823 /* conv. of pointer to %s may lose bits */ 1824 warning(134, tyname(buf, sizeof(buf), tp)); 1825 } else { 1826 /* conv. of pointer to %s loses bits */ 1827 warning(133, tyname(buf, sizeof(buf), tp)); 1828 } 1829 } 1830 } 1831 1832 /* 1833 * Print warnings for questionable pointer conversions. 1834 */ 1835 static void 1836 ppconv(op_t op, tnode_t *tn, type_t *tp) 1837 { 1838 tspec_t nt, ot; 1839 const char *nts, *ots; 1840 1841 /* 1842 * We got already an error (pointers of different types 1843 * without a cast) or we will not get a warning. 1844 */ 1845 if (op != CVT) 1846 return; 1847 1848 nt = tp->t_subt->t_tspec; 1849 ot = tn->tn_type->t_subt->t_tspec; 1850 1851 if (nt == VOID || ot == VOID) { 1852 if (sflag && (nt == FUNC || ot == FUNC)) { 1853 /* (void *)0 already handled in convert() */ 1854 *(nt == FUNC ? &nts : &ots) = "function pointer"; 1855 *(nt == VOID ? &nts : &ots) = "'void *'"; 1856 /* ANSI C forbids conversion of %s to %s */ 1857 warning(303, ots, nts); 1858 } 1859 return; 1860 } else if (nt == FUNC && ot == FUNC) { 1861 return; 1862 } else if (nt == FUNC || ot == FUNC) { 1863 /* questionable conversion of function pointer */ 1864 warning(229); 1865 return; 1866 } 1867 1868 if (getbound(tp->t_subt) > getbound(tn->tn_type->t_subt)) { 1869 if (hflag) 1870 /* possible pointer alignment problem */ 1871 warning(135); 1872 } 1873 if (((nt == STRUCT || nt == UNION) && 1874 tp->t_subt->t_str != tn->tn_type->t_subt->t_str) || 1875 psize(nt) != psize(ot)) { 1876 if (cflag) { 1877 /* pointer casts may be troublesome */ 1878 warning(247); 1879 } 1880 } 1881 } 1882 1883 /* 1884 * Converts a typed constant in a constant of another type. 1885 * 1886 * op operator which requires conversion 1887 * arg if op is FARG, # of argument 1888 * tp type in which to convert the constant 1889 * nv new constant 1890 * v old constant 1891 */ 1892 void 1893 cvtcon(op_t op, int arg, type_t *tp, val_t *nv, val_t *v) 1894 { 1895 char lbuf[64], rbuf[64]; 1896 tspec_t ot, nt; 1897 ldbl_t max = 0.0, min = 0.0; 1898 int sz, rchk; 1899 int64_t xmask, xmsk1; 1900 int osz, nsz; 1901 1902 ot = v->v_tspec; 1903 nt = nv->v_tspec = tp->t_tspec; 1904 rchk = 0; 1905 1906 if (ot == FLOAT || ot == DOUBLE || ot == LDOUBLE) { 1907 switch (nt) { 1908 case CHAR: 1909 max = CHAR_MAX; min = CHAR_MIN; break; 1910 case UCHAR: 1911 max = UCHAR_MAX; min = 0; break; 1912 case SCHAR: 1913 max = SCHAR_MAX; min = SCHAR_MIN; break; 1914 case SHORT: 1915 max = SHRT_MAX; min = SHRT_MIN; break; 1916 case USHORT: 1917 max = USHRT_MAX; min = 0; break; 1918 case ENUM: 1919 case INT: 1920 max = INT_MAX; min = INT_MIN; break; 1921 case UINT: 1922 max = (u_int)UINT_MAX; min = 0; break; 1923 case LONG: 1924 max = LONG_MAX; min = LONG_MIN; break; 1925 case ULONG: 1926 max = (u_long)ULONG_MAX; min = 0; break; 1927 case QUAD: 1928 max = QUAD_MAX; min = QUAD_MIN; break; 1929 case UQUAD: 1930 max = (uint64_t)UQUAD_MAX; min = 0; break; 1931 case FLOAT: 1932 max = FLT_MAX; min = -FLT_MAX; break; 1933 case DOUBLE: 1934 max = DBL_MAX; min = -DBL_MAX; break; 1935 case PTR: 1936 /* Got already an error because of float --> ptr */ 1937 case LDOUBLE: 1938 max = LDBL_MAX; min = -LDBL_MAX; break; 1939 default: 1940 LERROR("cvtcon()"); 1941 } 1942 if (v->v_ldbl > max || v->v_ldbl < min) { 1943 if (nt == LDOUBLE) 1944 LERROR("cvtcon()"); 1945 if (op == FARG) { 1946 /* conv. of %s to %s is out of rng., arg #%d */ 1947 warning(295, tyname(lbuf, sizeof(lbuf), 1948 gettyp(ot)), tyname(rbuf, sizeof(rbuf), tp), 1949 arg); 1950 } else { 1951 /* conversion of %s to %s is out of range */ 1952 warning(119, tyname(lbuf, sizeof(lbuf), 1953 gettyp(ot)), 1954 tyname(rbuf, sizeof(rbuf), tp)); 1955 } 1956 v->v_ldbl = v->v_ldbl > 0 ? max : min; 1957 } 1958 if (nt == FLOAT) { 1959 nv->v_ldbl = (float)v->v_ldbl; 1960 } else if (nt == DOUBLE) { 1961 nv->v_ldbl = (double)v->v_ldbl; 1962 } else if (nt == LDOUBLE) { 1963 nv->v_ldbl = v->v_ldbl; 1964 } else { 1965 nv->v_quad = (nt == PTR || isutyp(nt)) ? 1966 (uint64_t)v->v_ldbl : (int64_t)v->v_ldbl; 1967 } 1968 } else { 1969 if (nt == FLOAT) { 1970 nv->v_ldbl = (ot == PTR || isutyp(ot)) ? 1971 (float)(uint64_t)v->v_quad : (float)v->v_quad; 1972 } else if (nt == DOUBLE) { 1973 nv->v_ldbl = (ot == PTR || isutyp(ot)) ? 1974 (double)(uint64_t)v->v_quad : (double)v->v_quad; 1975 } else if (nt == LDOUBLE) { 1976 nv->v_ldbl = (ot == PTR || isutyp(ot)) ? 1977 (ldbl_t)(uint64_t)v->v_quad : (ldbl_t)v->v_quad; 1978 } else { 1979 rchk = 1; /* Check for lost precision. */ 1980 nv->v_quad = v->v_quad; 1981 } 1982 } 1983 1984 if (v->v_ansiu && isftyp(nt)) { 1985 /* ANSI C treats constant as unsigned */ 1986 warning(157); 1987 v->v_ansiu = 0; 1988 } else if (v->v_ansiu && (isityp(nt) && !isutyp(nt) && 1989 psize(nt) > psize(ot))) { 1990 /* ANSI C treats constant as unsigned */ 1991 warning(157); 1992 v->v_ansiu = 0; 1993 } 1994 1995 if (nt != FLOAT && nt != DOUBLE && nt != LDOUBLE) { 1996 sz = tp->t_isfield ? tp->t_flen : size(nt); 1997 nv->v_quad = xsign(nv->v_quad, nt, sz); 1998 } 1999 2000 if (rchk && op != CVT) { 2001 osz = size(ot); 2002 nsz = tp->t_isfield ? tp->t_flen : size(nt); 2003 xmask = qlmasks[nsz] ^ qlmasks[osz]; 2004 xmsk1 = qlmasks[nsz] ^ qlmasks[osz - 1]; 2005 /* 2006 * For bitwise operations we are not interested in the 2007 * value, but in the bits itself. 2008 */ 2009 if (op == ORASS || op == OR || op == XOR) { 2010 /* 2011 * Print a warning if bits which were set are 2012 * lost due to the conversion. 2013 * This can happen with operator ORASS only. 2014 */ 2015 if (nsz < osz && (v->v_quad & xmask) != 0) { 2016 /* constant truncated by conv., op %s */ 2017 warning(306, modtab[op].m_name); 2018 } 2019 } else if (op == ANDASS || op == AND) { 2020 /* 2021 * Print a warning if additional bits are not all 1 2022 * and the most significant bit of the old value is 1, 2023 * or if at least one (but not all) removed bit was 0. 2024 */ 2025 if (nsz > osz && 2026 (nv->v_quad & qbmasks[osz - 1]) != 0 && 2027 (nv->v_quad & xmask) != xmask) { 2028 /* 2029 * extra bits set to 0 in conversion 2030 * of '%s' to '%s', op %s 2031 */ 2032 warning(309, tyname(lbuf, sizeof(lbuf), 2033 gettyp(ot)), tyname(rbuf, sizeof(rbuf), tp), 2034 modtab[op].m_name); 2035 } else if (nsz < osz && 2036 (v->v_quad & xmask) != xmask && 2037 (v->v_quad & xmask) != 0) { 2038 /* const. truncated by conv., op %s */ 2039 warning(306, modtab[op].m_name); 2040 } 2041 } else if ((nt != PTR && isutyp(nt)) && 2042 (ot != PTR && !isutyp(ot)) && v->v_quad < 0) { 2043 if (op == ASSIGN) { 2044 /* assignment of negative constant to ... */ 2045 warning(164); 2046 } else if (op == INIT) { 2047 /* initialisation of unsigned with neg. ... */ 2048 warning(221); 2049 } else if (op == FARG) { 2050 /* conversion of neg. const. to ..., arg #%d */ 2051 warning(296, arg); 2052 } else if (modtab[op].m_comp) { 2053 /* we get this warning already in chkcomp() */ 2054 } else { 2055 /* conversion of negative constant to ... */ 2056 warning(222); 2057 } 2058 } else if (nv->v_quad != v->v_quad && nsz <= osz && 2059 (v->v_quad & xmask) != 0 && 2060 (isutyp(ot) || (v->v_quad & xmsk1) != xmsk1)) { 2061 /* 2062 * Loss of significant bit(s). All truncated bits 2063 * of unsigned types or all truncated bits plus the 2064 * msb of the target for signed types are considered 2065 * to be significant bits. Loss of significant bits 2066 * means that at least on of the bits was set in an 2067 * unsigned type or that at least one, but not all of 2068 * the bits was set in an signed type. 2069 * Loss of significant bits means that it is not 2070 * possible, also not with necessary casts, to convert 2071 * back to the original type. A example for a 2072 * necessary cast is: 2073 * char c; int i; c = 128; 2074 * i = c; ** yields -128 ** 2075 * i = (unsigned char)c; ** yields 128 ** 2076 */ 2077 if (op == ASSIGN && tp->t_isfield) { 2078 /* precision lost in bit-field assignment */ 2079 warning(166); 2080 } else if (op == ASSIGN) { 2081 /* constant truncated by assignment */ 2082 warning(165); 2083 } else if (op == INIT && tp->t_isfield) { 2084 /* bit-field initializer does not fit */ 2085 warning(180); 2086 } else if (op == INIT) { 2087 /* initializer does not fit */ 2088 warning(178); 2089 } else if (op == CASE) { 2090 /* case label affected by conversion */ 2091 warning(196); 2092 } else if (op == FARG) { 2093 /* conv. of %s to %s is out of rng., arg #%d */ 2094 warning(295, tyname(lbuf, sizeof(lbuf), 2095 gettyp(ot)), tyname(rbuf, sizeof(rbuf), tp), 2096 arg); 2097 } else { 2098 /* conversion of %s to %s is out of range */ 2099 warning(119, tyname(lbuf, sizeof(lbuf), 2100 gettyp(ot)), 2101 tyname(rbuf, sizeof(rbuf), tp)); 2102 } 2103 } else if (nv->v_quad != v->v_quad) { 2104 if (op == ASSIGN && tp->t_isfield) { 2105 /* precision lost in bit-field assignment */ 2106 warning(166); 2107 } else if (op == INIT && tp->t_isfield) { 2108 /* bit-field initializer out of range */ 2109 warning(11); 2110 } else if (op == CASE) { 2111 /* case label affected by conversion */ 2112 warning(196); 2113 } else if (op == FARG) { 2114 /* conv. of %s to %s is out of rng., arg #%d */ 2115 warning(295, tyname(lbuf, sizeof(lbuf), 2116 gettyp(ot)), tyname(rbuf, sizeof(rbuf), tp), 2117 arg); 2118 } else { 2119 /* conversion of %s to %s is out of range */ 2120 warning(119, tyname(lbuf, sizeof(lbuf), 2121 gettyp(ot)), 2122 tyname(rbuf, sizeof(rbuf), tp)); 2123 } 2124 } 2125 } 2126 } 2127 2128 /* 2129 * Called if incompatible types were detected. 2130 * Prints a appropriate warning. 2131 */ 2132 static void 2133 incompat(op_t op, tspec_t lt, tspec_t rt) 2134 { 2135 mod_t *mp; 2136 2137 mp = &modtab[op]; 2138 2139 if (lt == VOID || (mp->m_binary && rt == VOID)) { 2140 /* void type illegal in expression */ 2141 error(109); 2142 } else if (op == ASSIGN) { 2143 if ((lt == STRUCT || lt == UNION) && 2144 (rt == STRUCT || rt == UNION)) { 2145 /* assignment of different structures */ 2146 error(240); 2147 } else { 2148 /* assignment type mismatch */ 2149 error(171); 2150 } 2151 } else if (mp->m_binary) { 2152 /* operands of %s have incompatible types */ 2153 error(107, mp->m_name); 2154 } else { 2155 /* operand of %s has incompatible type */ 2156 error(108, mp->m_name); 2157 } 2158 } 2159 2160 /* 2161 * Called if incompatible pointer types are detected. 2162 * Print an appropriate warning. 2163 */ 2164 static void 2165 illptrc(mod_t *mp, type_t *ltp, type_t *rtp) 2166 { 2167 tspec_t lt, rt; 2168 2169 if (ltp->t_tspec != PTR || rtp->t_tspec != PTR) 2170 LERROR("illptrc()"); 2171 2172 lt = ltp->t_subt->t_tspec; 2173 rt = rtp->t_subt->t_tspec; 2174 2175 if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION)) { 2176 if (mp == NULL) { 2177 /* illegal structure pointer combination */ 2178 warning(244); 2179 } else { 2180 /* illegal structure pointer combination, op %s */ 2181 warning(245, mp->m_name); 2182 } 2183 } else { 2184 if (mp == NULL) { 2185 /* illegal pointer combination */ 2186 warning(184); 2187 } else { 2188 /* illegal pointer combination, op %s */ 2189 warning(124, mp->m_name); 2190 } 2191 } 2192 } 2193 2194 /* 2195 * Make sure type (*tpp)->t_subt has at least the qualifiers 2196 * of tp1->t_subt and tp2->t_subt. 2197 */ 2198 static void 2199 mrgqual(type_t **tpp, type_t *tp1, type_t *tp2) 2200 { 2201 2202 if ((*tpp)->t_tspec != PTR || 2203 tp1->t_tspec != PTR || tp2->t_tspec != PTR) { 2204 LERROR("mrgqual()"); 2205 } 2206 2207 if ((*tpp)->t_subt->t_const == 2208 (tp1->t_subt->t_const | tp2->t_subt->t_const) && 2209 (*tpp)->t_subt->t_volatile == 2210 (tp1->t_subt->t_volatile | tp2->t_subt->t_volatile)) { 2211 return; 2212 } 2213 2214 *tpp = tduptyp(*tpp); 2215 (*tpp)->t_subt = tduptyp((*tpp)->t_subt); 2216 (*tpp)->t_subt->t_const = 2217 tp1->t_subt->t_const | tp2->t_subt->t_const; 2218 (*tpp)->t_subt->t_volatile = 2219 tp1->t_subt->t_volatile | tp2->t_subt->t_volatile; 2220 } 2221 2222 /* 2223 * Returns 1 if the given structure or union has a constant member 2224 * (maybe recursively). 2225 */ 2226 static int 2227 conmemb(type_t *tp) 2228 { 2229 sym_t *m; 2230 tspec_t t; 2231 2232 if ((t = tp->t_tspec) != STRUCT && t != UNION) 2233 LERROR("conmemb()"); 2234 for (m = tp->t_str->memb; m != NULL; m = m->s_nxt) { 2235 tp = m->s_type; 2236 if (tp->t_const) 2237 return (1); 2238 if ((t = tp->t_tspec) == STRUCT || t == UNION) { 2239 if (conmemb(m->s_type)) 2240 return (1); 2241 } 2242 } 2243 return (0); 2244 } 2245 2246 const char * 2247 basictyname(tspec_t t) 2248 { 2249 switch (t) { 2250 case CHAR: return "char"; 2251 case UCHAR: return "unsigned char"; 2252 case SCHAR: return "signed char"; 2253 case SHORT: return "short"; 2254 case USHORT: return "unsigned short"; 2255 case INT: return "int"; 2256 case UINT: return "unsigned int"; 2257 case LONG: return "long"; 2258 case ULONG: return "unsigned long"; 2259 case QUAD: return "long long"; 2260 case UQUAD: return "unsigned long long"; 2261 case FLOAT: return "float"; 2262 case DOUBLE: return "double"; 2263 case LDOUBLE: return "long double"; 2264 case PTR: return "pointer"; 2265 case ENUM: return "enum"; 2266 case STRUCT: return "struct"; 2267 case UNION: return "union"; 2268 case FUNC: return "function"; 2269 case ARRAY: return "array"; 2270 default: 2271 LERROR("basictyname()"); 2272 return NULL; 2273 } 2274 } 2275 2276 const char * 2277 tyname(char *buf, size_t bufsiz, type_t *tp) 2278 { 2279 tspec_t t; 2280 const char *s; 2281 char lbuf[64]; 2282 2283 if ((t = tp->t_tspec) == INT && tp->t_isenum) 2284 t = ENUM; 2285 2286 s = basictyname(t); 2287 2288 2289 switch (t) { 2290 case CHAR: 2291 case UCHAR: 2292 case SCHAR: 2293 case SHORT: 2294 case USHORT: 2295 case INT: 2296 case UINT: 2297 case LONG: 2298 case ULONG: 2299 case QUAD: 2300 case UQUAD: 2301 case FLOAT: 2302 case DOUBLE: 2303 case LDOUBLE: 2304 case FUNC: 2305 (void)snprintf(buf, bufsiz, "%s", s); 2306 break; 2307 case PTR: 2308 (void)snprintf(buf, bufsiz, "%s to %s", s, 2309 tyname(lbuf, sizeof(lbuf), tp->t_subt)); 2310 break; 2311 case ENUM: 2312 (void)snprintf(buf, bufsiz, "%s %s", s, 2313 tp->t_enum->etag->s_name); 2314 break; 2315 case STRUCT: 2316 case UNION: 2317 (void)snprintf(buf, bufsiz, "%s %s", s, 2318 tp->t_str->stag->s_name); 2319 break; 2320 case ARRAY: 2321 (void)snprintf(buf, bufsiz, "%s of %s[%d]", s, 2322 tyname(lbuf, sizeof(lbuf), tp->t_subt), tp->t_dim); 2323 break; 2324 default: 2325 LERROR("tyname()"); 2326 } 2327 return (buf); 2328 } 2329 2330 /* 2331 * Create a new node for one of the operators POINT and ARROW. 2332 */ 2333 static tnode_t * 2334 bldstr(op_t op, tnode_t *ln, tnode_t *rn) 2335 { 2336 tnode_t *ntn, *ctn; 2337 int nolval; 2338 2339 if (rn->tn_op != NAME) 2340 LERROR("bldstr()"); 2341 if (rn->tn_sym->s_value.v_tspec != INT) 2342 LERROR("bldstr()"); 2343 if (rn->tn_sym->s_scl != MOS && rn->tn_sym->s_scl != MOU) 2344 LERROR("bldstr()"); 2345 2346 /* 2347 * Remember if the left operand is an lvalue (structure members 2348 * are lvalues if and only if the structure itself is an lvalue). 2349 */ 2350 nolval = op == POINT && !ln->tn_lvalue; 2351 2352 if (op == POINT) { 2353 ln = bldamper(ln, 1); 2354 } else if (ln->tn_type->t_tspec != PTR) { 2355 if (!tflag || !isityp(ln->tn_type->t_tspec)) 2356 LERROR("bldstr()"); 2357 ln = convert(NOOP, 0, tincref(gettyp(VOID), PTR), ln); 2358 } 2359 2360 #if PTRDIFF_IS_LONG 2361 ctn = getinode(LONG, rn->tn_sym->s_value.v_quad / CHAR_BIT); 2362 #else 2363 ctn = getinode(INT, rn->tn_sym->s_value.v_quad / CHAR_BIT); 2364 #endif 2365 2366 ntn = mktnode(PLUS, tincref(rn->tn_type, PTR), ln, ctn); 2367 if (ln->tn_op == CON) 2368 ntn = fold(ntn); 2369 2370 if (rn->tn_type->t_isfield) { 2371 ntn = mktnode(FSEL, ntn->tn_type->t_subt, ntn, NULL); 2372 } else { 2373 ntn = mktnode(STAR, ntn->tn_type->t_subt, ntn, NULL); 2374 } 2375 2376 if (nolval) 2377 ntn->tn_lvalue = 0; 2378 2379 return (ntn); 2380 } 2381 2382 /* 2383 * Create a node for INCAFT, INCBEF, DECAFT and DECBEF. 2384 */ 2385 static tnode_t * 2386 bldincdec(op_t op, tnode_t *ln) 2387 { 2388 tnode_t *cn, *ntn; 2389 2390 if (ln == NULL) 2391 LERROR("bldincdec()"); 2392 2393 if (ln->tn_type->t_tspec == PTR) { 2394 cn = plength(ln->tn_type); 2395 } else { 2396 cn = getinode(INT, (int64_t)1); 2397 } 2398 ntn = mktnode(op, ln->tn_type, ln, cn); 2399 2400 return (ntn); 2401 } 2402 2403 /* 2404 * Create a tree node for the & operator 2405 */ 2406 static tnode_t * 2407 bldamper(tnode_t *tn, int noign) 2408 { 2409 tnode_t *ntn; 2410 tspec_t t; 2411 2412 if (!noign && ((t = tn->tn_type->t_tspec) == ARRAY || t == FUNC)) { 2413 /* & before array or function: ignored */ 2414 if (tflag) 2415 warning(127); 2416 return (tn); 2417 } 2418 2419 /* eliminate &* */ 2420 if (tn->tn_op == STAR && 2421 tn->tn_left->tn_type->t_tspec == PTR && 2422 tn->tn_left->tn_type->t_subt == tn->tn_type) { 2423 return (tn->tn_left); 2424 } 2425 2426 ntn = mktnode(AMPER, tincref(tn->tn_type, PTR), tn, NULL); 2427 2428 return (ntn); 2429 } 2430 2431 /* 2432 * Create a node for operators PLUS and MINUS. 2433 */ 2434 static tnode_t * 2435 bldplmi(op_t op, tnode_t *ln, tnode_t *rn) 2436 { 2437 tnode_t *ntn, *ctn; 2438 type_t *tp; 2439 2440 /* If pointer and integer, then pointer to the lhs. */ 2441 if (rn->tn_type->t_tspec == PTR && isityp(ln->tn_type->t_tspec)) { 2442 ntn = ln; 2443 ln = rn; 2444 rn = ntn; 2445 } 2446 2447 if (ln->tn_type->t_tspec == PTR && rn->tn_type->t_tspec != PTR) { 2448 2449 if (!isityp(rn->tn_type->t_tspec)) 2450 LERROR("bldplmi()"); 2451 2452 ctn = plength(ln->tn_type); 2453 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec) 2454 rn = convert(NOOP, 0, ctn->tn_type, rn); 2455 rn = mktnode(MULT, rn->tn_type, rn, ctn); 2456 if (rn->tn_left->tn_op == CON) 2457 rn = fold(rn); 2458 ntn = mktnode(op, ln->tn_type, ln, rn); 2459 2460 } else if (rn->tn_type->t_tspec == PTR) { 2461 2462 if (ln->tn_type->t_tspec != PTR || op != MINUS) 2463 LERROR("bldplmi()"); 2464 #if PTRDIFF_IS_LONG 2465 tp = gettyp(LONG); 2466 #else 2467 tp = gettyp(INT); 2468 #endif 2469 ntn = mktnode(op, tp, ln, rn); 2470 if (ln->tn_op == CON && rn->tn_op == CON) 2471 ntn = fold(ntn); 2472 ctn = plength(ln->tn_type); 2473 balance(NOOP, &ntn, &ctn); 2474 ntn = mktnode(DIV, tp, ntn, ctn); 2475 2476 } else { 2477 2478 ntn = mktnode(op, ln->tn_type, ln, rn); 2479 2480 } 2481 return (ntn); 2482 } 2483 2484 /* 2485 * Create a node for operators SHL and SHR. 2486 */ 2487 static tnode_t * 2488 bldshft(op_t op, tnode_t *ln, tnode_t *rn) 2489 { 2490 tspec_t t; 2491 tnode_t *ntn; 2492 2493 if ((t = rn->tn_type->t_tspec) != INT && t != UINT) 2494 rn = convert(CVT, 0, gettyp(INT), rn); 2495 ntn = mktnode(op, ln->tn_type, ln, rn); 2496 return (ntn); 2497 } 2498 2499 /* 2500 * Create a node for COLON. 2501 */ 2502 static tnode_t * 2503 bldcol(tnode_t *ln, tnode_t *rn) 2504 { 2505 tspec_t lt, rt, pdt; 2506 type_t *rtp; 2507 tnode_t *ntn; 2508 2509 lt = ln->tn_type->t_tspec; 2510 rt = rn->tn_type->t_tspec; 2511 #if PTRDIFF_IS_LONG 2512 pdt = LONG; 2513 #else 2514 pdt = INT; 2515 #endif 2516 2517 /* 2518 * Arithmetic types are balanced, all other type combinations 2519 * still need to be handled. 2520 */ 2521 if (isatyp(lt) && isatyp(rt)) { 2522 rtp = ln->tn_type; 2523 } else if (lt == VOID || rt == VOID) { 2524 rtp = gettyp(VOID); 2525 } else if (lt == STRUCT || lt == UNION) { 2526 /* Both types must be identical. */ 2527 if (rt != STRUCT && rt != UNION) 2528 LERROR("bldcol()"); 2529 if (ln->tn_type->t_str != rn->tn_type->t_str) 2530 LERROR("bldcol()"); 2531 if (incompl(ln->tn_type)) { 2532 /* unknown operand size, op %s */ 2533 error(138, modtab[COLON].m_name); 2534 return (NULL); 2535 } 2536 rtp = ln->tn_type; 2537 } else if (lt == PTR && isityp(rt)) { 2538 if (rt != pdt) { 2539 rn = convert(NOOP, 0, gettyp(pdt), rn); 2540 rt = pdt; 2541 } 2542 rtp = ln->tn_type; 2543 } else if (rt == PTR && isityp(lt)) { 2544 if (lt != pdt) { 2545 ln = convert(NOOP, 0, gettyp(pdt), ln); 2546 lt = pdt; 2547 } 2548 rtp = rn->tn_type; 2549 } else if (lt == PTR && ln->tn_type->t_subt->t_tspec == VOID) { 2550 if (rt != PTR) 2551 LERROR("bldcol()"); 2552 rtp = ln->tn_type; 2553 mrgqual(&rtp, ln->tn_type, rn->tn_type); 2554 } else if (rt == PTR && rn->tn_type->t_subt->t_tspec == VOID) { 2555 if (lt != PTR) 2556 LERROR("bldcol()"); 2557 rtp = rn->tn_type; 2558 mrgqual(&rtp, ln->tn_type, rn->tn_type); 2559 } else { 2560 if (lt != PTR || rt != PTR) 2561 LERROR("bldcol()"); 2562 /* 2563 * XXX For now we simply take the left type. This is 2564 * probably wrong, if one type contains a functionprototype 2565 * and the other one, at the same place, only an old style 2566 * declaration. 2567 */ 2568 rtp = ln->tn_type; 2569 mrgqual(&rtp, ln->tn_type, rn->tn_type); 2570 } 2571 2572 ntn = mktnode(COLON, rtp, ln, rn); 2573 2574 return (ntn); 2575 } 2576 2577 /* 2578 * Create a node for an assignment operator (both = and op= ). 2579 */ 2580 static tnode_t * 2581 bldasgn(op_t op, tnode_t *ln, tnode_t *rn) 2582 { 2583 tspec_t lt, rt; 2584 tnode_t *ntn, *ctn; 2585 2586 if (ln == NULL || rn == NULL) 2587 LERROR("bldasgn()"); 2588 2589 lt = ln->tn_type->t_tspec; 2590 rt = rn->tn_type->t_tspec; 2591 2592 if ((op == ADDASS || op == SUBASS) && lt == PTR) { 2593 if (!isityp(rt)) 2594 LERROR("bldasgn()"); 2595 ctn = plength(ln->tn_type); 2596 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec) 2597 rn = convert(NOOP, 0, ctn->tn_type, rn); 2598 rn = mktnode(MULT, rn->tn_type, rn, ctn); 2599 if (rn->tn_left->tn_op == CON) 2600 rn = fold(rn); 2601 } 2602 2603 if ((op == ASSIGN || op == RETURN) && (lt == STRUCT || rt == STRUCT)) { 2604 if (rt != lt || ln->tn_type->t_str != rn->tn_type->t_str) 2605 LERROR("bldasgn()"); 2606 if (incompl(ln->tn_type)) { 2607 if (op == RETURN) { 2608 /* cannot return incomplete type */ 2609 error(212); 2610 } else { 2611 /* unknown operand size, op %s */ 2612 error(138, modtab[op].m_name); 2613 } 2614 return (NULL); 2615 } 2616 } 2617 2618 if (op == SHLASS || op == SHRASS) { 2619 if (rt != INT) { 2620 rn = convert(NOOP, 0, gettyp(INT), rn); 2621 rt = INT; 2622 } 2623 } else { 2624 if (op == ASSIGN || lt != PTR) { 2625 if (lt != rt || 2626 (ln->tn_type->t_isfield && rn->tn_op == CON)) { 2627 rn = convert(op, 0, ln->tn_type, rn); 2628 rt = lt; 2629 } 2630 } 2631 } 2632 2633 ntn = mktnode(op, ln->tn_type, ln, rn); 2634 2635 return (ntn); 2636 } 2637 2638 /* 2639 * Get length of type tp->t_subt. 2640 */ 2641 static tnode_t * 2642 plength(type_t *tp) 2643 { 2644 int elem, elsz; 2645 tspec_t st; 2646 2647 if (tp->t_tspec != PTR) 2648 LERROR("plength()"); 2649 tp = tp->t_subt; 2650 2651 elem = 1; 2652 elsz = 0; 2653 2654 while (tp->t_tspec == ARRAY) { 2655 elem *= tp->t_dim; 2656 tp = tp->t_subt; 2657 } 2658 2659 switch (tp->t_tspec) { 2660 case FUNC: 2661 /* pointer to function is not allowed here */ 2662 error(110); 2663 break; 2664 case VOID: 2665 /* cannot do pointer arithmetic on operand of ... */ 2666 (void)gnuism(136); 2667 break; 2668 case STRUCT: 2669 case UNION: 2670 if ((elsz = tp->t_str->size) == 0) 2671 /* cannot do pointer arithmetic on operand of ... */ 2672 error(136); 2673 break; 2674 case ENUM: 2675 if (incompl(tp)) { 2676 /* cannot do pointer arithmetic on operand of ... */ 2677 warning(136); 2678 } 2679 /* FALLTHROUGH */ 2680 default: 2681 if ((elsz = size(tp->t_tspec)) == 0) { 2682 /* cannot do pointer arithmetic on operand of ... */ 2683 error(136); 2684 } else if (elsz == -1) { 2685 LERROR("plength()"); 2686 } 2687 break; 2688 } 2689 2690 if (elem == 0 && elsz != 0) { 2691 /* cannot do pointer arithmetic on operand of ... */ 2692 error(136); 2693 } 2694 2695 if (elsz == 0) 2696 elsz = CHAR_BIT; 2697 2698 #if PTRDIFF_IS_LONG 2699 st = LONG; 2700 #else 2701 st = INT; 2702 #endif 2703 2704 return (getinode(st, (int64_t)(elem * elsz / CHAR_BIT))); 2705 } 2706 2707 /* 2708 * XXX 2709 * Note: There appear to be a number of bugs in detecting overflow in 2710 * this function. An audit and a set of proper regression tests are needed. 2711 * --Perry Metzger, Nov. 16, 2001 2712 */ 2713 /* 2714 * Do only as much as necessary to compute constant expressions. 2715 * Called only if the operator allows folding and (both) operands 2716 * are constants. 2717 */ 2718 static tnode_t * 2719 fold(tnode_t *tn) 2720 { 2721 val_t *v; 2722 tspec_t t; 2723 int utyp, ovfl; 2724 int64_t sl, sr = 0, q = 0, mask; 2725 uint64_t ul, ur = 0; 2726 tnode_t *cn; 2727 2728 v = xcalloc(1, sizeof (val_t)); 2729 v->v_tspec = t = tn->tn_type->t_tspec; 2730 2731 utyp = t == PTR || isutyp(t); 2732 ul = sl = tn->tn_left->tn_val->v_quad; 2733 if (modtab[tn->tn_op].m_binary) 2734 ur = sr = tn->tn_right->tn_val->v_quad; 2735 2736 mask = qlmasks[size(t)]; 2737 ovfl = 0; 2738 2739 switch (tn->tn_op) { 2740 case UPLUS: 2741 q = sl; 2742 break; 2743 case UMINUS: 2744 q = -sl; 2745 if (msb(q, t, -1) == msb(sl, t, -1)) 2746 ovfl = 1; 2747 break; 2748 case COMPL: 2749 q = ~sl; 2750 break; 2751 case MULT: 2752 if (utyp) { 2753 q = ul * ur; 2754 if (q != (q & mask)) 2755 ovfl = 1; 2756 else if ((ul != 0) && ((q / ul) != ur)) 2757 ovfl = 1; 2758 } else { 2759 q = sl * sr; 2760 if (msb(q, t, -1) != (msb(sl, t, -1) ^ msb(sr, t, -1))) 2761 ovfl = 1; 2762 } 2763 break; 2764 case DIV: 2765 if (sr == 0) { 2766 /* division by 0 */ 2767 error(139); 2768 q = utyp ? UQUAD_MAX : QUAD_MAX; 2769 } else { 2770 q = utyp ? ul / ur : sl / sr; 2771 } 2772 break; 2773 case MOD: 2774 if (sr == 0) { 2775 /* modulus by 0 */ 2776 error(140); 2777 q = 0; 2778 } else { 2779 q = utyp ? ul % ur : sl % sr; 2780 } 2781 break; 2782 case PLUS: 2783 q = utyp ? ul + ur : sl + sr; 2784 if (msb(sl, t, -1) != 0 && msb(sr, t, -1) != 0) { 2785 if (msb(q, t, -1) == 0) 2786 ovfl = 1; 2787 } else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) == 0) { 2788 if (msb(q, t, -1) != 0) 2789 ovfl = 1; 2790 } 2791 break; 2792 case MINUS: 2793 q = utyp ? ul - ur : sl - sr; 2794 if (msb(sl, t, -1) != 0 && msb(sr, t, -1) == 0) { 2795 if (msb(q, t, -1) == 0) 2796 ovfl = 1; 2797 } else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) != 0) { 2798 if (msb(q, t, -1) != 0) 2799 ovfl = 1; 2800 } 2801 break; 2802 case SHL: 2803 q = utyp ? ul << sr : sl << sr; 2804 break; 2805 case SHR: 2806 /* 2807 * The sign must be explicitly extended because 2808 * shifts of signed values are implementation dependent. 2809 */ 2810 q = ul >> sr; 2811 q = xsign(q, t, size(t) - (int)sr); 2812 break; 2813 case LT: 2814 q = utyp ? ul < ur : sl < sr; 2815 break; 2816 case LE: 2817 q = utyp ? ul <= ur : sl <= sr; 2818 break; 2819 case GE: 2820 q = utyp ? ul >= ur : sl >= sr; 2821 break; 2822 case GT: 2823 q = utyp ? ul > ur : sl > sr; 2824 break; 2825 case EQ: 2826 q = utyp ? ul == ur : sl == sr; 2827 break; 2828 case NE: 2829 q = utyp ? ul != ur : sl != sr; 2830 break; 2831 case AND: 2832 q = utyp ? ul & ur : sl & sr; 2833 break; 2834 case XOR: 2835 q = utyp ? ul ^ ur : sl ^ sr; 2836 break; 2837 case OR: 2838 q = utyp ? ul | ur : sl | sr; 2839 break; 2840 default: 2841 LERROR("fold()"); 2842 } 2843 2844 /* XXX does not work for quads. */ 2845 if (ovfl || ((q | mask) != ~(uint64_t)0 && (q & ~mask) != 0)) { 2846 if (hflag) 2847 /* integer overflow detected, op %s */ 2848 warning(141, modtab[tn->tn_op].m_name); 2849 } 2850 2851 v->v_quad = xsign(q, t, -1); 2852 2853 cn = getcnode(tn->tn_type, v); 2854 2855 return (cn); 2856 } 2857 2858 /* 2859 * Same for operators whose operands are compared with 0 (test context). 2860 */ 2861 static tnode_t * 2862 foldtst(tnode_t *tn) 2863 { 2864 int l, r = 0; 2865 val_t *v; 2866 2867 v = xcalloc(1, sizeof (val_t)); 2868 v->v_tspec = tn->tn_type->t_tspec; 2869 if (tn->tn_type->t_tspec != INT) 2870 LERROR("foldtst()"); 2871 2872 if (isftyp(tn->tn_left->tn_type->t_tspec)) { 2873 l = tn->tn_left->tn_val->v_ldbl != 0.0; 2874 } else { 2875 l = tn->tn_left->tn_val->v_quad != 0; 2876 } 2877 2878 if (modtab[tn->tn_op].m_binary) { 2879 if (isftyp(tn->tn_right->tn_type->t_tspec)) { 2880 r = tn->tn_right->tn_val->v_ldbl != 0.0; 2881 } else { 2882 r = tn->tn_right->tn_val->v_quad != 0; 2883 } 2884 } 2885 2886 switch (tn->tn_op) { 2887 case NOT: 2888 if (hflag) 2889 /* constant argument to NOT */ 2890 warning(239); 2891 v->v_quad = !l; 2892 break; 2893 case LOGAND: 2894 v->v_quad = l && r; 2895 break; 2896 case LOGOR: 2897 v->v_quad = l || r; 2898 break; 2899 default: 2900 LERROR("foldtst()"); 2901 } 2902 2903 return (getcnode(tn->tn_type, v)); 2904 } 2905 2906 /* 2907 * Same for operands with floating point type. 2908 */ 2909 static tnode_t * 2910 foldflt(tnode_t *tn) 2911 { 2912 val_t *v; 2913 tspec_t t; 2914 ldbl_t l, r = 0; 2915 2916 v = xcalloc(1, sizeof (val_t)); 2917 v->v_tspec = t = tn->tn_type->t_tspec; 2918 2919 if (!isftyp(t)) 2920 LERROR("foldflt()"); 2921 2922 if (t != tn->tn_left->tn_type->t_tspec) 2923 LERROR("foldflt()"); 2924 if (modtab[tn->tn_op].m_binary && t != tn->tn_right->tn_type->t_tspec) 2925 LERROR("foldflt()"); 2926 2927 l = tn->tn_left->tn_val->v_ldbl; 2928 if (modtab[tn->tn_op].m_binary) 2929 r = tn->tn_right->tn_val->v_ldbl; 2930 2931 switch (tn->tn_op) { 2932 case UPLUS: 2933 v->v_ldbl = l; 2934 break; 2935 case UMINUS: 2936 v->v_ldbl = -l; 2937 break; 2938 case MULT: 2939 v->v_ldbl = l * r; 2940 break; 2941 case DIV: 2942 if (r == 0.0) { 2943 /* division by 0 */ 2944 error(139); 2945 if (t == FLOAT) { 2946 v->v_ldbl = l < 0 ? -FLT_MAX : FLT_MAX; 2947 } else if (t == DOUBLE) { 2948 v->v_ldbl = l < 0 ? -DBL_MAX : DBL_MAX; 2949 } else { 2950 v->v_ldbl = l < 0 ? -LDBL_MAX : LDBL_MAX; 2951 } 2952 } else { 2953 v->v_ldbl = l / r; 2954 } 2955 break; 2956 case PLUS: 2957 v->v_ldbl = l + r; 2958 break; 2959 case MINUS: 2960 v->v_ldbl = l - r; 2961 break; 2962 case LT: 2963 v->v_quad = l < r; 2964 break; 2965 case LE: 2966 v->v_quad = l <= r; 2967 break; 2968 case GE: 2969 v->v_quad = l >= r; 2970 break; 2971 case GT: 2972 v->v_quad = l > r; 2973 break; 2974 case EQ: 2975 v->v_quad = l == r; 2976 break; 2977 case NE: 2978 v->v_quad = l != r; 2979 break; 2980 default: 2981 LERROR("foldflt()"); 2982 } 2983 2984 if (isnan((double)v->v_ldbl)) 2985 LERROR("foldflt()"); 2986 if (!finite((double)v->v_ldbl) || 2987 (t == FLOAT && 2988 (v->v_ldbl > FLT_MAX || v->v_ldbl < -FLT_MAX)) || 2989 (t == DOUBLE && 2990 (v->v_ldbl > DBL_MAX || v->v_ldbl < -DBL_MAX))) { 2991 /* floating point overflow detected, op %s */ 2992 warning(142, modtab[tn->tn_op].m_name); 2993 if (t == FLOAT) { 2994 v->v_ldbl = v->v_ldbl < 0 ? -FLT_MAX : FLT_MAX; 2995 } else if (t == DOUBLE) { 2996 v->v_ldbl = v->v_ldbl < 0 ? -DBL_MAX : DBL_MAX; 2997 } else { 2998 v->v_ldbl = v->v_ldbl < 0 ? -LDBL_MAX: LDBL_MAX; 2999 } 3000 } 3001 3002 return (getcnode(tn->tn_type, v)); 3003 } 3004 3005 /* 3006 * Create a constant node for sizeof. 3007 */ 3008 tnode_t * 3009 bldszof(type_t *tp) 3010 { 3011 int elem, elsz; 3012 tspec_t st; 3013 3014 elem = 1; 3015 while (tp->t_tspec == ARRAY) { 3016 elem *= tp->t_dim; 3017 tp = tp->t_subt; 3018 } 3019 if (elem == 0) { 3020 /* cannot take size of incomplete type */ 3021 error(143); 3022 elem = 1; 3023 } 3024 switch (tp->t_tspec) { 3025 case FUNC: 3026 /* cannot take size of function */ 3027 error(144); 3028 elsz = 1; 3029 break; 3030 case STRUCT: 3031 case UNION: 3032 if (incompl(tp)) { 3033 /* cannot take size of incomplete type */ 3034 error(143); 3035 elsz = 1; 3036 } else { 3037 elsz = tp->t_str->size; 3038 } 3039 break; 3040 case ENUM: 3041 if (incompl(tp)) { 3042 /* cannot take size of incomplete type */ 3043 warning(143); 3044 } 3045 /* FALLTHROUGH */ 3046 default: 3047 if (tp->t_isfield) { 3048 /* cannot take size of bit-field */ 3049 error(145); 3050 } 3051 if (tp->t_tspec == VOID) { 3052 /* cannot take size of void */ 3053 error(146); 3054 elsz = 1; 3055 } else { 3056 elsz = size(tp->t_tspec); 3057 if (elsz <= 0) 3058 LERROR("bldszof()"); 3059 } 3060 break; 3061 } 3062 3063 #if SIZEOF_IS_ULONG 3064 st = ULONG; 3065 #else 3066 st = UINT; 3067 #endif 3068 3069 return (getinode(st, (int64_t)(elem * elsz / CHAR_BIT))); 3070 } 3071 3072 /* 3073 * Type casts. 3074 */ 3075 tnode_t * 3076 cast(tnode_t *tn, type_t *tp) 3077 { 3078 tspec_t nt, ot; 3079 3080 if (tn == NULL) 3081 return (NULL); 3082 3083 tn = cconv(tn); 3084 3085 nt = tp->t_tspec; 3086 ot = tn->tn_type->t_tspec; 3087 3088 if (nt == VOID) { 3089 /* 3090 * XXX ANSI C requires scalar types or void (Plauger&Brodie). 3091 * But this seams really questionable. 3092 */ 3093 } else if (nt == STRUCT || nt == UNION || nt == ARRAY || nt == FUNC) { 3094 /* invalid cast expression */ 3095 error(147); 3096 return (NULL); 3097 } else if (ot == STRUCT || ot == UNION) { 3098 /* invalid cast expression */ 3099 error(147); 3100 return (NULL); 3101 } else if (ot == VOID) { 3102 /* improper cast of void expression */ 3103 error(148); 3104 return (NULL); 3105 } else if (isityp(nt) && issclt(ot)) { 3106 /* ok */ 3107 } else if (isftyp(nt) && isatyp(ot)) { 3108 /* ok */ 3109 } else if (nt == PTR && isityp(ot)) { 3110 /* ok */ 3111 } else if (nt == PTR && ot == PTR) { 3112 if (!tp->t_subt->t_const && tn->tn_type->t_subt->t_const) { 3113 if (hflag) 3114 /* cast discards 'const' from ... */ 3115 warning(275); 3116 } 3117 } else { 3118 /* invalid cast expression */ 3119 error(147); 3120 return (NULL); 3121 } 3122 3123 tn = convert(CVT, 0, tp, tn); 3124 tn->tn_cast = 1; 3125 3126 return (tn); 3127 } 3128 3129 /* 3130 * Create the node for a function argument. 3131 * All necessary conversions and type checks are done in funccall(), because 3132 * in funcarg() we have no information about expected argument types. 3133 */ 3134 tnode_t * 3135 funcarg(tnode_t *args, tnode_t *arg) 3136 { 3137 tnode_t *ntn; 3138 3139 /* 3140 * If there was a serious error in the expression for the argument, 3141 * create a dummy argument so the positions of the remaining arguments 3142 * will not change. 3143 */ 3144 if (arg == NULL) 3145 arg = getinode(INT, (int64_t)0); 3146 3147 ntn = mktnode(PUSH, arg->tn_type, arg, args); 3148 3149 return (ntn); 3150 } 3151 3152 /* 3153 * Create the node for a function call. Also check types of 3154 * function arguments and insert conversions, if necessary. 3155 */ 3156 tnode_t * 3157 funccall(tnode_t *func, tnode_t *args) 3158 { 3159 tnode_t *ntn; 3160 op_t fcop; 3161 3162 if (func == NULL) 3163 return (NULL); 3164 3165 if (func->tn_op == NAME && func->tn_type->t_tspec == FUNC) { 3166 fcop = CALL; 3167 } else { 3168 fcop = ICALL; 3169 } 3170 3171 /* 3172 * after cconv() func will always be a pointer to a function 3173 * if it is a valid function designator. 3174 */ 3175 func = cconv(func); 3176 3177 if (func->tn_type->t_tspec != PTR || 3178 func->tn_type->t_subt->t_tspec != FUNC) { 3179 /* illegal function */ 3180 error(149); 3181 return (NULL); 3182 } 3183 3184 args = chkfarg(func->tn_type->t_subt, args); 3185 3186 ntn = mktnode(fcop, func->tn_type->t_subt->t_subt, func, args); 3187 3188 return (ntn); 3189 } 3190 3191 /* 3192 * Check types of all function arguments and insert conversions, 3193 * if necessary. 3194 */ 3195 static tnode_t * 3196 chkfarg(type_t *ftp, tnode_t *args) 3197 { 3198 tnode_t *arg; 3199 sym_t *asym; 3200 tspec_t at; 3201 int narg, npar, n, i; 3202 3203 /* get # of args in the prototype */ 3204 npar = 0; 3205 for (asym = ftp->t_args; asym != NULL; asym = asym->s_nxt) 3206 npar++; 3207 3208 /* get # of args in function call */ 3209 narg = 0; 3210 for (arg = args; arg != NULL; arg = arg->tn_right) 3211 narg++; 3212 3213 asym = ftp->t_args; 3214 if (ftp->t_proto && npar != narg && !(ftp->t_vararg && npar < narg)) { 3215 /* argument mismatch: %d arg%s passed, %d expected */ 3216 error(150, narg, narg > 1 ? "s" : "", npar); 3217 asym = NULL; 3218 } 3219 3220 for (n = 1; n <= narg; n++) { 3221 3222 /* 3223 * The rightmost argument is at the top of the argument 3224 * subtree. 3225 */ 3226 for (i = narg, arg = args; i > n; i--, arg = arg->tn_right) 3227 continue; 3228 3229 /* some things which are always not allowd */ 3230 if ((at = arg->tn_left->tn_type->t_tspec) == VOID) { 3231 /* void expressions may not be arguments, arg #%d */ 3232 error(151, n); 3233 return (NULL); 3234 } else if ((at == STRUCT || at == UNION) && 3235 incompl(arg->tn_left->tn_type)) { 3236 /* argument cannot have unknown size, arg #%d */ 3237 error(152, n); 3238 return (NULL); 3239 } else if (isityp(at) && arg->tn_left->tn_type->t_isenum && 3240 incompl(arg->tn_left->tn_type)) { 3241 /* argument cannot have unknown size, arg #%d */ 3242 warning(152, n); 3243 } 3244 3245 /* class conversions (arg in value context) */ 3246 arg->tn_left = cconv(arg->tn_left); 3247 3248 if (asym != NULL) { 3249 arg->tn_left = parg(n, asym->s_type, arg->tn_left); 3250 } else { 3251 arg->tn_left = promote(NOOP, 1, arg->tn_left); 3252 } 3253 arg->tn_type = arg->tn_left->tn_type; 3254 3255 if (asym != NULL) 3256 asym = asym->s_nxt; 3257 } 3258 3259 return (args); 3260 } 3261 3262 /* 3263 * Compare the type of an argument with the corresponding type of a 3264 * prototype parameter. If it is a valid combination, but both types 3265 * are not the same, insert a conversion to convert the argument into 3266 * the type of the parameter. 3267 */ 3268 static tnode_t * 3269 parg( int n, /* pos of arg */ 3270 type_t *tp, /* expected type (from prototype) */ 3271 tnode_t *tn) /* argument */ 3272 { 3273 tnode_t *ln; 3274 int warn; 3275 3276 ln = xcalloc(1, sizeof (tnode_t)); 3277 ln->tn_type = tduptyp(tp); 3278 ln->tn_type->t_const = 0; 3279 ln->tn_lvalue = 1; 3280 if (typeok(FARG, n, ln, tn)) { 3281 if (!eqtype(tp, tn->tn_type, 1, 0, (warn = 0, &warn)) || warn) 3282 tn = convert(FARG, n, tp, tn); 3283 } 3284 free(ln); 3285 return (tn); 3286 } 3287 3288 /* 3289 * Return the value of an integral constant expression. 3290 * If the expression is not constant or its type is not an integer 3291 * type, an error message is printed. 3292 */ 3293 val_t * 3294 constant(tnode_t *tn, int required) 3295 { 3296 val_t *v; 3297 3298 if (tn != NULL) 3299 tn = cconv(tn); 3300 if (tn != NULL) 3301 tn = promote(NOOP, 0, tn); 3302 3303 v = xcalloc(1, sizeof (val_t)); 3304 3305 if (tn == NULL) { 3306 if (nerr == 0) 3307 LERROR("constant()"); 3308 v->v_tspec = INT; 3309 v->v_quad = 1; 3310 return (v); 3311 } 3312 3313 v->v_tspec = tn->tn_type->t_tspec; 3314 3315 if (tn->tn_op == CON) { 3316 if (tn->tn_type->t_tspec != tn->tn_val->v_tspec) 3317 LERROR("constant()"); 3318 if (isityp(tn->tn_val->v_tspec)) { 3319 v->v_ansiu = tn->tn_val->v_ansiu; 3320 v->v_quad = tn->tn_val->v_quad; 3321 return (v); 3322 } 3323 v->v_quad = tn->tn_val->v_ldbl; 3324 } else { 3325 v->v_quad = 1; 3326 } 3327 3328 /* integral constant expression expected */ 3329 if (required) 3330 error(55); 3331 else 3332 gnuism(318); 3333 3334 if (!isityp(v->v_tspec)) 3335 v->v_tspec = INT; 3336 3337 return (v); 3338 } 3339 3340 /* 3341 * Perform some tests on expressions which can't be done in build() and 3342 * functions called by build(). These tests must be done here because 3343 * we need some information about the context in which the operations 3344 * are performed. 3345 * After all tests are performed, expr() frees the memory which is used 3346 * for the expression. 3347 */ 3348 void 3349 expr(tnode_t *tn, int vctx, int tctx, int freeblk) 3350 { 3351 3352 if (tn == NULL && nerr == 0) 3353 LERROR("expr()"); 3354 3355 if (tn == NULL) { 3356 tfreeblk(); 3357 return; 3358 } 3359 3360 /* expr() is also called in global initialisations */ 3361 if (dcs->d_ctx != EXTERN) 3362 chkreach(); 3363 3364 chkmisc(tn, vctx, tctx, !tctx, 0, 0, 0); 3365 if (tn->tn_op == ASSIGN) { 3366 if (hflag && tctx) 3367 /* assignment in conditional context */ 3368 warning(159); 3369 } else if (tn->tn_op == CON) { 3370 if (hflag && tctx && !ccflg) 3371 /* constant in conditional context */ 3372 warning(161); 3373 } 3374 if (!modtab[tn->tn_op].m_sideeff) { 3375 /* 3376 * for left operands of COMMA this warning is already 3377 * printed 3378 */ 3379 if (tn->tn_op != COMMA && !vctx && !tctx) 3380 nulleff(tn); 3381 } 3382 if (dflag) 3383 displexpr(tn, 0); 3384 3385 /* free the tree memory */ 3386 if (freeblk) 3387 tfreeblk(); 3388 } 3389 3390 static void 3391 nulleff(tnode_t *tn) 3392 { 3393 3394 if (!hflag) 3395 return; 3396 3397 while (!modtab[tn->tn_op].m_sideeff) { 3398 if (tn->tn_op == CVT && tn->tn_type->t_tspec == VOID) { 3399 tn = tn->tn_left; 3400 } else if (tn->tn_op == LOGAND || tn->tn_op == LOGOR) { 3401 /* 3402 * && and || have a side effect if the right operand 3403 * has a side effect. 3404 */ 3405 tn = tn->tn_right; 3406 } else if (tn->tn_op == QUEST) { 3407 /* 3408 * ? has a side effect if at least one of its right 3409 * operands has a side effect 3410 */ 3411 tn = tn->tn_right; 3412 } else if (tn->tn_op == COLON || tn->tn_op == COMMA) { 3413 /* 3414 * : has a side effect if at least one of its operands 3415 * has a side effect 3416 */ 3417 if (modtab[tn->tn_left->tn_op].m_sideeff) { 3418 tn = tn->tn_left; 3419 } else if (modtab[tn->tn_right->tn_op].m_sideeff) { 3420 tn = tn->tn_right; 3421 } else { 3422 break; 3423 } 3424 } else { 3425 break; 3426 } 3427 } 3428 if (!modtab[tn->tn_op].m_sideeff) 3429 /* expression has null effect */ 3430 warning(129); 3431 } 3432 3433 /* 3434 * Dump an expression to stdout 3435 * only used for debugging 3436 */ 3437 static void 3438 displexpr(tnode_t *tn, int offs) 3439 { 3440 uint64_t uq; 3441 3442 if (tn == NULL) { 3443 (void)printf("%*s%s\n", offs, "", "NULL"); 3444 return; 3445 } 3446 (void)printf("%*sop %s ", offs, "", modtab[tn->tn_op].m_name); 3447 3448 if (tn->tn_op == NAME) { 3449 (void)printf("%s: %s ", 3450 tn->tn_sym->s_name, scltoa(tn->tn_sym->s_scl)); 3451 } else if (tn->tn_op == CON && isftyp(tn->tn_type->t_tspec)) { 3452 (void)printf("%#g ", (double)tn->tn_val->v_ldbl); 3453 } else if (tn->tn_op == CON && isityp(tn->tn_type->t_tspec)) { 3454 uq = tn->tn_val->v_quad; 3455 (void)printf("0x %08lx %08lx ", (long)(uq >> 32) & 0xffffffffl, 3456 (long)uq & 0xffffffffl); 3457 } else if (tn->tn_op == CON) { 3458 if (tn->tn_type->t_tspec != PTR) 3459 LERROR("displexpr()"); 3460 (void)printf("0x%0*lx ", (int)(sizeof (void *) * CHAR_BIT / 4), 3461 (u_long)tn->tn_val->v_quad); 3462 } else if (tn->tn_op == STRING) { 3463 if (tn->tn_strg->st_tspec == CHAR) { 3464 (void)printf("\"%s\"", tn->tn_strg->st_cp); 3465 } else { 3466 char *s; 3467 size_t n; 3468 n = MB_CUR_MAX * (tn->tn_strg->st_len + 1); 3469 s = xmalloc(n); 3470 (void)wcstombs(s, tn->tn_strg->st_wcp, n); 3471 (void)printf("L\"%s\"", s); 3472 free(s); 3473 } 3474 (void)printf(" "); 3475 } else if (tn->tn_op == FSEL) { 3476 (void)printf("o=%d, l=%d ", tn->tn_type->t_foffs, 3477 tn->tn_type->t_flen); 3478 } 3479 (void)printf("%s\n", ttos(tn->tn_type)); 3480 if (tn->tn_op == NAME || tn->tn_op == CON || tn->tn_op == STRING) 3481 return; 3482 displexpr(tn->tn_left, offs + 2); 3483 if (modtab[tn->tn_op].m_binary || 3484 (tn->tn_op == PUSH && tn->tn_right != NULL)) { 3485 displexpr(tn->tn_right, offs + 2); 3486 } 3487 } 3488 3489 /* 3490 * Called by expr() to recursively perform some tests. 3491 */ 3492 /* ARGSUSED */ 3493 void 3494 chkmisc(tnode_t *tn, int vctx, int tctx, int eqwarn, int fcall, int rvdisc, 3495 int szof) 3496 { 3497 tnode_t *ln, *rn; 3498 mod_t *mp; 3499 int nrvdisc, cvctx, ctctx; 3500 op_t op; 3501 scl_t sc; 3502 dinfo_t *di; 3503 3504 if (tn == NULL) 3505 return; 3506 3507 ln = tn->tn_left; 3508 rn = tn->tn_right; 3509 mp = &modtab[op = tn->tn_op]; 3510 3511 switch (op) { 3512 case AMPER: 3513 if (ln->tn_op == NAME && (reached || rchflg)) { 3514 if (!szof) 3515 setsflg(ln->tn_sym); 3516 setuflg(ln->tn_sym, fcall, szof); 3517 } 3518 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS) 3519 /* check the range of array indices */ 3520 chkaidx(ln->tn_left, 1); 3521 break; 3522 case LOAD: 3523 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS) 3524 /* check the range of array indices */ 3525 chkaidx(ln->tn_left, 0); 3526 /* FALLTHROUGH */ 3527 case PUSH: 3528 case INCBEF: 3529 case DECBEF: 3530 case INCAFT: 3531 case DECAFT: 3532 case ADDASS: 3533 case SUBASS: 3534 case MULASS: 3535 case DIVASS: 3536 case MODASS: 3537 case ANDASS: 3538 case ORASS: 3539 case XORASS: 3540 case SHLASS: 3541 case SHRASS: 3542 if (ln->tn_op == NAME && (reached || rchflg)) { 3543 sc = ln->tn_sym->s_scl; 3544 /* 3545 * Look if there was a asm statement in one of the 3546 * compound statements we are in. If not, we don't 3547 * print a warning. 3548 */ 3549 for (di = dcs; di != NULL; di = di->d_nxt) { 3550 if (di->d_asm) 3551 break; 3552 } 3553 if (sc != EXTERN && sc != STATIC && 3554 !ln->tn_sym->s_set && !szof && di == NULL) { 3555 /* %s may be used before set */ 3556 warning(158, ln->tn_sym->s_name); 3557 setsflg(ln->tn_sym); 3558 } 3559 setuflg(ln->tn_sym, 0, 0); 3560 } 3561 break; 3562 case ASSIGN: 3563 if (ln->tn_op == NAME && !szof && (reached || rchflg)) { 3564 setsflg(ln->tn_sym); 3565 if (ln->tn_sym->s_scl == EXTERN) 3566 outusg(ln->tn_sym); 3567 } 3568 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS) 3569 /* check the range of array indices */ 3570 chkaidx(ln->tn_left, 0); 3571 break; 3572 case CALL: 3573 if (ln->tn_op != AMPER || ln->tn_left->tn_op != NAME) 3574 LERROR("chkmisc()"); 3575 if (!szof) 3576 outcall(tn, vctx || tctx, rvdisc); 3577 break; 3578 case EQ: 3579 /* equality operator "==" found where "=" was exp. */ 3580 if (hflag && eqwarn) 3581 warning(160); 3582 break; 3583 case CON: 3584 case NAME: 3585 case STRING: 3586 return; 3587 /* LINTED (enumeration values not handled in switch) */ 3588 case OR: 3589 case XOR: 3590 case NE: 3591 case GE: 3592 case GT: 3593 case LE: 3594 case LT: 3595 case SHR: 3596 case SHL: 3597 case MINUS: 3598 case PLUS: 3599 case MOD: 3600 case DIV: 3601 case MULT: 3602 case STAR: 3603 case UMINUS: 3604 case UPLUS: 3605 case DEC: 3606 case INC: 3607 case COMPL: 3608 case NOT: 3609 case POINT: 3610 case ARROW: 3611 case NOOP: 3612 case AND: 3613 case FARG: 3614 case CASE: 3615 case INIT: 3616 case RETURN: 3617 case ICALL: 3618 case CVT: 3619 case COMMA: 3620 case FSEL: 3621 case COLON: 3622 case QUEST: 3623 case LOGOR: 3624 case LOGAND: 3625 break; 3626 } 3627 3628 cvctx = mp->m_vctx; 3629 ctctx = mp->m_tctx; 3630 /* 3631 * values of operands of ':' are not used if the type of at least 3632 * one of the operands (for gcc compatibility) is void 3633 * XXX test/value context of QUEST should probably be used as 3634 * context for both operands of COLON 3635 */ 3636 if (op == COLON && tn->tn_type->t_tspec == VOID) 3637 cvctx = ctctx = 0; 3638 nrvdisc = op == CVT && tn->tn_type->t_tspec == VOID; 3639 chkmisc(ln, cvctx, ctctx, mp->m_eqwarn, op == CALL, nrvdisc, szof); 3640 3641 switch (op) { 3642 case PUSH: 3643 if (rn != NULL) 3644 chkmisc(rn, 0, 0, mp->m_eqwarn, 0, 0, szof); 3645 break; 3646 case LOGAND: 3647 case LOGOR: 3648 chkmisc(rn, 0, 1, mp->m_eqwarn, 0, 0, szof); 3649 break; 3650 case COLON: 3651 chkmisc(rn, cvctx, ctctx, mp->m_eqwarn, 0, 0, szof); 3652 break; 3653 case COMMA: 3654 chkmisc(rn, vctx, tctx, mp->m_eqwarn, 0, 0, szof); 3655 break; 3656 default: 3657 if (mp->m_binary) 3658 chkmisc(rn, 1, 0, mp->m_eqwarn, 0, 0, szof); 3659 break; 3660 } 3661 3662 } 3663 3664 /* 3665 * Checks the range of array indices, if possible. 3666 * amper is set if only the address of the element is used. This 3667 * means that the index is allowd to refere to the first element 3668 * after the array. 3669 */ 3670 static void 3671 chkaidx(tnode_t *tn, int amper) 3672 { 3673 int dim; 3674 tnode_t *ln, *rn; 3675 int elsz; 3676 int64_t con; 3677 3678 ln = tn->tn_left; 3679 rn = tn->tn_right; 3680 3681 /* We can only check constant indices. */ 3682 if (rn->tn_op != CON) 3683 return; 3684 3685 /* Return if the left node does not stem from an array. */ 3686 if (ln->tn_op != AMPER) 3687 return; 3688 if (ln->tn_left->tn_op != STRING && ln->tn_left->tn_op != NAME) 3689 return; 3690 if (ln->tn_left->tn_type->t_tspec != ARRAY) 3691 return; 3692 3693 /* 3694 * For incomplete array types, we can print a warning only if 3695 * the index is negative. 3696 */ 3697 if (incompl(ln->tn_left->tn_type) && rn->tn_val->v_quad >= 0) 3698 return; 3699 3700 /* Get the size of one array element */ 3701 if ((elsz = length(ln->tn_type->t_subt, NULL)) == 0) 3702 return; 3703 elsz /= CHAR_BIT; 3704 3705 /* Change the unit of the index from bytes to element size. */ 3706 if (isutyp(rn->tn_type->t_tspec)) { 3707 con = (uint64_t)rn->tn_val->v_quad / elsz; 3708 } else { 3709 con = rn->tn_val->v_quad / elsz; 3710 } 3711 3712 dim = ln->tn_left->tn_type->t_dim + (amper ? 1 : 0); 3713 3714 if (!isutyp(rn->tn_type->t_tspec) && con < 0) { 3715 /* array subscript cannot be negative: %ld */ 3716 warning(167, (long)con); 3717 } else if (dim > 0 && (uint64_t)con >= dim) { 3718 /* array subscript cannot be > %d: %ld */ 3719 warning(168, dim - 1, (long)con); 3720 } 3721 } 3722 3723 /* 3724 * Check for ordered comparisons of unsigned values with 0. 3725 */ 3726 static void 3727 chkcomp(op_t op, tnode_t *ln, tnode_t *rn) 3728 { 3729 char buf[64]; 3730 tspec_t lt, rt; 3731 mod_t *mp; 3732 3733 lt = ln->tn_type->t_tspec; 3734 rt = rn->tn_type->t_tspec; 3735 mp = &modtab[op]; 3736 3737 if (ln->tn_op != CON && rn->tn_op != CON) 3738 return; 3739 3740 if (!isityp(lt) || !isityp(rt)) 3741 return; 3742 3743 if ((hflag || pflag) && lt == CHAR && rn->tn_op == CON && 3744 (rn->tn_val->v_quad < 0 || 3745 rn->tn_val->v_quad > ~(~0 << (CHAR_BIT - 1)))) { 3746 /* nonportable character comparison, op %s */ 3747 warning(230, mp->m_name); 3748 return; 3749 } 3750 if ((hflag || pflag) && rt == CHAR && ln->tn_op == CON && 3751 (ln->tn_val->v_quad < 0 || 3752 ln->tn_val->v_quad > ~(~0 << (CHAR_BIT - 1)))) { 3753 /* nonportable character comparison, op %s */ 3754 warning(230, mp->m_name); 3755 return; 3756 } 3757 if (isutyp(lt) && !isutyp(rt) && 3758 rn->tn_op == CON && rn->tn_val->v_quad <= 0) { 3759 if (rn->tn_val->v_quad < 0) { 3760 /* comparison of %s with %s, op %s */ 3761 warning(162, tyname(buf, sizeof(buf), ln->tn_type), 3762 "negative constant", mp->m_name); 3763 } else if (op == LT || op == GE || (hflag && op == LE)) { 3764 /* comparison of %s with %s, op %s */ 3765 warning(162, tyname(buf, sizeof(buf), ln->tn_type), 3766 "0", mp->m_name); 3767 } 3768 return; 3769 } 3770 if (isutyp(rt) && !isutyp(lt) && 3771 ln->tn_op == CON && ln->tn_val->v_quad <= 0) { 3772 if (ln->tn_val->v_quad < 0) { 3773 /* comparison of %s with %s, op %s */ 3774 warning(162, "negative constant", 3775 tyname(buf, sizeof(buf), rn->tn_type), mp->m_name); 3776 } else if (op == GT || op == LE || (hflag && op == GE)) { 3777 /* comparison of %s with %s, op %s */ 3778 warning(162, "0", tyname(buf, sizeof(buf), rn->tn_type), 3779 mp->m_name); 3780 } 3781 return; 3782 } 3783 } 3784 3785 /* 3786 * Takes an expression an returns 0 if this expression can be used 3787 * for static initialisation, otherwise -1. 3788 * 3789 * Constant initialisation expressions must be costant or an address 3790 * of a static object with an optional offset. In the first case, 3791 * the result is returned in *offsp. In the second case, the static 3792 * object is returned in *symp and the offset in *offsp. 3793 * 3794 * The expression can consist of PLUS, MINUS, AMPER, NAME, STRING and 3795 * CON. Type conversions are allowed if they do not change binary 3796 * representation (including width). 3797 */ 3798 int 3799 conaddr(tnode_t *tn, sym_t **symp, ptrdiff_t *offsp) 3800 { 3801 sym_t *sym; 3802 ptrdiff_t offs1, offs2; 3803 tspec_t t, ot; 3804 3805 switch (tn->tn_op) { 3806 case MINUS: 3807 if (tn->tn_right->tn_op == CVT) 3808 return conaddr(tn->tn_right, symp, offsp); 3809 else if (tn->tn_right->tn_op != CON) 3810 return (-1); 3811 /* FALLTHROUGH */ 3812 case PLUS: 3813 offs1 = offs2 = 0; 3814 if (tn->tn_left->tn_op == CON) { 3815 offs1 = (ptrdiff_t)tn->tn_left->tn_val->v_quad; 3816 if (conaddr(tn->tn_right, &sym, &offs2) == -1) 3817 return (-1); 3818 } else if (tn->tn_right->tn_op == CON) { 3819 offs2 = (ptrdiff_t)tn->tn_right->tn_val->v_quad; 3820 if (tn->tn_op == MINUS) 3821 offs2 = -offs2; 3822 if (conaddr(tn->tn_left, &sym, &offs1) == -1) 3823 return (-1); 3824 } else { 3825 return (-1); 3826 } 3827 *symp = sym; 3828 *offsp = offs1 + offs2; 3829 break; 3830 case AMPER: 3831 if (tn->tn_left->tn_op == NAME) { 3832 *symp = tn->tn_left->tn_sym; 3833 *offsp = 0; 3834 } else if (tn->tn_left->tn_op == STRING) { 3835 /* 3836 * If this would be the front end of a compiler we 3837 * would return a label instead of 0. 3838 */ 3839 *offsp = 0; 3840 } 3841 break; 3842 case CVT: 3843 t = tn->tn_type->t_tspec; 3844 ot = tn->tn_left->tn_type->t_tspec; 3845 if ((!isityp(t) && t != PTR) || (!isityp(ot) && ot != PTR)) 3846 return (-1); 3847 #ifdef notdef 3848 /* 3849 * consider: 3850 * struct foo { 3851 * unsigned char a; 3852 * } f = { 3853 * (u_char)(u_long)(&(((struct foo *)0)->a)) 3854 * }; 3855 * since psize(u_long) != psize(u_char) this fails. 3856 */ 3857 else if (psize(t) != psize(ot)) 3858 return (-1); 3859 #endif 3860 if (conaddr(tn->tn_left, symp, offsp) == -1) 3861 return (-1); 3862 break; 3863 default: 3864 return (-1); 3865 } 3866 return (0); 3867 } 3868 3869 /* 3870 * Concatenate two string constants. 3871 */ 3872 strg_t * 3873 catstrg(strg_t *strg1, strg_t *strg2) 3874 { 3875 size_t len1, len2, len; 3876 3877 if (strg1->st_tspec != strg2->st_tspec) { 3878 /* cannot concatenate wide and regular string literals */ 3879 error(292); 3880 return (strg1); 3881 } 3882 3883 len = (len1 = strg1->st_len) + (len2 = strg2->st_len); 3884 3885 if (strg1->st_tspec == CHAR) { 3886 strg1->st_cp = xrealloc(strg1->st_cp, len + 1); 3887 (void)memcpy(strg1->st_cp + len1, strg2->st_cp, len2 + 1); 3888 free(strg2->st_cp); 3889 } else { 3890 strg1->st_wcp = xrealloc(strg1->st_wcp, 3891 (len + 1) * sizeof (wchar_t)); 3892 (void)memcpy(strg1->st_wcp + len1, strg2->st_wcp, 3893 (len2 + 1) * sizeof (wchar_t)); 3894 free(strg2->st_wcp); 3895 } 3896 free(strg2); 3897 3898 return (strg1); 3899 } 3900 3901 /* 3902 * Print a warning if the given node has operands which should be 3903 * parenthesized. 3904 * 3905 * XXX Does not work if an operand is a constant expression. Constant 3906 * expressions are already folded. 3907 */ 3908 static void 3909 precconf(tnode_t *tn) 3910 { 3911 tnode_t *ln, *rn; 3912 op_t lop, rop = NOOP; 3913 int lparn, rparn = 0; 3914 mod_t *mp; 3915 int warn; 3916 3917 if (!hflag) 3918 return; 3919 3920 mp = &modtab[tn->tn_op]; 3921 3922 lparn = 0; 3923 for (ln = tn->tn_left; ln->tn_op == CVT; ln = ln->tn_left) 3924 lparn |= ln->tn_parn; 3925 lparn |= ln->tn_parn; 3926 lop = ln->tn_op; 3927 3928 if (mp->m_binary) { 3929 rparn = 0; 3930 for (rn = tn->tn_right; tn->tn_op == CVT; rn = rn->tn_left) 3931 rparn |= rn->tn_parn; 3932 rparn |= rn->tn_parn; 3933 rop = rn->tn_op; 3934 } 3935 3936 warn = 0; 3937 3938 switch (tn->tn_op) { 3939 case SHL: 3940 case SHR: 3941 if (!lparn && (lop == PLUS || lop == MINUS)) { 3942 warn = 1; 3943 } else if (!rparn && (rop == PLUS || rop == MINUS)) { 3944 warn = 1; 3945 } 3946 break; 3947 case LOGOR: 3948 if (!lparn && lop == LOGAND) { 3949 warn = 1; 3950 } else if (!rparn && rop == LOGAND) { 3951 warn = 1; 3952 } 3953 break; 3954 case AND: 3955 case XOR: 3956 case OR: 3957 if (!lparn && lop != tn->tn_op) { 3958 if (lop == PLUS || lop == MINUS) { 3959 warn = 1; 3960 } else if (lop == AND || lop == XOR) { 3961 warn = 1; 3962 } 3963 } 3964 if (!warn && !rparn && rop != tn->tn_op) { 3965 if (rop == PLUS || rop == MINUS) { 3966 warn = 1; 3967 } else if (rop == AND || rop == XOR) { 3968 warn = 1; 3969 } 3970 } 3971 break; 3972 /* LINTED (enumeration values not handled in switch) */ 3973 case DECAFT: 3974 case XORASS: 3975 case SHLASS: 3976 case NOOP: 3977 case ARROW: 3978 case ORASS: 3979 case POINT: 3980 case NAME: 3981 case NOT: 3982 case COMPL: 3983 case CON: 3984 case INC: 3985 case STRING: 3986 case DEC: 3987 case INCBEF: 3988 case DECBEF: 3989 case INCAFT: 3990 case FSEL: 3991 case CALL: 3992 case COMMA: 3993 case CVT: 3994 case ICALL: 3995 case LOAD: 3996 case PUSH: 3997 case RETURN: 3998 case INIT: 3999 case CASE: 4000 case FARG: 4001 case SUBASS: 4002 case ADDASS: 4003 case MODASS: 4004 case DIVASS: 4005 case MULASS: 4006 case ASSIGN: 4007 case COLON: 4008 case QUEST: 4009 case LOGAND: 4010 case NE: 4011 case EQ: 4012 case GE: 4013 case GT: 4014 case LE: 4015 case LT: 4016 case MINUS: 4017 case PLUS: 4018 case MOD: 4019 case DIV: 4020 case MULT: 4021 case AMPER: 4022 case STAR: 4023 case UMINUS: 4024 case SHRASS: 4025 case UPLUS: 4026 case ANDASS: 4027 break; 4028 } 4029 4030 if (warn) { 4031 /* precedence confusion possible: parenthesize! */ 4032 warning(169); 4033 } 4034 4035 } 4036