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