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