| 1 | # B::DeparseTree::P526.pm | ||
| 2 | # Copyright (c) 1998-2000, 2002, 2003, 2004, 2005, 2006 Stephen McCamant. | ||
| 3 | # Copyright (c) 2015, 2017, 2018 Rocky Bernstein | ||
| 4 | # All rights reserved. | ||
| 5 | # This module is free software; you can redistribute and/or modify | ||
| 6 | # it under the same terms as Perl itself. | ||
| 7 | |||
| 8 | # This is based on the module B::Deparse (for perl 5.22) by Stephen McCamant. | ||
| 9 | # It has been extended save tree structure, and is addressible | ||
| 10 | # by opcode address. | ||
| 11 | |||
| 12 | # B::Parse in turn is based on the module of the same name by Malcolm Beattie, | ||
| 13 | # but essentially none of his code remains. | ||
| 14 | |||
| 15 | use v5.26; | ||
| 16 | |||
| 17 | use rlib '../..'; | ||
| 18 | |||
| 19 | package B::DeparseTree::P526; | ||
| 20 | use Carp; | ||
| 21 | |||
| 22 | use B qw( | ||
| 23 | CVf_METHOD | ||
| 24 | MDEREF_ACTION_MASK | ||
| 25 | MDEREF_AV_gvav_aelem | ||
| 26 | MDEREF_AV_gvsv_vivify_rv2av_aelem | ||
| 27 | MDEREF_AV_padav_aelem | ||
| 28 | MDEREF_AV_padsv_vivify_rv2av_aelem | ||
| 29 | MDEREF_AV_pop_rv2av_aelem | ||
| 30 | MDEREF_AV_vivify_rv2av_aelem | ||
| 31 | MDEREF_FLAG_last | ||
| 32 | MDEREF_HV_gvhv_helem | ||
| 33 | MDEREF_HV_gvsv_vivify_rv2hv_helem | ||
| 34 | MDEREF_HV_padhv_helem | ||
| 35 | MDEREF_HV_padsv_vivify_rv2hv_helem | ||
| 36 | MDEREF_HV_pop_rv2hv_helem | ||
| 37 | MDEREF_HV_vivify_rv2hv_helem | ||
| 38 | MDEREF_INDEX_MASK | ||
| 39 | MDEREF_INDEX_const | ||
| 40 | MDEREF_INDEX_gvsv | ||
| 41 | MDEREF_INDEX_none | ||
| 42 | MDEREF_INDEX_padsv | ||
| 43 | MDEREF_MASK | ||
| 44 | MDEREF_SHIFT | ||
| 45 | MDEREF_reload | ||
| 46 | OPf_KIDS | ||
| 47 | OPf_MOD | ||
| 48 | OPf_PARENS | ||
| 49 | OPf_REF | ||
| 50 | OPf_SPECIAL | ||
| 51 | OPf_STACKED | ||
| 52 | OPf_WANT | ||
| 53 | OPf_WANT_LIST | ||
| 54 | OPf_WANT_SCALAR | ||
| 55 | OPf_WANT_VOID | ||
| 56 | OPpCONST_BARE | ||
| 57 | OPpENTERSUB_AMPER | ||
| 58 | OPpEXISTS_SUB | ||
| 59 | OPpLVAL_INTRO | ||
| 60 | OPpMULTIDEREF_DELETE | ||
| 61 | OPpMULTIDEREF_EXISTS | ||
| 62 | OPpOUR_INTRO | ||
| 63 | OPpPADRANGE_COUNTSHIFT | ||
| 64 | OPpSLICE | ||
| 65 | OPpSORT_INTEGER | ||
| 66 | OPpSORT_NUMERIC | ||
| 67 | OPpSORT_REVERSE | ||
| 68 | OPpSPLIT_ASSIGN OPpSPLIT_LEX | ||
| 69 | OPpTARGET_MY | ||
| 70 | PADNAMEt_OUTER | ||
| 71 | PMf_CONTINUE | ||
| 72 | PMf_EVAL | ||
| 73 | PMf_EXTENDED | ||
| 74 | PMf_EXTENDED_MORE | ||
| 75 | PMf_FOLD | ||
| 76 | PMf_GLOBAL | ||
| 77 | PMf_KEEP | ||
| 78 | PMf_MULTILINE | ||
| 79 | PMf_ONCE | ||
| 80 | PMf_SINGLELINE | ||
| 81 | SVf_FAKE | ||
| 82 | SVf_ROK SVpad_OUR | ||
| 83 | SVpad_TYPED | ||
| 84 | SVs_RMG | ||
| 85 | SVs_SMG | ||
| 86 | class | ||
| 87 | main_cv | ||
| 88 | main_root | ||
| 89 | main_start | ||
| 90 | opnumber | ||
| 91 | perlstring | ||
| 92 | svref_2object | ||
| 93 | ); | ||
| 94 | |||
| 95 | use B::DeparseTree::PPfns; | ||
| 96 | use B::DeparseTree::SyntaxTree; | ||
| 97 | use B::DeparseTree::PP; | ||
| 98 | use B::Deparse; | ||
| 99 | |||
| 100 | # Copy unchanged functions from B::Deparse | ||
| 101 | *begin_is_use = *B::Deparse::begin_is_use; | ||
| 102 | *const_sv = *B::Deparse::const_sv; | ||
| 103 | *escape_re = *B::Deparse::escape_re; | ||
| 104 | *find_scope_st = *B::Deparse::find_scope_st; | ||
| 105 | *gv_name = *B::Deparse::gv_name; | ||
| 106 | *keyword = *B::Deparse::keyword; | ||
| 107 | *meth_pad_subs = *B::Deparse::pad_subs; | ||
| 108 | *meth_rclass_sv = *B::Deparse::meth_rclass_sv; | ||
| 109 | *meth_sv = *B::Deparse::meth_sv; | ||
| 110 | *padany = *B::Deparse::padany; | ||
| 111 | *padname = *B::Deparse::padname; | ||
| 112 | *padname_sv = *B::Deparse::padname_sv; | ||
| 113 | *padval = *B::Deparse::padval; | ||
| 114 | *re_flags = *B::Deparse::re_flags; | ||
| 115 | *stash_variable = *B::Deparse::stash_variable; | ||
| 116 | *stash_variable_name = *B::Deparse::stash_variable_name; | ||
| 117 | *tr_chr = *B::Deparse::tr_chr; | ||
| 118 | |||
| 119 | use strict; | ||
| 120 | use vars qw/$AUTOLOAD/; | ||
| 121 | use warnings (); | ||
| 122 | require feature; | ||
| 123 | |||
| 124 | our(@EXPORT, @ISA); | ||
| 125 | our $VERSION = '3.2.0'; | ||
| 126 | |||
| 127 | @ISA = qw(B::DeparseTree::PP); | ||
| 128 | |||
| 129 | @EXPORT = qw(slice); | ||
| 130 | |||
| 131 | BEGIN { | ||
| 132 | # List version-specific constants here. | ||
| 133 | # Easiest way to keep this code portable between version looks to | ||
| 134 | # be to fake up a dummy constant that will never actually be true. | ||
| 135 | foreach (qw(OPpSORT_INPLACE OPpSORT_DESCEND OPpITER_REVERSED OPpCONST_NOVER | ||
| 136 | OPpPAD_STATE PMf_SKIPWHITE RXf_SKIPWHITE | ||
| 137 | PMf_CHARSET PMf_KEEPCOPY PMf_NOCAPTURE CVf_ANONCONST | ||
| 138 | CVf_LOCKED OPpREVERSE_INPLACE OPpSUBSTR_REPL_FIRST | ||
| 139 | PMf_NONDESTRUCT OPpCONST_ARYBASE OPpEVAL_BYTES | ||
| 140 | OPpLVREF_TYPE OPpLVREF_SV OPpLVREF_AV OPpLVREF_HV | ||
| 141 | OPpLVREF_CV OPpLVREF_ELEM SVpad_STATE)) { | ||
| 142 | eval { B->import($_) }; | ||
| 143 | no strict 'refs'; | ||
| 144 | *{$_} = sub () {0} unless *{$_}{CODE}; | ||
| 145 | } | ||
| 146 | } | ||
| 147 | |||
| 148 | BEGIN { for (qw[ rv2sv aelem | ||
| 149 | rv2av rv2hv helem custom ]) { | ||
| 150 | eval "sub OP_\U$_ () { " . opnumber($_) . "}" | ||
| 151 | }} | ||
| 152 | |||
| 153 | # pp_padany -- does not exist after parsing | ||
| 154 | |||
| 155 | sub AUTOLOAD { | ||
| 156 | if ($AUTOLOAD =~ s/^.*::pp_//) { | ||
| 157 | warn "unexpected OP_".uc $AUTOLOAD; | ||
| 158 | ($_[1]->type == OP_CUSTOM ? "CUSTOM ($AUTOLOAD)" : uc $AUTOLOAD); | ||
| 159 | return "XXX"; | ||
| 160 | } else { | ||
| 161 | Carp::confess "Undefined subroutine $AUTOLOAD called"; | ||
| 162 | } | ||
| 163 | } | ||
| 164 | |||
| 165 | sub DESTROY {} # Do not AUTOLOAD | ||
| 166 | |||
| 167 | # The BEGIN {} is used here because otherwise this code isn't executed | ||
| 168 | # when you run B::Deparse on itself. | ||
| 169 | my %globalnames; | ||
| 170 | BEGIN { map($globalnames{$_}++, "SIG", "STDIN", "STDOUT", "STDERR", "INC", | ||
| 171 | "ENV", "ARGV", "ARGVOUT", "_"); } | ||
| 172 | |||
| 173 | { no strict 'refs'; *{"pp_r$_"} = *{"pp_$_"} for qw< keys each values >; } | ||
| 174 | |||
| 175 | # FIXME: | ||
| 176 | # Different in 5.20. Go over differences to see if okay in 5.20. | ||
| 177 | sub pp_chdir { | ||
| 178 | my ($self, $op, $cx) = @_; | ||
| 179 | if (($op->flags & (OPf_SPECIAL|OPf_KIDS)) == (OPf_SPECIAL|OPf_KIDS)) { | ||
| 180 | my $kw = $self->keyword("chdir"); | ||
| 181 | my $kid = $self->const_sv($op->first)->PV; | ||
| 182 | my $code = $kw | ||
| 183 | . ($cx >= 16 || $self->{'parens'} ? "($kid)" : " $kid"); | ||
| 184 | maybe_targmy(@_, sub { $_[3] }, $code); | ||
| 185 | } else { | ||
| 186 | maybe_targmy(@_, \&unop, "chdir") | ||
| 187 | } | ||
| 188 | } | ||
| 189 | |||
| 190 | my @threadsv_names = B::threadsv_names; | ||
| 191 | sub pp_threadsv { | ||
| 192 | my $self = shift; | ||
| 193 | my($op, $cx) = @_; | ||
| 194 | return $self->maybe_local_str($op, $cx, "\$" . $threadsv_names[$op->targ]); | ||
| 195 | } | ||
| 196 | |||
| 197 | sub pp_rv2sv { maybe_local_str(@_, rv2x(@_, "\$")) } | ||
| 198 | sub pp_rv2hv { maybe_local_str(@_, rv2x(@_, "%")) } | ||
| 199 | sub pp_rv2gv { maybe_local_str(@_, rv2x(@_, "*")) } | ||
| 200 | |||
| 201 | # skip rv2av | ||
| 202 | sub pp_av2arylen { | ||
| 203 | my $self = shift; | ||
| 204 | my($op, $cx) = @_; | ||
| 205 | if ($op->first->name eq "padav") { | ||
| 206 | return $self->maybe_local_str($op, $cx, '$#' . $self->padany($op->first)); | ||
| 207 | } else { | ||
| 208 | return $self->maybe_local_str($op, $cx, | ||
| 209 | $self->rv2x($op->first, $cx, '$#')); | ||
| 210 | } | ||
| 211 | } | ||
| 212 | |||
| 213 | sub pp_rv2av { | ||
| 214 | my $self = shift; | ||
| 215 | my($op, $cx) = @_; | ||
| 216 | my $kid = $op->first; | ||
| 217 | if ($kid->name eq "const") { # constant list | ||
| 218 | my $av = $self->const_sv($kid); | ||
| 219 | return $self->list_const($kid, $cx, $av->ARRAY); | ||
| 220 | } else { | ||
| 221 | return $self->maybe_local_str($op, $cx, | ||
| 222 | $self->rv2x($op, $cx, "\@")); | ||
| 223 | } | ||
| 224 | } | ||
| 225 | |||
| 226 | sub elem_or_slice_array_name | ||
| 227 | { | ||
| 228 | my $self = shift; | ||
| 229 | my ($array, $left, $padname, $allow_arrow) = @_; | ||
| 230 | |||
| 231 | if ($array->name eq $padname) { | ||
| 232 | return $self->padany($array); | ||
| 233 | } elsif (B::Deparse::is_scope($array)) { # ${expr}[0] | ||
| 234 | return "{" . $self->deparse($array, 0) . "}"; | ||
| 235 | } elsif ($array->name eq "gv") { | ||
| 236 | ($array, my $quoted) = | ||
| 237 | $self->stash_variable_name( | ||
| 238 | $left eq '[' ? '@' : '%', $self->gv_or_padgv($array) | ||
| 239 | ); | ||
| 240 | if (!$allow_arrow && $quoted) { | ||
| 241 | # This cannot happen. | ||
| 242 | die "Invalid variable name $array for slice"; | ||
| 243 | } | ||
| 244 | return $quoted ? "$array->" : $array; | ||
| 245 | } elsif (!$allow_arrow || B::Deparse::is_scalar $array) { | ||
| 246 | # $x[0], $$x[0], ... | ||
| 247 | return $self->deparse($array, 24)->{text}; | ||
| 248 | } else { | ||
| 249 | return undef; | ||
| 250 | } | ||
| 251 | } | ||
| 252 | |||
| 253 | sub elem_or_slice_single_index($$) | ||
| 254 | { | ||
| 255 | my ($self, $idx, $parent) = @_; | ||
| 256 | |||
| 257 | my $idx_info = $self->deparse($idx, 1, $parent); | ||
| 258 | my $idx_str = $idx_info->{text}; | ||
| 259 | |||
| 260 | # Outer parens in an array index will confuse perl | ||
| 261 | # if we're interpolating in a regular expression, i.e. | ||
| 262 | # /$x$foo[(-1)]/ is *not* the same as /$x$foo[-1]/ | ||
| 263 | # | ||
| 264 | # If $self->{parens}, then an initial '(' will | ||
| 265 | # definitely be paired with a final ')'. If | ||
| 266 | # !$self->{parens}, the misleading parens won't | ||
| 267 | # have been added in the first place. | ||
| 268 | # | ||
| 269 | # [You might think that we could get "(...)...(...)" | ||
| 270 | # where the initial and final parens do not match | ||
| 271 | # each other. But we can't, because the above would | ||
| 272 | # only happen if there's an infix binop between the | ||
| 273 | # two pairs of parens, and *that* means that the whole | ||
| 274 | # expression would be parenthesized as well.] | ||
| 275 | # | ||
| 276 | $idx_str =~ s/^\((.*)\)$/$1/ if $self->{'parens'}; | ||
| 277 | |||
| 278 | # Hash-element braces will autoquote a bareword inside themselves. | ||
| 279 | # We need to make sure that C<$hash{warn()}> doesn't come out as | ||
| 280 | # C<$hash{warn}>, which has a quite different meaning. Currently | ||
| 281 | # B::Deparse will always quote strings, even if the string was a | ||
| 282 | # bareword in the original (i.e. the OPpCONST_BARE flag is ignored | ||
| 283 | # for constant strings.) So we can cheat slightly here - if we see | ||
| 284 | # a bareword, we know that it is supposed to be a function call. | ||
| 285 | # | ||
| 286 | $idx_str =~ s/^([A-Za-z_]\w*)$/$1()/; | ||
| 287 | |||
| 288 | return info_from_text($idx_info->{op}, $self, $idx_str, | ||
| 289 | 'elem_or_slice_single_index', | ||
| 290 | {body => [$idx_info]}); | ||
| 291 | } | ||
| 292 | |||
| 293 | sub elem | ||
| 294 | { | ||
| 295 | my ($self, $op, $cx, $left, $right, $padname) = @_; | ||
| 296 | my($array, $idx) = ($op->first, $op->first->sibling); | ||
| 297 | |||
| 298 | my $idx_info = $self->elem_or_slice_single_index($idx, $op); | ||
| 299 | my $opts = {body => [$idx_info]}; | ||
| 300 | |||
| 301 | unless ($array->name eq $padname) { # Maybe this has been fixed | ||
| 302 | $opts->{other_ops} = [$array]; | ||
| 303 | $array = $array->first; # skip rv2av (or ex-rv2av in _53+) | ||
| 304 | } | ||
| 305 | my @texts = (); | ||
| 306 | my $info; | ||
| 307 | my $array_name=$self->elem_or_slice_array_name($array, $left, $padname, 1); | ||
| 308 | if ($array_name) { | ||
| 309 | if ($array_name !~ /->\z/) { | ||
| 310 | if ($array_name eq '#') { | ||
| 311 | $array_name = '${#}'; | ||
| 312 | } else { | ||
| 313 | $array_name = '$' . $array_name ; | ||
| 314 | } | ||
| 315 | } | ||
| 316 | push @texts, $array_name; | ||
| 317 | push @texts, $left if $left; | ||
| 318 | push @texts, $idx_info->{text}, $right; | ||
| 319 | return info_from_list($op, $self, \@texts, '', 'elem', $opts) | ||
| 320 | } else { | ||
| 321 | # $x[20][3]{hi} or expr->[20] | ||
| 322 | my $type; | ||
| 323 | my $array_info = $self->deparse($array, 24, $op); | ||
| 324 | push @{$info->{body}}, $array_info; | ||
| 325 | @texts = ($array_info->{text}); | ||
| 326 | if (B::Deparse::is_subscriptable($array)) { | ||
| 327 | push @texts, $left, $idx_info->{text}, $right; | ||
| 328 | $type = 'elem_no_arrow'; | ||
| 329 | } else { | ||
| 330 | push @texts, '->', $left, $idx_info->{text}, $right; | ||
| 331 | $type = 'elem_arrow'; | ||
| 332 | } | ||
| 333 | return info_from_list($op, $self, \@texts, '', $type, $opts); | ||
| 334 | } | ||
| 335 | Carp::confess("unhandled condition in elem"); | ||
| 336 | } | ||
| 337 | |||
| 338 | # a simplified version of elem_or_slice_array_name() | ||
| 339 | # for the use of pp_multideref | ||
| 340 | |||
| 341 | sub multideref_var_name($$$) | ||
| 342 | { | ||
| 343 | my ($self, $gv, $is_hash) = @_; | ||
| 344 | |||
| 345 | my ($name, $quoted) = | ||
| 346 | $self->stash_variable_name( $is_hash ? '%' : '@', $gv); | ||
| 347 | return $quoted ? "$name->" | ||
| 348 | : $name eq '#' | ||
| 349 | ? '${#}' # avoid ${#}[1] => $#[1] | ||
| 350 | : '$' . $name; | ||
| 351 | } | ||
| 352 | |||
| 353 | sub pp_multideref | ||
| 354 | { | ||
| 355 | my($self, $op, $cx) = @_; | ||
| 356 | my @texts = (); | ||
| 357 | |||
| 358 | if ($op->private & OPpMULTIDEREF_EXISTS) { | ||
| 359 | @texts = ($self->keyword("exists"), ' '); | ||
| 360 | } | ||
| 361 | elsif ($op->private & OPpMULTIDEREF_DELETE) { | ||
| 362 | @texts = ($self->keyword("delete"), ' ') | ||
| 363 | } | ||
| 364 | elsif ($op->private & OPpLVAL_INTRO) { | ||
| 365 | @texts = ($self->keyword("local"), ' ') | ||
| 366 | } | ||
| 367 | |||
| 368 | if ($op->first && ($op->first->flags & OPf_KIDS)) { | ||
| 369 | # arbitrary initial expression, e.g. f(1,2,3)->[...] | ||
| 370 | my $first = $self->deparse($op->first, 24, $op); | ||
| 371 | push @texts, $first->{text}; | ||
| 372 | } | ||
| 373 | |||
| 374 | my @items = $op->aux_list($self->{curcv}); | ||
| 375 | my $actions = shift @items; | ||
| 376 | |||
| 377 | my $is_hash; | ||
| 378 | my $derefs = 0; | ||
| 379 | |||
| 380 | while (1) { | ||
| 381 | if (($actions & MDEREF_ACTION_MASK) == MDEREF_reload) { | ||
| 382 | $actions = shift @items; | ||
| 383 | next; | ||
| 384 | } | ||
| 385 | |||
| 386 | $is_hash = ( | ||
| 387 | ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_pop_rv2hv_helem | ||
| 388 | || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_gvsv_vivify_rv2hv_helem | ||
| 389 | || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_padsv_vivify_rv2hv_helem | ||
| 390 | || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_vivify_rv2hv_helem | ||
| 391 | || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_padhv_helem | ||
| 392 | || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_gvhv_helem | ||
| 393 | ); | ||
| 394 | |||
| 395 | if ( ($actions & MDEREF_ACTION_MASK) == MDEREF_AV_padav_aelem | ||
| 396 | || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_padhv_helem) | ||
| 397 | { | ||
| 398 | $derefs = 1; | ||
| 399 | push @texts, '$' . substr($self->padname(shift @items), 1); | ||
| 400 | } | ||
| 401 | elsif ( ($actions & MDEREF_ACTION_MASK) == MDEREF_AV_gvav_aelem | ||
| 402 | || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_gvhv_helem) | ||
| 403 | { | ||
| 404 | $derefs = 1; | ||
| 405 | push @texts, $self->multideref_var_name(shift @items, $is_hash); | ||
| 406 | } | ||
| 407 | else { | ||
| 408 | if ( ($actions & MDEREF_ACTION_MASK) == | ||
| 409 | MDEREF_AV_padsv_vivify_rv2av_aelem | ||
| 410 | || ($actions & MDEREF_ACTION_MASK) == | ||
| 411 | MDEREF_HV_padsv_vivify_rv2hv_helem) | ||
| 412 | { | ||
| 413 | push @texts, $self->padname(shift @items); | ||
| 414 | } | ||
| 415 | elsif ( ($actions & MDEREF_ACTION_MASK) == | ||
| 416 | MDEREF_AV_gvsv_vivify_rv2av_aelem | ||
| 417 | || ($actions & MDEREF_ACTION_MASK) == | ||
| 418 | MDEREF_HV_gvsv_vivify_rv2hv_helem) | ||
| 419 | { | ||
| 420 | push @texts, $self->multideref_var_name(shift @items, $is_hash); | ||
| 421 | } | ||
| 422 | elsif ( ($actions & MDEREF_ACTION_MASK) == | ||
| 423 | MDEREF_AV_pop_rv2av_aelem | ||
| 424 | || ($actions & MDEREF_ACTION_MASK) == | ||
| 425 | MDEREF_HV_pop_rv2hv_helem) | ||
| 426 | { | ||
| 427 | if ( ($op->flags & OPf_KIDS) | ||
| 428 | && ( B::Deparse::_op_is_or_was($op->first, OP_RV2AV) | ||
| 429 | || B::Deparse::_op_is_or_was($op->first, OP_RV2HV)) | ||
| 430 | && ($op->first->flags & OPf_KIDS) | ||
| 431 | && ( B::Deparse::_op_is_or_was($op->first->first, OP_AELEM) | ||
| 432 | || B::Deparse::_op_is_or_was($op->first->first, OP_HELEM)) | ||
| 433 | ) | ||
| 434 | { | ||
| 435 | $derefs++; | ||
| 436 | } | ||
| 437 | } | ||
| 438 | |||
| 439 | push(@texts, '->') if !$derefs++; | ||
| 440 | } | ||
| 441 | |||
| 442 | |||
| 443 | if (($actions & MDEREF_INDEX_MASK) == MDEREF_INDEX_none) { | ||
| 444 | last; | ||
| 445 | } | ||
| 446 | |||
| 447 | push(@texts, $is_hash ? '{' : '['); | ||
| 448 | |||
| 449 | if (($actions & MDEREF_INDEX_MASK) == MDEREF_INDEX_const) { | ||
| 450 | my $key = shift @items; | ||
| 451 | if ($is_hash) { | ||
| 452 | push @texts, $self->const($key, $cx)->{text}; | ||
| 453 | } | ||
| 454 | else { | ||
| 455 | push @texts, $key; | ||
| 456 | } | ||
| 457 | } | ||
| 458 | elsif (($actions & MDEREF_INDEX_MASK) == MDEREF_INDEX_padsv) { | ||
| 459 | push @texts, $self->padname(shift @items); | ||
| 460 | } | ||
| 461 | elsif (($actions & MDEREF_INDEX_MASK) == MDEREF_INDEX_gvsv) { | ||
| 462 | push @texts,('$' . ($self->stash_variable_name('$', shift @items))[0]); | ||
| 463 | } | ||
| 464 | |||
| 465 | push(@texts, $is_hash ? '}' : ']'); | ||
| 466 | |||
| 467 | if ($actions & MDEREF_FLAG_last) { | ||
| 468 | last; | ||
| 469 | } | ||
| 470 | $actions >>= MDEREF_SHIFT; | ||
| 471 | } | ||
| 472 | |||
| 473 | return info_from_list($op, $self, \@texts, '', 'multideref', {}); | ||
| 474 | } | ||
| 475 | |||
| 476 | # returns "&" and the argument bodies if the prototype doesn't match the args, | ||
| 477 | # or ("", $args_after_prototype_demunging) if it does. | ||
| 478 | sub check_proto { | ||
| 479 | my $self = shift; | ||
| 480 | my $op = shift; | ||
| 481 | return ('&', []) if $self->{'noproto'}; | ||
| 482 | my($proto, @args) = @_; | ||
| 483 | my($arg, $real); | ||
| 484 | my $doneok = 0; | ||
| 485 | my @reals; | ||
| 486 | # An unbackslashed @ or % gobbles up the rest of the args | ||
| 487 | 1 while $proto =~ s/(?<!\\)([@%])[^\]]+$/$1/; | ||
| 488 | $proto =~ s/^\s*//; | ||
| 489 | while ($proto) { | ||
| 490 | $proto =~ s/^(\\?[\$\@&%*_]|\\\[[\$\@&%*]+\]|;)\s*//; | ||
| 491 | my $chr = $1; | ||
| 492 | if ($chr eq "") { | ||
| 493 | return ('&', []) if @args; | ||
| 494 | } elsif ($chr eq ";") { | ||
| 495 | $doneok = 1; | ||
| 496 | } elsif ($chr eq "@" or $chr eq "%") { | ||
| 497 | push @reals, map($self->deparse($_, 6), @args, $op); | ||
| 498 | @args = (); | ||
| 499 | } else { | ||
| 500 | $arg = shift @args; | ||
| 501 | last unless $arg; | ||
| 502 | if ($chr eq "\$" || $chr eq "_") { | ||
| 503 | if (B::Deparse::want_scalar $arg) { | ||
| 504 | push @reals, $self->deparse($arg, 6, $op); | ||
| 505 | } else { | ||
| 506 | return ('&', []); | ||
| 507 | } | ||
| 508 | } elsif ($chr eq "&") { | ||
| 509 | if ($arg->name =~ /^(s?refgen|undef)$/) { | ||
| 510 | push @reals, $self->deparse($arg, 6, $op); | ||
| 511 | } else { | ||
| 512 | return ('&', []); | ||
| 513 | } | ||
| 514 | } elsif ($chr eq "*") { | ||
| 515 | if ($arg->name =~ /^s?refgen$/ | ||
| 516 | and $arg->first->first->name eq "rv2gv") | ||
| 517 | { | ||
| 518 | $real = $arg->first->first; # skip refgen, null | ||
| 519 | if ($real->first->name eq "gv") { | ||
| 520 | push @reals, $self->deparse($real, 6, $op); | ||
| 521 | } else { | ||
| 522 | push @reals, $self->deparse($real->first, 6, $op); | ||
| 523 | } | ||
| 524 | } else { | ||
| 525 | return ('&', []); | ||
| 526 | } | ||
| 527 | } elsif (substr($chr, 0, 1) eq "\\") { | ||
| 528 | $chr =~ tr/\\[]//d; | ||
| 529 | if ($arg->name =~ /^s?refgen$/ and | ||
| 530 | !B::Deparse::null($real = $arg->first) and | ||
| 531 | ($chr =~ /\$/ && B::Deparse::is_scalar($real->first) | ||
| 532 | or ($chr =~ /@/ | ||
| 533 | && class($real->first->sibling) ne 'NULL' | ||
| 534 | && $real->first->sibling->name | ||
| 535 | =~ /^(rv2|pad)av$/) | ||
| 536 | or ($chr =~ /%/ | ||
| 537 | && class($real->first->sibling) ne 'NULL' | ||
| 538 | && $real->first->sibling->name | ||
| 539 | =~ /^(rv2|pad)hv$/) | ||
| 540 | #or ($chr =~ /&/ # This doesn't work | ||
| 541 | # && $real->first->name eq "rv2cv") | ||
| 542 | or ($chr =~ /\*/ | ||
| 543 | && $real->first->name eq "rv2gv"))) | ||
| 544 | { | ||
| 545 | push @reals, $self->deparse($real, 6, $op); | ||
| 546 | } else { | ||
| 547 | return ('&', []); | ||
| 548 | } | ||
| 549 | } | ||
| 550 | } | ||
| 551 | } | ||
| 552 | return ('&', []) if $proto and !$doneok; # too few args and no ';' | ||
| 553 | return ('&', []) if @args; # too many args | ||
| 554 | return ('', \@reals); | ||
| 555 | } | ||
| 556 | |||
| 557 | # Like dq(), but different | ||
| 558 | sub re_dq { | ||
| 559 | my $self = shift; | ||
| 560 | my ($op) = @_; | ||
| 561 | my ($re_dq_info, $fmt); | ||
| 562 | |||
| 563 | my $type = $op->name; | ||
| 564 | if ($type eq "const") { | ||
| 565 | return '$[' if $op->private & OPpCONST_ARYBASE; | ||
| 566 | my $unbacked = B::Deparse::re_unback($self->const_sv($op)->as_string); | ||
| 567 | return B::Deparse::re_uninterp(escape_re($unbacked)); | ||
| 568 | } elsif ($type eq "concat") { | ||
| 569 | my $first = $self->re_dq($op->first); | ||
| 570 | my $last = $self->re_dq($op->last); | ||
| 571 | return B::Deparse::re_dq_disambiguate($first, $last); | ||
| 572 | } elsif ($type eq "uc") { | ||
| 573 | $re_dq_info = $self->re_dq($op->first->sibling); | ||
| 574 | $fmt = '\U%c\E'; | ||
| 575 | $type .= ' uc'; | ||
| 576 | } elsif ($type eq "lc") { | ||
| 577 | $re_dq_info = $self->re_dq($op->first->sibling); | ||
| 578 | $fmt = '\L%c\E'; | ||
| 579 | $type .= ' lc'; | ||
| 580 | } elsif ($type eq "ucfirst") { | ||
| 581 | $re_dq_info = $self->re_dq($op->first->sibling); | ||
| 582 | $fmt = '\u%c'; | ||
| 583 | $type .= ' ucfirst'; | ||
| 584 | } elsif ($type eq "lcfirst") { | ||
| 585 | $re_dq_info = $self->re_dq($op->first->sibling); | ||
| 586 | $fmt = '\u%c'; | ||
| 587 | $type .= ' lcfirst'; | ||
| 588 | } elsif ($type eq "quotemeta") { | ||
| 589 | $re_dq_info = $self->re_dq($op->first->sibling); | ||
| 590 | $fmt = '\Q%c\E'; | ||
| 591 | $type .= ' quotemeta'; | ||
| 592 | } elsif ($type eq "fc") { | ||
| 593 | $re_dq_info = $self->re_dq($op->first->sibling); | ||
| 594 | $fmt = '\F%c\E'; | ||
| 595 | $type .= ' fc'; | ||
| 596 | } elsif ($type eq "join") { | ||
| 597 | return $self->deparse($op->last, 26); # was join($", @ary) | ||
| 598 | } else { | ||
| 599 | my $ret = $self->deparse($op, 26); | ||
| 600 | $ret =~ s/^\$([(|)])\z/\${$1}/ # $( $| $) need braces | ||
| 601 | or $ret =~ s/^\@([-+])\z/\@{$1}/; # @- @+ need braces | ||
| 602 | return $ret; | ||
| 603 | } | ||
| 604 | return $self->info_from_template($type, $op->first->sibling, | ||
| 605 | $fmt, [$re_dq_info], [0]); | ||
| 606 | } | ||
| 607 | |||
| 608 | sub pure_string { | ||
| 609 | my ($self, $op) = @_; | ||
| 610 | return 0 if B::Deparse::null $op; | ||
| 611 | my $type = $op->name; | ||
| 612 | |||
| 613 | if ($type eq 'const' || $type eq 'av2arylen') { | ||
| 614 | return 1; | ||
| 615 | } | ||
| 616 | elsif ($type =~ /^(?:[ul]c(first)?|fc)$/ || $type eq 'quotemeta') { | ||
| 617 | return $self->pure_string($op->first->sibling); | ||
| 618 | } | ||
| 619 | elsif ($type eq 'join') { | ||
| 620 | my $join_op = $op->first->sibling; # Skip pushmark | ||
| 621 | return 0 unless $join_op->name eq 'null' && $join_op->targ == OP_RV2SV; | ||
| 622 | |||
| 623 | my $gvop = $join_op->first; | ||
| 624 | return 0 unless $gvop->name eq 'gvsv'; | ||
| 625 | return 0 unless '"' eq $self->gv_name($self->gv_or_padgv($gvop)); | ||
| 626 | |||
| 627 | return 0 unless ${$join_op->sibling} eq ${$op->last}; | ||
| 628 | return 0 unless $op->last->name =~ /^(?:[ah]slice|(?:rv2|pad)av)$/; | ||
| 629 | } | ||
| 630 | elsif ($type eq 'concat') { | ||
| 631 | return $self->pure_string($op->first) | ||
| 632 | && $self->pure_string($op->last); | ||
| 633 | } | ||
| 634 | elsif (B::Deparse::is_scalar($op) || $type =~ /^[ah]elem$/) { | ||
| 635 | return 1; | ||
| 636 | } | ||
| 637 | elsif ($type eq "null" and $op->can('first') and not B::Deparse::null $op->first and | ||
| 638 | ($op->first->name eq "null" and $op->first->can('first') | ||
| 639 | and not B::Deparse::null $op->first->first and | ||
| 640 | $op->first->first->name eq "aelemfast" | ||
| 641 | or | ||
| 642 | $op->first->name =~ /^aelemfast(?:_lex)?\z/ | ||
| 643 | )) { | ||
| 644 | return 1; | ||
| 645 | } | ||
| 646 | else { | ||
| 647 | return 0; | ||
| 648 | } | ||
| 649 | |||
| 650 | return 1; | ||
| 651 | } | ||
| 652 | |||
| 653 | sub regcomp | ||
| 654 | { | ||
| 655 | my($self, $op, $cx, $extended) = @_; | ||
| 656 | my @other_ops = (); | ||
| 657 | my $kid = $op->first; | ||
| 658 | if ($kid->name eq "regcmaybe") { | ||
| 659 | push @other_ops, $kid; | ||
| 660 | $kid = $kid->first; | ||
| 661 | } | ||
| 662 | if ($kid->name eq "regcreset") { | ||
| 663 | push @other_ops, $kid; | ||
| 664 | $kid = $kid->first; | ||
| 665 | } | ||
| 666 | if ($kid->name eq "null" and !B::Deparse::null($kid->first) | ||
| 667 | and $kid->first->name eq 'pushmark') { | ||
| 668 | my $str = ''; | ||
| 669 | push(@other_ops, $kid); | ||
| 670 | $kid = $kid->first->sibling; | ||
| 671 | my @body = (); | ||
| 672 | while (!B::Deparse::null($kid)) { | ||
| 673 | my $first = $str; | ||
| 674 | my $last = $self->re_dq($kid, $extended); | ||
| 675 | push @body, $last; | ||
| 676 | push(@other_ops, $kid); | ||
| 677 | $str = B::Deparse::re_dq_disambiguate($first, | ||
| 678 | $self->info2str($last)); | ||
| 679 | $kid = $kid->sibling; | ||
| 680 | } | ||
| 681 | return (info_from_text($op, $self, $str, 'regcomp', | ||
| 682 | {other_ops => \@other_ops, | ||
| 683 | body => \@body}), 1); | ||
| 684 | } | ||
| 685 | |||
| 686 | if ($self->pure_string($kid)) { | ||
| 687 | my $info = $self->re_dq($kid, $extended); | ||
| 688 | my @kid_ops = $info->{other_ops} ? @{$info->{other_ops}} : (); | ||
| 689 | push @other_ops, @kid_ops; | ||
| 690 | $info->{other_ops} = \@other_ops; | ||
| 691 | return ($info, 1); | ||
| 692 | } | ||
| 693 | return ($self->deparse($kid, $cx, $op), 0, $op); | ||
| 694 | } | ||
| 695 | |||
| 696 | sub pp_split | ||
| 697 | { | ||
| 698 | my($self, $op, $cx) = @_; | ||
| 699 | my($kid, @exprs, $ary_info, $expr); | ||
| 700 | my $stacked = $op->flags & OPf_STACKED; | ||
| 701 | my $ary = ''; | ||
| 702 | my @body = (); | ||
| 703 | my @other_ops = (); | ||
| 704 | $kid = $op->first; | ||
| 705 | |||
| 706 | $kid = $op->first; | ||
| 707 | $kid = $kid->sibling if $kid->name eq 'regcomp'; | ||
| 708 | for (; !B::Deparse::null($kid); $kid = $kid->sibling) { | ||
| 709 | push @exprs, $self->deparse($kid, 6, $op); | ||
| 710 | } | ||
| 711 | |||
| 712 | unshift @exprs, $self->matchop($op, $cx, "m", "/"); | ||
| 713 | |||
| 714 | if ($op->private & OPpSPLIT_ASSIGN) { | ||
| 715 | # With C<@array = split(/pat/, str);>, | ||
| 716 | # array is stored in split's pmreplroot; either | ||
| 717 | # as an integer index into the pad (for a lexical array) | ||
| 718 | # or as GV for a package array (which will be a pad index | ||
| 719 | # on threaded builds) | ||
| 720 | # With my/our @array = split(/pat/, str), the array is instead | ||
| 721 | # accessed via an extra padav/rv2av op at the end of the | ||
| 722 | # split's kid ops. | ||
| 723 | |||
| 724 | if ($stacked) { | ||
| 725 | $ary = pop @exprs; | ||
| 726 | } | ||
| 727 | else { | ||
| 728 | if ($op->private & OPpSPLIT_LEX) { | ||
| 729 | $ary = $self->padname($op->pmreplroot); | ||
| 730 | } | ||
| 731 | else { | ||
| 732 | # union with op_pmtargetoff, op_pmtargetgv | ||
| 733 | my $gv = $op->pmreplroot; | ||
| 734 | $gv = $self->padval($gv) if !ref($gv); | ||
| 735 | $ary = $self->maybe_local(@_, | ||
| 736 | $self->stash_variable('@', | ||
| 737 | $self->gv_name($gv), | ||
| 738 | $cx)) | ||
| 739 | } | ||
| 740 | if ($op->private & OPpLVAL_INTRO) { | ||
| 741 | $ary = $op->private & OPpSPLIT_LEX ? "my $ary" : "local $ary"; | ||
| 742 | } | ||
| 743 | } | ||
| 744 | } | ||
| 745 | |||
| 746 | push @body, @exprs; | ||
| 747 | my $opts = {body => \@exprs}; | ||
| 748 | |||
| 749 | # handle special case of split(), and split(' ') that compiles to /\s+/ | ||
| 750 | if (($op->reflags // 0) & RXf_SKIPWHITE()) { | ||
| 751 | my $expr0 = $exprs[0]; | ||
| 752 | my $expr0b0 = $expr0->{body}[0]; | ||
| 753 | my $bsep = $expr0b0->{sep}; | ||
| 754 | my $sep = $expr0->{sep}; | ||
| 755 | $expr0b0->{texts}[1] = ' '; | ||
| 756 | # substr($expr0b0->{text}, 1, 0) = ' '; | ||
| 757 | substr($expr0->{texts}[0], 1, 0) = ' '; | ||
| 758 | substr($expr0->{text}, 1, 0) = ' '; | ||
| 759 | } | ||
| 760 | my @args_texts = map $_->{text}, @exprs; | ||
| 761 | |||
| 762 | my $sep = ''; | ||
| 763 | my $type; | ||
| 764 | my @expr_texts; | ||
| 765 | if ($ary) { | ||
| 766 | @expr_texts = ("$ary", '=', join(', ', @args_texts)); | ||
| 767 | $sep = ' '; | ||
| 768 | $type = 'split_array'; | ||
| 769 | $opts->{maybe_parens} = [$self, $cx, 7]; | ||
| 770 | } else { | ||
| 771 | @expr_texts = ('split', '(', join(', ', @args_texts), ')'); | ||
| 772 | $type = 'split'; | ||
| 773 | |||
| 774 | } | ||
| 775 | return info_from_list($op, $self, \@expr_texts, $sep, $type, $opts); | ||
| 776 | } | ||
| 777 | |||
| 778 | # Not in Perl 5.20 and presumably < 5.20. No harm in adding to 5.20? | ||
| 779 | *pp_ncomplement = *pp_complement; | ||
| 780 | |||
| 781 | 1; |