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