-
-
Notifications
You must be signed in to change notification settings - Fork 372
/
SymbolTable.pm
787 lines (704 loc) Β· 30.3 KB
/
SymbolTable.pm
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
use NQPHLL;
use Perl6::ModuleLoader;
# 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_BLOCK := 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_PARCEL := 1024;
my $SIG_ELEM_IS_OPTIONAL := 2048;
my $SIG_ELEM_ARRAY_SIGIL := 4096;
my $SIG_ELEM_HASH_SIGIL := 8192;
# This builds upon the SerializationContextBuilder to add the specifics
# needed by Rakudo Perl 6.
class Perl6::SymbolTable is HLL::Compiler::SerializationContextBuilder {
# The stack of lexical pads, actually as PAST::Block objects. The
# outermost frame is at the bottom, the latest frame is on top.
has @!BLOCKS;
# Array of stubs to check and the end of compilation.
has @!stub_check;
# Creates a new lexical scope and puts it on top of the stack.
method push_lexpad($/) {
my $pad := PAST::Block.new( PAST::Stmts.new(), :node($/) );
@!BLOCKS[+@!BLOCKS] := $pad;
$pad
}
# Pops a lexical scope off the stack.
method pop_lexpad() {
@!BLOCKS.pop()
}
# Gets the top lexpad.
method cur_lexpad() {
@!BLOCKS[+@!BLOCKS - 1]
}
# Pushes a stub on the "stubs to check" list.
method add_stub_to_check($stub) {
@!stub_check[+@!stub_check] := $stub;
}
# Checks for any stubs that weren't completed.
method assert_stubs_defined() {
my @incomplete;
for @!stub_check {
unless $_.HOW.is_composed($_) {
@incomplete.push($_.HOW.name($_));
}
}
if +@incomplete {
pir::die("The following packages were stubbed but not defined:\n " ~
pir::join("\n ", @incomplete) ~ "\n");
}
}
# Loads a setting.
method load_setting($setting_name) {
# Do nothing for the NULL setting.
if $setting_name ne 'NULL' {
# 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);
# Do load in code.
my $fixup := PAST::Stmts.new(
PAST::Op.new(
:pirop('load_bytecode vs'), 'blib/Perl6/ModuleLoader.pbc'
),
PAST::Op.new(
:pasttype('callmethod'), :name('set_outer_ctx'),
PAST::Val.new( :value($*UNIT_OUTER) ),
PAST::Op.new(
:pasttype('callmethod'), :name('load_setting'),
PAST::Var.new( :name('ModuleLoader'), :namespace([]), :scope('package') ),
$setting_name
)
)
);
self.add_event(:deserialize_past($fixup), :fixup_past($fixup));
return pir::getattribute__PPs($setting, 'lex_pad');
}
}
# Loads a module immediately, and also makes sure we load it
# during the deserialization.
method load_module($module_name, $cur_GLOBALish) {
# Immediate loading.
my $module := Perl6::ModuleLoader.load_module($module_name, $cur_GLOBALish);
# Make sure we do the loading during deserialization.
self.add_event(:deserialize_past(PAST::Stmts.new(
PAST::Op.new(
:pirop('load_bytecode vs'), 'blib/Perl6/ModuleLoader.pbc'
),
PAST::Op.new(
:pasttype('callmethod'), :name('load_module'),
PAST::Var.new( :name('ModuleLoader'), :namespace([]), :scope('package') ),
$module_name,
self.get_slot_past_for_object($cur_GLOBALish)
))));
return pir::getattribute__PPs($module, 'lex_pad');
}
# Implements a basic first cut of import. Works out what needs to
# happen and builds code to do it at fixup/deserialization; also
# does it so that things are available at compile time. Note that
# import is done into the current lexpad.
method import($package) {
my %stash := $package.WHO;
my $target := self.cur_lexpad();
my $fixups := PAST::Stmts.new();
for %stash {
# Install the imported symbol directly as a block symbol.
$target.symbol($_.key, :scope('lexical'), :value($_.value));
$target[0].push(PAST::Var.new( :scope('lexical'), :name($_.key), :isdecl(1) ));
# Add fixup/deserialize event to stick it in the static lexpad.
$fixups.push(PAST::Op.new(
:pasttype('callmethod'), :name('set_static_lexpad_value'),
PAST::Op.new(
:pasttype('callmethod'), :name('get_lexinfo'),
PAST::Val.new( :value($target) )
),
$_.key,
PAST::Var.new(
:scope('keyed'),
PAST::Op.new(
:pirop('get_who PP'),
self.get_object_sc_ref_past($package)
),
$_.key
),
0
));
}
self.add_event(:deserialize_past($fixups), :fixup_past($fixups));
}
# Installs a lexical symbol. Takes a PAST::Block object, name and
# the object to install. Does an immediate installation in the
# compile-time block symbol table, and ensures that the installation
# gets fixed up at runtime too.
method install_lexical_symbol($block, $name, $obj) {
# Install the object directly as a block symbol.
$block.symbol($name, :scope('lexical'), :value($obj));
$block[0].push(PAST::Var.new( :scope('lexical'), :name($name), :isdecl(1) ));
# Fixup and deserialization task is the same.
my $fixup := PAST::Op.new(
:pasttype('callmethod'), :name('set_static_lexpad_value'),
PAST::Op.new(
:pasttype('callmethod'), :name('get_lexinfo'),
PAST::Val.new( :value($block) )
),
~$name, self.get_object_sc_ref_past($obj),
0
);
self.add_event(:deserialize_past($fixup), :fixup_past($fixup));
}
# Installs a lexical symbol. Takes a PAST::Block object, name and
# the type of container to install.
method install_lexical_container($block, $name, $type_name, $descriptor) {
# Add to block. Note that it doesn't really have a compile time
# value.
$block.symbol($name, :scope('lexical'), :descriptor($descriptor));
$block[0].push(PAST::Var.new( :scope('lexical'), :name($name), :isdecl(1) ));
# Look up container type and create code to instantiate it.
# XXX Set default value and container descriptor.
my $type_obj := self.find_symbol([$type_name]);
my $cont_code := PAST::Op.new(
:pirop('repr_instance_of PP'),
self.get_object_sc_ref_past($type_obj)
);
# Fixup and deserialization task is the same - creating the
# container type and put it in the static lexpad with a clone
# flag set.
my $fixup := PAST::Op.new(
:pasttype('callmethod'), :name('set_static_lexpad_value'),
PAST::Op.new(
:pasttype('callmethod'), :name('get_lexinfo'),
PAST::Val.new( :value($block) )
),
~$name, $cont_code, 1
);
self.add_event(:deserialize_past($fixup), :fixup_past($fixup));
}
# Installs a symbol into the package. Does so immediately, and
# makes sure this happens on deserialization also.
method install_package_symbol($package, $symbol, $obj) {
my @sym := pir::split('::', $symbol);
my $name := ~@sym.pop();
# Install symbol immediately.
my $target := $package;
for @sym {
# XXX Fix...
pir::die("Cannot handle multi-part names yet!");
#$target := pir::perl6_get_package_through_who__PPs($target, $_);
}
($target.WHO){$name} := $obj;
# Add deserialization installation of the symbol.
my $path := self.get_slot_past_for_object($package);
for @sym {
$path := PAST::Op.new(:pirop('perl6_get_package_through_who PPs'), $path, ~$_);
}
self.add_event(:deserialize_past(PAST::Op.new(
:pasttype('bind'),
PAST::Var.new(
:scope('keyed'),
PAST::Op.new( :pirop('get_who PP'), $path ),
$name
),
self.get_slot_past_for_object($obj)
)));
}
# Creates a parameter object.
method create_parameter(%param_info) {
# Create parameter object now.
my $par_type := self.find_symbol(['Parameter']);
my $parameter := pir::repr_instance_of__PP($par_type);
my $slot := self.add_object($parameter);
# Create PAST to make it when deserializing.
my $set_attrs := PAST::Stmts.new();
self.add_event(:deserialize_past(PAST::Stmts.new(
self.set_slot_past($slot, self.set_cur_sc(PAST::Op.new(
:pirop('repr_instance_of PP'),
self.get_object_sc_ref_past($par_type)
))),
$set_attrs
)));
# Set name if there is one.
if pir::exists(%param_info, 'variable_name') {
pir::repr_bind_attr_str__vPPsS($parameter, $par_type, '$!variable_name', %param_info<variable_name>);
$set_attrs.push(self.set_attribute_typed($parameter, $par_type,
'$!variable_name', %param_info<variable_name>, str));
}
# Set nominal type.
pir::setattribute__vPPsP($parameter, $par_type, '$!nominal_type', %param_info<nominal_type>);
$set_attrs.push(self.set_attribute($parameter, $par_type, '$!nominal_type',
self.get_object_sc_ref_past(%param_info<nominal_type>)));
# Calculate and set flags.
# XXX Many more to do.
my $flags := 0;
if %param_info<is_invocant> {
$flags := $flags + $SIG_ELEM_INVOCANT;
}
if %param_info<is_multi_invocant> {
$flags := $flags + $SIG_ELEM_MULTI_INVOCANT;
}
if %param_info<is_parcel> {
$flags := $flags + $SIG_ELEM_IS_PARCEL;
}
pir::repr_bind_attr_int__vPPsI($parameter, $par_type, '$!flags', $flags);
$set_attrs.push(self.set_attribute_typed($parameter, $par_type,
'$!flags', $flags, int));
# XXX Set up other various attribute values.
# Set container descriptor, if there is one.
if pir::exists(%param_info, 'container_descriptor') {
pir::setattribute__vPPsP($parameter, $par_type, '$!container_descriptor', %param_info<container_descriptor>);
$set_attrs.push(self.set_attribute($parameter, $par_type, '$!container_descriptor',
self.get_object_sc_ref_past(%param_info<container_descriptor>)));
}
# Return created parameter.
$parameter
}
# Creates a signature object from a set of parameters.
method create_signature(@parameters) {
# Create signature object now.
my $sig_type := self.find_symbol(['Signature']);
my $signature := pir::repr_instance_of__PP($sig_type);
my $slot := self.add_object($signature);
# Set parameters.
pir::setattribute__vPPsP($signature, $sig_type, '$!params', @parameters);
# Create PAST to make it when deserializing.
my $param_past := PAST::Op.new( :pasttype('list') );
for @parameters {
$param_past.push(self.get_slot_past_for_object($_));
}
self.add_event(:deserialize_past(PAST::Stmts.new(
self.set_slot_past($slot, self.set_cur_sc(PAST::Op.new(
:pirop('repr_instance_of PP'),
self.get_object_sc_ref_past($sig_type)
))),
self.set_attribute($signature, $sig_type, '$!params', $param_past)
)));
# Return created signature.
$signature
}
# Creates a code object and ensures that it gets fixed up with the compiled
# body at fixup time; during the deserialize we just set the already compiled
# output right into place. If we get a request to run the code before we did
# really compiling it, we can do that - we just dynamically compile it.
method create_code_object($code_past, $type, $signature, $is_dispatcher = 0) {
my $fixups := PAST::Stmts.new();
my $des := PAST::Stmts.new();
# Create code object now.
my $type_obj := self.find_symbol([$type]);
my $code := pir::repr_instance_of__PP($type_obj);
my $slot := self.add_object($code);
# For now, install stub that will dynamically compile the code if we ever try
# to run it during compilation.
my $precomp;
my $stub := sub (*@pos, *%named) {
unless $precomp {
$precomp := self.compile_in_context($code_past);
}
$precomp(|@pos, |%named);
};
my $code_type := self.find_symbol(['Code']);
pir::setattribute__vPPsP($code, $code_type, '$!do', $stub);
# Fixup will install the real thing.
$fixups.push(self.set_attribute($code, $code_type, '$!do', PAST::Val.new( :value($code_past) )));
# Desserialization should do the actual creation and just put the right
# code in there in the first place.
$des.push(self.set_slot_past($slot, self.set_cur_sc(PAST::Op.new(
:pirop('repr_instance_of PP'),
self.get_object_sc_ref_past($type_obj)
))));
$des.push(self.set_attribute($code, $code_type, '$!do', PAST::Val.new( :value($code_past) )));
# Install signauture now and add to deserialization.
pir::setattribute__vPPsP($code, $code_type, '$!signature', $signature);
$des.push(self.set_attribute($code, $code_type, '$!signature', self.get_object_sc_ref_past($signature)));
# If this is a dispatcher, install dispatchee list that we can
# add the candidates too.
if $is_dispatcher {
pir::setattribute__vPPsP($code, $code_type, '$!dispatchees', []);
$des.push(self.set_attribute($code, $code_type, '$!dispatchees',
PAST::Op.new( :pasttype('list') )));
}
self.add_event(:deserialize_past($des), :fixup_past($fixups));
$code;
}
# Adds a multi candidate to a proto/dispatch.
method add_dispatchee_to_proto($proto, $candidate) {
# Add it to the list.
my $code_type := self.find_symbol(['Code']);
pir::getattribute__PPPs($proto, $code_type, '$!dispatchees').push($candidate);
# Deserializatin code to add it.
self.add_event(:deserialize_past(PAST::Op.new(
:pirop('push vPP'),
PAST::Var.new(
:scope('attribute_6model'), :name('$!dispatchees'),
self.get_object_sc_ref_past($proto),
self.get_object_sc_ref_past($code_type)
),
self.get_object_sc_ref_past($candidate)
)));
}
# Creates a new container descriptor and adds it to the SC.
method create_container_descriptor($of, $rw, $name) {
# Create descriptor object now.
my $cd_type := self.find_symbol(['ContainerDescriptor']);
my $cd := pir::repr_instance_of__PP($cd_type);
my $slot := self.add_object($cd);
# Create PAST to make it when deserializing.
my $set_attrs := PAST::Stmts.new();
self.add_event(:deserialize_past(PAST::Stmts.new(
self.set_slot_past($slot, self.set_cur_sc(PAST::Op.new(
:pirop('repr_instance_of PP'),
self.get_object_sc_ref_past($cd_type)
))),
$set_attrs
)));
# Set attributes.
pir::setattribute__vPPsP($cd, $cd_type, '$!of', $of);
$set_attrs.push(self.set_attribute($cd, $cd_type, '$!of',
self.get_object_sc_ref_past($of)));
pir::repr_bind_attr_int__vPPsI($cd, $cd_type, '$!rw', $rw);
$set_attrs.push(self.set_attribute_typed($cd, $cd_type, '$!rw', $rw, int));
pir::repr_bind_attr_str__vPPsS($cd, $cd_type, '$!name', $name);
$set_attrs.push(self.set_attribute_typed($cd, $cd_type, '$!name', $name, str));
$cd
}
# Helper to make PAST for setting an attribute to a value. Value should
# be a PAST tree.
method set_attribute($obj, $class, $name, $value_past) {
PAST::Op.new(
:pasttype('bind'),
PAST::Var.new(
:name($name), :scope('attribute_6model'),
self.get_object_sc_ref_past($obj),
self.get_object_sc_ref_past($class)
),
$value_past
)
}
# Helper to make PAST for setting a typed attribute to a value. Value should
# be a PAST tree.
method set_attribute_typed($obj, $class, $name, $value_past, $type) {
PAST::Op.new(
:pasttype('bind'),
PAST::Var.new(
:name($name), :scope('attribute_6model'), :type($type),
self.get_object_sc_ref_past($obj),
self.get_object_sc_ref_past($class)
),
$value_past
)
}
# Takes a PAST::Block and compiles it for running during "compile time".
# We need to do this for BEGIN but also for things that get called in
# the compilation process, like user defined traits.
method compile_in_context($past) {
# Ensure that we have the appropriate op libs loaded and correct
# HLL.
$past.loadlibs('perl6_group', 'perl6_ops');
$past.hll('perl6');
# Create outer lexical contexts with all symbols visible.
# XXX TODO
# Compile and return.
my $pir := PAST::Compiler.compile($past, :target('pir'));
PAST::Compiler.compile($past)
}
# Adds a constant value to the constants table. Returns PAST to do
# the lookup of the constant.
method add_constant($type, $primitive, $value) {
# Find type object for the box typed we'll create.
# On deserialization, we'll need to look it up too.
my $type_obj := self.find_symbol([$type]);
my $type_obj_lookup := self.get_object_sc_ref_past($type_obj);
# Go by the primitive type we're boxing. Need to create
# the boxed value and also code to produce it.
my $constant;
my $des;
if $primitive eq 'int' {
$constant := pir::repr_box_int__PiP($value, $type_obj);
$des := PAST::Op.new( :pirop('repr_box_int PiP'), $value, $type_obj_lookup );
}
elsif $primitive eq 'str' {
$constant := pir::repr_box_str__PsP($value, $type_obj);
$des := PAST::Op.new( :pirop('repr_box_str PsP'), $value, $type_obj_lookup );
}
elsif $primitive eq 'num' {
$constant := pir::repr_box_num__PnP($value, $type_obj);
$des := PAST::Op.new( :pirop('repr_box_num PnP'), $value, $type_obj_lookup );
}
else {
pir::die("Don't know how to build a $primitive constant");
}
# Add to SC, finish up deserialization code.
my $slot := self.add_object($constant);
self.add_event(:deserialize_past(
self.set_slot_past($slot, self.set_cur_sc($des))
));
# Build PAST for getting the boxed constant from the constants
# table, but also annotate it with the constant itself in case
# we need it.
my $past := self.get_slot_past_for_object($constant);
$past<has_compile_time_value> := 1;
$past<compile_time_value> := $constant;
return $past;
}
# Creates a meta-object for a package, adds it to the root objects and
# stores an event for the action. Returns the created object.
method pkg_create_mo($how, :$name, :$repr) {
# Create the meta-object and add to root objects.
my %args;
if pir::defined($name) { %args<name> := $name; }
if pir::defined($repr) { %args<repr> := $repr; }
my $mo := $how.new_type(|%args);
my $slot := self.add_object($mo);
# Add an event. There's no fixup to do, just a type object to create
# on deserialization.
my $setup_call := PAST::Op.new(
:pasttype('callmethod'), :name('new_type'),
self.get_object_sc_ref_past($how)
);
if pir::defined($name) {
$setup_call.push(PAST::Val.new( :value($name), :named('name') ));
}
if pir::defined($repr) {
$setup_call.push(PAST::Val.new( :value($repr), :named('repr') ));
}
self.add_event(:deserialize_past(
self.set_slot_past($slot, self.set_cur_sc($setup_call))));
# Result is just the object.
return $mo;
}
# Constructs a meta-attribute and adds it to a meta-object. Expects to
# be passed the meta-attribute type object, a set of literal named
# arguments to pass and a set of name to object mappings to pass also
# as named arguments, but where these passed objects also live in a
# serialization context. The type would be passed in this way.
method pkg_add_attribute($obj, $meta_attr, %lit_args, %obj_args) {
# Create and add right away.
my $attr := $meta_attr.new(|%lit_args, |%obj_args);
$obj.HOW.add_attribute($obj, $attr);
# Emit code to create and add it when deserializing.
my $create_call := PAST::Op.new(
:pasttype('callmethod'), :name('new'),
self.get_object_sc_ref_past($meta_attr)
);
for %lit_args {
$create_call.push(PAST::Val.new( :value($_.value), :named($_.key) ));
}
for %obj_args {
my $lookup := self.get_object_sc_ref_past($_.value);
$lookup.named($_.key);
$create_call.push($lookup);
}
my $obj_slot_past := self.get_object_sc_ref_past($obj);
self.add_event(:deserialize_past(PAST::Op.new(
:pasttype('callmethod'), :name('add_attribute'),
PAST::Op.new( :pirop('get_how PP'), $obj_slot_past ),
$obj_slot_past,
$create_call
)));
}
# Adds a method to the meta-object, and stores an event for the action.
method pkg_add_method($obj, $meta_method_name, $name, $code_object) {
# Add it to the compile time meta-object.
$obj.HOW."$meta_method_name"($obj, $name, $code_object);
# Add call to add the method at deserialization time.
my $slot_past := self.get_object_sc_ref_past($obj);
self.add_event(
:deserialize_past(PAST::Op.new(
:pasttype('callmethod'), :name($meta_method_name),
PAST::Op.new( :pirop('get_how PP'), $slot_past ),
$slot_past,
$name,
self.get_object_sc_ref_past($code_object)
)));
}
# Composes the package, and stores an event for this action.
method pkg_compose($obj) {
# Compose.
$obj.HOW.compose($obj);
# Emit code to do the composition when deserializing.
my $slot_past := self.get_object_sc_ref_past($obj);
self.add_event(:deserialize_past(PAST::Op.new(
:pasttype('callmethod'), :name('compose'),
PAST::Op.new( :pirop('get_how PP'), $slot_past ),
$slot_past
)));
}
# Checks if a given symbol is declared and also a type object.
method is_type(@name) {
my $is_type := 0;
try {
$is_type := !pir::repr_defined__IP(self.find_symbol(@name))
}
$is_type
}
# Finds a symbol that has a known value at compile time from the
# perspective of the current scope. Checks for lexicals, then if
# that fails tries package lookup.
method find_symbol(@name) {
# Make sure it's not an empty name.
unless +@name { pir::die("Cannot look up empty name"); }
# If it's a single-part name, look through the lexical
# scopes.
if +@name == 1 {
my $final_name := @name[0];
my $i := +@!BLOCKS;
while $i > 0 {
$i := $i - 1;
my %sym := @!BLOCKS[$i].symbol($final_name);
if +%sym {
if pir::exists(%sym, 'value') {
return %sym<value>;
}
else {
pir::die("No compile-time value for $final_name");
}
}
}
}
# If it's a multi-part name, see if the containing package
# is a lexical somewhere. Otherwise we fall back to looking
# in GLOBALish.
my $result := $*GLOBALish;
if +@name >= 2 {
my $first := @name[0];
my $i := +@!BLOCKS;
while $i > 0 {
$i := $i - 1;
my %sym := @!BLOCKS[$i].symbol($first);
if +%sym {
if pir::exists(%sym, 'value') {
$result := %sym<value>;
@name.shift();
last;
}
else {
pir::die("No compile-time value for $first");
}
}
}
}
# Try to chase down the parts of the name.
for @name {
if pir::exists($result.WHO, ~$_) {
$result := ($result.WHO){$_};
}
else {
pir::die("Could not locate compile-time value for symbol " ~
pir::join('::', @name));
}
}
$result;
}
# Takes a name and compiles it to a lookup for the symbol.
method symbol_lookup(@name, $/) {
# Catch empty names and die helpfully.
if +@name == 0 { $/.CURSOR.panic("Cannot compile empty name"); }
# If it's a single item, then go hunting for it through the
# block stack.
if +@name == 1 {
my $i := +@!BLOCKS;
while $i > 0 {
$i := $i - 1;
my %sym := @!BLOCKS[$i].symbol(@name[0]);
if +%sym {
return PAST::Var.new( :name(@name[0]), :scope(%sym<scope>) );
}
}
}
# The final lookup will always be just a keyed access to a
# symbol table.
my $final_name := @name.pop();
my $lookup := PAST::Var.new( :scope('keyed'), ~$final_name);
# If there's no explicit qualification, then look it up in the
# current package, and fall back to looking in GLOBAL.
if +@name == 0 {
$lookup.unshift(PAST::Op.new(
:pirop('get_who PP'),
PAST::Var.new( :name('$?PACKAGE'), :scope('lexical') )
));
$lookup.viviself(PAST::Var.new(
:scope('keyed'),
PAST::Op.new(
:pirop('get_who PP'),
PAST::Var.new( :name('GLOBAL'), :namespace([]), :scope('package') )
),
~$final_name
));
}
# Otherwise, see if the first part of the name is lexically
# known. If not, it's in GLOBAL. Also, if first part is GLOBAL
# then strip it off.
else {
my $path := self.is_lexical(@name[0]) ??
PAST::Var.new( :name(@name.shift()), :scope('lexical') ) !!
PAST::Var.new( :name('GLOBAL'), :namespace([]), :scope('package') );
if @name[0] eq 'GLOBAL' {
@name.shift();
}
for @name {
$path := PAST::Op.new(
:pirop('nqp_get_package_through_who PPs'),
$path, ~$_);
}
$lookup.unshift(PAST::Op.new(:pirop('get_who PP'), $path));
}
return $lookup;
}
# Checks if the given name is known anywhere in the lexpad
# and with lexical scope.
method is_lexical($name) {
my $i := +@!BLOCKS;
while $i > 0 {
$i := $i - 1;
my %sym := @!BLOCKS[$i].symbol($name);
if +%sym {
return %sym<scope> eq 'lexical';
}
}
0;
}
# XXX TODO
method is_attr_alias($name) {
0
}
# Generates a series of PAST operations that will build this context if
# it doesn't exist, and fix it up if it already does.
method to_past() {
my $des := PAST::Stmts.new();
my $fix := PAST::Stmts.new();
for self.event_stream() {
$des.push($_.deserialize_past()) if pir::defined($_.deserialize_past());
$fix.push($_.fixup_past()) if pir::defined($_.fixup_past());
}
make PAST::Op.new(
:pasttype('if'),
PAST::Op.new(
:pirop('isnull IP'),
PAST::Op.new( :pirop('nqp_get_sc Ps'), self.handle() )
),
PAST::Stmts.new(
PAST::Op.new( :pirop('nqp_dynop_setup v') ),
PAST::Op.new( :pirop('rakudo_dynop_setup v') ),
PAST::Op.new(
:pasttype('callmethod'), :name('hll_map'),
PAST::Op.new( :pirop('getinterp P') ),
PAST::Op.new( :pirop('get_class Ps'), 'LexPad' ),
PAST::Op.new( :pirop('get_class Ps'), 'Perl6LexPad' )
),
PAST::Op.new(
:pasttype('bind'),
PAST::Var.new( :name('cur_sc'), :scope('register'), :isdecl(1) ),
PAST::Op.new( :pirop('nqp_create_sc Ps'), self.handle() )
),
$des
),
$fix
);
}
}