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