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