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