1 /* $NetBSD: tree.c,v 1.12 1995/10/02 17:37:57 jpo Exp $ */
4 * Copyright (c) 1994, 1995 Jochen Pohl
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Jochen Pohl for
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * $NetBSD: tree.c,v 1.12 1995/10/02 17:37:57 jpo Exp $
45 /* Various flags for each operator. */
46 static mod_t modtab[NOPS];
48 static tnode_t *getinode __P((tspec_t, quad_t));
49 static void ptrcmpok __P((op_t, tnode_t *, tnode_t *));
50 static int asgntypok __P((op_t, int, tnode_t *, tnode_t *));
51 static void chkbeop __P((op_t, tnode_t *, tnode_t *));
52 static void chkeop2 __P((op_t, int, tnode_t *, tnode_t *));
53 static void chkeop1 __P((op_t, int, tnode_t *, tnode_t *));
54 static tnode_t *mktnode __P((op_t, type_t *, tnode_t *, tnode_t *));
55 static void balance __P((op_t, tnode_t **, tnode_t **));
56 static void incompat __P((op_t, tspec_t, tspec_t));
57 static void illptrc __P((mod_t *, type_t *, type_t *));
58 static void mrgqual __P((type_t **, type_t *, type_t *));
59 static int conmemb __P((type_t *));
60 static void ptconv __P((int, tspec_t, tspec_t, type_t *, tnode_t *));
61 static void iiconv __P((op_t, int, tspec_t, tspec_t, type_t *, tnode_t *));
62 static void piconv __P((op_t, tspec_t, type_t *, tnode_t *));
63 static void ppconv __P((op_t, tnode_t *, type_t *));
64 static tnode_t *bldstr __P((op_t, tnode_t *, tnode_t *));
65 static tnode_t *bldincdec __P((op_t, tnode_t *));
66 static tnode_t *bldamper __P((tnode_t *, int));
67 static tnode_t *bldplmi __P((op_t, tnode_t *, tnode_t *));
68 static tnode_t *bldshft __P((op_t, tnode_t *, tnode_t *));
69 static tnode_t *bldcol __P((tnode_t *, tnode_t *));
70 static tnode_t *bldasgn __P((op_t, tnode_t *, tnode_t *));
71 static tnode_t *plength __P((type_t *));
72 static tnode_t *fold __P((tnode_t *));
73 static tnode_t *foldtst __P((tnode_t *));
74 static tnode_t *foldflt __P((tnode_t *));
75 static tnode_t *chkfarg __P((type_t *, tnode_t *));
76 static tnode_t *parg __P((int, type_t *, tnode_t *));
77 static void nulleff __P((tnode_t *));
78 static void displexpr __P((tnode_t *, int));
79 static void chkaidx __P((tnode_t *, int));
80 static void chkcomp __P((op_t, tnode_t *, tnode_t *));
81 static void precconf __P((tnode_t *));
84 * Initialize mods of operators.
93 { ARROW, { 1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,
95 { POINT, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
97 { NOT, { 0,1,0,1,0,1,0,1,0,0,0,0,0,0,0,1,0,
99 { COMPL, { 0,0,1,0,0,1,1,0,0,0,0,0,0,0,0,1,1,
101 { INCBEF, { 0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,
103 { DECBEF, { 0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,
105 { INCAFT, { 0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,
107 { DECAFT, { 0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,
109 { UPLUS, { 0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,1,
111 { UMINUS, { 0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,1,1,
113 { STAR, { 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,
115 { AMPER, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
117 { MULT, { 1,0,0,0,1,1,1,0,1,0,0,1,0,0,0,1,1,
119 { DIV, { 1,0,0,0,1,1,1,0,1,0,1,1,0,0,0,1,1,
121 { MOD, { 1,0,1,0,0,1,1,0,1,0,1,1,0,0,0,1,1,
123 { PLUS, { 1,0,0,1,0,1,1,0,1,0,0,0,0,0,0,1,0,
125 { MINUS, { 1,0,0,1,0,1,1,0,1,0,0,0,0,0,0,1,0,
127 { SHL, { 1,0,1,0,0,1,1,0,0,0,0,0,1,0,0,1,1,
129 { SHR, { 1,0,1,0,0,1,1,0,0,0,1,0,1,0,0,1,1,
131 { LT, { 1,1,0,1,0,1,1,0,1,0,1,1,0,1,1,0,1,
133 { LE, { 1,1,0,1,0,1,1,0,1,0,1,1,0,1,1,0,1,
135 { GT, { 1,1,0,1,0,1,1,0,1,0,1,1,0,1,1,0,1,
137 { GE, { 1,1,0,1,0,1,1,0,1,0,1,1,0,1,1,0,1,
139 { EQ, { 1,1,0,1,0,1,1,0,1,0,0,0,0,1,1,0,1,
141 { NE, { 1,1,0,1,0,1,1,0,1,0,0,0,0,1,1,0,1,
143 { AND, { 1,0,1,0,0,1,1,0,1,0,0,0,1,0,0,1,0,
145 { XOR, { 1,0,1,0,0,1,1,0,1,0,0,0,1,0,0,1,0,
147 { OR, { 1,0,1,0,0,1,1,0,1,0,0,0,1,0,0,1,0,
149 { LOGAND, { 1,1,0,1,0,1,0,1,0,0,0,0,0,0,0,1,0,
151 { LOGOR, { 1,1,0,1,0,1,0,1,0,0,0,0,1,0,0,1,0,
153 { QUEST, { 1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,
155 { COLON, { 1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,
157 { ASSIGN, { 1,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,
159 { MULASS, { 1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,1,0,
161 { DIVASS, { 1,0,0,0,1,0,0,0,0,1,0,1,0,0,0,1,0,
163 { MODASS, { 1,0,1,0,0,0,0,0,0,1,0,1,0,0,0,1,0,
165 { ADDASS, { 1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,
167 { SUBASS, { 1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,
169 { SHLASS, { 1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,
171 { SHRASS, { 1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,
173 { ANDASS, { 1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,
175 { XORASS, { 1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,
177 { ORASS, { 1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,
179 { NAME, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
181 { CON, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
183 { STRING, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
185 { FSEL, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
187 { CALL, { 1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
189 { COMMA, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
191 { CVT, { 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,
193 { ICALL, { 1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
195 { LOAD, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
197 { PUSH, { 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,
199 { RETURN, { 1,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,
201 { INIT, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,
203 { FARG, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,
209 for (i = 0; imods[i].op != NOOP; i++)
210 STRUCT_ASSIGN(modtab[imods[i].op], imods[i].m);
214 * Increase degree of reference.
215 * This is most often used to change type "T" in type "pointer to T".
224 tp2 = getblk(sizeof (type_t));
231 * same for use in expressions
240 tp2 = tgetblk(sizeof (type_t));
247 * Create a node for a constant.
259 n->tn_val = tgetblk(sizeof (val_t));
260 n->tn_val->v_tspec = tp->t_tspec;
261 n->tn_val->v_ansiu = v->v_ansiu;
262 n->tn_val->v_u = v->v_u;
268 * Create a node for a integer constant.
279 n->tn_type = gettyp(t);
280 n->tn_val = tgetblk(sizeof (val_t));
281 n->tn_val->v_tspec = t;
282 n->tn_val->v_quad = q;
287 * Create a node for a name (symbol table entry).
288 * ntok is the token which follows the name.
297 if (sym->s_scl == NOSCL) {
300 if (ntok == T_LPARN) {
302 /* function implicitly declared to ... */
306 * XXX if tflag is set the symbol should be
307 * exported to level 0
309 sym->s_type = incref(sym->s_type, FUNC);
312 error(99, sym->s_name);
316 if (sym->s_kind != FVFT && sym->s_kind != FMOS)
317 lerror("getnnode() 1");
320 n->tn_type = sym->s_type;
321 if (sym->s_scl != ENUMCON) {
324 if (sym->s_kind == FVFT && sym->s_type->t_tspec != FUNC)
328 n->tn_val = tgetblk(sizeof (val_t));
329 *n->tn_val = sym->s_value;
336 * Create a node for a string.
350 n->tn_type = tincref(gettyp(strg->st_tspec), ARRAY);
351 n->tn_type->t_dim = len + 1;
354 n->tn_strg = tgetblk(sizeof (strg_t));
355 n->tn_strg->st_tspec = strg->st_tspec;
356 n->tn_strg->st_len = len;
358 if (strg->st_tspec == CHAR) {
359 n->tn_strg->st_cp = tgetblk(len + 1);
360 (void)memcpy(n->tn_strg->st_cp, strg->st_cp, len + 1);
363 n->tn_strg->st_wcp = tgetblk((len + 1) * sizeof (wchar_t));
364 (void)memcpy(n->tn_strg->st_wcp, strg->st_wcp,
365 (len + 1) * sizeof (wchar_t));
374 * Returns a symbol which has the same name as the msym argument and is a
375 * member of the struct or union specified by the tn argument.
378 strmemb(tn, op, msym)
390 * Remove the member if it was unknown until now (Which means
391 * that no defined struct or union has a member with the same name).
393 if (msym->s_scl == NOSCL) {
394 /* undefined struct/union member: %s */
395 error(101, msym->s_name);
399 msym->s_styp = tgetblk(sizeof (str_t));
400 msym->s_styp->stag = tgetblk(sizeof (sym_t));
401 msym->s_styp->stag->s_name = unnamed;
402 msym->s_value.v_tspec = INT;
406 /* Set str to the tag of which msym is expected to be a member. */
408 t = (tp = tn->tn_type)->t_tspec;
410 if (t == STRUCT || t == UNION)
412 } else if (op == ARROW && t == PTR) {
413 t = (tp = tp->t_subt)->t_tspec;
414 if (t == STRUCT || t == UNION)
419 * If this struct/union has a member with the name of msym, return
423 for (sym = msym; sym != NULL; sym = sym->s_link) {
424 if (sym->s_scl != MOS && sym->s_scl != MOU)
426 if (sym->s_styp != str)
428 if (strcmp(sym->s_name, msym->s_name) != 0)
435 * Set eq to 0 if there are struct/union members with the same name
436 * and different types and/or offsets.
439 for (csym = msym; csym != NULL; csym = csym->s_link) {
440 if (csym->s_scl != MOS && csym->s_scl != MOU)
442 if (strcmp(msym->s_name, csym->s_name) != 0)
444 for (sym = csym->s_link ; sym != NULL; sym = sym->s_link) {
447 if (sym->s_scl != MOS && sym->s_scl != MOU)
449 if (strcmp(csym->s_name, sym->s_name) != 0)
451 if (csym->s_value.v_quad != sym->s_value.v_quad) {
456 eq = eqtype(csym->s_type, sym->s_type, 0, 0, &w) && !w;
459 if (csym->s_field != sym->s_field) {
468 if (tp1->t_flen != tp2->t_flen) {
472 if (tp1->t_foffs != tp2->t_foffs) {
483 * Now handle the case in which the left operand refers really
484 * to a struct/union, but the right operand is not member of it.
487 /* illegal member use: %s */
489 warning(102, msym->s_name);
491 error(102, msym->s_name);
497 * Now the left operand of ARROW does not point to a struct/union
498 * or the left operand of POINT is no struct/union.
502 /* left operand of "." must be struct/union object */
509 /* left operand of "->" must be pointer to ... */
510 if (tflag && tn->tn_type->t_tspec == PTR) {
518 /* non-unique member requires struct/union %s */
519 error(105, op == POINT ? "object" : "pointer");
521 /* unacceptable operand of %s */
522 error(111, modtab[op].m_name);
530 * Create a tree node. Called for most operands except function calls,
535 * rn if not NULL, right operand
548 /* If there was an error in one of the operands, return. */
549 if (ln == NULL || (mp->m_binary && rn == NULL))
553 * Apply class conversions to the left operand, but only if its
554 * value is needed or it is compaired with null.
556 if (mp->m_vctx || mp->m_tctx)
559 * The right operand is almost always in a test or value context,
560 * except if it is a struct or union member.
562 if (mp->m_binary && op != ARROW && op != POINT)
566 * Print some warnings for comparisions of unsigned values with
567 * constants lower than or equal to null. This must be done
568 * before promote() because otherwise unsigned char and unsigned
569 * short would be promoted to int. Also types are tested to be
570 * CHAR, which would also become int.
576 * Promote the left operand if it is in a test or value context
578 if (mp->m_vctx || mp->m_tctx)
579 ln = promote(op, 0, ln);
581 * Promote the right operand, but only if it is no struct or
582 * union member, or if it is not to be assigned to the left operand
584 if (mp->m_binary && op != ARROW && op != POINT &&
585 op != ASSIGN && op != RETURN) {
586 rn = promote(op, 0, rn);
590 * If the result of the operation is different for signed or
591 * unsigned operands and one of the operands is signed only in
592 * ANSI C, print a warning.
594 if (mp->m_tlansiu && ln->tn_op == CON && ln->tn_val->v_ansiu) {
595 /* ANSI C treats constant as unsigned, op %s */
596 warning(218, mp->m_name);
597 ln->tn_val->v_ansiu = 0;
599 if (mp->m_transiu && rn->tn_op == CON && rn->tn_val->v_ansiu) {
600 /* ANSI C treats constant as unsigned, op %s */
601 warning(218, mp->m_name);
602 rn->tn_val->v_ansiu = 0;
605 /* Make sure both operands are of the same type */
606 if (mp->m_balance || (tflag && (op == SHL || op == SHR)))
607 balance(op, &ln, &rn);
610 * Check types for compatibility with the operation and mutual
611 * compatibility. Return if there are serios problems.
613 if (!typeok(op, 0, ln, rn))
616 /* And now create the node. */
620 ntn = bldstr(op, ln, rn);
626 ntn = bldincdec(op, ln);
629 ntn = bldamper(ln, 0);
632 ntn = mktnode(STAR, ln->tn_type->t_subt, ln, NULL);
636 ntn = bldplmi(op, ln, rn);
640 ntn = bldshft(op, ln, rn);
643 ntn = bldcol(ln, rn);
657 ntn = bldasgn(op, ln, rn);
661 ntn = mktnode(op, rn->tn_type, ln, rn);
664 rtp = mp->m_logop ? gettyp(INT) : ln->tn_type;
665 if (!mp->m_binary && rn != NULL)
667 ntn = mktnode(op, rtp, ln, rn);
671 /* Return if an error occured. */
675 /* Print a warning if precedence confusion is possible */
680 * Print a warning if one of the operands is in a context where
681 * it is compared with null and if this operand is a constant.
684 if (ln->tn_op == CON ||
685 ((mp->m_binary && op != QUEST) && rn->tn_op == CON)) {
687 /* constant in conditional context */
692 /* Fold if the operator requires it */
694 if (ln->tn_op == CON && (!mp->m_binary || rn->tn_op == CON)) {
697 } else if (isftyp(ntn->tn_type->t_tspec)) {
702 } else if (op == QUEST && ln->tn_op == CON) {
703 ntn = ln->tn_val->v_quad ? rn->tn_left : rn->tn_right;
711 * Perform class conversions.
713 * Arrays of type T are converted into pointers to type T.
714 * Functions are converted to pointers to functions.
715 * Lvalues are converted to rvalues.
724 * Array-lvalue (array of type T) is converted into rvalue
725 * (pointer to type T)
727 if (tn->tn_type->t_tspec == ARRAY) {
728 if (!tn->tn_lvalue) {
729 /* %soperand of '%s' must be lvalue */
730 /* XXX print correct operator */
731 (void)gnuism(114, "", modtab[AMPER].m_name);
733 tn = mktnode(AMPER, tincref(tn->tn_type->t_subt, PTR),
738 * Expression of type function (function with return value of type T)
739 * in rvalue-expression (pointer to function with return value
742 if (tn->tn_type->t_tspec == FUNC)
743 tn = bldamper(tn, 1);
745 /* lvalue to rvalue */
747 tp = tduptyp(tn->tn_type);
748 tp->t_const = tp->t_volatile = 0;
749 tn = mktnode(LOAD, tp, tn, NULL);
756 * Perform most type checks. First the types are checked using
757 * informations from modtab[]. After that it is done by hand for
758 * more complicated operators and type combinations.
760 * If the types are ok, typeok() returns 1, otherwise 0.
763 typeok(op, arg, ln, rn)
769 tspec_t lt, rt, lst, rst, olt, ort;
770 type_t *ltp, *rtp, *lstp, *rstp;
775 if ((lt = (ltp = ln->tn_type)->t_tspec) == PTR)
776 lst = (lstp = ltp->t_subt)->t_tspec;
778 if ((rt = (rtp = rn->tn_type)->t_tspec) == PTR)
779 rst = (rstp = rtp->t_subt)->t_tspec;
783 /* integertypes required */
784 if (!isityp(lt) || (mp->m_binary && !isityp(rt))) {
785 incompat(op, lt, rt);
788 } else if (mp->m_rqsclt) {
789 /* scalar types required */
790 if (!issclt(lt) || (mp->m_binary && !issclt(rt))) {
791 incompat(op, lt, rt);
794 } else if (mp->m_rqatyp) {
795 /* arithmetic types required */
796 if (!isatyp(lt) || (mp->m_binary && !isatyp(rt))) {
797 incompat(op, lt, rt);
802 if (op == SHL || op == SHR || op == SHLASS || op == SHRASS) {
804 * For these operations we need the types before promotion
807 for (tn=ln; tn->tn_op==CVT && !tn->tn_cast; tn=tn->tn_left) ;
808 olt = tn->tn_type->t_tspec;
809 for (tn=rn; tn->tn_op==CVT && !tn->tn_cast; tn=tn->tn_left) ;
810 ort = tn->tn_type->t_tspec;
816 * Most errors required by ANSI C are reported in strmemb().
817 * Here we only must check for totaly wrong things.
819 if (lt == FUNC || lt == VOID || ltp->t_isfield ||
820 ((lt != STRUCT && lt != UNION) && !ln->tn_lvalue)) {
821 /* Without tflag we got already an error */
823 /* unacceptable operand of %s */
824 error(111, mp->m_name);
827 /* Now we have an object we can create a pointer to */
830 if (lt != PTR && !(tflag && isityp(lt))) {
831 /* Without tflag we got already an error */
833 /* unacceptabel operand of %s */
834 error(111, mp->m_name);
842 /* operands have scalar types (checked above) */
843 if (!ln->tn_lvalue) {
844 if (ln->tn_op == CVT && ln->tn_cast &&
845 ln->tn_left->tn_op == LOAD) {
846 /* a cast does not yield an lvalue */
849 /* %soperand of %s must be lvalue */
850 error(114, "", mp->m_name);
852 } else if (ltp->t_const) {
853 /* %soperand of %s must be modifiable lvalue */
855 warning(115, "", mp->m_name);
859 if (lt == ARRAY || lt == FUNC) {
860 /* ok, a warning comes later (in bldamper()) */
861 } else if (!ln->tn_lvalue) {
862 if (ln->tn_op == CVT && ln->tn_cast &&
863 ln->tn_left->tn_op == LOAD) {
864 /* a cast does not yield an lvalue */
867 /* %soperand of %s must be lvalue */
868 error(114, "", mp->m_name);
870 } else if (issclt(lt)) {
871 if (ltp->t_isfield) {
872 /* cannot take address of bit-field */
876 } else if (lt != STRUCT && lt != UNION) {
877 /* unacceptable operand of %s */
878 error(111, mp->m_name);
881 if (ln->tn_op == NAME && ln->tn_sym->s_reg) {
882 /* cannot take address of register %s */
883 error(113, ln->tn_sym->s_name);
888 /* until now there were no type checks for this operator */
890 /* cannot dereference non-pointer type */
896 /* operands have scalar types (checked above) */
897 if ((lt == PTR && !isityp(rt)) || (rt == PTR && !isityp(lt))) {
898 incompat(op, lt, rt);
903 /* operands have scalar types (checked above) */
904 if (lt == PTR && (!isityp(rt) && rt != PTR)) {
905 incompat(op, lt, rt);
907 } else if (rt == PTR && lt != PTR) {
908 incompat(op, lt, rt);
911 if (lt == PTR && rt == PTR) {
912 if (!eqtype(lstp, rstp, 1, 0, NULL)) {
913 /* illegal pointer subtraction */
919 /* operands have integer types (checked above) */
920 if (pflag && !isutyp(lt)) {
922 * The left operand is signed. This means that
923 * the operation is (possibly) nonportable.
925 /* bitwise operation on signed value nonportable */
926 if (ln->tn_op != CON) {
927 /* possibly nonportable */
929 } else if (ln->tn_val->v_quad < 0) {
932 } else if (!tflag && !sflag && !isutyp(olt) && isutyp(ort)) {
934 * The left operand would become unsigned in
938 (ln->tn_op != CON || ln->tn_val->v_quad < 0)) {
939 /* semantics of %s change in ANSI C; use ... */
940 warning(118, mp->m_name);
942 } else if (!tflag && !sflag && !isutyp(olt) && !isutyp(ort) &&
943 psize(lt) < psize(rt)) {
945 * In traditional C the left operand would be extended,
946 * possibly with 1, and then shifted.
949 (ln->tn_op != CON || ln->tn_val->v_quad < 0)) {
950 /* semantics of %s change in ANSI C; use ... */
951 warning(118, mp->m_name);
957 * ANSI C does not perform balancing for shift operations,
958 * but traditional C does. If the width of the right operand
959 * is greather than the width of the left operand, than in
960 * traditional C the left operand would be extendet to the
961 * width of the right operand. For SHL this may result in
964 if (psize(lt) < psize(rt)) {
966 * XXX If both operands are constant make sure
967 * that there is really a differencs between
968 * ANSI C and traditional C.
971 /* semantics of %s change in ANSI C; use ... */
972 warning(118, mp->m_name);
975 if (rn->tn_op == CON) {
976 if (!isutyp(rt) && rn->tn_val->v_quad < 0) {
979 } else if ((u_quad_t)rn->tn_val->v_quad == size(lt)) {
980 /* shift equal to size fo object */
982 } else if ((u_quad_t)rn->tn_val->v_quad > size(lt)) {
983 /* shift greater than size of object */
991 * Accept some things which are allowed with EQ and NE,
992 * but not with ordered comparisions.
994 if (lt == PTR && ((rt == PTR && rst == VOID) || isityp(rt))) {
995 if (rn->tn_op == CON && rn->tn_val->v_quad == 0)
998 if (rt == PTR && ((lt == PTR && lst == VOID) || isityp(lt))) {
999 if (ln->tn_op == CON && ln->tn_val->v_quad == 0)
1007 if ((lt == PTR || rt == PTR) && lt != rt) {
1008 if (isityp(lt) || isityp(rt)) {
1009 /* illegal comb. of pointer and int., op %s */
1010 warning(123, mp->m_name);
1012 incompat(op, lt, rt);
1015 } else if (lt == PTR && rt == PTR) {
1016 ptrcmpok(op, ln, rn);
1021 /* first operand must have scalar type, op ? : */
1025 if (rn->tn_op != COLON)
1026 lerror("typeok() 2");
1030 if (isatyp(lt) && isatyp(rt))
1033 if (lt == STRUCT && rt == STRUCT && ltp->t_str == rtp->t_str)
1035 if (lt == UNION && rt == UNION && ltp->t_str == rtp->t_str)
1038 /* combination of any pointer and 0, 0L or (void *)0 is ok */
1039 if (lt == PTR && ((rt == PTR && rst == VOID) || isityp(rt))) {
1040 if (rn->tn_op == CON && rn->tn_val->v_quad == 0)
1043 if (rt == PTR && ((lt == PTR && lst == VOID) || isityp(lt))) {
1044 if (ln->tn_op == CON && ln->tn_val->v_quad == 0)
1048 if ((lt == PTR && isityp(rt)) || (isityp(lt) && rt == PTR)) {
1049 /* illegal comb. of ptr. and int., op %s */
1050 warning(123, mp->m_name);
1054 if (lt == VOID || rt == VOID) {
1055 if (lt != VOID || rt != VOID)
1056 /* incompatible types in conditional */
1061 if (lt == PTR && rt == PTR && ((lst == VOID && rst == FUNC) ||
1062 (lst == FUNC && rst == VOID))) {
1063 /* (void *)0 handled above */
1065 /* ANSI C forbids conv. of %s to %s, op %s */
1066 warning(305, "function pointer", "'void *'",
1071 if (rt == PTR && lt == PTR) {
1072 if (!eqtype(lstp, rstp, 1, 0, NULL))
1073 illptrc(mp, ltp, rtp);
1077 /* incompatible types in conditional */
1085 if (!asgntypok(op, arg, ln, rn))
1094 /* operands have scalar types (checked above) */
1095 if ((lt == PTR && !isityp(rt)) || rt == PTR) {
1096 incompat(op, lt, rt);
1103 if (pflag && !isutyp(lt) && !(tflag && isutyp(rt))) {
1104 /* bitwise operation on s.v. possibly nonportabel */
1113 if (!ln->tn_lvalue) {
1114 if (ln->tn_op == CVT && ln->tn_cast &&
1115 ln->tn_left->tn_op == LOAD) {
1116 /* a cast does not yield an lvalue */
1119 /* %soperand of %s must be lvalue */
1120 error(114, "left ", mp->m_name);
1122 } else if (ltp->t_const || ((lt == STRUCT || lt == UNION) &&
1124 /* %soperand of %s must be modifiable lvalue */
1126 warning(115, "left ", mp->m_name);
1130 if (!modtab[ln->tn_op].m_sideeff)
1133 /* LINTED (enumeration values not handled in switch) */
1138 (ltp->t_isenum || (mp->m_binary && rtp->t_isenum))) {
1139 chkbeop(op, ln, rn);
1140 } else if (mp->m_enumop && (ltp->t_isenum && rtp->t_isenum)) {
1141 chkeop2(op, arg, ln, rn);
1142 } else if (mp->m_enumop && (ltp->t_isenum || rtp->t_isenum)) {
1143 chkeop1(op, arg, ln, rn);
1150 ptrcmpok(op, ln, rn)
1156 const char *lts, *rts;
1158 lt = (ltp = ln->tn_type)->t_subt->t_tspec;
1159 rt = (rtp = rn->tn_type)->t_subt->t_tspec;
1161 if (lt == VOID || rt == VOID) {
1162 if (sflag && (lt == FUNC || rt == FUNC)) {
1163 /* (void *)0 already handled in typeok() */
1164 *(lt == FUNC ? <s : &rts) = "function pointer";
1165 *(lt == VOID ? <s : &rts) = "'void *'";
1166 /* ANSI C forbids comparision of %s with %s */
1167 warning(274, lts, rts);
1172 if (!eqtype(ltp->t_subt, rtp->t_subt, 1, 0, NULL)) {
1173 illptrc(&modtab[op], ltp, rtp);
1177 if (lt == FUNC && rt == FUNC) {
1178 if (sflag && op != EQ && op != NE)
1179 /* ANSI C forbids ordered comp. of func ptr */
1185 * Checks type compatibility for ASSIGN, INIT, FARG and RETURN
1186 * and prints warnings/errors if necessary.
1187 * If the types are (almost) compatible, 1 is returned, otherwise 0.
1190 asgntypok(op, arg, ln, rn)
1195 tspec_t lt, rt, lst, rst;
1196 type_t *ltp, *rtp, *lstp, *rstp;
1198 const char *lts, *rts;
1200 if ((lt = (ltp = ln->tn_type)->t_tspec) == PTR)
1201 lst = (lstp = ltp->t_subt)->t_tspec;
1202 if ((rt = (rtp = rn->tn_type)->t_tspec) == PTR)
1203 rst = (rstp = rtp->t_subt)->t_tspec;
1206 if (isatyp(lt) && isatyp(rt))
1209 if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION))
1210 /* both are struct or union */
1211 return (ltp->t_str == rtp->t_str);
1213 /* 0, 0L and (void *)0 may be assigned to any pointer */
1214 if (lt == PTR && ((rt == PTR && rst == VOID) || isityp(rt))) {
1215 if (rn->tn_op == CON && rn->tn_val->v_quad == 0)
1219 if (lt == PTR && rt == PTR && (lst == VOID || rst == VOID)) {
1220 /* two pointers, at least one pointer to void */
1221 if (sflag && (lst == FUNC || rst == FUNC)) {
1222 /* comb. of ptr to func and ptr to void */
1223 *(lst == FUNC ? <s : &rts) = "function pointer";
1224 *(lst == VOID ? <s : &rts) = "'void *'";
1228 /* ANSI C forbids conversion of %s to %s */
1229 warning(303, rts, lts);
1232 /* ANSI C forbids conv. of %s to %s, arg #%d */
1233 warning(304, rts, lts, arg);
1236 /* ANSI C forbids conv. of %s to %s, op %s */
1237 warning(305, rts, lts, mp->m_name);
1243 if (lt == PTR && rt == PTR && (lst == VOID || rst == VOID ||
1244 eqtype(lstp, rstp, 1, 0, NULL))) {
1245 /* compatible pointer types (qualifiers ignored) */
1247 ((!lstp->t_const && rstp->t_const) ||
1248 (!lstp->t_volatile && rstp->t_volatile))) {
1249 /* left side has not all qualifiers of right */
1253 /* incompatible pointer types */
1257 /* argument has incompat. ptr. type, arg #%d */
1261 /* operands have incompat. ptr. types, op %s */
1262 warning(128, mp->m_name);
1269 if ((lt == PTR && isityp(rt)) || (isityp(lt) && rt == PTR)) {
1273 /* illegal combination of pointer and integer */
1277 /* illegal comb. of ptr. and int., arg #%d */
1281 /* illegal comb. of ptr. and int., op %s */
1282 warning(123, mp->m_name);
1288 if (lt == PTR && rt == PTR) {
1292 illptrc(NULL, ltp, rtp);
1295 /* argument has incompatible pointer type, arg #%d */
1299 illptrc(mp, ltp, rtp);
1307 /* initialisation type mismatch */
1311 /* return value type mismatch */
1315 /* argument is incompatible with prototype, arg #%d */
1319 incompat(op, lt, rt);
1327 * Prints a warning if an operator, which should be senseless for an
1328 * enum type, is applied to an enum type.
1342 if (!(ln->tn_type->t_isenum ||
1343 (mp->m_binary && rn->tn_type->t_isenum))) {
1348 * Enum as offset to a pointer is an exception (otherwise enums
1349 * could not be used as array indizes).
1352 ((ln->tn_type->t_isenum && rn->tn_type->t_tspec == PTR) ||
1353 (rn->tn_type->t_isenum && ln->tn_type->t_tspec == PTR))) {
1357 /* dubious operation on enum, op %s */
1358 warning(241, mp->m_name);
1363 * Prints a warning if an operator is applied to two different enum types.
1366 chkeop2(op, arg, ln, rn)
1375 if (ln->tn_type->t_enum != rn->tn_type->t_enum) {
1378 /* enum type mismatch in initialisation */
1382 /* enum type mismatch, arg #%d */
1386 /* return value type mismatch */
1390 /* enum type mismatch, op %s */
1391 warning(130, mp->m_name);
1395 } else if (mp->m_comp && op != EQ && op != NE) {
1397 /* dubious comparisions of enums */
1398 warning(243, mp->m_name);
1404 * Prints a warning if an operator has both enum end other integer
1408 chkeop1(op, arg, ln, 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, type, ln, rn)
1461 ntn->tn_type = type;
1465 if (op == STAR || op == FSEL) {
1466 if (ln->tn_type->t_tspec == PTR) {
1467 t = ln->tn_type->t_subt->t_tspec;
1468 if (t != FUNC && t != VOID)
1471 lerror("mktnode() 2");
1479 * Performs usual conversion of operands to (unsigned) int.
1481 * If tflag is set or the operand is a function argument with no
1482 * type information (no prototype or variable # of args), convert
1486 promote(op, farg, tn)
1495 t = tn->tn_type->t_tspec;
1502 * ANSI C requires that the result is always of type INT
1503 * if INT can represent all possible values of the previous
1506 if (tn->tn_type->t_isfield) {
1507 len = tn->tn_type->t_flen;
1508 if (size(INT) > len) {
1511 if (size(INT) != len)
1512 lerror("promote() 1");
1519 } else if (t == CHAR || t == UCHAR || t == SCHAR) {
1520 t = (size(CHAR) < size(INT) || t != UCHAR) ?
1522 } else if (t == SHORT || t == USHORT) {
1523 t = (size(SHORT) < size(INT) || t == SHORT) ?
1525 } else if (t == ENUM) {
1527 } else if (farg && t == FLOAT) {
1532 * In traditional C, keep unsigned and promote FLOAT
1535 if (t == UCHAR || t == USHORT) {
1537 } else if (t == CHAR || t == SCHAR || t == SHORT) {
1539 } else if (t == FLOAT) {
1541 } else if (t == ENUM) {
1546 if (t != tn->tn_type->t_tspec) {
1547 ntp = tduptyp(tn->tn_type);
1550 * Keep t_isenum so we are later able to check compatibility
1553 tn = convert(op, 0, ntp, tn);
1560 * Insert conversions which are necessary to give both operands the same
1561 * type. This is done in different ways for traditional C and ANIS C.
1564 balance(op, lnp, rnp)
1566 tnode_t **lnp, **rnp;
1571 static tspec_t tl[] = {
1572 LDOUBLE, DOUBLE, FLOAT, UQUAD, QUAD, ULONG, LONG, UINT, INT,
1575 lt = (*lnp)->tn_type->t_tspec;
1576 rt = (*rnp)->tn_type->t_tspec;
1578 if (!isatyp(lt) || !isatyp(rt))
1584 } else if (lt == LDOUBLE || rt == LDOUBLE) {
1586 } else if (lt == DOUBLE || rt == DOUBLE) {
1588 } else if (lt == FLOAT || rt == FLOAT) {
1592 * If type A has more bits than type B it should
1593 * be able to hold all possible values of type B.
1595 if (size(lt) > size(rt)) {
1597 } else if (size(lt) < size(rt)) {
1600 for (i = 3; tl[i] != INT; i++) {
1601 if (tl[i] == lt || tl[i] == rt)
1604 if ((isutyp(lt) || isutyp(rt)) &&
1612 /* Keep unsigned in traditional C */
1613 u = isutyp(lt) || isutyp(rt);
1614 for (i = 0; tl[i] != INT; i++) {
1615 if (lt == tl[i] || rt == tl[i])
1619 if (u && isityp(t) && !isutyp(t))
1624 ntp = tduptyp((*lnp)->tn_type);
1626 *lnp = convert(op, 0, ntp, *lnp);
1629 ntp = tduptyp((*rnp)->tn_type);
1631 *rnp = convert(op, 0, ntp, *rnp);
1636 * Insert a conversion operator, which converts the type of the node
1637 * to another given type.
1638 * If op is FARG, arg is the number of the argument (used for warnings).
1641 convert(op, arg, tp, tn)
1648 tspec_t nt, ot, ost;
1651 lerror("convert() 1");
1654 if ((ot = tn->tn_type->t_tspec) == PTR)
1655 ost = tn->tn_type->t_subt->t_tspec;
1657 if (!tflag && !sflag && op == FARG)
1658 ptconv(arg, nt, ot, tp, tn);
1659 if (isityp(nt) && isityp(ot)) {
1660 iiconv(op, arg, nt, ot, tp, tn);
1661 } else if (nt == PTR && ((ot == PTR && ost == VOID) || isityp(ot)) &&
1662 tn->tn_op == CON && tn->tn_val->v_quad == 0) {
1663 /* 0, 0L and (void *)0 may be assigned to any pointer. */
1664 } else if (isityp(nt) && ot == PTR) {
1665 piconv(op, nt, tp, tn);
1666 } else if (nt == PTR && ot == PTR) {
1673 ntn->tn_cast = op == CVT;
1674 if (tn->tn_op != CON || nt == VOID) {
1678 ntn->tn_val = tgetblk(sizeof (val_t));
1679 cvtcon(op, arg, ntn->tn_type, ntn->tn_val, tn->tn_val);
1686 * Print a warning if a prototype causes a type conversion that is
1687 * different from what would happen to the same argument in the
1688 * absence of a prototype.
1690 * Errors/Warnings about illegal type combinations are already printed
1694 ptconv(arg, nt, ot, tp, tn)
1702 if (!isatyp(nt) || !isatyp(ot))
1706 * If the type of the formal parameter is char/short, a warning
1707 * would be useless, because functions declared the old style
1708 * can't expect char/short arguments.
1710 if (nt == CHAR || nt == UCHAR || nt == SHORT || nt == USHORT)
1713 /* get default promotion */
1714 ptn = promote(NOOP, 1, tn);
1715 ot = ptn->tn_type->t_tspec;
1717 /* return if types are the same with and without prototype */
1718 if (nt == ot || (nt == ENUM && ot == INT))
1721 if (isftyp(nt) != isftyp(ot) || psize(nt) != psize(ot)) {
1722 /* representation and/or width change */
1723 if (styp(nt) != SHORT || !isityp(ot) || psize(ot) > psize(INT))
1724 /* conversion to '%s' due to prototype, arg #%d */
1725 warning(259, tyname(tp), arg);
1728 * they differ in sign or base type (char, short, int,
1729 * long, long long, float, double, long double)
1731 * if they differ only in sign and the argument is a constant
1732 * and the msb of the argument is not set, print no warning
1734 if (ptn->tn_op == CON && isityp(nt) && styp(nt) == styp(ot) &&
1735 msb(ptn->tn_val->v_quad, ot, -1) == 0) {
1738 /* conversion to '%s' due to prototype, arg #%d */
1739 warning(259, tyname(tp), arg);
1745 * Print warnings for conversions of integer types which my cause
1750 iiconv(op, arg, nt, ot, tp, tn)
1757 if (tn->tn_op == CON)
1764 if (psize(nt) > psize(ot) && isutyp(nt) != isutyp(ot)) {
1765 /* conversion to %s may sign-extend incorrectly (, arg #%d) */
1766 if (aflag && pflag) {
1768 warning(297, tyname(tp), arg);
1770 warning(131, tyname(tp));
1776 if (psize(nt) < psize(ot) &&
1777 (ot == LONG || ot == ULONG || ot == QUAD || ot == UQUAD ||
1779 /* conversion from '%s' may lose accuracy */
1782 warning(298, tyname(tn->tn_type), arg);
1784 warning(132, tyname(tn->tn_type));
1791 * Print warnings for dubious conversions of pointer to integer.
1794 piconv(op, nt, tp, tn)
1800 if (tn->tn_op == CON)
1804 /* We got already an error. */
1808 if (psize(nt) < psize(PTR)) {
1809 if (pflag && size(nt) >= size(PTR)) {
1810 /* conv. of pointer to %s may lose bits */
1811 warning(134, tyname(tp));
1813 /* conv. of pointer to %s loses bits */
1814 warning(133, tyname(tp));
1820 * Print warnings for questionable pointer conversions.
1829 const char *nts, *ots;
1832 * We got already an error (pointers of different types
1833 * without a cast) or we will not get a warning.
1838 nt = tp->t_subt->t_tspec;
1839 ot = tn->tn_type->t_subt->t_tspec;
1841 if (nt == VOID || ot == VOID) {
1842 if (sflag && (nt == FUNC || ot == FUNC)) {
1843 /* (void *)0 already handled in convert() */
1844 *(nt == FUNC ? &nts : &ots) = "function pointer";
1845 *(nt == VOID ? &nts : &ots) = "'void *'";
1846 /* ANSI C forbids conversion of %s to %s */
1847 warning(303, ots, nts);
1850 } else if (nt == FUNC && ot == FUNC) {
1852 } else if (nt == FUNC || ot == FUNC) {
1853 /* questionable conversion of function pointer */
1858 if (getbound(tp->t_subt) > getbound(tn->tn_type->t_subt)) {
1860 /* possible pointer alignment problem */
1863 if (((nt == STRUCT || nt == UNION) &&
1864 tp->t_subt->t_str != tn->tn_type->t_subt->t_str) ||
1865 psize(nt) != psize(ot)) {
1867 /* pointer casts may be troublesome */
1874 * Converts a typed constant in a constant of another type.
1876 * op operator which requires conversion
1877 * arg if op is FARG, # of argument
1878 * tp type in which to convert the constant
1883 cvtcon(op, arg, tp, nv, v)
1892 quad_t xmask, xmsk1;
1896 nt = nv->v_tspec = tp->t_tspec;
1899 if (ot == FLOAT || ot == DOUBLE || ot == LDOUBLE) {
1902 max = CHAR_MAX; min = CHAR_MIN; break;
1904 max = UCHAR_MAX; min = 0; break;
1906 max = SCHAR_MAX; min = SCHAR_MIN; break;
1908 max = SHRT_MAX; min = SHRT_MIN; break;
1910 max = USHRT_MAX; min = 0; break;
1913 max = INT_MAX; min = INT_MIN; break;
1915 max = (u_int)UINT_MAX; min = 0; break;
1917 max = LONG_MAX; min = LONG_MIN; break;
1919 max = (u_long)ULONG_MAX; min = 0; break;
1921 max = QUAD_MAX; min = QUAD_MIN; break;
1923 max = (u_quad_t)UQUAD_MAX; min = 0; break;
1925 max = FLT_MAX; min = -FLT_MAX; break;
1927 max = DBL_MAX; min = -DBL_MAX; break;
1929 /* Got already an error because of float --> ptr */
1931 max = LDBL_MAX; min = -LDBL_MAX; break;
1933 lerror("cvtcon() 1");
1935 if (v->v_ldbl > max || v->v_ldbl < min) {
1937 lerror("cvtcon() 2");
1939 /* conv. of %s to %s is out of rng., arg #%d */
1940 warning(295, tyname(gettyp(ot)), tyname(tp),
1943 /* conversion of %s to %s is out of range */
1944 warning(119, tyname(gettyp(ot)), tyname(tp));
1946 v->v_ldbl = v->v_ldbl > 0 ? max : min;
1949 nv->v_ldbl = (float)v->v_ldbl;
1950 } else if (nt == DOUBLE) {
1951 nv->v_ldbl = (double)v->v_ldbl;
1952 } else if (nt == LDOUBLE) {
1953 nv->v_ldbl = v->v_ldbl;
1955 nv->v_quad = (nt == PTR || isutyp(nt)) ?
1956 (u_quad_t)v->v_ldbl : (quad_t)v->v_ldbl;
1960 nv->v_ldbl = (ot == PTR || isutyp(ot)) ?
1961 (float)(u_quad_t)v->v_quad : (float)v->v_quad;
1962 } else if (nt == DOUBLE) {
1963 nv->v_ldbl = (ot == PTR || isutyp(ot)) ?
1964 (double)(u_quad_t)v->v_quad : (double)v->v_quad;
1965 } else if (nt == LDOUBLE) {
1966 nv->v_ldbl = (ot == PTR || isutyp(ot)) ?
1967 (ldbl_t)(u_quad_t)v->v_quad : (ldbl_t)v->v_quad;
1969 rchk = 1; /* Check for lost precision. */
1970 nv->v_quad = v->v_quad;
1974 if (v->v_ansiu && isftyp(nt)) {
1975 /* ANSI C treats constant as unsigned */
1978 } else if (v->v_ansiu && (isityp(nt) && !isutyp(nt) &&
1979 psize(nt) > psize(ot))) {
1980 /* ANSI C treats constant as unsigned */
1985 if (nt != FLOAT && nt != DOUBLE && nt != LDOUBLE) {
1986 sz = tp->t_isfield ? tp->t_flen : size(nt);
1987 nv->v_quad = xsign(nv->v_quad, nt, sz);
1990 if (rchk && op != CVT) {
1992 nsz = tp->t_isfield ? tp->t_flen : size(nt);
1993 xmask = qlmasks[nsz] ^ qlmasks[osz];
1994 xmsk1 = qlmasks[nsz] ^ qlmasks[osz - 1];
1996 * For bitwise operations we are not interested in the
1997 * value, but in the bits itself.
1999 if (op == ORASS || op == OR || op == XOR) {
2001 * Print a warning if bits which were set are
2002 * lost due to the conversion.
2003 * This can happen with operator ORASS only.
2005 if (nsz < osz && (v->v_quad & xmask) != 0) {
2006 /* constant truncated by conv., op %s */
2007 warning(306, modtab[op].m_name);
2009 } else if (op == ANDASS || op == AND) {
2011 * Print a warning if additional bits are not all 1
2012 * and the most significant bit of the old value is 1,
2013 * or if at least one (but not all) removed bit was 0.
2016 (nv->v_quad & qbmasks[osz - 1]) != 0 &&
2017 (nv->v_quad & xmask) != xmask) {
2019 * extra bits set to 0 in conversion
2020 * of '%s' to '%s', op %s
2022 warning(309, tyname(gettyp(ot)),
2023 tyname(tp), modtab[op].m_name);
2024 } else if (nsz < osz &&
2025 (v->v_quad & xmask) != xmask &&
2026 (v->v_quad & xmask) != 0) {
2027 /* const. truncated by conv., op %s */
2028 warning(306, modtab[op].m_name);
2030 } else if ((nt != PTR && isutyp(nt)) &&
2031 (ot != PTR && !isutyp(ot)) && v->v_quad < 0) {
2033 /* assignment of negative constant to ... */
2035 } else if (op == INIT) {
2036 /* initialisation of unsigned with neg. ... */
2038 } else if (op == FARG) {
2039 /* conversion of neg. const. to ..., arg #%d */
2041 } else if (modtab[op].m_comp) {
2042 /* we get this warning already in chkcomp() */
2044 /* conversion of negative constant to ... */
2047 } else if (nv->v_quad != v->v_quad && nsz <= osz &&
2048 (v->v_quad & xmask) != 0 &&
2049 (isutyp(ot) || (v->v_quad & xmsk1) != xmsk1)) {
2051 * Loss of significant bit(s). All truncated bits
2052 * of unsigned types or all truncated bits plus the
2053 * msb of the target for signed types are considered
2054 * to be significant bits. Loss of significant bits
2055 * means that at least on of the bits was set in an
2056 * unsigned type or that at least one, but not all of
2057 * the bits was set in an signed type.
2058 * Loss of significant bits means that it is not
2059 * possible, also not with necessary casts, to convert
2060 * back to the original type. A example for a
2061 * necessary cast is:
2062 * char c; int i; c = 128;
2063 * i = c; ** yields -128 **
2064 * i = (unsigned char)c; ** yields 128 **
2066 if (op == ASSIGN && tp->t_isfield) {
2067 /* precision lost in bit-field assignment */
2069 } else if (op == ASSIGN) {
2070 /* constant truncated by assignment */
2072 } else if (op == INIT && tp->t_isfield) {
2073 /* bit-field initializer does not fit */
2075 } else if (op == INIT) {
2076 /* initializer does not fit */
2078 } else if (op == CASE) {
2079 /* case label affected by conversion */
2081 } else if (op == FARG) {
2082 /* conv. of %s to %s is out of rng., arg #%d */
2083 warning(295, tyname(gettyp(ot)), tyname(tp),
2086 /* conversion of %s to %s is out of range */
2087 warning(119, tyname(gettyp(ot)), tyname(tp));
2089 } else if (nv->v_quad != v->v_quad) {
2090 if (op == ASSIGN && tp->t_isfield) {
2091 /* precision lost in bit-field assignment */
2093 } else if (op == INIT && tp->t_isfield) {
2094 /* bit-field initializer out of range */
2096 } else if (op == CASE) {
2097 /* case label affected by conversion */
2099 } else if (op == FARG) {
2100 /* conv. of %s to %s is out of rng., arg #%d */
2101 warning(295, tyname(gettyp(ot)), tyname(tp),
2104 /* conversion of %s to %s is out of range */
2105 warning(119, tyname(gettyp(ot)), tyname(tp));
2112 * Called if incompatible types were detected.
2113 * Prints a appropriate warning.
2116 incompat(op, lt, rt)
2124 if (lt == VOID || (mp->m_binary && rt == VOID)) {
2125 /* void type illegal in expression */
2127 } else if (op == ASSIGN) {
2128 if ((lt == STRUCT || lt == UNION) &&
2129 (rt == STRUCT || rt == UNION)) {
2130 /* assignment of different structures */
2133 /* assignment type mismatch */
2136 } else if (mp->m_binary) {
2137 /* operands of %s have incompatible types */
2138 error(107, mp->m_name);
2140 /* operand of %s has incompatible type */
2141 error(108, mp->m_name);
2146 * Called if incompatible pointer types are detected.
2147 * Print an appropriate warning.
2150 illptrc(mp, ltp, rtp)
2156 if (ltp->t_tspec != PTR || rtp->t_tspec != PTR)
2157 lerror("illptrc() 1");
2159 lt = ltp->t_subt->t_tspec;
2160 rt = rtp->t_subt->t_tspec;
2162 if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION)) {
2164 /* illegal structure pointer combination */
2167 /* illegal structure pointer combination, op %s */
2168 warning(245, mp->m_name);
2172 /* illegal pointer combination */
2175 /* illegal pointer combination, op %s */
2176 warning(124, mp->m_name);
2182 * Make sure type (*tpp)->t_subt has at least the qualifiers
2183 * of tp1->t_subt and tp2->t_subt.
2186 mrgqual(tpp, tp1, tp2)
2187 type_t **tpp, *tp1, *tp2;
2189 if ((*tpp)->t_tspec != PTR ||
2190 tp1->t_tspec != PTR || tp2->t_tspec != PTR) {
2191 lerror("mrgqual()");
2194 if ((*tpp)->t_subt->t_const ==
2195 (tp1->t_subt->t_const | tp2->t_subt->t_const) &&
2196 (*tpp)->t_subt->t_volatile ==
2197 (tp1->t_subt->t_volatile | tp2->t_subt->t_volatile)) {
2201 *tpp = tduptyp(*tpp);
2202 (*tpp)->t_subt = tduptyp((*tpp)->t_subt);
2203 (*tpp)->t_subt->t_const =
2204 tp1->t_subt->t_const | tp2->t_subt->t_const;
2205 (*tpp)->t_subt->t_volatile =
2206 tp1->t_subt->t_volatile | tp2->t_subt->t_volatile;
2210 * Returns 1 if the given structure or union has a constant member
2211 * (maybe recursively).
2220 if ((t = tp->t_tspec) != STRUCT && t != UNION)
2221 lerror("conmemb()");
2222 for (m = tp->t_str->memb; m != NULL; m = m->s_nxt) {
2226 if ((t = tp->t_tspec) == STRUCT || t == UNION) {
2227 if (conmemb(m->s_type))
2241 if ((t = tp->t_tspec) == INT && tp->t_isenum)
2245 case CHAR: s = "char"; break;
2246 case UCHAR: s = "unsigned char"; break;
2247 case SCHAR: s = "signed char"; break;
2248 case SHORT: s = "short"; break;
2249 case USHORT: s = "unsigned short"; break;
2250 case INT: s = "int"; break;
2251 case UINT: s = "unsigned int"; break;
2252 case LONG: s = "long"; break;
2253 case ULONG: s = "unsigned long"; break;
2254 case QUAD: s = "long long"; break;
2255 case UQUAD: s = "unsigned long long"; break;
2256 case FLOAT: s = "float"; break;
2257 case DOUBLE: s = "double"; break;
2258 case LDOUBLE: s = "long double"; break;
2259 case PTR: s = "pointer"; break;
2260 case ENUM: s = "enum"; break;
2261 case STRUCT: s = "struct"; break;
2262 case UNION: s = "union"; break;
2263 case FUNC: s = "function"; break;
2264 case ARRAY: s = "array"; break;
2272 * Create a new node for one of the operators POINT and ARROW.
2282 if (rn->tn_op != NAME)
2283 lerror("bldstr() 1");
2284 if (rn->tn_sym->s_value.v_tspec != INT)
2285 lerror("bldstr() 2");
2286 if (rn->tn_sym->s_scl != MOS && rn->tn_sym->s_scl != MOU)
2287 lerror("bldstr() 3");
2290 * Remember if the left operand is an lvalue (structure members
2291 * are lvalues if and only if the structure itself is an lvalue).
2293 nolval = op == POINT && !ln->tn_lvalue;
2296 ln = bldamper(ln, 1);
2297 } else if (ln->tn_type->t_tspec != PTR) {
2298 if (!tflag || !isityp(ln->tn_type->t_tspec))
2299 lerror("bldstr() 4");
2300 ln = convert(NOOP, 0, tincref(gettyp(VOID), PTR), ln);
2304 ctn = getinode(LONG, rn->tn_sym->s_value.v_quad / CHAR_BIT);
2306 ctn = getinode(INT, rn->tn_sym->s_value.v_quad / CHAR_BIT);
2309 ntn = mktnode(PLUS, tincref(rn->tn_type, PTR), ln, ctn);
2310 if (ln->tn_op == CON)
2313 if (rn->tn_type->t_isfield) {
2314 ntn = mktnode(FSEL, ntn->tn_type->t_subt, ntn, NULL);
2316 ntn = mktnode(STAR, ntn->tn_type->t_subt, ntn, NULL);
2326 * Create a node for INCAFT, INCBEF, DECAFT and DECBEF.
2336 lerror("bldincdec() 1");
2338 if (ln->tn_type->t_tspec == PTR) {
2339 cn = plength(ln->tn_type);
2341 cn = getinode(INT, (quad_t)1);
2343 ntn = mktnode(op, ln->tn_type, ln, cn);
2349 * Create a tree node for the & operator
2359 if (!noign && ((t = tn->tn_type->t_tspec) == ARRAY || t == FUNC)) {
2360 /* & before array or function: ignored */
2367 if (tn->tn_op == STAR &&
2368 tn->tn_left->tn_type->t_tspec == PTR &&
2369 tn->tn_left->tn_type->t_subt == tn->tn_type) {
2370 return (tn->tn_left);
2373 ntn = mktnode(AMPER, tincref(tn->tn_type, PTR), tn, NULL);
2379 * Create a node for operators PLUS and MINUS.
2389 /* If pointer and integer, then pointer to the lhs. */
2390 if (rn->tn_type->t_tspec == PTR && isityp(ln->tn_type->t_tspec)) {
2396 if (ln->tn_type->t_tspec == PTR && rn->tn_type->t_tspec != PTR) {
2398 if (!isityp(rn->tn_type->t_tspec))
2399 lerror("bldplmi() 1");
2401 ctn = plength(ln->tn_type);
2402 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
2403 rn = convert(NOOP, 0, ctn->tn_type, rn);
2404 rn = mktnode(MULT, rn->tn_type, rn, ctn);
2405 if (rn->tn_left->tn_op == CON)
2407 ntn = mktnode(op, ln->tn_type, ln, rn);
2409 } else if (rn->tn_type->t_tspec == PTR) {
2411 if (ln->tn_type->t_tspec != PTR || op != MINUS)
2412 lerror("bldplmi() 2");
2418 ntn = mktnode(op, tp, ln, rn);
2419 if (ln->tn_op == CON && rn->tn_op == CON)
2421 ctn = plength(ln->tn_type);
2422 balance(NOOP, &ntn, &ctn);
2423 ntn = mktnode(DIV, tp, ntn, ctn);
2427 ntn = mktnode(op, ln->tn_type, ln, rn);
2434 * Create a node for operators SHL and SHR.
2444 if ((t = rn->tn_type->t_tspec) != INT && t != UINT)
2445 rn = convert(CVT, 0, gettyp(INT), rn);
2446 ntn = mktnode(op, ln->tn_type, ln, rn);
2451 * Create a node for COLON.
2457 tspec_t lt, rt, pdt;
2461 lt = ln->tn_type->t_tspec;
2462 rt = rn->tn_type->t_tspec;
2470 * Arithmetic types are balanced, all other type combinations
2471 * still need to be handled.
2473 if (isatyp(lt) && isatyp(rt)) {
2475 } else if (lt == VOID || rt == VOID) {
2477 } else if (lt == STRUCT || lt == UNION) {
2478 /* Both types must be identical. */
2479 if (rt != STRUCT && rt != UNION)
2480 lerror("bldcol() 1");
2481 if (ln->tn_type->t_str != rn->tn_type->t_str)
2482 lerror("bldcol() 2");
2483 if (incompl(ln->tn_type)) {
2484 /* unknown operand size, op %s */
2485 error(138, modtab[COLON].m_name);
2489 } else if (lt == PTR && isityp(rt)) {
2491 rn = convert(NOOP, 0, gettyp(pdt), rn);
2495 } else if (rt == PTR && isityp(lt)) {
2497 ln = convert(NOOP, 0, gettyp(pdt), ln);
2501 } else if (lt == PTR && ln->tn_type->t_subt->t_tspec == VOID) {
2503 lerror("bldcol() 4");
2505 mrgqual(&rtp, ln->tn_type, rn->tn_type);
2506 } else if (rt == PTR && rn->tn_type->t_subt->t_tspec == VOID) {
2508 lerror("bldcol() 5");
2510 mrgqual(&rtp, ln->tn_type, rn->tn_type);
2512 if (lt != PTR || rt != PTR)
2513 lerror("bldcol() 6");
2515 * XXX For now we simply take the left type. This is
2516 * probably wrong, if one type contains a functionprototype
2517 * and the other one, at the same place, only an old style
2521 mrgqual(&rtp, ln->tn_type, rn->tn_type);
2524 ntn = mktnode(COLON, rtp, ln, rn);
2530 * Create a node for an assignment operator (both = and op= ).
2540 if (ln == NULL || rn == NULL)
2541 lerror("bldasgn() 1");
2543 lt = ln->tn_type->t_tspec;
2544 rt = rn->tn_type->t_tspec;
2546 if ((op == ADDASS || op == SUBASS) && lt == PTR) {
2548 lerror("bldasgn() 2");
2549 ctn = plength(ln->tn_type);
2550 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
2551 rn = convert(NOOP, 0, ctn->tn_type, rn);
2552 rn = mktnode(MULT, rn->tn_type, rn, ctn);
2553 if (rn->tn_left->tn_op == CON)
2557 if ((op == ASSIGN || op == RETURN) && (lt == STRUCT || rt == STRUCT)) {
2558 if (rt != lt || ln->tn_type->t_str != rn->tn_type->t_str)
2559 lerror("bldasgn() 3");
2560 if (incompl(ln->tn_type)) {
2562 /* cannot return incomplete type */
2565 /* unknown operand size, op %s */
2566 error(138, modtab[op].m_name);
2572 if (op == SHLASS || op == SHRASS) {
2574 rn = convert(NOOP, 0, gettyp(INT), rn);
2578 if (op == ASSIGN || lt != PTR) {
2580 (ln->tn_type->t_isfield && rn->tn_op == CON)) {
2581 rn = convert(op, 0, ln->tn_type, rn);
2587 ntn = mktnode(op, ln->tn_type, ln, rn);
2593 * Get length of type tp->t_subt.
2602 if (tp->t_tspec != PTR)
2603 lerror("plength() 1");
2609 while (tp->t_tspec == ARRAY) {
2614 switch (tp->t_tspec) {
2616 /* pointer to function is not allowed here */
2620 /* cannot do pointer arithmetic on operand of ... */
2625 if ((elsz = tp->t_str->size) == 0)
2626 /* cannot do pointer arithmetic on operand of ... */
2631 /* cannot do pointer arithmetic on operand of ... */
2636 if ((elsz = size(tp->t_tspec)) == 0) {
2637 /* cannot do pointer arithmetic on operand of ... */
2639 } else if (elsz == -1) {
2640 lerror("plength() 2");
2645 if (elem == 0 && elsz != 0) {
2646 /* cannot do pointer arithmetic on operand of ... */
2659 return (getinode(st, (quad_t)(elem * elsz / CHAR_BIT)));
2662 #ifdef XXX_BROKEN_GCC
2679 * Do only as much as necessary to compute constant expressions.
2680 * Called only if the operator allows folding and (both) operands
2690 quad_t sl, sr, q, mask;
2694 v = xcalloc(1, sizeof (val_t));
2695 v->v_tspec = t = tn->tn_type->t_tspec;
2697 utyp = t == PTR || isutyp(t);
2698 ul = sl = tn->tn_left->tn_val->v_quad;
2699 if (modtab[tn->tn_op].m_binary)
2700 ur = sr = tn->tn_right->tn_val->v_quad;
2704 switch (tn->tn_op) {
2710 if (msb(q, t, -1) == msb(sl, t, -1))
2717 q = utyp ? ul * ur : sl * sr;
2718 if (msb(q, t, -1) != (msb(sl, t, -1) ^ msb(sr, t, -1)))
2725 q = utyp ? UQUAD_MAX : QUAD_MAX;
2727 q = utyp ? ul / ur : sl / sr;
2736 q = utyp ? ul % ur : sl % sr;
2740 q = utyp ? ul + ur : sl + sr;
2741 if (msb(sl, t, -1) != 0 && msb(sr, t, -1) != 0) {
2742 if (msb(q, t, -1) == 0)
2744 } else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) == 0) {
2745 if (msb(q, t, -1) != 0)
2750 q = utyp ? ul - ur : sl - sr;
2751 if (msb(sl, t, -1) != 0 && msb(sr, t, -1) == 0) {
2752 if (msb(q, t, -1) == 0)
2754 } else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) != 0) {
2755 if (msb(q, t, -1) != 0)
2760 q = utyp ? ul << sr : sl << sr;
2764 * The sign must be explizitly extended because
2765 * shifts of signed values are implementation dependent.
2768 q = xsign(q, t, size(t) - (int)sr);
2771 q = utyp ? ul < ur : sl < sr;
2774 q = utyp ? ul <= ur : sl <= sr;
2777 q = utyp ? ul >= ur : sl >= sr;
2780 q = utyp ? ul > ur : sl > sr;
2783 #ifdef XXX_BROKEN_GCC
2784 q = utyp ? u_quad_t_eq(ul, ur) : quad_t_eq(sl, sr);
2786 q = utyp ? ul == ur : sl == sr;
2790 q = utyp ? ul != ur : sl != sr;
2793 q = utyp ? ul & ur : sl & sr;
2796 q = utyp ? ul ^ ur : sl ^ sr;
2799 q = utyp ? ul | ur : sl | sr;
2805 mask = qlmasks[size(t)];
2807 /* XXX does not work for quads. */
2808 if (ovfl || ((q | mask) != ~(u_quad_t)0 && (q & ~mask) != 0)) {
2810 /* integer overflow detected, op %s */
2811 warning(141, modtab[tn->tn_op].m_name);
2814 v->v_quad = xsign(q, t, -1);
2816 cn = getcnode(tn->tn_type, v);
2821 #ifdef XXX_BROKEN_GCC
2831 * Same for operators whose operands are compared with 0 (test context).
2840 v = xcalloc(1, sizeof (val_t));
2841 v->v_tspec = tn->tn_type->t_tspec;
2842 if (tn->tn_type->t_tspec != INT)
2843 lerror("foldtst() 1");
2845 if (isftyp(tn->tn_left->tn_type->t_tspec)) {
2846 #ifdef XXX_BROKEN_GCC
2847 l = ldbl_t_neq(tn->tn_left->tn_val->v_ldbl, 0.0);
2849 l = tn->tn_left->tn_val->v_ldbl != 0.0;
2852 l = tn->tn_left->tn_val->v_quad != 0;
2855 if (modtab[tn->tn_op].m_binary) {
2856 if (isftyp(tn->tn_right->tn_type->t_tspec)) {
2857 #ifdef XXX_BROKEN_GCC
2858 r = ldbl_t_neq(tn->tn_right->tn_val->v_ldbl, 0.0);
2860 r = tn->tn_right->tn_val->v_ldbl != 0.0;
2863 r = tn->tn_right->tn_val->v_quad != 0;
2867 switch (tn->tn_op) {
2870 /* constant argument to NOT */
2881 lerror("foldtst() 1");
2884 return (getcnode(tn->tn_type, v));
2888 * Same for operands with floating point type.
2898 v = xcalloc(1, sizeof (val_t));
2899 v->v_tspec = t = tn->tn_type->t_tspec;
2902 lerror("foldflt() 1");
2904 if (t != tn->tn_left->tn_type->t_tspec)
2905 lerror("foldflt() 2");
2906 if (modtab[tn->tn_op].m_binary && t != tn->tn_right->tn_type->t_tspec)
2907 lerror("foldflt() 3");
2909 l = tn->tn_left->tn_val->v_ldbl;
2910 if (modtab[tn->tn_op].m_binary)
2911 r = tn->tn_right->tn_val->v_ldbl;
2913 switch (tn->tn_op) {
2928 v->v_ldbl = l < 0 ? -FLT_MAX : FLT_MAX;
2929 } else if (t == DOUBLE) {
2930 v->v_ldbl = l < 0 ? -DBL_MAX : DBL_MAX;
2932 v->v_ldbl = l < 0 ? -LDBL_MAX : LDBL_MAX;
2963 lerror("foldflt() 4");
2966 if (isnan((double)v->v_ldbl))
2967 lerror("foldflt() 5");
2968 if (isinf((double)v->v_ldbl) ||
2970 (v->v_ldbl > FLT_MAX || v->v_ldbl < -FLT_MAX)) ||
2972 (v->v_ldbl > DBL_MAX || v->v_ldbl < -DBL_MAX))) {
2973 /* floating point overflow detected, op %s */
2974 warning(142, modtab[tn->tn_op].m_name);
2976 v->v_ldbl = v->v_ldbl < 0 ? -FLT_MAX : FLT_MAX;
2977 } else if (t == DOUBLE) {
2978 v->v_ldbl = v->v_ldbl < 0 ? -DBL_MAX : DBL_MAX;
2980 v->v_ldbl = v->v_ldbl < 0 ? -LDBL_MAX: LDBL_MAX;
2984 return (getcnode(tn->tn_type, v));
2988 * Create a constant node for sizeof.
2998 while (tp->t_tspec == ARRAY) {
3003 /* cannot take size of incomplete type */
3007 switch (tp->t_tspec) {
3009 /* cannot take size of function */
3016 /* cannot take size of incomplete type */
3020 elsz = tp->t_str->size;
3025 /* cannot take size of incomplete type */
3030 if (tp->t_isfield) {
3031 /* cannot take size of bit-field */
3034 if (tp->t_tspec == VOID) {
3035 /* cannot take size of void */
3039 elsz = size(tp->t_tspec);
3041 lerror("bldszof() 1");
3052 return (getinode(st, (quad_t)(elem * elsz / CHAR_BIT)));
3071 ot = tn->tn_type->t_tspec;
3075 * XXX ANSI C requires scalar types or void (Plauger&Brodie).
3076 * But this seams really questionable.
3078 } else if (nt == STRUCT || nt == UNION || nt == ARRAY || nt == FUNC) {
3079 /* invalid cast expression */
3082 } else if (ot == STRUCT || ot == UNION) {
3083 /* invalid cast expression */
3086 } else if (ot == VOID) {
3087 /* improper cast of void expression */
3090 } else if (isityp(nt) && issclt(ot)) {
3092 } else if (isftyp(nt) && isatyp(ot)) {
3094 } else if (nt == PTR && isityp(ot)) {
3096 } else if (nt == PTR && ot == PTR) {
3097 if (!tp->t_subt->t_const && tn->tn_type->t_subt->t_const) {
3099 /* cast discards 'const' from ... */
3103 /* invalid cast expression */
3108 tn = convert(CVT, 0, tp, tn);
3115 * Create the node for a function argument.
3116 * All necessary conversions and type checks are done in funccall(), because
3117 * in funcarg() we have no information about expected argument types.
3121 tnode_t *args, *arg;
3126 * If there was a serious error in the expression for the argument,
3127 * create a dummy argument so the positions of the remaining arguments
3131 arg = getinode(INT, (quad_t)0);
3133 ntn = mktnode(PUSH, arg->tn_type, arg, args);
3139 * Create the node for a function call. Also check types of
3140 * function arguments and insert conversions, if necessary.
3143 funccall(func, args)
3144 tnode_t *func, *args;
3152 if (func->tn_op == NAME && func->tn_type->t_tspec == FUNC) {
3159 * after cconv() func will always be a pointer to a function
3160 * if it is a valid function designator.
3164 if (func->tn_type->t_tspec != PTR ||
3165 func->tn_type->t_subt->t_tspec != FUNC) {
3166 /* illegal function */
3171 args = chkfarg(func->tn_type->t_subt, args);
3173 ntn = mktnode(fcop, func->tn_type->t_subt->t_subt, func, args);
3179 * Check types of all function arguments and insert conversions,
3184 type_t *ftp; /* type of called function */
3185 tnode_t *args; /* arguments */
3190 int narg, npar, n, i;
3192 /* get # of args in the prototype */
3194 for (asym = ftp->t_args; asym != NULL; asym = asym->s_nxt)
3197 /* get # of args in function call */
3199 for (arg = args; arg != NULL; arg = arg->tn_right)
3203 if (ftp->t_proto && npar != narg && !(ftp->t_vararg && npar < narg)) {
3204 /* argument mismatch: %d arg%s passed, %d expected */
3205 error(150, narg, narg > 1 ? "s" : "", npar);
3209 for (n = 1; n <= narg; n++) {
3212 * The rightmost argument is at the top of the argument
3215 for (i = narg, arg = args; i > n; i--, arg = arg->tn_right) ;
3217 /* some things which are always not allowd */
3218 if ((at = arg->tn_left->tn_type->t_tspec) == VOID) {
3219 /* void expressions may not be arguments, arg #%d */
3222 } else if ((at == STRUCT || at == UNION) &&
3223 incompl(arg->tn_left->tn_type)) {
3224 /* argument cannot have unknown size, arg #%d */
3227 } else if (isityp(at) && arg->tn_left->tn_type->t_isenum &&
3228 incompl(arg->tn_left->tn_type)) {
3229 /* argument cannot have unknown size, arg #%d */
3233 /* class conversions (arg in value context) */
3234 arg->tn_left = cconv(arg->tn_left);
3237 arg->tn_left = parg(n, asym->s_type, arg->tn_left);
3239 arg->tn_left = promote(NOOP, 1, arg->tn_left);
3241 arg->tn_type = arg->tn_left->tn_type;
3251 * Compare the type of an argument with the corresponding type of a
3252 * prototype parameter. If it is a valid combination, but both types
3253 * are not the same, insert a conversion to convert the argument into
3254 * the type of the parameter.
3258 int n; /* pos of arg */
3259 type_t *tp; /* expected type (from prototype) */
3260 tnode_t *tn; /* argument */
3265 ln = xcalloc(1, sizeof (tnode_t));
3266 ln->tn_type = tduptyp(tp);
3267 ln->tn_type->t_const = 0;
3269 if (typeok(FARG, n, ln, tn)) {
3270 if (!eqtype(tp, tn->tn_type, 1, 0, (warn = 0, &warn)) || warn)
3271 tn = convert(FARG, n, tp, tn);
3278 * Return the value of an integral constant expression.
3279 * If the expression is not constant or its type is not an integer
3280 * type, an error message is printed.
3291 tn = promote(NOOP, 0, tn);
3293 v = xcalloc(1, sizeof (val_t));
3297 lerror("constant() 1");
3303 v->v_tspec = tn->tn_type->t_tspec;
3305 if (tn->tn_op == CON) {
3306 if (tn->tn_type->t_tspec != tn->tn_val->v_tspec)
3307 lerror("constant() 2");
3308 if (isityp(tn->tn_val->v_tspec)) {
3309 v->v_ansiu = tn->tn_val->v_ansiu;
3310 v->v_quad = tn->tn_val->v_quad;
3313 v->v_quad = tn->tn_val->v_ldbl;
3318 /* integral constant expression expected */
3321 if (!isityp(v->v_tspec))
3328 * Perform some tests on expressions which can't be done in build() and
3329 * functions called by build(). These tests must be done here because
3330 * we need some information about the context in which the operations
3332 * After all tests are performed, expr() frees the memory which is used
3333 * for the expression.
3336 expr(tn, vctx, tctx)
3340 if (tn == NULL && nerr == 0)
3348 /* expr() is also called in global initialisations */
3349 if (dcs->d_ctx != EXTERN)
3352 chkmisc(tn, vctx, tctx, !tctx, 0, 0, 0);
3353 if (tn->tn_op == ASSIGN) {
3355 /* assignment in conditional context */
3357 } else if (tn->tn_op == CON) {
3358 if (hflag && tctx && !ccflg)
3359 /* constant in conditional context */
3362 if (!modtab[tn->tn_op].m_sideeff) {
3364 * for left operands of COMMA this warning is already
3367 if (tn->tn_op != COMMA && !vctx && !tctx)
3373 /* free the tree memory */
3384 while (!modtab[tn->tn_op].m_sideeff) {
3385 if (tn->tn_op == CVT && tn->tn_type->t_tspec == VOID) {
3387 } else if (tn->tn_op == LOGAND || tn->tn_op == LOGOR) {
3389 * && and || have a side effect if the right operand
3390 * has a side effect.
3393 } else if (tn->tn_op == QUEST) {
3395 * ? has a side effect if at least one of its right
3396 * operands has a side effect
3399 } else if (tn->tn_op == COLON) {
3401 * : has a side effect if at least one of its operands
3404 if (modtab[tn->tn_left->tn_op].m_sideeff) {
3406 } else if (modtab[tn->tn_right->tn_op].m_sideeff) {
3415 if (!modtab[tn->tn_op].m_sideeff)
3416 /* expression has null effect */
3421 * Dump an expression to stdout
3422 * only used for debugging
3432 (void)printf("%*s%s\n", offs, "", "NULL");
3435 (void)printf("%*sop %s ", offs, "", modtab[tn->tn_op].m_name);
3437 if (tn->tn_op == NAME) {
3438 (void)printf("%s: %s ",
3439 tn->tn_sym->s_name, scltoa(tn->tn_sym->s_scl));
3440 } else if (tn->tn_op == CON && isftyp(tn->tn_type->t_tspec)) {
3441 (void)printf("%#g ", (double)tn->tn_val->v_ldbl);
3442 } else if (tn->tn_op == CON && isityp(tn->tn_type->t_tspec)) {
3443 uq = tn->tn_val->v_quad;
3444 (void)printf("0x %08lx %08lx ", (long)(uq >> 32) & 0xffffffffl,
3445 (long)uq & 0xffffffffl);
3446 } else if (tn->tn_op == CON) {
3447 if (tn->tn_type->t_tspec != PTR)
3448 lerror("displexpr() 1");
3449 (void)printf("0x%0*lx ", (int)(sizeof (void *) * CHAR_BIT / 4),
3450 (u_long)tn->tn_val->v_quad);
3451 } else if (tn->tn_op == STRING) {
3452 if (tn->tn_strg->st_tspec == CHAR) {
3453 (void)printf("\"%s\"", tn->tn_strg->st_cp);
3457 n = MB_CUR_MAX * (tn->tn_strg->st_len + 1);
3459 (void)wcstombs(s, tn->tn_strg->st_wcp, n);
3460 (void)printf("L\"%s\"", s);
3464 } else if (tn->tn_op == FSEL) {
3465 (void)printf("o=%d, l=%d ", tn->tn_type->t_foffs,
3466 tn->tn_type->t_flen);
3468 (void)printf("%s\n", ttos(tn->tn_type));
3469 if (tn->tn_op == NAME || tn->tn_op == CON || tn->tn_op == STRING)
3471 displexpr(tn->tn_left, offs + 2);
3472 if (modtab[tn->tn_op].m_binary ||
3473 (tn->tn_op == PUSH && tn->tn_right != NULL)) {
3474 displexpr(tn->tn_right, offs + 2);
3479 * Called by expr() to recursively perform some tests.
3483 chkmisc(tn, vctx, tctx, eqwarn, fcall, rvdisc, szof)
3485 int vctx, tctx, eqwarn, fcall, rvdisc, szof;
3489 int nrvdisc, cvctx, ctctx;
3499 mp = &modtab[op = tn->tn_op];
3503 if (ln->tn_op == NAME && (reached || rchflg)) {
3505 setsflg(ln->tn_sym);
3506 setuflg(ln->tn_sym, fcall, szof);
3508 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3509 /* check the range of array indices */
3510 chkaidx(ln->tn_left, 1);
3513 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3514 /* check the range of array indices */
3515 chkaidx(ln->tn_left, 0);
3532 if (ln->tn_op == NAME && (reached || rchflg)) {
3533 sc = ln->tn_sym->s_scl;
3535 * Look if there was a asm statement in one of the
3536 * compound statements we are in. If not, we don't
3539 for (di = dcs; di != NULL; di = di->d_nxt) {
3543 if (sc != EXTERN && sc != STATIC &&
3544 !ln->tn_sym->s_set && !szof && di == NULL) {
3545 /* %s may be used before set */
3546 warning(158, ln->tn_sym->s_name);
3547 setsflg(ln->tn_sym);
3549 setuflg(ln->tn_sym, 0, 0);
3553 if (ln->tn_op == NAME && !szof && (reached || rchflg)) {
3554 setsflg(ln->tn_sym);
3555 if (ln->tn_sym->s_scl == EXTERN)
3558 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3559 /* check the range of array indices */
3560 chkaidx(ln->tn_left, 0);
3563 if (ln->tn_op != AMPER || ln->tn_left->tn_op != NAME)
3564 lerror("chkmisc() 1");
3566 outcall(tn, vctx || tctx, rvdisc);
3569 /* equality operator "==" found where "=" was exp. */
3570 if (hflag && eqwarn)
3577 /* LINTED (enumeration values not handled in switch) */
3584 * values of operands of ':' are not used if the type of at least
3585 * one of the operands (for gcc compatibility) is void
3586 * XXX test/value context of QUEST should probably be used as
3587 * context for both operands of COLON
3589 if (op == COLON && tn->tn_type->t_tspec == VOID)
3591 nrvdisc = op == CVT && tn->tn_type->t_tspec == VOID;
3592 chkmisc(ln, cvctx, ctctx, mp->m_eqwarn, op == CALL, nrvdisc, szof);
3597 chkmisc(rn, 0, 0, mp->m_eqwarn, 0, 0, szof);
3601 chkmisc(rn, 0, 1, mp->m_eqwarn, 0, 0, szof);
3604 chkmisc(rn, cvctx, ctctx, mp->m_eqwarn, 0, 0, szof);
3608 chkmisc(rn, 1, 0, mp->m_eqwarn, 0, 0, szof);
3615 * Checks the range of array indices, if possible.
3616 * amper is set if only the address of the element is used. This
3617 * means that the index is allowd to refere to the first element
3633 /* We can only check constant indices. */
3634 if (rn->tn_op != CON)
3637 /* Return if the left node does not stem from an array. */
3638 if (ln->tn_op != AMPER)
3640 if (ln->tn_left->tn_op != STRING && ln->tn_left->tn_op != NAME)
3642 if (ln->tn_left->tn_type->t_tspec != ARRAY)
3646 * For incomplete array types, we can print a warning only if
3647 * the index is negative.
3649 if (incompl(ln->tn_left->tn_type) && rn->tn_val->v_quad >= 0)
3652 /* Get the size of one array element */
3653 if ((elsz = length(ln->tn_type->t_subt, NULL)) == 0)
3657 /* Change the unit of the index from bytes to element size. */
3658 if (isutyp(rn->tn_type->t_tspec)) {
3659 con = (u_quad_t)rn->tn_val->v_quad / elsz;
3661 con = rn->tn_val->v_quad / elsz;
3664 dim = ln->tn_left->tn_type->t_dim + (amper ? 1 : 0);
3666 if (!isutyp(rn->tn_type->t_tspec) && con < 0) {
3667 /* array subscript cannot be negative: %ld */
3668 warning(167, (long)con);
3669 } else if (dim > 0 && (u_quad_t)con >= dim) {
3670 /* array subscript cannot be > %d: %ld */
3671 warning(168, dim - 1, (long)con);
3676 * Check for ordered comparisions of unsigned values with 0.
3686 lt = ln->tn_type->t_tspec;
3687 rt = rn->tn_type->t_tspec;
3690 if (ln->tn_op != CON && rn->tn_op != CON)
3693 if (!isityp(lt) || !isityp(rt))
3696 if ((hflag || pflag) && lt == CHAR && rn->tn_op == CON &&
3697 (rn->tn_val->v_quad < 0 ||
3698 rn->tn_val->v_quad > ~(~0 << (CHAR_BIT - 1)))) {
3699 /* nonportable character comparision, op %s */
3700 warning(230, mp->m_name);
3703 if ((hflag || pflag) && rt == CHAR && ln->tn_op == CON &&
3704 (ln->tn_val->v_quad < 0 ||
3705 ln->tn_val->v_quad > ~(~0 << (CHAR_BIT - 1)))) {
3706 /* nonportable character comparision, op %s */
3707 warning(230, mp->m_name);
3710 if (isutyp(lt) && !isutyp(rt) &&
3711 rn->tn_op == CON && rn->tn_val->v_quad <= 0) {
3712 if (rn->tn_val->v_quad < 0) {
3713 /* comparision of %s with %s, op %s */
3714 warning(162, tyname(ln->tn_type), "negative constant",
3716 } else if (op == LT || op == GE || (hflag && op == LE)) {
3717 /* comparision of %s with %s, op %s */
3718 warning(162, tyname(ln->tn_type), "0", mp->m_name);
3722 if (isutyp(rt) && !isutyp(lt) &&
3723 ln->tn_op == CON && ln->tn_val->v_quad <= 0) {
3724 if (ln->tn_val->v_quad < 0) {
3725 /* comparision of %s with %s, op %s */
3726 warning(162, "negative constant", tyname(rn->tn_type),
3728 } else if (op == GT || op == LE || (hflag && op == GE)) {
3729 /* comparision of %s with %s, op %s */
3730 warning(162, "0", tyname(rn->tn_type), mp->m_name);
3737 * Takes an expression an returns 0 if this expression can be used
3738 * for static initialisation, otherwise -1.
3740 * Constant initialisation expressions must be costant or an address
3741 * of a static object with an optional offset. In the first case,
3742 * the result is returned in *offsp. In the second case, the static
3743 * object is returned in *symp and the offset in *offsp.
3745 * The expression can consist of PLUS, MINUS, AMPER, NAME, STRING and
3746 * CON. Type conversions are allowed if they do not change binary
3747 * representation (including width).
3750 conaddr(tn, symp, offsp)
3756 ptrdiff_t offs1, offs2;
3759 switch (tn->tn_op) {
3761 if (tn->tn_right->tn_op != CON)
3766 if (tn->tn_left->tn_op == CON) {
3767 offs1 = (ptrdiff_t)tn->tn_left->tn_val->v_quad;
3768 if (conaddr(tn->tn_right, &sym, &offs2) == -1)
3770 } else if (tn->tn_right->tn_op == CON) {
3771 offs2 = (ptrdiff_t)tn->tn_right->tn_val->v_quad;
3772 if (tn->tn_op == MINUS)
3774 if (conaddr(tn->tn_left, &sym, &offs1) == -1)
3780 *offsp = offs1 + offs2;
3783 if (tn->tn_left->tn_op == NAME) {
3784 *symp = tn->tn_left->tn_sym;
3786 } else if (tn->tn_left->tn_op == STRING) {
3788 * If this would be the front end of a compiler we
3789 * would return a label instead of 0.
3795 t = tn->tn_type->t_tspec;
3796 ot = tn->tn_left->tn_type->t_tspec;
3797 if ((!isityp(t) && t != PTR) || (!isityp(ot) && ot != PTR)) {
3799 } else if (psize(t) != psize(ot)) {
3802 if (conaddr(tn->tn_left, symp, offsp) == -1)
3812 * Concatenate two string constants.
3815 catstrg(strg1, strg2)
3816 strg_t *strg1, *strg2;
3818 size_t len1, len2, len;
3820 if (strg1->st_tspec != strg2->st_tspec) {
3821 /* cannot concatenate wide and regular string literals */
3826 len = (len1 = strg1->st_len) + (len2 = strg2->st_len);
3828 if (strg1->st_tspec == CHAR) {
3829 strg1->st_cp = xrealloc(strg1->st_cp, len + 1);
3830 (void)memcpy(strg1->st_cp + len1, strg2->st_cp, len2 + 1);
3833 strg1->st_wcp = xrealloc(strg1->st_wcp,
3834 (len + 1) * sizeof (wchar_t));
3835 (void)memcpy(strg1->st_wcp + len1, strg2->st_wcp,
3836 (len2 + 1) * sizeof (wchar_t));
3837 free(strg2->st_wcp);
3845 * Print a warning if the given node has operands which should be
3848 * XXX Does not work if an operand is a constant expression. Constant
3849 * expressions are already folded.
3864 mp = &modtab[tn->tn_op];
3867 for (ln = tn->tn_left; ln->tn_op == CVT; ln = ln->tn_left)
3868 lparn |= ln->tn_parn;
3869 lparn |= ln->tn_parn;
3874 for (rn = tn->tn_right; tn->tn_op == CVT; rn = rn->tn_left)
3875 rparn |= rn->tn_parn;
3876 rparn |= rn->tn_parn;
3882 switch (tn->tn_op) {
3885 if (!lparn && (lop == PLUS || lop == MINUS)) {
3887 } else if (!rparn && (rop == PLUS || rop == MINUS)) {
3892 if (!lparn && lop == LOGAND) {
3894 } else if (!rparn && rop == LOGAND) {
3901 if (!lparn && lop != tn->tn_op) {
3902 if (lop == PLUS || lop == MINUS) {
3904 } else if (lop == AND || lop == XOR) {
3908 if (!warn && !rparn && rop != tn->tn_op) {
3909 if (rop == PLUS || rop == MINUS) {
3911 } else if (rop == AND || rop == XOR) {
3916 /* LINTED (enumeration values not handled in switch) */
3921 /* precedence confusion possible: parenthesize! */