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

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