Home | History | Annotate | Line # | Download | only in lint1
tree.c revision 1.647
      1 /*	$NetBSD: tree.c,v 1.647 2024/06/09 16:53:06 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.647 2024/06/09 16:53:06 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 check_lossy_floating_to_integer_conversion(
   3293     op_t op, int arg, const type_t *tp, const tnode_t *tn)
   3294 {
   3295 	long double x = tn->u.value.u.floating;
   3296 	integer_constraints ic = ic_any(tp);
   3297 	if (is_uinteger(tp->t_tspec)
   3298 	    ? x >= ic.umin && x <= ic.umax && x == (uint64_t)x
   3299 	    : x >= ic.smin && x <= ic.smax && x == (int64_t)x)
   3300 		return;
   3301 	if (op == FARG)
   3302 		/* lossy conversion of %Lg to '%s', arg #%d */
   3303 		warning(380, x, type_name(tp), arg);
   3304 	else
   3305 		/* lossy conversion of %Lg to '%s' */
   3306 		warning(381, x, type_name(tp));
   3307 }
   3308 
   3309 static void
   3310 convert_integer_from_floating(
   3311     op_t op, int arg, const type_t *tp, const tnode_t *tn)
   3312 {
   3313 
   3314 	if (op == CVT)
   3315 		/* cast from floating point '%s' to integer '%s' */
   3316 		query_message(2, type_name(tn->tn_type), type_name(tp));
   3317 	else
   3318 		/* implicit conversion from floating point '%s' to ... */
   3319 		query_message(1, type_name(tn->tn_type), type_name(tp));
   3320 	if (tn->tn_op == CON)
   3321 		check_lossy_floating_to_integer_conversion(op, arg, tp, tn);
   3322 }
   3323 
   3324 static bool
   3325 should_warn_about_prototype_conversion(tspec_t nt,
   3326 				       tspec_t ot, const tnode_t *ptn)
   3327 {
   3328 
   3329 	if (nt == ot)
   3330 		return false;
   3331 
   3332 	if (nt == ENUM && ot == INT)
   3333 		return false;
   3334 
   3335 	if (is_floating(nt) != is_floating(ot) ||
   3336 	    portable_rank_cmp(nt, ot) != 0) {
   3337 		/* representation and/or width change */
   3338 		if (!is_integer(ot))
   3339 			return true;
   3340 		/*
   3341 		 * XXX: Investigate whether this rule makes sense; see
   3342 		 * tests/usr.bin/xlint/lint1/platform_long.c.
   3343 		 */
   3344 		return portable_rank_cmp(ot, INT) > 0;
   3345 	}
   3346 
   3347 	if (!hflag)
   3348 		return false;
   3349 
   3350 	/*
   3351 	 * If the types differ only in sign and the argument has the same
   3352 	 * representation in both types, print no warning.
   3353 	 */
   3354 	if (ptn->tn_op == CON && is_integer(nt) &&
   3355 	    signed_type(nt) == signed_type(ot) &&
   3356 	    !msb(ptn->u.value.u.integer, ot))
   3357 		return false;
   3358 
   3359 	return true;
   3360 }
   3361 
   3362 /*
   3363  * Warn if a prototype causes a type conversion that is different from what
   3364  * would happen to the same argument in the absence of a prototype.  This
   3365  * check is intended for code that needs to stay compatible with pre-C90 C.
   3366  *
   3367  * Errors/warnings about illegal type combinations are already printed
   3368  * in check_assign_types_compatible().
   3369  */
   3370 static void
   3371 check_prototype_conversion(int arg, tspec_t nt, tspec_t ot, type_t *tp,
   3372 			   tnode_t *tn)
   3373 {
   3374 
   3375 	if (!is_arithmetic(nt) || !is_arithmetic(ot))
   3376 		return;
   3377 
   3378 	/*
   3379 	 * If the type of the formal parameter is char/short, a warning would
   3380 	 * be useless, because functions declared the old style can't expect
   3381 	 * char/short arguments.
   3382 	 */
   3383 	if (nt == CHAR || nt == SCHAR || nt == UCHAR ||
   3384 	    nt == SHORT || nt == USHORT)
   3385 		return;
   3386 
   3387 	tnode_t *ptn = promote(NOOP, true, tn);
   3388 	ot = ptn->tn_type->t_tspec;
   3389 
   3390 	if (should_warn_about_prototype_conversion(nt, ot, ptn)) {
   3391 		/* argument %d is converted from '%s' to '%s' ... */
   3392 		warning(259, arg, type_name(tn->tn_type), type_name(tp));
   3393 	}
   3394 }
   3395 
   3396 /*
   3397  * When converting a large integer type to a small integer type, in some
   3398  * cases the value of the actual expression is further restricted than the
   3399  * type bounds, such as in (expr & 0xFF) or (expr % 100) or (expr >> 24).
   3400  */
   3401 static bool
   3402 can_represent(const type_t *tp, const tnode_t *tn)
   3403 {
   3404 
   3405 	debug_step("%s: type '%s'", __func__, type_name(tp));
   3406 	debug_node(tn);
   3407 
   3408 	uint64_t nmask = value_bits(width_in_bits(tp));
   3409 	if (!is_uinteger(tp->t_tspec))
   3410 		nmask >>= 1;
   3411 
   3412 	integer_constraints c = ic_expr(tn);
   3413 	if ((~c.bclr & ~nmask) == 0)
   3414 		return true;
   3415 
   3416 	integer_constraints tpc = ic_any(tp);
   3417 	if (is_uinteger(tp->t_tspec)
   3418 	    ? tpc.umin <= c.umin && tpc.umax >= c.umax
   3419 	    : tpc.smin <= c.smin && tpc.smax >= c.smax)
   3420 		return true;
   3421 
   3422 	return false;
   3423 }
   3424 
   3425 static bool
   3426 should_warn_about_integer_conversion(const type_t *ntp, tspec_t nt,
   3427 				     const tnode_t *otn, tspec_t ot)
   3428 {
   3429 
   3430 	// XXX: The portable_rank_cmp aims at portable mode, independent of the
   3431 	// current platform, while can_represent acts on the actual type sizes
   3432 	// from the current platform.  This mix is inconsistent, but anything
   3433 	// else would make the exact conditions too complicated to grasp.
   3434 	if (aflag > 0 && portable_rank_cmp(nt, ot) < 0) {
   3435 		if (ot == LONG || ot == ULONG
   3436 		    || ot == LLONG || ot == ULLONG
   3437 #ifdef INT128_SIZE
   3438 		    || ot == INT128 || ot == UINT128
   3439 #endif
   3440 		    || aflag > 1)
   3441 			return !can_represent(ntp, otn);
   3442 	}
   3443 	return false;
   3444 }
   3445 
   3446 static void
   3447 convert_integer_from_integer(op_t op, int arg, tspec_t nt, tspec_t ot,
   3448 			     type_t *tp, tnode_t *tn)
   3449 {
   3450 
   3451 	if (tn->tn_op == CON)
   3452 		return;
   3453 
   3454 	if (op == CVT)
   3455 		return;
   3456 
   3457 	if (Pflag && pflag && aflag > 0 &&
   3458 	    portable_rank_cmp(nt, ot) > 0 &&
   3459 	    is_uinteger(nt) != is_uinteger(ot)) {
   3460 		if (op == FARG)
   3461 			/* conversion to '%s' may sign-extend ... */
   3462 			warning(297, type_name(tp), arg);
   3463 		else
   3464 			/* conversion to '%s' may sign-extend ... */
   3465 			warning(131, type_name(tp));
   3466 	}
   3467 
   3468 	if (Pflag && portable_rank_cmp(nt, ot) > 0 &&
   3469 	    (tn->tn_op == PLUS || tn->tn_op == MINUS || tn->tn_op == MULT ||
   3470 	     tn->tn_op == SHL)) {
   3471 		/* suggest cast from '%s' to '%s' on op '%s' to ... */
   3472 		warning(324, type_name(gettyp(ot)), type_name(tp),
   3473 		    op_name(tn->tn_op));
   3474 	}
   3475 
   3476 	if (should_warn_about_integer_conversion(tp, nt, tn, ot)) {
   3477 		if (op == FARG) {
   3478 			/* conversion from '%s' to '%s' may lose ... */
   3479 			warning(298,
   3480 			    type_name(tn->tn_type), type_name(tp), arg);
   3481 		} else {
   3482 			/* conversion from '%s' to '%s' may lose accuracy */
   3483 			warning(132,
   3484 			    type_name(tn->tn_type), type_name(tp));
   3485 		}
   3486 	}
   3487 
   3488 	if (any_query_enabled && is_uinteger(nt) != is_uinteger(ot))
   3489 		/* implicit conversion changes sign from '%s' to '%s' */
   3490 		query_message(3, type_name(tn->tn_type), type_name(tp));
   3491 }
   3492 
   3493 static void
   3494 convert_integer_from_pointer(op_t op, tspec_t nt, type_t *tp, tnode_t *tn)
   3495 {
   3496 
   3497 	if (tn->tn_op == CON)
   3498 		return;
   3499 	if (op != CVT)
   3500 		return;		/* We already got an error. */
   3501 	if (portable_rank_cmp(nt, PTR) >= 0)
   3502 		return;
   3503 
   3504 	if (pflag && size_in_bits(nt) >= size_in_bits(PTR)) {
   3505 		/* conversion of pointer to '%s' may lose bits */
   3506 		warning(134, type_name(tp));
   3507 	} else {
   3508 		/* conversion of pointer to '%s' loses bits */
   3509 		warning(133, type_name(tp));
   3510 	}
   3511 }
   3512 
   3513 static bool
   3514 struct_starts_with(const type_t *struct_tp, const type_t *member_tp)
   3515 {
   3516 
   3517 	return struct_tp->u.sou->sou_first_member != NULL &&
   3518 	    types_compatible(struct_tp->u.sou->sou_first_member->s_type,
   3519 		member_tp, true, false, NULL);
   3520 }
   3521 
   3522 static bool
   3523 is_byte_array(const type_t *tp)
   3524 {
   3525 
   3526 	return tp->t_tspec == ARRAY &&
   3527 	    (tp->t_subt->t_tspec == CHAR || tp->t_subt->t_tspec == UCHAR);
   3528 }
   3529 
   3530 static bool
   3531 union_contains(const type_t *utp, const type_t *mtp)
   3532 {
   3533 	for (const sym_t *mem = utp->u.sou->sou_first_member;
   3534 	    mem != NULL; mem = mem->s_next) {
   3535 		if (types_compatible(mem->s_type, mtp, true, false, NULL))
   3536 			return true;
   3537 	}
   3538 	return false;
   3539 }
   3540 
   3541 static bool
   3542 should_warn_about_pointer_cast(const type_t *nstp, tspec_t nst,
   3543 			       const type_t *ostp, tspec_t ost)
   3544 {
   3545 
   3546 	while (nst == ARRAY)
   3547 		nstp = nstp->t_subt, nst = nstp->t_tspec;
   3548 	while (ost == ARRAY)
   3549 		ostp = ostp->t_subt, ost = ostp->t_tspec;
   3550 
   3551 	if (nst == STRUCT && ost == STRUCT &&
   3552 	    (struct_starts_with(nstp, ostp) ||
   3553 	     struct_starts_with(ostp, nstp)))
   3554 		return false;
   3555 
   3556 	if (is_incomplete(nstp) || is_incomplete(ostp))
   3557 		return false;
   3558 
   3559 	if (nst == CHAR || nst == UCHAR)
   3560 		return false;	/* for the sake of traditional C code */
   3561 	if (ost == CHAR || ost == UCHAR)
   3562 		return false;	/* for the sake of traditional C code */
   3563 
   3564 	/* Allow cast between pointers to sockaddr variants. */
   3565 	if (nst == STRUCT && ost == STRUCT) {
   3566 		const sym_t *nmem = nstp->u.sou->sou_first_member;
   3567 		const sym_t *omem = ostp->u.sou->sou_first_member;
   3568 		while (nmem != NULL && omem != NULL &&
   3569 		    types_compatible(nmem->s_type, omem->s_type,
   3570 			true, false, NULL))
   3571 			nmem = nmem->s_next, omem = omem->s_next;
   3572 		if (nmem != NULL && is_byte_array(nmem->s_type))
   3573 			return false;
   3574 		if (omem != NULL && is_byte_array(omem->s_type))
   3575 			return false;
   3576 		if (nmem == NULL && omem == NULL)
   3577 			return false;
   3578 	}
   3579 
   3580 	if (nst == UNION || ost == UNION) {
   3581 		const type_t *union_tp = nst == UNION ? nstp : ostp;
   3582 		const type_t *other_tp = nst == UNION ? ostp : nstp;
   3583 		if (union_contains(union_tp, other_tp))
   3584 			return false;
   3585 	}
   3586 
   3587 	if (is_struct_or_union(nst) && is_struct_or_union(ost))
   3588 		return nstp->u.sou != ostp->u.sou;
   3589 
   3590 	enum rank_kind rk1 = type_properties(nst)->tt_rank_kind;
   3591 	enum rank_kind rk2 = type_properties(ost)->tt_rank_kind;
   3592 	if (rk1 != rk2 || rk1 == RK_NONE)
   3593 		return true;
   3594 
   3595 	return portable_rank_cmp(nst, ost) != 0;
   3596 }
   3597 
   3598 static void
   3599 convert_pointer_from_pointer(type_t *ntp, tnode_t *tn)
   3600 {
   3601 	const type_t *nstp = ntp->t_subt;
   3602 	const type_t *otp = tn->tn_type;
   3603 	const type_t *ostp = otp->t_subt;
   3604 	tspec_t nst = nstp->t_tspec;
   3605 	tspec_t ost = ostp->t_tspec;
   3606 
   3607 	if (nst == VOID || ost == VOID) {
   3608 		/* TODO: C99 behaves like C90 here. */
   3609 		if (!allow_trad && !allow_c99 && (nst == FUNC || ost == FUNC)) {
   3610 			const char *nts, *ots;
   3611 			/* null pointers are already handled in convert() */
   3612 			*(nst == FUNC ? &nts : &ots) = "function pointer";
   3613 			*(nst == VOID ? &nts : &ots) = "'void *'";
   3614 			/* conversion of %s to %s requires a cast */
   3615 			warning(303, ots, nts);
   3616 		}
   3617 		return;
   3618 	}
   3619 	if (nst == FUNC && ost == FUNC)
   3620 		return;
   3621 	if (nst == FUNC || ost == FUNC) {
   3622 		/* converting '%s' to '%s' is questionable */
   3623 		warning(229, type_name(otp), type_name(ntp));
   3624 		return;
   3625 	}
   3626 
   3627 	if (hflag && alignment(nstp) > alignment(ostp) &&
   3628 	    ost != CHAR && ost != UCHAR &&
   3629 	    !is_incomplete(ostp) &&
   3630 	    !(nst == UNION && union_contains(nstp, ostp))) {
   3631 		/* converting '%s' to '%s' increases alignment ... */
   3632 		warning(135, type_name(otp), type_name(ntp),
   3633 		    alignment(ostp), alignment(nstp));
   3634 	}
   3635 
   3636 	if (cflag && should_warn_about_pointer_cast(nstp, nst, ostp, ost)) {
   3637 		/* pointer cast from '%s' to '%s' may be troublesome */
   3638 		warning(247, type_name(otp), type_name(ntp));
   3639 	}
   3640 }
   3641 
   3642 /*
   3643  * Insert a conversion operator, which converts the type of the node
   3644  * to another given type.
   3645  *
   3646  * Possible values for 'op':
   3647  *	CVT	a cast-expression
   3648  *	binary	integer promotion for one of the operands, or a usual
   3649  *		arithmetic conversion
   3650  *	binary	plain or compound assignments to bit-fields
   3651  *	FARG	'arg' is the number of the parameter (used for warnings)
   3652  *	NOOP	several other implicit conversions
   3653  *	...
   3654  */
   3655 tnode_t *
   3656 convert(op_t op, int arg, type_t *tp, tnode_t *tn)
   3657 {
   3658 	tspec_t nt = tp->t_tspec;
   3659 	tspec_t ot = tn->tn_type->t_tspec;
   3660 
   3661 	if (allow_trad && allow_c90 && op == FARG)
   3662 		check_prototype_conversion(arg, nt, ot, tp, tn);
   3663 
   3664 	if (nt == BOOL) {
   3665 		/* No further checks. */
   3666 
   3667 	} else if (is_integer(nt)) {
   3668 		if (ot == BOOL) {
   3669 			/* No further checks. */
   3670 		} else if (is_integer(ot))
   3671 			convert_integer_from_integer(op, arg, nt, ot, tp, tn);
   3672 		else if (is_floating(ot))
   3673 			convert_integer_from_floating(op, arg, tp, tn);
   3674 		else if (ot == PTR)
   3675 			convert_integer_from_pointer(op, nt, tp, tn);
   3676 
   3677 	} else if (is_floating(nt)) {
   3678 		if (is_integer(ot) && op != CVT) {
   3679 			/* implicit conversion from integer '%s' to ... */
   3680 			query_message(19,
   3681 			    type_name(tn->tn_type), type_name(tp));
   3682 		}
   3683 
   3684 	} else if (nt == PTR) {
   3685 		if (is_null_pointer(tn)) {
   3686 			/* a null pointer may be assigned to any pointer. */
   3687 		} else if (ot == PTR && op == CVT)
   3688 			convert_pointer_from_pointer(tp, tn);
   3689 	}
   3690 
   3691 	tnode_t *ntn = expr_alloc_tnode();
   3692 	ntn->tn_op = CVT;
   3693 	ntn->tn_type = tp;
   3694 	ntn->tn_cast = op == CVT;
   3695 	ntn->tn_sys |= tn->tn_sys;
   3696 	ntn->u.ops.right = NULL;
   3697 	if (tn->tn_op != CON || nt == VOID) {
   3698 		ntn->u.ops.left = tn;
   3699 	} else {
   3700 		ntn->tn_op = CON;
   3701 		convert_constant(op, arg, ntn->tn_type, &ntn->u.value,
   3702 		    &tn->u.value);
   3703 	}
   3704 
   3705 	return ntn;
   3706 }
   3707 
   3708 static void
   3709 convert_constant_from_floating(op_t op, int arg, const type_t *ntp,
   3710 			       tspec_t nt, val_t *nv, val_t *ov)
   3711 {
   3712 	long double max = 0.0, min = 0.0;
   3713 
   3714 	switch (nt) {
   3715 	case CHAR:
   3716 		max = TARG_CHAR_MAX;	min = TARG_CHAR_MIN;	break;
   3717 	case UCHAR:
   3718 		max = TARG_UCHAR_MAX;	min = 0;		break;
   3719 	case SCHAR:
   3720 		max = TARG_SCHAR_MAX;	min = TARG_SCHAR_MIN;	break;
   3721 	case SHORT:
   3722 		max = TARG_SHRT_MAX;	min = TARG_SHRT_MIN;	break;
   3723 	case USHORT:
   3724 		max = TARG_USHRT_MAX;	min = 0;		break;
   3725 	case ENUM:
   3726 	case INT:
   3727 		max = TARG_INT_MAX;	min = TARG_INT_MIN;	break;
   3728 	case UINT:
   3729 		max = TARG_UINT_MAX;	min = 0;		break;
   3730 	case LONG:
   3731 		max = TARG_LONG_MAX;	min = TARG_LONG_MIN;	break;
   3732 	case ULONG:
   3733 		max = TARG_ULONG_MAX;	min = 0;		break;
   3734 	case LLONG:
   3735 		max = LLONG_MAX;	min = LLONG_MIN;	break;
   3736 	case ULLONG:
   3737 		max = ULLONG_MAX;	min = 0;		break;
   3738 	case FLOAT:
   3739 	case FCOMPLEX:
   3740 		max = FLT_MAX;		min = -FLT_MAX;		break;
   3741 	case DOUBLE:
   3742 	case DCOMPLEX:
   3743 		max = DBL_MAX;		min = -DBL_MAX;		break;
   3744 	case LDOUBLE:
   3745 	case LCOMPLEX:
   3746 		/* LINTED 248; see floating_error_value. */
   3747 		max = LDBL_MAX;		min = -max;		break;
   3748 	default:
   3749 		lint_assert(/*CONSTCOND*/false);
   3750 	}
   3751 	if (ov->u.floating > max || ov->u.floating < min) {
   3752 		lint_assert(nt != LDOUBLE);
   3753 		const char *ot_name = type_name(gettyp(ov->v_tspec));
   3754 		const char *nt_name = type_name(ntp);
   3755 		if (is_integer(nt))
   3756 			goto after_warning;
   3757 		if (op == FARG)
   3758 			/* conversion of '%s' to '%s' is out of range, ... */
   3759 			warning(295, ot_name, nt_name, arg);
   3760 		else
   3761 			/* conversion of '%s' to '%s' is out of range */
   3762 			warning(119, ot_name, nt_name);
   3763 	after_warning:
   3764 		ov->u.floating = ov->u.floating > 0 ? max : min;
   3765 	}
   3766 
   3767 	if (nt == FLOAT || nt == FCOMPLEX)
   3768 		nv->u.floating = (float)ov->u.floating;
   3769 	else if (nt == DOUBLE || nt == DCOMPLEX)
   3770 		nv->u.floating = (double)ov->u.floating;
   3771 	else if (nt == LDOUBLE || nt == LCOMPLEX)
   3772 		nv->u.floating = ov->u.floating;
   3773 	else
   3774 		nv->u.integer = (int64_t)ov->u.floating;
   3775 }
   3776 
   3777 static bool
   3778 convert_constant_to_floating(tspec_t nt, val_t *nv,
   3779 			     tspec_t ot, const val_t *v)
   3780 {
   3781 	if (nt == FLOAT) {
   3782 		nv->u.floating = (ot == PTR || is_uinteger(ot)) ?
   3783 		    (float)(uint64_t)v->u.integer : (float)v->u.integer;
   3784 	} else if (nt == DOUBLE) {
   3785 		nv->u.floating = (ot == PTR || is_uinteger(ot)) ?
   3786 		    (double)(uint64_t)v->u.integer : (double)v->u.integer;
   3787 	} else if (nt == LDOUBLE) {
   3788 		nv->u.floating = (ot == PTR || is_uinteger(ot))
   3789 		    ? (long double)(uint64_t)v->u.integer
   3790 		    : (long double)v->u.integer;
   3791 	} else
   3792 		return false;
   3793 	return true;
   3794 }
   3795 
   3796 /*
   3797  * Print a warning if bits which were set are lost due to the conversion.
   3798  * This can happen with operator ORASS only.
   3799  */
   3800 static void
   3801 convert_constant_check_range_bitor(size_t nsz, size_t osz, const val_t *v,
   3802 				   uint64_t xmask, op_t op)
   3803 {
   3804 	if (nsz < osz && (v->u.integer & xmask) != 0)
   3805 		/* constant truncated by conversion, op '%s' */
   3806 		warning(306, op_name(op));
   3807 }
   3808 
   3809 static void
   3810 convert_constant_check_range_bitand(size_t nsz, size_t osz,
   3811 				    uint64_t xmask, const val_t *nv,
   3812 				    tspec_t ot, const val_t *v,
   3813 				    const type_t *tp, op_t op)
   3814 {
   3815 	if (nsz > osz &&
   3816 	    (nv->u.integer & bit((unsigned int)(osz - 1))) != 0 &&
   3817 	    (nv->u.integer & xmask) != xmask) {
   3818 		/* extra bits set to 0 in conversion of '%s' to '%s', ... */
   3819 		warning(309, type_name(gettyp(ot)),
   3820 		    type_name(tp), op_name(op));
   3821 	} else if (nsz < osz &&
   3822 	    (v->u.integer & xmask) != xmask &&
   3823 	    (v->u.integer & xmask) != 0)
   3824 		/* constant truncated by conversion, op '%s' */
   3825 		warning(306, op_name(op));
   3826 }
   3827 
   3828 static void
   3829 convert_constant_check_range_signed(op_t op, int arg,
   3830 				    const type_t *ntp, int64_t ov)
   3831 {
   3832 	if (op == ASSIGN)
   3833 		/* assignment of negative constant %lld to unsigned ... */
   3834 		warning(164, (long long)ov, type_name(ntp));
   3835 	else if (op == INIT)
   3836 		/* initialization of unsigned type '%s' with negative ... */
   3837 		warning(221, type_name(ntp), (long long)ov);
   3838 	else if (op == FARG)
   3839 		/* conversion of negative constant %lld to unsigned ... */
   3840 		warning(296, (long long)ov, type_name(ntp), arg);
   3841 	else if (modtab[op].m_comparison) {
   3842 		/* handled by check_integer_comparison() */
   3843 	} else
   3844 		/* conversion of negative constant %lld to unsigned ... */
   3845 		warning(222, (long long)ov, type_name(ntp));
   3846 }
   3847 
   3848 /*
   3849  * Loss of significant bit(s). All truncated bits of unsigned types or all
   3850  * truncated bits plus the msb of the target for signed types are considered
   3851  * to be significant bits. Loss of significant bits means that at least one
   3852  * of the bits was set in an unsigned type or that at least one but not all
   3853  * of the bits was set in a signed type. Loss of significant bits means that
   3854  * it is not possible, also not with necessary casts, to convert back to the
   3855  * original type. An example for a necessary cast is:
   3856  *	char c;	int	i; c = 128;
   3857  *	i = c;			** yields -128 **
   3858  *	i = (unsigned char)c;	** yields 128 **
   3859  */
   3860 static void
   3861 warn_constant_check_range_truncated(op_t op, int arg, const type_t *tp,
   3862 				    tspec_t ot)
   3863 {
   3864 	if (op == ASSIGN && tp->t_bitfield)
   3865 		/* precision lost in bit-field assignment */
   3866 		warning(166);
   3867 	else if (op == ASSIGN)
   3868 		/* constant truncated by assignment */
   3869 		warning(165);
   3870 	else if (op == INIT && tp->t_bitfield)
   3871 		/* bit-field initializer does not fit */
   3872 		warning(180);
   3873 	else if (op == INIT)
   3874 		/* initializer does not fit */
   3875 		warning(178);
   3876 	else if (op == CASE)
   3877 		/* case label affected by conversion */
   3878 		warning(196);
   3879 	else if (op == FARG)
   3880 		/* conversion of '%s' to '%s' is out of range, arg #%d */
   3881 		warning(295, type_name(gettyp(ot)), type_name(tp), arg);
   3882 	else
   3883 		/* conversion of '%s' to '%s' is out of range */
   3884 		warning(119, type_name(gettyp(ot)), type_name(tp));
   3885 }
   3886 
   3887 static void
   3888 warn_constant_check_range_loss(op_t op, int arg, const type_t *tp,
   3889 				  tspec_t ot)
   3890 {
   3891 	if (op == ASSIGN && tp->t_bitfield)
   3892 		/* precision lost in bit-field assignment */
   3893 		warning(166);
   3894 	else if (op == INIT && tp->t_bitfield)
   3895 		/* bit-field initializer out of range */
   3896 		warning(11);
   3897 	else if (op == CASE)
   3898 		/* case label affected by conversion */
   3899 		warning(196);
   3900 	else if (op == FARG)
   3901 		/* conversion of '%s' to '%s' is out of range, arg #%d */
   3902 		warning(295, type_name(gettyp(ot)), type_name(tp), arg);
   3903 	else
   3904 		/* conversion of '%s' to '%s' is out of range */
   3905 		warning(119, type_name(gettyp(ot)), type_name(tp));
   3906 }
   3907 
   3908 static void
   3909 convert_constant_check_range(tspec_t ot, const type_t *tp, tspec_t nt,
   3910 			     op_t op, int arg, const val_t *v, val_t *nv)
   3911 {
   3912 	unsigned int obitsz, nbitsz;
   3913 	uint64_t xmask, xmsk1;
   3914 
   3915 	obitsz = size_in_bits(ot);
   3916 	nbitsz = tp->t_bitfield ? tp->t_bit_field_width : size_in_bits(nt);
   3917 	xmask = value_bits(nbitsz) ^ value_bits(obitsz);
   3918 	xmsk1 = value_bits(nbitsz) ^ value_bits(obitsz - 1);
   3919 	if (op == ORASS || op == BITOR || op == BITXOR) {
   3920 		convert_constant_check_range_bitor(
   3921 		    nbitsz, obitsz, v, xmask, op);
   3922 	} else if (op == ANDASS || op == BITAND) {
   3923 		convert_constant_check_range_bitand(
   3924 		    nbitsz, obitsz, xmask, nv, ot, v, tp, op);
   3925 	} else if (nt != PTR && is_uinteger(nt) &&
   3926 	    ot != PTR && !is_uinteger(ot) &&
   3927 	    v->u.integer < 0)
   3928 		convert_constant_check_range_signed(op, arg,
   3929 		    tp, v->u.integer);
   3930 	else if (nv->u.integer != v->u.integer && nbitsz <= obitsz &&
   3931 	    (v->u.integer & xmask) != 0 &&
   3932 	    (is_uinteger(ot) || (v->u.integer & xmsk1) != xmsk1))
   3933 		warn_constant_check_range_truncated(op, arg, tp, ot);
   3934 	else if (nv->u.integer != v->u.integer)
   3935 		warn_constant_check_range_loss(op, arg, tp, ot);
   3936 }
   3937 
   3938 /* Converts a typed constant to a constant of another type. */
   3939 void
   3940 convert_constant(op_t op, int arg, const type_t *ntp, val_t *nv, val_t *ov)
   3941 {
   3942 	/*
   3943 	 * TODO: make 'ov' const; the name of this function does not suggest
   3944 	 *  that it modifies 'ov'.
   3945 	 */
   3946 	tspec_t ot = ov->v_tspec;
   3947 	tspec_t nt = nv->v_tspec = ntp->t_tspec;
   3948 	bool range_check = false;
   3949 
   3950 	if (nt == BOOL) {	/* C99 6.3.1.2 */
   3951 		nv->v_unsigned_since_c90 = false;
   3952 		nv->u.integer = is_nonzero_val(ov) ? 1 : 0;
   3953 		return;
   3954 	}
   3955 
   3956 	if (ot == FLOAT || ot == DOUBLE || ot == LDOUBLE)
   3957 		convert_constant_from_floating(op, arg, ntp, nt, nv, ov);
   3958 	else if (!convert_constant_to_floating(nt, nv, ot, ov)) {
   3959 		range_check = true;	/* Check for lost precision. */
   3960 		nv->u.integer = ov->u.integer;
   3961 	}
   3962 
   3963 	if (allow_trad && allow_c90 && ov->v_unsigned_since_c90 &&
   3964 	    (is_floating(nt) || (
   3965 		(is_integer(nt) && !is_uinteger(nt) &&
   3966 		    portable_rank_cmp(nt, ot) > 0)))) {
   3967 		/* C90 treats constant as unsigned */
   3968 		warning(157);
   3969 		ov->v_unsigned_since_c90 = false;
   3970 	}
   3971 
   3972 	if (is_integer(nt)) {
   3973 		unsigned int size = ntp->t_bitfield
   3974 		    ? ntp->t_bit_field_width : size_in_bits(nt);
   3975 		nv->u.integer = convert_integer(nv->u.integer, nt, size);
   3976 	}
   3977 
   3978 	if (range_check && op != CVT)
   3979 		convert_constant_check_range(ot, ntp, nt, op, arg, ov, nv);
   3980 }
   3981 
   3982 tnode_t *
   3983 build_sizeof(const type_t *tp)
   3984 {
   3985 	unsigned int size_in_bytes = type_size_in_bits(tp) / CHAR_SIZE;
   3986 	tnode_t *tn = build_integer_constant(SIZEOF_TSPEC, size_in_bytes);
   3987 	tn->tn_system_dependent = true;
   3988 	debug_step("build_sizeof '%s' = %u", type_name(tp), size_in_bytes);
   3989 	return tn;
   3990 }
   3991 
   3992 tnode_t *
   3993 build_offsetof(const type_t *tp, designation dn)
   3994 {
   3995 	unsigned int offset_in_bits = 0;
   3996 
   3997 	if (!is_struct_or_union(tp->t_tspec)) {
   3998 		/* unacceptable operand of '%s' */
   3999 		error(111, "offsetof");
   4000 		goto proceed;
   4001 	}
   4002 	for (size_t i = 0; i < dn.dn_len; i++) {
   4003 		const designator *dr = dn.dn_items + i;
   4004 		if (dr->dr_kind == DK_SUBSCRIPT) {
   4005 			if (tp->t_tspec != ARRAY)
   4006 				goto proceed;	/* silent error */
   4007 			tp = tp->t_subt;
   4008 			offset_in_bits += (unsigned) dr->dr_subscript
   4009 			    * type_size_in_bits(tp);
   4010 		} else {
   4011 			if (!is_struct_or_union(tp->t_tspec))
   4012 				goto proceed;	/* silent error */
   4013 			const char *name = dr->dr_member->s_name;
   4014 			sym_t *mem = find_member(tp->u.sou, name);
   4015 			if (mem == NULL) {
   4016 				/* type '%s' does not have member '%s' */
   4017 				error(101, name, type_name(tp));
   4018 				goto proceed;
   4019 			}
   4020 			tp = mem->s_type;
   4021 			offset_in_bits += mem->u.s_member.sm_offset_in_bits;
   4022 		}
   4023 	}
   4024 	free(dn.dn_items);
   4025 
   4026 proceed:;
   4027 	unsigned int offset_in_bytes = offset_in_bits / CHAR_SIZE;
   4028 	tnode_t *tn = build_integer_constant(SIZEOF_TSPEC, offset_in_bytes);
   4029 	tn->tn_system_dependent = true;
   4030 	return tn;
   4031 }
   4032 
   4033 unsigned int
   4034 type_size_in_bits(const type_t *tp)
   4035 {
   4036 
   4037 	unsigned int elem = 1;
   4038 	bool flex = false;
   4039 	lint_assert(tp != NULL);
   4040 	while (tp->t_tspec == ARRAY) {
   4041 		flex = true;	/* allow c99 flex arrays [] [0] */
   4042 		elem *= tp->u.dimension;
   4043 		tp = tp->t_subt;
   4044 	}
   4045 	if (elem == 0 && !flex) {
   4046 		/* cannot take size/alignment of incomplete type */
   4047 		error(143);
   4048 		elem = 1;
   4049 	}
   4050 
   4051 	unsigned int elsz;
   4052 	switch (tp->t_tspec) {
   4053 	case VOID:
   4054 		/* cannot take size/alignment of void */
   4055 		error(146);
   4056 		elsz = 1;
   4057 		break;
   4058 	case FUNC:
   4059 		/* cannot take size/alignment of function type '%s' */
   4060 		error(144, type_name(tp));
   4061 		elsz = 1;
   4062 		break;
   4063 	case STRUCT:
   4064 	case UNION:
   4065 		if (is_incomplete(tp)) {
   4066 			/* cannot take size/alignment of incomplete type */
   4067 			error(143);
   4068 			elsz = 1;
   4069 		} else
   4070 			elsz = tp->u.sou->sou_size_in_bits;
   4071 		break;
   4072 	case ENUM:
   4073 		if (is_incomplete(tp)) {
   4074 			/* cannot take size/alignment of incomplete type */
   4075 			warning(143);
   4076 		}
   4077 		/* FALLTHROUGH */
   4078 	default:
   4079 		if (tp->t_bitfield)
   4080 			/* cannot take size/alignment of bit-field */
   4081 			error(145);
   4082 		elsz = size_in_bits(tp->t_tspec);
   4083 		lint_assert(elsz > 0);
   4084 		break;
   4085 	}
   4086 
   4087 	return elem * elsz;
   4088 }
   4089 
   4090 /* C11 6.5.3.4, GCC */
   4091 tnode_t *
   4092 build_alignof(const type_t *tp)
   4093 {
   4094 	if (tp->t_tspec == FUNC) {
   4095 		/* cannot take size/alignment of function type '%s' */
   4096 		error(144, type_name(tp));
   4097 		return NULL;
   4098 	}
   4099 	if (tp->t_tspec == VOID) {
   4100 		/* cannot take size/alignment of void */
   4101 		error(146);
   4102 		return NULL;
   4103 	}
   4104 	if (is_incomplete(tp)) {
   4105 		/* cannot take size/alignment of incomplete type */
   4106 		error(143);
   4107 		return NULL;
   4108 	}
   4109 	if (tp->t_bitfield) {
   4110 		/* cannot take size/alignment of bit-field */
   4111 		error(145);
   4112 		return NULL;
   4113 	}
   4114 	return build_integer_constant(SIZEOF_TSPEC, (int64_t)alignment(tp));
   4115 }
   4116 
   4117 static tnode_t *
   4118 cast_to_union(tnode_t *otn, bool sys, type_t *ntp)
   4119 {
   4120 
   4121 	if (!allow_gcc) {
   4122 		/* union cast is a GCC extension */
   4123 		error(328);
   4124 		return NULL;
   4125 	}
   4126 
   4127 	for (const sym_t *m = ntp->u.sou->sou_first_member;
   4128 	    m != NULL; m = m->s_next) {
   4129 		if (types_compatible(m->s_type, otn->tn_type,
   4130 		    false, false, NULL)) {
   4131 			tnode_t *ntn = build_op(CVT, sys, ntp, otn, NULL);
   4132 			ntn->tn_cast = true;
   4133 			return ntn;
   4134 		}
   4135 	}
   4136 
   4137 	/* type '%s' is not a member of '%s' */
   4138 	error(329, type_name(otn->tn_type), type_name(ntp));
   4139 	return NULL;
   4140 }
   4141 
   4142 // In GCC mode, allow 'nullptr + offset' as a constant expression.
   4143 static tnode_t *
   4144 null_pointer_offset(tnode_t *tn)
   4145 {
   4146 	uint64_t off = 0;
   4147 	const tnode_t *n = tn;
   4148 	while ((n->tn_op == PLUS || n->tn_op == MINUS)
   4149 	    && is_integer(n->u.ops.right->tn_type->t_tspec)) {
   4150 		off += (uint64_t)n->u.ops.right->u.value.u.integer;
   4151 		n = n->u.ops.left;
   4152 	}
   4153 	if (n->tn_type->t_tspec == PTR
   4154 	    && n->tn_op == ADDR
   4155 	    && n->u.ops.left->tn_op == INDIR
   4156 	    && n->u.ops.left->u.ops.left->tn_op == CON
   4157 	    && n->u.ops.left->u.ops.left->tn_type->t_tspec == PTR) {
   4158 		off += (uint64_t)n->u.ops.left->u.ops.left->u.value.u.integer;
   4159 		return build_integer_constant(SIZEOF_TSPEC, (int64_t)off);
   4160 	}
   4161 	return tn;
   4162 }
   4163 
   4164 tnode_t *
   4165 cast(tnode_t *tn, bool sys, type_t *tp)
   4166 {
   4167 
   4168 	if (tn == NULL)
   4169 		return NULL;
   4170 
   4171 	tn = cconv(tn);
   4172 
   4173 	lint_assert(tp != NULL);
   4174 	tspec_t nt = tp->t_tspec;
   4175 	tspec_t ot = tn->tn_type->t_tspec;
   4176 
   4177 	if (nt == VOID) {
   4178 		/*
   4179 		 * C90 6.3.4, C99 6.5.4p2 and C11 6.5.4p2 allow any type to be
   4180 		 * cast to void.  The only other allowed casts are from a
   4181 		 * scalar type to a scalar type.
   4182 		 */
   4183 	} else if (nt == UNION)
   4184 		return cast_to_union(tn, sys, tp);
   4185 	else if (nt == STRUCT || nt == ARRAY || nt == FUNC) {
   4186 		/* Casting to a struct is an undocumented GCC extension. */
   4187 		if (!(allow_gcc && nt == STRUCT))
   4188 			goto invalid_cast;
   4189 	} else if (is_struct_or_union(ot))
   4190 		goto invalid_cast;
   4191 	else if (ot == VOID) {
   4192 		/* improper cast of void expression */
   4193 		error(148);
   4194 		return NULL;
   4195 	} else if (is_integer(nt) && is_scalar(ot)) {
   4196 		tn = null_pointer_offset(tn);
   4197 	} else if (is_floating(nt) && is_arithmetic(ot)) {
   4198 		/* ok */
   4199 	} else if (nt == PTR && is_integer(ot)) {
   4200 		/* ok */
   4201 	} else if (nt == PTR && ot == PTR) {
   4202 		if (!tp->t_subt->t_const && tn->tn_type->t_subt->t_const) {
   4203 			if (hflag)
   4204 				/* cast discards 'const' from type '%s' */
   4205 				warning(275, type_name(tn->tn_type));
   4206 		}
   4207 	} else
   4208 		goto invalid_cast;
   4209 
   4210 	if (any_query_enabled
   4211 	    && types_compatible(tp, tn->tn_type, false, false, NULL))
   4212 		/* no-op cast from '%s' to '%s' */
   4213 		query_message(6, type_name(tn->tn_type), type_name(tp));
   4214 
   4215 	tn = convert(CVT, 0, tp, tn);
   4216 	tn->tn_cast = true;
   4217 	tn->tn_sys = sys;
   4218 
   4219 	return tn;
   4220 
   4221 invalid_cast:
   4222 	/* invalid cast from '%s' to '%s' */
   4223 	error(147, type_name(tn->tn_type), type_name(tp));
   4224 	return NULL;
   4225 }
   4226 
   4227 void
   4228 add_function_argument(function_call *call, tnode_t *arg)
   4229 {
   4230 	/*
   4231 	 * If there was a serious error in the expression for the argument,
   4232 	 * create a dummy argument so the positions of the remaining arguments
   4233 	 * will not change.
   4234 	 */
   4235 	if (arg == NULL)
   4236 		arg = build_integer_constant(INT, 0);
   4237 
   4238 	if (call->args_len >= call->args_cap) {
   4239 		call->args_cap += 8;
   4240 		tnode_t **new_args = expr_zero_alloc(
   4241 		    call->args_cap * sizeof(*call->args), "tnode*[]");
   4242 		if (call->args_len > 0)
   4243 			memcpy(new_args, call->args,
   4244 			    call->args_len * sizeof(*call->args));
   4245 		call->args = new_args;
   4246 	}
   4247 	call->args[call->args_len++] = arg;
   4248 }
   4249 
   4250 /*
   4251  * Compare the type of an argument with the corresponding type of a
   4252  * prototype parameter. If it is a valid combination, but both types
   4253  * are not the same, insert a conversion to convert the argument into
   4254  * the type of the parameter.
   4255  */
   4256 static tnode_t *
   4257 check_prototype_argument(
   4258 	int n,		/* pos of arg */
   4259 	type_t *tp,		/* expected type (from prototype) */
   4260 	tnode_t *tn)		/* argument */
   4261 {
   4262 	tnode_t *ln = xcalloc(1, sizeof(*ln));
   4263 	ln->tn_type = expr_unqualified_type(tp);
   4264 	ln->tn_lvalue = true;
   4265 	if (typeok(FARG, n, ln, tn)) {
   4266 		bool dowarn;
   4267 		if (!types_compatible(tp, tn->tn_type,
   4268 		    true, false, (dowarn = false, &dowarn)) || dowarn)
   4269 			tn = convert(FARG, n, tp, tn);
   4270 	}
   4271 	free(ln);
   4272 	return tn;
   4273 }
   4274 
   4275 /*
   4276  * Check types of all function arguments and insert conversions,
   4277  * if necessary.
   4278  */
   4279 static void
   4280 check_function_arguments(const function_call *call)
   4281 {
   4282 	type_t *ftp = call->func->tn_type->t_subt;
   4283 
   4284 	/* get # of parameters in the prototype */
   4285 	int npar = 0;
   4286 	for (const sym_t *p = ftp->u.params; p != NULL; p = p->s_next)
   4287 		npar++;
   4288 
   4289 	int narg = (int)call->args_len;
   4290 
   4291 	const sym_t *param = ftp->u.params;
   4292 	if (ftp->t_proto && npar != narg && !(ftp->t_vararg && npar < narg)) {
   4293 		/* argument mismatch: %d %s passed, %d expected */
   4294 		error(150, narg, narg != 1 ? "arguments" : "argument", npar);
   4295 		param = NULL;
   4296 	}
   4297 
   4298 	for (int i = 0; i < narg; i++) {
   4299 		tnode_t *arg = call->args[i];
   4300 
   4301 		/* some things which are always not allowed */
   4302 		tspec_t at = arg->tn_type->t_tspec;
   4303 		if (at == VOID) {
   4304 			/* void expressions may not be arguments, arg #%d */
   4305 			error(151, i + 1);
   4306 			return;
   4307 		}
   4308 		if (is_struct_or_union(at) && is_incomplete(arg->tn_type)) {
   4309 			/* argument cannot have unknown size, arg #%d */
   4310 			error(152, i + 1);
   4311 			return;
   4312 		}
   4313 		if (is_integer(at) &&
   4314 		    arg->tn_type->t_is_enum &&
   4315 		    is_incomplete(arg->tn_type)) {
   4316 			/* argument cannot have unknown size, arg #%d */
   4317 			warning(152, i + 1);
   4318 		}
   4319 
   4320 		arg = cconv(arg);
   4321 		call->args[i] = arg;
   4322 
   4323 		arg = param != NULL
   4324 		    ? check_prototype_argument(i + 1, param->s_type, arg)
   4325 		    : promote(NOOP, true, arg);
   4326 		call->args[i] = arg;
   4327 
   4328 		if (param != NULL)
   4329 			param = param->s_next;
   4330 	}
   4331 }
   4332 
   4333 tnode_t *
   4334 build_function_call(tnode_t *func, bool sys, function_call *call)
   4335 {
   4336 
   4337 	if (func == NULL)
   4338 		return NULL;
   4339 
   4340 	call->func = func;
   4341 	check_ctype_function_call(call);
   4342 
   4343 	func = cconv(func);
   4344 	call->func = func;
   4345 
   4346 	if (func->tn_type->t_tspec != PTR ||
   4347 	    func->tn_type->t_subt->t_tspec != FUNC) {
   4348 		/* cannot call '%s', must be a function */
   4349 		error(149, type_name(func->tn_type));
   4350 		return NULL;
   4351 	}
   4352 
   4353 	check_function_arguments(call);
   4354 
   4355 	tnode_t *ntn = expr_alloc_tnode();
   4356 	ntn->tn_op = CALL;
   4357 	ntn->tn_type = func->tn_type->t_subt->t_subt;
   4358 	ntn->tn_sys = sys;
   4359 	ntn->u.call = call;
   4360 	return ntn;
   4361 }
   4362 
   4363 /*
   4364  * Return the value of an integral constant expression.
   4365  * If the expression is not constant or its type is not an integer
   4366  * type, an error message is printed.
   4367  */
   4368 val_t *
   4369 integer_constant(tnode_t *tn, bool required)
   4370 {
   4371 
   4372 	if (tn != NULL)
   4373 		tn = cconv(tn);
   4374 	if (tn != NULL)
   4375 		tn = promote(NOOP, false, tn);
   4376 
   4377 	val_t *v = xcalloc(1, sizeof(*v));
   4378 
   4379 	if (tn == NULL) {
   4380 		lint_assert(seen_error);
   4381 		debug_step("constant node is null; returning 1 instead");
   4382 		v->v_tspec = INT;
   4383 		v->u.integer = 1;
   4384 		return v;
   4385 	}
   4386 
   4387 	v->v_tspec = tn->tn_type->t_tspec;
   4388 
   4389 	if (tn->tn_op == CON) {
   4390 		lint_assert(tn->tn_type->t_tspec == tn->u.value.v_tspec);
   4391 		if (is_integer(tn->u.value.v_tspec)) {
   4392 			v->v_unsigned_since_c90 =
   4393 			    tn->u.value.v_unsigned_since_c90;
   4394 			v->u.integer = tn->u.value.u.integer;
   4395 			return v;
   4396 		}
   4397 		v->u.integer = (int64_t)tn->u.value.u.floating;
   4398 	} else
   4399 		v->u.integer = 1;
   4400 
   4401 	if (required)
   4402 		/* integral constant expression expected */
   4403 		error(55);
   4404 	else
   4405 		/* variable array dimension is a C99/GCC extension */
   4406 		c99ism(318);
   4407 
   4408 	if (!is_integer(v->v_tspec))
   4409 		v->v_tspec = INT;
   4410 
   4411 	return v;
   4412 }
   4413 
   4414 static bool
   4415 is_constcond_false(const tnode_t *tn, tspec_t t)
   4416 {
   4417 	return (t == BOOL || t == INT) &&
   4418 	    tn->tn_op == CON && tn->u.value.u.integer == 0;
   4419 }
   4420 
   4421 /*
   4422  * Perform some tests on expressions which can't be done in build_binary()
   4423  * and functions called by build_binary(). These tests must be done here
   4424  * because we need some information about the context in which the operations
   4425  * are performed.
   4426  * After all tests are performed and dofreeblk is true, expr() frees the
   4427  * memory which is used for the expression.
   4428  */
   4429 void
   4430 expr(tnode_t *tn, bool vctx, bool cond, bool dofreeblk, bool is_do_while)
   4431 {
   4432 
   4433 	if (tn == NULL) {	/* in case of errors */
   4434 		expr_free_all();
   4435 		return;
   4436 	}
   4437 
   4438 	/* expr() is also called in global initializations */
   4439 	if (dcs->d_kind != DLK_EXTERN && !is_do_while)
   4440 		check_statement_reachable();
   4441 
   4442 	check_expr_misc(tn, vctx, cond, !cond, false, false, false);
   4443 	if (tn->tn_op == ASSIGN && !tn->tn_parenthesized) {
   4444 		if (hflag && cond)
   4445 			/* assignment in conditional context */
   4446 			warning(159);
   4447 	} else if (tn->tn_op == CON) {
   4448 		if (hflag && cond && !suppress_constcond &&
   4449 		    !tn->tn_system_dependent &&
   4450 		    !(is_do_while &&
   4451 		      is_constcond_false(tn, tn->tn_type->t_tspec)))
   4452 			/* constant in conditional context */
   4453 			warning(161);
   4454 	}
   4455 	if (!modtab[tn->tn_op].m_has_side_effect) {
   4456 		/*
   4457 		 * for left operands of COMMA this warning is already printed
   4458 		 */
   4459 		if (tn->tn_op != COMMA && !vctx && !cond)
   4460 			check_null_effect(tn);
   4461 	}
   4462 	debug_node(tn);
   4463 
   4464 	if (dofreeblk)
   4465 		expr_free_all();
   4466 }
   4467 
   4468 /* If the expression has the form '*(arr + idx)', check the array index. */
   4469 static void
   4470 check_array_index(const tnode_t *indir, bool taking_address)
   4471 {
   4472 	const tnode_t *plus, *arr, *idx;
   4473 
   4474 	if (indir->tn_op == INDIR
   4475 	    && (plus = indir->u.ops.left, plus->tn_op == PLUS)
   4476 	    && plus->u.ops.left->tn_op == ADDR
   4477 	    && (arr = plus->u.ops.left->u.ops.left, true)
   4478 	    && (arr->tn_op == STRING || arr->tn_op == NAME)
   4479 	    && arr->tn_type->t_tspec == ARRAY
   4480 	    && (idx = plus->u.ops.right, idx->tn_op == CON)
   4481 	    && (!is_incomplete(arr->tn_type) || idx->u.value.u.integer < 0))
   4482 		goto proceed;
   4483 	return;
   4484 
   4485 proceed:;
   4486 	int elsz = length_in_bits(arr->tn_type->t_subt, NULL);
   4487 	if (elsz == 0)
   4488 		return;
   4489 	elsz /= CHAR_SIZE;
   4490 
   4491 	/* Change the unit of the index from bytes to element size. */
   4492 	int64_t con = is_uinteger(idx->tn_type->t_tspec)
   4493 	    ? (int64_t)((uint64_t)idx->u.value.u.integer / elsz)
   4494 	    : idx->u.value.u.integer / elsz;
   4495 
   4496 	int dim = arr->tn_type->u.dimension + (taking_address ? 1 : 0);
   4497 
   4498 	if (!is_uinteger(idx->tn_type->t_tspec) && con < 0)
   4499 		/* array subscript %jd cannot be negative */
   4500 		warning(167, (intmax_t)con);
   4501 	else if (dim > 0 && (uint64_t)con >= (uint64_t)dim)
   4502 		/* array subscript %ju cannot be > %d */
   4503 		warning(168, (uintmax_t)con, dim - 1);
   4504 }
   4505 
   4506 static void
   4507 check_expr_addr(const tnode_t *ln, bool szof, bool fcall)
   4508 {
   4509 	/* XXX: Taking warn_about_unreachable into account here feels wrong. */
   4510 	if (ln->tn_op == NAME && (reached || !warn_about_unreachable)) {
   4511 		if (!szof)
   4512 			mark_as_set(ln->u.sym);
   4513 		mark_as_used(ln->u.sym, fcall, szof);
   4514 	}
   4515 	check_array_index(ln, true);
   4516 }
   4517 
   4518 /*
   4519  * If there is an asm statement in one of the compound statements around,
   4520  * there may be other side effects, so don't warn.
   4521  */
   4522 static bool
   4523 is_asm_around(void)
   4524 {
   4525 	for (decl_level *dl = dcs; dl != NULL; dl = dl->d_enclosing)
   4526 		if (dl->d_asm)
   4527 			return true;
   4528 	return false;
   4529 }
   4530 
   4531 static void
   4532 check_expr_side_effect(const tnode_t *ln, bool szof)
   4533 {
   4534 
   4535 	/* XXX: Taking warn_about_unreachable into account here feels wrong. */
   4536 	if (ln->tn_op == NAME && (reached || !warn_about_unreachable)) {
   4537 		scl_t sc = ln->u.sym->s_scl;
   4538 		if (sc != EXTERN && sc != STATIC &&
   4539 		    !ln->u.sym->s_set && !szof && !is_asm_around()) {
   4540 			/* '%s' may be used before set */
   4541 			warning(158, ln->u.sym->s_name);
   4542 			mark_as_set(ln->u.sym);
   4543 		}
   4544 		mark_as_used(ln->u.sym, false, false);
   4545 	}
   4546 }
   4547 
   4548 static void
   4549 check_expr_assign(const tnode_t *ln, bool szof)
   4550 {
   4551 	/* XXX: Taking warn_about_unreachable into account here feels wrong. */
   4552 	if (ln->tn_op == NAME && !szof && (reached || !warn_about_unreachable)) {
   4553 		mark_as_set(ln->u.sym);
   4554 		if (ln->u.sym->s_scl == EXTERN)
   4555 			outusg(ln->u.sym);
   4556 	}
   4557 	check_array_index(ln, false);
   4558 }
   4559 
   4560 static void
   4561 check_expr_call(const tnode_t *tn, const tnode_t *ln,
   4562 		bool szof, bool vctx, bool cond, bool retval_discarded)
   4563 {
   4564 	lint_assert(ln->tn_op == ADDR);
   4565 	lint_assert(ln->u.ops.left->tn_op == NAME);
   4566 	if (!szof && !is_compiler_builtin(ln->u.ops.left->u.sym->s_name))
   4567 		outcall(tn, vctx || cond, retval_discarded);
   4568 
   4569 	const function_call *call = tn->u.call;
   4570 	if (call->args_len == 4 || call->args_len == 5)
   4571 		check_snprintb(call);
   4572 }
   4573 
   4574 static void
   4575 check_expr_op(op_t op, const tnode_t *ln, bool szof, bool fcall, bool eqwarn)
   4576 {
   4577 	switch (op) {
   4578 	case ADDR:
   4579 		check_expr_addr(ln, szof, fcall);
   4580 		break;
   4581 	case LOAD:
   4582 		check_array_index(ln, false);
   4583 		/* FALLTHROUGH */
   4584 	case INCBEF:
   4585 	case DECBEF:
   4586 	case INCAFT:
   4587 	case DECAFT:
   4588 	case ADDASS:
   4589 	case SUBASS:
   4590 	case MULASS:
   4591 	case DIVASS:
   4592 	case MODASS:
   4593 	case ANDASS:
   4594 	case ORASS:
   4595 	case XORASS:
   4596 	case SHLASS:
   4597 	case SHRASS:
   4598 	case REAL:
   4599 	case IMAG:
   4600 		check_expr_side_effect(ln, szof);
   4601 		break;
   4602 	case ASSIGN:
   4603 		check_expr_assign(ln, szof);
   4604 		break;
   4605 	case EQ:
   4606 		if (hflag && eqwarn)
   4607 			/* operator '==' found where '=' was expected */
   4608 			warning(160);
   4609 		break;
   4610 	default:
   4611 		break;
   4612 	}
   4613 }
   4614 
   4615 /*
   4616  *	vctx			???
   4617  *	cond			whether the expression is a condition that
   4618  *				will be compared with 0
   4619  *	eqwarn			whether the operator '==' might be a
   4620  *				misspelled '='
   4621  *	fcall			whether the expression is a function call
   4622  *	retval_discarded	whether the return value of a function call
   4623  *				is discarded; such calls will be analyzed by
   4624  *				lint2 in messages 4, 8 and 9
   4625  *	szof			whether the expression is part of a sizeof
   4626  *				expression, which means that its value is
   4627  *				discarded since only the type is relevant
   4628  */
   4629 void
   4630 check_expr_misc(const tnode_t *tn, bool vctx, bool cond,
   4631 		bool eqwarn, bool fcall, bool retval_discarded, bool szof)
   4632 {
   4633 
   4634 	if (tn == NULL)
   4635 		return;
   4636 	op_t op = tn->tn_op;
   4637 	if (op == NAME || op == CON || op == STRING)
   4638 		return;
   4639 	bool is_direct = op == CALL
   4640 	    && tn->u.call->func->tn_op == ADDR
   4641 	    && tn->u.call->func->u.ops.left->tn_op == NAME;
   4642 	if (op == CALL) {
   4643 		const function_call *call = tn->u.call;
   4644 		if (is_direct)
   4645 			check_expr_call(tn, call->func,
   4646 			    szof, vctx, cond, retval_discarded);
   4647 		bool discard = op == CVT && tn->tn_type->t_tspec == VOID;
   4648 		check_expr_misc(call->func, false, false, false, is_direct,
   4649 		    discard, szof);
   4650 		for (size_t i = 0, n = call->args_len; i < n; i++)
   4651 			check_expr_misc(call->args[i],
   4652 			    true, false, false, false, false, szof);
   4653 		return;
   4654 	}
   4655 
   4656 	lint_assert(has_operands(tn));
   4657 	tnode_t *ln = tn->u.ops.left;
   4658 	tnode_t *rn = tn->u.ops.right;
   4659 	check_expr_op(op, ln, szof, fcall, eqwarn);
   4660 
   4661 	const mod_t *mp = &modtab[op];
   4662 	bool cvctx = mp->m_value_context;
   4663 	bool ccond = mp->m_compares_with_zero;
   4664 	bool eq = mp->m_warn_if_operand_eq &&
   4665 	    !ln->tn_parenthesized &&
   4666 	    rn != NULL && !rn->tn_parenthesized;
   4667 
   4668 	/*
   4669 	 * Values of operands of ':' are not used if the type of at least
   4670 	 * one of the operands (for GCC compatibility) is 'void'.
   4671 	 *
   4672 	 * XXX test/value context of QUEST should probably be used as
   4673 	 * context for both operands of COLON.
   4674 	 */
   4675 	if (op == COLON && tn->tn_type->t_tspec == VOID)
   4676 		cvctx = ccond = false;
   4677 	bool discard = op == CVT && tn->tn_type->t_tspec == VOID;
   4678 	check_expr_misc(ln, cvctx, ccond, eq, is_direct, discard, szof);
   4679 
   4680 	switch (op) {
   4681 	case LOGAND:
   4682 	case LOGOR:
   4683 		check_expr_misc(rn, false, true, eq, false, false, szof);
   4684 		break;
   4685 	case COLON:
   4686 		check_expr_misc(rn, cvctx, ccond, eq, false, false, szof);
   4687 		break;
   4688 	case COMMA:
   4689 		check_expr_misc(rn, vctx, cond, false, false, false, szof);
   4690 		break;
   4691 	default:
   4692 		if (mp->m_binary)
   4693 			check_expr_misc(rn, true, false, eq, false, false,
   4694 			    szof);
   4695 		break;
   4696 	}
   4697 }
   4698 
   4699 /*
   4700  * Return whether the expression can be used for static initialization.
   4701  *
   4702  * Constant initialization expressions must be constant or an address
   4703  * of a static object with an optional offset. In the first case,
   4704  * the result is returned in *offsp. In the second case, the static
   4705  * object is returned in *symp and the offset in *offsp.
   4706  *
   4707  * The expression can consist of PLUS, MINUS, ADDR, NAME, STRING and
   4708  * CON. Type conversions are allowed if they do not change binary
   4709  * representation (including width).
   4710  *
   4711  * C99 6.6 "Constant expressions"
   4712  * C99 6.7.8p4 restricts initializers for static storage duration
   4713  */
   4714 bool
   4715 constant_addr(const tnode_t *tn, const sym_t **symp, ptrdiff_t *offsp)
   4716 {
   4717 	const sym_t *sym;
   4718 	ptrdiff_t offs1, offs2;
   4719 	tspec_t t, ot;
   4720 
   4721 	switch (tn->tn_op) {
   4722 	case MINUS:
   4723 		if (tn->u.ops.right->tn_op == CVT)
   4724 			return constant_addr(tn->u.ops.right, symp, offsp);
   4725 		else if (tn->u.ops.right->tn_op != CON)
   4726 			return false;
   4727 		/* FALLTHROUGH */
   4728 	case PLUS:
   4729 		offs1 = offs2 = 0;
   4730 		if (tn->u.ops.left->tn_op == CON) {
   4731 			offs1 = (ptrdiff_t)tn->u.ops.left->u.value.u.integer;
   4732 			if (!constant_addr(tn->u.ops.right, &sym, &offs2))
   4733 				return false;
   4734 		} else if (tn->u.ops.right->tn_op == CON) {
   4735 			offs2 = (ptrdiff_t)tn->u.ops.right->u.value.u.integer;
   4736 			if (tn->tn_op == MINUS)
   4737 				offs2 = -offs2;
   4738 			if (!constant_addr(tn->u.ops.left, &sym, &offs1))
   4739 				return false;
   4740 		} else {
   4741 			return false;
   4742 		}
   4743 		*symp = sym;
   4744 		*offsp = offs1 + offs2;
   4745 		return true;
   4746 	case ADDR:
   4747 		if (tn->u.ops.left->tn_op == NAME) {
   4748 			*symp = tn->u.ops.left->u.sym;
   4749 			*offsp = 0;
   4750 			return true;
   4751 		} else {
   4752 			/*
   4753 			 * If this were the front end of a compiler, we would
   4754 			 * return a label instead of 0, at least if
   4755 			 * 'tn->u.ops.left->tn_op == STRING'.
   4756 			 */
   4757 			*symp = NULL;
   4758 			*offsp = 0;
   4759 			return true;
   4760 		}
   4761 	case CVT:
   4762 		t = tn->tn_type->t_tspec;
   4763 		ot = tn->u.ops.left->tn_type->t_tspec;
   4764 		if ((!is_integer(t) && t != PTR) ||
   4765 		    (!is_integer(ot) && ot != PTR)) {
   4766 			return false;
   4767 		}
   4768 #if 0
   4769 		/*-
   4770 		 * consider:
   4771 		 *	struct foo {
   4772 		 *		unsigned char a;
   4773 		 *	} f = {
   4774 		 *		(unsigned char)(unsigned long)
   4775 		 *		    (&(((struct foo *)0)->a))
   4776 		 *	};
   4777 		 * since psize(unsigned long) != psize(unsigned char),
   4778 		 * this fails.
   4779 		 */
   4780 		else if (psize(t) != psize(ot))
   4781 			return -1;
   4782 #endif
   4783 		return constant_addr(tn->u.ops.left, symp, offsp);
   4784 	default:
   4785 		return false;
   4786 	}
   4787 }
   4788 
   4789 /* Append s2 to s1, then free s2. */
   4790 buffer *
   4791 cat_strings(buffer *s1, buffer *s2)
   4792 {
   4793 
   4794 	if ((s1->data != NULL) != (s2->data != NULL)) {
   4795 		/* cannot concatenate wide and regular string literals */
   4796 		error(292);
   4797 		return s1;
   4798 	}
   4799 
   4800 	if (s1->data != NULL) {
   4801 		while (s1->len + s2->len + 1 > s1->cap)
   4802 			s1->cap *= 2;
   4803 		s1->data = xrealloc(s1->data, s1->cap);
   4804 		memcpy(s1->data + s1->len, s2->data, s2->len + 1);
   4805 		free(s2->data);
   4806 	}
   4807 	s1->len += s2->len;
   4808 	free(s2);
   4809 
   4810 	return s1;
   4811 }
   4812 
   4813 
   4814 typedef struct stmt_expr {
   4815 	memory_pool se_mem;
   4816 	sym_t *se_sym;
   4817 	struct stmt_expr *se_enclosing;
   4818 } stmt_expr;
   4819 
   4820 static stmt_expr *stmt_exprs;
   4821 
   4822 void
   4823 begin_statement_expr(void)
   4824 {
   4825 	debug_enter();
   4826 
   4827 	stmt_expr *se = xmalloc(sizeof(*se));
   4828 	se->se_mem = expr_save_memory();
   4829 	se->se_sym = NULL;
   4830 	se->se_enclosing = stmt_exprs;
   4831 	stmt_exprs = se;
   4832 }
   4833 
   4834 void
   4835 do_statement_expr(tnode_t *tn)
   4836 {
   4837 	block_level--;
   4838 	mem_block_level--;
   4839 	stmt_exprs->se_sym = tn != NULL
   4840 	    ? mktempsym(block_dup_type(tn->tn_type))
   4841 	    : NULL;		/* after a syntax error */
   4842 	mem_block_level++;
   4843 	block_level++;
   4844 	/* '({ ... })' is a GCC extension */
   4845 	gnuism(320);
   4846 }
   4847 
   4848 tnode_t *
   4849 end_statement_expr(void)
   4850 {
   4851 	tnode_t *tn;
   4852 
   4853 	stmt_expr *se = stmt_exprs;
   4854 	if (se->se_sym == NULL) {
   4855 		tn = NULL;	/* after a syntax error */
   4856 		goto end;
   4857 	}
   4858 
   4859 	tn = build_name(se->se_sym, false);
   4860 	(void)expr_save_memory();	/* leak */
   4861 	expr_restore_memory(se->se_mem);
   4862 	stmt_exprs = se->se_enclosing;
   4863 	free(se);
   4864 
   4865 end:
   4866 	debug_leave();
   4867 	return tn;
   4868 }
   4869 
   4870 bool
   4871 in_statement_expr(void)
   4872 {
   4873 	return stmt_exprs != NULL;
   4874 }
   4875