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