Skip to content

Commit

Permalink
Mark all potentially useless arguments
Browse files Browse the repository at this point in the history
- if a method accepts a single positional argument and it is **NOT**
  being used inside the method, rename it to $XXX and make it optional
- if a method accepts multiple positional arguments and the first one
  is not being used, rename it to $XXX
- if the first positional argument *is* used in the method, rename
  it to $target and change all references to that as well.  It may
  well be that it turns out that the argument *is* useless in the
  end.  By renaming it, it is at least clear that we've looked at
  it.
  • Loading branch information
lizmat committed Feb 27, 2024
1 parent cf4babd commit 4f64b48
Show file tree
Hide file tree
Showing 33 changed files with 499 additions and 595 deletions.
2 changes: 1 addition & 1 deletion src/Perl6/Metamodel/ArrayType.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ role Perl6::Metamodel::ArrayType {
method array_type($XXX?) { $!array_type }
method is_array_type($XXX?) { $!is_array_type }

method set_array_type($obj, $type) {
method set_array_type($XXX, $type) {
$!array_type := $type;
$!is_array_type := 1;
}
Expand Down
38 changes: 17 additions & 21 deletions src/Perl6/Metamodel/AttributeContainer.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ role Perl6::Metamodel::AttributeContainer {
has $!attr_rw_by_default;

# Adds an attribute.
method add_attribute($obj, $attr) {
method add_attribute($target, $attr) {
my $meta_attr := nqp::decont($attr);
my $name := $meta_attr.name;
if nqp::isnull(%!attribute_lookup) {
@!attributes := nqp::list();
%!attribute_lookup := nqp::hash();
}
if nqp::existskey(%!attribute_lookup, $name) {
nqp::die("Package '" ~ self.name($obj) ~
nqp::die("Package '" ~ self.name($target) ~
"' already has an attribute named '$name'");
}
if $!attr_rw_by_default { $meta_attr.default_to_rw() }
Expand All @@ -24,11 +24,11 @@ role Perl6::Metamodel::AttributeContainer {
}

# Composes all attributes.
method compose_attributes($the-obj, :$compiler_services) {
my $obj := nqp::decont($the-obj);
method compose_attributes($target, :$compiler_services) {
$target := nqp::decont($target);

my %seen_with_accessor;
my %meths := nqp::hllize(self.method_table($obj));
my %meths := nqp::hllize(self.method_table($target));
my %orig_meths;
for %meths {
%orig_meths{$_.key} := 1;
Expand All @@ -44,59 +44,55 @@ role Perl6::Metamodel::AttributeContainer {
# Heuristic to pass along compiler_services only to Perl 6 MOP,
# not to NQP one.
nqp::isconcrete($compiler_services) && nqp::can($_, 'gist')
?? $_.compose($obj, :$compiler_services)
!! $_.compose($obj)
?? $_.compose($target, :$compiler_services)
!! $_.compose($target)
}
}

# Makes setting the type represented by the meta-object rw mean that its
# attributes are rw by default. For cases when status is late set, like
# with 'also is rw', fixup the previously added attributes. Note that we
# can safely use 'default_to_rw' because it would pay respect to `is readonly`
method set_rw($obj) {
method set_rw($XXX?) {
for @!attributes {
$_.default_to_rw();
$_.default_to_rw;
}
$!attr_rw_by_default := 1;
}

# Is this type's attributes rw by default?
method rw($obj) {
$!attr_rw_by_default
}
method rw($XXX?) { $!attr_rw_by_default }

# Gets the attribute meta-object for an attribute if it exists.
# This is called by the parser so it should only return attributes
# that are visible inside the current package.
method get_attribute_for_usage($obj, $name) {
method get_attribute_for_usage($target, $name) {
unless nqp::existskey(%!attribute_lookup, $name) {
nqp::die("No $name attribute in " ~ self.name($obj))
nqp::die("No $name attribute in " ~ self.name($target))
}
%!attribute_lookup{$name}
}

# Returns true if attribute exists locally.
method has_attribute($obj, $name) {
method has_attribute($XXX, $name) {
nqp::existskey(%!attribute_lookup, $name)
}
method has_public_attribute($obj, $name) {
method has_public_attribute($XXX, $name) {
nqp::existskey(%!attribute_lookup, $name) && %!attribute_lookup{$name}.has_accessor
}

method attribute_table($obj) {
%!attribute_lookup
}
method attribute_table($XXX?) { %!attribute_lookup }

# Introspect attributes.
method attributes($obj, :$local, :$excl, :$all) {
method attributes($target, :$local, :$excl, :$all) {
my @attrs;

for @!attributes {
@attrs.push($_);
}

unless $local {
for self.parents($obj, :excl($excl), :all($all)) {
for self.parents($target, :excl($excl), :all($all)) {
for $_.HOW.attributes($_, :local(1)) {
@attrs.push($_);
}
Expand Down
47 changes: 21 additions & 26 deletions src/Perl6/Metamodel/BUILDPLAN.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ role Perl6::Metamodel::BUILDPLAN {
# 1502 die if a required num attribute is 0e0
# 1503 die if a required str attribute is null_s (will be '' in the future)
# 1510 die if a required uint attribute is 0
method create_BUILDPLAN($obj) {
method create_BUILDPLAN($target) {
# First, we'll create the build plan for just this class.
my @plan;
my @attrs := $obj.HOW.attributes($obj, :local(1));
my @attrs := $target.HOW.attributes($target, :local(1));
# When adding role's BUILD/TWEAK into the buildplan for pre-6.e classes only roles of 6.e+ origin must be
# considered.
my $ohow := $obj.HOW;
my $ohow := $target.HOW;
my $only_6e_roles := nqp::can($ohow, 'language_revision')
?? $ohow.language_revision($obj) < 3
?? $ohow.language_revision($target) < 3
!! nqp::can($ohow, 'lang-rev-before')
?? $ohow.lang-rev-before($obj, 'e') # Support legacy approach where implemented
?? $ohow.lang-rev-before($target, 'e') # Support legacy approach where implemented
!! 1; # Assume the HOW being compiled against an older Raku language version

# Emit any container initializers. Also build hash of attrs we
Expand All @@ -71,7 +71,7 @@ role Perl6::Metamodel::BUILDPLAN {
"Defaults on compound attribute types",
workaround =>
"Create/Adapt TWEAK method in class "
~ $obj.HOW.name($obj)
~ $target.HOW.name($target)
~ ", e.g:\n\n method TWEAK() \{\n "
~ $_.name
~ " := (initial values) unless "
Expand All @@ -81,7 +81,7 @@ role Perl6::Metamodel::BUILDPLAN {
}
}

nqp::push(@plan,[900, $obj, $_.name, $ci]);
nqp::push(@plan,[900, $target, $_.name, $ci]);
next;
}
}
Expand All @@ -91,7 +91,7 @@ role Perl6::Metamodel::BUILDPLAN {
}

sub add_from_roles($name) {
my @ins_roles := self.ins_roles($obj, :with-submethods-only);
my @ins_roles := self.ins_roles($target, :with-submethods-only);
my $i := +@ins_roles;
while --$i >= 0 {
my $role := @ins_roles[$i];
Expand All @@ -107,7 +107,7 @@ role Perl6::Metamodel::BUILDPLAN {
add_from_roles('BUILD');

# Does it have its own BUILD?
my $build := $obj.HOW.find_method($obj, 'BUILD', :no_fallback(1));
my $build := $target.HOW.find_method($target, 'BUILD', :no_fallback(1));
if !nqp::isnull($build) && $build {
# We'll call the custom one.
nqp::push(@plan,$build);
Expand All @@ -129,7 +129,7 @@ role Perl6::Metamodel::BUILDPLAN {
?? 0 + $primspec
!! 1300;

my $info := [$action,$obj,$name,nqp::substr($name,2)];
my $info := [$action,$target,$name,nqp::substr($name,2)];

# binding may need type info for runtime checks
if $action == 1300 {
Expand All @@ -153,7 +153,7 @@ role Perl6::Metamodel::BUILDPLAN {
my $type := $_.type;
my int $primspec := nqp::objprimspec($type);
my int $op := $primspec ?? 1500 + $primspec !! 800;
nqp::push(@plan,[$op, $obj, $_.name, $_.required]);
nqp::push(@plan,[$op, $target, $_.name, $_.required]);
nqp::deletekey(%attrs_untouched, $_.name);
}
}
Expand All @@ -174,7 +174,7 @@ role Perl6::Metamodel::BUILDPLAN {
if nqp::isconcrete($default) {
my $name := $_.name;
my $opcode := $primspec || !$_.is_bound ?? 400 + $primspec !! 1400;
my @action := [$opcode, $obj, $name, $default];
my @action := [$opcode, $target, $name, $default];

# binding defaults to additional check at runtime
my $check-at-runtime := $opcode == 1400;
Expand Down Expand Up @@ -239,28 +239,28 @@ role Perl6::Metamodel::BUILDPLAN {
# Add vivify instructions.
for @attrs { # iterate over the array to get a consistent order
if nqp::existskey(%attrs_untouched, $_.name) {
nqp::push(@plan,[1000, $obj, $_.name]);
nqp::push(@plan,[1000, $target, $_.name]);
}
}

add_from_roles('TWEAK');

# Does it have a TWEAK?
my $TWEAK := $obj.HOW.find_method($obj, 'TWEAK', :no_fallback(1));
my $TWEAK := $target.HOW.find_method($target, 'TWEAK', :no_fallback(1));
if !nqp::isnull($TWEAK) && $TWEAK {
nqp::push(@plan,$TWEAK);
}

# Something in the buildplan of this class
if @plan || nqp::elems(self.parents($obj)) > 1 {
if @plan || nqp::elems(self.parents($target)) > 1 {

# Install plan for this class.
@!BUILDPLAN := @plan;

# Now create the full plan by getting the MRO, and working from
# least derived to most derived, copying the plans.
my @all_plan;
my @mro := self.mro($obj);
my @mro := self.mro($target);
my $i := +@mro;
my $noops := 0;
while $i > 0 {
Expand Down Expand Up @@ -289,7 +289,7 @@ role Perl6::Metamodel::BUILDPLAN {
@!BUILDPLAN := @EMPTY;

# Take the first "super"class's BUILDALLPLAN if possible
my @mro := self.mro($obj);
my @mro := self.mro($target);
@!BUILDALLPLAN := +@mro > 1
?? @mro[1].HOW.BUILDALLPLAN(@mro[1])
!! @EMPTY
Expand All @@ -314,24 +314,19 @@ role Perl6::Metamodel::BUILDPLAN {
}
}

method ins_roles($obj, :$with-submethods-only = 0) {
method ins_roles($target, :$with-submethods-only = 0) {
my @ins_roles;
if nqp::can(self, 'concretizations') {
for self.concretizations($obj, :local) {
for self.concretizations($target, :local) {
next if $with-submethods-only && !nqp::can($_.HOW, 'submethod_table');
@ins_roles.push($_);
}
}
@ins_roles
}

method BUILDPLAN($obj) {
@!BUILDPLAN
}

method BUILDALLPLAN($obj) {
@!BUILDALLPLAN
}
method BUILDPLAN( $XXX?) { @!BUILDPLAN }
method BUILDALLPLAN($XXX?) { @!BUILDALLPLAN }
}

# vim: expandtab sw=4
12 changes: 6 additions & 6 deletions src/Perl6/Metamodel/BaseType.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,30 @@ role Perl6::Metamodel::BaseType {
has $!base_type_set;
has @!mro;

method set_base_type($obj, $base_type) {
method set_base_type($target, $base_type) {
if $!base_type_set {
nqp::die("Base type has already been set for " ~ self.name($obj));
nqp::die("Base type has already been set for " ~ self.name($target));
}
$!base_type := $base_type;
$!base_type_set := 1;
}

# Our MRO is just that of base type.
method mro($obj, :$roles = 0, :$concretizations = 0, :$unhidden = 0) {
method mro($target, :$roles = 0, :$concretizations = 0, :$unhidden = 0) {
unless @!mro {
@!mro := nqp::list();
@!mro[0] := $obj;
@!mro[0] := $target;
for $!base_type.HOW.mro($!base_type, :$roles, :$concretizations, :$unhidden) {
@!mro.push($_);
}
}
@!mro
}

method parents($obj, :$local, :$excl, :$all) {
method parents($XXX?, :$local, :$excl, :$all) {
my @parents := [$!base_type];
unless $local {
for $!base_type.HOW.parents($!base_type, :excl($excl), :all($all)) {
for $!base_type.HOW.parents($!base_type, :$excl, :$all) {
@parents.push($_);
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/Perl6/Metamodel/C3MRO.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ role Perl6::Metamodel::C3MRO {
}

# Introspects the Method Resolution Order.
method mro($obj, :$roles = 0, :$concretizations = 0, :$unhidden = 0) {
method mro($target, :$roles = 0, :$concretizations = 0, :$unhidden = 0) {
# Make sure we get a snapshot of MRO hash without competing
# with compute_mro working in another thread. It should be
# safe to pull in just $!mro without cloning it because the
Expand All @@ -250,7 +250,7 @@ role Perl6::Metamodel::C3MRO {
my $mro := $!mro;

# Compute the MRO if there is none yet (???)
$mro := self.compute_mro($obj) if nqp::eqaddr($mro,NQPMu);
$mro := self.compute_mro($target) if nqp::eqaddr($mro,NQPMu);

nqp::atkey(
nqp::atkey($mro, $unhidden ?? 'unhidden' !! 'all'),
Expand All @@ -264,8 +264,8 @@ role Perl6::Metamodel::C3MRO {

# Introspects the Method Resolution Order without anything that has
# been hidden.
method mro_unhidden($obj, :$roles = 0, :$concretizations = 0) {
self.mro($obj, :$roles, :$concretizations, :unhidden)
method mro_unhidden($target, :$roles = 0, :$concretizations = 0) {
self.mro($target, :$roles, :$concretizations, :unhidden)
}
}

Expand Down
Loading

0 comments on commit 4f64b48

Please sign in to comment.