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