1 /* $NetBSD: tree.c,v 1.12 1995/10/02 17:37:57 jpo Exp $ */
4 * Copyright (c) 1994, 1995 Jochen Pohl
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Jochen Pohl for
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * $NetBSD: tree.c,v 1.12 1995/10/02 17:37:57 jpo Exp $
34 * $DragonFly: src/usr.bin/xlint/lint1/tree.c,v 1.3 2003/11/03 19:14:36 eirikn Exp $
46 /* Various flags for each operator. */
47 static mod_t modtab[NOPS];
49 static tnode_t *getinode __P((tspec_t, quad_t));
50 static void ptrcmpok __P((op_t, tnode_t *, tnode_t *));
51 static int asgntypok __P((op_t, int, tnode_t *, tnode_t *));
52 static void chkbeop __P((op_t, tnode_t *, tnode_t *));
53 static void chkeop2 __P((op_t, int, tnode_t *, tnode_t *));
54 static void chkeop1 __P((op_t, int, tnode_t *, tnode_t *));
55 static tnode_t *mktnode __P((op_t, type_t *, tnode_t *, tnode_t *));
56 static void balance __P((op_t, tnode_t **, tnode_t **));
57 static void incompat __P((op_t, tspec_t, tspec_t));
58 static void illptrc __P((mod_t *, type_t *, type_t *));
59 static void mrgqual __P((type_t **, type_t *, type_t *));
60 static int conmemb __P((type_t *));
61 static void ptconv __P((int, tspec_t, tspec_t, type_t *, tnode_t *));
62 static void iiconv __P((op_t, int, tspec_t, tspec_t, type_t *, tnode_t *));
63 static void piconv __P((op_t, tspec_t, type_t *, tnode_t *));
64 static void ppconv __P((op_t, tnode_t *, type_t *));
65 static tnode_t *bldstr __P((op_t, tnode_t *, tnode_t *));
66 static tnode_t *bldincdec __P((op_t, tnode_t *));
67 static tnode_t *bldamper __P((tnode_t *, int));
68 static tnode_t *bldplmi __P((op_t, tnode_t *, tnode_t *));
69 static tnode_t *bldshft __P((op_t, tnode_t *, tnode_t *));
70 static tnode_t *bldcol __P((tnode_t *, tnode_t *));
71 static tnode_t *bldasgn __P((op_t, tnode_t *, tnode_t *));
72 static tnode_t *plength __P((type_t *));
73 static tnode_t *fold __P((tnode_t *));
74 static tnode_t *foldtst __P((tnode_t *));
75 static tnode_t *foldflt __P((tnode_t *));
76 static tnode_t *chkfarg __P((type_t *, tnode_t *));
77 static tnode_t *parg __P((int, type_t *, tnode_t *));
78 static void nulleff __P((tnode_t *));
79 static void displexpr __P((tnode_t *, int));
80 static void chkaidx __P((tnode_t *, int));
81 static void chkcomp __P((op_t, tnode_t *, tnode_t *));
82 static void precconf __P((tnode_t *));
85 * Initialize mods of operators.
94 { ARROW, { 1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,
96 { POINT, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
98 { NOT, { 0,1,0,1,0,1,0,1,0,0,0,0,0,0,0,1,0,
100 { COMPL, { 0,0,1,0,0,1,1,0,0,0,0,0,0,0,0,1,1,
102 { INCBEF, { 0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,
104 { DECBEF, { 0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,
106 { INCAFT, { 0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,
108 { DECAFT, { 0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,
110 { UPLUS, { 0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,1,
112 { UMINUS, { 0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,1,1,
114 { STAR, { 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,
116 { AMPER, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
118 { MULT, { 1,0,0,0,1,1,1,0,1,0,0,1,0,0,0,1,1,
120 { DIV, { 1,0,0,0,1,1,1,0,1,0,1,1,0,0,0,1,1,
122 { MOD, { 1,0,1,0,0,1,1,0,1,0,1,1,0,0,0,1,1,
124 { PLUS, { 1,0,0,1,0,1,1,0,1,0,0,0,0,0,0,1,0,
126 { MINUS, { 1,0,0,1,0,1,1,0,1,0,0,0,0,0,0,1,0,
128 { SHL, { 1,0,1,0,0,1,1,0,0,0,0,0,1,0,0,1,1,
130 { SHR, { 1,0,1,0,0,1,1,0,0,0,1,0,1,0,0,1,1,
132 { LT, { 1,1,0,1,0,1,1,0,1,0,1,1,0,1,1,0,1,
134 { LE, { 1,1,0,1,0,1,1,0,1,0,1,1,0,1,1,0,1,
136 { GT, { 1,1,0,1,0,1,1,0,1,0,1,1,0,1,1,0,1,
138 { GE, { 1,1,0,1,0,1,1,0,1,0,1,1,0,1,1,0,1,
140 { EQ, { 1,1,0,1,0,1,1,0,1,0,0,0,0,1,1,0,1,
142 { NE, { 1,1,0,1,0,1,1,0,1,0,0,0,0,1,1,0,1,
144 { AND, { 1,0,1,0,0,1,1,0,1,0,0,0,1,0,0,1,0,
146 { XOR, { 1,0,1,0,0,1,1,0,1,0,0,0,1,0,0,1,0,
148 { OR, { 1,0,1,0,0,1,1,0,1,0,0,0,1,0,0,1,0,
150 { LOGAND, { 1,1,0,1,0,1,0,1,0,0,0,0,0,0,0,1,0,
152 { LOGOR, { 1,1,0,1,0,1,0,1,0,0,0,0,1,0,0,1,0,
154 { QUEST, { 1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,
156 { COLON, { 1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,
158 { ASSIGN, { 1,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,
160 { MULASS, { 1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,1,0,
162 { DIVASS, { 1,0,0,0,1,0,0,0,0,1,0,1,0,0,0,1,0,
164 { MODASS, { 1,0,1,0,0,0,0,0,0,1,0,1,0,0,0,1,0,
166 { ADDASS, { 1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,
168 { SUBASS, { 1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,
170 { SHLASS, { 1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,
172 { SHRASS, { 1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,
174 { ANDASS, { 1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,
176 { XORASS, { 1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,
178 { ORASS, { 1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,
180 { NAME, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
182 { CON, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
184 { STRING, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
186 { FSEL, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
188 { CALL, { 1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
190 { COMMA, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
192 { CVT, { 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,
194 { ICALL, { 1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
196 { LOAD, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
198 { PUSH, { 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,
200 { RETURN, { 1,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,
202 { INIT, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,
204 { FARG, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,
210 for (i = 0; imods[i].op != NOOP; i++)
211 STRUCT_ASSIGN(modtab[imods[i].op], imods[i].m);
215 * Increase degree of reference.
216 * This is most often used to change type "T" in type "pointer to T".
225 tp2 = getblk(sizeof (type_t));
232 * same for use in expressions
241 tp2 = tgetblk(sizeof (type_t));
248 * Create a node for a constant.
260 n->tn_val = tgetblk(sizeof (val_t));
261 n->tn_val->v_tspec = tp->t_tspec;
262 n->tn_val->v_ansiu = v->v_ansiu;
263 n->tn_val->v_u = v->v_u;
269 * Create a node for a integer constant.
280 n->tn_type = gettyp(t);
281 n->tn_val = tgetblk(sizeof (val_t));
282 n->tn_val->v_tspec = t;
283 n->tn_val->v_quad = q;
288 * Create a node for a name (symbol table entry).
289 * ntok is the token which follows the name.
298 if (sym->s_scl == NOSCL) {
301 if (ntok == T_LPARN) {
303 /* function implicitly declared to ... */
307 * XXX if tflag is set the symbol should be
308 * exported to level 0
310 sym->s_type = incref(sym->s_type, FUNC);
313 error(99, sym->s_name);
317 if (sym->s_kind != FVFT && sym->s_kind != FMOS)
318 lerror("getnnode() 1");
321 n->tn_type = sym->s_type;
322 if (sym->s_scl != ENUMCON) {
325 if (sym->s_kind == FVFT && sym->s_type->t_tspec != FUNC)
329 n->tn_val = tgetblk(sizeof (val_t));
330 *n->tn_val = sym->s_value;
337 * Create a node for a string.
351 n->tn_type = tincref(gettyp(strg->st_tspec), ARRAY);
352 n->tn_type->t_dim = len + 1;
355 n->tn_strg = tgetblk(sizeof (strg_t));
356 n->tn_strg->st_tspec = strg->st_tspec;
357 n->tn_strg->st_len = len;
359 if (strg->st_tspec == CHAR) {
360 n->tn_strg->st_cp = tgetblk(len + 1);
361 (void)memcpy(n->tn_strg->st_cp, strg->st_cp, len + 1);
364 n->tn_strg->st_wcp = tgetblk((len + 1) * sizeof (wchar_t));
365 (void)memcpy(n->tn_strg->st_wcp, strg->st_wcp,
366 (len + 1) * sizeof (wchar_t));
375 * Returns a symbol which has the same name as the msym argument and is a
376 * member of the struct or union specified by the tn argument.
379 strmemb(tn, op, msym)
391 * Remove the member if it was unknown until now (Which means
392 * that no defined struct or union has a member with the same name).
394 if (msym->s_scl == NOSCL) {
395 /* undefined struct/union member: %s */
396 error(101, msym->s_name);
400 msym->s_styp = tgetblk(sizeof (str_t));
401 msym->s_styp->stag = tgetblk(sizeof (sym_t));
402 msym->s_styp->stag->s_name = unnamed;
403 msym->s_value.v_tspec = INT;
407 /* Set str to the tag of which msym is expected to be a member. */
409 t = (tp = tn->tn_type)->t_tspec;
411 if (t == STRUCT || t == UNION)
413 } else if (op == ARROW && t == PTR) {
414 t = (tp = tp->t_subt)->t_tspec;
415 if (t == STRUCT || t == UNION)
420 * If this struct/union has a member with the name of msym, return
424 for (sym = msym; sym != NULL; sym = sym->s_link) {
425 if (sym->s_scl != MOS && sym->s_scl != MOU)
427 if (sym->s_styp != str)
429 if (strcmp(sym->s_name, msym->s_name) != 0)
436 * Set eq to 0 if there are struct/union members with the same name
437 * and different types and/or offsets.
440 for (csym = msym; csym != NULL; csym = csym->s_link) {
441 if (csym->s_scl != MOS && csym->s_scl != MOU)
443 if (strcmp(msym->s_name, csym->s_name) != 0)
445 for (sym = csym->s_link ; sym != NULL; sym = sym->s_link) {
448 if (sym->s_scl != MOS && sym->s_scl != MOU)
450 if (strcmp(csym->s_name, sym->s_name) != 0)
452 if (csym->s_value.v_quad != sym->s_value.v_quad) {
457 eq = eqtype(csym->s_type, sym->s_type, 0, 0, &w) && !w;
460 if (csym->s_field != sym->s_field) {
469 if (tp1->t_flen != tp2->t_flen) {
473 if (tp1->t_foffs != tp2->t_foffs) {
484 * Now handle the case in which the left operand refers really
485 * to a struct/union, but the right operand is not member of it.
488 /* illegal member use: %s */
490 warning(102, msym->s_name);
492 error(102, msym->s_name);
498 * Now the left operand of ARROW does not point to a struct/union
499 * or the left operand of POINT is no struct/union.
503 /* left operand of "." must be struct/union object */
510 /* left operand of "->" must be pointer to ... */
511 if (tflag && tn->tn_type->t_tspec == PTR) {
519 /* non-unique member requires struct/union %s */
520 error(105, op == POINT ? "object" : "pointer");
522 /* unacceptable operand of %s */
523 error(111, modtab[op].m_name);
531 * Create a tree node. Called for most operands except function calls,
536 * rn if not NULL, right operand
549 /* If there was an error in one of the operands, return. */
550 if (ln == NULL || (mp->m_binary && rn == NULL))
554 * Apply class conversions to the left operand, but only if its
555 * value is needed or it is compaired with null.
557 if (mp->m_vctx || mp->m_tctx)
560 * The right operand is almost always in a test or value context,
561 * except if it is a struct or union member.
563 if (mp->m_binary && op != ARROW && op != POINT)
567 * Print some warnings for comparisions of unsigned values with
568 * constants lower than or equal to null. This must be done
569 * before promote() because otherwise unsigned char and unsigned
570 * short would be promoted to int. Also types are tested to be
571 * CHAR, which would also become int.
577 * Promote the left operand if it is in a test or value context
579 if (mp->m_vctx || mp->m_tctx)
580 ln = promote(op, 0, ln);
582 * Promote the right operand, but only if it is no struct or
583 * union member, or if it is not to be assigned to the left operand
585 if (mp->m_binary && op != ARROW && op != POINT &&
586 op != ASSIGN && op != RETURN) {
587 rn = promote(op, 0, rn);
591 * If the result of the operation is different for signed or
592 * unsigned operands and one of the operands is signed only in
593 * ANSI C, print a warning.
595 if (mp->m_tlansiu && ln->tn_op == CON && ln->tn_val->v_ansiu) {
596 /* ANSI C treats constant as unsigned, op %s */
597 warning(218, mp->m_name);
598 ln->tn_val->v_ansiu = 0;
600 if (mp->m_transiu && rn->tn_op == CON && rn->tn_val->v_ansiu) {
601 /* ANSI C treats constant as unsigned, op %s */
602 warning(218, mp->m_name);
603 rn->tn_val->v_ansiu = 0;
606 /* Make sure both operands are of the same type */
607 if (mp->m_balance || (tflag && (op == SHL || op == SHR)))
608 balance(op, &ln, &rn);
611 * Check types for compatibility with the operation and mutual
612 * compatibility. Return if there are serios problems.
614 if (!typeok(op, 0, ln, rn))
617 /* And now create the node. */
621 ntn = bldstr(op, ln, rn);
627 ntn = bldincdec(op, ln);
630 ntn = bldamper(ln, 0);
633 ntn = mktnode(STAR, ln->tn_type->t_subt, ln, NULL);
637 ntn = bldplmi(op, ln, rn);
641 ntn = bldshft(op, ln, rn);
644 ntn = bldcol(ln, rn);
658 ntn = bldasgn(op, ln, rn);
662 ntn = mktnode(op, rn->tn_type, ln, rn);
665 rtp = mp->m_logop ? gettyp(INT) : ln->tn_type;
666 if (!mp->m_binary && rn != NULL)
668 ntn = mktnode(op, rtp, ln, rn);
672 /* Return if an error occured. */
676 /* Print a warning if precedence confusion is possible */
681 * Print a warning if one of the operands is in a context where
682 * it is compared with null and if this operand is a constant.
685 if (ln->tn_op == CON ||
686 ((mp->m_binary && op != QUEST) && rn->tn_op == CON)) {
688 /* constant in conditional context */
693 /* Fold if the operator requires it */
695 if (ln->tn_op == CON && (!mp->m_binary || rn->tn_op == CON)) {
698 } else if (isftyp(ntn->tn_type->t_tspec)) {
703 } else if (op == QUEST && ln->tn_op == CON) {
704 ntn = ln->tn_val->v_quad ? rn->tn_left : rn->tn_right;
712 * Perform class conversions.
714 * Arrays of type T are converted into pointers to type T.
715 * Functions are converted to pointers to functions.
716 * Lvalues are converted to rvalues.
725 * Array-lvalue (array of type T) is converted into rvalue
726 * (pointer to type T)
728 if (tn->tn_type->t_tspec == ARRAY) {
729 if (!tn->tn_lvalue) {
730 /* %soperand of '%s' must be lvalue */
731 /* XXX print correct operator */
732 (void)gnuism(114, "", modtab[AMPER].m_name);
734 tn = mktnode(AMPER, tincref(tn->tn_type->t_subt, PTR),
739 * Expression of type function (function with return value of type T)
740 * in rvalue-expression (pointer to function with return value
743 if (tn->tn_type->t_tspec == FUNC)
744 tn = bldamper(tn, 1);
746 /* lvalue to rvalue */
748 tp = tduptyp(tn->tn_type);
749 tp->t_const = tp->t_volatile = 0;
750 tn = mktnode(LOAD, tp, tn, NULL);
757 * Perform most type checks. First the types are checked using
758 * informations from modtab[]. After that it is done by hand for
759 * more complicated operators and type combinations.
761 * If the types are ok, typeok() returns 1, otherwise 0.
764 typeok(op, arg, ln, rn)
770 tspec_t lt, rt, lst, rst, olt, ort;
771 type_t *ltp, *rtp, *lstp, *rstp;
776 if ((lt = (ltp = ln->tn_type)->t_tspec) == PTR)
777 lst = (lstp = ltp->t_subt)->t_tspec;
779 if ((rt = (rtp = rn->tn_type)->t_tspec) == PTR)
780 rst = (rstp = rtp->t_subt)->t_tspec;
784 /* integertypes required */
785 if (!isityp(lt) || (mp->m_binary && !isityp(rt))) {
786 incompat(op, lt, rt);
789 } else if (mp->m_rqsclt) {
790 /* scalar types required */
791 if (!issclt(lt) || (mp->m_binary && !issclt(rt))) {
792 incompat(op, lt, rt);
795 } else if (mp->m_rqatyp) {
796 /* arithmetic types required */
797 if (!isatyp(lt) || (mp->m_binary && !isatyp(rt))) {
798 incompat(op, lt, rt);
803 if (op == SHL || op == SHR || op == SHLASS || op == SHRASS) {
805 * For these operations we need the types before promotion
808 for (tn=ln; tn->tn_op==CVT && !tn->tn_cast; tn=tn->tn_left) ;
809 olt = tn->tn_type->t_tspec;
810 for (tn=rn; tn->tn_op==CVT && !tn->tn_cast; tn=tn->tn_left) ;
811 ort = tn->tn_type->t_tspec;
817 * Most errors required by ANSI C are reported in strmemb().
818 * Here we only must check for totaly wrong things.
820 if (lt == FUNC || lt == VOID || ltp->t_isfield ||
821 ((lt != STRUCT && lt != UNION) && !ln->tn_lvalue)) {
822 /* Without tflag we got already an error */
824 /* unacceptable operand of %s */
825 error(111, mp->m_name);
828 /* Now we have an object we can create a pointer to */
831 if (lt != PTR && !(tflag && isityp(lt))) {
832 /* Without tflag we got already an error */
834 /* unacceptabel operand of %s */
835 error(111, mp->m_name);
843 /* operands have scalar types (checked above) */
844 if (!ln->tn_lvalue) {
845 if (ln->tn_op == CVT && ln->tn_cast &&
846 ln->tn_left->tn_op == LOAD) {
847 /* a cast does not yield an lvalue */
850 /* %soperand of %s must be lvalue */
851 error(114, "", mp->m_name);
853 } else if (ltp->t_const) {
854 /* %soperand of %s must be modifiable lvalue */
856 warning(115, "", mp->m_name);
860 if (lt == ARRAY || lt == FUNC) {
861 /* ok, a warning comes later (in bldamper()) */
862 } else if (!ln->tn_lvalue) {
863 if (ln->tn_op == CVT && ln->tn_cast &&
864 ln->tn_left->tn_op == LOAD) {
865 /* a cast does not yield an lvalue */
868 /* %soperand of %s must be lvalue */
869 error(114, "", mp->m_name);
871 } else if (issclt(lt)) {
872 if (ltp->t_isfield) {
873 /* cannot take address of bit-field */
877 } else if (lt != STRUCT && lt != UNION) {
878 /* unacceptable operand of %s */
879 error(111, mp->m_name);
882 if (ln->tn_op == NAME && ln->tn_sym->s_reg) {
883 /* cannot take address of register %s */
884 error(113, ln->tn_sym->s_name);
889 /* until now there were no type checks for this operator */
891 /* cannot dereference non-pointer type */
897 /* operands have scalar types (checked above) */
898 if ((lt == PTR && !isityp(rt)) || (rt == PTR && !isityp(lt))) {
899 incompat(op, lt, rt);
904 /* operands have scalar types (checked above) */
905 if (lt == PTR && (!isityp(rt) && rt != PTR)) {
906 incompat(op, lt, rt);
908 } else if (rt == PTR && lt != PTR) {
909 incompat(op, lt, rt);
912 if (lt == PTR && rt == PTR) {
913 if (!eqtype(lstp, rstp, 1, 0, NULL)) {
914 /* illegal pointer subtraction */
920 /* operands have integer types (checked above) */
921 if (pflag && !isutyp(lt)) {
923 * The left operand is signed. This means that
924 * the operation is (possibly) nonportable.
926 /* bitwise operation on signed value nonportable */
927 if (ln->tn_op != CON) {
928 /* possibly nonportable */
930 } else if (ln->tn_val->v_quad < 0) {
933 } else if (!tflag && !sflag && !isutyp(olt) && isutyp(ort)) {
935 * The left operand would become unsigned in
939 (ln->tn_op != CON || ln->tn_val->v_quad < 0)) {
940 /* semantics of %s change in ANSI C; use ... */
941 warning(118, mp->m_name);
943 } else if (!tflag && !sflag && !isutyp(olt) && !isutyp(ort) &&
944 psize(lt) < psize(rt)) {
946 * In traditional C the left operand would be extended,
947 * possibly with 1, and then shifted.
950 (ln->tn_op != CON || ln->tn_val->v_quad < 0)) {
951 /* semantics of %s change in ANSI C; use ... */
952 warning(118, mp->m_name);
958 * ANSI C does not perform balancing for shift operations,
959 * but traditional C does. If the width of the right operand
960 * is greather than the width of the left operand, than in
961 * traditional C the left operand would be extendet to the
962 * width of the right operand. For SHL this may result in
965 if (psize(lt) < psize(rt)) {
967 * XXX If both operands are constant make sure
968 * that there is really a differencs between
969 * ANSI C and traditional C.
972 /* semantics of %s change in ANSI C; use ... */
973 warning(118, mp->m_name);
976 if (rn->tn_op == CON) {
977 if (!isutyp(rt) && rn->tn_val->v_quad < 0) {
980 } else if ((u_quad_t)rn->tn_val->v_quad == size(lt)) {
981 /* shift equal to size fo object */
983 } else if ((u_quad_t)rn->tn_val->v_quad > size(lt)) {
984 /* shift greater than size of object */
992 * Accept some things which are allowed with EQ and NE,
993 * but not with ordered comparisions.
995 if (lt == PTR && ((rt == PTR && rst == VOID) || isityp(rt))) {
996 if (rn->tn_op == CON && rn->tn_val->v_quad == 0)
999 if (rt == PTR && ((lt == PTR && lst == VOID) || isityp(lt))) {
1000 if (ln->tn_op == CON && ln->tn_val->v_quad == 0)
1008 if ((lt == PTR || rt == PTR) && lt != rt) {
1009 if (isityp(lt) || isityp(rt)) {
1010 /* illegal comb. of pointer and int., op %s */
1011 warning(123, mp->m_name);
1013 incompat(op, lt, rt);
1016 } else if (lt == PTR && rt == PTR) {
1017 ptrcmpok(op, ln, rn);
1022 /* first operand must have scalar type, op ? : */
1026 if (rn->tn_op != COLON)
1027 lerror("typeok() 2");
1031 if (isatyp(lt) && isatyp(rt))
1034 if (lt == STRUCT && rt == STRUCT && ltp->t_str == rtp->t_str)
1036 if (lt == UNION && rt == UNION && ltp->t_str == rtp->t_str)
1039 /* combination of any pointer and 0, 0L or (void *)0 is ok */
1040 if (lt == PTR && ((rt == PTR && rst == VOID) || isityp(rt))) {
1041 if (rn->tn_op == CON && rn->tn_val->v_quad == 0)
1044 if (rt == PTR && ((lt == PTR && lst == VOID) || isityp(lt))) {
1045 if (ln->tn_op == CON && ln->tn_val->v_quad == 0)
1049 if ((lt == PTR && isityp(rt)) || (isityp(lt) && rt == PTR)) {
1050 /* illegal comb. of ptr. and int., op %s */
1051 warning(123, mp->m_name);
1055 if (lt == VOID || rt == VOID) {
1056 if (lt != VOID || rt != VOID)
1057 /* incompatible types in conditional */
1062 if (lt == PTR && rt == PTR && ((lst == VOID && rst == FUNC) ||
1063 (lst == FUNC && rst == VOID))) {
1064 /* (void *)0 handled above */
1066 /* ANSI C forbids conv. of %s to %s, op %s */
1067 warning(305, "function pointer", "'void *'",
1072 if (rt == PTR && lt == PTR) {
1073 if (!eqtype(lstp, rstp, 1, 0, NULL))
1074 illptrc(mp, ltp, rtp);
1078 /* incompatible types in conditional */
1086 if (!asgntypok(op, arg, ln, rn))
1095 /* operands have scalar types (checked above) */
1096 if ((lt == PTR && !isityp(rt)) || rt == PTR) {
1097 incompat(op, lt, rt);
1104 if (pflag && !isutyp(lt) && !(tflag && isutyp(rt))) {
1105 /* bitwise operation on s.v. possibly nonportabel */
1114 if (!ln->tn_lvalue) {
1115 if (ln->tn_op == CVT && ln->tn_cast &&
1116 ln->tn_left->tn_op == LOAD) {
1117 /* a cast does not yield an lvalue */
1120 /* %soperand of %s must be lvalue */
1121 error(114, "left ", mp->m_name);
1123 } else if (ltp->t_const || ((lt == STRUCT || lt == UNION) &&
1125 /* %soperand of %s must be modifiable lvalue */
1127 warning(115, "left ", mp->m_name);
1131 if (!modtab[ln->tn_op].m_sideeff)
1134 /* LINTED (enumeration values not handled in switch) */
1139 (ltp->t_isenum || (mp->m_binary && rtp->t_isenum))) {
1140 chkbeop(op, ln, rn);
1141 } else if (mp->m_enumop && (ltp->t_isenum && rtp->t_isenum)) {
1142 chkeop2(op, arg, ln, rn);
1143 } else if (mp->m_enumop && (ltp->t_isenum || rtp->t_isenum)) {
1144 chkeop1(op, arg, ln, rn);
1151 ptrcmpok(op, ln, rn)
1157 const char *lts, *rts;
1159 lt = (ltp = ln->tn_type)->t_subt->t_tspec;
1160 rt = (rtp = rn->tn_type)->t_subt->t_tspec;
1162 if (lt == VOID || rt == VOID) {
1163 if (sflag && (lt == FUNC || rt == FUNC)) {
1164 /* (void *)0 already handled in typeok() */
1165 *(lt == FUNC ? <s : &rts) = "function pointer";
1166 *(lt == VOID ? <s : &rts) = "'void *'";
1167 /* ANSI C forbids comparision of %s with %s */
1168 warning(274, lts, rts);
1173 if (!eqtype(ltp->t_subt, rtp->t_subt, 1, 0, NULL)) {
1174 illptrc(&modtab[op], ltp, rtp);
1178 if (lt == FUNC && rt == FUNC) {
1179 if (sflag && op != EQ && op != NE)
1180 /* ANSI C forbids ordered comp. of func ptr */
1186 * Checks type compatibility for ASSIGN, INIT, FARG and RETURN
1187 * and prints warnings/errors if necessary.
1188 * If the types are (almost) compatible, 1 is returned, otherwise 0.
1191 asgntypok(op, arg, ln, rn)
1196 tspec_t lt, rt, lst, rst;
1197 type_t *ltp, *rtp, *lstp, *rstp;
1199 const char *lts, *rts;
1201 if ((lt = (ltp = ln->tn_type)->t_tspec) == PTR)
1202 lst = (lstp = ltp->t_subt)->t_tspec;
1203 if ((rt = (rtp = rn->tn_type)->t_tspec) == PTR)
1204 rst = (rstp = rtp->t_subt)->t_tspec;
1207 if (isatyp(lt) && isatyp(rt))
1210 if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION))
1211 /* both are struct or union */
1212 return (ltp->t_str == rtp->t_str);
1214 /* 0, 0L and (void *)0 may be assigned to any pointer */
1215 if (lt == PTR && ((rt == PTR && rst == VOID) || isityp(rt))) {
1216 if (rn->tn_op == CON && rn->tn_val->v_quad == 0)
1220 if (lt == PTR && rt == PTR && (lst == VOID || rst == VOID)) {
1221 /* two pointers, at least one pointer to void */
1222 if (sflag && (lst == FUNC || rst == FUNC)) {
1223 /* comb. of ptr to func and ptr to void */
1224 *(lst == FUNC ? <s : &rts) = "function pointer";
1225 *(lst == VOID ? <s : &rts) = "'void *'";
1229 /* ANSI C forbids conversion of %s to %s */
1230 warning(303, rts, lts);
1233 /* ANSI C forbids conv. of %s to %s, arg #%d */
1234 warning(304, rts, lts, arg);
1237 /* ANSI C forbids conv. of %s to %s, op %s */
1238 warning(305, rts, lts, mp->m_name);
1244 if (lt == PTR && rt == PTR && (lst == VOID || rst == VOID ||
1245 eqtype(lstp, rstp, 1, 0, NULL))) {
1246 /* compatible pointer types (qualifiers ignored) */
1248 ((!lstp->t_const && rstp->t_const) ||
1249 (!lstp->t_volatile && rstp->t_volatile))) {
1250 /* left side has not all qualifiers of right */
1254 /* incompatible pointer types */
1258 /* argument has incompat. ptr. type, arg #%d */
1262 /* operands have incompat. ptr. types, op %s */
1263 warning(128, mp->m_name);
1270 if ((lt == PTR && isityp(rt)) || (isityp(lt) && rt == PTR)) {
1274 /* illegal combination of pointer and integer */
1278 /* illegal comb. of ptr. and int., arg #%d */
1282 /* illegal comb. of ptr. and int., op %s */
1283 warning(123, mp->m_name);
1289 if (lt == PTR && rt == PTR) {
1293 illptrc(NULL, ltp, rtp);
1296 /* argument has incompatible pointer type, arg #%d */
1300 illptrc(mp, ltp, rtp);
1308 /* initialisation type mismatch */
1312 /* return value type mismatch */
1316 /* argument is incompatible with prototype, arg #%d */
1320 incompat(op, lt, rt);
1328 * Prints a warning if an operator, which should be senseless for an
1329 * enum type, is applied to an enum type.
1343 if (!(ln->tn_type->t_isenum ||
1344 (mp->m_binary && rn->tn_type->t_isenum))) {
1349 * Enum as offset to a pointer is an exception (otherwise enums
1350 * could not be used as array indizes).
1353 ((ln->tn_type->t_isenum && rn->tn_type->t_tspec == PTR) ||
1354 (rn->tn_type->t_isenum && ln->tn_type->t_tspec == PTR))) {
1358 /* dubious operation on enum, op %s */
1359 warning(241, mp->m_name);
1364 * Prints a warning if an operator is applied to two different enum types.
1367 chkeop2(op, arg, ln, rn)
1376 if (ln->tn_type->t_enum != rn->tn_type->t_enum) {
1379 /* enum type mismatch in initialisation */
1383 /* enum type mismatch, arg #%d */
1387 /* return value type mismatch */
1391 /* enum type mismatch, op %s */
1392 warning(130, mp->m_name);
1396 } else if (mp->m_comp && op != EQ && op != NE) {
1398 /* dubious comparisions of enums */
1399 warning(243, mp->m_name);
1405 * Prints a warning if an operator has both enum end other integer
1409 chkeop1(op, arg, ln, rn)
1420 * Initializations with 0 should be allowed. Otherwise,
1421 * we should complain about all uninitialized enums,
1424 if (!rn->tn_type->t_isenum && rn->tn_op == CON &&
1425 isityp(rn->tn_type->t_tspec) && rn->tn_val->v_quad == 0) {
1428 /* initialisation of '%s' with '%s' */
1429 warning(277, tyname(ln->tn_type), tyname(rn->tn_type));
1432 /* combination of '%s' and '%s', arg #%d */
1433 warning(278, tyname(ln->tn_type), tyname(rn->tn_type), arg);
1436 /* combination of '%s' and '%s' in return */
1437 warning(279, tyname(ln->tn_type), tyname(rn->tn_type));
1440 /* combination of '%s' and %s, op %s */
1441 warning(242, tyname(ln->tn_type), tyname(rn->tn_type),
1448 * Build and initialize a new node.
1451 mktnode(op, type, ln, rn)
1462 ntn->tn_type = type;
1466 if (op == STAR || op == FSEL) {
1467 if (ln->tn_type->t_tspec == PTR) {
1468 t = ln->tn_type->t_subt->t_tspec;
1469 if (t != FUNC && t != VOID)
1472 lerror("mktnode() 2");
1480 * Performs usual conversion of operands to (unsigned) int.
1482 * If tflag is set or the operand is a function argument with no
1483 * type information (no prototype or variable # of args), convert
1487 promote(op, farg, tn)
1496 t = tn->tn_type->t_tspec;
1503 * ANSI C requires that the result is always of type INT
1504 * if INT can represent all possible values of the previous
1507 if (tn->tn_type->t_isfield) {
1508 len = tn->tn_type->t_flen;
1509 if (size(INT) > len) {
1512 if (size(INT) != len)
1513 lerror("promote() 1");
1520 } else if (t == CHAR || t == UCHAR || t == SCHAR) {
1521 t = (size(CHAR) < size(INT) || t != UCHAR) ?
1523 } else if (t == SHORT || t == USHORT) {
1524 t = (size(SHORT) < size(INT) || t == SHORT) ?
1526 } else if (t == ENUM) {
1528 } else if (farg && t == FLOAT) {
1533 * In traditional C, keep unsigned and promote FLOAT
1536 if (t == UCHAR || t == USHORT) {
1538 } else if (t == CHAR || t == SCHAR || t == SHORT) {
1540 } else if (t == FLOAT) {
1542 } else if (t == ENUM) {
1547 if (t != tn->tn_type->t_tspec) {
1548 ntp = tduptyp(tn->tn_type);
1551 * Keep t_isenum so we are later able to check compatibility
1554 tn = convert(op, 0, ntp, tn);
1561 * Insert conversions which are necessary to give both operands the same
1562 * type. This is done in different ways for traditional C and ANIS C.
1565 balance(op, lnp, rnp)
1567 tnode_t **lnp, **rnp;
1572 static tspec_t tl[] = {
1573 LDOUBLE, DOUBLE, FLOAT, UQUAD, QUAD, ULONG, LONG, UINT, INT,
1576 lt = (*lnp)->tn_type->t_tspec;
1577 rt = (*rnp)->tn_type->t_tspec;
1579 if (!isatyp(lt) || !isatyp(rt))
1585 } else if (lt == LDOUBLE || rt == LDOUBLE) {
1587 } else if (lt == DOUBLE || rt == DOUBLE) {
1589 } else if (lt == FLOAT || rt == FLOAT) {
1593 * If type A has more bits than type B it should
1594 * be able to hold all possible values of type B.
1596 if (size(lt) > size(rt)) {
1598 } else if (size(lt) < size(rt)) {
1601 for (i = 3; tl[i] != INT; i++) {
1602 if (tl[i] == lt || tl[i] == rt)
1605 if ((isutyp(lt) || isutyp(rt)) &&
1613 /* Keep unsigned in traditional C */
1614 u = isutyp(lt) || isutyp(rt);
1615 for (i = 0; tl[i] != INT; i++) {
1616 if (lt == tl[i] || rt == tl[i])
1620 if (u && isityp(t) && !isutyp(t))
1625 ntp = tduptyp((*lnp)->tn_type);
1627 *lnp = convert(op, 0, ntp, *lnp);
1630 ntp = tduptyp((*rnp)->tn_type);
1632 *rnp = convert(op, 0, ntp, *rnp);
1637 * Insert a conversion operator, which converts the type of the node
1638 * to another given type.
1639 * If op is FARG, arg is the number of the argument (used for warnings).
1642 convert(op, arg, tp, tn)
1649 tspec_t nt, ot, ost;
1652 lerror("convert() 1");
1655 if ((ot = tn->tn_type->t_tspec) == PTR)
1656 ost = tn->tn_type->t_subt->t_tspec;
1658 if (!tflag && !sflag && op == FARG)
1659 ptconv(arg, nt, ot, tp, tn);
1660 if (isityp(nt) && isityp(ot)) {
1661 iiconv(op, arg, nt, ot, tp, tn);
1662 } else if (nt == PTR && ((ot == PTR && ost == VOID) || isityp(ot)) &&
1663 tn->tn_op == CON && tn->tn_val->v_quad == 0) {
1664 /* 0, 0L and (void *)0 may be assigned to any pointer. */
1665 } else if (isityp(nt) && ot == PTR) {
1666 piconv(op, nt, tp, tn);
1667 } else if (nt == PTR && ot == PTR) {
1674 ntn->tn_cast = op == CVT;
1675 if (tn->tn_op != CON || nt == VOID) {
1679 ntn->tn_val = tgetblk(sizeof (val_t));
1680 cvtcon(op, arg, ntn->tn_type, ntn->tn_val, tn->tn_val);
1687 * Print a warning if a prototype causes a type conversion that is
1688 * different from what would happen to the same argument in the
1689 * absence of a prototype.
1691 * Errors/Warnings about illegal type combinations are already printed
1695 ptconv(arg, nt, ot, tp, tn)
1703 if (!isatyp(nt) || !isatyp(ot))
1707 * If the type of the formal parameter is char/short, a warning
1708 * would be useless, because functions declared the old style
1709 * can't expect char/short arguments.
1711 if (nt == CHAR || nt == UCHAR || nt == SHORT || nt == USHORT)
1714 /* get default promotion */
1715 ptn = promote(NOOP, 1, tn);
1716 ot = ptn->tn_type->t_tspec;
1718 /* return if types are the same with and without prototype */
1719 if (nt == ot || (nt == ENUM && ot == INT))
1722 if (isftyp(nt) != isftyp(ot) || psize(nt) != psize(ot)) {
1723 /* representation and/or width change */
1724 if (styp(nt) != SHORT || !isityp(ot) || psize(ot) > psize(INT))
1725 /* conversion to '%s' due to prototype, arg #%d */
1726 warning(259, tyname(tp), arg);
1729 * they differ in sign or base type (char, short, int,
1730 * long, long long, float, double, long double)
1732 * if they differ only in sign and the argument is a constant
1733 * and the msb of the argument is not set, print no warning
1735 if (ptn->tn_op == CON && isityp(nt) && styp(nt) == styp(ot) &&
1736 msb(ptn->tn_val->v_quad, ot, -1) == 0) {
1739 /* conversion to '%s' due to prototype, arg #%d */
1740 warning(259, tyname(tp), arg);
1746 * Print warnings for conversions of integer types which my cause
1751 iiconv(op, arg, nt, ot, tp, tn)
1758 if (tn->tn_op == CON)
1765 if (psize(nt) > psize(ot) && isutyp(nt) != isutyp(ot)) {
1766 /* conversion to %s may sign-extend incorrectly (, arg #%d) */
1767 if (aflag && pflag) {
1769 warning(297, tyname(tp), arg);
1771 warning(131, tyname(tp));
1777 if (psize(nt) < psize(ot) &&
1778 (ot == LONG || ot == ULONG || ot == QUAD || ot == UQUAD ||
1780 /* conversion from '%s' may lose accuracy */
1783 warning(298, tyname(tn->tn_type), arg);
1785 warning(132, tyname(tn->tn_type));
1792 * Print warnings for dubious conversions of pointer to integer.
1795 piconv(op, nt, tp, tn)
1801 if (tn->tn_op == CON)
1805 /* We got already an error. */
1809 if (psize(nt) < psize(PTR)) {
1810 if (pflag && size(nt) >= size(PTR)) {
1811 /* conv. of pointer to %s may lose bits */
1812 warning(134, tyname(tp));
1814 /* conv. of pointer to %s loses bits */
1815 warning(133, tyname(tp));
1821 * Print warnings for questionable pointer conversions.
1830 const char *nts, *ots;
1833 * We got already an error (pointers of different types
1834 * without a cast) or we will not get a warning.
1839 nt = tp->t_subt->t_tspec;
1840 ot = tn->tn_type->t_subt->t_tspec;
1842 if (nt == VOID || ot == VOID) {
1843 if (sflag && (nt == FUNC || ot == FUNC)) {
1844 /* (void *)0 already handled in convert() */
1845 *(nt == FUNC ? &nts : &ots) = "function pointer";
1846 *(nt == VOID ? &nts : &ots) = "'void *'";
1847 /* ANSI C forbids conversion of %s to %s */
1848 warning(303, ots, nts);
1851 } else if (nt == FUNC && ot == FUNC) {
1853 } else if (nt == FUNC || ot == FUNC) {
1854 /* questionable conversion of function pointer */
1859 if (getbound(tp->t_subt) > getbound(tn->tn_type->t_subt)) {
1861 /* possible pointer alignment problem */
1864 if (((nt == STRUCT || nt == UNION) &&
1865 tp->t_subt->t_str != tn->tn_type->t_subt->t_str) ||
1866 psize(nt) != psize(ot)) {
1868 /* pointer casts may be troublesome */
1875 * Converts a typed constant in a constant of another type.
1877 * op operator which requires conversion
1878 * arg if op is FARG, # of argument
1879 * tp type in which to convert the constant
1884 cvtcon(op, arg, tp, nv, v)
1893 quad_t xmask, xmsk1;
1897 nt = nv->v_tspec = tp->t_tspec;
1900 if (ot == FLOAT || ot == DOUBLE || ot == LDOUBLE) {
1903 max = CHAR_MAX; min = CHAR_MIN; break;
1905 max = UCHAR_MAX; min = 0; break;
1907 max = SCHAR_MAX; min = SCHAR_MIN; break;
1909 max = SHRT_MAX; min = SHRT_MIN; break;
1911 max = USHRT_MAX; min = 0; break;
1914 max = INT_MAX; min = INT_MIN; break;
1916 max = (u_int)UINT_MAX; min = 0; break;
1918 max = LONG_MAX; min = LONG_MIN; break;
1920 max = (u_long)ULONG_MAX; min = 0; break;
1922 max = QUAD_MAX; min = QUAD_MIN; break;
1924 max = (u_quad_t)UQUAD_MAX; min = 0; break;
1926 max = FLT_MAX; min = -FLT_MAX; break;
1928 max = DBL_MAX; min = -DBL_MAX; break;
1930 /* Got already an error because of float --> ptr */
1932 max = LDBL_MAX; min = -LDBL_MAX; break;
1934 lerror("cvtcon() 1");
1936 if (v->v_ldbl > max || v->v_ldbl < min) {
1938 lerror("cvtcon() 2");
1940 /* conv. of %s to %s is out of rng., arg #%d */
1941 warning(295, tyname(gettyp(ot)), tyname(tp),
1944 /* conversion of %s to %s is out of range */
1945 warning(119, tyname(gettyp(ot)), tyname(tp));
1947 v->v_ldbl = v->v_ldbl > 0 ? max : min;
1950 nv->v_ldbl = (float)v->v_ldbl;
1951 } else if (nt == DOUBLE) {
1952 nv->v_ldbl = (double)v->v_ldbl;
1953 } else if (nt == LDOUBLE) {
1954 nv->v_ldbl = v->v_ldbl;
1956 nv->v_quad = (nt == PTR || isutyp(nt)) ?
1957 (u_quad_t)v->v_ldbl : (quad_t)v->v_ldbl;
1961 nv->v_ldbl = (ot == PTR || isutyp(ot)) ?
1962 (float)(u_quad_t)v->v_quad : (float)v->v_quad;
1963 } else if (nt == DOUBLE) {
1964 nv->v_ldbl = (ot == PTR || isutyp(ot)) ?
1965 (double)(u_quad_t)v->v_quad : (double)v->v_quad;
1966 } else if (nt == LDOUBLE) {
1967 nv->v_ldbl = (ot == PTR || isutyp(ot)) ?
1968 (ldbl_t)(u_quad_t)v->v_quad : (ldbl_t)v->v_quad;
1970 rchk = 1; /* Check for lost precision. */
1971 nv->v_quad = v->v_quad;
1975 if (v->v_ansiu && isftyp(nt)) {
1976 /* ANSI C treats constant as unsigned */
1979 } else if (v->v_ansiu && (isityp(nt) && !isutyp(nt) &&
1980 psize(nt) > psize(ot))) {
1981 /* ANSI C treats constant as unsigned */
1986 if (nt != FLOAT && nt != DOUBLE && nt != LDOUBLE) {
1987 sz = tp->t_isfield ? tp->t_flen : size(nt);
1988 nv->v_quad = xsign(nv->v_quad, nt, sz);
1991 if (rchk && op != CVT) {
1993 nsz = tp->t_isfield ? tp->t_flen : size(nt);
1994 xmask = qlmasks[nsz] ^ qlmasks[osz];
1995 xmsk1 = qlmasks[nsz] ^ qlmasks[osz - 1];
1997 * For bitwise operations we are not interested in the
1998 * value, but in the bits itself.
2000 if (op == ORASS || op == OR || op == XOR) {
2002 * Print a warning if bits which were set are
2003 * lost due to the conversion.
2004 * This can happen with operator ORASS only.
2006 if (nsz < osz && (v->v_quad & xmask) != 0) {
2007 /* constant truncated by conv., op %s */
2008 warning(306, modtab[op].m_name);
2010 } else if (op == ANDASS || op == AND) {
2012 * Print a warning if additional bits are not all 1
2013 * and the most significant bit of the old value is 1,
2014 * or if at least one (but not all) removed bit was 0.
2017 (nv->v_quad & qbmasks[osz - 1]) != 0 &&
2018 (nv->v_quad & xmask) != xmask) {
2020 * extra bits set to 0 in conversion
2021 * of '%s' to '%s', op %s
2023 warning(309, tyname(gettyp(ot)),
2024 tyname(tp), modtab[op].m_name);
2025 } else if (nsz < osz &&
2026 (v->v_quad & xmask) != xmask &&
2027 (v->v_quad & xmask) != 0) {
2028 /* const. truncated by conv., op %s */
2029 warning(306, modtab[op].m_name);
2031 } else if ((nt != PTR && isutyp(nt)) &&
2032 (ot != PTR && !isutyp(ot)) && v->v_quad < 0) {
2034 /* assignment of negative constant to ... */
2036 } else if (op == INIT) {
2037 /* initialisation of unsigned with neg. ... */
2039 } else if (op == FARG) {
2040 /* conversion of neg. const. to ..., arg #%d */
2042 } else if (modtab[op].m_comp) {
2043 /* we get this warning already in chkcomp() */
2045 /* conversion of negative constant to ... */
2048 } else if (nv->v_quad != v->v_quad && nsz <= osz &&
2049 (v->v_quad & xmask) != 0 &&
2050 (isutyp(ot) || (v->v_quad & xmsk1) != xmsk1)) {
2052 * Loss of significant bit(s). All truncated bits
2053 * of unsigned types or all truncated bits plus the
2054 * msb of the target for signed types are considered
2055 * to be significant bits. Loss of significant bits
2056 * means that at least on of the bits was set in an
2057 * unsigned type or that at least one, but not all of
2058 * the bits was set in an signed type.
2059 * Loss of significant bits means that it is not
2060 * possible, also not with necessary casts, to convert
2061 * back to the original type. A example for a
2062 * necessary cast is:
2063 * char c; int i; c = 128;
2064 * i = c; ** yields -128 **
2065 * i = (unsigned char)c; ** yields 128 **
2067 if (op == ASSIGN && tp->t_isfield) {
2068 /* precision lost in bit-field assignment */
2070 } else if (op == ASSIGN) {
2071 /* constant truncated by assignment */
2073 } else if (op == INIT && tp->t_isfield) {
2074 /* bit-field initializer does not fit */
2076 } else if (op == INIT) {
2077 /* initializer does not fit */
2079 } else if (op == CASE) {
2080 /* case label affected by conversion */
2082 } else if (op == FARG) {
2083 /* conv. of %s to %s is out of rng., arg #%d */
2084 warning(295, tyname(gettyp(ot)), tyname(tp),
2087 /* conversion of %s to %s is out of range */
2088 warning(119, tyname(gettyp(ot)), tyname(tp));
2090 } else if (nv->v_quad != v->v_quad) {
2091 if (op == ASSIGN && tp->t_isfield) {
2092 /* precision lost in bit-field assignment */
2094 } else if (op == INIT && tp->t_isfield) {
2095 /* bit-field initializer out of range */
2097 } else if (op == CASE) {
2098 /* case label affected by conversion */
2100 } else if (op == FARG) {
2101 /* conv. of %s to %s is out of rng., arg #%d */
2102 warning(295, tyname(gettyp(ot)), tyname(tp),
2105 /* conversion of %s to %s is out of range */
2106 warning(119, tyname(gettyp(ot)), tyname(tp));
2113 * Called if incompatible types were detected.
2114 * Prints a appropriate warning.
2117 incompat(op, lt, rt)
2125 if (lt == VOID || (mp->m_binary && rt == VOID)) {
2126 /* void type illegal in expression */
2128 } else if (op == ASSIGN) {
2129 if ((lt == STRUCT || lt == UNION) &&
2130 (rt == STRUCT || rt == UNION)) {
2131 /* assignment of different structures */
2134 /* assignment type mismatch */
2137 } else if (mp->m_binary) {
2138 /* operands of %s have incompatible types */
2139 error(107, mp->m_name);
2141 /* operand of %s has incompatible type */
2142 error(108, mp->m_name);
2147 * Called if incompatible pointer types are detected.
2148 * Print an appropriate warning.
2151 illptrc(mp, ltp, rtp)
2157 if (ltp->t_tspec != PTR || rtp->t_tspec != PTR)
2158 lerror("illptrc() 1");
2160 lt = ltp->t_subt->t_tspec;
2161 rt = rtp->t_subt->t_tspec;
2163 if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION)) {
2165 /* illegal structure pointer combination */
2168 /* illegal structure pointer combination, op %s */
2169 warning(245, mp->m_name);
2173 /* illegal pointer combination */
2176 /* illegal pointer combination, op %s */
2177 warning(124, mp->m_name);
2183 * Make sure type (*tpp)->t_subt has at least the qualifiers
2184 * of tp1->t_subt and tp2->t_subt.
2187 mrgqual(tpp, tp1, tp2)
2188 type_t **tpp, *tp1, *tp2;
2190 if ((*tpp)->t_tspec != PTR ||
2191 tp1->t_tspec != PTR || tp2->t_tspec != PTR) {
2192 lerror("mrgqual()");
2195 if ((*tpp)->t_subt->t_const ==
2196 (tp1->t_subt->t_const | tp2->t_subt->t_const) &&
2197 (*tpp)->t_subt->t_volatile ==
2198 (tp1->t_subt->t_volatile | tp2->t_subt->t_volatile)) {
2202 *tpp = tduptyp(*tpp);
2203 (*tpp)->t_subt = tduptyp((*tpp)->t_subt);
2204 (*tpp)->t_subt->t_const =
2205 tp1->t_subt->t_const | tp2->t_subt->t_const;
2206 (*tpp)->t_subt->t_volatile =
2207 tp1->t_subt->t_volatile | tp2->t_subt->t_volatile;
2211 * Returns 1 if the given structure or union has a constant member
2212 * (maybe recursively).
2221 if ((t = tp->t_tspec) != STRUCT && t != UNION)
2222 lerror("conmemb()");
2223 for (m = tp->t_str->memb; m != NULL; m = m->s_nxt) {
2227 if ((t = tp->t_tspec) == STRUCT || t == UNION) {
2228 if (conmemb(m->s_type))
2242 if ((t = tp->t_tspec) == INT && tp->t_isenum)
2246 case CHAR: s = "char"; break;
2247 case UCHAR: s = "unsigned char"; break;
2248 case SCHAR: s = "signed char"; break;
2249 case SHORT: s = "short"; break;
2250 case USHORT: s = "unsigned short"; break;
2251 case INT: s = "int"; break;
2252 case UINT: s = "unsigned int"; break;
2253 case LONG: s = "long"; break;
2254 case ULONG: s = "unsigned long"; break;
2255 case QUAD: s = "long long"; break;
2256 case UQUAD: s = "unsigned long long"; break;
2257 case FLOAT: s = "float"; break;
2258 case DOUBLE: s = "double"; break;
2259 case LDOUBLE: s = "long double"; break;
2260 case PTR: s = "pointer"; break;
2261 case ENUM: s = "enum"; break;
2262 case STRUCT: s = "struct"; break;
2263 case UNION: s = "union"; break;
2264 case FUNC: s = "function"; break;
2265 case ARRAY: s = "array"; break;
2273 * Create a new node for one of the operators POINT and ARROW.
2283 if (rn->tn_op != NAME)
2284 lerror("bldstr() 1");
2285 if (rn->tn_sym->s_value.v_tspec != INT)
2286 lerror("bldstr() 2");
2287 if (rn->tn_sym->s_scl != MOS && rn->tn_sym->s_scl != MOU)
2288 lerror("bldstr() 3");
2291 * Remember if the left operand is an lvalue (structure members
2292 * are lvalues if and only if the structure itself is an lvalue).
2294 nolval = op == POINT && !ln->tn_lvalue;
2297 ln = bldamper(ln, 1);
2298 } else if (ln->tn_type->t_tspec != PTR) {
2299 if (!tflag || !isityp(ln->tn_type->t_tspec))
2300 lerror("bldstr() 4");
2301 ln = convert(NOOP, 0, tincref(gettyp(VOID), PTR), ln);
2305 ctn = getinode(LONG, rn->tn_sym->s_value.v_quad / CHAR_BIT);
2307 ctn = getinode(INT, rn->tn_sym->s_value.v_quad / CHAR_BIT);
2310 ntn = mktnode(PLUS, tincref(rn->tn_type, PTR), ln, ctn);
2311 if (ln->tn_op == CON)
2314 if (rn->tn_type->t_isfield) {
2315 ntn = mktnode(FSEL, ntn->tn_type->t_subt, ntn, NULL);
2317 ntn = mktnode(STAR, ntn->tn_type->t_subt, ntn, NULL);
2327 * Create a node for INCAFT, INCBEF, DECAFT and DECBEF.
2337 lerror("bldincdec() 1");
2339 if (ln->tn_type->t_tspec == PTR) {
2340 cn = plength(ln->tn_type);
2342 cn = getinode(INT, (quad_t)1);
2344 ntn = mktnode(op, ln->tn_type, ln, cn);
2350 * Create a tree node for the & operator
2360 if (!noign && ((t = tn->tn_type->t_tspec) == ARRAY || t == FUNC)) {
2361 /* & before array or function: ignored */
2368 if (tn->tn_op == STAR &&
2369 tn->tn_left->tn_type->t_tspec == PTR &&
2370 tn->tn_left->tn_type->t_subt == tn->tn_type) {
2371 return (tn->tn_left);
2374 ntn = mktnode(AMPER, tincref(tn->tn_type, PTR), tn, NULL);
2380 * Create a node for operators PLUS and MINUS.
2390 /* If pointer and integer, then pointer to the lhs. */
2391 if (rn->tn_type->t_tspec == PTR && isityp(ln->tn_type->t_tspec)) {
2397 if (ln->tn_type->t_tspec == PTR && rn->tn_type->t_tspec != PTR) {
2399 if (!isityp(rn->tn_type->t_tspec))
2400 lerror("bldplmi() 1");
2402 ctn = plength(ln->tn_type);
2403 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
2404 rn = convert(NOOP, 0, ctn->tn_type, rn);
2405 rn = mktnode(MULT, rn->tn_type, rn, ctn);
2406 if (rn->tn_left->tn_op == CON)
2408 ntn = mktnode(op, ln->tn_type, ln, rn);
2410 } else if (rn->tn_type->t_tspec == PTR) {
2412 if (ln->tn_type->t_tspec != PTR || op != MINUS)
2413 lerror("bldplmi() 2");
2419 ntn = mktnode(op, tp, ln, rn);
2420 if (ln->tn_op == CON && rn->tn_op == CON)
2422 ctn = plength(ln->tn_type);
2423 balance(NOOP, &ntn, &ctn);
2424 ntn = mktnode(DIV, tp, ntn, ctn);
2428 ntn = mktnode(op, ln->tn_type, ln, rn);
2435 * Create a node for operators SHL and SHR.
2445 if ((t = rn->tn_type->t_tspec) != INT && t != UINT)
2446 rn = convert(CVT, 0, gettyp(INT), rn);
2447 ntn = mktnode(op, ln->tn_type, ln, rn);
2452 * Create a node for COLON.
2458 tspec_t lt, rt, pdt;
2462 lt = ln->tn_type->t_tspec;
2463 rt = rn->tn_type->t_tspec;
2471 * Arithmetic types are balanced, all other type combinations
2472 * still need to be handled.
2474 if (isatyp(lt) && isatyp(rt)) {
2476 } else if (lt == VOID || rt == VOID) {
2478 } else if (lt == STRUCT || lt == UNION) {
2479 /* Both types must be identical. */
2480 if (rt != STRUCT && rt != UNION)
2481 lerror("bldcol() 1");
2482 if (ln->tn_type->t_str != rn->tn_type->t_str)
2483 lerror("bldcol() 2");
2484 if (incompl(ln->tn_type)) {
2485 /* unknown operand size, op %s */
2486 error(138, modtab[COLON].m_name);
2490 } else if (lt == PTR && isityp(rt)) {
2492 rn = convert(NOOP, 0, gettyp(pdt), rn);
2496 } else if (rt == PTR && isityp(lt)) {
2498 ln = convert(NOOP, 0, gettyp(pdt), ln);
2502 } else if (lt == PTR && ln->tn_type->t_subt->t_tspec == VOID) {
2504 lerror("bldcol() 4");
2506 mrgqual(&rtp, ln->tn_type, rn->tn_type);
2507 } else if (rt == PTR && rn->tn_type->t_subt->t_tspec == VOID) {
2509 lerror("bldcol() 5");
2511 mrgqual(&rtp, ln->tn_type, rn->tn_type);
2513 if (lt != PTR || rt != PTR)
2514 lerror("bldcol() 6");
2516 * XXX For now we simply take the left type. This is
2517 * probably wrong, if one type contains a functionprototype
2518 * and the other one, at the same place, only an old style
2522 mrgqual(&rtp, ln->tn_type, rn->tn_type);
2525 ntn = mktnode(COLON, rtp, ln, rn);
2531 * Create a node for an assignment operator (both = and op= ).
2541 if (ln == NULL || rn == NULL)
2542 lerror("bldasgn() 1");
2544 lt = ln->tn_type->t_tspec;
2545 rt = rn->tn_type->t_tspec;
2547 if ((op == ADDASS || op == SUBASS) && lt == PTR) {
2549 lerror("bldasgn() 2");
2550 ctn = plength(ln->tn_type);
2551 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
2552 rn = convert(NOOP, 0, ctn->tn_type, rn);
2553 rn = mktnode(MULT, rn->tn_type, rn, ctn);
2554 if (rn->tn_left->tn_op == CON)
2558 if ((op == ASSIGN || op == RETURN) && (lt == STRUCT || rt == STRUCT)) {
2559 if (rt != lt || ln->tn_type->t_str != rn->tn_type->t_str)
2560 lerror("bldasgn() 3");
2561 if (incompl(ln->tn_type)) {
2563 /* cannot return incomplete type */
2566 /* unknown operand size, op %s */
2567 error(138, modtab[op].m_name);
2573 if (op == SHLASS || op == SHRASS) {
2575 rn = convert(NOOP, 0, gettyp(INT), rn);
2579 if (op == ASSIGN || lt != PTR) {
2581 (ln->tn_type->t_isfield && rn->tn_op == CON)) {
2582 rn = convert(op, 0, ln->tn_type, rn);
2588 ntn = mktnode(op, ln->tn_type, ln, rn);
2594 * Get length of type tp->t_subt.
2603 if (tp->t_tspec != PTR)
2604 lerror("plength() 1");
2610 while (tp->t_tspec == ARRAY) {
2615 switch (tp->t_tspec) {
2617 /* pointer to function is not allowed here */
2621 /* cannot do pointer arithmetic on operand of ... */
2626 if ((elsz = tp->t_str->size) == 0)
2627 /* cannot do pointer arithmetic on operand of ... */
2632 /* cannot do pointer arithmetic on operand of ... */
2637 if ((elsz = size(tp->t_tspec)) == 0) {
2638 /* cannot do pointer arithmetic on operand of ... */
2640 } else if (elsz == -1) {
2641 lerror("plength() 2");
2646 if (elem == 0 && elsz != 0) {
2647 /* cannot do pointer arithmetic on operand of ... */
2660 return (getinode(st, (quad_t)(elem * elsz / CHAR_BIT)));
2663 #ifdef XXX_BROKEN_GCC
2680 * Do only as much as necessary to compute constant expressions.
2681 * Called only if the operator allows folding and (both) operands
2691 quad_t sl, sr, q, mask;
2695 v = xcalloc(1, sizeof (val_t));
2696 v->v_tspec = t = tn->tn_type->t_tspec;
2698 utyp = t == PTR || isutyp(t);
2699 ul = sl = tn->tn_left->tn_val->v_quad;
2700 if (modtab[tn->tn_op].m_binary)
2701 ur = sr = tn->tn_right->tn_val->v_quad;
2705 switch (tn->tn_op) {
2711 if (msb(q, t, -1) == msb(sl, t, -1))
2718 q = utyp ? ul * ur : sl * sr;
2719 if (msb(q, t, -1) != (msb(sl, t, -1) ^ msb(sr, t, -1)))
2726 q = utyp ? UQUAD_MAX : QUAD_MAX;
2728 q = utyp ? ul / ur : sl / sr;
2737 q = utyp ? ul % ur : sl % sr;
2741 q = utyp ? ul + ur : sl + sr;
2742 if (msb(sl, t, -1) != 0 && msb(sr, t, -1) != 0) {
2743 if (msb(q, t, -1) == 0)
2745 } else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) == 0) {
2746 if (msb(q, t, -1) != 0)
2751 q = utyp ? ul - ur : sl - sr;
2752 if (msb(sl, t, -1) != 0 && msb(sr, t, -1) == 0) {
2753 if (msb(q, t, -1) == 0)
2755 } else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) != 0) {
2756 if (msb(q, t, -1) != 0)
2761 q = utyp ? ul << sr : sl << sr;
2765 * The sign must be explizitly extended because
2766 * shifts of signed values are implementation dependent.
2769 q = xsign(q, t, size(t) - (int)sr);
2772 q = utyp ? ul < ur : sl < sr;
2775 q = utyp ? ul <= ur : sl <= sr;
2778 q = utyp ? ul >= ur : sl >= sr;
2781 q = utyp ? ul > ur : sl > sr;
2784 #ifdef XXX_BROKEN_GCC
2785 q = utyp ? u_quad_t_eq(ul, ur) : quad_t_eq(sl, sr);
2787 q = utyp ? ul == ur : sl == sr;
2791 q = utyp ? ul != ur : sl != sr;
2794 q = utyp ? ul & ur : sl & sr;
2797 q = utyp ? ul ^ ur : sl ^ sr;
2800 q = utyp ? ul | ur : sl | sr;
2806 mask = qlmasks[size(t)];
2808 /* XXX does not work for quads. */
2809 if (ovfl || ((q | mask) != ~(u_quad_t)0 && (q & ~mask) != 0)) {
2811 /* integer overflow detected, op %s */
2812 warning(141, modtab[tn->tn_op].m_name);
2815 v->v_quad = xsign(q, t, -1);
2817 cn = getcnode(tn->tn_type, v);
2822 #ifdef XXX_BROKEN_GCC
2832 * Same for operators whose operands are compared with 0 (test context).
2841 v = xcalloc(1, sizeof (val_t));
2842 v->v_tspec = tn->tn_type->t_tspec;
2843 if (tn->tn_type->t_tspec != INT)
2844 lerror("foldtst() 1");
2846 if (isftyp(tn->tn_left->tn_type->t_tspec)) {
2847 #ifdef XXX_BROKEN_GCC
2848 l = ldbl_t_neq(tn->tn_left->tn_val->v_ldbl, 0.0);
2850 l = tn->tn_left->tn_val->v_ldbl != 0.0;
2853 l = tn->tn_left->tn_val->v_quad != 0;
2856 if (modtab[tn->tn_op].m_binary) {
2857 if (isftyp(tn->tn_right->tn_type->t_tspec)) {
2858 #ifdef XXX_BROKEN_GCC
2859 r = ldbl_t_neq(tn->tn_right->tn_val->v_ldbl, 0.0);
2861 r = tn->tn_right->tn_val->v_ldbl != 0.0;
2864 r = tn->tn_right->tn_val->v_quad != 0;
2868 switch (tn->tn_op) {
2871 /* constant argument to NOT */
2882 lerror("foldtst() 1");
2885 return (getcnode(tn->tn_type, v));
2889 * Same for operands with floating point type.
2899 v = xcalloc(1, sizeof (val_t));
2900 v->v_tspec = t = tn->tn_type->t_tspec;
2903 lerror("foldflt() 1");
2905 if (t != tn->tn_left->tn_type->t_tspec)
2906 lerror("foldflt() 2");
2907 if (modtab[tn->tn_op].m_binary && t != tn->tn_right->tn_type->t_tspec)
2908 lerror("foldflt() 3");
2910 l = tn->tn_left->tn_val->v_ldbl;
2911 if (modtab[tn->tn_op].m_binary)
2912 r = tn->tn_right->tn_val->v_ldbl;
2914 switch (tn->tn_op) {
2929 v->v_ldbl = l < 0 ? -FLT_MAX : FLT_MAX;
2930 } else if (t == DOUBLE) {
2931 v->v_ldbl = l < 0 ? -DBL_MAX : DBL_MAX;
2933 v->v_ldbl = l < 0 ? -LDBL_MAX : LDBL_MAX;
2964 lerror("foldflt() 4");
2967 if (isnan((double)v->v_ldbl))
2968 lerror("foldflt() 5");
2969 if (isinf((double)v->v_ldbl) ||
2971 (v->v_ldbl > FLT_MAX || v->v_ldbl < -FLT_MAX)) ||
2973 (v->v_ldbl > DBL_MAX || v->v_ldbl < -DBL_MAX))) {
2974 /* floating point overflow detected, op %s */
2975 warning(142, modtab[tn->tn_op].m_name);
2977 v->v_ldbl = v->v_ldbl < 0 ? -FLT_MAX : FLT_MAX;
2978 } else if (t == DOUBLE) {
2979 v->v_ldbl = v->v_ldbl < 0 ? -DBL_MAX : DBL_MAX;
2981 v->v_ldbl = v->v_ldbl < 0 ? -LDBL_MAX: LDBL_MAX;
2985 return (getcnode(tn->tn_type, v));
2989 * Create a constant node for sizeof.
2999 while (tp->t_tspec == ARRAY) {
3004 /* cannot take size of incomplete type */
3008 switch (tp->t_tspec) {
3010 /* cannot take size of function */
3017 /* cannot take size of incomplete type */
3021 elsz = tp->t_str->size;
3026 /* cannot take size of incomplete type */
3031 if (tp->t_isfield) {
3032 /* cannot take size of bit-field */
3035 if (tp->t_tspec == VOID) {
3036 /* cannot take size of void */
3040 elsz = size(tp->t_tspec);
3042 lerror("bldszof() 1");
3053 return (getinode(st, (quad_t)(elem * elsz / CHAR_BIT)));
3072 ot = tn->tn_type->t_tspec;
3076 * XXX ANSI C requires scalar types or void (Plauger&Brodie).
3077 * But this seams really questionable.
3079 } else if (nt == STRUCT || nt == UNION || nt == ARRAY || nt == FUNC) {
3080 /* invalid cast expression */
3083 } else if (ot == STRUCT || ot == UNION) {
3084 /* invalid cast expression */
3087 } else if (ot == VOID) {
3088 /* improper cast of void expression */
3091 } else if (isityp(nt) && issclt(ot)) {
3093 } else if (isftyp(nt) && isatyp(ot)) {
3095 } else if (nt == PTR && isityp(ot)) {
3097 } else if (nt == PTR && ot == PTR) {
3098 if (!tp->t_subt->t_const && tn->tn_type->t_subt->t_const) {
3100 /* cast discards 'const' from ... */
3104 /* invalid cast expression */
3109 tn = convert(CVT, 0, tp, tn);
3116 * Create the node for a function argument.
3117 * All necessary conversions and type checks are done in funccall(), because
3118 * in funcarg() we have no information about expected argument types.
3122 tnode_t *args, *arg;
3127 * If there was a serious error in the expression for the argument,
3128 * create a dummy argument so the positions of the remaining arguments
3132 arg = getinode(INT, (quad_t)0);
3134 ntn = mktnode(PUSH, arg->tn_type, arg, args);
3140 * Create the node for a function call. Also check types of
3141 * function arguments and insert conversions, if necessary.
3144 funccall(func, args)
3145 tnode_t *func, *args;
3153 if (func->tn_op == NAME && func->tn_type->t_tspec == FUNC) {
3160 * after cconv() func will always be a pointer to a function
3161 * if it is a valid function designator.
3165 if (func->tn_type->t_tspec != PTR ||
3166 func->tn_type->t_subt->t_tspec != FUNC) {
3167 /* illegal function */
3172 args = chkfarg(func->tn_type->t_subt, args);
3174 ntn = mktnode(fcop, func->tn_type->t_subt->t_subt, func, args);
3180 * Check types of all function arguments and insert conversions,
3185 type_t *ftp; /* type of called function */
3186 tnode_t *args; /* arguments */
3191 int narg, npar, n, i;
3193 /* get # of args in the prototype */
3195 for (asym = ftp->t_args; asym != NULL; asym = asym->s_nxt)
3198 /* get # of args in function call */
3200 for (arg = args; arg != NULL; arg = arg->tn_right)
3204 if (ftp->t_proto && npar != narg && !(ftp->t_vararg && npar < narg)) {
3205 /* argument mismatch: %d arg%s passed, %d expected */
3206 error(150, narg, narg > 1 ? "s" : "", npar);
3210 for (n = 1; n <= narg; n++) {
3213 * The rightmost argument is at the top of the argument
3216 for (i = narg, arg = args; i > n; i--, arg = arg->tn_right) ;
3218 /* some things which are always not allowd */
3219 if ((at = arg->tn_left->tn_type->t_tspec) == VOID) {
3220 /* void expressions may not be arguments, arg #%d */
3223 } else if ((at == STRUCT || at == UNION) &&
3224 incompl(arg->tn_left->tn_type)) {
3225 /* argument cannot have unknown size, arg #%d */
3228 } else if (isityp(at) && arg->tn_left->tn_type->t_isenum &&
3229 incompl(arg->tn_left->tn_type)) {
3230 /* argument cannot have unknown size, arg #%d */
3234 /* class conversions (arg in value context) */
3235 arg->tn_left = cconv(arg->tn_left);
3238 arg->tn_left = parg(n, asym->s_type, arg->tn_left);
3240 arg->tn_left = promote(NOOP, 1, arg->tn_left);
3242 arg->tn_type = arg->tn_left->tn_type;
3252 * Compare the type of an argument with the corresponding type of a
3253 * prototype parameter. If it is a valid combination, but both types
3254 * are not the same, insert a conversion to convert the argument into
3255 * the type of the parameter.
3259 int n; /* pos of arg */
3260 type_t *tp; /* expected type (from prototype) */
3261 tnode_t *tn; /* argument */
3266 ln = xcalloc(1, sizeof (tnode_t));
3267 ln->tn_type = tduptyp(tp);
3268 ln->tn_type->t_const = 0;
3270 if (typeok(FARG, n, ln, tn)) {
3271 if (!eqtype(tp, tn->tn_type, 1, 0, (warn = 0, &warn)) || warn)
3272 tn = convert(FARG, n, tp, tn);
3279 * Return the value of an integral constant expression.
3280 * If the expression is not constant or its type is not an integer
3281 * type, an error message is printed.
3292 tn = promote(NOOP, 0, tn);
3294 v = xcalloc(1, sizeof (val_t));
3298 lerror("constant() 1");
3304 v->v_tspec = tn->tn_type->t_tspec;
3306 if (tn->tn_op == CON) {
3307 if (tn->tn_type->t_tspec != tn->tn_val->v_tspec)
3308 lerror("constant() 2");
3309 if (isityp(tn->tn_val->v_tspec)) {
3310 v->v_ansiu = tn->tn_val->v_ansiu;
3311 v->v_quad = tn->tn_val->v_quad;
3314 v->v_quad = tn->tn_val->v_ldbl;
3319 /* integral constant expression expected */
3322 if (!isityp(v->v_tspec))
3329 * Perform some tests on expressions which can't be done in build() and
3330 * functions called by build(). These tests must be done here because
3331 * we need some information about the context in which the operations
3333 * After all tests are performed, expr() frees the memory which is used
3334 * for the expression.
3337 expr(tn, vctx, tctx)
3341 if (tn == NULL && nerr == 0)
3349 /* expr() is also called in global initialisations */
3350 if (dcs->d_ctx != EXTERN)
3353 chkmisc(tn, vctx, tctx, !tctx, 0, 0, 0);
3354 if (tn->tn_op == ASSIGN) {
3356 /* assignment in conditional context */
3358 } else if (tn->tn_op == CON) {
3359 if (hflag && tctx && !ccflg)
3360 /* constant in conditional context */
3363 if (!modtab[tn->tn_op].m_sideeff) {
3365 * for left operands of COMMA this warning is already
3368 if (tn->tn_op != COMMA && !vctx && !tctx)
3374 /* free the tree memory */
3385 while (!modtab[tn->tn_op].m_sideeff) {
3386 if (tn->tn_op == CVT && tn->tn_type->t_tspec == VOID) {
3388 } else if (tn->tn_op == LOGAND || tn->tn_op == LOGOR) {
3390 * && and || have a side effect if the right operand
3391 * has a side effect.
3394 } else if (tn->tn_op == QUEST) {
3396 * ? has a side effect if at least one of its right
3397 * operands has a side effect
3400 } else if (tn->tn_op == COLON) {
3402 * : has a side effect if at least one of its operands
3405 if (modtab[tn->tn_left->tn_op].m_sideeff) {
3407 } else if (modtab[tn->tn_right->tn_op].m_sideeff) {
3416 if (!modtab[tn->tn_op].m_sideeff)
3417 /* expression has null effect */
3422 * Dump an expression to stdout
3423 * only used for debugging
3433 (void)printf("%*s%s\n", offs, "", "NULL");
3436 (void)printf("%*sop %s ", offs, "", modtab[tn->tn_op].m_name);
3438 if (tn->tn_op == NAME) {
3439 (void)printf("%s: %s ",
3440 tn->tn_sym->s_name, scltoa(tn->tn_sym->s_scl));
3441 } else if (tn->tn_op == CON && isftyp(tn->tn_type->t_tspec)) {
3442 (void)printf("%#g ", (double)tn->tn_val->v_ldbl);
3443 } else if (tn->tn_op == CON && isityp(tn->tn_type->t_tspec)) {
3444 uq = tn->tn_val->v_quad;
3445 (void)printf("0x %08lx %08lx ", (long)(uq >> 32) & 0xffffffffl,
3446 (long)uq & 0xffffffffl);
3447 } else if (tn->tn_op == CON) {
3448 if (tn->tn_type->t_tspec != PTR)
3449 lerror("displexpr() 1");
3450 (void)printf("0x%0*lx ", (int)(sizeof (void *) * CHAR_BIT / 4),
3451 (u_long)tn->tn_val->v_quad);
3452 } else if (tn->tn_op == STRING) {
3453 if (tn->tn_strg->st_tspec == CHAR) {
3454 (void)printf("\"%s\"", tn->tn_strg->st_cp);
3458 n = MB_CUR_MAX * (tn->tn_strg->st_len + 1);
3460 (void)wcstombs(s, tn->tn_strg->st_wcp, n);
3461 (void)printf("L\"%s\"", s);
3465 } else if (tn->tn_op == FSEL) {
3466 (void)printf("o=%d, l=%d ", tn->tn_type->t_foffs,
3467 tn->tn_type->t_flen);
3469 (void)printf("%s\n", ttos(tn->tn_type));
3470 if (tn->tn_op == NAME || tn->tn_op == CON || tn->tn_op == STRING)
3472 displexpr(tn->tn_left, offs + 2);
3473 if (modtab[tn->tn_op].m_binary ||
3474 (tn->tn_op == PUSH && tn->tn_right != NULL)) {
3475 displexpr(tn->tn_right, offs + 2);
3480 * Called by expr() to recursively perform some tests.
3484 chkmisc(tn, vctx, tctx, eqwarn, fcall, rvdisc, szof)
3486 int vctx, tctx, eqwarn, fcall, rvdisc, szof;
3490 int nrvdisc, cvctx, ctctx;
3500 mp = &modtab[op = tn->tn_op];
3504 if (ln->tn_op == NAME && (reached || rchflg)) {
3506 setsflg(ln->tn_sym);
3507 setuflg(ln->tn_sym, fcall, szof);
3509 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3510 /* check the range of array indices */
3511 chkaidx(ln->tn_left, 1);
3514 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3515 /* check the range of array indices */
3516 chkaidx(ln->tn_left, 0);
3533 if (ln->tn_op == NAME && (reached || rchflg)) {
3534 sc = ln->tn_sym->s_scl;
3536 * Look if there was a asm statement in one of the
3537 * compound statements we are in. If not, we don't
3540 for (di = dcs; di != NULL; di = di->d_nxt) {
3544 if (sc != EXTERN && sc != STATIC &&
3545 !ln->tn_sym->s_set && !szof && di == NULL) {
3546 /* %s may be used before set */
3547 warning(158, ln->tn_sym->s_name);
3548 setsflg(ln->tn_sym);
3550 setuflg(ln->tn_sym, 0, 0);
3554 if (ln->tn_op == NAME && !szof && (reached || rchflg)) {
3555 setsflg(ln->tn_sym);
3556 if (ln->tn_sym->s_scl == EXTERN)
3559 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3560 /* check the range of array indices */
3561 chkaidx(ln->tn_left, 0);
3564 if (ln->tn_op != AMPER || ln->tn_left->tn_op != NAME)
3565 lerror("chkmisc() 1");
3567 outcall(tn, vctx || tctx, rvdisc);
3570 /* equality operator "==" found where "=" was exp. */
3571 if (hflag && eqwarn)
3578 /* LINTED (enumeration values not handled in switch) */
3585 * values of operands of ':' are not used if the type of at least
3586 * one of the operands (for gcc compatibility) is void
3587 * XXX test/value context of QUEST should probably be used as
3588 * context for both operands of COLON
3590 if (op == COLON && tn->tn_type->t_tspec == VOID)
3592 nrvdisc = op == CVT && tn->tn_type->t_tspec == VOID;
3593 chkmisc(ln, cvctx, ctctx, mp->m_eqwarn, op == CALL, nrvdisc, szof);
3598 chkmisc(rn, 0, 0, mp->m_eqwarn, 0, 0, szof);
3602 chkmisc(rn, 0, 1, mp->m_eqwarn, 0, 0, szof);
3605 chkmisc(rn, cvctx, ctctx, mp->m_eqwarn, 0, 0, szof);
3609 chkmisc(rn, 1, 0, mp->m_eqwarn, 0, 0, szof);
3616 * Checks the range of array indices, if possible.
3617 * amper is set if only the address of the element is used. This
3618 * means that the index is allowd to refere to the first element
3634 /* We can only check constant indices. */
3635 if (rn->tn_op != CON)
3638 /* Return if the left node does not stem from an array. */
3639 if (ln->tn_op != AMPER)
3641 if (ln->tn_left->tn_op != STRING && ln->tn_left->tn_op != NAME)
3643 if (ln->tn_left->tn_type->t_tspec != ARRAY)
3647 * For incomplete array types, we can print a warning only if
3648 * the index is negative.
3650 if (incompl(ln->tn_left->tn_type) && rn->tn_val->v_quad >= 0)
3653 /* Get the size of one array element */
3654 if ((elsz = length(ln->tn_type->t_subt, NULL)) == 0)
3658 /* Change the unit of the index from bytes to element size. */
3659 if (isutyp(rn->tn_type->t_tspec)) {
3660 con = (u_quad_t)rn->tn_val->v_quad / elsz;
3662 con = rn->tn_val->v_quad / elsz;
3665 dim = ln->tn_left->tn_type->t_dim + (amper ? 1 : 0);
3667 if (!isutyp(rn->tn_type->t_tspec) && con < 0) {
3668 /* array subscript cannot be negative: %ld */
3669 warning(167, (long)con);
3670 } else if (dim > 0 && (u_quad_t)con >= dim) {
3671 /* array subscript cannot be > %d: %ld */
3672 warning(168, dim - 1, (long)con);
3677 * Check for ordered comparisions of unsigned values with 0.
3687 lt = ln->tn_type->t_tspec;
3688 rt = rn->tn_type->t_tspec;
3691 if (ln->tn_op != CON && rn->tn_op != CON)
3694 if (!isityp(lt) || !isityp(rt))
3697 if ((hflag || pflag) && lt == CHAR && rn->tn_op == CON &&
3698 (rn->tn_val->v_quad < 0 ||
3699 rn->tn_val->v_quad > ~(~0 << (CHAR_BIT - 1)))) {
3700 /* nonportable character comparision, op %s */
3701 warning(230, mp->m_name);
3704 if ((hflag || pflag) && rt == CHAR && ln->tn_op == CON &&
3705 (ln->tn_val->v_quad < 0 ||
3706 ln->tn_val->v_quad > ~(~0 << (CHAR_BIT - 1)))) {
3707 /* nonportable character comparision, op %s */
3708 warning(230, mp->m_name);
3711 if (isutyp(lt) && !isutyp(rt) &&
3712 rn->tn_op == CON && rn->tn_val->v_quad <= 0) {
3713 if (rn->tn_val->v_quad < 0) {
3714 /* comparision of %s with %s, op %s */
3715 warning(162, tyname(ln->tn_type), "negative constant",
3717 } else if (op == LT || op == GE || (hflag && op == LE)) {
3718 /* comparision of %s with %s, op %s */
3719 warning(162, tyname(ln->tn_type), "0", mp->m_name);
3723 if (isutyp(rt) && !isutyp(lt) &&
3724 ln->tn_op == CON && ln->tn_val->v_quad <= 0) {
3725 if (ln->tn_val->v_quad < 0) {
3726 /* comparision of %s with %s, op %s */
3727 warning(162, "negative constant", tyname(rn->tn_type),
3729 } else if (op == GT || op == LE || (hflag && op == GE)) {
3730 /* comparision of %s with %s, op %s */
3731 warning(162, "0", tyname(rn->tn_type), mp->m_name);
3738 * Takes an expression an returns 0 if this expression can be used
3739 * for static initialisation, otherwise -1.
3741 * Constant initialisation expressions must be costant or an address
3742 * of a static object with an optional offset. In the first case,
3743 * the result is returned in *offsp. In the second case, the static
3744 * object is returned in *symp and the offset in *offsp.
3746 * The expression can consist of PLUS, MINUS, AMPER, NAME, STRING and
3747 * CON. Type conversions are allowed if they do not change binary
3748 * representation (including width).
3751 conaddr(tn, symp, offsp)
3757 ptrdiff_t offs1, offs2;
3760 switch (tn->tn_op) {
3762 if (tn->tn_right->tn_op != CON)
3767 if (tn->tn_left->tn_op == CON) {
3768 offs1 = (ptrdiff_t)tn->tn_left->tn_val->v_quad;
3769 if (conaddr(tn->tn_right, &sym, &offs2) == -1)
3771 } else if (tn->tn_right->tn_op == CON) {
3772 offs2 = (ptrdiff_t)tn->tn_right->tn_val->v_quad;
3773 if (tn->tn_op == MINUS)
3775 if (conaddr(tn->tn_left, &sym, &offs1) == -1)
3781 *offsp = offs1 + offs2;
3784 if (tn->tn_left->tn_op == NAME) {
3785 *symp = tn->tn_left->tn_sym;
3787 } else if (tn->tn_left->tn_op == STRING) {
3789 * If this would be the front end of a compiler we
3790 * would return a label instead of 0.
3796 t = tn->tn_type->t_tspec;
3797 ot = tn->tn_left->tn_type->t_tspec;
3798 if ((!isityp(t) && t != PTR) || (!isityp(ot) && ot != PTR)) {
3800 } else if (psize(t) != psize(ot)) {
3803 if (conaddr(tn->tn_left, symp, offsp) == -1)
3813 * Concatenate two string constants.
3816 catstrg(strg1, strg2)
3817 strg_t *strg1, *strg2;
3819 size_t len1, len2, len;
3821 if (strg1->st_tspec != strg2->st_tspec) {
3822 /* cannot concatenate wide and regular string literals */
3827 len = (len1 = strg1->st_len) + (len2 = strg2->st_len);
3829 if (strg1->st_tspec == CHAR) {
3830 strg1->st_cp = xrealloc(strg1->st_cp, len + 1);
3831 (void)memcpy(strg1->st_cp + len1, strg2->st_cp, len2 + 1);
3834 strg1->st_wcp = xrealloc(strg1->st_wcp,
3835 (len + 1) * sizeof (wchar_t));
3836 (void)memcpy(strg1->st_wcp + len1, strg2->st_wcp,
3837 (len2 + 1) * sizeof (wchar_t));
3838 free(strg2->st_wcp);
3846 * Print a warning if the given node has operands which should be
3849 * XXX Does not work if an operand is a constant expression. Constant
3850 * expressions are already folded.
3865 mp = &modtab[tn->tn_op];
3868 for (ln = tn->tn_left; ln->tn_op == CVT; ln = ln->tn_left)
3869 lparn |= ln->tn_parn;
3870 lparn |= ln->tn_parn;
3875 for (rn = tn->tn_right; tn->tn_op == CVT; rn = rn->tn_left)
3876 rparn |= rn->tn_parn;
3877 rparn |= rn->tn_parn;
3883 switch (tn->tn_op) {
3886 if (!lparn && (lop == PLUS || lop == MINUS)) {
3888 } else if (!rparn && (rop == PLUS || rop == MINUS)) {
3893 if (!lparn && lop == LOGAND) {
3895 } else if (!rparn && rop == LOGAND) {
3902 if (!lparn && lop != tn->tn_op) {
3903 if (lop == PLUS || lop == MINUS) {
3905 } else if (lop == AND || lop == XOR) {
3909 if (!warn && !rparn && rop != tn->tn_op) {
3910 if (rop == PLUS || rop == MINUS) {
3912 } else if (rop == AND || rop == XOR) {
3917 /* LINTED (enumeration values not handled in switch) */
3922 /* precedence confusion possible: parenthesize! */