1 /* $NetBSD: tree.c,v 1.45 2008/03/04 02:41:46 christos 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.
34 #include <sys/cdefs.h>
35 #if defined(__RCSID) && !defined(lint)
36 __RCSID("$NetBSD: tree.c,v 1.45 2008/03/04 02:41:46 christos Exp $");
38 __FBSDID("$FreeBSD$");
49 /* Various flags for each operator. */
50 static mod_t modtab[NOPS];
52 static tnode_t *getinode(tspec_t, int64_t);
53 static void ptrcmpok(op_t, tnode_t *, tnode_t *);
54 static int asgntypok(op_t, int, tnode_t *, tnode_t *);
55 static void chkbeop(op_t, tnode_t *, tnode_t *);
56 static void chkeop2(op_t, int, tnode_t *, tnode_t *);
57 static void chkeop1(op_t, int, tnode_t *, tnode_t *);
58 static tnode_t *mktnode(op_t, type_t *, tnode_t *, tnode_t *);
59 static void balance(op_t, tnode_t **, tnode_t **);
60 static void incompat(op_t, tspec_t, tspec_t);
61 static void illptrc(mod_t *, type_t *, type_t *);
62 static void mrgqual(type_t **, type_t *, type_t *);
63 static int conmemb(type_t *);
64 static void ptconv(int, tspec_t, tspec_t, type_t *, tnode_t *);
65 static void iiconv(op_t, int, tspec_t, tspec_t, type_t *, tnode_t *);
66 static void piconv(op_t, tspec_t, type_t *, tnode_t *);
67 static void ppconv(op_t, tnode_t *, type_t *);
68 static tnode_t *bldstr(op_t, tnode_t *, tnode_t *);
69 static tnode_t *bldincdec(op_t, tnode_t *);
70 static tnode_t *bldamper(tnode_t *, int);
71 static tnode_t *bldplmi(op_t, tnode_t *, tnode_t *);
72 static tnode_t *bldshft(op_t, tnode_t *, tnode_t *);
73 static tnode_t *bldcol(tnode_t *, tnode_t *);
74 static tnode_t *bldasgn(op_t, tnode_t *, tnode_t *);
75 static tnode_t *plength(type_t *);
76 static tnode_t *fold(tnode_t *);
77 static tnode_t *foldtst(tnode_t *);
78 static tnode_t *foldflt(tnode_t *);
79 static tnode_t *chkfarg(type_t *, tnode_t *);
80 static tnode_t *parg(int, type_t *, tnode_t *);
81 static void nulleff(tnode_t *);
82 static void displexpr(tnode_t *, int);
83 static void chkaidx(tnode_t *, int);
84 static void chkcomp(op_t, tnode_t *, tnode_t *);
85 static void precconf(tnode_t *);
88 * Initialize mods of operators.
97 { ARROW, { 1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,
99 { POINT, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
101 { NOT, { 0,1,0,1,0,1,0,1,0,0,0,0,0,0,0,1,0,
103 { COMPL, { 0,0,1,0,0,1,1,0,0,0,0,0,0,0,0,1,1,
105 { INCBEF, { 0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,
107 { DECBEF, { 0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,
109 { INCAFT, { 0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,
111 { DECAFT, { 0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,
113 { UPLUS, { 0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,1,
115 { UMINUS, { 0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,1,1,
117 { STAR, { 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,
119 { AMPER, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
121 { MULT, { 1,0,0,0,1,1,1,0,1,0,0,1,0,0,0,1,1,
123 { DIV, { 1,0,0,0,1,1,1,0,1,0,1,1,0,0,0,1,1,
125 { MOD, { 1,0,1,0,0,1,1,0,1,0,1,1,0,0,0,1,1,
127 { PLUS, { 1,0,0,1,0,1,1,0,1,0,0,0,0,0,0,1,0,
129 { MINUS, { 1,0,0,1,0,1,1,0,1,0,0,0,0,0,0,1,0,
131 { SHL, { 1,0,1,0,0,1,1,0,0,0,0,0,1,0,0,1,1,
133 { SHR, { 1,0,1,0,0,1,1,0,0,0,1,0,1,0,0,1,1,
135 { LT, { 1,1,0,1,0,1,1,0,1,0,1,1,0,1,1,0,1,
137 { LE, { 1,1,0,1,0,1,1,0,1,0,1,1,0,1,1,0,1,
139 { GT, { 1,1,0,1,0,1,1,0,1,0,1,1,0,1,1,0,1,
141 { GE, { 1,1,0,1,0,1,1,0,1,0,1,1,0,1,1,0,1,
143 { EQ, { 1,1,0,1,0,1,1,0,1,0,0,0,0,1,1,0,1,
145 { NE, { 1,1,0,1,0,1,1,0,1,0,0,0,0,1,1,0,1,
147 { AND, { 1,0,1,0,0,1,1,0,1,0,0,0,1,0,0,1,0,
149 { XOR, { 1,0,1,0,0,1,1,0,1,0,0,0,1,0,0,1,0,
151 { OR, { 1,0,1,0,0,1,1,0,1,0,0,0,1,0,0,1,0,
153 { LOGAND, { 1,1,0,1,0,1,0,1,0,0,0,0,0,0,0,1,0,
155 { LOGOR, { 1,1,0,1,0,1,0,1,0,0,0,0,1,0,0,1,0,
157 { QUEST, { 1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,
159 { COLON, { 1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,
161 { ASSIGN, { 1,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,
163 { MULASS, { 1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,1,0,
165 { DIVASS, { 1,0,0,0,1,0,0,0,0,1,0,1,0,0,0,1,0,
167 { MODASS, { 1,0,1,0,0,0,0,0,0,1,0,1,0,0,0,1,0,
169 { ADDASS, { 1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,
171 { SUBASS, { 1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,
173 { SHLASS, { 1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,
175 { SHRASS, { 1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,
177 { ANDASS, { 1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,
179 { XORASS, { 1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,
181 { ORASS, { 1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,
183 { NAME, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
185 { CON, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
187 { STRING, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
189 { FSEL, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
191 { CALL, { 1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
193 { COMMA, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
195 { CVT, { 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,
197 { ICALL, { 1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
199 { LOAD, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
201 { PUSH, { 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,
203 { RETURN, { 1,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,
205 { INIT, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,
207 { FARG, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,
213 for (i = 0; imods[i].op != NOOP; i++)
214 STRUCT_ASSIGN(modtab[imods[i].op], imods[i].m);
218 * Increase degree of reference.
219 * This is most often used to change type "T" in type "pointer to T".
222 incref(type_t *tp, tspec_t t)
226 tp2 = getblk(sizeof (type_t));
233 * same for use in expressions
236 tincref(type_t *tp, tspec_t t)
240 tp2 = tgetblk(sizeof (type_t));
247 * Create a node for a constant.
250 getcnode(type_t *tp, val_t *v)
257 n->tn_val = tgetblk(sizeof (val_t));
258 n->tn_val->v_tspec = tp->t_tspec;
259 n->tn_val->v_ansiu = v->v_ansiu;
260 n->tn_val->v_u = v->v_u;
266 * Create a node for an integer constant.
269 getinode(tspec_t t, int64_t q)
275 n->tn_type = gettyp(t);
276 n->tn_val = tgetblk(sizeof (val_t));
277 n->tn_val->v_tspec = t;
278 n->tn_val->v_quad = q;
283 * Create a node for a name (symbol table entry).
284 * ntok is the token which follows the name.
287 getnnode(sym_t *sym, int ntok)
291 if (sym->s_scl == NOSCL) {
294 if (ntok == T_LPARN) {
296 /* function implicitly declared to ... */
300 * XXX if tflag is set the symbol should be
301 * exported to level 0
303 sym->s_type = incref(sym->s_type, FUNC);
307 error(99, sym->s_name);
310 if (strcmp(sym->s_name, "__FUNCTION__") == 0) {
313 } else if (strcmp(sym->s_name, "__func__") == 0) {
318 error(99, sym->s_name);
322 sym->s_type = incref(gettyp(CHAR), PTR);
323 sym->s_type->t_const = 1;
329 if (sym->s_kind != FVFT && sym->s_kind != FMOS)
330 LERROR("getnnode()");
333 n->tn_type = sym->s_type;
334 if (sym->s_scl != ENUMCON) {
337 if (sym->s_kind == FVFT && sym->s_type->t_tspec != FUNC)
341 n->tn_val = tgetblk(sizeof (val_t));
342 *n->tn_val = sym->s_value;
349 * Create a node for a string.
352 getsnode(strg_t *strg)
362 n->tn_type = tincref(gettyp(strg->st_tspec), ARRAY);
363 n->tn_type->t_dim = len + 1;
366 n->tn_strg = tgetblk(sizeof (strg_t));
367 n->tn_strg->st_tspec = strg->st_tspec;
368 n->tn_strg->st_len = len;
370 if (strg->st_tspec == CHAR) {
371 n->tn_strg->st_cp = tgetblk(len + 1);
372 (void)memcpy(n->tn_strg->st_cp, strg->st_cp, len + 1);
375 n->tn_strg->st_wcp = tgetblk((len + 1) * sizeof (wchar_t));
376 (void)memcpy(n->tn_strg->st_wcp, strg->st_wcp,
377 (len + 1) * sizeof (wchar_t));
386 * Returns a symbol which has the same name as the msym argument and is a
387 * member of the struct or union specified by the tn argument.
390 strmemb(tnode_t *tn, op_t op, sym_t *msym)
399 * Remove the member if it was unknown until now (Which means
400 * that no defined struct or union has a member with the same name).
402 if (msym->s_scl == NOSCL) {
403 /* undefined struct/union member: %s */
404 fprintf(stderr, "3. %s\n", msym->s_name);
405 error(101, msym->s_name);
409 msym->s_styp = tgetblk(sizeof (str_t));
410 msym->s_styp->stag = tgetblk(sizeof (sym_t));
411 msym->s_styp->stag->s_name = unnamed;
412 msym->s_value.v_tspec = INT;
416 /* Set str to the tag of which msym is expected to be a member. */
418 t = (tp = tn->tn_type)->t_tspec;
420 if (t == STRUCT || t == UNION)
422 } else if (op == ARROW && t == PTR) {
423 t = (tp = tp->t_subt)->t_tspec;
424 if (t == STRUCT || t == UNION)
429 * If this struct/union has a member with the name of msym, return
433 for (sym = msym; sym != NULL; sym = sym->s_link) {
434 if (sym->s_scl != MOS && sym->s_scl != MOU)
436 if (sym->s_styp != str)
438 if (strcmp(sym->s_name, msym->s_name) != 0)
445 * Set eq to 0 if there are struct/union members with the same name
446 * and different types and/or offsets.
449 for (csym = msym; csym != NULL; csym = csym->s_link) {
450 if (csym->s_scl != MOS && csym->s_scl != MOU)
452 if (strcmp(msym->s_name, csym->s_name) != 0)
454 for (sym = csym->s_link ; sym != NULL; sym = sym->s_link) {
457 if (sym->s_scl != MOS && sym->s_scl != MOU)
459 if (strcmp(csym->s_name, sym->s_name) != 0)
461 if (csym->s_value.v_quad != sym->s_value.v_quad) {
466 eq = eqtype(csym->s_type, sym->s_type, 0, 0, &w) && !w;
469 if (csym->s_field != sym->s_field) {
478 if (tp1->t_flen != tp2->t_flen) {
482 if (tp1->t_foffs != tp2->t_foffs) {
493 * Now handle the case in which the left operand refers really
494 * to a struct/union, but the right operand is not member of it.
497 /* illegal member use: %s */
499 warning(102, msym->s_name);
501 error(102, msym->s_name);
507 * Now the left operand of ARROW does not point to a struct/union
508 * or the left operand of POINT is no struct/union.
512 /* left operand of "." must be struct/union object */
519 /* left operand of "->" must be pointer to ... */
520 if (tflag && tn->tn_type->t_tspec == PTR) {
528 /* non-unique member requires struct/union %s */
529 error(105, op == POINT ? "object" : "pointer");
531 /* unacceptable operand of %s */
532 error(111, modtab[op].m_name);
540 * Create a tree node. Called for most operands except function calls,
545 * rn if not NULL, right operand
548 build(op_t op, tnode_t *ln, tnode_t *rn)
556 /* If there was an error in one of the operands, return. */
557 if (ln == NULL || (mp->m_binary && rn == NULL))
561 * Apply class conversions to the left operand, but only if its
562 * value is needed or it is compared with null.
564 if (mp->m_vctx || mp->m_tctx)
567 * The right operand is almost always in a test or value context,
568 * except if it is a struct or union member.
570 if (mp->m_binary && op != ARROW && op != POINT)
574 * Print some warnings for comparisons of unsigned values with
575 * constants lower than or equal to null. This must be done
576 * before promote() because otherwise unsigned char and unsigned
577 * short would be promoted to int. Also types are tested to be
578 * CHAR, which would also become int.
584 * Promote the left operand if it is in a test or value context
586 if (mp->m_vctx || mp->m_tctx)
587 ln = promote(op, 0, ln);
589 * Promote the right operand, but only if it is no struct or
590 * union member, or if it is not to be assigned to the left operand
592 if (mp->m_binary && op != ARROW && op != POINT &&
593 op != ASSIGN && op != RETURN) {
594 rn = promote(op, 0, rn);
598 * If the result of the operation is different for signed or
599 * unsigned operands and one of the operands is signed only in
600 * ANSI C, print a warning.
602 if (mp->m_tlansiu && ln->tn_op == CON && ln->tn_val->v_ansiu) {
603 /* ANSI C treats constant as unsigned, op %s */
604 warning(218, mp->m_name);
605 ln->tn_val->v_ansiu = 0;
607 if (mp->m_transiu && rn->tn_op == CON && rn->tn_val->v_ansiu) {
608 /* ANSI C treats constant as unsigned, op %s */
609 warning(218, mp->m_name);
610 rn->tn_val->v_ansiu = 0;
613 /* Make sure both operands are of the same type */
614 if (mp->m_balance || (tflag && (op == SHL || op == SHR)))
615 balance(op, &ln, &rn);
618 * Check types for compatibility with the operation and mutual
619 * compatibility. Return if there are serious problems.
621 if (!typeok(op, 0, ln, rn))
624 /* And now create the node. */
628 ntn = bldstr(op, ln, rn);
634 ntn = bldincdec(op, ln);
637 ntn = bldamper(ln, 0);
640 ntn = mktnode(STAR, ln->tn_type->t_subt, ln, NULL);
644 ntn = bldplmi(op, ln, rn);
648 ntn = bldshft(op, ln, rn);
651 ntn = bldcol(ln, rn);
665 ntn = bldasgn(op, ln, rn);
669 ntn = mktnode(op, rn->tn_type, ln, rn);
672 rtp = mp->m_logop ? gettyp(INT) : ln->tn_type;
673 if (!mp->m_binary && rn != NULL)
675 ntn = mktnode(op, rtp, ln, rn);
679 /* Return if an error occurred. */
683 /* Print a warning if precedence confusion is possible */
688 * Print a warning if one of the operands is in a context where
689 * it is compared with null and if this operand is a constant.
692 if (ln->tn_op == CON ||
693 ((mp->m_binary && op != QUEST) && rn->tn_op == CON)) {
695 /* constant in conditional context */
700 /* Fold if the operator requires it */
702 if (ln->tn_op == CON && (!mp->m_binary || rn->tn_op == CON)) {
705 } else if (isftyp(ntn->tn_type->t_tspec)) {
710 } else if (op == QUEST && ln->tn_op == CON) {
711 ntn = ln->tn_val->v_quad ? rn->tn_left : rn->tn_right;
719 * Perform class conversions.
721 * Arrays of type T are converted into pointers to type T.
722 * Functions are converted to pointers to functions.
723 * Lvalues are converted to rvalues.
731 * Array-lvalue (array of type T) is converted into rvalue
732 * (pointer to type T)
734 if (tn->tn_type->t_tspec == ARRAY) {
735 if (!tn->tn_lvalue) {
736 /* operand of '%s' must be lvalue */
737 /* XXX print correct operator */
738 (void)gnuism(114, "", modtab[AMPER].m_name);
740 tn = mktnode(AMPER, tincref(tn->tn_type->t_subt, PTR),
745 * Expression of type function (function with return value of type T)
746 * in rvalue-expression (pointer to function with return value
749 if (tn->tn_type->t_tspec == FUNC)
750 tn = bldamper(tn, 1);
752 /* lvalue to rvalue */
754 tp = tduptyp(tn->tn_type);
755 tp->t_const = tp->t_volatile = 0;
756 tn = mktnode(LOAD, tp, tn, NULL);
763 * Perform most type checks. First the types are checked using
764 * information from modtab[]. After that it is done by hand for
765 * more complicated operators and type combinations.
767 * If the types are ok, typeok() returns 1, otherwise 0.
770 typeok(op_t op, int arg, tnode_t *ln, tnode_t *rn)
773 tspec_t lt, rt = NOTSPEC, lst = NOTSPEC, rst = NOTSPEC, olt = NOTSPEC,
775 type_t *ltp, *rtp = NULL, *lstp = NULL, *rstp = NULL;
780 if ((ltp = ln->tn_type) == NULL)
783 if ((lt = ltp->t_tspec) == PTR)
784 lst = (lstp = ltp->t_subt)->t_tspec;
786 if ((rtp = rn->tn_type) == NULL)
788 if ((rt = rtp->t_tspec) == PTR)
789 rst = (rstp = rtp->t_subt)->t_tspec;
793 /* integer types required */
794 if (!isityp(lt) || (mp->m_binary && !isityp(rt))) {
795 incompat(op, lt, rt);
798 } else if (mp->m_rqsclt) {
799 /* scalar types required */
800 if (!issclt(lt) || (mp->m_binary && !issclt(rt))) {
801 incompat(op, lt, rt);
804 } else if (mp->m_rqatyp) {
805 /* arithmetic types required */
806 if (!isatyp(lt) || (mp->m_binary && !isatyp(rt))) {
807 incompat(op, lt, rt);
812 if (op == SHL || op == SHR || op == SHLASS || op == SHRASS) {
814 * For these operations we need the types before promotion
817 for (tn=ln; tn->tn_op==CVT && !tn->tn_cast; tn=tn->tn_left)
819 olt = tn->tn_type->t_tspec;
820 for (tn=rn; tn->tn_op==CVT && !tn->tn_cast; tn=tn->tn_left)
822 ort = tn->tn_type->t_tspec;
828 * Most errors required by ANSI C are reported in strmemb().
829 * Here we only must check for totally wrong things.
831 if (lt == FUNC || lt == VOID || ltp->t_isfield ||
832 ((lt != STRUCT && lt != UNION) && !ln->tn_lvalue)) {
833 /* Without tflag we already got an error */
835 /* unacceptable operand of %s */
836 error(111, mp->m_name);
839 /* Now we have an object we can create a pointer to */
842 if (lt != PTR && !(tflag && isityp(lt))) {
843 /* Without tflag we got already an error */
845 /* unacceptable operand of %s */
846 error(111, mp->m_name);
854 /* operands have scalar types (checked above) */
855 if (!ln->tn_lvalue) {
856 if (ln->tn_op == CVT && ln->tn_cast &&
857 ln->tn_left->tn_op == LOAD) {
858 /* a cast does not yield an lvalue */
861 /* operand of %s must be lvalue */
862 error(114, "", mp->m_name);
864 } else if (ltp->t_const) {
865 /* operand of %s must be modifiable lvalue */
867 warning(115, "", mp->m_name);
871 if (lt == ARRAY || lt == FUNC) {
872 /* ok, a warning comes later (in bldamper()) */
873 } else if (!ln->tn_lvalue) {
874 if (ln->tn_op == CVT && ln->tn_cast &&
875 ln->tn_left->tn_op == LOAD) {
876 /* a cast does not yield an lvalue */
879 /* %soperand of %s must be lvalue */
880 error(114, "", mp->m_name);
882 } else if (issclt(lt)) {
883 if (ltp->t_isfield) {
884 /* cannot take address of bit-field */
888 } else if (lt != STRUCT && lt != UNION) {
889 /* unacceptable operand of %s */
890 error(111, mp->m_name);
893 if (ln->tn_op == NAME && ln->tn_sym->s_reg) {
894 /* cannot take address of register %s */
895 error(113, ln->tn_sym->s_name);
900 /* until now there were no type checks for this operator */
902 /* cannot dereference non-pointer type */
908 /* operands have scalar types (checked above) */
909 if ((lt == PTR && !isityp(rt)) || (rt == PTR && !isityp(lt))) {
910 incompat(op, lt, rt);
915 /* operands have scalar types (checked above) */
916 if (lt == PTR && (!isityp(rt) && rt != PTR)) {
917 incompat(op, lt, rt);
919 } else if (rt == PTR && lt != PTR) {
920 incompat(op, lt, rt);
923 if (lt == PTR && rt == PTR) {
924 if (!eqtype(lstp, rstp, 1, 0, NULL)) {
925 /* illegal pointer subtraction */
931 /* operands have integer types (checked above) */
932 if (pflag && !isutyp(lt)) {
934 * The left operand is signed. This means that
935 * the operation is (possibly) nonportable.
937 /* bitwise operation on signed value nonportable */
938 if (ln->tn_op != CON) {
939 /* possibly nonportable */
941 } else if (ln->tn_val->v_quad < 0) {
944 } else if (!tflag && !sflag && !isutyp(olt) && isutyp(ort)) {
946 * The left operand would become unsigned in
950 (ln->tn_op != CON || ln->tn_val->v_quad < 0)) {
951 /* semantics of %s change in ANSI C; use ... */
952 warning(118, mp->m_name);
954 } else if (!tflag && !sflag && !isutyp(olt) && !isutyp(ort) &&
955 psize(lt) < psize(rt)) {
957 * In traditional C the left operand would be extended,
958 * possibly with 1, and then shifted.
961 (ln->tn_op != CON || ln->tn_val->v_quad < 0)) {
962 /* semantics of %s change in ANSI C; use ... */
963 warning(118, mp->m_name);
969 * ANSI C does not perform balancing for shift operations,
970 * but traditional C does. If the width of the right operand
971 * is greather than the width of the left operand, than in
972 * traditional C the left operand would be extendet to the
973 * width of the right operand. For SHL this may result in
976 if (psize(lt) < psize(rt)) {
978 * XXX If both operands are constant make sure
979 * that there is really a differencs between
980 * ANSI C and traditional C.
983 /* semantics of %s change in ANSI C; use ... */
984 warning(118, mp->m_name);
987 if (rn->tn_op == CON) {
988 if (!isutyp(rt) && rn->tn_val->v_quad < 0) {
991 } else if ((uint64_t)rn->tn_val->v_quad == size(lt)) {
992 /* shift equal to size fo object */
994 } else if ((uint64_t)rn->tn_val->v_quad > size(lt)) {
995 /* shift greater than size of object */
1003 * Accept some things which are allowed with EQ and NE,
1004 * but not with ordered comparisons.
1006 if (lt == PTR && ((rt == PTR && rst == VOID) || isityp(rt))) {
1007 if (rn->tn_op == CON && rn->tn_val->v_quad == 0)
1010 if (rt == PTR && ((lt == PTR && lst == VOID) || isityp(lt))) {
1011 if (ln->tn_op == CON && ln->tn_val->v_quad == 0)
1019 if ((lt == PTR || rt == PTR) && lt != rt) {
1020 if (isityp(lt) || isityp(rt)) {
1021 /* illegal comb. of pointer and int., op %s */
1022 warning(123, mp->m_name);
1024 incompat(op, lt, rt);
1027 } else if (lt == PTR && rt == PTR) {
1028 ptrcmpok(op, ln, rn);
1033 /* first operand must have scalar type, op ? : */
1037 while (rn->tn_op == CVT)
1039 if (rn->tn_op != COLON)
1044 if (isatyp(lt) && isatyp(rt))
1047 if (lt == STRUCT && rt == STRUCT && ltp->t_str == rtp->t_str)
1049 if (lt == UNION && rt == UNION && ltp->t_str == rtp->t_str)
1052 /* combination of any pointer and 0, 0L or (void *)0 is ok */
1053 if (lt == PTR && ((rt == PTR && rst == VOID) || isityp(rt))) {
1054 if (rn->tn_op == CON && rn->tn_val->v_quad == 0)
1057 if (rt == PTR && ((lt == PTR && lst == VOID) || isityp(lt))) {
1058 if (ln->tn_op == CON && ln->tn_val->v_quad == 0)
1062 if ((lt == PTR && isityp(rt)) || (isityp(lt) && rt == PTR)) {
1063 /* illegal comb. of ptr. and int., op %s */
1064 warning(123, mp->m_name);
1068 if (lt == VOID || rt == VOID) {
1069 if (lt != VOID || rt != VOID)
1070 /* incompatible types in conditional */
1075 if (lt == PTR && rt == PTR && ((lst == VOID && rst == FUNC) ||
1076 (lst == FUNC && rst == VOID))) {
1077 /* (void *)0 handled above */
1079 /* ANSI C forbids conv. of %s to %s, op %s */
1080 warning(305, "function pointer", "'void *'",
1085 if (rt == PTR && lt == PTR) {
1086 if (!eqtype(lstp, rstp, 1, 0, NULL))
1087 illptrc(mp, ltp, rtp);
1091 /* incompatible types in conditional */
1099 if (!asgntypok(op, arg, ln, rn))
1108 /* operands have scalar types (checked above) */
1109 if ((lt == PTR && !isityp(rt)) || rt == PTR) {
1110 incompat(op, lt, rt);
1117 if (pflag && !isutyp(lt) && !(tflag && isutyp(rt))) {
1118 /* bitwise operation on s.v. possibly nonportabel */
1127 if (!ln->tn_lvalue) {
1128 if (ln->tn_op == CVT && ln->tn_cast &&
1129 ln->tn_left->tn_op == LOAD) {
1130 /* a cast does not yield an lvalue */
1133 /* %soperand of %s must be lvalue */
1134 error(114, "left ", mp->m_name);
1136 } else if (ltp->t_const || ((lt == STRUCT || lt == UNION) &&
1138 /* %soperand of %s must be modifiable lvalue */
1140 warning(115, "left ", mp->m_name);
1144 if (!modtab[ln->tn_op].m_sideeff)
1147 /* LINTED (enumeration values not handled in switch) */
1177 (ltp->t_isenum || (mp->m_binary && rtp->t_isenum))) {
1178 chkbeop(op, ln, rn);
1179 } else if (mp->m_enumop && (ltp->t_isenum && rtp && rtp->t_isenum)) {
1180 chkeop2(op, arg, ln, rn);
1181 } else if (mp->m_enumop && (ltp->t_isenum || (rtp &&rtp->t_isenum))) {
1182 chkeop1(op, arg, ln, rn);
1189 ptrcmpok(op_t op, tnode_t *ln, tnode_t *rn)
1193 const char *lts, *rts;
1195 lt = (ltp = ln->tn_type)->t_subt->t_tspec;
1196 rt = (rtp = rn->tn_type)->t_subt->t_tspec;
1198 if (lt == VOID || rt == VOID) {
1199 if (sflag && (lt == FUNC || rt == FUNC)) {
1200 /* (void *)0 already handled in typeok() */
1201 *(lt == FUNC ? <s : &rts) = "function pointer";
1202 *(lt == VOID ? <s : &rts) = "'void *'";
1203 /* ANSI C forbids comparison of %s with %s */
1204 warning(274, lts, rts);
1209 if (!eqtype(ltp->t_subt, rtp->t_subt, 1, 0, NULL)) {
1210 illptrc(&modtab[op], ltp, rtp);
1214 if (lt == FUNC && rt == FUNC) {
1215 if (sflag && op != EQ && op != NE)
1216 /* ANSI C forbids ordered comp. of func ptr */
1222 * Checks type compatibility for ASSIGN, INIT, FARG and RETURN
1223 * and prints warnings/errors if necessary.
1224 * If the types are (almost) compatible, 1 is returned, otherwise 0.
1227 asgntypok(op_t op, int arg, tnode_t *ln, tnode_t *rn)
1229 tspec_t lt, rt, lst = NOTSPEC, rst = NOTSPEC;
1230 type_t *ltp, *rtp, *lstp = NULL, *rstp = NULL;
1232 const char *lts, *rts;
1234 if ((lt = (ltp = ln->tn_type)->t_tspec) == PTR)
1235 lst = (lstp = ltp->t_subt)->t_tspec;
1236 if ((rt = (rtp = rn->tn_type)->t_tspec) == PTR)
1237 rst = (rstp = rtp->t_subt)->t_tspec;
1240 if (isatyp(lt) && isatyp(rt))
1243 if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION))
1244 /* both are struct or union */
1245 return (ltp->t_str == rtp->t_str);
1247 /* 0, 0L and (void *)0 may be assigned to any pointer */
1248 if (lt == PTR && ((rt == PTR && rst == VOID) || isityp(rt))) {
1249 if (rn->tn_op == CON && rn->tn_val->v_quad == 0)
1253 if (lt == PTR && rt == PTR && (lst == VOID || rst == VOID)) {
1254 /* two pointers, at least one pointer to void */
1255 if (sflag && (lst == FUNC || rst == FUNC)) {
1256 /* comb. of ptr to func and ptr to void */
1257 *(lst == FUNC ? <s : &rts) = "function pointer";
1258 *(lst == VOID ? <s : &rts) = "'void *'";
1262 /* ANSI C forbids conversion of %s to %s */
1263 warning(303, rts, lts);
1266 /* ANSI C forbids conv. of %s to %s, arg #%d */
1267 warning(304, rts, lts, arg);
1270 /* ANSI C forbids conv. of %s to %s, op %s */
1271 warning(305, rts, lts, mp->m_name);
1277 if (lt == PTR && rt == PTR && (lst == VOID || rst == VOID ||
1278 eqtype(lstp, rstp, 1, 0, NULL))) {
1279 /* compatible pointer types (qualifiers ignored) */
1281 ((!lstp->t_const && rstp->t_const) ||
1282 (!lstp->t_volatile && rstp->t_volatile))) {
1283 /* left side has not all qualifiers of right */
1287 /* incompatible pointer types */
1291 /* argument has incompat. ptr. type, arg #%d */
1295 /* operands have incompat. ptr. types, op %s */
1296 warning(128, mp->m_name);
1303 if ((lt == PTR && isityp(rt)) || (isityp(lt) && rt == PTR)) {
1307 /* illegal combination of pointer and integer */
1311 /* illegal comb. of ptr. and int., arg #%d */
1315 /* illegal comb. of ptr. and int., op %s */
1316 warning(123, mp->m_name);
1322 if (lt == PTR && rt == PTR) {
1326 illptrc(NULL, ltp, rtp);
1329 /* argument has incompatible pointer type, arg #%d */
1333 illptrc(mp, ltp, rtp);
1341 /* initialisation type mismatch */
1345 /* return value type mismatch */
1349 /* argument is incompatible with prototype, arg #%d */
1353 incompat(op, lt, rt);
1361 * Prints a warning if an operator, which should be senseless for an
1362 * enum type, is applied to an enum type.
1365 chkbeop(op_t op, tnode_t *ln, tnode_t *rn)
1374 if (!(ln->tn_type->t_isenum ||
1375 (mp->m_binary && rn->tn_type->t_isenum))) {
1380 * Enum as offset to a pointer is an exception (otherwise enums
1381 * could not be used as array indizes).
1384 ((ln->tn_type->t_isenum && rn->tn_type->t_tspec == PTR) ||
1385 (rn->tn_type->t_isenum && ln->tn_type->t_tspec == PTR))) {
1389 /* dubious operation on enum, op %s */
1390 warning(241, mp->m_name);
1395 * Prints a warning if an operator is applied to two different enum types.
1398 chkeop2(op_t op, int arg, tnode_t *ln, tnode_t *rn)
1404 if (ln->tn_type->t_enum != rn->tn_type->t_enum) {
1407 /* enum type mismatch in initialisation */
1411 /* enum type mismatch, arg #%d */
1415 /* return value type mismatch */
1419 /* enum type mismatch, op %s */
1420 warning(130, mp->m_name);
1424 } else if (mp->m_comp && op != EQ && op != NE) {
1426 /* dubious comparisons of enums */
1427 warning(243, mp->m_name);
1433 * Prints a warning if an operator has both enum end other integer
1437 chkeop1(op_t op, int arg, tnode_t *ln, tnode_t *rn)
1439 char lbuf[64], rbuf[64];
1447 * Initializations with 0 should be allowed. Otherwise,
1448 * we should complain about all uninitialized enums,
1451 if (!rn->tn_type->t_isenum && rn->tn_op == CON &&
1452 isityp(rn->tn_type->t_tspec) && rn->tn_val->v_quad == 0) {
1455 /* initialisation of '%s' with '%s' */
1456 warning(277, tyname(lbuf, sizeof(lbuf), ln->tn_type),
1457 tyname(rbuf, sizeof(rbuf), rn->tn_type));
1460 /* combination of '%s' and '%s', arg #%d */
1461 warning(278, tyname(lbuf, sizeof(lbuf), ln->tn_type),
1462 tyname(rbuf, sizeof(rbuf), rn->tn_type), arg);
1465 /* combination of '%s' and '%s' in return */
1466 warning(279, tyname(lbuf, sizeof(lbuf), ln->tn_type),
1467 tyname(rbuf, sizeof(rbuf), rn->tn_type));
1470 /* combination of '%s' and %s, op %s */
1471 warning(242, tyname(lbuf, sizeof(lbuf), ln->tn_type),
1472 tyname(rbuf, sizeof(rbuf), rn->tn_type),
1479 * Build and initialize a new node.
1482 mktnode(op_t op, type_t *type, tnode_t *ln, tnode_t *rn)
1490 ntn->tn_type = type;
1494 if (op == STAR || op == FSEL) {
1495 if (ln->tn_type->t_tspec == PTR) {
1496 t = ln->tn_type->t_subt->t_tspec;
1497 if (t != FUNC && t != VOID)
1500 LERROR("mktnode()");
1508 * Performs usual conversion of operands to (unsigned) int.
1510 * If tflag is set or the operand is a function argument with no
1511 * type information (no prototype or variable # of args), convert
1515 promote(op_t op, int farg, tnode_t *tn)
1521 t = tn->tn_type->t_tspec;
1528 * ANSI C requires that the result is always of type INT
1529 * if INT can represent all possible values of the previous
1532 if (tn->tn_type->t_isfield) {
1533 len = tn->tn_type->t_flen;
1534 if (size(INT) > len) {
1537 if (size(INT) != len)
1538 LERROR("promote()");
1545 } else if (t == CHAR || t == UCHAR || t == SCHAR) {
1546 t = (size(CHAR) < size(INT) || t != UCHAR) ?
1548 } else if (t == SHORT || t == USHORT) {
1549 t = (size(SHORT) < size(INT) || t == SHORT) ?
1551 } else if (t == ENUM) {
1553 } else if (farg && t == FLOAT) {
1558 * In traditional C, keep unsigned and promote FLOAT
1561 if (t == UCHAR || t == USHORT) {
1563 } else if (t == CHAR || t == SCHAR || t == SHORT) {
1565 } else if (t == FLOAT) {
1567 } else if (t == ENUM) {
1572 if (t != tn->tn_type->t_tspec) {
1573 ntp = tduptyp(tn->tn_type);
1576 * Keep t_isenum so we are later able to check compatibility
1579 tn = convert(op, 0, ntp, tn);
1586 * Insert conversions which are necessary to give both operands the same
1587 * type. This is done in different ways for traditional C and ANSI C.
1590 balance(op_t op, tnode_t **lnp, tnode_t **rnp)
1595 static tspec_t tl[] = {
1596 LDOUBLE, DOUBLE, FLOAT, UQUAD, QUAD, ULONG, LONG, UINT, INT,
1599 lt = (*lnp)->tn_type->t_tspec;
1600 rt = (*rnp)->tn_type->t_tspec;
1602 if (!isatyp(lt) || !isatyp(rt))
1608 } else if (lt == LDOUBLE || rt == LDOUBLE) {
1610 } else if (lt == DOUBLE || rt == DOUBLE) {
1612 } else if (lt == FLOAT || rt == FLOAT) {
1616 * If type A has more bits than type B it should
1617 * be able to hold all possible values of type B.
1619 if (size(lt) > size(rt)) {
1621 } else if (size(lt) < size(rt)) {
1624 for (i = 3; tl[i] != INT; i++) {
1625 if (tl[i] == lt || tl[i] == rt)
1628 if ((isutyp(lt) || isutyp(rt)) &&
1636 /* Keep unsigned in traditional C */
1637 u = isutyp(lt) || isutyp(rt);
1638 for (i = 0; tl[i] != INT; i++) {
1639 if (lt == tl[i] || rt == tl[i])
1643 if (u && isityp(t) && !isutyp(t))
1648 ntp = tduptyp((*lnp)->tn_type);
1650 *lnp = convert(op, 0, ntp, *lnp);
1653 ntp = tduptyp((*rnp)->tn_type);
1655 *rnp = convert(op, 0, ntp, *rnp);
1660 * Insert a conversion operator, which converts the type of the node
1661 * to another given type.
1662 * If op is FARG, arg is the number of the argument (used for warnings).
1665 convert(op_t op, int arg, type_t *tp, tnode_t *tn)
1668 tspec_t nt, ot, ost = NOTSPEC;
1671 LERROR("convert()");
1674 if ((ot = tn->tn_type->t_tspec) == PTR)
1675 ost = tn->tn_type->t_subt->t_tspec;
1677 if (!tflag && !sflag && op == FARG)
1678 ptconv(arg, nt, ot, tp, tn);
1679 if (isityp(nt) && isityp(ot)) {
1680 iiconv(op, arg, nt, ot, tp, tn);
1681 } else if (nt == PTR && ((ot == PTR && ost == VOID) || isityp(ot)) &&
1682 tn->tn_op == CON && tn->tn_val->v_quad == 0) {
1683 /* 0, 0L and (void *)0 may be assigned to any pointer. */
1684 } else if (isityp(nt) && ot == PTR) {
1685 piconv(op, nt, tp, tn);
1686 } else if (nt == PTR && ot == PTR) {
1693 ntn->tn_cast = op == CVT;
1694 if (tn->tn_op != CON || nt == VOID) {
1698 ntn->tn_val = tgetblk(sizeof (val_t));
1699 cvtcon(op, arg, ntn->tn_type, ntn->tn_val, tn->tn_val);
1706 * Print a warning if a prototype causes a type conversion that is
1707 * different from what would happen to the same argument in the
1708 * absence of a prototype.
1710 * Errors/Warnings about illegal type combinations are already printed
1714 ptconv(int arg, tspec_t nt, tspec_t ot, type_t *tp, tnode_t *tn)
1719 if (!isatyp(nt) || !isatyp(ot))
1723 * If the type of the formal parameter is char/short, a warning
1724 * would be useless, because functions declared the old style
1725 * can't expect char/short arguments.
1727 if (nt == CHAR || nt == UCHAR || nt == SHORT || nt == USHORT)
1730 /* get default promotion */
1731 ptn = promote(NOOP, 1, tn);
1732 ot = ptn->tn_type->t_tspec;
1734 /* return if types are the same with and without prototype */
1735 if (nt == ot || (nt == ENUM && ot == INT))
1738 if (isftyp(nt) != isftyp(ot) || psize(nt) != psize(ot)) {
1739 /* representation and/or width change */
1740 if (!isityp(ot) || psize(ot) > psize(INT)) {
1741 /* conversion to '%s' due to prototype, arg #%d */
1742 warning(259, tyname(buf, sizeof(buf), tp), arg);
1746 * they differ in sign or base type (char, short, int,
1747 * long, long long, float, double, long double)
1749 * if they differ only in sign and the argument is a constant
1750 * and the msb of the argument is not set, print no warning
1752 if (ptn->tn_op == CON && isityp(nt) && styp(nt) == styp(ot) &&
1753 msb(ptn->tn_val->v_quad, ot, -1) == 0) {
1756 /* conversion to '%s' due to prototype, arg #%d */
1757 warning(259, tyname(buf, sizeof(buf), tp), arg);
1763 * Print warnings for conversions of integer types which may cause
1768 iiconv(op_t op, int arg, tspec_t nt, tspec_t ot, type_t *tp, tnode_t *tn)
1770 char lbuf[64], rbuf[64];
1771 if (tn->tn_op == CON)
1778 if (psize(nt) > psize(ot) && isutyp(nt) != isutyp(ot)) {
1779 /* conversion to %s may sign-extend incorrectly (, arg #%d) */
1780 if (aflag && pflag) {
1782 warning(297, tyname(lbuf, sizeof(lbuf), tp),
1785 warning(131, tyname(lbuf, sizeof(lbuf), tp));
1791 if (psize(nt) < psize(ot) &&
1792 (ot == LONG || ot == ULONG || ot == QUAD || ot == UQUAD ||
1794 /* conversion from '%s' may lose accuracy */
1798 tyname(rbuf, sizeof(rbuf), tn->tn_type),
1799 tyname(lbuf, sizeof(lbuf), tp),
1803 tyname(rbuf, sizeof(rbuf), tn->tn_type),
1804 tyname(lbuf, sizeof(lbuf), tp));
1811 * Print warnings for dubious conversions of pointer to integer.
1814 piconv(op_t op, tspec_t nt, type_t *tp, tnode_t *tn)
1818 if (tn->tn_op == CON)
1822 /* We already got an error. */
1826 if (psize(nt) < psize(PTR)) {
1827 if (pflag && size(nt) >= size(PTR)) {
1828 /* conv. of pointer to %s may lose bits */
1829 warning(134, tyname(buf, sizeof(buf), tp));
1831 /* conv. of pointer to %s loses bits */
1832 warning(133, tyname(buf, sizeof(buf), tp));
1838 * Print warnings for questionable pointer conversions.
1841 ppconv(op_t op, tnode_t *tn, type_t *tp)
1844 const char *nts, *ots;
1847 * We already got an error (pointers of different types
1848 * without a cast) or we will not get a warning.
1853 nt = tp->t_subt->t_tspec;
1854 ot = tn->tn_type->t_subt->t_tspec;
1856 if (nt == VOID || ot == VOID) {
1857 if (sflag && (nt == FUNC || ot == FUNC)) {
1858 /* (void *)0 already handled in convert() */
1859 *(nt == FUNC ? &nts : &ots) = "function pointer";
1860 *(nt == VOID ? &nts : &ots) = "'void *'";
1861 /* ANSI C forbids conversion of %s to %s */
1862 warning(303, ots, nts);
1865 } else if (nt == FUNC && ot == FUNC) {
1867 } else if (nt == FUNC || ot == FUNC) {
1868 /* questionable conversion of function pointer */
1873 if (getbound(tp->t_subt) > getbound(tn->tn_type->t_subt)) {
1875 /* possible pointer alignment problem */
1878 if (((nt == STRUCT || nt == UNION) &&
1879 tp->t_subt->t_str != tn->tn_type->t_subt->t_str) ||
1880 psize(nt) != psize(ot)) {
1882 /* pointer casts may be troublesome */
1889 * Converts a typed constant in a constant of another type.
1891 * op operator which requires conversion
1892 * arg if op is FARG, # of argument
1893 * tp type to which convert the constant
1898 cvtcon(op_t op, int arg, type_t *tp, val_t *nv, val_t *v)
1900 char lbuf[64], rbuf[64];
1902 ldbl_t max = 0.0, min = 0.0;
1904 int64_t xmask, xmsk1;
1908 nt = nv->v_tspec = tp->t_tspec;
1911 if (ot == FLOAT || ot == DOUBLE || ot == LDOUBLE) {
1914 max = CHAR_MAX; min = CHAR_MIN; break;
1916 max = UCHAR_MAX; min = 0; break;
1918 max = SCHAR_MAX; min = SCHAR_MIN; break;
1920 max = SHRT_MAX; min = SHRT_MIN; break;
1922 max = USHRT_MAX; min = 0; break;
1925 max = INT_MAX; min = INT_MIN; break;
1927 max = (u_int)UINT_MAX; min = 0; break;
1929 max = LONG_MAX; min = LONG_MIN; break;
1931 max = (u_long)ULONG_MAX; min = 0; break;
1933 max = QUAD_MAX; min = QUAD_MIN; break;
1935 max = (uint64_t)UQUAD_MAX; min = 0; break;
1937 max = FLT_MAX; min = -FLT_MAX; break;
1939 max = DBL_MAX; min = -DBL_MAX; break;
1941 /* Already got an error because of float --> ptr */
1943 max = LDBL_MAX; min = -LDBL_MAX; break;
1947 if (v->v_ldbl > max || v->v_ldbl < min) {
1951 /* conv. of %s to %s is out of rng., arg #%d */
1952 warning(295, tyname(lbuf, sizeof(lbuf),
1953 gettyp(ot)), tyname(rbuf, sizeof(rbuf), tp),
1956 /* conversion of %s to %s is out of range */
1957 warning(119, tyname(lbuf, sizeof(lbuf),
1959 tyname(rbuf, sizeof(rbuf), tp));
1961 v->v_ldbl = v->v_ldbl > 0 ? max : min;
1964 nv->v_ldbl = (float)v->v_ldbl;
1965 } else if (nt == DOUBLE) {
1966 nv->v_ldbl = (double)v->v_ldbl;
1967 } else if (nt == LDOUBLE) {
1968 nv->v_ldbl = v->v_ldbl;
1970 nv->v_quad = (nt == PTR || isutyp(nt)) ?
1971 (uint64_t)v->v_ldbl : (int64_t)v->v_ldbl;
1975 nv->v_ldbl = (ot == PTR || isutyp(ot)) ?
1976 (float)(uint64_t)v->v_quad : (float)v->v_quad;
1977 } else if (nt == DOUBLE) {
1978 nv->v_ldbl = (ot == PTR || isutyp(ot)) ?
1979 (double)(uint64_t)v->v_quad : (double)v->v_quad;
1980 } else if (nt == LDOUBLE) {
1981 nv->v_ldbl = (ot == PTR || isutyp(ot)) ?
1982 (ldbl_t)(uint64_t)v->v_quad : (ldbl_t)v->v_quad;
1984 rchk = 1; /* Check for lost precision. */
1985 nv->v_quad = v->v_quad;
1989 if (v->v_ansiu && isftyp(nt)) {
1990 /* ANSI C treats constant as unsigned */
1993 } else if (v->v_ansiu && (isityp(nt) && !isutyp(nt) &&
1994 psize(nt) > psize(ot))) {
1995 /* ANSI C treats constant as unsigned */
2000 if (nt != FLOAT && nt != DOUBLE && nt != LDOUBLE) {
2001 sz = tp->t_isfield ? tp->t_flen : size(nt);
2002 nv->v_quad = xsign(nv->v_quad, nt, sz);
2005 if (rchk && op != CVT) {
2007 nsz = tp->t_isfield ? tp->t_flen : size(nt);
2008 xmask = qlmasks[nsz] ^ qlmasks[osz];
2009 xmsk1 = qlmasks[nsz] ^ qlmasks[osz - 1];
2011 * For bitwise operations we are not interested in the
2012 * value, but in the bits itself.
2014 if (op == ORASS || op == OR || op == XOR) {
2016 * Print a warning if bits which were set are
2017 * lost due to the conversion.
2018 * This can happen with operator ORASS only.
2020 if (nsz < osz && (v->v_quad & xmask) != 0) {
2021 /* constant truncated by conv., op %s */
2022 warning(306, modtab[op].m_name);
2024 } else if (op == ANDASS || op == AND) {
2026 * Print a warning if additional bits are not all 1
2027 * and the most significant bit of the old value is 1,
2028 * or if at least one (but not all) removed bit was 0.
2031 (nv->v_quad & qbmasks[osz - 1]) != 0 &&
2032 (nv->v_quad & xmask) != xmask) {
2034 * extra bits set to 0 in conversion
2035 * of '%s' to '%s', op %s
2037 warning(309, tyname(lbuf, sizeof(lbuf),
2038 gettyp(ot)), tyname(rbuf, sizeof(rbuf), tp),
2040 } else if (nsz < osz &&
2041 (v->v_quad & xmask) != xmask &&
2042 (v->v_quad & xmask) != 0) {
2043 /* const. truncated by conv., op %s */
2044 warning(306, modtab[op].m_name);
2046 } else if ((nt != PTR && isutyp(nt)) &&
2047 (ot != PTR && !isutyp(ot)) && v->v_quad < 0) {
2049 /* assignment of negative constant to ... */
2051 } else if (op == INIT) {
2052 /* initialisation of unsigned with neg. ... */
2054 } else if (op == FARG) {
2055 /* conversion of neg. const. to ..., arg #%d */
2057 } else if (modtab[op].m_comp) {
2058 /* we get this warning already in chkcomp() */
2060 /* conversion of negative constant to ... */
2063 } else if (nv->v_quad != v->v_quad && nsz <= osz &&
2064 (v->v_quad & xmask) != 0 &&
2065 (isutyp(ot) || (v->v_quad & xmsk1) != xmsk1)) {
2067 * Loss of significant bit(s). All truncated bits
2068 * of unsigned types or all truncated bits plus the
2069 * msb of the target for signed types are considered
2070 * to be significant bits. Loss of significant bits
2071 * means that at least on of the bits was set in an
2072 * unsigned type or that at least one, but not all of
2073 * the bits was set in a signed type.
2074 * Loss of significant bits means that it is not
2075 * possible, also not with necessary casts, to convert
2076 * back to the original type. An example for a
2077 * necessary cast is:
2078 * char c; int i; c = 128;
2079 * i = c; ** yields -128 **
2080 * i = (unsigned char)c; ** yields 128 **
2082 if (op == ASSIGN && tp->t_isfield) {
2083 /* precision lost in bit-field assignment */
2085 } else if (op == ASSIGN) {
2086 /* constant truncated by assignment */
2088 } else if (op == INIT && tp->t_isfield) {
2089 /* bit-field initializer does not fit */
2091 } else if (op == INIT) {
2092 /* initializer does not fit */
2094 } else if (op == CASE) {
2095 /* case label affected by conversion */
2097 } else if (op == FARG) {
2098 /* conv. of %s to %s is out of rng., arg #%d */
2099 warning(295, tyname(lbuf, sizeof(lbuf),
2100 gettyp(ot)), tyname(rbuf, sizeof(rbuf), tp),
2103 /* conversion of %s to %s is out of range */
2104 warning(119, tyname(lbuf, sizeof(lbuf),
2106 tyname(rbuf, sizeof(rbuf), tp));
2108 } else if (nv->v_quad != v->v_quad) {
2109 if (op == ASSIGN && tp->t_isfield) {
2110 /* precision lost in bit-field assignment */
2112 } else if (op == INIT && tp->t_isfield) {
2113 /* bit-field initializer out of range */
2115 } else if (op == CASE) {
2116 /* case label affected by conversion */
2118 } else if (op == FARG) {
2119 /* conv. of %s to %s is out of rng., arg #%d */
2120 warning(295, tyname(lbuf, sizeof(lbuf),
2121 gettyp(ot)), tyname(rbuf, sizeof(rbuf), tp),
2124 /* conversion of %s to %s is out of range */
2125 warning(119, tyname(lbuf, sizeof(lbuf),
2127 tyname(rbuf, sizeof(rbuf), tp));
2134 * Called if incompatible types were detected.
2135 * Prints an appropriate warning.
2138 incompat(op_t op, tspec_t lt, tspec_t rt)
2144 if (lt == VOID || (mp->m_binary && rt == VOID)) {
2145 /* void type illegal in expression */
2147 } else if (op == ASSIGN) {
2148 if ((lt == STRUCT || lt == UNION) &&
2149 (rt == STRUCT || rt == UNION)) {
2150 /* assignment of different structures */
2153 /* assignment type mismatch */
2156 } else if (mp->m_binary) {
2157 /* operands of %s have incompatible types */
2158 error(107, mp->m_name);
2160 /* operand of %s has incompatible type */
2161 error(108, mp->m_name);
2166 * Called if incompatible pointer types are detected.
2167 * Print an appropriate warning.
2170 illptrc(mod_t *mp, type_t *ltp, type_t *rtp)
2174 if (ltp->t_tspec != PTR || rtp->t_tspec != PTR)
2175 LERROR("illptrc()");
2177 lt = ltp->t_subt->t_tspec;
2178 rt = rtp->t_subt->t_tspec;
2180 if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION)) {
2182 /* illegal structure pointer combination */
2185 /* illegal structure pointer combination, op %s */
2186 warning(245, mp->m_name);
2190 /* illegal pointer combination */
2193 /* illegal pointer combination, op %s */
2194 warning(124, mp->m_name);
2200 * Make sure type (*tpp)->t_subt has at least the qualifiers
2201 * of tp1->t_subt and tp2->t_subt.
2204 mrgqual(type_t **tpp, type_t *tp1, type_t *tp2)
2207 if ((*tpp)->t_tspec != PTR ||
2208 tp1->t_tspec != PTR || tp2->t_tspec != PTR) {
2209 LERROR("mrgqual()");
2212 if ((*tpp)->t_subt->t_const ==
2213 (tp1->t_subt->t_const | tp2->t_subt->t_const) &&
2214 (*tpp)->t_subt->t_volatile ==
2215 (tp1->t_subt->t_volatile | tp2->t_subt->t_volatile)) {
2219 *tpp = tduptyp(*tpp);
2220 (*tpp)->t_subt = tduptyp((*tpp)->t_subt);
2221 (*tpp)->t_subt->t_const =
2222 tp1->t_subt->t_const | tp2->t_subt->t_const;
2223 (*tpp)->t_subt->t_volatile =
2224 tp1->t_subt->t_volatile | tp2->t_subt->t_volatile;
2228 * Returns 1 if the given structure or union has a constant member
2229 * (maybe recursively).
2237 if ((t = tp->t_tspec) != STRUCT && t != UNION)
2238 LERROR("conmemb()");
2239 for (m = tp->t_str->memb; m != NULL; m = m->s_nxt) {
2243 if ((t = tp->t_tspec) == STRUCT || t == UNION) {
2244 if (conmemb(m->s_type))
2252 basictyname(tspec_t t)
2255 case CHAR: return "char";
2256 case UCHAR: return "unsigned char";
2257 case SCHAR: return "signed char";
2258 case SHORT: return "short";
2259 case USHORT: return "unsigned short";
2260 case INT: return "int";
2261 case UINT: return "unsigned int";
2262 case LONG: return "long";
2263 case ULONG: return "unsigned long";
2264 case QUAD: return "long long";
2265 case UQUAD: return "unsigned long long";
2266 case FLOAT: return "float";
2267 case DOUBLE: return "double";
2268 case LDOUBLE: return "long double";
2269 case PTR: return "pointer";
2270 case ENUM: return "enum";
2271 case STRUCT: return "struct";
2272 case UNION: return "union";
2273 case FUNC: return "function";
2274 case ARRAY: return "array";
2276 LERROR("basictyname()");
2282 tyname(char *buf, size_t bufsiz, type_t *tp)
2288 if ((t = tp->t_tspec) == INT && tp->t_isenum)
2310 (void)snprintf(buf, bufsiz, "%s", s);
2313 (void)snprintf(buf, bufsiz, "%s to %s", s,
2314 tyname(lbuf, sizeof(lbuf), tp->t_subt));
2317 (void)snprintf(buf, bufsiz, "%s %s", s,
2318 tp->t_enum->etag->s_name);
2322 (void)snprintf(buf, bufsiz, "%s %s", s,
2323 tp->t_str->stag->s_name);
2326 (void)snprintf(buf, bufsiz, "%s of %s[%d]", s,
2327 tyname(lbuf, sizeof(lbuf), tp->t_subt), tp->t_dim);
2336 * Create a new node for one of the operators POINT and ARROW.
2339 bldstr(op_t op, tnode_t *ln, tnode_t *rn)
2344 if (rn->tn_op != NAME)
2346 if (rn->tn_sym->s_value.v_tspec != INT)
2348 if (rn->tn_sym->s_scl != MOS && rn->tn_sym->s_scl != MOU)
2352 * Remember if the left operand is an lvalue (structure members
2353 * are lvalues if and only if the structure itself is an lvalue).
2355 nolval = op == POINT && !ln->tn_lvalue;
2358 ln = bldamper(ln, 1);
2359 } else if (ln->tn_type->t_tspec != PTR) {
2360 if (!tflag || !isityp(ln->tn_type->t_tspec))
2362 ln = convert(NOOP, 0, tincref(gettyp(VOID), PTR), ln);
2366 ctn = getinode(LONG, rn->tn_sym->s_value.v_quad / CHAR_BIT);
2368 ctn = getinode(INT, rn->tn_sym->s_value.v_quad / CHAR_BIT);
2371 ntn = mktnode(PLUS, tincref(rn->tn_type, PTR), ln, ctn);
2372 if (ln->tn_op == CON)
2375 if (rn->tn_type->t_isfield) {
2376 ntn = mktnode(FSEL, ntn->tn_type->t_subt, ntn, NULL);
2378 ntn = mktnode(STAR, ntn->tn_type->t_subt, ntn, NULL);
2388 * Create a node for INCAFT, INCBEF, DECAFT and DECBEF.
2391 bldincdec(op_t op, tnode_t *ln)
2396 LERROR("bldincdec()");
2398 if (ln->tn_type->t_tspec == PTR) {
2399 cn = plength(ln->tn_type);
2401 cn = getinode(INT, (int64_t)1);
2403 ntn = mktnode(op, ln->tn_type, ln, cn);
2409 * Create a tree node for the & operator
2412 bldamper(tnode_t *tn, int noign)
2417 if (!noign && ((t = tn->tn_type->t_tspec) == ARRAY || t == FUNC)) {
2418 /* & before array or function: ignored */
2425 if (tn->tn_op == STAR &&
2426 tn->tn_left->tn_type->t_tspec == PTR &&
2427 tn->tn_left->tn_type->t_subt == tn->tn_type) {
2428 return (tn->tn_left);
2431 ntn = mktnode(AMPER, tincref(tn->tn_type, PTR), tn, NULL);
2437 * Create a node for operators PLUS and MINUS.
2440 bldplmi(op_t op, tnode_t *ln, tnode_t *rn)
2445 /* If pointer and integer, then pointer to the lhs. */
2446 if (rn->tn_type->t_tspec == PTR && isityp(ln->tn_type->t_tspec)) {
2452 if (ln->tn_type->t_tspec == PTR && rn->tn_type->t_tspec != PTR) {
2454 if (!isityp(rn->tn_type->t_tspec))
2455 LERROR("bldplmi()");
2457 ctn = plength(ln->tn_type);
2458 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
2459 rn = convert(NOOP, 0, ctn->tn_type, rn);
2460 rn = mktnode(MULT, rn->tn_type, rn, ctn);
2461 if (rn->tn_left->tn_op == CON)
2463 ntn = mktnode(op, ln->tn_type, ln, rn);
2465 } else if (rn->tn_type->t_tspec == PTR) {
2467 if (ln->tn_type->t_tspec != PTR || op != MINUS)
2468 LERROR("bldplmi()");
2474 ntn = mktnode(op, tp, ln, rn);
2475 if (ln->tn_op == CON && rn->tn_op == CON)
2477 ctn = plength(ln->tn_type);
2478 balance(NOOP, &ntn, &ctn);
2479 ntn = mktnode(DIV, tp, ntn, ctn);
2483 ntn = mktnode(op, ln->tn_type, ln, rn);
2490 * Create a node for operators SHL and SHR.
2493 bldshft(op_t op, tnode_t *ln, tnode_t *rn)
2498 if ((t = rn->tn_type->t_tspec) != INT && t != UINT)
2499 rn = convert(CVT, 0, gettyp(INT), rn);
2500 ntn = mktnode(op, ln->tn_type, ln, rn);
2505 * Create a node for COLON.
2508 bldcol(tnode_t *ln, tnode_t *rn)
2510 tspec_t lt, rt, pdt;
2514 lt = ln->tn_type->t_tspec;
2515 rt = rn->tn_type->t_tspec;
2523 * Arithmetic types are balanced, all other type combinations
2524 * still need to be handled.
2526 if (isatyp(lt) && isatyp(rt)) {
2528 } else if (lt == VOID || rt == VOID) {
2530 } else if (lt == STRUCT || lt == UNION) {
2531 /* Both types must be identical. */
2532 if (rt != STRUCT && rt != UNION)
2534 if (ln->tn_type->t_str != rn->tn_type->t_str)
2536 if (incompl(ln->tn_type)) {
2537 /* unknown operand size, op %s */
2538 error(138, modtab[COLON].m_name);
2542 } else if (lt == PTR && isityp(rt)) {
2544 rn = convert(NOOP, 0, gettyp(pdt), rn);
2548 } else if (rt == PTR && isityp(lt)) {
2550 ln = convert(NOOP, 0, gettyp(pdt), ln);
2554 } else if (lt == PTR && ln->tn_type->t_subt->t_tspec == VOID) {
2558 mrgqual(&rtp, ln->tn_type, rn->tn_type);
2559 } else if (rt == PTR && rn->tn_type->t_subt->t_tspec == VOID) {
2563 mrgqual(&rtp, ln->tn_type, rn->tn_type);
2565 if (lt != PTR || rt != PTR)
2568 * XXX For now we simply take the left type. This is
2569 * probably wrong, if one type contains a function prototype
2570 * and the other one, at the same place, only an old style
2574 mrgqual(&rtp, ln->tn_type, rn->tn_type);
2577 ntn = mktnode(COLON, rtp, ln, rn);
2583 * Create a node for an assignment operator (both = and op= ).
2586 bldasgn(op_t op, tnode_t *ln, tnode_t *rn)
2591 if (ln == NULL || rn == NULL)
2592 LERROR("bldasgn()");
2594 lt = ln->tn_type->t_tspec;
2595 rt = rn->tn_type->t_tspec;
2597 if ((op == ADDASS || op == SUBASS) && lt == PTR) {
2599 LERROR("bldasgn()");
2600 ctn = plength(ln->tn_type);
2601 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
2602 rn = convert(NOOP, 0, ctn->tn_type, rn);
2603 rn = mktnode(MULT, rn->tn_type, rn, ctn);
2604 if (rn->tn_left->tn_op == CON)
2608 if ((op == ASSIGN || op == RETURN) && (lt == STRUCT || rt == STRUCT)) {
2609 if (rt != lt || ln->tn_type->t_str != rn->tn_type->t_str)
2610 LERROR("bldasgn()");
2611 if (incompl(ln->tn_type)) {
2613 /* cannot return incomplete type */
2616 /* unknown operand size, op %s */
2617 error(138, modtab[op].m_name);
2624 if (psize(lt) < psize(rt)) {
2626 /* semantics of %s change in ANSI C; use ... */
2627 warning(118, "<<=");
2629 } else if (op != SHRASS) {
2630 if (op == ASSIGN || lt != PTR) {
2632 (ln->tn_type->t_isfield && rn->tn_op == CON)) {
2633 rn = convert(op, 0, ln->tn_type, rn);
2639 ntn = mktnode(op, ln->tn_type, ln, rn);
2645 * Get length of type tp->t_subt.
2653 if (tp->t_tspec != PTR)
2654 LERROR("plength()");
2660 while (tp->t_tspec == ARRAY) {
2665 switch (tp->t_tspec) {
2667 /* pointer to function is not allowed here */
2671 /* cannot do pointer arithmetic on operand of ... */
2676 if ((elsz = tp->t_str->size) == 0)
2677 /* cannot do pointer arithmetic on operand of ... */
2682 /* cannot do pointer arithmetic on operand of ... */
2687 if ((elsz = size(tp->t_tspec)) == 0) {
2688 /* cannot do pointer arithmetic on operand of ... */
2690 } else if (elsz == -1) {
2691 LERROR("plength()");
2696 if (elem == 0 && elsz != 0) {
2697 /* cannot do pointer arithmetic on operand of ... */
2710 return (getinode(st, (int64_t)(elem * elsz / CHAR_BIT)));
2715 * Note: There appear to be a number of bugs in detecting overflow in
2716 * this function. An audit and a set of proper regression tests are needed.
2717 * --Perry Metzger, Nov. 16, 2001
2720 * Do only as much as necessary to compute constant expressions.
2721 * Called only if the operator allows folding and (both) operands
2730 int64_t sl, sr = 0, q = 0, mask;
2731 uint64_t ul, ur = 0;
2734 if ((v = calloc(1, sizeof (val_t))) == NULL)
2736 v->v_tspec = t = tn->tn_type->t_tspec;
2738 utyp = t == PTR || isutyp(t);
2739 ul = sl = tn->tn_left->tn_val->v_quad;
2740 if (modtab[tn->tn_op].m_binary)
2741 ur = sr = tn->tn_right->tn_val->v_quad;
2743 mask = qlmasks[size(t)];
2746 switch (tn->tn_op) {
2752 if (msb(q, t, -1) == msb(sl, t, -1))
2761 if (q != (q & mask))
2763 else if ((ul != 0) && ((q / ul) != ur))
2767 if (msb(q, t, -1) != (msb(sl, t, -1) ^ msb(sr, t, -1)))
2775 q = utyp ? UQUAD_MAX : QUAD_MAX;
2777 q = utyp ? ul / ur : sl / sr;
2786 q = utyp ? ul % ur : sl % sr;
2790 q = utyp ? ul + ur : sl + sr;
2791 if (msb(sl, t, -1) != 0 && msb(sr, t, -1) != 0) {
2792 if (msb(q, t, -1) == 0)
2794 } else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) == 0) {
2795 if (msb(q, t, -1) != 0)
2800 q = utyp ? ul - ur : sl - sr;
2801 if (msb(sl, t, -1) != 0 && msb(sr, t, -1) == 0) {
2802 if (msb(q, t, -1) == 0)
2804 } else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) != 0) {
2805 if (msb(q, t, -1) != 0)
2810 q = utyp ? ul << sr : sl << sr;
2814 * The sign must be explicitly extended because
2815 * shifts of signed values are implementation dependent.
2818 q = xsign(q, t, size(t) - (int)sr);
2821 q = utyp ? ul < ur : sl < sr;
2824 q = utyp ? ul <= ur : sl <= sr;
2827 q = utyp ? ul >= ur : sl >= sr;
2830 q = utyp ? ul > ur : sl > sr;
2833 q = utyp ? ul == ur : sl == sr;
2836 q = utyp ? ul != ur : sl != sr;
2839 q = utyp ? ul & ur : sl & sr;
2842 q = utyp ? ul ^ ur : sl ^ sr;
2845 q = utyp ? ul | ur : sl | sr;
2851 /* XXX does not work for quads. */
2852 if (ovfl || ((q | mask) != ~(uint64_t)0 && (q & ~mask) != 0)) {
2854 /* integer overflow detected, op %s */
2855 warning(141, modtab[tn->tn_op].m_name);
2858 v->v_quad = xsign(q, t, -1);
2860 cn = getcnode(tn->tn_type, v);
2866 * Same for operators whose operands are compared with 0 (test context).
2869 foldtst(tnode_t *tn)
2874 if ((v = calloc(1, sizeof (val_t))) == NULL)
2876 v->v_tspec = tn->tn_type->t_tspec;
2877 if (tn->tn_type->t_tspec != INT)
2878 LERROR("foldtst()");
2880 if (isftyp(tn->tn_left->tn_type->t_tspec)) {
2881 l = tn->tn_left->tn_val->v_ldbl != 0.0;
2883 l = tn->tn_left->tn_val->v_quad != 0;
2886 if (modtab[tn->tn_op].m_binary) {
2887 if (isftyp(tn->tn_right->tn_type->t_tspec)) {
2888 r = tn->tn_right->tn_val->v_ldbl != 0.0;
2890 r = tn->tn_right->tn_val->v_quad != 0;
2894 switch (tn->tn_op) {
2897 /* constant argument to NOT */
2908 LERROR("foldtst()");
2911 return (getcnode(tn->tn_type, v));
2915 * Same for operands with floating point type.
2918 foldflt(tnode_t *tn)
2924 if ((v = calloc(1, sizeof (val_t))) == NULL)
2926 v->v_tspec = t = tn->tn_type->t_tspec;
2929 LERROR("foldflt()");
2931 if (t != tn->tn_left->tn_type->t_tspec)
2932 LERROR("foldflt()");
2933 if (modtab[tn->tn_op].m_binary && t != tn->tn_right->tn_type->t_tspec)
2934 LERROR("foldflt()");
2936 l = tn->tn_left->tn_val->v_ldbl;
2937 if (modtab[tn->tn_op].m_binary)
2938 r = tn->tn_right->tn_val->v_ldbl;
2940 switch (tn->tn_op) {
2955 v->v_ldbl = l < 0 ? -FLT_MAX : FLT_MAX;
2956 } else if (t == DOUBLE) {
2957 v->v_ldbl = l < 0 ? -DBL_MAX : DBL_MAX;
2959 v->v_ldbl = l < 0 ? -LDBL_MAX : LDBL_MAX;
2990 LERROR("foldflt()");
2993 if (isnan((double)v->v_ldbl))
2994 LERROR("foldflt()");
2995 if (!finite((double)v->v_ldbl) ||
2997 (v->v_ldbl > FLT_MAX || v->v_ldbl < -FLT_MAX)) ||
2999 (v->v_ldbl > DBL_MAX || v->v_ldbl < -DBL_MAX))) {
3000 /* floating point overflow detected, op %s */
3001 warning(142, modtab[tn->tn_op].m_name);
3003 v->v_ldbl = v->v_ldbl < 0 ? -FLT_MAX : FLT_MAX;
3004 } else if (t == DOUBLE) {
3005 v->v_ldbl = v->v_ldbl < 0 ? -DBL_MAX : DBL_MAX;
3007 v->v_ldbl = v->v_ldbl < 0 ? -LDBL_MAX: LDBL_MAX;
3011 return (getcnode(tn->tn_type, v));
3015 * Create a constant node for sizeof.
3024 while (tp->t_tspec == ARRAY) {
3029 /* cannot take size of incomplete type */
3033 switch (tp->t_tspec) {
3035 /* cannot take size of function */
3042 /* cannot take size of incomplete type */
3046 elsz = tp->t_str->size;
3051 /* cannot take size of incomplete type */
3056 if (tp->t_isfield) {
3057 /* cannot take size of bit-field */
3060 if (tp->t_tspec == VOID) {
3061 /* cannot take size of void */
3065 elsz = size(tp->t_tspec);
3067 LERROR("bldszof()");
3078 return (getinode(st, (int64_t)(elem * elsz / CHAR_BIT)));
3085 cast(tnode_t *tn, type_t *tp)
3095 ot = tn->tn_type->t_tspec;
3099 * XXX ANSI C requires scalar types or void (Plauger&Brodie).
3100 * But this seams really questionable.
3102 } else if (nt == STRUCT || nt == UNION || nt == ARRAY || nt == FUNC) {
3103 /* invalid cast expression */
3106 } else if (ot == STRUCT || ot == UNION) {
3107 /* invalid cast expression */
3110 } else if (ot == VOID) {
3111 /* improper cast of void expression */
3114 } else if (isityp(nt) && issclt(ot)) {
3116 } else if (isftyp(nt) && isatyp(ot)) {
3118 } else if (nt == PTR && isityp(ot)) {
3120 } else if (nt == PTR && ot == PTR) {
3121 if (!tp->t_subt->t_const && tn->tn_type->t_subt->t_const) {
3123 /* cast discards 'const' from ... */
3127 /* invalid cast expression */
3132 tn = convert(CVT, 0, tp, tn);
3139 * Create the node for a function argument.
3140 * All necessary conversions and type checks are done in funccall(), because
3141 * in funcarg() we have no information about expected argument types.
3144 funcarg(tnode_t *args, tnode_t *arg)
3149 * If there was a serious error in the expression for the argument,
3150 * create a dummy argument so the positions of the remaining arguments
3154 arg = getinode(INT, (int64_t)0);
3156 ntn = mktnode(PUSH, arg->tn_type, arg, args);
3162 * Create the node for a function call. Also check types of
3163 * function arguments and insert conversions, if necessary.
3166 funccall(tnode_t *func, tnode_t *args)
3174 if (func->tn_op == NAME && func->tn_type->t_tspec == FUNC) {
3181 * after cconv() func will always be a pointer to a function
3182 * if it is a valid function designator.
3186 if (func->tn_type->t_tspec != PTR ||
3187 func->tn_type->t_subt->t_tspec != FUNC) {
3188 /* illegal function */
3193 args = chkfarg(func->tn_type->t_subt, args);
3195 ntn = mktnode(fcop, func->tn_type->t_subt->t_subt, func, args);
3201 * Check types of all function arguments and insert conversions,
3205 chkfarg(type_t *ftp, tnode_t *args)
3210 int narg, npar, n, i;
3212 /* get # of args in the prototype */
3214 for (asym = ftp->t_args; asym != NULL; asym = asym->s_nxt)
3217 /* get # of args in function call */
3219 for (arg = args; arg != NULL; arg = arg->tn_right)
3223 if (ftp->t_proto && npar != narg && !(ftp->t_vararg && npar < narg)) {
3224 /* argument mismatch: %d arg%s passed, %d expected */
3225 error(150, narg, narg > 1 ? "s" : "", npar);
3229 for (n = 1; n <= narg; n++) {
3232 * The rightmost argument is at the top of the argument
3235 for (i = narg, arg = args; i > n; i--, arg = arg->tn_right)
3238 /* some things which are always not allowd */
3239 if ((at = arg->tn_left->tn_type->t_tspec) == VOID) {
3240 /* void expressions may not be arguments, arg #%d */
3243 } else if ((at == STRUCT || at == UNION) &&
3244 incompl(arg->tn_left->tn_type)) {
3245 /* argument cannot have unknown size, arg #%d */
3248 } else if (isityp(at) && arg->tn_left->tn_type->t_isenum &&
3249 incompl(arg->tn_left->tn_type)) {
3250 /* argument cannot have unknown size, arg #%d */
3254 /* class conversions (arg in value context) */
3255 arg->tn_left = cconv(arg->tn_left);
3258 arg->tn_left = parg(n, asym->s_type, arg->tn_left);
3260 arg->tn_left = promote(NOOP, 1, arg->tn_left);
3262 arg->tn_type = arg->tn_left->tn_type;
3272 * Compare the type of an argument with the corresponding type of a
3273 * prototype parameter. If it is a valid combination, but both types
3274 * are not the same, insert a conversion to convert the argument into
3275 * the type of the parameter.
3278 parg( int n, /* pos of arg */
3279 type_t *tp, /* expected type (from prototype) */
3280 tnode_t *tn) /* argument */
3285 if ((ln = calloc(1, sizeof (tnode_t))) == NULL)
3287 ln->tn_type = tduptyp(tp);
3288 ln->tn_type->t_const = 0;
3290 if (typeok(FARG, n, ln, tn)) {
3291 if (!eqtype(tp, tn->tn_type, 1, 0, (warn = 0, &warn)) || warn)
3292 tn = convert(FARG, n, tp, tn);
3299 * Return the value of an integral constant expression.
3300 * If the expression is not constant or its type is not an integer
3301 * type, an error message is printed.
3304 constant(tnode_t *tn, int required)
3311 tn = promote(NOOP, 0, tn);
3313 if ((v = calloc(1, sizeof (val_t))) == NULL)
3318 LERROR("constant()");
3324 v->v_tspec = tn->tn_type->t_tspec;
3326 if (tn->tn_op == CON) {
3327 if (tn->tn_type->t_tspec != tn->tn_val->v_tspec)
3328 LERROR("constant()");
3329 if (isityp(tn->tn_val->v_tspec)) {
3330 v->v_ansiu = tn->tn_val->v_ansiu;
3331 v->v_quad = tn->tn_val->v_quad;
3334 v->v_quad = tn->tn_val->v_ldbl;
3339 /* integral constant expression expected */
3345 if (!isityp(v->v_tspec))
3352 * Perform some tests on expressions which can't be done in build() and
3353 * functions called by build(). These tests must be done here because
3354 * we need some information about the context in which the operations
3356 * After all tests are performed, expr() frees the memory which is used
3357 * for the expression.
3360 expr(tnode_t *tn, int vctx, int tctx, int freeblk)
3363 if (tn == NULL && nerr == 0)
3371 /* expr() is also called in global initialisations */
3372 if (dcs->d_ctx != EXTERN)
3375 chkmisc(tn, vctx, tctx, !tctx, 0, 0, 0);
3376 if (tn->tn_op == ASSIGN) {
3378 /* assignment in conditional context */
3380 } else if (tn->tn_op == CON) {
3381 if (hflag && tctx && !ccflg)
3382 /* constant in conditional context */
3385 if (!modtab[tn->tn_op].m_sideeff) {
3387 * for left operands of COMMA this warning is already
3390 if (tn->tn_op != COMMA && !vctx && !tctx)
3396 /* free the tree memory */
3402 nulleff(tnode_t *tn)
3408 while (!modtab[tn->tn_op].m_sideeff) {
3409 if (tn->tn_op == CVT && tn->tn_type->t_tspec == VOID) {
3411 } else if (tn->tn_op == LOGAND || tn->tn_op == LOGOR) {
3413 * && and || have a side effect if the right operand
3414 * has a side effect.
3417 } else if (tn->tn_op == QUEST) {
3419 * ? has a side effect if at least one of its right
3420 * operands has a side effect
3423 } else if (tn->tn_op == COLON || tn->tn_op == COMMA) {
3425 * : has a side effect if at least one of its operands
3428 if (modtab[tn->tn_left->tn_op].m_sideeff) {
3430 } else if (modtab[tn->tn_right->tn_op].m_sideeff) {
3439 if (!modtab[tn->tn_op].m_sideeff)
3440 /* expression has null effect */
3445 * Dump an expression to stdout
3446 * only used for debugging
3449 displexpr(tnode_t *tn, int offs)
3454 (void)printf("%*s%s\n", offs, "", "NULL");
3457 (void)printf("%*sop %s ", offs, "", modtab[tn->tn_op].m_name);
3459 if (tn->tn_op == NAME) {
3460 (void)printf("%s: %s ",
3461 tn->tn_sym->s_name, scltoa(tn->tn_sym->s_scl));
3462 } else if (tn->tn_op == CON && isftyp(tn->tn_type->t_tspec)) {
3463 (void)printf("%#g ", (double)tn->tn_val->v_ldbl);
3464 } else if (tn->tn_op == CON && isityp(tn->tn_type->t_tspec)) {
3465 uq = tn->tn_val->v_quad;
3466 (void)printf("0x %08lx %08lx ", (long)(uq >> 32) & 0xffffffffl,
3467 (long)uq & 0xffffffffl);
3468 } else if (tn->tn_op == CON) {
3469 if (tn->tn_type->t_tspec != PTR)
3470 LERROR("displexpr()");
3471 (void)printf("0x%0*lx ", (int)(sizeof (void *) * CHAR_BIT / 4),
3472 (u_long)tn->tn_val->v_quad);
3473 } else if (tn->tn_op == STRING) {
3474 if (tn->tn_strg->st_tspec == CHAR) {
3475 (void)printf("\"%s\"", tn->tn_strg->st_cp);
3479 n = MB_CUR_MAX * (tn->tn_strg->st_len + 1);
3480 if ((s = malloc(n)) == NULL)
3482 (void)wcstombs(s, tn->tn_strg->st_wcp, n);
3483 (void)printf("L\"%s\"", s);
3487 } else if (tn->tn_op == FSEL) {
3488 (void)printf("o=%d, l=%d ", tn->tn_type->t_foffs,
3489 tn->tn_type->t_flen);
3491 (void)printf("%s\n", ttos(tn->tn_type));
3492 if (tn->tn_op == NAME || tn->tn_op == CON || tn->tn_op == STRING)
3494 displexpr(tn->tn_left, offs + 2);
3495 if (modtab[tn->tn_op].m_binary ||
3496 (tn->tn_op == PUSH && tn->tn_right != NULL)) {
3497 displexpr(tn->tn_right, offs + 2);
3502 * Called by expr() to recursively perform some tests.
3506 chkmisc(tnode_t *tn, int vctx, int tctx, int eqwarn, int fcall, int rvdisc,
3511 int nrvdisc, cvctx, ctctx;
3521 mp = &modtab[op = tn->tn_op];
3525 if (ln->tn_op == NAME && (reached || rchflg)) {
3527 setsflg(ln->tn_sym);
3528 setuflg(ln->tn_sym, fcall, szof);
3530 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3531 /* check the range of array indices */
3532 chkaidx(ln->tn_left, 1);
3535 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3536 /* check the range of array indices */
3537 chkaidx(ln->tn_left, 0);
3554 if (ln->tn_op == NAME && (reached || rchflg)) {
3555 sc = ln->tn_sym->s_scl;
3557 * Look if there was an asm statement in one of the
3558 * compound statements we are in. If not, we don't
3561 for (di = dcs; di != NULL; di = di->d_nxt) {
3565 if (sc != EXTERN && sc != STATIC &&
3566 !ln->tn_sym->s_set && !szof && di == NULL) {
3567 /* %s may be used before set */
3568 warning(158, ln->tn_sym->s_name);
3569 setsflg(ln->tn_sym);
3571 setuflg(ln->tn_sym, 0, 0);
3575 if (ln->tn_op == NAME && !szof && (reached || rchflg)) {
3576 setsflg(ln->tn_sym);
3577 if (ln->tn_sym->s_scl == EXTERN)
3580 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3581 /* check the range of array indices */
3582 chkaidx(ln->tn_left, 0);
3585 if (ln->tn_op != AMPER || ln->tn_left->tn_op != NAME)
3586 LERROR("chkmisc()");
3588 outcall(tn, vctx || tctx, rvdisc);
3591 /* equality operator "==" found where "=" was exp. */
3592 if (hflag && eqwarn)
3599 /* LINTED (enumeration values not handled in switch) */
3643 * values of operands of ':' are not used if the type of at least
3644 * one of the operands (for gcc compatibility) is void
3645 * XXX test/value context of QUEST should probably be used as
3646 * context for both operands of COLON
3648 if (op == COLON && tn->tn_type->t_tspec == VOID)
3650 nrvdisc = op == CVT && tn->tn_type->t_tspec == VOID;
3651 chkmisc(ln, cvctx, ctctx, mp->m_eqwarn, op == CALL, nrvdisc, szof);
3656 chkmisc(rn, 0, 0, mp->m_eqwarn, 0, 0, szof);
3660 chkmisc(rn, 0, 1, mp->m_eqwarn, 0, 0, szof);
3663 chkmisc(rn, cvctx, ctctx, mp->m_eqwarn, 0, 0, szof);
3666 chkmisc(rn, vctx, tctx, mp->m_eqwarn, 0, 0, szof);
3670 chkmisc(rn, 1, 0, mp->m_eqwarn, 0, 0, szof);
3677 * Checks the range of array indices, if possible.
3678 * amper is set if only the address of the element is used. This
3679 * means that the index is allowed to refer to the first element
3683 chkaidx(tnode_t *tn, int amper)
3693 /* We can only check constant indices. */
3694 if (rn->tn_op != CON)
3697 /* Return if the left node does not stem from an array. */
3698 if (ln->tn_op != AMPER)
3700 if (ln->tn_left->tn_op != STRING && ln->tn_left->tn_op != NAME)
3702 if (ln->tn_left->tn_type->t_tspec != ARRAY)
3706 * For incomplete array types, we can print a warning only if
3707 * the index is negative.
3709 if (incompl(ln->tn_left->tn_type) && rn->tn_val->v_quad >= 0)
3712 /* Get the size of one array element */
3713 if ((elsz = length(ln->tn_type->t_subt, NULL)) == 0)
3717 /* Change the unit of the index from bytes to element size. */
3718 if (isutyp(rn->tn_type->t_tspec)) {
3719 con = (uint64_t)rn->tn_val->v_quad / elsz;
3721 con = rn->tn_val->v_quad / elsz;
3724 dim = ln->tn_left->tn_type->t_dim + (amper ? 1 : 0);
3726 if (!isutyp(rn->tn_type->t_tspec) && con < 0) {
3727 /* array subscript cannot be negative: %ld */
3728 warning(167, (long)con);
3729 } else if (dim > 0 && (uint64_t)con >= dim) {
3730 /* array subscript cannot be > %d: %ld */
3731 warning(168, dim - 1, (long)con);
3736 * Check for ordered comparisons of unsigned values with 0.
3739 chkcomp(op_t op, tnode_t *ln, tnode_t *rn)
3745 lt = ln->tn_type->t_tspec;
3746 rt = rn->tn_type->t_tspec;
3749 if (ln->tn_op != CON && rn->tn_op != CON)
3752 if (!isityp(lt) || !isityp(rt))
3755 if ((hflag || pflag) && lt == CHAR && rn->tn_op == CON &&
3756 (rn->tn_val->v_quad < 0 ||
3757 rn->tn_val->v_quad > ~(~0 << (CHAR_BIT - 1)))) {
3758 /* nonportable character comparison, op %s */
3759 warning(230, mp->m_name);
3762 if ((hflag || pflag) && rt == CHAR && ln->tn_op == CON &&
3763 (ln->tn_val->v_quad < 0 ||
3764 ln->tn_val->v_quad > ~(~0 << (CHAR_BIT - 1)))) {
3765 /* nonportable character comparison, op %s */
3766 warning(230, mp->m_name);
3769 if (isutyp(lt) && !isutyp(rt) &&
3770 rn->tn_op == CON && rn->tn_val->v_quad <= 0) {
3771 if (rn->tn_val->v_quad < 0) {
3772 /* comparison of %s with %s, op %s */
3773 warning(162, tyname(buf, sizeof(buf), ln->tn_type),
3774 "negative constant", mp->m_name);
3775 } else if (op == LT || op == GE || (hflag && op == LE)) {
3776 /* comparison of %s with %s, op %s */
3777 warning(162, tyname(buf, sizeof(buf), ln->tn_type),
3782 if (isutyp(rt) && !isutyp(lt) &&
3783 ln->tn_op == CON && ln->tn_val->v_quad <= 0) {
3784 if (ln->tn_val->v_quad < 0) {
3785 /* comparison of %s with %s, op %s */
3786 warning(162, "negative constant",
3787 tyname(buf, sizeof(buf), rn->tn_type), mp->m_name);
3788 } else if (op == GT || op == LE || (hflag && op == GE)) {
3789 /* comparison of %s with %s, op %s */
3790 warning(162, "0", tyname(buf, sizeof(buf), rn->tn_type),
3798 * Takes an expression and returns 0 if this expression can be used
3799 * for static initialisation, otherwise -1.
3801 * Constant initialisation expressions must be constant or an address
3802 * of a static object with an optional offset. In the first case,
3803 * the result is returned in *offsp. In the second case, the static
3804 * object is returned in *symp and the offset in *offsp.
3806 * The expression can consist of PLUS, MINUS, AMPER, NAME, STRING and
3807 * CON. Type conversions are allowed if they do not change binary
3808 * representation (including width).
3811 conaddr(tnode_t *tn, sym_t **symp, ptrdiff_t *offsp)
3814 ptrdiff_t offs1, offs2;
3817 switch (tn->tn_op) {
3819 if (tn->tn_right->tn_op == CVT)
3820 return conaddr(tn->tn_right, symp, offsp);
3821 else if (tn->tn_right->tn_op != CON)
3826 if (tn->tn_left->tn_op == CON) {
3827 offs1 = (ptrdiff_t)tn->tn_left->tn_val->v_quad;
3828 if (conaddr(tn->tn_right, &sym, &offs2) == -1)
3830 } else if (tn->tn_right->tn_op == CON) {
3831 offs2 = (ptrdiff_t)tn->tn_right->tn_val->v_quad;
3832 if (tn->tn_op == MINUS)
3834 if (conaddr(tn->tn_left, &sym, &offs1) == -1)
3840 *offsp = offs1 + offs2;
3843 if (tn->tn_left->tn_op == NAME) {
3844 *symp = tn->tn_left->tn_sym;
3846 } else if (tn->tn_left->tn_op == STRING) {
3848 * If this would be the front end of a compiler we
3849 * would return a label instead of 0.
3855 t = tn->tn_type->t_tspec;
3856 ot = tn->tn_left->tn_type->t_tspec;
3857 if ((!isityp(t) && t != PTR) || (!isityp(ot) && ot != PTR))
3859 else if (psize(t) != psize(ot)) {
3862 if (conaddr(tn->tn_left, symp, offsp) == -1)
3872 * Concatenate two string constants.
3875 catstrg(strg_t *strg1, strg_t *strg2)
3877 size_t len1, len2, len;
3879 if (strg1->st_tspec != strg2->st_tspec) {
3880 /* cannot concatenate wide and regular string literals */
3885 len = (len1 = strg1->st_len) + (len2 = strg2->st_len);
3887 if (strg1->st_tspec == CHAR) {
3888 if ((strg1->st_cp = realloc(strg1->st_cp, len + 1)) == NULL)
3890 (void)memcpy(strg1->st_cp + len1, strg2->st_cp, len2 + 1);
3893 if ((strg1->st_wcp = realloc(strg1->st_wcp, (len + 1) *
3894 sizeof (wchar_t))) == NULL)
3896 (void)memcpy(strg1->st_wcp + len1, strg2->st_wcp,
3897 (len2 + 1) * sizeof (wchar_t));
3898 free(strg2->st_wcp);
3900 strg1->st_len = len;
3907 * Print a warning if the given node has operands which should be
3910 * XXX Does not work if an operand is a constant expression. Constant
3911 * expressions are already folded.
3914 precconf(tnode_t *tn)
3917 op_t lop, rop = NOOP;
3918 int lparn, rparn = 0;
3925 mp = &modtab[tn->tn_op];
3928 for (ln = tn->tn_left; ln->tn_op == CVT; ln = ln->tn_left)
3929 lparn |= ln->tn_parn;
3930 lparn |= ln->tn_parn;
3935 for (rn = tn->tn_right; tn->tn_op == CVT; rn = rn->tn_left)
3936 rparn |= rn->tn_parn;
3937 rparn |= rn->tn_parn;
3943 switch (tn->tn_op) {
3946 if (!lparn && (lop == PLUS || lop == MINUS)) {
3948 } else if (!rparn && (rop == PLUS || rop == MINUS)) {
3953 if (!lparn && lop == LOGAND) {
3955 } else if (!rparn && rop == LOGAND) {
3962 if (!lparn && lop != tn->tn_op) {
3963 if (lop == PLUS || lop == MINUS) {
3965 } else if (lop == AND || lop == XOR) {
3969 if (!warn && !rparn && rop != tn->tn_op) {
3970 if (rop == PLUS || rop == MINUS) {
3972 } else if (rop == AND || rop == XOR) {
3977 /* LINTED (enumeration values not handled in switch) */
4036 /* precedence confusion possible: parenthesize! */