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