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