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