Skip to content

Commit

Permalink
Streamline some other places with .DEFINITE§
Browse files Browse the repository at this point in the history
Replace .DEFINITE by nqp::isconcrete wherever possible: that is, when
the value was only needed in a condition and not to be returned as a HLL
Bool.

Note there are still a lot of places with .DEFINITE, but they need some
more careful inspection.
  • Loading branch information
lizmat committed Mar 23, 2018
1 parent 6a66b35 commit c2eae41
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 58 deletions.
37 changes: 17 additions & 20 deletions src/core/Hash.pm6
Expand Up @@ -99,7 +99,7 @@ my class Hash { # declared in BOOTSTRAP

multi method ASSIGN-KEY(Hash:D: Str:D \key, Mu \assignval) is raw {
nqp::if(
nqp::getattr(self,Map,'$!storage').DEFINITE,
nqp::isconcrete(nqp::getattr(self,Map,'$!storage')),
(nqp::ifnull(
nqp::atkey(
nqp::getattr(self,Map,'$!storage'),
Expand All @@ -120,7 +120,7 @@ my class Hash { # declared in BOOTSTRAP
}
multi method ASSIGN-KEY(Hash:D: \key, Mu \assignval) is raw {
nqp::if(
nqp::getattr(self,Map,'$!storage').DEFINITE,
nqp::isconcrete(nqp::getattr(self,Map,'$!storage')),
(nqp::ifnull(
nqp::atkey(
nqp::getattr(self,Map,'$!storage'),
Expand Down Expand Up @@ -152,9 +152,9 @@ my class Hash { # declared in BOOTSTRAP
multi method DELETE-KEY(Hash:U: --> Nil) { }
multi method DELETE-KEY(Hash:D: Str:D \key) {
nqp::if(
(nqp::getattr(self,Map,'$!storage').DEFINITE
nqp::isconcrete(nqp::getattr(self,Map,'$!storage'))
&& nqp::existskey(nqp::getattr(self,Map,'$!storage'),
nqp::unbox_s(key))),
nqp::unbox_s(key)),
nqp::stmts(
(my $value = nqp::atkey(nqp::getattr(self,Map,'$!storage'),
nqp::unbox_s(key))),
Expand All @@ -169,8 +169,8 @@ my class Hash { # declared in BOOTSTRAP
nqp::stmts(
(my str $key = nqp::unbox_s(key.Str)),
nqp::if(
(nqp::getattr(self,Map,'$!storage').DEFINITE
&& nqp::existskey(nqp::getattr(self,Map,'$!storage'),$key)),
nqp::isconcrete(nqp::getattr(self,Map,'$!storage'))
&& nqp::existskey(nqp::getattr(self,Map,'$!storage'),$key),
nqp::stmts(
(my $value = nqp::atkey(nqp::getattr(self,Map,'$!storage'),$key)),
nqp::deletekey(nqp::getattr(self,Map,'$!storage'),$key),
Expand Down Expand Up @@ -443,7 +443,7 @@ my class Hash { # declared in BOOTSTRAP
# eventuality, so to appease roast, we need these.
multi method ASSIGN-KEY(::?CLASS:D: Str:D \key, Mu \assignval) is raw {
nqp::if(
nqp::getattr(self,Map,'$!storage').DEFINITE,
nqp::isconcrete(nqp::getattr(self,Map,'$!storage')),
nqp::if(
nqp::existskey(
nqp::getattr(self,Map,'$!storage'),
Expand Down Expand Up @@ -472,7 +472,7 @@ my class Hash { # declared in BOOTSTRAP
nqp::stmts(
(my str $key = nqp::unbox_s(key.Str)),
nqp::if(
nqp::getattr(self,Map,'$!storage').DEFINITE,
nqp::isconcrete(nqp::getattr(self,Map,'$!storage')),
nqp::if(
nqp::existskey(
nqp::getattr(self,Map,'$!storage'),
Expand Down Expand Up @@ -514,7 +514,7 @@ my class Hash { # declared in BOOTSTRAP
method keyof () { TKey }
method AT-KEY(::?CLASS:D: TKey \key) is raw {
nqp::if(
nqp::getattr(self,Map,'$!storage').DEFINITE,
nqp::isconcrete(nqp::getattr(self,Map,'$!storage')),
nqp::if(
nqp::existskey(nqp::getattr(self,Map,'$!storage'),
(my str $which = nqp::unbox_s(key.WHICH))),
Expand All @@ -537,7 +537,7 @@ my class Hash { # declared in BOOTSTRAP
'$!whence',
-> { nqp::bindkey(
nqp::if(
nqp::getattr(self,Map,'$!storage').DEFINITE,
nqp::isconcrete(nqp::getattr(self,Map,'$!storage')),
nqp::getattr(self,Map,'$!storage'),
nqp::bindattr(self,Map,'$!storage',nqp::hash)
),
Expand All @@ -560,7 +560,7 @@ my class Hash { # declared in BOOTSTRAP

method ASSIGN-KEY(::?CLASS:D: TKey \key, TValue \assignval) is raw {
nqp::if(
nqp::getattr(self,Map,'$!storage').DEFINITE,
nqp::isconcrete(nqp::getattr(self,Map,'$!storage')),
nqp::if(
nqp::existskey(nqp::getattr(self,Map,'$!storage'),
my str $which = nqp::unbox_s(key.WHICH)),
Expand All @@ -585,7 +585,7 @@ my class Hash { # declared in BOOTSTRAP
method BIND-KEY(TKey \key, TValue \bindval) is raw {
nqp::getattr(
nqp::if(
nqp::getattr(self,Map,'$!storage').DEFINITE,
nqp::isconcrete(nqp::getattr(self,Map,'$!storage')),
nqp::bindkey(nqp::getattr(self,Map,'$!storage'),
nqp::unbox_s(key.WHICH),
Pair.new(key,bindval)),
Expand All @@ -606,9 +606,9 @@ my class Hash { # declared in BOOTSTRAP

method DELETE-KEY(TKey \key) {
nqp::if(
(nqp::getattr(self,Map,'$!storage').DEFINITE
nqp::isconcrete(nqp::getattr(self,Map,'$!storage'))
&& nqp::existskey(nqp::getattr(self,Map,'$!storage'),
(my str $which = key.WHICH))),
(my str $which = key.WHICH)),
nqp::stmts(
(my TValue $value =
nqp::getattr(
Expand All @@ -625,7 +625,7 @@ my class Hash { # declared in BOOTSTRAP
nqp::stmts(
(my $flattened := nqp::hash),
nqp::if(
(my $raw := nqp::getattr(self,Map,'$!storage'))
nqp::isconcrete(my $raw := nqp::getattr(self,Map,'$!storage'))
&& (my $iter := nqp::iterator($raw)),
nqp::while(
$iter,
Expand Down Expand Up @@ -655,11 +655,8 @@ my class Hash { # declared in BOOTSTRAP
nqp::stmts(
(my $buffer := nqp::create(IterationBuffer)),
nqp::if(
nqp::defined(
nqp::getattr(self,Map,'$!storage')
) && nqp::elems(
nqp::getattr(self,Map,'$!storage')
),
nqp::isconcrete(nqp::getattr(self,Map,'$!storage'))
&& nqp::elems(nqp::getattr(self,Map,'$!storage')),
nqp::stmts(
(my $iterator := nqp::iterator(
nqp::getattr(self,Map,'$!storage')
Expand Down
4 changes: 2 additions & 2 deletions src/core/IO/Handle.pm6
Expand Up @@ -426,14 +426,14 @@ my class IO::Handle {
}
method pull-one() {
nqp::if(
(my $line := $!handle.get).DEFINITE,
nqp::isconcrete(my $line := $!handle.get),
$line,
IterationEnd
)
}
method push-all($target --> IterationEnd) {
nqp::while(
(my $line := $!handle.get).DEFINITE,
nqp::isconcrete(my $line := $!handle.get),
$target.push($line)
)
}
Expand Down
19 changes: 11 additions & 8 deletions src/core/Rakudo/Iterator.pm6
Expand Up @@ -832,13 +832,15 @@ class Rakudo::Iterator {
# something more elaborate
nqp::if(
nqp::istype($elem,List)
&& nqp::not_i(
nqp::getattr($elem,List,'$!todo').DEFINITE),
&& nqp::not_i(nqp::isconcrete(
nqp::getattr($elem,List,'$!todo')
)),

# it's a List, may have a reified we can use directly
nqp::if(
($elem := nqp::getattr($elem,List,'$!reified'))
&& nqp::isgt_i(nqp::elems($elem),0),
nqp::isconcrete(
$elem := nqp::getattr($elem,List,'$!reified')
) && nqp::isgt_i(nqp::elems($elem),0),

# use the available reified directly
nqp::stmts(
Expand Down Expand Up @@ -1103,14 +1105,15 @@ class Rakudo::Iterator {
# something more elaborate
nqp::if(
nqp::istype($elem,List)
&& nqp::not_i(
nqp::getattr($elem,List,'$!todo').DEFINITE),
&& nqp::not_i(nqp::isconcrete(
nqp::getattr($elem,List,'$!todo')
)),

# it's a List, may have a reified we can use directly
nqp::if(
nqp::isnull(
$elem := nqp::getattr($elem,List,'$!reified'))
|| nqp::iseq_i(nqp::elems($elem),0),
$elem := nqp::getattr($elem,List,'$!reified')
) || nqp::iseq_i(nqp::elems($elem),0),

# cross with an empty list is always an empty list
(return Rakudo::Iterator.Empty),
Expand Down
4 changes: 3 additions & 1 deletion src/core/Regex.pm6
Expand Up @@ -80,7 +80,9 @@ my class Regex { # declared in BOOTSTRAP
(my $ctx := nqp::ctx),
nqp::until(
nqp::isnull($ctx := nqp::ctxcallerskipthunks($ctx))
|| (my $underscore := nqp::getlexrelcaller($ctx,'$_')).DEFINITE,
|| nqp::isconcrete(
my $underscore := nqp::getlexrelcaller($ctx,'$_')
),
nqp::null
),
nqp::if(
Expand Down
26 changes: 13 additions & 13 deletions src/core/Seq.pm6
Expand Up @@ -17,13 +17,13 @@ my class Seq is Cool does Iterable does Sequence {

method iterator(Seq:D:) {
nqp::if(
(my \iter = $!iter).DEFINITE,
nqp::isconcrete(my \iter = $!iter),
nqp::stmts(
($!iter := Iterator),
iter
),
nqp::if(
$!list.DEFINITE,
nqp::isconcrete($!list),
$!list.iterator,
X::Seq::Consumed.new.throw
)
Expand All @@ -32,10 +32,10 @@ my class Seq is Cool does Iterable does Sequence {

multi method is-lazy(Seq:D:) {
nqp::if(
$!iter.DEFINITE,
nqp::isconcrete($!iter),
$!iter.is-lazy,
nqp::if(
$!list.DEFINITE,
nqp::isconcrete($!list),
$!list.is-lazy,
X::Seq::Consumed.new.throw
)
Expand All @@ -53,7 +53,7 @@ my class Seq is Cool does Iterable does Sequence {
self.is-lazy,
Failure.new(X::Cannot::Lazy.new(action => '.elems')),
nqp::if(
($!iter.DEFINITE && nqp::can($!iter,'count-only')),
nqp::isconcrete($!iter) && nqp::can($!iter,'count-only'),
$!iter.count-only,
self.cache.elems
)
Expand All @@ -62,23 +62,23 @@ my class Seq is Cool does Iterable does Sequence {

method Numeric() {
nqp::if(
($!iter.DEFINITE && nqp::can($!iter,'count-only')),
nqp::isconcrete($!iter) && nqp::can($!iter,'count-only'),
$!iter.count-only,
self.cache.Numeric
)
}

method Int() {
nqp::if(
($!iter.DEFINITE && nqp::can($!iter,'count-only')),
nqp::isconcrete($!iter) && nqp::can($!iter,'count-only'),
$!iter.count-only,
self.cache.Int
)
}

method Bool(Seq:D:) {
nqp::if(
$!iter.DEFINITE,
nqp::isconcrete($!iter),
nqp::if(
nqp::can($!iter,'bool-only'),
$!iter.bool-only,
Expand Down Expand Up @@ -134,13 +134,13 @@ my class Seq is Cool does Iterable does Sequence {

method sink(--> Nil) {
nqp::if(
$!iter.DEFINITE,
nqp::isconcrete($!iter),
nqp::stmts(
$!iter.sink-all,
($!iter := Iterator)
),
nqp::if(
$!list.DEFINITE,
nqp::isconcrete($!list),
$!list.sink
)
)
Expand Down Expand Up @@ -219,13 +219,13 @@ sub GATHER(&block) {
result,
nqp::stmts(
nqp::unless(
$!push-target.DEFINITE,
nqp::isconcrete($!push-target),
($!push-target := nqp::create(IterationBuffer))
),
($!wanted = 1),
nqp::continuationreset(PROMPT, &!resumption),
nqp::if(
&!resumption.DEFINITE,
nqp::isconcrete(&!resumption),
nqp::shift($!push-target),
IterationEnd
)
Expand All @@ -251,7 +251,7 @@ sub GATHER(&block) {
nqp::continuationreset(PROMPT, &!resumption),
($!push-target := nqp::null),
nqp::if(
&!resumption.DEFINITE,
nqp::isconcrete(&!resumption),
($n - $!wanted),
IterationEnd
)
Expand Down
8 changes: 5 additions & 3 deletions src/core/Sequence.pm6
Expand Up @@ -22,9 +22,11 @@ my role PositionalBindFailover {
has $!list;

method cache() {
$!list.DEFINITE
?? $!list
!! ($!list := List.from-iterator(self.iterator))
nqp::if(
nqp::isconcrete($!list),
$!list,
($!list := List.from-iterator(self.iterator))
)
}
multi method list(::?CLASS:D:) {
List.from-iterator(self.iterator)
Expand Down
4 changes: 2 additions & 2 deletions src/core/Slip.pm6
Expand Up @@ -22,12 +22,12 @@ my class Slip { # is List
nqp::stmts(
(my $list := nqp::create(List)),
nqp::if(
nqp::getattr(self,List,'$!todo').DEFINITE,
nqp::isconcrete(nqp::getattr(self,List,'$!todo')),
nqp::bindattr($list,List,'$!todo',
nqp::getattr(self,List,'$!todo'))
),
nqp::if(
nqp::getattr(self,List,'$!reified').DEFINITE,
nqp::isconcrete(nqp::getattr(self,List,'$!reified')),
nqp::bindattr($list,List,'$!reified',
nqp::getattr(self,List,'$!reified'))
),
Expand Down
16 changes: 8 additions & 8 deletions src/core/TypedArray.pm6
Expand Up @@ -68,11 +68,11 @@
:what($*INDEX // 'Index'),:got($pos),:range<0..^Inf>)),
nqp::stmts(
nqp::if(
nqp::getattr(self,List,'$!reified').DEFINITE,
nqp::isconcrete(nqp::getattr(self,List,'$!reified')),
nqp::if(
(nqp::isge_i(
$pos,nqp::elems(nqp::getattr(self,List,'$!reified')))
&& nqp::getattr(self,List,'$!todo').DEFINITE),
nqp::isge_i(
$pos,nqp::elems(nqp::getattr(self,List,'$!reified'))
) && nqp::isconcrete(nqp::getattr(self,List,'$!todo')),
nqp::getattr(self,List,'$!todo').reify-at-least(
nqp::add_i($pos,1)),
),
Expand All @@ -90,11 +90,11 @@
:what($*INDEX // 'Index'),:got($pos),:range<0..^Inf>)),
nqp::stmts(
nqp::if(
nqp::getattr(self,List,'$!reified').DEFINITE,
nqp::isconcrete(nqp::getattr(self,List,'$!reified')),
nqp::if(
(nqp::isge_i(
$pos,nqp::elems(nqp::getattr(self,List,'$!reified')))
&& nqp::getattr(self,List,'$!todo').DEFINITE),
nqp::isge_i(
$pos,nqp::elems(nqp::getattr(self,List,'$!reified'))
) && nqp::isconcrete(nqp::getattr(self,List,'$!todo')),
nqp::getattr(self,List,'$!todo').reify-at-least(
nqp::add_i($pos,1)),
),
Expand Down
2 changes: 1 addition & 1 deletion src/core/control.pm6
Expand Up @@ -200,7 +200,7 @@ constant NaN = nqp::p6box_n(nqp::nan());
# method, because then the return value is always HLLized :-(
sub CLONE-HASH-DECONTAINERIZED(\hash) {
nqp::if(
nqp::getattr(hash,Map,'$!storage').DEFINITE,
nqp::isconcrete(nqp::getattr(hash,Map,'$!storage')),
nqp::stmts(
(my $clone := nqp::hash),
(my $iter := nqp::iterator(nqp::getattr(hash,Map,'$!storage'))),
Expand Down

0 comments on commit c2eae41

Please sign in to comment.