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