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