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