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