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