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