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