Skip to content
← Back

src/syntax/process/case_pattern_domain.ghul

1
namespace Syntax.Process is
2
use Source
3
use Semantic.Types.Type
4
use Semantic.Symbols.Classy
5
use Semantic.Symbols.ENUM_STRUCT_MEMBER
6
7
enum ConstructorKind is
8
// Absence, the extra alternative an optional type carries.
9
NULL,
10
FALSE,
11
TRUE,
12
// A union variant, a subclass of a closed root, or the
13
// constructible root itself.
14
CLASSY,
15
ENUM_MEMBER,
16
// The single, irrefutable alternative of a type that
17
// destructures — a tuple, or anything else with a positional
18
// shape.
19
PRODUCT,
20
// A literal drawn from a domain too large to enumerate. Never
21
// completes a domain, and never equal to any other.
22
OPAQUE,
23
si
24
25
// One alternative a value of some domain can take.
26
//
27
// Identity is the object, never the name — two constructors are
28
// the same alternative when they are the same instance. A domain
29
// builds its alternatives once and every pattern that selects one
30
// refers back to that instance, so `==` answers correctly and
31
// nothing is keyed on rendered text. `name` exists only to
32
// describe a missing case in a diagnostic.
33
class PATTERN_CONSTRUCTOR is
34
kind: ConstructorKind public
35
name: string public
36
37
// The type a sub-pattern of this alternative is matched
38
// against, when the alternative carries fields. Null for a
39
// leaf alternative.
40
alternative_type: Type? public
41
42
// Set for CLASSY, and compared unspecialized, so an arm
43
// ascribing a constructed `Result.OK[int, string]` selects
44
// the same alternative the union declared.
45
classy: Classy? public
46
47
// Set for ENUM_MEMBER. The member's own symbol is the key; a
48
// literal arm is resolved to it once, by value, at the point
49
// the pattern is built.
50
enum_member: ENUM_STRUCT_MEMBER? public
51
52
_classifier: CASE_DOMAIN_CLASSIFIER?
53
_field_domains: Collections.List[PATTERN_DOMAIN?]?
54
55
// The session view the constructor's alternative was selected
56
// under: a nested destructure classifies its fields against the
57
// same cell's view rather than the whole session.
58
_site: Source.LOCATION?
59
60
// An alternative whose fields are described by a type the
61
// classifier will read when, and only when, a pattern
62
// descends into them.
63
init(
64
kind: ConstructorKind,
65
name: string,
66
alternative_type: Type?,
67
classifier: CASE_DOMAIN_CLASSIFIER
68
) is
69
super.init()
70
71
self.kind = kind
72
self.name = name
73
self.alternative_type = alternative_type
74
self._classifier = classifier
75
si
76
77
set_site(site: Source.LOCATION?) is
78
self._site = site
79
si
80
81
// An alternative whose field domains are already known.
82
init(kind: ConstructorKind, name: string, field_domains: Collections.List[PATTERN_DOMAIN?]) is
83
super.init()
84
85
self.kind = kind
86
self.name = name
87
self._field_domains = field_domains
88
si
89
90
set_classy(classy: Classy) is
91
self.classy = classy
92
si
93
94
set_enum_member(member: ENUM_STRUCT_MEMBER) is
95
self.enum_member = member
96
si
97
98
// The domain of each field, read on first use. Deferred
99
// because a type reaches itself through its own fields
100
// whenever it is recursive, and the walk only ever descends
101
// as far as a written pattern goes.
102
field_domains: Collections.List[PATTERN_DOMAIN?] is
103
if !_field_domains? then
104
_field_domains = _classifier!.field_domains_of(alternative_type, _site)
105
fi
106
107
return _field_domains
108
si
109
110
arity: int => field_domains.count
111
si
112
113
// The finite set of alternatives a value of a type can take. A
114
// type whose values cannot be enumerated this way — `int`,
115
// `string`, an open class hierarchy — has no domain, and the
116
// classifier answers null for it.
117
class PATTERN_DOMAIN(constructors: Collections.List[PATTERN_CONSTRUCTOR]) is
118
// Set when the alternatives are enum members, so a literal
119
// arm can be checked against the scrutinee's own enum before
120
// its value is matched to a member.
121
enum_root: Classy? public
122
123
super()
124
125
set_enum_root(root: Classy) is
126
self.enum_root = root
127
si
128
si
129
130
// Classifies a type into the alternatives its values can take.
131
//
132
// The order alternatives are produced in is the order a
133
// diagnostic lists them: absence first, then `false` before
134
// `true`, then a constructible root before its subclasses, then
135
// variants and enum members in declaration order.
136
class CASE_DOMAIN_CLASSIFIER is
137
_innate_symbol_lookup: Semantic.Lookups.InnateSymbolLookup
138
139
// Set by `_classify_core` for the classification in progress
140
// and consumed by `classify` before it returns; an enum
141
// domain carries its root so a literal arm can be checked
142
// against the scrutinee's own enum.
143
_enum_root: Classy?
144
145
init(innate_symbol_lookup: Semantic.Lookups.InnateSymbolLookup) is
146
super.init()
147
148
_innate_symbol_lookup = innate_symbol_lookup
149
si
150
151
classify(type: Type?, site: Source.LOCATION?) -> PATTERN_DOMAIN? is
152
if !type? \/ !type.is_settled then
153
return null
154
fi
155
156
let is_optional = type.is_optional
157
let core = if is_optional then type.as_non_optional() else type fi
158
159
_enum_root = null
160
161
let core_constructors = _classify_core(core, site)
162
let enum_root = _enum_root
163
164
if !core_constructors? then
165
return null
166
fi
167
168
if !is_optional then
169
return _domain(core_constructors, enum_root)
170
fi
171
172
let constructors = Collections.LIST[PATTERN_CONSTRUCTOR]()
173
174
constructors.add(PATTERN_CONSTRUCTOR(ConstructorKind.NULL, "null", null, self))
175
176
for c in core_constructors do
177
constructors.add(c)
178
od
179
180
return _domain(constructors, enum_root)
181
si
182
183
_domain(
184
constructors: Collections.List[PATTERN_CONSTRUCTOR],
185
enum_root: Classy?
186
) -> PATTERN_DOMAIN static is
187
let domain = PATTERN_DOMAIN(constructors)
188
189
if let root = enum_root then
190
domain.set_enum_root(root)
191
fi
192
193
return domain
194
si
195
196
_classify_core(core: Type, site: Source.LOCATION?) -> Collections.List[PATTERN_CONSTRUCTOR]? is
197
if core.matches(_innate_symbol_lookup.get_bool_type()) then
198
let constructors = Collections.LIST[PATTERN_CONSTRUCTOR]()
199
200
constructors.add(PATTERN_CONSTRUCTOR(ConstructorKind.FALSE, "false", null, self))
201
constructors.add(PATTERN_CONSTRUCTOR(ConstructorKind.TRUE, "true", null, self))
202
203
return constructors
204
fi
205
206
if let one_of = cast Semantic.Types.ONE_OF?(core) then
207
// A narrowed receiver lists the exact runtime types it
208
// can hold, so the root is an alternative only when it
209
// appears among them.
210
if !(cast Classy?(one_of.underlying_type.unspecialized_symbol))? then
211
return null
212
fi
213
214
let constructors = Collections.LIST[PATTERN_CONSTRUCTOR]()
215
216
for subtype in one_of.subtypes do
217
constructors.add(_classy_constructor(subtype, site))
218
od
219
220
return constructors
221
fi
222
223
let root = _try_get_classy(core)
224
225
if !root? then
226
return _product_constructors(core)
227
fi
228
229
if root.is_closed_root then
230
let alternatives = Collections.LIST[Classy]()
231
232
// A session compiles each cell against the cells before
233
// it, so an alternative a later cell declares is outside
234
// this case's domain: covering what the site's cell saw
235
// is exhaustive, and the throw a session emits carries
236
// an alternative added later.
237
for alternative in root.closed_alternatives do
238
if !site? \/ Semantic.Symbols.SESSION_VIEW.is_visible(alternative, site) then
239
alternatives.add(alternative)
240
fi
241
od
242
243
if alternatives.count == 0 then
244
return _product_constructors(core)
245
fi
246
247
let constructors = Collections.LIST[PATTERN_CONSTRUCTOR]()
248
249
// A concrete root is constructible in its own right,
250
// so covering every subclass leaves bare root
251
// instances unaccounted for.
252
if root.is_class /\ !root.is_abstract then
253
let root_constructor = PATTERN_CONSTRUCTOR(ConstructorKind.CLASSY, root.name, core, self)
254
255
root_constructor.set_classy(root)
256
257
constructors.add(root_constructor)
258
fi
259
260
for alternative in alternatives do
261
constructors.add(_classy_constructor(alternative, site))
262
od
263
264
return constructors
265
fi
266
267
if root.symbol_kind == Semantic.Symbols.SymbolKind.ENUM then
268
let constructors = Collections.LIST[PATTERN_CONSTRUCTOR]()
269
270
for symbol in root.symbols do
271
if let member: ENUM_STRUCT_MEMBER = symbol then
272
let constructor = PATTERN_CONSTRUCTOR(ConstructorKind.ENUM_MEMBER, member.name, null, self)
273
274
constructor.set_enum_member(member)
275
276
constructors.add(constructor)
277
fi
278
od
279
280
if constructors.count > 0 then
281
_enum_root = root
282
283
return constructors
284
fi
285
fi
286
287
return _product_constructors(core)
288
si
289
290
// A type that is not a sum of alternatives can still be a
291
// product of them, when it destructures. The single
292
// constructor is irrefutable, so such a domain is covered
293
// exactly when its fields are.
294
_product_constructors(core: Type) -> Collections.List[PATTERN_CONSTRUCTOR]? is
295
if !_destructure_element_types(core)? then
296
return null
297
fi
298
299
let constructors = Collections.LIST[PATTERN_CONSTRUCTOR]()
300
301
constructors.add(PATTERN_CONSTRUCTOR(ConstructorKind.PRODUCT, core.short_description, core, self))
302
303
return constructors
304
si
305
306
_classy_constructor(alternative: Classy, site: Source.LOCATION?) -> PATTERN_CONSTRUCTOR is
307
let constructor = PATTERN_CONSTRUCTOR(ConstructorKind.CLASSY, alternative.name, alternative.type, self)
308
309
constructor.set_classy(alternative)
310
constructor.set_site(site)
311
312
return constructor
313
si
314
315
field_domains_of(type: Type?, site: Source.LOCATION?) -> Collections.List[PATTERN_DOMAIN?] is
316
let result = Collections.LIST[PATTERN_DOMAIN?]()
317
318
if !type? then
319
return result
320
fi
321
322
let element_types = _destructure_element_types(type)
323
324
if !element_types? then
325
return result
326
fi
327
328
for element_type in element_types do
329
result.add(classify(element_type, site))
330
od
331
332
return result
333
si
334
335
// The types a positional destructure of `type` binds, in
336
// order, or null when the type does not destructure. Resolved
337
// through DESTRUCTURE_RESOLVER, so the shape the checker
338
// reasons about is the shape the language matches.
339
_destructure_element_types(type: Type) -> Collections.List[Type]? is
340
let arity = _destructure_arity(type)
341
342
if arity <= 0 then
343
return null
344
fi
345
346
let strategy = DESTRUCTURE_RESOLVER.resolve_strategy(type, arity)
347
348
let result = Collections.LIST[Type]()
349
350
if let deconstruct = strategy.deconstruct_function then
351
// A `deconstruct` writes each element through a `T
352
// ref` parameter, so the element's own type is the
353
// one the reference points at.
354
for argument in deconstruct.arguments do
355
let element_type = _pointee(argument.type)
356
357
if !element_type? then
358
return null
359
fi
360
361
result.add(element_type)
362
od
363
364
return result
365
fi
366
367
for member in strategy.members do
368
if !member? \/ !member.type? then
369
return null
370
fi
371
372
result.add(member.type!)
373
od
374
375
return result
376
si
377
378
_pointee(type: Type?) -> Type? static is
379
if !type? then
380
return null
381
fi
382
383
if let reference = cast Semantic.Types.REFERENCE?(type) then
384
if reference.arguments.count == 1 then
385
return reference.arguments[0]
386
fi
387
388
return null
389
fi
390
391
return type
392
si
393
394
_destructure_arity(type: Type) -> int is
395
if type.is_value_tuple then
396
return type.arguments.count
397
fi
398
399
let positional = DESTRUCTURE_RESOLVER.positional_member_count(type)
400
401
if positional > 0 then
402
return positional
403
fi
404
405
return _deconstruct_arity(type)
406
si
407
408
// The arity of the type's `deconstruct`, when exactly one
409
// viable overload exists. Several viable arities leave the
410
// shape ambiguous, and the type is treated as not
411
// destructurable rather than one of them being picked.
412
_deconstruct_arity(type: Type) -> int is
413
let member = type.find_member("deconstruct")
414
415
if !member? then
416
return 0
417
fi
418
419
let candidates = Collections.LIST[Semantic.Symbols.Function]()
420
421
if let group: Semantic.Symbols.FUNCTION_GROUP = member then
422
for f in group.functions do
423
candidates.add(f)
424
od
425
elif let function: Semantic.Symbols.Function = member then
426
candidates.add(function)
427
fi
428
429
let arity mut = 0
430
431
for candidate in candidates do
432
if !candidate.are_arguments_declared then
433
continue
434
fi
435
436
let candidate_arity = candidate.arguments.count
437
438
if !DESTRUCTURE_RESOLVER.is_viable_deconstruct(candidate, candidate_arity) then
439
continue
440
fi
441
442
if arity > 0 /\ arity != candidate_arity then
443
return 0
444
fi
445
446
arity = candidate_arity
447
od
448
449
return arity
450
si
451
452
_try_get_classy(type: Type) -> Classy? is
453
if let named = cast Semantic.Types.NAMED?(type) then
454
return cast Classy?(named.symbol.unspecialized_symbol)
455
fi
456
457
return null
458
si
459
si
460
si