From 089dab7b3f3025496a841cacdd210c27a039b862 Mon Sep 17 00:00:00 2001 From: Vic Nightfall Date: Sat, 9 Nov 2024 12:47:07 +0100 Subject: [PATCH] Generate function for weak references too --- src/scope.pr | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/scope.pr b/src/scope.pr index d8736f5..01832f5 100644 --- a/src/scope.pr +++ b/src/scope.pr @@ -728,7 +728,7 @@ def find_function(scope: &Scope, node: &parser::Node, v: &Value, parameter_t: &V } let s = typechecking::overload_score(v.tpe, parameter_t, context.module, false) - //print(scope.module.module, " ", v.tpe.type_name, " ", s, "\n") + //print(scope.module.module if scope.module else to_str(""), " ", v.tpe.type_name, " ", s, "\n") if s >= 0 { if s < @score { @score = s @@ -896,8 +896,15 @@ export def generate_function(scope: &Scope, node: &parser::Node, parameter_t: &V if vector::length(parameter_t) >= 1 { var first_parameter = parameter_t(0) if not first_parameter.tpe { return null } - if first_parameter.tpe.kind == typechecking::TypeKind::REFERENCE and - (first_parameter.tpe.tpe and first_parameter.tpe.tpe.kind == typechecking::TypeKind::STRUCTURAL) { + + var ftpe = first_parameter.tpe + if is_ref_or_weak(ftpe) and + (ftpe.tpe and ftpe.tpe.kind == typechecking::TypeKind::STRUCTURAL) { + + // Switch to strong reference + // TODO this removes the reference's canonical name but this is needed to only create one function + // We should really get rid of the string name creating a hash + ftpe = reference(ftpe.tpe) //let equals = get(toolchain::runtime_.scope, parser::make_identifier("equals")) //consteval::compile_function(equals, scope) @@ -905,7 +912,7 @@ export def generate_function(scope: &Scope, node: &parser::Node, parameter_t: &V let parameter_t2 = vector::copy(parameter_t) vector::remove(parameter_t2, 0) - let stpe = first_parameter.tpe.tpe + let stpe = ftpe.tpe let module = stpe.module let scope = module.scope @@ -957,7 +964,7 @@ export def generate_function(scope: &Scope, node: &parser::Node, parameter_t: &V vector::insert(parameter_t3, 0, [ name = "__ref", - _tpe = first_parameter.tpe + _tpe = ftpe ] !typechecking::NamedParameter) let tpe = typechecking::make_function_type_n(name_node, parameter_t3, return_t, module) let fun = has_function(scope, tpe) @@ -974,13 +981,13 @@ export def generate_function(scope: &Scope, node: &parser::Node, parameter_t: &V module.dyn_dispatch_consteval.push(tpe) return value - } else if vector::length(parameter_t) == 1 and is_pointer(first_parameter.tpe) and - name == "__destruct__" and typechecking::has_destructor(first_parameter.tpe.tpe) { + } else if vector::length(parameter_t) == 1 and is_pointer(ftpe) and + name == "__destruct__" and typechecking::has_destructor(ftpe.tpe) { let args = vector::make(typechecking::NamedParameter) args.push([ name = "__ptr", - _tpe = first_parameter.tpe + _tpe = ftpe ] !typechecking::NamedParameter) let name_node = parser::make_identifier("__destruct__") @@ -992,19 +999,19 @@ export def generate_function(scope: &Scope, node: &parser::Node, parameter_t: &V if module { compiler::predeclare_function(tpe, module) } - compiler::destructors(debug::type_to_str(first_parameter.tpe, full_name = true)) = tpe + compiler::destructors(debug::type_to_str(ftpe, full_name = true)) = tpe value.identifier = name_node value.is_generated = true return value - } else if vector::length(parameter_t) == 2 and first_parameter.tpe.kind == typechecking::TypeKind::POINTER and - name == "__construct__" and typechecking::has_copy_constructor(first_parameter.tpe.tpe, lookup = false) { + } else if vector::length(parameter_t) == 2 and ftpe.kind == typechecking::TypeKind::POINTER and + name == "__construct__" and typechecking::has_copy_constructor(ftpe.tpe, lookup = false) { let second_parameter = vector::get(parameter_t, 1) !*NamedParameter - if equals(second_parameter.tpe, first_parameter.tpe) { + if equals(second_parameter.tpe, ftpe) { let args = vector::make(typechecking::NamedParameter) args.push([ name = "__copy", - _tpe = first_parameter.tpe + _tpe = ftpe ] !typechecking::NamedParameter) args.push([ name = "__this", @@ -1020,7 +1027,7 @@ export def generate_function(scope: &Scope, node: &parser::Node, parameter_t: &V if module { compiler::predeclare_function(tpe, module) } - compiler::constructors(debug::type_to_str(first_parameter.tpe, full_name = true)) = tpe + compiler::constructors(debug::type_to_str(ftpe, full_name = true)) = tpe value.identifier = name_node value.is_generated = true return value