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