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