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