check.c (119756B)
1 #include <assert.h> 2 #include <errno.h> 3 #include <stdarg.h> 4 #include <stdint.h> 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <string.h> 8 #include "ast.h" 9 #include "check.h" 10 #include "eval.h" 11 #include "expr.h" 12 #include "mod.h" 13 #include "scope.h" 14 #include "type_store.h" 15 #include "typedef.h" 16 #include "types.h" 17 #include "util.h" 18 19 void 20 mkident(struct context *ctx, struct identifier *out, const struct identifier *in) 21 { 22 identifier_dup(out, in); 23 if (ctx->ns) { 24 out->ns = xcalloc(1, sizeof(struct identifier)); 25 identifier_dup(out->ns, ctx->ns); 26 } 27 } 28 29 static char * 30 gen_typename(const struct type *type) 31 { 32 size_t sz = 0; 33 char *ptr = NULL; 34 FILE *f = open_memstream(&ptr, &sz); 35 if (f == NULL) { 36 fprintf(stderr, "Unable to open memstream: %s\n", 37 strerror(errno)); 38 exit(EXIT_FAILURE); 39 } 40 emit_type(type, f); 41 fclose(f); 42 return ptr; 43 } 44 45 static void 46 handle_errors(struct errors *errors) 47 { 48 struct errors *error = errors; 49 while (error) { 50 fprintf(stderr, "Error %s:%d:%d: %s\n", sources[error->loc.file], 51 error->loc.lineno, error->loc.colno, error->msg); 52 struct errors *next = error->next; 53 free(error); 54 error = next; 55 } 56 if (errors) { 57 exit(EXIT_FAILURE); 58 } 59 } 60 61 static void 62 verror(struct context *ctx, const struct location loc, struct expression *expr, 63 char *fmt, va_list ap) 64 { 65 if (expr) { 66 expr->type = EXPR_CONSTANT; 67 // TODO: We should have a separate type for errors, to avoid 68 // spurious errors 69 expr->result = &builtin_type_void; 70 expr->terminates = false; 71 expr->loc = loc; 72 } 73 74 va_list copy; 75 va_copy(copy, ap); 76 size_t sz = vsnprintf(NULL, 0, fmt, copy); 77 va_end(copy); 78 79 char *msg = xcalloc(1, sz + 1); 80 vsnprintf(msg, sz + 1, fmt, ap); 81 82 struct errors *next = *ctx->next = xcalloc(1, sizeof(struct errors)); 83 next->loc = loc; 84 next->msg = msg; 85 ctx->next = &next->next; 86 } 87 88 static void 89 error(struct context *ctx, const struct location loc, struct expression *expr, 90 char *fmt, ...) 91 { 92 va_list ap; 93 va_start(ap, fmt); 94 verror(ctx, loc, expr, fmt, ap); 95 va_end(ap); 96 } 97 98 static void 99 expect(struct context *ctx, const struct location *loc, bool constraint, 100 char *fmt, ...) 101 { 102 if (!constraint) { 103 va_list ap; 104 va_start(ap, fmt); 105 verror(ctx, *loc, NULL, fmt, ap); 106 va_end(ap); 107 108 handle_errors(ctx->errors); 109 } 110 } 111 112 static struct expression * 113 lower_implicit_cast(const struct type *to, struct expression *expr) 114 { 115 if (to == expr->result || expr->terminates) { 116 return expr; 117 } 118 119 if (type_dealias(to)->storage == STORAGE_TAGGED) { 120 const struct type *interim = 121 tagged_select_subtype(to, expr->result); 122 if (interim) { 123 expr = lower_implicit_cast(interim, expr); 124 } 125 } 126 127 struct expression *cast = xcalloc(1, sizeof(struct expression)); 128 cast->type = EXPR_CAST; 129 cast->result = cast->cast.secondary = to; 130 cast->terminates = false; 131 cast->cast.kind = C_CAST; 132 cast->cast.value = expr; 133 cast->cast.lowered = true; 134 return cast; 135 } 136 137 static void 138 check_expr_access(struct context *ctx, 139 const struct ast_expression *aexpr, 140 struct expression *expr, 141 const struct type *hint) 142 { 143 expr->type = EXPR_ACCESS; 144 expr->access.type = aexpr->access.type; 145 146 const struct scope_object *obj = NULL; 147 switch (expr->access.type) { 148 case ACCESS_IDENTIFIER: 149 obj = scope_lookup(ctx->scope, &aexpr->access.ident); 150 if (!obj) { 151 char buf[1024]; 152 identifier_unparse_static(&aexpr->access.ident, 153 buf, sizeof(buf)); 154 error(ctx, aexpr->loc, expr, 155 "Unknown object '%s'", buf); 156 return; 157 } 158 if (obj->otype == O_SCAN) { 159 // complete the declaration first 160 obj = wrap_resolver(ctx, obj, resolve_decl); 161 } 162 163 switch (obj->otype) { 164 case O_CONST: 165 // Lower constants 166 *expr = *obj->value; 167 break; 168 case O_BIND: 169 case O_DECL: 170 expr->result = obj->type; 171 expr->access.object = obj; 172 break; 173 case O_TYPE: 174 if (type_dealias(obj->type)->storage != STORAGE_VOID) { 175 error(ctx, aexpr->loc, expr, 176 "Cannot use non-void type alias '%s' as constant", 177 identifier_unparse(&obj->type->alias.ident)); 178 return; 179 } 180 expr->type = EXPR_CONSTANT; 181 expr->result = obj->type; 182 break; 183 case O_SCAN: 184 assert(0); // handled above 185 } 186 break; 187 case ACCESS_INDEX: 188 expr->access.array = xcalloc(1, sizeof(struct expression)); 189 expr->access.index = xcalloc(1, sizeof(struct expression)); 190 check_expression(ctx, aexpr->access.array, expr->access.array, NULL); 191 check_expression(ctx, aexpr->access.index, expr->access.index, &builtin_type_size); 192 const struct type *atype = 193 type_dereference(expr->access.array->result); 194 if (!atype) { 195 error(ctx, aexpr->access.array->loc, expr, 196 "Cannot dereference nullable pointer for indexing"); 197 return; 198 } 199 atype = type_dealias(atype); 200 const struct type *itype = 201 type_dealias(expr->access.index->result); 202 if (atype->storage != STORAGE_ARRAY 203 && atype->storage != STORAGE_SLICE) { 204 error(ctx, aexpr->access.array->loc, expr, 205 "Can only index into array or slice object, but got %s", 206 type_storage_unparse(atype->storage)); 207 return; 208 } 209 if (!type_is_integer(itype)) { 210 error(ctx, aexpr->access.index->loc, expr, 211 "Cannot use non-integer %s type as slice/array index", 212 type_storage_unparse(itype->storage)); 213 return; 214 } 215 expr->access.index = lower_implicit_cast( 216 &builtin_type_size, expr->access.index); 217 expr->result = type_store_lookup_with_flags(ctx->store, 218 atype->array.members, atype->flags | atype->array.members->flags); 219 break; 220 case ACCESS_FIELD: 221 expr->access._struct = xcalloc(1, sizeof(struct expression)); 222 check_expression(ctx, aexpr->access._struct, expr->access._struct, NULL); 223 const struct type *stype = 224 type_dereference(expr->access._struct->result); 225 if (!stype) { 226 error(ctx, aexpr->access._struct->loc, expr, 227 "Cannot dereference nullable pointer for field selection"); 228 return; 229 } 230 stype = type_dealias(stype); 231 if (stype->storage != STORAGE_STRUCT 232 && stype->storage != STORAGE_UNION) { 233 error(ctx, aexpr->access._struct->loc, expr, 234 "Cannot select field from non-struct, non-union object"); 235 return; 236 } 237 expr->access.field = type_get_field(stype, aexpr->access.field); 238 if (!expr->access.field) { 239 error(ctx, aexpr->access._struct->loc, expr, 240 "No such struct field '%s'", aexpr->access.field); 241 return; 242 } 243 expr->result = expr->access.field->type; 244 break; 245 case ACCESS_TUPLE: 246 expr->access.tuple = xcalloc(1, sizeof(struct expression)); 247 struct expression *value = xcalloc(1, sizeof(struct expression)); 248 check_expression(ctx, aexpr->access.tuple, expr->access.tuple, NULL); 249 check_expression(ctx, aexpr->access.value, value, NULL); 250 assert(value->type == EXPR_CONSTANT); 251 252 const struct type *ttype = 253 type_dereference(expr->access.tuple->result); 254 if (!ttype) { 255 error(ctx, aexpr->access.tuple->loc, expr, 256 "Cannot dereference nullable pointer for value selection"); 257 return; 258 } 259 ttype = type_dealias(ttype); 260 if (ttype->storage != STORAGE_TUPLE) { 261 error(ctx, aexpr->access.tuple->loc, expr, 262 "Cannot select value from non-tuple object"); 263 return; 264 } 265 if (!type_is_integer(value->result)) { 266 error(ctx, aexpr->access.tuple->loc, expr, 267 "Cannot use non-integer constant to select tuple value"); 268 return; 269 } 270 271 expr->access.tvalue = type_get_value(ttype, 272 aexpr->access.value->constant.uval); 273 if (!expr->access.tvalue) { 274 error(ctx, aexpr->access.tuple->loc, expr, 275 "No such tuple value '%zu'", 276 aexpr->access.value->constant.uval); 277 return; 278 } 279 expr->access.tindex = aexpr->access.value->constant.uval; 280 281 expr->result = expr->access.tvalue->type; 282 break; 283 } 284 } 285 286 static void 287 check_expr_alloc_init(struct context *ctx, 288 const struct ast_expression *aexpr, 289 struct expression *expr, 290 const struct type *hint) 291 { 292 // alloc(initializer) case 293 int ptrflags = 0; 294 const struct type *inithint = NULL; 295 if (hint) { 296 const struct type *htype = type_dealias(hint); 297 switch (htype->storage) { 298 case STORAGE_POINTER: 299 inithint = htype->pointer.referent; 300 // TODO: Describe the use of pointer flags in the spec 301 ptrflags = htype->pointer.flags; 302 break; 303 case STORAGE_SLICE: 304 inithint = hint; 305 break; 306 case STORAGE_TAGGED: 307 // TODO 308 break; 309 default: 310 // The user's code is wrong here, but we'll let it fail 311 // later. 312 break; 313 } 314 } 315 316 check_expression(ctx, aexpr->alloc.init, expr->alloc.init, inithint); 317 318 const struct type *objtype = expr->alloc.init->result; 319 if (type_dealias(objtype)->storage == STORAGE_ARRAY 320 && type_dealias(objtype)->array.expandable) { 321 const struct type *atype = type_dealias(objtype); 322 if (!hint) { 323 error(ctx, aexpr->loc, expr, 324 "Cannot infer expandable array length without type hint"); 325 return; 326 } 327 const struct type *htype = type_dereference(hint); 328 if (htype->storage != STORAGE_ARRAY) { 329 error(ctx, aexpr->loc, expr, 330 "Cannot assign expandable array from non-array type"); 331 return; 332 } 333 assert(htype->array.members == atype->array.members); 334 objtype = hint; 335 } 336 expr->result = type_store_lookup_pointer(ctx->store, objtype, ptrflags); 337 if (expr->result->size == 0 || expr->result->size == SIZE_UNDEFINED) { 338 error(ctx, aexpr->loc, expr, 339 "Cannot allocate object of zero or undefined size"); 340 } 341 } 342 343 static void 344 check_expr_alloc_slice(struct context *ctx, 345 const struct ast_expression *aexpr, 346 struct expression *expr, 347 const struct type *hint) 348 { 349 // alloc(init, capacity) case 350 check_expression(ctx, aexpr->alloc.init, expr->alloc.init, hint); 351 352 const struct type *objtype = expr->alloc.init->result; 353 if (type_dealias(objtype)->storage != STORAGE_ARRAY 354 && type_dealias(objtype)->storage != STORAGE_SLICE) { 355 error(ctx, aexpr->alloc.init->loc, expr, 356 "Slice initializer must be of slice or array type, not %s", 357 type_storage_unparse(type_dealias(objtype)->storage)); 358 return; 359 } 360 361 const struct type *caphint = &builtin_type_size; 362 expr->alloc.cap = xcalloc(1, sizeof(struct expression)); 363 check_expression(ctx, aexpr->alloc.cap, expr->alloc.cap, caphint); 364 365 const struct type *captype = expr->alloc.cap->result; 366 if (!type_is_assignable(&builtin_type_size, captype)) { 367 error(ctx, aexpr->alloc.cap->loc, expr, 368 "Slice capacity must be assignable to size"); 369 return; 370 } 371 expr->alloc.cap = lower_implicit_cast(&builtin_type_size, expr->alloc.cap); 372 373 struct expression cap = {0}; 374 if (expr->alloc.init->type == EXPR_CONSTANT 375 && eval_expr(ctx, expr->alloc.cap, &cap) == EVAL_OK) { 376 uintmax_t len = 0; 377 for (struct array_constant *c = expr->alloc.init->constant.array; 378 c != NULL; c = c->next) { 379 len++; 380 } 381 if (cap.constant.uval < len) { 382 error(ctx, aexpr->alloc.cap->loc, expr, 383 "Slice capacity cannot be smaller than length of initializer"); 384 return; 385 } 386 } 387 388 const struct type *membtype = type_dealias(objtype)->array.members; 389 expr->result = type_store_lookup_slice(ctx->store, membtype); 390 391 if (objtype->storage == STORAGE_ARRAY 392 && objtype->array.expandable) { 393 expr->alloc.kind = ALLOC_WITH_LEN; 394 } 395 } 396 397 static void 398 check_expr_alloc_copy(struct context *ctx, 399 const struct ast_expression *aexpr, 400 struct expression *expr, 401 const struct type *hint) 402 { 403 // alloc(init...) case 404 check_expression(ctx, aexpr->alloc.init, expr->alloc.init, hint); 405 406 const struct type *result = type_dealias(expr->alloc.init->result); 407 if (result->storage != STORAGE_ARRAY 408 && result->storage != STORAGE_SLICE) { 409 error(ctx, aexpr->alloc.init->loc, expr, 410 "Slice initializer must be of slice or array type, not %s", 411 type_storage_unparse(result->storage)); 412 return; 413 } 414 if (hint) { 415 const struct type *htype = type_dealias(hint); 416 if (htype->storage != STORAGE_SLICE 417 && htype->storage != STORAGE_TAGGED) { 418 error(ctx, aexpr->alloc.init->loc, expr, 419 "Hint must be a slice type, not %s", 420 type_storage_unparse(htype->storage)); 421 return; 422 } 423 } 424 425 check_expression(ctx, aexpr->alloc.init, expr->alloc.init, hint); 426 result = type_dealias(expr->alloc.init->result); 427 expr->result = type_store_lookup_slice(ctx->store, result->array.members); 428 } 429 430 static void 431 check_expr_alloc(struct context *ctx, 432 const struct ast_expression *aexpr, 433 struct expression *expr, 434 const struct type *hint) 435 { 436 assert(aexpr->type == EXPR_ALLOC); 437 expr->type = EXPR_ALLOC; 438 expr->alloc.init = xcalloc(1, sizeof(struct expression)); 439 expr->alloc.kind = aexpr->alloc.kind; 440 switch (aexpr->alloc.kind) { 441 case ALLOC_OBJECT: 442 check_expr_alloc_init(ctx, aexpr, expr, hint); 443 break; 444 case ALLOC_WITH_CAP: 445 check_expr_alloc_slice(ctx, aexpr, expr, hint); 446 break; 447 case ALLOC_COPY: 448 check_expr_alloc_copy(ctx, aexpr, expr, hint); 449 break; 450 case ALLOC_WITH_LEN: 451 abort(); // Not determined by parse 452 } 453 } 454 455 static void 456 check_expr_append_insert(struct context *ctx, 457 const struct ast_expression *aexpr, 458 struct expression *expr, 459 const struct type *hint) 460 { 461 assert(aexpr->type == EXPR_APPEND || aexpr->type == EXPR_INSERT); 462 expr->loc = aexpr->loc; 463 expr->type = aexpr->type; 464 expr->result = &builtin_type_void; 465 expr->append.is_static = aexpr->append.is_static; 466 expr->append.is_multi = aexpr->append.is_multi; 467 expr->append.object = xcalloc(sizeof(struct expression), 1); 468 check_expression(ctx, aexpr->append.object, expr->append.object, NULL); 469 if (expr->append.object->type != EXPR_ACCESS) { 470 error(ctx, aexpr->append.object->loc, expr, 471 "Expression must operate on an object"); 472 return; 473 }; 474 475 const struct type *sltype; 476 const struct type *sltypename; 477 const char *exprtype_name; 478 switch (expr->type) { 479 case EXPR_APPEND: 480 sltypename = expr->append.object->result; 481 exprtype_name = "append"; 482 break; 483 case EXPR_INSERT: 484 assert(expr->append.object->access.type == ACCESS_INDEX); 485 sltypename = expr->append.object->access.array->result; 486 exprtype_name = "insert"; 487 break; 488 default: 489 abort(); // Invariant 490 } 491 sltype = type_dealias(type_dereference(sltypename)); 492 493 if (sltype->storage != STORAGE_SLICE) { 494 char *typename = gen_typename(sltypename); 495 error(ctx, aexpr->append.object->loc, expr, 496 "%s expression must operate on a slice, but got %s", 497 exprtype_name, typename); 498 free(typename); 499 return; 500 } 501 if (sltype->flags & TYPE_CONST) { 502 error(ctx, aexpr->append.object->loc, expr, 503 "expression must operate on a mutable slice"); 504 return; 505 } 506 507 expr->append.value = xcalloc(sizeof(struct expression), 1); 508 509 if (!expr->append.is_multi && !aexpr->append.length) { 510 check_expression(ctx, aexpr->append.value, expr->append.value, 511 sltype->array.members); 512 if (!type_is_assignable(sltype->array.members, 513 expr->append.value->result)) { 514 error(ctx, aexpr->append.value->loc, expr, 515 "Value type must be assignable to object member type"); 516 return; 517 } 518 expr->append.value = lower_implicit_cast( 519 sltype->array.members, expr->append.value); 520 return; 521 } 522 523 check_expression(ctx, aexpr->append.value, expr->append.value, sltype); 524 const struct type *valtype = type_dereference(expr->append.value->result); 525 valtype = type_dealias(valtype); 526 if (aexpr->append.length) { 527 if (valtype->storage != STORAGE_ARRAY 528 || !valtype->array.expandable) { 529 error(ctx, aexpr->append.value->loc, expr, 530 "Value must be an expandable array in append with length"); 531 return; 532 } 533 struct expression *len = xcalloc(sizeof(struct expression), 1); 534 check_expression(ctx, aexpr->append.length, len, &builtin_type_size); 535 if (!type_is_assignable(&builtin_type_size, len->result)) { 536 error(ctx, aexpr->append.length->loc, expr, 537 "Length parameter must be assignable to size"); 538 return; 539 } 540 len = lower_implicit_cast(&builtin_type_size, len); 541 expr->append.length = len; 542 } else { 543 if (valtype->storage != STORAGE_SLICE 544 && valtype->storage != STORAGE_ARRAY) { 545 error(ctx, aexpr->append.value->loc, expr, 546 "Value must be an array or a slice in multi-valued %s", 547 exprtype_name); 548 return; 549 } 550 } 551 if (sltype->array.members != valtype->array.members) { 552 error(ctx, aexpr->loc, expr, 553 "Value member type must match object member type"); 554 return; 555 } 556 } 557 558 static void 559 check_expr_assert(struct context *ctx, 560 const struct ast_expression *aexpr, 561 struct expression *expr, 562 const struct type *hint) 563 { 564 expr->type = EXPR_ASSERT; 565 expr->result = &builtin_type_void; 566 expr->assert.is_static = aexpr->assert.is_static; 567 568 if (aexpr->assert.cond != NULL) { 569 expr->assert.cond = xcalloc(1, sizeof(struct expression)); 570 check_expression(ctx, aexpr->assert.cond, expr->assert.cond, &builtin_type_bool); 571 if (type_dealias(expr->assert.cond->result)->storage != STORAGE_BOOL) { 572 error(ctx, aexpr->assert.cond->loc, expr, 573 "Assertion condition must be boolean"); 574 return; 575 } 576 } else { 577 expr->terminates = true; 578 } 579 580 expr->assert.message = xcalloc(1, sizeof(struct expression)); 581 if (aexpr->assert.message != NULL) { 582 check_expression(ctx, aexpr->assert.message, expr->assert.message, &builtin_type_str); 583 if (expr->assert.message->result->storage != STORAGE_STRING) { 584 error(ctx, aexpr->assert.message->loc, expr, 585 "Assertion message must be string"); 586 return; 587 } 588 589 assert(expr->assert.message->type == EXPR_CONSTANT); 590 size_t n = snprintf(NULL, 0, "%s:%d:%d: ", 591 sources[aexpr->loc.file], 592 aexpr->loc.lineno, aexpr->loc.colno); 593 size_t s_len = expr->assert.message->constant.string.len; 594 char *s = xcalloc(1, n + s_len + 1); 595 snprintf(s, n + 1, "%s:%d:%d: ", sources[aexpr->loc.file], 596 aexpr->loc.lineno, aexpr->loc.colno); 597 memcpy(s+n, expr->assert.message->constant.string.value, s_len); 598 s[n + s_len] = '\0'; 599 600 expr->assert.message->constant.string.value = s; 601 expr->assert.message->constant.string.len = n + s_len; 602 } else { 603 int n = snprintf(NULL, 0, "Assertion failed: %s:%d:%d", 604 sources[aexpr->loc.file], 605 aexpr->loc.lineno, aexpr->loc.colno); 606 char *s = xcalloc(1, n + 1); 607 snprintf(s, n, "Assertion failed: %s:%d:%d", 608 sources[aexpr->loc.file], 609 aexpr->loc.lineno, aexpr->loc.colno); 610 611 expr->assert.message->type = EXPR_CONSTANT; 612 expr->assert.message->result = &builtin_type_const_str; 613 expr->assert.message->constant.string.value = s; 614 expr->assert.message->constant.string.len = n - 1; 615 } 616 617 if (expr->assert.is_static) { 618 bool cond; 619 if (expr->assert.cond != NULL) { 620 struct expression out = {0}; 621 enum eval_result r = 622 eval_expr(ctx, expr->assert.cond, &out); 623 if (r != EVAL_OK) { 624 error(ctx, aexpr->assert.cond->loc, expr, 625 "Unable to evaluate static assertion at compile time"); 626 return; 627 } 628 assert(out.result->storage == STORAGE_BOOL); 629 cond = out.constant.bval; 630 } else { 631 cond = false; 632 } 633 // XXX: Should these abort immediately? 634 if (!cond) { 635 if (aexpr->assert.message != NULL) { 636 char format[40]; 637 snprintf(format, 40, "Static assertion failed %%%lds", 638 expr->assert.message->constant.string.len); 639 if (aexpr->assert.cond == NULL) { 640 error(ctx, aexpr->loc, expr, format, 641 expr->assert.message->constant.string.value); 642 return; 643 } else { 644 error(ctx, aexpr->assert.cond->loc, 645 expr, format, 646 expr->assert.message->constant.string.value); 647 return; 648 }; 649 } else { 650 error(ctx, aexpr->loc, expr, 651 "Static assertion failed"); 652 return; 653 } 654 } 655 } 656 } 657 658 static void 659 check_expr_assign(struct context *ctx, 660 const struct ast_expression *aexpr, 661 struct expression *expr, 662 const struct type *hint) 663 { 664 expr->type = EXPR_ASSIGN; 665 expr->result = &builtin_type_void; 666 expr->assign.indirect = aexpr->assign.indirect; 667 struct expression *object = xcalloc(1, sizeof(struct expression)); 668 struct expression *value = xcalloc(1, sizeof(struct expression)); 669 670 check_expression(ctx, aexpr->assign.object, object, NULL); 671 672 expr->assign.op = aexpr->assign.op; 673 674 if (aexpr->assign.indirect) { 675 const struct type *otype = type_dealias(object->result); 676 if (otype->storage != STORAGE_POINTER) { 677 error(ctx, aexpr->loc, expr, 678 "Cannot dereference non-pointer type for assignment"); 679 return; 680 } 681 if (otype->pointer.flags & PTR_NULLABLE) { 682 error(ctx, aexpr->loc, expr, 683 "Cannot dereference nullable pointer type"); 684 return; 685 } 686 check_expression(ctx, aexpr->assign.value, value, otype->pointer.referent); 687 if (!type_is_assignable(otype->pointer.referent, 688 value->result)) { 689 error(ctx, aexpr->loc, expr, 690 "Value type is not assignable to pointer type"); 691 return; 692 } 693 value = lower_implicit_cast(otype->pointer.referent, value); 694 } else { 695 check_expression(ctx, aexpr->assign.value, value, object->result); 696 assert(object->type == EXPR_CONSTANT // If error 697 || object->type == EXPR_ACCESS 698 || object->type == EXPR_SLICE); // Invariant 699 if (object->type == EXPR_SLICE) { 700 if (expr->assign.op != BIN_LEQUAL) { 701 error(ctx, aexpr->assign.object->loc, expr, 702 "Slice assignments may not have a binop"); 703 return; 704 } 705 } 706 if (object->result->flags & TYPE_CONST) { 707 error(ctx, aexpr->loc, expr, 708 "Cannot assign to const object"); 709 return; 710 } 711 if (!type_is_assignable(object->result, value->result)) { 712 char *valtypename = gen_typename(value->result); 713 char *objtypename = gen_typename(object->result); 714 error(ctx, aexpr->loc, expr, 715 "rvalue type (%s) is not assignable to lvalue (%s)", 716 valtypename, objtypename); 717 free(valtypename); 718 free(objtypename); 719 return; 720 } 721 value = lower_implicit_cast(object->result, value); 722 } 723 724 expr->assign.object = object; 725 expr->assign.value = value; 726 } 727 728 static const struct type * 729 type_promote(struct type_store *store, 730 const struct type *a, 731 const struct type *b) 732 { 733 // Note: we must return either a, b, or NULL 734 // TODO: There are likely some improperly handled edge cases around type 735 // flags, both here and in the spec 736 const struct type *da = type_store_lookup_with_flags(store, a, 0); 737 const struct type *db = type_store_lookup_with_flags(store, b, 0); 738 739 if (da == db) { 740 const struct type *base = type_store_lookup_with_flags(store, a, 741 a->flags | b->flags); 742 assert(base == a || base == b); 743 return base; 744 } 745 746 if (a->storage == STORAGE_ALIAS && b->storage == STORAGE_ALIAS) { 747 return NULL; 748 } 749 750 da = type_dealias(da); 751 db = type_dealias(db); 752 753 if (da == db) { 754 return a->storage == STORAGE_ALIAS ? a : b; 755 } 756 757 if (type_is_constant(da) || type_is_constant(db)) { 758 return promote_const(a, b); 759 } 760 761 if (db->storage == STORAGE_ENUM && da->storage == db->alias.type->storage) { 762 return b; 763 } 764 switch (da->storage) { 765 case STORAGE_ARRAY: 766 if (da->array.length == SIZE_UNDEFINED && da->array.members) { 767 return b; 768 } 769 if (db->array.length == SIZE_UNDEFINED && db->array.members) { 770 return a; 771 } 772 return NULL; 773 case STORAGE_ENUM: 774 if (da->alias.type->storage == db->storage) { 775 return a; 776 } 777 return NULL; 778 case STORAGE_I8: 779 case STORAGE_I16: 780 case STORAGE_I32: 781 case STORAGE_I64: 782 case STORAGE_INT: 783 if (!type_is_integer(db) || !type_is_signed(db) 784 || db->size == da->size) { 785 return NULL; 786 } 787 return da->size > db->size ? a : b; 788 case STORAGE_U32: 789 case STORAGE_U16: 790 case STORAGE_U64: 791 case STORAGE_UINT: 792 case STORAGE_SIZE: 793 case STORAGE_U8: 794 case STORAGE_CHAR: 795 if (!type_is_integer(db) || type_is_signed(db) 796 || db->size == da->size) { 797 return NULL; 798 } 799 return da->size > db->size ? a : b; 800 case STORAGE_F32: 801 case STORAGE_F64: 802 if (!type_is_float(db) || db->size == da->size) { 803 return NULL; 804 } 805 return da->size > db->size ? a : b; 806 case STORAGE_POINTER: 807 if (db->storage == STORAGE_NULL) { 808 return a; 809 } 810 if (db->storage != STORAGE_POINTER) { 811 return NULL; 812 } 813 if (da->pointer.referent->storage == STORAGE_VOID || 814 db->pointer.referent->storage == STORAGE_VOID) { 815 return a; 816 } 817 const struct type *r = type_promote(store, 818 da->pointer.referent, db->pointer.referent); 819 if (r == da->pointer.referent) { 820 return a; 821 } 822 if (r == db->pointer.referent) { 823 return b; 824 } 825 assert(r == NULL); 826 return NULL; 827 case STORAGE_NULL: 828 assert(db->storage != STORAGE_NULL); 829 if (db->storage == STORAGE_POINTER) { 830 return b; 831 } 832 return NULL; 833 // Cannot be promoted 834 case STORAGE_BOOL: 835 case STORAGE_FUNCTION: 836 case STORAGE_RUNE: 837 case STORAGE_SLICE: 838 case STORAGE_STRING: 839 case STORAGE_STRUCT: 840 case STORAGE_TAGGED: 841 case STORAGE_TUPLE: 842 case STORAGE_UINTPTR: 843 case STORAGE_UNION: 844 case STORAGE_VALIST: 845 case STORAGE_VOID: 846 return NULL; 847 // Handled above 848 case STORAGE_ALIAS: 849 case STORAGE_FCONST: 850 case STORAGE_ICONST: 851 case STORAGE_RCONST: 852 assert(0); 853 } 854 assert(0); 855 } 856 857 static void 858 check_expr_binarithm(struct context *ctx, 859 const struct ast_expression *aexpr, 860 struct expression *expr, 861 const struct type *hint) 862 { 863 expr->type = EXPR_BINARITHM; 864 expr->binarithm.op = aexpr->binarithm.op; 865 866 enum { 867 BT_INVALID = -1, 868 BT_NUMERIC, 869 BT_INTEGER, 870 BT_LOGICAL, 871 BT_COMPARISON, 872 BT_EQUALITY, 873 } btype; 874 875 btype = BT_INVALID; 876 877 switch (expr->binarithm.op) { 878 // Numeric arithmetic 879 case BIN_DIV: 880 case BIN_MINUS: 881 case BIN_PLUS: 882 case BIN_TIMES: 883 btype = BT_NUMERIC; 884 break; 885 // Integer artithmetic 886 case BIN_BAND: 887 case BIN_BOR: 888 case BIN_LSHIFT: 889 case BIN_MODULO: 890 case BIN_RSHIFT: 891 case BIN_BXOR: 892 btype = BT_INTEGER; 893 break; 894 // Logical arithmetic 895 case BIN_LAND: 896 case BIN_LOR: 897 case BIN_LXOR: 898 btype = BT_LOGICAL; 899 hint = NULL; 900 break; 901 case BIN_GREATER: 902 case BIN_GREATEREQ: 903 case BIN_LESS: 904 case BIN_LESSEQ: 905 btype = BT_COMPARISON; 906 hint = NULL; 907 break; 908 case BIN_LEQUAL: 909 case BIN_NEQUAL: 910 btype = BT_EQUALITY; 911 hint = NULL; 912 break; 913 } 914 915 struct expression *lvalue = xcalloc(1, sizeof(struct expression)), 916 *rvalue = xcalloc(1, sizeof(struct expression)); 917 struct ast_expression *alvalue = aexpr->binarithm.lvalue, 918 *arvalue = aexpr->binarithm.rvalue; 919 // XXX: Should hints be passed down? 920 (void)hint; 921 check_expression(ctx, alvalue, lvalue, NULL); 922 check_expression(ctx, arvalue, rvalue, NULL); 923 924 const struct type *p = 925 type_promote(ctx->store, lvalue->result, rvalue->result); 926 if (p == NULL) { 927 char *ltypename = gen_typename(lvalue->result); 928 char *rtypename = gen_typename(rvalue->result); 929 error(ctx, aexpr->loc, expr, 930 "Cannot promote lvalue %s and rvalue %s", 931 ltypename, rtypename); 932 free(ltypename); 933 free(rtypename); 934 return; 935 } 936 expr->result = &builtin_type_bool; 937 switch (btype) { 938 case BT_NUMERIC: 939 if (!type_is_numeric(p)) { 940 error(ctx, aexpr->loc, expr, 941 "Cannot perform arithmetic on non-numeric %s type", 942 type_storage_unparse(type_dealias(p)->storage)); 943 } 944 expr->result = p; 945 break; 946 case BT_INTEGER: 947 if (!type_is_integer(p)) { 948 error(ctx, aexpr->loc, expr, 949 "Cannot perform operation on non-integer %s type", 950 type_storage_unparse(type_dealias(p)->storage)); 951 } 952 expr->result = p; 953 break; 954 case BT_LOGICAL: 955 if (type_dealias(p)->storage != STORAGE_BOOL) { 956 error(ctx, aexpr->loc, expr, 957 "Cannot perform logical arithmetic on non-bool %s type", 958 type_storage_unparse(type_dealias(p)->storage)); 959 } 960 break; 961 case BT_COMPARISON: 962 if (!type_is_numeric(p)) { 963 error(ctx, aexpr->loc, expr, 964 "Cannot perform comparison on non-numeric %s type", 965 type_storage_unparse(type_dealias(p)->storage)); 966 } 967 break; 968 case BT_EQUALITY: 969 if (!type_is_numeric(p) && type_dealias(p)->storage != STORAGE_POINTER 970 && type_dealias(p)->storage != STORAGE_STRING 971 && type_dealias(p)->storage != STORAGE_BOOL 972 && type_dealias(p)->storage != STORAGE_RCONST 973 && type_dealias(p)->storage != STORAGE_RUNE) { 974 error(ctx, aexpr->loc, expr, 975 "Cannot perform equality test on %s type", 976 type_storage_unparse(type_dealias(p)->storage)); 977 } 978 break; 979 case BT_INVALID: 980 abort(); 981 break; 982 } 983 lvalue = lower_implicit_cast(p, lvalue); 984 rvalue = lower_implicit_cast(p, rvalue); 985 986 expr->binarithm.lvalue = lvalue; 987 expr->binarithm.rvalue = rvalue; 988 } 989 990 static void 991 check_binding_unpack(struct context *ctx, 992 const struct type *type, 993 const struct ast_expression_binding *abinding, 994 struct expression_binding *binding, 995 const struct ast_expression *aexpr, 996 struct expression *expr) 997 { 998 assert(abinding->unpack); 999 const struct ast_binding_unpack *cur = abinding->unpack; 1000 binding->unpack = xcalloc(1, sizeof(struct binding_unpack)); 1001 struct binding_unpack *unpack = binding->unpack; 1002 1003 struct expression *initializer = xcalloc(1, sizeof(struct expression)); 1004 check_expression(ctx, abinding->initializer, initializer, type); 1005 if (type_dealias(initializer->result)->storage != STORAGE_TUPLE) { 1006 error(ctx, aexpr->loc, expr, "Could not unpack non-tuple type"); 1007 return; 1008 } 1009 1010 if (!type) { 1011 type = type_store_lookup_with_flags( 1012 ctx->store, initializer->result, abinding->flags); 1013 } 1014 type = type_dealias(type); 1015 1016 binding->initializer = lower_implicit_cast(type, initializer); 1017 1018 if (abinding->is_static) { 1019 struct expression *value = xcalloc(1, sizeof(struct expression)); 1020 enum eval_result r = eval_expr(ctx, binding->initializer, value); 1021 if (r != EVAL_OK) { 1022 error(ctx, abinding->initializer->loc, 1023 expr, 1024 "Unable to evaluate static initializer at compile time"); 1025 return; 1026 } 1027 // TODO: Free initializer 1028 binding->initializer = value; 1029 assert(binding->initializer->type == EXPR_CONSTANT); 1030 } 1031 1032 if (type->storage != STORAGE_TUPLE) { 1033 error(ctx, abinding->initializer->loc, expr, 1034 "Unable to unpack tuple with non-tuple type specifier"); 1035 return; 1036 } 1037 const struct type_tuple *type_tuple = &type->tuple; 1038 bool found_binding = false; 1039 while (cur) { 1040 if (type_tuple->type->storage == STORAGE_NULL) { 1041 error(ctx, aexpr->loc, expr, 1042 "Null is not a valid type for a binding"); 1043 return; 1044 } 1045 1046 if (cur->name) { 1047 struct identifier ident = { 1048 .name = cur->name, 1049 }; 1050 1051 if (abinding->is_static) { 1052 struct identifier gen = {0}; 1053 1054 // Generate a static declaration identifier 1055 int n = snprintf(NULL, 0, "static.%d", ctx->id); 1056 gen.name = xcalloc(n + 1, 1); 1057 snprintf(gen.name, n + 1, "static.%d", ctx->id); 1058 ++ctx->id; 1059 1060 unpack->object = scope_insert( 1061 ctx->scope, O_DECL, &gen, &ident, 1062 type_tuple->type, NULL); 1063 } else { 1064 unpack->object = scope_insert( 1065 ctx->scope, O_BIND, &ident, &ident, 1066 type_tuple->type, NULL); 1067 } 1068 1069 unpack->offset = type_tuple->offset; 1070 1071 found_binding = true; 1072 } 1073 1074 cur = cur->next; 1075 type_tuple = type_tuple->next; 1076 1077 if (cur && found_binding && cur->name) { 1078 unpack->next = xcalloc(1, sizeof(struct binding_unpack)); 1079 unpack = unpack->next; 1080 } 1081 } 1082 1083 if (!found_binding) { 1084 error(ctx, aexpr->loc, expr, 1085 "Must have at least one non-underscore value when unpacking tuples"); 1086 return; 1087 } 1088 1089 if (type_tuple) { 1090 error(ctx, aexpr->loc, expr, 1091 "Fewer bindings than tuple elements were provided when unpacking"); 1092 return; 1093 } 1094 } 1095 1096 static void 1097 check_expr_binding(struct context *ctx, 1098 const struct ast_expression *aexpr, 1099 struct expression *expr, 1100 const struct type *hint) 1101 { 1102 expr->type = EXPR_BINDING; 1103 expr->result = &builtin_type_void; 1104 1105 struct expression_binding *binding = &expr->binding; 1106 struct expression_binding **next = &expr->binding.next; 1107 1108 const struct ast_expression_binding *abinding = &aexpr->binding; 1109 while (abinding) { 1110 const struct type *type = NULL; 1111 if (abinding->type) { 1112 type = type_store_lookup_atype( 1113 ctx->store, abinding->type); 1114 type = type_store_lookup_with_flags(ctx->store, 1115 type, type->flags | abinding->flags); 1116 } 1117 1118 if (abinding->unpack) { 1119 check_binding_unpack(ctx, type, abinding, binding, 1120 aexpr, expr); 1121 goto done; 1122 } 1123 1124 struct identifier ident = { 1125 .name = abinding->name, 1126 }; 1127 struct expression *initializer = 1128 xcalloc(1, sizeof(struct expression)); 1129 1130 struct identifier gen = {0}; 1131 if (abinding->is_static) { 1132 // Generate a static declaration identifier 1133 int n = snprintf(NULL, 0, "static.%d", ctx->id); 1134 gen.name = xcalloc(n + 1, 1); 1135 snprintf(gen.name, n + 1, "static.%d", ctx->id); 1136 ++ctx->id; 1137 } 1138 1139 bool context = abinding->type 1140 && abinding->type->storage == STORAGE_ARRAY 1141 && abinding->type->array.contextual; 1142 const struct scope_object *shadowed = 1143 scope_lookup(ctx->scope, &ident); 1144 if (type && !context && shadowed == NULL) { 1145 // If the type is defined in advance, and a variable 1146 // isn't being shadowed, we can insert the object into 1147 // the scope early, which is required for 1148 // self-referencing objects. 1149 if (!abinding->is_static) { 1150 binding->object = scope_insert(ctx->scope, 1151 O_BIND, &ident, &ident, type, NULL); 1152 } else { 1153 binding->object = scope_insert(ctx->scope, 1154 O_DECL, &gen, &ident, type, NULL); 1155 } 1156 } 1157 1158 check_expression(ctx, abinding->initializer, initializer, type); 1159 1160 if (context) { 1161 if (initializer->result->storage != STORAGE_ARRAY) { 1162 error(ctx, aexpr->loc, expr, 1163 "Cannot infer array length from non-array type"); 1164 return; 1165 } 1166 if (initializer->result->array.members 1167 != type->array.members) { 1168 error(ctx, aexpr->loc, expr, 1169 "Initializer is not assignable to binding type"); 1170 return; 1171 } 1172 type = initializer->result; 1173 } 1174 1175 if (context || !type || shadowed != NULL) { 1176 if (!type) { 1177 type = type_store_lookup_with_flags(ctx->store, 1178 initializer->result, abinding->flags); 1179 } 1180 1181 if (!abinding->is_static) { 1182 binding->object = scope_insert(ctx->scope, 1183 O_BIND, &ident, &ident, type, NULL); 1184 } else { 1185 binding->object = scope_insert(ctx->scope, 1186 O_DECL, &gen, &ident, type, NULL); 1187 } 1188 } 1189 1190 if (type->storage == STORAGE_NULL) { 1191 error(ctx, aexpr->loc, expr, 1192 "Null is not a valid type for a binding"); 1193 return; 1194 } 1195 if (!type_is_assignable(type, initializer->result)) { 1196 error(ctx, aexpr->loc, expr, 1197 "Initializer is not assignable to binding type"); 1198 return; 1199 } 1200 // XXX: Can we avoid this? 1201 type = lower_const(type, NULL); 1202 if (type->size == 0 || type->size == SIZE_UNDEFINED) { 1203 error(ctx, aexpr->loc, expr, 1204 "Cannot create binding for type of zero or undefined size"); 1205 return; 1206 } 1207 binding->initializer = lower_implicit_cast(type, initializer); 1208 1209 if (abinding->is_static) { 1210 struct expression *value = 1211 xcalloc(1, sizeof(struct expression)); 1212 enum eval_result r = eval_expr( 1213 ctx, binding->initializer, value); 1214 if (r != EVAL_OK) { 1215 error(ctx, abinding->initializer->loc, expr, 1216 "Unable to evaluate static initializer at compile time"); 1217 return; 1218 } 1219 // TODO: Free initializer 1220 binding->initializer = value; 1221 } 1222 1223 done: 1224 if (abinding->next) { 1225 binding = *next = 1226 xcalloc(1, sizeof(struct expression_binding)); 1227 next = &binding->next; 1228 } 1229 1230 abinding = abinding->next; 1231 } 1232 } 1233 1234 // Lower Hare-style variadic arguments into an array literal 1235 static void 1236 lower_vaargs(struct context *ctx, 1237 const struct ast_call_argument *aarg, 1238 struct expression *vaargs, 1239 const struct type *type) 1240 { 1241 struct ast_expression val = { 1242 .type = EXPR_CONSTANT, 1243 .constant = { 1244 .storage = STORAGE_ARRAY, 1245 }, 1246 }; 1247 // TODO: Provide location some other way 1248 if (aarg) { 1249 val.loc = aarg->value->loc; 1250 } 1251 struct ast_array_constant **next = &val.constant.array; 1252 while (aarg) { 1253 struct ast_array_constant *item = *next = 1254 xcalloc(1, sizeof(struct ast_array_constant)); 1255 item->value = aarg->value; 1256 aarg = aarg->next; 1257 next = &item->next; 1258 } 1259 1260 // XXX: This error handling is minimum-effort and bad 1261 const struct type *hint = type_store_lookup_array( 1262 ctx->store, type, SIZE_UNDEFINED, false); 1263 check_expression(ctx, &val, vaargs, hint); 1264 if (vaargs->result->storage != STORAGE_ARRAY 1265 || vaargs->result->array.members != type) { 1266 error(ctx, val.loc, vaargs, 1267 "Argument is not assignable to variadic parameter type"); 1268 return; 1269 } 1270 1271 struct ast_array_constant *item = val.constant.array; 1272 while (item) { 1273 struct ast_array_constant *next = item->next; 1274 free(item); 1275 item = next; 1276 } 1277 } 1278 1279 static void 1280 check_expr_call(struct context *ctx, 1281 const struct ast_expression *aexpr, 1282 struct expression *expr, 1283 const struct type *hint) 1284 { 1285 expr->type = EXPR_CALL; 1286 1287 struct expression *lvalue = xcalloc(1, sizeof(struct expression)); 1288 check_expression(ctx, aexpr->call.lvalue, lvalue, NULL); 1289 expr->call.lvalue = lvalue; 1290 1291 const struct type *fntype = type_dereference(lvalue->result); 1292 if (!fntype) { 1293 error(ctx, aexpr->loc, expr, 1294 "Cannot dereference nullable pointer type for function call"); 1295 return; 1296 } 1297 fntype = type_dealias(fntype); 1298 if (fntype->storage != STORAGE_FUNCTION) { 1299 error(ctx, aexpr->loc, expr, 1300 "Cannot call non-function type"); 1301 return; 1302 } 1303 expr->result = fntype->func.result; 1304 if (fntype->func.flags & FN_NORETURN) { 1305 expr->terminates = true; 1306 } 1307 1308 struct call_argument *arg, **next = &expr->call.args; 1309 struct ast_call_argument *aarg = aexpr->call.args; 1310 struct type_func_param *param = fntype->func.params; 1311 while ((param || fntype->func.variadism == VARIADISM_C) && aarg) { 1312 arg = *next = xcalloc(1, sizeof(struct call_argument)); 1313 arg->value = xcalloc(1, sizeof(struct expression)); 1314 1315 if (param && !param->next 1316 && fntype->func.variadism == VARIADISM_HARE 1317 && !aarg->variadic) { 1318 lower_vaargs(ctx, aarg, arg->value, 1319 param->type->array.members); 1320 arg->value = lower_implicit_cast(param->type, arg->value); 1321 param = NULL; 1322 aarg = NULL; 1323 break; 1324 } 1325 1326 const struct type *ptype = NULL; 1327 if (param) { 1328 ptype = param->type; 1329 } 1330 check_expression(ctx, aarg->value, arg->value, ptype); 1331 1332 if (param) { 1333 if (!type_is_assignable(ptype, arg->value->result)) { 1334 char *argtypename = gen_typename(arg->value->result); 1335 char *paramtypename = gen_typename(param->type); 1336 error(ctx, aarg->value->loc, expr, 1337 "Argument type %s is not assignable to parameter type %s", 1338 argtypename, paramtypename); 1339 free(argtypename); 1340 free(paramtypename); 1341 return; 1342 } 1343 arg->value = lower_implicit_cast(ptype, arg->value); 1344 } 1345 1346 aarg = aarg->next; 1347 next = &arg->next; 1348 if (param) { 1349 param = param->next; 1350 } 1351 } 1352 1353 if (param && !param->next && fntype->func.variadism == VARIADISM_HARE) { 1354 // No variadic arguments, lower to empty slice 1355 arg = *next = xcalloc(1, sizeof(struct call_argument)); 1356 arg->value = xcalloc(1, sizeof(struct expression)); 1357 lower_vaargs(ctx, NULL, arg->value, 1358 param->type->array.members); 1359 arg->value = lower_implicit_cast(param->type, arg->value); 1360 param = param->next; 1361 } 1362 1363 if (aarg && fntype->func.variadism != VARIADISM_C) { 1364 error(ctx, aexpr->loc, expr, 1365 "Too many parameters for function call"); 1366 return; 1367 } 1368 if (param) { 1369 error(ctx, aexpr->loc, expr, 1370 "Not enough parameters for function call"); 1371 return; 1372 } 1373 1374 } 1375 1376 static void 1377 check_expr_cast(struct context *ctx, 1378 const struct ast_expression *aexpr, 1379 struct expression *expr, 1380 const struct type *hint) 1381 { 1382 expr->type = EXPR_CAST; 1383 expr->cast.kind = aexpr->cast.kind; 1384 struct expression *value = expr->cast.value = 1385 xcalloc(1, sizeof(struct expression)); 1386 const struct type *secondary = expr->cast.secondary = 1387 type_store_lookup_atype(ctx->store, aexpr->cast.type); 1388 // TODO: Instead of allowing errors on casts to void, we should use a 1389 // different nonterminal 1390 check_expression(ctx, aexpr->cast.value, value, 1391 secondary == &builtin_type_void ? NULL : secondary); 1392 if (value->terminates) { 1393 error(ctx, aexpr->cast.value->loc, expr, 1394 "Cast must not operate on terminating expression"); 1395 return; 1396 } 1397 1398 const struct type *primary = type_dealias(expr->cast.value->result); 1399 switch (aexpr->cast.kind) { 1400 case C_ASSERTION: 1401 case C_TEST: 1402 if (primary->storage == STORAGE_POINTER) { 1403 if (!(primary->pointer.flags & PTR_NULLABLE)) { 1404 error(ctx, aexpr->cast.value->loc, expr, 1405 "Expected a tagged union type or " 1406 "a nullable pointer"); 1407 return; 1408 } 1409 break; 1410 } 1411 if (primary->storage != STORAGE_TAGGED) { 1412 error(ctx, aexpr->cast.value->loc, expr, 1413 "Expected a tagged union type or " 1414 "a nullable pointer"); 1415 return; 1416 } 1417 // secondary type must be a strict subset or a 1418 // member of the primary type 1419 if (!((tagged_subset_compat(primary, secondary) 1420 || tagged_select_subtype(primary, secondary)) 1421 && !tagged_subset_compat(secondary, primary))) { 1422 error(ctx, aexpr->cast.type->loc, expr, 1423 "Type is not a valid member of " 1424 "the tagged union type"); 1425 return; 1426 } 1427 break; 1428 case C_CAST: 1429 if (!type_is_castable(secondary, value->result)) { 1430 char *primarytypename = gen_typename(value->result); 1431 char *secondarytypename = gen_typename(secondary); 1432 error(ctx, aexpr->cast.type->loc, expr, 1433 "Invalid cast from %s to %s", 1434 primarytypename, secondarytypename); 1435 free(primarytypename); 1436 free(secondarytypename); 1437 return; 1438 } 1439 if (primary->storage == STORAGE_RCONST) { 1440 uint32_t max = 0; 1441 switch (secondary->storage) { 1442 case STORAGE_RUNE: 1443 case STORAGE_U64: 1444 case STORAGE_I64: 1445 case STORAGE_U32: 1446 break; 1447 case STORAGE_U16: 1448 max = UINT16_MAX; 1449 break; 1450 case STORAGE_I16: 1451 max = INT16_MAX; 1452 break; 1453 case STORAGE_U8: 1454 max = UINT8_MAX; 1455 break; 1456 case STORAGE_I8: 1457 max = INT8_MAX; 1458 break; 1459 default: 1460 assert(0); // Invariant 1461 } 1462 1463 if (max != 0 && value->constant.rune > max) { 1464 char *typename = gen_typename(secondary); 1465 error(ctx, aexpr->cast.type->loc, expr, 1466 "Rune does not fit in %s", 1467 typename); 1468 free(typename); 1469 return; 1470 } 1471 } 1472 break; 1473 } 1474 expr->result = aexpr->cast.kind == C_TEST? &builtin_type_bool : secondary; 1475 } 1476 1477 static void 1478 check_expr_array(struct context *ctx, 1479 const struct ast_expression *aexpr, 1480 struct expression *expr, 1481 const struct type *hint) 1482 { 1483 size_t len = 0; 1484 bool expand = false; 1485 struct ast_array_constant *item = aexpr->constant.array; 1486 struct array_constant *cur, **next = &expr->constant.array; 1487 const struct type *type = NULL; 1488 if (hint) { 1489 hint = type_dealias(hint); 1490 1491 size_t narray = 0; 1492 switch (hint->storage) { 1493 case STORAGE_ARRAY: 1494 case STORAGE_SLICE: 1495 type = hint->array.members; 1496 break; 1497 case STORAGE_TAGGED: 1498 for (const struct type_tagged_union *tu = &hint->tagged; 1499 tu; tu = tu->next) { 1500 const struct type *t = type_dealias(tu->type); 1501 if (t->storage == STORAGE_ARRAY 1502 || t->storage == STORAGE_SLICE) { 1503 hint = t; 1504 type = hint->array.members; 1505 ++narray; 1506 } 1507 } 1508 if (narray != 1) { 1509 type = hint = NULL; 1510 } 1511 break; 1512 default: 1513 hint = NULL; 1514 break; 1515 } 1516 } 1517 1518 while (item) { 1519 struct expression *value = xcalloc(1, sizeof(struct expression)); 1520 check_expression(ctx, item->value, value, type); 1521 cur = *next = xcalloc(1, sizeof(struct array_constant)); 1522 cur->value = value; 1523 1524 if (!type) { 1525 type = value->result; 1526 } else { 1527 if (!hint) { 1528 // The promote_const in 1529 // check_expression_constant might've caused the 1530 // type to change out from under our feet 1531 type = expr->constant.array->value->result; 1532 } 1533 if (!type_is_assignable(type, value->result)) { 1534 char *typename1 = gen_typename(type); 1535 char *typename2 = gen_typename(value->result); 1536 error(ctx, item->value->loc, expr, 1537 "Array members must be of a uniform type, previously seen %s, but now see %s", 1538 typename1, typename2); 1539 free(typename1); 1540 free(typename2); 1541 return; 1542 } 1543 if (!hint) { 1544 // Ditto 1545 type = expr->constant.array->value->result; 1546 } 1547 cur->value = lower_implicit_cast(type, cur->value); 1548 } 1549 1550 if (item->expand) { 1551 expand = true; 1552 assert(!item->next); 1553 } 1554 1555 item = item->next; 1556 next = &cur->next; 1557 ++len; 1558 } 1559 1560 if (type == NULL) { 1561 error(ctx, aexpr->loc, expr, "Cannot infer array type from context, try casting it to the desired type"); 1562 return; 1563 } 1564 expr->result = type_store_lookup_array(ctx->store, type, len, expand); 1565 } 1566 1567 static void 1568 check_expr_compound(struct context *ctx, 1569 const struct ast_expression *aexpr, 1570 struct expression *expr, 1571 const struct type *hint) 1572 { 1573 expr->type = EXPR_COMPOUND; 1574 1575 struct scope *scope = scope_push(&ctx->scope, SCOPE_COMPOUND); 1576 scope->hint = hint; 1577 expr->compound.scope = scope; 1578 1579 if (aexpr->compound.label) { 1580 expr->compound.label = strdup(aexpr->compound.label); 1581 scope->label = strdup(aexpr->compound.label); 1582 } 1583 1584 struct expressions *list = &expr->compound.exprs; 1585 struct expressions **next = &list->next; 1586 1587 const struct ast_expression_list *alist = &aexpr->compound.list; 1588 struct expression *lexpr = NULL; 1589 while (alist) { 1590 lexpr = xcalloc(1, sizeof(struct expression)); 1591 check_expression(ctx, alist->expr, lexpr, &builtin_type_void); 1592 list->expr = lexpr; 1593 1594 alist = alist->next; 1595 if (alist) { 1596 *next = xcalloc(1, sizeof(struct expressions)); 1597 list = *next; 1598 next = &list->next; 1599 } 1600 if (alist && lexpr->terminates) { 1601 error(ctx, alist->expr->loc, expr, 1602 "A terminating expression may not be followed by additional expressions"); 1603 } 1604 } 1605 1606 expr->terminates = lexpr->terminates && lexpr->type != EXPR_YIELD; 1607 expr->result = type_store_reduce_result(ctx->store, scope->results); 1608 1609 for (struct yield *yield = scope->yields; yield;) { 1610 struct expression *lowered = lower_implicit_cast( 1611 expr->result, *yield->expression); 1612 if (*yield->expression != lowered) { 1613 *yield->expression = lowered; 1614 } 1615 1616 struct yield *next = yield->next; 1617 free(yield); 1618 yield = next; 1619 } 1620 1621 assert(expr->result); 1622 scope_pop(&ctx->scope); 1623 } 1624 1625 static void 1626 check_expr_constant(struct context *ctx, 1627 const struct ast_expression *aexpr, 1628 struct expression *expr, 1629 const struct type *hint) 1630 { 1631 expr->type = EXPR_CONSTANT; 1632 enum type_storage storage = aexpr->constant.storage; 1633 expr->result = builtin_type_for_storage(storage, false); 1634 if (storage == STORAGE_ICONST || storage == STORAGE_FCONST 1635 || storage == STORAGE_RCONST) { 1636 expr->result = type_create_const(storage, 1637 aexpr->constant.ival, aexpr->constant.ival); 1638 } 1639 1640 switch (aexpr->constant.storage) { 1641 case STORAGE_I8: 1642 case STORAGE_I16: 1643 case STORAGE_I32: 1644 case STORAGE_I64: 1645 case STORAGE_ICONST: 1646 case STORAGE_INT: 1647 expr->constant.ival = aexpr->constant.ival; 1648 break; 1649 case STORAGE_U8: 1650 case STORAGE_U16: 1651 case STORAGE_U32: 1652 case STORAGE_U64: 1653 case STORAGE_UINT: 1654 case STORAGE_SIZE: 1655 expr->constant.uval = aexpr->constant.uval; 1656 break; 1657 case STORAGE_RCONST: 1658 expr->constant.rune = aexpr->constant.rune; 1659 break; 1660 case STORAGE_BOOL: 1661 expr->constant.bval = aexpr->constant.bval; 1662 break; 1663 case STORAGE_NULL: 1664 case STORAGE_VOID: 1665 // No storage 1666 break; 1667 case STORAGE_ARRAY: 1668 check_expr_array(ctx, aexpr, expr, hint); 1669 break; 1670 case STORAGE_STRING: 1671 expr->constant.string.len = aexpr->constant.string.len; 1672 expr->constant.string.value = xcalloc(1, aexpr->constant.string.len); 1673 memcpy(expr->constant.string.value, aexpr->constant.string.value, 1674 aexpr->constant.string.len); 1675 break; 1676 case STORAGE_F32: 1677 case STORAGE_F64: 1678 case STORAGE_FCONST: 1679 expr->constant.fval = aexpr->constant.fval; 1680 break; 1681 case STORAGE_CHAR: 1682 case STORAGE_ENUM: 1683 case STORAGE_UINTPTR: 1684 case STORAGE_ALIAS: 1685 case STORAGE_FUNCTION: 1686 case STORAGE_POINTER: 1687 case STORAGE_RUNE: 1688 case STORAGE_SLICE: 1689 case STORAGE_TAGGED: 1690 case STORAGE_TUPLE: 1691 case STORAGE_STRUCT: 1692 case STORAGE_UNION: 1693 case STORAGE_VALIST: 1694 assert(0); // Invariant 1695 } 1696 } 1697 1698 static void 1699 check_expr_defer(struct context *ctx, 1700 const struct ast_expression *aexpr, 1701 struct expression *expr, 1702 const struct type *hint) 1703 { 1704 if (ctx->deferring) { 1705 error(ctx, aexpr->loc, expr, 1706 "Cannot defer within another defer expression."); 1707 return; 1708 } 1709 if (ctx->scope->class == SCOPE_FUNC) { 1710 error(ctx, aexpr->loc, expr, 1711 "Cannot defer in a function scope"); 1712 return; 1713 } 1714 expr->type = EXPR_DEFER; 1715 expr->result = &builtin_type_void; 1716 expr->defer.deferred = xcalloc(1, sizeof(struct expression)); 1717 ctx->deferring = true; 1718 check_expression(ctx, aexpr->defer.deferred, expr->defer.deferred, &builtin_type_void); 1719 ctx->deferring = false; 1720 } 1721 1722 static void 1723 check_expr_delete(struct context *ctx, 1724 const struct ast_expression *aexpr, 1725 struct expression *expr, 1726 const struct type *hint) 1727 { 1728 expr->type = EXPR_DELETE; 1729 expr->delete.is_static = aexpr->delete.is_static; 1730 expr->result = &builtin_type_void; 1731 struct expression *dexpr = expr->delete.expr = 1732 xcalloc(1, sizeof(struct expression)); 1733 check_expression(ctx, aexpr->delete.expr, expr->delete.expr, NULL); 1734 const struct type *otype = NULL; 1735 switch (dexpr->type) { 1736 case EXPR_SLICE: 1737 otype = dexpr->slice.object->result; 1738 break; 1739 case EXPR_ACCESS: 1740 if (dexpr->access.type != ACCESS_INDEX) { 1741 error(ctx, aexpr->delete.expr->loc, expr, 1742 "Deleted expression must be slicing or indexing expression"); 1743 return; 1744 } 1745 otype = dexpr->access.array->result; 1746 break; 1747 default: 1748 error(ctx, aexpr->delete.expr->loc, expr, 1749 "Deleted expression must be slicing or indexing expression"); 1750 return; 1751 } 1752 otype = type_dealias(type_dereference(otype)); 1753 if (otype->storage != STORAGE_SLICE) { 1754 error(ctx, aexpr->delete.expr->loc, expr, 1755 "delete must operate on a slice"); 1756 return; 1757 } 1758 if (otype->flags & TYPE_CONST) { 1759 error(ctx, aexpr->delete.expr->loc, expr, 1760 "delete must operate on a mutable slice"); 1761 return; 1762 } 1763 } 1764 1765 static void 1766 check_expr_control(struct context *ctx, 1767 const struct ast_expression *aexpr, 1768 struct expression *expr, 1769 const struct type *hint) 1770 { 1771 expr->type = aexpr->type; 1772 expr->result = &builtin_type_void; 1773 expr->control.label = aexpr->control.label; 1774 expr->terminates = true; 1775 1776 enum scope_class want; 1777 switch (expr->type) { 1778 case EXPR_BREAK: 1779 case EXPR_CONTINUE: 1780 want = SCOPE_LOOP; 1781 break; 1782 case EXPR_YIELD: 1783 want = SCOPE_COMPOUND; 1784 break; 1785 default: 1786 abort(); // Invariant 1787 } 1788 1789 struct scope *scope = scope_lookup_ancestor( 1790 ctx->scope, want, aexpr->control.label); 1791 if (!scope) { 1792 // XXX: This error message is bad 1793 error(ctx, aexpr->loc, expr, "No eligible loop for operation"); 1794 return; 1795 } 1796 expr->control.scope = scope; 1797 1798 if (aexpr->control.value) { 1799 expr->control.value = xcalloc(1, sizeof(struct expression)); 1800 check_expression(ctx, aexpr->control.value, 1801 expr->control.value, scope->hint); 1802 1803 struct type_tagged_union *result = 1804 xcalloc(1, sizeof(struct type_tagged_union)); 1805 result->type = expr->control.value->result; 1806 result->next = scope->results; 1807 scope->results = result; 1808 1809 struct yield *yield = xcalloc(1, sizeof(struct yield)); 1810 yield->expression = &expr->control.value; 1811 yield->next = scope->yields; 1812 scope->yields = yield; 1813 } 1814 } 1815 1816 static void 1817 check_expr_for(struct context *ctx, 1818 const struct ast_expression *aexpr, 1819 struct expression *expr, 1820 const struct type *hint) 1821 { 1822 expr->type = EXPR_FOR; 1823 expr->result = &builtin_type_void; 1824 1825 struct scope *scope = scope_push(&ctx->scope, SCOPE_LOOP); 1826 expr->_for.scope = scope; 1827 1828 struct expression *bindings = NULL, 1829 *cond = NULL, *afterthought = NULL, *body = NULL; 1830 1831 if (aexpr->_for.bindings) { 1832 bindings = xcalloc(1, sizeof(struct expression)); 1833 check_expression(ctx, aexpr->_for.bindings, bindings, NULL); 1834 expr->_for.bindings = bindings; 1835 } 1836 1837 cond = xcalloc(1, sizeof(struct expression)); 1838 check_expression(ctx, aexpr->_for.cond, cond, &builtin_type_bool); 1839 expr->_for.cond = cond; 1840 if (type_dealias(cond->result)->storage != STORAGE_BOOL) { 1841 error(ctx, aexpr->_for.cond->loc, expr, 1842 "Expected for condition to be boolean"); 1843 return; 1844 } 1845 1846 if (aexpr->_for.afterthought) { 1847 afterthought = xcalloc(1, sizeof(struct expression)); 1848 check_expression(ctx, aexpr->_for.afterthought, afterthought, &builtin_type_void); 1849 expr->_for.afterthought = afterthought; 1850 } 1851 1852 body = xcalloc(1, sizeof(struct expression)); 1853 check_expression(ctx, aexpr->_for.body, body, &builtin_type_void); 1854 expr->_for.body = body; 1855 1856 scope_pop(&ctx->scope); 1857 } 1858 1859 static void 1860 check_expr_free(struct context *ctx, 1861 const struct ast_expression *aexpr, 1862 struct expression *expr, 1863 const struct type *hint) 1864 { 1865 assert(aexpr->type == EXPR_FREE); 1866 expr->type = EXPR_FREE; 1867 expr->free.expr = xcalloc(sizeof(struct expression), 1); 1868 check_expression(ctx, aexpr->free.expr, expr->free.expr, NULL); 1869 enum type_storage storage = type_dealias(expr->free.expr->result)->storage; 1870 if (storage != STORAGE_SLICE && storage != STORAGE_STRING 1871 && storage != STORAGE_POINTER) { 1872 error(ctx, aexpr->free.expr->loc, expr, 1873 "free must operate on slice, string, or pointer"); 1874 return; 1875 } 1876 expr->result = &builtin_type_void; 1877 } 1878 1879 static void 1880 check_expr_if(struct context *ctx, 1881 const struct ast_expression *aexpr, 1882 struct expression *expr, 1883 const struct type *hint) 1884 { 1885 expr->type = EXPR_IF; 1886 1887 struct expression *cond, *true_branch, *false_branch = NULL; 1888 1889 cond = xcalloc(1, sizeof(struct expression)); 1890 check_expression(ctx, aexpr->_if.cond, cond, &builtin_type_bool); 1891 1892 true_branch = xcalloc(1, sizeof(struct expression)); 1893 check_expression(ctx, aexpr->_if.true_branch, true_branch, hint); 1894 1895 if (aexpr->_if.false_branch) { 1896 false_branch = xcalloc(1, sizeof(struct expression)); 1897 check_expression(ctx, aexpr->_if.false_branch, false_branch, hint); 1898 1899 bool tt = true_branch->terminates, ft = false_branch->terminates; 1900 if (tt && ft) { 1901 expr->terminates = true; 1902 expr->result = &builtin_type_void; 1903 } else if (!tt && ft) { 1904 expr->result = true_branch->result; 1905 } else if (tt && !ft) { 1906 expr->result = false_branch->result; 1907 } else if (hint && type_is_assignable(hint, true_branch->result) 1908 && type_is_assignable(hint, false_branch->result)) { 1909 expr->result = hint; 1910 } else { 1911 struct type_tagged_union _tags = { 1912 .type = false_branch->result, 1913 .next = NULL, 1914 }, tags = { 1915 .type = true_branch->result, 1916 .next = &_tags, 1917 }; 1918 expr->result = 1919 type_store_reduce_result(ctx->store, &tags); 1920 if (expr->result == NULL) { 1921 error(ctx, aexpr->loc, expr, 1922 "Invalid result type (dangling or ambiguous null)"); 1923 return; 1924 } 1925 } 1926 true_branch = lower_implicit_cast(expr->result, true_branch); 1927 false_branch = lower_implicit_cast(expr->result, false_branch); 1928 } else { 1929 expr->result = &builtin_type_void; 1930 expr->terminates = false; 1931 } 1932 1933 if (type_dealias(cond->result)->storage != STORAGE_BOOL) { 1934 error(ctx, aexpr->_if.cond->loc, expr, 1935 "Expected if condition to be boolean"); 1936 return; 1937 } 1938 1939 expr->_if.cond = cond; 1940 expr->_if.true_branch = true_branch; 1941 expr->_if.false_branch = false_branch; 1942 } 1943 1944 static void 1945 check_expr_match(struct context *ctx, 1946 const struct ast_expression *aexpr, 1947 struct expression *expr, 1948 const struct type *hint) 1949 { 1950 expr->type = EXPR_MATCH; 1951 1952 struct expression *value = xcalloc(1, sizeof(struct expression)); 1953 check_expression(ctx, aexpr->match.value, value, NULL); expr->match.value = value; 1954 1955 const struct type *type = type_dealias(value->result); 1956 bool is_ptr = type->storage == STORAGE_POINTER 1957 && type->pointer.flags & PTR_NULLABLE; 1958 if (type->storage != STORAGE_TAGGED && !is_ptr) { 1959 error(ctx, aexpr->match.value->loc, expr, 1960 "match value must be tagged union or nullable pointer type"); 1961 return; 1962 } 1963 1964 struct type_tagged_union result_type = {0}; 1965 struct type_tagged_union *tagged = &result_type, 1966 **next_tag = &tagged->next; 1967 1968 struct match_case **next = &expr->match.cases, *_case = NULL; 1969 for (struct ast_match_case *acase = aexpr->match.cases; 1970 acase; acase = acase->next) { 1971 _case = *next = xcalloc(1, sizeof(struct match_case)); 1972 next = &_case->next; 1973 1974 const struct type *ctype = NULL; 1975 if (acase->type) { 1976 ctype = type_store_lookup_atype(ctx->store, acase->type); 1977 if (is_ptr) { 1978 switch (ctype->storage) { 1979 case STORAGE_NULL: 1980 break; 1981 case STORAGE_POINTER: 1982 if (type->pointer.referent != ctype->pointer.referent) { 1983 error(ctx, acase->type->loc, expr, 1984 "Match case on incompatible pointer type"); 1985 return; 1986 } 1987 break; 1988 default: 1989 error(ctx, acase->type->loc, expr, 1990 "Invalid type for match case (expected null or pointer type)"); 1991 return; 1992 } 1993 } else { 1994 // TODO: Assign a score to tagged compatibility 1995 // and choose the branch with the highest score. 1996 if (!type_is_assignable(type, ctype)) { 1997 error(ctx, acase->type->loc, expr, 1998 "Invalid type for match case (match is not assignable to this type)"); 1999 return; 2000 } 2001 } 2002 } 2003 2004 if (acase->name) { 2005 assert(ctype); 2006 if (ctype->size == 0 || ctype->size == SIZE_UNDEFINED) { 2007 error(ctx, acase->type->loc, expr, 2008 "Cannot create binding for type of zero or undefined size"); 2009 return; 2010 } 2011 if (ctype->storage == STORAGE_NULL) { 2012 error(ctx, aexpr->loc, expr, 2013 "Null is not a valid type for a binding"); 2014 return; 2015 } 2016 struct identifier ident = { 2017 .name = acase->name, 2018 }; 2019 struct scope *scope = scope_push( 2020 &ctx->scope, SCOPE_MATCH); 2021 _case->object = scope_insert(scope, O_BIND, 2022 &ident, &ident, ctype, NULL); 2023 } 2024 2025 _case->value = xcalloc(1, sizeof(struct expression)); 2026 _case->type = ctype; 2027 2028 // Lower to compound 2029 // TODO: This should probably be done in a more first-class way 2030 struct ast_expression compound = { 2031 .type = EXPR_COMPOUND, 2032 .compound = { 2033 .list = acase->exprs, 2034 }, 2035 }; 2036 check_expression(ctx, &compound, _case->value, hint); 2037 2038 if (acase->name) { 2039 scope_pop(&ctx->scope); 2040 } 2041 2042 if (_case->value->terminates) { 2043 continue; 2044 } 2045 2046 if (expr->result == NULL) { 2047 expr->result = _case->value->result; 2048 tagged->type = expr->result; 2049 } else if (expr->result != _case->value->result) { 2050 tagged = *next_tag = 2051 xcalloc(1, sizeof(struct type_tagged_union)); 2052 next_tag = &tagged->next; 2053 tagged->type = _case->value->result; 2054 } 2055 } 2056 2057 if (expr->result == NULL) { 2058 expr->result = &builtin_type_void; 2059 expr->terminates = true; 2060 } 2061 2062 if (result_type.next) { 2063 if (hint) { 2064 expr->result = hint; 2065 } else { 2066 expr->result = type_store_reduce_result( 2067 ctx->store, &result_type); 2068 if (expr->result == NULL) { 2069 error(ctx, aexpr->loc, expr, 2070 "Invalid result type (dangling or ambiguous null)"); 2071 return; 2072 } 2073 } 2074 2075 struct match_case *_case = expr->match.cases; 2076 struct ast_match_case *acase = aexpr->match.cases; 2077 while (_case) { 2078 if (!_case->value->terminates && !type_is_assignable( 2079 expr->result, _case->value->result)) { 2080 error(ctx, acase->exprs.expr->loc, expr, 2081 "Match case is not assignable to result type"); 2082 return; 2083 } 2084 _case->value = lower_implicit_cast( 2085 expr->result, _case->value); 2086 _case = _case->next; 2087 acase = acase->next; 2088 } 2089 2090 struct type_tagged_union *tu = result_type.next; 2091 while (tu) { 2092 struct type_tagged_union *next = tu->next; 2093 free(tu); 2094 tu = next; 2095 } 2096 } 2097 } 2098 2099 static void 2100 check_expr_measure(struct context *ctx, 2101 const struct ast_expression *aexpr, 2102 struct expression *expr, 2103 const struct type *hint) 2104 { 2105 expr->type = EXPR_MEASURE; 2106 expr->result = &builtin_type_size; 2107 expr->measure.op = aexpr->measure.op; 2108 2109 switch (expr->measure.op) { 2110 case M_LEN: 2111 expr->measure.value = xcalloc(1, sizeof(struct expression)); 2112 check_expression(ctx, aexpr->measure.value, expr->measure.value, NULL); 2113 const struct type *atype = 2114 type_dereference(expr->measure.value->result); 2115 if (!atype) { 2116 error(ctx, aexpr->access.array->loc, expr, 2117 "Cannot dereference nullable pointer for len"); 2118 return; 2119 } 2120 enum type_storage vstor = type_dealias(atype)->storage; 2121 bool valid = vstor == STORAGE_ARRAY || vstor == STORAGE_SLICE 2122 || vstor == STORAGE_STRING; 2123 if (!valid) { 2124 char *typename = gen_typename(expr->measure.value->result); 2125 error(ctx, aexpr->measure.value->loc, expr, 2126 "len argument must be of an array, slice, or str type, but got %s", 2127 typename); 2128 free(typename); 2129 return; 2130 } 2131 if (atype->size == SIZE_UNDEFINED) { 2132 error(ctx, aexpr->measure.value->loc, expr, 2133 "Cannot take length of array type with undefined length"); 2134 return; 2135 } 2136 break; 2137 case M_SIZE: 2138 expr->measure.dimensions = type_store_lookup_dimensions( 2139 ctx->store, aexpr->measure.type); 2140 if (expr->measure.dimensions.size == SIZE_UNDEFINED) { 2141 error(ctx, aexpr->measure.value->loc, expr, 2142 "Cannot take size of a type with undefined size"); 2143 return; 2144 } 2145 break; 2146 case M_OFFSET: 2147 if (aexpr->measure.value->type != EXPR_ACCESS) { 2148 error(ctx, aexpr->measure.value->loc, expr, 2149 "offset argument must be a field or tuple access"); 2150 return; 2151 } 2152 if (aexpr->measure.value->access.type != ACCESS_FIELD 2153 && aexpr->measure.value->access.type != ACCESS_TUPLE) { 2154 error(ctx, aexpr->measure.value->loc, expr, 2155 "offset argument must be a field or tuple access"); 2156 return; 2157 } 2158 expr->measure.value = xcalloc(1, sizeof(struct expression)); 2159 check_expression(ctx, aexpr->measure.value, 2160 expr->measure.value, NULL); 2161 break; 2162 } 2163 } 2164 2165 static void 2166 check_expr_propagate(struct context *ctx, 2167 const struct ast_expression *aexpr, 2168 struct expression *expr, 2169 const struct type *hint) 2170 { 2171 struct expression *lvalue = xcalloc(1, sizeof(struct expression)); 2172 check_expression(ctx, aexpr->propagate.value, lvalue, hint == &builtin_type_void ? NULL : hint); 2173 2174 const struct type *intype = lvalue->result; 2175 if (type_dealias(intype)->storage != STORAGE_TAGGED) { 2176 char *typename = gen_typename(intype); 2177 error(ctx, aexpr->loc, expr, 2178 "Cannot use error propagation on non-tagged type %s", 2179 typename); 2180 free(typename); 2181 return; 2182 } 2183 if (!aexpr->propagate.abort) { 2184 if (ctx->deferring) { 2185 error(ctx, aexpr->loc, expr, 2186 "Cannot use error propagation in a defer expression"); 2187 return; 2188 } 2189 if (ctx->fntype->func.flags & FN_NORETURN) { 2190 error(ctx, aexpr->loc, expr, 2191 "Cannot use error propagation inside @noreturn function"); 2192 return; 2193 } 2194 } 2195 2196 struct type_tagged_union result_tagged = {0}; 2197 struct type_tagged_union *tagged = &result_tagged, 2198 **next_tag = &tagged->next; 2199 2200 struct type_tagged_union return_tagged = {0}; 2201 struct type_tagged_union *rtagged = &return_tagged, 2202 **next_rtag = &rtagged->next; 2203 2204 const struct type_tagged_union *intu = &type_dealias(intype)->tagged; 2205 for (; intu; intu = intu->next) { 2206 if (intu->type->flags & TYPE_ERROR) { 2207 if (rtagged->type) { 2208 rtagged = *next_rtag = 2209 xcalloc(1, sizeof(struct type_tagged_union)); 2210 next_rtag = &rtagged->next; 2211 rtagged->type = intu->type; 2212 } else { 2213 rtagged->type = intu->type; 2214 } 2215 } else { 2216 if (tagged->type) { 2217 tagged = *next_tag = 2218 xcalloc(1, sizeof(struct type_tagged_union)); 2219 next_tag = &tagged->next; 2220 tagged->type = intu->type; 2221 } else { 2222 tagged->type = intu->type; 2223 } 2224 } 2225 } 2226 2227 if (!return_tagged.type) { 2228 error(ctx, aexpr->loc, expr, 2229 "No error can occur here, cannot propagate"); 2230 return; 2231 } 2232 2233 const struct type *return_type; 2234 if (return_tagged.next) { 2235 return_type = type_store_lookup_tagged( 2236 ctx->store, &return_tagged); 2237 } else { 2238 return_type = return_tagged.type; 2239 } 2240 2241 const struct type *result_type; 2242 if (!result_tagged.type) { 2243 result_type = &builtin_type_void; 2244 } else if (result_tagged.next) { 2245 result_type = type_store_lookup_tagged( 2246 ctx->store, &result_tagged); 2247 } else { 2248 result_type = result_tagged.type; 2249 } 2250 2251 // Lower to a match expression 2252 expr->type = EXPR_MATCH; 2253 expr->match.value = lvalue; 2254 2255 struct scope *scope = scope_push(&ctx->scope, SCOPE_MATCH); 2256 struct match_case *case_ok = xcalloc(1, sizeof(struct match_case)); 2257 struct match_case *case_err = xcalloc(1, sizeof(struct match_case)); 2258 struct identifier ok_name = {0}, err_name = {0}; 2259 2260 int n = snprintf(NULL, 0, "ok.%d", ctx->id); 2261 ok_name.name = xcalloc(n + 1, 1); 2262 snprintf(ok_name.name, n + 1, "ok.%d", ctx->id); 2263 ++ctx->id; 2264 const struct scope_object *ok_obj = NULL; 2265 if (result_type->size != 0 && result_type->size != SIZE_UNDEFINED) { 2266 ok_obj = scope_insert(scope, O_BIND, &ok_name, 2267 &ok_name, result_type, NULL); 2268 } 2269 2270 n = snprintf(NULL, 0, "err.%d", ctx->id); 2271 err_name.name = xcalloc(n + 1, 1); 2272 snprintf(err_name.name, n + 1, "err.%d", ctx->id); 2273 ++ctx->id; 2274 const struct scope_object *err_obj = NULL; 2275 if (return_type->size != 0 && return_type->size != SIZE_UNDEFINED) { 2276 err_obj = scope_insert(scope, O_BIND, &err_name, 2277 &err_name, return_type, NULL); 2278 } 2279 2280 case_ok->type = result_type; 2281 case_ok->object = ok_obj; 2282 case_ok->value = xcalloc(1, sizeof(struct expression)); 2283 case_ok->value->result = result_type; 2284 if (ok_obj) { 2285 case_ok->value->type = EXPR_ACCESS; 2286 case_ok->value->access.type = ACCESS_IDENTIFIER; 2287 case_ok->value->access.object = ok_obj; 2288 } else { 2289 case_ok->value->type = EXPR_CONSTANT; 2290 } 2291 2292 case_err->type = return_type; 2293 case_err->object = err_obj; 2294 case_err->value = xcalloc(1, sizeof(struct expression)); 2295 2296 if (aexpr->propagate.abort) { 2297 case_err->value->type = EXPR_ASSERT; 2298 case_err->value->assert.cond = NULL; 2299 case_err->value->assert.is_static = false; 2300 2301 int n = snprintf(NULL, 0, "Assertion failed: error occured at %s:%d:%d", 2302 sources[aexpr->loc.file], 2303 aexpr->loc.lineno, aexpr->loc.colno); 2304 char *s = xcalloc(1, n + 1); 2305 snprintf(s, n, "Assertion failed: error occured at %s:%d:%d", 2306 sources[aexpr->loc.file], 2307 aexpr->loc.lineno, aexpr->loc.colno); 2308 2309 case_err->value->assert.message = xcalloc(1, sizeof(struct expression)); 2310 case_err->value->assert.message->type = EXPR_CONSTANT; 2311 case_err->value->assert.message->result = &builtin_type_const_str; 2312 case_err->value->assert.message->constant.string.value = s; 2313 case_err->value->assert.message->constant.string.len = n - 1; 2314 } else { 2315 if (!type_is_assignable(ctx->fntype->func.result, return_type)) { 2316 error(ctx, aexpr->loc, expr, 2317 "Error type is not assignable to function result type"); 2318 return; 2319 } 2320 2321 case_err->value->type = EXPR_RETURN; 2322 2323 struct expression *rval = 2324 xcalloc(1, sizeof(struct expression)); 2325 rval->result = return_type; 2326 if (err_obj != NULL) { 2327 rval->type = EXPR_ACCESS; 2328 rval->access.type = ACCESS_IDENTIFIER; 2329 rval->access.object = err_obj; 2330 } else { 2331 rval->type = EXPR_CONSTANT; 2332 } 2333 case_err->value->_return.value = lower_implicit_cast( 2334 ctx->fntype->func.result, rval); 2335 } 2336 case_err->value->terminates = true; 2337 case_err->value->result = &builtin_type_void; 2338 2339 expr->match.cases = case_ok; 2340 case_ok->next = case_err; 2341 2342 scope_pop(&ctx->scope); 2343 expr->result = result_type; 2344 } 2345 2346 static void 2347 check_expr_return(struct context *ctx, 2348 const struct ast_expression *aexpr, 2349 struct expression *expr, 2350 const struct type *hint) 2351 { 2352 if (ctx->deferring) { 2353 error(ctx, aexpr->loc, expr, 2354 "Cannot return inside a defer expression"); 2355 return; 2356 } 2357 if (ctx->fntype == NULL) { 2358 error(ctx, aexpr->loc, expr, "Cannot return outside a function body"); 2359 return; 2360 } 2361 if (ctx->fntype->func.flags & FN_NORETURN) { 2362 error(ctx, aexpr->loc, expr, 2363 "Cannot return inside @noreturn function"); 2364 return; 2365 } 2366 2367 expr->type = EXPR_RETURN; 2368 expr->result = &builtin_type_void; 2369 expr->terminates = true; 2370 2371 struct expression *rval = xcalloc(1, sizeof(struct expression)); 2372 if (aexpr->_return.value) { 2373 check_expression(ctx, aexpr->_return.value, rval, ctx->fntype->func.result); 2374 } else { 2375 rval->type = EXPR_CONSTANT; 2376 rval->result = &builtin_type_void; 2377 } 2378 2379 if (!type_is_assignable(ctx->fntype->func.result, rval->result)) { 2380 char *rettypename = gen_typename(rval->result); 2381 char *fntypename = gen_typename(ctx->fntype->func.result); 2382 error(ctx, aexpr->loc, expr, 2383 "Return value %s is not assignable to function result type %s", 2384 rettypename, fntypename); 2385 free(rettypename); 2386 free(fntypename); 2387 return; 2388 } 2389 if (ctx->fntype->func.result != rval->result) { 2390 rval = lower_implicit_cast( 2391 ctx->fntype->func.result, rval); 2392 } 2393 expr->_return.value = rval; 2394 } 2395 2396 static void 2397 check_expr_slice(struct context *ctx, 2398 const struct ast_expression *aexpr, 2399 struct expression *expr, 2400 const struct type *hint) 2401 { 2402 expr->type = EXPR_SLICE; 2403 2404 expr->slice.object = xcalloc(1, sizeof(struct expression)); 2405 check_expression(ctx, aexpr->slice.object, expr->slice.object, NULL); 2406 const struct type *atype = 2407 type_dereference(expr->slice.object->result); 2408 if (!atype) { 2409 error(ctx, aexpr->slice.object->loc, expr, 2410 "Cannot dereference nullable pointer for slicing"); 2411 return; 2412 } 2413 const struct type *dtype = type_dealias(atype); 2414 if (dtype->storage != STORAGE_SLICE 2415 && dtype->storage != STORAGE_ARRAY) { 2416 error(ctx, aexpr->slice.object->loc, expr, 2417 "Cannot slice non-array, non-slice object"); 2418 return; 2419 } 2420 2421 const struct type *itype; 2422 if (aexpr->slice.start) { 2423 expr->slice.start = xcalloc(1, sizeof(struct expression)); 2424 check_expression(ctx, aexpr->slice.start, expr->slice.start, &builtin_type_size); 2425 itype = type_dealias(expr->slice.start->result); 2426 if (!type_is_integer(itype)) { 2427 error(ctx, aexpr->slice.start->loc, expr, 2428 "Cannot use non-integer %s type as slicing operand", 2429 type_storage_unparse(itype->storage)); 2430 return; 2431 } 2432 expr->slice.start = lower_implicit_cast( 2433 &builtin_type_size, expr->slice.start); 2434 } 2435 2436 if (aexpr->slice.end) { 2437 expr->slice.end = xcalloc(1, sizeof(struct expression)); 2438 check_expression(ctx, aexpr->slice.end, expr->slice.end, &builtin_type_size); 2439 itype = type_dealias(expr->slice.end->result); 2440 if (!type_is_integer(itype)) { 2441 error(ctx, aexpr->slice.end->loc, expr, 2442 "Cannot use non-integer %s type as slicing operand", 2443 type_storage_unparse(itype->storage)); 2444 return; 2445 } 2446 expr->slice.end = lower_implicit_cast( 2447 &builtin_type_size, expr->slice.end); 2448 } else if (dtype->storage == STORAGE_ARRAY 2449 && dtype->array.length == SIZE_UNDEFINED) { 2450 error(ctx, aexpr->loc, expr, 2451 "Must have end index on array of undefined length"); 2452 return; 2453 } 2454 2455 if (dtype->storage == STORAGE_SLICE) { 2456 expr->result = atype; 2457 } else { 2458 expr->result = type_store_lookup_slice(ctx->store, 2459 dtype->array.members); 2460 } 2461 } 2462 2463 static void 2464 check_struct_exhaustive(struct context *ctx, 2465 const struct ast_expression *aexpr, 2466 struct expression *expr, 2467 const struct type *stype) 2468 { 2469 stype = type_dealias(stype); 2470 if (stype->storage == STORAGE_UNION) { 2471 return; 2472 } 2473 assert(stype->storage == STORAGE_STRUCT); 2474 struct struct_field *sf = stype->struct_union.fields; 2475 struct ast_field_value *af = aexpr->_struct.fields; 2476 2477 // XXX: O(n^2)? 2478 while (sf) { 2479 bool found = false; 2480 for (struct ast_field_value *f = af; f; 2481 f = f->next) { 2482 if (!sf->name) { 2483 check_struct_exhaustive(ctx, aexpr, expr, 2484 sf->type); 2485 found = true; 2486 continue; 2487 } 2488 if (strcmp(f->name, sf->name) == 0) { 2489 found = true; 2490 break; 2491 } 2492 } 2493 2494 if (!found) { 2495 error(ctx, aexpr->loc, expr, 2496 "Field '%s' is uninitialized", 2497 sf->name); 2498 } 2499 2500 sf = sf->next; 2501 } 2502 } 2503 2504 static void 2505 check_expr_struct(struct context *ctx, 2506 const struct ast_expression *aexpr, 2507 struct expression *expr, 2508 const struct type *hint) 2509 { 2510 expr->type = EXPR_STRUCT; 2511 2512 const struct type *stype = NULL; 2513 if (aexpr->_struct.type.name) { 2514 const struct scope_object *obj = scope_lookup(ctx->scope, 2515 &aexpr->_struct.type); 2516 if (!obj) { 2517 error(ctx, aexpr->loc, expr, 2518 "Unknown type alias"); 2519 return; 2520 } 2521 2522 // Do we want globals without type hints? 2523 assert(obj->otype != O_SCAN); 2524 2525 if (obj->otype != O_TYPE) { 2526 error(ctx, aexpr->loc, expr, 2527 "Name does not refer to a type"); 2528 return; 2529 } 2530 stype = obj->type; 2531 enum type_storage storage = type_dealias(stype)->storage; 2532 if (storage != STORAGE_STRUCT && storage != STORAGE_UNION) { 2533 error(ctx, aexpr->loc, expr, 2534 "Object named is not a struct or union type"); 2535 return; 2536 } 2537 } 2538 2539 struct ast_type satype = { 2540 .storage = STORAGE_STRUCT, 2541 .flags = TYPE_CONST, 2542 }; 2543 struct ast_struct_union_type *tfield = &satype.struct_union; 2544 struct ast_struct_union_type **tnext = &tfield->next; 2545 struct expr_struct_field *sexpr, **snext = &expr->_struct.fields; 2546 expr->_struct.autofill = aexpr->_struct.autofill; 2547 if (stype == NULL && expr->_struct.autofill) { 2548 error(ctx, aexpr->loc, expr, 2549 "Autofill is only permitted for named struct initializers"); 2550 return; 2551 } 2552 2553 struct ast_field_value *afield = aexpr->_struct.fields; 2554 while (afield) { 2555 const struct type *ftype; 2556 *snext = sexpr = xcalloc(1, sizeof(struct expr_struct_field)); 2557 snext = &sexpr->next; 2558 sexpr->value = xcalloc(1, sizeof(struct expression)); 2559 if (!stype) { 2560 assert(afield->name); // TODO 2561 if (!afield->type) { 2562 error(ctx, aexpr->loc, expr, 2563 "Unnamed struct must specify field type"); 2564 return; 2565 } 2566 tfield->name = afield->name; 2567 tfield->type = afield->type; 2568 ftype = type_store_lookup_atype(ctx->store, tfield->type); 2569 check_expression(ctx, afield->initializer, 2570 sexpr->value, ftype); 2571 if (afield->next) { 2572 *tnext = tfield = xcalloc( 2573 1, sizeof(struct ast_struct_union_type)); 2574 tnext = &tfield->next; 2575 } 2576 } else { 2577 if (!afield->name) { 2578 error(ctx, afield->initializer->loc, expr, 2579 "Cannot embed a struct literal into " 2580 "a named struct literal"); 2581 return; 2582 } 2583 sexpr->field = type_get_field(type_dealias(stype), 2584 afield->name); 2585 if (!sexpr->field) { 2586 error(ctx, afield->initializer->loc, expr, 2587 "No field by this name exists for this type"); 2588 return; 2589 } 2590 ftype = sexpr->field->type; 2591 check_expression(ctx, afield->initializer, 2592 sexpr->value, ftype); 2593 2594 if (!type_is_assignable(sexpr->field->type, sexpr->value->result)) { 2595 error(ctx, afield->initializer->loc, expr, 2596 "Initializer is not assignable to struct field"); 2597 return; 2598 } 2599 sexpr->value = lower_implicit_cast( 2600 sexpr->field->type, sexpr->value); 2601 } 2602 2603 afield = afield->next; 2604 } 2605 2606 if (stype) { 2607 if (!expr->_struct.autofill) { 2608 check_struct_exhaustive(ctx, aexpr, expr, stype); 2609 } 2610 expr->result = stype; 2611 } else { 2612 expr->result = type_store_lookup_atype(ctx->store, &satype); 2613 2614 tfield = &satype.struct_union; 2615 sexpr = expr->_struct.fields; 2616 while (tfield) { 2617 const struct struct_field *field = type_get_field( 2618 expr->result, tfield->name); 2619 if (!field) { 2620 // TODO: Use more specific error location 2621 error(ctx, aexpr->loc, expr, 2622 "No field by this name exists for this type"); 2623 return; 2624 } 2625 if (!type_is_assignable(field->type, sexpr->value->result)) { 2626 error(ctx, aexpr->loc, expr, 2627 "Cannot initialize struct field '%s' from value of this type", 2628 field->name); 2629 return; 2630 } 2631 sexpr->field = field; 2632 sexpr->value = lower_implicit_cast(field->type, sexpr->value); 2633 2634 struct ast_struct_union_type *next = tfield->next; 2635 if (tfield != &satype.struct_union) { 2636 free(tfield); 2637 } 2638 tfield = next; 2639 sexpr = sexpr->next; 2640 } 2641 } 2642 } 2643 2644 static void 2645 check_expr_switch(struct context *ctx, 2646 const struct ast_expression *aexpr, 2647 struct expression *expr, 2648 const struct type *hint) 2649 { 2650 expr->type = EXPR_SWITCH; 2651 2652 struct expression *value = xcalloc(1, sizeof(struct expression)); 2653 check_expression(ctx, aexpr->_switch.value, value, NULL); 2654 const struct type *type = type_dealias(value->result); 2655 expr->_switch.value = value; 2656 if (!type_is_numeric(type) 2657 && type_dealias(type)->storage != STORAGE_POINTER 2658 && type_dealias(type)->storage != STORAGE_STRING 2659 && type_dealias(type)->storage != STORAGE_BOOL 2660 && type_dealias(type)->storage != STORAGE_RCONST 2661 && type_dealias(type)->storage != STORAGE_RUNE) { 2662 error(ctx, aexpr->loc, expr, 2663 "Cannot switch on %s type", 2664 type_storage_unparse(type_dealias(type)->storage)); 2665 } 2666 2667 struct type_tagged_union result_type = {0}; 2668 struct type_tagged_union *tagged = &result_type, 2669 **next_tag = &tagged->next; 2670 2671 // TODO: Test for dupes, exhaustiveness 2672 struct switch_case **next = &expr->_switch.cases, *_case = NULL; 2673 for (struct ast_switch_case *acase = aexpr->_switch.cases; 2674 acase; acase = acase->next) { 2675 _case = *next = xcalloc(1, sizeof(struct switch_case)); 2676 next = &_case->next; 2677 2678 struct case_option *opt, **next_opt = &_case->options; 2679 for (struct ast_case_option *aopt = acase->options; 2680 aopt; aopt = aopt->next) { 2681 opt = *next_opt = xcalloc(1, sizeof(struct case_option)); 2682 struct expression *value = 2683 xcalloc(1, sizeof(struct expression)); 2684 struct expression *evaled = 2685 xcalloc(1, sizeof(struct expression)); 2686 2687 check_expression(ctx, aopt->value, value, type); 2688 if (!type_is_assignable(type_dealias(type), 2689 type_dealias(value->result))) { 2690 error(ctx, aopt->value->loc, expr, 2691 "Invalid type for switch case"); 2692 return; 2693 } 2694 2695 enum eval_result r = eval_expr(ctx, value, evaled); 2696 if (r != EVAL_OK) { 2697 error(ctx, aopt->value->loc, expr, 2698 "Unable to evaluate case at compile time"); 2699 return; 2700 } 2701 2702 opt->value = evaled; 2703 next_opt = &opt->next; 2704 } 2705 2706 _case->value = xcalloc(1, sizeof(struct expression)); 2707 2708 // Lower to compound 2709 // TODO: This should probably be done in a more first-class way 2710 struct ast_expression compound = { 2711 .type = EXPR_COMPOUND, 2712 .compound = { 2713 .list = acase->exprs, 2714 }, 2715 }; 2716 check_expression(ctx, &compound, _case->value, hint); 2717 2718 if (_case->value->terminates) { 2719 continue; 2720 } 2721 2722 if (expr->result == NULL) { 2723 expr->result = _case->value->result; 2724 tagged->type = expr->result; 2725 } else if (expr->result != _case->value->result) { 2726 tagged = *next_tag = 2727 xcalloc(1, sizeof(struct type_tagged_union)); 2728 next_tag = &tagged->next; 2729 tagged->type = _case->value->result; 2730 } 2731 } 2732 2733 if (expr->result == NULL) { 2734 expr->result = &builtin_type_void; 2735 expr->terminates = true; 2736 } 2737 2738 if (result_type.next) { 2739 if (hint) { 2740 expr->result = hint; 2741 } else { 2742 expr->result = type_store_reduce_result( 2743 ctx->store, &result_type); 2744 if (expr->result == NULL) { 2745 error(ctx, aexpr->loc, expr, 2746 "Invalid result type (dangling or ambiguous null)"); 2747 return; 2748 } 2749 } 2750 2751 struct switch_case *_case = expr->_switch.cases; 2752 struct ast_switch_case *acase = aexpr->_switch.cases; 2753 while (_case) { 2754 if (!_case->value->terminates && !type_is_assignable( 2755 expr->result, _case->value->result)) { 2756 error(ctx, acase->exprs.expr->loc, expr, 2757 "Switch case is not assignable to result type"); 2758 return; 2759 } 2760 _case->value = lower_implicit_cast( 2761 expr->result, _case->value); 2762 _case = _case->next; 2763 acase = acase->next; 2764 } 2765 2766 struct type_tagged_union *tu = result_type.next; 2767 while (tu) { 2768 struct type_tagged_union *next = tu->next; 2769 free(tu); 2770 tu = next; 2771 } 2772 } 2773 } 2774 2775 static void 2776 check_expr_tuple(struct context *ctx, 2777 const struct ast_expression *aexpr, 2778 struct expression *expr, 2779 const struct type *hint) 2780 { 2781 expr->type = EXPR_TUPLE; 2782 2783 const struct type_tuple *ttuple = NULL; 2784 if (hint && type_dealias(hint)->storage == STORAGE_TUPLE) { 2785 ttuple = &type_dealias(hint)->tuple; 2786 } 2787 2788 struct type_tuple result = {0}; 2789 struct type_tuple *rtype = &result; 2790 2791 struct expression_tuple *tuple = &expr->tuple; 2792 for (const struct ast_expression_tuple *atuple = &aexpr->tuple; 2793 atuple; atuple = atuple->next) { 2794 tuple->value = xcalloc(1, sizeof(struct expression)); 2795 check_expression(ctx, atuple->expr, tuple->value, ttuple ? ttuple->type : NULL); 2796 rtype->type = tuple->value->result; 2797 2798 if (atuple->next) { 2799 rtype->next = xcalloc(1, sizeof(struct type_tuple)); 2800 rtype = rtype->next; 2801 tuple->next = xcalloc(1, sizeof(struct expression_tuple)); 2802 tuple = tuple->next; 2803 } 2804 2805 if (ttuple) { 2806 ttuple = ttuple->next; 2807 } 2808 } 2809 2810 if (hint && type_dealias(hint)->storage == STORAGE_TUPLE) { 2811 expr->result = hint; 2812 } else if (hint && type_dealias(hint)->storage == STORAGE_TAGGED) { 2813 for (const struct type_tagged_union *tu = 2814 &type_dealias(hint)->tagged; 2815 tu; tu = tu->next) { 2816 if (type_dealias(tu->type)->storage != STORAGE_TUPLE) { 2817 continue; 2818 } 2819 const struct type_tuple *ttuple = 2820 &type_dealias(tu->type)->tuple; 2821 const struct expression_tuple *etuple = &expr->tuple; 2822 bool valid = true; 2823 while (etuple) { 2824 if (!ttuple || !type_is_assignable(ttuple->type, 2825 etuple->value->result)) { 2826 valid = false; 2827 break; 2828 } 2829 ttuple = ttuple->next; 2830 etuple = etuple->next; 2831 } 2832 if (!ttuple && valid) { 2833 expr->result = type_dealias(tu->type); 2834 break; 2835 } 2836 } 2837 if (!expr->result) { 2838 error(ctx, aexpr->loc, expr, 2839 "Tuple value is not assignable to tagged union hint"); 2840 return; 2841 } 2842 } else { 2843 expr->result = type_store_lookup_tuple(ctx->store, &result, 2844 aexpr->loc); 2845 } 2846 2847 ttuple = &type_dealias(expr->result)->tuple; 2848 struct expression_tuple *etuple = &expr->tuple; 2849 const struct ast_expression_tuple *atuple = &aexpr->tuple; 2850 while (etuple) { 2851 if (!ttuple) { 2852 error(ctx, atuple->expr->loc, expr, 2853 "Too many values for tuple type"); 2854 return; 2855 } 2856 if (!type_is_assignable(ttuple->type, etuple->value->result)) { 2857 error(ctx, atuple->expr->loc, expr, 2858 "Value is not assignable to tuple value type"); 2859 return; 2860 } 2861 etuple->value = lower_implicit_cast(ttuple->type, etuple->value); 2862 etuple = etuple->next; 2863 atuple = atuple->next; 2864 ttuple = ttuple->next; 2865 } 2866 if (ttuple) { 2867 error(ctx, aexpr->loc, expr, 2868 "Too few values for tuple type"); 2869 return; 2870 } 2871 } 2872 2873 static void 2874 check_expr_unarithm(struct context *ctx, 2875 const struct ast_expression *aexpr, 2876 struct expression *expr, 2877 const struct type *hint) 2878 { 2879 expr->type = EXPR_UNARITHM; 2880 2881 struct expression *operand = xcalloc(1, sizeof(struct expression)); 2882 check_expression(ctx, aexpr->unarithm.operand, operand, NULL); 2883 expr->unarithm.operand = operand; 2884 expr->unarithm.op = aexpr->unarithm.op; 2885 2886 switch (expr->unarithm.op) { 2887 case UN_LNOT: 2888 if (type_dealias(operand->result)->storage != STORAGE_BOOL) { 2889 error(ctx, aexpr->unarithm.operand->loc, expr, 2890 "Cannot perform logical NOT (!) on non-boolean type"); 2891 return; 2892 } 2893 expr->result = &builtin_type_bool; 2894 break; 2895 case UN_BNOT: 2896 if (!type_is_integer(operand->result)) { 2897 error(ctx, aexpr->unarithm.operand->loc, expr, 2898 "Cannot perform binary NOT (~) on non-integer type"); 2899 return; 2900 } 2901 expr->result = operand->result; 2902 break; 2903 case UN_MINUS: 2904 if (operand->result->storage == STORAGE_ICONST) { 2905 // Not technically quite right, but we need 2906 // operand->result to be lowered with expr->result, and 2907 // this is correct enough 2908 const struct type *old = operand->result; 2909 const struct type *new = type_create_const( 2910 STORAGE_ICONST, -old->_const.min, 2911 -old->_const.max); 2912 lower_const(old, new); 2913 } 2914 // Fallthrough 2915 case UN_PLUS: 2916 if (!type_is_numeric(operand->result)) { 2917 error(ctx, aexpr->unarithm.operand->loc, expr, 2918 "Cannot perform operation on non-numeric type"); 2919 return; 2920 } 2921 expr->result = operand->result; 2922 break; 2923 case UN_ADDRESS: 2924 expr->result = type_store_lookup_pointer( 2925 ctx->store, operand->result, 0); 2926 break; 2927 case UN_DEREF: 2928 if (type_dealias(operand->result)->storage != STORAGE_POINTER) { 2929 error(ctx, aexpr->unarithm.operand->loc, expr, 2930 "Cannot de-reference non-pointer type"); 2931 return; 2932 } 2933 if (type_dealias(operand->result)->pointer.flags 2934 & PTR_NULLABLE) { 2935 error(ctx, aexpr->unarithm.operand->loc, expr, 2936 "Cannot dereference nullable pointer type"); 2937 return; 2938 } 2939 expr->result = type_dealias(operand->result)->pointer.referent; 2940 break; 2941 } 2942 } 2943 2944 static void 2945 check_expr_vastart(struct context *ctx, 2946 const struct ast_expression *aexpr, 2947 struct expression *expr, 2948 const struct type *hint) 2949 { 2950 if (ctx->fntype->func.variadism != VARIADISM_C) { 2951 error(ctx, aexpr->loc, expr, 2952 "Cannot use vastart within function which does not use C-style variadism"); 2953 return; 2954 } 2955 expr->type = EXPR_VASTART; 2956 expr->result = &builtin_type_valist; 2957 } 2958 2959 static void 2960 check_expr_vaarg(struct context *ctx, 2961 const struct ast_expression *aexpr, 2962 struct expression *expr, 2963 const struct type *hint) 2964 { 2965 expr->type = EXPR_VAARG; 2966 if (hint == NULL) { 2967 error(ctx, aexpr->loc, expr, 2968 "Cannot infer type of vaarg without hint"); 2969 return; 2970 } 2971 expr->vaarg.ap = xcalloc(1, sizeof(struct expression)); 2972 check_expression(ctx, aexpr->vaarg.ap, expr->vaarg.ap, &builtin_type_valist); 2973 if (type_dealias(expr->vaarg.ap->result)->storage != STORAGE_VALIST) { 2974 error(ctx, aexpr->loc, expr, 2975 "Expected vaarg operand to be valist"); 2976 return; 2977 } 2978 expr->result = hint; 2979 } 2980 2981 static void 2982 check_expr_vaend(struct context *ctx, 2983 const struct ast_expression *aexpr, 2984 struct expression *expr, 2985 const struct type *hint) 2986 { 2987 expr->type = EXPR_VAEND; 2988 expr->vaarg.ap = xcalloc(1, sizeof(struct expression)); 2989 check_expression(ctx, aexpr->vaarg.ap, expr->vaarg.ap, &builtin_type_valist); 2990 if (type_dealias(expr->vaarg.ap->result)->storage != STORAGE_VALIST) { 2991 error(ctx, aexpr->loc, expr, 2992 "Expected vaend operand to be valist"); 2993 return; 2994 } 2995 expr->result = &builtin_type_void; 2996 } 2997 2998 void 2999 check_expression(struct context *ctx, 3000 const struct ast_expression *aexpr, 3001 struct expression *expr, 3002 const struct type *hint) 3003 { 3004 expr->loc = aexpr->loc; 3005 3006 switch (aexpr->type) { 3007 case EXPR_ACCESS: 3008 check_expr_access(ctx, aexpr, expr, hint); 3009 break; 3010 case EXPR_ALLOC: 3011 check_expr_alloc(ctx, aexpr, expr, hint); 3012 break; 3013 case EXPR_APPEND: 3014 check_expr_append_insert(ctx, aexpr, expr, hint); 3015 break; 3016 case EXPR_ASSERT: 3017 check_expr_assert(ctx, aexpr, expr, hint); 3018 break; 3019 case EXPR_ASSIGN: 3020 check_expr_assign(ctx, aexpr, expr, hint); 3021 break; 3022 case EXPR_BINARITHM: 3023 check_expr_binarithm(ctx, aexpr, expr, hint); 3024 break; 3025 case EXPR_BINDING: 3026 check_expr_binding(ctx, aexpr, expr, hint); 3027 break; 3028 case EXPR_BREAK: 3029 case EXPR_CONTINUE: 3030 case EXPR_YIELD: 3031 check_expr_control(ctx, aexpr, expr, hint); 3032 break; 3033 case EXPR_CALL: 3034 check_expr_call(ctx, aexpr, expr, hint); 3035 break; 3036 case EXPR_CAST: 3037 check_expr_cast(ctx, aexpr, expr, hint); 3038 break; 3039 case EXPR_COMPOUND: 3040 check_expr_compound(ctx, aexpr, expr, hint); 3041 break; 3042 case EXPR_CONSTANT: 3043 check_expr_constant(ctx, aexpr, expr, hint); 3044 break; 3045 case EXPR_DEFER: 3046 check_expr_defer(ctx, aexpr, expr, hint); 3047 break; 3048 case EXPR_DELETE: 3049 check_expr_delete(ctx, aexpr, expr, hint); 3050 break; 3051 case EXPR_FOR: 3052 check_expr_for(ctx, aexpr, expr, hint); 3053 break; 3054 case EXPR_FREE: 3055 check_expr_free(ctx, aexpr, expr, hint); 3056 break; 3057 case EXPR_IF: 3058 check_expr_if(ctx, aexpr, expr, hint); 3059 break; 3060 case EXPR_INSERT: 3061 check_expr_append_insert(ctx, aexpr, expr, hint); 3062 break; 3063 case EXPR_MATCH: 3064 check_expr_match(ctx, aexpr, expr, hint); 3065 break; 3066 case EXPR_MEASURE: 3067 check_expr_measure(ctx, aexpr, expr, hint); 3068 break; 3069 case EXPR_PROPAGATE: 3070 check_expr_propagate(ctx, aexpr, expr, hint); 3071 break; 3072 case EXPR_RETURN: 3073 check_expr_return(ctx, aexpr, expr, hint); 3074 break; 3075 case EXPR_SLICE: 3076 check_expr_slice(ctx, aexpr, expr, hint); 3077 break; 3078 case EXPR_STRUCT: 3079 check_expr_struct(ctx, aexpr, expr, hint); 3080 break; 3081 case EXPR_SWITCH: 3082 check_expr_switch(ctx, aexpr, expr, hint); 3083 break; 3084 case EXPR_TUPLE: 3085 check_expr_tuple(ctx, aexpr, expr, hint); 3086 break; 3087 case EXPR_UNARITHM: 3088 check_expr_unarithm(ctx, aexpr, expr, hint); 3089 break; 3090 case EXPR_VAARG: 3091 check_expr_vaarg(ctx, aexpr, expr, hint); 3092 break; 3093 case EXPR_VAEND: 3094 check_expr_vaend(ctx, aexpr, expr, hint); 3095 break; 3096 case EXPR_VASTART: 3097 check_expr_vastart(ctx, aexpr, expr, hint); 3098 break; 3099 } 3100 assert(expr->result); 3101 const_refer(expr->result, &expr->result); 3102 if (hint && hint->storage == STORAGE_VOID) { 3103 if ((expr->result->flags & TYPE_ERROR) != 0) { 3104 error(ctx, aexpr->loc, expr, 3105 "Cannot ignore error here"); 3106 return; 3107 } 3108 if (type_dealias(expr->result)->storage != STORAGE_TAGGED) { 3109 return; 3110 } 3111 const struct type_tagged_union *tu = 3112 &type_dealias(expr->result)->tagged; 3113 for (; tu; tu = tu->next) { 3114 if ((tu->type->flags & TYPE_ERROR) == 0) { 3115 continue; 3116 } 3117 error(ctx, aexpr->loc, expr, 3118 "Cannot ignore error here"); 3119 return; 3120 } 3121 } 3122 } 3123 3124 static struct declaration * 3125 check_const(struct context *ctx, 3126 const struct ast_global_decl *adecl) 3127 { 3128 const struct type *type = type_store_lookup_atype( 3129 ctx->store, adecl->type); 3130 struct declaration *decl = xcalloc(1, sizeof(struct declaration)); 3131 const struct scope_object *obj = scope_lookup( 3132 ctx->unit, &adecl->ident); 3133 decl->type = DECL_CONST; 3134 decl->constant.type = type; 3135 decl->constant.value = obj->value; 3136 mkident(ctx, &decl->ident, &adecl->ident); 3137 return decl; 3138 } 3139 3140 static struct declaration * 3141 check_function(struct context *ctx, 3142 const struct ast_decl *adecl) 3143 { 3144 const struct ast_function_decl *afndecl = &adecl->function; 3145 if ((adecl->function.flags & FN_TEST) && !ctx->is_test) { 3146 return NULL; 3147 } 3148 3149 const struct ast_type fn_atype = { 3150 .storage = STORAGE_FUNCTION, 3151 .flags = TYPE_CONST, 3152 .func = afndecl->prototype, 3153 }; 3154 const struct type *fntype = type_store_lookup_atype( 3155 ctx->store, &fn_atype); 3156 ctx->fntype = fntype; 3157 3158 struct declaration *decl = xcalloc(1, sizeof(struct declaration)); 3159 decl->type = DECL_FUNC; 3160 decl->func.type = fntype; 3161 decl->func.flags = afndecl->flags; 3162 3163 if (afndecl->symbol) { 3164 decl->symbol = strdup(afndecl->symbol); 3165 } 3166 mkident(ctx, &decl->ident, &afndecl->ident); 3167 3168 if (!adecl->function.body) { 3169 return decl; // Prototype 3170 } 3171 3172 decl->func.scope = scope_push(&ctx->scope, SCOPE_FUNC); 3173 struct ast_function_parameters *params = afndecl->prototype.params; 3174 while (params) { 3175 expect(ctx, ¶ms->loc, params->name, 3176 "Function parameters must be named"); 3177 struct identifier ident = { 3178 .name = params->name, 3179 }; 3180 const struct type *type = type_store_lookup_atype( 3181 ctx->store, params->type); 3182 if (fntype->func.variadism == VARIADISM_HARE 3183 && !params->next) { 3184 type = type_store_lookup_slice(ctx->store, type); 3185 } 3186 scope_insert(decl->func.scope, O_BIND, 3187 &ident, &ident, type, NULL); 3188 params = params->next; 3189 } 3190 3191 struct expression *body = xcalloc(1, sizeof(struct expression)); 3192 check_expression(ctx, afndecl->body, body, fntype->func.result); 3193 // TODO: Pass errors up and deal with them at the end of check 3194 3195 char *restypename = gen_typename(body->result); 3196 char *fntypename = gen_typename(fntype->func.result); 3197 expect(ctx, &afndecl->body->loc, 3198 body->terminates || type_is_assignable(fntype->func.result, body->result), 3199 "Result value %s is not assignable to function result type %s", 3200 restypename, fntypename); 3201 free(restypename); 3202 free(fntypename); 3203 if (!body->terminates && fntype->func.result != body->result) { 3204 body = lower_implicit_cast(fntype->func.result, body); 3205 } 3206 decl->func.body = body; 3207 3208 // TODO: Add function name to errors 3209 if (decl->func.flags != 0) { 3210 const char *flag = NULL; 3211 switch (decl->func.flags) { 3212 case FN_INIT: 3213 flag = "@init"; 3214 break; 3215 case FN_FINI: 3216 flag = "@fini"; 3217 break; 3218 case FN_TEST: 3219 flag = "@test"; 3220 break; 3221 default: 3222 expect(ctx, &adecl->loc, 0, 3223 "Only one of @init, @fini, or @test may be used in a function declaration"); 3224 }; 3225 expect(ctx, &adecl->loc, fntype->func.result == &builtin_type_void, 3226 "%s function must return void", flag); 3227 expect(ctx, &adecl->loc, (fntype->func.flags & FN_NORETURN) == 0, 3228 "%s function must return", flag); 3229 expect(ctx, &adecl->loc, !decl->exported, 3230 "%s function cannot be exported", flag); 3231 expect(ctx, &adecl->loc, !afndecl->prototype.params, 3232 "%s function cannot have parameters", flag); 3233 } 3234 if (fntype->func.flags & FN_NORETURN) { 3235 expect(ctx, &adecl->loc, fntype->func.result == &builtin_type_void, 3236 "@noreturn function must return void"); 3237 }; 3238 3239 scope_pop(&ctx->scope); 3240 ctx->fntype = NULL; 3241 return decl; 3242 } 3243 3244 static struct declaration * 3245 check_global(struct context *ctx, 3246 const struct ast_global_decl *adecl) 3247 { 3248 const struct type *type = type_store_lookup_atype( 3249 ctx->store, adecl->type); 3250 3251 struct declaration *decl = xcalloc(1, sizeof(struct declaration)); 3252 decl->type = DECL_GLOBAL; 3253 decl->global.type = type; 3254 3255 if (adecl->symbol) { 3256 decl->symbol = strdup(adecl->symbol); 3257 } 3258 mkident(ctx, &decl->ident, &adecl->ident); 3259 3260 if (!adecl->init) { 3261 return decl; // Forward declaration 3262 } 3263 3264 // TODO: Free initialier 3265 struct expression *initializer = 3266 xcalloc(1, sizeof(struct expression)); 3267 check_expression(ctx, adecl->init, initializer, type); 3268 // TODO: Pass errors up and deal with them at the end of check 3269 3270 char *typename1 = gen_typename(initializer->result); 3271 char *typename2 = gen_typename(type); 3272 expect(ctx, &adecl->init->loc, 3273 type_is_assignable(type, initializer->result), 3274 "Initializer type %s is not assignable to constant type %s", 3275 typename1, typename2); 3276 free(typename1); 3277 free(typename2); 3278 3279 bool context = adecl->type 3280 && adecl->type->storage == STORAGE_ARRAY 3281 && adecl->type->array.contextual; 3282 if (context) { 3283 // XXX: Do we need to do anything more here 3284 type = initializer->result; 3285 } 3286 3287 initializer = lower_implicit_cast(type, initializer); 3288 3289 struct expression *value = 3290 xcalloc(1, sizeof(struct expression)); 3291 enum eval_result r = eval_expr(ctx, initializer, value); 3292 expect(ctx, &adecl->init->loc, r == EVAL_OK, 3293 "Unable to evaluate global initializer at compile time"); 3294 3295 decl->global.value = value; 3296 3297 return decl; 3298 } 3299 3300 static struct declaration * 3301 check_type(struct context *ctx, 3302 const struct ast_type_decl *adecl, 3303 bool exported) 3304 { 3305 struct declaration *decl = xcalloc(1, sizeof(struct declaration)); 3306 if (!adecl->ident.ns) { 3307 mkident(ctx, &decl->ident, &adecl->ident); 3308 } else { 3309 decl->ident = adecl->ident; 3310 } 3311 decl->type = DECL_TYPE; 3312 if (adecl->type->storage == STORAGE_ENUM) { 3313 decl->_type = 3314 type_store_lookup_enum(ctx->store, adecl->type, exported); 3315 } else { 3316 const struct type *type = 3317 type_store_lookup_atype(ctx->store, adecl->type); 3318 struct type _alias = { 3319 .storage = STORAGE_ALIAS, 3320 .alias = { 3321 .ident = decl->ident, 3322 .name = adecl->ident, 3323 .type = type, 3324 .exported = exported, 3325 }, 3326 .size = type->size, 3327 .align = type->align, 3328 .flags = type->flags, 3329 }; 3330 decl->_type = type_store_lookup_alias(ctx->store, &_alias); 3331 } 3332 return decl; 3333 } 3334 3335 static struct declarations ** 3336 check_declarations(struct context *ctx, 3337 const struct ast_decls *adecls, 3338 struct declarations **next) 3339 { 3340 while (adecls) { 3341 struct declaration *decl = NULL; 3342 const struct ast_decl *adecl = &adecls->decl; 3343 switch (adecl->decl_type) { 3344 case AST_DECL_CONST: 3345 for (const struct ast_global_decl *c = &adecl->constant; 3346 c; c = c->next) { 3347 decl = check_const(ctx, c); 3348 struct declarations *decls = *next = 3349 xcalloc(1, sizeof(struct declarations)); 3350 decl->exported = adecl->exported; 3351 decl->loc = adecl->loc; 3352 decls->decl = decl; 3353 next = &decls->next; 3354 } 3355 decl = NULL; 3356 break; 3357 case AST_DECL_FUNC: 3358 decl = check_function(ctx, adecl); 3359 break; 3360 case AST_DECL_GLOBAL: 3361 for (const struct ast_global_decl *g = &adecl->global; 3362 g; g = g->next) { 3363 decl = check_global(ctx, g); 3364 if (decl == NULL) { 3365 continue; 3366 } 3367 struct declarations *decls = *next = 3368 xcalloc(1, sizeof(struct declarations)); 3369 decl->exported = adecl->exported; 3370 decl->loc = adecl->loc; 3371 decls->decl = decl; 3372 next = &decls->next; 3373 } 3374 decl = NULL; 3375 break; 3376 case AST_DECL_TYPE: 3377 for (const struct ast_type_decl *t = &adecl->type; 3378 t; t = t->next) { 3379 decl = check_type(ctx, t, adecl->exported); 3380 struct declarations *decls = *next = 3381 xcalloc(1, sizeof(struct declarations)); 3382 decl->exported = adecl->exported; 3383 decls->decl = decl; 3384 next = &decls->next; 3385 } 3386 decl = NULL; 3387 break; 3388 } 3389 3390 if (decl) { 3391 struct declarations *decls = *next = 3392 xcalloc(1, sizeof(struct declarations)); 3393 decl->exported = adecl->exported; 3394 decl->loc = adecl->loc; 3395 decls->decl = decl; 3396 next = &decls->next; 3397 } 3398 3399 adecls = adecls->next; 3400 } 3401 return next; 3402 } 3403 3404 static struct incomplete_declaration * 3405 incomplete_declaration_create(struct context *ctx, struct location loc, 3406 struct scope *scope, const struct identifier *ident, 3407 const struct identifier *name) 3408 { 3409 struct scope *subunit = ctx->unit->parent; 3410 ctx->unit->parent = NULL; 3411 struct incomplete_declaration *idecl = 3412 (struct incomplete_declaration *)scope_lookup(scope, name); 3413 ctx->unit->parent = subunit; 3414 3415 if (idecl) { 3416 expect(ctx, &loc, NULL, "Duplicate global identifier '%s'", 3417 identifier_unparse(ident)); 3418 return idecl; 3419 } 3420 idecl = xcalloc(1, sizeof(struct incomplete_declaration)); 3421 3422 scope_object_init((struct scope_object *)idecl, O_SCAN, 3423 ident, name, NULL, NULL); 3424 scope_insert_from_object(scope, (struct scope_object *)idecl); 3425 return idecl; 3426 } 3427 3428 static void 3429 incomplete_enum_field_create(struct context *ctx, struct scope *imports, 3430 struct scope *enum_scope, const struct type *etype, 3431 struct ast_enum_field *f) 3432 { 3433 // We have to process the last field first 3434 // This way, objects in enum_scope will have lnext pointing to 3435 // the previous element, which is important for implicit enum values. 3436 if (f->next) { 3437 incomplete_enum_field_create(ctx, imports, enum_scope, 3438 etype, f->next); 3439 } 3440 assert(etype->storage == STORAGE_ENUM); 3441 struct incomplete_enum_field *field = 3442 xcalloc(1, sizeof(struct incomplete_enum_field)); 3443 *field = (struct incomplete_enum_field){ 3444 .field = f, 3445 .type = etype, 3446 .enum_scope = enum_scope, 3447 }; 3448 3449 struct identifier localname = { 3450 .name = strdup(f->name), 3451 }; 3452 struct identifier name_ns = { 3453 .name = etype->alias.name.name, 3454 .ns = etype->alias.name.ns, 3455 }; 3456 struct identifier name = { 3457 .name = strdup(f->name), 3458 .ns = &name_ns, 3459 }; 3460 struct incomplete_declaration *fld = 3461 incomplete_declaration_create(ctx, f->loc, enum_scope, 3462 &name, &localname); 3463 fld->type = IDECL_ENUM_FLD; 3464 fld->imports = imports; 3465 fld->field = field; 3466 3467 struct identifier _ident = {0}; 3468 mkident(ctx, &_ident, &etype->alias.name); 3469 struct identifier ident = { 3470 .name = strdup(f->name), 3471 .ns = &_ident, 3472 }; 3473 fld = incomplete_declaration_create(ctx, f->loc, ctx->scope, 3474 &ident, &name); 3475 fld->type = IDECL_ENUM_FLD, 3476 fld->imports = imports, 3477 fld->field = field; 3478 free(name.name); 3479 } 3480 3481 static void 3482 incomplete_types_create(struct context *ctx, struct scope *imp, struct ast_decl *decl) 3483 { 3484 for (struct ast_type_decl *t = &decl->type; t; t = t->next) { 3485 struct identifier with_ns = {0}; 3486 mkident(ctx, &with_ns, &t->ident); 3487 struct incomplete_declaration *idecl = 3488 incomplete_declaration_create(ctx, decl->loc, ctx->scope, 3489 &with_ns, &t->ident); 3490 idecl->decl = (struct ast_decl){ 3491 .decl_type = AST_DECL_TYPE, 3492 .loc = decl->loc, 3493 .type = *t, 3494 .exported = decl->exported, 3495 }; 3496 idecl->imports = imp; 3497 if (t->type->storage == STORAGE_ENUM) { 3498 bool exported = idecl->decl.exported; 3499 const struct type *type = type_store_lookup_enum( 3500 ctx->store, t->type, exported); 3501 if (type->storage == STORAGE_VOID) { 3502 return; // error occured 3503 } 3504 scope_push((struct scope **)&type->_enum.values, SCOPE_ENUM); 3505 incomplete_enum_field_create(ctx, imp, 3506 type->_enum.values, type, t->type->_enum.values); 3507 type->_enum.values->parent = ctx->unit; 3508 scope_insert(ctx->scope, O_TYPE, &with_ns, 3509 &t->ident, type, NULL); 3510 } else { 3511 idecl->type = IDECL_DECL; 3512 } 3513 } 3514 } 3515 3516 const struct scope_object * 3517 scan_const(struct context *ctx, const struct ast_global_decl *decl) 3518 { 3519 assert(!decl->symbol); // Invariant 3520 3521 const struct type *type = type_store_lookup_atype( 3522 ctx->store, decl->type); 3523 expect(ctx, &decl->type->loc, type != NULL, "Unable to resolve type"); 3524 // TODO: Free the initializer 3525 struct expression *initializer = xcalloc(1, sizeof(struct expression)); 3526 check_expression(ctx, decl->init, initializer, type); 3527 bool context = decl->type 3528 && decl->type->storage == STORAGE_ARRAY 3529 && decl->type->array.contextual; 3530 if (context) { 3531 // XXX: Do we need to do anything more here 3532 type = initializer->result; 3533 } 3534 3535 char *typename1 = gen_typename(initializer->result); 3536 char *typename2 = gen_typename(type); 3537 expect(ctx, &decl->init->loc, type_is_assignable(type, initializer->result), 3538 "Initializer type %s is not assignable to constant type %s", 3539 typename1, typename2); 3540 free(typename1); 3541 free(typename2); 3542 initializer = lower_implicit_cast(type, initializer); 3543 3544 struct expression *value = 3545 xcalloc(1, sizeof(struct expression)); 3546 enum eval_result r = eval_expr(ctx, initializer, value); 3547 expect(ctx, &decl->init->loc, r == EVAL_OK, 3548 "Unable to evaluate constant initializer at compile time"); 3549 expect(ctx, &decl->init->loc, type->storage != STORAGE_NULL, 3550 "Null is not a valid type for a constant"); 3551 3552 struct identifier ident = {0}; 3553 mkident(ctx, &ident, &decl->ident); 3554 return scope_insert(ctx->unit, O_CONST, &ident, 3555 &decl->ident, type, value); 3556 } 3557 3558 const struct scope_object * 3559 scan_function(struct context *ctx, const struct ast_function_decl *decl) 3560 { 3561 const struct ast_type fn_atype = { 3562 .storage = STORAGE_FUNCTION, 3563 .flags = TYPE_CONST, 3564 .func = decl->prototype, 3565 }; 3566 const struct type *fntype = type_store_lookup_atype( 3567 ctx->store, &fn_atype); 3568 3569 if (!decl->flags) { 3570 struct identifier ident = {0}; 3571 if (decl->symbol) { 3572 ident.name = strdup(decl->symbol); 3573 } else if (!decl->ident.ns) { 3574 mkident(ctx, &ident, &decl->ident); 3575 } else { 3576 ident = decl->ident; 3577 } 3578 return scope_insert(ctx->unit, O_DECL, &ident, 3579 &decl->ident, fntype, NULL); 3580 } 3581 return NULL; 3582 } 3583 3584 const struct scope_object * 3585 scan_global(struct context *ctx, const struct ast_global_decl *decl) 3586 { 3587 const struct type *type = type_store_lookup_atype( 3588 ctx->store, decl->type); 3589 3590 if (decl->type->storage == STORAGE_ARRAY 3591 && decl->type->array.contextual) { 3592 expect(ctx, &decl->type->loc, decl->init, 3593 "Cannot infer array length without an initializer"); 3594 3595 // TODO: Free initialier 3596 struct expression *initializer = 3597 xcalloc(1, sizeof(struct expression)); 3598 check_expression(ctx, decl->init, initializer, type); 3599 expect(ctx, &decl->init->loc, 3600 initializer->result->storage == STORAGE_ARRAY, 3601 "Cannot infer array length from non-array type"); 3602 expect(ctx, &decl->init->loc, 3603 initializer->result->array.members == type->array.members, 3604 "Initializer is not assignable to binding type"); 3605 type = initializer->result; 3606 } 3607 3608 expect(ctx, &decl->init->loc, type->storage != STORAGE_NULL, 3609 "Null is not a valid type for a global"); 3610 3611 struct identifier ident = {0}; 3612 if (decl->symbol) { 3613 ident.name = strdup(decl->symbol); 3614 } else { 3615 mkident(ctx, &ident, &decl->ident); 3616 } 3617 return scope_insert(ctx->unit, O_DECL, &ident, &decl->ident, type, NULL); 3618 } 3619 3620 const struct scope_object * 3621 resolve_enum_field(struct context *ctx, const struct scope_object *obj) 3622 { 3623 assert(obj->otype == O_SCAN); 3624 struct incomplete_declaration *idecl = (struct incomplete_declaration*)obj; 3625 assert(idecl->type == IDECL_ENUM_FLD); 3626 3627 const struct type *type = idecl->field->type; 3628 3629 struct identifier nsident, nsname, localname = { 3630 .name = idecl->obj.ident.name 3631 }; 3632 identifier_dup(&nsident, &type->alias.ident); 3633 identifier_dup(&nsname, &type->alias.name); 3634 struct identifier ident = { 3635 .name = localname.name, 3636 .ns = &nsident 3637 }; 3638 struct identifier name = { 3639 .name = localname.name, 3640 .ns = &nsname 3641 }; 3642 3643 const struct scope_object *new = 3644 scope_lookup(idecl->field->enum_scope, &localname); 3645 if (new != obj) { 3646 if (new->otype == O_SCAN) { 3647 new = wrap_resolver(ctx, new, resolve_enum_field); 3648 } 3649 assert(new->otype == O_CONST); 3650 obj = scope_lookup(ctx->scope, &name); 3651 if (obj->otype == O_SCAN) { 3652 return scope_insert(ctx->scope, O_CONST, &ident, 3653 &name, type, new->value); 3654 } 3655 return obj; 3656 } 3657 3658 idecl = (struct incomplete_declaration *)obj; 3659 assert(idecl->type == IDECL_ENUM_FLD); 3660 3661 ctx->scope = idecl->field->enum_scope; 3662 struct expression *value = xcalloc(1, sizeof(struct expression)); 3663 value->result = type; 3664 if (idecl->field->field->value) { // explicit value 3665 struct expression *initializer = 3666 xcalloc(1, sizeof(struct expression)); 3667 check_expression(ctx, idecl->field->field->value, 3668 initializer, type->alias.type); 3669 3670 char *inittypename = gen_typename(initializer->result); 3671 char *builtintypename = gen_typename(type->alias.type); 3672 expect(ctx, &idecl->field->field->value->loc, 3673 type_is_assignable(type->alias.type, initializer->result), 3674 "Enum value type (%s) is not assignable from initializer type (%s) for value %s", 3675 builtintypename, inittypename, idecl->obj.ident.name); 3676 free(inittypename); 3677 free(builtintypename); 3678 3679 initializer = lower_implicit_cast(type, initializer); 3680 enum eval_result r = eval_expr(ctx, initializer, value); 3681 expect(ctx, &idecl->field->field->value->loc, r == EVAL_OK, 3682 "Unable to evaluate constant initializer at compile time"); 3683 } else { // implicit value 3684 const struct scope_object *obj = idecl->obj.lnext; 3685 // find previous enum value 3686 if (obj && obj->otype == O_SCAN) { 3687 // complete previous value first 3688 obj = wrap_resolver(ctx, obj, resolve_enum_field); 3689 } else { 3690 obj = NULL; 3691 } 3692 value->type = EXPR_CONSTANT; 3693 if (type_is_signed(type_dealias(type))) { 3694 if (obj == NULL) { 3695 value->constant.ival = 0; 3696 } else { 3697 value->constant.ival = obj->value->constant.ival + 1; 3698 } 3699 } else { 3700 if (obj == NULL) { 3701 value->constant.uval = 0; 3702 } else { 3703 value->constant.uval = obj->value->constant.uval + 1; 3704 } 3705 } 3706 } 3707 3708 return scope_insert(idecl->field->enum_scope, O_CONST, &name, &localname, type, value); 3709 } 3710 3711 const struct scope_object * 3712 resolve_enum_alias(struct context *ctx, const struct scope_object *obj) 3713 { 3714 struct identifier sub; 3715 struct incomplete_declaration *idecl = NULL; 3716 const struct scope_object *orig = obj; 3717 switch (obj->otype) { 3718 case O_SCAN: 3719 idecl = (struct incomplete_declaration *)obj; 3720 switch (idecl->type) { 3721 case IDECL_ENUM_FLD: 3722 return NULL; 3723 case IDECL_DECL: 3724 if (idecl->decl.decl_type != AST_DECL_TYPE) { 3725 return NULL; 3726 } 3727 if (idecl->decl.type.type->storage != STORAGE_ALIAS) { 3728 return NULL; 3729 } 3730 identifier_dup(&sub, &idecl->decl.type.type->alias); 3731 break; 3732 } 3733 break; 3734 case O_TYPE: 3735 if (obj->type->storage == STORAGE_ENUM) { 3736 return obj; 3737 } 3738 enum type_storage storage = obj->type->alias.type->storage; 3739 if (storage != STORAGE_ALIAS && storage != STORAGE_ENUM) { 3740 return NULL; 3741 } 3742 identifier_dup(&sub, &obj->type->alias.type->alias.name); 3743 break; 3744 case O_BIND: 3745 case O_CONST: 3746 case O_DECL: 3747 return NULL; 3748 } 3749 3750 obj = scope_lookup(ctx->scope, &sub); 3751 if (!obj) { 3752 // nonexistent secondary types can only happen in incomplete 3753 // types, so dereferencing idecl here is safe 3754 error(ctx, idecl->decl.loc, NULL, 3755 "Unknown object '%s'", identifier_unparse(&sub)); 3756 return NULL; 3757 } 3758 if (obj->otype == O_SCAN) { 3759 obj = wrap_resolver(ctx, obj, resolve_enum_alias); 3760 } else { 3761 assert(obj->otype == O_TYPE); 3762 obj = resolve_enum_alias(ctx, obj); 3763 } 3764 if (!obj) { 3765 return NULL; 3766 } 3767 assert(obj->otype == O_TYPE && obj->type->storage == STORAGE_ENUM); 3768 3769 // orig->type is (perhaps transitively) an alias of a resolved enum 3770 // type, which means its dependency graph is a linear chain of 3771 // resolved types ending with that enum, so we can immediately resolve it 3772 // There's no need to wrap this call, because the context is already 3773 // correct 3774 const struct type *type = resolve_type(ctx, orig)->type; 3775 3776 for (const struct scope_object *val = obj->type->_enum.values->objects; 3777 val; val = val->lnext) { 3778 struct identifier ns; 3779 identifier_dup(&ns, &orig->name); 3780 struct identifier ident, name = { 3781 .name = val->name.name, 3782 .ns = &ns, 3783 }; 3784 mkident(ctx, &ident, &name); 3785 if (val->otype != O_SCAN) { 3786 scope_insert(ctx->scope, O_CONST, &ident, &name, 3787 type, val->value); 3788 continue; 3789 } 3790 struct ast_enum_field *afield = 3791 xcalloc(1, sizeof(struct ast_enum_field)); 3792 *afield = (struct ast_enum_field){ 3793 .loc = (struct location){0}, // XXX: what to put here? 3794 .name = strdup(val->name.name), 3795 }; 3796 3797 struct incomplete_enum_field *field = 3798 xcalloc(1, sizeof(struct incomplete_enum_field)); 3799 idecl = (struct incomplete_declaration *)val; 3800 *field = (struct incomplete_enum_field){ 3801 .field = afield, 3802 .type = type, 3803 .enum_scope = idecl->field->enum_scope, 3804 }; 3805 3806 idecl = incomplete_declaration_create(ctx, (struct location){0}, 3807 ctx->scope, &ident, &name); 3808 idecl->type = IDECL_ENUM_FLD; 3809 idecl->field = field; 3810 }; 3811 return obj; 3812 } 3813 3814 const struct scope_object * 3815 resolve_dimensions(struct context *ctx, const struct scope_object *obj) 3816 { 3817 struct incomplete_declaration *idecl = (struct incomplete_declaration*)obj; 3818 struct dimensions dim = type_store_lookup_dimensions(ctx->store, 3819 idecl->decl.type.type); 3820 ((struct scope_object *)obj)->type = xcalloc(1, sizeof(struct type)); 3821 *(struct type *)obj->type = (struct type){ 3822 .size = dim.size, 3823 .align = dim.align, 3824 }; 3825 return &idecl->obj; 3826 } 3827 3828 const struct scope_object * 3829 resolve_type(struct context *ctx, const struct scope_object *obj) 3830 { 3831 if (obj->otype != O_SCAN) { 3832 assert(obj->otype == O_TYPE); 3833 return obj; 3834 } 3835 struct incomplete_declaration *idecl = (struct incomplete_declaration*)obj; 3836 if (idecl->type != IDECL_DECL || idecl->decl.decl_type != AST_DECL_TYPE) { 3837 struct location loc; 3838 if (idecl->type == IDECL_ENUM_FLD) { 3839 loc = idecl->field->field->loc; 3840 } else { 3841 loc = idecl->decl.loc; 3842 } 3843 error(ctx, loc, false, "'%s' is not a type", 3844 identifier_unparse(&idecl->obj.name)); 3845 handle_errors(ctx->errors); 3846 } 3847 3848 // 1. compute type dimensions 3849 struct dimensions dim = type_store_lookup_dimensions(ctx->store, 3850 idecl->decl.type.type); 3851 3852 handle_errors(ctx->errors); 3853 idecl->in_progress = false; 3854 3855 // 2. compute type representation and store it 3856 struct identifier ident = {0}, name = {0}; 3857 identifier_dup(&name, &idecl->decl.type.ident); 3858 mkident(ctx, &ident, &name); 3859 3860 struct type _alias = { 3861 .storage = STORAGE_ALIAS, 3862 .alias = { 3863 .ident = ident, 3864 .name = name, 3865 .type = NULL, 3866 .exported = idecl->decl.exported, 3867 }, 3868 .size = dim.size, 3869 .align = dim.align, 3870 .flags = idecl->decl.type.type->flags, 3871 }; 3872 3873 const struct type *alias = type_store_lookup_alias(ctx->store, &_alias); 3874 const struct scope_object *ret = 3875 scope_insert(ctx->scope, O_TYPE, &ident, &name, alias, NULL); 3876 ((struct type *)ret->type)->alias.type = 3877 type_store_lookup_atype(ctx->store, idecl->decl.type.type); 3878 return ret; 3879 } 3880 3881 static void 3882 scan_decl_start(struct context *ctx, struct scope *imports, struct ast_decl *decl) 3883 { 3884 switch (decl->decl_type) { 3885 case AST_DECL_CONST: 3886 for (struct ast_global_decl *g = &decl->constant; g; g = g->next) { 3887 struct identifier with_ns = {0}; 3888 mkident(ctx, &with_ns, &g->ident); 3889 struct incomplete_declaration *idecl = 3890 incomplete_declaration_create(ctx, decl->loc, 3891 ctx->scope, &with_ns, &g->ident); 3892 idecl->type = IDECL_DECL; 3893 idecl->decl = (struct ast_decl){ 3894 .decl_type = AST_DECL_CONST, 3895 .loc = decl->loc, 3896 .constant = *g, 3897 .exported = decl->exported, 3898 }; 3899 idecl->imports = imports; 3900 } 3901 break; 3902 case AST_DECL_GLOBAL: 3903 for (struct ast_global_decl *g = &decl->global; g; g = g->next) { 3904 struct identifier with_ns = {0}; 3905 mkident(ctx, &with_ns, &g->ident); 3906 struct incomplete_declaration *idecl = 3907 incomplete_declaration_create(ctx, decl->loc, 3908 ctx->scope, &with_ns, &g->ident); 3909 idecl->type = IDECL_DECL; 3910 idecl->decl = (struct ast_decl){ 3911 .decl_type = AST_DECL_GLOBAL, 3912 .loc = decl->loc, 3913 .global = *g, 3914 .exported = decl->exported, 3915 }; 3916 idecl->imports = imports; 3917 } 3918 break; 3919 case AST_DECL_FUNC: 3920 if (decl->function.flags) { 3921 scan_function(ctx, &decl->function); 3922 return; 3923 } 3924 struct ast_function_decl *func = &decl->function; 3925 struct identifier with_ns = {0}; 3926 mkident(ctx, &with_ns, &func->ident); 3927 struct incomplete_declaration *idecl = 3928 incomplete_declaration_create(ctx, decl->loc, 3929 ctx->scope, &with_ns, &func->ident); 3930 idecl->type = IDECL_DECL; 3931 idecl->decl = (struct ast_decl){ 3932 .decl_type = AST_DECL_FUNC, 3933 .loc = decl->loc, 3934 .function = *func, 3935 .exported = decl->exported, 3936 }; 3937 idecl->imports = imports; 3938 break; 3939 case AST_DECL_TYPE: 3940 incomplete_types_create(ctx, imports, decl); 3941 break; 3942 } 3943 } 3944 3945 const struct scope_object * 3946 resolve_decl(struct context *ctx, const struct scope_object *obj) 3947 { 3948 struct incomplete_declaration *idecl = (struct incomplete_declaration*)obj; 3949 switch (idecl->type) { 3950 case IDECL_ENUM_FLD: 3951 return resolve_enum_field(ctx, obj); 3952 case IDECL_DECL: 3953 break; 3954 } 3955 3956 switch (idecl->decl.decl_type) { 3957 case AST_DECL_CONST: 3958 return scan_const(ctx, &idecl->decl.constant); 3959 case AST_DECL_GLOBAL: 3960 return scan_global(ctx, &idecl->decl.global); 3961 case AST_DECL_FUNC: 3962 return scan_function(ctx, &idecl->decl.function); 3963 case AST_DECL_TYPE: 3964 return resolve_type(ctx, obj); 3965 } 3966 abort(); 3967 } 3968 3969 const struct scope_object * 3970 wrap_resolver(struct context *ctx, const struct scope_object *obj, 3971 resolvefn resolver) 3972 { 3973 // save current subunit and enum context 3974 struct scope *scope = ctx->scope; 3975 struct scope *subunit = ctx->unit->parent; 3976 ctx->unit->parent = NULL; 3977 3978 // ensure this declaration wasn't already scanned 3979 struct incomplete_declaration *idecl = (struct incomplete_declaration *)obj; 3980 obj = scope_lookup(ctx->scope, &idecl->obj.name); 3981 if (obj && obj->otype != O_SCAN) { 3982 goto exit; 3983 } 3984 3985 // load this declaration's subunit context 3986 ctx->scope = ctx->unit; 3987 ctx->unit->parent = idecl->imports; 3988 3989 // resolving a declaration that is already in progress -> cycle 3990 if (idecl->in_progress) { 3991 struct location loc; 3992 if (idecl->type == IDECL_ENUM_FLD) { 3993 loc = idecl->field->field->loc; 3994 } else { 3995 loc = idecl->decl.loc; 3996 } 3997 error(ctx, loc, NULL, "Circular dependency for '%s'\n", 3998 identifier_unparse(&idecl->obj.name)); 3999 handle_errors(ctx->errors); 4000 } 4001 idecl->in_progress = true; 4002 4003 obj = resolver(ctx, &idecl->obj); 4004 exit: 4005 // load stored context 4006 ctx->unit->parent = subunit; 4007 ctx->scope = scope; 4008 idecl->in_progress = false; 4009 return obj; 4010 } 4011 4012 static void 4013 load_import(struct context *ctx, struct ast_imports *import, 4014 struct type_store *ts, struct scope *scope) 4015 { 4016 struct context *old_ctx = ctx->store->check_context; 4017 struct scope *mod = module_resolve(ctx->modcache, 4018 ctx->defines, &import->ident, ts); 4019 ctx->store->check_context = old_ctx; 4020 4021 struct identifier _ident = {0}; 4022 struct identifier *mod_ident = &_ident; 4023 if (import->mode & (AST_IMPORT_WILDCARD | AST_IMPORT_ALIAS)) { 4024 mod_ident = import->alias; 4025 } else { 4026 mod_ident->name = import->ident.name; 4027 } 4028 if (import->mode & AST_IMPORT_MEMBERS) { 4029 assert(!(import->mode & AST_IMPORT_WILDCARD)); 4030 for (struct ast_imports *member = import->members; 4031 member; member = member->next) { 4032 struct identifier name = { 4033 .name = member->alias? 4034 member->alias->name : member->ident.name, 4035 .ns = import->alias, 4036 }; 4037 struct identifier ident = { 4038 .name = member->ident.name, 4039 .ns = &import->ident, 4040 }; 4041 const struct scope_object *obj = scope_lookup(mod, &ident); 4042 if (!obj) { 4043 expect(ctx, &member->loc, false, "Unknown object '%s'", 4044 identifier_unparse(&ident)); 4045 } 4046 scope_insert(scope, obj->otype, &obj->ident, 4047 &name, obj->type, obj->value); 4048 if (obj->otype != O_TYPE 4049 || type_dealias(obj->type)->storage 4050 != STORAGE_ENUM) { 4051 continue; 4052 }; 4053 struct scope *enum_scope = 4054 type_dealias(obj->type)->_enum.values; 4055 for (struct scope_object *o = enum_scope->objects; 4056 o; o = o->lnext) { 4057 struct identifier value_ident = { 4058 .name = o->name.name, 4059 .ns = &ident, 4060 }; 4061 struct identifier value_name = { 4062 .name = o->name.name, 4063 .ns = &name, 4064 }; 4065 scope_insert(scope, o->otype, &value_ident, 4066 &value_name, o->type, o->value); 4067 }; 4068 4069 } 4070 } else { 4071 for (struct scope_object *obj = mod->objects; 4072 obj; obj = obj->lnext) { 4073 if (obj->otype == O_SCAN) { 4074 continue; 4075 } 4076 4077 if (!(import->mode & AST_IMPORT_ALIAS) 4078 && import->ident.ns != NULL) { 4079 scope_insert(scope, obj->otype, &obj->ident, 4080 &obj->name, obj->type, obj->value); 4081 } 4082 4083 struct identifier ns, name = { 4084 .name = obj->name.name, 4085 .ns = mod_ident, 4086 }; 4087 if (type_dealias(obj->type)->storage == STORAGE_ENUM 4088 && obj->otype == O_CONST) { 4089 ns = (struct identifier){ 4090 .name = obj->name.ns->name, 4091 .ns = mod_ident, 4092 }; 4093 name.ns = &ns; 4094 }; 4095 scope_insert(scope, obj->otype, &obj->ident, 4096 &name, obj->type, obj->value); 4097 } 4098 } 4099 } 4100 4101 struct scope * 4102 check_internal(struct type_store *ts, 4103 struct modcache **cache, 4104 bool is_test, 4105 struct define *defines, 4106 const struct ast_unit *aunit, 4107 struct unit *unit, 4108 bool scan_only) 4109 { 4110 struct context ctx = {0}; 4111 ctx.ns = unit->ns; 4112 ctx.is_test = is_test; 4113 ctx.store = ts; 4114 ctx.store->check_context = &ctx; 4115 ctx.defines = defines; 4116 ctx.next = &ctx.errors; 4117 ctx.modcache = cache; 4118 4119 // Top-level scope management involves: 4120 // 4121 // - Creating a top-level scope for the whole unit, to which 4122 // declarations are added. 4123 // - Creating a scope for each sub-unit, and populating it with imports. 4124 // 4125 // Further down the call frame, subsequent functions will create 4126 // sub-scopes for each declaration, expression-list, etc. 4127 4128 struct scope *def_scope = NULL; 4129 scope_push(&def_scope, SCOPE_UNIT); 4130 4131 // Put defines into a temporary scope (-D on the command line) 4132 // XXX: This duplicates a lot of code with scan_const 4133 for (struct define *def = defines; def; def = def->next) { 4134 struct location loc = { 4135 .file = 0, .lineno = 1, .colno = 1, 4136 }; 4137 const struct type *type = type_store_lookup_atype( 4138 ctx.store, def->type); 4139 expect(&ctx, &loc, type != NULL, "Unable to resolve type"); 4140 struct expression *initializer = 4141 xcalloc(1, sizeof(struct expression)); 4142 check_expression(&ctx, def->initializer, initializer, type); 4143 // TODO: This could be more detailed 4144 expect(&ctx, &loc, ctx.errors == NULL, "Invalid initializer"); 4145 char *typename1 = gen_typename(initializer->result); 4146 char *typename2 = gen_typename(type); 4147 expect(&ctx, &loc, type_is_assignable(type, initializer->result), 4148 "Initializer type %s is not assignable to constant type type %s", 4149 typename1, typename2); 4150 free(typename1); 4151 free(typename2); 4152 4153 initializer = lower_implicit_cast(type, initializer); 4154 struct expression *value = 4155 xcalloc(1, sizeof(struct expression)); 4156 enum eval_result r = eval_expr(&ctx, initializer, value); 4157 expect(&ctx, &loc, r == EVAL_OK, 4158 "Unable to evaluate constant initializer at compile time"); 4159 scope_insert(def_scope, O_CONST, 4160 &def->ident, &def->ident, type, value); 4161 } 4162 struct scopes *subunit_scopes = NULL; 4163 struct scopes **next = &subunit_scopes; 4164 struct scope *su_scope = NULL; 4165 struct identifiers **inext = &unit->imports; 4166 4167 ctx.scope = NULL; 4168 ctx.unit = scope_push(&ctx.scope, SCOPE_UNIT); 4169 4170 // Populate the imports and put declarations into a scope. 4171 // Each declaration holds a reference to its subunit's imports 4172 // A scope gets us: 4173 // a) duplicate detection for free 4174 // b) a way to find declaration's definition when it's refered to 4175 for (const struct ast_subunit *su = &aunit->subunits; 4176 su; su = su->next) { 4177 su_scope = NULL; 4178 scope_push(&su_scope, SCOPE_SUBUNIT); 4179 for (struct ast_imports *imports = su->imports; 4180 imports; imports = imports->next) { 4181 load_import(&ctx, imports, ts, su_scope); 4182 4183 bool found = false; 4184 for (struct identifiers *uimports = unit->imports; 4185 uimports; uimports = uimports->next) { 4186 if (identifier_eq(&uimports->ident, &imports->ident)) { 4187 found = true; 4188 break; 4189 } 4190 } 4191 if (!found) { 4192 struct identifiers *uimport = *inext = 4193 xcalloc(1, sizeof(struct identifiers)); 4194 identifier_dup(&uimport->ident, &imports->ident); 4195 inext = &uimport->next; 4196 } 4197 } 4198 4199 for (struct ast_decls *d = su->decls; d; d = d->next) { 4200 scan_decl_start(&ctx, su_scope, &d->decl); 4201 }; 4202 4203 *next = xcalloc(1, sizeof(struct scopes)); 4204 (*next)->scope = su_scope; 4205 next = &(*next)->next; 4206 } 4207 4208 // Put defines into unit scope 4209 // We have to insert them *after* declarations, because this way they 4210 // shadow declarations, not the other way around 4211 // 4212 // XXX: shadowed declarations are not checked for consistency 4213 for (const struct scope_object *obj = def_scope->objects; 4214 obj; obj = obj->lnext) { 4215 scope_insert(ctx.unit, O_CONST, &obj->ident, &obj->name, 4216 obj->type, obj->value); 4217 } 4218 scope_free(def_scope); 4219 4220 // Find enum aliases and store them in incomplete enum value declarations 4221 for (const struct scope_object *obj = ctx.scope->objects; 4222 obj; obj = obj->lnext) { 4223 if (obj->otype != O_SCAN) { 4224 continue; 4225 } 4226 struct incomplete_declaration *idecl = 4227 (struct incomplete_declaration *)obj; 4228 if (idecl->type == IDECL_DECL 4229 && idecl->decl.decl_type == AST_DECL_TYPE) { 4230 wrap_resolver(&ctx, obj, resolve_enum_alias); 4231 } 4232 } 4233 4234 // Perform actual declaration resolution 4235 for (const struct scope_object *obj = ctx.scope->objects; 4236 obj; obj = obj->lnext) { 4237 if (obj->otype != O_SCAN) { 4238 continue; 4239 } 4240 wrap_resolver(&ctx, obj, resolve_decl); 4241 } 4242 4243 handle_errors(ctx.errors); 4244 4245 if (scan_only) { 4246 ctx.store->check_context = NULL; 4247 ctx.unit->parent = NULL; 4248 return ctx.unit; 4249 } 4250 4251 // Populate the expression graph 4252 struct scopes *scope = subunit_scopes; 4253 struct declarations **next_decl = &unit->declarations; 4254 for (const struct ast_subunit *su = &aunit->subunits; 4255 su; su = su->next) { 4256 // subunit scope has to be *behind* unit scope 4257 ctx.scope->parent = scope->scope; 4258 next_decl = check_declarations(&ctx, su->decls, next_decl); 4259 scope = scope->next; 4260 } 4261 4262 if (!unit->declarations) { 4263 fprintf(stderr, "Error: module contains no declarations\n"); 4264 exit(EXIT_FAILURE); 4265 } 4266 4267 handle_errors(ctx.errors); 4268 4269 ctx.store->check_context = NULL; 4270 ctx.unit->parent = NULL; 4271 return ctx.unit; 4272 } 4273 4274 struct scope * 4275 check(struct type_store *ts, 4276 bool is_test, 4277 struct define *defines, 4278 const struct ast_unit *aunit, 4279 struct unit *unit) 4280 { 4281 struct modcache *modcache[MODCACHE_BUCKETS]; 4282 memset(modcache, 0, sizeof(modcache)); 4283 return check_internal(ts, modcache, is_test, defines, aunit, unit, false); 4284 }