Propagating constants in operator overloads

This commit is contained in:
Krzosa Karol
2023-02-09 16:11:16 +01:00
parent 0029e0688b
commit 5e8b3739af
2 changed files with 23 additions and 11 deletions

View File

@@ -539,21 +539,22 @@ gen_ast(Ast *ast){
Arena *scratch = pctx->scratch;
Scratch_Scope _scope(scratch);
Intern_String var_name = pctx->intern(unique_name_scratch(scratch, node));
gen_simple_decl(node->resolved_type, var_name);
gen(";");
gen("return (");
gen_simple_decl(node->resolved_type);
gen("){");
int i = 0;
global_indent++;
For(node->expr){
genln("%QMemoryCopy(&%Q.m%d, ", pctx->symbol_prefix, var_name, i);
if(!is_array(it->resolved_type)) gen("&");
gen("(");
gen_line(it);
genln("");
gen_expr(it);
gen(")");
gen(", sizeof(%Q.m%d));", var_name, i++);
if (!node->expr.is_last(&it)) {
gen(",");
}
genln("return %Q;", var_name);
}
global_indent--;
genln("");
gen("};");
return;
}

View File

@@ -1307,6 +1307,7 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str
try_converting_untyped_to_default_type(&right_copy);
U64 hash = calculate_hash_for_arguments(left_copy.type, right_copy.type);
Ast_Decl *operator_overload = resolve_operator_overload(node->parent_scope, left_copy.type, right_copy.type, node->pos, node->op, hash);
if(operator_overload){
proceed_to_default_operator_handler = false;
if(operator_overload->lambda->ret.len != 1){
@@ -1315,6 +1316,16 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str
left.value = left_copy;
right.value = right_copy;
// @warning: might be buggy, added after a long break
// Propagate const
if (left.is_const) {
Ast_Atom *atom_left = (Ast_Atom *)node->left;
atom_left->value = left.value;
}
if (right.is_const) {
Ast_Atom *atom_right = (Ast_Atom *)node->right;
atom_right->value = right.value;
}
node->resolved_type = operator_overload->type->func.ret;
node->resolved_operator_overload = operator_overload;