blib/lib/B/DeparseTree/P526.pm untested

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