Skip to content
← Back

src/ir/equality_operator.ghul

1
namespace IR is
2
use Semantic.Types.Type
3
use Semantic.Symbols.Function
4
5
/// The `=~` a type declares for itself, which a target calls to compare
6
/// two values of the type that `=~` reaches through no other route.
7
class EQUALITY_OPERATOR is
8
/// The `=~` `type` declares taking a value of itself, or for a class
9
/// that declares none, an `equals` taking `object?` that the class
10
/// declares or inherits from anything but `object` itself, which is
11
/// what .NET's default comparer calls. For `object` or a trait, the
12
/// root's `equals`, called through dispatch. Null where there is none.
13
///
14
/// - type: the type whose operator to find
15
declared(type: Type) -> Function? static is
16
if let group: Semantic.Symbols.FUNCTION_GROUP = type.find_member("=~") then
17
for function in group.functions do
18
if function.arguments.count == 1 /\ function.arguments[0].is_assignable_from(type) then
19
return function
20
fi
21
od
22
fi
23
24
if type.is_value_type then
25
return null
26
fi
27
28
if let found = _equals_override(type) then
29
return found
30
fi
31
32
// A value held as `object` or as a trait can be of any class, so
33
// the root's `equals` is called, and dispatch reaches whatever
34
// the value's own class answers with.
35
if type.is_object \/ (cast Semantic.Symbols.Classy?(type.symbol)?.is_trait ?? false) then
36
return _root_equals()
37
fi
38
39
return null
40
si
41
42
_root_equals() -> Function? static is
43
let object_type = IoC.CONTAINER.instance.innate_symbol_lookup.get_object_type()
44
45
if let group: Semantic.Symbols.FUNCTION_GROUP = object_type.find_member("equals") then
46
for function in group.functions do
47
if function.is_instance /\ function.arguments.count == 1 then
48
return function
49
fi
50
od
51
fi
52
53
return null
54
si
55
56
_equals_override(type: Type) -> Function? static is
57
let object_type = IoC.CONTAINER.instance.innate_symbol_lookup.get_object_type()
58
59
if let group: Semantic.Symbols.FUNCTION_GROUP = type.find_member("equals") then
60
for function in group.functions do
61
if
62
function.is_instance /\
63
function.arguments.count == 1 /\
64
(function.arguments[0].optional_inner_type ?? function.arguments[0]).is_object /\
65
function.owner?.unspecialized_symbol != object_type.symbol.unspecialized_symbol
66
then
67
return function
68
fi
69
od
70
fi
71
72
return null
73
si
74
si
75
si