1 /* $NetBSD: tree.c,v 1.24 2002/01/31 22:30:20 tv 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.24 2002/01/31 22:30:20 tv 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);
306 error(99, sym->s_name);
310 if (sym->s_kind != FVFT && sym->s_kind != FMOS)
311 lerror("getnnode() 1");
314 n->tn_type = sym->s_type;
315 if (sym->s_scl != ENUMCON) {
318 if (sym->s_kind == FVFT && sym->s_type->t_tspec != FUNC)
322 n->tn_val = tgetblk(sizeof (val_t));
323 *n->tn_val = sym->s_value;
330 * Create a node for a string.
333 getsnode(strg_t *strg)
343 n->tn_type = tincref(gettyp(strg->st_tspec), ARRAY);
344 n->tn_type->t_dim = len + 1;
347 n->tn_strg = tgetblk(sizeof (strg_t));
348 n->tn_strg->st_tspec = strg->st_tspec;
349 n->tn_strg->st_len = len;
351 if (strg->st_tspec == CHAR) {
352 n->tn_strg->st_cp = tgetblk(len + 1);
353 (void)memcpy(n->tn_strg->st_cp, strg->st_cp, len + 1);
356 n->tn_strg->st_wcp = tgetblk((len + 1) * sizeof (wchar_t));
357 (void)memcpy(n->tn_strg->st_wcp, strg->st_wcp,
358 (len + 1) * sizeof (wchar_t));
367 * Returns a symbol which has the same name as the msym argument and is a
368 * member of the struct or union specified by the tn argument.
371 strmemb(tnode_t *tn, op_t op, sym_t *msym)
380 * Remove the member if it was unknown until now (Which means
381 * that no defined struct or union has a member with the same name).
383 if (msym->s_scl == NOSCL) {
384 /* undefined struct/union member: %s */
385 error(101, msym->s_name);
389 msym->s_styp = tgetblk(sizeof (str_t));
390 msym->s_styp->stag = tgetblk(sizeof (sym_t));
391 msym->s_styp->stag->s_name = unnamed;
392 msym->s_value.v_tspec = INT;
396 /* Set str to the tag of which msym is expected to be a member. */
398 t = (tp = tn->tn_type)->t_tspec;
400 if (t == STRUCT || t == UNION)
402 } else if (op == ARROW && t == PTR) {
403 t = (tp = tp->t_subt)->t_tspec;
404 if (t == STRUCT || t == UNION)
409 * If this struct/union has a member with the name of msym, return
413 for (sym = msym; sym != NULL; sym = sym->s_link) {
414 if (sym->s_scl != MOS && sym->s_scl != MOU)
416 if (sym->s_styp != str)
418 if (strcmp(sym->s_name, msym->s_name) != 0)
425 * Set eq to 0 if there are struct/union members with the same name
426 * and different types and/or offsets.
429 for (csym = msym; csym != NULL; csym = csym->s_link) {
430 if (csym->s_scl != MOS && csym->s_scl != MOU)
432 if (strcmp(msym->s_name, csym->s_name) != 0)
434 for (sym = csym->s_link ; sym != NULL; sym = sym->s_link) {
437 if (sym->s_scl != MOS && sym->s_scl != MOU)
439 if (strcmp(csym->s_name, sym->s_name) != 0)
441 if (csym->s_value.v_quad != sym->s_value.v_quad) {
446 eq = eqtype(csym->s_type, sym->s_type, 0, 0, &w) && !w;
449 if (csym->s_field != sym->s_field) {
458 if (tp1->t_flen != tp2->t_flen) {
462 if (tp1->t_foffs != tp2->t_foffs) {
473 * Now handle the case in which the left operand refers really
474 * to a struct/union, but the right operand is not member of it.
477 /* illegal member use: %s */
479 warning(102, msym->s_name);
481 error(102, msym->s_name);
487 * Now the left operand of ARROW does not point to a struct/union
488 * or the left operand of POINT is no struct/union.
492 /* left operand of "." must be struct/union object */
499 /* left operand of "->" must be pointer to ... */
500 if (tflag && tn->tn_type->t_tspec == PTR) {
508 /* non-unique member requires struct/union %s */
509 error(105, op == POINT ? "object" : "pointer");
511 /* unacceptable operand of %s */
512 error(111, modtab[op].m_name);
520 * Create a tree node. Called for most operands except function calls,
525 * rn if not NULL, right operand
528 build(op_t op, tnode_t *ln, tnode_t *rn)
536 /* If there was an error in one of the operands, return. */
537 if (ln == NULL || (mp->m_binary && rn == NULL))
541 * Apply class conversions to the left operand, but only if its
542 * value is needed or it is compaired with null.
544 if (mp->m_vctx || mp->m_tctx)
547 * The right operand is almost always in a test or value context,
548 * except if it is a struct or union member.
550 if (mp->m_binary && op != ARROW && op != POINT)
554 * Print some warnings for comparisons of unsigned values with
555 * constants lower than or equal to null. This must be done
556 * before promote() because otherwise unsigned char and unsigned
557 * short would be promoted to int. Also types are tested to be
558 * CHAR, which would also become int.
564 * Promote the left operand if it is in a test or value context
566 if (mp->m_vctx || mp->m_tctx)
567 ln = promote(op, 0, ln);
569 * Promote the right operand, but only if it is no struct or
570 * union member, or if it is not to be assigned to the left operand
572 if (mp->m_binary && op != ARROW && op != POINT &&
573 op != ASSIGN && op != RETURN) {
574 rn = promote(op, 0, rn);
578 * If the result of the operation is different for signed or
579 * unsigned operands and one of the operands is signed only in
580 * ANSI C, print a warning.
582 if (mp->m_tlansiu && ln->tn_op == CON && ln->tn_val->v_ansiu) {
583 /* ANSI C treats constant as unsigned, op %s */
584 warning(218, mp->m_name);
585 ln->tn_val->v_ansiu = 0;
587 if (mp->m_transiu && rn->tn_op == CON && rn->tn_val->v_ansiu) {
588 /* ANSI C treats constant as unsigned, op %s */
589 warning(218, mp->m_name);
590 rn->tn_val->v_ansiu = 0;
593 /* Make sure both operands are of the same type */
594 if (mp->m_balance || (tflag && (op == SHL || op == SHR)))
595 balance(op, &ln, &rn);
598 * Check types for compatibility with the operation and mutual
599 * compatibility. Return if there are serios problems.
601 if (!typeok(op, 0, ln, rn))
604 /* And now create the node. */
608 ntn = bldstr(op, ln, rn);
614 ntn = bldincdec(op, ln);
617 ntn = bldamper(ln, 0);
620 ntn = mktnode(STAR, ln->tn_type->t_subt, ln, NULL);
624 ntn = bldplmi(op, ln, rn);
628 ntn = bldshft(op, ln, rn);
631 ntn = bldcol(ln, rn);
645 ntn = bldasgn(op, ln, rn);
649 ntn = mktnode(op, rn->tn_type, ln, rn);
652 rtp = mp->m_logop ? gettyp(INT) : ln->tn_type;
653 if (!mp->m_binary && rn != NULL)
655 ntn = mktnode(op, rtp, ln, rn);
659 /* Return if an error occurred. */
663 /* Print a warning if precedence confusion is possible */
668 * Print a warning if one of the operands is in a context where
669 * it is compared with null and if this operand is a constant.
672 if (ln->tn_op == CON ||
673 ((mp->m_binary && op != QUEST) && rn->tn_op == CON)) {
675 /* constant in conditional context */
680 /* Fold if the operator requires it */
682 if (ln->tn_op == CON && (!mp->m_binary || rn->tn_op == CON)) {
685 } else if (isftyp(ntn->tn_type->t_tspec)) {
690 } else if (op == QUEST && ln->tn_op == CON) {
691 ntn = ln->tn_val->v_quad ? rn->tn_left : rn->tn_right;
699 * Perform class conversions.
701 * Arrays of type T are converted into pointers to type T.
702 * Functions are converted to pointers to functions.
703 * Lvalues are converted to rvalues.
711 * Array-lvalue (array of type T) is converted into rvalue
712 * (pointer to type T)
714 if (tn->tn_type->t_tspec == ARRAY) {
715 if (!tn->tn_lvalue) {
716 /* %soperand of '%s' must be lvalue */
717 /* XXX print correct operator */
718 (void)gnuism(114, "", modtab[AMPER].m_name);
720 tn = mktnode(AMPER, tincref(tn->tn_type->t_subt, PTR),
725 * Expression of type function (function with return value of type T)
726 * in rvalue-expression (pointer to function with return value
729 if (tn->tn_type->t_tspec == FUNC)
730 tn = bldamper(tn, 1);
732 /* lvalue to rvalue */
734 tp = tduptyp(tn->tn_type);
735 tp->t_const = tp->t_volatile = 0;
736 tn = mktnode(LOAD, tp, tn, NULL);
743 * Perform most type checks. First the types are checked using
744 * informations from modtab[]. After that it is done by hand for
745 * more complicated operators and type combinations.
747 * If the types are ok, typeok() returns 1, otherwise 0.
750 typeok(op_t op, int arg, tnode_t *ln, tnode_t *rn)
753 tspec_t lt, rt = NOTSPEC, lst = NOTSPEC, rst = NOTSPEC, olt = NOTSPEC,
755 type_t *ltp, *rtp = NULL, *lstp = NULL, *rstp = NULL;
760 if ((lt = (ltp = ln->tn_type)->t_tspec) == PTR)
761 lst = (lstp = ltp->t_subt)->t_tspec;
763 if ((rt = (rtp = rn->tn_type)->t_tspec) == PTR)
764 rst = (rstp = rtp->t_subt)->t_tspec;
768 /* integertypes required */
769 if (!isityp(lt) || (mp->m_binary && !isityp(rt))) {
770 incompat(op, lt, rt);
773 } else if (mp->m_rqsclt) {
774 /* scalar types required */
775 if (!issclt(lt) || (mp->m_binary && !issclt(rt))) {
776 incompat(op, lt, rt);
779 } else if (mp->m_rqatyp) {
780 /* arithmetic types required */
781 if (!isatyp(lt) || (mp->m_binary && !isatyp(rt))) {
782 incompat(op, lt, rt);
787 if (op == SHL || op == SHR || op == SHLASS || op == SHRASS) {
789 * For these operations we need the types before promotion
792 for (tn=ln; tn->tn_op==CVT && !tn->tn_cast; tn=tn->tn_left)
794 olt = tn->tn_type->t_tspec;
795 for (tn=rn; tn->tn_op==CVT && !tn->tn_cast; tn=tn->tn_left)
797 ort = tn->tn_type->t_tspec;
803 * Most errors required by ANSI C are reported in strmemb().
804 * Here we only must check for totaly wrong things.
806 if (lt == FUNC || lt == VOID || ltp->t_isfield ||
807 ((lt != STRUCT && lt != UNION) && !ln->tn_lvalue)) {
808 /* Without tflag we got already an error */
810 /* unacceptable operand of %s */
811 error(111, mp->m_name);
814 /* Now we have an object we can create a pointer to */
817 if (lt != PTR && !(tflag && isityp(lt))) {
818 /* Without tflag we got already an error */
820 /* unacceptabel operand of %s */
821 error(111, mp->m_name);
829 /* operands have scalar types (checked above) */
830 if (!ln->tn_lvalue) {
831 if (ln->tn_op == CVT && ln->tn_cast &&
832 ln->tn_left->tn_op == LOAD) {
833 /* a cast does not yield an lvalue */
836 /* %soperand of %s must be lvalue */
837 error(114, "", mp->m_name);
839 } else if (ltp->t_const) {
840 /* %soperand of %s must be modifiable lvalue */
842 warning(115, "", mp->m_name);
846 if (lt == ARRAY || lt == FUNC) {
847 /* ok, a warning comes later (in bldamper()) */
848 } else if (!ln->tn_lvalue) {
849 if (ln->tn_op == CVT && ln->tn_cast &&
850 ln->tn_left->tn_op == LOAD) {
851 /* a cast does not yield an lvalue */
854 /* %soperand of %s must be lvalue */
855 error(114, "", mp->m_name);
857 } else if (issclt(lt)) {
858 if (ltp->t_isfield) {
859 /* cannot take address of bit-field */
863 } else if (lt != STRUCT && lt != UNION) {
864 /* unacceptable operand of %s */
865 error(111, mp->m_name);
868 if (ln->tn_op == NAME && ln->tn_sym->s_reg) {
869 /* cannot take address of register %s */
870 error(113, ln->tn_sym->s_name);
875 /* until now there were no type checks for this operator */
877 /* cannot dereference non-pointer type */
883 /* operands have scalar types (checked above) */
884 if ((lt == PTR && !isityp(rt)) || (rt == PTR && !isityp(lt))) {
885 incompat(op, lt, rt);
890 /* operands have scalar types (checked above) */
891 if (lt == PTR && (!isityp(rt) && rt != PTR)) {
892 incompat(op, lt, rt);
894 } else if (rt == PTR && lt != PTR) {
895 incompat(op, lt, rt);
898 if (lt == PTR && rt == PTR) {
899 if (!eqtype(lstp, rstp, 1, 0, NULL)) {
900 /* illegal pointer subtraction */
906 /* operands have integer types (checked above) */
907 if (pflag && !isutyp(lt)) {
909 * The left operand is signed. This means that
910 * the operation is (possibly) nonportable.
912 /* bitwise operation on signed value nonportable */
913 if (ln->tn_op != CON) {
914 /* possibly nonportable */
916 } else if (ln->tn_val->v_quad < 0) {
919 } else if (!tflag && !sflag && !isutyp(olt) && isutyp(ort)) {
921 * The left operand would become unsigned in
925 (ln->tn_op != CON || ln->tn_val->v_quad < 0)) {
926 /* semantics of %s change in ANSI C; use ... */
927 warning(118, mp->m_name);
929 } else if (!tflag && !sflag && !isutyp(olt) && !isutyp(ort) &&
930 psize(lt) < psize(rt)) {
932 * In traditional C the left operand would be extended,
933 * possibly with 1, and then shifted.
936 (ln->tn_op != CON || ln->tn_val->v_quad < 0)) {
937 /* semantics of %s change in ANSI C; use ... */
938 warning(118, mp->m_name);
944 * ANSI C does not perform balancing for shift operations,
945 * but traditional C does. If the width of the right operand
946 * is greather than the width of the left operand, than in
947 * traditional C the left operand would be extendet to the
948 * width of the right operand. For SHL this may result in
951 if (psize(lt) < psize(rt)) {
953 * XXX If both operands are constant make sure
954 * that there is really a differencs between
955 * ANSI C and traditional C.
958 /* semantics of %s change in ANSI C; use ... */
959 warning(118, mp->m_name);
962 if (rn->tn_op == CON) {
963 if (!isutyp(rt) && rn->tn_val->v_quad < 0) {
966 } else if ((uint64_t)rn->tn_val->v_quad == size(lt)) {
967 /* shift equal to size fo object */
969 } else if ((uint64_t)rn->tn_val->v_quad > size(lt)) {
970 /* shift greater than size of object */
978 * Accept some things which are allowed with EQ and NE,
979 * but not with ordered comparisons.
981 if (lt == PTR && ((rt == PTR && rst == VOID) || isityp(rt))) {
982 if (rn->tn_op == CON && rn->tn_val->v_quad == 0)
985 if (rt == PTR && ((lt == PTR && lst == VOID) || isityp(lt))) {
986 if (ln->tn_op == CON && ln->tn_val->v_quad == 0)
994 if ((lt == PTR || rt == PTR) && lt != rt) {
995 if (isityp(lt) || isityp(rt)) {
996 /* illegal comb. of pointer and int., op %s */
997 warning(123, mp->m_name);
999 incompat(op, lt, rt);
1002 } else if (lt == PTR && rt == PTR) {
1003 ptrcmpok(op, ln, rn);
1008 /* first operand must have scalar type, op ? : */
1012 if (rn->tn_op != COLON)
1013 lerror("typeok() 2");
1017 if (isatyp(lt) && isatyp(rt))
1020 if (lt == STRUCT && rt == STRUCT && ltp->t_str == rtp->t_str)
1022 if (lt == UNION && rt == UNION && ltp->t_str == rtp->t_str)
1025 /* combination of any pointer and 0, 0L or (void *)0 is ok */
1026 if (lt == PTR && ((rt == PTR && rst == VOID) || isityp(rt))) {
1027 if (rn->tn_op == CON && rn->tn_val->v_quad == 0)
1030 if (rt == PTR && ((lt == PTR && lst == VOID) || isityp(lt))) {
1031 if (ln->tn_op == CON && ln->tn_val->v_quad == 0)
1035 if ((lt == PTR && isityp(rt)) || (isityp(lt) && rt == PTR)) {
1036 /* illegal comb. of ptr. and int., op %s */
1037 warning(123, mp->m_name);
1041 if (lt == VOID || rt == VOID) {
1042 if (lt != VOID || rt != VOID)
1043 /* incompatible types in conditional */
1048 if (lt == PTR && rt == PTR && ((lst == VOID && rst == FUNC) ||
1049 (lst == FUNC && rst == VOID))) {
1050 /* (void *)0 handled above */
1052 /* ANSI C forbids conv. of %s to %s, op %s */
1053 warning(305, "function pointer", "'void *'",
1058 if (rt == PTR && lt == PTR) {
1059 if (!eqtype(lstp, rstp, 1, 0, NULL))
1060 illptrc(mp, ltp, rtp);
1064 /* incompatible types in conditional */
1072 if (!asgntypok(op, arg, ln, rn))
1081 /* operands have scalar types (checked above) */
1082 if ((lt == PTR && !isityp(rt)) || rt == PTR) {
1083 incompat(op, lt, rt);
1090 if (pflag && !isutyp(lt) && !(tflag && isutyp(rt))) {
1091 /* bitwise operation on s.v. possibly nonportabel */
1100 if (!ln->tn_lvalue) {
1101 if (ln->tn_op == CVT && ln->tn_cast &&
1102 ln->tn_left->tn_op == LOAD) {
1103 /* a cast does not yield an lvalue */
1106 /* %soperand of %s must be lvalue */
1107 error(114, "left ", mp->m_name);
1109 } else if (ltp->t_const || ((lt == STRUCT || lt == UNION) &&
1111 /* %soperand of %s must be modifiable lvalue */
1113 warning(115, "left ", mp->m_name);
1117 if (!modtab[ln->tn_op].m_sideeff)
1120 /* LINTED (enumeration values not handled in switch) */
1150 (ltp->t_isenum || (mp->m_binary && rtp->t_isenum))) {
1151 chkbeop(op, ln, rn);
1152 } else if (mp->m_enumop && (ltp->t_isenum && rtp->t_isenum)) {
1153 chkeop2(op, arg, ln, rn);
1154 } else if (mp->m_enumop && (ltp->t_isenum || rtp->t_isenum)) {
1155 chkeop1(op, arg, ln, rn);
1162 ptrcmpok(op_t op, tnode_t *ln, tnode_t *rn)
1166 const char *lts, *rts;
1168 lt = (ltp = ln->tn_type)->t_subt->t_tspec;
1169 rt = (rtp = rn->tn_type)->t_subt->t_tspec;
1171 if (lt == VOID || rt == VOID) {
1172 if (sflag && (lt == FUNC || rt == FUNC)) {
1173 /* (void *)0 already handled in typeok() */
1174 *(lt == FUNC ? <s : &rts) = "function pointer";
1175 *(lt == VOID ? <s : &rts) = "'void *'";
1176 /* ANSI C forbids comparison of %s with %s */
1177 warning(274, lts, rts);
1182 if (!eqtype(ltp->t_subt, rtp->t_subt, 1, 0, NULL)) {
1183 illptrc(&modtab[op], ltp, rtp);
1187 if (lt == FUNC && rt == FUNC) {
1188 if (sflag && op != EQ && op != NE)
1189 /* ANSI C forbids ordered comp. of func ptr */
1195 * Checks type compatibility for ASSIGN, INIT, FARG and RETURN
1196 * and prints warnings/errors if necessary.
1197 * If the types are (almost) compatible, 1 is returned, otherwise 0.
1200 asgntypok(op_t op, int arg, tnode_t *ln, tnode_t *rn)
1202 tspec_t lt, rt, lst = NOTSPEC, rst = NOTSPEC;
1203 type_t *ltp, *rtp, *lstp = NULL, *rstp = NULL;
1205 const char *lts, *rts;
1207 if ((lt = (ltp = ln->tn_type)->t_tspec) == PTR)
1208 lst = (lstp = ltp->t_subt)->t_tspec;
1209 if ((rt = (rtp = rn->tn_type)->t_tspec) == PTR)
1210 rst = (rstp = rtp->t_subt)->t_tspec;
1213 if (isatyp(lt) && isatyp(rt))
1216 if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION))
1217 /* both are struct or union */
1218 return (ltp->t_str == rtp->t_str);
1220 /* 0, 0L and (void *)0 may be assigned to any pointer */
1221 if (lt == PTR && ((rt == PTR && rst == VOID) || isityp(rt))) {
1222 if (rn->tn_op == CON && rn->tn_val->v_quad == 0)
1226 if (lt == PTR && rt == PTR && (lst == VOID || rst == VOID)) {
1227 /* two pointers, at least one pointer to void */
1228 if (sflag && (lst == FUNC || rst == FUNC)) {
1229 /* comb. of ptr to func and ptr to void */
1230 *(lst == FUNC ? <s : &rts) = "function pointer";
1231 *(lst == VOID ? <s : &rts) = "'void *'";
1235 /* ANSI C forbids conversion of %s to %s */
1236 warning(303, rts, lts);
1239 /* ANSI C forbids conv. of %s to %s, arg #%d */
1240 warning(304, rts, lts, arg);
1243 /* ANSI C forbids conv. of %s to %s, op %s */
1244 warning(305, rts, lts, mp->m_name);
1250 if (lt == PTR && rt == PTR && (lst == VOID || rst == VOID ||
1251 eqtype(lstp, rstp, 1, 0, NULL))) {
1252 /* compatible pointer types (qualifiers ignored) */
1254 ((!lstp->t_const && rstp->t_const) ||
1255 (!lstp->t_volatile && rstp->t_volatile))) {
1256 /* left side has not all qualifiers of right */
1260 /* incompatible pointer types */
1264 /* argument has incompat. ptr. type, arg #%d */
1268 /* operands have incompat. ptr. types, op %s */
1269 warning(128, mp->m_name);
1276 if ((lt == PTR && isityp(rt)) || (isityp(lt) && rt == PTR)) {
1280 /* illegal combination of pointer and integer */
1284 /* illegal comb. of ptr. and int., arg #%d */
1288 /* illegal comb. of ptr. and int., op %s */
1289 warning(123, mp->m_name);
1295 if (lt == PTR && rt == PTR) {
1299 illptrc(NULL, ltp, rtp);
1302 /* argument has incompatible pointer type, arg #%d */
1306 illptrc(mp, ltp, rtp);
1314 /* initialisation type mismatch */
1318 /* return value type mismatch */
1322 /* argument is incompatible with prototype, arg #%d */
1326 incompat(op, lt, rt);
1334 * Prints a warning if an operator, which should be senseless for an
1335 * enum type, is applied to an enum type.
1338 chkbeop(op_t op, tnode_t *ln, tnode_t *rn)
1347 if (!(ln->tn_type->t_isenum ||
1348 (mp->m_binary && rn->tn_type->t_isenum))) {
1353 * Enum as offset to a pointer is an exception (otherwise enums
1354 * could not be used as array indizes).
1357 ((ln->tn_type->t_isenum && rn->tn_type->t_tspec == PTR) ||
1358 (rn->tn_type->t_isenum && ln->tn_type->t_tspec == PTR))) {
1362 /* dubious operation on enum, op %s */
1363 warning(241, mp->m_name);
1368 * Prints a warning if an operator is applied to two different enum types.
1371 chkeop2(op_t op, int arg, tnode_t *ln, tnode_t *rn)
1377 if (ln->tn_type->t_enum != rn->tn_type->t_enum) {
1380 /* enum type mismatch in initialisation */
1384 /* enum type mismatch, arg #%d */
1388 /* return value type mismatch */
1392 /* enum type mismatch, op %s */
1393 warning(130, mp->m_name);
1397 } else if (mp->m_comp && op != EQ && op != NE) {
1399 /* dubious comparisons of enums */
1400 warning(243, mp->m_name);
1406 * Prints a warning if an operator has both enum end other integer
1410 chkeop1(op_t op, int arg, tnode_t *ln, tnode_t *rn)
1419 * Initializations with 0 should be allowed. Otherwise,
1420 * we should complain about all uninitialized enums,
1423 if (!rn->tn_type->t_isenum && rn->tn_op == CON &&
1424 isityp(rn->tn_type->t_tspec) && rn->tn_val->v_quad == 0) {
1427 /* initialisation of '%s' with '%s' */
1428 warning(277, tyname(ln->tn_type), tyname(rn->tn_type));
1431 /* combination of '%s' and '%s', arg #%d */
1432 warning(278, tyname(ln->tn_type), tyname(rn->tn_type), arg);
1435 /* combination of '%s' and '%s' in return */
1436 warning(279, tyname(ln->tn_type), tyname(rn->tn_type));
1439 /* combination of '%s' and %s, op %s */
1440 warning(242, tyname(ln->tn_type), tyname(rn->tn_type),
1447 * Build and initialize a new node.
1450 mktnode(op_t op, type_t *type, tnode_t *ln, tnode_t *rn)
1458 ntn->tn_type = type;
1462 if (op == STAR || op == FSEL) {
1463 if (ln->tn_type->t_tspec == PTR) {
1464 t = ln->tn_type->t_subt->t_tspec;
1465 if (t != FUNC && t != VOID)
1468 lerror("mktnode() 2");
1476 * Performs usual conversion of operands to (unsigned) int.
1478 * If tflag is set or the operand is a function argument with no
1479 * type information (no prototype or variable # of args), convert
1483 promote(op_t op, int farg, tnode_t *tn)
1489 t = tn->tn_type->t_tspec;
1496 * ANSI C requires that the result is always of type INT
1497 * if INT can represent all possible values of the previous
1500 if (tn->tn_type->t_isfield) {
1501 len = tn->tn_type->t_flen;
1502 if (size(INT) > len) {
1505 if (size(INT) != len)
1506 lerror("promote() 1");
1513 } else if (t == CHAR || t == UCHAR || t == SCHAR) {
1514 t = (size(CHAR) < size(INT) || t != UCHAR) ?
1516 } else if (t == SHORT || t == USHORT) {
1517 t = (size(SHORT) < size(INT) || t == SHORT) ?
1519 } else if (t == ENUM) {
1521 } else if (farg && t == FLOAT) {
1526 * In traditional C, keep unsigned and promote FLOAT
1529 if (t == UCHAR || t == USHORT) {
1531 } else if (t == CHAR || t == SCHAR || t == SHORT) {
1533 } else if (t == FLOAT) {
1535 } else if (t == ENUM) {
1540 if (t != tn->tn_type->t_tspec) {
1541 ntp = tduptyp(tn->tn_type);
1544 * Keep t_isenum so we are later able to check compatibility
1547 tn = convert(op, 0, ntp, tn);
1554 * Insert conversions which are necessary to give both operands the same
1555 * type. This is done in different ways for traditional C and ANIS C.
1558 balance(op_t op, tnode_t **lnp, tnode_t **rnp)
1563 static tspec_t tl[] = {
1564 LDOUBLE, DOUBLE, FLOAT, UQUAD, QUAD, ULONG, LONG, UINT, INT,
1567 lt = (*lnp)->tn_type->t_tspec;
1568 rt = (*rnp)->tn_type->t_tspec;
1570 if (!isatyp(lt) || !isatyp(rt))
1576 } else if (lt == LDOUBLE || rt == LDOUBLE) {
1578 } else if (lt == DOUBLE || rt == DOUBLE) {
1580 } else if (lt == FLOAT || rt == FLOAT) {
1584 * If type A has more bits than type B it should
1585 * be able to hold all possible values of type B.
1587 if (size(lt) > size(rt)) {
1589 } else if (size(lt) < size(rt)) {
1592 for (i = 3; tl[i] != INT; i++) {
1593 if (tl[i] == lt || tl[i] == rt)
1596 if ((isutyp(lt) || isutyp(rt)) &&
1604 /* Keep unsigned in traditional C */
1605 u = isutyp(lt) || isutyp(rt);
1606 for (i = 0; tl[i] != INT; i++) {
1607 if (lt == tl[i] || rt == tl[i])
1611 if (u && isityp(t) && !isutyp(t))
1616 ntp = tduptyp((*lnp)->tn_type);
1618 *lnp = convert(op, 0, ntp, *lnp);
1621 ntp = tduptyp((*rnp)->tn_type);
1623 *rnp = convert(op, 0, ntp, *rnp);
1628 * Insert a conversion operator, which converts the type of the node
1629 * to another given type.
1630 * If op is FARG, arg is the number of the argument (used for warnings).
1633 convert(op_t op, int arg, type_t *tp, tnode_t *tn)
1636 tspec_t nt, ot, ost = NOTSPEC;
1639 lerror("convert() 1");
1642 if ((ot = tn->tn_type->t_tspec) == PTR)
1643 ost = tn->tn_type->t_subt->t_tspec;
1645 if (!tflag && !sflag && op == FARG)
1646 ptconv(arg, nt, ot, tp, tn);
1647 if (isityp(nt) && isityp(ot)) {
1648 iiconv(op, arg, nt, ot, tp, tn);
1649 } else if (nt == PTR && ((ot == PTR && ost == VOID) || isityp(ot)) &&
1650 tn->tn_op == CON && tn->tn_val->v_quad == 0) {
1651 /* 0, 0L and (void *)0 may be assigned to any pointer. */
1652 } else if (isityp(nt) && ot == PTR) {
1653 piconv(op, nt, tp, tn);
1654 } else if (nt == PTR && ot == PTR) {
1661 ntn->tn_cast = op == CVT;
1662 if (tn->tn_op != CON || nt == VOID) {
1666 ntn->tn_val = tgetblk(sizeof (val_t));
1667 cvtcon(op, arg, ntn->tn_type, ntn->tn_val, tn->tn_val);
1674 * Print a warning if a prototype causes a type conversion that is
1675 * different from what would happen to the same argument in the
1676 * absence of a prototype.
1678 * Errors/Warnings about illegal type combinations are already printed
1682 ptconv(int arg, tspec_t nt, tspec_t ot, type_t *tp, tnode_t *tn)
1686 if (!isatyp(nt) || !isatyp(ot))
1690 * If the type of the formal parameter is char/short, a warning
1691 * would be useless, because functions declared the old style
1692 * can't expect char/short arguments.
1694 if (nt == CHAR || nt == UCHAR || nt == SHORT || nt == USHORT)
1697 /* get default promotion */
1698 ptn = promote(NOOP, 1, tn);
1699 ot = ptn->tn_type->t_tspec;
1701 /* return if types are the same with and without prototype */
1702 if (nt == ot || (nt == ENUM && ot == INT))
1705 if (isftyp(nt) != isftyp(ot) || psize(nt) != psize(ot)) {
1706 /* representation and/or width change */
1707 if (styp(nt) != SHORT || !isityp(ot) || psize(ot) > psize(INT))
1708 /* conversion to '%s' due to prototype, arg #%d */
1709 warning(259, tyname(tp), arg);
1712 * they differ in sign or base type (char, short, int,
1713 * long, long long, float, double, long double)
1715 * if they differ only in sign and the argument is a constant
1716 * and the msb of the argument is not set, print no warning
1718 if (ptn->tn_op == CON && isityp(nt) && styp(nt) == styp(ot) &&
1719 msb(ptn->tn_val->v_quad, ot, -1) == 0) {
1722 /* conversion to '%s' due to prototype, arg #%d */
1723 warning(259, tyname(tp), arg);
1729 * Print warnings for conversions of integer types which my cause
1734 iiconv(op_t op, int arg, tspec_t nt, tspec_t ot, type_t *tp, tnode_t *tn)
1736 if (tn->tn_op == CON)
1743 if (psize(nt) > psize(ot) && isutyp(nt) != isutyp(ot)) {
1744 /* conversion to %s may sign-extend incorrectly (, arg #%d) */
1745 if (aflag && pflag) {
1747 warning(297, tyname(tp), arg);
1749 warning(131, tyname(tp));
1755 if (psize(nt) < psize(ot) &&
1756 (ot == LONG || ot == ULONG || ot == QUAD || ot == UQUAD ||
1758 /* conversion from '%s' may lose accuracy */
1761 warning(298, tyname(tn->tn_type), arg);
1763 warning(132, tyname(tn->tn_type));
1770 * Print warnings for dubious conversions of pointer to integer.
1773 piconv(op_t op, tspec_t nt, type_t *tp, tnode_t *tn)
1776 if (tn->tn_op == CON)
1780 /* We got already an error. */
1784 if (psize(nt) < psize(PTR)) {
1785 if (pflag && size(nt) >= size(PTR)) {
1786 /* conv. of pointer to %s may lose bits */
1787 warning(134, tyname(tp));
1789 /* conv. of pointer to %s loses bits */
1790 warning(133, tyname(tp));
1796 * Print warnings for questionable pointer conversions.
1799 ppconv(op_t op, tnode_t *tn, type_t *tp)
1802 const char *nts, *ots;
1805 * We got already an error (pointers of different types
1806 * without a cast) or we will not get a warning.
1811 nt = tp->t_subt->t_tspec;
1812 ot = tn->tn_type->t_subt->t_tspec;
1814 if (nt == VOID || ot == VOID) {
1815 if (sflag && (nt == FUNC || ot == FUNC)) {
1816 /* (void *)0 already handled in convert() */
1817 *(nt == FUNC ? &nts : &ots) = "function pointer";
1818 *(nt == VOID ? &nts : &ots) = "'void *'";
1819 /* ANSI C forbids conversion of %s to %s */
1820 warning(303, ots, nts);
1823 } else if (nt == FUNC && ot == FUNC) {
1825 } else if (nt == FUNC || ot == FUNC) {
1826 /* questionable conversion of function pointer */
1831 if (getbound(tp->t_subt) > getbound(tn->tn_type->t_subt)) {
1833 /* possible pointer alignment problem */
1836 if (((nt == STRUCT || nt == UNION) &&
1837 tp->t_subt->t_str != tn->tn_type->t_subt->t_str) ||
1838 psize(nt) != psize(ot)) {
1840 /* pointer casts may be troublesome */
1847 * Converts a typed constant in a constant of another type.
1849 * op operator which requires conversion
1850 * arg if op is FARG, # of argument
1851 * tp type in which to convert the constant
1856 cvtcon(op_t op, int arg, type_t *tp, val_t *nv, val_t *v)
1859 ldbl_t max = 0.0, min = 0.0;
1861 int64_t xmask, xmsk1;
1865 nt = nv->v_tspec = tp->t_tspec;
1868 if (ot == FLOAT || ot == DOUBLE || ot == LDOUBLE) {
1871 max = CHAR_MAX; min = CHAR_MIN; break;
1873 max = UCHAR_MAX; min = 0; break;
1875 max = SCHAR_MAX; min = SCHAR_MIN; break;
1877 max = SHRT_MAX; min = SHRT_MIN; break;
1879 max = USHRT_MAX; min = 0; break;
1882 max = INT_MAX; min = INT_MIN; break;
1884 max = (u_int)UINT_MAX; min = 0; break;
1886 max = LONG_MAX; min = LONG_MIN; break;
1888 max = (u_long)ULONG_MAX; min = 0; break;
1890 max = QUAD_MAX; min = QUAD_MIN; break;
1892 max = (uint64_t)UQUAD_MAX; min = 0; break;
1894 max = FLT_MAX; min = -FLT_MAX; break;
1896 max = DBL_MAX; min = -DBL_MAX; break;
1898 /* Got already an error because of float --> ptr */
1900 max = LDBL_MAX; min = -LDBL_MAX; break;
1902 lerror("cvtcon() 1");
1904 if (v->v_ldbl > max || v->v_ldbl < min) {
1906 lerror("cvtcon() 2");
1908 /* conv. of %s to %s is out of rng., arg #%d */
1909 warning(295, tyname(gettyp(ot)), tyname(tp),
1912 /* conversion of %s to %s is out of range */
1913 warning(119, tyname(gettyp(ot)), tyname(tp));
1915 v->v_ldbl = v->v_ldbl > 0 ? max : min;
1918 nv->v_ldbl = (float)v->v_ldbl;
1919 } else if (nt == DOUBLE) {
1920 nv->v_ldbl = (double)v->v_ldbl;
1921 } else if (nt == LDOUBLE) {
1922 nv->v_ldbl = v->v_ldbl;
1924 nv->v_quad = (nt == PTR || isutyp(nt)) ?
1925 (uint64_t)v->v_ldbl : (int64_t)v->v_ldbl;
1929 nv->v_ldbl = (ot == PTR || isutyp(ot)) ?
1930 (float)(uint64_t)v->v_quad : (float)v->v_quad;
1931 } else if (nt == DOUBLE) {
1932 nv->v_ldbl = (ot == PTR || isutyp(ot)) ?
1933 (double)(uint64_t)v->v_quad : (double)v->v_quad;
1934 } else if (nt == LDOUBLE) {
1935 nv->v_ldbl = (ot == PTR || isutyp(ot)) ?
1936 (ldbl_t)(uint64_t)v->v_quad : (ldbl_t)v->v_quad;
1938 rchk = 1; /* Check for lost precision. */
1939 nv->v_quad = v->v_quad;
1943 if (v->v_ansiu && isftyp(nt)) {
1944 /* ANSI C treats constant as unsigned */
1947 } else if (v->v_ansiu && (isityp(nt) && !isutyp(nt) &&
1948 psize(nt) > psize(ot))) {
1949 /* ANSI C treats constant as unsigned */
1954 if (nt != FLOAT && nt != DOUBLE && nt != LDOUBLE) {
1955 sz = tp->t_isfield ? tp->t_flen : size(nt);
1956 nv->v_quad = xsign(nv->v_quad, nt, sz);
1959 if (rchk && op != CVT) {
1961 nsz = tp->t_isfield ? tp->t_flen : size(nt);
1962 xmask = qlmasks[nsz] ^ qlmasks[osz];
1963 xmsk1 = qlmasks[nsz] ^ qlmasks[osz - 1];
1965 * For bitwise operations we are not interested in the
1966 * value, but in the bits itself.
1968 if (op == ORASS || op == OR || op == XOR) {
1970 * Print a warning if bits which were set are
1971 * lost due to the conversion.
1972 * This can happen with operator ORASS only.
1974 if (nsz < osz && (v->v_quad & xmask) != 0) {
1975 /* constant truncated by conv., op %s */
1976 warning(306, modtab[op].m_name);
1978 } else if (op == ANDASS || op == AND) {
1980 * Print a warning if additional bits are not all 1
1981 * and the most significant bit of the old value is 1,
1982 * or if at least one (but not all) removed bit was 0.
1985 (nv->v_quad & qbmasks[osz - 1]) != 0 &&
1986 (nv->v_quad & xmask) != xmask) {
1988 * extra bits set to 0 in conversion
1989 * of '%s' to '%s', op %s
1991 warning(309, tyname(gettyp(ot)),
1992 tyname(tp), modtab[op].m_name);
1993 } else if (nsz < osz &&
1994 (v->v_quad & xmask) != xmask &&
1995 (v->v_quad & xmask) != 0) {
1996 /* const. truncated by conv., op %s */
1997 warning(306, modtab[op].m_name);
1999 } else if ((nt != PTR && isutyp(nt)) &&
2000 (ot != PTR && !isutyp(ot)) && v->v_quad < 0) {
2002 /* assignment of negative constant to ... */
2004 } else if (op == INIT) {
2005 /* initialisation of unsigned with neg. ... */
2007 } else if (op == FARG) {
2008 /* conversion of neg. const. to ..., arg #%d */
2010 } else if (modtab[op].m_comp) {
2011 /* we get this warning already in chkcomp() */
2013 /* conversion of negative constant to ... */
2016 } else if (nv->v_quad != v->v_quad && nsz <= osz &&
2017 (v->v_quad & xmask) != 0 &&
2018 (isutyp(ot) || (v->v_quad & xmsk1) != xmsk1)) {
2020 * Loss of significant bit(s). All truncated bits
2021 * of unsigned types or all truncated bits plus the
2022 * msb of the target for signed types are considered
2023 * to be significant bits. Loss of significant bits
2024 * means that at least on of the bits was set in an
2025 * unsigned type or that at least one, but not all of
2026 * the bits was set in a signed type.
2027 * Loss of significant bits means that it is not
2028 * possible, also not with necessary casts, to convert
2029 * back to the original type. An example for a
2030 * necessary cast is:
2031 * char c; int i; c = 128;
2032 * i = c; ** yields -128 **
2033 * i = (unsigned char)c; ** yields 128 **
2035 if (op == ASSIGN && tp->t_isfield) {
2036 /* precision lost in bit-field assignment */
2038 } else if (op == ASSIGN) {
2039 /* constant truncated by assignment */
2041 } else if (op == INIT && tp->t_isfield) {
2042 /* bit-field initializer does not fit */
2044 } else if (op == INIT) {
2045 /* initializer does not fit */
2047 } else if (op == CASE) {
2048 /* case label affected by conversion */
2050 } else if (op == FARG) {
2051 /* conv. of %s to %s is out of rng., arg #%d */
2052 warning(295, tyname(gettyp(ot)), tyname(tp),
2055 /* conversion of %s to %s is out of range */
2056 warning(119, tyname(gettyp(ot)), tyname(tp));
2058 } else if (nv->v_quad != v->v_quad) {
2059 if (op == ASSIGN && tp->t_isfield) {
2060 /* precision lost in bit-field assignment */
2062 } else if (op == INIT && tp->t_isfield) {
2063 /* bit-field initializer out of range */
2065 } else if (op == CASE) {
2066 /* case label affected by conversion */
2068 } else if (op == FARG) {
2069 /* conv. of %s to %s is out of rng., arg #%d */
2070 warning(295, tyname(gettyp(ot)), tyname(tp),
2073 /* conversion of %s to %s is out of range */
2074 warning(119, tyname(gettyp(ot)), tyname(tp));
2081 * Called if incompatible types were detected.
2082 * Prints an appropriate warning.
2085 incompat(op_t op, tspec_t lt, tspec_t rt)
2091 if (lt == VOID || (mp->m_binary && rt == VOID)) {
2092 /* void type illegal in expression */
2094 } else if (op == ASSIGN) {
2095 if ((lt == STRUCT || lt == UNION) &&
2096 (rt == STRUCT || rt == UNION)) {
2097 /* assignment of different structures */
2100 /* assignment type mismatch */
2103 } else if (mp->m_binary) {
2104 /* operands of %s have incompatible types */
2105 error(107, mp->m_name);
2107 /* operand of %s has incompatible type */
2108 error(108, mp->m_name);
2113 * Called if incompatible pointer types are detected.
2114 * Print an appropriate warning.
2117 illptrc(mod_t *mp, type_t *ltp, type_t *rtp)
2121 if (ltp->t_tspec != PTR || rtp->t_tspec != PTR)
2122 lerror("illptrc() 1");
2124 lt = ltp->t_subt->t_tspec;
2125 rt = rtp->t_subt->t_tspec;
2127 if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION)) {
2129 /* illegal structure pointer combination */
2132 /* illegal structure pointer combination, op %s */
2133 warning(245, mp->m_name);
2137 /* illegal pointer combination */
2140 /* illegal pointer combination, op %s */
2141 warning(124, mp->m_name);
2147 * Make sure type (*tpp)->t_subt has at least the qualifiers
2148 * of tp1->t_subt and tp2->t_subt.
2151 mrgqual(type_t **tpp, type_t *tp1, type_t *tp2)
2154 if ((*tpp)->t_tspec != PTR ||
2155 tp1->t_tspec != PTR || tp2->t_tspec != PTR) {
2156 lerror("mrgqual()");
2159 if ((*tpp)->t_subt->t_const ==
2160 (tp1->t_subt->t_const | tp2->t_subt->t_const) &&
2161 (*tpp)->t_subt->t_volatile ==
2162 (tp1->t_subt->t_volatile | tp2->t_subt->t_volatile)) {
2166 *tpp = tduptyp(*tpp);
2167 (*tpp)->t_subt = tduptyp((*tpp)->t_subt);
2168 (*tpp)->t_subt->t_const =
2169 tp1->t_subt->t_const | tp2->t_subt->t_const;
2170 (*tpp)->t_subt->t_volatile =
2171 tp1->t_subt->t_volatile | tp2->t_subt->t_volatile;
2175 * Returns 1 if the given structure or union has a constant member
2176 * (maybe recursively).
2184 if ((t = tp->t_tspec) != STRUCT && t != UNION)
2185 lerror("conmemb()");
2186 for (m = tp->t_str->memb; m != NULL; m = m->s_nxt) {
2190 if ((t = tp->t_tspec) == STRUCT || t == UNION) {
2191 if (conmemb(m->s_type))
2204 if ((t = tp->t_tspec) == INT && tp->t_isenum)
2208 case CHAR: s = "char"; break;
2209 case UCHAR: s = "unsigned char"; break;
2210 case SCHAR: s = "signed char"; break;
2211 case SHORT: s = "short"; break;
2212 case USHORT: s = "unsigned short"; break;
2213 case INT: s = "int"; break;
2214 case UINT: s = "unsigned int"; break;
2215 case LONG: s = "long"; break;
2216 case ULONG: s = "unsigned long"; break;
2217 case QUAD: s = "long long"; break;
2218 case UQUAD: s = "unsigned long long"; break;
2219 case FLOAT: s = "float"; break;
2220 case DOUBLE: s = "double"; break;
2221 case LDOUBLE: s = "long double"; break;
2222 case PTR: s = "pointer"; break;
2223 case ENUM: s = "enum"; break;
2224 case STRUCT: s = "struct"; break;
2225 case UNION: s = "union"; break;
2226 case FUNC: s = "function"; break;
2227 case ARRAY: s = "array"; break;
2235 * Create a new node for one of the operators POINT and ARROW.
2238 bldstr(op_t op, tnode_t *ln, tnode_t *rn)
2243 if (rn->tn_op != NAME)
2244 lerror("bldstr() 1");
2245 if (rn->tn_sym->s_value.v_tspec != INT)
2246 lerror("bldstr() 2");
2247 if (rn->tn_sym->s_scl != MOS && rn->tn_sym->s_scl != MOU)
2248 lerror("bldstr() 3");
2251 * Remember if the left operand is an lvalue (structure members
2252 * are lvalues if and only if the structure itself is an lvalue).
2254 nolval = op == POINT && !ln->tn_lvalue;
2257 ln = bldamper(ln, 1);
2258 } else if (ln->tn_type->t_tspec != PTR) {
2259 if (!tflag || !isityp(ln->tn_type->t_tspec))
2260 lerror("bldstr() 4");
2261 ln = convert(NOOP, 0, tincref(gettyp(VOID), PTR), ln);
2265 ctn = getinode(LONG, rn->tn_sym->s_value.v_quad / CHAR_BIT);
2267 ctn = getinode(INT, rn->tn_sym->s_value.v_quad / CHAR_BIT);
2270 ntn = mktnode(PLUS, tincref(rn->tn_type, PTR), ln, ctn);
2271 if (ln->tn_op == CON)
2274 if (rn->tn_type->t_isfield) {
2275 ntn = mktnode(FSEL, ntn->tn_type->t_subt, ntn, NULL);
2277 ntn = mktnode(STAR, ntn->tn_type->t_subt, ntn, NULL);
2287 * Create a node for INCAFT, INCBEF, DECAFT and DECBEF.
2290 bldincdec(op_t op, tnode_t *ln)
2295 lerror("bldincdec() 1");
2297 if (ln->tn_type->t_tspec == PTR) {
2298 cn = plength(ln->tn_type);
2300 cn = getinode(INT, (int64_t)1);
2302 ntn = mktnode(op, ln->tn_type, ln, cn);
2308 * Create a tree node for the & operator
2311 bldamper(tnode_t *tn, int noign)
2316 if (!noign && ((t = tn->tn_type->t_tspec) == ARRAY || t == FUNC)) {
2317 /* & before array or function: ignored */
2324 if (tn->tn_op == STAR &&
2325 tn->tn_left->tn_type->t_tspec == PTR &&
2326 tn->tn_left->tn_type->t_subt == tn->tn_type) {
2327 return (tn->tn_left);
2330 ntn = mktnode(AMPER, tincref(tn->tn_type, PTR), tn, NULL);
2336 * Create a node for operators PLUS and MINUS.
2339 bldplmi(op_t op, tnode_t *ln, tnode_t *rn)
2344 /* If pointer and integer, then pointer to the lhs. */
2345 if (rn->tn_type->t_tspec == PTR && isityp(ln->tn_type->t_tspec)) {
2351 if (ln->tn_type->t_tspec == PTR && rn->tn_type->t_tspec != PTR) {
2353 if (!isityp(rn->tn_type->t_tspec))
2354 lerror("bldplmi() 1");
2356 ctn = plength(ln->tn_type);
2357 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
2358 rn = convert(NOOP, 0, ctn->tn_type, rn);
2359 rn = mktnode(MULT, rn->tn_type, rn, ctn);
2360 if (rn->tn_left->tn_op == CON)
2362 ntn = mktnode(op, ln->tn_type, ln, rn);
2364 } else if (rn->tn_type->t_tspec == PTR) {
2366 if (ln->tn_type->t_tspec != PTR || op != MINUS)
2367 lerror("bldplmi() 2");
2373 ntn = mktnode(op, tp, ln, rn);
2374 if (ln->tn_op == CON && rn->tn_op == CON)
2376 ctn = plength(ln->tn_type);
2377 balance(NOOP, &ntn, &ctn);
2378 ntn = mktnode(DIV, tp, ntn, ctn);
2382 ntn = mktnode(op, ln->tn_type, ln, rn);
2389 * Create a node for operators SHL and SHR.
2392 bldshft(op_t op, tnode_t *ln, tnode_t *rn)
2397 if ((t = rn->tn_type->t_tspec) != INT && t != UINT)
2398 rn = convert(CVT, 0, gettyp(INT), rn);
2399 ntn = mktnode(op, ln->tn_type, ln, rn);
2404 * Create a node for COLON.
2407 bldcol(tnode_t *ln, tnode_t *rn)
2409 tspec_t lt, rt, pdt;
2413 lt = ln->tn_type->t_tspec;
2414 rt = rn->tn_type->t_tspec;
2422 * Arithmetic types are balanced, all other type combinations
2423 * still need to be handled.
2425 if (isatyp(lt) && isatyp(rt)) {
2427 } else if (lt == VOID || rt == VOID) {
2429 } else if (lt == STRUCT || lt == UNION) {
2430 /* Both types must be identical. */
2431 if (rt != STRUCT && rt != UNION)
2432 lerror("bldcol() 1");
2433 if (ln->tn_type->t_str != rn->tn_type->t_str)
2434 lerror("bldcol() 2");
2435 if (incompl(ln->tn_type)) {
2436 /* unknown operand size, op %s */
2437 error(138, modtab[COLON].m_name);
2441 } else if (lt == PTR && isityp(rt)) {
2443 rn = convert(NOOP, 0, gettyp(pdt), rn);
2447 } else if (rt == PTR && isityp(lt)) {
2449 ln = convert(NOOP, 0, gettyp(pdt), ln);
2453 } else if (lt == PTR && ln->tn_type->t_subt->t_tspec == VOID) {
2455 lerror("bldcol() 4");
2457 mrgqual(&rtp, ln->tn_type, rn->tn_type);
2458 } else if (rt == PTR && rn->tn_type->t_subt->t_tspec == VOID) {
2460 lerror("bldcol() 5");
2462 mrgqual(&rtp, ln->tn_type, rn->tn_type);
2464 if (lt != PTR || rt != PTR)
2465 lerror("bldcol() 6");
2467 * XXX For now we simply take the left type. This is
2468 * probably wrong, if one type contains a functionprototype
2469 * and the other one, at the same place, only an old style
2473 mrgqual(&rtp, ln->tn_type, rn->tn_type);
2476 ntn = mktnode(COLON, rtp, ln, rn);
2482 * Create a node for an assignment operator (both = and op= ).
2485 bldasgn(op_t op, tnode_t *ln, tnode_t *rn)
2490 if (ln == NULL || rn == NULL)
2491 lerror("bldasgn() 1");
2493 lt = ln->tn_type->t_tspec;
2494 rt = rn->tn_type->t_tspec;
2496 if ((op == ADDASS || op == SUBASS) && lt == PTR) {
2498 lerror("bldasgn() 2");
2499 ctn = plength(ln->tn_type);
2500 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
2501 rn = convert(NOOP, 0, ctn->tn_type, rn);
2502 rn = mktnode(MULT, rn->tn_type, rn, ctn);
2503 if (rn->tn_left->tn_op == CON)
2507 if ((op == ASSIGN || op == RETURN) && (lt == STRUCT || rt == STRUCT)) {
2508 if (rt != lt || ln->tn_type->t_str != rn->tn_type->t_str)
2509 lerror("bldasgn() 3");
2510 if (incompl(ln->tn_type)) {
2512 /* cannot return incomplete type */
2515 /* unknown operand size, op %s */
2516 error(138, modtab[op].m_name);
2522 if (op == SHLASS || op == SHRASS) {
2524 rn = convert(NOOP, 0, gettyp(INT), rn);
2528 if (op == ASSIGN || lt != PTR) {
2530 (ln->tn_type->t_isfield && rn->tn_op == CON)) {
2531 rn = convert(op, 0, ln->tn_type, rn);
2537 ntn = mktnode(op, ln->tn_type, ln, rn);
2543 * Get length of type tp->t_subt.
2551 if (tp->t_tspec != PTR)
2552 lerror("plength() 1");
2558 while (tp->t_tspec == ARRAY) {
2563 switch (tp->t_tspec) {
2565 /* pointer to function is not allowed here */
2569 /* cannot do pointer arithmetic on operand of ... */
2574 if ((elsz = tp->t_str->size) == 0)
2575 /* cannot do pointer arithmetic on operand of ... */
2580 /* cannot do pointer arithmetic on operand of ... */
2585 if ((elsz = size(tp->t_tspec)) == 0) {
2586 /* cannot do pointer arithmetic on operand of ... */
2588 } else if (elsz == -1) {
2589 lerror("plength() 2");
2594 if (elem == 0 && elsz != 0) {
2595 /* cannot do pointer arithmetic on operand of ... */
2608 return (getinode(st, (int64_t)(elem * elsz / CHAR_BIT)));
2613 * Note: There appear to be a number of bugs in detecting overflow in
2614 * this function. An audit and a set of proper regression tests are needed.
2615 * --Perry Metzger, Nov. 16, 2001
2618 * Do only as much as necessary to compute constant expressions.
2619 * Called only if the operator allows folding and (both) operands
2628 int64_t sl, sr = 0, q = 0, mask;
2629 uint64_t ul, ur = 0;
2632 if ((v = calloc(1, sizeof (val_t))) == NULL)
2634 v->v_tspec = t = tn->tn_type->t_tspec;
2636 utyp = t == PTR || isutyp(t);
2637 ul = sl = tn->tn_left->tn_val->v_quad;
2638 if (modtab[tn->tn_op].m_binary)
2639 ur = sr = tn->tn_right->tn_val->v_quad;
2641 mask = qlmasks[size(t)];
2644 switch (tn->tn_op) {
2650 if (msb(q, t, -1) == msb(sl, t, -1))
2659 if (q != (q & mask))
2661 else if ((ul != 0) && ((q / ul) != ur))
2665 if (msb(q, t, -1) != (msb(sl, t, -1) ^ msb(sr, t, -1)))
2673 q = utyp ? UQUAD_MAX : QUAD_MAX;
2675 q = utyp ? ul / ur : sl / sr;
2684 q = utyp ? ul % ur : sl % sr;
2688 q = utyp ? ul + ur : sl + sr;
2689 if (msb(sl, t, -1) != 0 && msb(sr, t, -1) != 0) {
2690 if (msb(q, t, -1) == 0)
2692 } else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) == 0) {
2693 if (msb(q, t, -1) != 0)
2698 q = utyp ? ul - ur : sl - sr;
2699 if (msb(sl, t, -1) != 0 && msb(sr, t, -1) == 0) {
2700 if (msb(q, t, -1) == 0)
2702 } else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) != 0) {
2703 if (msb(q, t, -1) != 0)
2708 q = utyp ? ul << sr : sl << sr;
2712 * The sign must be explicitly extended because
2713 * shifts of signed values are implementation dependent.
2716 q = xsign(q, t, size(t) - (int)sr);
2719 q = utyp ? ul < ur : sl < sr;
2722 q = utyp ? ul <= ur : sl <= sr;
2725 q = utyp ? ul >= ur : sl >= sr;
2728 q = utyp ? ul > ur : sl > sr;
2731 q = utyp ? ul == ur : sl == sr;
2734 q = utyp ? ul != ur : sl != sr;
2737 q = utyp ? ul & ur : sl & sr;
2740 q = utyp ? ul ^ ur : sl ^ sr;
2743 q = utyp ? ul | ur : sl | sr;
2749 /* XXX does not work for quads. */
2750 if (ovfl || ((q | mask) != ~(uint64_t)0 && (q & ~mask) != 0)) {
2752 /* integer overflow detected, op %s */
2753 warning(141, modtab[tn->tn_op].m_name);
2756 v->v_quad = xsign(q, t, -1);
2758 cn = getcnode(tn->tn_type, v);
2764 * Same for operators whose operands are compared with 0 (test context).
2767 foldtst(tnode_t *tn)
2772 if ((v = calloc(1, sizeof (val_t))) == NULL)
2774 v->v_tspec = tn->tn_type->t_tspec;
2775 if (tn->tn_type->t_tspec != INT)
2776 lerror("foldtst() 1");
2778 if (isftyp(tn->tn_left->tn_type->t_tspec)) {
2779 l = tn->tn_left->tn_val->v_ldbl != 0.0;
2781 l = tn->tn_left->tn_val->v_quad != 0;
2784 if (modtab[tn->tn_op].m_binary) {
2785 if (isftyp(tn->tn_right->tn_type->t_tspec)) {
2786 r = tn->tn_right->tn_val->v_ldbl != 0.0;
2788 r = tn->tn_right->tn_val->v_quad != 0;
2792 switch (tn->tn_op) {
2795 /* constant argument to NOT */
2806 lerror("foldtst() 1");
2809 return (getcnode(tn->tn_type, v));
2813 * Same for operands with floating point type.
2816 foldflt(tnode_t *tn)
2822 if ((v = calloc(1, sizeof (val_t))) == NULL)
2824 v->v_tspec = t = tn->tn_type->t_tspec;
2827 lerror("foldflt() 1");
2829 if (t != tn->tn_left->tn_type->t_tspec)
2830 lerror("foldflt() 2");
2831 if (modtab[tn->tn_op].m_binary && t != tn->tn_right->tn_type->t_tspec)
2832 lerror("foldflt() 3");
2834 l = tn->tn_left->tn_val->v_ldbl;
2835 if (modtab[tn->tn_op].m_binary)
2836 r = tn->tn_right->tn_val->v_ldbl;
2838 switch (tn->tn_op) {
2853 v->v_ldbl = l < 0 ? -FLT_MAX : FLT_MAX;
2854 } else if (t == DOUBLE) {
2855 v->v_ldbl = l < 0 ? -DBL_MAX : DBL_MAX;
2857 v->v_ldbl = l < 0 ? -LDBL_MAX : LDBL_MAX;
2888 lerror("foldflt() 4");
2891 if (isnan((double)v->v_ldbl))
2892 lerror("foldflt() 5");
2893 if (!finite((double)v->v_ldbl) ||
2895 (v->v_ldbl > FLT_MAX || v->v_ldbl < -FLT_MAX)) ||
2897 (v->v_ldbl > DBL_MAX || v->v_ldbl < -DBL_MAX))) {
2898 /* floating point overflow detected, op %s */
2899 warning(142, modtab[tn->tn_op].m_name);
2901 v->v_ldbl = v->v_ldbl < 0 ? -FLT_MAX : FLT_MAX;
2902 } else if (t == DOUBLE) {
2903 v->v_ldbl = v->v_ldbl < 0 ? -DBL_MAX : DBL_MAX;
2905 v->v_ldbl = v->v_ldbl < 0 ? -LDBL_MAX: LDBL_MAX;
2909 return (getcnode(tn->tn_type, v));
2913 * Create a constant node for sizeof.
2922 while (tp->t_tspec == ARRAY) {
2927 /* cannot take size of incomplete type */
2931 switch (tp->t_tspec) {
2933 /* cannot take size of function */
2940 /* cannot take size of incomplete type */
2944 elsz = tp->t_str->size;
2949 /* cannot take size of incomplete type */
2954 if (tp->t_isfield) {
2955 /* cannot take size of bit-field */
2958 if (tp->t_tspec == VOID) {
2959 /* cannot take size of void */
2963 elsz = size(tp->t_tspec);
2965 lerror("bldszof() 1");
2976 return (getinode(st, (int64_t)(elem * elsz / CHAR_BIT)));
2983 cast(tnode_t *tn, type_t *tp)
2993 ot = tn->tn_type->t_tspec;
2997 * XXX ANSI C requires scalar types or void (Plauger&Brodie).
2998 * But this seams really questionable.
3000 } else if (nt == STRUCT || nt == UNION || nt == ARRAY || nt == FUNC) {
3001 /* invalid cast expression */
3004 } else if (ot == STRUCT || ot == UNION) {
3005 /* invalid cast expression */
3008 } else if (ot == VOID) {
3009 /* improper cast of void expression */
3012 } else if (isityp(nt) && issclt(ot)) {
3014 } else if (isftyp(nt) && isatyp(ot)) {
3016 } else if (nt == PTR && isityp(ot)) {
3018 } else if (nt == PTR && ot == PTR) {
3019 if (!tp->t_subt->t_const && tn->tn_type->t_subt->t_const) {
3021 /* cast discards 'const' from ... */
3025 /* invalid cast expression */
3030 tn = convert(CVT, 0, tp, tn);
3037 * Create the node for a function argument.
3038 * All necessary conversions and type checks are done in funccall(), because
3039 * in funcarg() we have no information about expected argument types.
3042 funcarg(tnode_t *args, tnode_t *arg)
3047 * If there was a serious error in the expression for the argument,
3048 * create a dummy argument so the positions of the remaining arguments
3052 arg = getinode(INT, (int64_t)0);
3054 ntn = mktnode(PUSH, arg->tn_type, arg, args);
3060 * Create the node for a function call. Also check types of
3061 * function arguments and insert conversions, if necessary.
3064 funccall(tnode_t *func, tnode_t *args)
3072 if (func->tn_op == NAME && func->tn_type->t_tspec == FUNC) {
3079 * after cconv() func will always be a pointer to a function
3080 * if it is a valid function designator.
3084 if (func->tn_type->t_tspec != PTR ||
3085 func->tn_type->t_subt->t_tspec != FUNC) {
3086 /* illegal function */
3091 args = chkfarg(func->tn_type->t_subt, args);
3093 ntn = mktnode(fcop, func->tn_type->t_subt->t_subt, func, args);
3099 * Check types of all function arguments and insert conversions,
3103 chkfarg(type_t *ftp, tnode_t *args)
3108 int narg, npar, n, i;
3110 /* get # of args in the prototype */
3112 for (asym = ftp->t_args; asym != NULL; asym = asym->s_nxt)
3115 /* get # of args in function call */
3117 for (arg = args; arg != NULL; arg = arg->tn_right)
3121 if (ftp->t_proto && npar != narg && !(ftp->t_vararg && npar < narg)) {
3122 /* argument mismatch: %d arg%s passed, %d expected */
3123 error(150, narg, narg > 1 ? "s" : "", npar);
3127 for (n = 1; n <= narg; n++) {
3130 * The rightmost argument is at the top of the argument
3133 for (i = narg, arg = args; i > n; i--, arg = arg->tn_right)
3136 /* some things which are always not allowd */
3137 if ((at = arg->tn_left->tn_type->t_tspec) == VOID) {
3138 /* void expressions may not be arguments, arg #%d */
3141 } else if ((at == STRUCT || at == UNION) &&
3142 incompl(arg->tn_left->tn_type)) {
3143 /* argument cannot have unknown size, arg #%d */
3146 } else if (isityp(at) && arg->tn_left->tn_type->t_isenum &&
3147 incompl(arg->tn_left->tn_type)) {
3148 /* argument cannot have unknown size, arg #%d */
3152 /* class conversions (arg in value context) */
3153 arg->tn_left = cconv(arg->tn_left);
3156 arg->tn_left = parg(n, asym->s_type, arg->tn_left);
3158 arg->tn_left = promote(NOOP, 1, arg->tn_left);
3160 arg->tn_type = arg->tn_left->tn_type;
3170 * Compare the type of an argument with the corresponding type of a
3171 * prototype parameter. If it is a valid combination, but both types
3172 * are not the same, insert a conversion to convert the argument into
3173 * the type of the parameter.
3176 parg( int n, /* pos of arg */
3177 type_t *tp, /* expected type (from prototype) */
3178 tnode_t *tn) /* argument */
3183 if ((ln = calloc(1, sizeof (tnode_t))) == NULL)
3185 ln->tn_type = tduptyp(tp);
3186 ln->tn_type->t_const = 0;
3188 if (typeok(FARG, n, ln, tn)) {
3189 if (!eqtype(tp, tn->tn_type, 1, 0, (warn = 0, &warn)) || warn)
3190 tn = convert(FARG, n, tp, tn);
3197 * Return the value of an integral constant expression.
3198 * If the expression is not constant or its type is not an integer
3199 * type, an error message is printed.
3202 constant(tnode_t *tn)
3209 tn = promote(NOOP, 0, tn);
3211 if ((v = calloc(1, sizeof (val_t))) == NULL)
3216 lerror("constant() 1");
3222 v->v_tspec = tn->tn_type->t_tspec;
3224 if (tn->tn_op == CON) {
3225 if (tn->tn_type->t_tspec != tn->tn_val->v_tspec)
3226 lerror("constant() 2");
3227 if (isityp(tn->tn_val->v_tspec)) {
3228 v->v_ansiu = tn->tn_val->v_ansiu;
3229 v->v_quad = tn->tn_val->v_quad;
3232 v->v_quad = tn->tn_val->v_ldbl;
3237 /* integral constant expression expected */
3240 if (!isityp(v->v_tspec))
3247 * Perform some tests on expressions which can't be done in build() and
3248 * functions called by build(). These tests must be done here because
3249 * we need some information about the context in which the operations
3251 * After all tests are performed, expr() frees the memory which is used
3252 * for the expression.
3255 expr(tnode_t *tn, int vctx, int tctx)
3258 if (tn == NULL && nerr == 0)
3266 /* expr() is also called in global initialisations */
3267 if (dcs->d_ctx != EXTERN)
3270 chkmisc(tn, vctx, tctx, !tctx, 0, 0, 0);
3271 if (tn->tn_op == ASSIGN) {
3273 /* assignment in conditional context */
3275 } else if (tn->tn_op == CON) {
3276 if (hflag && tctx && !ccflg)
3277 /* constant in conditional context */
3280 if (!modtab[tn->tn_op].m_sideeff) {
3282 * for left operands of COMMA this warning is already
3285 if (tn->tn_op != COMMA && !vctx && !tctx)
3291 /* free the tree memory */
3296 nulleff(tnode_t *tn)
3302 while (!modtab[tn->tn_op].m_sideeff) {
3303 if (tn->tn_op == CVT && tn->tn_type->t_tspec == VOID) {
3305 } else if (tn->tn_op == LOGAND || tn->tn_op == LOGOR) {
3307 * && and || have a side effect if the right operand
3308 * has a side effect.
3311 } else if (tn->tn_op == QUEST) {
3313 * ? has a side effect if at least one of its right
3314 * operands has a side effect
3317 } else if (tn->tn_op == COLON || tn->tn_op == COMMA) {
3319 * : has a side effect if at least one of its operands
3322 if (modtab[tn->tn_left->tn_op].m_sideeff) {
3324 } else if (modtab[tn->tn_right->tn_op].m_sideeff) {
3333 if (!modtab[tn->tn_op].m_sideeff)
3334 /* expression has null effect */
3339 * Dump an expression to stdout
3340 * only used for debugging
3343 displexpr(tnode_t *tn, int offs)
3348 (void)printf("%*s%s\n", offs, "", "NULL");
3351 (void)printf("%*sop %s ", offs, "", modtab[tn->tn_op].m_name);
3353 if (tn->tn_op == NAME) {
3354 (void)printf("%s: %s ",
3355 tn->tn_sym->s_name, scltoa(tn->tn_sym->s_scl));
3356 } else if (tn->tn_op == CON && isftyp(tn->tn_type->t_tspec)) {
3357 (void)printf("%#g ", (double)tn->tn_val->v_ldbl);
3358 } else if (tn->tn_op == CON && isityp(tn->tn_type->t_tspec)) {
3359 uq = tn->tn_val->v_quad;
3360 (void)printf("0x %08lx %08lx ", (long)(uq >> 32) & 0xffffffffl,
3361 (long)uq & 0xffffffffl);
3362 } else if (tn->tn_op == CON) {
3363 if (tn->tn_type->t_tspec != PTR)
3364 lerror("displexpr() 1");
3365 (void)printf("0x%0*lx ", (int)(sizeof (void *) * CHAR_BIT / 4),
3366 (u_long)tn->tn_val->v_quad);
3367 } else if (tn->tn_op == STRING) {
3368 if (tn->tn_strg->st_tspec == CHAR) {
3369 (void)printf("\"%s\"", tn->tn_strg->st_cp);
3373 n = MB_CUR_MAX * (tn->tn_strg->st_len + 1);
3374 if ((s = malloc(n)) == NULL)
3376 (void)wcstombs(s, tn->tn_strg->st_wcp, n);
3377 (void)printf("L\"%s\"", s);
3381 } else if (tn->tn_op == FSEL) {
3382 (void)printf("o=%d, l=%d ", tn->tn_type->t_foffs,
3383 tn->tn_type->t_flen);
3385 (void)printf("%s\n", ttos(tn->tn_type));
3386 if (tn->tn_op == NAME || tn->tn_op == CON || tn->tn_op == STRING)
3388 displexpr(tn->tn_left, offs + 2);
3389 if (modtab[tn->tn_op].m_binary ||
3390 (tn->tn_op == PUSH && tn->tn_right != NULL)) {
3391 displexpr(tn->tn_right, offs + 2);
3396 * Called by expr() to recursively perform some tests.
3400 chkmisc(tnode_t *tn, int vctx, int tctx, int eqwarn, int fcall, int rvdisc,
3405 int nrvdisc, cvctx, ctctx;
3415 mp = &modtab[op = tn->tn_op];
3419 if (ln->tn_op == NAME && (reached || rchflg)) {
3421 setsflg(ln->tn_sym);
3422 setuflg(ln->tn_sym, fcall, szof);
3424 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3425 /* check the range of array indices */
3426 chkaidx(ln->tn_left, 1);
3429 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3430 /* check the range of array indices */
3431 chkaidx(ln->tn_left, 0);
3448 if (ln->tn_op == NAME && (reached || rchflg)) {
3449 sc = ln->tn_sym->s_scl;
3451 * Look if there was an asm statement in one of the
3452 * compound statements we are in. If not, we don't
3455 for (di = dcs; di != NULL; di = di->d_nxt) {
3459 if (sc != EXTERN && sc != STATIC &&
3460 !ln->tn_sym->s_set && !szof && di == NULL) {
3461 /* %s may be used before set */
3462 warning(158, ln->tn_sym->s_name);
3463 setsflg(ln->tn_sym);
3465 setuflg(ln->tn_sym, 0, 0);
3469 if (ln->tn_op == NAME && !szof && (reached || rchflg)) {
3470 setsflg(ln->tn_sym);
3471 if (ln->tn_sym->s_scl == EXTERN)
3474 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3475 /* check the range of array indices */
3476 chkaidx(ln->tn_left, 0);
3479 if (ln->tn_op != AMPER || ln->tn_left->tn_op != NAME)
3480 lerror("chkmisc() 1");
3482 outcall(tn, vctx || tctx, rvdisc);
3485 /* equality operator "==" found where "=" was exp. */
3486 if (hflag && eqwarn)
3493 /* LINTED (enumeration values not handled in switch) */
3537 * values of operands of ':' are not used if the type of at least
3538 * one of the operands (for gcc compatibility) is void
3539 * XXX test/value context of QUEST should probably be used as
3540 * context for both operands of COLON
3542 if (op == COLON && tn->tn_type->t_tspec == VOID)
3544 nrvdisc = op == CVT && tn->tn_type->t_tspec == VOID;
3545 chkmisc(ln, cvctx, ctctx, mp->m_eqwarn, op == CALL, nrvdisc, szof);
3550 chkmisc(rn, 0, 0, mp->m_eqwarn, 0, 0, szof);
3554 chkmisc(rn, 0, 1, mp->m_eqwarn, 0, 0, szof);
3557 chkmisc(rn, cvctx, ctctx, mp->m_eqwarn, 0, 0, szof);
3560 chkmisc(rn, vctx, tctx, mp->m_eqwarn, 0, 0, szof);
3564 chkmisc(rn, 1, 0, mp->m_eqwarn, 0, 0, szof);
3571 * Checks the range of array indices, if possible.
3572 * amper is set if only the address of the element is used. This
3573 * means that the index is allowd to refere to the first element
3577 chkaidx(tnode_t *tn, int amper)
3587 /* We can only check constant indices. */
3588 if (rn->tn_op != CON)
3591 /* Return if the left node does not stem from an array. */
3592 if (ln->tn_op != AMPER)
3594 if (ln->tn_left->tn_op != STRING && ln->tn_left->tn_op != NAME)
3596 if (ln->tn_left->tn_type->t_tspec != ARRAY)
3600 * For incomplete array types, we can print a warning only if
3601 * the index is negative.
3603 if (incompl(ln->tn_left->tn_type) && rn->tn_val->v_quad >= 0)
3606 /* Get the size of one array element */
3607 if ((elsz = length(ln->tn_type->t_subt, NULL)) == 0)
3611 /* Change the unit of the index from bytes to element size. */
3612 if (isutyp(rn->tn_type->t_tspec)) {
3613 con = (uint64_t)rn->tn_val->v_quad / elsz;
3615 con = rn->tn_val->v_quad / elsz;
3618 dim = ln->tn_left->tn_type->t_dim + (amper ? 1 : 0);
3620 if (!isutyp(rn->tn_type->t_tspec) && con < 0) {
3621 /* array subscript cannot be negative: %ld */
3622 warning(167, (long)con);
3623 } else if (dim > 0 && (uint64_t)con >= dim) {
3624 /* array subscript cannot be > %d: %ld */
3625 warning(168, dim - 1, (long)con);
3630 * Check for ordered comparisons of unsigned values with 0.
3633 chkcomp(op_t op, tnode_t *ln, tnode_t *rn)
3638 lt = ln->tn_type->t_tspec;
3639 rt = rn->tn_type->t_tspec;
3642 if (ln->tn_op != CON && rn->tn_op != CON)
3645 if (!isityp(lt) || !isityp(rt))
3648 if ((hflag || pflag) && lt == CHAR && rn->tn_op == CON &&
3649 (rn->tn_val->v_quad < 0 ||
3650 rn->tn_val->v_quad > ~(~0 << (CHAR_BIT - 1)))) {
3651 /* nonportable character comparison, op %s */
3652 warning(230, mp->m_name);
3655 if ((hflag || pflag) && rt == CHAR && ln->tn_op == CON &&
3656 (ln->tn_val->v_quad < 0 ||
3657 ln->tn_val->v_quad > ~(~0 << (CHAR_BIT - 1)))) {
3658 /* nonportable character comparison, op %s */
3659 warning(230, mp->m_name);
3662 if (isutyp(lt) && !isutyp(rt) &&
3663 rn->tn_op == CON && rn->tn_val->v_quad <= 0) {
3664 if (rn->tn_val->v_quad < 0) {
3665 /* comparison of %s with %s, op %s */
3666 warning(162, tyname(ln->tn_type), "negative constant",
3668 } else if (op == LT || op == GE || (hflag && op == LE)) {
3669 /* comparison of %s with %s, op %s */
3670 warning(162, tyname(ln->tn_type), "0", mp->m_name);
3674 if (isutyp(rt) && !isutyp(lt) &&
3675 ln->tn_op == CON && ln->tn_val->v_quad <= 0) {
3676 if (ln->tn_val->v_quad < 0) {
3677 /* comparison of %s with %s, op %s */
3678 warning(162, "negative constant", tyname(rn->tn_type),
3680 } else if (op == GT || op == LE || (hflag && op == GE)) {
3681 /* comparison of %s with %s, op %s */
3682 warning(162, "0", tyname(rn->tn_type), mp->m_name);
3689 * Takes an expression an returns 0 if this expression can be used
3690 * for static initialisation, otherwise -1.
3692 * Constant initialisation expressions must be costant or an address
3693 * of a static object with an optional offset. In the first case,
3694 * the result is returned in *offsp. In the second case, the static
3695 * object is returned in *symp and the offset in *offsp.
3697 * The expression can consist of PLUS, MINUS, AMPER, NAME, STRING and
3698 * CON. Type conversions are allowed if they do not change binary
3699 * representation (including width).
3702 conaddr(tnode_t *tn, sym_t **symp, ptrdiff_t *offsp)
3705 ptrdiff_t offs1, offs2;
3708 switch (tn->tn_op) {
3710 if (tn->tn_right->tn_op != CON)
3715 if (tn->tn_left->tn_op == CON) {
3716 offs1 = (ptrdiff_t)tn->tn_left->tn_val->v_quad;
3717 if (conaddr(tn->tn_right, &sym, &offs2) == -1)
3719 } else if (tn->tn_right->tn_op == CON) {
3720 offs2 = (ptrdiff_t)tn->tn_right->tn_val->v_quad;
3721 if (tn->tn_op == MINUS)
3723 if (conaddr(tn->tn_left, &sym, &offs1) == -1)
3729 *offsp = offs1 + offs2;
3732 if (tn->tn_left->tn_op == NAME) {
3733 *symp = tn->tn_left->tn_sym;
3735 } else if (tn->tn_left->tn_op == STRING) {
3737 * If this would be the front end of a compiler we
3738 * would return a label instead of 0.
3744 t = tn->tn_type->t_tspec;
3745 ot = tn->tn_left->tn_type->t_tspec;
3746 if ((!isityp(t) && t != PTR) || (!isityp(ot) && ot != PTR)) {
3748 } else if (psize(t) != psize(ot)) {
3751 if (conaddr(tn->tn_left, symp, offsp) == -1)
3761 * Concatenate two string constants.
3764 catstrg(strg_t *strg1, strg_t *strg2)
3766 size_t len1, len2, len;
3768 if (strg1->st_tspec != strg2->st_tspec) {
3769 /* cannot concatenate wide and regular string literals */
3774 len = (len1 = strg1->st_len) + (len2 = strg2->st_len);
3776 if (strg1->st_tspec == CHAR) {
3777 if ((strg1->st_cp = realloc(strg1->st_cp, len + 1)) == NULL)
3779 (void)memcpy(strg1->st_cp + len1, strg2->st_cp, len2 + 1);
3782 if ((strg1->st_wcp = realloc(strg1->st_wcp, (len + 1) *
3783 sizeof (wchar_t))) == NULL)
3785 (void)memcpy(strg1->st_wcp + len1, strg2->st_wcp,
3786 (len2 + 1) * sizeof (wchar_t));
3787 free(strg2->st_wcp);
3789 strg1->st_len = len;
3796 * Print a warning if the given node has operands which should be
3799 * XXX Does not work if an operand is a constant expression. Constant
3800 * expressions are already folded.
3803 precconf(tnode_t *tn)
3806 op_t lop, rop = NOOP;
3807 int lparn, rparn = 0;
3814 mp = &modtab[tn->tn_op];
3817 for (ln = tn->tn_left; ln->tn_op == CVT; ln = ln->tn_left)
3818 lparn |= ln->tn_parn;
3819 lparn |= ln->tn_parn;
3824 for (rn = tn->tn_right; tn->tn_op == CVT; rn = rn->tn_left)
3825 rparn |= rn->tn_parn;
3826 rparn |= rn->tn_parn;
3832 switch (tn->tn_op) {
3835 if (!lparn && (lop == PLUS || lop == MINUS)) {
3837 } else if (!rparn && (rop == PLUS || rop == MINUS)) {
3842 if (!lparn && lop == LOGAND) {
3844 } else if (!rparn && rop == LOGAND) {
3851 if (!lparn && lop != tn->tn_op) {
3852 if (lop == PLUS || lop == MINUS) {
3854 } else if (lop == AND || lop == XOR) {
3858 if (!warn && !rparn && rop != tn->tn_op) {
3859 if (rop == PLUS || rop == MINUS) {
3861 } else if (rop == AND || rop == XOR) {
3866 /* LINTED (enumeration values not handled in switch) */
3925 /* precedence confusion possible: parenthesize! */