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