@@ -245,6 +245,27 @@ export type Scope = struct {
245245 polymorphics: &SMap(&Scope)
246246}
247247
248+ export def print_scope(scope: &Scope) {
249+ print_scope(scope, "")
250+ }
251+
252+ def print_scope(scope: &Scope, indent: Str) {
253+ if not scope { return }
254+ print(indent, "Scope (module=", scope.module.module if scope.module else to_str("<null>"), "):\n")
255+ let keys = scope.fields.keys()
256+ for var i in 0..keys.size {
257+ let key = keys(i)
258+ let value = scope.fields(key)
259+ print(indent, " ", key, " -> ", typechecking::generic_to_string(value.tpe), " (", to_string(value.kind), ", ", to_string(value.phase), ")\n")
260+ }
261+ if scope.parents {
262+ for var parent in scope.parents {
263+ print(indent, "Parent:\n")
264+ print_scope(parent, indent + " ")
265+ }
266+ }
267+ }
268+
248269export def parent(scope: &Scope) -> &Scope {
249270 if scope.parents and scope.parents.length > 0 {
250271 return scope.parents(0)
@@ -729,19 +750,32 @@ def find_function(scope: &Scope, node: &parser::Node, v: &Value, parameter_t: &V
729750 if v.kind != ValueKind::FUNCTION { return false, null }
730751
731752 var found: &Value = null
753+ var found_scope: &Scope = null
732754 var d = false
733755 loop {
734756 if not v { break }
735757 /*if v.node and v.state {
736758 v.tpe = typechecking::lookup_parameters(v.node, v.state)
737759 }*/
738760
739- let s = typechecking::overload_score(v.fdef, parameter_t, context, false)
761+ var new_scope = context
762+ if typechecking::is_polymorph(v.fdef.parameter_t) {
763+ // Create temporary scope for type lookup
764+ // TODO Maybe cache it somewhere instead of creating a new one when creating a polymorphic instance
765+ let parents = vector::make(type weak &Scope)
766+ parents.push(scope)
767+ parents.push(context)
768+ new_scope = enter_scope(parents, scope.module)
769+ }
770+ let s = typechecking::overload_score(v.fdef, parameter_t, new_scope, false)
740771 //print(scope.module.module if scope.module else to_str("<unknown>"), " ", v.tpe.type_name, " ", s, "\n")
772+ //print_fdef(v.fdef)
773+ //print("\tscore: ", s, "\n")
741774 if s >= 0 {
742775 if s < @score {
743776 @score = s
744777 found = v
778+ found_scope = new_scope
745779 d = false
746780 } else if s == @score {
747781 d = true
@@ -764,7 +798,14 @@ def find_function(scope: &Scope, node: &parser::Node, v: &Value, parameter_t: &V
764798 if not dry_run { ambigous_reference(node) }
765799 return true, null
766800 }
767- return found != null, found
801+
802+ if found != null {
803+ if found.phase == Phase::DEFINED and not dry_run {
804+ found = typecheck_function(found, parameter_t, found_scope)
805+ }
806+ return true, found
807+ }
808+ return false, null
768809}
769810
770811export def find_implicit_function(scope: &Scope, parameter_t: &Vector(typechecking::NamedParameter), ret: &typechecking::Type, force_compile: bool = true, context: &Scope = null) -> &Value {
@@ -903,6 +944,7 @@ export def remove(scope: &Scope, value: &Value) {
903944
904945export def generate_function(scope: &Scope, node: &parser::Node, parameter_t: &Vector(typechecking::NamedParameter), dry_run: bool = false) -> &Value {
905946 let module = scope.module
947+ if not module { return null }
906948 scope = module.scope
907949
908950 let name = last_path_element(node)
@@ -1084,13 +1126,13 @@ export def create_underscore(scope: &Scope, name_node: &parser::Node, tpe: &type
10841126
10851127export def typecheck_function(value: &Value, parameter_t: &Vector(typechecking::NamedParameter), context: &Scope) -> &Value {
10861128 assert value.kind == ValueKind::FUNCTION
1087- value.phase = Phase::COMPILED
10881129 //let tpe = typechecking::lookup_parameters(value.node, value.state)
10891130 if typechecking::is_polymorph(value.fdef.parameter_t) {
10901131 let node = typechecking::walk_Def_with_type_argument(value.node, parameter_t, context, typechecking::make_state(value.module))
10911132 let value = node.value.def_.function.value
10921133 return value
10931134 } else {
1135+ value.phase = Phase::COMPILED
10941136 let state = typechecking::make_state(value.module)
10951137 state.context = value.node.scope
10961138 typechecking::walk_Def(value.node, state)
@@ -1101,7 +1143,6 @@ export def typecheck_function(value: &Value, parameter_t: &Vector(typechecking::
11011143
11021144export def typecheck(value: &Value) {
11031145 if not value.node { return }
1104- value.phase = Phase::COMPILED
11051146 let state = typechecking::make_state(value.module)
11061147 if value.node.kind == parser::NodeKind::DEF {
11071148 assert value.kind == ValueKind::FUNCTION
@@ -1110,9 +1151,11 @@ export def typecheck(value: &Value) {
11101151 if not typechecking::is_polymorph(fdef.parameter_t) {
11111152 state.context = value.node.scope
11121153 typechecking::walk_Def(value.node, state)
1154+ value.phase = Phase::COMPILED
11131155 value.tpe = typechecking::make_type_ref(value.node.tpe)
11141156 }
11151157 } else {
1158+ value.phase = Phase::COMPILED
11161159 typechecking::walk(value.node.parent, value.node, state)
11171160 }
11181161}
@@ -1304,9 +1347,9 @@ export def get_function_check(
13041347 }
13051348 }
13061349
1307- if value and value.phase == Phase::DEFINED and force_compile {
1308- value = typecheck_function(value, parameter_t, context)
1309- }
1350+ // if value and value.phase == Phase::DEFINED and force_compile {
1351+ // value = typecheck_function(value, parameter_t, context)
1352+ // }
13101353
13111354
13121355 return code, value
@@ -1416,9 +1459,9 @@ export def get(
14161459 var score = 0
14171460 let _, (value) = find_function(scope, id, value, id.value.identifier.types, *score, true, context)
14181461
1419- if value and force_compile and value.phase == Phase::DEFINED {
1420- value = typecheck_function(value, id.value.identifier.types, context)
1421- }
1462+ // if value and force_compile and value.phase == Phase::DEFINED {
1463+ // value = typecheck_function(value, id.value.identifier.types, context)
1464+ // }
14221465 return value
14231466 } else if value.kind == ValueKind::FUNCTION and id.parent and id.parent.kind == parser::NodeKind::PTR and value.next {
14241467 if not dry_run { ambigous_reference(id) }
@@ -1446,9 +1489,9 @@ export def get(
14461489 if id.value.identifier.types {
14471490 var score = 0
14481491 let _, (value2) = find_function(scope, id, value2, id.value.identifier.types, *score, true, context)
1449- if value2 and force_compile and value2.phase == Phase::DEFINED {
1450- value2 = typecheck_function(value2, id.value.identifier.types, context)
1451- }
1492+ // if value2 and force_compile and value2.phase == Phase::DEFINED {
1493+ // value2 = typecheck_function(value2, id.value.identifier.types, context)
1494+ // }
14521495 } else {
14531496 if force_compile and value2.phase == Phase::DEFINED {
14541497 typecheck(value2)
0 commit comments