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