Home | History | Annotate | Line # | Download | only in lint1
tree.c revision 1.456
      1 /*	$NetBSD: tree.c,v 1.456 2022/06/21 22:10:30 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.456 2022/06/21 22:10:30 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 (is_struct_or_union(t))
    567 			str = tp->t_str;
    568 	} else if (op == ARROW && t == PTR) {
    569 		t = (tp = tp->t_subt)->t_tspec;
    570 		if (is_struct_or_union(t))
    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(const type_t *tp, 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 '%s' */
   1085 		error(96, type_name(tp));
   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 || (is_struct_or_union(lt) &&
   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(ltp, 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 (is_struct_or_union(lt) && is_struct_or_union(rt))
   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 (is_struct_or_union(nst) && 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 (is_struct_or_union(lt) && is_struct_or_union(rt)) {
   2893 			/* assignment of different structures (%s != %s) */
   2894 			error(240, tspec_name(lt), tspec_name(rt));
   2895 		} else {
   2896 			/* cannot assign to '%s' from '%s' */
   2897 			error(171, type_name(ltp), type_name(rtp));
   2898 		}
   2899 	} else if (mp->m_binary) {
   2900 		/* operands of '%s' have incompatible types '%s' and '%s' */
   2901 		error(107, mp->m_name, tspec_name(lt), tspec_name(rt));
   2902 	} else {
   2903 		lint_assert(rt == NOTSPEC);
   2904 		/* operand of '%s' has invalid type '%s' */
   2905 		error(108, mp->m_name, type_name(ltp));
   2906 	}
   2907 }
   2908 
   2909 /*
   2910  * Called if incompatible pointer types are detected.
   2911  * Print an appropriate warning.
   2912  */
   2913 static void
   2914 warn_incompatible_pointers(const mod_t *mp,
   2915 			   const type_t *ltp, const type_t *rtp)
   2916 {
   2917 	tspec_t	lt, rt;
   2918 
   2919 	lint_assert(ltp->t_tspec == PTR);
   2920 	lint_assert(rtp->t_tspec == PTR);
   2921 
   2922 	lt = ltp->t_subt->t_tspec;
   2923 	rt = rtp->t_subt->t_tspec;
   2924 
   2925 	if (is_struct_or_union(lt) && is_struct_or_union(rt)) {
   2926 		if (mp == NULL) {
   2927 			/* illegal structure pointer combination */
   2928 			warning(244);
   2929 		} else {
   2930 			/* incompatible structure pointers: '%s' '%s' '%s' */
   2931 			warning(245, type_name(ltp), mp->m_name, type_name(rtp));
   2932 		}
   2933 	} else {
   2934 		if (mp == NULL) {
   2935 			/* illegal combination of '%s' and '%s' */
   2936 			warning(184, type_name(ltp), type_name(rtp));
   2937 		} else {
   2938 			/* illegal combination of '%s' and '%s', op '%s' */
   2939 			warning(124,
   2940 			    type_name(ltp), type_name(rtp), mp->m_name);
   2941 		}
   2942 	}
   2943 }
   2944 
   2945 /* Return a type based on tp1, with added qualifiers from tp2. */
   2946 static type_t *
   2947 merge_qualifiers(type_t *tp1, const type_t *tp2)
   2948 {
   2949 	type_t *ntp, *nstp;
   2950 	bool c1, c2, v1, v2;
   2951 
   2952 	lint_assert(tp1->t_tspec == PTR);
   2953 	lint_assert(tp2->t_tspec == PTR);
   2954 
   2955 	c1 = tp1->t_subt->t_const;
   2956 	c2 = tp2->t_subt->t_const;
   2957 	v1 = tp1->t_subt->t_volatile;
   2958 	v2 = tp2->t_subt->t_volatile;
   2959 
   2960 	if (c1 == (c1 | c2) && v1 == (v1 | v2))
   2961 		return tp1;
   2962 
   2963 	nstp = expr_dup_type(tp1->t_subt);
   2964 	nstp->t_const |= c2;
   2965 	nstp->t_volatile |= v2;
   2966 
   2967 	ntp = expr_dup_type(tp1);
   2968 	ntp->t_subt = nstp;
   2969 	return ntp;
   2970 }
   2971 
   2972 /*
   2973  * Returns true if the given structure or union has a constant member
   2974  * (maybe recursively).
   2975  */
   2976 static bool
   2977 has_constant_member(const type_t *tp)
   2978 {
   2979 	sym_t *m;
   2980 
   2981 	lint_assert(is_struct_or_union(tp->t_tspec));
   2982 
   2983 	for (m = tp->t_str->sou_first_member; m != NULL; m = m->s_next) {
   2984 		const type_t *mtp = m->s_type;
   2985 		if (mtp->t_const)
   2986 			return true;
   2987 		if (is_struct_or_union(mtp->t_tspec) &&
   2988 		    has_constant_member(mtp))
   2989 			return true;
   2990 	}
   2991 	return false;
   2992 }
   2993 
   2994 /*
   2995  * Create a new node for one of the operators POINT and ARROW.
   2996  */
   2997 static tnode_t *
   2998 build_struct_access(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
   2999 {
   3000 	tnode_t	*ntn, *ctn;
   3001 	bool	nolval;
   3002 
   3003 	lint_assert(rn->tn_op == NAME);
   3004 	lint_assert(is_member(rn->tn_sym));
   3005 
   3006 	/*
   3007 	 * Remember if the left operand is an lvalue (structure members
   3008 	 * are lvalues if and only if the structure itself is an lvalue).
   3009 	 */
   3010 	nolval = op == POINT && !ln->tn_lvalue;
   3011 
   3012 	if (op == POINT) {
   3013 		ln = build_address(sys, ln, true);
   3014 	} else if (ln->tn_type->t_tspec != PTR) {
   3015 		lint_assert(!allow_c90);
   3016 		lint_assert(is_integer(ln->tn_type->t_tspec));
   3017 		ln = convert(NOOP, 0, expr_derive_type(gettyp(VOID), PTR), ln);
   3018 	}
   3019 
   3020 	ctn = build_integer_constant(PTRDIFF_TSPEC,
   3021 	    rn->tn_sym->u.s_member.sm_offset_in_bits / CHAR_SIZE);
   3022 
   3023 	ntn = new_tnode(PLUS, sys, expr_derive_type(rn->tn_type, PTR),
   3024 	    ln, ctn);
   3025 	if (ln->tn_op == CON)
   3026 		ntn = fold(ntn);
   3027 
   3028 	if (rn->tn_type->t_bitfield) {
   3029 		ntn = new_tnode(FSEL, sys, ntn->tn_type->t_subt, ntn, NULL);
   3030 	} else {
   3031 		ntn = new_tnode(INDIR, sys, ntn->tn_type->t_subt, ntn, NULL);
   3032 	}
   3033 
   3034 	if (nolval)
   3035 		ntn->tn_lvalue = false;
   3036 
   3037 	return ntn;
   3038 }
   3039 
   3040 /*
   3041  * Create a node for INCAFT, INCBEF, DECAFT and DECBEF.
   3042  */
   3043 static tnode_t *
   3044 build_prepost_incdec(op_t op, bool sys, tnode_t *ln)
   3045 {
   3046 	tnode_t	*cn, *ntn;
   3047 
   3048 	lint_assert(ln != NULL);
   3049 
   3050 	if (ln->tn_type->t_tspec == PTR) {
   3051 		cn = subt_size_in_bytes(ln->tn_type);
   3052 	} else {
   3053 		cn = build_integer_constant(INT, (int64_t)1);
   3054 	}
   3055 	ntn = new_tnode(op, sys, ln->tn_type, ln, cn);
   3056 
   3057 	return ntn;
   3058 }
   3059 
   3060 /*
   3061  * Create a node for REAL, IMAG
   3062  */
   3063 static tnode_t *
   3064 build_real_imag(op_t op, bool sys, tnode_t *ln)
   3065 {
   3066 	tnode_t	*cn, *ntn;
   3067 
   3068 	lint_assert(ln != NULL);
   3069 
   3070 	if (ln->tn_op == NAME) {
   3071 		/*
   3072 		 * This may be too much, but it avoids wrong warnings.
   3073 		 * See d_c99_complex_split.c.
   3074 		 */
   3075 		mark_as_used(ln->tn_sym, false, false);
   3076 		mark_as_set(ln->tn_sym);
   3077 	}
   3078 
   3079 	switch (ln->tn_type->t_tspec) {
   3080 	case LCOMPLEX:
   3081 		/* XXX: integer and LDOUBLE don't match. */
   3082 		cn = build_integer_constant(LDOUBLE, (int64_t)1);
   3083 		break;
   3084 	case DCOMPLEX:
   3085 		/* XXX: integer and DOUBLE don't match. */
   3086 		cn = build_integer_constant(DOUBLE, (int64_t)1);
   3087 		break;
   3088 	case FCOMPLEX:
   3089 		/* XXX: integer and FLOAT don't match. */
   3090 		cn = build_integer_constant(FLOAT, (int64_t)1);
   3091 		break;
   3092 	default:
   3093 		/* __%s__ is illegal for type %s */
   3094 		error(276, op == REAL ? "real" : "imag",
   3095 		    type_name(ln->tn_type));
   3096 		return NULL;
   3097 	}
   3098 	ntn = new_tnode(op, sys, cn->tn_type, ln, cn);
   3099 	ntn->tn_lvalue = true;
   3100 
   3101 	return ntn;
   3102 }
   3103 
   3104 /*
   3105  * Create a tree node for the unary & operator
   3106  */
   3107 static tnode_t *
   3108 build_address(bool sys, tnode_t *tn, bool noign)
   3109 {
   3110 	tspec_t	t;
   3111 
   3112 	if (!noign && ((t = tn->tn_type->t_tspec) == ARRAY || t == FUNC)) {
   3113 		if (!allow_c90)
   3114 			/* '&' before array or function: ignored */
   3115 			warning(127);
   3116 		return tn;
   3117 	}
   3118 
   3119 	/* eliminate &* */
   3120 	if (tn->tn_op == INDIR &&
   3121 	    tn->tn_left->tn_type->t_tspec == PTR &&
   3122 	    tn->tn_left->tn_type->t_subt == tn->tn_type) {
   3123 		return tn->tn_left;
   3124 	}
   3125 
   3126 	return new_tnode(ADDR, sys, expr_derive_type(tn->tn_type, PTR),
   3127 	    tn, NULL);
   3128 }
   3129 
   3130 /*
   3131  * Create a node for operators PLUS and MINUS.
   3132  */
   3133 static tnode_t *
   3134 build_plus_minus(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
   3135 {
   3136 
   3137 	/* If pointer and integer, then pointer to the lhs. */
   3138 	if (rn->tn_type->t_tspec == PTR && is_integer(ln->tn_type->t_tspec)) {
   3139 		tnode_t *tmp = ln;
   3140 		ln = rn;
   3141 		rn = tmp;
   3142 	}
   3143 
   3144 	/* pointer +- integer */
   3145 	if (ln->tn_type->t_tspec == PTR && rn->tn_type->t_tspec != PTR) {
   3146 		lint_assert(is_integer(rn->tn_type->t_tspec));
   3147 
   3148 		check_ctype_macro_invocation(ln, rn);
   3149 		check_enum_array_index(ln, rn);
   3150 
   3151 		tnode_t *elsz = subt_size_in_bytes(ln->tn_type);
   3152 		if (rn->tn_type->t_tspec != elsz->tn_type->t_tspec)
   3153 			rn = convert(NOOP, 0, elsz->tn_type, rn);
   3154 
   3155 		tnode_t *prod = new_tnode(MULT, sys, rn->tn_type, rn, elsz);
   3156 		if (rn->tn_op == CON)
   3157 			prod = fold(prod);
   3158 
   3159 		return new_tnode(op, sys, ln->tn_type, ln, prod);
   3160 	}
   3161 
   3162 	/* pointer - pointer */
   3163 	if (rn->tn_type->t_tspec == PTR) {
   3164 		lint_assert(ln->tn_type->t_tspec == PTR);
   3165 		lint_assert(op == MINUS);
   3166 
   3167 		type_t *ptrdiff = gettyp(PTRDIFF_TSPEC);
   3168 		tnode_t *raw_diff = new_tnode(op, sys, ptrdiff, ln, rn);
   3169 		if (ln->tn_op == CON && rn->tn_op == CON)
   3170 			raw_diff = fold(raw_diff);
   3171 
   3172 		tnode_t *elsz = subt_size_in_bytes(ln->tn_type);
   3173 		balance(NOOP, &raw_diff, &elsz);
   3174 
   3175 		return new_tnode(DIV, sys, ptrdiff, raw_diff, elsz);
   3176 	}
   3177 
   3178 	return new_tnode(op, sys, ln->tn_type, ln, rn);
   3179 }
   3180 
   3181 /*
   3182  * Create a node for operators SHL and SHR.
   3183  */
   3184 static tnode_t *
   3185 build_bit_shift(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
   3186 {
   3187 	tspec_t	t;
   3188 	tnode_t	*ntn;
   3189 
   3190 	if ((t = rn->tn_type->t_tspec) != INT && t != UINT)
   3191 		rn = convert(CVT, 0, gettyp(INT), rn);
   3192 	ntn = new_tnode(op, sys, ln->tn_type, ln, rn);
   3193 	return ntn;
   3194 }
   3195 
   3196 /*
   3197  * Create a node for COLON.
   3198  */
   3199 static tnode_t *
   3200 build_colon(bool sys, tnode_t *ln, tnode_t *rn)
   3201 {
   3202 	tspec_t	lt, rt, pdt;
   3203 	type_t	*tp;
   3204 	tnode_t	*ntn;
   3205 
   3206 	lt = ln->tn_type->t_tspec;
   3207 	rt = rn->tn_type->t_tspec;
   3208 	pdt = PTRDIFF_TSPEC;
   3209 
   3210 	/*
   3211 	 * Arithmetic types are balanced, all other type combinations
   3212 	 * still need to be handled.
   3213 	 */
   3214 	if (is_arithmetic(lt) && is_arithmetic(rt)) {
   3215 		tp = ln->tn_type;
   3216 	} else if (lt == BOOL && rt == BOOL) {
   3217 		tp = ln->tn_type;
   3218 	} else if (lt == VOID || rt == VOID) {
   3219 		tp = gettyp(VOID);
   3220 	} else if (is_struct_or_union(lt)) {
   3221 		/* Both types must be identical. */
   3222 		lint_assert(is_struct_or_union(rt));
   3223 		lint_assert(ln->tn_type->t_str == rn->tn_type->t_str);
   3224 		if (is_incomplete(ln->tn_type)) {
   3225 			/* unknown operand size, op %s */
   3226 			error(138, op_name(COLON));
   3227 			return NULL;
   3228 		}
   3229 		tp = ln->tn_type;
   3230 	} else if (lt == PTR && is_integer(rt)) {
   3231 		if (rt != pdt) {
   3232 			rn = convert(NOOP, 0, gettyp(pdt), rn);
   3233 			rt = pdt;
   3234 		}
   3235 		tp = ln->tn_type;
   3236 	} else if (rt == PTR && is_integer(lt)) {
   3237 		if (lt != pdt) {
   3238 			ln = convert(NOOP, 0, gettyp(pdt), ln);
   3239 			lt = pdt;
   3240 		}
   3241 		tp = rn->tn_type;
   3242 	} else if (lt == PTR && ln->tn_type->t_subt->t_tspec == VOID) {
   3243 		tp = merge_qualifiers(rn->tn_type, ln->tn_type);
   3244 	} else if (rt == PTR && rn->tn_type->t_subt->t_tspec == VOID) {
   3245 		tp = merge_qualifiers(ln->tn_type, rn->tn_type);
   3246 	} else {
   3247 		/*
   3248 		 * XXX For now we simply take the left type. This is
   3249 		 * probably wrong, if one type contains a function prototype
   3250 		 * and the other one, at the same place, only an old style
   3251 		 * declaration.
   3252 		 */
   3253 		tp = merge_qualifiers(ln->tn_type, rn->tn_type);
   3254 	}
   3255 
   3256 	ntn = new_tnode(COLON, sys, tp, ln, rn);
   3257 
   3258 	return ntn;
   3259 }
   3260 
   3261 /*
   3262  * Create a node for an assignment operator (both = and op= ).
   3263  */
   3264 static tnode_t *
   3265 build_assignment(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
   3266 {
   3267 	tspec_t	lt, rt;
   3268 	tnode_t	*ntn, *ctn;
   3269 
   3270 	lint_assert(ln != NULL);
   3271 	lint_assert(rn != NULL);
   3272 
   3273 	lt = ln->tn_type->t_tspec;
   3274 	rt = rn->tn_type->t_tspec;
   3275 
   3276 	if ((op == ADDASS || op == SUBASS) && lt == PTR) {
   3277 		lint_assert(is_integer(rt));
   3278 		ctn = subt_size_in_bytes(ln->tn_type);
   3279 		if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
   3280 			rn = convert(NOOP, 0, ctn->tn_type, rn);
   3281 		rn = new_tnode(MULT, sys, rn->tn_type, rn, ctn);
   3282 		if (rn->tn_left->tn_op == CON)
   3283 			rn = fold(rn);
   3284 	}
   3285 
   3286 	if ((op == ASSIGN || op == RETURN || op == INIT) &&
   3287 	    (lt == STRUCT || rt == STRUCT)) {
   3288 		lint_assert(lt == rt);
   3289 		lint_assert(ln->tn_type->t_str == rn->tn_type->t_str);
   3290 		if (is_incomplete(ln->tn_type)) {
   3291 			if (op == RETURN) {
   3292 				/* cannot return incomplete type */
   3293 				error(212);
   3294 			} else {
   3295 				/* unknown operand size, op %s */
   3296 				error(138, op_name(op));
   3297 			}
   3298 			return NULL;
   3299 		}
   3300 	}
   3301 
   3302 	if (op == SHLASS) {
   3303 		if (portable_size_in_bits(lt) < portable_size_in_bits(rt)) {
   3304 			if (hflag)
   3305 				/* semantics of '%s' change in ANSI C; ... */
   3306 				warning(118, "<<=");
   3307 		}
   3308 	} else if (op != SHRASS) {
   3309 		if (op == ASSIGN || lt != PTR) {
   3310 			if (lt != rt ||
   3311 			    (ln->tn_type->t_bitfield && rn->tn_op == CON)) {
   3312 				rn = convert(op, 0, ln->tn_type, rn);
   3313 				rt = lt;
   3314 			}
   3315 		}
   3316 	}
   3317 
   3318 	ntn = new_tnode(op, sys, ln->tn_type, ln, rn);
   3319 
   3320 	return ntn;
   3321 }
   3322 
   3323 /*
   3324  * Get the size in bytes of type tp->t_subt, as a constant expression of type
   3325  * ptrdiff_t as seen from the target platform.
   3326  */
   3327 static tnode_t *
   3328 subt_size_in_bytes(type_t *tp)
   3329 {
   3330 	int elem, elsz_in_bits;
   3331 
   3332 	lint_assert(tp->t_tspec == PTR);
   3333 	tp = tp->t_subt;
   3334 
   3335 	elem = 1;
   3336 	elsz_in_bits = 0;
   3337 
   3338 	while (tp->t_tspec == ARRAY) {
   3339 		elem *= tp->t_dim;
   3340 		tp = tp->t_subt;
   3341 	}
   3342 
   3343 	switch (tp->t_tspec) {
   3344 	case FUNC:
   3345 		/* pointer to function is not allowed here */
   3346 		error(110);
   3347 		break;
   3348 	case VOID:
   3349 		/* cannot do pointer arithmetic on operand of unknown size */
   3350 		gnuism(136);
   3351 		break;
   3352 	case STRUCT:
   3353 	case UNION:
   3354 		if ((elsz_in_bits = tp->t_str->sou_size_in_bits) == 0)
   3355 			/* cannot do pointer arithmetic on operand of ... */
   3356 			error(136);
   3357 		break;
   3358 	case ENUM:
   3359 		if (is_incomplete(tp)) {
   3360 			/* cannot do pointer arithmetic on operand of ... */
   3361 			warning(136);
   3362 		}
   3363 		/* FALLTHROUGH */
   3364 	default:
   3365 		if ((elsz_in_bits = size_in_bits(tp->t_tspec)) == 0) {
   3366 			/* cannot do pointer arithmetic on operand of ... */
   3367 			error(136);
   3368 		} else {
   3369 			lint_assert(elsz_in_bits != -1);
   3370 		}
   3371 		break;
   3372 	}
   3373 
   3374 	if (elem == 0 && elsz_in_bits != 0) {
   3375 		/* cannot do pointer arithmetic on operand of unknown size */
   3376 		error(136);
   3377 	}
   3378 
   3379 	if (elsz_in_bits == 0)
   3380 		elsz_in_bits = CHAR_SIZE;
   3381 
   3382 	return build_integer_constant(PTRDIFF_TSPEC,
   3383 	    (int64_t)(elem * elsz_in_bits / CHAR_SIZE));
   3384 }
   3385 
   3386 /*
   3387  * XXX
   3388  * Note: There appear to be a number of bugs in detecting overflow in
   3389  * this function. An audit and a set of proper regression tests are needed.
   3390  *     --Perry Metzger, Nov. 16, 2001
   3391  */
   3392 /*
   3393  * Do only as much as necessary to compute constant expressions.
   3394  * Called only if the operator allows folding and all operands are constants.
   3395  */
   3396 static tnode_t *
   3397 fold(tnode_t *tn)
   3398 {
   3399 	val_t *v;
   3400 	tspec_t t;
   3401 	bool utyp, ovfl;
   3402 	int64_t sl, sr = 0, q = 0, mask;
   3403 	uint64_t ul, ur = 0;
   3404 	tnode_t *cn;
   3405 
   3406 	v = xcalloc(1, sizeof(*v));
   3407 	v->v_tspec = tn->tn_type->t_tspec;
   3408 
   3409 	t = tn->tn_left->tn_type->t_tspec;
   3410 	utyp = !is_integer(t) || is_uinteger(t);
   3411 	ul = sl = tn->tn_left->tn_val->v_quad;
   3412 	if (is_binary(tn))
   3413 		ur = sr = tn->tn_right->tn_val->v_quad;
   3414 
   3415 	mask = value_bits(size_in_bits(t));
   3416 	ovfl = false;
   3417 
   3418 	switch (tn->tn_op) {
   3419 	case UPLUS:
   3420 		q = sl;
   3421 		break;
   3422 	case UMINUS:
   3423 		q = sl == INT64_MIN ? sl : -sl;
   3424 		if (sl != 0 && msb(q, t) == msb(sl, t))
   3425 			ovfl = true;
   3426 		break;
   3427 	case COMPL:
   3428 		q = ~sl;
   3429 		break;
   3430 	case MULT:
   3431 		if (utyp) {
   3432 			q = ul * ur;
   3433 			if (q != (q & mask))
   3434 				ovfl = true;
   3435 			else if ((ul != 0) && ((q / ul) != ur))
   3436 				ovfl = true;
   3437 		} else {
   3438 			q = sl * sr;
   3439 			if (msb(q, t) != (msb(sl, t) ^ msb(sr, t)))
   3440 				ovfl = true;
   3441 		}
   3442 		break;
   3443 	case DIV:
   3444 		if (sr == 0) {
   3445 			/* division by 0 */
   3446 			error(139);
   3447 			q = utyp ? -1 : INT64_MAX;
   3448 		} else {
   3449 			q = utyp ? (int64_t)(ul / ur) : sl / sr;
   3450 		}
   3451 		break;
   3452 	case MOD:
   3453 		if (sr == 0) {
   3454 			/* modulus by 0 */
   3455 			error(140);
   3456 			q = 0;
   3457 		} else {
   3458 			q = utyp ? (int64_t)(ul % ur) : sl % sr;
   3459 		}
   3460 		break;
   3461 	case PLUS:
   3462 		q = utyp ? (int64_t)(ul + ur) : sl + sr;
   3463 		if (msb(sl, t) && msb(sr, t) && !msb(q, t))
   3464 			ovfl = true;
   3465 		if (!utyp && !msb(sl, t) && !msb(sr, t) && msb(q, t))
   3466 			ovfl = true;
   3467 		break;
   3468 	case MINUS:
   3469 		q = utyp ? (int64_t)(ul - ur) : sl - sr;
   3470 		if (!utyp && msb(sl, t) && !msb(sr, t) && !msb(q, t))
   3471 			ovfl = true;
   3472 		if (!msb(sl, t) && msb(sr, t) && msb(q, t))
   3473 			ovfl = true;
   3474 		break;
   3475 	case SHL:
   3476 		q = utyp ? (int64_t)(ul << sr) : sl << sr;
   3477 		break;
   3478 	case SHR:
   3479 		/*
   3480 		 * The sign must be explicitly extended because
   3481 		 * shifts of signed values are implementation dependent.
   3482 		 */
   3483 		q = ul >> sr;
   3484 		q = convert_integer(q, t, size_in_bits(t) - (int)sr);
   3485 		break;
   3486 	case LT:
   3487 		q = (utyp ? ul < ur : sl < sr) ? 1 : 0;
   3488 		break;
   3489 	case LE:
   3490 		q = (utyp ? ul <= ur : sl <= sr) ? 1 : 0;
   3491 		break;
   3492 	case GE:
   3493 		q = (utyp ? ul >= ur : sl >= sr) ? 1 : 0;
   3494 		break;
   3495 	case GT:
   3496 		q = (utyp ? ul > ur : sl > sr) ? 1 : 0;
   3497 		break;
   3498 	case EQ:
   3499 		q = (utyp ? ul == ur : sl == sr) ? 1 : 0;
   3500 		break;
   3501 	case NE:
   3502 		q = (utyp ? ul != ur : sl != sr) ? 1 : 0;
   3503 		break;
   3504 	case BITAND:
   3505 		q = utyp ? (int64_t)(ul & ur) : sl & sr;
   3506 		break;
   3507 	case BITXOR:
   3508 		q = utyp ? (int64_t)(ul ^ ur) : sl ^ sr;
   3509 		break;
   3510 	case BITOR:
   3511 		q = utyp ? (int64_t)(ul | ur) : sl | sr;
   3512 		break;
   3513 	default:
   3514 		lint_assert(/*CONSTCOND*/false);
   3515 	}
   3516 
   3517 	/* XXX does not work for quads. */
   3518 	if (ovfl ||
   3519 	    ((uint64_t)(q | mask) != ~(uint64_t)0 && (q & ~mask) != 0)) {
   3520 		if (hflag)
   3521 			/* integer overflow detected, op '%s' */
   3522 			warning(141, op_name(tn->tn_op));
   3523 	}
   3524 
   3525 	v->v_quad = convert_integer(q, t, 0);
   3526 
   3527 	cn = build_constant(tn->tn_type, v);
   3528 	if (tn->tn_left->tn_system_dependent)
   3529 		cn->tn_system_dependent = true;
   3530 	if (is_binary(tn) && tn->tn_right->tn_system_dependent)
   3531 		cn->tn_system_dependent = true;
   3532 
   3533 	return cn;
   3534 }
   3535 
   3536 /*
   3537  * Fold constant nodes, as much as is needed for comparing the value with 0.
   3538  */
   3539 static tnode_t *
   3540 fold_bool(tnode_t *tn)
   3541 {
   3542 	bool	l, r;
   3543 	val_t	*v;
   3544 
   3545 	v = xcalloc(1, sizeof(*v));
   3546 	v->v_tspec = tn->tn_type->t_tspec;
   3547 	lint_assert(v->v_tspec == INT || (Tflag && v->v_tspec == BOOL));
   3548 
   3549 	l = constant_is_nonzero(tn->tn_left);
   3550 	r = is_binary(tn) && constant_is_nonzero(tn->tn_right);
   3551 
   3552 	switch (tn->tn_op) {
   3553 	case NOT:
   3554 		if (hflag && !constcond_flag)
   3555 			/* constant argument to '!' */
   3556 			warning(239);
   3557 		v->v_quad = !l ? 1 : 0;
   3558 		break;
   3559 	case LOGAND:
   3560 		v->v_quad = l && r ? 1 : 0;
   3561 		break;
   3562 	case LOGOR:
   3563 		v->v_quad = l || r ? 1 : 0;
   3564 		break;
   3565 	default:
   3566 		lint_assert(/*CONSTCOND*/false);
   3567 	}
   3568 
   3569 	return build_constant(tn->tn_type, v);
   3570 }
   3571 
   3572 static ldbl_t
   3573 floating_error_value(tspec_t t, ldbl_t lv)
   3574 {
   3575 	if (t == FLOAT) {
   3576 		return lv < 0 ? -FLT_MAX : FLT_MAX;
   3577 	} else if (t == DOUBLE) {
   3578 		return lv < 0 ? -DBL_MAX : DBL_MAX;
   3579 	} else {
   3580 		/* LINTED 248: floating-point constant out of range */
   3581 		ldbl_t max = LDBL_MAX;
   3582 		return lv < 0 ? -max : max;
   3583 	}
   3584 }
   3585 
   3586 /*
   3587  * Fold constant nodes having operands with floating point type.
   3588  */
   3589 static tnode_t *
   3590 fold_float(tnode_t *tn)
   3591 {
   3592 	val_t	*v;
   3593 	tspec_t	t;
   3594 	ldbl_t	lv, rv = 0;
   3595 
   3596 	fpe = 0;
   3597 	v = xcalloc(1, sizeof(*v));
   3598 	v->v_tspec = t = tn->tn_type->t_tspec;
   3599 
   3600 	lint_assert(is_floating(t));
   3601 	lint_assert(t == tn->tn_left->tn_type->t_tspec);
   3602 	lint_assert(!is_binary(tn) || t == tn->tn_right->tn_type->t_tspec);
   3603 
   3604 	lv = tn->tn_left->tn_val->v_ldbl;
   3605 	if (is_binary(tn))
   3606 		rv = tn->tn_right->tn_val->v_ldbl;
   3607 
   3608 	switch (tn->tn_op) {
   3609 	case UPLUS:
   3610 		v->v_ldbl = lv;
   3611 		break;
   3612 	case UMINUS:
   3613 		v->v_ldbl = -lv;
   3614 		break;
   3615 	case MULT:
   3616 		v->v_ldbl = lv * rv;
   3617 		break;
   3618 	case DIV:
   3619 		if (rv == 0.0) {
   3620 			/* division by 0 */
   3621 			error(139);
   3622 			v->v_ldbl = floating_error_value(t, lv);
   3623 		} else {
   3624 			v->v_ldbl = lv / rv;
   3625 		}
   3626 		break;
   3627 	case PLUS:
   3628 		v->v_ldbl = lv + rv;
   3629 		break;
   3630 	case MINUS:
   3631 		v->v_ldbl = lv - rv;
   3632 		break;
   3633 	case LT:
   3634 		v->v_quad = lv < rv ? 1 : 0;
   3635 		break;
   3636 	case LE:
   3637 		v->v_quad = lv <= rv ? 1 : 0;
   3638 		break;
   3639 	case GE:
   3640 		v->v_quad = lv >= rv ? 1 : 0;
   3641 		break;
   3642 	case GT:
   3643 		v->v_quad = lv > rv ? 1 : 0;
   3644 		break;
   3645 	case EQ:
   3646 		v->v_quad = lv == rv ? 1 : 0;
   3647 		break;
   3648 	case NE:
   3649 		v->v_quad = lv != rv ? 1 : 0;
   3650 		break;
   3651 	default:
   3652 		lint_assert(/*CONSTCOND*/false);
   3653 	}
   3654 
   3655 	lint_assert(fpe != 0 || isnan((double)v->v_ldbl) == 0);
   3656 	if (fpe != 0 || isfinite((double)v->v_ldbl) == 0 ||
   3657 	    (t == FLOAT &&
   3658 	     (v->v_ldbl > FLT_MAX || v->v_ldbl < -FLT_MAX)) ||
   3659 	    (t == DOUBLE &&
   3660 	     (v->v_ldbl > DBL_MAX || v->v_ldbl < -DBL_MAX))) {
   3661 		/* floating point overflow detected, op %s */
   3662 		warning(142, op_name(tn->tn_op));
   3663 		v->v_ldbl = floating_error_value(t, v->v_ldbl);
   3664 		fpe = 0;
   3665 	}
   3666 
   3667 	return build_constant(tn->tn_type, v);
   3668 }
   3669 
   3670 
   3671 /*
   3672  * Create a constant node for sizeof.
   3673  */
   3674 tnode_t *
   3675 build_sizeof(const type_t *tp)
   3676 {
   3677 	unsigned int size_in_bytes = type_size_in_bits(tp) / CHAR_SIZE;
   3678 	tnode_t *tn = build_integer_constant(SIZEOF_TSPEC, size_in_bytes);
   3679 	tn->tn_system_dependent = true;
   3680 	debug_step("build_sizeof '%s' = %u", type_name(tp), size_in_bytes);
   3681 	return tn;
   3682 }
   3683 
   3684 /*
   3685  * Create a constant node for offsetof.
   3686  */
   3687 /* ARGSUSED */ /* See implementation comments. */
   3688 tnode_t *
   3689 build_offsetof(const type_t *tp, const sym_t *sym)
   3690 {
   3691 	unsigned int offset_in_bytes;
   3692 	tnode_t *tn;
   3693 
   3694 	if (!is_struct_or_union(tp->t_tspec))
   3695 		/* unacceptable operand of '%s' */
   3696 		error(111, "offsetof");
   3697 
   3698 	/* XXX: wrong size, no checking for sym fixme */
   3699 	offset_in_bytes = type_size_in_bits(tp) / CHAR_SIZE;
   3700 	tn = build_integer_constant(SIZEOF_TSPEC, offset_in_bytes);
   3701 	tn->tn_system_dependent = true;
   3702 	return tn;
   3703 }
   3704 
   3705 unsigned int
   3706 type_size_in_bits(const type_t *tp)
   3707 {
   3708 	unsigned int elem, elsz;
   3709 	bool	flex;
   3710 
   3711 	elem = 1;
   3712 	flex = false;
   3713 	lint_assert(tp != NULL);
   3714 	while (tp->t_tspec == ARRAY) {
   3715 		flex = true;	/* allow c99 flex arrays [] [0] */
   3716 		elem *= tp->t_dim;
   3717 		tp = tp->t_subt;
   3718 	}
   3719 	if (elem == 0) {
   3720 		if (!flex) {
   3721 			/* cannot take size/alignment of incomplete type */
   3722 			error(143);
   3723 			elem = 1;
   3724 		}
   3725 	}
   3726 	switch (tp->t_tspec) {
   3727 	case FUNC:
   3728 		/* cannot take size/alignment of function type '%s' */
   3729 		error(144, type_name(tp));
   3730 		elsz = 1;
   3731 		break;
   3732 	case STRUCT:
   3733 	case UNION:
   3734 		if (is_incomplete(tp)) {
   3735 			/* cannot take size/alignment of incomplete type */
   3736 			error(143);
   3737 			elsz = 1;
   3738 		} else {
   3739 			elsz = tp->t_str->sou_size_in_bits;
   3740 		}
   3741 		break;
   3742 	case ENUM:
   3743 		if (is_incomplete(tp)) {
   3744 			/* cannot take size/alignment of incomplete type */
   3745 			warning(143);
   3746 		}
   3747 		/* FALLTHROUGH */
   3748 	default:
   3749 		if (tp->t_bitfield) {
   3750 			/* cannot take size/alignment of bit-field */
   3751 			error(145);
   3752 		}
   3753 		if (tp->t_tspec == VOID) {
   3754 			/* cannot take size/alignment of void */
   3755 			error(146);
   3756 			elsz = 1;
   3757 		} else {
   3758 			elsz = size_in_bits(tp->t_tspec);
   3759 			lint_assert(elsz > 0);
   3760 		}
   3761 		break;
   3762 	}
   3763 
   3764 	return elem * elsz;
   3765 }
   3766 
   3767 tnode_t *
   3768 build_alignof(const type_t *tp)
   3769 {
   3770 	switch (tp->t_tspec) {
   3771 	case ARRAY:
   3772 		break;
   3773 
   3774 	case FUNC:
   3775 		/* cannot take size/alignment of function type '%s' */
   3776 		error(144, type_name(tp));
   3777 		return 0;
   3778 
   3779 	case STRUCT:
   3780 	case UNION:
   3781 		if (is_incomplete(tp)) {
   3782 			/* cannot take size/alignment of incomplete type */
   3783 			error(143);
   3784 			return 0;
   3785 		}
   3786 		break;
   3787 	case ENUM:
   3788 		break;
   3789 	default:
   3790 		if (tp->t_bitfield) {
   3791 			/* cannot take size/alignment of bit-field */
   3792 			error(145);
   3793 			return 0;
   3794 		}
   3795 		if (tp->t_tspec == VOID) {
   3796 			/* cannot take size/alignment of void */
   3797 			error(146);
   3798 			return 0;
   3799 		}
   3800 		break;
   3801 	}
   3802 
   3803 	return build_integer_constant(SIZEOF_TSPEC,
   3804 	    (int64_t)alignment_in_bits(tp) / CHAR_SIZE);
   3805 }
   3806 
   3807 /*
   3808  * Type casts.
   3809  */
   3810 tnode_t *
   3811 cast(tnode_t *tn, type_t *tp)
   3812 {
   3813 	tspec_t	nt, ot;
   3814 
   3815 	if (tn == NULL)
   3816 		return NULL;
   3817 
   3818 	tn = cconv(tn);
   3819 
   3820 	lint_assert(tp != NULL);
   3821 	nt = tp->t_tspec;
   3822 	ot = tn->tn_type->t_tspec;
   3823 
   3824 	if (nt == VOID) {
   3825 		/*
   3826 		 * C90 6.3.4, C99 6.5.4p2 and C11 6.5.4p2 allow any type to
   3827 		 * be cast to void.  The only other allowed casts are from a
   3828 		 * scalar type to a scalar type.
   3829 		 */
   3830 	} else if (nt == UNION) {
   3831 		sym_t *m;
   3832 		struct_or_union *str = tp->t_str;
   3833 		if (!allow_gcc) {
   3834 			/* union cast is a GCC extension */
   3835 			error(328);
   3836 			return NULL;
   3837 		}
   3838 		for (m = str->sou_first_member; m != NULL; m = m->s_next) {
   3839 			if (eqtype(m->s_type, tn->tn_type,
   3840 			    false, false, NULL)) {
   3841 				tn = expr_alloc_tnode();
   3842 				tn->tn_op = CVT;
   3843 				tn->tn_type = tp;
   3844 				tn->tn_cast = true;
   3845 				tn->tn_right = NULL;
   3846 				return tn;
   3847 			}
   3848 		}
   3849 		/* type '%s' is not a member of '%s' */
   3850 		error(329, type_name(tn->tn_type), type_name(tp));
   3851 		return NULL;
   3852 	} else if (nt == STRUCT || nt == ARRAY || nt == FUNC) {
   3853 		/* Casting to a struct is an undocumented GCC extension. */
   3854 		if (!(allow_gcc && nt == STRUCT))
   3855 			goto invalid_cast;
   3856 	} else if (is_struct_or_union(ot)) {
   3857 		goto invalid_cast;
   3858 	} else if (ot == VOID) {
   3859 		/* improper cast of void expression */
   3860 		error(148);
   3861 		return NULL;
   3862 	} else if (is_integer(nt) && is_scalar(ot)) {
   3863 		/* ok */
   3864 	} else if (is_floating(nt) && is_arithmetic(ot)) {
   3865 		/* ok */
   3866 	} else if (nt == PTR && is_integer(ot)) {
   3867 		/* ok */
   3868 	} else if (nt == PTR && ot == PTR) {
   3869 		if (!tp->t_subt->t_const && tn->tn_type->t_subt->t_const) {
   3870 			if (hflag)
   3871 				/* cast discards 'const' from type '%s' */
   3872 				warning(275, type_name(tn->tn_type));
   3873 		}
   3874 	} else
   3875 		goto invalid_cast;
   3876 
   3877 	tn = convert(CVT, 0, tp, tn);
   3878 	tn->tn_cast = true;
   3879 
   3880 	return tn;
   3881 
   3882 invalid_cast:
   3883 	/* invalid cast from '%s' to '%s' */
   3884 	error(147, type_name(tn->tn_type), type_name(tp));
   3885 	return NULL;
   3886 }
   3887 
   3888 /*
   3889  * Create the node for a function argument.
   3890  * All necessary conversions and type checks are done in
   3891  * build_function_call because build_function_argument has no
   3892  * information about expected argument types.
   3893  */
   3894 tnode_t *
   3895 build_function_argument(tnode_t *args, tnode_t *arg)
   3896 {
   3897 	tnode_t	*ntn;
   3898 
   3899 	/*
   3900 	 * If there was a serious error in the expression for the argument,
   3901 	 * create a dummy argument so the positions of the remaining arguments
   3902 	 * will not change.
   3903 	 */
   3904 	if (arg == NULL)
   3905 		arg = build_integer_constant(INT, 0);
   3906 
   3907 	ntn = new_tnode(PUSH, arg->tn_sys, arg->tn_type, arg, args);
   3908 
   3909 	return ntn;
   3910 }
   3911 
   3912 /*
   3913  * Create the node for a function call. Also check types of
   3914  * function arguments and insert conversions, if necessary.
   3915  */
   3916 tnode_t *
   3917 build_function_call(tnode_t *func, bool sys, tnode_t *args)
   3918 {
   3919 	tnode_t	*ntn;
   3920 	op_t	fcop;
   3921 
   3922 	if (func == NULL)
   3923 		return NULL;
   3924 
   3925 	if (func->tn_op == NAME && func->tn_type->t_tspec == FUNC) {
   3926 		fcop = CALL;
   3927 	} else {
   3928 		fcop = ICALL;
   3929 	}
   3930 
   3931 	check_ctype_function_call(func, args);
   3932 
   3933 	/*
   3934 	 * after cconv() func will always be a pointer to a function
   3935 	 * if it is a valid function designator.
   3936 	 */
   3937 	func = cconv(func);
   3938 
   3939 	if (func->tn_type->t_tspec != PTR ||
   3940 	    func->tn_type->t_subt->t_tspec != FUNC) {
   3941 		/* illegal function (type %s) */
   3942 		error(149, type_name(func->tn_type));
   3943 		return NULL;
   3944 	}
   3945 
   3946 	args = check_function_arguments(func->tn_type->t_subt, args);
   3947 
   3948 	ntn = new_tnode(fcop, sys, func->tn_type->t_subt->t_subt, func, args);
   3949 
   3950 	return ntn;
   3951 }
   3952 
   3953 /*
   3954  * Check types of all function arguments and insert conversions,
   3955  * if necessary.
   3956  */
   3957 static tnode_t *
   3958 check_function_arguments(type_t *ftp, tnode_t *args)
   3959 {
   3960 	tnode_t	*arg;
   3961 	sym_t	*asym;
   3962 	tspec_t	at;
   3963 	int	narg, npar, n, i;
   3964 
   3965 	/* get # of args in the prototype */
   3966 	npar = 0;
   3967 	for (asym = ftp->t_args; asym != NULL; asym = asym->s_next)
   3968 		npar++;
   3969 
   3970 	/* get # of args in function call */
   3971 	narg = 0;
   3972 	for (arg = args; arg != NULL; arg = arg->tn_right)
   3973 		narg++;
   3974 
   3975 	asym = ftp->t_args;
   3976 	if (ftp->t_proto && npar != narg && !(ftp->t_vararg && npar < narg)) {
   3977 		/* argument mismatch: %d arg%s passed, %d expected */
   3978 		error(150, narg, narg > 1 ? "s" : "", npar);
   3979 		asym = NULL;
   3980 	}
   3981 
   3982 	for (n = 1; n <= narg; n++) {
   3983 
   3984 		/*
   3985 		 * The rightmost argument is at the top of the argument
   3986 		 * subtree.
   3987 		 */
   3988 		for (i = narg, arg = args; i > n; i--, arg = arg->tn_right)
   3989 			continue;
   3990 
   3991 		/* some things which are always not allowed */
   3992 		if ((at = arg->tn_left->tn_type->t_tspec) == VOID) {
   3993 			/* void expressions may not be arguments, arg #%d */
   3994 			error(151, n);
   3995 			return NULL;
   3996 		} else if (is_struct_or_union(at) &&
   3997 			   is_incomplete(arg->tn_left->tn_type)) {
   3998 			/* argument cannot have unknown size, arg #%d */
   3999 			error(152, n);
   4000 			return NULL;
   4001 		} else if (is_integer(at) &&
   4002 			   arg->tn_left->tn_type->t_is_enum &&
   4003 			   is_incomplete(arg->tn_left->tn_type)) {
   4004 			/* argument cannot have unknown size, arg #%d */
   4005 			warning(152, n);
   4006 		}
   4007 
   4008 		/* class conversions (arg in value context) */
   4009 		arg->tn_left = cconv(arg->tn_left);
   4010 
   4011 		if (asym != NULL) {
   4012 			arg->tn_left = check_prototype_argument(
   4013 			    n, asym->s_type, arg->tn_left);
   4014 		} else {
   4015 			arg->tn_left = promote(NOOP, true, arg->tn_left);
   4016 		}
   4017 		arg->tn_type = arg->tn_left->tn_type;
   4018 
   4019 		if (asym != NULL)
   4020 			asym = asym->s_next;
   4021 	}
   4022 
   4023 	return args;
   4024 }
   4025 
   4026 /*
   4027  * Compare the type of an argument with the corresponding type of a
   4028  * prototype parameter. If it is a valid combination, but both types
   4029  * are not the same, insert a conversion to convert the argument into
   4030  * the type of the parameter.
   4031  */
   4032 static tnode_t *
   4033 check_prototype_argument(
   4034 	int	n,		/* pos of arg */
   4035 	type_t	*tp,		/* expected type (from prototype) */
   4036 	tnode_t	*tn)		/* argument */
   4037 {
   4038 	tnode_t	*ln;
   4039 	bool	dowarn;
   4040 
   4041 	ln = xcalloc(1, sizeof(*ln));
   4042 	ln->tn_type = expr_unqualified_type(tp);
   4043 	ln->tn_lvalue = true;
   4044 	if (typeok(FARG, n, ln, tn)) {
   4045 		if (!eqtype(tp, tn->tn_type,
   4046 		    true, false, (dowarn = false, &dowarn)) || dowarn)
   4047 			tn = convert(FARG, n, tp, tn);
   4048 	}
   4049 	free(ln);
   4050 	return tn;
   4051 }
   4052 
   4053 /*
   4054  * Return the value of an integral constant expression.
   4055  * If the expression is not constant or its type is not an integer
   4056  * type, an error message is printed.
   4057  */
   4058 val_t *
   4059 constant(tnode_t *tn, bool required)
   4060 {
   4061 	val_t	*v;
   4062 
   4063 	if (tn != NULL)
   4064 		tn = cconv(tn);
   4065 	if (tn != NULL)
   4066 		tn = promote(NOOP, false, tn);
   4067 
   4068 	v = xcalloc(1, sizeof(*v));
   4069 
   4070 	if (tn == NULL) {
   4071 		lint_assert(nerr != 0);
   4072 		debug_step("constant node is null; returning 1 instead");
   4073 		v->v_tspec = INT;
   4074 		v->v_quad = 1;
   4075 		return v;
   4076 	}
   4077 
   4078 	v->v_tspec = tn->tn_type->t_tspec;
   4079 
   4080 	if (tn->tn_op == CON) {
   4081 		lint_assert(tn->tn_type->t_tspec == tn->tn_val->v_tspec);
   4082 		if (is_integer(tn->tn_val->v_tspec)) {
   4083 			v->v_unsigned_since_c90 =
   4084 			    tn->tn_val->v_unsigned_since_c90;
   4085 			v->v_quad = tn->tn_val->v_quad;
   4086 			return v;
   4087 		}
   4088 		v->v_quad = tn->tn_val->v_ldbl;
   4089 	} else {
   4090 		v->v_quad = 1;
   4091 	}
   4092 
   4093 	if (required)
   4094 		/* integral constant expression expected */
   4095 		error(55);
   4096 	else
   4097 		/* variable array dimension is a C99/GCC extension */
   4098 		c99ism(318);
   4099 
   4100 	if (!is_integer(v->v_tspec))
   4101 		v->v_tspec = INT;
   4102 
   4103 	return v;
   4104 }
   4105 
   4106 static bool
   4107 is_constcond_false(const tnode_t *tn, tspec_t t)
   4108 {
   4109 	return (t == BOOL || t == INT) &&
   4110 	       tn->tn_op == CON && tn->tn_val->v_quad == 0;
   4111 }
   4112 
   4113 /*
   4114  * Perform some tests on expressions which can't be done in build_binary()
   4115  * and functions called by build_binary(). These tests must be done here
   4116  * because we need some information about the context in which the operations
   4117  * are performed.
   4118  * After all tests are performed and dofreeblk is true, expr() frees the
   4119  * memory which is used for the expression.
   4120  */
   4121 void
   4122 expr(tnode_t *tn, bool vctx, bool cond, bool dofreeblk, bool is_do_while)
   4123 {
   4124 
   4125 	if (tn == NULL) {	/* in case of errors */
   4126 		expr_free_all();
   4127 		return;
   4128 	}
   4129 
   4130 	/* expr() is also called in global initializations */
   4131 	if (dcs->d_kind != DK_EXTERN && !is_do_while)
   4132 		check_statement_reachable();
   4133 
   4134 	check_expr_misc(tn, vctx, cond, !cond, false, false, false);
   4135 	if (tn->tn_op == ASSIGN) {
   4136 		if (hflag && cond)
   4137 			/* assignment in conditional context */
   4138 			warning(159);
   4139 	} else if (tn->tn_op == CON) {
   4140 		if (hflag && cond && !constcond_flag &&
   4141 		    !tn->tn_system_dependent &&
   4142 		    !(is_do_while &&
   4143 		      is_constcond_false(tn, tn->tn_type->t_tspec)))
   4144 			/* constant in conditional context */
   4145 			warning(161);
   4146 	}
   4147 	if (!modtab[tn->tn_op].m_has_side_effect) {
   4148 		/*
   4149 		 * for left operands of COMMA this warning is already
   4150 		 * printed
   4151 		 */
   4152 		if (tn->tn_op != COMMA && !vctx && !cond)
   4153 			check_null_effect(tn);
   4154 	}
   4155 	debug_node(tn);
   4156 
   4157 	/* free the tree memory */
   4158 	if (dofreeblk)
   4159 		expr_free_all();
   4160 }
   4161 
   4162 static bool
   4163 has_side_effect(const tnode_t *tn) /* NOLINT(misc-no-recursion) */
   4164 {
   4165 	op_t op = tn->tn_op;
   4166 
   4167 	if (modtab[op].m_has_side_effect)
   4168 		return true;
   4169 
   4170 	if (op == CVT && tn->tn_type->t_tspec == VOID)
   4171 		return has_side_effect(tn->tn_left);
   4172 
   4173 	/* XXX: Why not has_side_effect(tn->tn_left) as well? */
   4174 	if (op == LOGAND || op == LOGOR)
   4175 		return has_side_effect(tn->tn_right);
   4176 
   4177 	/* XXX: Why not has_side_effect(tn->tn_left) as well? */
   4178 	if (op == QUEST)
   4179 		return has_side_effect(tn->tn_right);
   4180 
   4181 	if (op == COLON || op == COMMA) {
   4182 		return has_side_effect(tn->tn_left) ||
   4183 		       has_side_effect(tn->tn_right);
   4184 	}
   4185 
   4186 	return false;
   4187 }
   4188 
   4189 static bool
   4190 is_void_cast(const tnode_t *tn)
   4191 {
   4192 
   4193 	return tn->tn_op == CVT && tn->tn_cast &&
   4194 	       tn->tn_type->t_tspec == VOID;
   4195 }
   4196 
   4197 static bool
   4198 is_local_symbol(const tnode_t *tn)
   4199 {
   4200 
   4201 	return tn->tn_op == LOAD &&
   4202 	       tn->tn_left->tn_op == NAME &&
   4203 	       tn->tn_left->tn_sym->s_scl == AUTO;
   4204 }
   4205 
   4206 static bool
   4207 is_int_constant_zero(const tnode_t *tn)
   4208 {
   4209 
   4210 	return tn->tn_op == CON &&
   4211 	       tn->tn_type->t_tspec == INT &&
   4212 	       tn->tn_val->v_quad == 0;
   4213 }
   4214 
   4215 static void
   4216 check_null_effect(const tnode_t *tn)
   4217 {
   4218 
   4219 	if (!hflag)
   4220 		return;
   4221 	if (has_side_effect(tn))
   4222 		return;
   4223 	if (is_void_cast(tn) && is_local_symbol(tn->tn_left))
   4224 		return;
   4225 	if (is_void_cast(tn) && is_int_constant_zero(tn->tn_left))
   4226 		return;
   4227 
   4228 	/* expression has null effect */
   4229 	warning(129);
   4230 }
   4231 
   4232 static void
   4233 check_expr_addr(const tnode_t *ln, bool szof, bool fcall)
   4234 {
   4235 	/* XXX: Taking warn_about_unreachable into account here feels wrong. */
   4236 	if (ln->tn_op == NAME && (reached || !warn_about_unreachable)) {
   4237 		if (!szof)
   4238 			mark_as_set(ln->tn_sym);
   4239 		mark_as_used(ln->tn_sym, fcall, szof);
   4240 	}
   4241 	if (ln->tn_op == INDIR && ln->tn_left->tn_op == PLUS)
   4242 		/* check the range of array indices */
   4243 		check_array_index(ln->tn_left, true);
   4244 }
   4245 
   4246 static void
   4247 check_expr_load(const tnode_t *ln)
   4248 {
   4249 	if (ln->tn_op == INDIR && ln->tn_left->tn_op == PLUS)
   4250 		/* check the range of array indices */
   4251 		check_array_index(ln->tn_left, false);
   4252 }
   4253 
   4254 static void
   4255 check_expr_side_effect(const tnode_t *ln, bool szof)
   4256 {
   4257 	scl_t sc;
   4258 	dinfo_t *di;
   4259 
   4260 	/* XXX: Taking warn_about_unreachable into account here feels wrong. */
   4261 	if (ln->tn_op == NAME && (reached || !warn_about_unreachable)) {
   4262 		sc = ln->tn_sym->s_scl;
   4263 		/*
   4264 		 * Look if there was a asm statement in one of the
   4265 		 * compound statements we are in. If not, we don't
   4266 		 * print a warning.
   4267 		 */
   4268 		for (di = dcs; di != NULL; di = di->d_enclosing) {
   4269 			if (di->d_asm)
   4270 				break;
   4271 		}
   4272 		if (sc != EXTERN && sc != STATIC &&
   4273 		    !ln->tn_sym->s_set && !szof && di == NULL) {
   4274 			/* %s may be used before set */
   4275 			warning(158, ln->tn_sym->s_name);
   4276 			mark_as_set(ln->tn_sym);
   4277 		}
   4278 		mark_as_used(ln->tn_sym, false, false);
   4279 	}
   4280 }
   4281 
   4282 static void
   4283 check_expr_assign(const tnode_t *ln, bool szof)
   4284 {
   4285 	/* XXX: Taking warn_about_unreachable into account here feels wrong. */
   4286 	if (ln->tn_op == NAME && !szof && (reached || !warn_about_unreachable)) {
   4287 		mark_as_set(ln->tn_sym);
   4288 		if (ln->tn_sym->s_scl == EXTERN)
   4289 			outusg(ln->tn_sym);
   4290 	}
   4291 	if (ln->tn_op == INDIR && ln->tn_left->tn_op == PLUS)
   4292 		/* check the range of array indices */
   4293 		check_array_index(ln->tn_left, false);
   4294 }
   4295 
   4296 static void
   4297 check_expr_call(const tnode_t *tn, const tnode_t *ln,
   4298 		bool szof, bool vctx, bool cond, bool retval_discarded)
   4299 {
   4300 	lint_assert(ln->tn_op == ADDR);
   4301 	lint_assert(ln->tn_left->tn_op == NAME);
   4302 	if (!szof &&
   4303 	    !is_compiler_builtin(ln->tn_left->tn_sym->s_name))
   4304 		outcall(tn, vctx || cond, retval_discarded);
   4305 }
   4306 
   4307 static bool
   4308 check_expr_op(const tnode_t *tn, op_t op, const tnode_t *ln,
   4309 	      bool szof, bool fcall, bool vctx, bool cond,
   4310 	      bool retval_discarded, bool eqwarn)
   4311 {
   4312 	switch (op) {
   4313 	case ADDR:
   4314 		check_expr_addr(ln, szof, fcall);
   4315 		break;
   4316 	case LOAD:
   4317 		check_expr_load(ln);
   4318 		/* FALLTHROUGH */
   4319 	case PUSH:
   4320 	case INCBEF:
   4321 	case DECBEF:
   4322 	case INCAFT:
   4323 	case DECAFT:
   4324 	case ADDASS:
   4325 	case SUBASS:
   4326 	case MULASS:
   4327 	case DIVASS:
   4328 	case MODASS:
   4329 	case ANDASS:
   4330 	case ORASS:
   4331 	case XORASS:
   4332 	case SHLASS:
   4333 	case SHRASS:
   4334 	case REAL:
   4335 	case IMAG:
   4336 		check_expr_side_effect(ln, szof);
   4337 		break;
   4338 	case ASSIGN:
   4339 		check_expr_assign(ln, szof);
   4340 		break;
   4341 	case CALL:
   4342 		check_expr_call(tn, ln, szof, vctx, cond, retval_discarded);
   4343 		break;
   4344 	case EQ:
   4345 		if (hflag && eqwarn)
   4346 			/* operator '==' found where '=' was expected */
   4347 			warning(160);
   4348 		break;
   4349 	case CON:
   4350 	case NAME:
   4351 	case STRING:
   4352 		return false;
   4353 	default:
   4354 		break;
   4355 	}
   4356 	return true;
   4357 }
   4358 
   4359 /*
   4360  *	vctx			???
   4361  *	cond			whether the expression is a condition that
   4362  *				will be compared with 0
   4363  *	eqwarn			whether the operator '==' might be a
   4364  *				misspelled '='
   4365  *	fcall			whether the expression is a function call
   4366  *	retval_discarded	whether the return value of a function call
   4367  *				is discarded; such calls will be analyzed by
   4368  *				lint2 in messages 4, 8 and 9
   4369  *	szof			whether the expression is part of a sizeof
   4370  *				expression, which means that its value is
   4371  *				discarded since only the type is relevant
   4372  */
   4373 void
   4374 check_expr_misc(const tnode_t *tn, bool vctx, bool cond,
   4375 		bool eqwarn, bool fcall, bool retval_discarded, bool szof)
   4376 {
   4377 	tnode_t *ln, *rn;
   4378 	const mod_t *mp;
   4379 	op_t op;
   4380 	bool cvctx, ccond, eq, discard;
   4381 
   4382 	if (tn == NULL)
   4383 		return;
   4384 
   4385 	ln = tn->tn_left;
   4386 	rn = tn->tn_right;
   4387 	mp = &modtab[op = tn->tn_op];
   4388 
   4389 	if (!check_expr_op(tn, op, ln,
   4390 	    szof, fcall, vctx, cond, retval_discarded, eqwarn))
   4391 		return;
   4392 
   4393 	cvctx = mp->m_value_context;
   4394 	ccond = mp->m_compares_with_zero;
   4395 	eq = mp->m_warn_if_operand_eq &&
   4396 	     !ln->tn_parenthesized &&
   4397 	     rn != NULL && !rn->tn_parenthesized;
   4398 
   4399 	/*
   4400 	 * values of operands of ':' are not used if the type of at least
   4401 	 * one of the operands (for gcc compatibility) is void
   4402 	 * XXX test/value context of QUEST should probably be used as
   4403 	 * context for both operands of COLON
   4404 	 */
   4405 	if (op == COLON && tn->tn_type->t_tspec == VOID)
   4406 		cvctx = ccond = false;
   4407 	discard = op == CVT && tn->tn_type->t_tspec == VOID;
   4408 	check_expr_misc(ln, cvctx, ccond, eq, op == CALL, discard, szof);
   4409 
   4410 	switch (op) {
   4411 	case PUSH:
   4412 		if (rn != NULL)
   4413 			check_expr_misc(rn, false, false, eq, false, false,
   4414 			    szof);
   4415 		break;
   4416 	case LOGAND:
   4417 	case LOGOR:
   4418 		check_expr_misc(rn, false, true, eq, false, false, szof);
   4419 		break;
   4420 	case COLON:
   4421 		check_expr_misc(rn, cvctx, ccond, eq, false, false, szof);
   4422 		break;
   4423 	case COMMA:
   4424 		check_expr_misc(rn, vctx, cond, false, false, false, szof);
   4425 		break;
   4426 	default:
   4427 		if (mp->m_binary)
   4428 			check_expr_misc(rn, true, false, eq, false, false,
   4429 			    szof);
   4430 		break;
   4431 	}
   4432 }
   4433 
   4434 /*
   4435  * Checks the range of array indices, if possible.
   4436  * amper is set if only the address of the element is used. This
   4437  * means that the index is allowed to refer to the first element
   4438  * after the array.
   4439  */
   4440 static void
   4441 check_array_index(tnode_t *tn, bool amper)
   4442 {
   4443 	int	dim;
   4444 	tnode_t	*ln, *rn;
   4445 	int	elsz;
   4446 	int64_t	con;
   4447 
   4448 	ln = tn->tn_left;
   4449 	rn = tn->tn_right;
   4450 
   4451 	/* We can only check constant indices. */
   4452 	if (rn->tn_op != CON)
   4453 		return;
   4454 
   4455 	/* Return if the left node does not stem from an array. */
   4456 	if (ln->tn_op != ADDR)
   4457 		return;
   4458 	if (ln->tn_left->tn_op != STRING && ln->tn_left->tn_op != NAME)
   4459 		return;
   4460 	if (ln->tn_left->tn_type->t_tspec != ARRAY)
   4461 		return;
   4462 
   4463 	/*
   4464 	 * For incomplete array types, we can print a warning only if
   4465 	 * the index is negative.
   4466 	 */
   4467 	if (is_incomplete(ln->tn_left->tn_type) && rn->tn_val->v_quad >= 0)
   4468 		return;
   4469 
   4470 	/* Get the size of one array element */
   4471 	if ((elsz = length_in_bits(ln->tn_type->t_subt, NULL)) == 0)
   4472 		return;
   4473 	elsz /= CHAR_SIZE;
   4474 
   4475 	/* Change the unit of the index from bytes to element size. */
   4476 	if (is_uinteger(rn->tn_type->t_tspec)) {
   4477 		con = (uint64_t)rn->tn_val->v_quad / elsz;
   4478 	} else {
   4479 		con = rn->tn_val->v_quad / elsz;
   4480 	}
   4481 
   4482 	dim = ln->tn_left->tn_type->t_dim + (amper ? 1 : 0);
   4483 
   4484 	if (!is_uinteger(rn->tn_type->t_tspec) && con < 0) {
   4485 		/* array subscript cannot be negative: %ld */
   4486 		warning(167, (long)con);
   4487 	} else if (dim > 0 && (uint64_t)con >= (uint64_t)dim) {
   4488 		/* array subscript cannot be > %d: %ld */
   4489 		warning(168, dim - 1, (long)con);
   4490 	}
   4491 }
   4492 
   4493 static bool
   4494 is_out_of_char_range(const tnode_t *tn)
   4495 {
   4496 	return tn->tn_op == CON &&
   4497 	       !(0 <= tn->tn_val->v_quad &&
   4498 		 tn->tn_val->v_quad < 1 << (CHAR_SIZE - 1));
   4499 }
   4500 
   4501 /*
   4502  * Check for ordered comparisons of unsigned values with 0.
   4503  */
   4504 static void
   4505 check_integer_comparison(op_t op, tnode_t *ln, tnode_t *rn)
   4506 {
   4507 	tspec_t	lt, rt;
   4508 
   4509 	lt = ln->tn_type->t_tspec;
   4510 	rt = rn->tn_type->t_tspec;
   4511 
   4512 	if (ln->tn_op != CON && rn->tn_op != CON)
   4513 		return;
   4514 
   4515 	if (!is_integer(lt) || !is_integer(rt))
   4516 		return;
   4517 
   4518 	if (hflag || pflag) {
   4519 		if (lt == CHAR && is_out_of_char_range(rn)) {
   4520 			/* nonportable character comparison '%s %d' */
   4521 			warning(230, op_name(op), (int)rn->tn_val->v_quad);
   4522 			return;
   4523 		}
   4524 		if (rt == CHAR && is_out_of_char_range(ln)) {
   4525 			/* nonportable character comparison '%s %d' */
   4526 			warning(230, op_name(op), (int)ln->tn_val->v_quad);
   4527 			return;
   4528 		}
   4529 	}
   4530 
   4531 	if (is_uinteger(lt) && !is_uinteger(rt) &&
   4532 	    rn->tn_op == CON && rn->tn_val->v_quad <= 0) {
   4533 		if (rn->tn_val->v_quad < 0) {
   4534 			/* comparison of %s with %s, op %s */
   4535 			warning(162, type_name(ln->tn_type),
   4536 			    "negative constant", op_name(op));
   4537 		} else if (op == LT || op == GE) {
   4538 			/* comparison of %s with %s, op %s */
   4539 			warning(162, type_name(ln->tn_type), "0", op_name(op));
   4540 		}
   4541 		return;
   4542 	}
   4543 	if (is_uinteger(rt) && !is_uinteger(lt) &&
   4544 	    ln->tn_op == CON && ln->tn_val->v_quad <= 0) {
   4545 		if (ln->tn_val->v_quad < 0) {
   4546 			/* comparison of %s with %s, op %s */
   4547 			warning(162, "negative constant",
   4548 			    type_name(rn->tn_type), op_name(op));
   4549 		} else if (op == GT || op == LE) {
   4550 			/* comparison of %s with %s, op %s */
   4551 			warning(162, "0", type_name(rn->tn_type), op_name(op));
   4552 		}
   4553 		return;
   4554 	}
   4555 }
   4556 
   4557 /*
   4558  * Return whether the expression can be used for static initialization.
   4559  *
   4560  * Constant initialization expressions must be constant or an address
   4561  * of a static object with an optional offset. In the first case,
   4562  * the result is returned in *offsp. In the second case, the static
   4563  * object is returned in *symp and the offset in *offsp.
   4564  *
   4565  * The expression can consist of PLUS, MINUS, ADDR, NAME, STRING and
   4566  * CON. Type conversions are allowed if they do not change binary
   4567  * representation (including width).
   4568  *
   4569  * C99 6.6 "Constant expressions"
   4570  * C99 6.7.8p4 restricts initializers for static storage duration
   4571  */
   4572 bool
   4573 constant_addr(const tnode_t *tn, const sym_t **symp, ptrdiff_t *offsp)
   4574 {
   4575 	const sym_t *sym;
   4576 	ptrdiff_t offs1, offs2;
   4577 	tspec_t	t, ot;
   4578 
   4579 	switch (tn->tn_op) {
   4580 	case MINUS:
   4581 		if (tn->tn_right->tn_op == CVT)
   4582 			return constant_addr(tn->tn_right, symp, offsp);
   4583 		else if (tn->tn_right->tn_op != CON)
   4584 			return false;
   4585 		/* FALLTHROUGH */
   4586 	case PLUS:
   4587 		offs1 = offs2 = 0;
   4588 		if (tn->tn_left->tn_op == CON) {
   4589 			offs1 = (ptrdiff_t)tn->tn_left->tn_val->v_quad;
   4590 			if (!constant_addr(tn->tn_right, &sym, &offs2))
   4591 				return false;
   4592 		} else if (tn->tn_right->tn_op == CON) {
   4593 			offs2 = (ptrdiff_t)tn->tn_right->tn_val->v_quad;
   4594 			if (tn->tn_op == MINUS)
   4595 				offs2 = -offs2;
   4596 			if (!constant_addr(tn->tn_left, &sym, &offs1))
   4597 				return false;
   4598 		} else {
   4599 			return false;
   4600 		}
   4601 		*symp = sym;
   4602 		*offsp = offs1 + offs2;
   4603 		return true;
   4604 	case ADDR:
   4605 		if (tn->tn_left->tn_op == NAME) {
   4606 			*symp = tn->tn_left->tn_sym;
   4607 			*offsp = 0;
   4608 			return true;
   4609 		} else {
   4610 			/*
   4611 			 * If this would be the front end of a compiler we
   4612 			 * would return a label instead of 0, at least if
   4613 			 * 'tn->tn_left->tn_op == STRING'.
   4614 			 */
   4615 			*symp = NULL;
   4616 			*offsp = 0;
   4617 			return true;
   4618 		}
   4619 	case CVT:
   4620 		t = tn->tn_type->t_tspec;
   4621 		ot = tn->tn_left->tn_type->t_tspec;
   4622 		if ((!is_integer(t) && t != PTR) ||
   4623 		    (!is_integer(ot) && ot != PTR)) {
   4624 			return false;
   4625 		}
   4626 #if 0
   4627 		/*
   4628 		 * consider:
   4629 		 *	struct foo {
   4630 		 *		unsigned char a;
   4631 		 *	} f = {
   4632 		 *		(unsigned char)(unsigned long)
   4633 		 *		    (&(((struct foo *)0)->a))
   4634 		 *	};
   4635 		 * since psize(unsigned long) != psize(unsigned char),
   4636 		 * this fails.
   4637 		 */
   4638 		else if (psize(t) != psize(ot))
   4639 			return -1;
   4640 #endif
   4641 		return constant_addr(tn->tn_left, symp, offsp);
   4642 	default:
   4643 		return false;
   4644 	}
   4645 }
   4646 
   4647 /* Append s2 to s1, then free s2. */
   4648 strg_t *
   4649 cat_strings(strg_t *s1, strg_t *s2)
   4650 {
   4651 
   4652 	if (s1->st_char != s2->st_char) {
   4653 		/* cannot concatenate wide and regular string literals */
   4654 		error(292);
   4655 		return s1;
   4656 	}
   4657 
   4658 	size_t len1 = s1->st_len;
   4659 	size_t len2 = s2->st_len;
   4660 	size_t chsize = s1->st_char ? sizeof(char) : sizeof(wchar_t);
   4661 	size_t size1 = len1 * chsize;
   4662 	size_t size2 = (len2 + 1) * chsize;
   4663 	s1->st_mem = xrealloc(s1->st_mem, size1 + size2);
   4664 	memcpy((char *)s1->st_mem + size1, s2->st_mem, size2);
   4665 	free(s2->st_mem);
   4666 
   4667 	s1->st_len = len1 + len2;
   4668 	free(s2);
   4669 
   4670 	return s1;
   4671 }
   4672 
   4673 static bool
   4674 is_confusing_precedence(op_t op, op_t lop, bool lparen, op_t rop, bool rparen)
   4675 {
   4676 
   4677 	if (op == SHL || op == SHR) {
   4678 		if (!lparen && (lop == PLUS || lop == MINUS))
   4679 			return true;
   4680 		if (!rparen && (rop == PLUS || rop == MINUS))
   4681 			return true;
   4682 		return false;
   4683 	}
   4684 
   4685 	if (op == LOGOR) {
   4686 		if (!lparen && lop == LOGAND)
   4687 			return true;
   4688 		if (!rparen && rop == LOGAND)
   4689 			return true;
   4690 		return false;
   4691 	}
   4692 
   4693 	lint_assert(op == BITAND || op == BITXOR || op == BITOR);
   4694 	if (!lparen && lop != op) {
   4695 		if (lop == PLUS || lop == MINUS)
   4696 			return true;
   4697 		if (lop == BITAND || lop == BITXOR)
   4698 			return true;
   4699 	}
   4700 	if (!rparen && rop != op) {
   4701 		if (rop == PLUS || rop == MINUS)
   4702 			return true;
   4703 		if (rop == BITAND || rop == BITXOR)
   4704 			return true;
   4705 	}
   4706 	return false;
   4707 }
   4708 
   4709 /*
   4710  * Print a warning if the given node has operands which should be
   4711  * parenthesized.
   4712  *
   4713  * XXX Does not work if an operand is a constant expression. Constant
   4714  * expressions are already folded.
   4715  */
   4716 static void
   4717 check_precedence_confusion(tnode_t *tn)
   4718 {
   4719 	tnode_t *ln, *rn;
   4720 
   4721 	if (!hflag)
   4722 		return;
   4723 
   4724 	debug_node(tn);
   4725 
   4726 	lint_assert(is_binary(tn));
   4727 	for (ln = tn->tn_left; ln->tn_op == CVT; ln = ln->tn_left)
   4728 		continue;
   4729 	for (rn = tn->tn_right; rn->tn_op == CVT; rn = rn->tn_left)
   4730 		continue;
   4731 
   4732 	if (is_confusing_precedence(tn->tn_op,
   4733 	    ln->tn_op, ln->tn_parenthesized,
   4734 	    rn->tn_op, rn->tn_parenthesized)) {
   4735 		/* precedence confusion possible: parenthesize! */
   4736 		warning(169);
   4737 	}
   4738 }
   4739 
   4740 typedef struct stmt_expr {
   4741 	struct memory_block *se_mem;
   4742 	sym_t *se_sym;
   4743 	struct stmt_expr *se_enclosing;
   4744 } stmt_expr;
   4745 
   4746 static stmt_expr *stmt_exprs;
   4747 
   4748 void
   4749 begin_statement_expr(void)
   4750 {
   4751 	stmt_expr *se = xmalloc(sizeof(*se));
   4752 	se->se_mem = expr_save_memory();
   4753 	se->se_sym = NULL;
   4754 	se->se_enclosing = stmt_exprs;
   4755 	stmt_exprs = se;
   4756 }
   4757 
   4758 void
   4759 do_statement_expr(tnode_t *tn)
   4760 {
   4761 	block_level--;
   4762 	mem_block_level--;
   4763 	stmt_exprs->se_sym = tn != NULL
   4764 	    ? mktempsym(block_dup_type(tn->tn_type))
   4765 	    : NULL;		/* after a syntax error */
   4766 	mem_block_level++;
   4767 	block_level++;
   4768 	/* ({ }) is a GCC extension */
   4769 	gnuism(320);
   4770 }
   4771 
   4772 tnode_t *
   4773 end_statement_expr(void)
   4774 {
   4775 	stmt_expr *se = stmt_exprs;
   4776 	if (se->se_sym == NULL)
   4777 		return NULL;	/* after a syntax error */
   4778 	tnode_t *tn = build_name(se->se_sym, false);
   4779 	(void)expr_save_memory();	/* leak */
   4780 	expr_restore_memory(se->se_mem);
   4781 	stmt_exprs = se->se_enclosing;
   4782 	free(se);
   4783 	return tn;
   4784 }
   4785