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