1 /* $NetBSD: tree.c,v 1.61 2009/10/02 19:02:16 christos Exp $ */
4 * Copyright (c) 1994, 1995 Jochen Pohl
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Jochen Pohl for
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #if HAVE_NBTOOL_CONFIG_H
35 #include "nbtool_config.h"
38 #include <sys/cdefs.h>
39 #if defined(__RCSID) && !defined(lint)
40 __RCSID("$NetBSD: tree.c,v 1.61 2009/10/02 19:02:16 christos Exp $");
54 /* Various flags for each operator. */
55 static mod_t modtab
[NOPS
];
57 static tnode_t
*getinode(tspec_t
, int64_t);
58 static void ptrcmpok(op_t
, tnode_t
*, tnode_t
*);
59 static int asgntypok(op_t
, int, tnode_t
*, tnode_t
*);
60 static void chkbeop(op_t
, tnode_t
*, tnode_t
*);
61 static void chkeop2(op_t
, int, tnode_t
*, tnode_t
*);
62 static void chkeop1(op_t
, int, tnode_t
*, tnode_t
*);
63 static tnode_t
*mktnode(op_t
, type_t
*, tnode_t
*, tnode_t
*);
64 static void balance(op_t
, tnode_t
**, tnode_t
**);
65 static void incompat(op_t
, tspec_t
, tspec_t
);
66 static void illptrc(mod_t
*, type_t
*, type_t
*);
67 static void mrgqual(type_t
**, type_t
*, type_t
*);
68 static int conmemb(type_t
*);
69 static void ptconv(int, tspec_t
, tspec_t
, type_t
*, tnode_t
*);
70 static void iiconv(op_t
, int, tspec_t
, tspec_t
, type_t
*, tnode_t
*);
71 static void piconv(op_t
, tspec_t
, type_t
*, tnode_t
*);
72 static void ppconv(op_t
, tnode_t
*, type_t
*);
73 static tnode_t
*bldstr(op_t
, tnode_t
*, tnode_t
*);
74 static tnode_t
*bldincdec(op_t
, tnode_t
*);
75 static tnode_t
*bldri(op_t
, tnode_t
*);
76 static tnode_t
*bldamper(tnode_t
*, int);
77 static tnode_t
*bldplmi(op_t
, tnode_t
*, tnode_t
*);
78 static tnode_t
*bldshft(op_t
, tnode_t
*, tnode_t
*);
79 static tnode_t
*bldcol(tnode_t
*, tnode_t
*);
80 static tnode_t
*bldasgn(op_t
, tnode_t
*, tnode_t
*);
81 static tnode_t
*plength(type_t
*);
82 static tnode_t
*fold(tnode_t
*);
83 static tnode_t
*foldtst(tnode_t
*);
84 static tnode_t
*foldflt(tnode_t
*);
85 static tnode_t
*chkfarg(type_t
*, tnode_t
*);
86 static tnode_t
*parg(int, type_t
*, tnode_t
*);
87 static void nulleff(tnode_t
*);
88 static void displexpr(tnode_t
*, int);
89 static void chkaidx(tnode_t
*, int);
90 static void chkcomp(op_t
, tnode_t
*, tnode_t
*);
91 static void precconf(tnode_t
*);
93 extern sig_atomic_t fpe
;
96 * Initialize mods of operators.
105 { ARROW
, { 1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,
107 { POINT
, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
109 { NOT
, { 0,1,0,1,0,1,0,1,0,0,0,0,0,0,0,1,0,0,
111 { COMPL
, { 0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,1,1,
113 { INCBEF
, { 0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,
115 { DECBEF
, { 0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,
117 { INCAFT
, { 0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,
119 { DECAFT
, { 0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,
121 { UPLUS
, { 0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,1,0,
123 { UMINUS
, { 0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,1,1,0,
125 { STAR
, { 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,
127 { AMPER
, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
129 { MULT
, { 1,0,0,0,1,1,1,0,1,0,0,1,0,0,0,1,1,0,
131 { DIV
, { 1,0,0,0,1,1,1,0,1,0,1,1,0,0,0,1,1,0,
133 { MOD
, { 1,0,1,0,0,1,1,0,1,0,1,1,0,0,0,1,1,0,
135 { PLUS
, { 1,0,0,1,0,1,1,0,1,0,0,0,0,0,0,1,0,0,
137 { MINUS
, { 1,0,0,1,0,1,1,0,1,0,0,0,0,0,0,1,0,0,
139 { SHL
, { 1,0,1,0,0,1,1,0,0,0,0,0,1,0,0,1,1,0,
141 { SHR
, { 1,0,1,0,0,1,1,0,0,0,1,0,1,0,0,1,1,0,
143 { LT
, { 1,1,0,1,0,1,1,0,1,0,1,1,0,1,1,0,1,0,
145 { LE
, { 1,1,0,1,0,1,1,0,1,0,1,1,0,1,1,0,1,0,
147 { GT
, { 1,1,0,1,0,1,1,0,1,0,1,1,0,1,1,0,1,0,
149 { GE
, { 1,1,0,1,0,1,1,0,1,0,1,1,0,1,1,0,1,0,
151 { EQ
, { 1,1,0,1,0,1,1,0,1,0,0,0,0,1,1,0,1,0,
153 { NE
, { 1,1,0,1,0,1,1,0,1,0,0,0,0,1,1,0,1,0,
155 { AND
, { 1,0,1,0,0,1,1,0,1,0,0,0,1,0,0,1,0,0,
157 { XOR
, { 1,0,1,0,0,1,1,0,1,0,0,0,1,0,0,1,0,0,
159 { OR
, { 1,0,1,0,0,1,1,0,1,0,0,0,1,0,0,1,0,0,
161 { LOGAND
, { 1,1,0,1,0,1,0,1,0,0,0,0,0,0,0,1,0,0,
163 { LOGOR
, { 1,1,0,1,0,1,0,1,0,0,0,0,1,0,0,1,0,0,
165 { QUEST
, { 1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,
167 { COLON
, { 1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,
169 { ASSIGN
, { 1,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,
171 { MULASS
, { 1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,
173 { DIVASS
, { 1,0,0,0,1,0,0,0,0,1,0,1,0,0,0,1,0,0,
175 { MODASS
, { 1,0,1,0,0,0,0,0,0,1,0,1,0,0,0,1,0,0,
177 { ADDASS
, { 1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,
179 { SUBASS
, { 1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,
181 { SHLASS
, { 1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,
183 { SHRASS
, { 1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,
185 { ANDASS
, { 1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,
187 { XORASS
, { 1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,
189 { ORASS
, { 1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,
191 { NAME
, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
193 { CON
, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
195 { STRING
, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
197 { FSEL
, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
199 { CALL
, { 1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,
201 { COMMA
, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,
203 { CVT
, { 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,
205 { ICALL
, { 1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,
207 { LOAD
, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
209 { PUSH
, { 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,
211 { RETURN
, { 1,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,
213 { INIT
, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,
215 { FARG
, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,
217 { NOOP
, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, NULL
} }
221 for (i
= 0; imods
[i
].op
!= NOOP
; i
++)
222 STRUCT_ASSIGN(modtab
[imods
[i
].op
], imods
[i
].m
);
226 * Increase degree of reference.
227 * This is most often used to change type "T" in type "pointer to T".
230 incref(type_t
*tp
, tspec_t t
)
234 tp2
= getblk(sizeof (type_t
));
241 * same for use in expressions
244 tincref(type_t
*tp
, tspec_t t
)
248 tp2
= tgetblk(sizeof (type_t
));
255 * Create a node for a constant.
258 getcnode(type_t
*tp
, val_t
*v
)
265 n
->tn_val
= tgetblk(sizeof (val_t
));
266 n
->tn_val
->v_tspec
= tp
->t_tspec
;
267 n
->tn_val
->v_ansiu
= v
->v_ansiu
;
268 n
->tn_val
->v_u
= v
->v_u
;
274 * Create a node for a integer constant.
277 getinode(tspec_t t
, int64_t q
)
283 n
->tn_type
= gettyp(t
);
284 n
->tn_val
= tgetblk(sizeof (val_t
));
285 n
->tn_val
->v_tspec
= t
;
286 n
->tn_val
->v_quad
= q
;
291 * Create a node for a name (symbol table entry).
292 * ntok is the token which follows the name.
295 getnnode(sym_t
*sym
, int ntok
)
299 if (sym
->s_scl
== NOSCL
) {
302 if (ntok
== T_LPARN
) {
304 /* function implicitly declared to ... */
308 * XXX if tflag is set the symbol should be
309 * exported to level 0
311 sym
->s_type
= incref(sym
->s_type
, FUNC
);
315 error(99, sym
->s_name
);
318 if (strcmp(sym
->s_name
, "__FUNCTION__") == 0) {
321 } else if (strcmp(sym
->s_name
, "__func__") == 0) {
326 error(99, sym
->s_name
);
330 sym
->s_type
= incref(gettyp(CHAR
), PTR
);
331 sym
->s_type
->t_const
= 1;
337 if (sym
->s_kind
!= FVFT
&& sym
->s_kind
!= FMOS
)
338 LERROR("getnnode()");
341 n
->tn_type
= sym
->s_type
;
342 if (sym
->s_scl
!= ENUMCON
) {
345 if (sym
->s_kind
== FVFT
&& sym
->s_type
->t_tspec
!= FUNC
)
349 n
->tn_val
= tgetblk(sizeof (val_t
));
350 *n
->tn_val
= sym
->s_value
;
357 * Create a node for a string.
360 getsnode(strg_t
*strg
)
370 n
->tn_type
= tincref(gettyp(strg
->st_tspec
), ARRAY
);
371 n
->tn_type
->t_dim
= len
+ 1;
374 n
->tn_strg
= tgetblk(sizeof (strg_t
));
375 n
->tn_strg
->st_tspec
= strg
->st_tspec
;
376 n
->tn_strg
->st_len
= len
;
378 if (strg
->st_tspec
== CHAR
) {
379 n
->tn_strg
->st_cp
= tgetblk(len
+ 1);
380 (void)memcpy(n
->tn_strg
->st_cp
, strg
->st_cp
, len
+ 1);
383 n
->tn_strg
->st_wcp
= tgetblk((len
+ 1) * sizeof (wchar_t));
384 (void)memcpy(n
->tn_strg
->st_wcp
, strg
->st_wcp
,
385 (len
+ 1) * sizeof (wchar_t));
394 * Returns a symbol which has the same name as the msym argument and is a
395 * member of the struct or union specified by the tn argument.
398 strmemb(tnode_t
*tn
, op_t op
, sym_t
*msym
)
407 * Remove the member if it was unknown until now (Which means
408 * that no defined struct or union has a member with the same name).
410 if (msym
->s_scl
== NOSCL
) {
411 /* undefined struct/union member: %s */
412 error(101, msym
->s_name
);
416 msym
->s_styp
= tgetblk(sizeof (str_t
));
417 msym
->s_styp
->stag
= tgetblk(sizeof (sym_t
));
418 msym
->s_styp
->stag
->s_name
= unnamed
;
419 msym
->s_value
.v_tspec
= INT
;
423 /* Set str to the tag of which msym is expected to be a member. */
425 t
= (tp
= tn
->tn_type
)->t_tspec
;
427 if (t
== STRUCT
|| t
== UNION
)
429 } else if (op
== ARROW
&& t
== PTR
) {
430 t
= (tp
= tp
->t_subt
)->t_tspec
;
431 if (t
== STRUCT
|| t
== UNION
)
436 * If this struct/union has a member with the name of msym, return
440 for (sym
= msym
; sym
!= NULL
; sym
= sym
->s_link
) {
441 if (sym
->s_scl
!= MOS
&& sym
->s_scl
!= MOU
)
443 if (sym
->s_styp
!= str
)
445 if (strcmp(sym
->s_name
, msym
->s_name
) != 0)
452 * Set eq to 0 if there are struct/union members with the same name
453 * and different types and/or offsets.
456 for (csym
= msym
; csym
!= NULL
; csym
= csym
->s_link
) {
457 if (csym
->s_scl
!= MOS
&& csym
->s_scl
!= MOU
)
459 if (strcmp(msym
->s_name
, csym
->s_name
) != 0)
461 for (sym
= csym
->s_link
; sym
!= NULL
; sym
= sym
->s_link
) {
464 if (sym
->s_scl
!= MOS
&& sym
->s_scl
!= MOU
)
466 if (strcmp(csym
->s_name
, sym
->s_name
) != 0)
468 if (csym
->s_value
.v_quad
!= sym
->s_value
.v_quad
) {
473 eq
= eqtype(csym
->s_type
, sym
->s_type
, 0, 0, &w
) && !w
;
476 if (csym
->s_field
!= sym
->s_field
) {
485 if (tp1
->t_flen
!= tp2
->t_flen
) {
489 if (tp1
->t_foffs
!= tp2
->t_foffs
) {
500 * Now handle the case in which the left operand refers really
501 * to a struct/union, but the right operand is not member of it.
504 /* illegal member use: %s */
506 warning(102, msym
->s_name
);
508 error(102, msym
->s_name
);
514 * Now the left operand of ARROW does not point to a struct/union
515 * or the left operand of POINT is no struct/union.
519 /* left operand of "." must be struct/union object */
526 /* left operand of "->" must be pointer to ... */
527 if (tflag
&& tn
->tn_type
->t_tspec
== PTR
) {
535 /* non-unique member requires struct/union %s */
536 error(105, op
== POINT
? "object" : "pointer");
538 /* unacceptable operand of %s */
539 error(111, modtab
[op
].m_name
);
547 * Create a tree node. Called for most operands except function calls,
552 * rn if not NULL, right operand
555 build(op_t op
, tnode_t
*ln
, tnode_t
*rn
)
563 /* If there was an error in one of the operands, return. */
564 if (ln
== NULL
|| (mp
->m_binary
&& rn
== NULL
))
568 * Apply class conversions to the left operand, but only if its
569 * value is needed or it is compaired with null.
571 if (mp
->m_vctx
|| mp
->m_tctx
)
574 * The right operand is almost always in a test or value context,
575 * except if it is a struct or union member.
577 if (mp
->m_binary
&& op
!= ARROW
&& op
!= POINT
)
581 * Print some warnings for comparisons of unsigned values with
582 * constants lower than or equal to null. This must be done
583 * before promote() because otherwise unsigned char and unsigned
584 * short would be promoted to int. Also types are tested to be
585 * CHAR, which would also become int.
591 * Promote the left operand if it is in a test or value context
593 if (mp
->m_vctx
|| mp
->m_tctx
)
594 ln
= promote(op
, 0, ln
);
596 * Promote the right operand, but only if it is no struct or
597 * union member, or if it is not to be assigned to the left operand
599 if (mp
->m_binary
&& op
!= ARROW
&& op
!= POINT
&&
600 op
!= ASSIGN
&& op
!= RETURN
) {
601 rn
= promote(op
, 0, rn
);
605 * If the result of the operation is different for signed or
606 * unsigned operands and one of the operands is signed only in
607 * ANSI C, print a warning.
609 if (mp
->m_tlansiu
&& ln
->tn_op
== CON
&& ln
->tn_val
->v_ansiu
) {
610 /* ANSI C treats constant as unsigned, op %s */
611 warning(218, mp
->m_name
);
612 ln
->tn_val
->v_ansiu
= 0;
614 if (mp
->m_transiu
&& rn
->tn_op
== CON
&& rn
->tn_val
->v_ansiu
) {
615 /* ANSI C treats constant as unsigned, op %s */
616 warning(218, mp
->m_name
);
617 rn
->tn_val
->v_ansiu
= 0;
620 /* Make sure both operands are of the same type */
621 if (mp
->m_balance
|| (tflag
&& (op
== SHL
|| op
== SHR
)))
622 balance(op
, &ln
, &rn
);
625 * Check types for compatibility with the operation and mutual
626 * compatibility. Return if there are serios problems.
628 if (!typeok(op
, 0, ln
, rn
))
631 /* And now create the node. */
635 ntn
= bldstr(op
, ln
, rn
);
641 ntn
= bldincdec(op
, ln
);
644 ntn
= bldamper(ln
, 0);
647 ntn
= mktnode(STAR
, ln
->tn_type
->t_subt
, ln
, NULL
);
651 ntn
= bldplmi(op
, ln
, rn
);
655 ntn
= bldshft(op
, ln
, rn
);
658 ntn
= bldcol(ln
, rn
);
672 ntn
= bldasgn(op
, ln
, rn
);
676 ntn
= mktnode(op
, rn
->tn_type
, ln
, rn
);
683 rtp
= mp
->m_logop
? gettyp(INT
) : ln
->tn_type
;
684 if (!mp
->m_binary
&& rn
!= NULL
)
686 ntn
= mktnode(op
, rtp
, ln
, rn
);
690 /* Return if an error occurred. */
694 /* Print a warning if precedence confusion is possible */
699 * Print a warning if one of the operands is in a context where
700 * it is compared with null and if this operand is a constant.
703 if (ln
->tn_op
== CON
||
704 ((mp
->m_binary
&& op
!= QUEST
) && rn
->tn_op
== CON
)) {
706 /* constant in conditional context */
711 /* Fold if the operator requires it */
713 if (ln
->tn_op
== CON
&& (!mp
->m_binary
|| rn
->tn_op
== CON
)) {
716 } else if (isftyp(ntn
->tn_type
->t_tspec
)) {
721 } else if (op
== QUEST
&& ln
->tn_op
== CON
) {
722 ntn
= ln
->tn_val
->v_quad
? rn
->tn_left
: rn
->tn_right
;
730 * Perform class conversions.
732 * Arrays of type T are converted into pointers to type T.
733 * Functions are converted to pointers to functions.
734 * Lvalues are converted to rvalues.
742 * Array-lvalue (array of type T) is converted into rvalue
743 * (pointer to type T)
745 if (tn
->tn_type
->t_tspec
== ARRAY
) {
746 if (!tn
->tn_lvalue
) {
747 /* %soperand of '%s' must be lvalue */
748 /* XXX print correct operator */
749 (void)gnuism(114, "", modtab
[AMPER
].m_name
);
751 tn
= mktnode(AMPER
, tincref(tn
->tn_type
->t_subt
, PTR
),
756 * Expression of type function (function with return value of type T)
757 * in rvalue-expression (pointer to function with return value
760 if (tn
->tn_type
->t_tspec
== FUNC
)
761 tn
= bldamper(tn
, 1);
763 /* lvalue to rvalue */
765 tp
= tduptyp(tn
->tn_type
);
766 tp
->t_const
= tp
->t_volatile
= 0;
767 tn
= mktnode(LOAD
, tp
, tn
, NULL
);
774 * Perform most type checks. First the types are checked using
775 * informations from modtab[]. After that it is done by hand for
776 * more complicated operators and type combinations.
778 * If the types are ok, typeok() returns 1, otherwise 0.
781 typeok(op_t op
, int arg
, tnode_t
*ln
, tnode_t
*rn
)
784 tspec_t lt
, rt
= NOTSPEC
, lst
= NOTSPEC
, rst
= NOTSPEC
, olt
= NOTSPEC
,
786 type_t
*ltp
, *rtp
= NULL
, *lstp
= NULL
, *rstp
= NULL
;
791 if ((ltp
= ln
->tn_type
) == NULL
)
794 if ((lt
= ltp
->t_tspec
) == PTR
)
795 lst
= (lstp
= ltp
->t_subt
)->t_tspec
;
797 if ((rtp
= rn
->tn_type
) == NULL
)
799 if ((rt
= rtp
->t_tspec
) == PTR
)
800 rst
= (rstp
= rtp
->t_subt
)->t_tspec
;
804 /* integertypes required */
805 if (!isityp(lt
) || (mp
->m_binary
&& !isityp(rt
))) {
806 incompat(op
, lt
, rt
);
809 } else if (mp
->m_rqintcomp
) {
810 /* integertypes required */
811 if ((!isityp(lt
) && !isctyp(lt
)) ||
812 (mp
->m_binary
&& (!isityp(rt
) && !isctyp(rt
)))) {
813 incompat(op
, lt
, rt
);
816 } else if (mp
->m_rqsclt
) {
817 /* scalar types required */
818 if (!issclt(lt
) || (mp
->m_binary
&& !issclt(rt
))) {
819 incompat(op
, lt
, rt
);
822 } else if (mp
->m_rqatyp
) {
823 /* arithmetic types required */
824 if (!isatyp(lt
) || (mp
->m_binary
&& !isatyp(rt
))) {
825 incompat(op
, lt
, rt
);
830 if (op
== SHL
|| op
== SHR
|| op
== SHLASS
|| op
== SHRASS
) {
832 * For these operations we need the types before promotion
835 for (tn
=ln
; tn
->tn_op
==CVT
&& !tn
->tn_cast
; tn
=tn
->tn_left
)
837 olt
= tn
->tn_type
->t_tspec
;
838 for (tn
=rn
; tn
->tn_op
==CVT
&& !tn
->tn_cast
; tn
=tn
->tn_left
)
840 ort
= tn
->tn_type
->t_tspec
;
846 * Most errors required by ANSI C are reported in strmemb().
847 * Here we only must check for totaly wrong things.
849 if (lt
== FUNC
|| lt
== VOID
|| ltp
->t_isfield
||
850 ((lt
!= STRUCT
&& lt
!= UNION
) && !ln
->tn_lvalue
)) {
851 /* Without tflag we got already an error */
853 /* unacceptable operand of %s */
854 error(111, mp
->m_name
);
857 /* Now we have an object we can create a pointer to */
860 if (lt
!= PTR
&& !(tflag
&& isityp(lt
))) {
861 /* Without tflag we got already an error */
863 /* unacceptabel operand of %s */
864 error(111, mp
->m_name
);
872 /* operands have scalar types (checked above) */
873 if (!ln
->tn_lvalue
) {
874 if (ln
->tn_op
== CVT
&& ln
->tn_cast
&&
875 ln
->tn_left
->tn_op
== LOAD
) {
876 /* a cast to non-ptr does not yield an lvalue */
877 if (ln
->tn_type
->t_tspec
== PTR
)
881 /* %soperand of %s must be lvalue */
882 error(114, "", mp
->m_name
);
884 } else if (ltp
->t_const
) {
885 /* %soperand of %s must be modifiable lvalue */
887 warning(115, "", mp
->m_name
);
891 if (lt
== ARRAY
|| lt
== FUNC
) {
892 /* ok, a warning comes later (in bldamper()) */
893 } else if (!ln
->tn_lvalue
) {
894 if (ln
->tn_op
== CVT
&& ln
->tn_cast
&&
895 ln
->tn_left
->tn_op
== LOAD
) {
896 /* a cast to non-ptr does not yield an lvalue */
897 if (ln
->tn_type
->t_tspec
== PTR
)
901 /* %soperand of %s must be lvalue */
902 error(114, "", mp
->m_name
);
904 } else if (issclt(lt
)) {
905 if (ltp
->t_isfield
) {
906 /* cannot take address of bit-field */
910 } else if (lt
!= STRUCT
&& lt
!= UNION
) {
911 /* unacceptable operand of %s */
912 error(111, mp
->m_name
);
915 if (ln
->tn_op
== NAME
&& ln
->tn_sym
->s_reg
) {
916 /* cannot take address of register %s */
917 error(113, ln
->tn_sym
->s_name
);
922 /* until now there were no type checks for this operator */
924 /* cannot dereference non-pointer type */
930 /* operands have scalar types (checked above) */
931 if ((lt
== PTR
&& !isityp(rt
)) || (rt
== PTR
&& !isityp(lt
))) {
932 incompat(op
, lt
, rt
);
937 /* operands have scalar types (checked above) */
938 if (lt
== PTR
&& (!isityp(rt
) && rt
!= PTR
)) {
939 incompat(op
, lt
, rt
);
941 } else if (rt
== PTR
&& lt
!= PTR
) {
942 incompat(op
, lt
, rt
);
945 if (lt
== PTR
&& rt
== PTR
) {
946 if (!eqtype(lstp
, rstp
, 1, 0, NULL
)) {
947 /* illegal pointer subtraction */
953 /* operands have integer types (checked above) */
954 if (pflag
&& !isutyp(lt
)) {
956 * The left operand is signed. This means that
957 * the operation is (possibly) nonportable.
959 /* bitwise operation on signed value nonportable */
960 if (ln
->tn_op
!= CON
) {
961 /* possibly nonportable */
963 } else if (ln
->tn_val
->v_quad
< 0) {
966 } else if (!tflag
&& !sflag
&& !isutyp(olt
) && isutyp(ort
)) {
968 * The left operand would become unsigned in
972 (ln
->tn_op
!= CON
|| ln
->tn_val
->v_quad
< 0)) {
973 /* semantics of %s change in ANSI C; use ... */
974 warning(118, mp
->m_name
);
976 } else if (!tflag
&& !sflag
&& !isutyp(olt
) && !isutyp(ort
) &&
977 psize(lt
) < psize(rt
)) {
979 * In traditional C the left operand would be extended,
980 * possibly with 1, and then shifted.
983 (ln
->tn_op
!= CON
|| ln
->tn_val
->v_quad
< 0)) {
984 /* semantics of %s change in ANSI C; use ... */
985 warning(118, mp
->m_name
);
991 * ANSI C does not perform balancing for shift operations,
992 * but traditional C does. If the width of the right operand
993 * is greather than the width of the left operand, than in
994 * traditional C the left operand would be extendet to the
995 * width of the right operand. For SHL this may result in
998 if (psize(lt
) < psize(rt
)) {
1000 * XXX If both operands are constant make sure
1001 * that there is really a differencs between
1002 * ANSI C and traditional C.
1005 /* semantics of %s change in ANSI C; use ... */
1006 warning(118, mp
->m_name
);
1009 if (rn
->tn_op
== CON
) {
1010 if (!isutyp(rt
) && rn
->tn_val
->v_quad
< 0) {
1011 /* negative shift */
1013 } else if ((uint64_t)rn
->tn_val
->v_quad
== (uint64_t)size(lt
)) {
1014 /* shift equal to size fo object */
1016 } else if ((uint64_t)rn
->tn_val
->v_quad
> (uint64_t)size(lt
)) {
1017 /* shift greater than size of object */
1025 * Accept some things which are allowed with EQ and NE,
1026 * but not with ordered comparisons.
1028 if (lt
== PTR
&& ((rt
== PTR
&& rst
== VOID
) || isityp(rt
))) {
1029 if (rn
->tn_op
== CON
&& rn
->tn_val
->v_quad
== 0)
1032 if (rt
== PTR
&& ((lt
== PTR
&& lst
== VOID
) || isityp(lt
))) {
1033 if (ln
->tn_op
== CON
&& ln
->tn_val
->v_quad
== 0)
1041 if ((lt
== PTR
|| rt
== PTR
) && lt
!= rt
) {
1042 if (isityp(lt
) || isityp(rt
)) {
1043 /* illegal comb. of pointer and int., op %s */
1044 warning(123, mp
->m_name
);
1046 incompat(op
, lt
, rt
);
1049 } else if (lt
== PTR
&& rt
== PTR
) {
1050 ptrcmpok(op
, ln
, rn
);
1055 /* first operand must have scalar type, op ? : */
1059 while (rn
->tn_op
== CVT
)
1061 if (rn
->tn_op
!= COLON
)
1066 if (isatyp(lt
) && isatyp(rt
))
1069 if (lt
== STRUCT
&& rt
== STRUCT
&& ltp
->t_str
== rtp
->t_str
)
1071 if (lt
== UNION
&& rt
== UNION
&& ltp
->t_str
== rtp
->t_str
)
1074 /* combination of any pointer and 0, 0L or (void *)0 is ok */
1075 if (lt
== PTR
&& ((rt
== PTR
&& rst
== VOID
) || isityp(rt
))) {
1076 if (rn
->tn_op
== CON
&& rn
->tn_val
->v_quad
== 0)
1079 if (rt
== PTR
&& ((lt
== PTR
&& lst
== VOID
) || isityp(lt
))) {
1080 if (ln
->tn_op
== CON
&& ln
->tn_val
->v_quad
== 0)
1084 if ((lt
== PTR
&& isityp(rt
)) || (isityp(lt
) && rt
== PTR
)) {
1085 /* illegal comb. of ptr. and int., op %s */
1086 warning(123, mp
->m_name
);
1090 if (lt
== VOID
|| rt
== VOID
) {
1091 if (lt
!= VOID
|| rt
!= VOID
)
1092 /* incompatible types in conditional */
1097 if (lt
== PTR
&& rt
== PTR
&& ((lst
== VOID
&& rst
== FUNC
) ||
1098 (lst
== FUNC
&& rst
== VOID
))) {
1099 /* (void *)0 handled above */
1101 /* ANSI C forbids conv. of %s to %s, op %s */
1102 warning(305, "function pointer", "'void *'",
1107 if (rt
== PTR
&& lt
== PTR
) {
1108 if (eqptrtype(lstp
, rstp
, 1))
1110 if (!eqtype(lstp
, rstp
, 1, 0, NULL
))
1111 illptrc(mp
, ltp
, rtp
);
1115 /* incompatible types in conditional */
1123 if (!asgntypok(op
, arg
, ln
, rn
))
1132 /* operands have scalar types (checked above) */
1133 if ((lt
== PTR
&& !isityp(rt
)) || rt
== PTR
) {
1134 incompat(op
, lt
, rt
);
1141 if (pflag
&& !isutyp(lt
) && !(tflag
&& isutyp(rt
))) {
1142 /* bitwise operation on s.v. possibly nonportabel */
1151 if (!ln
->tn_lvalue
) {
1152 if (ln
->tn_op
== CVT
&& ln
->tn_cast
&&
1153 ln
->tn_left
->tn_op
== LOAD
) {
1154 /* a cast to non-ptr does not yield an lvalue */
1155 if (ln
->tn_type
->t_tspec
== PTR
)
1159 /* %soperand of %s must be lvalue */
1160 error(114, "left ", mp
->m_name
);
1162 } else if (ltp
->t_const
|| ((lt
== STRUCT
|| lt
== UNION
) &&
1164 /* %soperand of %s must be modifiable lvalue */
1166 warning(115, "left ", mp
->m_name
);
1170 if (!modtab
[ln
->tn_op
].m_sideeff
)
1173 /* LINTED (enumeration values not handled in switch) */
1205 (ltp
->t_isenum
|| (mp
->m_binary
&& rtp
->t_isenum
))) {
1206 chkbeop(op
, ln
, rn
);
1207 } else if (mp
->m_enumop
&& (ltp
->t_isenum
&& rtp
&& rtp
->t_isenum
)) {
1208 chkeop2(op
, arg
, ln
, rn
);
1209 } else if (mp
->m_enumop
&& (ltp
->t_isenum
|| (rtp
&& rtp
->t_isenum
))) {
1210 chkeop1(op
, arg
, ln
, rn
);
1217 ptrcmpok(op_t op
, tnode_t
*ln
, tnode_t
*rn
)
1221 const char *lts
, *rts
;
1223 lt
= (ltp
= ln
->tn_type
)->t_subt
->t_tspec
;
1224 rt
= (rtp
= rn
->tn_type
)->t_subt
->t_tspec
;
1226 if (lt
== VOID
|| rt
== VOID
) {
1227 if (sflag
&& (lt
== FUNC
|| rt
== FUNC
)) {
1228 /* (void *)0 already handled in typeok() */
1229 *(lt
== FUNC
? <s
: &rts
) = "function pointer";
1230 *(lt
== VOID
? <s
: &rts
) = "'void *'";
1231 /* ANSI C forbids comparison of %s with %s */
1232 warning(274, lts
, rts
);
1237 if (!eqtype(ltp
->t_subt
, rtp
->t_subt
, 1, 0, NULL
)) {
1238 illptrc(&modtab
[op
], ltp
, rtp
);
1242 if (lt
== FUNC
&& rt
== FUNC
) {
1243 if (sflag
&& op
!= EQ
&& op
!= NE
)
1244 /* ANSI C forbids ordered comp. of func ptr */
1250 * Checks type compatibility for ASSIGN, INIT, FARG and RETURN
1251 * and prints warnings/errors if necessary.
1252 * If the types are (almost) compatible, 1 is returned, otherwise 0.
1255 asgntypok(op_t op
, int arg
, tnode_t
*ln
, tnode_t
*rn
)
1257 tspec_t lt
, rt
, lst
= NOTSPEC
, rst
= NOTSPEC
;
1258 type_t
*ltp
, *rtp
, *lstp
= NULL
, *rstp
= NULL
;
1260 const char *lts
, *rts
;
1261 char lbuf
[128], rbuf
[128];
1263 if ((lt
= (ltp
= ln
->tn_type
)->t_tspec
) == PTR
)
1264 lst
= (lstp
= ltp
->t_subt
)->t_tspec
;
1265 if ((rt
= (rtp
= rn
->tn_type
)->t_tspec
) == PTR
)
1266 rst
= (rstp
= rtp
->t_subt
)->t_tspec
;
1269 if (isatyp(lt
) && isatyp(rt
))
1272 if ((lt
== STRUCT
|| lt
== UNION
) && (rt
== STRUCT
|| rt
== UNION
))
1273 /* both are struct or union */
1274 return (ltp
->t_str
== rtp
->t_str
);
1276 /* 0, 0L and (void *)0 may be assigned to any pointer */
1277 if (lt
== PTR
&& ((rt
== PTR
&& rst
== VOID
) || isityp(rt
))) {
1278 if (rn
->tn_op
== CON
&& rn
->tn_val
->v_quad
== 0)
1282 if (lt
== PTR
&& rt
== PTR
&& (lst
== VOID
|| rst
== VOID
)) {
1283 /* two pointers, at least one pointer to void */
1284 if (sflag
&& (lst
== FUNC
|| rst
== FUNC
)) {
1285 /* comb. of ptr to func and ptr to void */
1286 *(lst
== FUNC
? <s
: &rts
) = "function pointer";
1287 *(lst
== VOID
? <s
: &rts
) = "'void *'";
1291 /* ANSI C forbids conversion of %s to %s */
1292 warning(303, rts
, lts
);
1295 /* ANSI C forbids conv. of %s to %s, arg #%d */
1296 warning(304, rts
, lts
, arg
);
1299 /* ANSI C forbids conv. of %s to %s, op %s */
1300 warning(305, rts
, lts
, mp
->m_name
);
1306 if (lt
== PTR
&& rt
== PTR
&& (lst
== VOID
|| rst
== VOID
||
1307 eqtype(lstp
, rstp
, 1, 0, NULL
))) {
1308 /* compatible pointer types (qualifiers ignored) */
1310 ((!lstp
->t_const
&& rstp
->t_const
) ||
1311 (!lstp
->t_volatile
&& rstp
->t_volatile
))) {
1312 /* left side has not all qualifiers of right */
1313 tyname(lbuf
, sizeof(lbuf
), lstp
);
1314 tyname(rbuf
, sizeof(rbuf
), rstp
);
1318 /* incompatible pointer types */
1319 warning(182, lbuf
, rbuf
);
1322 /* argument has incompat. ptr. type, arg #%d */
1323 warning(153, arg
, lbuf
, rbuf
);
1326 /* operands have incompat. ptr. types, op %s */
1327 warning(128, mp
->m_name
, lbuf
, rbuf
);
1334 if ((lt
== PTR
&& isityp(rt
)) || (isityp(lt
) && rt
== PTR
)) {
1338 /* illegal combination of pointer and integer */
1342 /* illegal comb. of ptr. and int., arg #%d */
1346 /* illegal comb. of ptr. and int., op %s */
1347 warning(123, mp
->m_name
);
1353 if (lt
== PTR
&& rt
== PTR
) {
1357 illptrc(NULL
, ltp
, rtp
);
1360 /* argument has incompatible pointer type, arg #%d */
1361 warning(153, arg
, tyname(lbuf
, sizeof(lbuf
), ltp
),
1362 tyname(rbuf
, sizeof(rbuf
), rtp
));
1365 illptrc(mp
, ltp
, rtp
);
1373 /* initialisation type mismatch */
1377 /* return value type mismatch */
1381 /* argument is incompatible with prototype, arg #%d */
1385 incompat(op
, lt
, rt
);
1393 * Prints a warning if an operator, which should be senseless for an
1394 * enum type, is applied to an enum type.
1397 chkbeop(op_t op
, tnode_t
*ln
, tnode_t
*rn
)
1406 if (!(ln
->tn_type
->t_isenum
||
1407 (mp
->m_binary
&& rn
->tn_type
->t_isenum
))) {
1412 * Enum as offset to a pointer is an exception (otherwise enums
1413 * could not be used as array indizes).
1416 ((ln
->tn_type
->t_isenum
&& rn
->tn_type
->t_tspec
== PTR
) ||
1417 (rn
->tn_type
->t_isenum
&& ln
->tn_type
->t_tspec
== PTR
))) {
1421 /* dubious operation on enum, op %s */
1422 warning(241, mp
->m_name
);
1427 * Prints a warning if an operator is applied to two different enum types.
1430 chkeop2(op_t op
, int arg
, tnode_t
*ln
, tnode_t
*rn
)
1436 if (ln
->tn_type
->t_enum
!= rn
->tn_type
->t_enum
) {
1439 /* enum type mismatch in initialisation */
1443 /* enum type mismatch, arg #%d */
1447 /* return value type mismatch */
1451 /* enum type mismatch, op %s */
1452 warning(130, mp
->m_name
);
1455 } else if (Pflag
&& mp
->m_comp
&& op
!= EQ
&& op
!= NE
) {
1457 /* dubious comparisons of enums */
1458 warning(243, mp
->m_name
);
1463 * Prints a warning if an operator has both enum end other integer
1467 chkeop1(op_t op
, int arg
, tnode_t
*ln
, tnode_t
*rn
)
1469 char lbuf
[64], rbuf
[64];
1477 * Initializations with 0 should be allowed. Otherwise,
1478 * we should complain about all uninitialized enums,
1481 if (!rn
->tn_type
->t_isenum
&& rn
->tn_op
== CON
&&
1482 isityp(rn
->tn_type
->t_tspec
) && rn
->tn_val
->v_quad
== 0) {
1485 /* initialisation of '%s' with '%s' */
1486 warning(277, tyname(lbuf
, sizeof(lbuf
), ln
->tn_type
),
1487 tyname(rbuf
, sizeof(rbuf
), rn
->tn_type
));
1490 /* combination of '%s' and '%s', arg #%d */
1491 warning(278, tyname(lbuf
, sizeof(lbuf
), ln
->tn_type
),
1492 tyname(rbuf
, sizeof(rbuf
), rn
->tn_type
), arg
);
1495 /* combination of '%s' and '%s' in return */
1496 warning(279, tyname(lbuf
, sizeof(lbuf
), ln
->tn_type
),
1497 tyname(rbuf
, sizeof(rbuf
), rn
->tn_type
));
1500 /* combination of '%s' and %s, op %s */
1501 warning(242, tyname(lbuf
, sizeof(lbuf
), ln
->tn_type
),
1502 tyname(rbuf
, sizeof(rbuf
), rn
->tn_type
),
1509 * Build and initialize a new node.
1512 mktnode(op_t op
, type_t
*type
, tnode_t
*ln
, tnode_t
*rn
)
1520 ntn
->tn_type
= type
;
1524 if (op
== STAR
|| op
== FSEL
) {
1525 if (ln
->tn_type
->t_tspec
== PTR
) {
1526 t
= ln
->tn_type
->t_subt
->t_tspec
;
1527 if (t
!= FUNC
&& t
!= VOID
)
1530 LERROR("mktnode()");
1538 * Performs usual conversion of operands to (unsigned) int.
1540 * If tflag is set or the operand is a function argument with no
1541 * type information (no prototype or variable # of args), convert
1545 promote(op_t op
, int farg
, tnode_t
*tn
)
1551 t
= tn
->tn_type
->t_tspec
;
1558 * ANSI C requires that the result is always of type INT
1559 * if INT can represent all possible values of the previous
1562 if (tn
->tn_type
->t_isfield
) {
1563 len
= tn
->tn_type
->t_flen
;
1564 if (size(INT
) > len
) {
1567 if (size(INT
) != len
)
1568 LERROR("promote()");
1575 } else if (t
== CHAR
|| t
== UCHAR
|| t
== SCHAR
) {
1576 t
= (size(CHAR
) < size(INT
) || t
!= UCHAR
) ?
1578 } else if (t
== SHORT
|| t
== USHORT
) {
1579 t
= (size(SHORT
) < size(INT
) || t
== SHORT
) ?
1581 } else if (t
== ENUM
) {
1583 } else if (farg
&& t
== FLOAT
) {
1588 * In traditional C, keep unsigned and promote FLOAT
1591 if (t
== UCHAR
|| t
== USHORT
) {
1593 } else if (t
== CHAR
|| t
== SCHAR
|| t
== SHORT
) {
1595 } else if (t
== FLOAT
) {
1597 } else if (t
== ENUM
) {
1602 if (t
!= tn
->tn_type
->t_tspec
) {
1603 ntp
= tduptyp(tn
->tn_type
);
1606 * Keep t_isenum so we are later able to check compatibility
1609 tn
= convert(op
, 0, ntp
, tn
);
1616 * Insert conversions which are necessary to give both operands the same
1617 * type. This is done in different ways for traditional C and ANIS C.
1620 balance(op_t op
, tnode_t
**lnp
, tnode_t
**rnp
)
1625 static tspec_t tl
[] = {
1626 LDOUBLE
, DOUBLE
, FLOAT
, UQUAD
, QUAD
, ULONG
, LONG
, UINT
, INT
,
1629 lt
= (*lnp
)->tn_type
->t_tspec
;
1630 rt
= (*rnp
)->tn_type
->t_tspec
;
1632 if (!isatyp(lt
) || !isatyp(rt
))
1638 } else if (lt
== LCOMPLEX
|| rt
== LCOMPLEX
) {
1640 } else if (lt
== DCOMPLEX
|| rt
== DCOMPLEX
) {
1642 } else if (lt
== COMPLEX
|| rt
== COMPLEX
) {
1644 } else if (lt
== FCOMPLEX
|| rt
== FCOMPLEX
) {
1646 } else if (lt
== LDOUBLE
|| rt
== LDOUBLE
) {
1648 } else if (lt
== DOUBLE
|| rt
== DOUBLE
) {
1650 } else if (lt
== FLOAT
|| rt
== FLOAT
) {
1654 * If type A has more bits than type B it should
1655 * be able to hold all possible values of type B.
1657 if (size(lt
) > size(rt
)) {
1659 } else if (size(lt
) < size(rt
)) {
1662 for (i
= 3; tl
[i
] != INT
; i
++) {
1663 if (tl
[i
] == lt
|| tl
[i
] == rt
)
1666 if ((isutyp(lt
) || isutyp(rt
)) &&
1674 /* Keep unsigned in traditional C */
1675 u
= isutyp(lt
) || isutyp(rt
);
1676 for (i
= 0; tl
[i
] != INT
; i
++) {
1677 if (lt
== tl
[i
] || rt
== tl
[i
])
1681 if (u
&& isityp(t
) && !isutyp(t
))
1686 ntp
= tduptyp((*lnp
)->tn_type
);
1688 *lnp
= convert(op
, 0, ntp
, *lnp
);
1691 ntp
= tduptyp((*rnp
)->tn_type
);
1693 *rnp
= convert(op
, 0, ntp
, *rnp
);
1698 * Insert a conversion operator, which converts the type of the node
1699 * to another given type.
1700 * If op is FARG, arg is the number of the argument (used for warnings).
1703 convert(op_t op
, int arg
, type_t
*tp
, tnode_t
*tn
)
1706 tspec_t nt
, ot
, ost
= NOTSPEC
;
1709 LERROR("convert()");
1712 if ((ot
= tn
->tn_type
->t_tspec
) == PTR
)
1713 ost
= tn
->tn_type
->t_subt
->t_tspec
;
1715 if (!tflag
&& !sflag
&& op
== FARG
)
1716 ptconv(arg
, nt
, ot
, tp
, tn
);
1717 if (isityp(nt
) && isityp(ot
)) {
1718 iiconv(op
, arg
, nt
, ot
, tp
, tn
);
1719 } else if (nt
== PTR
&& ((ot
== PTR
&& ost
== VOID
) || isityp(ot
)) &&
1720 tn
->tn_op
== CON
&& tn
->tn_val
->v_quad
== 0) {
1721 /* 0, 0L and (void *)0 may be assigned to any pointer. */
1722 } else if (isityp(nt
) && ot
== PTR
) {
1723 piconv(op
, nt
, tp
, tn
);
1724 } else if (nt
== PTR
&& ot
== PTR
) {
1731 ntn
->tn_cast
= op
== CVT
;
1732 if (tn
->tn_op
!= CON
|| nt
== VOID
) {
1736 ntn
->tn_val
= tgetblk(sizeof (val_t
));
1737 cvtcon(op
, arg
, ntn
->tn_type
, ntn
->tn_val
, tn
->tn_val
);
1744 * Print a warning if a prototype causes a type conversion that is
1745 * different from what would happen to the same argument in the
1746 * absence of a prototype.
1748 * Errors/Warnings about illegal type combinations are already printed
1752 ptconv(int arg
, tspec_t nt
, tspec_t ot
, type_t
*tp
, tnode_t
*tn
)
1757 if (!isatyp(nt
) || !isatyp(ot
))
1761 * If the type of the formal parameter is char/short, a warning
1762 * would be useless, because functions declared the old style
1763 * can't expect char/short arguments.
1765 if (nt
== CHAR
|| nt
== UCHAR
|| nt
== SHORT
|| nt
== USHORT
)
1768 /* get default promotion */
1769 ptn
= promote(NOOP
, 1, tn
);
1770 ot
= ptn
->tn_type
->t_tspec
;
1772 /* return if types are the same with and without prototype */
1773 if (nt
== ot
|| (nt
== ENUM
&& ot
== INT
))
1776 if (isftyp(nt
) != isftyp(ot
) || psize(nt
) != psize(ot
)) {
1777 /* representation and/or width change */
1778 if (!isityp(ot
) || psize(ot
) > psize(INT
)) {
1779 /* conversion to '%s' due to prototype, arg #%d */
1780 warning(259, tyname(buf
, sizeof(buf
), tp
), arg
);
1784 * they differ in sign or base type (char, short, int,
1785 * long, long long, float, double, long double)
1787 * if they differ only in sign and the argument is a constant
1788 * and the msb of the argument is not set, print no warning
1790 if (ptn
->tn_op
== CON
&& isityp(nt
) && styp(nt
) == styp(ot
) &&
1791 msb(ptn
->tn_val
->v_quad
, ot
, -1) == 0) {
1794 /* conversion to '%s' due to prototype, arg #%d */
1795 warning(259, tyname(buf
, sizeof(buf
), tp
), arg
);
1801 * Print warnings for conversions of integer types which my cause
1806 iiconv(op_t op
, int arg
, tspec_t nt
, tspec_t ot
, type_t
*tp
, tnode_t
*tn
)
1808 char lbuf
[64], rbuf
[64], opbuf
[16];
1809 if (tn
->tn_op
== CON
)
1815 if (Pflag
&& psize(nt
) > psize(ot
) && isutyp(nt
) != isutyp(ot
)) {
1816 /* conversion to %s may sign-extend incorrectly (, arg #%d) */
1817 if (aflag
&& pflag
) {
1819 warning(297, tyname(lbuf
, sizeof(lbuf
), tp
),
1822 warning(131, tyname(lbuf
, sizeof(lbuf
), tp
));
1827 if (Pflag
&& psize(nt
) > psize(ot
)) {
1828 switch (tn
->tn_op
) {
1834 tyname(rbuf
, sizeof(rbuf
), gettyp(ot
)),
1835 tyname(lbuf
, sizeof(lbuf
), tp
),
1836 prtnode(opbuf
, sizeof(opbuf
), tn
));
1843 if (psize(nt
) < psize(ot
) &&
1844 (ot
== LONG
|| ot
== ULONG
|| ot
== QUAD
|| ot
== UQUAD
||
1846 /* conversion from '%s' may lose accuracy */
1850 tyname(rbuf
, sizeof(rbuf
), tn
->tn_type
),
1851 tyname(lbuf
, sizeof(lbuf
), tp
),
1855 tyname(rbuf
, sizeof(rbuf
), tn
->tn_type
),
1856 tyname(lbuf
, sizeof(lbuf
), tp
));
1863 * Print warnings for dubious conversions of pointer to integer.
1866 piconv(op_t op
, tspec_t nt
, type_t
*tp
, tnode_t
*tn
)
1870 if (tn
->tn_op
== CON
)
1874 /* We got already an error. */
1878 if (psize(nt
) < psize(PTR
)) {
1879 if (pflag
&& size(nt
) >= size(PTR
)) {
1880 /* conv. of pointer to %s may lose bits */
1881 warning(134, tyname(buf
, sizeof(buf
), tp
));
1883 /* conv. of pointer to %s loses bits */
1884 warning(133, tyname(buf
, sizeof(buf
), tp
));
1890 * Print warnings for questionable pointer conversions.
1893 ppconv(op_t op
, tnode_t
*tn
, type_t
*tp
)
1896 const char *nts
, *ots
;
1899 * We got already an error (pointers of different types
1900 * without a cast) or we will not get a warning.
1905 nt
= tp
->t_subt
->t_tspec
;
1906 ot
= tn
->tn_type
->t_subt
->t_tspec
;
1908 if (nt
== VOID
|| ot
== VOID
) {
1909 if (sflag
&& (nt
== FUNC
|| ot
== FUNC
)) {
1910 /* (void *)0 already handled in convert() */
1911 *(nt
== FUNC
? &nts
: &ots
) = "function pointer";
1912 *(nt
== VOID
? &nts
: &ots
) = "'void *'";
1913 /* ANSI C forbids conversion of %s to %s */
1914 warning(303, ots
, nts
);
1917 } else if (nt
== FUNC
&& ot
== FUNC
) {
1919 } else if (nt
== FUNC
|| ot
== FUNC
) {
1920 /* questionable conversion of function pointer */
1925 if (getbound(tp
->t_subt
) > getbound(tn
->tn_type
->t_subt
)) {
1927 /* possible pointer alignment problem */
1930 if (((nt
== STRUCT
|| nt
== UNION
) &&
1931 tp
->t_subt
->t_str
!= tn
->tn_type
->t_subt
->t_str
) ||
1932 psize(nt
) != psize(ot
)) {
1934 /* pointer casts may be troublesome */
1941 * Converts a typed constant in a constant of another type.
1943 * op operator which requires conversion
1944 * arg if op is FARG, # of argument
1945 * tp type in which to convert the constant
1950 cvtcon(op_t op
, int arg
, type_t
*tp
, val_t
*nv
, val_t
*v
)
1952 char lbuf
[64], rbuf
[64];
1954 ldbl_t max
= 0.0, min
= 0.0;
1956 int64_t xmask
, xmsk1
;
1960 nt
= nv
->v_tspec
= tp
->t_tspec
;
1963 if (ot
== FLOAT
|| ot
== DOUBLE
|| ot
== LDOUBLE
) {
1966 max
= 1; min
= 0; break;
1968 max
= CHAR_MAX
; min
= CHAR_MIN
; break;
1970 max
= UCHAR_MAX
; min
= 0; break;
1972 max
= SCHAR_MAX
; min
= SCHAR_MIN
; break;
1974 max
= SHRT_MAX
; min
= SHRT_MIN
; break;
1976 max
= USHRT_MAX
; min
= 0; break;
1979 max
= INT_MAX
; min
= INT_MIN
; break;
1981 max
= (u_int
)UINT_MAX
; min
= 0; break;
1983 max
= LONG_MAX
; min
= LONG_MIN
; break;
1985 max
= (u_long
)ULONG_MAX
; min
= 0; break;
1987 max
= QUAD_MAX
; min
= QUAD_MIN
; break;
1989 max
= (uint64_t)UQUAD_MAX
; min
= 0; break;
1992 max
= FLT_MAX
; min
= -FLT_MAX
; break;
1995 max
= DBL_MAX
; min
= -DBL_MAX
; break;
1997 /* Got already an error because of float --> ptr */
2000 max
= LDBL_MAX
; min
= -LDBL_MAX
; break;
2004 if (v
->v_ldbl
> max
|| v
->v_ldbl
< min
) {
2008 /* conv. of %s to %s is out of rng., arg #%d */
2009 warning(295, tyname(lbuf
, sizeof(lbuf
),
2010 gettyp(ot
)), tyname(rbuf
, sizeof(rbuf
), tp
),
2013 /* conversion of %s to %s is out of range */
2014 warning(119, tyname(lbuf
, sizeof(lbuf
),
2016 tyname(rbuf
, sizeof(rbuf
), tp
));
2018 v
->v_ldbl
= v
->v_ldbl
> 0 ? max
: min
;
2021 nv
->v_ldbl
= (float)v
->v_ldbl
;
2022 } else if (nt
== DOUBLE
) {
2023 nv
->v_ldbl
= (double)v
->v_ldbl
;
2024 } else if (nt
== LDOUBLE
) {
2025 nv
->v_ldbl
= v
->v_ldbl
;
2027 nv
->v_quad
= (nt
== PTR
|| isutyp(nt
)) ?
2028 (int64_t)v
->v_ldbl
: (int64_t)v
->v_ldbl
;
2032 nv
->v_ldbl
= (ot
== PTR
|| isutyp(ot
)) ?
2033 (float)(uint64_t)v
->v_quad
: (float)v
->v_quad
;
2034 } else if (nt
== DOUBLE
) {
2035 nv
->v_ldbl
= (ot
== PTR
|| isutyp(ot
)) ?
2036 (double)(uint64_t)v
->v_quad
: (double)v
->v_quad
;
2037 } else if (nt
== LDOUBLE
) {
2038 nv
->v_ldbl
= (ot
== PTR
|| isutyp(ot
)) ?
2039 (ldbl_t
)(uint64_t)v
->v_quad
: (ldbl_t
)v
->v_quad
;
2041 rchk
= 1; /* Check for lost precision. */
2042 nv
->v_quad
= v
->v_quad
;
2046 if (v
->v_ansiu
&& isftyp(nt
)) {
2047 /* ANSI C treats constant as unsigned */
2050 } else if (v
->v_ansiu
&& (isityp(nt
) && !isutyp(nt
) &&
2051 psize(nt
) > psize(ot
))) {
2052 /* ANSI C treats constant as unsigned */
2057 if (nt
!= FLOAT
&& nt
!= DOUBLE
&& nt
!= LDOUBLE
) {
2058 sz
= tp
->t_isfield
? tp
->t_flen
: size(nt
);
2059 nv
->v_quad
= xsign(nv
->v_quad
, nt
, sz
);
2062 if (rchk
&& op
!= CVT
) {
2064 nsz
= tp
->t_isfield
? tp
->t_flen
: size(nt
);
2065 xmask
= qlmasks
[nsz
] ^ qlmasks
[osz
];
2066 xmsk1
= qlmasks
[nsz
] ^ qlmasks
[osz
- 1];
2068 * For bitwise operations we are not interested in the
2069 * value, but in the bits itself.
2071 if (op
== ORASS
|| op
== OR
|| op
== XOR
) {
2073 * Print a warning if bits which were set are
2074 * lost due to the conversion.
2075 * This can happen with operator ORASS only.
2077 if (nsz
< osz
&& (v
->v_quad
& xmask
) != 0) {
2078 /* constant truncated by conv., op %s */
2079 warning(306, modtab
[op
].m_name
);
2081 } else if (op
== ANDASS
|| op
== AND
) {
2083 * Print a warning if additional bits are not all 1
2084 * and the most significant bit of the old value is 1,
2085 * or if at least one (but not all) removed bit was 0.
2088 (nv
->v_quad
& qbmasks
[osz
- 1]) != 0 &&
2089 (nv
->v_quad
& xmask
) != xmask
) {
2091 * extra bits set to 0 in conversion
2092 * of '%s' to '%s', op %s
2094 warning(309, tyname(lbuf
, sizeof(lbuf
),
2095 gettyp(ot
)), tyname(rbuf
, sizeof(rbuf
), tp
),
2097 } else if (nsz
< osz
&&
2098 (v
->v_quad
& xmask
) != xmask
&&
2099 (v
->v_quad
& xmask
) != 0) {
2100 /* const. truncated by conv., op %s */
2101 warning(306, modtab
[op
].m_name
);
2103 } else if ((nt
!= PTR
&& isutyp(nt
)) &&
2104 (ot
!= PTR
&& !isutyp(ot
)) && v
->v_quad
< 0) {
2106 /* assignment of negative constant to ... */
2108 } else if (op
== INIT
) {
2109 /* initialisation of unsigned with neg. ... */
2111 } else if (op
== FARG
) {
2112 /* conversion of neg. const. to ..., arg #%d */
2114 } else if (modtab
[op
].m_comp
) {
2115 /* we get this warning already in chkcomp() */
2117 /* conversion of negative constant to ... */
2120 } else if (nv
->v_quad
!= v
->v_quad
&& nsz
<= osz
&&
2121 (v
->v_quad
& xmask
) != 0 &&
2122 (isutyp(ot
) || (v
->v_quad
& xmsk1
) != xmsk1
)) {
2124 * Loss of significant bit(s). All truncated bits
2125 * of unsigned types or all truncated bits plus the
2126 * msb of the target for signed types are considered
2127 * to be significant bits. Loss of significant bits
2128 * means that at least on of the bits was set in an
2129 * unsigned type or that at least one, but not all of
2130 * the bits was set in an signed type.
2131 * Loss of significant bits means that it is not
2132 * possible, also not with necessary casts, to convert
2133 * back to the original type. A example for a
2134 * necessary cast is:
2135 * char c; int i; c = 128;
2136 * i = c; ** yields -128 **
2137 * i = (unsigned char)c; ** yields 128 **
2139 if (op
== ASSIGN
&& tp
->t_isfield
) {
2140 /* precision lost in bit-field assignment */
2142 } else if (op
== ASSIGN
) {
2143 /* constant truncated by assignment */
2145 } else if (op
== INIT
&& tp
->t_isfield
) {
2146 /* bit-field initializer does not fit */
2148 } else if (op
== INIT
) {
2149 /* initializer does not fit */
2151 } else if (op
== CASE
) {
2152 /* case label affected by conversion */
2154 } else if (op
== FARG
) {
2155 /* conv. of %s to %s is out of rng., arg #%d */
2156 warning(295, tyname(lbuf
, sizeof(lbuf
),
2157 gettyp(ot
)), tyname(rbuf
, sizeof(rbuf
), tp
),
2160 /* conversion of %s to %s is out of range */
2161 warning(119, tyname(lbuf
, sizeof(lbuf
),
2163 tyname(rbuf
, sizeof(rbuf
), tp
));
2165 } else if (nv
->v_quad
!= v
->v_quad
) {
2166 if (op
== ASSIGN
&& tp
->t_isfield
) {
2167 /* precision lost in bit-field assignment */
2169 } else if (op
== INIT
&& tp
->t_isfield
) {
2170 /* bit-field initializer out of range */
2172 } else if (op
== CASE
) {
2173 /* case label affected by conversion */
2175 } else if (op
== FARG
) {
2176 /* conv. of %s to %s is out of rng., arg #%d */
2177 warning(295, tyname(lbuf
, sizeof(lbuf
),
2178 gettyp(ot
)), tyname(rbuf
, sizeof(rbuf
), tp
),
2181 /* conversion of %s to %s is out of range */
2182 warning(119, tyname(lbuf
, sizeof(lbuf
),
2184 tyname(rbuf
, sizeof(rbuf
), tp
));
2191 * Called if incompatible types were detected.
2192 * Prints a appropriate warning.
2195 incompat(op_t op
, tspec_t lt
, tspec_t rt
)
2202 if (lt
== VOID
|| (mp
->m_binary
&& rt
== VOID
)) {
2203 /* void type illegal in expression */
2205 } else if (op
== ASSIGN
) {
2206 if ((lt
== STRUCT
|| lt
== UNION
) &&
2207 (rt
== STRUCT
|| rt
== UNION
)) {
2208 /* assignment of different structures */
2211 /* assignment type mismatch */
2214 } else if (mp
->m_binary
) {
2215 /* operands of %s have incompatible types */
2218 /* operand of %s has incompatible type */
2229 error(e
, mp
->m_name
, basictyname(lt
), basictyname(rt
));
2232 error(e
, basictyname(lt
), basictyname(rt
));
2238 * Called if incompatible pointer types are detected.
2239 * Print an appropriate warning.
2242 illptrc(mod_t
*mp
, type_t
*ltp
, type_t
*rtp
)
2246 if (ltp
->t_tspec
!= PTR
|| rtp
->t_tspec
!= PTR
)
2247 LERROR("illptrc()");
2249 lt
= ltp
->t_subt
->t_tspec
;
2250 rt
= rtp
->t_subt
->t_tspec
;
2252 if ((lt
== STRUCT
|| lt
== UNION
) && (rt
== STRUCT
|| rt
== UNION
)) {
2254 /* illegal structure pointer combination */
2257 /* illegal structure pointer combination, op %s */
2258 warning(245, mp
->m_name
);
2262 /* illegal pointer combination */
2265 /* illegal pointer combination, op %s */
2266 warning(124, mp
->m_name
);
2272 * Make sure type (*tpp)->t_subt has at least the qualifiers
2273 * of tp1->t_subt and tp2->t_subt.
2276 mrgqual(type_t
**tpp
, type_t
*tp1
, type_t
*tp2
)
2279 if ((*tpp
)->t_tspec
!= PTR
||
2280 tp1
->t_tspec
!= PTR
|| tp2
->t_tspec
!= PTR
) {
2281 LERROR("mrgqual()");
2284 if ((*tpp
)->t_subt
->t_const
==
2285 (tp1
->t_subt
->t_const
| tp2
->t_subt
->t_const
) &&
2286 (*tpp
)->t_subt
->t_volatile
==
2287 (tp1
->t_subt
->t_volatile
| tp2
->t_subt
->t_volatile
)) {
2291 *tpp
= tduptyp(*tpp
);
2292 (*tpp
)->t_subt
= tduptyp((*tpp
)->t_subt
);
2293 (*tpp
)->t_subt
->t_const
=
2294 tp1
->t_subt
->t_const
| tp2
->t_subt
->t_const
;
2295 (*tpp
)->t_subt
->t_volatile
=
2296 tp1
->t_subt
->t_volatile
| tp2
->t_subt
->t_volatile
;
2300 * Returns 1 if the given structure or union has a constant member
2301 * (maybe recursively).
2309 if ((t
= tp
->t_tspec
) != STRUCT
&& t
!= UNION
)
2310 LERROR("conmemb()");
2311 for (m
= tp
->t_str
->memb
; m
!= NULL
; m
= m
->s_nxt
) {
2315 if ((t
= tp
->t_tspec
) == STRUCT
|| t
== UNION
) {
2316 if (conmemb(m
->s_type
))
2324 * Create a new node for one of the operators POINT and ARROW.
2327 bldstr(op_t op
, tnode_t
*ln
, tnode_t
*rn
)
2332 if (rn
->tn_op
!= NAME
)
2334 if (rn
->tn_sym
->s_value
.v_tspec
!= INT
)
2336 if (rn
->tn_sym
->s_scl
!= MOS
&& rn
->tn_sym
->s_scl
!= MOU
)
2340 * Remember if the left operand is an lvalue (structure members
2341 * are lvalues if and only if the structure itself is an lvalue).
2343 nolval
= op
== POINT
&& !ln
->tn_lvalue
;
2346 ln
= bldamper(ln
, 1);
2347 } else if (ln
->tn_type
->t_tspec
!= PTR
) {
2348 if (!tflag
|| !isityp(ln
->tn_type
->t_tspec
))
2350 ln
= convert(NOOP
, 0, tincref(gettyp(VOID
), PTR
), ln
);
2354 ctn
= getinode(LONG
, rn
->tn_sym
->s_value
.v_quad
/ CHAR_BIT
);
2356 ctn
= getinode(INT
, rn
->tn_sym
->s_value
.v_quad
/ CHAR_BIT
);
2359 ntn
= mktnode(PLUS
, tincref(rn
->tn_type
, PTR
), ln
, ctn
);
2360 if (ln
->tn_op
== CON
)
2363 if (rn
->tn_type
->t_isfield
) {
2364 ntn
= mktnode(FSEL
, ntn
->tn_type
->t_subt
, ntn
, NULL
);
2366 ntn
= mktnode(STAR
, ntn
->tn_type
->t_subt
, ntn
, NULL
);
2376 * Create a node for INCAFT, INCBEF, DECAFT and DECBEF.
2379 bldincdec(op_t op
, tnode_t
*ln
)
2384 LERROR("bldincdec()");
2386 if (ln
->tn_type
->t_tspec
== PTR
) {
2387 cn
= plength(ln
->tn_type
);
2389 cn
= getinode(INT
, (int64_t)1);
2391 ntn
= mktnode(op
, ln
->tn_type
, ln
, cn
);
2397 * Create a node for REAL, IMAG
2400 bldri(op_t op
, tnode_t
*ln
)
2406 LERROR("bldincdec()");
2408 switch (ln
->tn_type
->t_tspec
) {
2410 cn
= getinode(LDOUBLE
, (int64_t)1);
2413 cn
= getinode(DOUBLE
, (int64_t)1);
2416 cn
= getinode(FLOAT
, (int64_t)1);
2419 error(276, op
== REAL
? "real" : "imag",
2420 tyname(buf
, sizeof(buf
), ln
->tn_type
));
2423 ntn
= mktnode(op
, cn
->tn_type
, ln
, cn
);
2428 * Create a tree node for the & operator
2431 bldamper(tnode_t
*tn
, int noign
)
2436 if (!noign
&& ((t
= tn
->tn_type
->t_tspec
) == ARRAY
|| t
== FUNC
)) {
2437 /* & before array or function: ignored */
2444 if (tn
->tn_op
== STAR
&&
2445 tn
->tn_left
->tn_type
->t_tspec
== PTR
&&
2446 tn
->tn_left
->tn_type
->t_subt
== tn
->tn_type
) {
2447 return (tn
->tn_left
);
2450 ntn
= mktnode(AMPER
, tincref(tn
->tn_type
, PTR
), tn
, NULL
);
2456 * Create a node for operators PLUS and MINUS.
2459 bldplmi(op_t op
, tnode_t
*ln
, tnode_t
*rn
)
2464 /* If pointer and integer, then pointer to the lhs. */
2465 if (rn
->tn_type
->t_tspec
== PTR
&& isityp(ln
->tn_type
->t_tspec
)) {
2471 if (ln
->tn_type
->t_tspec
== PTR
&& rn
->tn_type
->t_tspec
!= PTR
) {
2473 if (!isityp(rn
->tn_type
->t_tspec
))
2474 LERROR("bldplmi()");
2476 ctn
= plength(ln
->tn_type
);
2477 if (rn
->tn_type
->t_tspec
!= ctn
->tn_type
->t_tspec
)
2478 rn
= convert(NOOP
, 0, ctn
->tn_type
, rn
);
2479 rn
= mktnode(MULT
, rn
->tn_type
, rn
, ctn
);
2480 if (rn
->tn_left
->tn_op
== CON
)
2482 ntn
= mktnode(op
, ln
->tn_type
, ln
, rn
);
2484 } else if (rn
->tn_type
->t_tspec
== PTR
) {
2486 if (ln
->tn_type
->t_tspec
!= PTR
|| op
!= MINUS
)
2487 LERROR("bldplmi()");
2493 ntn
= mktnode(op
, tp
, ln
, rn
);
2494 if (ln
->tn_op
== CON
&& rn
->tn_op
== CON
)
2496 ctn
= plength(ln
->tn_type
);
2497 balance(NOOP
, &ntn
, &ctn
);
2498 ntn
= mktnode(DIV
, tp
, ntn
, ctn
);
2502 ntn
= mktnode(op
, ln
->tn_type
, ln
, rn
);
2509 * Create a node for operators SHL and SHR.
2512 bldshft(op_t op
, tnode_t
*ln
, tnode_t
*rn
)
2517 if ((t
= rn
->tn_type
->t_tspec
) != INT
&& t
!= UINT
)
2518 rn
= convert(CVT
, 0, gettyp(INT
), rn
);
2519 ntn
= mktnode(op
, ln
->tn_type
, ln
, rn
);
2524 * Create a node for COLON.
2527 bldcol(tnode_t
*ln
, tnode_t
*rn
)
2529 tspec_t lt
, rt
, pdt
;
2533 lt
= ln
->tn_type
->t_tspec
;
2534 rt
= rn
->tn_type
->t_tspec
;
2542 * Arithmetic types are balanced, all other type combinations
2543 * still need to be handled.
2545 if (isatyp(lt
) && isatyp(rt
)) {
2547 } else if (lt
== VOID
|| rt
== VOID
) {
2549 } else if (lt
== STRUCT
|| lt
== UNION
) {
2550 /* Both types must be identical. */
2551 if (rt
!= STRUCT
&& rt
!= UNION
)
2553 if (ln
->tn_type
->t_str
!= rn
->tn_type
->t_str
)
2555 if (incompl(ln
->tn_type
)) {
2556 /* unknown operand size, op %s */
2557 error(138, modtab
[COLON
].m_name
);
2561 } else if (lt
== PTR
&& isityp(rt
)) {
2563 rn
= convert(NOOP
, 0, gettyp(pdt
), rn
);
2567 } else if (rt
== PTR
&& isityp(lt
)) {
2569 ln
= convert(NOOP
, 0, gettyp(pdt
), ln
);
2573 } else if (lt
== PTR
&& ln
->tn_type
->t_subt
->t_tspec
== VOID
) {
2577 mrgqual(&rtp
, ln
->tn_type
, rn
->tn_type
);
2578 } else if (rt
== PTR
&& rn
->tn_type
->t_subt
->t_tspec
== VOID
) {
2582 mrgqual(&rtp
, ln
->tn_type
, rn
->tn_type
);
2584 if (lt
!= PTR
|| rt
!= PTR
)
2587 * XXX For now we simply take the left type. This is
2588 * probably wrong, if one type contains a functionprototype
2589 * and the other one, at the same place, only an old style
2593 mrgqual(&rtp
, ln
->tn_type
, rn
->tn_type
);
2596 ntn
= mktnode(COLON
, rtp
, ln
, rn
);
2602 * Create a node for an assignment operator (both = and op= ).
2605 bldasgn(op_t op
, tnode_t
*ln
, tnode_t
*rn
)
2610 if (ln
== NULL
|| rn
== NULL
)
2611 LERROR("bldasgn()");
2613 lt
= ln
->tn_type
->t_tspec
;
2614 rt
= rn
->tn_type
->t_tspec
;
2616 if ((op
== ADDASS
|| op
== SUBASS
) && lt
== PTR
) {
2618 LERROR("bldasgn()");
2619 ctn
= plength(ln
->tn_type
);
2620 if (rn
->tn_type
->t_tspec
!= ctn
->tn_type
->t_tspec
)
2621 rn
= convert(NOOP
, 0, ctn
->tn_type
, rn
);
2622 rn
= mktnode(MULT
, rn
->tn_type
, rn
, ctn
);
2623 if (rn
->tn_left
->tn_op
== CON
)
2627 if ((op
== ASSIGN
|| op
== RETURN
) && (lt
== STRUCT
|| rt
== STRUCT
)) {
2628 if (rt
!= lt
|| ln
->tn_type
->t_str
!= rn
->tn_type
->t_str
)
2629 LERROR("bldasgn()");
2630 if (incompl(ln
->tn_type
)) {
2632 /* cannot return incomplete type */
2635 /* unknown operand size, op %s */
2636 error(138, modtab
[op
].m_name
);
2643 if (psize(lt
) < psize(rt
)) {
2645 /* semantics of %s change in ANSI C; use ... */
2646 warning(118, "<<=");
2648 } else if (op
!= SHRASS
) {
2649 if (op
== ASSIGN
|| lt
!= PTR
) {
2651 (ln
->tn_type
->t_isfield
&& rn
->tn_op
== CON
)) {
2652 rn
= convert(op
, 0, ln
->tn_type
, rn
);
2658 ntn
= mktnode(op
, ln
->tn_type
, ln
, rn
);
2664 * Get length of type tp->t_subt.
2672 if (tp
->t_tspec
!= PTR
)
2673 LERROR("plength()");
2679 while (tp
->t_tspec
== ARRAY
) {
2684 switch (tp
->t_tspec
) {
2686 /* pointer to function is not allowed here */
2690 /* cannot do pointer arithmetic on operand of ... */
2695 if ((elsz
= tp
->t_str
->size
) == 0)
2696 /* cannot do pointer arithmetic on operand of ... */
2701 /* cannot do pointer arithmetic on operand of ... */
2706 if ((elsz
= size(tp
->t_tspec
)) == 0) {
2707 /* cannot do pointer arithmetic on operand of ... */
2709 } else if (elsz
== -1) {
2710 LERROR("plength()");
2715 if (elem
== 0 && elsz
!= 0) {
2716 /* cannot do pointer arithmetic on operand of ... */
2729 return (getinode(st
, (int64_t)(elem
* elsz
/ CHAR_BIT
)));
2734 * Note: There appear to be a number of bugs in detecting overflow in
2735 * this function. An audit and a set of proper regression tests are needed.
2736 * --Perry Metzger, Nov. 16, 2001
2739 * Do only as much as necessary to compute constant expressions.
2740 * Called only if the operator allows folding and (both) operands
2749 int64_t sl
, sr
= 0, q
= 0, mask
;
2750 uint64_t ul
, ur
= 0;
2753 v
= xcalloc(1, sizeof (val_t
));
2754 v
->v_tspec
= t
= tn
->tn_type
->t_tspec
;
2756 utyp
= t
== PTR
|| isutyp(t
);
2757 ul
= sl
= tn
->tn_left
->tn_val
->v_quad
;
2758 if (modtab
[tn
->tn_op
].m_binary
)
2759 ur
= sr
= tn
->tn_right
->tn_val
->v_quad
;
2761 mask
= qlmasks
[size(t
)];
2764 switch (tn
->tn_op
) {
2770 if (msb(q
, t
, -1) == msb(sl
, t
, -1))
2779 if (q
!= (q
& mask
))
2781 else if ((ul
!= 0) && ((q
/ ul
) != ur
))
2785 if (msb(q
, t
, -1) != (msb(sl
, t
, -1) ^ msb(sr
, t
, -1)))
2793 q
= utyp
? UQUAD_MAX
: QUAD_MAX
;
2795 q
= utyp
? (int64_t)(ul
/ ur
) : sl
/ sr
;
2804 q
= utyp
? (int64_t)(ul
% ur
) : sl
% sr
;
2808 q
= utyp
? (int64_t)(ul
+ ur
) : sl
+ sr
;
2809 if (msb(sl
, t
, -1) != 0 && msb(sr
, t
, -1) != 0) {
2810 if (msb(q
, t
, -1) == 0)
2812 } else if (msb(sl
, t
, -1) == 0 && msb(sr
, t
, -1) == 0) {
2813 if (msb(q
, t
, -1) != 0)
2818 q
= utyp
? (int64_t)(ul
- ur
) : sl
- sr
;
2819 if (msb(sl
, t
, -1) != 0 && msb(sr
, t
, -1) == 0) {
2820 if (msb(q
, t
, -1) == 0)
2822 } else if (msb(sl
, t
, -1) == 0 && msb(sr
, t
, -1) != 0) {
2823 if (msb(q
, t
, -1) != 0)
2828 q
= utyp
? (int64_t)(ul
<< sr
) : sl
<< sr
;
2832 * The sign must be explicitly extended because
2833 * shifts of signed values are implementation dependent.
2836 q
= xsign(q
, t
, size(t
) - (int)sr
);
2839 q
= utyp
? ul
< ur
: sl
< sr
;
2842 q
= utyp
? ul
<= ur
: sl
<= sr
;
2845 q
= utyp
? ul
>= ur
: sl
>= sr
;
2848 q
= utyp
? ul
> ur
: sl
> sr
;
2851 q
= utyp
? ul
== ur
: sl
== sr
;
2854 q
= utyp
? ul
!= ur
: sl
!= sr
;
2857 q
= utyp
? (int64_t)(ul
& ur
) : sl
& sr
;
2860 q
= utyp
? (int64_t)(ul
^ ur
) : sl
^ sr
;
2863 q
= utyp
? (int64_t)(ul
| ur
) : sl
| sr
;
2869 /* XXX does not work for quads. */
2870 if (ovfl
|| ((uint64_t)(q
| mask
) != ~(uint64_t)0 &&
2871 (q
& ~mask
) != 0)) {
2873 /* integer overflow detected, op %s */
2874 warning(141, modtab
[tn
->tn_op
].m_name
);
2877 v
->v_quad
= xsign(q
, t
, -1);
2879 cn
= getcnode(tn
->tn_type
, v
);
2885 * Same for operators whose operands are compared with 0 (test context).
2888 foldtst(tnode_t
*tn
)
2893 v
= xcalloc(1, sizeof (val_t
));
2894 v
->v_tspec
= tn
->tn_type
->t_tspec
;
2895 if (tn
->tn_type
->t_tspec
!= INT
)
2896 LERROR("foldtst()");
2898 if (isftyp(tn
->tn_left
->tn_type
->t_tspec
)) {
2899 l
= tn
->tn_left
->tn_val
->v_ldbl
!= 0.0;
2901 l
= tn
->tn_left
->tn_val
->v_quad
!= 0;
2904 if (modtab
[tn
->tn_op
].m_binary
) {
2905 if (isftyp(tn
->tn_right
->tn_type
->t_tspec
)) {
2906 r
= tn
->tn_right
->tn_val
->v_ldbl
!= 0.0;
2908 r
= tn
->tn_right
->tn_val
->v_quad
!= 0;
2912 switch (tn
->tn_op
) {
2915 /* constant argument to NOT */
2926 LERROR("foldtst()");
2929 return (getcnode(tn
->tn_type
, v
));
2933 * Same for operands with floating point type.
2936 foldflt(tnode_t
*tn
)
2943 v
= xcalloc(1, sizeof (val_t
));
2944 v
->v_tspec
= t
= tn
->tn_type
->t_tspec
;
2947 LERROR("foldflt()");
2949 if (t
!= tn
->tn_left
->tn_type
->t_tspec
)
2950 LERROR("foldflt()");
2951 if (modtab
[tn
->tn_op
].m_binary
&& t
!= tn
->tn_right
->tn_type
->t_tspec
)
2952 LERROR("foldflt()");
2954 l
= tn
->tn_left
->tn_val
->v_ldbl
;
2955 if (modtab
[tn
->tn_op
].m_binary
)
2956 r
= tn
->tn_right
->tn_val
->v_ldbl
;
2958 switch (tn
->tn_op
) {
2973 v
->v_ldbl
= l
< 0 ? -FLT_MAX
: FLT_MAX
;
2974 } else if (t
== DOUBLE
) {
2975 v
->v_ldbl
= l
< 0 ? -DBL_MAX
: DBL_MAX
;
2977 v
->v_ldbl
= l
< 0 ? -LDBL_MAX
: LDBL_MAX
;
3008 LERROR("foldflt()");
3011 if (!fpe
&& isnan((double)v
->v_ldbl
))
3012 LERROR("foldflt()");
3013 if (fpe
|| !finite((double)v
->v_ldbl
) ||
3015 (v
->v_ldbl
> FLT_MAX
|| v
->v_ldbl
< -FLT_MAX
)) ||
3017 (v
->v_ldbl
> DBL_MAX
|| v
->v_ldbl
< -DBL_MAX
))) {
3018 /* floating point overflow detected, op %s */
3019 warning(142, modtab
[tn
->tn_op
].m_name
);
3021 v
->v_ldbl
= v
->v_ldbl
< 0 ? -FLT_MAX
: FLT_MAX
;
3022 } else if (t
== DOUBLE
) {
3023 v
->v_ldbl
= v
->v_ldbl
< 0 ? -DBL_MAX
: DBL_MAX
;
3025 v
->v_ldbl
= v
->v_ldbl
< 0 ? -LDBL_MAX
: LDBL_MAX
;
3030 return (getcnode(tn
->tn_type
, v
));
3035 * Create a constant node for sizeof.
3046 return getinode(st
, tsize(tp
) / CHAR_BIT
);
3055 while (tp
->t_tspec
== ARRAY
) {
3060 /* cannot take size of incomplete type */
3064 switch (tp
->t_tspec
) {
3066 /* cannot take size of function */
3073 /* cannot take size of incomplete type */
3077 elsz
= tp
->t_str
->size
;
3082 /* cannot take size of incomplete type */
3087 if (tp
->t_isfield
) {
3088 /* cannot take size of bit-field */
3091 if (tp
->t_tspec
== VOID
) {
3092 /* cannot take size of void */
3096 elsz
= size(tp
->t_tspec
);
3098 LERROR("bldszof()");
3103 return (int64_t)(elem
* elsz
);
3113 switch (tp
->t_tspec
) {
3118 /* cannot take align of function */
3125 /* cannot take align of incomplete type */
3133 if (tp
->t_isfield
) {
3134 /* cannot take align of bit-field */
3138 if (tp
->t_tspec
== VOID
) {
3139 /* cannot take alignsize of void */
3152 return getinode(st
, (int64_t)getbound(tp
));
3159 cast(tnode_t
*tn
, type_t
*tp
)
3169 ot
= tn
->tn_type
->t_tspec
;
3173 * XXX ANSI C requires scalar types or void (Plauger&Brodie).
3174 * But this seams really questionable.
3176 } else if (nt
== STRUCT
|| nt
== UNION
|| nt
== ARRAY
|| nt
== FUNC
) {
3177 /* invalid cast expression */
3180 } else if (ot
== STRUCT
|| ot
== UNION
) {
3181 /* invalid cast expression */
3184 } else if (ot
== VOID
) {
3185 /* improper cast of void expression */
3188 } else if (isityp(nt
) && issclt(ot
)) {
3190 } else if (isftyp(nt
) && isatyp(ot
)) {
3192 } else if (nt
== PTR
&& isityp(ot
)) {
3194 } else if (nt
== PTR
&& ot
== PTR
) {
3195 if (!tp
->t_subt
->t_const
&& tn
->tn_type
->t_subt
->t_const
) {
3197 /* cast discards 'const' from ... */
3201 /* invalid cast expression */
3206 tn
= convert(CVT
, 0, tp
, tn
);
3213 * Create the node for a function argument.
3214 * All necessary conversions and type checks are done in funccall(), because
3215 * in funcarg() we have no information about expected argument types.
3218 funcarg(tnode_t
*args
, tnode_t
*arg
)
3223 * If there was a serious error in the expression for the argument,
3224 * create a dummy argument so the positions of the remaining arguments
3228 arg
= getinode(INT
, (int64_t)0);
3230 ntn
= mktnode(PUSH
, arg
->tn_type
, arg
, args
);
3236 * Create the node for a function call. Also check types of
3237 * function arguments and insert conversions, if necessary.
3240 funccall(tnode_t
*func
, tnode_t
*args
)
3248 if (func
->tn_op
== NAME
&& func
->tn_type
->t_tspec
== FUNC
) {
3255 * after cconv() func will always be a pointer to a function
3256 * if it is a valid function designator.
3260 if (func
->tn_type
->t_tspec
!= PTR
||
3261 func
->tn_type
->t_subt
->t_tspec
!= FUNC
) {
3262 /* illegal function */
3267 args
= chkfarg(func
->tn_type
->t_subt
, args
);
3269 ntn
= mktnode(fcop
, func
->tn_type
->t_subt
->t_subt
, func
, args
);
3275 * Check types of all function arguments and insert conversions,
3279 chkfarg(type_t
*ftp
, tnode_t
*args
)
3284 int narg
, npar
, n
, i
;
3286 /* get # of args in the prototype */
3288 for (asym
= ftp
->t_args
; asym
!= NULL
; asym
= asym
->s_nxt
)
3291 /* get # of args in function call */
3293 for (arg
= args
; arg
!= NULL
; arg
= arg
->tn_right
)
3297 if (ftp
->t_proto
&& npar
!= narg
&& !(ftp
->t_vararg
&& npar
< narg
)) {
3298 /* argument mismatch: %d arg%s passed, %d expected */
3299 error(150, narg
, narg
> 1 ? "s" : "", npar
);
3303 for (n
= 1; n
<= narg
; n
++) {
3306 * The rightmost argument is at the top of the argument
3309 for (i
= narg
, arg
= args
; i
> n
; i
--, arg
= arg
->tn_right
)
3312 /* some things which are always not allowd */
3313 if ((at
= arg
->tn_left
->tn_type
->t_tspec
) == VOID
) {
3314 /* void expressions may not be arguments, arg #%d */
3317 } else if ((at
== STRUCT
|| at
== UNION
) &&
3318 incompl(arg
->tn_left
->tn_type
)) {
3319 /* argument cannot have unknown size, arg #%d */
3322 } else if (isityp(at
) && arg
->tn_left
->tn_type
->t_isenum
&&
3323 incompl(arg
->tn_left
->tn_type
)) {
3324 /* argument cannot have unknown size, arg #%d */
3328 /* class conversions (arg in value context) */
3329 arg
->tn_left
= cconv(arg
->tn_left
);
3332 arg
->tn_left
= parg(n
, asym
->s_type
, arg
->tn_left
);
3334 arg
->tn_left
= promote(NOOP
, 1, arg
->tn_left
);
3336 arg
->tn_type
= arg
->tn_left
->tn_type
;
3346 * Compare the type of an argument with the corresponding type of a
3347 * prototype parameter. If it is a valid combination, but both types
3348 * are not the same, insert a conversion to convert the argument into
3349 * the type of the parameter.
3352 parg( int n
, /* pos of arg */
3353 type_t
*tp
, /* expected type (from prototype) */
3354 tnode_t
*tn
) /* argument */
3359 ln
= xcalloc(1, sizeof (tnode_t
));
3360 ln
->tn_type
= tduptyp(tp
);
3361 ln
->tn_type
->t_const
= 0;
3363 if (typeok(FARG
, n
, ln
, tn
)) {
3364 if (!eqtype(tp
, tn
->tn_type
, 1, 0, (dowarn
= 0, &dowarn
)) || dowarn
)
3365 tn
= convert(FARG
, n
, tp
, tn
);
3372 * Return the value of an integral constant expression.
3373 * If the expression is not constant or its type is not an integer
3374 * type, an error message is printed.
3377 constant(tnode_t
*tn
, int required
)
3384 tn
= promote(NOOP
, 0, tn
);
3386 v
= xcalloc(1, sizeof (val_t
));
3390 LERROR("constant()");
3396 v
->v_tspec
= tn
->tn_type
->t_tspec
;
3398 if (tn
->tn_op
== CON
) {
3399 if (tn
->tn_type
->t_tspec
!= tn
->tn_val
->v_tspec
)
3400 LERROR("constant()");
3401 if (isityp(tn
->tn_val
->v_tspec
)) {
3402 v
->v_ansiu
= tn
->tn_val
->v_ansiu
;
3403 v
->v_quad
= tn
->tn_val
->v_quad
;
3406 v
->v_quad
= tn
->tn_val
->v_ldbl
;
3411 /* integral constant expression expected */
3417 if (!isityp(v
->v_tspec
))
3424 * Perform some tests on expressions which can't be done in build() and
3425 * functions called by build(). These tests must be done here because
3426 * we need some information about the context in which the operations
3428 * After all tests are performed, expr() frees the memory which is used
3429 * for the expression.
3432 expr(tnode_t
*tn
, int vctx
, int tctx
, int dofreeblk
)
3435 if (tn
== NULL
&& nerr
== 0)
3443 /* expr() is also called in global initialisations */
3444 if (dcs
->d_ctx
!= EXTERN
)
3447 chkmisc(tn
, vctx
, tctx
, !tctx
, 0, 0, 0);
3448 if (tn
->tn_op
== ASSIGN
) {
3450 /* assignment in conditional context */
3452 } else if (tn
->tn_op
== CON
) {
3453 if (hflag
&& tctx
&& !ccflg
)
3454 /* constant in conditional context */
3457 if (!modtab
[tn
->tn_op
].m_sideeff
) {
3459 * for left operands of COMMA this warning is already
3462 if (tn
->tn_op
!= COMMA
&& !vctx
&& !tctx
)
3468 /* free the tree memory */
3474 nulleff(tnode_t
*tn
)
3480 while (!modtab
[tn
->tn_op
].m_sideeff
) {
3481 if (tn
->tn_op
== CVT
&& tn
->tn_type
->t_tspec
== VOID
) {
3483 } else if (tn
->tn_op
== LOGAND
|| tn
->tn_op
== LOGOR
) {
3485 * && and || have a side effect if the right operand
3486 * has a side effect.
3489 } else if (tn
->tn_op
== QUEST
) {
3491 * ? has a side effect if at least one of its right
3492 * operands has a side effect
3495 } else if (tn
->tn_op
== COLON
|| tn
->tn_op
== COMMA
) {
3497 * : has a side effect if at least one of its operands
3500 if (modtab
[tn
->tn_left
->tn_op
].m_sideeff
) {
3502 } else if (modtab
[tn
->tn_right
->tn_op
].m_sideeff
) {
3511 if (!modtab
[tn
->tn_op
].m_sideeff
)
3512 /* expression has null effect */
3517 * Dump an expression to stdout
3518 * only used for debugging
3521 displexpr(tnode_t
*tn
, int offs
)
3526 (void)printf("%*s%s\n", offs
, "", "NULL");
3529 (void)printf("%*sop %s ", offs
, "", modtab
[tn
->tn_op
].m_name
);
3531 if (tn
->tn_op
== NAME
) {
3532 (void)printf("%s: %s ",
3533 tn
->tn_sym
->s_name
, scltoa(tn
->tn_sym
->s_scl
));
3534 } else if (tn
->tn_op
== CON
&& isftyp(tn
->tn_type
->t_tspec
)) {
3535 (void)printf("%#g ", (double)tn
->tn_val
->v_ldbl
);
3536 } else if (tn
->tn_op
== CON
&& isityp(tn
->tn_type
->t_tspec
)) {
3537 uq
= tn
->tn_val
->v_quad
;
3538 (void)printf("0x %08lx %08lx ", (long)(uq
>> 32) & 0xffffffffl
,
3539 (long)uq
& 0xffffffffl
);
3540 } else if (tn
->tn_op
== CON
) {
3541 if (tn
->tn_type
->t_tspec
!= PTR
)
3542 LERROR("displexpr()");
3543 (void)printf("0x%0*lx ", (int)(sizeof (void *) * CHAR_BIT
/ 4),
3544 (u_long
)tn
->tn_val
->v_quad
);
3545 } else if (tn
->tn_op
== STRING
) {
3546 if (tn
->tn_strg
->st_tspec
== CHAR
) {
3547 (void)printf("\"%s\"", tn
->tn_strg
->st_cp
);
3551 n
= MB_CUR_MAX
* (tn
->tn_strg
->st_len
+ 1);
3553 (void)wcstombs(s
, tn
->tn_strg
->st_wcp
, n
);
3554 (void)printf("L\"%s\"", s
);
3558 } else if (tn
->tn_op
== FSEL
) {
3559 (void)printf("o=%d, l=%d ", tn
->tn_type
->t_foffs
,
3560 tn
->tn_type
->t_flen
);
3562 (void)printf("%s\n", ttos(tn
->tn_type
));
3563 if (tn
->tn_op
== NAME
|| tn
->tn_op
== CON
|| tn
->tn_op
== STRING
)
3565 displexpr(tn
->tn_left
, offs
+ 2);
3566 if (modtab
[tn
->tn_op
].m_binary
||
3567 (tn
->tn_op
== PUSH
&& tn
->tn_right
!= NULL
)) {
3568 displexpr(tn
->tn_right
, offs
+ 2);
3573 * Called by expr() to recursively perform some tests.
3577 chkmisc(tnode_t
*tn
, int vctx
, int tctx
, int eqwarn
, int fcall
, int rvdisc
,
3582 int nrvdisc
, cvctx
, ctctx
;
3592 mp
= &modtab
[op
= tn
->tn_op
];
3596 if (ln
->tn_op
== NAME
&& (reached
|| rchflg
)) {
3598 setsflg(ln
->tn_sym
);
3599 setuflg(ln
->tn_sym
, fcall
, szof
);
3601 if (ln
->tn_op
== STAR
&& ln
->tn_left
->tn_op
== PLUS
)
3602 /* check the range of array indices */
3603 chkaidx(ln
->tn_left
, 1);
3606 if (ln
->tn_op
== STAR
&& ln
->tn_left
->tn_op
== PLUS
)
3607 /* check the range of array indices */
3608 chkaidx(ln
->tn_left
, 0);
3627 if (ln
->tn_op
== NAME
&& (reached
|| rchflg
)) {
3628 sc
= ln
->tn_sym
->s_scl
;
3630 * Look if there was a asm statement in one of the
3631 * compound statements we are in. If not, we don't
3634 for (di
= dcs
; di
!= NULL
; di
= di
->d_nxt
) {
3638 if (sc
!= EXTERN
&& sc
!= STATIC
&&
3639 !ln
->tn_sym
->s_set
&& !szof
&& di
== NULL
) {
3640 /* %s may be used before set */
3641 warning(158, ln
->tn_sym
->s_name
);
3642 setsflg(ln
->tn_sym
);
3644 setuflg(ln
->tn_sym
, 0, 0);
3648 if (ln
->tn_op
== NAME
&& !szof
&& (reached
|| rchflg
)) {
3649 setsflg(ln
->tn_sym
);
3650 if (ln
->tn_sym
->s_scl
== EXTERN
)
3653 if (ln
->tn_op
== STAR
&& ln
->tn_left
->tn_op
== PLUS
)
3654 /* check the range of array indices */
3655 chkaidx(ln
->tn_left
, 0);
3658 if (ln
->tn_op
!= AMPER
|| ln
->tn_left
->tn_op
!= NAME
)
3659 LERROR("chkmisc()");
3661 outcall(tn
, vctx
|| tctx
, rvdisc
);
3664 /* equality operator "==" found where "=" was exp. */
3665 if (hflag
&& eqwarn
)
3672 /* LINTED (enumeration values not handled in switch) */
3716 * values of operands of ':' are not used if the type of at least
3717 * one of the operands (for gcc compatibility) is void
3718 * XXX test/value context of QUEST should probably be used as
3719 * context for both operands of COLON
3721 if (op
== COLON
&& tn
->tn_type
->t_tspec
== VOID
)
3723 nrvdisc
= op
== CVT
&& tn
->tn_type
->t_tspec
== VOID
;
3724 chkmisc(ln
, cvctx
, ctctx
, mp
->m_eqwarn
, op
== CALL
, nrvdisc
, szof
);
3729 chkmisc(rn
, 0, 0, mp
->m_eqwarn
, 0, 0, szof
);
3733 chkmisc(rn
, 0, 1, mp
->m_eqwarn
, 0, 0, szof
);
3736 chkmisc(rn
, cvctx
, ctctx
, mp
->m_eqwarn
, 0, 0, szof
);
3739 chkmisc(rn
, vctx
, tctx
, mp
->m_eqwarn
, 0, 0, szof
);
3743 chkmisc(rn
, 1, 0, mp
->m_eqwarn
, 0, 0, szof
);
3750 * Checks the range of array indices, if possible.
3751 * amper is set if only the address of the element is used. This
3752 * means that the index is allowd to refere to the first element
3756 chkaidx(tnode_t
*tn
, int amper
)
3766 /* We can only check constant indices. */
3767 if (rn
->tn_op
!= CON
)
3770 /* Return if the left node does not stem from an array. */
3771 if (ln
->tn_op
!= AMPER
)
3773 if (ln
->tn_left
->tn_op
!= STRING
&& ln
->tn_left
->tn_op
!= NAME
)
3775 if (ln
->tn_left
->tn_type
->t_tspec
!= ARRAY
)
3779 * For incomplete array types, we can print a warning only if
3780 * the index is negative.
3782 if (incompl(ln
->tn_left
->tn_type
) && rn
->tn_val
->v_quad
>= 0)
3785 /* Get the size of one array element */
3786 if ((elsz
= length(ln
->tn_type
->t_subt
, NULL
)) == 0)
3790 /* Change the unit of the index from bytes to element size. */
3791 if (isutyp(rn
->tn_type
->t_tspec
)) {
3792 con
= (uint64_t)rn
->tn_val
->v_quad
/ elsz
;
3794 con
= rn
->tn_val
->v_quad
/ elsz
;
3797 dim
= ln
->tn_left
->tn_type
->t_dim
+ (amper
? 1 : 0);
3799 if (!isutyp(rn
->tn_type
->t_tspec
) && con
< 0) {
3800 /* array subscript cannot be negative: %ld */
3801 warning(167, (long)con
);
3802 } else if (dim
> 0 && (uint64_t)con
>= (uint64_t)dim
) {
3803 /* array subscript cannot be > %d: %ld */
3804 warning(168, dim
- 1, (long)con
);
3809 * Check for ordered comparisons of unsigned values with 0.
3812 chkcomp(op_t op
, tnode_t
*ln
, tnode_t
*rn
)
3818 lt
= ln
->tn_type
->t_tspec
;
3819 rt
= rn
->tn_type
->t_tspec
;
3822 if (ln
->tn_op
!= CON
&& rn
->tn_op
!= CON
)
3825 if (!isityp(lt
) || !isityp(rt
))
3828 if ((hflag
|| pflag
) && lt
== CHAR
&& rn
->tn_op
== CON
&&
3829 (rn
->tn_val
->v_quad
< 0 ||
3830 rn
->tn_val
->v_quad
> ~(~0 << (CHAR_BIT
- 1)))) {
3831 /* nonportable character comparison, op %s */
3832 warning(230, mp
->m_name
);
3835 if ((hflag
|| pflag
) && rt
== CHAR
&& ln
->tn_op
== CON
&&
3836 (ln
->tn_val
->v_quad
< 0 ||
3837 ln
->tn_val
->v_quad
> ~(~0 << (CHAR_BIT
- 1)))) {
3838 /* nonportable character comparison, op %s */
3839 warning(230, mp
->m_name
);
3842 if (isutyp(lt
) && !isutyp(rt
) &&
3843 rn
->tn_op
== CON
&& rn
->tn_val
->v_quad
<= 0) {
3844 if (rn
->tn_val
->v_quad
< 0) {
3845 /* comparison of %s with %s, op %s */
3846 warning(162, tyname(buf
, sizeof(buf
), ln
->tn_type
),
3847 "negative constant", mp
->m_name
);
3848 } else if (op
== LT
|| op
== GE
|| (hflag
&& op
== LE
)) {
3849 /* comparison of %s with %s, op %s */
3850 warning(162, tyname(buf
, sizeof(buf
), ln
->tn_type
),
3855 if (isutyp(rt
) && !isutyp(lt
) &&
3856 ln
->tn_op
== CON
&& ln
->tn_val
->v_quad
<= 0) {
3857 if (ln
->tn_val
->v_quad
< 0) {
3858 /* comparison of %s with %s, op %s */
3859 warning(162, "negative constant",
3860 tyname(buf
, sizeof(buf
), rn
->tn_type
), mp
->m_name
);
3861 } else if (op
== GT
|| op
== LE
|| (hflag
&& op
== GE
)) {
3862 /* comparison of %s with %s, op %s */
3863 warning(162, "0", tyname(buf
, sizeof(buf
), rn
->tn_type
),
3871 * Takes an expression an returns 0 if this expression can be used
3872 * for static initialisation, otherwise -1.
3874 * Constant initialisation expressions must be constant or an address
3875 * of a static object with an optional offset. In the first case,
3876 * the result is returned in *offsp. In the second case, the static
3877 * object is returned in *symp and the offset in *offsp.
3879 * The expression can consist of PLUS, MINUS, AMPER, NAME, STRING and
3880 * CON. Type conversions are allowed if they do not change binary
3881 * representation (including width).
3884 conaddr(tnode_t
*tn
, sym_t
**symp
, ptrdiff_t *offsp
)
3887 ptrdiff_t offs1
, offs2
;
3890 switch (tn
->tn_op
) {
3892 if (tn
->tn_right
->tn_op
== CVT
)
3893 return conaddr(tn
->tn_right
, symp
, offsp
);
3894 else if (tn
->tn_right
->tn_op
!= CON
)
3899 if (tn
->tn_left
->tn_op
== CON
) {
3900 offs1
= (ptrdiff_t)tn
->tn_left
->tn_val
->v_quad
;
3901 if (conaddr(tn
->tn_right
, &sym
, &offs2
) == -1)
3903 } else if (tn
->tn_right
->tn_op
== CON
) {
3904 offs2
= (ptrdiff_t)tn
->tn_right
->tn_val
->v_quad
;
3905 if (tn
->tn_op
== MINUS
)
3907 if (conaddr(tn
->tn_left
, &sym
, &offs1
) == -1)
3913 *offsp
= offs1
+ offs2
;
3916 if (tn
->tn_left
->tn_op
== NAME
) {
3917 *symp
= tn
->tn_left
->tn_sym
;
3919 } else if (tn
->tn_left
->tn_op
== STRING
) {
3921 * If this would be the front end of a compiler we
3922 * would return a label instead of 0.
3928 t
= tn
->tn_type
->t_tspec
;
3929 ot
= tn
->tn_left
->tn_type
->t_tspec
;
3930 if ((!isityp(t
) && t
!= PTR
) || (!isityp(ot
) && ot
!= PTR
))
3938 * (u_char)(u_long)(&(((struct foo *)0)->a))
3940 * since psize(u_long) != psize(u_char) this fails.
3942 else if (psize(t
) != psize(ot
))
3945 if (conaddr(tn
->tn_left
, symp
, offsp
) == -1)
3955 * Concatenate two string constants.
3958 catstrg(strg_t
*strg1
, strg_t
*strg2
)
3960 size_t len1
, len2
, len
;
3962 if (strg1
->st_tspec
!= strg2
->st_tspec
) {
3963 /* cannot concatenate wide and regular string literals */
3968 len
= (len1
= strg1
->st_len
) + (len2
= strg2
->st_len
);
3970 if (strg1
->st_tspec
== CHAR
) {
3971 strg1
->st_cp
= xrealloc(strg1
->st_cp
, len
+ 1);
3972 (void)memcpy(strg1
->st_cp
+ len1
, strg2
->st_cp
, len2
+ 1);
3975 strg1
->st_wcp
= xrealloc(strg1
->st_wcp
,
3976 (len
+ 1) * sizeof (wchar_t));
3977 (void)memcpy(strg1
->st_wcp
+ len1
, strg2
->st_wcp
,
3978 (len2
+ 1) * sizeof (wchar_t));
3979 free(strg2
->st_wcp
);
3987 * Print a warning if the given node has operands which should be
3990 * XXX Does not work if an operand is a constant expression. Constant
3991 * expressions are already folded.
3994 precconf(tnode_t
*tn
)
3997 op_t lop
, rop
= NOOP
;
3998 int lparn
, rparn
= 0;
4005 mp
= &modtab
[tn
->tn_op
];
4008 for (ln
= tn
->tn_left
; ln
->tn_op
== CVT
; ln
= ln
->tn_left
)
4009 lparn
|= ln
->tn_parn
;
4010 lparn
|= ln
->tn_parn
;
4015 for (rn
= tn
->tn_right
; tn
->tn_op
== CVT
; rn
= rn
->tn_left
)
4016 rparn
|= rn
->tn_parn
;
4017 rparn
|= rn
->tn_parn
;
4023 switch (tn
->tn_op
) {
4026 if (!lparn
&& (lop
== PLUS
|| lop
== MINUS
)) {
4028 } else if (!rparn
&& (rop
== PLUS
|| rop
== MINUS
)) {
4033 if (!lparn
&& lop
== LOGAND
) {
4035 } else if (!rparn
&& rop
== LOGAND
) {
4042 if (!lparn
&& lop
!= tn
->tn_op
) {
4043 if (lop
== PLUS
|| lop
== MINUS
) {
4045 } else if (lop
== AND
|| lop
== XOR
) {
4049 if (!dowarn
&& !rparn
&& rop
!= tn
->tn_op
) {
4050 if (rop
== PLUS
|| rop
== MINUS
) {
4052 } else if (rop
== AND
|| rop
== XOR
) {
4057 /* LINTED (enumeration values not handled in switch) */
4118 /* precedence confusion possible: parenthesize! */