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