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