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