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