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