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