Propagating constants in operator overloads
This commit is contained in:
@@ -539,21 +539,22 @@ gen_ast(Ast *ast){
|
|||||||
Arena *scratch = pctx->scratch;
|
Arena *scratch = pctx->scratch;
|
||||||
Scratch_Scope _scope(scratch);
|
Scratch_Scope _scope(scratch);
|
||||||
|
|
||||||
Intern_String var_name = pctx->intern(unique_name_scratch(scratch, node));
|
gen("return (");
|
||||||
gen_simple_decl(node->resolved_type, var_name);
|
gen_simple_decl(node->resolved_type);
|
||||||
gen(";");
|
gen("){");
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
global_indent++;
|
||||||
For(node->expr){
|
For(node->expr){
|
||||||
genln("%QMemoryCopy(&%Q.m%d, ", pctx->symbol_prefix, var_name, i);
|
gen_line(it);
|
||||||
if(!is_array(it->resolved_type)) gen("&");
|
genln("");
|
||||||
gen("(");
|
|
||||||
gen_expr(it);
|
gen_expr(it);
|
||||||
gen(")");
|
if (!node->expr.is_last(&it)) {
|
||||||
gen(", sizeof(%Q.m%d));", var_name, i++);
|
gen(",");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
genln("return %Q;", var_name);
|
global_indent--;
|
||||||
|
genln("");
|
||||||
|
gen("};");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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);
|
try_converting_untyped_to_default_type(&right_copy);
|
||||||
U64 hash = calculate_hash_for_arguments(left_copy.type, right_copy.type);
|
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);
|
Ast_Decl *operator_overload = resolve_operator_overload(node->parent_scope, left_copy.type, right_copy.type, node->pos, node->op, hash);
|
||||||
|
|
||||||
if(operator_overload){
|
if(operator_overload){
|
||||||
proceed_to_default_operator_handler = false;
|
proceed_to_default_operator_handler = false;
|
||||||
if(operator_overload->lambda->ret.len != 1){
|
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;
|
left.value = left_copy;
|
||||||
right.value = right_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_type = operator_overload->type->func.ret;
|
||||||
node->resolved_operator_overload = operator_overload;
|
node->resolved_operator_overload = operator_overload;
|
||||||
|
|||||||
Reference in New Issue
Block a user