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