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