Skip to content
← Back

src/syntax/process/compile-expressions/pack_wrap_builder.ghul

1
namespace Syntax.Process is
2
use Semantic.Types.Type
3
4
use IR.Values.Value
5
6
// Presents a function value of one argument pack shape in the other.
7
// A function of two or more parameters going into a formal that takes
8
// the pack as one tuple is wrapped in a thunk that takes the tuple,
9
// unpacks it and calls the function; the other direction packs the
10
// elements into the tuple the function takes.
11
//
12
// A thunk is a static closure symbol with no literal behind it, whose
13
// body is emitted from the description recorded on it. Its first
14
// parameter is the function it stands for, and its delegate is built
15
// closed over that parameter, so the value is evaluated once, where
16
// it stands, and nothing is captured. A pack marked past the
17
// function's own type - `make: (int) -> T.. -> U` - takes a chain:
18
// each thunk above the pack calls its function and returns the next
19
// thunk's delegate, closed over the result.
20
//
21
// Nothing here touches a syntax tree. A call is compiled once for
22
// every walk of the body it stands in, so the thunks made for a given
23
// argument of a given call are kept and reused rather than declared
24
// again.
25
class PACK_WRAP_BUILDER is
26
_symbol_table: Semantic.SYMBOL_TABLE
27
_symbol_loader: Semantic.SYMBOL_LOADER
28
_innate_symbol_lookup: Semantic.Lookups.InnateSymbolLookup
29
30
_thunks: Collections.MAP[Trees.Node, Collections.MAP[int, Collections.LIST[Semantic.Symbols.Closure]]]
31
32
_index: int
33
34
init(
35
symbol_table: Semantic.SYMBOL_TABLE,
36
symbol_loader: Semantic.SYMBOL_LOADER,
37
innate_symbol_lookup: Semantic.Lookups.InnateSymbolLookup
38
) is
39
_symbol_table = symbol_table
40
_symbol_loader = symbol_loader
41
_innate_symbol_lookup = innate_symbol_lookup
42
43
_thunks = Collections.MAP[Trees.Node, Collections.MAP[int, Collections.LIST[Semantic.Symbols.Closure]]]()
44
si
45
46
// The type `function_type` has once its parameters after the
47
// first `fixed_count` are taken as one tuple. Absent where the
48
// type is not one this applies to, or where a parameter is not
49
// settled enough to be a tuple element.
50
packed_type(function_type: Type?, fixed_count: int) -> Type? =>
51
packed_type(function_type, fixed_count, false)
52
53
// As above. `provisional` lets a parameter still being inferred
54
// stand in the tuple, for a value that nothing has typed yet and
55
// that a later use of the call's result will.
56
packed_type(function_type: Type?, fixed_count: int, provisional: bool) -> Type? is
57
if !function_type? \/ !function_type.is_function then
58
return null
59
fi
60
61
let count = Semantic.ARGUMENT_PACK.parameter_count(function_type)
62
let arity = count - fixed_count
63
64
if arity < 2 \/ arity > Semantic.ARGUMENT_PACK.MAXIMUM_ARITY then
65
return null
66
fi
67
68
let parameters = Collections.LIST[Type]()
69
let elements = Collections.LIST[Type]()
70
71
for i in 0..count do
72
let parameter = function_type.arguments[i]
73
74
// A type parameter of the function being compiled, or of
75
// one enclosing it, names a type as definitely as a
76
// concrete one does. `is_wild` answers true for those as
77
// well as for a parameter whose index means something
78
// only inside an overload specialization that may already
79
// have been discarded, and it is the second that has no
80
// tuple element to be.
81
if
82
parameter.is_error \/
83
(parameter.contains_inferred /\ !provisional) \/
84
parameter.has_function_generic_argument_foreign_to(_symbol_table.current_function)
85
then
86
return null
87
fi
88
89
if i < fixed_count then
90
parameters.add(parameter)
91
else
92
elements.add(parameter)
93
fi
94
od
95
96
parameters.add(_innate_symbol_lookup.get_tuple_type(elements, null))
97
98
parameters.add(
99
if function_type.is_action then
100
_innate_symbol_lookup.get_void_type()
101
else
102
function_type.arguments[count]
103
fi
104
)
105
106
return _innate_symbol_lookup.get_function_type(parameters, function_type.is_pure_function)
107
si
108
109
// `function_type` with the function `depth` returns along its
110
// spine packed as above, and every hop before it kept as it is.
111
// Absent where the spine is shorter than that.
112
packed_spine(function_type: Type?, depth: int, fixed_count: int) -> Type? is
113
if depth == 0 then
114
return packed_type(function_type, fixed_count)
115
fi
116
117
if !function_type? \/ !function_type.is_function \/ function_type.is_action then
118
return null
119
fi
120
121
let count = Semantic.ARGUMENT_PACK.parameter_count(function_type)
122
123
let inner = packed_spine(function_type.arguments[count], depth - 1, fixed_count)
124
125
if !inner? then
126
return null
127
fi
128
129
let parameters = Collections.LIST[Type]()
130
131
for i in 0..count do
132
parameters.add(function_type.arguments[i])
133
od
134
135
parameters.add(inner)
136
137
return _innate_symbol_lookup.get_function_type(parameters, function_type.is_pure_function)
138
si
139
140
// The type `function_type` has once the tuple it takes after its
141
// first `fixed_count` parameters is spread out as parameters of
142
// its own. Absent where the type is not one this applies to.
143
spread_type(function_type: Type?, fixed_count: int) -> Type? is
144
if !function_type? \/ !function_type.is_function then
145
return null
146
fi
147
148
let count = Semantic.ARGUMENT_PACK.parameter_count(function_type)
149
150
if count != fixed_count + 1 then
151
return null
152
fi
153
154
let tuple = function_type.arguments[fixed_count]
155
156
if
157
!(tuple.is_value_tuple \/ isa Semantic.Types.TUPLE(tuple)) \/
158
tuple.arguments.count < 2 \/
159
tuple.arguments.count > Semantic.ARGUMENT_PACK.MAXIMUM_ARITY \/
160
tuple.contains_inferred
161
then
162
return null
163
fi
164
165
let parameters = Collections.LIST[Type]()
166
167
for i in 0..fixed_count do
168
parameters.add(function_type.arguments[i])
169
od
170
171
for element in tuple.arguments do
172
parameters.add(element)
173
od
174
175
parameters.add(
176
if function_type.is_action then
177
_innate_symbol_lookup.get_void_type()
178
else
179
function_type.arguments[count]
180
fi
181
)
182
183
return _innate_symbol_lookup.get_function_type(parameters, function_type.is_pure_function)
184
si
185
186
// Wraps `value`, a function of the pack's elements, as a function
187
// of their tuple. `key` and `index` say which argument of which
188
// call this is.
189
unpack(
190
key: Trees.Node,
191
index: int,
192
location: Source.LOCATION,
193
value: Value,
194
fixed_count: int
195
) -> Value? =>
196
unpack(key, index, location, value, fixed_count, 0)
197
198
// As above, for a pack `depth` returns past the value's own type:
199
// the value is a function that, called that many times, gives the
200
// function of the pack's elements.
201
unpack(
202
key: Trees.Node,
203
index: int,
204
location: Source.LOCATION,
205
value: Value,
206
fixed_count: int,
207
depth: int
208
) -> Value? is
209
let callee_type = value.type
210
211
if !callee_type? then
212
return null
213
fi
214
215
let presented = packed_spine(callee_type, depth, fixed_count)
216
217
if !presented? then
218
return null
219
fi
220
221
let enclosing = _symbol_table.current_function
222
223
if !enclosing? then
224
return null
225
fi
226
227
let thunks = _thunks_for(key, index, location, enclosing, depth + 1)
228
229
if !thunks? then
230
return null
231
fi
232
233
// The chain is built from the pack upwards: the thunk at depth
234
// zero packs, and each above it hands its result down.
235
let hop_types = _hops(callee_type, depth)
236
let hop_presented = _hops(presented, depth)
237
238
let innermost_type = hop_types[depth]
239
240
let elements = Collections.LIST[Type]()
241
242
for i in fixed_count..Semantic.ARGUMENT_PACK.parameter_count(innermost_type) do
243
elements.add(innermost_type.arguments[i])
244
od
245
246
let target = if depth == 0 then _direct_target(value) else null fi
247
248
_describe(
249
thunks[0],
250
location,
251
innermost_type,
252
hop_presented[depth],
253
Semantic.Symbols.PACK_THUNK(true, fixed_count, elements, innermost_type, target)
254
)
255
256
for d in 1..depth + 1 do
257
_describe(
258
thunks[d],
259
location,
260
hop_types[depth - d],
261
hop_presented[depth - d],
262
Semantic.Symbols.PACK_THUNK.chain(hop_types[depth - d], d, thunks[d - 1], hop_presented[depth - d + 1])
263
)
264
od
265
266
return _present(thunks[depth], location, value, presented)
267
si
268
269
// Wraps `value`, a function of the pack's tuple, as a function of
270
// its elements. `callee_type` is the type it is called at, which
271
// may be more settled than the value's own.
272
pack(
273
key: Trees.Node,
274
index: int,
275
location: Source.LOCATION,
276
value: Value,
277
callee_type: Type,
278
fixed_count: int
279
) -> Value? is
280
let presented = spread_type(callee_type, fixed_count)
281
282
if !presented? then
283
return null
284
fi
285
286
let enclosing = _symbol_table.current_function
287
288
if !enclosing? then
289
return null
290
fi
291
292
let thunks = _thunks_for(key, index, location, enclosing, 1)
293
294
if !thunks? then
295
return null
296
fi
297
298
let elements = Collections.LIST[Type]()
299
300
for element in callee_type.arguments[fixed_count].arguments do
301
elements.add(element)
302
od
303
304
_describe(
305
thunks[0],
306
location,
307
callee_type,
308
presented,
309
Semantic.Symbols.PACK_THUNK(false, fixed_count, elements, callee_type, _direct_target(value))
310
)
311
312
return _present(thunks[0], location, value, presented)
313
si
314
315
// `function_type` and the type each of `depth` returns along its
316
// spine gives, outermost first.
317
_hops(function_type: Type, depth: int) -> Collections.LIST[Type] is
318
let result = Collections.LIST[Type]()
319
320
let hop mut = function_type
321
322
result.add(hop)
323
324
for _ in 0..depth do
325
hop = hop.arguments[hop.arguments.count - 1]
326
327
result.add(hop)
328
od
329
330
return result
331
si
332
333
// Gives `thunk` the parameters of `presented`, its function as a
334
// parameter before them where it is not called by name, its
335
// return, and `description` as what its body does.
336
_describe(
337
thunk: Semantic.Symbols.Closure,
338
location: Source.LOCATION,
339
callee_type: Type,
340
presented: Type,
341
description: Semantic.Symbols.PACK_THUNK
342
) is
343
let parameter_types = Collections.LIST[Type]()
344
let parameter_names = Collections.LIST[string]()
345
346
if description.takes_callee then
347
parameter_types.add(callee_type)
348
parameter_names.add("$pack_callee")
349
350
_parameter(thunk, location, "$pack_callee").set_type(callee_type)
351
fi
352
353
for i in 0..Semantic.ARGUMENT_PACK.parameter_count(presented) do
354
parameter_types.add(presented.arguments[i])
355
parameter_names.add("$pack_{i}")
356
357
_parameter(thunk, location, "$pack_{i}").set_type(presented.arguments[i])
358
od
359
360
thunk.arguments = parameter_types
361
thunk.argument_names = parameter_names
362
363
thunk.set_return_type(
364
if presented.is_action then
365
_innate_symbol_lookup.get_void_type()
366
else
367
presented.arguments[presented.arguments.count - 1]
368
fi
369
)
370
371
for t in parameter_types do
372
_record_type_arguments(thunk, t)
373
od
374
375
_record_type_arguments(thunk, thunk.return_type!)
376
377
thunk.pack_thunk = description
378
si
379
380
// The presented value: `thunk`'s delegate, closed over `value`
381
// where the thunk takes its function as a parameter, and built
382
// once and kept where it calls one by name.
383
_present(
384
thunk: Semantic.Symbols.Closure,
385
location: Source.LOCATION,
386
value: Value,
387
presented: Type
388
) -> Value is
389
let block = IR.Values.PACK_WRAP(presented, value)
390
391
if thunk.pack_thunk!.takes_callee then
392
block.add(thunk.load_closed_over(location, value, presented))
393
else
394
block.add(thunk.load(location, _symbol_loader))
395
fi
396
397
return block
398
si
399
400
_direct_target(value: Value) -> Semantic.Symbols.Function? is
401
let function = value.referenced_function
402
403
if
404
!function? \/
405
function.is_instance \/
406
function.is_generic \/
407
isa Semantic.Symbols.Closure(function) \/
408
!(isa Semantic.Symbols.GLOBAL_FUNCTION(function) \/ isa Semantic.Symbols.STATIC_METHOD(function))
409
then
410
return null
411
fi
412
413
return function
414
si
415
416
_parameter(
417
thunk: Semantic.Symbols.Closure,
418
location: Source.LOCATION,
419
name: string
420
) -> Semantic.Symbols.Variable is
421
if let existing: Semantic.Symbols.Variable = thunk.find_direct(name) then
422
return existing
423
fi
424
425
thunk.start_declaring_arguments()
426
427
let declared = cast Semantic.Symbols.Variable?(thunk.declare_variable(location, name, false, null))!
428
429
thunk.end_declaring_arguments()
430
431
declared.mark_synthesized()
432
declared.define()
433
434
return declared
435
si
436
437
_record_type_arguments(thunk: Semantic.Symbols.Closure, type: Type) is
438
let collected = Collections.LIST[Semantic.Symbols.Symbol]()
439
440
Semantic.INFERENCE_HELPERS.collect_method_level_type_variables(type, collected)
441
442
for u in collected do
443
thunk.add_type_argument_reference(u)
444
od
445
si
446
447
// The thunks for argument `index` of the call `key` is, `count` of
448
// them from the pack upwards, declared on first use and kept.
449
_thunks_for(
450
key: Trees.Node,
451
index: int,
452
location: Source.LOCATION,
453
enclosing: Semantic.Symbols.Function,
454
count: int
455
) -> Collections.LIST[Semantic.Symbols.Closure]? is
456
let for_key: Collections.MAP[int, Collections.LIST[Semantic.Symbols.Closure]] mut
457
458
if !_thunks.try_get_value(key, for_key ref) then
459
for_key = Collections.MAP[int, Collections.LIST[Semantic.Symbols.Closure]]()
460
_thunks[key] = for_key
461
fi
462
463
let thunks: Collections.LIST[Semantic.Symbols.Closure] mut
464
465
if !for_key.try_get_value(index, thunks ref) then
466
thunks = Collections.LIST[Semantic.Symbols.Closure]()
467
for_key[index] = thunks
468
fi
469
470
while thunks.count < count do
471
let thunk =
472
cast Semantic.Symbols.Closure?(
473
enclosing.declare_closure(
474
location,
475
"$pack_thunk_{_index}",
476
cast Semantic.Scope?(_symbol_table.current_closure_context)!,
477
_symbol_table.current_scope,
478
false,
479
null
480
)
481
)
482
483
_index = _index + 1
484
485
if !thunk? then
486
return null
487
fi
488
489
thunk.mark_synthesized()
490
491
_symbol_table.current_closure_context.add_closure(thunk)
492
493
thunks.add(thunk)
494
od
495
496
return thunks
497
si
498
si
499
si