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