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