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