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