Home | History | Annotate | Line # | Download | only in lint1
tree.c revision 1.632
      1 /*	$NetBSD: tree.c,v 1.632 2024/03/30 17:23:13 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)
     40 __RCSID("$NetBSD: tree.c,v 1.632 2024/03/30 17:23:13 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 
     52 
     53 typedef struct integer_constraints {
     54 	int64_t		smin;	/* signed minimum */
     55 	int64_t		smax;	/* signed maximum */
     56 	uint64_t	umin;	/* unsigned minimum */
     57 	uint64_t	umax;	/* unsigned maximum */
     58 	uint64_t	bset;	/* bits that are definitely set */
     59 	uint64_t	bclr;	/* bits that are definitely clear */
     60 } integer_constraints;
     61 
     62 
     63 static uint64_t
     64 u64_fill_right(uint64_t x)
     65 {
     66 	x |= x >> 1;
     67 	x |= x >> 2;
     68 	x |= x >> 4;
     69 	x |= x >> 8;
     70 	x |= x >> 16;
     71 	x |= x >> 32;
     72 	return x;
     73 }
     74 
     75 static bool
     76 str_ends_with(const char *haystack, const char *needle)
     77 {
     78 	size_t hlen = strlen(haystack);
     79 	size_t nlen = strlen(needle);
     80 
     81 	return nlen <= hlen &&
     82 	    memcmp(haystack + hlen - nlen, needle, nlen) == 0;
     83 }
     84 
     85 static unsigned
     86 width_in_bits(const type_t *tp)
     87 {
     88 
     89 	lint_assert(is_integer(tp->t_tspec));
     90 	return tp->t_bitfield
     91 	    ? tp->t_bit_field_width
     92 	    : size_in_bits(tp->t_tspec);
     93 }
     94 
     95 static int
     96 portable_rank_cmp(tspec_t t1, tspec_t t2)
     97 {
     98 	const ttab_t *p1 = type_properties(t1), *p2 = type_properties(t2);
     99 	lint_assert(p1->tt_rank_kind == p2->tt_rank_kind);
    100 	lint_assert(p1->tt_rank_value > 0);
    101 	lint_assert(p2->tt_rank_value > 0);
    102 	return (int)p1->tt_rank_value - (int)p2->tt_rank_value;
    103 }
    104 
    105 static bool
    106 ic_maybe_signed(const type_t *tp, const integer_constraints *ic)
    107 {
    108 	return !is_uinteger(tp->t_tspec) && ic->bclr >> 63 == 0;
    109 }
    110 
    111 static integer_constraints
    112 ic_any(const type_t *tp)
    113 {
    114 	integer_constraints c;
    115 
    116 	uint64_t vbits = value_bits(width_in_bits(tp));
    117 	if (is_uinteger(tp->t_tspec)) {
    118 		c.smin = INT64_MIN;
    119 		c.smax = INT64_MAX;
    120 		c.umin = 0;
    121 		c.umax = vbits;
    122 		c.bset = 0;
    123 		c.bclr = ~c.umax;
    124 	} else {
    125 		c.smin = (int64_t)-1 - (int64_t)(vbits >> 1);
    126 		c.smax = (int64_t)(vbits >> 1);
    127 		c.umin = 0;
    128 		c.umax = UINT64_MAX;
    129 		c.bset = 0;
    130 		c.bclr = 0;
    131 	}
    132 	return c;
    133 }
    134 
    135 static integer_constraints
    136 ic_con(const type_t *tp, const val_t *v)
    137 {
    138 	integer_constraints c;
    139 
    140 	lint_assert(is_integer(tp->t_tspec));
    141 	int64_t si = v->u.integer;
    142 	uint64_t ui = (uint64_t)si;
    143 	c.smin = si;
    144 	c.smax = si;
    145 	c.umin = ui;
    146 	c.umax = ui;
    147 	c.bset = ui;
    148 	c.bclr = ~ui;
    149 	return c;
    150 }
    151 
    152 static integer_constraints
    153 ic_cvt(const type_t *ntp, const type_t *otp, integer_constraints a)
    154 {
    155 	unsigned nw = width_in_bits(ntp);
    156 	unsigned ow = width_in_bits(otp);
    157 	bool nu = is_uinteger(ntp->t_tspec);
    158 	bool ou = is_uinteger(otp->t_tspec);
    159 
    160 	if (nw >= ow && nu == ou)
    161 		return a;
    162 	if (nw > ow && ou)
    163 		return a;
    164 	return ic_any(ntp);
    165 }
    166 
    167 static integer_constraints
    168 ic_bitand(integer_constraints a, integer_constraints b)
    169 {
    170 	integer_constraints c;
    171 
    172 	c.smin = INT64_MIN;
    173 	c.smax = INT64_MAX;
    174 	c.umin = 0;
    175 	c.umax = UINT64_MAX;
    176 	c.bset = a.bset & b.bset;
    177 	c.bclr = a.bclr | b.bclr;
    178 	return c;
    179 }
    180 
    181 static integer_constraints
    182 ic_bitor(integer_constraints a, integer_constraints b)
    183 {
    184 	integer_constraints c;
    185 
    186 	c.smin = INT64_MIN;
    187 	c.smax = INT64_MAX;
    188 	c.umin = 0;
    189 	c.umax = UINT64_MAX;
    190 	c.bset = a.bset | b.bset;
    191 	c.bclr = a.bclr & b.bclr;
    192 	return c;
    193 }
    194 
    195 static integer_constraints
    196 ic_mod(const type_t *tp, integer_constraints a, integer_constraints b)
    197 {
    198 	integer_constraints c;
    199 
    200 	if (ic_maybe_signed(tp, &a) || ic_maybe_signed(tp, &b))
    201 		return ic_any(tp);
    202 
    203 	c.smin = INT64_MIN;
    204 	c.smax = INT64_MAX;
    205 	c.umin = 0;
    206 	c.umax = b.umax - 1;
    207 	c.bset = 0;
    208 	c.bclr = ~u64_fill_right(c.umax);
    209 	return c;
    210 }
    211 
    212 static integer_constraints
    213 ic_shl(const type_t *tp, integer_constraints a, integer_constraints b)
    214 {
    215 	integer_constraints c;
    216 	unsigned int amount;
    217 
    218 	if (ic_maybe_signed(tp, &a))
    219 		return ic_any(tp);
    220 
    221 	if (b.smin == b.smax && b.smin >= 0 && b.smin < 64)
    222 		amount = (unsigned int)b.smin;
    223 	else if (b.umin == b.umax && b.umin < 64)
    224 		amount = (unsigned int)b.umin;
    225 	else
    226 		return ic_any(tp);
    227 
    228 	c.smin = INT64_MIN;
    229 	c.smax = INT64_MAX;
    230 	c.umin = 0;
    231 	c.umax = UINT64_MAX;
    232 	c.bset = a.bset << amount;
    233 	c.bclr = a.bclr << amount | (((uint64_t)1 << amount) - 1);
    234 	return c;
    235 }
    236 
    237 static integer_constraints
    238 ic_shr(const type_t *tp, integer_constraints a, integer_constraints b)
    239 {
    240 	integer_constraints c;
    241 	unsigned int amount;
    242 
    243 	if (ic_maybe_signed(tp, &a))
    244 		return ic_any(tp);
    245 
    246 	if (b.smin == b.smax && b.smin >= 0 && b.smin < 64)
    247 		amount = (unsigned int)b.smin;
    248 	else if (b.umin == b.umax && b.umin < 64)
    249 		amount = (unsigned int)b.umin;
    250 	else
    251 		return ic_any(tp);
    252 
    253 	c.smin = INT64_MIN;
    254 	c.smax = INT64_MAX;
    255 	c.umin = 0;
    256 	c.umax = UINT64_MAX;
    257 	c.bset = a.bset >> amount;
    258 	c.bclr = a.bclr >> amount | ~(~(uint64_t)0 >> amount);
    259 	return c;
    260 }
    261 
    262 static integer_constraints
    263 ic_cond(integer_constraints a, integer_constraints b)
    264 {
    265 	integer_constraints c;
    266 
    267 	c.smin = a.smin < b.smin ? a.smin : b.smin;
    268 	c.smax = a.smax > b.smax ? a.smax : b.smax;
    269 	c.umin = a.umin < b.umin ? a.umin : b.umin;
    270 	c.umax = a.umax > b.umax ? a.umax : b.umax;
    271 	c.bset = a.bset | b.bset;
    272 	c.bclr = a.bclr & b.bclr;
    273 	return c;
    274 }
    275 
    276 static integer_constraints
    277 ic_expr(const tnode_t *tn)
    278 {
    279 	integer_constraints lc, rc;
    280 
    281 	lint_assert(is_integer(tn->tn_type->t_tspec));
    282 
    283 	switch (tn->tn_op) {
    284 	case CON:
    285 		return ic_con(tn->tn_type, &tn->u.value);
    286 	case CVT:
    287 		if (!is_integer(tn->u.ops.left->tn_type->t_tspec))
    288 			return ic_any(tn->tn_type);
    289 		lc = ic_expr(tn->u.ops.left);
    290 		return ic_cvt(tn->tn_type, tn->u.ops.left->tn_type, lc);
    291 	case MOD:
    292 		lc = ic_expr(before_conversion(tn->u.ops.left));
    293 		rc = ic_expr(before_conversion(tn->u.ops.right));
    294 		return ic_mod(tn->tn_type, lc, rc);
    295 	case SHL:
    296 		lc = ic_expr(tn->u.ops.left);
    297 		rc = ic_expr(tn->u.ops.right);
    298 		return ic_shl(tn->tn_type, lc, rc);
    299 	case SHR:
    300 		lc = ic_expr(tn->u.ops.left);
    301 		rc = ic_expr(tn->u.ops.right);
    302 		return ic_shr(tn->tn_type, lc, rc);
    303 	case BITAND:
    304 		lc = ic_expr(tn->u.ops.left);
    305 		rc = ic_expr(tn->u.ops.right);
    306 		return ic_bitand(lc, rc);
    307 	case BITOR:
    308 		lc = ic_expr(tn->u.ops.left);
    309 		rc = ic_expr(tn->u.ops.right);
    310 		return ic_bitor(lc, rc);
    311 	case QUEST:
    312 		lc = ic_expr(tn->u.ops.right->u.ops.left);
    313 		rc = ic_expr(tn->u.ops.right->u.ops.right);
    314 		return ic_cond(lc, rc);
    315 	default:
    316 		return ic_any(tn->tn_type);
    317 	}
    318 }
    319 
    320 uint64_t
    321 possible_bits(const tnode_t *tn)
    322 {
    323 	return ~ic_expr(tn).bclr;
    324 }
    325 
    326 /* Build 'pointer to tp', 'array of tp' or 'function returning tp'. */
    327 type_t *
    328 block_derive_type(type_t *tp, tspec_t t)
    329 {
    330 
    331 	type_t *tp2 = block_zero_alloc(sizeof(*tp2), "type");
    332 	tp2->t_tspec = t;
    333 	tp2->t_subt = tp;
    334 	return tp2;
    335 }
    336 
    337 /*
    338  * Derive 'pointer to tp' or 'function returning tp'.
    339  * The memory is freed at the end of the current expression.
    340  */
    341 type_t *
    342 expr_derive_type(type_t *tp, tspec_t t)
    343 {
    344 
    345 	type_t *tp2 = expr_zero_alloc(sizeof(*tp2), "type");
    346 	tp2->t_tspec = t;
    347 	tp2->t_subt = tp;
    348 	return tp2;
    349 }
    350 
    351 /* Create an expression from a unary or binary operator and its operands. */
    352 static tnode_t *
    353 build_op(op_t op, bool sys, type_t *type, tnode_t *ln, tnode_t *rn)
    354 {
    355 
    356 	tnode_t *ntn = expr_alloc_tnode();
    357 	ntn->tn_op = op;
    358 	ntn->tn_type = type;
    359 	ntn->tn_sys = sys;
    360 	ntn->u.ops.left = ln;
    361 	ntn->u.ops.right = rn;
    362 
    363 	if (op == INDIR || op == FSEL) {
    364 		lint_assert(ln->tn_type->t_tspec == PTR);
    365 		tspec_t t = ln->tn_type->t_subt->t_tspec;
    366 		ntn->tn_lvalue = t != FUNC && t != VOID;
    367 	}
    368 
    369 	return ntn;
    370 }
    371 
    372 tnode_t *
    373 build_constant(type_t *tp, val_t *v)
    374 {
    375 
    376 	tnode_t *n = expr_alloc_tnode();
    377 	n->tn_op = CON;
    378 	n->tn_type = tp;
    379 	n->u.value = *v;
    380 	n->u.value.v_tspec = tp->t_tspec;
    381 	free(v);
    382 	return n;
    383 }
    384 
    385 static tnode_t *
    386 build_integer_constant(tspec_t t, int64_t si)
    387 {
    388 
    389 	tnode_t *n = expr_alloc_tnode();
    390 	n->tn_op = CON;
    391 	n->tn_type = gettyp(t);
    392 	n->u.value.v_tspec = t;
    393 	n->u.value.v_unsigned_since_c90 = false;
    394 	n->u.value.v_char_constant = false;
    395 	n->u.value.u.integer = si;
    396 	return n;
    397 }
    398 
    399 static void
    400 fallback_symbol(sym_t *sym)
    401 {
    402 
    403 	if (Tflag && fallback_symbol_strict_bool(sym))
    404 		return;
    405 
    406 	if (block_level > 0 && (strcmp(sym->s_name, "__FUNCTION__") == 0 ||
    407 			   strcmp(sym->s_name, "__PRETTY_FUNCTION__") == 0)) {
    408 		/* __FUNCTION__/__PRETTY_FUNCTION__ is a GCC extension */
    409 		gnuism(316);
    410 		// XXX: Should probably be ARRAY instead of PTR.
    411 		sym->s_type = block_derive_type(gettyp(CHAR), PTR);
    412 		sym->s_type->t_const = true;
    413 		return;
    414 	}
    415 
    416 	if (funcsym != NULL && strcmp(sym->s_name, "__func__") == 0) {
    417 		if (!allow_c99)
    418 			/* __func__ is a C99 feature */
    419 			warning(317);
    420 		/* C11 6.4.2.2 */
    421 		sym->s_type = block_derive_type(gettyp(CHAR), ARRAY);
    422 		sym->s_type->t_const = true;
    423 		sym->s_type->u.dimension = (int)strlen(funcsym->s_name) + 1;
    424 		return;
    425 	}
    426 
    427 	/* '%s' undefined */
    428 	error(99, sym->s_name);
    429 }
    430 
    431 /*
    432  * Functions that are predeclared by GCC or other compilers can be called
    433  * with arbitrary arguments.  Since lint usually runs after a successful
    434  * compilation, it's the compiler's job to catch any errors.
    435  */
    436 bool
    437 is_compiler_builtin(const char *name)
    438 {
    439 	/* https://gcc.gnu.org/onlinedocs/gcc/C-Extensions.html */
    440 	if (allow_gcc) {
    441 		if (strncmp(name, "__atomic_", 9) == 0 ||
    442 		    strncmp(name, "__builtin_", 10) == 0 ||
    443 		    strcmp(name, "alloca") == 0 ||
    444 		    /* obsolete but still in use, as of 2021 */
    445 		    strncmp(name, "__sync_", 7) == 0)
    446 			return true;
    447 	}
    448 
    449 	/* https://software.intel.com/sites/landingpage/IntrinsicsGuide/ */
    450 	if (strncmp(name, "_mm_", 4) == 0)
    451 		return true;
    452 
    453 	return false;
    454 }
    455 
    456 /* https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html */
    457 static bool
    458 is_gcc_bool_builtin(const char *name)
    459 {
    460 	return strncmp(name, "__builtin_", 10) == 0 &&
    461 	    (str_ends_with(name, "_overflow") ||
    462 		str_ends_with(name, "_overflow_p"));
    463 }
    464 
    465 static void
    466 build_name_call(sym_t *sym)
    467 {
    468 
    469 	if (is_compiler_builtin(sym->s_name)) {
    470 		/*
    471 		 * Do not warn about these, just assume that they are regular
    472 		 * functions compatible with non-prototype calling conventions.
    473 		 */
    474 		if (allow_gcc && is_gcc_bool_builtin(sym->s_name))
    475 			sym->s_type = gettyp(BOOL);
    476 	} else if (allow_c99)
    477 		/* function '%s' implicitly declared to return int */
    478 		error(215, sym->s_name);
    479 	else if (!allow_trad)
    480 		/* function '%s' implicitly declared to return int */
    481 		warning(215, sym->s_name);
    482 
    483 	/* XXX if !allow_c90, the symbol should be exported to level 0 */
    484 	sym->s_type = block_derive_type(sym->s_type, FUNC);
    485 }
    486 
    487 /* Create a node for a name (symbol table entry). */
    488 tnode_t *
    489 build_name(sym_t *sym, bool is_funcname)
    490 {
    491 
    492 	if (sym->s_scl == NO_SCL && !in_gcc_attribute) {
    493 		sym->s_scl = EXTERN;
    494 		sym->s_def = DECL;
    495 		if (is_funcname)
    496 			build_name_call(sym);
    497 		else
    498 			fallback_symbol(sym);
    499 	}
    500 
    501 	lint_assert(sym->s_kind == SK_VCFT || sym->s_kind == SK_MEMBER);
    502 
    503 	tnode_t *n = expr_alloc_tnode();
    504 	n->tn_type = sym->s_type;
    505 	if (sym->s_scl == BOOL_CONST) {
    506 		n->tn_op = CON;
    507 		n->u.value.v_tspec = BOOL;
    508 		n->u.value.v_unsigned_since_c90 = false;
    509 		n->u.value.v_char_constant = false;
    510 		n->u.value.u.integer = sym->u.s_bool_constant ? 1 : 0;
    511 	} else if (sym->s_scl == ENUM_CONST) {
    512 		n->tn_op = CON;
    513 		n->u.value.v_tspec = INT;	/* ENUM is in n->tn_type */
    514 		n->u.value.v_unsigned_since_c90 = false;
    515 		n->u.value.v_char_constant = false;
    516 		n->u.value.u.integer = sym->u.s_enum_constant;
    517 	} else {
    518 		n->tn_op = NAME;
    519 		n->u.sym = sym;
    520 		if (sym->s_kind == SK_VCFT && sym->s_type->t_tspec != FUNC)
    521 			n->tn_lvalue = true;
    522 	}
    523 
    524 	return n;
    525 }
    526 
    527 tnode_t *
    528 build_string(buffer *lit)
    529 {
    530 	size_t value_len = lit->len;
    531 	if (lit->data != NULL) {
    532 		quoted_iterator it = { .end = 0 };
    533 		for (value_len = 0; quoted_next(lit, &it); value_len++)
    534 			continue;
    535 	}
    536 
    537 	type_t *tp = expr_zero_alloc(sizeof(*tp), "type");
    538 	tp->t_tspec = ARRAY;
    539 	tp->t_subt = gettyp(lit->data != NULL ? CHAR : WCHAR_TSPEC);
    540 	tp->u.dimension = (int)(value_len + 1);
    541 
    542 	tnode_t *n = expr_alloc_tnode();
    543 	n->tn_op = STRING;
    544 	n->tn_type = tp;
    545 	n->tn_lvalue = true;
    546 
    547 	n->u.str_literals = expr_zero_alloc(sizeof(*n->u.str_literals), "tnode.string");
    548 	n->u.str_literals->len = lit->len;
    549 
    550 	if (lit->data != NULL) {
    551 		n->u.str_literals->data = expr_zero_alloc(lit->len + 1,
    552 		    "tnode.string.data");
    553 		(void)memcpy(n->u.str_literals->data, lit->data, lit->len + 1);
    554 		free(lit->data);
    555 	}
    556 	free(lit);
    557 
    558 	return n;
    559 }
    560 
    561 tnode_t *
    562 build_generic_selection(const tnode_t *expr,
    563 			struct generic_association *sel)
    564 {
    565 	tnode_t *default_result = NULL;
    566 
    567 	for (; sel != NULL; sel = sel->ga_prev) {
    568 		if (expr != NULL &&
    569 		    types_compatible(sel->ga_arg, expr->tn_type,
    570 			false, false, NULL))
    571 			return sel->ga_result;
    572 		else if (sel->ga_arg == NULL)
    573 			default_result = sel->ga_result;
    574 	}
    575 	return default_result;
    576 }
    577 
    578 static bool
    579 is_out_of_char_range(const tnode_t *tn)
    580 {
    581 	return tn->tn_op == CON &&
    582 	    !tn->u.value.v_char_constant &&
    583 	    !(0 <= tn->u.value.u.integer &&
    584 		tn->u.value.u.integer < 1 << (CHAR_SIZE - 1));
    585 }
    586 
    587 static bool
    588 check_nonportable_char_comparison(op_t op,
    589 				  const tnode_t *ln, tspec_t lt,
    590 				  const tnode_t *rn, tspec_t rt)
    591 {
    592 	if (!(hflag || pflag))
    593 		return true;
    594 
    595 	if (lt == CHAR && is_out_of_char_range(rn)) {
    596 		char buf[128];
    597 		(void)snprintf(buf, sizeof(buf), "%s %d",
    598 		    op_name(op), (int)rn->u.value.u.integer);
    599 		/* nonportable character comparison '%s' */
    600 		warning(230, buf);
    601 		return false;
    602 	}
    603 	if (rt == CHAR && is_out_of_char_range(ln)) {
    604 		char buf[128];
    605 		(void)snprintf(buf, sizeof(buf), "%d %s ?",
    606 		    (int)ln->u.value.u.integer, op_name(op));
    607 		/* nonportable character comparison '%s' */
    608 		warning(230, buf);
    609 		return false;
    610 	}
    611 	return true;
    612 }
    613 
    614 static void
    615 check_integer_comparison(op_t op, tnode_t *ln, tnode_t *rn)
    616 {
    617 
    618 	tspec_t lt = ln->tn_type->t_tspec;
    619 	tspec_t rt = rn->tn_type->t_tspec;
    620 
    621 	if (ln->tn_op != CON && rn->tn_op != CON)
    622 		return;
    623 
    624 	if (!is_integer(lt) || !is_integer(rt))
    625 		return;
    626 
    627 	if (any_query_enabled && !in_system_header) {
    628 		if (lt == CHAR && rn->tn_op == CON &&
    629 		    !rn->u.value.v_char_constant) {
    630 			/* comparison '%s' of 'char' with plain integer %d */
    631 			query_message(14,
    632 			    op_name(op), (int)rn->u.value.u.integer);
    633 		}
    634 		if (rt == CHAR && ln->tn_op == CON &&
    635 		    !ln->u.value.v_char_constant) {
    636 			/* comparison '%s' of 'char' with plain integer %d */
    637 			query_message(14,
    638 			    op_name(op), (int)ln->u.value.u.integer);
    639 		}
    640 	}
    641 
    642 	if (!check_nonportable_char_comparison(op, ln, lt, rn, rt))
    643 		return;
    644 
    645 	if (is_uinteger(lt) && !is_uinteger(rt) &&
    646 	    rn->tn_op == CON && rn->u.value.u.integer <= 0) {
    647 		if (rn->u.value.u.integer < 0) {
    648 			/* operator '%s' compares '%s' with '%s' */
    649 			warning(162, op_name(op),
    650 			    type_name(ln->tn_type), "negative constant");
    651 		} else if (op == LT || op == GE)
    652 			/* operator '%s' compares '%s' with '%s' */
    653 			warning(162, op_name(op), type_name(ln->tn_type), "0");
    654 		return;
    655 	}
    656 	if (is_uinteger(rt) && !is_uinteger(lt) &&
    657 	    ln->tn_op == CON && ln->u.value.u.integer <= 0) {
    658 		if (ln->u.value.u.integer < 0) {
    659 			/* operator '%s' compares '%s' with '%s' */
    660 			warning(162, op_name(op),
    661 			    "negative constant", type_name(rn->tn_type));
    662 		} else if (op == GT || op == LE)
    663 			/* operator '%s' compares '%s' with '%s' */
    664 			warning(162, op_name(op), "0", type_name(rn->tn_type));
    665 		return;
    666 	}
    667 }
    668 
    669 static const tspec_t arith_rank[] = {
    670 	LDOUBLE, DOUBLE, FLOAT,
    671 #ifdef INT128_SIZE
    672 	UINT128, INT128,
    673 #endif
    674 	ULLONG, LLONG,
    675 	ULONG, LONG,
    676 	UINT, INT,
    677 };
    678 
    679 /* Keep unsigned in traditional C */
    680 static tspec_t
    681 usual_arithmetic_conversion_trad(tspec_t lt, tspec_t rt)
    682 {
    683 
    684 	size_t i;
    685 	for (i = 0; arith_rank[i] != INT; i++)
    686 		if (lt == arith_rank[i] || rt == arith_rank[i])
    687 			break;
    688 
    689 	tspec_t t = arith_rank[i];
    690 	if (is_uinteger(lt) || is_uinteger(rt))
    691 		if (is_integer(t) && !is_uinteger(t))
    692 			return unsigned_type(t);
    693 	return t;
    694 }
    695 
    696 static tspec_t
    697 usual_arithmetic_conversion_c90(tspec_t lt, tspec_t rt)
    698 {
    699 
    700 	if (lt == rt)
    701 		return lt;
    702 
    703 	if (lt == LCOMPLEX || rt == LCOMPLEX)
    704 		return LCOMPLEX;
    705 	if (lt == DCOMPLEX || rt == DCOMPLEX)
    706 		return DCOMPLEX;
    707 	if (lt == FCOMPLEX || rt == FCOMPLEX)
    708 		return FCOMPLEX;
    709 	if (lt == LDOUBLE || rt == LDOUBLE)
    710 		return LDOUBLE;
    711 	if (lt == DOUBLE || rt == DOUBLE)
    712 		return DOUBLE;
    713 	if (lt == FLOAT || rt == FLOAT)
    714 		return FLOAT;
    715 
    716 	if (size_in_bits(lt) > size_in_bits(rt))
    717 		return lt;
    718 	if (size_in_bits(lt) < size_in_bits(rt))
    719 		return rt;
    720 
    721 	size_t i;
    722 	for (i = 3; arith_rank[i] != INT; i++)
    723 		if (arith_rank[i] == lt || arith_rank[i] == rt)
    724 			break;
    725 	if ((is_uinteger(lt) || is_uinteger(rt)) &&
    726 	    !is_uinteger(arith_rank[i]))
    727 		i--;
    728 	return arith_rank[i];
    729 }
    730 
    731 static tnode_t *
    732 apply_usual_arithmetic_conversions(op_t op, tnode_t *tn, tspec_t t)
    733 {
    734 	type_t *ntp = expr_dup_type(tn->tn_type);
    735 	ntp->t_tspec = t;
    736 	if (tn->tn_op != CON) {
    737 		/* usual arithmetic conversion for '%s' from '%s' to '%s' */
    738 		query_message(4, op_name(op),
    739 		    type_name(tn->tn_type), type_name(ntp));
    740 	}
    741 	return convert(op, 0, ntp, tn);
    742 }
    743 
    744 /*
    745  * Apply the "usual arithmetic conversions" (C99 6.3.1.8), which gives both
    746  * operands the same type.
    747  */
    748 static void
    749 balance(op_t op, tnode_t **lnp, tnode_t **rnp)
    750 {
    751 
    752 	tspec_t lt = (*lnp)->tn_type->t_tspec;
    753 	tspec_t rt = (*rnp)->tn_type->t_tspec;
    754 	if (!is_arithmetic(lt) || !is_arithmetic(rt))
    755 		return;
    756 
    757 	tspec_t t = allow_c90
    758 	    ? usual_arithmetic_conversion_c90(lt, rt)
    759 	    : usual_arithmetic_conversion_trad(lt, rt);
    760 
    761 	if (t != lt)
    762 		*lnp = apply_usual_arithmetic_conversions(op, *lnp, t);
    763 	if (t != rt)
    764 		*rnp = apply_usual_arithmetic_conversions(op, *rnp, t);
    765 
    766 	unsigned lw = (*lnp)->tn_type->t_bit_field_width;
    767 	unsigned rw = (*rnp)->tn_type->t_bit_field_width;
    768 	if (lw < rw)
    769 		*lnp = convert(NOOP, 0, (*rnp)->tn_type, *lnp);
    770 	if (rw < lw)
    771 		*rnp = convert(NOOP, 0, (*lnp)->tn_type, *rnp);
    772 }
    773 
    774 static tnode_t *
    775 build_address(bool sys, tnode_t *tn, bool force)
    776 {
    777 	tspec_t t;
    778 
    779 	if (!force && ((t = tn->tn_type->t_tspec) == ARRAY || t == FUNC)) {
    780 		if (!allow_c90)
    781 			/* '&' before array or function: ignored */
    782 			warning(127);
    783 		return tn;
    784 	}
    785 
    786 	/* eliminate '&*' */
    787 	if (tn->tn_op == INDIR &&
    788 	    tn->u.ops.left->tn_type->t_tspec == PTR &&
    789 	    tn->u.ops.left->tn_type->t_subt == tn->tn_type) {
    790 		return tn->u.ops.left;
    791 	}
    792 
    793 	return build_op(ADDR, sys, expr_derive_type(tn->tn_type, PTR),
    794 	    tn, NULL);
    795 }
    796 
    797 static uint64_t
    798 fold_unsigned_integer(op_t op, uint64_t l, uint64_t r,
    799 		      uint64_t max_value, bool *overflow)
    800 {
    801 	switch (op) {
    802 	case COMPL:
    803 		return ~l & max_value;
    804 	case UPLUS:
    805 		return +l;
    806 	case UMINUS:
    807 		return -l & max_value;
    808 	case MULT:
    809 		*overflow = r > 0 && l > max_value / r;
    810 		return l * r;
    811 	case DIV:
    812 		if (r == 0) {
    813 			/* division by 0 */
    814 			error(139);
    815 			return max_value;
    816 		}
    817 		return l / r;
    818 	case MOD:
    819 		if (r == 0) {
    820 			/* modulus by 0 */
    821 			error(140);
    822 			return 0;
    823 		}
    824 		return l % r;
    825 	case PLUS:
    826 		*overflow = l > max_value - r;
    827 		return l + r;
    828 	case MINUS:
    829 		*overflow = l < r;
    830 		return l - r;
    831 	case SHL:
    832 		/* TODO: warn about out-of-bounds 'r'. */
    833 		/* TODO: warn about overflow. */
    834 		return l << (r & 63);
    835 	case SHR:
    836 		/* TODO: warn about out-of-bounds 'r'. */
    837 		return l >> (r & 63);
    838 	case LT:
    839 		return l < r ? 1 : 0;
    840 	case LE:
    841 		return l <= r ? 1 : 0;
    842 	case GT:
    843 		return l > r ? 1 : 0;
    844 	case GE:
    845 		return l >= r ? 1 : 0;
    846 	case EQ:
    847 		return l == r ? 1 : 0;
    848 	case NE:
    849 		return l != r ? 1 : 0;
    850 	case BITAND:
    851 		return l & r;
    852 	case BITXOR:
    853 		return l ^ r;
    854 	case BITOR:
    855 		return l | r;
    856 	default:
    857 		lint_assert(/*CONSTCOND*/false);
    858 		/* NOTREACHED */
    859 	}
    860 }
    861 
    862 static int64_t
    863 fold_signed_integer(op_t op, int64_t l, int64_t r,
    864 		    int64_t min_value, int64_t max_value, bool *overflow)
    865 {
    866 	switch (op) {
    867 	case COMPL:
    868 		return ~l;
    869 	case UPLUS:
    870 		return +l;
    871 	case UMINUS:
    872 		*overflow = l == min_value;
    873 		return *overflow ? l : -l;
    874 	case MULT:;
    875 		uint64_t al = l >= 0 ? (uint64_t)l : -(uint64_t)l;
    876 		uint64_t ar = r >= 0 ? (uint64_t)r : -(uint64_t)r;
    877 		bool neg = (l >= 0) != (r >= 0);
    878 		uint64_t max_prod = (uint64_t)max_value + (neg ? 1 : 0);
    879 		if (al > 0 && ar > max_prod / al) {
    880 			*overflow = true;
    881 			return neg ? min_value : max_value;
    882 		}
    883 		return l * r;
    884 	case DIV:
    885 		if (r == 0) {
    886 			/* division by 0 */
    887 			error(139);
    888 			return max_value;
    889 		}
    890 		if (l == min_value && r == -1) {
    891 			*overflow = true;
    892 			return l;
    893 		}
    894 		return l / r;
    895 	case MOD:
    896 		if (r == 0) {
    897 			/* modulus by 0 */
    898 			error(140);
    899 			return 0;
    900 		}
    901 		if (l == min_value && r == -1) {
    902 			*overflow = true;
    903 			return 0;
    904 		}
    905 		return l % r;
    906 	case PLUS:
    907 		if (r > 0 && l > max_value - r) {
    908 			*overflow = true;
    909 			return max_value;
    910 		}
    911 		if (r < 0 && l < min_value - r) {
    912 			*overflow = true;
    913 			return min_value;
    914 		}
    915 		return l + r;
    916 	case MINUS:
    917 		if (r > 0 && l < min_value + r) {
    918 			*overflow = true;
    919 			return min_value;
    920 		}
    921 		if (r < 0 && l > max_value + r) {
    922 			*overflow = true;
    923 			return max_value;
    924 		}
    925 		return l - r;
    926 	case SHL:
    927 		/* TODO: warn about out-of-bounds 'r'. */
    928 		/* TODO: warn about overflow. */
    929 		return l << (r & 63);
    930 	case SHR:
    931 		/* TODO: warn about out-of-bounds 'r'. */
    932 		if (l < 0)
    933 			return (int64_t)~(~(uint64_t)l >> (r & 63));
    934 		return (int64_t)((uint64_t)l >> (r & 63));
    935 	case LT:
    936 		return l < r ? 1 : 0;
    937 	case LE:
    938 		return l <= r ? 1 : 0;
    939 	case GT:
    940 		return l > r ? 1 : 0;
    941 	case GE:
    942 		return l >= r ? 1 : 0;
    943 	case EQ:
    944 		return l == r ? 1 : 0;
    945 	case NE:
    946 		return l != r ? 1 : 0;
    947 	case BITAND:
    948 		return l & r;
    949 	case BITXOR:
    950 		return l ^ r;
    951 	case BITOR:
    952 		return l | r;
    953 	default:
    954 		lint_assert(/*CONSTCOND*/false);
    955 		/* NOTREACHED */
    956 	}
    957 }
    958 
    959 static tnode_t *
    960 fold_constant_integer(tnode_t *tn)
    961 {
    962 
    963 	lint_assert(has_operands(tn));
    964 	tspec_t t = tn->u.ops.left->tn_type->t_tspec;
    965 	int64_t l = tn->u.ops.left->u.value.u.integer;
    966 	int64_t r = is_binary(tn) ? tn->u.ops.right->u.value.u.integer : 0;
    967 	uint64_t mask = value_bits(size_in_bits(t));
    968 
    969 	int64_t res;
    970 	bool overflow = false;
    971 	if (!is_integer(t) || is_uinteger(t)) {
    972 		uint64_t u_res = fold_unsigned_integer(tn->tn_op,
    973 		    (uint64_t)l, (uint64_t)r, mask, &overflow);
    974 		if (u_res > mask)
    975 			overflow = true;
    976 		res = (int64_t)u_res;
    977 		if (overflow && hflag) {
    978 			char buf[128];
    979 			if (is_binary(tn)) {
    980 				snprintf(buf, sizeof(buf), "%ju %s %ju",
    981 				    (uintmax_t)l, op_name(tn->tn_op),
    982 				    (uintmax_t)r);
    983 			} else {
    984 				snprintf(buf, sizeof(buf), "%s%ju",
    985 				    op_name(tn->tn_op), (uintmax_t)l);
    986 			}
    987 			/* '%s' overflows '%s' */
    988 			warning(141, buf, type_name(tn->tn_type));
    989 		}
    990 	} else {
    991 		int64_t max_value = (int64_t)(mask >> 1);
    992 		int64_t min_value = -max_value - 1;
    993 		res = fold_signed_integer(tn->tn_op,
    994 		    l, r, min_value, max_value, &overflow);
    995 		if (res < min_value || res > max_value)
    996 			overflow = true;
    997 		if (overflow && hflag) {
    998 			char buf[128];
    999 			if (is_binary(tn)) {
   1000 				snprintf(buf, sizeof(buf), "%jd %s %jd",
   1001 				    (intmax_t)l, op_name(tn->tn_op),
   1002 				    (intmax_t)r);
   1003 			} else if (tn->tn_op == UMINUS && l < 0) {
   1004 				snprintf(buf, sizeof(buf), "-(%jd)",
   1005 				    (intmax_t)l);
   1006 			} else {
   1007 				snprintf(buf, sizeof(buf), "%s%jd",
   1008 				    op_name(tn->tn_op), (intmax_t)l);
   1009 			}
   1010 			/* '%s' overflows '%s' */
   1011 			warning(141, buf, type_name(tn->tn_type));
   1012 		}
   1013 	}
   1014 
   1015 	val_t *v = xcalloc(1, sizeof(*v));
   1016 	v->v_tspec = tn->tn_type->t_tspec;
   1017 	v->u.integer = convert_integer(res, t, size_in_bits(t));
   1018 
   1019 	tnode_t *cn = build_constant(tn->tn_type, v);
   1020 	if (tn->u.ops.left->tn_system_dependent)
   1021 		cn->tn_system_dependent = true;
   1022 	if (is_binary(tn) && tn->u.ops.right->tn_system_dependent)
   1023 		cn->tn_system_dependent = true;
   1024 
   1025 	return cn;
   1026 }
   1027 
   1028 static tnode_t *
   1029 build_struct_access(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
   1030 {
   1031 
   1032 	lint_assert(rn->tn_op == NAME);
   1033 	lint_assert(is_member(rn->u.sym));
   1034 
   1035 	bool lvalue = op == ARROW || ln->tn_lvalue;
   1036 
   1037 	if (op == POINT)
   1038 		ln = build_address(sys, ln, true);
   1039 	else if (ln->tn_type->t_tspec != PTR) {
   1040 		lint_assert(!allow_c90);
   1041 		lint_assert(is_integer(ln->tn_type->t_tspec));
   1042 		ln = convert(NOOP, 0, expr_derive_type(gettyp(VOID), PTR), ln);
   1043 	}
   1044 
   1045 	tnode_t *ctn = build_integer_constant(PTRDIFF_TSPEC,
   1046 	    rn->u.sym->u.s_member.sm_offset_in_bits / CHAR_SIZE);
   1047 
   1048 	type_t *ptr_tp = expr_derive_type(rn->tn_type, PTR);
   1049 	tnode_t *ntn = build_op(PLUS, sys, ptr_tp, ln, ctn);
   1050 	if (ln->tn_op == CON)
   1051 		ntn = fold_constant_integer(ntn);
   1052 
   1053 	op_t nop = rn->tn_type->t_bitfield ? FSEL : INDIR;
   1054 	ntn = build_op(nop, sys, ntn->tn_type->t_subt, ntn, NULL);
   1055 	if (!lvalue)
   1056 		ntn->tn_lvalue = false;
   1057 
   1058 	return ntn;
   1059 }
   1060 
   1061 /*
   1062  * Get the size in bytes of type tp->t_subt, as a constant expression of type
   1063  * ptrdiff_t as seen from the target platform.
   1064  */
   1065 static tnode_t *
   1066 subt_size_in_bytes(type_t *tp)
   1067 {
   1068 
   1069 	lint_assert(tp->t_tspec == PTR);
   1070 	tp = tp->t_subt;
   1071 
   1072 	int elem = 1;
   1073 	while (tp->t_tspec == ARRAY) {
   1074 		elem *= tp->u.dimension;
   1075 		tp = tp->t_subt;
   1076 	}
   1077 
   1078 	int elsz_in_bits = 0;
   1079 	switch (tp->t_tspec) {
   1080 	case FUNC:
   1081 		/* pointer to function is not allowed here */
   1082 		error(110);
   1083 		break;
   1084 	case VOID:
   1085 		/* cannot do pointer arithmetic on operand of unknown size */
   1086 		gnuism(136);
   1087 		break;
   1088 	case STRUCT:
   1089 	case UNION:
   1090 		if ((elsz_in_bits = (int)tp->u.sou->sou_size_in_bits) == 0)
   1091 			/* cannot do pointer arithmetic on operand of ... */
   1092 			error(136);
   1093 		break;
   1094 	case ENUM:
   1095 		if (is_incomplete(tp))
   1096 			/* cannot do pointer arithmetic on operand of ... */
   1097 			warning(136);
   1098 		/* FALLTHROUGH */
   1099 	default:
   1100 		if ((elsz_in_bits = size_in_bits(tp->t_tspec)) == 0)
   1101 			/* cannot do pointer arithmetic on operand of ... */
   1102 			error(136);
   1103 		else
   1104 			lint_assert(elsz_in_bits != -1);
   1105 		break;
   1106 	}
   1107 
   1108 	if (elem == 0 && elsz_in_bits != 0)
   1109 		/* cannot do pointer arithmetic on operand of unknown size */
   1110 		error(136);
   1111 
   1112 	if (elsz_in_bits == 0)
   1113 		elsz_in_bits = CHAR_SIZE;
   1114 
   1115 	return build_integer_constant(PTRDIFF_TSPEC,
   1116 	    (int64_t)(elem * elsz_in_bits / CHAR_SIZE));
   1117 }
   1118 
   1119 static tnode_t *
   1120 build_prepost_incdec(op_t op, bool sys, tnode_t *ln)
   1121 {
   1122 
   1123 	lint_assert(ln != NULL);
   1124 	tnode_t *cn = ln->tn_type->t_tspec == PTR
   1125 	    ? subt_size_in_bytes(ln->tn_type)
   1126 	    : build_integer_constant(INT, 1);
   1127 	return build_op(op, sys, ln->tn_type, ln, cn);
   1128 }
   1129 
   1130 static void
   1131 check_enum_array_index(const tnode_t *ln, const tnode_t *rn)
   1132 {
   1133 
   1134 	if (ln->tn_op != ADDR || ln->u.ops.left->tn_op != NAME)
   1135 		return;
   1136 
   1137 	const type_t *ltp = ln->u.ops.left->tn_type;
   1138 	if (ltp->t_tspec != ARRAY || ltp->t_incomplete_array)
   1139 		return;
   1140 
   1141 	if (rn->tn_op != CVT || !rn->tn_type->t_is_enum)
   1142 		return;
   1143 	if (rn->u.ops.left->tn_op != LOAD)
   1144 		return;
   1145 
   1146 	const type_t *rtp = rn->u.ops.left->tn_type;
   1147 	const sym_t *ec = rtp->u.enumer->en_first_enumerator;
   1148 	const sym_t *max_ec = ec;
   1149 	lint_assert(ec != NULL);
   1150 	for (ec = ec->s_next; ec != NULL; ec = ec->s_next)
   1151 		if (ec->u.s_enum_constant > max_ec->u.s_enum_constant)
   1152 			max_ec = ec;
   1153 
   1154 	int64_t max_enum_value = max_ec->u.s_enum_constant;
   1155 	lint_assert(INT_MIN <= max_enum_value && max_enum_value <= INT_MAX);
   1156 
   1157 	int max_array_index = ltp->u.dimension - 1;
   1158 	if (max_enum_value == max_array_index)
   1159 		return;
   1160 
   1161 	if (max_enum_value == max_array_index + 1 &&
   1162 	    (strstr(max_ec->s_name, "MAX") != NULL ||
   1163 	     strstr(max_ec->s_name, "max") != NULL ||
   1164 	     strstr(max_ec->s_name, "NUM") != NULL ||
   1165 	     strstr(max_ec->s_name, "num") != NULL))
   1166 		return;
   1167 
   1168 	/* maximum value %d of '%s' does not match maximum array index %d */
   1169 	warning(348, (int)max_enum_value, type_name(rtp), max_array_index);
   1170 	print_previous_declaration(max_ec);
   1171 }
   1172 
   1173 static tnode_t *
   1174 build_plus_minus(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
   1175 {
   1176 
   1177 	if (rn->tn_type->t_tspec == PTR && is_integer(ln->tn_type->t_tspec)) {
   1178 		tnode_t *tmp = ln;
   1179 		ln = rn;
   1180 		rn = tmp;
   1181 		/* pointer addition has integer on the left-hand side */
   1182 		query_message(5);
   1183 	}
   1184 
   1185 	/* pointer +- integer */
   1186 	tspec_t lt = ln->tn_type->t_tspec;
   1187 	tspec_t rt = rn->tn_type->t_tspec;
   1188 	if (lt == PTR && rt != PTR) {
   1189 		lint_assert(is_integer(rt));
   1190 
   1191 		check_ctype_macro_invocation(ln, rn);
   1192 		check_enum_array_index(ln, rn);
   1193 
   1194 		tnode_t *elsz = subt_size_in_bytes(ln->tn_type);
   1195 		tspec_t szt = elsz->tn_type->t_tspec;
   1196 		if (rt != szt && rt != unsigned_type(szt))
   1197 			rn = convert(NOOP, 0, elsz->tn_type, rn);
   1198 
   1199 		tnode_t *prod = build_op(MULT, sys, rn->tn_type, rn, elsz);
   1200 		if (rn->tn_op == CON)
   1201 			prod = fold_constant_integer(prod);
   1202 
   1203 		return build_op(op, sys, ln->tn_type, ln, prod);
   1204 	}
   1205 
   1206 	/* pointer - pointer */
   1207 	if (rt == PTR) {
   1208 		lint_assert(lt == PTR);
   1209 		lint_assert(op == MINUS);
   1210 
   1211 		type_t *ptrdiff = gettyp(PTRDIFF_TSPEC);
   1212 		tnode_t *raw_diff = build_op(op, sys, ptrdiff, ln, rn);
   1213 		if (ln->tn_op == CON && rn->tn_op == CON)
   1214 			raw_diff = fold_constant_integer(raw_diff);
   1215 
   1216 		tnode_t *elsz = subt_size_in_bytes(ln->tn_type);
   1217 		balance(NOOP, &raw_diff, &elsz);
   1218 
   1219 		return build_op(DIV, sys, ptrdiff, raw_diff, elsz);
   1220 	}
   1221 
   1222 	return build_op(op, sys, ln->tn_type, ln, rn);
   1223 }
   1224 
   1225 static tnode_t *
   1226 build_bit_shift(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
   1227 {
   1228 
   1229 	if (!allow_c90 && rn->tn_type->t_tspec != INT)
   1230 		// XXX: C1978 7.5 says: "Both [operators] perform the usual
   1231 		// arithmetic conversions on their operands."
   1232 		// TODO: Add a test to exercise this part of the code.
   1233 		rn = convert(NOOP, 0, gettyp(INT), rn);
   1234 	return build_op(op, sys, ln->tn_type, ln, rn);
   1235 }
   1236 
   1237 static bool
   1238 is_null_pointer(const tnode_t *tn)
   1239 {
   1240 	tspec_t t = tn->tn_type->t_tspec;
   1241 
   1242 	// TODO: Investigate how other pointers are stored, in particular,
   1243 	// whether a pointer constant can have a non-zero value.
   1244 	// If not, simplify the code below.
   1245 	return ((t == PTR && tn->tn_type->t_subt->t_tspec == VOID)
   1246 		|| is_integer(t))
   1247 	    && (tn->tn_op == CON && tn->u.value.u.integer == 0);
   1248 }
   1249 
   1250 /* Return a type based on tp1, with added qualifiers from tp2. */
   1251 static type_t *
   1252 merge_qualifiers(type_t *tp1, const type_t *tp2)
   1253 {
   1254 
   1255 	lint_assert(tp1->t_tspec == PTR);
   1256 	lint_assert(tp2->t_tspec == PTR);
   1257 
   1258 	bool c1 = tp1->t_subt->t_const;
   1259 	bool c2 = tp2->t_subt->t_const;
   1260 	bool v1 = tp1->t_subt->t_volatile;
   1261 	bool v2 = tp2->t_subt->t_volatile;
   1262 
   1263 	if (c1 == (c1 | c2) && v1 == (v1 | v2))
   1264 		return tp1;
   1265 
   1266 	type_t *nstp = expr_dup_type(tp1->t_subt);
   1267 	nstp->t_const |= c2;
   1268 	nstp->t_volatile |= v2;
   1269 
   1270 	type_t *ntp = expr_dup_type(tp1);
   1271 	ntp->t_subt = nstp;
   1272 	return ntp;
   1273 }
   1274 
   1275 /* See C99 6.5.15 "Conditional operator". */
   1276 static tnode_t *
   1277 build_colon(bool sys, tnode_t *ln, tnode_t *rn)
   1278 {
   1279 	tspec_t lt = ln->tn_type->t_tspec;
   1280 	tspec_t rt = rn->tn_type->t_tspec;
   1281 
   1282 	type_t *tp;
   1283 	if (is_arithmetic(lt) && is_arithmetic(rt))
   1284 		/* The operands were already balanced in build_binary. */
   1285 		tp = ln->tn_type;
   1286 	else if (lt == BOOL && rt == BOOL)
   1287 		tp = ln->tn_type;
   1288 	else if (lt == VOID || rt == VOID)
   1289 		tp = gettyp(VOID);
   1290 	else if (is_struct_or_union(lt)) {
   1291 		lint_assert(is_struct_or_union(rt));
   1292 		lint_assert(ln->tn_type->u.sou == rn->tn_type->u.sou);
   1293 		if (is_incomplete(ln->tn_type)) {
   1294 			/* unknown operand size, op '%s' */
   1295 			error(138, op_name(COLON));
   1296 			return NULL;
   1297 		}
   1298 		tp = ln->tn_type;
   1299 	} else if (lt == PTR && is_integer(rt)) {
   1300 		if (rt != PTRDIFF_TSPEC)
   1301 			rn = convert(NOOP, 0, gettyp(PTRDIFF_TSPEC), rn);
   1302 		tp = ln->tn_type;
   1303 	} else if (rt == PTR && is_integer(lt)) {
   1304 		if (lt != PTRDIFF_TSPEC)
   1305 			ln = convert(NOOP, 0, gettyp(PTRDIFF_TSPEC), ln);
   1306 		tp = rn->tn_type;
   1307 	} else if (lt == PTR && is_null_pointer(rn))
   1308 		tp = merge_qualifiers(ln->tn_type, rn->tn_type);
   1309 	else if (rt == PTR && is_null_pointer(ln))
   1310 		tp = merge_qualifiers(rn->tn_type, ln->tn_type);
   1311 	else if (lt == PTR && ln->tn_type->t_subt->t_tspec == VOID)
   1312 		tp = merge_qualifiers(ln->tn_type, rn->tn_type);
   1313 	else if (rt == PTR && rn->tn_type->t_subt->t_tspec == VOID)
   1314 		tp = merge_qualifiers(rn->tn_type, ln->tn_type);
   1315 	else {
   1316 		/*
   1317 		 * XXX For now we simply take the left type. This is probably
   1318 		 * wrong, if one type contains a function prototype and the
   1319 		 * other one, at the same place, only an old-style declaration.
   1320 		 */
   1321 		tp = merge_qualifiers(ln->tn_type, rn->tn_type);
   1322 	}
   1323 
   1324 	return build_op(COLON, sys, tp, ln, rn);
   1325 }
   1326 
   1327 /* TODO: check for varargs */
   1328 static bool
   1329 is_cast_redundant(const tnode_t *tn)
   1330 {
   1331 	const type_t *ntp = tn->tn_type, *otp = tn->u.ops.left->tn_type;
   1332 	tspec_t nt = ntp->t_tspec, ot = otp->t_tspec;
   1333 
   1334 	if (nt == BOOL || ot == BOOL)
   1335 		return nt == BOOL && ot == BOOL;
   1336 
   1337 	if (is_integer(nt) && is_integer(ot)) {
   1338 		unsigned int nw = width_in_bits(ntp), ow = width_in_bits(otp);
   1339 		if (is_uinteger(nt) == is_uinteger(ot))
   1340 			return nw >= ow;
   1341 		return is_uinteger(ot) && nw > ow;
   1342 	}
   1343 
   1344 	if (is_complex(nt) || is_complex(ot))
   1345 		return is_complex(nt) && is_complex(ot) &&
   1346 		    size_in_bits(nt) >= size_in_bits(ot);
   1347 
   1348 	if (is_floating(nt) && is_floating(ot))
   1349 		return size_in_bits(nt) >= size_in_bits(ot);
   1350 
   1351 	if (nt == PTR && ot == PTR) {
   1352 		if (!ntp->t_subt->t_const && otp->t_subt->t_const)
   1353 			return false;
   1354 		if (!ntp->t_subt->t_volatile && otp->t_subt->t_volatile)
   1355 			return false;
   1356 
   1357 		if (ntp->t_subt->t_tspec == VOID ||
   1358 		    otp->t_subt->t_tspec == VOID ||
   1359 		    types_compatible(ntp->t_subt, otp->t_subt,
   1360 			false, false, NULL))
   1361 			return true;
   1362 	}
   1363 
   1364 	return false;
   1365 }
   1366 
   1367 static bool
   1368 is_assignment(op_t op)
   1369 {
   1370 
   1371 	return op == ASSIGN ||
   1372 	    op == MULASS ||
   1373 	    op == DIVASS ||
   1374 	    op == MODASS ||
   1375 	    op == ADDASS ||
   1376 	    op == SUBASS ||
   1377 	    op == SHLASS ||
   1378 	    op == SHRASS ||
   1379 	    op == ANDASS ||
   1380 	    op == XORASS ||
   1381 	    op == ORASS ||
   1382 	    op == RETURN ||
   1383 	    op == INIT;
   1384 }
   1385 
   1386 static tnode_t *
   1387 build_assignment(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
   1388 {
   1389 
   1390 	tspec_t lt = ln->tn_type->t_tspec;
   1391 	tspec_t rt = rn->tn_type->t_tspec;
   1392 
   1393 	if (any_query_enabled && is_assignment(rn->tn_op)) {
   1394 		/* chained assignment with '%s' and '%s' */
   1395 		query_message(10, op_name(op), op_name(rn->tn_op));
   1396 	}
   1397 
   1398 	if ((op == ADDASS || op == SUBASS) && lt == PTR) {
   1399 		lint_assert(is_integer(rt));
   1400 		tnode_t *ctn = subt_size_in_bytes(ln->tn_type);
   1401 		if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
   1402 			rn = convert(NOOP, 0, ctn->tn_type, rn);
   1403 		rn = build_op(MULT, sys, rn->tn_type, rn, ctn);
   1404 		if (rn->u.ops.left->tn_op == CON)
   1405 			rn = fold_constant_integer(rn);
   1406 	}
   1407 
   1408 	if ((op == ASSIGN || op == RETURN || op == INIT) &&
   1409 	    (lt == STRUCT || rt == STRUCT)) {
   1410 		lint_assert(lt == rt);
   1411 		lint_assert(ln->tn_type->u.sou == rn->tn_type->u.sou);
   1412 		if (is_incomplete(ln->tn_type)) {
   1413 			if (op == RETURN)
   1414 				/* cannot return incomplete type */
   1415 				error(212);
   1416 			else
   1417 				/* unknown operand size, op '%s' */
   1418 				error(138, op_name(op));
   1419 			return NULL;
   1420 		}
   1421 	}
   1422 
   1423 	if (op == SHLASS && hflag && allow_trad && allow_c90
   1424 	    && portable_rank_cmp(lt, rt) < 0)
   1425 		/* semantics of '%s' change in C90; ... */
   1426 		warning(118, "<<=");
   1427 
   1428 	if (op != SHLASS && op != SHRASS
   1429 	    && (op == ASSIGN || lt != PTR)
   1430 	    && (lt != rt || (ln->tn_type->t_bitfield && rn->tn_op == CON))) {
   1431 		rn = convert(op, 0, ln->tn_type, rn);
   1432 		rt = lt;
   1433 	}
   1434 
   1435 	if (any_query_enabled && rn->tn_op == CVT && rn->tn_cast &&
   1436 	    types_compatible(ln->tn_type, rn->tn_type, false, false, NULL) &&
   1437 	    is_cast_redundant(rn)) {
   1438 		/* redundant cast from '%s' to '%s' before assignment */
   1439 		query_message(7, type_name(rn->u.ops.left->tn_type),
   1440 		    type_name(rn->tn_type));
   1441 	}
   1442 
   1443 	return build_op(op, sys, ln->tn_type, ln, rn);
   1444 }
   1445 
   1446 static tnode_t *
   1447 build_real_imag(op_t op, bool sys, tnode_t *ln)
   1448 {
   1449 
   1450 	lint_assert(ln != NULL);
   1451 	if (ln->tn_op == NAME) {
   1452 		/*
   1453 		 * This may be too much, but it avoids wrong warnings. See
   1454 		 * d_c99_complex_split.c.
   1455 		 */
   1456 		mark_as_used(ln->u.sym, false, false);
   1457 		mark_as_set(ln->u.sym);
   1458 	}
   1459 
   1460 	tspec_t t;
   1461 	switch (ln->tn_type->t_tspec) {
   1462 	case LCOMPLEX:
   1463 		t = LDOUBLE;
   1464 		break;
   1465 	case DCOMPLEX:
   1466 		t = DOUBLE;
   1467 		break;
   1468 	case FCOMPLEX:
   1469 		t = FLOAT;
   1470 		break;
   1471 	default:
   1472 		/* '__%s__' is illegal for type '%s' */
   1473 		error(276, op == REAL ? "real" : "imag",
   1474 		    type_name(ln->tn_type));
   1475 		return NULL;
   1476 	}
   1477 
   1478 	tnode_t *ntn = build_op(op, sys, gettyp(t), ln, NULL);
   1479 	ntn->tn_lvalue = true;
   1480 	return ntn;
   1481 }
   1482 
   1483 static bool
   1484 is_confusing_precedence(op_t op, op_t lop, bool lparen, op_t rop, bool rparen)
   1485 {
   1486 
   1487 	if (op == SHL || op == SHR) {
   1488 		if (!lparen && (lop == PLUS || lop == MINUS))
   1489 			return true;
   1490 		if (!rparen && (rop == PLUS || rop == MINUS))
   1491 			return true;
   1492 		return false;
   1493 	}
   1494 
   1495 	if (op == LOGOR) {
   1496 		if (!lparen && lop == LOGAND)
   1497 			return true;
   1498 		if (!rparen && rop == LOGAND)
   1499 			return true;
   1500 		return false;
   1501 	}
   1502 
   1503 	lint_assert(op == BITAND || op == BITXOR || op == BITOR);
   1504 	if (!lparen && lop != op) {
   1505 		if (lop == PLUS || lop == MINUS)
   1506 			return true;
   1507 		if (lop == BITAND || lop == BITXOR)
   1508 			return true;
   1509 	}
   1510 	if (!rparen && rop != op) {
   1511 		if (rop == PLUS || rop == MINUS)
   1512 			return true;
   1513 		if (rop == BITAND || rop == BITXOR)
   1514 			return true;
   1515 	}
   1516 	return false;
   1517 }
   1518 
   1519 /*
   1520  * Print a warning if the given node has operands which should be
   1521  * parenthesized.
   1522  *
   1523  * XXX Does not work if an operand is a constant expression. Constant
   1524  * expressions are already folded.
   1525  */
   1526 static void
   1527 check_precedence_confusion(tnode_t *tn)
   1528 {
   1529 	tnode_t *ln, *rn;
   1530 
   1531 	if (!hflag)
   1532 		return;
   1533 
   1534 	debug_node(tn);
   1535 
   1536 	lint_assert(is_binary(tn));
   1537 	for (ln = tn->u.ops.left; ln->tn_op == CVT; ln = ln->u.ops.left)
   1538 		continue;
   1539 	for (rn = tn->u.ops.right; rn->tn_op == CVT; rn = rn->u.ops.left)
   1540 		continue;
   1541 
   1542 	if (is_confusing_precedence(tn->tn_op,
   1543 	    ln->tn_op, ln->tn_parenthesized,
   1544 	    rn->tn_op, rn->tn_parenthesized)) {
   1545 		/* precedence confusion possible: parenthesize! */
   1546 		warning(169);
   1547 	}
   1548 }
   1549 
   1550 static tnode_t *
   1551 fold_constant_compare_zero(tnode_t *tn)
   1552 {
   1553 
   1554 	val_t *v = xcalloc(1, sizeof(*v));
   1555 	v->v_tspec = tn->tn_type->t_tspec;
   1556 	lint_assert(v->v_tspec == INT || (Tflag && v->v_tspec == BOOL));
   1557 
   1558 	lint_assert(has_operands(tn));
   1559 	bool l = constant_is_nonzero(tn->u.ops.left);
   1560 	bool r = is_binary(tn) && constant_is_nonzero(tn->u.ops.right);
   1561 
   1562 	switch (tn->tn_op) {
   1563 	case NOT:
   1564 		if (hflag && !suppress_constcond)
   1565 			/* constant operand to '!' */
   1566 			warning(239);
   1567 		v->u.integer = !l ? 1 : 0;
   1568 		break;
   1569 	case LOGAND:
   1570 		v->u.integer = l && r ? 1 : 0;
   1571 		break;
   1572 	case LOGOR:
   1573 		v->u.integer = l || r ? 1 : 0;
   1574 		break;
   1575 	default:
   1576 		lint_assert(/*CONSTCOND*/false);
   1577 	}
   1578 
   1579 	return build_constant(tn->tn_type, v);
   1580 }
   1581 
   1582 static long double
   1583 floating_error_value(tspec_t t, long double lv)
   1584 {
   1585 	if (t == FLOAT)
   1586 		return lv < 0 ? -FLT_MAX : FLT_MAX;
   1587 	if (t == DOUBLE)
   1588 		return lv < 0 ? -DBL_MAX : DBL_MAX;
   1589 	/*
   1590 	 * When NetBSD is cross-built in MKLINT=yes mode on x86_64 for sparc64,
   1591 	 * tools/lint checks this code while building usr.bin/xlint. In that
   1592 	 * situation, lint uses the preprocessor for sparc64, in which the type
   1593 	 * 'long double' is IEEE-754-binary128, affecting the macro LDBL_MAX
   1594 	 * below. The type 'long double', as well as the strtold
   1595 	 * implementation, comes from the host platform x86_64 though, where
   1596 	 * 'long double' consumes 128 bits as well but only uses 80 of them.
   1597 	 * The exponent range of the two 'long double' types is the same, but
   1598 	 * the maximum finite value differs due to the extended precision on
   1599 	 * sparc64.
   1600 	 *
   1601 	 * To properly handle the data types of the target platform, lint would
   1602 	 * have to implement the floating-point types in a platform-independent
   1603 	 * way, which is not worth the effort, given how few programs
   1604 	 * practically use 'long double'.
   1605 	 */
   1606 	/* LINTED 248: floating-point constant out of range */
   1607 	long double max = LDBL_MAX;
   1608 	return lv < 0 ? -max : max;
   1609 }
   1610 
   1611 static bool
   1612 is_floating_overflow(tspec_t t, long double val)
   1613 {
   1614 	if (fpe != 0 || isfinite(val) == 0)
   1615 		return true;
   1616 	if (t == FLOAT && (val > FLT_MAX || val < -FLT_MAX))
   1617 		return true;
   1618 	if (t == DOUBLE && (val > DBL_MAX || val < -DBL_MAX))
   1619 		return true;
   1620 	return false;
   1621 }
   1622 
   1623 static tnode_t *
   1624 fold_constant_floating(tnode_t *tn)
   1625 {
   1626 
   1627 	fpe = 0;
   1628 
   1629 	tspec_t t = tn->tn_type->t_tspec;
   1630 
   1631 	val_t *v = xcalloc(1, sizeof(*v));
   1632 	v->v_tspec = t;
   1633 
   1634 	lint_assert(is_floating(t));
   1635 	lint_assert(has_operands(tn));
   1636 	lint_assert(t == tn->u.ops.left->tn_type->t_tspec);
   1637 	lint_assert(!is_binary(tn) || t == tn->u.ops.right->tn_type->t_tspec);
   1638 
   1639 	long double lv = tn->u.ops.left->u.value.u.floating;
   1640 	long double rv = is_binary(tn) ? tn->u.ops.right->u.value.u.floating
   1641 	    : 0.0;
   1642 
   1643 	switch (tn->tn_op) {
   1644 	case UPLUS:
   1645 		v->u.floating = lv;
   1646 		break;
   1647 	case UMINUS:
   1648 		v->u.floating = -lv;
   1649 		break;
   1650 	case MULT:
   1651 		v->u.floating = lv * rv;
   1652 		break;
   1653 	case DIV:
   1654 		if (rv == 0.0) {
   1655 			/* division by 0 */
   1656 			error(139);
   1657 			v->u.floating = floating_error_value(t, lv);
   1658 		} else {
   1659 			v->u.floating = lv / rv;
   1660 		}
   1661 		break;
   1662 	case PLUS:
   1663 		v->u.floating = lv + rv;
   1664 		break;
   1665 	case MINUS:
   1666 		v->u.floating = lv - rv;
   1667 		break;
   1668 	case LT:
   1669 		v->u.integer = lv < rv ? 1 : 0;
   1670 		break;
   1671 	case LE:
   1672 		v->u.integer = lv <= rv ? 1 : 0;
   1673 		break;
   1674 	case GE:
   1675 		v->u.integer = lv >= rv ? 1 : 0;
   1676 		break;
   1677 	case GT:
   1678 		v->u.integer = lv > rv ? 1 : 0;
   1679 		break;
   1680 	case EQ:
   1681 		v->u.integer = lv == rv ? 1 : 0;
   1682 		break;
   1683 	case NE:
   1684 		v->u.integer = lv != rv ? 1 : 0;
   1685 		break;
   1686 	default:
   1687 		lint_assert(/*CONSTCOND*/false);
   1688 	}
   1689 
   1690 	// XXX: Must not access u.floating after setting u.integer.
   1691 	lint_assert(fpe != 0 || isnan(v->u.floating) == 0);
   1692 	if (is_complex(v->v_tspec)) {
   1693 		/*
   1694 		 * Don't warn, as lint doesn't model the imaginary part of
   1695 		 * complex numbers.
   1696 		 */
   1697 		fpe = 0;
   1698 	} else if (is_floating_overflow(t, v->u.floating)) {
   1699 		/* operator '%s' produces floating point overflow */
   1700 		warning(142, op_name(tn->tn_op));
   1701 		v->u.floating = floating_error_value(t, v->u.floating);
   1702 		fpe = 0;
   1703 	}
   1704 
   1705 	return build_constant(tn->tn_type, v);
   1706 }
   1707 
   1708 static void
   1709 use(const tnode_t *tn)
   1710 {
   1711 	if (tn == NULL)
   1712 		return;
   1713 	switch (tn->tn_op) {
   1714 	case NAME:
   1715 		mark_as_used(tn->u.sym, false /* XXX */, false /* XXX */);
   1716 		break;
   1717 	case CON:
   1718 	case STRING:
   1719 		break;
   1720 	case CALL:
   1721 	case ICALL:;
   1722 		const function_call *call = tn->u.call;
   1723 		for (size_t i = 0, n = call->args_len; i < n; i++)
   1724 			use(call->args[i]);
   1725 		break;
   1726 	default:
   1727 		lint_assert(has_operands(tn));
   1728 		use(tn->u.ops.left);
   1729 		if (is_binary(tn))
   1730 			use(tn->u.ops.right);
   1731 	}
   1732 }
   1733 
   1734 /*
   1735  * Create a tree node for a binary operator and its two operands. Also called
   1736  * for unary operators; in that case rn is NULL.
   1737  *
   1738  * Function calls, sizeof and casts are handled elsewhere.
   1739  */
   1740 tnode_t *
   1741 build_binary(tnode_t *ln, op_t op, bool sys, tnode_t *rn)
   1742 {
   1743 	const mod_t *mp = &modtab[op];
   1744 
   1745 	/* If there was an error in one of the operands, return. */
   1746 	if (ln == NULL || (mp->m_binary && rn == NULL))
   1747 		return NULL;
   1748 
   1749 	if (mp->m_value_context || mp->m_compares_with_zero)
   1750 		ln = cconv(ln);
   1751 	if (mp->m_binary && op != ARROW && op != POINT)
   1752 		rn = cconv(rn);
   1753 
   1754 	if (mp->m_comparison)
   1755 		check_integer_comparison(op, ln, rn);
   1756 
   1757 	if (mp->m_value_context || mp->m_compares_with_zero)
   1758 		ln = promote(op, false, ln);
   1759 	if (mp->m_binary && op != ARROW && op != POINT &&
   1760 	    op != ASSIGN && op != RETURN && op != INIT)
   1761 		rn = promote(op, false, rn);
   1762 
   1763 	if (mp->m_warn_if_left_unsigned_in_c90 &&
   1764 	    ln->tn_op == CON && ln->u.value.v_unsigned_since_c90) {
   1765 		/* C90 treats constant as unsigned, op '%s' */
   1766 		warning(218, op_name(op));
   1767 		ln->u.value.v_unsigned_since_c90 = false;
   1768 	}
   1769 	if (mp->m_warn_if_right_unsigned_in_c90 &&
   1770 	    rn->tn_op == CON && rn->u.value.v_unsigned_since_c90) {
   1771 		/* C90 treats constant as unsigned, op '%s' */
   1772 		warning(218, op_name(op));
   1773 		rn->u.value.v_unsigned_since_c90 = false;
   1774 	}
   1775 
   1776 	if (mp->m_balance_operands || (!allow_c90 && (op == SHL || op == SHR)))
   1777 		balance(op, &ln, &rn);
   1778 
   1779 	if (!typeok(op, 0, ln, rn))
   1780 		return NULL;
   1781 
   1782 	tnode_t *ntn;
   1783 	switch (op) {
   1784 	case POINT:
   1785 	case ARROW:
   1786 		ntn = build_struct_access(op, sys, ln, rn);
   1787 		break;
   1788 	case INCAFT:
   1789 	case DECAFT:
   1790 	case INCBEF:
   1791 	case DECBEF:
   1792 		ntn = build_prepost_incdec(op, sys, ln);
   1793 		break;
   1794 	case ADDR:
   1795 		ntn = build_address(sys, ln, false);
   1796 		break;
   1797 	case INDIR:
   1798 		ntn = build_op(INDIR, sys, ln->tn_type->t_subt, ln, NULL);
   1799 		break;
   1800 	case PLUS:
   1801 	case MINUS:
   1802 		ntn = build_plus_minus(op, sys, ln, rn);
   1803 		break;
   1804 	case SHL:
   1805 	case SHR:
   1806 		ntn = build_bit_shift(op, sys, ln, rn);
   1807 		break;
   1808 	case COLON:
   1809 		ntn = build_colon(sys, ln, rn);
   1810 		break;
   1811 	case ASSIGN:
   1812 	case MULASS:
   1813 	case DIVASS:
   1814 	case MODASS:
   1815 	case ADDASS:
   1816 	case SUBASS:
   1817 	case SHLASS:
   1818 	case SHRASS:
   1819 	case ANDASS:
   1820 	case XORASS:
   1821 	case ORASS:
   1822 	case RETURN:
   1823 	case INIT:
   1824 		ntn = build_assignment(op, sys, ln, rn);
   1825 		break;
   1826 	case COMMA:
   1827 		if (any_query_enabled) {
   1828 			/* comma operator with types '%s' and '%s' */
   1829 			query_message(12,
   1830 			    type_name(ln->tn_type), type_name(rn->tn_type));
   1831 		}
   1832 		/* FALLTHROUGH */
   1833 	case QUEST:
   1834 		ntn = build_op(op, sys, rn->tn_type, ln, rn);
   1835 		break;
   1836 	case REAL:
   1837 	case IMAG:
   1838 		ntn = build_real_imag(op, sys, ln);
   1839 		break;
   1840 	default:
   1841 		lint_assert(mp->m_binary == (rn != NULL));
   1842 		type_t *rettp = mp->m_returns_bool
   1843 		    ? gettyp(Tflag ? BOOL : INT) : ln->tn_type;
   1844 		ntn = build_op(op, sys, rettp, ln, rn);
   1845 		break;
   1846 	}
   1847 
   1848 	if (ntn == NULL)
   1849 		return NULL;
   1850 
   1851 	if (mp->m_possible_precedence_confusion)
   1852 		check_precedence_confusion(ntn);
   1853 
   1854 	if (hflag && !suppress_constcond &&
   1855 	    mp->m_compares_with_zero &&
   1856 	    (ln->tn_op == CON ||
   1857 	     (mp->m_binary && op != QUEST && rn->tn_op == CON)) &&
   1858 	    /* XXX: rn->tn_system_dependent should be checked as well */
   1859 	    !ln->tn_system_dependent)
   1860 		/* constant in conditional context */
   1861 		warning(161);
   1862 
   1863 	if (mp->m_fold_constant_operands && ln->tn_op == CON) {
   1864 		if (!mp->m_binary || rn->tn_op == CON) {
   1865 			if (mp->m_compares_with_zero)
   1866 				ntn = fold_constant_compare_zero(ntn);
   1867 			else if (is_floating(ntn->tn_type->t_tspec))
   1868 				ntn = fold_constant_floating(ntn);
   1869 			else
   1870 				ntn = fold_constant_integer(ntn);
   1871 		} else if (op == QUEST) {
   1872 			lint_assert(has_operands(rn));
   1873 			use(ln->u.value.u.integer != 0
   1874 			    ? rn->u.ops.right : rn->u.ops.left);
   1875 			ntn = ln->u.value.u.integer != 0
   1876 			    ? rn->u.ops.left : rn->u.ops.right;
   1877 		}
   1878 	}
   1879 
   1880 	return ntn;
   1881 }
   1882 
   1883 tnode_t *
   1884 build_unary(op_t op, bool sys, tnode_t *tn)
   1885 {
   1886 	return build_binary(tn, op, sys, NULL);
   1887 }
   1888 
   1889 static bool
   1890 are_members_compatible(const sym_t *a, const sym_t *b)
   1891 {
   1892 	if (a->u.s_member.sm_offset_in_bits != b->u.s_member.sm_offset_in_bits)
   1893 		return false;
   1894 
   1895 	const type_t *atp = a->s_type;
   1896 	const type_t *btp = b->s_type;
   1897 	bool w = false;
   1898 	if (!types_compatible(atp, btp, false, false, &w) && !w)
   1899 		return false;
   1900 	if (a->s_bitfield != b->s_bitfield)
   1901 		return false;
   1902 	if (a->s_bitfield) {
   1903 		if (atp->t_bit_field_width != btp->t_bit_field_width)
   1904 			return false;
   1905 		if (atp->t_bit_field_offset != btp->t_bit_field_offset)
   1906 			return false;
   1907 	}
   1908 	return true;
   1909 }
   1910 
   1911 /*
   1912  * Return whether all struct/union members with the same name have the same
   1913  * type and offset.
   1914  */
   1915 static bool
   1916 all_members_compatible(const sym_t *msym)
   1917 {
   1918 	for (const sym_t *csym = msym;
   1919 	    csym != NULL; csym = csym->s_symtab_next) {
   1920 		if (!is_member(csym))
   1921 			continue;
   1922 		if (strcmp(msym->s_name, csym->s_name) != 0)
   1923 			continue;
   1924 
   1925 		for (const sym_t *sym = csym->s_symtab_next;
   1926 		    sym != NULL; sym = sym->s_symtab_next) {
   1927 			if (is_member(sym)
   1928 			    && strcmp(csym->s_name, sym->s_name) == 0
   1929 			    && !are_members_compatible(csym, sym))
   1930 				return false;
   1931 		}
   1932 	}
   1933 	return true;
   1934 }
   1935 
   1936 sym_t *
   1937 find_member(const struct_or_union *sou, const char *name)
   1938 {
   1939 	for (sym_t *mem = sou->sou_first_member;
   1940 	    mem != NULL; mem = mem->s_next) {
   1941 		lint_assert(is_member(mem));
   1942 		lint_assert(mem->u.s_member.sm_containing_type == sou);
   1943 		if (strcmp(mem->s_name, name) == 0)
   1944 			return mem;
   1945 	}
   1946 
   1947 	for (sym_t *mem = sou->sou_first_member;
   1948 	    mem != NULL; mem = mem->s_next) {
   1949 		if (is_struct_or_union(mem->s_type->t_tspec)
   1950 		    && mem->s_name == unnamed) {
   1951 			sym_t *nested_mem =
   1952 			    find_member(mem->s_type->u.sou, name);
   1953 			if (nested_mem != NULL)
   1954 				return nested_mem;
   1955 		}
   1956 	}
   1957 	return NULL;
   1958 }
   1959 
   1960 /*
   1961  * Remove the member if it was unknown until now, which means
   1962  * that no defined struct or union has a member with the same name.
   1963  */
   1964 static void
   1965 remove_unknown_member(tnode_t *tn, sym_t *msym)
   1966 {
   1967 	/* type '%s' does not have member '%s' */
   1968 	error(101, type_name(tn->tn_type), msym->s_name);
   1969 	symtab_remove_forever(msym);
   1970 	msym->s_kind = SK_MEMBER;
   1971 	msym->s_scl = STRUCT_MEMBER;
   1972 
   1973 	struct_or_union *sou = expr_zero_alloc(sizeof(*sou),
   1974 	    "struct_or_union");
   1975 	sou->sou_tag = expr_zero_alloc(sizeof(*sou->sou_tag), "sym");
   1976 	sou->sou_tag->s_name = unnamed;
   1977 
   1978 	msym->u.s_member.sm_containing_type = sou;
   1979 	/*
   1980 	 * The member sm_offset_in_bits is not needed here since this symbol
   1981 	 * can only be used for error reporting.
   1982 	 */
   1983 }
   1984 
   1985 /*
   1986  * Returns a symbol which has the same name as 'msym' and is a member of the
   1987  * struct or union specified by 'tn'.
   1988  */
   1989 static sym_t *
   1990 struct_or_union_member(tnode_t *tn, op_t op, sym_t *msym)
   1991 {
   1992 
   1993 	/* Determine the tag type of which msym is expected to be a member. */
   1994 	const type_t *tp = NULL;
   1995 	if (op == POINT && is_struct_or_union(tn->tn_type->t_tspec))
   1996 		tp = tn->tn_type;
   1997 	if (op == ARROW && tn->tn_type->t_tspec == PTR
   1998 	    && is_struct_or_union(tn->tn_type->t_subt->t_tspec))
   1999 		tp = tn->tn_type->t_subt;
   2000 	struct_or_union *sou = tp != NULL ? tp->u.sou : NULL;
   2001 
   2002 	if (sou != NULL) {
   2003 		sym_t *nested_mem = find_member(sou, msym->s_name);
   2004 		if (nested_mem != NULL)
   2005 			return nested_mem;
   2006 	}
   2007 
   2008 	if (msym->s_scl == NO_SCL) {
   2009 		remove_unknown_member(tn, msym);
   2010 		return msym;
   2011 	}
   2012 
   2013 	bool eq = all_members_compatible(msym);
   2014 
   2015 	/*
   2016 	 * Now handle the case in which the left operand refers really to a
   2017 	 * struct/union, but the right operand is not member of it.
   2018 	 */
   2019 	if (sou != NULL) {
   2020 		if (eq && !allow_c90)
   2021 			/* illegal use of member '%s' */
   2022 			warning(102, msym->s_name);
   2023 		else
   2024 			/* illegal use of member '%s' */
   2025 			error(102, msym->s_name);
   2026 		return msym;
   2027 	}
   2028 
   2029 	if (eq) {
   2030 		if (op == POINT) {
   2031 			if (!allow_c90)
   2032 				/* left operand of '.' must be struct ... */
   2033 				warning(103, type_name(tn->tn_type));
   2034 			else
   2035 				/* left operand of '.' must be struct ... */
   2036 				error(103, type_name(tn->tn_type));
   2037 		} else {
   2038 			if (!allow_c90 && tn->tn_type->t_tspec == PTR)
   2039 				/* left operand of '->' must be pointer ... */
   2040 				warning(104, type_name(tn->tn_type));
   2041 			else
   2042 				/* left operand of '->' must be pointer ... */
   2043 				error(104, type_name(tn->tn_type));
   2044 		}
   2045 	} else {
   2046 		if (!allow_c90)
   2047 			/* non-unique member requires struct/union %s */
   2048 			error(105, op == POINT ? "object" : "pointer");
   2049 		else
   2050 			/* unacceptable operand of '%s' */
   2051 			error(111, op_name(op));
   2052 	}
   2053 
   2054 	return msym;
   2055 }
   2056 
   2057 tnode_t *
   2058 build_member_access(tnode_t *ln, op_t op, bool sys, sbuf_t *member)
   2059 {
   2060 	sym_t *msym;
   2061 
   2062 	if (ln == NULL)
   2063 		return NULL;
   2064 
   2065 	if (op == ARROW)
   2066 		/* must do this before struct_or_union_member is called */
   2067 		ln = cconv(ln);
   2068 	msym = struct_or_union_member(ln, op, getsym(member));
   2069 	return build_binary(ln, op, sys, build_name(msym, false));
   2070 }
   2071 
   2072 /*
   2073  * Perform class conversions.
   2074  *
   2075  * Arrays of type T are converted into pointers to type T.
   2076  * Functions are converted to pointers to functions.
   2077  * Lvalues are converted to rvalues.
   2078  *
   2079  * C99 6.3 "Conversions"
   2080  * C99 6.3.2 "Other operands"
   2081  * C99 6.3.2.1 "Lvalues, arrays, and function designators"
   2082  */
   2083 tnode_t *
   2084 cconv(tnode_t *tn)
   2085 {
   2086 	if (tn->tn_type->t_tspec == ARRAY) {
   2087 		if (!tn->tn_lvalue) {
   2088 			/* XXX print correct operator */
   2089 			/* %soperand of '%s' must be lvalue */
   2090 			gnuism(114, "", op_name(ADDR));
   2091 		}
   2092 		tn = build_op(ADDR, tn->tn_sys,
   2093 		    expr_derive_type(tn->tn_type->t_subt, PTR), tn, NULL);
   2094 	}
   2095 
   2096 	if (tn->tn_type->t_tspec == FUNC)
   2097 		tn = build_address(tn->tn_sys, tn, true);
   2098 
   2099 	if (tn->tn_lvalue) {
   2100 		type_t *tp = expr_dup_type(tn->tn_type);
   2101 		/* C99 6.3.2.1p2 sentence 2 says to remove the qualifiers. */
   2102 		tp->t_const = tp->t_volatile = false;
   2103 		tn = build_op(LOAD, tn->tn_sys, tp, tn, NULL);
   2104 	}
   2105 
   2106 	return tn;
   2107 }
   2108 
   2109 const tnode_t *
   2110 before_conversion(const tnode_t *tn)
   2111 {
   2112 	while (tn->tn_op == CVT && !tn->tn_cast)
   2113 		tn = tn->u.ops.left;
   2114 	return tn;
   2115 }
   2116 
   2117 /*
   2118  * Most errors required by C90 are reported in struct_or_union_member().
   2119  * Here we only check for totally wrong things.
   2120  */
   2121 static bool
   2122 typeok_point(const tnode_t *ln, const type_t *ltp, tspec_t lt)
   2123 {
   2124 	if (is_struct_or_union(lt))
   2125 		return true;
   2126 
   2127 	if (lt == FUNC || lt == VOID || ltp->t_bitfield)
   2128 		goto wrong;
   2129 
   2130 	/*
   2131 	 * Some C dialects from before C90 tolerated any lvalue on the
   2132 	 * left-hand side of the '.' operator, allowing things like 'char
   2133 	 * st[100]; st.st_mtime', assuming that the member 'st_mtime' only
   2134 	 * occurred in a single struct; see typeok_arrow.
   2135 	 */
   2136 	if (ln->tn_lvalue)
   2137 		return true;
   2138 
   2139 wrong:
   2140 	/* With allow_c90 we already got an error */
   2141 	if (!allow_c90)
   2142 		/* unacceptable operand of '%s' */
   2143 		error(111, op_name(POINT));
   2144 
   2145 	return false;
   2146 }
   2147 
   2148 static bool
   2149 typeok_arrow(tspec_t lt)
   2150 {
   2151 	/*
   2152 	 * C1978 Appendix A 14.1 says: <quote>In fact, any lvalue is allowed
   2153 	 * before '.', and that lvalue is then assumed to have the form of the
   2154 	 * structure of which the name of the right is a member. [...] Such
   2155 	 * constructions are non-portable.</quote>
   2156 	 */
   2157 	if (lt == PTR || (!allow_c90 && is_integer(lt)))
   2158 		return true;
   2159 
   2160 	/* With allow_c90 we already got an error */
   2161 	if (!allow_c90)
   2162 		/* unacceptable operand of '%s' */
   2163 		error(111, op_name(ARROW));
   2164 	return false;
   2165 }
   2166 
   2167 static bool
   2168 typeok_incdec(op_t op, const tnode_t *tn, const type_t *tp)
   2169 {
   2170 	/* operand has scalar type (checked in typeok) */
   2171 	if (!tn->tn_lvalue) {
   2172 		if (tn->tn_op == CVT && tn->tn_cast &&
   2173 		    tn->u.ops.left->tn_op == LOAD)
   2174 			/* a cast does not yield an lvalue */
   2175 			error(163);
   2176 		/* %soperand of '%s' must be lvalue */
   2177 		error(114, "", op_name(op));
   2178 		return false;
   2179 	}
   2180 	if (tp->t_const && allow_c90)
   2181 		/* %soperand of '%s' must be modifiable lvalue */
   2182 		warning(115, "", op_name(op));
   2183 	return true;
   2184 }
   2185 
   2186 static bool
   2187 typeok_address(op_t op, const tnode_t *tn, const type_t *tp, tspec_t t)
   2188 {
   2189 	if (t == ARRAY || t == FUNC) {
   2190 		/* ok, a warning comes later (in build_address()) */
   2191 	} else if (!tn->tn_lvalue) {
   2192 		if (tn->tn_op == CVT && tn->tn_cast &&
   2193 		    tn->u.ops.left->tn_op == LOAD)
   2194 			/* a cast does not yield an lvalue */
   2195 			error(163);
   2196 		/* %soperand of '%s' must be lvalue */
   2197 		error(114, "", op_name(op));
   2198 		return false;
   2199 	} else if (is_scalar(t)) {
   2200 		if (tp->t_bitfield) {
   2201 			/* cannot take address of bit-field */
   2202 			error(112);
   2203 			return false;
   2204 		}
   2205 	} else if (t != STRUCT && t != UNION) {
   2206 		/* unacceptable operand of '%s' */
   2207 		error(111, op_name(op));
   2208 		return false;
   2209 	}
   2210 	if (tn->tn_op == NAME && tn->u.sym->s_register) {
   2211 		/* cannot take address of register '%s' */
   2212 		error(113, tn->u.sym->s_name);
   2213 		return false;
   2214 	}
   2215 	return true;
   2216 }
   2217 
   2218 static bool
   2219 typeok_indir(const type_t *tp, tspec_t t)
   2220 {
   2221 
   2222 	if (t != PTR) {
   2223 		/* cannot dereference non-pointer type '%s' */
   2224 		error(96, type_name(tp));
   2225 		return false;
   2226 	}
   2227 	return true;
   2228 }
   2229 
   2230 static void
   2231 warn_incompatible_types(op_t op,
   2232 			const type_t *ltp, tspec_t lt,
   2233 			const type_t *rtp, tspec_t rt)
   2234 {
   2235 	bool binary = modtab[op].m_binary;
   2236 
   2237 	if (lt == VOID || (binary && rt == VOID)) {
   2238 		/* void type illegal in expression */
   2239 		error(109);
   2240 	} else if (op == ASSIGN)
   2241 		/* cannot assign to '%s' from '%s' */
   2242 		error(171, type_name(ltp), type_name(rtp));
   2243 	else if (binary)
   2244 		/* operands of '%s' have incompatible types '%s' and '%s' */
   2245 		error(107, op_name(op), type_name(ltp), type_name(rtp));
   2246 	else {
   2247 		lint_assert(rt == NO_TSPEC);
   2248 		/* operand of '%s' has invalid type '%s' */
   2249 		error(108, op_name(op), type_name(ltp));
   2250 	}
   2251 }
   2252 
   2253 static bool
   2254 typeok_plus(op_t op,
   2255 	    const type_t *ltp, tspec_t lt,
   2256 	    const type_t *rtp, tspec_t rt)
   2257 {
   2258 	/* operands have scalar types (checked in typeok) */
   2259 	if ((lt == PTR && !is_integer(rt)) || (rt == PTR && !is_integer(lt))) {
   2260 		warn_incompatible_types(op, ltp, lt, rtp, rt);
   2261 		return false;
   2262 	}
   2263 	return true;
   2264 }
   2265 
   2266 static bool
   2267 typeok_minus(op_t op,
   2268 	     const type_t *ltp, tspec_t lt,
   2269 	     const type_t *rtp, tspec_t rt)
   2270 {
   2271 	/* operands have scalar types (checked in typeok) */
   2272 	if ((lt == PTR && rt != PTR && !is_integer(rt)) ||
   2273 	    (lt != PTR && rt == PTR)) {
   2274 		warn_incompatible_types(op, ltp, lt, rtp, rt);
   2275 		return false;
   2276 	}
   2277 	if (lt == PTR && rt == PTR &&
   2278 	    !types_compatible(ltp->t_subt, rtp->t_subt, true, false, NULL)) {
   2279 		/* illegal pointer subtraction */
   2280 		error(116);
   2281 	}
   2282 	return true;
   2283 }
   2284 
   2285 static void
   2286 typeok_shr(op_t op,
   2287 	   const tnode_t *ln, tspec_t lt,
   2288 	   const tnode_t *rn, tspec_t rt)
   2289 {
   2290 	tspec_t olt = before_conversion(ln)->tn_type->t_tspec;
   2291 	tspec_t ort = before_conversion(rn)->tn_type->t_tspec;
   2292 
   2293 	/* operands have integer types (checked in typeok) */
   2294 	if (pflag && !is_uinteger(olt)) {
   2295 		integer_constraints lc = ic_expr(ln);
   2296 		if (!ic_maybe_signed(ln->tn_type, &lc))
   2297 			return;
   2298 
   2299 		if (ln->tn_op != CON)
   2300 			/* bitwise '%s' on signed value possibly nonportable */
   2301 			warning(117, op_name(op));
   2302 		else if (ln->u.value.u.integer < 0)
   2303 			/* bitwise '%s' on signed value nonportable */
   2304 			warning(120, op_name(op));
   2305 	} else if (allow_trad && allow_c90 &&
   2306 	    !is_uinteger(olt) && is_uinteger(ort)) {
   2307 		/* The left operand would become unsigned in traditional C. */
   2308 		if (hflag && (ln->tn_op != CON || ln->u.value.u.integer < 0)) {
   2309 			/* semantics of '%s' change in C90; use ... */
   2310 			warning(118, op_name(op));
   2311 		}
   2312 	} else if (allow_trad && allow_c90 &&
   2313 	    !is_uinteger(olt) && !is_uinteger(ort) &&
   2314 	    portable_rank_cmp(lt, rt) < 0) {
   2315 		/*
   2316 		 * In traditional C, the left operand would be extended
   2317 		 * (possibly sign-extended) and then shifted.
   2318 		 */
   2319 		if (hflag && (ln->tn_op != CON || ln->u.value.u.integer < 0)) {
   2320 			/* semantics of '%s' change in C90; use ... */
   2321 			warning(118, op_name(op));
   2322 		}
   2323 	}
   2324 }
   2325 
   2326 static void
   2327 typeok_shl(op_t op, tspec_t lt, tspec_t rt)
   2328 {
   2329 	/*
   2330 	 * C90 does not perform balancing for shift operations, but traditional
   2331 	 * C does. If the width of the right operand is greater than the width
   2332 	 * of the left operand, then in traditional C the left operand would be
   2333 	 * extended to the width of the right operand. For SHL this may result
   2334 	 * in different results.
   2335 	 */
   2336 	if (portable_rank_cmp(lt, rt) < 0) {
   2337 		/*
   2338 		 * XXX If both operands are constant, make sure that there is
   2339 		 * really a difference between C90 and traditional C.
   2340 		 */
   2341 		if (hflag && allow_trad && allow_c90)
   2342 			/* semantics of '%s' change in C90; use ... */
   2343 			warning(118, op_name(op));
   2344 	}
   2345 }
   2346 
   2347 static void
   2348 typeok_shift(const type_t *ltp, tspec_t lt, const tnode_t *rn, tspec_t rt)
   2349 {
   2350 	if (rn->tn_op != CON)
   2351 		return;
   2352 
   2353 	if (!is_uinteger(rt) && rn->u.value.u.integer < 0)
   2354 		/* negative shift */
   2355 		warning(121);
   2356 	else if ((uint64_t)rn->u.value.u.integer == size_in_bits(lt))
   2357 		/* shift amount %u equals bit-size of '%s' */
   2358 		warning(267, (unsigned)rn->u.value.u.integer, type_name(ltp));
   2359 	else if ((uint64_t)rn->u.value.u.integer > size_in_bits(lt)) {
   2360 		/* shift amount %llu is greater than bit-size %llu of '%s' */
   2361 		warning(122, (unsigned long long)rn->u.value.u.integer,
   2362 		    (unsigned long long)size_in_bits(lt),
   2363 		    tspec_name(lt));
   2364 	}
   2365 }
   2366 
   2367 static bool
   2368 is_typeok_eq(const tnode_t *ln, tspec_t lt, const tnode_t *rn, tspec_t rt)
   2369 {
   2370 	if (lt == PTR && is_null_pointer(rn))
   2371 		return true;
   2372 	if (rt == PTR && is_null_pointer(ln))
   2373 		return true;
   2374 	return false;
   2375 }
   2376 
   2377 static void
   2378 warn_incompatible_pointers(op_t op, const type_t *ltp, const type_t *rtp)
   2379 {
   2380 	lint_assert(ltp->t_tspec == PTR);
   2381 	lint_assert(rtp->t_tspec == PTR);
   2382 
   2383 	tspec_t lt = ltp->t_subt->t_tspec;
   2384 	tspec_t rt = rtp->t_subt->t_tspec;
   2385 
   2386 	if (is_struct_or_union(lt) && is_struct_or_union(rt)) {
   2387 		if (op == RETURN)
   2388 			/* illegal structure pointer combination */
   2389 			warning(244);
   2390 		else {
   2391 			/* incompatible structure pointers: '%s' '%s' '%s' */
   2392 			warning(245, type_name(ltp),
   2393 			    op_name(op), type_name(rtp));
   2394 		}
   2395 	} else {
   2396 		if (op == RETURN)
   2397 			/* illegal combination of '%s' and '%s' */
   2398 			warning(184, type_name(ltp), type_name(rtp));
   2399 		else {
   2400 			/* illegal combination of '%s' and '%s', op '%s' */
   2401 			warning(124,
   2402 			    type_name(ltp), type_name(rtp), op_name(op));
   2403 		}
   2404 	}
   2405 }
   2406 
   2407 static void
   2408 check_pointer_comparison(op_t op, const tnode_t *ln, const tnode_t *rn)
   2409 {
   2410 	type_t *ltp = ln->tn_type, *rtp = rn->tn_type;
   2411 	tspec_t lst = ltp->t_subt->t_tspec, rst = rtp->t_subt->t_tspec;
   2412 
   2413 	if (lst == VOID || rst == VOID) {
   2414 		/* TODO: C99 behaves like C90 here. */
   2415 		if (!allow_trad && !allow_c99 &&
   2416 		    (lst == FUNC || rst == FUNC)) {
   2417 			/* (void *)0 is already handled in typeok() */
   2418 			const char *lsts, *rsts;
   2419 			*(lst == FUNC ? &lsts : &rsts) = "function pointer";
   2420 			*(lst == VOID ? &lsts : &rsts) = "'void *'";
   2421 			/* C90 or later forbid comparison of %s with %s */
   2422 			warning(274, lsts, rsts);
   2423 		}
   2424 		return;
   2425 	}
   2426 
   2427 	if (!types_compatible(ltp->t_subt, rtp->t_subt, true, false, NULL)) {
   2428 		warn_incompatible_pointers(op, ltp, rtp);
   2429 		return;
   2430 	}
   2431 
   2432 	if (lst == FUNC && rst == FUNC) {
   2433 		/* TODO: C99 behaves like C90 here, see C99 6.5.8p2. */
   2434 		if (!allow_trad && !allow_c99 && op != EQ && op != NE)
   2435 			/* pointers to functions can only be compared ... */
   2436 			warning(125);
   2437 	}
   2438 }
   2439 
   2440 static bool
   2441 typeok_compare(op_t op,
   2442 	       const tnode_t *ln, const type_t *ltp, tspec_t lt,
   2443 	       const tnode_t *rn, const type_t *rtp, tspec_t rt)
   2444 {
   2445 	if (lt == PTR && rt == PTR) {
   2446 		check_pointer_comparison(op, ln, rn);
   2447 		return true;
   2448 	}
   2449 
   2450 	if (lt != PTR && rt != PTR)
   2451 		return true;
   2452 
   2453 	if (!is_integer(lt) && !is_integer(rt)) {
   2454 		warn_incompatible_types(op, ltp, lt, rtp, rt);
   2455 		return false;
   2456 	}
   2457 
   2458 	const char *lx = lt == PTR ? "pointer" : "integer";
   2459 	const char *rx = rt == PTR ? "pointer" : "integer";
   2460 	/* illegal combination of %s '%s' and %s '%s', op '%s' */
   2461 	warning(123, lx, type_name(ltp), rx, type_name(rtp), op_name(op));
   2462 	return true;
   2463 }
   2464 
   2465 static bool
   2466 typeok_quest(tspec_t lt, const tnode_t *rn)
   2467 {
   2468 	if (!is_scalar(lt)) {
   2469 		/* first operand of '?' must have scalar type */
   2470 		error(170);
   2471 		return false;
   2472 	}
   2473 	lint_assert(before_conversion(rn)->tn_op == COLON);
   2474 	return true;
   2475 }
   2476 
   2477 static void
   2478 typeok_colon_pointer(const type_t *ltp, const type_t *rtp)
   2479 {
   2480 	type_t *lstp = ltp->t_subt;
   2481 	type_t *rstp = rtp->t_subt;
   2482 	tspec_t lst = lstp->t_tspec;
   2483 	tspec_t rst = rstp->t_tspec;
   2484 
   2485 	if ((lst == VOID && rst == FUNC) || (lst == FUNC && rst == VOID)) {
   2486 		/* (void *)0 is handled in typeok_colon */
   2487 		/* TODO: C99 behaves like C90 here. */
   2488 		if (!allow_trad && !allow_c99)
   2489 			/* conversion of %s to %s requires a cast, op %s */
   2490 			warning(305, "function pointer", "'void *'",
   2491 			    op_name(COLON));
   2492 		return;
   2493 	}
   2494 
   2495 	if (pointer_types_are_compatible(lstp, rstp, true))
   2496 		return;
   2497 	if (!types_compatible(lstp, rstp, true, false, NULL))
   2498 		warn_incompatible_pointers(COLON, ltp, rtp);
   2499 }
   2500 
   2501 static bool
   2502 typeok_colon(const tnode_t *ln, const type_t *ltp, tspec_t lt,
   2503 	     const tnode_t *rn, const type_t *rtp, tspec_t rt)
   2504 {
   2505 
   2506 	if (is_arithmetic(lt) && is_arithmetic(rt))
   2507 		return true;
   2508 	if (lt == BOOL && rt == BOOL)
   2509 		return true;
   2510 
   2511 	if (lt == STRUCT && rt == STRUCT && ltp->u.sou == rtp->u.sou)
   2512 		return true;
   2513 	if (lt == UNION && rt == UNION && ltp->u.sou == rtp->u.sou)
   2514 		return true;
   2515 
   2516 	if (lt == PTR && is_null_pointer(rn))
   2517 		return true;
   2518 	if (rt == PTR && is_null_pointer(ln))
   2519 		return true;
   2520 
   2521 	if ((lt == PTR && is_integer(rt)) || (is_integer(lt) && rt == PTR)) {
   2522 		const char *lx = lt == PTR ? "pointer" : "integer";
   2523 		const char *rx = rt == PTR ? "pointer" : "integer";
   2524 		/* illegal combination of %s '%s' and %s '%s', op '%s' */
   2525 		warning(123, lx, type_name(ltp),
   2526 		    rx, type_name(rtp), op_name(COLON));
   2527 		return true;
   2528 	}
   2529 
   2530 	if (lt == VOID || rt == VOID) {
   2531 		if (lt != VOID || rt != VOID)
   2532 			/* incompatible types '%s' and '%s' in conditional */
   2533 			warning(126, type_name(ltp), type_name(rtp));
   2534 		return true;
   2535 	}
   2536 
   2537 	if (lt == PTR && rt == PTR) {
   2538 		typeok_colon_pointer(ltp, rtp);
   2539 		return true;
   2540 	}
   2541 
   2542 	/* incompatible types '%s' and '%s' in conditional */
   2543 	error(126, type_name(ltp), type_name(rtp));
   2544 	return false;
   2545 }
   2546 
   2547 static bool
   2548 has_constant_member(const type_t *tp)
   2549 {
   2550 	lint_assert(is_struct_or_union(tp->t_tspec));
   2551 
   2552 	for (sym_t *m = tp->u.sou->sou_first_member;
   2553 	    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 static bool
   2565 typeok_assign(op_t op, const tnode_t *ln, const type_t *ltp, tspec_t lt)
   2566 {
   2567 	if (op == RETURN || op == INIT || op == FARG)
   2568 		return true;
   2569 
   2570 	if (!ln->tn_lvalue) {
   2571 		if (ln->tn_op == CVT && ln->tn_cast &&
   2572 		    ln->u.ops.left->tn_op == LOAD)
   2573 			/* a cast does not yield an lvalue */
   2574 			error(163);
   2575 		/* %soperand of '%s' must be lvalue */
   2576 		error(114, "left ", op_name(op));
   2577 		return false;
   2578 	} else if (ltp->t_const
   2579 	    || (is_struct_or_union(lt) && has_constant_member(ltp))) {
   2580 		if (allow_c90)
   2581 			/* %soperand of '%s' must be modifiable lvalue */
   2582 			warning(115, "left ", op_name(op));
   2583 	}
   2584 	return true;
   2585 }
   2586 
   2587 static bool
   2588 typeok_scalar(op_t op, const mod_t *mp,
   2589 	      const type_t *ltp, tspec_t lt,
   2590 	      const type_t *rtp, tspec_t rt)
   2591 {
   2592 	if (mp->m_takes_bool && lt == BOOL && rt == BOOL)
   2593 		return true;
   2594 	if (mp->m_requires_integer) {
   2595 		if (!is_integer(lt) || (mp->m_binary && !is_integer(rt))) {
   2596 			warn_incompatible_types(op, ltp, lt, rtp, rt);
   2597 			return false;
   2598 		}
   2599 	} else if (mp->m_requires_integer_or_complex) {
   2600 		if ((!is_integer(lt) && !is_complex(lt)) ||
   2601 		    (mp->m_binary && (!is_integer(rt) && !is_complex(rt)))) {
   2602 			warn_incompatible_types(op, ltp, lt, rtp, rt);
   2603 			return false;
   2604 		}
   2605 	} else if (mp->m_requires_scalar) {
   2606 		if (!is_scalar(lt) || (mp->m_binary && !is_scalar(rt))) {
   2607 			warn_incompatible_types(op, ltp, lt, rtp, rt);
   2608 			return false;
   2609 		}
   2610 	} else if (mp->m_requires_arith) {
   2611 		if (!is_arithmetic(lt) ||
   2612 		    (mp->m_binary && !is_arithmetic(rt))) {
   2613 			warn_incompatible_types(op, ltp, lt, rtp, rt);
   2614 			return false;
   2615 		}
   2616 	}
   2617 	return true;
   2618 }
   2619 
   2620 static void
   2621 check_assign_void_pointer(op_t op, int arg,
   2622 			  tspec_t lt, tspec_t lst,
   2623 			  tspec_t rt, tspec_t rst)
   2624 {
   2625 
   2626 	if (!(lt == PTR && rt == PTR && (lst == VOID || rst == VOID)))
   2627 		return;
   2628 	/* two pointers, at least one pointer to void */
   2629 
   2630 	/* TODO: C99 behaves like C90 here. */
   2631 	if (!(!allow_trad && !allow_c99 && (lst == FUNC || rst == FUNC)))
   2632 		return;
   2633 	/* comb. of ptr to func and ptr to void */
   2634 
   2635 	const char *lts, *rts;
   2636 	*(lst == FUNC ? &lts : &rts) = "function pointer";
   2637 	*(lst == VOID ? &lts : &rts) = "'void *'";
   2638 
   2639 	switch (op) {
   2640 	case INIT:
   2641 	case RETURN:
   2642 		/* conversion of %s to %s requires a cast */
   2643 		warning(303, rts, lts);
   2644 		break;
   2645 	case FARG:
   2646 		/* conversion of %s to %s requires a cast, arg #%d */
   2647 		warning(304, rts, lts, arg);
   2648 		break;
   2649 	default:
   2650 		/* conversion of %s to %s requires a cast, op %s */
   2651 		warning(305, rts, lts, op_name(op));
   2652 		break;
   2653 	}
   2654 }
   2655 
   2656 static bool
   2657 is_direct_function_call(const tnode_t *tn, const char **out_name)
   2658 {
   2659 
   2660 	if (tn->tn_op == CALL
   2661 	    && tn->u.call->func->tn_op == ADDR
   2662 	    && tn->u.call->func->u.ops.left->tn_op == NAME) {
   2663 		*out_name = tn->u.call->func->u.ops.left->u.sym->s_name;
   2664 		return true;
   2665 	}
   2666 	return false;
   2667 }
   2668 
   2669 static bool
   2670 is_unconst_function(const char *name)
   2671 {
   2672 
   2673 	return strcmp(name, "memchr") == 0 ||
   2674 	    strcmp(name, "strchr") == 0 ||
   2675 	    strcmp(name, "strpbrk") == 0 ||
   2676 	    strcmp(name, "strrchr") == 0 ||
   2677 	    strcmp(name, "strstr") == 0;
   2678 }
   2679 
   2680 static bool
   2681 is_const_char_pointer(const tnode_t *tn)
   2682 {
   2683 	/*
   2684 	 * For traditional reasons, C99 6.4.5p5 defines that string literals
   2685 	 * have type 'char[]'.  They are often implicitly converted to 'char
   2686 	 * *', for example when they are passed as function arguments.
   2687 	 *
   2688 	 * C99 6.4.5p6 further defines that modifying a string that is
   2689 	 * constructed from a string literal invokes undefined behavior.
   2690 	 *
   2691 	 * Out of these reasons, string literals are treated as 'effectively
   2692 	 * const' here.
   2693 	 */
   2694 	if (tn->tn_op == CVT &&
   2695 	    tn->u.ops.left->tn_op == ADDR &&
   2696 	    tn->u.ops.left->u.ops.left->tn_op == STRING)
   2697 		return true;
   2698 
   2699 	const type_t *tp = before_conversion(tn)->tn_type;
   2700 	return tp->t_tspec == PTR &&
   2701 	    tp->t_subt->t_tspec == CHAR &&
   2702 	    tp->t_subt->t_const;
   2703 }
   2704 
   2705 static bool
   2706 is_const_pointer(const tnode_t *tn)
   2707 {
   2708 	const type_t *tp = before_conversion(tn)->tn_type;
   2709 	return tp->t_tspec == PTR && tp->t_subt->t_const;
   2710 }
   2711 
   2712 static void
   2713 check_unconst_function(const type_t *lstp, const tnode_t *rn)
   2714 {
   2715 	const char *function_name;
   2716 
   2717 	if (lstp->t_tspec == CHAR && !lstp->t_const &&
   2718 	    is_direct_function_call(rn, &function_name) &&
   2719 	    is_unconst_function(function_name) &&
   2720 	    rn->u.call->args_len >= 1 &&
   2721 	    is_const_char_pointer(rn->u.call->args[0])) {
   2722 		/* call to '%s' effectively discards 'const' from argument */
   2723 		warning(346, function_name);
   2724 	}
   2725 
   2726 	if (!lstp->t_const &&
   2727 	    is_direct_function_call(rn, &function_name) &&
   2728 	    strcmp(function_name, "bsearch") == 0 &&
   2729 	    rn->u.call->args_len >= 2 &&
   2730 	    is_const_pointer(rn->u.call->args[1])) {
   2731 		/* call to '%s' effectively discards 'const' from argument */
   2732 		warning(346, function_name);
   2733 	}
   2734 }
   2735 
   2736 static bool
   2737 check_assign_void_pointer_compat(op_t op, int arg,
   2738 				 const type_t *ltp, tspec_t lt,
   2739 				 const type_t *lstp, tspec_t lst,
   2740 				 const tnode_t *rn,
   2741 				 const type_t *rtp, tspec_t rt,
   2742 				 const type_t *rstp, tspec_t rst)
   2743 {
   2744 	if (!(lt == PTR && rt == PTR && (lst == VOID || rst == VOID ||
   2745 					 types_compatible(lstp, rstp,
   2746 					     true, false, NULL))))
   2747 		return false;
   2748 
   2749 	/* compatible pointer types (qualifiers ignored) */
   2750 	if (allow_c90 &&
   2751 	    ((!lstp->t_const && rstp->t_const) ||
   2752 	     (!lstp->t_volatile && rstp->t_volatile))) {
   2753 		/* left side has not all qualifiers of right */
   2754 		switch (op) {
   2755 		case INIT:
   2756 		case RETURN:
   2757 			/* incompatible pointer types to '%s' and '%s' */
   2758 			warning(182, type_name(lstp), type_name(rstp));
   2759 			break;
   2760 		case FARG:
   2761 			/* converting '%s' to incompatible '%s' ... */
   2762 			warning(153,
   2763 			    type_name(rtp), type_name(ltp), arg);
   2764 			break;
   2765 		default:
   2766 			/* operands of '%s' have incompatible pointer ... */
   2767 			warning(128, op_name(op),
   2768 			    type_name(lstp), type_name(rstp));
   2769 			break;
   2770 		}
   2771 	}
   2772 
   2773 	if (allow_c90)
   2774 		check_unconst_function(lstp, rn);
   2775 
   2776 	return true;
   2777 }
   2778 
   2779 static bool
   2780 check_assign_pointer_integer(op_t op, int arg,
   2781 			     const type_t *ltp, tspec_t lt,
   2782 			     const type_t *rtp, tspec_t rt)
   2783 {
   2784 
   2785 	if (!((lt == PTR && is_integer(rt)) || (is_integer(lt) && rt == PTR)))
   2786 		return false;
   2787 
   2788 	const char *lx = lt == PTR ? "pointer" : "integer";
   2789 	const char *rx = rt == PTR ? "pointer" : "integer";
   2790 
   2791 	switch (op) {
   2792 	case INIT:
   2793 	case RETURN:
   2794 		/* illegal combination of %s '%s' and %s '%s' */
   2795 		warning(183, lx, type_name(ltp), rx, type_name(rtp));
   2796 		break;
   2797 	case FARG:
   2798 		/* illegal combination of %s '%s' and %s '%s', arg #%d */
   2799 		warning(154,
   2800 		    lx, type_name(ltp), rx, type_name(rtp), arg);
   2801 		break;
   2802 	default:
   2803 		/* illegal combination of %s '%s' and %s '%s', op '%s' */
   2804 		warning(123,
   2805 		    lx, type_name(ltp), rx, type_name(rtp), op_name(op));
   2806 		break;
   2807 	}
   2808 	return true;
   2809 }
   2810 
   2811 static bool
   2812 check_assign_pointer(op_t op, int arg,
   2813 		     const type_t *ltp, tspec_t lt,
   2814 		     const type_t *rtp, tspec_t rt)
   2815 {
   2816 	if (!(lt == PTR && rt == PTR))
   2817 		return false;
   2818 
   2819 	if (op == FARG)
   2820 		/* converting '%s' to incompatible '%s' for ... */
   2821 		warning(153, type_name(rtp), type_name(ltp), arg);
   2822 	else
   2823 		warn_incompatible_pointers(op, ltp, rtp);
   2824 	return true;
   2825 }
   2826 
   2827 static void
   2828 warn_assign(op_t op, int arg,
   2829 	    const type_t *ltp, tspec_t lt,
   2830 	    const type_t *rtp, tspec_t rt)
   2831 {
   2832 	switch (op) {
   2833 	case INIT:
   2834 		/* cannot initialize '%s' from '%s' */
   2835 		error(185, type_name(ltp), type_name(rtp));
   2836 		break;
   2837 	case RETURN:
   2838 		/* function has return type '%s' but returns '%s' */
   2839 		error(211, type_name(ltp), type_name(rtp));
   2840 		break;
   2841 	case FARG:
   2842 		/* passing '%s' to incompatible '%s', arg #%d */
   2843 		warning(155, type_name(rtp), type_name(ltp), arg);
   2844 		break;
   2845 	default:
   2846 		warn_incompatible_types(op, ltp, lt, rtp, rt);
   2847 		break;
   2848 	}
   2849 }
   2850 
   2851 /*
   2852  * Checks type compatibility for ASSIGN, INIT, FARG and RETURN
   2853  * and prints warnings/errors if necessary.
   2854  * Returns whether the types are (almost) compatible.
   2855  */
   2856 static bool
   2857 check_assign_types_compatible(op_t op, int arg,
   2858 			      const tnode_t *ln, const tnode_t *rn)
   2859 {
   2860 	tspec_t lt, rt, lst = NO_TSPEC, rst = NO_TSPEC;
   2861 	type_t *ltp, *rtp, *lstp = NULL, *rstp = NULL;
   2862 
   2863 	if ((lt = (ltp = ln->tn_type)->t_tspec) == PTR)
   2864 		lst = (lstp = ltp->t_subt)->t_tspec;
   2865 	if ((rt = (rtp = rn->tn_type)->t_tspec) == PTR)
   2866 		rst = (rstp = rtp->t_subt)->t_tspec;
   2867 
   2868 	if (lt == BOOL && is_scalar(rt))	/* C99 6.3.1.2 */
   2869 		return true;
   2870 
   2871 	if (is_arithmetic(lt) && (is_arithmetic(rt) || rt == BOOL))
   2872 		return true;
   2873 
   2874 	if (is_struct_or_union(lt) && is_struct_or_union(rt))
   2875 		return ltp->u.sou == rtp->u.sou;
   2876 
   2877 	if (lt == PTR && is_null_pointer(rn)) {
   2878 		if (is_integer(rn->tn_type->t_tspec))
   2879 			/* implicit conversion from integer 0 to pointer ... */
   2880 			query_message(15, type_name(ltp));
   2881 		return true;
   2882 	}
   2883 
   2884 	check_assign_void_pointer(op, arg, lt, lst, rt, rst);
   2885 
   2886 	if (check_assign_void_pointer_compat(op, arg,
   2887 	    ltp, lt, lstp, lst, rn, rtp, rt, rstp, rst))
   2888 		return true;
   2889 
   2890 	if (check_assign_pointer_integer(op, arg, ltp, lt, rtp, rt))
   2891 		return true;
   2892 
   2893 	if (check_assign_pointer(op, arg, ltp, lt, rtp, rt))
   2894 		return true;
   2895 
   2896 	warn_assign(op, arg, ltp, lt, rtp, rt);
   2897 	return false;
   2898 }
   2899 
   2900 static bool
   2901 has_side_effect(const tnode_t *tn) /* NOLINT(misc-no-recursion) */
   2902 {
   2903 	op_t op = tn->tn_op;
   2904 
   2905 	if (modtab[op].m_has_side_effect)
   2906 		return true;
   2907 
   2908 	if (op == CVT && tn->tn_type->t_tspec == VOID)
   2909 		return has_side_effect(tn->u.ops.left);
   2910 
   2911 	/* XXX: Why not has_side_effect(tn->u.ops.left) as well? */
   2912 	if (op == LOGAND || op == LOGOR)
   2913 		return has_side_effect(tn->u.ops.right);
   2914 
   2915 	/* XXX: Why not has_side_effect(tn->u.ops.left) as well? */
   2916 	if (op == QUEST)
   2917 		return has_side_effect(tn->u.ops.right);
   2918 
   2919 	if (op == COLON || op == COMMA) {
   2920 		return has_side_effect(tn->u.ops.left) ||
   2921 		    has_side_effect(tn->u.ops.right);
   2922 	}
   2923 
   2924 	return false;
   2925 }
   2926 
   2927 static bool
   2928 is_void_cast(const tnode_t *tn)
   2929 {
   2930 
   2931 	return tn->tn_op == CVT && tn->tn_cast &&
   2932 	    tn->tn_type->t_tspec == VOID;
   2933 }
   2934 
   2935 static bool
   2936 is_local_symbol(const tnode_t *tn)
   2937 {
   2938 
   2939 	return tn->tn_op == LOAD &&
   2940 	    tn->u.ops.left->tn_op == NAME &&
   2941 	    tn->u.ops.left->u.sym->s_scl == AUTO;
   2942 }
   2943 
   2944 static bool
   2945 is_int_constant_zero(const tnode_t *tn)
   2946 {
   2947 
   2948 	return tn->tn_op == CON &&
   2949 	    tn->tn_type->t_tspec == INT &&
   2950 	    tn->u.value.u.integer == 0;
   2951 }
   2952 
   2953 static void
   2954 check_null_effect(const tnode_t *tn)
   2955 {
   2956 
   2957 	if (hflag &&
   2958 	    !has_side_effect(tn) &&
   2959 	    !(is_void_cast(tn) && is_local_symbol(tn->u.ops.left)) &&
   2960 	    !(is_void_cast(tn) && is_int_constant_zero(tn->u.ops.left))) {
   2961 		/* expression has null effect */
   2962 		warning(129);
   2963 	}
   2964 }
   2965 
   2966 /*
   2967  * Check the types for specific operators and type combinations.
   2968  *
   2969  * At this point, the operands already conform to the type requirements of
   2970  * the operator, such as being integer, floating or scalar.
   2971  */
   2972 static bool
   2973 typeok_op(op_t op, int arg,
   2974 	  const tnode_t *ln, const type_t *ltp, tspec_t lt,
   2975 	  const tnode_t *rn, const type_t *rtp, tspec_t rt)
   2976 {
   2977 	switch (op) {
   2978 	case ARROW:
   2979 		return typeok_arrow(lt);
   2980 	case POINT:
   2981 		return typeok_point(ln, ltp, lt);
   2982 	case INCBEF:
   2983 	case DECBEF:
   2984 	case INCAFT:
   2985 	case DECAFT:
   2986 		return typeok_incdec(op, ln, ltp);
   2987 	case INDIR:
   2988 		return typeok_indir(ltp, lt);
   2989 	case ADDR:
   2990 		return typeok_address(op, ln, ltp, lt);
   2991 	case PLUS:
   2992 		return typeok_plus(op, ltp, lt, rtp, rt);
   2993 	case MINUS:
   2994 		return typeok_minus(op, ltp, lt, rtp, rt);
   2995 	case SHL:
   2996 		typeok_shl(op, lt, rt);
   2997 		goto shift;
   2998 	case SHR:
   2999 		typeok_shr(op, ln, lt, rn, rt);
   3000 	shift:
   3001 		typeok_shift(ltp, lt, rn, rt);
   3002 		break;
   3003 	case LT:
   3004 	case LE:
   3005 	case GT:
   3006 	case GE:
   3007 	compare:
   3008 		return typeok_compare(op, ln, ltp, lt, rn, rtp, rt);
   3009 	case EQ:
   3010 	case NE:
   3011 		if (is_typeok_eq(ln, lt, rn, rt))
   3012 			break;
   3013 		goto compare;
   3014 	case QUEST:
   3015 		return typeok_quest(lt, rn);
   3016 	case COLON:
   3017 		return typeok_colon(ln, ltp, lt, rn, rtp, rt);
   3018 	case ASSIGN:
   3019 	case INIT:
   3020 	case FARG:
   3021 	case RETURN:
   3022 		if (!check_assign_types_compatible(op, arg, ln, rn))
   3023 			return false;
   3024 		goto assign;
   3025 	case MULASS:
   3026 	case DIVASS:
   3027 	case MODASS:
   3028 		goto assign;
   3029 	case ADDASS:
   3030 	case SUBASS:
   3031 		if ((lt == PTR && !is_integer(rt)) || rt == PTR) {
   3032 			warn_incompatible_types(op, ltp, lt, rtp, rt);
   3033 			return false;
   3034 		}
   3035 		goto assign;
   3036 	case SHLASS:
   3037 		goto assign;
   3038 	case SHRASS:
   3039 		if (pflag && !is_uinteger(lt) &&
   3040 		    !(!allow_c90 && is_uinteger(rt))) {
   3041 			/* bitwise '%s' on signed value possibly nonportable */
   3042 			warning(117, op_name(op));
   3043 		}
   3044 		goto assign;
   3045 	case ANDASS:
   3046 	case XORASS:
   3047 	case ORASS:
   3048 	assign:
   3049 		return typeok_assign(op, ln, ltp, lt);
   3050 	case COMMA:
   3051 		if (!modtab[ln->tn_op].m_has_side_effect)
   3052 			check_null_effect(ln);
   3053 		break;
   3054 	default:
   3055 		break;
   3056 	}
   3057 	return true;
   3058 }
   3059 
   3060 static void
   3061 check_bad_enum_operation(op_t op, const tnode_t *ln, const tnode_t *rn)
   3062 {
   3063 
   3064 	if (!eflag)
   3065 		return;
   3066 
   3067 	/* Allow enum in array indices. */
   3068 	if (op == PLUS &&
   3069 	    ((ln->tn_type->t_is_enum && rn->tn_type->t_tspec == PTR) ||
   3070 	     (rn->tn_type->t_is_enum && ln->tn_type->t_tspec == PTR))) {
   3071 		return;
   3072 	}
   3073 
   3074 	/* dubious operation '%s' on enum */
   3075 	warning(241, op_name(op));
   3076 }
   3077 
   3078 static void
   3079 check_enum_type_mismatch(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
   3080 {
   3081 	const mod_t *mp = &modtab[op];
   3082 
   3083 	if (ln->tn_type->u.enumer != rn->tn_type->u.enumer) {
   3084 		switch (op) {
   3085 		case INIT:
   3086 			/* enum type mismatch between '%s' and '%s' in ... */
   3087 			warning(210,
   3088 			    type_name(ln->tn_type), type_name(rn->tn_type));
   3089 			break;
   3090 		case FARG:
   3091 			/* function expects '%s', passing '%s' for arg #%d */
   3092 			warning(156,
   3093 			    type_name(ln->tn_type), type_name(rn->tn_type),
   3094 			    arg);
   3095 			break;
   3096 		case RETURN:
   3097 			/* function has return type '%s' but returns '%s' */
   3098 			warning(211,
   3099 			    type_name(ln->tn_type), type_name(rn->tn_type));
   3100 			break;
   3101 		default:
   3102 			/* enum type mismatch: '%s' '%s' '%s' */
   3103 			warning(130, type_name(ln->tn_type), op_name(op),
   3104 			    type_name(rn->tn_type));
   3105 			break;
   3106 		}
   3107 	} else if (Pflag && eflag && mp->m_comparison && op != EQ && op != NE)
   3108 		/* operator '%s' assumes that '%s' is ordered */
   3109 		warning(243, op_name(op), type_name(ln->tn_type));
   3110 }
   3111 
   3112 static void
   3113 check_enum_int_mismatch(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
   3114 {
   3115 
   3116 	if (!eflag)
   3117 		return;
   3118 
   3119 	switch (op) {
   3120 	case INIT:
   3121 		/*
   3122 		 * Initialization with 0 is allowed. Otherwise, all implicit
   3123 		 * initializations would need to be warned upon as well.
   3124 		 */
   3125 		if (!rn->tn_type->t_is_enum && rn->tn_op == CON &&
   3126 		    is_integer(rn->tn_type->t_tspec) &&
   3127 		    rn->u.value.u.integer == 0) {
   3128 			return;
   3129 		}
   3130 		/* initialization of '%s' with '%s' */
   3131 		warning(277, type_name(ln->tn_type), type_name(rn->tn_type));
   3132 		break;
   3133 	case FARG:
   3134 		/* combination of '%s' and '%s', arg #%d */
   3135 		warning(278,
   3136 		    type_name(ln->tn_type), type_name(rn->tn_type), arg);
   3137 		break;
   3138 	case RETURN:
   3139 		/* combination of '%s' and '%s' in return */
   3140 		warning(279, type_name(ln->tn_type), type_name(rn->tn_type));
   3141 		break;
   3142 	default:
   3143 		/* combination of '%s' and '%s', op '%s' */
   3144 		warning(242, type_name(ln->tn_type), type_name(rn->tn_type),
   3145 		    op_name(op));
   3146 		break;
   3147 	}
   3148 }
   3149 
   3150 static void
   3151 typeok_enum(op_t op, const mod_t *mp, int arg,
   3152 	    const tnode_t *ln, const type_t *ltp,
   3153 	    const tnode_t *rn, const type_t *rtp)
   3154 {
   3155 	if (mp->m_bad_on_enum &&
   3156 	    (ltp->t_is_enum || (mp->m_binary && rtp->t_is_enum))) {
   3157 		check_bad_enum_operation(op, ln, rn);
   3158 	} else if (mp->m_valid_on_enum &&
   3159 	    (ltp->t_is_enum && rtp != NULL && rtp->t_is_enum)) {
   3160 		check_enum_type_mismatch(op, arg, ln, rn);
   3161 	} else if (mp->m_valid_on_enum &&
   3162 	    (ltp->t_is_enum || (rtp != NULL && rtp->t_is_enum))) {
   3163 		check_enum_int_mismatch(op, arg, ln, rn);
   3164 	}
   3165 }
   3166 
   3167 /* Perform most type checks. Return whether the types are ok. */
   3168 bool
   3169 typeok(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
   3170 {
   3171 
   3172 	const mod_t *mp = &modtab[op];
   3173 
   3174 	type_t *ltp = ln->tn_type;
   3175 	tspec_t lt = ltp->t_tspec;
   3176 
   3177 	type_t *rtp = mp->m_binary ? rn->tn_type : NULL;
   3178 	tspec_t rt = mp->m_binary ? rtp->t_tspec : NO_TSPEC;
   3179 
   3180 	if (Tflag && !typeok_scalar_strict_bool(op, mp, arg, ln, rn))
   3181 		return false;
   3182 	if (!typeok_scalar(op, mp, ltp, lt, rtp, rt))
   3183 		return false;
   3184 
   3185 	if (!typeok_op(op, arg, ln, ltp, lt, rn, rtp, rt))
   3186 		return false;
   3187 
   3188 	typeok_enum(op, mp, arg, ln, ltp, rn, rtp);
   3189 	return true;
   3190 }
   3191 
   3192 /* In traditional C, keep unsigned and promote FLOAT to DOUBLE. */
   3193 static tspec_t
   3194 promote_trad(tspec_t t)
   3195 {
   3196 
   3197 	if (t == UCHAR || t == USHORT)
   3198 		return UINT;
   3199 	if (t == CHAR || t == SCHAR || t == SHORT)
   3200 		return INT;
   3201 	if (t == FLOAT)
   3202 		return DOUBLE;
   3203 	if (t == ENUM)
   3204 		return INT;
   3205 	return t;
   3206 }
   3207 
   3208 /*
   3209  * C99 6.3.1.1p2 requires for types with lower rank than int that "If an int
   3210  * can represent all the values of the original type, the value is converted
   3211  * to an int; otherwise it is converted to an unsigned int", and that "All
   3212  * other types are unchanged by the integer promotions".
   3213  */
   3214 static tspec_t
   3215 promote_c90(const tnode_t *tn, tspec_t t, bool farg)
   3216 {
   3217 	if (tn->tn_type->t_bitfield) {
   3218 		unsigned int width = tn->tn_type->t_bit_field_width;
   3219 		unsigned int int_width = size_in_bits(INT);
   3220 		// XXX: What about _Bool bit-fields, since C99?
   3221 		if (width < int_width)
   3222 			return INT;
   3223 		if (width == int_width)
   3224 			return is_uinteger(t) ? UINT : INT;
   3225 		return t;
   3226 	}
   3227 
   3228 	if (t == CHAR || t == SCHAR)
   3229 		return INT;
   3230 	if (t == UCHAR)
   3231 		return size_in_bits(CHAR) < size_in_bits(INT) ? INT : UINT;
   3232 	if (t == SHORT)
   3233 		return INT;
   3234 	if (t == USHORT)
   3235 		return size_in_bits(SHORT) < size_in_bits(INT) ? INT : UINT;
   3236 	if (t == ENUM)
   3237 		return INT;
   3238 	if (farg && t == FLOAT)
   3239 		return DOUBLE;
   3240 	return t;
   3241 }
   3242 
   3243 /*
   3244  * Performs the "integer promotions" (C99 6.3.1.1p2), which convert small
   3245  * integer types to either int or unsigned int.
   3246  *
   3247  * If allow_c90 is unset or the operand is a function argument with no type
   3248  * information (no prototype or variable # of args), converts float to double.
   3249  */
   3250 tnode_t *
   3251 promote(op_t op, bool farg, tnode_t *tn)
   3252 {
   3253 
   3254 	const type_t *otp = tn->tn_type;
   3255 	tspec_t ot = otp->t_tspec;
   3256 	if (!is_arithmetic(ot))
   3257 		return tn;
   3258 
   3259 	tspec_t nt = allow_c90 ? promote_c90(tn, ot, farg) : promote_trad(ot);
   3260 	if (nt == ot)
   3261 		return tn;
   3262 
   3263 	type_t *ntp = expr_dup_type(gettyp(nt));
   3264 	ntp->t_tspec = nt;
   3265 	ntp->t_is_enum = otp->t_is_enum;
   3266 	if (ntp->t_is_enum)
   3267 		ntp->u.enumer = otp->u.enumer;
   3268 	ntp->t_bitfield = otp->t_bitfield;
   3269 	if (ntp->t_bitfield) {
   3270 		ntp->t_bit_field_width = otp->t_bit_field_width;
   3271 		ntp->t_bit_field_offset = otp->t_bit_field_offset;
   3272 	}
   3273 	if (ntp->t_bitfield && is_uinteger(ot) && !is_uinteger(nt))
   3274 		ntp->t_bit_field_width++;
   3275 	return convert(op, 0, ntp, tn);
   3276 }
   3277 
   3278 static void
   3279 convert_integer_from_floating(op_t op, const type_t *tp, const tnode_t *tn)
   3280 {
   3281 
   3282 	if (op == CVT)
   3283 		/* cast from floating point '%s' to integer '%s' */
   3284 		query_message(2, type_name(tn->tn_type), type_name(tp));
   3285 	else
   3286 		/* implicit conversion from floating point '%s' to ... */
   3287 		query_message(1, type_name(tn->tn_type), type_name(tp));
   3288 }
   3289 
   3290 static bool
   3291 should_warn_about_prototype_conversion(tspec_t nt,
   3292 				       tspec_t ot, const tnode_t *ptn)
   3293 {
   3294 
   3295 	if (nt == ot)
   3296 		return false;
   3297 
   3298 	if (nt == ENUM && ot == INT)
   3299 		return false;
   3300 
   3301 	if (is_floating(nt) != is_floating(ot) ||
   3302 	    portable_rank_cmp(nt, ot) != 0) {
   3303 		/* representation and/or width change */
   3304 		if (!is_integer(ot))
   3305 			return true;
   3306 		/*
   3307 		 * XXX: Investigate whether this rule makes sense; see
   3308 		 * tests/usr.bin/xlint/lint1/platform_long.c.
   3309 		 */
   3310 		return portable_rank_cmp(ot, INT) > 0;
   3311 	}
   3312 
   3313 	if (!hflag)
   3314 		return false;
   3315 
   3316 	/*
   3317 	 * If the types differ only in sign and the argument has the same
   3318 	 * representation in both types, print no warning.
   3319 	 */
   3320 	if (ptn->tn_op == CON && is_integer(nt) &&
   3321 	    signed_type(nt) == signed_type(ot) &&
   3322 	    !msb(ptn->u.value.u.integer, ot))
   3323 		return false;
   3324 
   3325 	return true;
   3326 }
   3327 
   3328 /*
   3329  * Warn if a prototype causes a type conversion that is different from what
   3330  * would happen to the same argument in the absence of a prototype.  This
   3331  * check is intended for code that needs to stay compatible with pre-C90 C.
   3332  *
   3333  * Errors/warnings about illegal type combinations are already printed
   3334  * in check_assign_types_compatible().
   3335  */
   3336 static void
   3337 check_prototype_conversion(int arg, tspec_t nt, tspec_t ot, type_t *tp,
   3338 			   tnode_t *tn)
   3339 {
   3340 
   3341 	if (!is_arithmetic(nt) || !is_arithmetic(ot))
   3342 		return;
   3343 
   3344 	/*
   3345 	 * If the type of the formal parameter is char/short, a warning would
   3346 	 * be useless, because functions declared the old style can't expect
   3347 	 * char/short arguments.
   3348 	 */
   3349 	if (nt == CHAR || nt == SCHAR || nt == UCHAR ||
   3350 	    nt == SHORT || nt == USHORT)
   3351 		return;
   3352 
   3353 	tnode_t *ptn = promote(NOOP, true, tn);
   3354 	ot = ptn->tn_type->t_tspec;
   3355 
   3356 	if (should_warn_about_prototype_conversion(nt, ot, ptn)) {
   3357 		/* argument %d is converted from '%s' to '%s' ... */
   3358 		warning(259, arg, type_name(tn->tn_type), type_name(tp));
   3359 	}
   3360 }
   3361 
   3362 /*
   3363  * When converting a large integer type to a small integer type, in some
   3364  * cases the value of the actual expression is further restricted than the
   3365  * type bounds, such as in (expr & 0xFF) or (expr % 100) or (expr >> 24).
   3366  */
   3367 static bool
   3368 can_represent(const type_t *tp, const tnode_t *tn)
   3369 {
   3370 
   3371 	debug_step("%s: type '%s'", __func__, type_name(tp));
   3372 	debug_node(tn);
   3373 
   3374 	uint64_t nmask = value_bits(width_in_bits(tp));
   3375 	if (!is_uinteger(tp->t_tspec))
   3376 		nmask >>= 1;
   3377 
   3378 	integer_constraints c = ic_expr(tn);
   3379 	if ((~c.bclr & ~nmask) == 0)
   3380 		return true;
   3381 
   3382 	integer_constraints tpc = ic_any(tp);
   3383 	if (is_uinteger(tp->t_tspec)
   3384 	    ? tpc.umin <= c.umin && tpc.umax >= c.umax
   3385 	    : tpc.smin <= c.smin && tpc.smax >= c.smax)
   3386 		return true;
   3387 
   3388 	return false;
   3389 }
   3390 
   3391 static bool
   3392 should_warn_about_integer_conversion(const type_t *ntp, tspec_t nt,
   3393 				     const tnode_t *otn, tspec_t ot)
   3394 {
   3395 
   3396 	// XXX: The portable_rank_cmp aims at portable mode, independent of the
   3397 	// current platform, while can_represent acts on the actual type sizes
   3398 	// from the current platform.  This mix is inconsistent, but anything
   3399 	// else would make the exact conditions too complicated to grasp.
   3400 	if (aflag > 0 && portable_rank_cmp(nt, ot) < 0) {
   3401 		if (ot == LONG || ot == ULONG
   3402 		    || ot == LLONG || ot == ULLONG
   3403 #ifdef INT128_SIZE
   3404 		    || ot == INT128 || ot == UINT128
   3405 #endif
   3406 		    || aflag > 1)
   3407 			return !can_represent(ntp, otn);
   3408 	}
   3409 	return false;
   3410 }
   3411 
   3412 static void
   3413 convert_integer_from_integer(op_t op, int arg, tspec_t nt, tspec_t ot,
   3414 			     type_t *tp, tnode_t *tn)
   3415 {
   3416 
   3417 	if (tn->tn_op == CON)
   3418 		return;
   3419 
   3420 	if (op == CVT)
   3421 		return;
   3422 
   3423 	if (Pflag && pflag && aflag > 0 &&
   3424 	    portable_rank_cmp(nt, ot) > 0 &&
   3425 	    is_uinteger(nt) != is_uinteger(ot)) {
   3426 		if (op == FARG)
   3427 			/* conversion to '%s' may sign-extend ... */
   3428 			warning(297, type_name(tp), arg);
   3429 		else
   3430 			/* conversion to '%s' may sign-extend ... */
   3431 			warning(131, type_name(tp));
   3432 	}
   3433 
   3434 	if (Pflag && portable_rank_cmp(nt, ot) > 0 &&
   3435 	    (tn->tn_op == PLUS || tn->tn_op == MINUS || tn->tn_op == MULT ||
   3436 	     tn->tn_op == SHL)) {
   3437 		/* suggest cast from '%s' to '%s' on op '%s' to ... */
   3438 		warning(324, type_name(gettyp(ot)), type_name(tp),
   3439 		    op_name(tn->tn_op));
   3440 	}
   3441 
   3442 	if (should_warn_about_integer_conversion(tp, nt, tn, ot)) {
   3443 		if (op == FARG) {
   3444 			/* conversion from '%s' to '%s' may lose ... */
   3445 			warning(298,
   3446 			    type_name(tn->tn_type), type_name(tp), arg);
   3447 		} else {
   3448 			/* conversion from '%s' to '%s' may lose accuracy */
   3449 			warning(132,
   3450 			    type_name(tn->tn_type), type_name(tp));
   3451 		}
   3452 	}
   3453 
   3454 	if (any_query_enabled && is_uinteger(nt) != is_uinteger(ot))
   3455 		/* implicit conversion changes sign from '%s' to '%s' */
   3456 		query_message(3, type_name(tn->tn_type), type_name(tp));
   3457 }
   3458 
   3459 static void
   3460 convert_integer_from_pointer(op_t op, tspec_t nt, type_t *tp, tnode_t *tn)
   3461 {
   3462 
   3463 	if (tn->tn_op == CON)
   3464 		return;
   3465 	if (op != CVT)
   3466 		return;		/* We already got an error. */
   3467 	if (portable_rank_cmp(nt, PTR) >= 0)
   3468 		return;
   3469 
   3470 	if (pflag && size_in_bits(nt) >= size_in_bits(PTR)) {
   3471 		/* conversion of pointer to '%s' may lose bits */
   3472 		warning(134, type_name(tp));
   3473 	} else {
   3474 		/* conversion of pointer to '%s' loses bits */
   3475 		warning(133, type_name(tp));
   3476 	}
   3477 }
   3478 
   3479 static bool
   3480 struct_starts_with(const type_t *struct_tp, const type_t *member_tp)
   3481 {
   3482 
   3483 	return struct_tp->u.sou->sou_first_member != NULL &&
   3484 	    types_compatible(struct_tp->u.sou->sou_first_member->s_type,
   3485 		member_tp, true, false, NULL);
   3486 }
   3487 
   3488 static bool
   3489 is_byte_array(const type_t *tp)
   3490 {
   3491 
   3492 	return tp->t_tspec == ARRAY &&
   3493 	    (tp->t_subt->t_tspec == CHAR || tp->t_subt->t_tspec == UCHAR);
   3494 }
   3495 
   3496 static bool
   3497 union_contains(const type_t *utp, const type_t *mtp)
   3498 {
   3499 	for (const sym_t *mem = utp->u.sou->sou_first_member;
   3500 	    mem != NULL; mem = mem->s_next) {
   3501 		if (types_compatible(mem->s_type, mtp, true, false, NULL))
   3502 			return true;
   3503 	}
   3504 	return false;
   3505 }
   3506 
   3507 static bool
   3508 should_warn_about_pointer_cast(const type_t *nstp, tspec_t nst,
   3509 			       const type_t *ostp, tspec_t ost)
   3510 {
   3511 
   3512 	while (nst == ARRAY)
   3513 		nstp = nstp->t_subt, nst = nstp->t_tspec;
   3514 	while (ost == ARRAY)
   3515 		ostp = ostp->t_subt, ost = ostp->t_tspec;
   3516 
   3517 	if (nst == STRUCT && ost == STRUCT &&
   3518 	    (struct_starts_with(nstp, ostp) ||
   3519 	     struct_starts_with(ostp, nstp)))
   3520 		return false;
   3521 
   3522 	if (is_incomplete(nstp) || is_incomplete(ostp))
   3523 		return false;
   3524 
   3525 	if (nst == CHAR || nst == UCHAR)
   3526 		return false;	/* for the sake of traditional C code */
   3527 	if (ost == CHAR || ost == UCHAR)
   3528 		return false;	/* for the sake of traditional C code */
   3529 
   3530 	/* Allow cast between pointers to sockaddr variants. */
   3531 	if (nst == STRUCT && ost == STRUCT) {
   3532 		const sym_t *nmem = nstp->u.sou->sou_first_member;
   3533 		const sym_t *omem = ostp->u.sou->sou_first_member;
   3534 		while (nmem != NULL && omem != NULL &&
   3535 		    types_compatible(nmem->s_type, omem->s_type,
   3536 			true, false, NULL))
   3537 			nmem = nmem->s_next, omem = omem->s_next;
   3538 		if (nmem != NULL && is_byte_array(nmem->s_type))
   3539 			return false;
   3540 		if (omem != NULL && is_byte_array(omem->s_type))
   3541 			return false;
   3542 		if (nmem == NULL && omem == NULL)
   3543 			return false;
   3544 	}
   3545 
   3546 	if (nst == UNION || ost == UNION) {
   3547 		const type_t *union_tp = nst == UNION ? nstp : ostp;
   3548 		const type_t *other_tp = nst == UNION ? ostp : nstp;
   3549 		if (union_contains(union_tp, other_tp))
   3550 			return false;
   3551 	}
   3552 
   3553 	if (is_struct_or_union(nst) && is_struct_or_union(ost))
   3554 		return nstp->u.sou != ostp->u.sou;
   3555 
   3556 	enum rank_kind rk1 = type_properties(nst)->tt_rank_kind;
   3557 	enum rank_kind rk2 = type_properties(ost)->tt_rank_kind;
   3558 	if (rk1 != rk2 || rk1 == RK_NONE)
   3559 		return true;
   3560 
   3561 	return portable_rank_cmp(nst, ost) != 0;
   3562 }
   3563 
   3564 static void
   3565 convert_pointer_from_pointer(type_t *ntp, tnode_t *tn)
   3566 {
   3567 	const type_t *nstp = ntp->t_subt;
   3568 	const type_t *otp = tn->tn_type;
   3569 	const type_t *ostp = otp->t_subt;
   3570 	tspec_t nst = nstp->t_tspec;
   3571 	tspec_t ost = ostp->t_tspec;
   3572 
   3573 	if (nst == VOID || ost == VOID) {
   3574 		/* TODO: C99 behaves like C90 here. */
   3575 		if (!allow_trad && !allow_c99 && (nst == FUNC || ost == FUNC)) {
   3576 			const char *nts, *ots;
   3577 			/* null pointers are already handled in convert() */
   3578 			*(nst == FUNC ? &nts : &ots) = "function pointer";
   3579 			*(nst == VOID ? &nts : &ots) = "'void *'";
   3580 			/* conversion of %s to %s requires a cast */
   3581 			warning(303, ots, nts);
   3582 		}
   3583 		return;
   3584 	}
   3585 	if (nst == FUNC && ost == FUNC)
   3586 		return;
   3587 	if (nst == FUNC || ost == FUNC) {
   3588 		/* converting '%s' to '%s' is questionable */
   3589 		warning(229, type_name(otp), type_name(ntp));
   3590 		return;
   3591 	}
   3592 
   3593 	if (hflag && alignment_in_bits(nstp) > alignment_in_bits(ostp) &&
   3594 	    ost != CHAR && ost != UCHAR &&
   3595 	    !is_incomplete(ostp) &&
   3596 	    !(nst == UNION && union_contains(nstp, ostp))) {
   3597 		/* converting '%s' to '%s' increases alignment ... */
   3598 		warning(135, type_name(otp), type_name(ntp),
   3599 		    alignment_in_bits(ostp) / CHAR_SIZE,
   3600 		    alignment_in_bits(nstp) / CHAR_SIZE);
   3601 	}
   3602 
   3603 	if (cflag && should_warn_about_pointer_cast(nstp, nst, ostp, ost)) {
   3604 		/* pointer cast from '%s' to '%s' may be troublesome */
   3605 		warning(247, type_name(otp), type_name(ntp));
   3606 	}
   3607 }
   3608 
   3609 /*
   3610  * Insert a conversion operator, which converts the type of the node
   3611  * to another given type.
   3612  *
   3613  * Possible values for 'op':
   3614  *	CVT	a cast-expression
   3615  *	binary	integer promotion for one of the operands, or a usual
   3616  *		arithmetic conversion
   3617  *	binary	plain or compound assignments to bit-fields
   3618  *	FARG	'arg' is the number of the parameter (used for warnings)
   3619  *	NOOP	several other implicit conversions
   3620  *	...
   3621  */
   3622 tnode_t *
   3623 convert(op_t op, int arg, type_t *tp, tnode_t *tn)
   3624 {
   3625 	tspec_t nt = tp->t_tspec;
   3626 	tspec_t ot = tn->tn_type->t_tspec;
   3627 
   3628 	if (allow_trad && allow_c90 && op == FARG)
   3629 		check_prototype_conversion(arg, nt, ot, tp, tn);
   3630 
   3631 	if (nt == BOOL) {
   3632 		/* No further checks. */
   3633 
   3634 	} else if (is_integer(nt)) {
   3635 		if (ot == BOOL) {
   3636 			/* No further checks. */
   3637 		} else if (is_integer(ot))
   3638 			convert_integer_from_integer(op, arg, nt, ot, tp, tn);
   3639 		else if (is_floating(ot))
   3640 			convert_integer_from_floating(op, tp, tn);
   3641 		else if (ot == PTR)
   3642 			convert_integer_from_pointer(op, nt, tp, tn);
   3643 
   3644 	} else if (is_floating(nt)) {
   3645 		if (is_integer(ot)) {
   3646 			/* implicit conversion from integer '%s' to ... */
   3647 			query_message(19,
   3648 			    type_name(tn->tn_type), type_name(tp));
   3649 		}
   3650 
   3651 	} else if (nt == PTR) {
   3652 		if (is_null_pointer(tn)) {
   3653 			/* a null pointer may be assigned to any pointer. */
   3654 		} else if (ot == PTR && op == CVT)
   3655 			convert_pointer_from_pointer(tp, tn);
   3656 	}
   3657 
   3658 	tnode_t *ntn = expr_alloc_tnode();
   3659 	ntn->tn_op = CVT;
   3660 	ntn->tn_type = tp;
   3661 	ntn->tn_cast = op == CVT;
   3662 	ntn->tn_sys |= tn->tn_sys;
   3663 	ntn->u.ops.right = NULL;
   3664 	if (tn->tn_op != CON || nt == VOID) {
   3665 		ntn->u.ops.left = tn;
   3666 	} else {
   3667 		ntn->tn_op = CON;
   3668 		convert_constant(op, arg, ntn->tn_type, &ntn->u.value,
   3669 		    &tn->u.value);
   3670 	}
   3671 
   3672 	return ntn;
   3673 }
   3674 
   3675 static void
   3676 convert_constant_floating(op_t op, int arg, tspec_t ot, const type_t *tp,
   3677 			  tspec_t nt, val_t *v, val_t *nv)
   3678 {
   3679 	long double max = 0.0, min = 0.0;
   3680 
   3681 	switch (nt) {
   3682 	case CHAR:
   3683 		max = TARG_CHAR_MAX;	min = TARG_CHAR_MIN;	break;
   3684 	case UCHAR:
   3685 		max = TARG_UCHAR_MAX;	min = 0;		break;
   3686 	case SCHAR:
   3687 		max = TARG_SCHAR_MAX;	min = TARG_SCHAR_MIN;	break;
   3688 	case SHORT:
   3689 		max = TARG_SHRT_MAX;	min = TARG_SHRT_MIN;	break;
   3690 	case USHORT:
   3691 		max = TARG_USHRT_MAX;	min = 0;		break;
   3692 	case ENUM:
   3693 	case INT:
   3694 		max = TARG_INT_MAX;	min = TARG_INT_MIN;	break;
   3695 	case UINT:
   3696 		max = TARG_UINT_MAX;	min = 0;		break;
   3697 	case LONG:
   3698 		max = TARG_LONG_MAX;	min = TARG_LONG_MIN;	break;
   3699 	case ULONG:
   3700 		max = TARG_ULONG_MAX;	min = 0;		break;
   3701 	case LLONG:
   3702 		max = LLONG_MAX;	min = LLONG_MIN;	break;
   3703 	case ULLONG:
   3704 		max = ULLONG_MAX;	min = 0;		break;
   3705 	case FLOAT:
   3706 	case FCOMPLEX:
   3707 		max = FLT_MAX;		min = -FLT_MAX;		break;
   3708 	case DOUBLE:
   3709 	case DCOMPLEX:
   3710 		max = DBL_MAX;		min = -DBL_MAX;		break;
   3711 	case PTR:
   3712 		/* Already got an error because of float --> ptr */
   3713 	case LDOUBLE:
   3714 	case LCOMPLEX:
   3715 		/* LINTED 248 */
   3716 		max = LDBL_MAX;		min = -max;		break;
   3717 	default:
   3718 		lint_assert(/*CONSTCOND*/false);
   3719 	}
   3720 	if (v->u.floating > max || v->u.floating < min) {
   3721 		lint_assert(nt != LDOUBLE);
   3722 		if (op == FARG) {
   3723 			/* conversion of '%s' to '%s' is out of range, ... */
   3724 			warning(295,
   3725 			    type_name(gettyp(ot)), type_name(tp), arg);
   3726 		} else {
   3727 			/* conversion of '%s' to '%s' is out of range */
   3728 			warning(119, type_name(gettyp(ot)), type_name(tp));
   3729 		}
   3730 		v->u.floating = v->u.floating > 0 ? max : min;
   3731 	}
   3732 
   3733 	if (nt == FLOAT || nt == FCOMPLEX)
   3734 		nv->u.floating = (float)v->u.floating;
   3735 	else if (nt == DOUBLE || nt == DCOMPLEX)
   3736 		nv->u.floating = (double)v->u.floating;
   3737 	else if (nt == LDOUBLE || nt == LCOMPLEX)
   3738 		nv->u.floating = v->u.floating;
   3739 	else
   3740 		nv->u.integer = (int64_t)v->u.floating;
   3741 }
   3742 
   3743 static bool
   3744 convert_constant_to_floating(tspec_t nt, val_t *nv,
   3745 			     tspec_t ot, const val_t *v)
   3746 {
   3747 	if (nt == FLOAT) {
   3748 		nv->u.floating = (ot == PTR || is_uinteger(ot)) ?
   3749 		    (float)(uint64_t)v->u.integer : (float)v->u.integer;
   3750 	} else if (nt == DOUBLE) {
   3751 		nv->u.floating = (ot == PTR || is_uinteger(ot)) ?
   3752 		    (double)(uint64_t)v->u.integer : (double)v->u.integer;
   3753 	} else if (nt == LDOUBLE) {
   3754 		nv->u.floating = (ot == PTR || is_uinteger(ot))
   3755 		    ? (long double)(uint64_t)v->u.integer
   3756 		    : (long double)v->u.integer;
   3757 	} else
   3758 		return false;
   3759 	return true;
   3760 }
   3761 
   3762 /*
   3763  * Print a warning if bits which were set are lost due to the conversion.
   3764  * This can happen with operator ORASS only.
   3765  */
   3766 static void
   3767 convert_constant_check_range_bitor(size_t nsz, size_t osz, const val_t *v,
   3768 				   uint64_t xmask, op_t op)
   3769 {
   3770 	if (nsz < osz && (v->u.integer & xmask) != 0)
   3771 		/* constant truncated by conversion, op '%s' */
   3772 		warning(306, op_name(op));
   3773 }
   3774 
   3775 static void
   3776 convert_constant_check_range_bitand(size_t nsz, size_t osz,
   3777 				    uint64_t xmask, const val_t *nv,
   3778 				    tspec_t ot, const val_t *v,
   3779 				    const type_t *tp, op_t op)
   3780 {
   3781 	if (nsz > osz &&
   3782 	    (nv->u.integer & bit((unsigned int)(osz - 1))) != 0 &&
   3783 	    (nv->u.integer & xmask) != xmask) {
   3784 		/* extra bits set to 0 in conversion of '%s' to '%s', ... */
   3785 		warning(309, type_name(gettyp(ot)),
   3786 		    type_name(tp), op_name(op));
   3787 	} else if (nsz < osz &&
   3788 	    (v->u.integer & xmask) != xmask &&
   3789 	    (v->u.integer & xmask) != 0)
   3790 		/* constant truncated by conversion, op '%s' */
   3791 		warning(306, op_name(op));
   3792 }
   3793 
   3794 static void
   3795 convert_constant_check_range_signed(op_t op, int arg)
   3796 {
   3797 	if (op == ASSIGN)
   3798 		/* assignment of negative constant to unsigned type */
   3799 		warning(164);
   3800 	else if (op == INIT)
   3801 		/* initialization of unsigned with negative constant */
   3802 		warning(221);
   3803 	else if (op == FARG)
   3804 		/* conversion of negative constant to unsigned type, ... */
   3805 		warning(296, arg);
   3806 	else if (modtab[op].m_comparison) {
   3807 		/* handled by check_integer_comparison() */
   3808 	} else
   3809 		/* conversion of negative constant to unsigned type */
   3810 		warning(222);
   3811 }
   3812 
   3813 /*
   3814  * Loss of significant bit(s). All truncated bits of unsigned types or all
   3815  * truncated bits plus the msb of the target for signed types are considered
   3816  * to be significant bits. Loss of significant bits means that at least one
   3817  * of the bits was set in an unsigned type or that at least one but not all
   3818  * of the bits was set in a signed type. Loss of significant bits means that
   3819  * it is not possible, also not with necessary casts, to convert back to the
   3820  * original type. An example for a necessary cast is:
   3821  *	char c;	int	i; c = 128;
   3822  *	i = c;			** yields -128 **
   3823  *	i = (unsigned char)c;	** yields 128 **
   3824  */
   3825 static void
   3826 warn_constant_check_range_truncated(op_t op, int arg, const type_t *tp,
   3827 				    tspec_t ot)
   3828 {
   3829 	if (op == ASSIGN && tp->t_bitfield)
   3830 		/* precision lost in bit-field assignment */
   3831 		warning(166);
   3832 	else if (op == ASSIGN)
   3833 		/* constant truncated by assignment */
   3834 		warning(165);
   3835 	else if (op == INIT && tp->t_bitfield)
   3836 		/* bit-field initializer does not fit */
   3837 		warning(180);
   3838 	else if (op == INIT)
   3839 		/* initializer does not fit */
   3840 		warning(178);
   3841 	else if (op == CASE)
   3842 		/* case label affected by conversion */
   3843 		warning(196);
   3844 	else if (op == FARG)
   3845 		/* conversion of '%s' to '%s' is out of range, arg #%d */
   3846 		warning(295, type_name(gettyp(ot)), type_name(tp), arg);
   3847 	else
   3848 		/* conversion of '%s' to '%s' is out of range */
   3849 		warning(119, type_name(gettyp(ot)), type_name(tp));
   3850 }
   3851 
   3852 static void
   3853 warn_constant_check_range_loss(op_t op, int arg, const type_t *tp,
   3854 				  tspec_t ot)
   3855 {
   3856 	if (op == ASSIGN && tp->t_bitfield)
   3857 		/* precision lost in bit-field assignment */
   3858 		warning(166);
   3859 	else if (op == INIT && tp->t_bitfield)
   3860 		/* bit-field initializer out of range */
   3861 		warning(11);
   3862 	else if (op == CASE)
   3863 		/* case label affected by conversion */
   3864 		warning(196);
   3865 	else if (op == FARG)
   3866 		/* conversion of '%s' to '%s' is out of range, arg #%d */
   3867 		warning(295, type_name(gettyp(ot)), type_name(tp), arg);
   3868 	else
   3869 		/* conversion of '%s' to '%s' is out of range */
   3870 		warning(119, type_name(gettyp(ot)), type_name(tp));
   3871 }
   3872 
   3873 static void
   3874 convert_constant_check_range(tspec_t ot, const type_t *tp, tspec_t nt,
   3875 			     op_t op, int arg, const val_t *v, val_t *nv)
   3876 {
   3877 	unsigned int obitsz, nbitsz;
   3878 	uint64_t xmask, xmsk1;
   3879 
   3880 	obitsz = size_in_bits(ot);
   3881 	nbitsz = tp->t_bitfield ? tp->t_bit_field_width : size_in_bits(nt);
   3882 	xmask = value_bits(nbitsz) ^ value_bits(obitsz);
   3883 	xmsk1 = value_bits(nbitsz) ^ value_bits(obitsz - 1);
   3884 	if (op == ORASS || op == BITOR || op == BITXOR) {
   3885 		convert_constant_check_range_bitor(
   3886 		    nbitsz, obitsz, v, xmask, op);
   3887 	} else if (op == ANDASS || op == BITAND) {
   3888 		convert_constant_check_range_bitand(
   3889 		    nbitsz, obitsz, xmask, nv, ot, v, tp, op);
   3890 	} else if (nt != PTR && is_uinteger(nt) &&
   3891 	    ot != PTR && !is_uinteger(ot) &&
   3892 	    v->u.integer < 0)
   3893 		convert_constant_check_range_signed(op, arg);
   3894 	else if (nv->u.integer != v->u.integer && nbitsz <= obitsz &&
   3895 	    (v->u.integer & xmask) != 0 &&
   3896 	    (is_uinteger(ot) || (v->u.integer & xmsk1) != xmsk1))
   3897 		warn_constant_check_range_truncated(op, arg, tp, ot);
   3898 	else if (nv->u.integer != v->u.integer)
   3899 		warn_constant_check_range_loss(op, arg, tp, ot);
   3900 }
   3901 
   3902 /* Converts a typed constant to a constant of another type. */
   3903 void
   3904 convert_constant(op_t op, int arg, const type_t *ntp, val_t *nv, val_t *ov)
   3905 {
   3906 	/*
   3907 	 * TODO: make 'ov' const; the name of this function does not suggest
   3908 	 *  that it modifies 'ov'.
   3909 	 */
   3910 	tspec_t ot = ov->v_tspec;
   3911 	tspec_t nt = nv->v_tspec = ntp->t_tspec;
   3912 	bool range_check = false;
   3913 
   3914 	if (nt == BOOL) {	/* C99 6.3.1.2 */
   3915 		nv->v_unsigned_since_c90 = false;
   3916 		nv->u.integer = is_nonzero_val(ov) ? 1 : 0;
   3917 		return;
   3918 	}
   3919 
   3920 	if (ot == FLOAT || ot == DOUBLE || ot == LDOUBLE)
   3921 		convert_constant_floating(op, arg, ot, ntp, nt, ov, nv);
   3922 	else if (!convert_constant_to_floating(nt, nv, ot, ov)) {
   3923 		range_check = true;	/* Check for lost precision. */
   3924 		nv->u.integer = ov->u.integer;
   3925 	}
   3926 
   3927 	if (allow_trad && allow_c90 && ov->v_unsigned_since_c90 &&
   3928 	    (is_floating(nt) || (
   3929 		(is_integer(nt) && !is_uinteger(nt) &&
   3930 		    portable_rank_cmp(nt, ot) > 0)))) {
   3931 		/* C90 treats constant as unsigned */
   3932 		warning(157);
   3933 		ov->v_unsigned_since_c90 = false;
   3934 	}
   3935 
   3936 	if (is_integer(nt)) {
   3937 		unsigned int size = ntp->t_bitfield
   3938 		    ? ntp->t_bit_field_width : size_in_bits(nt);
   3939 		nv->u.integer = convert_integer(nv->u.integer, nt, size);
   3940 	}
   3941 
   3942 	if (range_check && op != CVT)
   3943 		convert_constant_check_range(ot, ntp, nt, op, arg, ov, nv);
   3944 }
   3945 
   3946 tnode_t *
   3947 build_sizeof(const type_t *tp)
   3948 {
   3949 	unsigned int size_in_bytes = type_size_in_bits(tp) / CHAR_SIZE;
   3950 	tnode_t *tn = build_integer_constant(SIZEOF_TSPEC, size_in_bytes);
   3951 	tn->tn_system_dependent = true;
   3952 	debug_step("build_sizeof '%s' = %u", type_name(tp), size_in_bytes);
   3953 	return tn;
   3954 }
   3955 
   3956 tnode_t *
   3957 build_offsetof(const type_t *tp, designation dn)
   3958 {
   3959 	unsigned int offset_in_bits = 0;
   3960 
   3961 	if (!is_struct_or_union(tp->t_tspec)) {
   3962 		/* unacceptable operand of '%s' */
   3963 		error(111, "offsetof");
   3964 		goto proceed;
   3965 	}
   3966 	for (size_t i = 0; i < dn.dn_len; i++) {
   3967 		const designator *dr = dn.dn_items + i;
   3968 		if (dr->dr_kind == DK_SUBSCRIPT) {
   3969 			if (tp->t_tspec != ARRAY)
   3970 				goto proceed;	/* silent error */
   3971 			tp = tp->t_subt;
   3972 			offset_in_bits += (unsigned) dr->dr_subscript
   3973 			    * type_size_in_bits(tp);
   3974 		} else {
   3975 			if (!is_struct_or_union(tp->t_tspec))
   3976 				goto proceed;	/* silent error */
   3977 			const char *name = dr->dr_member->s_name;
   3978 			sym_t *mem = find_member(tp->u.sou, name);
   3979 			if (mem == NULL) {
   3980 				/* type '%s' does not have member '%s' */
   3981 				error(101, name, type_name(tp));
   3982 				goto proceed;
   3983 			}
   3984 			tp = mem->s_type;
   3985 			offset_in_bits += mem->u.s_member.sm_offset_in_bits;
   3986 		}
   3987 	}
   3988 	free(dn.dn_items);
   3989 
   3990 proceed:;
   3991 	unsigned int offset_in_bytes = offset_in_bits / CHAR_SIZE;
   3992 	tnode_t *tn = build_integer_constant(SIZEOF_TSPEC, offset_in_bytes);
   3993 	tn->tn_system_dependent = true;
   3994 	return tn;
   3995 }
   3996 
   3997 unsigned int
   3998 type_size_in_bits(const type_t *tp)
   3999 {
   4000 
   4001 	unsigned int elem = 1;
   4002 	bool flex = false;
   4003 	lint_assert(tp != NULL);
   4004 	while (tp->t_tspec == ARRAY) {
   4005 		flex = true;	/* allow c99 flex arrays [] [0] */
   4006 		elem *= tp->u.dimension;
   4007 		tp = tp->t_subt;
   4008 	}
   4009 	if (elem == 0 && !flex) {
   4010 		/* cannot take size/alignment of incomplete type */
   4011 		error(143);
   4012 		elem = 1;
   4013 	}
   4014 
   4015 	unsigned int elsz;
   4016 	switch (tp->t_tspec) {
   4017 	case VOID:
   4018 		/* cannot take size/alignment of void */
   4019 		error(146);
   4020 		elsz = 1;
   4021 		break;
   4022 	case FUNC:
   4023 		/* cannot take size/alignment of function type '%s' */
   4024 		error(144, type_name(tp));
   4025 		elsz = 1;
   4026 		break;
   4027 	case STRUCT:
   4028 	case UNION:
   4029 		if (is_incomplete(tp)) {
   4030 			/* cannot take size/alignment of incomplete type */
   4031 			error(143);
   4032 			elsz = 1;
   4033 		} else
   4034 			elsz = tp->u.sou->sou_size_in_bits;
   4035 		break;
   4036 	case ENUM:
   4037 		if (is_incomplete(tp)) {
   4038 			/* cannot take size/alignment of incomplete type */
   4039 			warning(143);
   4040 		}
   4041 		/* FALLTHROUGH */
   4042 	default:
   4043 		if (tp->t_bitfield)
   4044 			/* cannot take size/alignment of bit-field */
   4045 			error(145);
   4046 		elsz = size_in_bits(tp->t_tspec);
   4047 		lint_assert(elsz > 0);
   4048 		break;
   4049 	}
   4050 
   4051 	return elem * elsz;
   4052 }
   4053 
   4054 /* C11 6.5.3.4, GCC */
   4055 tnode_t *
   4056 build_alignof(const type_t *tp)
   4057 {
   4058 	if (tp->t_tspec == FUNC) {
   4059 		/* cannot take size/alignment of function type '%s' */
   4060 		error(144, type_name(tp));
   4061 		return NULL;
   4062 	}
   4063 	if (tp->t_tspec == VOID) {
   4064 		/* cannot take size/alignment of void */
   4065 		error(146);
   4066 		return NULL;
   4067 	}
   4068 	if (is_incomplete(tp)) {
   4069 		/* cannot take size/alignment of incomplete type */
   4070 		error(143);
   4071 		return NULL;
   4072 	}
   4073 	if (tp->t_bitfield) {
   4074 		/* cannot take size/alignment of bit-field */
   4075 		error(145);
   4076 		return NULL;
   4077 	}
   4078 	return build_integer_constant(SIZEOF_TSPEC,
   4079 	    (int64_t)alignment_in_bits(tp) / CHAR_SIZE);
   4080 }
   4081 
   4082 static tnode_t *
   4083 cast_to_union(tnode_t *otn, bool sys, type_t *ntp)
   4084 {
   4085 
   4086 	if (!allow_gcc) {
   4087 		/* union cast is a GCC extension */
   4088 		error(328);
   4089 		return NULL;
   4090 	}
   4091 
   4092 	for (const sym_t *m = ntp->u.sou->sou_first_member;
   4093 	    m != NULL; m = m->s_next) {
   4094 		if (types_compatible(m->s_type, otn->tn_type,
   4095 		    false, false, NULL)) {
   4096 			tnode_t *ntn = build_op(CVT, sys, ntp, otn, NULL);
   4097 			ntn->tn_cast = true;
   4098 			return ntn;
   4099 		}
   4100 	}
   4101 
   4102 	/* type '%s' is not a member of '%s' */
   4103 	error(329, type_name(otn->tn_type), type_name(ntp));
   4104 	return NULL;
   4105 }
   4106 
   4107 tnode_t *
   4108 cast(tnode_t *tn, bool sys, type_t *tp)
   4109 {
   4110 
   4111 	if (tn == NULL)
   4112 		return NULL;
   4113 
   4114 	tn = cconv(tn);
   4115 
   4116 	lint_assert(tp != NULL);
   4117 	tspec_t nt = tp->t_tspec;
   4118 	tspec_t ot = tn->tn_type->t_tspec;
   4119 
   4120 	if (nt == VOID) {
   4121 		/*
   4122 		 * C90 6.3.4, C99 6.5.4p2 and C11 6.5.4p2 allow any type to be
   4123 		 * cast to void.  The only other allowed casts are from a
   4124 		 * scalar type to a scalar type.
   4125 		 */
   4126 	} else if (nt == UNION)
   4127 		return cast_to_union(tn, sys, tp);
   4128 	else if (nt == STRUCT || nt == ARRAY || nt == FUNC) {
   4129 		/* Casting to a struct is an undocumented GCC extension. */
   4130 		if (!(allow_gcc && nt == STRUCT))
   4131 			goto invalid_cast;
   4132 	} else if (is_struct_or_union(ot))
   4133 		goto invalid_cast;
   4134 	else if (ot == VOID) {
   4135 		/* improper cast of void expression */
   4136 		error(148);
   4137 		return NULL;
   4138 	} else if (is_integer(nt) && is_scalar(ot)) {
   4139 		/* ok */
   4140 	} else if (is_floating(nt) && is_arithmetic(ot)) {
   4141 		/* ok */
   4142 	} else if (nt == PTR && is_integer(ot)) {
   4143 		/* ok */
   4144 	} else if (nt == PTR && ot == PTR) {
   4145 		if (!tp->t_subt->t_const && tn->tn_type->t_subt->t_const) {
   4146 			if (hflag)
   4147 				/* cast discards 'const' from type '%s' */
   4148 				warning(275, type_name(tn->tn_type));
   4149 		}
   4150 	} else
   4151 		goto invalid_cast;
   4152 
   4153 	if (any_query_enabled
   4154 	    && types_compatible(tp, tn->tn_type, false, false, NULL))
   4155 		/* no-op cast from '%s' to '%s' */
   4156 		query_message(6, type_name(tn->tn_type), type_name(tp));
   4157 
   4158 	tn = convert(CVT, 0, tp, tn);
   4159 	tn->tn_cast = true;
   4160 	tn->tn_sys = sys;
   4161 
   4162 	return tn;
   4163 
   4164 invalid_cast:
   4165 	/* invalid cast from '%s' to '%s' */
   4166 	error(147, type_name(tn->tn_type), type_name(tp));
   4167 	return NULL;
   4168 }
   4169 
   4170 void
   4171 add_function_argument(function_call *call, tnode_t *arg)
   4172 {
   4173 	/*
   4174 	 * If there was a serious error in the expression for the argument,
   4175 	 * create a dummy argument so the positions of the remaining arguments
   4176 	 * will not change.
   4177 	 */
   4178 	if (arg == NULL)
   4179 		arg = build_integer_constant(INT, 0);
   4180 
   4181 	if (call->args_len >= call->args_cap) {
   4182 		call->args_cap += 8;
   4183 		tnode_t **new_args = expr_zero_alloc(
   4184 		    call->args_cap * sizeof(*call->args),
   4185 		    "function_call.args");
   4186 		memcpy(new_args, call->args,
   4187 		    call->args_len * sizeof(*call->args));
   4188 		call->args = new_args;
   4189 	}
   4190 	call->args[call->args_len++] = arg;
   4191 }
   4192 
   4193 /*
   4194  * Compare the type of an argument with the corresponding type of a
   4195  * prototype parameter. If it is a valid combination, but both types
   4196  * are not the same, insert a conversion to convert the argument into
   4197  * the type of the parameter.
   4198  */
   4199 static tnode_t *
   4200 check_prototype_argument(
   4201 	int n,		/* pos of arg */
   4202 	type_t *tp,		/* expected type (from prototype) */
   4203 	tnode_t *tn)		/* argument */
   4204 {
   4205 	tnode_t *ln = xcalloc(1, sizeof(*ln));
   4206 	ln->tn_type = expr_unqualified_type(tp);
   4207 	ln->tn_lvalue = true;
   4208 	if (typeok(FARG, n, ln, tn)) {
   4209 		bool dowarn;
   4210 		if (!types_compatible(tp, tn->tn_type,
   4211 		    true, false, (dowarn = false, &dowarn)) || dowarn)
   4212 			tn = convert(FARG, n, tp, tn);
   4213 	}
   4214 	free(ln);
   4215 	return tn;
   4216 }
   4217 
   4218 /*
   4219  * Check types of all function arguments and insert conversions,
   4220  * if necessary.
   4221  */
   4222 static void
   4223 check_function_arguments(const function_call *call)
   4224 {
   4225 	type_t *ftp = call->func->tn_type->t_subt;
   4226 
   4227 	/* get # of parameters in the prototype */
   4228 	int npar = 0;
   4229 	for (const sym_t *p = ftp->u.params; p != NULL; p = p->s_next)
   4230 		npar++;
   4231 
   4232 	int narg = (int)call->args_len;
   4233 
   4234 	const sym_t *param = ftp->u.params;
   4235 	if (ftp->t_proto && npar != narg && !(ftp->t_vararg && npar < narg)) {
   4236 		/* argument mismatch: %d %s passed, %d expected */
   4237 		error(150, narg, narg != 1 ? "arguments" : "argument", npar);
   4238 		param = NULL;
   4239 	}
   4240 
   4241 	for (int i = 0; i < narg; i++) {
   4242 		tnode_t *arg = call->args[i];
   4243 
   4244 		/* some things which are always not allowed */
   4245 		tspec_t at = arg->tn_type->t_tspec;
   4246 		if (at == VOID) {
   4247 			/* void expressions may not be arguments, arg #%d */
   4248 			error(151, i + 1);
   4249 			return;
   4250 		}
   4251 		if (is_struct_or_union(at) && is_incomplete(arg->tn_type)) {
   4252 			/* argument cannot have unknown size, arg #%d */
   4253 			error(152, i + 1);
   4254 			return;
   4255 		}
   4256 		if (is_integer(at) &&
   4257 		    arg->tn_type->t_is_enum &&
   4258 		    is_incomplete(arg->tn_type)) {
   4259 			/* argument cannot have unknown size, arg #%d */
   4260 			warning(152, i + 1);
   4261 		}
   4262 
   4263 		arg = cconv(arg);
   4264 		call->args[i] = arg;
   4265 
   4266 		arg = param != NULL
   4267 		    ? check_prototype_argument(i + 1, param->s_type, arg)
   4268 		    : promote(NOOP, true, arg);
   4269 		call->args[i] = arg;
   4270 
   4271 		if (param != NULL)
   4272 			param = param->s_next;
   4273 	}
   4274 }
   4275 
   4276 tnode_t *
   4277 build_function_call(tnode_t *func, bool sys, function_call *call)
   4278 {
   4279 
   4280 	if (func == NULL)
   4281 		return NULL;
   4282 
   4283 	op_t op = func->tn_op == NAME && func->tn_type->t_tspec == FUNC
   4284 	    ? CALL : ICALL;
   4285 
   4286 	call->func = func;
   4287 	check_ctype_function_call(call);
   4288 
   4289 	func = cconv(func);
   4290 	call->func = func;
   4291 
   4292 	if (func->tn_type->t_tspec != PTR ||
   4293 	    func->tn_type->t_subt->t_tspec != FUNC) {
   4294 		/* cannot call '%s', must be a function */
   4295 		error(149, type_name(func->tn_type));
   4296 		return NULL;
   4297 	}
   4298 
   4299 	check_function_arguments(call);
   4300 
   4301 	tnode_t *ntn = expr_alloc_tnode();
   4302 	ntn->tn_op = op;
   4303 	ntn->tn_type = func->tn_type->t_subt->t_subt;
   4304 	ntn->tn_sys = sys;
   4305 	ntn->u.call = call;
   4306 	return ntn;
   4307 }
   4308 
   4309 /*
   4310  * Return the value of an integral constant expression.
   4311  * If the expression is not constant or its type is not an integer
   4312  * type, an error message is printed.
   4313  */
   4314 val_t *
   4315 integer_constant(tnode_t *tn, bool required)
   4316 {
   4317 
   4318 	if (tn != NULL)
   4319 		tn = cconv(tn);
   4320 	if (tn != NULL)
   4321 		tn = promote(NOOP, false, tn);
   4322 
   4323 	val_t *v = xcalloc(1, sizeof(*v));
   4324 
   4325 	if (tn == NULL) {
   4326 		lint_assert(seen_error);
   4327 		debug_step("constant node is null; returning 1 instead");
   4328 		v->v_tspec = INT;
   4329 		v->u.integer = 1;
   4330 		return v;
   4331 	}
   4332 
   4333 	v->v_tspec = tn->tn_type->t_tspec;
   4334 
   4335 	if (tn->tn_op == CON) {
   4336 		lint_assert(tn->tn_type->t_tspec == tn->u.value.v_tspec);
   4337 		if (is_integer(tn->u.value.v_tspec)) {
   4338 			v->v_unsigned_since_c90 =
   4339 			    tn->u.value.v_unsigned_since_c90;
   4340 			v->u.integer = tn->u.value.u.integer;
   4341 			return v;
   4342 		}
   4343 		v->u.integer = (int64_t)tn->u.value.u.floating;
   4344 	} else
   4345 		v->u.integer = 1;
   4346 
   4347 	if (required)
   4348 		/* integral constant expression expected */
   4349 		error(55);
   4350 	else
   4351 		/* variable array dimension is a C99/GCC extension */
   4352 		c99ism(318);
   4353 
   4354 	if (!is_integer(v->v_tspec))
   4355 		v->v_tspec = INT;
   4356 
   4357 	return v;
   4358 }
   4359 
   4360 static bool
   4361 is_constcond_false(const tnode_t *tn, tspec_t t)
   4362 {
   4363 	return (t == BOOL || t == INT) &&
   4364 	    tn->tn_op == CON && tn->u.value.u.integer == 0;
   4365 }
   4366 
   4367 /*
   4368  * Perform some tests on expressions which can't be done in build_binary()
   4369  * and functions called by build_binary(). These tests must be done here
   4370  * because we need some information about the context in which the operations
   4371  * are performed.
   4372  * After all tests are performed and dofreeblk is true, expr() frees the
   4373  * memory which is used for the expression.
   4374  */
   4375 void
   4376 expr(tnode_t *tn, bool vctx, bool cond, bool dofreeblk, bool is_do_while)
   4377 {
   4378 
   4379 	if (tn == NULL) {	/* in case of errors */
   4380 		expr_free_all();
   4381 		return;
   4382 	}
   4383 
   4384 	/* expr() is also called in global initializations */
   4385 	if (dcs->d_kind != DLK_EXTERN && !is_do_while)
   4386 		check_statement_reachable();
   4387 
   4388 	check_expr_misc(tn, vctx, cond, !cond, false, false, false);
   4389 	if (tn->tn_op == ASSIGN && !tn->tn_parenthesized) {
   4390 		if (hflag && cond)
   4391 			/* assignment in conditional context */
   4392 			warning(159);
   4393 	} else if (tn->tn_op == CON) {
   4394 		if (hflag && cond && !suppress_constcond &&
   4395 		    !tn->tn_system_dependent &&
   4396 		    !(is_do_while &&
   4397 		      is_constcond_false(tn, tn->tn_type->t_tspec)))
   4398 			/* constant in conditional context */
   4399 			warning(161);
   4400 	}
   4401 	if (!modtab[tn->tn_op].m_has_side_effect) {
   4402 		/*
   4403 		 * for left operands of COMMA this warning is already printed
   4404 		 */
   4405 		if (tn->tn_op != COMMA && !vctx && !cond)
   4406 			check_null_effect(tn);
   4407 	}
   4408 	debug_node(tn);
   4409 
   4410 	if (dofreeblk)
   4411 		expr_free_all();
   4412 }
   4413 
   4414 /* If the expression has the form '*(arr + idx)', check the array index. */
   4415 static void
   4416 check_array_index(const tnode_t *indir, bool taking_address)
   4417 {
   4418 	const tnode_t *plus, *arr, *idx;
   4419 
   4420 	if (indir->tn_op == INDIR
   4421 	    && (plus = indir->u.ops.left, plus->tn_op == PLUS)
   4422 	    && plus->u.ops.left->tn_op == ADDR
   4423 	    && (arr = plus->u.ops.left->u.ops.left, true)
   4424 	    && (arr->tn_op == STRING || arr->tn_op == NAME)
   4425 	    && arr->tn_type->t_tspec == ARRAY
   4426 	    && (idx = plus->u.ops.right, idx->tn_op == CON)
   4427 	    && (!is_incomplete(arr->tn_type) || idx->u.value.u.integer < 0))
   4428 		goto proceed;
   4429 	return;
   4430 
   4431 proceed:;
   4432 	int elsz = length_in_bits(arr->tn_type->t_subt, NULL);
   4433 	if (elsz == 0)
   4434 		return;
   4435 	elsz /= CHAR_SIZE;
   4436 
   4437 	/* Change the unit of the index from bytes to element size. */
   4438 	int64_t con = is_uinteger(idx->tn_type->t_tspec)
   4439 	    ? (int64_t)((uint64_t)idx->u.value.u.integer / elsz)
   4440 	    : idx->u.value.u.integer / elsz;
   4441 
   4442 	int dim = arr->tn_type->u.dimension + (taking_address ? 1 : 0);
   4443 
   4444 	if (!is_uinteger(idx->tn_type->t_tspec) && con < 0)
   4445 		/* array subscript %jd cannot be negative */
   4446 		warning(167, (intmax_t)con);
   4447 	else if (dim > 0 && (uint64_t)con >= (uint64_t)dim)
   4448 		/* array subscript %ju cannot be > %d */
   4449 		warning(168, (uintmax_t)con, dim - 1);
   4450 }
   4451 
   4452 static void
   4453 check_expr_addr(const tnode_t *ln, bool szof, bool fcall)
   4454 {
   4455 	/* XXX: Taking warn_about_unreachable into account here feels wrong. */
   4456 	if (ln->tn_op == NAME && (reached || !warn_about_unreachable)) {
   4457 		if (!szof)
   4458 			mark_as_set(ln->u.sym);
   4459 		mark_as_used(ln->u.sym, fcall, szof);
   4460 	}
   4461 	check_array_index(ln, true);
   4462 }
   4463 
   4464 /*
   4465  * If there is an asm statement in one of the compound statements around,
   4466  * there may be other side effects, so don't warn.
   4467  */
   4468 static bool
   4469 is_asm_around(void)
   4470 {
   4471 	for (decl_level *dl = dcs; dl != NULL; dl = dl->d_enclosing)
   4472 		if (dl->d_asm)
   4473 			return true;
   4474 	return false;
   4475 }
   4476 
   4477 static void
   4478 check_expr_side_effect(const tnode_t *ln, bool szof)
   4479 {
   4480 
   4481 	/* XXX: Taking warn_about_unreachable into account here feels wrong. */
   4482 	if (ln->tn_op == NAME && (reached || !warn_about_unreachable)) {
   4483 		scl_t sc = ln->u.sym->s_scl;
   4484 		if (sc != EXTERN && sc != STATIC &&
   4485 		    !ln->u.sym->s_set && !szof && !is_asm_around()) {
   4486 			/* '%s' may be used before set */
   4487 			warning(158, ln->u.sym->s_name);
   4488 			mark_as_set(ln->u.sym);
   4489 		}
   4490 		mark_as_used(ln->u.sym, false, false);
   4491 	}
   4492 }
   4493 
   4494 static void
   4495 check_expr_assign(const tnode_t *ln, bool szof)
   4496 {
   4497 	/* XXX: Taking warn_about_unreachable into account here feels wrong. */
   4498 	if (ln->tn_op == NAME && !szof && (reached || !warn_about_unreachable)) {
   4499 		mark_as_set(ln->u.sym);
   4500 		if (ln->u.sym->s_scl == EXTERN)
   4501 			outusg(ln->u.sym);
   4502 	}
   4503 	check_array_index(ln, false);
   4504 }
   4505 
   4506 static void
   4507 check_expr_call(const tnode_t *tn, const tnode_t *ln,
   4508 		bool szof, bool vctx, bool cond, bool retval_discarded)
   4509 {
   4510 	lint_assert(ln->tn_op == ADDR);
   4511 	lint_assert(ln->u.ops.left->tn_op == NAME);
   4512 	if (!szof && !is_compiler_builtin(ln->u.ops.left->u.sym->s_name))
   4513 		outcall(tn, vctx || cond, retval_discarded);
   4514 	check_snprintb(tn);
   4515 }
   4516 
   4517 static void
   4518 check_expr_op(op_t op, const tnode_t *ln, bool szof, bool fcall, bool eqwarn)
   4519 {
   4520 	switch (op) {
   4521 	case ADDR:
   4522 		check_expr_addr(ln, szof, fcall);
   4523 		break;
   4524 	case LOAD:
   4525 		check_array_index(ln, false);
   4526 		/* FALLTHROUGH */
   4527 	case INCBEF:
   4528 	case DECBEF:
   4529 	case INCAFT:
   4530 	case DECAFT:
   4531 	case ADDASS:
   4532 	case SUBASS:
   4533 	case MULASS:
   4534 	case DIVASS:
   4535 	case MODASS:
   4536 	case ANDASS:
   4537 	case ORASS:
   4538 	case XORASS:
   4539 	case SHLASS:
   4540 	case SHRASS:
   4541 	case REAL:
   4542 	case IMAG:
   4543 		check_expr_side_effect(ln, szof);
   4544 		break;
   4545 	case ASSIGN:
   4546 		check_expr_assign(ln, szof);
   4547 		break;
   4548 	case EQ:
   4549 		if (hflag && eqwarn)
   4550 			/* operator '==' found where '=' was expected */
   4551 			warning(160);
   4552 		break;
   4553 	default:
   4554 		break;
   4555 	}
   4556 }
   4557 
   4558 /*
   4559  *	vctx			???
   4560  *	cond			whether the expression is a condition that
   4561  *				will be compared with 0
   4562  *	eqwarn			whether the operator '==' might be a
   4563  *				misspelled '='
   4564  *	fcall			whether the expression is a function call
   4565  *	retval_discarded	whether the return value of a function call
   4566  *				is discarded; such calls will be analyzed by
   4567  *				lint2 in messages 4, 8 and 9
   4568  *	szof			whether the expression is part of a sizeof
   4569  *				expression, which means that its value is
   4570  *				discarded since only the type is relevant
   4571  */
   4572 void
   4573 check_expr_misc(const tnode_t *tn, bool vctx, bool cond,
   4574 		bool eqwarn, bool fcall, bool retval_discarded, bool szof)
   4575 {
   4576 
   4577 	if (tn == NULL)
   4578 		return;
   4579 	op_t op = tn->tn_op;
   4580 	if (op == NAME || op == CON || op == STRING)
   4581 		return;
   4582 	if (op == CALL || op == ICALL) {
   4583 		const function_call *call = tn->u.call;
   4584 		if (op == CALL)
   4585 			check_expr_call(tn, call->func,
   4586 			    szof, vctx, cond, retval_discarded);
   4587 		bool discard = op == CVT && tn->tn_type->t_tspec == VOID;
   4588 		check_expr_misc(call->func, false, false, false, op == CALL,
   4589 		    discard, szof);
   4590 		for (size_t i = 0, n = call->args_len; i < n; i++)
   4591 			check_expr_misc(call->args[i],
   4592 			    false, false, false, false, false, szof);
   4593 		return;
   4594 	}
   4595 
   4596 	lint_assert(has_operands(tn));
   4597 	tnode_t *ln = tn->u.ops.left;
   4598 	tnode_t *rn = tn->u.ops.right;
   4599 	check_expr_op(op, ln, szof, fcall, eqwarn);
   4600 
   4601 	const mod_t *mp = &modtab[op];
   4602 	bool cvctx = mp->m_value_context;
   4603 	bool ccond = mp->m_compares_with_zero;
   4604 	bool eq = mp->m_warn_if_operand_eq &&
   4605 	    !ln->tn_parenthesized &&
   4606 	    rn != NULL && !rn->tn_parenthesized;
   4607 
   4608 	/*
   4609 	 * Values of operands of ':' are not used if the type of at least
   4610 	 * one of the operands (for GCC compatibility) is 'void'.
   4611 	 *
   4612 	 * XXX test/value context of QUEST should probably be used as
   4613 	 * context for both operands of COLON.
   4614 	 */
   4615 	if (op == COLON && tn->tn_type->t_tspec == VOID)
   4616 		cvctx = ccond = false;
   4617 	bool discard = op == CVT && tn->tn_type->t_tspec == VOID;
   4618 	check_expr_misc(ln, cvctx, ccond, eq, op == CALL, discard, szof);
   4619 
   4620 	switch (op) {
   4621 	case LOGAND:
   4622 	case LOGOR:
   4623 		check_expr_misc(rn, false, true, eq, false, false, szof);
   4624 		break;
   4625 	case COLON:
   4626 		check_expr_misc(rn, cvctx, ccond, eq, false, false, szof);
   4627 		break;
   4628 	case COMMA:
   4629 		check_expr_misc(rn, vctx, cond, false, false, false, szof);
   4630 		break;
   4631 	default:
   4632 		if (mp->m_binary)
   4633 			check_expr_misc(rn, true, false, eq, false, false,
   4634 			    szof);
   4635 		break;
   4636 	}
   4637 }
   4638 
   4639 /*
   4640  * Return whether the expression can be used for static initialization.
   4641  *
   4642  * Constant initialization expressions must be constant or an address
   4643  * of a static object with an optional offset. In the first case,
   4644  * the result is returned in *offsp. In the second case, the static
   4645  * object is returned in *symp and the offset in *offsp.
   4646  *
   4647  * The expression can consist of PLUS, MINUS, ADDR, NAME, STRING and
   4648  * CON. Type conversions are allowed if they do not change binary
   4649  * representation (including width).
   4650  *
   4651  * C99 6.6 "Constant expressions"
   4652  * C99 6.7.8p4 restricts initializers for static storage duration
   4653  */
   4654 bool
   4655 constant_addr(const tnode_t *tn, const sym_t **symp, ptrdiff_t *offsp)
   4656 {
   4657 	const sym_t *sym;
   4658 	ptrdiff_t offs1, offs2;
   4659 	tspec_t t, ot;
   4660 
   4661 	switch (tn->tn_op) {
   4662 	case MINUS:
   4663 		if (tn->u.ops.right->tn_op == CVT)
   4664 			return constant_addr(tn->u.ops.right, symp, offsp);
   4665 		else if (tn->u.ops.right->tn_op != CON)
   4666 			return false;
   4667 		/* FALLTHROUGH */
   4668 	case PLUS:
   4669 		offs1 = offs2 = 0;
   4670 		if (tn->u.ops.left->tn_op == CON) {
   4671 			offs1 = (ptrdiff_t)tn->u.ops.left->u.value.u.integer;
   4672 			if (!constant_addr(tn->u.ops.right, &sym, &offs2))
   4673 				return false;
   4674 		} else if (tn->u.ops.right->tn_op == CON) {
   4675 			offs2 = (ptrdiff_t)tn->u.ops.right->u.value.u.integer;
   4676 			if (tn->tn_op == MINUS)
   4677 				offs2 = -offs2;
   4678 			if (!constant_addr(tn->u.ops.left, &sym, &offs1))
   4679 				return false;
   4680 		} else {
   4681 			return false;
   4682 		}
   4683 		*symp = sym;
   4684 		*offsp = offs1 + offs2;
   4685 		return true;
   4686 	case ADDR:
   4687 		if (tn->u.ops.left->tn_op == NAME) {
   4688 			*symp = tn->u.ops.left->u.sym;
   4689 			*offsp = 0;
   4690 			return true;
   4691 		} else {
   4692 			/*
   4693 			 * If this were the front end of a compiler, we would
   4694 			 * return a label instead of 0, at least if
   4695 			 * 'tn->u.ops.left->tn_op == STRING'.
   4696 			 */
   4697 			*symp = NULL;
   4698 			*offsp = 0;
   4699 			return true;
   4700 		}
   4701 	case CVT:
   4702 		t = tn->tn_type->t_tspec;
   4703 		ot = tn->u.ops.left->tn_type->t_tspec;
   4704 		if ((!is_integer(t) && t != PTR) ||
   4705 		    (!is_integer(ot) && ot != PTR)) {
   4706 			return false;
   4707 		}
   4708 #if 0
   4709 		/*-
   4710 		 * consider:
   4711 		 *	struct foo {
   4712 		 *		unsigned char a;
   4713 		 *	} f = {
   4714 		 *		(unsigned char)(unsigned long)
   4715 		 *		    (&(((struct foo *)0)->a))
   4716 		 *	};
   4717 		 * since psize(unsigned long) != psize(unsigned char),
   4718 		 * this fails.
   4719 		 */
   4720 		else if (psize(t) != psize(ot))
   4721 			return -1;
   4722 #endif
   4723 		return constant_addr(tn->u.ops.left, symp, offsp);
   4724 	default:
   4725 		return false;
   4726 	}
   4727 }
   4728 
   4729 /* Append s2 to s1, then free s2. */
   4730 buffer *
   4731 cat_strings(buffer *s1, buffer *s2)
   4732 {
   4733 
   4734 	if ((s1->data != NULL) != (s2->data != NULL)) {
   4735 		/* cannot concatenate wide and regular string literals */
   4736 		error(292);
   4737 		return s1;
   4738 	}
   4739 
   4740 	if (s1->data != NULL) {
   4741 		while (s1->len + s2->len + 1 > s1->cap)
   4742 			s1->cap *= 2;
   4743 		s1->data = xrealloc(s1->data, s1->cap);
   4744 		memcpy(s1->data + s1->len, s2->data, s2->len + 1);
   4745 		free(s2->data);
   4746 	}
   4747 	s1->len += s2->len;
   4748 	free(s2);
   4749 
   4750 	return s1;
   4751 }
   4752 
   4753 
   4754 typedef struct stmt_expr {
   4755 	memory_pool se_mem;
   4756 	sym_t *se_sym;
   4757 	struct stmt_expr *se_enclosing;
   4758 } stmt_expr;
   4759 
   4760 static stmt_expr *stmt_exprs;
   4761 
   4762 void
   4763 begin_statement_expr(void)
   4764 {
   4765 	debug_enter();
   4766 
   4767 	stmt_expr *se = xmalloc(sizeof(*se));
   4768 	se->se_mem = expr_save_memory();
   4769 	se->se_sym = NULL;
   4770 	se->se_enclosing = stmt_exprs;
   4771 	stmt_exprs = se;
   4772 }
   4773 
   4774 void
   4775 do_statement_expr(tnode_t *tn)
   4776 {
   4777 	block_level--;
   4778 	mem_block_level--;
   4779 	stmt_exprs->se_sym = tn != NULL
   4780 	    ? mktempsym(block_dup_type(tn->tn_type))
   4781 	    : NULL;		/* after a syntax error */
   4782 	mem_block_level++;
   4783 	block_level++;
   4784 	/* '({ ... })' is a GCC extension */
   4785 	gnuism(320);
   4786 }
   4787 
   4788 tnode_t *
   4789 end_statement_expr(void)
   4790 {
   4791 	tnode_t *tn;
   4792 
   4793 	stmt_expr *se = stmt_exprs;
   4794 	if (se->se_sym == NULL) {
   4795 		tn = NULL;	/* after a syntax error */
   4796 		goto end;
   4797 	}
   4798 
   4799 	tn = build_name(se->se_sym, false);
   4800 	(void)expr_save_memory();	/* leak */
   4801 	expr_restore_memory(se->se_mem);
   4802 	stmt_exprs = se->se_enclosing;
   4803 	free(se);
   4804 
   4805 end:
   4806 	debug_leave();
   4807 	return tn;
   4808 }
   4809 
   4810 bool
   4811 in_statement_expr(void)
   4812 {
   4813 	return stmt_exprs != NULL;
   4814 }
   4815