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