Home | History | Annotate | Line # | Download | only in lint1
tree.c revision 1.692
      1 /*	$NetBSD: tree.c,v 1.692 2025/09/14 12:27:42 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.692 2025/09/14 12:27:42 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 			    expr_type_name(ln), "negative constant");
    926 		} else if (op == LT || op == GE)
    927 			/* operator '%s' compares '%s' with '%s' */
    928 			warning(162, op_name(op), expr_type_name(ln), "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", expr_type_name(rn));
    937 		} else if (op == GT || op == LE)
    938 			/* operator '%s' compares '%s' with '%s' */
    939 			warning(162, op_name(op), "0", expr_type_name(rn));
    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 		    expr_type_name(tn), 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, expr_type_name(*lnp),
   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 (int64_t)((uint64_t)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, expr_type_name(tn));
   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, expr_type_name(tn));
   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 		/* '%s' %s '%s' differs between traditional C and C90 */
   1708 		warning(118, tspec_name(lt), "<<=", tspec_name(rt));
   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, expr_type_name(rn->u.ops.left),
   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 		    expr_type_name(ln), expr_type_name(rn));
   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, expr_type_name(ln), 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, expr_type_name(rn), 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, expr_type_name(tn));
   2311 			else
   2312 				/* left operand of '.' must be struct ... */
   2313 				error(103, expr_type_name(tn));
   2314 		} else {
   2315 			if (!allow_c90 && tn->tn_type->t_tspec == PTR)
   2316 				/* left operand of '->' must be pointer ... */
   2317 				warning(104, expr_type_name(tn));
   2318 			else
   2319 				/* left operand of '->' must be pointer ... */
   2320 				error(104, expr_type_name(tn));
   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(const tnode_t *ln, tspec_t lt,
   2564 	   const tnode_t *rn, tspec_t rt)
   2565 {
   2566 	tspec_t olt = before_conversion(ln)->tn_type->t_tspec;
   2567 	tspec_t ort = before_conversion(rn)->tn_type->t_tspec;
   2568 
   2569 	if (is_uinteger(olt))
   2570 		return;
   2571 
   2572 	if (pflag) {
   2573 		integer_constraints lc = ic_expr(ln);
   2574 		if (lc.bclr >> 63 != 0)
   2575 			return;
   2576 
   2577 		if (ln->tn_op != CON)
   2578 			/* bitwise '%s' on signed '%s' possibly nonportable */
   2579 			warning(117, ">>", expr_type_name(ln));
   2580 		else if (ln->u.value.u.integer < 0)
   2581 			/* bitwise '%s' on signed '%s' nonportable */
   2582 			warning(120, ">>", expr_type_name(ln));
   2583 		return;
   2584 	}
   2585 
   2586 	/* The left operand would become unsigned in traditional C. */
   2587 	if (allow_trad && allow_c90 && hflag && is_uinteger(ort)
   2588 	    && !(ln->tn_op == CON && ln->u.value.u.integer >= 0))
   2589 		/* '%s' %s '%s' differs between traditional C and C90 */
   2590 		warning(118, tspec_name(lt), ">>", tspec_name(rt));
   2591 
   2592 	/*
   2593 	 * In traditional C, the left operand would be extended
   2594 	 * (possibly sign-extended) and then shifted.
   2595 	 */
   2596 	if (allow_trad && allow_c90 && hflag && !is_uinteger(ort)
   2597 	    && portable_rank_cmp(lt, rt) < 0
   2598 	    && !(ln->tn_op == CON && ln->u.value.u.integer >= 0))
   2599 		/* '%s' %s '%s' differs between traditional C and C90 */
   2600 		warning(118, tspec_name(lt), ">>", tspec_name(rt));
   2601 }
   2602 
   2603 static void
   2604 typeok_shl(tspec_t lt, tspec_t rt)
   2605 {
   2606 	/*
   2607 	 * Traditional C performs the usual arithmetic conversions on the
   2608 	 * operands, C90 and later don't.
   2609 	 */
   2610 	if (hflag && allow_trad && allow_c90 && portable_rank_cmp(lt, rt) < 0)
   2611 		/* '%s' %s '%s' differs between traditional C and C90 */
   2612 		warning(118, tspec_name(lt), "<<", tspec_name(rt));
   2613 }
   2614 
   2615 static void
   2616 typeok_shift(const tnode_t *ln, tspec_t lt, const tnode_t *rn, tspec_t rt)
   2617 {
   2618 	if (rn->tn_op != CON)
   2619 		return;
   2620 
   2621 	if (!is_uinteger(rt) && rn->u.value.u.integer < 0)
   2622 		/* negative shift */
   2623 		warning(121);
   2624 	else if ((uint64_t)rn->u.value.u.integer == size_in_bits(lt))
   2625 		/* shift amount %u equals bit-size of '%s' */
   2626 		warning(267,
   2627 		    (unsigned)rn->u.value.u.integer, expr_type_name(ln));
   2628 	else if ((uint64_t)rn->u.value.u.integer > size_in_bits(lt)) {
   2629 		/* shift amount %llu is greater than bit-size %llu of '%s' */
   2630 		warning(122, (unsigned long long)rn->u.value.u.integer,
   2631 		    (unsigned long long)size_in_bits(lt),
   2632 		    expr_type_name(ln));
   2633 	}
   2634 }
   2635 
   2636 static bool
   2637 is_typeok_eq(const tnode_t *ln, tspec_t lt, const tnode_t *rn, tspec_t rt)
   2638 {
   2639 	if (lt == PTR && is_null_pointer(rn))
   2640 		return true;
   2641 	if (rt == PTR && is_null_pointer(ln))
   2642 		return true;
   2643 	return false;
   2644 }
   2645 
   2646 static void
   2647 warn_incompatible_pointers(op_t op, const type_t *ltp, const type_t *rtp)
   2648 {
   2649 	lint_assert(ltp->t_tspec == PTR);
   2650 	lint_assert(rtp->t_tspec == PTR);
   2651 
   2652 	tspec_t lt = ltp->t_subt->t_tspec;
   2653 	tspec_t rt = rtp->t_subt->t_tspec;
   2654 
   2655 	if (is_struct_or_union(lt) && is_struct_or_union(rt)) {
   2656 		if (op == RETURN)
   2657 			/* invalid structure pointer combination */
   2658 			warning(244);
   2659 		else {
   2660 			/* incompatible structure pointers: '%s' '%s' '%s' */
   2661 			warning(245, type_name(ltp),
   2662 			    op_name(op), type_name(rtp));
   2663 		}
   2664 	} else {
   2665 		if (op == RETURN)
   2666 			/* invalid combination of '%s' and '%s' */
   2667 			warning(184, type_name(ltp), type_name(rtp));
   2668 		else {
   2669 			/* invalid combination of '%s' and '%s', op '%s' */
   2670 			warning(124,
   2671 			    type_name(ltp), type_name(rtp), op_name(op));
   2672 		}
   2673 	}
   2674 }
   2675 
   2676 static void
   2677 check_pointer_comparison(op_t op, const tnode_t *ln, const tnode_t *rn)
   2678 {
   2679 	type_t *ltp = ln->tn_type, *rtp = rn->tn_type;
   2680 	tspec_t lst = ltp->t_subt->t_tspec, rst = rtp->t_subt->t_tspec;
   2681 
   2682 	if (lst == VOID || rst == VOID) {
   2683 		/* TODO: C99 behaves like C90 here. */
   2684 		if (!allow_trad && !allow_c99 &&
   2685 		    (lst == FUNC || rst == FUNC)) {
   2686 			/* (void *)0 is already handled in typeok() */
   2687 			const char *lsts, *rsts;
   2688 			*(lst == FUNC ? &lsts : &rsts) = "function pointer";
   2689 			*(lst == VOID ? &lsts : &rsts) = "'void *'";
   2690 			/* C90 or later forbid comparison of %s with %s */
   2691 			warning(274, lsts, rsts);
   2692 		}
   2693 		return;
   2694 	}
   2695 
   2696 	if (!types_compatible(ltp->t_subt, rtp->t_subt, true, false, NULL)) {
   2697 		warn_incompatible_pointers(op, ltp, rtp);
   2698 		return;
   2699 	}
   2700 
   2701 	if (lst == FUNC && rst == FUNC) {
   2702 		/* TODO: C99 behaves like C90 here, see C99 6.5.8p2. */
   2703 		if (!allow_trad && !allow_c99 && op != EQ && op != NE)
   2704 			/* pointers to functions can only be compared ... */
   2705 			warning(125);
   2706 	}
   2707 }
   2708 
   2709 static bool
   2710 typeok_compare(op_t op,
   2711 	       const tnode_t *ln, const type_t *ltp, tspec_t lt,
   2712 	       const tnode_t *rn, const type_t *rtp, tspec_t rt)
   2713 {
   2714 	if (lt == PTR && rt == PTR) {
   2715 		check_pointer_comparison(op, ln, rn);
   2716 		return true;
   2717 	}
   2718 
   2719 	if (lt != PTR && rt != PTR)
   2720 		return true;
   2721 
   2722 	if (!is_integer(lt) && !is_integer(rt)) {
   2723 		warn_incompatible_types(op, ltp, lt, rtp, rt);
   2724 		return false;
   2725 	}
   2726 
   2727 	const char *lx = lt == PTR ? "pointer" : "integer";
   2728 	const char *rx = rt == PTR ? "pointer" : "integer";
   2729 	/* invalid combination of %s '%s' and %s '%s', op '%s' */
   2730 	warning(123, lx, type_name(ltp), rx, type_name(rtp), op_name(op));
   2731 	return true;
   2732 }
   2733 
   2734 static bool
   2735 typeok_quest(tspec_t lt, const tnode_t *rn)
   2736 {
   2737 	if (!is_scalar(lt)) {
   2738 		/* first operand of '?' must have scalar type */
   2739 		error(170);
   2740 		return false;
   2741 	}
   2742 	lint_assert(before_conversion(rn)->tn_op == COLON);
   2743 	return true;
   2744 }
   2745 
   2746 static void
   2747 typeok_colon_pointer(const type_t *ltp, const type_t *rtp)
   2748 {
   2749 	type_t *lstp = ltp->t_subt;
   2750 	type_t *rstp = rtp->t_subt;
   2751 	tspec_t lst = lstp->t_tspec;
   2752 	tspec_t rst = rstp->t_tspec;
   2753 
   2754 	if ((lst == VOID && rst == FUNC) || (lst == FUNC && rst == VOID)) {
   2755 		/* (void *)0 is handled in typeok_colon */
   2756 		/* TODO: C99 behaves like C90 here. */
   2757 		if (!allow_trad && !allow_c99)
   2758 			/* conversion of %s to %s requires a cast, op %s */
   2759 			warning(305, "function pointer", "'void *'",
   2760 			    op_name(COLON));
   2761 		return;
   2762 	}
   2763 
   2764 	if (pointer_types_are_compatible(lstp, rstp, true))
   2765 		return;
   2766 	if (!types_compatible(lstp, rstp, true, false, NULL))
   2767 		warn_incompatible_pointers(COLON, ltp, rtp);
   2768 }
   2769 
   2770 static bool
   2771 typeok_colon(const tnode_t *ln, const type_t *ltp, tspec_t lt,
   2772 	     const tnode_t *rn, const type_t *rtp, tspec_t rt)
   2773 {
   2774 
   2775 	if (is_arithmetic(lt) && is_arithmetic(rt))
   2776 		return true;
   2777 	if (lt == BOOL && rt == BOOL)
   2778 		return true;
   2779 
   2780 	if (lt == STRUCT && rt == STRUCT && ltp->u.sou == rtp->u.sou)
   2781 		return true;
   2782 	if (lt == UNION && rt == UNION && ltp->u.sou == rtp->u.sou)
   2783 		return true;
   2784 
   2785 	if (lt == PTR && is_null_pointer(rn))
   2786 		return true;
   2787 	if (rt == PTR && is_null_pointer(ln))
   2788 		return true;
   2789 
   2790 	if ((lt == PTR && is_integer(rt)) || (is_integer(lt) && rt == PTR)) {
   2791 		const char *lx = lt == PTR ? "pointer" : "integer";
   2792 		const char *rx = rt == PTR ? "pointer" : "integer";
   2793 		/* invalid combination of %s '%s' and %s '%s', op '%s' */
   2794 		warning(123, lx, type_name(ltp),
   2795 		    rx, type_name(rtp), op_name(COLON));
   2796 		return true;
   2797 	}
   2798 
   2799 	if (lt == VOID || rt == VOID) {
   2800 		if (lt != VOID || rt != VOID)
   2801 			/* incompatible types '%s' and '%s' in conditional */
   2802 			warning(126, expr_type_name(ln), expr_type_name(rn));
   2803 		return true;
   2804 	}
   2805 
   2806 	if (lt == PTR && rt == PTR) {
   2807 		typeok_colon_pointer(ltp, rtp);
   2808 		return true;
   2809 	}
   2810 
   2811 	/* incompatible types '%s' and '%s' in conditional */
   2812 	error(126, expr_type_name(ln), expr_type_name(rn));
   2813 	return false;
   2814 }
   2815 
   2816 static bool
   2817 has_constant_member(const type_t *tp)
   2818 {
   2819 	lint_assert(is_struct_or_union(tp->t_tspec));
   2820 
   2821 	for (sym_t *m = tp->u.sou->sou_first_member;
   2822 	    m != NULL; m = m->s_next) {
   2823 		const type_t *mtp = m->s_type;
   2824 		if (mtp->t_const)
   2825 			return true;
   2826 		if (is_struct_or_union(mtp->t_tspec) &&
   2827 		    has_constant_member(mtp))
   2828 			return true;
   2829 	}
   2830 	return false;
   2831 }
   2832 
   2833 static bool
   2834 typeok_assign(op_t op, const tnode_t *ln, const type_t *ltp, tspec_t lt)
   2835 {
   2836 	if (op == RETURN || op == INIT || op == FARG)
   2837 		return true;
   2838 
   2839 	if (!ln->tn_lvalue) {
   2840 		if (ln->tn_op == CVT && ln->tn_cast &&
   2841 		    ln->u.ops.left->tn_op == LOAD)
   2842 			/* a cast does not yield an lvalue */
   2843 			error(163);
   2844 		/* %soperand of '%s' must be lvalue */
   2845 		error(114, "left ", op_name(op));
   2846 		return false;
   2847 	} else if (ltp->t_const
   2848 	    || (is_struct_or_union(lt) && has_constant_member(ltp))) {
   2849 		if (allow_c90)
   2850 			/* %soperand of '%s' must be modifiable lvalue */
   2851 			warning(115, "left ", op_name(op));
   2852 	}
   2853 	return true;
   2854 }
   2855 
   2856 static bool
   2857 typeok_scalar(op_t op, const mod_t *mp,
   2858 	      const type_t *ltp, tspec_t lt,
   2859 	      const type_t *rtp, tspec_t rt)
   2860 {
   2861 	if (mp->m_takes_bool && lt == BOOL && rt == BOOL)
   2862 		return true;
   2863 	if (mp->m_requires_integer) {
   2864 		if (!is_integer(lt) || (mp->m_binary && !is_integer(rt))) {
   2865 			warn_incompatible_types(op, ltp, lt, rtp, rt);
   2866 			return false;
   2867 		}
   2868 	} else if (mp->m_requires_integer_or_complex) {
   2869 		if ((!is_integer(lt) && !is_complex(lt)) ||
   2870 		    (mp->m_binary && (!is_integer(rt) && !is_complex(rt)))) {
   2871 			warn_incompatible_types(op, ltp, lt, rtp, rt);
   2872 			return false;
   2873 		}
   2874 	} else if (mp->m_requires_scalar) {
   2875 		if (!is_scalar(lt) || (mp->m_binary && !is_scalar(rt))) {
   2876 			warn_incompatible_types(op, ltp, lt, rtp, rt);
   2877 			return false;
   2878 		}
   2879 	} else if (mp->m_requires_arith) {
   2880 		if (!is_arithmetic(lt) ||
   2881 		    (mp->m_binary && !is_arithmetic(rt))) {
   2882 			warn_incompatible_types(op, ltp, lt, rtp, rt);
   2883 			return false;
   2884 		}
   2885 	}
   2886 	return true;
   2887 }
   2888 
   2889 static void
   2890 check_assign_void_pointer(op_t op, int arg,
   2891 			  tspec_t lt, tspec_t lst,
   2892 			  tspec_t rt, tspec_t rst)
   2893 {
   2894 
   2895 	if (!(lt == PTR && rt == PTR && (lst == VOID || rst == VOID)))
   2896 		return;
   2897 	/* two pointers, at least one pointer to void */
   2898 
   2899 	/* TODO: C99 behaves like C90 here. */
   2900 	if (!(!allow_trad && !allow_c99 && (lst == FUNC || rst == FUNC)))
   2901 		return;
   2902 	/* comb. of ptr to func and ptr to void */
   2903 
   2904 	const char *lts, *rts;
   2905 	*(lst == FUNC ? &lts : &rts) = "function pointer";
   2906 	*(lst == VOID ? &lts : &rts) = "'void *'";
   2907 
   2908 	switch (op) {
   2909 	case INIT:
   2910 	case RETURN:
   2911 		/* conversion of %s to %s requires a cast */
   2912 		warning(303, rts, lts);
   2913 		break;
   2914 	case FARG:
   2915 		/* conversion of %s to %s requires a cast, arg #%d */
   2916 		warning(304, rts, lts, arg);
   2917 		break;
   2918 	default:
   2919 		/* conversion of %s to %s requires a cast, op %s */
   2920 		warning(305, rts, lts, op_name(op));
   2921 		break;
   2922 	}
   2923 }
   2924 
   2925 static bool
   2926 is_direct_function_call(const tnode_t *tn, const char **out_name)
   2927 {
   2928 
   2929 	if (tn->tn_op == CALL
   2930 	    && tn->u.call->func->tn_op == ADDR
   2931 	    && tn->u.call->func->u.ops.left->tn_op == NAME) {
   2932 		*out_name = tn->u.call->func->u.ops.left->u.sym->s_name;
   2933 		return true;
   2934 	}
   2935 	return false;
   2936 }
   2937 
   2938 static bool
   2939 is_unconst_function(const char *name)
   2940 {
   2941 
   2942 	return strcmp(name, "memchr") == 0 ||
   2943 	    strcmp(name, "strchr") == 0 ||
   2944 	    strcmp(name, "strpbrk") == 0 ||
   2945 	    strcmp(name, "strrchr") == 0 ||
   2946 	    strcmp(name, "strstr") == 0;
   2947 }
   2948 
   2949 static bool
   2950 is_const_char_pointer(const tnode_t *tn)
   2951 {
   2952 	/*
   2953 	 * For traditional reasons, C99 6.4.5p5 defines that string literals
   2954 	 * have type 'char[]'.  They are often implicitly converted to 'char
   2955 	 * *', for example when they are passed as function arguments.
   2956 	 *
   2957 	 * C99 6.4.5p6 further defines that modifying a string that is
   2958 	 * constructed from a string literal invokes undefined behavior.
   2959 	 *
   2960 	 * Out of these reasons, string literals are treated as 'effectively
   2961 	 * const' here.
   2962 	 */
   2963 	if (tn->tn_op == CVT &&
   2964 	    tn->u.ops.left->tn_op == ADDR &&
   2965 	    tn->u.ops.left->u.ops.left->tn_op == STRING)
   2966 		return true;
   2967 
   2968 	const type_t *tp = before_conversion(tn)->tn_type;
   2969 	return tp->t_tspec == PTR &&
   2970 	    tp->t_subt->t_tspec == CHAR &&
   2971 	    tp->t_subt->t_const;
   2972 }
   2973 
   2974 static bool
   2975 is_const_pointer(const tnode_t *tn)
   2976 {
   2977 	const type_t *tp = before_conversion(tn)->tn_type;
   2978 	return tp->t_tspec == PTR && tp->t_subt->t_const;
   2979 }
   2980 
   2981 static void
   2982 check_unconst_function(const type_t *lstp, const tnode_t *rn)
   2983 {
   2984 	const char *function_name;
   2985 
   2986 	if (lstp->t_tspec == CHAR && !lstp->t_const &&
   2987 	    is_direct_function_call(rn, &function_name) &&
   2988 	    is_unconst_function(function_name) &&
   2989 	    rn->u.call->args_len >= 1 &&
   2990 	    is_const_char_pointer(rn->u.call->args[0])) {
   2991 		/* call to '%s' effectively discards 'const' from argument */
   2992 		warning(346, function_name);
   2993 	}
   2994 
   2995 	if (!lstp->t_const &&
   2996 	    is_direct_function_call(rn, &function_name) &&
   2997 	    strcmp(function_name, "bsearch") == 0 &&
   2998 	    rn->u.call->args_len >= 2 &&
   2999 	    is_const_pointer(rn->u.call->args[1])) {
   3000 		/* call to '%s' effectively discards 'const' from argument */
   3001 		warning(346, function_name);
   3002 	}
   3003 }
   3004 
   3005 static bool
   3006 check_assign_void_pointer_compat(op_t op, const function_call *call, int arg,
   3007 				 tspec_t lt,
   3008 				 const type_t *lstp, tspec_t lst,
   3009 				 const tnode_t *rn,
   3010 				 const type_t *rtp, tspec_t rt,
   3011 				 const type_t *rstp, tspec_t rst)
   3012 {
   3013 	if (!(lt == PTR && rt == PTR && (lst == VOID || rst == VOID ||
   3014 					 types_compatible(lstp, rstp,
   3015 					     true, false, NULL))))
   3016 		return false;
   3017 
   3018 	/* compatible pointer types (qualifiers ignored) */
   3019 	char qualifiers[32];
   3020 	snprintf(qualifiers, sizeof(qualifiers), "%s%s",
   3021 	    !lstp->t_const && rstp->t_const ? " const" : "",
   3022 	    !lstp->t_volatile && rstp->t_volatile ? " volatile" : "");
   3023 	if (allow_c90 && qualifiers[0] != '\0') {
   3024 		switch (op) {
   3025 		case INIT:
   3026 		case RETURN:
   3027 			/* '%s' discards '%s' from '%s' */
   3028 			warning(182, op_name(op),
   3029 			    qualifiers + 1, type_name(rtp));
   3030 			break;
   3031 		case FARG:
   3032 			/* passing '%s' as argument %d to '%s' discards '%s' */
   3033 			warning(383, type_name(rtp), arg,
   3034 			    function_call_descr(call), qualifiers + 1);
   3035 			break;
   3036 		default:
   3037 			/* operator '%s' discards '%s' from '%s' */
   3038 			warning(128, op_name(op),
   3039 			    qualifiers + 1, type_name(rtp));
   3040 			break;
   3041 		}
   3042 	}
   3043 
   3044 	if (allow_c90)
   3045 		check_unconst_function(lstp, rn);
   3046 
   3047 	return true;
   3048 }
   3049 
   3050 static bool
   3051 check_assign_pointer_integer(op_t op, int arg,
   3052 			     const type_t *ltp, tspec_t lt,
   3053 			     const type_t *rtp, tspec_t rt)
   3054 {
   3055 
   3056 	if (!((lt == PTR && is_integer(rt)) || (is_integer(lt) && rt == PTR)))
   3057 		return false;
   3058 
   3059 	const char *lx = lt == PTR ? "pointer" : "integer";
   3060 	const char *rx = rt == PTR ? "pointer" : "integer";
   3061 
   3062 	switch (op) {
   3063 	case INIT:
   3064 	case RETURN:
   3065 		/* invalid combination of %s '%s' and %s '%s' for '%s' */
   3066 		warning(183,
   3067 		    lx, type_name(ltp), rx, type_name(rtp), op_name(op));
   3068 		break;
   3069 	case FARG:
   3070 		/* invalid combination of %s '%s' and %s '%s', arg #%d */
   3071 		warning(154,
   3072 		    lx, type_name(ltp), rx, type_name(rtp), arg);
   3073 		break;
   3074 	default:
   3075 		/* invalid combination of %s '%s' and %s '%s', op '%s' */
   3076 		warning(123,
   3077 		    lx, type_name(ltp), rx, type_name(rtp), op_name(op));
   3078 		break;
   3079 	}
   3080 	return true;
   3081 }
   3082 
   3083 static bool
   3084 check_assign_pointer(op_t op, int arg,
   3085 		     const type_t *ltp, tspec_t lt,
   3086 		     const type_t *rtp, tspec_t rt)
   3087 {
   3088 	if (!(lt == PTR && rt == PTR))
   3089 		return false;
   3090 
   3091 	if (op == FARG)
   3092 		/* converting '%s' to incompatible '%s' for ... */
   3093 		warning(153, type_name(rtp), type_name(ltp), arg);
   3094 	else
   3095 		warn_incompatible_pointers(op, ltp, rtp);
   3096 	return true;
   3097 }
   3098 
   3099 static void
   3100 warn_assign(op_t op, int arg,
   3101 	    const type_t *ltp, tspec_t lt,
   3102 	    const type_t *rtp, tspec_t rt)
   3103 {
   3104 	switch (op) {
   3105 	case INIT:
   3106 		/* cannot initialize '%s' from '%s' */
   3107 		error(185, type_name(ltp), type_name(rtp));
   3108 		break;
   3109 	case RETURN:
   3110 		/* function has return type '%s' but returns '%s' */
   3111 		error(211, type_name(ltp), type_name(rtp));
   3112 		break;
   3113 	case FARG:
   3114 		/* passing '%s' to incompatible '%s', arg #%d */
   3115 		warning(155, type_name(rtp), type_name(ltp), arg);
   3116 		break;
   3117 	default:
   3118 		warn_incompatible_types(op, ltp, lt, rtp, rt);
   3119 		break;
   3120 	}
   3121 }
   3122 
   3123 /*
   3124  * Checks type compatibility for ASSIGN, INIT, FARG and RETURN
   3125  * and prints warnings/errors if necessary.
   3126  * Returns whether the types are (almost) compatible.
   3127  */
   3128 static bool
   3129 check_assign_types_compatible(op_t op, const function_call *call, int arg,
   3130 			      const tnode_t *ln, const tnode_t *rn)
   3131 {
   3132 	tspec_t lt, rt, lst = NO_TSPEC, rst = NO_TSPEC;
   3133 	type_t *ltp, *rtp, *lstp = NULL, *rstp = NULL;
   3134 
   3135 	if ((lt = (ltp = ln->tn_type)->t_tspec) == PTR)
   3136 		lst = (lstp = ltp->t_subt)->t_tspec;
   3137 	if ((rt = (rtp = rn->tn_type)->t_tspec) == PTR)
   3138 		rst = (rstp = rtp->t_subt)->t_tspec;
   3139 
   3140 	if (lt == BOOL && is_scalar(rt))	/* C99 6.3.1.2 */
   3141 		return true;
   3142 
   3143 	if (is_arithmetic(lt) && (is_arithmetic(rt) || rt == BOOL))
   3144 		return true;
   3145 
   3146 	if (is_struct_or_union(lt) && is_struct_or_union(rt))
   3147 		return ltp->u.sou == rtp->u.sou;
   3148 
   3149 	if (lt == PTR && is_null_pointer(rn)) {
   3150 		if (is_integer(rn->tn_type->t_tspec))
   3151 			/* implicit conversion from integer 0 to pointer ... */
   3152 			query_message(15, type_name(ltp));
   3153 		return true;
   3154 	}
   3155 
   3156 	check_assign_void_pointer(op, arg, lt, lst, rt, rst);
   3157 
   3158 	if (check_assign_void_pointer_compat(op, call, arg,
   3159 	    lt, lstp, lst, rn, rtp, rt, rstp, rst))
   3160 		return true;
   3161 
   3162 	if (check_assign_pointer_integer(op, arg, ltp, lt, rtp, rt))
   3163 		return true;
   3164 
   3165 	if (check_assign_pointer(op, arg, ltp, lt, rtp, rt))
   3166 		return true;
   3167 
   3168 	warn_assign(op, arg, ltp, lt, rtp, rt);
   3169 	return false;
   3170 }
   3171 
   3172 static bool
   3173 has_side_effect(const tnode_t *tn) /* NOLINT(misc-no-recursion) */
   3174 {
   3175 	op_t op = tn->tn_op;
   3176 
   3177 	if (modtab[op].m_has_side_effect)
   3178 		return true;
   3179 
   3180 	if (op == CVT && tn->tn_type->t_tspec == VOID)
   3181 		return has_side_effect(tn->u.ops.left);
   3182 
   3183 	/* XXX: Why not has_side_effect(tn->u.ops.left) as well? */
   3184 	if (op == LOGAND || op == LOGOR)
   3185 		return has_side_effect(tn->u.ops.right);
   3186 
   3187 	/* XXX: Why not has_side_effect(tn->u.ops.left) as well? */
   3188 	if (op == QUEST)
   3189 		return has_side_effect(tn->u.ops.right);
   3190 
   3191 	if (op == COLON || op == COMMA) {
   3192 		return has_side_effect(tn->u.ops.left) ||
   3193 		    has_side_effect(tn->u.ops.right);
   3194 	}
   3195 
   3196 	return false;
   3197 }
   3198 
   3199 static bool
   3200 is_void_cast(const tnode_t *tn)
   3201 {
   3202 
   3203 	return tn->tn_op == CVT && tn->tn_cast &&
   3204 	    tn->tn_type->t_tspec == VOID;
   3205 }
   3206 
   3207 static bool
   3208 is_local_symbol(const tnode_t *tn)
   3209 {
   3210 
   3211 	return tn->tn_op == LOAD &&
   3212 	    tn->u.ops.left->tn_op == NAME &&
   3213 	    tn->u.ops.left->u.sym->s_scl == AUTO;
   3214 }
   3215 
   3216 static bool
   3217 is_int_constant_zero(const tnode_t *tn)
   3218 {
   3219 
   3220 	return tn->tn_op == CON &&
   3221 	    tn->tn_type->t_tspec == INT &&
   3222 	    tn->u.value.u.integer == 0;
   3223 }
   3224 
   3225 static void
   3226 check_null_effect(const tnode_t *tn)
   3227 {
   3228 
   3229 	if (hflag &&
   3230 	    !has_side_effect(tn) &&
   3231 	    !(is_void_cast(tn) && is_local_symbol(tn->u.ops.left)) &&
   3232 	    !(is_void_cast(tn) && is_int_constant_zero(tn->u.ops.left))) {
   3233 		/* expression has null effect */
   3234 		warning(129);
   3235 	}
   3236 }
   3237 
   3238 /*
   3239  * Check the types for specific operators and type combinations.
   3240  *
   3241  * At this point, the operands already conform to the type requirements of
   3242  * the operator, such as being integer, floating or scalar.
   3243  */
   3244 static bool
   3245 typeok_op(op_t op, const function_call *call, int arg,
   3246 	  const tnode_t *ln, const type_t *ltp, tspec_t lt,
   3247 	  const tnode_t *rn, const type_t *rtp, tspec_t rt)
   3248 {
   3249 	switch (op) {
   3250 	case ARROW:
   3251 		return typeok_arrow(lt);
   3252 	case POINT:
   3253 		return typeok_point(ln, ltp, lt);
   3254 	case INCBEF:
   3255 	case DECBEF:
   3256 	case INCAFT:
   3257 	case DECAFT:
   3258 		return typeok_incdec(op, ln, ltp);
   3259 	case INDIR:
   3260 		return typeok_indir(ltp, lt);
   3261 	case ADDR:
   3262 		return typeok_address(op, ln, ltp, lt);
   3263 	case PLUS:
   3264 		return typeok_plus(op, ltp, lt, rtp, rt);
   3265 	case MINUS:
   3266 		return typeok_minus(op, ltp, lt, rtp, rt);
   3267 	case SHL:
   3268 		typeok_shl(lt, rt);
   3269 		goto shift;
   3270 	case SHR:
   3271 		typeok_shr(ln, lt, rn, rt);
   3272 	shift:
   3273 		typeok_shift(ln, lt, rn, rt);
   3274 		break;
   3275 	case LT:
   3276 	case LE:
   3277 	case GT:
   3278 	case GE:
   3279 	compare:
   3280 		return typeok_compare(op, ln, ltp, lt, rn, rtp, rt);
   3281 	case EQ:
   3282 	case NE:
   3283 		if (is_typeok_eq(ln, lt, rn, rt))
   3284 			break;
   3285 		goto compare;
   3286 	case QUEST:
   3287 		return typeok_quest(lt, rn);
   3288 	case COLON:
   3289 		return typeok_colon(ln, ltp, lt, rn, rtp, rt);
   3290 	case ASSIGN:
   3291 	case INIT:
   3292 	case FARG:
   3293 	case RETURN:
   3294 		if (!check_assign_types_compatible(op, call, arg, ln, rn))
   3295 			return false;
   3296 		goto assign;
   3297 	case MULASS:
   3298 	case DIVASS:
   3299 	case MODASS:
   3300 		goto assign;
   3301 	case ADDASS:
   3302 	case SUBASS:
   3303 		if ((lt == PTR && !is_integer(rt)) || rt == PTR) {
   3304 			warn_incompatible_types(op, ltp, lt, rtp, rt);
   3305 			return false;
   3306 		}
   3307 		goto assign;
   3308 	case SHLASS:
   3309 		goto assign;
   3310 	case SHRASS:
   3311 		if (pflag && !is_uinteger(lt) &&
   3312 		    !(!allow_c90 && is_uinteger(rt))) {
   3313 			/* bitwise '%s' on signed '%s' possibly nonportable */
   3314 			warning(117, op_name(op), expr_type_name(rn));
   3315 		}
   3316 		goto assign;
   3317 	case ANDASS:
   3318 	case XORASS:
   3319 	case ORASS:
   3320 	assign:
   3321 		return typeok_assign(op, ln, ltp, lt);
   3322 	case COMMA:
   3323 		if (!modtab[ln->tn_op].m_has_side_effect)
   3324 			check_null_effect(ln);
   3325 		break;
   3326 	default:
   3327 		break;
   3328 	}
   3329 	return true;
   3330 }
   3331 
   3332 static void
   3333 check_bad_enum_operation(op_t op, const tnode_t *ln, const tnode_t *rn)
   3334 {
   3335 
   3336 	if (!eflag)
   3337 		return;
   3338 
   3339 	/* Allow enum in array indices. */
   3340 	if (op == PLUS &&
   3341 	    ((ln->tn_type->t_is_enum && rn->tn_type->t_tspec == PTR) ||
   3342 	     (rn->tn_type->t_is_enum && ln->tn_type->t_tspec == PTR))) {
   3343 		return;
   3344 	}
   3345 
   3346 	/* dubious operation '%s' on enum */
   3347 	warning(241, op_name(op));
   3348 }
   3349 
   3350 static void
   3351 check_enum_type_mismatch(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
   3352 {
   3353 	const mod_t *mp = &modtab[op];
   3354 
   3355 	if (ln->tn_type->u.enumer != rn->tn_type->u.enumer) {
   3356 		switch (op) {
   3357 		case INIT:
   3358 			/* enum type mismatch between '%s' and '%s' in ... */
   3359 			warning(210,
   3360 			    type_name(ln->tn_type), type_name(rn->tn_type));
   3361 			break;
   3362 		case FARG:
   3363 			/* function expects '%s', passing '%s' for arg #%d */
   3364 			warning(156,
   3365 			    type_name(ln->tn_type), type_name(rn->tn_type),
   3366 			    arg);
   3367 			break;
   3368 		case RETURN:
   3369 			/* function has return type '%s' but returns '%s' */
   3370 			warning(211,
   3371 			    type_name(ln->tn_type), type_name(rn->tn_type));
   3372 			break;
   3373 		default:
   3374 			/* enum type mismatch: '%s' '%s' '%s' */
   3375 			warning(130, expr_type_name(before_conversion(ln)),
   3376 			    op_name(op),
   3377 			    expr_type_name(before_conversion(rn)));
   3378 			break;
   3379 		}
   3380 	} else if (Pflag && eflag && mp->m_comparison && op != EQ && op != NE)
   3381 		/* operator '%s' assumes that '%s' is ordered */
   3382 		warning(243, op_name(op), type_name(ln->tn_type));
   3383 }
   3384 
   3385 static void
   3386 check_enum_int_mismatch(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
   3387 {
   3388 
   3389 	if (!eflag)
   3390 		return;
   3391 
   3392 	switch (op) {
   3393 	case INIT:
   3394 		/*
   3395 		 * Initialization with 0 is allowed. Otherwise, all implicit
   3396 		 * initializations would need to be warned upon as well.
   3397 		 */
   3398 		if (!rn->tn_type->t_is_enum && rn->tn_op == CON &&
   3399 		    is_integer(rn->tn_type->t_tspec) &&
   3400 		    rn->u.value.u.integer == 0) {
   3401 			return;
   3402 		}
   3403 		/* initialization of '%s' with '%s' */
   3404 		warning(277, type_name(ln->tn_type), expr_type_name(rn));
   3405 		break;
   3406 	case FARG:
   3407 		/* combination of '%s' and '%s', arg #%d */
   3408 		warning(278,
   3409 		    type_name(ln->tn_type), expr_type_name(rn), arg);
   3410 		break;
   3411 	case RETURN:
   3412 		/* combination of '%s' and '%s' in return */
   3413 		warning(279, type_name(ln->tn_type), expr_type_name(rn));
   3414 		break;
   3415 	default:
   3416 		/* combination of '%s' and '%s', op '%s' */
   3417 		warning(242, type_name(ln->tn_type), expr_type_name(rn),
   3418 		    op_name(op));
   3419 		break;
   3420 	}
   3421 }
   3422 
   3423 static void
   3424 typeok_enum(op_t op, const mod_t *mp, int arg,
   3425 	    const tnode_t *ln, const type_t *ltp,
   3426 	    const tnode_t *rn, const type_t *rtp)
   3427 {
   3428 	if (mp->m_bad_on_enum &&
   3429 	    (ltp->t_is_enum || (mp->m_binary && rtp->t_is_enum))) {
   3430 		check_bad_enum_operation(op, ln, rn);
   3431 	} else if (mp->m_valid_on_enum &&
   3432 	    (ltp->t_is_enum && rtp != NULL && rtp->t_is_enum)) {
   3433 		check_enum_type_mismatch(op, arg, ln, rn);
   3434 	} else if (mp->m_valid_on_enum &&
   3435 	    (ltp->t_is_enum || (rtp != NULL && rtp->t_is_enum))) {
   3436 		check_enum_int_mismatch(op, arg, ln, rn);
   3437 	}
   3438 }
   3439 
   3440 /* Perform most type checks. Return whether the types are ok. */
   3441 bool
   3442 typeok(op_t op, const function_call *call, int arg,
   3443     const tnode_t *ln, const tnode_t *rn)
   3444 {
   3445 
   3446 	const mod_t *mp = &modtab[op];
   3447 
   3448 	type_t *ltp = ln->tn_type;
   3449 	tspec_t lt = ltp->t_tspec;
   3450 
   3451 	type_t *rtp = mp->m_binary ? rn->tn_type : NULL;
   3452 	tspec_t rt = mp->m_binary ? rtp->t_tspec : NO_TSPEC;
   3453 
   3454 	if (Tflag && !typeok_scalar_strict_bool(op, mp, arg, ln, rn))
   3455 		return false;
   3456 	if (!typeok_scalar(op, mp, ltp, lt, rtp, rt))
   3457 		return false;
   3458 
   3459 	if (!typeok_op(op, call, arg, ln, ltp, lt, rn, rtp, rt))
   3460 		return false;
   3461 
   3462 	typeok_enum(op, mp, arg, ln, ltp, rn, rtp);
   3463 	return true;
   3464 }
   3465 
   3466 /* In traditional C, keep unsigned and promote FLOAT to DOUBLE. */
   3467 static tspec_t
   3468 promote_trad(tspec_t t)
   3469 {
   3470 
   3471 	if (t == UCHAR || t == USHORT)
   3472 		return UINT;
   3473 	if (t == CHAR || t == SCHAR || t == SHORT)
   3474 		return INT;
   3475 	if (t == FLOAT)
   3476 		return DOUBLE;
   3477 	if (t == ENUM)
   3478 		return INT;
   3479 	return t;
   3480 }
   3481 
   3482 /*
   3483  * C99 6.3.1.1p2 requires for types with lower rank than int that "If an int
   3484  * can represent all the values of the original type, the value is converted
   3485  * to an int; otherwise it is converted to an unsigned int", and that "All
   3486  * other types are unchanged by the integer promotions".
   3487  */
   3488 static tspec_t
   3489 promote_c90(const tnode_t *tn, tspec_t t, bool farg)
   3490 {
   3491 	if (tn->tn_type->t_bitfield) {
   3492 		unsigned int width = tn->tn_type->t_bit_field_width;
   3493 		unsigned int int_width = size_in_bits(INT);
   3494 		// XXX: What about _Bool bit-fields, since C99?
   3495 		if (width < int_width)
   3496 			return INT;
   3497 		if (width == int_width)
   3498 			return is_uinteger(t) ? UINT : INT;
   3499 		return t;
   3500 	}
   3501 
   3502 	if (t == CHAR || t == SCHAR)
   3503 		return INT;
   3504 	if (t == UCHAR)
   3505 		return size_in_bits(CHAR) < size_in_bits(INT) ? INT : UINT;
   3506 	if (t == SHORT)
   3507 		return INT;
   3508 	if (t == USHORT)
   3509 		return size_in_bits(SHORT) < size_in_bits(INT) ? INT : UINT;
   3510 	if (t == ENUM)
   3511 		return INT;
   3512 	if (farg && t == FLOAT)
   3513 		return DOUBLE;
   3514 	return t;
   3515 }
   3516 
   3517 /*
   3518  * Performs the "integer promotions" (C99 6.3.1.1p2), which convert small
   3519  * integer types to either int or unsigned int.
   3520  *
   3521  * If allow_c90 is unset or the operand is a function argument with no type
   3522  * information (no prototype or variable # of args), converts float to double.
   3523  */
   3524 tnode_t *
   3525 promote(op_t op, bool farg, tnode_t *tn)
   3526 {
   3527 
   3528 	const type_t *otp = tn->tn_type;
   3529 	tspec_t ot = otp->t_tspec;
   3530 	if (!is_arithmetic(ot))
   3531 		return tn;
   3532 
   3533 	tspec_t nt = allow_c90 ? promote_c90(tn, ot, farg) : promote_trad(ot);
   3534 	if (nt == ot)
   3535 		return tn;
   3536 
   3537 	type_t *ntp = expr_dup_type(gettyp(nt));
   3538 	ntp->t_tspec = nt;
   3539 	ntp->t_is_enum = otp->t_is_enum;
   3540 	if (ntp->t_is_enum)
   3541 		ntp->u.enumer = otp->u.enumer;
   3542 	return convert(op, 0, ntp, tn);
   3543 }
   3544 
   3545 static void
   3546 check_lossy_floating_to_integer_conversion(
   3547     op_t op, int arg, const type_t *tp, const tnode_t *tn)
   3548 {
   3549 	long double x = tn->u.value.u.floating;
   3550 	integer_constraints ic = ic_any(tp);
   3551 	if (is_uinteger(tp->t_tspec)
   3552 	    ? x >= ic.umin && x <= ic.umax && x == (uint64_t)x
   3553 	    : x >= ic.smin && x <= ic.smax && x == (int64_t)x)
   3554 		return;
   3555 	if (op == FARG)
   3556 		/* lossy conversion of %Lg to '%s', arg #%d */
   3557 		warning(380, x, type_name(tp), arg);
   3558 	else
   3559 		/* lossy conversion of %Lg to '%s' */
   3560 		warning(381, x, type_name(tp));
   3561 }
   3562 
   3563 static void
   3564 convert_integer_from_floating(
   3565     op_t op, int arg, const type_t *tp, const tnode_t *tn)
   3566 {
   3567 
   3568 	if (op == CVT)
   3569 		/* cast from floating point '%s' to integer '%s' */
   3570 		query_message(2, type_name(tn->tn_type), type_name(tp));
   3571 	else
   3572 		/* implicit conversion from floating point '%s' to ... */
   3573 		query_message(1, type_name(tn->tn_type), type_name(tp));
   3574 	if (tn->tn_op == CON)
   3575 		check_lossy_floating_to_integer_conversion(op, arg, tp, tn);
   3576 }
   3577 
   3578 static bool
   3579 should_warn_about_prototype_conversion(tspec_t nt,
   3580 				       tspec_t ot, const tnode_t *ptn)
   3581 {
   3582 
   3583 	if (nt == ot)
   3584 		return false;
   3585 
   3586 	if (nt == ENUM && ot == INT)
   3587 		return false;
   3588 
   3589 	if (is_floating(nt) != is_floating(ot) ||
   3590 	    portable_rank_cmp(nt, ot) != 0) {
   3591 		/* representation and/or width change */
   3592 		if (!is_integer(ot))
   3593 			return true;
   3594 		/*
   3595 		 * XXX: Investigate whether this rule makes sense; see
   3596 		 * tests/usr.bin/xlint/lint1/platform_long.c.
   3597 		 */
   3598 		return portable_rank_cmp(ot, INT) > 0;
   3599 	}
   3600 
   3601 	if (!hflag)
   3602 		return false;
   3603 
   3604 	/*
   3605 	 * If the types differ only in sign and the argument has the same
   3606 	 * representation in both types, print no warning.
   3607 	 */
   3608 	if (ptn->tn_op == CON && is_integer(nt) &&
   3609 	    signed_type(nt) == signed_type(ot) &&
   3610 	    !msb(ptn->u.value.u.integer, ot))
   3611 		return false;
   3612 
   3613 	return true;
   3614 }
   3615 
   3616 /*
   3617  * Warn if a prototype causes a type conversion that is different from what
   3618  * would happen to the same argument in the absence of a prototype.  This
   3619  * check is intended for code that needs to stay compatible with pre-C90 C.
   3620  *
   3621  * Errors/warnings about invalid type combinations are already printed
   3622  * in check_assign_types_compatible().
   3623  */
   3624 static void
   3625 check_prototype_conversion(int arg, tspec_t nt, tspec_t ot, type_t *tp,
   3626 			   tnode_t *tn)
   3627 {
   3628 
   3629 	if (!is_arithmetic(nt) || !is_arithmetic(ot))
   3630 		return;
   3631 
   3632 	/*
   3633 	 * If the type of the formal parameter is char/short, a warning would
   3634 	 * be useless, because functions declared the old style can't expect
   3635 	 * char/short arguments.
   3636 	 */
   3637 	if (nt == CHAR || nt == SCHAR || nt == UCHAR ||
   3638 	    nt == SHORT || nt == USHORT)
   3639 		return;
   3640 
   3641 	tnode_t *ptn = promote(NOOP, true, tn);
   3642 	ot = ptn->tn_type->t_tspec;
   3643 
   3644 	if (should_warn_about_prototype_conversion(nt, ot, ptn)) {
   3645 		/* argument %d is converted from '%s' to '%s' ... */
   3646 		warning(259, arg, expr_type_name(tn), type_name(tp));
   3647 	}
   3648 }
   3649 
   3650 /*
   3651  * When converting a large integer type to a small integer type, in some
   3652  * cases the value of the actual expression is further restricted than the
   3653  * type bounds, such as in (expr & 0xFF) or (expr % 100) or (expr >> 24).
   3654  */
   3655 static bool
   3656 can_represent(const type_t *tp, const tnode_t *tn)
   3657 {
   3658 	uint64_t nmask = value_bits(width_in_bits(tp));
   3659 	if (!is_uinteger(tp->t_tspec))
   3660 		nmask >>= 1;
   3661 
   3662 	integer_constraints c = ic_expr(tn);
   3663 	if ((~c.bclr & ~nmask) == 0)
   3664 		return true;
   3665 
   3666 	integer_constraints tpc = ic_any(tp);
   3667 	if (is_uinteger(tp->t_tspec)
   3668 	    ? tpc.umin <= c.umin && tpc.umax >= c.umax
   3669 	    : tpc.smin <= c.smin && tpc.smax >= c.smax)
   3670 		return true;
   3671 
   3672 	debug_enter();
   3673 	debug_step("type '%s' cannot represent:", type_name(tp));
   3674 	debug_node(tn);
   3675 	debug_leave();
   3676 	return false;
   3677 }
   3678 
   3679 static bool
   3680 should_warn_about_integer_conversion(const type_t *ntp, tspec_t nt,
   3681 				     const tnode_t *otn, tspec_t ot)
   3682 {
   3683 
   3684 	// XXX: The portable_rank_cmp aims at portable mode, independent of the
   3685 	// current platform, while can_represent acts on the actual type sizes
   3686 	// from the current platform.  This mix is inconsistent, but anything
   3687 	// else would make the exact conditions too complicated to grasp.
   3688 	if (aflag > 0 && portable_rank_cmp(nt, ot) < 0) {
   3689 		if (ot == LONG || ot == ULONG
   3690 		    || ot == LLONG || ot == ULLONG
   3691 #ifdef INT128_SIZE
   3692 		    || ot == INT128 || ot == UINT128
   3693 #endif
   3694 		    || aflag > 1)
   3695 			return !can_represent(ntp, otn);
   3696 	}
   3697 	return false;
   3698 }
   3699 
   3700 static void
   3701 convert_integer_from_integer(op_t op, int arg, tspec_t nt, tspec_t ot,
   3702 			     type_t *tp, tnode_t *tn)
   3703 {
   3704 
   3705 	if (tn->tn_op == CON)
   3706 		return;
   3707 
   3708 	if (op == CVT)
   3709 		return;
   3710 
   3711 	if (Pflag && pflag && aflag > 0 &&
   3712 	    portable_rank_cmp(nt, ot) > 0 &&
   3713 	    is_uinteger(nt) != is_uinteger(ot)) {
   3714 		if (op == FARG)
   3715 			/* conversion to '%s' may sign-extend ... */
   3716 			warning(297, type_name(tp), arg);
   3717 		else
   3718 			/* conversion to '%s' may sign-extend ... */
   3719 			warning(131, type_name(tp));
   3720 	}
   3721 
   3722 	if (Pflag && portable_rank_cmp(nt, ot) > 0 &&
   3723 	    (tn->tn_op == PLUS || tn->tn_op == MINUS || tn->tn_op == MULT ||
   3724 	     tn->tn_op == SHL)) {
   3725 		/* suggest cast from '%s' to '%s' on op '%s' to ... */
   3726 		warning(324, type_name(gettyp(ot)), type_name(tp),
   3727 		    op_name(tn->tn_op));
   3728 	}
   3729 
   3730 	if (should_warn_about_integer_conversion(tp, nt, tn, ot)) {
   3731 		if (op == FARG)
   3732 			/* conversion from '%s' to '%s' may lose ... */
   3733 			warning(298, expr_type_name(tn), type_name(tp), arg);
   3734 		else
   3735 			/* conversion from '%s' to '%s' may lose accuracy */
   3736 			warning(132, expr_type_name(tn), type_name(tp));
   3737 	}
   3738 
   3739 	if (is_uinteger(nt) != is_uinteger(ot))
   3740 		/* implicit conversion changes sign from '%s' to '%s' */
   3741 		query_message(3, expr_type_name(tn), type_name(tp));
   3742 }
   3743 
   3744 static void
   3745 convert_integer_from_pointer(op_t op, tspec_t nt, type_t *tp, tnode_t *tn)
   3746 {
   3747 
   3748 	if (tn->tn_op == CON)
   3749 		return;
   3750 	if (op != CVT)
   3751 		return;		/* We already got an error. */
   3752 	if (portable_rank_cmp(nt, PTR) >= 0)
   3753 		return;
   3754 
   3755 	if (pflag && size_in_bits(nt) >= size_in_bits(PTR)) {
   3756 		/* conversion of pointer to '%s' may lose bits */
   3757 		warning(134, type_name(tp));
   3758 	} else {
   3759 		/* conversion of pointer to '%s' loses bits */
   3760 		warning(133, type_name(tp));
   3761 	}
   3762 }
   3763 
   3764 static bool
   3765 struct_starts_with(const type_t *struct_tp, const type_t *member_tp)
   3766 {
   3767 
   3768 	return struct_tp->u.sou->sou_first_member != NULL &&
   3769 	    types_compatible(struct_tp->u.sou->sou_first_member->s_type,
   3770 		member_tp, true, false, NULL);
   3771 }
   3772 
   3773 static bool
   3774 is_byte_array(const type_t *tp)
   3775 {
   3776 
   3777 	return tp->t_tspec == ARRAY &&
   3778 	    (tp->t_subt->t_tspec == CHAR || tp->t_subt->t_tspec == UCHAR);
   3779 }
   3780 
   3781 static bool
   3782 union_contains(const type_t *utp, const type_t *mtp)
   3783 {
   3784 	for (const sym_t *mem = utp->u.sou->sou_first_member;
   3785 	    mem != NULL; mem = mem->s_next) {
   3786 		if (types_compatible(mem->s_type, mtp, true, false, NULL))
   3787 			return true;
   3788 	}
   3789 	return false;
   3790 }
   3791 
   3792 static bool
   3793 should_warn_about_pointer_cast(const type_t *nstp, tspec_t nst,
   3794 			       const type_t *ostp, tspec_t ost)
   3795 {
   3796 
   3797 	while (nst == ARRAY)
   3798 		nstp = nstp->t_subt, nst = nstp->t_tspec;
   3799 	while (ost == ARRAY)
   3800 		ostp = ostp->t_subt, ost = ostp->t_tspec;
   3801 
   3802 	if (nst == STRUCT && ost == STRUCT &&
   3803 	    (struct_starts_with(nstp, ostp) ||
   3804 	     struct_starts_with(ostp, nstp)))
   3805 		return false;
   3806 
   3807 	if (is_incomplete(nstp) || is_incomplete(ostp))
   3808 		return false;
   3809 
   3810 	if (nst == CHAR || nst == UCHAR)
   3811 		return false;	/* for the sake of traditional C code */
   3812 	if (ost == CHAR || ost == UCHAR)
   3813 		return false;	/* for the sake of traditional C code */
   3814 
   3815 	/* Allow cast between pointers to sockaddr variants. */
   3816 	if (nst == STRUCT && ost == STRUCT) {
   3817 		const sym_t *nmem = nstp->u.sou->sou_first_member;
   3818 		const sym_t *omem = ostp->u.sou->sou_first_member;
   3819 		while (nmem != NULL && omem != NULL &&
   3820 		    types_compatible(nmem->s_type, omem->s_type,
   3821 			true, false, NULL))
   3822 			nmem = nmem->s_next, omem = omem->s_next;
   3823 		if (nmem != NULL && is_byte_array(nmem->s_type))
   3824 			return false;
   3825 		if (omem != NULL && is_byte_array(omem->s_type))
   3826 			return false;
   3827 		if (nmem == NULL && omem == NULL)
   3828 			return false;
   3829 	}
   3830 
   3831 	if (nst == UNION || ost == UNION) {
   3832 		const type_t *union_tp = nst == UNION ? nstp : ostp;
   3833 		const type_t *other_tp = nst == UNION ? ostp : nstp;
   3834 		if (union_contains(union_tp, other_tp))
   3835 			return false;
   3836 	}
   3837 
   3838 	if (is_struct_or_union(nst) && is_struct_or_union(ost))
   3839 		return nstp->u.sou != ostp->u.sou;
   3840 
   3841 	enum rank_kind rk1 = type_properties(nst)->tt_rank_kind;
   3842 	enum rank_kind rk2 = type_properties(ost)->tt_rank_kind;
   3843 	if (rk1 != rk2 || rk1 == RK_NONE)
   3844 		return true;
   3845 
   3846 	return portable_rank_cmp(nst, ost) != 0;
   3847 }
   3848 
   3849 static void
   3850 convert_pointer_from_pointer(type_t *ntp, tnode_t *tn)
   3851 {
   3852 	const type_t *nstp = ntp->t_subt;
   3853 	const type_t *otp = tn->tn_type;
   3854 	const type_t *ostp = otp->t_subt;
   3855 	tspec_t nst = nstp->t_tspec;
   3856 	tspec_t ost = ostp->t_tspec;
   3857 
   3858 	if (nst == VOID || ost == VOID) {
   3859 		/* TODO: C99 behaves like C90 here. */
   3860 		if (!allow_trad && !allow_c99 && (nst == FUNC || ost == FUNC)) {
   3861 			const char *nts, *ots;
   3862 			/* null pointers are already handled in convert() */
   3863 			*(nst == FUNC ? &nts : &ots) = "function pointer";
   3864 			*(nst == VOID ? &nts : &ots) = "'void *'";
   3865 			/* conversion of %s to %s requires a cast */
   3866 			warning(303, ots, nts);
   3867 		}
   3868 		return;
   3869 	}
   3870 	if (nst == FUNC && ost == FUNC)
   3871 		return;
   3872 	if (nst == FUNC || ost == FUNC) {
   3873 		/* converting '%s' to '%s' is questionable */
   3874 		warning(229, type_name(otp), type_name(ntp));
   3875 		return;
   3876 	}
   3877 
   3878 	if (hflag && alignment(nstp) > alignment(ostp) &&
   3879 	    !is_incomplete(ostp) && alignment(ostp) > 1 &&
   3880 	    !(nst == UNION && union_contains(nstp, ostp))) {
   3881 		/* converting '%s' to '%s' increases alignment ... */
   3882 		warning(135, type_name(otp), type_name(ntp),
   3883 		    alignment(ostp), alignment(nstp));
   3884 	}
   3885 
   3886 	if (cflag && should_warn_about_pointer_cast(nstp, nst, ostp, ost)) {
   3887 		/* pointer cast from '%s' to unrelated '%s' */
   3888 		warning(247, type_name(ostp), type_name(nstp));
   3889 	}
   3890 }
   3891 
   3892 /*
   3893  * Insert a conversion operator, which converts the type of the node
   3894  * to another given type.
   3895  *
   3896  * Possible values for 'op':
   3897  *	CVT	a cast-expression
   3898  *	binary	integer promotion for one of the operands, or a usual
   3899  *		arithmetic conversion
   3900  *	binary	plain or compound assignments to bit-fields
   3901  *	FARG	'arg' is the number of the parameter (used for warnings)
   3902  *	NOOP	several other implicit conversions
   3903  *	...
   3904  */
   3905 tnode_t *
   3906 convert(op_t op, int arg, type_t *tp, tnode_t *tn)
   3907 {
   3908 	tspec_t nt = tp->t_tspec;
   3909 	tspec_t ot = tn->tn_type->t_tspec;
   3910 
   3911 	if (allow_trad && allow_c90 && op == FARG)
   3912 		check_prototype_conversion(arg, nt, ot, tp, tn);
   3913 
   3914 	if (nt == BOOL) {
   3915 		/* No further checks. */
   3916 
   3917 	} else if (is_integer(nt)) {
   3918 		if (ot == BOOL) {
   3919 			/* No further checks. */
   3920 		} else if (is_integer(ot))
   3921 			convert_integer_from_integer(op, arg, nt, ot, tp, tn);
   3922 		else if (is_floating(ot))
   3923 			convert_integer_from_floating(op, arg, tp, tn);
   3924 		else if (ot == PTR)
   3925 			convert_integer_from_pointer(op, nt, tp, tn);
   3926 
   3927 	} else if (is_floating(nt)) {
   3928 		if (is_integer(ot) && op != CVT) {
   3929 			/* implicit conversion from integer '%s' to ... */
   3930 			query_message(19,
   3931 			    type_name(tn->tn_type), type_name(tp));
   3932 		}
   3933 
   3934 	} else if (nt == PTR) {
   3935 		if (is_null_pointer(tn)) {
   3936 			/* a null pointer may be assigned to any pointer. */
   3937 		} else if (ot == PTR && op == CVT)
   3938 			convert_pointer_from_pointer(tp, tn);
   3939 	}
   3940 
   3941 	tnode_t *ntn = expr_alloc_tnode();
   3942 	ntn->tn_op = CVT;
   3943 	ntn->tn_type = tp;
   3944 	ntn->tn_cast = op == CVT;
   3945 	ntn->tn_sys |= tn->tn_sys;
   3946 	ntn->u.ops.right = NULL;
   3947 	if (tn->tn_op != CON || nt == VOID) {
   3948 		ntn->u.ops.left = tn;
   3949 	} else {
   3950 		ntn->tn_op = CON;
   3951 		convert_constant(op, arg, ntn->tn_type, &ntn->u.value,
   3952 		    &tn->u.value);
   3953 	}
   3954 
   3955 	return ntn;
   3956 }
   3957 
   3958 static void
   3959 convert_constant_from_floating(op_t op, int arg, const type_t *ntp,
   3960 			       tspec_t nt, val_t *nv, val_t *ov)
   3961 {
   3962 	long double max = 0.0, min = 0.0;
   3963 
   3964 	switch (nt) {
   3965 	case CHAR:
   3966 		max = TARG_CHAR_MAX;	min = TARG_CHAR_MIN;	break;
   3967 	case UCHAR:
   3968 		max = TARG_UCHAR_MAX;	min = 0;		break;
   3969 	case SCHAR:
   3970 		max = TARG_SCHAR_MAX;	min = TARG_SCHAR_MIN;	break;
   3971 	case SHORT:
   3972 		max = TARG_SHRT_MAX;	min = TARG_SHRT_MIN;	break;
   3973 	case USHORT:
   3974 		max = TARG_USHRT_MAX;	min = 0;		break;
   3975 	case ENUM:
   3976 	case INT:
   3977 		max = TARG_INT_MAX;	min = TARG_INT_MIN;	break;
   3978 	case UINT:
   3979 		max = TARG_UINT_MAX;	min = 0;		break;
   3980 	case LONG:
   3981 		max = TARG_LONG_MAX;	min = TARG_LONG_MIN;	break;
   3982 	case ULONG:
   3983 		max = TARG_ULONG_MAX;	min = 0;		break;
   3984 	case LLONG:
   3985 		max = LLONG_MAX;	min = LLONG_MIN;	break;
   3986 	case ULLONG:
   3987 		max = ULLONG_MAX;	min = 0;		break;
   3988 	case FLOAT:
   3989 	case FCOMPLEX:
   3990 		max = FLT_MAX;		min = -FLT_MAX;		break;
   3991 	case DOUBLE:
   3992 	case DCOMPLEX:
   3993 		max = DBL_MAX;		min = -DBL_MAX;		break;
   3994 	case LDOUBLE:
   3995 	case LCOMPLEX:
   3996 		/* LINTED 248; see floating_error_value. */
   3997 		max = LDBL_MAX;		min = -max;		break;
   3998 	default:
   3999 		lint_assert(false);
   4000 	}
   4001 	if (ov->u.floating > max || ov->u.floating < min) {
   4002 		lint_assert(nt != LDOUBLE);
   4003 		const char *ot_name = type_name(gettyp(ov->v_tspec));
   4004 		const char *nt_name = type_name(ntp);
   4005 		if (is_integer(nt))
   4006 			goto after_warning;
   4007 		if (op == FARG)
   4008 			/* conversion of '%s' to '%s' is out of range, ... */
   4009 			warning(295, ot_name, nt_name, arg);
   4010 		else
   4011 			/* conversion of '%s' to '%s' is out of range */
   4012 			warning(119, ot_name, nt_name);
   4013 	after_warning:
   4014 		ov->u.floating = ov->u.floating > 0 ? max : min;
   4015 	}
   4016 
   4017 	if (nt == FLOAT || nt == FCOMPLEX)
   4018 		nv->u.floating = (float)ov->u.floating;
   4019 	else if (nt == DOUBLE || nt == DCOMPLEX)
   4020 		nv->u.floating = (double)ov->u.floating;
   4021 	else if (nt == LDOUBLE || nt == LCOMPLEX)
   4022 		nv->u.floating = ov->u.floating;
   4023 	else
   4024 		nv->u.integer = (int64_t)ov->u.floating;
   4025 }
   4026 
   4027 static bool
   4028 convert_constant_to_floating(tspec_t nt, val_t *nv,
   4029 			     tspec_t ot, const val_t *v)
   4030 {
   4031 	if (nt == FLOAT) {
   4032 		nv->u.floating = (ot == PTR || is_uinteger(ot)) ?
   4033 		    (float)(uint64_t)v->u.integer : (float)v->u.integer;
   4034 	} else if (nt == DOUBLE) {
   4035 		nv->u.floating = (ot == PTR || is_uinteger(ot)) ?
   4036 		    (double)(uint64_t)v->u.integer : (double)v->u.integer;
   4037 	} else if (nt == LDOUBLE) {
   4038 		nv->u.floating = (ot == PTR || is_uinteger(ot))
   4039 		    ? (long double)(uint64_t)v->u.integer
   4040 		    : (long double)v->u.integer;
   4041 	} else
   4042 		return false;
   4043 	return true;
   4044 }
   4045 
   4046 static void
   4047 warn_constant_truncated(op_t op, const val_t *v)
   4048 {
   4049 	char buf[256];
   4050 	bool is_unsigned = is_uinteger(v->v_tspec);
   4051 	int64_t val = v->u.integer;
   4052 	unsigned long long abs_val = is_unsigned || val >= 0
   4053 	    ? (unsigned long long)val
   4054 	    : -(unsigned long long)val;
   4055 	const char *sign = is_unsigned || val >= 0 ? "" : "-";
   4056 	snprintf(buf, sizeof(buf), "%s%#llx", sign, abs_val);
   4057 	/* constant %s truncated by conversion, op '%s' */
   4058 	warning(306, buf, op_name(op));
   4059 }
   4060 
   4061 static void
   4062 convert_constant_check_range_bitor(size_t nsz, size_t osz, const val_t *v,
   4063 				   uint64_t xmask, op_t op)
   4064 {
   4065 	if (nsz < osz && (v->u.integer & xmask) != 0)
   4066 		warn_constant_truncated(op, v);
   4067 }
   4068 
   4069 static void
   4070 convert_constant_check_range_bitand(size_t nsz, size_t osz,
   4071 				    uint64_t xmask, const val_t *nv,
   4072 				    tspec_t ot, const val_t *v,
   4073 				    const type_t *tp, op_t op)
   4074 {
   4075 	if (nsz > osz &&
   4076 	    (nv->u.integer & bit((unsigned int)(osz - 1))) != 0 &&
   4077 	    (nv->u.integer & xmask) != xmask) {
   4078 		/* '%s' converts '%s' with its most significant bit being set to '%s' */
   4079 		warning(309,
   4080 		    op_name(op), type_name(gettyp(ot)), type_name(tp));
   4081 	} else if (nsz < osz &&
   4082 	    (v->u.integer & xmask) != xmask &&
   4083 	    (v->u.integer & xmask) != 0)
   4084 		warn_constant_truncated(op, v);
   4085 }
   4086 
   4087 static void
   4088 convert_constant_check_range_signed(op_t op, int arg,
   4089 				    const type_t *ntp, int64_t ov)
   4090 {
   4091 	if (op == ASSIGN)
   4092 		/* assignment of negative constant %lld to unsigned ... */
   4093 		warning(164, (long long)ov, type_name(ntp));
   4094 	else if (op == INIT)
   4095 		/* initialization of unsigned type '%s' with negative ... */
   4096 		warning(221, type_name(ntp), (long long)ov);
   4097 	else if (op == FARG)
   4098 		/* conversion of negative constant %lld to unsigned ... */
   4099 		warning(296, (long long)ov, type_name(ntp), arg);
   4100 	else if (modtab[op].m_comparison) {
   4101 		/* handled by check_integer_comparison() */
   4102 	} else
   4103 		/* conversion of negative constant %lld to unsigned ... */
   4104 		warning(222, (long long)ov, type_name(ntp));
   4105 }
   4106 
   4107 /*
   4108  * Loss of significant bit(s). All truncated bits of unsigned types or all
   4109  * truncated bits plus the msb of the target for signed types are considered
   4110  * to be significant bits. Loss of significant bits means that at least one
   4111  * of the bits was set in an unsigned type or that at least one but not all
   4112  * of the bits was set in a signed type. Loss of significant bits means that
   4113  * it is not possible, also not with necessary casts, to convert back to the
   4114  * original type. An example for a necessary cast is:
   4115  *	char c;	int	i; c = 128;
   4116  *	i = c;			** yields -128 **
   4117  *	i = (unsigned char)c;	** yields 128 **
   4118  */
   4119 static void
   4120 warn_constant_check_range_truncated(op_t op, int arg, const type_t *tp,
   4121 				    tspec_t ot)
   4122 {
   4123 	if (op == ASSIGN && tp->t_bitfield)
   4124 		/* precision lost in bit-field assignment */
   4125 		warning(166);
   4126 	else if (op == ASSIGN)
   4127 		/* constant truncated by assignment */
   4128 		warning(165);
   4129 	else if (op == INIT && tp->t_bitfield)
   4130 		/* bit-field initializer does not fit */
   4131 		warning(180);
   4132 	else if (op == INIT)
   4133 		/* initializer does not fit */
   4134 		warning(178);
   4135 	else if (op == CASE)
   4136 		/* case label is converted from '%s' to '%s' */
   4137 		warning(196, tspec_name(ot), type_name(tp));
   4138 	else if (op == FARG)
   4139 		/* conversion of '%s' to '%s' is out of range, arg #%d */
   4140 		warning(295, type_name(gettyp(ot)), type_name(tp), arg);
   4141 	else
   4142 		/* conversion of '%s' to '%s' is out of range */
   4143 		warning(119, type_name(gettyp(ot)), type_name(tp));
   4144 }
   4145 
   4146 static void
   4147 warn_constant_check_range_loss(op_t op, int arg, const type_t *tp,
   4148 				  tspec_t ot)
   4149 {
   4150 	if (op == ASSIGN && tp->t_bitfield)
   4151 		/* precision lost in bit-field assignment */
   4152 		warning(166);
   4153 	else if (op == INIT && tp->t_bitfield)
   4154 		/* bit-field initializer out of range */
   4155 		warning(11);
   4156 	else if (op == CASE)
   4157 		/* case label is converted from '%s' to '%s' */
   4158 		warning(196, tspec_name(ot), type_name(tp));
   4159 	else if (op == FARG)
   4160 		/* conversion of '%s' to '%s' is out of range, arg #%d */
   4161 		warning(295, type_name(gettyp(ot)), type_name(tp), arg);
   4162 	else
   4163 		/* conversion of '%s' to '%s' is out of range */
   4164 		warning(119, type_name(gettyp(ot)), type_name(tp));
   4165 }
   4166 
   4167 static void
   4168 convert_constant_check_range(tspec_t ot, const type_t *tp, tspec_t nt,
   4169 			     op_t op, int arg, const val_t *v, val_t *nv)
   4170 {
   4171 	unsigned int obitsz, nbitsz;
   4172 	uint64_t xmask, xmsk1;
   4173 
   4174 	obitsz = size_in_bits(ot);
   4175 	nbitsz = tp->t_bitfield ? tp->t_bit_field_width : size_in_bits(nt);
   4176 	xmask = value_bits(nbitsz) ^ value_bits(obitsz);
   4177 	xmsk1 = value_bits(nbitsz) ^ value_bits(obitsz - 1);
   4178 	if (op == ORASS || op == BITOR || op == BITXOR) {
   4179 		convert_constant_check_range_bitor(
   4180 		    nbitsz, obitsz, v, xmask, op);
   4181 	} else if (op == ANDASS || op == BITAND) {
   4182 		convert_constant_check_range_bitand(
   4183 		    nbitsz, obitsz, xmask, nv, ot, v, tp, op);
   4184 	} else if (nt != PTR && is_uinteger(nt) &&
   4185 	    ot != PTR && !is_uinteger(ot) && v->u.integer < 0)
   4186 		convert_constant_check_range_signed(op, arg, tp, v->u.integer);
   4187 	else if (nv->u.integer != v->u.integer && nbitsz <= obitsz &&
   4188 	    (v->u.integer & xmask) != 0 &&
   4189 	    (is_uinteger(ot) || (v->u.integer & xmsk1) != xmsk1))
   4190 		warn_constant_check_range_truncated(op, arg, tp, ot);
   4191 	else if (nv->u.integer != v->u.integer)
   4192 		warn_constant_check_range_loss(op, arg, tp, ot);
   4193 }
   4194 
   4195 /* Converts a typed constant to a constant of another type. */
   4196 void
   4197 convert_constant(op_t op, int arg, const type_t *ntp, val_t *nv, val_t *ov)
   4198 {
   4199 	/*
   4200 	 * TODO: make 'ov' const; the name of this function does not suggest
   4201 	 *  that it modifies 'ov'.
   4202 	 */
   4203 	tspec_t ot = ov->v_tspec;
   4204 	tspec_t nt = nv->v_tspec = ntp->t_tspec;
   4205 	bool range_check = false;
   4206 
   4207 	if (nt == BOOL) {	/* C99 6.3.1.2 */
   4208 		nv->v_unsigned_since_c90 = false;
   4209 		nv->u.integer = is_nonzero_val(ov) ? 1 : 0;
   4210 		return;
   4211 	}
   4212 
   4213 	if (ot == FLOAT || ot == DOUBLE || ot == LDOUBLE)
   4214 		convert_constant_from_floating(op, arg, ntp, nt, nv, ov);
   4215 	else if (!convert_constant_to_floating(nt, nv, ot, ov)) {
   4216 		range_check = true;	/* Check for lost precision. */
   4217 		nv->u.integer = ov->u.integer;
   4218 	}
   4219 
   4220 	if (allow_trad && allow_c90 && ov->v_unsigned_since_c90 &&
   4221 	    (is_floating(nt) || (
   4222 		(is_integer(nt) && !is_uinteger(nt) &&
   4223 		    portable_rank_cmp(nt, ot) > 0)))) {
   4224 		/* C90 treats constant as unsigned */
   4225 		warning(157);
   4226 		ov->v_unsigned_since_c90 = false;
   4227 	}
   4228 
   4229 	if (is_integer(nt)) {
   4230 		unsigned int size = ntp->t_bitfield
   4231 		    ? ntp->t_bit_field_width : size_in_bits(nt);
   4232 		nv->u.integer = convert_integer(nv->u.integer, nt, size);
   4233 	}
   4234 
   4235 	if (range_check && op != CVT)
   4236 		convert_constant_check_range(ot, ntp, nt, op, arg, ov, nv);
   4237 }
   4238 
   4239 tnode_t *
   4240 build_sizeof(const type_t *tp)
   4241 {
   4242 	unsigned int size_in_bytes = type_size_in_bits(tp) / CHAR_SIZE;
   4243 	tnode_t *tn = build_integer_constant(SIZEOF_TSPEC, size_in_bytes);
   4244 	tn->tn_system_dependent = true;
   4245 	debug_step("build_sizeof '%s' = %u", type_name(tp), size_in_bytes);
   4246 	return tn;
   4247 }
   4248 
   4249 tnode_t *
   4250 build_offsetof(const type_t *tp, designation dn)
   4251 {
   4252 	unsigned int offset_in_bits = 0;
   4253 
   4254 	if (!is_struct_or_union(tp->t_tspec)) {
   4255 		/* unacceptable operand of '%s' */
   4256 		error(111, "offsetof");
   4257 		goto proceed;
   4258 	}
   4259 	for (size_t i = 0; i < dn.dn_len; i++) {
   4260 		const designator *dr = dn.dn_items + i;
   4261 		if (dr->dr_kind == DK_SUBSCRIPT) {
   4262 			if (tp->t_tspec != ARRAY)
   4263 				goto proceed;	/* silent error */
   4264 			tp = tp->t_subt;
   4265 			offset_in_bits += (unsigned)dr->dr_subscript
   4266 			    * type_size_in_bits(tp);
   4267 		} else {
   4268 			if (!is_struct_or_union(tp->t_tspec))
   4269 				goto proceed;	/* silent error */
   4270 			const char *name = dr->dr_member->s_name;
   4271 			sym_t *mem = find_member(tp->u.sou, name);
   4272 			if (mem == NULL) {
   4273 				/* type '%s' does not have member '%s' */
   4274 				error(101, name, type_name(tp));
   4275 				goto proceed;
   4276 			}
   4277 			tp = mem->s_type;
   4278 			offset_in_bits += mem->u.s_member.sm_offset_in_bits;
   4279 		}
   4280 	}
   4281 	free(dn.dn_items);
   4282 
   4283 proceed:;
   4284 	unsigned int offset_in_bytes = offset_in_bits / CHAR_SIZE;
   4285 	tnode_t *tn = build_integer_constant(SIZEOF_TSPEC, offset_in_bytes);
   4286 	tn->tn_system_dependent = true;
   4287 	return tn;
   4288 }
   4289 
   4290 unsigned int
   4291 type_size_in_bits(const type_t *tp)
   4292 {
   4293 
   4294 	unsigned int elem = 1;
   4295 	bool flex = false;
   4296 	lint_assert(tp != NULL);
   4297 	while (tp->t_tspec == ARRAY) {
   4298 		flex = true;	/* allow c99 flex arrays [] [0] */
   4299 		elem *= tp->u.dimension;
   4300 		tp = tp->t_subt;
   4301 	}
   4302 	if (elem == 0 && !flex) {
   4303 		/* cannot take size/alignment of incomplete type */
   4304 		error(143);
   4305 		elem = 1;
   4306 	}
   4307 
   4308 	unsigned int elsz;
   4309 	switch (tp->t_tspec) {
   4310 	case VOID:
   4311 		/* cannot take size/alignment of void */
   4312 		error(146);
   4313 		elsz = 1;
   4314 		break;
   4315 	case FUNC:
   4316 		/* cannot take size/alignment of function type '%s' */
   4317 		error(144, type_name(tp));
   4318 		elsz = 1;
   4319 		break;
   4320 	case STRUCT:
   4321 	case UNION:
   4322 		if (is_incomplete(tp)) {
   4323 			/* cannot take size/alignment of incomplete type */
   4324 			error(143);
   4325 			elsz = 1;
   4326 		} else
   4327 			elsz = tp->u.sou->sou_size_in_bits;
   4328 		break;
   4329 	case ENUM:
   4330 		if (is_incomplete(tp)) {
   4331 			/* cannot take size/alignment of incomplete type */
   4332 			warning(143);
   4333 		}
   4334 		/* FALLTHROUGH */
   4335 	default:
   4336 		if (tp->t_bitfield)
   4337 			/* cannot take size/alignment of bit-field */
   4338 			error(145);
   4339 		elsz = size_in_bits(tp->t_tspec);
   4340 		lint_assert(elsz > 0);
   4341 		break;
   4342 	}
   4343 
   4344 	return elem * elsz;
   4345 }
   4346 
   4347 /* C11 6.5.3.4, GCC */
   4348 tnode_t *
   4349 build_alignof(const type_t *tp)
   4350 {
   4351 	if (tp->t_tspec == FUNC) {
   4352 		/* cannot take size/alignment of function type '%s' */
   4353 		error(144, type_name(tp));
   4354 		return NULL;
   4355 	}
   4356 	if (tp->t_tspec == VOID) {
   4357 		/* cannot take size/alignment of void */
   4358 		error(146);
   4359 		return NULL;
   4360 	}
   4361 	if (is_incomplete(tp)) {
   4362 		/* cannot take size/alignment of incomplete type */
   4363 		error(143);
   4364 		return NULL;
   4365 	}
   4366 	if (tp->t_bitfield) {
   4367 		/* cannot take size/alignment of bit-field */
   4368 		error(145);
   4369 		return NULL;
   4370 	}
   4371 	return build_integer_constant(SIZEOF_TSPEC, (int64_t)alignment(tp));
   4372 }
   4373 
   4374 static tnode_t *
   4375 cast_to_union(tnode_t *otn, bool sys, type_t *ntp)
   4376 {
   4377 
   4378 	if (!allow_gcc) {
   4379 		/* union cast is a GCC extension */
   4380 		error(328);
   4381 		return NULL;
   4382 	}
   4383 
   4384 	for (const sym_t *m = ntp->u.sou->sou_first_member;
   4385 	    m != NULL; m = m->s_next) {
   4386 		if (types_compatible(m->s_type, otn->tn_type,
   4387 		    false, false, NULL)) {
   4388 			tnode_t *ntn = build_op(CVT, sys, ntp, otn, NULL);
   4389 			ntn->tn_cast = true;
   4390 			return ntn;
   4391 		}
   4392 	}
   4393 
   4394 	/* type '%s' is not a member of '%s' */
   4395 	error(329, type_name(otn->tn_type), type_name(ntp));
   4396 	return NULL;
   4397 }
   4398 
   4399 // In GCC mode, allow 'nullptr + offset' as a constant expression.
   4400 static tnode_t *
   4401 null_pointer_offset(tnode_t *tn)
   4402 {
   4403 	uint64_t off = 0;
   4404 	const tnode_t *n = tn;
   4405 	while ((n->tn_op == PLUS || n->tn_op == MINUS)
   4406 	    && is_integer(n->u.ops.right->tn_type->t_tspec)) {
   4407 		off += (uint64_t)n->u.ops.right->u.value.u.integer;
   4408 		n = n->u.ops.left;
   4409 	}
   4410 	if (n->tn_type->t_tspec == PTR
   4411 	    && n->tn_op == ADDR
   4412 	    && n->u.ops.left->tn_op == INDIR
   4413 	    && n->u.ops.left->u.ops.left->tn_op == CON
   4414 	    && n->u.ops.left->u.ops.left->tn_type->t_tspec == PTR) {
   4415 		off += (uint64_t)n->u.ops.left->u.ops.left->u.value.u.integer;
   4416 		return build_integer_constant(SIZEOF_TSPEC, (int64_t)off);
   4417 	}
   4418 	return tn;
   4419 }
   4420 
   4421 tnode_t *
   4422 cast(tnode_t *tn, bool sys, type_t *tp)
   4423 {
   4424 
   4425 	if (tn == NULL)
   4426 		return NULL;
   4427 
   4428 	tn = cconv(tn);
   4429 
   4430 	lint_assert(tp != NULL);
   4431 	tspec_t nt = tp->t_tspec;
   4432 	tspec_t ot = tn->tn_type->t_tspec;
   4433 
   4434 	if (nt == VOID) {
   4435 		/*
   4436 		 * C90 6.3.4, C99 6.5.4p2 and C11 6.5.4p2 allow any type to be
   4437 		 * cast to void.  The only other allowed casts are from a
   4438 		 * scalar type to a scalar type.
   4439 		 */
   4440 	} else if (nt == UNION)
   4441 		return cast_to_union(tn, sys, tp);
   4442 	else if (nt == STRUCT || nt == ARRAY || nt == FUNC) {
   4443 		/* Casting to a struct is an undocumented GCC extension. */
   4444 		if (!(allow_gcc && nt == STRUCT))
   4445 			goto invalid_cast;
   4446 	} else if (is_struct_or_union(ot))
   4447 		goto invalid_cast;
   4448 	else if (ot == VOID) {
   4449 		/* improper cast of void expression */
   4450 		error(148);
   4451 		return NULL;
   4452 	} else if (is_integer(nt) && is_scalar(ot)) {
   4453 		tn = null_pointer_offset(tn);
   4454 	} else if (is_floating(nt) && is_arithmetic(ot)) {
   4455 		/* ok */
   4456 	} else if (nt == PTR && is_integer(ot)) {
   4457 		/* ok */
   4458 	} else if (nt == PTR && ot == PTR) {
   4459 		if (!tp->t_subt->t_const && tn->tn_type->t_subt->t_const) {
   4460 			if (hflag)
   4461 				/* cast discards 'const' from type '%s' */
   4462 				warning(275, type_name(tn->tn_type));
   4463 		}
   4464 	} else
   4465 		goto invalid_cast;
   4466 
   4467 	if (any_query_enabled
   4468 	    && types_compatible(tp, tn->tn_type, false, false, NULL))
   4469 		/* no-op cast from '%s' to '%s' */
   4470 		query_message(6, expr_type_name(tn), type_name(tp));
   4471 
   4472 	tn = convert(CVT, 0, tp, tn);
   4473 	tn->tn_cast = true;
   4474 	tn->tn_sys = sys;
   4475 
   4476 	return tn;
   4477 
   4478 invalid_cast:
   4479 	/* invalid cast from '%s' to '%s' */
   4480 	error(147, expr_type_name(tn), type_name(tp));
   4481 	return NULL;
   4482 }
   4483 
   4484 void
   4485 add_function_argument(function_call *call, tnode_t *arg)
   4486 {
   4487 	/*
   4488 	 * If there was a serious error in the expression for the argument,
   4489 	 * create a dummy argument so the positions of the remaining arguments
   4490 	 * will not change.
   4491 	 */
   4492 	if (arg == NULL)
   4493 		arg = build_integer_constant(INT, 0);
   4494 
   4495 	if (call->args_len >= call->args_cap) {
   4496 		call->args_cap += 8;
   4497 		tnode_t **new_args = expr_zero_alloc(
   4498 		    call->args_cap * sizeof(*call->args), "tnode*[]");
   4499 		if (call->args_len > 0)
   4500 			memcpy(new_args, call->args,
   4501 			    call->args_len * sizeof(*call->args));
   4502 		call->args = new_args;
   4503 	}
   4504 	call->args[call->args_len++] = arg;
   4505 }
   4506 
   4507 /*
   4508  * Compare the type of an argument with the corresponding type of a
   4509  * prototype parameter. If it is a valid combination, but both types
   4510  * are not the same, insert a conversion to convert the argument into
   4511  * the type of the parameter.
   4512  */
   4513 static tnode_t *
   4514 check_prototype_argument(const function_call *call, int arg,
   4515     type_t *tp, tnode_t *tn)
   4516 {
   4517 	tnode_t *ln = xcalloc(1, sizeof(*ln));
   4518 	ln->tn_type = expr_unqualified_type(tp);
   4519 	ln->tn_lvalue = true;
   4520 	if (typeok(FARG, call, arg, ln, tn)) {
   4521 		bool dowarn;
   4522 		if (!types_compatible(tp, tn->tn_type,
   4523 		    true, false, (dowarn = false, &dowarn)) || dowarn)
   4524 			tn = convert(FARG, arg, tp, tn);
   4525 	}
   4526 	free(ln);
   4527 	return tn;
   4528 }
   4529 
   4530 /*
   4531  * Check types of all function arguments and insert conversions,
   4532  * if necessary.
   4533  */
   4534 static void
   4535 check_function_arguments(const function_call *call)
   4536 {
   4537 	type_t *ftp = call->func->tn_type->t_subt;
   4538 
   4539 	/* get # of parameters in the prototype */
   4540 	int npar = 0;
   4541 	for (const sym_t *p = ftp->u.params; p != NULL; p = p->s_next)
   4542 		npar++;
   4543 
   4544 	int narg = (int)call->args_len;
   4545 
   4546 	const sym_t *param = ftp->u.params;
   4547 	if (ftp->t_proto && npar != narg && !(ftp->t_vararg && npar < narg)) {
   4548 		/* argument mismatch: %d %s passed, %d expected */
   4549 		error(150, narg, narg != 1 ? "arguments" : "argument", npar);
   4550 		param = NULL;
   4551 	}
   4552 
   4553 	for (int i = 0; i < narg; i++) {
   4554 		tnode_t *arg = call->args[i];
   4555 
   4556 		/* some things which are always not allowed */
   4557 		tspec_t at = arg->tn_type->t_tspec;
   4558 		if (at == VOID) {
   4559 			/* void expressions may not be arguments, arg #%d */
   4560 			error(151, i + 1);
   4561 			return;
   4562 		}
   4563 		if (is_struct_or_union(at) && is_incomplete(arg->tn_type)) {
   4564 			/* argument cannot have unknown size, arg #%d */
   4565 			error(152, i + 1);
   4566 			return;
   4567 		}
   4568 		if (is_integer(at) &&
   4569 		    arg->tn_type->t_is_enum &&
   4570 		    is_incomplete(arg->tn_type)) {
   4571 			/* argument cannot have unknown size, arg #%d */
   4572 			warning(152, i + 1);
   4573 		}
   4574 
   4575 		arg = cconv(arg);
   4576 		call->args[i] = arg;
   4577 
   4578 		arg = param != NULL
   4579 		    ? check_prototype_argument(call, i + 1, param->s_type, arg)
   4580 		    : promote(NOOP, true, arg);
   4581 		call->args[i] = arg;
   4582 
   4583 		if (param != NULL)
   4584 			param = param->s_next;
   4585 	}
   4586 }
   4587 
   4588 static bool
   4589 is_gcc_generic_atomic(const char *name)
   4590 {
   4591 	// https://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html
   4592 	return strcmp(name, "__atomic_load_n") == 0
   4593 	    || strcmp(name, "__atomic_exchange_n") == 0
   4594 	    || strcmp(name, "__atomic_add_fetch") == 0
   4595 	    || strcmp(name, "__atomic_sub_fetch") == 0
   4596 	    || strcmp(name, "__atomic_and_fetch") == 0
   4597 	    || strcmp(name, "__atomic_xor_fetch") == 0
   4598 	    || strcmp(name, "__atomic_or_fetch") == 0
   4599 	    || strcmp(name, "__atomic_nand_fetch") == 0
   4600 	    || strcmp(name, "__atomic_fetch_add") == 0
   4601 	    || strcmp(name, "__atomic_fetch_sub") == 0
   4602 	    || strcmp(name, "__atomic_fetch_and") == 0
   4603 	    || strcmp(name, "__atomic_fetch_xor") == 0
   4604 	    || strcmp(name, "__atomic_fetch_or") == 0
   4605 	    || strcmp(name, "__atomic_fetch_nand") == 0;
   4606 }
   4607 
   4608 static type_t *
   4609 return_type(const function_call *call)
   4610 {
   4611 	const tnode_t *func = call->func;
   4612 	if (allow_gcc
   4613 	    && func->tn_op == ADDR
   4614 	    && func->u.ops.left->tn_op == NAME
   4615 	    && is_gcc_generic_atomic(func->u.ops.left->u.sym->s_name)
   4616 	    && call->args_len > 0
   4617 	    && call->args[0]->tn_type->t_tspec == PTR)
   4618 		return call->args[0]->tn_type->t_subt;
   4619 	return func->tn_type->t_subt->t_subt;
   4620 }
   4621 
   4622 tnode_t *
   4623 build_function_call(tnode_t *func, bool sys, function_call *call)
   4624 {
   4625 
   4626 	if (func == NULL)
   4627 		return NULL;
   4628 
   4629 	call->func = func;
   4630 	check_ctype_function_call(call);
   4631 
   4632 	func = cconv(func);
   4633 	call->func = func;
   4634 
   4635 	if (func->tn_type->t_tspec != PTR ||
   4636 	    func->tn_type->t_subt->t_tspec != FUNC) {
   4637 		/* cannot call '%s', must be a function */
   4638 		error(149, expr_type_name(func));
   4639 		return NULL;
   4640 	}
   4641 
   4642 	check_function_arguments(call);
   4643 
   4644 	tnode_t *ntn = expr_alloc_tnode();
   4645 	ntn->tn_op = CALL;
   4646 	ntn->tn_type = return_type(call);
   4647 	ntn->tn_sys = sys;
   4648 	ntn->u.call = call;
   4649 	return ntn;
   4650 }
   4651 
   4652 /*
   4653  * Return the value of an integral constant expression.
   4654  * If the expression is not constant or its type is not an integer
   4655  * type, an error message is printed.
   4656  */
   4657 val_t *
   4658 integer_constant(tnode_t *tn, bool required)
   4659 {
   4660 
   4661 	if (tn != NULL)
   4662 		tn = cconv(tn);
   4663 	if (tn != NULL)
   4664 		tn = promote(NOOP, false, tn);
   4665 
   4666 	val_t *v = xcalloc(1, sizeof(*v));
   4667 
   4668 	if (tn == NULL) {
   4669 		lint_assert(seen_error);
   4670 		debug_step("constant node is null; returning 1 instead");
   4671 		v->v_tspec = INT;
   4672 		v->u.integer = 1;
   4673 		return v;
   4674 	}
   4675 
   4676 	v->v_tspec = tn->tn_type->t_tspec;
   4677 
   4678 	if (tn->tn_op == CON) {
   4679 		lint_assert(tn->tn_type->t_tspec == tn->u.value.v_tspec);
   4680 		if (is_integer(tn->u.value.v_tspec)) {
   4681 			v->v_unsigned_since_c90 =
   4682 			    tn->u.value.v_unsigned_since_c90;
   4683 			v->u.integer = tn->u.value.u.integer;
   4684 			return v;
   4685 		}
   4686 		v->u.integer = (int64_t)tn->u.value.u.floating;
   4687 	} else
   4688 		v->u.integer = 1;
   4689 
   4690 	if (required)
   4691 		/* integral constant expression expected */
   4692 		error(55);
   4693 	else
   4694 		/* variable array dimension is a C99/GCC extension */
   4695 		c99ism(318);
   4696 
   4697 	if (!is_integer(v->v_tspec))
   4698 		v->v_tspec = INT;
   4699 
   4700 	return v;
   4701 }
   4702 
   4703 /*
   4704  * Perform some tests on expressions which can't be done in build_binary()
   4705  * and functions called by build_binary(). These tests must be done here
   4706  * because we need some information about the context in which the operations
   4707  * are performed.
   4708  * After all tests are performed, if free_expr is true, expr() frees the
   4709  * memory for the expression.
   4710  */
   4711 void
   4712 expr(tnode_t *tn, bool used, bool cond, bool free_expr, bool is_do_while,
   4713     const char *stmt_kind)
   4714 {
   4715 
   4716 	if (tn == NULL) {	/* in case of errors */
   4717 		expr_free_all();
   4718 		return;
   4719 	}
   4720 
   4721 	/* expr() is also called in global initializations */
   4722 	if (dcs->d_kind != DLK_EXTERN && !is_do_while)
   4723 		check_statement_reachable(stmt_kind);
   4724 
   4725 	check_expr_misc(tn, used, cond, !cond, false, false, false);
   4726 	if (tn->tn_op == ASSIGN && !tn->tn_parenthesized) {
   4727 		if (hflag && cond)
   4728 			/* assignment in conditional context */
   4729 			warning(159);
   4730 	}
   4731 	if (!modtab[tn->tn_op].m_has_side_effect) {
   4732 		/*
   4733 		 * for left operands of COMMA this warning is already printed
   4734 		 */
   4735 		if (tn->tn_op != COMMA && !used && !cond)
   4736 			check_null_effect(tn);
   4737 	}
   4738 	debug_node(tn);
   4739 
   4740 	if (free_expr)
   4741 		expr_free_all();
   4742 }
   4743 
   4744 /* If the expression has the form '*(arr + idx)', check the array index. */
   4745 static void
   4746 check_array_index(const tnode_t *indir, bool taking_address)
   4747 {
   4748 	const tnode_t *plus, *arr, *idx;
   4749 
   4750 	if (indir->tn_op == INDIR
   4751 	    && (plus = indir->u.ops.left, plus->tn_op == PLUS)
   4752 	    && plus->u.ops.left->tn_op == ADDR
   4753 	    && (arr = plus->u.ops.left->u.ops.left, true)
   4754 	    && (arr->tn_op == STRING || arr->tn_op == NAME)
   4755 	    && arr->tn_type->t_tspec == ARRAY
   4756 	    && (idx = plus->u.ops.right, idx->tn_op == CON)
   4757 	    && (!is_incomplete(arr->tn_type) || idx->u.value.u.integer < 0))
   4758 		goto proceed;
   4759 	return;
   4760 
   4761 proceed:;
   4762 	int elsz = length_in_bits(arr->tn_type->t_subt, NULL);
   4763 	if (elsz == 0)
   4764 		return;
   4765 	elsz /= CHAR_SIZE;
   4766 
   4767 	/* Change the unit of the index from bytes to element size. */
   4768 	int64_t con = is_uinteger(idx->tn_type->t_tspec)
   4769 	    ? (int64_t)((uint64_t)idx->u.value.u.integer / elsz)
   4770 	    : idx->u.value.u.integer / elsz;
   4771 
   4772 	int dim = arr->tn_type->u.dimension + (taking_address ? 1 : 0);
   4773 
   4774 	if (!is_uinteger(idx->tn_type->t_tspec) && con < 0)
   4775 		/* array subscript %jd cannot be negative */
   4776 		warning(167, (intmax_t)con);
   4777 	else if (dim > 0 && (uint64_t)con >= (uint64_t)dim)
   4778 		/* array subscript %ju cannot be > %d */
   4779 		warning(168, (uintmax_t)con, dim - 1);
   4780 }
   4781 
   4782 static void
   4783 check_expr_addr(const tnode_t *ln, bool szof, bool fcall)
   4784 {
   4785 	/* XXX: Taking warn_about_unreachable into account here feels wrong. */
   4786 	if (ln->tn_op == NAME && (reached || !warn_about_unreachable)) {
   4787 		if (!szof)
   4788 			mark_as_set(ln->u.sym);
   4789 		mark_as_used(ln->u.sym, fcall, szof);
   4790 	}
   4791 	check_array_index(ln, true);
   4792 }
   4793 
   4794 /*
   4795  * If there is an asm statement in one of the compound statements around,
   4796  * there may be other side effects, so don't warn.
   4797  */
   4798 static bool
   4799 is_asm_around(void)
   4800 {
   4801 	for (decl_level *dl = dcs; dl != NULL; dl = dl->d_enclosing)
   4802 		if (dl->d_asm)
   4803 			return true;
   4804 	return false;
   4805 }
   4806 
   4807 static void
   4808 check_expr_side_effect(const tnode_t *ln, bool szof)
   4809 {
   4810 
   4811 	/* XXX: Taking warn_about_unreachable into account here feels wrong. */
   4812 	if (ln->tn_op == NAME && (reached || !warn_about_unreachable)) {
   4813 		scl_t sc = ln->u.sym->s_scl;
   4814 		if (sc != EXTERN && sc != STATIC &&
   4815 		    !ln->u.sym->s_set && !szof && !is_asm_around()) {
   4816 			/* '%s' may be used before set */
   4817 			warning(158, ln->u.sym->s_name);
   4818 			mark_as_set(ln->u.sym);
   4819 		}
   4820 		mark_as_used(ln->u.sym, false, false);
   4821 	}
   4822 }
   4823 
   4824 static void
   4825 check_expr_assign(const tnode_t *ln, bool szof)
   4826 {
   4827 	/* XXX: Taking warn_about_unreachable into account here feels wrong. */
   4828 	if (ln->tn_op == NAME && !szof && (reached || !warn_about_unreachable)) {
   4829 		mark_as_set(ln->u.sym);
   4830 		if (ln->u.sym->s_scl == EXTERN)
   4831 			outusg(ln->u.sym);
   4832 	}
   4833 	check_array_index(ln, false);
   4834 }
   4835 
   4836 static void
   4837 check_expr_call(const tnode_t *tn, const tnode_t *ln,
   4838 		bool szof, bool vctx, bool cond, bool retval_discarded)
   4839 {
   4840 	lint_assert(ln->tn_op == ADDR);
   4841 	lint_assert(ln->u.ops.left->tn_op == NAME);
   4842 	if (!szof && !is_compiler_builtin(ln->u.ops.left->u.sym->s_name))
   4843 		outcall(tn, vctx || cond, retval_discarded);
   4844 
   4845 	const function_call *call = tn->u.call;
   4846 	if (call->args_len == 4 || call->args_len == 5)
   4847 		check_snprintb(call);
   4848 }
   4849 
   4850 static void
   4851 check_expr_op(op_t op, const tnode_t *ln, bool szof, bool fcall, bool eqwarn)
   4852 {
   4853 	switch (op) {
   4854 	case ADDR:
   4855 		check_expr_addr(ln, szof, fcall);
   4856 		break;
   4857 	case LOAD:
   4858 		check_array_index(ln, false);
   4859 		/* FALLTHROUGH */
   4860 	case INCBEF:
   4861 	case DECBEF:
   4862 	case INCAFT:
   4863 	case DECAFT:
   4864 	case ADDASS:
   4865 	case SUBASS:
   4866 	case MULASS:
   4867 	case DIVASS:
   4868 	case MODASS:
   4869 	case ANDASS:
   4870 	case ORASS:
   4871 	case XORASS:
   4872 	case SHLASS:
   4873 	case SHRASS:
   4874 	case REAL:
   4875 	case IMAG:
   4876 		check_expr_side_effect(ln, szof);
   4877 		break;
   4878 	case ASSIGN:
   4879 		check_expr_assign(ln, szof);
   4880 		break;
   4881 	case EQ:
   4882 		if (hflag && eqwarn)
   4883 			/* operator '==' found where '=' was expected */
   4884 			warning(160);
   4885 		break;
   4886 	default:
   4887 		break;
   4888 	}
   4889 }
   4890 
   4891 /*
   4892  *	vctx			???
   4893  *	cond			whether the expression is a condition that
   4894  *				will be compared with 0
   4895  *	eqwarn			whether the operator '==' might be a
   4896  *				misspelled '='
   4897  *	fcall			whether the expression is a function call
   4898  *	retval_discarded	whether the return value of a function call
   4899  *				is discarded; such calls will be analyzed by
   4900  *				lint2 in messages 4, 8 and 9
   4901  *	szof			whether the expression is part of a sizeof
   4902  *				expression, which means that its value is
   4903  *				discarded since only the type is relevant
   4904  */
   4905 void
   4906 check_expr_misc(const tnode_t *tn, bool vctx, bool cond,
   4907 		bool eqwarn, bool fcall, bool retval_discarded, bool szof)
   4908 {
   4909 
   4910 	if (tn == NULL)
   4911 		return;
   4912 	op_t op = tn->tn_op;
   4913 	if (op == NAME || op == CON || op == STRING)
   4914 		return;
   4915 	bool is_direct = op == CALL
   4916 	    && tn->u.call->func->tn_op == ADDR
   4917 	    && tn->u.call->func->u.ops.left->tn_op == NAME;
   4918 	if (op == CALL) {
   4919 		const function_call *call = tn->u.call;
   4920 		if (is_direct)
   4921 			check_expr_call(tn, call->func,
   4922 			    szof, vctx, cond, retval_discarded);
   4923 		bool discard = op == CVT && tn->tn_type->t_tspec == VOID;
   4924 		check_expr_misc(call->func, false, false, false, is_direct,
   4925 		    discard, szof);
   4926 		for (size_t i = 0, n = call->args_len; i < n; i++)
   4927 			check_expr_misc(call->args[i],
   4928 			    true, false, false, false, false, szof);
   4929 		return;
   4930 	}
   4931 
   4932 	lint_assert(has_operands(tn));
   4933 	tnode_t *ln = tn->u.ops.left;
   4934 	tnode_t *rn = tn->u.ops.right;
   4935 	check_expr_op(op, ln, szof, fcall, eqwarn);
   4936 
   4937 	const mod_t *mp = &modtab[op];
   4938 	bool cvctx = mp->m_value_context;
   4939 	bool ccond = mp->m_compares_with_zero;
   4940 	bool eq = mp->m_warn_if_operand_eq &&
   4941 	    !ln->tn_parenthesized &&
   4942 	    rn != NULL && !rn->tn_parenthesized;
   4943 
   4944 	/*
   4945 	 * Values of operands of ':' are not used if the type of at least
   4946 	 * one of the operands (for GCC compatibility) is 'void'.
   4947 	 *
   4948 	 * XXX test/value context of QUEST should probably be used as
   4949 	 * context for both operands of COLON.
   4950 	 */
   4951 	if (op == COLON && tn->tn_type->t_tspec == VOID)
   4952 		cvctx = ccond = false;
   4953 	bool discard = op == CVT && tn->tn_type->t_tspec == VOID;
   4954 	check_expr_misc(ln, cvctx, ccond, eq, is_direct, discard, szof);
   4955 
   4956 	switch (op) {
   4957 	case LOGAND:
   4958 	case LOGOR:
   4959 		check_expr_misc(rn, false, true, eq, false, false, szof);
   4960 		break;
   4961 	case COLON:
   4962 		check_expr_misc(rn, cvctx, ccond, eq, false, false, szof);
   4963 		break;
   4964 	case COMMA:
   4965 		check_expr_misc(rn, vctx, cond, false, false, false, szof);
   4966 		break;
   4967 	default:
   4968 		if (mp->m_binary)
   4969 			check_expr_misc(rn, true, false, eq, false, false,
   4970 			    szof);
   4971 		break;
   4972 	}
   4973 }
   4974 
   4975 /*
   4976  * Return whether the expression can be used for static initialization.
   4977  *
   4978  * Constant initialization expressions must be constant or an address
   4979  * of a static object with an optional offset. In the first case,
   4980  * the result is returned in *offsp. In the second case, the static
   4981  * object is returned in *symp and the offset in *offsp.
   4982  *
   4983  * The expression can consist of PLUS, MINUS, ADDR, NAME, STRING and
   4984  * CON. Type conversions are allowed if they do not change binary
   4985  * representation (including width).
   4986  *
   4987  * C99 6.6 "Constant expressions"
   4988  * C99 6.7.8p4 restricts initializers for static storage duration
   4989  */
   4990 bool
   4991 constant_addr(const tnode_t *tn, const sym_t **symp, ptrdiff_t *offsp)
   4992 {
   4993 	const sym_t *sym;
   4994 	ptrdiff_t offs1, offs2;
   4995 	tspec_t t, ot;
   4996 
   4997 	switch (tn->tn_op) {
   4998 	case MINUS:
   4999 		if (tn->u.ops.right->tn_op == CVT)
   5000 			return constant_addr(tn->u.ops.right, symp, offsp);
   5001 		if (tn->u.ops.right->tn_op != CON)
   5002 			return false;
   5003 		/* FALLTHROUGH */
   5004 	case PLUS:
   5005 		offs1 = offs2 = 0;
   5006 		if (tn->u.ops.left->tn_op == CON) {
   5007 			offs1 = (ptrdiff_t)tn->u.ops.left->u.value.u.integer;
   5008 			if (!constant_addr(tn->u.ops.right, &sym, &offs2))
   5009 				return false;
   5010 		} else if (tn->u.ops.right->tn_op == CON) {
   5011 			offs2 = (ptrdiff_t)tn->u.ops.right->u.value.u.integer;
   5012 			if (tn->tn_op == MINUS)
   5013 				offs2 = -offs2;
   5014 			if (!constant_addr(tn->u.ops.left, &sym, &offs1))
   5015 				return false;
   5016 		} else {
   5017 			return false;
   5018 		}
   5019 		*symp = sym;
   5020 		*offsp = offs1 + offs2;
   5021 		return true;
   5022 	case ADDR:
   5023 		if (tn->u.ops.left->tn_op == NAME) {
   5024 			*symp = tn->u.ops.left->u.sym;
   5025 			*offsp = 0;
   5026 			return true;
   5027 		}
   5028 		*symp = NULL;
   5029 		*offsp = 0;
   5030 		return true;
   5031 	case CVT:
   5032 		t = tn->tn_type->t_tspec;
   5033 		ot = tn->u.ops.left->tn_type->t_tspec;
   5034 		if ((!is_integer(t) && t != PTR) ||
   5035 		    (!is_integer(ot) && ot != PTR)) {
   5036 			return false;
   5037 		}
   5038 #if 0
   5039 		/*-
   5040 		 * consider:
   5041 		 *	struct foo {
   5042 		 *		unsigned char a;
   5043 		 *	} f = {
   5044 		 *		(unsigned char)(unsigned long)
   5045 		 *		    (&(((struct foo *)0)->a))
   5046 		 *	};
   5047 		 * since psize(unsigned long) != psize(unsigned char),
   5048 		 * this fails.
   5049 		 */
   5050 		else if (psize(t) != psize(ot))
   5051 			return -1;
   5052 #endif
   5053 		return constant_addr(tn->u.ops.left, symp, offsp);
   5054 	default:
   5055 		return false;
   5056 	}
   5057 }
   5058 
   5059 /* Append s2 to s1, then free s2. */
   5060 buffer *
   5061 cat_strings(buffer *s1, buffer *s2)
   5062 {
   5063 
   5064 	if ((s1->data != NULL) != (s2->data != NULL)) {
   5065 		/* cannot concatenate wide and regular string literals */
   5066 		error(292);
   5067 		return s1;
   5068 	}
   5069 
   5070 	if (s1->data != NULL) {
   5071 		while (s1->len + s2->len + 1 > s1->cap)
   5072 			s1->cap *= 2;
   5073 		s1->data = xrealloc(s1->data, s1->cap);
   5074 		memcpy(s1->data + s1->len, s2->data, s2->len + 1);
   5075 		free(s2->data);
   5076 	}
   5077 	s1->len += s2->len;
   5078 	free(s2);
   5079 
   5080 	return s1;
   5081 }
   5082 
   5083 
   5084 typedef struct stmt_expr {
   5085 	memory_pool se_mem;
   5086 	sym_t *se_sym;
   5087 	struct stmt_expr *se_enclosing;
   5088 } stmt_expr;
   5089 
   5090 static stmt_expr *stmt_exprs;
   5091 
   5092 void
   5093 begin_statement_expr(void)
   5094 {
   5095 	debug_enter();
   5096 
   5097 	stmt_expr *se = xmalloc(sizeof(*se));
   5098 	se->se_mem = expr_save_memory();
   5099 	se->se_sym = NULL;
   5100 	se->se_enclosing = stmt_exprs;
   5101 	stmt_exprs = se;
   5102 }
   5103 
   5104 void
   5105 do_statement_expr(tnode_t *tn)
   5106 {
   5107 	block_level--;
   5108 	mem_block_level--;
   5109 	stmt_exprs->se_sym = tn != NULL
   5110 	    ? mktempsym(block_dup_type(tn->tn_type))
   5111 	    : NULL;		/* after a syntax error */
   5112 	mem_block_level++;
   5113 	block_level++;
   5114 	/* '({ ... })' is a GCC extension */
   5115 	gnuism(320);
   5116 }
   5117 
   5118 tnode_t *
   5119 end_statement_expr(void)
   5120 {
   5121 	tnode_t *tn;
   5122 
   5123 	stmt_expr *se = stmt_exprs;
   5124 	if (se->se_sym == NULL) {
   5125 		tn = NULL;	/* after a syntax error */
   5126 		goto end;
   5127 	}
   5128 
   5129 	tn = build_name(se->se_sym, false);
   5130 	(void)expr_save_memory();	/* leak */
   5131 	expr_restore_memory(se->se_mem);
   5132 	stmt_exprs = se->se_enclosing;
   5133 	free(se);
   5134 
   5135 end:
   5136 	debug_leave();
   5137 	return tn;
   5138 }
   5139 
   5140 bool
   5141 in_statement_expr(void)
   5142 {
   5143 	return stmt_exprs != NULL;
   5144 }
   5145