Skip to content
← Back

src/semantic/user_defined_conversion_lookup.ghul

1
namespace Semantic is
2
use Types.Type
3
4
// Looks up a .NET user-defined conversion operator (`op_Implicit`
5
// or `op_Explicit`) reachable from a source or target type that
6
// converts between the two. Reflection pulls these down as
7
// `$op_implicit` / `$op_explicit` static methods - unreachable by
8
// name from ghūl source since `$` is not an identifier character,
9
// but addressable here because several overloads sharing that
10
// name are distinguished only by return type, which `cast`'s
11
// explicit target type supplies.
12
class USER_DEFINED_CONVERSION_LOOKUP is
13
init() is si
14
15
find(source_type: Type, target_type: Type) -> Symbols.Function? is
16
let target_non_optional = target_type.optional_inner_type ?? target_type
17
18
let candidates = Collections.LIST[Symbols.Function]()
19
20
_collect_candidates(source_type, "$op_implicit", candidates)
21
_collect_candidates(target_non_optional, "$op_implicit", candidates)
22
_collect_candidates(source_type, "$op_explicit", candidates)
23
_collect_candidates(target_non_optional, "$op_explicit", candidates)
24
25
// A built-in type the compiler constructs, such as bigint on the
26
// wasm target, takes its conversions from the static
27
// `op_implicit` and `op_explicit` the core library declares on
28
// it, which it can name where it cannot name the reflected form.
29
_collect_constructed_candidates(source_type, "op_implicit", candidates)
30
_collect_constructed_candidates(target_non_optional, "op_implicit", candidates)
31
_collect_constructed_candidates(source_type, "op_explicit", candidates)
32
_collect_constructed_candidates(target_non_optional, "op_explicit", candidates)
33
34
let best: Symbols.Function? mut = null
35
36
for f in candidates do
37
if f.arguments.count != 1 \/ !f.return_type? then
38
continue
39
fi
40
41
if !(f.return_type.matches(target_non_optional)) then
42
continue
43
fi
44
45
let parameter_type = f.arguments[0]
46
47
if !parameter_type.is_assignable_from(source_type) then
48
continue
49
fi
50
51
if !best? \/ (parameter_type.matches(source_type) /\ !(best.arguments[0].matches(source_type))) then
52
best = f
53
fi
54
od
55
56
return best
57
si
58
59
_collect_constructed_candidates(owner_type: Type, name: string, into: Collections.LIST[Symbols.Function]) is
60
if let symbol = owner_type.symbol /\ !symbol.is_reflected /\ symbol.location == Source.LOCATION.internal then
61
let start = into.count
62
63
_collect_candidates(owner_type, name, into)
64
65
let i mut = into.count - 1
66
67
while i >= start do
68
if into[i].is_instance then
69
into.remove_at(i)
70
fi
71
72
i = i - 1
73
od
74
fi
75
si
76
77
_collect_candidates(owner_type: Type, name: string, into: Collections.LIST[Symbols.Function]) is
78
let member = owner_type.find_member(name)
79
80
if let group: Symbols.FUNCTION_GROUP = member then
81
for f in group.functions do
82
into.add(f)
83
od
84
elif let single: Symbols.Function = member then
85
into.add(single)
86
fi
87
si
88
si
89
si