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