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