Home | History | Annotate | Line # | Download | only in lint1
tree.c revision 1.649
      1 /*	$NetBSD: tree.c,v 1.649 2024/07/10 20:33:37 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.649 2024/07/10 20:33:37 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 NOT:
   1813 		if (ln->tn_op == ASSIGN && ln->u.ops.right->tn_op == CON) {
   1814 			/* constant assignment of type '%s' in operand ... */
   1815 			warning(382, type_name(ln->tn_type),
   1816 			    is_nonzero_val(&ln->u.ops.right->u.value)
   1817 			    ? "true" : "false");
   1818 		}
   1819 		ntn = build_op(op, sys, gettyp(Tflag ? BOOL : INT), ln, NULL);
   1820 		break;
   1821 	case INCAFT:
   1822 	case DECAFT:
   1823 	case INCBEF:
   1824 	case DECBEF:
   1825 		ntn = build_prepost_incdec(op, sys, ln);
   1826 		break;
   1827 	case ADDR:
   1828 		ntn = build_address(sys, ln, false);
   1829 		break;
   1830 	case INDIR:
   1831 		ntn = build_op(INDIR, sys, ln->tn_type->t_subt, ln, NULL);
   1832 		break;
   1833 	case PLUS:
   1834 	case MINUS:
   1835 		ntn = build_plus_minus(op, sys, ln, rn);
   1836 		break;
   1837 	case SHL:
   1838 	case SHR:
   1839 		ntn = build_bit_shift(op, sys, ln, rn);
   1840 		break;
   1841 	case COLON:
   1842 		ntn = build_colon(sys, ln, rn);
   1843 		break;
   1844 	case ASSIGN:
   1845 	case MULASS:
   1846 	case DIVASS:
   1847 	case MODASS:
   1848 	case ADDASS:
   1849 	case SUBASS:
   1850 	case SHLASS:
   1851 	case SHRASS:
   1852 	case ANDASS:
   1853 	case XORASS:
   1854 	case ORASS:
   1855 	case RETURN:
   1856 	case INIT:
   1857 		ntn = build_assignment(op, sys, ln, rn);
   1858 		break;
   1859 	case COMMA:
   1860 		if (any_query_enabled) {
   1861 			/* comma operator with types '%s' and '%s' */
   1862 			query_message(12,
   1863 			    type_name(ln->tn_type), type_name(rn->tn_type));
   1864 		}
   1865 		/* FALLTHROUGH */
   1866 	case QUEST:
   1867 		ntn = build_op(op, sys, rn->tn_type, ln, rn);
   1868 		break;
   1869 	case REAL:
   1870 	case IMAG:
   1871 		ntn = build_real_imag(op, sys, ln);
   1872 		break;
   1873 	default:
   1874 		lint_assert(mp->m_binary == (rn != NULL));
   1875 		type_t *rettp = mp->m_returns_bool
   1876 		    ? gettyp(Tflag ? BOOL : INT) : ln->tn_type;
   1877 		ntn = build_op(op, sys, rettp, ln, rn);
   1878 		break;
   1879 	}
   1880 
   1881 	if (ntn == NULL)
   1882 		return NULL;
   1883 
   1884 	if (mp->m_possible_precedence_confusion)
   1885 		check_precedence_confusion(ntn);
   1886 
   1887 	if (hflag && !suppress_constcond &&
   1888 	    mp->m_compares_with_zero &&
   1889 	    (ln->tn_op == CON ||
   1890 	     (mp->m_binary && op != QUEST && rn->tn_op == CON)) &&
   1891 	    /* XXX: rn->tn_system_dependent should be checked as well */
   1892 	    !ln->tn_system_dependent)
   1893 		/* constant in conditional context */
   1894 		warning(161);
   1895 
   1896 	if (mp->m_fold_constant_operands && ln->tn_op == CON) {
   1897 		if (!mp->m_binary || rn->tn_op == CON) {
   1898 			if (mp->m_compares_with_zero)
   1899 				ntn = fold_constant_compare_zero(ntn);
   1900 			else if (is_floating(ntn->tn_type->t_tspec))
   1901 				ntn = fold_constant_floating(ntn);
   1902 			else
   1903 				ntn = fold_constant_integer(ntn);
   1904 		} else if (op == QUEST) {
   1905 			lint_assert(has_operands(rn));
   1906 			use(ln->u.value.u.integer != 0
   1907 			    ? rn->u.ops.right : rn->u.ops.left);
   1908 			ntn = ln->u.value.u.integer != 0
   1909 			    ? rn->u.ops.left : rn->u.ops.right;
   1910 		}
   1911 	}
   1912 
   1913 	return ntn;
   1914 }
   1915 
   1916 tnode_t *
   1917 build_unary(op_t op, bool sys, tnode_t *tn)
   1918 {
   1919 	return build_binary(tn, op, sys, NULL);
   1920 }
   1921 
   1922 static bool
   1923 are_members_compatible(const sym_t *a, const sym_t *b)
   1924 {
   1925 	if (a->u.s_member.sm_offset_in_bits != b->u.s_member.sm_offset_in_bits)
   1926 		return false;
   1927 
   1928 	const type_t *atp = a->s_type;
   1929 	const type_t *btp = b->s_type;
   1930 	bool w = false;
   1931 	if (!types_compatible(atp, btp, false, false, &w) && !w)
   1932 		return false;
   1933 	if (a->s_bitfield != b->s_bitfield)
   1934 		return false;
   1935 	if (a->s_bitfield) {
   1936 		if (atp->t_bit_field_width != btp->t_bit_field_width)
   1937 			return false;
   1938 		if (atp->t_bit_field_offset != btp->t_bit_field_offset)
   1939 			return false;
   1940 	}
   1941 	return true;
   1942 }
   1943 
   1944 /*
   1945  * Return whether all struct/union members with the same name have the same
   1946  * type and offset.
   1947  */
   1948 static bool
   1949 all_members_compatible(const sym_t *msym)
   1950 {
   1951 	for (const sym_t *csym = msym;
   1952 	    csym != NULL; csym = csym->s_symtab_next) {
   1953 		if (!is_member(csym))
   1954 			continue;
   1955 		if (strcmp(msym->s_name, csym->s_name) != 0)
   1956 			continue;
   1957 
   1958 		for (const sym_t *sym = csym->s_symtab_next;
   1959 		    sym != NULL; sym = sym->s_symtab_next) {
   1960 			if (is_member(sym)
   1961 			    && strcmp(csym->s_name, sym->s_name) == 0
   1962 			    && !are_members_compatible(csym, sym))
   1963 				return false;
   1964 		}
   1965 	}
   1966 	return true;
   1967 }
   1968 
   1969 sym_t *
   1970 find_member(const struct_or_union *sou, const char *name)
   1971 {
   1972 	for (sym_t *mem = sou->sou_first_member;
   1973 	    mem != NULL; mem = mem->s_next) {
   1974 		lint_assert(is_member(mem));
   1975 		lint_assert(mem->u.s_member.sm_containing_type == sou);
   1976 		if (strcmp(mem->s_name, name) == 0)
   1977 			return mem;
   1978 	}
   1979 
   1980 	for (sym_t *mem = sou->sou_first_member;
   1981 	    mem != NULL; mem = mem->s_next) {
   1982 		if (is_struct_or_union(mem->s_type->t_tspec)
   1983 		    && mem->s_name == unnamed) {
   1984 			sym_t *nested_mem =
   1985 			    find_member(mem->s_type->u.sou, name);
   1986 			if (nested_mem != NULL)
   1987 				return nested_mem;
   1988 		}
   1989 	}
   1990 	return NULL;
   1991 }
   1992 
   1993 /*
   1994  * Remove the member if it was unknown until now, which means
   1995  * that no defined struct or union has a member with the same name.
   1996  */
   1997 static void
   1998 remove_unknown_member(tnode_t *tn, sym_t *msym)
   1999 {
   2000 	/* type '%s' does not have member '%s' */
   2001 	error(101, type_name(tn->tn_type), msym->s_name);
   2002 	symtab_remove_forever(msym);
   2003 	msym->s_kind = SK_MEMBER;
   2004 	msym->s_scl = STRUCT_MEMBER;
   2005 
   2006 	struct_or_union *sou = expr_zero_alloc(sizeof(*sou),
   2007 	    "struct_or_union");
   2008 	sou->sou_tag = expr_zero_alloc(sizeof(*sou->sou_tag), "sym");
   2009 	sou->sou_tag->s_name = unnamed;
   2010 
   2011 	msym->u.s_member.sm_containing_type = sou;
   2012 	/*
   2013 	 * The member sm_offset_in_bits is not needed here since this symbol
   2014 	 * can only be used for error reporting.
   2015 	 */
   2016 }
   2017 
   2018 /*
   2019  * Returns a symbol which has the same name as 'msym' and is a member of the
   2020  * struct or union specified by 'tn'.
   2021  */
   2022 static sym_t *
   2023 struct_or_union_member(tnode_t *tn, op_t op, sym_t *msym)
   2024 {
   2025 
   2026 	/* Determine the tag type of which msym is expected to be a member. */
   2027 	const type_t *tp = NULL;
   2028 	if (op == POINT && is_struct_or_union(tn->tn_type->t_tspec))
   2029 		tp = tn->tn_type;
   2030 	if (op == ARROW && tn->tn_type->t_tspec == PTR
   2031 	    && is_struct_or_union(tn->tn_type->t_subt->t_tspec))
   2032 		tp = tn->tn_type->t_subt;
   2033 	struct_or_union *sou = tp != NULL ? tp->u.sou : NULL;
   2034 
   2035 	if (sou != NULL) {
   2036 		sym_t *nested_mem = find_member(sou, msym->s_name);
   2037 		if (nested_mem != NULL)
   2038 			return nested_mem;
   2039 	}
   2040 
   2041 	if (msym->s_scl == NO_SCL) {
   2042 		remove_unknown_member(tn, msym);
   2043 		return msym;
   2044 	}
   2045 
   2046 	bool eq = all_members_compatible(msym);
   2047 
   2048 	/*
   2049 	 * Now handle the case in which the left operand refers really to a
   2050 	 * struct/union, but the right operand is not member of it.
   2051 	 */
   2052 	if (sou != NULL) {
   2053 		if (eq && !allow_c90)
   2054 			/* illegal use of member '%s' */
   2055 			warning(102, msym->s_name);
   2056 		else
   2057 			/* illegal use of member '%s' */
   2058 			error(102, msym->s_name);
   2059 		return msym;
   2060 	}
   2061 
   2062 	if (eq) {
   2063 		if (op == POINT) {
   2064 			if (!allow_c90)
   2065 				/* left operand of '.' must be struct ... */
   2066 				warning(103, type_name(tn->tn_type));
   2067 			else
   2068 				/* left operand of '.' must be struct ... */
   2069 				error(103, type_name(tn->tn_type));
   2070 		} else {
   2071 			if (!allow_c90 && tn->tn_type->t_tspec == PTR)
   2072 				/* left operand of '->' must be pointer ... */
   2073 				warning(104, type_name(tn->tn_type));
   2074 			else
   2075 				/* left operand of '->' must be pointer ... */
   2076 				error(104, type_name(tn->tn_type));
   2077 		}
   2078 	} else {
   2079 		if (!allow_c90)
   2080 			/* non-unique member requires struct/union %s */
   2081 			error(105, op == POINT ? "object" : "pointer");
   2082 		else
   2083 			/* unacceptable operand of '%s' */
   2084 			error(111, op_name(op));
   2085 	}
   2086 
   2087 	return msym;
   2088 }
   2089 
   2090 tnode_t *
   2091 build_member_access(tnode_t *ln, op_t op, bool sys, sbuf_t *member)
   2092 {
   2093 	sym_t *msym;
   2094 
   2095 	if (ln == NULL)
   2096 		return NULL;
   2097 
   2098 	if (op == ARROW)
   2099 		/* must do this before struct_or_union_member is called */
   2100 		ln = cconv(ln);
   2101 	msym = struct_or_union_member(ln, op, getsym(member));
   2102 	return build_binary(ln, op, sys, build_name(msym, false));
   2103 }
   2104 
   2105 /*
   2106  * Perform class conversions.
   2107  *
   2108  * Arrays of type T are converted into pointers to type T.
   2109  * Functions are converted to pointers to functions.
   2110  * Lvalues are converted to rvalues.
   2111  *
   2112  * C99 6.3 "Conversions"
   2113  * C99 6.3.2 "Other operands"
   2114  * C99 6.3.2.1 "Lvalues, arrays, and function designators"
   2115  */
   2116 tnode_t *
   2117 cconv(tnode_t *tn)
   2118 {
   2119 	if (tn->tn_type->t_tspec == ARRAY) {
   2120 		if (!tn->tn_lvalue) {
   2121 			/* XXX print correct operator */
   2122 			/* %soperand of '%s' must be lvalue */
   2123 			gnuism(114, "", op_name(ADDR));
   2124 		}
   2125 		tn = build_op(ADDR, tn->tn_sys,
   2126 		    expr_derive_type(tn->tn_type->t_subt, PTR), tn, NULL);
   2127 	}
   2128 
   2129 	if (tn->tn_type->t_tspec == FUNC)
   2130 		tn = build_address(tn->tn_sys, tn, true);
   2131 
   2132 	if (tn->tn_lvalue) {
   2133 		type_t *tp = expr_dup_type(tn->tn_type);
   2134 		/* C99 6.3.2.1p2 sentence 2 says to remove the qualifiers. */
   2135 		tp->t_const = tp->t_volatile = false;
   2136 		tn = build_op(LOAD, tn->tn_sys, tp, tn, NULL);
   2137 	}
   2138 
   2139 	return tn;
   2140 }
   2141 
   2142 const tnode_t *
   2143 before_conversion(const tnode_t *tn)
   2144 {
   2145 	while (tn->tn_op == CVT && !tn->tn_cast)
   2146 		tn = tn->u.ops.left;
   2147 	return tn;
   2148 }
   2149 
   2150 /*
   2151  * Most errors required by C90 are reported in struct_or_union_member().
   2152  * Here we only check for totally wrong things.
   2153  */
   2154 static bool
   2155 typeok_point(const tnode_t *ln, const type_t *ltp, tspec_t lt)
   2156 {
   2157 	if (is_struct_or_union(lt))
   2158 		return true;
   2159 
   2160 	if (lt == FUNC || lt == VOID || ltp->t_bitfield)
   2161 		goto wrong;
   2162 
   2163 	/*
   2164 	 * Some C dialects from before C90 tolerated any lvalue on the
   2165 	 * left-hand side of the '.' operator, allowing things like 'char
   2166 	 * st[100]; st.st_mtime', assuming that the member 'st_mtime' only
   2167 	 * occurred in a single struct; see typeok_arrow.
   2168 	 */
   2169 	if (ln->tn_lvalue)
   2170 		return true;
   2171 
   2172 wrong:
   2173 	/* With allow_c90 we already got an error */
   2174 	if (!allow_c90)
   2175 		/* unacceptable operand of '%s' */
   2176 		error(111, op_name(POINT));
   2177 
   2178 	return false;
   2179 }
   2180 
   2181 static bool
   2182 typeok_arrow(tspec_t lt)
   2183 {
   2184 	/*
   2185 	 * C1978 Appendix A 14.1 says: <quote>In fact, any lvalue is allowed
   2186 	 * before '.', and that lvalue is then assumed to have the form of the
   2187 	 * structure of which the name of the right is a member. [...] Such
   2188 	 * constructions are non-portable.</quote>
   2189 	 */
   2190 	if (lt == PTR || (!allow_c90 && is_integer(lt)))
   2191 		return true;
   2192 
   2193 	/* With allow_c90 we already got an error */
   2194 	if (!allow_c90)
   2195 		/* unacceptable operand of '%s' */
   2196 		error(111, op_name(ARROW));
   2197 	return false;
   2198 }
   2199 
   2200 static bool
   2201 typeok_incdec(op_t op, const tnode_t *tn, const type_t *tp)
   2202 {
   2203 	/* operand has scalar type (checked in typeok) */
   2204 	if (!tn->tn_lvalue) {
   2205 		if (tn->tn_op == CVT && tn->tn_cast &&
   2206 		    tn->u.ops.left->tn_op == LOAD)
   2207 			/* a cast does not yield an lvalue */
   2208 			error(163);
   2209 		/* %soperand of '%s' must be lvalue */
   2210 		error(114, "", op_name(op));
   2211 		return false;
   2212 	}
   2213 	if (tp->t_const && allow_c90)
   2214 		/* %soperand of '%s' must be modifiable lvalue */
   2215 		warning(115, "", op_name(op));
   2216 	return true;
   2217 }
   2218 
   2219 static bool
   2220 typeok_address(op_t op, const tnode_t *tn, const type_t *tp, tspec_t t)
   2221 {
   2222 	if (t == ARRAY || t == FUNC) {
   2223 		/* ok, a warning comes later (in build_address()) */
   2224 	} else if (!tn->tn_lvalue) {
   2225 		if (tn->tn_op == CVT && tn->tn_cast &&
   2226 		    tn->u.ops.left->tn_op == LOAD)
   2227 			/* a cast does not yield an lvalue */
   2228 			error(163);
   2229 		/* %soperand of '%s' must be lvalue */
   2230 		error(114, "", op_name(op));
   2231 		return false;
   2232 	} else if (is_scalar(t)) {
   2233 		if (tp->t_bitfield) {
   2234 			/* cannot take address of bit-field */
   2235 			error(112);
   2236 			return false;
   2237 		}
   2238 	} else if (t != STRUCT && t != UNION) {
   2239 		/* unacceptable operand of '%s' */
   2240 		error(111, op_name(op));
   2241 		return false;
   2242 	}
   2243 	if (tn->tn_op == NAME && tn->u.sym->s_register) {
   2244 		/* cannot take address of register '%s' */
   2245 		error(113, tn->u.sym->s_name);
   2246 		return false;
   2247 	}
   2248 	return true;
   2249 }
   2250 
   2251 static bool
   2252 typeok_indir(const type_t *tp, tspec_t t)
   2253 {
   2254 
   2255 	if (t != PTR) {
   2256 		/* cannot dereference non-pointer type '%s' */
   2257 		error(96, type_name(tp));
   2258 		return false;
   2259 	}
   2260 	return true;
   2261 }
   2262 
   2263 static void
   2264 warn_incompatible_types(op_t op,
   2265 			const type_t *ltp, tspec_t lt,
   2266 			const type_t *rtp, tspec_t rt)
   2267 {
   2268 	bool binary = modtab[op].m_binary;
   2269 
   2270 	if (lt == VOID || (binary && rt == VOID)) {
   2271 		/* void type illegal in expression */
   2272 		error(109);
   2273 	} else if (op == ASSIGN)
   2274 		/* cannot assign to '%s' from '%s' */
   2275 		error(171, type_name(ltp), type_name(rtp));
   2276 	else if (binary)
   2277 		/* operands of '%s' have incompatible types '%s' and '%s' */
   2278 		error(107, op_name(op), type_name(ltp), type_name(rtp));
   2279 	else {
   2280 		lint_assert(rt == NO_TSPEC);
   2281 		/* operand of '%s' has invalid type '%s' */
   2282 		error(108, op_name(op), type_name(ltp));
   2283 	}
   2284 }
   2285 
   2286 static bool
   2287 typeok_plus(op_t op,
   2288 	    const type_t *ltp, tspec_t lt,
   2289 	    const type_t *rtp, tspec_t rt)
   2290 {
   2291 	/* operands have scalar types (checked in typeok) */
   2292 	if ((lt == PTR && !is_integer(rt)) || (rt == PTR && !is_integer(lt))) {
   2293 		warn_incompatible_types(op, ltp, lt, rtp, rt);
   2294 		return false;
   2295 	}
   2296 	return true;
   2297 }
   2298 
   2299 static bool
   2300 typeok_minus(op_t op,
   2301 	     const type_t *ltp, tspec_t lt,
   2302 	     const type_t *rtp, tspec_t rt)
   2303 {
   2304 	/* operands have scalar types (checked in typeok) */
   2305 	if ((lt == PTR && rt != PTR && !is_integer(rt)) ||
   2306 	    (lt != PTR && rt == PTR)) {
   2307 		warn_incompatible_types(op, ltp, lt, rtp, rt);
   2308 		return false;
   2309 	}
   2310 	if (lt == PTR && rt == PTR &&
   2311 	    !types_compatible(ltp->t_subt, rtp->t_subt, true, false, NULL)) {
   2312 		/* illegal pointer subtraction */
   2313 		error(116);
   2314 	}
   2315 	return true;
   2316 }
   2317 
   2318 static void
   2319 typeok_shr(op_t op,
   2320 	   const tnode_t *ln, tspec_t lt,
   2321 	   const tnode_t *rn, tspec_t rt)
   2322 {
   2323 	tspec_t olt = before_conversion(ln)->tn_type->t_tspec;
   2324 	tspec_t ort = before_conversion(rn)->tn_type->t_tspec;
   2325 
   2326 	/* operands have integer types (checked in typeok) */
   2327 	if (pflag && !is_uinteger(olt)) {
   2328 		integer_constraints lc = ic_expr(ln);
   2329 		if (!ic_maybe_signed(ln->tn_type, &lc))
   2330 			return;
   2331 
   2332 		if (ln->tn_op != CON)
   2333 			/* bitwise '%s' on signed value possibly nonportable */
   2334 			warning(117, op_name(op));
   2335 		else if (ln->u.value.u.integer < 0)
   2336 			/* bitwise '%s' on signed value nonportable */
   2337 			warning(120, op_name(op));
   2338 	} else if (allow_trad && allow_c90 &&
   2339 	    !is_uinteger(olt) && is_uinteger(ort)) {
   2340 		/* The left operand would become unsigned in traditional C. */
   2341 		if (hflag && (ln->tn_op != CON || ln->u.value.u.integer < 0)) {
   2342 			/* semantics of '%s' change in C90; use ... */
   2343 			warning(118, op_name(op));
   2344 		}
   2345 	} else if (allow_trad && allow_c90 &&
   2346 	    !is_uinteger(olt) && !is_uinteger(ort) &&
   2347 	    portable_rank_cmp(lt, rt) < 0) {
   2348 		/*
   2349 		 * In traditional C, the left operand would be extended
   2350 		 * (possibly sign-extended) and then shifted.
   2351 		 */
   2352 		if (hflag && (ln->tn_op != CON || ln->u.value.u.integer < 0)) {
   2353 			/* semantics of '%s' change in C90; use ... */
   2354 			warning(118, op_name(op));
   2355 		}
   2356 	}
   2357 }
   2358 
   2359 static void
   2360 typeok_shl(op_t op, tspec_t lt, tspec_t rt)
   2361 {
   2362 	/*
   2363 	 * C90 does not perform balancing for shift operations, but traditional
   2364 	 * C does. If the width of the right operand is greater than the width
   2365 	 * of the left operand, then in traditional C the left operand would be
   2366 	 * extended to the width of the right operand. For SHL this may result
   2367 	 * in different results.
   2368 	 */
   2369 	if (portable_rank_cmp(lt, rt) < 0) {
   2370 		/*
   2371 		 * XXX If both operands are constant, make sure that there is
   2372 		 * really a difference between C90 and traditional C.
   2373 		 */
   2374 		if (hflag && allow_trad && allow_c90)
   2375 			/* semantics of '%s' change in C90; use ... */
   2376 			warning(118, op_name(op));
   2377 	}
   2378 }
   2379 
   2380 static void
   2381 typeok_shift(const type_t *ltp, tspec_t lt, const tnode_t *rn, tspec_t rt)
   2382 {
   2383 	if (rn->tn_op != CON)
   2384 		return;
   2385 
   2386 	if (!is_uinteger(rt) && rn->u.value.u.integer < 0)
   2387 		/* negative shift */
   2388 		warning(121);
   2389 	else if ((uint64_t)rn->u.value.u.integer == size_in_bits(lt))
   2390 		/* shift amount %u equals bit-size of '%s' */
   2391 		warning(267, (unsigned)rn->u.value.u.integer, type_name(ltp));
   2392 	else if ((uint64_t)rn->u.value.u.integer > size_in_bits(lt)) {
   2393 		/* shift amount %llu is greater than bit-size %llu of '%s' */
   2394 		warning(122, (unsigned long long)rn->u.value.u.integer,
   2395 		    (unsigned long long)size_in_bits(lt),
   2396 		    tspec_name(lt));
   2397 	}
   2398 }
   2399 
   2400 static bool
   2401 is_typeok_eq(const tnode_t *ln, tspec_t lt, const tnode_t *rn, tspec_t rt)
   2402 {
   2403 	if (lt == PTR && is_null_pointer(rn))
   2404 		return true;
   2405 	if (rt == PTR && is_null_pointer(ln))
   2406 		return true;
   2407 	return false;
   2408 }
   2409 
   2410 static void
   2411 warn_incompatible_pointers(op_t op, const type_t *ltp, const type_t *rtp)
   2412 {
   2413 	lint_assert(ltp->t_tspec == PTR);
   2414 	lint_assert(rtp->t_tspec == PTR);
   2415 
   2416 	tspec_t lt = ltp->t_subt->t_tspec;
   2417 	tspec_t rt = rtp->t_subt->t_tspec;
   2418 
   2419 	if (is_struct_or_union(lt) && is_struct_or_union(rt)) {
   2420 		if (op == RETURN)
   2421 			/* illegal structure pointer combination */
   2422 			warning(244);
   2423 		else {
   2424 			/* incompatible structure pointers: '%s' '%s' '%s' */
   2425 			warning(245, type_name(ltp),
   2426 			    op_name(op), type_name(rtp));
   2427 		}
   2428 	} else {
   2429 		if (op == RETURN)
   2430 			/* illegal combination of '%s' and '%s' */
   2431 			warning(184, type_name(ltp), type_name(rtp));
   2432 		else {
   2433 			/* illegal combination of '%s' and '%s', op '%s' */
   2434 			warning(124,
   2435 			    type_name(ltp), type_name(rtp), op_name(op));
   2436 		}
   2437 	}
   2438 }
   2439 
   2440 static void
   2441 check_pointer_comparison(op_t op, const tnode_t *ln, const tnode_t *rn)
   2442 {
   2443 	type_t *ltp = ln->tn_type, *rtp = rn->tn_type;
   2444 	tspec_t lst = ltp->t_subt->t_tspec, rst = rtp->t_subt->t_tspec;
   2445 
   2446 	if (lst == VOID || rst == VOID) {
   2447 		/* TODO: C99 behaves like C90 here. */
   2448 		if (!allow_trad && !allow_c99 &&
   2449 		    (lst == FUNC || rst == FUNC)) {
   2450 			/* (void *)0 is already handled in typeok() */
   2451 			const char *lsts, *rsts;
   2452 			*(lst == FUNC ? &lsts : &rsts) = "function pointer";
   2453 			*(lst == VOID ? &lsts : &rsts) = "'void *'";
   2454 			/* C90 or later forbid comparison of %s with %s */
   2455 			warning(274, lsts, rsts);
   2456 		}
   2457 		return;
   2458 	}
   2459 
   2460 	if (!types_compatible(ltp->t_subt, rtp->t_subt, true, false, NULL)) {
   2461 		warn_incompatible_pointers(op, ltp, rtp);
   2462 		return;
   2463 	}
   2464 
   2465 	if (lst == FUNC && rst == FUNC) {
   2466 		/* TODO: C99 behaves like C90 here, see C99 6.5.8p2. */
   2467 		if (!allow_trad && !allow_c99 && op != EQ && op != NE)
   2468 			/* pointers to functions can only be compared ... */
   2469 			warning(125);
   2470 	}
   2471 }
   2472 
   2473 static bool
   2474 typeok_compare(op_t op,
   2475 	       const tnode_t *ln, const type_t *ltp, tspec_t lt,
   2476 	       const tnode_t *rn, const type_t *rtp, tspec_t rt)
   2477 {
   2478 	if (lt == PTR && rt == PTR) {
   2479 		check_pointer_comparison(op, ln, rn);
   2480 		return true;
   2481 	}
   2482 
   2483 	if (lt != PTR && rt != PTR)
   2484 		return true;
   2485 
   2486 	if (!is_integer(lt) && !is_integer(rt)) {
   2487 		warn_incompatible_types(op, ltp, lt, rtp, rt);
   2488 		return false;
   2489 	}
   2490 
   2491 	const char *lx = lt == PTR ? "pointer" : "integer";
   2492 	const char *rx = rt == PTR ? "pointer" : "integer";
   2493 	/* illegal combination of %s '%s' and %s '%s', op '%s' */
   2494 	warning(123, lx, type_name(ltp), rx, type_name(rtp), op_name(op));
   2495 	return true;
   2496 }
   2497 
   2498 static bool
   2499 typeok_quest(tspec_t lt, const tnode_t *rn)
   2500 {
   2501 	if (!is_scalar(lt)) {
   2502 		/* first operand of '?' must have scalar type */
   2503 		error(170);
   2504 		return false;
   2505 	}
   2506 	lint_assert(before_conversion(rn)->tn_op == COLON);
   2507 	return true;
   2508 }
   2509 
   2510 static void
   2511 typeok_colon_pointer(const type_t *ltp, const type_t *rtp)
   2512 {
   2513 	type_t *lstp = ltp->t_subt;
   2514 	type_t *rstp = rtp->t_subt;
   2515 	tspec_t lst = lstp->t_tspec;
   2516 	tspec_t rst = rstp->t_tspec;
   2517 
   2518 	if ((lst == VOID && rst == FUNC) || (lst == FUNC && rst == VOID)) {
   2519 		/* (void *)0 is handled in typeok_colon */
   2520 		/* TODO: C99 behaves like C90 here. */
   2521 		if (!allow_trad && !allow_c99)
   2522 			/* conversion of %s to %s requires a cast, op %s */
   2523 			warning(305, "function pointer", "'void *'",
   2524 			    op_name(COLON));
   2525 		return;
   2526 	}
   2527 
   2528 	if (pointer_types_are_compatible(lstp, rstp, true))
   2529 		return;
   2530 	if (!types_compatible(lstp, rstp, true, false, NULL))
   2531 		warn_incompatible_pointers(COLON, ltp, rtp);
   2532 }
   2533 
   2534 static bool
   2535 typeok_colon(const tnode_t *ln, const type_t *ltp, tspec_t lt,
   2536 	     const tnode_t *rn, const type_t *rtp, tspec_t rt)
   2537 {
   2538 
   2539 	if (is_arithmetic(lt) && is_arithmetic(rt))
   2540 		return true;
   2541 	if (lt == BOOL && rt == BOOL)
   2542 		return true;
   2543 
   2544 	if (lt == STRUCT && rt == STRUCT && ltp->u.sou == rtp->u.sou)
   2545 		return true;
   2546 	if (lt == UNION && rt == UNION && ltp->u.sou == rtp->u.sou)
   2547 		return true;
   2548 
   2549 	if (lt == PTR && is_null_pointer(rn))
   2550 		return true;
   2551 	if (rt == PTR && is_null_pointer(ln))
   2552 		return true;
   2553 
   2554 	if ((lt == PTR && is_integer(rt)) || (is_integer(lt) && rt == PTR)) {
   2555 		const char *lx = lt == PTR ? "pointer" : "integer";
   2556 		const char *rx = rt == PTR ? "pointer" : "integer";
   2557 		/* illegal combination of %s '%s' and %s '%s', op '%s' */
   2558 		warning(123, lx, type_name(ltp),
   2559 		    rx, type_name(rtp), op_name(COLON));
   2560 		return true;
   2561 	}
   2562 
   2563 	if (lt == VOID || rt == VOID) {
   2564 		if (lt != VOID || rt != VOID)
   2565 			/* incompatible types '%s' and '%s' in conditional */
   2566 			warning(126, type_name(ltp), type_name(rtp));
   2567 		return true;
   2568 	}
   2569 
   2570 	if (lt == PTR && rt == PTR) {
   2571 		typeok_colon_pointer(ltp, rtp);
   2572 		return true;
   2573 	}
   2574 
   2575 	/* incompatible types '%s' and '%s' in conditional */
   2576 	error(126, type_name(ltp), type_name(rtp));
   2577 	return false;
   2578 }
   2579 
   2580 static bool
   2581 has_constant_member(const type_t *tp)
   2582 {
   2583 	lint_assert(is_struct_or_union(tp->t_tspec));
   2584 
   2585 	for (sym_t *m = tp->u.sou->sou_first_member;
   2586 	    m != NULL; m = m->s_next) {
   2587 		const type_t *mtp = m->s_type;
   2588 		if (mtp->t_const)
   2589 			return true;
   2590 		if (is_struct_or_union(mtp->t_tspec) &&
   2591 		    has_constant_member(mtp))
   2592 			return true;
   2593 	}
   2594 	return false;
   2595 }
   2596 
   2597 static bool
   2598 typeok_assign(op_t op, const tnode_t *ln, const type_t *ltp, tspec_t lt)
   2599 {
   2600 	if (op == RETURN || op == INIT || op == FARG)
   2601 		return true;
   2602 
   2603 	if (!ln->tn_lvalue) {
   2604 		if (ln->tn_op == CVT && ln->tn_cast &&
   2605 		    ln->u.ops.left->tn_op == LOAD)
   2606 			/* a cast does not yield an lvalue */
   2607 			error(163);
   2608 		/* %soperand of '%s' must be lvalue */
   2609 		error(114, "left ", op_name(op));
   2610 		return false;
   2611 	} else if (ltp->t_const
   2612 	    || (is_struct_or_union(lt) && has_constant_member(ltp))) {
   2613 		if (allow_c90)
   2614 			/* %soperand of '%s' must be modifiable lvalue */
   2615 			warning(115, "left ", op_name(op));
   2616 	}
   2617 	return true;
   2618 }
   2619 
   2620 static bool
   2621 typeok_scalar(op_t op, const mod_t *mp,
   2622 	      const type_t *ltp, tspec_t lt,
   2623 	      const type_t *rtp, tspec_t rt)
   2624 {
   2625 	if (mp->m_takes_bool && lt == BOOL && rt == BOOL)
   2626 		return true;
   2627 	if (mp->m_requires_integer) {
   2628 		if (!is_integer(lt) || (mp->m_binary && !is_integer(rt))) {
   2629 			warn_incompatible_types(op, ltp, lt, rtp, rt);
   2630 			return false;
   2631 		}
   2632 	} else if (mp->m_requires_integer_or_complex) {
   2633 		if ((!is_integer(lt) && !is_complex(lt)) ||
   2634 		    (mp->m_binary && (!is_integer(rt) && !is_complex(rt)))) {
   2635 			warn_incompatible_types(op, ltp, lt, rtp, rt);
   2636 			return false;
   2637 		}
   2638 	} else if (mp->m_requires_scalar) {
   2639 		if (!is_scalar(lt) || (mp->m_binary && !is_scalar(rt))) {
   2640 			warn_incompatible_types(op, ltp, lt, rtp, rt);
   2641 			return false;
   2642 		}
   2643 	} else if (mp->m_requires_arith) {
   2644 		if (!is_arithmetic(lt) ||
   2645 		    (mp->m_binary && !is_arithmetic(rt))) {
   2646 			warn_incompatible_types(op, ltp, lt, rtp, rt);
   2647 			return false;
   2648 		}
   2649 	}
   2650 	return true;
   2651 }
   2652 
   2653 static void
   2654 check_assign_void_pointer(op_t op, int arg,
   2655 			  tspec_t lt, tspec_t lst,
   2656 			  tspec_t rt, tspec_t rst)
   2657 {
   2658 
   2659 	if (!(lt == PTR && rt == PTR && (lst == VOID || rst == VOID)))
   2660 		return;
   2661 	/* two pointers, at least one pointer to void */
   2662 
   2663 	/* TODO: C99 behaves like C90 here. */
   2664 	if (!(!allow_trad && !allow_c99 && (lst == FUNC || rst == FUNC)))
   2665 		return;
   2666 	/* comb. of ptr to func and ptr to void */
   2667 
   2668 	const char *lts, *rts;
   2669 	*(lst == FUNC ? &lts : &rts) = "function pointer";
   2670 	*(lst == VOID ? &lts : &rts) = "'void *'";
   2671 
   2672 	switch (op) {
   2673 	case INIT:
   2674 	case RETURN:
   2675 		/* conversion of %s to %s requires a cast */
   2676 		warning(303, rts, lts);
   2677 		break;
   2678 	case FARG:
   2679 		/* conversion of %s to %s requires a cast, arg #%d */
   2680 		warning(304, rts, lts, arg);
   2681 		break;
   2682 	default:
   2683 		/* conversion of %s to %s requires a cast, op %s */
   2684 		warning(305, rts, lts, op_name(op));
   2685 		break;
   2686 	}
   2687 }
   2688 
   2689 static bool
   2690 is_direct_function_call(const tnode_t *tn, const char **out_name)
   2691 {
   2692 
   2693 	if (tn->tn_op == CALL
   2694 	    && tn->u.call->func->tn_op == ADDR
   2695 	    && tn->u.call->func->u.ops.left->tn_op == NAME) {
   2696 		*out_name = tn->u.call->func->u.ops.left->u.sym->s_name;
   2697 		return true;
   2698 	}
   2699 	return false;
   2700 }
   2701 
   2702 static bool
   2703 is_unconst_function(const char *name)
   2704 {
   2705 
   2706 	return strcmp(name, "memchr") == 0 ||
   2707 	    strcmp(name, "strchr") == 0 ||
   2708 	    strcmp(name, "strpbrk") == 0 ||
   2709 	    strcmp(name, "strrchr") == 0 ||
   2710 	    strcmp(name, "strstr") == 0;
   2711 }
   2712 
   2713 static bool
   2714 is_const_char_pointer(const tnode_t *tn)
   2715 {
   2716 	/*
   2717 	 * For traditional reasons, C99 6.4.5p5 defines that string literals
   2718 	 * have type 'char[]'.  They are often implicitly converted to 'char
   2719 	 * *', for example when they are passed as function arguments.
   2720 	 *
   2721 	 * C99 6.4.5p6 further defines that modifying a string that is
   2722 	 * constructed from a string literal invokes undefined behavior.
   2723 	 *
   2724 	 * Out of these reasons, string literals are treated as 'effectively
   2725 	 * const' here.
   2726 	 */
   2727 	if (tn->tn_op == CVT &&
   2728 	    tn->u.ops.left->tn_op == ADDR &&
   2729 	    tn->u.ops.left->u.ops.left->tn_op == STRING)
   2730 		return true;
   2731 
   2732 	const type_t *tp = before_conversion(tn)->tn_type;
   2733 	return tp->t_tspec == PTR &&
   2734 	    tp->t_subt->t_tspec == CHAR &&
   2735 	    tp->t_subt->t_const;
   2736 }
   2737 
   2738 static bool
   2739 is_const_pointer(const tnode_t *tn)
   2740 {
   2741 	const type_t *tp = before_conversion(tn)->tn_type;
   2742 	return tp->t_tspec == PTR && tp->t_subt->t_const;
   2743 }
   2744 
   2745 static void
   2746 check_unconst_function(const type_t *lstp, const tnode_t *rn)
   2747 {
   2748 	const char *function_name;
   2749 
   2750 	if (lstp->t_tspec == CHAR && !lstp->t_const &&
   2751 	    is_direct_function_call(rn, &function_name) &&
   2752 	    is_unconst_function(function_name) &&
   2753 	    rn->u.call->args_len >= 1 &&
   2754 	    is_const_char_pointer(rn->u.call->args[0])) {
   2755 		/* call to '%s' effectively discards 'const' from argument */
   2756 		warning(346, function_name);
   2757 	}
   2758 
   2759 	if (!lstp->t_const &&
   2760 	    is_direct_function_call(rn, &function_name) &&
   2761 	    strcmp(function_name, "bsearch") == 0 &&
   2762 	    rn->u.call->args_len >= 2 &&
   2763 	    is_const_pointer(rn->u.call->args[1])) {
   2764 		/* call to '%s' effectively discards 'const' from argument */
   2765 		warning(346, function_name);
   2766 	}
   2767 }
   2768 
   2769 static bool
   2770 check_assign_void_pointer_compat(op_t op, int arg,
   2771 				 const type_t *ltp, tspec_t lt,
   2772 				 const type_t *lstp, tspec_t lst,
   2773 				 const tnode_t *rn,
   2774 				 const type_t *rtp, tspec_t rt,
   2775 				 const type_t *rstp, tspec_t rst)
   2776 {
   2777 	if (!(lt == PTR && rt == PTR && (lst == VOID || rst == VOID ||
   2778 					 types_compatible(lstp, rstp,
   2779 					     true, false, NULL))))
   2780 		return false;
   2781 
   2782 	/* compatible pointer types (qualifiers ignored) */
   2783 	if (allow_c90 &&
   2784 	    ((!lstp->t_const && rstp->t_const) ||
   2785 	     (!lstp->t_volatile && rstp->t_volatile))) {
   2786 		/* left side has not all qualifiers of right */
   2787 		switch (op) {
   2788 		case INIT:
   2789 		case RETURN:
   2790 			/* incompatible pointer types to '%s' and '%s' */
   2791 			warning(182, type_name(lstp), type_name(rstp));
   2792 			break;
   2793 		case FARG:
   2794 			/* converting '%s' to incompatible '%s' ... */
   2795 			warning(153,
   2796 			    type_name(rtp), type_name(ltp), arg);
   2797 			break;
   2798 		default:
   2799 			/* operands of '%s' have incompatible pointer ... */
   2800 			warning(128, op_name(op),
   2801 			    type_name(lstp), type_name(rstp));
   2802 			break;
   2803 		}
   2804 	}
   2805 
   2806 	if (allow_c90)
   2807 		check_unconst_function(lstp, rn);
   2808 
   2809 	return true;
   2810 }
   2811 
   2812 static bool
   2813 check_assign_pointer_integer(op_t op, int arg,
   2814 			     const type_t *ltp, tspec_t lt,
   2815 			     const type_t *rtp, tspec_t rt)
   2816 {
   2817 
   2818 	if (!((lt == PTR && is_integer(rt)) || (is_integer(lt) && rt == PTR)))
   2819 		return false;
   2820 
   2821 	const char *lx = lt == PTR ? "pointer" : "integer";
   2822 	const char *rx = rt == PTR ? "pointer" : "integer";
   2823 
   2824 	switch (op) {
   2825 	case INIT:
   2826 	case RETURN:
   2827 		/* illegal combination of %s '%s' and %s '%s' */
   2828 		warning(183, lx, type_name(ltp), rx, type_name(rtp));
   2829 		break;
   2830 	case FARG:
   2831 		/* illegal combination of %s '%s' and %s '%s', arg #%d */
   2832 		warning(154,
   2833 		    lx, type_name(ltp), rx, type_name(rtp), arg);
   2834 		break;
   2835 	default:
   2836 		/* illegal combination of %s '%s' and %s '%s', op '%s' */
   2837 		warning(123,
   2838 		    lx, type_name(ltp), rx, type_name(rtp), op_name(op));
   2839 		break;
   2840 	}
   2841 	return true;
   2842 }
   2843 
   2844 static bool
   2845 check_assign_pointer(op_t op, int arg,
   2846 		     const type_t *ltp, tspec_t lt,
   2847 		     const type_t *rtp, tspec_t rt)
   2848 {
   2849 	if (!(lt == PTR && rt == PTR))
   2850 		return false;
   2851 
   2852 	if (op == FARG)
   2853 		/* converting '%s' to incompatible '%s' for ... */
   2854 		warning(153, type_name(rtp), type_name(ltp), arg);
   2855 	else
   2856 		warn_incompatible_pointers(op, ltp, rtp);
   2857 	return true;
   2858 }
   2859 
   2860 static void
   2861 warn_assign(op_t op, int arg,
   2862 	    const type_t *ltp, tspec_t lt,
   2863 	    const type_t *rtp, tspec_t rt)
   2864 {
   2865 	switch (op) {
   2866 	case INIT:
   2867 		/* cannot initialize '%s' from '%s' */
   2868 		error(185, type_name(ltp), type_name(rtp));
   2869 		break;
   2870 	case RETURN:
   2871 		/* function has return type '%s' but returns '%s' */
   2872 		error(211, type_name(ltp), type_name(rtp));
   2873 		break;
   2874 	case FARG:
   2875 		/* passing '%s' to incompatible '%s', arg #%d */
   2876 		warning(155, type_name(rtp), type_name(ltp), arg);
   2877 		break;
   2878 	default:
   2879 		warn_incompatible_types(op, ltp, lt, rtp, rt);
   2880 		break;
   2881 	}
   2882 }
   2883 
   2884 /*
   2885  * Checks type compatibility for ASSIGN, INIT, FARG and RETURN
   2886  * and prints warnings/errors if necessary.
   2887  * Returns whether the types are (almost) compatible.
   2888  */
   2889 static bool
   2890 check_assign_types_compatible(op_t op, int arg,
   2891 			      const tnode_t *ln, const tnode_t *rn)
   2892 {
   2893 	tspec_t lt, rt, lst = NO_TSPEC, rst = NO_TSPEC;
   2894 	type_t *ltp, *rtp, *lstp = NULL, *rstp = NULL;
   2895 
   2896 	if ((lt = (ltp = ln->tn_type)->t_tspec) == PTR)
   2897 		lst = (lstp = ltp->t_subt)->t_tspec;
   2898 	if ((rt = (rtp = rn->tn_type)->t_tspec) == PTR)
   2899 		rst = (rstp = rtp->t_subt)->t_tspec;
   2900 
   2901 	if (lt == BOOL && is_scalar(rt))	/* C99 6.3.1.2 */
   2902 		return true;
   2903 
   2904 	if (is_arithmetic(lt) && (is_arithmetic(rt) || rt == BOOL))
   2905 		return true;
   2906 
   2907 	if (is_struct_or_union(lt) && is_struct_or_union(rt))
   2908 		return ltp->u.sou == rtp->u.sou;
   2909 
   2910 	if (lt == PTR && is_null_pointer(rn)) {
   2911 		if (is_integer(rn->tn_type->t_tspec))
   2912 			/* implicit conversion from integer 0 to pointer ... */
   2913 			query_message(15, type_name(ltp));
   2914 		return true;
   2915 	}
   2916 
   2917 	check_assign_void_pointer(op, arg, lt, lst, rt, rst);
   2918 
   2919 	if (check_assign_void_pointer_compat(op, arg,
   2920 	    ltp, lt, lstp, lst, rn, rtp, rt, rstp, rst))
   2921 		return true;
   2922 
   2923 	if (check_assign_pointer_integer(op, arg, ltp, lt, rtp, rt))
   2924 		return true;
   2925 
   2926 	if (check_assign_pointer(op, arg, ltp, lt, rtp, rt))
   2927 		return true;
   2928 
   2929 	warn_assign(op, arg, ltp, lt, rtp, rt);
   2930 	return false;
   2931 }
   2932 
   2933 static bool
   2934 has_side_effect(const tnode_t *tn) /* NOLINT(misc-no-recursion) */
   2935 {
   2936 	op_t op = tn->tn_op;
   2937 
   2938 	if (modtab[op].m_has_side_effect)
   2939 		return true;
   2940 
   2941 	if (op == CVT && tn->tn_type->t_tspec == VOID)
   2942 		return has_side_effect(tn->u.ops.left);
   2943 
   2944 	/* XXX: Why not has_side_effect(tn->u.ops.left) as well? */
   2945 	if (op == LOGAND || op == LOGOR)
   2946 		return has_side_effect(tn->u.ops.right);
   2947 
   2948 	/* XXX: Why not has_side_effect(tn->u.ops.left) as well? */
   2949 	if (op == QUEST)
   2950 		return has_side_effect(tn->u.ops.right);
   2951 
   2952 	if (op == COLON || op == COMMA) {
   2953 		return has_side_effect(tn->u.ops.left) ||
   2954 		    has_side_effect(tn->u.ops.right);
   2955 	}
   2956 
   2957 	return false;
   2958 }
   2959 
   2960 static bool
   2961 is_void_cast(const tnode_t *tn)
   2962 {
   2963 
   2964 	return tn->tn_op == CVT && tn->tn_cast &&
   2965 	    tn->tn_type->t_tspec == VOID;
   2966 }
   2967 
   2968 static bool
   2969 is_local_symbol(const tnode_t *tn)
   2970 {
   2971 
   2972 	return tn->tn_op == LOAD &&
   2973 	    tn->u.ops.left->tn_op == NAME &&
   2974 	    tn->u.ops.left->u.sym->s_scl == AUTO;
   2975 }
   2976 
   2977 static bool
   2978 is_int_constant_zero(const tnode_t *tn)
   2979 {
   2980 
   2981 	return tn->tn_op == CON &&
   2982 	    tn->tn_type->t_tspec == INT &&
   2983 	    tn->u.value.u.integer == 0;
   2984 }
   2985 
   2986 static void
   2987 check_null_effect(const tnode_t *tn)
   2988 {
   2989 
   2990 	if (hflag &&
   2991 	    !has_side_effect(tn) &&
   2992 	    !(is_void_cast(tn) && is_local_symbol(tn->u.ops.left)) &&
   2993 	    !(is_void_cast(tn) && is_int_constant_zero(tn->u.ops.left))) {
   2994 		/* expression has null effect */
   2995 		warning(129);
   2996 	}
   2997 }
   2998 
   2999 /*
   3000  * Check the types for specific operators and type combinations.
   3001  *
   3002  * At this point, the operands already conform to the type requirements of
   3003  * the operator, such as being integer, floating or scalar.
   3004  */
   3005 static bool
   3006 typeok_op(op_t op, int arg,
   3007 	  const tnode_t *ln, const type_t *ltp, tspec_t lt,
   3008 	  const tnode_t *rn, const type_t *rtp, tspec_t rt)
   3009 {
   3010 	switch (op) {
   3011 	case ARROW:
   3012 		return typeok_arrow(lt);
   3013 	case POINT:
   3014 		return typeok_point(ln, ltp, lt);
   3015 	case INCBEF:
   3016 	case DECBEF:
   3017 	case INCAFT:
   3018 	case DECAFT:
   3019 		return typeok_incdec(op, ln, ltp);
   3020 	case INDIR:
   3021 		return typeok_indir(ltp, lt);
   3022 	case ADDR:
   3023 		return typeok_address(op, ln, ltp, lt);
   3024 	case PLUS:
   3025 		return typeok_plus(op, ltp, lt, rtp, rt);
   3026 	case MINUS:
   3027 		return typeok_minus(op, ltp, lt, rtp, rt);
   3028 	case SHL:
   3029 		typeok_shl(op, lt, rt);
   3030 		goto shift;
   3031 	case SHR:
   3032 		typeok_shr(op, ln, lt, rn, rt);
   3033 	shift:
   3034 		typeok_shift(ltp, lt, rn, rt);
   3035 		break;
   3036 	case LT:
   3037 	case LE:
   3038 	case GT:
   3039 	case GE:
   3040 	compare:
   3041 		return typeok_compare(op, ln, ltp, lt, rn, rtp, rt);
   3042 	case EQ:
   3043 	case NE:
   3044 		if (is_typeok_eq(ln, lt, rn, rt))
   3045 			break;
   3046 		goto compare;
   3047 	case QUEST:
   3048 		return typeok_quest(lt, rn);
   3049 	case COLON:
   3050 		return typeok_colon(ln, ltp, lt, rn, rtp, rt);
   3051 	case ASSIGN:
   3052 	case INIT:
   3053 	case FARG:
   3054 	case RETURN:
   3055 		if (!check_assign_types_compatible(op, arg, ln, rn))
   3056 			return false;
   3057 		goto assign;
   3058 	case MULASS:
   3059 	case DIVASS:
   3060 	case MODASS:
   3061 		goto assign;
   3062 	case ADDASS:
   3063 	case SUBASS:
   3064 		if ((lt == PTR && !is_integer(rt)) || rt == PTR) {
   3065 			warn_incompatible_types(op, ltp, lt, rtp, rt);
   3066 			return false;
   3067 		}
   3068 		goto assign;
   3069 	case SHLASS:
   3070 		goto assign;
   3071 	case SHRASS:
   3072 		if (pflag && !is_uinteger(lt) &&
   3073 		    !(!allow_c90 && is_uinteger(rt))) {
   3074 			/* bitwise '%s' on signed value possibly nonportable */
   3075 			warning(117, op_name(op));
   3076 		}
   3077 		goto assign;
   3078 	case ANDASS:
   3079 	case XORASS:
   3080 	case ORASS:
   3081 	assign:
   3082 		return typeok_assign(op, ln, ltp, lt);
   3083 	case COMMA:
   3084 		if (!modtab[ln->tn_op].m_has_side_effect)
   3085 			check_null_effect(ln);
   3086 		break;
   3087 	default:
   3088 		break;
   3089 	}
   3090 	return true;
   3091 }
   3092 
   3093 static void
   3094 check_bad_enum_operation(op_t op, const tnode_t *ln, const tnode_t *rn)
   3095 {
   3096 
   3097 	if (!eflag)
   3098 		return;
   3099 
   3100 	/* Allow enum in array indices. */
   3101 	if (op == PLUS &&
   3102 	    ((ln->tn_type->t_is_enum && rn->tn_type->t_tspec == PTR) ||
   3103 	     (rn->tn_type->t_is_enum && ln->tn_type->t_tspec == PTR))) {
   3104 		return;
   3105 	}
   3106 
   3107 	/* dubious operation '%s' on enum */
   3108 	warning(241, op_name(op));
   3109 }
   3110 
   3111 static void
   3112 check_enum_type_mismatch(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
   3113 {
   3114 	const mod_t *mp = &modtab[op];
   3115 
   3116 	if (ln->tn_type->u.enumer != rn->tn_type->u.enumer) {
   3117 		switch (op) {
   3118 		case INIT:
   3119 			/* enum type mismatch between '%s' and '%s' in ... */
   3120 			warning(210,
   3121 			    type_name(ln->tn_type), type_name(rn->tn_type));
   3122 			break;
   3123 		case FARG:
   3124 			/* function expects '%s', passing '%s' for arg #%d */
   3125 			warning(156,
   3126 			    type_name(ln->tn_type), type_name(rn->tn_type),
   3127 			    arg);
   3128 			break;
   3129 		case RETURN:
   3130 			/* function has return type '%s' but returns '%s' */
   3131 			warning(211,
   3132 			    type_name(ln->tn_type), type_name(rn->tn_type));
   3133 			break;
   3134 		default:
   3135 			/* enum type mismatch: '%s' '%s' '%s' */
   3136 			warning(130, type_name(ln->tn_type), op_name(op),
   3137 			    type_name(rn->tn_type));
   3138 			break;
   3139 		}
   3140 	} else if (Pflag && eflag && mp->m_comparison && op != EQ && op != NE)
   3141 		/* operator '%s' assumes that '%s' is ordered */
   3142 		warning(243, op_name(op), type_name(ln->tn_type));
   3143 }
   3144 
   3145 static void
   3146 check_enum_int_mismatch(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
   3147 {
   3148 
   3149 	if (!eflag)
   3150 		return;
   3151 
   3152 	switch (op) {
   3153 	case INIT:
   3154 		/*
   3155 		 * Initialization with 0 is allowed. Otherwise, all implicit
   3156 		 * initializations would need to be warned upon as well.
   3157 		 */
   3158 		if (!rn->tn_type->t_is_enum && rn->tn_op == CON &&
   3159 		    is_integer(rn->tn_type->t_tspec) &&
   3160 		    rn->u.value.u.integer == 0) {
   3161 			return;
   3162 		}
   3163 		/* initialization of '%s' with '%s' */
   3164 		warning(277, type_name(ln->tn_type), type_name(rn->tn_type));
   3165 		break;
   3166 	case FARG:
   3167 		/* combination of '%s' and '%s', arg #%d */
   3168 		warning(278,
   3169 		    type_name(ln->tn_type), type_name(rn->tn_type), arg);
   3170 		break;
   3171 	case RETURN:
   3172 		/* combination of '%s' and '%s' in return */
   3173 		warning(279, type_name(ln->tn_type), type_name(rn->tn_type));
   3174 		break;
   3175 	default:
   3176 		/* combination of '%s' and '%s', op '%s' */
   3177 		warning(242, type_name(ln->tn_type), type_name(rn->tn_type),
   3178 		    op_name(op));
   3179 		break;
   3180 	}
   3181 }
   3182 
   3183 static void
   3184 typeok_enum(op_t op, const mod_t *mp, int arg,
   3185 	    const tnode_t *ln, const type_t *ltp,
   3186 	    const tnode_t *rn, const type_t *rtp)
   3187 {
   3188 	if (mp->m_bad_on_enum &&
   3189 	    (ltp->t_is_enum || (mp->m_binary && rtp->t_is_enum))) {
   3190 		check_bad_enum_operation(op, ln, rn);
   3191 	} else if (mp->m_valid_on_enum &&
   3192 	    (ltp->t_is_enum && rtp != NULL && rtp->t_is_enum)) {
   3193 		check_enum_type_mismatch(op, arg, ln, rn);
   3194 	} else if (mp->m_valid_on_enum &&
   3195 	    (ltp->t_is_enum || (rtp != NULL && rtp->t_is_enum))) {
   3196 		check_enum_int_mismatch(op, arg, ln, rn);
   3197 	}
   3198 }
   3199 
   3200 /* Perform most type checks. Return whether the types are ok. */
   3201 bool
   3202 typeok(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
   3203 {
   3204 
   3205 	const mod_t *mp = &modtab[op];
   3206 
   3207 	type_t *ltp = ln->tn_type;
   3208 	tspec_t lt = ltp->t_tspec;
   3209 
   3210 	type_t *rtp = mp->m_binary ? rn->tn_type : NULL;
   3211 	tspec_t rt = mp->m_binary ? rtp->t_tspec : NO_TSPEC;
   3212 
   3213 	if (Tflag && !typeok_scalar_strict_bool(op, mp, arg, ln, rn))
   3214 		return false;
   3215 	if (!typeok_scalar(op, mp, ltp, lt, rtp, rt))
   3216 		return false;
   3217 
   3218 	if (!typeok_op(op, arg, ln, ltp, lt, rn, rtp, rt))
   3219 		return false;
   3220 
   3221 	typeok_enum(op, mp, arg, ln, ltp, rn, rtp);
   3222 	return true;
   3223 }
   3224 
   3225 /* In traditional C, keep unsigned and promote FLOAT to DOUBLE. */
   3226 static tspec_t
   3227 promote_trad(tspec_t t)
   3228 {
   3229 
   3230 	if (t == UCHAR || t == USHORT)
   3231 		return UINT;
   3232 	if (t == CHAR || t == SCHAR || t == SHORT)
   3233 		return INT;
   3234 	if (t == FLOAT)
   3235 		return DOUBLE;
   3236 	if (t == ENUM)
   3237 		return INT;
   3238 	return t;
   3239 }
   3240 
   3241 /*
   3242  * C99 6.3.1.1p2 requires for types with lower rank than int that "If an int
   3243  * can represent all the values of the original type, the value is converted
   3244  * to an int; otherwise it is converted to an unsigned int", and that "All
   3245  * other types are unchanged by the integer promotions".
   3246  */
   3247 static tspec_t
   3248 promote_c90(const tnode_t *tn, tspec_t t, bool farg)
   3249 {
   3250 	if (tn->tn_type->t_bitfield) {
   3251 		unsigned int width = tn->tn_type->t_bit_field_width;
   3252 		unsigned int int_width = size_in_bits(INT);
   3253 		// XXX: What about _Bool bit-fields, since C99?
   3254 		if (width < int_width)
   3255 			return INT;
   3256 		if (width == int_width)
   3257 			return is_uinteger(t) ? UINT : INT;
   3258 		return t;
   3259 	}
   3260 
   3261 	if (t == CHAR || t == SCHAR)
   3262 		return INT;
   3263 	if (t == UCHAR)
   3264 		return size_in_bits(CHAR) < size_in_bits(INT) ? INT : UINT;
   3265 	if (t == SHORT)
   3266 		return INT;
   3267 	if (t == USHORT)
   3268 		return size_in_bits(SHORT) < size_in_bits(INT) ? INT : UINT;
   3269 	if (t == ENUM)
   3270 		return INT;
   3271 	if (farg && t == FLOAT)
   3272 		return DOUBLE;
   3273 	return t;
   3274 }
   3275 
   3276 /*
   3277  * Performs the "integer promotions" (C99 6.3.1.1p2), which convert small
   3278  * integer types to either int or unsigned int.
   3279  *
   3280  * If allow_c90 is unset or the operand is a function argument with no type
   3281  * information (no prototype or variable # of args), converts float to double.
   3282  */
   3283 tnode_t *
   3284 promote(op_t op, bool farg, tnode_t *tn)
   3285 {
   3286 
   3287 	const type_t *otp = tn->tn_type;
   3288 	tspec_t ot = otp->t_tspec;
   3289 	if (!is_arithmetic(ot))
   3290 		return tn;
   3291 
   3292 	tspec_t nt = allow_c90 ? promote_c90(tn, ot, farg) : promote_trad(ot);
   3293 	if (nt == ot)
   3294 		return tn;
   3295 
   3296 	type_t *ntp = expr_dup_type(gettyp(nt));
   3297 	ntp->t_tspec = nt;
   3298 	ntp->t_is_enum = otp->t_is_enum;
   3299 	if (ntp->t_is_enum)
   3300 		ntp->u.enumer = otp->u.enumer;
   3301 	ntp->t_bitfield = otp->t_bitfield;
   3302 	if (ntp->t_bitfield) {
   3303 		ntp->t_bit_field_width = otp->t_bit_field_width;
   3304 		ntp->t_bit_field_offset = otp->t_bit_field_offset;
   3305 	}
   3306 	if (ntp->t_bitfield && is_uinteger(ot) && !is_uinteger(nt))
   3307 		ntp->t_bit_field_width++;
   3308 	return convert(op, 0, ntp, tn);
   3309 }
   3310 
   3311 static void
   3312 check_lossy_floating_to_integer_conversion(
   3313     op_t op, int arg, const type_t *tp, const tnode_t *tn)
   3314 {
   3315 	long double x = tn->u.value.u.floating;
   3316 	integer_constraints ic = ic_any(tp);
   3317 	if (is_uinteger(tp->t_tspec)
   3318 	    ? x >= ic.umin && x <= ic.umax && x == (uint64_t)x
   3319 	    : x >= ic.smin && x <= ic.smax && x == (int64_t)x)
   3320 		return;
   3321 	if (op == FARG)
   3322 		/* lossy conversion of %Lg to '%s', arg #%d */
   3323 		warning(380, x, type_name(tp), arg);
   3324 	else
   3325 		/* lossy conversion of %Lg to '%s' */
   3326 		warning(381, x, type_name(tp));
   3327 }
   3328 
   3329 static void
   3330 convert_integer_from_floating(
   3331     op_t op, int arg, const type_t *tp, const tnode_t *tn)
   3332 {
   3333 
   3334 	if (op == CVT)
   3335 		/* cast from floating point '%s' to integer '%s' */
   3336 		query_message(2, type_name(tn->tn_type), type_name(tp));
   3337 	else
   3338 		/* implicit conversion from floating point '%s' to ... */
   3339 		query_message(1, type_name(tn->tn_type), type_name(tp));
   3340 	if (tn->tn_op == CON)
   3341 		check_lossy_floating_to_integer_conversion(op, arg, tp, tn);
   3342 }
   3343 
   3344 static bool
   3345 should_warn_about_prototype_conversion(tspec_t nt,
   3346 				       tspec_t ot, const tnode_t *ptn)
   3347 {
   3348 
   3349 	if (nt == ot)
   3350 		return false;
   3351 
   3352 	if (nt == ENUM && ot == INT)
   3353 		return false;
   3354 
   3355 	if (is_floating(nt) != is_floating(ot) ||
   3356 	    portable_rank_cmp(nt, ot) != 0) {
   3357 		/* representation and/or width change */
   3358 		if (!is_integer(ot))
   3359 			return true;
   3360 		/*
   3361 		 * XXX: Investigate whether this rule makes sense; see
   3362 		 * tests/usr.bin/xlint/lint1/platform_long.c.
   3363 		 */
   3364 		return portable_rank_cmp(ot, INT) > 0;
   3365 	}
   3366 
   3367 	if (!hflag)
   3368 		return false;
   3369 
   3370 	/*
   3371 	 * If the types differ only in sign and the argument has the same
   3372 	 * representation in both types, print no warning.
   3373 	 */
   3374 	if (ptn->tn_op == CON && is_integer(nt) &&
   3375 	    signed_type(nt) == signed_type(ot) &&
   3376 	    !msb(ptn->u.value.u.integer, ot))
   3377 		return false;
   3378 
   3379 	return true;
   3380 }
   3381 
   3382 /*
   3383  * Warn if a prototype causes a type conversion that is different from what
   3384  * would happen to the same argument in the absence of a prototype.  This
   3385  * check is intended for code that needs to stay compatible with pre-C90 C.
   3386  *
   3387  * Errors/warnings about illegal type combinations are already printed
   3388  * in check_assign_types_compatible().
   3389  */
   3390 static void
   3391 check_prototype_conversion(int arg, tspec_t nt, tspec_t ot, type_t *tp,
   3392 			   tnode_t *tn)
   3393 {
   3394 
   3395 	if (!is_arithmetic(nt) || !is_arithmetic(ot))
   3396 		return;
   3397 
   3398 	/*
   3399 	 * If the type of the formal parameter is char/short, a warning would
   3400 	 * be useless, because functions declared the old style can't expect
   3401 	 * char/short arguments.
   3402 	 */
   3403 	if (nt == CHAR || nt == SCHAR || nt == UCHAR ||
   3404 	    nt == SHORT || nt == USHORT)
   3405 		return;
   3406 
   3407 	tnode_t *ptn = promote(NOOP, true, tn);
   3408 	ot = ptn->tn_type->t_tspec;
   3409 
   3410 	if (should_warn_about_prototype_conversion(nt, ot, ptn)) {
   3411 		/* argument %d is converted from '%s' to '%s' ... */
   3412 		warning(259, arg, type_name(tn->tn_type), type_name(tp));
   3413 	}
   3414 }
   3415 
   3416 /*
   3417  * When converting a large integer type to a small integer type, in some
   3418  * cases the value of the actual expression is further restricted than the
   3419  * type bounds, such as in (expr & 0xFF) or (expr % 100) or (expr >> 24).
   3420  */
   3421 static bool
   3422 can_represent(const type_t *tp, const tnode_t *tn)
   3423 {
   3424 
   3425 	debug_step("%s: type '%s'", __func__, type_name(tp));
   3426 	debug_node(tn);
   3427 
   3428 	uint64_t nmask = value_bits(width_in_bits(tp));
   3429 	if (!is_uinteger(tp->t_tspec))
   3430 		nmask >>= 1;
   3431 
   3432 	integer_constraints c = ic_expr(tn);
   3433 	if ((~c.bclr & ~nmask) == 0)
   3434 		return true;
   3435 
   3436 	integer_constraints tpc = ic_any(tp);
   3437 	if (is_uinteger(tp->t_tspec)
   3438 	    ? tpc.umin <= c.umin && tpc.umax >= c.umax
   3439 	    : tpc.smin <= c.smin && tpc.smax >= c.smax)
   3440 		return true;
   3441 
   3442 	return false;
   3443 }
   3444 
   3445 static bool
   3446 should_warn_about_integer_conversion(const type_t *ntp, tspec_t nt,
   3447 				     const tnode_t *otn, tspec_t ot)
   3448 {
   3449 
   3450 	// XXX: The portable_rank_cmp aims at portable mode, independent of the
   3451 	// current platform, while can_represent acts on the actual type sizes
   3452 	// from the current platform.  This mix is inconsistent, but anything
   3453 	// else would make the exact conditions too complicated to grasp.
   3454 	if (aflag > 0 && portable_rank_cmp(nt, ot) < 0) {
   3455 		if (ot == LONG || ot == ULONG
   3456 		    || ot == LLONG || ot == ULLONG
   3457 #ifdef INT128_SIZE
   3458 		    || ot == INT128 || ot == UINT128
   3459 #endif
   3460 		    || aflag > 1)
   3461 			return !can_represent(ntp, otn);
   3462 	}
   3463 	return false;
   3464 }
   3465 
   3466 static void
   3467 convert_integer_from_integer(op_t op, int arg, tspec_t nt, tspec_t ot,
   3468 			     type_t *tp, tnode_t *tn)
   3469 {
   3470 
   3471 	if (tn->tn_op == CON)
   3472 		return;
   3473 
   3474 	if (op == CVT)
   3475 		return;
   3476 
   3477 	if (Pflag && pflag && aflag > 0 &&
   3478 	    portable_rank_cmp(nt, ot) > 0 &&
   3479 	    is_uinteger(nt) != is_uinteger(ot)) {
   3480 		if (op == FARG)
   3481 			/* conversion to '%s' may sign-extend ... */
   3482 			warning(297, type_name(tp), arg);
   3483 		else
   3484 			/* conversion to '%s' may sign-extend ... */
   3485 			warning(131, type_name(tp));
   3486 	}
   3487 
   3488 	if (Pflag && portable_rank_cmp(nt, ot) > 0 &&
   3489 	    (tn->tn_op == PLUS || tn->tn_op == MINUS || tn->tn_op == MULT ||
   3490 	     tn->tn_op == SHL)) {
   3491 		/* suggest cast from '%s' to '%s' on op '%s' to ... */
   3492 		warning(324, type_name(gettyp(ot)), type_name(tp),
   3493 		    op_name(tn->tn_op));
   3494 	}
   3495 
   3496 	if (should_warn_about_integer_conversion(tp, nt, tn, ot)) {
   3497 		if (op == FARG) {
   3498 			/* conversion from '%s' to '%s' may lose ... */
   3499 			warning(298,
   3500 			    type_name(tn->tn_type), type_name(tp), arg);
   3501 		} else {
   3502 			/* conversion from '%s' to '%s' may lose accuracy */
   3503 			warning(132,
   3504 			    type_name(tn->tn_type), type_name(tp));
   3505 		}
   3506 	}
   3507 
   3508 	if (any_query_enabled && is_uinteger(nt) != is_uinteger(ot))
   3509 		/* implicit conversion changes sign from '%s' to '%s' */
   3510 		query_message(3, type_name(tn->tn_type), type_name(tp));
   3511 }
   3512 
   3513 static void
   3514 convert_integer_from_pointer(op_t op, tspec_t nt, type_t *tp, tnode_t *tn)
   3515 {
   3516 
   3517 	if (tn->tn_op == CON)
   3518 		return;
   3519 	if (op != CVT)
   3520 		return;		/* We already got an error. */
   3521 	if (portable_rank_cmp(nt, PTR) >= 0)
   3522 		return;
   3523 
   3524 	if (pflag && size_in_bits(nt) >= size_in_bits(PTR)) {
   3525 		/* conversion of pointer to '%s' may lose bits */
   3526 		warning(134, type_name(tp));
   3527 	} else {
   3528 		/* conversion of pointer to '%s' loses bits */
   3529 		warning(133, type_name(tp));
   3530 	}
   3531 }
   3532 
   3533 static bool
   3534 struct_starts_with(const type_t *struct_tp, const type_t *member_tp)
   3535 {
   3536 
   3537 	return struct_tp->u.sou->sou_first_member != NULL &&
   3538 	    types_compatible(struct_tp->u.sou->sou_first_member->s_type,
   3539 		member_tp, true, false, NULL);
   3540 }
   3541 
   3542 static bool
   3543 is_byte_array(const type_t *tp)
   3544 {
   3545 
   3546 	return tp->t_tspec == ARRAY &&
   3547 	    (tp->t_subt->t_tspec == CHAR || tp->t_subt->t_tspec == UCHAR);
   3548 }
   3549 
   3550 static bool
   3551 union_contains(const type_t *utp, const type_t *mtp)
   3552 {
   3553 	for (const sym_t *mem = utp->u.sou->sou_first_member;
   3554 	    mem != NULL; mem = mem->s_next) {
   3555 		if (types_compatible(mem->s_type, mtp, true, false, NULL))
   3556 			return true;
   3557 	}
   3558 	return false;
   3559 }
   3560 
   3561 static bool
   3562 should_warn_about_pointer_cast(const type_t *nstp, tspec_t nst,
   3563 			       const type_t *ostp, tspec_t ost)
   3564 {
   3565 
   3566 	while (nst == ARRAY)
   3567 		nstp = nstp->t_subt, nst = nstp->t_tspec;
   3568 	while (ost == ARRAY)
   3569 		ostp = ostp->t_subt, ost = ostp->t_tspec;
   3570 
   3571 	if (nst == STRUCT && ost == STRUCT &&
   3572 	    (struct_starts_with(nstp, ostp) ||
   3573 	     struct_starts_with(ostp, nstp)))
   3574 		return false;
   3575 
   3576 	if (is_incomplete(nstp) || is_incomplete(ostp))
   3577 		return false;
   3578 
   3579 	if (nst == CHAR || nst == UCHAR)
   3580 		return false;	/* for the sake of traditional C code */
   3581 	if (ost == CHAR || ost == UCHAR)
   3582 		return false;	/* for the sake of traditional C code */
   3583 
   3584 	/* Allow cast between pointers to sockaddr variants. */
   3585 	if (nst == STRUCT && ost == STRUCT) {
   3586 		const sym_t *nmem = nstp->u.sou->sou_first_member;
   3587 		const sym_t *omem = ostp->u.sou->sou_first_member;
   3588 		while (nmem != NULL && omem != NULL &&
   3589 		    types_compatible(nmem->s_type, omem->s_type,
   3590 			true, false, NULL))
   3591 			nmem = nmem->s_next, omem = omem->s_next;
   3592 		if (nmem != NULL && is_byte_array(nmem->s_type))
   3593 			return false;
   3594 		if (omem != NULL && is_byte_array(omem->s_type))
   3595 			return false;
   3596 		if (nmem == NULL && omem == NULL)
   3597 			return false;
   3598 	}
   3599 
   3600 	if (nst == UNION || ost == UNION) {
   3601 		const type_t *union_tp = nst == UNION ? nstp : ostp;
   3602 		const type_t *other_tp = nst == UNION ? ostp : nstp;
   3603 		if (union_contains(union_tp, other_tp))
   3604 			return false;
   3605 	}
   3606 
   3607 	if (is_struct_or_union(nst) && is_struct_or_union(ost))
   3608 		return nstp->u.sou != ostp->u.sou;
   3609 
   3610 	enum rank_kind rk1 = type_properties(nst)->tt_rank_kind;
   3611 	enum rank_kind rk2 = type_properties(ost)->tt_rank_kind;
   3612 	if (rk1 != rk2 || rk1 == RK_NONE)
   3613 		return true;
   3614 
   3615 	return portable_rank_cmp(nst, ost) != 0;
   3616 }
   3617 
   3618 static void
   3619 convert_pointer_from_pointer(type_t *ntp, tnode_t *tn)
   3620 {
   3621 	const type_t *nstp = ntp->t_subt;
   3622 	const type_t *otp = tn->tn_type;
   3623 	const type_t *ostp = otp->t_subt;
   3624 	tspec_t nst = nstp->t_tspec;
   3625 	tspec_t ost = ostp->t_tspec;
   3626 
   3627 	if (nst == VOID || ost == VOID) {
   3628 		/* TODO: C99 behaves like C90 here. */
   3629 		if (!allow_trad && !allow_c99 && (nst == FUNC || ost == FUNC)) {
   3630 			const char *nts, *ots;
   3631 			/* null pointers are already handled in convert() */
   3632 			*(nst == FUNC ? &nts : &ots) = "function pointer";
   3633 			*(nst == VOID ? &nts : &ots) = "'void *'";
   3634 			/* conversion of %s to %s requires a cast */
   3635 			warning(303, ots, nts);
   3636 		}
   3637 		return;
   3638 	}
   3639 	if (nst == FUNC && ost == FUNC)
   3640 		return;
   3641 	if (nst == FUNC || ost == FUNC) {
   3642 		/* converting '%s' to '%s' is questionable */
   3643 		warning(229, type_name(otp), type_name(ntp));
   3644 		return;
   3645 	}
   3646 
   3647 	if (hflag && alignment(nstp) > alignment(ostp) &&
   3648 	    ost != CHAR && ost != UCHAR &&
   3649 	    !is_incomplete(ostp) &&
   3650 	    !(nst == UNION && union_contains(nstp, ostp))) {
   3651 		/* converting '%s' to '%s' increases alignment ... */
   3652 		warning(135, type_name(otp), type_name(ntp),
   3653 		    alignment(ostp), alignment(nstp));
   3654 	}
   3655 
   3656 	if (cflag && should_warn_about_pointer_cast(nstp, nst, ostp, ost)) {
   3657 		/* pointer cast from '%s' to '%s' may be troublesome */
   3658 		warning(247, type_name(otp), type_name(ntp));
   3659 	}
   3660 }
   3661 
   3662 /*
   3663  * Insert a conversion operator, which converts the type of the node
   3664  * to another given type.
   3665  *
   3666  * Possible values for 'op':
   3667  *	CVT	a cast-expression
   3668  *	binary	integer promotion for one of the operands, or a usual
   3669  *		arithmetic conversion
   3670  *	binary	plain or compound assignments to bit-fields
   3671  *	FARG	'arg' is the number of the parameter (used for warnings)
   3672  *	NOOP	several other implicit conversions
   3673  *	...
   3674  */
   3675 tnode_t *
   3676 convert(op_t op, int arg, type_t *tp, tnode_t *tn)
   3677 {
   3678 	tspec_t nt = tp->t_tspec;
   3679 	tspec_t ot = tn->tn_type->t_tspec;
   3680 
   3681 	if (allow_trad && allow_c90 && op == FARG)
   3682 		check_prototype_conversion(arg, nt, ot, tp, tn);
   3683 
   3684 	if (nt == BOOL) {
   3685 		/* No further checks. */
   3686 
   3687 	} else if (is_integer(nt)) {
   3688 		if (ot == BOOL) {
   3689 			/* No further checks. */
   3690 		} else if (is_integer(ot))
   3691 			convert_integer_from_integer(op, arg, nt, ot, tp, tn);
   3692 		else if (is_floating(ot))
   3693 			convert_integer_from_floating(op, arg, tp, tn);
   3694 		else if (ot == PTR)
   3695 			convert_integer_from_pointer(op, nt, tp, tn);
   3696 
   3697 	} else if (is_floating(nt)) {
   3698 		if (is_integer(ot) && op != CVT) {
   3699 			/* implicit conversion from integer '%s' to ... */
   3700 			query_message(19,
   3701 			    type_name(tn->tn_type), type_name(tp));
   3702 		}
   3703 
   3704 	} else if (nt == PTR) {
   3705 		if (is_null_pointer(tn)) {
   3706 			/* a null pointer may be assigned to any pointer. */
   3707 		} else if (ot == PTR && op == CVT)
   3708 			convert_pointer_from_pointer(tp, tn);
   3709 	}
   3710 
   3711 	tnode_t *ntn = expr_alloc_tnode();
   3712 	ntn->tn_op = CVT;
   3713 	ntn->tn_type = tp;
   3714 	ntn->tn_cast = op == CVT;
   3715 	ntn->tn_sys |= tn->tn_sys;
   3716 	ntn->u.ops.right = NULL;
   3717 	if (tn->tn_op != CON || nt == VOID) {
   3718 		ntn->u.ops.left = tn;
   3719 	} else {
   3720 		ntn->tn_op = CON;
   3721 		convert_constant(op, arg, ntn->tn_type, &ntn->u.value,
   3722 		    &tn->u.value);
   3723 	}
   3724 
   3725 	return ntn;
   3726 }
   3727 
   3728 static void
   3729 convert_constant_from_floating(op_t op, int arg, const type_t *ntp,
   3730 			       tspec_t nt, val_t *nv, val_t *ov)
   3731 {
   3732 	long double max = 0.0, min = 0.0;
   3733 
   3734 	switch (nt) {
   3735 	case CHAR:
   3736 		max = TARG_CHAR_MAX;	min = TARG_CHAR_MIN;	break;
   3737 	case UCHAR:
   3738 		max = TARG_UCHAR_MAX;	min = 0;		break;
   3739 	case SCHAR:
   3740 		max = TARG_SCHAR_MAX;	min = TARG_SCHAR_MIN;	break;
   3741 	case SHORT:
   3742 		max = TARG_SHRT_MAX;	min = TARG_SHRT_MIN;	break;
   3743 	case USHORT:
   3744 		max = TARG_USHRT_MAX;	min = 0;		break;
   3745 	case ENUM:
   3746 	case INT:
   3747 		max = TARG_INT_MAX;	min = TARG_INT_MIN;	break;
   3748 	case UINT:
   3749 		max = TARG_UINT_MAX;	min = 0;		break;
   3750 	case LONG:
   3751 		max = TARG_LONG_MAX;	min = TARG_LONG_MIN;	break;
   3752 	case ULONG:
   3753 		max = TARG_ULONG_MAX;	min = 0;		break;
   3754 	case LLONG:
   3755 		max = LLONG_MAX;	min = LLONG_MIN;	break;
   3756 	case ULLONG:
   3757 		max = ULLONG_MAX;	min = 0;		break;
   3758 	case FLOAT:
   3759 	case FCOMPLEX:
   3760 		max = FLT_MAX;		min = -FLT_MAX;		break;
   3761 	case DOUBLE:
   3762 	case DCOMPLEX:
   3763 		max = DBL_MAX;		min = -DBL_MAX;		break;
   3764 	case LDOUBLE:
   3765 	case LCOMPLEX:
   3766 		/* LINTED 248; see floating_error_value. */
   3767 		max = LDBL_MAX;		min = -max;		break;
   3768 	default:
   3769 		lint_assert(/*CONSTCOND*/false);
   3770 	}
   3771 	if (ov->u.floating > max || ov->u.floating < min) {
   3772 		lint_assert(nt != LDOUBLE);
   3773 		const char *ot_name = type_name(gettyp(ov->v_tspec));
   3774 		const char *nt_name = type_name(ntp);
   3775 		if (is_integer(nt))
   3776 			goto after_warning;
   3777 		if (op == FARG)
   3778 			/* conversion of '%s' to '%s' is out of range, ... */
   3779 			warning(295, ot_name, nt_name, arg);
   3780 		else
   3781 			/* conversion of '%s' to '%s' is out of range */
   3782 			warning(119, ot_name, nt_name);
   3783 	after_warning:
   3784 		ov->u.floating = ov->u.floating > 0 ? max : min;
   3785 	}
   3786 
   3787 	if (nt == FLOAT || nt == FCOMPLEX)
   3788 		nv->u.floating = (float)ov->u.floating;
   3789 	else if (nt == DOUBLE || nt == DCOMPLEX)
   3790 		nv->u.floating = (double)ov->u.floating;
   3791 	else if (nt == LDOUBLE || nt == LCOMPLEX)
   3792 		nv->u.floating = ov->u.floating;
   3793 	else
   3794 		nv->u.integer = (int64_t)ov->u.floating;
   3795 }
   3796 
   3797 static bool
   3798 convert_constant_to_floating(tspec_t nt, val_t *nv,
   3799 			     tspec_t ot, const val_t *v)
   3800 {
   3801 	if (nt == FLOAT) {
   3802 		nv->u.floating = (ot == PTR || is_uinteger(ot)) ?
   3803 		    (float)(uint64_t)v->u.integer : (float)v->u.integer;
   3804 	} else if (nt == DOUBLE) {
   3805 		nv->u.floating = (ot == PTR || is_uinteger(ot)) ?
   3806 		    (double)(uint64_t)v->u.integer : (double)v->u.integer;
   3807 	} else if (nt == LDOUBLE) {
   3808 		nv->u.floating = (ot == PTR || is_uinteger(ot))
   3809 		    ? (long double)(uint64_t)v->u.integer
   3810 		    : (long double)v->u.integer;
   3811 	} else
   3812 		return false;
   3813 	return true;
   3814 }
   3815 
   3816 /*
   3817  * Print a warning if bits which were set are lost due to the conversion.
   3818  * This can happen with operator ORASS only.
   3819  */
   3820 static void
   3821 convert_constant_check_range_bitor(size_t nsz, size_t osz, const val_t *v,
   3822 				   uint64_t xmask, op_t op)
   3823 {
   3824 	if (nsz < osz && (v->u.integer & xmask) != 0)
   3825 		/* constant truncated by conversion, op '%s' */
   3826 		warning(306, op_name(op));
   3827 }
   3828 
   3829 static void
   3830 convert_constant_check_range_bitand(size_t nsz, size_t osz,
   3831 				    uint64_t xmask, const val_t *nv,
   3832 				    tspec_t ot, const val_t *v,
   3833 				    const type_t *tp, op_t op)
   3834 {
   3835 	if (nsz > osz &&
   3836 	    (nv->u.integer & bit((unsigned int)(osz - 1))) != 0 &&
   3837 	    (nv->u.integer & xmask) != xmask) {
   3838 		/* extra bits set to 0 in conversion of '%s' to '%s', ... */
   3839 		warning(309, type_name(gettyp(ot)),
   3840 		    type_name(tp), op_name(op));
   3841 	} else if (nsz < osz &&
   3842 	    (v->u.integer & xmask) != xmask &&
   3843 	    (v->u.integer & xmask) != 0)
   3844 		/* constant truncated by conversion, op '%s' */
   3845 		warning(306, op_name(op));
   3846 }
   3847 
   3848 static void
   3849 convert_constant_check_range_signed(op_t op, int arg,
   3850 				    const type_t *ntp, int64_t ov)
   3851 {
   3852 	if (op == ASSIGN)
   3853 		/* assignment of negative constant %lld to unsigned ... */
   3854 		warning(164, (long long)ov, type_name(ntp));
   3855 	else if (op == INIT)
   3856 		/* initialization of unsigned type '%s' with negative ... */
   3857 		warning(221, type_name(ntp), (long long)ov);
   3858 	else if (op == FARG)
   3859 		/* conversion of negative constant %lld to unsigned ... */
   3860 		warning(296, (long long)ov, type_name(ntp), arg);
   3861 	else if (modtab[op].m_comparison) {
   3862 		/* handled by check_integer_comparison() */
   3863 	} else
   3864 		/* conversion of negative constant %lld to unsigned ... */
   3865 		warning(222, (long long)ov, type_name(ntp));
   3866 }
   3867 
   3868 /*
   3869  * Loss of significant bit(s). All truncated bits of unsigned types or all
   3870  * truncated bits plus the msb of the target for signed types are considered
   3871  * to be significant bits. Loss of significant bits means that at least one
   3872  * of the bits was set in an unsigned type or that at least one but not all
   3873  * of the bits was set in a signed type. Loss of significant bits means that
   3874  * it is not possible, also not with necessary casts, to convert back to the
   3875  * original type. An example for a necessary cast is:
   3876  *	char c;	int	i; c = 128;
   3877  *	i = c;			** yields -128 **
   3878  *	i = (unsigned char)c;	** yields 128 **
   3879  */
   3880 static void
   3881 warn_constant_check_range_truncated(op_t op, int arg, const type_t *tp,
   3882 				    tspec_t ot)
   3883 {
   3884 	if (op == ASSIGN && tp->t_bitfield)
   3885 		/* precision lost in bit-field assignment */
   3886 		warning(166);
   3887 	else if (op == ASSIGN)
   3888 		/* constant truncated by assignment */
   3889 		warning(165);
   3890 	else if (op == INIT && tp->t_bitfield)
   3891 		/* bit-field initializer does not fit */
   3892 		warning(180);
   3893 	else if (op == INIT)
   3894 		/* initializer does not fit */
   3895 		warning(178);
   3896 	else if (op == CASE)
   3897 		/* case label affected by conversion */
   3898 		warning(196);
   3899 	else if (op == FARG)
   3900 		/* conversion of '%s' to '%s' is out of range, arg #%d */
   3901 		warning(295, type_name(gettyp(ot)), type_name(tp), arg);
   3902 	else
   3903 		/* conversion of '%s' to '%s' is out of range */
   3904 		warning(119, type_name(gettyp(ot)), type_name(tp));
   3905 }
   3906 
   3907 static void
   3908 warn_constant_check_range_loss(op_t op, int arg, const type_t *tp,
   3909 				  tspec_t ot)
   3910 {
   3911 	if (op == ASSIGN && tp->t_bitfield)
   3912 		/* precision lost in bit-field assignment */
   3913 		warning(166);
   3914 	else if (op == INIT && tp->t_bitfield)
   3915 		/* bit-field initializer out of range */
   3916 		warning(11);
   3917 	else if (op == CASE)
   3918 		/* case label affected by conversion */
   3919 		warning(196);
   3920 	else if (op == FARG)
   3921 		/* conversion of '%s' to '%s' is out of range, arg #%d */
   3922 		warning(295, type_name(gettyp(ot)), type_name(tp), arg);
   3923 	else
   3924 		/* conversion of '%s' to '%s' is out of range */
   3925 		warning(119, type_name(gettyp(ot)), type_name(tp));
   3926 }
   3927 
   3928 static void
   3929 convert_constant_check_range(tspec_t ot, const type_t *tp, tspec_t nt,
   3930 			     op_t op, int arg, const val_t *v, val_t *nv)
   3931 {
   3932 	unsigned int obitsz, nbitsz;
   3933 	uint64_t xmask, xmsk1;
   3934 
   3935 	obitsz = size_in_bits(ot);
   3936 	nbitsz = tp->t_bitfield ? tp->t_bit_field_width : size_in_bits(nt);
   3937 	xmask = value_bits(nbitsz) ^ value_bits(obitsz);
   3938 	xmsk1 = value_bits(nbitsz) ^ value_bits(obitsz - 1);
   3939 	if (op == ORASS || op == BITOR || op == BITXOR) {
   3940 		convert_constant_check_range_bitor(
   3941 		    nbitsz, obitsz, v, xmask, op);
   3942 	} else if (op == ANDASS || op == BITAND) {
   3943 		convert_constant_check_range_bitand(
   3944 		    nbitsz, obitsz, xmask, nv, ot, v, tp, op);
   3945 	} else if (nt != PTR && is_uinteger(nt) &&
   3946 	    ot != PTR && !is_uinteger(ot) &&
   3947 	    v->u.integer < 0)
   3948 		convert_constant_check_range_signed(op, arg,
   3949 		    tp, v->u.integer);
   3950 	else if (nv->u.integer != v->u.integer && nbitsz <= obitsz &&
   3951 	    (v->u.integer & xmask) != 0 &&
   3952 	    (is_uinteger(ot) || (v->u.integer & xmsk1) != xmsk1))
   3953 		warn_constant_check_range_truncated(op, arg, tp, ot);
   3954 	else if (nv->u.integer != v->u.integer)
   3955 		warn_constant_check_range_loss(op, arg, tp, ot);
   3956 }
   3957 
   3958 /* Converts a typed constant to a constant of another type. */
   3959 void
   3960 convert_constant(op_t op, int arg, const type_t *ntp, val_t *nv, val_t *ov)
   3961 {
   3962 	/*
   3963 	 * TODO: make 'ov' const; the name of this function does not suggest
   3964 	 *  that it modifies 'ov'.
   3965 	 */
   3966 	tspec_t ot = ov->v_tspec;
   3967 	tspec_t nt = nv->v_tspec = ntp->t_tspec;
   3968 	bool range_check = false;
   3969 
   3970 	if (nt == BOOL) {	/* C99 6.3.1.2 */
   3971 		nv->v_unsigned_since_c90 = false;
   3972 		nv->u.integer = is_nonzero_val(ov) ? 1 : 0;
   3973 		return;
   3974 	}
   3975 
   3976 	if (ot == FLOAT || ot == DOUBLE || ot == LDOUBLE)
   3977 		convert_constant_from_floating(op, arg, ntp, nt, nv, ov);
   3978 	else if (!convert_constant_to_floating(nt, nv, ot, ov)) {
   3979 		range_check = true;	/* Check for lost precision. */
   3980 		nv->u.integer = ov->u.integer;
   3981 	}
   3982 
   3983 	if (allow_trad && allow_c90 && ov->v_unsigned_since_c90 &&
   3984 	    (is_floating(nt) || (
   3985 		(is_integer(nt) && !is_uinteger(nt) &&
   3986 		    portable_rank_cmp(nt, ot) > 0)))) {
   3987 		/* C90 treats constant as unsigned */
   3988 		warning(157);
   3989 		ov->v_unsigned_since_c90 = false;
   3990 	}
   3991 
   3992 	if (is_integer(nt)) {
   3993 		unsigned int size = ntp->t_bitfield
   3994 		    ? ntp->t_bit_field_width : size_in_bits(nt);
   3995 		nv->u.integer = convert_integer(nv->u.integer, nt, size);
   3996 	}
   3997 
   3998 	if (range_check && op != CVT)
   3999 		convert_constant_check_range(ot, ntp, nt, op, arg, ov, nv);
   4000 }
   4001 
   4002 tnode_t *
   4003 build_sizeof(const type_t *tp)
   4004 {
   4005 	unsigned int size_in_bytes = type_size_in_bits(tp) / CHAR_SIZE;
   4006 	tnode_t *tn = build_integer_constant(SIZEOF_TSPEC, size_in_bytes);
   4007 	tn->tn_system_dependent = true;
   4008 	debug_step("build_sizeof '%s' = %u", type_name(tp), size_in_bytes);
   4009 	return tn;
   4010 }
   4011 
   4012 tnode_t *
   4013 build_offsetof(const type_t *tp, designation dn)
   4014 {
   4015 	unsigned int offset_in_bits = 0;
   4016 
   4017 	if (!is_struct_or_union(tp->t_tspec)) {
   4018 		/* unacceptable operand of '%s' */
   4019 		error(111, "offsetof");
   4020 		goto proceed;
   4021 	}
   4022 	for (size_t i = 0; i < dn.dn_len; i++) {
   4023 		const designator *dr = dn.dn_items + i;
   4024 		if (dr->dr_kind == DK_SUBSCRIPT) {
   4025 			if (tp->t_tspec != ARRAY)
   4026 				goto proceed;	/* silent error */
   4027 			tp = tp->t_subt;
   4028 			offset_in_bits += (unsigned) dr->dr_subscript
   4029 			    * type_size_in_bits(tp);
   4030 		} else {
   4031 			if (!is_struct_or_union(tp->t_tspec))
   4032 				goto proceed;	/* silent error */
   4033 			const char *name = dr->dr_member->s_name;
   4034 			sym_t *mem = find_member(tp->u.sou, name);
   4035 			if (mem == NULL) {
   4036 				/* type '%s' does not have member '%s' */
   4037 				error(101, name, type_name(tp));
   4038 				goto proceed;
   4039 			}
   4040 			tp = mem->s_type;
   4041 			offset_in_bits += mem->u.s_member.sm_offset_in_bits;
   4042 		}
   4043 	}
   4044 	free(dn.dn_items);
   4045 
   4046 proceed:;
   4047 	unsigned int offset_in_bytes = offset_in_bits / CHAR_SIZE;
   4048 	tnode_t *tn = build_integer_constant(SIZEOF_TSPEC, offset_in_bytes);
   4049 	tn->tn_system_dependent = true;
   4050 	return tn;
   4051 }
   4052 
   4053 unsigned int
   4054 type_size_in_bits(const type_t *tp)
   4055 {
   4056 
   4057 	unsigned int elem = 1;
   4058 	bool flex = false;
   4059 	lint_assert(tp != NULL);
   4060 	while (tp->t_tspec == ARRAY) {
   4061 		flex = true;	/* allow c99 flex arrays [] [0] */
   4062 		elem *= tp->u.dimension;
   4063 		tp = tp->t_subt;
   4064 	}
   4065 	if (elem == 0 && !flex) {
   4066 		/* cannot take size/alignment of incomplete type */
   4067 		error(143);
   4068 		elem = 1;
   4069 	}
   4070 
   4071 	unsigned int elsz;
   4072 	switch (tp->t_tspec) {
   4073 	case VOID:
   4074 		/* cannot take size/alignment of void */
   4075 		error(146);
   4076 		elsz = 1;
   4077 		break;
   4078 	case FUNC:
   4079 		/* cannot take size/alignment of function type '%s' */
   4080 		error(144, type_name(tp));
   4081 		elsz = 1;
   4082 		break;
   4083 	case STRUCT:
   4084 	case UNION:
   4085 		if (is_incomplete(tp)) {
   4086 			/* cannot take size/alignment of incomplete type */
   4087 			error(143);
   4088 			elsz = 1;
   4089 		} else
   4090 			elsz = tp->u.sou->sou_size_in_bits;
   4091 		break;
   4092 	case ENUM:
   4093 		if (is_incomplete(tp)) {
   4094 			/* cannot take size/alignment of incomplete type */
   4095 			warning(143);
   4096 		}
   4097 		/* FALLTHROUGH */
   4098 	default:
   4099 		if (tp->t_bitfield)
   4100 			/* cannot take size/alignment of bit-field */
   4101 			error(145);
   4102 		elsz = size_in_bits(tp->t_tspec);
   4103 		lint_assert(elsz > 0);
   4104 		break;
   4105 	}
   4106 
   4107 	return elem * elsz;
   4108 }
   4109 
   4110 /* C11 6.5.3.4, GCC */
   4111 tnode_t *
   4112 build_alignof(const type_t *tp)
   4113 {
   4114 	if (tp->t_tspec == FUNC) {
   4115 		/* cannot take size/alignment of function type '%s' */
   4116 		error(144, type_name(tp));
   4117 		return NULL;
   4118 	}
   4119 	if (tp->t_tspec == VOID) {
   4120 		/* cannot take size/alignment of void */
   4121 		error(146);
   4122 		return NULL;
   4123 	}
   4124 	if (is_incomplete(tp)) {
   4125 		/* cannot take size/alignment of incomplete type */
   4126 		error(143);
   4127 		return NULL;
   4128 	}
   4129 	if (tp->t_bitfield) {
   4130 		/* cannot take size/alignment of bit-field */
   4131 		error(145);
   4132 		return NULL;
   4133 	}
   4134 	return build_integer_constant(SIZEOF_TSPEC, (int64_t)alignment(tp));
   4135 }
   4136 
   4137 static tnode_t *
   4138 cast_to_union(tnode_t *otn, bool sys, type_t *ntp)
   4139 {
   4140 
   4141 	if (!allow_gcc) {
   4142 		/* union cast is a GCC extension */
   4143 		error(328);
   4144 		return NULL;
   4145 	}
   4146 
   4147 	for (const sym_t *m = ntp->u.sou->sou_first_member;
   4148 	    m != NULL; m = m->s_next) {
   4149 		if (types_compatible(m->s_type, otn->tn_type,
   4150 		    false, false, NULL)) {
   4151 			tnode_t *ntn = build_op(CVT, sys, ntp, otn, NULL);
   4152 			ntn->tn_cast = true;
   4153 			return ntn;
   4154 		}
   4155 	}
   4156 
   4157 	/* type '%s' is not a member of '%s' */
   4158 	error(329, type_name(otn->tn_type), type_name(ntp));
   4159 	return NULL;
   4160 }
   4161 
   4162 // In GCC mode, allow 'nullptr + offset' as a constant expression.
   4163 static tnode_t *
   4164 null_pointer_offset(tnode_t *tn)
   4165 {
   4166 	uint64_t off = 0;
   4167 	const tnode_t *n = tn;
   4168 	while ((n->tn_op == PLUS || n->tn_op == MINUS)
   4169 	    && is_integer(n->u.ops.right->tn_type->t_tspec)) {
   4170 		off += (uint64_t)n->u.ops.right->u.value.u.integer;
   4171 		n = n->u.ops.left;
   4172 	}
   4173 	if (n->tn_type->t_tspec == PTR
   4174 	    && n->tn_op == ADDR
   4175 	    && n->u.ops.left->tn_op == INDIR
   4176 	    && n->u.ops.left->u.ops.left->tn_op == CON
   4177 	    && n->u.ops.left->u.ops.left->tn_type->t_tspec == PTR) {
   4178 		off += (uint64_t)n->u.ops.left->u.ops.left->u.value.u.integer;
   4179 		return build_integer_constant(SIZEOF_TSPEC, (int64_t)off);
   4180 	}
   4181 	return tn;
   4182 }
   4183 
   4184 tnode_t *
   4185 cast(tnode_t *tn, bool sys, type_t *tp)
   4186 {
   4187 
   4188 	if (tn == NULL)
   4189 		return NULL;
   4190 
   4191 	tn = cconv(tn);
   4192 
   4193 	lint_assert(tp != NULL);
   4194 	tspec_t nt = tp->t_tspec;
   4195 	tspec_t ot = tn->tn_type->t_tspec;
   4196 
   4197 	if (nt == VOID) {
   4198 		/*
   4199 		 * C90 6.3.4, C99 6.5.4p2 and C11 6.5.4p2 allow any type to be
   4200 		 * cast to void.  The only other allowed casts are from a
   4201 		 * scalar type to a scalar type.
   4202 		 */
   4203 	} else if (nt == UNION)
   4204 		return cast_to_union(tn, sys, tp);
   4205 	else if (nt == STRUCT || nt == ARRAY || nt == FUNC) {
   4206 		/* Casting to a struct is an undocumented GCC extension. */
   4207 		if (!(allow_gcc && nt == STRUCT))
   4208 			goto invalid_cast;
   4209 	} else if (is_struct_or_union(ot))
   4210 		goto invalid_cast;
   4211 	else if (ot == VOID) {
   4212 		/* improper cast of void expression */
   4213 		error(148);
   4214 		return NULL;
   4215 	} else if (is_integer(nt) && is_scalar(ot)) {
   4216 		tn = null_pointer_offset(tn);
   4217 	} else if (is_floating(nt) && is_arithmetic(ot)) {
   4218 		/* ok */
   4219 	} else if (nt == PTR && is_integer(ot)) {
   4220 		/* ok */
   4221 	} else if (nt == PTR && ot == PTR) {
   4222 		if (!tp->t_subt->t_const && tn->tn_type->t_subt->t_const) {
   4223 			if (hflag)
   4224 				/* cast discards 'const' from type '%s' */
   4225 				warning(275, type_name(tn->tn_type));
   4226 		}
   4227 	} else
   4228 		goto invalid_cast;
   4229 
   4230 	if (any_query_enabled
   4231 	    && types_compatible(tp, tn->tn_type, false, false, NULL))
   4232 		/* no-op cast from '%s' to '%s' */
   4233 		query_message(6, type_name(tn->tn_type), type_name(tp));
   4234 
   4235 	tn = convert(CVT, 0, tp, tn);
   4236 	tn->tn_cast = true;
   4237 	tn->tn_sys = sys;
   4238 
   4239 	return tn;
   4240 
   4241 invalid_cast:
   4242 	/* invalid cast from '%s' to '%s' */
   4243 	error(147, type_name(tn->tn_type), type_name(tp));
   4244 	return NULL;
   4245 }
   4246 
   4247 void
   4248 add_function_argument(function_call *call, tnode_t *arg)
   4249 {
   4250 	/*
   4251 	 * If there was a serious error in the expression for the argument,
   4252 	 * create a dummy argument so the positions of the remaining arguments
   4253 	 * will not change.
   4254 	 */
   4255 	if (arg == NULL)
   4256 		arg = build_integer_constant(INT, 0);
   4257 
   4258 	if (call->args_len >= call->args_cap) {
   4259 		call->args_cap += 8;
   4260 		tnode_t **new_args = expr_zero_alloc(
   4261 		    call->args_cap * sizeof(*call->args), "tnode*[]");
   4262 		if (call->args_len > 0)
   4263 			memcpy(new_args, call->args,
   4264 			    call->args_len * sizeof(*call->args));
   4265 		call->args = new_args;
   4266 	}
   4267 	call->args[call->args_len++] = arg;
   4268 }
   4269 
   4270 /*
   4271  * Compare the type of an argument with the corresponding type of a
   4272  * prototype parameter. If it is a valid combination, but both types
   4273  * are not the same, insert a conversion to convert the argument into
   4274  * the type of the parameter.
   4275  */
   4276 static tnode_t *
   4277 check_prototype_argument(
   4278 	int n,		/* pos of arg */
   4279 	type_t *tp,		/* expected type (from prototype) */
   4280 	tnode_t *tn)		/* argument */
   4281 {
   4282 	tnode_t *ln = xcalloc(1, sizeof(*ln));
   4283 	ln->tn_type = expr_unqualified_type(tp);
   4284 	ln->tn_lvalue = true;
   4285 	if (typeok(FARG, n, ln, tn)) {
   4286 		bool dowarn;
   4287 		if (!types_compatible(tp, tn->tn_type,
   4288 		    true, false, (dowarn = false, &dowarn)) || dowarn)
   4289 			tn = convert(FARG, n, tp, tn);
   4290 	}
   4291 	free(ln);
   4292 	return tn;
   4293 }
   4294 
   4295 /*
   4296  * Check types of all function arguments and insert conversions,
   4297  * if necessary.
   4298  */
   4299 static void
   4300 check_function_arguments(const function_call *call)
   4301 {
   4302 	type_t *ftp = call->func->tn_type->t_subt;
   4303 
   4304 	/* get # of parameters in the prototype */
   4305 	int npar = 0;
   4306 	for (const sym_t *p = ftp->u.params; p != NULL; p = p->s_next)
   4307 		npar++;
   4308 
   4309 	int narg = (int)call->args_len;
   4310 
   4311 	const sym_t *param = ftp->u.params;
   4312 	if (ftp->t_proto && npar != narg && !(ftp->t_vararg && npar < narg)) {
   4313 		/* argument mismatch: %d %s passed, %d expected */
   4314 		error(150, narg, narg != 1 ? "arguments" : "argument", npar);
   4315 		param = NULL;
   4316 	}
   4317 
   4318 	for (int i = 0; i < narg; i++) {
   4319 		tnode_t *arg = call->args[i];
   4320 
   4321 		/* some things which are always not allowed */
   4322 		tspec_t at = arg->tn_type->t_tspec;
   4323 		if (at == VOID) {
   4324 			/* void expressions may not be arguments, arg #%d */
   4325 			error(151, i + 1);
   4326 			return;
   4327 		}
   4328 		if (is_struct_or_union(at) && is_incomplete(arg->tn_type)) {
   4329 			/* argument cannot have unknown size, arg #%d */
   4330 			error(152, i + 1);
   4331 			return;
   4332 		}
   4333 		if (is_integer(at) &&
   4334 		    arg->tn_type->t_is_enum &&
   4335 		    is_incomplete(arg->tn_type)) {
   4336 			/* argument cannot have unknown size, arg #%d */
   4337 			warning(152, i + 1);
   4338 		}
   4339 
   4340 		arg = cconv(arg);
   4341 		call->args[i] = arg;
   4342 
   4343 		arg = param != NULL
   4344 		    ? check_prototype_argument(i + 1, param->s_type, arg)
   4345 		    : promote(NOOP, true, arg);
   4346 		call->args[i] = arg;
   4347 
   4348 		if (param != NULL)
   4349 			param = param->s_next;
   4350 	}
   4351 }
   4352 
   4353 tnode_t *
   4354 build_function_call(tnode_t *func, bool sys, function_call *call)
   4355 {
   4356 
   4357 	if (func == NULL)
   4358 		return NULL;
   4359 
   4360 	call->func = func;
   4361 	check_ctype_function_call(call);
   4362 
   4363 	func = cconv(func);
   4364 	call->func = func;
   4365 
   4366 	if (func->tn_type->t_tspec != PTR ||
   4367 	    func->tn_type->t_subt->t_tspec != FUNC) {
   4368 		/* cannot call '%s', must be a function */
   4369 		error(149, type_name(func->tn_type));
   4370 		return NULL;
   4371 	}
   4372 
   4373 	check_function_arguments(call);
   4374 
   4375 	tnode_t *ntn = expr_alloc_tnode();
   4376 	ntn->tn_op = CALL;
   4377 	ntn->tn_type = func->tn_type->t_subt->t_subt;
   4378 	ntn->tn_sys = sys;
   4379 	ntn->u.call = call;
   4380 	return ntn;
   4381 }
   4382 
   4383 /*
   4384  * Return the value of an integral constant expression.
   4385  * If the expression is not constant or its type is not an integer
   4386  * type, an error message is printed.
   4387  */
   4388 val_t *
   4389 integer_constant(tnode_t *tn, bool required)
   4390 {
   4391 
   4392 	if (tn != NULL)
   4393 		tn = cconv(tn);
   4394 	if (tn != NULL)
   4395 		tn = promote(NOOP, false, tn);
   4396 
   4397 	val_t *v = xcalloc(1, sizeof(*v));
   4398 
   4399 	if (tn == NULL) {
   4400 		lint_assert(seen_error);
   4401 		debug_step("constant node is null; returning 1 instead");
   4402 		v->v_tspec = INT;
   4403 		v->u.integer = 1;
   4404 		return v;
   4405 	}
   4406 
   4407 	v->v_tspec = tn->tn_type->t_tspec;
   4408 
   4409 	if (tn->tn_op == CON) {
   4410 		lint_assert(tn->tn_type->t_tspec == tn->u.value.v_tspec);
   4411 		if (is_integer(tn->u.value.v_tspec)) {
   4412 			v->v_unsigned_since_c90 =
   4413 			    tn->u.value.v_unsigned_since_c90;
   4414 			v->u.integer = tn->u.value.u.integer;
   4415 			return v;
   4416 		}
   4417 		v->u.integer = (int64_t)tn->u.value.u.floating;
   4418 	} else
   4419 		v->u.integer = 1;
   4420 
   4421 	if (required)
   4422 		/* integral constant expression expected */
   4423 		error(55);
   4424 	else
   4425 		/* variable array dimension is a C99/GCC extension */
   4426 		c99ism(318);
   4427 
   4428 	if (!is_integer(v->v_tspec))
   4429 		v->v_tspec = INT;
   4430 
   4431 	return v;
   4432 }
   4433 
   4434 static bool
   4435 is_constcond_false(const tnode_t *tn, tspec_t t)
   4436 {
   4437 	return (t == BOOL || t == INT) &&
   4438 	    tn->tn_op == CON && tn->u.value.u.integer == 0;
   4439 }
   4440 
   4441 /*
   4442  * Perform some tests on expressions which can't be done in build_binary()
   4443  * and functions called by build_binary(). These tests must be done here
   4444  * because we need some information about the context in which the operations
   4445  * are performed.
   4446  * After all tests are performed and dofreeblk is true, expr() frees the
   4447  * memory which is used for the expression.
   4448  */
   4449 void
   4450 expr(tnode_t *tn, bool vctx, bool cond, bool dofreeblk, bool is_do_while)
   4451 {
   4452 
   4453 	if (tn == NULL) {	/* in case of errors */
   4454 		expr_free_all();
   4455 		return;
   4456 	}
   4457 
   4458 	/* expr() is also called in global initializations */
   4459 	if (dcs->d_kind != DLK_EXTERN && !is_do_while)
   4460 		check_statement_reachable();
   4461 
   4462 	check_expr_misc(tn, vctx, cond, !cond, false, false, false);
   4463 	if (tn->tn_op == ASSIGN && !tn->tn_parenthesized) {
   4464 		if (hflag && cond)
   4465 			/* assignment in conditional context */
   4466 			warning(159);
   4467 	} else if (tn->tn_op == CON) {
   4468 		if (hflag && cond && !suppress_constcond &&
   4469 		    !tn->tn_system_dependent &&
   4470 		    !(is_do_while &&
   4471 		      is_constcond_false(tn, tn->tn_type->t_tspec)))
   4472 			/* constant in conditional context */
   4473 			warning(161);
   4474 	}
   4475 	if (!modtab[tn->tn_op].m_has_side_effect) {
   4476 		/*
   4477 		 * for left operands of COMMA this warning is already printed
   4478 		 */
   4479 		if (tn->tn_op != COMMA && !vctx && !cond)
   4480 			check_null_effect(tn);
   4481 	}
   4482 	debug_node(tn);
   4483 
   4484 	if (dofreeblk)
   4485 		expr_free_all();
   4486 }
   4487 
   4488 /* If the expression has the form '*(arr + idx)', check the array index. */
   4489 static void
   4490 check_array_index(const tnode_t *indir, bool taking_address)
   4491 {
   4492 	const tnode_t *plus, *arr, *idx;
   4493 
   4494 	if (indir->tn_op == INDIR
   4495 	    && (plus = indir->u.ops.left, plus->tn_op == PLUS)
   4496 	    && plus->u.ops.left->tn_op == ADDR
   4497 	    && (arr = plus->u.ops.left->u.ops.left, true)
   4498 	    && (arr->tn_op == STRING || arr->tn_op == NAME)
   4499 	    && arr->tn_type->t_tspec == ARRAY
   4500 	    && (idx = plus->u.ops.right, idx->tn_op == CON)
   4501 	    && (!is_incomplete(arr->tn_type) || idx->u.value.u.integer < 0))
   4502 		goto proceed;
   4503 	return;
   4504 
   4505 proceed:;
   4506 	int elsz = length_in_bits(arr->tn_type->t_subt, NULL);
   4507 	if (elsz == 0)
   4508 		return;
   4509 	elsz /= CHAR_SIZE;
   4510 
   4511 	/* Change the unit of the index from bytes to element size. */
   4512 	int64_t con = is_uinteger(idx->tn_type->t_tspec)
   4513 	    ? (int64_t)((uint64_t)idx->u.value.u.integer / elsz)
   4514 	    : idx->u.value.u.integer / elsz;
   4515 
   4516 	int dim = arr->tn_type->u.dimension + (taking_address ? 1 : 0);
   4517 
   4518 	if (!is_uinteger(idx->tn_type->t_tspec) && con < 0)
   4519 		/* array subscript %jd cannot be negative */
   4520 		warning(167, (intmax_t)con);
   4521 	else if (dim > 0 && (uint64_t)con >= (uint64_t)dim)
   4522 		/* array subscript %ju cannot be > %d */
   4523 		warning(168, (uintmax_t)con, dim - 1);
   4524 }
   4525 
   4526 static void
   4527 check_expr_addr(const tnode_t *ln, bool szof, bool fcall)
   4528 {
   4529 	/* XXX: Taking warn_about_unreachable into account here feels wrong. */
   4530 	if (ln->tn_op == NAME && (reached || !warn_about_unreachable)) {
   4531 		if (!szof)
   4532 			mark_as_set(ln->u.sym);
   4533 		mark_as_used(ln->u.sym, fcall, szof);
   4534 	}
   4535 	check_array_index(ln, true);
   4536 }
   4537 
   4538 /*
   4539  * If there is an asm statement in one of the compound statements around,
   4540  * there may be other side effects, so don't warn.
   4541  */
   4542 static bool
   4543 is_asm_around(void)
   4544 {
   4545 	for (decl_level *dl = dcs; dl != NULL; dl = dl->d_enclosing)
   4546 		if (dl->d_asm)
   4547 			return true;
   4548 	return false;
   4549 }
   4550 
   4551 static void
   4552 check_expr_side_effect(const tnode_t *ln, bool szof)
   4553 {
   4554 
   4555 	/* XXX: Taking warn_about_unreachable into account here feels wrong. */
   4556 	if (ln->tn_op == NAME && (reached || !warn_about_unreachable)) {
   4557 		scl_t sc = ln->u.sym->s_scl;
   4558 		if (sc != EXTERN && sc != STATIC &&
   4559 		    !ln->u.sym->s_set && !szof && !is_asm_around()) {
   4560 			/* '%s' may be used before set */
   4561 			warning(158, ln->u.sym->s_name);
   4562 			mark_as_set(ln->u.sym);
   4563 		}
   4564 		mark_as_used(ln->u.sym, false, false);
   4565 	}
   4566 }
   4567 
   4568 static void
   4569 check_expr_assign(const tnode_t *ln, bool szof)
   4570 {
   4571 	/* XXX: Taking warn_about_unreachable into account here feels wrong. */
   4572 	if (ln->tn_op == NAME && !szof && (reached || !warn_about_unreachable)) {
   4573 		mark_as_set(ln->u.sym);
   4574 		if (ln->u.sym->s_scl == EXTERN)
   4575 			outusg(ln->u.sym);
   4576 	}
   4577 	check_array_index(ln, false);
   4578 }
   4579 
   4580 static void
   4581 check_expr_call(const tnode_t *tn, const tnode_t *ln,
   4582 		bool szof, bool vctx, bool cond, bool retval_discarded)
   4583 {
   4584 	lint_assert(ln->tn_op == ADDR);
   4585 	lint_assert(ln->u.ops.left->tn_op == NAME);
   4586 	if (!szof && !is_compiler_builtin(ln->u.ops.left->u.sym->s_name))
   4587 		outcall(tn, vctx || cond, retval_discarded);
   4588 
   4589 	const function_call *call = tn->u.call;
   4590 	if (call->args_len == 4 || call->args_len == 5)
   4591 		check_snprintb(call);
   4592 }
   4593 
   4594 static void
   4595 check_expr_op(op_t op, const tnode_t *ln, bool szof, bool fcall, bool eqwarn)
   4596 {
   4597 	switch (op) {
   4598 	case ADDR:
   4599 		check_expr_addr(ln, szof, fcall);
   4600 		break;
   4601 	case LOAD:
   4602 		check_array_index(ln, false);
   4603 		/* FALLTHROUGH */
   4604 	case INCBEF:
   4605 	case DECBEF:
   4606 	case INCAFT:
   4607 	case DECAFT:
   4608 	case ADDASS:
   4609 	case SUBASS:
   4610 	case MULASS:
   4611 	case DIVASS:
   4612 	case MODASS:
   4613 	case ANDASS:
   4614 	case ORASS:
   4615 	case XORASS:
   4616 	case SHLASS:
   4617 	case SHRASS:
   4618 	case REAL:
   4619 	case IMAG:
   4620 		check_expr_side_effect(ln, szof);
   4621 		break;
   4622 	case ASSIGN:
   4623 		check_expr_assign(ln, szof);
   4624 		break;
   4625 	case EQ:
   4626 		if (hflag && eqwarn)
   4627 			/* operator '==' found where '=' was expected */
   4628 			warning(160);
   4629 		break;
   4630 	default:
   4631 		break;
   4632 	}
   4633 }
   4634 
   4635 /*
   4636  *	vctx			???
   4637  *	cond			whether the expression is a condition that
   4638  *				will be compared with 0
   4639  *	eqwarn			whether the operator '==' might be a
   4640  *				misspelled '='
   4641  *	fcall			whether the expression is a function call
   4642  *	retval_discarded	whether the return value of a function call
   4643  *				is discarded; such calls will be analyzed by
   4644  *				lint2 in messages 4, 8 and 9
   4645  *	szof			whether the expression is part of a sizeof
   4646  *				expression, which means that its value is
   4647  *				discarded since only the type is relevant
   4648  */
   4649 void
   4650 check_expr_misc(const tnode_t *tn, bool vctx, bool cond,
   4651 		bool eqwarn, bool fcall, bool retval_discarded, bool szof)
   4652 {
   4653 
   4654 	if (tn == NULL)
   4655 		return;
   4656 	op_t op = tn->tn_op;
   4657 	if (op == NAME || op == CON || op == STRING)
   4658 		return;
   4659 	bool is_direct = op == CALL
   4660 	    && tn->u.call->func->tn_op == ADDR
   4661 	    && tn->u.call->func->u.ops.left->tn_op == NAME;
   4662 	if (op == CALL) {
   4663 		const function_call *call = tn->u.call;
   4664 		if (is_direct)
   4665 			check_expr_call(tn, call->func,
   4666 			    szof, vctx, cond, retval_discarded);
   4667 		bool discard = op == CVT && tn->tn_type->t_tspec == VOID;
   4668 		check_expr_misc(call->func, false, false, false, is_direct,
   4669 		    discard, szof);
   4670 		for (size_t i = 0, n = call->args_len; i < n; i++)
   4671 			check_expr_misc(call->args[i],
   4672 			    true, false, false, false, false, szof);
   4673 		return;
   4674 	}
   4675 
   4676 	lint_assert(has_operands(tn));
   4677 	tnode_t *ln = tn->u.ops.left;
   4678 	tnode_t *rn = tn->u.ops.right;
   4679 	check_expr_op(op, ln, szof, fcall, eqwarn);
   4680 
   4681 	const mod_t *mp = &modtab[op];
   4682 	bool cvctx = mp->m_value_context;
   4683 	bool ccond = mp->m_compares_with_zero;
   4684 	bool eq = mp->m_warn_if_operand_eq &&
   4685 	    !ln->tn_parenthesized &&
   4686 	    rn != NULL && !rn->tn_parenthesized;
   4687 
   4688 	/*
   4689 	 * Values of operands of ':' are not used if the type of at least
   4690 	 * one of the operands (for GCC compatibility) is 'void'.
   4691 	 *
   4692 	 * XXX test/value context of QUEST should probably be used as
   4693 	 * context for both operands of COLON.
   4694 	 */
   4695 	if (op == COLON && tn->tn_type->t_tspec == VOID)
   4696 		cvctx = ccond = false;
   4697 	bool discard = op == CVT && tn->tn_type->t_tspec == VOID;
   4698 	check_expr_misc(ln, cvctx, ccond, eq, is_direct, discard, szof);
   4699 
   4700 	switch (op) {
   4701 	case LOGAND:
   4702 	case LOGOR:
   4703 		check_expr_misc(rn, false, true, eq, false, false, szof);
   4704 		break;
   4705 	case COLON:
   4706 		check_expr_misc(rn, cvctx, ccond, eq, false, false, szof);
   4707 		break;
   4708 	case COMMA:
   4709 		check_expr_misc(rn, vctx, cond, false, false, false, szof);
   4710 		break;
   4711 	default:
   4712 		if (mp->m_binary)
   4713 			check_expr_misc(rn, true, false, eq, false, false,
   4714 			    szof);
   4715 		break;
   4716 	}
   4717 }
   4718 
   4719 /*
   4720  * Return whether the expression can be used for static initialization.
   4721  *
   4722  * Constant initialization expressions must be constant or an address
   4723  * of a static object with an optional offset. In the first case,
   4724  * the result is returned in *offsp. In the second case, the static
   4725  * object is returned in *symp and the offset in *offsp.
   4726  *
   4727  * The expression can consist of PLUS, MINUS, ADDR, NAME, STRING and
   4728  * CON. Type conversions are allowed if they do not change binary
   4729  * representation (including width).
   4730  *
   4731  * C99 6.6 "Constant expressions"
   4732  * C99 6.7.8p4 restricts initializers for static storage duration
   4733  */
   4734 bool
   4735 constant_addr(const tnode_t *tn, const sym_t **symp, ptrdiff_t *offsp)
   4736 {
   4737 	const sym_t *sym;
   4738 	ptrdiff_t offs1, offs2;
   4739 	tspec_t t, ot;
   4740 
   4741 	switch (tn->tn_op) {
   4742 	case MINUS:
   4743 		if (tn->u.ops.right->tn_op == CVT)
   4744 			return constant_addr(tn->u.ops.right, symp, offsp);
   4745 		else if (tn->u.ops.right->tn_op != CON)
   4746 			return false;
   4747 		/* FALLTHROUGH */
   4748 	case PLUS:
   4749 		offs1 = offs2 = 0;
   4750 		if (tn->u.ops.left->tn_op == CON) {
   4751 			offs1 = (ptrdiff_t)tn->u.ops.left->u.value.u.integer;
   4752 			if (!constant_addr(tn->u.ops.right, &sym, &offs2))
   4753 				return false;
   4754 		} else if (tn->u.ops.right->tn_op == CON) {
   4755 			offs2 = (ptrdiff_t)tn->u.ops.right->u.value.u.integer;
   4756 			if (tn->tn_op == MINUS)
   4757 				offs2 = -offs2;
   4758 			if (!constant_addr(tn->u.ops.left, &sym, &offs1))
   4759 				return false;
   4760 		} else {
   4761 			return false;
   4762 		}
   4763 		*symp = sym;
   4764 		*offsp = offs1 + offs2;
   4765 		return true;
   4766 	case ADDR:
   4767 		if (tn->u.ops.left->tn_op == NAME) {
   4768 			*symp = tn->u.ops.left->u.sym;
   4769 			*offsp = 0;
   4770 			return true;
   4771 		} else {
   4772 			/*
   4773 			 * If this were the front end of a compiler, we would
   4774 			 * return a label instead of 0, at least if
   4775 			 * 'tn->u.ops.left->tn_op == STRING'.
   4776 			 */
   4777 			*symp = NULL;
   4778 			*offsp = 0;
   4779 			return true;
   4780 		}
   4781 	case CVT:
   4782 		t = tn->tn_type->t_tspec;
   4783 		ot = tn->u.ops.left->tn_type->t_tspec;
   4784 		if ((!is_integer(t) && t != PTR) ||
   4785 		    (!is_integer(ot) && ot != PTR)) {
   4786 			return false;
   4787 		}
   4788 #if 0
   4789 		/*-
   4790 		 * consider:
   4791 		 *	struct foo {
   4792 		 *		unsigned char a;
   4793 		 *	} f = {
   4794 		 *		(unsigned char)(unsigned long)
   4795 		 *		    (&(((struct foo *)0)->a))
   4796 		 *	};
   4797 		 * since psize(unsigned long) != psize(unsigned char),
   4798 		 * this fails.
   4799 		 */
   4800 		else if (psize(t) != psize(ot))
   4801 			return -1;
   4802 #endif
   4803 		return constant_addr(tn->u.ops.left, symp, offsp);
   4804 	default:
   4805 		return false;
   4806 	}
   4807 }
   4808 
   4809 /* Append s2 to s1, then free s2. */
   4810 buffer *
   4811 cat_strings(buffer *s1, buffer *s2)
   4812 {
   4813 
   4814 	if ((s1->data != NULL) != (s2->data != NULL)) {
   4815 		/* cannot concatenate wide and regular string literals */
   4816 		error(292);
   4817 		return s1;
   4818 	}
   4819 
   4820 	if (s1->data != NULL) {
   4821 		while (s1->len + s2->len + 1 > s1->cap)
   4822 			s1->cap *= 2;
   4823 		s1->data = xrealloc(s1->data, s1->cap);
   4824 		memcpy(s1->data + s1->len, s2->data, s2->len + 1);
   4825 		free(s2->data);
   4826 	}
   4827 	s1->len += s2->len;
   4828 	free(s2);
   4829 
   4830 	return s1;
   4831 }
   4832 
   4833 
   4834 typedef struct stmt_expr {
   4835 	memory_pool se_mem;
   4836 	sym_t *se_sym;
   4837 	struct stmt_expr *se_enclosing;
   4838 } stmt_expr;
   4839 
   4840 static stmt_expr *stmt_exprs;
   4841 
   4842 void
   4843 begin_statement_expr(void)
   4844 {
   4845 	debug_enter();
   4846 
   4847 	stmt_expr *se = xmalloc(sizeof(*se));
   4848 	se->se_mem = expr_save_memory();
   4849 	se->se_sym = NULL;
   4850 	se->se_enclosing = stmt_exprs;
   4851 	stmt_exprs = se;
   4852 }
   4853 
   4854 void
   4855 do_statement_expr(tnode_t *tn)
   4856 {
   4857 	block_level--;
   4858 	mem_block_level--;
   4859 	stmt_exprs->se_sym = tn != NULL
   4860 	    ? mktempsym(block_dup_type(tn->tn_type))
   4861 	    : NULL;		/* after a syntax error */
   4862 	mem_block_level++;
   4863 	block_level++;
   4864 	/* '({ ... })' is a GCC extension */
   4865 	gnuism(320);
   4866 }
   4867 
   4868 tnode_t *
   4869 end_statement_expr(void)
   4870 {
   4871 	tnode_t *tn;
   4872 
   4873 	stmt_expr *se = stmt_exprs;
   4874 	if (se->se_sym == NULL) {
   4875 		tn = NULL;	/* after a syntax error */
   4876 		goto end;
   4877 	}
   4878 
   4879 	tn = build_name(se->se_sym, false);
   4880 	(void)expr_save_memory();	/* leak */
   4881 	expr_restore_memory(se->se_mem);
   4882 	stmt_exprs = se->se_enclosing;
   4883 	free(se);
   4884 
   4885 end:
   4886 	debug_leave();
   4887 	return tn;
   4888 }
   4889 
   4890 bool
   4891 in_statement_expr(void)
   4892 {
   4893 	return stmt_exprs != NULL;
   4894 }
   4895