xref: /netbsd-src/usr.bin/xlint/lint1/tree.c (revision 21e37cc72a480a47828990a439cde7ac9ffaf0c6)
1 /*	$NetBSD: tree.c,v 1.37 2004/06/20 22:20:17 jmc 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.37 2004/06/20 22:20:17 jmc 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 CHAR:
1915 			max = CHAR_MAX;		min = CHAR_MIN;		break;
1916 		case UCHAR:
1917 			max = UCHAR_MAX;	min = 0;		break;
1918 		case SCHAR:
1919 			max = SCHAR_MAX;	min = SCHAR_MIN;	break;
1920 		case SHORT:
1921 			max = SHRT_MAX;		min = SHRT_MIN;		break;
1922 		case USHORT:
1923 			max = USHRT_MAX;	min = 0;		break;
1924 		case ENUM:
1925 		case INT:
1926 			max = INT_MAX;		min = INT_MIN;		break;
1927 		case UINT:
1928 			max = (u_int)UINT_MAX;	min = 0;		break;
1929 		case LONG:
1930 			max = LONG_MAX;		min = LONG_MIN;		break;
1931 		case ULONG:
1932 			max = (u_long)ULONG_MAX; min = 0;		break;
1933 		case QUAD:
1934 			max = QUAD_MAX;		min = QUAD_MIN;		break;
1935 		case UQUAD:
1936 			max = (uint64_t)UQUAD_MAX; min = 0;		break;
1937 		case FLOAT:
1938 			max = FLT_MAX;		min = -FLT_MAX;		break;
1939 		case DOUBLE:
1940 			max = DBL_MAX;		min = -DBL_MAX;		break;
1941 		case PTR:
1942 			/* Got already an error because of float --> ptr */
1943 		case LDOUBLE:
1944 			max = LDBL_MAX;		min = -LDBL_MAX;	break;
1945 		default:
1946 			LERROR("cvtcon()");
1947 		}
1948 		if (v->v_ldbl > max || v->v_ldbl < min) {
1949 			if (nt == LDOUBLE)
1950 				LERROR("cvtcon()");
1951 			if (op == FARG) {
1952 				/* conv. of %s to %s is out of rng., arg #%d */
1953 				warning(295, tyname(lbuf, sizeof(lbuf),
1954 				    gettyp(ot)), tyname(rbuf, sizeof(rbuf), tp),
1955 				    arg);
1956 			} else {
1957 				/* conversion of %s to %s is out of range */
1958 				warning(119, tyname(lbuf, sizeof(lbuf),
1959 				    gettyp(ot)),
1960 				    tyname(rbuf, sizeof(rbuf), tp));
1961 			}
1962 			v->v_ldbl = v->v_ldbl > 0 ? max : min;
1963 		}
1964 		if (nt == FLOAT) {
1965 			nv->v_ldbl = (float)v->v_ldbl;
1966 		} else if (nt == DOUBLE) {
1967 			nv->v_ldbl = (double)v->v_ldbl;
1968 		} else if (nt == LDOUBLE) {
1969 			nv->v_ldbl = v->v_ldbl;
1970 		} else {
1971 			nv->v_quad = (nt == PTR || isutyp(nt)) ?
1972 				(uint64_t)v->v_ldbl : (int64_t)v->v_ldbl;
1973 		}
1974 	} else {
1975 		if (nt == FLOAT) {
1976 			nv->v_ldbl = (ot == PTR || isutyp(ot)) ?
1977 			       (float)(uint64_t)v->v_quad : (float)v->v_quad;
1978 		} else if (nt == DOUBLE) {
1979 			nv->v_ldbl = (ot == PTR || isutyp(ot)) ?
1980 			       (double)(uint64_t)v->v_quad : (double)v->v_quad;
1981 		} else if (nt == LDOUBLE) {
1982 			nv->v_ldbl = (ot == PTR || isutyp(ot)) ?
1983 			       (ldbl_t)(uint64_t)v->v_quad : (ldbl_t)v->v_quad;
1984 		} else {
1985 			rchk = 1;		/* Check for lost precision. */
1986 			nv->v_quad = v->v_quad;
1987 		}
1988 	}
1989 
1990 	if (v->v_ansiu && isftyp(nt)) {
1991 		/* ANSI C treats constant as unsigned */
1992 		warning(157);
1993 		v->v_ansiu = 0;
1994 	} else if (v->v_ansiu && (isityp(nt) && !isutyp(nt) &&
1995 				  psize(nt) > psize(ot))) {
1996 		/* ANSI C treats constant as unsigned */
1997 		warning(157);
1998 		v->v_ansiu = 0;
1999 	}
2000 
2001 	if (nt != FLOAT && nt != DOUBLE && nt != LDOUBLE) {
2002 		sz = tp->t_isfield ? tp->t_flen : size(nt);
2003 		nv->v_quad = xsign(nv->v_quad, nt, sz);
2004 	}
2005 
2006 	if (rchk && op != CVT) {
2007 		osz = size(ot);
2008 		nsz = tp->t_isfield ? tp->t_flen : size(nt);
2009 		xmask = qlmasks[nsz] ^ qlmasks[osz];
2010 		xmsk1 = qlmasks[nsz] ^ qlmasks[osz - 1];
2011 		/*
2012 		 * For bitwise operations we are not interested in the
2013 		 * value, but in the bits itself.
2014 		 */
2015 		if (op == ORASS || op == OR || op == XOR) {
2016 			/*
2017 			 * Print a warning if bits which were set are
2018 			 * lost due to the conversion.
2019 			 * This can happen with operator ORASS only.
2020 			 */
2021 			if (nsz < osz && (v->v_quad & xmask) != 0) {
2022 				/* constant truncated by conv., op %s */
2023 				warning(306, modtab[op].m_name);
2024 			}
2025 		} else if (op == ANDASS || op == AND) {
2026 			/*
2027 			 * Print a warning if additional bits are not all 1
2028 			 * and the most significant bit of the old value is 1,
2029 			 * or if at least one (but not all) removed bit was 0.
2030 			 */
2031 			if (nsz > osz &&
2032 			    (nv->v_quad & qbmasks[osz - 1]) != 0 &&
2033 			    (nv->v_quad & xmask) != xmask) {
2034 				/*
2035 				 * extra bits set to 0 in conversion
2036 				 * of '%s' to '%s', op %s
2037 				 */
2038 				warning(309, tyname(lbuf, sizeof(lbuf),
2039 				    gettyp(ot)), tyname(rbuf, sizeof(rbuf), tp),
2040 				    modtab[op].m_name);
2041 			} else if (nsz < osz &&
2042 				   (v->v_quad & xmask) != xmask &&
2043 				   (v->v_quad & xmask) != 0) {
2044 				/* const. truncated by conv., op %s */
2045 				warning(306, modtab[op].m_name);
2046 			}
2047 		} else if ((nt != PTR && isutyp(nt)) &&
2048 			   (ot != PTR && !isutyp(ot)) && v->v_quad < 0) {
2049 			if (op == ASSIGN) {
2050 				/* assignment of negative constant to ... */
2051 				warning(164);
2052 			} else if (op == INIT) {
2053 				/* initialisation of unsigned with neg. ... */
2054 				warning(221);
2055 			} else if (op == FARG) {
2056 				/* conversion of neg. const. to ..., arg #%d */
2057 				warning(296, arg);
2058 			} else if (modtab[op].m_comp) {
2059 				/* we get this warning already in chkcomp() */
2060 			} else {
2061 				/* conversion of negative constant to ... */
2062 				warning(222);
2063 			}
2064 		} else if (nv->v_quad != v->v_quad && nsz <= osz &&
2065 			   (v->v_quad & xmask) != 0 &&
2066 			   (isutyp(ot) || (v->v_quad & xmsk1) != xmsk1)) {
2067 			/*
2068 			 * Loss of significant bit(s). All truncated bits
2069 			 * of unsigned types or all truncated bits plus the
2070 			 * msb of the target for signed types are considered
2071 			 * to be significant bits. Loss of significant bits
2072 			 * means that at least on of the bits was set in an
2073 			 * unsigned type or that at least one, but not all of
2074 			 * the bits was set in an signed type.
2075 			 * Loss of significant bits means that it is not
2076 			 * possible, also not with necessary casts, to convert
2077 			 * back to the original type. A example for a
2078 			 * necessary cast is:
2079 			 *	char c;	int	i; c = 128;
2080 			 *	i = c;			** yields -128 **
2081 			 *	i = (unsigned char)c;	** yields 128 **
2082 			 */
2083 			if (op == ASSIGN && tp->t_isfield) {
2084 				/* precision lost in bit-field assignment */
2085 				warning(166);
2086 			} else if (op == ASSIGN) {
2087 				/* constant truncated by assignment */
2088 				warning(165);
2089 			} else if (op == INIT && tp->t_isfield) {
2090 				/* bit-field initializer does not fit */
2091 				warning(180);
2092 			} else if (op == INIT) {
2093 				/* initializer does not fit */
2094 				warning(178);
2095 			} else if (op == CASE) {
2096 				/* case label affected by conversion */
2097 				warning(196);
2098 			} else if (op == FARG) {
2099 				/* conv. of %s to %s is out of rng., arg #%d */
2100 				warning(295, tyname(lbuf, sizeof(lbuf),
2101 				    gettyp(ot)), tyname(rbuf, sizeof(rbuf), tp),
2102 				    arg);
2103 			} else {
2104 				/* conversion of %s to %s is out of range */
2105 				warning(119, tyname(lbuf, sizeof(lbuf),
2106 				    gettyp(ot)),
2107 				    tyname(rbuf, sizeof(rbuf), tp));
2108 			}
2109 		} else if (nv->v_quad != v->v_quad) {
2110 			if (op == ASSIGN && tp->t_isfield) {
2111 				/* precision lost in bit-field assignment */
2112 				warning(166);
2113 			} else if (op == INIT && tp->t_isfield) {
2114 				/* bit-field initializer out of range */
2115 				warning(11);
2116 			} else if (op == CASE) {
2117 				/* case label affected by conversion */
2118 				warning(196);
2119 			} else if (op == FARG) {
2120 				/* conv. of %s to %s is out of rng., arg #%d */
2121 				warning(295, tyname(lbuf, sizeof(lbuf),
2122 				    gettyp(ot)), tyname(rbuf, sizeof(rbuf), tp),
2123 				    arg);
2124 			} else {
2125 				/* conversion of %s to %s is out of range */
2126 				warning(119, tyname(lbuf, sizeof(lbuf),
2127 				    gettyp(ot)),
2128 				    tyname(rbuf, sizeof(rbuf), tp));
2129 			}
2130 		}
2131 	}
2132 }
2133 
2134 /*
2135  * Called if incompatible types were detected.
2136  * Prints a appropriate warning.
2137  */
2138 static void
2139 incompat(op_t op, tspec_t lt, tspec_t rt)
2140 {
2141 	mod_t	*mp;
2142 
2143 	mp = &modtab[op];
2144 
2145 	if (lt == VOID || (mp->m_binary && rt == VOID)) {
2146 		/* void type illegal in expression */
2147 		error(109);
2148 	} else if (op == ASSIGN) {
2149 		if ((lt == STRUCT || lt == UNION) &&
2150 		    (rt == STRUCT || rt == UNION)) {
2151 			/* assignment of different structures */
2152 			error(240);
2153 		} else {
2154 			/* assignment type mismatch */
2155 			error(171);
2156 		}
2157 	} else if (mp->m_binary) {
2158 		/* operands of %s have incompatible types */
2159 		error(107, mp->m_name);
2160 	} else {
2161 		/* operand of %s has incompatible type */
2162 		error(108, mp->m_name);
2163 	}
2164 }
2165 
2166 /*
2167  * Called if incompatible pointer types are detected.
2168  * Print an appropriate warning.
2169  */
2170 static void
2171 illptrc(mod_t *mp, type_t *ltp, type_t *rtp)
2172 {
2173 	tspec_t	lt, rt;
2174 
2175 	if (ltp->t_tspec != PTR || rtp->t_tspec != PTR)
2176 		LERROR("illptrc()");
2177 
2178 	lt = ltp->t_subt->t_tspec;
2179 	rt = rtp->t_subt->t_tspec;
2180 
2181 	if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION)) {
2182 		if (mp == NULL) {
2183 			/* illegal structure pointer combination */
2184 			warning(244);
2185 		} else {
2186 			/* illegal structure pointer combination, op %s */
2187 			warning(245, mp->m_name);
2188 		}
2189 	} else {
2190 		if (mp == NULL) {
2191 			/* illegal pointer combination */
2192 			warning(184);
2193 		} else {
2194 			/* illegal pointer combination, op %s */
2195 			warning(124, mp->m_name);
2196 		}
2197 	}
2198 }
2199 
2200 /*
2201  * Make sure type (*tpp)->t_subt has at least the qualifiers
2202  * of tp1->t_subt and tp2->t_subt.
2203  */
2204 static void
2205 mrgqual(type_t **tpp, type_t *tp1, type_t *tp2)
2206 {
2207 
2208 	if ((*tpp)->t_tspec != PTR ||
2209 	    tp1->t_tspec != PTR || tp2->t_tspec != PTR) {
2210 		LERROR("mrgqual()");
2211 	}
2212 
2213 	if ((*tpp)->t_subt->t_const ==
2214 	    (tp1->t_subt->t_const | tp2->t_subt->t_const) &&
2215 	    (*tpp)->t_subt->t_volatile ==
2216 	    (tp1->t_subt->t_volatile | tp2->t_subt->t_volatile)) {
2217 		return;
2218 	}
2219 
2220 	*tpp = tduptyp(*tpp);
2221 	(*tpp)->t_subt = tduptyp((*tpp)->t_subt);
2222 	(*tpp)->t_subt->t_const =
2223 		tp1->t_subt->t_const | tp2->t_subt->t_const;
2224 	(*tpp)->t_subt->t_volatile =
2225 		tp1->t_subt->t_volatile | tp2->t_subt->t_volatile;
2226 }
2227 
2228 /*
2229  * Returns 1 if the given structure or union has a constant member
2230  * (maybe recursively).
2231  */
2232 static int
2233 conmemb(type_t *tp)
2234 {
2235 	sym_t	*m;
2236 	tspec_t	t;
2237 
2238 	if ((t = tp->t_tspec) != STRUCT && t != UNION)
2239 		LERROR("conmemb()");
2240 	for (m = tp->t_str->memb; m != NULL; m = m->s_nxt) {
2241 		tp = m->s_type;
2242 		if (tp->t_const)
2243 			return (1);
2244 		if ((t = tp->t_tspec) == STRUCT || t == UNION) {
2245 			if (conmemb(m->s_type))
2246 				return (1);
2247 		}
2248 	}
2249 	return (0);
2250 }
2251 
2252 const char *
2253 basictyname(tspec_t t)
2254 {
2255 	switch (t) {
2256 	case CHAR:	return "char";
2257 	case UCHAR:	return "unsigned char";
2258 	case SCHAR:	return "signed char";
2259 	case SHORT:	return "short";
2260 	case USHORT:	return "unsigned short";
2261 	case INT:	return "int";
2262 	case UINT:	return "unsigned int";
2263 	case LONG:	return "long";
2264 	case ULONG:	return "unsigned long";
2265 	case QUAD:	return "long long";
2266 	case UQUAD:	return "unsigned long long";
2267 	case FLOAT:	return "float";
2268 	case DOUBLE:	return "double";
2269 	case LDOUBLE:	return "long double";
2270 	case PTR:	return "pointer";
2271 	case ENUM:	return "enum";
2272 	case STRUCT:	return "struct";
2273 	case UNION:	return "union";
2274 	case FUNC:	return "function";
2275 	case ARRAY:	return "array";
2276 	default:
2277 		LERROR("basictyname()");
2278 		return NULL;
2279 	}
2280 }
2281 
2282 const char *
2283 tyname(char *buf, size_t bufsiz, type_t *tp)
2284 {
2285 	tspec_t	t;
2286 	const	char *s;
2287 	char lbuf[64];
2288 
2289 	if ((t = tp->t_tspec) == INT && tp->t_isenum)
2290 		t = ENUM;
2291 
2292 	s = basictyname(t);
2293 
2294 
2295 	switch (t) {
2296 	case CHAR:
2297 	case UCHAR:
2298 	case SCHAR:
2299 	case SHORT:
2300 	case USHORT:
2301 	case INT:
2302 	case UINT:
2303 	case LONG:
2304 	case ULONG:
2305 	case QUAD:
2306 	case UQUAD:
2307 	case FLOAT:
2308 	case DOUBLE:
2309 	case LDOUBLE:
2310 	case FUNC:
2311 		(void)snprintf(buf, bufsiz, "%s", s);
2312 		break;
2313 	case PTR:
2314 		(void)snprintf(buf, bufsiz, "%s to %s", s,
2315 		    tyname(lbuf, sizeof(lbuf), tp->t_subt));
2316 		break;
2317 	case ENUM:
2318 		(void)snprintf(buf, bufsiz, "%s %s", s,
2319 		    tp->t_enum->etag->s_name);
2320 		break;
2321 	case STRUCT:
2322 	case UNION:
2323 		(void)snprintf(buf, bufsiz, "%s %s", s,
2324 		    tp->t_str->stag->s_name);
2325 		break;
2326 	case ARRAY:
2327 		(void)snprintf(buf, bufsiz, "%s of %s[%d]", s,
2328 		    tyname(lbuf, sizeof(lbuf), tp->t_subt), tp->t_dim);
2329 		break;
2330 	default:
2331 		LERROR("tyname()");
2332 	}
2333 	return (buf);
2334 }
2335 
2336 /*
2337  * Create a new node for one of the operators POINT and ARROW.
2338  */
2339 static tnode_t *
2340 bldstr(op_t op, tnode_t *ln, tnode_t *rn)
2341 {
2342 	tnode_t	*ntn, *ctn;
2343 	int	nolval;
2344 
2345 	if (rn->tn_op != NAME)
2346 		LERROR("bldstr()");
2347 	if (rn->tn_sym->s_value.v_tspec != INT)
2348 		LERROR("bldstr()");
2349 	if (rn->tn_sym->s_scl != MOS && rn->tn_sym->s_scl != MOU)
2350 		LERROR("bldstr()");
2351 
2352 	/*
2353 	 * Remember if the left operand is an lvalue (structure members
2354 	 * are lvalues if and only if the structure itself is an lvalue).
2355 	 */
2356 	nolval = op == POINT && !ln->tn_lvalue;
2357 
2358 	if (op == POINT) {
2359 		ln = bldamper(ln, 1);
2360 	} else if (ln->tn_type->t_tspec != PTR) {
2361 		if (!tflag || !isityp(ln->tn_type->t_tspec))
2362 			LERROR("bldstr()");
2363 		ln = convert(NOOP, 0, tincref(gettyp(VOID), PTR), ln);
2364 	}
2365 
2366 #if PTRDIFF_IS_LONG
2367 	ctn = getinode(LONG, rn->tn_sym->s_value.v_quad / CHAR_BIT);
2368 #else
2369 	ctn = getinode(INT, rn->tn_sym->s_value.v_quad / CHAR_BIT);
2370 #endif
2371 
2372 	ntn = mktnode(PLUS, tincref(rn->tn_type, PTR), ln, ctn);
2373 	if (ln->tn_op == CON)
2374 		ntn = fold(ntn);
2375 
2376 	if (rn->tn_type->t_isfield) {
2377 		ntn = mktnode(FSEL, ntn->tn_type->t_subt, ntn, NULL);
2378 	} else {
2379 		ntn = mktnode(STAR, ntn->tn_type->t_subt, ntn, NULL);
2380 	}
2381 
2382 	if (nolval)
2383 		ntn->tn_lvalue = 0;
2384 
2385 	return (ntn);
2386 }
2387 
2388 /*
2389  * Create a node for INCAFT, INCBEF, DECAFT and DECBEF.
2390  */
2391 static tnode_t *
2392 bldincdec(op_t op, tnode_t *ln)
2393 {
2394 	tnode_t	*cn, *ntn;
2395 
2396 	if (ln == NULL)
2397 		LERROR("bldincdec()");
2398 
2399 	if (ln->tn_type->t_tspec == PTR) {
2400 		cn = plength(ln->tn_type);
2401 	} else {
2402 		cn = getinode(INT, (int64_t)1);
2403 	}
2404 	ntn = mktnode(op, ln->tn_type, ln, cn);
2405 
2406 	return (ntn);
2407 }
2408 
2409 /*
2410  * Create a tree node for the & operator
2411  */
2412 static tnode_t *
2413 bldamper(tnode_t *tn, int noign)
2414 {
2415 	tnode_t	*ntn;
2416 	tspec_t	t;
2417 
2418 	if (!noign && ((t = tn->tn_type->t_tspec) == ARRAY || t == FUNC)) {
2419 		/* & before array or function: ignored */
2420 		if (tflag)
2421 			warning(127);
2422 		return (tn);
2423 	}
2424 
2425 	/* eliminate &* */
2426 	if (tn->tn_op == STAR &&
2427 	    tn->tn_left->tn_type->t_tspec == PTR &&
2428 	    tn->tn_left->tn_type->t_subt == tn->tn_type) {
2429 		return (tn->tn_left);
2430 	}
2431 
2432 	ntn = mktnode(AMPER, tincref(tn->tn_type, PTR), tn, NULL);
2433 
2434 	return (ntn);
2435 }
2436 
2437 /*
2438  * Create a node for operators PLUS and MINUS.
2439  */
2440 static tnode_t *
2441 bldplmi(op_t op, tnode_t *ln, tnode_t *rn)
2442 {
2443 	tnode_t	*ntn, *ctn;
2444 	type_t	*tp;
2445 
2446 	/* If pointer and integer, then pointer to the lhs. */
2447 	if (rn->tn_type->t_tspec == PTR && isityp(ln->tn_type->t_tspec)) {
2448 		ntn = ln;
2449 		ln = rn;
2450 		rn = ntn;
2451 	}
2452 
2453 	if (ln->tn_type->t_tspec == PTR && rn->tn_type->t_tspec != PTR) {
2454 
2455 		if (!isityp(rn->tn_type->t_tspec))
2456 			LERROR("bldplmi()");
2457 
2458 		ctn = plength(ln->tn_type);
2459 		if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
2460 			rn = convert(NOOP, 0, ctn->tn_type, rn);
2461 		rn = mktnode(MULT, rn->tn_type, rn, ctn);
2462 		if (rn->tn_left->tn_op == CON)
2463 			rn = fold(rn);
2464 		ntn = mktnode(op, ln->tn_type, ln, rn);
2465 
2466 	} else if (rn->tn_type->t_tspec == PTR) {
2467 
2468 		if (ln->tn_type->t_tspec != PTR || op != MINUS)
2469 			LERROR("bldplmi()");
2470 #if PTRDIFF_IS_LONG
2471 		tp = gettyp(LONG);
2472 #else
2473 		tp = gettyp(INT);
2474 #endif
2475 		ntn = mktnode(op, tp, ln, rn);
2476 		if (ln->tn_op == CON && rn->tn_op == CON)
2477 			ntn = fold(ntn);
2478 		ctn = plength(ln->tn_type);
2479 		balance(NOOP, &ntn, &ctn);
2480 		ntn = mktnode(DIV, tp, ntn, ctn);
2481 
2482 	} else {
2483 
2484 		ntn = mktnode(op, ln->tn_type, ln, rn);
2485 
2486 	}
2487 	return (ntn);
2488 }
2489 
2490 /*
2491  * Create a node for operators SHL and SHR.
2492  */
2493 static tnode_t *
2494 bldshft(op_t op, tnode_t *ln, tnode_t *rn)
2495 {
2496 	tspec_t	t;
2497 	tnode_t	*ntn;
2498 
2499 	if ((t = rn->tn_type->t_tspec) != INT && t != UINT)
2500 		rn = convert(CVT, 0, gettyp(INT), rn);
2501 	ntn = mktnode(op, ln->tn_type, ln, rn);
2502 	return (ntn);
2503 }
2504 
2505 /*
2506  * Create a node for COLON.
2507  */
2508 static tnode_t *
2509 bldcol(tnode_t *ln, tnode_t *rn)
2510 {
2511 	tspec_t	lt, rt, pdt;
2512 	type_t	*rtp;
2513 	tnode_t	*ntn;
2514 
2515 	lt = ln->tn_type->t_tspec;
2516 	rt = rn->tn_type->t_tspec;
2517 #if PTRDIFF_IS_LONG
2518 	pdt = LONG;
2519 #else
2520 	pdt = INT;
2521 #endif
2522 
2523 	/*
2524 	 * Arithmetic types are balanced, all other type combinations
2525 	 * still need to be handled.
2526 	 */
2527 	if (isatyp(lt) && isatyp(rt)) {
2528 		rtp = ln->tn_type;
2529 	} else if (lt == VOID || rt == VOID) {
2530 		rtp = gettyp(VOID);
2531 	} else if (lt == STRUCT || lt == UNION) {
2532 		/* Both types must be identical. */
2533 		if (rt != STRUCT && rt != UNION)
2534 			LERROR("bldcol()");
2535 		if (ln->tn_type->t_str != rn->tn_type->t_str)
2536 			LERROR("bldcol()");
2537 		if (incompl(ln->tn_type)) {
2538 			/* unknown operand size, op %s */
2539 			error(138, modtab[COLON].m_name);
2540 			return (NULL);
2541 		}
2542 		rtp = ln->tn_type;
2543 	} else if (lt == PTR && isityp(rt)) {
2544 		if (rt != pdt) {
2545 			rn = convert(NOOP, 0, gettyp(pdt), rn);
2546 			rt = pdt;
2547 		}
2548 		rtp = ln->tn_type;
2549 	} else if (rt == PTR && isityp(lt)) {
2550 		if (lt != pdt) {
2551 			ln = convert(NOOP, 0, gettyp(pdt), ln);
2552 			lt = pdt;
2553 		}
2554 		rtp = rn->tn_type;
2555 	} else if (lt == PTR && ln->tn_type->t_subt->t_tspec == VOID) {
2556 		if (rt != PTR)
2557 			LERROR("bldcol()");
2558 		rtp = ln->tn_type;
2559 		mrgqual(&rtp, ln->tn_type, rn->tn_type);
2560 	} else if (rt == PTR && rn->tn_type->t_subt->t_tspec == VOID) {
2561 		if (lt != PTR)
2562 			LERROR("bldcol()");
2563 		rtp = rn->tn_type;
2564 		mrgqual(&rtp, ln->tn_type, rn->tn_type);
2565 	} else {
2566 		if (lt != PTR || rt != PTR)
2567 			LERROR("bldcol()");
2568 		/*
2569 		 * XXX For now we simply take the left type. This is
2570 		 * probably wrong, if one type contains a functionprototype
2571 		 * and the other one, at the same place, only an old style
2572 		 * declaration.
2573 		 */
2574 		rtp = ln->tn_type;
2575 		mrgqual(&rtp, ln->tn_type, rn->tn_type);
2576 	}
2577 
2578 	ntn = mktnode(COLON, rtp, ln, rn);
2579 
2580 	return (ntn);
2581 }
2582 
2583 /*
2584  * Create a node for an assignment operator (both = and op= ).
2585  */
2586 static tnode_t *
2587 bldasgn(op_t op, tnode_t *ln, tnode_t *rn)
2588 {
2589 	tspec_t	lt, rt;
2590 	tnode_t	*ntn, *ctn;
2591 
2592 	if (ln == NULL || rn == NULL)
2593 		LERROR("bldasgn()");
2594 
2595 	lt = ln->tn_type->t_tspec;
2596 	rt = rn->tn_type->t_tspec;
2597 
2598 	if ((op == ADDASS || op == SUBASS) && lt == PTR) {
2599 		if (!isityp(rt))
2600 			LERROR("bldasgn()");
2601 		ctn = plength(ln->tn_type);
2602 		if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
2603 			rn = convert(NOOP, 0, ctn->tn_type, rn);
2604 		rn = mktnode(MULT, rn->tn_type, rn, ctn);
2605 		if (rn->tn_left->tn_op == CON)
2606 			rn = fold(rn);
2607 	}
2608 
2609 	if ((op == ASSIGN || op == RETURN) && (lt == STRUCT || rt == STRUCT)) {
2610 		if (rt != lt || ln->tn_type->t_str != rn->tn_type->t_str)
2611 			LERROR("bldasgn()");
2612 		if (incompl(ln->tn_type)) {
2613 			if (op == RETURN) {
2614 				/* cannot return incomplete type */
2615 				error(212);
2616 			} else {
2617 				/* unknown operand size, op %s */
2618 				error(138, modtab[op].m_name);
2619 			}
2620 			return (NULL);
2621 		}
2622 	}
2623 
2624 	if (op == SHLASS || op == SHRASS) {
2625 		if (rt != INT) {
2626 			rn = convert(NOOP, 0, gettyp(INT), rn);
2627 			rt = INT;
2628 		}
2629 	} else {
2630 		if (op == ASSIGN || lt != PTR) {
2631 			if (lt != rt ||
2632 			    (ln->tn_type->t_isfield && rn->tn_op == CON)) {
2633 				rn = convert(op, 0, ln->tn_type, rn);
2634 				rt = lt;
2635 			}
2636 		}
2637 	}
2638 
2639 	ntn = mktnode(op, ln->tn_type, ln, rn);
2640 
2641 	return (ntn);
2642 }
2643 
2644 /*
2645  * Get length of type tp->t_subt.
2646  */
2647 static tnode_t *
2648 plength(type_t *tp)
2649 {
2650 	int	elem, elsz;
2651 	tspec_t	st;
2652 
2653 	if (tp->t_tspec != PTR)
2654 		LERROR("plength()");
2655 	tp = tp->t_subt;
2656 
2657 	elem = 1;
2658 	elsz = 0;
2659 
2660 	while (tp->t_tspec == ARRAY) {
2661 		elem *= tp->t_dim;
2662 		tp = tp->t_subt;
2663 	}
2664 
2665 	switch (tp->t_tspec) {
2666 	case FUNC:
2667 		/* pointer to function is not allowed here */
2668 		error(110);
2669 		break;
2670 	case VOID:
2671 		/* cannot do pointer arithmetic on operand of ... */
2672 		(void)gnuism(136);
2673 		break;
2674 	case STRUCT:
2675 	case UNION:
2676 		if ((elsz = tp->t_str->size) == 0)
2677 			/* cannot do pointer arithmetic on operand of ... */
2678 			error(136);
2679 		break;
2680 	case ENUM:
2681 		if (incompl(tp)) {
2682 			/* cannot do pointer arithmetic on operand of ... */
2683 			warning(136);
2684 		}
2685 		/* FALLTHROUGH */
2686 	default:
2687 		if ((elsz = size(tp->t_tspec)) == 0) {
2688 			/* cannot do pointer arithmetic on operand of ... */
2689 			error(136);
2690 		} else if (elsz == -1) {
2691 			LERROR("plength()");
2692 		}
2693 		break;
2694 	}
2695 
2696 	if (elem == 0 && elsz != 0) {
2697 		/* cannot do pointer arithmetic on operand of ... */
2698 		error(136);
2699 	}
2700 
2701 	if (elsz == 0)
2702 		elsz = CHAR_BIT;
2703 
2704 #if PTRDIFF_IS_LONG
2705 	st = LONG;
2706 #else
2707 	st = INT;
2708 #endif
2709 
2710 	return (getinode(st, (int64_t)(elem * elsz / CHAR_BIT)));
2711 }
2712 
2713 /*
2714  * XXX
2715  * Note: There appear to be a number of bugs in detecting overflow in
2716  * this function. An audit and a set of proper regression tests are needed.
2717  *     --Perry Metzger, Nov. 16, 2001
2718  */
2719 /*
2720  * Do only as much as necessary to compute constant expressions.
2721  * Called only if the operator allows folding and (both) operands
2722  * are constants.
2723  */
2724 static tnode_t *
2725 fold(tnode_t *tn)
2726 {
2727 	val_t	*v;
2728 	tspec_t	t;
2729 	int	utyp, ovfl;
2730 	int64_t	sl, sr = 0, q = 0, mask;
2731 	uint64_t ul, ur = 0;
2732 	tnode_t	*cn;
2733 
2734 	v = xcalloc(1, sizeof (val_t));
2735 	v->v_tspec = t = tn->tn_type->t_tspec;
2736 
2737 	utyp = t == PTR || isutyp(t);
2738 	ul = sl = tn->tn_left->tn_val->v_quad;
2739 	if (modtab[tn->tn_op].m_binary)
2740 		ur = sr = tn->tn_right->tn_val->v_quad;
2741 
2742 	mask = qlmasks[size(t)];
2743 	ovfl = 0;
2744 
2745 	switch (tn->tn_op) {
2746 	case UPLUS:
2747 		q = sl;
2748 		break;
2749 	case UMINUS:
2750 		q = -sl;
2751 		if (msb(q, t, -1) == msb(sl, t, -1))
2752 			ovfl = 1;
2753 		break;
2754 	case COMPL:
2755 		q = ~sl;
2756 		break;
2757 	case MULT:
2758 		if (utyp) {
2759 			q = ul * ur;
2760 			if (q != (q & mask))
2761 				ovfl = 1;
2762 			else if ((ul != 0) && ((q / ul) != ur))
2763 				ovfl = 1;
2764 		} else {
2765 			q = sl * sr;
2766 			if (msb(q, t, -1) != (msb(sl, t, -1) ^ msb(sr, t, -1)))
2767 				ovfl = 1;
2768 		}
2769 		break;
2770 	case DIV:
2771 		if (sr == 0) {
2772 			/* division by 0 */
2773 			error(139);
2774 			q = utyp ? UQUAD_MAX : QUAD_MAX;
2775 		} else {
2776 			q = utyp ? ul / ur : sl / sr;
2777 		}
2778 		break;
2779 	case MOD:
2780 		if (sr == 0) {
2781 			/* modulus by 0 */
2782 			error(140);
2783 			q = 0;
2784 		} else {
2785 			q = utyp ? ul % ur : sl % sr;
2786 		}
2787 		break;
2788 	case PLUS:
2789 		q = utyp ? ul + ur : sl + sr;
2790 		if (msb(sl, t, -1)  != 0 && msb(sr, t, -1) != 0) {
2791 			if (msb(q, t, -1) == 0)
2792 				ovfl = 1;
2793 		} else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) == 0) {
2794 			if (msb(q, t, -1) != 0)
2795 				ovfl = 1;
2796 		}
2797 		break;
2798 	case MINUS:
2799 		q = utyp ? ul - ur : sl - sr;
2800 		if (msb(sl, t, -1) != 0 && msb(sr, t, -1) == 0) {
2801 			if (msb(q, t, -1) == 0)
2802 				ovfl = 1;
2803 		} else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) != 0) {
2804 			if (msb(q, t, -1) != 0)
2805 				ovfl = 1;
2806 		}
2807 		break;
2808 	case SHL:
2809 		q = utyp ? ul << sr : sl << sr;
2810 		break;
2811 	case SHR:
2812 		/*
2813 		 * The sign must be explicitly extended because
2814 		 * shifts of signed values are implementation dependent.
2815 		 */
2816 		q = ul >> sr;
2817 		q = xsign(q, t, size(t) - (int)sr);
2818 		break;
2819 	case LT:
2820 		q = utyp ? ul < ur : sl < sr;
2821 		break;
2822 	case LE:
2823 		q = utyp ? ul <= ur : sl <= sr;
2824 		break;
2825 	case GE:
2826 		q = utyp ? ul >= ur : sl >= sr;
2827 		break;
2828 	case GT:
2829 		q = utyp ? ul > ur : sl > sr;
2830 		break;
2831 	case EQ:
2832 		q = utyp ? ul == ur : sl == sr;
2833 		break;
2834 	case NE:
2835 		q = utyp ? ul != ur : sl != sr;
2836 		break;
2837 	case AND:
2838 		q = utyp ? ul & ur : sl & sr;
2839 		break;
2840 	case XOR:
2841 		q = utyp ? ul ^ ur : sl ^ sr;
2842 		break;
2843 	case OR:
2844 		q = utyp ? ul | ur : sl | sr;
2845 		break;
2846 	default:
2847 		LERROR("fold()");
2848 	}
2849 
2850 	/* XXX does not work for quads. */
2851 	if (ovfl || ((q | mask) != ~(uint64_t)0 && (q & ~mask) != 0)) {
2852 		if (hflag)
2853 			/* integer overflow detected, op %s */
2854 			warning(141, modtab[tn->tn_op].m_name);
2855 	}
2856 
2857 	v->v_quad = xsign(q, t, -1);
2858 
2859 	cn = getcnode(tn->tn_type, v);
2860 
2861 	return (cn);
2862 }
2863 
2864 /*
2865  * Same for operators whose operands are compared with 0 (test context).
2866  */
2867 static tnode_t *
2868 foldtst(tnode_t *tn)
2869 {
2870 	int	l, r = 0;
2871 	val_t	*v;
2872 
2873 	v = xcalloc(1, sizeof (val_t));
2874 	v->v_tspec = tn->tn_type->t_tspec;
2875 	if (tn->tn_type->t_tspec != INT)
2876 		LERROR("foldtst()");
2877 
2878 	if (isftyp(tn->tn_left->tn_type->t_tspec)) {
2879 		l = tn->tn_left->tn_val->v_ldbl != 0.0;
2880 	} else {
2881 		l = tn->tn_left->tn_val->v_quad != 0;
2882 	}
2883 
2884 	if (modtab[tn->tn_op].m_binary) {
2885 		if (isftyp(tn->tn_right->tn_type->t_tspec)) {
2886 			r = tn->tn_right->tn_val->v_ldbl != 0.0;
2887 		} else {
2888 			r = tn->tn_right->tn_val->v_quad != 0;
2889 		}
2890 	}
2891 
2892 	switch (tn->tn_op) {
2893 	case NOT:
2894 		if (hflag)
2895 			/* constant argument to NOT */
2896 			warning(239);
2897 		v->v_quad = !l;
2898 		break;
2899 	case LOGAND:
2900 		v->v_quad = l && r;
2901 		break;
2902 	case LOGOR:
2903 		v->v_quad = l || r;
2904 		break;
2905 	default:
2906 		LERROR("foldtst()");
2907 	}
2908 
2909 	return (getcnode(tn->tn_type, v));
2910 }
2911 
2912 /*
2913  * Same for operands with floating point type.
2914  */
2915 static tnode_t *
2916 foldflt(tnode_t *tn)
2917 {
2918 	val_t	*v;
2919 	tspec_t	t;
2920 	ldbl_t	l, r = 0;
2921 
2922 	v = xcalloc(1, sizeof (val_t));
2923 	v->v_tspec = t = tn->tn_type->t_tspec;
2924 
2925 	if (!isftyp(t))
2926 		LERROR("foldflt()");
2927 
2928 	if (t != tn->tn_left->tn_type->t_tspec)
2929 		LERROR("foldflt()");
2930 	if (modtab[tn->tn_op].m_binary && t != tn->tn_right->tn_type->t_tspec)
2931 		LERROR("foldflt()");
2932 
2933 	l = tn->tn_left->tn_val->v_ldbl;
2934 	if (modtab[tn->tn_op].m_binary)
2935 		r = tn->tn_right->tn_val->v_ldbl;
2936 
2937 	switch (tn->tn_op) {
2938 	case UPLUS:
2939 		v->v_ldbl = l;
2940 		break;
2941 	case UMINUS:
2942 		v->v_ldbl = -l;
2943 		break;
2944 	case MULT:
2945 		v->v_ldbl = l * r;
2946 		break;
2947 	case DIV:
2948 		if (r == 0.0) {
2949 			/* division by 0 */
2950 			error(139);
2951 			if (t == FLOAT) {
2952 				v->v_ldbl = l < 0 ? -FLT_MAX : FLT_MAX;
2953 			} else if (t == DOUBLE) {
2954 				v->v_ldbl = l < 0 ? -DBL_MAX : DBL_MAX;
2955 			} else {
2956 				v->v_ldbl = l < 0 ? -LDBL_MAX : LDBL_MAX;
2957 			}
2958 		} else {
2959 			v->v_ldbl = l / r;
2960 		}
2961 		break;
2962 	case PLUS:
2963 		v->v_ldbl = l + r;
2964 		break;
2965 	case MINUS:
2966 		v->v_ldbl = l - r;
2967 		break;
2968 	case LT:
2969 		v->v_quad = l < r;
2970 		break;
2971 	case LE:
2972 		v->v_quad = l <= r;
2973 		break;
2974 	case GE:
2975 		v->v_quad = l >= r;
2976 		break;
2977 	case GT:
2978 		v->v_quad = l > r;
2979 		break;
2980 	case EQ:
2981 		v->v_quad = l == r;
2982 		break;
2983 	case NE:
2984 		v->v_quad = l != r;
2985 		break;
2986 	default:
2987 		LERROR("foldflt()");
2988 	}
2989 
2990 	if (isnan((double)v->v_ldbl))
2991 		LERROR("foldflt()");
2992 	if (!finite((double)v->v_ldbl) ||
2993 	    (t == FLOAT &&
2994 	     (v->v_ldbl > FLT_MAX || v->v_ldbl < -FLT_MAX)) ||
2995 	    (t == DOUBLE &&
2996 	     (v->v_ldbl > DBL_MAX || v->v_ldbl < -DBL_MAX))) {
2997 		/* floating point overflow detected, op %s */
2998 		warning(142, modtab[tn->tn_op].m_name);
2999 		if (t == FLOAT) {
3000 			v->v_ldbl = v->v_ldbl < 0 ? -FLT_MAX : FLT_MAX;
3001 		} else if (t == DOUBLE) {
3002 			v->v_ldbl = v->v_ldbl < 0 ? -DBL_MAX : DBL_MAX;
3003 		} else {
3004 			v->v_ldbl = v->v_ldbl < 0 ? -LDBL_MAX: LDBL_MAX;
3005 		}
3006 	}
3007 
3008 	return (getcnode(tn->tn_type, v));
3009 }
3010 
3011 /*
3012  * Create a constant node for sizeof.
3013  */
3014 tnode_t *
3015 bldszof(type_t *tp)
3016 {
3017 	int	elem, elsz;
3018 	tspec_t	st;
3019 
3020 	elem = 1;
3021 	while (tp->t_tspec == ARRAY) {
3022 		elem *= tp->t_dim;
3023 		tp = tp->t_subt;
3024 	}
3025 	if (elem == 0) {
3026 		/* cannot take size of incomplete type */
3027 		error(143);
3028 		elem = 1;
3029 	}
3030 	switch (tp->t_tspec) {
3031 	case FUNC:
3032 		/* cannot take size of function */
3033 		error(144);
3034 		elsz = 1;
3035 		break;
3036 	case STRUCT:
3037 	case UNION:
3038 		if (incompl(tp)) {
3039 			/* cannot take size of incomplete type */
3040 			error(143);
3041 			elsz = 1;
3042 		} else {
3043 			elsz = tp->t_str->size;
3044 		}
3045 		break;
3046 	case ENUM:
3047 		if (incompl(tp)) {
3048 			/* cannot take size of incomplete type */
3049 			warning(143);
3050 		}
3051 		/* FALLTHROUGH */
3052 	default:
3053 		if (tp->t_isfield) {
3054 			/* cannot take size of bit-field */
3055 			error(145);
3056 		}
3057 		if (tp->t_tspec == VOID) {
3058 			/* cannot take size of void */
3059 			error(146);
3060 			elsz = 1;
3061 		} else {
3062 			elsz = size(tp->t_tspec);
3063 			if (elsz <= 0)
3064 				LERROR("bldszof()");
3065 		}
3066 		break;
3067 	}
3068 
3069 #if SIZEOF_IS_ULONG
3070 	st = ULONG;
3071 #else
3072 	st = UINT;
3073 #endif
3074 
3075 	return (getinode(st, (int64_t)(elem * elsz / CHAR_BIT)));
3076 }
3077 
3078 /*
3079  * Type casts.
3080  */
3081 tnode_t *
3082 cast(tnode_t *tn, type_t *tp)
3083 {
3084 	tspec_t	nt, ot;
3085 
3086 	if (tn == NULL)
3087 		return (NULL);
3088 
3089 	tn = cconv(tn);
3090 
3091 	nt = tp->t_tspec;
3092 	ot = tn->tn_type->t_tspec;
3093 
3094 	if (nt == VOID) {
3095 		/*
3096 		 * XXX ANSI C requires scalar types or void (Plauger&Brodie).
3097 		 * But this seams really questionable.
3098 		 */
3099 	} else if (nt == STRUCT || nt == UNION || nt == ARRAY || nt == FUNC) {
3100 		/* invalid cast expression */
3101 		error(147);
3102 		return (NULL);
3103 	} else if (ot == STRUCT || ot == UNION) {
3104 		/* invalid cast expression */
3105 		error(147);
3106 		return (NULL);
3107 	} else if (ot == VOID) {
3108 		/* improper cast of void expression */
3109 		error(148);
3110 		return (NULL);
3111 	} else if (isityp(nt) && issclt(ot)) {
3112 		/* ok */
3113 	} else if (isftyp(nt) && isatyp(ot)) {
3114 		/* ok */
3115 	} else if (nt == PTR && isityp(ot)) {
3116 		/* ok */
3117 	} else if (nt == PTR && ot == PTR) {
3118 		if (!tp->t_subt->t_const && tn->tn_type->t_subt->t_const) {
3119 			if (hflag)
3120 				/* cast discards 'const' from ... */
3121 				warning(275);
3122 		}
3123 	} else {
3124 		/* invalid cast expression */
3125 		error(147);
3126 		return (NULL);
3127 	}
3128 
3129 	tn = convert(CVT, 0, tp, tn);
3130 	tn->tn_cast = 1;
3131 
3132 	return (tn);
3133 }
3134 
3135 /*
3136  * Create the node for a function argument.
3137  * All necessary conversions and type checks are done in funccall(), because
3138  * in funcarg() we have no information about expected argument types.
3139  */
3140 tnode_t *
3141 funcarg(tnode_t *args, tnode_t *arg)
3142 {
3143 	tnode_t	*ntn;
3144 
3145 	/*
3146 	 * If there was a serious error in the expression for the argument,
3147 	 * create a dummy argument so the positions of the remaining arguments
3148 	 * will not change.
3149 	 */
3150 	if (arg == NULL)
3151 		arg = getinode(INT, (int64_t)0);
3152 
3153 	ntn = mktnode(PUSH, arg->tn_type, arg, args);
3154 
3155 	return (ntn);
3156 }
3157 
3158 /*
3159  * Create the node for a function call. Also check types of
3160  * function arguments and insert conversions, if necessary.
3161  */
3162 tnode_t *
3163 funccall(tnode_t *func, tnode_t *args)
3164 {
3165 	tnode_t	*ntn;
3166 	op_t	fcop;
3167 
3168 	if (func == NULL)
3169 		return (NULL);
3170 
3171 	if (func->tn_op == NAME && func->tn_type->t_tspec == FUNC) {
3172 		fcop = CALL;
3173 	} else {
3174 		fcop = ICALL;
3175 	}
3176 
3177 	/*
3178 	 * after cconv() func will always be a pointer to a function
3179 	 * if it is a valid function designator.
3180 	 */
3181 	func = cconv(func);
3182 
3183 	if (func->tn_type->t_tspec != PTR ||
3184 	    func->tn_type->t_subt->t_tspec != FUNC) {
3185 		/* illegal function */
3186 		error(149);
3187 		return (NULL);
3188 	}
3189 
3190 	args = chkfarg(func->tn_type->t_subt, args);
3191 
3192 	ntn = mktnode(fcop, func->tn_type->t_subt->t_subt, func, args);
3193 
3194 	return (ntn);
3195 }
3196 
3197 /*
3198  * Check types of all function arguments and insert conversions,
3199  * if necessary.
3200  */
3201 static tnode_t *
3202 chkfarg(type_t *ftp, tnode_t *args)
3203 {
3204 	tnode_t	*arg;
3205 	sym_t	*asym;
3206 	tspec_t	at;
3207 	int	narg, npar, n, i;
3208 
3209 	/* get # of args in the prototype */
3210 	npar = 0;
3211 	for (asym = ftp->t_args; asym != NULL; asym = asym->s_nxt)
3212 		npar++;
3213 
3214 	/* get # of args in function call */
3215 	narg = 0;
3216 	for (arg = args; arg != NULL; arg = arg->tn_right)
3217 		narg++;
3218 
3219 	asym = ftp->t_args;
3220 	if (ftp->t_proto && npar != narg && !(ftp->t_vararg && npar < narg)) {
3221 		/* argument mismatch: %d arg%s passed, %d expected */
3222 		error(150, narg, narg > 1 ? "s" : "", npar);
3223 		asym = NULL;
3224 	}
3225 
3226 	for (n = 1; n <= narg; n++) {
3227 
3228 		/*
3229 		 * The rightmost argument is at the top of the argument
3230 		 * subtree.
3231 		 */
3232 		for (i = narg, arg = args; i > n; i--, arg = arg->tn_right)
3233 			continue;
3234 
3235 		/* some things which are always not allowd */
3236 		if ((at = arg->tn_left->tn_type->t_tspec) == VOID) {
3237 			/* void expressions may not be arguments, arg #%d */
3238 			error(151, n);
3239 			return (NULL);
3240 		} else if ((at == STRUCT || at == UNION) &&
3241 			   incompl(arg->tn_left->tn_type)) {
3242 			/* argument cannot have unknown size, arg #%d */
3243 			error(152, n);
3244 			return (NULL);
3245 		} else if (isityp(at) && arg->tn_left->tn_type->t_isenum &&
3246 			   incompl(arg->tn_left->tn_type)) {
3247 			/* argument cannot have unknown size, arg #%d */
3248 			warning(152, n);
3249 		}
3250 
3251 		/* class conversions (arg in value context) */
3252 		arg->tn_left = cconv(arg->tn_left);
3253 
3254 		if (asym != NULL) {
3255 			arg->tn_left = parg(n, asym->s_type, arg->tn_left);
3256 		} else {
3257 			arg->tn_left = promote(NOOP, 1, arg->tn_left);
3258 		}
3259 		arg->tn_type = arg->tn_left->tn_type;
3260 
3261 		if (asym != NULL)
3262 			asym = asym->s_nxt;
3263 	}
3264 
3265 	return (args);
3266 }
3267 
3268 /*
3269  * Compare the type of an argument with the corresponding type of a
3270  * prototype parameter. If it is a valid combination, but both types
3271  * are not the same, insert a conversion to convert the argument into
3272  * the type of the parameter.
3273  */
3274 static tnode_t *
3275 parg(	int	n,		/* pos of arg */
3276 	type_t	*tp,		/* expected type (from prototype) */
3277 	tnode_t	*tn)		/* argument */
3278 {
3279 	tnode_t	*ln;
3280 	int	warn;
3281 
3282 	ln = xcalloc(1, sizeof (tnode_t));
3283 	ln->tn_type = tduptyp(tp);
3284 	ln->tn_type->t_const = 0;
3285 	ln->tn_lvalue = 1;
3286 	if (typeok(FARG, n, ln, tn)) {
3287 		if (!eqtype(tp, tn->tn_type, 1, 0, (warn = 0, &warn)) || warn)
3288 			tn = convert(FARG, n, tp, tn);
3289 	}
3290 	free(ln);
3291 	return (tn);
3292 }
3293 
3294 /*
3295  * Return the value of an integral constant expression.
3296  * If the expression is not constant or its type is not an integer
3297  * type, an error message is printed.
3298  */
3299 val_t *
3300 constant(tnode_t *tn, int required)
3301 {
3302 	val_t	*v;
3303 
3304 	if (tn != NULL)
3305 		tn = cconv(tn);
3306 	if (tn != NULL)
3307 		tn = promote(NOOP, 0, tn);
3308 
3309 	v = xcalloc(1, sizeof (val_t));
3310 
3311 	if (tn == NULL) {
3312 		if (nerr == 0)
3313 			LERROR("constant()");
3314 		v->v_tspec = INT;
3315 		v->v_quad = 1;
3316 		return (v);
3317 	}
3318 
3319 	v->v_tspec = tn->tn_type->t_tspec;
3320 
3321 	if (tn->tn_op == CON) {
3322 		if (tn->tn_type->t_tspec != tn->tn_val->v_tspec)
3323 			LERROR("constant()");
3324 		if (isityp(tn->tn_val->v_tspec)) {
3325 			v->v_ansiu = tn->tn_val->v_ansiu;
3326 			v->v_quad = tn->tn_val->v_quad;
3327 			return (v);
3328 		}
3329 		v->v_quad = tn->tn_val->v_ldbl;
3330 	} else {
3331 		v->v_quad = 1;
3332 	}
3333 
3334 	/* integral constant expression expected */
3335 	if (required)
3336 		error(55);
3337 	else
3338 		gnuism(318);
3339 
3340 	if (!isityp(v->v_tspec))
3341 		v->v_tspec = INT;
3342 
3343 	return (v);
3344 }
3345 
3346 /*
3347  * Perform some tests on expressions which can't be done in build() and
3348  * functions called by build(). These tests must be done here because
3349  * we need some information about the context in which the operations
3350  * are performed.
3351  * After all tests are performed, expr() frees the memory which is used
3352  * for the expression.
3353  */
3354 void
3355 expr(tnode_t *tn, int vctx, int tctx, int freeblk)
3356 {
3357 
3358 	if (tn == NULL && nerr == 0)
3359 		LERROR("expr()");
3360 
3361 	if (tn == NULL) {
3362 		tfreeblk();
3363 		return;
3364 	}
3365 
3366 	/* expr() is also called in global initialisations */
3367 	if (dcs->d_ctx != EXTERN)
3368 		chkreach();
3369 
3370 	chkmisc(tn, vctx, tctx, !tctx, 0, 0, 0);
3371 	if (tn->tn_op == ASSIGN) {
3372 		if (hflag && tctx)
3373 			/* assignment in conditional context */
3374 			warning(159);
3375 	} else if (tn->tn_op == CON) {
3376 		if (hflag && tctx && !ccflg)
3377 			/* constant in conditional context */
3378 			warning(161);
3379 	}
3380 	if (!modtab[tn->tn_op].m_sideeff) {
3381 		/*
3382 		 * for left operands of COMMA this warning is already
3383 		 * printed
3384 		 */
3385 		if (tn->tn_op != COMMA && !vctx && !tctx)
3386 			nulleff(tn);
3387 	}
3388 	if (dflag)
3389 		displexpr(tn, 0);
3390 
3391 	/* free the tree memory */
3392 	if (freeblk)
3393 		tfreeblk();
3394 }
3395 
3396 static void
3397 nulleff(tnode_t *tn)
3398 {
3399 
3400 	if (!hflag)
3401 		return;
3402 
3403 	while (!modtab[tn->tn_op].m_sideeff) {
3404 		if (tn->tn_op == CVT && tn->tn_type->t_tspec == VOID) {
3405 			tn = tn->tn_left;
3406 		} else if (tn->tn_op == LOGAND || tn->tn_op == LOGOR) {
3407 			/*
3408 			 * && and || have a side effect if the right operand
3409 			 * has a side effect.
3410 			 */
3411 			tn = tn->tn_right;
3412 		} else if (tn->tn_op == QUEST) {
3413 			/*
3414 			 * ? has a side effect if at least one of its right
3415 			 * operands has a side effect
3416 			 */
3417 			tn = tn->tn_right;
3418 		} else if (tn->tn_op == COLON || tn->tn_op == COMMA) {
3419 			/*
3420 			 * : has a side effect if at least one of its operands
3421 			 * has a side effect
3422 			 */
3423 			if (modtab[tn->tn_left->tn_op].m_sideeff) {
3424 				tn = tn->tn_left;
3425 			} else if (modtab[tn->tn_right->tn_op].m_sideeff) {
3426 				tn = tn->tn_right;
3427 			} else {
3428 				break;
3429 			}
3430 		} else {
3431 			break;
3432 		}
3433 	}
3434 	if (!modtab[tn->tn_op].m_sideeff)
3435 		/* expression has null effect */
3436 		warning(129);
3437 }
3438 
3439 /*
3440  * Dump an expression to stdout
3441  * only used for debugging
3442  */
3443 static void
3444 displexpr(tnode_t *tn, int offs)
3445 {
3446 	uint64_t uq;
3447 
3448 	if (tn == NULL) {
3449 		(void)printf("%*s%s\n", offs, "", "NULL");
3450 		return;
3451 	}
3452 	(void)printf("%*sop %s  ", offs, "", modtab[tn->tn_op].m_name);
3453 
3454 	if (tn->tn_op == NAME) {
3455 		(void)printf("%s: %s ",
3456 			     tn->tn_sym->s_name, scltoa(tn->tn_sym->s_scl));
3457 	} else if (tn->tn_op == CON && isftyp(tn->tn_type->t_tspec)) {
3458 		(void)printf("%#g ", (double)tn->tn_val->v_ldbl);
3459 	} else if (tn->tn_op == CON && isityp(tn->tn_type->t_tspec)) {
3460 		uq = tn->tn_val->v_quad;
3461 		(void)printf("0x %08lx %08lx ", (long)(uq >> 32) & 0xffffffffl,
3462 			     (long)uq & 0xffffffffl);
3463 	} else if (tn->tn_op == CON) {
3464 		if (tn->tn_type->t_tspec != PTR)
3465 			LERROR("displexpr()");
3466 		(void)printf("0x%0*lx ", (int)(sizeof (void *) * CHAR_BIT / 4),
3467 			     (u_long)tn->tn_val->v_quad);
3468 	} else if (tn->tn_op == STRING) {
3469 		if (tn->tn_strg->st_tspec == CHAR) {
3470 			(void)printf("\"%s\"", tn->tn_strg->st_cp);
3471 		} else {
3472 			char	*s;
3473 			size_t	n;
3474 			n = MB_CUR_MAX * (tn->tn_strg->st_len + 1);
3475 			s = xmalloc(n);
3476 			(void)wcstombs(s, tn->tn_strg->st_wcp, n);
3477 			(void)printf("L\"%s\"", s);
3478 			free(s);
3479 		}
3480 		(void)printf(" ");
3481 	} else if (tn->tn_op == FSEL) {
3482 		(void)printf("o=%d, l=%d ", tn->tn_type->t_foffs,
3483 			     tn->tn_type->t_flen);
3484 	}
3485 	(void)printf("%s\n", ttos(tn->tn_type));
3486 	if (tn->tn_op == NAME || tn->tn_op == CON || tn->tn_op == STRING)
3487 		return;
3488 	displexpr(tn->tn_left, offs + 2);
3489 	if (modtab[tn->tn_op].m_binary ||
3490 	    (tn->tn_op == PUSH && tn->tn_right != NULL)) {
3491 		displexpr(tn->tn_right, offs + 2);
3492 	}
3493 }
3494 
3495 /*
3496  * Called by expr() to recursively perform some tests.
3497  */
3498 /* ARGSUSED */
3499 void
3500 chkmisc(tnode_t *tn, int vctx, int tctx, int eqwarn, int fcall, int rvdisc,
3501 	int szof)
3502 {
3503 	tnode_t	*ln, *rn;
3504 	mod_t	*mp;
3505 	int	nrvdisc, cvctx, ctctx;
3506 	op_t	op;
3507 	scl_t	sc;
3508 	dinfo_t	*di;
3509 
3510 	if (tn == NULL)
3511 		return;
3512 
3513 	ln = tn->tn_left;
3514 	rn = tn->tn_right;
3515 	mp = &modtab[op = tn->tn_op];
3516 
3517 	switch (op) {
3518 	case AMPER:
3519 		if (ln->tn_op == NAME && (reached || rchflg)) {
3520 			if (!szof)
3521 				setsflg(ln->tn_sym);
3522 			setuflg(ln->tn_sym, fcall, szof);
3523 		}
3524 		if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3525 			/* check the range of array indices */
3526 			chkaidx(ln->tn_left, 1);
3527 		break;
3528 	case LOAD:
3529 		if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3530 			/* check the range of array indices */
3531 			chkaidx(ln->tn_left, 0);
3532 		/* FALLTHROUGH */
3533 	case PUSH:
3534 	case INCBEF:
3535 	case DECBEF:
3536 	case INCAFT:
3537 	case DECAFT:
3538 	case ADDASS:
3539 	case SUBASS:
3540 	case MULASS:
3541 	case DIVASS:
3542 	case MODASS:
3543 	case ANDASS:
3544 	case ORASS:
3545 	case XORASS:
3546 	case SHLASS:
3547 	case SHRASS:
3548 		if (ln->tn_op == NAME && (reached || rchflg)) {
3549 			sc = ln->tn_sym->s_scl;
3550 			/*
3551 			 * Look if there was a asm statement in one of the
3552 			 * compound statements we are in. If not, we don't
3553 			 * print a warning.
3554 			 */
3555 			for (di = dcs; di != NULL; di = di->d_nxt) {
3556 				if (di->d_asm)
3557 					break;
3558 			}
3559 			if (sc != EXTERN && sc != STATIC &&
3560 			    !ln->tn_sym->s_set && !szof && di == NULL) {
3561 				/* %s may be used before set */
3562 				warning(158, ln->tn_sym->s_name);
3563 				setsflg(ln->tn_sym);
3564 			}
3565 			setuflg(ln->tn_sym, 0, 0);
3566 		}
3567 		break;
3568 	case ASSIGN:
3569 		if (ln->tn_op == NAME && !szof && (reached || rchflg)) {
3570 			setsflg(ln->tn_sym);
3571 			if (ln->tn_sym->s_scl == EXTERN)
3572 				outusg(ln->tn_sym);
3573 		}
3574 		if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3575 			/* check the range of array indices */
3576 			chkaidx(ln->tn_left, 0);
3577 		break;
3578 	case CALL:
3579 		if (ln->tn_op != AMPER || ln->tn_left->tn_op != NAME)
3580 			LERROR("chkmisc()");
3581 		if (!szof)
3582 			outcall(tn, vctx || tctx, rvdisc);
3583 		break;
3584 	case EQ:
3585 		/* equality operator "==" found where "=" was exp. */
3586 		if (hflag && eqwarn)
3587 			warning(160);
3588 		break;
3589 	case CON:
3590 	case NAME:
3591 	case STRING:
3592 		return;
3593 		/* LINTED (enumeration values not handled in switch) */
3594 	case OR:
3595 	case XOR:
3596 	case NE:
3597 	case GE:
3598 	case GT:
3599 	case LE:
3600 	case LT:
3601 	case SHR:
3602 	case SHL:
3603 	case MINUS:
3604 	case PLUS:
3605 	case MOD:
3606 	case DIV:
3607 	case MULT:
3608 	case STAR:
3609 	case UMINUS:
3610 	case UPLUS:
3611 	case DEC:
3612 	case INC:
3613 	case COMPL:
3614 	case NOT:
3615 	case POINT:
3616 	case ARROW:
3617 	case NOOP:
3618 	case AND:
3619 	case FARG:
3620 	case CASE:
3621 	case INIT:
3622 	case RETURN:
3623 	case ICALL:
3624 	case CVT:
3625 	case COMMA:
3626 	case FSEL:
3627 	case COLON:
3628 	case QUEST:
3629 	case LOGOR:
3630 	case LOGAND:
3631 		break;
3632 	}
3633 
3634 	cvctx = mp->m_vctx;
3635 	ctctx = mp->m_tctx;
3636 	/*
3637 	 * values of operands of ':' are not used if the type of at least
3638 	 * one of the operands (for gcc compatibility) is void
3639 	 * XXX test/value context of QUEST should probably be used as
3640 	 * context for both operands of COLON
3641 	 */
3642 	if (op == COLON && tn->tn_type->t_tspec == VOID)
3643 		cvctx = ctctx = 0;
3644 	nrvdisc = op == CVT && tn->tn_type->t_tspec == VOID;
3645 	chkmisc(ln, cvctx, ctctx, mp->m_eqwarn, op == CALL, nrvdisc, szof);
3646 
3647 	switch (op) {
3648 	case PUSH:
3649 		if (rn != NULL)
3650 			chkmisc(rn, 0, 0, mp->m_eqwarn, 0, 0, szof);
3651 		break;
3652 	case LOGAND:
3653 	case LOGOR:
3654 		chkmisc(rn, 0, 1, mp->m_eqwarn, 0, 0, szof);
3655 		break;
3656 	case COLON:
3657 		chkmisc(rn, cvctx, ctctx, mp->m_eqwarn, 0, 0, szof);
3658 		break;
3659 	case COMMA:
3660 		chkmisc(rn, vctx, tctx, mp->m_eqwarn, 0, 0, szof);
3661 		break;
3662 	default:
3663 		if (mp->m_binary)
3664 			chkmisc(rn, 1, 0, mp->m_eqwarn, 0, 0, szof);
3665 		break;
3666 	}
3667 
3668 }
3669 
3670 /*
3671  * Checks the range of array indices, if possible.
3672  * amper is set if only the address of the element is used. This
3673  * means that the index is allowd to refere to the first element
3674  * after the array.
3675  */
3676 static void
3677 chkaidx(tnode_t *tn, int amper)
3678 {
3679 	int	dim;
3680 	tnode_t	*ln, *rn;
3681 	int	elsz;
3682 	int64_t	con;
3683 
3684 	ln = tn->tn_left;
3685 	rn = tn->tn_right;
3686 
3687 	/* We can only check constant indices. */
3688 	if (rn->tn_op != CON)
3689 		return;
3690 
3691 	/* Return if the left node does not stem from an array. */
3692 	if (ln->tn_op != AMPER)
3693 		return;
3694 	if (ln->tn_left->tn_op != STRING && ln->tn_left->tn_op != NAME)
3695 		return;
3696 	if (ln->tn_left->tn_type->t_tspec != ARRAY)
3697 		return;
3698 
3699 	/*
3700 	 * For incomplete array types, we can print a warning only if
3701 	 * the index is negative.
3702 	 */
3703 	if (incompl(ln->tn_left->tn_type) && rn->tn_val->v_quad >= 0)
3704 		return;
3705 
3706 	/* Get the size of one array element */
3707 	if ((elsz = length(ln->tn_type->t_subt, NULL)) == 0)
3708 		return;
3709 	elsz /= CHAR_BIT;
3710 
3711 	/* Change the unit of the index from bytes to element size. */
3712 	if (isutyp(rn->tn_type->t_tspec)) {
3713 		con = (uint64_t)rn->tn_val->v_quad / elsz;
3714 	} else {
3715 		con = rn->tn_val->v_quad / elsz;
3716 	}
3717 
3718 	dim = ln->tn_left->tn_type->t_dim + (amper ? 1 : 0);
3719 
3720 	if (!isutyp(rn->tn_type->t_tspec) && con < 0) {
3721 		/* array subscript cannot be negative: %ld */
3722 		warning(167, (long)con);
3723 	} else if (dim > 0 && (uint64_t)con >= dim) {
3724 		/* array subscript cannot be > %d: %ld */
3725 		warning(168, dim - 1, (long)con);
3726 	}
3727 }
3728 
3729 /*
3730  * Check for ordered comparisons of unsigned values with 0.
3731  */
3732 static void
3733 chkcomp(op_t op, tnode_t *ln, tnode_t *rn)
3734 {
3735 	char buf[64];
3736 	tspec_t	lt, rt;
3737 	mod_t	*mp;
3738 
3739 	lt = ln->tn_type->t_tspec;
3740 	rt = rn->tn_type->t_tspec;
3741 	mp = &modtab[op];
3742 
3743 	if (ln->tn_op != CON && rn->tn_op != CON)
3744 		return;
3745 
3746 	if (!isityp(lt) || !isityp(rt))
3747 		return;
3748 
3749 	if ((hflag || pflag) && lt == CHAR && rn->tn_op == CON &&
3750 	    (rn->tn_val->v_quad < 0 ||
3751 	     rn->tn_val->v_quad > ~(~0 << (CHAR_BIT - 1)))) {
3752 		/* nonportable character comparison, op %s */
3753 		warning(230, mp->m_name);
3754 		return;
3755 	}
3756 	if ((hflag || pflag) && rt == CHAR && ln->tn_op == CON &&
3757 	    (ln->tn_val->v_quad < 0 ||
3758 	     ln->tn_val->v_quad > ~(~0 << (CHAR_BIT - 1)))) {
3759 		/* nonportable character comparison, op %s */
3760 		warning(230, mp->m_name);
3761 		return;
3762 	}
3763 	if (isutyp(lt) && !isutyp(rt) &&
3764 	    rn->tn_op == CON && rn->tn_val->v_quad <= 0) {
3765 		if (rn->tn_val->v_quad < 0) {
3766 			/* comparison of %s with %s, op %s */
3767 			warning(162, tyname(buf, sizeof(buf), ln->tn_type),
3768 			    "negative constant", mp->m_name);
3769 		} else if (op == LT || op == GE || (hflag && op == LE)) {
3770 			/* comparison of %s with %s, op %s */
3771 			warning(162, tyname(buf, sizeof(buf), ln->tn_type),
3772 			    "0", mp->m_name);
3773 		}
3774 		return;
3775 	}
3776 	if (isutyp(rt) && !isutyp(lt) &&
3777 	    ln->tn_op == CON && ln->tn_val->v_quad <= 0) {
3778 		if (ln->tn_val->v_quad < 0) {
3779 			/* comparison of %s with %s, op %s */
3780 			warning(162, "negative constant",
3781 			    tyname(buf, sizeof(buf), rn->tn_type), mp->m_name);
3782 		} else if (op == GT || op == LE || (hflag && op == GE)) {
3783 			/* comparison of %s with %s, op %s */
3784 			warning(162, "0", tyname(buf, sizeof(buf), rn->tn_type),
3785 			    mp->m_name);
3786 		}
3787 		return;
3788 	}
3789 }
3790 
3791 /*
3792  * Takes an expression an returns 0 if this expression can be used
3793  * for static initialisation, otherwise -1.
3794  *
3795  * Constant initialisation expressions must be constant or an address
3796  * of a static object with an optional offset. In the first case,
3797  * the result is returned in *offsp. In the second case, the static
3798  * object is returned in *symp and the offset in *offsp.
3799  *
3800  * The expression can consist of PLUS, MINUS, AMPER, NAME, STRING and
3801  * CON. Type conversions are allowed if they do not change binary
3802  * representation (including width).
3803  */
3804 int
3805 conaddr(tnode_t *tn, sym_t **symp, ptrdiff_t *offsp)
3806 {
3807 	sym_t	*sym;
3808 	ptrdiff_t offs1, offs2;
3809 	tspec_t	t, ot;
3810 
3811 	switch (tn->tn_op) {
3812 	case MINUS:
3813 		if (tn->tn_right->tn_op == CVT)
3814 			return conaddr(tn->tn_right, symp, offsp);
3815 		else if (tn->tn_right->tn_op != CON)
3816 			return (-1);
3817 		/* FALLTHROUGH */
3818 	case PLUS:
3819 		offs1 = offs2 = 0;
3820 		if (tn->tn_left->tn_op == CON) {
3821 			offs1 = (ptrdiff_t)tn->tn_left->tn_val->v_quad;
3822 			if (conaddr(tn->tn_right, &sym, &offs2) == -1)
3823 				return (-1);
3824 		} else if (tn->tn_right->tn_op == CON) {
3825 			offs2 = (ptrdiff_t)tn->tn_right->tn_val->v_quad;
3826 			if (tn->tn_op == MINUS)
3827 				offs2 = -offs2;
3828 			if (conaddr(tn->tn_left, &sym, &offs1) == -1)
3829 				return (-1);
3830 		} else {
3831 			return (-1);
3832 		}
3833 		*symp = sym;
3834 		*offsp = offs1 + offs2;
3835 		break;
3836 	case AMPER:
3837 		if (tn->tn_left->tn_op == NAME) {
3838 			*symp = tn->tn_left->tn_sym;
3839 			*offsp = 0;
3840 		} else if (tn->tn_left->tn_op == STRING) {
3841 			/*
3842 			 * If this would be the front end of a compiler we
3843 			 * would return a label instead of 0.
3844 			 */
3845 			*offsp = 0;
3846 		}
3847 		break;
3848 	case CVT:
3849 		t = tn->tn_type->t_tspec;
3850 		ot = tn->tn_left->tn_type->t_tspec;
3851 		if ((!isityp(t) && t != PTR) || (!isityp(ot) && ot != PTR))
3852 			return (-1);
3853 #ifdef notdef
3854 		/*
3855 		 * consider:
3856 		 * 	struct foo {
3857 		 *	    unsigned char a;
3858 		 *      } f = {
3859 		 *          (u_char)(u_long)(&(((struct foo *)0)->a))
3860 		 *	};
3861 		 * since psize(u_long) != psize(u_char) this fails.
3862 		 */
3863 		else if (psize(t) != psize(ot))
3864 			return (-1);
3865 #endif
3866 		if (conaddr(tn->tn_left, symp, offsp) == -1)
3867 			return (-1);
3868 		break;
3869 	default:
3870 		return (-1);
3871 	}
3872 	return (0);
3873 }
3874 
3875 /*
3876  * Concatenate two string constants.
3877  */
3878 strg_t *
3879 catstrg(strg_t *strg1, strg_t *strg2)
3880 {
3881 	size_t	len1, len2, len;
3882 
3883 	if (strg1->st_tspec != strg2->st_tspec) {
3884 		/* cannot concatenate wide and regular string literals */
3885 		error(292);
3886 		return (strg1);
3887 	}
3888 
3889 	len = (len1 = strg1->st_len) + (len2 = strg2->st_len);
3890 
3891 	if (strg1->st_tspec == CHAR) {
3892 		strg1->st_cp = xrealloc(strg1->st_cp, len + 1);
3893 		(void)memcpy(strg1->st_cp + len1, strg2->st_cp, len2 + 1);
3894 		free(strg2->st_cp);
3895 	} else {
3896 		strg1->st_wcp = xrealloc(strg1->st_wcp,
3897 					 (len + 1) * sizeof (wchar_t));
3898 		(void)memcpy(strg1->st_wcp + len1, strg2->st_wcp,
3899 			     (len2 + 1) * sizeof (wchar_t));
3900 		free(strg2->st_wcp);
3901 	}
3902 	free(strg2);
3903 
3904 	return (strg1);
3905 }
3906 
3907 /*
3908  * Print a warning if the given node has operands which should be
3909  * parenthesized.
3910  *
3911  * XXX Does not work if an operand is a constant expression. Constant
3912  * expressions are already folded.
3913  */
3914 static void
3915 precconf(tnode_t *tn)
3916 {
3917 	tnode_t	*ln, *rn;
3918 	op_t	lop, rop = NOOP;
3919 	int	lparn, rparn = 0;
3920 	mod_t	*mp;
3921 	int	warn;
3922 
3923 	if (!hflag)
3924 		return;
3925 
3926 	mp = &modtab[tn->tn_op];
3927 
3928 	lparn = 0;
3929 	for (ln = tn->tn_left; ln->tn_op == CVT; ln = ln->tn_left)
3930 		lparn |= ln->tn_parn;
3931 	lparn |= ln->tn_parn;
3932 	lop = ln->tn_op;
3933 
3934 	if (mp->m_binary) {
3935 		rparn = 0;
3936 		for (rn = tn->tn_right; tn->tn_op == CVT; rn = rn->tn_left)
3937 			rparn |= rn->tn_parn;
3938 		rparn |= rn->tn_parn;
3939 		rop = rn->tn_op;
3940 	}
3941 
3942 	warn = 0;
3943 
3944 	switch (tn->tn_op) {
3945 	case SHL:
3946 	case SHR:
3947 		if (!lparn && (lop == PLUS || lop == MINUS)) {
3948 			warn = 1;
3949 		} else if (!rparn && (rop == PLUS || rop == MINUS)) {
3950 			warn = 1;
3951 		}
3952 		break;
3953 	case LOGOR:
3954 		if (!lparn && lop == LOGAND) {
3955 			warn = 1;
3956 		} else if (!rparn && rop == LOGAND) {
3957 			warn = 1;
3958 		}
3959 		break;
3960 	case AND:
3961 	case XOR:
3962 	case OR:
3963 		if (!lparn && lop != tn->tn_op) {
3964 			if (lop == PLUS || lop == MINUS) {
3965 				warn = 1;
3966 			} else if (lop == AND || lop == XOR) {
3967 				warn = 1;
3968 			}
3969 		}
3970 		if (!warn && !rparn && rop != tn->tn_op) {
3971 			if (rop == PLUS || rop == MINUS) {
3972 				warn = 1;
3973 			} else if (rop == AND || rop == XOR) {
3974 				warn = 1;
3975 			}
3976 		}
3977 		break;
3978 		/* LINTED (enumeration values not handled in switch) */
3979 	case DECAFT:
3980 	case XORASS:
3981 	case SHLASS:
3982 	case NOOP:
3983 	case ARROW:
3984 	case ORASS:
3985 	case POINT:
3986 	case NAME:
3987 	case NOT:
3988 	case COMPL:
3989 	case CON:
3990 	case INC:
3991 	case STRING:
3992 	case DEC:
3993 	case INCBEF:
3994 	case DECBEF:
3995 	case INCAFT:
3996 	case FSEL:
3997 	case CALL:
3998 	case COMMA:
3999 	case CVT:
4000 	case ICALL:
4001 	case LOAD:
4002 	case PUSH:
4003 	case RETURN:
4004 	case INIT:
4005 	case CASE:
4006 	case FARG:
4007 	case SUBASS:
4008 	case ADDASS:
4009 	case MODASS:
4010 	case DIVASS:
4011 	case MULASS:
4012 	case ASSIGN:
4013 	case COLON:
4014 	case QUEST:
4015 	case LOGAND:
4016 	case NE:
4017 	case EQ:
4018 	case GE:
4019 	case GT:
4020 	case LE:
4021 	case LT:
4022 	case MINUS:
4023 	case PLUS:
4024 	case MOD:
4025 	case DIV:
4026 	case MULT:
4027 	case AMPER:
4028 	case STAR:
4029 	case UMINUS:
4030 	case SHRASS:
4031 	case UPLUS:
4032 	case ANDASS:
4033 		break;
4034 	}
4035 
4036 	if (warn) {
4037 		/* precedence confusion possible: parenthesize! */
4038 		warning(169);
4039 	}
4040 
4041 }
4042