commit a56678581d9b6a7ac11c117d750142863dffc3b8
parent a3551bd75a0712760a8e2204e4a23dfb9e4b5edc
Author: Drew DeVault <sir@cmpwn.com>
Date: Mon, 11 Jan 2021 10:55:51 -0500
Implement implicit cast from slice to array
Diffstat:
2 files changed, 9 insertions(+), 2 deletions(-)
diff --git a/src/gen.c b/src/gen.c
@@ -644,7 +644,12 @@ gen_expr_cast(struct gen_context *ctx,
return;
}
- gen_temp(ctx, &in, qtype_for_type(ctx, from, false), "cast.in.%d");
+ if (type_is_aggregate(expr->result)) {
+ alloc_temp(ctx, &in, expr->result, "cast.in.%d");
+ qval_deref(&in);
+ } else {
+ gen_temp(ctx, &in, qtype_for_type(ctx, from, false), "cast.in.%d");
+ }
gen_expression(ctx, expr->cast.value, &in);
enum qbe_instr op;
diff --git a/src/type_store.c b/src/type_store.c
@@ -114,6 +114,9 @@ type_is_assignable(struct type_store *store,
return to == &builtin_type_const_ptr_char;
case TYPE_STORAGE_VOID:
return true;
+ case TYPE_STORAGE_SLICE:
+ return from->storage == TYPE_STORAGE_ARRAY
+ && to->array.members == from->array.members;
// The following types are only assignable from themselves, and are
// handled above:
case TYPE_STORAGE_ARRAY:
@@ -122,7 +125,6 @@ type_is_assignable(struct type_store *store,
case TYPE_STORAGE_FUNCTION:
case TYPE_STORAGE_NULL:
case TYPE_STORAGE_RUNE:
- case TYPE_STORAGE_SLICE:
case TYPE_STORAGE_STRUCT:
case TYPE_STORAGE_UNION:
return false;