Skip to content
← Back

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

1
namespace Syntax.Process is
2
use System.Exception
3
use Ghul.Pipes.any
4
5
use IR.Values.Value
6
use Semantic.Types.Type
7
8
// `[element for p in source if condition ...]`. The clauses are
9
// compiled outermost first, each inside the scope of the ones before
10
// it: a `for` clause types its source and pattern the way a `for`
11
// statement does, and an `if` clause narrows everything after it the
12
// way an `if` narrows its then-arm. The element comes last, and its
13
// type is the element type of the resulting array. generate-il emits
14
// the loops into the BLOCK left as the node's value.
15
partial COMPILE_EXPRESSIONS is
16
pre(comprehension: Trees.Expressions.COMPREHENSION) -> bool is
17
super.pre(comprehension)
18
19
try
20
if comprehension.is_lazy then
21
_compile_lazy_comprehension(comprehension)
22
else
23
_compile_comprehension(comprehension)
24
fi
25
catch ex: Exception
26
comprehension.compile_expressions_state.value = IR.Values.DUMMY(Semantic.Types.ERROR(), comprehension.location)
27
_logger.exception(comprehension.location, ex, "exception compiling list comprehension")
28
yrt
29
30
return true
31
si
32
33
visit(comprehension: Trees.Expressions.COMPREHENSION) is
34
super.visit(comprehension)
35
si
36
37
// `{element for p in source ...}`: the clauses are compiled exactly
38
// as an array comprehension's are, but inside the generator literal
39
// the comprehension owns, so its iterators and loop variables become
40
// fields of that literal's frame and survive between elements. The
41
// literal returns Pipe[T] for the element's type T, and the
42
// comprehension's value is a call to it: each evaluation makes a new
43
// pipe, and each read of that pipe re-runs the clauses.
44
_compile_lazy_comprehension(comprehension: Trees.Expressions.COMPREHENSION) is
45
let generator = cast Semantic.Symbols.Closure?(comprehension.scope)!
46
let state_machine = Semantic.Symbols.state_machine_for(generator)!
47
48
generator.map_type_arguments()
49
50
if let dropped = state_machine.reset() then
51
_composite_spill_state.drop_owned_by(dropped)
52
fi
53
54
// The literal takes no arguments; declaring that is what lets its
55
// function type be built once its return type is known.
56
generator.set_arguments(Collections.LIST[string](), Collections.LIST[Type]())
57
58
if let function = _symbol_table.current_function,
59
origin = Semantic.RESULT_ORIGINS.origin_for(comprehension, comprehension.location, cast Semantic.Scope(function), "$comprehension@{comprehension.location.start_line}:{comprehension.location.start_column}") then
60
origin.description = "the type of this comprehension"
61
62
generator.set_return_type(Semantic.Types.INFERRED_VARIABLE_TYPE(origin))
63
else
64
generator.set_return_type(Semantic.Types.INFERRED_RETURN_TYPE())
65
fi
66
67
_symbol_table.current_closure_context.add_closure(generator)
68
69
let element_constraint = Semantic.Symbols.sequence_element_type(comprehension.expected_type)
70
71
if let constraint = element_constraint then
72
comprehension.element.set_expected_type(
73
constraint,
74
"element type {0} not compatible with comprehension type {1}"
75
)
76
else
77
comprehension.element.clear_expected_type()
78
fi
79
80
let outside = _flow.current_env.copy()
81
82
_flow.push_literal_frame()
83
84
generator.enter_literal_body()
85
_compile_clause(comprehension, comprehension.first)
86
generator.leave_literal_body()
87
88
generator.literal_body_impure = _flow.pop_literal_frame()
89
90
_flow.set_env(outside)
91
92
COMPREHENSION_RESTRICTIONS(_logger).check(comprehension, loop_value_frames.count > 0)
93
94
let value_type = comprehension.element.value?.type
95
96
let element_types = COMPREHENSION_ELEMENT_TYPE()
97
98
let element_type: Type =
99
if let resolved = element_types.element_type(element_constraint, value_type) then
100
resolved
101
else
102
_logger.error(comprehension.element.location, "cannot infer the element type of this comprehension")
103
Semantic.Types.ERROR()
104
fi
105
106
if !element_types.fits(element_constraint, value_type) then
107
_logger.error(
108
comprehension.element.location,
109
"element type {value_type} not compatible with comprehension type {element_type}"
110
)
111
fi
112
113
comprehension.element_type = element_type
114
115
let pipe_type = if !element_type.is_error then Semantic.Symbols.generator_return_type(element_type) else null fi
116
117
if !pipe_type? then
118
generator.set_return_type(Semantic.Types.ERROR())
119
generator.unmap_type_arguments()
120
comprehension.compile_expressions_state.value = IR.Values.DUMMY(Semantic.Types.ERROR(), comprehension.location)
121
122
return
123
fi
124
125
generator.set_return_type(pipe_type)
126
127
generator.unmap_type_arguments()
128
129
let delegate = generator.load(comprehension.location, _symbol_loader)
130
131
// Loading the literal is what gives it a captures frame when it
132
// needs one, and the generator frame's reference to that is
133
// settled when the frame is declared, so the frame is declared
134
// after the load, as a generator literal's is.
135
if let frame = state_machine.frame then
136
frame.declare()
137
fi
138
139
comprehension.compile_expressions_state.value =
140
IR.Values.Call.CLOSURE(
141
delegate,
142
pipe_type,
143
false,
144
delegate.type!,
145
Collections.LIST[Value]()
146
)
147
si
148
149
pre(clause: Trees.Expressions.COMPREHENSION_FOR) -> bool => true
150
visit(clause: Trees.Expressions.COMPREHENSION_FOR) is si
151
152
pre(clause: Trees.Expressions.COMPREHENSION_IF) -> bool => true
153
visit(clause: Trees.Expressions.COMPREHENSION_IF) is si
154
155
_compile_comprehension(comprehension: Trees.Expressions.COMPREHENSION) is
156
comprehension.type_expression.walk(self)
157
158
let explicit_type =
159
if !isa Trees.TypeExpressions.INFER(comprehension.type_expression) then
160
comprehension.type_expression.type
161
else
162
null
163
fi
164
165
let element_types = COMPREHENSION_ELEMENT_TYPE()
166
let element_constraint = element_types.constraint(explicit_type, comprehension.expected_type)
167
168
if let constraint = element_constraint then
169
comprehension.element.set_expected_type(
170
constraint,
171
"element type {0} not compatible with list comprehension type {1}"
172
)
173
else
174
comprehension.element.clear_expected_type()
175
fi
176
177
// Nothing the clauses prove about a variable outlives the
178
// comprehension.
179
let outside = _flow.current_env.copy()
180
181
_compile_clause(comprehension, comprehension.first)
182
183
_flow.set_env(outside)
184
185
COMPREHENSION_RESTRICTIONS(_logger).check(comprehension, loop_value_frames.count > 0)
186
187
let value_type = comprehension.element.value?.type
188
189
let element_type: Type =
190
if let resolved = element_types.element_type(element_constraint, value_type) then
191
resolved
192
else
193
_logger.error(comprehension.element.location, "cannot infer the element type of this list comprehension")
194
Semantic.Types.ERROR()
195
fi
196
197
if !element_types.fits(element_constraint, value_type) then
198
_logger.error(
199
comprehension.element.location,
200
"element type {value_type} not compatible with list comprehension type {element_type}"
201
)
202
fi
203
204
let array_type =
205
if let explicit = explicit_type then
206
explicit
207
else
208
_innate_symbol_lookup.get_array_type(element_type)
209
fi
210
211
comprehension.element_type = element_type
212
213
if !element_type.is_error then
214
_resolve_comprehension_list(comprehension, element_type)
215
_try_presize_comprehension(comprehension)
216
fi
217
218
comprehension.compile_expressions_state.value = IR.Values.BLOCK(array_type)
219
si
220
221
// One `for` clause and no `if` means one element per source
222
// element, so where the source's count is known before the loop the
223
// array can be allocated at that length and filled by index.
224
_try_presize_comprehension(comprehension: Trees.Expressions.COMPREHENSION) is
225
let clause = comprehension.first
226
227
if clause.inner? then
228
return
229
fi
230
231
if let source_value = clause.source.value, source_type = source_value.type then
232
let source = IR.LOCAL(".comprehension_source.{_next_fused_local_id()}", source_type)
233
let load = IR.Values.Load.TEMP(source)
234
235
if let count = known_counts.count_of(clause.source, load, source_type, comprehension.location) then
236
comprehension.presize_count = count
237
comprehension.presize_source = source
238
fi
239
fi
240
si
241
242
_resolve_comprehension_list(comprehension: Trees.Expressions.COMPREHENSION, element_type: Type) is
243
let list_type = _innate_symbol_lookup.get_list_type(element_type)
244
245
if !list_type? then
246
_logger.error(comprehension.location, "cannot resolve the list type a list comprehension is built with")
247
return
248
fi
249
250
comprehension.list_type = list_type
251
comprehension.list_constructor = _resolve_ctor_by_arity(list_type, 0)
252
comprehension.list_add = _resolve_member_by_arity(list_type, "add", 1)
253
comprehension.list_to_array = _resolve_member_by_arity(list_type, "to_array", 0)
254
si
255
256
_compile_clause(comprehension: Trees.Expressions.COMPREHENSION, clause: Trees.Expressions.COMPREHENSION_CLAUSE?) is
257
if !clause? then
258
comprehension.element.walk(self)
259
return
260
fi
261
262
if let for_clause: Trees.Expressions.COMPREHENSION_FOR = clause then
263
_compile_for_clause(comprehension, for_clause)
264
elif let if_clause: Trees.Expressions.COMPREHENSION_IF = clause then
265
_compile_if_clause(comprehension, if_clause)
266
fi
267
si
268
269
_compile_for_clause(comprehension: Trees.Expressions.COMPREHENSION, clause: Trees.Expressions.COMPREHENSION_FOR) is
270
super.pre(clause)
271
272
try
273
let source = clause.source
274
let variable = clause.variable
275
let type: Type mut = Semantic.Types.ERROR()
276
277
if !source.is_poisoned then
278
source.walk(self)
279
280
record_iterable_constraint(source)
281
282
if let sv = source.value, sv_type = sv.type /\ set_iterator_for(clause, sv_type, false) then
283
check_receiver_present(source)
284
285
if _never_ends(sv) /\ !comprehension.is_lazy then
286
_logger.warn(
287
source.location,
288
"unbounded-comprehension-source",
289
"list comprehension over a sequence that never ends"
290
)
291
fi
292
293
let current = clause.read_current!.return_type!
294
295
if let
296
te = variable.type_expression,
297
te_type = te.type /\
298
!isa Trees.TypeExpressions.INFER(te)
299
then
300
if te_type.is_assignable_from(current) then
301
type = te_type
302
else
303
_logger.error(variable.location, "type mismatch")
304
fi
305
else
306
type = current
307
fi
308
fi
309
fi
310
311
if !variable.is_poisoned then
312
set_symbol_type(variable.left, type)
313
declare_state_machine_local_fields(variable.left)
314
fi
315
316
// The clauses after this one run once per element, so a
317
// narrow on anything they write cannot be assumed on the
318
// way round.
319
_flow.set_env(_loops.loop_kept_env(comprehension))
320
321
_compile_clause(comprehension, clause.inner)
322
finally
323
super.visit(clause)
324
yrt
325
si
326
327
// Whether `value` is a `Ghul.Pipes` source that never ends, or one
328
// seen through stages that keep every element coming: map,
329
// filter, index, skip and memo. Anything else - take, or a
330
// function this does not know - may end, and is not reported.
331
_never_ends(value: Value) -> bool is
332
if let call: IR.Values.Call.GLOBAL = value then
333
let function = call.function.root_specialized_from
334
335
if _innate_symbol_lookup.get_pipes_unbounded_sources().contains(function) then
336
return true
337
fi
338
339
let role: string mut
340
341
if
342
_innate_symbol_lookup.get_pipes_fusion_functions().try_get_value(function, role ref) /\
343
["map", "filter", "index", "skip", "memo"] |> any(r => r =~ role) /\
344
call.arguments.count > 0
345
then
346
return _never_ends(call.arguments[0])
347
fi
348
fi
349
350
return false
351
si
352
353
_compile_if_clause(comprehension: Trees.Expressions.COMPREHENSION, clause: Trees.Expressions.COMPREHENSION_IF) is
354
let condition = clause.condition
355
let epoch = _flow.heap_epoch
356
let mark = _flow.crossing_mark
357
358
condition.walk(self)
359
360
if Value.check_is_consumable(_logger, condition.location, condition.value) then
361
if !condition.value!.type!.matches(_innate_symbol_lookup.get_bool_type()) then
362
_logger.error(condition.location, "{comprehension.description} condition must be bool")
363
fi
364
fi
365
366
let facts = _condition_analyzer.analyze_condition(condition, _flow.current_env)
367
368
if _flow.heap_killed_since(epoch) then
369
facts.then_env.drop_heap_facts()
370
else
371
_flow.adopt_crossings_since(facts.then_env, mark)
372
fi
373
374
_flow.set_env(facts.then_env)
375
376
_compile_clause(comprehension, clause.inner)
377
si
378
si
379
si