Skip to content

Commit 2b1619e

Browse files
committed
Compile time symbol installation becomes a task for the SC builder.
1 parent e458b36 commit 2b1619e

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

src/HLL/SerializationContextBuilder.pm

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,30 @@ class HLL::Compiler::SerializationContextBuilder {
169169
}
170170
}
171171

172+
# Installs a symbol into the package. Does so immediately, and
173+
# makes sure this happens on deserialization also.
174+
method install_package_symbol(@sym, $obj) {
175+
@sym := pir::clone__PP(@sym);
176+
my $name := ~@sym.pop();
177+
178+
# XXX Eventually, we'll want to do immediate installation.
179+
#pir::get_hll_namespace__PP(@sym).add_var($name, $obj);
180+
#pir::say('# installed ' ~ pir::join('::', @sym) ~ "::" ~ $name);
181+
182+
# Deserialization installation. XXX And fixup for now too.
183+
self.add_event(:deserialize_past(
184+
PAST::Op.new(
185+
:pasttype('bind'),
186+
PAST::Var.new( :name($name), :namespace(@sym), :scope('package') ),
187+
self.get_slot_past_for_object($obj)
188+
)),
189+
:fixup_past(PAST::Op.new(
190+
:pasttype('bind'),
191+
PAST::Var.new( :name($name), :namespace(@sym), :scope('package') ),
192+
self.get_slot_past_for_object($obj)
193+
)));
194+
}
195+
172196
# Creates a meta-object for a package, adds it to the root objects and
173197
# stores an event for the action. Returns the created object.
174198
method pkg_create_mo($how, :$name, :$repr) {

src/NQP/Actions.pm

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,16 @@ method package_def($/) {
415415
# Get the body code.
416416
my $past := $<block> ?? $<block>.ast !! $<comp_unit>.ast;
417417

418-
# Prefix the class initialization with initial setup. Also install it
419-
# in the symbol table right away.
418+
# Install it in the package or lexpad as needed.
419+
if $*SCOPE eq 'our' || $*SCOPE eq '' {
420+
$past.namespace( $<name><identifier> );
421+
$*SC.install_package_symbol($<name><identifier>, $*PKGMETA);
422+
}
423+
else {
424+
$/.CURSOR.panic("$*SCOPE scoped packages are not supported");
425+
}
426+
427+
# Prefix the class initialization with initial setup.
420428
$*PACKAGE-SETUP.unshift(PAST::Stmts.new(
421429
PAST::Op.new( :pasttype('bind'),
422430
PAST::Var.new( :name('type_obj'), :scope('register'), :isdecl(1) ),
@@ -427,18 +435,6 @@ method package_def($/) {
427435
PAST::Var.new( :name('type_obj'), :scope('register') )
428436
)
429437
));
430-
431-
# Install it in the package or lexpad as needed.
432-
if $*SCOPE eq 'our' || $*SCOPE eq '' {
433-
$past.namespace( $<name><identifier> );
434-
$*PACKAGE-SETUP[0].push(PAST::Op.new( :pasttype('bind'),
435-
PAST::Var.new( :name($name), :namespace(@ns), :scope('package') ),
436-
PAST::Var.new( :name('type_obj'), :scope('register') )
437-
));
438-
}
439-
else {
440-
$/.CURSOR.panic("$*SCOPE scoped packages are not supported");
441-
}
442438

443439
# Evaluate everything in the package in-line unless this is a generic
444440
# type in which case it needs delayed evaluation. Normally, $?CLASS is
@@ -523,9 +519,6 @@ method package_def($/) {
523519
),
524520
PAST::Var.new( :name('type_obj'), :scope('register') )
525521
));
526-
527-
# Set up slot for the type object to live in.
528-
@BLOCK[0][0].unshift(PAST::Var.new( :name($name), :namespace(@ns), :scope('package'), :isdecl(1) ));
529522

530523
# Attach the class code to run at loadinit time.
531524
$past.loadinit.push(PAST::Block.new( :blocktype('immediate'), $*PACKAGE-SETUP ));

0 commit comments

Comments
 (0)