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