1 /* $NetBSD: tree.c,v 1.12 1995/10/02 17:37:57 jpo Exp $ */
4 * Copyright (c) 1994, 1995 Jochen Pohl
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Jochen Pohl for
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * $NetBSD: tree.c,v 1.12 1995/10/02 17:37:57 jpo Exp $
34 * $DragonFly: src/usr.bin/xlint/lint1/tree.c,v 1.7 2004/07/07 12:24:00 asmodai Exp $
46 /* Various flags for each operator. */
47 static mod_t modtab[NOPS];
49 static tnode_t *getinode(tspec_t, quad_t);
50 static void ptrcmpok(op_t, tnode_t *, tnode_t *);
51 static int asgntypok(op_t, int, tnode_t *, tnode_t *);
52 static void chkbeop(op_t, tnode_t *, tnode_t *);
53 static void chkeop2(op_t, int, tnode_t *, tnode_t *);
54 static void chkeop1(op_t, int, tnode_t *, tnode_t *);
55 static tnode_t *mktnode(op_t, type_t *, tnode_t *, tnode_t *);
56 static void balance(op_t, tnode_t **, tnode_t **);
57 static void incompat(op_t, tspec_t, tspec_t);
58 static void illptrc(mod_t *, type_t *, type_t *);
59 static void mrgqual(type_t **, type_t *, type_t *);
60 static int conmemb(type_t *);
61 static void ptconv(int, tspec_t, tspec_t, type_t *, tnode_t *);
62 static void iiconv(op_t, int, tspec_t, tspec_t, type_t *, tnode_t *);
63 static void piconv(op_t, tspec_t, type_t *, tnode_t *);
64 static void ppconv(op_t, tnode_t *, type_t *);
65 static tnode_t *bldstr(op_t, tnode_t *, tnode_t *);
66 static tnode_t *bldincdec(op_t, tnode_t *);
67 static tnode_t *bldamper(tnode_t *, int);
68 static tnode_t *bldplmi(op_t, tnode_t *, tnode_t *);
69 static tnode_t *bldshft(op_t, tnode_t *, tnode_t *);
70 static tnode_t *bldcol(tnode_t *, tnode_t *);
71 static tnode_t *bldasgn(op_t, tnode_t *, tnode_t *);
72 static tnode_t *plength(type_t *);
73 static tnode_t *fold(tnode_t *);
74 static tnode_t *foldtst(tnode_t *);
75 static tnode_t *foldflt(tnode_t *);
76 static tnode_t *chkfarg(type_t *, tnode_t *);
77 static tnode_t *parg(int, type_t *, tnode_t *);
78 static void nulleff(tnode_t *);
79 static void displexpr(tnode_t *, int);
80 static void chkaidx(tnode_t *, int);
81 static void chkcomp(op_t, tnode_t *, tnode_t *);
82 static void precconf(tnode_t *);
85 * Initialize mods of operators.
94 { ARROW, { 1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,
96 { POINT, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
98 { NOT, { 0,1,0,1,0,1,0,1,0,0,0,0,0,0,0,1,0,
100 { COMPL, { 0,0,1,0,0,1,1,0,0,0,0,0,0,0,0,1,1,
102 { INCBEF, { 0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,
104 { DECBEF, { 0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,
106 { INCAFT, { 0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,
108 { DECAFT, { 0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,
110 { UPLUS, { 0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,1,
112 { UMINUS, { 0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,1,1,
114 { STAR, { 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,
116 { AMPER, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
118 { MULT, { 1,0,0,0,1,1,1,0,1,0,0,1,0,0,0,1,1,
120 { DIV, { 1,0,0,0,1,1,1,0,1,0,1,1,0,0,0,1,1,
122 { MOD, { 1,0,1,0,0,1,1,0,1,0,1,1,0,0,0,1,1,
124 { PLUS, { 1,0,0,1,0,1,1,0,1,0,0,0,0,0,0,1,0,
126 { MINUS, { 1,0,0,1,0,1,1,0,1,0,0,0,0,0,0,1,0,
128 { SHL, { 1,0,1,0,0,1,1,0,0,0,0,0,1,0,0,1,1,
130 { SHR, { 1,0,1,0,0,1,1,0,0,0,1,0,1,0,0,1,1,
132 { LT, { 1,1,0,1,0,1,1,0,1,0,1,1,0,1,1,0,1,
134 { LE, { 1,1,0,1,0,1,1,0,1,0,1,1,0,1,1,0,1,
136 { GT, { 1,1,0,1,0,1,1,0,1,0,1,1,0,1,1,0,1,
138 { GE, { 1,1,0,1,0,1,1,0,1,0,1,1,0,1,1,0,1,
140 { EQ, { 1,1,0,1,0,1,1,0,1,0,0,0,0,1,1,0,1,
142 { NE, { 1,1,0,1,0,1,1,0,1,0,0,0,0,1,1,0,1,
144 { AND, { 1,0,1,0,0,1,1,0,1,0,0,0,1,0,0,1,0,
146 { XOR, { 1,0,1,0,0,1,1,0,1,0,0,0,1,0,0,1,0,
148 { OR, { 1,0,1,0,0,1,1,0,1,0,0,0,1,0,0,1,0,
150 { LOGAND, { 1,1,0,1,0,1,0,1,0,0,0,0,0,0,0,1,0,
152 { LOGOR, { 1,1,0,1,0,1,0,1,0,0,0,0,1,0,0,1,0,
154 { QUEST, { 1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,
156 { COLON, { 1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,
158 { ASSIGN, { 1,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,
160 { MULASS, { 1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,1,0,
162 { DIVASS, { 1,0,0,0,1,0,0,0,0,1,0,1,0,0,0,1,0,
164 { MODASS, { 1,0,1,0,0,0,0,0,0,1,0,1,0,0,0,1,0,
166 { ADDASS, { 1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,
168 { SUBASS, { 1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,
170 { SHLASS, { 1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,
172 { SHRASS, { 1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,
174 { ANDASS, { 1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,
176 { XORASS, { 1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,
178 { ORASS, { 1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,
180 { NAME, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
182 { CON, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
184 { STRING, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
186 { FSEL, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
188 { CALL, { 1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
190 { COMMA, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
192 { CVT, { 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,
194 { ICALL, { 1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
196 { LOAD, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
198 { PUSH, { 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,
200 { RETURN, { 1,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,
202 { INIT, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,
204 { FARG, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,
210 for (i = 0; imods[i].op != NOOP; i++)
211 STRUCT_ASSIGN(modtab[imods[i].op], imods[i].m);
215 * Increase degree of reference.
216 * This is most often used to change type "T" in type "pointer to T".
219 incref(type_t *tp, tspec_t t)
223 tp2 = getblk(sizeof (type_t));
230 * same for use in expressions
233 tincref(type_t *tp, tspec_t t)
237 tp2 = tgetblk(sizeof (type_t));
244 * Create a node for a constant.
247 getcnode(type_t *tp, val_t *v)
254 n->tn_val = tgetblk(sizeof (val_t));
255 n->tn_val->v_tspec = tp->t_tspec;
256 n->tn_val->v_ansiu = v->v_ansiu;
257 n->tn_val->v_u = v->v_u;
263 * Create a node for a integer constant.
266 getinode(tspec_t t, quad_t q)
272 n->tn_type = gettyp(t);
273 n->tn_val = tgetblk(sizeof (val_t));
274 n->tn_val->v_tspec = t;
275 n->tn_val->v_quad = q;
280 * Create a node for a name (symbol table entry).
281 * ntok is the token which follows the name.
284 getnnode(sym_t *sym, int ntok)
288 if (sym->s_scl == NOSCL) {
291 if (ntok == T_LPARN) {
293 /* function implicitly declared to ... */
297 * XXX if tflag is set the symbol should be
298 * exported to level 0
300 sym->s_type = incref(sym->s_type, FUNC);
303 error(99, sym->s_name);
307 if (sym->s_kind != FVFT && sym->s_kind != FMOS)
308 lerror("getnnode() 1");
311 n->tn_type = sym->s_type;
312 if (sym->s_scl != ENUMCON) {
315 if (sym->s_kind == FVFT && sym->s_type->t_tspec != FUNC)
319 n->tn_val = tgetblk(sizeof (val_t));
320 *n->tn_val = sym->s_value;
327 * Create a node for a string.
330 getsnode(strg_t *strg)
340 n->tn_type = tincref(gettyp(strg->st_tspec), ARRAY);
341 n->tn_type->t_dim = len + 1;
344 n->tn_strg = tgetblk(sizeof (strg_t));
345 n->tn_strg->st_tspec = strg->st_tspec;
346 n->tn_strg->st_len = len;
348 if (strg->st_tspec == CHAR) {
349 n->tn_strg->st_cp = tgetblk(len + 1);
350 (void)memcpy(n->tn_strg->st_cp, strg->st_cp, len + 1);
353 n->tn_strg->st_wcp = tgetblk((len + 1) * sizeof (wchar_t));
354 (void)memcpy(n->tn_strg->st_wcp, strg->st_wcp,
355 (len + 1) * sizeof (wchar_t));
364 * Returns a symbol which has the same name as the msym argument and is a
365 * member of the struct or union specified by the tn argument.
368 strmemb(tnode_t *tn, op_t op, sym_t *msym)
377 * Remove the member if it was unknown until now (Which means
378 * that no defined struct or union has a member with the same name).
380 if (msym->s_scl == NOSCL) {
381 /* undefined struct/union member: %s */
382 error(101, msym->s_name);
386 msym->s_styp = tgetblk(sizeof (str_t));
387 msym->s_styp->stag = tgetblk(sizeof (sym_t));
388 msym->s_styp->stag->s_name = unnamed;
389 msym->s_value.v_tspec = INT;
393 /* Set str to the tag of which msym is expected to be a member. */
395 t = (tp = tn->tn_type)->t_tspec;
397 if (t == STRUCT || t == UNION)
399 } else if (op == ARROW && t == PTR) {
400 t = (tp = tp->t_subt)->t_tspec;
401 if (t == STRUCT || t == UNION)
406 * If this struct/union has a member with the name of msym, return
410 for (sym = msym; sym != NULL; sym = sym->s_link) {
411 if (sym->s_scl != MOS && sym->s_scl != MOU)
413 if (sym->s_styp != str)
415 if (strcmp(sym->s_name, msym->s_name) != 0)
422 * Set eq to 0 if there are struct/union members with the same name
423 * and different types and/or offsets.
426 for (csym = msym; csym != NULL; csym = csym->s_link) {
427 if (csym->s_scl != MOS && csym->s_scl != MOU)
429 if (strcmp(msym->s_name, csym->s_name) != 0)
431 for (sym = csym->s_link ; sym != NULL; sym = sym->s_link) {
434 if (sym->s_scl != MOS && sym->s_scl != MOU)
436 if (strcmp(csym->s_name, sym->s_name) != 0)
438 if (csym->s_value.v_quad != sym->s_value.v_quad) {
443 eq = eqtype(csym->s_type, sym->s_type, 0, 0, &w) && !w;
446 if (csym->s_field != sym->s_field) {
455 if (tp1->t_flen != tp2->t_flen) {
459 if (tp1->t_foffs != tp2->t_foffs) {
470 * Now handle the case in which the left operand refers really
471 * to a struct/union, but the right operand is not member of it.
474 /* illegal member use: %s */
476 warning(102, msym->s_name);
478 error(102, msym->s_name);
484 * Now the left operand of ARROW does not point to a struct/union
485 * or the left operand of POINT is no struct/union.
489 /* left operand of "." must be struct/union object */
496 /* left operand of "->" must be pointer to ... */
497 if (tflag && tn->tn_type->t_tspec == PTR) {
505 /* non-unique member requires struct/union %s */
506 error(105, op == POINT ? "object" : "pointer");
508 /* unacceptable operand of %s */
509 error(111, modtab[op].m_name);
517 * Create a tree node. Called for most operands except function calls,
522 * rn if not NULL, right operand
525 build(op_t op, tnode_t *ln, tnode_t *rn)
533 /* If there was an error in one of the operands, return. */
534 if (ln == NULL || (mp->m_binary && rn == NULL))
538 * Apply class conversions to the left operand, but only if its
539 * value is needed or it is compaired with null.
541 if (mp->m_vctx || mp->m_tctx)
544 * The right operand is almost always in a test or value context,
545 * except if it is a struct or union member.
547 if (mp->m_binary && op != ARROW && op != POINT)
551 * Print some warnings for comparisions of unsigned values with
552 * constants lower than or equal to null. This must be done
553 * before promote() because otherwise unsigned char and unsigned
554 * short would be promoted to int. Also types are tested to be
555 * CHAR, which would also become int.
561 * Promote the left operand if it is in a test or value context
563 if (mp->m_vctx || mp->m_tctx)
564 ln = promote(op, 0, ln);
566 * Promote the right operand, but only if it is no struct or
567 * union member, or if it is not to be assigned to the left operand
569 if (mp->m_binary && op != ARROW && op != POINT &&
570 op != ASSIGN && op != RETURN) {
571 rn = promote(op, 0, rn);
575 * If the result of the operation is different for signed or
576 * unsigned operands and one of the operands is signed only in
577 * ANSI C, print a warning.
579 if (mp->m_tlansiu && ln->tn_op == CON && ln->tn_val->v_ansiu) {
580 /* ANSI C treats constant as unsigned, op %s */
581 warning(218, mp->m_name);
582 ln->tn_val->v_ansiu = 0;
584 if (mp->m_transiu && rn->tn_op == CON && rn->tn_val->v_ansiu) {
585 /* ANSI C treats constant as unsigned, op %s */
586 warning(218, mp->m_name);
587 rn->tn_val->v_ansiu = 0;
590 /* Make sure both operands are of the same type */
591 if (mp->m_balance || (tflag && (op == SHL || op == SHR)))
592 balance(op, &ln, &rn);
595 * Check types for compatibility with the operation and mutual
596 * compatibility. Return if there are serios problems.
598 if (!typeok(op, 0, ln, rn))
601 /* And now create the node. */
605 ntn = bldstr(op, ln, rn);
611 ntn = bldincdec(op, ln);
614 ntn = bldamper(ln, 0);
617 ntn = mktnode(STAR, ln->tn_type->t_subt, ln, NULL);
621 ntn = bldplmi(op, ln, rn);
625 ntn = bldshft(op, ln, rn);
628 ntn = bldcol(ln, rn);
642 ntn = bldasgn(op, ln, rn);
646 ntn = mktnode(op, rn->tn_type, ln, rn);
649 rtp = mp->m_logop ? gettyp(INT) : ln->tn_type;
650 if (!mp->m_binary && rn != NULL)
652 ntn = mktnode(op, rtp, ln, rn);
656 /* Return if an error occured. */
660 /* Print a warning if precedence confusion is possible */
665 * Print a warning if one of the operands is in a context where
666 * it is compared with null and if this operand is a constant.
669 if (ln->tn_op == CON ||
670 ((mp->m_binary && op != QUEST) && rn->tn_op == CON)) {
672 /* constant in conditional context */
677 /* Fold if the operator requires it */
679 if (ln->tn_op == CON && (!mp->m_binary || rn->tn_op == CON)) {
682 } else if (isftyp(ntn->tn_type->t_tspec)) {
687 } else if (op == QUEST && ln->tn_op == CON) {
688 ntn = ln->tn_val->v_quad ? rn->tn_left : rn->tn_right;
696 * Perform class conversions.
698 * Arrays of type T are converted into pointers to type T.
699 * Functions are converted to pointers to functions.
700 * Lvalues are converted to rvalues.
708 * Array-lvalue (array of type T) is converted into rvalue
709 * (pointer to type T)
711 if (tn->tn_type->t_tspec == ARRAY) {
712 if (!tn->tn_lvalue) {
713 /* %soperand of '%s' must be lvalue */
714 /* XXX print correct operator */
715 (void)gnuism(114, "", modtab[AMPER].m_name);
717 tn = mktnode(AMPER, tincref(tn->tn_type->t_subt, PTR),
722 * Expression of type function (function with return value of type T)
723 * in rvalue-expression (pointer to function with return value
726 if (tn->tn_type->t_tspec == FUNC)
727 tn = bldamper(tn, 1);
729 /* lvalue to rvalue */
731 tp = tduptyp(tn->tn_type);
732 tp->t_const = tp->t_volatile = 0;
733 tn = mktnode(LOAD, tp, tn, NULL);
740 * Perform most type checks. First the types are checked using
741 * informations from modtab[]. After that it is done by hand for
742 * more complicated operators and type combinations.
744 * If the types are ok, typeok() returns 1, otherwise 0.
747 typeok(op_t op, int arg, tnode_t *ln, tnode_t *rn)
750 tspec_t lt, rt, lst, rst, olt, ort;
751 type_t *ltp, *rtp, *lstp, *rstp;
756 if ((lt = (ltp = ln->tn_type)->t_tspec) == PTR)
757 lst = (lstp = ltp->t_subt)->t_tspec;
759 if ((rt = (rtp = rn->tn_type)->t_tspec) == PTR)
760 rst = (rstp = rtp->t_subt)->t_tspec;
764 /* integertypes required */
765 if (!isityp(lt) || (mp->m_binary && !isityp(rt))) {
766 incompat(op, lt, rt);
769 } else if (mp->m_rqsclt) {
770 /* scalar types required */
771 if (!issclt(lt) || (mp->m_binary && !issclt(rt))) {
772 incompat(op, lt, rt);
775 } else if (mp->m_rqatyp) {
776 /* arithmetic types required */
777 if (!isatyp(lt) || (mp->m_binary && !isatyp(rt))) {
778 incompat(op, lt, rt);
783 if (op == SHL || op == SHR || op == SHLASS || op == SHRASS) {
785 * For these operations we need the types before promotion
788 for (tn=ln; tn->tn_op==CVT && !tn->tn_cast; tn=tn->tn_left) ;
789 olt = tn->tn_type->t_tspec;
790 for (tn=rn; tn->tn_op==CVT && !tn->tn_cast; tn=tn->tn_left) ;
791 ort = tn->tn_type->t_tspec;
797 * Most errors required by ANSI C are reported in strmemb().
798 * Here we only must check for totaly wrong things.
800 if (lt == FUNC || lt == VOID || ltp->t_isfield ||
801 ((lt != STRUCT && lt != UNION) && !ln->tn_lvalue)) {
802 /* Without tflag we got already an error */
804 /* unacceptable operand of %s */
805 error(111, mp->m_name);
808 /* Now we have an object we can create a pointer to */
811 if (lt != PTR && !(tflag && isityp(lt))) {
812 /* Without tflag we got already an error */
814 /* unacceptabel operand of %s */
815 error(111, mp->m_name);
823 /* operands have scalar types (checked above) */
824 if (!ln->tn_lvalue) {
825 if (ln->tn_op == CVT && ln->tn_cast &&
826 ln->tn_left->tn_op == LOAD) {
827 /* a cast does not yield an lvalue */
830 /* %soperand of %s must be lvalue */
831 error(114, "", mp->m_name);
833 } else if (ltp->t_const) {
834 /* %soperand of %s must be modifiable lvalue */
836 warning(115, "", mp->m_name);
840 if (lt == ARRAY || lt == FUNC) {
841 /* ok, a warning comes later (in bldamper()) */
842 } else if (!ln->tn_lvalue) {
843 if (ln->tn_op == CVT && ln->tn_cast &&
844 ln->tn_left->tn_op == LOAD) {
845 /* a cast does not yield an lvalue */
848 /* %soperand of %s must be lvalue */
849 error(114, "", mp->m_name);
851 } else if (issclt(lt)) {
852 if (ltp->t_isfield) {
853 /* cannot take address of bit-field */
857 } else if (lt != STRUCT && lt != UNION) {
858 /* unacceptable operand of %s */
859 error(111, mp->m_name);
862 if (ln->tn_op == NAME && ln->tn_sym->s_reg) {
863 /* cannot take address of register %s */
864 error(113, ln->tn_sym->s_name);
869 /* until now there were no type checks for this operator */
871 /* cannot dereference non-pointer type */
877 /* operands have scalar types (checked above) */
878 if ((lt == PTR && !isityp(rt)) || (rt == PTR && !isityp(lt))) {
879 incompat(op, lt, rt);
884 /* operands have scalar types (checked above) */
885 if (lt == PTR && (!isityp(rt) && rt != PTR)) {
886 incompat(op, lt, rt);
888 } else if (rt == PTR && lt != PTR) {
889 incompat(op, lt, rt);
892 if (lt == PTR && rt == PTR) {
893 if (!eqtype(lstp, rstp, 1, 0, NULL)) {
894 /* illegal pointer subtraction */
900 /* operands have integer types (checked above) */
901 if (pflag && !isutyp(lt)) {
903 * The left operand is signed. This means that
904 * the operation is (possibly) nonportable.
906 /* bitwise operation on signed value nonportable */
907 if (ln->tn_op != CON) {
908 /* possibly nonportable */
910 } else if (ln->tn_val->v_quad < 0) {
913 } else if (!tflag && !sflag && !isutyp(olt) && isutyp(ort)) {
915 * The left operand would become unsigned in
919 (ln->tn_op != CON || ln->tn_val->v_quad < 0)) {
920 /* semantics of %s change in ANSI C; use ... */
921 warning(118, mp->m_name);
923 } else if (!tflag && !sflag && !isutyp(olt) && !isutyp(ort) &&
924 psize(lt) < psize(rt)) {
926 * In traditional C the left operand would be extended,
927 * possibly with 1, and then shifted.
930 (ln->tn_op != CON || ln->tn_val->v_quad < 0)) {
931 /* semantics of %s change in ANSI C; use ... */
932 warning(118, mp->m_name);
938 * ANSI C does not perform balancing for shift operations,
939 * but traditional C does. If the width of the right operand
940 * is greather than the width of the left operand, than in
941 * traditional C the left operand would be extendet to the
942 * width of the right operand. For SHL this may result in
945 if (psize(lt) < psize(rt)) {
947 * XXX If both operands are constant make sure
948 * that there is really a differencs between
949 * ANSI C and traditional C.
952 /* semantics of %s change in ANSI C; use ... */
953 warning(118, mp->m_name);
956 if (rn->tn_op == CON) {
957 if (!isutyp(rt) && rn->tn_val->v_quad < 0) {
960 } else if ((u_quad_t)rn->tn_val->v_quad == size(lt)) {
961 /* shift equal to size fo object */
963 } else if ((u_quad_t)rn->tn_val->v_quad > size(lt)) {
964 /* shift greater than size of object */
972 * Accept some things which are allowed with EQ and NE,
973 * but not with ordered comparisions.
975 if (lt == PTR && ((rt == PTR && rst == VOID) || isityp(rt))) {
976 if (rn->tn_op == CON && rn->tn_val->v_quad == 0)
979 if (rt == PTR && ((lt == PTR && lst == VOID) || isityp(lt))) {
980 if (ln->tn_op == CON && ln->tn_val->v_quad == 0)
988 if ((lt == PTR || rt == PTR) && lt != rt) {
989 if (isityp(lt) || isityp(rt)) {
990 /* illegal comb. of pointer and int., op %s */
991 warning(123, mp->m_name);
993 incompat(op, lt, rt);
996 } else if (lt == PTR && rt == PTR) {
997 ptrcmpok(op, ln, rn);
1002 /* first operand must have scalar type, op ? : */
1006 if (rn->tn_op != COLON)
1007 lerror("typeok() 2");
1011 if (isatyp(lt) && isatyp(rt))
1014 if (lt == STRUCT && rt == STRUCT && ltp->t_str == rtp->t_str)
1016 if (lt == UNION && rt == UNION && ltp->t_str == rtp->t_str)
1019 /* combination of any pointer and 0, 0L or (void *)0 is ok */
1020 if (lt == PTR && ((rt == PTR && rst == VOID) || isityp(rt))) {
1021 if (rn->tn_op == CON && rn->tn_val->v_quad == 0)
1024 if (rt == PTR && ((lt == PTR && lst == VOID) || isityp(lt))) {
1025 if (ln->tn_op == CON && ln->tn_val->v_quad == 0)
1029 if ((lt == PTR && isityp(rt)) || (isityp(lt) && rt == PTR)) {
1030 /* illegal comb. of ptr. and int., op %s */
1031 warning(123, mp->m_name);
1035 if (lt == VOID || rt == VOID) {
1036 if (lt != VOID || rt != VOID)
1037 /* incompatible types in conditional */
1042 if (lt == PTR && rt == PTR && ((lst == VOID && rst == FUNC) ||
1043 (lst == FUNC && rst == VOID))) {
1044 /* (void *)0 handled above */
1046 /* ANSI C forbids conv. of %s to %s, op %s */
1047 warning(305, "function pointer", "'void *'",
1052 if (rt == PTR && lt == PTR) {
1053 if (!eqtype(lstp, rstp, 1, 0, NULL))
1054 illptrc(mp, ltp, rtp);
1058 /* incompatible types in conditional */
1066 if (!asgntypok(op, arg, ln, rn))
1075 /* operands have scalar types (checked above) */
1076 if ((lt == PTR && !isityp(rt)) || rt == PTR) {
1077 incompat(op, lt, rt);
1084 if (pflag && !isutyp(lt) && !(tflag && isutyp(rt))) {
1085 /* bitwise operation on s.v. possibly nonportabel */
1094 if (!ln->tn_lvalue) {
1095 if (ln->tn_op == CVT && ln->tn_cast &&
1096 ln->tn_left->tn_op == LOAD) {
1097 /* a cast does not yield an lvalue */
1100 /* %soperand of %s must be lvalue */
1101 error(114, "left ", mp->m_name);
1103 } else if (ltp->t_const || ((lt == STRUCT || lt == UNION) &&
1105 /* %soperand of %s must be modifiable lvalue */
1107 warning(115, "left ", mp->m_name);
1111 if (!modtab[ln->tn_op].m_sideeff)
1114 /* LINTED (enumeration values not handled in switch) */
1120 (ltp->t_isenum || (mp->m_binary && rtp->t_isenum))) {
1121 chkbeop(op, ln, rn);
1122 } else if (mp->m_enumop && (ltp->t_isenum && rtp->t_isenum)) {
1123 chkeop2(op, arg, ln, rn);
1124 } else if (mp->m_enumop && (ltp->t_isenum || rtp->t_isenum)) {
1125 chkeop1(op, arg, ln, rn);
1132 ptrcmpok(op_t op, tnode_t *ln, tnode_t *rn)
1136 const char *lts, *rts;
1138 lt = (ltp = ln->tn_type)->t_subt->t_tspec;
1139 rt = (rtp = rn->tn_type)->t_subt->t_tspec;
1141 if (lt == VOID || rt == VOID) {
1142 if (sflag && (lt == FUNC || rt == FUNC)) {
1143 /* (void *)0 already handled in typeok() */
1144 *(lt == FUNC ? <s : &rts) = "function pointer";
1145 *(lt == VOID ? <s : &rts) = "'void *'";
1146 /* ANSI C forbids comparision of %s with %s */
1147 warning(274, lts, rts);
1152 if (!eqtype(ltp->t_subt, rtp->t_subt, 1, 0, NULL)) {
1153 illptrc(&modtab[op], ltp, rtp);
1157 if (lt == FUNC && rt == FUNC) {
1158 if (sflag && op != EQ && op != NE)
1159 /* ANSI C forbids ordered comp. of func ptr */
1165 * Checks type compatibility for ASSIGN, INIT, FARG and RETURN
1166 * and prints warnings/errors if necessary.
1167 * If the types are (almost) compatible, 1 is returned, otherwise 0.
1170 asgntypok(op_t op, int arg, tnode_t *ln, tnode_t *rn)
1172 tspec_t lt, rt, lst, rst;
1173 type_t *ltp, *rtp, *lstp, *rstp;
1175 const char *lts, *rts;
1177 if ((lt = (ltp = ln->tn_type)->t_tspec) == PTR)
1178 lst = (lstp = ltp->t_subt)->t_tspec;
1179 if ((rt = (rtp = rn->tn_type)->t_tspec) == PTR)
1180 rst = (rstp = rtp->t_subt)->t_tspec;
1183 if (isatyp(lt) && isatyp(rt))
1186 if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION))
1187 /* both are struct or union */
1188 return (ltp->t_str == rtp->t_str);
1190 /* 0, 0L and (void *)0 may be assigned to any pointer */
1191 if (lt == PTR && ((rt == PTR && rst == VOID) || isityp(rt))) {
1192 if (rn->tn_op == CON && rn->tn_val->v_quad == 0)
1196 if (lt == PTR && rt == PTR && (lst == VOID || rst == VOID)) {
1197 /* two pointers, at least one pointer to void */
1198 if (sflag && (lst == FUNC || rst == FUNC)) {
1199 /* comb. of ptr to func and ptr to void */
1200 *(lst == FUNC ? <s : &rts) = "function pointer";
1201 *(lst == VOID ? <s : &rts) = "'void *'";
1205 /* ANSI C forbids conversion of %s to %s */
1206 warning(303, rts, lts);
1209 /* ANSI C forbids conv. of %s to %s, arg #%d */
1210 warning(304, rts, lts, arg);
1213 /* ANSI C forbids conv. of %s to %s, op %s */
1214 warning(305, rts, lts, mp->m_name);
1220 if (lt == PTR && rt == PTR && (lst == VOID || rst == VOID ||
1221 eqtype(lstp, rstp, 1, 0, NULL))) {
1222 /* compatible pointer types (qualifiers ignored) */
1224 ((!lstp->t_const && rstp->t_const) ||
1225 (!lstp->t_volatile && rstp->t_volatile))) {
1226 /* left side has not all qualifiers of right */
1230 /* incompatible pointer types */
1234 /* argument has incompat. ptr. type, arg #%d */
1238 /* operands have incompat. ptr. types, op %s */
1239 warning(128, mp->m_name);
1246 if ((lt == PTR && isityp(rt)) || (isityp(lt) && rt == PTR)) {
1250 /* illegal combination of pointer and integer */
1254 /* illegal comb. of ptr. and int., arg #%d */
1258 /* illegal comb. of ptr. and int., op %s */
1259 warning(123, mp->m_name);
1265 if (lt == PTR && rt == PTR) {
1269 illptrc(NULL, ltp, rtp);
1272 /* argument has incompatible pointer type, arg #%d */
1276 illptrc(mp, ltp, rtp);
1284 /* initialisation type mismatch */
1288 /* return value type mismatch */
1292 /* argument is incompatible with prototype, arg #%d */
1296 incompat(op, lt, rt);
1304 * Prints a warning if an operator, which should be senseless for an
1305 * enum type, is applied to an enum type.
1308 chkbeop(op_t op, tnode_t *ln, tnode_t *rn)
1317 if (!(ln->tn_type->t_isenum ||
1318 (mp->m_binary && rn->tn_type->t_isenum))) {
1323 * Enum as offset to a pointer is an exception (otherwise enums
1324 * could not be used as array indizes).
1327 ((ln->tn_type->t_isenum && rn->tn_type->t_tspec == PTR) ||
1328 (rn->tn_type->t_isenum && ln->tn_type->t_tspec == PTR))) {
1332 /* dubious operation on enum, op %s */
1333 warning(241, mp->m_name);
1338 * Prints a warning if an operator is applied to two different enum types.
1341 chkeop2(op_t op, int arg, tnode_t *ln, tnode_t *rn)
1347 if (ln->tn_type->t_enum != rn->tn_type->t_enum) {
1350 /* enum type mismatch in initialisation */
1354 /* enum type mismatch, arg #%d */
1358 /* return value type mismatch */
1362 /* enum type mismatch, op %s */
1363 warning(130, mp->m_name);
1367 } else if (mp->m_comp && op != EQ && op != NE) {
1369 /* dubious comparisions of enums */
1370 warning(243, mp->m_name);
1376 * Prints a warning if an operator has both enum end other integer
1380 chkeop1(op_t op, int arg, tnode_t *ln, tnode_t *rn)
1388 * Initializations with 0 should be allowed. Otherwise,
1389 * we should complain about all uninitialized enums,
1392 if (!rn->tn_type->t_isenum && rn->tn_op == CON &&
1393 isityp(rn->tn_type->t_tspec) && rn->tn_val->v_quad == 0) {
1396 /* initialisation of '%s' with '%s' */
1397 warning(277, tyname(ln->tn_type), tyname(rn->tn_type));
1400 /* combination of '%s' and '%s', arg #%d */
1401 warning(278, tyname(ln->tn_type), tyname(rn->tn_type), arg);
1404 /* combination of '%s' and '%s' in return */
1405 warning(279, tyname(ln->tn_type), tyname(rn->tn_type));
1408 /* combination of '%s' and %s, op %s */
1409 warning(242, tyname(ln->tn_type), tyname(rn->tn_type),
1416 * Build and initialize a new node.
1419 mktnode(op_t op, type_t *type, tnode_t *ln, tnode_t *rn)
1427 ntn->tn_type = type;
1431 if (op == STAR || op == FSEL) {
1432 if (ln->tn_type->t_tspec == PTR) {
1433 t = ln->tn_type->t_subt->t_tspec;
1434 if (t != FUNC && t != VOID)
1437 lerror("mktnode() 2");
1445 * Performs usual conversion of operands to (unsigned) int.
1447 * If tflag is set or the operand is a function argument with no
1448 * type information (no prototype or variable # of args), convert
1452 promote(op_t op, int farg, tnode_t *tn)
1458 t = tn->tn_type->t_tspec;
1465 * ANSI C requires that the result is always of type INT
1466 * if INT can represent all possible values of the previous
1469 if (tn->tn_type->t_isfield) {
1470 len = tn->tn_type->t_flen;
1471 if (size(INT) > len) {
1474 if (size(INT) != len)
1475 lerror("promote() 1");
1482 } else if (t == CHAR || t == UCHAR || t == SCHAR) {
1483 t = (size(CHAR) < size(INT) || t != UCHAR) ?
1485 } else if (t == SHORT || t == USHORT) {
1486 t = (size(SHORT) < size(INT) || t == SHORT) ?
1488 } else if (t == ENUM) {
1490 } else if (farg && t == FLOAT) {
1495 * In traditional C, keep unsigned and promote FLOAT
1498 if (t == UCHAR || t == USHORT) {
1500 } else if (t == CHAR || t == SCHAR || t == SHORT) {
1502 } else if (t == FLOAT) {
1504 } else if (t == ENUM) {
1509 if (t != tn->tn_type->t_tspec) {
1510 ntp = tduptyp(tn->tn_type);
1513 * Keep t_isenum so we are later able to check compatibility
1516 tn = convert(op, 0, ntp, tn);
1523 * Insert conversions which are necessary to give both operands the same
1524 * type. This is done in different ways for traditional C and ANIS C.
1527 balance(op_t op, tnode_t **lnp, tnode_t **rnp)
1532 static tspec_t tl[] = {
1533 LDOUBLE, DOUBLE, FLOAT, UQUAD, QUAD, ULONG, LONG, UINT, INT,
1536 lt = (*lnp)->tn_type->t_tspec;
1537 rt = (*rnp)->tn_type->t_tspec;
1539 if (!isatyp(lt) || !isatyp(rt))
1545 } else if (lt == LDOUBLE || rt == LDOUBLE) {
1547 } else if (lt == DOUBLE || rt == DOUBLE) {
1549 } else if (lt == FLOAT || rt == FLOAT) {
1553 * If type A has more bits than type B it should
1554 * be able to hold all possible values of type B.
1556 if (size(lt) > size(rt)) {
1558 } else if (size(lt) < size(rt)) {
1561 for (i = 3; tl[i] != INT; i++) {
1562 if (tl[i] == lt || tl[i] == rt)
1565 if ((isutyp(lt) || isutyp(rt)) &&
1573 /* Keep unsigned in traditional C */
1574 u = isutyp(lt) || isutyp(rt);
1575 for (i = 0; tl[i] != INT; i++) {
1576 if (lt == tl[i] || rt == tl[i])
1580 if (u && isityp(t) && !isutyp(t))
1585 ntp = tduptyp((*lnp)->tn_type);
1587 *lnp = convert(op, 0, ntp, *lnp);
1590 ntp = tduptyp((*rnp)->tn_type);
1592 *rnp = convert(op, 0, ntp, *rnp);
1597 * Insert a conversion operator, which converts the type of the node
1598 * to another given type.
1599 * If op is FARG, arg is the number of the argument (used for warnings).
1602 convert(op_t op, int arg, type_t *tp, tnode_t *tn)
1605 tspec_t nt, ot, ost;
1608 lerror("convert() 1");
1611 if ((ot = tn->tn_type->t_tspec) == PTR)
1612 ost = tn->tn_type->t_subt->t_tspec;
1614 if (!tflag && !sflag && op == FARG)
1615 ptconv(arg, nt, ot, tp, tn);
1616 if (isityp(nt) && isityp(ot)) {
1617 iiconv(op, arg, nt, ot, tp, tn);
1618 } else if (nt == PTR && ((ot == PTR && ost == VOID) || isityp(ot)) &&
1619 tn->tn_op == CON && tn->tn_val->v_quad == 0) {
1620 /* 0, 0L and (void *)0 may be assigned to any pointer. */
1621 } else if (isityp(nt) && ot == PTR) {
1622 piconv(op, nt, tp, tn);
1623 } else if (nt == PTR && ot == PTR) {
1630 ntn->tn_cast = op == CVT;
1631 if (tn->tn_op != CON || nt == VOID) {
1635 ntn->tn_val = tgetblk(sizeof (val_t));
1636 cvtcon(op, arg, ntn->tn_type, ntn->tn_val, tn->tn_val);
1643 * Print a warning if a prototype causes a type conversion that is
1644 * different from what would happen to the same argument in the
1645 * absence of a prototype.
1647 * Errors/Warnings about illegal type combinations are already printed
1651 ptconv(int arg, tspec_t nt, tspec_t ot, type_t *tp, tnode_t *tn)
1655 if (!isatyp(nt) || !isatyp(ot))
1659 * If the type of the formal parameter is char/short, a warning
1660 * would be useless, because functions declared the old style
1661 * can't expect char/short arguments.
1663 if (nt == CHAR || nt == UCHAR || nt == SHORT || nt == USHORT)
1666 /* get default promotion */
1667 ptn = promote(NOOP, 1, tn);
1668 ot = ptn->tn_type->t_tspec;
1670 /* return if types are the same with and without prototype */
1671 if (nt == ot || (nt == ENUM && ot == INT))
1674 if (isftyp(nt) != isftyp(ot) || psize(nt) != psize(ot)) {
1675 /* representation and/or width change */
1676 if (styp(nt) != SHORT || !isityp(ot) || psize(ot) > psize(INT))
1677 /* conversion to '%s' due to prototype, arg #%d */
1678 warning(259, tyname(tp), arg);
1681 * they differ in sign or base type (char, short, int,
1682 * long, long long, float, double, long double)
1684 * if they differ only in sign and the argument is a constant
1685 * and the msb of the argument is not set, print no warning
1687 if (ptn->tn_op == CON && isityp(nt) && styp(nt) == styp(ot) &&
1688 msb(ptn->tn_val->v_quad, ot, -1) == 0) {
1691 /* conversion to '%s' due to prototype, arg #%d */
1692 warning(259, tyname(tp), arg);
1698 * Print warnings for conversions of integer types which my cause
1703 iiconv(op_t op, int arg, tspec_t nt, tspec_t ot, type_t *tp, tnode_t *tn)
1705 if (tn->tn_op == CON)
1712 if (psize(nt) > psize(ot) && isutyp(nt) != isutyp(ot)) {
1713 /* conversion to %s may sign-extend incorrectly (, arg #%d) */
1714 if (aflag && pflag) {
1716 warning(297, tyname(tp), arg);
1718 warning(131, tyname(tp));
1724 if (psize(nt) < psize(ot) &&
1725 (ot == LONG || ot == ULONG || ot == QUAD || ot == UQUAD ||
1727 /* conversion from '%s' may lose accuracy */
1730 warning(298, tyname(tn->tn_type), arg);
1732 warning(132, tyname(tn->tn_type));
1739 * Print warnings for dubious conversions of pointer to integer.
1742 piconv(op_t op, tspec_t nt, type_t *tp, tnode_t *tn)
1744 if (tn->tn_op == CON)
1748 /* We got already an error. */
1752 if (psize(nt) < psize(PTR)) {
1753 if (pflag && size(nt) >= size(PTR)) {
1754 /* conv. of pointer to %s may lose bits */
1755 warning(134, tyname(tp));
1757 /* conv. of pointer to %s loses bits */
1758 warning(133, tyname(tp));
1764 * Print warnings for questionable pointer conversions.
1767 ppconv(op_t op, tnode_t *tn, type_t *tp)
1770 const char *nts, *ots;
1773 * We got already an error (pointers of different types
1774 * without a cast) or we will not get a warning.
1779 nt = tp->t_subt->t_tspec;
1780 ot = tn->tn_type->t_subt->t_tspec;
1782 if (nt == VOID || ot == VOID) {
1783 if (sflag && (nt == FUNC || ot == FUNC)) {
1784 /* (void *)0 already handled in convert() */
1785 *(nt == FUNC ? &nts : &ots) = "function pointer";
1786 *(nt == VOID ? &nts : &ots) = "'void *'";
1787 /* ANSI C forbids conversion of %s to %s */
1788 warning(303, ots, nts);
1791 } else if (nt == FUNC && ot == FUNC) {
1793 } else if (nt == FUNC || ot == FUNC) {
1794 /* questionable conversion of function pointer */
1799 if (getbound(tp->t_subt) > getbound(tn->tn_type->t_subt)) {
1801 /* possible pointer alignment problem */
1804 if (((nt == STRUCT || nt == UNION) &&
1805 tp->t_subt->t_str != tn->tn_type->t_subt->t_str) ||
1806 psize(nt) != psize(ot)) {
1808 /* pointer casts may be troublesome */
1815 * Converts a typed constant in a constant of another type.
1817 * op operator which requires conversion
1818 * arg if op is FARG, # of argument
1819 * tp type in which to convert the constant
1824 cvtcon(op_t op, int arg, type_t *tp, val_t *nv, val_t *v)
1829 quad_t xmask, xmsk1;
1833 nt = nv->v_tspec = tp->t_tspec;
1836 if (ot == FLOAT || ot == DOUBLE || ot == LDOUBLE) {
1839 max = CHAR_MAX; min = CHAR_MIN; break;
1841 max = UCHAR_MAX; min = 0; break;
1843 max = SCHAR_MAX; min = SCHAR_MIN; break;
1845 max = SHRT_MAX; min = SHRT_MIN; break;
1847 max = USHRT_MAX; min = 0; break;
1850 max = INT_MAX; min = INT_MIN; break;
1852 max = (u_int)UINT_MAX; min = 0; break;
1854 max = LONG_MAX; min = LONG_MIN; break;
1856 max = (u_long)ULONG_MAX; min = 0; break;
1858 max = QUAD_MAX; min = QUAD_MIN; break;
1860 max = (u_quad_t)UQUAD_MAX; min = 0; break;
1862 max = FLT_MAX; min = -FLT_MAX; break;
1864 max = DBL_MAX; min = -DBL_MAX; break;
1866 /* Got already an error because of float --> ptr */
1868 max = LDBL_MAX; min = -LDBL_MAX; break;
1870 lerror("cvtcon() 1");
1872 if (v->v_ldbl > max || v->v_ldbl < min) {
1874 lerror("cvtcon() 2");
1876 /* conv. of %s to %s is out of rng., arg #%d */
1877 warning(295, tyname(gettyp(ot)), tyname(tp),
1880 /* conversion of %s to %s is out of range */
1881 warning(119, tyname(gettyp(ot)), tyname(tp));
1883 v->v_ldbl = v->v_ldbl > 0 ? max : min;
1886 nv->v_ldbl = (float)v->v_ldbl;
1887 } else if (nt == DOUBLE) {
1888 nv->v_ldbl = (double)v->v_ldbl;
1889 } else if (nt == LDOUBLE) {
1890 nv->v_ldbl = v->v_ldbl;
1892 nv->v_quad = (nt == PTR || isutyp(nt)) ?
1893 (u_quad_t)v->v_ldbl : (quad_t)v->v_ldbl;
1897 nv->v_ldbl = (ot == PTR || isutyp(ot)) ?
1898 (float)(u_quad_t)v->v_quad : (float)v->v_quad;
1899 } else if (nt == DOUBLE) {
1900 nv->v_ldbl = (ot == PTR || isutyp(ot)) ?
1901 (double)(u_quad_t)v->v_quad : (double)v->v_quad;
1902 } else if (nt == LDOUBLE) {
1903 nv->v_ldbl = (ot == PTR || isutyp(ot)) ?
1904 (ldbl_t)(u_quad_t)v->v_quad : (ldbl_t)v->v_quad;
1906 rchk = 1; /* Check for lost precision. */
1907 nv->v_quad = v->v_quad;
1911 if (v->v_ansiu && isftyp(nt)) {
1912 /* ANSI C treats constant as unsigned */
1915 } else if (v->v_ansiu && (isityp(nt) && !isutyp(nt) &&
1916 psize(nt) > psize(ot))) {
1917 /* ANSI C treats constant as unsigned */
1922 if (nt != FLOAT && nt != DOUBLE && nt != LDOUBLE) {
1923 sz = tp->t_isfield ? tp->t_flen : size(nt);
1924 nv->v_quad = xsign(nv->v_quad, nt, sz);
1927 if (rchk && op != CVT) {
1929 nsz = tp->t_isfield ? tp->t_flen : size(nt);
1930 xmask = qlmasks[nsz] ^ qlmasks[osz];
1931 xmsk1 = qlmasks[nsz] ^ qlmasks[osz - 1];
1933 * For bitwise operations we are not interested in the
1934 * value, but in the bits itself.
1936 if (op == ORASS || op == OR || op == XOR) {
1938 * Print a warning if bits which were set are
1939 * lost due to the conversion.
1940 * This can happen with operator ORASS only.
1942 if (nsz < osz && (v->v_quad & xmask) != 0) {
1943 /* constant truncated by conv., op %s */
1944 warning(306, modtab[op].m_name);
1946 } else if (op == ANDASS || op == AND) {
1948 * Print a warning if additional bits are not all 1
1949 * and the most significant bit of the old value is 1,
1950 * or if at least one (but not all) removed bit was 0.
1953 (nv->v_quad & qbmasks[osz - 1]) != 0 &&
1954 (nv->v_quad & xmask) != xmask) {
1956 * extra bits set to 0 in conversion
1957 * of '%s' to '%s', op %s
1959 warning(309, tyname(gettyp(ot)),
1960 tyname(tp), modtab[op].m_name);
1961 } else if (nsz < osz &&
1962 (v->v_quad & xmask) != xmask &&
1963 (v->v_quad & xmask) != 0) {
1964 /* const. truncated by conv., op %s */
1965 warning(306, modtab[op].m_name);
1967 } else if ((nt != PTR && isutyp(nt)) &&
1968 (ot != PTR && !isutyp(ot)) && v->v_quad < 0) {
1970 /* assignment of negative constant to ... */
1972 } else if (op == INIT) {
1973 /* initialisation of unsigned with neg. ... */
1975 } else if (op == FARG) {
1976 /* conversion of neg. const. to ..., arg #%d */
1978 } else if (modtab[op].m_comp) {
1979 /* we get this warning already in chkcomp() */
1981 /* conversion of negative constant to ... */
1984 } else if (nv->v_quad != v->v_quad && nsz <= osz &&
1985 (v->v_quad & xmask) != 0 &&
1986 (isutyp(ot) || (v->v_quad & xmsk1) != xmsk1)) {
1988 * Loss of significant bit(s). All truncated bits
1989 * of unsigned types or all truncated bits plus the
1990 * msb of the target for signed types are considered
1991 * to be significant bits. Loss of significant bits
1992 * means that at least on of the bits was set in an
1993 * unsigned type or that at least one, but not all of
1994 * the bits was set in an signed type.
1995 * Loss of significant bits means that it is not
1996 * possible, also not with necessary casts, to convert
1997 * back to the original type. A example for a
1998 * necessary cast is:
1999 * char c; int i; c = 128;
2000 * i = c; ** yields -128 **
2001 * i = (unsigned char)c; ** yields 128 **
2003 if (op == ASSIGN && tp->t_isfield) {
2004 /* precision lost in bit-field assignment */
2006 } else if (op == ASSIGN) {
2007 /* constant truncated by assignment */
2009 } else if (op == INIT && tp->t_isfield) {
2010 /* bit-field initializer does not fit */
2012 } else if (op == INIT) {
2013 /* initializer does not fit */
2015 } else if (op == CASE) {
2016 /* case label affected by conversion */
2018 } else if (op == FARG) {
2019 /* conv. of %s to %s is out of rng., arg #%d */
2020 warning(295, tyname(gettyp(ot)), tyname(tp),
2023 /* conversion of %s to %s is out of range */
2024 warning(119, tyname(gettyp(ot)), tyname(tp));
2026 } else if (nv->v_quad != v->v_quad) {
2027 if (op == ASSIGN && tp->t_isfield) {
2028 /* precision lost in bit-field assignment */
2030 } else if (op == INIT && tp->t_isfield) {
2031 /* bit-field initializer out of range */
2033 } else if (op == CASE) {
2034 /* case label affected by conversion */
2036 } else if (op == FARG) {
2037 /* conv. of %s to %s is out of rng., arg #%d */
2038 warning(295, tyname(gettyp(ot)), tyname(tp),
2041 /* conversion of %s to %s is out of range */
2042 warning(119, tyname(gettyp(ot)), tyname(tp));
2049 * Called if incompatible types were detected.
2050 * Prints a appropriate warning.
2053 incompat(op_t op, tspec_t lt, tspec_t rt)
2059 if (lt == VOID || (mp->m_binary && rt == VOID)) {
2060 /* void type illegal in expression */
2062 } else if (op == ASSIGN) {
2063 if ((lt == STRUCT || lt == UNION) &&
2064 (rt == STRUCT || rt == UNION)) {
2065 /* assignment of different structures */
2068 /* assignment type mismatch */
2071 } else if (mp->m_binary) {
2072 /* operands of %s have incompatible types */
2073 error(107, mp->m_name);
2075 /* operand of %s has incompatible type */
2076 error(108, mp->m_name);
2081 * Called if incompatible pointer types are detected.
2082 * Print an appropriate warning.
2085 illptrc(mod_t *mp, type_t *ltp, type_t *rtp)
2089 if (ltp->t_tspec != PTR || rtp->t_tspec != PTR)
2090 lerror("illptrc() 1");
2092 lt = ltp->t_subt->t_tspec;
2093 rt = rtp->t_subt->t_tspec;
2095 if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION)) {
2097 /* illegal structure pointer combination */
2100 /* illegal structure pointer combination, op %s */
2101 warning(245, mp->m_name);
2105 /* illegal pointer combination */
2108 /* illegal pointer combination, op %s */
2109 warning(124, mp->m_name);
2115 * Make sure type (*tpp)->t_subt has at least the qualifiers
2116 * of tp1->t_subt and tp2->t_subt.
2119 mrgqual(type_t **tpp, type_t *tp1, type_t *tp2)
2121 if ((*tpp)->t_tspec != PTR ||
2122 tp1->t_tspec != PTR || tp2->t_tspec != PTR) {
2123 lerror("mrgqual()");
2126 if ((*tpp)->t_subt->t_const ==
2127 (tp1->t_subt->t_const | tp2->t_subt->t_const) &&
2128 (*tpp)->t_subt->t_volatile ==
2129 (tp1->t_subt->t_volatile | tp2->t_subt->t_volatile)) {
2133 *tpp = tduptyp(*tpp);
2134 (*tpp)->t_subt = tduptyp((*tpp)->t_subt);
2135 (*tpp)->t_subt->t_const =
2136 tp1->t_subt->t_const | tp2->t_subt->t_const;
2137 (*tpp)->t_subt->t_volatile =
2138 tp1->t_subt->t_volatile | tp2->t_subt->t_volatile;
2142 * Returns 1 if the given structure or union has a constant member
2143 * (maybe recursively).
2151 if ((t = tp->t_tspec) != STRUCT && t != UNION)
2152 lerror("conmemb()");
2153 for (m = tp->t_str->memb; m != NULL; m = m->s_nxt) {
2157 if ((t = tp->t_tspec) == STRUCT || t == UNION) {
2158 if (conmemb(m->s_type))
2171 if ((t = tp->t_tspec) == INT && tp->t_isenum)
2175 case CHAR: s = "char"; break;
2176 case UCHAR: s = "unsigned char"; break;
2177 case SCHAR: s = "signed char"; break;
2178 case SHORT: s = "short"; break;
2179 case USHORT: s = "unsigned short"; break;
2180 case INT: s = "int"; break;
2181 case UINT: s = "unsigned int"; break;
2182 case LONG: s = "long"; break;
2183 case ULONG: s = "unsigned long"; break;
2184 case QUAD: s = "long long"; break;
2185 case UQUAD: s = "unsigned long long"; break;
2186 case FLOAT: s = "float"; break;
2187 case DOUBLE: s = "double"; break;
2188 case LDOUBLE: s = "long double"; break;
2189 case PTR: s = "pointer"; break;
2190 case ENUM: s = "enum"; break;
2191 case STRUCT: s = "struct"; break;
2192 case UNION: s = "union"; break;
2193 case FUNC: s = "function"; break;
2194 case ARRAY: s = "array"; break;
2202 * Create a new node for one of the operators POINT and ARROW.
2205 bldstr(op_t op, tnode_t *ln, tnode_t *rn)
2210 if (rn->tn_op != NAME)
2211 lerror("bldstr() 1");
2212 if (rn->tn_sym->s_value.v_tspec != INT)
2213 lerror("bldstr() 2");
2214 if (rn->tn_sym->s_scl != MOS && rn->tn_sym->s_scl != MOU)
2215 lerror("bldstr() 3");
2218 * Remember if the left operand is an lvalue (structure members
2219 * are lvalues if and only if the structure itself is an lvalue).
2221 nolval = op == POINT && !ln->tn_lvalue;
2224 ln = bldamper(ln, 1);
2225 } else if (ln->tn_type->t_tspec != PTR) {
2226 if (!tflag || !isityp(ln->tn_type->t_tspec))
2227 lerror("bldstr() 4");
2228 ln = convert(NOOP, 0, tincref(gettyp(VOID), PTR), ln);
2232 ctn = getinode(LONG, rn->tn_sym->s_value.v_quad / CHAR_BIT);
2234 ctn = getinode(INT, rn->tn_sym->s_value.v_quad / CHAR_BIT);
2237 ntn = mktnode(PLUS, tincref(rn->tn_type, PTR), ln, ctn);
2238 if (ln->tn_op == CON)
2241 if (rn->tn_type->t_isfield) {
2242 ntn = mktnode(FSEL, ntn->tn_type->t_subt, ntn, NULL);
2244 ntn = mktnode(STAR, ntn->tn_type->t_subt, ntn, NULL);
2254 * Create a node for INCAFT, INCBEF, DECAFT and DECBEF.
2257 bldincdec(op_t op, tnode_t *ln)
2262 lerror("bldincdec() 1");
2264 if (ln->tn_type->t_tspec == PTR) {
2265 cn = plength(ln->tn_type);
2267 cn = getinode(INT, (quad_t)1);
2269 ntn = mktnode(op, ln->tn_type, ln, cn);
2275 * Create a tree node for the & operator
2278 bldamper(tnode_t *tn, int noign)
2283 if (!noign && ((t = tn->tn_type->t_tspec) == ARRAY || t == FUNC)) {
2284 /* & before array or function: ignored */
2291 if (tn->tn_op == STAR &&
2292 tn->tn_left->tn_type->t_tspec == PTR &&
2293 tn->tn_left->tn_type->t_subt == tn->tn_type) {
2294 return (tn->tn_left);
2297 ntn = mktnode(AMPER, tincref(tn->tn_type, PTR), tn, NULL);
2303 * Create a node for operators PLUS and MINUS.
2306 bldplmi(op_t op, tnode_t *ln, tnode_t *rn)
2311 /* If pointer and integer, then pointer to the lhs. */
2312 if (rn->tn_type->t_tspec == PTR && isityp(ln->tn_type->t_tspec)) {
2318 if (ln->tn_type->t_tspec == PTR && rn->tn_type->t_tspec != PTR) {
2320 if (!isityp(rn->tn_type->t_tspec))
2321 lerror("bldplmi() 1");
2323 ctn = plength(ln->tn_type);
2324 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
2325 rn = convert(NOOP, 0, ctn->tn_type, rn);
2326 rn = mktnode(MULT, rn->tn_type, rn, ctn);
2327 if (rn->tn_left->tn_op == CON)
2329 ntn = mktnode(op, ln->tn_type, ln, rn);
2331 } else if (rn->tn_type->t_tspec == PTR) {
2333 if (ln->tn_type->t_tspec != PTR || op != MINUS)
2334 lerror("bldplmi() 2");
2340 ntn = mktnode(op, tp, ln, rn);
2341 if (ln->tn_op == CON && rn->tn_op == CON)
2343 ctn = plength(ln->tn_type);
2344 balance(NOOP, &ntn, &ctn);
2345 ntn = mktnode(DIV, tp, ntn, ctn);
2349 ntn = mktnode(op, ln->tn_type, ln, rn);
2356 * Create a node for operators SHL and SHR.
2359 bldshft(op_t op, tnode_t *ln, tnode_t *rn)
2364 if ((t = rn->tn_type->t_tspec) != INT && t != UINT)
2365 rn = convert(CVT, 0, gettyp(INT), rn);
2366 ntn = mktnode(op, ln->tn_type, ln, rn);
2371 * Create a node for COLON.
2374 bldcol(tnode_t *ln, tnode_t *rn)
2376 tspec_t lt, rt, pdt;
2380 lt = ln->tn_type->t_tspec;
2381 rt = rn->tn_type->t_tspec;
2389 * Arithmetic types are balanced, all other type combinations
2390 * still need to be handled.
2392 if (isatyp(lt) && isatyp(rt)) {
2394 } else if (lt == VOID || rt == VOID) {
2396 } else if (lt == STRUCT || lt == UNION) {
2397 /* Both types must be identical. */
2398 if (rt != STRUCT && rt != UNION)
2399 lerror("bldcol() 1");
2400 if (ln->tn_type->t_str != rn->tn_type->t_str)
2401 lerror("bldcol() 2");
2402 if (incompl(ln->tn_type)) {
2403 /* unknown operand size, op %s */
2404 error(138, modtab[COLON].m_name);
2408 } else if (lt == PTR && isityp(rt)) {
2410 rn = convert(NOOP, 0, gettyp(pdt), rn);
2414 } else if (rt == PTR && isityp(lt)) {
2416 ln = convert(NOOP, 0, gettyp(pdt), ln);
2420 } else if (lt == PTR && ln->tn_type->t_subt->t_tspec == VOID) {
2422 lerror("bldcol() 4");
2424 mrgqual(&rtp, ln->tn_type, rn->tn_type);
2425 } else if (rt == PTR && rn->tn_type->t_subt->t_tspec == VOID) {
2427 lerror("bldcol() 5");
2429 mrgqual(&rtp, ln->tn_type, rn->tn_type);
2431 if (lt != PTR || rt != PTR)
2432 lerror("bldcol() 6");
2434 * XXX For now we simply take the left type. This is
2435 * probably wrong, if one type contains a functionprototype
2436 * and the other one, at the same place, only an old style
2440 mrgqual(&rtp, ln->tn_type, rn->tn_type);
2443 ntn = mktnode(COLON, rtp, ln, rn);
2449 * Create a node for an assignment operator (both = and op= ).
2452 bldasgn(op_t op, tnode_t *ln, tnode_t *rn)
2457 if (ln == NULL || rn == NULL)
2458 lerror("bldasgn() 1");
2460 lt = ln->tn_type->t_tspec;
2461 rt = rn->tn_type->t_tspec;
2463 if ((op == ADDASS || op == SUBASS) && lt == PTR) {
2465 lerror("bldasgn() 2");
2466 ctn = plength(ln->tn_type);
2467 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
2468 rn = convert(NOOP, 0, ctn->tn_type, rn);
2469 rn = mktnode(MULT, rn->tn_type, rn, ctn);
2470 if (rn->tn_left->tn_op == CON)
2474 if ((op == ASSIGN || op == RETURN) && (lt == STRUCT || rt == STRUCT)) {
2475 if (rt != lt || ln->tn_type->t_str != rn->tn_type->t_str)
2476 lerror("bldasgn() 3");
2477 if (incompl(ln->tn_type)) {
2479 /* cannot return incomplete type */
2482 /* unknown operand size, op %s */
2483 error(138, modtab[op].m_name);
2489 if (op == SHLASS || op == SHRASS) {
2491 rn = convert(NOOP, 0, gettyp(INT), rn);
2495 if (op == ASSIGN || lt != PTR) {
2497 (ln->tn_type->t_isfield && rn->tn_op == CON)) {
2498 rn = convert(op, 0, ln->tn_type, rn);
2504 ntn = mktnode(op, ln->tn_type, ln, rn);
2510 * Get length of type tp->t_subt.
2518 if (tp->t_tspec != PTR)
2519 lerror("plength() 1");
2525 while (tp->t_tspec == ARRAY) {
2530 switch (tp->t_tspec) {
2532 /* pointer to function is not allowed here */
2536 /* cannot do pointer arithmetic on operand of ... */
2541 if ((elsz = tp->t_str->size) == 0)
2542 /* cannot do pointer arithmetic on operand of ... */
2547 /* cannot do pointer arithmetic on operand of ... */
2552 if ((elsz = size(tp->t_tspec)) == 0) {
2553 /* cannot do pointer arithmetic on operand of ... */
2555 } else if (elsz == -1) {
2556 lerror("plength() 2");
2561 if (elem == 0 && elsz != 0) {
2562 /* cannot do pointer arithmetic on operand of ... */
2575 return (getinode(st, (quad_t)(elem * elsz / CHAR_BIT)));
2578 #ifdef XXX_BROKEN_GCC
2580 quad_t_eq(quad_t x, quad_t y)
2586 u_quad_t_eq(u_quad_t x, u_quad_t y)
2593 * Do only as much as necessary to compute constant expressions.
2594 * Called only if the operator allows folding and (both) operands
2603 quad_t sl, sr, q, mask;
2607 v = xcalloc(1, sizeof (val_t));
2608 v->v_tspec = t = tn->tn_type->t_tspec;
2610 utyp = t == PTR || isutyp(t);
2611 ul = sl = tn->tn_left->tn_val->v_quad;
2612 if (modtab[tn->tn_op].m_binary)
2613 ur = sr = tn->tn_right->tn_val->v_quad;
2617 switch (tn->tn_op) {
2623 if (msb(q, t, -1) == msb(sl, t, -1))
2630 q = utyp ? ul * ur : sl * sr;
2631 if (msb(q, t, -1) != (msb(sl, t, -1) ^ msb(sr, t, -1)))
2638 q = utyp ? UQUAD_MAX : QUAD_MAX;
2640 q = utyp ? ul / ur : sl / sr;
2649 q = utyp ? ul % ur : sl % sr;
2653 q = utyp ? ul + ur : sl + sr;
2654 if (msb(sl, t, -1) != 0 && msb(sr, t, -1) != 0) {
2655 if (msb(q, t, -1) == 0)
2657 } else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) == 0) {
2658 if (msb(q, t, -1) != 0)
2663 q = utyp ? ul - ur : sl - sr;
2664 if (msb(sl, t, -1) != 0 && msb(sr, t, -1) == 0) {
2665 if (msb(q, t, -1) == 0)
2667 } else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) != 0) {
2668 if (msb(q, t, -1) != 0)
2673 q = utyp ? ul << sr : sl << sr;
2677 * The sign must be explizitly extended because
2678 * shifts of signed values are implementation dependent.
2681 q = xsign(q, t, size(t) - (int)sr);
2684 q = utyp ? ul < ur : sl < sr;
2687 q = utyp ? ul <= ur : sl <= sr;
2690 q = utyp ? ul >= ur : sl >= sr;
2693 q = utyp ? ul > ur : sl > sr;
2696 #ifdef XXX_BROKEN_GCC
2697 q = utyp ? u_quad_t_eq(ul, ur) : quad_t_eq(sl, sr);
2699 q = utyp ? ul == ur : sl == sr;
2703 q = utyp ? ul != ur : sl != sr;
2706 q = utyp ? ul & ur : sl & sr;
2709 q = utyp ? ul ^ ur : sl ^ sr;
2712 q = utyp ? ul | ur : sl | sr;
2718 mask = qlmasks[size(t)];
2720 /* XXX does not work for quads. */
2721 if (ovfl || ((q | mask) != ~(u_quad_t)0 && (q & ~mask) != 0)) {
2723 /* integer overflow detected, op %s */
2724 warning(141, modtab[tn->tn_op].m_name);
2727 v->v_quad = xsign(q, t, -1);
2729 cn = getcnode(tn->tn_type, v);
2734 #ifdef XXX_BROKEN_GCC
2736 ldbl_t_neq(ldbl_t x, ldbl_t y)
2743 * Same for operators whose operands are compared with 0 (test context).
2746 foldtst(tnode_t *tn)
2751 v = xcalloc(1, sizeof (val_t));
2752 v->v_tspec = tn->tn_type->t_tspec;
2753 if (tn->tn_type->t_tspec != INT)
2754 lerror("foldtst() 1");
2756 if (isftyp(tn->tn_left->tn_type->t_tspec)) {
2757 #ifdef XXX_BROKEN_GCC
2758 l = ldbl_t_neq(tn->tn_left->tn_val->v_ldbl, 0.0);
2760 l = tn->tn_left->tn_val->v_ldbl != 0.0;
2763 l = tn->tn_left->tn_val->v_quad != 0;
2766 if (modtab[tn->tn_op].m_binary) {
2767 if (isftyp(tn->tn_right->tn_type->t_tspec)) {
2768 #ifdef XXX_BROKEN_GCC
2769 r = ldbl_t_neq(tn->tn_right->tn_val->v_ldbl, 0.0);
2771 r = tn->tn_right->tn_val->v_ldbl != 0.0;
2774 r = tn->tn_right->tn_val->v_quad != 0;
2778 switch (tn->tn_op) {
2781 /* constant argument to NOT */
2792 lerror("foldtst() 1");
2795 return (getcnode(tn->tn_type, v));
2799 * Same for operands with floating point type.
2802 foldflt(tnode_t *tn)
2808 v = xcalloc(1, sizeof (val_t));
2809 v->v_tspec = t = tn->tn_type->t_tspec;
2812 lerror("foldflt() 1");
2814 if (t != tn->tn_left->tn_type->t_tspec)
2815 lerror("foldflt() 2");
2816 if (modtab[tn->tn_op].m_binary && t != tn->tn_right->tn_type->t_tspec)
2817 lerror("foldflt() 3");
2819 l = tn->tn_left->tn_val->v_ldbl;
2820 if (modtab[tn->tn_op].m_binary)
2821 r = tn->tn_right->tn_val->v_ldbl;
2823 switch (tn->tn_op) {
2838 v->v_ldbl = l < 0 ? -FLT_MAX : FLT_MAX;
2839 } else if (t == DOUBLE) {
2840 v->v_ldbl = l < 0 ? -DBL_MAX : DBL_MAX;
2842 v->v_ldbl = l < 0 ? -LDBL_MAX : LDBL_MAX;
2873 lerror("foldflt() 4");
2876 if (isnan((double)v->v_ldbl))
2877 lerror("foldflt() 5");
2878 if (isinf((double)v->v_ldbl) ||
2880 (v->v_ldbl > FLT_MAX || v->v_ldbl < -FLT_MAX)) ||
2882 (v->v_ldbl > DBL_MAX || v->v_ldbl < -DBL_MAX))) {
2883 /* floating point overflow detected, op %s */
2884 warning(142, modtab[tn->tn_op].m_name);
2886 v->v_ldbl = v->v_ldbl < 0 ? -FLT_MAX : FLT_MAX;
2887 } else if (t == DOUBLE) {
2888 v->v_ldbl = v->v_ldbl < 0 ? -DBL_MAX : DBL_MAX;
2890 v->v_ldbl = v->v_ldbl < 0 ? -LDBL_MAX: LDBL_MAX;
2894 return (getcnode(tn->tn_type, v));
2898 * Create a constant node for sizeof.
2907 while (tp->t_tspec == ARRAY) {
2912 /* cannot take size of incomplete type */
2916 switch (tp->t_tspec) {
2918 /* cannot take size of function */
2925 /* cannot take size of incomplete type */
2929 elsz = tp->t_str->size;
2934 /* cannot take size of incomplete type */
2939 if (tp->t_isfield) {
2940 /* cannot take size of bit-field */
2943 if (tp->t_tspec == VOID) {
2944 /* cannot take size of void */
2948 elsz = size(tp->t_tspec);
2950 lerror("bldszof() 1");
2961 return (getinode(st, (quad_t)(elem * elsz / CHAR_BIT)));
2968 cast(tnode_t *tn, type_t *tp)
2978 ot = tn->tn_type->t_tspec;
2982 * XXX ANSI C requires scalar types or void (Plauger&Brodie).
2983 * But this seams really questionable.
2985 } else if (nt == STRUCT || nt == UNION || nt == ARRAY || nt == FUNC) {
2986 /* invalid cast expression */
2989 } else if (ot == STRUCT || ot == UNION) {
2990 /* invalid cast expression */
2993 } else if (ot == VOID) {
2994 /* improper cast of void expression */
2997 } else if (isityp(nt) && issclt(ot)) {
2999 } else if (isftyp(nt) && isatyp(ot)) {
3001 } else if (nt == PTR && isityp(ot)) {
3003 } else if (nt == PTR && ot == PTR) {
3004 if (!tp->t_subt->t_const && tn->tn_type->t_subt->t_const) {
3006 /* cast discards 'const' from ... */
3010 /* invalid cast expression */
3015 tn = convert(CVT, 0, tp, tn);
3022 * Create the node for a function argument.
3023 * All necessary conversions and type checks are done in funccall(), because
3024 * in funcarg() we have no information about expected argument types.
3027 funcarg(tnode_t *args, tnode_t *arg)
3032 * If there was a serious error in the expression for the argument,
3033 * create a dummy argument so the positions of the remaining arguments
3037 arg = getinode(INT, (quad_t)0);
3039 ntn = mktnode(PUSH, arg->tn_type, arg, args);
3045 * Create the node for a function call. Also check types of
3046 * function arguments and insert conversions, if necessary.
3049 funccall(tnode_t *func, tnode_t *args)
3057 if (func->tn_op == NAME && func->tn_type->t_tspec == FUNC) {
3064 * after cconv() func will always be a pointer to a function
3065 * if it is a valid function designator.
3069 if (func->tn_type->t_tspec != PTR ||
3070 func->tn_type->t_subt->t_tspec != FUNC) {
3071 /* illegal function */
3076 args = chkfarg(func->tn_type->t_subt, args);
3078 ntn = mktnode(fcop, func->tn_type->t_subt->t_subt, func, args);
3084 * Check types of all function arguments and insert conversions,
3088 chkfarg(type_t *ftp, tnode_t *args)
3093 int narg, npar, n, i;
3095 /* get # of args in the prototype */
3097 for (asym = ftp->t_args; asym != NULL; asym = asym->s_nxt)
3100 /* get # of args in function call */
3102 for (arg = args; arg != NULL; arg = arg->tn_right)
3106 if (ftp->t_proto && npar != narg && !(ftp->t_vararg && npar < narg)) {
3107 /* argument mismatch: %d arg%s passed, %d expected */
3108 error(150, narg, narg > 1 ? "s" : "", npar);
3112 for (n = 1; n <= narg; n++) {
3115 * The rightmost argument is at the top of the argument
3118 for (i = narg, arg = args; i > n; i--, arg = arg->tn_right) ;
3120 /* some things which are always not allowd */
3121 if ((at = arg->tn_left->tn_type->t_tspec) == VOID) {
3122 /* void expressions may not be arguments, arg #%d */
3125 } else if ((at == STRUCT || at == UNION) &&
3126 incompl(arg->tn_left->tn_type)) {
3127 /* argument cannot have unknown size, arg #%d */
3130 } else if (isityp(at) && arg->tn_left->tn_type->t_isenum &&
3131 incompl(arg->tn_left->tn_type)) {
3132 /* argument cannot have unknown size, arg #%d */
3136 /* class conversions (arg in value context) */
3137 arg->tn_left = cconv(arg->tn_left);
3140 arg->tn_left = parg(n, asym->s_type, arg->tn_left);
3142 arg->tn_left = promote(NOOP, 1, arg->tn_left);
3144 arg->tn_type = arg->tn_left->tn_type;
3154 * Compare the type of an argument with the corresponding type of a
3155 * prototype parameter. If it is a valid combination, but both types
3156 * are not the same, insert a conversion to convert the argument into
3157 * the type of the parameter.
3160 parg(int n, type_t *tp, tnode_t *tn)
3165 ln = xcalloc(1, sizeof (tnode_t));
3166 ln->tn_type = tduptyp(tp);
3167 ln->tn_type->t_const = 0;
3169 if (typeok(FARG, n, ln, tn)) {
3170 if (!eqtype(tp, tn->tn_type, 1, 0, (warn = 0, &warn)) || warn)
3171 tn = convert(FARG, n, tp, tn);
3178 * Return the value of an integral constant expression.
3179 * If the expression is not constant or its type is not an integer
3180 * type, an error message is printed.
3183 constant(tnode_t *tn)
3190 tn = promote(NOOP, 0, tn);
3192 v = xcalloc(1, sizeof (val_t));
3196 lerror("constant() 1");
3202 v->v_tspec = tn->tn_type->t_tspec;
3204 if (tn->tn_op == CON) {
3205 if (tn->tn_type->t_tspec != tn->tn_val->v_tspec)
3206 lerror("constant() 2");
3207 if (isityp(tn->tn_val->v_tspec)) {
3208 v->v_ansiu = tn->tn_val->v_ansiu;
3209 v->v_quad = tn->tn_val->v_quad;
3212 v->v_quad = tn->tn_val->v_ldbl;
3217 /* integral constant expression expected */
3220 if (!isityp(v->v_tspec))
3227 * Perform some tests on expressions which can't be done in build() and
3228 * functions called by build(). These tests must be done here because
3229 * we need some information about the context in which the operations
3231 * After all tests are performed, expr() frees the memory which is used
3232 * for the expression.
3235 expr(tnode_t *tn, int vctx, int tctx)
3237 if (tn == NULL && nerr == 0)
3245 /* expr() is also called in global initialisations */
3246 if (dcs->d_ctx != EXTERN)
3249 chkmisc(tn, vctx, tctx, !tctx, 0, 0, 0);
3250 if (tn->tn_op == ASSIGN) {
3252 /* assignment in conditional context */
3254 } else if (tn->tn_op == CON) {
3255 if (hflag && tctx && !ccflg)
3256 /* constant in conditional context */
3259 if (!modtab[tn->tn_op].m_sideeff) {
3261 * for left operands of COMMA this warning is already
3264 if (tn->tn_op != COMMA && !vctx && !tctx)
3270 /* free the tree memory */
3275 nulleff(tnode_t *tn)
3280 while (!modtab[tn->tn_op].m_sideeff) {
3281 if (tn->tn_op == CVT && tn->tn_type->t_tspec == VOID) {
3283 } else if (tn->tn_op == LOGAND || tn->tn_op == LOGOR) {
3285 * && and || have a side effect if the right operand
3286 * has a side effect.
3289 } else if (tn->tn_op == QUEST) {
3291 * ? has a side effect if at least one of its right
3292 * operands has a side effect
3295 } else if (tn->tn_op == COLON) {
3297 * : has a side effect if at least one of its operands
3300 if (modtab[tn->tn_left->tn_op].m_sideeff) {
3302 } else if (modtab[tn->tn_right->tn_op].m_sideeff) {
3311 if (!modtab[tn->tn_op].m_sideeff)
3312 /* expression has null effect */
3317 * Dump an expression to stdout
3318 * only used for debugging
3321 displexpr(tnode_t *tn, int offs)
3326 (void)printf("%*s%s\n", offs, "", "NULL");
3329 (void)printf("%*sop %s ", offs, "", modtab[tn->tn_op].m_name);
3331 if (tn->tn_op == NAME) {
3332 (void)printf("%s: %s ",
3333 tn->tn_sym->s_name, scltoa(tn->tn_sym->s_scl));
3334 } else if (tn->tn_op == CON && isftyp(tn->tn_type->t_tspec)) {
3335 (void)printf("%#g ", (double)tn->tn_val->v_ldbl);
3336 } else if (tn->tn_op == CON && isityp(tn->tn_type->t_tspec)) {
3337 uq = tn->tn_val->v_quad;
3338 (void)printf("0x %08lx %08lx ", (long)(uq >> 32) & 0xffffffffl,
3339 (long)uq & 0xffffffffl);
3340 } else if (tn->tn_op == CON) {
3341 if (tn->tn_type->t_tspec != PTR)
3342 lerror("displexpr() 1");
3343 (void)printf("0x%0*lx ", (int)(sizeof (void *) * CHAR_BIT / 4),
3344 (u_long)tn->tn_val->v_quad);
3345 } else if (tn->tn_op == STRING) {
3346 if (tn->tn_strg->st_tspec == CHAR) {
3347 (void)printf("\"%s\"", tn->tn_strg->st_cp);
3351 n = MB_CUR_MAX * (tn->tn_strg->st_len + 1);
3353 (void)wcstombs(s, tn->tn_strg->st_wcp, n);
3354 (void)printf("L\"%s\"", s);
3358 } else if (tn->tn_op == FSEL) {
3359 (void)printf("o=%d, l=%d ", tn->tn_type->t_foffs,
3360 tn->tn_type->t_flen);
3362 (void)printf("%s\n", ttos(tn->tn_type));
3363 if (tn->tn_op == NAME || tn->tn_op == CON || tn->tn_op == STRING)
3365 displexpr(tn->tn_left, offs + 2);
3366 if (modtab[tn->tn_op].m_binary ||
3367 (tn->tn_op == PUSH && tn->tn_right != NULL)) {
3368 displexpr(tn->tn_right, offs + 2);
3373 * Called by expr() to recursively perform some tests.
3377 chkmisc(tnode_t *tn, int vctx, int tctx, int eqwarn, int fcall, int rvdisc,
3382 int nrvdisc, cvctx, ctctx;
3392 mp = &modtab[op = tn->tn_op];
3396 if (ln->tn_op == NAME && (reached || rchflg)) {
3398 setsflg(ln->tn_sym);
3399 setuflg(ln->tn_sym, fcall, szof);
3401 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3402 /* check the range of array indices */
3403 chkaidx(ln->tn_left, 1);
3406 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3407 /* check the range of array indices */
3408 chkaidx(ln->tn_left, 0);
3425 if (ln->tn_op == NAME && (reached || rchflg)) {
3426 sc = ln->tn_sym->s_scl;
3428 * Look if there was a asm statement in one of the
3429 * compound statements we are in. If not, we don't
3432 for (di = dcs; di != NULL; di = di->d_nxt) {
3436 if (sc != EXTERN && sc != STATIC &&
3437 !ln->tn_sym->s_set && !szof && di == NULL) {
3438 /* %s may be used before set */
3439 warning(158, ln->tn_sym->s_name);
3440 setsflg(ln->tn_sym);
3442 setuflg(ln->tn_sym, 0, 0);
3446 if (ln->tn_op == NAME && !szof && (reached || rchflg)) {
3447 setsflg(ln->tn_sym);
3448 if (ln->tn_sym->s_scl == EXTERN)
3451 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3452 /* check the range of array indices */
3453 chkaidx(ln->tn_left, 0);
3456 if (ln->tn_op != AMPER || ln->tn_left->tn_op != NAME)
3457 lerror("chkmisc() 1");
3459 outcall(tn, vctx || tctx, rvdisc);
3462 /* equality operator "==" found where "=" was exp. */
3463 if (hflag && eqwarn)
3470 /* LINTED (enumeration values not handled in switch) */
3478 * values of operands of ':' are not used if the type of at least
3479 * one of the operands (for gcc compatibility) is void
3480 * XXX test/value context of QUEST should probably be used as
3481 * context for both operands of COLON
3483 if (op == COLON && tn->tn_type->t_tspec == VOID)
3485 nrvdisc = op == CVT && tn->tn_type->t_tspec == VOID;
3486 chkmisc(ln, cvctx, ctctx, mp->m_eqwarn, op == CALL, nrvdisc, szof);
3491 chkmisc(rn, 0, 0, mp->m_eqwarn, 0, 0, szof);
3495 chkmisc(rn, 0, 1, mp->m_eqwarn, 0, 0, szof);
3498 chkmisc(rn, cvctx, ctctx, mp->m_eqwarn, 0, 0, szof);
3502 chkmisc(rn, 1, 0, mp->m_eqwarn, 0, 0, szof);
3509 * Checks the range of array indices, if possible.
3510 * amper is set if only the address of the element is used. This
3511 * means that the index is allowd to refere to the first element
3515 chkaidx(tnode_t *tn, int amper)
3525 /* We can only check constant indices. */
3526 if (rn->tn_op != CON)
3529 /* Return if the left node does not stem from an array. */
3530 if (ln->tn_op != AMPER)
3532 if (ln->tn_left->tn_op != STRING && ln->tn_left->tn_op != NAME)
3534 if (ln->tn_left->tn_type->t_tspec != ARRAY)
3538 * For incomplete array types, we can print a warning only if
3539 * the index is negative.
3541 if (incompl(ln->tn_left->tn_type) && rn->tn_val->v_quad >= 0)
3544 /* Get the size of one array element */
3545 if ((elsz = length(ln->tn_type->t_subt, NULL)) == 0)
3549 /* Change the unit of the index from bytes to element size. */
3550 if (isutyp(rn->tn_type->t_tspec)) {
3551 con = (u_quad_t)rn->tn_val->v_quad / elsz;
3553 con = rn->tn_val->v_quad / elsz;
3556 dim = ln->tn_left->tn_type->t_dim + (amper ? 1 : 0);
3558 if (!isutyp(rn->tn_type->t_tspec) && con < 0) {
3559 /* array subscript cannot be negative: %ld */
3560 warning(167, (long)con);
3561 } else if (dim > 0 && (u_quad_t)con >= dim) {
3562 /* array subscript cannot be > %d: %ld */
3563 warning(168, dim - 1, (long)con);
3568 * Check for ordered comparisions of unsigned values with 0.
3571 chkcomp(op_t op, tnode_t *ln, tnode_t *rn)
3576 lt = ln->tn_type->t_tspec;
3577 rt = rn->tn_type->t_tspec;
3580 if (ln->tn_op != CON && rn->tn_op != CON)
3583 if (!isityp(lt) || !isityp(rt))
3586 if ((hflag || pflag) && lt == CHAR && rn->tn_op == CON &&
3587 (rn->tn_val->v_quad < 0 ||
3588 rn->tn_val->v_quad > ~(~0 << (CHAR_BIT - 1)))) {
3589 /* nonportable character comparision, op %s */
3590 warning(230, mp->m_name);
3593 if ((hflag || pflag) && rt == CHAR && ln->tn_op == CON &&
3594 (ln->tn_val->v_quad < 0 ||
3595 ln->tn_val->v_quad > ~(~0 << (CHAR_BIT - 1)))) {
3596 /* nonportable character comparision, op %s */
3597 warning(230, mp->m_name);
3600 if (isutyp(lt) && !isutyp(rt) &&
3601 rn->tn_op == CON && rn->tn_val->v_quad <= 0) {
3602 if (rn->tn_val->v_quad < 0) {
3603 /* comparision of %s with %s, op %s */
3604 warning(162, tyname(ln->tn_type), "negative constant",
3606 } else if (op == LT || op == GE || (hflag && op == LE)) {
3607 /* comparision of %s with %s, op %s */
3608 warning(162, tyname(ln->tn_type), "0", mp->m_name);
3612 if (isutyp(rt) && !isutyp(lt) &&
3613 ln->tn_op == CON && ln->tn_val->v_quad <= 0) {
3614 if (ln->tn_val->v_quad < 0) {
3615 /* comparision of %s with %s, op %s */
3616 warning(162, "negative constant", tyname(rn->tn_type),
3618 } else if (op == GT || op == LE || (hflag && op == GE)) {
3619 /* comparision of %s with %s, op %s */
3620 warning(162, "0", tyname(rn->tn_type), mp->m_name);
3627 * Takes an expression an returns 0 if this expression can be used
3628 * for static initialisation, otherwise -1.
3630 * Constant initialisation expressions must be costant or an address
3631 * of a static object with an optional offset. In the first case,
3632 * the result is returned in *offsp. In the second case, the static
3633 * object is returned in *symp and the offset in *offsp.
3635 * The expression can consist of PLUS, MINUS, AMPER, NAME, STRING and
3636 * CON. Type conversions are allowed if they do not change binary
3637 * representation (including width).
3640 conaddr(tnode_t *tn, sym_t **symp, ptrdiff_t *offsp)
3643 ptrdiff_t offs1, offs2;
3646 switch (tn->tn_op) {
3648 if (tn->tn_right->tn_op != CON)
3653 if (tn->tn_left->tn_op == CON) {
3654 offs1 = (ptrdiff_t)tn->tn_left->tn_val->v_quad;
3655 if (conaddr(tn->tn_right, &sym, &offs2) == -1)
3657 } else if (tn->tn_right->tn_op == CON) {
3658 offs2 = (ptrdiff_t)tn->tn_right->tn_val->v_quad;
3659 if (tn->tn_op == MINUS)
3661 if (conaddr(tn->tn_left, &sym, &offs1) == -1)
3667 *offsp = offs1 + offs2;
3670 if (tn->tn_left->tn_op == NAME) {
3671 *symp = tn->tn_left->tn_sym;
3673 } else if (tn->tn_left->tn_op == STRING) {
3675 * If this would be the front end of a compiler we
3676 * would return a label instead of 0.
3682 t = tn->tn_type->t_tspec;
3683 ot = tn->tn_left->tn_type->t_tspec;
3684 if ((!isityp(t) && t != PTR) || (!isityp(ot) && ot != PTR)) {
3686 } else if (psize(t) != psize(ot)) {
3689 if (conaddr(tn->tn_left, symp, offsp) == -1)
3699 * Concatenate two string constants.
3702 catstrg(strg_t *strg1, strg_t *strg2)
3704 size_t len1, len2, len;
3706 if (strg1->st_tspec != strg2->st_tspec) {
3707 /* cannot concatenate wide and regular string literals */
3712 len = (len1 = strg1->st_len) + (len2 = strg2->st_len);
3714 if (strg1->st_tspec == CHAR) {
3715 strg1->st_cp = xrealloc(strg1->st_cp, len + 1);
3716 (void)memcpy(strg1->st_cp + len1, strg2->st_cp, len2 + 1);
3719 strg1->st_wcp = xrealloc(strg1->st_wcp,
3720 (len + 1) * sizeof (wchar_t));
3721 (void)memcpy(strg1->st_wcp + len1, strg2->st_wcp,
3722 (len2 + 1) * sizeof (wchar_t));
3723 free(strg2->st_wcp);
3731 * Print a warning if the given node has operands which should be
3734 * XXX Does not work if an operand is a constant expression. Constant
3735 * expressions are already folded.
3738 precconf(tnode_t *tn)
3749 mp = &modtab[tn->tn_op];
3752 for (ln = tn->tn_left; ln->tn_op == CVT; ln = ln->tn_left)
3753 lparn |= ln->tn_parn;
3754 lparn |= ln->tn_parn;
3759 for (rn = tn->tn_right; tn->tn_op == CVT; rn = rn->tn_left)
3760 rparn |= rn->tn_parn;
3761 rparn |= rn->tn_parn;
3767 switch (tn->tn_op) {
3770 if (!lparn && (lop == PLUS || lop == MINUS)) {
3772 } else if (!rparn && (rop == PLUS || rop == MINUS)) {
3777 if (!lparn && lop == LOGAND) {
3779 } else if (!rparn && rop == LOGAND) {
3786 if (!lparn && lop != tn->tn_op) {
3787 if (lop == PLUS || lop == MINUS) {
3789 } else if (lop == AND || lop == XOR) {
3793 if (!warn && !rparn && rop != tn->tn_op) {
3794 if (rop == PLUS || rop == MINUS) {
3796 } else if (rop == AND || rop == XOR) {
3801 /* LINTED (enumeration values not handled in switch) */
3807 /* precedence confusion possible: parenthesize! */