• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * sparse/evaluate.c
3  *
4  * Copyright (C) 2003 Transmeta Corp.
5  *               2003-2004 Linus Torvalds
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  *
25  * Evaluate constant expressions.
26  */
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <stddef.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <limits.h>
36 
37 #include "evaluate.h"
38 #include "lib.h"
39 #include "allocate.h"
40 #include "parse.h"
41 #include "token.h"
42 #include "symbol.h"
43 #include "target.h"
44 #include "expression.h"
45 
46 struct symbol *current_fn;
47 
48 struct ident bad_address_space = { .len = 6, .name = "bad AS", };
49 
50 static struct symbol *degenerate(struct expression *expr);
51 static struct symbol *evaluate_symbol(struct symbol *sym);
52 
valid_expr_type(struct expression * expr)53 static inline int valid_expr_type(struct expression *expr)
54 {
55 	return expr && valid_type(expr->ctype);
56 }
57 
valid_subexpr_type(struct expression * expr)58 static inline int valid_subexpr_type(struct expression *expr)
59 {
60 	return valid_expr_type(expr->left)
61 	    && valid_expr_type(expr->right);
62 }
63 
unqualify_type(struct symbol * ctype)64 static struct symbol *unqualify_type(struct symbol *ctype)
65 {
66 	if (!ctype)
67 		return ctype;
68 	if (ctype->type == SYM_NODE && (ctype->ctype.modifiers & MOD_QUALIFIER)) {
69 		struct symbol *unqual = alloc_symbol(ctype->pos, 0);
70 
71 		*unqual = *ctype;
72 		unqual->ctype.modifiers &= ~MOD_QUALIFIER;
73 		return unqual;
74 	}
75 	return ctype;
76 }
77 
evaluate_symbol_expression(struct expression * expr)78 static struct symbol *evaluate_symbol_expression(struct expression *expr)
79 {
80 	struct expression *addr;
81 	struct symbol *sym = expr->symbol;
82 	struct symbol *base_type;
83 
84 	if (!sym) {
85 		expression_error(expr, "undefined identifier '%s'", show_ident(expr->symbol_name));
86 		return NULL;
87 	}
88 
89 	examine_symbol_type(sym);
90 
91 	base_type = get_base_type(sym);
92 	if (!base_type) {
93 		expression_error(expr, "identifier '%s' has no type", show_ident(expr->symbol_name));
94 		return NULL;
95 	}
96 
97 	addr = alloc_expression(expr->pos, EXPR_SYMBOL);
98 	addr->symbol = sym;
99 	addr->symbol_name = expr->symbol_name;
100 	addr->ctype = &lazy_ptr_ctype;	/* Lazy evaluation: we need to do a proper job if somebody does &sym */
101 	addr->flags = expr->flags;
102 	expr->type = EXPR_PREOP;
103 	expr->op = '*';
104 	expr->unop = addr;
105 	expr->flags = CEF_NONE;
106 
107 	/* The type of a symbol is the symbol itself! */
108 	expr->ctype = sym;
109 	return sym;
110 }
111 
evaluate_string(struct expression * expr)112 static struct symbol *evaluate_string(struct expression *expr)
113 {
114 	struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
115 	struct symbol *array = alloc_symbol(expr->pos, SYM_ARRAY);
116 	struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
117 	struct expression *initstr = alloc_expression(expr->pos, EXPR_STRING);
118 	unsigned int length = expr->string->length;
119 	struct symbol *char_type = expr->wide ? wchar_ctype : &char_ctype;
120 
121 	sym->array_size = alloc_const_expression(expr->pos, length);
122 	sym->bit_size = length * char_type->bit_size;
123 	sym->ctype.alignment = 1;
124 	sym->string = 1;
125 	sym->ctype.modifiers = MOD_STATIC;
126 	sym->ctype.base_type = array;
127 	sym->initializer = initstr;
128 	sym->examined = 1;
129 	sym->evaluated = 1;
130 
131 	initstr->ctype = sym;
132 	initstr->string = expr->string;
133 
134 	array->array_size = sym->array_size;
135 	array->bit_size = sym->bit_size;
136 	array->ctype.alignment = char_type->ctype.alignment;
137 	array->ctype.modifiers = MOD_STATIC;
138 	array->ctype.base_type = char_type;
139 	array->examined = 1;
140 	array->evaluated = 1;
141 
142 	addr->symbol = sym;
143 	addr->ctype = &lazy_ptr_ctype;
144 	addr->flags = CEF_ADDR;
145 
146 	expr->type = EXPR_PREOP;
147 	expr->op = '*';
148 	expr->unop = addr;
149 	expr->ctype = sym;
150 	return sym;
151 }
152 
153 /* type has come from classify_type and is an integer type */
integer_promotion(struct symbol * type)154 static inline struct symbol *integer_promotion(struct symbol *type)
155 {
156 	unsigned long mod =  type->ctype.modifiers;
157 	int width = type->bit_size;
158 
159 	/*
160 	 * Bitfields always promote to the base type,
161 	 * even if the bitfield might be bigger than
162 	 * an "int".
163 	 */
164 	if (type->type == SYM_BITFIELD) {
165 		type = type->ctype.base_type;
166 	}
167 	mod = type->ctype.modifiers;
168 	if (width < bits_in_int)
169 		return &int_ctype;
170 
171 	/* If char/short has as many bits as int, it still gets "promoted" */
172 	if (type->rank < 0) {
173 		if (mod & MOD_UNSIGNED)
174 			return &uint_ctype;
175 		return &int_ctype;
176 	}
177 	return type;
178 }
179 
180 /*
181  * After integer promotons:
182  * If both types are the same
183  *   -> no conversion needed
184  * If the types have the same signedness (their rank must be different)
185  *   -> convert to the type of the highest rank
186  * If rank(unsigned type) >= rank(signed type)
187  *   -> convert to the unsigned type
188  * If size(signed type) > size(unsigned type)
189  *   -> convert to the signed type
190  * Otherwise
191  *   -> convert to the unsigned type corresponding to the signed type.
192  */
bigger_int_type(struct symbol * left,struct symbol * right)193 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
194 {
195 	static struct symbol *unsigned_types[] = {
196 		[0] = &uint_ctype,
197 		[1] = &ulong_ctype,
198 		[2] = &ullong_ctype,
199 		[3] = &uint128_ctype,
200 	};
201 	unsigned long lmod, rmod;
202 	struct symbol *stype, *utype;
203 
204 	left = integer_promotion(left);
205 	right = integer_promotion(right);
206 
207 	if (left == right)
208 		return left;
209 
210 	lmod = left->ctype.modifiers;
211 	rmod = right->ctype.modifiers;
212 	if (((lmod ^ rmod) & MOD_UNSIGNED) == 0)
213 		return (left->rank > right->rank) ? left : right;
214 	if (lmod & MOD_UNSIGNED) {
215 		utype = left;
216 		stype = right;
217 	} else {
218 		stype = left;
219 		utype = right;
220 	}
221 	if (utype->rank >= stype->rank)
222 		return utype;
223 	if (stype->bit_size > utype->bit_size)
224 		return stype;
225 	utype = unsigned_types[stype->rank];
226 	return utype;
227 }
228 
base_type(struct symbol * node,unsigned long * modp,struct ident ** asp)229 static struct symbol *base_type(struct symbol *node, unsigned long *modp, struct ident **asp)
230 {
231 	unsigned long mod = 0;
232 	struct ident *as = NULL;
233 
234 	while (node) {
235 		mod |= node->ctype.modifiers;
236 		combine_address_space(node->pos, &as, node->ctype.as);
237 		if (node->type == SYM_NODE) {
238 			node = node->ctype.base_type;
239 			continue;
240 		}
241 		break;
242 	}
243 	*modp = mod & ~MOD_IGNORE;
244 	*asp = as;
245 	return node;
246 }
247 
is_same_type(struct expression * expr,struct symbol * new)248 static int is_same_type(struct expression *expr, struct symbol *new)
249 {
250 	struct symbol *old = expr->ctype;
251 	unsigned long oldmod, newmod;
252 	struct ident *oldas, *newas;
253 
254 	old = base_type(old, &oldmod, &oldas);
255 	new = base_type(new, &newmod, &newas);
256 
257 	/* Same base type, same address space? */
258 	if (old == new && oldas == newas) {
259 		unsigned long difmod;
260 
261 		/* Check the modifier bits. */
262 		difmod = (oldmod ^ newmod) & ~MOD_NOCAST;
263 
264 		/* Exact same type? */
265 		if (!difmod)
266 			return 1;
267 
268 		/*
269 		 * Not the same type, but differs only in "const".
270 		 * Don't warn about MOD_NOCAST.
271 		 */
272 		if (difmod == MOD_CONST)
273 			return 0;
274 	}
275 	if ((oldmod | newmod) & MOD_NOCAST) {
276 		const char *tofrom = "to/from";
277 		if (!(newmod & MOD_NOCAST))
278 			tofrom = "from";
279 		if (!(oldmod & MOD_NOCAST))
280 			tofrom = "to";
281 		warning(expr->pos, "implicit cast %s nocast type", tofrom);
282 	}
283 	return 0;
284 }
285 
286 static void
warn_for_different_enum_types(struct position pos,struct symbol * typea,struct symbol * typeb)287 warn_for_different_enum_types (struct position pos,
288 			       struct symbol *typea,
289 			       struct symbol *typeb)
290 {
291 	if (!Wenum_mismatch)
292 		return;
293 	if (typea->type == SYM_NODE)
294 		typea = typea->ctype.base_type;
295 	if (typeb->type == SYM_NODE)
296 		typeb = typeb->ctype.base_type;
297 
298 	if (typea == typeb)
299 		return;
300 
301 	if (typea->type == SYM_ENUM && typeb->type == SYM_ENUM) {
302 		warning(pos, "mixing different enum types:");
303 		info(pos, "   %s", show_typename(typea));
304 		info(pos, "   %s", show_typename(typeb));
305 	}
306 }
307 
308 static int cast_flags(struct expression *expr, struct expression *target);
309 static struct symbol *cast_to_bool(struct expression *expr);
310 
311 /*
312  * This gets called for implicit casts in assignments and
313  * integer promotion.
314  */
cast_to(struct expression * old,struct symbol * type)315 static struct expression * cast_to(struct expression *old, struct symbol *type)
316 {
317 	struct expression *expr;
318 
319 	warn_for_different_enum_types (old->pos, old->ctype, type);
320 
321 	if (old->ctype != &null_ctype && is_same_type(old, type))
322 		return old;
323 
324 	expr = alloc_expression(old->pos, EXPR_IMPLIED_CAST);
325 	expr->ctype = type;
326 	expr->cast_type = type;
327 	expr->cast_expression = old;
328 	expr->flags = cast_flags(expr, old);
329 
330 	if (is_bool_type(type))
331 		cast_to_bool(expr);
332 
333 	return expr;
334 }
335 
336 enum {
337 	TYPE_NUM = 1,
338 	TYPE_BITFIELD = 2,
339 	TYPE_RESTRICT = 4,
340 	TYPE_FLOAT = 8,
341 	TYPE_PTR = 16,
342 	TYPE_COMPOUND = 32,
343 	TYPE_FOULED = 64,
344 	TYPE_FN = 128,
345 };
346 
classify_type(struct symbol * type,struct symbol ** base)347 static inline int classify_type(struct symbol *type, struct symbol **base)
348 {
349 	static int type_class[SYM_BAD + 1] = {
350 		[SYM_PTR] = TYPE_PTR,
351 		[SYM_FN] = TYPE_PTR | TYPE_FN,
352 		[SYM_ARRAY] = TYPE_PTR | TYPE_COMPOUND,
353 		[SYM_STRUCT] = TYPE_COMPOUND,
354 		[SYM_UNION] = TYPE_COMPOUND,
355 		[SYM_BITFIELD] = TYPE_NUM | TYPE_BITFIELD,
356 		[SYM_RESTRICT] = TYPE_NUM | TYPE_RESTRICT,
357 		[SYM_FOULED] = TYPE_NUM | TYPE_RESTRICT | TYPE_FOULED,
358 	};
359 	if (type->type == SYM_NODE)
360 		type = type->ctype.base_type;
361 	if (type->type == SYM_TYPEOF) {
362 		type = examine_symbol_type(type);
363 		if (type->type == SYM_NODE)
364 			type = type->ctype.base_type;
365 	}
366 	if (type->type == SYM_ENUM)
367 		type = type->ctype.base_type;
368 	*base = type;
369 	if (type->type == SYM_BASETYPE) {
370 		if (type->ctype.base_type == &int_type)
371 			return TYPE_NUM;
372 		if (type->ctype.base_type == &fp_type)
373 			return TYPE_NUM | TYPE_FLOAT;
374 	}
375 	return type_class[type->type];
376 }
377 
378 #define is_int(class) ((class & (TYPE_NUM | TYPE_FLOAT)) == TYPE_NUM)
379 
is_string_type(struct symbol * type)380 static inline int is_string_type(struct symbol *type)
381 {
382 	if (type->type == SYM_NODE)
383 		type = type->ctype.base_type;
384 	if (type->type != SYM_ARRAY)
385 		return 0;
386 	type = type->ctype.base_type;
387 	return is_byte_type(type) || is_wchar_type(type);
388 }
389 
bad_expr_type(struct expression * expr)390 static struct symbol *bad_expr_type(struct expression *expr)
391 {
392 	switch (expr->type) {
393 	case EXPR_BINOP:
394 	case EXPR_COMPARE:
395 		if (!valid_subexpr_type(expr))
396 			break;
397 		sparse_error(expr->pos, "incompatible types for operation (%s):", show_special(expr->op));
398 		info(expr->pos, "   %s", show_typename(expr->left->ctype));
399 		info(expr->pos, "   %s", show_typename(expr->right->ctype));
400 		break;
401 	case EXPR_PREOP:
402 	case EXPR_POSTOP:
403 		if (!valid_expr_type(expr->unop))
404 			break;
405 		sparse_error(expr->pos, "incompatible type for operation (%s):", show_special(expr->op));
406 		info(expr->pos, "   %s", show_typename(expr->unop->ctype));
407 		break;
408 	default:
409 		break;
410 	}
411 
412 	expr->flags = CEF_NONE;
413 	return expr->ctype = &bad_ctype;
414 }
415 
restricted_value(struct expression * v,struct symbol * type)416 static int restricted_value(struct expression *v, struct symbol *type)
417 {
418 	if (v->type != EXPR_VALUE)
419 		return 1;
420 	if (v->value != 0)
421 		return 1;
422 	return 0;
423 }
424 
restricted_binop(int op,struct symbol * type)425 static int restricted_binop(int op, struct symbol *type)
426 {
427 	switch (op) {
428 		case '&':
429 		case '=':
430 		case SPECIAL_AND_ASSIGN:
431 		case SPECIAL_OR_ASSIGN:
432 		case SPECIAL_XOR_ASSIGN:
433 			return 1;	/* unfoul */
434 		case '|':
435 		case '^':
436 		case '?':
437 			return 2;	/* keep fouled */
438 		case SPECIAL_EQUAL:
439 		case SPECIAL_NOTEQUAL:
440 			return 3;	/* warn if fouled */
441 		default:
442 			return 0;	/* warn */
443 	}
444 }
445 
restricted_unop(int op,struct symbol ** type)446 static int restricted_unop(int op, struct symbol **type)
447 {
448 	if (op == '~') {
449 		if ((*type)->bit_size < bits_in_int)
450 			*type = befoul(*type);
451 		return 0;
452 	} if (op == '+')
453 		return 0;
454 	return 1;
455 }
456 
457 /* type should be SYM_FOULED */
unfoul(struct symbol * type)458 static inline struct symbol *unfoul(struct symbol *type)
459 {
460 	return type->ctype.base_type;
461 }
462 
restricted_binop_type(int op,struct expression * left,struct expression * right,int lclass,int rclass,struct symbol * ltype,struct symbol * rtype)463 static struct symbol *restricted_binop_type(int op,
464 					struct expression *left,
465 					struct expression *right,
466 					int lclass, int rclass,
467 					struct symbol *ltype,
468 					struct symbol *rtype)
469 {
470 	struct symbol *ctype = NULL;
471 	if (lclass & TYPE_RESTRICT) {
472 		if (rclass & TYPE_RESTRICT) {
473 			if (ltype == rtype) {
474 				ctype = ltype;
475 			} else if (lclass & TYPE_FOULED) {
476 				if (unfoul(ltype) == rtype)
477 					ctype = ltype;
478 			} else if (rclass & TYPE_FOULED) {
479 				if (unfoul(rtype) == ltype)
480 					ctype = rtype;
481 			}
482 		} else {
483 			if (!restricted_value(right, ltype))
484 				ctype = ltype;
485 		}
486 	} else if (!restricted_value(left, rtype))
487 		ctype = rtype;
488 
489 	if (ctype) {
490 		switch (restricted_binop(op, ctype)) {
491 		case 1:
492 			if ((lclass ^ rclass) & TYPE_FOULED)
493 				ctype = unfoul(ctype);
494 			break;
495 		case 3:
496 			if (!(lclass & rclass & TYPE_FOULED))
497 				break;
498 		case 0:
499 			ctype = NULL;
500 		default:
501 			break;
502 		}
503 	}
504 
505 	return ctype;
506 }
507 
unrestrict(struct expression * expr,int class,struct symbol ** ctype)508 static inline void unrestrict(struct expression *expr,
509 			      int class, struct symbol **ctype)
510 {
511 	if (class & TYPE_RESTRICT) {
512 		if (class & TYPE_FOULED)
513 			*ctype = unfoul(*ctype);
514 		warning(expr->pos, "%s degrades to integer",
515 			show_typename(*ctype));
516 		*ctype = (*ctype)->ctype.base_type; /* get to arithmetic type */
517 	}
518 }
519 
usual_conversions(int op,struct expression * left,struct expression * right,int lclass,int rclass,struct symbol * ltype,struct symbol * rtype)520 static struct symbol *usual_conversions(int op,
521 					struct expression *left,
522 					struct expression *right,
523 					int lclass, int rclass,
524 					struct symbol *ltype,
525 					struct symbol *rtype)
526 {
527 	struct symbol *ctype;
528 
529 	warn_for_different_enum_types(right->pos, left->ctype, right->ctype);
530 
531 	if ((lclass | rclass) & TYPE_RESTRICT)
532 		goto Restr;
533 
534 Normal:
535 	if (!(lclass & TYPE_FLOAT)) {
536 		if (!(rclass & TYPE_FLOAT))
537 			return bigger_int_type(ltype, rtype);
538 		else
539 			return rtype;
540 	} else if (rclass & TYPE_FLOAT) {
541 		if (rtype->rank > ltype->rank)
542 			return rtype;
543 		else
544 			return ltype;
545 	} else
546 		return ltype;
547 
548 Restr:
549 	ctype = restricted_binop_type(op, left, right,
550 				      lclass, rclass, ltype, rtype);
551 	if (ctype)
552 		return ctype;
553 
554 	unrestrict(left, lclass, &ltype);
555 	unrestrict(right, rclass, &rtype);
556 
557 	goto Normal;
558 }
559 
lvalue_expression(struct expression * expr)560 static inline int lvalue_expression(struct expression *expr)
561 {
562 	return expr->type == EXPR_PREOP && expr->op == '*';
563 }
564 
evaluate_ptr_add(struct expression * expr,struct symbol * itype)565 static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *itype)
566 {
567 	struct expression *index = expr->right;
568 	struct symbol *ctype, *base;
569 	int multiply;
570 
571 	classify_type(degenerate(expr->left), &ctype);
572 	base = examine_pointer_target(ctype);
573 
574 	/*
575 	 * An address constant +/- an integer constant expression
576 	 * yields an address constant again [6.6(7)].
577 	 */
578 	if ((expr->left->flags & CEF_ADDR) && (expr->right->flags & CEF_ICE))
579 		expr->flags = CEF_ADDR;
580 
581 	if (!base) {
582 		expression_error(expr, "missing type information");
583 		return NULL;
584 	}
585 	if (is_function(base)) {
586 		expression_error(expr, "arithmetics on pointers to functions");
587 		return NULL;
588 	}
589 
590 	/* Get the size of whatever the pointer points to */
591 	multiply = is_void_type(base) ? 1 : bits_to_bytes(base->bit_size);
592 
593 	if (ctype == &null_ctype)
594 		ctype = &ptr_ctype;
595 	expr->ctype = ctype;
596 
597 	if (multiply == 1 && itype->bit_size == bits_in_pointer)
598 		return ctype;
599 
600 	if (index->type == EXPR_VALUE) {
601 		struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
602 		unsigned long long v = index->value, mask;
603 		mask = 1ULL << (itype->bit_size - 1);
604 		if (v & mask)
605 			v |= -mask;
606 		else
607 			v &= mask - 1;
608 		v *= multiply;
609 		mask = 1ULL << (bits_in_pointer - 1);
610 		v &= mask | (mask - 1);
611 		val->value = v;
612 		val->ctype = ssize_t_ctype;
613 		expr->right = val;
614 		return ctype;
615 	}
616 
617 	if (itype->bit_size != bits_in_pointer)
618 		index = cast_to(index, ssize_t_ctype);
619 
620 	if (multiply > 1) {
621 		struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
622 		struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
623 
624 		val->ctype = ssize_t_ctype;
625 		val->value = multiply;
626 
627 		mul->op = '*';
628 		mul->ctype = ssize_t_ctype;
629 		mul->left = index;
630 		mul->right = val;
631 		index = mul;
632 	}
633 
634 	expr->right = index;
635 	return ctype;
636 }
637 
638 static void examine_fn_arguments(struct symbol *fn);
639 
640 #define MOD_IGN (MOD_QUALIFIER | MOD_FUN_ATTR)
641 
type_difference(struct ctype * c1,struct ctype * c2,unsigned long mod1,unsigned long mod2)642 const char *type_difference(struct ctype *c1, struct ctype *c2,
643 	unsigned long mod1, unsigned long mod2)
644 {
645 	struct ident *as1 = c1->as, *as2 = c2->as;
646 	struct symbol *t1 = c1->base_type;
647 	struct symbol *t2 = c2->base_type;
648 	int move1 = 1, move2 = 1;
649 	mod1 |= c1->modifiers;
650 	mod2 |= c2->modifiers;
651 	for (;;) {
652 		unsigned long diff;
653 		int type;
654 		struct symbol *base1 = t1->ctype.base_type;
655 		struct symbol *base2 = t2->ctype.base_type;
656 
657 		/*
658 		 * FIXME! Collect alignment and context too here!
659 		 */
660 		if (move1) {
661 			if (t1 && t1->type != SYM_PTR) {
662 				mod1 |= t1->ctype.modifiers;
663 				combine_address_space(t1->pos, &as1, t1->ctype.as);
664 			}
665 			move1 = 0;
666 		}
667 
668 		if (move2) {
669 			if (t2 && t2->type != SYM_PTR) {
670 				mod2 |= t2->ctype.modifiers;
671 				combine_address_space(t2->pos, &as2, t2->ctype.as);
672 			}
673 			move2 = 0;
674 		}
675 
676 		if (t1 == t2)
677 			break;
678 		if (!t1 || !t2)
679 			return "different types";
680 
681 		if (t1->type == SYM_NODE || t1->type == SYM_ENUM) {
682 			t1 = base1;
683 			move1 = 1;
684 			if (!t1)
685 				return "bad types";
686 			continue;
687 		}
688 
689 		if (t2->type == SYM_NODE || t2->type == SYM_ENUM) {
690 			t2 = base2;
691 			move2 = 1;
692 			if (!t2)
693 				return "bad types";
694 			continue;
695 		}
696 
697 		move1 = move2 = 1;
698 		type = t1->type;
699 		if (type != t2->type)
700 			return "different base types";
701 
702 		switch (type) {
703 		default:
704 			sparse_error(t1->pos,
705 				     "internal error: bad type in derived(%d)",
706 				     type);
707 			return "bad types";
708 		case SYM_RESTRICT:
709 			return "different base types";
710 		case SYM_UNION:
711 		case SYM_STRUCT:
712 			/* allow definition of incomplete structs and unions */
713 			if (t1->ident == t2->ident)
714 			  return NULL;
715 			return "different base types";
716 		case SYM_ARRAY:
717 			/* XXX: we ought to compare sizes */
718 			break;
719 		case SYM_PTR:
720 			if (as1 != as2)
721 				return "different address spaces";
722 			/* MOD_SPECIFIER is due to idiocy in parse.c */
723 			if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SPECIFIER)
724 				return "different modifiers";
725 			/* we could be lazier here */
726 			base1 = examine_pointer_target(t1);
727 			base2 = examine_pointer_target(t2);
728 			mod1 = t1->ctype.modifiers;
729 			as1 = t1->ctype.as;
730 			mod2 = t2->ctype.modifiers;
731 			as2 = t2->ctype.as;
732 			break;
733 		case SYM_FN: {
734 			struct symbol *arg1, *arg2;
735 			int i;
736 
737 			if (as1 != as2)
738 				return "different address spaces";
739 			if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SIGNEDNESS)
740 				return "different modifiers";
741 			mod1 = t1->ctype.modifiers;
742 			as1 = t1->ctype.as;
743 			mod2 = t2->ctype.modifiers;
744 			as2 = t2->ctype.as;
745 
746 			if (t1->variadic != t2->variadic)
747 				return "incompatible variadic arguments";
748 			examine_fn_arguments(t1);
749 			examine_fn_arguments(t2);
750 			PREPARE_PTR_LIST(t1->arguments, arg1);
751 			PREPARE_PTR_LIST(t2->arguments, arg2);
752 			i = 1;
753 			for (;;) {
754 				const char *diffstr;
755 				if (!arg1 && !arg2)
756 					break;
757 				if (!arg1 || !arg2)
758 					return "different argument counts";
759 				diffstr = type_difference(&arg1->ctype,
760 							  &arg2->ctype,
761 							  MOD_IGN, MOD_IGN);
762 				if (diffstr) {
763 					static char argdiff[80];
764 					sprintf(argdiff, "incompatible argument %d (%s)", i, diffstr);
765 					return argdiff;
766 				}
767 				NEXT_PTR_LIST(arg1);
768 				NEXT_PTR_LIST(arg2);
769 				i++;
770 			}
771 			FINISH_PTR_LIST(arg2);
772 			FINISH_PTR_LIST(arg1);
773 			break;
774 		}
775 		case SYM_BASETYPE:
776 			if (as1 != as2)
777 				return "different address spaces";
778 			if (base1 != base2)
779 				return "different base types";
780 			if (t1->rank != t2->rank)
781 				return "different type sizes";
782 			diff = (mod1 ^ mod2) & ~MOD_IGNORE;
783 			if (!diff)
784 				return NULL;
785 			else if (diff & ~MOD_SIGNEDNESS)
786 				return "different modifiers";
787 			else
788 				return "different signedness";
789 		}
790 		t1 = base1;
791 		t2 = base2;
792 	}
793 	if (as1 != as2)
794 		return "different address spaces";
795 	if ((mod1 ^ mod2) & ~MOD_IGNORE & ~MOD_SIGNEDNESS)
796 		return "different modifiers";
797 	return NULL;
798 }
799 
bad_null(struct expression * expr)800 static void bad_null(struct expression *expr)
801 {
802 	if (Wnon_pointer_null)
803 		warning(expr->pos, "Using plain integer as NULL pointer");
804 }
805 
target_qualifiers(struct symbol * type)806 static unsigned long target_qualifiers(struct symbol *type)
807 {
808 	unsigned long mod = type->ctype.modifiers & MOD_IGN;
809 	if (type->ctype.base_type && type->ctype.base_type->type == SYM_ARRAY)
810 		mod = 0;
811 	return mod;
812 }
813 
evaluate_ptr_sub(struct expression * expr)814 static struct symbol *evaluate_ptr_sub(struct expression *expr)
815 {
816 	const char *typediff;
817 	struct symbol *ltype, *rtype;
818 	struct expression *l = expr->left;
819 	struct expression *r = expr->right;
820 	struct symbol *lbase;
821 
822 	classify_type(degenerate(l), &ltype);
823 	classify_type(degenerate(r), &rtype);
824 
825 	lbase = examine_pointer_target(ltype);
826 	examine_pointer_target(rtype);
827 	typediff = type_difference(&ltype->ctype, &rtype->ctype,
828 				   target_qualifiers(rtype),
829 				   target_qualifiers(ltype));
830 	if (typediff)
831 		expression_error(expr, "subtraction of different types can't work (%s)", typediff);
832 
833 	if (is_function(lbase)) {
834 		expression_error(expr, "subtraction of functions? Share your drugs");
835 		return NULL;
836 	}
837 
838 	expr->ctype = ssize_t_ctype;
839 	if (lbase->bit_size > bits_in_char) {
840 		struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
841 		struct expression *div = expr;
842 		struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
843 		unsigned long value = bits_to_bytes(lbase->bit_size);
844 
845 		val->ctype = size_t_ctype;
846 		val->value = value;
847 
848 		if (value & (value-1)) {
849 			if (Wptr_subtraction_blows) {
850 				warning(expr->pos, "potentially expensive pointer subtraction");
851 				info(expr->pos, "    '%s' has a non-power-of-2 size: %lu", show_typename(lbase), value);
852 			}
853 		}
854 
855 		sub->op = '-';
856 		sub->ctype = ssize_t_ctype;
857 		sub->left = l;
858 		sub->right = r;
859 
860 		div->op = '/';
861 		div->left = sub;
862 		div->right = val;
863 	}
864 
865 	return ssize_t_ctype;
866 }
867 
868 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
869 
evaluate_conditional(struct expression * expr,int iterator)870 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
871 {
872 	struct symbol *ctype;
873 
874 	if (!expr)
875 		return NULL;
876 
877 	if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
878 		warning(expr->pos, "assignment expression in conditional");
879 
880 	ctype = evaluate_expression(expr);
881 	if (!valid_type(ctype))
882 		return NULL;
883 	if (is_safe_type(ctype))
884 		warning(expr->pos, "testing a 'safe expression'");
885 	if (is_func_type(ctype)) {
886 		if (Waddress)
887 			warning(expr->pos, "the address of %s will always evaluate as true", "a function");
888 	} else if (is_array_type(ctype)) {
889 		if (Waddress)
890 			warning(expr->pos, "the address of %s will always evaluate as true", "an array");
891 	} else if (!is_scalar_type(ctype)) {
892 		sparse_error(expr->pos, "non-scalar type in conditional:");
893 		info(expr->pos, "   %s", show_typename(ctype));
894 		return NULL;
895 	}
896 
897 	ctype = degenerate(expr);
898 	return ctype;
899 }
900 
evaluate_logical(struct expression * expr)901 static struct symbol *evaluate_logical(struct expression *expr)
902 {
903 	if (!evaluate_conditional(expr->left, 0))
904 		return NULL;
905 	if (!evaluate_conditional(expr->right, 0))
906 		return NULL;
907 
908 	/* the result is int [6.5.13(3), 6.5.14(3)] */
909 	expr->ctype = &int_ctype;
910 	expr->flags = expr->left->flags & expr->right->flags;
911 	expr->flags &= ~(CEF_CONST_MASK | CEF_ADDR);
912 	return &int_ctype;
913 }
914 
evaluate_binop(struct expression * expr)915 static struct symbol *evaluate_binop(struct expression *expr)
916 {
917 	struct symbol *ltype, *rtype, *ctype;
918 	int lclass = classify_type(expr->left->ctype, &ltype);
919 	int rclass = classify_type(expr->right->ctype, &rtype);
920 	int op = expr->op;
921 
922 	/* number op number */
923 	if (lclass & rclass & TYPE_NUM) {
924 		expr->flags = expr->left->flags & expr->right->flags;
925 		expr->flags &= ~CEF_CONST_MASK;
926 
927 		if ((lclass | rclass) & TYPE_FLOAT) {
928 			switch (op) {
929 			case '+': case '-': case '*': case '/':
930 				break;
931 			default:
932 				return bad_expr_type(expr);
933 			}
934 		}
935 
936 		if (op == SPECIAL_LEFTSHIFT || op == SPECIAL_RIGHTSHIFT) {
937 			// shifts do integer promotions, but that's it.
938 			unrestrict(expr->left, lclass, &ltype);
939 			unrestrict(expr->right, rclass, &rtype);
940 			ctype = ltype = integer_promotion(ltype);
941 			rtype = integer_promotion(rtype);
942 		} else {
943 			// The rest do usual conversions
944 			const unsigned left_not  = expr->left->type == EXPR_PREOP
945 			                           && expr->left->op == '!';
946 			const unsigned right_not = expr->right->type == EXPR_PREOP
947 			                           && expr->right->op == '!';
948 			if ((op == '&' || op == '|') && (left_not || right_not))
949 				warning(expr->pos, "dubious: %sx %c %sy",
950 				        left_not ? "!" : "",
951 					op,
952 					right_not ? "!" : "");
953 
954 			ltype = usual_conversions(op, expr->left, expr->right,
955 						  lclass, rclass, ltype, rtype);
956 			ctype = rtype = ltype;
957 		}
958 
959 		expr->left = cast_to(expr->left, ltype);
960 		expr->right = cast_to(expr->right, rtype);
961 		expr->ctype = ctype;
962 		return ctype;
963 	}
964 
965 	/* pointer (+|-) integer */
966 	if (lclass & TYPE_PTR && is_int(rclass) && (op == '+' || op == '-')) {
967 		unrestrict(expr->right, rclass, &rtype);
968 		return evaluate_ptr_add(expr, rtype);
969 	}
970 
971 	/* integer + pointer */
972 	if (rclass & TYPE_PTR && is_int(lclass) && op == '+') {
973 		struct expression *index = expr->left;
974 		unrestrict(index, lclass, &ltype);
975 		expr->left = expr->right;
976 		expr->right = index;
977 		return evaluate_ptr_add(expr, ltype);
978 	}
979 
980 	/* pointer - pointer */
981 	if (lclass & rclass & TYPE_PTR && expr->op == '-')
982 		return evaluate_ptr_sub(expr);
983 
984 	return bad_expr_type(expr);
985 }
986 
evaluate_comma(struct expression * expr)987 static struct symbol *evaluate_comma(struct expression *expr)
988 {
989 	expr->ctype = unqualify_type(degenerate(expr->right));
990 	if (expr->ctype == &null_ctype)
991 		expr->ctype = &ptr_ctype;
992 	expr->flags &= expr->left->flags & expr->right->flags;
993 	return expr->ctype;
994 }
995 
modify_for_unsigned(int op)996 static int modify_for_unsigned(int op)
997 {
998 	if (op == '<')
999 		op = SPECIAL_UNSIGNED_LT;
1000 	else if (op == '>')
1001 		op = SPECIAL_UNSIGNED_GT;
1002 	else if (op == SPECIAL_LTE)
1003 		op = SPECIAL_UNSIGNED_LTE;
1004 	else if (op == SPECIAL_GTE)
1005 		op = SPECIAL_UNSIGNED_GTE;
1006 	return op;
1007 }
1008 
1009 enum null_constant_type {
1010 	NON_NULL,
1011 	NULL_PTR,
1012 	NULL_ZERO,
1013 };
1014 
is_null_pointer_constant(struct expression * e)1015 static inline int is_null_pointer_constant(struct expression *e)
1016 {
1017 	if (e->ctype == &null_ctype)
1018 		return NULL_PTR;
1019 	if (!(e->flags & CEF_ICE))
1020 		return NON_NULL;
1021 	return is_zero_constant(e) ? NULL_ZERO : NON_NULL;
1022 }
1023 
evaluate_compare(struct expression * expr)1024 static struct symbol *evaluate_compare(struct expression *expr)
1025 {
1026 	struct expression *left = expr->left, *right = expr->right;
1027 	struct symbol *ltype, *rtype, *lbase, *rbase;
1028 	int lclass = classify_type(degenerate(left), &ltype);
1029 	int rclass = classify_type(degenerate(right), &rtype);
1030 	struct symbol *ctype;
1031 	const char *typediff;
1032 
1033 	/* Type types? */
1034 	if (is_type_type(ltype) && is_type_type(rtype)) {
1035 		/*
1036 		 * __builtin_types_compatible_p() yields an integer
1037 		 * constant expression
1038 		 */
1039 		expr->flags = CEF_SET_ICE;
1040 		goto OK;
1041 	}
1042 
1043 	if (is_safe_type(left->ctype) || is_safe_type(right->ctype))
1044 		warning(expr->pos, "testing a 'safe expression'");
1045 
1046 	expr->flags = left->flags & right->flags & ~CEF_CONST_MASK & ~CEF_ADDR;
1047 
1048 	/* number on number */
1049 	if (lclass & rclass & TYPE_NUM) {
1050 		ctype = usual_conversions(expr->op, expr->left, expr->right,
1051 					  lclass, rclass, ltype, rtype);
1052 		expr->left = cast_to(expr->left, ctype);
1053 		expr->right = cast_to(expr->right, ctype);
1054 		if (ctype->ctype.modifiers & MOD_UNSIGNED)
1055 			expr->op = modify_for_unsigned(expr->op);
1056 		goto OK;
1057 	}
1058 
1059 	/* at least one must be a pointer */
1060 	if (!((lclass | rclass) & TYPE_PTR))
1061 		return bad_expr_type(expr);
1062 
1063 	/* equality comparisons can be with null pointer constants */
1064 	if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1065 		int is_null1 = is_null_pointer_constant(left);
1066 		int is_null2 = is_null_pointer_constant(right);
1067 		if (is_null1 == NULL_ZERO)
1068 			bad_null(left);
1069 		if (is_null2 == NULL_ZERO)
1070 			bad_null(right);
1071 		if (is_null1 && is_null2) {
1072 			int positive = expr->op == SPECIAL_EQUAL;
1073 			expr->type = EXPR_VALUE;
1074 			expr->value = positive;
1075 			goto OK;
1076 		}
1077 		if (is_null1 && (rclass & TYPE_PTR)) {
1078 			expr->left = cast_to(left, rtype);
1079 			goto OK;
1080 		}
1081 		if (is_null2 && (lclass & TYPE_PTR)) {
1082 			expr->right = cast_to(right, ltype);
1083 			goto OK;
1084 		}
1085 	}
1086 	/* both should be pointers */
1087 	if (!(lclass & rclass & TYPE_PTR))
1088 		return bad_expr_type(expr);
1089 	expr->op = modify_for_unsigned(expr->op);
1090 
1091 	lbase = examine_pointer_target(ltype);
1092 	rbase = examine_pointer_target(rtype);
1093 
1094 	/* they also have special treatment for pointers to void */
1095 	if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1096 		if (ltype->ctype.as == rtype->ctype.as) {
1097 			if (lbase == &void_ctype) {
1098 				expr->right = cast_to(right, ltype);
1099 				goto OK;
1100 			}
1101 			if (rbase == &void_ctype) {
1102 				expr->left = cast_to(left, rtype);
1103 				goto OK;
1104 			}
1105 		}
1106 	}
1107 
1108 	typediff = type_difference(&ltype->ctype, &rtype->ctype,
1109 				   target_qualifiers(rtype),
1110 				   target_qualifiers(ltype));
1111 	if (!typediff)
1112 		goto OK;
1113 
1114 	expression_error(expr, "incompatible types in comparison expression (%s):", typediff);
1115 	info(expr->pos, "   %s", show_typename(ltype));
1116 	info(expr->pos, "   %s", show_typename(rtype));
1117 	return NULL;
1118 
1119 OK:
1120 	/* the result is int [6.5.8(6), 6.5.9(3)]*/
1121 	expr->ctype = &int_ctype;
1122 	return &int_ctype;
1123 }
1124 
1125 /*
1126  * NOTE! The degenerate case of "x ? : y", where we don't
1127  * have a true case, this will possibly promote "x" to the
1128  * same type as "y", and thus _change_ the conditional
1129  * test in the expression. But since promotion is "safe"
1130  * for testing, that's OK.
1131  */
evaluate_conditional_expression(struct expression * expr)1132 static struct symbol *evaluate_conditional_expression(struct expression *expr)
1133 {
1134 	struct expression **cond;
1135 	struct symbol *ctype, *ltype, *rtype, *lbase, *rbase;
1136 	int lclass, rclass;
1137 	const char * typediff;
1138 	int qual;
1139 
1140 	if (!evaluate_conditional(expr->conditional, 0))
1141 		return NULL;
1142 	if (!evaluate_expression(expr->cond_false))
1143 		return NULL;
1144 
1145 	ctype = degenerate(expr->conditional);
1146 	rtype = degenerate(expr->cond_false);
1147 
1148 	cond = &expr->conditional;
1149 	ltype = ctype;
1150 	if (expr->cond_true) {
1151 		if (!evaluate_expression(expr->cond_true))
1152 			return NULL;
1153 		ltype = degenerate(expr->cond_true);
1154 		cond = &expr->cond_true;
1155 	}
1156 
1157 	expr->flags = (expr->conditional->flags & (*cond)->flags &
1158 			expr->cond_false->flags & ~CEF_CONST_MASK);
1159 	/*
1160 	 * In the standard, it is defined that an integer constant expression
1161 	 * shall only have operands that are themselves constant [6.6(6)].
1162 	 * While this definition is very clear for expressions that need all
1163 	 * their operands to be evaluated, for conditional expressions with a
1164 	 * constant condition things are much less obvious.
1165 	 * So, as an extension, do the same as GCC seems to do:
1166 	 *	Consider a conditional expression with a constant condition
1167 	 *	as having the same constantness as the argument corresponding
1168 	 *	to the truth value (including in the case of address constants
1169 	 *	which are defined more stricly [6.6(9)]).
1170 	 */
1171 	if (expr->conditional->flags & (CEF_ACE | CEF_ADDR)) {
1172 		int is_true = expr_truth_value(expr->conditional);
1173 		struct expression *arg = is_true ? *cond : expr->cond_false;
1174 		expr->flags = arg->flags & ~CEF_CONST_MASK;
1175 	}
1176 
1177 	lclass = classify_type(ltype, &ltype);
1178 	rclass = classify_type(rtype, &rtype);
1179 	if (lclass & rclass & TYPE_NUM) {
1180 		ctype = usual_conversions('?', *cond, expr->cond_false,
1181 					  lclass, rclass, ltype, rtype);
1182 		*cond = cast_to(*cond, ctype);
1183 		expr->cond_false = cast_to(expr->cond_false, ctype);
1184 		goto out;
1185 	}
1186 
1187 	if ((lclass | rclass) & TYPE_PTR) {
1188 		int is_null1 = is_null_pointer_constant(*cond);
1189 		int is_null2 = is_null_pointer_constant(expr->cond_false);
1190 
1191 		if (is_null1 && is_null2) {
1192 			*cond = cast_to(*cond, &ptr_ctype);
1193 			expr->cond_false = cast_to(expr->cond_false, &ptr_ctype);
1194 			ctype = &ptr_ctype;
1195 			goto out;
1196 		}
1197 		if (is_null1 && (rclass & TYPE_PTR)) {
1198 			if (is_null1 == NULL_ZERO)
1199 				bad_null(*cond);
1200 			*cond = cast_to(*cond, rtype);
1201 			ctype = rtype;
1202 			goto out;
1203 		}
1204 		if (is_null2 && (lclass & TYPE_PTR)) {
1205 			if (is_null2 == NULL_ZERO)
1206 				bad_null(expr->cond_false);
1207 			expr->cond_false = cast_to(expr->cond_false, ltype);
1208 			ctype = ltype;
1209 			goto out;
1210 		}
1211 		if (!(lclass & rclass & TYPE_PTR)) {
1212 			typediff = "different types";
1213 			goto Err;
1214 		}
1215 		/* OK, it's pointer on pointer */
1216 		if (ltype->ctype.as != rtype->ctype.as) {
1217 			typediff = "different address spaces";
1218 			goto Err;
1219 		}
1220 
1221 		/* need to be lazier here */
1222 		lbase = examine_pointer_target(ltype);
1223 		rbase = examine_pointer_target(rtype);
1224 		qual = target_qualifiers(ltype) | target_qualifiers(rtype);
1225 
1226 		if (lbase == &void_ctype) {
1227 			/* XXX: pointers to function should warn here */
1228 			ctype = ltype;
1229 			goto Qual;
1230 
1231 		}
1232 		if (rbase == &void_ctype) {
1233 			/* XXX: pointers to function should warn here */
1234 			ctype = rtype;
1235 			goto Qual;
1236 		}
1237 		/* XXX: that should be pointer to composite */
1238 		ctype = ltype;
1239 		typediff = type_difference(&ltype->ctype, &rtype->ctype,
1240 					   qual, qual);
1241 		if (!typediff)
1242 			goto Qual;
1243 		goto Err;
1244 	}
1245 
1246 	/* void on void, struct on same struct, union on same union */
1247 	if (ltype == rtype) {
1248 		ctype = ltype;
1249 		goto out;
1250 	}
1251 	typediff = "different base types";
1252 
1253 Err:
1254 	expression_error(expr, "incompatible types in conditional expression (%s):", typediff);
1255 	info(expr->pos, "   %s", show_typename(ltype));
1256 	info(expr->pos, "   %s", show_typename(rtype));
1257 	/*
1258 	 * if the condition is constant, the type is in fact known
1259 	 * so use it, as gcc & clang do.
1260 	 */
1261 	switch (expr_truth_value(expr->conditional)) {
1262 	case 1:	expr->ctype = ltype;
1263 		break;
1264 	case 0: expr->ctype = rtype;
1265 		break;
1266 	default:
1267 		break;
1268 	}
1269 	return NULL;
1270 
1271 out:
1272 	expr->ctype = ctype;
1273 	return ctype;
1274 
1275 Qual:
1276 	if (qual & ~ctype->ctype.modifiers) {
1277 		struct symbol *sym = alloc_symbol(ctype->pos, SYM_PTR);
1278 		*sym = *ctype;
1279 		sym->ctype.modifiers |= qual;
1280 		ctype = sym;
1281 	}
1282 	*cond = cast_to(*cond, ctype);
1283 	expr->cond_false = cast_to(expr->cond_false, ctype);
1284 	goto out;
1285 }
1286 
1287 /* FP assignments can not do modulo or bit operations */
compatible_float_op(int op)1288 static int compatible_float_op(int op)
1289 {
1290 	return	op == SPECIAL_ADD_ASSIGN ||
1291 		op == SPECIAL_SUB_ASSIGN ||
1292 		op == SPECIAL_MUL_ASSIGN ||
1293 		op == SPECIAL_DIV_ASSIGN;
1294 }
1295 
evaluate_assign_op(struct expression * expr)1296 static int evaluate_assign_op(struct expression *expr)
1297 {
1298 	struct symbol *target = expr->left->ctype;
1299 	struct symbol *source = expr->right->ctype;
1300 	struct symbol *t, *s;
1301 	int tclass = classify_type(target, &t);
1302 	int sclass = classify_type(source, &s);
1303 	int op = expr->op;
1304 
1305 	if (tclass & sclass & TYPE_NUM) {
1306 		if (tclass & TYPE_FLOAT && !compatible_float_op(op)) {
1307 			expression_error(expr, "invalid assignment");
1308 			return 0;
1309 		}
1310 		if (tclass & TYPE_RESTRICT) {
1311 			if (!restricted_binop(op, t)) {
1312 				warning(expr->pos, "bad assignment (%s) to %s",
1313 					show_special(op), show_typename(t));
1314 				expr->right = cast_to(expr->right, target);
1315 				return 0;
1316 			}
1317 			/* allowed assignments unfoul */
1318 			if (sclass & TYPE_FOULED && unfoul(s) == t)
1319 				goto Cast;
1320 			if (!restricted_value(expr->right, t))
1321 				return 1;
1322 		} else if (op == SPECIAL_SHR_ASSIGN || op == SPECIAL_SHL_ASSIGN) {
1323 			// shifts do integer promotions, but that's it.
1324 			unrestrict(expr->left, tclass, &t);
1325 			target = integer_promotion(t);
1326 
1327 			unrestrict(expr->right, sclass, &s);
1328 			source = integer_promotion(s);
1329 			expr->right = cast_to(expr->right, source);
1330 
1331 			// both gcc & clang seems to do this, so ...
1332 			if (target->bit_size > source->bit_size)
1333 				expr->right = cast_to(expr->right, &uint_ctype);
1334 
1335 			goto Cast;
1336 		} else if (!(sclass & TYPE_RESTRICT))
1337 			goto usual;
1338 		/* source and target would better be identical restricted */
1339 		if (t == s)
1340 			return 1;
1341 		warning(expr->pos, "invalid assignment: %s", show_special(op));
1342 		info(expr->pos, "   left side has type %s", show_typename(t));
1343 		info(expr->pos, "   right side has type %s", show_typename(s));
1344 		expr->right = cast_to(expr->right, target);
1345 		return 0;
1346 	}
1347 	if (tclass == TYPE_PTR && is_int(sclass)) {
1348 		if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1349 			unrestrict(expr->right, sclass, &s);
1350 			evaluate_ptr_add(expr, s);
1351 			return 1;
1352 		}
1353 		expression_error(expr, "invalid pointer assignment");
1354 		return 0;
1355 	}
1356 
1357 	expression_error(expr, "invalid assignment");
1358 	return 0;
1359 
1360 usual:
1361 	target = usual_conversions(op, expr->left, expr->right,
1362 				tclass, sclass, target, source);
1363 Cast:
1364 	expr->right = cast_to(expr->right, target);
1365 	return 1;
1366 }
1367 
whitelist_pointers(struct symbol * t1,struct symbol * t2)1368 static int whitelist_pointers(struct symbol *t1, struct symbol *t2)
1369 {
1370 	if (t1 == t2)
1371 		return 0;	/* yes, 0 - we don't want a cast_to here */
1372 	if (t1 == &void_ctype)
1373 		return 1;
1374 	if (t2 == &void_ctype)
1375 		return 1;
1376 	if (classify_type(t1, &t1) != TYPE_NUM)
1377 		return 0;
1378 	if (classify_type(t2, &t2) != TYPE_NUM)
1379 		return 0;
1380 	if (t1 == t2)
1381 		return 1;
1382 	if (t1->rank == -2 && t2->rank == -2)
1383 		return 1;
1384 	if (t1->rank != t2->rank)
1385 		return 0;
1386 	return !Wtypesign;
1387 }
1388 
check_assignment_types(struct symbol * target,struct expression ** rp,const char ** typediff)1389 static int check_assignment_types(struct symbol *target, struct expression **rp,
1390 	const char **typediff)
1391 {
1392 	struct symbol *source = degenerate(*rp);
1393 	struct symbol *t, *s;
1394 	int tclass = classify_type(target, &t);
1395 	int sclass = classify_type(source, &s);
1396 
1397 	if (tclass & sclass & TYPE_NUM) {
1398 		if (tclass & TYPE_RESTRICT) {
1399 			/* allowed assignments unfoul */
1400 			if (sclass & TYPE_FOULED && unfoul(s) == t)
1401 				goto Cast;
1402 			if (!restricted_value(*rp, target))
1403 				goto Cast;
1404 			if (s == t)
1405 				return 1;
1406 		} else if (!(sclass & TYPE_RESTRICT))
1407 			goto Cast;
1408                 if (t == &bool_ctype) {
1409                         if (is_fouled_type(s))
1410                                 warning((*rp)->pos, "%s degrades to integer",
1411                                         show_typename(s->ctype.base_type));
1412                         goto Cast;
1413                 }
1414 		*typediff = "different base types";
1415 		return 0;
1416 	}
1417 
1418 	if (tclass == TYPE_PTR) {
1419 		unsigned long mod1, mod2;
1420 		unsigned long modl, modr;
1421 		struct symbol *b1, *b2;
1422 		// NULL pointer is always OK
1423 		int is_null = is_null_pointer_constant(*rp);
1424 		if (is_null) {
1425 			if (is_null == NULL_ZERO)
1426 				bad_null(*rp);
1427 			goto Cast;
1428 		}
1429 		if (!(sclass & TYPE_PTR)) {
1430 			*typediff = "different base types";
1431 			return 0;
1432 		}
1433 		b1 = examine_pointer_target(t);
1434 		b2 = examine_pointer_target(s);
1435 		mod1 = t->ctype.modifiers & MOD_IGN;
1436 		mod2 = s->ctype.modifiers & MOD_IGN;
1437 		if (whitelist_pointers(b1, b2)) {
1438 			/*
1439 			 * assignments to/from void * are OK, provided that
1440 			 * we do not remove qualifiers from pointed to [C]
1441 			 * or mix address spaces [sparse].
1442 			 */
1443 			if (t->ctype.as != s->ctype.as) {
1444 				*typediff = "different address spaces";
1445 				return 0;
1446 			}
1447 			/*
1448 			 * If this is a function pointer assignment, it is
1449 			 * actually fine to assign a pointer to const data to
1450 			 * it, as a function pointer points to const data
1451 			 * implicitly, i.e., dereferencing it does not produce
1452 			 * an lvalue.
1453 			 */
1454 			if (b1->type == SYM_FN)
1455 				mod1 |= MOD_CONST;
1456 			if (mod2 & ~mod1 & ~MOD_FUN_ATTR) {
1457 				*typediff = "different modifiers";
1458 				return 0;
1459 			}
1460 			goto Cast;
1461 		}
1462 		/* It's OK if the target is more volatile or const than the source */
1463 		/* It's OK if the source is more pure/noreturn than the target */
1464 		modr = mod1 & ~MOD_REV_QUAL;
1465 		modl = mod2 &  MOD_REV_QUAL;
1466 		*typediff = type_difference(&t->ctype, &s->ctype, modl, modr);
1467 		if (*typediff)
1468 			return 0;
1469 		return 1;
1470 	}
1471 
1472 	if ((tclass & TYPE_COMPOUND) && s == t)
1473 		return 1;
1474 
1475 	if (tclass & TYPE_NUM) {
1476 		/* XXX: need to turn into comparison with NULL */
1477 		if (t == &bool_ctype && (sclass & TYPE_PTR))
1478 			goto Cast;
1479 		*typediff = "different base types";
1480 		return 0;
1481 	}
1482 	*typediff = "invalid types";
1483 	return 0;
1484 
1485 Cast:
1486 	*rp = cast_to(*rp, target);
1487 	return 1;
1488 }
1489 
compatible_assignment_types(struct expression * expr,struct symbol * target,struct expression ** rp,const char * where)1490 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1491 	struct expression **rp, const char *where)
1492 {
1493 	const char *typediff;
1494 
1495 	if (!check_assignment_types(target, rp, &typediff)) {
1496 		struct symbol *source = *rp ? (*rp)->ctype : NULL;
1497 		warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1498 		info(expr->pos, "   expected %s", show_typename(target));
1499 		info(expr->pos, "   got %s", show_typename(source));
1500 		*rp = cast_to(*rp, target);
1501 		return 0;
1502 	}
1503 
1504 	return 1;
1505 }
1506 
compatible_transparent_union(struct symbol * target,struct expression ** rp)1507 static int compatible_transparent_union(struct symbol *target,
1508 	struct expression **rp)
1509 {
1510 	struct symbol *t, *member;
1511 	classify_type(target, &t);
1512 	if (t->type != SYM_UNION || !t->transparent_union)
1513 		return 0;
1514 
1515 	FOR_EACH_PTR(t->symbol_list, member) {
1516 		const char *typediff;
1517 		if (check_assignment_types(member, rp, &typediff))
1518 			return 1;
1519 	} END_FOR_EACH_PTR(member);
1520 
1521 	return 0;
1522 }
1523 
compatible_argument_type(struct expression * expr,struct symbol * target,struct expression ** rp,const char * where)1524 static int compatible_argument_type(struct expression *expr, struct symbol *target,
1525 	struct expression **rp, const char *where)
1526 {
1527 	if (compatible_transparent_union(target, rp))
1528 		return 1;
1529 
1530 	return compatible_assignment_types(expr, target, rp, where);
1531 }
1532 
mark_addressable(struct expression * expr)1533 static void mark_addressable(struct expression *expr)
1534 {
1535 	while (expr->type == EXPR_BINOP && expr->op == '+')
1536 		expr = expr->left;
1537 	if (expr->type == EXPR_SYMBOL) {
1538 		struct symbol *sym = expr->symbol;
1539 		sym->ctype.modifiers |= MOD_ADDRESSABLE;
1540 	}
1541 }
1542 
mark_assigned(struct expression * expr)1543 static void mark_assigned(struct expression *expr)
1544 {
1545 	struct symbol *sym;
1546 
1547 	if (!expr)
1548 		return;
1549 	switch (expr->type) {
1550 	case EXPR_SYMBOL:
1551 		sym = expr->symbol;
1552 		if (!sym)
1553 			return;
1554 		if (sym->type != SYM_NODE)
1555 			return;
1556 		sym->ctype.modifiers |= MOD_ASSIGNED;
1557 		return;
1558 
1559 	case EXPR_BINOP:
1560 		mark_assigned(expr->left);
1561 		mark_assigned(expr->right);
1562 		return;
1563 	case EXPR_CAST:
1564 	case EXPR_FORCE_CAST:
1565 		mark_assigned(expr->cast_expression);
1566 		return;
1567 	case EXPR_SLICE:
1568 		mark_assigned(expr->base);
1569 		return;
1570 	default:
1571 		/* Hmm? */
1572 		return;
1573 	}
1574 }
1575 
evaluate_assign_to(struct expression * left,struct symbol * type)1576 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1577 {
1578 	if (type->ctype.modifiers & MOD_CONST)
1579 		expression_error(left, "assignment to const expression");
1580 
1581 	/* We know left is an lvalue, so it's a "preop-*" */
1582 	mark_assigned(left->unop);
1583 }
1584 
evaluate_assignment(struct expression * expr)1585 static struct symbol *evaluate_assignment(struct expression *expr)
1586 {
1587 	struct expression *left = expr->left;
1588 	struct symbol *ltype;
1589 
1590 	if (!lvalue_expression(left)) {
1591 		expression_error(expr, "not an lvalue");
1592 		return NULL;
1593 	}
1594 
1595 	ltype = left->ctype;
1596 
1597 	if (expr->op != '=') {
1598 		if (!evaluate_assign_op(expr))
1599 			return NULL;
1600 	} else {
1601 		if (!compatible_assignment_types(expr, ltype, &expr->right, "assignment"))
1602 			return NULL;
1603 	}
1604 
1605 	evaluate_assign_to(left, ltype);
1606 
1607 	expr->ctype = ltype;
1608 	return ltype;
1609 }
1610 
examine_fn_arguments(struct symbol * fn)1611 static void examine_fn_arguments(struct symbol *fn)
1612 {
1613 	struct symbol *s;
1614 
1615 	FOR_EACH_PTR(fn->arguments, s) {
1616 		struct symbol *arg = evaluate_symbol(s);
1617 		/* Array/function arguments silently degenerate into pointers */
1618 		if (arg) {
1619 			struct symbol *ptr;
1620 			switch(arg->type) {
1621 			case SYM_ARRAY:
1622 			case SYM_FN:
1623 				ptr = alloc_symbol(s->pos, SYM_PTR);
1624 				if (arg->type == SYM_ARRAY)
1625 					ptr->ctype = arg->ctype;
1626 				else
1627 					ptr->ctype.base_type = arg;
1628 				combine_address_space(s->pos, &ptr->ctype.as, s->ctype.as);
1629 				ptr->ctype.modifiers |= s->ctype.modifiers & MOD_PTRINHERIT;
1630 
1631 				s->ctype.base_type = ptr;
1632 				s->ctype.as = NULL;
1633 				s->ctype.modifiers &= ~MOD_PTRINHERIT;
1634 				s->bit_size = 0;
1635 				s->examined = 0;
1636 				examine_symbol_type(s);
1637 				break;
1638 			default:
1639 				/* nothing */
1640 				break;
1641 			}
1642 		}
1643 	} END_FOR_EACH_PTR(s);
1644 }
1645 
convert_to_as_mod(struct symbol * sym,struct ident * as,int mod)1646 static struct symbol *convert_to_as_mod(struct symbol *sym, struct ident *as, int mod)
1647 {
1648 	/* Take the modifiers of the pointer, and apply them to the member */
1649 	mod |= sym->ctype.modifiers;
1650 	if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1651 		struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1652 		*newsym = *sym;
1653 		newsym->ctype.as = as;
1654 		newsym->ctype.modifiers = mod;
1655 		sym = newsym;
1656 	}
1657 	return sym;
1658 }
1659 
create_pointer(struct expression * expr,struct symbol * sym,int degenerate)1660 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1661 {
1662 	struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1663 	struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1664 
1665 	node->ctype.base_type = ptr;
1666 	ptr->bit_size = bits_in_pointer;
1667 	ptr->ctype.alignment = pointer_alignment;
1668 
1669 	node->bit_size = bits_in_pointer;
1670 	node->ctype.alignment = pointer_alignment;
1671 
1672 	access_symbol(sym);
1673 	if (sym->ctype.modifiers & MOD_REGISTER) {
1674 		warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1675 		sym->ctype.modifiers &= ~MOD_REGISTER;
1676 	}
1677 	if (sym->type == SYM_NODE) {
1678 		combine_address_space(sym->pos, &ptr->ctype.as, sym->ctype.as);
1679 		ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1680 		sym = sym->ctype.base_type;
1681 	}
1682 	if (degenerate && sym->type == SYM_ARRAY) {
1683 		combine_address_space(sym->pos, &ptr->ctype.as, sym->ctype.as);
1684 		ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1685 		sym = sym->ctype.base_type;
1686 	}
1687 	ptr->ctype.base_type = sym;
1688 
1689 	return node;
1690 }
1691 
1692 /* Arrays degenerate into pointers on pointer arithmetic */
degenerate(struct expression * expr)1693 static struct symbol *degenerate(struct expression *expr)
1694 {
1695 	struct symbol *ctype, *base;
1696 
1697 	if (!expr)
1698 		return NULL;
1699 	ctype = expr->ctype;
1700 	if (!ctype)
1701 		return NULL;
1702 	base = examine_symbol_type(ctype);
1703 	if (ctype->type == SYM_NODE)
1704 		base = ctype->ctype.base_type;
1705 	/*
1706 	 * Arrays degenerate into pointers to the entries, while
1707 	 * functions degenerate into pointers to themselves.
1708 	 * If array was part of non-lvalue compound, we create a copy
1709 	 * of that compound first and then act as if we were dealing with
1710 	 * the corresponding field in there.
1711 	 */
1712 	switch (base->type) {
1713 	case SYM_ARRAY:
1714 		if (expr->type == EXPR_SLICE) {
1715 			struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1716 			struct expression *e0, *e1, *e2, *e3, *e4;
1717 
1718 			a->ctype.base_type = expr->base->ctype;
1719 			a->bit_size = expr->base->ctype->bit_size;
1720 			a->array_size = expr->base->ctype->array_size;
1721 
1722 			e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1723 			e0->symbol = a;
1724 			e0->ctype = &lazy_ptr_ctype;
1725 
1726 			e1 = alloc_expression(expr->pos, EXPR_PREOP);
1727 			e1->unop = e0;
1728 			e1->op = '*';
1729 			e1->ctype = expr->base->ctype;	/* XXX */
1730 
1731 			e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1732 			e2->left = e1;
1733 			e2->right = expr->base;
1734 			e2->op = '=';
1735 			e2->ctype = expr->base->ctype;
1736 
1737 			if (expr->r_bitpos) {
1738 				e3 = alloc_expression(expr->pos, EXPR_BINOP);
1739 				e3->op = '+';
1740 				e3->left = e0;
1741 				e3->right = alloc_const_expression(expr->pos,
1742 							bits_to_bytes(expr->r_bitpos));
1743 				e3->ctype = &lazy_ptr_ctype;
1744 			} else {
1745 				e3 = e0;
1746 			}
1747 
1748 			e4 = alloc_expression(expr->pos, EXPR_COMMA);
1749 			e4->left = e2;
1750 			e4->right = e3;
1751 			e4->ctype = &lazy_ptr_ctype;
1752 
1753 			expr->unop = e4;
1754 			expr->type = EXPR_PREOP;
1755 			expr->op = '*';
1756 		}
1757 	case SYM_FN:
1758 		if (expr->op != '*' || expr->type != EXPR_PREOP) {
1759 			expression_error(expr, "strange non-value function or array");
1760 			return &bad_ctype;
1761 		}
1762 		if (ctype->builtin)
1763 			sparse_error(expr->pos, "taking the address of built-in function '%s'", show_ident(ctype->ident));
1764 		*expr = *expr->unop;
1765 		ctype = create_pointer(expr, ctype, 1);
1766 		expr->ctype = ctype;
1767 		mark_addressable(expr);
1768 	default:
1769 		/* nothing */;
1770 	}
1771 	return ctype;
1772 }
1773 
evaluate_addressof(struct expression * expr)1774 static struct symbol *evaluate_addressof(struct expression *expr)
1775 {
1776 	struct expression *op = expr->unop;
1777 	struct symbol *ctype;
1778 
1779 	if (op->op != '*' || op->type != EXPR_PREOP) {
1780 		expression_error(expr, "not addressable");
1781 		return NULL;
1782 	}
1783 	ctype = op->ctype;
1784 	if (ctype->builtin)
1785 		sparse_error(expr->pos, "taking the address of built-in function '%s'", show_ident(ctype->ident));
1786 	*expr = *op->unop;
1787 
1788 	mark_addressable(expr);
1789 
1790 	/*
1791 	 * symbol expression evaluation is lazy about the type
1792 	 * of the sub-expression, so we may have to generate
1793 	 * the type here if so..
1794 	 */
1795 	if (expr->ctype == &lazy_ptr_ctype) {
1796 		ctype = create_pointer(expr, ctype, 0);
1797 		expr->ctype = ctype;
1798 	}
1799 	return expr->ctype;
1800 }
1801 
1802 
evaluate_dereference(struct expression * expr)1803 static struct symbol *evaluate_dereference(struct expression *expr)
1804 {
1805 	struct expression *op = expr->unop;
1806 	struct symbol *ctype = op->ctype, *node, *target;
1807 
1808 	/* Simplify: *&(expr) => (expr) */
1809 	if (op->type == EXPR_PREOP && op->op == '&') {
1810 		*expr = *op->unop;
1811 		expr->flags = CEF_NONE;
1812 		return expr->ctype;
1813 	}
1814 
1815 	examine_symbol_type(ctype);
1816 
1817 	/* Dereferencing a node drops all the node information. */
1818 	if (ctype->type == SYM_NODE)
1819 		ctype = ctype->ctype.base_type;
1820 
1821 	target = ctype->ctype.base_type;
1822 
1823 	switch (ctype->type) {
1824 	default:
1825 		expression_error(expr, "cannot dereference this type");
1826 		return NULL;
1827 	case SYM_FN:
1828 		*expr = *op;
1829 		return expr->ctype;
1830 	case SYM_PTR:
1831 		examine_symbol_type(target);
1832 		node = alloc_symbol(expr->pos, SYM_NODE);
1833 		node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1834 		merge_type(node, ctype);
1835 		break;
1836 
1837 	case SYM_ARRAY:
1838 		if (!lvalue_expression(op)) {
1839 			expression_error(op, "non-lvalue array??");
1840 			return NULL;
1841 		}
1842 
1843 		/* Do the implied "addressof" on the array */
1844 		*op = *op->unop;
1845 
1846 		/*
1847 		 * When an array is dereferenced, we need to pick
1848 		 * up the attributes of the original node too..
1849 		 */
1850 		node = alloc_symbol(expr->pos, SYM_NODE);
1851 		merge_type(node, op->ctype);
1852 		merge_type(node, ctype);
1853 		break;
1854 	}
1855 
1856 	node->bit_size = target->bit_size;
1857 	node->array_size = target->array_size;
1858 
1859 	expr->ctype = node;
1860 	return node;
1861 }
1862 
1863 /*
1864  * Unary post-ops: x++ and x--
1865  */
evaluate_postop(struct expression * expr)1866 static struct symbol *evaluate_postop(struct expression *expr)
1867 {
1868 	struct expression *op = expr->unop;
1869 	struct symbol *ctype = op->ctype;
1870 	int class = classify_type(ctype, &ctype);
1871 	int multiply = 0;
1872 
1873 	if (!class || class & TYPE_COMPOUND) {
1874 		expression_error(expr, "need scalar for ++/--");
1875 		return NULL;
1876 	}
1877 	if (!lvalue_expression(expr->unop)) {
1878 		expression_error(expr, "need lvalue expression for ++/--");
1879 		return NULL;
1880 	}
1881 
1882 	unrestrict(expr, class, &ctype);
1883 
1884 	if (class & TYPE_NUM) {
1885 		multiply = 1;
1886 	} else if (class == TYPE_PTR) {
1887 		struct symbol *target = examine_pointer_target(ctype);
1888 		if (!is_function(target))
1889 			multiply = bits_to_bytes(target->bit_size);
1890 	}
1891 
1892 	if (multiply) {
1893 		evaluate_assign_to(op, op->ctype);
1894 		expr->op_value = multiply;
1895 		expr->ctype = ctype;
1896 		return ctype;
1897 	}
1898 
1899 	expression_error(expr, "bad argument type for ++/--");
1900 	return NULL;
1901 }
1902 
evaluate_sign(struct expression * expr)1903 static struct symbol *evaluate_sign(struct expression *expr)
1904 {
1905 	struct symbol *ctype = expr->unop->ctype;
1906 	int class = classify_type(ctype, &ctype);
1907 	unsigned char flags = expr->unop->flags & ~CEF_CONST_MASK;
1908 
1909 	/* should be an arithmetic type */
1910 	if (!(class & TYPE_NUM))
1911 		return bad_expr_type(expr);
1912 	if (class & TYPE_RESTRICT)
1913 		goto Restr;
1914 Normal:
1915 	if (!(class & TYPE_FLOAT)) {
1916 		ctype = integer_promotion(ctype);
1917 		expr->unop = cast_to(expr->unop, ctype);
1918 	} else if (expr->op != '~') {
1919 		/* no conversions needed */
1920 	} else {
1921 		return bad_expr_type(expr);
1922 	}
1923 	if (expr->op == '+')
1924 		*expr = *expr->unop;
1925 	expr->flags = flags;
1926 	expr->ctype = ctype;
1927 	return ctype;
1928 Restr:
1929 	if (restricted_unop(expr->op, &ctype))
1930 		unrestrict(expr, class, &ctype);
1931 	goto Normal;
1932 }
1933 
evaluate_preop(struct expression * expr)1934 static struct symbol *evaluate_preop(struct expression *expr)
1935 {
1936 	struct symbol *ctype = expr->unop->ctype;
1937 
1938 	switch (expr->op) {
1939 	case '(':
1940 		*expr = *expr->unop;
1941 		return ctype;
1942 
1943 	case '+':
1944 	case '-':
1945 	case '~':
1946 		return evaluate_sign(expr);
1947 
1948 	case '*':
1949 		return evaluate_dereference(expr);
1950 
1951 	case '&':
1952 		return evaluate_addressof(expr);
1953 
1954 	case SPECIAL_INCREMENT:
1955 	case SPECIAL_DECREMENT:
1956 		/*
1957 		 * From a type evaluation standpoint the preops are
1958 		 * the same as the postops
1959 		 */
1960 		return evaluate_postop(expr);
1961 
1962 	case '!':
1963 		ctype = degenerate(expr->unop);
1964 		expr->flags = expr->unop->flags & ~CEF_CONST_MASK;
1965 		/*
1966 		 * A logical negation never yields an address constant
1967 		 * [6.6(9)].
1968 		 */
1969 		expr->flags &= ~CEF_ADDR;
1970 
1971 		if (is_safe_type(ctype))
1972 			warning(expr->pos, "testing a 'safe expression'");
1973 		if (is_float_type(ctype)) {
1974 			struct expression *arg = expr->unop;
1975 			expr->type = EXPR_COMPARE;
1976 			expr->op = SPECIAL_EQUAL;
1977 			expr->left = arg;
1978 			expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1979 			expr->right->ctype = ctype;
1980 			expr->right->fvalue = 0;
1981 		} else if (is_fouled_type(ctype)) {
1982 			warning(expr->pos, "%s degrades to integer",
1983 				show_typename(ctype->ctype.base_type));
1984 		}
1985 		/* the result is int [6.5.3.3(5)]*/
1986 		ctype = &int_ctype;
1987 		break;
1988 
1989 	default:
1990 		break;
1991 	}
1992 	expr->ctype = ctype;
1993 	return ctype;
1994 }
1995 
find_identifier(struct ident * ident,struct symbol_list * _list,int * offset)1996 static struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1997 {
1998 	struct ptr_list *head = (struct ptr_list *)_list;
1999 	struct ptr_list *list = head;
2000 
2001 	if (!head)
2002 		return NULL;
2003 	do {
2004 		int i;
2005 		for (i = 0; i < list->nr; i++) {
2006 			struct symbol *sym = (struct symbol *) list->list[i];
2007 			if (sym->ident) {
2008 				if (sym->ident != ident)
2009 					continue;
2010 				*offset = sym->offset;
2011 				return sym;
2012 			} else {
2013 				struct symbol *ctype = sym->ctype.base_type;
2014 				struct symbol *sub;
2015 				if (!ctype)
2016 					continue;
2017 				if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
2018 					continue;
2019 				sub = find_identifier(ident, ctype->symbol_list, offset);
2020 				if (!sub)
2021 					continue;
2022 				*offset += sym->offset;
2023 				return sub;
2024 			}
2025 		}
2026 	} while ((list = list->next) != head);
2027 	return NULL;
2028 }
2029 
evaluate_offset(struct expression * expr,unsigned long offset)2030 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
2031 {
2032 	struct expression *add;
2033 
2034 	/*
2035 	 * Create a new add-expression
2036 	 *
2037 	 * NOTE! Even if we just add zero, we need a new node
2038 	 * for the member pointer, since it has a different
2039 	 * type than the original pointer. We could make that
2040 	 * be just a cast, but the fact is, a node is a node,
2041 	 * so we might as well just do the "add zero" here.
2042 	 */
2043 	add = alloc_expression(expr->pos, EXPR_BINOP);
2044 	add->op = '+';
2045 	add->left = expr;
2046 	add->right = alloc_expression(expr->pos, EXPR_VALUE);
2047 	add->right->ctype = &int_ctype;
2048 	add->right->value = offset;
2049 
2050 	/*
2051 	 * The ctype of the pointer will be lazily evaluated if
2052 	 * we ever take the address of this member dereference..
2053 	 */
2054 	add->ctype = &lazy_ptr_ctype;
2055 	/*
2056 	 * The resulting address of a member access through an address
2057 	 * constant is an address constant again [6.6(9)].
2058 	 */
2059 	add->flags = expr->flags;
2060 
2061 	return add;
2062 }
2063 
2064 /* structure/union dereference */
evaluate_member_dereference(struct expression * expr)2065 static struct symbol *evaluate_member_dereference(struct expression *expr)
2066 {
2067 	int offset;
2068 	struct symbol *ctype, *member;
2069 	struct expression *deref = expr->deref, *add;
2070 	struct ident *ident = expr->member;
2071 	struct ident *address_space;
2072 	unsigned int mod;
2073 
2074 	if (!evaluate_expression(deref))
2075 		return NULL;
2076 	if (!ident) {
2077 		expression_error(expr, "bad member name");
2078 		return NULL;
2079 	}
2080 
2081 	ctype = deref->ctype;
2082 	examine_symbol_type(ctype);
2083 	address_space = ctype->ctype.as;
2084 	mod = ctype->ctype.modifiers;
2085 	if (ctype->type == SYM_NODE) {
2086 		ctype = ctype->ctype.base_type;
2087 		combine_address_space(deref->pos, &address_space, ctype->ctype.as);
2088 		mod |= ctype->ctype.modifiers;
2089 	}
2090 	if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
2091 		expression_error(expr, "expected structure or union");
2092 		return NULL;
2093 	}
2094 	offset = 0;
2095 	member = find_identifier(ident, ctype->symbol_list, &offset);
2096 	if (!member) {
2097 		const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
2098 		const char *name = "<unnamed>";
2099 		int namelen = 9;
2100 		if (ctype->ident) {
2101 			name = ctype->ident->name;
2102 			namelen = ctype->ident->len;
2103 		}
2104 		if (ctype->symbol_list)
2105 			expression_error(expr, "no member '%s' in %s %.*s",
2106 				show_ident(ident), type, namelen, name);
2107 		else
2108 			expression_error(expr, "using member '%s' in "
2109 				"incomplete %s %.*s", show_ident(ident),
2110 				type, namelen, name);
2111 		return NULL;
2112 	}
2113 
2114 	/*
2115 	 * The member needs to take on the address space and modifiers of
2116 	 * the "parent" type.
2117 	 */
2118 	member = convert_to_as_mod(member, address_space, mod);
2119 	ctype = get_base_type(member);
2120 
2121 	if (!lvalue_expression(deref)) {
2122 		if (deref->type != EXPR_SLICE) {
2123 			expr->base = deref;
2124 			expr->r_bitpos = 0;
2125 		} else {
2126 			expr->base = deref->base;
2127 			expr->r_bitpos = deref->r_bitpos;
2128 		}
2129 		expr->r_bitpos += bytes_to_bits(offset);
2130 		expr->type = EXPR_SLICE;
2131 		expr->r_bitpos += member->bit_offset;
2132 		expr->ctype = member;
2133 		return member;
2134 	}
2135 
2136 	deref = deref->unop;
2137 	expr->deref = deref;
2138 
2139 	add = evaluate_offset(deref, offset);
2140 	expr->type = EXPR_PREOP;
2141 	expr->op = '*';
2142 	expr->unop = add;
2143 
2144 	expr->ctype = member;
2145 	return member;
2146 }
2147 
is_promoted(struct expression * expr)2148 static int is_promoted(struct expression *expr)
2149 {
2150 	while (1) {
2151 		switch (expr->type) {
2152 		case EXPR_BINOP:
2153 		case EXPR_SELECT:
2154 		case EXPR_CONDITIONAL:
2155 			return 1;
2156 		case EXPR_COMMA:
2157 			expr = expr->right;
2158 			continue;
2159 		case EXPR_PREOP:
2160 			switch (expr->op) {
2161 			case '(':
2162 				expr = expr->unop;
2163 				continue;
2164 			case '+':
2165 			case '-':
2166 			case '~':
2167 				return 1;
2168 			default:
2169 				return 0;
2170 			}
2171 		default:
2172 			return 0;
2173 		}
2174 	}
2175 }
2176 
2177 
evaluate_type_information(struct expression * expr)2178 static struct symbol *evaluate_type_information(struct expression *expr)
2179 {
2180 	struct symbol *sym = expr->cast_type;
2181 	if (!sym) {
2182 		sym = evaluate_expression(expr->cast_expression);
2183 		if (!sym)
2184 			return NULL;
2185 		/*
2186 		 * Expressions of restricted types will possibly get
2187 		 * promoted - check that here
2188 		 */
2189 		if (is_restricted_type(sym)) {
2190 			if (sym->bit_size < bits_in_int && is_promoted(expr))
2191 				sym = &int_ctype;
2192 		} else if (is_fouled_type(sym)) {
2193 			sym = &int_ctype;
2194 		}
2195 	}
2196 	examine_symbol_type(sym);
2197 	if (is_bitfield_type(sym)) {
2198 		expression_error(expr, "trying to examine bitfield type");
2199 		return NULL;
2200 	}
2201 	return sym;
2202 }
2203 
evaluate_sizeof(struct expression * expr)2204 static struct symbol *evaluate_sizeof(struct expression *expr)
2205 {
2206 	struct symbol *type;
2207 	int size;
2208 
2209 	type = evaluate_type_information(expr);
2210 	if (!type)
2211 		return NULL;
2212 
2213 	size = type->bit_size;
2214 
2215 	if (size < 0 && is_void_type(type)) {
2216 		if (Wpointer_arith)
2217 			warning(expr->pos, "expression using sizeof(void)");
2218 		size = bits_in_char;
2219 	}
2220 
2221 	if (is_bool_type(type)) {
2222 		if (Wsizeof_bool)
2223 			warning(expr->pos, "expression using sizeof _Bool");
2224 		size = bits_to_bytes(bits_in_bool) * bits_in_char;
2225 	}
2226 
2227 	if (is_function(type->ctype.base_type)) {
2228 		if (Wpointer_arith)
2229 			warning(expr->pos, "expression using sizeof on a function");
2230 		size = bits_in_char;
2231 	}
2232 
2233 	if (has_flexible_array(type) && Wflexible_array_sizeof)
2234 		warning(expr->pos, "using sizeof on a flexible structure");
2235 
2236 	if (is_array_type(type) && size < 0) {	// VLA, 1-dimension only
2237 		struct expression *base, *size;
2238 		struct symbol *base_type;
2239 
2240 		if (type->type == SYM_NODE)
2241 			type = type->ctype.base_type;	// strip the SYM_NODE
2242 		base_type = get_base_type(type);
2243 		if (!base_type)
2244 			goto error;
2245 		if (base_type->bit_size <= 0) {
2246 			base = alloc_expression(expr->pos, EXPR_SIZEOF);
2247 			base->cast_type = base_type;
2248 			if (!evaluate_sizeof(base))
2249 				goto error;
2250 		} else {
2251 			base = alloc_expression(expr->pos, EXPR_VALUE);
2252 			base->value = bits_to_bytes(base_type->bit_size);
2253 			base->ctype = size_t_ctype;
2254 		}
2255 		size = alloc_expression(expr->pos, EXPR_CAST);
2256 		size->cast_type = size_t_ctype;
2257 		size->cast_expression = type->array_size;
2258 		if (!evaluate_expression(size))
2259 			goto error;
2260 		expr->left = size;
2261 		expr->right = base;
2262 		expr->type = EXPR_BINOP;
2263 		expr->op = '*';
2264 		return expr->ctype = size_t_ctype;
2265 	}
2266 
2267 error:
2268 	if ((size < 0) || (size & (bits_in_char - 1)))
2269 		expression_error(expr, "cannot size expression");
2270 
2271 	expr->type = EXPR_VALUE;
2272 	expr->value = bits_to_bytes(size);
2273 	expr->taint = 0;
2274 	expr->ctype = size_t_ctype;
2275 	return size_t_ctype;
2276 }
2277 
evaluate_ptrsizeof(struct expression * expr)2278 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
2279 {
2280 	struct symbol *type;
2281 	int size;
2282 
2283 	type = evaluate_type_information(expr);
2284 	if (!type)
2285 		return NULL;
2286 
2287 	if (type->type == SYM_NODE)
2288 		type = type->ctype.base_type;
2289 	if (!type)
2290 		return NULL;
2291 	switch (type->type) {
2292 	case SYM_ARRAY:
2293 		break;
2294 	case SYM_PTR:
2295 		type = get_base_type(type);
2296 		if (type)
2297 			break;
2298 	default:
2299 		expression_error(expr, "expected pointer expression");
2300 		return NULL;
2301 	}
2302 	size = type->bit_size;
2303 	if (size & (bits_in_char-1))
2304 		size = 0;
2305 	expr->type = EXPR_VALUE;
2306 	expr->value = bits_to_bytes(size);
2307 	expr->taint = 0;
2308 	expr->ctype = size_t_ctype;
2309 	return size_t_ctype;
2310 }
2311 
evaluate_alignof(struct expression * expr)2312 static struct symbol *evaluate_alignof(struct expression *expr)
2313 {
2314 	struct symbol *type;
2315 
2316 	type = evaluate_type_information(expr);
2317 	if (!type)
2318 		return NULL;
2319 
2320 	expr->type = EXPR_VALUE;
2321 	expr->value = type->ctype.alignment;
2322 	expr->taint = 0;
2323 	expr->ctype = size_t_ctype;
2324 	return size_t_ctype;
2325 }
2326 
evaluate_arguments(struct symbol_list * argtypes,struct expression_list * head)2327 int evaluate_arguments(struct symbol_list *argtypes, struct expression_list *head)
2328 {
2329 	struct expression *expr;
2330 	struct symbol *argtype;
2331 	int i = 1;
2332 
2333 	PREPARE_PTR_LIST(argtypes, argtype);
2334 	FOR_EACH_PTR (head, expr) {
2335 		struct expression **p = THIS_ADDRESS(expr);
2336 		struct symbol *ctype, *target;
2337 		ctype = evaluate_expression(expr);
2338 
2339 		if (!ctype)
2340 			return 0;
2341 
2342 		target = argtype;
2343 		if (!target) {
2344 			struct symbol *type;
2345 			int class = classify_type(ctype, &type);
2346 			if (is_int(class)) {
2347 				*p = cast_to(expr, integer_promotion(type));
2348 			} else if (class & TYPE_FLOAT) {
2349 				if (type->rank < 0)
2350 					*p = cast_to(expr, &double_ctype);
2351 			} else if (class & TYPE_PTR) {
2352 				if (expr->ctype == &null_ctype)
2353 					*p = cast_to(expr, &ptr_ctype);
2354 				else
2355 					degenerate(expr);
2356 			}
2357 		} else if (!target->forced_arg){
2358 			static char where[30];
2359 			examine_symbol_type(target);
2360 			sprintf(where, "argument %d", i);
2361 			compatible_argument_type(expr, target, p, where);
2362 		}
2363 
2364 		i++;
2365 		NEXT_PTR_LIST(argtype);
2366 	} END_FOR_EACH_PTR(expr);
2367 	FINISH_PTR_LIST(argtype);
2368 	return 1;
2369 }
2370 
convert_index(struct expression * e)2371 static void convert_index(struct expression *e)
2372 {
2373 	struct expression *child = e->idx_expression;
2374 	unsigned from = e->idx_from;
2375 	unsigned to = e->idx_to + 1;
2376 	e->type = EXPR_POS;
2377 	e->init_offset = from * bits_to_bytes(e->ctype->bit_size);
2378 	e->init_nr = to - from;
2379 	e->init_expr = child;
2380 }
2381 
convert_ident(struct expression * e)2382 static void convert_ident(struct expression *e)
2383 {
2384 	struct expression *child = e->ident_expression;
2385 	int offset = e->offset;
2386 
2387 	e->type = EXPR_POS;
2388 	e->init_offset = offset;
2389 	e->init_nr = 1;
2390 	e->init_expr = child;
2391 }
2392 
convert_designators(struct expression * e)2393 static void convert_designators(struct expression *e)
2394 {
2395 	while (e) {
2396 		if (e->type == EXPR_INDEX)
2397 			convert_index(e);
2398 		else if (e->type == EXPR_IDENTIFIER)
2399 			convert_ident(e);
2400 		else
2401 			break;
2402 		e = e->init_expr;
2403 	}
2404 }
2405 
excess(struct expression * e,const char * s)2406 static void excess(struct expression *e, const char *s)
2407 {
2408 	warning(e->pos, "excessive elements in %s initializer", s);
2409 }
2410 
2411 /*
2412  * implicit designator for the first element
2413  */
first_subobject(struct symbol * ctype,int class,struct expression ** v)2414 static struct expression *first_subobject(struct symbol *ctype, int class,
2415 					  struct expression **v)
2416 {
2417 	struct expression *e = *v, *new;
2418 
2419 	if (ctype->type == SYM_NODE)
2420 		ctype = ctype->ctype.base_type;
2421 
2422 	if (class & TYPE_PTR) { /* array */
2423 		if (!ctype->bit_size)
2424 			return NULL;
2425 		new = alloc_expression(e->pos, EXPR_INDEX);
2426 		new->idx_expression = e;
2427 		new->ctype = ctype->ctype.base_type;
2428 	} else  {
2429 		struct symbol *field, *p;
2430 		PREPARE_PTR_LIST(ctype->symbol_list, p);
2431 		while (p && !p->ident && is_bitfield_type(p))
2432 			NEXT_PTR_LIST(p);
2433 		field = p;
2434 		FINISH_PTR_LIST(p);
2435 		if (!field)
2436 			return NULL;
2437 		new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2438 		new->ident_expression = e;
2439 		new->field = new->ctype = field;
2440 		new->offset = field->offset;
2441 	}
2442 	*v = new;
2443 	return new;
2444 }
2445 
2446 /*
2447  * sanity-check explicit designators; return the innermost one or NULL
2448  * in case of error.  Assign types.
2449  */
check_designators(struct expression * e,struct symbol * ctype)2450 static struct expression *check_designators(struct expression *e,
2451 					    struct symbol *ctype)
2452 {
2453 	struct expression *last = NULL;
2454 	const char *err;
2455 	while (1) {
2456 		if (ctype->type == SYM_NODE)
2457 			ctype = ctype->ctype.base_type;
2458 		if (e->type == EXPR_INDEX) {
2459 			struct symbol *type;
2460 			if (ctype->type != SYM_ARRAY) {
2461 				err = "array index in non-array";
2462 				break;
2463 			}
2464 			type = ctype->ctype.base_type;
2465 			if (ctype->bit_size >= 0 && type->bit_size >= 0) {
2466 				unsigned offset = array_element_offset(type->bit_size, e->idx_to);
2467 				if (offset >= ctype->bit_size) {
2468 					err = "index out of bounds in";
2469 					break;
2470 				}
2471 			}
2472 			e->ctype = ctype = type;
2473 			ctype = type;
2474 			last = e;
2475 			if (!e->idx_expression) {
2476 				err = "invalid";
2477 				break;
2478 			}
2479 			e = e->idx_expression;
2480 		} else if (e->type == EXPR_IDENTIFIER) {
2481 			int offset = 0;
2482 			if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2483 				err = "field name not in struct or union";
2484 				break;
2485 			}
2486 			ctype = find_identifier(e->expr_ident, ctype->symbol_list, &offset);
2487 			if (!ctype) {
2488 				err = "unknown field name in";
2489 				break;
2490 			}
2491 			e->offset = offset;
2492 			e->field = e->ctype = ctype;
2493 			last = e;
2494 			if (!e->ident_expression) {
2495 				err = "invalid";
2496 				break;
2497 			}
2498 			e = e->ident_expression;
2499 		} else if (e->type == EXPR_POS) {
2500 			err = "internal front-end error: EXPR_POS in";
2501 			break;
2502 		} else
2503 			return last;
2504 	}
2505 	expression_error(e, "%s initializer", err);
2506 	return NULL;
2507 }
2508 
2509 /*
2510  * choose the next subobject to initialize.
2511  *
2512  * Get designators for next element, switch old ones to EXPR_POS.
2513  * Return the resulting expression or NULL if we'd run out of subobjects.
2514  * The innermost designator is returned in *v.  Designators in old
2515  * are assumed to be already sanity-checked.
2516  */
next_designators(struct expression * old,struct symbol * ctype,struct expression * e,struct expression ** v)2517 static struct expression *next_designators(struct expression *old,
2518 			     struct symbol *ctype,
2519 			     struct expression *e, struct expression **v)
2520 {
2521 	struct expression *new = NULL;
2522 
2523 	if (!old)
2524 		return NULL;
2525 	if (old->type == EXPR_INDEX) {
2526 		struct expression *copy;
2527 		unsigned n;
2528 
2529 		copy = next_designators(old->idx_expression,
2530 					old->ctype, e, v);
2531 		if (!copy) {
2532 			n = old->idx_to + 1;
2533 			if (array_element_offset(old->ctype->bit_size, n) == ctype->bit_size) {
2534 				convert_index(old);
2535 				return NULL;
2536 			}
2537 			copy = e;
2538 			*v = new = alloc_expression(e->pos, EXPR_INDEX);
2539 		} else {
2540 			n = old->idx_to;
2541 			new = alloc_expression(e->pos, EXPR_INDEX);
2542 		}
2543 
2544 		new->idx_from = new->idx_to = n;
2545 		new->idx_expression = copy;
2546 		new->ctype = old->ctype;
2547 		convert_index(old);
2548 	} else if (old->type == EXPR_IDENTIFIER) {
2549 		struct expression *copy;
2550 		struct symbol *field;
2551 		int offset = 0;
2552 
2553 		copy = next_designators(old->ident_expression,
2554 					old->ctype, e, v);
2555 		if (!copy) {
2556 			field = old->field->next_subobject;
2557 			if (!field) {
2558 				convert_ident(old);
2559 				return NULL;
2560 			}
2561 			copy = e;
2562 			*v = new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2563 			/*
2564 			 * We can't necessarily trust "field->offset",
2565 			 * because the field might be in an anonymous
2566 			 * union, and the field offset is then the offset
2567 			 * within that union.
2568 			 *
2569 			 * The "old->offset - old->field->offset"
2570 			 * would be the offset of such an anonymous
2571 			 * union.
2572 			 */
2573 			offset = old->offset - old->field->offset;
2574 		} else {
2575 			field = old->field;
2576 			new = alloc_expression(e->pos, EXPR_IDENTIFIER);
2577 		}
2578 
2579 		new->field = field;
2580 		new->expr_ident = field->ident;
2581 		new->ident_expression = copy;
2582 		new->ctype = field;
2583 		new->offset = field->offset + offset;
2584 		convert_ident(old);
2585 	}
2586 	return new;
2587 }
2588 
2589 static int handle_initializer(struct expression **ep, int nested,
2590 		int class, struct symbol *ctype, unsigned long mods);
2591 
2592 /*
2593  * deal with traversing subobjects [6.7.8(17,18,20)]
2594  */
handle_list_initializer(struct expression * expr,int class,struct symbol * ctype,unsigned long mods)2595 static void handle_list_initializer(struct expression *expr,
2596 		int class, struct symbol *ctype, unsigned long mods)
2597 {
2598 	struct expression *e, *last = NULL, *top = NULL, *next;
2599 	int jumped = 0;	// has the last designator multiple levels?
2600 
2601 	if (expr->zero_init)
2602 		free_ptr_list(&expr->expr_list);
2603 
2604 	FOR_EACH_PTR(expr->expr_list, e) {
2605 		struct expression **v;
2606 		struct symbol *type;
2607 		int lclass;
2608 
2609 		if (e->type != EXPR_INDEX && e->type != EXPR_IDENTIFIER) {
2610 			struct symbol *struct_sym;
2611 			if (!top) {
2612 				top = e;
2613 				last = first_subobject(ctype, class, &top);
2614 			} else {
2615 				last = next_designators(last, ctype, e, &top);
2616 			}
2617 			if (!last) {
2618 				excess(e, class & TYPE_PTR ? "array" :
2619 							"struct or union");
2620 				DELETE_CURRENT_PTR(e);
2621 				continue;
2622 			}
2623 			struct_sym = ctype->type == SYM_NODE ? ctype->ctype.base_type : ctype;
2624 			if (Wdesignated_init && struct_sym->designated_init)
2625 				warning(e->pos, "%s%.*s%spositional init of field in %s %s, declared with attribute designated_init",
2626 					ctype->ident ? "in initializer for " : "",
2627 					ctype->ident ? ctype->ident->len : 0,
2628 					ctype->ident ? ctype->ident->name : "",
2629 					ctype->ident ? ": " : "",
2630 					get_type_name(struct_sym->type),
2631 					show_ident(struct_sym->ident));
2632 			if (jumped && Wpast_deep_designator) {
2633 				warning(e->pos, "advancing past deep designator");
2634 				jumped = 0;
2635 			}
2636 			REPLACE_CURRENT_PTR(e, last);
2637 		} else {
2638 			next = check_designators(e, ctype);
2639 			if (!next) {
2640 				DELETE_CURRENT_PTR(e);
2641 				continue;
2642 			}
2643 			top = next;
2644 			/* deeper than one designator? */
2645 			jumped = top != e;
2646 			convert_designators(last);
2647 			last = e;
2648 		}
2649 
2650 found:
2651 		lclass = classify_type(top->ctype, &type);
2652 		if (top->type == EXPR_INDEX)
2653 			v = &top->idx_expression;
2654 		else
2655 			v = &top->ident_expression;
2656 
2657 		mods |= ctype->ctype.modifiers & MOD_STORAGE;
2658 		if (handle_initializer(v, 1, lclass, top->ctype, mods))
2659 			continue;
2660 
2661 		if (!(lclass & TYPE_COMPOUND)) {
2662 			warning(e->pos, "bogus scalar initializer");
2663 			DELETE_CURRENT_PTR(e);
2664 			continue;
2665 		}
2666 
2667 		next = first_subobject(type, lclass, v);
2668 		if (next) {
2669 			warning(e->pos, "missing braces around initializer");
2670 			top = next;
2671 			goto found;
2672 		}
2673 
2674 		DELETE_CURRENT_PTR(e);
2675 		excess(e, lclass & TYPE_PTR ? "array" : "struct or union");
2676 
2677 	} END_FOR_EACH_PTR(e);
2678 
2679 	convert_designators(last);
2680 	expr->ctype = ctype;
2681 }
2682 
is_string_literal(struct expression ** v)2683 static int is_string_literal(struct expression **v)
2684 {
2685 	struct expression *e = *v;
2686 	while (e && e->type == EXPR_PREOP && e->op == '(')
2687 		e = e->unop;
2688 	if (!e || e->type != EXPR_STRING)
2689 		return 0;
2690 	if (e != *v && Wparen_string)
2691 		warning(e->pos,
2692 			"array initialized from parenthesized string constant");
2693 	*v = e;
2694 	return 1;
2695 }
2696 
2697 /*
2698  * We want a normal expression, possibly in one layer of braces.  Warn
2699  * if the latter happens inside a list (it's legal, but likely to be
2700  * an effect of screwup).  In case of anything not legal, we are definitely
2701  * having an effect of screwup, so just fail and let the caller warn.
2702  */
handle_scalar(struct expression * e,int nested)2703 static struct expression *handle_scalar(struct expression *e, int nested)
2704 {
2705 	struct expression *v = NULL, *p;
2706 	int count = 0;
2707 
2708 	/* normal case */
2709 	if (e->type != EXPR_INITIALIZER)
2710 		return e;
2711 
2712 	FOR_EACH_PTR(e->expr_list, p) {
2713 		if (!v)
2714 			v = p;
2715 		count++;
2716 	} END_FOR_EACH_PTR(p);
2717 	if (count != 1)
2718 		return NULL;
2719 	switch(v->type) {
2720 	case EXPR_INITIALIZER:
2721 	case EXPR_INDEX:
2722 	case EXPR_IDENTIFIER:
2723 		return NULL;
2724 	default:
2725 		break;
2726 	}
2727 	if (nested)
2728 		warning(e->pos, "braces around scalar initializer");
2729 	return v;
2730 }
2731 
2732 /*
2733  * deal with the cases that don't care about subobjects:
2734  * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2735  * character array <- string literal, possibly in braces [6.7.8(14)]
2736  * struct or union <- assignment expression of compatible type [6.7.8(13)]
2737  * compound type <- initializer list in braces [6.7.8(16)]
2738  * The last one punts to handle_list_initializer() which, in turn will call
2739  * us for individual elements of the list.
2740  *
2741  * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2742  * the lack of support of wide char stuff in general.
2743  *
2744  * One note: we need to take care not to evaluate a string literal until
2745  * we know that we *will* handle it right here.  Otherwise we would screw
2746  * the cases like struct { struct {char s[10]; ...} ...} initialized with
2747  * { "string", ...} - we need to preserve that string literal recognizable
2748  * until we dig into the inner struct.
2749  */
handle_initializer(struct expression ** ep,int nested,int class,struct symbol * ctype,unsigned long mods)2750 static int handle_initializer(struct expression **ep, int nested,
2751 		int class, struct symbol *ctype, unsigned long mods)
2752 {
2753 	struct expression *e = *ep, *p;
2754 	struct symbol *type;
2755 
2756 	if (!e)
2757 		return 0;
2758 
2759 	/* scalar */
2760 	if (!(class & TYPE_COMPOUND)) {
2761 		e = handle_scalar(e, nested);
2762 		if (!e)
2763 			return 0;
2764 		*ep = e;
2765 		if (!evaluate_expression(e))
2766 			return 1;
2767 		compatible_assignment_types(e, ctype, ep, "initializer");
2768 		/*
2769 		 * Initializers for static storage duration objects
2770 		 * shall be constant expressions or a string literal [6.7.8(4)].
2771 		 */
2772 		mods |= ctype->ctype.modifiers;
2773 		mods &= (MOD_TOPLEVEL | MOD_STATIC);
2774 		if (mods && !(e->flags & (CEF_ACE | CEF_ADDR)))
2775 			if (Wconstexpr_not_const)
2776 				warning(e->pos, "non-constant initializer for static object");
2777 
2778 		return 1;
2779 	}
2780 
2781 	/*
2782 	 * sublist; either a string, or we dig in; the latter will deal with
2783 	 * pathologies, so we don't need anything fancy here.
2784 	 */
2785 	if (e->type == EXPR_INITIALIZER) {
2786 		if (is_string_type(ctype)) {
2787 			struct expression *v = NULL;
2788 			int count = 0;
2789 
2790 			FOR_EACH_PTR(e->expr_list, p) {
2791 				if (!v)
2792 					v = p;
2793 				count++;
2794 			} END_FOR_EACH_PTR(p);
2795 			if (count == 1 && is_string_literal(&v)) {
2796 				*ep = e = v;
2797 				goto String;
2798 			}
2799 		}
2800 		handle_list_initializer(e, class, ctype, mods);
2801 		return 1;
2802 	}
2803 
2804 	/* string */
2805 	if (is_string_literal(&e)) {
2806 		/* either we are doing array of char, or we'll have to dig in */
2807 		if (is_string_type(ctype)) {
2808 			*ep = e;
2809 			goto String;
2810 		}
2811 		return 0;
2812 	}
2813 	/* struct or union can be initialized by compatible */
2814 	if (class != TYPE_COMPOUND)
2815 		return 0;
2816 	type = evaluate_expression(e);
2817 	if (!type)
2818 		return 0;
2819 	if (ctype->type == SYM_NODE)
2820 		ctype = ctype->ctype.base_type;
2821 	if (type->type == SYM_NODE)
2822 		type = type->ctype.base_type;
2823 	if (ctype == type)
2824 		return 1;
2825 	return 0;
2826 
2827 String:
2828 	p = alloc_expression(e->pos, EXPR_STRING);
2829 	*p = *e;
2830 	type = evaluate_expression(p);
2831 	if (ctype->bit_size != -1) {
2832 		struct symbol *char_type = e->wide ? wchar_ctype : &char_ctype;
2833 		unsigned int size_with_null = ctype->bit_size + char_type->bit_size;
2834 		if (size_with_null < type->bit_size)
2835 			warning(e->pos,
2836 				"too long initializer-string for array of char");
2837 		else if (Winit_cstring && size_with_null == type->bit_size) {
2838 			warning(e->pos,
2839 				"too long initializer-string for array of char(no space for nul char)");
2840 		}
2841 	}
2842 	*ep = p;
2843 	return 1;
2844 }
2845 
evaluate_initializer(struct symbol * ctype,struct expression ** ep)2846 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
2847 {
2848 	struct symbol *type;
2849 	int class = classify_type(ctype, &type);
2850 	if (!handle_initializer(ep, 0, class, ctype, 0))
2851 		expression_error(*ep, "invalid initializer");
2852 }
2853 
cast_to_bool(struct expression * expr)2854 static struct symbol *cast_to_bool(struct expression *expr)
2855 {
2856 	struct expression *old = expr->cast_expression;
2857 	struct expression *zero;
2858 	struct symbol *otype;
2859 	int oclass = classify_type(degenerate(old), &otype);
2860 	struct symbol *ctype;
2861 
2862 	if (oclass & TYPE_COMPOUND)
2863 		return NULL;
2864 
2865 	zero = alloc_const_expression(expr->pos, 0);
2866 	if (oclass & TYPE_PTR)
2867 		zero->ctype = otype;
2868 	expr->op = SPECIAL_NOTEQUAL;
2869 	ctype = usual_conversions(expr->op, old, zero,
2870 			oclass, TYPE_NUM, otype, zero->ctype);
2871 	expr->type = EXPR_COMPARE;
2872 	expr->left = cast_to(old, ctype);
2873 	expr->right = cast_to(zero, ctype);
2874 
2875 	return expr->ctype;
2876 }
2877 
cast_flags(struct expression * expr,struct expression * old)2878 static int cast_flags(struct expression *expr, struct expression *old)
2879 {
2880 	struct symbol *t;
2881 	int class;
2882 	int flags = CEF_NONE;
2883 
2884 	class = classify_type(expr->ctype, &t);
2885 	if (class & TYPE_NUM) {
2886 		flags = old->flags & ~CEF_CONST_MASK;
2887 		/*
2888 		 * Casts to numeric types never result in address
2889 		 * constants [6.6(9)].
2890 		 */
2891 		flags &= ~CEF_ADDR;
2892 
2893 		/*
2894 		 * As an extension, treat address constants cast to
2895 		 * integer type as an arithmetic constant.
2896 		 */
2897 		if (old->flags & CEF_ADDR)
2898 			flags = CEF_ACE;
2899 
2900 		/*
2901 		 * Cast to float type -> not an integer constant
2902 		 * expression [6.6(6)].
2903 		 */
2904 		if (class & TYPE_FLOAT)
2905 			flags &= ~CEF_CLR_ICE;
2906 		/*
2907 		 * Casts of float literals to integer type results in
2908 		 * a constant integer expression [6.6(6)].
2909 		 */
2910 		else if (old->flags & CEF_FLOAT)
2911 			flags = CEF_SET_ICE;
2912 	} else if (class & TYPE_PTR) {
2913 		/*
2914 		 * Casts of integer literals to pointer type yield
2915 		 * address constants [6.6(9)].
2916 		 *
2917 		 * As an extension, treat address constants cast to a
2918 		 * different pointer type as address constants again.
2919 		 *
2920 		 * As another extension, treat integer constant
2921 		 * expressions (in contrast to literals) cast to
2922 		 * pointer type as address constants.
2923 		 */
2924 		if (old->flags & (CEF_ICE | CEF_ADDR))
2925 			flags = CEF_ADDR;
2926 	}
2927 
2928 	return flags;
2929 }
2930 
2931 ///
2932 // check if a type matches one of the members of a union type
2933 // @utype: the union type
2934 // @type: to type to check
2935 // @return: to identifier of the matching type in the union.
find_member_type(struct symbol * utype,struct symbol * type)2936 static struct symbol *find_member_type(struct symbol *utype, struct symbol *type)
2937 {
2938 	struct symbol *t, *member;
2939 
2940 	if (utype->type != SYM_UNION)
2941 		return NULL;
2942 
2943 	FOR_EACH_PTR(utype->symbol_list, member) {
2944 		classify_type(member, &t);
2945 		if (type == t)
2946 			return member;
2947 	} END_FOR_EACH_PTR(member);
2948 	return NULL;
2949 }
2950 
evaluate_compound_literal(struct expression * expr,struct expression * source)2951 static struct symbol *evaluate_compound_literal(struct expression *expr, struct expression *source)
2952 {
2953 	struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2954 	struct symbol *sym = expr->cast_type;
2955 
2956 	sym->initializer = source;
2957 	evaluate_symbol(sym);
2958 
2959 	addr->ctype = &lazy_ptr_ctype;	/* Lazy eval */
2960 	addr->symbol = sym;
2961 	if (sym->ctype.modifiers & MOD_TOPLEVEL)
2962 		addr->flags |= CEF_ADDR;
2963 
2964 	expr->type = EXPR_PREOP;
2965 	expr->op = '*';
2966 	expr->deref = addr;
2967 	expr->ctype = sym;
2968 	return sym;
2969 }
2970 
evaluate_cast(struct expression * expr)2971 static struct symbol *evaluate_cast(struct expression *expr)
2972 {
2973 	struct expression *source = expr->cast_expression;
2974 	struct symbol *ctype;
2975 	struct symbol *ttype, *stype;
2976 	struct symbol *member;
2977 	int tclass, sclass;
2978 	struct ident *tas = NULL, *sas = NULL;
2979 
2980 	if (!source)
2981 		return NULL;
2982 
2983 	/*
2984 	 * Special case: a cast can be followed by an
2985 	 * initializer, in which case we need to pass
2986 	 * the type value down to that initializer rather
2987 	 * than trying to evaluate it as an expression
2988 	 * (cfr. compound literals: C99 & C11 6.5.2.5).
2989 	 *
2990 	 * A more complex case is when the initializer is
2991 	 * dereferenced as part of a post-fix expression.
2992 	 * We need to produce an expression that can be dereferenced.
2993 	 */
2994 	if (source->type == EXPR_INITIALIZER)
2995 		return evaluate_compound_literal(expr, source);
2996 
2997 	ctype = examine_symbol_type(expr->cast_type);
2998 	ctype = unqualify_type(ctype);
2999 	expr->ctype = ctype;
3000 	expr->cast_type = ctype;
3001 
3002 	evaluate_expression(source);
3003 	degenerate(source);
3004 
3005 	tclass = classify_type(ctype, &ttype);
3006 
3007 	expr->flags = cast_flags(expr, source);
3008 
3009 	/*
3010 	 * You can always throw a value away by casting to
3011 	 * "void" - that's an implicit "force". Note that
3012 	 * the same is _not_ true of "void *".
3013 	 */
3014 	if (ttype == &void_ctype)
3015 		goto out;
3016 
3017 	stype = source->ctype;
3018 	if (!stype) {
3019 		expression_error(expr, "cast from unknown type");
3020 		goto out;
3021 	}
3022 	sclass = classify_type(stype, &stype);
3023 
3024 	if (expr->type == EXPR_FORCE_CAST)
3025 		goto out;
3026 
3027 	if (tclass & (TYPE_COMPOUND | TYPE_FN)) {
3028 		/*
3029 		 * Special case: cast to union type (GCC extension)
3030 		 * The effect is similar to a compound literal except
3031 		 * that the result is a rvalue.
3032 		 */
3033 		if ((member = find_member_type(ttype, stype))) {
3034 			struct expression *item, *init;
3035 
3036 			if (Wunion_cast)
3037 				warning(expr->pos, "cast to union type");
3038 
3039 			item = alloc_expression(source->pos, EXPR_IDENTIFIER);
3040 			item->expr_ident = member->ident;
3041 			item->ident_expression = source;
3042 
3043 			init = alloc_expression(source->pos, EXPR_INITIALIZER);
3044 			add_expression(&init->expr_list, item);
3045 
3046 			// FIXME: this should be a rvalue
3047 			evaluate_compound_literal(expr, init);
3048 			return ctype;
3049 		}
3050 
3051 		warning(expr->pos, "cast to non-scalar");
3052 	}
3053 
3054 	if (sclass & TYPE_COMPOUND)
3055 		warning(expr->pos, "cast from non-scalar");
3056 
3057 	/* allowed cast unfouls */
3058 	if (sclass & TYPE_FOULED)
3059 		stype = unfoul(stype);
3060 
3061 	if (ttype != stype) {
3062 		if ((tclass & TYPE_RESTRICT) && restricted_value(source, ttype))
3063 			warning(expr->pos, "cast to %s",
3064 				show_typename(ttype));
3065 		if (sclass & TYPE_RESTRICT) {
3066 			if (ttype == &bool_ctype) {
3067 				if (sclass & TYPE_FOULED)
3068 					warning(expr->pos, "%s degrades to integer",
3069 						show_typename(stype));
3070 			} else {
3071 				warning(expr->pos, "cast from %s",
3072 					show_typename(stype));
3073 			}
3074 		}
3075 	}
3076 
3077 	if ((ttype == &ulong_ctype || ttype == uintptr_ctype) && !Wcast_from_as)
3078 		tas = &bad_address_space;
3079 	else if (tclass == TYPE_PTR) {
3080 		examine_pointer_target(ttype);
3081 		tas = ttype->ctype.as;
3082 	}
3083 
3084 	if ((stype == &ulong_ctype || stype == uintptr_ctype))
3085 		sas = &bad_address_space;
3086 	else if (sclass == TYPE_PTR) {
3087 		examine_pointer_target(stype);
3088 		sas = stype->ctype.as;
3089 	}
3090 
3091 	if (!tas && valid_as(sas))
3092 		warning(expr->pos, "cast removes address space '%s' of expression", show_as(sas));
3093 	if (valid_as(tas) && valid_as(sas) && tas != sas)
3094 		warning(expr->pos, "cast between address spaces (%s -> %s)", show_as(sas), show_as(tas));
3095 	if (valid_as(tas) && !sas &&
3096 	    !is_null_pointer_constant(source) && Wcast_to_as)
3097 		warning(expr->pos,
3098 			"cast adds address space '%s' to expression", show_as(tas));
3099 
3100 	if (!(ttype->ctype.modifiers & MOD_PTRINHERIT) && tclass == TYPE_PTR &&
3101 	    !tas && (source->flags & CEF_ICE)) {
3102 		if (ttype->ctype.base_type == &void_ctype) {
3103 			if (is_zero_constant(source)) {
3104 				/* NULL */
3105 				expr->type = EXPR_VALUE;
3106 				expr->ctype = &null_ctype;
3107 				expr->value = 0;
3108 				return expr->ctype;
3109 			}
3110 		}
3111 	}
3112 
3113 	if (ttype == &bool_ctype)
3114 		cast_to_bool(expr);
3115 
3116 	// checks pointers to restricted
3117 	while (Wbitwise_pointer && tclass == TYPE_PTR && sclass == TYPE_PTR) {
3118 		tclass = classify_type(ttype->ctype.base_type, &ttype);
3119 		sclass = classify_type(stype->ctype.base_type, &stype);
3120 		if (ttype == stype)
3121 			break;
3122 		if (!ttype || !stype)
3123 			break;
3124 		if (ttype == &void_ctype || stype == &void_ctype)
3125 			break;
3126 		if (tclass & TYPE_RESTRICT) {
3127 			warning(expr->pos, "cast to %s", show_typename(ctype));
3128 			break;
3129 		}
3130 		if (sclass & TYPE_RESTRICT) {
3131 			warning(expr->pos, "cast from %s", show_typename(source->ctype));
3132 			break;
3133 		}
3134 	}
3135 out:
3136 	return ctype;
3137 }
3138 
3139 /*
3140  * Evaluate a call expression with a symbol. This
3141  * should expand inline functions, and evaluate
3142  * builtins.
3143  */
evaluate_symbol_call(struct expression * expr)3144 static int evaluate_symbol_call(struct expression *expr)
3145 {
3146 	struct expression *fn = expr->fn;
3147 	struct symbol *ctype = fn->ctype;
3148 
3149 	if (fn->type != EXPR_PREOP)
3150 		return 0;
3151 
3152 	if (ctype->op && ctype->op->evaluate)
3153 		return ctype->op->evaluate(expr);
3154 
3155 	return 0;
3156 }
3157 
evaluate_call(struct expression * expr)3158 static struct symbol *evaluate_call(struct expression *expr)
3159 {
3160 	int args, fnargs;
3161 	struct symbol *ctype, *sym;
3162 	struct expression *fn = expr->fn;
3163 	struct expression_list *arglist = expr->args;
3164 
3165 	if (!evaluate_expression(fn))
3166 		return NULL;
3167 	sym = ctype = fn->ctype;
3168 	if (ctype->type == SYM_NODE)
3169 		ctype = ctype->ctype.base_type;
3170 	if (ctype->type == SYM_PTR)
3171 		ctype = get_base_type(ctype);
3172 
3173 	if (ctype->type != SYM_FN) {
3174 		struct expression *arg;
3175 
3176 		if (fn->ctype == &bad_ctype)
3177 			return NULL;
3178 
3179 		expression_error(expr, "not a function %s",
3180 			     show_ident(sym->ident));
3181 		/* do typechecking in arguments */
3182 		FOR_EACH_PTR (arglist, arg) {
3183 			evaluate_expression(arg);
3184 		} END_FOR_EACH_PTR(arg);
3185 		return NULL;
3186 	}
3187 
3188 	examine_fn_arguments(ctype);
3189         if (sym->type == SYM_NODE && fn->type == EXPR_PREOP &&
3190 	    sym->op && sym->op->args) {
3191 		if (!sym->op->args(expr))
3192 			return NULL;
3193 	} else {
3194 		if (!evaluate_arguments(ctype->arguments, arglist))
3195 			return NULL;
3196 		args = expression_list_size(expr->args);
3197 		fnargs = symbol_list_size(ctype->arguments);
3198 		if (args < fnargs) {
3199 			expression_error(expr,
3200 				     "not enough arguments for function %s",
3201 				     show_ident(sym->ident));
3202 			return NULL;
3203 		}
3204 		if (args > fnargs && !ctype->variadic)
3205 			expression_error(expr,
3206 				     "too many arguments for function %s",
3207 				     show_ident(sym->ident));
3208 	}
3209 	expr->ctype = ctype->ctype.base_type;
3210 	if (sym->type == SYM_NODE) {
3211 		if (evaluate_symbol_call(expr))
3212 			return expr->ctype;
3213 	}
3214 	return expr->ctype;
3215 }
3216 
evaluate_offsetof(struct expression * expr)3217 static struct symbol *evaluate_offsetof(struct expression *expr)
3218 {
3219 	struct expression *e = expr->down;
3220 	struct symbol *ctype = expr->in;
3221 	int class;
3222 
3223 	if (expr->op == '.') {
3224 		struct symbol *field;
3225 		int offset = 0;
3226 		if (!ctype) {
3227 			expression_error(expr, "expected structure or union");
3228 			return NULL;
3229 		}
3230 		examine_symbol_type(ctype);
3231 		class = classify_type(ctype, &ctype);
3232 		if (class != TYPE_COMPOUND) {
3233 			expression_error(expr, "expected structure or union");
3234 			return NULL;
3235 		}
3236 
3237 		field = find_identifier(expr->ident, ctype->symbol_list, &offset);
3238 		if (!field) {
3239 			expression_error(expr, "unknown member");
3240 			return NULL;
3241 		}
3242 		ctype = field;
3243 		expr->type = EXPR_VALUE;
3244 		expr->flags = CEF_SET_ICE;
3245 		expr->value = offset;
3246 		expr->taint = 0;
3247 		expr->ctype = size_t_ctype;
3248 	} else {
3249 		if (!ctype) {
3250 			expression_error(expr, "expected structure or union");
3251 			return NULL;
3252 		}
3253 		examine_symbol_type(ctype);
3254 		class = classify_type(ctype, &ctype);
3255 		if (class != (TYPE_COMPOUND | TYPE_PTR)) {
3256 			expression_error(expr, "expected array");
3257 			return NULL;
3258 		}
3259 		ctype = ctype->ctype.base_type;
3260 		if (!expr->index) {
3261 			expr->type = EXPR_VALUE;
3262 			expr->flags = CEF_SET_ICE;
3263 			expr->value = 0;
3264 			expr->taint = 0;
3265 			expr->ctype = size_t_ctype;
3266 		} else {
3267 			struct expression *idx = expr->index, *m;
3268 			struct symbol *i_type = evaluate_expression(idx);
3269 			unsigned old_idx_flags;
3270 			int i_class = classify_type(i_type, &i_type);
3271 
3272 			if (!is_int(i_class)) {
3273 				expression_error(expr, "non-integer index");
3274 				return NULL;
3275 			}
3276 			unrestrict(idx, i_class, &i_type);
3277 			old_idx_flags = idx->flags;
3278 			idx = cast_to(idx, size_t_ctype);
3279 			idx->flags = old_idx_flags;
3280 			m = alloc_const_expression(expr->pos,
3281 						   bits_to_bytes(ctype->bit_size));
3282 			m->ctype = size_t_ctype;
3283 			m->flags = CEF_SET_INT;
3284 			expr->type = EXPR_BINOP;
3285 			expr->left = idx;
3286 			expr->right = m;
3287 			expr->op = '*';
3288 			expr->ctype = size_t_ctype;
3289 			expr->flags = m->flags & idx->flags & ~CEF_CONST_MASK;
3290 		}
3291 	}
3292 	if (e) {
3293 		struct expression *copy = __alloc_expression(0);
3294 		*copy = *expr;
3295 		if (e->type == EXPR_OFFSETOF)
3296 			e->in = ctype;
3297 		if (!evaluate_expression(e))
3298 			return NULL;
3299 		expr->type = EXPR_BINOP;
3300 		expr->flags = e->flags & copy->flags & ~CEF_CONST_MASK;
3301 		expr->op = '+';
3302 		expr->ctype = size_t_ctype;
3303 		expr->left = copy;
3304 		expr->right = e;
3305 	}
3306 	return size_t_ctype;
3307 }
3308 
check_label_declaration(struct position pos,struct symbol * label)3309 static void check_label_declaration(struct position pos, struct symbol *label)
3310 {
3311 	switch (label->namespace) {
3312 	case NS_LABEL:
3313 		if (label->stmt)
3314 			break;
3315 		sparse_error(pos, "label '%s' was not declared", show_ident(label->ident));
3316 		/* fallthrough */
3317 	case NS_NONE:
3318 		current_fn->bogus_linear = 1;
3319 	default:
3320 		break;
3321 	}
3322 }
3323 
type_selection(struct symbol * ctrl,struct symbol * type)3324 static int type_selection(struct symbol *ctrl, struct symbol *type)
3325 {
3326 	struct ctype c = { .base_type = ctrl };
3327 	struct ctype t = { .base_type = type };
3328 
3329 	return !type_difference(&c, &t, 0, 0);
3330 }
3331 
evaluate_generic_selection(struct expression * expr)3332 static struct symbol *evaluate_generic_selection(struct expression *expr)
3333 {
3334 	struct type_expression *map;
3335 	struct expression *res;
3336 	struct symbol source;
3337 	struct symbol *ctrl;
3338 
3339 	if (!evaluate_expression(expr->control))
3340 		return NULL;
3341 	if (!(ctrl = degenerate(expr->control)))
3342 		return NULL;
3343 
3344 	source = *ctrl;
3345 	source.ctype.modifiers &= ~(MOD_QUALIFIER|MOD_ATOMIC);
3346 	for (map = expr->map; map; map = map->next) {
3347 		struct symbol *stype = map->type;
3348 		struct symbol *base;
3349 
3350 		if (!evaluate_symbol(stype))
3351 			continue;
3352 
3353 		base = stype->ctype.base_type;
3354 		if (base->type == SYM_ARRAY && base->array_size) {
3355 			get_expression_value_silent(base->array_size);
3356 			if (base->array_size->type == EXPR_VALUE)
3357 				continue;
3358 			sparse_error(stype->pos, "variable length array type in generic selection");
3359 			continue;
3360 		}
3361 		if (is_func_type(stype)) {
3362 			sparse_error(stype->pos, "function type in generic selection");
3363 			continue;
3364 		}
3365 		if (stype->bit_size <= 0 || is_void_type(stype)) {
3366 			sparse_error(stype->pos, "incomplete type in generic selection");
3367 			continue;
3368 		}
3369 		if (!type_selection(&source, stype))
3370 			continue;
3371 
3372 		res = map->expr;
3373 		goto found;
3374 	}
3375 	res = expr->def;
3376 	if (!res) {
3377 		sparse_error(expr->pos, "no generic selection for '%s'", show_typename(ctrl));
3378 		return NULL;
3379 	}
3380 
3381 found:
3382 	*expr = *res;
3383 	return evaluate_expression(expr);
3384 }
3385 
evaluate_expression(struct expression * expr)3386 struct symbol *evaluate_expression(struct expression *expr)
3387 {
3388 	if (!expr)
3389 		return NULL;
3390 	if (expr->ctype)
3391 		return expr->ctype;
3392 
3393 	switch (expr->type) {
3394 	case EXPR_VALUE:
3395 	case EXPR_FVALUE:
3396 		expression_error(expr, "value expression without a type");
3397 		return NULL;
3398 	case EXPR_STRING:
3399 		return evaluate_string(expr);
3400 	case EXPR_SYMBOL:
3401 		return evaluate_symbol_expression(expr);
3402 	case EXPR_BINOP:
3403 		evaluate_expression(expr->left);
3404 		evaluate_expression(expr->right);
3405 		if (!valid_subexpr_type(expr))
3406 			return NULL;
3407 		return evaluate_binop(expr);
3408 	case EXPR_LOGICAL:
3409 		return evaluate_logical(expr);
3410 	case EXPR_COMMA:
3411 		evaluate_expression(expr->left);
3412 		if (!evaluate_expression(expr->right))
3413 			return NULL;
3414 		return evaluate_comma(expr);
3415 	case EXPR_COMPARE:
3416 		evaluate_expression(expr->left);
3417 		evaluate_expression(expr->right);
3418 		if (!valid_subexpr_type(expr))
3419 			return NULL;
3420 		return evaluate_compare(expr);
3421 	case EXPR_ASSIGNMENT:
3422 		evaluate_expression(expr->left);
3423 		evaluate_expression(expr->right);
3424 		if (!valid_subexpr_type(expr))
3425 			return NULL;
3426 		return evaluate_assignment(expr);
3427 	case EXPR_PREOP:
3428 		if (!evaluate_expression(expr->unop))
3429 			return NULL;
3430 		return evaluate_preop(expr);
3431 	case EXPR_POSTOP:
3432 		if (!evaluate_expression(expr->unop))
3433 			return NULL;
3434 		return evaluate_postop(expr);
3435 	case EXPR_CAST:
3436 	case EXPR_FORCE_CAST:
3437 	case EXPR_IMPLIED_CAST:
3438 		return evaluate_cast(expr);
3439 	case EXPR_SIZEOF:
3440 		return evaluate_sizeof(expr);
3441 	case EXPR_PTRSIZEOF:
3442 		return evaluate_ptrsizeof(expr);
3443 	case EXPR_ALIGNOF:
3444 		return evaluate_alignof(expr);
3445 	case EXPR_DEREF:
3446 		return evaluate_member_dereference(expr);
3447 	case EXPR_CALL:
3448 		return evaluate_call(expr);
3449 	case EXPR_SELECT:
3450 	case EXPR_CONDITIONAL:
3451 		return evaluate_conditional_expression(expr);
3452 	case EXPR_STATEMENT:
3453 		expr->ctype = evaluate_statement(expr->statement);
3454 		return expr->ctype;
3455 
3456 	case EXPR_LABEL:
3457 		expr->ctype = &ptr_ctype;
3458 		check_label_declaration(expr->pos, expr->label_symbol);
3459 		return &ptr_ctype;
3460 
3461 	case EXPR_TYPE:
3462 		/* Evaluate the type of the symbol .. */
3463 		evaluate_symbol(expr->symbol);
3464 		/* .. but the type of the _expression_ is a "type" */
3465 		expr->ctype = &type_ctype;
3466 		return &type_ctype;
3467 
3468 	case EXPR_OFFSETOF:
3469 		return evaluate_offsetof(expr);
3470 
3471 	case EXPR_GENERIC:
3472 		return evaluate_generic_selection(expr);
3473 
3474 	/* These can not exist as stand-alone expressions */
3475 	case EXPR_INITIALIZER:
3476 	case EXPR_IDENTIFIER:
3477 	case EXPR_INDEX:
3478 	case EXPR_POS:
3479 		expression_error(expr, "internal front-end error: initializer in expression");
3480 		return NULL;
3481 	case EXPR_SLICE:
3482 		expression_error(expr, "internal front-end error: SLICE re-evaluated");
3483 		return NULL;
3484 	}
3485 	return NULL;
3486 }
3487 
check_duplicates(struct symbol * sym)3488 void check_duplicates(struct symbol *sym)
3489 {
3490 	int declared = 0;
3491 	struct symbol *next = sym;
3492 	int initialized = sym->initializer != NULL;
3493 
3494 	while ((next = next->same_symbol) != NULL) {
3495 		const char *typediff;
3496 		evaluate_symbol(next);
3497 		if (initialized && next->initializer) {
3498 			sparse_error(sym->pos, "symbol '%s' has multiple initializers (originally initialized at %s:%d)",
3499 				show_ident(sym->ident),
3500 				stream_name(next->pos.stream), next->pos.line);
3501 			/* Only warn once */
3502 			initialized = 0;
3503 		}
3504 		declared++;
3505 		typediff = type_difference(&sym->ctype, &next->ctype, 0, 0);
3506 		if (typediff) {
3507 			sparse_error(sym->pos, "symbol '%s' redeclared with different type (%s):",
3508 				show_ident(sym->ident), typediff);
3509 			info(sym->pos, "   %s", show_typename(sym));
3510 			info(next->pos, "note: previously declared as:");
3511 			info(next->pos, "   %s", show_typename(next));
3512 			return;
3513 		}
3514 	}
3515 	if (!declared) {
3516 		unsigned long mod = sym->ctype.modifiers;
3517 		if (mod & (MOD_STATIC | MOD_REGISTER | MOD_EXT_VISIBLE))
3518 			return;
3519 		if (!(mod & MOD_TOPLEVEL))
3520 			return;
3521 		if (!Wdecl)
3522 			return;
3523 		if (sym->ident == &main_ident)
3524 			return;
3525 		warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
3526 	}
3527 }
3528 
evaluate_symbol(struct symbol * sym)3529 static struct symbol *evaluate_symbol(struct symbol *sym)
3530 {
3531 	struct symbol *base_type;
3532 
3533 	if (!sym)
3534 		return sym;
3535 	if (sym->evaluated)
3536 		return sym;
3537 	sym->evaluated = 1;
3538 
3539 	sym = examine_symbol_type(sym);
3540 	base_type = get_base_type(sym);
3541 	if (!base_type)
3542 		return NULL;
3543 
3544 	/* Evaluate the initializers */
3545 	if (sym->initializer)
3546 		evaluate_initializer(sym, &sym->initializer);
3547 
3548 	/* And finally, evaluate the body of the symbol too */
3549 	if (base_type->type == SYM_FN) {
3550 		struct symbol *curr = current_fn;
3551 
3552 		if (sym->definition && sym->definition != sym)
3553 			return evaluate_symbol(sym->definition);
3554 
3555 		current_fn = sym;
3556 
3557 		examine_fn_arguments(base_type);
3558 		if (!base_type->stmt && base_type->inline_stmt)
3559 			uninline(sym);
3560 		if (base_type->stmt)
3561 			evaluate_statement(base_type->stmt);
3562 
3563 		current_fn = curr;
3564 	}
3565 
3566 	return base_type;
3567 }
3568 
evaluate_symbol_list(struct symbol_list * list)3569 void evaluate_symbol_list(struct symbol_list *list)
3570 {
3571 	struct symbol *sym;
3572 
3573 	FOR_EACH_PTR(list, sym) {
3574 		has_error &= ~ERROR_CURR_PHASE;
3575 		evaluate_symbol(sym);
3576 		check_duplicates(sym);
3577 	} END_FOR_EACH_PTR(sym);
3578 }
3579 
evaluate_return_expression(struct statement * stmt)3580 static struct symbol *evaluate_return_expression(struct statement *stmt)
3581 {
3582 	struct expression *expr = stmt->expression;
3583 	struct symbol *fntype, *rettype;
3584 
3585 	evaluate_expression(expr);
3586 	fntype = current_fn->ctype.base_type;
3587 	rettype = fntype->ctype.base_type;
3588 	if (!rettype || rettype == &void_ctype) {
3589 		if (expr && expr->ctype && !is_void_type(expr->ctype))
3590 			expression_error(expr, "return expression in %s function", rettype?"void":"typeless");
3591 		if (expr && Wreturn_void)
3592 			warning(stmt->pos, "returning void-valued expression");
3593 		return NULL;
3594 	}
3595 
3596 	if (!expr) {
3597 		sparse_error(stmt->pos, "return with no return value");
3598 		return NULL;
3599 	}
3600 	if (!expr->ctype)
3601 		return NULL;
3602 	compatible_assignment_types(expr, rettype, &stmt->expression, "return expression");
3603 	return NULL;
3604 }
3605 
evaluate_if_statement(struct statement * stmt)3606 static void evaluate_if_statement(struct statement *stmt)
3607 {
3608 	if (!stmt->if_conditional)
3609 		return;
3610 
3611 	evaluate_conditional(stmt->if_conditional, 0);
3612 	evaluate_statement(stmt->if_true);
3613 	evaluate_statement(stmt->if_false);
3614 }
3615 
evaluate_iterator(struct statement * stmt)3616 static void evaluate_iterator(struct statement *stmt)
3617 {
3618 	evaluate_symbol_list(stmt->iterator_syms);
3619 	evaluate_conditional(stmt->iterator_pre_condition, 1);
3620 	evaluate_conditional(stmt->iterator_post_condition,1);
3621 	evaluate_statement(stmt->iterator_pre_statement);
3622 	evaluate_statement(stmt->iterator_statement);
3623 	evaluate_statement(stmt->iterator_post_statement);
3624 }
3625 
3626 
parse_asm_constraint(struct asm_operand * op)3627 static void parse_asm_constraint(struct asm_operand *op)
3628 {
3629 	struct expression *constraint = op->constraint;
3630 	const char *str = constraint->string->data;
3631 	int c;
3632 
3633 	switch (str[0]) {
3634 	case '\0':
3635 		sparse_error(constraint->pos, "invalid ASM constraint (\"\")");
3636 		break;
3637 	case '+':
3638 		op->is_modify = true;
3639 		/* fall-through */
3640 	case '=':
3641 		op->is_assign = true;
3642 		str++;
3643 		break;
3644 	}
3645 
3646 	while ((c = *str++)) {
3647 		switch (c) {
3648 		case '=':
3649 		case '+':
3650 			sparse_error(constraint->pos, "invalid ASM constraint '%c'", c);
3651 			break;
3652 
3653 		case '&':
3654 			op->is_earlyclobber = true;
3655 			break;
3656 		case '%':
3657 			op->is_commutative = true;
3658 			break;
3659 		case 'r':
3660 			op->is_register = true;
3661 			break;
3662 
3663 		case 'm':
3664 		case 'o':
3665 		case 'V':
3666 		case 'Q':
3667 			op->is_memory = true;
3668 			break;
3669 
3670 		case '<':
3671 		case '>':
3672 			// FIXME: ignored for now
3673 			break;
3674 
3675 		case ',':
3676 			// FIXME: multiple alternative constraints
3677 			break;
3678 
3679 		case '0' ... '9':
3680 			// FIXME: numeric  matching constraint?
3681 			break;
3682 		case '[':
3683 			// FIXME: symbolic matching constraint
3684 			return;
3685 
3686 		default:
3687 			if (arch_target->asm_constraint)
3688 				str = arch_target->asm_constraint(op, c, str);
3689 
3690 			// FIXME: multi-letter constraints
3691 			break;
3692 		}
3693 	}
3694 
3695 	// FIXME: how to deal with multi-constraint?
3696 	if (op->is_register)
3697 		op->is_memory = 0;
3698 }
3699 
verify_output_constraint(struct asm_operand * op)3700 static void verify_output_constraint(struct asm_operand *op)
3701 {
3702 	struct expression *expr = op->constraint;
3703 	const char *constraint = expr->string->data;
3704 
3705 	if (!op->is_assign)
3706 		expression_error(expr, "output constraint is not an assignment constraint (\"%s\")", constraint);
3707 }
3708 
verify_input_constraint(struct asm_operand * op)3709 static void verify_input_constraint(struct asm_operand *op)
3710 {
3711 	struct expression *expr = op->constraint;
3712 	const char *constraint = expr->string->data;
3713 
3714 	if (op->is_assign)
3715 		expression_error(expr, "input constraint with assignment (\"%s\")", constraint);
3716 }
3717 
evaluate_asm_memop(struct asm_operand * op)3718 static void evaluate_asm_memop(struct asm_operand *op)
3719 {
3720 	if (op->is_memory) {
3721 		struct expression *expr = op->expr;
3722 		struct expression *addr;
3723 
3724 		// implicit addressof
3725 		addr = alloc_expression(expr->pos, EXPR_PREOP);
3726 		addr->op = '&';
3727 		addr->unop = expr;
3728 
3729 		evaluate_addressof(addr);
3730 		op->expr = addr;
3731 	} else {
3732 		evaluate_expression(op->expr);
3733 		degenerate(op->expr);
3734 	}
3735 }
3736 
evaluate_asm_statement(struct statement * stmt)3737 static void evaluate_asm_statement(struct statement *stmt)
3738 {
3739 	struct expression *expr;
3740 	struct asm_operand *op;
3741 	struct symbol *sym;
3742 
3743 	if (!stmt->asm_string)
3744 		return;
3745 
3746 	FOR_EACH_PTR(stmt->asm_outputs, op) {
3747 		/* Identifier */
3748 
3749 		/* Constraint */
3750 		if (op->constraint) {
3751 			parse_asm_constraint(op);
3752 			verify_output_constraint(op);
3753 		}
3754 
3755 		/* Expression */
3756 		expr = op->expr;
3757 		if (!evaluate_expression(expr))
3758 			return;
3759 		if (!lvalue_expression(expr))
3760 			warning(expr->pos, "asm output is not an lvalue");
3761 		evaluate_assign_to(expr, expr->ctype);
3762 		evaluate_asm_memop(op);
3763 	} END_FOR_EACH_PTR(op);
3764 
3765 	FOR_EACH_PTR(stmt->asm_inputs, op) {
3766 		/* Identifier */
3767 
3768 		/* Constraint */
3769 		if (op->constraint) {
3770 			parse_asm_constraint(op);
3771 			verify_input_constraint(op);
3772 		}
3773 
3774 		/* Expression */
3775 		if (!evaluate_expression(op->expr))
3776 			return;
3777 		evaluate_asm_memop(op);
3778 	} END_FOR_EACH_PTR(op);
3779 
3780 	FOR_EACH_PTR(stmt->asm_clobbers, expr) {
3781 		if (!expr) {
3782 			sparse_error(stmt->pos, "bad asm clobbers");
3783 			return;
3784 		}
3785 		if (expr->type == EXPR_STRING)
3786 			continue;
3787 		expression_error(expr, "asm clobber is not a string");
3788 	} END_FOR_EACH_PTR(expr);
3789 
3790 	FOR_EACH_PTR(stmt->asm_labels, sym) {
3791 		if (!sym || sym->type != SYM_LABEL) {
3792 			sparse_error(stmt->pos, "bad asm label");
3793 			return;
3794 		}
3795 	} END_FOR_EACH_PTR(sym);
3796 }
3797 
evaluate_case_statement(struct statement * stmt)3798 static void evaluate_case_statement(struct statement *stmt)
3799 {
3800 	evaluate_expression(stmt->case_expression);
3801 	evaluate_expression(stmt->case_to);
3802 	evaluate_statement(stmt->case_statement);
3803 }
3804 
check_case_type(struct expression * switch_expr,struct expression * case_expr,struct expression ** enumcase)3805 static void check_case_type(struct expression *switch_expr,
3806 			    struct expression *case_expr,
3807 			    struct expression **enumcase)
3808 {
3809 	struct symbol *switch_type, *case_type;
3810 	int sclass, cclass;
3811 
3812 	if (!case_expr)
3813 		return;
3814 
3815 	switch_type = switch_expr->ctype;
3816 	case_type = evaluate_expression(case_expr);
3817 
3818 	if (!switch_type || !case_type)
3819 		goto Bad;
3820 	if (enumcase) {
3821 		if (*enumcase)
3822 			warn_for_different_enum_types(case_expr->pos, case_type, (*enumcase)->ctype);
3823 		else if (is_enum_type(case_type))
3824 			*enumcase = case_expr;
3825 	}
3826 
3827 	sclass = classify_type(switch_type, &switch_type);
3828 	cclass = classify_type(case_type, &case_type);
3829 
3830 	/* both should be arithmetic */
3831 	if (!(sclass & cclass & TYPE_NUM))
3832 		goto Bad;
3833 
3834 	/* neither should be floating */
3835 	if ((sclass | cclass) & TYPE_FLOAT)
3836 		goto Bad;
3837 
3838 	/* if neither is restricted, we are OK */
3839 	if (!((sclass | cclass) & TYPE_RESTRICT))
3840 		return;
3841 
3842 	if (!restricted_binop_type(SPECIAL_EQUAL, case_expr, switch_expr,
3843 				   cclass, sclass, case_type, switch_type)) {
3844 		unrestrict(case_expr, cclass, &case_type);
3845 		unrestrict(switch_expr, sclass, &switch_type);
3846 	}
3847 	return;
3848 
3849 Bad:
3850 	expression_error(case_expr, "incompatible types for 'case' statement");
3851 }
3852 
evaluate_switch_statement(struct statement * stmt)3853 static void evaluate_switch_statement(struct statement *stmt)
3854 {
3855 	struct symbol *sym;
3856 	struct expression *enumcase = NULL;
3857 	struct expression **enumcase_holder = &enumcase;
3858 	struct expression *sel = stmt->switch_expression;
3859 
3860 	evaluate_expression(sel);
3861 	evaluate_statement(stmt->switch_statement);
3862 	if (!sel)
3863 		return;
3864 	if (sel->ctype && is_enum_type(sel->ctype))
3865 		enumcase_holder = NULL; /* Only check cases against switch */
3866 
3867 	FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
3868 		struct statement *case_stmt = sym->stmt;
3869 		check_case_type(sel, case_stmt->case_expression, enumcase_holder);
3870 		check_case_type(sel, case_stmt->case_to, enumcase_holder);
3871 	} END_FOR_EACH_PTR(sym);
3872 }
3873 
evaluate_goto_statement(struct statement * stmt)3874 static void evaluate_goto_statement(struct statement *stmt)
3875 {
3876 	struct symbol *label = stmt->goto_label;
3877 
3878 	if (!label) {
3879 		// no label associated, may be a computed goto
3880 		evaluate_expression(stmt->goto_expression);
3881 		return;
3882 	}
3883 
3884 	check_label_declaration(stmt->pos, label);
3885 }
3886 
evaluate_statement(struct statement * stmt)3887 struct symbol *evaluate_statement(struct statement *stmt)
3888 {
3889 	if (!stmt)
3890 		return NULL;
3891 
3892 	switch (stmt->type) {
3893 	case STMT_DECLARATION: {
3894 		struct symbol *s;
3895 		FOR_EACH_PTR(stmt->declaration, s) {
3896 			evaluate_symbol(s);
3897 		} END_FOR_EACH_PTR(s);
3898 		return NULL;
3899 	}
3900 
3901 	case STMT_RETURN:
3902 		return evaluate_return_expression(stmt);
3903 
3904 	case STMT_EXPRESSION:
3905 		if (!evaluate_expression(stmt->expression))
3906 			return NULL;
3907 		if (stmt->expression->ctype == &null_ctype)
3908 			stmt->expression = cast_to(stmt->expression, &ptr_ctype);
3909 		return unqualify_type(degenerate(stmt->expression));
3910 
3911 	case STMT_COMPOUND: {
3912 		struct statement *s;
3913 		struct symbol *type = NULL;
3914 
3915 		/* Evaluate the return symbol in the compound statement */
3916 		evaluate_symbol(stmt->ret);
3917 
3918 		/*
3919 		 * Then, evaluate each statement, making the type of the
3920 		 * compound statement be the type of the last statement
3921 		 */
3922 		type = evaluate_statement(stmt->args);
3923 		FOR_EACH_PTR(stmt->stmts, s) {
3924 			type = evaluate_statement(s);
3925 		} END_FOR_EACH_PTR(s);
3926 		if (!type)
3927 			type = &void_ctype;
3928 		return type;
3929 	}
3930 	case STMT_IF:
3931 		evaluate_if_statement(stmt);
3932 		return NULL;
3933 	case STMT_ITERATOR:
3934 		evaluate_iterator(stmt);
3935 		return NULL;
3936 	case STMT_SWITCH:
3937 		evaluate_switch_statement(stmt);
3938 		return NULL;
3939 	case STMT_CASE:
3940 		evaluate_case_statement(stmt);
3941 		return NULL;
3942 	case STMT_LABEL:
3943 		return evaluate_statement(stmt->label_statement);
3944 	case STMT_GOTO:
3945 		evaluate_goto_statement(stmt);
3946 		return NULL;
3947 	case STMT_NONE:
3948 		break;
3949 	case STMT_ASM:
3950 		evaluate_asm_statement(stmt);
3951 		return NULL;
3952 	case STMT_CONTEXT:
3953 		evaluate_expression(stmt->expression);
3954 		return NULL;
3955 	case STMT_RANGE:
3956 		evaluate_expression(stmt->range_expression);
3957 		evaluate_expression(stmt->range_low);
3958 		evaluate_expression(stmt->range_high);
3959 		return NULL;
3960 	}
3961 	return NULL;
3962 }
3963