Skip to content
← Back

src/syntax/process/declare-members/declare_members_bodies.ghul

1
namespace Syntax.Process is
2
use Logging
3
use Source
4
use Trees
5
6
// The body half of DECLARE_MEMBERS: the block scopes, locals,
7
// formal and destructured arguments, closures and lambda
8
// parameters declared inside function bodies. The class head,
9
// state and the type-level member handlers are in
10
// declare_members.ghul.
11
partial DECLARE_MEMBERS is
12
visit(variable: Variables.VARIABLE) is
13
// A destructured formal argument is one physical parameter
14
// at the aggregate (tuple) type, unpacked into its leaf
15
// names at method entry - not one parameter per leaf. The
16
// leaves are ordinary body-scoped locals, declared here
17
// even though we're inside the function's `_declaring_
18
// arguments` window, so declare_variable doesn't turn them
19
// into LOCAL_ARGUMENTs.
20
if variable.is_argument /\ !variable.left.is_simple_name then
21
let function = cast Semantic.Symbols.Function?(current_declaration_context)
22
23
// Declaring into the enclosing function's rejecting scope
24
// (see pre(FUNCTION) above) reaches here too, with no
25
// function symbol to open a declaring-arguments window
26
// against — there is nothing to distinguish an argument
27
// from any other local in a block scope, so the window
28
// toggle is simply skipped.
29
assert function? \/ isa Semantic.BLOCK_SCOPE(current_declaration_context) else "destructured formal argument declared outside a function scope"
30
31
let group_symbol =
32
current_declaration_context.declare_variable(
33
variable.location,
34
next_argument_group_name(),
35
variable.is_static,
36
_symbol_definition_listener
37
)
38
39
group_symbol.mark_synthesized()
40
41
associate_node_with_scope(variable, group_symbol)
42
43
if function? then
44
function.end_declaring_arguments()
45
fi
46
47
for name in variable.names do
48
let is_discard = name.name =~ "_"
49
50
if is_discard then
51
name.name = next_discard_name()
52
fi
53
54
let declared_leaf = current_declaration_context.declare_variable(name.location, name.name, variable.is_static, _symbol_definition_listener)
55
56
if is_discard then
57
declared_leaf.mark_synthesized()
58
fi
59
od
60
61
if function? then
62
function.start_declaring_arguments()
63
fi
64
65
if variable.pragmas? then
66
_logger.error(variable.location, "attribute is not allowed on a destructured parameter")
67
fi
68
69
return
70
fi
71
72
let declared_symbol: Semantic.Symbols.Symbol? mut = null
73
74
for name in variable.names do
75
76
let is_discard = name.name =~ "_"
77
78
if is_discard then
79
name.name = next_discard_name()
80
fi
81
82
// A top-level `let` is declared into the file's namespace
83
// as a static field on the globals container rather than
84
// as a local of the synthesised entry, so sibling global
85
// functions can read it. The namespace is always found
86
// here: the entry only exists inside a file's root
87
// namespace.
88
let `namespace =
89
if variable.is_top_level then
90
_symbol_table.current_namespace_symbol
91
else
92
null
93
fi
94
95
let declared =
96
if `namespace? then
97
`namespace.declare_top_level_variable(name.location, name.name, _symbol_definition_listener)
98
else
99
current_declaration_context.declare_variable(name.location, name.name, variable.is_static, _symbol_definition_listener)
100
fi
101
102
if variable.is_synthesized \/ is_discard then
103
declared.mark_synthesized()
104
fi
105
106
if variable.is_mutable_marked then
107
if let v: Semantic.Symbols.Variable = declared then
108
v.is_mutable_marked = true
109
fi
110
fi
111
112
declared_symbol = declared
113
od
114
115
// Only a formal-argument VARIABLE can carry attribute
116
// pragmas (parsed only inside CONTEXT.in_formal_arguments);
117
// associate it with its declared symbol so compile-expressions
118
// and generate-il can find it again via symbol_for. A
119
// destructured parameter declares more than one name, so
120
// there is no single symbol an attribute could sensibly
121
// attach to.
122
if variable.pragmas? then
123
if declared_symbol? /\ variable.left.is_simple_name then
124
associate_node_with_scope(variable, declared_symbol)
125
else
126
_logger.error(variable.location, "attribute is not allowed on a destructured parameter")
127
fi
128
fi
129
130
// A nested named function's own name. The local is declared
131
// here, after the literal it initializes has been walked, so
132
// the closure is told which local names it now - references to
133
// the name from inside the body are compiled later, and read
134
// the function from its recurse field rather than from a local
135
// that is still empty there.
136
if let literal = cast Expressions.FUNCTION?(variable.initializer) then
137
if literal.nested_name? then
138
if let local: Semantic.Symbols.LOCAL_VARIABLE = declared_symbol then
139
local.is_nested_function = true
140
fi
141
142
if let closure: Semantic.Symbols.Closure = symbol_for(literal) then
143
closure.self_reference_variable = cast Semantic.Symbols.Variable?(declared_symbol)
144
fi
145
fi
146
fi
147
si
148
149
// Controlled walk: per clause, walk the clause's pieces and
150
// THEN declare its pattern names — so later clauses'
151
// scrutinees see earlier clauses' bindings during
152
// name-resolution. Mirrors what plain `let a = …; let b = …;`
153
// gets for free across separate statements (visit(VARIABLE) on
154
// the first runs before the second is walked).
155
pre(rb: Statements.REFUTABLE_BINDING) -> bool is
156
for c in rb.clauses do
157
if let c.narrow_type_expression? then
158
narrow_type_expression.walk(self)
159
fi
160
161
c.scrutinee.walk(self)
162
c.pattern.walk(self)
163
164
if let c.guard? then
165
guard.walk(self)
166
fi
167
168
for name in c.pattern.names! do
169
let is_discard = name.name =~ "_"
170
171
if is_discard then
172
name.name = next_discard_name()
173
fi
174
175
let declared = current_declaration_context.declare_variable(
176
name.location,
177
name.name,
178
false,
179
_symbol_definition_listener
180
)
181
182
if is_discard then
183
declared.mark_synthesized()
184
fi
185
od
186
od
187
188
return true
189
si
190
191
visit(rb: Statements.REFUTABLE_BINDING) is
192
si
193
194
visit(variable: Expressions.VARIABLE) is
195
si
196
197
pre(block: Expressions.VAL_BLOCK) -> bool is
198
create_and_enter_block_scope(block)
199
return false
200
si
201
202
visit(block: Expressions.VAL_BLOCK) is
203
leave_scope(block)
204
si
205
206
pre(if_branch: Statements.IF_BRANCH) -> bool is
207
create_and_enter_block_scope(if_branch)
208
return false
209
si
210
211
visit(if_branch: Statements.IF_BRANCH) is
212
leave_scope(if_branch)
213
si
214
215
pre(`case: Statements.CASE) -> bool is
216
create_and_enter_block_scope(`case)
217
return false
218
si
219
220
visit(`case: Statements.CASE) is
221
leave_scope(`case)
222
si
223
224
pre(case_match: Statements.CASE_MATCH) -> bool is
225
create_and_enter_block_scope(case_match)
226
return false
227
si
228
229
visit(case_match: Statements.CASE_MATCH) is
230
leave_scope(case_match)
231
si
232
233
pre(`try: Statements.TRY) -> bool is
234
create_and_enter_block_scope(`try)
235
return false
236
si
237
238
visit(`try: Statements.TRY) is
239
leave_try_scope(`try)
240
si
241
242
pre(`catch: Statements.CATCH) -> bool is
243
create_and_enter_block_scope(`catch)
244
return false
245
si
246
247
visit(`catch: Statements.CATCH) is
248
leave_scope(`catch)
249
si
250
251
pre(`do: Statements.DO) -> bool is
252
create_and_enter_block_scope(`do)
253
_declare_pending_loop_label()
254
return false
255
si
256
257
visit(`do: Statements.DO) is
258
leave_scope(`do)
259
si
260
261
pre(`for: Statements.FOR) -> bool is
262
create_and_enter_block_scope(`for)
263
_declare_pending_loop_label()
264
return false
265
si
266
267
visit(`for: Statements.FOR) is
268
leave_scope(`for)
269
si
270
271
// A label names its loop's body, so it declares into the body
272
// scope the wrapped loop creates — visible inside (and under
273
// nested loops), gone once the loop ends, and sibling loops may
274
// reuse a name. The parser only wraps loops in LABELLED.
275
pre(labelled: Statements.LABELLED) -> bool is
276
_pending_labels.add(labelled.label)
277
return false
278
si
279
280
visit(labelled: Statements.LABELLED) is
281
si
282
283
_declare_pending_loop_label() is
284
if _pending_labels.count > 0 then
285
let label = _pending_labels[_pending_labels.count - 1]
286
_pending_labels.remove_at(_pending_labels.count - 1)
287
288
current_declaration_context.declare_label(label.location, label.name, _symbol_definition_listener)
289
fi
290
si
291
292
pre(variable: Expressions.VARIABLE) -> bool is
293
let is_discard = variable.name.name =~ "_"
294
295
if is_discard then
296
variable.name.name = next_discard_name()
297
fi
298
299
// A destructured lambda parameter is one physical argument
300
// under a synthesised name, unpacked into its leaves at
301
// entry - the same shape declare-members gives a
302
// destructured formal argument of a named function. The
303
// leaves are ordinary body locals, so they are declared
304
// outside the function's declaring-arguments window.
305
if let variable.left? then
306
variable.name.name = next_argument_group_name()
307
308
current_declaration_context.declare_variable(variable.name.location, variable.name.name, false, _symbol_definition_listener)
309
.mark_synthesized()
310
311
let function = cast Semantic.Symbols.Function?(current_declaration_context)
312
313
if function? then
314
function.end_declaring_arguments()
315
fi
316
317
for name in left.names! do
318
let leaf_is_discard = name.name =~ "_"
319
320
if leaf_is_discard then
321
name.name = next_discard_name()
322
fi
323
324
let declared_leaf = current_declaration_context.declare_variable(name.location, name.name, false, _symbol_definition_listener)
325
326
if leaf_is_discard then
327
declared_leaf.mark_synthesized()
328
fi
329
od
330
331
if function? then
332
function.start_declaring_arguments()
333
fi
334
335
return false
336
fi
337
338
let declared = current_declaration_context.declare_variable(variable.name.location, variable.name.name, false, _symbol_definition_listener)
339
340
if is_discard \/ variable.is_synthesized then
341
declared.mark_synthesized()
342
fi
343
344
return false
345
si
346
347
pre(function: Expressions.FUNCTION) -> bool is
348
// A lambda whose body contains `await` (not inside another
349
// nested lambda) is classified as `*_ASYNC_CLOSURE` so
350
// `async_state_machine_for` picks it up for state-machine
351
// IL emission. `contains_let_await` and `is_void_async`
352
// are propagated to the AST node so compile-lambdas can
353
// settle the inferred return type to `Tasks.TASK` /
354
// `Tasks.TASK[T]` shape.
355
let await_scanner = AWAIT_SCANNER()
356
if !function.body.is_null then
357
await_scanner.scan(function.body)
358
fi
359
360
// A literal whose body contains `yield` (again, not inside a
361
// nested literal) is a generator, classified as
362
// `*_GENERATOR_CLOSURE` so `state_machine_for` picks it up. A
363
// body with both is reported, and is a generator.
364
let body_has_yield = !function.body.is_null /\ YIELD_SCANNER().body_has_yield(function.body)
365
366
if body_has_yield /\ await_scanner.found then
367
_logger.error(function.location, "function cannot be both a generator and async")
368
fi
369
370
let body_has_await = await_scanner.found /\ !body_has_yield
371
function.contains_let_await = body_has_await
372
function.is_void_async = body_has_await /\ !await_scanner.found_value_return
373
374
// A nested named function reaches itself by its own name, so
375
// whether it recurses is a fact about the body rather than
376
// something the source marked, and it has to be settled before
377
// the closure is built.
378
if let name = function.nested_name then
379
if NAME_REFERENCE_SCANNER().body_references(function.body, name.name) then
380
function.mark_recursive()
381
fi
382
fi
383
384
// a lambda expression always sits inside a function
385
let current_function = self.current_function!
386
387
let closure_symbol: Semantic.Symbols.Symbol mut
388
389
if body_has_yield then
390
closure_symbol = current_function.declare_generator_closure(
391
function.location,
392
next_anon_name(),
393
// FIXME:
394
cast Semantic.Scope?(current_closure_context)!,
395
current_scope,
396
function.is_recursive,
397
_symbol_definition_listener
398
)
399
elif body_has_await then
400
closure_symbol = current_function.declare_async_closure(
401
function.location,
402
next_anon_name(),
403
// FIXME:
404
cast Semantic.Scope?(current_closure_context)!,
405
current_scope,
406
function.is_recursive,
407
_symbol_definition_listener
408
)
409
else
410
closure_symbol = current_function.declare_closure(
411
function.location,
412
next_anon_name(),
413
// FIXME:
414
cast Semantic.Scope?(current_closure_context)!,
415
current_scope,
416
function.is_recursive,
417
_symbol_definition_listener
418
)
419
fi
420
421
closure_symbol.mark_synthesized()
422
423
associate_and_enter_scope(
424
function,
425
closure_symbol
426
)
427
428
return true
429
si
430
431
visit(function: Expressions.FUNCTION) is
432
let symbol = symbol_for(function)
433
434
if symbol? /\ isa Semantic.Symbols.Function(symbol) then
435
let function_symbol = symbol
436
437
let arguments = function.arguments.expressions
438
439
// we don't know if identifier expressions in a tuple are actually untyped
440
// anonymous function formal arguments until we know the context, so re-write
441
// them now to be variables:
442
for index in 0..arguments.count do
443
let a mut = arguments[index]
444
445
if isa Trees.Expressions.IDENTIFIER(a) then
446
let infer = Trees.TypeExpressions.INFER(a.location)
447
448
a = Trees.Expressions.VARIABLE(a.location, cast Syntax.Trees.Expressions.IDENTIFIER(a).identifier, infer, null)
449
450
arguments[index] = a
451
elif isa Trees.Expressions.DEFAULT(a) /\ a.could_be_formal_argument then
452
// A bare `_` formal (`_ => ...`) is parsed as a
453
// default-value expression and re-written here to
454
// an untyped discard parameter. Multi-arg and
455
// destructure formals reach this via
456
// rewrite_as_variables instead.
457
a = Trees.Expressions.VARIABLE(
458
a.location,
459
Trees.Identifiers.Identifier(a.location, "_"),
460
Trees.TypeExpressions.INFER(a.location),
461
null
462
)
463
464
arguments[index] = a
465
fi
466
od
467
468
function_symbol.start_declaring_arguments()
469
470
function.arguments.walk(self)
471
472
function_symbol.end_declaring_arguments()
473
fi
474
475
function.body.walk(self)
476
477
leave_scope(function)
478
si
479
480
pre(let_in: Expressions.LET_IN) -> bool is
481
create_and_enter_block_scope(let_in)
482
return false
483
si
484
485
visit(let_in: Expressions.LET_IN) is
486
leave_scope(let_in)
487
si
488
489
// A lazy comprehension owns a generator literal taking no
490
// arguments: its clauses and element are that literal's body, so
491
// the variables it reads from outside are captured as any literal's
492
// are.
493
pre(comprehension: Expressions.COMPREHENSION) -> bool is
494
if !comprehension.is_lazy then
495
return false
496
fi
497
498
let generator = self.current_function!.declare_generator_closure(
499
comprehension.location,
500
next_anon_name(),
501
cast Semantic.Scope?(current_closure_context)!,
502
current_scope,
503
false,
504
_symbol_definition_listener
505
)
506
507
generator.mark_synthesized()
508
509
associate_and_enter_scope(comprehension, generator)
510
511
if let function: Semantic.Symbols.Function = generator then
512
function.start_declaring_arguments()
513
function.end_declaring_arguments()
514
fi
515
516
return false
517
si
518
519
visit(comprehension: Expressions.COMPREHENSION) is
520
if comprehension.is_lazy then
521
leave_scope(comprehension)
522
fi
523
si
524
525
// A comprehension's for clause scopes its pattern over the clauses
526
// after it and the element. It declares no loop label: nothing can
527
// break out of a comprehension.
528
pre(clause: Expressions.COMPREHENSION_FOR) -> bool is
529
create_and_enter_block_scope(clause)
530
return false
531
si
532
533
visit(clause: Expressions.COMPREHENSION_FOR) is
534
leave_scope(clause)
535
si
536
537
pre(expression: Bodies.EXPRESSION) -> bool is
538
create_and_enter_block_scope(expression)
539
return false
540
si
541
542
visit(expression: Bodies.EXPRESSION) is
543
leave_scope(expression)
544
si
545
546
pre(block: Bodies.BLOCK) -> bool is
547
create_and_enter_block_scope(block)
548
return false
549
si
550
551
visit(block: Bodies.BLOCK) is
552
leave_scope(block)
553
si
554
si
555
si