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