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