-
-
Notifications
You must be signed in to change notification settings - Fork 372
/
World.nqp
5465 lines (4936 loc) Β· 207 KB
/
World.nqp
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
use NQPHLL;
use QAST;
use Perl6::ModuleLoader;
use Perl6::Pod;
use Perl6::Ops;
# Binder constants.
# XXX Want constant syntax in NQP really.
my $SIG_ELEM_BIND_CAPTURE := 1;
my $SIG_ELEM_BIND_PRIVATE_ATTR := 2;
my $SIG_ELEM_BIND_PUBLIC_ATTR := 4;
my $SIG_ELEM_SLURPY_POS := 8;
my $SIG_ELEM_SLURPY_NAMED := 16;
my $SIG_ELEM_SLURPY_LOL := 32;
my $SIG_ELEM_INVOCANT := 64;
my $SIG_ELEM_MULTI_INVOCANT := 128;
my $SIG_ELEM_IS_RW := 256;
my $SIG_ELEM_IS_COPY := 512;
my $SIG_ELEM_IS_RAW := 1024;
my $SIG_ELEM_IS_OPTIONAL := 2048;
my $SIG_ELEM_ARRAY_SIGIL := 4096;
my $SIG_ELEM_HASH_SIGIL := 8192;
my $SIG_ELEM_DEFAULT_FROM_OUTER := 16384;
my $SIG_ELEM_IS_CAPTURE := 32768;
my $SIG_ELEM_UNDEFINED_ONLY := 65536;
my $SIG_ELEM_DEFINED_ONLY := 131072;
my $SIG_ELEM_NOMINAL_GENERIC := 524288;
my $SIG_ELEM_DEFAULT_IS_LITERAL := 1048576;
my $SIG_ELEM_NATIVE_INT_VALUE := 2097152;
my $SIG_ELEM_NATIVE_NUM_VALUE := 4194304;
my $SIG_ELEM_NATIVE_STR_VALUE := 8388608;
my $SIG_ELEM_SLURPY_ONEARG := 16777216;
my $SIG_ELEM_CODE_SIGIL := 33554432;
sub p6ize_recursive($x) {
if nqp::islist($x) {
my @copy := [];
for $x {
nqp::push(@copy, p6ize_recursive($_));
}
return nqp::hllizefor(@copy, 'perl6');
}
elsif nqp::ishash($x) {
my %copy := nqp::hash();
for $x {
%copy{$_.key} := p6ize_recursive($_.value);
}
return nqp::hllizefor(%copy, 'perl6').item;
}
nqp::hllizefor($x, 'perl6');
}
# Helper sub that turns a list of items into an NQPArray. This is needed when
# using e.g. $*W.find_symbol from Perl 6 code (example: slangs).
sub nqplist(*@arr) { @arr }
nqp::bindcurhllsym('nqplist', &nqplist);
# this levenshtein implementation is used to suggest good alternatives
# when deriving from an unknown/typo'd class.
sub levenshtein($a, $b) {
my %memo;
my $alen := nqp::chars($a);
my $blen := nqp::chars($b);
return 0 if $alen eq 0 || $blen eq 0;
# the longer of the two strings is an upper bound.
#my $bound := $alen < $blen ?? $blen !! $alen;
sub changecost($ac, $bc) {
sub issigil($_) { nqp::index('$@%&|', $_) != -1 };
return 0 if $ac eq $bc;
return 0.1 if nqp::fc($ac) eq nqp::fc($bc);
return 0.5 if issigil($ac) && issigil($bc);
return 1;
}
sub levenshtein_impl($apos, $bpos, $estimate) {
my $key := join(":", ($apos, $bpos));
return %memo{$key} if nqp::existskey(%memo, $key);
# if either cursor reached the end of the respective string,
# the result is the remaining length of the other string.
sub check($pos1, $len1, $pos2, $len2) {
if $pos2 == $len2 {
return $len1 - $pos1;
}
return -1;
}
my $check := check($apos, $alen, $bpos, $blen);
return $check unless $check == -1;
$check := check($bpos, $blen, $apos, $alen);
return $check unless $check == -1;
my $achar := nqp::substr($a, $apos, 1);
my $bchar := nqp::substr($b, $bpos, 1);
my $cost := changecost($achar, $bchar);
# hyphens and underscores cost half when adding/deleting.
my $addcost := 1;
$addcost := 0.5 if $bchar eq "-" || $bchar eq "_";
my $delcost := 1;
$delcost := 0.5 if $achar eq "-" || $achar eq "_";
my $ca := levenshtein_impl($apos+1, $bpos, $estimate+$delcost) + $delcost; # what if we remove the current letter from A?
my $cb := levenshtein_impl($apos, $bpos+1, $estimate+$addcost) + $addcost; # what if we add the current letter from B?
my $cc := levenshtein_impl($apos+1, $bpos+1, $estimate+$cost) + $cost; # what if we change/keep the current letter?
# the result is the shortest of the three sub-tasks
my $distance;
$distance := $ca if $ca <= $cb && $ca <= $cc;
$distance := $cb if $cb <= $ca && $cb <= $cc;
$distance := $cc if $cc <= $ca && $cc <= $cb;
# switching two letters costs only 1 instead of 2.
if $apos + 1 <= $alen && $bpos + 1 <= $blen &&
nqp::eqat($a, $bchar, $apos + 1) && nqp::eqat($b, $achar, $bpos + 1) {
my $cd := levenshtein_impl($apos+2, $bpos+2, $estimate+1) + 1;
$distance := $cd if $cd < $distance;
}
%memo{$key} := $distance;
return $distance;
}
my $result := levenshtein_impl(0, 0, 0);
return $result;
}
sub make_levenshtein_evaluator($orig_name, @candidates) {
my $Str-obj := $*W.find_symbol(["Str"], :setting-only);
my $find-count := 0;
my $try-count := 0;
sub inner($name, $hash) {
# difference in length is a good lower bound.
$try-count := $try-count + 1;
return 0 if $find-count > 20 || $try-count > 1000;
my $parlen := nqp::chars($orig_name);
my $lendiff := nqp::chars($name) - $parlen;
$lendiff := -$lendiff if $lendiff < 0;
return 1 if $lendiff >= $parlen * 0.3;
my $dist := levenshtein($orig_name, $name) / $parlen;
my $target := -1;
$target := @candidates[0] if $dist <= 0.1;
$target := @candidates[1] if 0.1 < $dist && $dist <= 0.2;
$target := @candidates[2] if 0.2 < $dist && $dist <= 0.35;
if $target != -1 {
my $name-str := nqp::box_s($name, $Str-obj);
nqp::push($target, $name-str);
$find-count := $find-count + 1;
}
1;
}
return &inner;
}
sub levenshtein_candidate_heuristic(@candidates, $target) {
# only take a few suggestions
my $to-add := 5;
for @candidates[0] {
$target.push($_) if $to-add > 0;
$to-add := $to-add - 1;
}
$to-add := $to-add - 1 if +@candidates[0] > 0;
for @candidates[1] {
$target.push($_) if $to-add > 0;
$to-add := $to-add - 1;
}
$to-add := $to-add - 2 if +@candidates[1] > 0;
for @candidates[2] {
$target.push($_) if $to-add > 0;
$to-add := $to-add - 1;
}
}
# This builds upon the HLL::World to add the specifics needed by Rakudo Perl 6.
class Perl6::World is HLL::World {
my class Perl6CompilationContext is HLL::World::CompilationContext {
# The stack of lexical pads, actually as QAST::Block objects. The
# outermost frame is at the bottom, the latest frame is on top.
has @!PADS;
# The stack of QAST::Blocks together with the ones that are not
# lexpads.
# The outermost block is at the bottom, the latest block is on top.
has @!PADS_AND_THUNKS;
# The stack of code objects; phasers get attached to the top one.
has @!CODES;
# Mapping of sub IDs to their code objects; used for fixing up in
# dynamic compilation.
has %!sub_id_to_code_object;
# Mapping of sub IDs to any code objects that were cloned during
# compilation before we had chance to compile the code. These are
# not true closures (in those cases the surrounding scope that it
# would close over is also compiled), but rather are clones for
# things like proto method derivation.
has %!sub_id_to_cloned_code_objects;
# Mapping of sub IDs to SC indexes of code stubs.
has %!sub_id_to_sc_idx;
# Array of stubs to check and the end of compilation.
has @!stub_check;
# Array of protos that can have their candidates pre-sorted at CHECK
# time.
has @!protos_to_sort;
# Cached constants that we've built.
has %!const_cache;
# Cached * and ** instances.
has $!the_whatever;
has $!the_hyper_whatever;
# List of CHECK blocks to run.
has @!CHECKs;
# Clean-up tasks, to do after CHECK time.
has @!cleanup_tasks;
# Cache of container info and descriptor for magicals.
has %!magical_cds;
method BUILD(:$handle, :$description) {
@!PADS := [];
@!PADS_AND_THUNKS := [];
@!CODES := [];
@!stub_check := [];
@!protos_to_sort := [];
@!CHECKs := [];
%!sub_id_to_code_object := {};
%!sub_id_to_cloned_code_objects := {};
%!sub_id_to_sc_idx := {};
%!const_cache := {};
@!cleanup_tasks := [];
%!magical_cds := {};
}
method blocks() {
@!PADS
}
method create_block($/) {
# Create pad, link to outer, annotate with creating statement.
my $pad := QAST::Block.new( QAST::Stmts.new( :node($/) ) );
if $*WANTEDOUTERBLOCK { # (outside of 1st push/pop pass)
$pad.annotate('outer', $*WANTEDOUTERBLOCK);
}
elsif +@!PADS {
$pad.annotate('outer', @!PADS[+@!PADS - 1]);
}
$pad.annotate('statement_id', $*STATEMENT_ID);
$pad.annotate('in_stmt_mod', $*IN_STMT_MOD);
$pad
}
# Creates a new lexical scope and puts it on top of the stack.
method push_lexpad($/) {
my $pad := self.create_block($/);
@!PADS[+@!PADS] := $pad;
@!PADS_AND_THUNKS[+@!PADS_AND_THUNKS] := $pad;
$pad;
}
# Pops a lexical scope off the stack.
method pop_lexpad() {
@!PADS_AND_THUNKS.pop();
@!PADS.pop();
}
# Gets the top lexpad.
method cur_lexpad() {
@!PADS[+@!PADS - 1]
}
# Creates a new thunk and puts it on top of the stack
method push_thunk($/) {
my $thunk := self.create_block($/);
@!PADS_AND_THUNKS[+@!PADS_AND_THUNKS] := $thunk;
$thunk;
}
# Pops a thunk off the stack
method pop_thunk() {
@!PADS_AND_THUNKS.pop();
}
# Gets the top block or thunk.
method cur_block_or_thunk() {
@!PADS_AND_THUNKS[+@!PADS_AND_THUNKS - 1]
}
# Marks the current lexpad as being a signatured block.
method mark_cur_lexpad_signatured() {
@!PADS[+@!PADS - 1].annotate('signatured', 1);
}
# Finds the nearest signatured block and checks if it declares
# a certain symbol.
method nearest_signatured_block_declares(str $symbol) {
my $i := +@!PADS;
while $i > 0 {
$i := $i - 1;
if @!PADS[$i].ann('signatured') {
return +@!PADS[$i].symbol($symbol);
}
}
}
# Marks all blocks upto and including one declaring a $*DISPATCHER as
# being no-inline.
method mark_no_inline_upto_dispatcher() {
my $i := +@!PADS_AND_THUNKS;
while $i > 0 {
$i := $i - 1;
my $block := @!PADS_AND_THUNKS[$i];
$block.no_inline(1);
last if $block.symbol('$*DISPATCHER');
}
}
# Hunts through scopes to find the type of a lexical.
method find_lexical_container_type(str $name) {
my int $i := +@!PADS;
while $i > 0 {
$i := $i - 1;
my %sym := @!PADS[$i].symbol($name);
if +%sym {
if nqp::existskey(%sym, 'type') {
return %sym<type>;
}
else {
$i := 0;
}
}
}
nqp::die("Could not find container descriptor for $name");
}
# Hunts through scopes to find a lexical and returns if it is
# known to be read-only.
method is_lexical_marked_ro(str $name) {
my int $i := +@!PADS;
while $i > 0 {
$i := $i - 1;
my %sym := @!PADS[$i].symbol($name);
if %sym {
return nqp::existskey(%sym, 'ro');
}
}
0;
}
# Checks if the given name is known anywhere in the lexpad
# and with lexical scope.
method is_lexical(str $name) {
my int $i := +@!PADS;
while $i > 0 {
$i := $i - 1;
my %sym := @!PADS[$i].symbol($name);
if +%sym {
return %sym<scope> eq 'lexical' &&
# Make sure it's now a lowered away lexical from a flattened
# inner scope in an EVAL in the REPL
($i > 0 || nqp::iscclass(nqp::const::CCLASS_ALPHABETIC, $name, 0) ||
!nqp::istype(
$*W.force_value(%sym, $name, 0),
$*W.find_symbol(['Rakudo', 'Internals', 'LoweredAwayLexical'])));
}
}
0;
}
# Checks if the symbol is really an alias to an attribute.
method is_attr_alias(str $name) {
my int $i := +@!PADS;
while $i > 0 {
$i := $i - 1;
my %sym := @!PADS[$i].symbol($name);
if +%sym {
return %sym<attr_alias>;
}
}
}
# Gets top code object in the code objects stack, or optionally the
# one the specified number of scopes down.
method get_code_object(int :$scopes = 0) {
$scopes < nqp::elems(@!CODES)
?? @!CODES[nqp::elems(@!CODES) - ($scopes + 1)]
!! NQPMu
}
method push_code_object($code) {
@!CODES[+@!CODES] := $code
}
method pop_code_object() {
@!CODES.pop()
}
method cur_code_object() {
@!CODES[+@!CODES - 1]
}
# Pushes a stub on the "stubs to check" list.
method add_stub_to_check($stub) {
nqp::push(@!stub_check, $stub);
}
method stub_check() {
@!stub_check
}
# Adds a proto to be sorted at CHECK time.
method add_proto_to_sort($proto) {
nqp::push(@!protos_to_sort, $proto);
}
method protos_to_sort() {
@!protos_to_sort
}
method magical_cds() {
%!magical_cds
}
method sub_id_to_code_object() {
%!sub_id_to_code_object
}
method sub_id_to_sc_idx() {
%!sub_id_to_sc_idx
}
method const_cache() {
%!const_cache
}
method add_cleanup_task($task) {
nqp::push(@!cleanup_tasks, $task)
}
method cleanup_tasks() {
@!cleanup_tasks
}
method add_clone_for_cuid($clone, $cuid) {
unless %!sub_id_to_cloned_code_objects{$cuid} {
%!sub_id_to_cloned_code_objects{$cuid} := [];
}
%!sub_id_to_cloned_code_objects{$cuid}.push($clone);
}
method sub_id_to_cloned_code_objects() {
%!sub_id_to_cloned_code_objects
}
method whatever() {
$!the_whatever
}
method set_whatever($whatever) {
$!the_whatever := $whatever
}
method hyper_whatever() {
$!the_hyper_whatever
}
method set_hyper_whatever($hyper_whatever) {
$!the_hyper_whatever := $hyper_whatever
}
method add_check($check) {
@!CHECKs := [] unless @!CHECKs;
@!CHECKs.unshift($check);
}
method checks() {
@!CHECKs
}
}
method context_class() {
Perl6CompilationContext
}
# Mapping of QAST::Stmts node containing fixups, keyed by sub ID. If
# we do dynamic compilation then we do the fixups immediately and
# then clear this list.
# Doesn't need to be shared - is used for BEGIN blocks
has %!code_object_fixup_list;
# Cached compiler services object, if any.
has $!compiler_services;
# are we module debugging?
has $!RAKUDO_MODULE_DEBUG;
has $!record_precompilation_dependencies;
has %!quote_lang_cache;
method BUILD(*%adv) {
%!code_object_fixup_list := {};
$!record_precompilation_dependencies := 1;
%!quote_lang_cache := {};
}
method create_nested() {
Perl6::World.new(:handle(self.handle), :context(self.context()))
}
method lang-ver-before(str $want) {
nqp::chars($want) == 1 || nqp::die(
'Version to $*W.lang_ver_before'
~ " must be 1 char long ('c', 'd', etc). Got `$want`.");
nqp::cmp_s(
nqp::substr(nqp::getcomp('perl6').language_version, 2, 1),
$want
) == -1
}
method !check-version-modifier($ver-match, $rev, $modifier, $comp) {
my %lang_rev := $comp.language_revisions;
unless nqp::existskey(%lang_rev, $rev) &&
(!$modifier || nqp::existskey(%lang_rev{$rev}<mods>, $modifier)) {
$ver-match.typed_panic: 'X::Language::Unsupported', version => ~$ver-match;
}
# See if requested revision is not supported without a modifier. Most likely it'll be PREVIEW modifier for
# unreleased revisions.
if nqp::existskey(%lang_rev{$rev}, 'require') {
if nqp::iseq_s(%lang_rev{$rev}<require>, $modifier) {
return;
}
$ver-match.typed_panic: 'X::Language::ModRequired',
version => ~$ver-match,
modifier => %lang_rev{$rev}<require>;
}
if %lang_rev{$rev}<mods>{$modifier}<deprecate> {
$ver-match.PRECURSOR.worry("$modifier modifier is deprecated for Perl 6.$rev");
}
}
# NOTE: Revision .c has special meaning because it doesn't have own dedicated CORE setting and serves as the base
# for all other revisions.
method load-lang-ver($ver-match, $comp) {
$*MAIN := 'MAIN';
$*STRICT := 1 if $*begin_compunit;
my str $version := ~$ver-match;
my @vparts := nqp::split('.', $version);
my $vWhatever := nqp::isge_i(nqp::index($version, '*'), 0);
my $vPlus := nqp::isge_i(nqp::index($version, '+'), 0);
my $default_rev := nqp::substr($comp.config<language-version>, 2, 1);
# Do we have dot-splitted version string?
if !($vWhatever || $vPlus) &&
( ((@vparts > 1) && nqp::iseq_s(@vparts[0], 'v6'))
|| ($version eq 'v6') ) {
my $revision := @vparts[1] || $default_rev;
my $lang_ver := '6.' ~ $revision;
self."!check-version-modifier"($ver-match, $revision, @vparts[2] || '', $comp);
$comp.set_language_version: $lang_ver;
# fast-path the common cases
if $revision eq 'c' {
$*CAN_LOWER_TOPIC := 0;
# CORE.c is currently our lowest core, which we don't "load"
return;
}
# Speed up loading assuming that the default language version would be the most used one.
if $lang_ver eq $comp.config<language-version> {
self.load_setting: $ver-match, 'CORE.' ~ $revision;
return;
}
}
my $Version := self.find_symbol: ['Version'];
my $vWant := $ver-match.ast.compile_time_value;
my $vWantParts := $vWant.parts;
my %lang_rev := $comp.language_revisions;
unless $vWhatever || $vWant.plus {
# It makes no sense checking for modifier when something like v6.* or v6.c+ is wanted.
my $rev := $vWantParts.AT-POS(1);
my str $rev_mod := $vWantParts.elems > 2 ?? $vWantParts.AT-POS(2) !! '';
self."!check-version-modifier"($ver-match, $rev, $rev_mod, $comp);
}
my @can_ver_reversed;
for $comp.can_language_versions { nqp::unshift(@can_ver_reversed, $_) }
for @can_ver_reversed -> $can-ver {
# Skip if tried version doesn't match the wanted one
next unless $vWant.ACCEPTS: my $vCan := $Version.new: $can-ver;
my $vCanElems := $vCan.parts.elems;
my $can_rev := $vCan.parts.AT-POS: 1;
# Skip if 2-part version tried now has a required modifier
next if nqp::iseq_i($vCanElems, 2) && nqp::existskey(%lang_rev{$can_rev}, 'require');
$comp.set_language_version: '6.' ~ $can_rev;
$comp.set_language_modifier: $vCan.parts.AT-POS: 2 if $vCanElems > 2;
if $can_rev eq 'c' {
$*CAN_LOWER_TOPIC := 0;
# CORE.c is our lowest core, which we don't "load"
}
else {
self.load_setting: $ver-match, 'CORE.' ~ $can_rev
}
return;
}
$ver-match.typed_panic: 'X::Language::Unsupported', :$version;
}
method RAKUDO_MODULE_DEBUG() {
if nqp::isconcrete($!RAKUDO_MODULE_DEBUG) {
$!RAKUDO_MODULE_DEBUG
}
elsif !$*COMPILING_CORE_SETTING {
$!RAKUDO_MODULE_DEBUG :=
self.find_symbol(['&DYNAMIC'])('$*RAKUDO_MODULE_DEBUG')
}
else {
$!RAKUDO_MODULE_DEBUG := False
}
}
method loading_and_symbol_setup($/) {
my $setting_name;
# Create unit outer (where we assemble any lexicals accumulated
# from e.g. REPL) and the real UNIT.
$*UNIT_OUTER := self.push_lexpad($/);
$*UNIT := self.push_lexpad($/);
my $in_eval := 0;
# If we already have a specified outer context, then that's
# our setting. Otherwise, load one.
my $have_outer := nqp::defined(%*COMPILING<%?OPTIONS><outer_ctx>);
if $have_outer {
$setting_name := '';
$*UNIT.annotate('IN_DECL', 'eval');
$in_eval := 1;
}
else {
$setting_name := %*COMPILING<%?OPTIONS><setting> // 'CORE';
$*COMPILING_CORE_SETTING := 1 if $setting_name eq 'NULL';
$*SET_DEFAULT_LANG_VER := 0
if nqp::eqat($setting_name, 'NULL', 0);
self.load_setting($/,$setting_name);
$*UNIT.annotate('IN_DECL', 'mainline');
}
$/.unitstart();
try {
my $EXPORTHOW := self.find_symbol(['EXPORTHOW']);
for self.stash_hash($EXPORTHOW) {
$*LANG.set_how($_.key, $_.value);
}
}
# Create GLOBAL(ish), unless we were given one.
if nqp::existskey(%*COMPILING<%?OPTIONS>, 'global') {
$*GLOBALish := %*COMPILING<%?OPTIONS><global>;
}
elsif $have_outer && $*UNIT_OUTER.symbol('GLOBALish') {
$*GLOBALish :=
self.force_value($*UNIT_OUTER.symbol('GLOBALish'),'GLOBALish',1);
}
else {
$*GLOBALish :=
self.pkg_create_mo($/,$/.how('package'),:name('GLOBAL'));
self.pkg_compose($/, $*GLOBALish);
}
# Create or pull in existing EXPORT.
if $have_outer && $*UNIT_OUTER.symbol('EXPORT') {
$*EXPORT :=
self.force_value($*UNIT_OUTER.symbol('EXPORT'), 'EXPORT', 1);
}
else {
$*EXPORT := self.pkg_create_mo($/, $/.how('package'), :name('EXPORT'));
self.pkg_compose($/, $*EXPORT);
}
# If there's a self in scope, set $*HAS_SELF.
if $have_outer && $*UNIT_OUTER.symbol('self') {
$*HAS_SELF := 'complete';
}
# Take current package from outer context if any, otherwise for a
# fresh compilation unit we start in GLOBAL.
my $package;
if $have_outer && $*UNIT_OUTER.symbol('$?PACKAGE') {
$package :=
self.force_value($*UNIT_OUTER.symbol('$?PACKAGE'),'$?PACKAGE',1);
}
else {
$package := $*GLOBALish;
}
$*PACKAGE := $package;
$/.set_package($package);
# If we're eval'ing in the context of a %?LANG, set up our own
# %*LANG based on it.
if $have_outer && $*UNIT_OUTER.symbol('%?LANG') {
for self.force_value(
$*UNIT_OUTER.symbol('%?LANG'), '%?LANG', 1).FLATTENABLE_HASH() {
%*LANG{$_.key} := $_.value;
}
}
if $have_outer && $*UNIT_OUTER.symbol('$*MAIN') {
$*MAIN :=
self.force_value($*UNIT_OUTER.symbol('$*MAIN'), '$*MAIN', 1);
}
if $have_outer && $*UNIT_OUTER.symbol('$?STRICT') {
$*STRICT :=
self.force_value($*UNIT_OUTER.symbol('$*STRICT'), '$*STRICT', 1);
}
else {
$*STRICT := 1;
}
# Bootstrap
if $setting_name eq 'NULL' {
my $name := "Perl6::BOOTSTRAP";
my $module := self.load_module_early($/, $name, $*GLOBALish);
my $EXPORT := $module<EXPORT>.WHO;
my @to_import := ['MANDATORY', 'DEFAULT'];
for @to_import -> $tag {
if nqp::existskey($EXPORT, $tag) {
self.import($/, self.stash_hash($EXPORT{$tag}), $name);
}
}
for $module<EXPORTHOW>.WHO {
my str $key := $_.key;
$*LANG.set_how($key, nqp::decont($_.value));
}
}
# Install as we've no setting, in which case we've likely no
# static lexpad class yet either. Also, UNIT needs a code object.
else {
self.install_lexical_symbol($*UNIT, 'GLOBALish', $*GLOBALish);
self.install_lexical_symbol($*UNIT, 'EXPORT', $*EXPORT);
self.install_lexical_symbol($*UNIT, '$?PACKAGE', $package);
self.install_lexical_symbol($*UNIT, '::?PACKAGE', $package);
$*CODE_OBJECT := $*DECLARAND := self.stub_code_object('Block');
unless $in_eval {
self.install_lexical_symbol(
$*UNIT,'$=finish',self.find_symbol(['Mu'], :setting-only));
}
}
unless $in_eval {
my $M := %*COMPILING<%?OPTIONS><M>;
if nqp::defined($M) {
for nqp::islist($M) ?? $M !! [$M] -> $longname {
self.do_pragma_or_load_module($/,1,$longname);
}
}
}
}
method mop_up_and_check($/) {
# Install POD-related variables.
$*POD_PAST := self.add_constant(
'Array', 'type_new', :nocache, |$*POD_BLOCKS
);
self.install_lexical_symbol(
$*UNIT, '$=pod', $*POD_PAST.compile_time_value
);
# Tag UNIT with a magical lexical. Also if we're compiling CORE,
# give it such a tag too.
my $name := $*COMPILING_CORE_SETTING
?? '!CORE_MARKER'
!! '!UNIT_MARKER';
my $marker := self.pkg_create_mo($/, $/.how('package'), :$name);
$marker.HOW.compose($marker);
self.install_lexical_symbol($*UNIT, $name, $marker);
# CHECK time.
self.CHECK();
# Clean up compiler services.
if $!compiler_services {
my $cs := $!compiler_services;
nqp::bindattr($cs, $cs.WHAT, '$!compiler', nqp::null());
nqp::bindattr($cs, $cs.WHAT, '$!current-match', nqp::null());
}
}
# Creates a new lexical scope and puts it on top of the stack.
method push_lexpad($/) {
self.context().push_lexpad($/)
}
# Pops a lexical scope off the stack.
method pop_lexpad() {
self.context().pop_lexpad()
}
# Gets the top lexpad.
method cur_lexpad() {
self.context().cur_lexpad()
}
# Creates a new thunk and puts it on top of the stack
method push_thunk($/) {
self.context().push_thunk($/)
}
# Pops a thunk off the stack.
method pop_thunk() {
self.context().pop_thunk()
}
# Push inner block
method push_inner_block($block) {
self.context().cur_block_or_thunk()[0].push($block);
}
# Marks the current lexpad as being a signatured block.
method mark_cur_lexpad_signatured() {
self.context().mark_cur_lexpad_signatured()
}
# Finds the nearest signatured block and checks if it declares
# a certain symbol.
method nearest_signatured_block_declares(str $symbol) {
self.context().nearest_signatured_block_declares($symbol)
}
# Marks all blocks upto and including one declaring a $*DISPATCHER as
# being no-inline.
method mark_no_inline_upto_dispatcher() {
self.context().mark_no_inline_upto_dispatcher()
}
# Gets top code object in the code objects stack, or optionally the
# one the specified number of scopes down.
method get_code_object(int :$scopes = 0) {
self.context().get_code_object(:$scopes)
}
# Pushes a stub on the "stubs to check" list.
method add_stub_to_check($stub) {
self.context().add_stub_to_check($stub)
}
# Adds a proto to be sorted at CHECK time.
method add_proto_to_sort($proto) {
self.context().add_proto_to_sort($proto)
}
# Checks for any stubs that weren't completed.
method assert_stubs_defined($/) {
my @incomplete;
for self.context().stub_check {
unless $_.HOW.is_composed($_) {
@incomplete.push($_.HOW.name($_));
}
}
if +@incomplete {
self.throw($/, 'X::Package::Stubbed', packages => @incomplete);
}
}
# Sorts all protos.
method sort_protos() {
for self.context().protos_to_sort() {
if nqp::can($_, 'sort_dispatchees') {
$_.sort_dispatchees();
}
}
}
# Loads a setting.
method load_setting($/, $setting_name) {
# Do nothing for the NULL setting.
if $setting_name ne 'NULL' {
# XXX TODO: see https://github.com/rakudo/rakudo/issues/2432
$setting_name := Perl6::ModuleLoader.transform_setting_name($setting_name);
# Load it immediately, so the compile time info is available.
# Once it's loaded, set it as the outer context of the code
# being compiled.
my $setting := %*COMPILING<%?OPTIONS><outer_ctx>
:= Perl6::ModuleLoader.load_setting($setting_name);
# Add a fixup and deserialization task also.
my $fixup := QAST::Stmt.new(
self.perl6_module_loader_code(),
QAST::Op.new(
:op('forceouterctx'),
QAST::BVal.new( :value($*UNIT_OUTER) ),
QAST::Op.new(
:op('callmethod'), :name('load_setting'),
QAST::Op.new(
:op('getcurhllsym'),
QAST::SVal.new( :value('ModuleLoader') )
),
QAST::SVal.new( :value($setting_name) )
)
)
);
self.add_load_dependency_task(:deserialize_ast($fixup), :fixup_ast($fixup));
return nqp::ctxlexpad($setting);
}
}
method import_EXPORTHOW($/, $handle) {
my $EXPORTHOW := $handle.export-how-package;
if nqp::defined($EXPORTHOW) {
$EXPORTHOW.pairs.map(-> $pair {
my str $key := $pair.key;
if $key eq 'SUPERSEDE' {
my %SUPERSEDE := self.stash_hash($pair.value);
for %SUPERSEDE {
my str $pdecl := $_.key;
my $meta := nqp::decont($_.value);
unless $/.know_how($pdecl) {
$/.typed_panic('X::EXPORTHOW::NothingToSupersede',
declarator => $pdecl);
}
if $/.know_how("U:$pdecl") {
$/.typed_panic('X::EXPORTHOW::Conflict',
declarator => $pdecl, directive => $key);
}
$*LANG.set_how($pdecl, $meta);
$*LANG.set_how("U:$pdecl", nqp::hash('SUPERSEDE', $meta));
}
}
elsif $key eq 'DECLARE' {
my %DECLARE := self.stash_hash($pair.value);
for %DECLARE {
my str $pdecl := $_.key;
my $meta := nqp::decont($_.value);
if $/.know_how($pdecl) {
$/.typed_panic('X::EXPORTHOW::Conflict',
declarator => $pdecl, directive => $key);
}
$*LANG.set_how($pdecl, $meta);
$*LANG.set_how("U:$pdecl", nqp::hash('DECLARE', $meta));
self.add_package_declarator($/,$pdecl);
}
}
elsif $key eq 'COMPOSE' {
my %COMPOSE := self.stash_hash($pair.value);
$/.NYI('EXPORTHOW::COMPOSE');
}
else {
if $key eq nqp::lc($key) {
# Support legacy API, which behaves like an unchecked
# supersede.
# XXX Can give deprecation warning in the future, remove
# before 6.0.0.
$*LANG.set_how($key, nqp::decont($pair.value));
}
else {
$/.typed_panic('X::EXPORTHOW::InvalidDirective', directive => $key);
}
}
}).eager;
}
}
method add_package_declarator($/, str $pdecl) {
my $cursor := $/;
# Compute name of grammar/action entry.
my $canname := 'package_declarator:sym<' ~ $pdecl ~ '>';
# Add to grammar if needed.
unless nqp::can($cursor, $canname) {
my role PackageDeclarator[$meth_name, $declarator] {
token ::($meth_name) {