| 1 | # B::DeparseTree::P528.pm | ||
| 2 | # Copyright (c) 2018 Rocky Bernstein | ||
| 3 | # All rights reserved. | ||
| 4 | # This module is free software; you can redistribute and/or modify | ||
| 5 | # it under the same terms as Perl itself. | ||
| 6 | |||
| 7 | use rlib '../..'; | ||
| 8 | |||
| 9 | package B::DeparseTree::P528; | ||
| 10 | use B::DeparseTree::P526; | ||
| 11 | |||
| 12 | use strict; | ||
| 13 | use warnings (); | ||
| 14 | |||
| 15 | our(@EXPORT, @ISA); | ||
| 16 | our $VERSION = '3.2.0'; | ||
| 17 | |||
| 18 | # Is the same as P522. Note however | ||
| 19 | # we import from B::Deparse and there are differences | ||
| 20 | # in those routines between 5.22 and 5.24 | ||
| 21 | @ISA = qw(B::DeparseTree::P526); | ||
| 22 | |||
| 23 | sub maybe_var_attr { | ||
| 24 | my ($self, $op, $cx) = @_; | ||
| 25 | |||
| 26 | my @other_ops = ($op->first); | ||
| 27 | my $kid = $op->first->sibling; # skip pushmark | ||
| 28 | return if class($kid) eq 'NULL'; | ||
| 29 | |||
| 30 | Carp::confess("Can't handle var attr yet"); | ||
| 31 | |||
| 32 | my $lop; | ||
| 33 | my $type; | ||
| 34 | |||
| 35 | # Extract out all the pad ops and entersub ops into | ||
| 36 | # @padops and @entersubops. Return if anything else seen. | ||
| 37 | # Also determine what class (if any) all the pad vars belong to | ||
| 38 | my $class; | ||
| 39 | my $decl; # 'my' or 'state' | ||
| 40 | my (@padops, @entersubops); | ||
| 41 | for ($lop = $kid; !B::Deparse::null($lop); $lop = $lop->sibling) { | ||
| 42 | my $lopname = $lop->name; | ||
| 43 | my $loppriv = $lop->private; | ||
| 44 | if ($lopname =~ /^pad[sah]v$/) { | ||
| 45 | return unless $loppriv & OPpLVAL_INTRO; | ||
| 46 | |||
| 47 | my $padname = $self->padname_sv($lop->targ); | ||
| 48 | my $thisclass = ($padname->FLAGS & SVpad_TYPED) | ||
| 49 | ? $padname->SvSTASH->NAME : 'main'; | ||
| 50 | |||
| 51 | # all pad vars must be in the same class | ||
| 52 | $class //= $thisclass; | ||
| 53 | return unless $thisclass eq $class; | ||
| 54 | |||
| 55 | # all pad vars must be the same sort of declaration | ||
| 56 | # (all my, all state, etc) | ||
| 57 | my $this = ($loppriv & OPpPAD_STATE) ? 'state' : 'my'; | ||
| 58 | if (defined $decl) { | ||
| 59 | return unless $this eq $decl; | ||
| 60 | } | ||
| 61 | $decl = $this; | ||
| 62 | |||
| 63 | push @padops, $lop; | ||
| 64 | } | ||
| 65 | elsif ($lopname eq 'entersub') { | ||
| 66 | push @entersubops, $lop; | ||
| 67 | } | ||
| 68 | else { | ||
| 69 | return; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | return unless @padops && @padops == @entersubops; | ||
| 74 | |||
| 75 | # there should be a balance: each padop has a corresponding | ||
| 76 | # 'attributes'->import() method call, in the same order. | ||
| 77 | |||
| 78 | my @varnames; | ||
| 79 | my $attr_text; | ||
| 80 | |||
| 81 | for my $i (0..$#padops) { | ||
| 82 | my $padop = $padops[$i]; | ||
| 83 | my $esop = $entersubops[$i]; | ||
| 84 | |||
| 85 | push @varnames, $self->padname($padop->targ); | ||
| 86 | |||
| 87 | return unless ($esop->flags & OPf_KIDS); | ||
| 88 | |||
| 89 | my $kid = $esop->first; | ||
| 90 | return unless $kid->type == OP_PUSHMARK; | ||
| 91 | |||
| 92 | $kid = $kid->sibling; | ||
| 93 | return unless $$kid && $kid->type == OP_CONST; | ||
| 94 | return unless $self->const_sv($kid)->PV eq 'attributes'; | ||
| 95 | |||
| 96 | $kid = $kid->sibling; | ||
| 97 | return unless $$kid && $kid->type == OP_CONST; # __PACKAGE__ | ||
| 98 | |||
| 99 | $kid = $kid->sibling; | ||
| 100 | return unless $$kid | ||
| 101 | && $kid->name eq "srefgen" | ||
| 102 | && ($kid->flags & OPf_KIDS) | ||
| 103 | && ($kid->first->flags & OPf_KIDS) | ||
| 104 | && $kid->first->first->name =~ /^pad[sah]v$/ | ||
| 105 | && $kid->first->first->targ == $padop->targ; | ||
| 106 | |||
| 107 | $kid = $kid->sibling; | ||
| 108 | my @attr; | ||
| 109 | while ($$kid) { | ||
| 110 | last if ($kid->type != OP_CONST); | ||
| 111 | push @attr, $self->const_sv($kid)->PV; | ||
| 112 | $kid = $kid->sibling; | ||
| 113 | } | ||
| 114 | return unless @attr; | ||
| 115 | my $thisattr = ":" . join(' ', @attr); | ||
| 116 | $attr_text //= $thisattr; | ||
| 117 | # all import calls must have the same list of attributes | ||
| 118 | return unless $attr_text eq $thisattr; | ||
| 119 | |||
| 120 | return unless $kid->name eq 'method_named'; | ||
| 121 | return unless $self->meth_sv($kid)->PV eq 'import'; | ||
| 122 | |||
| 123 | $kid = $kid->sibling; | ||
| 124 | return if $$kid; | ||
| 125 | } | ||
| 126 | |||
| 127 | my $res = $decl; | ||
| 128 | $res .= " $class " if $class ne 'main'; | ||
| 129 | $res .= | ||
| 130 | (@varnames > 1) | ||
| 131 | ? "(" . join(', ', @varnames) . ')' | ||
| 132 | : " $varnames[0]"; | ||
| 133 | |||
| 134 | return "$res $attr_text"; | ||
| 135 | } | ||
| 136 | |||
| 137 | 1; |