Home | History | Annotate | Line # | Download | only in lint1
tree.c revision 1.496
      1 /*	$NetBSD: tree.c,v 1.496 2023/01/28 00:24:05 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.496 2023/01/28 00:24:05 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 (Tflag && 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 (!is_member(csym))
    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 (!is_member(sym))
    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 = STRUCT_MEMBER;
    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 
   2078 	tnode_t *ntn = expr_alloc_tnode();
   2079 	ntn->tn_op = op;
   2080 	ntn->tn_type = type;
   2081 	ntn->tn_sys = sys;
   2082 	ntn->tn_left = ln;
   2083 	ntn->tn_right = rn;
   2084 
   2085 	if (op == INDIR || op == FSEL) {
   2086 		lint_assert(ln->tn_type->t_tspec == PTR);
   2087 		tspec_t t = ln->tn_type->t_subt->t_tspec;
   2088 		if (t != FUNC && t != VOID)
   2089 			ntn->tn_lvalue = true;
   2090 	}
   2091 
   2092 	return ntn;
   2093 }
   2094 
   2095 /* In traditional C, keep unsigned and promote FLOAT to DOUBLE. */
   2096 static tspec_t
   2097 promote_trad(tspec_t t)
   2098 {
   2099 
   2100 	if (t == UCHAR || t == USHORT)
   2101 		return UINT;
   2102 	if (t == CHAR || t == SCHAR || t == SHORT)
   2103 		return INT;
   2104 	if (t == FLOAT)
   2105 		return DOUBLE;
   2106 	if (t == ENUM)
   2107 		return INT;
   2108 	return t;
   2109 }
   2110 
   2111 /*
   2112  * C99 6.3.1.1p2 requires for types with lower rank than int that "If an int
   2113  * can represent all the values of the original type, the value is converted
   2114  * to an int; otherwise it is converted to an unsigned int", and that "All
   2115  * other types are unchanged by the integer promotions".
   2116  */
   2117 static tspec_t
   2118 promote_c90(const tnode_t *tn, tspec_t t, bool farg)
   2119 {
   2120 	if (tn->tn_type->t_bitfield) {
   2121 		unsigned int len = tn->tn_type->t_flen;
   2122 		if (len < size_in_bits(INT))
   2123 			return INT;
   2124 		if (len == size_in_bits(INT))
   2125 			return is_uinteger(t) ? UINT : INT;
   2126 		return t;
   2127 	}
   2128 
   2129 	if (t == CHAR || t == UCHAR || t == SCHAR)
   2130 		return size_in_bits(CHAR) < size_in_bits(INT) || t != UCHAR
   2131 		    ? INT : UINT;
   2132 	if (t == SHORT || t == USHORT)
   2133 		return size_in_bits(SHORT) < size_in_bits(INT) || t == SHORT
   2134 		    ? INT : UINT;
   2135 	if (t == ENUM)
   2136 		return INT;
   2137 	if (farg && t == FLOAT)
   2138 		return DOUBLE;
   2139 	return t;
   2140 }
   2141 
   2142 /*
   2143  * Performs the "integer promotions" (C99 6.3.1.1p2), which convert small
   2144  * integer types to either int or unsigned int.
   2145  *
   2146  * If allow_c90 is unset or the operand is a function argument with no type
   2147  * information (no prototype or variable # of args), converts float to double.
   2148  */
   2149 tnode_t *
   2150 promote(op_t op, bool farg, tnode_t *tn)
   2151 {
   2152 
   2153 	tspec_t ot = tn->tn_type->t_tspec;
   2154 	if (!is_arithmetic(ot))
   2155 		return tn;
   2156 
   2157 	tspec_t nt = allow_c90 ? promote_c90(tn, ot, farg) : promote_trad(ot);
   2158 	if (nt == ot)
   2159 		return tn;
   2160 
   2161 	type_t *ntp = expr_dup_type(tn->tn_type);
   2162 	ntp->t_tspec = nt;
   2163 	/*
   2164 	 * Keep t_is_enum even though t_tspec gets converted from
   2165 	 * ENUM to INT, so we are later able to check compatibility
   2166 	 * of enum types.
   2167 	 */
   2168 	return convert(op, 0, ntp, tn);
   2169 }
   2170 
   2171 static tnode_t *
   2172 apply_usual_arithmetic_conversions(op_t op, tnode_t *tn, tspec_t t)
   2173 {
   2174 	type_t *ntp = expr_dup_type(tn->tn_type);
   2175 	ntp->t_tspec = t;
   2176 	if (tn->tn_op != CON) {
   2177 		/* usual arithmetic conversion for '%s' from '%s' to '%s' */
   2178 		query_message(4, op_name(op),
   2179 		    type_name(tn->tn_type), type_name(ntp));
   2180 	}
   2181 	return convert(op, 0, ntp, tn);
   2182 }
   2183 
   2184 /*
   2185  * Apply the "usual arithmetic conversions" (C99 6.3.1.8).
   2186  *
   2187  * This gives both operands the same type.
   2188  * This is done in different ways for traditional C and C90.
   2189  */
   2190 static void
   2191 balance(op_t op, tnode_t **lnp, tnode_t **rnp)
   2192 {
   2193 	tspec_t	lt, rt, t;
   2194 	int	i;
   2195 	bool	u;
   2196 	static const tspec_t tl[] = {
   2197 		LDOUBLE, DOUBLE, FLOAT,
   2198 #ifdef INT128_SIZE
   2199 		UINT128, INT128,
   2200 #endif
   2201 		UQUAD, QUAD,
   2202 		ULONG, LONG,
   2203 		UINT, INT,
   2204 	};
   2205 
   2206 	lt = (*lnp)->tn_type->t_tspec;
   2207 	rt = (*rnp)->tn_type->t_tspec;
   2208 
   2209 	if (!is_arithmetic(lt) || !is_arithmetic(rt))
   2210 		return;
   2211 
   2212 	if (allow_c90) {
   2213 		if (lt == rt) {
   2214 			t = lt;
   2215 		} else if (lt == LCOMPLEX || rt == LCOMPLEX) {
   2216 			t = LCOMPLEX;
   2217 		} else if (lt == DCOMPLEX || rt == DCOMPLEX) {
   2218 			t = DCOMPLEX;
   2219 		} else if (lt == FCOMPLEX || rt == FCOMPLEX) {
   2220 			t = FCOMPLEX;
   2221 		} else if (lt == LDOUBLE || rt == LDOUBLE) {
   2222 			t = LDOUBLE;
   2223 		} else if (lt == DOUBLE || rt == DOUBLE) {
   2224 			t = DOUBLE;
   2225 		} else if (lt == FLOAT || rt == FLOAT) {
   2226 			t = FLOAT;
   2227 		} else {
   2228 			/*
   2229 			 * If type A has more bits than type B it should
   2230 			 * be able to hold all possible values of type B.
   2231 			 */
   2232 			if (size_in_bits(lt) > size_in_bits(rt)) {
   2233 				t = lt;
   2234 			} else if (size_in_bits(lt) < size_in_bits(rt)) {
   2235 				t = rt;
   2236 			} else {
   2237 				for (i = 3; tl[i] != INT; i++) {
   2238 					if (tl[i] == lt || tl[i] == rt)
   2239 						break;
   2240 				}
   2241 				if ((is_uinteger(lt) || is_uinteger(rt)) &&
   2242 				    !is_uinteger(tl[i])) {
   2243 					i--;
   2244 				}
   2245 				t = tl[i];
   2246 			}
   2247 		}
   2248 	} else {
   2249 		/* Keep unsigned in traditional C */
   2250 		u = is_uinteger(lt) || is_uinteger(rt);
   2251 		for (i = 0; tl[i] != INT; i++) {
   2252 			if (lt == tl[i] || rt == tl[i])
   2253 				break;
   2254 		}
   2255 		t = tl[i];
   2256 		if (u && is_integer(t) && !is_uinteger(t))
   2257 			t = unsigned_type(t);
   2258 	}
   2259 
   2260 	if (t != lt)
   2261 		*lnp = apply_usual_arithmetic_conversions(op, *lnp, t);
   2262 	if (t != rt)
   2263 		*rnp = apply_usual_arithmetic_conversions(op, *rnp, t);
   2264 }
   2265 
   2266 static void
   2267 convert_integer_from_floating(op_t op, const type_t *tp, const tnode_t *tn)
   2268 {
   2269 
   2270 	if (op == CVT)
   2271 		/* cast from floating point '%s' to integer '%s' */
   2272 		query_message(2, type_name(tn->tn_type), type_name(tp));
   2273 	else
   2274 		/* implicit conversion from floating point '%s' to ... */
   2275 		query_message(1, type_name(tn->tn_type), type_name(tp));
   2276 }
   2277 
   2278 /*
   2279  * Insert a conversion operator, which converts the type of the node
   2280  * to another given type.
   2281  *
   2282  * Possible values for 'op':
   2283  *	CVT	a cast-expression
   2284  *	binary	integer promotion for one of the operands, or a usual
   2285  *		arithmetic conversion
   2286  *	binary	plain or compound assignments to bit-fields
   2287  *	FARG	'arg' is the number of the argument (used for warnings)
   2288  *	NOOP	several other implicit conversions
   2289  *	...
   2290  */
   2291 tnode_t *
   2292 convert(op_t op, int arg, type_t *tp, tnode_t *tn)
   2293 {
   2294 	tnode_t	*ntn;
   2295 	tspec_t	nt, ot;
   2296 
   2297 	nt = tp->t_tspec;
   2298 	ot = tn->tn_type->t_tspec;
   2299 
   2300 	if (allow_trad && allow_c90 && op == FARG)
   2301 		check_prototype_conversion(arg, nt, ot, tp, tn);
   2302 
   2303 	if (nt == BOOL) {
   2304 		/* No further checks. */
   2305 
   2306 	} else if (is_integer(nt)) {
   2307 		if (ot == BOOL) {
   2308 			/* No further checks. */
   2309 		} else if (is_integer(ot)) {
   2310 			convert_integer_from_integer(op, arg, nt, ot, tp, tn);
   2311 		} else if (is_floating(ot)) {
   2312 			convert_integer_from_floating(op, tp, tn);
   2313 		} else if (ot == PTR) {
   2314 			convert_integer_from_pointer(op, nt, tp, tn);
   2315 		}
   2316 
   2317 	} else if (is_floating(nt)) {
   2318 		/* No further checks. */
   2319 
   2320 	} else if (nt == PTR) {
   2321 		if (is_null_pointer(tn)) {
   2322 			/* a null pointer may be assigned to any pointer. */
   2323 		} else if (ot == PTR && op == CVT) {
   2324 			convert_pointer_from_pointer(tp, tn);
   2325 		}
   2326 	}
   2327 
   2328 	ntn = expr_alloc_tnode();
   2329 	ntn->tn_op = CVT;
   2330 	ntn->tn_type = tp;
   2331 	ntn->tn_cast = op == CVT;
   2332 	ntn->tn_sys |= tn->tn_sys;
   2333 	ntn->tn_right = NULL;
   2334 	if (tn->tn_op != CON || nt == VOID) {
   2335 		ntn->tn_left = tn;
   2336 	} else {
   2337 		ntn->tn_op = CON;
   2338 		ntn->tn_val = expr_zero_alloc(sizeof(*ntn->tn_val));
   2339 		convert_constant(op, arg, ntn->tn_type, ntn->tn_val,
   2340 		    tn->tn_val);
   2341 	}
   2342 
   2343 	return ntn;
   2344 }
   2345 
   2346 static bool
   2347 should_warn_about_prototype_conversion(tspec_t nt,
   2348 				       tspec_t ot, const tnode_t *ptn)
   2349 {
   2350 
   2351 	if (nt == ot)
   2352 		return false;
   2353 
   2354 	if (nt == ENUM && ot == INT)
   2355 		return false;
   2356 
   2357 	if (is_floating(nt) != is_floating(ot) ||
   2358 	    portable_size_in_bits(nt) != portable_size_in_bits(ot)) {
   2359 		/* representation and/or width change */
   2360 		if (!is_integer(ot))
   2361 			return true;
   2362 		/*
   2363 		 * XXX: Investigate whether this rule makes sense; see
   2364 		 * tests/usr.bin/xlint/lint1/platform_long.c.
   2365 		 */
   2366 		return portable_size_in_bits(ot) > portable_size_in_bits(INT);
   2367 	}
   2368 
   2369 	if (!hflag)
   2370 		return false;
   2371 
   2372 	/*
   2373 	 * If the types differ only in sign and the argument has the same
   2374 	 * representation in both types, print no warning.
   2375 	 */
   2376 	if (ptn->tn_op == CON && is_integer(nt) &&
   2377 	    signed_type(nt) == signed_type(ot) &&
   2378 	    !msb(ptn->tn_val->v_quad, ot))
   2379 		return false;
   2380 
   2381 	return true;
   2382 }
   2383 
   2384 /*
   2385  * Warn if a prototype causes a type conversion that is different from what
   2386  * would happen to the same argument in the absence of a prototype.  This
   2387  * check is intended for code that needs to stay compatible with pre-C90 C.
   2388  *
   2389  * Errors/warnings about illegal type combinations are already printed
   2390  * in check_assign_types_compatible().
   2391  */
   2392 static void
   2393 check_prototype_conversion(int arg, tspec_t nt, tspec_t ot, type_t *tp,
   2394 			   tnode_t *tn)
   2395 {
   2396 	tnode_t	*ptn;
   2397 
   2398 	if (!is_arithmetic(nt) || !is_arithmetic(ot))
   2399 		return;
   2400 
   2401 	/*
   2402 	 * If the type of the formal parameter is char/short, a warning
   2403 	 * would be useless, because functions declared the old style
   2404 	 * can't expect char/short arguments.
   2405 	 */
   2406 	if (nt == CHAR || nt == SCHAR || nt == UCHAR ||
   2407 	    nt == SHORT || nt == USHORT)
   2408 		return;
   2409 
   2410 	/* apply the default promotion */
   2411 	ptn = promote(NOOP, true, tn);
   2412 	ot = ptn->tn_type->t_tspec;
   2413 
   2414 	if (should_warn_about_prototype_conversion(nt, ot, ptn)) {
   2415 		/* argument #%d is converted from '%s' to '%s' ... */
   2416 		warning(259, arg, type_name(tn->tn_type), type_name(tp));
   2417 	}
   2418 }
   2419 
   2420 /*
   2421  * When converting a large integer type to a small integer type, in some
   2422  * cases the value of the actual expression is further restricted than the
   2423  * type bounds, such as in (expr & 0xFF) or (expr % 100) or (expr >> 24).
   2424  *
   2425  * See new_tnode, the '#if 0' code for SHR.
   2426  */
   2427 static bool
   2428 can_represent(const type_t *tp, const tnode_t *tn)
   2429 {
   2430 
   2431 	debug_step("%s: type '%s'", __func__, type_name(tp));
   2432 	debug_node(tn);
   2433 
   2434 	uint64_t nmask = value_bits(width_in_bits(tp));
   2435 	if (!is_uinteger(tp->t_tspec))
   2436 		nmask >>= 1;
   2437 
   2438 	integer_constraints c = ic_expr(tn);
   2439 	if ((~c.bclr & ~nmask) == 0)
   2440 		return true;
   2441 
   2442 	return false;
   2443 }
   2444 
   2445 static void
   2446 convert_integer_from_integer(op_t op, int arg, tspec_t nt, tspec_t ot,
   2447 			     type_t *tp, tnode_t *tn)
   2448 {
   2449 
   2450 	if (tn->tn_op == CON)
   2451 		return;
   2452 
   2453 	if (op == CVT)
   2454 		return;
   2455 
   2456 	if (Pflag && pflag && aflag > 0 &&
   2457 	    portable_size_in_bits(nt) > portable_size_in_bits(ot) &&
   2458 	    is_uinteger(nt) != is_uinteger(ot)) {
   2459 		if (op == FARG) {
   2460 			/* conversion to '%s' may sign-extend ... */
   2461 			warning(297, type_name(tp), arg);
   2462 		} else {
   2463 			/* conversion to '%s' may sign-extend ... */
   2464 			warning(131, type_name(tp));
   2465 		}
   2466 	}
   2467 
   2468 	if (Pflag && portable_size_in_bits(nt) > portable_size_in_bits(ot) &&
   2469 	    (tn->tn_op == PLUS || tn->tn_op == MINUS || tn->tn_op == MULT ||
   2470 	     tn->tn_op == SHL)) {
   2471 		/* suggest cast from '%s' to '%s' on op '%s' to ... */
   2472 		warning(324, type_name(gettyp(ot)), type_name(tp),
   2473 		    op_name(tn->tn_op));
   2474 	}
   2475 
   2476 	if (aflag > 0 &&
   2477 	    portable_size_in_bits(nt) < portable_size_in_bits(ot) &&
   2478 	    (ot == LONG || ot == ULONG || ot == QUAD || ot == UQUAD ||
   2479 	     aflag > 1) &&
   2480 	    !can_represent(tp, tn)) {
   2481 		if (op == FARG) {
   2482 			/* conversion from '%s' to '%s' may lose ... */
   2483 			warning(298,
   2484 			    type_name(tn->tn_type), type_name(tp), arg);
   2485 		} else {
   2486 			/* conversion from '%s' to '%s' may lose accuracy */
   2487 			warning(132,
   2488 			    type_name(tn->tn_type), type_name(tp));
   2489 		}
   2490 	}
   2491 
   2492 	if (is_uinteger(nt) != is_uinteger(ot))
   2493 		/* implicit conversion changes sign from '%s' to '%s' */
   2494 		query_message(3, type_name(tn->tn_type), type_name(tp));
   2495 }
   2496 
   2497 static void
   2498 convert_integer_from_pointer(op_t op, tspec_t nt, type_t *tp, tnode_t *tn)
   2499 {
   2500 
   2501 	if (tn->tn_op == CON)
   2502 		return;
   2503 	if (op != CVT)
   2504 		return;		/* We got already an error. */
   2505 	if (portable_size_in_bits(nt) >= portable_size_in_bits(PTR))
   2506 		return;
   2507 
   2508 	if (pflag && size_in_bits(nt) >= size_in_bits(PTR)) {
   2509 		/* conversion of pointer to '%s' may lose bits */
   2510 		warning(134, type_name(tp));
   2511 	} else {
   2512 		/* conversion of pointer to '%s' loses bits */
   2513 		warning(133, type_name(tp));
   2514 	}
   2515 }
   2516 
   2517 static bool
   2518 struct_starts_with(const type_t *struct_tp, const type_t *member_tp)
   2519 {
   2520 
   2521 	return struct_tp->t_str->sou_first_member != NULL &&
   2522 	       types_compatible(struct_tp->t_str->sou_first_member->s_type,
   2523 		   member_tp, true, false, NULL);
   2524 }
   2525 
   2526 static bool
   2527 is_byte_array(const type_t *tp)
   2528 {
   2529 
   2530 	return tp->t_tspec == ARRAY &&
   2531 	       (tp->t_subt->t_tspec == CHAR || tp->t_subt->t_tspec == UCHAR);
   2532 }
   2533 
   2534 static bool
   2535 should_warn_about_pointer_cast(const type_t *nstp, tspec_t nst,
   2536 			       const type_t *ostp, tspec_t ost)
   2537 {
   2538 
   2539 	while (nst == ARRAY)
   2540 		nstp = nstp->t_subt, nst = nstp->t_tspec;
   2541 	while (ost == ARRAY)
   2542 		ostp = ostp->t_subt, ost = ostp->t_tspec;
   2543 
   2544 	if (nst == STRUCT && ost == STRUCT &&
   2545 	    (struct_starts_with(nstp, ostp) ||
   2546 	     struct_starts_with(ostp, nstp)))
   2547 		return false;
   2548 
   2549 	if (is_incomplete(nstp) || is_incomplete(ostp))
   2550 		return false;
   2551 
   2552 	if (nst == CHAR || nst == UCHAR)
   2553 		return false;	/* for the sake of traditional C code */
   2554 	if (ost == CHAR || ost == UCHAR)
   2555 		return false;	/* for the sake of traditional C code */
   2556 
   2557 	/* Allow cast between pointers to sockaddr variants. */
   2558 	if (nst == STRUCT && ost == STRUCT) {
   2559 		debug_type(nstp);
   2560 		debug_type(ostp);
   2561 		const sym_t *nmem = nstp->t_str->sou_first_member;
   2562 		const sym_t *omem = ostp->t_str->sou_first_member;
   2563 		while (nmem != NULL && omem != NULL &&
   2564 		       types_compatible(nmem->s_type, omem->s_type,
   2565 			   true, false, NULL))
   2566 			nmem = nmem->s_next, omem = omem->s_next;
   2567 		if (nmem != NULL && is_byte_array(nmem->s_type))
   2568 			return false;
   2569 		if (omem != NULL && is_byte_array(omem->s_type))
   2570 			return false;
   2571 		if (nmem == NULL && omem == NULL)
   2572 			return false;
   2573 	}
   2574 
   2575 	if (is_struct_or_union(nst) && nstp->t_str != ostp->t_str)
   2576 		return true;
   2577 
   2578 	return portable_size_in_bits(nst) != portable_size_in_bits(ost);
   2579 }
   2580 
   2581 static void
   2582 convert_pointer_from_pointer(type_t *ntp, tnode_t *tn)
   2583 {
   2584 	const type_t *nstp, *otp, *ostp;
   2585 	tspec_t nst, ost;
   2586 	const char *nts, *ots;
   2587 
   2588 	nstp = ntp->t_subt;
   2589 	otp = tn->tn_type;
   2590 	ostp = otp->t_subt;
   2591 	nst = nstp->t_tspec;
   2592 	ost = ostp->t_tspec;
   2593 
   2594 	if (nst == VOID || ost == VOID) {
   2595 		/* TODO: C99 behaves like C90 here. */
   2596 		if ((!allow_trad && !allow_c99) && (nst == FUNC || ost == FUNC)) {
   2597 			/* null pointers are already handled in convert() */
   2598 			*(nst == FUNC ? &nts : &ots) = "function pointer";
   2599 			*(nst == VOID ? &nts : &ots) = "'void *'";
   2600 			/* ANSI C forbids conversion of %s to %s */
   2601 			warning(303, ots, nts);
   2602 		}
   2603 		return;
   2604 	} else if (nst == FUNC && ost == FUNC) {
   2605 		return;
   2606 	} else if (nst == FUNC || ost == FUNC) {
   2607 		/* converting '%s' to '%s' is questionable */
   2608 		warning(229, type_name(otp), type_name(ntp));
   2609 		return;
   2610 	}
   2611 
   2612 	if (hflag && alignment_in_bits(nstp) > alignment_in_bits(ostp) &&
   2613 	    ost != CHAR && ost != UCHAR &&
   2614 	    !is_incomplete(ostp)) {
   2615 		/* converting '%s' to '%s' increases alignment ... */
   2616 		warning(135, type_name(otp), type_name(ntp),
   2617 		    alignment_in_bits(ostp) / CHAR_SIZE,
   2618 		    alignment_in_bits(nstp) / CHAR_SIZE);
   2619 	}
   2620 
   2621 	if (cflag && should_warn_about_pointer_cast(nstp, nst, ostp, ost)) {
   2622 		/* pointer cast from '%s' to '%s' may be troublesome */
   2623 		warning(247, type_name(otp), type_name(ntp));
   2624 	}
   2625 }
   2626 
   2627 static void
   2628 convert_constant_floating(op_t op, int arg, tspec_t ot, const type_t *tp,
   2629 			  tspec_t nt, val_t *v, val_t *nv)
   2630 {
   2631 	ldbl_t	max = 0.0, min = 0.0;
   2632 
   2633 	switch (nt) {
   2634 	case CHAR:
   2635 		max = TARG_CHAR_MAX;	min = TARG_CHAR_MIN;	break;
   2636 	case UCHAR:
   2637 		max = TARG_UCHAR_MAX;	min = 0;		break;
   2638 	case SCHAR:
   2639 		max = TARG_SCHAR_MAX;	min = TARG_SCHAR_MIN;	break;
   2640 	case SHORT:
   2641 		max = TARG_SHRT_MAX;	min = TARG_SHRT_MIN;	break;
   2642 	case USHORT:
   2643 		max = TARG_USHRT_MAX;	min = 0;		break;
   2644 	case ENUM:
   2645 	case INT:
   2646 		max = TARG_INT_MAX;	min = TARG_INT_MIN;	break;
   2647 	case UINT:
   2648 		max = TARG_UINT_MAX;	min = 0;		break;
   2649 	case LONG:
   2650 		max = TARG_LONG_MAX;	min = TARG_LONG_MIN;	break;
   2651 	case ULONG:
   2652 		max = TARG_ULONG_MAX;	min = 0;		break;
   2653 	case QUAD:
   2654 		max = QUAD_MAX;		min = QUAD_MIN;		break;
   2655 	case UQUAD:
   2656 		max = UQUAD_MAX;	min = 0;		break;
   2657 	case FLOAT:
   2658 	case FCOMPLEX:
   2659 		max = FLT_MAX;		min = -FLT_MAX;		break;
   2660 	case DOUBLE:
   2661 	case DCOMPLEX:
   2662 		max = DBL_MAX;		min = -DBL_MAX;		break;
   2663 	case PTR:
   2664 		/* Got already an error because of float --> ptr */
   2665 	case LDOUBLE:
   2666 	case LCOMPLEX:
   2667 		/* LINTED 248 */
   2668 		max = LDBL_MAX;		min = -max;		break;
   2669 	default:
   2670 		lint_assert(/*CONSTCOND*/false);
   2671 	}
   2672 	if (v->v_ldbl > max || v->v_ldbl < min) {
   2673 		lint_assert(nt != LDOUBLE);
   2674 		if (op == FARG) {
   2675 			/* conversion of '%s' to '%s' is out of range, ... */
   2676 			warning(295,
   2677 			    type_name(gettyp(ot)), type_name(tp), arg);
   2678 		} else {
   2679 			/* conversion of '%s' to '%s' is out of range */
   2680 			warning(119,
   2681 			    type_name(gettyp(ot)), type_name(tp));
   2682 		}
   2683 		v->v_ldbl = v->v_ldbl > 0 ? max : min;
   2684 	}
   2685 
   2686 	if (nt == FLOAT) {
   2687 		nv->v_ldbl = (float)v->v_ldbl;
   2688 	} else if (nt == DOUBLE) {
   2689 		nv->v_ldbl = (double)v->v_ldbl;
   2690 	} else if (nt == LDOUBLE) {
   2691 		nv->v_ldbl = v->v_ldbl;
   2692 	} else {
   2693 		nv->v_quad = (int64_t)v->v_ldbl;
   2694 	}
   2695 }
   2696 
   2697 static bool
   2698 convert_constant_to_floating(tspec_t nt, val_t *nv,
   2699 			     tspec_t ot, const val_t *v)
   2700 {
   2701 	if (nt == FLOAT) {
   2702 		nv->v_ldbl = (ot == PTR || is_uinteger(ot)) ?
   2703 		    (float)(uint64_t)v->v_quad : (float)v->v_quad;
   2704 	} else if (nt == DOUBLE) {
   2705 		nv->v_ldbl = (ot == PTR || is_uinteger(ot)) ?
   2706 		    (double)(uint64_t)v->v_quad : (double)v->v_quad;
   2707 	} else if (nt == LDOUBLE) {
   2708 		nv->v_ldbl = (ot == PTR || is_uinteger(ot)) ?
   2709 		    (ldbl_t)(uint64_t)v->v_quad : (ldbl_t)v->v_quad;
   2710 	} else
   2711 		return false;
   2712 	return true;
   2713 }
   2714 
   2715 /*
   2716  * Print a warning if bits which were set are lost due to the conversion.
   2717  * This can happen with operator ORASS only.
   2718  */
   2719 static void
   2720 convert_constant_check_range_bitor(size_t nsz, size_t osz, const val_t *v,
   2721 				   uint64_t xmask, op_t op)
   2722 {
   2723 	if (nsz < osz && (v->v_quad & xmask) != 0) {
   2724 		/* constant truncated by conversion, op '%s' */
   2725 		warning(306, op_name(op));
   2726 	}
   2727 }
   2728 
   2729 /*
   2730  * Print a warning if additional bits are not all 1
   2731  * and the most significant bit of the old value is 1,
   2732  * or if at least one (but not all) removed bit was 0.
   2733  */
   2734 static void
   2735 convert_constant_check_range_bitand(size_t nsz, size_t osz,
   2736 				    uint64_t xmask, const val_t *nv,
   2737 				    tspec_t ot, const val_t *v,
   2738 				    const type_t *tp, op_t op)
   2739 {
   2740 	if (nsz > osz &&
   2741 	    (nv->v_quad & bit((unsigned int)(osz - 1))) != 0 &&
   2742 	    (nv->v_quad & xmask) != xmask) {
   2743 		/* extra bits set to 0 in conversion of '%s' to '%s', ... */
   2744 		warning(309, type_name(gettyp(ot)),
   2745 		    type_name(tp), op_name(op));
   2746 	} else if (nsz < osz &&
   2747 		   (v->v_quad & xmask) != xmask &&
   2748 		   (v->v_quad & xmask) != 0) {
   2749 		/* constant truncated by conversion, op '%s' */
   2750 		warning(306, op_name(op));
   2751 	}
   2752 }
   2753 
   2754 static void
   2755 convert_constant_check_range_signed(op_t op, int arg)
   2756 {
   2757 	if (op == ASSIGN) {
   2758 		/* assignment of negative constant to unsigned type */
   2759 		warning(164);
   2760 	} else if (op == INIT) {
   2761 		/* initialization of unsigned with negative constant */
   2762 		warning(221);
   2763 	} else if (op == FARG) {
   2764 		/* conversion of negative constant to unsigned type, ... */
   2765 		warning(296, arg);
   2766 	} else if (modtab[op].m_comparison) {
   2767 		/* handled by check_integer_comparison() */
   2768 	} else {
   2769 		/* conversion of negative constant to unsigned type */
   2770 		warning(222);
   2771 	}
   2772 }
   2773 
   2774 /*
   2775  * Loss of significant bit(s). All truncated bits
   2776  * of unsigned types or all truncated bits plus the
   2777  * msb of the target for signed types are considered
   2778  * to be significant bits. Loss of significant bits
   2779  * means that at least one of the bits was set in an
   2780  * unsigned type or that at least one but not all of
   2781  * the bits was set in a signed type.
   2782  * Loss of significant bits means that it is not
   2783  * possible, also not with necessary casts, to convert
   2784  * back to the original type. A example for a
   2785  * necessary cast is:
   2786  *	char c;	int	i; c = 128;
   2787  *	i = c;			** yields -128 **
   2788  *	i = (unsigned char)c;	** yields 128 **
   2789  */
   2790 static void
   2791 convert_constant_check_range_truncated(op_t op, int arg, const type_t *tp,
   2792 				       tspec_t ot)
   2793 {
   2794 	if (op == ASSIGN && tp->t_bitfield) {
   2795 		/* precision lost in bit-field assignment */
   2796 		warning(166);
   2797 	} else if (op == ASSIGN) {
   2798 		/* constant truncated by assignment */
   2799 		warning(165);
   2800 	} else if (op == INIT && tp->t_bitfield) {
   2801 		/* bit-field initializer does not fit */
   2802 		warning(180);
   2803 	} else if (op == INIT) {
   2804 		/* initializer does not fit */
   2805 		warning(178);
   2806 	} else if (op == CASE) {
   2807 		/* case label affected by conversion */
   2808 		warning(196);
   2809 	} else if (op == FARG) {
   2810 		/* conversion of '%s' to '%s' is out of range, arg #%d */
   2811 		warning(295,
   2812 		    type_name(gettyp(ot)), type_name(tp), arg);
   2813 	} else {
   2814 		/* conversion of '%s' to '%s' is out of range */
   2815 		warning(119,
   2816 		    type_name(gettyp(ot)), type_name(tp));
   2817 	}
   2818 }
   2819 
   2820 static void
   2821 convert_constant_check_range_loss(op_t op, int arg, const type_t *tp,
   2822 				  tspec_t ot)
   2823 {
   2824 	if (op == ASSIGN && tp->t_bitfield) {
   2825 		/* precision lost in bit-field assignment */
   2826 		warning(166);
   2827 	} else if (op == INIT && tp->t_bitfield) {
   2828 		/* bit-field initializer out of range */
   2829 		warning(11);
   2830 	} else if (op == CASE) {
   2831 		/* case label affected by conversion */
   2832 		warning(196);
   2833 	} else if (op == FARG) {
   2834 		/* conversion of '%s' to '%s' is out of range, arg #%d */
   2835 		warning(295,
   2836 		    type_name(gettyp(ot)), type_name(tp), arg);
   2837 	} else {
   2838 		/* conversion of '%s' to '%s' is out of range */
   2839 		warning(119,
   2840 		    type_name(gettyp(ot)), type_name(tp));
   2841 	}
   2842 }
   2843 
   2844 static void
   2845 convert_constant_check_range(tspec_t ot, const type_t *tp, tspec_t nt,
   2846 			     op_t op, int arg, const val_t *v, val_t *nv)
   2847 {
   2848 	unsigned int obitsz, nbitsz;
   2849 	uint64_t xmask, xmsk1;
   2850 
   2851 	obitsz = size_in_bits(ot);
   2852 	nbitsz = tp->t_bitfield ? tp->t_flen : size_in_bits(nt);
   2853 	xmask = value_bits(nbitsz) ^ value_bits(obitsz);
   2854 	xmsk1 = value_bits(nbitsz) ^ value_bits(obitsz - 1);
   2855 	/*
   2856 	 * For bitwise operations we are not interested in the arithmetic
   2857 	 * value, but in the bits itself.
   2858 	 */
   2859 	if (op == ORASS || op == BITOR || op == BITXOR) {
   2860 		convert_constant_check_range_bitor(
   2861 		    nbitsz, obitsz, v, xmask, op);
   2862 	} else if (op == ANDASS || op == BITAND) {
   2863 		convert_constant_check_range_bitand(
   2864 		    nbitsz, obitsz, xmask, nv, ot, v, tp, op);
   2865 	} else if ((nt != PTR && is_uinteger(nt)) &&
   2866 		   (ot != PTR && !is_uinteger(ot)) &&
   2867 		   v->v_quad < 0) {
   2868 		convert_constant_check_range_signed(op, arg);
   2869 	} else if (nv->v_quad != v->v_quad && nbitsz <= obitsz &&
   2870 		   (v->v_quad & xmask) != 0 &&
   2871 		   (is_uinteger(ot) || (v->v_quad & xmsk1) != xmsk1)) {
   2872 		convert_constant_check_range_truncated(op, arg, tp, ot);
   2873 	} else if (nv->v_quad != v->v_quad) {
   2874 		convert_constant_check_range_loss(op, arg, tp, ot);
   2875 	}
   2876 }
   2877 
   2878 /*
   2879  * Converts a typed constant to a constant of another type.
   2880  *
   2881  * op		operator which requires conversion
   2882  * arg		if op is FARG, # of argument
   2883  * tp		type in which to convert the constant
   2884  * nv		new constant
   2885  * v		old constant
   2886  */
   2887 void
   2888 convert_constant(op_t op, int arg, const type_t *tp, val_t *nv, val_t *v)
   2889 {
   2890 	tspec_t ot, nt;
   2891 	unsigned int sz;
   2892 	bool range_check;
   2893 
   2894 	/*
   2895 	 * TODO: make 'v' const; the name of this function does not suggest
   2896 	 *  that it modifies 'v'.
   2897 	 */
   2898 	ot = v->v_tspec;
   2899 	nt = nv->v_tspec = tp->t_tspec;
   2900 	range_check = false;
   2901 
   2902 	if (nt == BOOL) {	/* C99 6.3.1.2 */
   2903 		nv->v_unsigned_since_c90 = false;
   2904 		nv->v_quad = is_nonzero_val(v) ? 1 : 0;
   2905 		return;
   2906 	}
   2907 
   2908 	if (ot == FLOAT || ot == DOUBLE || ot == LDOUBLE) {
   2909 		convert_constant_floating(op, arg, ot, tp, nt, v, nv);
   2910 	} else if (!convert_constant_to_floating(nt, nv, ot, v)) {
   2911 		range_check = true;	/* Check for lost precision. */
   2912 		nv->v_quad = v->v_quad;
   2913 	}
   2914 
   2915 	if (allow_trad && allow_c90 && v->v_unsigned_since_c90 &&
   2916 	    (is_floating(nt) || (
   2917 		(is_integer(nt) && !is_uinteger(nt) &&
   2918 		 portable_size_in_bits(nt) > portable_size_in_bits(ot))))) {
   2919 		/* ANSI C treats constant as unsigned */
   2920 		warning(157);
   2921 		v->v_unsigned_since_c90 = false;
   2922 	}
   2923 
   2924 	if (is_integer(nt)) {
   2925 		sz = tp->t_bitfield ? tp->t_flen : size_in_bits(nt);
   2926 		nv->v_quad = convert_integer(nv->v_quad, nt, sz);
   2927 	}
   2928 
   2929 	if (range_check && op != CVT)
   2930 		convert_constant_check_range(ot, tp, nt, op, arg, v, nv);
   2931 }
   2932 
   2933 /*
   2934  * Called if incompatible types were detected.
   2935  * Prints a appropriate warning.
   2936  */
   2937 static void
   2938 warn_incompatible_types(op_t op,
   2939 			const type_t *ltp, tspec_t lt,
   2940 			const type_t *rtp, tspec_t rt)
   2941 {
   2942 	const mod_t *mp;
   2943 
   2944 	mp = &modtab[op];
   2945 
   2946 	if (lt == VOID || (mp->m_binary && rt == VOID)) {
   2947 		/* void type illegal in expression */
   2948 		error(109);
   2949 	} else if (op == ASSIGN) {
   2950 		/* cannot assign to '%s' from '%s' */
   2951 		error(171, type_name(ltp), type_name(rtp));
   2952 	} else if (mp->m_binary) {
   2953 		/* operands of '%s' have incompatible types '%s' and '%s' */
   2954 		error(107, mp->m_name, tspec_name(lt), tspec_name(rt));
   2955 	} else {
   2956 		lint_assert(rt == NOTSPEC);
   2957 		/* operand of '%s' has invalid type '%s' */
   2958 		error(108, mp->m_name, type_name(ltp));
   2959 	}
   2960 }
   2961 
   2962 /*
   2963  * Called if incompatible pointer types are detected.
   2964  * Print an appropriate warning.
   2965  */
   2966 static void
   2967 warn_incompatible_pointers(const mod_t *mp,
   2968 			   const type_t *ltp, const type_t *rtp)
   2969 {
   2970 	tspec_t	lt, rt;
   2971 
   2972 	lint_assert(ltp->t_tspec == PTR);
   2973 	lint_assert(rtp->t_tspec == PTR);
   2974 
   2975 	lt = ltp->t_subt->t_tspec;
   2976 	rt = rtp->t_subt->t_tspec;
   2977 
   2978 	if (is_struct_or_union(lt) && is_struct_or_union(rt)) {
   2979 		if (mp == NULL) {
   2980 			/* illegal structure pointer combination */
   2981 			warning(244);
   2982 		} else {
   2983 			/* incompatible structure pointers: '%s' '%s' '%s' */
   2984 			warning(245, type_name(ltp), mp->m_name, type_name(rtp));
   2985 		}
   2986 	} else {
   2987 		if (mp == NULL) {
   2988 			/* illegal combination of '%s' and '%s' */
   2989 			warning(184, type_name(ltp), type_name(rtp));
   2990 		} else {
   2991 			/* illegal combination of '%s' and '%s', op '%s' */
   2992 			warning(124,
   2993 			    type_name(ltp), type_name(rtp), mp->m_name);
   2994 		}
   2995 	}
   2996 }
   2997 
   2998 /* Return a type based on tp1, with added qualifiers from tp2. */
   2999 static type_t *
   3000 merge_qualifiers(type_t *tp1, const type_t *tp2)
   3001 {
   3002 	type_t *ntp, *nstp;
   3003 	bool c1, c2, v1, v2;
   3004 
   3005 	lint_assert(tp1->t_tspec == PTR);
   3006 	lint_assert(tp2->t_tspec == PTR);
   3007 
   3008 	c1 = tp1->t_subt->t_const;
   3009 	c2 = tp2->t_subt->t_const;
   3010 	v1 = tp1->t_subt->t_volatile;
   3011 	v2 = tp2->t_subt->t_volatile;
   3012 
   3013 	if (c1 == (c1 | c2) && v1 == (v1 | v2))
   3014 		return tp1;
   3015 
   3016 	nstp = expr_dup_type(tp1->t_subt);
   3017 	nstp->t_const |= c2;
   3018 	nstp->t_volatile |= v2;
   3019 
   3020 	ntp = expr_dup_type(tp1);
   3021 	ntp->t_subt = nstp;
   3022 	return ntp;
   3023 }
   3024 
   3025 /*
   3026  * Returns true if the given structure or union has a constant member
   3027  * (maybe recursively).
   3028  */
   3029 static bool
   3030 has_constant_member(const type_t *tp)
   3031 {
   3032 	sym_t *m;
   3033 
   3034 	lint_assert(is_struct_or_union(tp->t_tspec));
   3035 
   3036 	for (m = tp->t_str->sou_first_member; m != NULL; m = m->s_next) {
   3037 		const type_t *mtp = m->s_type;
   3038 		if (mtp->t_const)
   3039 			return true;
   3040 		if (is_struct_or_union(mtp->t_tspec) &&
   3041 		    has_constant_member(mtp))
   3042 			return true;
   3043 	}
   3044 	return false;
   3045 }
   3046 
   3047 /*
   3048  * Create a new node for one of the operators POINT and ARROW.
   3049  */
   3050 static tnode_t *
   3051 build_struct_access(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
   3052 {
   3053 	tnode_t	*ntn, *ctn;
   3054 	bool	nolval;
   3055 
   3056 	lint_assert(rn->tn_op == NAME);
   3057 	lint_assert(is_member(rn->tn_sym));
   3058 
   3059 	/*
   3060 	 * Remember if the left operand is an lvalue (structure members
   3061 	 * are lvalues if and only if the structure itself is an lvalue).
   3062 	 */
   3063 	nolval = op == POINT && !ln->tn_lvalue;
   3064 
   3065 	if (op == POINT) {
   3066 		ln = build_address(sys, ln, true);
   3067 	} else if (ln->tn_type->t_tspec != PTR) {
   3068 		lint_assert(!allow_c90);
   3069 		lint_assert(is_integer(ln->tn_type->t_tspec));
   3070 		ln = convert(NOOP, 0, expr_derive_type(gettyp(VOID), PTR), ln);
   3071 	}
   3072 
   3073 	ctn = build_integer_constant(PTRDIFF_TSPEC,
   3074 	    rn->tn_sym->u.s_member.sm_offset_in_bits / CHAR_SIZE);
   3075 
   3076 	ntn = new_tnode(PLUS, sys, expr_derive_type(rn->tn_type, PTR),
   3077 	    ln, ctn);
   3078 	if (ln->tn_op == CON)
   3079 		ntn = fold(ntn);
   3080 
   3081 	if (rn->tn_type->t_bitfield) {
   3082 		ntn = new_tnode(FSEL, sys, ntn->tn_type->t_subt, ntn, NULL);
   3083 	} else {
   3084 		ntn = new_tnode(INDIR, sys, ntn->tn_type->t_subt, ntn, NULL);
   3085 	}
   3086 
   3087 	if (nolval)
   3088 		ntn->tn_lvalue = false;
   3089 
   3090 	return ntn;
   3091 }
   3092 
   3093 /*
   3094  * Create a node for INCAFT, INCBEF, DECAFT and DECBEF.
   3095  */
   3096 static tnode_t *
   3097 build_prepost_incdec(op_t op, bool sys, tnode_t *ln)
   3098 {
   3099 	tnode_t	*cn, *ntn;
   3100 
   3101 	lint_assert(ln != NULL);
   3102 
   3103 	if (ln->tn_type->t_tspec == PTR) {
   3104 		cn = subt_size_in_bytes(ln->tn_type);
   3105 	} else {
   3106 		cn = build_integer_constant(INT, (int64_t)1);
   3107 	}
   3108 	ntn = new_tnode(op, sys, ln->tn_type, ln, cn);
   3109 
   3110 	return ntn;
   3111 }
   3112 
   3113 /*
   3114  * Create a node for REAL, IMAG
   3115  */
   3116 static tnode_t *
   3117 build_real_imag(op_t op, bool sys, tnode_t *ln)
   3118 {
   3119 	tnode_t	*cn, *ntn;
   3120 
   3121 	lint_assert(ln != NULL);
   3122 
   3123 	if (ln->tn_op == NAME) {
   3124 		/*
   3125 		 * This may be too much, but it avoids wrong warnings.
   3126 		 * See d_c99_complex_split.c.
   3127 		 */
   3128 		mark_as_used(ln->tn_sym, false, false);
   3129 		mark_as_set(ln->tn_sym);
   3130 	}
   3131 
   3132 	switch (ln->tn_type->t_tspec) {
   3133 	case LCOMPLEX:
   3134 		/* XXX: integer and LDOUBLE don't match. */
   3135 		cn = build_integer_constant(LDOUBLE, (int64_t)1);
   3136 		break;
   3137 	case DCOMPLEX:
   3138 		/* XXX: integer and DOUBLE don't match. */
   3139 		cn = build_integer_constant(DOUBLE, (int64_t)1);
   3140 		break;
   3141 	case FCOMPLEX:
   3142 		/* XXX: integer and FLOAT don't match. */
   3143 		cn = build_integer_constant(FLOAT, (int64_t)1);
   3144 		break;
   3145 	default:
   3146 		/* '__%s__' is illegal for type '%s' */
   3147 		error(276, op == REAL ? "real" : "imag",
   3148 		    type_name(ln->tn_type));
   3149 		return NULL;
   3150 	}
   3151 	ntn = new_tnode(op, sys, cn->tn_type, ln, cn);
   3152 	ntn->tn_lvalue = true;
   3153 
   3154 	return ntn;
   3155 }
   3156 
   3157 /*
   3158  * Create a tree node for the unary & operator
   3159  */
   3160 static tnode_t *
   3161 build_address(bool sys, tnode_t *tn, bool noign)
   3162 {
   3163 	tspec_t	t;
   3164 
   3165 	if (!noign && ((t = tn->tn_type->t_tspec) == ARRAY || t == FUNC)) {
   3166 		if (!allow_c90)
   3167 			/* '&' before array or function: ignored */
   3168 			warning(127);
   3169 		return tn;
   3170 	}
   3171 
   3172 	/* eliminate &* */
   3173 	if (tn->tn_op == INDIR &&
   3174 	    tn->tn_left->tn_type->t_tspec == PTR &&
   3175 	    tn->tn_left->tn_type->t_subt == tn->tn_type) {
   3176 		return tn->tn_left;
   3177 	}
   3178 
   3179 	return new_tnode(ADDR, sys, expr_derive_type(tn->tn_type, PTR),
   3180 	    tn, NULL);
   3181 }
   3182 
   3183 /*
   3184  * Create a node for operators PLUS and MINUS.
   3185  */
   3186 static tnode_t *
   3187 build_plus_minus(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
   3188 {
   3189 
   3190 	/* If pointer and integer, then pointer to the lhs. */
   3191 	if (rn->tn_type->t_tspec == PTR && is_integer(ln->tn_type->t_tspec)) {
   3192 		tnode_t *tmp = ln;
   3193 		ln = rn;
   3194 		rn = tmp;
   3195 		/* pointer addition has integer on the left-hand side */
   3196 		query_message(5);
   3197 	}
   3198 
   3199 	/* pointer +- integer */
   3200 	if (ln->tn_type->t_tspec == PTR && rn->tn_type->t_tspec != PTR) {
   3201 		lint_assert(is_integer(rn->tn_type->t_tspec));
   3202 
   3203 		check_ctype_macro_invocation(ln, rn);
   3204 		check_enum_array_index(ln, rn);
   3205 
   3206 		tnode_t *elsz = subt_size_in_bytes(ln->tn_type);
   3207 		if (rn->tn_type->t_tspec != elsz->tn_type->t_tspec)
   3208 			rn = convert(NOOP, 0, elsz->tn_type, rn);
   3209 
   3210 		tnode_t *prod = new_tnode(MULT, sys, rn->tn_type, rn, elsz);
   3211 		if (rn->tn_op == CON)
   3212 			prod = fold(prod);
   3213 
   3214 		return new_tnode(op, sys, ln->tn_type, ln, prod);
   3215 	}
   3216 
   3217 	/* pointer - pointer */
   3218 	if (rn->tn_type->t_tspec == PTR) {
   3219 		lint_assert(ln->tn_type->t_tspec == PTR);
   3220 		lint_assert(op == MINUS);
   3221 
   3222 		type_t *ptrdiff = gettyp(PTRDIFF_TSPEC);
   3223 		tnode_t *raw_diff = new_tnode(op, sys, ptrdiff, ln, rn);
   3224 		if (ln->tn_op == CON && rn->tn_op == CON)
   3225 			raw_diff = fold(raw_diff);
   3226 
   3227 		tnode_t *elsz = subt_size_in_bytes(ln->tn_type);
   3228 		balance(NOOP, &raw_diff, &elsz);
   3229 
   3230 		return new_tnode(DIV, sys, ptrdiff, raw_diff, elsz);
   3231 	}
   3232 
   3233 	return new_tnode(op, sys, ln->tn_type, ln, rn);
   3234 }
   3235 
   3236 /*
   3237  * Create a node for operators SHL and SHR.
   3238  */
   3239 static tnode_t *
   3240 build_bit_shift(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
   3241 {
   3242 
   3243 	if (!allow_c90 && rn->tn_type->t_tspec != INT)
   3244 		rn = convert(NOOP, 0, gettyp(INT), rn);
   3245 	return new_tnode(op, sys, ln->tn_type, ln, rn);
   3246 }
   3247 
   3248 /* See C99 6.5.15 "Conditional operator". */
   3249 static tnode_t *
   3250 build_colon(bool sys, tnode_t *ln, tnode_t *rn)
   3251 {
   3252 	tspec_t	lt, rt;
   3253 	type_t	*tp;
   3254 
   3255 	lt = ln->tn_type->t_tspec;
   3256 	rt = rn->tn_type->t_tspec;
   3257 
   3258 	if (is_arithmetic(lt) && is_arithmetic(rt)) {
   3259 		/* The operands were already balanced in build_binary. */
   3260 		tp = ln->tn_type;
   3261 	} else if (lt == BOOL && rt == BOOL) {
   3262 		tp = ln->tn_type;
   3263 	} else if (lt == VOID || rt == VOID) {
   3264 		tp = gettyp(VOID);
   3265 	} else if (is_struct_or_union(lt)) {
   3266 		/* Both types must be identical. */
   3267 		lint_assert(is_struct_or_union(rt));
   3268 		lint_assert(ln->tn_type->t_str == rn->tn_type->t_str);
   3269 		if (is_incomplete(ln->tn_type)) {
   3270 			/* unknown operand size, op '%s' */
   3271 			error(138, op_name(COLON));
   3272 			return NULL;
   3273 		}
   3274 		tp = ln->tn_type;
   3275 	} else if (lt == PTR && is_integer(rt)) {
   3276 		if (rt != PTRDIFF_TSPEC)
   3277 			rn = convert(NOOP, 0, gettyp(PTRDIFF_TSPEC), rn);
   3278 		tp = ln->tn_type;
   3279 	} else if (rt == PTR && is_integer(lt)) {
   3280 		if (lt != PTRDIFF_TSPEC)
   3281 			ln = convert(NOOP, 0, gettyp(PTRDIFF_TSPEC), ln);
   3282 		tp = rn->tn_type;
   3283 	} else if (lt == PTR && is_null_pointer(rn)) {
   3284 		tp = merge_qualifiers(ln->tn_type, rn->tn_type);
   3285 	} else if (rt == PTR && is_null_pointer(ln)) {
   3286 		tp = merge_qualifiers(rn->tn_type, ln->tn_type);
   3287 	} else if (lt == PTR && ln->tn_type->t_subt->t_tspec == VOID) {
   3288 		tp = merge_qualifiers(ln->tn_type, rn->tn_type);
   3289 	} else if (rt == PTR && rn->tn_type->t_subt->t_tspec == VOID) {
   3290 		tp = merge_qualifiers(rn->tn_type, ln->tn_type);
   3291 	} else {
   3292 		/*
   3293 		 * XXX For now we simply take the left type. This is
   3294 		 * probably wrong, if one type contains a function prototype
   3295 		 * and the other one, at the same place, only an old-style
   3296 		 * declaration.
   3297 		 */
   3298 		tp = merge_qualifiers(ln->tn_type, rn->tn_type);
   3299 	}
   3300 
   3301 	return new_tnode(COLON, sys, tp, ln, rn);
   3302 }
   3303 
   3304 /* TODO: check for varargs */
   3305 static bool
   3306 is_cast_redundant(const tnode_t *tn)
   3307 {
   3308 	const type_t *ntp = tn->tn_type, *otp = tn->tn_left->tn_type;
   3309 	tspec_t nt = ntp->t_tspec, ot = otp->t_tspec;
   3310 
   3311 	if (nt == BOOL || ot == BOOL)
   3312 		return nt == BOOL && ot == BOOL;
   3313 
   3314 	if (is_integer(nt) && is_integer(ot)) {
   3315 		unsigned int nw = width_in_bits(ntp), ow = width_in_bits(otp);
   3316 		if (is_uinteger(nt) == is_uinteger(ot))
   3317 		       return nw >= ow;
   3318 		return is_uinteger(ot) && nw > ow;
   3319 	}
   3320 
   3321 	if (is_complex(nt) || is_complex(ot))
   3322 		return is_complex(nt) && is_complex(ot) &&
   3323 		       size_in_bits(nt) >= size_in_bits(ot);
   3324 
   3325 	if (is_floating(nt) && is_floating(ot))
   3326 		return size_in_bits(nt) >= size_in_bits(ot);
   3327 
   3328 	if (nt == PTR && ot == PTR) {
   3329 		if (!ntp->t_subt->t_const && otp->t_subt->t_const)
   3330 			return false;
   3331 		if (!ntp->t_subt->t_volatile && otp->t_subt->t_volatile)
   3332 			return false;
   3333 
   3334 		if (ntp->t_subt->t_tspec == VOID ||
   3335 		    otp->t_subt->t_tspec == VOID ||
   3336 		    types_compatible(ntp->t_subt, otp->t_subt,
   3337 			false, false, NULL))
   3338 			return true;
   3339 	}
   3340 
   3341 	return false;
   3342 }
   3343 
   3344 /*
   3345  * Create a node for an assignment operator (both = and op= ).
   3346  */
   3347 static tnode_t *
   3348 build_assignment(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
   3349 {
   3350 	tspec_t	lt, rt;
   3351 	tnode_t	*ntn, *ctn;
   3352 
   3353 	lint_assert(ln != NULL);
   3354 	lint_assert(rn != NULL);
   3355 
   3356 	lt = ln->tn_type->t_tspec;
   3357 	rt = rn->tn_type->t_tspec;
   3358 
   3359 	if ((op == ADDASS || op == SUBASS) && lt == PTR) {
   3360 		lint_assert(is_integer(rt));
   3361 		ctn = subt_size_in_bytes(ln->tn_type);
   3362 		if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
   3363 			rn = convert(NOOP, 0, ctn->tn_type, rn);
   3364 		rn = new_tnode(MULT, sys, rn->tn_type, rn, ctn);
   3365 		if (rn->tn_left->tn_op == CON)
   3366 			rn = fold(rn);
   3367 	}
   3368 
   3369 	if ((op == ASSIGN || op == RETURN || op == INIT) &&
   3370 	    (lt == STRUCT || rt == STRUCT)) {
   3371 		lint_assert(lt == rt);
   3372 		lint_assert(ln->tn_type->t_str == rn->tn_type->t_str);
   3373 		if (is_incomplete(ln->tn_type)) {
   3374 			if (op == RETURN) {
   3375 				/* cannot return incomplete type */
   3376 				error(212);
   3377 			} else {
   3378 				/* unknown operand size, op '%s' */
   3379 				error(138, op_name(op));
   3380 			}
   3381 			return NULL;
   3382 		}
   3383 	}
   3384 
   3385 	if (op == SHLASS) {
   3386 		if (portable_size_in_bits(lt) < portable_size_in_bits(rt)) {
   3387 			if (hflag)
   3388 				/* semantics of '%s' change in ANSI C; ... */
   3389 				warning(118, "<<=");
   3390 		}
   3391 	} else if (op != SHRASS) {
   3392 		if (op == ASSIGN || lt != PTR) {
   3393 			if (lt != rt ||
   3394 			    (ln->tn_type->t_bitfield && rn->tn_op == CON)) {
   3395 				rn = convert(op, 0, ln->tn_type, rn);
   3396 				rt = lt;
   3397 			}
   3398 		}
   3399 	}
   3400 
   3401 	if (any_query_enabled && rn->tn_op == CVT && rn->tn_cast &&
   3402 	    types_compatible(ln->tn_type, rn->tn_type, false, false, NULL) &&
   3403 	    is_cast_redundant(rn)) {
   3404 		/* redundant cast from '%s' to '%s' before assignment */
   3405 		query_message(7,
   3406 		    type_name(rn->tn_left->tn_type), type_name(rn->tn_type));
   3407 	}
   3408 
   3409 	ntn = new_tnode(op, sys, ln->tn_type, ln, rn);
   3410 
   3411 	return ntn;
   3412 }
   3413 
   3414 /*
   3415  * Get the size in bytes of type tp->t_subt, as a constant expression of type
   3416  * ptrdiff_t as seen from the target platform.
   3417  */
   3418 static tnode_t *
   3419 subt_size_in_bytes(type_t *tp)
   3420 {
   3421 	int elem, elsz_in_bits;
   3422 
   3423 	lint_assert(tp->t_tspec == PTR);
   3424 	tp = tp->t_subt;
   3425 
   3426 	elem = 1;
   3427 	elsz_in_bits = 0;
   3428 
   3429 	while (tp->t_tspec == ARRAY) {
   3430 		elem *= tp->t_dim;
   3431 		tp = tp->t_subt;
   3432 	}
   3433 
   3434 	switch (tp->t_tspec) {
   3435 	case FUNC:
   3436 		/* pointer to function is not allowed here */
   3437 		error(110);
   3438 		break;
   3439 	case VOID:
   3440 		/* cannot do pointer arithmetic on operand of unknown size */
   3441 		gnuism(136);
   3442 		break;
   3443 	case STRUCT:
   3444 	case UNION:
   3445 		if ((elsz_in_bits = tp->t_str->sou_size_in_bits) == 0)
   3446 			/* cannot do pointer arithmetic on operand of ... */
   3447 			error(136);
   3448 		break;
   3449 	case ENUM:
   3450 		if (is_incomplete(tp)) {
   3451 			/* cannot do pointer arithmetic on operand of ... */
   3452 			warning(136);
   3453 		}
   3454 		/* FALLTHROUGH */
   3455 	default:
   3456 		if ((elsz_in_bits = size_in_bits(tp->t_tspec)) == 0) {
   3457 			/* cannot do pointer arithmetic on operand of ... */
   3458 			error(136);
   3459 		} else {
   3460 			lint_assert(elsz_in_bits != -1);
   3461 		}
   3462 		break;
   3463 	}
   3464 
   3465 	if (elem == 0 && elsz_in_bits != 0) {
   3466 		/* cannot do pointer arithmetic on operand of unknown size */
   3467 		error(136);
   3468 	}
   3469 
   3470 	if (elsz_in_bits == 0)
   3471 		elsz_in_bits = CHAR_SIZE;
   3472 
   3473 	return build_integer_constant(PTRDIFF_TSPEC,
   3474 	    (int64_t)(elem * elsz_in_bits / CHAR_SIZE));
   3475 }
   3476 
   3477 /*
   3478  * XXX
   3479  * Note: There appear to be a number of bugs in detecting overflow in
   3480  * this function. An audit and a set of proper regression tests are needed.
   3481  *     --Perry Metzger, Nov. 16, 2001
   3482  */
   3483 /*
   3484  * Do only as much as necessary to compute constant expressions.
   3485  * Called only if the operator allows folding and all operands are constants.
   3486  */
   3487 static tnode_t *
   3488 fold(tnode_t *tn)
   3489 {
   3490 	val_t *v;
   3491 	tspec_t t;
   3492 	bool utyp, ovfl;
   3493 	int64_t sl, sr = 0, q = 0, mask;
   3494 	uint64_t ul, ur = 0;
   3495 	tnode_t *cn;
   3496 
   3497 	v = xcalloc(1, sizeof(*v));
   3498 	v->v_tspec = tn->tn_type->t_tspec;
   3499 
   3500 	t = tn->tn_left->tn_type->t_tspec;
   3501 	utyp = !is_integer(t) || is_uinteger(t);
   3502 	ul = sl = tn->tn_left->tn_val->v_quad;
   3503 	if (is_binary(tn))
   3504 		ur = sr = tn->tn_right->tn_val->v_quad;
   3505 
   3506 	mask = value_bits(size_in_bits(t));
   3507 	ovfl = false;
   3508 
   3509 	switch (tn->tn_op) {
   3510 	case UPLUS:
   3511 		q = sl;
   3512 		break;
   3513 	case UMINUS:
   3514 		q = sl == INT64_MIN ? sl : -sl;
   3515 		if (sl != 0 && msb(q, t) == msb(sl, t))
   3516 			ovfl = true;
   3517 		break;
   3518 	case COMPL:
   3519 		q = ~sl;
   3520 		break;
   3521 	case MULT:
   3522 		if (utyp) {
   3523 			q = ul * ur;
   3524 			if (q != (q & mask))
   3525 				ovfl = true;
   3526 			else if ((ul != 0) && ((q / ul) != ur))
   3527 				ovfl = true;
   3528 		} else {
   3529 			q = sl * sr;
   3530 			if (msb(q, t) != (msb(sl, t) ^ msb(sr, t)))
   3531 				ovfl = true;
   3532 		}
   3533 		break;
   3534 	case DIV:
   3535 		if (sr == 0) {
   3536 			/* division by 0 */
   3537 			error(139);
   3538 			q = utyp ? -1 : INT64_MAX;
   3539 		} else {
   3540 			q = utyp ? (int64_t)(ul / ur) : sl / sr;
   3541 		}
   3542 		break;
   3543 	case MOD:
   3544 		if (sr == 0) {
   3545 			/* modulus by 0 */
   3546 			error(140);
   3547 			q = 0;
   3548 		} else {
   3549 			q = utyp ? (int64_t)(ul % ur) : sl % sr;
   3550 		}
   3551 		break;
   3552 	case PLUS:
   3553 		q = utyp ? (int64_t)(ul + ur) : sl + sr;
   3554 		if (msb(sl, t) && msb(sr, t) && !msb(q, t))
   3555 			ovfl = true;
   3556 		if (!utyp && !msb(sl, t) && !msb(sr, t) && msb(q, t))
   3557 			ovfl = true;
   3558 		break;
   3559 	case MINUS:
   3560 		q = utyp ? (int64_t)(ul - ur) : sl - sr;
   3561 		if (!utyp && msb(sl, t) && !msb(sr, t) && !msb(q, t))
   3562 			ovfl = true;
   3563 		if (!msb(sl, t) && msb(sr, t) && msb(q, t))
   3564 			ovfl = true;
   3565 		break;
   3566 	case SHL:
   3567 		/* TODO: warn about out-of-bounds 'sr'. */
   3568 		/* TODO: warn about overflow in signed '<<'. */
   3569 		q = utyp ? (int64_t)(ul << (sr & 63)) : sl << (sr & 63);
   3570 		break;
   3571 	case SHR:
   3572 		/*
   3573 		 * The sign must be explicitly extended because
   3574 		 * shifts of signed values are implementation dependent.
   3575 		 */
   3576 		/* TODO: warn about out-of-bounds 'sr'. */
   3577 		q = ul >> (sr & 63);
   3578 		q = convert_integer(q, t, size_in_bits(t) - (int)sr);
   3579 		break;
   3580 	case LT:
   3581 		q = (utyp ? ul < ur : sl < sr) ? 1 : 0;
   3582 		break;
   3583 	case LE:
   3584 		q = (utyp ? ul <= ur : sl <= sr) ? 1 : 0;
   3585 		break;
   3586 	case GE:
   3587 		q = (utyp ? ul >= ur : sl >= sr) ? 1 : 0;
   3588 		break;
   3589 	case GT:
   3590 		q = (utyp ? ul > ur : sl > sr) ? 1 : 0;
   3591 		break;
   3592 	case EQ:
   3593 		q = (utyp ? ul == ur : sl == sr) ? 1 : 0;
   3594 		break;
   3595 	case NE:
   3596 		q = (utyp ? ul != ur : sl != sr) ? 1 : 0;
   3597 		break;
   3598 	case BITAND:
   3599 		q = utyp ? (int64_t)(ul & ur) : sl & sr;
   3600 		break;
   3601 	case BITXOR:
   3602 		q = utyp ? (int64_t)(ul ^ ur) : sl ^ sr;
   3603 		break;
   3604 	case BITOR:
   3605 		q = utyp ? (int64_t)(ul | ur) : sl | sr;
   3606 		break;
   3607 	default:
   3608 		lint_assert(/*CONSTCOND*/false);
   3609 	}
   3610 
   3611 	/* XXX does not work for quads. */
   3612 	if (ovfl ||
   3613 	    ((uint64_t)(q | mask) != ~(uint64_t)0 && (q & ~mask) != 0)) {
   3614 		if (hflag)
   3615 			/* integer overflow detected, op '%s' */
   3616 			warning(141, op_name(tn->tn_op));
   3617 	}
   3618 
   3619 	v->v_quad = convert_integer(q, t, 0);
   3620 
   3621 	cn = build_constant(tn->tn_type, v);
   3622 	if (tn->tn_left->tn_system_dependent)
   3623 		cn->tn_system_dependent = true;
   3624 	if (is_binary(tn) && tn->tn_right->tn_system_dependent)
   3625 		cn->tn_system_dependent = true;
   3626 
   3627 	return cn;
   3628 }
   3629 
   3630 /*
   3631  * Fold constant nodes, as much as is needed for comparing the value with 0.
   3632  */
   3633 static tnode_t *
   3634 fold_bool(tnode_t *tn)
   3635 {
   3636 	bool	l, r;
   3637 	val_t	*v;
   3638 
   3639 	v = xcalloc(1, sizeof(*v));
   3640 	v->v_tspec = tn->tn_type->t_tspec;
   3641 	lint_assert(v->v_tspec == INT || (Tflag && v->v_tspec == BOOL));
   3642 
   3643 	l = constant_is_nonzero(tn->tn_left);
   3644 	r = is_binary(tn) && constant_is_nonzero(tn->tn_right);
   3645 
   3646 	switch (tn->tn_op) {
   3647 	case NOT:
   3648 		if (hflag && !constcond_flag)
   3649 			/* constant argument to '!' */
   3650 			warning(239);
   3651 		v->v_quad = !l ? 1 : 0;
   3652 		break;
   3653 	case LOGAND:
   3654 		v->v_quad = l && r ? 1 : 0;
   3655 		break;
   3656 	case LOGOR:
   3657 		v->v_quad = l || r ? 1 : 0;
   3658 		break;
   3659 	default:
   3660 		lint_assert(/*CONSTCOND*/false);
   3661 	}
   3662 
   3663 	return build_constant(tn->tn_type, v);
   3664 }
   3665 
   3666 static ldbl_t
   3667 floating_error_value(tspec_t t, ldbl_t lv)
   3668 {
   3669 	if (t == FLOAT) {
   3670 		return lv < 0 ? -FLT_MAX : FLT_MAX;
   3671 	} else if (t == DOUBLE) {
   3672 		return lv < 0 ? -DBL_MAX : DBL_MAX;
   3673 	} else {
   3674 		/* LINTED 248: floating-point constant out of range */
   3675 		ldbl_t max = LDBL_MAX;
   3676 		return lv < 0 ? -max : max;
   3677 	}
   3678 }
   3679 
   3680 /*
   3681  * Fold constant nodes having operands with floating point type.
   3682  */
   3683 static tnode_t *
   3684 fold_float(tnode_t *tn)
   3685 {
   3686 	val_t	*v;
   3687 	tspec_t	t;
   3688 	ldbl_t	lv, rv = 0;
   3689 
   3690 	fpe = 0;
   3691 	v = xcalloc(1, sizeof(*v));
   3692 	v->v_tspec = t = tn->tn_type->t_tspec;
   3693 
   3694 	lint_assert(is_floating(t));
   3695 	lint_assert(t == tn->tn_left->tn_type->t_tspec);
   3696 	lint_assert(!is_binary(tn) || t == tn->tn_right->tn_type->t_tspec);
   3697 
   3698 	lv = tn->tn_left->tn_val->v_ldbl;
   3699 	if (is_binary(tn))
   3700 		rv = tn->tn_right->tn_val->v_ldbl;
   3701 
   3702 	switch (tn->tn_op) {
   3703 	case UPLUS:
   3704 		v->v_ldbl = lv;
   3705 		break;
   3706 	case UMINUS:
   3707 		v->v_ldbl = -lv;
   3708 		break;
   3709 	case MULT:
   3710 		v->v_ldbl = lv * rv;
   3711 		break;
   3712 	case DIV:
   3713 		if (rv == 0.0) {
   3714 			/* division by 0 */
   3715 			error(139);
   3716 			v->v_ldbl = floating_error_value(t, lv);
   3717 		} else {
   3718 			v->v_ldbl = lv / rv;
   3719 		}
   3720 		break;
   3721 	case PLUS:
   3722 		v->v_ldbl = lv + rv;
   3723 		break;
   3724 	case MINUS:
   3725 		v->v_ldbl = lv - rv;
   3726 		break;
   3727 	case LT:
   3728 		v->v_quad = lv < rv ? 1 : 0;
   3729 		break;
   3730 	case LE:
   3731 		v->v_quad = lv <= rv ? 1 : 0;
   3732 		break;
   3733 	case GE:
   3734 		v->v_quad = lv >= rv ? 1 : 0;
   3735 		break;
   3736 	case GT:
   3737 		v->v_quad = lv > rv ? 1 : 0;
   3738 		break;
   3739 	case EQ:
   3740 		v->v_quad = lv == rv ? 1 : 0;
   3741 		break;
   3742 	case NE:
   3743 		v->v_quad = lv != rv ? 1 : 0;
   3744 		break;
   3745 	default:
   3746 		lint_assert(/*CONSTCOND*/false);
   3747 	}
   3748 
   3749 	lint_assert(fpe != 0 || isnan((double)v->v_ldbl) == 0);
   3750 	if (fpe != 0 || isfinite((double)v->v_ldbl) == 0 ||
   3751 	    (t == FLOAT &&
   3752 	     (v->v_ldbl > FLT_MAX || v->v_ldbl < -FLT_MAX)) ||
   3753 	    (t == DOUBLE &&
   3754 	     (v->v_ldbl > DBL_MAX || v->v_ldbl < -DBL_MAX))) {
   3755 		/* floating point overflow on operator '%s' */
   3756 		warning(142, op_name(tn->tn_op));
   3757 		v->v_ldbl = floating_error_value(t, v->v_ldbl);
   3758 		fpe = 0;
   3759 	}
   3760 
   3761 	return build_constant(tn->tn_type, v);
   3762 }
   3763 
   3764 
   3765 /*
   3766  * Create a constant node for sizeof.
   3767  */
   3768 tnode_t *
   3769 build_sizeof(const type_t *tp)
   3770 {
   3771 	unsigned int size_in_bytes = type_size_in_bits(tp) / CHAR_SIZE;
   3772 	tnode_t *tn = build_integer_constant(SIZEOF_TSPEC, size_in_bytes);
   3773 	tn->tn_system_dependent = true;
   3774 	debug_step("build_sizeof '%s' = %u", type_name(tp), size_in_bytes);
   3775 	return tn;
   3776 }
   3777 
   3778 /*
   3779  * Create a constant node for offsetof.
   3780  */
   3781 /* ARGSUSED */ /* See implementation comments. */
   3782 tnode_t *
   3783 build_offsetof(const type_t *tp, const sym_t *sym)
   3784 {
   3785 	unsigned int offset_in_bytes;
   3786 	tnode_t *tn;
   3787 
   3788 	if (!is_struct_or_union(tp->t_tspec))
   3789 		/* unacceptable operand of '%s' */
   3790 		error(111, "offsetof");
   3791 
   3792 	/* XXX: wrong size, no checking for sym fixme */
   3793 	offset_in_bytes = type_size_in_bits(tp) / CHAR_SIZE;
   3794 	tn = build_integer_constant(SIZEOF_TSPEC, offset_in_bytes);
   3795 	tn->tn_system_dependent = true;
   3796 	return tn;
   3797 }
   3798 
   3799 unsigned int
   3800 type_size_in_bits(const type_t *tp)
   3801 {
   3802 	unsigned int elem, elsz;
   3803 	bool	flex;
   3804 
   3805 	elem = 1;
   3806 	flex = false;
   3807 	lint_assert(tp != NULL);
   3808 	while (tp->t_tspec == ARRAY) {
   3809 		flex = true;	/* allow c99 flex arrays [] [0] */
   3810 		elem *= tp->t_dim;
   3811 		tp = tp->t_subt;
   3812 	}
   3813 	if (elem == 0) {
   3814 		if (!flex) {
   3815 			/* cannot take size/alignment of incomplete type */
   3816 			error(143);
   3817 			elem = 1;
   3818 		}
   3819 	}
   3820 	switch (tp->t_tspec) {
   3821 	case FUNC:
   3822 		/* cannot take size/alignment of function type '%s' */
   3823 		error(144, type_name(tp));
   3824 		elsz = 1;
   3825 		break;
   3826 	case STRUCT:
   3827 	case UNION:
   3828 		if (is_incomplete(tp)) {
   3829 			/* cannot take size/alignment of incomplete type */
   3830 			error(143);
   3831 			elsz = 1;
   3832 		} else {
   3833 			elsz = tp->t_str->sou_size_in_bits;
   3834 		}
   3835 		break;
   3836 	case ENUM:
   3837 		if (is_incomplete(tp)) {
   3838 			/* cannot take size/alignment of incomplete type */
   3839 			warning(143);
   3840 		}
   3841 		/* FALLTHROUGH */
   3842 	default:
   3843 		if (tp->t_bitfield) {
   3844 			/* cannot take size/alignment of bit-field */
   3845 			error(145);
   3846 		}
   3847 		if (tp->t_tspec == VOID) {
   3848 			/* cannot take size/alignment of void */
   3849 			error(146);
   3850 			elsz = 1;
   3851 		} else {
   3852 			elsz = size_in_bits(tp->t_tspec);
   3853 			lint_assert(elsz > 0);
   3854 		}
   3855 		break;
   3856 	}
   3857 
   3858 	return elem * elsz;
   3859 }
   3860 
   3861 tnode_t *
   3862 build_alignof(const type_t *tp)
   3863 {
   3864 	switch (tp->t_tspec) {
   3865 	case ARRAY:
   3866 		break;
   3867 
   3868 	case FUNC:
   3869 		/* cannot take size/alignment of function type '%s' */
   3870 		error(144, type_name(tp));
   3871 		return 0;
   3872 
   3873 	case STRUCT:
   3874 	case UNION:
   3875 		if (is_incomplete(tp)) {
   3876 			/* cannot take size/alignment of incomplete type */
   3877 			error(143);
   3878 			return 0;
   3879 		}
   3880 		break;
   3881 	case ENUM:
   3882 		break;
   3883 	default:
   3884 		if (tp->t_bitfield) {
   3885 			/* cannot take size/alignment of bit-field */
   3886 			error(145);
   3887 			return 0;
   3888 		}
   3889 		if (tp->t_tspec == VOID) {
   3890 			/* cannot take size/alignment of void */
   3891 			error(146);
   3892 			return 0;
   3893 		}
   3894 		break;
   3895 	}
   3896 
   3897 	return build_integer_constant(SIZEOF_TSPEC,
   3898 	    (int64_t)alignment_in_bits(tp) / CHAR_SIZE);
   3899 }
   3900 
   3901 /*
   3902  * Type casts.
   3903  */
   3904 tnode_t *
   3905 cast(tnode_t *tn, type_t *tp)
   3906 {
   3907 	tspec_t	nt, ot;
   3908 
   3909 	if (tn == NULL)
   3910 		return NULL;
   3911 
   3912 	tn = cconv(tn);
   3913 
   3914 	lint_assert(tp != NULL);
   3915 	nt = tp->t_tspec;
   3916 	ot = tn->tn_type->t_tspec;
   3917 
   3918 	if (nt == VOID) {
   3919 		/*
   3920 		 * C90 6.3.4, C99 6.5.4p2 and C11 6.5.4p2 allow any type to
   3921 		 * be cast to void.  The only other allowed casts are from a
   3922 		 * scalar type to a scalar type.
   3923 		 */
   3924 	} else if (nt == UNION) {
   3925 		sym_t *m;
   3926 		struct_or_union *str = tp->t_str;
   3927 		if (!allow_gcc) {
   3928 			/* union cast is a GCC extension */
   3929 			error(328);
   3930 			return NULL;
   3931 		}
   3932 		for (m = str->sou_first_member; m != NULL; m = m->s_next) {
   3933 			if (types_compatible(m->s_type, tn->tn_type,
   3934 			    false, false, NULL)) {
   3935 				tn = expr_alloc_tnode();
   3936 				tn->tn_op = CVT;
   3937 				tn->tn_type = tp;
   3938 				tn->tn_cast = true;
   3939 				tn->tn_right = NULL;
   3940 				return tn;
   3941 			}
   3942 		}
   3943 		/* type '%s' is not a member of '%s' */
   3944 		error(329, type_name(tn->tn_type), type_name(tp));
   3945 		return NULL;
   3946 	} else if (nt == STRUCT || nt == ARRAY || nt == FUNC) {
   3947 		/* Casting to a struct is an undocumented GCC extension. */
   3948 		if (!(allow_gcc && nt == STRUCT))
   3949 			goto invalid_cast;
   3950 	} else if (is_struct_or_union(ot)) {
   3951 		goto invalid_cast;
   3952 	} else if (ot == VOID) {
   3953 		/* improper cast of void expression */
   3954 		error(148);
   3955 		return NULL;
   3956 	} else if (is_integer(nt) && is_scalar(ot)) {
   3957 		/* ok */
   3958 	} else if (is_floating(nt) && is_arithmetic(ot)) {
   3959 		/* ok */
   3960 	} else if (nt == PTR && is_integer(ot)) {
   3961 		/* ok */
   3962 	} else if (nt == PTR && ot == PTR) {
   3963 		if (!tp->t_subt->t_const && tn->tn_type->t_subt->t_const) {
   3964 			if (hflag)
   3965 				/* cast discards 'const' from type '%s' */
   3966 				warning(275, type_name(tn->tn_type));
   3967 		}
   3968 	} else
   3969 		goto invalid_cast;
   3970 
   3971 	if (any_query_enabled && types_compatible(tp, tn->tn_type,
   3972 	    false, false, NULL)) {
   3973 		/* no-op cast from '%s' to '%s' */
   3974 		query_message(6, type_name(tn->tn_type), type_name(tp));
   3975 	}
   3976 
   3977 	tn = convert(CVT, 0, tp, tn);
   3978 	tn->tn_cast = true;
   3979 
   3980 	return tn;
   3981 
   3982 invalid_cast:
   3983 	/* invalid cast from '%s' to '%s' */
   3984 	error(147, type_name(tn->tn_type), type_name(tp));
   3985 	return NULL;
   3986 }
   3987 
   3988 /*
   3989  * Create the node for a function argument.
   3990  * All necessary conversions and type checks are done in
   3991  * build_function_call because build_function_argument has no
   3992  * information about expected argument types.
   3993  */
   3994 tnode_t *
   3995 build_function_argument(tnode_t *args, tnode_t *arg)
   3996 {
   3997 	/*
   3998 	 * If there was a serious error in the expression for the argument,
   3999 	 * create a dummy argument so the positions of the remaining arguments
   4000 	 * will not change.
   4001 	 */
   4002 	if (arg == NULL)
   4003 		arg = build_integer_constant(INT, 0);
   4004 
   4005 	return new_tnode(PUSH, arg->tn_sys, arg->tn_type, arg, args);
   4006 }
   4007 
   4008 /*
   4009  * Create the node for a function call. Also check types of
   4010  * function arguments and insert conversions, if necessary.
   4011  */
   4012 tnode_t *
   4013 build_function_call(tnode_t *func, bool sys, tnode_t *args)
   4014 {
   4015 	tnode_t	*ntn;
   4016 	op_t	fcop;
   4017 
   4018 	if (func == NULL)
   4019 		return NULL;
   4020 
   4021 	if (func->tn_op == NAME && func->tn_type->t_tspec == FUNC) {
   4022 		fcop = CALL;
   4023 	} else {
   4024 		fcop = ICALL;
   4025 	}
   4026 
   4027 	check_ctype_function_call(func, args);
   4028 
   4029 	/*
   4030 	 * after cconv() func will always be a pointer to a function
   4031 	 * if it is a valid function designator.
   4032 	 */
   4033 	func = cconv(func);
   4034 
   4035 	if (func->tn_type->t_tspec != PTR ||
   4036 	    func->tn_type->t_subt->t_tspec != FUNC) {
   4037 		/* cannot call '%s', must be a function */
   4038 		error(149, type_name(func->tn_type));
   4039 		return NULL;
   4040 	}
   4041 
   4042 	args = check_function_arguments(func->tn_type->t_subt, args);
   4043 
   4044 	ntn = new_tnode(fcop, sys, func->tn_type->t_subt->t_subt, func, args);
   4045 
   4046 	return ntn;
   4047 }
   4048 
   4049 /*
   4050  * Check types of all function arguments and insert conversions,
   4051  * if necessary.
   4052  */
   4053 static tnode_t *
   4054 check_function_arguments(type_t *ftp, tnode_t *args)
   4055 {
   4056 	tnode_t	*arg;
   4057 	sym_t	*asym;
   4058 	tspec_t	at;
   4059 	int	narg, npar, n, i;
   4060 
   4061 	/* get # of args in the prototype */
   4062 	npar = 0;
   4063 	for (asym = ftp->t_args; asym != NULL; asym = asym->s_next)
   4064 		npar++;
   4065 
   4066 	/* get # of args in function call */
   4067 	narg = 0;
   4068 	for (arg = args; arg != NULL; arg = arg->tn_right)
   4069 		narg++;
   4070 
   4071 	asym = ftp->t_args;
   4072 	if (ftp->t_proto && npar != narg && !(ftp->t_vararg && npar < narg)) {
   4073 		/* argument mismatch: %d %s passed, %d expected */
   4074 		error(150, narg, narg > 1 ? "arguments" : "argument", npar);
   4075 		asym = NULL;
   4076 	}
   4077 
   4078 	for (n = 1; n <= narg; n++) {
   4079 
   4080 		/*
   4081 		 * The rightmost argument is at the top of the argument
   4082 		 * subtree.
   4083 		 */
   4084 		for (i = narg, arg = args; i > n; i--, arg = arg->tn_right)
   4085 			continue;
   4086 
   4087 		/* some things which are always not allowed */
   4088 		if ((at = arg->tn_left->tn_type->t_tspec) == VOID) {
   4089 			/* void expressions may not be arguments, arg #%d */
   4090 			error(151, n);
   4091 			return NULL;
   4092 		} else if (is_struct_or_union(at) &&
   4093 			   is_incomplete(arg->tn_left->tn_type)) {
   4094 			/* argument cannot have unknown size, arg #%d */
   4095 			error(152, n);
   4096 			return NULL;
   4097 		} else if (is_integer(at) &&
   4098 			   arg->tn_left->tn_type->t_is_enum &&
   4099 			   is_incomplete(arg->tn_left->tn_type)) {
   4100 			/* argument cannot have unknown size, arg #%d */
   4101 			warning(152, n);
   4102 		}
   4103 
   4104 		/* class conversions (arg in value context) */
   4105 		arg->tn_left = cconv(arg->tn_left);
   4106 
   4107 		if (asym != NULL) {
   4108 			arg->tn_left = check_prototype_argument(
   4109 			    n, asym->s_type, arg->tn_left);
   4110 		} else {
   4111 			arg->tn_left = promote(NOOP, true, arg->tn_left);
   4112 		}
   4113 		arg->tn_type = arg->tn_left->tn_type;
   4114 
   4115 		if (asym != NULL)
   4116 			asym = asym->s_next;
   4117 	}
   4118 
   4119 	return args;
   4120 }
   4121 
   4122 /*
   4123  * Compare the type of an argument with the corresponding type of a
   4124  * prototype parameter. If it is a valid combination, but both types
   4125  * are not the same, insert a conversion to convert the argument into
   4126  * the type of the parameter.
   4127  */
   4128 static tnode_t *
   4129 check_prototype_argument(
   4130 	int	n,		/* pos of arg */
   4131 	type_t	*tp,		/* expected type (from prototype) */
   4132 	tnode_t	*tn)		/* argument */
   4133 {
   4134 	tnode_t	*ln;
   4135 	bool	dowarn;
   4136 
   4137 	ln = xcalloc(1, sizeof(*ln));
   4138 	ln->tn_type = expr_unqualified_type(tp);
   4139 	ln->tn_lvalue = true;
   4140 	if (typeok(FARG, n, ln, tn)) {
   4141 		if (!types_compatible(tp, tn->tn_type,
   4142 		    true, false, (dowarn = false, &dowarn)) || dowarn)
   4143 			tn = convert(FARG, n, tp, tn);
   4144 	}
   4145 	free(ln);
   4146 	return tn;
   4147 }
   4148 
   4149 /*
   4150  * Return the value of an integral constant expression.
   4151  * If the expression is not constant or its type is not an integer
   4152  * type, an error message is printed.
   4153  */
   4154 val_t *
   4155 constant(tnode_t *tn, bool required)
   4156 {
   4157 	val_t	*v;
   4158 
   4159 	if (tn != NULL)
   4160 		tn = cconv(tn);
   4161 	if (tn != NULL)
   4162 		tn = promote(NOOP, false, tn);
   4163 
   4164 	v = xcalloc(1, sizeof(*v));
   4165 
   4166 	if (tn == NULL) {
   4167 		lint_assert(nerr != 0);
   4168 		debug_step("constant node is null; returning 1 instead");
   4169 		v->v_tspec = INT;
   4170 		v->v_quad = 1;
   4171 		return v;
   4172 	}
   4173 
   4174 	v->v_tspec = tn->tn_type->t_tspec;
   4175 
   4176 	if (tn->tn_op == CON) {
   4177 		lint_assert(tn->tn_type->t_tspec == tn->tn_val->v_tspec);
   4178 		if (is_integer(tn->tn_val->v_tspec)) {
   4179 			v->v_unsigned_since_c90 =
   4180 			    tn->tn_val->v_unsigned_since_c90;
   4181 			v->v_quad = tn->tn_val->v_quad;
   4182 			return v;
   4183 		}
   4184 		v->v_quad = tn->tn_val->v_ldbl;
   4185 	} else {
   4186 		v->v_quad = 1;
   4187 	}
   4188 
   4189 	if (required)
   4190 		/* integral constant expression expected */
   4191 		error(55);
   4192 	else
   4193 		/* variable array dimension is a C99/GCC extension */
   4194 		c99ism(318);
   4195 
   4196 	if (!is_integer(v->v_tspec))
   4197 		v->v_tspec = INT;
   4198 
   4199 	return v;
   4200 }
   4201 
   4202 static bool
   4203 is_constcond_false(const tnode_t *tn, tspec_t t)
   4204 {
   4205 	return (t == BOOL || t == INT) &&
   4206 	       tn->tn_op == CON && tn->tn_val->v_quad == 0;
   4207 }
   4208 
   4209 /*
   4210  * Perform some tests on expressions which can't be done in build_binary()
   4211  * and functions called by build_binary(). These tests must be done here
   4212  * because we need some information about the context in which the operations
   4213  * are performed.
   4214  * After all tests are performed and dofreeblk is true, expr() frees the
   4215  * memory which is used for the expression.
   4216  */
   4217 void
   4218 expr(tnode_t *tn, bool vctx, bool cond, bool dofreeblk, bool is_do_while)
   4219 {
   4220 
   4221 	if (tn == NULL) {	/* in case of errors */
   4222 		expr_free_all();
   4223 		return;
   4224 	}
   4225 
   4226 	/* expr() is also called in global initializations */
   4227 	if (dcs->d_kind != DK_EXTERN && !is_do_while)
   4228 		check_statement_reachable();
   4229 
   4230 	check_expr_misc(tn, vctx, cond, !cond, false, false, false);
   4231 	if (tn->tn_op == ASSIGN) {
   4232 		if (hflag && cond)
   4233 			/* assignment in conditional context */
   4234 			warning(159);
   4235 	} else if (tn->tn_op == CON) {
   4236 		if (hflag && cond && !constcond_flag &&
   4237 		    !tn->tn_system_dependent &&
   4238 		    !(is_do_while &&
   4239 		      is_constcond_false(tn, tn->tn_type->t_tspec)))
   4240 			/* constant in conditional context */
   4241 			warning(161);
   4242 	}
   4243 	if (!modtab[tn->tn_op].m_has_side_effect) {
   4244 		/*
   4245 		 * for left operands of COMMA this warning is already
   4246 		 * printed
   4247 		 */
   4248 		if (tn->tn_op != COMMA && !vctx && !cond)
   4249 			check_null_effect(tn);
   4250 	}
   4251 	debug_node(tn);
   4252 
   4253 	/* free the tree memory */
   4254 	if (dofreeblk)
   4255 		expr_free_all();
   4256 }
   4257 
   4258 static bool
   4259 has_side_effect(const tnode_t *tn) /* NOLINT(misc-no-recursion) */
   4260 {
   4261 	op_t op = tn->tn_op;
   4262 
   4263 	if (modtab[op].m_has_side_effect)
   4264 		return true;
   4265 
   4266 	if (op == CVT && tn->tn_type->t_tspec == VOID)
   4267 		return has_side_effect(tn->tn_left);
   4268 
   4269 	/* XXX: Why not has_side_effect(tn->tn_left) as well? */
   4270 	if (op == LOGAND || op == LOGOR)
   4271 		return has_side_effect(tn->tn_right);
   4272 
   4273 	/* XXX: Why not has_side_effect(tn->tn_left) as well? */
   4274 	if (op == QUEST)
   4275 		return has_side_effect(tn->tn_right);
   4276 
   4277 	if (op == COLON || op == COMMA) {
   4278 		return has_side_effect(tn->tn_left) ||
   4279 		       has_side_effect(tn->tn_right);
   4280 	}
   4281 
   4282 	return false;
   4283 }
   4284 
   4285 static bool
   4286 is_void_cast(const tnode_t *tn)
   4287 {
   4288 
   4289 	return tn->tn_op == CVT && tn->tn_cast &&
   4290 	       tn->tn_type->t_tspec == VOID;
   4291 }
   4292 
   4293 static bool
   4294 is_local_symbol(const tnode_t *tn)
   4295 {
   4296 
   4297 	return tn->tn_op == LOAD &&
   4298 	       tn->tn_left->tn_op == NAME &&
   4299 	       tn->tn_left->tn_sym->s_scl == AUTO;
   4300 }
   4301 
   4302 static bool
   4303 is_int_constant_zero(const tnode_t *tn)
   4304 {
   4305 
   4306 	return tn->tn_op == CON &&
   4307 	       tn->tn_type->t_tspec == INT &&
   4308 	       tn->tn_val->v_quad == 0;
   4309 }
   4310 
   4311 static void
   4312 check_null_effect(const tnode_t *tn)
   4313 {
   4314 
   4315 	if (!hflag)
   4316 		return;
   4317 	if (has_side_effect(tn))
   4318 		return;
   4319 	if (is_void_cast(tn) && is_local_symbol(tn->tn_left))
   4320 		return;
   4321 	if (is_void_cast(tn) && is_int_constant_zero(tn->tn_left))
   4322 		return;
   4323 
   4324 	/* expression has null effect */
   4325 	warning(129);
   4326 }
   4327 
   4328 static void
   4329 check_expr_addr(const tnode_t *ln, bool szof, bool fcall)
   4330 {
   4331 	/* XXX: Taking warn_about_unreachable into account here feels wrong. */
   4332 	if (ln->tn_op == NAME && (reached || !warn_about_unreachable)) {
   4333 		if (!szof)
   4334 			mark_as_set(ln->tn_sym);
   4335 		mark_as_used(ln->tn_sym, fcall, szof);
   4336 	}
   4337 	if (ln->tn_op == INDIR && ln->tn_left->tn_op == PLUS)
   4338 		/* check the range of array indices */
   4339 		check_array_index(ln->tn_left, true);
   4340 }
   4341 
   4342 static void
   4343 check_expr_load(const tnode_t *ln)
   4344 {
   4345 	if (ln->tn_op == INDIR && ln->tn_left->tn_op == PLUS)
   4346 		/* check the range of array indices */
   4347 		check_array_index(ln->tn_left, false);
   4348 }
   4349 
   4350 static void
   4351 check_expr_side_effect(const tnode_t *ln, bool szof)
   4352 {
   4353 	scl_t sc;
   4354 	dinfo_t *di;
   4355 
   4356 	/* XXX: Taking warn_about_unreachable into account here feels wrong. */
   4357 	if (ln->tn_op == NAME && (reached || !warn_about_unreachable)) {
   4358 		sc = ln->tn_sym->s_scl;
   4359 		/*
   4360 		 * Look if there was a asm statement in one of the
   4361 		 * compound statements we are in. If not, we don't
   4362 		 * print a warning.
   4363 		 */
   4364 		for (di = dcs; di != NULL; di = di->d_enclosing) {
   4365 			if (di->d_asm)
   4366 				break;
   4367 		}
   4368 		if (sc != EXTERN && sc != STATIC &&
   4369 		    !ln->tn_sym->s_set && !szof && di == NULL) {
   4370 			/* '%s' may be used before set */
   4371 			warning(158, ln->tn_sym->s_name);
   4372 			mark_as_set(ln->tn_sym);
   4373 		}
   4374 		mark_as_used(ln->tn_sym, false, false);
   4375 	}
   4376 }
   4377 
   4378 static void
   4379 check_expr_assign(const tnode_t *ln, bool szof)
   4380 {
   4381 	/* XXX: Taking warn_about_unreachable into account here feels wrong. */
   4382 	if (ln->tn_op == NAME && !szof && (reached || !warn_about_unreachable)) {
   4383 		mark_as_set(ln->tn_sym);
   4384 		if (ln->tn_sym->s_scl == EXTERN)
   4385 			outusg(ln->tn_sym);
   4386 	}
   4387 	if (ln->tn_op == INDIR && ln->tn_left->tn_op == PLUS)
   4388 		/* check the range of array indices */
   4389 		check_array_index(ln->tn_left, false);
   4390 }
   4391 
   4392 static void
   4393 check_expr_call(const tnode_t *tn, const tnode_t *ln,
   4394 		bool szof, bool vctx, bool cond, bool retval_discarded)
   4395 {
   4396 	lint_assert(ln->tn_op == ADDR);
   4397 	lint_assert(ln->tn_left->tn_op == NAME);
   4398 	if (!szof &&
   4399 	    !is_compiler_builtin(ln->tn_left->tn_sym->s_name))
   4400 		outcall(tn, vctx || cond, retval_discarded);
   4401 }
   4402 
   4403 static bool
   4404 check_expr_op(const tnode_t *tn, op_t op, const tnode_t *ln,
   4405 	      bool szof, bool fcall, bool vctx, bool cond,
   4406 	      bool retval_discarded, bool eqwarn)
   4407 {
   4408 	switch (op) {
   4409 	case ADDR:
   4410 		check_expr_addr(ln, szof, fcall);
   4411 		break;
   4412 	case LOAD:
   4413 		check_expr_load(ln);
   4414 		/* FALLTHROUGH */
   4415 	case PUSH:
   4416 	case INCBEF:
   4417 	case DECBEF:
   4418 	case INCAFT:
   4419 	case DECAFT:
   4420 	case ADDASS:
   4421 	case SUBASS:
   4422 	case MULASS:
   4423 	case DIVASS:
   4424 	case MODASS:
   4425 	case ANDASS:
   4426 	case ORASS:
   4427 	case XORASS:
   4428 	case SHLASS:
   4429 	case SHRASS:
   4430 	case REAL:
   4431 	case IMAG:
   4432 		check_expr_side_effect(ln, szof);
   4433 		break;
   4434 	case ASSIGN:
   4435 		check_expr_assign(ln, szof);
   4436 		break;
   4437 	case CALL:
   4438 		check_expr_call(tn, ln, szof, vctx, cond, retval_discarded);
   4439 		break;
   4440 	case EQ:
   4441 		if (hflag && eqwarn)
   4442 			/* operator '==' found where '=' was expected */
   4443 			warning(160);
   4444 		break;
   4445 	case CON:
   4446 	case NAME:
   4447 	case STRING:
   4448 		return false;
   4449 	default:
   4450 		break;
   4451 	}
   4452 	return true;
   4453 }
   4454 
   4455 /*
   4456  *	vctx			???
   4457  *	cond			whether the expression is a condition that
   4458  *				will be compared with 0
   4459  *	eqwarn			whether the operator '==' might be a
   4460  *				misspelled '='
   4461  *	fcall			whether the expression is a function call
   4462  *	retval_discarded	whether the return value of a function call
   4463  *				is discarded; such calls will be analyzed by
   4464  *				lint2 in messages 4, 8 and 9
   4465  *	szof			whether the expression is part of a sizeof
   4466  *				expression, which means that its value is
   4467  *				discarded since only the type is relevant
   4468  */
   4469 void
   4470 check_expr_misc(const tnode_t *tn, bool vctx, bool cond,
   4471 		bool eqwarn, bool fcall, bool retval_discarded, bool szof)
   4472 {
   4473 	tnode_t *ln, *rn;
   4474 	const mod_t *mp;
   4475 	op_t op;
   4476 	bool cvctx, ccond, eq, discard;
   4477 
   4478 	if (tn == NULL)
   4479 		return;
   4480 
   4481 	ln = tn->tn_left;
   4482 	rn = tn->tn_right;
   4483 	mp = &modtab[op = tn->tn_op];
   4484 
   4485 	if (!check_expr_op(tn, op, ln,
   4486 	    szof, fcall, vctx, cond, retval_discarded, eqwarn))
   4487 		return;
   4488 
   4489 	cvctx = mp->m_value_context;
   4490 	ccond = mp->m_compares_with_zero;
   4491 	eq = mp->m_warn_if_operand_eq &&
   4492 	     !ln->tn_parenthesized &&
   4493 	     rn != NULL && !rn->tn_parenthesized;
   4494 
   4495 	/*
   4496 	 * values of operands of ':' are not used if the type of at least
   4497 	 * one of the operands (for gcc compatibility) is void
   4498 	 * XXX test/value context of QUEST should probably be used as
   4499 	 * context for both operands of COLON
   4500 	 */
   4501 	if (op == COLON && tn->tn_type->t_tspec == VOID)
   4502 		cvctx = ccond = false;
   4503 	discard = op == CVT && tn->tn_type->t_tspec == VOID;
   4504 	check_expr_misc(ln, cvctx, ccond, eq, op == CALL, discard, szof);
   4505 
   4506 	switch (op) {
   4507 	case PUSH:
   4508 		if (rn != NULL)
   4509 			check_expr_misc(rn, false, false, eq, false, false,
   4510 			    szof);
   4511 		break;
   4512 	case LOGAND:
   4513 	case LOGOR:
   4514 		check_expr_misc(rn, false, true, eq, false, false, szof);
   4515 		break;
   4516 	case COLON:
   4517 		check_expr_misc(rn, cvctx, ccond, eq, false, false, szof);
   4518 		break;
   4519 	case COMMA:
   4520 		check_expr_misc(rn, vctx, cond, false, false, false, szof);
   4521 		break;
   4522 	default:
   4523 		if (mp->m_binary)
   4524 			check_expr_misc(rn, true, false, eq, false, false,
   4525 			    szof);
   4526 		break;
   4527 	}
   4528 }
   4529 
   4530 /*
   4531  * Checks the range of array indices, if possible.
   4532  * amper is set if only the address of the element is used. This
   4533  * means that the index is allowed to refer to the first element
   4534  * after the array.
   4535  */
   4536 static void
   4537 check_array_index(tnode_t *tn, bool amper)
   4538 {
   4539 	int	dim;
   4540 	tnode_t	*ln, *rn;
   4541 	int	elsz;
   4542 	int64_t	con;
   4543 
   4544 	ln = tn->tn_left;
   4545 	rn = tn->tn_right;
   4546 
   4547 	/* We can only check constant indices. */
   4548 	if (rn->tn_op != CON)
   4549 		return;
   4550 
   4551 	/* Return if the left node does not stem from an array. */
   4552 	if (ln->tn_op != ADDR)
   4553 		return;
   4554 	if (ln->tn_left->tn_op != STRING && ln->tn_left->tn_op != NAME)
   4555 		return;
   4556 	if (ln->tn_left->tn_type->t_tspec != ARRAY)
   4557 		return;
   4558 
   4559 	/*
   4560 	 * For incomplete array types, we can print a warning only if
   4561 	 * the index is negative.
   4562 	 */
   4563 	if (is_incomplete(ln->tn_left->tn_type) && rn->tn_val->v_quad >= 0)
   4564 		return;
   4565 
   4566 	/* Get the size of one array element */
   4567 	if ((elsz = length_in_bits(ln->tn_type->t_subt, NULL)) == 0)
   4568 		return;
   4569 	elsz /= CHAR_SIZE;
   4570 
   4571 	/* Change the unit of the index from bytes to element size. */
   4572 	if (is_uinteger(rn->tn_type->t_tspec)) {
   4573 		con = (uint64_t)rn->tn_val->v_quad / elsz;
   4574 	} else {
   4575 		con = rn->tn_val->v_quad / elsz;
   4576 	}
   4577 
   4578 	dim = ln->tn_left->tn_type->t_dim + (amper ? 1 : 0);
   4579 
   4580 	if (!is_uinteger(rn->tn_type->t_tspec) && con < 0) {
   4581 		/* array subscript cannot be negative: %ld */
   4582 		warning(167, (long)con);
   4583 	} else if (dim > 0 && (uint64_t)con >= (uint64_t)dim) {
   4584 		/* array subscript cannot be > %d: %ld */
   4585 		warning(168, dim - 1, (long)con);
   4586 	}
   4587 }
   4588 
   4589 static bool
   4590 is_out_of_char_range(const tnode_t *tn)
   4591 {
   4592 	return tn->tn_op == CON &&
   4593 	       !(0 <= tn->tn_val->v_quad &&
   4594 		 tn->tn_val->v_quad < 1 << (CHAR_SIZE - 1));
   4595 }
   4596 
   4597 /*
   4598  * Check for ordered comparisons of unsigned values with 0.
   4599  */
   4600 static void
   4601 check_integer_comparison(op_t op, tnode_t *ln, tnode_t *rn)
   4602 {
   4603 	tspec_t	lt, rt;
   4604 
   4605 	lt = ln->tn_type->t_tspec;
   4606 	rt = rn->tn_type->t_tspec;
   4607 
   4608 	if (ln->tn_op != CON && rn->tn_op != CON)
   4609 		return;
   4610 
   4611 	if (!is_integer(lt) || !is_integer(rt))
   4612 		return;
   4613 
   4614 	if (hflag || pflag) {
   4615 		if (lt == CHAR && is_out_of_char_range(rn)) {
   4616 			/* nonportable character comparison '%s %d' */
   4617 			warning(230, op_name(op), (int)rn->tn_val->v_quad);
   4618 			return;
   4619 		}
   4620 		if (rt == CHAR && is_out_of_char_range(ln)) {
   4621 			/* nonportable character comparison '%s %d' */
   4622 			warning(230, op_name(op), (int)ln->tn_val->v_quad);
   4623 			return;
   4624 		}
   4625 	}
   4626 
   4627 	if (is_uinteger(lt) && !is_uinteger(rt) &&
   4628 	    rn->tn_op == CON && rn->tn_val->v_quad <= 0) {
   4629 		if (rn->tn_val->v_quad < 0) {
   4630 			/* operator '%s' compares '%s' with '%s' */
   4631 			warning(162, op_name(op),
   4632 			    type_name(ln->tn_type), "negative constant");
   4633 		} else if (op == LT || op == GE) {
   4634 			/* operator '%s' compares '%s' with '%s' */
   4635 			warning(162, op_name(op), type_name(ln->tn_type), "0");
   4636 		}
   4637 		return;
   4638 	}
   4639 	if (is_uinteger(rt) && !is_uinteger(lt) &&
   4640 	    ln->tn_op == CON && ln->tn_val->v_quad <= 0) {
   4641 		if (ln->tn_val->v_quad < 0) {
   4642 			/* operator '%s' compares '%s' with '%s' */
   4643 			warning(162, op_name(op),
   4644 			    "negative constant", type_name(rn->tn_type));
   4645 		} else if (op == GT || op == LE) {
   4646 			/* operator '%s' compares '%s' with '%s' */
   4647 			warning(162, op_name(op), "0", type_name(rn->tn_type));
   4648 		}
   4649 		return;
   4650 	}
   4651 }
   4652 
   4653 /*
   4654  * Return whether the expression can be used for static initialization.
   4655  *
   4656  * Constant initialization expressions must be constant or an address
   4657  * of a static object with an optional offset. In the first case,
   4658  * the result is returned in *offsp. In the second case, the static
   4659  * object is returned in *symp and the offset in *offsp.
   4660  *
   4661  * The expression can consist of PLUS, MINUS, ADDR, NAME, STRING and
   4662  * CON. Type conversions are allowed if they do not change binary
   4663  * representation (including width).
   4664  *
   4665  * C99 6.6 "Constant expressions"
   4666  * C99 6.7.8p4 restricts initializers for static storage duration
   4667  */
   4668 bool
   4669 constant_addr(const tnode_t *tn, const sym_t **symp, ptrdiff_t *offsp)
   4670 {
   4671 	const sym_t *sym;
   4672 	ptrdiff_t offs1, offs2;
   4673 	tspec_t	t, ot;
   4674 
   4675 	switch (tn->tn_op) {
   4676 	case MINUS:
   4677 		if (tn->tn_right->tn_op == CVT)
   4678 			return constant_addr(tn->tn_right, symp, offsp);
   4679 		else if (tn->tn_right->tn_op != CON)
   4680 			return false;
   4681 		/* FALLTHROUGH */
   4682 	case PLUS:
   4683 		offs1 = offs2 = 0;
   4684 		if (tn->tn_left->tn_op == CON) {
   4685 			offs1 = (ptrdiff_t)tn->tn_left->tn_val->v_quad;
   4686 			if (!constant_addr(tn->tn_right, &sym, &offs2))
   4687 				return false;
   4688 		} else if (tn->tn_right->tn_op == CON) {
   4689 			offs2 = (ptrdiff_t)tn->tn_right->tn_val->v_quad;
   4690 			if (tn->tn_op == MINUS)
   4691 				offs2 = -offs2;
   4692 			if (!constant_addr(tn->tn_left, &sym, &offs1))
   4693 				return false;
   4694 		} else {
   4695 			return false;
   4696 		}
   4697 		*symp = sym;
   4698 		*offsp = offs1 + offs2;
   4699 		return true;
   4700 	case ADDR:
   4701 		if (tn->tn_left->tn_op == NAME) {
   4702 			*symp = tn->tn_left->tn_sym;
   4703 			*offsp = 0;
   4704 			return true;
   4705 		} else {
   4706 			/*
   4707 			 * If this would be the front end of a compiler we
   4708 			 * would return a label instead of 0, at least if
   4709 			 * 'tn->tn_left->tn_op == STRING'.
   4710 			 */
   4711 			*symp = NULL;
   4712 			*offsp = 0;
   4713 			return true;
   4714 		}
   4715 	case CVT:
   4716 		t = tn->tn_type->t_tspec;
   4717 		ot = tn->tn_left->tn_type->t_tspec;
   4718 		if ((!is_integer(t) && t != PTR) ||
   4719 		    (!is_integer(ot) && ot != PTR)) {
   4720 			return false;
   4721 		}
   4722 #if 0
   4723 		/*
   4724 		 * consider:
   4725 		 *	struct foo {
   4726 		 *		unsigned char a;
   4727 		 *	} f = {
   4728 		 *		(unsigned char)(unsigned long)
   4729 		 *		    (&(((struct foo *)0)->a))
   4730 		 *	};
   4731 		 * since psize(unsigned long) != psize(unsigned char),
   4732 		 * this fails.
   4733 		 */
   4734 		else if (psize(t) != psize(ot))
   4735 			return -1;
   4736 #endif
   4737 		return constant_addr(tn->tn_left, symp, offsp);
   4738 	default:
   4739 		return false;
   4740 	}
   4741 }
   4742 
   4743 /* Append s2 to s1, then free s2. */
   4744 strg_t *
   4745 cat_strings(strg_t *s1, strg_t *s2)
   4746 {
   4747 
   4748 	if (s1->st_char != s2->st_char) {
   4749 		/* cannot concatenate wide and regular string literals */
   4750 		error(292);
   4751 		return s1;
   4752 	}
   4753 
   4754 	size_t len1 = s1->st_len;
   4755 	size_t len2 = s2->st_len;
   4756 	size_t chsize = s1->st_char ? sizeof(char) : sizeof(wchar_t);
   4757 	size_t size1 = len1 * chsize;
   4758 	size_t size2 = (len2 + 1) * chsize;
   4759 	s1->st_mem = xrealloc(s1->st_mem, size1 + size2);
   4760 	memcpy((char *)s1->st_mem + size1, s2->st_mem, size2);
   4761 	free(s2->st_mem);
   4762 
   4763 	s1->st_len = len1 + len2;
   4764 	free(s2);
   4765 
   4766 	return s1;
   4767 }
   4768 
   4769 static bool
   4770 is_confusing_precedence(op_t op, op_t lop, bool lparen, op_t rop, bool rparen)
   4771 {
   4772 
   4773 	if (op == SHL || op == SHR) {
   4774 		if (!lparen && (lop == PLUS || lop == MINUS))
   4775 			return true;
   4776 		if (!rparen && (rop == PLUS || rop == MINUS))
   4777 			return true;
   4778 		return false;
   4779 	}
   4780 
   4781 	if (op == LOGOR) {
   4782 		if (!lparen && lop == LOGAND)
   4783 			return true;
   4784 		if (!rparen && rop == LOGAND)
   4785 			return true;
   4786 		return false;
   4787 	}
   4788 
   4789 	lint_assert(op == BITAND || op == BITXOR || op == BITOR);
   4790 	if (!lparen && lop != op) {
   4791 		if (lop == PLUS || lop == MINUS)
   4792 			return true;
   4793 		if (lop == BITAND || lop == BITXOR)
   4794 			return true;
   4795 	}
   4796 	if (!rparen && rop != op) {
   4797 		if (rop == PLUS || rop == MINUS)
   4798 			return true;
   4799 		if (rop == BITAND || rop == BITXOR)
   4800 			return true;
   4801 	}
   4802 	return false;
   4803 }
   4804 
   4805 /*
   4806  * Print a warning if the given node has operands which should be
   4807  * parenthesized.
   4808  *
   4809  * XXX Does not work if an operand is a constant expression. Constant
   4810  * expressions are already folded.
   4811  */
   4812 static void
   4813 check_precedence_confusion(tnode_t *tn)
   4814 {
   4815 	tnode_t *ln, *rn;
   4816 
   4817 	if (!hflag)
   4818 		return;
   4819 
   4820 	debug_node(tn);
   4821 
   4822 	lint_assert(is_binary(tn));
   4823 	for (ln = tn->tn_left; ln->tn_op == CVT; ln = ln->tn_left)
   4824 		continue;
   4825 	for (rn = tn->tn_right; rn->tn_op == CVT; rn = rn->tn_left)
   4826 		continue;
   4827 
   4828 	if (is_confusing_precedence(tn->tn_op,
   4829 	    ln->tn_op, ln->tn_parenthesized,
   4830 	    rn->tn_op, rn->tn_parenthesized)) {
   4831 		/* precedence confusion possible: parenthesize! */
   4832 		warning(169);
   4833 	}
   4834 }
   4835 
   4836 typedef struct stmt_expr {
   4837 	memory_pool se_mem;
   4838 	sym_t *se_sym;
   4839 	struct stmt_expr *se_enclosing;
   4840 } stmt_expr;
   4841 
   4842 static stmt_expr *stmt_exprs;
   4843 
   4844 void
   4845 begin_statement_expr(void)
   4846 {
   4847 	stmt_expr *se = xmalloc(sizeof(*se));
   4848 	se->se_mem = expr_save_memory();
   4849 	se->se_sym = NULL;
   4850 	se->se_enclosing = stmt_exprs;
   4851 	stmt_exprs = se;
   4852 }
   4853 
   4854 void
   4855 do_statement_expr(tnode_t *tn)
   4856 {
   4857 	block_level--;
   4858 	mem_block_level--;
   4859 	stmt_exprs->se_sym = tn != NULL
   4860 	    ? mktempsym(block_dup_type(tn->tn_type))
   4861 	    : NULL;		/* after a syntax error */
   4862 	mem_block_level++;
   4863 	block_level++;
   4864 	/* ({ }) is a GCC extension */
   4865 	gnuism(320);
   4866 }
   4867 
   4868 tnode_t *
   4869 end_statement_expr(void)
   4870 {
   4871 	stmt_expr *se = stmt_exprs;
   4872 	if (se->se_sym == NULL)
   4873 		return NULL;	/* after a syntax error */
   4874 	tnode_t *tn = build_name(se->se_sym, false);
   4875 	(void)expr_save_memory();	/* leak */
   4876 	expr_restore_memory(se->se_mem);
   4877 	stmt_exprs = se->se_enclosing;
   4878 	free(se);
   4879 	return tn;
   4880 }
   4881