From 5e8b3739af5feff6afbef96d9bad7ebcdfeef422 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Thu, 9 Feb 2023 16:11:16 +0100 Subject: [PATCH] Propagating constants in operator overloads --- core_codegen_c_language.cpp | 23 ++++++++++++----------- core_typechecking.cpp | 11 +++++++++++ 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/core_codegen_c_language.cpp b/core_codegen_c_language.cpp index b01a407..480f97d 100644 --- a/core_codegen_c_language.cpp +++ b/core_codegen_c_language.cpp @@ -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; } diff --git a/core_typechecking.cpp b/core_typechecking.cpp index 966f86b..c041a53 100644 --- a/core_typechecking.cpp +++ b/core_typechecking.cpp @@ -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;