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