Skip to content

Commit

Permalink
Fix polymorphs not getting considered when imported
Browse files Browse the repository at this point in the history
  • Loading branch information
Victorious3 committed Oct 1, 2024
1 parent 5567dcd commit d03d06f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 50 deletions.
1 change: 1 addition & 0 deletions src/compiler.pr
Original file line number Diff line number Diff line change
Expand Up @@ -7277,6 +7277,7 @@ export def create_function(

function.forward_declare = false
function.allocas = vector::make(type &Insn)
function.function_locals = set::make()
let current_fun = errors::current_function
let current_signature = errors::current_signature
let previous_inline_offset = state.inline_offset
Expand Down
101 changes: 51 additions & 50 deletions src/scope.pr
Original file line number Diff line number Diff line change
Expand Up @@ -1145,56 +1145,6 @@ export def get_function_check(
let first_score = score
var value = first_function

// Check for polymorphic instances
if is_global(scope) and (not first_function or first_function.tpe.is_polymorph) {
var root_scope = context.module.scope
var new_score = std::MAX_INT32

if root_scope.polymorphics {
// We need to check the path we took to get here and then see if
// there is a module under that path
var poly_value: &Value
if path.length > 0 {
let module_scope = get(root_scope, parser::make_identifier(@path.to_array()))
if module_scope and root_scope.polymorphics.contains(module_scope.module.filename) {
// If there is a module we can look into the polymorphic instances of that module
let poly_scope = root_scope.polymorphics(module_scope.module.filename)
if poly_scope {
let val = poly_scope.fields.get_or_default(name, null)
let new_code, new_value = find_function(poly_scope, id, val, parameter_t, *new_score, dry_run, context)
if new_value and new_score < score {
return new_code, new_value
}
}
}
} else if first_function and first_function.tpe.is_polymorph {
if root_scope.polymorphics.contains(first_function.module.filename) {
// We found the function and we should only look into the polymorphs from this module
let poly_scope = root_scope.polymorphics(first_function.module.filename)
if poly_scope {
let val = poly_scope.fields.get_or_default(name, null)
let new_code, new_value = find_function(poly_scope, id, val, parameter_t, *new_score, dry_run, context)
if new_value and new_score < score {
return new_code, new_value
}
}
}
} else {
// Otherwise we have a non qualified call, which means we need to look at ALL modules
for var module_name in @root_scope.polymorphics.keys() {
let poly_scope = root_scope.polymorphics(module_name)
if poly_scope {
let val = poly_scope.fields.get_or_default(name, null)
let new_code, new_value = find_function(poly_scope, id, val, parameter_t, *new_score, dry_run, context)
if new_value and new_score < score {
return new_code, new_value
}
}
}
}
}
}

var d = false
if (@scope).imports {
for var i in 0..vector::length((@scope).imports) {
Expand Down Expand Up @@ -1276,6 +1226,57 @@ export def get_function_check(
value = typecheck_function(value, id, scope, dry_run, parameter_t, context)
}

// Check for polymorphic instances
if is_global(scope) and (not first_function or first_function.tpe.is_polymorph) {
var root_scope = context.module.scope
var new_score = std::MAX_INT32

if root_scope.polymorphics {
// We need to check the path we took to get here and then see if
// there is a module under that path
var poly_value: &Value
if path.length > 0 {
let module_scope = get(root_scope, parser::make_identifier(@path.to_array()))
if module_scope and root_scope.polymorphics.contains(module_scope.module.filename) {
// If there is a module we can look into the polymorphic instances of that module
let poly_scope = root_scope.polymorphics(module_scope.module.filename)
if poly_scope {
let val = poly_scope.fields.get_or_default(name, null)
let new_code, new_value = find_function(poly_scope, id, val, parameter_t, *new_score, dry_run, context)
if new_value and new_score < score {
return new_code, new_value
}
}
}
} else if first_function and first_function.tpe.is_polymorph {
if root_scope.polymorphics.contains(first_function.module.filename) {
// We found the function and we should only look into the polymorphs from this module
let poly_scope = root_scope.polymorphics(first_function.module.filename)
if poly_scope {
let val = poly_scope.fields.get_or_default(name, null)
let new_code, new_value = find_function(poly_scope, id, val, parameter_t, *new_score, dry_run, context)
if new_value and new_score < score {
return new_code, new_value
}
}
}
} else {
// Otherwise we have a non qualified call, which means we need to look at ALL modules
for var module_name in @root_scope.polymorphics.keys() {
let poly_scope = root_scope.polymorphics(module_name)
if poly_scope {
let val = poly_scope.fields.get_or_default(name, null)
let new_code, new_value = find_function(poly_scope, id, val, parameter_t, *new_score, dry_run, context)
if new_value and new_score < score {
return new_code, new_value
}
}
}
}
}
}


return code, value
} else {
let head = vector::head_vec((@id).value.identifier.path)
Expand Down

0 comments on commit d03d06f

Please sign in to comment.