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

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