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