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