Home | History | Annotate | Line # | Download | only in lint1
tree.c revision 1.237
      1 /*	$NetBSD: tree.c,v 1.237 2021/03/19 08:21:26 rillig Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994, 1995 Jochen Pohl
      5  * All Rights Reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     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
     18  *	The NetBSD Project.
     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.
     21  *
     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.
     32  */
     33 
     34 #if HAVE_NBTOOL_CONFIG_H
     35 #include "nbtool_config.h"
     36 #endif
     37 
     38 #include <sys/cdefs.h>
     39 #if defined(__RCSID) && !defined(lint)
     40 __RCSID("$NetBSD: tree.c,v 1.237 2021/03/19 08:21:26 rillig Exp $");
     41 #endif
     42 
     43 #include <float.h>
     44 #include <limits.h>
     45 #include <math.h>
     46 #include <signal.h>
     47 #include <stdlib.h>
     48 #include <string.h>
     49 
     50 #include "lint1.h"
     51 #include "cgram.h"
     52 
     53 static	tnode_t	*new_integer_constant_node(tspec_t, int64_t);
     54 static	void	check_pointer_comparison(op_t,
     55 					 const tnode_t *, const tnode_t *);
     56 static	bool	check_assign_types_compatible(op_t, int,
     57 					      const tnode_t *, const tnode_t *);
     58 static	void	check_bad_enum_operation(op_t,
     59 					 const tnode_t *, const tnode_t *);
     60 static	void	check_enum_type_mismatch(op_t, int,
     61 				         const tnode_t *, const tnode_t *);
     62 static	void	check_enum_int_mismatch(op_t, int,
     63 					const tnode_t *, const tnode_t *);
     64 static	tnode_t	*new_tnode(op_t, type_t *, tnode_t *, tnode_t *);
     65 static	void	balance(op_t, tnode_t **, tnode_t **);
     66 static	void	warn_incompatible_types(op_t, tspec_t, tspec_t);
     67 static	void	warn_incompatible_pointers(const mod_t *,
     68 					   const type_t *, const type_t *);
     69 static	void	merge_qualifiers(type_t **, type_t *, type_t *);
     70 static	bool	has_constant_member(const type_t *);
     71 static	void	check_prototype_conversion(int, tspec_t, tspec_t, type_t *,
     72 					   tnode_t *);
     73 static	void	check_integer_conversion(op_t, int, tspec_t, tspec_t, type_t *,
     74 					 tnode_t *);
     75 static	void	check_pointer_integer_conversion(op_t, tspec_t, type_t *,
     76 						 tnode_t *);
     77 static	void	check_pointer_conversion(op_t, tnode_t *, type_t *);
     78 static	tnode_t	*build_struct_access(op_t, tnode_t *, tnode_t *);
     79 static	tnode_t	*build_prepost_incdec(op_t, tnode_t *);
     80 static	tnode_t	*build_real_imag(op_t, tnode_t *);
     81 static	tnode_t	*build_address(tnode_t *, bool);
     82 static	tnode_t	*build_plus_minus(op_t, tnode_t *, tnode_t *);
     83 static	tnode_t	*build_bit_shift(op_t, tnode_t *, tnode_t *);
     84 static	tnode_t	*build_colon(tnode_t *, tnode_t *);
     85 static	tnode_t	*build_assignment(op_t, tnode_t *, tnode_t *);
     86 static	tnode_t	*plength(type_t *);
     87 static	tnode_t	*fold(tnode_t *);
     88 static	tnode_t	*fold_test(tnode_t *);
     89 static	tnode_t	*fold_float(tnode_t *);
     90 static	tnode_t	*check_function_arguments(type_t *, tnode_t *);
     91 static	tnode_t	*check_prototype_argument(int, type_t *, tnode_t *);
     92 static	void	check_null_effect(const tnode_t *);
     93 static	void	display_expression(const tnode_t *, int);
     94 static	void	check_array_index(tnode_t *, bool);
     95 static	void	check_integer_comparison(op_t, tnode_t *, tnode_t *);
     96 static	void	check_precedence_confusion(tnode_t *);
     97 
     98 extern sig_atomic_t fpe;
     99 
    100 #ifdef DEBUG
    101 void
    102 debug_node(const tnode_t *tn, int indent)
    103 {
    104 	op_t op;
    105 
    106 	if (tn == NULL) {
    107 		printf("%*s" "null\n", indent, "");
    108 		return;
    109 	}
    110 
    111 	op = tn->tn_op;
    112 	printf("%*s%s with type '%s'%s%s",
    113 	    2 * indent, "",
    114 	    op == CVT && !tn->tn_cast ? "convert" : getopname(op),
    115 	    type_name(tn->tn_type), tn->tn_lvalue ? ", lvalue" : "",
    116 	    tn->tn_parenthesized ? ", parenthesized" : "");
    117 
    118 	if (op == NAME)
    119 		printf(" %s\n", tn->tn_sym->s_name);
    120 	else if (op == CON && is_floating(tn->tn_type->t_tspec))
    121 		printf(", value %Lg", tn->tn_val->v_ldbl);
    122 	else if (op == CON && is_uinteger(tn->tn_type->t_tspec))
    123 		printf(", value %llu\n", (unsigned long long)tn->tn_val->v_quad);
    124 	else if (op == CON && is_integer(tn->tn_type->t_tspec))
    125 		printf(", value %lld\n", (long long)tn->tn_val->v_quad);
    126 	else if (op == CON)
    127 		printf(", unknown value\n");
    128 	else if (op == STRING)
    129 		printf(", length %zu\n", tn->tn_string->st_len);
    130 	else {
    131 		printf("\n");
    132 
    133 		debug_node(tn->tn_left, indent + 1);
    134 		if (modtab[op].m_binary || tn->tn_right != NULL)
    135 			debug_node(tn->tn_right, indent + 1);
    136 	}
    137 }
    138 #endif
    139 
    140 /*
    141  * Increase degree of reference.
    142  * This is most often used to change type "T" in type "pointer to T".
    143  */
    144 type_t *
    145 incref(type_t *tp, tspec_t t)
    146 {
    147 	type_t	*tp2;
    148 
    149 	tp2 = getblk(sizeof (type_t));
    150 	tp2->t_tspec = t;
    151 	tp2->t_subt = tp;
    152 	return tp2;
    153 }
    154 
    155 /*
    156  * same for use in expressions
    157  */
    158 type_t *
    159 tincref(type_t *tp, tspec_t t)
    160 {
    161 	type_t	*tp2;
    162 
    163 	tp2 = tgetblk(sizeof (type_t));
    164 	tp2->t_tspec = t;
    165 	tp2->t_subt = tp;
    166 	return tp2;
    167 }
    168 
    169 /*
    170  * Create a node for a constant.
    171  */
    172 tnode_t *
    173 new_constant_node(type_t *tp, val_t *v)
    174 {
    175 	tnode_t	*n;
    176 
    177 	n = getnode();
    178 	n->tn_op = CON;
    179 	n->tn_type = tp;
    180 	n->tn_val = tgetblk(sizeof (val_t));
    181 	n->tn_val->v_tspec = tp->t_tspec;
    182 	n->tn_val->v_ansiu = v->v_ansiu;
    183 	n->tn_val->v_u = v->v_u;
    184 	free(v);
    185 	return n;
    186 }
    187 
    188 static tnode_t *
    189 new_integer_constant_node(tspec_t t, int64_t q)
    190 {
    191 	tnode_t	*n;
    192 
    193 	n = getnode();
    194 	n->tn_op = CON;
    195 	n->tn_type = gettyp(t);
    196 	n->tn_val = tgetblk(sizeof (val_t));
    197 	n->tn_val->v_tspec = t;
    198 	n->tn_val->v_quad = q;
    199 	return n;
    200 }
    201 
    202 static void
    203 fallback_symbol(sym_t *sym)
    204 {
    205 
    206 	if (Tflag && strcmp(sym->s_name, "__lint_false") == 0) {
    207 		sym->s_scl = CTCONST; /* close enough */
    208 		sym->s_type = gettyp(BOOL);
    209 		sym->s_value.v_tspec = BOOL;
    210 		sym->s_value.v_ansiu = false;
    211 		sym->s_value.v_quad = 0;
    212 		return;
    213 	}
    214 
    215 	if (Tflag && strcmp(sym->s_name, "__lint_true") == 0) {
    216 		sym->s_scl = CTCONST; /* close enough */
    217 		sym->s_type = gettyp(BOOL);
    218 		sym->s_value.v_tspec = BOOL;
    219 		sym->s_value.v_ansiu = false;
    220 		sym->s_value.v_quad = 1;
    221 		return;
    222 	}
    223 
    224 	if (block_level > 0 && (strcmp(sym->s_name, "__FUNCTION__") == 0 ||
    225 			   strcmp(sym->s_name, "__PRETTY_FUNCTION__") == 0)) {
    226 		/* __FUNCTION__/__PRETTY_FUNCTION__ is a GCC extension */
    227 		gnuism(316);
    228 		sym->s_type = incref(gettyp(CHAR), PTR);
    229 		sym->s_type->t_const = true;
    230 		return;
    231 	}
    232 
    233 	if (block_level > 0 && strcmp(sym->s_name, "__func__") == 0) {
    234 		if (!Sflag)
    235 			/* __func__ is a C9X feature */
    236 			warning(317);
    237 		sym->s_type = incref(gettyp(CHAR), PTR);
    238 		sym->s_type->t_const = true;
    239 		return;
    240 	}
    241 
    242 	/* %s undefined */
    243 	error(99, sym->s_name);
    244 }
    245 
    246 /*
    247  * Create a node for a name (symbol table entry).
    248  * ntok is the token which follows the name.
    249  */
    250 tnode_t *
    251 new_name_node(sym_t *sym, int ntok)
    252 {
    253 	tnode_t	*n;
    254 
    255 	if (sym->s_scl == NOSCL) {
    256 		sym->s_scl = EXTERN;
    257 		sym->s_def = DECL;
    258 		if (ntok == T_LPAREN) {
    259 			if (sflag) {
    260 				/* function implicitly declared to ... */
    261 				warning(215);
    262 			}
    263 			/*
    264 			 * XXX if tflag is set the symbol should be
    265 			 * exported to level 0
    266 			 */
    267 			sym->s_type = incref(sym->s_type, FUNC);
    268 		} else {
    269 			fallback_symbol(sym);
    270 		}
    271 	}
    272 
    273 	if (sym->s_kind != FVFT && sym->s_kind != FMEMBER)
    274 		LERROR("new_name_node(%d)", sym->s_kind);
    275 
    276 	n = getnode();
    277 	n->tn_type = sym->s_type;
    278 	if (sym->s_scl != CTCONST) {
    279 		n->tn_op = NAME;
    280 		n->tn_sym = sym;
    281 		if (sym->s_kind == FVFT && sym->s_type->t_tspec != FUNC)
    282 			n->tn_lvalue = true;
    283 	} else {
    284 		n->tn_op = CON;
    285 		n->tn_val = tgetblk(sizeof (val_t));
    286 		*n->tn_val = sym->s_value;
    287 	}
    288 
    289 	return n;
    290 }
    291 
    292 tnode_t *
    293 new_string_node(strg_t *strg)
    294 {
    295 	size_t	len;
    296 	tnode_t	*n;
    297 
    298 	len = strg->st_len;
    299 
    300 	n = getnode();
    301 
    302 	n->tn_op = STRING;
    303 	n->tn_type = tincref(gettyp(strg->st_tspec), ARRAY);
    304 	n->tn_type->t_dim = len + 1;
    305 	n->tn_lvalue = true;
    306 
    307 	n->tn_string = tgetblk(sizeof (strg_t));
    308 	n->tn_string->st_tspec = strg->st_tspec;
    309 	n->tn_string->st_len = len;
    310 
    311 	if (strg->st_tspec == CHAR) {
    312 		n->tn_string->st_cp = tgetblk(len + 1);
    313 		(void)memcpy(n->tn_string->st_cp, strg->st_cp, len + 1);
    314 		free(strg->st_cp);
    315 	} else {
    316 		n->tn_string->st_wcp = tgetblk((len + 1) * sizeof (wchar_t));
    317 		(void)memcpy(n->tn_string->st_wcp, strg->st_wcp,
    318 			     (len + 1) * sizeof (wchar_t));
    319 		free(strg->st_wcp);
    320 	}
    321 	free(strg);
    322 
    323 	return n;
    324 }
    325 
    326 /*
    327  * Returns a symbol which has the same name as the msym argument and is a
    328  * member of the struct or union specified by the tn argument.
    329  */
    330 sym_t *
    331 struct_or_union_member(tnode_t *tn, op_t op, sym_t *msym)
    332 {
    333 	struct_or_union	*str;
    334 	type_t	*tp;
    335 	sym_t	*sym, *csym;
    336 	bool	eq;
    337 	tspec_t	t;
    338 
    339 	/*
    340 	 * Remove the member if it was unknown until now, which means
    341 	 * that no defined struct or union has a member with the same name.
    342 	 */
    343 	if (msym->s_scl == NOSCL) {
    344 		/* undefined struct/union member: %s */
    345 		error(101, msym->s_name);
    346 		rmsym(msym);
    347 		msym->s_kind = FMEMBER;
    348 		msym->s_scl = MOS;
    349 		msym->s_styp = tgetblk(sizeof (struct_or_union));
    350 		msym->s_styp->sou_tag = tgetblk(sizeof (sym_t));
    351 		msym->s_styp->sou_tag->s_name = unnamed;
    352 		msym->s_value.v_tspec = INT;
    353 		return msym;
    354 	}
    355 
    356 	/* Set str to the tag of which msym is expected to be a member. */
    357 	str = NULL;
    358 	t = (tp = tn->tn_type)->t_tspec;
    359 	if (op == POINT) {
    360 		if (t == STRUCT || t == UNION)
    361 			str = tp->t_str;
    362 	} else if (op == ARROW && t == PTR) {
    363 		t = (tp = tp->t_subt)->t_tspec;
    364 		if (t == STRUCT || t == UNION)
    365 			str = tp->t_str;
    366 	}
    367 
    368 	/*
    369 	 * If this struct/union has a member with the name of msym, return it.
    370 	 */
    371 	if (str != NULL) {
    372 		for (sym = msym; sym != NULL; sym = sym->s_link) {
    373 			if (sym->s_scl != MOS && sym->s_scl != MOU)
    374 				continue;
    375 			if (sym->s_styp != str)
    376 				continue;
    377 			if (strcmp(sym->s_name, msym->s_name) != 0)
    378 				continue;
    379 			return sym;
    380 		}
    381 	}
    382 
    383 	/*
    384 	 * Set eq to false if there are struct/union members with the same
    385 	 * name and different types and/or offsets.
    386 	 */
    387 	eq = true;
    388 	for (csym = msym; csym != NULL; csym = csym->s_link) {
    389 		if (csym->s_scl != MOS && csym->s_scl != MOU)
    390 			continue;
    391 		if (strcmp(msym->s_name, csym->s_name) != 0)
    392 			continue;
    393 		for (sym = csym->s_link ; sym != NULL; sym = sym->s_link) {
    394 			bool w;
    395 
    396 			if (sym->s_scl != MOS && sym->s_scl != MOU)
    397 				continue;
    398 			if (strcmp(csym->s_name, sym->s_name) != 0)
    399 				continue;
    400 			if (csym->s_value.v_quad != sym->s_value.v_quad) {
    401 				eq = false;
    402 				break;
    403 			}
    404 			w = false;
    405 			eq = eqtype(csym->s_type, sym->s_type,
    406 			    false, false, &w) && !w;
    407 			if (!eq)
    408 				break;
    409 			if (csym->s_bitfield != sym->s_bitfield) {
    410 				eq = false;
    411 				break;
    412 			}
    413 			if (csym->s_bitfield) {
    414 				type_t	*tp1, *tp2;
    415 
    416 				tp1 = csym->s_type;
    417 				tp2 = sym->s_type;
    418 				if (tp1->t_flen != tp2->t_flen) {
    419 					eq = false;
    420 					break;
    421 				}
    422 				if (tp1->t_foffs != tp2->t_foffs) {
    423 					eq = false;
    424 					break;
    425 				}
    426 			}
    427 		}
    428 		if (!eq)
    429 			break;
    430 	}
    431 
    432 	/*
    433 	 * Now handle the case in which the left operand refers really
    434 	 * to a struct/union, but the right operand is not member of it.
    435 	 */
    436 	if (str != NULL) {
    437 		if (eq && tflag) {
    438 			/* illegal member use: %s */
    439 			warning(102, msym->s_name);
    440 		} else {
    441 			/* illegal member use: %s */
    442 			error(102, msym->s_name);
    443 		}
    444 		return msym;
    445 	}
    446 
    447 	/*
    448 	 * Now the left operand of ARROW does not point to a struct/union
    449 	 * or the left operand of POINT is no struct/union.
    450 	 */
    451 	if (eq) {
    452 		if (op == POINT) {
    453 			if (tflag) {
    454 				/* left operand of '.' must be struct/... */
    455 				warning(103);
    456 			} else {
    457 				/* left operand of '.' must be struct/... */
    458 				error(103);
    459 			}
    460 		} else {
    461 			if (tflag && tn->tn_type->t_tspec == PTR) {
    462 				/* left operand of '->' must be pointer ... */
    463 				warning(104, type_name(tn->tn_type));
    464 			} else {
    465 				/* left operand of '->' must be pointer ... */
    466 				error(104, type_name(tn->tn_type));
    467 			}
    468 		}
    469 	} else {
    470 		if (tflag) {
    471 			/* non-unique member requires struct/union %s */
    472 			error(105, op == POINT ? "object" : "pointer");
    473 		} else {
    474 			/* unacceptable operand of '%s' */
    475 			error(111, modtab[op].m_name);
    476 		}
    477 	}
    478 
    479 	return msym;
    480 }
    481 
    482 /*
    483  * Create a tree node. Called for most operands except function calls,
    484  * sizeof and casts.
    485  *
    486  * op	operator
    487  * ln	left operand
    488  * rn	if not NULL, right operand
    489  */
    490 tnode_t *
    491 build(op_t op, tnode_t *ln, tnode_t *rn)
    492 {
    493 	mod_t	*mp;
    494 	tnode_t	*ntn;
    495 	type_t	*rettp;
    496 
    497 	mp = &modtab[op];
    498 
    499 	/* If there was an error in one of the operands, return. */
    500 	if (ln == NULL || (mp->m_binary && rn == NULL))
    501 		return NULL;
    502 
    503 	/*
    504 	 * Apply class conversions to the left operand, but only if its
    505 	 * value is needed or it is compared with null.
    506 	 */
    507 	if (mp->m_left_value_context || mp->m_left_test_context)
    508 		ln = cconv(ln);
    509 	/*
    510 	 * The right operand is almost always in a test or value context,
    511 	 * except if it is a struct or union member.
    512 	 */
    513 	if (mp->m_binary && op != ARROW && op != POINT)
    514 		rn = cconv(rn);
    515 
    516 	/*
    517 	 * Print some warnings for comparisons of unsigned values with
    518 	 * constants lower than or equal to null. This must be done
    519 	 * before promote() because otherwise unsigned char and unsigned
    520 	 * short would be promoted to int. Also types are tested to be
    521 	 * CHAR, which would also become int.
    522 	 */
    523 	if (mp->m_comparison)
    524 		check_integer_comparison(op, ln, rn);
    525 
    526 	/*
    527 	 * Promote the left operand if it is in a test or value context
    528 	 */
    529 	if (mp->m_left_value_context || mp->m_left_test_context)
    530 		ln = promote(op, false, ln);
    531 	/*
    532 	 * Promote the right operand, but only if it is no struct or
    533 	 * union member, or if it is not to be assigned to the left operand
    534 	 */
    535 	if (mp->m_binary && op != ARROW && op != POINT &&
    536 	    op != ASSIGN && op != RETURN) {
    537 		rn = promote(op, false, rn);
    538 	}
    539 
    540 	/*
    541 	 * If the result of the operation is different for signed or
    542 	 * unsigned operands and one of the operands is signed only in
    543 	 * ANSI C, print a warning.
    544 	 */
    545 	if (mp->m_warn_if_left_unsigned_in_c90 &&
    546 	    ln->tn_op == CON && ln->tn_val->v_ansiu) {
    547 		/* ANSI C treats constant as unsigned, op %s */
    548 		warning(218, mp->m_name);
    549 		ln->tn_val->v_ansiu = false;
    550 	}
    551 	if (mp->m_warn_if_right_unsigned_in_c90 &&
    552 	    rn->tn_op == CON && rn->tn_val->v_ansiu) {
    553 		/* ANSI C treats constant as unsigned, op %s */
    554 		warning(218, mp->m_name);
    555 		rn->tn_val->v_ansiu = false;
    556 	}
    557 
    558 	/* Make sure both operands are of the same type */
    559 	if (mp->m_balance_operands || (tflag && (op == SHL || op == SHR)))
    560 		balance(op, &ln, &rn);
    561 
    562 	/*
    563 	 * Check types for compatibility with the operation and mutual
    564 	 * compatibility. Return if there are serious problems.
    565 	 */
    566 	if (!typeok(op, 0, ln, rn))
    567 		return NULL;
    568 
    569 	/* And now create the node. */
    570 	switch (op) {
    571 	case POINT:
    572 	case ARROW:
    573 		ntn = build_struct_access(op, ln, rn);
    574 		break;
    575 	case INCAFT:
    576 	case DECAFT:
    577 	case INCBEF:
    578 	case DECBEF:
    579 		ntn = build_prepost_incdec(op, ln);
    580 		break;
    581 	case ADDR:
    582 		ntn = build_address(ln, false);
    583 		break;
    584 	case INDIR:
    585 		ntn = new_tnode(INDIR, ln->tn_type->t_subt, ln, NULL);
    586 		break;
    587 	case PLUS:
    588 	case MINUS:
    589 		ntn = build_plus_minus(op, ln, rn);
    590 		break;
    591 	case SHL:
    592 	case SHR:
    593 		ntn = build_bit_shift(op, ln, rn);
    594 		break;
    595 	case COLON:
    596 		ntn = build_colon(ln, rn);
    597 		break;
    598 	case ASSIGN:
    599 	case MULASS:
    600 	case DIVASS:
    601 	case MODASS:
    602 	case ADDASS:
    603 	case SUBASS:
    604 	case SHLASS:
    605 	case SHRASS:
    606 	case ANDASS:
    607 	case XORASS:
    608 	case ORASS:
    609 	case RETURN:
    610 		ntn = build_assignment(op, ln, rn);
    611 		break;
    612 	case COMMA:
    613 	case QUEST:
    614 		ntn = new_tnode(op, rn->tn_type, ln, rn);
    615 		break;
    616 	case REAL:
    617 	case IMAG:
    618 		ntn = build_real_imag(op, ln);
    619 		break;
    620 	default:
    621 		rettp = mp->m_returns_bool
    622 		    ? gettyp(Tflag ? BOOL : INT) : ln->tn_type;
    623 		lint_assert(mp->m_binary || rn == NULL);
    624 		ntn = new_tnode(op, rettp, ln, rn);
    625 		break;
    626 	}
    627 
    628 	/* Return if an error occurred. */
    629 	if (ntn == NULL)
    630 		return NULL;
    631 
    632 	/* Print a warning if precedence confusion is possible */
    633 	if (mp->m_possible_precedence_confusion)
    634 		check_precedence_confusion(ntn);
    635 
    636 	/*
    637 	 * Print a warning if one of the operands is in a context where
    638 	 * it is compared with null and if this operand is a constant.
    639 	 */
    640 	if (mp->m_left_test_context) {
    641 		if (ln->tn_op == CON ||
    642 		    ((mp->m_binary && op != QUEST) && rn->tn_op == CON)) {
    643 			if (hflag && !constcond_flag &&
    644 			    !ln->tn_system_dependent)
    645 				/* constant in conditional context */
    646 				warning(161);
    647 		}
    648 	}
    649 
    650 	/* Fold if the operator requires it */
    651 	if (mp->m_fold_constant_operands) {
    652 		if (ln->tn_op == CON && (!mp->m_binary || rn->tn_op == CON)) {
    653 			if (mp->m_left_test_context) {
    654 				ntn = fold_test(ntn);
    655 			} else if (is_floating(ntn->tn_type->t_tspec)) {
    656 				ntn = fold_float(ntn);
    657 			} else {
    658 				ntn = fold(ntn);
    659 			}
    660 		} else if (op == QUEST && ln->tn_op == CON) {
    661 			ntn = ln->tn_val->v_quad != 0
    662 			    ? rn->tn_left : rn->tn_right;
    663 		}
    664 	}
    665 
    666 	return ntn;
    667 }
    668 
    669 /*
    670  * Perform class conversions.
    671  *
    672  * Arrays of type T are converted into pointers to type T.
    673  * Functions are converted to pointers to functions.
    674  * Lvalues are converted to rvalues.
    675  *
    676  * C99 6.3 "Conversions"
    677  * C99 6.3.2 "Other operands"
    678  * C99 6.3.2.1 "Lvalues, arrays, and function designators"
    679  */
    680 tnode_t *
    681 cconv(tnode_t *tn)
    682 {
    683 	type_t	*tp;
    684 
    685 	/*
    686 	 * Array-lvalue (array of type T) is converted into rvalue
    687 	 * (pointer to type T)
    688 	 */
    689 	if (tn->tn_type->t_tspec == ARRAY) {
    690 		if (!tn->tn_lvalue) {
    691 			/* XXX print correct operator */
    692 			/* %soperand of '%s' must be lvalue */
    693 			gnuism(114, "", modtab[ADDR].m_name);
    694 		}
    695 		tn = new_tnode(ADDR, tincref(tn->tn_type->t_subt, PTR),
    696 			     tn, NULL);
    697 	}
    698 
    699 	/*
    700 	 * Expression of type function (function with return value of type T)
    701 	 * in rvalue-expression (pointer to function with return value
    702 	 * of type T)
    703 	 */
    704 	if (tn->tn_type->t_tspec == FUNC)
    705 		tn = build_address(tn, true);
    706 
    707 	/* lvalue to rvalue */
    708 	if (tn->tn_lvalue) {
    709 		tp = tduptyp(tn->tn_type);
    710 		tp->t_const = tp->t_volatile = false;
    711 		tn = new_tnode(LOAD, tp, tn, NULL);
    712 	}
    713 
    714 	return tn;
    715 }
    716 
    717 static const tnode_t *
    718 before_conversion(const tnode_t *tn)
    719 {
    720 	while (tn->tn_op == CVT && !tn->tn_cast)
    721 		tn = tn->tn_left;
    722 	return tn;
    723 }
    724 
    725 static bool
    726 is_null_pointer(const tnode_t *tn)
    727 {
    728 	tspec_t t = tn->tn_type->t_tspec;
    729 
    730 	return ((t == PTR && tn->tn_type->t_subt->t_tspec == VOID) ||
    731 		is_integer(t))
    732 	       && (tn->tn_op == CON && tn->tn_val->v_quad == 0);
    733 }
    734 
    735 /*
    736  * See if the node is valid as operand of an operator that compares its
    737  * argument with 0.
    738  */
    739 bool
    740 is_typeok_bool_operand(const tnode_t *tn)
    741 {
    742 	tspec_t t;
    743 
    744 	lint_assert(Tflag);
    745 
    746 	tn = before_conversion(tn);
    747 	t = tn->tn_type->t_tspec;
    748 
    749 	if (t == BOOL)
    750 		return true;
    751 
    752 	if (tn->tn_from_system_header && is_scalar(t))
    753 		return true;
    754 
    755 	/* For enums that are used as bit sets, allow "flags & FLAG". */
    756 	if (tn->tn_op == BITAND &&
    757 	    tn->tn_left->tn_op == CVT &&
    758 	    tn->tn_left->tn_type->t_tspec == INT && !tn->tn_left->tn_cast &&
    759 	    tn->tn_left->tn_left->tn_type->t_tspec == ENUM &&
    760 	    /*
    761 	     * XXX: Somehow the type information got lost here.  The type
    762 	     * of the enum constant on the right-hand side should still be
    763 	     * ENUM, but is INT.
    764 	     */
    765 	    tn->tn_right->tn_type->t_tspec == INT)
    766 		return true;
    767 
    768 	return false;
    769 }
    770 
    771 static bool
    772 typeok_incdec(const mod_t *mp, const tnode_t *tn, const type_t *tp)
    773 {
    774 	/* operand has scalar type (checked in typeok) */
    775 	if (!tn->tn_lvalue) {
    776 		if (tn->tn_op == CVT && tn->tn_cast &&
    777 		    tn->tn_left->tn_op == LOAD) {
    778 			if (tn->tn_type->t_tspec == PTR)
    779 				return true;
    780 			/* a cast does not yield an lvalue */
    781 			error(163);
    782 		}
    783 		/* %soperand of '%s' must be lvalue */
    784 		error(114, "", mp->m_name);
    785 		return false;
    786 	} else if (tp->t_const) {
    787 		if (!tflag)
    788 			/* %soperand of '%s' must be modifiable lvalue */
    789 			warning(115, "", mp->m_name);
    790 	}
    791 	return true;
    792 }
    793 
    794 static bool
    795 typeok_address(const mod_t *mp,
    796 	    const tnode_t *tn, const type_t *tp, tspec_t t)
    797 {
    798 	if (t == ARRAY || t == FUNC) {
    799 		/* ok, a warning comes later (in build_address()) */
    800 	} else if (!tn->tn_lvalue) {
    801 		if (tn->tn_op == CVT && tn->tn_cast &&
    802 		    tn->tn_left->tn_op == LOAD) {
    803 			if (tn->tn_type->t_tspec == PTR)
    804 				return true;
    805 			/* a cast does not yield an lvalue */
    806 			error(163);
    807 		}
    808 		/* %soperand of '%s' must be lvalue */
    809 		error(114, "", mp->m_name);
    810 		return false;
    811 	} else if (is_scalar(t)) {
    812 		if (tp->t_bitfield) {
    813 			/* cannot take address of bit-field */
    814 			error(112);
    815 			return false;
    816 		}
    817 	} else if (t != STRUCT && t != UNION) {
    818 		/* unacceptable operand of '%s' */
    819 		error(111, mp->m_name);
    820 		return false;
    821 	}
    822 	if (tn->tn_op == NAME && tn->tn_sym->s_reg) {
    823 		/* cannot take address of register %s */
    824 		error(113, tn->tn_sym->s_name);
    825 		return false;
    826 	}
    827 	return true;
    828 }
    829 
    830 static bool
    831 typeok_star(tspec_t t)
    832 {
    833 	/* until now there were no type checks for this operator */
    834 	if (t != PTR) {
    835 		/* cannot dereference non-pointer type */
    836 		error(96);
    837 		return false;
    838 	}
    839 	return true;
    840 }
    841 
    842 static bool
    843 typeok_plus(op_t op, tspec_t lt, tspec_t rt)
    844 {
    845 	/* operands have scalar types (checked above) */
    846 	if ((lt == PTR && !is_integer(rt)) || (rt == PTR && !is_integer(lt))) {
    847 		warn_incompatible_types(op, lt, rt);
    848 		return false;
    849 	}
    850 	return true;
    851 }
    852 
    853 static bool
    854 typeok_minus(op_t op,
    855 	     const type_t *ltp, tspec_t lt,
    856 	     const type_t *rtp, tspec_t rt)
    857 {
    858 	/* operands have scalar types (checked above) */
    859 	if (lt == PTR && (!is_integer(rt) && rt != PTR)) {
    860 		warn_incompatible_types(op, lt, rt);
    861 		return false;
    862 	} else if (rt == PTR && lt != PTR) {
    863 		warn_incompatible_types(op, lt, rt);
    864 		return false;
    865 	}
    866 	if (lt == PTR && rt == PTR) {
    867 		if (!eqtype(ltp->t_subt, rtp->t_subt, true, false, NULL)) {
    868 			/* illegal pointer subtraction */
    869 			error(116);
    870 		}
    871 	}
    872 	return true;
    873 }
    874 
    875 static void
    876 typeok_shr(const mod_t *mp,
    877 	   const tnode_t *ln, tspec_t lt,
    878 	   const tnode_t *rn, tspec_t rt)
    879 {
    880 	tspec_t olt, ort;
    881 
    882 	olt = before_conversion(ln)->tn_type->t_tspec;
    883 	ort = before_conversion(rn)->tn_type->t_tspec;
    884 
    885 	/* operands have integer types (checked above) */
    886 	if (pflag && !is_uinteger(lt)) {
    887 		/*
    888 		 * The left operand is signed. This means that
    889 		 * the operation is (possibly) nonportable.
    890 		 */
    891 		if (ln->tn_op != CON) {
    892 			/* bitwise '%s' on signed value possibly nonportable */
    893 			warning(117, mp->m_name);
    894 		} else if (ln->tn_val->v_quad < 0) {
    895 			/* bitwise '%s' on signed value nonportable */
    896 			warning(120, mp->m_name);
    897 		}
    898 	} else if (!tflag && !sflag && !is_uinteger(olt) && is_uinteger(ort)) {
    899 		/*
    900 		 * The left operand would become unsigned in
    901 		 * traditional C.
    902 		 */
    903 		if (hflag &&
    904 		    (ln->tn_op != CON || ln->tn_val->v_quad < 0)) {
    905 			/* semantics of '%s' change in ANSI C; use ... */
    906 			warning(118, mp->m_name);
    907 		}
    908 	} else if (!tflag && !sflag && !is_uinteger(olt) && !is_uinteger(ort) &&
    909 		   portable_size_in_bits(lt) < portable_size_in_bits(rt)) {
    910 		/*
    911 		 * In traditional C the left operand would be extended,
    912 		 * possibly with 1, and then shifted.
    913 		 */
    914 		if (hflag &&
    915 		    (ln->tn_op != CON || ln->tn_val->v_quad < 0)) {
    916 			/* semantics of '%s' change in ANSI C; use ... */
    917 			warning(118, mp->m_name);
    918 		}
    919 	}
    920 }
    921 
    922 static void
    923 typeok_shl(const mod_t *mp, tspec_t lt, tspec_t rt)
    924 {
    925 	/*
    926 	 * C90 does not perform balancing for shift operations,
    927 	 * but traditional C does. If the width of the right operand
    928 	 * is greater than the width of the left operand, then in
    929 	 * traditional C the left operand would be extended to the
    930 	 * width of the right operand. For SHL this may result in
    931 	 * different results.
    932 	 */
    933 	if (portable_size_in_bits(lt) < portable_size_in_bits(rt)) {
    934 		/*
    935 		 * XXX If both operands are constant, make sure
    936 		 * that there is really a difference between
    937 		 * ANSI C and traditional C.
    938 		 */
    939 		if (hflag)
    940 			/* semantics of '%s' change in ANSI C; use ... */
    941 			warning(118, mp->m_name);
    942 	}
    943 }
    944 
    945 static void
    946 typeok_shift(tspec_t lt, const tnode_t *rn, tspec_t rt)
    947 {
    948 	if (rn->tn_op == CON) {
    949 		if (!is_uinteger(rt) && rn->tn_val->v_quad < 0) {
    950 			/* negative shift */
    951 			warning(121);
    952 		} else if ((uint64_t)rn->tn_val->v_quad ==
    953 			   (uint64_t)size_in_bits(lt)) {
    954 			/* shift equal to size of object */
    955 			warning(267);
    956 		} else if ((uint64_t)rn->tn_val->v_quad >
    957 			   (uint64_t)size_in_bits(lt)) {
    958 			/* shift greater than size of object */
    959 			warning(122);
    960 		}
    961 	}
    962 }
    963 
    964 static bool
    965 is_typeok_eq(const tnode_t *ln, tspec_t lt, const tnode_t *rn, tspec_t rt)
    966 {
    967 	if (lt == PTR && is_null_pointer(rn))
    968 		return true;
    969 	if (rt == PTR && is_null_pointer(ln))
    970 		return true;
    971 	return false;
    972 }
    973 
    974 static bool
    975 typeok_ordered_comparison(op_t op,
    976 			  const tnode_t *ln, const type_t *ltp, tspec_t lt,
    977 			  const tnode_t *rn, const type_t *rtp, tspec_t rt)
    978 {
    979 	if (lt == PTR && rt == PTR) {
    980 		check_pointer_comparison(op, ln, rn);
    981 		return true;
    982 	}
    983 
    984 	if (lt != PTR && rt != PTR)
    985 		return true;
    986 
    987 	if (!is_integer(lt) && !is_integer(rt)) {
    988 		warn_incompatible_types(op, lt, rt);
    989 		return false;
    990 	}
    991 
    992 	const char *lx = lt == PTR ? "pointer" : "integer";
    993 	const char *rx = rt == PTR ? "pointer" : "integer";
    994 	/* illegal combination of %s (%s) and %s (%s), op %s */
    995 	warning(123, lx, type_name(ltp), rx, type_name(rtp), getopname(op));
    996 	return true;
    997 }
    998 
    999 static bool
   1000 typeok_quest(tspec_t lt, const tnode_t **rn)
   1001 {
   1002 	if (!is_scalar(lt)) {
   1003 		/* first operand must have scalar type, op ? : */
   1004 		error(170);
   1005 		return false;
   1006 	}
   1007 	while ((*rn)->tn_op == CVT)
   1008 		*rn = (*rn)->tn_left;
   1009 	lint_assert((*rn)->tn_op == COLON);
   1010 	return true;
   1011 }
   1012 
   1013 static void
   1014 typeok_colon_pointer(const mod_t *mp, const type_t *ltp, const type_t *rtp)
   1015 {
   1016 	type_t *lstp = ltp->t_subt;
   1017 	type_t *rstp = rtp->t_subt;
   1018 	tspec_t lst = lstp->t_tspec;
   1019 	tspec_t rst = rstp->t_tspec;
   1020 
   1021 	if ((lst == VOID && rst == FUNC) || (lst == FUNC && rst == VOID)) {
   1022 		/* (void *)0 handled above */
   1023 		if (sflag)
   1024 			/* ANSI C forbids conv. of %s to %s, op %s */
   1025 			warning(305, "function pointer", "'void *'",
   1026 			    mp->m_name);
   1027 		return;
   1028 	}
   1029 
   1030 	if (eqptrtype(lstp, rstp, true))
   1031 		return;
   1032 	if (!eqtype(lstp, rstp, true, false, NULL))
   1033 		warn_incompatible_pointers(mp, ltp, rtp);
   1034 }
   1035 
   1036 static bool
   1037 typeok_colon(const mod_t *mp,
   1038 	     const tnode_t *ln, const type_t *ltp, tspec_t lt,
   1039 	     const tnode_t *rn, const type_t *rtp, tspec_t rt)
   1040 {
   1041 
   1042 	if (is_arithmetic(lt) && is_arithmetic(rt))
   1043 		return true;
   1044 	if (lt == BOOL && rt == BOOL)
   1045 		return true;
   1046 
   1047 	if (lt == STRUCT && rt == STRUCT && ltp->t_str == rtp->t_str)
   1048 		return true;
   1049 	if (lt == UNION && rt == UNION && ltp->t_str == rtp->t_str)
   1050 		return true;
   1051 
   1052 	if (lt == PTR && is_null_pointer(rn))
   1053 		return true;
   1054 	if (rt == PTR && is_null_pointer(ln))
   1055 		return true;
   1056 
   1057 	if ((lt == PTR && is_integer(rt)) || (is_integer(lt) && rt == PTR)) {
   1058 		const char *lx = lt == PTR ?  "pointer" : "integer";
   1059 		const char *rx = rt == PTR ?  "pointer" : "integer";
   1060 		/* illegal combination of %s (%s) and %s (%s), op %s */
   1061 		warning(123, lx, type_name(ltp),
   1062 		    rx, type_name(rtp), mp->m_name);
   1063 		return true;
   1064 	}
   1065 
   1066 	if (lt == VOID || rt == VOID) {
   1067 		if (lt != VOID || rt != VOID)
   1068 			/* incompatible types '%s' and '%s' in conditional */
   1069 			warning(126, type_name(ltp), type_name(rtp));
   1070 		return true;
   1071 	}
   1072 
   1073 	if (lt == PTR && rt == PTR) {
   1074 		typeok_colon_pointer(mp, ltp, rtp);
   1075 		return true;
   1076 	}
   1077 
   1078 	/* incompatible types '%s' and '%s' in conditional */
   1079 	error(126, type_name(ltp), type_name(rtp));
   1080 	return false;
   1081 }
   1082 
   1083 static bool
   1084 typeok_assign(const mod_t *mp, const tnode_t *ln, const type_t *ltp, tspec_t lt)
   1085 {
   1086 	if (!ln->tn_lvalue) {
   1087 		if (ln->tn_op == CVT && ln->tn_cast &&
   1088 		    ln->tn_left->tn_op == LOAD) {
   1089 			if (ln->tn_type->t_tspec == PTR)
   1090 				return true;
   1091 			/* a cast does not yield an lvalue */
   1092 			error(163);
   1093 		}
   1094 		/* %soperand of '%s' must be lvalue */
   1095 		error(114, "left ", mp->m_name);
   1096 		return false;
   1097 	} else if (ltp->t_const || ((lt == STRUCT || lt == UNION) &&
   1098 				    has_constant_member(ltp))) {
   1099 		if (!tflag)
   1100 			/* %soperand of '%s' must be modifiable lvalue */
   1101 			warning(115, "left ", mp->m_name);
   1102 	}
   1103 	return true;
   1104 }
   1105 
   1106 /*
   1107  * See if in strict bool mode, the operator takes either two bool operands
   1108  * or two arbitrary other operands.
   1109  */
   1110 static bool
   1111 is_assignment_bool_or_other(op_t op)
   1112 {
   1113 	return op == ASSIGN ||
   1114 	       op == ANDASS || op == XORASS || op == ORASS ||
   1115 	       op == RETURN || op == FARG;
   1116 }
   1117 
   1118 static bool
   1119 is_symmetric_bool_or_other(op_t op)
   1120 {
   1121 	return op == EQ || op == NE ||
   1122 	       op == BITAND || op == BITXOR || op == BITOR ||
   1123 	       op == COLON;
   1124 }
   1125 
   1126 static bool
   1127 is_int_constant_zero(const tnode_t *tn, tspec_t t)
   1128 {
   1129 	return t == INT && tn->tn_op == CON && tn->tn_val->v_quad == 0;
   1130 }
   1131 
   1132 static bool
   1133 is_typeok_strict_bool(op_t op,
   1134 		      const tnode_t *ln, tspec_t lt,
   1135 		      const tnode_t *rn, tspec_t rt)
   1136 {
   1137 	if (rn == NULL)
   1138 		return true;	/* TODO: check unary operators as well. */
   1139 
   1140 	if ((lt == BOOL) == (rt == BOOL))
   1141 		return true;
   1142 
   1143 	if ((ln->tn_from_system_header || rn->tn_from_system_header) &&
   1144 	    (is_int_constant_zero(ln, lt) || is_int_constant_zero(rn, rt)))
   1145 		return true;
   1146 
   1147 	if (is_assignment_bool_or_other(op)) {
   1148 		return lt != BOOL &&
   1149 		       (ln->tn_from_system_header || rn->tn_from_system_header);
   1150 	}
   1151 
   1152 	return !is_symmetric_bool_or_other(op);
   1153 }
   1154 
   1155 /*
   1156  * Some operators require that either both operands are bool or both are
   1157  * scalar.
   1158  *
   1159  * Code that passes this check can be compiled in a pre-C99 environment that
   1160  * doesn't implement the special rule C99 6.3.1.2, without silent change in
   1161  * behavior.
   1162  */
   1163 static bool
   1164 typeok_strict_bool_compatible(op_t op, int arg,
   1165 			      const tnode_t *ln, tspec_t lt,
   1166 			      const tnode_t *rn, tspec_t rt)
   1167 {
   1168 
   1169 	if (is_typeok_strict_bool(op, ln, lt, rn, rt))
   1170 		return true;
   1171 
   1172 	if (op == FARG) {
   1173 		/* argument #%d expects '%s', gets passed '%s' */
   1174 		error(334, arg, tspec_name(lt), tspec_name(rt));
   1175 	} else if (op == RETURN) {
   1176 		/* return value type mismatch (%s) and (%s) */
   1177 		error(211, tspec_name(lt), tspec_name(rt));
   1178 	} else {
   1179 		/* operands of '%s' have incompatible types (%s != %s) */
   1180 		error(107, getopname(op), tspec_name(lt), tspec_name(rt));
   1181 	}
   1182 
   1183 	return false;
   1184 }
   1185 
   1186 /*
   1187  * In strict bool mode, check whether the types of the operands match the
   1188  * operator.
   1189  */
   1190 static bool
   1191 typeok_scalar_strict_bool(op_t op, const mod_t *mp, int arg,
   1192 			  const tnode_t *ln,
   1193 			  const tnode_t *rn)
   1194 
   1195 {
   1196 	tspec_t lt, rt;
   1197 
   1198 	ln = before_conversion(ln);
   1199 	lt = ln->tn_type->t_tspec;
   1200 
   1201 	if (rn != NULL) {
   1202 		rn = before_conversion(rn);
   1203 		rt = rn->tn_type->t_tspec;
   1204 	} else {
   1205 		rt = NOTSPEC;
   1206 	}
   1207 
   1208 	if (!typeok_strict_bool_compatible(op, arg, ln, lt, rn, rt))
   1209 		return false;
   1210 
   1211 	if (mp->m_requires_bool || op == QUEST) {
   1212 		bool binary = mp->m_binary;
   1213 		bool lbool = is_typeok_bool_operand(ln);
   1214 		bool ok = true;
   1215 
   1216 		if (!binary && !lbool) {
   1217 			/* operand of '%s' must be bool, not '%s' */
   1218 			error(330, getopname(op), tspec_name(lt));
   1219 			ok = false;
   1220 		}
   1221 		if (binary && !lbool) {
   1222 			/* left operand of '%s' must be bool, not '%s' */
   1223 			error(331, getopname(op), tspec_name(lt));
   1224 			ok = false;
   1225 		}
   1226 		if (binary && op != QUEST && !is_typeok_bool_operand(rn)) {
   1227 			/* right operand of '%s' must be bool, not '%s' */
   1228 			error(332, getopname(op), tspec_name(rt));
   1229 			ok = false;
   1230 		}
   1231 		return ok;
   1232 	}
   1233 
   1234 	if (!mp->m_takes_bool) {
   1235 		bool binary = mp->m_binary;
   1236 		bool lbool = ln->tn_type->t_tspec == BOOL;
   1237 		bool ok = true;
   1238 
   1239 		if (!binary && lbool) {
   1240 			/* operand of '%s' must not be bool */
   1241 			error(335, getopname(op));
   1242 			ok = false;
   1243 		}
   1244 		if (binary && lbool) {
   1245 			/* left operand of '%s' must not be bool */
   1246 			error(336, getopname(op));
   1247 			ok = false;
   1248 		}
   1249 		if (binary && rn->tn_type->t_tspec == BOOL) {
   1250 			/* right operand of '%s' must not be bool */
   1251 			error(337, getopname(op));
   1252 			ok = false;
   1253 		}
   1254 		return ok;
   1255 	}
   1256 
   1257 	return true;
   1258 }
   1259 
   1260 /* Check the types using the information from modtab[]. */
   1261 static bool
   1262 typeok_scalar(op_t op, const mod_t *mp, tspec_t lt, tspec_t rt)
   1263 {
   1264 	if (mp->m_takes_bool && lt == BOOL && rt == BOOL)
   1265 		return true;
   1266 	if (mp->m_requires_integer) {
   1267 		if (!is_integer(lt) || (mp->m_binary && !is_integer(rt))) {
   1268 			warn_incompatible_types(op, lt, rt);
   1269 			return false;
   1270 		}
   1271 	} else if (mp->m_requires_integer_or_complex) {
   1272 		if ((!is_integer(lt) && !is_complex(lt)) ||
   1273 		    (mp->m_binary && (!is_integer(rt) && !is_complex(rt)))) {
   1274 			warn_incompatible_types(op, lt, rt);
   1275 			return false;
   1276 		}
   1277 	} else if (mp->m_requires_scalar) {
   1278 		if (!is_scalar(lt) || (mp->m_binary && !is_scalar(rt))) {
   1279 			warn_incompatible_types(op, lt, rt);
   1280 			return false;
   1281 		}
   1282 	} else if (mp->m_requires_arith) {
   1283 		if (!is_arithmetic(lt) ||
   1284 		    (mp->m_binary && !is_arithmetic(rt))) {
   1285 			warn_incompatible_types(op, lt, rt);
   1286 			return false;
   1287 		}
   1288 	}
   1289 	return true;
   1290 }
   1291 
   1292 /* Check the types for specific operators and type combinations. */
   1293 static bool
   1294 typeok_op(op_t op, const mod_t *mp, int arg,
   1295 	  const tnode_t *ln, const type_t *ltp, tspec_t lt,
   1296 	  const tnode_t *rn, const type_t *rtp, tspec_t rt)
   1297 {
   1298 	switch (op) {
   1299 	case POINT:
   1300 		/*
   1301 		 * Most errors required by ANSI C are reported in
   1302 		 * struct_or_union_member().
   1303 		 * Here we only must check for totally wrong things.
   1304 		 */
   1305 		if (lt == FUNC || lt == VOID || ltp->t_bitfield ||
   1306 		    ((lt != STRUCT && lt != UNION) && !ln->tn_lvalue)) {
   1307 			/* Without tflag we got already an error */
   1308 			if (tflag)
   1309 				/* unacceptable operand of '%s' */
   1310 				error(111, mp->m_name);
   1311 			return false;
   1312 		}
   1313 		/* Now we have an object we can create a pointer to */
   1314 		break;
   1315 	case ARROW:
   1316 		if (lt != PTR && !(tflag && is_integer(lt))) {
   1317 			/* Without tflag we got already an error */
   1318 			if (tflag)
   1319 				/* unacceptable operand of '%s' */
   1320 				error(111, mp->m_name);
   1321 			return false;
   1322 		}
   1323 		break;
   1324 	case INCAFT:
   1325 	case DECAFT:
   1326 	case INCBEF:
   1327 	case DECBEF:
   1328 		if (!typeok_incdec(mp, ln, ltp))
   1329 			return false;
   1330 		break;
   1331 	case ADDR:
   1332 		if (!typeok_address(mp, ln, ltp, lt))
   1333 			return false;
   1334 		break;
   1335 	case INDIR:
   1336 		if (!typeok_star(lt))
   1337 			return false;
   1338 		break;
   1339 	case PLUS:
   1340 		if (!typeok_plus(op, lt, rt))
   1341 			return false;
   1342 		break;
   1343 	case MINUS:
   1344 		if (!typeok_minus(op, ltp, lt, rtp, rt))
   1345 			return false;
   1346 		break;
   1347 	case SHR:
   1348 		typeok_shr(mp, ln, lt, rn, rt);
   1349 		goto shift;
   1350 	case SHL:
   1351 		typeok_shl(mp, lt, rt);
   1352 	shift:
   1353 		typeok_shift(lt, rn, rt);
   1354 		break;
   1355 	case EQ:
   1356 	case NE:
   1357 		/*
   1358 		 * Accept some things which are allowed with EQ and NE,
   1359 		 * but not with ordered comparisons.
   1360 		 */
   1361 		if (is_typeok_eq(ln, lt, rn, rt))
   1362 			break;
   1363 		/* FALLTHROUGH */
   1364 	case LT:
   1365 	case GT:
   1366 	case LE:
   1367 	case GE:
   1368 		if (!typeok_ordered_comparison(op, ln, ltp, lt, rn, rtp, rt))
   1369 			return false;
   1370 		break;
   1371 	case QUEST:
   1372 		if (!typeok_quest(lt, &rn))
   1373 			return false;
   1374 		break;
   1375 	case COLON:
   1376 		if (!typeok_colon(mp, ln, ltp, lt, rn, rtp, rt))
   1377 			return false;
   1378 		break;
   1379 	case ASSIGN:
   1380 	case INIT:
   1381 	case FARG:
   1382 	case RETURN:
   1383 		if (!check_assign_types_compatible(op, arg, ln, rn))
   1384 			return false;
   1385 		goto assign;
   1386 	case MULASS:
   1387 	case DIVASS:
   1388 	case MODASS:
   1389 		goto assign;
   1390 	case ADDASS:
   1391 	case SUBASS:
   1392 		/* operands have scalar types (checked above) */
   1393 		if ((lt == PTR && !is_integer(rt)) || rt == PTR) {
   1394 			warn_incompatible_types(op, lt, rt);
   1395 			return false;
   1396 		}
   1397 		goto assign;
   1398 	case SHLASS:
   1399 		goto assign;
   1400 	case SHRASS:
   1401 		if (pflag && !is_uinteger(lt) && !(tflag && is_uinteger(rt))) {
   1402 			/* bitwise '%s' on signed value possibly nonportable */
   1403 			warning(117, mp->m_name);
   1404 		}
   1405 		goto assign;
   1406 	case ANDASS:
   1407 	case XORASS:
   1408 	case ORASS:
   1409 		goto assign;
   1410 	assign:
   1411 		if (!typeok_assign(mp, ln, ltp, lt))
   1412 			return false;
   1413 		break;
   1414 	case COMMA:
   1415 		if (!modtab[ln->tn_op].m_has_side_effect)
   1416 			check_null_effect(ln);
   1417 		break;
   1418 		/* LINTED206: (enumeration values not handled in switch) */
   1419 	case CON:
   1420 	case CASE:
   1421 	case PUSH:
   1422 	case LOAD:
   1423 	case ICALL:
   1424 	case CVT:
   1425 	case CALL:
   1426 	case FSEL:
   1427 	case STRING:
   1428 	case NAME:
   1429 	case LOGOR:
   1430 	case LOGAND:
   1431 	case BITOR:
   1432 	case BITXOR:
   1433 	case BITAND:
   1434 	case MOD:
   1435 	case DIV:
   1436 	case MULT:
   1437 	case UMINUS:
   1438 	case UPLUS:
   1439 	case DEC:
   1440 	case INC:
   1441 	case COMPL:
   1442 	case NOT:
   1443 	case NOOP:
   1444 	case REAL:
   1445 	case IMAG:
   1446 		break;
   1447 	}
   1448 	return true;
   1449 }
   1450 
   1451 static void
   1452 typeok_enum(op_t op, const mod_t *mp, int arg,
   1453 	    const tnode_t *ln, const type_t *ltp,
   1454 	    const tnode_t *rn, const type_t *rtp)
   1455 {
   1456 	if (mp->m_bad_on_enum &&
   1457 	    (ltp->t_is_enum || (mp->m_binary && rtp->t_is_enum))) {
   1458 		check_bad_enum_operation(op, ln, rn);
   1459 	} else if (mp->m_valid_on_enum &&
   1460 		   (ltp->t_is_enum && rtp != NULL && rtp->t_is_enum)) {
   1461 		check_enum_type_mismatch(op, arg, ln, rn);
   1462 	} else if (mp->m_valid_on_enum &&
   1463 		   (ltp->t_is_enum || (rtp != NULL && rtp->t_is_enum))) {
   1464 		check_enum_int_mismatch(op, arg, ln, rn);
   1465 	}
   1466 }
   1467 
   1468 /* Perform most type checks. Return whether the types are ok. */
   1469 bool
   1470 typeok(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
   1471 {
   1472 	mod_t	*mp;
   1473 	tspec_t	lt, rt;
   1474 	type_t	*ltp, *rtp;
   1475 
   1476 	mp = &modtab[op];
   1477 
   1478 	lint_assert((ltp = ln->tn_type) != NULL);
   1479 	lt = ltp->t_tspec;
   1480 
   1481 	if (mp->m_binary) {
   1482 		lint_assert((rtp = rn->tn_type) != NULL);
   1483 		rt = rtp->t_tspec;
   1484 	} else {
   1485 		rtp = NULL;
   1486 		rt = NOTSPEC;
   1487 	}
   1488 
   1489 	if (Tflag && !typeok_scalar_strict_bool(op, mp, arg, ln, rn))
   1490 		return false;
   1491 	if (!typeok_scalar(op, mp, lt, rt))
   1492 		return false;
   1493 
   1494 	if (!typeok_op(op, mp, arg, ln, ltp, lt, rn, rtp, rt))
   1495 		return false;
   1496 
   1497 	typeok_enum(op, mp, arg, ln, ltp, rn, rtp);
   1498 	return true;
   1499 }
   1500 
   1501 static void
   1502 check_pointer_comparison(op_t op, const tnode_t *ln, const tnode_t *rn)
   1503 {
   1504 	type_t	*ltp, *rtp;
   1505 	tspec_t	lst, rst;
   1506 	const	char *lsts, *rsts;
   1507 
   1508 	lst = (ltp = ln->tn_type)->t_subt->t_tspec;
   1509 	rst = (rtp = rn->tn_type)->t_subt->t_tspec;
   1510 
   1511 	if (lst == VOID || rst == VOID) {
   1512 		if (sflag && (lst == FUNC || rst == FUNC)) {
   1513 			/* (void *)0 already handled in typeok() */
   1514 			*(lst == FUNC ? &lsts : &rsts) = "function pointer";
   1515 			*(lst == VOID ? &lsts : &rsts) = "'void *'";
   1516 			/* ANSI C forbids comparison of %s with %s */
   1517 			warning(274, lsts, rsts);
   1518 		}
   1519 		return;
   1520 	}
   1521 
   1522 	if (!eqtype(ltp->t_subt, rtp->t_subt, true, false, NULL)) {
   1523 		warn_incompatible_pointers(&modtab[op], ltp, rtp);
   1524 		return;
   1525 	}
   1526 
   1527 	if (lst == FUNC && rst == FUNC) {
   1528 		if (sflag && op != EQ && op != NE)
   1529 			/* ANSI C forbids ordered comparisons of ... */
   1530 			warning(125);
   1531 	}
   1532 }
   1533 
   1534 /*
   1535  * Checks type compatibility for ASSIGN, INIT, FARG and RETURN
   1536  * and prints warnings/errors if necessary.
   1537  * If the types are (almost) compatible, 1 is returned, otherwise 0.
   1538  */
   1539 static bool
   1540 check_assign_types_compatible(op_t op, int arg,
   1541 			      const tnode_t *ln, const tnode_t *rn)
   1542 {
   1543 	tspec_t	lt, rt, lst = NOTSPEC, rst = NOTSPEC;
   1544 	type_t	*ltp, *rtp, *lstp = NULL, *rstp = NULL;
   1545 	mod_t	*mp;
   1546 	const	char *lts, *rts;
   1547 
   1548 	if ((lt = (ltp = ln->tn_type)->t_tspec) == PTR)
   1549 		lst = (lstp = ltp->t_subt)->t_tspec;
   1550 	if ((rt = (rtp = rn->tn_type)->t_tspec) == PTR)
   1551 		rst = (rstp = rtp->t_subt)->t_tspec;
   1552 	mp = &modtab[op];
   1553 
   1554 	if (lt == BOOL && is_scalar(rt))	/* C99 6.3.1.2 */
   1555 		return true;
   1556 
   1557 	if (is_arithmetic(lt) && (is_arithmetic(rt) || rt == BOOL))
   1558 		return true;
   1559 
   1560 	if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION))
   1561 		/* both are struct or union */
   1562 		return ltp->t_str == rtp->t_str;
   1563 
   1564 	/* a null pointer may be assigned to any pointer */
   1565 	if (lt == PTR && is_null_pointer(rn))
   1566 		return true;
   1567 
   1568 	if (lt == PTR && rt == PTR && (lst == VOID || rst == VOID)) {
   1569 		/* two pointers, at least one pointer to void */
   1570 		if (sflag && (lst == FUNC || rst == FUNC)) {
   1571 			/* comb. of ptr to func and ptr to void */
   1572 			*(lst == FUNC ? &lts : &rts) = "function pointer";
   1573 			*(lst == VOID ? &lts : &rts) = "'void *'";
   1574 			switch (op) {
   1575 			case INIT:
   1576 			case RETURN:
   1577 				/* ANSI C forbids conversion of %s to %s */
   1578 				warning(303, rts, lts);
   1579 				break;
   1580 			case FARG:
   1581 				/* ANSI C forbids conv. of %s to %s, arg #%d */
   1582 				warning(304, rts, lts, arg);
   1583 				break;
   1584 			default:
   1585 				/* ANSI C forbids conv. of %s to %s, op %s */
   1586 				warning(305, rts, lts, mp->m_name);
   1587 				break;
   1588 			}
   1589 		}
   1590 	}
   1591 
   1592 	if (lt == PTR && rt == PTR && (lst == VOID || rst == VOID ||
   1593 				       eqtype(lstp, rstp, true, false, NULL))) {
   1594 		/* compatible pointer types (qualifiers ignored) */
   1595 		if (!tflag &&
   1596 		    ((!lstp->t_const && rstp->t_const) ||
   1597 		     (!lstp->t_volatile && rstp->t_volatile))) {
   1598 			/* left side has not all qualifiers of right */
   1599 			switch (op) {
   1600 			case INIT:
   1601 			case RETURN:
   1602 				/* incompatible pointer types (%s != %s) */
   1603 				warning(182, type_name(lstp), type_name(rstp));
   1604 				break;
   1605 			case FARG:
   1606 				/* converting '%s' to incompatible '%s' ... */
   1607 				warning(153,
   1608 				    type_name(rtp), type_name(ltp), arg);
   1609 				break;
   1610 			default:
   1611 				/* operands have incompatible pointer type... */
   1612 				warning(128, mp->m_name,
   1613 				    type_name(lstp), type_name(rstp));
   1614 				break;
   1615 			}
   1616 		}
   1617 		return true;
   1618 	}
   1619 
   1620 	if ((lt == PTR && is_integer(rt)) || (is_integer(lt) && rt == PTR)) {
   1621 		const char *lx = lt == PTR ? "pointer" : "integer";
   1622 		const char *rx = rt == PTR ? "pointer" : "integer";
   1623 
   1624 		switch (op) {
   1625 		case INIT:
   1626 		case RETURN:
   1627 			/* illegal combination of %s (%s) and %s (%s) */
   1628 			warning(183, lx, type_name(ltp), rx, type_name(rtp));
   1629 			break;
   1630 		case FARG:
   1631 			/* illegal comb. of %s (%s) and %s (%s), arg #%d */
   1632 			warning(154,
   1633 			    lx, type_name(ltp), rx, type_name(rtp), arg);
   1634 			break;
   1635 		default:
   1636 			/* illegal combination of %s (%s) and %s (%s), op %s */
   1637 			warning(123,
   1638 			    lx, type_name(ltp), rx, type_name(rtp), mp->m_name);
   1639 			break;
   1640 		}
   1641 		return true;
   1642 	}
   1643 
   1644 	if (lt == PTR && rt == PTR) {
   1645 		switch (op) {
   1646 		case INIT:
   1647 		case RETURN:
   1648 			warn_incompatible_pointers(NULL, ltp, rtp);
   1649 			break;
   1650 		case FARG:
   1651 			/* converting '%s' to incompatible '%s' for ... */
   1652 			warning(153, type_name(rtp), type_name(ltp), arg);
   1653 			break;
   1654 		default:
   1655 			warn_incompatible_pointers(mp, ltp, rtp);
   1656 			break;
   1657 		}
   1658 		return true;
   1659 	}
   1660 
   1661 	switch (op) {
   1662 	case INIT:
   1663 		/* cannot initialize '%s' from '%s' */
   1664 		error(185, type_name(ltp), type_name(rtp));
   1665 		break;
   1666 	case RETURN:
   1667 		/* return value type mismatch (%s) and (%s) */
   1668 		error(211, type_name(ltp), type_name(rtp));
   1669 		break;
   1670 	case FARG:
   1671 		/* argument is incompatible with prototype, arg #%d */
   1672 		warning(155, arg);
   1673 		break;
   1674 	default:
   1675 		warn_incompatible_types(op, lt, rt);
   1676 		break;
   1677 	}
   1678 
   1679 	return false;
   1680 }
   1681 
   1682 /* Prints a warning if a strange operator is used on an enum type. */
   1683 static void
   1684 check_bad_enum_operation(op_t op, const tnode_t *ln, const tnode_t *rn)
   1685 {
   1686 	mod_t	*mp;
   1687 
   1688 	if (!eflag)
   1689 		return;
   1690 
   1691 	mp = &modtab[op];
   1692 
   1693 	if (!(ln->tn_type->t_is_enum ||
   1694 	      (mp->m_binary && rn->tn_type->t_is_enum))) {
   1695 		return;
   1696 	}
   1697 
   1698 	/*
   1699 	 * Enum as offset to a pointer is an exception (otherwise enums
   1700 	 * could not be used as array indices).
   1701 	 */
   1702 	if (op == PLUS &&
   1703 	    ((ln->tn_type->t_is_enum && rn->tn_type->t_tspec == PTR) ||
   1704 	     (rn->tn_type->t_is_enum && ln->tn_type->t_tspec == PTR))) {
   1705 		return;
   1706 	}
   1707 
   1708 	/* dubious operation on enum, op %s */
   1709 	warning(241, mp->m_name);
   1710 
   1711 }
   1712 
   1713 /*
   1714  * Prints a warning if an operator is applied to two different enum types.
   1715  */
   1716 static void
   1717 check_enum_type_mismatch(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
   1718 {
   1719 	mod_t	*mp;
   1720 
   1721 	mp = &modtab[op];
   1722 
   1723 	if (ln->tn_type->t_enum != rn->tn_type->t_enum) {
   1724 		switch (op) {
   1725 		case INIT:
   1726 			/* enum type mismatch between '%s' and '%s' in ... */
   1727 			warning(210,
   1728 			    type_name(ln->tn_type), type_name(rn->tn_type));
   1729 			break;
   1730 		case FARG:
   1731 			/* enum type mismatch, arg #%d (%s != %s) */
   1732 			warning(156, arg,
   1733 			    type_name(ln->tn_type), type_name(rn->tn_type));
   1734 			break;
   1735 		case RETURN:
   1736 			/* return value type mismatch (%s) and (%s) */
   1737 			warning(211,
   1738 			    type_name(ln->tn_type), type_name(rn->tn_type));
   1739 			break;
   1740 		default:
   1741 			/* enum type mismatch: '%s' '%s' '%s' */
   1742 			warning(130, type_name(ln->tn_type), mp->m_name,
   1743 			    type_name(rn->tn_type));
   1744 			break;
   1745 		}
   1746 	} else if (Pflag && mp->m_comparison && op != EQ && op != NE) {
   1747 		if (eflag)
   1748 			/* dubious comparison of enums, op %s */
   1749 			warning(243, mp->m_name);
   1750 	}
   1751 }
   1752 
   1753 /* Prints a warning if the operands mix between enum and integer. */
   1754 static void
   1755 check_enum_int_mismatch(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
   1756 {
   1757 
   1758 	if (!eflag)
   1759 		return;
   1760 
   1761 	switch (op) {
   1762 	case INIT:
   1763 		/*
   1764 		 * Initialization with 0 is allowed. Otherwise, all implicit
   1765 		 * initializations would need to be warned upon as well.
   1766 		 */
   1767 		if (!rn->tn_type->t_is_enum && rn->tn_op == CON &&
   1768 		    is_integer(rn->tn_type->t_tspec) &&
   1769 		    rn->tn_val->v_quad == 0) {
   1770 			return;
   1771 		}
   1772 		/* initialization of '%s' with '%s' */
   1773 		warning(277, type_name(ln->tn_type), type_name(rn->tn_type));
   1774 		break;
   1775 	case FARG:
   1776 		/* combination of '%s' and '%s', arg #%d */
   1777 		warning(278,
   1778 		    type_name(ln->tn_type), type_name(rn->tn_type), arg);
   1779 		break;
   1780 	case RETURN:
   1781 		/* combination of '%s' and '%s' in return */
   1782 		warning(279, type_name(ln->tn_type), type_name(rn->tn_type));
   1783 		break;
   1784 	default:
   1785 		/* combination of '%s' and '%s', op %s */
   1786 		warning(242, type_name(ln->tn_type), type_name(rn->tn_type),
   1787 		    modtab[op].m_name);
   1788 		break;
   1789 	}
   1790 }
   1791 
   1792 /*
   1793  * Build and initialize a new node.
   1794  */
   1795 static tnode_t *
   1796 new_tnode(op_t op, type_t *type, tnode_t *ln, tnode_t *rn)
   1797 {
   1798 	tnode_t	*ntn;
   1799 	tspec_t	t;
   1800 #ifdef notyet
   1801 	size_t l;
   1802 	uint64_t rnum;
   1803 #endif
   1804 
   1805 	ntn = getnode();
   1806 
   1807 	ntn->tn_op = op;
   1808 	ntn->tn_type = type;
   1809 	if (ln->tn_from_system_header)
   1810 		ntn->tn_from_system_header = true;
   1811 	if (rn != NULL && rn->tn_from_system_header)
   1812 		ntn->tn_from_system_header = true;
   1813 	ntn->tn_left = ln;
   1814 	ntn->tn_right = rn;
   1815 
   1816 	switch (op) {
   1817 #ifdef notyet
   1818 	case SHR:
   1819 		if (rn->tn_op != CON)
   1820 			break;
   1821 		rnum = rn->tn_val->v_quad;
   1822 		l = type_size_in_bits(ln->tn_type) / CHAR_SIZE;
   1823 		t = ln->tn_type->t_tspec;
   1824 		switch (l) {
   1825 		case 8:
   1826 			if (rnum >= 56)
   1827 				t = UCHAR;
   1828 			else if (rnum >= 48)
   1829 				t = USHORT;
   1830 			else if (rnum >= 32)
   1831 				t = UINT;
   1832 			break;
   1833 		case 4:
   1834 			if (rnum >= 24)
   1835 				t = UCHAR;
   1836 			else if (rnum >= 16)
   1837 				t = USHORT;
   1838 			break;
   1839 		case 2:
   1840 			if (rnum >= 8)
   1841 				t = UCHAR;
   1842 			break;
   1843 		default:
   1844 			break;
   1845 		}
   1846 		if (t != ln->tn_type->t_tspec)
   1847 			ntn->tn_type->t_tspec = t;
   1848 		break;
   1849 #endif
   1850 	case INDIR:
   1851 	case FSEL:
   1852 		lint_assert(ln->tn_type->t_tspec == PTR);
   1853 		t = ln->tn_type->t_subt->t_tspec;
   1854 		if (t != FUNC && t != VOID)
   1855 			ntn->tn_lvalue = true;
   1856 		break;
   1857 	default:
   1858 		break;
   1859 	}
   1860 
   1861 	return ntn;
   1862 }
   1863 
   1864 /*
   1865  * Performs the "integer promotions" (C99 6.3.1.1p2), which convert small
   1866  * integer types to either int or unsigned int.
   1867  *
   1868  * If tflag is set or the operand is a function argument with no type
   1869  * information (no prototype or variable # of args), converts float to double.
   1870  */
   1871 tnode_t *
   1872 promote(op_t op, bool farg, tnode_t *tn)
   1873 {
   1874 	tspec_t	t;
   1875 	type_t	*ntp;
   1876 	u_int	len;
   1877 
   1878 	t = tn->tn_type->t_tspec;
   1879 
   1880 	if (!is_arithmetic(t))
   1881 		return tn;
   1882 
   1883 	if (!tflag) {
   1884 		/*
   1885 		 * ANSI C requires that the result is always of type INT
   1886 		 * if INT can represent all possible values of the previous
   1887 		 * type.
   1888 		 */
   1889 		if (tn->tn_type->t_bitfield) {
   1890 			len = tn->tn_type->t_flen;
   1891 			if (size_in_bits(INT) > len) {
   1892 				t = INT;
   1893 			} else {
   1894 				lint_assert(len == size_in_bits(INT));
   1895 				if (is_uinteger(t)) {
   1896 					t = UINT;
   1897 				} else {
   1898 					t = INT;
   1899 				}
   1900 			}
   1901 		} else if (t == CHAR || t == UCHAR || t == SCHAR) {
   1902 			t = (size_in_bits(CHAR) < size_in_bits(INT)
   1903 			     || t != UCHAR) ? INT : UINT;
   1904 		} else if (t == SHORT || t == USHORT) {
   1905 			t = (size_in_bits(SHORT) < size_in_bits(INT)
   1906 			     || t == SHORT) ? INT : UINT;
   1907 		} else if (t == ENUM) {
   1908 			t = INT;
   1909 		} else if (farg && t == FLOAT) {
   1910 			t = DOUBLE;
   1911 		}
   1912 	} else {
   1913 		/*
   1914 		 * In traditional C, keep unsigned and promote FLOAT
   1915 		 * to DOUBLE.
   1916 		 */
   1917 		if (t == UCHAR || t == USHORT) {
   1918 			t = UINT;
   1919 		} else if (t == CHAR || t == SCHAR || t == SHORT) {
   1920 			t = INT;
   1921 		} else if (t == FLOAT) {
   1922 			t = DOUBLE;
   1923 		} else if (t == ENUM) {
   1924 			t = INT;
   1925 		}
   1926 	}
   1927 
   1928 	if (t != tn->tn_type->t_tspec) {
   1929 		ntp = tduptyp(tn->tn_type);
   1930 		ntp->t_tspec = t;
   1931 		/*
   1932 		 * Keep t_is_enum so we are later able to check compatibility
   1933 		 * of enum types.
   1934 		 */
   1935 		tn = convert(op, 0, ntp, tn);
   1936 	}
   1937 
   1938 	return tn;
   1939 }
   1940 
   1941 /*
   1942  * Apply the "usual arithmetic conversions" (C99 6.3.1.8).
   1943  *
   1944  * This gives both operands the same type.
   1945  * This is done in different ways for traditional C and C90.
   1946  */
   1947 static void
   1948 balance(op_t op, tnode_t **lnp, tnode_t **rnp)
   1949 {
   1950 	tspec_t	lt, rt, t;
   1951 	int	i;
   1952 	bool	u;
   1953 	type_t	*ntp;
   1954 	static const tspec_t tl[] = {
   1955 		LDOUBLE, DOUBLE, FLOAT, UQUAD, QUAD, ULONG, LONG, UINT, INT,
   1956 	};
   1957 
   1958 	lt = (*lnp)->tn_type->t_tspec;
   1959 	rt = (*rnp)->tn_type->t_tspec;
   1960 
   1961 	if (!is_arithmetic(lt) || !is_arithmetic(rt))
   1962 		return;
   1963 
   1964 	if (!tflag) {
   1965 		if (lt == rt) {
   1966 			t = lt;
   1967 		} else if (lt == LCOMPLEX || rt == LCOMPLEX) {
   1968 			t = LCOMPLEX;
   1969 		} else if (lt == DCOMPLEX || rt == DCOMPLEX) {
   1970 			t = DCOMPLEX;
   1971 		} else if (lt == COMPLEX || rt == COMPLEX) {
   1972 			t = COMPLEX;
   1973 		} else if (lt == FCOMPLEX || rt == FCOMPLEX) {
   1974 			t = FCOMPLEX;
   1975 		} else if (lt == LDOUBLE || rt == LDOUBLE) {
   1976 			t = LDOUBLE;
   1977 		} else if (lt == DOUBLE || rt == DOUBLE) {
   1978 			t = DOUBLE;
   1979 		} else if (lt == FLOAT || rt == FLOAT) {
   1980 			t = FLOAT;
   1981 		} else {
   1982 			/*
   1983 			 * If type A has more bits than type B it should
   1984 			 * be able to hold all possible values of type B.
   1985 			 */
   1986 			if (size_in_bits(lt) > size_in_bits(rt)) {
   1987 				t = lt;
   1988 			} else if (size_in_bits(lt) < size_in_bits(rt)) {
   1989 				t = rt;
   1990 			} else {
   1991 				for (i = 3; tl[i] != INT; i++) {
   1992 					if (tl[i] == lt || tl[i] == rt)
   1993 						break;
   1994 				}
   1995 				if ((is_uinteger(lt) || is_uinteger(rt)) &&
   1996 				    !is_uinteger(tl[i])) {
   1997 					i--;
   1998 				}
   1999 				t = tl[i];
   2000 			}
   2001 		}
   2002 	} else {
   2003 		/* Keep unsigned in traditional C */
   2004 		u = is_uinteger(lt) || is_uinteger(rt);
   2005 		for (i = 0; tl[i] != INT; i++) {
   2006 			if (lt == tl[i] || rt == tl[i])
   2007 				break;
   2008 		}
   2009 		t = tl[i];
   2010 		if (u && is_integer(t) && !is_uinteger(t))
   2011 			t = unsigned_type(t);
   2012 	}
   2013 
   2014 	if (t != lt) {
   2015 		ntp = tduptyp((*lnp)->tn_type);
   2016 		ntp->t_tspec = t;
   2017 		*lnp = convert(op, 0, ntp, *lnp);
   2018 	}
   2019 	if (t != rt) {
   2020 		ntp = tduptyp((*rnp)->tn_type);
   2021 		ntp->t_tspec = t;
   2022 		*rnp = convert(op, 0, ntp, *rnp);
   2023 	}
   2024 }
   2025 
   2026 /*
   2027  * Insert a conversion operator, which converts the type of the node
   2028  * to another given type.
   2029  * If op is FARG, arg is the number of the argument (used for warnings).
   2030  */
   2031 tnode_t *
   2032 convert(op_t op, int arg, type_t *tp, tnode_t *tn)
   2033 {
   2034 	tnode_t	*ntn;
   2035 	tspec_t	nt, ot;
   2036 
   2037 	nt = tp->t_tspec;
   2038 	ot = tn->tn_type->t_tspec;
   2039 
   2040 	if (!tflag && !sflag && op == FARG)
   2041 		check_prototype_conversion(arg, nt, ot, tp, tn);
   2042 	if (is_integer(nt) && is_integer(ot)) {
   2043 		check_integer_conversion(op, arg, nt, ot, tp, tn);
   2044 	} else if (nt == PTR && is_null_pointer(tn)) {
   2045 		/* a null pointer may be assigned to any pointer. */
   2046 	} else if (is_integer(nt) && nt != BOOL && ot == PTR) {
   2047 		check_pointer_integer_conversion(op, nt, tp, tn);
   2048 	} else if (nt == PTR && ot == PTR) {
   2049 		check_pointer_conversion(op, tn, tp);
   2050 	}
   2051 
   2052 	ntn = getnode();
   2053 	ntn->tn_op = CVT;
   2054 	ntn->tn_type = tp;
   2055 	ntn->tn_cast = op == CVT;
   2056 	ntn->tn_from_system_header |= tn->tn_from_system_header;
   2057 	ntn->tn_right = NULL;
   2058 	if (tn->tn_op != CON || nt == VOID) {
   2059 		ntn->tn_left = tn;
   2060 	} else {
   2061 		ntn->tn_op = CON;
   2062 		ntn->tn_val = tgetblk(sizeof (val_t));
   2063 		convert_constant(op, arg, ntn->tn_type, ntn->tn_val,
   2064 		    tn->tn_val);
   2065 	}
   2066 
   2067 	return ntn;
   2068 }
   2069 
   2070 /*
   2071  * Print a warning if a prototype causes a type conversion that is
   2072  * different from what would happen to the same argument in the
   2073  * absence of a prototype.
   2074  *
   2075  * Errors/warnings about illegal type combinations are already printed
   2076  * in check_assign_types_compatible().
   2077  */
   2078 static void
   2079 check_prototype_conversion(int arg, tspec_t nt, tspec_t ot, type_t *tp,
   2080 			   tnode_t *tn)
   2081 {
   2082 	tnode_t	*ptn;
   2083 
   2084 	if (!is_arithmetic(nt) || !is_arithmetic(ot))
   2085 		return;
   2086 
   2087 	/*
   2088 	 * If the type of the formal parameter is char/short, a warning
   2089 	 * would be useless, because functions declared the old style
   2090 	 * can't expect char/short arguments.
   2091 	 */
   2092 	/* XXX: what about SCHAR? */
   2093 	if (nt == CHAR || nt == UCHAR || nt == SHORT || nt == USHORT)
   2094 		return;
   2095 
   2096 	/* get default promotion */
   2097 	ptn = promote(NOOP, true, tn);
   2098 	ot = ptn->tn_type->t_tspec;
   2099 
   2100 	/* return if types are the same with and without prototype */
   2101 	if (nt == ot || (nt == ENUM && ot == INT))
   2102 		return;
   2103 
   2104 	if (is_floating(nt) != is_floating(ot) ||
   2105 	    portable_size_in_bits(nt) != portable_size_in_bits(ot)) {
   2106 		/* representation and/or width change */
   2107 		if (!is_integer(ot) ||
   2108 		    portable_size_in_bits(ot) > portable_size_in_bits(INT)) {
   2109 			/* argument #%d is converted from '%s' to '%s' ... */
   2110 			warning(259,
   2111 			    arg, type_name(tn->tn_type), type_name(tp));
   2112 		}
   2113 	} else if (hflag) {
   2114 		/*
   2115 		 * they differ in sign or base type (char, short, int,
   2116 		 * long, long long, float, double, long double)
   2117 		 *
   2118 		 * if they differ only in sign and the argument is a constant
   2119 		 * and the msb of the argument is not set, print no warning
   2120 		 */
   2121 		if (ptn->tn_op == CON && is_integer(nt) &&
   2122 		    signed_type(nt) == signed_type(ot) &&
   2123 		    msb(ptn->tn_val->v_quad, ot, -1) == 0) {
   2124 			/* ok */
   2125 		} else {
   2126 			/* argument #%d is converted from '%s' to '%s' ... */
   2127 			warning(259,
   2128 			    arg, type_name(tn->tn_type), type_name(tp));
   2129 		}
   2130 	}
   2131 }
   2132 
   2133 /*
   2134  * Print warnings for conversions of integer types which may cause problems.
   2135  */
   2136 /* ARGSUSED */
   2137 static void
   2138 check_integer_conversion(op_t op, int arg, tspec_t nt, tspec_t ot, type_t *tp,
   2139 			 tnode_t *tn)
   2140 {
   2141 	char opbuf[16];
   2142 
   2143 	if (tn->tn_op == CON)
   2144 		return;
   2145 
   2146 	if (op == CVT)
   2147 		return;
   2148 
   2149 	if (Pflag && portable_size_in_bits(nt) > portable_size_in_bits(ot) &&
   2150 	    is_uinteger(nt) != is_uinteger(ot)) {
   2151 		if (aflag > 0 && pflag) {
   2152 			if (op == FARG) {
   2153 				/* conversion to '%s' may sign-extend ... */
   2154 				warning(297, type_name(tp), arg);
   2155 			} else {
   2156 				/* conversion to '%s' may sign-extend ... */
   2157 				warning(131, type_name(tp));
   2158 			}
   2159 		}
   2160 	}
   2161 
   2162 	if (Pflag && portable_size_in_bits(nt) > portable_size_in_bits(ot)) {
   2163 		switch (tn->tn_op) {
   2164 		case PLUS:
   2165 		case MINUS:
   2166 		case MULT:
   2167 		case SHL:
   2168 			/* suggest cast from '%s' to '%s' on op %s to ... */
   2169 			warning(324, type_name(gettyp(ot)), type_name(tp),
   2170 			    print_tnode(opbuf, sizeof(opbuf), tn));
   2171 			break;
   2172 		default:
   2173 			break;
   2174 		}
   2175 	}
   2176 
   2177 	if (portable_size_in_bits(nt) < portable_size_in_bits(ot) &&
   2178 	    (ot == LONG || ot == ULONG || ot == QUAD || ot == UQUAD ||
   2179 	     aflag > 1)) {
   2180 		/* conversion from '%s' may lose accuracy */
   2181 		if (aflag > 0) {
   2182 			if (op == FARG) {
   2183 				/* conv. from '%s' to '%s' may lose ... */
   2184 				warning(298,
   2185 				    type_name(tn->tn_type), type_name(tp), arg);
   2186 			} else {
   2187 				/* conv. from '%s' to '%s' may lose accuracy */
   2188 				warning(132,
   2189 				    type_name(tn->tn_type), type_name(tp));
   2190 			}
   2191 		}
   2192 	}
   2193 }
   2194 
   2195 /*
   2196  * Print warnings for dubious conversions of pointer to integer.
   2197  */
   2198 static void
   2199 check_pointer_integer_conversion(op_t op, tspec_t nt, type_t *tp, tnode_t *tn)
   2200 {
   2201 
   2202 	if (tn->tn_op == CON)
   2203 		return;
   2204 	if (op != CVT)
   2205 		return;		/* We got already an error. */
   2206 	if (portable_size_in_bits(nt) >= portable_size_in_bits(PTR))
   2207 		return;
   2208 
   2209 	if (pflag && size_in_bits(nt) >= size_in_bits(PTR)) {
   2210 		/* conversion of pointer to '%s' may lose bits */
   2211 		warning(134, type_name(tp));
   2212 	} else {
   2213 		/* conversion of pointer to '%s' loses bits */
   2214 		warning(133, type_name(tp));
   2215 	}
   2216 }
   2217 
   2218 /*
   2219  * Print warnings for questionable pointer conversions.
   2220  */
   2221 static void
   2222 check_pointer_conversion(op_t op, tnode_t *tn, type_t *tp)
   2223 {
   2224 	tspec_t nt, ot;
   2225 	const	char *nts, *ots;
   2226 
   2227 	/*
   2228 	 * We got already an error (pointers of different types
   2229 	 * without a cast) or we will not get a warning.
   2230 	 */
   2231 	if (op != CVT)
   2232 		return;
   2233 
   2234 	nt = tp->t_subt->t_tspec;
   2235 	ot = tn->tn_type->t_subt->t_tspec;
   2236 
   2237 	if (nt == VOID || ot == VOID) {
   2238 		if (sflag && (nt == FUNC || ot == FUNC)) {
   2239 			/* (void *)0 already handled in convert() */
   2240 			*(nt == FUNC ? &nts : &ots) = "function pointer";
   2241 			*(nt == VOID ? &nts : &ots) = "'void *'";
   2242 			/* ANSI C forbids conversion of %s to %s */
   2243 			warning(303, ots, nts);
   2244 		}
   2245 		return;
   2246 	} else if (nt == FUNC && ot == FUNC) {
   2247 		return;
   2248 	} else if (nt == FUNC || ot == FUNC) {
   2249 		/* converting '%s' to '%s' is questionable */
   2250 		warning(229, type_name(tn->tn_type), type_name(tp));
   2251 		return;
   2252 	}
   2253 
   2254 	if (hflag && alignment_in_bits(tp->t_subt) >
   2255 		     alignment_in_bits(tn->tn_type->t_subt)) {
   2256 		/* converting '%s' to '%s' may cause alignment problem */
   2257 		warning(135, type_name(tn->tn_type), type_name(tp));
   2258 	}
   2259 
   2260 	if (((nt == STRUCT || nt == UNION) &&
   2261 	     tp->t_subt->t_str != tn->tn_type->t_subt->t_str) ||
   2262 	    portable_size_in_bits(nt) != portable_size_in_bits(ot)) {
   2263 		if (cflag) {
   2264 			/* pointer cast from '%s' to '%s' may be troublesome */
   2265 			warning(247, type_name(tn->tn_type), type_name(tp));
   2266 		}
   2267 	}
   2268 }
   2269 
   2270 /*
   2271  * Converts a typed constant to a constant of another type.
   2272  *
   2273  * op		operator which requires conversion
   2274  * arg		if op is FARG, # of argument
   2275  * tp		type in which to convert the constant
   2276  * nv		new constant
   2277  * v		old constant
   2278  */
   2279 void
   2280 convert_constant(op_t op, int arg, type_t *tp, val_t *nv, val_t *v)
   2281 {
   2282 	tspec_t	ot, nt;
   2283 	ldbl_t	max = 0.0, min = 0.0;
   2284 	int	sz;
   2285 	bool	rchk;
   2286 	int64_t	xmask, xmsk1;
   2287 	int	osz, nsz;
   2288 
   2289 	ot = v->v_tspec;
   2290 	nt = nv->v_tspec = tp->t_tspec;
   2291 	rchk = false;
   2292 
   2293 	if (nt == BOOL) {	/* C99 6.3.1.2 */
   2294 		nv->v_ansiu = false;
   2295 		nv->v_quad = is_nonzero_val(v) ? 1 : 0;
   2296 		return;
   2297 	}
   2298 
   2299 	if (ot == FLOAT || ot == DOUBLE || ot == LDOUBLE) {
   2300 		switch (nt) {
   2301 		case CHAR:
   2302 			max = TARG_CHAR_MAX;	min = TARG_CHAR_MIN;	break;
   2303 		case UCHAR:
   2304 			max = TARG_UCHAR_MAX;	min = 0;		break;
   2305 		case SCHAR:
   2306 			max = TARG_SCHAR_MAX;	min = TARG_SCHAR_MIN;	break;
   2307 		case SHORT:
   2308 			max = TARG_SHRT_MAX;	min = TARG_SHRT_MIN;	break;
   2309 		case USHORT:
   2310 			max = TARG_USHRT_MAX;	min = 0;		break;
   2311 		case ENUM:
   2312 		case INT:
   2313 			max = TARG_INT_MAX;	min = TARG_INT_MIN;	break;
   2314 		case UINT:
   2315 			max = (u_int)TARG_UINT_MAX;min = 0;		break;
   2316 		case LONG:
   2317 			max = TARG_LONG_MAX;	min = TARG_LONG_MIN;	break;
   2318 		case ULONG:
   2319 			max = (u_long)TARG_ULONG_MAX; min = 0;		break;
   2320 		case QUAD:
   2321 			max = QUAD_MAX;		min = QUAD_MIN;		break;
   2322 		case UQUAD:
   2323 			max = (uint64_t)UQUAD_MAX; min = 0;		break;
   2324 		case FLOAT:
   2325 		case FCOMPLEX:
   2326 			max = FLT_MAX;		min = -FLT_MAX;		break;
   2327 		case DOUBLE:
   2328 		case DCOMPLEX:
   2329 			max = DBL_MAX;		min = -DBL_MAX;		break;
   2330 		case PTR:
   2331 			/* Got already an error because of float --> ptr */
   2332 		case LDOUBLE:
   2333 		case LCOMPLEX:
   2334 			max = LDBL_MAX;		min = -LDBL_MAX;	break;
   2335 		default:
   2336 			lint_assert(/*CONSTCOND*/false);
   2337 		}
   2338 		if (v->v_ldbl > max || v->v_ldbl < min) {
   2339 			lint_assert(nt != LDOUBLE);
   2340 			if (op == FARG) {
   2341 				/* conv. of '%s' to '%s' is out of range, ... */
   2342 				warning(295,
   2343 				    type_name(gettyp(ot)), type_name(tp), arg);
   2344 			} else {
   2345 				/* conversion of '%s' to '%s' is out of range */
   2346 				warning(119,
   2347 				    type_name(gettyp(ot)), type_name(tp));
   2348 			}
   2349 			v->v_ldbl = v->v_ldbl > 0 ? max : min;
   2350 		}
   2351 		if (nt == FLOAT) {
   2352 			nv->v_ldbl = (float)v->v_ldbl;
   2353 		} else if (nt == DOUBLE) {
   2354 			nv->v_ldbl = (double)v->v_ldbl;
   2355 		} else if (nt == LDOUBLE) {
   2356 			nv->v_ldbl = v->v_ldbl;
   2357 		} else {
   2358 			nv->v_quad = (nt == PTR || is_uinteger(nt)) ?
   2359 				(int64_t)v->v_ldbl : (int64_t)v->v_ldbl;
   2360 		}
   2361 	} else {
   2362 		if (nt == FLOAT) {
   2363 			nv->v_ldbl = (ot == PTR || is_uinteger(ot)) ?
   2364 			       (float)(uint64_t)v->v_quad : (float)v->v_quad;
   2365 		} else if (nt == DOUBLE) {
   2366 			nv->v_ldbl = (ot == PTR || is_uinteger(ot)) ?
   2367 			       (double)(uint64_t)v->v_quad : (double)v->v_quad;
   2368 		} else if (nt == LDOUBLE) {
   2369 			nv->v_ldbl = (ot == PTR || is_uinteger(ot)) ?
   2370 			       (ldbl_t)(uint64_t)v->v_quad : (ldbl_t)v->v_quad;
   2371 		} else {
   2372 			rchk = true;		/* Check for lost precision. */
   2373 			nv->v_quad = v->v_quad;
   2374 		}
   2375 	}
   2376 
   2377 	if (v->v_ansiu && is_floating(nt)) {
   2378 		/* ANSI C treats constant as unsigned */
   2379 		warning(157);
   2380 		v->v_ansiu = false;
   2381 	} else if (v->v_ansiu && (is_integer(nt) && !is_uinteger(nt) &&
   2382 				  portable_size_in_bits(nt) >
   2383 				  portable_size_in_bits(ot))) {
   2384 		/* ANSI C treats constant as unsigned */
   2385 		warning(157);
   2386 		v->v_ansiu = false;
   2387 	}
   2388 
   2389 	switch (nt) {
   2390 	case FLOAT:
   2391 	case FCOMPLEX:
   2392 	case DOUBLE:
   2393 	case DCOMPLEX:
   2394 	case LDOUBLE:
   2395 	case LCOMPLEX:
   2396 		break;
   2397 	default:
   2398 		sz = tp->t_bitfield ? tp->t_flen : size_in_bits(nt);
   2399 		nv->v_quad = xsign(nv->v_quad, nt, sz);
   2400 		break;
   2401 	}
   2402 
   2403 	if (rchk && op != CVT) {
   2404 		osz = size_in_bits(ot);
   2405 		nsz = tp->t_bitfield ? tp->t_flen : size_in_bits(nt);
   2406 		xmask = qlmasks[nsz] ^ qlmasks[osz];
   2407 		xmsk1 = qlmasks[nsz] ^ qlmasks[osz - 1];
   2408 		/*
   2409 		 * For bitwise operations we are not interested in the
   2410 		 * value, but in the bits itself.
   2411 		 */
   2412 		if (op == ORASS || op == BITOR || op == BITXOR) {
   2413 			/*
   2414 			 * Print a warning if bits which were set are
   2415 			 * lost due to the conversion.
   2416 			 * This can happen with operator ORASS only.
   2417 			 */
   2418 			if (nsz < osz && (v->v_quad & xmask) != 0) {
   2419 				/* constant truncated by conv., op %s */
   2420 				warning(306, modtab[op].m_name);
   2421 			}
   2422 		} else if (op == ANDASS || op == BITAND) {
   2423 			/*
   2424 			 * Print a warning if additional bits are not all 1
   2425 			 * and the most significant bit of the old value is 1,
   2426 			 * or if at least one (but not all) removed bit was 0.
   2427 			 */
   2428 			if (nsz > osz &&
   2429 			    (nv->v_quad & qbmasks[osz - 1]) != 0 &&
   2430 			    (nv->v_quad & xmask) != xmask) {
   2431 				/* extra bits set to 0 in conv. of '%s' ... */
   2432 				warning(309, type_name(gettyp(ot)),
   2433 				    type_name(tp), modtab[op].m_name);
   2434 			} else if (nsz < osz &&
   2435 				   (v->v_quad & xmask) != xmask &&
   2436 				   (v->v_quad & xmask) != 0) {
   2437 				/* constant truncated by conv., op %s */
   2438 				warning(306, modtab[op].m_name);
   2439 			}
   2440 		} else if ((nt != PTR && is_uinteger(nt)) &&
   2441 			   (ot != PTR && !is_uinteger(ot)) &&
   2442 			   v->v_quad < 0) {
   2443 			if (op == ASSIGN) {
   2444 				/* assignment of negative constant to ... */
   2445 				warning(164);
   2446 			} else if (op == INIT) {
   2447 				/* initialization of unsigned with neg... */
   2448 				warning(221);
   2449 			} else if (op == FARG) {
   2450 				/* conversion of negative constant to ... */
   2451 				warning(296, arg);
   2452 			} else if (modtab[op].m_comparison) {
   2453 				/* handled by check_integer_comparison() */
   2454 			} else {
   2455 				/* conversion of negative constant to ... */
   2456 				warning(222);
   2457 			}
   2458 		} else if (nv->v_quad != v->v_quad && nsz <= osz &&
   2459 			   (v->v_quad & xmask) != 0 &&
   2460 			   (is_uinteger(ot) || (v->v_quad & xmsk1) != xmsk1)) {
   2461 			/*
   2462 			 * Loss of significant bit(s). All truncated bits
   2463 			 * of unsigned types or all truncated bits plus the
   2464 			 * msb of the target for signed types are considered
   2465 			 * to be significant bits. Loss of significant bits
   2466 			 * means that at least on of the bits was set in an
   2467 			 * unsigned type or that at least one, but not all of
   2468 			 * the bits was set in an signed type.
   2469 			 * Loss of significant bits means that it is not
   2470 			 * possible, also not with necessary casts, to convert
   2471 			 * back to the original type. A example for a
   2472 			 * necessary cast is:
   2473 			 *	char c;	int	i; c = 128;
   2474 			 *	i = c;			** yields -128 **
   2475 			 *	i = (unsigned char)c;	** yields 128 **
   2476 			 */
   2477 			if (op == ASSIGN && tp->t_bitfield) {
   2478 				/* precision lost in bit-field assignment */
   2479 				warning(166);
   2480 			} else if (op == ASSIGN) {
   2481 				/* constant truncated by assignment */
   2482 				warning(165);
   2483 			} else if (op == INIT && tp->t_bitfield) {
   2484 				/* bit-field initializer does not fit */
   2485 				warning(180);
   2486 			} else if (op == INIT) {
   2487 				/* initializer does not fit */
   2488 				warning(178);
   2489 			} else if (op == CASE) {
   2490 				/* case label affected by conversion */
   2491 				warning(196);
   2492 			} else if (op == FARG) {
   2493 				/* conv. of '%s' to '%s' is out of range, ... */
   2494 				warning(295,
   2495 				    type_name(gettyp(ot)), type_name(tp), arg);
   2496 			} else {
   2497 				/* conversion of '%s' to '%s' is out of range */
   2498 				warning(119,
   2499 				    type_name(gettyp(ot)), type_name(tp));
   2500 			}
   2501 		} else if (nv->v_quad != v->v_quad) {
   2502 			if (op == ASSIGN && tp->t_bitfield) {
   2503 				/* precision lost in bit-field assignment */
   2504 				warning(166);
   2505 			} else if (op == INIT && tp->t_bitfield) {
   2506 				/* bit-field initializer out of range */
   2507 				warning(11);
   2508 			} else if (op == CASE) {
   2509 				/* case label affected by conversion */
   2510 				warning(196);
   2511 			} else if (op == FARG) {
   2512 				/* conv. of '%s' to '%s' is out of range, ... */
   2513 				warning(295,
   2514 				    type_name(gettyp(ot)), type_name(tp), arg);
   2515 			} else {
   2516 				/* conversion of '%s' to '%s' is out of range */
   2517 				warning(119,
   2518 				    type_name(gettyp(ot)), type_name(tp));
   2519 			}
   2520 		}
   2521 	}
   2522 }
   2523 
   2524 /*
   2525  * Called if incompatible types were detected.
   2526  * Prints a appropriate warning.
   2527  */
   2528 static void
   2529 warn_incompatible_types(op_t op, tspec_t lt, tspec_t rt)
   2530 {
   2531 	mod_t	*mp;
   2532 
   2533 	mp = &modtab[op];
   2534 
   2535 	if (lt == VOID || (mp->m_binary && rt == VOID)) {
   2536 		/* void type illegal in expression */
   2537 		error(109);
   2538 	} else if (op == ASSIGN) {
   2539 		if ((lt == STRUCT || lt == UNION) &&
   2540 		    (rt == STRUCT || rt == UNION)) {
   2541 			/* assignment of different structures (%s != %s) */
   2542 			error(240, tspec_name(lt), tspec_name(rt));
   2543 		} else {
   2544 			/* assignment type mismatch (%s != %s) */
   2545 			error(171, tspec_name(lt), tspec_name(rt));
   2546 		}
   2547 	} else if (mp->m_binary) {
   2548 		/* operands of '%s' have incompatible types (%s != %s) */
   2549 		error(107, mp->m_name, tspec_name(lt), tspec_name(rt));
   2550 	} else {
   2551 		lint_assert(rt == NOTSPEC);
   2552 		/* operand of '%s' has invalid type (%s) */
   2553 		error(108, mp->m_name, tspec_name(lt));
   2554 	}
   2555 }
   2556 
   2557 /*
   2558  * Called if incompatible pointer types are detected.
   2559  * Print an appropriate warning.
   2560  */
   2561 static void
   2562 warn_incompatible_pointers(const mod_t *mp,
   2563 			   const type_t *ltp, const type_t *rtp)
   2564 {
   2565 	tspec_t	lt, rt;
   2566 
   2567 	lint_assert(ltp->t_tspec == PTR);
   2568 	lint_assert(rtp->t_tspec == PTR);
   2569 
   2570 	lt = ltp->t_subt->t_tspec;
   2571 	rt = rtp->t_subt->t_tspec;
   2572 
   2573 	if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION)) {
   2574 		if (mp == NULL) {
   2575 			/* illegal structure pointer combination */
   2576 			warning(244);
   2577 		} else {
   2578 			/* incompatible structure pointers: '%s' '%s' '%s' */
   2579 			warning(245, type_name(ltp), mp->m_name, type_name(rtp));
   2580 		}
   2581 	} else {
   2582 		if (mp == NULL) {
   2583 			/* illegal pointer combination */
   2584 			warning(184);
   2585 		} else {
   2586 			/* illegal pointer combination (%s) and (%s), op %s */
   2587 			warning(124,
   2588 			    type_name(ltp), type_name(rtp), mp->m_name);
   2589 		}
   2590 	}
   2591 }
   2592 
   2593 /*
   2594  * Make sure type (*tpp)->t_subt has at least the qualifiers
   2595  * of tp1->t_subt and tp2->t_subt.
   2596  */
   2597 static void
   2598 merge_qualifiers(type_t **tpp, type_t *tp1, type_t *tp2)
   2599 {
   2600 
   2601 	lint_assert((*tpp)->t_tspec == PTR);
   2602 	lint_assert(tp1->t_tspec == PTR);
   2603 	lint_assert(tp2->t_tspec == PTR);
   2604 
   2605 	if ((*tpp)->t_subt->t_const ==
   2606 	    (tp1->t_subt->t_const | tp2->t_subt->t_const) &&
   2607 	    (*tpp)->t_subt->t_volatile ==
   2608 	    (tp1->t_subt->t_volatile | tp2->t_subt->t_volatile)) {
   2609 		return;
   2610 	}
   2611 
   2612 	*tpp = tduptyp(*tpp);
   2613 	(*tpp)->t_subt = tduptyp((*tpp)->t_subt);
   2614 	(*tpp)->t_subt->t_const =
   2615 		tp1->t_subt->t_const | tp2->t_subt->t_const;
   2616 	(*tpp)->t_subt->t_volatile =
   2617 		tp1->t_subt->t_volatile | tp2->t_subt->t_volatile;
   2618 }
   2619 
   2620 /*
   2621  * Returns true if the given structure or union has a constant member
   2622  * (maybe recursively).
   2623  */
   2624 static bool
   2625 has_constant_member(const type_t *tp)
   2626 {
   2627 	sym_t	*m;
   2628 	tspec_t	t;
   2629 
   2630 	lint_assert((t = tp->t_tspec) == STRUCT || t == UNION);
   2631 
   2632 	for (m = tp->t_str->sou_first_member; m != NULL; m = m->s_next) {
   2633 		tp = m->s_type;
   2634 		if (tp->t_const)
   2635 			return true;
   2636 		if ((t = tp->t_tspec) == STRUCT || t == UNION) {
   2637 			if (has_constant_member(m->s_type))
   2638 				return true;
   2639 		}
   2640 	}
   2641 	return false;
   2642 }
   2643 
   2644 /*
   2645  * Create a new node for one of the operators POINT and ARROW.
   2646  */
   2647 static tnode_t *
   2648 build_struct_access(op_t op, tnode_t *ln, tnode_t *rn)
   2649 {
   2650 	tnode_t	*ntn, *ctn;
   2651 	bool	nolval;
   2652 
   2653 	lint_assert(rn->tn_op == NAME);
   2654 	lint_assert(rn->tn_sym->s_value.v_tspec == INT);
   2655 	lint_assert(rn->tn_sym->s_scl == MOS || rn->tn_sym->s_scl == MOU);
   2656 
   2657 	/*
   2658 	 * Remember if the left operand is an lvalue (structure members
   2659 	 * are lvalues if and only if the structure itself is an lvalue).
   2660 	 */
   2661 	nolval = op == POINT && !ln->tn_lvalue;
   2662 
   2663 	if (op == POINT) {
   2664 		ln = build_address(ln, true);
   2665 	} else if (ln->tn_type->t_tspec != PTR) {
   2666 		lint_assert(tflag);
   2667 		lint_assert(is_integer(ln->tn_type->t_tspec));
   2668 		ln = convert(NOOP, 0, tincref(gettyp(VOID), PTR), ln);
   2669 	}
   2670 
   2671 	ctn = new_integer_constant_node(PTRDIFF_TSPEC,
   2672 	    rn->tn_sym->s_value.v_quad / CHAR_SIZE);
   2673 
   2674 	ntn = new_tnode(PLUS, tincref(rn->tn_type, PTR), ln, ctn);
   2675 	if (ln->tn_op == CON)
   2676 		ntn = fold(ntn);
   2677 
   2678 	if (rn->tn_type->t_bitfield) {
   2679 		ntn = new_tnode(FSEL, ntn->tn_type->t_subt, ntn, NULL);
   2680 	} else {
   2681 		ntn = new_tnode(INDIR, ntn->tn_type->t_subt, ntn, NULL);
   2682 	}
   2683 
   2684 	if (nolval)
   2685 		ntn->tn_lvalue = false;
   2686 
   2687 	return ntn;
   2688 }
   2689 
   2690 /*
   2691  * Create a node for INCAFT, INCBEF, DECAFT and DECBEF.
   2692  */
   2693 static tnode_t *
   2694 build_prepost_incdec(op_t op, tnode_t *ln)
   2695 {
   2696 	tnode_t	*cn, *ntn;
   2697 
   2698 	lint_assert(ln != NULL);
   2699 
   2700 	if (ln->tn_type->t_tspec == PTR) {
   2701 		cn = plength(ln->tn_type);
   2702 	} else {
   2703 		cn = new_integer_constant_node(INT, (int64_t)1);
   2704 	}
   2705 	ntn = new_tnode(op, ln->tn_type, ln, cn);
   2706 
   2707 	return ntn;
   2708 }
   2709 
   2710 /*
   2711  * Create a node for REAL, IMAG
   2712  */
   2713 static tnode_t *
   2714 build_real_imag(op_t op, tnode_t *ln)
   2715 {
   2716 	tnode_t	*cn, *ntn;
   2717 
   2718 	lint_assert(ln != NULL);
   2719 
   2720 	switch (ln->tn_type->t_tspec) {
   2721 	case LCOMPLEX:
   2722 		/* XXX: integer and LDOUBLE don't match. */
   2723 		cn = new_integer_constant_node(LDOUBLE, (int64_t)1);
   2724 		break;
   2725 	case DCOMPLEX:
   2726 		cn = new_integer_constant_node(DOUBLE, (int64_t)1);
   2727 		break;
   2728 	case FCOMPLEX:
   2729 		cn = new_integer_constant_node(FLOAT, (int64_t)1);
   2730 		break;
   2731 	default:
   2732 		/* __%s__ is illegal for type %s */
   2733 		error(276, op == REAL ? "real" : "imag",
   2734 		    type_name(ln->tn_type));
   2735 		return NULL;
   2736 	}
   2737 	ntn = new_tnode(op, cn->tn_type, ln, cn);
   2738 	ntn->tn_lvalue = true;
   2739 
   2740 	return ntn;
   2741 }
   2742 /*
   2743  * Create a tree node for the unary & operator
   2744  */
   2745 static tnode_t *
   2746 build_address(tnode_t *tn, bool noign)
   2747 {
   2748 	tspec_t	t;
   2749 
   2750 	if (!noign && ((t = tn->tn_type->t_tspec) == ARRAY || t == FUNC)) {
   2751 		if (tflag)
   2752 			/* '&' before array or function: ignored */
   2753 			warning(127);
   2754 		return tn;
   2755 	}
   2756 
   2757 	/* eliminate &* */
   2758 	if (tn->tn_op == INDIR &&
   2759 	    tn->tn_left->tn_type->t_tspec == PTR &&
   2760 	    tn->tn_left->tn_type->t_subt == tn->tn_type) {
   2761 		return tn->tn_left;
   2762 	}
   2763 
   2764 	return new_tnode(ADDR, tincref(tn->tn_type, PTR), tn, NULL);
   2765 }
   2766 
   2767 /*
   2768  * Create a node for operators PLUS and MINUS.
   2769  */
   2770 static tnode_t *
   2771 build_plus_minus(op_t op, tnode_t *ln, tnode_t *rn)
   2772 {
   2773 	tnode_t	*ntn, *ctn;
   2774 	type_t	*tp;
   2775 
   2776 	/* If pointer and integer, then pointer to the lhs. */
   2777 	if (rn->tn_type->t_tspec == PTR && is_integer(ln->tn_type->t_tspec)) {
   2778 		ntn = ln;
   2779 		ln = rn;
   2780 		rn = ntn;
   2781 	}
   2782 
   2783 	if (ln->tn_type->t_tspec == PTR && rn->tn_type->t_tspec != PTR) {
   2784 
   2785 		lint_assert(is_integer(rn->tn_type->t_tspec));
   2786 
   2787 		ctn = plength(ln->tn_type);
   2788 		if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
   2789 			rn = convert(NOOP, 0, ctn->tn_type, rn);
   2790 		rn = new_tnode(MULT, rn->tn_type, rn, ctn);
   2791 		if (rn->tn_left->tn_op == CON)
   2792 			rn = fold(rn);
   2793 		ntn = new_tnode(op, ln->tn_type, ln, rn);
   2794 
   2795 	} else if (rn->tn_type->t_tspec == PTR) {
   2796 
   2797 		lint_assert(ln->tn_type->t_tspec == PTR);
   2798 		lint_assert(op == MINUS);
   2799 		tp = gettyp(PTRDIFF_TSPEC);
   2800 		ntn = new_tnode(op, tp, ln, rn);
   2801 		if (ln->tn_op == CON && rn->tn_op == CON)
   2802 			ntn = fold(ntn);
   2803 		ctn = plength(ln->tn_type);
   2804 		balance(NOOP, &ntn, &ctn);
   2805 		ntn = new_tnode(DIV, tp, ntn, ctn);
   2806 
   2807 	} else {
   2808 
   2809 		ntn = new_tnode(op, ln->tn_type, ln, rn);
   2810 
   2811 	}
   2812 	return ntn;
   2813 }
   2814 
   2815 /*
   2816  * Create a node for operators SHL and SHR.
   2817  */
   2818 static tnode_t *
   2819 build_bit_shift(op_t op, tnode_t *ln, tnode_t *rn)
   2820 {
   2821 	tspec_t	t;
   2822 	tnode_t	*ntn;
   2823 
   2824 	if ((t = rn->tn_type->t_tspec) != INT && t != UINT)
   2825 		rn = convert(CVT, 0, gettyp(INT), rn);
   2826 	ntn = new_tnode(op, ln->tn_type, ln, rn);
   2827 	return ntn;
   2828 }
   2829 
   2830 /*
   2831  * Create a node for COLON.
   2832  */
   2833 static tnode_t *
   2834 build_colon(tnode_t *ln, tnode_t *rn)
   2835 {
   2836 	tspec_t	lt, rt, pdt;
   2837 	type_t	*rtp;
   2838 	tnode_t	*ntn;
   2839 
   2840 	lt = ln->tn_type->t_tspec;
   2841 	rt = rn->tn_type->t_tspec;
   2842 	pdt = PTRDIFF_TSPEC;
   2843 
   2844 	/*
   2845 	 * Arithmetic types are balanced, all other type combinations
   2846 	 * still need to be handled.
   2847 	 */
   2848 	if (is_arithmetic(lt) && is_arithmetic(rt)) {
   2849 		rtp = ln->tn_type;
   2850 	} else if (lt == BOOL && rt == BOOL) {
   2851 		rtp = ln->tn_type;
   2852 	} else if (lt == VOID || rt == VOID) {
   2853 		rtp = gettyp(VOID);
   2854 	} else if (lt == STRUCT || lt == UNION) {
   2855 		/* Both types must be identical. */
   2856 		lint_assert(rt == STRUCT || rt == UNION);
   2857 		lint_assert(ln->tn_type->t_str == rn->tn_type->t_str);
   2858 		if (is_incomplete(ln->tn_type)) {
   2859 			/* unknown operand size, op %s */
   2860 			error(138, modtab[COLON].m_name);
   2861 			return NULL;
   2862 		}
   2863 		rtp = ln->tn_type;
   2864 	} else if (lt == PTR && is_integer(rt)) {
   2865 		if (rt != pdt) {
   2866 			rn = convert(NOOP, 0, gettyp(pdt), rn);
   2867 			rt = pdt;
   2868 		}
   2869 		rtp = ln->tn_type;
   2870 	} else if (rt == PTR && is_integer(lt)) {
   2871 		if (lt != pdt) {
   2872 			ln = convert(NOOP, 0, gettyp(pdt), ln);
   2873 			lt = pdt;
   2874 		}
   2875 		rtp = rn->tn_type;
   2876 	} else if (lt == PTR && ln->tn_type->t_subt->t_tspec == VOID) {
   2877 		lint_assert(rt == PTR);
   2878 		rtp = rn->tn_type;
   2879 		merge_qualifiers(&rtp, ln->tn_type, rn->tn_type);
   2880 	} else if (rt == PTR && rn->tn_type->t_subt->t_tspec == VOID) {
   2881 		lint_assert(lt == PTR);
   2882 		rtp = ln->tn_type;
   2883 		merge_qualifiers(&rtp, ln->tn_type, rn->tn_type);
   2884 	} else {
   2885 		lint_assert(lt == PTR);
   2886 		lint_assert(rt == PTR);
   2887 		/*
   2888 		 * XXX For now we simply take the left type. This is
   2889 		 * probably wrong, if one type contains a function prototype
   2890 		 * and the other one, at the same place, only an old style
   2891 		 * declaration.
   2892 		 */
   2893 		rtp = ln->tn_type;
   2894 		merge_qualifiers(&rtp, ln->tn_type, rn->tn_type);
   2895 	}
   2896 
   2897 	ntn = new_tnode(COLON, rtp, ln, rn);
   2898 
   2899 	return ntn;
   2900 }
   2901 
   2902 /*
   2903  * Create a node for an assignment operator (both = and op= ).
   2904  */
   2905 static tnode_t *
   2906 build_assignment(op_t op, tnode_t *ln, tnode_t *rn)
   2907 {
   2908 	tspec_t	lt, rt;
   2909 	tnode_t	*ntn, *ctn;
   2910 
   2911 	lint_assert(ln != NULL);
   2912 	lint_assert(rn != NULL);
   2913 
   2914 	lt = ln->tn_type->t_tspec;
   2915 	rt = rn->tn_type->t_tspec;
   2916 
   2917 	if ((op == ADDASS || op == SUBASS) && lt == PTR) {
   2918 		lint_assert(is_integer(rt));
   2919 		ctn = plength(ln->tn_type);
   2920 		if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
   2921 			rn = convert(NOOP, 0, ctn->tn_type, rn);
   2922 		rn = new_tnode(MULT, rn->tn_type, rn, ctn);
   2923 		if (rn->tn_left->tn_op == CON)
   2924 			rn = fold(rn);
   2925 	}
   2926 
   2927 	if ((op == ASSIGN || op == RETURN) && (lt == STRUCT || rt == STRUCT)) {
   2928 		lint_assert(lt == rt);
   2929 		lint_assert(ln->tn_type->t_str == rn->tn_type->t_str);
   2930 		if (is_incomplete(ln->tn_type)) {
   2931 			if (op == RETURN) {
   2932 				/* cannot return incomplete type */
   2933 				error(212);
   2934 			} else {
   2935 				/* unknown operand size, op %s */
   2936 				error(138, modtab[op].m_name);
   2937 			}
   2938 			return NULL;
   2939 		}
   2940 	}
   2941 
   2942 	if (op == SHLASS) {
   2943 		if (portable_size_in_bits(lt) < portable_size_in_bits(rt)) {
   2944 			if (hflag)
   2945 				/* semantics of '%s' change in ANSI C; ... */
   2946 				warning(118, "<<=");
   2947 		}
   2948 	} else if (op != SHRASS) {
   2949 		if (op == ASSIGN || lt != PTR) {
   2950 			if (lt != rt ||
   2951 			    (ln->tn_type->t_bitfield && rn->tn_op == CON)) {
   2952 				rn = convert(op, 0, ln->tn_type, rn);
   2953 				rt = lt;
   2954 			}
   2955 		}
   2956 	}
   2957 
   2958 	ntn = new_tnode(op, ln->tn_type, ln, rn);
   2959 
   2960 	return ntn;
   2961 }
   2962 
   2963 /*
   2964  * Get length of type tp->t_subt.
   2965  */
   2966 static tnode_t *
   2967 plength(type_t *tp)
   2968 {
   2969 	int	elem, elsz;
   2970 
   2971 	lint_assert(tp->t_tspec == PTR);
   2972 	tp = tp->t_subt;
   2973 
   2974 	elem = 1;
   2975 	elsz = 0;
   2976 
   2977 	while (tp->t_tspec == ARRAY) {
   2978 		elem *= tp->t_dim;
   2979 		tp = tp->t_subt;
   2980 	}
   2981 
   2982 	switch (tp->t_tspec) {
   2983 	case FUNC:
   2984 		/* pointer to function is not allowed here */
   2985 		error(110);
   2986 		break;
   2987 	case VOID:
   2988 		/* cannot do pointer arithmetic on operand of unknown size */
   2989 		gnuism(136);
   2990 		break;
   2991 	case STRUCT:
   2992 	case UNION:
   2993 		if ((elsz = tp->t_str->sou_size_in_bits) == 0)
   2994 			/* cannot do pointer arithmetic on operand of ... */
   2995 			error(136);
   2996 		break;
   2997 	case ENUM:
   2998 		if (is_incomplete(tp)) {
   2999 			/* cannot do pointer arithmetic on operand of ... */
   3000 			warning(136);
   3001 		}
   3002 		/* FALLTHROUGH */
   3003 	default:
   3004 		if ((elsz = size_in_bits(tp->t_tspec)) == 0) {
   3005 			/* cannot do pointer arithmetic on operand of ... */
   3006 			error(136);
   3007 		} else {
   3008 			lint_assert(elsz != -1);
   3009 		}
   3010 		break;
   3011 	}
   3012 
   3013 	if (elem == 0 && elsz != 0) {
   3014 		/* cannot do pointer arithmetic on operand of unknown size */
   3015 		error(136);
   3016 	}
   3017 
   3018 	if (elsz == 0)
   3019 		elsz = CHAR_SIZE;
   3020 
   3021 	return new_integer_constant_node(PTRDIFF_TSPEC,
   3022 	    (int64_t)(elem * elsz / CHAR_SIZE));
   3023 }
   3024 
   3025 /*
   3026  * XXX
   3027  * Note: There appear to be a number of bugs in detecting overflow in
   3028  * this function. An audit and a set of proper regression tests are needed.
   3029  *     --Perry Metzger, Nov. 16, 2001
   3030  */
   3031 /*
   3032  * Do only as much as necessary to compute constant expressions.
   3033  * Called only if the operator allows folding and all operands are constants.
   3034  */
   3035 static tnode_t *
   3036 fold(tnode_t *tn)
   3037 {
   3038 	val_t	*v;
   3039 	tspec_t	t;
   3040 	bool	utyp, ovfl;
   3041 	int64_t	sl, sr = 0, q = 0, mask;
   3042 	uint64_t ul, ur = 0;
   3043 	tnode_t	*cn;
   3044 
   3045 	v = xcalloc(1, sizeof (val_t));
   3046 	v->v_tspec = t = tn->tn_type->t_tspec;
   3047 
   3048 	utyp = t == PTR || is_uinteger(t);
   3049 	ul = sl = tn->tn_left->tn_val->v_quad;
   3050 	if (modtab[tn->tn_op].m_binary)
   3051 		ur = sr = tn->tn_right->tn_val->v_quad;
   3052 
   3053 	mask = qlmasks[size_in_bits(t)];
   3054 	ovfl = false;
   3055 
   3056 	switch (tn->tn_op) {
   3057 	case UPLUS:
   3058 		q = sl;
   3059 		break;
   3060 	case UMINUS:
   3061 		q = -sl;
   3062 		if (sl != 0 && msb(q, t, -1) == msb(sl, t, -1))
   3063 			ovfl = true;
   3064 		break;
   3065 	case COMPL:
   3066 		q = ~sl;
   3067 		break;
   3068 	case MULT:
   3069 		if (utyp) {
   3070 			q = ul * ur;
   3071 			if (q != (q & mask))
   3072 				ovfl = true;
   3073 			else if ((ul != 0) && ((q / ul) != ur))
   3074 				ovfl = true;
   3075 		} else {
   3076 			q = sl * sr;
   3077 			if (msb(q, t, -1) != (msb(sl, t, -1) ^ msb(sr, t, -1)))
   3078 				ovfl = true;
   3079 		}
   3080 		break;
   3081 	case DIV:
   3082 		if (sr == 0) {
   3083 			/* division by 0 */
   3084 			error(139);
   3085 			q = utyp ? UQUAD_MAX : QUAD_MAX;
   3086 		} else {
   3087 			q = utyp ? (int64_t)(ul / ur) : sl / sr;
   3088 		}
   3089 		break;
   3090 	case MOD:
   3091 		if (sr == 0) {
   3092 			/* modulus by 0 */
   3093 			error(140);
   3094 			q = 0;
   3095 		} else {
   3096 			q = utyp ? (int64_t)(ul % ur) : sl % sr;
   3097 		}
   3098 		break;
   3099 	case PLUS:
   3100 		q = utyp ? (int64_t)(ul + ur) : sl + sr;
   3101 		if (msb(sl, t, -1)  != 0 && msb(sr, t, -1) != 0) {
   3102 			if (msb(q, t, -1) == 0)
   3103 				ovfl = true;
   3104 		} else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) == 0) {
   3105 			if (msb(q, t, -1) != 0)
   3106 				ovfl = true;
   3107 		}
   3108 		break;
   3109 	case MINUS:
   3110 		q = utyp ? (int64_t)(ul - ur) : sl - sr;
   3111 		if (msb(sl, t, -1) != 0 && msb(sr, t, -1) == 0) {
   3112 			if (msb(q, t, -1) == 0)
   3113 				ovfl = true;
   3114 		} else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) != 0) {
   3115 			if (msb(q, t, -1) != 0)
   3116 				ovfl = true;
   3117 		}
   3118 		break;
   3119 	case SHL:
   3120 		q = utyp ? (int64_t)(ul << sr) : sl << sr;
   3121 		break;
   3122 	case SHR:
   3123 		/*
   3124 		 * The sign must be explicitly extended because
   3125 		 * shifts of signed values are implementation dependent.
   3126 		 */
   3127 		q = ul >> sr;
   3128 		q = xsign(q, t, size_in_bits(t) - (int)sr);
   3129 		break;
   3130 	case LT:
   3131 		q = (utyp ? ul < ur : sl < sr) ? 1 : 0;
   3132 		break;
   3133 	case LE:
   3134 		q = (utyp ? ul <= ur : sl <= sr) ? 1 : 0;
   3135 		break;
   3136 	case GE:
   3137 		q = (utyp ? ul >= ur : sl >= sr) ? 1 : 0;
   3138 		break;
   3139 	case GT:
   3140 		q = (utyp ? ul > ur : sl > sr) ? 1 : 0;
   3141 		break;
   3142 	case EQ:
   3143 		q = (utyp ? ul == ur : sl == sr) ? 1 : 0;
   3144 		break;
   3145 	case NE:
   3146 		q = (utyp ? ul != ur : sl != sr) ? 1 : 0;
   3147 		break;
   3148 	case BITAND:
   3149 		q = utyp ? (int64_t)(ul & ur) : sl & sr;
   3150 		break;
   3151 	case BITXOR:
   3152 		q = utyp ? (int64_t)(ul ^ ur) : sl ^ sr;
   3153 		break;
   3154 	case BITOR:
   3155 		q = utyp ? (int64_t)(ul | ur) : sl | sr;
   3156 		break;
   3157 	default:
   3158 		lint_assert(/*CONSTCOND*/false);
   3159 	}
   3160 
   3161 	/* XXX does not work for quads. */
   3162 	if (ovfl || ((uint64_t)(q | mask) != ~(uint64_t)0 &&
   3163 	    (q & ~mask) != 0)) {
   3164 		if (hflag)
   3165 			/* integer overflow detected, op %s */
   3166 			warning(141, modtab[tn->tn_op].m_name);
   3167 	}
   3168 
   3169 	v->v_quad = xsign(q, t, -1);
   3170 
   3171 	cn = new_constant_node(tn->tn_type, v);
   3172 	if (tn->tn_left->tn_system_dependent)
   3173 		cn->tn_system_dependent = true;
   3174 	if (modtab[tn->tn_op].m_binary && tn->tn_right->tn_system_dependent)
   3175 		cn->tn_system_dependent = true;
   3176 
   3177 	return cn;
   3178 }
   3179 
   3180 /*
   3181  * Fold constant nodes, as much as is needed for comparing the value with 0
   3182  * (test context, for controlling expressions).
   3183  */
   3184 static tnode_t *
   3185 fold_test(tnode_t *tn)
   3186 {
   3187 	bool	l, r;
   3188 	val_t	*v;
   3189 
   3190 	v = xcalloc(1, sizeof (val_t));
   3191 	v->v_tspec = tn->tn_type->t_tspec;
   3192 	lint_assert(v->v_tspec == INT || (Tflag && v->v_tspec == BOOL));
   3193 
   3194 	l = constant_is_nonzero(tn->tn_left);
   3195 	r = modtab[tn->tn_op].m_binary && constant_is_nonzero(tn->tn_right);
   3196 
   3197 	switch (tn->tn_op) {
   3198 	case NOT:
   3199 		if (hflag && !constcond_flag)
   3200 			/* constant argument to NOT */
   3201 			warning(239);
   3202 		v->v_quad = !l ? 1 : 0;
   3203 		break;
   3204 	case LOGAND:
   3205 		v->v_quad = l && r ? 1 : 0;
   3206 		break;
   3207 	case LOGOR:
   3208 		v->v_quad = l || r ? 1 : 0;
   3209 		break;
   3210 	default:
   3211 		lint_assert(/*CONSTCOND*/false);
   3212 	}
   3213 
   3214 	return new_constant_node(tn->tn_type, v);
   3215 }
   3216 
   3217 /*
   3218  * Fold constant nodes having operands with floating point type.
   3219  */
   3220 static tnode_t *
   3221 fold_float(tnode_t *tn)
   3222 {
   3223 	val_t	*v;
   3224 	tspec_t	t;
   3225 	ldbl_t	l, r = 0;
   3226 
   3227 	fpe = 0;
   3228 	v = xcalloc(1, sizeof (val_t));
   3229 	v->v_tspec = t = tn->tn_type->t_tspec;
   3230 
   3231 	lint_assert(is_floating(t));
   3232 	lint_assert(t == tn->tn_left->tn_type->t_tspec);
   3233 	lint_assert(!modtab[tn->tn_op].m_binary ||
   3234 	    t == tn->tn_right->tn_type->t_tspec);
   3235 
   3236 	l = tn->tn_left->tn_val->v_ldbl;
   3237 	if (modtab[tn->tn_op].m_binary)
   3238 		r = tn->tn_right->tn_val->v_ldbl;
   3239 
   3240 	switch (tn->tn_op) {
   3241 	case UPLUS:
   3242 		v->v_ldbl = l;
   3243 		break;
   3244 	case UMINUS:
   3245 		v->v_ldbl = -l;
   3246 		break;
   3247 	case MULT:
   3248 		v->v_ldbl = l * r;
   3249 		break;
   3250 	case DIV:
   3251 		if (r == 0.0) {
   3252 			/* division by 0 */
   3253 			error(139);
   3254 			if (t == FLOAT) {
   3255 				v->v_ldbl = l < 0 ? -FLT_MAX : FLT_MAX;
   3256 			} else if (t == DOUBLE) {
   3257 				v->v_ldbl = l < 0 ? -DBL_MAX : DBL_MAX;
   3258 			} else {
   3259 				v->v_ldbl = l < 0 ? -LDBL_MAX : LDBL_MAX;
   3260 			}
   3261 		} else {
   3262 			v->v_ldbl = l / r;
   3263 		}
   3264 		break;
   3265 	case PLUS:
   3266 		v->v_ldbl = l + r;
   3267 		break;
   3268 	case MINUS:
   3269 		v->v_ldbl = l - r;
   3270 		break;
   3271 	case LT:
   3272 		v->v_quad = l < r ? 1 : 0;
   3273 		break;
   3274 	case LE:
   3275 		v->v_quad = l <= r ? 1 : 0;
   3276 		break;
   3277 	case GE:
   3278 		v->v_quad = l >= r ? 1 : 0;
   3279 		break;
   3280 	case GT:
   3281 		v->v_quad = l > r ? 1 : 0;
   3282 		break;
   3283 	case EQ:
   3284 		v->v_quad = l == r ? 1 : 0;
   3285 		break;
   3286 	case NE:
   3287 		v->v_quad = l != r ? 1 : 0;
   3288 		break;
   3289 	default:
   3290 		lint_assert(/*CONSTCOND*/false);
   3291 	}
   3292 
   3293 	lint_assert(fpe != 0 || isnan((double)v->v_ldbl) == 0);
   3294 	if (fpe != 0 || finite((double)v->v_ldbl) == 0 ||
   3295 	    (t == FLOAT &&
   3296 	     (v->v_ldbl > FLT_MAX || v->v_ldbl < -FLT_MAX)) ||
   3297 	    (t == DOUBLE &&
   3298 	     (v->v_ldbl > DBL_MAX || v->v_ldbl < -DBL_MAX))) {
   3299 		/* floating point overflow detected, op %s */
   3300 		warning(142, modtab[tn->tn_op].m_name);
   3301 		if (t == FLOAT) {
   3302 			v->v_ldbl = v->v_ldbl < 0 ? -FLT_MAX : FLT_MAX;
   3303 		} else if (t == DOUBLE) {
   3304 			v->v_ldbl = v->v_ldbl < 0 ? -DBL_MAX : DBL_MAX;
   3305 		} else {
   3306 			v->v_ldbl = v->v_ldbl < 0 ? -LDBL_MAX: LDBL_MAX;
   3307 		}
   3308 	    fpe = 0;
   3309 	}
   3310 
   3311 	return new_constant_node(tn->tn_type, v);
   3312 }
   3313 
   3314 
   3315 /*
   3316  * Create a constant node for sizeof.
   3317  */
   3318 tnode_t *
   3319 build_sizeof(type_t *tp)
   3320 {
   3321 	int64_t size_in_bytes = type_size_in_bits(tp) / CHAR_SIZE;
   3322 	tnode_t *tn = new_integer_constant_node(SIZEOF_TSPEC, size_in_bytes);
   3323 	tn->tn_system_dependent = true;
   3324 	return tn;
   3325 }
   3326 
   3327 /*
   3328  * Create a constant node for offsetof.
   3329  */
   3330 tnode_t *
   3331 build_offsetof(type_t *tp, sym_t *sym)
   3332 {
   3333 	tspec_t t = tp->t_tspec;
   3334 	if (t != STRUCT && t != UNION)
   3335 		/* unacceptable operand of '%s' */
   3336 		error(111, "offsetof");
   3337 
   3338 	// XXX: wrong size, no checking for sym fixme
   3339 	int64_t offset_in_bytes = type_size_in_bits(tp) / CHAR_SIZE;
   3340 	tnode_t *tn = new_integer_constant_node(SIZEOF_TSPEC, offset_in_bytes);
   3341 	tn->tn_system_dependent = true;
   3342 	return tn;
   3343 }
   3344 
   3345 int64_t
   3346 type_size_in_bits(type_t *tp)
   3347 {
   3348 	int	elem, elsz;
   3349 	bool	flex;
   3350 
   3351 	elem = 1;
   3352 	flex = false;
   3353 	while (tp->t_tspec == ARRAY) {
   3354 		flex = true;	/* allow c99 flex arrays [] [0] */
   3355 		elem *= tp->t_dim;
   3356 		tp = tp->t_subt;
   3357 	}
   3358 	if (elem == 0) {
   3359 		if (!flex) {
   3360 			/* cannot take size/alignment of incomplete type */
   3361 			error(143);
   3362 			elem = 1;
   3363 		}
   3364 	}
   3365 	switch (tp->t_tspec) {
   3366 	case FUNC:
   3367 		/* cannot take size/alignment of function */
   3368 		error(144);
   3369 		elsz = 1;
   3370 		break;
   3371 	case STRUCT:
   3372 	case UNION:
   3373 		if (is_incomplete(tp)) {
   3374 			/* cannot take size/alignment of incomplete type */
   3375 			error(143);
   3376 			elsz = 1;
   3377 		} else {
   3378 			elsz = tp->t_str->sou_size_in_bits;
   3379 		}
   3380 		break;
   3381 	case ENUM:
   3382 		if (is_incomplete(tp)) {
   3383 			/* cannot take size/alignment of incomplete type */
   3384 			warning(143);
   3385 		}
   3386 		/* FALLTHROUGH */
   3387 	default:
   3388 		if (tp->t_bitfield) {
   3389 			/* cannot take size/alignment of bit-field */
   3390 			error(145);
   3391 		}
   3392 		if (tp->t_tspec == VOID) {
   3393 			/* cannot take size/alignment of void */
   3394 			error(146);
   3395 			elsz = 1;
   3396 		} else {
   3397 			elsz = size_in_bits(tp->t_tspec);
   3398 			lint_assert(elsz > 0);
   3399 		}
   3400 		break;
   3401 	}
   3402 
   3403 	return (int64_t)elem * elsz;
   3404 }
   3405 
   3406 tnode_t *
   3407 build_alignof(type_t *tp)
   3408 {
   3409 	switch (tp->t_tspec) {
   3410 	case ARRAY:
   3411 		break;
   3412 
   3413 	case FUNC:
   3414 		/* cannot take size/alignment of function */
   3415 		error(144);
   3416 		return 0;
   3417 
   3418 	case STRUCT:
   3419 	case UNION:
   3420 		if (is_incomplete(tp)) {
   3421 			/* cannot take size/alignment of incomplete type */
   3422 			error(143);
   3423 			return 0;
   3424 		}
   3425 		break;
   3426 	case ENUM:
   3427 		break;
   3428 	default:
   3429 		if (tp->t_bitfield) {
   3430 			/* cannot take size/alignment of bit-field */
   3431 			error(145);
   3432 			return 0;
   3433 		}
   3434 		if (tp->t_tspec == VOID) {
   3435 			/* cannot take size/alignment of void */
   3436 			error(146);
   3437 			return 0;
   3438 		}
   3439 		break;
   3440 	}
   3441 
   3442 	return new_integer_constant_node(SIZEOF_TSPEC,
   3443 	    (int64_t)alignment_in_bits(tp) / CHAR_SIZE);
   3444 }
   3445 
   3446 /*
   3447  * Type casts.
   3448  */
   3449 tnode_t *
   3450 cast(tnode_t *tn, type_t *tp)
   3451 {
   3452 	tspec_t	nt, ot;
   3453 
   3454 	if (tn == NULL)
   3455 		return NULL;
   3456 
   3457 	/*
   3458 	 * XXX: checking for tp == NULL is only a quick fix for PR 22119.
   3459 	 *  The proper fix needs to be investigated properly.
   3460 	 *  See d_pr_22119.c for how to get here.
   3461 	 */
   3462 	if (tp == NULL)
   3463 		return NULL;
   3464 
   3465 	tn = cconv(tn);
   3466 
   3467 	nt = tp->t_tspec;
   3468 	ot = tn->tn_type->t_tspec;
   3469 
   3470 	if (nt == VOID) {
   3471 		/*
   3472 		 * XXX ANSI C requires scalar types or void (Plauger & Brodie).
   3473 		 * But this seems really questionable.
   3474 		 */
   3475 	} else if (nt == UNION) {
   3476 		sym_t *m;
   3477 		struct_or_union *str = tp->t_str;
   3478 		if (!Sflag) {
   3479 			/* union cast is a C9X feature */
   3480 			error(328);
   3481 			return NULL;
   3482 		}
   3483 		for (m = str->sou_first_member; m != NULL; m = m->s_next) {
   3484 			if (sametype(m->s_type, tn->tn_type)) {
   3485 				tn = getnode();
   3486 				tn->tn_op = CVT;
   3487 				tn->tn_type = tp;
   3488 				tn->tn_cast = true;
   3489 				tn->tn_right = NULL;
   3490 				return tn;
   3491 			}
   3492 		}
   3493 		/* type '%s' is not a member of '%s' */
   3494 		error(329, type_name(tn->tn_type), type_name(tp));
   3495 		return NULL;
   3496 	} else if (nt == STRUCT || nt == ARRAY || nt == FUNC) {
   3497 		if (!Sflag || nt == ARRAY || nt == FUNC) {
   3498 			/* invalid cast expression */
   3499 			error(147);
   3500 			return NULL;
   3501 		}
   3502 	} else if (ot == STRUCT || ot == UNION) {
   3503 		/* invalid cast expression */
   3504 		error(147);
   3505 		return NULL;
   3506 	} else if (ot == VOID) {
   3507 		/* improper cast of void expression */
   3508 		error(148);
   3509 		return NULL;
   3510 	} else if (is_integer(nt) && is_scalar(ot)) {
   3511 		/* ok */
   3512 	} else if (is_floating(nt) && is_arithmetic(ot)) {
   3513 		/* ok */
   3514 	} else if (nt == PTR && is_integer(ot)) {
   3515 		/* ok */
   3516 	} else if (nt == PTR && ot == PTR) {
   3517 		if (!tp->t_subt->t_const && tn->tn_type->t_subt->t_const) {
   3518 			if (hflag)
   3519 				/* cast discards 'const' from type '%s' */
   3520 				warning(275, type_name(tn->tn_type));
   3521 		}
   3522 	} else {
   3523 		/* invalid cast expression */
   3524 		error(147);
   3525 		return NULL;
   3526 	}
   3527 
   3528 	tn = convert(CVT, 0, tp, tn);
   3529 	tn->tn_cast = true;
   3530 
   3531 	return tn;
   3532 }
   3533 
   3534 /*
   3535  * Create the node for a function argument.
   3536  * All necessary conversions and type checks are done in
   3537  * new_function_call_node because new_function_argument_node has no
   3538  * information about expected argument types.
   3539  */
   3540 tnode_t *
   3541 new_function_argument_node(tnode_t *args, tnode_t *arg)
   3542 {
   3543 	tnode_t	*ntn;
   3544 
   3545 	/*
   3546 	 * If there was a serious error in the expression for the argument,
   3547 	 * create a dummy argument so the positions of the remaining arguments
   3548 	 * will not change.
   3549 	 */
   3550 	if (arg == NULL)
   3551 		arg = new_integer_constant_node(INT, (int64_t)0);
   3552 
   3553 	ntn = new_tnode(PUSH, arg->tn_type, arg, args);
   3554 
   3555 	return ntn;
   3556 }
   3557 
   3558 /*
   3559  * Create the node for a function call. Also check types of
   3560  * function arguments and insert conversions, if necessary.
   3561  */
   3562 tnode_t *
   3563 new_function_call_node(tnode_t *func, tnode_t *args)
   3564 {
   3565 	tnode_t	*ntn;
   3566 	op_t	fcop;
   3567 
   3568 	if (func == NULL)
   3569 		return NULL;
   3570 
   3571 	if (func->tn_op == NAME && func->tn_type->t_tspec == FUNC) {
   3572 		fcop = CALL;
   3573 	} else {
   3574 		fcop = ICALL;
   3575 	}
   3576 
   3577 	/*
   3578 	 * after cconv() func will always be a pointer to a function
   3579 	 * if it is a valid function designator.
   3580 	 */
   3581 	func = cconv(func);
   3582 
   3583 	if (func->tn_type->t_tspec != PTR ||
   3584 	    func->tn_type->t_subt->t_tspec != FUNC) {
   3585 		/* illegal function (type %s) */
   3586 		error(149, type_name(func->tn_type));
   3587 		return NULL;
   3588 	}
   3589 
   3590 	args = check_function_arguments(func->tn_type->t_subt, args);
   3591 
   3592 	ntn = new_tnode(fcop, func->tn_type->t_subt->t_subt, func, args);
   3593 
   3594 	return ntn;
   3595 }
   3596 
   3597 /*
   3598  * Check types of all function arguments and insert conversions,
   3599  * if necessary.
   3600  */
   3601 static tnode_t *
   3602 check_function_arguments(type_t *ftp, tnode_t *args)
   3603 {
   3604 	tnode_t	*arg;
   3605 	sym_t	*asym;
   3606 	tspec_t	at;
   3607 	int	narg, npar, n, i;
   3608 
   3609 	/* get # of args in the prototype */
   3610 	npar = 0;
   3611 	for (asym = ftp->t_args; asym != NULL; asym = asym->s_next)
   3612 		npar++;
   3613 
   3614 	/* get # of args in function call */
   3615 	narg = 0;
   3616 	for (arg = args; arg != NULL; arg = arg->tn_right)
   3617 		narg++;
   3618 
   3619 	asym = ftp->t_args;
   3620 	if (ftp->t_proto && npar != narg && !(ftp->t_vararg && npar < narg)) {
   3621 		/* argument mismatch: %d arg%s passed, %d expected */
   3622 		error(150, narg, narg > 1 ? "s" : "", npar);
   3623 		asym = NULL;
   3624 	}
   3625 
   3626 	for (n = 1; n <= narg; n++) {
   3627 
   3628 		/*
   3629 		 * The rightmost argument is at the top of the argument
   3630 		 * subtree.
   3631 		 */
   3632 		for (i = narg, arg = args; i > n; i--, arg = arg->tn_right)
   3633 			continue;
   3634 
   3635 		/* some things which are always not allowed */
   3636 		if ((at = arg->tn_left->tn_type->t_tspec) == VOID) {
   3637 			/* void expressions may not be arguments, arg #%d */
   3638 			error(151, n);
   3639 			return NULL;
   3640 		} else if ((at == STRUCT || at == UNION) &&
   3641 			   is_incomplete(arg->tn_left->tn_type)) {
   3642 			/* argument cannot have unknown size, arg #%d */
   3643 			error(152, n);
   3644 			return NULL;
   3645 		} else if (is_integer(at) &&
   3646 			   arg->tn_left->tn_type->t_is_enum &&
   3647 			   is_incomplete(arg->tn_left->tn_type)) {
   3648 			/* argument cannot have unknown size, arg #%d */
   3649 			warning(152, n);
   3650 		}
   3651 
   3652 		/* class conversions (arg in value context) */
   3653 		arg->tn_left = cconv(arg->tn_left);
   3654 
   3655 		if (asym != NULL) {
   3656 			arg->tn_left = check_prototype_argument(
   3657 			    n, asym->s_type, arg->tn_left);
   3658 		} else {
   3659 			arg->tn_left = promote(NOOP, true, arg->tn_left);
   3660 		}
   3661 		arg->tn_type = arg->tn_left->tn_type;
   3662 
   3663 		if (asym != NULL)
   3664 			asym = asym->s_next;
   3665 	}
   3666 
   3667 	return args;
   3668 }
   3669 
   3670 /*
   3671  * Compare the type of an argument with the corresponding type of a
   3672  * prototype parameter. If it is a valid combination, but both types
   3673  * are not the same, insert a conversion to convert the argument into
   3674  * the type of the parameter.
   3675  */
   3676 static tnode_t *
   3677 check_prototype_argument(
   3678 	int	n,		/* pos of arg */
   3679 	type_t	*tp,		/* expected type (from prototype) */
   3680 	tnode_t	*tn)		/* argument */
   3681 {
   3682 	tnode_t	*ln;
   3683 	bool	dowarn;
   3684 
   3685 	ln = xcalloc(1, sizeof (tnode_t));
   3686 	ln->tn_type = tduptyp(tp);
   3687 	ln->tn_type->t_const = false;
   3688 	ln->tn_lvalue = true;
   3689 	if (typeok(FARG, n, ln, tn)) {
   3690 		if (!eqtype(tp, tn->tn_type,
   3691 		    true, false, (dowarn = false, &dowarn)) || dowarn)
   3692 			tn = convert(FARG, n, tp, tn);
   3693 	}
   3694 	free(ln);
   3695 	return tn;
   3696 }
   3697 
   3698 /*
   3699  * Return the value of an integral constant expression.
   3700  * If the expression is not constant or its type is not an integer
   3701  * type, an error message is printed.
   3702  */
   3703 val_t *
   3704 constant(tnode_t *tn, bool required)
   3705 {
   3706 	val_t	*v;
   3707 
   3708 	if (tn != NULL)
   3709 		tn = cconv(tn);
   3710 	if (tn != NULL)
   3711 		tn = promote(NOOP, false, tn);
   3712 
   3713 	v = xcalloc(1, sizeof (val_t));
   3714 
   3715 	if (tn == NULL) {
   3716 		lint_assert(nerr != 0);
   3717 		if (dflag)
   3718 			printf("constant node is null; returning 1 instead\n");
   3719 		v->v_tspec = INT;
   3720 		v->v_quad = 1;
   3721 		return v;
   3722 	}
   3723 
   3724 	v->v_tspec = tn->tn_type->t_tspec;
   3725 
   3726 	if (tn->tn_op == CON) {
   3727 		lint_assert(tn->tn_type->t_tspec == tn->tn_val->v_tspec);
   3728 		if (is_integer(tn->tn_val->v_tspec)) {
   3729 			v->v_ansiu = tn->tn_val->v_ansiu;
   3730 			v->v_quad = tn->tn_val->v_quad;
   3731 			return v;
   3732 		}
   3733 		v->v_quad = tn->tn_val->v_ldbl;
   3734 	} else {
   3735 		v->v_quad = 1;
   3736 	}
   3737 
   3738 	if (required)
   3739 		/* integral constant expression expected */
   3740 		error(55);
   3741 	else
   3742 		/* variable array dimension is a C99/GCC extension */
   3743 		c99ism(318);
   3744 
   3745 	if (!is_integer(v->v_tspec))
   3746 		v->v_tspec = INT;
   3747 
   3748 	return v;
   3749 }
   3750 
   3751 static bool
   3752 is_constcond_false(const tnode_t *tn, tspec_t t)
   3753 {
   3754 	return (t == BOOL || t == INT) &&
   3755 	       tn->tn_op == CON && tn->tn_val->v_quad == 0;
   3756 }
   3757 
   3758 /*
   3759  * Perform some tests on expressions which can't be done in build() and
   3760  * functions called by build(). These tests must be done here because
   3761  * we need some information about the context in which the operations
   3762  * are performed.
   3763  * After all tests are performed and dofreeblk is true, expr() frees the
   3764  * memory which is used for the expression.
   3765  */
   3766 void
   3767 expr(tnode_t *tn, bool vctx, bool tctx, bool dofreeblk, bool constcond_false_ok)
   3768 {
   3769 
   3770 	lint_assert(tn != NULL || nerr != 0);
   3771 
   3772 	if (tn == NULL) {
   3773 		tfreeblk();
   3774 		return;
   3775 	}
   3776 
   3777 	/* expr() is also called in global initializations */
   3778 	if (dcs->d_ctx != EXTERN)
   3779 		check_statement_reachable();
   3780 
   3781 	check_expr_misc(tn, vctx, tctx, !tctx, false, false, false);
   3782 	if (tn->tn_op == ASSIGN) {
   3783 		if (hflag && tctx)
   3784 			/* assignment in conditional context */
   3785 			warning(159);
   3786 	} else if (tn->tn_op == CON) {
   3787 		if (hflag && tctx && !constcond_flag &&
   3788 		    !tn->tn_system_dependent &&
   3789 		    !(constcond_false_ok &&
   3790 		      is_constcond_false(tn, tn->tn_type->t_tspec)))
   3791 			/* constant in conditional context */
   3792 			warning(161);
   3793 	}
   3794 	if (!modtab[tn->tn_op].m_has_side_effect) {
   3795 		/*
   3796 		 * for left operands of COMMA this warning is already
   3797 		 * printed
   3798 		 */
   3799 		if (tn->tn_op != COMMA && !vctx && !tctx)
   3800 			check_null_effect(tn);
   3801 	}
   3802 	if (dflag)
   3803 		display_expression(tn, 0);
   3804 
   3805 	/* free the tree memory */
   3806 	if (dofreeblk)
   3807 		tfreeblk();
   3808 }
   3809 
   3810 static bool
   3811 has_side_effect(const tnode_t *tn) // NOLINT(misc-no-recursion)
   3812 {
   3813 	op_t op = tn->tn_op;
   3814 
   3815 	if (modtab[op].m_has_side_effect)
   3816 		return true;
   3817 
   3818 	if (op == CVT && tn->tn_type->t_tspec == VOID)
   3819 		return has_side_effect(tn->tn_left);
   3820 
   3821 	/* XXX: Why not has_side_effect(tn->tn_left) as well? */
   3822 	if (op == LOGAND || op == LOGOR)
   3823 		return has_side_effect(tn->tn_right);
   3824 
   3825 	/* XXX: Why not has_side_effect(tn->tn_left) as well? */
   3826 	if (op == QUEST)
   3827 		return has_side_effect(tn->tn_right);
   3828 
   3829 	if (op == COLON || op == COMMA) {
   3830 		return has_side_effect(tn->tn_left) ||
   3831 		       has_side_effect(tn->tn_right);
   3832 	}
   3833 
   3834 	return false;
   3835 }
   3836 
   3837 static void
   3838 check_null_effect(const tnode_t *tn)
   3839 {
   3840 
   3841 	if (hflag && !has_side_effect(tn)) {
   3842 		/* expression has null effect */
   3843 		warning(129);
   3844 	}
   3845 }
   3846 
   3847 /*
   3848  * Dump an expression to stdout
   3849  * only used for debugging
   3850  */
   3851 static void
   3852 display_expression(const tnode_t *tn, int offs)
   3853 {
   3854 	uint64_t uq;
   3855 
   3856 	if (tn == NULL) {
   3857 		(void)printf("%*s%s\n", offs, "", "NULL");
   3858 		return;
   3859 	}
   3860 	(void)printf("%*sop %s  ", offs, "", modtab[tn->tn_op].m_name);
   3861 
   3862 	if (tn->tn_op == NAME) {
   3863 		(void)printf("%s: %s ",
   3864 		    tn->tn_sym->s_name,
   3865 		    storage_class_name(tn->tn_sym->s_scl));
   3866 	} else if (tn->tn_op == CON && is_floating(tn->tn_type->t_tspec)) {
   3867 		(void)printf("%#g ", (double)tn->tn_val->v_ldbl);
   3868 	} else if (tn->tn_op == CON && is_integer(tn->tn_type->t_tspec)) {
   3869 		uq = tn->tn_val->v_quad;
   3870 		(void)printf("0x %08lx %08lx ",
   3871 		    (long)(uq >> 32) & 0xffffffffl,
   3872 		    (long)uq & 0xffffffffl);
   3873 	} else if (tn->tn_op == CON) {
   3874 		lint_assert(tn->tn_type->t_tspec == PTR);
   3875 		(void)printf("0x%0*lx ", (int)(sizeof (void *) * CHAR_BIT / 4),
   3876 			     (u_long)tn->tn_val->v_quad);
   3877 	} else if (tn->tn_op == STRING) {
   3878 		if (tn->tn_string->st_tspec == CHAR) {
   3879 			(void)printf("\"%s\"", tn->tn_string->st_cp);
   3880 		} else {
   3881 			char	*s;
   3882 			size_t	n;
   3883 			n = MB_CUR_MAX * (tn->tn_string->st_len + 1);
   3884 			s = xmalloc(n);
   3885 			(void)wcstombs(s, tn->tn_string->st_wcp, n);
   3886 			(void)printf("L\"%s\"", s);
   3887 			free(s);
   3888 		}
   3889 		(void)printf(" ");
   3890 	} else if (tn->tn_op == FSEL) {
   3891 		(void)printf("o=%d, l=%d ", tn->tn_type->t_foffs,
   3892 			     tn->tn_type->t_flen);
   3893 	}
   3894 	(void)printf("%s\n", ttos(tn->tn_type));
   3895 	if (tn->tn_op == NAME || tn->tn_op == CON || tn->tn_op == STRING)
   3896 		return;
   3897 	display_expression(tn->tn_left, offs + 2);
   3898 	if (modtab[tn->tn_op].m_binary ||
   3899 	    (tn->tn_op == PUSH && tn->tn_right != NULL)) {
   3900 		display_expression(tn->tn_right, offs + 2);
   3901 	}
   3902 }
   3903 
   3904 /*
   3905  * Called by expr() to recursively perform some tests.
   3906  */
   3907 /* ARGSUSED */
   3908 void
   3909 check_expr_misc(const tnode_t *tn, bool vctx, bool tctx,
   3910 		bool eqwarn, bool fcall, bool rvdisc, bool szof)
   3911 {
   3912 	tnode_t	*ln, *rn;
   3913 	mod_t	*mp;
   3914 	op_t	op;
   3915 	scl_t	sc;
   3916 	dinfo_t	*di;
   3917 
   3918 	if (tn == NULL)
   3919 		return;
   3920 
   3921 	ln = tn->tn_left;
   3922 	rn = tn->tn_right;
   3923 	mp = &modtab[op = tn->tn_op];
   3924 
   3925 	switch (op) {
   3926 	case ADDR:
   3927 		if (ln->tn_op == NAME && (reached || rchflg)) {
   3928 			if (!szof)
   3929 				mark_as_set(ln->tn_sym);
   3930 			mark_as_used(ln->tn_sym, fcall, szof);
   3931 		}
   3932 		if (ln->tn_op == INDIR && ln->tn_left->tn_op == PLUS)
   3933 			/* check the range of array indices */
   3934 			check_array_index(ln->tn_left, true);
   3935 		break;
   3936 	case LOAD:
   3937 		if (ln->tn_op == INDIR && ln->tn_left->tn_op == PLUS)
   3938 			/* check the range of array indices */
   3939 			check_array_index(ln->tn_left, false);
   3940 		/* FALLTHROUGH */
   3941 	case PUSH:
   3942 	case INCBEF:
   3943 	case DECBEF:
   3944 	case INCAFT:
   3945 	case DECAFT:
   3946 	case ADDASS:
   3947 	case SUBASS:
   3948 	case MULASS:
   3949 	case DIVASS:
   3950 	case MODASS:
   3951 	case ANDASS:
   3952 	case ORASS:
   3953 	case XORASS:
   3954 	case SHLASS:
   3955 	case SHRASS:
   3956 	case REAL:
   3957 	case IMAG:
   3958 		if (ln->tn_op == NAME && (reached || rchflg)) {
   3959 			sc = ln->tn_sym->s_scl;
   3960 			/*
   3961 			 * Look if there was a asm statement in one of the
   3962 			 * compound statements we are in. If not, we don't
   3963 			 * print a warning.
   3964 			 */
   3965 			for (di = dcs; di != NULL; di = di->d_next) {
   3966 				if (di->d_asm)
   3967 					break;
   3968 			}
   3969 			if (sc != EXTERN && sc != STATIC &&
   3970 			    !ln->tn_sym->s_set && !szof && di == NULL) {
   3971 				/* %s may be used before set */
   3972 				warning(158, ln->tn_sym->s_name);
   3973 				mark_as_set(ln->tn_sym);
   3974 			}
   3975 			mark_as_used(ln->tn_sym, false, false);
   3976 		}
   3977 		break;
   3978 	case ASSIGN:
   3979 		if (ln->tn_op == NAME && !szof && (reached || rchflg)) {
   3980 			mark_as_set(ln->tn_sym);
   3981 			if (ln->tn_sym->s_scl == EXTERN)
   3982 				outusg(ln->tn_sym);
   3983 		}
   3984 		if (ln->tn_op == INDIR && ln->tn_left->tn_op == PLUS)
   3985 			/* check the range of array indices */
   3986 			check_array_index(ln->tn_left, false);
   3987 		break;
   3988 	case CALL:
   3989 		lint_assert(ln->tn_op == ADDR);
   3990 		lint_assert(ln->tn_left->tn_op == NAME);
   3991 		if (!szof)
   3992 			outcall(tn, vctx || tctx, rvdisc);
   3993 		break;
   3994 	case EQ:
   3995 		if (hflag && eqwarn)
   3996 			/* operator '==' found where '=' was expected */
   3997 			warning(160);
   3998 		break;
   3999 	case CON:
   4000 	case NAME:
   4001 	case STRING:
   4002 		return;
   4003 		/* LINTED206: (enumeration values not handled in switch) */
   4004 	case BITOR:
   4005 	case BITXOR:
   4006 	case NE:
   4007 	case GE:
   4008 	case GT:
   4009 	case LE:
   4010 	case LT:
   4011 	case SHR:
   4012 	case SHL:
   4013 	case MINUS:
   4014 	case PLUS:
   4015 	case MOD:
   4016 	case DIV:
   4017 	case MULT:
   4018 	case INDIR:
   4019 	case UMINUS:
   4020 	case UPLUS:
   4021 	case DEC:
   4022 	case INC:
   4023 	case COMPL:
   4024 	case NOT:
   4025 	case POINT:
   4026 	case ARROW:
   4027 	case NOOP:
   4028 	case BITAND:
   4029 	case FARG:
   4030 	case CASE:
   4031 	case INIT:
   4032 	case RETURN:
   4033 	case ICALL:
   4034 	case CVT:
   4035 	case COMMA:
   4036 	case FSEL:
   4037 	case COLON:
   4038 	case QUEST:
   4039 	case LOGOR:
   4040 	case LOGAND:
   4041 		break;
   4042 	}
   4043 
   4044 	bool cvctx = mp->m_left_value_context;
   4045 	bool ctctx = mp->m_left_test_context;
   4046 	bool eq = mp->m_warn_if_operand_eq &&
   4047 		  !ln->tn_parenthesized &&
   4048 		  rn != NULL && !rn->tn_parenthesized;
   4049 
   4050 	/*
   4051 	 * values of operands of ':' are not used if the type of at least
   4052 	 * one of the operands (for gcc compatibility) is void
   4053 	 * XXX test/value context of QUEST should probably be used as
   4054 	 * context for both operands of COLON
   4055 	 */
   4056 	if (op == COLON && tn->tn_type->t_tspec == VOID)
   4057 		cvctx = ctctx = false;
   4058 	bool discard = op == CVT && tn->tn_type->t_tspec == VOID;
   4059 	check_expr_misc(ln, cvctx, ctctx, eq, op == CALL, discard, szof);
   4060 
   4061 	switch (op) {
   4062 	case PUSH:
   4063 		if (rn != NULL)
   4064 			check_expr_misc(rn, false, false, eq, false, false,
   4065 			    szof);
   4066 		break;
   4067 	case LOGAND:
   4068 	case LOGOR:
   4069 		check_expr_misc(rn, false, true, eq, false, false, szof);
   4070 		break;
   4071 	case COLON:
   4072 		check_expr_misc(rn, cvctx, ctctx, eq, false, false, szof);
   4073 		break;
   4074 	case COMMA:
   4075 		check_expr_misc(rn, vctx, tctx, eq, false, false, szof);
   4076 		break;
   4077 	default:
   4078 		if (mp->m_binary)
   4079 			check_expr_misc(rn, true, false, eq, false, false,
   4080 			    szof);
   4081 		break;
   4082 	}
   4083 
   4084 }
   4085 
   4086 /*
   4087  * Checks the range of array indices, if possible.
   4088  * amper is set if only the address of the element is used. This
   4089  * means that the index is allowed to refer to the first element
   4090  * after the array.
   4091  */
   4092 static void
   4093 check_array_index(tnode_t *tn, bool amper)
   4094 {
   4095 	int	dim;
   4096 	tnode_t	*ln, *rn;
   4097 	int	elsz;
   4098 	int64_t	con;
   4099 
   4100 	ln = tn->tn_left;
   4101 	rn = tn->tn_right;
   4102 
   4103 	/* We can only check constant indices. */
   4104 	if (rn->tn_op != CON)
   4105 		return;
   4106 
   4107 	/* Return if the left node does not stem from an array. */
   4108 	if (ln->tn_op != ADDR)
   4109 		return;
   4110 	if (ln->tn_left->tn_op != STRING && ln->tn_left->tn_op != NAME)
   4111 		return;
   4112 	if (ln->tn_left->tn_type->t_tspec != ARRAY)
   4113 		return;
   4114 
   4115 	/*
   4116 	 * For incomplete array types, we can print a warning only if
   4117 	 * the index is negative.
   4118 	 */
   4119 	if (is_incomplete(ln->tn_left->tn_type) && rn->tn_val->v_quad >= 0)
   4120 		return;
   4121 
   4122 	/* Get the size of one array element */
   4123 	if ((elsz = length(ln->tn_type->t_subt, NULL)) == 0)
   4124 		return;
   4125 	elsz /= CHAR_SIZE;
   4126 
   4127 	/* Change the unit of the index from bytes to element size. */
   4128 	if (is_uinteger(rn->tn_type->t_tspec)) {
   4129 		con = (uint64_t)rn->tn_val->v_quad / elsz;
   4130 	} else {
   4131 		con = rn->tn_val->v_quad / elsz;
   4132 	}
   4133 
   4134 	dim = ln->tn_left->tn_type->t_dim + (amper ? 1 : 0);
   4135 
   4136 	if (!is_uinteger(rn->tn_type->t_tspec) && con < 0) {
   4137 		/* array subscript cannot be negative: %ld */
   4138 		warning(167, (long)con);
   4139 	} else if (dim > 0 && (uint64_t)con >= (uint64_t)dim) {
   4140 		/* array subscript cannot be > %d: %ld */
   4141 		warning(168, dim - 1, (long)con);
   4142 	}
   4143 }
   4144 
   4145 /*
   4146  * Check for ordered comparisons of unsigned values with 0.
   4147  */
   4148 static void
   4149 check_integer_comparison(op_t op, tnode_t *ln, tnode_t *rn)
   4150 {
   4151 	tspec_t	lt, rt;
   4152 	mod_t	*mp;
   4153 
   4154 	lt = ln->tn_type->t_tspec;
   4155 	rt = rn->tn_type->t_tspec;
   4156 	mp = &modtab[op];
   4157 
   4158 	if (ln->tn_op != CON && rn->tn_op != CON)
   4159 		return;
   4160 
   4161 	if (!is_integer(lt) || !is_integer(rt))
   4162 		return;
   4163 
   4164 	if ((hflag || pflag) && lt == CHAR && rn->tn_op == CON &&
   4165 	    (rn->tn_val->v_quad < 0 ||
   4166 	     rn->tn_val->v_quad > (int)~(~0U << (CHAR_SIZE - 1)))) {
   4167 		/* nonportable character comparison, op %s */
   4168 		warning(230, mp->m_name);
   4169 		return;
   4170 	}
   4171 	if ((hflag || pflag) && rt == CHAR && ln->tn_op == CON &&
   4172 	    (ln->tn_val->v_quad < 0 ||
   4173 	     ln->tn_val->v_quad > (int)~(~0U << (CHAR_SIZE - 1)))) {
   4174 		/* nonportable character comparison, op %s */
   4175 		warning(230, mp->m_name);
   4176 		return;
   4177 	}
   4178 	if (is_uinteger(lt) && !is_uinteger(rt) &&
   4179 	    rn->tn_op == CON && rn->tn_val->v_quad <= 0) {
   4180 		if (rn->tn_val->v_quad < 0) {
   4181 			/* comparison of %s with %s, op %s */
   4182 			warning(162, type_name(ln->tn_type),
   4183 			    "negative constant", mp->m_name);
   4184 		} else if (op == LT || op == GE || (hflag && op == LE)) {
   4185 			/* comparison of %s with %s, op %s */
   4186 			warning(162, type_name(ln->tn_type), "0", mp->m_name);
   4187 		}
   4188 		return;
   4189 	}
   4190 	if (is_uinteger(rt) && !is_uinteger(lt) &&
   4191 	    ln->tn_op == CON && ln->tn_val->v_quad <= 0) {
   4192 		if (ln->tn_val->v_quad < 0) {
   4193 			/* comparison of %s with %s, op %s */
   4194 			warning(162, "negative constant",
   4195 			    type_name(rn->tn_type), mp->m_name);
   4196 		} else if (op == GT || op == LE || (hflag && op == GE)) {
   4197 			/* comparison of %s with %s, op %s */
   4198 			warning(162, "0", type_name(rn->tn_type), mp->m_name);
   4199 		}
   4200 		return;
   4201 	}
   4202 }
   4203 
   4204 /*
   4205  * Return whether the expression can be used for static initialization.
   4206  *
   4207  * Constant initialization expressions must be constant or an address
   4208  * of a static object with an optional offset. In the first case,
   4209  * the result is returned in *offsp. In the second case, the static
   4210  * object is returned in *symp and the offset in *offsp.
   4211  *
   4212  * The expression can consist of PLUS, MINUS, ADDR, NAME, STRING and
   4213  * CON. Type conversions are allowed if they do not change binary
   4214  * representation (including width).
   4215  */
   4216 bool
   4217 constant_addr(const tnode_t *tn, sym_t **symp, ptrdiff_t *offsp)
   4218 {
   4219 	sym_t	*sym;
   4220 	ptrdiff_t offs1, offs2;
   4221 	tspec_t	t, ot;
   4222 
   4223 	switch (tn->tn_op) {
   4224 	case MINUS:
   4225 		if (tn->tn_right->tn_op == CVT)
   4226 			return constant_addr(tn->tn_right, symp, offsp);
   4227 		else if (tn->tn_right->tn_op != CON)
   4228 			return false;
   4229 		/* FALLTHROUGH */
   4230 	case PLUS:
   4231 		offs1 = offs2 = 0;
   4232 		if (tn->tn_left->tn_op == CON) {
   4233 			offs1 = (ptrdiff_t)tn->tn_left->tn_val->v_quad;
   4234 			if (!constant_addr(tn->tn_right, &sym, &offs2))
   4235 				return false;
   4236 		} else if (tn->tn_right->tn_op == CON) {
   4237 			offs2 = (ptrdiff_t)tn->tn_right->tn_val->v_quad;
   4238 			if (tn->tn_op == MINUS)
   4239 				offs2 = -offs2;
   4240 			if (!constant_addr(tn->tn_left, &sym, &offs1))
   4241 				return false;
   4242 		} else {
   4243 			return false;
   4244 		}
   4245 		*symp = sym;
   4246 		*offsp = offs1 + offs2;
   4247 		return true;
   4248 	case ADDR:
   4249 		if (tn->tn_left->tn_op == NAME) {
   4250 			*symp = tn->tn_left->tn_sym;
   4251 			*offsp = 0;
   4252 			return true;
   4253 		} else {
   4254 			/*
   4255 			 * If this would be the front end of a compiler we
   4256 			 * would return a label instead of 0, at least if
   4257 			 * 'tn->tn_left->tn_op == STRING'.
   4258 			 */
   4259 			*symp = NULL;
   4260 			*offsp = 0;
   4261 			return true;
   4262 		}
   4263 	case CVT:
   4264 		t = tn->tn_type->t_tspec;
   4265 		ot = tn->tn_left->tn_type->t_tspec;
   4266 		if ((!is_integer(t) && t != PTR) ||
   4267 		    (!is_integer(ot) && ot != PTR)) {
   4268 			return false;
   4269 		}
   4270 #ifdef notdef
   4271 		/*
   4272 		 * consider:
   4273 		 *	struct foo {
   4274 		 *		unsigned char a;
   4275 		 *	} f = {
   4276 		 *		(u_char)(u_long)(&(((struct foo *)0)->a))
   4277 		 *	};
   4278 		 * since psize(u_long) != psize(u_char) this fails.
   4279 		 */
   4280 		else if (psize(t) != psize(ot))
   4281 			return -1;
   4282 #endif
   4283 		return constant_addr(tn->tn_left, symp, offsp);
   4284 	default:
   4285 		return false;
   4286 	}
   4287 }
   4288 
   4289 /*
   4290  * Concatenate two string constants.
   4291  */
   4292 strg_t *
   4293 cat_strings(strg_t *strg1, strg_t *strg2)
   4294 {
   4295 	size_t	len1, len2, len;
   4296 
   4297 	if (strg1->st_tspec != strg2->st_tspec) {
   4298 		/* cannot concatenate wide and regular string literals */
   4299 		error(292);
   4300 		return strg1;
   4301 	}
   4302 
   4303 	len1 = strg1->st_len;
   4304 	len2 = strg2->st_len + 1;	/* + NUL */
   4305 	len = len1 + len2;
   4306 
   4307 #define COPY(F) \
   4308     do { \
   4309 	strg1->F = xrealloc(strg1->F, len * sizeof(*strg1->F)); \
   4310 	(void)memcpy(strg1->F + len1, strg2->F, len2 * sizeof(*strg1->F)); \
   4311 	free(strg2->F); \
   4312     } while (/*CONSTCOND*/false)
   4313 
   4314 	if (strg1->st_tspec == CHAR)
   4315 		COPY(st_cp);
   4316 	else
   4317 		COPY(st_wcp);
   4318 
   4319 	strg1->st_len = len - 1; /* - NUL */
   4320 	free(strg2);
   4321 
   4322 	return strg1;
   4323 }
   4324 
   4325 static bool
   4326 is_confusing_precedence(op_t op, op_t lop, bool lparen, op_t rop, bool rparen)
   4327 {
   4328 
   4329 	if (op == SHL || op == SHR) {
   4330 		if (!lparen && (lop == PLUS || lop == MINUS))
   4331 			return true;
   4332 		if (!rparen && (rop == PLUS || rop == MINUS))
   4333 			return true;
   4334 		return false;
   4335 	}
   4336 
   4337 	if (op == LOGOR) {
   4338 		if (!lparen && lop == LOGAND)
   4339 			return true;
   4340 		if (!rparen && rop == LOGAND)
   4341 			return true;
   4342 		return false;
   4343 	}
   4344 
   4345 	lint_assert(op == BITAND || op == BITXOR || op == BITOR);
   4346 	if (!lparen && lop != op) {
   4347 		if (lop == PLUS || lop == MINUS)
   4348 			return true;
   4349 		if (lop == BITAND || lop == BITXOR)
   4350 			return true;
   4351 	}
   4352 	if (!rparen && rop != op) {
   4353 		if (rop == PLUS || rop == MINUS)
   4354 			return true;
   4355 		if (rop == BITAND || rop == BITXOR)
   4356 			return true;
   4357 	}
   4358 	return false;
   4359 }
   4360 
   4361 /*
   4362  * Print a warning if the given node has operands which should be
   4363  * parenthesized.
   4364  *
   4365  * XXX Does not work if an operand is a constant expression. Constant
   4366  * expressions are already folded.
   4367  */
   4368 static void
   4369 check_precedence_confusion(tnode_t *tn)
   4370 {
   4371 	tnode_t *ln, *rn;
   4372 
   4373 	if (!hflag)
   4374 		return;
   4375 
   4376 	debug_node(tn, 0);
   4377 
   4378 	lint_assert(modtab[tn->tn_op].m_binary);
   4379 	for (ln = tn->tn_left; ln->tn_op == CVT; ln = ln->tn_left)
   4380 		continue;
   4381 	for (rn = tn->tn_right; rn->tn_op == CVT; rn = rn->tn_left)
   4382 		continue;
   4383 
   4384 	if (is_confusing_precedence(tn->tn_op,
   4385 	    ln->tn_op, ln->tn_parenthesized,
   4386 	    rn->tn_op, rn->tn_parenthesized)) {
   4387 		/* precedence confusion possible: parenthesize! */
   4388 		warning(169);
   4389 	}
   4390 }
   4391