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