Skip to content

Commit

Permalink
Generate function for weak references too
Browse files Browse the repository at this point in the history
  • Loading branch information
Victorious3 committed Nov 9, 2024
1 parent 30d5db9 commit 089dab7
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions src/scope.pr
Original file line number Diff line number Diff line change
Expand Up @@ -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("<unknown>"), " ", v.tpe.type_name, " ", s, "\n")
if s >= 0 {
if s < @score {
@score = s
Expand Down Expand Up @@ -896,16 +896,23 @@ 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)

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

Expand Down Expand Up @@ -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)
Expand All @@ -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__")
Expand All @@ -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",
Expand All @@ -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
Expand Down

0 comments on commit 089dab7

Please sign in to comment.