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