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

filter: Statementstmt 0.0% 0 / 550 Branchbran 0.0% 0 / 216 Conditioncond 0.0% 0 / 135 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 65 Podpod 0.0% 0 / 49 totaltotal 0.0% 0 / 1015 SCAR 93.0
CC
104
Cov
0%
check_proto
342.0
pure_string
182.0
re_dq
156.0
CRAP 10920.0
1 # Copyright (c) 1998-2000, 2002, 2003, 2004, 2005, 2006 Stephen McCamant.
2 # Copyright (c) 2015, 2018 Rocky Bernstein
3 # All rights reserved.
4 # This module is free software; you can redistribute and/or modify
5 # it under the same terms as Perl itself.
6  
7 # This is based on the module B::Deparse (for perl 5.20) by Stephen McCamant.
8 # It has been extended save tree structure, and is addressible
9 # by opcode address.
10  
11 # B::Parse in turn is based on the module of the same name by Malcolm Beattie,
12 # but essentially none of his code remains.
13  
14 use v5.16;
15  
16 use rlib '../..';
17  
18 package B::DeparseTree::P520;
19 use Carp;
20 use B qw(class opnumber
21 OPf_KIDS OPf_REF OPf_STACKED OPf_SPECIAL OPf_MOD
22 OPpENTERSUB_AMPER OPpSLICE
23 PMf_KEEP PMf_GLOBAL PMf_EVAL PMf_ONCE
24 SVpad_OUR SVf_FAKE SVs_RMG SVs_SMG
25 PMf_MULTILINE PMf_SINGLELINE PMf_FOLD PMf_EXTENDED);
26  
27 use B::DeparseTree::PPfns;
28 use B::DeparseTree::SyntaxTree;
29 use B::DeparseTree::PP;
30 use B::Deparse;
31  
32 # Copy unchanged functions from B::Deparse
33 *begin_is_use = *B::Deparse::begin_is_use;
34 *const_sv = *B::Deparse::const_sv;
35 *escape_extended_re = *B::Deparse::escape_extended_re;
36 *find_scope_st = *B::Deparse::find_scope_st;
37 *gv_name = *B::Deparse::gv_name;
38 *keyword = *B::Deparse::keyword;
39 *meth_rclass_sv = *B::Deparse::meth_rclass_sv;
40 *meth_sv = *B::Deparse::meth_sv;
41 *padany = *B::Deparse::padany;
42 *padname = *B::Deparse::padname;
43 *padname_sv = *B::Deparse::padname_sv;
44 *padval = *B::Deparse::padval;
45 *populate_curcvlex = *B::Deparse::populate_curcvlex;
46 *re_flags = *B::Deparse::re_flags;
47 *rv2gv_or_string = *B::Deparse::rv2gv_or_string;
48 *stash_variable = *B::Deparse::stash_variable;
49 *stash_variable_name = *B::Deparse::stash_variable_name;
50 *tr_chr = *B::Deparse::tr_chr;
51  
52 use strict;
53 use vars qw/$AUTOLOAD/;
54 use warnings ();
55 require feature;
56  
57 our(@EXPORT, @ISA);
58 our $VERSION = '3.2.0';
59  
60 @ISA = qw(Exporter);
61  
62 BEGIN {
63 # List version-specific constants here.
64 # Easiest way to keep this code portable between version looks to
65 # be to fake up a dummy constant that will never actually be true.
66 foreach (qw(OPpSORT_INPLACE OPpSORT_DESCEND OPpITER_REVERSED OPpCONST_NOVER
67 OPpPAD_STATE PMf_SKIPWHITE RXf_SKIPWHITE
68 RXf_PMf_CHARSET RXf_PMf_KEEPCOPY
69 CVf_LOCKED OPpREVERSE_INPLACE OPpSUBSTR_REPL_FIRST
70 PMf_NONDESTRUCT OPpCONST_ARYBASE OPpEVAL_BYTES)) {
71 eval { import B $_ };
72 no strict 'refs';
73 *{$_} = sub () {0} unless *{$_}{CODE};
74 }
75 }
76  
77 BEGIN { for (qw[rv2sv list]) {
78 eval "sub OP_\U$_ () { " . opnumber($_) . "}"
79 }}
80  
81 # pp_padany -- does not exist after parsing
82  
83 sub AUTOLOAD {
84 if ($AUTOLOAD =~ s/^.*::pp_//) {
85 warn "unexpected OP_".uc $AUTOLOAD;
86 } else {
87 Carp::confess "Undefined subroutine $AUTOLOAD called";
88 }
89 }
90  
91 sub DESTROY {} # Do not AUTOLOAD
92  
93 # The BEGIN {} is used here because otherwise this code isn't executed
94 # when you run B::Deparse on itself.
95 my %globalnames;
96 BEGIN { map($globalnames{$_}++, "SIG", "STDIN", "STDOUT", "STDERR", "INC",
97 "ENV", "ARGV", "ARGVOUT", "_"); }
98  
99 { no strict 'refs'; *{"pp_r$_"} = *{"pp_$_"} for qw< keys each values >; }
100  
101 sub pp_readline {
102 my $self = shift;
103 my($op, $cx) = @_;
104 my $kid = $op->first;
105 $kid = $kid->first if $kid->name eq "rv2gv"; # <$fh>
106 if (B::Deparse::is_scalar($kid)) {
107 my $body = [$self->deparse($kid, 1, $op)];
108 return info_from_list($op, $self, ['<', $body->[0]{text}, '>'], '',
109 'readline_scalar', {body=>$body});
110 }
111 return $self->unop($op, $cx, "readline");
112 }
113  
114 sub pp_rcatline {
115 my $self = shift;
116 my($op) = @_;
117 return info_from_list($op, $self, ["<", $self->gv_name($self->gv_or_padgv($op)), ">"],
118 '', 'rcatline', {});
119 }
120  
121 sub pp_smartmatch {
122 my ($self, $op, $cx) = @_;
123 if ($op->flags & OPf_SPECIAL) {
124 return $self->deparse($op->last, $cx, $op);
125 }
126 else {
127 binop(@_, "~~", 14);
128 }
129 }
130  
131 sub bin_info_join($$$$$$$) {
132 my ($self, $op, $lhs, $rhs, $mid, $sep, $type) = @_;
133 my $texts = [$lhs->{text}, $mid, $rhs->{text}];
134 return info_from_list($op, $self, $texts, ' ', $type, {})
135 }
136  
137 sub bin_info_join_maybe_parens($$$$$$$$$) {
138 my ($self, $op, $lhs, $rhs, $mid, $sep, $cx, $prec, $type) = @_;
139 my $info = bin_info_join($self, $op, $lhs, $rhs, $mid, $sep, $type);
140 $info->{text} = $self->maybe_parens($info->{text}, $cx, $prec);
141 return $info;
142 }
143  
144 sub range {
145 my $self = shift;
146 my ($op, $cx, $type) = @_;
147 my $left = $op->first;
148 my $right = $left->sibling;
149 $left = $self->deparse($left, 9, $op);
150 $right = $self->deparse($right, 9, $op);
151 return info_from_list($op, $self, [$left, $type, $right], ' ', 'range',
152 {maybe_parens => [$self, $cx, 9]});
153 }
154  
155 sub pp_substr {
156 my ($self,$op,$cx) = @_;
157 if ($op->private & OPpSUBSTR_REPL_FIRST) {
158 my $left = listop($self, $op, 7, "substr", $op->first->sibling->sibling);
159 my $right = $self->deparse($op->first->sibling, 7, $op);
160 return info_from_list($op, $self,[$left, '=', $right], ' ',
161 'substr_repl_first', {});
162 }
163 return maybe_local(@_, listop(@_, "substr"))
164 }
165 # Truncate is special because OPf_SPECIAL makes a bareword first arg
166 # be a filehandle. This could probably be better fixed in the core
167 # by moving the GV lookup into ck_truc.
168  
169 sub pp_truncate
170 {
171 my($self, $op, $cx) = @_;
172 my(@exprs);
173 my $parens = ($cx >= 5) || $self->{'parens'};
174 my $kid = $op->first->sibling;
175 my $fh;
176 if ($op->flags & OPf_SPECIAL) {
177 # $kid is an OP_CONST
178 $fh = $self->const_sv($kid)->PV;
179 } else {
180 $fh = $self->deparse($kid, 6, $op);
181 $fh = "+$fh" if not $parens and substr($fh, 0, 1) eq "(";
182 }
183 my $len = $self->deparse($kid->sibling, 6, $op);
184 my $name = $self->keyword('truncate');
185 my $opts = {body => [$fh, $len]};
186 my $args = "$fh->{text}, $len->{text}";
187 if ($parens) {
188 return info_from_list($op, $self, [$name, '(', $args, ')'], '',
189 'truncate_parens', $opts);
190 return "$name($fh, $len)";
191 } else {
192 return info_from_list($op, $self, [$name, $args], '', 'truncate', $opts);
193 }
194 }
195  
196 sub for_loop {
197 my $self = shift;
198 my($op, $cx, $parent) = @_;
199 my $init = $self->deparse($op, 1, $parent);
200 my $s = $op->sibling;
201 my $ll = $s->name eq "unstack" ? $s->sibling : $s->first->sibling;
202 return $self->loop_common($ll, $cx, $init);
203 }
204  
205 sub pp_padsv {
206 my $self = shift;
207 my($op, $cx, $forbid_parens) = @_;
208 return $self->maybe_my($op, $cx, $self->padname($op->targ),
209 $forbid_parens);
210 }
211  
212 my @threadsv_names = B::threadsv_names;
213 sub pp_threadsv {
214 my $self = shift;
215 my($op, $cx) = @_;
216 return $self->maybe_local_str($op, $cx, "\$" . $threadsv_names[$op->targ]);
217 }
218  
219 sub gv_or_padgv {
220 my $self = shift;
221 my $op = shift;
222 if (class($op) eq "PADOP") {
223 return $self->padval($op->padix);
224 } else { # class($op) eq "SVOP"
225 return $op->gv;
226 }
227 }
228  
229 sub pp_aelemfast_lex
230 {
231 my($self, $op, $cx) = @_;
232 my $name = $self->padname($op->targ);
233 $name =~ s/^@/\$/;
234 return info_from_list($op, $self, [$name, "[", ($op->private + $self->{'arybase'}), "]"],
235 '', 'pp_aelemfast_lex', {});
236 }
237  
238 sub pp_aelemfast
239 {
240 my($self, $op, $cx) = @_;
241 # optimised PADAV, pre 5.15
242 return $self->pp_aelemfast_lex(@_) if ($op->flags & OPf_SPECIAL);
243  
244 my $gv = $self->gv_or_padgv($op);
245 my($name,$quoted) = $self->stash_variable_name('@',$gv);
246 $name = $quoted ? "$name->" : '$' . $name;
247 my $i = $op->private;
248 $i -= 256 if $i > 127;
249 return info_from_list($op, $self, [$name, "[", ($op->private + $self->{'arybase'}), "]"],
250 '', 'pp_aelemfast', {});
251 }
252  
253 sub pp_rv2sv { maybe_local(@_, rv2x(@_, "\$")) }
254 sub pp_rv2hv { maybe_local(@_, rv2x(@_, "%")) }
255 sub pp_rv2gv { maybe_local(@_, rv2x(@_, "*")) }
256  
257 # skip rv2av
258 sub pp_av2arylen {
259 my $self = shift;
260 my($op, $cx) = @_;
261 if ($op->first->name eq "padav") {
262 return $self->maybe_local_str($op, $cx, '$#' . $self->padany($op->first));
263 } else {
264 return $self->maybe_local($op, $cx,
265 $self->rv2x($op->first, $cx, '$#'));
266 }
267 }
268  
269 # skip down to the old, ex-rv2cv
270 sub pp_rv2cv {
271 my ($self, $op, $cx) = @_;
272 if (!B::Deparse::null($op->first) && $op->first->name eq 'null' &&
273 $op->first->targ == OP_LIST)
274 {
275 return $self->rv2x($op->first->first->sibling, $cx, "&")
276 }
277 else {
278 return $self->rv2x($op, $cx, "")
279 }
280 }
281  
282 sub list_const($$$) {
283 my $self = shift;
284 my($op, $cx, @list) = @_;
285 my @a = map $self->const($_, 6), @list;
286 my @texts = $self->map_texts(\@a);
287 my $type = 'list_const';
288 my $prec = 6;
289 if (@texts == 0) {
290 return info_from_list($op, $self, ['(', ')'], '', 'list_const_null', {});
291 } elsif (@texts == 1) {
292 return info_from_text($op, $self, $texts[0], 'list_const_one',
293 {body => \@a});
294 } elsif ( @texts > 2 and !grep(!/^-?\d+$/, @texts)) {
295 # collapse (-1,0,1,2) into (-1..2)
296 my ($s, $e) = @texts[0,-1];
297 my $i = $s;
298 unless (grep $i++ != $_, @texts) {
299 @texts = ($s, '..', $e);
300 $type = 'list_const_range';
301 $prec = 9;
302 }
303 }
304 return info_from_list('const', $self, \@texts, '', $type,
305 {maybe_parens => [$self, $cx, $prec]});
306 }
307  
308 sub pp_rv2av {
309 my $self = shift;
310 my($op, $cx) = @_;
311 my $kid = $op->first;
312 if ($kid->name eq "const") { # constant list
313 my $av = $self->const_sv($kid);
314 return $self->list_const($kid, $cx, $av->ARRAY);
315 } else {
316 return $self->maybe_local($op, $cx, $self->rv2x($op, $cx, "\@"));
317 }
318 }
319  
320 sub elem_or_slice_array_name
321 {
322 my $self = shift;
323 my ($array, $left, $padname, $allow_arrow) = @_;
324  
325 if ($array->name eq $padname) {
326 return $self->padany($array);
327 } elsif (B::Deparse::is_scope($array)) { # ${expr}[0]
328 return "{" . $self->deparse($array, 0) . "}";
329 } elsif ($array->name eq "gv") {
330 ($array, my $quoted) =
331 $self->stash_variable_name(
332 $left eq '[' ? '@' : '%', $self->gv_or_padgv($array)
333 );
334 if (!$allow_arrow && $quoted) {
335 # This cannot happen.
336 die "Invalid variable name $array for slice";
337 }
338 return $quoted ? "$array->" : $array;
339 } elsif (!$allow_arrow || B::Deparse::is_scalar $array) {
340 # $x[0], $$x[0], ...
341 return $self->deparse($array, 24)->{text};
342 } else {
343 return undef;
344 }
345 }
346  
347 sub elem_or_slice_single_index($$)
348 {
349 my ($self, $idx, $parent) = @_;
350  
351 my $idx_info = $self->deparse($idx, 1, $parent);
352 my $idx_str = $idx_info->{text};
353  
354 # Outer parens in an array index will confuse perl
355 # if we're interpolating in a regular expression, i.e.
356 # /$x$foo[(-1)]/ is *not* the same as /$x$foo[-1]/
357 #
358 # If $self->{parens}, then an initial '(' will
359 # definitely be paired with a final ')'. If
360 # !$self->{parens}, the misleading parens won't
361 # have been added in the first place.
362 #
363 # [You might think that we could get "(...)...(...)"
364 # where the initial and final parens do not match
365 # each other. But we can't, because the above would
366 # only happen if there's an infix binop between the
367 # two pairs of parens, and *that* means that the whole
368 # expression would be parenthesized as well.]
369 #
370 $idx_str =~ s/^\((.*)\)$/$1/ if $self->{'parens'};
371  
372 # Hash-element braces will autoquote a bareword inside themselves.
373 # We need to make sure that C<$hash{warn()}> doesn't come out as
374 # C<$hash{warn}>, which has a quite different meaning. Currently
375 # B::Deparse will always quote strings, even if the string was a
376 # bareword in the original (i.e. the OPpCONST_BARE flag is ignored
377 # for constant strings.) So we can cheat slightly here - if we see
378 # a bareword, we know that it is supposed to be a function call.
379 #
380 $idx_str =~ s/^([A-Za-z_]\w*)$/$1()/;
381  
382 return info_from_text($idx_info->{op}, $self, $idx_str,
383 'elem_or_slice_single_index',
384 {body => [$idx_info]});
385 }
386  
387 sub pp_gelem
388 {
389 my($self, $op, $cx) = @_;
390 my($glob, $part) = ($op->first, $op->last);
391 $glob = $glob->first; # skip rv2gv
392 $glob = $glob->first if $glob->name eq "rv2gv"; # this one's a bug
393 my $scope = B::Deparse::is_scope($glob);
394 $glob = $self->deparse($glob, 0);
395 $part = $self->deparse($part, 1);
396 return "*" . ($scope ? "{$glob}" : $glob) . "{$part}";
397 }
398  
399 sub pp_lslice
400 {
401 my ($self, $op, $cs) = @_;
402 my $idx = $op->first;
403 my $list = $op->last;
404 my(@elems, $kid);
405 my $list_info = $self->deparse($list, 1, $op);
406 my $idx_info = $self->deparse($idx, 1, $op);
407 return info_from_list($op, $self, ['(', $list_info->{text}, ')', '[', $idx_info->{text}, ']'],
408 '', 'lslice', {body=>[$list_info, $idx_info]});
409 }
410  
411 sub _method
412 {
413 my($self, $op, $cx) = @_;
414 my @other_ops = ($op->first);
415 my $kid = $op->first->sibling; # skip pushmark
416 my($meth, $obj, @exprs);
417 if ($kid->name eq "list" and B::Deparse::want_list $kid) {
418 # When an indirect object isn't a bareword but the args are in
419 # parens, the parens aren't part of the method syntax (the LLAFR
420 # doesn't apply), but they make a list with OPf_PARENS set that
421 # doesn't get flattened by the append_elem that adds the method,
422 # making a (object, arg1, arg2, ...) list where the object
423 # usually is. This can be distinguished from
424 # '($obj, $arg1, $arg2)->meth()' (which is legal if $arg2 is an
425 # object) because in the later the list is in scalar context
426 # as the left side of -> always is, while in the former
427 # the list is in list context as method arguments always are.
428 # (Good thing there aren't method prototypes!)
429 $meth = $kid->sibling;
430 push @other_ops, $kid->first;
431 $kid = $kid->first->sibling; # skip pushmark
432 $obj = $kid;
433 $kid = $kid->sibling;
434 for (; not B::Deparse::null $kid; $kid = $kid->sibling) {
435 push @exprs, $kid;
436 }
437 } else {
438 $obj = $kid;
439 $kid = $kid->sibling;
440 for (; !B::Deparse::null ($kid->sibling) && $kid->name!~/^method(?:_named)?\z/;
441 $kid = $kid->sibling) {
442 push @exprs, $kid
443 }
444 $meth = $kid;
445 }
446  
447 if ($meth->name eq "method_named") {
448 $meth = $self->const_sv($meth)->PV;
449 } else {
450 $meth = $meth->first;
451 if ($meth->name eq "const") {
452 # As of 5.005_58, this case is probably obsoleted by the
453 # method_named case above
454 $meth = $self->const_sv($meth)->PV; # needs to be bare
455 }
456 }
457  
458 return {
459 method => $meth,
460 variable_method => ref($meth),
461 object => $obj,
462 args => \@exprs,
463 other_ops => \@other_ops
464 }, $cx;
465 }
466  
467 sub e_method {
468 my ($self, $op, $minfo, $cx) = @_;
469 my $obj = $self->deparse($minfo->{object}, 24, $op);
470 my @body = ($obj);
471 my $other_ops = $minfo->{other_ops};
472  
473 my $meth_name = $minfo->{method};
474 my $meth_info;
475 if ($minfo->{variable_method}) {
476 $meth_info = $self->deparse($meth_name, 1, $op);
477 push @body, $meth_info;
478 }
479 my @args = map { $self->deparse($_, 6, $op) } @{$minfo->{args}};
480 push @body, @args;
481 my @args_texts = map $_->{text}, @args;
482 my $args = join(", ", @args_texts);
483  
484 my $opts = {other_ops => $other_ops};
485 my @texts = ();
486 my $type;
487  
488  
489 my $meth_object = $meth_info ? defined($meth_info) : $meth_name;
490 if ($minfo->{object}->name eq 'scope' && B::Deparse::want_list $minfo->{object}) {
491 # method { $object }
492 # This must be deparsed this way to preserve list context
493 # of $object.
494 my $need_paren = $cx >= 6;
495 if ($need_paren) {
496 @texts = ('(', $meth_object, substr($obj,2),
497 $args, ')');
498 $type = 'e_method list ()';
499 } else {
500 @texts = ($meth_object, substr($obj,2), $args);
501 $type = 'e_method list, no ()';
502 }
503 return info_from_list($op, $self, \@texts, '', $type, $opts);
504 }
505  
506 if (length $args) {
507 @texts = ($obj, '->', $meth_object, '(', $args, ')');
508 $type = 'e_method -> ()';
509 } else {
510 @texts = ($obj, '->', $meth_object);
511 $type = 'e_method -> no ()';
512 }
513 return info_from_list($op, $self, \@texts, '', $type, $opts);
514 }
515  
516 # returns "&" and the argument bodies if the prototype doesn't match the args,
517 # or ("", $args_after_prototype_demunging) if it does.
518 sub check_proto {
519 my $self = shift;
520 my $op = shift;
521 return ('&', []) if $self->{'noproto'};
522 my($proto, @args) = @_;
523 my($arg, $real);
524 my $doneok = 0;
525 my @reals;
526 # An unbackslashed @ or % gobbles up the rest of the args
527 1 while $proto =~ s/(?<!\\)([@%])[^\]]+$/$1/;
528 $proto =~ s/^\s*//;
529 while ($proto) {
530 $proto =~ s/^(\\?[\$\@&%*_]|\\\[[\$\@&%*]+\]|;)\s*//;
531 my $chr = $1;
532 if ($chr eq "") {
533 return ('&', []) if @args;
534 } elsif ($chr eq ";") {
535 $doneok = 1;
536 } elsif ($chr eq "@" or $chr eq "%") {
537 push @reals, map($self->deparse($_, 6), @args, $op);
538 @args = ();
539 } else {
540 $arg = shift @args;
541 last unless $arg;
542 if ($chr eq "\$" || $chr eq "_") {
543 if (B::Deparse::want_scalar $arg) {
544 push @reals, $self->deparse($arg, 6, $op);
545 } else {
546 return ('&', []);
547 }
548 } elsif ($chr eq "&") {
549 if ($arg->name =~ /^(s?refgen|undef)$/) {
550 push @reals, $self->deparse($arg, 6, $op);
551 } else {
552 return ('&', []);
553 }
554 } elsif ($chr eq "*") {
555 if ($arg->name =~ /^s?refgen$/
556 and $arg->first->first->name eq "rv2gv")
557 {
558 $real = $arg->first->first; # skip refgen, null
559 if ($real->first->name eq "gv") {
560 push @reals, $self->deparse($real, 6, $op);
561 } else {
562 push @reals, $self->deparse($real->first, 6, $op);
563 }
564 } else {
565 return ('&', []);
566 }
567 } elsif (substr($chr, 0, 1) eq "\\") {
568 $chr =~ tr/\\[]//d;
569 if ($arg->name =~ /^s?refgen$/ and
570 !B::Deparse::null($real = $arg->first) and
571 ($chr =~ /\$/ && B::Deparse::is_scalar($real->first)
572 or ($chr =~ /@/
573 && class($real->first->sibling) ne 'NULL'
574 && $real->first->sibling->name
575 =~ /^(rv2|pad)av$/)
576 or ($chr =~ /%/
577 && class($real->first->sibling) ne 'NULL'
578 && $real->first->sibling->name
579 =~ /^(rv2|pad)hv$/)
580 #or ($chr =~ /&/ # This doesn't work
581 # && $real->first->name eq "rv2cv")
582 or ($chr =~ /\*/
583 && $real->first->name eq "rv2gv")))
584 {
585 push @reals, $self->deparse($real, 6, $op);
586 } else {
587 return ('&', []);
588 }
589 }
590 }
591 }
592 return ('&', []) if $proto and !$doneok; # too few args and no ';'
593 return ('&', []) if @args; # too many args
594 return ('', \@reals);
595 }
596  
597 sub pp_enterwrite { unop(@_, "write") }
598  
599 my $max_prec;
600 BEGIN { $max_prec = int(0.999 + 8*length(pack("F", 42))*log(2)/log(10)); }
601  
602 # Split a floating point number into an integer mantissa and a binary
603 # exponent. Assumes you've already made sure the number isn't zero or
604 # some weird infinity or NaN.
605 sub split_float {
606 my($f) = @_;
607 my $exponent = 0;
608 if ($f == int($f)) {
609 while ($f % 2 == 0) {
610 $f /= 2;
611 $exponent++;
612 }
613 } else {
614 while ($f != int($f)) {
615 $f *= 2;
616 $exponent--;
617 }
618 }
619 my $mantissa = sprintf("%.0f", $f);
620 return ($mantissa, $exponent);
621 }
622  
623 sub pp_const {
624 my $self = shift;
625 my($op, $cx) = @_;
626 if ($op->private & OPpCONST_ARYBASE) {
627 return info_from_text($op, $self, '$[', 'const_ary', {});
628 }
629 # if ($op->private & OPpCONST_BARE) { # trouble with '=>' autoquoting
630 # return $self->const_sv($op)->PV;
631 # }
632 my $sv = $self->const_sv($op);
633 return $self->const($sv, $cx);;
634 }
635  
636 # OP_STRINGIFY is a listop, but it only ever has one arg
637 sub pp_stringify { maybe_targmy(@_, \&dquote) }
638  
639 # Like dq(), but different
640 sub re_dq {
641 my $self = shift;
642 my ($op, $extended) = @_;
643 my ($re_dq_info, $fmt);
644  
645 my $type = $op->name;
646 my ($re, @texts, $type);
647 my $opts = {};
648 if ($type eq "const") {
649 return info_from_text($op, $self, '$[', 're_dq_const', {})
650 if $op->private & OPpCONST_ARYBASE;
651 my $unbacked = B::Deparse::re_unback($self->const_sv($op)->as_string);
652 return B::Deparse::re_uninterp_extended(escape_extended_re($unbacked))
653 if $extended;
654 return B::Deparse::re_uninterp(B::Deparse::escape_str($unbacked));
655 } elsif ($type eq "concat") {
656 my $first = $self->re_dq($op->first, $extended);
657 my $last = $self->re_dq($op->last, $extended);
658 return B::Deparse::re_dq_disambiguate($first, $last);
659 } elsif ($type eq "uc") {
660 $re_dq_info = $self->re_dq($op->first->sibling, $extended);
661 $fmt = '\U%c\E';
662 $type .= ' uc';
663 } elsif ($type eq "lc") {
664 $re_dq_info = $self->re_dq($op->first->sibling, $extended);
665 $fmt = '\L%c\E';
666 $type .= ' lc';
667 } elsif ($type eq "ucfirst") {
668 $re_dq_info = $self->re_dq($op->first->sibling, $extended);
669 $fmt = '\u%c';
670 $type .= ' ucfirst';
671 } elsif ($type eq "lcfirst") {
672 $re_dq_info = $self->re_dq($op->first->sibling, $extended);
673 $fmt = '\u%c';
674 $type .= ' lcfirst';
675 } elsif ($type eq "quotemeta") {
676 $re = $self->re_dq($op->first->sibling, $extended);
677 @texts = ['\Q', $re->{text},'\E'];
678 $type .= ' quotemeta';
679 } elsif ($type eq "fc") {
680 $re = $self->re_dq($op->first->sibling, $extended);
681 @texts = ['\F', $re->{text},'\E'];
682 $type .= ' fc';
683 } elsif ($type eq "join") {
684 return $self->deparse($op->last, 26, $op); # was join($", @ary)
685 } else {
686 my $info = $self->deparse($op, 26, $op);
687 $info->{type} = 're_dq';
688 $info->{text} =~ s/^\$([(|)])\z/\${$1}/; # $( $| $) need braces
689 return $info;
690 }
691 $opts->{body} = [$re];
692 return info_from_list($op, $self, \@texts, '', $type, $opts);
693 }
694  
695 sub pure_string {
696 my ($self, $op) = @_;
697 return 0 if B::Deparse::null $op;
698 my $type = $op->name;
699  
700 if ($type eq 'const' || $type eq 'av2arylen') {
701 return 1;
702 }
703 elsif ($type =~ /^(?:[ul]c(first)?|fc)$/ || $type eq 'quotemeta') {
704 return $self->pure_string($op->first->sibling);
705 }
706 elsif ($type eq 'join') {
707 my $join_op = $op->first->sibling; # Skip pushmark
708 return 0 unless $join_op->name eq 'null' && $join_op->targ == OP_RV2SV;
709  
710 my $gvop = $join_op->first;
711 return 0 unless $gvop->name eq 'gvsv';
712 return 0 unless '"' eq $self->gv_name($self->gv_or_padgv($gvop));
713  
714 return 0 unless ${$join_op->sibling} eq ${$op->last};
715 return 0 unless $op->last->name =~ /^(?:[ah]slice|(?:rv2|pad)av)$/;
716 }
717 elsif ($type eq 'concat') {
718 return $self->pure_string($op->first)
719 && $self->pure_string($op->last);
720 }
721 elsif (B::Deparse::is_scalar($op) || $type =~ /^[ah]elem$/) {
722 return 1;
723 }
724 elsif ($type eq "null" and $op->can('first') and not B::Deparse::null $op->first and
725 ($op->first->name eq "null" and $op->first->can('first')
726 and not B::Deparse::null $op->first->first and
727 $op->first->first->name eq "aelemfast"
728 or
729 $op->first->name =~ /^aelemfast(?:_lex)?\z/
730 )) {
731 return 1;
732 }
733 else {
734 return 0;
735 }
736  
737 return 1;
738 }
739  
740 sub regcomp
741 {
742 my($self, $op, $cx, $extended) = @_;
743 my @other_ops = ();
744 my $kid = $op->first;
745 if ($kid->name eq "regcmaybe") {
746 push @other_ops, $kid;
747 $kid = $kid->first;
748 }
749 if ($kid->name eq "regcreset") {
750 push @other_ops, $kid;
751 $kid = $kid->first;
752 }
753 if ($kid->name eq "null" and !B::Deparse::null($kid->first)
754 and $kid->first->name eq 'pushmark') {
755 my $str = '';
756 push(@other_ops, $kid);
757 $kid = $kid->first->sibling;
758 my @body = ();
759 while (!B::Deparse::null($kid)) {
760 my $first = $str;
761 my $last = $self->re_dq($kid, $extended);
762 push @body, $last;
763 push(@other_ops, $kid);
764 $str = B::Deparse::re_dq_disambiguate($first,
765 $self->info2str($last));
766 $kid = $kid->sibling;
767 }
768 return (info_from_text($op, $self, $str, 'regcomp',
769 {other_ops => \@other_ops,
770 body => \@body}), 1);
771 }
772  
773 if ($self->pure_string($kid)) {
774 my $info = $self->re_dq($kid, $extended);
775 my @kid_ops = $info->{other_ops} ? @{$info->{other_ops}} : ();
776 push @other_ops, @kid_ops;
777 $info->{other_ops} = \@other_ops;
778 return ($info, 1);
779 }
780 return ($self->deparse($kid, $cx, $op), 0, $op);
781 }
782  
783 sub re_flags
784 {
785 my ($self, $op) = @_;
786 my $flags = '';
787 my $pmflags = $op->pmflags;
788 $flags .= "g" if $pmflags & PMf_GLOBAL;
789 $flags .= "i" if $pmflags & PMf_FOLD;
790 $flags .= "m" if $pmflags & PMf_MULTILINE;
791 $flags .= "o" if $pmflags & PMf_KEEP;
792 $flags .= "s" if $pmflags & PMf_SINGLELINE;
793 $flags .= "x" if $pmflags & PMf_EXTENDED;
794 $flags .= "p" if $pmflags & RXf_PMf_KEEPCOPY;
795 if (my $charset = $pmflags & RXf_PMf_CHARSET) {
796 # Hardcoding this is fragile, but B does not yet export the
797 # constants we need.
798 $flags .= qw(d l u a aa)[$charset >> 5]
799 }
800 # The /d flag is indicated by 0; only show it if necessary.
801 elsif ($self->{hinthash} and
802 $self->{hinthash}{reflags_charset}
803 || $self->{hinthash}{feature_unicode}
804 or $self->{hints} & $feature::hint_mask
805 && ($self->{hints} & $feature::hint_mask)
806 != $feature::hint_mask
807 && do {
808 $self->{hints} & $feature::hint_uni8bit;
809 }
810 ) {
811 $flags .= 'd';
812 }
813 $flags;
814 }
815  
816 sub pp_match { matchop(@_, "m", "/") }
817 sub pp_pushre { matchop(@_, "m", "/") }
818 sub pp_qr { matchop(@_, "qr", "") }
819  
820 sub pp_split
821 {
822 my($self, $op, $cx) = @_;
823 my($kid, @exprs, $ary_info, $expr);
824 my $ary = '';
825 my @body = ();
826 my @other_ops = ();
827 $kid = $op->first;
828  
829 # For our kid (an OP_PUSHRE), pmreplroot is never actually the
830 # root of a replacement; it's either empty, or abused to point to
831 # the GV for an array we split into (an optimization to save
832 # assignment overhead). Depending on whether we're using ithreads,
833 # this OP* holds either a GV* or a PADOFFSET. Luckily, B.xs
834 # figures out for us which it is.
835 my $replroot = $kid->pmreplroot;
836 my $gv = 0;
837 if (ref($replroot) eq "B::GV") {
838 $gv = $replroot;
839 } elsif (!ref($replroot) and $replroot > 0) {
840 $gv = $self->padval($replroot);
841 }
842 $ary = $self->stash_variable('@', $self->gv_name($gv), $cx) if $gv;
843  
844 for (; !B::Deparse::null($kid); $kid = $kid->sibling) {
845 push @exprs, $self->deparse($kid, 6, $op);
846 }
847  
848 my $opts = {body => \@exprs};
849  
850 my @args_texts = map $_->{text}, @exprs;
851 # handle special case of split(), and split(' ') that compiles to /\s+/
852 # Under 5.10, the reflags may be undef if the split regexp isn't a constant
853 # Under 5.17.5-5.17.9, the special flag is on split itself.
854 $kid = $op->first;
855 if ( $op->flags & OPf_SPECIAL ) {
856 $exprs[0]->{text} = "' '";
857 }
858  
859 my $sep = '';
860 my $type;
861 my @expr_texts;
862 if ($ary) {
863 @expr_texts = ("$ary", '=', join(', ', @args_texts));
864 $sep = ' ';
865 $type = 'split_array';
866 $opts->{maybe_parens} = [$self, $cx, 7];
867 } else {
868 @expr_texts = ('split', '(', join(', ', @args_texts), ')');
869 $type = 'split';
870  
871 }
872 return info_from_list($op, $self, \@expr_texts, $sep, $type, $opts);
873 }
874  
875 # Kind of silly, but we prefer, subst regexp flags joined together to
876 # make words. For example: s/a/b/xo => s/a/b/ox
877  
878 # oxime -- any of various compounds obtained chiefly by the action of
879 # hydroxylamine on aldehydes and ketones and characterized by the
880 # bivalent grouping C=NOH [Webster's Tenth]
881  
882 my %substwords;
883 map($substwords{join "", sort split //, $_} = $_, 'ego', 'egoism', 'em',
884 'es', 'ex', 'exes', 'gee', 'go', 'goes', 'ie', 'ism', 'iso', 'me',
885 'meese', 'meso', 'mig', 'mix', 'os', 'ox', 'oxime', 'see', 'seem',
886 'seg', 'sex', 'sig', 'six', 'smog', 'sog', 'some', 'xi', 'rogue',
887 'sir', 'rise', 'smore', 'more', 'seer', 'rome', 'gore', 'grim', 'grime',
888 'or', 'rose', 'rosie');
889  
890 unless (caller) {
891 eval "use Data::Printer;";
892  
893 eval {
894 our($Fileparse_fstype);
895 sub fib($) {
896 my $x = shift;
897 return 1 if $x <= 1;
898 return(fib($x-1) + fib($x-2))
899 }
900 sub fileparse {
901 no strict;
902 # my($fullname,@suffices) = @_;
903  
904 my $tail = '';
905 $tail = $1 . $tail;
906  
907 # Ensure taint is propagated from the path to its pieces.
908 $tail .= $taint;
909 wantarray ? ($basename .= $taint, $dirpath .= $taint, $tail)
910 : ($basename .= $taint);
911 }
912 sub baz {
913 no strict;
914 if ($basename =~ s/$pat//s) {
915 }
916 }
917 };
918  
919 my $deparse = __PACKAGE__->new("-l", "-c");
920 my $info = $deparse->coderef2info(\&fileparse);
921 # my $info = $deparse->coderef2info(\&baz);
922 import Data::Printer colored => 0;
923 Data::Printer::p($info);
924 print "\n", '=' x 30, "\n";
925 # print $deparse->($deparse->deparse_subname('fib')->{text});
926 # print "\n", '=' x 30, "\n";
927 # print "\n", '-' x 30, "\n";
928 my $parent_op_name;
929 while (my($key, $value) = each %{$deparse->{optree}}) {
930  
931 if ($value->{parent}) {
932 my $parent = $deparse->{optree}{$value->{parent}};
933 $parent_op_name = $parent->{op}->name if $parent->{op};
934 }
935 if (eval{$value->{op}->name}) {
936 printf("0x%x %s/%s of %s |\n%s",
937 $key, $value->{op}->name, $value->{type},
938 $parent_op_name, $deparse->{text});
939 } else {
940 printf("0x%x %s of %s |\n",
941 $key, $value->{type},
942 $parent_op_name);
943 }
944 printf " ## line %s\n", $value->{cop} ? $value->{cop}->line : 'undef';
945 print '-' x 30, "\n";
946 }
947 # use B::Deparse;
948 # my $deparse_old = B::Deparse->new("-l", "-sC");
949 # print $deparse_old->coderef2text(\&baz);
950 }
951  
952 1;