Skip to content
← Back

src/ir/program/frame_member_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.STATE_MACHINE_FRAME
7
use Semantic.Symbols.ASYNC_STATE_MACHINE_FRAME
8
use Semantic.Types.Type
9
10
use Source.LOCATION
11
12
/// The methods a generator's frame has on a whole-program target. The frame
13
/// declares no symbol for its iteration members, which the lowering
14
/// builds bodies for, so each is given one here: a method of the frame
15
/// overriding the trait member it implements, which dispatch reaches as
16
/// it reaches any other implementation.
17
class FRAME_MEMBER_METHODS is
18
/// The method `member` of `frame` is, or null where no trait the
19
/// frame implements declares a member it stands for.
20
method_for(frame: STATE_MACHINE_FRAME, member: FrameMember) -> Function? static is
21
let lookup = IoC.CONTAINER.instance.innate_symbol_lookup
22
23
let iterator = _definition(lookup.get_unspecialized_iterator_type())
24
let iterable = _definition(lookup.get_unspecialized_iterable_type())
25
26
if !iterator? \/ !iterable? then
27
return null
28
fi
29
30
let (owner, name, return_type) =
31
case member
32
when FrameMember.MOVE_NEXT then
33
(iterator, "move_next", lookup.get_bool_type())
34
when FrameMember.GET_CURRENT then
35
(iterator, "current", frame.current_field.type!)
36
when FrameMember.GET_ENUMERATOR then
37
(iterable, "iterator", Semantic.Types.GENERIC(LOCATION.internal, iterator, [frame.class_element_type]))
38
when FrameMember.DISPOSE then
39
(iterator, "dispose", lookup.get_void_type())
40
else
41
return null
42
esac
43
44
let overridee = _member(owner, name)
45
46
if !overridee? then
47
return null
48
fi
49
50
let method = Semantic.Symbols.INSTANCE_METHOD(LOCATION.internal, LOCATION.internal, frame, name, frame)
51
52
method.set_arguments(LIST[string](), LIST[Type]())
53
method.set_return_type(return_type)
54
method.add_overridee(overridee)
55
56
return method
57
si
58
59
/// The method `member` of the frame of an asynchronous function is:
60
/// one of the two the frame implements `IAsyncStateMachine` with, or
61
/// null where the core library declares no such trait.
62
method_for(frame: ASYNC_STATE_MACHINE_FRAME, member: FrameMember) -> Function? static is
63
let lookup = IoC.CONTAINER.instance.innate_symbol_lookup
64
65
let state_machine_type = lookup.get_async_state_machine_interface_type()
66
67
if !state_machine_type? then
68
return null
69
fi
70
71
let state_machine = _definition(state_machine_type)
72
73
if !state_machine? then
74
return null
75
fi
76
77
let (name, argument_names, argument_types) =
78
case member
79
when FrameMember.MOVE_NEXT then
80
("move_next", LIST[string](), LIST[Type]())
81
when FrameMember.SET_STATE_MACHINE then
82
("set_state_machine", LIST[string](["state_machine"]), LIST[Type]([state_machine_type]))
83
else
84
return null
85
esac
86
87
let overridee = _member_of_arity(state_machine, name, argument_types.count)
88
89
if !overridee? then
90
return null
91
fi
92
93
let method = Semantic.Symbols.INSTANCE_METHOD(LOCATION.internal, LOCATION.internal, frame, name, frame)
94
95
method.set_arguments(argument_names, argument_types)
96
method.set_return_type(lookup.get_void_type())
97
method.add_overridee(overridee)
98
99
return method
100
si
101
102
_member_of_arity(owner: Classy, name: string, arity: int) -> Function? static is
103
let symbol = owner.find_direct(name)
104
105
if let function: Function = symbol /\ function.arguments.count == arity then
106
return function
107
elif let group: Semantic.Symbols.FUNCTION_GROUP = symbol then
108
for function in group.functions do
109
if function.arguments.count == arity then
110
return function
111
fi
112
od
113
fi
114
115
return null
116
si
117
118
_definition(type: Type) -> Classy? static =>
119
cast Classy?(type.symbol?.unspecialized_symbol)
120
121
// The parameterless method `name` of `owner`, or the getter of its
122
// property `name`.
123
_member(owner: Classy, name: string) -> Function? static is
124
let symbol = owner.find_direct(name)
125
126
if let property: Semantic.Symbols.Property = symbol then
127
return property.read_function
128
elif let function: Function = symbol /\ function.arguments.count == 0 then
129
return function
130
elif let group: Semantic.Symbols.FUNCTION_GROUP = symbol then
131
for function in group.functions do
132
if function.arguments.count == 0 then
133
return function
134
fi
135
od
136
fi
137
138
return null
139
si
140
si
141
si