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