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