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