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