expr.c (22032B)
1 #include <assert.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 #include <scc/cstd.h> 6 #include <scc/scc.h> 7 #include "cc1.h" 8 9 #define XCHG(lp, rp, np) (np = lp, lp = rp, rp = np) 10 11 int 12 power2node(Node *np, int *log) 13 { 14 int n; 15 unsigned long long u; 16 Symbol *sym; 17 18 if (!np || !(np->flags & NCONST) || !np->sym) 19 return 0; 20 21 sym = np->sym; 22 if (sym->type->op != INT || (u = sym->u.u) == 0) 23 return 0; 24 25 for (n = 0; (u & 1) == 0; n++) 26 u >>= 1; 27 28 if (log) 29 *log = n; 30 31 return u == 1; 32 } 33 34 int 35 cmpnode(Node *np, unsigned long long val) 36 { 37 Symbol *sym; 38 Type *tp; 39 unsigned long long mask, nodeval; 40 41 if (!np || !(np->flags & NCONST) || !np->sym) 42 return 0; 43 sym = np->sym; 44 tp = sym->type; 45 46 switch (tp->op) { 47 case ENUM: 48 case PTR: 49 case INT: 50 mask = (val > 1) ? ones(np->type->size) : -1; 51 nodeval = (tp->prop & TSIGNED) ? sym->u.i : sym->u.u; 52 return (nodeval & mask) == (val & mask); 53 default: 54 abort(); 55 } 56 return 0; 57 } 58 59 static Node * 60 promote(Node *np) 61 { 62 Type *tp; 63 Node *new; 64 struct limits *lim, *ilim; 65 66 tp = np->type; 67 68 switch (tp->op) { 69 case ENUM: 70 case INT: 71 if (tp->n.rank >= inttype->n.rank) 72 return np; 73 lim = getlimits(tp); 74 ilim = getlimits(inttype); 75 tp = (lim->max.i <= ilim->max.i) ? inttype : uinttype; 76 break; 77 case FLOAT: 78 return np; 79 default: 80 abort(); 81 } 82 if ((new = convert(np, tp, 1)) != NULL) 83 return new; 84 return np; 85 } 86 87 static void 88 arithconv(Node **p1, Node **p2) 89 { 90 int to = 0, s1, s2; 91 unsigned r1, r2; 92 Type *tp1, *tp2; 93 Node *np1, *np2; 94 struct limits *lp1, *lp2; 95 96 np1 = promote(*p1); 97 np2 = promote(*p2); 98 99 tp1 = np1->type; 100 tp2 = np2->type; 101 102 if (tp1 == tp2) 103 goto set_p1_p2; 104 105 s1 = (tp1->prop & TSIGNED) != 0; 106 r1 = tp1->n.rank; 107 lp1 = getlimits(tp1); 108 109 s2 = (tp2->prop & TSIGNED) != 0; 110 r2 = tp2->n.rank; 111 lp2 = getlimits(tp2); 112 113 if (s1 == s2 || tp1->op == FLOAT || tp2->op == FLOAT) { 114 to = r1 - r2; 115 } else if (!s1) { 116 if (r1 >= r2 || lp1->max.i >= lp2->max.i) 117 to = 1; 118 else 119 to = -1; 120 } else { 121 if (r2 >= r1 || lp2->max.i >= lp1->max.i) 122 to = -1; 123 else 124 to = 1; 125 } 126 127 if (to > 0) 128 np2 = convert(np2, tp1, 1); 129 else if (to < 0) 130 np1 = convert(np1, tp2, 1); 131 132 set_p1_p2: 133 *p1 = np1; 134 *p2 = np2; 135 } 136 137 static int 138 null(Node *np) 139 { 140 if (np->type != pvoidtype) 141 return 0; 142 143 switch (np->op) { 144 case OCAST: 145 np = np->left; 146 if (np->type != inttype) 147 return 0; 148 case OSYM: 149 return cmpnode(np, 0); 150 default: 151 return 0; 152 } 153 } 154 155 static Node * 156 chkternary(Node *yes, Node *no) 157 { 158 /* 159 * FIXME: 160 * We are ignoring type qualifiers here, 161 * but the standard has strong rules about this. 162 * take a look to 6.5.15 163 */ 164 165 if (!eqtype(yes->type, no->type, EQUIV)) { 166 if ((yes->type->prop & TARITH) && (no->type->prop & TARITH)) { 167 arithconv(&yes, &no); 168 } else if (yes->type->op != PTR && no->type->op != PTR) { 169 goto wrong_type; 170 } else { 171 /* convert integer 0 to NULL */ 172 if ((yes->type->prop & TINTEGER) && cmpnode(yes, 0)) 173 yes = convert(yes, pvoidtype, 0); 174 if ((no->type->prop & TINTEGER) && cmpnode(no, 0)) 175 no = convert(no, pvoidtype, 0); 176 /* 177 * At this point the type of both should be 178 * a pointer to something, or we have don't 179 * compatible types 180 */ 181 if (yes->type->op != PTR || no->type->op != PTR) 182 goto wrong_type; 183 /* 184 * If we have a null pointer constant then 185 * convert to the another type 186 */ 187 if (null(yes)) 188 yes = convert(yes, no->type, 0); 189 if (null(no)) 190 no = convert(no, yes->type, 0); 191 192 if (!eqtype(yes->type, no->type, EQUIV)) 193 goto wrong_type; 194 } 195 } 196 return node(OCOLON, yes->type, yes, no); 197 198 wrong_type: 199 errorp("type mismatch in conditional expression"); 200 freetree(yes); 201 freetree(no); 202 return constnode(zero); 203 } 204 205 static void 206 chklvalue(Node *np) 207 { 208 if (!(np->flags & NLVAL)) 209 errorp("lvalue required in operation"); 210 if (np->type == voidtype) 211 errorp("invalid use of void expression"); 212 } 213 214 static Node * 215 chkconstaddr(Node *var, Node *addr) 216 { 217 unsigned mask = SGLOBAL|SLOCAL|SPRIVATE|SEXTERN; 218 219 if (var->sym && var->sym->flags & mask 220 || var->op == OFIELD && var->left->op == OSYM 221 || var->op == OFIELD && (var->left->flags & NCONST)) { 222 addr->flags |= NCONST; 223 } 224 225 return addr; 226 } 227 228 Node * 229 decay(Node *np) 230 { 231 Node *new; 232 Type *tp = np->type; 233 234 switch (tp->op) { 235 case ARY: 236 DBG("EXPR decay ary"); 237 tp = tp->type; 238 if (np->op != OPTR) 239 goto new_node; 240 new = np->left; 241 free(np); 242 new->type = mktype(tp, PTR, 0, NULL); 243 return chkconstaddr(new, new); 244 case FTN: 245 DBG("EXPR decay function"); 246 new_node: 247 new = node(OADDR, mktype(tp, PTR, 0, NULL), np, NULL); 248 new->flags |= NDECAY; 249 return chkconstaddr(np, new); 250 default: 251 return np; 252 } 253 } 254 255 static Node * 256 integerop(int op, Node *lp, Node *rp) 257 { 258 if (!(lp->type->prop & TINTEGER) || !(rp->type->prop & TINTEGER)) 259 error("operator requires integer operands"); 260 261 262 switch (op) { 263 case OA_MOD: 264 case OA_SHL: 265 case OA_SHR: 266 case OA_AND: 267 case OA_XOR: 268 case OA_OR: 269 rp = convert(rp, lp->type, 0); 270 break; 271 default: 272 arithconv(&lp, &rp); 273 break; 274 } 275 276 return node(op, lp->type, lp, rp); 277 } 278 279 static Node * 280 integeruop(int op, Node *np) 281 { 282 if (!(np->type->prop & TINTEGER)) 283 error("unary operator requires integer operand"); 284 np = promote(np); 285 return node(op, np->type, np, NULL); 286 } 287 288 static Node * 289 numericaluop(int op, Node *np) 290 { 291 if (!(np->type->prop & TARITH)) 292 error("unary operator requires numerical operand"); 293 np = promote(np); 294 return node(op, np->type, np, NULL); 295 } 296 297 Node * 298 convert(Node *np, Type *newtp, int iscast) 299 { 300 Type *oldtp = np->type; 301 int op = newtp->op; 302 303 if (eqtype(newtp, oldtp, EQUAL)) 304 return np; 305 if (iscast && op == VOID) 306 goto good_conv; 307 308 switch (oldtp->op) { 309 case ENUM: 310 case INT: 311 if (op == PTR && (iscast || cmpnode(np, 0))) 312 goto good_conv; 313 case FLOAT: 314 if (op == INT || op == FLOAT || op == ENUM) 315 goto good_conv; 316 return NULL; 317 case PTR: 318 if (op == ENUM || op == INT) { 319 if (iscast) 320 goto good_conv; 321 } else if (op == PTR) { 322 if (eqtype(newtp, oldtp, EQUIV)) 323 goto good_ptr_conv; 324 if (iscast) 325 goto good_ptr_conv; 326 if (newtp == pvoidtype || oldtp == pvoidtype) 327 goto good_ptr_conv; 328 } 329 default: 330 return NULL; 331 } 332 333 good_ptr_conv: 334 np->type = newtp; 335 return np; 336 337 good_conv: 338 return node(OCAST, newtp, np, NULL); 339 } 340 341 static Node * 342 parithmetic(int op, Node *lp, Node *rp) 343 { 344 Type *tp; 345 Node *size, *np; 346 347 if (lp->type->op != PTR) 348 XCHG(lp, rp, np); 349 350 tp = rp->type; 351 if (tp->op == PTR && !(tp->type->prop & TDEFINED)) 352 goto incomplete; 353 tp = lp->type; 354 if (!(tp->type->prop & TDEFINED)) 355 goto incomplete; 356 size = sizeofnode(tp->type); 357 358 if (op == OSUB && rp->type->op == PTR) { 359 if ((rp = convert(rp, lp->type, 0)) == NULL) 360 goto incorrect; 361 lp = node(OSUB, pdifftype, lp, rp); 362 return node(ODIV, pdifftype, lp, size); 363 } 364 if (!(rp->type->prop & TINTEGER)) 365 goto incorrect; 366 367 rp = convert(promote(rp), sizettype, 0); 368 rp = node(OMUL, sizettype, rp, size); 369 rp = convert(rp, tp, 1); 370 371 return node(op, tp, lp, rp); 372 373 incomplete: 374 errorp("invalid use of undefined type"); 375 return lp; 376 incorrect: 377 errorp("incorrect arithmetic operands"); 378 return lp; 379 380 } 381 382 static Node * 383 arithmetic(int op, Node *lp, Node *rp) 384 { 385 Node *np; 386 Type *ltp = lp->type, *rtp = rp->type; 387 388 if ((ltp->prop & TARITH) && (rtp->prop & TARITH)) { 389 switch (op) { 390 case OA_ADD: 391 case OA_SUB: 392 case OA_MUL: 393 case OA_DIV: 394 rp = convert(rp, lp->type, 0); 395 break; 396 default: 397 arithconv(&lp, &rp); 398 break; 399 } 400 return node(op, lp->type, lp, rp); 401 } else if ((ltp->op == PTR || rtp->op == PTR)) { 402 switch (op) { 403 case OADD: 404 case OSUB: 405 case OA_ADD: 406 case OA_SUB: 407 case OINC: 408 case ODEC: 409 np = parithmetic(op, lp, rp); 410 if ((lp->flags&NCONST) && (rp->flags&NCONST)) 411 np->flags |= NCONST; 412 return np; 413 } 414 } 415 errorp("incorrect arithmetic operands"); 416 return lp; 417 } 418 419 static Node * 420 pcompare(int op, Node *lp, Node *rp) 421 { 422 Node *np; 423 424 if (lp->type->prop&TINTEGER) { 425 if ((np = convert(lp, rp->type, 0)) == NULL) 426 errorp("incompatible types in comparison"); 427 else 428 lp = np; 429 } 430 if (rp->type->prop&TINTEGER) { 431 if ((np = convert(rp, lp->type, 0)) == NULL) 432 errorp("incompatible types in comparison"); 433 else 434 rp = np; 435 } 436 437 return convert(node(op, pvoidtype, lp, rp), inttype, 1); 438 } 439 440 static Node * 441 compare(int op, Node *lp, Node *rp) 442 { 443 Type *ltp, *rtp; 444 445 ltp = lp->type; 446 rtp = rp->type; 447 448 if (ltp->op == PTR || rtp->op == PTR) { 449 return pcompare(op, lp, rp); 450 } else if ((ltp->prop & TARITH) && (rtp->prop & TARITH)) { 451 arithconv(&lp, &rp); 452 return convert(node(op, lp->type, lp, rp), inttype, 1); 453 } else { 454 errorp("incompatible types in comparison"); 455 freetree(lp); 456 freetree(rp); 457 return constnode(zero); 458 } 459 } 460 461 int 462 negop(int op) 463 { 464 switch (op) { 465 case OEQ: return ONE; 466 case ONE: return OEQ; 467 case OLT: return OGE; 468 case OGE: return OLT; 469 case OLE: return OGT; 470 case OGT: return OLE; 471 default: abort(); 472 } 473 return op; 474 } 475 476 static Node * 477 exp2cond(Node *np, int neg) 478 { 479 int op; 480 481 if (np->type->prop & TAGGREG) { 482 errorp("used struct/union type value where scalar is required"); 483 return constnode(zero); 484 } 485 switch (np->op) { 486 case ONEG: 487 case OOR: 488 case OAND: 489 return (neg) ? node(ONEG, inttype, np, NULL) : np; 490 case OEQ: 491 case ONE: 492 case OLT: 493 case OGE: 494 case OLE: 495 case OGT: 496 if (neg) 497 np->op = negop(np->op); 498 return np; 499 default: 500 op = (neg) ? OEQ : ONE; 501 return compare(op, np, constnode(zero)); 502 } 503 } 504 505 static Node * 506 logic(int op, Node *lp, Node *rp) 507 { 508 lp = exp2cond(lp, 0); 509 rp = exp2cond(rp, 0); 510 return node(op, inttype, lp, rp); 511 } 512 513 static Node * 514 field(Node *np) 515 { 516 Symbol *sym; 517 518 namespace = np->type->ns; 519 next(); 520 namespace = NS_IDEN; 521 522 sym = yylval.sym; 523 if (yytoken != IDEN) 524 unexpected(); 525 next(); 526 527 if (!(np->type->prop & TAGGREG)) { 528 errorp("request for member '%s' in something not a structure or union", 529 yylval.sym->name); 530 goto free_np; 531 } 532 if ((sym->flags & SDECLARED) == 0) { 533 errorp("incorrect field in struct/union"); 534 goto free_np; 535 } 536 np = node(OFIELD, sym->type, np, varnode(sym)); 537 np->flags |= NLVAL; 538 return np; 539 540 free_np: 541 freetree(np); 542 return constnode(zero); 543 } 544 545 static Node * 546 content(int op, Node *np) 547 { 548 if (np->type->op != PTR) { 549 errorp("invalid argument of memory indirection"); 550 } else { 551 np = node(op, np->type->type, np, NULL); 552 np->flags |= NLVAL; 553 } 554 return np; 555 } 556 557 static Node * 558 array(Node *lp, Node *rp) 559 { 560 Type *tp; 561 Node *np; 562 563 if (!(lp->type->prop & TINTEGER) && !(rp->type->prop & TINTEGER)) 564 error("array subscript is not an integer"); 565 np = arithmetic(OADD, lp, rp); 566 tp = np->type; 567 if (tp->op != PTR) 568 errorp("subscripted value is neither array nor pointer"); 569 return content(OPTR, np); 570 } 571 572 static Node * 573 assignop(int op, Node *lp, Node *rp) 574 { 575 if ((rp = convert(rp, lp->type, 0)) == NULL) { 576 errorp("incompatible types when assigning"); 577 return lp; 578 } 579 580 return node(op, lp->type, lp, rp); 581 } 582 583 static Node * 584 incdec(Node *np, int op) 585 { 586 Type *tp = np->type; 587 Node *inc; 588 589 chklvalue(np); 590 np->flags |= NEFFECT; 591 592 if (!(tp->prop & TDEFINED)) { 593 errorp("invalid use of undefined type"); 594 return np; 595 } else if (tp->op == PTR && !(tp->type->prop & TDEFINED)) { 596 errorp("%s of pointer to an incomplete type", 597 (op == OINC || op == OA_ADD) ? "increment" : "decrement"); 598 return np; 599 } else if (tp->op == PTR || (tp->prop & TARITH)) { 600 inc = constnode(one); 601 } else { 602 errorp("wrong type argument to increment or decrement"); 603 return np; 604 } 605 return arithmetic(op, np, inc); 606 } 607 608 static Node * 609 address(int op, Node *np) 610 { 611 Node *new; 612 Type *tp; 613 Symbol *sym = np->sym; 614 615 if ((np->flags & NDECAY) != 0) { 616 new = np->left; 617 free(np); 618 np = new; 619 } 620 tp = np->type; 621 622 /* 623 * ansi c accepts & applied to a function name, and it generates 624 * a function pointer 625 */ 626 if (np->op == OSYM) { 627 if (tp->op == FTN) 628 return decay(np); 629 if (tp->op == ARY) 630 goto dont_check_lvalue; 631 } 632 chklvalue(np); 633 634 dont_check_lvalue: 635 if (sym && (sym->flags & SREGISTER)) 636 errorp("address of register variable '%s' requested", yytext); 637 new = node(op, mktype(tp, PTR, 0, NULL), np, NULL); 638 639 return chkconstaddr(np, new); 640 } 641 642 static Node * 643 negation(int op, Node *np) 644 { 645 if (!(np->type->prop & TARITH) && np->type->op != PTR) { 646 errorp("invalid argument of unary '!'"); 647 return constnode(zero); 648 } 649 return exp2cond(np, 1); 650 } 651 652 static Symbol * 653 adjstrings(Symbol *sym) 654 { 655 char *d, *s; 656 Type *tp, *base; 657 size_t len, n, osiz, nsiz; 658 659 tp = sym->type; 660 base = tp->type; 661 for (len = tp->n.elem; next() == STRING; len += n - 1) { 662 if (yylval.sym->type->type != base) 663 break; 664 665 d = sym->u.s; 666 s = yylval.sym->u.s; 667 n = yylval.sym->type->n.elem; 668 osiz = (len-1) * base->size; 669 nsiz = n * base->size; 670 671 sym->u.s = d = xrealloc(d, osiz + nsiz); 672 memcpy(d + osiz, s, nsiz); 673 } 674 675 if (tp->n.elem != len) 676 sym->type = mktype(base, ARY, len, NULL); 677 678 return sym; 679 } 680 681 static Node * 682 funcsym(Symbol *sym) 683 { 684 char *s; 685 Node *np; 686 687 sym = install(sym->ns, sym); 688 s = curfun->name; 689 np = constnode(newstring(s, strlen(s)+1)); 690 sym->type = np->type; 691 sym->flags |= SHASINIT | SLOCAL | SUSED; 692 emit(ODECL, sym); 693 emit(OINIT, np); 694 695 return varnode(sym); 696 } 697 698 /************************************************************* 699 * grammar functions * 700 *************************************************************/ 701 static Node * 702 primary(void) 703 { 704 Node *np; 705 Symbol *sym; 706 Node *(*fun)(Symbol *); 707 708 sym = yylval.sym; 709 switch (yytoken) { 710 case STRING: 711 adjstrings(sym); 712 sym->flags |= SHASINIT; 713 if (!disstring) { 714 emit(ODECL, sym); 715 emit(OINIT, constnode(sym)); 716 } 717 return varnode(sym); 718 case BUILTIN: 719 fun = sym->u.fun; 720 next(); 721 expect('('); 722 np = (*fun)(sym); 723 expect(')'); 724 725 /* do not call to next */ 726 return np; 727 case CONSTANT: 728 np = constnode(sym); 729 break; 730 case DEFINED: 731 np = defined(); 732 break; 733 case '(': 734 next(); 735 np = expr(); 736 expect(')'); 737 738 /* do not call to next */ 739 return np; 740 case IDEN: 741 assert((sym->flags & SCONSTANT) == 0); 742 if ((sym->flags & SDECLARED) != 0) { 743 sym->flags |= SUSED; 744 np = varnode(sym); 745 } else if (namespace == NS_CPP) { 746 np = constnode(zero); 747 } else if (!strcmp(yytext, "__func__") && curctx > PARAMCTX) { 748 np = funcsym(sym); 749 } else { 750 errorp("'%s' undeclared", yytext); 751 sym->type = inttype; 752 sym = install(sym->ns, sym); 753 sym->flags |= SUSED; 754 np = varnode(sym); 755 } 756 break; 757 default: 758 unexpected(); 759 } 760 next(); 761 762 return np; 763 } 764 765 static Node * 766 arguments(Node *np) 767 { 768 int toomany, n, op; 769 Node *par = NULL, *arg; 770 Type *argtype, *tp = np->type, *rettype; 771 Type **targs = (Type *[]) {ellipsistype}; 772 773 if (tp->op == PTR && tp->type->op == FTN) { 774 np = content(OPTR, np); 775 tp = np->type; 776 } 777 if (tp->op != FTN) { 778 n = 1; 779 rettype = inttype; 780 errorp("function or function pointer expected"); 781 } else { 782 targs = tp->p.pars; 783 n = tp->n.elem; 784 rettype = tp->type; 785 } 786 787 expect('('); 788 if (yytoken == ')') 789 goto no_pars; 790 toomany = 0; 791 792 do { 793 arg = assign(); 794 argtype = *targs; 795 if (argtype == ellipsistype) { 796 n = 0; 797 switch (arg->type->op) { 798 case INT: 799 arg = promote(arg); 800 break; 801 case FLOAT: 802 if (arg->type == floattype) 803 arg = convert(arg, doubletype, 1); 804 break; 805 } 806 par = node(OPAR, arg->type, par, arg); 807 continue; 808 } 809 if (--n < 0) { 810 if (!toomany) 811 errorp("too many arguments in function call"); 812 toomany = 1; 813 continue; 814 } 815 ++targs; 816 if ((arg = convert(arg, argtype, 0)) != NULL) { 817 par = node(OPAR, arg->type, par, arg); 818 continue; 819 } 820 errorp("incompatible type for argument %d in function call", 821 tp->n.elem - n); 822 } while (accept(',')); 823 824 no_pars: 825 expect(')'); 826 if (n > 0 && *targs != ellipsistype) 827 errorp("too few arguments in function call"); 828 829 op = (tp->prop&TELLIPSIS) ? OCALLE : OCALL; 830 return node(op, rettype, np, par); 831 } 832 833 static Type * 834 gettype(Node *np) 835 { 836 Node *new; 837 Type *tp; 838 839 if (np == NULL) 840 unexpected(); 841 if ((np->flags & NDECAY) != 0) { 842 new = np->left; 843 free(np); 844 np = new; 845 } 846 tp = np->type; 847 freetree(np); 848 return tp; 849 } 850 851 static Node *unary(void); 852 853 static Node * 854 postfix(Node *lp) 855 { 856 int op; 857 Node *rp; 858 859 for (;;) { 860 switch (yytoken) { 861 case '[': 862 next(); 863 rp = expr(); 864 expect(']'); 865 lp = array(decay(lp), rp); 866 break; 867 case DEC: 868 case INC: 869 op = (yytoken == INC) ? OINC : ODEC; 870 lp = incdec(decay(lp), op); 871 next(); 872 break; 873 874 case INDIR: 875 lp = content(OPTR, decay(lp)); 876 case '.': 877 lp = field(decay(lp)); 878 break; 879 case '(': 880 lp = arguments(decay(lp)); 881 lp->flags |= NEFFECT; 882 break; 883 default: 884 return lp; 885 } 886 } 887 } 888 889 static Type * 890 sizeexp(void) 891 { 892 Node *np; 893 Type *tp; 894 895 if (!accept('(')) 896 return gettype(unary()); 897 898 switch (yytoken) { 899 case TYPE: 900 case TYPEIDEN: 901 tp = typename(); 902 expect(')'); 903 break; 904 default: 905 np = expr(); 906 expect(')'); 907 tp = gettype(postfix(np)); 908 break; 909 } 910 911 return tp; 912 } 913 914 static Node *cast(void); 915 916 static Node * 917 unary(void) 918 { 919 Node *(*fun)(int, Node *), *np; 920 int op; 921 Type *tp; 922 923 switch (yytoken) { 924 case '!': op = 0; fun = negation; break; 925 case '+': op = OADD; fun = numericaluop; break; 926 case '-': op = OSNEG; fun = numericaluop; break; 927 case '~': op = OCPL; fun = integeruop; break; 928 case '&': op = OADDR; fun = address; break; 929 case '*': op = OPTR; fun = content; break; 930 case SIZEOF: 931 disstring = 1; 932 next(); 933 tp = sizeexp(); 934 disstring = 0; 935 if (!(tp->prop & TDEFINED)) 936 errorp("sizeof applied to an incomplete type"); 937 return sizeofnode(tp); 938 case INC: 939 case DEC: 940 op = (yytoken == INC) ? OA_ADD : OA_SUB; 941 next(); 942 np = incdec(unary(), op); 943 goto decay; 944 case DEFINED: 945 return defined(); 946 default: 947 np = postfix(primary()); 948 goto decay; 949 } 950 951 next(); 952 np = (*fun)(op, cast()); 953 954 decay: 955 return decay(np); 956 } 957 958 static Node * 959 cast(void) 960 { 961 Node *tmp, *np; 962 Type *tp; 963 static int nested; 964 965 if (!accept('(')) 966 return unary(); 967 968 switch (yytoken) { 969 case TQUALIFIER: 970 case TYPE: 971 case TYPEIDEN: 972 tp = typename(); 973 expect(')'); 974 975 if (yytoken == '{') { 976 Symbol *sym = newsym(NS_IDEN, NULL); 977 sym->id = newid(); 978 sym->type = tp; 979 sym->flags |= (curctx == GLOBALCTX) ? SPRIVATE : SAUTO; 980 initializer(sym); 981 982 return decay(varnode(sym)); 983 } 984 985 switch (tp->op) { 986 case ARY: 987 error("cast specifies an array type"); 988 default: 989 tmp = cast(); 990 if ((np = convert(tmp, tp, 1)) == NULL) 991 error("bad type conversion requested"); 992 np->flags &= ~NLVAL; 993 } 994 break; 995 default: 996 if (nested == NR_SUBEXPR) 997 error("too many expressions nested by parentheses"); 998 ++nested; 999 np = expr(); 1000 --nested; 1001 expect(')'); 1002 np = postfix(np); 1003 break; 1004 } 1005 1006 return np; 1007 } 1008 1009 static Node * 1010 mul(void) 1011 { 1012 Node *np, *(*fun)(int, Node *, Node *); 1013 int op; 1014 1015 np = cast(); 1016 for (;;) { 1017 switch (yytoken) { 1018 case '*': op = OMUL; fun = arithmetic; break; 1019 case '/': op = ODIV; fun = arithmetic; break; 1020 case '%': op = OMOD; fun = integerop; break; 1021 default: return np; 1022 } 1023 next(); 1024 np = (*fun)(op, np, cast()); 1025 } 1026 } 1027 1028 static Node * 1029 add(void) 1030 { 1031 int op; 1032 Node *np; 1033 1034 np = mul(); 1035 for (;;) { 1036 switch (yytoken) { 1037 case '+': op = OADD; break; 1038 case '-': op = OSUB; break; 1039 default: return np; 1040 } 1041 next(); 1042 np = arithmetic(op, np, mul()); 1043 } 1044 } 1045 1046 static Node * 1047 shift(void) 1048 { 1049 int op; 1050 Node *np; 1051 1052 np = add(); 1053 for (;;) { 1054 switch (yytoken) { 1055 case SHL: op = OSHL; break; 1056 case SHR: op = OSHR; break; 1057 default: return np; 1058 } 1059 next(); 1060 np = integerop(op, np, add()); 1061 } 1062 } 1063 1064 static Node * 1065 relational(void) 1066 { 1067 int op; 1068 Node *np; 1069 1070 np = shift(); 1071 for (;;) { 1072 switch (yytoken) { 1073 case '<': op = OLT; break; 1074 case '>': op = OGT; break; 1075 case GE: op = OGE; break; 1076 case LE: op = OLE; break; 1077 default: return np; 1078 } 1079 next(); 1080 np = compare(op, np, shift()); 1081 } 1082 } 1083 1084 static Node * 1085 eq(void) 1086 { 1087 int op; 1088 Node *np; 1089 1090 np = relational(); 1091 for (;;) { 1092 switch (yytoken) { 1093 case EQ: op = OEQ; break; 1094 case NE: op = ONE; break; 1095 default: return np; 1096 } 1097 next(); 1098 np = compare(op, np, relational()); 1099 } 1100 } 1101 1102 static Node * 1103 bit_and(void) 1104 { 1105 Node *np; 1106 1107 np = eq(); 1108 while (accept('&')) 1109 np = integerop(OBAND, np, eq()); 1110 return np; 1111 } 1112 1113 static Node * 1114 bit_xor(void) 1115 { 1116 Node *np; 1117 1118 np = bit_and(); 1119 while (accept('^')) 1120 np = integerop(OBXOR, np, bit_and()); 1121 return np; 1122 } 1123 1124 static Node * 1125 bit_or(void) 1126 { 1127 Node *np; 1128 1129 np = bit_xor(); 1130 while (accept('|')) 1131 np = integerop(OBOR, np, bit_xor()); 1132 return np; 1133 } 1134 1135 static Node * 1136 and(void) 1137 { 1138 Node *np; 1139 1140 np = bit_or(); 1141 while (accept(AND)) 1142 np = logic(OAND, np, bit_or()); 1143 return np; 1144 } 1145 1146 static Node * 1147 or(void) 1148 { 1149 Node *np; 1150 1151 np = and(); 1152 while (accept(OR)) 1153 np = logic(OOR, np, and()); 1154 return np; 1155 } 1156 1157 static Node * 1158 ternary(void) 1159 { 1160 Node *cond; 1161 1162 cond = or(); 1163 while (accept('?')) { 1164 Node *ifyes, *ifno, *np; 1165 1166 cond = exp2cond(cond, 0); 1167 ifyes = expr(); 1168 expect(':'); 1169 ifno = ternary(); 1170 np = chkternary(ifyes, ifno); 1171 cond = node(OASK, np->type, cond, np); 1172 } 1173 return cond; 1174 } 1175 1176 Node * 1177 assign(void) 1178 { 1179 Node *np, *(*fun)(int , Node *, Node *); 1180 int op; 1181 1182 np = ternary(); 1183 for (;;) { 1184 switch (yytoken) { 1185 case '=': op = OASSIGN; fun = assignop; break; 1186 case MUL_EQ: op = OA_MUL; fun = arithmetic; break; 1187 case DIV_EQ: op = OA_DIV; fun = arithmetic; break; 1188 case MOD_EQ: op = OA_MOD; fun = integerop; break; 1189 case ADD_EQ: op = OA_ADD; fun = arithmetic; break; 1190 case SUB_EQ: op = OA_SUB; fun = arithmetic; break; 1191 case SHL_EQ: op = OA_SHL; fun = integerop; break; 1192 case SHR_EQ: op = OA_SHR; fun = integerop; break; 1193 case AND_EQ: op = OA_AND; fun = integerop; break; 1194 case XOR_EQ: op = OA_XOR; fun = integerop; break; 1195 case OR_EQ: op = OA_OR; fun = integerop; break; 1196 default: return np; 1197 } 1198 chklvalue(np); 1199 np->flags |= NEFFECT; 1200 next(); 1201 np = (fun)(op, np, assign()); 1202 } 1203 } 1204 1205 Node * 1206 expr(void) 1207 { 1208 Node *lp, *rp; 1209 1210 lp = assign(); 1211 while (accept(',')) { 1212 rp = assign(); 1213 lp = node(OCOMMA, rp->type, lp, rp); 1214 } 1215 1216 return lp; 1217 } 1218 1219 Node * 1220 iconstexpr(void) 1221 { 1222 Node *np; 1223 1224 np = ternary(); 1225 if (np && np->type->op == INT) { 1226 np = simplify(convert(np, inttype, 0)); 1227 if (np->flags & NCONST) 1228 return np; 1229 } 1230 freetree(np); 1231 return NULL; 1232 } 1233 1234 Node * 1235 condexpr(int neg) 1236 { 1237 Node *np; 1238 1239 np = exp2cond(expr(), neg); 1240 if (np->flags & NCONST) 1241 warn("conditional expression is constant"); 1242 return simplify(np); 1243 }