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