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

filter: Statementstmt 0.0% 0 / 1990 Branchbran 0.0% 0 / 794 Conditioncond 0.0% 0 / 486 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 385 Podpod 0.0% 0 / 368 totaltotal 0.0% 0 / 4023 CC 393 SCAR 45.9
CC
393
Cov
0%
begin_is_use
600.0
ambient_pragmas
506.0
loop_common
462.0
CRAP 98.1
1 # B::DeparseTree::P520.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.20) 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 # For now 5.18 and 5.20 are the same. If in the future they should be
16 # different, # we can deal with that here.
17 use v5.16;
18  
19 use rlib '../..';
20  
21 package B::DeparseTree::P520;
22 use Carp;
23 use B qw(class opnumber
24 OPf_KIDS OPf_REF OPf_STACKED OPf_SPECIAL OPf_MOD
25 OPf_WANT OPf_WANT_SCALAR OPf_WANT_LIST
26 OPpLVAL_INTRO OPpOUR_INTRO OPpENTERSUB_AMPER OPpSLICE OPpCONST_BARE
27 OPpSORT_NUMERIC OPpSORT_INTEGER
28 OPpSORT_REVERSE
29 OPpTRANS_SQUASH OPpTRANS_DELETE OPpTRANS_COMPLEMENT OPpTARGET_MY
30 PMf_KEEP PMf_GLOBAL PMf_CONTINUE PMf_EVAL PMf_ONCE
31 SVpad_OUR SVf_FAKE SVs_RMG SVs_SMG
32 PMf_MULTILINE PMf_SINGLELINE PMf_FOLD PMf_EXTENDED);
33  
34 use B::DeparseTree::Common;
35 use B::DeparseTree::PP;
36  
37 use strict;
38 use vars qw/$AUTOLOAD/;
39 use warnings ();
40 require feature;
41  
42 our($VERSION, @EXPORT, @ISA);
43 our $VERSION = '2.0';
44  
45 @ISA = qw(Exporter B::DeparseTree::Common);
46 @EXPORT = qw(compile);
47  
48 BEGIN {
49 # List version-specific constants here.
50 # Easiest way to keep this code portable between version looks to
51 # be to fake up a dummy constant that will never actually be true.
52 foreach (qw(OPpSORT_INPLACE OPpSORT_DESCEND OPpITER_REVERSED OPpCONST_NOVER
53 OPpPAD_STATE PMf_SKIPWHITE RXf_SKIPWHITE
54 RXf_PMf_CHARSET RXf_PMf_KEEPCOPY
55 CVf_LOCKED OPpREVERSE_INPLACE OPpSUBSTR_REPL_FIRST
56 PMf_NONDESTRUCT OPpCONST_ARYBASE OPpEVAL_BYTES)) {
57 eval { import B $_ };
58 no strict 'refs';
59 *{$_} = sub () {0} unless *{$_}{CODE};
60 }
61 }
62  
63 # See older source for inline comments recording changes prior to 2.0
64  
65 # In order to test modulie, we run this over Perl test suite.
66 # Then we run the test on the deparsed code. Slick, eh?
67  
68 # Here are B::DeparseTree failure notes:
69  
70 # Current test.deparse failures
71 # comp/hints 6 - location of BEGIN blocks wrt. block openings
72 # run/switchI 1 - missing -I switches entirely
73 # perl -Ifoo -e 'print @INC'
74 # op/caller 2 - warning mask propagates backwards before warnings::register
75 # 'use warnings; BEGIN {${^WARNING_BITS} eq "U"x12;} use warnings::register'
76 # op/getpid 2 - can't assign to shared my() declaration (threads only)
77 # 'my $x : shared = 5'
78 # op/override 7 - parens on overridden require change v-string interpretation
79 # 'BEGIN{*CORE::GLOBAL::require=sub {}} require v5.6'
80 # c.f. 'BEGIN { *f = sub {0} }; f 2'
81 # op/pat 774 - losing Unicode-ness of Latin1-only strings
82 # 'use charnames ":short"; $x="\N{latin:a with acute}"'
83 # op/recurse 12 - missing parens on recursive call makes it look like method
84 # 'sub f { f($x) }'
85 # op/subst 90 - inconsistent handling of utf8 under "use utf8"
86 # op/taint 29 - "use re 'taint'" deparsed in the wrong place wrt. block open
87 # op/tiehandle compile - "use strict" deparsed in the wrong place
88 # uni/tr_ several
89 # ext/B/t/xref 11 - line numbers when we add newlines to one-line subs
90 # ext/Data/Dumper/t/dumper compile
91 # ext/DB_file/several
92 # ext/Encode/several
93 # ext/Ernno/Errno warnings
94 # ext/IO/lib/IO/t/io_sel 23
95 # ext/PerlIO/t/encoding compile
96 # ext/POSIX/t/posix 6
97 # ext/Socket/Socket 8
98 # ext/Storable/t/croak compile
99 # lib/Attribute/Handlers/t/multi compile
100 # lib/bignum/ several
101 # lib/charnames 35
102 # lib/constant 32
103 # lib/English 40
104 # lib/ExtUtils/t/bytes 4
105 # lib/File/DosGlob compile
106 # lib/Filter/Simple/t/data 1
107 # lib/Math/BigInt/t/constant 1
108 # lib/Net/t/config Deparse-warning
109 # lib/overload compile
110 # lib/Switch/ several
111 # lib/Symbol 4
112 # lib/Test/Simple several
113 # lib/Term/Complete
114 # lib/Tie/File/t/29_downcopy 5
115 # lib/vars 22
116  
117 # Object fields:
118 #
119 # in_coderef2text:
120 # True when deparsing via $deparse->coderef2text; false when deparsing the
121 # main program.
122 #
123 # avoid_local:
124 # (local($a), local($b)) and local($a, $b) have the same internal
125 # representation but the short form looks better. We notice we can
126 # use a large-scale local when checking the list, but need to prevent
127 # individual locals too. This hash holds the addresses of OPs that
128 # have already had their local-ness accounted for. The same thing
129 # is done with my().
130 #
131 # curcv:
132 # CV for current sub (or main program) being deparsed
133 #
134 # curcvlex:
135 # Cached hash of lexical variables for curcv: keys are
136 # names prefixed with "m" or "o" (representing my/our), and
137 # each value is an array with two elements indicating the cop_seq
138 # of scopes in which a var of that name is valid and a third ele-
139 # ment referencing the pad name.
140 #
141 # curcop:
142 # COP for statement being deparsed
143 #
144 # curstash:
145 # name of the current package for deparsed code
146 #
147 # subs_todo:
148 # array of [cop_seq, CV, is_format?, name] for subs and formats we still
149 # want to deparse. The fourth element is a pad name thingy for lexical
150 # subs or a string for special blocks. For other subs, it is undef. For
151 # lexical subs, CV may be undef, indicating a stub declaration.
152 #
153 # protos_todo:
154 # as above, but [name, prototype] for subs that never got a GV
155 #
156 # subs_done, forms_done:
157 # keys are addresses of GVs for subs and formats we've already
158 # deparsed (or at least put into subs_todo)
159 #
160 # subs_declared
161 # keys are names of subs for which we've printed declarations.
162 # That means we can omit parentheses from the arguments. It also means we
163 # need to put CORE:: on core functions of the same name.
164 #
165 # subs_deparsed
166 # Keeps track of fully qualified names of all deparsed subs.
167 #
168 # in_subst_repl
169 # True when deparsing the replacement part of a substitution.
170 #
171 # in_refgen
172 # True when deparsing the argument to \.
173 #
174 # parens: -p
175 # linenums: -l
176 # opaddr: -a
177 # unquote: -q
178 # use_tabs: -sT
179 # ex_const: -sv
180  
181 # A little explanation of how precedence contexts and associativity
182 # work:
183 #
184 # deparse() calls each per-op subroutine with an argument $cx (short
185 # for context, but not the same as the cx* in the perl core), which is
186 # a number describing the op's parents in terms of precedence, whether
187 # they're inside an expression or at statement level, etc. (see
188 # chart below). When ops with children call deparse on them, they pass
189 # along their precedence. Fractional values are used to implement
190 # associativity ('($x + $y) + $z' => '$x + $y + $y') and related
191 # parentheses hacks. The major disadvantage of this scheme is that
192 # it doesn't know about right sides and left sides, so say if you
193 # assign a listop to a variable, it can't tell it's allowed to leave
194 # the parens off the listop.
195  
196 # Precedences:
197 # 26 [TODO] inside interpolation context ("")
198 # 25 left terms and list operators (leftward)
199 # 24 left ->
200 # 23 nonassoc ++ --
201 # 22 right **
202 # 21 right ! ~ \ and unary + and -
203 # 20 left =~ !~
204 # 19 left * / % x
205 # 18 left + - .
206 # 17 left << >>
207 # 16 nonassoc named unary operators
208 # 15 nonassoc < > <= >= lt gt le ge
209 # 14 nonassoc == != <=> eq ne cmp
210 # 13 left &
211 # 12 left | ^
212 # 11 left &&
213 # 10 left ||
214 # 9 nonassoc .. ...
215 # 8 right ?:
216 # 7 right = += -= *= etc.
217 # 6 left , =>
218 # 5 nonassoc list operators (rightward)
219 # 4 right not
220 # 3 left and
221 # 2 left or xor
222 # 1 statement modifiers
223 # 0.5 statements, but still print scopes as do { ... }
224 # 0 statement level
225 # -1 format body
226  
227 # Nonprinting characters with special meaning:
228 # \cS - steal parens (see maybe_parens_unop)
229 # \n - newline and indent
230 # \t - increase indent
231 # \b - decrease indent ('outdent')
232 # \f - flush left (no indent)
233 # \cK - kill following semicolon, if any
234  
235 # Semicolon handling:
236 # - Individual statements are not deparsed with trailing semicolons.
237 # (If necessary, \cK is tacked on to the end.)
238 # - Whatever code joins statements together or emits them (lineseq,
239 # scopeop, deparse_root) is responsible for adding semicolons where
240 # necessary.
241 # - use statements are deparsed with trailing semicolons because they are
242 # immediately concatenated with the following statement.
243 # - indent() removes semicolons wherever it sees \cK.
244  
245  
246 BEGIN { for (qw[ const stringify rv2sv list glob pushmark null]) {
247 eval "sub OP_\U$_ () { " . opnumber($_) . "}"
248 }}
249  
250 sub deparse_format($$$)
251 {
252 my ($self, $form, $parent) = @_;
253 my @texts;
254 local($self->{'curcv'}) = $form;
255 local($self->{'curcvlex'});
256 local($self->{'in_format'}) = 1;
257 local(@$self{qw'curstash warnings hints hinthash'})
258 = @$self{qw'curstash warnings hints hinthash'};
259 my $op = $form->ROOT;
260 local $B::overlay = {};
261 $self->pessimise($op, $form->START);
262 my $info = {
263 op => $op,
264 parent => $parent,
265 cop => $self->{'curcop'}
266 };
267 $self->{optree}{$$op} = $info;
268  
269 if ($op->first->name eq 'stub' || $op->first->name eq 'nextstate') {
270 my $info->{text} = "\f.";
271 return $info;
272 }
273  
274 $op->{other_ops} = [$op->first];
275 $op = $op->first->first; # skip leavewrite, lineseq
276 my $kid;
277 while (not null $op) {
278 push @{$op->{other_ops}}, $op;
279 $op = $op->sibling; # skip nextstate
280 my @body;
281 push @{$op->{other_ops}}, $op->first;
282 $kid = $op->first->sibling; # skip a pushmark
283 push @texts, "\f".$self->const_sv($kid)->PV;
284 push @{$op->{other_ops}}, $kid;
285 $kid = $kid->sibling;
286 for (; not null $kid; $kid = $kid->sibling) {
287 push @body, $self->deparse($kid, -1, $op);
288 $body[-1] =~ s/;\z//;
289 }
290 push @texts, "\f".join(", ", @body)."\n" if @body;
291 $op = $op->sibling;
292 }
293  
294 $info->{text} = $self->combine2str(\@texts) . "\f.";
295 $info->{texts} = \@texts;
296 return $info;
297 }
298  
299 # Return a "use" declaration for this BEGIN block, if appropriate
300 sub begin_is_use
301 {
302 my ($self, $cv) = @_;
303 my $root = $cv->ROOT;
304 local @$self{qw'curcv curcvlex'} = ($cv);
305 local $B::overlay = {};
306 $self->pessimise($root, $cv->START);
307 # require B::Debug;
308 # B::walkoptree($cv->ROOT, "debug");
309 my $lineseq = $root->first;
310 return if $lineseq->name ne "lineseq";
311  
312 my $req_op = $lineseq->first->sibling;
313 return if $req_op->name ne "require";
314  
315 my $module;
316 if ($req_op->first->private & OPpCONST_BARE) {
317 # Actually it should always be a bareword
318 $module = $self->const_sv($req_op->first)->PV;
319 $module =~ s[/][::]g;
320 $module =~ s/.pm$//;
321 }
322 else {
323 $module = $self->const($self->const_sv($req_op->first), 6);
324 }
325  
326 my $version;
327 my $version_op = $req_op->sibling;
328 return if class($version_op) eq "NULL";
329 if ($version_op->name eq "lineseq") {
330 # We have a version parameter; skip nextstate & pushmark
331 my $constop = $version_op->first->next->next;
332  
333 return unless $self->const_sv($constop)->PV eq $module;
334 $constop = $constop->sibling;
335 $version = $self->const_sv($constop);
336 if (class($version) eq "IV") {
337 $version = $version->int_value;
338 } elsif (class($version) eq "NV") {
339 $version = $version->NV;
340 } elsif (class($version) ne "PVMG") {
341 # Includes PVIV and PVNV
342 $version = $version->PV;
343 } else {
344 # version specified as a v-string
345 $version = 'v'.join '.', map ord, split //, $version->PV;
346 }
347 $constop = $constop->sibling;
348 return if $constop->name ne "method_named";
349 return if $self->const_sv($constop)->PV ne "VERSION";
350 }
351  
352 $lineseq = $version_op->sibling;
353 return if $lineseq->name ne "lineseq";
354 my $entersub = $lineseq->first->sibling;
355 if ($entersub->name eq "stub") {
356 return "use $module $version ();\n" if defined $version;
357 return "use $module ();\n";
358 }
359 return if $entersub->name ne "entersub";
360  
361 # See if there are import arguments
362 my $args = '';
363  
364 my $svop = $entersub->first->sibling; # Skip over pushmark
365 return unless $self->const_sv($svop)->PV eq $module;
366  
367 # Pull out the arguments
368 for ($svop=$svop->sibling; $svop->name ne "method_named";
369 $svop = $svop->sibling) {
370 $args .= ", " if length($args);
371 $args .= $self->deparse($svop, 6, $root)->{text};
372 }
373  
374 my $use = 'use';
375 my $method_named = $svop;
376 return if $method_named->name ne "method_named";
377 my $method_name = $self->const_sv($method_named)->PV;
378  
379 if ($method_name eq "unimport") {
380 $use = 'no';
381 }
382  
383 # Certain pragmas are dealt with using hint bits,
384 # so we ignore them here
385 if ($module eq 'strict' || $module eq 'integer'
386 || $module eq 'bytes' || $module eq 'warnings'
387 || $module eq 'feature') {
388 return "";
389 }
390  
391 if (defined $version && length $args) {
392 return "$use $module $version ($args);\n";
393 } elsif (defined $version) {
394 return "$use $module $version;\n";
395 } elsif (length $args) {
396 return "$use $module ($args);\n";
397 } else {
398 return "$use $module;\n";
399 }
400 }
401  
402 sub ambient_pragmas {
403 my $self = shift;
404 my ($arybase, $hint_bits, $warning_bits, $hinthash) = (0, 0);
405  
406 while (@_ > 1) {
407 my $name = shift();
408 my $val = shift();
409  
410 if ($name eq 'strict') {
411 require strict;
412  
413 if ($val eq 'none') {
414 $hint_bits &= $strict_bits{$_} for qw/refs subs vars/;
415 next();
416 }
417  
418 my @names;
419 if ($val eq "all") {
420 @names = qw/refs subs vars/;
421 }
422 elsif (ref $val) {
423 @names = @$val;
424 }
425 else {
426 @names = split' ', $val;
427 }
428 $hint_bits |= $strict_bits{$_} for @names;
429 }
430  
431 elsif ($name eq '$[') {
432 if (OPpCONST_ARYBASE) {
433 $arybase = $val;
434 } else {
435 croak "\$[ can't be non-zero on this perl" unless $val == 0;
436 }
437 }
438  
439 elsif ($name eq 'integer'
440 || $name eq 'bytes'
441 || $name eq 'utf8') {
442 require "$name.pm";
443 if ($val) {
444 $hint_bits |= ${$::{"${name}::"}{"hint_bits"}};
445 }
446 else {
447 $hint_bits &= ~${$::{"${name}::"}{"hint_bits"}};
448 }
449 }
450  
451 elsif ($name eq 're') {
452 require re;
453 if ($val eq 'none') {
454 $hint_bits &= ~re::bits(qw/taint eval/);
455 next();
456 }
457  
458 my @names;
459 if ($val eq 'all') {
460 @names = qw/taint eval/;
461 }
462 elsif (ref $val) {
463 @names = @$val;
464 }
465 else {
466 @names = split' ',$val;
467 }
468 $hint_bits |= re::bits(@names);
469 }
470  
471 elsif ($name eq 'warnings') {
472 if ($val eq 'none') {
473 $warning_bits = $warnings::NONE;
474 next();
475 }
476  
477 my @names;
478 if (ref $val) {
479 @names = @$val;
480 }
481 else {
482 @names = split/\s+/, $val;
483 }
484  
485 $warning_bits = $warnings::NONE if !defined ($warning_bits);
486 $warning_bits |= warnings::bits(@names);
487 }
488  
489 elsif ($name eq 'warning_bits') {
490 $warning_bits = $val;
491 }
492  
493 elsif ($name eq 'hint_bits') {
494 $hint_bits = $val;
495 }
496  
497 elsif ($name eq '%^H') {
498 $hinthash = $val;
499 }
500  
501 else {
502 croak "Unknown pragma type: $name";
503 }
504 }
505 if (@_) {
506 croak "The ambient_pragmas method expects an even number of args";
507 }
508  
509 $self->{'ambient_arybase'} = $arybase;
510 $self->{'ambient_warnings'} = $warning_bits;
511 $self->{'ambient_hints'} = $hint_bits;
512 $self->{'ambient_hinthash'} = $hinthash;
513 }
514  
515 # Sort of like maybe_parens in that we may possibly add (). However we take
516 # an op rather than text, and return a tree node. Also, we get around
517 # the 'if it looks like a function' rule.
518 sub maybe_parens_unop($self, $name, $op, $cx, $parent)
519 {
520 my $self = shift;
521 my($name, $op, $cx, $parent) = @_;
522 my $info = $self->deparse($op, 1, $parent);
523 my $text = $info->{text};
524 if ($name eq "umask" && $info->{text} =~ /^\d+$/) {
525 $text = sprintf("%#o", $text);
526 }
527 if ($cx > 16 or $self->{'parens'}) {
528 return info_from_list($op, $self, [$self->keyword($name), '(', $text, ')'],
529 '', 'maybe_parens_unop_parens', {body => [$info]});
530 } else {
531 $name = $self->keyword($name);
532 if (substr($text, 0, 1) eq "\cS") {
533 # use op's parens
534 return info_from_list($op, $self,[$name, substr($text, 1)], '',
535 'maybe_parens_unop_cS', {body => [$info]});
536 } elsif (substr($text, 0, 1) eq "(") {
537 # avoid looks-like-a-function trap with extra parens
538 # ('+' can lead to ambiguities)
539 return info_from_list($op, $self, [$name, '(', $text, ')'], '',
540 "maybe_parens_unop_fn", {});
541 } else {
542 return info_from_list($op, $self, [$name, $text], ' ',
543 'maybe_parens_unop', {body => [$info]});
544 }
545 }
546 Carp::confess("unhandled condition in maybe_parens_unop");
547 }
548  
549 sub maybe_parens_func($$$$$)
550 {
551 my($self, $func, $params, $cx, $prec) = @_;
552 if ($prec <= $cx or substr($params, 0, 1) eq "(" or $self->{'parens'}) {
553 return ($func, '(', $params, ')');
554 } else {
555 return ($func, ' ', $params);
556 }
557 }
558  
559 sub maybe_local_str($$$$)
560 {
561 my($self, $op, $cx, $text) = @_;
562 my $our_intro = ($op->name =~ /^(gv|rv2)[ash]v$/) ? OPpOUR_INTRO : 0;
563 if ($op->private & (OPpLVAL_INTRO|$our_intro)
564 and not $self->{'avoid_local'}{$$op}) {
565 my $our_local = ($op->private & OPpLVAL_INTRO) ? "local" : "our";
566 if( $our_local eq 'our' ) {
567 if ( $text !~ /^\W(\w+::)*\w+\z/
568 and !utf8::decode($text) || $text !~ /^\W(\w+::)*\w+\z/
569 ) {
570 die "Unexpected our($text)\n";
571 }
572 $text =~ s/(\w+::)+//;
573 }
574 if (want_scalar($op)) {
575 return info_from_list($op, $self,[$our_local, $text], ' ',
576 'maybe_local_scalar', {});
577 } else {
578 my @texts = $self->maybe_parens_func($our_local, $text,
579 $cx, 16);
580 return info_from_list($op, $self, \@texts, '',
581 'maybe_local_array', {});
582 }
583 } else {
584 return info_from_text($op, $self, $text, 'maybe_local', {});
585 }
586 }
587  
588 sub maybe_local {
589 my($self, $op, $cx, $var_info) = @_;
590 $var_info->{parent} = $$op;
591 return maybe_local_str($self, $op, $cx, $var_info->{text});
592 }
593  
594 sub padname_sv {
595 my $self = shift;
596 my $targ = shift;
597 return $self->{'curcv'}->PADLIST->ARRAYelt(0)->ARRAYelt($targ);
598 }
599  
600 sub maybe_my {
601 my $self = shift;
602 my($op, $cx, $text, $forbid_parens) = @_;
603 if ($op->private & OPpLVAL_INTRO and not $self->{'avoid_local'}{$$op}) {
604 my $my_str = $op->private & OPpPAD_STATE
605 ? $self->keyword("state")
606 : "my";
607 if ($forbid_parens || want_scalar($op)) {
608 return info_from_list($op, $self, [$my_str, $text], ' ',
609 'maybe_my_no_parens', {});
610 } else {
611 return info_from_list($op, $self, [$my_str, $text], ' ',
612 'maybe_my_parens',
613 {maybe_parens => [$self, $cx, 16]});
614 }
615 } else {
616 return info_from_text($op, $self, $text, 'maybe_my_avoid_local', {});
617 }
618 }
619  
620 # The following OPs don't have functions:
621  
622 # pp_padany -- does not exist after parsing
623  
624 sub AUTOLOAD {
625 if ($AUTOLOAD =~ s/^.*::pp_//) {
626 warn "unexpected OP_".uc $AUTOLOAD;
627 } else {
628 Carp::confess "Undefined subroutine $AUTOLOAD called";
629 }
630 }
631  
632 sub DESTROY {} # Do not AUTOLOAD
633  
634 # The BEGIN {} is used here because otherwise this code isn't executed
635 # when you run B::Deparse on itself.
636 my %globalnames;
637 BEGIN { map($globalnames{$_}++, "SIG", "STDIN", "STDOUT", "STDERR", "INC",
638 "ENV", "ARGV", "ARGVOUT", "_"); }
639  
640 sub gv_name {
641 my $self = shift;
642 my $gv = shift;
643 my $raw = shift;
644 Carp::confess() unless ref($gv) eq "B::GV";
645 my $stash = $gv->STASH->NAME;
646 my $name = $raw ? $gv->NAME : $gv->SAFENAME;
647 if ($stash eq 'main' && $name =~ /^::/) {
648 $stash = '::';
649 }
650 elsif (($stash eq 'main'
651 && ($globalnames{$name} || $name =~ /^[^A-Za-z_:]/))
652 or ($stash eq $self->{'curstash'} && !$globalnames{$name}
653 && ($stash eq 'main' || $name !~ /::/))
654 )
655 {
656 $stash = "";
657 } else {
658 $stash = $stash . "::";
659 }
660 if (!$raw and $name =~ /^(\^..|{)/) {
661 $name = "{$name}"; # ${^WARNING_BITS}, etc and ${
662 }
663 return $stash . $name;
664 }
665  
666 # Return the name to use for a stash variable.
667 # If a lexical with the same name is in scope, or
668 # if strictures are enabled, it may need to be
669 # fully-qualified.
670 sub stash_variable {
671 my ($self, $prefix, $name, $cx) = @_;
672  
673 return "$prefix$name" if $name =~ /::/;
674  
675 unless ($prefix eq '$' || $prefix eq '@' || #'
676 $prefix eq '%' || $prefix eq '$#') {
677 return "$prefix$name";
678 }
679  
680 if ($name =~ /^[^\w+-]$/) {
681 if (defined $cx && $cx == 26) {
682 if ($prefix eq '@') {
683 return "$prefix\{$name}";
684 }
685 elsif ($name eq '#') { return '${#}' } # "${#}a" vs "$#a"
686 }
687 if ($prefix eq '$#') {
688 return "\$#{$name}";
689 }
690 }
691  
692 return $prefix . $self->maybe_qualify($prefix, $name);
693 }
694  
695 sub lex_in_scope {
696 my ($self, $name, $our) = @_;
697 substr $name, 0, 0, = $our ? 'o' : 'm'; # our/my
698 $self->populate_curcvlex() if !defined $self->{'curcvlex'};
699  
700 return 0 if !defined($self->{'curcop'});
701 my $seq = $self->{'curcop'}->cop_seq;
702 return 0 if !exists $self->{'curcvlex'}{$name};
703 for my $a (@{$self->{'curcvlex'}{$name}}) {
704 my ($st, $en) = @$a;
705 return 1 if $seq > $st && $seq <= $en;
706 }
707 return 0;
708 }
709  
710 sub populate_curcvlex {
711 my $self = shift;
712 for (my $cv = $self->{'curcv'}; class($cv) eq "CV"; $cv = $cv->OUTSIDE) {
713 my $padlist = $cv->PADLIST;
714 # an undef CV still in lexical chain
715 next if class($padlist) eq "SPECIAL";
716 my @padlist = $padlist->ARRAY;
717 my @ns = $padlist[0]->ARRAY;
718  
719 for (my $i=0; $i<@ns; ++$i) {
720 next if class($ns[$i]) eq "SPECIAL";
721 if (class($ns[$i]) eq "PV") {
722 # Probably that pesky lexical @_
723 next;
724 }
725 my $name = $ns[$i]->PVX;
726 next unless defined $name;
727 my ($seq_st, $seq_en) =
728 ($ns[$i]->FLAGS & SVf_FAKE)
729 ? (0, 999999)
730 : ($ns[$i]->COP_SEQ_RANGE_LOW, $ns[$i]->COP_SEQ_RANGE_HIGH);
731  
732 push @{$self->{'curcvlex'}{
733 ($ns[$i]->FLAGS & SVpad_OUR ? 'o' : 'm') . $name
734 }}, [$seq_st, $seq_en, $ns[$i]];
735 }
736 }
737 }
738  
739 sub find_scope_st { ((find_scope(@_))[0]); }
740 sub find_scope_en { ((find_scope(@_))[1]); }
741  
742 # Recurses down the tree, looking for pad variable introductions and COPs
743 sub find_scope {
744 my ($self, $op, $scope_st, $scope_en) = @_;
745 carp("Undefined op in find_scope") if !defined $op;
746 return ($scope_st, $scope_en) unless $op->flags & OPf_KIDS;
747  
748 my @queue = ($op);
749 while(my $op = shift @queue ) {
750 for (my $o=$op->first; $$o; $o=$o->sibling) {
751 if ($o->name =~ /^pad.v$/ && $o->private & OPpLVAL_INTRO) {
752 my $s = int($self->padname_sv($o->targ)->COP_SEQ_RANGE_LOW);
753 my $e = $self->padname_sv($o->targ)->COP_SEQ_RANGE_HIGH;
754 $scope_st = $s if !defined($scope_st) || $s < $scope_st;
755 $scope_en = $e if !defined($scope_en) || $e > $scope_en;
756 return ($scope_st, $scope_en);
757 }
758 elsif (is_state($o)) {
759 my $c = $o->cop_seq;
760 $scope_st = $c if !defined($scope_st) || $c < $scope_st;
761 $scope_en = $c if !defined($scope_en) || $c > $scope_en;
762 return ($scope_st, $scope_en);
763 }
764 elsif ($o->flags & OPf_KIDS) {
765 unshift (@queue, $o);
766 }
767 }
768 }
769  
770 return ($scope_st, $scope_en);
771 }
772  
773 # Returns a list of subs which should be inserted before the COP
774 sub cop_subs {
775 my ($self, $op, $out_seq) = @_;
776 my $seq = $op->cop_seq;
777 # If we have nephews, then our sequence number indicates
778 # the cop_seq of the end of some sort of scope.
779 if (class($op->sibling) ne "NULL" && $op->sibling->flags & OPf_KIDS
780 and my $nseq = $self->find_scope_st($op->sibling) ) {
781 $seq = $nseq;
782 }
783 $seq = $out_seq if defined($out_seq) && $out_seq < $seq;
784 return $self->seq_subs($seq);
785 }
786  
787 my %feature_keywords = (
788 # keyword => 'feature',
789 state => 'state',
790 say => 'say',
791 given => 'switch',
792 when => 'switch',
793 default => 'switch',
794 break => 'switch',
795 evalbytes=>'evalbytes',
796 __SUB__ => '__SUB__',
797 fc => 'fc',
798 );
799  
800 # keywords that are strong and also have a prototype
801 #
802 my %strong_proto_keywords = map { $_ => 1 } qw(
803 pos
804 prototype
805 scalar
806 study
807 undef
808 );
809  
810 sub keyword {
811 my $self = shift;
812 my $name = shift;
813 return $name if $name =~ /^CORE::/; # just in case
814 if (exists $feature_keywords{$name}) {
815 my $hh;
816 my $hints = $self->{hints} & $feature::hint_mask;
817 if ($hints && $hints != $feature::hint_mask) {
818 $hh = _features_from_bundle($hints);
819 }
820 elsif ($hints) { $hh = $self->{'hinthash'} }
821 return "CORE::$name"
822 if !$hh
823 || !$hh->{"feature_$feature_keywords{$name}"}
824 }
825 if ($strong_proto_keywords{$name}
826 || ($name !~ /^(?:chom?p|do|exec|glob|s(?:elect|ystem))\z/
827 && !defined eval{prototype "CORE::$name"})
828 ) { return $name }
829 if (
830 exists $self->{subs_declared}{$name}
831 or
832 exists &{"$self->{curstash}::$name"}
833 ) {
834 return "CORE::$name"
835 }
836 return $name;
837 }
838  
839 sub pp_not
840 {
841 my($self, $op, $cx) = @_;
842 if ($cx <= 4) {
843 $self->listop($op, $cx, "not", $op->first);
844 } else {
845 $self->pfixop($op, $cx, "!", 21);
846 }
847 }
848  
849 # Note 5.20 and up
850 sub pp_null
851 {
852 my($self, $op, $cx) = @_;
853 my $info;
854 if (class($op) eq "OP") {
855 if ($op->targ == B::Deparse::OP_CONST) {
856 # The Perl source constant value can't be recovered.
857 # We'll use the 'ex_const' value as a substitute
858 return info_from_text($op, $self, $self->{'ex_const'}, 'constant unrecoverable', {})
859 } else {
860 return info_from_text($op, $self, '', 'constant ""', {});
861 }
862 } elsif (class ($op) eq "COP") {
863 return $self->pp_nextstate($op, $cx);
864 }
865 my $kid = $op->first;
866 if ($op->first->name eq 'pushmark'
867 or $op->first->name eq 'null'
868 && $op->first->targ == B::Deparse::OP_PUSHMARK
869 && B::Deparse::_op_is_or_was($op, B::Deparse::OP_LIST)) {
870 return $self->pp_list($op, $cx);
871 } elsif ($kid->name eq "enter") {
872 return $self->pp_leave($op, $cx);
873 } elsif ($kid->name eq "leave") {
874 return $self->pp_leave($kid, $cx);
875 } elsif ($kid->name eq "scope") {
876 return $self->pp_scope($kid, $cx);
877 } elsif ($op->targ == B::Deparse::OP_STRINGIFY) {
878 return $self->dquote($op, $cx);
879 } elsif ($op->targ == B::Deparse::OP_GLOB) {
880 my @other_ops = ($kid, $kid->first, $kid->first->first);
881 my $info = $self->pp_glob(
882 $kid # entersub
883 ->first # ex-list
884 ->first # pushmark
885 ->sibling, # glob
886 $cx
887 );
888 push @{$info->{other_ops}}, @other_ops;
889 return $info;
890 } elsif (!null($kid->sibling) and
891 $kid->sibling->name eq "readline" and
892 $kid->sibling->flags & OPf_STACKED) {
893 my $lhs = $self->deparse($kid, 7, $op);
894 my $rhs = $self->deparse($kid->sibling, 7, $kid);
895 return $self->bin_info_join_maybe_parens($op, $lhs, $rhs, '=', " ", $cx, 7,
896 'readline');
897 } elsif (!null($kid->sibling) and
898 $kid->sibling->name eq "trans" and
899 $kid->sibling->flags & OPf_STACKED) {
900 my $lhs = $self->deparse($kid, 20, $op);
901 my $rhs = $self->deparse($kid->sibling, 20, $op);
902 return $self->bin_info_join_maybe_parens($op, $lhs, $rhs, '=~', " ", $cx, 20,
903 'trans');
904 } elsif ($op->flags & OPf_SPECIAL && $cx < 1 && !$op->targ) {
905 my $kid_info = $self->deparse($kid, $cx, $op);
906 return info_from_list($op, $self, ['do', "{\n\t", $kid_info->{text},
907 "\n\b};"], '', 'null_special',
908 {body => [$kid_info]});
909 } elsif (!null($kid->sibling) and
910 $kid->sibling->name eq "null" and
911 class($kid->sibling) eq "UNOP" and
912 $kid->sibling->first->flags & OPf_STACKED and
913 $kid->sibling->first->name eq "rcatline") {
914 my $lhs = $self->deparse($kid, 18, $op);
915 my $rhs = $self->deparse($kid->sibling, 18, $op);
916 return $self->bin_info_join_maybe_parens($op, $lhs, $rhs, '=', " ", $cx, 20,
917 'null_rcatline');
918 } else {
919 return $self->deparse($kid, $cx, $op);
920 }
921 Carp::confess("unhandled condition in null");
922 }
923  
924 # Note: maybe_local things can't be moved to PP yet.
925 sub pp_pos { maybe_local(@_, unop(@_, "pos")) }
926 sub pp_sin { maybe_targmy(@_, \&unop, "sin") }
927 sub pp_cos { maybe_targmy(@_, \&unop, "cos") }
928 sub pp_rand { maybe_targmy(@_, \&unop, "rand") }
929  
930 sub pp_srand { unop(@_, "srand") }
931  
932 sub pp_exp { maybe_targmy(@_, \&unop, "exp") }
933 sub pp_log { maybe_targmy(@_, \&unop, "log") }
934 sub pp_sqrt { maybe_targmy(@_, \&unop, "sqrt") }
935 sub pp_int { maybe_targmy(@_, \&unop, "int") }
936 sub pp_hex { maybe_targmy(@_, \&unop, "hex") }
937 sub pp_oct { maybe_targmy(@_, \&unop, "oct") }
938 sub pp_abs { maybe_targmy(@_, \&unop, "abs") }
939  
940 sub pp_length { maybe_targmy(@_, \&unop, "length") }
941 sub pp_ord { maybe_targmy(@_, \&unop, "ord") }
942 sub pp_chr { maybe_targmy(@_, \&unop, "chr") }
943  
944 sub pp_each { unop(@_, "each") }
945 sub pp_values { unop(@_, "values") }
946 sub pp_keys { unop(@_, "keys") }
947 { no strict 'refs'; *{"pp_r$_"} = *{"pp_$_"} for qw< keys each values >; }
948 sub pp_boolkeys
949 {
950 # no name because its an optimisation op that has no keyword
951 unop(@_,"");
952 }
953 sub pp_aeach { unop(@_, "each") }
954 sub pp_avalues { unop(@_, "values") }
955 sub pp_akeys { unop(@_, "keys") }
956 sub pp_pop { unop(@_, "pop") }
957 sub pp_shift { unop(@_, "shift") }
958  
959 sub pp_caller { unop(@_, "caller") }
960 sub pp_reset { unop(@_, "reset") }
961 sub pp_exit { unop(@_, "exit") }
962 sub pp_prototype { unop(@_, "prototype") }
963  
964 sub pp_close { unop(@_, "close") }
965 sub pp_fileno { unop(@_, "fileno") }
966 sub pp_umask { unop(@_, "umask") }
967 sub pp_untie { unop(@_, "untie") }
968 sub pp_tied { unop(@_, "tied") }
969 sub pp_dbmclose { unop(@_, "dbmclose") }
970 sub pp_getc { unop(@_, "getc") }
971 sub pp_eof { unop(@_, "eof") }
972 sub pp_tell { unop(@_, "tell") }
973 sub pp_getsockname { unop(@_, "getsockname") }
974 sub pp_getpeername { unop(@_, "getpeername") }
975  
976 sub pp_chdir { maybe_targmy(@_, \&unop, "chdir") }
977 sub pp_chroot { maybe_targmy(@_, \&unop, "chroot") }
978 sub pp_readlink { unop(@_, "readlink") }
979 sub pp_rmdir { maybe_targmy(@_, \&unop, "rmdir") }
980 sub pp_readdir { unop(@_, "readdir") }
981 sub pp_telldir { unop(@_, "telldir") }
982 sub pp_rewinddir { unop(@_, "rewinddir") }
983 sub pp_closedir { unop(@_, "closedir") }
984 sub pp_getpgrp { maybe_targmy(@_, \&unop, "getpgrp") }
985 sub pp_localtime { unop(@_, "localtime") }
986 sub pp_gmtime { unop(@_, "gmtime") }
987 sub pp_alarm { unop(@_, "alarm") }
988 sub pp_sleep { maybe_targmy(@_, \&unop, "sleep") }
989  
990 sub pp_dofile
991 {
992 my $code = unop(@_, "do", 1); # llafr does not apply
993 if ($code =~ s/^((?:CORE::)?do) \{/$1({/) { $code .= ')' }
994 $code;
995 }
996  
997 sub pp_entereval
998 {
999 unop(
1000 @_,
1001 $_[1]->private & OPpEVAL_BYTES ? $_[0]->keyword('evalbytes') : "eval"
1002 )
1003 }
1004  
1005 sub pp_ghbyname { unop(@_, "gethostbyname") }
1006 sub pp_gnbyname { unop(@_, "getnetbyname") }
1007 sub pp_gpbyname { unop(@_, "getprotobyname") }
1008 sub pp_shostent { unop(@_, "sethostent") }
1009 sub pp_snetent { unop(@_, "setnetent") }
1010 sub pp_sprotoent { unop(@_, "setprotoent") }
1011 sub pp_sservent { unop(@_, "setservent") }
1012 sub pp_gpwnam { unop(@_, "getpwnam") }
1013 sub pp_gpwuid { unop(@_, "getpwuid") }
1014 sub pp_ggrnam { unop(@_, "getgrnam") }
1015 sub pp_ggrgid { unop(@_, "getgrgid") }
1016  
1017 sub pp_lock { unop(@_, "lock") }
1018  
1019 sub pp_continue { unop(@_, "continue"); }
1020 sub pp_break { unop(@_, "break"); }
1021  
1022 sub givwhen
1023 {
1024 my($self, $op, $cx, $givwhen) = @_;
1025  
1026 my $enterop = $op->first;
1027 my ($head, $block);
1028 if ($enterop->flags & OPf_SPECIAL) {
1029 $head = $self->keyword("default");
1030 $block = $self->deparse($enterop->first, 0, $enterop, $op);
1031 }
1032 else {
1033 my $cond = $enterop->first;
1034 my $cond_str = $self->deparse($cond, 1, $enterop, $op);
1035 $head = "$givwhen ($cond_str)";
1036 $block = $self->deparse($cond->sibling, 0, $enterop, $op);
1037 }
1038  
1039 return info_from_list($op, $self, [$head, "{",
1040 "\n\t", $block->{text}, "\n\b",
1041 "}\cK"], '', 'givwhen',
1042 {body => [$block]});
1043 }
1044  
1045 sub pp_leavegiven { givwhen(@_, $_[0]->keyword("given")); }
1046 sub pp_leavewhen { givwhen(@_, $_[0]->keyword("when")); }
1047  
1048 sub pp_delete
1049 {
1050 my($self, $op, $cx) = @_;
1051 my $arg;
1052 my ($info, $body, @texts, $type);
1053 if ($op->private & OPpSLICE) {
1054 if ($op->flags & OPf_SPECIAL) {
1055 # Deleting from an array, not a hash
1056 $info = $self->pp_aslice($op->first, 16);
1057 $type = 'delete_slice';
1058 }
1059 } else {
1060 if ($op->flags & OPf_SPECIAL) {
1061 # Deleting from an array, not a hash
1062 $info = $self->pp_aelem($op->first, 16);
1063 $type = 'delete_array'
1064 } else {
1065 $info = $self->pp_helem($op->first, 16);
1066 $type = 'delete_hash';
1067 }
1068 }
1069 my @texts = $self->maybe_parens_func("delete",
1070 $info->{text}, $cx, 16);
1071 return info_from_list($op, $self, \@texts, '', $type, {body => [$info]});
1072 }
1073  
1074 sub pp_require
1075 {
1076 my($self, $op, $cx) = @_;
1077 my $opname = $op->flags & OPf_SPECIAL ? 'CORE::require' : 'require';
1078 if (class($op) eq "UNOP" and $op->first->name eq "const"
1079 and $op->first->private & OPpCONST_BARE) {
1080 my $name = $self->const_sv($op->first)->PV;
1081 $name =~ s[/][::]g;
1082 $name =~ s/\.pm//g;
1083 return info_from_list($op, $self, [$opname, $name], ' ',
1084 'require',
1085 {maybe_parens => [$self, $cx, 16]});
1086 } else {
1087 return $self->unop(
1088 $op, $cx,
1089 $op->first->name eq 'const'
1090 && $op->first->private & OPpCONST_NOVER
1091 ? "no"
1092 : $opname,
1093 1, # llafr does not apply
1094 );
1095 }
1096 Carp::confess("unhandled condition in pp_require");
1097 }
1098  
1099 sub pp_scalar
1100 {
1101 my($self, $op, $cx) = @_;
1102 my $kid = $op->first;
1103 if (not null $kid->sibling) {
1104 # XXX Was a here-doc
1105 return $self->dquote($op);
1106 }
1107 $self->unop($op, $cx, "scalar");
1108 }
1109  
1110  
1111 sub padval
1112 {
1113 my $self = shift;
1114 my $targ = shift;
1115 return $self->{'curcv'}->PADLIST->ARRAYelt(1)->ARRAYelt($targ);
1116 }
1117  
1118 sub anon_hash_or_list
1119 {
1120 my $self = shift;
1121 my($op, $cx) = @_;
1122  
1123 my $name = $op->name;
1124 my($pre, $post) = @{{"anonlist" => ["[","]"],
1125 "anonhash" => ["{","}"]}->{$name}};
1126 my($expr, @exprs);
1127 my $other_ops => [$op->first];
1128 $op = $op->first->sibling; # skip pushmark
1129 for (; !null($op); $op = $op->sibling) {
1130 $expr = $self->deparse($op, 6, $op);
1131 push @exprs, [$expr, $op];
1132 }
1133 if ($pre eq "{" and $cx < 1) {
1134 # Disambiguate that it's not a block
1135 $pre = "+{";
1136 }
1137 my $texts = [$pre, $self->combine(", ", \@exprs), $post];
1138 return info_from_list($op, $self, $texts, '', $name,
1139 {body => \@exprs,
1140 other_ops => $other_ops
1141 });
1142 }
1143  
1144 sub pp_anonlist {
1145 my $self = shift;
1146 my ($op, $cx) = @_;
1147 if ($op->flags & OPf_SPECIAL) {
1148 return $self->anon_hash_or_list($op, $cx);
1149 }
1150 warn "Unexpected op pp_" . $op->name() . " without OPf_SPECIAL";
1151 return info_from_text($op, $self, 'XXX', 'bad_anonlist', {});
1152 }
1153  
1154 *pp_anonhash = \&pp_anonlist;
1155  
1156 sub e_anoncode($$)
1157 {
1158 my ($self, $info) = @_;
1159 my $sub_info = $self->deparse_sub($info->{code});
1160 return info_from_list($sub_info->{op}, $self,
1161 ['sub', $sub_info->{text}], ' ', 'e_anoncode',
1162 {body=> [$sub_info]});
1163 }
1164  
1165 sub pp_refgen
1166 {
1167 my($self, $op, $cx) = @_;
1168 my $kid = $op->first;
1169 if ($kid->name eq "null") {
1170 my $other_ops = [$kid];
1171 my $anoncode = $kid = $kid->first;
1172 if ($anoncode->name eq "anonconst") {
1173 $anoncode = $anoncode->first->first->sibling;
1174 }
1175 if ($anoncode->name eq "anoncode"
1176 or !null($anoncode = $kid->sibling) and
1177 $anoncode->name eq "anoncode") {
1178 return $self->e_anoncode({ code => $self->padval($anoncode->targ) });
1179 } elsif ($kid->name eq "pushmark") {
1180 my $sib_name = $kid->sibling->name;
1181 if ($sib_name eq 'entersub') {
1182 my $kid_info = $self->deparse($kid->sibling, 1, $op);
1183 # Always show parens for \(&func()), but only with -p otherwise
1184 my @texts = ('\\', $kid_info->{text});
1185 if ($self->{'parens'} or $kid->sibling->private & OPpENTERSUB_AMPER) {
1186 @texts = ('(', "\\", $kid_info->{text}, ')');
1187 }
1188 return info_from_list($op, $self, \@texts, '', 'refgen_entersub',
1189 {body => [$kid_info],
1190 other_ops => $other_ops});
1191 }
1192 }
1193 }
1194 local $self->{'in_refgen'} = 1;
1195 $self->pfixop($op, $cx, "\\", 20);
1196 }
1197  
1198 sub pp_srefgen { pp_refgen(@_) }
1199  
1200 sub pp_readline {
1201 my $self = shift;
1202 my($op, $cx) = @_;
1203 my $kid = $op->first;
1204 $kid = $kid->first if $kid->name eq "rv2gv"; # <$fh>
1205 if (is_scalar($kid)) {
1206 my $body = [$self->deparse($kid, 1, $op)];
1207 return info_from_list($op, $self, ['<', $body->[0]{text}, '>'], '',
1208 'readline_scalar', {body=>$body});
1209 }
1210 return $self->unop($op, $cx, "readline");
1211 }
1212  
1213 sub pp_rcatline {
1214 my $self = shift;
1215 my($op) = @_;
1216 return info_from_list($op, $self, ["<", $self->gv_name($self->gv_or_padgv($op)), ">"],
1217 '', 'rcatline', {});
1218 }
1219  
1220 # Unary operators that can occur as pseudo-listops inside double quotes
1221 sub dq_unop
1222 {
1223 my($self, $op, $cx, $name, $prec, $flags) = (@_, 0, 0);
1224 my $kid;
1225 if ($op->flags & OPf_KIDS) {
1226 my $other_ops = undef;
1227 $kid = $op->first;
1228 if (not null $kid->sibling) {
1229 # If there's more than one kid, the first is an ex-pushmark.
1230 $other_ops = [$kid];
1231 $kid = $kid->sibling;
1232 }
1233 my $info = $self->maybe_parens_unop($name, $kid, $cx, $op);
1234 $info->{other_ops} = $other_ops if $other_ops;
1235 return $info;
1236 } else {
1237 my @texts = ($name);
1238 push @texts, '(', ')' if $op->flags & OPf_SPECIAL;
1239 return info_from_list($op, $self, \@texts, '', 'dq', {});
1240 }
1241 Carp::confess("unhandled condition in dq_unop");
1242 }
1243  
1244 sub pp_ucfirst { dq_unop(@_, "ucfirst") }
1245 sub pp_lcfirst { dq_unop(@_, "lcfirst") }
1246 sub pp_uc { dq_unop(@_, "uc") }
1247 sub pp_lc { dq_unop(@_, "lc") }
1248 sub pp_quotemeta { maybe_targmy(@_, \&dq_unop, "quotemeta") }
1249 sub pp_fc { dq_unop(@_, "fc") }
1250  
1251 # loop expressions
1252 sub loopex
1253 {
1254 my ($self, $op, $cx, $name) = @_;
1255 my $opts = {maybe_parens => [$self, $cx, 7]};
1256 my ($type, $body);
1257 if (class($op) eq "PVOP") {
1258 return info_from_list($op, $self, [$name, $op->pv], ' ', 'loopex_pvop', {});
1259 } elsif (class($op) eq "OP") {
1260 # no-op
1261 $type = 'loopex_op';
1262 return info_from_text($op, $self, $name, 'loopex_op', $opts);
1263 } elsif (class($op) eq "UNOP") {
1264 (my $kid_info = $self->deparse($op->first, 7, $op)) =~ s/^\cS//;
1265 $opts->{body} = [$kid_info];
1266 return info_from_list($op, $self, [$name, $op->pv], ' ', 'loopex_unop', $opts);
1267 } else {
1268 return info_from_text($op, $self, $name, 'loopex', $opts);
1269 }
1270 Carp::confess("unhandled condition in lopex");
1271 }
1272  
1273 sub pp_last { loopex(@_, "last") }
1274 sub pp_next { loopex(@_, "next") }
1275 sub pp_redo { loopex(@_, "redo") }
1276 sub pp_goto { loopex(@_, "goto") }
1277 sub pp_dump { loopex(@_, "CORE::dump") }
1278  
1279 sub ftst
1280 {
1281 my($self, $op, $cx, $name) = @_;
1282 if (class($op) eq "UNOP") {
1283 # Genuine '-X' filetests are exempt from the LLAFR, but not
1284 # l?stat()
1285 if ($name =~ /^-/) {
1286 (my $kid = $self->deparse($op->first, 16, $op)) =~ s/^\cS//;
1287 return info_from_list($op, $self, [$name, $kid->{text}], ' ',
1288 'ftst_unop_dash',
1289 {body => [$kid],
1290 maybe_parens => [$self, $cx, 16]});
1291 }
1292 return $self->maybe_parens_unop($name, $op->first, $cx, $op);
1293 } elsif (class($op) =~ /^(SV|PAD)OP$/) {
1294 my @list = $self->maybe_parens_func($name, $self->pp_gv($op, 1), $cx, 16);
1295 return info_from_list($op, $self, \@list, ' ', 'ftst_list', {});
1296 } else { # I don't think baseop filetests ever survive ck_ftst, but...
1297 return info_from_text($op, $self, $name, 'unop', {});
1298 }
1299 }
1300  
1301 sub pp_lstat { ftst(@_, "lstat") }
1302 sub pp_stat { ftst(@_, "stat") }
1303 sub pp_ftrread { ftst(@_, "-R") }
1304 sub pp_ftrwrite { ftst(@_, "-W") }
1305 sub pp_ftrexec { ftst(@_, "-X") }
1306 sub pp_fteread { ftst(@_, "-r") }
1307 sub pp_ftewrite { ftst(@_, "-w") }
1308 sub pp_fteexec { ftst(@_, "-x") }
1309 sub pp_ftis { ftst(@_, "-e") }
1310 sub pp_fteowned { ftst(@_, "-O") }
1311 sub pp_ftrowned { ftst(@_, "-o") }
1312 sub pp_ftzero { ftst(@_, "-z") }
1313 sub pp_ftsize { ftst(@_, "-s") }
1314 sub pp_ftmtime { ftst(@_, "-M") }
1315 sub pp_ftatime { ftst(@_, "-A") }
1316 sub pp_ftctime { ftst(@_, "-C") }
1317 sub pp_ftsock { ftst(@_, "-S") }
1318 sub pp_ftchr { ftst(@_, "-c") }
1319 sub pp_ftblk { ftst(@_, "-b") }
1320 sub pp_ftfile { ftst(@_, "-f") }
1321 sub pp_ftdir { ftst(@_, "-d") }
1322 sub pp_ftpipe { ftst(@_, "-p") }
1323 sub pp_ftlink { ftst(@_, "-l") }
1324 sub pp_ftsuid { ftst(@_, "-u") }
1325 sub pp_ftsgid { ftst(@_, "-g") }
1326 sub pp_ftsvtx { ftst(@_, "-k") }
1327 sub pp_fttty { ftst(@_, "-t") }
1328 sub pp_fttext { ftst(@_, "-T") }
1329 sub pp_ftbinary { ftst(@_, "-B") }
1330  
1331 sub SWAP_CHILDREN () { 1 }
1332 sub ASSIGN () { 2 } # has OP= variant
1333 sub LIST_CONTEXT () { 4 } # Assignment is in list context
1334  
1335 my(%left, %right);
1336  
1337 sub assoc_class {
1338 my $op = shift;
1339 my $name = $op->name;
1340 if ($name eq "concat" and $op->first->name eq "concat") {
1341 # avoid spurious '=' -- see comment in pp_concat
1342 return "concat";
1343 }
1344 if ($name eq "null" and class($op) eq "UNOP"
1345 and $op->first->name =~ /^(and|x?or)$/
1346 and null $op->first->sibling)
1347 {
1348 # Like all conditional constructs, OP_ANDs and OP_ORs are topped
1349 # with a null that's used as the common end point of the two
1350 # flows of control. For precedence purposes, ignore it.
1351 # (COND_EXPRs have these too, but we don't bother with
1352 # their associativity).
1353 return assoc_class($op->first);
1354 }
1355 return $name . ($op->flags & OPf_STACKED ? "=" : "");
1356 }
1357  
1358 # Left associative operators, like '+', for which
1359 # $a + $b + $c is equivalent to ($a + $b) + $c
1360  
1361 BEGIN {
1362 %left = ('multiply' => 19, 'i_multiply' => 19,
1363 'divide' => 19, 'i_divide' => 19,
1364 'modulo' => 19, 'i_modulo' => 19,
1365 'repeat' => 19,
1366 'add' => 18, 'i_add' => 18,
1367 'subtract' => 18, 'i_subtract' => 18,
1368 'concat' => 18,
1369 'left_shift' => 17, 'right_shift' => 17,
1370 'bit_and' => 13,
1371 'bit_or' => 12, 'bit_xor' => 12,
1372 'and' => 3,
1373 'or' => 2, 'xor' => 2,
1374 );
1375 }
1376  
1377 sub deparse_binop_left {
1378 my $self = shift;
1379 my($op, $left, $prec) = @_;
1380 if ($left{assoc_class($op)} && $left{assoc_class($left)}
1381 and $left{assoc_class($op)} == $left{assoc_class($left)})
1382 {
1383 return $self->deparse($left, $prec - .00001, $op);
1384 } else {
1385 return $self->deparse($left, $prec, $op);
1386 }
1387 }
1388  
1389 # Right associative operators, like '=', for which
1390 # $a = $b = $c is equivalent to $a = ($b = $c)
1391  
1392 BEGIN {
1393 %right = ('pow' => 22,
1394 'sassign=' => 7, 'aassign=' => 7,
1395 'multiply=' => 7, 'i_multiply=' => 7,
1396 'divide=' => 7, 'i_divide=' => 7,
1397 'modulo=' => 7, 'i_modulo=' => 7,
1398 'repeat=' => 7,
1399 'add=' => 7, 'i_add=' => 7,
1400 'subtract=' => 7, 'i_subtract=' => 7,
1401 'concat=' => 7,
1402 'left_shift=' => 7, 'right_shift=' => 7,
1403 'bit_and=' => 7,
1404 'bit_or=' => 7, 'bit_xor=' => 7,
1405 'andassign' => 7,
1406 'orassign' => 7,
1407 );
1408 }
1409  
1410 sub deparse_binop_right {
1411 my $self = shift;
1412 my($op, $right, $prec) = @_;
1413 if ($right{assoc_class($op)} && $right{assoc_class($right)}
1414 and $right{assoc_class($op)} == $right{assoc_class($right)})
1415 {
1416 return $self->deparse($right, $prec - .00001, $op);
1417 } else {
1418 return $self->deparse($right, $prec, $op);
1419 }
1420 }
1421  
1422 sub pp_add { maybe_targmy(@_, \&binop, "+", 18, ASSIGN) }
1423 sub pp_multiply { maybe_targmy(@_, \&binop, "*", 19, ASSIGN) }
1424 sub pp_subtract { maybe_targmy(@_, \&binop, "-",18, ASSIGN) }
1425 sub pp_divide { maybe_targmy(@_, \&binop, "/", 19, ASSIGN) }
1426 sub pp_modulo { maybe_targmy(@_, \&binop, "%", 19, ASSIGN) }
1427 sub pp_i_add { maybe_targmy(@_, \&binop, "+", 18, ASSIGN) }
1428 sub pp_i_multiply { maybe_targmy(@_, \&binop, "*", 19, ASSIGN) }
1429 sub pp_i_subtract { maybe_targmy(@_, \&binop, "-", 18, ASSIGN) }
1430 sub pp_i_divide { maybe_targmy(@_, \&binop, "/", 19, ASSIGN) }
1431 sub pp_i_modulo { maybe_targmy(@_, \&binop, "%", 19, ASSIGN) }
1432 sub pp_pow { maybe_targmy(@_, \&binop, "**", 22, ASSIGN) }
1433  
1434 sub pp_left_shift { maybe_targmy(@_, \&binop, "<<", 17, ASSIGN) }
1435 sub pp_right_shift { maybe_targmy(@_, \&binop, ">>", 17, ASSIGN) }
1436 sub pp_bit_and { maybe_targmy(@_, \&binop, "&", 13, ASSIGN) }
1437 sub pp_bit_or { maybe_targmy(@_, \&binop, "|", 12, ASSIGN) }
1438 sub pp_bit_xor { maybe_targmy(@_, \&binop, "^", 12, ASSIGN) }
1439  
1440 sub pp_eq { binop(@_, "==", 14) }
1441 sub pp_ne { binop(@_, "!=", 14) }
1442 sub pp_lt { binop(@_, "<", 15) }
1443 sub pp_gt { binop(@_, ">", 15) }
1444 sub pp_ge { binop(@_, ">=", 15) }
1445 sub pp_le { binop(@_, "<=", 15) }
1446 sub pp_ncmp { binop(@_, "<=>", 14) }
1447 sub pp_i_eq { binop(@_, "==", 14) }
1448 sub pp_i_ne { binop(@_, "!=", 14) }
1449 sub pp_i_lt { binop(@_, "<", 15) }
1450 sub pp_i_gt { binop(@_, ">", 15) }
1451 sub pp_i_ge { binop(@_, ">=", 15) }
1452 sub pp_i_le { binop(@_, "<=", 15) }
1453 sub pp_i_ncmp { binop(@_, "<=>", 14) }
1454  
1455 sub pp_seq { binop(@_, "eq", 14) }
1456 sub pp_sne { binop(@_, "ne", 14) }
1457 sub pp_slt { binop(@_, "lt", 15) }
1458 sub pp_sgt { binop(@_, "gt", 15) }
1459 sub pp_sge { binop(@_, "ge", 15) }
1460 sub pp_sle { binop(@_, "le", 15) }
1461 sub pp_scmp { binop(@_, "cmp", 14) }
1462  
1463 sub pp_sassign { binop(@_, "=", 7, SWAP_CHILDREN) }
1464 sub pp_aassign { binop(@_, "=", 7, SWAP_CHILDREN | LIST_CONTEXT) }
1465  
1466 sub pp_smartmatch {
1467 my ($self, $op, $cx) = @_;
1468 if ($op->flags & OPf_SPECIAL) {
1469 return $self->deparse($op->last, $cx, $op);
1470 }
1471 else {
1472 binop(@_, "~~", 14);
1473 }
1474 }
1475  
1476 sub bin_info_join($$$$$$$) {
1477 my ($self, $op, $lhs, $rhs, $mid, $sep, $type) = @_;
1478 my $texts = [$lhs->{text}, $mid, $rhs->{text}];
1479 return info_from_list($op, $self, $texts, ' ', $type, {})
1480 }
1481  
1482 sub bin_info_join_maybe_parens($$$$$$$$$) {
1483 my ($self, $op, $lhs, $rhs, $mid, $sep, $cx, $prec, $type) = @_;
1484 my $info = bin_info_join($self, $op, $lhs, $rhs, $mid, $sep, $type);
1485 $info->{text} = $self->maybe_parens($info->{text}, $cx, $prec);
1486 return $info;
1487 }
1488  
1489 # '.' is special because concats-of-concats are optimized to save copying
1490 # by making all but the first concat stacked. The effect is as if the
1491 # programmer had written '($a . $b) .= $c', except legal.
1492 sub pp_concat { maybe_targmy(@_, \&real_concat) }
1493 sub real_concat {
1494 my $self = shift;
1495 my($op, $cx) = @_;
1496 my $left = $op->first;
1497 my $right = $op->last;
1498 my $eq = "";
1499 my $prec = 18;
1500 if ($op->flags & OPf_STACKED and $op->first->name ne "concat") {
1501 $eq = "=";
1502 $prec = 7;
1503 }
1504 my $lhs = $self->deparse_binop_left($op, $left, $prec);
1505 my $rhs = $self->deparse_binop_right($op, $right, $prec);
1506 return $self->bin_info_join_maybe_parens($op, $lhs, $rhs, ".$eq", " ", $cx, $prec,
1507 'real_concat');
1508 }
1509  
1510 sub range {
1511 my $self = shift;
1512 my ($op, $cx, $type) = @_;
1513 my $left = $op->first;
1514 my $right = $left->sibling;
1515 $left = $self->deparse($left, 9, $op);
1516 $right = $self->deparse($right, 9, $op);
1517 return info_from_list($op, $self, [$left, $type, $right], ' ', 'range',
1518 {maybe_parens => [$self, $cx, 9]});
1519 }
1520  
1521 sub pp_flop
1522 {
1523 my $self = shift;
1524 my($op, $cx) = @_;
1525 my $flip = $op->first;
1526 my $type = ($flip->flags & OPf_SPECIAL) ? "..." : "..";
1527 return info_from_text($op, $self, $self->range($flip->first, $cx, $type), 'pp_flop', {});
1528 }
1529  
1530 sub logassignop
1531 {
1532 my ($self, $op, $cx, $opname) = @_;
1533 my $left = $op->first;
1534  
1535 my $right = $op->first->sibling->first; # skip sassign
1536 $left = $self->deparse($left, 7, $op);
1537 $right = $self->deparse($right, 7, $op);
1538 return info_from_list($op, $self, [$left->{text}, $opname, $right->{text}], ' ',
1539 'logassignop',
1540 {other_ops => [$op->first->sibling],
1541 body => [$left, $right],
1542 maybe_parens => [$self, $cx, 7]});
1543 }
1544  
1545 sub pp_andassign { logassignop(@_, "&&=") }
1546 sub pp_orassign { logassignop(@_, "||=") }
1547 sub pp_dorassign { logassignop(@_, "//=") }
1548  
1549 sub rv2gv_or_string {
1550 my($self,$op, $parent) = @_;
1551 if ($op->name eq "gv") { # could be open("open") or open("###")
1552 my($name,$quoted) =
1553 $self->stash_variable_name("", $self->gv_or_padgv($op));
1554 return info_from_text($op, $self, $quoted ? $name : "*$name", 'r2gv_or_string', {});
1555 }
1556 else {
1557 return $self->deparse($op, 6, $parent);
1558 }
1559 }
1560  
1561 sub pp_bless { listop(@_, "bless") }
1562 sub pp_atan2 { maybe_targmy(@_, \&listop, "atan2") }
1563 sub pp_substr {
1564 my ($self,$op,$cx) = @_;
1565 if ($op->private & OPpSUBSTR_REPL_FIRST) {
1566 my $left = listop($self, $op, 7, "substr", $op->first->sibling->sibling);
1567 my $right = $self->deparse($op->first->sibling, 7, $op);
1568 return info_from_list($op, $self,[$left, '=', $right], ' ',
1569 'substr_repl_first', {});
1570 }
1571 return maybe_local(@_, listop(@_, "substr"))
1572 }
1573 sub pp_vec { maybe_local(@_, listop(@_, "vec")) }
1574 sub pp_index { maybe_targmy(@_, \&listop, "index") }
1575 sub pp_rindex { maybe_targmy(@_, \&listop, "rindex") }
1576 sub pp_sprintf { maybe_targmy(@_, \&listop, "sprintf") }
1577 sub pp_formline { listop(@_, "formline") } # see also deparse_format
1578 sub pp_crypt { maybe_targmy(@_, \&listop, "crypt") }
1579 sub pp_unpack { listop(@_, "unpack") }
1580 sub pp_pack { listop(@_, "pack") }
1581 sub pp_join { maybe_targmy(@_, \&listop, "join") }
1582 sub pp_splice { listop(@_, "splice") }
1583 sub pp_push { maybe_targmy(@_, \&listop, "push") }
1584 sub pp_unshift { maybe_targmy(@_, \&listop, "unshift") }
1585 sub pp_reverse { listop(@_, "reverse") }
1586 sub pp_warn { listop(@_, "warn") }
1587 sub pp_die { listop(@_, "die") }
1588 sub pp_return { listop(@_, "return", undef, 1) } # llafr does not apply
1589 sub pp_open { listop(@_, "open") }
1590 sub pp_pipe_op { listop(@_, "pipe") }
1591 sub pp_tie { listop(@_, "tie") }
1592 sub pp_binmode { listop(@_, "binmode") }
1593 sub pp_dbmopen { listop(@_, "dbmopen") }
1594 sub pp_sselect { listop(@_, "select") }
1595 sub pp_select { listop(@_, "select") }
1596 sub pp_read { listop(@_, "read") }
1597 sub pp_sysopen { listop(@_, "sysopen") }
1598 sub pp_sysseek { listop(@_, "sysseek") }
1599 sub pp_sysread { listop(@_, "sysread") }
1600 sub pp_syswrite { listop(@_, "syswrite") }
1601 sub pp_send { listop(@_, "send") }
1602 sub pp_recv { listop(@_, "recv") }
1603 sub pp_seek { listop(@_, "seek") }
1604 sub pp_fcntl { listop(@_, "fcntl") }
1605 sub pp_ioctl { listop(@_, "ioctl") }
1606 sub pp_flock { maybe_targmy(@_, \&listop, "flock") }
1607 sub pp_socket { listop(@_, "socket") }
1608 sub pp_sockpair { listop(@_, "socketpair") }
1609 sub pp_bind { listop(@_, "bind") }
1610 sub pp_connect { listop(@_, "connect") }
1611 sub pp_listen { listop(@_, "listen") }
1612 sub pp_accept { listop(@_, "accept") }
1613 sub pp_shutdown { listop(@_, "shutdown") }
1614 sub pp_gsockopt { listop(@_, "getsockopt") }
1615 sub pp_ssockopt { listop(@_, "setsockopt") }
1616 sub pp_chown { maybe_targmy(@_, \&listop, "chown") }
1617 sub pp_unlink { maybe_targmy(@_, \&listop, "unlink") }
1618 sub pp_chmod { maybe_targmy(@_, \&listop, "chmod") }
1619 sub pp_utime { maybe_targmy(@_, \&listop, "utime") }
1620 sub pp_rename { maybe_targmy(@_, \&listop, "rename") }
1621 sub pp_link { maybe_targmy(@_, \&listop, "link") }
1622 sub pp_symlink { maybe_targmy(@_, \&listop, "symlink") }
1623 sub pp_mkdir { maybe_targmy(@_, \&listop, "mkdir") }
1624 sub pp_open_dir { listop(@_, "opendir") }
1625 sub pp_seekdir { listop(@_, "seekdir") }
1626 sub pp_waitpid { maybe_targmy(@_, \&listop, "waitpid") }
1627 sub pp_system { maybe_targmy(@_, \&listop, "system") }
1628 sub pp_exec { maybe_targmy(@_, \&listop, "exec") }
1629 sub pp_kill { maybe_targmy(@_, \&listop, "kill") }
1630 sub pp_setpgrp { maybe_targmy(@_, \&listop, "setpgrp") }
1631 sub pp_getpriority { maybe_targmy(@_, \&listop, "getpriority") }
1632 sub pp_setpriority { maybe_targmy(@_, \&listop, "setpriority") }
1633 sub pp_shmget { listop(@_, "shmget") }
1634 sub pp_shmctl { listop(@_, "shmctl") }
1635 sub pp_shmread { listop(@_, "shmread") }
1636 sub pp_shmwrite { listop(@_, "shmwrite") }
1637 sub pp_msgget { listop(@_, "msgget") }
1638 sub pp_msgctl { listop(@_, "msgctl") }
1639 sub pp_msgsnd { listop(@_, "msgsnd") }
1640 sub pp_msgrcv { listop(@_, "msgrcv") }
1641 sub pp_semget { listop(@_, "semget") }
1642 sub pp_semctl { listop(@_, "semctl") }
1643 sub pp_semop { listop(@_, "semop") }
1644 sub pp_ghbyaddr { listop(@_, "gethostbyaddr") }
1645 sub pp_gnbyaddr { listop(@_, "getnetbyaddr") }
1646 sub pp_gpbynumber { listop(@_, "getprotobynumber") }
1647 sub pp_gsbyname { listop(@_, "getservbyname") }
1648 sub pp_gsbyport { listop(@_, "getservbyport") }
1649 sub pp_syscall { listop(@_, "syscall") }
1650  
1651 sub pp_glob
1652 {
1653 my($self, $op, $cx) = @_;
1654  
1655 my $opts = {other_ops => [$op->first]};
1656 my $kid = $op->first->sibling; # skip pushmark
1657 my $keyword =
1658 $op->flags & OPf_SPECIAL ? 'glob' : $self->keyword('glob');
1659  
1660 if ($keyword =~ /^CORE::/ or $kid->name ne 'const') {
1661 my $kid_info = $self->dq($kid, $op);
1662 my $body = [$kid_info];
1663 my $text = $kid_info->{text};
1664 if ($text =~ /^\$?(\w|::|\`)+$/ # could look like a readline
1665 or $text =~ /[<>]/) {
1666 $kid_info = $self->deparse($kid, 0, $op);
1667 $body = [$kid_info];
1668 $text = $kid_info->{text};
1669 $opts->{body} = $body;
1670 if ($cx >= 5 || $self->{'parens'}) {
1671 return info_from_list($op, $self, [$keyword, '(', $text, ')'], '',
1672 'glob_paren', $opts);
1673 } else {
1674 return info_from_list($op, $self, [$keyword, $text], ' ',
1675 'glob_space', $opts);
1676 }
1677 } else {
1678 return info_from_list($op, $self, ['<', $text, '>'], '', 'glob_angle', $opts);
1679 }
1680 }
1681 return info_from_list($op, $self, ['<', '>'], '', 'glob_angle', $opts);
1682 }
1683  
1684 # Truncate is special because OPf_SPECIAL makes a bareword first arg
1685 # be a filehandle. This could probably be better fixed in the core
1686 # by moving the GV lookup into ck_truc.
1687  
1688 sub pp_truncate
1689 {
1690 my($self, $op, $cx) = @_;
1691 my(@exprs);
1692 my $parens = ($cx >= 5) || $self->{'parens'};
1693 my $kid = $op->first->sibling;
1694 my $fh;
1695 if ($op->flags & OPf_SPECIAL) {
1696 # $kid is an OP_CONST
1697 $fh = $self->const_sv($kid)->PV;
1698 } else {
1699 $fh = $self->deparse($kid, 6, $op);
1700 $fh = "+$fh" if not $parens and substr($fh, 0, 1) eq "(";
1701 }
1702 my $len = $self->deparse($kid->sibling, 6, $op);
1703 my $name = $self->keyword('truncate');
1704 my $opts = {body => [$fh, $len]};
1705 my $args = "$fh->{text}, $len->{text}";
1706 if ($parens) {
1707 return info_from_list($op, $self, [$name, '(', $args, ')'], '',
1708 'truncate_parens', $opts);
1709 return "$name($fh, $len)";
1710 } else {
1711 return info_from_list($op, $self, [$name, $args], '', 'truncate', $opts);
1712 }
1713 }
1714  
1715 sub pp_list
1716 {
1717 my($self, $op, $cx) = @_;
1718 my($expr, @exprs);
1719  
1720 my $other_op = $op->first;
1721 my $kid = $op->first->sibling; # skip a pushmark
1722  
1723 if (class($kid) eq 'NULL') {
1724 return info_from_text($op, $self, '', 'list_null',
1725 {other_ops => [$other_op]});
1726 }
1727 my $lop;
1728 my $local = "either"; # could be local(...), my(...), state(...) or our(...)
1729 for ($lop = $kid; !null($lop); $lop = $lop->sibling) {
1730 # This assumes that no other private flags equal 128, and that
1731 # OPs that store things other than flags in their op_private,
1732 # like OP_AELEMFAST, won't be immediate children of a list.
1733 #
1734 # OP_ENTERSUB and OP_SPLIT can break this logic, so check for them.
1735 # I suspect that open and exit can too.
1736 # XXX This really needs to be rewritten to accept only those ops
1737 # known to take the OPpLVAL_INTRO flag.
1738  
1739 if (!($lop->private & (OPpLVAL_INTRO|OPpOUR_INTRO)
1740 or $lop->name eq "undef")
1741 or $lop->name =~ /^(?:entersub|exit|open|split)\z/)
1742 {
1743 $local = ""; # or not
1744 last;
1745 }
1746 if ($lop->name =~ /^pad[ash]v$/) {
1747 if ($lop->private & OPpPAD_STATE) { # state()
1748 ($local = "", last) if $local =~ /^(?:local|our|my)$/;
1749 $local = "state";
1750 } else { # my()
1751 ($local = "", last) if $local =~ /^(?:local|our|state)$/;
1752 $local = "my";
1753 }
1754 } elsif ($lop->name =~ /^(gv|rv2)[ash]v$/
1755 && $lop->private & OPpOUR_INTRO
1756 or $lop->name eq "null" && $lop->first->name eq "gvsv"
1757 && $lop->first->private & OPpOUR_INTRO) { # our()
1758 ($local = "", last) if $local =~ /^(?:my|local|state)$/;
1759 $local = "our";
1760 } elsif ($lop->name ne "undef"
1761 # specifically avoid the "reverse sort" optimisation,
1762 # where "reverse" is nullified
1763 && !($lop->name eq 'sort' && ($lop->flags & OPpSORT_REVERSE)))
1764 {
1765 # local()
1766 ($local = "", last) if $local =~ /^(?:my|our|state)$/;
1767 $local = "local";
1768 }
1769 }
1770 $local = "" if $local eq "either"; # no point if it's all undefs
1771 if (null $kid->sibling and not $local) {
1772 my $info = $self->deparse($kid, $cx, $op);
1773 my @other_ops = $info->{other_ops} ? @{$info->{other_ops}} : ();
1774 push @other_ops, $other_op;
1775 $info->{other_ops} = \@other_ops;
1776 return $info;
1777 }
1778  
1779 for (; !null($kid); $kid = $kid->sibling) {
1780 if ($local) {
1781 if (class($kid) eq "UNOP" and $kid->first->name eq "gvsv") {
1782 $lop = $kid->first;
1783 } else {
1784 $lop = $kid;
1785 }
1786 $self->{'avoid_local'}{$$lop}++;
1787 $expr = $self->deparse($kid, 6, $op);
1788 delete $self->{'avoid_local'}{$$lop};
1789 } else {
1790 $expr = $self->deparse($kid, 6, $op);
1791 }
1792 push @exprs, $expr;
1793 }
1794  
1795 my $type;
1796 my @texts;
1797 my $opts = {};
1798 if ($local) {
1799 $type = 'list_local';
1800 @texts = ($local, '(', $self->combine(", ", \@exprs), ')');
1801 } else {
1802 $type = 'list';
1803 @texts = ($self->combine(", ", \@exprs));
1804 $opts->{maybe_parens} = [$self, $cx, 6];
1805  
1806 }
1807 return info_from_list($op, $self, \@texts, '', $type, $opts);
1808 }
1809  
1810 sub loop_common
1811 {
1812 my $self = shift;
1813 my($op, $cx, $init) = @_;
1814 my $enter = $op->first;
1815 my $kid = $enter->sibling;
1816 local(@$self{qw'curstash warnings hints hinthash'})
1817 = @$self{qw'curstash warnings hints hinthash'};
1818  
1819 my ($body, @body, @other_ops, $type);
1820 my (@head, @head_text) = ((), ());
1821 my ($bare, $cond_info) = (0, undef);
1822  
1823 if ($kid->name eq "lineseq") {
1824 # bare or infinite loop
1825 $type = 'loop_bare_or_infinite';
1826  
1827 if ($kid->last->name eq "unstack") { # infinite
1828 @head_text = ("while", "(1)"); # Can't use for(;;) if there's a continue
1829 } else {
1830 $bare = 1;
1831 }
1832 $body = $kid;
1833 } elsif ($enter->name eq "enteriter") {
1834 # foreach
1835 $type = 'loop_foreach';
1836  
1837 push @other_ops, $enter->first;
1838 my $ary = $enter->first->sibling; # first was pushmark
1839 my @ary_info;
1840 my @ary_text;
1841 my $var = $ary->sibling;
1842 if ($ary->name eq 'null' and $enter->private & OPpITER_REVERSED) {
1843 # "reverse" was optimised away
1844 @ary_info = (listop($self, $ary->first->sibling, 1, 'reverse'));
1845 @ary_text = ($ary_info[0]->{text});
1846 } elsif ($enter->flags & OPf_STACKED
1847 and not null $ary->first->sibling->sibling) {
1848 @ary_info = ($self->deparse($ary->first->sibling, 9, $op),
1849 $self->deparse($ary->first->sibling->sibling, 9, $op));
1850 @ary_text = ($ary_info[0]->{text}, "..", $ary_info[1]->{text});
1851  
1852 } else {
1853 @ary_info = ($self->deparse($ary, 1, $op));
1854 @ary_text = $ary_info[0]->{text};
1855 }
1856  
1857 my @var_info = ();
1858 my @var_text;
1859 if (null $var) {
1860 if (($enter->flags & OPf_SPECIAL) && ($] < 5.009)) {
1861 # thread special var, under 5005threads
1862 @var_text = ($self->pp_threadsv($enter, 1));
1863 } else { # regular my() variable
1864 my $var_info = $self->pp_padsv($enter, 1, 1);
1865 @var_info =($var_info);
1866 @var_text = ($var_info->{text});
1867 }
1868 } elsif ($var->name eq "rv2gv") {
1869 @var_info = ($self->pp_rv2sv($var, 1));
1870 @var_text = ($var_info[0]->{text});
1871 if ($enter->private & OPpOUR_INTRO) {
1872 # our declarations don't have package names
1873 $var_text[0] =~ s/^(.).*::/$1/;
1874 unshift @var_text, 'our';
1875 }
1876 } elsif ($var->name eq "gv") {
1877 @var_info = ($self->deparse($var, 1, $op));
1878 @var_text = ("\$" . $var_info[0]->{text});
1879 }
1880  
1881 # skip OP_AND and OP_ITER
1882 push @other_ops, $kid->first, $kid->first->first;
1883 $body = $kid->first->first->sibling;
1884  
1885 @head = (@ary_info, @var_info);
1886 if (!is_state $body->first and $body->first->name !~ /^(?:stub|leave|scope)$/) {
1887 Carp::confess("var ne \$_") unless join('', @var_text) eq '$_';
1888 $body = $body->first;
1889 my $body_info = $self->deparse($body, 2, $op);
1890 push @head, $body_info;
1891 my @texts = ($body_info->{text}, "foreach", '(', @ary_text, ')');
1892 return info_from_list($op, $self, \@texts, ' ', 'loop_foreach_ary',
1893 {other_ops => \@other_ops});
1894 }
1895 @head_text = ("foreach", @var_text, '(', @ary_text, ')');
1896 } elsif ($kid->name eq "null") {
1897 # while/until
1898 $type = 'loop_while_until';
1899  
1900 $kid = $kid->first;
1901 my $name = {"and" => "while", "or" => "until"}->{$kid->name};
1902 $cond_info = $self->deparse($kid->first, 1, $op);
1903 @head_text = ($name, "($cond_info->{text})");
1904 $body = $kid->first->sibling;
1905 } elsif ($kid->name eq "stub") {
1906 # bare and empty
1907 return info_from_list($op, $self, ['{', ';', '}'], '', 'loop_stub', {});
1908 }
1909  
1910 # If there isn't a continue block, then the next pointer for the loop
1911 # will point to the unstack, which is kid's last child, except
1912 # in a bare loop, when it will point to the leaveloop. When neither of
1913 # these conditions hold, then the second-to-last child is the continue
1914 # block (or the last in a bare loop).
1915 my $cont_start = $enter->nextop;
1916 my ($cont, @cont_text, $body_info);
1917 my @cont = ();
1918 if ($$cont_start != $$op && ${$cont_start} != ${$body->last}) {
1919 $type .= '_continue';
1920  
1921 if ($bare) {
1922 $cont = $body->last;
1923 } else {
1924 $cont = $body->first;
1925 while (!null($cont->sibling->sibling)) {
1926 $cont = $cont->sibling;
1927 }
1928 }
1929 my $state = $body->first;
1930 my $cuddle = " ";
1931 my @states;
1932 for (; $$state != $$cont; $state = $state->sibling) {
1933 push @states, $state;
1934 }
1935 $body_info = $self->lineseq(undef, 0, @states);
1936 if (defined $cond_info and not is_scope $cont and $self->{'expand'} < 3) {
1937 my $cont_info = $self->deparse($cont, 1, $op);
1938 push @head, $cont_info;
1939 my $init_text = defined($init) ? $init->{text} : ' ';
1940 @head_text = ('for', '(', "$init_text;", $cont_info->{text}, ')');
1941 @cont_text = ("\cK");
1942 } else {
1943 my $cont_info = $self->deparse($cont, 0, $op);
1944 push @head, $cont_info;
1945 @cont_text = ($cuddle, 'continue', "{\n\t",
1946 $cont_info->{text} , "\n\b}\cK");
1947 }
1948 } else {
1949 return info_from_text($op, $self, [''], 'loop_no_body', {}) if !defined $body;
1950 if (defined $init) {
1951 @head_text = ('for', '(', "$init->{text};", "$cond_info->{text};", ")");
1952 }
1953 @cont_text = ("\cK");
1954 $body_info = $self->deparse($body, 0, $op);
1955 }
1956 (my $body_text = $body_info->{text}) =~ s/;?$/;\n/;
1957  
1958 my @texts = (@head_text, "{\n\t", $body_text, "\b}", @cont_text);
1959 return info_from_list($op, $self, \@texts, ' ', $type,
1960 {body => [$body_info]});
1961 }
1962  
1963 sub pp_leaveloop {
1964 shift->loop_common(@_, undef)
1965 }
1966  
1967 sub for_loop {
1968 my $self = shift;
1969 my($op, $cx, $parent) = @_;
1970 my $init = $self->deparse($op, 1, $parent);
1971 my $s = $op->sibling;
1972 my $ll = $s->name eq "unstack" ? $s->sibling : $s->first->sibling;
1973 return $self->loop_common($ll, $cx, $init);
1974 }
1975  
1976 sub pp_leavetry {
1977 my ($self, $op, $cx) = @_;
1978 my $leave_info = $self->pp_leave($op, $cx);
1979 return info_from_list($op, $self, ['eval', '{\n\t"', $leave_info->{text}, "\n\b}"],
1980 ' ', 'leavetry', {body=>[$leave_info]});
1981 }
1982  
1983 sub _op_is_or_was {
1984 my ($op, $expect_type) = @_;
1985 my $type = $op->type;
1986 return($type == $expect_type
1987 || ($type == OP_NULL && $op->targ == $expect_type));
1988 }
1989  
1990 sub padname {
1991 my $self = shift;
1992 my $targ = shift;
1993 return $self->padname_sv($targ)->PVX;
1994 }
1995  
1996 sub padany {
1997 my $self = shift;
1998 my $op = shift;
1999 return substr($self->padname($op->targ), 1); # skip $/@/%
2000 }
2001  
2002 sub pp_padsv {
2003 my $self = shift;
2004 my($op, $cx, $forbid_parens) = @_;
2005 return $self->maybe_my($op, $cx, $self->padname($op->targ),
2006 $forbid_parens);
2007 }
2008  
2009 sub pp_padav { pp_padsv(@_) }
2010 sub pp_padhv { pp_padsv(@_) }
2011  
2012 my @threadsv_names = B::threadsv_names;
2013 sub pp_threadsv {
2014 my $self = shift;
2015 my($op, $cx) = @_;
2016 return $self->maybe_local_str($op, $cx, "\$" . $threadsv_names[$op->targ]);
2017 }
2018  
2019 sub gv_or_padgv {
2020 my $self = shift;
2021 my $op = shift;
2022 if (class($op) eq "PADOP") {
2023 return $self->padval($op->padix);
2024 } else { # class($op) eq "SVOP"
2025 return $op->gv;
2026 }
2027 }
2028  
2029 sub pp_gvsv
2030 {
2031 my($self, $op, $cx) = @_;
2032 my $gv = $self->gv_or_padgv($op);
2033 return $self->maybe_local_str($op, $cx,
2034 $self->stash_variable("\$",
2035 $self->gv_name($gv), $cx));
2036 }
2037  
2038 sub pp_gv
2039 {
2040 my($self, $op, $cx) = @_;
2041 my $gv = $self->gv_or_padgv($op);
2042 return info_from_text($op, $self, $self->gv_name($gv), 'pp_gv', {});
2043 }
2044  
2045 sub pp_aelemfast_lex
2046 {
2047 my($self, $op, $cx) = @_;
2048 my $name = $self->padname($op->targ);
2049 $name =~ s/^@/\$/;
2050 return info_from_list($op, $self, [$name, "[", ($op->private + $self->{'arybase'}), "]"],
2051 '', 'pp_aelemfast_lex', {});
2052 }
2053  
2054 sub pp_aelemfast
2055 {
2056 my($self, $op, $cx) = @_;
2057 # optimised PADAV, pre 5.15
2058 return $self->pp_aelemfast_lex(@_) if ($op->flags & OPf_SPECIAL);
2059  
2060 my $gv = $self->gv_or_padgv($op);
2061 my($name,$quoted) = $self->stash_variable_name('@',$gv);
2062 $name = $quoted ? "$name->" : '$' . $name;
2063 my $i = $op->private;
2064 $i -= 256 if $i > 127;
2065 return info_from_list($op, $self, [$name, "[", ($op->private + $self->{'arybase'}), "]"],
2066 '', 'pp_aelemfast', {});
2067 }
2068  
2069 sub rv2x
2070 {
2071 my($self, $op, $cx, $type) = @_;
2072  
2073 if (class($op) eq 'NULL' || !$op->can("first")) {
2074 carp("Unexpected op in pp_rv2x");
2075 return info_from_text($op, $self, 'XXX', 'bad_rv2x', {});
2076 }
2077 my ($info, $kid_info);
2078 my $kid = $op->first;
2079 if ($kid->name eq "gv") {
2080 $kid_info = $self->deparse($kid, 0, $op);
2081 my $str = $self->stash_variable($type, $kid_info->{text}, $cx);
2082 return info_from_text($op, $self, $str, 'rv2x_gv', {body => [$kid_info]});
2083 } elsif (is_scalar $kid) {
2084 $kid_info = $self->deparse($kid, 0, $op);
2085 my $str = $kid_info->{text};
2086 if ($str =~ /^\$([^\w\d])\z/) {
2087 # "$$+" isn't a legal way to write the scalar dereference
2088 # of $+, since the lexer can't tell you aren't trying to
2089 # do something like "$$ + 1" to get one more than your
2090 # PID. Either "${$+}" or "$${+}" are workable
2091 # disambiguations, but if the programmer did the former,
2092 # they'd be in the "else" clause below rather than here.
2093 # It's not clear if this should somehow be unified with
2094 # the code in dq and re_dq that also adds lexer
2095 # disambiguation braces.
2096 $str = '$' . "{$1}"; #'
2097 }
2098 return info_from_list($op, $self, [$type, $str], '', 'rv2x_scalar',
2099 {body => [$kid_info]});
2100 } else {
2101 my $kid_info = $self->deparse($kid, 0, $op);
2102 return info_from_list($op, $self, [$type, "{", "}"], '', 'rv2x',
2103 {body => [$kid_info]});
2104 }
2105 Carp::confess("unhandled condition in rv2x");
2106 }
2107  
2108 sub pp_rv2sv { maybe_local(@_, rv2x(@_, "\$")) }
2109 sub pp_rv2hv { maybe_local(@_, rv2x(@_, "%")) }
2110 sub pp_rv2gv { maybe_local(@_, rv2x(@_, "*")) }
2111  
2112 # skip rv2av
2113 sub pp_av2arylen {
2114 my $self = shift;
2115 my($op, $cx) = @_;
2116 if ($op->first->name eq "padav") {
2117 return $self->maybe_local_str($op, $cx, '$#' . $self->padany($op->first));
2118 } else {
2119 return $self->maybe_local($op, $cx,
2120 $self->rv2x($op->first, $cx, '$#'));
2121 }
2122 }
2123  
2124 # skip down to the old, ex-rv2cv
2125 sub pp_rv2cv {
2126 my ($self, $op, $cx) = @_;
2127 if (!null($op->first) && $op->first->name eq 'null' &&
2128 $op->first->targ == OP_LIST)
2129 {
2130 return $self->rv2x($op->first->first->sibling, $cx, "&")
2131 }
2132 else {
2133 return $self->rv2x($op, $cx, "")
2134 }
2135 }
2136  
2137 sub list_const($$$) {
2138 my $self = shift;
2139 my($op, $cx, @list) = @_;
2140 my @a = map $self->const($_, 6), @list;
2141 my @texts = $self->map_texts(\@a);
2142 my $type = 'list_const';
2143 my $prec = 6;
2144 if (@texts == 0) {
2145 return info_from_list($op, $self, ['(', ')'], '', 'list_const_null', {});
2146 } elsif (@texts == 1) {
2147 return info_from_text($op, $self, $texts[0], 'list_const_one',
2148 {body => \@a});
2149 } elsif ( @texts > 2 and !grep(!/^-?\d+$/, @texts)) {
2150 # collapse (-1,0,1,2) into (-1..2)
2151 my ($s, $e) = @texts[0,-1];
2152 my $i = $s;
2153 unless (grep $i++ != $_, @texts) {
2154 @texts = ($s, '..', $e);
2155 $type = 'list_const_range';
2156 $prec = 9;
2157 }
2158 }
2159 return info_from_list('const', $self, \@texts, '', $type,
2160 {maybe_parens => [$self, $cx, $prec]});
2161 }
2162  
2163 sub pp_rv2av {
2164 my $self = shift;
2165 my($op, $cx) = @_;
2166 my $kid = $op->first;
2167 if ($kid->name eq "const") { # constant list
2168 my $av = $self->const_sv($kid);
2169 return $self->list_const($kid, $cx, $av->ARRAY);
2170 } else {
2171 return $self->maybe_local($op, $cx, $self->rv2x($op, $cx, "\@"));
2172 }
2173 }
2174  
2175 sub elem_or_slice_array_name
2176 {
2177 my $self = shift;
2178 my ($array, $left, $padname, $allow_arrow) = @_;
2179  
2180 if ($array->name eq $padname) {
2181 return $self->padany($array);
2182 } elsif (is_scope($array)) { # ${expr}[0]
2183 return "{" . $self->deparse($array, 0) . "}";
2184 } elsif ($array->name eq "gv") {
2185 ($array, my $quoted) =
2186 $self->stash_variable_name(
2187 $left eq '[' ? '@' : '%', $self->gv_or_padgv($array)
2188 );
2189 if (!$allow_arrow && $quoted) {
2190 # This cannot happen.
2191 die "Invalid variable name $array for slice";
2192 }
2193 return $quoted ? "$array->" : $array;
2194 } elsif (!$allow_arrow || is_scalar $array) { # $x[0], $$x[0], ...
2195 return $self->deparse($array, 24)->{text};
2196 } else {
2197 return undef;
2198 }
2199 }
2200  
2201 sub elem_or_slice_single_index($$)
2202 {
2203 my ($self, $idx, $parent) = @_;
2204  
2205 my $idx_info = $self->deparse($idx, 1, $parent);
2206 my $idx_str = $idx_info->{text};
2207  
2208 # Outer parens in an array index will confuse perl
2209 # if we're interpolating in a regular expression, i.e.
2210 # /$x$foo[(-1)]/ is *not* the same as /$x$foo[-1]/
2211 #
2212 # If $self->{parens}, then an initial '(' will
2213 # definitely be paired with a final ')'. If
2214 # !$self->{parens}, the misleading parens won't
2215 # have been added in the first place.
2216 #
2217 # [You might think that we could get "(...)...(...)"
2218 # where the initial and final parens do not match
2219 # each other. But we can't, because the above would
2220 # only happen if there's an infix binop between the
2221 # two pairs of parens, and *that* means that the whole
2222 # expression would be parenthesized as well.]
2223 #
2224 $idx_str =~ s/^\((.*)\)$/$1/ if $self->{'parens'};
2225  
2226 # Hash-element braces will autoquote a bareword inside themselves.
2227 # We need to make sure that C<$hash{warn()}> doesn't come out as
2228 # C<$hash{warn}>, which has a quite different meaning. Currently
2229 # B::Deparse will always quote strings, even if the string was a
2230 # bareword in the original (i.e. the OPpCONST_BARE flag is ignored
2231 # for constant strings.) So we can cheat slightly here - if we see
2232 # a bareword, we know that it is supposed to be a function call.
2233 #
2234 $idx_str =~ s/^([A-Za-z_]\w*)$/$1()/;
2235  
2236 return info_from_text($idx_info->{op}, $self, $idx_str,
2237 'elem_or_slice_single_index',
2238 {body => [$idx_info]});
2239 }
2240  
2241 sub elem
2242 {
2243 my ($self, $op, $cx, $left, $right, $padname) = @_;
2244 my($array, $idx) = ($op->first, $op->first->sibling);
2245  
2246 my $idx_info = $self->elem_or_slice_single_index($idx, $op);
2247 my $opts = {body => [$idx_info]};
2248  
2249 unless ($array->name eq $padname) { # Maybe this has been fixed
2250 $opts->{other_ops} = [$array];
2251 $array = $array->first; # skip rv2av (or ex-rv2av in _53+)
2252 }
2253 my @texts = ();
2254 my $info;
2255 my $array_name=$self->elem_or_slice_array_name($array, $left, $padname, 1);
2256 if ($array_name) {
2257 if ($array_name !~ /->\z/) {
2258 if ($array_name eq '#') {
2259 $array_name = '${#}';
2260 } else {
2261 $array_name = '$' . $array_name ;
2262 }
2263 }
2264 push @texts, $array_name;
2265 push @texts, $left if $left;
2266 push @texts, $idx_info->{text}, $right;
2267 return info_from_list($op, $self, \@texts, '', 'elem', $opts)
2268 } else {
2269 # $x[20][3]{hi} or expr->[20]
2270 my $type;
2271 my $array_info = $self->deparse($array, 24, $op);
2272 push @{$info->{body}}, $array_info;
2273 @texts = ($array_info->{text});
2274 if (is_subscriptable($array)) {
2275 push @texts, $left, $idx_info->{text}, $right;
2276 $type = 'elem_no_arrow';
2277 } else {
2278 push @texts, '->', $left, $idx_info->{text}, $right;
2279 $type = 'elem_arrow';
2280 }
2281 return info_from_list($op, $self, \@texts, '', $type, $opts);
2282 }
2283 Carp::confess("unhandled condition in elem");
2284 }
2285  
2286 sub pp_aelem { maybe_local(@_, elem(@_, "[", "]", "padav")) }
2287 sub pp_helem { maybe_local(@_, elem(@_, "{", "}", "padhv")) }
2288  
2289 sub pp_gelem
2290 {
2291 my($self, $op, $cx) = @_;
2292 my($glob, $part) = ($op->first, $op->last);
2293 $glob = $glob->first; # skip rv2gv
2294 $glob = $glob->first if $glob->name eq "rv2gv"; # this one's a bug
2295 my $scope = is_scope($glob);
2296 $glob = $self->deparse($glob, 0);
2297 $part = $self->deparse($part, 1);
2298 return "*" . ($scope ? "{$glob}" : $glob) . "{$part}";
2299 }
2300  
2301 sub slice
2302 {
2303 my ($self, $op, $cx, $left, $right, $regname, $padname) = @_;
2304 my $last;
2305 my(@elems, $kid, $array);
2306 if (class($op) eq "LISTOP") {
2307 $last = $op->last;
2308 } else { # ex-hslice inside delete()
2309 for ($kid = $op->first; !null $kid->sibling; $kid = $kid->sibling) {}
2310 $last = $kid;
2311 }
2312 $array = $last;
2313 $array = $array->first
2314 if $array->name eq $regname or $array->name eq "null";
2315 my $array_info = $self->elem_or_slice_array_name($array, $left, $padname, 0);
2316 $kid = $op->first->sibling; # skip pushmark
2317  
2318 if ($kid->name eq "list") {
2319 # skip list, pushmark
2320 $kid = $kid->first->sibling;
2321 for (; !null $kid; $kid = $kid->sibling) {
2322 push @elems, $self->deparse($kid, 6, $op);
2323 }
2324 } else {
2325 @elems = ($self->elem_or_slice_single_index($kid, $op));
2326 }
2327 my $list = join(', ', map($_->{text}, @elems));
2328 my $lead = '@';
2329 $lead = '%' if $op->name =~ /^kv/i;
2330 my (@texts, $type);
2331 if ($array_info) {
2332 @texts = ($lead, $array_info, $left, $list, $right);
2333 $type='slice1';
2334 } else {
2335 @texts = ($lead, $left, $list, $right);
2336 $type='slice';
2337 }
2338 return info_from_list($op, $self, \@texts, '', $type, {body => \@elems});
2339 }
2340  
2341 sub pp_aslice { maybe_local(@_, slice(@_, "[", "]", "rv2av", "padav")) }
2342 sub pp_kvaslice { slice(@_, "[", "]", "rv2av", "padav") }
2343 sub pp_hslice { maybe_local(@_, slice(@_, "{", "}", "rv2hv", "padhv")) }
2344 sub pp_kvhslice { slice(@_, "{", "}", "rv2hv", "padhv") }
2345  
2346 sub pp_lslice
2347 {
2348 my ($self, $op, $cs) = @_;
2349 my $idx = $op->first;
2350 my $list = $op->last;
2351 my(@elems, $kid);
2352 my $list_info = $self->deparse($list, 1, $op);
2353 my $idx_info = $self->deparse($idx, 1, $op);
2354 return info_from_list($op, $self, ['(', $list_info->{text}, ')', '[', $idx_info->{text}, ']'],
2355 '', 'lslice', {body=>[$list_info, $idx_info]});
2356 }
2357  
2358 sub want_scalar
2359 {
2360 my $op = shift;
2361 return ($op->flags & OPf_WANT) == OPf_WANT_SCALAR;
2362 }
2363  
2364 sub want_list
2365 {
2366 my $op = shift;
2367 return ($op->flags & OPf_WANT) == OPf_WANT_LIST;
2368 }
2369  
2370 sub _method
2371 {
2372 my($self, $op, $cx) = @_;
2373 my @other_ops = ($op->first);
2374 my $kid = $op->first->sibling; # skip pushmark
2375 my($meth, $obj, @exprs);
2376 if ($kid->name eq "list" and want_list $kid) {
2377 # When an indirect object isn't a bareword but the args are in
2378 # parens, the parens aren't part of the method syntax (the LLAFR
2379 # doesn't apply), but they make a list with OPf_PARENS set that
2380 # doesn't get flattened by the append_elem that adds the method,
2381 # making a (object, arg1, arg2, ...) list where the object
2382 # usually is. This can be distinguished from
2383 # '($obj, $arg1, $arg2)->meth()' (which is legal if $arg2 is an
2384 # object) because in the later the list is in scalar context
2385 # as the left side of -> always is, while in the former
2386 # the list is in list context as method arguments always are.
2387 # (Good thing there aren't method prototypes!)
2388 $meth = $kid->sibling;
2389 push @other_ops, $kid->first;
2390 $kid = $kid->first->sibling; # skip pushmark
2391 $obj = $kid;
2392 $kid = $kid->sibling;
2393 for (; not null $kid; $kid = $kid->sibling) {
2394 push @exprs, $kid;
2395 }
2396 } else {
2397 $obj = $kid;
2398 $kid = $kid->sibling;
2399 for (; !null ($kid->sibling) && $kid->name!~/^method(?:_named)?\z/;
2400 $kid = $kid->sibling) {
2401 push @exprs, $kid
2402 }
2403 $meth = $kid;
2404 }
2405  
2406 if ($meth->name eq "method_named") {
2407 $meth = $self->const_sv($meth)->PV;
2408 } else {
2409 $meth = $meth->first;
2410 if ($meth->name eq "const") {
2411 # As of 5.005_58, this case is probably obsoleted by the
2412 # method_named case above
2413 $meth = $self->const_sv($meth)->PV; # needs to be bare
2414 }
2415 }
2416  
2417 return {
2418 method => $meth,
2419 variable_method => ref($meth),
2420 object => $obj,
2421 args => \@exprs,
2422 other_ops => \@other_ops
2423 }, $cx;
2424 }
2425  
2426 sub e_method {
2427 my ($self, $op, $minfo, $cx) = @_;
2428 my $obj = $self->deparse($minfo->{object}, 24, $op);
2429 my @body = ($obj);
2430 my $other_ops = $minfo->{other_ops};
2431  
2432 my $meth_name = $minfo->{method};
2433 my $meth_info;
2434 if ($minfo->{variable_method}) {
2435 $meth_info = $self->deparse($meth_name, 1, $op);
2436 push @body, $meth_info;
2437 }
2438 my @args = map { $self->deparse($_, 6, $op) } @{$minfo->{args}};
2439 push @body, @args;
2440 my @args_texts = map $_->{text}, @args;
2441 my $args = join(", ", @args_texts);
2442  
2443 my $opts = {other_ops => $other_ops};
2444 my @texts = ();
2445 my $type;
2446  
2447  
2448 my $meth_object = $meth_info ? defined($meth_info) : $meth_name;
2449 if ($minfo->{object}->name eq 'scope' && want_list $minfo->{object}) {
2450 # method { $object }
2451 # This must be deparsed this way to preserve list context
2452 # of $object.
2453 my $need_paren = $cx >= 6;
2454 if ($need_paren) {
2455 @texts = ('(', $meth_object, substr($obj,2),
2456 $args, ')');
2457 $type = 'e_method list ()';
2458 } else {
2459 @texts = ($meth_object, substr($obj,2), $args);
2460 $type = 'e_method list, no ()';
2461 }
2462 return info_from_list($op, $self, \@texts, '', $type, $opts);
2463 }
2464  
2465 if (length $args) {
2466 @texts = ($obj, '->', $meth_object, '(', $args, ')');
2467 $type = 'e_method -> ()';
2468 } else {
2469 @texts = ($obj, '->', $meth_object);
2470 $type = 'e_method -> no ()';
2471 }
2472 return info_from_list($op, $self, \@texts, '', $type, $opts);
2473 }
2474  
2475 # returns "&" and the argument bodies if the prototype doesn't match the args,
2476 # or ("", $args_after_prototype_demunging) if it does.
2477 sub check_proto {
2478 my $self = shift;
2479 my $op = shift;
2480 return ('&', []) if $self->{'noproto'};
2481 my($proto, @args) = @_;
2482 my($arg, $real);
2483 my $doneok = 0;
2484 my @reals;
2485 # An unbackslashed @ or % gobbles up the rest of the args
2486 1 while $proto =~ s/(?<!\\)([@%])[^\]]+$/$1/;
2487 $proto =~ s/^\s*//;
2488 while ($proto) {
2489 $proto =~ s/^(\\?[\$\@&%*_]|\\\[[\$\@&%*]+\]|;)\s*//;
2490 my $chr = $1;
2491 if ($chr eq "") {
2492 return ('&', []) if @args;
2493 } elsif ($chr eq ";") {
2494 $doneok = 1;
2495 } elsif ($chr eq "@" or $chr eq "%") {
2496 push @reals, map($self->deparse($_, 6), @args, $op);
2497 @args = ();
2498 } else {
2499 $arg = shift @args;
2500 last unless $arg;
2501 if ($chr eq "\$" || $chr eq "_") {
2502 if (want_scalar $arg) {
2503 push @reals, $self->deparse($arg, 6, $op);
2504 } else {
2505 return ('&', []);
2506 }
2507 } elsif ($chr eq "&") {
2508 if ($arg->name =~ /^(s?refgen|undef)$/) {
2509 push @reals, $self->deparse($arg, 6, $op);
2510 } else {
2511 return ('&', []);
2512 }
2513 } elsif ($chr eq "*") {
2514 if ($arg->name =~ /^s?refgen$/
2515 and $arg->first->first->name eq "rv2gv")
2516 {
2517 $real = $arg->first->first; # skip refgen, null
2518 if ($real->first->name eq "gv") {
2519 push @reals, $self->deparse($real, 6, $op);
2520 } else {
2521 push @reals, $self->deparse($real->first, 6, $op);
2522 }
2523 } else {
2524 return ('&', []);
2525 }
2526 } elsif (substr($chr, 0, 1) eq "\\") {
2527 $chr =~ tr/\\[]//d;
2528 if ($arg->name =~ /^s?refgen$/ and
2529 !null($real = $arg->first) and
2530 ($chr =~ /\$/ && is_scalar($real->first)
2531 or ($chr =~ /@/
2532 && class($real->first->sibling) ne 'NULL'
2533 && $real->first->sibling->name
2534 =~ /^(rv2|pad)av$/)
2535 or ($chr =~ /%/
2536 && class($real->first->sibling) ne 'NULL'
2537 && $real->first->sibling->name
2538 =~ /^(rv2|pad)hv$/)
2539 #or ($chr =~ /&/ # This doesn't work
2540 # && $real->first->name eq "rv2cv")
2541 or ($chr =~ /\*/
2542 && $real->first->name eq "rv2gv")))
2543 {
2544 push @reals, $self->deparse($real, 6, $op);
2545 } else {
2546 return ('&', []);
2547 }
2548 }
2549 }
2550 }
2551 return ('&', []) if $proto and !$doneok; # too few args and no ';'
2552 return ('&', []) if @args; # too many args
2553 return ('', \@reals);
2554 }
2555  
2556 sub pp_enterwrite { unop(@_, "write") }
2557  
2558 # For regexes with the /x modifier.
2559 # Leave whitespace unmangled.
2560 sub escape_extended_re {
2561 my($str) = @_;
2562 $str =~ s/(.)/ord($1) > 255 ? sprintf("\\x{%x}", ord($1)) : $1/eg;
2563 $str =~ s/([[:^print:]])/
2564 ($1 =~ y! \t\n!!) ? $1 : sprintf("\\%03o", ord($1))/ge;
2565 $str =~ s/\n/\n\f/g;
2566 return $str;
2567 }
2568  
2569 my $max_prec;
2570 BEGIN { $max_prec = int(0.999 + 8*length(pack("F", 42))*log(2)/log(10)); }
2571  
2572 # Split a floating point number into an integer mantissa and a binary
2573 # exponent. Assumes you've already made sure the number isn't zero or
2574 # some weird infinity or NaN.
2575 sub split_float {
2576 my($f) = @_;
2577 my $exponent = 0;
2578 if ($f == int($f)) {
2579 while ($f % 2 == 0) {
2580 $f /= 2;
2581 $exponent++;
2582 }
2583 } else {
2584 while ($f != int($f)) {
2585 $f *= 2;
2586 $exponent--;
2587 }
2588 }
2589 my $mantissa = sprintf("%.0f", $f);
2590 return ($mantissa, $exponent);
2591 }
2592  
2593 sub pp_const {
2594 my $self = shift;
2595 my($op, $cx) = @_;
2596 if ($op->private & OPpCONST_ARYBASE) {
2597 return info_from_text($op, $self, '$[', 'const_ary', {});
2598 }
2599 # if ($op->private & OPpCONST_BARE) { # trouble with '=>' autoquoting
2600 # return $self->const_sv($op)->PV;
2601 # }
2602 my $sv = $self->const_sv($op);
2603 return $self->const($sv, $cx);;
2604 }
2605  
2606 sub dq
2607 {
2608 my ($self, $op, $parent) = @_;
2609 my $type = $op->name;
2610 my $info;
2611 if ($type eq "const") {
2612 return info_from_text($op, $self, '$[', 'dq_const_ary', {}) if $op->private & OPpCONST_ARYBASE;
2613 return info_from_text($op, $self,
2614 B::Deparse::uninterp(B::Deparse::escape_str(B::Deparse::unback($self->const_sv($op)->as_string))),
2615 'dq_const', {});
2616 } elsif ($type eq "concat") {
2617 my $first = $self->dq($op->first, $op);
2618 my $last = $self->dq($op->last, $op);
2619  
2620 # Disambiguate "${foo}bar", "${foo}{bar}", "${foo}[1]", "$foo\::bar"
2621 ($last =~ /^[A-Z\\\^\[\]_?]/ &&
2622 $first =~ s/([\$@])\^$/${1}{^}/) # "${^}W" etc
2623 || ($last =~ /^[:'{\[\w_]/ && #'
2624 $first =~ s/([\$@])([A-Za-z_]\w*)$/${1}{$2}/);
2625  
2626 return info_from_list($op, $self, [$first->{text}, $last->{text}], '', 'dq_concat',
2627 {body => [$first, $last]});
2628 } elsif ($type eq "join") {
2629 return $self->deparse($op->last, 26, $op); # was join($", @ary)
2630 } else {
2631 return $self->deparse($op, 26, $parent);
2632 }
2633 my $kid = $self->dq($op->first->sibling, $op);
2634 my $kid_text = $kid->{text};
2635 if ($type eq "uc") {
2636 $info = info_from_lists(['\U', $kid, '\E'], '', 'dq_uc', {});
2637 } elsif ($type eq "lc") {
2638 $info = info_from_lists(['\L', $kid, '\E'], '', 'dq_lc', {});
2639 } elsif ($type eq "ucfirst") {
2640 $info = info_from_lists(['\u', $kid, '\E'], '', 'dq_ucfirst', {});
2641 } elsif ($type eq "lcfirst") {
2642 $info = info_from_lists(['\l', $kid, '\E'], '', 'dq_lcfirst', {});
2643 } elsif ($type eq "quotemeta") {
2644 $info = info_from_lists(['\Q', $kid, '\E'], '', 'dq_quotemeta', {});
2645 } elsif ($type eq "fc") {
2646 $info = info_from_lists(['\F', $kid, '\E'], '', 'dq_fc', {});
2647 }
2648 $info->{body} = [$kid];
2649 return $info;
2650 }
2651  
2652 sub pp_backtick
2653 {
2654 my($self, $op, $cx) = @_;
2655 # skip pushmark if it exists (readpipe() vs ``)
2656 my $child = $op->first->sibling->isa('B::NULL')
2657 ? $op->first : $op->first->sibling;
2658 if ($self->pure_string($child)) {
2659 return $self->single_delim($op, "qx", '`', $self->dq($child, 1)->{text});
2660 }
2661 unop($self, $op, $cx, "readpipe");
2662 }
2663  
2664 # OP_STRINGIFY is a listop, but it only ever has one arg
2665 sub pp_stringify { maybe_targmy(@_, \&dquote) }
2666  
2667 # tr/// and s/// (and tr[][], tr[]//, tr###, etc)
2668 # note that tr(from)/to/ is OK, but not tr/from/(to)
2669 sub double_delim {
2670 my($from, $to) = @_;
2671 my($succeed, $delim);
2672 if ($from !~ m[/] and $to !~ m[/]) {
2673 return "/$from/$to/";
2674 } elsif (($succeed, $from) = balanced_delim($from) and $succeed) {
2675 if (($succeed, $to) = balanced_delim($to) and $succeed) {
2676 return "$from$to";
2677 } else {
2678 for $delim ('/', '"', '#') { # note no "'" -- s''' is special
2679 return "$from$delim$to$delim" if index($to, $delim) == -1;
2680 }
2681 $to =~ s[/][\\/]g;
2682 return "$from/$to/";
2683 }
2684 } else {
2685 for $delim ('/', '"', '#') { # note no '
2686 return "$delim$from$delim$to$delim"
2687 if index($to . $from, $delim) == -1;
2688 }
2689 $from =~ s[/][\\/]g;
2690 $to =~ s[/][\\/]g;
2691 return "/$from/$to/";
2692 }
2693 }
2694  
2695 # Only used by tr///, so backslashes hyphens
2696 sub pchr { # ASCII
2697 my($n) = @_;
2698 if ($n == ord '\\') {
2699 return '\\\\';
2700 } elsif ($n == ord "-") {
2701 return "\\-";
2702 } elsif ($n >= ord(' ') and $n <= ord('~')) {
2703 return chr($n);
2704 } elsif ($n == ord "\a") {
2705 return '\\a';
2706 } elsif ($n == ord "\b") {
2707 return '\\b';
2708 } elsif ($n == ord "\t") {
2709 return '\\t';
2710 } elsif ($n == ord "\n") {
2711 return '\\n';
2712 } elsif ($n == ord "\e") {
2713 return '\\e';
2714 } elsif ($n == ord "\f") {
2715 return '\\f';
2716 } elsif ($n == ord "\r") {
2717 return '\\r';
2718 } elsif ($n >= ord("\cA") and $n <= ord("\cZ")) {
2719 return '\\c' . chr(ord("@") + $n);
2720 } else {
2721 # return '\x' . sprintf("%02x", $n);
2722 return '\\' . sprintf("%03o", $n);
2723 }
2724 }
2725  
2726 sub collapse {
2727 my(@chars) = @_;
2728 my($str, $c, $tr) = ("");
2729 for ($c = 0; $c < @chars; $c++) {
2730 $tr = $chars[$c];
2731 $str .= pchr($tr);
2732 if ($c <= $#chars - 2 and $chars[$c + 1] == $tr + 1 and
2733 $chars[$c + 2] == $tr + 2)
2734 {
2735 for (; $c <= $#chars-1 and $chars[$c + 1] == $chars[$c] + 1; $c++)
2736 {}
2737 $str .= "-";
2738 $str .= pchr($chars[$c]);
2739 }
2740 }
2741 return $str;
2742 }
2743  
2744 sub tr_decode_byte {
2745 my($table, $flags) = @_;
2746 my(@table) = unpack("s*", $table);
2747 splice @table, 0x100, 1; # Number of subsequent elements
2748 my($c, $tr, @from, @to, @delfrom, $delhyphen);
2749 if ($table[ord "-"] != -1 and
2750 $table[ord("-") - 1] == -1 || $table[ord("-") + 1] == -1)
2751 {
2752 $tr = $table[ord "-"];
2753 $table[ord "-"] = -1;
2754 if ($tr >= 0) {
2755 @from = ord("-");
2756 @to = $tr;
2757 } else { # -2 ==> delete
2758 $delhyphen = 1;
2759 }
2760 }
2761 for ($c = 0; $c < @table; $c++) {
2762 $tr = $table[$c];
2763 if ($tr >= 0) {
2764 push @from, $c; push @to, $tr;
2765 } elsif ($tr == -2) {
2766 push @delfrom, $c;
2767 }
2768 }
2769 @from = (@from, @delfrom);
2770 if ($flags & OPpTRANS_COMPLEMENT) {
2771 my @newfrom = ();
2772 my %from;
2773 @from{@from} = (1) x @from;
2774 for ($c = 0; $c < 256; $c++) {
2775 push @newfrom, $c unless $from{$c};
2776 }
2777 @from = @newfrom;
2778 }
2779 unless ($flags & OPpTRANS_DELETE || !@to) {
2780 pop @to while $#to and $to[$#to] == $to[$#to -1];
2781 }
2782 my($from, $to);
2783 $from = collapse(@from);
2784 $to = collapse(@to);
2785 $from .= "-" if $delhyphen;
2786 return ($from, $to);
2787 }
2788  
2789 sub tr_chr {
2790 my $x = shift;
2791 if ($x == ord "-") {
2792 return "\\-";
2793 } elsif ($x == ord "\\") {
2794 return "\\\\";
2795 } else {
2796 return chr $x;
2797 }
2798 }
2799  
2800 # XXX This doesn't yet handle all cases correctly either
2801  
2802 sub tr_decode_utf8 {
2803 my($swash_hv, $flags) = @_;
2804 my %swash = $swash_hv->ARRAY;
2805 my $final = undef;
2806 $final = $swash{'FINAL'}->IV if exists $swash{'FINAL'};
2807 my $none = $swash{"NONE"}->IV;
2808 my $extra = $none + 1;
2809 my(@from, @delfrom, @to);
2810 my $line;
2811 foreach $line (split /\n/, $swash{'LIST'}->PV) {
2812 my($min, $max, $result) = split(/\t/, $line);
2813 $min = hex $min;
2814 if (length $max) {
2815 $max = hex $max;
2816 } else {
2817 $max = $min;
2818 }
2819 $result = hex $result;
2820 if ($result == $extra) {
2821 push @delfrom, [$min, $max];
2822 } else {
2823 push @from, [$min, $max];
2824 push @to, [$result, $result + $max - $min];
2825 }
2826 }
2827 for my $i (0 .. $#from) {
2828 if ($from[$i][0] == ord '-') {
2829 unshift @from, splice(@from, $i, 1);
2830 unshift @to, splice(@to, $i, 1);
2831 last;
2832 } elsif ($from[$i][1] == ord '-') {
2833 $from[$i][1]--;
2834 $to[$i][1]--;
2835 unshift @from, ord '-';
2836 unshift @to, ord '-';
2837 last;
2838 }
2839 }
2840 for my $i (0 .. $#delfrom) {
2841 if ($delfrom[$i][0] == ord '-') {
2842 push @delfrom, splice(@delfrom, $i, 1);
2843 last;
2844 } elsif ($delfrom[$i][1] == ord '-') {
2845 $delfrom[$i][1]--;
2846 push @delfrom, ord '-';
2847 last;
2848 }
2849 }
2850 if (defined $final and $to[$#to][1] != $final) {
2851 push @to, [$final, $final];
2852 }
2853 push @from, @delfrom;
2854 if ($flags & OPpTRANS_COMPLEMENT) {
2855 my @newfrom;
2856 my $next = 0;
2857 for my $i (0 .. $#from) {
2858 push @newfrom, [$next, $from[$i][0] - 1];
2859 $next = $from[$i][1] + 1;
2860 }
2861 @from = ();
2862 for my $range (@newfrom) {
2863 if ($range->[0] <= $range->[1]) {
2864 push @from, $range;
2865 }
2866 }
2867 }
2868 my($from, $to, $diff);
2869 for my $chunk (@from) {
2870 $diff = $chunk->[1] - $chunk->[0];
2871 if ($diff > 1) {
2872 $from .= tr_chr($chunk->[0]) . "-" . tr_chr($chunk->[1]);
2873 } elsif ($diff == 1) {
2874 $from .= tr_chr($chunk->[0]) . tr_chr($chunk->[1]);
2875 } else {
2876 $from .= tr_chr($chunk->[0]);
2877 }
2878 }
2879 for my $chunk (@to) {
2880 $diff = $chunk->[1] - $chunk->[0];
2881 if ($diff > 1) {
2882 $to .= tr_chr($chunk->[0]) . "-" . tr_chr($chunk->[1]);
2883 } elsif ($diff == 1) {
2884 $to .= tr_chr($chunk->[0]) . tr_chr($chunk->[1]);
2885 } else {
2886 $to .= tr_chr($chunk->[0]);
2887 }
2888 }
2889 #$final = sprintf("%04x", $final) if defined $final;
2890 #$none = sprintf("%04x", $none) if defined $none;
2891 #$extra = sprintf("%04x", $extra) if defined $extra;
2892 #print STDERR "final: $final\n none: $none\nextra: $extra\n";
2893 #print STDERR $swash{'LIST'}->PV;
2894 return (B::Deparse::escape_str($from), B::Deparse::escape_str($to));
2895 }
2896  
2897 sub pp_trans {
2898 my $self = shift;
2899 my($op, $cx) = @_;
2900 my($from, $to);
2901 my $class = class($op);
2902 my $priv_flags = $op->private;
2903 if ($class eq "PVOP") {
2904 ($from, $to) = tr_decode_byte($op->pv, $priv_flags);
2905 } elsif ($class eq "PADOP") {
2906 ($from, $to)
2907 = tr_decode_utf8($self->padval($op->padix)->RV, $priv_flags);
2908 } else { # class($op) eq "SVOP"
2909 ($from, $to) = tr_decode_utf8($op->sv->RV, $priv_flags);
2910 }
2911 my $flags = "";
2912 $flags .= "c" if $priv_flags & OPpTRANS_COMPLEMENT;
2913 $flags .= "d" if $priv_flags & OPpTRANS_DELETE;
2914 $to = "" if $from eq $to and $flags eq "";
2915 $flags .= "s" if $priv_flags & OPpTRANS_SQUASH;
2916 return info_from_list($op, $self, ['tr', double_delim($from, $to), $flags],
2917 '', 'pp_trans', {});
2918 }
2919  
2920 sub pp_transr {
2921 my $self = $_[0];
2922 my $op = $_[1];
2923 my $info = pp_trans(@_);
2924 return info_from_text($op, $self, $info->{text} . 'r', 'pp_transr',
2925 {body => [$info]});
2926 }
2927  
2928 sub re_dq_disambiguate {
2929 my ($first, $last) = @_;
2930 # Disambiguate "${foo}bar", "${foo}{bar}", "${foo}[1]"
2931 ($last =~ /^[A-Z\\\^\[\]_?]/ &&
2932 $first =~ s/([\$@])\^$/${1}{^}/) # "${^}W" etc
2933 || ($last =~ /^[{\[\w_]/ &&
2934 $first =~ s/([\$@])([A-Za-z_]\w*)$/${1}{$2}/);
2935 return $first . $last;
2936 }
2937  
2938 # Like dq(), but different
2939 sub re_dq {
2940 my $self = shift;
2941 my ($op, $extended) = @_;
2942  
2943 my $type = $op->name;
2944 my ($re, @texts, $type);
2945 my $opts = {};
2946 if ($type eq "const") {
2947 return info_from_text($op, $self, '$[', 're_dq_const', {})
2948 if $op->private & OPpCONST_ARYBASE;
2949 my $unbacked = B::Deparse::re_unback($self->const_sv($op)->as_string);
2950 return B::Deparse::re_uninterp_extended(escape_extended_re($unbacked))
2951 if $extended;
2952 return B::Deparse::re_uninterp(B::Deparse::escape_str($unbacked));
2953 } elsif ($type eq "concat") {
2954 my $first = $self->re_dq($op->first, $extended);
2955 my $last = $self->re_dq($op->last, $extended);
2956 return re_dq_disambiguate($first, $last);
2957 } elsif ($type eq "uc") {
2958 $re = $self->re_dq($op->first->sibling, $extended);
2959 @texts = ['\U', $re->{text},'\E'];
2960 $type = 're_dq_U';
2961 } elsif ($type eq "lc") {
2962 $re = $self->re_dq($op->first->sibling, $extended);
2963 @texts = ['\L', $re->{text},'\E'];
2964 $type = 're_dq_L';
2965 } elsif ($type eq "ucfirst") {
2966 $re = $self->re_dq($op->first->sibling, $extended);
2967 @texts = ['\u', $re->{text}];
2968 $type = 're_dq_u';
2969 } elsif ($type eq "lcfirst") {
2970 $re = $self->re_dq($op->first->sibling, $extended);
2971 @texts = ['\l', $re->{text}];
2972 $type = 're_dq_l';
2973 } elsif ($type eq "quotemeta") {
2974 $re = $self->re_dq($op->first->sibling, $extended);
2975 @texts = ['\Q', $re->{text},'\E'];
2976 $type = 're_dq_Q';
2977 } elsif ($type eq "fc") {
2978 $re = $self->re_dq($op->first->sibling, $extended);
2979 @texts = ['\F', $re->{text},'\E'];
2980 $type = 're_dq_Q';
2981 } elsif ($type eq "join") {
2982 return $self->deparse($op->last, 26, $op); # was join($", @ary)
2983 } else {
2984 my $info = $self->deparse($op, 26, $op);
2985 $info->{type} = 're_dq';
2986 $info->{text} =~ s/^\$([(|)])\z/\${$1}/; # $( $| $) need braces
2987 return $info;
2988 }
2989 $opts->{body} = [$re];
2990 return info_from_list($op, $self, \@texts, '', $type, $opts);
2991 }
2992  
2993 sub pure_string {
2994 my ($self, $op) = @_;
2995 return 0 if null $op;
2996 my $type = $op->name;
2997  
2998 if ($type eq 'const' || $type eq 'av2arylen') {
2999 return 1;
3000 }
3001 elsif ($type =~ /^(?:[ul]c(first)?|fc)$/ || $type eq 'quotemeta') {
3002 return $self->pure_string($op->first->sibling);
3003 }
3004 elsif ($type eq 'join') {
3005 my $join_op = $op->first->sibling; # Skip pushmark
3006 return 0 unless $join_op->name eq 'null' && $join_op->targ == OP_RV2SV;
3007  
3008 my $gvop = $join_op->first;
3009 return 0 unless $gvop->name eq 'gvsv';
3010 return 0 unless '"' eq $self->gv_name($self->gv_or_padgv($gvop));
3011  
3012 return 0 unless ${$join_op->sibling} eq ${$op->last};
3013 return 0 unless $op->last->name =~ /^(?:[ah]slice|(?:rv2|pad)av)$/;
3014 }
3015 elsif ($type eq 'concat') {
3016 return $self->pure_string($op->first)
3017 && $self->pure_string($op->last);
3018 }
3019 elsif (is_scalar($op) || $type =~ /^[ah]elem$/) {
3020 return 1;
3021 }
3022 elsif ($type eq "null" and $op->can('first') and not null $op->first and
3023 ($op->first->name eq "null" and $op->first->can('first')
3024 and not null $op->first->first and
3025 $op->first->first->name eq "aelemfast"
3026 or
3027 $op->first->name =~ /^aelemfast(?:_lex)?\z/
3028 )) {
3029 return 1;
3030 }
3031 else {
3032 return 0;
3033 }
3034  
3035 return 1;
3036 }
3037  
3038 sub regcomp
3039 {
3040 my($self, $op, $cx, $extended) = @_;
3041 my @other_ops = ();
3042 my $kid = $op->first;
3043 if ($kid->name eq "regcmaybe") {
3044 push @other_ops, $kid;
3045 $kid = $kid->first;
3046 }
3047 if ($kid->name eq "regcreset") {
3048 push @other_ops, $kid;
3049 $kid = $kid->first;
3050 }
3051 if ($kid->name eq "null" and !null($kid->first)
3052 and $kid->first->name eq 'pushmark') {
3053 my $str = '';
3054 push(@other_ops, $kid);
3055 $kid = $kid->first->sibling;
3056 my @body = ();
3057 while (!null($kid)) {
3058 my $first = $str;
3059 my $last = $self->re_dq($kid, $extended);
3060 push @body, $last;
3061 push(@other_ops, $kid);
3062 $str = re_dq_disambiguate($first, $last->{text});
3063 $kid = $kid->sibling;
3064 }
3065 return (info_from_text($op, $self, $str, 'regcomp',
3066 {other_ops => \@other_ops,
3067 body => \@body}), 1);
3068 }
3069  
3070 if ($self->pure_string($kid)) {
3071 my $info = $self->re_dq($kid, $extended);
3072 my @kid_ops = $info->{other_ops} ? @{$info->{other_ops}} : ();
3073 push @other_ops, @kid_ops;
3074 $info->{other_ops} = \@other_ops;
3075 return ($info, 1);
3076 }
3077 return ($self->deparse($kid, $cx, $op), 0, $op);
3078 }
3079  
3080 sub pp_regcomp
3081 {
3082 my ($self, $op, $cx) = @_;
3083 return (($self->regcomp($op, $cx, 0))[0]);
3084 }
3085  
3086 sub re_flags
3087 {
3088 my ($self, $op) = @_;
3089 my $flags = '';
3090 my $pmflags = $op->pmflags;
3091 $flags .= "g" if $pmflags & PMf_GLOBAL;
3092 $flags .= "i" if $pmflags & PMf_FOLD;
3093 $flags .= "m" if $pmflags & PMf_MULTILINE;
3094 $flags .= "o" if $pmflags & PMf_KEEP;
3095 $flags .= "s" if $pmflags & PMf_SINGLELINE;
3096 $flags .= "x" if $pmflags & PMf_EXTENDED;
3097 $flags .= "p" if $pmflags & RXf_PMf_KEEPCOPY;
3098 if (my $charset = $pmflags & RXf_PMf_CHARSET) {
3099 # Hardcoding this is fragile, but B does not yet export the
3100 # constants we need.
3101 $flags .= qw(d l u a aa)[$charset >> 5]
3102 }
3103 # The /d flag is indicated by 0; only show it if necessary.
3104 elsif ($self->{hinthash} and
3105 $self->{hinthash}{reflags_charset}
3106 || $self->{hinthash}{feature_unicode}
3107 or $self->{hints} & $feature::hint_mask
3108 && ($self->{hints} & $feature::hint_mask)
3109 != $feature::hint_mask
3110 && do {
3111 $self->{hints} & $feature::hint_uni8bit;
3112 }
3113 ) {
3114 $flags .= 'd';
3115 }
3116 $flags;
3117 }
3118  
3119 # osmic acid -- see osmium tetroxide
3120  
3121 my %matchwords;
3122 map($matchwords{join "", sort split //, $_} = $_, 'cig', 'cog', 'cos', 'cogs',
3123 'cox', 'go', 'is', 'ism', 'iso', 'mig', 'mix', 'osmic', 'ox', 'sic',
3124 'sig', 'six', 'smog', 'so', 'soc', 'sog', 'xi');
3125  
3126 sub matchop
3127 {
3128 my($self, $op, $cx, $name, $delim) = @_;
3129 my $kid = $op->first;
3130 my $info = {};
3131 my @body = ();
3132 my ($binop, $var, $re_str) = ("", "", "");
3133 my $re;
3134 if ($op->flags & OPf_STACKED) {
3135 $binop = 1;
3136 $var = $self->deparse($kid, 20, $op);
3137 push @body, $var;
3138 $kid = $kid->sibling;
3139 }
3140 my $quote = 1;
3141 my $pmflags = $op->pmflags;
3142 my $extended = ($pmflags & PMf_EXTENDED);
3143 my $rhs_bound_to_defsv;
3144 if (null $kid) {
3145 my $unbacked = B::Deparse::re_unback($op->precomp);
3146 if ($extended) {
3147 $re_str = B::Deparse::re_uninterp_extended(B::Deparse::escape_extended_re($unbacked));
3148 } else {
3149 $re_str = B::Deparse::re_uninterp(B::Deparse::escape_str(B::Deparse::re_unback($op->precomp)));
3150 }
3151 } elsif ($kid->name ne 'regcomp') {
3152 carp("found ".$kid->name." where regcomp expected");
3153 } else {
3154 ($re, $quote) = $self->regcomp($kid, 21, $extended);
3155 push @body, $re;
3156 $re_str = $re->{text};
3157 my $matchop = $kid->first;
3158 if ($matchop->name eq 'regcrest') {
3159 $matchop = $matchop->first;
3160 }
3161 if ($matchop->name =~ /^(?:match|transr?|subst)\z/
3162 && $matchop->flags & OPf_SPECIAL) {
3163 $rhs_bound_to_defsv = 1;
3164 }
3165 }
3166 my $flags = '';
3167 $flags .= "c" if $pmflags & PMf_CONTINUE;
3168 $flags .= $self->re_flags($op);
3169 $flags = join '', sort split //, $flags;
3170 $flags = $matchwords{$flags} if $matchwords{$flags};
3171  
3172 if ($pmflags & PMf_ONCE) { # only one kind of delimiter works here
3173 $re_str =~ s/\?/\\?/g;
3174 $re_str = "?$re_str?";
3175 } elsif ($quote) {
3176 my $re = $self->single_delim($kid, $name, $delim, $re_str);
3177 push @body, $re;
3178 $re_str = $re->{text};
3179 }
3180 my $opts = {body => \@body};
3181 my @texts;
3182 $re_str .= $flags if $quote;
3183 my $type;
3184 if ($binop) {
3185 if ($rhs_bound_to_defsv) {
3186 @texts = ($var->{text}, ' =~ ', "(", '$_', ' =~ ', $re_str, ')');
3187 } else {
3188 @texts = ($var->{text}, ' =~ ', $re_str);
3189 }
3190 $opts->{maybe_parens} = [$self, $cx, 20];
3191 $type = 'matchop_binop';
3192 } else {
3193 @texts = ($re_str);
3194 $type = 'matchop_unnop';
3195 }
3196 return info_from_list($op, $self, \@texts, '', $type, $opts);
3197 }
3198  
3199 sub pp_match { matchop(@_, "m", "/") }
3200 sub pp_pushre { matchop(@_, "m", "/") }
3201 sub pp_qr { matchop(@_, "qr", "") }
3202  
3203 sub pp_runcv { unop(@_, "__SUB__"); }
3204  
3205 sub pp_split
3206 {
3207 my($self, $op, $cx) = @_;
3208 my($kid, @exprs, $ary_info, $expr);
3209 my $ary = '';
3210 my @body = ();
3211 my @other_ops = ();
3212 $kid = $op->first;
3213  
3214 # For our kid (an OP_PUSHRE), pmreplroot is never actually the
3215 # root of a replacement; it's either empty, or abused to point to
3216 # the GV for an array we split into (an optimization to save
3217 # assignment overhead). Depending on whether we're using ithreads,
3218 # this OP* holds either a GV* or a PADOFFSET. Luckily, B.xs
3219 # figures out for us which it is.
3220 my $replroot = $kid->pmreplroot;
3221 my $gv = 0;
3222 if (ref($replroot) eq "B::GV") {
3223 $gv = $replroot;
3224 } elsif (!ref($replroot) and $replroot > 0) {
3225 $gv = $self->padval($replroot);
3226 }
3227 $ary = $self->stash_variable('@', $self->gv_name($gv), $cx) if $gv;
3228  
3229 for (; !null($kid); $kid = $kid->sibling) {
3230 push @exprs, $self->deparse($kid, 6, $op);
3231 }
3232  
3233 my $opts = {body => \@exprs};
3234  
3235 my @args_texts = map $_->{text}, @exprs;
3236 # handle special case of split(), and split(' ') that compiles to /\s+/
3237 # Under 5.10, the reflags may be undef if the split regexp isn't a constant
3238 # Under 5.17.5-5.17.9, the special flag is on split itself.
3239 $kid = $op->first;
3240 if ( $op->flags & OPf_SPECIAL ) {
3241 $exprs[0]->{text} = "' '";
3242 }
3243  
3244 my $sep = '';
3245 my $type;
3246 my @expr_texts;
3247 if ($ary) {
3248 @expr_texts = ("$ary", '=', join(', ', @args_texts));
3249 $sep = ' ';
3250 $type = 'split_array';
3251 $opts->{maybe_parens} = [$self, $cx, 7];
3252 } else {
3253 @expr_texts = ('split', '(', join(', ', @args_texts), ')');
3254 $type = 'split';
3255  
3256 }
3257 return info_from_list($op, $self, \@expr_texts, $sep, $type, $opts);
3258 }
3259  
3260 # Kind of silly, but we prefer, subst regexp flags joined together to
3261 # make words. For example: s/a/b/xo => s/a/b/ox
3262  
3263 # oxime -- any of various compounds obtained chiefly by the action of
3264 # hydroxylamine on aldehydes and ketones and characterized by the
3265 # bivalent grouping C=NOH [Webster's Tenth]
3266  
3267 my %substwords;
3268 map($substwords{join "", sort split //, $_} = $_, 'ego', 'egoism', 'em',
3269 'es', 'ex', 'exes', 'gee', 'go', 'goes', 'ie', 'ism', 'iso', 'me',
3270 'meese', 'meso', 'mig', 'mix', 'os', 'ox', 'oxime', 'see', 'seem',
3271 'seg', 'sex', 'sig', 'six', 'smog', 'sog', 'some', 'xi', 'rogue',
3272 'sir', 'rise', 'smore', 'more', 'seer', 'rome', 'gore', 'grim', 'grime',
3273 'or', 'rose', 'rosie');
3274  
3275 sub pp_subst
3276 {
3277 my($self, $op, $cx) = @_;
3278 my $kid = $op->first;
3279 my($binop, $var, $re, @other_ops) = ("", "", "", ());
3280 my @body = ();
3281 my ($repl, $repl_info);
3282 if ($op->flags & OPf_STACKED) {
3283 $binop = 1;
3284 $var = $self->deparse($kid, 20, $op);
3285 $kid = $kid->sibling;
3286 }
3287 my $flags = "";
3288 my $pmflags = $op->pmflags;
3289 if (null($op->pmreplroot)) {
3290 $repl = $kid;
3291 $kid = $kid->sibling;
3292 } else {
3293 push @other_ops, $op->pmreplroot;
3294 $repl = $op->pmreplroot->first; # skip substcont
3295 }
3296 while ($repl->name eq "entereval") {
3297 push @other_ops, $repl;
3298 $repl = $repl->first;
3299 $flags .= "e";
3300 }
3301 {
3302 local $self->{in_subst_repl} = 1;
3303 if ($pmflags & PMf_EVAL) {
3304 $repl_info = $self->deparse($repl->first, 0, $repl);
3305 } else {
3306 $repl_info = $self->dq($repl);
3307 }
3308 }
3309 my $extended = ($pmflags & PMf_EXTENDED);
3310 if (null $kid) {
3311 my $unbacked = B::Deparse::re_unback($op->precomp);
3312 if ($extended) {
3313 $re = B::Deparse::re_uninterp_extended(escape_extended_re($unbacked));
3314 }
3315 else {
3316 $re = B::Deparse::re_uninterp(B::Deparse::escape_str($unbacked));
3317 }
3318 } else {
3319 my ($re_info, $junk) = $self->regcomp($kid, 1, $extended);
3320 push @body, $re_info;
3321 $re = $re_info->{text};
3322 }
3323 $flags .= "r" if $pmflags & PMf_NONDESTRUCT;
3324 $flags .= "e" if $pmflags & PMf_EVAL;
3325 $flags .= $self->re_flags($op);
3326 $flags = join '', sort split //, $flags;
3327 $flags = $substwords{$flags} if $substwords{$flags};
3328 my $info;
3329 push @body, $repl_info;
3330 my $repl_text = $repl_info->{text};
3331 my $opts = {body => \@body};
3332 $opts->{other_ops} = \@other_ops if @other_ops;
3333 if ($binop) {
3334 my @texts = ($var->{text}, " ", "=~", " ", "s", double_delim($re, $repl_text), $flags);
3335 $opts->{maybe_parens} = [$self, $cx, 20];
3336 return info_from_list($op, $self, \@texts, '', 'subst_binop', $opts);
3337 } else {
3338 return info_from_list($op, $self, ['s', double_delim($re, $repl_text)], '', 'subst',
3339 $opts);
3340 }
3341 Carp::confess("unhandled condition in pp_subst");
3342 }
3343  
3344 sub pp_introcv
3345 {
3346 my($self, $op, $cx) = @_;
3347 # For now, deparsing doesn't worry about the distinction between introcv
3348 # and clonecv, so pretend this op doesn't exist:
3349 return info_from_text($op, $self, '', 'introcv', {});
3350 }
3351  
3352 sub pp_clonecv {
3353 my $self = shift;
3354 my($op, $cx) = @_;
3355 my $sv = $self->padname_sv($op->targ);
3356 my $name = substr $sv->PVX, 1; # skip &/$/@/%, like $self->padany
3357 return info_from_list($op, $self, ['my', 'sub', $name], ' ', 'clonev', {});
3358 }
3359  
3360 sub pp_padcv {
3361 my($self, $op, $cx) = @_;
3362 return info_from_text($op, $self, $self->padany($op), 'padcv', {});
3363 }
3364  
3365 unless (caller) {
3366 eval "use Data::Printer;";
3367  
3368 eval {
3369 our($Fileparse_fstype);
3370 sub fib($) {
3371 my $x = shift;
3372 return 1 if $x <= 1;
3373 return(fib($x-1) + fib($x-2))
3374 }
3375 sub fileparse {
3376 no strict;
3377 # my($fullname,@suffices) = @_;
3378  
3379 my $tail = '';
3380 $tail = $1 . $tail;
3381  
3382 # Ensure taint is propagated from the path to its pieces.
3383 $tail .= $taint;
3384 wantarray ? ($basename .= $taint, $dirpath .= $taint, $tail)
3385 : ($basename .= $taint);
3386 }
3387 sub baz {
3388 no strict;
3389 if ($basename =~ s/$pat//s) {
3390 }
3391 }
3392 };
3393  
3394 my $deparse = __PACKAGE__->new("-l", "-c");
3395 my $info = $deparse->coderef2info(\&fileparse);
3396 # my $info = $deparse->coderef2info(\&baz);
3397 import Data::Printer colored => 0;
3398 Data::Printer::p($info);
3399 print "\n", '=' x 30, "\n";
3400 # print $deparse->indent($deparse->deparse_subname('fib')->{text});
3401 # print "\n", '=' x 30, "\n";
3402 # print "\n", '-' x 30, "\n";
3403 while (my($key, $value) = each %{$deparse->{optree}}) {
3404 my $parent_op_name = 'undef';
3405 if ($value->{parent}) {
3406 my $parent = $deparse->{optree}{$value->{parent}};
3407 $parent_op_name = $parent->{op}->name if $parent->{op};
3408 }
3409 if (eval{$value->{op}->name}) {
3410 printf("0x%x %s/%s of %s |\n%s",
3411 $key, $value->{op}->name, $value->{type},
3412 $parent_op_name, $deparse->indent($value->{text}));
3413 } else {
3414 printf("0x%x %s of %s |\n%s",
3415 $key, $value->{type},
3416 $parent_op_name, $deparse->indent($value->{text}));
3417 }
3418 printf " ## line %s\n", $value->{cop} ? $value->{cop}->line : 'undef';
3419 print '-' x 30, "\n";
3420 }
3421 # use B::Deparse;
3422 # my $deparse_old = B::Deparse->new("-l", "-sC");
3423 # print $deparse_old->coderef2text(\&baz);
3424 }
3425  
3426 1;