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