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