Skip to content

Commit

Permalink
RakuAST: proper support for rw native args
Browse files Browse the repository at this point in the history
For writable native args we need to create lexicalref vars instead of
lexicals.
  • Loading branch information
niner committed Apr 19, 2024
1 parent c053144 commit b213dba
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/Raku/ast/signature.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,7 @@ class RakuAST::Parameter
{
my $cd := ContainerDescriptor.new(:of($type), :$name, :default($type), :dynamic(0));
nqp::bindattr($parameter, Parameter, '$!container_descriptor', $cd);
$!target.set-rw;
last;
}
}
Expand Down Expand Up @@ -1288,6 +1289,7 @@ class RakuAST::Parameter
class RakuAST::ParameterTarget
is RakuAST::Node
{
method set-rw() { }
method sigil() { '' }
method name() { '' }
}
Expand Down Expand Up @@ -1410,6 +1412,10 @@ class RakuAST::ParameterTarget::Var
nqp::bindattr(self, RakuAST::ParameterTarget::Var, '$!of', $of);
}

method set-rw() {
$!declaration.set-rw;
}

method attach(RakuAST::Resolver $resolver) {
$!declaration.attach($resolver);
}
Expand Down
11 changes: 9 additions & 2 deletions src/Raku/ast/variable-declaration.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ class RakuAST::VarDeclaration::Simple
has RakuAST::Expression $.where;
has RakuAST::Type $.original-type;
has Bool $!is-parameter;
has Bool $!is-rw;

has Mu $!container-initializer;
has Mu $!package;
Expand Down Expand Up @@ -565,6 +566,7 @@ class RakuAST::VarDeclaration::Simple
$where // RakuAST::Expression);
nqp::bindattr($obj, RakuAST::VarDeclaration::Simple, '$!original-type',
$type // RakuAST::Type);
nqp::bindattr($obj, RakuAST::VarDeclaration::Simple, '$!is-rw', False);

if $WHY {
$scope && $scope eq 'has'
Expand Down Expand Up @@ -592,6 +594,10 @@ class RakuAST::VarDeclaration::Simple
nqp::bindattr(self, RakuAST::VarDeclaration::Simple, '$!type', $type);
}

method set-rw() {
nqp::bindattr(self, RakuAST::VarDeclaration::Simple, '$!is-rw', True);
}

# Generate a lookup of this variable, already resolved to this declaration.
method generate-lookup() {
if self.is-lexical {
Expand Down Expand Up @@ -1014,7 +1020,7 @@ class RakuAST::VarDeclaration::Simple
if $sigil eq '$' && nqp::objprimspec($of) {
# Natively typed; just declare it.
QAST::Var.new(
:scope('lexical'), :decl('var'), :name(self.name),
:scope($!is-rw ?? 'lexicalref' !! 'lexical'), :decl('var'), :name(self.name),
:returns($of)
)
}
Expand Down Expand Up @@ -1212,10 +1218,11 @@ class RakuAST::VarDeclaration::Simple

method IMPL-BIND-QAST(RakuAST::IMPL::QASTContext $context, QAST::Node $source-qast) {
my str $scope := self.scope;
my $native := nqp::objprimspec(self.IMPL-OF-TYPE);
nqp::die('Can only compile bind to my-scoped variables') unless $scope eq 'my' || $scope eq 'state';
QAST::Op.new(
:op('bind'),
QAST::Var.new( :name(self.name), :scope('lexical') ),
QAST::Var.new( :name(self.name), :scope($native && $!is-rw ?? 'lexicalref' !! 'lexical') ),
$source-qast
)
}
Expand Down

0 comments on commit b213dba

Please sign in to comment.