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