Skip to content

Commit

Permalink
Use shortcuts for $target.HOW
Browse files Browse the repository at this point in the history
So we don't need to do that over and over again
  • Loading branch information
lizmat committed Feb 27, 2024
1 parent 68cd850 commit fd936ef
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
13 changes: 8 additions & 5 deletions src/Perl6/Metamodel/RoleToClassApplier.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ my class RoleToClassApplier {
}

method prepare($target, @roles) {
my $HOW := $target.HOW;

$!target := $target;
@!roles := @roles;

# If we have many things to compose, then get them into a single helper
# role first.
if +@roles == 1 {
Expand All @@ -43,7 +46,7 @@ my class RoleToClassApplier {
else {
$!to_compose := $concrete.new_type();
$!to_compose_meta := $!to_compose.HOW;
$!to_compose_meta.set_language_revision($!to_compose, $target.HOW.language_revision);
$!to_compose_meta.set_language_revision($!to_compose, $HOW.language_revision);
for @roles {
$!to_compose_meta.add_role($!to_compose, $_);
}
Expand All @@ -58,7 +61,7 @@ my class RoleToClassApplier {
Perl6::Metamodel::Configuration.throw_or_die(
'X::Role::Unresolved::Private',
"Private method '" ~ $_.name
~ "' must be resolved by class " ~ $target.HOW.name($target)
~ "' must be resolved by class " ~ $HOW.name($target)
~ " because it exists in multiple roles (" ~ nqp::join(", ", $_.roles) ~ ")",
:method($_),
:$target,
Expand All @@ -67,7 +70,7 @@ my class RoleToClassApplier {
}
elsif nqp::isconcrete($_.multi) {
my $match := 0;
for $target.HOW.multi_methods_to_incorporate($target) -> $maybe {
for $HOW.multi_methods_to_incorporate($target) -> $maybe {
if $_.name eq $maybe.name &&
Perl6::Metamodel::Configuration.compare_multi_sigs($_.multi, $maybe.code) {
$match := 1;
Expand All @@ -78,7 +81,7 @@ my class RoleToClassApplier {
Perl6::Metamodel::Configuration.throw_or_die(
'X::Role::Unresolved::Multi',
"Multi method '" ~ $_.name ~ "' with signature "
~ $_.multi.signature.raku ~ " must be resolved by class " ~ $target.HOW.name($target)
~ $_.multi.signature.raku ~ " must be resolved by class " ~ $HOW.name($target)
~ " because it exists in multiple roles (" ~ nqp::join(", ", $_.roles) ~ ")",
:method($_),
:$target
Expand All @@ -90,7 +93,7 @@ my class RoleToClassApplier {
Perl6::Metamodel::Configuration.throw_or_die(
'X::Role::Unresolved::Method',
"Method '" ~ $_.name
~ "' must be resolved by class " ~ $target.HOW.name($target)
~ "' must be resolved by class " ~ $HOW.name($target)
~ " because it exists in multiple roles (" ~ nqp::join(", ", $_.roles) ~ ")",
:method($_),
:$target
Expand Down
42 changes: 22 additions & 20 deletions src/Perl6/Metamodel/RoleToRoleApplier.nqp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
my class RoleToRoleApplier {
method apply($target, @roles) {
my $HOW := $target.HOW;

# Ensure we actually have something to appply.
unless +@roles {
return [];
Expand All @@ -13,7 +15,7 @@ my class RoleToRoleApplier {
my %priv_meth_info;
my @priv_meth_names;
my %priv_meth_providers;
my $with_submethods := $target.HOW.language_revision < 3; # less than 6.e
my $with_submethods := $HOW.language_revision < 3; # less than 6.e
my $submethod_type := Perl6::Metamodel::Configuration.submethod_type;
for @roles {
my $role := $_;
Expand Down Expand Up @@ -68,7 +70,7 @@ my class RoleToRoleApplier {
}

# Also need methods of target.
my %target_meth_info := nqp::hllize($target.HOW.method_table($target));
my %target_meth_info := nqp::hllize($HOW.method_table($target));

# Process method list.
for @meth_names -> $name {
Expand All @@ -79,7 +81,7 @@ my class RoleToRoleApplier {
unless nqp::existskey(%target_meth_info, $name) {
# No methods in the target role. If only one, it's easy...
if +@add_meths == 1 {
$target.HOW.add_method($target, $name, @add_meths[0]);
$HOW.add_method($target, $name, @add_meths[0]);
}
else {
# Find if any of the methods are actually requirements, not
Expand All @@ -97,26 +99,26 @@ my class RoleToRoleApplier {
# If we got down to just one, add it. If they were all requirements,
# just choose one.
if +@impl_meths == 1 {
$target.HOW.add_method($target, $name, @impl_meths[0]);
$HOW.add_method($target, $name, @impl_meths[0]);
}
elsif +@impl_meths == 0 {
$target.HOW.add_method($target, $name, @add_meths[0]);
$HOW.add_method($target, $name, @add_meths[0]);
}
else {
$target.HOW.add_collision($target, $name, %meth_providers{$name});
$HOW.add_collision($target, $name, %meth_providers{$name});
}
}
}
}

# Process private method list.
if nqp::can($target.HOW, 'private_method_table') {
my %target_priv_meth_info := nqp::hllize($target.HOW.private_method_table($target));
if nqp::can($HOW, 'private_method_table') {
my %target_priv_meth_info := nqp::hllize($HOW.private_method_table($target));
for @priv_meth_names -> $name {
my @add_meths := %priv_meth_info{$name};
unless nqp::existskey(%target_priv_meth_info, $name) {
if +@add_meths == 1 {
$target.HOW.add_private_method($target, $name, @add_meths[0]);
$HOW.add_private_method($target, $name, @add_meths[0]);
}
else {
# Find if any of the methods are actually requirements, not
Expand All @@ -134,14 +136,14 @@ my class RoleToRoleApplier {
# If we got down to just one, add it. If they were all requirements,
# just choose one.
if +@impl_meths == 1 {
$target.HOW.add_private_method($target, $name, @impl_meths[0]);
$HOW.add_private_method($target, $name, @impl_meths[0]);
}
elsif +@impl_meths == 0 {
# any of the method stubs will do
$target.HOW.add_private_method($target, $name, @add_meths[0]);
$HOW.add_private_method($target, $name, @add_meths[0]);
}
else {
$target.HOW.add_collision($target, $name, %priv_meth_providers{$name}, :private(1));
$HOW.add_collision($target, $name, %priv_meth_providers{$name}, :private(1));
}
}
}
Expand Down Expand Up @@ -207,10 +209,10 @@ my class RoleToRoleApplier {
}
}
if @collides {
$target.HOW.add_collision($target, $name, @collides, :multi($c1[1]));
$HOW.add_collision($target, $name, @collides, :multi($c1[1]));
}
else {
$target.HOW.add_multi_method($target, $name, $c1[1]);
$HOW.add_multi_method($target, $name, $c1[1]);
}
}
}
Expand All @@ -220,7 +222,7 @@ my class RoleToRoleApplier {
# class, so we can avoid duplicating that logic here.)
for @multis_required_names -> $name {
for %multis_required_by_name{$name} {
$target.HOW.add_multi_method($target, $name, $_);
$HOW.add_multi_method($target, $name, $_);
}
}

Expand All @@ -237,7 +239,7 @@ my class RoleToRoleApplier {
%cur-attrs{$attr.name} := AttrReg.new(:$attr, :$from);
}

my @cur_attrs := $target.HOW.attributes($target, :local(1));
my @cur_attrs := $HOW.attributes($target, :local(1));
for @cur_attrs {
reg-cur-attr($_, $target);
}
Expand Down Expand Up @@ -278,7 +280,7 @@ my class RoleToRoleApplier {
}

unless $skip {
$target.HOW.add_attribute($target, $add_attr);
$HOW.add_attribute($target, $add_attr);
reg-cur-attr($add_attr, $r);
}
}
Expand All @@ -287,14 +289,14 @@ my class RoleToRoleApplier {
if nqp::can($how, 'parents') {
my @parents := $how.parents($r, :local(1));
for @parents -> $p {
$target.HOW.add_parent($target, $p, :hides($how.hides_parent($r, $p)));
$HOW.add_parent($target, $p, :hides($how.hides_parent($r, $p)));
}
}

if nqp::can($target.HOW, 'is_array_type') && !$target.HOW.is_array_type {
if nqp::can($HOW, 'is_array_type') && !$HOW.is_array_type {
if nqp::can($how, 'is_array_type') {
if $how.is_array_type {
$target.HOW.set_array_type($target, $how.array_type);
$HOW.set_array_type($target, $how.array_type);
}
}
}
Expand Down

0 comments on commit fd936ef

Please sign in to comment.