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