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