Skip to content
← Back

src/syntax/trees/definitions/variables/variable.ghul

1
// FIXME: correct this namespace:
2
namespace Syntax.Trees.Variables is
3
4
use Source
5
6
use Ghul.Pipes
7
8
class VARIABLE: Trees.Definitions.Definition, ScopeCarrier is
9
scope: Semantic.Scope? public
10
11
left: VariableLeft
12
// Null on a destructure-pattern variable — only a SIMPLE_VARIABLE_LEFT
13
// has a single name. Callers that work only on simple variables can
14
// check `is_simple_name` first; callers that need to iterate every
15
// bound identifier should use `names`.
16
name: Identifiers.Identifier? => left.name
17
18
// every concrete VariableLeft subclass overrides names to non-null
19
names: Collections.Iterator[Identifiers.Identifier] => left.names!
20
21
type_expression: TypeExpressions.TypeExpression
22
initializer: Expressions.Expression?
23
is_static: bool
24
is_explicit_type: bool
25
is_variable: bool => true
26
want_dispose: bool
27
is_mutable_marked: bool
28
// Set on the binding of an `if let` arm so the compile pass can
29
// treat the initializer's optionality as already-checked: a
30
// refutable destructure unwraps `T?` to `T` before resolving
31
// element members.
32
is_refutable: bool
33
// Set on every formal-argument VARIABLE. A formal argument's
34
// initializer is its declared default value (consumed by
35
// callers), not code to run at function entry.
36
is_argument: bool
37
// Set on a VARIABLE that represents the `..` splice marker in
38
// a secondary `init` formal-argument list. Carries only its
39
// location; the rewrite pass expands it into the surrounding
40
// class's primary parameters before any downstream phase
41
// observes it.
42
is_splice: bool
43
44
// Set on the variables of a `let` that is a direct top-level
45
// statement of a file's synthesised entry point. Declare-members
46
// declares these into the file's namespace as top-level variables
47
// (static fields on the globals container) instead of as locals
48
// of the entry, so sibling global functions can read them.
49
is_top_level: bool public
50
51
// Set on a formal argument whose type ran on into the rest of its
52
// header, where the argument list was missing its `)`.
53
type_ran_on: bool public
54
55
// Set on a variant field VARIABLE inserted by expanding a `..`
56
// splice against the enclosing union's primary-constructor
57
// parameters. The variant's synthesised init forwards these
58
// names to super.init(...) and skips the self.<field>=
59
// assignment the union base already performs.
60
is_inherited_primary: bool
61
62
// The union's own member an inherited field is read through.
63
// It differs from the parameter's name where the header
64
// declares the parameter private, which captures it under an
65
// underscore.
66
inherited_member_name: string?
67
68
// Set on a VARIABLE synthesised as the backing field of an
69
// auto-property. It carries the property's own source location
70
// (so hover and go-to-definition on the backing field still work)
71
// rather than LOCATION.internal, so declare-members needs this to
72
// mark the resulting symbol synthesised.
73
is_synthesized: bool
74
75
// Populated only for primary-ctor parameters. Carries the
76
// trailing modifier suffixes (`public` / `field` / `init`) that
77
// describe the auto-generated body field/property the rewrite
78
// pass will synthesise for this parameter. Null in every other
79
// variable-parsing context.
80
modifiers: Modifiers.LIST? public
81
82
// Attribute pragmas (`@Foo() name: T`) written directly before a
83
// formal-argument VARIABLE — only ever populated when the parser
84
// is inside a function/method parameter list (see
85
// CONTEXT.in_formal_arguments); null everywhere else.
86
pragmas: Collections.LIST[Pragmas.PRAGMA]? public
87
88
init(
89
location: LOCATION,
90
left: VariableLeft,
91
type_expression: TypeExpressions.TypeExpression,
92
is_static: bool,
93
is_explicit_type: bool,
94
initializer: Expressions.Expression?
95
) is
96
super.init(location)
97
98
99
self.left = left
100
self.type_expression = type_expression
101
self.is_static = is_static
102
self.is_explicit_type = is_explicit_type
103
self.initializer = initializer
104
si
105
106
init(
107
location: LOCATION,
108
name: Identifiers.Identifier,
109
type_expression: TypeExpressions.TypeExpression,
110
is_static: bool,
111
is_explicit_type: bool,
112
initializer: Expressions.Expression?
113
) is
114
init(
115
location,
116
SIMPLE_VARIABLE_LEFT(location, name),
117
type_expression,
118
is_static,
119
is_explicit_type,
120
initializer
121
)
122
si
123
124
set_type_expression(type_expression: TypeExpressions.TypeExpression) is
125
self.type_expression = type_expression
126
si
127
128
mark_want_dispose() is
129
want_dispose = true
130
si
131
132
mark_mutable() is
133
is_mutable_marked = true
134
si
135
136
mark_refutable() is
137
is_refutable = true
138
si
139
140
mark_argument() is
141
is_argument = true
142
si
143
144
mark_splice() is
145
is_splice = true
146
si
147
148
mark_inherited_primary(member_name: string) is
149
is_inherited_primary = true
150
inherited_member_name = member_name
151
si
152
153
mark_synthesized() is
154
is_synthesized = true
155
si
156
157
set_modifiers(modifiers: Modifiers.LIST) is
158
self.modifiers = modifiers
159
si
160
161
set_pragmas(pragmas: Collections.LIST[Pragmas.PRAGMA]) is
162
self.pragmas = pragmas
163
si
164
165
copy() -> VARIABLE is
166
assert initializer == null else "cannot copy a variable node with non null initializer"
167
168
let result =
169
VARIABLE(
170
location,
171
left.copy(),
172
type_expression.copy(),
173
is_static,
174
is_explicit_type,
175
null
176
)
177
178
if let self_pragmas = pragmas then
179
result.set_pragmas(self_pragmas)
180
fi
181
182
return result
183
si
184
185
accept(visitor: Visitor) is
186
visitor.visit(self)
187
si
188
189
walk(visitor: Visitor) is
190
// Inherited-primary entries in a variant's field list
191
// exist purely to feed init synthesis (the variant's
192
// init forwards them to super.init). The union base
193
// owns the storage and visits all the symbol-side
194
// processing (declare_symbols, resolve types, etc.)
195
// through the union's own field declarations. Walking
196
// them at the variant level would double-process the
197
// same name and trip "set type twice" / shadow checks.
198
if is_inherited_primary then
199
return
200
fi
201
if !visitor.pre(self) then
202
if pragmas? then
203
for pragma in pragmas do
204
pragma.walk(visitor)
205
od
206
fi
207
left.walk(visitor)
208
type_expression.walk(visitor)
209
if initializer? then
210
initializer.walk(visitor)
211
fi
212
fi
213
accept(visitor)
214
si
215
si
216
217
class VariableLeft: Trees.Node is
218
is_simple_name: bool => false
219
name: Identifiers.Identifier? => null
220
names: Collections.Iterator[Identifiers.Identifier]? => null
221
elements: Collections.List[VariableLeft]? => null
222
223
// Per-element type ascription. Allowed on any element of a
224
// destructure pattern, at any nesting depth. In an ordinary
225
// `let`, this is a static type assertion on the bound slot;
226
// in an `if let` arm (parent VARIABLE.is_refutable), it is
227
// additionally a runtime narrowing test on that element.
228
type_expression: TypeExpressions.TypeExpression?
229
230
// Set on every element of a by-name destructure group:
231
// `(local = field, …) = source` records `field` here so the
232
// resolver pulls `source.field` rather than the positional
233
// slot. Null on positional-group elements. The parser
234
// enforces all-or-nothing per `(...)` group; mixing is a
235
// parse error.
236
source_field_name: Identifiers.Identifier? public
237
238
set_source_field_name(name: Identifiers.Identifier) is
239
self.source_field_name = name
240
si
241
242
// Compile-expressions output for this node lives in the pass's
243
// own VARIABLE_LEFT_STATE_STORE, keyed by this node, not here.
244
245
// Propagated from the parent VARIABLE for `if let` arms.
246
is_refutable: bool public
247
248
// Propagated from the parent VARIABLE when it's a formal
249
// argument. A destructured formal argument has no initializer
250
// to derive a value from - the leaves' types are assigned
251
// directly from the parameter's aggregate type by resolve-
252
// explicit-types, and generate-il sources the unpack from the
253
// synthesised parameter symbol rather than a walked value - so
254
// compile-expressions' usual initializer-driven handling of a
255
// destructure pattern doesn't apply and skips this subtree.
256
is_argument_left: bool public
257
258
init(location: LOCATION) is
259
super.init(location)
260
si
261
262
set_type_expression(type_expression: TypeExpressions.TypeExpression) is
263
self.type_expression = type_expression
264
si
265
266
// Propagate `is_refutable` to every leaf of a destructure tree
267
// so a `LITERAL_VARIABLE_LEFT` nested inside a tuple destructure
268
// (`if let (1, y) = pair`) knows it sits in a refutable
269
// context and is not a silent no-op.
270
mark_refutable_recursive() is
271
is_refutable = true
272
si
273
274
// Propagate `is_argument_left` to every leaf, same shape as
275
// mark_refutable_recursive above.
276
mark_argument_recursive() is
277
is_argument_left = true
278
si
279
280
// True when this leaf's own `: T` ascription (if any) performs
281
// a runtime test that can genuinely fail — a reference-type
282
// narrow (an isinst test), or a value-type ascription that is
283
// itself option-shaped (`T?` / NULLABLE[T], tested via
284
// presence). A non-optional value-type ascription (`i: int`)
285
// can never fail, so it contributes no refutability — the
286
// same reasoning `PATTERN_CHECKER.should_emit_value_type_narrow_error`
287
// applies to the top-level ascription.
288
_ascription_is_refutable: bool is
289
let ascribed = type_expression?.type
290
291
if !ascribed? then
292
return false
293
fi
294
295
if !ascribed.is_value_type then
296
return true
297
fi
298
299
return ascribed.find_member("has_value")? /\ ascribed.find_member("value")?
300
si
301
302
// True when this leaf, or any leaf nested inside it, carries
303
// its own runtime refutability — a literal/null/enum-member
304
// leaf, or a genuinely-refutable `: T` ascription — independent
305
// of whether the top-level scrutinee itself is optional. An
306
// `if let` pattern needs at least one of these somewhere in
307
// its shape, or the arm can never fail to match.
308
// DESTRUCTURING_VARIABLE_LEFT additionally recurses into its
309
// elements.
310
has_intrinsic_refutability: bool => _ascription_is_refutable
311
312
// True when this node, or any nested destructure group at any
313
// depth, is a by-name group (source_field_name set on its own
314
// first element - the parser enforces all-or-nothing per
315
// group, so checking the first element is enough for that
316
// group). Used to reject named destructuring anywhere in a
317
// formal-argument pattern, not just at its outermost level.
318
has_named_group: bool => false
319
320
get_names_into(into: Collections.MutableList[Identifiers.Identifier])
321
322
copy() -> VariableLeft
323
324
copy_base_values_from(other: VariableLeft) is
325
if let other_type_expression = other.type_expression then
326
type_expression = other_type_expression.copy()
327
fi
328
329
if let other_source_field_name = other.source_field_name then
330
source_field_name = other_source_field_name.copy()
331
fi
332
si
333
si
334
335
class SIMPLE_VARIABLE_LEFT: VariableLeft is
336
name: Identifiers.Identifier
337
is_simple_name: bool => true
338
339
elements: Collections.List[VariableLeft] => []
340
names: Collections.Iterator[Identifiers.Identifier] => [name].iterator
341
342
init(
343
location: LOCATION,
344
name: Identifiers.Identifier
345
) is
346
super.init(location)
347
348
self.name = name
349
si
350
351
get_names_into(into: Collections.MutableList[Identifiers.Identifier]) is
352
into.add(name)
353
si
354
355
accept(visitor: Visitor) is
356
visitor.visit(self)
357
si
358
359
walk(visitor: Visitor) is
360
if !visitor.pre(self) then
361
name.walk(visitor)
362
363
if let self.type_expression? then
364
type_expression.walk(visitor)
365
fi
366
fi
367
368
accept(visitor)
369
si
370
371
copy() -> VariableLeft is
372
let result = SIMPLE_VARIABLE_LEFT(location, name)
373
result.copy_base_values_from(self)
374
return result
375
si
376
si
377
378
// A literal / `null` / enum-member leaf inside a destructure
379
// pattern. Has no binding name — at runtime, the source's
380
// corresponding element is value-equality tested against the
381
// wrapped expression, and the arm only matches when the test
382
// succeeds. Visible only on the LHS of refutable bindings
383
// (`if let` / `case`-when patterns); a plain `let` never has
384
// a refutable element.
385
class LITERAL_VARIABLE_LEFT: VariableLeft is
386
expression: Expressions.Expression
387
388
// Whether the source wrote the `~` match marker. It changes
389
// nothing about what the leaf does — a bare name only reaches
390
// this node by being marked, and every other leaf form matches
391
// with or without it — but the formatter has to write back
392
// what it read.
393
is_marked: bool
394
395
is_simple_name: bool => false
396
elements: Collections.List[VariableLeft] => []
397
names: Collections.Iterator[Identifiers.Identifier] => (let empty: Identifiers.Identifier[] = []; empty.iterator)
398
399
// A literal leaf is itself a runtime equality test, so it
400
// always provides refutability regardless of any ascription.
401
has_intrinsic_refutability: bool => true
402
403
init(location: LOCATION, expression: Expressions.Expression, is_marked: bool) is
404
super.init(location)
405
406
self.expression = expression
407
self.is_marked = is_marked
408
si
409
410
get_names_into(into: Collections.MutableList[Identifiers.Identifier]) is
411
si
412
413
accept(visitor: Visitor) is
414
visitor.visit(self)
415
si
416
417
walk(visitor: Visitor) is
418
if !visitor.pre(self) then
419
expression.walk(visitor)
420
fi
421
422
accept(visitor)
423
si
424
425
copy() -> VariableLeft is
426
let result = LITERAL_VARIABLE_LEFT(location, expression, is_marked)
427
result.copy_base_values_from(self)
428
return result
429
si
430
si
431
432
class DESTRUCTURING_VARIABLE_LEFT: VariableLeft is
433
elements: Collections.List[VariableLeft]
434
435
names: Collections.Iterator[Identifiers.Identifier] => _flatten_names()
436
437
_flatten_names() -> Ghul.Pipes.Pipe[Identifiers.Identifier] is
438
for element in elements do
439
let element_names = element.names
440
441
if element_names? then
442
for n in element_names do
443
yield n
444
od
445
fi
446
od
447
si
448
449
get_names_into(into: Collections.MutableList[Identifiers.Identifier]) is
450
for e in elements do
451
e.get_names_into(into)
452
od
453
si
454
455
mark_refutable_recursive() is
456
is_refutable = true
457
for e in elements do
458
e.mark_refutable_recursive()
459
od
460
si
461
462
// Own ascription (a nested group's `: T`), if it is itself
463
// refutable, or any element's, at any depth.
464
has_intrinsic_refutability: bool =>
465
_ascription_is_refutable \/ (elements |> any(e => e.has_intrinsic_refutability))
466
467
mark_argument_recursive() is
468
is_argument_left = true
469
for e in elements do
470
e.mark_argument_recursive()
471
od
472
si
473
474
has_named_group: bool =>
475
(elements.count > 0 /\ elements[0].source_field_name?) \/
476
elements |> any(e => e.has_named_group)
477
478
init(location: LOCATION, elements: Collections.List[VariableLeft]) is
479
super.init(location)
480
481
482
self.elements = elements
483
si
484
485
accept(visitor: Visitor) is
486
visitor.visit(self)
487
si
488
489
walk(visitor: Visitor) is
490
if !visitor.pre(self) then
491
for e in elements do
492
e.walk(visitor)
493
od
494
495
if let self.type_expression? then
496
type_expression.walk(visitor)
497
fi
498
fi
499
accept(visitor)
500
si
501
502
copy() -> VariableLeft is
503
let new_elements = Collections.LIST[VariableLeft](elements.count)
504
505
for e in elements do
506
new_elements.add(e.copy())
507
od
508
509
let result = DESTRUCTURING_VARIABLE_LEFT(location, new_elements)
510
result.copy_base_values_from(self)
511
return result
512
si
513
si
514
si