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