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