Skip to content

Commit

Permalink
Eliminate resolve-with
Browse files Browse the repository at this point in the history
Resolutions are now performed at the appropriate phase (parse, begin, or
check).
  • Loading branch information
jnthn committed Jun 5, 2023
1 parent f7501f0 commit cc3aa43
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 120 deletions.
1 change: 0 additions & 1 deletion src/Raku/Actions.nqp
Expand Up @@ -1629,7 +1629,6 @@ class Raku::Actions is HLL::Actions does Raku::CommonActions {
}
else {
$*PACKAGE := my $package := self.r('Package').new: :$declarator, :$how, :$name, :$scope;
$package.resolve-with($*R);
}
self.set-declarand($/, $*PACKAGE);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Raku/Grammar.nqp
Expand Up @@ -458,7 +458,7 @@ role Raku::Common {
method check_variable($var) {
my $ast := $var.ast;
if nqp::eqaddr($ast.WHAT,self.actions.r('Var', 'Lexical').WHAT) {
$ast.resolve-with($*R);
$ast.ensure-parse-performed($*R, $*CU.context);
unless $ast.is-resolved {
if $ast.sigil eq '&' {
if $ast.IMPL-IS-META-OP {
Expand Down
17 changes: 3 additions & 14 deletions src/Raku/ast/base.rakumod
Expand Up @@ -119,11 +119,6 @@ class RakuAST::Node {
$is-parse-time := 0;
}

# TODO Move all resolve-with into parse time or begin time
if nqp::istype(self, RakuAST::Lookup) && !self.is-resolved {
self.resolve-with($resolver);
}

# Visit children.
my int $is-package := nqp::istype(self, RakuAST::Package);
$resolver.push-scope(self) if $is-scope;
Expand Down Expand Up @@ -161,15 +156,6 @@ class RakuAST::Node {
$resolver.pop-scope() if $is-scope;
$resolver.pop-package() if $is-package;

# Do resolution.
# TODO eliminate in favor of it happening at explicit parse/begin/check times
if nqp::istype(self, RakuAST::Lookup) && !self.is-resolved {
self.resolve-with($resolver);
if !$resolve-only && !self.is-resolved && self.needs-resolution {
$resolver.add-node-unresolved-after-check-time(self);
}
}

# Unless in resolve-only mode, do other check-time activities.
# TODO eliminate resolve-only, since that's just check time.
unless $resolve-only {
Expand All @@ -183,6 +169,9 @@ class RakuAST::Node {
$resolver.add-node-with-check-time-problems(self);
}
}
if nqp::istype(self, RakuAST::Lookup) && !self.is-resolved && self.needs-resolution {
$resolver.add-node-unresolved-after-check-time(self);
}
}

Nil
Expand Down
9 changes: 6 additions & 3 deletions src/Raku/ast/call.rakumod
Expand Up @@ -196,6 +196,7 @@ class RakuAST::Call::Name
is RakuAST::Term
is RakuAST::Call
is RakuAST::Lookup
is RakuAST::CheckTime
{
has RakuAST::Name $.name;
has Mu $!package;
Expand All @@ -214,7 +215,7 @@ class RakuAST::Call::Name

method needs-resolution() { $!name.is-identifier }

method resolve-with(RakuAST::Resolver $resolver) {
method PERFORM-CHECK(RakuAST::Resolver $resolver, RakuAST::IMPL::QASTContext $context) {
nqp::bindattr(self, RakuAST::Call::Name, '$!package', $resolver.current-package);
my $resolved := $resolver.resolve-name($!name, :sigil('&'));
if $resolved {
Expand Down Expand Up @@ -514,6 +515,7 @@ class RakuAST::Call::PrivateMethod
is RakuAST::Call::Methodish
is RakuAST::Lookup
is RakuAST::ImplicitLookups
is RakuAST::ParseTime
{
has RakuAST::Name $.name;
has Mu $!package;
Expand All @@ -532,7 +534,7 @@ class RakuAST::Call::PrivateMethod

method needs-resolution() { False }

method resolve-with(RakuAST::Resolver $resolver) {
method PERFORM-PARSE(RakuAST::Resolver $resolver, RakuAST::IMPL::QASTContext $context) {
nqp::bindattr(self, RakuAST::Call::PrivateMethod, '$!package', $resolver.current-package);
Nil
}
Expand Down Expand Up @@ -620,6 +622,7 @@ class RakuAST::Call::MaybeMethod
class RakuAST::Call::VarMethod
is RakuAST::Call::Methodish
is RakuAST::Lookup
is RakuAST::CheckTime
{
has RakuAST::Name $.name;

Expand All @@ -639,7 +642,7 @@ class RakuAST::Call::VarMethod

method needs-resolution() { $!name.is-identifier }

method resolve-with(RakuAST::Resolver $resolver) {
method PERFORM-CHECK(RakuAST::Resolver $resolver, RakuAST::IMPL::QASTContext $context) {
my $resolved := $resolver.resolve-name($!name, :sigil('&'));
if $resolved {
self.set-resolution($resolved);
Expand Down
6 changes: 4 additions & 2 deletions src/Raku/ast/circumfix.rakumod
Expand Up @@ -36,6 +36,7 @@ class RakuAST::Circumfix::Parentheses
class RakuAST::Circumfix::ArrayComposer
is RakuAST::Circumfix
is RakuAST::Lookup
is RakuAST::CheckTime
is RakuAST::ColonPairish
{
has RakuAST::SemiList $.semilist;
Expand All @@ -62,7 +63,7 @@ class RakuAST::Circumfix::ArrayComposer
}
}

method resolve-with(RakuAST::Resolver $resolver) {
method PERFORM-CHECK(RakuAST::Resolver $resolver, RakuAST::IMPL::QASTContext $context) {
my $resolved := $resolver.resolve-lexical('&circumfix:<[ ]>');
if $resolved {
self.set-resolution($resolved);
Expand Down Expand Up @@ -102,6 +103,7 @@ class RakuAST::Circumfix::ArrayComposer
class RakuAST::Circumfix::HashComposer
is RakuAST::Circumfix
is RakuAST::Lookup
is RakuAST::CheckTime
{
has RakuAST::Expression $.expression;

Expand All @@ -117,7 +119,7 @@ class RakuAST::Circumfix::HashComposer
Nil
}

method resolve-with(RakuAST::Resolver $resolver) {
method PERFORM-CHECK(RakuAST::Resolver $resolver, RakuAST::IMPL::QASTContext $context) {
my $resolved := $resolver.resolve-lexical('&circumfix:<{ }>');
if $resolved {
self.set-resolution($resolved);
Expand Down
1 change: 0 additions & 1 deletion src/Raku/ast/code.rakumod
Expand Up @@ -1765,7 +1765,6 @@ class RakuAST::Methodish
$signature.set-is-on-method(True);
$signature.set-is-on-named-method(True) if self.name;
$signature.set-is-on-meta-method(True) if nqp::can(self, 'meta') && self.meta;
$signature.attach($resolver);
$signature.IMPL-ENSURE-IMPLICITS;
}

Expand Down
29 changes: 18 additions & 11 deletions src/Raku/ast/expressions.rakumod
Expand Up @@ -147,6 +147,7 @@ class RakuAST::Infixish
class RakuAST::Infix
is RakuAST::Infixish
is RakuAST::Lookup
is RakuAST::ParseTime
{
has str $.operator;
has OperatorProperties $.properties;
Expand All @@ -169,7 +170,7 @@ class RakuAST::Infix
])
}

method resolve-with(RakuAST::Resolver $resolver) {
method PERFORM-PARSE(RakuAST::Resolver $resolver, RakuAST::IMPL::QASTContext $context) {
my $resolved := $resolver.resolve-infix($!operator);
if $resolved {
self.set-resolution($resolved);
Expand Down Expand Up @@ -959,6 +960,7 @@ class RakuAST::Prefixish
class RakuAST::Prefix
is RakuAST::Prefixish
is RakuAST::Lookup
is RakuAST::ParseTime
{
has str $.operator;

Expand All @@ -968,7 +970,7 @@ class RakuAST::Prefix
$obj
}

method resolve-with(RakuAST::Resolver $resolver) {
method PERFORM-PARSE(RakuAST::Resolver $resolver, RakuAST::IMPL::QASTContext $context) {
my $resolved := $resolver.resolve-prefix($!operator);
if $resolved {
self.set-resolution($resolved);
Expand Down Expand Up @@ -1032,9 +1034,9 @@ class RakuAST::ApplyPrefix
method PERFORM-BEGIN(RakuAST::Resolver $resolver, RakuAST::IMPL::QASTContext $context) {
if nqp::bitand_i($!prefix.IMPL-CURRIES, 1) {
if nqp::istype($!operand, RakuAST::Term::Whatever) {
nqp::bindattr(self, RakuAST::ApplyPrefix, '$!operand', RakuAST::Var::Lexical.new('$_'));
nqp::bindattr(self, RakuAST::ApplyPrefix, '$!operand',
RakuAST::Var::Lexical.new('$_').to-begin-time($resolver, $context));
self.IMPL-CURRY($resolver, $context, '$_');
$!operand.resolve-with($resolver);
}
}
if nqp::bitand_i($!prefix.IMPL-CURRIES, 2) {
Expand Down Expand Up @@ -1092,6 +1094,7 @@ class RakuAST::Postfixish
class RakuAST::Postfix
is RakuAST::Postfixish
is RakuAST::Lookup
is RakuAST::ParseTime
{
has str $.operator;

Expand All @@ -1102,7 +1105,7 @@ class RakuAST::Postfix
$obj
}

method resolve-with(RakuAST::Resolver $resolver) {
method PERFORM-PARSE(RakuAST::Resolver $resolver, RakuAST::IMPL::QASTContext $context) {
my $resolved := $resolver.resolve-postfix($!operator);
if $resolved {
self.set-resolution($resolved);
Expand Down Expand Up @@ -1131,6 +1134,7 @@ class RakuAST::Postfix
class RakuAST::Postfix::Power
is RakuAST::Postfixish
is RakuAST::Lookup
is RakuAST::ParseTime
{
has Int $.power;

Expand All @@ -1141,7 +1145,7 @@ class RakuAST::Postfix::Power
$obj
}

method resolve-with(RakuAST::Resolver $resolver) {
method PERFORM-PARSE(RakuAST::Resolver $resolver, RakuAST::IMPL::QASTContext $context) {
my $resolved := $resolver.resolve-postfix('');
if $resolved {
self.set-resolution($resolved);
Expand Down Expand Up @@ -1172,6 +1176,7 @@ class RakuAST::Postcircumfix
class RakuAST::Postcircumfix::ArrayIndex
is RakuAST::Postcircumfix
is RakuAST::Lookup
is RakuAST::ParseTime
{
has RakuAST::SemiList $.index;
has RakuAST::Expression $.assignee;
Expand All @@ -1192,7 +1197,7 @@ class RakuAST::Postcircumfix::ArrayIndex
True
}

method resolve-with(RakuAST::Resolver $resolver) {
method PERFORM-PARSE(RakuAST::Resolver $resolver, RakuAST::IMPL::QASTContext $context) {
my $resolved := $resolver.resolve-lexical(
nqp::elems($!index.code-statements) > 1
?? '&postcircumfix:<[; ]>'
Expand Down Expand Up @@ -1245,6 +1250,7 @@ class RakuAST::Postcircumfix::ArrayIndex
class RakuAST::Postcircumfix::HashIndex
is RakuAST::Postcircumfix
is RakuAST::Lookup
is RakuAST::ParseTime
{
has RakuAST::SemiList $.index;

Expand All @@ -1259,7 +1265,7 @@ class RakuAST::Postcircumfix::HashIndex
True
}

method resolve-with(RakuAST::Resolver $resolver) {
method PERFORM-PARSE(RakuAST::Resolver $resolver, RakuAST::IMPL::QASTContext $context) {
my $resolved := $resolver.resolve-lexical(
nqp::elems($!index.code-statements) > 1
?? '&postcircumfix:<{; }>'
Expand Down Expand Up @@ -1302,6 +1308,7 @@ class RakuAST::Postcircumfix::HashIndex
class RakuAST::Postcircumfix::LiteralHashIndex
is RakuAST::Postcircumfix
is RakuAST::Lookup
is RakuAST::ParseTime
{
has RakuAST::QuotedString $.index;
has RakuAST::Expression $.assignee;
Expand All @@ -1322,7 +1329,7 @@ class RakuAST::Postcircumfix::LiteralHashIndex
True
}

method resolve-with(RakuAST::Resolver $resolver) {
method PERFORM-PARSE(RakuAST::Resolver $resolver, RakuAST::IMPL::QASTContext $context) {
my $resolved := $resolver.resolve-lexical('&postcircumfix:<{ }>');
if $resolved {
self.set-resolution($resolved);
Expand Down Expand Up @@ -1429,9 +1436,9 @@ class RakuAST::ApplyPostfix
method PERFORM-BEGIN(RakuAST::Resolver $resolver, RakuAST::IMPL::QASTContext $context) {
if nqp::bitand_i($!postfix.IMPL-CURRIES, 1) {
if nqp::istype($!operand, RakuAST::Term::Whatever) {
nqp::bindattr(self, RakuAST::ApplyPostfix, '$!operand', RakuAST::Var::Lexical.new('$_'));
nqp::bindattr(self, RakuAST::ApplyPostfix, '$!operand',
RakuAST::Var::Lexical.new('$_').to-begin-time($resolver, $context));
self.IMPL-CURRY($resolver, $context, '$_');
$!operand.resolve-with($resolver);
}
}
if nqp::bitand_i($!postfix.IMPL-CURRIES, 2) {
Expand Down
45 changes: 17 additions & 28 deletions src/Raku/ast/package.rakumod
Expand Up @@ -102,22 +102,6 @@ class RakuAST::Package
nqp::bindattr(self, RakuAST::Package, '$!is-stub', $is-stub ?? True !! False);
}

method resolve-with(RakuAST::Resolver $resolver) {
if $!name {
my $resolved := $resolver.resolve-name-constant($!name);
if $resolved {
my $meta-object := $resolved.compile-time-value;
if $meta-object.HOW.HOW.name($meta-object.HOW) ne 'Perl6::Metamodel::PackageHOW'
&& nqp::can($meta-object.HOW, 'is_composed')
&& !$meta-object.HOW.is_composed($meta-object)
{
self.set-resolution($resolved);
}
}
}
Nil
}

method default-scope() { 'our' }

method default-how() {
Expand Down Expand Up @@ -178,10 +162,23 @@ class RakuAST::Package
}

method PERFORM-PARSE(RakuAST::Resolver $resolver, RakuAST::IMPL::QASTContext $context) {
# Note that this early return is actually not effective as the begin handler will
# already be run when the parser enters the package and we only know that it's a
# stub when we are done parsing the body.
return Nil if $!is-stub;
if $!name {
my $resolved := $resolver.resolve-name-constant($!name);
if $resolved {
my $meta-object := $resolved.compile-time-value;
if $meta-object.HOW.HOW.name($meta-object.HOW) ne 'Perl6::Metamodel::PackageHOW'
&& nqp::can($meta-object.HOW, 'is_composed')
&& !$meta-object.HOW.is_composed($meta-object)
{
self.set-resolution($resolved);
}
}
}

# Note that this early return is actually not effective as the begin handler will
# already be run when the parser enters the package and we only know that it's a
# stub when we are done parsing the body.
return Nil if $!is-stub;

# Install the symbol.
my str $scope := self.scope;
Expand Down Expand Up @@ -432,14 +429,6 @@ class RakuAST::Package
class RakuAST::Package::Augmented
is RakuAST::Package
{
method resolve-with(RakuAST::Resolver $resolver) {
my $resolved := $resolver.resolve-name(self.name);
if $resolved {
self.set-resolution($resolved);
}
Nil
}

method PRODUCE-STUBBED-META-OBJECT() {
self.resolution.compile-time-value
}
Expand Down
4 changes: 0 additions & 4 deletions src/Raku/ast/scoping.rakumod
Expand Up @@ -721,10 +721,6 @@ class RakuAST::ImplicitLookups
method implicit-lookups-to-begin-time(RakuAST::Resolver $resolver, RakuAST::IMPL::QASTContext $context) {
for self.IMPL-UNWRAP-LIST(self.get-implicit-lookups()) {
$_.to-begin-time($resolver, $context);
# TODO Eliminate when resolve-with is gone
if nqp::can($_, 'resolve-with') {
$_.resolve-with($resolver);
}
}
}
}
Expand Down

0 comments on commit cc3aa43

Please sign in to comment.