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