Home | History | Annotate | Line # | Download | only in lint1
tree.c revision 1.580
      1 /*	$NetBSD: tree.c,v 1.580 2023/09/12 22:01:05 rillig Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994, 1995 Jochen Pohl
      5  * All Rights Reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Jochen Pohl for
     18  *	The NetBSD Project.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #if HAVE_NBTOOL_CONFIG_H
     35 #include "nbtool_config.h"
     36 #endif
     37 
     38 #include <sys/cdefs.h>
     39 #if defined(__RCSID)
     40 __RCSID("$NetBSD: tree.c,v 1.580 2023/09/12 22:01:05 rillig Exp $");
     41 #endif
     42 
     43 #include <float.h>
     44 #include <limits.h>
     45 #include <math.h>
     46 #include <signal.h>
     47 #include <stdlib.h>
     48 #include <string.h>
     49 
     50 #include "lint1.h"
     51 
     52 
     53 typedef struct integer_constraints {
     54 	int64_t		smin;	/* signed minimum */
     55 	int64_t		smax;	/* signed maximum */
     56 	uint64_t	umin;	/* unsigned minimum */
     57 	uint64_t	umax;	/* unsigned maximum */
     58 	uint64_t	bset;	/* bits that are definitely set */
     59 	uint64_t	bclr;	/* bits that are definitely clear */
     60 } integer_constraints;
     61 
     62 
     63 static uint64_t
     64 u64_fill_right(uint64_t x)
     65 {
     66 	x |= x >> 1;
     67 	x |= x >> 2;
     68 	x |= x >> 4;
     69 	x |= x >> 8;
     70 	x |= x >> 16;
     71 	x |= x >> 32;
     72 	return x;
     73 }
     74 
     75 static bool
     76 str_ends_with(const char *haystack, const char *needle)
     77 {
     78 	size_t hlen = strlen(haystack);
     79 	size_t nlen = strlen(needle);
     80 
     81 	return nlen <= hlen &&
     82 	       memcmp(haystack + hlen - nlen, needle, nlen) == 0;
     83 }
     84 
     85 static unsigned
     86 width_in_bits(const type_t *tp)
     87 {
     88 
     89 	lint_assert(is_integer(tp->t_tspec));
     90 	return tp->t_bitfield
     91 	    ? tp->t_bit_field_width
     92 	    : size_in_bits(tp->t_tspec);
     93 }
     94 
     95 static int
     96 portable_rank_cmp(tspec_t t1, tspec_t t2) {
     97 	const ttab_t *p1 = type_properties(t1), *p2 = type_properties(t2);
     98 	lint_assert(p1->tt_rank_kind == p2->tt_rank_kind);
     99 	lint_assert(p1->tt_rank_value > 0);
    100 	lint_assert(p2->tt_rank_value > 0);
    101 	return (int)p1->tt_rank_value - (int)p2->tt_rank_value;
    102 }
    103 
    104 static bool
    105 ic_maybe_signed(const type_t *tp, const integer_constraints *ic)
    106 {
    107 	return !is_uinteger(tp->t_tspec) && ic->bclr >> 63 == 0;
    108 }
    109 
    110 static integer_constraints
    111 ic_any(const type_t *tp)
    112 {
    113 	integer_constraints c;
    114 
    115 	uint64_t vbits = value_bits(width_in_bits(tp));
    116 	if (is_uinteger(tp->t_tspec)) {
    117 		c.smin = INT64_MIN;
    118 		c.smax = INT64_MAX;
    119 		c.umin = 0;
    120 		c.umax = vbits;
    121 		c.bset = 0;
    122 		c.bclr = ~c.umax;
    123 	} else {
    124 		c.smin = (int64_t)-1 - (int64_t)(vbits >> 1);
    125 		c.smax = (int64_t)(vbits >> 1);
    126 		c.umin = 0;
    127 		c.umax = UINT64_MAX;
    128 		c.bset = 0;
    129 		c.bclr = 0;
    130 	}
    131 	return c;
    132 }
    133 
    134 static integer_constraints
    135 ic_con(const type_t *tp, const val_t *v)
    136 {
    137 	integer_constraints c;
    138 
    139 	lint_assert(is_integer(tp->t_tspec));
    140 	int64_t si = v->u.integer;
    141 	uint64_t ui = (uint64_t)si;
    142 	c.smin = si;
    143 	c.smax = si;
    144 	c.umin = ui;
    145 	c.umax = ui;
    146 	c.bset = ui;
    147 	c.bclr = ~ui;
    148 	return c;
    149 }
    150 
    151 static integer_constraints
    152 ic_cvt(const type_t *ntp, const type_t *otp, integer_constraints a)
    153 {
    154 	unsigned nw = width_in_bits(ntp);
    155 	unsigned ow = width_in_bits(otp);
    156 	bool nu = is_uinteger(ntp->t_tspec);
    157 	bool ou = is_uinteger(otp->t_tspec);
    158 
    159 	if (nw >= ow && nu == ou)
    160 		return a;
    161 	if (nw > ow && ou)
    162 		return a;
    163 	return ic_any(ntp);
    164 }
    165 
    166 static integer_constraints
    167 ic_bitand(integer_constraints a, integer_constraints b)
    168 {
    169 	integer_constraints c;
    170 
    171 	c.smin = INT64_MIN;
    172 	c.smax = INT64_MAX;
    173 	c.umin = 0;
    174 	c.umax = UINT64_MAX;
    175 	c.bset = a.bset & b.bset;
    176 	c.bclr = a.bclr | b.bclr;
    177 	return c;
    178 }
    179 
    180 static integer_constraints
    181 ic_bitor(integer_constraints a, integer_constraints b)
    182 {
    183 	integer_constraints c;
    184 
    185 	c.smin = INT64_MIN;
    186 	c.smax = INT64_MAX;
    187 	c.umin = 0;
    188 	c.umax = UINT64_MAX;
    189 	c.bset = a.bset | b.bset;
    190 	c.bclr = a.bclr & b.bclr;
    191 	return c;
    192 }
    193 
    194 static integer_constraints
    195 ic_mod(const type_t *tp, integer_constraints a, integer_constraints b)
    196 {
    197 	integer_constraints c;
    198 
    199 	if (ic_maybe_signed(tp, &a) || ic_maybe_signed(tp, &b))
    200 		return ic_any(tp);
    201 
    202 	c.smin = INT64_MIN;
    203 	c.smax = INT64_MAX;
    204 	c.umin = 0;
    205 	c.umax = b.umax - 1;
    206 	c.bset = 0;
    207 	c.bclr = ~u64_fill_right(c.umax);
    208 	return c;
    209 }
    210 
    211 static integer_constraints
    212 ic_shl(const type_t *tp, integer_constraints a, integer_constraints b)
    213 {
    214 	integer_constraints c;
    215 	unsigned int amount;
    216 
    217 	if (ic_maybe_signed(tp, &a))
    218 		return ic_any(tp);
    219 
    220 	if (b.smin == b.smax && b.smin >= 0 && b.smin < 64)
    221 		amount = (unsigned int)b.smin;
    222 	else if (b.umin == b.umax && b.umin < 64)
    223 		amount = (unsigned int)b.umin;
    224 	else
    225 		return ic_any(tp);
    226 
    227 	c.smin = INT64_MIN;
    228 	c.smax = INT64_MAX;
    229 	c.umin = 0;
    230 	c.umax = UINT64_MAX;
    231 	c.bset = a.bset << amount;
    232 	c.bclr = a.bclr << amount | (((uint64_t)1 << amount) - 1);
    233 	return c;
    234 }
    235 
    236 static integer_constraints
    237 ic_shr(const type_t *tp, integer_constraints a, integer_constraints b)
    238 {
    239 	integer_constraints c;
    240 	unsigned int amount;
    241 
    242 	if (ic_maybe_signed(tp, &a))
    243 		return ic_any(tp);
    244 
    245 	if (b.smin == b.smax && b.smin >= 0 && b.smin < 64)
    246 		amount = (unsigned int)b.smin;
    247 	else if (b.umin == b.umax && b.umin < 64)
    248 		amount = (unsigned int)b.umin;
    249 	else
    250 		return ic_any(tp);
    251 
    252 	c.smin = INT64_MIN;
    253 	c.smax = INT64_MAX;
    254 	c.umin = 0;
    255 	c.umax = UINT64_MAX;
    256 	c.bset = a.bset >> amount;
    257 	c.bclr = a.bclr >> amount | ~(~(uint64_t)0 >> amount);
    258 	return c;
    259 }
    260 
    261 static integer_constraints
    262 ic_cond(integer_constraints a, integer_constraints b)
    263 {
    264 	integer_constraints c;
    265 
    266 	c.smin = a.smin < b.smin ? a.smin : b.smin;
    267 	c.smax = a.smax > b.smax ? a.smax : b.smax;
    268 	c.umin = a.umin < b.umin ? a.umin : b.umin;
    269 	c.umax = a.umax > b.umax ? a.umax : b.umax;
    270 	c.bset = a.bset | b.bset;
    271 	c.bclr = a.bclr & b.bclr;
    272 	return c;
    273 }
    274 
    275 static integer_constraints
    276 ic_expr(const tnode_t *tn)
    277 {
    278 	integer_constraints lc, rc;
    279 
    280 	lint_assert(is_integer(tn->tn_type->t_tspec));
    281 
    282 	switch (tn->tn_op) {
    283 	case CON:
    284 		return ic_con(tn->tn_type, &tn->tn_val);
    285 	case CVT:
    286 		if (!is_integer(tn->tn_left->tn_type->t_tspec))
    287 			return ic_any(tn->tn_type);
    288 		lc = ic_expr(tn->tn_left);
    289 		return ic_cvt(tn->tn_type, tn->tn_left->tn_type, lc);
    290 	case MOD:
    291 		lc = ic_expr(before_conversion(tn->tn_left));
    292 		rc = ic_expr(before_conversion(tn->tn_right));
    293 		return ic_mod(tn->tn_type, lc, rc);
    294 	case SHL:
    295 		lc = ic_expr(tn->tn_left);
    296 		rc = ic_expr(tn->tn_right);
    297 		return ic_shl(tn->tn_type, lc, rc);
    298 	case SHR:
    299 		lc = ic_expr(tn->tn_left);
    300 		rc = ic_expr(tn->tn_right);
    301 		return ic_shr(tn->tn_type, lc, rc);
    302 	case BITAND:
    303 		lc = ic_expr(tn->tn_left);
    304 		rc = ic_expr(tn->tn_right);
    305 		return ic_bitand(lc, rc);
    306 	case BITOR:
    307 		lc = ic_expr(tn->tn_left);
    308 		rc = ic_expr(tn->tn_right);
    309 		return ic_bitor(lc, rc);
    310 	case QUEST:
    311 		lc = ic_expr(tn->tn_right->tn_left);
    312 		rc = ic_expr(tn->tn_right->tn_right);
    313 		return ic_cond(lc, rc);
    314 	default:
    315 		return ic_any(tn->tn_type);
    316 	}
    317 }
    318 
    319 /* Build 'pointer to tp', 'array of tp' or 'function returning tp'. */
    320 type_t *
    321 block_derive_type(type_t *tp, tspec_t t)
    322 {
    323 	type_t *tp2;
    324 
    325 	tp2 = block_zero_alloc(sizeof(*tp2), "type");
    326 	tp2->t_tspec = t;
    327 	tp2->t_subt = tp;
    328 	return tp2;
    329 }
    330 
    331 /*
    332  * Derive 'pointer to tp' or 'function returning tp'.
    333  * The memory is freed at the end of the current expression.
    334  */
    335 type_t *
    336 expr_derive_type(type_t *tp, tspec_t t)
    337 {
    338 	type_t *tp2;
    339 
    340 	tp2 = expr_zero_alloc(sizeof(*tp2), "type");
    341 	tp2->t_tspec = t;
    342 	tp2->t_subt = tp;
    343 	return tp2;
    344 }
    345 
    346 /* Create an expression from a unary or binary operator and its operands. */
    347 static tnode_t *
    348 build_op(op_t op, bool sys, type_t *type, tnode_t *ln, tnode_t *rn)
    349 {
    350 
    351 	tnode_t *ntn = expr_alloc_tnode();
    352 	ntn->tn_op = op;
    353 	ntn->tn_type = type;
    354 	ntn->tn_sys = sys;
    355 	ntn->tn_left = ln;
    356 	ntn->tn_right = rn;
    357 
    358 	if (op == INDIR || op == FSEL) {
    359 		lint_assert(ln->tn_type->t_tspec == PTR);
    360 		tspec_t t = ln->tn_type->t_subt->t_tspec;
    361 		ntn->tn_lvalue = t != FUNC && t != VOID;
    362 	}
    363 
    364 	return ntn;
    365 }
    366 
    367 tnode_t *
    368 build_constant(type_t *tp, val_t *v)
    369 {
    370 
    371 	tnode_t *n = expr_alloc_tnode();
    372 	n->tn_op = CON;
    373 	n->tn_type = tp;
    374 	n->tn_val = *v;
    375 	n->tn_val.v_tspec = tp->t_tspec;
    376 	free(v);
    377 	return n;
    378 }
    379 
    380 static tnode_t *
    381 build_integer_constant(tspec_t t, int64_t si)
    382 {
    383 
    384 	tnode_t *n = expr_alloc_tnode();
    385 	n->tn_op = CON;
    386 	n->tn_type = gettyp(t);
    387 	n->tn_val.v_tspec = t;
    388 	n->tn_val.v_unsigned_since_c90 = false;
    389 	n->tn_val.v_char_constant = false;
    390 	n->tn_val.u.integer = si;
    391 	return n;
    392 }
    393 
    394 static void
    395 fallback_symbol(sym_t *sym)
    396 {
    397 
    398 	if (Tflag && fallback_symbol_strict_bool(sym))
    399 		return;
    400 
    401 	if (block_level > 0 && (strcmp(sym->s_name, "__FUNCTION__") == 0 ||
    402 			   strcmp(sym->s_name, "__PRETTY_FUNCTION__") == 0)) {
    403 		/* __FUNCTION__/__PRETTY_FUNCTION__ is a GCC extension */
    404 		gnuism(316);
    405 		// XXX: Should probably be ARRAY instead of PTR.
    406 		sym->s_type = block_derive_type(gettyp(CHAR), PTR);
    407 		sym->s_type->t_const = true;
    408 		return;
    409 	}
    410 
    411 	if (block_level > 0 && strcmp(sym->s_name, "__func__") == 0) {
    412 		if (!allow_c99)
    413 			/* __func__ is a C99 feature */
    414 			warning(317);
    415 		/* C11 6.4.2.2 */
    416 		sym->s_type = block_derive_type(gettyp(CHAR), ARRAY);
    417 		sym->s_type->t_const = true;
    418 		sym->s_type->t_dim = (int)strlen(funcsym->s_name) + 1;
    419 		return;
    420 	}
    421 
    422 	/* '%s' undefined */
    423 	error(99, sym->s_name);
    424 }
    425 
    426 /*
    427  * Functions that are predeclared by GCC or other compilers can be called
    428  * with arbitrary arguments.  Since lint usually runs after a successful
    429  * compilation, it's the compiler's job to catch any errors.
    430  */
    431 bool
    432 is_compiler_builtin(const char *name)
    433 {
    434 	/* https://gcc.gnu.org/onlinedocs/gcc/C-Extensions.html */
    435 	if (allow_gcc) {
    436 		if (strncmp(name, "__atomic_", 9) == 0 ||
    437 		    strncmp(name, "__builtin_", 10) == 0 ||
    438 		    strcmp(name, "alloca") == 0 ||
    439 		    /* obsolete but still in use, as of 2021 */
    440 		    strncmp(name, "__sync_", 7) == 0)
    441 			return true;
    442 	}
    443 
    444 	/* https://software.intel.com/sites/landingpage/IntrinsicsGuide/ */
    445 	if (strncmp(name, "_mm_", 4) == 0)
    446 		return true;
    447 
    448 	return false;
    449 }
    450 
    451 /* https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html */
    452 static bool
    453 is_gcc_bool_builtin(const char *name)
    454 {
    455 	return strncmp(name, "__builtin_", 10) == 0 &&
    456 	       (str_ends_with(name, "_overflow") ||
    457 		str_ends_with(name, "_overflow_p"));
    458 }
    459 
    460 static void
    461 build_name_call(sym_t *sym)
    462 {
    463 
    464 	if (is_compiler_builtin(sym->s_name)) {
    465 		/*
    466 		 * Do not warn about these, just assume that
    467 		 * they are regular functions compatible with
    468 		 * non-prototype calling conventions.
    469 		 */
    470 		if (allow_gcc && is_gcc_bool_builtin(sym->s_name))
    471 			sym->s_type = gettyp(BOOL);
    472 	} else if (allow_c99) {
    473 		/* function '%s' implicitly declared to return int */
    474 		error(215, sym->s_name);
    475 	} else if (!allow_trad) {
    476 		/* function '%s' implicitly declared to return int */
    477 		warning(215, sym->s_name);
    478 	}
    479 
    480 	/* XXX if !allow_c90, the symbol should be exported to level 0 */
    481 	sym->s_type = block_derive_type(sym->s_type, FUNC);
    482 }
    483 
    484 /* Create a node for a name (symbol table entry). */
    485 tnode_t *
    486 build_name(sym_t *sym, bool is_funcname)
    487 {
    488 
    489 	if (sym->s_scl == 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 C90; ... */
   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 operand 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 static void
   1639 use(const tnode_t *tn)
   1640 {
   1641 	if (tn == NULL)
   1642 		return;
   1643 	switch (tn->tn_op) {
   1644 	case NAME:
   1645 		mark_as_used(tn->tn_sym, false /* XXX */, false /* XXX */);
   1646 		break;
   1647 	case CON:
   1648 	case STRING:
   1649 		break;
   1650 	default:
   1651 		use(tn->tn_left);
   1652 		if (is_binary(tn) || tn->tn_op == PUSH)
   1653 			use(tn->tn_right);
   1654 	}
   1655 }
   1656 
   1657 /*
   1658  * Create a tree node for a binary operator and its two operands. Also called
   1659  * for unary operators; in that case rn is NULL.
   1660  *
   1661  * Function calls, sizeof and casts are handled elsewhere.
   1662  */
   1663 tnode_t *
   1664 build_binary(tnode_t *ln, op_t op, bool sys, tnode_t *rn)
   1665 {
   1666 	const mod_t *mp = &modtab[op];
   1667 
   1668 	/* If there was an error in one of the operands, return. */
   1669 	if (ln == NULL || (mp->m_binary && rn == NULL))
   1670 		return NULL;
   1671 
   1672 	/*
   1673 	 * Apply class conversions to the left operand, but only if its
   1674 	 * value is needed or compared with zero.
   1675 	 */
   1676 	if (mp->m_value_context || mp->m_compares_with_zero)
   1677 		ln = cconv(ln);
   1678 	/*
   1679 	 * The right operand is almost always in a test or value context,
   1680 	 * except if it is a struct or union member.
   1681 	 */
   1682 	if (mp->m_binary && op != ARROW && op != POINT)
   1683 		rn = cconv(rn);
   1684 
   1685 	/*
   1686 	 * Print some warnings for comparisons of unsigned values with
   1687 	 * constants lower than or equal to null. This must be done
   1688 	 * before promote() because otherwise unsigned char and unsigned
   1689 	 * short would be promoted to int. Types are also tested to be
   1690 	 * CHAR, which would also become int.
   1691 	 */
   1692 	if (mp->m_comparison)
   1693 		check_integer_comparison(op, ln, rn);
   1694 
   1695 	if (mp->m_value_context || mp->m_compares_with_zero)
   1696 		ln = promote(op, false, ln);
   1697 	if (mp->m_binary && op != ARROW && op != POINT &&
   1698 	    op != ASSIGN && op != RETURN && op != INIT) {
   1699 		rn = promote(op, false, rn);
   1700 	}
   1701 
   1702 	/*
   1703 	 * If the result of the operation is different for signed or
   1704 	 * unsigned operands and one of the operands is signed only in
   1705 	 * C90, print a warning.
   1706 	 */
   1707 	if (mp->m_warn_if_left_unsigned_in_c90 &&
   1708 	    ln->tn_op == CON && ln->tn_val.v_unsigned_since_c90) {
   1709 		/* C90 treats constant as unsigned, op '%s' */
   1710 		warning(218, op_name(op));
   1711 		ln->tn_val.v_unsigned_since_c90 = false;
   1712 	}
   1713 	if (mp->m_warn_if_right_unsigned_in_c90 &&
   1714 	    rn->tn_op == CON && rn->tn_val.v_unsigned_since_c90) {
   1715 		/* C90 treats constant as unsigned, op '%s' */
   1716 		warning(218, op_name(op));
   1717 		rn->tn_val.v_unsigned_since_c90 = false;
   1718 	}
   1719 
   1720 	/* Make sure both operands are of the same type */
   1721 	if (mp->m_balance_operands || (!allow_c90 && (op == SHL || op == SHR)))
   1722 		balance(op, &ln, &rn);
   1723 
   1724 	/*
   1725 	 * Check types for compatibility with the operation and mutual
   1726 	 * compatibility. Return if there are serious problems.
   1727 	 */
   1728 	if (!typeok(op, 0, ln, rn))
   1729 		return NULL;
   1730 
   1731 	/* And now create the node. */
   1732 	tnode_t *ntn;
   1733 	switch (op) {
   1734 	case POINT:
   1735 	case ARROW:
   1736 		ntn = build_struct_access(op, sys, ln, rn);
   1737 		break;
   1738 	case INCAFT:
   1739 	case DECAFT:
   1740 	case INCBEF:
   1741 	case DECBEF:
   1742 		ntn = build_prepost_incdec(op, sys, ln);
   1743 		break;
   1744 	case ADDR:
   1745 		ntn = build_address(sys, ln, false);
   1746 		break;
   1747 	case INDIR:
   1748 		ntn = build_op(INDIR, sys, ln->tn_type->t_subt, ln, NULL);
   1749 		break;
   1750 	case PLUS:
   1751 	case MINUS:
   1752 		ntn = build_plus_minus(op, sys, ln, rn);
   1753 		break;
   1754 	case SHL:
   1755 	case SHR:
   1756 		ntn = build_bit_shift(op, sys, ln, rn);
   1757 		break;
   1758 	case COLON:
   1759 		ntn = build_colon(sys, ln, rn);
   1760 		break;
   1761 	case ASSIGN:
   1762 	case MULASS:
   1763 	case DIVASS:
   1764 	case MODASS:
   1765 	case ADDASS:
   1766 	case SUBASS:
   1767 	case SHLASS:
   1768 	case SHRASS:
   1769 	case ANDASS:
   1770 	case XORASS:
   1771 	case ORASS:
   1772 	case RETURN:
   1773 	case INIT:
   1774 		ntn = build_assignment(op, sys, ln, rn);
   1775 		break;
   1776 	case COMMA:
   1777 		if (any_query_enabled) {
   1778 			/* comma operator with types '%s' and '%s' */
   1779 			query_message(12,
   1780 			    type_name(ln->tn_type), type_name(rn->tn_type));
   1781 		}
   1782 		/* FALLTHROUGH */
   1783 	case QUEST:
   1784 		ntn = build_op(op, sys, rn->tn_type, ln, rn);
   1785 		break;
   1786 	case REAL:
   1787 	case IMAG:
   1788 		ntn = build_real_imag(op, sys, ln);
   1789 		break;
   1790 	default:
   1791 		lint_assert(mp->m_binary == (rn != NULL));
   1792 		type_t *rettp = mp->m_returns_bool
   1793 		    ? gettyp(Tflag ? BOOL : INT) : ln->tn_type;
   1794 		ntn = build_op(op, sys, rettp, ln, rn);
   1795 		break;
   1796 	}
   1797 
   1798 	/* Return if an error occurred. */
   1799 	if (ntn == NULL)
   1800 		return NULL;
   1801 
   1802 	/* Print a warning if precedence confusion is possible */
   1803 	if (mp->m_possible_precedence_confusion)
   1804 		check_precedence_confusion(ntn);
   1805 
   1806 	/*
   1807 	 * Print a warning if one of the operands is in a context where
   1808 	 * it is compared with zero and if this operand is a constant.
   1809 	 */
   1810 	if (hflag && !suppress_constcond &&
   1811 	    mp->m_compares_with_zero &&
   1812 	    (ln->tn_op == CON ||
   1813 	     ((mp->m_binary && op != QUEST) && rn->tn_op == CON)) &&
   1814 	    /* XXX: rn->tn_system_dependent should be checked as well */
   1815 	    !ln->tn_system_dependent) {
   1816 		/* constant in conditional context */
   1817 		warning(161);
   1818 	}
   1819 
   1820 	/* Fold if the operator requires it */
   1821 	if (mp->m_fold_constant_operands) {
   1822 		if (ln->tn_op == CON && (!mp->m_binary || rn->tn_op == CON)) {
   1823 			if (mp->m_compares_with_zero) {
   1824 				ntn = fold_bool(ntn);
   1825 			} else if (is_floating(ntn->tn_type->t_tspec)) {
   1826 				ntn = fold_float(ntn);
   1827 			} else {
   1828 				ntn = fold(ntn);
   1829 			}
   1830 		} else if (op == QUEST && ln->tn_op == CON) {
   1831 			use(ln->tn_val.u.integer != 0
   1832 			    ? rn->tn_right : rn->tn_left);
   1833 			ntn = ln->tn_val.u.integer != 0
   1834 			    ? rn->tn_left : rn->tn_right;
   1835 		}
   1836 	}
   1837 
   1838 	return ntn;
   1839 }
   1840 
   1841 tnode_t *
   1842 build_unary(op_t op, bool sys, tnode_t *tn)
   1843 {
   1844 	return build_binary(tn, op, sys, NULL);
   1845 }
   1846 
   1847 static bool
   1848 are_members_compatible(const sym_t *a, const sym_t *b)
   1849 {
   1850 	if (a->u.s_member.sm_offset_in_bits != b->u.s_member.sm_offset_in_bits)
   1851 		return false;
   1852 
   1853 	const type_t *atp = a->s_type;
   1854 	const type_t *btp = b->s_type;
   1855 	bool w = false;
   1856 	if (!types_compatible(atp, btp, false, false, &w) && !w)
   1857 		return false;
   1858 	if (a->s_bitfield != b->s_bitfield)
   1859 		return false;
   1860 	if (a->s_bitfield) {
   1861 		if (atp->t_bit_field_width != btp->t_bit_field_width)
   1862 			return false;
   1863 		if (atp->t_bit_field_offset != btp->t_bit_field_offset)
   1864 			return false;
   1865 	}
   1866 	return true;
   1867 }
   1868 
   1869 /*
   1870  * Return whether all struct/union members with the same name have the same
   1871  * type and offset.
   1872  */
   1873 static bool
   1874 all_members_compatible(const sym_t *msym)
   1875 {
   1876 	for (const sym_t *csym = msym;
   1877 	     csym != NULL; csym = csym->s_symtab_next) {
   1878 		if (!is_member(csym))
   1879 			continue;
   1880 		if (strcmp(msym->s_name, csym->s_name) != 0)
   1881 			continue;
   1882 
   1883 		for (const sym_t *sym = csym->s_symtab_next;
   1884 		     sym != NULL; sym = sym->s_symtab_next) {
   1885 			if (is_member(sym)
   1886 			    && strcmp(csym->s_name, sym->s_name) == 0
   1887 			    && !are_members_compatible(csym, sym))
   1888 				return false;
   1889 		}
   1890 	}
   1891 	return true;
   1892 }
   1893 
   1894 sym_t *
   1895 find_member(const struct_or_union *sou, const char *name)
   1896 {
   1897 	for (sym_t *mem = sou->sou_first_member;
   1898 	     mem != NULL; mem = mem->s_next) {
   1899 		lint_assert(is_member(mem));
   1900 		lint_assert(mem->u.s_member.sm_containing_type == sou);
   1901 		if (strcmp(mem->s_name, name) == 0)
   1902 			return mem;
   1903 	}
   1904 
   1905 	for (sym_t *mem = sou->sou_first_member;
   1906 	     mem != NULL; mem = mem->s_next) {
   1907 		if (is_struct_or_union(mem->s_type->t_tspec)
   1908 		    && mem->s_name == unnamed) {
   1909 			sym_t *nested_mem =
   1910 			    find_member(mem->s_type->t_sou, name);
   1911 			if (nested_mem != NULL)
   1912 				return nested_mem;
   1913 		}
   1914 	}
   1915 	return NULL;
   1916 }
   1917 
   1918 /*
   1919  * Remove the member if it was unknown until now, which means
   1920  * that no defined struct or union has a member with the same name.
   1921  */
   1922 static void
   1923 remove_unknown_member(tnode_t *tn, sym_t *msym)
   1924 {
   1925 	/* type '%s' does not have member '%s' */
   1926 	error(101, type_name(tn->tn_type), msym->s_name);
   1927 	rmsym(msym);
   1928 	msym->s_kind = FMEMBER;
   1929 	msym->s_scl = STRUCT_MEMBER;
   1930 
   1931 	struct_or_union *sou = expr_zero_alloc(sizeof(*sou),
   1932 	    "struct_or_union");
   1933 	sou->sou_tag = expr_zero_alloc(sizeof(*sou->sou_tag), "sym");
   1934 	sou->sou_tag->s_name = unnamed;
   1935 
   1936 	msym->u.s_member.sm_containing_type = sou;
   1937 	/*
   1938 	 * The member sm_offset_in_bits is not needed here since this
   1939 	 * symbol can only be used for error reporting.
   1940 	 */
   1941 }
   1942 
   1943 /*
   1944  * Returns a symbol which has the same name as 'msym' and is a member of the
   1945  * struct or union specified by 'tn'.
   1946  */
   1947 static sym_t *
   1948 struct_or_union_member(tnode_t *tn, op_t op, sym_t *msym)
   1949 {
   1950 
   1951 	/* Determine the tag type of which msym is expected to be a member. */
   1952 	const type_t *tp = NULL;
   1953 	if (op == POINT && is_struct_or_union(tn->tn_type->t_tspec))
   1954 		tp = tn->tn_type;
   1955 	if (op == ARROW && tn->tn_type->t_tspec == PTR
   1956 	    && is_struct_or_union(tn->tn_type->t_subt->t_tspec))
   1957 		tp = tn->tn_type->t_subt;
   1958 	struct_or_union *sou = tp != NULL ? tp->t_sou : NULL;
   1959 
   1960 	if (sou != NULL) {
   1961 		sym_t *nested_mem = find_member(sou, msym->s_name);
   1962 		if (nested_mem != NULL)
   1963 			return nested_mem;
   1964 	}
   1965 
   1966 	if (msym->s_scl == NOSCL) {
   1967 		remove_unknown_member(tn, msym);
   1968 		return msym;
   1969 	}
   1970 
   1971 	bool eq = all_members_compatible(msym);
   1972 
   1973 	/*
   1974 	 * Now handle the case in which the left operand refers really
   1975 	 * to a struct/union, but the right operand is not member of it.
   1976 	 */
   1977 	if (sou != NULL) {
   1978 		if (eq && !allow_c90) {
   1979 			/* illegal use of member '%s' */
   1980 			warning(102, msym->s_name);
   1981 		} else {
   1982 			/* illegal use of member '%s' */
   1983 			error(102, msym->s_name);
   1984 		}
   1985 		return msym;
   1986 	}
   1987 
   1988 	/*
   1989 	 * Now the left operand of ARROW does not point to a struct/union
   1990 	 * or the left operand of POINT is no struct/union.
   1991 	 */
   1992 	if (eq) {
   1993 		if (op == POINT) {
   1994 			if (!allow_c90) {
   1995 				/* left operand of '.' must be struct ... */
   1996 				warning(103, type_name(tn->tn_type));
   1997 			} else {
   1998 				/* left operand of '.' must be struct ... */
   1999 				error(103, type_name(tn->tn_type));
   2000 			}
   2001 		} else {
   2002 			if (!allow_c90 && tn->tn_type->t_tspec == PTR) {
   2003 				/* left operand of '->' must be pointer ... */
   2004 				warning(104, type_name(tn->tn_type));
   2005 			} else {
   2006 				/* left operand of '->' must be pointer ... */
   2007 				error(104, type_name(tn->tn_type));
   2008 			}
   2009 		}
   2010 	} else {
   2011 		if (!allow_c90) {
   2012 			/* non-unique member requires struct/union %s */
   2013 			error(105, op == POINT ? "object" : "pointer");
   2014 		} else {
   2015 			/* unacceptable operand of '%s' */
   2016 			error(111, op_name(op));
   2017 		}
   2018 	}
   2019 
   2020 	return msym;
   2021 }
   2022 
   2023 tnode_t *
   2024 build_member_access(tnode_t *ln, op_t op, bool sys, sbuf_t *member)
   2025 {
   2026 	sym_t *msym;
   2027 
   2028 	if (ln == NULL)
   2029 		return NULL;
   2030 
   2031 	if (op == ARROW) {
   2032 		/* must do this before struct_or_union_member is called */
   2033 		ln = cconv(ln);
   2034 	}
   2035 	msym = struct_or_union_member(ln, op, getsym(member));
   2036 	return build_binary(ln, op, sys, build_name(msym, false));
   2037 }
   2038 
   2039 /*
   2040  * Perform class conversions.
   2041  *
   2042  * Arrays of type T are converted into pointers to type T.
   2043  * Functions are converted to pointers to functions.
   2044  * Lvalues are converted to rvalues.
   2045  *
   2046  * C99 6.3 "Conversions"
   2047  * C99 6.3.2 "Other operands"
   2048  * C99 6.3.2.1 "Lvalues, arrays, and function designators"
   2049  */
   2050 tnode_t *
   2051 cconv(tnode_t *tn)
   2052 {
   2053 	/*
   2054 	 * Array-lvalue (array of type T) is converted into rvalue
   2055 	 * (pointer to type T)
   2056 	 */
   2057 	if (tn->tn_type->t_tspec == ARRAY) {
   2058 		if (!tn->tn_lvalue) {
   2059 			/* XXX print correct operator */
   2060 			/* %soperand of '%s' must be lvalue */
   2061 			gnuism(114, "", op_name(ADDR));
   2062 		}
   2063 		tn = build_op(ADDR, tn->tn_sys,
   2064 		    expr_derive_type(tn->tn_type->t_subt, PTR), tn, NULL);
   2065 	}
   2066 
   2067 	/*
   2068 	 * Expression of type function (function with return value of type T)
   2069 	 * in rvalue-expression (pointer to function with return value
   2070 	 * of type T)
   2071 	 */
   2072 	if (tn->tn_type->t_tspec == FUNC)
   2073 		tn = build_address(tn->tn_sys, tn, true);
   2074 
   2075 	/* lvalue to rvalue */
   2076 	if (tn->tn_lvalue) {
   2077 		type_t *tp = expr_dup_type(tn->tn_type);
   2078 		/* C99 6.3.2.1p2 sentence 2 says to remove the qualifiers. */
   2079 		tp->t_const = tp->t_volatile = false;
   2080 		tn = build_op(LOAD, tn->tn_sys, tp, tn, NULL);
   2081 	}
   2082 
   2083 	return tn;
   2084 }
   2085 
   2086 const tnode_t *
   2087 before_conversion(const tnode_t *tn)
   2088 {
   2089 	while (tn->tn_op == CVT && !tn->tn_cast)
   2090 		tn = tn->tn_left;
   2091 	return tn;
   2092 }
   2093 
   2094 /*
   2095  * Most errors required by C90 are reported in struct_or_union_member().
   2096  * Here we only check for totally wrong things.
   2097  */
   2098 static bool
   2099 typeok_point(const tnode_t *ln, const type_t *ltp, tspec_t lt)
   2100 {
   2101 	if (is_struct_or_union(lt))
   2102 		return true;
   2103 
   2104 	if (lt == FUNC || lt == VOID || ltp->t_bitfield)
   2105 		goto wrong;
   2106 
   2107 	/*
   2108 	 * Some C dialects from before C90 tolerated any lvalue on the
   2109 	 * left-hand side of the '.' operator, allowing things like
   2110 	 * char st[100]; st.st_mtime, assuming that the member 'st_mtime'
   2111 	 * only occurred in a single struct; see typeok_arrow.
   2112 	 */
   2113 	if (ln->tn_lvalue)
   2114 		return true;
   2115 
   2116 wrong:
   2117 	/* With allow_c90 we already got an error */
   2118 	if (!allow_c90)
   2119 		/* unacceptable operand of '%s' */
   2120 		error(111, op_name(POINT));
   2121 
   2122 	return false;
   2123 }
   2124 
   2125 static bool
   2126 typeok_arrow(tspec_t lt)
   2127 {
   2128 	/*
   2129 	 * C1978 Appendix A 14.1 says: <quote>In fact, any lvalue is allowed
   2130 	 * before '.', and that lvalue is then assumed to have the form of
   2131 	 * the structure of which the name of the right is a member. [...]
   2132 	 * Such constructions are non-portable.</quote>
   2133 	 */
   2134 	if (lt == PTR || (!allow_c90 && is_integer(lt)))
   2135 		return true;
   2136 
   2137 	/* With allow_c90 we already got an error */
   2138 	if (!allow_c90)
   2139 		/* unacceptable operand of '%s' */
   2140 		error(111, op_name(ARROW));
   2141 	return false;
   2142 }
   2143 
   2144 static bool
   2145 typeok_incdec(op_t op, const tnode_t *tn, const type_t *tp)
   2146 {
   2147 	/* operand has scalar type (checked in typeok) */
   2148 	if (!tn->tn_lvalue) {
   2149 		if (tn->tn_op == CVT && tn->tn_cast &&
   2150 		    tn->tn_left->tn_op == LOAD) {
   2151 			/* a cast does not yield an lvalue */
   2152 			error(163);
   2153 		}
   2154 		/* %soperand of '%s' must be lvalue */
   2155 		error(114, "", op_name(op));
   2156 		return false;
   2157 	}
   2158 	if (tp->t_const && allow_c90) {
   2159 		/* %soperand of '%s' must be modifiable lvalue */
   2160 		warning(115, "", op_name(op));
   2161 	}
   2162 	return true;
   2163 }
   2164 
   2165 static bool
   2166 typeok_address(op_t op, const tnode_t *tn, const type_t *tp, tspec_t t)
   2167 {
   2168 	if (t == ARRAY || t == FUNC) {
   2169 		/* ok, a warning comes later (in build_address()) */
   2170 	} else if (!tn->tn_lvalue) {
   2171 		if (tn->tn_op == CVT && tn->tn_cast &&
   2172 		    tn->tn_left->tn_op == LOAD) {
   2173 			/* a cast does not yield an lvalue */
   2174 			error(163);
   2175 		}
   2176 		/* %soperand of '%s' must be lvalue */
   2177 		error(114, "", op_name(op));
   2178 		return false;
   2179 	} else if (is_scalar(t)) {
   2180 		if (tp->t_bitfield) {
   2181 			/* cannot take address of bit-field */
   2182 			error(112);
   2183 			return false;
   2184 		}
   2185 	} else if (t != STRUCT && t != UNION) {
   2186 		/* unacceptable operand of '%s' */
   2187 		error(111, op_name(op));
   2188 		return false;
   2189 	}
   2190 	if (tn->tn_op == NAME && tn->tn_sym->s_register) {
   2191 		/* cannot take address of register '%s' */
   2192 		error(113, tn->tn_sym->s_name);
   2193 		return false;
   2194 	}
   2195 	return true;
   2196 }
   2197 
   2198 static bool
   2199 typeok_indir(const type_t *tp, tspec_t t)
   2200 {
   2201 
   2202 	if (t != PTR) {
   2203 		/* cannot dereference non-pointer type '%s' */
   2204 		error(96, type_name(tp));
   2205 		return false;
   2206 	}
   2207 	return true;
   2208 }
   2209 
   2210 static void
   2211 warn_incompatible_types(op_t op,
   2212 			const type_t *ltp, tspec_t lt,
   2213 			const type_t *rtp, tspec_t rt)
   2214 {
   2215 	bool binary = modtab[op].m_binary;
   2216 
   2217 	if (lt == VOID || (binary && rt == VOID)) {
   2218 		/* void type illegal in expression */
   2219 		error(109);
   2220 	} else if (op == ASSIGN) {
   2221 		/* cannot assign to '%s' from '%s' */
   2222 		error(171, type_name(ltp), type_name(rtp));
   2223 	} else if (binary) {
   2224 		/* operands of '%s' have incompatible types '%s' and '%s' */
   2225 		error(107, op_name(op), type_name(ltp), type_name(rtp));
   2226 	} else {
   2227 		lint_assert(rt == NO_TSPEC);
   2228 		/* operand of '%s' has invalid type '%s' */
   2229 		error(108, op_name(op), type_name(ltp));
   2230 	}
   2231 }
   2232 
   2233 static bool
   2234 typeok_plus(op_t op,
   2235 	    const type_t *ltp, tspec_t lt,
   2236 	    const type_t *rtp, tspec_t rt)
   2237 {
   2238 	/* operands have scalar types (checked in typeok) */
   2239 	if ((lt == PTR && !is_integer(rt)) || (rt == PTR && !is_integer(lt))) {
   2240 		warn_incompatible_types(op, ltp, lt, rtp, rt);
   2241 		return false;
   2242 	}
   2243 	return true;
   2244 }
   2245 
   2246 static bool
   2247 typeok_minus(op_t op,
   2248 	     const type_t *ltp, tspec_t lt,
   2249 	     const type_t *rtp, tspec_t rt)
   2250 {
   2251 	/* operands have scalar types (checked in typeok) */
   2252 	if ((lt == PTR && rt != PTR && !is_integer(rt)) ||
   2253 	    (lt != PTR && rt == PTR)) {
   2254 		warn_incompatible_types(op, ltp, lt, rtp, rt);
   2255 		return false;
   2256 	}
   2257 	if (lt == PTR && rt == PTR &&
   2258 	    !types_compatible(ltp->t_subt, rtp->t_subt, true, false, NULL)) {
   2259 		/* illegal pointer subtraction */
   2260 		error(116);
   2261 	}
   2262 	return true;
   2263 }
   2264 
   2265 static void
   2266 typeok_shr(op_t op,
   2267 	   const tnode_t *ln, tspec_t lt,
   2268 	   const tnode_t *rn, tspec_t rt)
   2269 {
   2270 	tspec_t olt = before_conversion(ln)->tn_type->t_tspec;
   2271 	tspec_t ort = before_conversion(rn)->tn_type->t_tspec;
   2272 
   2273 	/* operands have integer types (checked in typeok) */
   2274 	if (pflag && !is_uinteger(olt)) {
   2275 		integer_constraints lc = ic_expr(ln);
   2276 		if (!ic_maybe_signed(ln->tn_type, &lc))
   2277 			return;
   2278 
   2279 		/*
   2280 		 * The left operand is signed. This means that
   2281 		 * the operation is (possibly) nonportable.
   2282 		 */
   2283 		if (ln->tn_op != CON) {
   2284 			/* bitwise '%s' on signed value possibly nonportable */
   2285 			warning(117, op_name(op));
   2286 		} else if (ln->tn_val.u.integer < 0) {
   2287 			/* bitwise '%s' on signed value nonportable */
   2288 			warning(120, op_name(op));
   2289 		}
   2290 	} else if (allow_trad && allow_c90 &&
   2291 		   !is_uinteger(olt) && is_uinteger(ort)) {
   2292 		/* The left operand would become unsigned in traditional C. */
   2293 		if (hflag && (ln->tn_op != CON || ln->tn_val.u.integer < 0)) {
   2294 			/* semantics of '%s' change in C90; use ... */
   2295 			warning(118, op_name(op));
   2296 		}
   2297 	} else if (allow_trad && allow_c90 &&
   2298 		   !is_uinteger(olt) && !is_uinteger(ort) &&
   2299 	    portable_rank_cmp(lt, rt) < 0) {
   2300 		/*
   2301 		 * In traditional C, the left operand would be extended
   2302 		 * (possibly sign-extended) and then shifted.
   2303 		 */
   2304 		if (hflag && (ln->tn_op != CON || ln->tn_val.u.integer < 0)) {
   2305 			/* semantics of '%s' change in C90; use ... */
   2306 			warning(118, op_name(op));
   2307 		}
   2308 	}
   2309 }
   2310 
   2311 static void
   2312 typeok_shl(op_t op, tspec_t lt, tspec_t rt)
   2313 {
   2314 	/*
   2315 	 * C90 does not perform balancing for shift operations,
   2316 	 * but traditional C does. If the width of the right operand
   2317 	 * is greater than the width of the left operand, then in
   2318 	 * traditional C the left operand would be extended to the
   2319 	 * width of the right operand. For SHL this may result in
   2320 	 * different results.
   2321 	 */
   2322 	if (portable_rank_cmp(lt, rt) < 0) {
   2323 		/*
   2324 		 * XXX If both operands are constant, make sure
   2325 		 * that there is really a difference between
   2326 		 * C90 and traditional C.
   2327 		 */
   2328 		if (hflag && allow_trad && allow_c90)
   2329 			/* semantics of '%s' change in C90; use ... */
   2330 			warning(118, op_name(op));
   2331 	}
   2332 }
   2333 
   2334 static void
   2335 typeok_shift(const type_t *ltp, tspec_t lt, const tnode_t *rn, tspec_t rt)
   2336 {
   2337 	if (rn->tn_op != CON)
   2338 		return;
   2339 
   2340 	if (!is_uinteger(rt) && rn->tn_val.u.integer < 0) {
   2341 		/* negative shift */
   2342 		warning(121);
   2343 	} else if ((uint64_t)rn->tn_val.u.integer == size_in_bits(lt)) {
   2344 		/* shift amount %u equals bit-size of '%s' */
   2345 		warning(267, (unsigned)rn->tn_val.u.integer, type_name(ltp));
   2346 	} else if ((uint64_t)rn->tn_val.u.integer > size_in_bits(lt)) {
   2347 		/* shift amount %llu is greater than bit-size %llu of '%s' */
   2348 		warning(122, (unsigned long long)rn->tn_val.u.integer,
   2349 		    (unsigned long long)size_in_bits(lt),
   2350 		    tspec_name(lt));
   2351 	}
   2352 }
   2353 
   2354 static bool
   2355 is_typeok_eq(const tnode_t *ln, tspec_t lt, const tnode_t *rn, tspec_t rt)
   2356 {
   2357 	if (lt == PTR && is_null_pointer(rn))
   2358 		return true;
   2359 	if (rt == PTR && is_null_pointer(ln))
   2360 		return true;
   2361 	return false;
   2362 }
   2363 
   2364 /*
   2365  * Called if incompatible pointer types are detected.
   2366  * Print an appropriate warning.
   2367  */
   2368 static void
   2369 warn_incompatible_pointers(op_t op, const type_t *ltp, const type_t *rtp)
   2370 {
   2371 	lint_assert(ltp->t_tspec == PTR);
   2372 	lint_assert(rtp->t_tspec == PTR);
   2373 
   2374 	tspec_t lt = ltp->t_subt->t_tspec;
   2375 	tspec_t rt = rtp->t_subt->t_tspec;
   2376 
   2377 	if (is_struct_or_union(lt) && is_struct_or_union(rt)) {
   2378 		if (op == RETURN) {
   2379 			/* illegal structure pointer combination */
   2380 			warning(244);
   2381 		} else {
   2382 			/* incompatible structure pointers: '%s' '%s' '%s' */
   2383 			warning(245, type_name(ltp),
   2384 			    op_name(op), type_name(rtp));
   2385 		}
   2386 	} else {
   2387 		if (op == RETURN) {
   2388 			/* illegal combination of '%s' and '%s' */
   2389 			warning(184, type_name(ltp), type_name(rtp));
   2390 		} else {
   2391 			/* illegal combination of '%s' and '%s', op '%s' */
   2392 			warning(124,
   2393 			    type_name(ltp), type_name(rtp), op_name(op));
   2394 		}
   2395 	}
   2396 }
   2397 
   2398 static void
   2399 check_pointer_comparison(op_t op, const tnode_t *ln, const tnode_t *rn)
   2400 {
   2401 	type_t *ltp = ln->tn_type, *rtp = rn->tn_type;
   2402 	tspec_t lst = ltp->t_subt->t_tspec, rst = rtp->t_subt->t_tspec;
   2403 
   2404 	if (lst == VOID || rst == VOID) {
   2405 		/* TODO: C99 behaves like C90 here. */
   2406 		if ((!allow_trad && !allow_c99) &&
   2407 		    (lst == FUNC || rst == FUNC)) {
   2408 			/* (void *)0 is already handled in typeok() */
   2409 			const char *lsts, *rsts;
   2410 			*(lst == FUNC ? &lsts : &rsts) = "function pointer";
   2411 			*(lst == VOID ? &lsts : &rsts) = "'void *'";
   2412 			/* C90 or later forbid comparison of %s with %s */
   2413 			warning(274, lsts, rsts);
   2414 		}
   2415 		return;
   2416 	}
   2417 
   2418 	if (!types_compatible(ltp->t_subt, rtp->t_subt, true, false, NULL)) {
   2419 		warn_incompatible_pointers(op, ltp, rtp);
   2420 		return;
   2421 	}
   2422 
   2423 	if (lst == FUNC && rst == FUNC) {
   2424 		/* TODO: C99 behaves like C90 here, see C99 6.5.8p2. */
   2425 		if ((!allow_trad && !allow_c99) && op != EQ && op != NE)
   2426 			/* pointers to functions can only be compared ... */
   2427 			warning(125);
   2428 	}
   2429 }
   2430 
   2431 static bool
   2432 typeok_compare(op_t op,
   2433 	       const tnode_t *ln, const type_t *ltp, tspec_t lt,
   2434 	       const tnode_t *rn, const type_t *rtp, tspec_t rt)
   2435 {
   2436 	if (lt == PTR && rt == PTR) {
   2437 		check_pointer_comparison(op, ln, rn);
   2438 		return true;
   2439 	}
   2440 
   2441 	if (lt != PTR && rt != PTR)
   2442 		return true;
   2443 
   2444 	if (!is_integer(lt) && !is_integer(rt)) {
   2445 		warn_incompatible_types(op, ltp, lt, rtp, rt);
   2446 		return false;
   2447 	}
   2448 
   2449 	const char *lx = lt == PTR ? "pointer" : "integer";
   2450 	const char *rx = rt == PTR ? "pointer" : "integer";
   2451 	/* illegal combination of %s '%s' and %s '%s', op '%s' */
   2452 	warning(123, lx, type_name(ltp), rx, type_name(rtp), op_name(op));
   2453 	return true;
   2454 }
   2455 
   2456 static bool
   2457 typeok_quest(tspec_t lt, const tnode_t *rn)
   2458 {
   2459 	if (!is_scalar(lt)) {
   2460 		/* first operand of '?' must have scalar type */
   2461 		error(170);
   2462 		return false;
   2463 	}
   2464 	lint_assert(before_conversion(rn)->tn_op == COLON);
   2465 	return true;
   2466 }
   2467 
   2468 static void
   2469 typeok_colon_pointer(const type_t *ltp, const type_t *rtp)
   2470 {
   2471 	type_t *lstp = ltp->t_subt;
   2472 	type_t *rstp = rtp->t_subt;
   2473 	tspec_t lst = lstp->t_tspec;
   2474 	tspec_t rst = rstp->t_tspec;
   2475 
   2476 	if ((lst == VOID && rst == FUNC) || (lst == FUNC && rst == VOID)) {
   2477 		/* (void *)0 is handled in typeok_colon */
   2478 		/* TODO: C99 behaves like C90 here. */
   2479 		if (!allow_trad && !allow_c99)
   2480 			/* conversion of %s to %s requires a cast, op %s */
   2481 			warning(305, "function pointer", "'void *'",
   2482 			    op_name(COLON));
   2483 		return;
   2484 	}
   2485 
   2486 	if (pointer_types_are_compatible(lstp, rstp, true))
   2487 		return;
   2488 	if (!types_compatible(lstp, rstp, true, false, NULL))
   2489 		warn_incompatible_pointers(COLON, ltp, rtp);
   2490 }
   2491 
   2492 static bool
   2493 typeok_colon(const tnode_t *ln, const type_t *ltp, tspec_t lt,
   2494 	     const tnode_t *rn, const type_t *rtp, tspec_t rt)
   2495 {
   2496 
   2497 	if (is_arithmetic(lt) && is_arithmetic(rt))
   2498 		return true;
   2499 	if (lt == BOOL && rt == BOOL)
   2500 		return true;
   2501 
   2502 	if (lt == STRUCT && rt == STRUCT && ltp->t_sou == rtp->t_sou)
   2503 		return true;
   2504 	if (lt == UNION && rt == UNION && ltp->t_sou == rtp->t_sou)
   2505 		return true;
   2506 
   2507 	if (lt == PTR && is_null_pointer(rn))
   2508 		return true;
   2509 	if (rt == PTR && is_null_pointer(ln))
   2510 		return true;
   2511 
   2512 	if ((lt == PTR && is_integer(rt)) || (is_integer(lt) && rt == PTR)) {
   2513 		const char *lx = lt == PTR ? "pointer" : "integer";
   2514 		const char *rx = rt == PTR ? "pointer" : "integer";
   2515 		/* illegal combination of %s '%s' and %s '%s', op '%s' */
   2516 		warning(123, lx, type_name(ltp),
   2517 		    rx, type_name(rtp), op_name(COLON));
   2518 		return true;
   2519 	}
   2520 
   2521 	if (lt == VOID || rt == VOID) {
   2522 		if (lt != VOID || rt != VOID)
   2523 			/* incompatible types '%s' and '%s' in conditional */
   2524 			warning(126, type_name(ltp), type_name(rtp));
   2525 		return true;
   2526 	}
   2527 
   2528 	if (lt == PTR && rt == PTR) {
   2529 		typeok_colon_pointer(ltp, rtp);
   2530 		return true;
   2531 	}
   2532 
   2533 	/* incompatible types '%s' and '%s' in conditional */
   2534 	error(126, type_name(ltp), type_name(rtp));
   2535 	return false;
   2536 }
   2537 
   2538 /*
   2539  * Returns true if the given structure or union has a constant member
   2540  * (maybe recursively).
   2541  */
   2542 static bool
   2543 has_constant_member(const type_t *tp)
   2544 {
   2545 	lint_assert(is_struct_or_union(tp->t_tspec));
   2546 
   2547 	for (sym_t *m = tp->t_sou->sou_first_member;
   2548 	     m != NULL; m = m->s_next) {
   2549 		const type_t *mtp = m->s_type;
   2550 		if (mtp->t_const)
   2551 			return true;
   2552 		if (is_struct_or_union(mtp->t_tspec) &&
   2553 		    has_constant_member(mtp))
   2554 			return true;
   2555 	}
   2556 	return false;
   2557 }
   2558 
   2559 static bool
   2560 typeok_assign(op_t op, const tnode_t *ln, const type_t *ltp, tspec_t lt)
   2561 {
   2562 	if (op == RETURN || op == INIT || op == FARG)
   2563 		return true;
   2564 
   2565 	if (!ln->tn_lvalue) {
   2566 		if (ln->tn_op == CVT && ln->tn_cast &&
   2567 		    ln->tn_left->tn_op == LOAD) {
   2568 			/* a cast does not yield an lvalue */
   2569 			error(163);
   2570 		}
   2571 		/* %soperand of '%s' must be lvalue */
   2572 		error(114, "left ", op_name(op));
   2573 		return false;
   2574 	} else if (ltp->t_const
   2575 	    || (is_struct_or_union(lt) && has_constant_member(ltp))) {
   2576 		if (allow_c90)
   2577 			/* %soperand of '%s' must be modifiable lvalue */
   2578 			warning(115, "left ", op_name(op));
   2579 	}
   2580 	return true;
   2581 }
   2582 
   2583 /* Check the types using the information from modtab[]. */
   2584 static bool
   2585 typeok_scalar(op_t op, const mod_t *mp,
   2586 	      const type_t *ltp, tspec_t lt,
   2587 	      const type_t *rtp, tspec_t rt)
   2588 {
   2589 	if (mp->m_takes_bool && lt == BOOL && rt == BOOL)
   2590 		return true;
   2591 	if (mp->m_requires_integer) {
   2592 		if (!is_integer(lt) || (mp->m_binary && !is_integer(rt))) {
   2593 			warn_incompatible_types(op, ltp, lt, rtp, rt);
   2594 			return false;
   2595 		}
   2596 	} else if (mp->m_requires_integer_or_complex) {
   2597 		if ((!is_integer(lt) && !is_complex(lt)) ||
   2598 		    (mp->m_binary && (!is_integer(rt) && !is_complex(rt)))) {
   2599 			warn_incompatible_types(op, ltp, lt, rtp, rt);
   2600 			return false;
   2601 		}
   2602 	} else if (mp->m_requires_scalar) {
   2603 		if (!is_scalar(lt) || (mp->m_binary && !is_scalar(rt))) {
   2604 			warn_incompatible_types(op, ltp, lt, rtp, rt);
   2605 			return false;
   2606 		}
   2607 	} else if (mp->m_requires_arith) {
   2608 		if (!is_arithmetic(lt) ||
   2609 		    (mp->m_binary && !is_arithmetic(rt))) {
   2610 			warn_incompatible_types(op, ltp, lt, rtp, rt);
   2611 			return false;
   2612 		}
   2613 	}
   2614 	return true;
   2615 }
   2616 
   2617 static void
   2618 check_assign_void_pointer(op_t op, int arg,
   2619 			  tspec_t lt, tspec_t lst,
   2620 			  tspec_t rt, tspec_t rst)
   2621 {
   2622 
   2623 	if (!(lt == PTR && rt == PTR && (lst == VOID || rst == VOID)))
   2624 		return;
   2625 	/* two pointers, at least one pointer to void */
   2626 
   2627 	/* TODO: C99 behaves like C90 here. */
   2628 	if (!((!allow_trad && !allow_c99) && (lst == FUNC || rst == FUNC)))
   2629 		return;
   2630 	/* comb. of ptr to func and ptr to void */
   2631 
   2632 	const char *lts, *rts;
   2633 	*(lst == FUNC ? &lts : &rts) = "function pointer";
   2634 	*(lst == VOID ? &lts : &rts) = "'void *'";
   2635 
   2636 	switch (op) {
   2637 	case INIT:
   2638 	case RETURN:
   2639 		/* conversion of %s to %s requires a cast */
   2640 		warning(303, rts, lts);
   2641 		break;
   2642 	case FARG:
   2643 		/* conversion of %s to %s requires a cast, arg #%d */
   2644 		warning(304, rts, lts, arg);
   2645 		break;
   2646 	default:
   2647 		/* conversion of %s to %s requires a cast, op %s */
   2648 		warning(305, rts, lts, op_name(op));
   2649 		break;
   2650 	}
   2651 }
   2652 
   2653 static bool
   2654 is_direct_function_call(const tnode_t *tn, const char **out_name)
   2655 {
   2656 
   2657 	if (!(tn->tn_op == CALL &&
   2658 	      tn->tn_left->tn_op == ADDR &&
   2659 	      tn->tn_left->tn_left->tn_op == NAME))
   2660 		return false;
   2661 
   2662 	*out_name = tn->tn_left->tn_left->tn_sym->s_name;
   2663 	return true;
   2664 }
   2665 
   2666 static bool
   2667 is_unconst_function(const char *name)
   2668 {
   2669 
   2670 	return strcmp(name, "memchr") == 0 ||
   2671 	       strcmp(name, "strchr") == 0 ||
   2672 	       strcmp(name, "strpbrk") == 0 ||
   2673 	       strcmp(name, "strrchr") == 0 ||
   2674 	       strcmp(name, "strstr") == 0;
   2675 }
   2676 
   2677 static bool
   2678 is_const_char_pointer(const tnode_t *tn)
   2679 {
   2680 	/*
   2681 	 * For traditional reasons, C99 6.4.5p5 defines that string literals
   2682 	 * have type 'char[]'.  They are often implicitly converted to
   2683 	 * 'char *', for example when they are passed as function arguments.
   2684 	 *
   2685 	 * C99 6.4.5p6 further defines that modifying a string that is
   2686 	 * constructed from a string literal invokes undefined behavior.
   2687 	 *
   2688 	 * Out of these reasons, string literals are treated as 'effectively
   2689 	 * const' here.
   2690 	 */
   2691 	if (tn->tn_op == CVT &&
   2692 	    tn->tn_left->tn_op == ADDR &&
   2693 	    tn->tn_left->tn_left->tn_op == STRING)
   2694 		return true;
   2695 
   2696 	const type_t *tp = before_conversion(tn)->tn_type;
   2697 	return tp->t_tspec == PTR &&
   2698 	       tp->t_subt->t_tspec == CHAR &&
   2699 	       tp->t_subt->t_const;
   2700 }
   2701 
   2702 static bool
   2703 is_first_arg_const_char_pointer(const tnode_t *tn)
   2704 {
   2705 	const tnode_t *an = tn->tn_right;
   2706 	if (an == NULL)
   2707 		return false;
   2708 
   2709 	while (an->tn_right != NULL)
   2710 		an = an->tn_right;
   2711 	return is_const_char_pointer(an->tn_left);
   2712 }
   2713 
   2714 static bool
   2715 is_const_pointer(const tnode_t *tn)
   2716 {
   2717 	const type_t *tp = before_conversion(tn)->tn_type;
   2718 	return tp->t_tspec == PTR && tp->t_subt->t_const;
   2719 }
   2720 
   2721 static bool
   2722 is_second_arg_const_pointer(const tnode_t *tn)
   2723 {
   2724 	const tnode_t *an = tn->tn_right;
   2725 	if (an == NULL || an->tn_right == NULL)
   2726 		return false;
   2727 
   2728 	while (an->tn_right->tn_right != NULL)
   2729 		an = an->tn_right;
   2730 	return is_const_pointer(an->tn_left);
   2731 }
   2732 
   2733 static void
   2734 check_unconst_function(const type_t *lstp, const tnode_t *rn)
   2735 {
   2736 	const char *function_name;
   2737 
   2738 	if (lstp->t_tspec == CHAR && !lstp->t_const &&
   2739 	    is_direct_function_call(rn, &function_name) &&
   2740 	    is_unconst_function(function_name) &&
   2741 	    is_first_arg_const_char_pointer(rn)) {
   2742 		/* call to '%s' effectively discards 'const' from argument */
   2743 		warning(346, function_name);
   2744 	}
   2745 
   2746 	if (!lstp->t_const &&
   2747 	    is_direct_function_call(rn, &function_name) &&
   2748 	    strcmp(function_name, "bsearch") == 0 &&
   2749 	    is_second_arg_const_pointer(rn)) {
   2750 		/* call to '%s' effectively discards 'const' from argument */
   2751 		warning(346, function_name);
   2752 	}
   2753 }
   2754 
   2755 static bool
   2756 check_assign_void_pointer_compat(op_t op, int arg,
   2757 				 const type_t *const ltp, tspec_t const lt,
   2758 				 const type_t *const lstp, tspec_t const lst,
   2759 				 const tnode_t *const rn,
   2760 				 const type_t *const rtp, tspec_t const rt,
   2761 				 const type_t *const rstp, tspec_t const rst)
   2762 {
   2763 	if (!(lt == PTR && rt == PTR && (lst == VOID || rst == VOID ||
   2764 					 types_compatible(lstp, rstp,
   2765 					     true, false, NULL))))
   2766 		return false;
   2767 
   2768 	/* compatible pointer types (qualifiers ignored) */
   2769 	if (allow_c90 &&
   2770 	    ((!lstp->t_const && rstp->t_const) ||
   2771 	     (!lstp->t_volatile && rstp->t_volatile))) {
   2772 		/* left side has not all qualifiers of right */
   2773 		switch (op) {
   2774 		case INIT:
   2775 		case RETURN:
   2776 			/* incompatible pointer types to '%s' and '%s' */
   2777 			warning(182, type_name(lstp), type_name(rstp));
   2778 			break;
   2779 		case FARG:
   2780 			/* converting '%s' to incompatible '%s' ... */
   2781 			warning(153,
   2782 			    type_name(rtp), type_name(ltp), arg);
   2783 			break;
   2784 		default:
   2785 			/* operands of '%s' have incompatible pointer ... */
   2786 			warning(128, op_name(op),
   2787 			    type_name(lstp), type_name(rstp));
   2788 			break;
   2789 		}
   2790 	}
   2791 
   2792 	if (allow_c90)
   2793 		check_unconst_function(lstp, rn);
   2794 
   2795 	return true;
   2796 }
   2797 
   2798 static bool
   2799 check_assign_pointer_integer(op_t op, int arg,
   2800 			     const type_t *const ltp, tspec_t const lt,
   2801 			     const type_t *const rtp, tspec_t const rt)
   2802 {
   2803 
   2804 	if (!((lt == PTR && is_integer(rt)) || (is_integer(lt) && rt == PTR)))
   2805 		return false;
   2806 
   2807 	const char *lx = lt == PTR ? "pointer" : "integer";
   2808 	const char *rx = rt == PTR ? "pointer" : "integer";
   2809 
   2810 	switch (op) {
   2811 	case INIT:
   2812 	case RETURN:
   2813 		/* illegal combination of %s '%s' and %s '%s' */
   2814 		warning(183, lx, type_name(ltp), rx, type_name(rtp));
   2815 		break;
   2816 	case FARG:
   2817 		/* illegal combination of %s '%s' and %s '%s', arg #%d */
   2818 		warning(154,
   2819 		    lx, type_name(ltp), rx, type_name(rtp), arg);
   2820 		break;
   2821 	default:
   2822 		/* illegal combination of %s '%s' and %s '%s', op '%s' */
   2823 		warning(123,
   2824 		    lx, type_name(ltp), rx, type_name(rtp), op_name(op));
   2825 		break;
   2826 	}
   2827 	return true;
   2828 }
   2829 
   2830 static bool
   2831 check_assign_pointer(op_t op, int arg,
   2832 		     const type_t *ltp, tspec_t lt,
   2833 		     const type_t *rtp, tspec_t rt)
   2834 {
   2835 	if (!(lt == PTR && rt == PTR))
   2836 		return false;
   2837 
   2838 	if (op == FARG)
   2839 		/* converting '%s' to incompatible '%s' for ... */
   2840 		warning(153, type_name(rtp), type_name(ltp), arg);
   2841 	else
   2842 		warn_incompatible_pointers(op, ltp, rtp);
   2843 	return true;
   2844 }
   2845 
   2846 static void
   2847 warn_assign(op_t op, int arg,
   2848 	    const type_t *ltp, tspec_t lt,
   2849 	    const type_t *rtp, tspec_t rt)
   2850 {
   2851 	switch (op) {
   2852 	case INIT:
   2853 		/* cannot initialize '%s' from '%s' */
   2854 		error(185, type_name(ltp), type_name(rtp));
   2855 		break;
   2856 	case RETURN:
   2857 		/* function has return type '%s' but returns '%s' */
   2858 		error(211, type_name(ltp), type_name(rtp));
   2859 		break;
   2860 	case FARG:
   2861 		/* passing '%s' to incompatible '%s', arg #%d */
   2862 		warning(155, type_name(rtp), type_name(ltp), arg);
   2863 		break;
   2864 	default:
   2865 		warn_incompatible_types(op, ltp, lt, rtp, rt);
   2866 		break;
   2867 	}
   2868 }
   2869 
   2870 /*
   2871  * Checks type compatibility for ASSIGN, INIT, FARG and RETURN
   2872  * and prints warnings/errors if necessary.
   2873  * Returns whether the types are (almost) compatible.
   2874  */
   2875 static bool
   2876 check_assign_types_compatible(op_t op, int arg,
   2877 			      const tnode_t *ln, const tnode_t *rn)
   2878 {
   2879 	tspec_t lt, rt, lst = NO_TSPEC, rst = NO_TSPEC;
   2880 	type_t *ltp, *rtp, *lstp = NULL, *rstp = NULL;
   2881 
   2882 	if ((lt = (ltp = ln->tn_type)->t_tspec) == PTR)
   2883 		lst = (lstp = ltp->t_subt)->t_tspec;
   2884 	if ((rt = (rtp = rn->tn_type)->t_tspec) == PTR)
   2885 		rst = (rstp = rtp->t_subt)->t_tspec;
   2886 
   2887 	if (lt == BOOL && is_scalar(rt))	/* C99 6.3.1.2 */
   2888 		return true;
   2889 
   2890 	if (is_arithmetic(lt) && (is_arithmetic(rt) || rt == BOOL))
   2891 		return true;
   2892 
   2893 	if (is_struct_or_union(lt) && is_struct_or_union(rt))
   2894 		/* both are struct or union */
   2895 		return ltp->t_sou == rtp->t_sou;
   2896 
   2897 	/* a null pointer may be assigned to any pointer */
   2898 	if (lt == PTR && is_null_pointer(rn)) {
   2899 		if (is_integer(rn->tn_type->t_tspec))
   2900 			/* implicit conversion from integer 0 to pointer ... */
   2901 			query_message(15, type_name(ltp));
   2902 		return true;
   2903 	}
   2904 
   2905 	check_assign_void_pointer(op, arg, lt, lst, rt, rst);
   2906 
   2907 	if (check_assign_void_pointer_compat(op, arg,
   2908 	    ltp, lt, lstp, lst, rn, rtp, rt, rstp, rst))
   2909 		return true;
   2910 
   2911 	if (check_assign_pointer_integer(op, arg, ltp, lt, rtp, rt))
   2912 		return true;
   2913 
   2914 	if (check_assign_pointer(op, arg, ltp, lt, rtp, rt))
   2915 		return true;
   2916 
   2917 	warn_assign(op, arg, ltp, lt, rtp, rt);
   2918 	return false;
   2919 }
   2920 
   2921 static bool
   2922 has_side_effect(const tnode_t *tn) /* NOLINT(misc-no-recursion) */
   2923 {
   2924 	op_t op = tn->tn_op;
   2925 
   2926 	if (modtab[op].m_has_side_effect)
   2927 		return true;
   2928 
   2929 	if (op == CVT && tn->tn_type->t_tspec == VOID)
   2930 		return has_side_effect(tn->tn_left);
   2931 
   2932 	/* XXX: Why not has_side_effect(tn->tn_left) as well? */
   2933 	if (op == LOGAND || op == LOGOR)
   2934 		return has_side_effect(tn->tn_right);
   2935 
   2936 	/* XXX: Why not has_side_effect(tn->tn_left) as well? */
   2937 	if (op == QUEST)
   2938 		return has_side_effect(tn->tn_right);
   2939 
   2940 	if (op == COLON || op == COMMA) {
   2941 		return has_side_effect(tn->tn_left) ||
   2942 		       has_side_effect(tn->tn_right);
   2943 	}
   2944 
   2945 	return false;
   2946 }
   2947 
   2948 static bool
   2949 is_void_cast(const tnode_t *tn)
   2950 {
   2951 
   2952 	return tn->tn_op == CVT && tn->tn_cast &&
   2953 	       tn->tn_type->t_tspec == VOID;
   2954 }
   2955 
   2956 static bool
   2957 is_local_symbol(const tnode_t *tn)
   2958 {
   2959 
   2960 	return tn->tn_op == LOAD &&
   2961 	       tn->tn_left->tn_op == NAME &&
   2962 	       tn->tn_left->tn_sym->s_scl == AUTO;
   2963 }
   2964 
   2965 static bool
   2966 is_int_constant_zero(const tnode_t *tn)
   2967 {
   2968 
   2969 	return tn->tn_op == CON &&
   2970 	       tn->tn_type->t_tspec == INT &&
   2971 	       tn->tn_val.u.integer == 0;
   2972 }
   2973 
   2974 static void
   2975 check_null_effect(const tnode_t *tn)
   2976 {
   2977 
   2978 	if (hflag &&
   2979 	    !has_side_effect(tn) &&
   2980 	    !(is_void_cast(tn) && is_local_symbol(tn->tn_left)) &&
   2981 	    !(is_void_cast(tn) && is_int_constant_zero(tn->tn_left))) {
   2982 		/* expression has null effect */
   2983 		warning(129);
   2984 	}
   2985 }
   2986 
   2987 /*
   2988  * Check the types for specific operators and type combinations.
   2989  *
   2990  * At this point, the operands already conform to the type requirements of
   2991  * the operator, such as being integer, floating or scalar.
   2992  */
   2993 static bool
   2994 typeok_op(op_t op, int arg,
   2995 	  const tnode_t *ln, const type_t *ltp, tspec_t lt,
   2996 	  const tnode_t *rn, const type_t *rtp, tspec_t rt)
   2997 {
   2998 	switch (op) {
   2999 	case ARROW:
   3000 		return typeok_arrow(lt);
   3001 	case POINT:
   3002 		return typeok_point(ln, ltp, lt);
   3003 	case INCBEF:
   3004 	case DECBEF:
   3005 	case INCAFT:
   3006 	case DECAFT:
   3007 		return typeok_incdec(op, ln, ltp);
   3008 	case INDIR:
   3009 		return typeok_indir(ltp, lt);
   3010 	case ADDR:
   3011 		return typeok_address(op, ln, ltp, lt);
   3012 	case PLUS:
   3013 		return typeok_plus(op, ltp, lt, rtp, rt);
   3014 	case MINUS:
   3015 		return typeok_minus(op, ltp, lt, rtp, rt);
   3016 	case SHL:
   3017 		typeok_shl(op, lt, rt);
   3018 		goto shift;
   3019 	case SHR:
   3020 		typeok_shr(op, ln, lt, rn, rt);
   3021 	shift:
   3022 		typeok_shift(ltp, lt, rn, rt);
   3023 		break;
   3024 	case LT:
   3025 	case LE:
   3026 	case GT:
   3027 	case GE:
   3028 	compare:
   3029 		return typeok_compare(op, ln, ltp, lt, rn, rtp, rt);
   3030 	case EQ:
   3031 	case NE:
   3032 		if (is_typeok_eq(ln, lt, rn, rt))
   3033 			break;
   3034 		goto compare;
   3035 	case QUEST:
   3036 		return typeok_quest(lt, rn);
   3037 	case COLON:
   3038 		return typeok_colon(ln, ltp, lt, rn, rtp, rt);
   3039 	case ASSIGN:
   3040 	case INIT:
   3041 	case FARG:
   3042 	case RETURN:
   3043 		if (!check_assign_types_compatible(op, arg, ln, rn))
   3044 			return false;
   3045 		goto assign;
   3046 	case MULASS:
   3047 	case DIVASS:
   3048 	case MODASS:
   3049 		goto assign;
   3050 	case ADDASS:
   3051 	case SUBASS:
   3052 		if ((lt == PTR && !is_integer(rt)) || rt == PTR) {
   3053 			warn_incompatible_types(op, ltp, lt, rtp, rt);
   3054 			return false;
   3055 		}
   3056 		goto assign;
   3057 	case SHLASS:
   3058 		goto assign;
   3059 	case SHRASS:
   3060 		if (pflag && !is_uinteger(lt) &&
   3061 		    !(!allow_c90 && is_uinteger(rt))) {
   3062 			/* bitwise '%s' on signed value possibly nonportable */
   3063 			warning(117, op_name(op));
   3064 		}
   3065 		goto assign;
   3066 	case ANDASS:
   3067 	case XORASS:
   3068 	case ORASS:
   3069 	assign:
   3070 		return typeok_assign(op, ln, ltp, lt);
   3071 	case COMMA:
   3072 		if (!modtab[ln->tn_op].m_has_side_effect)
   3073 			check_null_effect(ln);
   3074 		break;
   3075 	default:
   3076 		break;
   3077 	}
   3078 	return true;
   3079 }
   3080 
   3081 /* Prints a warning if a strange operator is used on an enum type. */
   3082 static void
   3083 check_bad_enum_operation(op_t op, const tnode_t *ln, const tnode_t *rn)
   3084 {
   3085 
   3086 	if (!eflag)
   3087 		return;
   3088 
   3089 	/* Allow enum in array indices. */
   3090 	if (op == PLUS &&
   3091 	    ((ln->tn_type->t_is_enum && rn->tn_type->t_tspec == PTR) ||
   3092 	     (rn->tn_type->t_is_enum && ln->tn_type->t_tspec == PTR))) {
   3093 		return;
   3094 	}
   3095 
   3096 	/* dubious operation '%s' on enum */
   3097 	warning(241, op_name(op));
   3098 }
   3099 
   3100 /* Prints a warning if an operator is applied to two different enum types. */
   3101 static void
   3102 check_enum_type_mismatch(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
   3103 {
   3104 	const mod_t *mp = &modtab[op];
   3105 
   3106 	if (ln->tn_type->t_enum != rn->tn_type->t_enum) {
   3107 		switch (op) {
   3108 		case INIT:
   3109 			/* enum type mismatch between '%s' and '%s' in ... */
   3110 			warning(210,
   3111 			    type_name(ln->tn_type), type_name(rn->tn_type));
   3112 			break;
   3113 		case FARG:
   3114 			/* function expects '%s', passing '%s' for arg #%d */
   3115 			warning(156,
   3116 			    type_name(ln->tn_type), type_name(rn->tn_type),
   3117 			    arg);
   3118 			break;
   3119 		case RETURN:
   3120 			/* function has return type '%s' but returns '%s' */
   3121 			warning(211,
   3122 			    type_name(ln->tn_type), type_name(rn->tn_type));
   3123 			break;
   3124 		default:
   3125 			/* enum type mismatch: '%s' '%s' '%s' */
   3126 			warning(130, type_name(ln->tn_type), op_name(op),
   3127 			    type_name(rn->tn_type));
   3128 			break;
   3129 		}
   3130 	} else if (Pflag && eflag && mp->m_comparison && op != EQ && op != NE)
   3131 		/* operator '%s' assumes that '%s' is ordered */
   3132 		warning(243, op_name(op), type_name(ln->tn_type));
   3133 }
   3134 
   3135 /* Prints a warning if the operands mix between enum and integer. */
   3136 static void
   3137 check_enum_int_mismatch(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
   3138 {
   3139 
   3140 	if (!eflag)
   3141 		return;
   3142 
   3143 	switch (op) {
   3144 	case INIT:
   3145 		/*
   3146 		 * Initialization with 0 is allowed. Otherwise, all implicit
   3147 		 * initializations would need to be warned upon as well.
   3148 		 */
   3149 		if (!rn->tn_type->t_is_enum && rn->tn_op == CON &&
   3150 		    is_integer(rn->tn_type->t_tspec) &&
   3151 		    rn->tn_val.u.integer == 0) {
   3152 			return;
   3153 		}
   3154 		/* initialization of '%s' with '%s' */
   3155 		warning(277, type_name(ln->tn_type), type_name(rn->tn_type));
   3156 		break;
   3157 	case FARG:
   3158 		/* combination of '%s' and '%s', arg #%d */
   3159 		warning(278,
   3160 		    type_name(ln->tn_type), type_name(rn->tn_type), arg);
   3161 		break;
   3162 	case RETURN:
   3163 		/* combination of '%s' and '%s' in return */
   3164 		warning(279, type_name(ln->tn_type), type_name(rn->tn_type));
   3165 		break;
   3166 	default:
   3167 		/* combination of '%s' and '%s', op '%s' */
   3168 		warning(242, type_name(ln->tn_type), type_name(rn->tn_type),
   3169 		    op_name(op));
   3170 		break;
   3171 	}
   3172 }
   3173 
   3174 static void
   3175 typeok_enum(op_t op, const mod_t *mp, int arg,
   3176 	    const tnode_t *ln, const type_t *ltp,
   3177 	    const tnode_t *rn, const type_t *rtp)
   3178 {
   3179 	if (mp->m_bad_on_enum &&
   3180 	    (ltp->t_is_enum || (mp->m_binary && rtp->t_is_enum))) {
   3181 		check_bad_enum_operation(op, ln, rn);
   3182 	} else if (mp->m_valid_on_enum &&
   3183 		   (ltp->t_is_enum && rtp != NULL && rtp->t_is_enum)) {
   3184 		check_enum_type_mismatch(op, arg, ln, rn);
   3185 	} else if (mp->m_valid_on_enum &&
   3186 		   (ltp->t_is_enum || (rtp != NULL && rtp->t_is_enum))) {
   3187 		check_enum_int_mismatch(op, arg, ln, rn);
   3188 	}
   3189 }
   3190 
   3191 /* Perform most type checks. Return whether the types are ok. */
   3192 bool
   3193 typeok(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
   3194 {
   3195 
   3196 	const mod_t *mp = &modtab[op];
   3197 
   3198 	type_t *ltp = ln->tn_type;
   3199 	tspec_t lt = ltp->t_tspec;
   3200 
   3201 	type_t *rtp = mp->m_binary ? rn->tn_type : NULL;
   3202 	tspec_t rt = mp->m_binary ? rtp->t_tspec : NO_TSPEC;
   3203 
   3204 	if (Tflag && !typeok_scalar_strict_bool(op, mp, arg, ln, rn))
   3205 		return false;
   3206 	if (!typeok_scalar(op, mp, ltp, lt, rtp, rt))
   3207 		return false;
   3208 
   3209 	if (!typeok_op(op, arg, ln, ltp, lt, rn, rtp, rt))
   3210 		return false;
   3211 
   3212 	typeok_enum(op, mp, arg, ln, ltp, rn, rtp);
   3213 	return true;
   3214 }
   3215 
   3216 /* In traditional C, keep unsigned and promote FLOAT to DOUBLE. */
   3217 static tspec_t
   3218 promote_trad(tspec_t t)
   3219 {
   3220 
   3221 	if (t == UCHAR || t == USHORT)
   3222 		return UINT;
   3223 	if (t == CHAR || t == SCHAR || t == SHORT)
   3224 		return INT;
   3225 	if (t == FLOAT)
   3226 		return DOUBLE;
   3227 	if (t == ENUM)
   3228 		return INT;
   3229 	return t;
   3230 }
   3231 
   3232 /*
   3233  * C99 6.3.1.1p2 requires for types with lower rank than int that "If an int
   3234  * can represent all the values of the original type, the value is converted
   3235  * to an int; otherwise it is converted to an unsigned int", and that "All
   3236  * other types are unchanged by the integer promotions".
   3237  */
   3238 static tspec_t
   3239 promote_c90(const tnode_t *tn, tspec_t t, bool farg)
   3240 {
   3241 	if (tn->tn_type->t_bitfield) {
   3242 		unsigned int width = tn->tn_type->t_bit_field_width;
   3243 		unsigned int int_width = size_in_bits(INT);
   3244 		// XXX: What about _Bool bit-fields, since C99?
   3245 		if (width < int_width)
   3246 			return INT;
   3247 		if (width == int_width)
   3248 			return is_uinteger(t) ? UINT : INT;
   3249 		return t;
   3250 	}
   3251 
   3252 	if (t == CHAR || t == SCHAR)
   3253 		return INT;
   3254 	if (t == UCHAR)
   3255 		return size_in_bits(CHAR) < size_in_bits(INT) ? INT : UINT;
   3256 	if (t == SHORT)
   3257 		return INT;
   3258 	if (t == USHORT)
   3259 		return size_in_bits(SHORT) < size_in_bits(INT) ? INT : UINT;
   3260 	if (t == ENUM)
   3261 		return INT;
   3262 	if (farg && t == FLOAT)
   3263 		return DOUBLE;
   3264 	return t;
   3265 }
   3266 
   3267 /*
   3268  * Performs the "integer promotions" (C99 6.3.1.1p2), which convert small
   3269  * integer types to either int or unsigned int.
   3270  *
   3271  * If allow_c90 is unset or the operand is a function argument with no type
   3272  * information (no prototype or variable # of args), converts float to double.
   3273  */
   3274 tnode_t *
   3275 promote(op_t op, bool farg, tnode_t *tn)
   3276 {
   3277 
   3278 	tspec_t ot = tn->tn_type->t_tspec;
   3279 	if (!is_arithmetic(ot))
   3280 		return tn;
   3281 
   3282 	tspec_t nt = allow_c90 ? promote_c90(tn, ot, farg) : promote_trad(ot);
   3283 	if (nt == ot)
   3284 		return tn;
   3285 
   3286 	type_t *ntp = expr_dup_type(tn->tn_type);
   3287 	ntp->t_tspec = nt;
   3288 	/*
   3289 	 * Keep t_is_enum even though t_tspec gets converted from
   3290 	 * ENUM to INT, so we are later able to check compatibility
   3291 	 * of enum types.
   3292 	 */
   3293 	return convert(op, 0, ntp, tn);
   3294 }
   3295 
   3296 static void
   3297 convert_integer_from_floating(op_t op, const type_t *tp, const tnode_t *tn)
   3298 {
   3299 
   3300 	if (op == CVT)
   3301 		/* cast from floating point '%s' to integer '%s' */
   3302 		query_message(2, type_name(tn->tn_type), type_name(tp));
   3303 	else
   3304 		/* implicit conversion from floating point '%s' to ... */
   3305 		query_message(1, type_name(tn->tn_type), type_name(tp));
   3306 }
   3307 
   3308 static bool
   3309 should_warn_about_prototype_conversion(tspec_t nt,
   3310 				       tspec_t ot, const tnode_t *ptn)
   3311 {
   3312 
   3313 	if (nt == ot)
   3314 		return false;
   3315 
   3316 	if (nt == ENUM && ot == INT)
   3317 		return false;
   3318 
   3319 	if (is_floating(nt) != is_floating(ot) ||
   3320 	    portable_rank_cmp(nt, ot) != 0) {
   3321 		/* representation and/or width change */
   3322 		if (!is_integer(ot))
   3323 			return true;
   3324 		/*
   3325 		 * XXX: Investigate whether this rule makes sense; see
   3326 		 * tests/usr.bin/xlint/lint1/platform_long.c.
   3327 		 */
   3328 		return portable_rank_cmp(ot, INT) > 0;
   3329 	}
   3330 
   3331 	if (!hflag)
   3332 		return false;
   3333 
   3334 	/*
   3335 	 * If the types differ only in sign and the argument has the same
   3336 	 * representation in both types, print no warning.
   3337 	 */
   3338 	if (ptn->tn_op == CON && is_integer(nt) &&
   3339 	    signed_type(nt) == signed_type(ot) &&
   3340 	    !msb(ptn->tn_val.u.integer, ot))
   3341 		return false;
   3342 
   3343 	return true;
   3344 }
   3345 
   3346 /*
   3347  * Warn if a prototype causes a type conversion that is different from what
   3348  * would happen to the same argument in the absence of a prototype.  This
   3349  * check is intended for code that needs to stay compatible with pre-C90 C.
   3350  *
   3351  * Errors/warnings about illegal type combinations are already printed
   3352  * in check_assign_types_compatible().
   3353  */
   3354 static void
   3355 check_prototype_conversion(int arg, tspec_t nt, tspec_t ot, type_t *tp,
   3356 			   tnode_t *tn)
   3357 {
   3358 
   3359 	if (!is_arithmetic(nt) || !is_arithmetic(ot))
   3360 		return;
   3361 
   3362 	/*
   3363 	 * If the type of the formal parameter is char/short, a warning
   3364 	 * would be useless, because functions declared the old style
   3365 	 * can't expect char/short arguments.
   3366 	 */
   3367 	if (nt == CHAR || nt == SCHAR || nt == UCHAR ||
   3368 	    nt == SHORT || nt == USHORT)
   3369 		return;
   3370 
   3371 	/* apply the default promotion */
   3372 	tnode_t *ptn = promote(NOOP, true, tn);
   3373 	ot = ptn->tn_type->t_tspec;
   3374 
   3375 	if (should_warn_about_prototype_conversion(nt, ot, ptn)) {
   3376 		/* argument %d is converted from '%s' to '%s' ... */
   3377 		warning(259, arg, type_name(tn->tn_type), type_name(tp));
   3378 	}
   3379 }
   3380 
   3381 /*
   3382  * When converting a large integer type to a small integer type, in some
   3383  * cases the value of the actual expression is further restricted than the
   3384  * type bounds, such as in (expr & 0xFF) or (expr % 100) or (expr >> 24).
   3385  */
   3386 static bool
   3387 can_represent(const type_t *tp, const tnode_t *tn)
   3388 {
   3389 
   3390 	debug_step("%s: type '%s'", __func__, type_name(tp));
   3391 	debug_node(tn);
   3392 
   3393 	uint64_t nmask = value_bits(width_in_bits(tp));
   3394 	if (!is_uinteger(tp->t_tspec))
   3395 		nmask >>= 1;
   3396 
   3397 	integer_constraints c = ic_expr(tn);
   3398 	if ((~c.bclr & ~nmask) == 0)
   3399 		return true;
   3400 
   3401 	integer_constraints tpc = ic_any(tp);
   3402 	if (is_uinteger(tp->t_tspec)
   3403 	    ? tpc.umin <= c.umin && tpc.umax >= c.umax
   3404 	    : tpc.smin <= c.smin && tpc.smax >= c.smax)
   3405 		return true;
   3406 
   3407 	return false;
   3408 }
   3409 
   3410 static bool
   3411 should_warn_about_integer_conversion(const type_t *ntp, tspec_t nt,
   3412 				     const tnode_t *otn, tspec_t ot)
   3413 {
   3414 
   3415 	// XXX: The portable_rank_cmp aims at portable mode, independent of the
   3416 	// current platform, while can_represent acts on the actual type sizes
   3417 	// from the current platform.  This mix is inconsistent, but anything
   3418 	// else would make the exact conditions too complicated to grasp.
   3419 	if (aflag > 0 && portable_rank_cmp(nt, ot) < 0) {
   3420 		if (ot == LONG || ot == ULONG
   3421 		    || ot == LLONG || ot == ULLONG
   3422 #ifdef INT128_SIZE
   3423 		    || ot == INT128 || ot == UINT128
   3424 #endif
   3425 		    || aflag > 1)
   3426 			return !can_represent(ntp, otn);
   3427 	}
   3428 	return false;
   3429 }
   3430 
   3431 static void
   3432 convert_integer_from_integer(op_t op, int arg, tspec_t nt, tspec_t ot,
   3433 			     type_t *tp, tnode_t *tn)
   3434 {
   3435 
   3436 	if (tn->tn_op == CON)
   3437 		return;
   3438 
   3439 	if (op == CVT)
   3440 		return;
   3441 
   3442 	if (Pflag && pflag && aflag > 0 &&
   3443 	    portable_rank_cmp(nt, ot) > 0 &&
   3444 	    is_uinteger(nt) != is_uinteger(ot)) {
   3445 		if (op == FARG) {
   3446 			/* conversion to '%s' may sign-extend ... */
   3447 			warning(297, type_name(tp), arg);
   3448 		} else {
   3449 			/* conversion to '%s' may sign-extend ... */
   3450 			warning(131, type_name(tp));
   3451 		}
   3452 	}
   3453 
   3454 	if (Pflag && portable_rank_cmp(nt, ot) > 0 &&
   3455 	    (tn->tn_op == PLUS || tn->tn_op == MINUS || tn->tn_op == MULT ||
   3456 	     tn->tn_op == SHL)) {
   3457 		/* suggest cast from '%s' to '%s' on op '%s' to ... */
   3458 		warning(324, type_name(gettyp(ot)), type_name(tp),
   3459 		    op_name(tn->tn_op));
   3460 	}
   3461 
   3462 	if (should_warn_about_integer_conversion(tp, nt, tn, ot)) {
   3463 		if (op == FARG) {
   3464 			/* conversion from '%s' to '%s' may lose ... */
   3465 			warning(298,
   3466 			    type_name(tn->tn_type), type_name(tp), arg);
   3467 		} else {
   3468 			/* conversion from '%s' to '%s' may lose accuracy */
   3469 			warning(132,
   3470 			    type_name(tn->tn_type), type_name(tp));
   3471 		}
   3472 	}
   3473 
   3474 	if (any_query_enabled && is_uinteger(nt) != is_uinteger(ot))
   3475 		/* implicit conversion changes sign from '%s' to '%s' */
   3476 		query_message(3, type_name(tn->tn_type), type_name(tp));
   3477 }
   3478 
   3479 static void
   3480 convert_integer_from_pointer(op_t op, tspec_t nt, type_t *tp, tnode_t *tn)
   3481 {
   3482 
   3483 	if (tn->tn_op == CON)
   3484 		return;
   3485 	if (op != CVT)
   3486 		return;		/* We already got an error. */
   3487 	if (portable_rank_cmp(nt, PTR) >= 0)
   3488 		return;
   3489 
   3490 	if (pflag && size_in_bits(nt) >= size_in_bits(PTR)) {
   3491 		/* conversion of pointer to '%s' may lose bits */
   3492 		warning(134, type_name(tp));
   3493 	} else {
   3494 		/* conversion of pointer to '%s' loses bits */
   3495 		warning(133, type_name(tp));
   3496 	}
   3497 }
   3498 
   3499 static bool
   3500 struct_starts_with(const type_t *struct_tp, const type_t *member_tp)
   3501 {
   3502 
   3503 	return struct_tp->t_sou->sou_first_member != NULL &&
   3504 	       types_compatible(struct_tp->t_sou->sou_first_member->s_type,
   3505 		   member_tp, true, false, NULL);
   3506 }
   3507 
   3508 static bool
   3509 is_byte_array(const type_t *tp)
   3510 {
   3511 
   3512 	return tp->t_tspec == ARRAY &&
   3513 	       (tp->t_subt->t_tspec == CHAR || tp->t_subt->t_tspec == UCHAR);
   3514 }
   3515 
   3516 static bool
   3517 union_contains(const type_t *utp, const type_t *mtp)
   3518 {
   3519 	for (const sym_t *mem = utp->t_sou->sou_first_member;
   3520 	     mem != NULL; mem = mem->s_next) {
   3521 		if (types_compatible(mem->s_type, mtp, true, false, NULL))
   3522 			return true;
   3523 	}
   3524 	return false;
   3525 }
   3526 
   3527 static bool
   3528 should_warn_about_pointer_cast(const type_t *nstp, tspec_t nst,
   3529 			       const type_t *ostp, tspec_t ost)
   3530 {
   3531 
   3532 	while (nst == ARRAY)
   3533 		nstp = nstp->t_subt, nst = nstp->t_tspec;
   3534 	while (ost == ARRAY)
   3535 		ostp = ostp->t_subt, ost = ostp->t_tspec;
   3536 
   3537 	if (nst == STRUCT && ost == STRUCT &&
   3538 	    (struct_starts_with(nstp, ostp) ||
   3539 	     struct_starts_with(ostp, nstp)))
   3540 		return false;
   3541 
   3542 	if (is_incomplete(nstp) || is_incomplete(ostp))
   3543 		return false;
   3544 
   3545 	if (nst == CHAR || nst == UCHAR)
   3546 		return false;	/* for the sake of traditional C code */
   3547 	if (ost == CHAR || ost == UCHAR)
   3548 		return false;	/* for the sake of traditional C code */
   3549 
   3550 	/* Allow cast between pointers to sockaddr variants. */
   3551 	if (nst == STRUCT && ost == STRUCT) {
   3552 		const sym_t *nmem = nstp->t_sou->sou_first_member;
   3553 		const sym_t *omem = ostp->t_sou->sou_first_member;
   3554 		while (nmem != NULL && omem != NULL &&
   3555 		       types_compatible(nmem->s_type, omem->s_type,
   3556 			   true, false, NULL))
   3557 			nmem = nmem->s_next, omem = omem->s_next;
   3558 		if (nmem != NULL && is_byte_array(nmem->s_type))
   3559 			return false;
   3560 		if (omem != NULL && is_byte_array(omem->s_type))
   3561 			return false;
   3562 		if (nmem == NULL && omem == NULL)
   3563 			return false;
   3564 	}
   3565 
   3566 	if (nst == UNION || ost == UNION) {
   3567 		const type_t *union_tp = nst == UNION ? nstp : ostp;
   3568 		const type_t *other_tp = nst == UNION ? ostp : nstp;
   3569 		if (union_contains(union_tp, other_tp))
   3570 			return false;
   3571 	}
   3572 
   3573 	if (is_struct_or_union(nst) && is_struct_or_union(ost))
   3574 		return nstp->t_sou != ostp->t_sou;
   3575 
   3576 	enum rank_kind rk1 = type_properties(nst)->tt_rank_kind;
   3577 	enum rank_kind rk2 = type_properties(ost)->tt_rank_kind;
   3578 	if (rk1 != rk2 || rk1 == RK_NONE)
   3579 		return true;
   3580 
   3581 	return portable_rank_cmp(nst, ost) != 0;
   3582 }
   3583 
   3584 static void
   3585 convert_pointer_from_pointer(type_t *ntp, tnode_t *tn)
   3586 {
   3587 	const type_t *nstp = ntp->t_subt;
   3588 	const type_t *otp = tn->tn_type;
   3589 	const type_t *ostp = otp->t_subt;
   3590 	tspec_t nst = nstp->t_tspec;
   3591 	tspec_t ost = ostp->t_tspec;
   3592 
   3593 	if (nst == VOID || ost == VOID) {
   3594 		/* TODO: C99 behaves like C90 here. */
   3595 		if ((!allow_trad && !allow_c99) && (nst == FUNC || ost == FUNC)) {
   3596 			const char *nts, *ots;
   3597 			/* null pointers are already handled in convert() */
   3598 			*(nst == FUNC ? &nts : &ots) = "function pointer";
   3599 			*(nst == VOID ? &nts : &ots) = "'void *'";
   3600 			/* conversion of %s to %s requires a cast */
   3601 			warning(303, ots, nts);
   3602 		}
   3603 		return;
   3604 	}
   3605 	if (nst == FUNC && ost == FUNC)
   3606 		return;
   3607 	if (nst == FUNC || ost == FUNC) {
   3608 		/* converting '%s' to '%s' is questionable */
   3609 		warning(229, type_name(otp), type_name(ntp));
   3610 		return;
   3611 	}
   3612 
   3613 	if (hflag && alignment_in_bits(nstp) > alignment_in_bits(ostp) &&
   3614 	    ost != CHAR && ost != UCHAR &&
   3615 	    !is_incomplete(ostp) &&
   3616 	    !(nst == UNION && union_contains(nstp, ostp))) {
   3617 		/* converting '%s' to '%s' increases alignment ... */
   3618 		warning(135, type_name(otp), type_name(ntp),
   3619 		    alignment_in_bits(ostp) / CHAR_SIZE,
   3620 		    alignment_in_bits(nstp) / CHAR_SIZE);
   3621 	}
   3622 
   3623 	if (cflag && should_warn_about_pointer_cast(nstp, nst, ostp, ost)) {
   3624 		/* pointer cast from '%s' to '%s' may be troublesome */
   3625 		warning(247, type_name(otp), type_name(ntp));
   3626 	}
   3627 }
   3628 
   3629 /*
   3630  * Insert a conversion operator, which converts the type of the node
   3631  * to another given type.
   3632  *
   3633  * Possible values for 'op':
   3634  *	CVT	a cast-expression
   3635  *	binary	integer promotion for one of the operands, or a usual
   3636  *		arithmetic conversion
   3637  *	binary	plain or compound assignments to bit-fields
   3638  *	FARG	'arg' is the number of the parameter (used for warnings)
   3639  *	NOOP	several other implicit conversions
   3640  *	...
   3641  */
   3642 tnode_t *
   3643 convert(op_t op, int arg, type_t *tp, tnode_t *tn)
   3644 {
   3645 	tspec_t nt = tp->t_tspec;
   3646 	tspec_t ot = tn->tn_type->t_tspec;
   3647 
   3648 	if (allow_trad && allow_c90 && op == FARG)
   3649 		check_prototype_conversion(arg, nt, ot, tp, tn);
   3650 
   3651 	if (nt == BOOL) {
   3652 		/* No further checks. */
   3653 
   3654 	} else if (is_integer(nt)) {
   3655 		if (ot == BOOL) {
   3656 			/* No further checks. */
   3657 		} else if (is_integer(ot))
   3658 			convert_integer_from_integer(op, arg, nt, ot, tp, tn);
   3659 		else if (is_floating(ot))
   3660 			convert_integer_from_floating(op, tp, tn);
   3661 		else if (ot == PTR)
   3662 			convert_integer_from_pointer(op, nt, tp, tn);
   3663 
   3664 	} else if (is_floating(nt)) {
   3665 		/* No further checks. */
   3666 
   3667 	} else if (nt == PTR) {
   3668 		if (is_null_pointer(tn)) {
   3669 			/* a null pointer may be assigned to any pointer. */
   3670 		} else if (ot == PTR && op == CVT)
   3671 			convert_pointer_from_pointer(tp, tn);
   3672 	}
   3673 
   3674 	tnode_t *ntn = expr_alloc_tnode();
   3675 	ntn->tn_op = CVT;
   3676 	ntn->tn_type = tp;
   3677 	ntn->tn_cast = op == CVT;
   3678 	ntn->tn_sys |= tn->tn_sys;
   3679 	ntn->tn_right = NULL;
   3680 	if (tn->tn_op != CON || nt == VOID) {
   3681 		ntn->tn_left = tn;
   3682 	} else {
   3683 		ntn->tn_op = CON;
   3684 		convert_constant(op, arg, ntn->tn_type, &ntn->tn_val,
   3685 		    &tn->tn_val);
   3686 	}
   3687 
   3688 	return ntn;
   3689 }
   3690 
   3691 static void
   3692 convert_constant_floating(op_t op, int arg, tspec_t ot, const type_t *tp,
   3693 			  tspec_t nt, val_t *v, val_t *nv)
   3694 {
   3695 	long double max = 0.0, min = 0.0;
   3696 
   3697 	switch (nt) {
   3698 	case CHAR:
   3699 		max = TARG_CHAR_MAX;	min = TARG_CHAR_MIN;	break;
   3700 	case UCHAR:
   3701 		max = TARG_UCHAR_MAX;	min = 0;		break;
   3702 	case SCHAR:
   3703 		max = TARG_SCHAR_MAX;	min = TARG_SCHAR_MIN;	break;
   3704 	case SHORT:
   3705 		max = TARG_SHRT_MAX;	min = TARG_SHRT_MIN;	break;
   3706 	case USHORT:
   3707 		max = TARG_USHRT_MAX;	min = 0;		break;
   3708 	case ENUM:
   3709 	case INT:
   3710 		max = TARG_INT_MAX;	min = TARG_INT_MIN;	break;
   3711 	case UINT:
   3712 		max = TARG_UINT_MAX;	min = 0;		break;
   3713 	case LONG:
   3714 		max = TARG_LONG_MAX;	min = TARG_LONG_MIN;	break;
   3715 	case ULONG:
   3716 		max = TARG_ULONG_MAX;	min = 0;		break;
   3717 	case LLONG:
   3718 		max = LLONG_MAX;	min = LLONG_MIN;	break;
   3719 	case ULLONG:
   3720 		max = ULLONG_MAX;	min = 0;		break;
   3721 	case FLOAT:
   3722 	case FCOMPLEX:
   3723 		max = FLT_MAX;		min = -FLT_MAX;		break;
   3724 	case DOUBLE:
   3725 	case DCOMPLEX:
   3726 		max = DBL_MAX;		min = -DBL_MAX;		break;
   3727 	case PTR:
   3728 		/* Already got an error because of float --> ptr */
   3729 	case LDOUBLE:
   3730 	case LCOMPLEX:
   3731 		/* LINTED 248 */
   3732 		max = LDBL_MAX;		min = -max;		break;
   3733 	default:
   3734 		lint_assert(/*CONSTCOND*/false);
   3735 	}
   3736 	if (v->u.floating > max || v->u.floating < min) {
   3737 		lint_assert(nt != LDOUBLE);
   3738 		if (op == FARG) {
   3739 			/* conversion of '%s' to '%s' is out of range, ... */
   3740 			warning(295,
   3741 			    type_name(gettyp(ot)), type_name(tp), arg);
   3742 		} else {
   3743 			/* conversion of '%s' to '%s' is out of range */
   3744 			warning(119, type_name(gettyp(ot)), type_name(tp));
   3745 		}
   3746 		v->u.floating = v->u.floating > 0 ? max : min;
   3747 	}
   3748 
   3749 	if (nt == FLOAT || nt == FCOMPLEX)
   3750 		nv->u.floating = (float)v->u.floating;
   3751 	else if (nt == DOUBLE || nt == DCOMPLEX)
   3752 		nv->u.floating = (double)v->u.floating;
   3753 	else if (nt == LDOUBLE || nt == LCOMPLEX)
   3754 		nv->u.floating = v->u.floating;
   3755 	else
   3756 		nv->u.integer = (int64_t)v->u.floating;
   3757 }
   3758 
   3759 static bool
   3760 convert_constant_to_floating(tspec_t nt, val_t *nv,
   3761 			     tspec_t ot, const val_t *v)
   3762 {
   3763 	if (nt == FLOAT) {
   3764 		nv->u.floating = (ot == PTR || is_uinteger(ot)) ?
   3765 		    (float)(uint64_t)v->u.integer : (float)v->u.integer;
   3766 	} else if (nt == DOUBLE) {
   3767 		nv->u.floating = (ot == PTR || is_uinteger(ot)) ?
   3768 		    (double)(uint64_t)v->u.integer : (double)v->u.integer;
   3769 	} else if (nt == LDOUBLE) {
   3770 		nv->u.floating = (ot == PTR || is_uinteger(ot))
   3771 		    ? (long double)(uint64_t)v->u.integer
   3772 		    : (long double)v->u.integer;
   3773 	} else
   3774 		return false;
   3775 	return true;
   3776 }
   3777 
   3778 /*
   3779  * Print a warning if bits which were set are lost due to the conversion.
   3780  * This can happen with operator ORASS only.
   3781  */
   3782 static void
   3783 convert_constant_check_range_bitor(size_t nsz, size_t osz, const val_t *v,
   3784 				   uint64_t xmask, op_t op)
   3785 {
   3786 	if (nsz < osz && (v->u.integer & xmask) != 0) {
   3787 		/* constant truncated by conversion, op '%s' */
   3788 		warning(306, op_name(op));
   3789 	}
   3790 }
   3791 
   3792 /*
   3793  * Print a warning if additional bits are not all 1
   3794  * and the most significant bit of the old value is 1,
   3795  * or if at least one (but not all) removed bit was 0.
   3796  */
   3797 static void
   3798 convert_constant_check_range_bitand(size_t nsz, size_t osz,
   3799 				    uint64_t xmask, const val_t *nv,
   3800 				    tspec_t ot, const val_t *v,
   3801 				    const type_t *tp, op_t op)
   3802 {
   3803 	if (nsz > osz &&
   3804 	    (nv->u.integer & bit((unsigned int)(osz - 1))) != 0 &&
   3805 	    (nv->u.integer & xmask) != xmask) {
   3806 		/* extra bits set to 0 in conversion of '%s' to '%s', ... */
   3807 		warning(309, type_name(gettyp(ot)),
   3808 		    type_name(tp), op_name(op));
   3809 	} else if (nsz < osz &&
   3810 		   (v->u.integer & xmask) != xmask &&
   3811 		   (v->u.integer & xmask) != 0) {
   3812 		/* constant truncated by conversion, op '%s' */
   3813 		warning(306, op_name(op));
   3814 	}
   3815 }
   3816 
   3817 static void
   3818 convert_constant_check_range_signed(op_t op, int arg)
   3819 {
   3820 	if (op == ASSIGN) {
   3821 		/* assignment of negative constant to unsigned type */
   3822 		warning(164);
   3823 	} else if (op == INIT) {
   3824 		/* initialization of unsigned with negative constant */
   3825 		warning(221);
   3826 	} else if (op == FARG) {
   3827 		/* conversion of negative constant to unsigned type, ... */
   3828 		warning(296, arg);
   3829 	} else if (modtab[op].m_comparison) {
   3830 		/* handled by check_integer_comparison() */
   3831 	} else {
   3832 		/* conversion of negative constant to unsigned type */
   3833 		warning(222);
   3834 	}
   3835 }
   3836 
   3837 /*
   3838  * Loss of significant bit(s). All truncated bits of unsigned types or all
   3839  * truncated bits plus the msb of the target for signed types are considered
   3840  * to be significant bits. Loss of significant bits means that at least one
   3841  * of the bits was set in an unsigned type or that at least one but not all
   3842  * of the bits was set in a signed type. Loss of significant bits means that
   3843  * it is not possible, also not with necessary casts, to convert back to the
   3844  * original type. An example for a necessary cast is:
   3845  *	char c;	int	i; c = 128;
   3846  *	i = c;			** yields -128 **
   3847  *	i = (unsigned char)c;	** yields 128 **
   3848  */
   3849 static void
   3850 warn_constant_check_range_truncated(op_t op, int arg, const type_t *tp,
   3851 				    tspec_t ot)
   3852 {
   3853 	if (op == ASSIGN && tp->t_bitfield)
   3854 		/* precision lost in bit-field assignment */
   3855 		warning(166);
   3856 	else if (op == ASSIGN)
   3857 		/* constant truncated by assignment */
   3858 		warning(165);
   3859 	else if (op == INIT && tp->t_bitfield)
   3860 		/* bit-field initializer does not fit */
   3861 		warning(180);
   3862 	else if (op == INIT)
   3863 		/* initializer does not fit */
   3864 		warning(178);
   3865 	else if (op == CASE)
   3866 		/* case label affected by conversion */
   3867 		warning(196);
   3868 	else if (op == FARG)
   3869 		/* conversion of '%s' to '%s' is out of range, arg #%d */
   3870 		warning(295, type_name(gettyp(ot)), type_name(tp), arg);
   3871 	else
   3872 		/* conversion of '%s' to '%s' is out of range */
   3873 		warning(119, type_name(gettyp(ot)), type_name(tp));
   3874 }
   3875 
   3876 static void
   3877 warn_constant_check_range_loss(op_t op, int arg, const type_t *tp,
   3878 				  tspec_t ot)
   3879 {
   3880 	if (op == ASSIGN && tp->t_bitfield)
   3881 		/* precision lost in bit-field assignment */
   3882 		warning(166);
   3883 	else if (op == INIT && tp->t_bitfield)
   3884 		/* bit-field initializer out of range */
   3885 		warning(11);
   3886 	else if (op == CASE)
   3887 		/* case label affected by conversion */
   3888 		warning(196);
   3889 	else if (op == FARG)
   3890 		/* conversion of '%s' to '%s' is out of range, arg #%d */
   3891 		warning(295, type_name(gettyp(ot)), type_name(tp), arg);
   3892 	else
   3893 		/* conversion of '%s' to '%s' is out of range */
   3894 		warning(119, type_name(gettyp(ot)), type_name(tp));
   3895 }
   3896 
   3897 static void
   3898 convert_constant_check_range(tspec_t ot, const type_t *tp, tspec_t nt,
   3899 			     op_t op, int arg, const val_t *v, val_t *nv)
   3900 {
   3901 	unsigned int obitsz, nbitsz;
   3902 	uint64_t xmask, xmsk1;
   3903 
   3904 	obitsz = size_in_bits(ot);
   3905 	nbitsz = tp->t_bitfield ? tp->t_bit_field_width : size_in_bits(nt);
   3906 	xmask = value_bits(nbitsz) ^ value_bits(obitsz);
   3907 	xmsk1 = value_bits(nbitsz) ^ value_bits(obitsz - 1);
   3908 	/*
   3909 	 * For bitwise operations we are not interested in the arithmetic
   3910 	 * value, but in the bits itself.
   3911 	 */
   3912 	if (op == ORASS || op == BITOR || op == BITXOR) {
   3913 		convert_constant_check_range_bitor(
   3914 		    nbitsz, obitsz, v, xmask, op);
   3915 	} else if (op == ANDASS || op == BITAND) {
   3916 		convert_constant_check_range_bitand(
   3917 		    nbitsz, obitsz, xmask, nv, ot, v, tp, op);
   3918 	} else if ((nt != PTR && is_uinteger(nt)) &&
   3919 		   (ot != PTR && !is_uinteger(ot)) &&
   3920 		   v->u.integer < 0)
   3921 		convert_constant_check_range_signed(op, arg);
   3922 	else if (nv->u.integer != v->u.integer && nbitsz <= obitsz &&
   3923 		 (v->u.integer & xmask) != 0 &&
   3924 		 (is_uinteger(ot) || (v->u.integer & xmsk1) != xmsk1))
   3925 		warn_constant_check_range_truncated(op, arg, tp, ot);
   3926 	else if (nv->u.integer != v->u.integer)
   3927 		warn_constant_check_range_loss(op, arg, tp, ot);
   3928 }
   3929 
   3930 /*-
   3931  * Converts a typed constant to a constant of another type.
   3932  *
   3933  * op		operator which requires conversion
   3934  * arg		if op is FARG, # of parameter
   3935  * tp		type to which to convert the constant
   3936  * nv		new constant
   3937  * v		old constant
   3938  */
   3939 void
   3940 convert_constant(op_t op, int arg, const type_t *tp, val_t *nv, val_t *v)
   3941 {
   3942 	/*
   3943 	 * TODO: make 'v' const; the name of this function does not suggest
   3944 	 *  that it modifies 'v'.
   3945 	 */
   3946 	tspec_t ot = v->v_tspec;
   3947 	tspec_t nt = nv->v_tspec = tp->t_tspec;
   3948 	bool range_check = false;
   3949 
   3950 	if (nt == BOOL) {	/* C99 6.3.1.2 */
   3951 		nv->v_unsigned_since_c90 = false;
   3952 		nv->u.integer = is_nonzero_val(v) ? 1 : 0;
   3953 		return;
   3954 	}
   3955 
   3956 	if (ot == FLOAT || ot == DOUBLE || ot == LDOUBLE)
   3957 		convert_constant_floating(op, arg, ot, tp, nt, v, nv);
   3958 	else if (!convert_constant_to_floating(nt, nv, ot, v)) {
   3959 		range_check = true;	/* Check for lost precision. */
   3960 		nv->u.integer = v->u.integer;
   3961 	}
   3962 
   3963 	if (allow_trad && allow_c90 && v->v_unsigned_since_c90 &&
   3964 	    (is_floating(nt) || (
   3965 		(is_integer(nt) && !is_uinteger(nt) &&
   3966 		    portable_rank_cmp(nt, ot) > 0)))) {
   3967 		/* C90 treats constant as unsigned */
   3968 		warning(157);
   3969 		v->v_unsigned_since_c90 = false;
   3970 	}
   3971 
   3972 	if (is_integer(nt)) {
   3973 		nv->u.integer = convert_integer(nv->u.integer, nt,
   3974 		    tp->t_bitfield ? tp->t_bit_field_width : size_in_bits(nt));
   3975 	}
   3976 
   3977 	if (range_check && op != CVT)
   3978 		convert_constant_check_range(ot, tp, nt, op, arg, v, nv);
   3979 }
   3980 
   3981 /*
   3982  * Create a constant node for sizeof.
   3983  */
   3984 tnode_t *
   3985 build_sizeof(const type_t *tp)
   3986 {
   3987 	unsigned int size_in_bytes = type_size_in_bits(tp) / CHAR_SIZE;
   3988 	tnode_t *tn = build_integer_constant(SIZEOF_TSPEC, size_in_bytes);
   3989 	tn->tn_system_dependent = true;
   3990 	debug_step("build_sizeof '%s' = %u", type_name(tp), size_in_bytes);
   3991 	return tn;
   3992 }
   3993 
   3994 /*
   3995  * Create a constant node for offsetof.
   3996  */
   3997 tnode_t *
   3998 build_offsetof(const type_t *tp, const sym_t *sym)
   3999 {
   4000 	unsigned int offset_in_bits = 0;
   4001 
   4002 	if (!is_struct_or_union(tp->t_tspec)) {
   4003 		/* unacceptable operand of '%s' */
   4004 		error(111, "offsetof");
   4005 		goto proceed;
   4006 	}
   4007 	sym_t *mem = find_member(tp->t_sou, sym->s_name);
   4008 	if (mem == NULL) {
   4009 		/* type '%s' does not have member '%s' */
   4010 		error(101, sym->s_name, type_name(tp));
   4011 		goto proceed;
   4012 	}
   4013 	offset_in_bits = mem->u.s_member.sm_offset_in_bits;
   4014 
   4015 proceed:;
   4016 	unsigned int offset_in_bytes = offset_in_bits / CHAR_SIZE;
   4017 	tnode_t *tn = build_integer_constant(SIZEOF_TSPEC, offset_in_bytes);
   4018 	tn->tn_system_dependent = true;
   4019 	return tn;
   4020 }
   4021 
   4022 unsigned int
   4023 type_size_in_bits(const type_t *tp)
   4024 {
   4025 
   4026 	unsigned int elem = 1;
   4027 	bool flex = false;
   4028 	lint_assert(tp != NULL);
   4029 	while (tp->t_tspec == ARRAY) {
   4030 		flex = true;	/* allow c99 flex arrays [] [0] */
   4031 		elem *= tp->t_dim;
   4032 		tp = tp->t_subt;
   4033 	}
   4034 	if (elem == 0 && !flex) {
   4035 		/* cannot take size/alignment of incomplete type */
   4036 		error(143);
   4037 		elem = 1;
   4038 	}
   4039 
   4040 	unsigned int elsz;
   4041 	switch (tp->t_tspec) {
   4042 	case VOID:
   4043 		/* cannot take size/alignment of void */
   4044 		error(146);
   4045 		elsz = 1;
   4046 		break;
   4047 	case FUNC:
   4048 		/* cannot take size/alignment of function type '%s' */
   4049 		error(144, type_name(tp));
   4050 		elsz = 1;
   4051 		break;
   4052 	case STRUCT:
   4053 	case UNION:
   4054 		if (is_incomplete(tp)) {
   4055 			/* cannot take size/alignment of incomplete type */
   4056 			error(143);
   4057 			elsz = 1;
   4058 		} else {
   4059 			elsz = tp->t_sou->sou_size_in_bits;
   4060 		}
   4061 		break;
   4062 	case ENUM:
   4063 		if (is_incomplete(tp)) {
   4064 			/* cannot take size/alignment of incomplete type */
   4065 			warning(143);
   4066 		}
   4067 		/* FALLTHROUGH */
   4068 	default:
   4069 		if (tp->t_bitfield) {
   4070 			/* cannot take size/alignment of bit-field */
   4071 			error(145);
   4072 		}
   4073 		elsz = size_in_bits(tp->t_tspec);
   4074 		lint_assert(elsz > 0);
   4075 		break;
   4076 	}
   4077 
   4078 	return elem * elsz;
   4079 }
   4080 
   4081 /* C11 6.5.3.4, GCC */
   4082 tnode_t *
   4083 build_alignof(const type_t *tp)
   4084 {
   4085 	if (tp->t_tspec == FUNC) {
   4086 		/* cannot take size/alignment of function type '%s' */
   4087 		error(144, type_name(tp));
   4088 		return NULL;
   4089 	}
   4090 	if (tp->t_tspec == VOID) {
   4091 		/* cannot take size/alignment of void */
   4092 		error(146);
   4093 		return NULL;
   4094 	}
   4095 	if (is_incomplete(tp)) {
   4096 		/* cannot take size/alignment of incomplete type */
   4097 		error(143);
   4098 		return NULL;
   4099 	}
   4100 	if (tp->t_bitfield) {
   4101 		/* cannot take size/alignment of bit-field */
   4102 		error(145);
   4103 		return NULL;
   4104 	}
   4105 	return build_integer_constant(SIZEOF_TSPEC,
   4106 	    (int64_t)alignment_in_bits(tp) / CHAR_SIZE);
   4107 }
   4108 
   4109 static tnode_t *
   4110 cast_to_union(tnode_t *otn, type_t *ntp)
   4111 {
   4112 
   4113 	if (!allow_gcc) {
   4114 		/* union cast is a GCC extension */
   4115 		error(328);
   4116 		return NULL;
   4117 	}
   4118 
   4119 	for (const sym_t *m = ntp->t_sou->sou_first_member;
   4120 	    m != NULL; m = m->s_next) {
   4121 		if (types_compatible(m->s_type, otn->tn_type,
   4122 		    false, false, NULL)) {
   4123 			tnode_t *ntn = expr_alloc_tnode();
   4124 			ntn->tn_op = CVT;
   4125 			ntn->tn_type = ntp;
   4126 			ntn->tn_cast = true;
   4127 			ntn->tn_left = otn;
   4128 			ntn->tn_right = NULL;
   4129 			return ntn;
   4130 		}
   4131 	}
   4132 
   4133 	/* type '%s' is not a member of '%s' */
   4134 	error(329, type_name(otn->tn_type), type_name(ntp));
   4135 	return NULL;
   4136 }
   4137 
   4138 /*
   4139  * Type casts.
   4140  */
   4141 tnode_t *
   4142 cast(tnode_t *tn, type_t *tp)
   4143 {
   4144 
   4145 	if (tn == NULL)
   4146 		return NULL;
   4147 
   4148 	tn = cconv(tn);
   4149 
   4150 	lint_assert(tp != NULL);
   4151 	tspec_t nt = tp->t_tspec;
   4152 	tspec_t ot = tn->tn_type->t_tspec;
   4153 
   4154 	if (nt == VOID) {
   4155 		/*
   4156 		 * C90 6.3.4, C99 6.5.4p2 and C11 6.5.4p2 allow any type to
   4157 		 * be cast to void.  The only other allowed casts are from a
   4158 		 * scalar type to a scalar type.
   4159 		 */
   4160 	} else if (nt == UNION)
   4161 		return cast_to_union(tn, tp);
   4162 	else if (nt == STRUCT || nt == ARRAY || nt == FUNC) {
   4163 		/* Casting to a struct is an undocumented GCC extension. */
   4164 		if (!(allow_gcc && nt == STRUCT))
   4165 			goto invalid_cast;
   4166 	} else if (is_struct_or_union(ot))
   4167 		goto invalid_cast;
   4168 	else if (ot == VOID) {
   4169 		/* improper cast of void expression */
   4170 		error(148);
   4171 		return NULL;
   4172 	} else if (is_integer(nt) && is_scalar(ot)) {
   4173 		/* ok */
   4174 	} else if (is_floating(nt) && is_arithmetic(ot)) {
   4175 		/* ok */
   4176 	} else if (nt == PTR && is_integer(ot)) {
   4177 		/* ok */
   4178 	} else if (nt == PTR && ot == PTR) {
   4179 		if (!tp->t_subt->t_const && tn->tn_type->t_subt->t_const) {
   4180 			if (hflag)
   4181 				/* cast discards 'const' from type '%s' */
   4182 				warning(275, type_name(tn->tn_type));
   4183 		}
   4184 	} else
   4185 		goto invalid_cast;
   4186 
   4187 	if (any_query_enabled && types_compatible(tp, tn->tn_type,
   4188 	    false, false, NULL)) {
   4189 		/* no-op cast from '%s' to '%s' */
   4190 		query_message(6, type_name(tn->tn_type), type_name(tp));
   4191 	}
   4192 
   4193 	tn = convert(CVT, 0, tp, tn);
   4194 	tn->tn_cast = true;
   4195 
   4196 	return tn;
   4197 
   4198 invalid_cast:
   4199 	/* invalid cast from '%s' to '%s' */
   4200 	error(147, type_name(tn->tn_type), type_name(tp));
   4201 	return NULL;
   4202 }
   4203 
   4204 /*
   4205  * Create the node for a function argument.
   4206  * All necessary conversions and type checks are done in
   4207  * build_function_call because build_function_argument has no
   4208  * information about the expected parameter types.
   4209  */
   4210 tnode_t *
   4211 build_function_argument(tnode_t *args, tnode_t *arg)
   4212 {
   4213 	/*
   4214 	 * If there was a serious error in the expression for the argument,
   4215 	 * create a dummy argument so the positions of the remaining arguments
   4216 	 * will not change.
   4217 	 */
   4218 	if (arg == NULL)
   4219 		arg = build_integer_constant(INT, 0);
   4220 
   4221 	return build_op(PUSH, arg->tn_sys, arg->tn_type, arg, args);
   4222 }
   4223 
   4224 /*
   4225  * Compare the type of an argument with the corresponding type of a
   4226  * prototype parameter. If it is a valid combination, but both types
   4227  * are not the same, insert a conversion to convert the argument into
   4228  * the type of the parameter.
   4229  */
   4230 static tnode_t *
   4231 check_prototype_argument(
   4232 	int	n,		/* pos of arg */
   4233 	type_t	*tp,		/* expected type (from prototype) */
   4234 	tnode_t	*tn)		/* argument */
   4235 {
   4236 	tnode_t *ln = xcalloc(1, sizeof(*ln));
   4237 	ln->tn_type = expr_unqualified_type(tp);
   4238 	ln->tn_lvalue = true;
   4239 	if (typeok(FARG, n, ln, tn)) {
   4240 		bool dowarn;
   4241 		if (!types_compatible(tp, tn->tn_type,
   4242 		    true, false, (dowarn = false, &dowarn)) || dowarn)
   4243 			tn = convert(FARG, n, tp, tn);
   4244 	}
   4245 	free(ln);
   4246 	return tn;
   4247 }
   4248 
   4249 /*
   4250  * Check types of all function arguments and insert conversions,
   4251  * if necessary.
   4252  */
   4253 static tnode_t *
   4254 check_function_arguments(type_t *ftp, tnode_t *args)
   4255 {
   4256 	/* get # of parameters in the prototype */
   4257 	int npar = 0;
   4258 	for (const sym_t *p = ftp->t_params; p != NULL; p = p->s_next)
   4259 		npar++;
   4260 
   4261 	/* get # of arguments in the function call */
   4262 	int narg = 0;
   4263 	for (const tnode_t *arg = args; arg != NULL; arg = arg->tn_right)
   4264 		narg++;
   4265 
   4266 	const sym_t *param = ftp->t_params;
   4267 	if (ftp->t_proto && npar != narg && !(ftp->t_vararg && npar < narg)) {
   4268 		/* argument mismatch: %d %s passed, %d expected */
   4269 		error(150, narg, narg != 1 ? "arguments" : "argument", npar);
   4270 		param = NULL;
   4271 	}
   4272 
   4273 	for (int n = 1; n <= narg; n++) {
   4274 
   4275 		// The rightmost argument starts the argument list.
   4276 		tnode_t *arg = args;
   4277 		for (int i = narg; i > n; i--, arg = arg->tn_right)
   4278 			continue;
   4279 
   4280 		/* some things which are always not allowed */
   4281 		tspec_t at = arg->tn_left->tn_type->t_tspec;
   4282 		if (at == VOID) {
   4283 			/* void expressions may not be arguments, arg #%d */
   4284 			error(151, n);
   4285 			return NULL;
   4286 		}
   4287 		if (is_struct_or_union(at) &&
   4288 			   is_incomplete(arg->tn_left->tn_type)) {
   4289 			/* argument cannot have unknown size, arg #%d */
   4290 			error(152, n);
   4291 			return NULL;
   4292 		}
   4293 		if (is_integer(at) &&
   4294 			   arg->tn_left->tn_type->t_is_enum &&
   4295 			   is_incomplete(arg->tn_left->tn_type)) {
   4296 			/* argument cannot have unknown size, arg #%d */
   4297 			warning(152, n);
   4298 		}
   4299 
   4300 		/* class conversions (arg in value context) */
   4301 		arg->tn_left = cconv(arg->tn_left);
   4302 
   4303 		if (param != NULL) {
   4304 			arg->tn_left = check_prototype_argument(
   4305 			    n, param->s_type, arg->tn_left);
   4306 		} else
   4307 			arg->tn_left = promote(NOOP, true, arg->tn_left);
   4308 		arg->tn_type = arg->tn_left->tn_type;
   4309 
   4310 		if (param != NULL)
   4311 			param = param->s_next;
   4312 	}
   4313 
   4314 	return args;
   4315 }
   4316 
   4317 /*
   4318  * Create the node for a function call. Also check types of
   4319  * function arguments and insert conversions, if necessary.
   4320  */
   4321 tnode_t *
   4322 build_function_call(tnode_t *func, bool sys, tnode_t *args)
   4323 {
   4324 
   4325 	if (func == NULL)
   4326 		return NULL;
   4327 
   4328 	op_t fcop = func->tn_op == NAME && func->tn_type->t_tspec == FUNC
   4329 	    ? CALL : ICALL;
   4330 
   4331 	check_ctype_function_call(func, args);
   4332 
   4333 	/* Turn the function name into a pointer to the function. */
   4334 	func = cconv(func);
   4335 
   4336 	if (func->tn_type->t_tspec != PTR ||
   4337 	    func->tn_type->t_subt->t_tspec != FUNC) {
   4338 		/* cannot call '%s', must be a function */
   4339 		error(149, type_name(func->tn_type));
   4340 		return NULL;
   4341 	}
   4342 
   4343 	args = check_function_arguments(func->tn_type->t_subt, args);
   4344 
   4345 	return build_op(fcop, sys, func->tn_type->t_subt->t_subt, func, args);
   4346 }
   4347 
   4348 /*
   4349  * Return the value of an integral constant expression.
   4350  * If the expression is not constant or its type is not an integer
   4351  * type, an error message is printed.
   4352  */
   4353 val_t *
   4354 integer_constant(tnode_t *tn, bool required)
   4355 {
   4356 
   4357 	if (tn != NULL)
   4358 		tn = cconv(tn);
   4359 	if (tn != NULL)
   4360 		tn = promote(NOOP, false, tn);
   4361 
   4362 	val_t *v = xcalloc(1, sizeof(*v));
   4363 
   4364 	if (tn == NULL) {
   4365 		lint_assert(seen_error);
   4366 		debug_step("constant node is null; returning 1 instead");
   4367 		v->v_tspec = INT;
   4368 		v->u.integer = 1;
   4369 		return v;
   4370 	}
   4371 
   4372 	v->v_tspec = tn->tn_type->t_tspec;
   4373 
   4374 	if (tn->tn_op == CON) {
   4375 		lint_assert(tn->tn_type->t_tspec == tn->tn_val.v_tspec);
   4376 		if (is_integer(tn->tn_val.v_tspec)) {
   4377 			v->v_unsigned_since_c90 =
   4378 			    tn->tn_val.v_unsigned_since_c90;
   4379 			v->u.integer = tn->tn_val.u.integer;
   4380 			return v;
   4381 		}
   4382 		v->u.integer = (int64_t)tn->tn_val.u.floating;
   4383 	} else {
   4384 		v->u.integer = 1;
   4385 	}
   4386 
   4387 	if (required)
   4388 		/* integral constant expression expected */
   4389 		error(55);
   4390 	else
   4391 		/* variable array dimension is a C99/GCC extension */
   4392 		c99ism(318);
   4393 
   4394 	if (!is_integer(v->v_tspec))
   4395 		v->v_tspec = INT;
   4396 
   4397 	return v;
   4398 }
   4399 
   4400 static bool
   4401 is_constcond_false(const tnode_t *tn, tspec_t t)
   4402 {
   4403 	return (t == BOOL || t == INT) &&
   4404 	       tn->tn_op == CON && tn->tn_val.u.integer == 0;
   4405 }
   4406 
   4407 /*
   4408  * Perform some tests on expressions which can't be done in build_binary()
   4409  * and functions called by build_binary(). These tests must be done here
   4410  * because we need some information about the context in which the operations
   4411  * are performed.
   4412  * After all tests are performed and dofreeblk is true, expr() frees the
   4413  * memory which is used for the expression.
   4414  */
   4415 void
   4416 expr(tnode_t *tn, bool vctx, bool cond, bool dofreeblk, bool is_do_while)
   4417 {
   4418 
   4419 	if (tn == NULL) {	/* in case of errors */
   4420 		expr_free_all();
   4421 		return;
   4422 	}
   4423 
   4424 	/* expr() is also called in global initializations */
   4425 	if (dcs->d_kind != DLK_EXTERN && !is_do_while)
   4426 		check_statement_reachable();
   4427 
   4428 	check_expr_misc(tn, vctx, cond, !cond, false, false, false);
   4429 	if (tn->tn_op == ASSIGN && !tn->tn_parenthesized) {
   4430 		if (hflag && cond)
   4431 			/* assignment in conditional context */
   4432 			warning(159);
   4433 	} else if (tn->tn_op == CON) {
   4434 		if (hflag && cond && !suppress_constcond &&
   4435 		    !tn->tn_system_dependent &&
   4436 		    !(is_do_while &&
   4437 		      is_constcond_false(tn, tn->tn_type->t_tspec)))
   4438 			/* constant in conditional context */
   4439 			warning(161);
   4440 	}
   4441 	if (!modtab[tn->tn_op].m_has_side_effect) {
   4442 		/*
   4443 		 * for left operands of COMMA this warning is already
   4444 		 * printed
   4445 		 */
   4446 		if (tn->tn_op != COMMA && !vctx && !cond)
   4447 			check_null_effect(tn);
   4448 	}
   4449 	debug_node(tn);
   4450 
   4451 	/* free the tree memory */
   4452 	if (dofreeblk)
   4453 		expr_free_all();
   4454 }
   4455 
   4456 /*
   4457  * Checks the range of array indices, if possible.
   4458  * amper is set if only the address of the element is used. This
   4459  * means that the index is allowed to refer to the first element
   4460  * after the array.
   4461  */
   4462 static void
   4463 check_array_index(tnode_t *tn, bool amper)
   4464 {
   4465 	const tnode_t *ln = tn->tn_left;
   4466 	const tnode_t *rn = tn->tn_right;
   4467 
   4468 	/* We can only check constant indices. */
   4469 	if (rn->tn_op != CON)
   4470 		return;
   4471 
   4472 	/* Return if the left node does not stem from an array. */
   4473 	if (ln->tn_op != ADDR)
   4474 		return;
   4475 	if (ln->tn_left->tn_op != STRING && ln->tn_left->tn_op != NAME)
   4476 		return;
   4477 	if (ln->tn_left->tn_type->t_tspec != ARRAY)
   4478 		return;
   4479 
   4480 	/*
   4481 	 * For incomplete array types, we can print a warning only if
   4482 	 * the index is negative.
   4483 	 */
   4484 	if (is_incomplete(ln->tn_left->tn_type) && rn->tn_val.u.integer >= 0)
   4485 		return;
   4486 
   4487 	/* Get the size of one array element */
   4488 	int elsz = length_in_bits(ln->tn_type->t_subt, NULL);
   4489 	if (elsz == 0)
   4490 		return;
   4491 	elsz /= CHAR_SIZE;
   4492 
   4493 	/* Change the unit of the index from bytes to element size. */
   4494 	int64_t con = is_uinteger(rn->tn_type->t_tspec)
   4495 	    ? (int64_t)((uint64_t)rn->tn_val.u.integer / elsz)
   4496 	    : rn->tn_val.u.integer / elsz;
   4497 
   4498 	int dim = ln->tn_left->tn_type->t_dim + (amper ? 1 : 0);
   4499 
   4500 	if (!is_uinteger(rn->tn_type->t_tspec) && con < 0) {
   4501 		/* array subscript cannot be negative: %ld */
   4502 		warning(167, (long)con);
   4503 	} else if (dim > 0 && (uint64_t)con >= (uint64_t)dim) {
   4504 		/* array subscript cannot be > %d: %ld */
   4505 		warning(168, dim - 1, (long)con);
   4506 	}
   4507 }
   4508 
   4509 static void
   4510 check_expr_addr(const tnode_t *ln, bool szof, bool fcall)
   4511 {
   4512 	/* XXX: Taking warn_about_unreachable into account here feels wrong. */
   4513 	if (ln->tn_op == NAME && (reached || !warn_about_unreachable)) {
   4514 		if (!szof)
   4515 			mark_as_set(ln->tn_sym);
   4516 		mark_as_used(ln->tn_sym, fcall, szof);
   4517 	}
   4518 	if (ln->tn_op == INDIR && ln->tn_left->tn_op == PLUS)
   4519 		/* check the range of array indices */
   4520 		check_array_index(ln->tn_left, true);
   4521 }
   4522 
   4523 static void
   4524 check_expr_load(const tnode_t *ln)
   4525 {
   4526 	if (ln->tn_op == INDIR && ln->tn_left->tn_op == PLUS)
   4527 		/* check the range of array indices */
   4528 		check_array_index(ln->tn_left, false);
   4529 }
   4530 
   4531 /*
   4532  * If there is an asm statement in one of the compound statements around,
   4533  * there may be other side effects, so don't warn.
   4534  */
   4535 static bool
   4536 is_asm_around(void)
   4537 {
   4538 	for (decl_level *dl = dcs; dl != NULL; dl = dl->d_enclosing)
   4539 		if (dl->d_asm)
   4540 			return true;
   4541 	return false;
   4542 }
   4543 
   4544 static void
   4545 check_expr_side_effect(const tnode_t *ln, bool szof)
   4546 {
   4547 
   4548 	/* XXX: Taking warn_about_unreachable into account here feels wrong. */
   4549 	if (ln->tn_op == NAME && (reached || !warn_about_unreachable)) {
   4550 		scl_t sc = ln->tn_sym->s_scl;
   4551 		if (sc != EXTERN && sc != STATIC &&
   4552 		    !ln->tn_sym->s_set && !szof && !is_asm_around()) {
   4553 			/* '%s' may be used before set */
   4554 			warning(158, ln->tn_sym->s_name);
   4555 			mark_as_set(ln->tn_sym);
   4556 		}
   4557 		mark_as_used(ln->tn_sym, false, false);
   4558 	}
   4559 }
   4560 
   4561 static void
   4562 check_expr_assign(const tnode_t *ln, bool szof)
   4563 {
   4564 	/* XXX: Taking warn_about_unreachable into account here feels wrong. */
   4565 	if (ln->tn_op == NAME && !szof && (reached || !warn_about_unreachable)) {
   4566 		mark_as_set(ln->tn_sym);
   4567 		if (ln->tn_sym->s_scl == EXTERN)
   4568 			outusg(ln->tn_sym);
   4569 	}
   4570 	if (ln->tn_op == INDIR && ln->tn_left->tn_op == PLUS)
   4571 		/* check the range of array indices */
   4572 		check_array_index(ln->tn_left, false);
   4573 }
   4574 
   4575 static void
   4576 check_expr_call(const tnode_t *tn, const tnode_t *ln,
   4577 		bool szof, bool vctx, bool cond, bool retval_discarded)
   4578 {
   4579 	lint_assert(ln->tn_op == ADDR);
   4580 	lint_assert(ln->tn_left->tn_op == NAME);
   4581 	if (!szof && !is_compiler_builtin(ln->tn_left->tn_sym->s_name))
   4582 		outcall(tn, vctx || cond, retval_discarded);
   4583 }
   4584 
   4585 static bool
   4586 check_expr_op(const tnode_t *tn, op_t op, const tnode_t *ln,
   4587 	      bool szof, bool fcall, bool vctx, bool cond,
   4588 	      bool retval_discarded, bool eqwarn)
   4589 {
   4590 	switch (op) {
   4591 	case ADDR:
   4592 		check_expr_addr(ln, szof, fcall);
   4593 		break;
   4594 	case LOAD:
   4595 		check_expr_load(ln);
   4596 		/* FALLTHROUGH */
   4597 	case PUSH:
   4598 	case INCBEF:
   4599 	case DECBEF:
   4600 	case INCAFT:
   4601 	case DECAFT:
   4602 	case ADDASS:
   4603 	case SUBASS:
   4604 	case MULASS:
   4605 	case DIVASS:
   4606 	case MODASS:
   4607 	case ANDASS:
   4608 	case ORASS:
   4609 	case XORASS:
   4610 	case SHLASS:
   4611 	case SHRASS:
   4612 	case REAL:
   4613 	case IMAG:
   4614 		check_expr_side_effect(ln, szof);
   4615 		break;
   4616 	case ASSIGN:
   4617 		check_expr_assign(ln, szof);
   4618 		break;
   4619 	case CALL:
   4620 		check_expr_call(tn, ln, szof, vctx, cond, retval_discarded);
   4621 		break;
   4622 	case EQ:
   4623 		if (hflag && eqwarn)
   4624 			/* operator '==' found where '=' was expected */
   4625 			warning(160);
   4626 		break;
   4627 	case CON:
   4628 	case NAME:
   4629 	case STRING:
   4630 		return false;
   4631 	default:
   4632 		break;
   4633 	}
   4634 	return true;
   4635 }
   4636 
   4637 /*
   4638  *	vctx			???
   4639  *	cond			whether the expression is a condition that
   4640  *				will be compared with 0
   4641  *	eqwarn			whether the operator '==' might be a
   4642  *				misspelled '='
   4643  *	fcall			whether the expression is a function call
   4644  *	retval_discarded	whether the return value of a function call
   4645  *				is discarded; such calls will be analyzed by
   4646  *				lint2 in messages 4, 8 and 9
   4647  *	szof			whether the expression is part of a sizeof
   4648  *				expression, which means that its value is
   4649  *				discarded since only the type is relevant
   4650  */
   4651 void
   4652 check_expr_misc(const tnode_t *tn, bool vctx, bool cond,
   4653 		bool eqwarn, bool fcall, bool retval_discarded, bool szof)
   4654 {
   4655 
   4656 	if (tn == NULL)
   4657 		return;
   4658 
   4659 	tnode_t *ln = tn->tn_left;
   4660 	tnode_t *rn = tn->tn_right;
   4661 	op_t op = tn->tn_op;
   4662 	const mod_t *mp = &modtab[op];
   4663 
   4664 	if (!check_expr_op(tn, op, ln,
   4665 	    szof, fcall, vctx, cond, retval_discarded, eqwarn))
   4666 		return;
   4667 
   4668 	bool cvctx = mp->m_value_context;
   4669 	bool ccond = mp->m_compares_with_zero;
   4670 	bool eq = mp->m_warn_if_operand_eq &&
   4671 	     !ln->tn_parenthesized &&
   4672 	     rn != NULL && !rn->tn_parenthesized;
   4673 
   4674 	/*
   4675 	 * values of operands of ':' are not used if the type of at least
   4676 	 * one of the operands (for gcc compatibility) is void
   4677 	 * XXX test/value context of QUEST should probably be used as
   4678 	 * context for both operands of COLON
   4679 	 */
   4680 	if (op == COLON && tn->tn_type->t_tspec == VOID)
   4681 		cvctx = ccond = false;
   4682 	bool discard = op == CVT && tn->tn_type->t_tspec == VOID;
   4683 	check_expr_misc(ln, cvctx, ccond, eq, op == CALL, discard, szof);
   4684 
   4685 	switch (op) {
   4686 	case PUSH:
   4687 		if (rn != NULL)
   4688 			check_expr_misc(rn, false, false, eq, false, false,
   4689 			    szof);
   4690 		break;
   4691 	case LOGAND:
   4692 	case LOGOR:
   4693 		check_expr_misc(rn, false, true, eq, false, false, szof);
   4694 		break;
   4695 	case COLON:
   4696 		check_expr_misc(rn, cvctx, ccond, eq, false, false, szof);
   4697 		break;
   4698 	case COMMA:
   4699 		check_expr_misc(rn, vctx, cond, false, false, false, szof);
   4700 		break;
   4701 	default:
   4702 		if (mp->m_binary)
   4703 			check_expr_misc(rn, true, false, eq, false, false,
   4704 			    szof);
   4705 		break;
   4706 	}
   4707 }
   4708 
   4709 /*
   4710  * Return whether the expression can be used for static initialization.
   4711  *
   4712  * Constant initialization expressions must be constant or an address
   4713  * of a static object with an optional offset. In the first case,
   4714  * the result is returned in *offsp. In the second case, the static
   4715  * object is returned in *symp and the offset in *offsp.
   4716  *
   4717  * The expression can consist of PLUS, MINUS, ADDR, NAME, STRING and
   4718  * CON. Type conversions are allowed if they do not change binary
   4719  * representation (including width).
   4720  *
   4721  * C99 6.6 "Constant expressions"
   4722  * C99 6.7.8p4 restricts initializers for static storage duration
   4723  */
   4724 bool
   4725 constant_addr(const tnode_t *tn, const sym_t **symp, ptrdiff_t *offsp)
   4726 {
   4727 	const sym_t *sym;
   4728 	ptrdiff_t offs1, offs2;
   4729 	tspec_t t, ot;
   4730 
   4731 	switch (tn->tn_op) {
   4732 	case MINUS:
   4733 		if (tn->tn_right->tn_op == CVT)
   4734 			return constant_addr(tn->tn_right, symp, offsp);
   4735 		else if (tn->tn_right->tn_op != CON)
   4736 			return false;
   4737 		/* FALLTHROUGH */
   4738 	case PLUS:
   4739 		offs1 = offs2 = 0;
   4740 		if (tn->tn_left->tn_op == CON) {
   4741 			offs1 = (ptrdiff_t)tn->tn_left->tn_val.u.integer;
   4742 			if (!constant_addr(tn->tn_right, &sym, &offs2))
   4743 				return false;
   4744 		} else if (tn->tn_right->tn_op == CON) {
   4745 			offs2 = (ptrdiff_t)tn->tn_right->tn_val.u.integer;
   4746 			if (tn->tn_op == MINUS)
   4747 				offs2 = -offs2;
   4748 			if (!constant_addr(tn->tn_left, &sym, &offs1))
   4749 				return false;
   4750 		} else {
   4751 			return false;
   4752 		}
   4753 		*symp = sym;
   4754 		*offsp = offs1 + offs2;
   4755 		return true;
   4756 	case ADDR:
   4757 		if (tn->tn_left->tn_op == NAME) {
   4758 			*symp = tn->tn_left->tn_sym;
   4759 			*offsp = 0;
   4760 			return true;
   4761 		} else {
   4762 			/*
   4763 			 * If this were the front end of a compiler, we
   4764 			 * would return a label instead of 0, at least if
   4765 			 * 'tn->tn_left->tn_op == STRING'.
   4766 			 */
   4767 			*symp = NULL;
   4768 			*offsp = 0;
   4769 			return true;
   4770 		}
   4771 	case CVT:
   4772 		t = tn->tn_type->t_tspec;
   4773 		ot = tn->tn_left->tn_type->t_tspec;
   4774 		if ((!is_integer(t) && t != PTR) ||
   4775 		    (!is_integer(ot) && ot != PTR)) {
   4776 			return false;
   4777 		}
   4778 #if 0
   4779 		/*
   4780 		 * consider:
   4781 		 *	struct foo {
   4782 		 *		unsigned char a;
   4783 		 *	} f = {
   4784 		 *		(unsigned char)(unsigned long)
   4785 		 *		    (&(((struct foo *)0)->a))
   4786 		 *	};
   4787 		 * since psize(unsigned long) != psize(unsigned char),
   4788 		 * this fails.
   4789 		 */
   4790 		else if (psize(t) != psize(ot))
   4791 			return -1;
   4792 #endif
   4793 		return constant_addr(tn->tn_left, symp, offsp);
   4794 	default:
   4795 		return false;
   4796 	}
   4797 }
   4798 
   4799 /* Append s2 to s1, then free s2. */
   4800 strg_t *
   4801 cat_strings(strg_t *s1, strg_t *s2)
   4802 {
   4803 
   4804 	if (s1->st_char != s2->st_char) {
   4805 		/* cannot concatenate wide and regular string literals */
   4806 		error(292);
   4807 		return s1;
   4808 	}
   4809 
   4810 	size_t len1 = s1->st_len;
   4811 	size_t len2 = s2->st_len;
   4812 	size_t chsize = s1->st_char ? sizeof(char) : sizeof(wchar_t);
   4813 	size_t size1 = len1 * chsize;
   4814 	size_t size2 = (len2 + 1) * chsize;
   4815 	s1->st_mem = xrealloc(s1->st_mem, size1 + size2);
   4816 	memcpy((char *)s1->st_mem + size1, s2->st_mem, size2);
   4817 	free(s2->st_mem);
   4818 
   4819 	s1->st_len = len1 + len2;
   4820 	free(s2);
   4821 
   4822 	return s1;
   4823 }
   4824 
   4825 
   4826 typedef struct stmt_expr {
   4827 	memory_pool se_mem;
   4828 	sym_t *se_sym;
   4829 	struct stmt_expr *se_enclosing;
   4830 } stmt_expr;
   4831 
   4832 static stmt_expr *stmt_exprs;
   4833 
   4834 void
   4835 begin_statement_expr(void)
   4836 {
   4837 	debug_enter();
   4838 
   4839 	stmt_expr *se = xmalloc(sizeof(*se));
   4840 	se->se_mem = expr_save_memory();
   4841 	se->se_sym = NULL;
   4842 	se->se_enclosing = stmt_exprs;
   4843 	stmt_exprs = se;
   4844 }
   4845 
   4846 void
   4847 do_statement_expr(tnode_t *tn)
   4848 {
   4849 	block_level--;
   4850 	mem_block_level--;
   4851 	stmt_exprs->se_sym = tn != NULL
   4852 	    ? mktempsym(block_dup_type(tn->tn_type))
   4853 	    : NULL;		/* after a syntax error */
   4854 	mem_block_level++;
   4855 	block_level++;
   4856 	/* '({ ... })' is a GCC extension */
   4857 	gnuism(320);
   4858 }
   4859 
   4860 tnode_t *
   4861 end_statement_expr(void)
   4862 {
   4863 	tnode_t *tn;
   4864 
   4865 	stmt_expr *se = stmt_exprs;
   4866 	if (se->se_sym == NULL) {
   4867 		tn = NULL;	/* after a syntax error */
   4868 		goto end;
   4869 	}
   4870 
   4871 	tn = build_name(se->se_sym, false);
   4872 	(void)expr_save_memory();	/* leak */
   4873 	expr_restore_memory(se->se_mem);
   4874 	stmt_exprs = se->se_enclosing;
   4875 	free(se);
   4876 
   4877 end:
   4878 	debug_leave();
   4879 	return tn;
   4880 }
   4881 
   4882 bool
   4883 in_statement_expr(void)
   4884 {
   4885 	return stmt_exprs != NULL;
   4886 }
   4887