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