Skip to content
← Back

src/syntax/process/rewrite-syntax-trees/rewrite_primary_constructors_synthesis.ghul

1
namespace Syntax.Process is
2
use Source
3
use Logging
4
use Trees
5
6
// Synthesis helpers: capture matching, pragma rewrapping,
7
// auto-property and deconstruct generation, variant splice
8
// expansion and secondary-init chaining. The walk itself is in
9
// rewrite_primary_constructors.ghul.
10
partial REWRITE_PRIMARY_CONSTRUCTORS is
11
_strip_orphan_super_calls(classy: Trees.Definitions.Classy) is
12
let kept = Collections.LIST[Trees.Definitions.Definition]()
13
let any_dropped mut = false
14
15
for d in classy.body do
16
if isa Trees.Definitions.SUPER_CALL(d) then
17
_logger.error(
18
d.location,
19
"super(...) declaration requires a primary constructor header"
20
)
21
any_dropped = true
22
elif _is_unmatched_capture_shorthand(d) then
23
// A name on its own is the capture shorthand, and there
24
// is no header for it to capture from - so it has
25
// neither a type nor an accessor, and nothing
26
// downstream can make sense of it. Say so here, where
27
// the shape is still recognisable.
28
_logger.error(
29
d.location,
30
"capture shorthand requires a primary constructor header"
31
)
32
any_dropped = true
33
else
34
if _has_init_modifier(d) then
35
_logger.error(
36
d.location,
37
"init modifier is only valid on a primary constructor parameter"
38
)
39
fi
40
kept.add(d)
41
fi
42
od
43
44
if any_dropped then
45
classy.body.clear_definitions()
46
47
for d in kept do
48
classy.body.add(d)
49
od
50
fi
51
si
52
53
_is_unmatched_capture_shorthand(d: Trees.Definitions.Definition) -> bool is
54
if !isa Trees.Definitions.PROPERTY(d) then
55
return false
56
fi
57
58
let p = cast Trees.Definitions.PROPERTY(d)
59
60
if p.read_body? \/ p.assign_body? then
61
return false
62
fi
63
64
return p.name? /\ p.type_expression.is_inferred
65
si
66
67
// Every @pragma(...) wrapping `d`, outermost first, unwrapping
68
// as many levels as were stacked - a declaration can carry more
69
// than one.
70
_collect_pragmas(d: Trees.Definitions.Definition) -> Collections.LIST[Trees.Definitions.PRAGMA] is
71
let result = Collections.LIST[Trees.Definitions.PRAGMA]()
72
let current: Trees.Definitions.Definition mut = d
73
74
while isa Trees.Definitions.PRAGMA(current) do
75
let p = cast Trees.Definitions.PRAGMA(current)
76
77
result.add(p)
78
current = p.definition
79
od
80
81
return result
82
si
83
84
// Rebuilds the nesting `_collect_pragmas` unwrapped, around a
85
// freshly synthesised definition in place of the original one.
86
_rewrap_pragmas(
87
pragmas: Collections.LIST[Trees.Definitions.PRAGMA],
88
inner: Trees.Definitions.Definition
89
) -> Trees.Definitions.Definition is
90
let result: Trees.Definitions.Definition mut = inner
91
let i mut = pragmas.count - 1
92
93
while i >= 0 do
94
let p = pragmas[i]
95
96
result = Trees.Definitions.PRAGMA(p.pragma.location :: result.location, p.pragma, result)
97
i = i - 1
98
od
99
100
return result
101
si
102
103
_is_init_function(d: Trees.Definitions.Definition) -> bool is
104
if !isa Trees.Definitions.FUNCTION(d) then
105
return false
106
fi
107
108
let f = cast Trees.Definitions.FUNCTION(d)
109
110
return f.name? /\ f.name.name =~ "init"
111
si
112
113
_is_primary_init(f: Trees.Definitions.FUNCTION) -> bool is
114
// init(..) — exactly one parameter and it's the splice marker.
115
let args = f.arguments.variables
116
117
return args.count == 1 /\ args[0].is_splice
118
si
119
120
_arg_list_has_splice(args: Trees.Variables.LIST?) -> bool is
121
if !args? then
122
return false
123
fi
124
125
for v in args do
126
if v.is_splice then
127
return true
128
fi
129
od
130
131
return false
132
si
133
134
_count_splices(args: Trees.Variables.LIST?) -> int is
135
if !args? then
136
return 0
137
fi
138
139
let n mut = 0
140
141
for v in args do
142
if v.is_splice then
143
n = n + 1
144
fi
145
od
146
147
return n
148
si
149
150
_strip_underscore(name: string) -> string is
151
if name.length > 0 /\ name[0] == '_' then
152
return name.substring(1)
153
fi
154
155
return name
156
si
157
158
// Diagnoses incoherent combinations on a primary-ctor parameter:
159
// - `init` combined with any visibility modifier or with another
160
// storage modifier means "no field, but also make the (non-
161
// existent) field public / a plain field" — incoherent.
162
// - `init` combined with a `_`-prefixed name means "no field, but
163
// the name implies a private field" — same incoherence.
164
// The rewriter only flags; it doesn't strip, so the user sees
165
// every diagnostic in one pass.
166
_validate_primary_param_modifiers(p: Trees.Variables.VARIABLE) is
167
let modifiers = p.modifiers
168
169
if !modifiers? then
170
return
171
fi
172
173
if modifiers.is_init then
174
if let modifiers.access_modifier? then
175
_logger.error(
176
access_modifier.location,
177
"init modifier cannot combine with visibility modifiers"
178
)
179
fi
180
if let
181
modifiers.storage_class? /\
182
!storage_class.is_init
183
then
184
_logger.error(
185
storage_class.location,
186
"init modifier cannot combine with other storage modifiers"
187
)
188
fi
189
if let
190
p.name? /\
191
name.name.length > 0 /\
192
name.name[0] == '_'
193
then
194
_logger.error(
195
p.location,
196
"init modifier cannot combine with a _-prefixed parameter name"
197
)
198
fi
199
fi
200
si
201
202
// Body decls with the `init` modifier are diagnosed at the body-
203
// iteration step. This helper recognises the (FUNCTION / PROPERTY)
204
// shapes that carry modifier lists.
205
_has_init_modifier(d: Trees.Definitions.Definition) -> bool is
206
if isa Trees.Definitions.PROPERTY(d) then
207
let p = cast Trees.Definitions.PROPERTY(d)
208
return p.modifiers.is_init
209
fi
210
if isa Trees.Definitions.FUNCTION(d) then
211
let f = cast Trees.Definitions.FUNCTION(d)
212
return f.modifiers.is_init
213
fi
214
return false
215
si
216
217
// Synthesise a body-level field/property declaration mirroring the
218
// primary-ctor parameter, ready to flow through
219
// `add_accessors_for_properties` and `declare_symbols` as if the
220
// user had written it by hand. Strips the `init` modifier from
221
// the synthesised modifier list as a safety net — even though the
222
// caller has already filtered INIT params, leaving INIT on the
223
// body decl would re-trigger the "init only on primary param"
224
// diagnostic.
225
_synthesise_auto_property(p: Trees.Variables.VARIABLE) -> Trees.Definitions.PROPERTY =>
226
let loc = p.location in
227
let modifiers = _make_body_modifiers(p) in
228
Trees.Definitions.PROPERTY(
229
loc,
230
p.type_expression.copy(),
231
_body_property_name(p),
232
modifiers,
233
null,
234
null,
235
null
236
)
237
238
// `private` names the member the way the naming convention does:
239
// `v: int private` captures into `_v`, leaving the parameter
240
// itself as `v`. The underscore is what the later passes read for
241
// visibility, so `_make_body_modifiers` drops the modifier rather
242
// than carrying both spellings of the same fact.
243
_body_property_name(p: Trees.Variables.VARIABLE) -> Trees.Identifiers.Identifier is
244
let name = p.name!
245
246
if _is_private_param(p) /\ !name.name.starts_with('_') then
247
return Trees.Identifiers.Identifier(name.location, "_{name.name}")
248
fi
249
250
return name.copy()
251
si
252
253
_is_private_param(p: Trees.Variables.VARIABLE) -> bool is
254
if let p.modifiers?, modifiers.access_modifier? /\ access_modifier.is_private then
255
return true
256
fi
257
258
return false
259
si
260
261
_make_body_modifiers(p: Trees.Variables.VARIABLE) -> Trees.Modifiers.LIST is
262
let loc = p.location
263
let modifiers = p.modifiers
264
265
if !modifiers? then
266
return Trees.Modifiers.LIST(loc, null, null)
267
fi
268
269
let access: Trees.Modifiers.AccessModifier? mut = null
270
let storage: Trees.Modifiers.StorageClass? mut = null
271
272
if let modifiers.access_modifier? /\ !access_modifier.is_private then
273
access = access_modifier.copy()
274
fi
275
276
// Drop INIT — it has no meaning on the body decl. Other
277
// storage classes (FIELD, etc.) flow through.
278
if let modifiers.storage_class? /\ !storage_class.is_init then
279
storage = storage_class.copy()
280
fi
281
282
return Trees.Modifiers.LIST(loc, access, storage)
283
si
284
285
// See `DESTRUCTURE_RESOLVER` for the precedence the synthesised
286
// deconstruct slots into. Guard preconditions:
287
// - User wrote no `deconstruct` of any arity in this class body.
288
// - User exposed no backtick-numeric property (`0`, `1`, ...).
289
// - At least one public-readable capture exists to expose.
290
// The synthesised method is `public`, instance, with one `T ref`
291
// parameter per included capture (named after the capture so the
292
// body reads naturally; the destructure call site only ever
293
// matches by arity + ref-ness).
294
_maybe_synthesise_deconstruct(
295
classy: Trees.Definitions.Classy,
296
class_location: LOCATION,
297
primary_params: Trees.Variables.LIST,
298
param_for_capture: Collections.MAP[Trees.Definitions.PROPERTY, Trees.Variables.VARIABLE],
299
already_captured_param_names: Collections.SET[string],
300
kept: Collections.MutableList[Trees.Definitions.Definition]
301
) is
302
for d in classy.body do
303
if isa Trees.Definitions.FUNCTION(d) then
304
let f = cast Trees.Definitions.FUNCTION?(d)!
305
if f.name? /\ f.name.name =~ "deconstruct" then
306
return
307
fi
308
elif isa Trees.Definitions.PROPERTY(d) then
309
let p = cast Trees.Definitions.PROPERTY?(d)!
310
if p.name? /\ Semantic.Symbols.Symbol.is_positional_member_name(p.name.name) then
311
return
312
fi
313
fi
314
od
315
316
// Reverse-lookup table: primary-ctor parameter name -> its
317
// capture property. Built once so the capture-collection loop
318
// below stays O(captures).
319
let capture_for_param_name = Collections.MAP[string, Trees.Definitions.PROPERTY]()
320
for kvp in param_for_capture do
321
let cap = kvp.key
322
let param = kvp.value
323
if let param.name? then
324
capture_for_param_name[name.name] = cap
325
fi
326
od
327
328
let deconstruct_captures = Collections.LIST[Trees.Definitions.PROPERTY]()
329
for p in primary_params do
330
let name = p.name
331
if !name? then
332
continue
333
fi
334
if !already_captured_param_names.contains(name.name) then
335
continue
336
fi
337
if !capture_for_param_name.contains_key(name.name) then
338
continue
339
fi
340
let capture = capture_for_param_name[name.name]
341
if !_is_public_readable_capture(capture) then
342
continue
343
fi
344
deconstruct_captures.add(capture)
345
od
346
347
if deconstruct_captures.count == 0 then
348
return
349
fi
350
351
let deconstruct = _synthesise_deconstruct(class_location, deconstruct_captures)
352
353
// Wrap in an @IL.name("Deconstruct") pragma so the method
354
// satisfies the standard .NET `Deconstruct` contract used by
355
// C# positional patterns and other cross-language consumers.
356
kept.add(_wrap_in_il_name_pragma(class_location, "Deconstruct", deconstruct))
357
si
358
359
_is_public_readable_capture(p: Trees.Definitions.PROPERTY) -> bool is
360
// ghūl carries read-visibility two ways: an explicit
361
// access modifier, or a leading `_` on the name (the
362
// convention: "_x" -> protected for reading). Both must
363
// be checked — the synthesised deconstruct is `public`,
364
// and surfacing a protected member through it would
365
// bypass the encapsulation the user signalled.
366
if let p.modifiers?, modifiers.access_modifier? /\ access_modifier.is_private then
367
return false
368
fi
369
if p.name? /\ p.name.name.length > 0 /\ p.name.name[0] == '_' then
370
return false
371
fi
372
return true
373
si
374
375
_synthesise_deconstruct(
376
class_location: LOCATION,
377
captures: Collections.List[Trees.Definitions.PROPERTY]
378
) -> Trees.Definitions.FUNCTION is
379
let arg_vars = Collections.LIST[Trees.Variables.VARIABLE]()
380
let body_stmts = Collections.LIST[Trees.Statements.Statement]()
381
382
for capture in captures do
383
let cap_loc = capture.location
384
let arg_type =
385
Trees.TypeExpressions.REFERENCE(
386
cap_loc,
387
capture.type_expression.copy()
388
)
389
arg_type.is_synthesised = true
390
let arg =
391
Trees.Variables.VARIABLE(
392
cap_loc,
393
capture.name!.copy(),
394
arg_type,
395
false,
396
true,
397
null
398
)
399
arg.mark_argument()
400
arg_vars.add(arg)
401
402
body_stmts.add(_make_deconstruct_assignment(capture))
403
od
404
405
let arg_list = Trees.Variables.LIST(class_location, arg_vars)
406
let body =
407
Trees.Bodies.BLOCK(
408
LOCATION.internal,
409
Trees.Statements.LIST(LOCATION.internal, body_stmts)
410
)
411
let modifiers =
412
Trees.Modifiers.LIST(
413
class_location,
414
Trees.Modifiers.PUBLIC(class_location),
415
null
416
)
417
let deconstruct =
418
Trees.Definitions.FUNCTION(
419
class_location,
420
Trees.Identifiers.Identifier(LOCATION.internal, "deconstruct"),
421
Trees.TypeExpressions.LIST(LOCATION.internal, Collections.LIST[Trees.TypeExpressions.TypeExpression](0)),
422
arg_list,
423
Trees.TypeExpressions.INFER(class_location),
424
modifiers,
425
body
426
)
427
deconstruct.mark_synthesized()
428
429
return deconstruct
430
si
431
432
_make_deconstruct_assignment(
433
capture: Trees.Definitions.PROPERTY
434
) -> Trees.Statements.Statement is
435
let loc = capture.location
436
437
// <capture-name>! = self.<capture-name>;
438
//
439
// Postfix `!` on a `T ref` LHS lowers to a `stobj <T>` —
440
// the write-through-ref form. Without it,
441
// `<capture-name> = self.<...>` would error with
442
// "Ghul.T is not assignable to Ghul.T ref".
443
let arg_ref =
444
Trees.Expressions.IDENTIFIER(loc, capture.name!.copy())
445
let unwrap = Trees.Expressions.UNWRAP(loc, arg_ref)
446
let left = Trees.Expressions.SIMPLE_LEFT_EXPRESSION(loc, unwrap)
447
448
let self_expr = Trees.Expressions.SELF(loc)
449
let right =
450
Trees.Expressions.MEMBER(
451
loc,
452
self_expr,
453
capture.name!.copy(),
454
loc
455
)
456
457
return Trees.Statements.ASSIGNMENT(loc, left, right)
458
si
459
460
_wrap_in_il_name_pragma(
461
loc: LOCATION,
462
il_name: string,
463
definition: Trees.Definitions.Definition
464
) -> Trees.Definitions.PRAGMA is
465
let name_literal = Trees.Expressions.Literals.STRING(loc, il_name)
466
let args = Collections.LIST[Trees.Expressions.Expression]()
467
args.add(name_literal)
468
let pragma =
469
Trees.Pragmas.PRAGMA(
470
loc,
471
Trees.Identifiers.Identifier(loc, IL_PRAGMAS.NAME),
472
Trees.Expressions.LIST(loc, args),
473
null
474
)
475
476
pragma.is_synthesised = true
477
478
return Trees.Definitions.PRAGMA(loc, pragma, definition)
479
si
480
481
_make_super_init_call(
482
super_call: Trees.Definitions.SUPER_CALL
483
) -> Trees.Statements.Statement is
484
let loc = super_call.location
485
let arg_exprs = Collections.LIST[Trees.Expressions.Expression]()
486
487
for arg_expr in super_call.args do
488
arg_exprs.add(arg_expr)
489
od
490
491
// super.init(args)
492
let super_expr = Trees.Expressions.SUPER(loc)
493
let init_identifier = Trees.Identifiers.Identifier(loc, "init")
494
let member =
495
Trees.Expressions.MEMBER(
496
loc,
497
super_expr,
498
init_identifier,
499
loc
500
)
501
let call =
502
Trees.Expressions.CALL(
503
loc,
504
member,
505
Trees.Expressions.LIST(loc, arg_exprs)
506
)
507
508
return Trees.Statements.EXPRESSION(loc, call)
509
si
510
511
_make_capture_assignment(capture: Trees.Definitions.PROPERTY, param: Trees.Variables.VARIABLE) -> Trees.Statements.Statement is
512
let loc = capture.location
513
514
// self.<capture-name> = <param-name>;
515
let self_expr = Trees.Expressions.SELF(loc)
516
let member =
517
Trees.Expressions.MEMBER(
518
loc,
519
self_expr,
520
capture.name!.copy(),
521
loc
522
)
523
let left = Trees.Expressions.SIMPLE_LEFT_EXPRESSION(loc, member)
524
let right =
525
Trees.Expressions.IDENTIFIER(
526
loc,
527
param.name!.copy()
528
)
529
530
return Trees.Statements.ASSIGNMENT(loc, left, right)
531
si
532
533
// Expand `..` in the variant's field list against the
534
// enclosing union's primary parameters. The splice marker is
535
// replaced in-place by deep-copies of the primary params, each
536
// marked is_inherited_primary. They stay in variant.fields so
537
// every existing consumer (arity, equality, hash, destructure)
538
// continues to see the variant's full shape. declare_symbols
539
// skips declaring inherited entries as variant-side fields —
540
// the union base owns the storage and variants inherit through
541
// the union → variant base-class relationship. The variant's
542
// synthesised init forwards inherited args to super.init(...);
543
// non-inherited fields get the usual self.<f> = <f>. Validates
544
// exactly one `..` per variant.
545
_expand_variant_splice(variant: Trees.Definitions.VARIANT, primary_params: Trees.Variables.LIST) is
546
if variant.is_poisoned then
547
return
548
fi
549
550
let splice_count = _count_splices(variant.fields)
551
552
if splice_count == 0 then
553
if variant.fields.count == 0 then
554
// No field list — splice the primary parameters as if
555
// the user had written `(..)`.
556
let new_fields = Collections.LIST[Trees.Variables.VARIABLE]()
557
558
for p in primary_params do
559
let inherited = _copy_variable(p)
560
inherited.mark_inherited_primary(_body_property_name(p).name)
561
new_fields.add(inherited)
562
od
563
564
variant.set_fields(Trees.Variables.LIST(variant.fields.location, new_fields))
565
return
566
fi
567
568
_logger.error(
569
variant.location,
570
"variant of a union with a primary constructor header must include .. to splice in the primary parameters"
571
)
572
return
573
fi
574
575
if splice_count > 1 then
576
_logger.error(
577
variant.location,
578
"variant field list contains more than one .."
579
)
580
fi
581
582
let new_fields = Collections.LIST[Trees.Variables.VARIABLE]()
583
let expanded mut = false
584
585
for v in variant.fields do
586
if v.is_splice then
587
if !expanded then
588
for p in primary_params do
589
let inherited = _copy_variable(p)
590
inherited.mark_inherited_primary(_body_property_name(p).name)
591
new_fields.add(inherited)
592
od
593
594
expanded = true
595
fi
596
else
597
new_fields.add(v)
598
fi
599
od
600
601
variant.set_fields(Trees.Variables.LIST(variant.fields.location, new_fields))
602
si
603
604
_reject_stray_splice(variant: Trees.Definitions.VARIANT) is
605
if variant.is_poisoned then
606
return
607
fi
608
609
for v in variant.fields do
610
if v.is_splice /\ !v.is_poisoned then
611
_logger.error(
612
v.location,
613
".. requires the surrounding union to declare a primary constructor header"
614
)
615
v.poison(true)
616
fi
617
od
618
si
619
620
_expand_secondary_init(f: Trees.Definitions.FUNCTION, primary_params: Trees.Variables.LIST) is
621
// Build the new argument list: every existing argument
622
// except the splice marker, with the primary parameters
623
// spliced in at the first splice's position. Any extra
624
// splice markers (already flagged as errors above) are
625
// dropped — expanding more than one would duplicate the
626
// primary parameter names and cascade into redefinition
627
// diagnostics that the user's own error already accounts
628
// for.
629
let old_args = f.arguments.variables
630
let new_args = Collections.LIST[Trees.Variables.VARIABLE]()
631
let expanded mut = false
632
633
for v in old_args do
634
if v.is_splice then
635
if !expanded then
636
for p in primary_params do
637
new_args.add(_copy_variable(p))
638
od
639
640
expanded = true
641
fi
642
else
643
new_args.add(v)
644
fi
645
od
646
647
let expanded_args =
648
Trees.Variables.LIST(
649
f.arguments.location,
650
new_args
651
)
652
653
f.arguments = expanded_args
654
655
// Prepend `self.init(<primary args>);` to the body.
656
if f.body? /\ isa Trees.Bodies.BLOCK(f.body) then
657
let block = cast Trees.Bodies.BLOCK(f.body)
658
let chain = _make_self_init_chain(f.location, primary_params)
659
660
let new_statements = Collections.LIST[Trees.Statements.Statement]()
661
new_statements.add(chain)
662
663
for s in block.statements do
664
new_statements.add(s)
665
od
666
667
block.statements.replace_all(new_statements)
668
fi
669
si
670
671
_make_self_init_chain(loc: LOCATION, primary_params: Trees.Variables.LIST) -> Trees.Statements.Statement is
672
let arg_exprs = Collections.LIST[Trees.Expressions.Expression]()
673
674
for p in primary_params do
675
arg_exprs.add(
676
Trees.Expressions.IDENTIFIER(loc, p.name!.copy())
677
)
678
od
679
680
let self_expr = Trees.Expressions.SELF(loc)
681
let init_identifier = Trees.Identifiers.Identifier(loc, "init")
682
let member =
683
Trees.Expressions.MEMBER(
684
loc,
685
self_expr,
686
init_identifier,
687
loc
688
)
689
let call =
690
Trees.Expressions.CALL(
691
loc,
692
member,
693
Trees.Expressions.LIST(loc, arg_exprs)
694
)
695
696
return Trees.Statements.EXPRESSION(loc, call)
697
si
698
699
_copy_variable(v: Trees.Variables.VARIABLE) -> Trees.Variables.VARIABLE is
700
let result =
701
Trees.Variables.VARIABLE(
702
v.location,
703
v.name!.copy(),
704
v.type_expression.copy(),
705
false,
706
true,
707
null
708
)
709
710
result.mark_argument()
711
712
return result
713
si
714
715
_copy_variable_list(list: Trees.Variables.LIST) -> Trees.Variables.LIST is
716
let copied = Collections.LIST[Trees.Variables.VARIABLE]()
717
718
for v in list do
719
copied.add(_copy_variable(v))
720
od
721
722
return Trees.Variables.LIST(list.location, copied)
723
si
724
si
725
si