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