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