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