/
Metamodel.pm
924 lines (768 loc) · 27.3 KB
/
Metamodel.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
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
use 5.010;
use strict;
use warnings;
use utf8;
package Metamodel;
use Unit;
use Body;
use Op;
use RxOp;
use Sig;
use YAML::XS;
use Scalar::Util 'blessed';
### NIECZA COMPILER METAMODEL
# The metamodel exists to create a timeline inside the compiler. Previously,
# the compiler operated as a pure tree transformer with no conception of how
# PRE-INIT code would play out, thus precluding a lot of important
# optimizations (based on precomputing immutable objects and optimizing
# references to them, mostly).
#
# The metamodel has two main life stages. First, it is built; an incremental
# process logically called BEGIN. Then, it is processed to perform closed-
# world optimizations and generate code; this is (UNIT)CHECK.
#
# Kinds of objects which exist in the metamodel
# - Static subs
# - Packages (incl. classes, modules, grammars)
# - Stashes (Foo::)
#
# This graph is a lot more random than the old trees were...
# these should only be used during the Op walk
our @opensubs;
our $unit;
# A stash is an object like Foo::. Foo and Foo:: are closely related, but
# generally must be accessed separately due to constants (which have Foo but
# not Foo::) and stub packages (vice versa). They are reached as distinct
# slots in the parent package. Note that the root stash can have no
# corresponding package.
#
# a stash is associated with a single unit. each unit has a graph of stashes
# rooted from the unit's global. 'my' stashes are really 'our' stashes with
# gensym mergable names. because stashes have no identity beyond their contents
# and set of names, they don't mind being copied around a lot.
#
# for the same reasons you generally shouldn't keep references to stashes. ask
# for the path and look it up when needed.
{
package Metamodel::Stash;
use Moose;
# zyg entries can point to:
# - other Stashes in the same unit (for ::; also, tree structure)
# - Metamodel::Stash::Graft for class import/export
# - StaticSub (via reference)
# - Package (via reference)
has zyg => (isa => 'HashRef', is => 'ro',
default => sub { +{} });
# not canonical, but at least usable in importers
# 1st element is always GLOBAL or $unitname $id
has path => (isa => 'ArrayRef[Str]', is => 'ro', required => 1);
has parent => (isa => 'Maybe[Metamodel::Stash]', is => 'ro');
sub bind_name {
my ($self, $name, $sub) = @_;
$self->zyg->{$name} = $sub;
}
no Moose;
__PACKAGE__->meta->make_immutable;
}
{
package Metamodel::Stash::Graft;
use Moose;
has to => (isa => 'ArrayRef[Str]', is => 'ro', required => 1);
no Moose;
__PACKAGE__->meta->make_immutable;
}
{
package Metamodel::Package;
use Moose;
has xid => (isa => 'Int', is => 'rw');
# an intrinsic name, even if anonymous
has name => (isa => 'Str', is => 'ro', default => 'ANON');
has unit_closed => (isa => 'Bool', is => 'rw');
sub BUILD { push @{ $unit->packages }, $_[0] }
sub add_attribute {
my ($self, $name) = @_;
die "attribute $name defined in a lowly package";
}
sub add_method {
my ($self, $name, $body) = @_;
die "method $name defined in a lowly package";
}
sub add_super {
my ($self, $super) = @_;
die "superclass " . $unit->deref($super)->name .
" defined in a lowly package";
}
sub close { }
no Moose;
__PACKAGE__->meta->make_immutable;
}
{
package Metamodel::Module;
use Moose;
extends 'Metamodel::Package';
no Moose;
__PACKAGE__->meta->make_immutable;
}
{
package Metamodel::Class;
use Moose;
extends 'Metamodel::Module';
has attributes => (isa => 'ArrayRef[Str]', is => 'ro',
default => sub { [] });
has methods => (isa => 'ArrayRef[Metamodel::Method]', is => 'ro',
default => sub { [] });
has superclasses => (isa => 'ArrayRef', is => 'ro',
default => sub { [] });
has multi_regex_lists => (isa => 'HashRef[ArrayRef]',
is => 'ro', lazy => 1, default => sub { +{} });
sub add_attribute {
my ($self, $name) = @_;
push @{ $self->attributes }, $name;
}
sub add_method {
my ($self, $type, $name, $body) = @_;
push @{ $self->methods }, Metamodel::Method->new(name => $name,
body => $body, private => ($type eq '!'));
}
sub push_multi_regex {
my ($self, $name, $body) = @_;
push @{ $self->multi_regex_lists->{$name} //= [] }, $body;
}
sub add_super {
my ($self, $targ) = @_;
Carp::confess "bad attempt to add null super" unless $targ;
push @{ $self->superclasses }, $targ;
}
sub close {
my ($self, $targ) = @_;
if ($self->name ne 'Mu' && $unit->is_true_setting
&& !@{ $self->superclasses }) {
$self->add_super($unit->get_stash_obj(
@{ $opensubs[-1]->find_pkg($self->_defsuper) }));
}
}
sub _defsuper { 'Any' } #XXX CORE::Any
no Moose;
__PACKAGE__->meta->make_immutable;
}
{
package Metamodel::Grammar;
use Moose;
extends 'Metamodel::Class';
sub _defsuper { 'Grammar' }
no Moose;
__PACKAGE__->meta->make_immutable;
}
{
package Metamodel::Method;
use Moose;
has name => (isa => 'Str', is => 'ro', required => 1);
has private => (isa => 'Bool', is => 'ro', required => 1);
has body => (is => 'ro', required => 1);
no Moose;
__PACKAGE__->meta->make_immutable;
}
# This is a static lexical; they exist in finite number per unit. They may
# occupy specific slots in pads, or globals, or something else entirely.
{
package Metamodel::Lexical;
use Moose;
no Moose;
__PACKAGE__->meta->make_immutable;
}
# my $foo, @foo, %foo, &foo
{
package Metamodel::Lexical::Simple;
use Moose;
extends 'Metamodel::Lexical';
has list => (isa => 'Bool', is => 'ro', default => 0);
has hash => (isa => 'Bool', is => 'ro', default => 0);
has noinit => (isa => 'Bool', is => 'ro', default => 0);
no Moose;
__PACKAGE__->meta->make_immutable;
}
# our...
{
package Metamodel::Lexical::Common;
use Moose;
extends 'Metamodel::Lexical';
has path => (isa => 'ArrayRef[Str]', is => 'ro', required => 1);
has name => (isa => 'Str', is => 'ro', required => 1);
no Moose;
__PACKAGE__->meta->make_immutable;
}
# mostly for state
{
package Metamodel::Lexical::Alias;
use Moose;
extends 'Metamodel::Lexical';
has to => (isa => 'Str', is => 'ro', required => 1);
sub BUILDARGS { +{ to => $_[1] } }
no Moose;
__PACKAGE__->meta->make_immutable;
}
# sub foo { ... }
{
package Metamodel::Lexical::SubDef;
use Moose;
extends 'Metamodel::Lexical';
has body => (isa => 'Metamodel::StaticSub', is => 'ro');
no Moose;
__PACKAGE__->meta->make_immutable;
}
{
package Metamodel::Lexical::SubImport;
use Moose;
extends 'Metamodel::Lexical';
has ref => (isa => 'ArrayRef', is => 'ro');
no Moose;
__PACKAGE__->meta->make_immutable;
}
# my class Foo { } or our class Foo { }; either case, the true stash lives in
# stashland
{
package Metamodel::Lexical::Stash;
use Moose;
extends 'Metamodel::Lexical';
has path => (isa => 'ArrayRef[Str]', is => 'ro');
no Moose;
__PACKAGE__->meta->make_immutable;
}
# The life cycle of a static sub has three phases.
# 1. Open - the end of the sub hasn't been seen, so the full code is absent.
# 2. Closing - all attributes are available but no references exist. The
# perfect time for most optimizations, especially ones that look like
# escape analyses.
# 3. Closed - references exist, possibly even from BEGIN-run code. The sub
# must be treated as semantically immutable. The code can probably still
# be changed to reflect new information, though.
# TODO: figure out how post-declared lexicals should interact with codegen
# std accepts: sub foo() { bar }; BEGIN { foo }; sub bar() { }
{
package Metamodel::StaticSub;
use Moose;
has unit => (isa => 'Metamodel::Unit', is => 'ro', weak_ref => 1);
has xid => (isa => 'Int', is => 'rw');
has outer => (is => 'bare');
has run_once => (isa => 'Bool', is => 'ro', default => 0);
has spad_exists => (isa => 'Bool', is => 'rw', default => 0);
has lexicals => (isa => 'HashRef[Metamodel::Lexical]', is => 'ro',
default => sub { +{} });
has code => (isa => 'Op', is => 'rw');
has signature=> (isa => 'Maybe[Sig]', is => 'rw');
# inject a take EMPTY
has gather_hack => (isa => 'Bool', is => 'ro', default => 0);
has strong_used => (isa => 'Bool', is => 'rw', default => 0);
has body_of => (isa => 'Maybe[ArrayRef]', is => 'ro');
has in_class => (isa => 'Maybe[ArrayRef]', is => 'ro');
has cur_pkg => (isa => 'Maybe[ArrayRef[Str]]', is => 'ro');
has name => (isa => 'Str', is => 'ro', default => 'ANON');
has returnable => (isa => 'Bool', is => 'ro', default => 0);
has augmenting => (isa => 'Bool', is => 'ro', default => 1);
has class => (isa => 'Str', is => 'ro', default => 'Sub');
has ltm => (is => 'rw');
has unit_closed => (isa => 'Bool', is => 'rw');
sub outer {
my $v = $_[0]{outer};
return $v if !$v or blessed($v);
$_[0]{unit}->deref($v);
}
sub children {
map { $_->body } grep { $_->isa('Metamodel::Lexical::SubDef') }
values %{ $_[0]->lexicals };
}
sub create_static_pad {
my ($self) = @_;
return if $self->spad_exists;
$self->spad_exists(1);
$self->outer->create_static_pad if $self->outer;
}
sub find_lex_pkg { my ($self, $name) = @_;
my $toplex = $self->find_lex($name) // return undef;
if (!$toplex->isa('Metamodel::Lexical::Stash')) {
die "$name is declared as a non-package";
}
$toplex->path;
}
sub find_pkg { my ($self, $names) = @_;
my @names = ref($names) ? @$names : ('MY', $names);
$_ =~ s/::$// for (@names); #XXX
my @tp;
if ($names[0] eq 'OUR') {
@tp = @{ $self->cur_pkg };
shift @names;
} elsif ($names[0] eq 'PROCESS' or $names[0] eq 'GLOBAL') {
@tp = shift(@names);
} elsif ($names[0] eq 'MY') {
@tp = @{ $self->find_lex_pkg($names[1]) };
splice @names, 0, 2;
} elsif (my $p = $self->find_lex_pkg($names->[0])) {
@tp = @$p;
shift @names;
} else {
@tp = 'GLOBAL';
}
[ @tp, @names ];
}
sub find_lex { my ($self, $name) = @_;
my $l = $self->lexicals->{$name};
if ($l) {
return $l->isa('Metamodel::Lexical::Alias') ?
$self->find_lex($l->to) : $l;
}
return ($self->outer ? $self->outer->find_lex($name) : undef);
}
sub add_my_name { my ($self, $slot, @ops) = @_;
$self->lexicals->{$slot} = Metamodel::Lexical::Simple->new(@ops);
}
sub add_common_name { my ($self, $slot, $path, $name) = @_;
$unit->get_stash(@$path);
$self->lexicals->{$slot} = Metamodel::Lexical::Common->new(
path => $path, name => $name);
}
sub add_state_name { my ($self, $slot, $back, @ops) = @_;
# outermost sub isn't cloned so a fallback to my is safe
my $up = $self->outer // $self;
$up->lexicals->{$back} = Metamodel::Lexical::Simple->new(@ops);
$self->lexicals->{$slot} = Metamodel::Lexical::Alias->new($back)
if defined($slot);
}
sub add_my_stash { my ($self, $slot, $path) = @_;
$self->lexicals->{$slot} = Metamodel::Lexical::Stash->new(
path => $path);
}
sub add_my_sub { my ($self, $slot, $body) = @_;
$self->lexicals->{$slot} = Metamodel::Lexical::SubDef->new(
body => $body);
}
sub add_pkg_exports { my ($self, $unit, $name, $path2, $tags) = @_;
my $thing = Metamodel::Stash::Graft->new(to => $path2);
for my $tag (@$tags) {
my $repo = $unit->get_stash(@{ $self->cur_pkg }, 'EXPORT', $tag);
$repo->bind_name($name . '::', $thing);
}
scalar @$tags;
}
sub add_exports { my ($self, $unit, $name, $thing, $tags) = @_;
for my $tag (@$tags) {
my $repo = $unit->get_stash(@{ $self->cur_pkg }, 'EXPORT', $tag);
$repo->bind_name($name, $thing);
}
scalar @$tags;
}
sub close { }
no Moose;
__PACKAGE__->meta->make_immutable;
}
{
package Metamodel::Unit;
use Moose;
has mainline => (isa => 'Metamodel::StaticSub', is => 'rw');
has name => (isa => 'Str', is => 'ro');
has sroot => (isa => 'Metamodel::Stash', is => 'ro', default =>
sub { Metamodel::Stash->new(path => []) });
has setting => (isa => 'Str', is => 'ro');
# ref to parent of Op::YouAreHere
has bottom_ref => (is => 'rw');
has xref => (isa => 'ArrayRef', is => 'ro', default => sub { [] });
has tdeps => (isa => 'HashRef[Metamodel::Unit]', is => 'ro',
default => sub { +{} });
has filename => (isa => 'Str', is => 'rw');
has modtime => (isa => 'Num', is => 'rw');
has syml => (is => 'rw');
# we like to delete staticsubs in the optimizer, so visiting them is
# a tad harder
has packages => (isa => 'ArrayRef[Metamodel::Package]', is => 'ro',
default => sub { [] });
has next_anon_stash => (isa => 'Int', is => 'rw', default => 0);
# XXX should be fed in perhaps from name, but this is good for testing
sub is_true_setting { 1 }
sub get_unit {
my ($self, $name) = @_;
if ($name eq $self->name) { return $self }
$self->tdeps->{$name};
}
sub anon_stash {
my $i = $_[0]->next_anon_stash;
$_[0]->next_anon_stash($i+1);
$_[0]->name . ":" . $i;
}
sub get_stash_obj {
my ($self, @path) = @_;
my $end = pop @path;
my $stash = $self->get_stash(@path);
return $stash->zyg->{$end};
}
sub get_stash {
my ($self, @path) = @_;
my $ptr = $self->sroot;
for (@path) {
my $name = $_;
$name =~ s/:://; #XXX frontend broken
if ($name eq 'PARENT') {
$ptr = $ptr->parent // die "stash has no parent";
} elsif ($name eq 'CALLER' || $name eq 'OUTER' ||
$name eq 'SETTING' || $name eq 'UNIT') {
die "$name cannot be used to descend from a package";
} else {
$ptr = $ptr->zyg->{$name . '::'} //=
Metamodel::Stash->new(parent => $ptr,
path => [ @{ $ptr->path }, $name ]);
$ptr = $self->deref($ptr) if !blessed($ptr);
if ($ptr->isa('Metamodel::Stash::Graft')) {
$ptr = $self->get_stash(@{ $ptr->to });
} elsif ($ptr->isa('Metamodel::Stash')) {
} else {
die "$name is a non-subpackage";
}
}
}
$ptr;
}
sub make_ref {
my ($self, $thing) = @_;
my $xid;
if (!defined ($xid = $thing->xid)) {
$thing->xid($xid = scalar @{ $self->xref });
push @{ $self->xref }, $thing;
}
return [ $self->name, $xid ];
}
sub deref {
my ($self, $thing) = @_;
Carp::confess "trying to dereference null" unless $thing;
return $self->get_unit($thing->[0])->xref->[$thing->[1]];
}
sub visit_units_preorder {
my ($self, $cb) = @_;
my %seen;
our $rec; local $rec = sub {
return if $seen{$_};
$seen{$_} = 1;
for (sort keys %{ $self->get_unit($_)->tdeps }) {
$rec->();
}
$cb->($_) for ($self->get_unit($_));
};
$rec->() for ($self->name);
}
sub visit_local_packages {
my ($self, $cb) = @_;
$cb->($_) for @{ $self->packages };
}
sub visit_local_stashes {
my ($self, $cb) = @_;
our $rec; local $rec = sub {
$cb->($_);
for (values %{ $_->zyg }) {
next unless blessed($_) && $_->isa('Metamodel::Stash');
$rec->();
}
};
$rec->() for $self->sroot;
}
sub visit_local_subs_postorder {
my ($self, $cb) = @_;
our $rec; local $rec = sub {
return if $_->unit_closed;
for ($_->children) { $rec->(); }
$cb->($_);
};
for ($self->mainline) { $rec->(); }
}
sub visit_local_subs_preorder {
my ($self, $cb) = @_;
our $rec; local $rec = sub {
return if $_->unit_closed;
$cb->($_);
for ($_->children) { $rec->(); }
};
for ($self->mainline) { $rec->(); }
}
# must be LAST call before Storable dump - breaks visitors!
sub close_unit {
my ($self) = @_;
$self->visit_local_subs_postorder(sub { $_->unit_closed(1) });
$self->visit_local_packages(sub { $_->unit_closed(1) });
@{ $self->packages } = ();
}
sub need_unit {
my ($self, $u2) = @_;
$self->tdeps->{$u2->name} = $u2;
for (keys %{ $u2->tdeps }) {
$self->tdeps->{$_} //= $u2->tdeps->{$_};
}
our $rec; local $rec = sub {
my (@path) = @_;
my $sl = $self->get_stash(@path);
my $sf = $u2->get_stash(@path);
for my $fk (sort keys %{ $sf->zyg }) {
my $fo = $sf->zyg->{$fk};
my $lo = $sl->zyg->{$fk};
if (!$lo && !blessed($fo)) {
$sl->bind_name($fk, $fo);
next;
}
if (blessed($fo) && (!$lo || blessed($lo))) {
$rec->(@path, $fk);
} elsif (!blessed($fo) && !blessed($lo) && $fo->[0] eq $lo->[0]
&& $fo->[1] == $lo->[1]) {
# same object, no collision
} else {
die "unification error: " . join("::", @path, $fk);
}
}
};
$rec->();
}
no Moose;
__PACKAGE__->meta->make_immutable;
}
### Code goes here to build up the metamodel from an Op tree
# We should eventually wire this to the parser, so that metamodel stuff can
# exist during the parse itself; will be needed for macros
sub Unit::begin {
my $self = shift;
local $unit = Metamodel::Unit->new(name => $self->name,
$::SETTING_UNIT ? (setting => $::SETTING_UNIT->name) : ());
$unit->need_unit($::SETTING_UNIT) if $::SETTING_UNIT;
$unit->get_stash('GLOBAL');
$unit->get_stash('PROCESS');
local @opensubs;
$unit->mainline($self->mainline->begin(once => 1,
top => ($::SETTING_UNIT ? $::SETTING_UNIT->bottom_ref : undef)));
$unit;
}
sub Body::begin {
my $self = shift;
my %args = @_;
my $top = @opensubs ? $opensubs[-1] : $args{top};
my $rtop = !$top ? $top : Scalar::Util::blessed($top) ? $top :
$unit->deref($top);
my $metabody = Metamodel::StaticSub->new(
unit => $unit,
outer => $top,
body_of => $args{body_of},
in_class => $args{body_of} // (@opensubs ? $opensubs[-1]->in_class :
undef),
cur_pkg => $args{cur_pkg} // (@opensubs ? $opensubs[-1]->cur_pkg :
[ 'GLOBAL' ]), # cur_pkg does NOT propagate down from settings
augmenting => $args{augmenting},
name => $self->name,
returnable => $self->returnable,
gather_hack=> $args{gather_hack},
class => $self->class,
ltm => $self->ltm,
run_once => $args{once} && (!@opensubs || $rtop->run_once));
if ($self->signature && @{ $self->signature->params } >= 1 &&
$self->signature->params->[0]->slot eq '$¢') {
$metabody->lexicals->{'$/'} = Metamodel::Lexical::Alias->new('$¢');
}
$unit->get_stash(@{ $metabody->cur_pkg });
push @opensubs, $metabody; # always visible in the signature XXX
if ($self->signature) {
$self->signature->begin;
$metabody->signature($self->signature);
}
pop @opensubs if $self->transparent;
$self->do->begin;
$metabody->code($self->do);
$metabody->close;
pop @opensubs unless $self->transparent;
$metabody;
}
sub Sig::begin {
my $self = shift;
$_->begin for @{ $self->params };
}
sub Sig::Parameter::begin {
my $self = shift;
$opensubs[-1]->add_my_name($self->slot, list => $self->list,
hash => $self->hash, noinit => 1) if defined $self->slot;
$self->default->begin if defined($self->default);
}
sub Op::begin {
my $self = shift;
$_->begin for $self->zyg;
}
sub Op::YouAreHere::begin {
my $self = shift;
$unit->bottom_ref($unit->make_ref($opensubs[-1]));
$opensubs[-1]->strong_used(1);
$opensubs[-1]->create_static_pad;
}
sub Op::Use::begin {
my $self = shift;
my $name = $self->unit;
my $u2 = CompilerDriver::metadata_for($self->unit);
$unit->need_unit($u2);
my @can = @{ $u2->mainline->find_pkg(split /::/, $name) };
my $exp = $unit->get_stash(@can, 'EXPORT', 'DEFAULT');
# XXX I am not sure how need binding should work
for my $en (sort keys %{ $exp->zyg }) {
my $uname = $en;
my $ref = $exp->zyg->{$en};
my $ex = blessed($ref) ? $ref : $unit->deref($ref);
my $lex;
if ($ex->isa('Metamodel::Stash')) {
$uname =~ s/:://;
$lex = Metamodel::Lexical::Stash->new(path => $ex->path);
} elsif ($ex->isa('Metamodel::Package')) {
next; # XXX this is hacked elsewhere
} elsif ($ex->isa('Metamodel::Stash::Graft')) {
$uname =~ s/:://;
$lex = Metamodel::Lexical::Stash->new(path => $ex->to);
} elsif ($ex->isa('Metamodel::StaticSub')) {
$lex = Metamodel::Lexical::SubImport->new(ref => $ref);
} elsif ($ex->isa('Metamodel::ExportedVar')) {
$lex = Metamodel::Lexical::VarImport->new(ref => $ref);
} else {
die "unhandled export type " . ref($ex);
}
$opensubs[-1]->lexicals->{$uname} = $lex;
}
}
sub Op::Lexical::begin {
my $self = shift;
if ($self->state_backing) {
$opensubs[-1]->add_state_name($self->name, $self->state_backing,
list => $self->list, hash => $self->hash);
} elsif ($self->declaring) {
$opensubs[-1]->add_my_name($self->name, list => $self->list,
hash =>$self->hash);
}
}
sub Op::CallMethod::begin {
my $self = shift;
$self->Op::begin;
if ($self->private) {
if ($self->ppath) {
$self->pclass($unit->get_stash_obj(@{ $opensubs[-1]->find_pkg($self->ppath) }));
} elsif ($opensubs[-1]->in_class) {
$self->pclass($opensubs[-1]->in_class);
} else {
die "unable to resolve class of reference for method";
}
}
}
sub Op::PackageVar::begin {
my $self = shift;
if ($self->looks_static) {
# cache the lookup here
$opensubs[-1]->add_common_name($self->slot,
$opensubs[-1]->find_pkg($self->path), $self->name);
}
}
sub Op::Attribute::begin {
my $self = shift;
my $ns = $opensubs[-1]->body_of // die ("attribute " . $self->name .
" declared outside of any class");
die "attribute $self->name declared in an augment"
if $opensubs[-1]->augmenting;
$ns = $unit->deref($ns);
$ns->add_attribute($self->name);
my $nb = Metamodel::StaticSub->new(
unit => $unit,
outer => $opensubs[-1],
name => $self->name,
cur_pkg => $opensubs[-1]->cur_pkg,
returnable => 0,
class => 'Sub',
run_once => 0,
code => Op::GetSlot->new(name => $self->name,
object => Op::CgOp->new(optree => [ pos => 0 ])));
$opensubs[-1]->create_static_pad; # for protosub instance
$nb->strong_used(1);
$opensubs[-1]->add_my_sub($self->name . '!a', $nb);
my $r = $unit->make_ref($nb);
$ns->add_method('!', $self->name, $r);
if ($self->accessor) {
$ns->add_method('', $self->name, $unit->make_ref($nb));
}
}
sub Op::Super::begin {
my $self = shift;
my $ns = $opensubs[-1]->body_of // die ("superclass " . $self->name .
" declared outside of any class");
$ns = $unit->deref($ns);
die "superclass $self->name declared in an augment"
if $opensubs[-1]->augmenting;
$ns->add_super($unit->get_stash_obj(@{ $opensubs[-1]->find_pkg($self->name) }));
}
sub Op::SubDef::begin {
my $self = shift;
my $body = $self->body->begin(once => $self->once);
$opensubs[-1]->add_my_sub($self->var, $body);
my $r;
if (@{ $self->exports } || defined($self->method_too) ||
defined ($self->proto_too)) {
$r = $unit->make_ref($body);
$body->strong_used(1);
}
$opensubs[-1]->create_static_pad if $body->strong_used;
if (defined($self->method_too)) {
$unit->deref($opensubs[-1]->body_of)
->add_method(@{ $self->method_too }, $r);
}
if (defined($self->proto_too)) {
$unit->deref($opensubs[-1]->body_of)
->push_multi_regex($self->proto_too, $r);
}
$opensubs[-1]->add_exports($unit, $self->var, $r, $self->exports);
delete $self->{$_} for (qw( body method_too proto_too exports ));
}
sub Op::BareBlock::begin {
my $self = shift;
my $body = $self->body->begin;
$opensubs[-1]->add_my_sub($self->var, $body);
delete $self->{$_} for (qw( body ));
}
sub Op::Gather::begin {
my $self = shift;
my $body = $self->body->begin(gather_hack => 1);
$opensubs[-1]->add_my_sub($self->var, $body);
delete $self->{$_} for (qw( body ));
}
sub Op::WhateverCode::begin {
my $self = shift;
my $body = Body->new(name => 'ANON', transparent => 1, do => $self->ops,
signature => Sig->simple(@{ $self->vars }));
delete $self->{$_} for (qw( vars ops ));
$opensubs[-1]->add_my_sub($self->slot, $body->begin);
}
sub Op::Start::begin {
my $self = shift;
$opensubs[-1]->add_state_name(undef, $self->condvar);
$self->Op::begin;
}
sub Op::PackageDef::begin {
my $self = shift;
my $pclass = ref($self);
$pclass =~ s/Op::(.*)Def/Metamodel::$1/;
my @ns = $self->ourpkg ?
(@{ $opensubs[-1]->find_pkg($self->ourpkg) }, $self->var) :
($unit->anon_stash);
my $n = pop(@ns);
$opensubs[-1]->add_my_stash($self->var, [ @ns, $n ]);
$opensubs[-1]->add_pkg_exports($unit, $self->var, [ @ns, $n ], $self->exports);
if (!$self->stub) {
my $obj = $unit->make_ref($pclass->new(name => $self->name));
my $body = $self->body->begin(body_of => $obj, cur_pkg => [ @ns, $n ],
once => 1);
$unit->deref($obj)->close;
$unit->get_stash(@ns)->bind_name($n, $obj);
$opensubs[-1]->add_exports($unit, $self->var, $obj, $self->exports);
$opensubs[-1]->add_my_sub($self->bodyvar, $body);
}
delete $self->{$_} for (qw(name body exports ourpkg));
}
sub Op::Augment::begin {
my $self = shift;
# XXX shouldn't we distinguish augment class Foo { } from ::Foo ?
my $pkg = $opensubs[-1]->find_pkg([ @{ $self->pkg }, $self->name ]);
my $body = $self->body->begin(body_of => $unit->get_stash_obj(@$pkg),
augmenting => 1, once => 1, cur_pkg => $pkg);
$opensubs[-1]->add_my_sub($self->bodyvar, $body);
delete $self->{$_} for (qw(name body pkg));
}
1;