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