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