Fix issue with overloaded subscript and ++/--.

This commit is contained in:
Christoffer Lerno
2024-11-20 00:23:08 +01:00
parent 489bb70901
commit ba54232b8d
4 changed files with 131 additions and 4 deletions

View File

@@ -14,6 +14,7 @@
- Support &a[0] returning the distinct type when applying it to a distinct of a pointer.
- Fix error when calling `HashMap.remove` on uninitialized `HashMap`.
- Fix issue with resolved try-unwrap in defer.
- Fix issue with overloaded subscript and ++/--.
### Stdlib changes
- Add `io::MultiReader`, `io::MultiWriter`, and `io::TeeReader` structs.

View File

@@ -735,8 +735,7 @@ void print_var_expr(FILE *file, Expr *expr)
fputs("TODO: EXPR_SUBSCRIPT_ADDR", file);
break;
case EXPR_SUBSCRIPT_ASSIGN:
fputs("TODO: EXPR_SUBSCRIPT_ASSIGN", file);
break;
UNREACHABLE
case EXPR_SWIZZLE:
fputs("TODO: EXPR_SWIZZLE", file);
break;

View File

@@ -7146,10 +7146,72 @@ static inline bool sema_expr_analyse_incdec(SemaContext *context, Expr *expr)
// 5. We can only inc/dec numbers or pointers.
if (!type_underlying_may_add_sub(type) && type->type_kind != TYPE_POINTER)
{
SEMA_ERROR(inner, "The expression must be a number or a pointer.");
return false;
RETURN_SEMA_ERROR(inner, "The expression must be a number or a pointer.");
}
if (inner->expr_kind == EXPR_SUBSCRIPT_ASSIGN)
{
Expr *increased = exprptr(inner->subscript_assign_expr.expr);
Type *type_check = increased->type->canonical;
Expr *index = exprptr(inner->subscript_assign_expr.index);
Decl *operator = sema_find_operator(context, type_check, OVERLOAD_ELEMENT_REF);
Expr **args = NULL;
if (operator)
{
vec_add(args, exprptr(inner->subscript_assign_expr.index));
if (!sema_insert_method_call(context, inner, operator, exprptr(inner->subscript_assign_expr.expr), args)) return false;
expr_rewrite_insert_deref(inner);
goto OK;
}
operator = sema_find_operator(context, type_check, OVERLOAD_ELEMENT_AT);
if (!operator)
{
RETURN_SEMA_ERROR(expr, "There is no overload for [] for %s.", type_quoted_error_string(increased->type));
}
Type *return_type = typeget(operator->func_decl.signature.rtype);
if (return_type->canonical != type->canonical)
{
RETURN_SEMA_ERROR(expr, "There is a type mismatch between overload for [] and []= for %s.", type_quoted_error_string(increased->type));
}
expr_insert_addr(increased);
Decl *temp_val = decl_new_generated_var(increased->type, VARDECL_LOCAL, increased->span);
Decl *index_val = decl_new_generated_var(index->type, VARDECL_LOCAL, index->span);
Decl *value_val = decl_new_generated_var(inner->type, VARDECL_LOCAL, expr->span);
Decl *result_val = decl_new_generated_var(inner->type, VARDECL_LOCAL, expr->span);
Expr *decl_expr = expr_generate_decl(temp_val, increased);
Expr *decl_index_expr = expr_generate_decl(index_val, index);
Expr *unary = expr_new_expr(expr->expr_kind, expr);
unary->unary_expr.expr = expr_variable(value_val);
unary->unary_expr.operator = expr->unary_expr.operator;
unary->unary_expr.no_wrap = expr->unary_expr.no_wrap;
expr->expr_kind = EXPR_EXPRESSION_LIST;
expr->expression_list = NULL;
// temp = indexed
vec_add(expr->expression_list, decl_expr);
// temp_index = index
vec_add(expr->expression_list, decl_index_expr);
Expr *get_expr = expr_new(EXPR_ACCESS, increased->span);
vec_add(args, expr_variable(index_val));
Expr *temp_val_1 = expr_variable(temp_val);
expr_rewrite_insert_deref(temp_val_1);
if (!sema_insert_method_call(context, get_expr, operator, temp_val_1, args)) return false;
Expr *value_val_expr = expr_generate_decl(value_val, get_expr);
// temp_value = func(temp, temp_index)
vec_add(expr->expression_list, value_val_expr);
// temp_result = temp_value++
vec_add(expr->expression_list, expr_generate_decl(result_val, unary));
args = NULL;
vec_add(args, expr_variable(index_val));
vec_add(args, expr_variable(value_val));
Expr *temp_val_2 = expr_variable(temp_val);
expr_rewrite_insert_deref(temp_val_2);
if (!sema_insert_method_call(context, inner, declptr(inner->subscript_assign_expr.method), temp_val_2, args)) return false;
vec_add(expr->expression_list, inner);
vec_add(expr->expression_list, expr_variable(result_val));
return sema_expr_analyse_expr_list(context, expr);
}
OK:
// 6. Done, the result is same as the inner type.
expr->type = inner->type;
return true;

View File

@@ -0,0 +1,65 @@
// #target: macos-x64
module test;
import std;
struct Foo
{
int[5] a;
}
fn int Foo.get(self, usz i) @operator([]) => self.a[i];
fn void Foo.set(&self, usz i, int j) @operator([]=) { self.a[i] = j; }
Foo m;
fn void main() {
m.a[3] = 100;
int x = m[3]--;
}
/* #expect: test.ll
define i32 @test.Foo.get(ptr byval(%Foo) align 8 %0, i64 %1) #0 {
entry:
%ptroffset = getelementptr inbounds [4 x i8], ptr %0, i64 %1
%2 = load i32, ptr %ptroffset, align 4
ret i32 %2
}
define void @test.Foo.set(ptr %0, i64 %1, i32 %2) #0 {
entry:
%ptroffset = getelementptr inbounds [4 x i8], ptr %0, i64 %1
store i32 %2, ptr %ptroffset, align 4
ret void
}
define void @test.main() #0 {
entry:
%x = alloca i32, align 4
%.anon = alloca ptr, align 8
%.anon1 = alloca i32, align 4
%.anon2 = alloca i32, align 4
%indirectarg = alloca %Foo, align 8
%.anon3 = alloca i32, align 4
store i32 100, ptr getelementptr inbounds (i8, ptr @test.m, i64 12), align 4
store ptr @test.m, ptr %.anon, align 8
store i32 3, ptr %.anon1, align 4
%0 = load ptr, ptr %.anon, align 8
%1 = load i32, ptr %.anon1, align 4
%sext = sext i32 %1 to i64
call void @llvm.memcpy.p0.p0.i32(ptr align 8 %indirectarg, ptr align 4 %0, i32 20, i1 false)
%2 = call i32 @test.Foo.get(ptr byval(%Foo) align 8 %indirectarg, i64 %sext)
store i32 %2, ptr %.anon2, align 4
%3 = load i32, ptr %.anon2, align 4
%sub = sub i32 %3, 1
store i32 %sub, ptr %.anon2, align 4
store i32 %3, ptr %.anon3, align 4
%4 = load i32, ptr %.anon1, align 4
%sext4 = sext i32 %4 to i64
%5 = load ptr, ptr %.anon, align 8
%6 = load i32, ptr %.anon2, align 4
call void @test.Foo.set(ptr %5, i64 %sext4, i32 %6)
%7 = load i32, ptr %.anon3, align 4
store i32 %7, ptr %x, align 4
ret void
}