| 1 | # B::DeparseTree::P524.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.24; | ||
| 16 | |||
| 17 | use rlib '../..'; | ||
| 18 | |||
| 19 | package B::DeparseTree::P524; | ||
| 20 | use Carp; | ||
| 21 | use B qw(class opnumber | ||
| 22 | OPf_KIDS OPf_REF OPf_STACKED OPf_SPECIAL OPf_MOD OPf_PARENS | ||
| 23 | OPpLVAL_INTRO OPpOUR_INTRO OPpENTERSUB_AMPER OPpSLICE OPpCONST_BARE | ||
| 24 | OPpMULTIDEREF_EXISTS OPpMULTIDEREF_DELETE | ||
| 25 | SVf_ROK SVpad_OUR SVf_FAKE SVs_RMG SVs_SMG | ||
| 26 | SVpad_TYPED | ||
| 27 | CVf_METHOD | ||
| 28 | PMf_KEEP PMf_GLOBAL PMf_CONTINUE PMf_EVAL PMf_ONCE | ||
| 29 | PMf_MULTILINE PMf_SINGLELINE PMf_FOLD PMf_EXTENDED PMf_EXTENDED_MORE | ||
| 30 | PADNAMEt_OUTER | ||
| 31 | MDEREF_reload | ||
| 32 | MDEREF_AV_pop_rv2av_aelem | ||
| 33 | MDEREF_AV_gvsv_vivify_rv2av_aelem | ||
| 34 | MDEREF_AV_padsv_vivify_rv2av_aelem | ||
| 35 | MDEREF_AV_vivify_rv2av_aelem | ||
| 36 | MDEREF_AV_padav_aelem | ||
| 37 | MDEREF_AV_gvav_aelem | ||
| 38 | MDEREF_HV_pop_rv2hv_helem | ||
| 39 | MDEREF_HV_gvsv_vivify_rv2hv_helem | ||
| 40 | MDEREF_HV_padsv_vivify_rv2hv_helem | ||
| 41 | MDEREF_HV_vivify_rv2hv_helem | ||
| 42 | MDEREF_HV_padhv_helem | ||
| 43 | MDEREF_HV_gvhv_helem | ||
| 44 | MDEREF_ACTION_MASK | ||
| 45 | MDEREF_INDEX_none | ||
| 46 | MDEREF_INDEX_const | ||
| 47 | MDEREF_INDEX_padsv | ||
| 48 | MDEREF_INDEX_gvsv | ||
| 49 | MDEREF_INDEX_MASK | ||
| 50 | MDEREF_FLAG_last | ||
| 51 | MDEREF_MASK | ||
| 52 | MDEREF_SHIFT | ||
| 53 | ); | ||
| 54 | |||
| 55 | use B::DeparseTree::PPfns; | ||
| 56 | use B::DeparseTree::SyntaxTree; | ||
| 57 | use B::DeparseTree::PP; | ||
| 58 | use B::Deparse; | ||
| 59 | |||
| 60 | # Copy unchanged functions from B::Deparse | ||
| 61 | *begin_is_use = *B::Deparse::begin_is_use; | ||
| 62 | *const_sv = *B::Deparse::const_sv; | ||
| 63 | *find_scope_st = *B::Deparse::find_scope_st; | ||
| 64 | *gv_name = *B::Deparse::gv_name; | ||
| 65 | *meth_rclass_sv = *B::Deparse::meth_rclass_sv; | ||
| 66 | *meth_sv = *B::Deparse::meth_sv; | ||
| 67 | *padany = *B::Deparse::padany; | ||
| 68 | *padname = *B::Deparse::padname; | ||
| 69 | *padname_sv = *B::Deparse::padname_sv; | ||
| 70 | *padval = *B::Deparse::padval; | ||
| 71 | *populate_curcvlex = *B::Deparse::populate_curcvlex; | ||
| 72 | *find_our_type = *B::Deparse::find_our_type; | ||
| 73 | *re_flags = *B::Deparse::re_flags; | ||
| 74 | *stash_variable = *B::Deparse::stash_variable; | ||
| 75 | *stash_variable_name = *B::Deparse::stash_variable_name; | ||
| 76 | *tr_chr = *B::Deparse::tr_chr; | ||
| 77 | |||
| 78 | use strict; | ||
| 79 | use vars qw/$AUTOLOAD/; | ||
| 80 | use warnings (); | ||
| 81 | require feature; | ||
| 82 | |||
| 83 | our(@EXPORT, @ISA); | ||
| 84 | our $VERSION = '3.2.0'; | ||
| 85 | |||
| 86 | @ISA = qw(Exporter); | ||
| 87 | |||
| 88 | BEGIN { | ||
| 89 | # List version-specific constants here. | ||
| 90 | # Easiest way to keep this code portable between version looks to | ||
| 91 | # be to fake up a dummy constant that will never actually be true. | ||
| 92 | foreach (qw(OPpSORT_INPLACE OPpSORT_DESCEND OPpITER_REVERSED OPpCONST_NOVER | ||
| 93 | OPpPAD_STATE PMf_SKIPWHITE RXf_SKIPWHITE | ||
| 94 | PMf_CHARSET PMf_KEEPCOPY PMf_NOCAPTURE CVf_ANONCONST | ||
| 95 | CVf_LOCKED OPpREVERSE_INPLACE OPpSUBSTR_REPL_FIRST | ||
| 96 | PMf_NONDESTRUCT OPpCONST_ARYBASE OPpEVAL_BYTES)) { | ||
| 97 | eval { import B $_ }; | ||
| 98 | no strict 'refs'; | ||
| 99 | *{$_} = sub () {0} unless *{$_}{CODE}; | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | BEGIN { for (qw[rv2sv aelem | ||
| 104 | rv2av rv2hv helem custom ]) { | ||
| 105 | eval "sub OP_\U$_ () { " . opnumber($_) . "}" | ||
| 106 | }} | ||
| 107 | |||
| 108 | # pp_padany -- does not exist after parsing | ||
| 109 | |||
| 110 | sub AUTOLOAD { | ||
| 111 | if ($AUTOLOAD =~ s/^.*::pp_//) { | ||
| 112 | warn "unexpected OP_". | ||
| 113 | ($_[1]->type == OP_CUSTOM ? "CUSTOM ($AUTOLOAD)" : uc $AUTOLOAD); | ||
| 114 | return "XXX"; | ||
| 115 | } else { | ||
| 116 | Carp::confess "Undefined subroutine $AUTOLOAD called"; | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | sub DESTROY {} # Do not AUTOLOAD | ||
| 121 | |||
| 122 | # The BEGIN {} is used here because otherwise this code isn't executed | ||
| 123 | # when you run B::Deparse on itself. | ||
| 124 | my %globalnames; | ||
| 125 | BEGIN { map($globalnames{$_}++, "SIG", "STDIN", "STDOUT", "STDERR", "INC", | ||
| 126 | "ENV", "ARGV", "ARGVOUT", "_"); } | ||
| 127 | |||
| 128 | my %feature_keywords = ( | ||
| 129 | # keyword => 'feature', | ||
| 130 | state => 'state', | ||
| 131 | say => 'say', | ||
| 132 | given => 'switch', | ||
| 133 | when => 'switch', | ||
| 134 | default => 'switch', | ||
| 135 | break => 'switch', | ||
| 136 | evalbytes=>'evalbytes', | ||
| 137 | __SUB__ => '__SUB__', | ||
| 138 | fc => 'fc', | ||
| 139 | ); | ||
| 140 | |||
| 141 | # keywords that are strong and also have a prototype | ||
| 142 | # | ||
| 143 | my %strong_proto_keywords = map { $_ => 1 } qw( | ||
| 144 | pos | ||
| 145 | prototype | ||
| 146 | scalar | ||
| 147 | study | ||
| 148 | undef | ||
| 149 | ); | ||
| 150 | |||
| 151 | # FIXME: we can't make this common just yet. | ||
| 152 | sub keyword { | ||
| 153 | my $self = shift; | ||
| 154 | my $name = shift; | ||
| 155 | return $name if $name =~ /^CORE::/; # just in case | ||
| 156 | if (exists $feature_keywords{$name}) { | ||
| 157 | my $hh; | ||
| 158 | my $hints = $self->{hints} & $feature::hint_mask; | ||
| 159 | if ($hints && $hints != $feature::hint_mask) { | ||
| 160 | $hh = B::Deparse::_features_from_bundle($hints); | ||
| 161 | } | ||
| 162 | elsif ($hints) { $hh = $self->{'hinthash'} } | ||
| 163 | return "CORE::$name" | ||
| 164 | if !$hh | ||
| 165 | || !$hh->{"feature_$feature_keywords{$name}"} | ||
| 166 | } | ||
| 167 | if ($strong_proto_keywords{$name} | ||
| 168 | || ($name !~ /^(?:chom?p|do|exec|glob|s(?:elect|ystem))\z/ | ||
| 169 | && !defined eval{prototype "CORE::$name"}) | ||
| 170 | ) { return $name } | ||
| 171 | if ( | ||
| 172 | exists $self->{subs_declared}{$name} | ||
| 173 | or | ||
| 174 | exists &{"$self->{curstash}::$name"} | ||
| 175 | ) { | ||
| 176 | return "CORE::$name" | ||
| 177 | } | ||
| 178 | return $name; | ||
| 179 | } | ||
| 180 | |||
| 181 | # Note: maybe_local things can't be moved to PP yet. | ||
| 182 | { no strict 'refs'; *{"pp_r$_"} = *{"pp_$_"} for qw< keys each values >; } | ||
| 183 | |||
| 184 | sub pp_readline { | ||
| 185 | my $self = shift; | ||
| 186 | my($op, $cx) = @_; | ||
| 187 | my $kid = $op->first; | ||
| 188 | $kid = $kid->first if $kid->name eq "rv2gv"; # <$fh> | ||
| 189 | if (B::Deparse::is_scalar($kid)) { | ||
| 190 | my $body = [$self->deparse($kid, 1, $op)]; | ||
| 191 | return info_from_list($op, $self, ['<', $body->[0]{text}, '>'], '', | ||
| 192 | 'readline_scalar', {body=>$body}); | ||
| 193 | } | ||
| 194 | return $self->unop($op, $cx, "readline"); | ||
| 195 | } | ||
| 196 | |||
| 197 | sub pp_rcatline { | ||
| 198 | my $self = shift; | ||
| 199 | my($op) = @_; | ||
| 200 | return info_from_list($op, $self, ["<", $self->gv_name($self->gv_or_padgv($op)), ">"], | ||
| 201 | '', 'rcatline', {}); | ||
| 202 | } | ||
| 203 | |||
| 204 | sub pp_smartmatch { | ||
| 205 | my ($self, $op, $cx) = @_; | ||
| 206 | if ($op->flags & OPf_SPECIAL) { | ||
| 207 | return $self->deparse($op->last, $cx, $op); | ||
| 208 | } | ||
| 209 | else { | ||
| 210 | binop(@_, "~~", 14); | ||
| 211 | } | ||
| 212 | } | ||
| 213 | |||
| 214 | sub bin_info_join($$$$$$$) { | ||
| 215 | my ($self, $op, $lhs, $rhs, $mid, $sep, $type) = @_; | ||
| 216 | my $texts = [$lhs->{text}, $mid, $rhs->{text}]; | ||
| 217 | return info_from_list($op, $self, $texts, ' ', $type, {}) | ||
| 218 | } | ||
| 219 | |||
| 220 | sub bin_info_join_maybe_parens($$$$$$$$$) { | ||
| 221 | my ($self, $op, $lhs, $rhs, $mid, $sep, $cx, $prec, $type) = @_; | ||
| 222 | my $info = bin_info_join($self, $op, $lhs, $rhs, $mid, $sep, $type); | ||
| 223 | $info->{text} = $self->maybe_parens($info->{text}, $cx, $prec); | ||
| 224 | return $info; | ||
| 225 | } | ||
| 226 | |||
| 227 | sub range { | ||
| 228 | my $self = shift; | ||
| 229 | my ($op, $cx, $type) = @_; | ||
| 230 | my $left = $op->first; | ||
| 231 | my $right = $left->sibling; | ||
| 232 | $left = $self->deparse($left, 9, $op); | ||
| 233 | $right = $self->deparse($right, 9, $op); | ||
| 234 | return info_from_list($op, $self, [$left, $type, $right], ' ', 'range', | ||
| 235 | {maybe_parens => [$self, $cx, 9]}); | ||
| 236 | } | ||
| 237 | |||
| 238 | sub pp_substr { | ||
| 239 | my ($self,$op,$cx) = @_; | ||
| 240 | if ($op->private & OPpSUBSTR_REPL_FIRST) { | ||
| 241 | my $left = listop($self, $op, 7, "substr", $op->first->sibling->sibling); | ||
| 242 | my $right = $self->deparse($op->first->sibling, 7, $op); | ||
| 243 | return info_from_list($op, $self,[$left, '=', $right], ' ', | ||
| 244 | 'substr_repl_first', {}); | ||
| 245 | } | ||
| 246 | return maybe_local(@_, listop(@_, "substr")) | ||
| 247 | } | ||
| 248 | # Truncate is special because OPf_SPECIAL makes a bareword first arg | ||
| 249 | # be a filehandle. This could probably be better fixed in the core | ||
| 250 | # by moving the GV lookup into ck_truc. | ||
| 251 | |||
| 252 | sub pp_truncate | ||
| 253 | { | ||
| 254 | my($self, $op, $cx) = @_; | ||
| 255 | my(@exprs); | ||
| 256 | my $parens = ($cx >= 5) || $self->{'parens'}; | ||
| 257 | my $kid = $op->first->sibling; | ||
| 258 | my $fh; | ||
| 259 | if ($op->flags & OPf_SPECIAL) { | ||
| 260 | # $kid is an OP_CONST | ||
| 261 | $fh = $self->const_sv($kid)->PV; | ||
| 262 | } else { | ||
| 263 | $fh = $self->deparse($kid, 6, $op); | ||
| 264 | $fh = "+$fh" if not $parens and substr($fh, 0, 1) eq "("; | ||
| 265 | } | ||
| 266 | my $len = $self->deparse($kid->sibling, 6, $op); | ||
| 267 | my $name = $self->keyword('truncate'); | ||
| 268 | my $opts = {body => [$fh, $len]}; | ||
| 269 | my $args = "$fh->{text}, $len->{text}"; | ||
| 270 | if ($parens) { | ||
| 271 | return info_from_list($op, $self, [$name, '(', $args, ')'], '', | ||
| 272 | 'truncate_parens', $opts); | ||
| 273 | return "$name($fh, $len)"; | ||
| 274 | } else { | ||
| 275 | return info_from_list($op, $self, [$name, $args], '', 'truncate', $opts); | ||
| 276 | } | ||
| 277 | } | ||
| 278 | |||
| 279 | sub for_loop { | ||
| 280 | my $self = shift; | ||
| 281 | my($op, $cx, $parent) = @_; | ||
| 282 | my $init = $self->deparse($op, 1, $parent); | ||
| 283 | my $s = $op->sibling; | ||
| 284 | my $ll = $s->name eq "unstack" ? $s->sibling : $s->first->sibling; | ||
| 285 | return $self->loop_common($ll, $cx, $init); | ||
| 286 | } | ||
| 287 | |||
| 288 | sub pp_padsv { | ||
| 289 | my $self = shift; | ||
| 290 | my($op, $cx, $forbid_parens) = @_; | ||
| 291 | return $self->maybe_my($op, $cx, $self->padname($op->targ), | ||
| 292 | $forbid_parens); | ||
| 293 | } | ||
| 294 | |||
| 295 | my @threadsv_names = B::threadsv_names; | ||
| 296 | sub pp_threadsv { | ||
| 297 | my $self = shift; | ||
| 298 | my($op, $cx) = @_; | ||
| 299 | return $self->maybe_local_str($op, $cx, "\$" . $threadsv_names[$op->targ]); | ||
| 300 | } | ||
| 301 | |||
| 302 | sub gv_or_padgv { | ||
| 303 | my $self = shift; | ||
| 304 | my $op = shift; | ||
| 305 | if (class($op) eq "PADOP") { | ||
| 306 | return $self->padval($op->padix); | ||
| 307 | } else { # class($op) eq "SVOP" | ||
| 308 | return $op->gv; | ||
| 309 | } | ||
| 310 | } | ||
| 311 | |||
| 312 | sub pp_aelemfast_lex | ||
| 313 | { | ||
| 314 | my($self, $op, $cx) = @_; | ||
| 315 | my $name = $self->padname($op->targ); | ||
| 316 | $name =~ s/^@/\$/; | ||
| 317 | return info_from_list($op, $self, [$name, "[", ($op->private + $self->{'arybase'}), "]"], | ||
| 318 | '', 'pp_aelemfast_lex', {}); | ||
| 319 | } | ||
| 320 | |||
| 321 | sub pp_aelemfast | ||
| 322 | { | ||
| 323 | my($self, $op, $cx) = @_; | ||
| 324 | # optimised PADAV, pre 5.15 | ||
| 325 | return $self->pp_aelemfast_lex(@_) if ($op->flags & OPf_SPECIAL); | ||
| 326 | |||
| 327 | my $gv = $self->gv_or_padgv($op); | ||
| 328 | my($name,$quoted) = $self->stash_variable_name('@',$gv); | ||
| 329 | $name = $quoted ? "$name->" : '$' . $name; | ||
| 330 | my $i = $op->private; | ||
| 331 | $i -= 256 if $i > 127; | ||
| 332 | return info_from_list($op, $self, [$name, "[", ($op->private + $self->{'arybase'}), "]"], | ||
| 333 | '', 'pp_aelemfast', {}); | ||
| 334 | } | ||
| 335 | |||
| 336 | sub pp_rv2sv { maybe_local(@_, rv2x(@_, "\$")) } | ||
| 337 | sub pp_rv2hv { maybe_local(@_, rv2x(@_, "%")) } | ||
| 338 | sub pp_rv2gv { maybe_local(@_, rv2x(@_, "*")) } | ||
| 339 | |||
| 340 | # skip rv2av | ||
| 341 | sub pp_av2arylen { | ||
| 342 | my $self = shift; | ||
| 343 | my($op, $cx) = @_; | ||
| 344 | if ($op->first->name eq "padav") { | ||
| 345 | return $self->maybe_local_str($op, $cx, '$#' . $self->padany($op->first)); | ||
| 346 | } else { | ||
| 347 | return $self->maybe_local($op, $cx, | ||
| 348 | $self->rv2x($op->first, $cx, '$#')); | ||
| 349 | } | ||
| 350 | } | ||
| 351 | |||
| 352 | sub list_const($$$) { | ||
| 353 | my $self = shift; | ||
| 354 | my($op, $cx, @list) = @_; | ||
| 355 | my @a = map $self->const($_, 6), @list; | ||
| 356 | my @texts = $self->map_texts(\@a); | ||
| 357 | my $type = 'list_const'; | ||
| 358 | my $prec = 6; | ||
| 359 | if (@texts == 0) { | ||
| 360 | return info_from_list($op, $self, ['(', ')'], '', 'list_const_null', {}); | ||
| 361 | } elsif (@texts == 1) { | ||
| 362 | return info_from_text($op, $self, $texts[0], 'list_const_one', | ||
| 363 | {body => \@a}); | ||
| 364 | } elsif ( @texts > 2 and !grep(!/^-?\d+$/, @texts)) { | ||
| 365 | # collapse (-1,0,1,2) into (-1..2) | ||
| 366 | my ($s, $e) = @texts[0,-1]; | ||
| 367 | my $i = $s; | ||
| 368 | unless (grep $i++ != $_, @texts) { | ||
| 369 | @texts = ($s, '..', $e); | ||
| 370 | $type = 'list_const_range'; | ||
| 371 | $prec = 9; | ||
| 372 | } | ||
| 373 | } | ||
| 374 | return info_from_list($op, $self, \@texts, '', $type, | ||
| 375 | {maybe_parens => [$self, $cx, $prec]}); | ||
| 376 | } | ||
| 377 | |||
| 378 | sub pp_rv2av { | ||
| 379 | my $self = shift; | ||
| 380 | my($op, $cx) = @_; | ||
| 381 | my $kid = $op->first; | ||
| 382 | if ($kid->name eq "const") { # constant list | ||
| 383 | my $av = $self->const_sv($kid); | ||
| 384 | return $self->list_const($kid, $cx, $av->ARRAY); | ||
| 385 | } else { | ||
| 386 | # FIXME? | ||
| 387 | return $self->maybe_local($op, $cx, $self->rv2x($op, $cx, "\@")); | ||
| 388 | } | ||
| 389 | } | ||
| 390 | |||
| 391 | sub elem_or_slice_array_name | ||
| 392 | { | ||
| 393 | my $self = shift; | ||
| 394 | my ($array, $left, $padname, $allow_arrow) = @_; | ||
| 395 | |||
| 396 | if ($array->name eq $padname) { | ||
| 397 | return $self->padany($array); | ||
| 398 | } elsif (B::Deparse::is_scope($array)) { # ${expr}[0] | ||
| 399 | return "{" . $self->deparse($array, 0) . "}"; | ||
| 400 | } elsif ($array->name eq "gv") { | ||
| 401 | ($array, my $quoted) = | ||
| 402 | $self->stash_variable_name( | ||
| 403 | $left eq '[' ? '@' : '%', $self->gv_or_padgv($array) | ||
| 404 | ); | ||
| 405 | if (!$allow_arrow && $quoted) { | ||
| 406 | # This cannot happen. | ||
| 407 | die "Invalid variable name $array for slice"; | ||
| 408 | } | ||
| 409 | return $quoted ? "$array->" : $array; | ||
| 410 | } elsif (!$allow_arrow || B::Deparse::is_scalar $array) { | ||
| 411 | # $x[0], $$x[0], ... | ||
| 412 | return $self->deparse($array, 24)->{text}; | ||
| 413 | } else { | ||
| 414 | return undef; | ||
| 415 | } | ||
| 416 | } | ||
| 417 | |||
| 418 | sub elem_or_slice_single_index($$) | ||
| 419 | { | ||
| 420 | my ($self, $idx, $parent) = @_; | ||
| 421 | |||
| 422 | my $idx_info = $self->deparse($idx, 1, $parent); | ||
| 423 | my $idx_str = $idx_info->{text}; | ||
| 424 | |||
| 425 | # Outer parens in an array index will confuse perl | ||
| 426 | # if we're interpolating in a regular expression, i.e. | ||
| 427 | # /$x$foo[(-1)]/ is *not* the same as /$x$foo[-1]/ | ||
| 428 | # | ||
| 429 | # If $self->{parens}, then an initial '(' will | ||
| 430 | # definitely be paired with a final ')'. If | ||
| 431 | # !$self->{parens}, the misleading parens won't | ||
| 432 | # have been added in the first place. | ||
| 433 | # | ||
| 434 | # [You might think that we could get "(...)...(...)" | ||
| 435 | # where the initial and final parens do not match | ||
| 436 | # each other. But we can't, because the above would | ||
| 437 | # only happen if there's an infix binop between the | ||
| 438 | # two pairs of parens, and *that* means that the whole | ||
| 439 | # expression would be parenthesized as well.] | ||
| 440 | # | ||
| 441 | $idx_str =~ s/^\((.*)\)$/$1/ if $self->{'parens'}; | ||
| 442 | |||
| 443 | # Hash-element braces will autoquote a bareword inside themselves. | ||
| 444 | # We need to make sure that C<$hash{warn()}> doesn't come out as | ||
| 445 | # C<$hash{warn}>, which has a quite different meaning. Currently | ||
| 446 | # B::Deparse will always quote strings, even if the string was a | ||
| 447 | # bareword in the original (i.e. the OPpCONST_BARE flag is ignored | ||
| 448 | # for constant strings.) So we can cheat slightly here - if we see | ||
| 449 | # a bareword, we know that it is supposed to be a function call. | ||
| 450 | # | ||
| 451 | $idx_str =~ s/^([A-Za-z_]\w*)$/$1()/; | ||
| 452 | |||
| 453 | return info_from_text($idx_info->{op}, $self, $idx_str, | ||
| 454 | 'elem_or_slice_single_index', | ||
| 455 | {body => [$idx_info]}); | ||
| 456 | } | ||
| 457 | |||
| 458 | # a simplified version of elem_or_slice_array_name() | ||
| 459 | # for the use of pp_multideref | ||
| 460 | |||
| 461 | sub multideref_var_name($$$) | ||
| 462 | { | ||
| 463 | my ($self, $gv, $is_hash) = @_; | ||
| 464 | |||
| 465 | my ($name, $quoted) = | ||
| 466 | $self->stash_variable_name( $is_hash ? '%' : '@', $gv); | ||
| 467 | return $quoted ? "$name->" | ||
| 468 | : $name eq '#' | ||
| 469 | ? '${#}' # avoid ${#}[1] => $#[1] | ||
| 470 | : '$' . $name; | ||
| 471 | } | ||
| 472 | |||
| 473 | sub pp_multideref | ||
| 474 | { | ||
| 475 | my($self, $op, $cx) = @_; | ||
| 476 | my @texts = (); | ||
| 477 | |||
| 478 | if ($op->private & OPpMULTIDEREF_EXISTS) { | ||
| 479 | @texts = ($self->keyword("exists"), ' '); | ||
| 480 | } | ||
| 481 | elsif ($op->private & OPpMULTIDEREF_DELETE) { | ||
| 482 | @texts = ($self->keyword("delete"), ' ') | ||
| 483 | } | ||
| 484 | elsif ($op->private & OPpLVAL_INTRO) { | ||
| 485 | @texts = ($self->keyword("local"), ' ') | ||
| 486 | } | ||
| 487 | |||
| 488 | if ($op->first && ($op->first->flags & OPf_KIDS)) { | ||
| 489 | # arbitrary initial expression, e.g. f(1,2,3)->[...] | ||
| 490 | my $first = $self->deparse($op->first, 24, $op); | ||
| 491 | push @texts, $first->{text}; | ||
| 492 | } | ||
| 493 | |||
| 494 | my @items = $op->aux_list($self->{curcv}); | ||
| 495 | my $actions = shift @items; | ||
| 496 | |||
| 497 | my $is_hash; | ||
| 498 | my $derefs = 0; | ||
| 499 | |||
| 500 | while (1) { | ||
| 501 | if (($actions & MDEREF_ACTION_MASK) == MDEREF_reload) { | ||
| 502 | $actions = shift @items; | ||
| 503 | next; | ||
| 504 | } | ||
| 505 | |||
| 506 | $is_hash = ( | ||
| 507 | ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_pop_rv2hv_helem | ||
| 508 | || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_gvsv_vivify_rv2hv_helem | ||
| 509 | || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_padsv_vivify_rv2hv_helem | ||
| 510 | || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_vivify_rv2hv_helem | ||
| 511 | || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_padhv_helem | ||
| 512 | || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_gvhv_helem | ||
| 513 | ); | ||
| 514 | |||
| 515 | if ( ($actions & MDEREF_ACTION_MASK) == MDEREF_AV_padav_aelem | ||
| 516 | || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_padhv_helem) | ||
| 517 | { | ||
| 518 | $derefs = 1; | ||
| 519 | push @texts, '$' . substr($self->padname(shift @items), 1); | ||
| 520 | } | ||
| 521 | elsif ( ($actions & MDEREF_ACTION_MASK) == MDEREF_AV_gvav_aelem | ||
| 522 | || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_gvhv_helem) | ||
| 523 | { | ||
| 524 | $derefs = 1; | ||
| 525 | push @texts, $self->multideref_var_name(shift @items, $is_hash); | ||
| 526 | } | ||
| 527 | else { | ||
| 528 | if ( ($actions & MDEREF_ACTION_MASK) == | ||
| 529 | MDEREF_AV_padsv_vivify_rv2av_aelem | ||
| 530 | || ($actions & MDEREF_ACTION_MASK) == | ||
| 531 | MDEREF_HV_padsv_vivify_rv2hv_helem) | ||
| 532 | { | ||
| 533 | push @texts, $self->padname(shift @items); | ||
| 534 | } | ||
| 535 | elsif ( ($actions & MDEREF_ACTION_MASK) == | ||
| 536 | MDEREF_AV_gvsv_vivify_rv2av_aelem | ||
| 537 | || ($actions & MDEREF_ACTION_MASK) == | ||
| 538 | MDEREF_HV_gvsv_vivify_rv2hv_helem) | ||
| 539 | { | ||
| 540 | push @texts, $self->multideref_var_name(shift @items, $is_hash); | ||
| 541 | } | ||
| 542 | elsif ( ($actions & MDEREF_ACTION_MASK) == | ||
| 543 | MDEREF_AV_pop_rv2av_aelem | ||
| 544 | || ($actions & MDEREF_ACTION_MASK) == | ||
| 545 | MDEREF_HV_pop_rv2hv_helem) | ||
| 546 | { | ||
| 547 | if ( ($op->flags & OPf_KIDS) | ||
| 548 | && ( B::Deparse::_op_is_or_was($op->first, OP_RV2AV) | ||
| 549 | || B::Deparse::_op_is_or_was($op->first, OP_RV2HV)) | ||
| 550 | && ($op->first->flags & OPf_KIDS) | ||
| 551 | && ( B::Deparse::_op_is_or_was($op->first->first, OP_AELEM) | ||
| 552 | || B::Deparse::_op_is_or_was($op->first->first, OP_HELEM)) | ||
| 553 | ) | ||
| 554 | { | ||
| 555 | $derefs++; | ||
| 556 | } | ||
| 557 | } | ||
| 558 | |||
| 559 | push(@texts, '->') if !$derefs++; | ||
| 560 | } | ||
| 561 | |||
| 562 | |||
| 563 | if (($actions & MDEREF_INDEX_MASK) == MDEREF_INDEX_none) { | ||
| 564 | last; | ||
| 565 | } | ||
| 566 | |||
| 567 | push(@texts, $is_hash ? '{' : '['); | ||
| 568 | |||
| 569 | if (($actions & MDEREF_INDEX_MASK) == MDEREF_INDEX_const) { | ||
| 570 | my $key = shift @items; | ||
| 571 | if ($is_hash) { | ||
| 572 | push @texts, $self->const($key, $cx)->{text}; | ||
| 573 | } | ||
| 574 | else { | ||
| 575 | push @texts, $key; | ||
| 576 | } | ||
| 577 | } | ||
| 578 | elsif (($actions & MDEREF_INDEX_MASK) == MDEREF_INDEX_padsv) { | ||
| 579 | push @texts, $self->padname(shift @items); | ||
| 580 | } | ||
| 581 | elsif (($actions & MDEREF_INDEX_MASK) == MDEREF_INDEX_gvsv) { | ||
| 582 | push @texts,('$' . ($self->stash_variable_name('$', shift @items))[0]); | ||
| 583 | } | ||
| 584 | |||
| 585 | push(@texts, $is_hash ? '}' : ']'); | ||
| 586 | |||
| 587 | if ($actions & MDEREF_FLAG_last) { | ||
| 588 | last; | ||
| 589 | } | ||
| 590 | $actions >>= MDEREF_SHIFT; | ||
| 591 | } | ||
| 592 | |||
| 593 | return info_from_list($op, $self, \@texts, '', 'multideref', {}); | ||
| 594 | } | ||
| 595 | |||
| 596 | sub pp_gelem | ||
| 597 | { | ||
| 598 | my($self, $op, $cx) = @_; | ||
| 599 | my($glob, $part) = ($op->first, $op->last); | ||
| 600 | $glob = $glob->first; # skip rv2gv | ||
| 601 | $glob = $glob->first if $glob->name eq "rv2gv"; # this one's a bug | ||
| 602 | my $scope = B::Deparse::is_scope($glob); | ||
| 603 | $glob = $self->deparse($glob, 0); | ||
| 604 | $part = $self->deparse($part, 1); | ||
| 605 | return "*" . ($scope ? "{$glob}" : $glob) . "{$part}"; | ||
| 606 | } | ||
| 607 | |||
| 608 | sub pp_lslice | ||
| 609 | { | ||
| 610 | my ($self, $op, $cs) = @_; | ||
| 611 | my $idx = $op->first; | ||
| 612 | my $list = $op->last; | ||
| 613 | my(@elems, $kid); | ||
| 614 | my $list_info = $self->deparse($list, 1, $op); | ||
| 615 | my $idx_info = $self->deparse($idx, 1, $op); | ||
| 616 | return info_from_list($op, $self, ['(', $list_info->{text}, ')', '[', $idx_info->{text}, ']'], | ||
| 617 | '', 'lslice', {body=>[$list_info, $idx_info]}); | ||
| 618 | } | ||
| 619 | |||
| 620 | sub _method | ||
| 621 | { | ||
| 622 | my($self, $op, $cx) = @_; | ||
| 623 | my @other_ops = ($op->first); | ||
| 624 | my $kid = $op->first->sibling; # skip pushmark | ||
| 625 | my($meth, $obj, @exprs); | ||
| 626 | if ($kid->name eq "list" and B::Deparse::want_list $kid) { | ||
| 627 | # When an indirect object isn't a bareword but the args are in | ||
| 628 | # parens, the parens aren't part of the method syntax (the LLAFR | ||
| 629 | # doesn't apply), but they make a list with OPf_PARENS set that | ||
| 630 | # doesn't get flattened by the append_elem that adds the method, | ||
| 631 | # making a (object, arg1, arg2, ...) list where the object | ||
| 632 | # usually is. This can be distinguished from | ||
| 633 | # '($obj, $arg1, $arg2)->meth()' (which is legal if $arg2 is an | ||
| 634 | # object) because in the later the list is in scalar context | ||
| 635 | # as the left side of -> always is, while in the former | ||
| 636 | # the list is in list context as method arguments always are. | ||
| 637 | # (Good thing there aren't method prototypes!) | ||
| 638 | $meth = $kid->sibling; | ||
| 639 | push @other_ops, $kid->first; | ||
| 640 | $kid = $kid->first->sibling; # skip pushmark | ||
| 641 | $obj = $kid; | ||
| 642 | $kid = $kid->sibling; | ||
| 643 | for (; not B::Deparse::null $kid; $kid = $kid->sibling) { | ||
| 644 | push @exprs, $kid; | ||
| 645 | } | ||
| 646 | } else { | ||
| 647 | $obj = $kid; | ||
| 648 | $kid = $kid->sibling; | ||
| 649 | for (; !B::Deparse::null ($kid->sibling) && $kid->name!~/^method(?:_named)?\z/; | ||
| 650 | $kid = $kid->sibling) { | ||
| 651 | push @exprs, $kid | ||
| 652 | } | ||
| 653 | $meth = $kid; | ||
| 654 | } | ||
| 655 | |||
| 656 | if ($meth->name eq "method_named") { | ||
| 657 | $meth = $self->meth_sv($meth)->PV; | ||
| 658 | } elsif ($meth->name eq "method_super") { | ||
| 659 | $meth = "SUPER::".$self->meth_sv($meth)->PV; | ||
| 660 | } elsif ($meth->name eq "method_redir") { | ||
| 661 | $meth = $self->meth_rclass_sv($meth)->PV.'::'.$self->meth_sv($meth)->PV; | ||
| 662 | } elsif ($meth->name eq "method_redir_super") { | ||
| 663 | $meth = $self->meth_rclass_sv($meth)->PV.'::SUPER::'. | ||
| 664 | $self->meth_sv($meth)->PV; | ||
| 665 | } else { | ||
| 666 | $meth = $meth->first; | ||
| 667 | if ($meth->name eq "const") { | ||
| 668 | # As of 5.005_58, this case is probably obsoleted by the | ||
| 669 | # method_named case above | ||
| 670 | $meth = $self->const_sv($meth)->PV; # needs to be bare | ||
| 671 | } | ||
| 672 | } | ||
| 673 | |||
| 674 | return { | ||
| 675 | method => $meth, | ||
| 676 | variable_method => ref($meth), | ||
| 677 | object => $obj, | ||
| 678 | args => \@exprs, | ||
| 679 | other_ops => \@other_ops | ||
| 680 | }, $cx; | ||
| 681 | } | ||
| 682 | |||
| 683 | sub e_method { | ||
| 684 | my ($self, $op, $minfo, $cx) = @_; | ||
| 685 | my $obj = $self->deparse($minfo->{object}, 24, $op); | ||
| 686 | my @body = ($obj); | ||
| 687 | my $other_ops = $minfo->{other_ops}; | ||
| 688 | |||
| 689 | my $meth_name = $minfo->{method}; | ||
| 690 | my $meth_info; | ||
| 691 | if ($minfo->{variable_method}) { | ||
| 692 | $meth_info = $self->deparse($meth_name, 1, $op); | ||
| 693 | push @body, $meth_info; | ||
| 694 | } | ||
| 695 | my @args = map { $self->deparse($_, 6, $op) } @{$minfo->{args}}; | ||
| 696 | push @body, @args; | ||
| 697 | my @args_texts = map $_->{text}, @args; | ||
| 698 | my $args = join(", ", @args_texts); | ||
| 699 | |||
| 700 | my $opts = {other_ops => $other_ops}; | ||
| 701 | my @texts = (); | ||
| 702 | my $type; | ||
| 703 | |||
| 704 | |||
| 705 | my $meth_object = $meth_info ? defined($meth_info) : $meth_name; | ||
| 706 | if ($minfo->{object}->name eq 'scope' && B::Deparse::want_list $minfo->{object}) { | ||
| 707 | # method { $object } | ||
| 708 | # This must be deparsed this way to preserve list context | ||
| 709 | # of $object. | ||
| 710 | my $need_paren = $cx >= 6; | ||
| 711 | if ($need_paren) { | ||
| 712 | @texts = ('(', $meth_object, substr($obj,2), | ||
| 713 | $args, ')'); | ||
| 714 | $type = 'e_method list ()'; | ||
| 715 | } else { | ||
| 716 | @texts = ($meth_object, substr($obj,2), $args); | ||
| 717 | $type = 'e_method list, no ()'; | ||
| 718 | } | ||
| 719 | return info_from_list($op, $self, \@texts, '', $type, $opts); | ||
| 720 | } | ||
| 721 | |||
| 722 | if (length $args) { | ||
| 723 | @texts = ($obj, '->', $meth_object, '(', $args, ')'); | ||
| 724 | $type = 'e_method -> ()'; | ||
| 725 | } else { | ||
| 726 | @texts = ($obj, '->', $meth_object); | ||
| 727 | $type = 'e_method -> no ()'; | ||
| 728 | } | ||
| 729 | return info_from_list($op, $self, \@texts, '', $type, $opts); | ||
| 730 | } | ||
| 731 | |||
| 732 | # returns "&" and the argument bodies if the prototype doesn't match the args, | ||
| 733 | # or ("", $args_after_prototype_demunging) if it does. | ||
| 734 | sub check_proto { | ||
| 735 | my $self = shift; | ||
| 736 | my $op = shift; | ||
| 737 | return ('&', []) if $self->{'noproto'}; | ||
| 738 | my($proto, @args) = @_; | ||
| 739 | my($arg, $real); | ||
| 740 | my $doneok = 0; | ||
| 741 | my @reals; | ||
| 742 | # An unbackslashed @ or % gobbles up the rest of the args | ||
| 743 | 1 while $proto =~ s/(?<!\\)([@%])[^\]]+$/$1/; | ||
| 744 | $proto =~ s/^\s*//; | ||
| 745 | while ($proto) { | ||
| 746 | $proto =~ s/^(\\?[\$\@&%*_]|\\\[[\$\@&%*]+\]|;)\s*//; | ||
| 747 | my $chr = $1; | ||
| 748 | if ($chr eq "") { | ||
| 749 | return ('&', []) if @args; | ||
| 750 | } elsif ($chr eq ";") { | ||
| 751 | $doneok = 1; | ||
| 752 | } elsif ($chr eq "@" or $chr eq "%") { | ||
| 753 | push @reals, map($self->deparse($_, 6), @args, $op); | ||
| 754 | @args = (); | ||
| 755 | } else { | ||
| 756 | $arg = shift @args; | ||
| 757 | last unless $arg; | ||
| 758 | if ($chr eq "\$" || $chr eq "_") { | ||
| 759 | if (B::Deparse::want_scalar $arg) { | ||
| 760 | push @reals, $self->deparse($arg, 6, $op); | ||
| 761 | } else { | ||
| 762 | return ('&', []); | ||
| 763 | } | ||
| 764 | } elsif ($chr eq "&") { | ||
| 765 | if ($arg->name =~ /^(s?refgen|undef)$/) { | ||
| 766 | push @reals, $self->deparse($arg, 6, $op); | ||
| 767 | } else { | ||
| 768 | return ('&', []); | ||
| 769 | } | ||
| 770 | } elsif ($chr eq "*") { | ||
| 771 | if ($arg->name =~ /^s?refgen$/ | ||
| 772 | and $arg->first->first->name eq "rv2gv") | ||
| 773 | { | ||
| 774 | $real = $arg->first->first; # skip refgen, null | ||
| 775 | if ($real->first->name eq "gv") { | ||
| 776 | push @reals, $self->deparse($real, 6, $op); | ||
| 777 | } else { | ||
| 778 | push @reals, $self->deparse($real->first, 6, $op); | ||
| 779 | } | ||
| 780 | } else { | ||
| 781 | return ('&', []); | ||
| 782 | } | ||
| 783 | } elsif (substr($chr, 0, 1) eq "\\") { | ||
| 784 | $chr =~ tr/\\[]//d; | ||
| 785 | if ($arg->name =~ /^s?refgen$/ and | ||
| 786 | !B::Deparse::null($real = $arg->first) and | ||
| 787 | ($chr =~ /\$/ && B::Deparse::is_scalar($real->first) | ||
| 788 | or ($chr =~ /@/ | ||
| 789 | && class($real->first->sibling) ne 'NULL' | ||
| 790 | && $real->first->sibling->name | ||
| 791 | =~ /^(rv2|pad)av$/) | ||
| 792 | or ($chr =~ /%/ | ||
| 793 | && class($real->first->sibling) ne 'NULL' | ||
| 794 | && $real->first->sibling->name | ||
| 795 | =~ /^(rv2|pad)hv$/) | ||
| 796 | #or ($chr =~ /&/ # This doesn't work | ||
| 797 | # && $real->first->name eq "rv2cv") | ||
| 798 | or ($chr =~ /\*/ | ||
| 799 | && $real->first->name eq "rv2gv"))) | ||
| 800 | { | ||
| 801 | push @reals, $self->deparse($real, 6, $op); | ||
| 802 | } else { | ||
| 803 | return ('&', []); | ||
| 804 | } | ||
| 805 | } | ||
| 806 | } | ||
| 807 | } | ||
| 808 | return ('&', []) if $proto and !$doneok; # too few args and no ';' | ||
| 809 | return ('&', []) if @args; # too many args | ||
| 810 | return ('', \@reals); | ||
| 811 | } | ||
| 812 | |||
| 813 | sub pp_enterwrite { unop(@_, "write") } | ||
| 814 | |||
| 815 | # Split a floating point number into an integer mantissa and a binary | ||
| 816 | # exponent. Assumes you've already made sure the number isn't zero or | ||
| 817 | # some weird infinity or NaN. | ||
| 818 | sub split_float { | ||
| 819 | my($f) = @_; | ||
| 820 | my $exponent = 0; | ||
| 821 | if ($f == int($f)) { | ||
| 822 | while ($f % 2 == 0) { | ||
| 823 | $f /= 2; | ||
| 824 | $exponent++; | ||
| 825 | } | ||
| 826 | } else { | ||
| 827 | while ($f != int($f)) { | ||
| 828 | $f *= 2; | ||
| 829 | $exponent--; | ||
| 830 | } | ||
| 831 | } | ||
| 832 | my $mantissa = sprintf("%.0f", $f); | ||
| 833 | return ($mantissa, $exponent); | ||
| 834 | } | ||
| 835 | |||
| 836 | # OP_STRINGIFY is a listop, but it only ever has one arg | ||
| 837 | sub pp_stringify { | ||
| 838 | my ($self, $op, $cx) = @_; | ||
| 839 | my $kid = $op->first->sibling; | ||
| 840 | my @other_ops = (); | ||
| 841 | while ($kid->name eq 'null' && !B::Deparse::null($kid->first)) { | ||
| 842 | push(@other_ops, $kid); | ||
| 843 | $kid = $kid->first; | ||
| 844 | } | ||
| 845 | my $info; | ||
| 846 | if ($kid->name =~ /^(?:const|padsv|rv2sv|av2arylen|gvsv|multideref | ||
| 847 | |aelemfast(?:_lex)?|[ah]elem|join|concat)\z/x) { | ||
| 848 | $info = maybe_targmy(@_, \&dquote); | ||
| 849 | } | ||
| 850 | else { | ||
| 851 | # Actually an optimised join. | ||
| 852 | my $info = listop(@_,"join"); | ||
| 853 | $info->{text} =~ s/join([( ])/join$1$self->{'ex_const'}, /; | ||
| 854 | } | ||
| 855 | push @{$info->{other_ops}}, @other_ops; | ||
| 856 | return $info; | ||
| 857 | } | ||
| 858 | |||
| 859 | |||
| 860 | # Like dq(), but different | ||
| 861 | sub re_dq { | ||
| 862 | my $self = shift; | ||
| 863 | my ($op, $extended) = @_; | ||
| 864 | my ($re_dq_info, $fmt); | ||
| 865 | |||
| 866 | my $type = $op->name; | ||
| 867 | my ($re, @texts); | ||
| 868 | my $opts = {}; | ||
| 869 | if ($type eq "const") { | ||
| 870 | return info_from_text($op, $self, '$[', 're_dq_const', {}) | ||
| 871 | if $op->private & OPpCONST_ARYBASE; | ||
| 872 | my $unbacked = B::Deparse::re_unback($self->const_sv($op)->as_string); | ||
| 873 | return B::Deparse::re_uninterp_extended(escape_extended_re($unbacked)) | ||
| 874 | if $extended; | ||
| 875 | return B::Deparse::re_uninterp(B::Deparse::escape_str($unbacked)); | ||
| 876 | } elsif ($type eq "concat") { | ||
| 877 | my $first = $self->re_dq($op->first, $extended); | ||
| 878 | my $last = $self->re_dq($op->last, $extended); | ||
| 879 | return B::Deparse::re_dq_disambiguate($first, $last); | ||
| 880 | } elsif ($type eq "uc") { | ||
| 881 | $re_dq_info = $self->re_dq($op->first->sibling, $extended); | ||
| 882 | $fmt = '\U%c\E'; | ||
| 883 | $type .= ' uc'; | ||
| 884 | } elsif ($type eq "lc") { | ||
| 885 | $re_dq_info = $self->re_dq($op->first->sibling, $extended); | ||
| 886 | $fmt = '\L%c\E'; | ||
| 887 | $type .= ' lc'; | ||
| 888 | } elsif ($type eq "ucfirst") { | ||
| 889 | $re_dq_info = $self->re_dq($op->first->sibling, $extended); | ||
| 890 | $fmt = '\u%c'; | ||
| 891 | $type .= ' ucfirst'; | ||
| 892 | } elsif ($type eq "lcfirst") { | ||
| 893 | $re_dq_info = $self->re_dq($op->first->sibling, $extended); | ||
| 894 | $fmt = '\u%c'; | ||
| 895 | $type .= ' lcfirst'; | ||
| 896 | } elsif ($type eq "quotemeta") { | ||
| 897 | $re = $self->re_dq($op->first->sibling, $extended); | ||
| 898 | @texts = ['\Q', $re->{text},'\E']; | ||
| 899 | $type .= ' quotemeta'; | ||
| 900 | } elsif ($type eq "fc") { | ||
| 901 | $re = $self->re_dq($op->first->sibling, $extended); | ||
| 902 | @texts = ['\F', $re->{text},'\E']; | ||
| 903 | $type .= ' fc'; | ||
| 904 | } elsif ($type eq "join") { | ||
| 905 | return $self->deparse($op->last, 26, $op); # was join($", @ary) | ||
| 906 | } else { | ||
| 907 | my $info = $self->deparse($op, 26, $op); | ||
| 908 | $info->{type} = 're_dq'; | ||
| 909 | $info->{text} =~ s/^\$([(|)])\z/\${$1}/; # $( $| $) need braces | ||
| 910 | return $info; | ||
| 911 | } | ||
| 912 | return info_from_list($op, $self, \@texts, '', $type, $opts); | ||
| 913 | } | ||
| 914 | |||
| 915 | sub pure_string { | ||
| 916 | my ($self, $op) = @_; | ||
| 917 | return 0 if B::Deparse::null $op; | ||
| 918 | my $type = $op->name; | ||
| 919 | |||
| 920 | if ($type eq 'const' || $type eq 'av2arylen') { | ||
| 921 | return 1; | ||
| 922 | } | ||
| 923 | elsif ($type =~ /^(?:[ul]c(first)?|fc)$/ || $type eq 'quotemeta') { | ||
| 924 | return $self->pure_string($op->first->sibling); | ||
| 925 | } | ||
| 926 | elsif ($type eq 'join') { | ||
| 927 | my $join_op = $op->first->sibling; # Skip pushmark | ||
| 928 | return 0 unless $join_op->name eq 'null' && $join_op->targ == OP_RV2SV; | ||
| 929 | |||
| 930 | my $gvop = $join_op->first; | ||
| 931 | return 0 unless $gvop->name eq 'gvsv'; | ||
| 932 | return 0 unless '"' eq $self->gv_name($self->gv_or_padgv($gvop)); | ||
| 933 | |||
| 934 | return 0 unless ${$join_op->sibling} eq ${$op->last}; | ||
| 935 | return 0 unless $op->last->name =~ /^(?:[ah]slice|(?:rv2|pad)av)$/; | ||
| 936 | } | ||
| 937 | elsif ($type eq 'concat') { | ||
| 938 | return $self->pure_string($op->first) | ||
| 939 | && $self->pure_string($op->last); | ||
| 940 | } | ||
| 941 | elsif (B::Deparse::is_scalar($op) || $type =~ /^[ah]elem$/) { | ||
| 942 | return 1; | ||
| 943 | } | ||
| 944 | elsif ($type eq "null" and $op->can('first') and not B::Deparse::null $op->first and | ||
| 945 | ($op->first->name eq "null" and $op->first->can('first') | ||
| 946 | and not B::Deparse::null $op->first->first and | ||
| 947 | $op->first->first->name eq "aelemfast" | ||
| 948 | or | ||
| 949 | $op->first->name =~ /^aelemfast(?:_lex)?\z/ | ||
| 950 | )) { | ||
| 951 | return 1; | ||
| 952 | } | ||
| 953 | else { | ||
| 954 | return 0; | ||
| 955 | } | ||
| 956 | |||
| 957 | return 1; | ||
| 958 | } | ||
| 959 | |||
| 960 | sub regcomp | ||
| 961 | { | ||
| 962 | my($self, $op, $cx, $extended) = @_; | ||
| 963 | my @other_ops = (); | ||
| 964 | my $kid = $op->first; | ||
| 965 | if ($kid->name eq "regcmaybe") { | ||
| 966 | push @other_ops, $kid; | ||
| 967 | $kid = $kid->first; | ||
| 968 | } | ||
| 969 | if ($kid->name eq "regcreset") { | ||
| 970 | push @other_ops, $kid; | ||
| 971 | $kid = $kid->first; | ||
| 972 | } | ||
| 973 | if ($kid->name eq "null" and !B::Deparse::null($kid->first) | ||
| 974 | and $kid->first->name eq 'pushmark') { | ||
| 975 | my $str = ''; | ||
| 976 | push(@other_ops, $kid); | ||
| 977 | $kid = $kid->first->sibling; | ||
| 978 | my @body = (); | ||
| 979 | while (!B::Deparse::null($kid)) { | ||
| 980 | my $first = $str; | ||
| 981 | my $last = $self->re_dq($kid, $extended); | ||
| 982 | push @body, $last; | ||
| 983 | push(@other_ops, $kid); | ||
| 984 | $str = B::Deparse::re_dq_disambiguate($first, | ||
| 985 | $self->info2str($last)); | ||
| 986 | $kid = $kid->sibling; | ||
| 987 | } | ||
| 988 | return (info_from_text($op, $self, $str, 'regcomp', | ||
| 989 | {other_ops => \@other_ops, | ||
| 990 | body => \@body}), 1); | ||
| 991 | } | ||
| 992 | |||
| 993 | if ($self->pure_string($kid)) { | ||
| 994 | my $info = $self->re_dq($kid, $extended); | ||
| 995 | my @kid_ops = $info->{other_ops} ? @{$info->{other_ops}} : (); | ||
| 996 | push @other_ops, @kid_ops; | ||
| 997 | $info->{other_ops} = \@other_ops; | ||
| 998 | return ($info, 1); | ||
| 999 | } | ||
| 1000 | return ($self->deparse($kid, $cx, $op), 0, $op); | ||
| 1001 | } | ||
| 1002 | |||
| 1003 | sub re_flags | ||
| 1004 | { | ||
| 1005 | my ($self, $op) = @_; | ||
| 1006 | my $flags = ''; | ||
| 1007 | my $pmflags = $op->pmflags; | ||
| 1008 | $flags .= "g" if $pmflags & PMf_GLOBAL; | ||
| 1009 | $flags .= "i" if $pmflags & PMf_FOLD; | ||
| 1010 | $flags .= "m" if $pmflags & PMf_MULTILINE; | ||
| 1011 | $flags .= "o" if $pmflags & PMf_KEEP; | ||
| 1012 | $flags .= "s" if $pmflags & PMf_SINGLELINE; | ||
| 1013 | $flags .= "x" if $pmflags & PMf_EXTENDED; | ||
| 1014 | $flags .= "p" if $pmflags & PMf_KEEPCOPY; | ||
| 1015 | $flags .= "n" if $pmflags & PMf_NOCAPTURE; | ||
| 1016 | if (my $charset = $pmflags & PMf_CHARSET) { | ||
| 1017 | # Hardcoding this is fragile, but B does not yet export the | ||
| 1018 | # constants we need. | ||
| 1019 | $flags .= qw(d l u a aa)[$charset >> 5] | ||
| 1020 | } | ||
| 1021 | # The /d flag is indicated by 0; only show it if necessary. | ||
| 1022 | elsif ($self->{hinthash} and | ||
| 1023 | $self->{hinthash}{reflags_charset} | ||
| 1024 | || $self->{hinthash}{feature_unicode} | ||
| 1025 | or $self->{hints} & $feature::hint_mask | ||
| 1026 | && ($self->{hints} & $feature::hint_mask) | ||
| 1027 | != $feature::hint_mask | ||
| 1028 | && do { | ||
| 1029 | $self->{hints} & $feature::hint_uni8bit; | ||
| 1030 | } | ||
| 1031 | ) { | ||
| 1032 | $flags .= 'd'; | ||
| 1033 | } | ||
| 1034 | $flags; | ||
| 1035 | } | ||
| 1036 | |||
| 1037 | # osmic acid -- see osmium tetroxide | ||
| 1038 | |||
| 1039 | sub pp_match { matchop(@_, "m", "/") } | ||
| 1040 | sub pp_pushre { matchop(@_, "m", "/") } | ||
| 1041 | sub pp_qr { matchop(@_, "qr", "") } | ||
| 1042 | |||
| 1043 | sub pp_split { | ||
| 1044 | maybe_targmy(@_, \&split, "split"); | ||
| 1045 | } | ||
| 1046 | |||
| 1047 | sub split | ||
| 1048 | { | ||
| 1049 | my($self, $op, $cx) = @_; | ||
| 1050 | my($kid, @exprs, $ary_info, $expr); | ||
| 1051 | my $ary = ''; | ||
| 1052 | my @body = (); | ||
| 1053 | my @other_ops = (); | ||
| 1054 | $kid = $op->first; | ||
| 1055 | |||
| 1056 | # For our kid (an OP_PUSHRE), pmreplroot is never actually the | ||
| 1057 | # root of a replacement; it's either empty, or abused to point to | ||
| 1058 | # the GV for an array we split into (an optimization to save | ||
| 1059 | # assignment overhead). Depending on whether we're using ithreads, | ||
| 1060 | # this OP* holds either a GV* or a PADOFFSET. Luckily, B.xs | ||
| 1061 | # figures out for us which it is. | ||
| 1062 | my $replroot = $kid->pmreplroot; | ||
| 1063 | my $gv = 0; | ||
| 1064 | my $stacked = $op->flags & OPf_STACKED; | ||
| 1065 | if (ref($replroot) eq "B::GV") { | ||
| 1066 | $gv = $replroot; | ||
| 1067 | } elsif (!ref($replroot) and $replroot > 0) { | ||
| 1068 | $gv = $self->padval($replroot); | ||
| 1069 | } elsif ($kid->targ) { | ||
| 1070 | $ary = $self->padname($kid->targ) | ||
| 1071 | } elsif ($stacked) { | ||
| 1072 | $ary_info = $self->deparse($op->last, 7, $op); | ||
| 1073 | push @body, $ary_info; | ||
| 1074 | $ary = $ary_info->{text}; | ||
| 1075 | } | ||
| 1076 | $ary_info = $self->maybe_local(@_, | ||
| 1077 | $self->stash_variable('@', | ||
| 1078 | $self->gv_name($gv), | ||
| 1079 | $cx)) | ||
| 1080 | if $gv; | ||
| 1081 | |||
| 1082 | # Skip the last kid when OPf_STACKED is set, since it is the array | ||
| 1083 | # on the left. | ||
| 1084 | for (; !B::Deparse::null($stacked ? $kid->sibling : $kid); | ||
| 1085 | $kid = $kid->sibling) { | ||
| 1086 | push @exprs, $self->deparse($kid, 6, $op); | ||
| 1087 | } | ||
| 1088 | |||
| 1089 | push @body, @exprs; | ||
| 1090 | my $opts = {body => \@exprs}; | ||
| 1091 | |||
| 1092 | my @args_texts = map $_->{text}, @exprs; | ||
| 1093 | # handle special case of split(), and split(' ') that compiles to /\s+/ | ||
| 1094 | # Under 5.10, the reflags may be undef if the split regexp isn't a constant | ||
| 1095 | # Under 5.17.5-5.17.9, the special flag is on split itself. | ||
| 1096 | $kid = $op->first; | ||
| 1097 | if ( $op->flags & OPf_SPECIAL ) { | ||
| 1098 | $exprs[0]->{text} = "' '"; | ||
| 1099 | } | ||
| 1100 | |||
| 1101 | my $sep = ''; | ||
| 1102 | my $type; | ||
| 1103 | my @expr_texts; | ||
| 1104 | if ($ary) { | ||
| 1105 | @expr_texts = ("$ary", '=', join(', ', @args_texts)); | ||
| 1106 | $sep = ' '; | ||
| 1107 | $type = 'split_array'; | ||
| 1108 | $opts->{maybe_parens} = [$self, $cx, 7]; | ||
| 1109 | } else { | ||
| 1110 | @expr_texts = ('split', '(', join(', ', @args_texts), ')'); | ||
| 1111 | $type = 'split'; | ||
| 1112 | |||
| 1113 | } | ||
| 1114 | return info_from_list($op, $self, \@expr_texts, $sep, $type, $opts); | ||
| 1115 | } | ||
| 1116 | |||
| 1117 | unless (caller) { | ||
| 1118 | eval "use Data::Printer;"; | ||
| 1119 | |||
| 1120 | eval { | ||
| 1121 | sub fib($) { | ||
| 1122 | my $x = shift; | ||
| 1123 | return 1 if $x <= 1; | ||
| 1124 | return(fib($x-1) + fib($x-2)) | ||
| 1125 | } | ||
| 1126 | sub baz { | ||
| 1127 | no strict; | ||
| 1128 | CORE::wait; | ||
| 1129 | } | ||
| 1130 | }; | ||
| 1131 | |||
| 1132 | # use B::Deparse; | ||
| 1133 | # my $deparse_old = B::Deparse->new("-l", "-sC"); | ||
| 1134 | # print $deparse_old->coderef2text(\&baz); | ||
| 1135 | # exit 1; | ||
| 1136 | my $deparse = __PACKAGE__->new("-l", "-c", "-sC"); | ||
| 1137 | my $info = $deparse->coderef2info(\&baz); | ||
| 1138 | import Data::Printer colored => 0; | ||
| 1139 | Data::Printer::p($info); | ||
| 1140 | print "\n", '=' x 30, "\n"; | ||
| 1141 | # print $deparse->indent($deparse->deparse_subname('fib')->{text}); | ||
| 1142 | # print "\n", '=' x 30, "\n"; | ||
| 1143 | # print "\n", '-' x 30, "\n"; | ||
| 1144 | while (my($key, $value) = each %{$deparse->{optree}}) { | ||
| 1145 | my $parent_op_name = 'undef'; | ||
| 1146 | if ($value->{parent}) { | ||
| 1147 | my $parent = $deparse->{optree}{$value->{parent}}; | ||
| 1148 | $parent_op_name = $parent->{op}->name if $parent->{op}; | ||
| 1149 | } | ||
| 1150 | printf("0x%x %s/%s of %s |\n%s", | ||
| 1151 | $key, $value->{op}->name, $value->{type}, | ||
| 1152 | $parent_op_name, $deparse->indent($value->{text})); | ||
| 1153 | printf " ## line %s\n", $value->{cop} ? $value->{cop}->line : 'undef'; | ||
| 1154 | print '-' x 30, "\n"; | ||
| 1155 | } | ||
| 1156 | } | ||
| 1157 | |||
| 1158 | # FIXME: | ||
| 1159 | # Different in 5.20. Go over differences to see if okay in 5.20. | ||
| 1160 | sub pp_chdir { | ||
| 1161 | my ($self, $op, $cx) = @_; | ||
| 1162 | if (($op->flags & (OPf_SPECIAL|OPf_KIDS)) == (OPf_SPECIAL|OPf_KIDS)) { | ||
| 1163 | my $kw = $self->keyword("chdir"); | ||
| 1164 | my $kid = $self->const_sv($op->first)->PV; | ||
| 1165 | my $code = $kw | ||
| 1166 | . ($cx >= 16 || $self->{'parens'} ? "($kid)" : " $kid"); | ||
| 1167 | maybe_targmy(@_, sub { $_[3] }, $code); | ||
| 1168 | } else { | ||
| 1169 | maybe_targmy(@_, \&unop, "chdir") | ||
| 1170 | } | ||
| 1171 | } | ||
| 1172 | |||
| 1173 | # Not in Perl 5.20 and presumeably < 5.20. No harm in adding to 5.20? | ||
| 1174 | *pp_ncomplement = *pp_complement; | ||
| 1175 | sub pp_scomplement { maybe_targmy(@_, \&pfixop, "~.", 21) } | ||
| 1176 | |||
| 1177 | unless (caller) { | ||
| 1178 | eval "use Data::Printer;"; | ||
| 1179 | |||
| 1180 | eval { | ||
| 1181 | our($Fileparse_fstype); | ||
| 1182 | sub fib($) { | ||
| 1183 | my $x = shift; | ||
| 1184 | return 1 if $x <= 1; | ||
| 1185 | return(fib($x-1) + fib($x-2)) | ||
| 1186 | } | ||
| 1187 | sub fileparse { | ||
| 1188 | no strict; | ||
| 1189 | # my($fullname,@suffices) = @_; | ||
| 1190 | |||
| 1191 | my $tail = ''; | ||
| 1192 | $tail = $1 . $tail; | ||
| 1193 | |||
| 1194 | # Ensure taint is propagated from the path to its pieces. | ||
| 1195 | $tail .= $taint; | ||
| 1196 | wantarray ? ($basename .= $taint, $dirpath .= $taint, $tail) | ||
| 1197 | : ($basename .= $taint); | ||
| 1198 | } | ||
| 1199 | sub baz { | ||
| 1200 | no strict; | ||
| 1201 | if ($basename =~ s/$pat//s) { | ||
| 1202 | } | ||
| 1203 | } | ||
| 1204 | }; | ||
| 1205 | |||
| 1206 | my $deparse = __PACKAGE__->new("-l", "-c"); | ||
| 1207 | my $info = $deparse->coderef2info(\&fileparse); | ||
| 1208 | # my $info = $deparse->coderef2info(\&baz); | ||
| 1209 | import Data::Printer colored => 0; | ||
| 1210 | Data::Printer::p($info); | ||
| 1211 | print "\n", '=' x 30, "\n"; | ||
| 1212 | # print $deparse->($deparse->deparse_subname('fib')->{text}); | ||
| 1213 | # print "\n", '=' x 30, "\n"; | ||
| 1214 | # print "\n", '-' x 30, "\n"; | ||
| 1215 | while (my($key, $value) = each %{$deparse->{optree}}) { | ||
| 1216 | my $parent_op_name = 'undef'; | ||
| 1217 | if ($value->{parent}) { | ||
| 1218 | my $parent = $deparse->{optree}{$value->{parent}}; | ||
| 1219 | $parent_op_name = $parent->{op}->name if $parent->{op}; | ||
| 1220 | } | ||
| 1221 | if (eval{$value->{op}->name}) { | ||
| 1222 | printf("0x%x %s/%s of %s |\n%s", | ||
| 1223 | $key, $value->{op}->name, $value->{type}, | ||
| 1224 | $parent_op_name, $deparse->{text}); | ||
| 1225 | } else { | ||
| 1226 | printf("0x%x %s of %s |\n", | ||
| 1227 | $key, $value->{type}, | ||
| 1228 | $parent_op_name); | ||
| 1229 | } | ||
| 1230 | printf " ## line %s\n", $value->{cop} ? $value->{cop}->line : 'undef'; | ||
| 1231 | print '-' x 30, "\n"; | ||
| 1232 | } | ||
| 1233 | # use B::Deparse; | ||
| 1234 | # my $deparse_old = B::Deparse->new("-l", "-sC"); | ||
| 1235 | # print $deparse_old->coderef2text(\&baz); | ||
| 1236 | } | ||
| 1237 | |||
| 1238 | 1; |