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