Skip to content
← Back

src/ir/program/enum_box_methods.ghul

1
namespace IR.Program is
2
use Collections.LIST
3
4
use Semantic.Symbols.Classy
5
use Semantic.Symbols.Function
6
use Semantic.Symbols.ENUM_STRUCT
7
use Semantic.Types.Type
8
use IR.Values.Value
9
10
use Source.LOCATION
11
12
/// The methods an enum's box has on a whole-program target. An enum
13
/// value is held as its integer, so a box holding one would otherwise
14
/// answer as the integer does; these make it answer as the enum.
15
class ENUM_BOX_METHODS is
16
/// The `to_string` of `enum`, with the body that gives a value's
17
/// member names as .NET does, or null where the core library cannot
18
/// name the enum's values. The method overrides the root's, so a
19
/// boxed value reached through `object` reads as its members.
20
///
21
/// - enum: the enum the box holds a value of
22
to_string_for(`enum: ENUM_STRUCT) -> (method: Function, body: LIST[Value])? static is
23
let lookup = IoC.CONTAINER.instance.innate_symbol_lookup
24
let enum_type = Semantic.Types.NAMED(`enum)
25
26
let text = INTERPOLATION_CALLS.enum_text(Values.Load.VALUE_SELF(`enum, enum_type), enum_type)
27
28
if !text? then
29
return null
30
fi
31
32
let object = cast Classy?(lookup.get_object_type().symbol)
33
let overridee = if let o = object then _member(o, "to_string") else null fi
34
35
if !overridee? then
36
return null
37
fi
38
39
let method = Semantic.Symbols.INSTANCE_METHOD(LOCATION.internal, LOCATION.internal, `enum, "to_string", `enum)
40
41
method.set_arguments(LIST[string](), LIST[Type]())
42
method.set_return_type(lookup.get_string_type())
43
method.add_overridee(overridee)
44
45
return (method, LIST[Value]([text, Values.RET()]))
46
si
47
48
// The method of `owner` named `name` that takes no arguments.
49
_member(owner: Classy, name: string) -> Function? static is
50
let symbol = owner.find_direct(name)
51
52
if let function: Function = symbol /\ function.arguments.count == 0 then
53
return function
54
elif let group: Semantic.Symbols.FUNCTION_GROUP = symbol then
55
for function in group.functions do
56
if function.arguments.count == 0 then
57
return function
58
fi
59
od
60
fi
61
62
return null
63
si
64
si
65
si