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