/
variable-declaration.rakumod
1107 lines (974 loc) · 40.2 KB
/
variable-declaration.rakumod
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# All initializers do this marker. An initializer is the `= 42` part in a
# declaration like `my $a = 42`.
class RakuAST::Initializer is RakuAST::Node {
method is-binding() { False }
}
# An assignment (`=`) initializer.
class RakuAST::Initializer::Assign is RakuAST::Initializer {
has RakuAST::Expression $.expression;
method new(RakuAST::Expression $expression) {
my $obj := nqp::create(self);
nqp::bindattr($obj, RakuAST::Initializer::Assign, '$!expression', $expression);
$obj
}
method visit-children(Code $visitor) {
$visitor($!expression);
}
method IMPL-TO-QAST(RakuAST::IMPL::QASTContext $context) {
$!expression.IMPL-TO-QAST($context)
}
}
# A bind (`:=`) initializer.
class RakuAST::Initializer::Bind is RakuAST::Initializer {
has RakuAST::Expression $.expression;
method new(RakuAST::Expression $expression) {
my $obj := nqp::create(self);
nqp::bindattr($obj, RakuAST::Initializer::Bind, '$!expression', $expression);
$obj
}
method is-binding() { True }
method visit-children(Code $visitor) {
$visitor($!expression);
}
method IMPL-TO-QAST(RakuAST::IMPL::QASTContext $context) {
$!expression.IMPL-TO-QAST($context)
}
}
class RakuAST::ContainerCreator {
has Mu $!container-base-type;
method IMPL-SET-CONTAINER-BASE-TYPE(Mu $type) {
nqp::bindattr(self, RakuAST::ContainerCreator, '$!container-base-type', $type);
}
method IMPL-HAS-CONTAINER-BASE-TYPE() {
!nqp::eqaddr($!container-base-type, Mu)
}
method IMPL-CONTAINER-BASE-TYPE() {
$!container-base-type
}
method IMPL-CONTAINER-DESCRIPTOR(Mu $of) {
# If it's a natively typed scalar, no container.
my str $sigil := self.sigil;
my int $is-native := nqp::objprimspec($of);
if $sigil eq '$' && $is-native {
return Nil;
}
# Form container descriptor.
my $default := self.type ?? $of !! Any;
my int $dynamic := self.twigil eq '*' ?? 1 !! 0;
(
nqp::eqaddr($of, Mu)
?? ContainerDescriptor::Untyped
!! ContainerDescriptor
).new(:$of, :$default, :$dynamic, :name(self.lexical-name))
}
method IMPL-CONTAINER(Mu $of, Mu $cont-desc) {
# Form the container.
my $default := self.type ?? $of !! Any;
my str $sigil := self.sigil;
my $container-base-type;
my $container-type;
if nqp::eqaddr($!container-base-type, Mu) {
if $sigil eq '@' {
$container-base-type := Array;
$container-type := self.type
?? Array.HOW.parameterize(Array, $of)
!! Array;
}
elsif $sigil eq '%' {
$container-base-type := Hash;
$container-type := self.type
?? Hash.HOW.parameterize(Hash, $of)
!! Hash;
}
else {
$container-base-type := Scalar;
$container-type := Scalar;
}
my $container := nqp::create($container-type);
nqp::bindattr($container, $container-base-type, '$!descriptor', $cont-desc);
unless $sigil eq '@' || $sigil eq '%' {
nqp::bindattr($container, $container-base-type, '$!value', $default);
}
$container
}
else {
$!container-base-type
}
}
}
class RakuAST::TraitTarget::Variable is RakuAST::TraitTarget is RakuAST::Meta is RakuAST::ImplicitLookups is RakuAST::BeginTime {
has str $!name;
has str $!scope;
has Mu $!cont;
has Mu $!code-object;
has Mu $!slash;
method new(str $name, str $scope, Mu $cont, Mu $code-object, Mu $slash) {
my $obj := nqp::create(self);
nqp::bindattr_s($obj, RakuAST::TraitTarget::Variable, '$!name', $name);
nqp::bindattr_s($obj, RakuAST::TraitTarget::Variable, '$!scope', $scope);
nqp::bindattr($obj, RakuAST::TraitTarget::Variable, '$!cont', $cont);
nqp::bindattr($obj, RakuAST::TraitTarget::Variable, '$!code-object', $code-object);
nqp::bindattr($obj, RakuAST::TraitTarget::Variable, '$!slash', $slash);
$obj
}
method PRODUCE-IMPLICIT-LOOKUPS() {
self.IMPL-WRAP-LIST([
RakuAST::Type::Setting.new(RakuAST::Name.from-identifier('Variable')),
])
}
method PERFORM-BEGIN(RakuAST::Resolver $resolver, RakuAST::IMPL::QASTContext $context) {
}
method PRODUCE-META-OBJECT() {
my @lookups := self.IMPL-UNWRAP-LIST(self.get-implicit-lookups);
my $Variable := @lookups[0].compile-time-value;
my $varvar := nqp::create($Variable);
nqp::bindattr_s($varvar, $Variable, '$!name', $!name);
nqp::bindattr_s($varvar, $Variable, '$!scope', $!scope);
nqp::bindattr($varvar, $Variable, '$!var', $!cont);
nqp::bindattr($varvar, $Variable, '$!block', $!code-object);
nqp::bindattr($varvar, $Variable, '$!slash', $!slash);
nqp::assign(
nqp::getattr($varvar, $Variable, '$!implicit-lexical-usage'),
True);
$varvar
}
}
# A basic variable declaration of the form `my SomeType $foo = 42` or `has Foo $x .= new`.
class RakuAST::VarDeclaration::Simple is RakuAST::Declaration is RakuAST::ImplicitLookups
is RakuAST::TraitTarget is RakuAST::ContainerCreator
is RakuAST::Meta is RakuAST::Attaching is RakuAST::BeginTime
is RakuAST::Term {
has RakuAST::Type $.type;
has str $.name;
has str $!storage-name;
has RakuAST::Initializer $.initializer;
has RakuAST::Package $!attribute-package;
has Mu $!package;
method new(str :$name!, RakuAST::Type :$type, RakuAST::Initializer :$initializer,
str :$scope) {
my $obj := nqp::create(self);
if nqp::chars($name) < 2 {
nqp::die('Cannot use RakuAST::VarDeclaration::Simple to declare an anonymous varialbe; use RakuAST::VarDeclaration::Anonymous');
}
nqp::bindattr_s($obj, RakuAST::VarDeclaration::Simple, '$!name', $name);
nqp::bindattr_s($obj, RakuAST::Declaration, '$!scope', $scope);
nqp::bindattr($obj, RakuAST::VarDeclaration::Simple, '$!type', $type // RakuAST::Type);
nqp::bindattr($obj, RakuAST::VarDeclaration::Simple, '$!initializer',
$initializer // RakuAST::Initializer);
$obj
}
method lexical-name() {
$!name
}
method sigil() {
nqp::substr($!name, 0, 1)
}
method twigil() {
if nqp::chars($!name) > 2 {
my str $twigil := nqp::substr($!name, 1, 1);
nqp::index('.!^:*?=~', $twigil) >= 0 ?? $twigil !! ''
}
else {
''
}
}
method desigilname() {
nqp::substr($!name, self.twigil ?? 2 !! 1)
}
# Generate a lookup of this variable, already resolved to this declaration.
method generate-lookup() {
if self.is-lexical {
my $lookup := RakuAST::Var::Lexical.new($!name);
$lookup.set-resolution(self);
$lookup
}
else {
nqp::die('Cannot generate lookup of simple var for scope ' ~ self.scope);
}
}
method can-be-bound-to() {
# Must be lexical and non-native.
if self.scope eq 'my' {
my str $sigil := self.sigil;
return True if $sigil eq '@' || $sigil eq '%';
return True unless $!type;
my @lookups := self.IMPL-UNWRAP-LIST(self.get-implicit-lookups());
return True unless nqp::objprimspec(@lookups[0].resolution.compile-time-value);
}
False
}
method visit-children(Code $visitor) {
my $type := $!type;
$visitor($type) if nqp::isconcrete($type);
my $initializer := $!initializer;
$visitor($initializer) if nqp::isconcrete($initializer);
self.visit-traits($visitor);
}
method default-scope() {
'my'
}
method allowed-scopes() {
self.IMPL-WRAP-LIST(['my', 'state', 'our', 'has', 'HAS'])
}
method is-lexical() {
# Overridden here because our-scoped variables are really lexical aliases.
my str $scope := self.scope;
$scope eq 'my' || $scope eq 'state' || $scope eq 'our'
}
method attach(RakuAST::Resolver $resolver) {
my str $scope := self.scope;
if $scope eq 'has' || $scope eq 'HAS' {
my $attribute-package := $resolver.find-attach-target('package');
if $attribute-package {
nqp::bindattr(self, RakuAST::VarDeclaration::Simple, '$!attribute-package',
$attribute-package);
}
else {
# TODO check-time error
}
}
elsif $scope eq 'our' {
my $package := $resolver.current-package;
# There is always a package, even if it's just GLOBALish
nqp::bindattr(self, RakuAST::VarDeclaration::Simple, '$!package',
$package);
}
}
method PERFORM-BEGIN(RakuAST::Resolver $resolver, RakuAST::IMPL::QASTContext $context) {
if $!attribute-package {
$!attribute-package.ATTACH-ATTRIBUTE(self);
}
# Process traits for `is Type` and `of Type`, which get special
# handling by the compiler.
my @late-traits;
my @traits := self.IMPL-UNWRAP-LIST(self.traits);
for @traits {
if nqp::istype($_, RakuAST::Trait::Of) {
npq::die('of trait not yet implemented on variables');
}
elsif nqp::istype($_, RakuAST::Trait::Is) {
my $type := $_.resolved-name;
# an actual type
if nqp::isconcrete($type) && !$_.argument && $type.is-resolved {
self.IMPL-SET-CONTAINER-BASE-TYPE($type.resolution.compile-time-value);
next;
}
}
nqp::push(@late-traits, $_);
}
# Apply any traits.
self.set-traits(self.IMPL-WRAP-LIST(@late-traits));
my str $scope := self.scope;
if $scope eq 'has' || $scope eq 'HAS' {
# For attributes our meta-object is an appropriate Attribute instance
self.apply-traits($resolver, $context, self);
}
else {
# For other variables the meta-object is just the container, but we
# need instances of Variable
my $meta := self.meta-object;
my $target := RakuAST::TraitTarget::Variable.new($!name, nqp::getattr(self, RakuAST::Declaration, '$!scope'), $meta, Mu, Mu);
# Get around RakuAST compiler deconting all arguments:
nqp::bindattr($target, RakuAST::TraitTarget::Variable, '$!cont', $meta);
$target.IMPL-CHECK($resolver, $context, False);
self.apply-traits($resolver, $context, $target);
}
}
method PRODUCE-IMPLICIT-LOOKUPS() {
my @lookups;
# If it's our-scoped, we need the package to bind it from.
my str $scope := self.scope;
if $scope eq 'our' {
@lookups.push(RakuAST::Var::Compiler::Lookup.new('$?PACKAGE'));
}
# If we have a type, we need to resolve that.
elsif $!type {
@lookups.push($!type);
}
# If we're has/HAS scope, we need Nil to evaluate to.
if $scope eq 'has' || $scope eq 'HAS' {
@lookups.push(RakuAST::Type::Setting.new(RakuAST::Name.from-identifier('Nil')));
}
self.IMPL-WRAP-LIST(@lookups)
}
method PRODUCE-META-OBJECT() {
# If it's our-scoped, then container is vivified via. package access.
my str $scope := self.scope;
return Nil if $scope eq 'our';
# Calculate the type.
my @lookups := self.IMPL-UNWRAP-LIST(self.get-implicit-lookups());
my $of := $!type ?? @lookups[0].resolution.compile-time-value !! Mu;
# If it's has scoped, we'll need to build an attribute.
if $scope eq 'has' || $scope eq 'HAS' {
my $cont-desc := self.IMPL-CONTAINER-DESCRIPTOR($of);
$!attribute-package.attribute-type.new(
name => self.sigil ~ '!' ~ self.desigilname,
type => $of,
has_accessor => self.twigil eq '.',
container_descriptor => $cont-desc,
auto_viv_container => self.IMPL-CONTAINER($of, $cont-desc),
package => $!attribute-package.compile-time-value
)
}
# Otherwise, it's lexically scoped, so the meta-object is just the
# container, if any.
else {
my $cont-desc := self.IMPL-CONTAINER-DESCRIPTOR($of);
self.IMPL-CONTAINER($of, $cont-desc)
}
}
method IMPL-QAST-DECL(RakuAST::IMPL::QASTContext $context) {
my @lookups := self.IMPL-UNWRAP-LIST(self.get-implicit-lookups());
my str $scope := self.scope;
if $scope eq 'my' {
# Lexically scoped
my $of := $!type ?? @lookups[0].resolution.compile-time-value !! Mu;
my str $sigil := self.sigil;
if $sigil eq '$' && nqp::objprimspec($of) {
# Natively typed; just declare it.
QAST::Var.new(
:scope('lexical'), :decl('var'), :name($!name),
:returns($of)
)
}
elsif $!initializer && $!initializer.is-binding {
# Will be bound on first use, so just a declaration.
QAST::Var.new( :scope('lexical'), :decl('var'), :name($!name) )
}
else {
# Need to vivify the object. Note: maybe we want to drop the
# contvar, though we'll need an alternative for BEGIN.
my $container := self.meta-object;
$context.ensure-sc($container);
my $qast := QAST::Var.new(
:scope('lexical'), :decl('contvar'), :name($!name),
:value($container)
);
if self.IMPL-HAS-CONTAINER-BASE-TYPE {
$qast := QAST::Op.new( :op('bind'), $qast, QAST::Op.new(
:op('callmethod'), :name('new'),
QAST::WVal.new( :value(self.IMPL-CONTAINER-BASE-TYPE) )
) );
}
$qast
}
}
elsif $scope eq 'our' {
# Package scoped lexical alias. We want to bind the lexical to a lookup
# in the package.
my $name := RakuAST::Name.from-identifier($!name);
QAST::Op.new(
:op('bind'),
QAST::Var.new( :scope('lexical'), :decl('var'), :name($!name) ),
$name.IMPL-QAST-PACKAGE-LOOKUP($context, QAST::WVal.new(:value($!package)))
)
}
elsif $scope eq 'has' || $scope eq 'HAS' {
# No declaration to install
QAST::Op.new( :op('null') )
}
elsif $scope eq 'state' {
# Lexically scoped state variable
my $of := $!type ?? @lookups[0].resolution.compile-time-value !! Mu;
my str $sigil := self.sigil;
if $sigil eq '$' && nqp::objprimspec($of) {
nqp::die("Natively typed state variables not yet implemented");
}
elsif $!initializer && $!initializer.is-binding {
# Will be bound on first use, so just a declaration.
QAST::Var.new(:scope('lexical'), :decl('var'), :name($!name))
}
else {
# Need to vivify the object.
my $container := self.meta-object;
$context.ensure-sc($container);
QAST::Var.new(
:scope('lexical'), :decl('statevar'), :name($!name),
:value($container)
)
}
}
else {
nqp::die("Don't know how to compile $scope scope variable");
}
}
method IMPL-TO-QAST(RakuAST::IMPL::QASTContext $context) {
my str $scope := self.scope;
my @lookups := self.IMPL-UNWRAP-LIST(self.get-implicit-lookups());
if $scope eq 'my' || $scope eq 'state' || $scope eq 'our' {
my str $name := $!name;
my str $sigil := self.sigil;
my $var-access := QAST::Var.new( :$name, :scope<lexical> );
my $of := $!type ?? @lookups[0].resolution.compile-time-value !! Mu;
if $sigil eq '$' && (my int $prim-spec := nqp::objprimspec($of)) {
# Natively typed value. Need to initialize it to a default
# in the absence of an initializer.
my $init;
if $!initializer {
if nqp::istype($!initializer, RakuAST::Initializer::Assign) {
$init := $!initializer.expression.IMPL-TO-QAST($context);
}
else {
nqp::die('Can only compile an assign initializer on a native');
}
}
elsif $prim-spec == 1 {
$init := QAST::IVal.new( :value(0) );
}
elsif $prim-spec == 2 {
$init := QAST::NVal.new( :value(0e0) );
}
else {
$init := QAST::SVal.new( :value('') );
}
QAST::Op.new( :op('bind'), $var-access, $init )
}
else {
# Reference type value.
if $!initializer {
my $init-qast := $!initializer.IMPL-TO-QAST($context);
my $perform-init-qast;
if $!initializer.is-binding {
# TODO type checking of source
my $source := $sigil eq '@' || $sigil eq '%'
?? QAST::Op.new( :op('decont'), $init-qast)
!! $init-qast;
$perform-init-qast := QAST::Op.new( :op('bind'), $var-access, $source );
}
else {
# Assignment. Case-analyze by sigil.
if $sigil eq '@' || $sigil eq '%' {
# Call STORE method, passing :INITIALIZE to indicate it's
# the initialization for immutable types.
$perform-init-qast := QAST::Op.new(
:op('callmethod'), :name('STORE'),
$var-access,
$init-qast,
QAST::WVal.new( :named('INITIALIZE'), :value(True) )
);
}
else {
# Scalar assignment.
$perform-init-qast := QAST::Op.new(
:op('p6assign'), $var-access, $init-qast );
}
}
if $scope eq 'state' {
QAST::Op.new(
:op('if'),
QAST::Op.new( :op('p6stateinit') ),
$perform-init-qast,
$var-access
)
}
else {
$perform-init-qast
}
}
else {
# Just a declaration; compile into an access to the variable.
$var-access
}
}
}
elsif $scope eq 'has' || $scope eq 'HAS' {
# These just evaluate to Nil
@lookups[nqp::elems(@lookups) - 1].IMPL-TO-QAST($context)
}
else {
nqp::die("Don't know how to compile initialization for scope $scope");
}
}
method IMPL-LOOKUP-QAST(RakuAST::IMPL::QASTContext $context, Mu :$rvalue) {
my str $scope := self.scope;
if $scope eq 'my' || $scope eq 'state' || $scope eq 'our' {
my str $scope := 'lexical';
unless $rvalue {
# Potentially l-value native lookups need a lexicalref.
if self.sigil eq '$' && self.scope ne 'our' {
my @lookups := self.IMPL-UNWRAP-LIST(self.get-implicit-lookups());
my $of := $!type ?? @lookups[0].resolution.compile-time-value !! Mu;
if nqp::objprimspec($of) {
$scope := 'lexicalref';
}
return QAST::Var.new( :name($!name), :$scope, :returns($of) );
}
}
QAST::Var.new( :name($!name), :$scope )
}
elsif $scope eq 'has' || $scope eq 'HAS' {
nqp::die('Cannot compile lookup of attributes yet')
}
else {
nqp::die("Cannot compile lookup of scope $scope")
}
}
method IMPL-BIND-QAST(RakuAST::IMPL::QASTContext $context, RakuAST::Expression $source) {
my str $scope := self.scope;
nqp::die('Can only compile bind to my-scoped variables') unless $scope eq 'my';
QAST::Op.new(
:op('bind'),
QAST::Var.new( :name($!name), :scope('lexical') ),
$source.IMPL-TO-QAST($context)
)
}
method needs-sink-call() { False }
}
class RakuAST::VarDeclaration::Signature is RakuAST::Declaration is RakuAST::ImplicitLookups
is RakuAST::TraitTarget is RakuAST::CheckTime
is RakuAST::Attaching is RakuAST::BeginTime {
has RakuAST::Signature $.signature;
has RakuAST::Type $.type;
has RakuAST::Initializer $.initializer;
has RakuAST::Package $!attribute-package;
has Mu $!package;
method new(RakuAST::Signature :$signature!, RakuAST::Type :$type, RakuAST::Initializer :$initializer,
str :$scope) {
my $obj := nqp::create(self);
nqp::bindattr($obj, RakuAST::VarDeclaration::Signature, '$!signature', $signature);
nqp::bindattr_s($obj, RakuAST::Declaration, '$!scope', $scope);
nqp::bindattr($obj, RakuAST::VarDeclaration::Signature, '$!type', $type // RakuAST::Type);
nqp::bindattr($obj, RakuAST::VarDeclaration::Signature, '$!initializer',
$initializer // RakuAST::Initializer);
$obj
}
method visit-children(Code $visitor) {
$visitor($!signature);
my $type := $!type;
$visitor($type) if nqp::isconcrete($type);
my $initializer := $!initializer;
$visitor($initializer) if nqp::isconcrete($initializer);
}
method default-scope() {
'my'
}
method allowed-scopes() {
self.IMPL-WRAP-LIST(['my', 'state', 'our', 'has', 'HAS'])
}
method is-simple-lexical-declaration() {
# The ParameterTargets in our signature will take care of declarations
False
}
method is-lexical() {
# Overridden here because our-scoped variables are really lexical aliases.
my str $scope := self.scope;
$scope eq 'my' || $scope eq 'state' || $scope eq 'our'
}
method attach(RakuAST::Resolver $resolver) {
my str $scope := self.scope;
if $scope eq 'has' || $scope eq 'HAS' {
my $attribute-package := $resolver.find-attach-target('package');
if $attribute-package {
nqp::bindattr(self, RakuAST::VarDeclaration::Signature, '$!attribute-package',
$attribute-package);
}
else {
# TODO check-time error
}
}
elsif $scope eq 'our' {
my $package := $resolver.current-package;
# There is always a package, even if it's just GLOBALish
nqp::bindattr(self, RakuAST::VarDeclaration::Signature, '$!package',
$package);
}
}
method PRODUCE-IMPLICIT-LOOKUPS() {
my @lookups;
# If it's our-scoped, we need the package to bind it from.
my str $scope := self.scope;
if $scope eq 'our' {
@lookups.push(RakuAST::Var::Compiler::Lookup.new('$?PACKAGE'));
}
# If we have a type, we need to resolve that.
elsif $!type {
@lookups.push($!type);
}
# If we're has/HAS scope, we need Nil to evaluate to.
if $scope eq 'has' || $scope eq 'HAS' {
@lookups.push(RakuAST::Type::Setting.new(RakuAST::Name.from-identifier('Nil')));
}
self.IMPL-WRAP-LIST(@lookups)
}
method PERFORM-BEGIN(RakuAST::Resolver $resolver, RakuAST::IMPL::QASTContext $context) {
if $!attribute-package {
$!attribute-package.ATTACH-ATTRIBUTE(self);
}
}
method PERFORM-CHECK(RakuAST::Resolver $resolver, RakuAST::IMPL::QASTContext $context) {
# tell the parameter targets to create containers
my @params := self.IMPL-UNWRAP-LIST($!signature.parameters);
my @lookups := self.IMPL-UNWRAP-LIST(self.get-implicit-lookups());
my $of := $!type ?? @lookups[0].resolution.compile-time-value !! Mu;
my $type := $!type;
unless $type {
$type := RakuAST::Type::Setting.new('Mu');
$type.set-resolution($resolver.resolve-lexical-constant-in-setting('Mu'));
}
for @params {
$_.target.set-container-type($type, $of);
}
}
method IMPL-TO-QAST(RakuAST::IMPL::QASTContext $context) {
if nqp::isconcrete($!initializer) {
if nqp::istype($!initializer, RakuAST::Initializer::Assign) {
my $list := QAST::Op.new( :op('call'), :name('&infix:<,>') );
my @params := self.IMPL-UNWRAP-LIST($!signature.parameters);
for @params {
$list.push: $_.target.IMPL-LOOKUP-QAST($context);
}
my $init-qast := $!initializer.IMPL-TO-QAST($context);
$list := QAST::Op.new( :op('p6store'), $list, $init-qast);
return $list;
}
else {
nqp::die('Not yet supported signature initializer: ' ~ $!initializer.HOW.name($!initializer));
}
}
QAST::Op.new(:op<null>);
}
method needs-sink-call() { False }
}
# An anonymous variable declaration, such as `my $ = 42`
class RakuAST::VarDeclaration::Anonymous is RakuAST::VarDeclaration::Simple {
has str $.sigil;
method new(str :$sigil!, RakuAST::Type :$type, RakuAST::Initializer :$initializer,
str :$scope) {
my $obj := nqp::create(self);
nqp::bindattr_s($obj, RakuAST::VarDeclaration::Simple, '$!name',
self.IMPL-GENERATE-NAME());
nqp::bindattr_s($obj, RakuAST::VarDeclaration::Anonymous, '$!sigil', $sigil);
nqp::bindattr_s($obj, RakuAST::Declaration, '$!scope', $scope);
nqp::bindattr($obj, RakuAST::VarDeclaration::Simple, '$!type', $type // RakuAST::Type);
nqp::bindattr($obj, RakuAST::VarDeclaration::Simple, '$!initializer',
$initializer // RakuAST::Initializer);
$obj
}
method IMPL-GENERATE-NAME() {
QAST::Node.unique('ANON_VAR')
}
method sigil() {
$!sigil
}
method twigil() {
''
}
method desigilname() {
''
}
method generate-lookup() {
nqp::die('Cannot generate lookup of an anonymous variable');
}
method allowed-scopes() {
self.IMPL-WRAP-LIST(['my', 'state'])
}
}
# The declaration of a term (sigilless) variable.
class RakuAST::VarDeclaration::Term is RakuAST::Declaration {
has RakuAST::Type $.type;
has RakuAST::Name $.name;
has RakuAST::Initializer $.initializer;
method new(str :$scope, RakuAST::Type :$type, RakuAST::Name :$name!,
RakuAST::Initializer :$initializer!) {
my $obj := nqp::create(self);
nqp::bindattr_s($obj, RakuAST::Declaration, '$!scope', $scope);
nqp::bindattr($obj, RakuAST::VarDeclaration::Term, '$!type', $type // RakuAST::Type);
nqp::bindattr($obj, RakuAST::VarDeclaration::Term, '$!name', $name);
nqp::bindattr($obj, RakuAST::VarDeclaration::Term, '$!initializer',
$initializer // RakuAST::Initializer);
$obj
}
method lexical-name() {
$!name.canonicalize
}
method generate-lookup() {
my $lookup := RakuAST::Term::Name.new($!name);
$lookup.set-resolution(self);
$lookup
}
method IMPL-QAST-DECL(RakuAST::IMPL::QASTContext $context) {
QAST::Var.new( :decl('var'), :scope('lexical'), :name($!name.canonicalize) )
}
method IMPL-TO-QAST(RakuAST::IMPL::QASTContext $context) {
my $init-qast := $!initializer.IMPL-TO-QAST($context);
if $!type {
$init-qast := QAST::Op.new(
:op('p6bindassert'),
$init-qast,
$!type.IMPL-TO-QAST($context)
);
}
QAST::Op.new( :op('bind'), self.IMPL-LOOKUP-QAST($context), $init-qast )
}
method IMPL-LOOKUP-QAST(RakuAST::IMPL::QASTContext $context) {
QAST::Var.new( :name($!name.canonicalize), :scope('lexical') )
}
method default-scope() { 'my' }
method allowed-scopes() { self.IMPL-WRAP-LIST(['my']) }
method needs-sink-call() { False }
method visit-children(Code $visitor) {
$visitor($!type) if $!type;
$visitor($!name);
$visitor($!initializer) if $!initializer;
}
}
# The commonalities for implicitly declared variables.
class RakuAST::VarDeclaration::Implicit is RakuAST::Declaration {
has str $.name;
method new(str :$name!, str :$scope) {
my $obj := nqp::create(self);
nqp::bindattr_s($obj, RakuAST::VarDeclaration::Implicit, '$!name', $name);
nqp::bindattr_s($obj, RakuAST::Declaration, '$!scope', $scope);
$obj
}
method lexical-name() {
$!name
}
method default-scope() {
'my'
}
method generate-lookup() {
my $lookup := RakuAST::Var::Lexical.new($!name);
$lookup.set-resolution(self);
$lookup
}
method IMPL-TO-QAST(RakuAST::IMPL::QASTContext $context) {
self.IMPL-LOOKUP-QAST($context)
}
method IMPL-LOOKUP-QAST(RakuAST::IMPL::QASTContext $context, Mu :$rvalue) {
QAST::Var.new( :name($!name), :scope('lexical') )
}
}
# An implicitly declared special variable. Typically used for $/, $!, and $_ in
# routines.
class RakuAST::VarDeclaration::Implicit::Special is RakuAST::VarDeclaration::Implicit
is RakuAST::Meta {
method PRODUCE-META-OBJECT() {
# Reuse the container descriptor for the common cases that we expect
# to have.
my constant COMMON := nqp::hash(
'$_', ContainerDescriptor.new(:of(Mu), :default(Any), :!dynamic, :name('$_')),
'$/', ContainerDescriptor.new(:of(Mu), :default(Nil), :dynamic, :name('$/')),
'$!', ContainerDescriptor.new(:of(Mu), :default(Nil), :dynamic, :name('$!'))
);
my $cont-desc := COMMON{self.name} //
ContainerDescriptor.new(:of(Mu), :default(Any), :!dynamic, :name(self.name));
my $container := nqp::create(Scalar);
nqp::bindattr($container, Scalar, '$!descriptor', $cont-desc);
nqp::bindattr($container, Scalar, '$!value', Any);
$container
}
method IMPL-QAST-DECL(RakuAST::IMPL::QASTContext $context) {
my $container := self.meta-object;
$context.ensure-sc($container);
QAST::Var.new(
:scope('lexical'), :decl('contvar'), :name(self.name),
:value($container)
)
}
}
# Implicit block topic declaration. By default it is an optional parameter,
# but can be configured to be either a non-parameter (always from outer), a
# required parameter, or to obtain the current exception (for when it is a
# CATCH or CONTROL block);
class RakuAST::VarDeclaration::Implicit::BlockTopic is RakuAST::VarDeclaration::Implicit {
has Bool $.parameter;
has Bool $.required;
has Bool $.exception;
method new(Bool :$parameter, Bool :$required, Bool :$exception) {
my $obj := nqp::create(self);
nqp::bindattr_s($obj, RakuAST::VarDeclaration::Implicit, '$!name', '$_');
nqp::bindattr_s($obj, RakuAST::Declaration, '$!scope', 'my');
nqp::bindattr($obj, RakuAST::VarDeclaration::Implicit::BlockTopic, '$!parameter',
$parameter // True);
nqp::bindattr($obj, RakuAST::VarDeclaration::Implicit::BlockTopic, '$!required',
$required // False);
nqp::bindattr($obj, RakuAST::VarDeclaration::Implicit::BlockTopic, '$!exception',
$exception // False);
$obj
}
method set-parameter(Bool $parameter) {
nqp::bindattr(self, RakuAST::VarDeclaration::Implicit::BlockTopic, '$!parameter',
$parameter);
Nil
}
method IMPL-QAST-DECL(RakuAST::IMPL::QASTContext $context) {
if $!exception {
QAST::Stmts.new(
QAST::Var.new( :decl('param'), :scope('local'), :name('EXCEPTION') ),
QAST::Op.new(
:op('bind'),
QAST::Var.new( :decl('var'), :scope('lexical'), :name('$_') ),
QAST::Op.new(
:op('call'), :name('&EXCEPTION'),
QAST::Var.new( :scope('local'), :name('EXCEPTION') )
)
),
QAST::Op.new(
:op('p6assign'),
QAST::Op.new( :op('getlexouter'), QAST::SVal.new( :value('$!') ) ),
QAST::Var.new( :scope('lexical'), :name('$_') )
)
)
}
elsif $!parameter {
my $param := QAST::Var.new( :decl('param'), :scope('lexical'), :name('$_') );
unless $!required {
$param.default(QAST::Op.new( :op('getlexouter'), QAST::SVal.new( :value('$_') ) ));
}
$param
}
else {
QAST::Op.new:
:op('bind'),
QAST::Var.new( :decl('var'), :scope('lexical'), :name('$_') ),
QAST::Op.new( :op('getlexouter'), QAST::SVal.new( :value('$_') ) )
}
}
}
# An implicitly declared constant variable - that is, one with a value that is
# fixed at compile time. Used for $?PACKAGE and similar.
class RakuAST::VarDeclaration::Implicit::Constant is RakuAST::VarDeclaration::Implicit
is RakuAST::TraitTarget is RakuAST::BeginTime
is RakuAST::Meta is RakuAST::CompileTimeValue {
has Mu $.value;
method new(str :$name!, Mu :$value!, str :$scope) {
my $obj := nqp::create(self);
nqp::bindattr_s($obj, RakuAST::VarDeclaration::Implicit, '$!name', $name);
nqp::bindattr($obj, RakuAST::VarDeclaration::Implicit::Constant, '$!value', $value);
nqp::bindattr_s($obj, RakuAST::Declaration, '$!scope', $scope);
$obj
}
method compile-time-value() { $!value }
method IMPL-QAST-DECL(RakuAST::IMPL::QASTContext $context) {
$context.ensure-sc($!value);
QAST::Var.new( :decl('static'), :scope('lexical'), :name(self.name), :value($!value) )
}
method PRODUCE-META-OBJECT() {
self.compile-time-value
}
method PERFORM-BEGIN(RakuAST::Resolver $resolver, RakuAST::IMPL::QASTContext $context) {
self.apply-traits($resolver, $context, self, :SYMBOL(RakuAST::StrLiteral.new(self.name)));
}
}
# An implicitly declared block (like an auto-generated proto)
class RakuAST::VarDeclaration::Implicit::Block is RakuAST::Declaration {
has Mu $.block;
method new(Mu :$block!, str :$scope) {
my $obj := nqp::create(self);
nqp::bindattr($obj, RakuAST::VarDeclaration::Implicit::Block, '$!block', $block);
nqp::bindattr_s($obj, RakuAST::Declaration, '$!scope', $scope);
$obj
}
method IMPL-QAST-DECL(RakuAST::IMPL::QASTContext $context) {
$!block.IMPL-QAST-DECL-CODE($context);
}
method lexical-name() { '' }
}
# The implicit `self` term declaration for the invocant.
class RakuAST::VarDeclaration::Implicit::Self is RakuAST::VarDeclaration::Implicit {
method new() {
my $obj := nqp::create(self);
nqp::bindattr_s($obj, RakuAST::VarDeclaration::Implicit, '$!name', 'self');
nqp::bindattr_s($obj, RakuAST::Declaration, '$!scope', 'my');
$obj
}
method IMPL-QAST-DECL(RakuAST::IMPL::QASTContext $context) {
QAST::Var.new( :decl('var'), :scope('lexical'), :name(self.name) )
}
}
# The implicit `$¢` declaration for the cursor.
class RakuAST::VarDeclaration::Implicit::Cursor is RakuAST::VarDeclaration::Implicit {
method new() {
my $obj := nqp::create(self);
nqp::bindattr_s($obj, RakuAST::VarDeclaration::Implicit, '$!name', '$¢');
nqp::bindattr_s($obj, RakuAST::Declaration, '$!scope', 'my');
$obj
}