Commit | Line | Data |
---|---|---|
6e90668e | 1 | # B::Deparse.pm |
4ca8de37 SM |
2 | # Copyright (c) 1998-2000, 2002, 2003, 2004, 2005, 2006 Stephen McCamant. |
3 | # All rights reserved. | |
6e90668e SM |
4 | # This module is free software; you can redistribute and/or modify |
5 | # it under the same terms as Perl itself. | |
6 | ||
7 | # This is based on the module of the same name by Malcolm Beattie, | |
8 | # but essentially none of his code remains. | |
9 | ||
a798dbf2 | 10 | package B::Deparse; |
ff97752d | 11 | use Carp; |
51a5edaf | 12 | use B qw(class main_root main_start main_cv svref_2object opnumber perlstring |
bd0865ec | 13 | OPf_WANT OPf_WANT_VOID OPf_WANT_SCALAR OPf_WANT_LIST |
2be95ceb | 14 | OPf_KIDS OPf_REF OPf_STACKED OPf_SPECIAL OPf_MOD |
ce4e655d | 15 | OPpLVAL_INTRO OPpOUR_INTRO OPpENTERSUB_AMPER OPpSLICE OPpCONST_BARE |
3ed82cfc | 16 | OPpTRANS_SQUASH OPpTRANS_DELETE OPpTRANS_COMPLEMENT OPpTARGET_MY |
e1dccc0d | 17 | OPpEXISTS_SUB OPpSORT_NUMERIC OPpSORT_INTEGER |
2be95ceb | 18 | OPpSORT_REVERSE |
d989cdac | 19 | SVf_IOK SVf_NOK SVf_ROK SVf_POK SVpad_OUR SVf_FAKE SVs_RMG SVs_SMG |
e95ab0c0 | 20 | CVf_METHOD CVf_LVALUE |
2be95ceb | 21 | PMf_KEEP PMf_GLOBAL PMf_CONTINUE PMf_EVAL PMf_ONCE |
ff0cf12f | 22 | PMf_MULTILINE PMf_SINGLELINE PMf_FOLD PMf_EXTENDED); |
a3011959 | 23 | $VERSION = '1.18'; |
a798dbf2 | 24 | use strict; |
2ae48fff | 25 | use vars qw/$AUTOLOAD/; |
34a48b4b | 26 | use warnings (); |
149758b3 | 27 | require feature; |
a798dbf2 | 28 | |
aa381260 | 29 | BEGIN { |
ff0cf12f | 30 | # List version-specific constants here. |
2be95ceb NC |
31 | # Easiest way to keep this code portable between version looks to |
32 | # be to fake up a dummy constant that will never actually be true. | |
33 | foreach (qw(OPpSORT_INPLACE OPpSORT_DESCEND OPpITER_REVERSED OPpCONST_NOVER | |
ff0cf12f | 34 | OPpPAD_STATE PMf_SKIPWHITE RXf_SKIPWHITE |
b9bc576f | 35 | RXf_PMf_CHARSET RXf_PMf_KEEPCOPY |
24fcb59f | 36 | CVf_LOCKED OPpREVERSE_INPLACE OPpSUBSTR_REPL_FIRST |
7d789282 | 37 | PMf_NONDESTRUCT OPpCONST_ARYBASE OPpEVAL_BYTES)) { |
ff0cf12f | 38 | eval { import B $_ }; |
2be95ceb NC |
39 | no strict 'refs'; |
40 | *{$_} = sub () {0} unless *{$_}{CODE}; | |
41 | } | |
aa381260 NC |
42 | } |
43 | ||
6e90668e SM |
44 | # Changes between 0.50 and 0.51: |
45 | # - fixed nulled leave with live enter in sort { } | |
46 | # - fixed reference constants (\"str") | |
47 | # - handle empty programs gracefully | |
c4a6f826 | 48 | # - handle infinite loops (for (;;) {}, while (1) {}) |
e38ccfd9 | 49 | # - differentiate between 'for my $x ...' and 'my $x; for $x ...' |
6e90668e SM |
50 | # - various minor cleanups |
51 | # - moved globals into an object | |
e38ccfd9 | 52 | # - added '-u', like B::C |
6e90668e SM |
53 | # - package declarations using cop_stash |
54 | # - subs, formats and code sorted by cop_seq | |
f6f9bdb7 | 55 | # Changes between 0.51 and 0.52: |
4d1ff10f | 56 | # - added pp_threadsv (special variables under USE_5005THREADS) |
f6f9bdb7 | 57 | # - added documentation |
bd0865ec | 58 | # Changes between 0.52 and 0.53: |
9d2c6865 | 59 | # - many changes adding precedence contexts and associativity |
e38ccfd9 | 60 | # - added '-p' and '-s' output style options |
9d2c6865 | 61 | # - various other minor fixes |
bd0865ec | 62 | # Changes between 0.53 and 0.54: |
e38ccfd9 | 63 | # - added support for new 'for (1..100)' optimization, |
d7f5b6da | 64 | # thanks to Gisle Aas |
bd0865ec | 65 | # Changes between 0.54 and 0.55: |
90be192f SM |
66 | # - added support for new qr// construct |
67 | # - added support for new pp_regcreset OP | |
bd0865ec | 68 | # Changes between 0.55 and 0.56: |
f5aa8f4e SM |
69 | # - tested on base/*.t, cmd/*.t, comp/*.t, io/*.t |
70 | # - fixed $# on non-lexicals broken in last big rewrite | |
71 | # - added temporary fix for change in opcode of OP_STRINGIFY | |
e38ccfd9 | 72 | # - fixed problem in 0.54's for() patch in 'for (@ary)' |
f5aa8f4e | 73 | # - fixed precedence in conditional of ?: |
e38ccfd9 | 74 | # - tweaked list paren elimination in 'my($x) = @_' |
f5aa8f4e SM |
75 | # - made continue-block detection trickier wrt. null ops |
76 | # - fixed various prototype problems in pp_entersub | |
77 | # - added support for sub prototypes that never get GVs | |
78 | # - added unquoting for special filehandle first arg in truncate | |
e38ccfd9 | 79 | # - print doubled rv2gv (a bug) as '*{*GV}' instead of illegal '**GV' |
f5aa8f4e | 80 | # - added semicolons at the ends of blocks |
e38ccfd9 | 81 | # - added -l '#line' declaration option -- fixes cmd/subval.t 27,28 |
bd0865ec GS |
82 | # Changes between 0.56 and 0.561: |
83 | # - fixed multiply-declared my var in pp_truncate (thanks to Sarathy) | |
84 | # - used new B.pm symbolic constants (done by Nick Ing-Simmons) | |
85 | # Changes between 0.561 and 0.57: | |
86 | # - stylistic changes to symbolic constant stuff | |
87 | # - handled scope in s///e replacement code | |
88 | # - added unquote option for expanding "" into concats, etc. | |
89 | # - split method and proto parts of pp_entersub into separate functions | |
90 | # - various minor cleanups | |
f4a44678 SM |
91 | # Changes after 0.57: |
92 | # - added parens in \&foo (patch by Albert Dvornik) | |
93 | # Changes between 0.57 and 0.58: | |
e38ccfd9 | 94 | # - fixed '0' statements that weren't being printed |
f4a44678 SM |
95 | # - added methods for use from other programs |
96 | # (based on patches from James Duncan and Hugo van der Sanden) | |
97 | # - added -si and -sT to control indenting (also based on a patch from Hugo) | |
98 | # - added -sv to print something else instead of '???' | |
99 | # - preliminary version of utf8 tr/// handling | |
3ed82cfc GS |
100 | # Changes after 0.58: |
101 | # - uses of $op->ppaddr changed to new $op->name (done by Sarathy) | |
d989cdac | 102 | # - added support for Hugo's new OP_SETSTATE (like nextstate) |
3ed82cfc GS |
103 | # Changes between 0.58 and 0.59 |
104 | # - added support for Chip's OP_METHOD_NAMED | |
105 | # - added support for Ilya's OPpTARGET_MY optimization | |
e38ccfd9 | 106 | # - elided arrows before '()' subscripts when possible |
58cccf98 | 107 | # Changes between 0.59 and 0.60 |
c4a6f826 | 108 | # - support for method attributes was added |
58cccf98 SM |
109 | # - some warnings fixed |
110 | # - separate recognition of constant subs | |
c4a6f826 | 111 | # - rewrote continue block handling, now recognizing for loops |
58cccf98 | 112 | # - added more control of expanding control structures |
c7f67cde RH |
113 | # Changes between 0.60 and 0.61 (mostly by Robin Houston) |
114 | # - many bug-fixes | |
115 | # - support for pragmas and 'use' | |
116 | # - support for the little-used $[ variable | |
117 | # - support for __DATA__ sections | |
118 | # - UTF8 support | |
119 | # - BEGIN, CHECK, INIT and END blocks | |
120 | # - scoping of subroutine declarations fixed | |
121 | # - compile-time output from the input program can be suppressed, so that the | |
122 | # output is just the deparsed code. (a change to O.pm in fact) | |
123 | # - our() declarations | |
124 | # - *all* the known bugs are now listed in the BUGS section | |
125 | # - comprehensive test mechanism (TEST -deparse) | |
9a58b761 RGS |
126 | # Changes between 0.62 and 0.63 (mostly by Rafael Garcia-Suarez) |
127 | # - bug-fixes | |
128 | # - new switch -P | |
129 | # - support for command-line switches (-l, -0, etc.) | |
d989cdac SM |
130 | # Changes between 0.63 and 0.64 |
131 | # - support for //, CHECK blocks, and assertions | |
132 | # - improved handling of foreach loops and lexicals | |
133 | # - option to use Data::Dumper for constants | |
134 | # - more bug fixes | |
135 | # - discovered lots more bugs not yet fixed | |
0d863452 RH |
136 | # |
137 | # ... | |
138 | # | |
139 | # Changes between 0.72 and 0.73 | |
140 | # - support new switch constructs | |
6e90668e SM |
141 | |
142 | # Todo: | |
2a9e2f8a RH |
143 | # (See also BUGS section at the end of this file) |
144 | # | |
f4a44678 SM |
145 | # - finish tr/// changes |
146 | # - add option for even more parens (generalize \&foo change) | |
90be192f | 147 | # - left/right context |
58cccf98 | 148 | # - copy comments (look at real text with $^P?) |
f5aa8f4e | 149 | # - avoid semis in one-statement blocks |
9d2c6865 | 150 | # - associativity of &&=, ||=, ?: |
6e90668e SM |
151 | # - ',' => '=>' (auto-unquote?) |
152 | # - break long lines ("\r" as discretionary break?) | |
f4a44678 SM |
153 | # - configurable syntax highlighting: ANSI color, HTML, TeX, etc. |
154 | # - more style options: brace style, hex vs. octal, quotes, ... | |
155 | # - print big ints as hex/octal instead of decimal (heuristic?) | |
e38ccfd9 | 156 | # - handle 'my $x if 0'? |
6e90668e SM |
157 | # - version using op_next instead of op_first/sibling? |
158 | # - avoid string copies (pass arrays, one big join?) | |
9d2c6865 | 159 | # - here-docs? |
6e90668e | 160 | |
d989cdac | 161 | # Current test.deparse failures |
d989cdac SM |
162 | # comp/hints 6 - location of BEGIN blocks wrt. block openings |
163 | # run/switchI 1 - missing -I switches entirely | |
164 | # perl -Ifoo -e 'print @INC' | |
165 | # op/caller 2 - warning mask propagates backwards before warnings::register | |
166 | # 'use warnings; BEGIN {${^WARNING_BITS} eq "U"x12;} use warnings::register' | |
167 | # op/getpid 2 - can't assign to shared my() declaration (threads only) | |
168 | # 'my $x : shared = 5' | |
c4a6f826 | 169 | # op/override 7 - parens on overridden require change v-string interpretation |
d989cdac SM |
170 | # 'BEGIN{*CORE::GLOBAL::require=sub {}} require v5.6' |
171 | # c.f. 'BEGIN { *f = sub {0} }; f 2' | |
172 | # op/pat 774 - losing Unicode-ness of Latin1-only strings | |
173 | # 'use charnames ":short"; $x="\N{latin:a with acute}"' | |
174 | # op/recurse 12 - missing parens on recursive call makes it look like method | |
175 | # 'sub f { f($x) }' | |
176 | # op/subst 90 - inconsistent handling of utf8 under "use utf8" | |
177 | # op/taint 29 - "use re 'taint'" deparsed in the wrong place wrt. block open | |
178 | # op/tiehandle compile - "use strict" deparsed in the wrong place | |
179 | # uni/tr_ several | |
180 | # ext/B/t/xref 11 - line numbers when we add newlines to one-line subs | |
181 | # ext/Data/Dumper/t/dumper compile | |
182 | # ext/DB_file/several | |
183 | # ext/Encode/several | |
184 | # ext/Ernno/Errno warnings | |
185 | # ext/IO/lib/IO/t/io_sel 23 | |
186 | # ext/PerlIO/t/encoding compile | |
187 | # ext/POSIX/t/posix 6 | |
188 | # ext/Socket/Socket 8 | |
189 | # ext/Storable/t/croak compile | |
190 | # lib/Attribute/Handlers/t/multi compile | |
191 | # lib/bignum/ several | |
192 | # lib/charnames 35 | |
193 | # lib/constant 32 | |
194 | # lib/English 40 | |
195 | # lib/ExtUtils/t/bytes 4 | |
196 | # lib/File/DosGlob compile | |
197 | # lib/Filter/Simple/t/data 1 | |
198 | # lib/Math/BigInt/t/constant 1 | |
199 | # lib/Net/t/config Deparse-warning | |
200 | # lib/overload compile | |
201 | # lib/Switch/ several | |
202 | # lib/Symbol 4 | |
203 | # lib/Test/Simple several | |
204 | # lib/Term/Complete | |
205 | # lib/Tie/File/t/29_downcopy 5 | |
206 | # lib/vars 22 | |
f5aa8f4e | 207 | |
6e90668e SM |
208 | # Object fields (were globals): |
209 | # | |
210 | # avoid_local: | |
211 | # (local($a), local($b)) and local($a, $b) have the same internal | |
212 | # representation but the short form looks better. We notice we can | |
213 | # use a large-scale local when checking the list, but need to prevent | |
d989cdac | 214 | # individual locals too. This hash holds the addresses of OPs that |
6e90668e SM |
215 | # have already had their local-ness accounted for. The same thing |
216 | # is done with my(). | |
217 | # | |
218 | # curcv: | |
219 | # CV for current sub (or main program) being deparsed | |
220 | # | |
8510e997 | 221 | # curcvlex: |
415d4c68 FC |
222 | # Cached hash of lexical variables for curcv: keys are |
223 | # names prefixed with "m" or "o" (representing my/our), and | |
8510e997 RH |
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 |
e38ccfd9 | 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 | |
e38ccfd9 | 269 | # associativity ('($x + $y) + $z' => '$x + $y + $y') and related |
9d2c6865 SM |
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 | 303 | # 0 statement level |
93a8ff62 | 304 | # -1 format body |
9d2c6865 SM |
305 | |
306 | # Nonprinting characters with special meaning: | |
307 | # \cS - steal parens (see maybe_parens_unop) | |
308 | # \n - newline and indent | |
309 | # \t - increase indent | |
e38ccfd9 | 310 | # \b - decrease indent ('outdent') |
f5aa8f4e | 311 | # \f - flush left (no indent) |
9d2c6865 | 312 | # \cK - kill following semicolon, if any |
6e90668e SM |
313 | |
314 | sub null { | |
315 | my $op = shift; | |
316 | return class($op) eq "NULL"; | |
317 | } | |
318 | ||
319 | sub todo { | |
320 | my $self = shift; | |
34a48b4b | 321 | my($cv, $is_form) = @_; |
e31885a0 | 322 | return unless ($cv->FILE eq $0 || exists $self->{files}{$cv->FILE}); |
6e90668e | 323 | my $seq; |
d989cdac SM |
324 | if ($cv->OUTSIDE_SEQ) { |
325 | $seq = $cv->OUTSIDE_SEQ; | |
326 | } elsif (!null($cv->START) and is_state($cv->START)) { | |
6e90668e SM |
327 | $seq = $cv->START->cop_seq; |
328 | } else { | |
329 | $seq = 0; | |
330 | } | |
34a48b4b | 331 | push @{$self->{'subs_todo'}}, [$seq, $cv, $is_form]; |
1d38190f RGS |
332 | unless ($is_form || class($cv->STASH) eq 'SPECIAL') { |
333 | $self->{'subs_deparsed'}{$cv->STASH->NAME."::".$cv->GV->NAME} = 1; | |
334 | } | |
6e90668e SM |
335 | } |
336 | ||
337 | sub next_todo { | |
338 | my $self = shift; | |
339 | my $ent = shift @{$self->{'subs_todo'}}; | |
34a48b4b RH |
340 | my $cv = $ent->[1]; |
341 | my $gv = $cv->GV; | |
342 | my $name = $self->gv_name($gv); | |
6e90668e SM |
343 | if ($ent->[2]) { |
344 | return "format $name =\n" | |
e31885a0 | 345 | . $self->deparse_format($ent->[1]). "\n"; |
6e90668e | 346 | } else { |
0ca62a8e | 347 | $self->{'subs_declared'}{$name} = 1; |
34a48b4b RH |
348 | if ($name eq "BEGIN") { |
349 | my $use_dec = $self->begin_is_use($cv); | |
d989cdac | 350 | if (defined ($use_dec) and $self->{'expand'} < 5) { |
0e7fe0f0 RH |
351 | return () if 0 == length($use_dec); |
352 | return $use_dec; | |
353 | } | |
34a48b4b | 354 | } |
a0035eb8 RH |
355 | my $l = ''; |
356 | if ($self->{'linenums'}) { | |
357 | my $line = $gv->LINE; | |
358 | my $file = $gv->FILE; | |
359 | $l = "\n\f#line $line \"$file\"\n"; | |
360 | } | |
127212b2 DM |
361 | my $p = ''; |
362 | if (class($cv->STASH) ne "SPECIAL") { | |
363 | my $stash = $cv->STASH->NAME; | |
364 | if ($stash ne $self->{'curstash'}) { | |
365 | $p = "package $stash;\n"; | |
366 | $name = "$self->{'curstash'}::$name" unless $name =~ /::/; | |
367 | $self->{'curstash'} = $stash; | |
368 | } | |
8b2d6640 | 369 | $name =~ s/^\Q$stash\E::(?!\z|.*::)//; |
127212b2 DM |
370 | } |
371 | return "${p}${l}sub $name " . $self->deparse_sub($cv); | |
34a48b4b RH |
372 | } |
373 | } | |
374 | ||
375 | # Return a "use" declaration for this BEGIN block, if appropriate | |
376 | sub begin_is_use { | |
377 | my ($self, $cv) = @_; | |
378 | my $root = $cv->ROOT; | |
80dc0729 | 379 | local @$self{qw'curcv curcvlex'} = ($cv); |
34a48b4b RH |
380 | #require B::Debug; |
381 | #B::walkoptree($cv->ROOT, "debug"); | |
382 | my $lineseq = $root->first; | |
383 | return if $lineseq->name ne "lineseq"; | |
384 | ||
385 | my $req_op = $lineseq->first->sibling; | |
386 | return if $req_op->name ne "require"; | |
387 | ||
388 | my $module; | |
389 | if ($req_op->first->private & OPpCONST_BARE) { | |
390 | # Actually it should always be a bareword | |
391 | $module = $self->const_sv($req_op->first)->PV; | |
392 | $module =~ s[/][::]g; | |
393 | $module =~ s/.pm$//; | |
394 | } | |
395 | else { | |
d989cdac | 396 | $module = $self->const($self->const_sv($req_op->first), 6); |
34a48b4b RH |
397 | } |
398 | ||
399 | my $version; | |
400 | my $version_op = $req_op->sibling; | |
401 | return if class($version_op) eq "NULL"; | |
402 | if ($version_op->name eq "lineseq") { | |
403 | # We have a version parameter; skip nextstate & pushmark | |
404 | my $constop = $version_op->first->next->next; | |
405 | ||
406 | return unless $self->const_sv($constop)->PV eq $module; | |
407 | $constop = $constop->sibling; | |
a58644de | 408 | $version = $self->const_sv($constop); |
d989cdac SM |
409 | if (class($version) eq "IV") { |
410 | $version = $version->int_value; | |
411 | } elsif (class($version) eq "NV") { | |
412 | $version = $version->NV; | |
413 | } elsif (class($version) ne "PVMG") { | |
414 | # Includes PVIV and PVNV | |
a58644de RGS |
415 | $version = $version->PV; |
416 | } else { | |
417 | # version specified as a v-string | |
418 | $version = 'v'.join '.', map ord, split //, $version->PV; | |
419 | } | |
34a48b4b RH |
420 | $constop = $constop->sibling; |
421 | return if $constop->name ne "method_named"; | |
422 | return if $self->const_sv($constop)->PV ne "VERSION"; | |
423 | } | |
424 | ||
425 | $lineseq = $version_op->sibling; | |
426 | return if $lineseq->name ne "lineseq"; | |
427 | my $entersub = $lineseq->first->sibling; | |
428 | if ($entersub->name eq "stub") { | |
429 | return "use $module $version ();\n" if defined $version; | |
430 | return "use $module ();\n"; | |
431 | } | |
432 | return if $entersub->name ne "entersub"; | |
433 | ||
434 | # See if there are import arguments | |
435 | my $args = ''; | |
436 | ||
6ec152c3 RH |
437 | my $svop = $entersub->first->sibling; # Skip over pushmark |
438 | return unless $self->const_sv($svop)->PV eq $module; | |
34a48b4b RH |
439 | |
440 | # Pull out the arguments | |
6ec152c3 RH |
441 | for ($svop=$svop->sibling; $svop->name ne "method_named"; |
442 | $svop = $svop->sibling) { | |
34a48b4b | 443 | $args .= ", " if length($args); |
6ec152c3 | 444 | $args .= $self->deparse($svop, 6); |
34a48b4b RH |
445 | } |
446 | ||
447 | my $use = 'use'; | |
6ec152c3 | 448 | my $method_named = $svop; |
34a48b4b RH |
449 | return if $method_named->name ne "method_named"; |
450 | my $method_name = $self->const_sv($method_named)->PV; | |
451 | ||
452 | if ($method_name eq "unimport") { | |
453 | $use = 'no'; | |
454 | } | |
455 | ||
456 | # Certain pragmas are dealt with using hint bits, | |
457 | # so we ignore them here | |
458 | if ($module eq 'strict' || $module eq 'integer' | |
0ced6c29 RGS |
459 | || $module eq 'bytes' || $module eq 'warnings' |
460 | || $module eq 'feature') { | |
34a48b4b RH |
461 | return ""; |
462 | } | |
463 | ||
464 | if (defined $version && length $args) { | |
465 | return "$use $module $version ($args);\n"; | |
466 | } elsif (defined $version) { | |
467 | return "$use $module $version;\n"; | |
468 | } elsif (length $args) { | |
469 | return "$use $module ($args);\n"; | |
470 | } else { | |
471 | return "$use $module;\n"; | |
6e90668e SM |
472 | } |
473 | } | |
474 | ||
6e90668e | 475 | sub stash_subs { |
894e98ac | 476 | my ($self, $pack, $seen) = @_; |
34a48b4b RH |
477 | my (@ret, $stash); |
478 | if (!defined $pack) { | |
479 | $pack = ''; | |
480 | $stash = \%::; | |
f5aa8f4e | 481 | } |
34a48b4b RH |
482 | else { |
483 | $pack =~ s/(::)?$/::/; | |
484 | no strict 'refs'; | |
d1dc589d | 485 | $stash = \%{"main::$pack"}; |
34a48b4b | 486 | } |
894e98ac FC |
487 | return |
488 | if ($seen ||= {})->{ | |
489 | $INC{"overload.pm"} ? overload::StrVal($stash) : $stash | |
490 | }++; | |
34a48b4b RH |
491 | my %stash = svref_2object($stash)->ARRAY; |
492 | while (my ($key, $val) = each %stash) { | |
f5aa8f4e SM |
493 | my $class = class($val); |
494 | if ($class eq "PV") { | |
a0035eb8 RH |
495 | # Just a prototype. As an ugly but fairly effective way |
496 | # to find out if it belongs here is to see if the AUTOLOAD | |
497 | # (if any) for the stash was defined in one of our files. | |
498 | my $A = $stash{"AUTOLOAD"}; | |
499 | if (defined ($A) && class($A) eq "GV" && defined($A->CV) | |
500 | && class($A->CV) eq "CV") { | |
501 | my $AF = $A->FILE; | |
502 | next unless $AF eq $0 || exists $self->{'files'}{$AF}; | |
503 | } | |
f5aa8f4e | 504 | push @{$self->{'protos_todo'}}, [$pack . $key, $val->PV]; |
5b4ee549 | 505 | } elsif ($class eq "IV" && !($val->FLAGS & SVf_ROK)) { |
a0035eb8 | 506 | # Just a name. As above. |
5b4ee549 NC |
507 | # But skip proxy constant subroutines, as some form of perl-space |
508 | # visible code must have created them, be it a use statement, or | |
509 | # some direct symbol-table manipulation code that we will Deparse | |
a0035eb8 RH |
510 | my $A = $stash{"AUTOLOAD"}; |
511 | if (defined ($A) && class($A) eq "GV" && defined($A->CV) | |
512 | && class($A->CV) eq "CV") { | |
513 | my $AF = $A->FILE; | |
514 | next unless $AF eq $0 || exists $self->{'files'}{$AF}; | |
515 | } | |
d989cdac | 516 | push @{$self->{'protos_todo'}}, [$pack . $key, undef]; |
f5aa8f4e | 517 | } elsif ($class eq "GV") { |
34a48b4b | 518 | if (class(my $cv = $val->CV) ne "SPECIAL") { |
f5aa8f4e | 519 | next if $self->{'subs_done'}{$$val}++; |
e31885a0 | 520 | next if $$val != ${$cv->GV}; # Ignore imposters |
8510e997 | 521 | $self->todo($cv, 0); |
f5aa8f4e | 522 | } |
e31885a0 | 523 | if (class(my $cv = $val->FORM) ne "SPECIAL") { |
f5aa8f4e | 524 | next if $self->{'forms_done'}{$$val}++; |
e31885a0 RH |
525 | next if $$val != ${$cv->GV}; # Ignore imposters |
526 | $self->todo($cv, 1); | |
f5aa8f4e | 527 | } |
34a48b4b | 528 | if (class($val->HV) ne "SPECIAL" && $key =~ /::$/) { |
74cd21ba | 529 | $self->stash_subs($pack . $key, $seen); |
34a48b4b | 530 | } |
6e90668e SM |
531 | } |
532 | } | |
533 | } | |
a798dbf2 | 534 | |
f5aa8f4e SM |
535 | sub print_protos { |
536 | my $self = shift; | |
537 | my $ar; | |
538 | my @ret; | |
539 | foreach $ar (@{$self->{'protos_todo'}}) { | |
540 | my $proto = (defined $ar->[1] ? " (". $ar->[1] . ")" : ""); | |
541 | push @ret, "sub " . $ar->[0] . "$proto;\n"; | |
542 | } | |
543 | delete $self->{'protos_todo'}; | |
544 | return @ret; | |
545 | } | |
546 | ||
9d2c6865 SM |
547 | sub style_opts { |
548 | my $self = shift; | |
549 | my $opts = shift; | |
550 | my $opt; | |
551 | while (length($opt = substr($opts, 0, 1))) { | |
552 | if ($opt eq "C") { | |
553 | $self->{'cuddle'} = " "; | |
f4a44678 SM |
554 | $opts = substr($opts, 1); |
555 | } elsif ($opt eq "i") { | |
556 | $opts =~ s/^i(\d+)//; | |
557 | $self->{'indent_size'} = $1; | |
558 | } elsif ($opt eq "T") { | |
559 | $self->{'use_tabs'} = 1; | |
560 | $opts = substr($opts, 1); | |
561 | } elsif ($opt eq "v") { | |
562 | $opts =~ s/^v([^.]*)(.|$)//; | |
563 | $self->{'ex_const'} = $1; | |
9d2c6865 | 564 | } |
9d2c6865 SM |
565 | } |
566 | } | |
567 | ||
f4a44678 SM |
568 | sub new { |
569 | my $class = shift; | |
570 | my $self = bless {}, $class; | |
f4a44678 | 571 | $self->{'cuddle'} = "\n"; |
d989cdac SM |
572 | $self->{'curcop'} = undef; |
573 | $self->{'curstash'} = "main"; | |
574 | $self->{'ex_const'} = "'???'"; | |
793e2a70 | 575 | $self->{'expand'} = 0; |
d989cdac SM |
576 | $self->{'files'} = {}; |
577 | $self->{'indent_size'} = 4; | |
793e2a70 RH |
578 | $self->{'linenums'} = 0; |
579 | $self->{'parens'} = 0; | |
d989cdac SM |
580 | $self->{'subs_todo'} = []; |
581 | $self->{'unquote'} = 0; | |
582 | $self->{'use_dumper'} = 0; | |
583 | $self->{'use_tabs'} = 0; | |
08c6f5ec | 584 | |
bc6b2ef6 | 585 | $self->{'ambient_arybase'} = 0; |
e31885a0 | 586 | $self->{'ambient_warnings'} = undef; # Assume no lexical warnings |
a0405c92 | 587 | $self->{'ambient_hints'} = 0; |
0ced6c29 | 588 | $self->{'ambient_hinthash'} = undef; |
08c6f5ec RH |
589 | $self->init(); |
590 | ||
f4a44678 | 591 | while (my $arg = shift @_) { |
d989cdac SM |
592 | if ($arg eq "-d") { |
593 | $self->{'use_dumper'} = 1; | |
594 | require Data::Dumper; | |
595 | } elsif ($arg =~ /^-f(.*)/) { | |
34a48b4b | 596 | $self->{'files'}{$1} = 1; |
d989cdac SM |
597 | } elsif ($arg eq "-l") { |
598 | $self->{'linenums'} = 1; | |
f4a44678 SM |
599 | } elsif ($arg eq "-p") { |
600 | $self->{'parens'} = 1; | |
acaaef34 RGS |
601 | } elsif ($arg eq "-P") { |
602 | $self->{'noproto'} = 1; | |
f4a44678 SM |
603 | } elsif ($arg eq "-q") { |
604 | $self->{'unquote'} = 1; | |
605 | } elsif (substr($arg, 0, 2) eq "-s") { | |
606 | $self->style_opts(substr $arg, 2); | |
58cccf98 SM |
607 | } elsif ($arg =~ /^-x(\d)$/) { |
608 | $self->{'expand'} = $1; | |
f4a44678 SM |
609 | } |
610 | } | |
611 | return $self; | |
612 | } | |
613 | ||
810aef70 RH |
614 | { |
615 | # Mask out the bits that L<warnings::register> uses | |
616 | my $WARN_MASK; | |
617 | BEGIN { | |
618 | $WARN_MASK = $warnings::Bits{all} | $warnings::DeadBits{all}; | |
619 | } | |
620 | sub WARN_MASK () { | |
621 | return $WARN_MASK; | |
622 | } | |
34a48b4b RH |
623 | } |
624 | ||
08c6f5ec RH |
625 | # Initialise the contextual information, either from |
626 | # defaults provided with the ambient_pragmas method, | |
627 | # or from perl's own defaults otherwise. | |
628 | sub init { | |
629 | my $self = shift; | |
630 | ||
bc6b2ef6 | 631 | $self->{'arybase'} = $self->{'ambient_arybase'}; |
e31885a0 RH |
632 | $self->{'warnings'} = defined ($self->{'ambient_warnings'}) |
633 | ? $self->{'ambient_warnings'} & WARN_MASK | |
634 | : undef; | |
d5ec2987 | 635 | $self->{'hints'} = $self->{'ambient_hints'}; |
e412117e | 636 | $self->{'hints'} &= 0xFF if $] < 5.009; |
0ced6c29 | 637 | $self->{'hinthash'} = $self->{'ambient_hinthash'}; |
217aba5d RH |
638 | |
639 | # also a convenient place to clear out subs_declared | |
640 | delete $self->{'subs_declared'}; | |
08c6f5ec RH |
641 | } |
642 | ||
a798dbf2 | 643 | sub compile { |
6e90668e | 644 | my(@args) = @_; |
d989cdac | 645 | return sub { |
f4a44678 | 646 | my $self = B::Deparse->new(@args); |
d2bc402e RGS |
647 | # First deparse command-line args |
648 | if (defined $^I) { # deparse -i | |
51a5edaf | 649 | print q(BEGIN { $^I = ).perlstring($^I).qq(; }\n); |
d2bc402e RGS |
650 | } |
651 | if ($^W) { # deparse -w | |
652 | print qq(BEGIN { \$^W = $^W; }\n); | |
653 | } | |
654 | if ($/ ne "\n" or defined $O::savebackslash) { # deparse -l and -0 | |
51a5edaf RGS |
655 | my $fs = perlstring($/) || 'undef'; |
656 | my $bs = perlstring($O::savebackslash) || 'undef'; | |
d2bc402e RGS |
657 | print qq(BEGIN { \$/ = $fs; \$\\ = $bs; }\n); |
658 | } | |
34a48b4b | 659 | my @BEGINs = B::begin_av->isa("B::AV") ? B::begin_av->ARRAY : (); |
676456c2 AG |
660 | my @UNITCHECKs = B::unitcheck_av->isa("B::AV") |
661 | ? B::unitcheck_av->ARRAY | |
662 | : (); | |
ece599bd | 663 | my @CHECKs = B::check_av->isa("B::AV") ? B::check_av->ARRAY : (); |
34a48b4b RH |
664 | my @INITs = B::init_av->isa("B::AV") ? B::init_av->ARRAY : (); |
665 | my @ENDs = B::end_av->isa("B::AV") ? B::end_av->ARRAY : (); | |
676456c2 | 666 | for my $block (@BEGINs, @UNITCHECKs, @CHECKs, @INITs, @ENDs) { |
e31885a0 | 667 | $self->todo($block, 0); |
34a48b4b RH |
668 | } |
669 | $self->stash_subs(); | |
d989cdac SM |
670 | local($SIG{"__DIE__"}) = |
671 | sub { | |
672 | if ($self->{'curcop'}) { | |
673 | my $cop = $self->{'curcop'}; | |
674 | my($line, $file) = ($cop->line, $cop->file); | |
675 | print STDERR "While deparsing $file near line $line,\n"; | |
676 | } | |
677 | }; | |
6e90668e | 678 | $self->{'curcv'} = main_cv; |
8510e997 | 679 | $self->{'curcvlex'} = undef; |
f5aa8f4e | 680 | print $self->print_protos; |
6e90668e | 681 | @{$self->{'subs_todo'}} = |
f4a44678 | 682 | sort {$a->[0] <=> $b->[0]} @{$self->{'subs_todo'}}; |
d989cdac | 683 | print $self->indent($self->deparse_root(main_root)), "\n" |
f4a44678 | 684 | unless null main_root; |
6e90668e SM |
685 | my @text; |
686 | while (scalar(@{$self->{'subs_todo'}})) { | |
687 | push @text, $self->next_todo; | |
688 | } | |
6f611a1a | 689 | print $self->indent(join("", @text)), "\n" if @text; |
e31885a0 RH |
690 | |
691 | # Print __DATA__ section, if necessary | |
692 | no strict 'refs'; | |
96c57f7e RGS |
693 | my $laststash = defined $self->{'curcop'} |
694 | ? $self->{'curcop'}->stash->NAME : $self->{'curstash'}; | |
695 | if (defined *{$laststash."::DATA"}{IO}) { | |
127212b2 DM |
696 | print "package $laststash;\n" |
697 | unless $laststash eq $self->{'curstash'}; | |
e31885a0 | 698 | print "__DATA__\n"; |
96c57f7e | 699 | print readline(*{$laststash."::DATA"}); |
e31885a0 | 700 | } |
a798dbf2 | 701 | } |
a798dbf2 MB |
702 | } |
703 | ||
f4a44678 SM |
704 | sub coderef2text { |
705 | my $self = shift; | |
706 | my $sub = shift; | |
0853f172 | 707 | croak "Usage: ->coderef2text(CODEREF)" unless UNIVERSAL::isa($sub, "CODE"); |
08c6f5ec RH |
708 | |
709 | $self->init(); | |
f4a44678 SM |
710 | return $self->indent($self->deparse_sub(svref_2object($sub))); |
711 | } | |
712 | ||
415d4c68 | 713 | my %strict_bits = do { |
d1718a7c | 714 | local $^H; |
415d4c68 FC |
715 | map +($_ => strict::bits($_)), qw/refs subs vars/ |
716 | }; | |
717 | ||
08c6f5ec RH |
718 | sub ambient_pragmas { |
719 | my $self = shift; | |
bc6b2ef6 | 720 | my ($arybase, $hint_bits, $warning_bits, $hinthash) = (0, 0); |
08c6f5ec RH |
721 | |
722 | while (@_ > 1) { | |
723 | my $name = shift(); | |
724 | my $val = shift(); | |
725 | ||
726 | if ($name eq 'strict') { | |
727 | require strict; | |
728 | ||
729 | if ($val eq 'none') { | |
415d4c68 | 730 | $hint_bits &= $strict_bits{$_} for qw/refs subs vars/; |
08c6f5ec RH |
731 | next(); |
732 | } | |
733 | ||
734 | my @names; | |
735 | if ($val eq "all") { | |
736 | @names = qw/refs subs vars/; | |
737 | } | |
738 | elsif (ref $val) { | |
739 | @names = @$val; | |
740 | } | |
741 | else { | |
a0405c92 | 742 | @names = split' ', $val; |
08c6f5ec | 743 | } |
415d4c68 | 744 | $hint_bits |= $strict_bits{$_} for @names; |
08c6f5ec RH |
745 | } |
746 | ||
bc6b2ef6 Z |
747 | elsif ($name eq '$[') { |
748 | if (OPpCONST_ARYBASE) { | |
749 | $arybase = $val; | |
750 | } else { | |
751 | croak "\$[ can't be non-zero on this perl" unless $val == 0; | |
752 | } | |
753 | } | |
754 | ||
a0405c92 RH |
755 | elsif ($name eq 'integer' |
756 | || $name eq 'bytes' | |
757 | || $name eq 'utf8') { | |
758 | require "$name.pm"; | |
08c6f5ec | 759 | if ($val) { |
a0405c92 RH |
760 | $hint_bits |= ${$::{"${name}::"}{"hint_bits"}}; |
761 | } | |
762 | else { | |
763 | $hint_bits &= ~${$::{"${name}::"}{"hint_bits"}}; | |
764 | } | |
765 | } | |
766 | ||
767 | elsif ($name eq 're') { | |
768 | require re; | |
769 | if ($val eq 'none') { | |
2570cdf1 | 770 | $hint_bits &= ~re::bits(qw/taint eval/); |
a0405c92 RH |
771 | next(); |
772 | } | |
773 | ||
774 | my @names; | |
775 | if ($val eq 'all') { | |
2570cdf1 | 776 | @names = qw/taint eval/; |
a0405c92 RH |
777 | } |
778 | elsif (ref $val) { | |
779 | @names = @$val; | |
08c6f5ec RH |
780 | } |
781 | else { | |
a0405c92 | 782 | @names = split' ',$val; |
08c6f5ec | 783 | } |
a0405c92 | 784 | $hint_bits |= re::bits(@names); |
08c6f5ec RH |
785 | } |
786 | ||
787 | elsif ($name eq 'warnings') { | |
08c6f5ec | 788 | if ($val eq 'none') { |
810aef70 | 789 | $warning_bits = $warnings::NONE; |
08c6f5ec RH |
790 | next(); |
791 | } | |
792 | ||
793 | my @names; | |
794 | if (ref $val) { | |
795 | @names = @$val; | |
796 | } | |
797 | else { | |
798 | @names = split/\s+/, $val; | |
799 | } | |
800 | ||
810aef70 | 801 | $warning_bits = $warnings::NONE if !defined ($warning_bits); |
08c6f5ec RH |
802 | $warning_bits |= warnings::bits(@names); |
803 | } | |
804 | ||
805 | elsif ($name eq 'warning_bits') { | |
806 | $warning_bits = $val; | |
807 | } | |
808 | ||
809 | elsif ($name eq 'hint_bits') { | |
810 | $hint_bits = $val; | |
811 | } | |
812 | ||
0ced6c29 RGS |
813 | elsif ($name eq '%^H') { |
814 | $hinthash = $val; | |
815 | } | |
816 | ||
08c6f5ec RH |
817 | else { |
818 | croak "Unknown pragma type: $name"; | |
819 | } | |
820 | } | |
821 | if (@_) { | |
822 | croak "The ambient_pragmas method expects an even number of args"; | |
823 | } | |
824 | ||
bc6b2ef6 | 825 | $self->{'ambient_arybase'} = $arybase; |
08c6f5ec | 826 | $self->{'ambient_warnings'} = $warning_bits; |
a0405c92 | 827 | $self->{'ambient_hints'} = $hint_bits; |
0ced6c29 | 828 | $self->{'ambient_hinthash'} = $hinthash; |
08c6f5ec RH |
829 | } |
830 | ||
d989cdac | 831 | # This method is the inner loop, so try to keep it simple |
6e90668e SM |
832 | sub deparse { |
833 | my $self = shift; | |
d989cdac | 834 | my($op, $cx) = @_; |
34a48b4b RH |
835 | |
836 | Carp::confess("Null op in deparse") if !defined($op) | |
837 | || class($op) eq "NULL"; | |
3f872cb9 | 838 | my $meth = "pp_" . $op->name; |
9d2c6865 | 839 | return $self->$meth($op, $cx); |
a798dbf2 MB |
840 | } |
841 | ||
6e90668e | 842 | sub indent { |
f4a44678 | 843 | my $self = shift; |
6e90668e SM |
844 | my $txt = shift; |
845 | my @lines = split(/\n/, $txt); | |
846 | my $leader = ""; | |
f4a44678 | 847 | my $level = 0; |
6e90668e SM |
848 | my $line; |
849 | for $line (@lines) { | |
f4a44678 SM |
850 | my $cmd = substr($line, 0, 1); |
851 | if ($cmd eq "\t" or $cmd eq "\b") { | |
852 | $level += ($cmd eq "\t" ? 1 : -1) * $self->{'indent_size'}; | |
853 | if ($self->{'use_tabs'}) { | |
854 | $leader = "\t" x ($level / 8) . " " x ($level % 8); | |
855 | } else { | |
856 | $leader = " " x $level; | |
857 | } | |
6e90668e SM |
858 | $line = substr($line, 1); |
859 | } | |
f5aa8f4e SM |
860 | if (substr($line, 0, 1) eq "\f") { |
861 | $line = substr($line, 1); # no indent | |
862 | } else { | |
863 | $line = $leader . $line; | |
864 | } | |
9d2c6865 | 865 | $line =~ s/\cK;?//g; |
6e90668e SM |
866 | } |
867 | return join("\n", @lines); | |
868 | } | |
869 | ||
6e90668e SM |
870 | sub deparse_sub { |
871 | my $self = shift; | |
872 | my $cv = shift; | |
873 | my $proto = ""; | |
ce4e655d | 874 | Carp::confess("NULL in deparse_sub") if !defined($cv) || $cv->isa("B::NULL"); |
34a48b4b RH |
875 | Carp::confess("SPECIAL in deparse_sub") if $cv->isa("B::SPECIAL"); |
876 | local $self->{'curcop'} = $self->{'curcop'}; | |
6e90668e SM |
877 | if ($cv->FLAGS & SVf_POK) { |
878 | $proto = "(". $cv->PV . ") "; | |
879 | } | |
aa381260 | 880 | if ($cv->CvFLAGS & (CVf_METHOD|CVf_LOCKED|CVf_LVALUE)) { |
6aaf4108 SC |
881 | $proto .= ": "; |
882 | $proto .= "lvalue " if $cv->CvFLAGS & CVf_LVALUE; | |
aa381260 | 883 | $proto .= "locked " if $cv->CvFLAGS & CVf_LOCKED; |
6aaf4108 SC |
884 | $proto .= "method " if $cv->CvFLAGS & CVf_METHOD; |
885 | } | |
886 | ||
6e90668e | 887 | local($self->{'curcv'}) = $cv; |
8510e997 | 888 | local($self->{'curcvlex'}); |
0ced6c29 RGS |
889 | local(@$self{qw'curstash warnings hints hinthash'}) |
890 | = @$self{qw'curstash warnings hints hinthash'}; | |
ce4e655d | 891 | my $body; |
6e90668e | 892 | if (not null $cv->ROOT) { |
ce4e655d RH |
893 | my $lineseq = $cv->ROOT->first; |
894 | if ($lineseq->name eq "lineseq") { | |
895 | my @ops; | |
896 | for(my$o=$lineseq->first; $$o; $o=$o->sibling) { | |
897 | push @ops, $o; | |
898 | } | |
93a8ff62 | 899 | $body = $self->lineseq(undef, 0, @ops).";"; |
ce4e655d RH |
900 | my $scope_en = $self->find_scope_en($lineseq); |
901 | if (defined $scope_en) { | |
902 | my $subs = join"", $self->seq_subs($scope_en); | |
903 | $body .= ";\n$subs" if length($subs); | |
904 | } | |
905 | } | |
906 | else { | |
907 | $body = $self->deparse($cv->ROOT->first, 0); | |
908 | } | |
de3f1649 | 909 | } |
ce4e655d RH |
910 | else { |
911 | my $sv = $cv->const_sv; | |
912 | if ($$sv) { | |
913 | # uh-oh. inlinable sub... format it differently | |
d989cdac | 914 | return $proto . "{ " . $self->const($sv, 0) . " }\n"; |
ce4e655d RH |
915 | } else { # XSUB? (or just a declaration) |
916 | return "$proto;\n"; | |
917 | } | |
6e90668e | 918 | } |
ce4e655d | 919 | return $proto ."{\n\t$body\n\b}" ."\n"; |
6e90668e SM |
920 | } |
921 | ||
922 | sub deparse_format { | |
923 | my $self = shift; | |
924 | my $form = shift; | |
925 | my @text; | |
926 | local($self->{'curcv'}) = $form; | |
8510e997 | 927 | local($self->{'curcvlex'}); |
67fc2416 | 928 | local($self->{'in_format'}) = 1; |
0ced6c29 RGS |
929 | local(@$self{qw'curstash warnings hints hinthash'}) |
930 | = @$self{qw'curstash warnings hints hinthash'}; | |
6e90668e SM |
931 | my $op = $form->ROOT; |
932 | my $kid; | |
fb725297 RGS |
933 | return "\f." if $op->first->name eq 'stub' |
934 | || $op->first->name eq 'nextstate'; | |
6e90668e SM |
935 | $op = $op->first->first; # skip leavewrite, lineseq |
936 | while (not null $op) { | |
937 | $op = $op->sibling; # skip nextstate | |
938 | my @exprs; | |
939 | $kid = $op->first->sibling; # skip pushmark | |
a5b0cd91 | 940 | push @text, "\f".$self->const_sv($kid)->PV; |
6e90668e SM |
941 | $kid = $kid->sibling; |
942 | for (; not null $kid; $kid = $kid->sibling) { | |
93a8ff62 | 943 | push @exprs, $self->deparse($kid, -1); |
31c26a0a | 944 | $exprs[-1] =~ s/;\z//; |
6e90668e | 945 | } |
a5b0cd91 | 946 | push @text, "\f".join(", ", @exprs)."\n" if @exprs; |
6e90668e SM |
947 | $op = $op->sibling; |
948 | } | |
a5b0cd91 | 949 | return join("", @text) . "\f."; |
6e90668e SM |
950 | } |
951 | ||
6e90668e | 952 | sub is_scope { |
a798dbf2 | 953 | my $op = shift; |
3f872cb9 GS |
954 | return $op->name eq "leave" || $op->name eq "scope" |
955 | || $op->name eq "lineseq" | |
d989cdac | 956 | || ($op->name eq "null" && class($op) eq "UNOP" |
3f872cb9 | 957 | && (is_scope($op->first) || $op->first->name eq "enter")); |
6e90668e SM |
958 | } |
959 | ||
960 | sub is_state { | |
3f872cb9 GS |
961 | my $name = $_[0]->name; |
962 | return $name eq "nextstate" || $name eq "dbstate" || $name eq "setstate"; | |
6e90668e SM |
963 | } |
964 | ||
e38ccfd9 | 965 | sub is_miniwhile { # check for one-line loop ('foo() while $y--') |
6e90668e | 966 | my $op = shift; |
d989cdac | 967 | return (!null($op) and null($op->sibling) |
3f872cb9 GS |
968 | and $op->name eq "null" and class($op) eq "UNOP" |
969 | and (($op->first->name =~ /^(and|or)$/ | |
970 | and $op->first->first->sibling->name eq "lineseq") | |
971 | or ($op->first->name eq "lineseq" | |
6e90668e | 972 | and not null $op->first->first->sibling |
3f872cb9 | 973 | and $op->first->first->sibling->name eq "unstack") |
6e90668e SM |
974 | )); |
975 | } | |
976 | ||
d989cdac SM |
977 | # Check if the op and its sibling are the initialization and the rest of a |
978 | # for (..;..;..) { ... } loop | |
979 | sub is_for_loop { | |
980 | my $op = shift; | |
981 | # This OP might be almost anything, though it won't be a | |
982 | # nextstate. (It's the initialization, so in the canonical case it | |
eae48c89 Z |
983 | # will be an sassign.) The sibling is (old style) a lineseq whose |
984 | # first child is a nextstate and whose second is a leaveloop, or | |
985 | # (new style) an unstack whose sibling is a leaveloop. | |
d989cdac | 986 | my $lseq = $op->sibling; |
eae48c89 Z |
987 | return 0 unless !is_state($op) and !null($lseq); |
988 | if ($lseq->name eq "lineseq") { | |
d989cdac SM |
989 | if ($lseq->first && !null($lseq->first) && is_state($lseq->first) |
990 | && (my $sib = $lseq->first->sibling)) { | |
991 | return (!null($sib) && $sib->name eq "leaveloop"); | |
992 | } | |
eae48c89 Z |
993 | } elsif ($lseq->name eq "unstack" && ($lseq->flags & OPf_SPECIAL)) { |
994 | my $sib = $lseq->sibling; | |
995 | return $sib && !null($sib) && $sib->name eq "leaveloop"; | |
d989cdac SM |
996 | } |
997 | return 0; | |
998 | } | |
999 | ||
6e90668e SM |
1000 | sub is_scalar { |
1001 | my $op = shift; | |
3f872cb9 GS |
1002 | return ($op->name eq "rv2sv" or |
1003 | $op->name eq "padsv" or | |
1004 | $op->name eq "gv" or # only in array/hash constructs | |
bd0865ec | 1005 | $op->flags & OPf_KIDS && !null($op->first) |
3f872cb9 | 1006 | && $op->first->name eq "gvsv"); |
6e90668e SM |
1007 | } |
1008 | ||
9d2c6865 SM |
1009 | sub maybe_parens { |
1010 | my $self = shift; | |
1011 | my($text, $cx, $prec) = @_; | |
1012 | if ($prec < $cx # unary ops nest just fine | |
1013 | or $prec == $cx and $cx != 4 and $cx != 16 and $cx != 21 | |
1014 | or $self->{'parens'}) | |
1015 | { | |
1016 | $text = "($text)"; | |
1017 | # In a unop, let parent reuse our parens; see maybe_parens_unop | |
1018 | $text = "\cS" . $text if $cx == 16; | |
1019 | return $text; | |
1020 | } else { | |
1021 | return $text; | |
1022 | } | |
1023 | } | |
1024 | ||
e38ccfd9 | 1025 | # same as above, but get around the 'if it looks like a function' rule |
9d2c6865 SM |
1026 | sub maybe_parens_unop { |
1027 | my $self = shift; | |
1028 | my($name, $kid, $cx) = @_; | |
1029 | if ($cx > 16 or $self->{'parens'}) { | |
a0035eb8 RH |
1030 | $kid = $self->deparse($kid, 1); |
1031 | if ($name eq "umask" && $kid =~ /^\d+$/) { | |
1032 | $kid = sprintf("%#o", $kid); | |
1033 | } | |
4a1ac32e | 1034 | return $self->keyword($name) . "($kid)"; |
9d2c6865 SM |
1035 | } else { |
1036 | $kid = $self->deparse($kid, 16); | |
a0035eb8 RH |
1037 | if ($name eq "umask" && $kid =~ /^\d+$/) { |
1038 | $kid = sprintf("%#o", $kid); | |
1039 | } | |
4a1ac32e | 1040 | $name = $self->keyword($name); |
9d2c6865 SM |
1041 | if (substr($kid, 0, 1) eq "\cS") { |
1042 | # use kid's parens | |
1043 | return $name . substr($kid, 1); | |
1044 | } elsif (substr($kid, 0, 1) eq "(") { | |
1045 | # avoid looks-like-a-function trap with extra parens | |
e38ccfd9 | 1046 | # ('+' can lead to ambiguities) |
9d2c6865 SM |
1047 | return "$name(" . $kid . ")"; |
1048 | } else { | |
1049 | return "$name $kid"; | |
1050 | } | |
1051 | } | |
1052 | } | |
1053 | ||
1054 | sub maybe_parens_func { | |
1055 | my $self = shift; | |
1056 | my($func, $text, $cx, $prec) = @_; | |
1057 | if ($prec <= $cx or substr($text, 0, 1) eq "(" or $self->{'parens'}) { | |
1058 | return "$func($text)"; | |
1059 | } else { | |
1060 | return "$func $text"; | |
1061 | } | |
1062 | } | |
1063 | ||
6e90668e SM |
1064 | sub maybe_local { |
1065 | my $self = shift; | |
9d2c6865 | 1066 | my($op, $cx, $text) = @_; |
ce4e655d RH |
1067 | my $our_intro = ($op->name =~ /^(gv|rv2)[ash]v$/) ? OPpOUR_INTRO : 0; |
1068 | if ($op->private & (OPpLVAL_INTRO|$our_intro) | |
1069 | and not $self->{'avoid_local'}{$$op}) { | |
1070 | my $our_local = ($op->private & OPpLVAL_INTRO) ? "local" : "our"; | |
8e3542b6 | 1071 | if( $our_local eq 'our' ) { |
640d5d41 FC |
1072 | if ( $text !~ /^\W(\w+::)*\w+\z/ |
1073 | and !utf8::decode($text) || $text !~ /^\W(\w+::)*\w+\z/ | |
1074 | ) { | |
1075 | die "Unexpected our($text)\n"; | |
1076 | } | |
d989cdac | 1077 | $text =~ s/(\w+::)+//; |
8e3542b6 | 1078 | } |
e8d3f51b | 1079 | if (want_scalar($op)) { |
ce4e655d | 1080 | return "$our_local $text"; |
e8d3f51b | 1081 | } else { |
ce4e655d | 1082 | return $self->maybe_parens_func("$our_local", $text, $cx, 16); |
e8d3f51b | 1083 | } |
6e90668e SM |
1084 | } else { |
1085 | return $text; | |
a798dbf2 | 1086 | } |
a798dbf2 MB |
1087 | } |
1088 | ||
3ed82cfc GS |
1089 | sub maybe_targmy { |
1090 | my $self = shift; | |
1091 | my($op, $cx, $func, @args) = @_; | |
1092 | if ($op->private & OPpTARGET_MY) { | |
1093 | my $var = $self->padname($op->targ); | |
1094 | my $val = $func->($self, $op, 7, @args); | |
1095 | return $self->maybe_parens("$var = $val", $cx, 7); | |
1096 | } else { | |
1097 | return $func->($self, $op, $cx, @args); | |
1098 | } | |
1099 | } | |
1100 | ||
6e90668e SM |
1101 | sub padname_sv { |
1102 | my $self = shift; | |
1103 | my $targ = shift; | |
d989cdac | 1104 | return $self->{'curcv'}->PADLIST->ARRAYelt(0)->ARRAYelt($targ); |
6e90668e SM |
1105 | } |
1106 | ||
1107 | sub maybe_my { | |
1108 | my $self = shift; | |
9d2c6865 | 1109 | my($op, $cx, $text) = @_; |
4c1f658f | 1110 | if ($op->private & OPpLVAL_INTRO and not $self->{'avoid_local'}{$$op}) { |
80e3f4ad FC |
1111 | my $my = $op->private & OPpPAD_STATE |
1112 | ? $self->keyword("state") | |
1113 | : "my"; | |
e8d3f51b | 1114 | if (want_scalar($op)) { |
3462b4ac | 1115 | return "$my $text"; |
e8d3f51b | 1116 | } else { |
3462b4ac | 1117 | return $self->maybe_parens_func($my, $text, $cx, 16); |
e8d3f51b | 1118 | } |
6e90668e SM |
1119 | } else { |
1120 | return $text; | |
1121 | } | |
1122 | } | |
1123 | ||
9d2c6865 SM |
1124 | # The following OPs don't have functions: |
1125 | ||
1126 | # pp_padany -- does not exist after parsing | |
9d2c6865 | 1127 | |
2ae48fff RGS |
1128 | sub AUTOLOAD { |
1129 | if ($AUTOLOAD =~ s/^.*::pp_//) { | |
1130 | warn "unexpected OP_".uc $AUTOLOAD; | |
1131 | return "XXX"; | |
1132 | } else { | |
1133 | die "Undefined subroutine $AUTOLOAD called"; | |
1134 | } | |
9d2c6865 | 1135 | } |
6e90668e | 1136 | |
611c1e95 IZ |
1137 | sub DESTROY {} # Do not AUTOLOAD |
1138 | ||
ce4e655d RH |
1139 | # $root should be the op which represents the root of whatever |
1140 | # we're sequencing here. If it's undefined, then we don't append | |
1141 | # any subroutine declarations to the deparsed ops, otherwise we | |
1142 | # append appropriate declarations. | |
58cccf98 | 1143 | sub lineseq { |
93a8ff62 | 1144 | my($self, $root, $cx, @ops) = @_; |
58cccf98 | 1145 | my($expr, @exprs); |
ce4e655d RH |
1146 | |
1147 | my $out_cop = $self->{'curcop'}; | |
1148 | my $out_seq = defined($out_cop) ? $out_cop->cop_seq : undef; | |
1149 | my $limit_seq; | |
1150 | if (defined $root) { | |
1151 | $limit_seq = $out_seq; | |
76df5e8f DM |
1152 | my $nseq; |
1153 | $nseq = $self->find_scope_st($root->sibling) if ${$root->sibling}; | |
ce4e655d RH |
1154 | $limit_seq = $nseq if !defined($limit_seq) |
1155 | or defined($nseq) && $nseq < $limit_seq; | |
1156 | } | |
1157 | $limit_seq = $self->{'limit_seq'} | |
1158 | if defined($self->{'limit_seq'}) | |
1159 | && (!defined($limit_seq) || $self->{'limit_seq'} < $limit_seq); | |
1160 | local $self->{'limit_seq'} = $limit_seq; | |
09d856fb CK |
1161 | |
1162 | $self->walk_lineseq($root, \@ops, | |
1163 | sub { push @exprs, $_[0]} ); | |
1164 | ||
93a8ff62 FC |
1165 | my $sep = $cx ? '; ' : ";\n"; |
1166 | my $body = join($sep, grep {length} @exprs); | |
ce4e655d | 1167 | my $subs = ""; |
67fc2416 | 1168 | if (defined $root && defined $limit_seq && !$self->{'in_format'}) { |
ce4e655d RH |
1169 | $subs = join "\n", $self->seq_subs($limit_seq); |
1170 | } | |
93a8ff62 | 1171 | return join($sep, grep {length} $body, $subs); |
6e90668e SM |
1172 | } |
1173 | ||
58cccf98 | 1174 | sub scopeop { |
d989cdac | 1175 | my($real_block, $self, $op, $cx) = @_; |
58cccf98 SM |
1176 | my $kid; |
1177 | my @kids; | |
a0405c92 | 1178 | |
0ced6c29 RGS |
1179 | local(@$self{qw'curstash warnings hints hinthash'}) |
1180 | = @$self{qw'curstash warnings hints hinthash'} if $real_block; | |
58cccf98 SM |
1181 | if ($real_block) { |
1182 | $kid = $op->first->sibling; # skip enter | |
1183 | if (is_miniwhile($kid)) { | |
1184 | my $top = $kid->first; | |
1185 | my $name = $top->name; | |
1186 | if ($name eq "and") { | |
1187 | $name = "while"; | |
1188 | } elsif ($name eq "or") { | |
1189 | $name = "until"; | |
1190 | } else { # no conditional -> while 1 or until 0 | |
1191 | return $self->deparse($top->first, 1) . " while 1"; | |
1192 | } | |
1193 | my $cond = $top->first; | |
1194 | my $body = $cond->sibling->first; # skip lineseq | |
1195 | $cond = $self->deparse($cond, 1); | |
1196 | $body = $self->deparse($body, 1); | |
1197 | return "$body $name $cond"; | |
6e90668e | 1198 | } |
58cccf98 SM |
1199 | } else { |
1200 | $kid = $op->first; | |
1201 | } | |
1202 | for (; !null($kid); $kid = $kid->sibling) { | |
1203 | push @kids, $kid; | |
6e90668e | 1204 | } |
d989cdac | 1205 | if ($cx > 0) { # inside an expression, (a do {} while for lineseq) |
93a8ff62 | 1206 | return "do {\n\t" . $self->lineseq($op, 0, @kids) . "\n\b}"; |
9d2c6865 | 1207 | } else { |
93a8ff62 | 1208 | my $lineseq = $self->lineseq($op, $cx, @kids); |
7a9b44b9 | 1209 | return (length ($lineseq) ? "$lineseq;" : ""); |
6e90668e | 1210 | } |
6e90668e SM |
1211 | } |
1212 | ||
ce4e655d | 1213 | sub pp_scope { scopeop(0, @_); } |
58cccf98 SM |
1214 | sub pp_lineseq { scopeop(0, @_); } |
1215 | sub pp_leave { scopeop(1, @_); } | |
9d2c6865 | 1216 | |
d989cdac SM |
1217 | # This is a special case of scopeop and lineseq, for the case of the |
1218 | # main_root. The difference is that we print the output statements as | |
1219 | # soon as we get them, for the sake of impatient users. | |
1220 | sub deparse_root { | |
1221 | my $self = shift; | |
1222 | my($op) = @_; | |
0ced6c29 RGS |
1223 | local(@$self{qw'curstash warnings hints hinthash'}) |
1224 | = @$self{qw'curstash warnings hints hinthash'}; | |
d989cdac | 1225 | my @kids; |
4ca8de37 | 1226 | return if null $op->first; # Can happen, e.g., for Bytecode without -k |
d989cdac SM |
1227 | for (my $kid = $op->first->sibling; !null($kid); $kid = $kid->sibling) { |
1228 | push @kids, $kid; | |
1229 | } | |
09d856fb CK |
1230 | $self->walk_lineseq($op, \@kids, |
1231 | sub { print $self->indent($_[0].';'); | |
1232 | print "\n" unless $_[1] == $#kids; | |
1233 | }); | |
1234 | } | |
1235 | ||
1236 | sub walk_lineseq { | |
1237 | my ($self, $op, $kids, $callback) = @_; | |
1238 | my @kids = @$kids; | |
d989cdac SM |
1239 | for (my $i = 0; $i < @kids; $i++) { |
1240 | my $expr = ""; | |
1241 | if (is_state $kids[$i]) { | |
09d856fb | 1242 | $expr = $self->deparse($kids[$i++], 0); |
d989cdac | 1243 | if ($i > $#kids) { |
09d856fb | 1244 | $callback->($expr, $i); |
d989cdac SM |
1245 | last; |
1246 | } | |
1247 | } | |
1248 | if (is_for_loop($kids[$i])) { | |
eae48c89 Z |
1249 | $callback->($expr . $self->for_loop($kids[$i], 0), |
1250 | $i += $kids[$i]->sibling->name eq "unstack" ? 2 : 1); | |
d989cdac SM |
1251 | next; |
1252 | } | |
1253 | $expr .= $self->deparse($kids[$i], (@kids != 1)/2); | |
1254 | $expr =~ s/;\n?\z//; | |
09d856fb | 1255 | $callback->($expr, $i); |
d989cdac SM |
1256 | } |
1257 | } | |
1258 | ||
6e90668e SM |
1259 | # The BEGIN {} is used here because otherwise this code isn't executed |
1260 | # when you run B::Deparse on itself. | |
1261 | my %globalnames; | |
1262 | BEGIN { map($globalnames{$_}++, "SIG", "STDIN", "STDOUT", "STDERR", "INC", | |
1263 | "ENV", "ARGV", "ARGVOUT", "_"); } | |
1264 | ||
1265 | sub gv_name { | |
1266 | my $self = shift; | |
1267 | my $gv = shift; | |
b89b7257 | 1268 | my $raw = shift; |
c6e79e55 | 1269 | Carp::confess() unless ref($gv) eq "B::GV"; |
6e90668e | 1270 | my $stash = $gv->STASH->NAME; |
b89b7257 | 1271 | my $name = $raw ? $gv->NAME : $gv->SAFENAME; |
8b2d6640 FC |
1272 | if ($stash eq 'main' && $name =~ /^::/) { |
1273 | $stash = '::'; | |
1274 | } | |
b861b87f FC |
1275 | elsif (($stash eq 'main' |
1276 | && ($globalnames{$name} || $name =~ /^[^A-Za-z_:]/)) | |
8b2d6640 FC |
1277 | or ($stash eq $self->{'curstash'} && !$globalnames{$name} |
1278 | && ($stash eq 'main' || $name !~ /::/)) | |
b861b87f | 1279 | ) |
9d2c6865 | 1280 | { |
6e90668e SM |
1281 | $stash = ""; |
1282 | } else { | |
1283 | $stash = $stash . "::"; | |
a798dbf2 | 1284 | } |
b89b7257 | 1285 | if (!$raw and $name =~ /^(\^..|{)/) { |
083bda02 | 1286 | $name = "{$name}"; # ${^WARNING_BITS}, etc and ${ |
6e90668e SM |
1287 | } |
1288 | return $stash . $name; | |
a798dbf2 MB |
1289 | } |
1290 | ||
8510e997 | 1291 | # Return the name to use for a stash variable. |
415d4c68 FC |
1292 | # If a lexical with the same name is in scope, or |
1293 | # if strictures are enabled, it may need to be | |
8510e997 RH |
1294 | # fully-qualified. |
1295 | sub stash_variable { | |
bb8996b8 | 1296 | my ($self, $prefix, $name, $cx) = @_; |
8510e997 RH |
1297 | |
1298 | return "$prefix$name" if $name =~ /::/; | |
1299 | ||
d989cdac | 1300 | unless ($prefix eq '$' || $prefix eq '@' || #' |
8510e997 RH |
1301 | $prefix eq '%' || $prefix eq '$#') { |
1302 | return "$prefix$name"; | |
1303 | } | |
1304 | ||
61154ac0 FC |
1305 | if ($name =~ /^[^\w+-]$/) { |
1306 | if (defined $cx && $cx == 26) { | |
1307 | if ($prefix eq '@') { | |
bb8996b8 HY |
1308 | return "$prefix\{$name}"; |
1309 | } | |
61154ac0 FC |
1310 | elsif ($name eq '#') { return '${#}' } # "${#}a" vs "$#a" |
1311 | } | |
1312 | if ($prefix eq '$#') { | |
6ec73527 | 1313 | return "\$#{$name}"; |
61154ac0 | 1314 | } |
6ec73527 | 1315 | } |
bb8996b8 | 1316 | |
415d4c68 | 1317 | return $prefix . $self->maybe_qualify($prefix, $name); |
8510e997 RH |
1318 | } |
1319 | ||
be6cf5cf FC |
1320 | # Return just the name, without the prefix. It may be returned as a quoted |
1321 | # string. The second return value is a boolean indicating that. | |
1322 | sub stash_variable_name { | |
1323 | my($self, $prefix, $gv) = @_; | |
1324 | my $name = $self->gv_name($gv, 1); | |
415d4c68 | 1325 | $name = $self->maybe_qualify($prefix,$name); |
be6cf5cf FC |
1326 | if ($name =~ /^(?:\S|(?!\d)[\ca-\cz]?(?:\w|::)*|\d+)\z/) { |
1327 | $name =~ s/^([\ca-\cz])/'^'.($1|'@')/e; | |
1328 | $name =~ /^(\^..|{)/ and $name = "{$name}"; | |
1329 | return $name, 0; # not quoted | |
1330 | } | |
1331 | else { | |
1332 | single_delim("q", "'", $name), 1; | |
1333 | } | |
1334 | } | |
1335 | ||
415d4c68 FC |
1336 | sub maybe_qualify { |
1337 | my ($self,$prefix,$name) = @_; | |
1338 | my $v = ($prefix eq '$#' ? '@' : $prefix) . $name; | |
1339 | return $name if !$prefix || $name =~ /::/; | |
1340 | return $self->{'curstash'}.'::'. $name | |
1341 | if | |
36727b53 FC |
1342 | $name =~ /^(?!\d)\w/ # alphabetic |
1343 | && $v !~ /^\$[ab]\z/ # not $a or $b | |
415d4c68 FC |
1344 | && !$globalnames{$name} # not a global name |
1345 | && $self->{hints} & $strict_bits{vars} # strict vars | |
1346 | && !$self->lex_in_scope($v,1) # no "our" | |
1347 | or $self->lex_in_scope($v); # conflicts with "my" variable | |
1348 | return $name; | |
1349 | } | |
1350 | ||
8510e997 | 1351 | sub lex_in_scope { |
415d4c68 FC |
1352 | my ($self, $name, $our) = @_; |
1353 | substr $name, 0, 0, = $our ? 'o' : 'm'; # our/my | |
8510e997 RH |
1354 | $self->populate_curcvlex() if !defined $self->{'curcvlex'}; |
1355 | ||
6ec152c3 | 1356 | return 0 if !defined($self->{'curcop'}); |
8510e997 RH |
1357 | my $seq = $self->{'curcop'}->cop_seq; |
1358 | return 0 if !exists $self->{'curcvlex'}{$name}; | |
1359 | for my $a (@{$self->{'curcvlex'}{$name}}) { | |
1360 | my ($st, $en) = @$a; | |
1361 | return 1 if $seq > $st && $seq <= $en; | |
1362 | } | |
1363 | return 0; | |
1364 | } | |
1365 | ||
1366 | sub populate_curcvlex { | |
1367 | my $self = shift; | |
ce4e655d | 1368 | for (my $cv = $self->{'curcv'}; class($cv) eq "CV"; $cv = $cv->OUTSIDE) { |
7dafbf52 DM |
1369 | my $padlist = $cv->PADLIST; |
1370 | # an undef CV still in lexical chain | |
1371 | next if class($padlist) eq "SPECIAL"; | |
1372 | my @padlist = $padlist->ARRAY; | |
8510e997 RH |
1373 | my @ns = $padlist[0]->ARRAY; |
1374 | ||
1375 | for (my $i=0; $i<@ns; ++$i) { | |
1376 | next if class($ns[$i]) eq "SPECIAL"; | |
0f2fe21d RH |
1377 | if (class($ns[$i]) eq "PV") { |
1378 | # Probably that pesky lexical @_ | |
1379 | next; | |
1380 | } | |
8510e997 | 1381 | my $name = $ns[$i]->PVX; |
7dafbf52 DM |
1382 | my ($seq_st, $seq_en) = |
1383 | ($ns[$i]->FLAGS & SVf_FAKE) | |
1384 | ? (0, 999999) | |
809abb02 | 1385 | : ($ns[$i]->COP_SEQ_RANGE_LOW, $ns[$i]->COP_SEQ_RANGE_HIGH); |
8510e997 | 1386 | |
415d4c68 FC |
1387 | push @{$self->{'curcvlex'}{ |
1388 | ($ns[$i]->FLAGS & SVpad_OUR ? 'o' : 'm') . $name | |
1389 | }}, [$seq_st, $seq_en]; | |
8510e997 RH |
1390 | } |
1391 | } | |
1392 | } | |
1393 | ||
ce4e655d RH |
1394 | sub find_scope_st { ((find_scope(@_))[0]); } |
1395 | sub find_scope_en { ((find_scope(@_))[1]); } | |
1396 | ||
1397 | # Recurses down the tree, looking for pad variable introductions and COPs | |
1398 | sub find_scope { | |
1399 | my ($self, $op, $scope_st, $scope_en) = @_; | |
ff97752d | 1400 | carp("Undefined op in find_scope") if !defined $op; |
ce4e655d RH |
1401 | return ($scope_st, $scope_en) unless $op->flags & OPf_KIDS; |
1402 | ||
b6b46d6f AB |
1403 | my @queue = ($op); |
1404 | while(my $op = shift @queue ) { | |
1405 | for (my $o=$op->first; $$o; $o=$o->sibling) { | |
1406 | if ($o->name =~ /^pad.v$/ && $o->private & OPpLVAL_INTRO) { | |
1407 | my $s = int($self->padname_sv($o->targ)->COP_SEQ_RANGE_LOW); | |
1408 | my $e = $self->padname_sv($o->targ)->COP_SEQ_RANGE_HIGH; | |
1409 | $scope_st = $s if !defined($scope_st) || $s < $scope_st; | |
1410 | $scope_en = $e if !defined($scope_en) || $e > $scope_en; | |
1411 | return ($scope_st, $scope_en); | |
1412 | } | |
1413 | elsif (is_state($o)) { | |
1414 | my $c = $o->cop_seq; | |
1415 | $scope_st = $c if !defined($scope_st) || $c < $scope_st; | |
1416 | $scope_en = $c if !defined($scope_en) || $c > $scope_en; | |
1417 | return ($scope_st, $scope_en); | |
1418 | } | |
1419 | elsif ($o->flags & OPf_KIDS) { | |
1420 | unshift (@queue, $o); | |
1421 | } | |
34a48b4b RH |
1422 | } |
1423 | } | |
ce4e655d RH |
1424 | |
1425 | return ($scope_st, $scope_en); | |
34a48b4b RH |
1426 | } |
1427 | ||
1428 | # Returns a list of subs which should be inserted before the COP | |
1429 | sub cop_subs { | |
1430 | my ($self, $op, $out_seq) = @_; | |
1431 | my $seq = $op->cop_seq; | |
1432 | # If we have nephews, then our sequence number indicates | |
1433 | # the cop_seq of the end of some sort of scope. | |
1434 | if (class($op->sibling) ne "NULL" && $op->sibling->flags & OPf_KIDS | |
ce4e655d RH |
1435 | and my $nseq = $self->find_scope_st($op->sibling) ) { |
1436 | $seq = $nseq; | |
34a48b4b RH |
1437 | } |
1438 | $seq = $out_seq if defined($out_seq) && $out_seq < $seq; | |
1439 | return $self->seq_subs($seq); | |
1440 | } | |
1441 | ||
1442 | sub seq_subs { | |
1443 | my ($self, $seq) = @_; | |
1444 | my @text; | |
1445 | #push @text, "# ($seq)\n"; | |
1446 | ||
ce4e655d | 1447 | return "" if !defined $seq; |
34a48b4b RH |
1448 | while (scalar(@{$self->{'subs_todo'}}) |
1449 | and $seq > $self->{'subs_todo'}[0][0]) { | |
1450 | push @text, $self->next_todo; | |
1451 | } | |
1452 | return @text; | |
1453 | } | |
1454 | ||
95c04cde NC |
1455 | sub _features_from_bundle { |
1456 | my ($hints, $hh) = @_; | |
1873980a | 1457 | foreach (@{$feature::feature_bundle{@feature::hint_bundles[$hints >> $feature::hint_shift]}}) { |
95c04cde NC |
1458 | $hh->{$feature::feature{$_}} = 1; |
1459 | } | |
1460 | return $hh; | |
1461 | } | |
1462 | ||
08c6f5ec | 1463 | # Notice how subs and formats are inserted between statements here; |
bc6b2ef6 | 1464 | # also $[ assignments and pragmas. |
6e90668e SM |
1465 | sub pp_nextstate { |
1466 | my $self = shift; | |
9d2c6865 | 1467 | my($op, $cx) = @_; |
34a48b4b | 1468 | $self->{'curcop'} = $op; |
6e90668e | 1469 | my @text; |
34a48b4b | 1470 | push @text, $self->cop_subs($op); |
11faa288 | 1471 | my $stash = $op->stashpv; |
6e90668e SM |
1472 | if ($stash ne $self->{'curstash'}) { |
1473 | push @text, "package $stash;\n"; | |
1474 | $self->{'curstash'} = $stash; | |
1475 | } | |
08c6f5ec | 1476 | |
bc6b2ef6 Z |
1477 | if (OPpCONST_ARYBASE && $self->{'arybase'} != $op->arybase) { |
1478 | push @text, '$[ = '. $op->arybase .";\n"; | |
1479 | $self->{'arybase'} = $op->arybase; | |
1480 | } | |
1481 | ||
7a9b44b9 RH |
1482 | my $warnings = $op->warnings; |
1483 | my $warning_bits; | |
1484 | if ($warnings->isa("B::SPECIAL") && $$warnings == 4) { | |
810aef70 | 1485 | $warning_bits = $warnings::Bits{"all"} & WARN_MASK; |
7a9b44b9 | 1486 | } |
e31885a0 | 1487 | elsif ($warnings->isa("B::SPECIAL") && $$warnings == 5) { |
810aef70 | 1488 | $warning_bits = $warnings::NONE; |
7a9b44b9 | 1489 | } |
e31885a0 RH |
1490 | elsif ($warnings->isa("B::SPECIAL")) { |
1491 | $warning_bits = undef; | |
1492 | } | |
7a9b44b9 | 1493 | else { |
34a48b4b | 1494 | $warning_bits = $warnings->PV & WARN_MASK; |
7a9b44b9 RH |
1495 | } |
1496 | ||
e31885a0 RH |
1497 | if (defined ($warning_bits) and |
1498 | !defined($self->{warnings}) || $self->{'warnings'} ne $warning_bits) { | |
08c6f5ec | 1499 | push @text, declare_warnings($self->{'warnings'}, $warning_bits); |
7a9b44b9 RH |
1500 | $self->{'warnings'} = $warning_bits; |
1501 | } | |
1502 | ||
2be95ceb | 1503 | my $hints = $] < 5.008009 ? $op->private : $op->hints; |
0bb01b05 | 1504 | my $old_hints = $self->{'hints'}; |
2be95ceb NC |
1505 | if ($self->{'hints'} != $hints) { |
1506 | push @text, declare_hints($self->{'hints'}, $hints); | |
1507 | $self->{'hints'} = $hints; | |
a0405c92 RH |
1508 | } |
1509 | ||
0bb01b05 FC |
1510 | my $newhh; |
1511 | if ($] > 5.009) { | |
1512 | $newhh = $op->hints_hash->HASH; | |
1513 | } | |
1514 | ||
1515 | if ($] >= 5.015006) { | |
1516 | # feature bundle hints | |
149758b3 NC |
1517 | my $from = $old_hints & $feature::hint_mask; |
1518 | my $to = $ hints & $feature::hint_mask; | |
0bb01b05 | 1519 | if ($from != $to) { |
149758b3 | 1520 | if ($to == $feature::hint_mask) { |
0bb01b05 FC |
1521 | if ($self->{'hinthash'}) { |
1522 | delete $self->{'hinthash'}{$_} | |
1523 | for grep /^feature_/, keys %{$self->{'hinthash'}}; | |
1524 | } | |
1525 | else { $self->{'hinthash'} = {} } | |
95c04cde NC |
1526 | $self->{'hinthash'} |
1527 | = _features_from_bundle($from, $self->{'hinthash'}); | |
0bb01b05 FC |
1528 | } |
1529 | else { | |
1530 | my $bundle = | |
1531 | $feature::hint_bundles[$to >> $feature::hint_shift]; | |
1532 | $bundle =~ s/(\d[13579])\z/$1+1/e; # 5.11 => 5.12 | |
1533 | push @text, "no feature;\n", | |
1534 | "use feature ':$bundle';\n"; | |
1535 | } | |
1536 | } | |
1537 | } | |
1538 | ||
1539 | if ($] > 5.009) { | |
1540 | push @text, declare_hinthash( | |
1541 | $self->{'hinthash'}, $newhh, | |
1542 | $self->{indent_size}, $self->{hints}, | |
1543 | ); | |
1544 | $self->{'hinthash'} = $newhh; | |
0ced6c29 RGS |
1545 | } |
1546 | ||
d989cdac SM |
1547 | # This should go after of any branches that add statements, to |
1548 | # increase the chances that it refers to the same line it did in | |
1549 | # the original program. | |
1550 | if ($self->{'linenums'}) { | |
1551 | push @text, "\f#line " . $op->line . | |
1552 | ' "' . $op->file, qq'"\n'; | |
1553 | } | |
1554 | ||
98a1a137 Z |
1555 | push @text, $op->label . ": " if $op->label; |
1556 | ||
6e90668e SM |
1557 | return join("", @text); |
1558 | } | |
1559 | ||
08c6f5ec RH |
1560 | sub declare_warnings { |
1561 | my ($from, $to) = @_; | |
e6f1f756 | 1562 | if (($to & WARN_MASK) eq (warnings::bits("all") & WARN_MASK)) { |
a0405c92 RH |
1563 | return "use warnings;\n"; |
1564 | } | |
e6f1f756 | 1565 | elsif (($to & WARN_MASK) eq ("\0"x length($to) & WARN_MASK)) { |
a0405c92 RH |
1566 | return "no warnings;\n"; |
1567 | } | |
51a5edaf | 1568 | return "BEGIN {\${^WARNING_BITS} = ".perlstring($to)."}\n"; |
a0405c92 RH |
1569 | } |
1570 | ||
1571 | sub declare_hints { | |
1572 | my ($from, $to) = @_; | |
a0035eb8 RH |
1573 | my $use = $to & ~$from; |
1574 | my $no = $from & ~$to; | |
1575 | my $decls = ""; | |
1576 | for my $pragma (hint_pragmas($use)) { | |
1577 | $decls .= "use $pragma;\n"; | |
1578 | } | |
1579 | for my $pragma (hint_pragmas($no)) { | |
1580 | $decls .= "no $pragma;\n"; | |
1581 | } | |
1582 | return $decls; | |
1583 | } | |
1584 | ||
493c23c6 NC |
1585 | # Internal implementation hints that the core sets automatically, so don't need |
1586 | # (or want) to be passed back to the user | |
1587 | my %ignored_hints = ( | |
1588 | 'open<' => 1, | |
1589 | 'open>' => 1, | |
dca6062a | 1590 | ':' => 1, |
1c74777c FC |
1591 | 'strict/refs' => 1, |
1592 | 'strict/subs' => 1, | |
1593 | 'strict/vars' => 1, | |
2e8342de | 1594 | ); |
493c23c6 | 1595 | |
a8095af7 FC |
1596 | my %rev_feature; |
1597 | ||
0ced6c29 | 1598 | sub declare_hinthash { |
0bb01b05 | 1599 | my ($from, $to, $indent, $hints) = @_; |
a8095af7 | 1600 | my $doing_features = |
149758b3 | 1601 | ($hints & $feature::hint_mask) == $feature::hint_mask; |
0ced6c29 | 1602 | my @decls; |
a8095af7 FC |
1603 | my @features; |
1604 | my @unfeatures; # bugs? | |
0bb01b05 | 1605 | for my $key (sort keys %$to) { |
493c23c6 | 1606 | next if $ignored_hints{$key}; |
a8095af7 FC |
1607 | my $is_feature = $key =~ /^feature_/ && $^V ge 5.15.6; |
1608 | next if $is_feature and not $doing_features; | |
04be0204 | 1609 | if (!exists $from->{$key} or $from->{$key} ne $to->{$key}) { |
a8095af7 | 1610 | push(@features, $key), next if $is_feature; |
035146a3 FC |
1611 | push @decls, |
1612 | qq(\$^H{) . single_delim("q", "'", $key) . qq(} = ) | |
1613 | . ( | |
1614 | defined $to->{$key} | |
1615 | ? single_delim("q", "'", $to->{$key}) | |
1616 | : 'undef' | |
1617 | ) | |
04be0204 | 1618 | . qq(;); |
0ced6c29 RGS |
1619 | } |
1620 | } | |
0bb01b05 | 1621 | for my $key (sort keys %$from) { |
493c23c6 | 1622 | next if $ignored_hints{$key}; |
a8095af7 FC |
1623 | my $is_feature = $key =~ /^feature_/ && $^V ge 5.15.6; |
1624 | next if $is_feature and not $doing_features; | |
0ced6c29 | 1625 | if (!exists $to->{$key}) { |
a8095af7 | 1626 | push(@unfeatures, $key), next if $is_feature; |
0ced6c29 RGS |
1627 | push @decls, qq(delete \$^H{'$key'};); |
1628 | } | |
1629 | } | |
a8095af7 FC |
1630 | my @ret; |
1631 | if (@features || @unfeatures) { | |
a8095af7 FC |
1632 | if (!%rev_feature) { %rev_feature = reverse %feature::feature } |
1633 | } | |
1634 | if (@features) { | |
1635 | push @ret, "use feature " | |
1636 | . join(", ", map "'$rev_feature{$_}'", @features) . ";\n"; | |
1637 | } | |
1638 | if (@unfeatures) { | |
1639 | push @ret, "no feature " | |
1640 | . join(", ", map "'$rev_feature{$_}'", @unfeatures) | |
1641 | . ";\n"; | |
1642 | } | |
1643 | @decls and | |
1644 | push @ret, | |
1645 | join("\n" . (" " x $indent), "BEGIN {", @decls) . "\n}\n"; | |
1646 | return @ret; | |
0ced6c29 RGS |
1647 | } |
1648 | ||
a0035eb8 RH |
1649 | sub hint_pragmas { |
1650 | my ($bits) = @_; | |
415d4c68 | 1651 | my (@pragmas, @strict); |
a0035eb8 | 1652 | push @pragmas, "integer" if $bits & 0x1; |
415d4c68 FC |
1653 | for (sort keys %strict_bits) { |
1654 | push @strict, "'$_'" if $bits & $strict_bits{$_}; | |
1655 | } | |
1656 | if (@strict == keys %strict_bits) { | |
1657 | push @pragmas, "strict"; | |
1658 | } | |
1659 | elsif (@strict) { | |
1660 | push @pragmas, "strict " . join ', ', @strict; | |
1661 | } | |
a0035eb8 RH |
1662 | push @pragmas, "bytes" if $bits & 0x8; |
1663 | return @pragmas; | |
08c6f5ec RH |
1664 | } |
1665 | ||
6e90668e | 1666 | sub pp_dbstate { pp_nextstate(@_) } |
3f872cb9 | 1667 | sub pp_setstate { pp_nextstate(@_) } |
6e90668e SM |
1668 | |
1669 | sub pp_unstack { return "" } # see also leaveloop | |
1670 | ||
80e3f4ad FC |
1671 | my %feature_keywords = ( |
1672 | # keyword => 'feature', | |
1673 | state => 'state', | |
1674 | say => 'say', | |
1675 | given => 'switch', | |
1676 | when => 'switch', | |
1677 | default => 'switch', | |
e36901c8 | 1678 | break => 'switch', |
7d789282 | 1679 | evalbytes=>'evalbytes', |
84ed0108 | 1680 | __SUB__ => '__SUB__', |
838f2281 | 1681 | fc => 'fc', |
80e3f4ad FC |
1682 | ); |
1683 | ||
3ac5308a DM |
1684 | # keywords that are strong and also have a prototype |
1685 | # | |
1686 | my %strong_proto_keywords = map { $_ => 1 } qw( | |
1687 | glob | |
1688 | pos | |
1689 | prototype | |
1690 | scalar | |
1691 | study | |
1692 | undef | |
1693 | ); | |
1694 | ||
4a1ac32e FC |
1695 | sub keyword { |
1696 | my $self = shift; | |
1697 | my $name = shift; | |
1698 | return $name if $name =~ /^CORE::/; # just in case | |
80e3f4ad | 1699 | if (exists $feature_keywords{$name}) { |
223b1722 | 1700 | my $hh; |
149758b3 NC |
1701 | my $hints = $self->{hints} & $feature::hint_mask; |
1702 | if ($hints && $hints != $feature::hint_mask) { | |
1873980a | 1703 | $hh = _features_from_bundle($hints); |
223b1722 FC |
1704 | } |
1705 | elsif ($hints) { $hh = $self->{'hinthash'} } | |
7d789282 | 1706 | return "CORE::$name" |
223b1722 FC |
1707 | if !$hh |
1708 | || !$hh->{"feature_$feature_keywords{$name}"} | |
80e3f4ad | 1709 | } |
3ac5308a DM |
1710 | if ($strong_proto_keywords{$name} |
1711 | || ($name !~ /^(?:chom?p|do|exec|glob|s(?:elect|ystem))\z/ | |
1712 | && !defined eval{prototype "CORE::$name"}) | |
4a1ac32e FC |
1713 | ) { return $name } |
1714 | if ( | |
1715 | exists $self->{subs_declared}{$name} | |
1716 | or | |
1717 | exists &{"$self->{curstash}::$name"} | |
1718 | ) { | |
1719 | return "CORE::$name" | |
1720 | } | |
1721 | return $name; | |
1722 | } | |
1723 | ||
6e90668e SM |
1724 | sub baseop { |
1725 | my $self = shift; | |
9d2c6865 | 1726 | my($op, $cx, $name) = @_; |
4a1ac32e | 1727 | return $self->keyword($name); |
6e90668e SM |
1728 | } |
1729 | ||
e99ebc55 RH |
1730 | sub pp_stub { |
1731 | my $self = shift; | |
1732 | my($op, $cx, $name) = @_; | |
d989cdac | 1733 | if ($cx >= 1) { |
e99ebc55 RH |
1734 | return "()"; |
1735 | } | |
1736 | else { | |
1737 | return "();"; | |
1738 | } | |
1739 | } | |
6e90668e SM |
1740 | sub pp_wantarray { baseop(@_, "wantarray") } |
1741 | sub pp_fork { baseop(@_, "fork") } | |
3ed82cfc GS |
1742 | sub pp_wait { maybe_targmy(@_, \&baseop, "wait") } |
1743 | sub pp_getppid { maybe_targmy(@_, \&baseop, "getppid") } | |
1744 | sub pp_time { maybe_targmy(@_, \&baseop, "time") } | |
6e90668e SM |
1745 | sub pp_tms { baseop(@_, "times") } |
1746 | sub pp_ghostent { baseop(@_, "gethostent") } | |
1747 | sub pp_gnetent { baseop(@_, "getnetent") } | |
1748 | sub pp_gprotoent { baseop(@_, "getprotoent") } | |
1749 | sub pp_gservent { baseop(@_, "getservent") } | |
1750 | sub pp_ehostent { baseop(@_, "endhostent") } | |
1751 | sub pp_enetent { baseop(@_, "endnetent") } | |
1752 | sub pp_eprotoent { baseop(@_, "endprotoent") } | |
1753 | sub pp_eservent { baseop(@_, "endservent") } | |
1754 | sub pp_gpwent { baseop(@_, "getpwent") } | |
1755 | sub pp_spwent { baseop(@_, "setpwent") } | |
1756 | sub pp_epwent { baseop(@_, "endpwent") } | |
1757 | sub pp_ggrent { baseop(@_, "getgrent") } | |
1758 | sub pp_sgrent { baseop(@_, "setgrent") } | |
1759 | sub pp_egrent { baseop(@_, "endgrent") } | |
1760 | sub pp_getlogin { baseop(@_, "getlogin") } | |
1761 | ||
1762 | sub POSTFIX () { 1 } | |
1763 | ||
9d2c6865 SM |
1764 | # I couldn't think of a good short name, but this is the category of |
1765 | # symbolic unary operators with interesting precedence | |
1766 | ||
1767 | sub pfixop { | |
1768 | my $self = shift; | |
1769 | my($op, $cx, $name, $prec, $flags) = (@_, 0); | |
1770 | my $kid = $op->first; | |
1771 | $kid = $self->deparse($kid, $prec); | |
843b15cc FC |
1772 | return $self->maybe_parens(($flags & POSTFIX) |
1773 | ? "$kid$name" | |
1774 | # avoid confusion with filetests | |
1775 | : $name eq '-' | |
1776 | && $kid =~ /^[a-zA-Z](?!\w)/ | |
1777 | ? "$name($kid)" | |
1778 | : "$name$kid", | |
9d2c6865 SM |
1779 | $cx, $prec); |
1780 | } | |
1781 | ||
1782 | sub pp_preinc { pfixop(@_, "++", 23) } | |
1783 | sub pp_predec { pfixop(@_, "--", 23) } | |
3ed82cfc GS |
1784 | sub pp_postinc { maybe_targmy(@_, \&pfixop, "++", 23, POSTFIX) } |
1785 | sub pp_postdec { maybe_targmy(@_, \&pfixop, "--", 23, POSTFIX) } | |
9d2c6865 SM |
1786 | sub pp_i_preinc { pfixop(@_, "++", 23) } |
1787 | sub pp_i_predec { pfixop(@_, "--", 23) } | |
3ed82cfc GS |
1788 | sub pp_i_postinc { maybe_targmy(@_, \&pfixop, "++", 23, POSTFIX) } |
1789 | sub pp_i_postdec { maybe_targmy(@_, \&pfixop, "--", 23, POSTFIX) } | |
68cc8748 | 1790 | sub pp_complement { maybe_targmy(@_, \&pfixop, "~", 21) } |
9d2c6865 | 1791 | |
3ed82cfc GS |
1792 | sub pp_negate { maybe_targmy(@_, \&real_negate) } |
1793 | sub real_negate { | |
9d2c6865 SM |
1794 | my $self = shift; |
1795 | my($op, $cx) = @_; | |
3f872cb9 | 1796 | if ($op->first->name =~ /^(i_)?negate$/) { |
9d2c6865 SM |
1797 | # avoid --$x |
1798 | $self->pfixop($op, $cx, "-", 21.5); | |
1799 | } else { | |
1800 | $self->pfixop($op, $cx, "-", 21); | |
1801 | } | |
1802 | } | |
1803 | sub pp_i_negate { pp_negate(@_) } | |
1804 | ||
1805 | sub pp_not { | |
1806 | my $self = shift; | |
1807 | my($op, $cx) = @_; | |
1808 | if ($cx <= 4) { | |
1cabb3b3 | 1809 | $self->listop($op, $cx, "not", $op->first); |
9d2c6865 SM |
1810 | } else { |
1811 | $self->pfixop($op, $cx, "!", 21); | |
1812 | } | |
1813 | } | |
1814 | ||
6e90668e SM |
1815 | sub unop { |
1816 | my $self = shift; | |
9c56d9ea | 1817 | my($op, $cx, $name, $nollafr) = @_; |
6e90668e | 1818 | my $kid; |
9d2c6865 | 1819 | if ($op->flags & OPf_KIDS) { |
aaf643ce | 1820 | $kid = $op->first; |
1c85afce YO |
1821 | if (not $name) { |
1822 | # this deals with 'boolkeys' right now | |
1823 | return $self->deparse($kid,$cx); | |
1824 | } | |
deb20ba3 RGS |
1825 | my $builtinname = $name; |
1826 | $builtinname =~ /^CORE::/ or $builtinname = "CORE::$name"; | |
1827 | if (defined prototype($builtinname) | |
1828 | && prototype($builtinname) =~ /^;?\*/ | |
e31885a0 RH |
1829 | && $kid->name eq "rv2gv") { |
1830 | $kid = $kid->first; | |
1831 | } | |
1832 | ||
9c56d9ea FC |
1833 | if ($nollafr) { |
1834 | ($kid = $self->deparse($kid, 16)) =~ s/^\cS//; | |
1835 | return $self->maybe_parens( | |
1836 | $self->keyword($name) . " $kid", $cx, 16 | |
1837 | ); | |
1838 | } | |
9d2c6865 | 1839 | return $self->maybe_parens_unop($name, $kid, $cx); |
6e90668e | 1840 | } else { |
4d8ac5c7 FC |
1841 | return $self->maybe_parens( |
1842 | $self->keyword($name) . ($op->flags & OPf_SPECIAL ? "()" : ""), | |
1843 | $cx, 16, | |
1844 | ); | |
6e90668e | 1845 | } |
6e90668e SM |
1846 | } |
1847 | ||
3ed82cfc GS |
1848 | sub pp_chop { maybe_targmy(@_, \&unop, "chop") } |
1849 | sub pp_chomp { maybe_targmy(@_, \&unop, "chomp") } | |
1850 | sub pp_schop { maybe_targmy(@_, \&unop, "chop") } | |
1851 | sub pp_schomp { maybe_targmy(@_, \&unop, "chomp") } | |
6e90668e SM |
1852 | sub pp_defined { unop(@_, "defined") } |
1853 | sub pp_undef { unop(@_, "undef") } | |
1854 | sub pp_study { unop(@_, "study") } | |
6e90668e SM |
1855 | sub pp_ref { unop(@_, "ref") } |
1856 | sub pp_pos { maybe_local(@_, unop(@_, "pos")) } | |
1857 | ||
3ed82cfc GS |
1858 | sub pp_sin { maybe_targmy(@_, \&unop, "sin") } |
1859 | sub pp_cos { maybe_targmy(@_, \&unop, "cos") } | |
1860 | sub pp_rand { maybe_targmy(@_, \&unop, "rand") } | |
6e90668e | 1861 | sub pp_srand { unop(@_, "srand") } |
3ed82cfc GS |
1862 | sub pp_exp { maybe_targmy(@_, \&unop, "exp") } |
1863 | sub pp_log { maybe_targmy(@_, \&unop, "log") } | |
1864 | sub pp_sqrt { maybe_targmy(@_, \&unop, "sqrt") } | |
1865 | sub pp_int { maybe_targmy(@_, \&unop, "int") } | |
1866 | sub pp_hex { maybe_targmy(@_, \&unop, "hex") } | |
1867 | sub pp_oct { maybe_targmy(@_, \&unop, "oct") } | |
1868 | sub pp_abs { maybe_targmy(@_, \&unop, "abs") } | |
1869 | ||
1870 | sub pp_length { maybe_targmy(@_, \&unop, "length") } | |
1871 | sub pp_ord { maybe_targmy(@_, \&unop, "ord") } | |
1872 | sub pp_chr { maybe_targmy(@_, \&unop, "chr") } | |
6e90668e SM |
1873 | |
1874 | sub pp_each { unop(@_, "each") } | |
1875 | sub pp_values { unop(@_, "values") } | |
1876 | sub pp_keys { unop(@_, "keys") } | |
09dcfa7d | 1877 | { no strict 'refs'; *{"pp_r$_"} = *{"pp_$_"} for qw< keys each values >; } |
1c85afce YO |
1878 | sub pp_boolkeys { |
1879 | # no name because its an optimisation op that has no keyword | |
1880 | unop(@_,""); | |
1881 | } | |
644741fd NC |
1882 | sub pp_aeach { unop(@_, "each") } |
1883 | sub pp_avalues { unop(@_, "values") } | |
1884 | sub pp_akeys { unop(@_, "keys") } | |
6e90668e SM |
1885 | sub pp_pop { unop(@_, "pop") } |
1886 | sub pp_shift { unop(@_, "shift") } | |
1887 | ||
1888 | sub pp_caller { unop(@_, "caller") } | |
1889 | sub pp_reset { unop(@_, "reset") } | |
1890 | sub pp_exit { unop(@_, "exit") } | |
1891 | sub pp_prototype { unop(@_, "prototype") } | |
1892 | ||
1893 | sub pp_close { unop(@_, "close") } | |
1894 | sub pp_fileno { unop(@_, "fileno") } | |
1895 | sub pp_umask { unop(@_, "umask") } | |
6e90668e SM |
1896 | sub pp_untie { unop(@_, "untie") } |
1897 | sub pp_tied { unop(@_, "tied") } | |
1898 | sub pp_dbmclose { unop(@_, "dbmclose") } | |
1899 | sub pp_getc { unop(@_, "getc") } | |
1900 | sub pp_eof { unop(@_, "eof") } | |
1901 | sub pp_tell { unop(@_, "tell") } | |
1902 | sub pp_getsockname { unop(@_, "getsockname") } | |
1903 | sub pp_getpeername { unop(@_, "getpeername") } | |
1904 | ||
3ed82cfc GS |
1905 | sub pp_chdir { maybe_targmy(@_, \&unop, "chdir") } |
1906 | sub pp_chroot { maybe_targmy(@_, \&unop, "chroot") } | |
6e90668e | 1907 | sub pp_readlink { unop(@_, "readlink") } |
3ed82cfc | 1908 | sub pp_rmdir { maybe_targmy(@_, \&unop, "rmdir") } |
6e90668e SM |
1909 | sub pp_readdir { unop(@_, "readdir") } |
1910 | sub pp_telldir { unop(@_, "telldir") } | |
1911 | sub pp_rewinddir { unop(@_, "rewinddir") } | |
1912 | sub pp_closedir { unop(@_, "closedir") } | |
3ed82cfc | 1913 | sub pp_getpgrp { maybe_targmy(@_, \&unop, "getpgrp") } |
6e90668e SM |
1914 | sub pp_localtime { unop(@_, "localtime") } |
1915 | sub pp_gmtime { unop(@_, "gmtime") } | |
1916 | sub pp_alarm { unop(@_, "alarm") } | |
3ed82cfc | 1917 | sub pp_sleep { maybe_targmy(@_, \&unop, "sleep") } |
6e90668e | 1918 | |
94bb57f9 | 1919 | sub pp_dofile { |
9c56d9ea | 1920 | my $code = unop(@_, "do", 1); # llafr does not apply |
8b46c09b | 1921 | if ($code =~ s/^((?:CORE::)?do) \{/$1({/) { $code .= ')' } |
94bb57f9 FC |
1922 | $code; |
1923 | } | |
7d789282 FC |
1924 | sub pp_entereval { |
1925 | unop( | |
1926 | @_, | |
1927 | $_[1]->private & OPpEVAL_BYTES ? $_[0]->keyword('evalbytes') : "eval" | |
1928 | ) | |
1929 | } | |
6e90668e SM |
1930 | |
1931 | sub pp_ghbyname { unop(@_, "gethostbyname") } | |
1932 | sub pp_gnbyname { unop(@_, "getnetbyname") } | |
1933 | sub pp_gpbyname { unop(@_, "getprotobyname") } | |
1934 | sub pp_shostent { unop(@_, "sethostent") } | |
1935 | sub pp_snetent { unop(@_, "setnetent") } | |
1936 | sub pp_sprotoent { unop(@_, "setprotoent") } | |
1937 | sub pp_sservent { unop(@_, "setservent") } | |
1938 | sub pp_gpwnam { unop(@_, "getpwnam") } | |
1939 | sub pp_gpwuid { unop(@_, "getpwuid") } | |
1940 | sub pp_ggrnam { unop(@_, "getgrnam") } | |
1941 | sub pp_ggrgid { unop(@_, "getgrgid") } | |
1942 | ||
1943 | sub pp_lock { unop(@_, "lock") } | |
1944 | ||
0d863452 | 1945 | sub pp_continue { unop(@_, "continue"); } |
c08f093b | 1946 | sub pp_break { unop(@_, "break"); } |
0d863452 RH |
1947 | |
1948 | sub givwhen { | |
1949 | my $self = shift; | |
1950 | my($op, $cx, $givwhen) = @_; | |
1951 | ||
1952 | my $enterop = $op->first; | |
1953 | my ($head, $block); | |
1954 | if ($enterop->flags & OPf_SPECIAL) { | |
80e3f4ad | 1955 | $head = $self->keyword("default"); |
0d863452 RH |
1956 | $block = $self->deparse($enterop->first, 0); |
1957 | } | |
1958 | else { | |
1959 | my $cond = $enterop->first; | |
1960 | my $cond_str = $self->deparse($cond, 1); | |
1961 | $head = "$givwhen ($cond_str)"; | |
1962 | $block = $self->deparse($cond->sibling, 0); | |
1963 | } | |
1964 | ||
1965 | return "$head {\n". | |
1966 | "\t$block\n". | |
1967 | "\b}\cK"; | |
1968 | } | |
1969 | ||
80e3f4ad FC |
1970 | sub pp_leavegiven { givwhen(@_, $_[0]->keyword("given")); } |
1971 | sub pp_leavewhen { givwhen(@_, $_[0]->keyword("when")); } | |
0d863452 | 1972 | |
6e90668e SM |
1973 | sub pp_exists { |
1974 | my $self = shift; | |
9d2c6865 | 1975 | my($op, $cx) = @_; |
34a48b4b RH |
1976 | my $arg; |
1977 | if ($op->private & OPpEXISTS_SUB) { | |
1978 | # Checking for the existence of a subroutine | |
1979 | return $self->maybe_parens_func("exists", | |
1980 | $self->pp_rv2cv($op->first, 16), $cx, 16); | |
1981 | } | |
1982 | if ($op->flags & OPf_SPECIAL) { | |
1983 | # Array element, not hash element | |
1984 | return $self->maybe_parens_func("exists", | |
1985 | $self->pp_aelem($op->first, 16), $cx, 16); | |
1986 | } | |
9d2c6865 SM |
1987 | return $self->maybe_parens_func("exists", $self->pp_helem($op->first, 16), |
1988 | $cx, 16); | |
6e90668e SM |
1989 | } |
1990 | ||
6e90668e SM |
1991 | sub pp_delete { |
1992 | my $self = shift; | |
9d2c6865 | 1993 | my($op, $cx) = @_; |
6e90668e SM |
1994 | my $arg; |
1995 | if ($op->private & OPpSLICE) { | |
34a48b4b RH |
1996 | if ($op->flags & OPf_SPECIAL) { |
1997 | # Deleting from an array, not a hash | |
1998 | return $self->maybe_parens_func("delete", | |
1999 | $self->pp_aslice($op->first, 16), | |
2000 | $cx, 16); | |
2001 | } | |
9d2c6865 SM |
2002 | return $self->maybe_parens_func("delete", |
2003 | $self->pp_hslice($op->first, 16), | |
2004 | $cx, 16); | |
6e90668e | 2005 | } else { |
34a48b4b RH |
2006 | if ($op->flags & OPf_SPECIAL) { |
2007 | # Deleting from an array, not a hash | |
2008 | return $self->maybe_parens_func("delete", | |
2009 | $self->pp_aelem($op->first, 16), | |
2010 | $cx, 16); | |
2011 | } | |
9d2c6865 SM |
2012 | return $self->maybe_parens_func("delete", |
2013 | $self->pp_helem($op->first, 16), | |
2014 | $cx, 16); | |
6e90668e | 2015 | } |
6e90668e SM |
2016 | } |
2017 | ||
6e90668e SM |
2018 | sub pp_require { |
2019 | my $self = shift; | |
9d2c6865 | 2020 | my($op, $cx) = @_; |
d5889722 | 2021 | my $opname = $op->flags & OPf_SPECIAL ? 'CORE::require' : 'require'; |
3f872cb9 | 2022 | if (class($op) eq "UNOP" and $op->first->name eq "const" |
4c1f658f | 2023 | and $op->first->private & OPpCONST_BARE) |
6e90668e | 2024 | { |
18228111 | 2025 | my $name = $self->const_sv($op->first)->PV; |
6e90668e SM |
2026 | $name =~ s[/][::]g; |
2027 | $name =~ s/\.pm//g; | |
41df74e3 | 2028 | return $self->maybe_parens("$opname $name", $cx, 16); |
6e90668e | 2029 | } else { |
41df74e3 FC |
2030 | $self->unop( |
2031 | $op, $cx, | |
c75b4828 FC |
2032 | $op->first->name eq 'const' |
2033 | && $op->first->private & OPpCONST_NOVER | |
2034 | ? "no" | |
2035 | : $opname, | |
41df74e3 FC |
2036 | 1, # llafr does not apply |
2037 | ); | |
6e90668e SM |
2038 | } |
2039 | } | |
2040 | ||
d989cdac | 2041 | sub pp_scalar { |
9d2c6865 | 2042 | my $self = shift; |
d9002312 | 2043 | my($op, $cx) = @_; |
9d2c6865 SM |
2044 | my $kid = $op->first; |
2045 | if (not null $kid->sibling) { | |
2046 | # XXX Was a here-doc | |
2047 | return $self->dquote($op); | |
2048 | } | |
2049 | $self->unop(@_, "scalar"); | |
2050 | } | |
2051 | ||
2052 | ||
6e90668e SM |
2053 | sub padval { |
2054 | my $self = shift; | |
2055 | my $targ = shift; | |
d989cdac | 2056 | return $self->{'curcv'}->PADLIST->ARRAYelt(1)->ARRAYelt($targ); |
6e90668e SM |
2057 | } |
2058 | ||
78c72037 NC |
2059 | sub anon_hash_or_list { |
2060 | my $self = shift; | |
d9002312 | 2061 | my($op, $cx) = @_; |
78c72037 NC |
2062 | |
2063 | my($pre, $post) = @{{"anonlist" => ["[","]"], | |
2064 | "anonhash" => ["{","}"]}->{$op->name}}; | |
2065 | my($expr, @exprs); | |
2066 | $op = $op->first->sibling; # skip pushmark | |
2067 | for (; !null($op); $op = $op->sibling) { | |
2068 | $expr = $self->deparse($op, 6); | |
2069 | push @exprs, $expr; | |
2070 | } | |
d9002312 SM |
2071 | if ($pre eq "{" and $cx < 1) { |
2072 | # Disambiguate that it's not a block | |
2073 | $pre = "+{"; | |
2074 | } | |
78c72037 NC |
2075 | return $pre . join(", ", @exprs) . $post; |
2076 | } | |
2077 | ||
2078 | sub pp_anonlist { | |
d9002312 SM |
2079 | my $self = shift; |
2080 | my ($op, $cx) = @_; | |
78c72037 | 2081 | if ($op->flags & OPf_SPECIAL) { |
d9002312 | 2082 | return $self->anon_hash_or_list($op, $cx); |
78c72037 NC |
2083 | } |
2084 | warn "Unexpected op pp_" . $op->name() . " without OPf_SPECIAL"; | |
2085 | return 'XXX'; | |
2086 | } | |
2087 | ||
2088 | *pp_anonhash = \&pp_anonlist; | |
2089 | ||
6e90668e SM |
2090 | sub pp_refgen { |
2091 | my $self = shift; | |
9d2c6865 | 2092 | my($op, $cx) = @_; |
6e90668e | 2093 | my $kid = $op->first; |
3f872cb9 | 2094 | if ($kid->name eq "null") { |
6e90668e | 2095 | $kid = $kid->first; |
35925e80 | 2096 | if (!null($kid->sibling) and |
3f872cb9 | 2097 | $kid->sibling->name eq "anoncode") { |
09d856fb | 2098 | return $self->e_anoncode({ code => $self->padval($kid->sibling->targ) }); |
3f872cb9 GS |
2099 | } elsif ($kid->name eq "pushmark") { |
2100 | my $sib_name = $kid->sibling->name; | |
2101 | if ($sib_name =~ /^(pad|rv2)[ah]v$/ | |
c8c62db7 AD |
2102 | and not $kid->sibling->flags & OPf_REF) |
2103 | { | |
2104 | # The @a in \(@a) isn't in ref context, but only when the | |
2105 | # parens are there. | |
127212b2 | 2106 | return "\\(" . $self->pp_list($op->first) . ")"; |
3f872cb9 | 2107 | } elsif ($sib_name eq 'entersub') { |
c8c62db7 AD |
2108 | my $text = $self->deparse($kid->sibling, 1); |
2109 | # Always show parens for \(&func()), but only with -p otherwise | |
2110 | $text = "($text)" if $self->{'parens'} | |
2111 | or $kid->sibling->private & OPpENTERSUB_AMPER; | |
2112 | return "\\$text"; | |
2113 | } | |
2114 | } | |
6e90668e | 2115 | } |
9d2c6865 | 2116 | $self->pfixop($op, $cx, "\\", 20); |
6e90668e SM |
2117 | } |
2118 | ||
09d856fb CK |
2119 | sub e_anoncode { |
2120 | my ($self, $info) = @_; | |
2121 | my $text = $self->deparse_sub($info->{code}); | |
2122 | return "sub " . $text; | |
2123 | } | |
2124 | ||
6e90668e SM |
2125 | sub pp_srefgen { pp_refgen(@_) } |
2126 | ||
2127 | sub pp_readline { | |
2128 | my $self = shift; | |
9d2c6865 | 2129 | my($op, $cx) = @_; |
6e90668e | 2130 | my $kid = $op->first; |
3f872cb9 | 2131 | $kid = $kid->first if $kid->name eq "rv2gv"; # <$fh> |
e31885a0 RH |
2132 | return "<" . $self->deparse($kid, 1) . ">" if is_scalar($kid); |
2133 | return $self->unop($op, $cx, "readline"); | |
6e90668e SM |
2134 | } |
2135 | ||
ad8caead RGS |
2136 | sub pp_rcatline { |
2137 | my $self = shift; | |
2138 | my($op) = @_; | |
d989cdac | 2139 | return "<" . $self->gv_name($self->gv_or_padgv($op)) . ">"; |
ad8caead RGS |
2140 | } |
2141 | ||
bd0865ec GS |
2142 | # Unary operators that can occur as pseudo-listops inside double quotes |
2143 | sub dq_unop { | |
2144 | my $self = shift; | |
2145 | my($op, $cx, $name, $prec, $flags) = (@_, 0, 0); | |
2146 | my $kid; | |
2147 | if ($op->flags & OPf_KIDS) { | |
2148 | $kid = $op->first; | |
2149 | # If there's more than one kid, the first is an ex-pushmark. | |
2150 | $kid = $kid->sibling if not null $kid->sibling; | |
2151 | return $self->maybe_parens_unop($name, $kid, $cx); | |
2152 | } else { | |
d989cdac | 2153 | return $name . ($op->flags & OPf_SPECIAL ? "()" : ""); |
bd0865ec GS |
2154 | } |
2155 | } | |
2156 | ||
2157 | sub pp_ucfirst { dq_unop(@_, "ucfirst") } | |
2158 | sub pp_lcfirst { dq_unop(@_, "lcfirst") } | |
2159 | sub pp_uc { dq_unop(@_, "uc") } | |
2160 | sub pp_lc { dq_unop(@_, "lc") } | |
3ed82cfc | 2161 | sub pp_quotemeta { maybe_targmy(@_, \&dq_unop, "quotemeta") } |
838f2281 | 2162 | sub pp_fc { dq_unop(@_, "fc") } |
bd0865ec | 2163 | |
6e90668e SM |
2164 | sub loopex { |
2165 | my $self = shift; | |
9d2c6865 | 2166 | my ($op, $cx, $name) = @_; |
6e90668e | 2167 | if (class($op) eq "PVOP") { |
41df74e3 | 2168 | $name .= " " . $op->pv; |
9d2c6865 | 2169 | } elsif (class($op) eq "OP") { |
41df74e3 | 2170 | # no-op |
6e90668e | 2171 | } elsif (class($op) eq "UNOP") { |
1eb0b7be | 2172 | (my $kid = $self->deparse($op->first, 7)) =~ s/^\cS//; |
41df74e3 | 2173 | $name .= " $kid"; |
6e90668e | 2174 | } |
1eb0b7be | 2175 | return $self->maybe_parens($name, $cx, 7); |
6e90668e SM |
2176 | } |
2177 | ||
2178 | sub pp_last { loopex(@_, "last") } | |
2179 | sub pp_next { loopex(@_, "next") } | |
2180 | sub pp_redo { loopex(@_, "redo") } | |
2181 | sub pp_goto { loopex(@_, "goto") } | |
266da325 | 2182 | sub pp_dump { loopex(@_, "CORE::dump") } |
6e90668e SM |
2183 | |
2184 | sub ftst { | |
2185 | my $self = shift; | |
9d2c6865 | 2186 | my($op, $cx, $name) = @_; |
6e90668e | 2187 | if (class($op) eq "UNOP") { |
e38ccfd9 | 2188 | # Genuine '-X' filetests are exempt from the LLAFR, but not |
5830412d FC |
2189 | # l?stat() |
2190 | if ($name =~ /^-/) { | |
2191 | (my $kid = $self->deparse($op->first, 16)) =~ s/^\cS//; | |
2192 | return $self->maybe_parens("$name $kid", $cx, 16); | |
2193 | } | |
9d2c6865 | 2194 | return $self->maybe_parens_unop($name, $op->first, $cx); |
d989cdac | 2195 | } elsif (class($op) =~ /^(SV|PAD)OP$/) { |
9d2c6865 | 2196 | return $self->maybe_parens_func($name, $self->pp_gv($op, 1), $cx, 16); |
6e90668e | 2197 | } else { # I don't think baseop filetests ever survive ck_ftst, but... |
9d2c6865 | 2198 | return $name; |
6e90668e | 2199 | } |
6e90668e SM |
2200 | } |
2201 | ||
d989cdac SM |
2202 | sub pp_lstat { ftst(@_, "lstat") } |
2203 | sub pp_stat { ftst(@_, "stat") } | |
2204 | sub pp_ftrread { ftst(@_, "-R") } | |
6e90668e | 2205 | sub pp_ftrwrite { ftst(@_, "-W") } |
d989cdac SM |
2206 | sub pp_ftrexec { ftst(@_, "-X") } |
2207 | sub pp_fteread { ftst(@_, "-r") } | |
e31885a0 | 2208 | sub pp_ftewrite { ftst(@_, "-w") } |
d989cdac SM |
2209 | sub pp_fteexec { ftst(@_, "-x") } |
2210 | sub pp_ftis { ftst(@_, "-e") } | |
6e90668e SM |
2211 | sub pp_fteowned { ftst(@_, "-O") } |
2212 | sub pp_ftrowned { ftst(@_, "-o") } | |
d989cdac SM |
2213 | sub pp_ftzero { ftst(@_, "-z") } |
2214 | sub pp_ftsize { ftst(@_, "-s") } | |
2215 | sub pp_ftmtime { ftst(@_, "-M") } | |
2216 | sub pp_ftatime { ftst(@_, "-A") } | |
2217 | sub pp_ftctime { ftst(@_, "-C") } | |
2218 | sub pp_ftsock { ftst(@_, "-S") } | |
2219 | sub pp_ftchr { ftst(@_, "-c") } | |
2220 | sub pp_ftblk { ftst(@_, "-b") } | |
2221 | sub pp_ftfile { ftst(@_, "-f") } | |
2222 | sub pp_ftdir { ftst(@_, "-d") } | |
2223 | sub pp_ftpipe { ftst(@_, "-p") } | |
2224 | sub pp_ftlink { ftst(@_, "-l") } | |
2225 | sub pp_ftsuid { ftst(@_, "-u") } | |
2226 | sub pp_ftsgid { ftst(@_, "-g") } | |
2227 | sub pp_ftsvtx { ftst(@_, "-k") } | |
2228 | sub pp_fttty { ftst(@_, "-t") } | |
2229 | sub pp_fttext { ftst(@_, "-T") } | |
6e90668e SM |
2230 | sub pp_ftbinary { ftst(@_, "-B") } |
2231 | ||
a798dbf2 | 2232 | sub SWAP_CHILDREN () { 1 } |
6e90668e | 2233 | sub ASSIGN () { 2 } # has OP= variant |
7013e6ae | 2234 | sub LIST_CONTEXT () { 4 } # Assignment is in list context |
6e90668e | 2235 | |
9d2c6865 SM |
2236 | my(%left, %right); |
2237 | ||
2238 | sub assoc_class { | |
2239 | my $op = shift; | |
3f872cb9 GS |
2240 | my $name = $op->name; |
2241 | if ($name eq "concat" and $op->first->name eq "concat") { | |
e38ccfd9 | 2242 | # avoid spurious '=' -- see comment in pp_concat |
3f872cb9 | 2243 | return "concat"; |
9d2c6865 | 2244 | } |
3f872cb9 GS |
2245 | if ($name eq "null" and class($op) eq "UNOP" |
2246 | and $op->first->name =~ /^(and|x?or)$/ | |
9d2c6865 SM |
2247 | and null $op->first->sibling) |
2248 | { | |
2249 | # Like all conditional constructs, OP_ANDs and OP_ORs are topped | |
2250 | # with a null that's used as the common end point of the two | |
2251 | # flows of control. For precedence purposes, ignore it. | |
2252 | # (COND_EXPRs have these too, but we don't bother with | |
2253 | # their associativity). | |
2254 | return assoc_class($op->first); | |
2255 | } | |
2256 | return $name . ($op->flags & OPf_STACKED ? "=" : ""); | |
2257 | } | |
2258 | ||
e38ccfd9 | 2259 | # Left associative operators, like '+', for which |
9d2c6865 SM |
2260 | # $a + $b + $c is equivalent to ($a + $b) + $c |
2261 | ||
2262 | BEGIN { | |
3f872cb9 GS |
2263 | %left = ('multiply' => 19, 'i_multiply' => 19, |
2264 | 'divide' => 19, 'i_divide' => 19, | |
2265 | 'modulo' => 19, 'i_modulo' => 19, | |
2266 | 'repeat' => 19, | |
2267 | 'add' => 18, 'i_add' => 18, | |
2268 | 'subtract' => 18, 'i_subtract' => 18, | |
2269 | 'concat' => 18, | |
2270 | 'left_shift' => 17, 'right_shift' => 17, | |
2271 | 'bit_and' => 13, | |
2272 | 'bit_or' => 12, 'bit_xor' => 12, | |
2273 | 'and' => 3, | |
2274 | 'or' => 2, 'xor' => 2, | |
9d2c6865 SM |
2275 | ); |
2276 | } | |
2277 | ||
2278 | sub deparse_binop_left { | |
2279 | my $self = shift; | |
2280 | my($op, $left, $prec) = @_; | |
58231d39 | 2281 | if ($left{assoc_class($op)} && $left{assoc_class($left)} |
9d2c6865 SM |
2282 | and $left{assoc_class($op)} == $left{assoc_class($left)}) |
2283 | { | |
2284 | return $self->deparse($left, $prec - .00001); | |
2285 | } else { | |
2286 | return $self->deparse($left, $prec); | |
2287 | } | |
2288 | } | |
2289 | ||
e38ccfd9 | 2290 | # Right associative operators, like '=', for which |
9d2c6865 SM |
2291 | # $a = $b = $c is equivalent to $a = ($b = $c) |
2292 | ||
2293 | BEGIN { | |
3f872cb9 GS |
2294 | %right = ('pow' => 22, |
2295 | 'sassign=' => 7, 'aassign=' => 7, | |
2296 | 'multiply=' => 7, 'i_multiply=' => 7, | |
2297 | 'divide=' => 7, 'i_divide=' => 7, | |
2298 | 'modulo=' => 7, 'i_modulo=' => 7, | |
2299 | 'repeat=' => 7, | |
2300 | 'add=' => 7, 'i_add=' => 7, | |
2301 | 'subtract=' => 7, 'i_subtract=' => 7, | |
2302 | 'concat=' => 7, | |
2303 | 'left_shift=' => 7, 'right_shift=' => 7, | |
2304 | 'bit_and=' => 7, | |
2305 | 'bit_or=' => 7, 'bit_xor=' => 7, | |
2306 | 'andassign' => 7, | |
2307 | 'orassign' => 7, | |
9d2c6865 SM |
2308 | ); |
2309 | } | |
2310 | ||
2311 | sub deparse_binop_right { | |
2312 | my $self = shift; | |
2313 | my($op, $right, $prec) = @_; | |
58231d39 | 2314 | if ($right{assoc_class($op)} && $right{assoc_class($right)} |
9d2c6865 SM |
2315 | and $right{assoc_class($op)} == $right{assoc_class($right)}) |
2316 | { | |
2317 | return $self->deparse($right, $prec - .00001); | |
2318 | } else { | |
2319 | return $self->deparse($right, $prec); | |
2320 | } | |
2321 | } | |
2322 | ||
a798dbf2 | 2323 | sub binop { |
6e90668e | 2324 | my $self = shift; |
9d2c6865 | 2325 | my ($op, $cx, $opname, $prec, $flags) = (@_, 0); |
a798dbf2 MB |
2326 | my $left = $op->first; |
2327 | my $right = $op->last; | |
9d2c6865 SM |
2328 | my $eq = ""; |
2329 | if ($op->flags & OPf_STACKED && $flags & ASSIGN) { | |
2330 | $eq = "="; | |
2331 | $prec = 7; | |
2332 | } | |
a798dbf2 MB |
2333 | if ($flags & SWAP_CHILDREN) { |
2334 | ($left, $right) = ($right, $left); | |
2335 | } | |
9d2c6865 | 2336 | $left = $self->deparse_binop_left($op, $left, $prec); |
90c0eb26 RH |
2337 | $left = "($left)" if $flags & LIST_CONTEXT |
2338 | && $left !~ /^(my|our|local|)[\@\(]/; | |
9d2c6865 SM |
2339 | $right = $self->deparse_binop_right($op, $right, $prec); |
2340 | return $self->maybe_parens("$left $opname$eq $right", $cx, $prec); | |
2341 | } | |
2342 | ||
3ed82cfc GS |
2343 | sub pp_add { maybe_targmy(@_, \&binop, "+", 18, ASSIGN) } |
2344 | sub pp_multiply { maybe_targmy(@_, \&binop, "*", 19, ASSIGN) } | |
2345 | sub pp_subtract { maybe_targmy(@_, \&binop, "-",18, ASSIGN) } | |
2346 | sub pp_divide { maybe_targmy(@_, \&binop, "/", 19, ASSIGN) } | |
2347 | sub pp_modulo { maybe_targmy(@_, \&binop, "%", 19, ASSIGN) } | |
2348 | sub pp_i_add { maybe_targmy(@_, \&binop, "+", 18, ASSIGN) } | |
2349 | sub pp_i_multiply { maybe_targmy(@_, \&binop, "*", 19, ASSIGN) } | |
2350 | sub pp_i_subtract { maybe_targmy(@_, \&binop, "-", 18, ASSIGN) } | |
2351 | sub pp_i_divide { maybe_targmy(@_, \&binop, "/", 19, ASSIGN) } | |
2352 | sub pp_i_modulo { maybe_targmy(@_, \&binop, "%", 19, ASSIGN) } | |
2353 | sub pp_pow { maybe_targmy(@_, \&binop, "**", 22, ASSIGN) } | |
2354 | ||
2355 | sub pp_left_shift { maybe_targmy(@_, \&binop, "<<", 17, ASSIGN) } | |
2356 | sub pp_right_shift { maybe_targmy(@_, \&binop, ">>", 17, ASSIGN) } | |
2357 | sub pp_bit_and { maybe_targmy(@_, \&binop, "&", 13, ASSIGN) } | |
2358 | sub pp_bit_or { maybe_targmy(@_, \&binop, "|", 12, ASSIGN) } | |
2359 | sub pp_bit_xor { maybe_targmy(@_, \&binop, "^", 12, ASSIGN) } | |
9d2c6865 SM |
2360 | |
2361 | sub pp_eq { binop(@_, "==", 14) } | |
2362 | sub pp_ne { binop(@_, "!=", 14) } | |
2363 | sub pp_lt { binop(@_, "<", 15) } | |
2364 | sub pp_gt { binop(@_, ">", 15) } | |
2365 | sub pp_ge { binop(@_, ">=", 15) } | |
2366 | sub pp_le { binop(@_, "<=", 15) } | |
2367 | sub pp_ncmp { binop(@_, "<=>", 14) } | |
2368 | sub pp_i_eq { binop(@_, "==", 14) } | |
2369 | sub pp_i_ne { binop(@_, "!=", 14) } | |
2370 | sub pp_i_lt { binop(@_, "<", 15) } | |
2371 | sub pp_i_gt { binop(@_, ">", 15) } | |
2372 | sub pp_i_ge { binop(@_, ">=", 15) } | |
2373 | sub pp_i_le { binop(@_, "<=", 15) } | |
2374 | sub pp_i_ncmp { binop(@_, "<=>", 14) } | |
2375 | ||
2376 | sub pp_seq { binop(@_, "eq", 14) } | |
2377 | sub pp_sne { binop(@_, "ne", 14) } | |
2378 | sub pp_slt { binop(@_, "lt", 15) } | |
2379 | sub pp_sgt { binop(@_, "gt", 15) } | |
2380 | sub pp_sge { binop(@_, "ge", 15) } | |
2381 | sub pp_sle { binop(@_, "le", 15) } | |
2382 | sub pp_scmp { binop(@_, "cmp", 14) } | |
2383 | ||
2384 | sub pp_sassign { binop(@_, "=", 7, SWAP_CHILDREN) } | |
7013e6ae | 2385 | sub pp_aassign { binop(@_, "=", 7, SWAP_CHILDREN | LIST_CONTEXT) } |
6e90668e | 2386 | |
0d863452 RH |
2387 | sub pp_smartmatch { |
2388 | my ($self, $op, $cx) = @_; | |
2389 | if ($op->flags & OPf_SPECIAL) { | |
9210de83 | 2390 | return $self->deparse($op->last, $cx); |
0d863452 RH |
2391 | } |
2392 | else { | |
2393 | binop(@_, "~~", 14); | |
2394 | } | |
2395 | } | |
2396 | ||
e38ccfd9 | 2397 | # '.' is special because concats-of-concats are optimized to save copying |
6e90668e | 2398 | # by making all but the first concat stacked. The effect is as if the |
e38ccfd9 | 2399 | # programmer had written '($a . $b) .= $c', except legal. |
3ed82cfc GS |
2400 | sub pp_concat { maybe_targmy(@_, \&real_concat) } |
2401 | sub real_concat { | |
6e90668e | 2402 | my $self = shift; |
9d2c6865 | 2403 | my($op, $cx) = @_; |
6e90668e SM |
2404 | my $left = $op->first; |
2405 | my $right = $op->last; | |
2406 | my $eq = ""; | |
9d2c6865 | 2407 | my $prec = 18; |
3f872cb9 | 2408 | if ($op->flags & OPf_STACKED and $op->first->name ne "concat") { |
6e90668e | 2409 | $eq = "="; |
9d2c6865 | 2410 | $prec = 7; |
6e90668e | 2411 | } |
9d2c6865 SM |
2412 | $left = $self->deparse_binop_left($op, $left, $prec); |
2413 | $right = $self->deparse_binop_right($op, $right, $prec); | |
2414 | return $self->maybe_parens("$left .$eq $right", $cx, $prec); | |
6e90668e SM |
2415 | } |
2416 | ||
e38ccfd9 | 2417 | # 'x' is weird when the left arg is a list |
6e90668e SM |
2418 | sub pp_repeat { |
2419 | my $self = shift; | |
9d2c6865 | 2420 | my($op, $cx) = @_; |
6e90668e SM |
2421 | my $left = $op->first; |
2422 | my $right = $op->last; | |
9d2c6865 SM |
2423 | my $eq = ""; |
2424 | my $prec = 19; | |
2425 | if ($op->flags & OPf_STACKED) { | |
2426 | $eq = "="; | |
2427 | $prec = 7; | |
2428 | } | |
6e90668e SM |
2429 | if (null($right)) { # list repeat; count is inside left-side ex-list |
2430 | my $kid = $left->first->sibling; # skip pushmark | |
2431 | my @exprs; | |
2432 | for (; !null($kid->sibling); $kid = $kid->sibling) { | |
9d2c6865 | 2433 | push @exprs, $self->deparse($kid, 6); |
6e90668e SM |
2434 | } |
2435 | $right = $kid; | |
2436 | $left = "(" . join(", ", @exprs). ")"; | |
2437 | } else { | |
9d2c6865 | 2438 | $left = $self->deparse_binop_left($op, $left, $prec); |
6e90668e | 2439 | } |
9d2c6865 SM |
2440 | $right = $self->deparse_binop_right($op, $right, $prec); |
2441 | return $self->maybe_parens("$left x$eq $right", $cx, $prec); | |
6e90668e SM |
2442 | } |
2443 | ||
2444 | sub range { | |
2445 | my $self = shift; | |
9d2c6865 | 2446 | my ($op, $cx, $type) = @_; |
6e90668e SM |
2447 | my $left = $op->first; |
2448 | my $right = $left->sibling; | |
9d2c6865 SM |
2449 | $left = $self->deparse($left, 9); |
2450 | $right = $self->deparse($right, 9); | |
2451 | return $self->maybe_parens("$left $type $right", $cx, 9); | |
6e90668e SM |
2452 | } |
2453 | ||
2454 | sub pp_flop { | |
2455 | my $self = shift; | |
9d2c6865 | 2456 | my($op, $cx) = @_; |
6e90668e SM |
2457 | my $flip = $op->first; |
2458 | my $type = ($flip->flags & OPf_SPECIAL) ? "..." : ".."; | |
9d2c6865 | 2459 | return $self->range($flip->first, $cx, $type); |
6e90668e SM |
2460 | } |
2461 | ||
2462 | # one-line while/until is handled in pp_leave | |
2463 | ||
2464 | sub logop { | |
2465 | my $self = shift; | |
9d2c6865 | 2466 | my ($op, $cx, $lowop, $lowprec, $highop, $highprec, $blockname) = @_; |
6e90668e SM |
2467 | my $left = $op->first; |
2468 | my $right = $op->first->sibling; | |
d989cdac | 2469 | if ($cx < 1 and is_scope($right) and $blockname |
58cccf98 SM |
2470 | and $self->{'expand'} < 7) |
2471 | { # if ($a) {$b} | |
9d2c6865 SM |
2472 | $left = $self->deparse($left, 1); |
2473 | $right = $self->deparse($right, 0); | |
2474 | return "$blockname ($left) {\n\t$right\n\b}\cK"; | |
d989cdac | 2475 | } elsif ($cx < 1 and $blockname and not $self->{'parens'} |
58cccf98 | 2476 | and $self->{'expand'} < 7) { # $b if $a |
9d2c6865 SM |
2477 | $right = $self->deparse($right, 1); |
2478 | $left = $self->deparse($left, 1); | |
2479 | return "$right $blockname $left"; | |
2480 | } elsif ($cx > $lowprec and $highop) { # $a && $b | |
2481 | $left = $self->deparse_binop_left($op, $left, $highprec); | |
2482 | $right = $self->deparse_binop_right($op, $right, $highprec); | |
2483 | return $self->maybe_parens("$left $highop $right", $cx, $highprec); | |
2484 | } else { # $a and $b | |
2485 | $left = $self->deparse_binop_left($op, $left, $lowprec); | |
2486 | $right = $self->deparse_binop_right($op, $right, $lowprec); | |
d989cdac | 2487 | return $self->maybe_parens("$left $lowop $right", $cx, $lowprec); |
9d2c6865 SM |
2488 | } |
2489 | } | |
2490 | ||
2491 | sub pp_and { logop(@_, "and", 3, "&&", 11, "if") } | |
f4a44678 | 2492 | sub pp_or { logop(@_, "or", 2, "||", 10, "unless") } |
5b99f273 | 2493 | sub pp_dor { logop(@_, "//", 10) } |
3ed82cfc GS |
2494 | |
2495 | # xor is syntactically a logop, but it's really a binop (contrary to | |
2496 | # old versions of opcode.pl). Syntax is what matters here. | |
9d2c6865 | 2497 | sub pp_xor { logop(@_, "xor", 2, "", 0, "") } |
6e90668e SM |
2498 | |
2499 | sub logassignop { | |
2500 | my $self = shift; | |
9d2c6865 | 2501 | my ($op, $cx, $opname) = @_; |
6e90668e SM |
2502 | my $left = $op->first; |
2503 | my $right = $op->first->sibling->first; # skip sassign | |
9d2c6865 SM |
2504 | $left = $self->deparse($left, 7); |
2505 | $right = $self->deparse($right, 7); | |
2506 | return $self->maybe_parens("$left $opname $right", $cx, 7); | |
a798dbf2 MB |
2507 | } |
2508 | ||
6e90668e | 2509 | sub pp_andassign { logassignop(@_, "&&=") } |
c963b151 BD |
2510 | sub pp_orassign { logassignop(@_, "||=") } |
2511 | sub pp_dorassign { logassignop(@_, "//=") } | |
6e90668e | 2512 | |
b89b7257 FC |
2513 | sub rv2gv_or_string { |
2514 | my($self,$op) = @_; | |
2515 | if ($op->name eq "gv") { # could be open("open") or open("###") | |
be6cf5cf | 2516 | my($name,$quoted) = |
1db94eeb | 2517 | $self->stash_variable_name("", $self->gv_or_padgv($op)); |
be6cf5cf | 2518 | $quoted ? $name : "*$name"; |
b89b7257 FC |
2519 | } |
2520 | else { | |
2521 | $self->deparse($op, 6); | |
2522 | } | |
2523 | } | |
2524 | ||
6e90668e SM |
2525 | sub listop { |
2526 | my $self = shift; | |
9c56d9ea | 2527 | my($op, $cx, $name, $kid, $nollafr) = @_; |
9d2c6865 SM |
2528 | my(@exprs); |
2529 | my $parens = ($cx >= 5) || $self->{'parens'}; | |
24fcb59f | 2530 | $kid ||= $op->first->sibling; |
4d8ac5c7 FC |
2531 | # If there are no arguments, add final parentheses (or parenthesize the |
2532 | # whole thing if the llafr does not apply) to account for cases like | |
2533 | # (return)+1 or setpgrp()+1. When the llafr does not apply, we use a | |
2534 | # precedence of 6 (< comma), as "return, 1" does not need parentheses. | |
2535 | if (null $kid) { | |
2536 | return $nollafr | |
2537 | ? $self->maybe_parens($self->keyword($name), $cx, 7) | |
2538 | : $self->keyword($name) . '()' x (7 < $cx); | |
2539 | } | |
e31885a0 | 2540 | my $first; |
fb725297 | 2541 | $name = "socketpair" if $name eq "sockpair"; |
4a1ac32e | 2542 | my $fullname = $self->keyword($name); |
b72c97e8 | 2543 | my $proto = prototype("CORE::$name"); |
bc1cc2c3 DM |
2544 | if ( |
2545 | ( (defined $proto && $proto =~ /^;?\*/) | |
2546 | || $name eq 'select' # select(F) doesn't have a proto | |
2547 | ) | |
2548 | && $kid->name eq "rv2gv" | |
2549 | && !($kid->private & OPpLVAL_INTRO) | |
2550 | ) { | |
b89b7257 | 2551 | $first = $self->rv2gv_or_string($kid->first); |
e31885a0 RH |
2552 | } |
2553 | else { | |
2554 | $first = $self->deparse($kid, 6); | |
2555 | } | |
e99ebc55 | 2556 | if ($name eq "chmod" && $first =~ /^\d+$/) { |
a0035eb8 | 2557 | $first = sprintf("%#o", $first); |
e99ebc55 | 2558 | } |
9c56d9ea FC |
2559 | $first = "+$first" |
2560 | if not $parens and not $nollafr and substr($first, 0, 1) eq "("; | |
9d2c6865 SM |
2561 | push @exprs, $first; |
2562 | $kid = $kid->sibling; | |
564cd6cb FC |
2563 | if (defined $proto && $proto =~ /^\*\*/ && $kid->name eq "rv2gv" |
2564 | && !($kid->private & OPpLVAL_INTRO)) { | |
b89b7257 | 2565 | push @exprs, $first = $self->rv2gv_or_string($kid->first); |
b72c97e8 RGS |
2566 | $kid = $kid->sibling; |
2567 | } | |
9d2c6865 SM |
2568 | for (; !null($kid); $kid = $kid->sibling) { |
2569 | push @exprs, $self->deparse($kid, 6); | |
2570 | } | |
689e417f | 2571 | if ($name eq "reverse" && ($op->private & OPpREVERSE_INPLACE)) { |
4a1ac32e FC |
2572 | return "$exprs[0] = $fullname" |
2573 | . ($parens ? "($exprs[0])" : " $exprs[0]"); | |
689e417f | 2574 | } |
327088eb DM |
2575 | if ($name =~ /^(system|exec)$/ |
2576 | && ($op->flags & OPf_STACKED) | |
2577 | && @exprs > 1) | |
2578 | { | |
2579 | # handle the "system prog a1,a2,.." form | |
2580 | my $prog = shift @exprs; | |
2581 | $exprs[0] = "$prog $exprs[0]"; | |
2582 | } | |
2583 | ||
9c56d9ea FC |
2584 | if ($parens && $nollafr) { |
2585 | return "($fullname " . join(", ", @exprs) . ")"; | |
2586 | } elsif ($parens) { | |
4a1ac32e | 2587 | return "$fullname(" . join(", ", @exprs) . ")"; |
9d2c6865 | 2588 | } else { |
4a1ac32e | 2589 | return "$fullname " . join(", ", @exprs); |
6e90668e | 2590 | } |
6e90668e | 2591 | } |
a798dbf2 | 2592 | |
6e90668e | 2593 | sub pp_bless { listop(@_, "bless") } |
3ed82cfc | 2594 | sub pp_atan2 { maybe_targmy(@_, \&listop, "atan2") } |
24fcb59f FC |
2595 | sub pp_substr { |
2596 | my ($self,$op,$cx) = @_; | |
2597 | if ($op->private & OPpSUBSTR_REPL_FIRST) { | |
2598 | return | |
2599 | listop($self, $op, 7, "substr", $op->first->sibling->sibling) | |
2600 | . " = " | |
2601 | . $self->deparse($op->first->sibling, 7); | |
2602 | } | |
2603 | maybe_local(@_, listop(@_, "substr")) | |
2604 | } | |
6e90668e | 2605 | sub pp_vec { maybe_local(@_, listop(@_, "vec")) } |
3ed82cfc GS |
2606 | sub pp_index { maybe_targmy(@_, \&listop, "index") } |
2607 | sub pp_rindex { maybe_targmy(@_, \&listop, "rindex") } | |
2608 | sub pp_sprintf { maybe_targmy(@_, \&listop, "sprintf") } | |
6e90668e | 2609 | sub pp_formline { listop(@_, "formline") } # see also deparse_format |
3ed82cfc | 2610 | sub pp_crypt { maybe_targmy(@_, \&listop, "crypt") } |
6e90668e SM |
2611 | sub pp_unpack { listop(@_, "unpack") } |
2612 | sub pp_pack { listop(@_, "pack") } | |
3ed82cfc | 2613 | sub pp_join { maybe_targmy(@_, \&listop, "join") } |
6e90668e | 2614 | sub pp_splice { listop(@_, "splice") } |
3ed82cfc GS |
2615 | sub pp_push { maybe_targmy(@_, \&listop, "push") } |
2616 | sub pp_unshift { maybe_targmy(@_, \&listop, "unshift") } | |
6e90668e SM |
2617 | sub pp_reverse { listop(@_, "reverse") } |
2618 | sub pp_warn { listop(@_, "warn") } | |
2619 | sub pp_die { listop(@_, "die") } | |
9c56d9ea | 2620 | sub pp_return { listop(@_, "return", undef, 1) } # llafr does not apply |
6e90668e SM |
2621 | sub pp_open { listop(@_, "open") } |
2622 | sub pp_pipe_op { listop(@_, "pipe") } | |
2623 | sub pp_tie { listop(@_, "tie") } | |
82bafd27 | 2624 | sub pp_binmode { listop(@_, "binmode") } |
6e90668e SM |
2625 | sub pp_dbmopen { listop(@_, "dbmopen") } |
2626 | sub pp_sselect { listop(@_, "select") } | |
2627 | sub pp_select { listop(@_, "select") } | |
2628 | sub pp_read { listop(@_, "read") } | |
2629 | sub pp_sysopen { listop(@_, "sysopen") } | |
2630 | sub pp_sysseek { listop(@_, "sysseek") } | |
2631 | sub pp_sysread { listop(@_, "sysread") } | |
2632 | sub pp_syswrite { listop(@_, "syswrite") } | |
2633 | sub pp_send { listop(@_, "send") } | |
2634 | sub pp_recv { listop(@_, "recv") } | |
2635 | sub pp_seek { listop(@_, "seek") } | |
6e90668e SM |
2636 | sub pp_fcntl { listop(@_, "fcntl") } |
2637 | sub pp_ioctl { listop(@_, "ioctl") } | |
3ed82cfc | 2638 | sub pp_flock { maybe_targmy(@_, \&listop, "flock") } |
6e90668e SM |
2639 | sub pp_socket { listop(@_, "socket") } |
2640 | sub pp_sockpair { listop(@_, "sockpair") } | |
2641 | sub pp_bind { listop(@_, "bind") } | |
2642 | sub pp_connect { listop(@_, "connect") } | |
2643 | sub pp_listen { listop(@_, "listen") } | |
2644 | sub pp_accept { listop(@_, "accept") } | |
2645 | sub pp_shutdown { listop(@_, "shutdown") } | |
2646 | sub pp_gsockopt { listop(@_, "getsockopt") } | |
2647 | sub pp_ssockopt { listop(@_, "setsockopt") } | |
3ed82cfc GS |
2648 | sub pp_chown { maybe_targmy(@_, \&listop, "chown") } |
2649 | sub pp_unlink { maybe_targmy(@_, \&listop, "unlink") } | |
2650 | sub pp_chmod { maybe_targmy(@_, \&listop, "chmod") } | |
2651 | sub pp_utime { maybe_targmy(@_, \&listop, "utime") } | |
2652 | sub pp_rename { maybe_targmy(@_, \&listop, "rename") } | |
2653 | sub pp_link { maybe_targmy(@_, \&listop, "link") } | |
2654 | sub pp_symlink { maybe_targmy(@_, \&listop, "symlink") } | |
2655 | sub pp_mkdir { maybe_targmy(@_, \&listop, "mkdir") } | |
6e90668e SM |
2656 | sub pp_open_dir { listop(@_, "opendir") } |
2657 | sub pp_seekdir { listop(@_, "seekdir") } | |
3ed82cfc GS |
2658 | sub pp_waitpid { maybe_targmy(@_, \&listop, "waitpid") } |
2659 | sub pp_system { maybe_targmy(@_, \&listop, "system") } | |
2660 | sub pp_exec { maybe_targmy(@_, \&listop, "exec") } | |
2661 | sub pp_kill { maybe_targmy(@_, \&listop, "kill") } | |
2662 | sub pp_setpgrp { maybe_targmy(@_, \&listop, "setpgrp") } | |
2663 | sub pp_getpriority { maybe_targmy(@_, \&listop, "getpriority") } | |
2664 | sub pp_setpriority { maybe_targmy(@_, \&listop, "setpriority") } | |
6e90668e SM |
2665 | sub pp_shmget { listop(@_, "shmget") } |
2666 | sub pp_shmctl { listop(@_, "shmctl") } | |
2667 | sub pp_shmread { listop(@_, "shmread") } | |
2668 | sub pp_shmwrite { listop(@_, "shmwrite") } | |
2669 | sub pp_msgget { listop(@_, "msgget") } | |
2670 | sub pp_msgctl { listop(@_, "msgctl") } | |
2671 | sub pp_msgsnd { listop(@_, "msgsnd") } | |
2672 | sub pp_msgrcv { listop(@_, "msgrcv") } | |
2673 | sub pp_semget { listop(@_, "semget") } | |
2674 | sub pp_semctl { listop(@_, "semctl") } | |
2675 | sub pp_semop { listop(@_, "semop") } | |
2676 | sub pp_ghbyaddr { listop(@_, "gethostbyaddr") } | |
2677 | sub pp_gnbyaddr { listop(@_, "getnetbyaddr") } | |
2678 | sub pp_gpbynumber { listop(@_, "getprotobynumber") } | |
2679 | sub pp_gsbyname { listop(@_, "getservbyname") } | |
2680 | sub pp_gsbyport { listop(@_, "getservbyport") } | |
2681 | sub pp_syscall { listop(@_, "syscall") } | |
2682 | ||
2683 | sub pp_glob { | |
2684 | my $self = shift; | |
9d2c6865 | 2685 | my($op, $cx) = @_; |
6e90668e | 2686 | my $text = $self->dq($op->first->sibling); # skip pushmark |
a32fbbd8 FC |
2687 | my $keyword = |
2688 | $op->flags & OPf_SPECIAL ? 'glob' : $self->keyword('glob'); | |
6e90668e | 2689 | if ($text =~ /^\$?(\w|::|\`)+$/ # could look like a readline |
a32fbbd8 FC |
2690 | or $keyword =~ /^CORE::/ |
2691 | or $text =~ /[<>]/) { | |
2692 | return "$keyword(" . single_delim('qq', '"', $text) . ')'; | |
6e90668e SM |
2693 | } else { |
2694 | return '<' . $text . '>'; | |
2695 | } | |
2696 | } | |
2697 | ||
f5aa8f4e SM |
2698 | # Truncate is special because OPf_SPECIAL makes a bareword first arg |
2699 | # be a filehandle. This could probably be better fixed in the core | |
2700 | # by moving the GV lookup into ck_truc. | |
2701 | ||
2702 | sub pp_truncate { | |
2703 | my $self = shift; | |
2704 | my($op, $cx) = @_; | |
2705 | my(@exprs); | |
2706 | my $parens = ($cx >= 5) || $self->{'parens'}; | |
2707 | my $kid = $op->first->sibling; | |
acba1d67 | 2708 | my $fh; |
f5aa8f4e SM |
2709 | if ($op->flags & OPf_SPECIAL) { |
2710 | # $kid is an OP_CONST | |
18228111 | 2711 | $fh = $self->const_sv($kid)->PV; |
f5aa8f4e SM |
2712 | } else { |
2713 | $fh = $self->deparse($kid, 6); | |
2714 | $fh = "+$fh" if not $parens and substr($fh, 0, 1) eq "("; | |
2715 | } | |
2716 | my $len = $self->deparse($kid->sibling, 6); | |
4a1ac32e | 2717 | my $name = $self->keyword('truncate'); |
f5aa8f4e | 2718 | if ($parens) { |
4a1ac32e | 2719 | return "$name($fh, $len)"; |
f5aa8f4e | 2720 | } else { |
4a1ac32e | 2721 | return "$name $fh, $len"; |
f5aa8f4e | 2722 | } |
f5aa8f4e SM |
2723 | } |
2724 | ||
6e90668e SM |
2725 | sub indirop { |
2726 | my $self = shift; | |
9d2c6865 | 2727 | my($op, $cx, $name) = @_; |
6e90668e | 2728 | my($expr, @exprs); |
521795fe | 2729 | my $firstkid = my $kid = $op->first->sibling; |
6e90668e SM |
2730 | my $indir = ""; |
2731 | if ($op->flags & OPf_STACKED) { | |
2732 | $indir = $kid; | |
2733 | $indir = $indir->first; # skip rv2gv | |
2734 | if (is_scope($indir)) { | |
9d2c6865 | 2735 | $indir = "{" . $self->deparse($indir, 0) . "}"; |
d989cdac | 2736 | $indir = "{;}" if $indir eq "{}"; |
c73811ab RH |
2737 | } elsif ($indir->name eq "const" && $indir->private & OPpCONST_BARE) { |
2738 | $indir = $self->const_sv($indir)->PV; | |
6e90668e | 2739 | } else { |
9d2c6865 | 2740 | $indir = $self->deparse($indir, 24); |
6e90668e SM |
2741 | } |
2742 | $indir = $indir . " "; | |
2743 | $kid = $kid->sibling; | |
2744 | } | |
7e80da18 | 2745 | if ($name eq "sort" && $op->private & (OPpSORT_NUMERIC | OPpSORT_INTEGER)) { |
3ac6e0f9 | 2746 | $indir = ($op->private & OPpSORT_DESCEND) ? '{$b <=> $a} ' |
7e80da18 RH |
2747 | : '{$a <=> $b} '; |
2748 | } | |
3ac6e0f9 | 2749 | elsif ($name eq "sort" && $op->private & OPpSORT_DESCEND) { |
7e80da18 RH |
2750 | $indir = '{$b cmp $a} '; |
2751 | } | |
6e90668e | 2752 | for (; !null($kid); $kid = $kid->sibling) { |
521795fe | 2753 | $expr = $self->deparse($kid, !$indir && $kid == $firstkid && $name eq "sort" && $firstkid->name eq "entersub" ? 16 : 6); |
6e90668e SM |
2754 | push @exprs, $expr; |
2755 | } | |
4a1ac32e | 2756 | my $name2; |
3ac6e0f9 | 2757 | if ($name eq "sort" && $op->private & OPpSORT_REVERSE) { |
4a1ac32e | 2758 | $name2 = $self->keyword('reverse') . ' ' . $self->keyword('sort'); |
3ac6e0f9 | 2759 | } |
4a1ac32e | 2760 | else { $name2 = $self->keyword($name) } |
2b6e98cb | 2761 | if ($name eq "sort" && ($op->private & OPpSORT_INPLACE)) { |
3ac6e0f9 | 2762 | return "$exprs[0] = $name2 $indir $exprs[0]"; |
2b6e98cb DM |
2763 | } |
2764 | ||
d989cdac | 2765 | my $args = $indir . join(", ", @exprs); |
521795fe | 2766 | if ($indir ne "" && $name eq "sort") { |
d989cdac SM |
2767 | # We don't want to say "sort(f 1, 2, 3)", since perl -w will |
2768 | # give bareword warnings in that case. Therefore if context | |
2769 | # requires, we'll put parens around the outside "(sort f 1, 2, | |
2770 | # 3)". Unfortunately, we'll currently think the parens are | |
3c4b39be | 2771 | # necessary more often that they really are, because we don't |
d989cdac SM |
2772 | # distinguish which side of an assignment we're on. |
2773 | if ($cx >= 5) { | |
3ac6e0f9 | 2774 | return "($name2 $args)"; |
d989cdac | 2775 | } else { |
3ac6e0f9 | 2776 | return "$name2 $args"; |
d989cdac | 2777 | } |
521795fe FC |
2778 | } elsif ( |
2779 | !$indir && $name eq "sort" | |
ca331985 | 2780 | && !null($op->first->sibling) |
521795fe FC |
2781 | && $op->first->sibling->name eq 'entersub' |
2782 | ) { | |
2783 | # We cannot say sort foo(bar), as foo will be interpreted as a | |
2784 | # comparison routine. We have to say sort(...) in that case. | |
2785 | return "$name2($args)"; | |
d989cdac | 2786 | } else { |
3ac6e0f9 | 2787 | return $self->maybe_parens_func($name2, $args, $cx, 5); |
d989cdac SM |
2788 | } |
2789 | ||
6e90668e SM |
2790 | } |
2791 | ||
2792 | sub pp_prtf { indirop(@_, "printf") } | |
2793 | sub pp_print { indirop(@_, "print") } | |
9b08e3d3 | 2794 | sub pp_say { indirop(@_, "say") } |
6e90668e SM |
2795 | sub pp_sort { indirop(@_, "sort") } |
2796 | ||
2797 | sub mapop { | |
2798 | my $self = shift; | |
9d2c6865 | 2799 | my($op, $cx, $name) = @_; |
6e90668e SM |
2800 | my($expr, @exprs); |
2801 | my $kid = $op->first; # this is the (map|grep)start | |
2802 | $kid = $kid->first->sibling; # skip a pushmark | |
2803 | my $code = $kid->first; # skip a null | |
2804 | if (is_scope $code) { | |
f4a44678 | 2805 | $code = "{" . $self->deparse($code, 0) . "} "; |
6e90668e | 2806 | } else { |
6d08f7b3 DM |
2807 | $code = $self->deparse($code, 24); |
2808 | $code .= ", " if !null($kid->sibling); | |
6e90668e SM |
2809 | } |
2810 | $kid = $kid->sibling; | |
2811 | for (; !null($kid); $kid = $kid->sibling) { | |
9d2c6865 | 2812 | $expr = $self->deparse($kid, 6); |
9a58b761 | 2813 | push @exprs, $expr if defined $expr; |
6e90668e | 2814 | } |
9d2c6865 | 2815 | return $self->maybe_parens_func($name, $code . join(", ", @exprs), $cx, 5); |
6e90668e SM |
2816 | } |
2817 | ||
d989cdac SM |
2818 | sub pp_mapwhile { mapop(@_, "map") } |
2819 | sub pp_grepwhile { mapop(@_, "grep") } | |
11e09183 SP |
2820 | sub pp_mapstart { baseop(@_, "map") } |
2821 | sub pp_grepstart { baseop(@_, "grep") } | |
6e90668e SM |
2822 | |
2823 | sub pp_list { | |
2824 | my $self = shift; | |
9d2c6865 | 2825 | my($op, $cx) = @_; |
6e90668e SM |
2826 | my($expr, @exprs); |
2827 | my $kid = $op->first->sibling; # skip pushmark | |
958ed56b | 2828 | return '' if class($kid) eq 'NULL'; |
6e90668e | 2829 | my $lop; |
3462b4ac | 2830 | my $local = "either"; # could be local(...), my(...), state(...) or our(...) |
6e90668e SM |
2831 | for ($lop = $kid; !null($lop); $lop = $lop->sibling) { |
2832 | # This assumes that no other private flags equal 128, and that | |
2833 | # OPs that store things other than flags in their op_private, | |
2834 | # like OP_AELEMFAST, won't be immediate children of a list. | |
b8e103fc RH |
2835 | # |
2836 | # OP_ENTERSUB can break this logic, so check for it. | |
2837 | # I suspect that open and exit can too. | |
2838 | ||
2839 | if (!($lop->private & (OPpLVAL_INTRO|OPpOUR_INTRO) | |
ce4e655d | 2840 | or $lop->name eq "undef") |
b8e103fc RH |
2841 | or $lop->name eq "entersub" |
2842 | or $lop->name eq "exit" | |
2843 | or $lop->name eq "open") | |
6e90668e SM |
2844 | { |
2845 | $local = ""; # or not | |
2846 | last; | |
2847 | } | |
3462b4ac RGS |
2848 | if ($lop->name =~ /^pad[ash]v$/) { |
2849 | if ($lop->private & OPpPAD_STATE) { # state() | |
2850 | ($local = "", last) if $local =~ /^(?:local|our|my)$/; | |
2851 | $local = "state"; | |
2852 | } else { # my() | |
2853 | ($local = "", last) if $local =~ /^(?:local|our|state)$/; | |
2854 | $local = "my"; | |
2855 | } | |
b8e103fc RH |
2856 | } elsif ($lop->name =~ /^(gv|rv2)[ash]v$/ |
2857 | && $lop->private & OPpOUR_INTRO | |
2858 | or $lop->name eq "null" && $lop->first->name eq "gvsv" | |
2859 | && $lop->first->private & OPpOUR_INTRO) { # our() | |
3462b4ac | 2860 | ($local = "", last) if $local =~ /^(?:my|local|state)$/; |
ce4e655d | 2861 | $local = "our"; |
3ac6e0f9 RGS |
2862 | } elsif ($lop->name ne "undef" |
2863 | # specifically avoid the "reverse sort" optimisation, | |
2864 | # where "reverse" is nullified | |
36d57d93 | 2865 | && !($lop->name eq 'sort' && ($lop->flags & OPpSORT_REVERSE))) |
3ac6e0f9 RGS |
2866 | { |
2867 | # local() | |
3462b4ac | 2868 | ($local = "", last) if $local =~ /^(?:my|our|state)$/; |
6e90668e SM |
2869 | $local = "local"; |
2870 | } | |
2871 | } | |
2872 | $local = "" if $local eq "either"; # no point if it's all undefs | |
f5aa8f4e | 2873 | return $self->deparse($kid, $cx) if null $kid->sibling and not $local; |
6e90668e SM |
2874 | for (; !null($kid); $kid = $kid->sibling) { |
2875 | if ($local) { | |
3f872cb9 | 2876 | if (class($kid) eq "UNOP" and $kid->first->name eq "gvsv") { |
6e90668e SM |
2877 | $lop = $kid->first; |
2878 | } else { | |
2879 | $lop = $kid; | |
2880 | } | |
2881 | $self->{'avoid_local'}{$$lop}++; | |
9d2c6865 | 2882 | $expr = $self->deparse($kid, 6); |
6e90668e SM |
2883 | delete $self->{'avoid_local'}{$$lop}; |
2884 | } else { | |
9d2c6865 | 2885 | $expr = $self->deparse($kid, 6); |
6e90668e SM |
2886 | } |
2887 | push @exprs, $expr; | |
2888 | } | |
9d2c6865 SM |
2889 | if ($local) { |
2890 | return "$local(" . join(", ", @exprs) . ")"; | |
2891 | } else { | |
2892 | return $self->maybe_parens( join(", ", @exprs), $cx, 6); | |
2893 | } | |
6e90668e SM |
2894 | } |
2895 | ||
6f611a1a GS |
2896 | sub is_ifelse_cont { |
2897 | my $op = shift; | |
2898 | return ($op->name eq "null" and class($op) eq "UNOP" | |
2899 | and $op->first->name =~ /^(and|cond_expr)$/ | |
2900 | and is_scope($op->first->first->sibling)); | |
2901 | } | |
2902 | ||
6e90668e SM |
2903 | sub pp_cond_expr { |
2904 | my $self = shift; | |
9d2c6865 | 2905 | my($op, $cx) = @_; |
6e90668e SM |
2906 | my $cond = $op->first; |
2907 | my $true = $cond->sibling; | |
2908 | my $false = $true->sibling; | |
9d2c6865 | 2909 | my $cuddle = $self->{'cuddle'}; |
d989cdac | 2910 | unless ($cx < 1 and (is_scope($true) and $true->name ne "null") and |
58cccf98 SM |
2911 | (is_scope($false) || is_ifelse_cont($false)) |
2912 | and $self->{'expand'} < 7) { | |
f5aa8f4e | 2913 | $cond = $self->deparse($cond, 8); |
cfaba469 | 2914 | $true = $self->deparse($true, 6); |
9d2c6865 SM |
2915 | $false = $self->deparse($false, 8); |
2916 | return $self->maybe_parens("$cond ? $true : $false", $cx, 8); | |
6f611a1a GS |
2917 | } |
2918 | ||
f5aa8f4e | 2919 | $cond = $self->deparse($cond, 1); |
d989cdac | 2920 | $true = $self->deparse($true, 0); |
6f611a1a GS |
2921 | my $head = "if ($cond) {\n\t$true\n\b}"; |
2922 | my @elsifs; | |
2923 | while (!null($false) and is_ifelse_cont($false)) { | |
2924 | my $newop = $false->first; | |
2925 | my $newcond = $newop->first; | |
2926 | my $newtrue = $newcond->sibling; | |
2927 | $false = $newtrue->sibling; # last in chain is OP_AND => no else | |
7ecdd211 PJ |
2928 | if ($newcond->name eq "lineseq") |
2929 | { | |
2930 | # lineseq to ensure correct line numbers in elsif() | |
2931 | # Bug #37302 fixed by change #33710. | |
2932 | $newcond = $newcond->first->sibling; | |
2933 | } | |
6f611a1a GS |
2934 | $newcond = $self->deparse($newcond, 1); |
2935 | $newtrue = $self->deparse($newtrue, 0); | |
2936 | push @elsifs, "elsif ($newcond) {\n\t$newtrue\n\b}"; | |
2937 | } | |
d989cdac | 2938 | if (!null($false)) { |
6f611a1a GS |
2939 | $false = $cuddle . "else {\n\t" . |
2940 | $self->deparse($false, 0) . "\n\b}\cK"; | |
2941 | } else { | |
2942 | $false = "\cK"; | |
6e90668e | 2943 | } |
d989cdac | 2944 | return $head . join($cuddle, "", @elsifs) . $false; |
6e90668e SM |
2945 | } |
2946 | ||
95562366 NC |
2947 | sub pp_once { |
2948 | my ($self, $op, $cx) = @_; | |
2949 | my $cond = $op->first; | |
2950 | my $true = $cond->sibling; | |
2951 | ||
2952 | return $self->deparse($true, $cx); | |
2953 | } | |
2954 | ||
58cccf98 | 2955 | sub loop_common { |
6e90668e | 2956 | my $self = shift; |
58cccf98 | 2957 | my($op, $cx, $init) = @_; |
6e90668e SM |
2958 | my $enter = $op->first; |
2959 | my $kid = $enter->sibling; | |
0ced6c29 RGS |
2960 | local(@$self{qw'curstash warnings hints hinthash'}) |
2961 | = @$self{qw'curstash warnings hints hinthash'}; | |
6e90668e | 2962 | my $head = ""; |
9d2c6865 | 2963 | my $bare = 0; |
58cccf98 SM |
2964 | my $body; |
2965 | my $cond = undef; | |
d989cdac | 2966 | if ($kid->name eq "lineseq") { # bare or infinite loop |
241416b8 | 2967 | if ($kid->last->name eq "unstack") { # infinite |
e99ebc55 | 2968 | $head = "while (1) "; # Can't use for(;;) if there's a continue |
58cccf98 | 2969 | $cond = ""; |
9d2c6865 SM |
2970 | } else { |
2971 | $bare = 1; | |
6e90668e | 2972 | } |
58cccf98 | 2973 | $body = $kid; |
3f872cb9 | 2974 | } elsif ($enter->name eq "enteriter") { # foreach |
6e90668e SM |
2975 | my $ary = $enter->first->sibling; # first was pushmark |
2976 | my $var = $ary->sibling; | |
36d57d93 RGS |
2977 | if ($ary->name eq 'null' and $enter->private & OPpITER_REVERSED) { |
2978 | # "reverse" was optimised away | |
aae53c41 | 2979 | $ary = listop($self, $ary->first->sibling, 1, 'reverse'); |
36d57d93 | 2980 | } elsif ($enter->flags & OPf_STACKED |
f5aa8f4e SM |
2981 | and not null $ary->first->sibling->sibling) |
2982 | { | |
d7f5b6da SM |
2983 | $ary = $self->deparse($ary->first->sibling, 9) . " .. " . |
2984 | $self->deparse($ary->first->sibling->sibling, 9); | |
d8d95777 GA |
2985 | } else { |
2986 | $ary = $self->deparse($ary, 1); | |
2987 | } | |
6e90668e | 2988 | if (null $var) { |
823eff14 NC |
2989 | if (($enter->flags & OPf_SPECIAL) && ($] < 5.009)) { |
2990 | # thread special var, under 5005threads | |
9d2c6865 | 2991 | $var = $self->pp_threadsv($enter, 1); |
f6f9bdb7 | 2992 | } else { # regular my() variable |
9d2c6865 | 2993 | $var = $self->pp_padsv($enter, 1); |
6e90668e | 2994 | } |
3f872cb9 | 2995 | } elsif ($var->name eq "rv2gv") { |
9d2c6865 | 2996 | $var = $self->pp_rv2sv($var, 1); |
241416b8 DM |
2997 | if ($enter->private & OPpOUR_INTRO) { |
2998 | # our declarations don't have package names | |
2999 | $var =~ s/^(.).*::/$1/; | |
3000 | $var = "our $var"; | |
3001 | } | |
3f872cb9 | 3002 | } elsif ($var->name eq "gv") { |
9d2c6865 | 3003 | $var = "\$" . $self->deparse($var, 1); |
6e90668e | 3004 | } |
58cccf98 | 3005 | $body = $kid->first->first->sibling; # skip OP_AND and OP_ITER |
afb60448 | 3006 | if (!is_state $body->first and $body->first->name !~ /^(?:stub|leave|scope)$/) { |
cf24a840 SM |
3007 | confess unless $var eq '$_'; |
3008 | $body = $body->first; | |
3009 | return $self->deparse($body, 2) . " foreach ($ary)"; | |
3010 | } | |
3011 | $head = "foreach $var ($ary) "; | |
3f872cb9 | 3012 | } elsif ($kid->name eq "null") { # while/until |
6e90668e | 3013 | $kid = $kid->first; |
58cccf98 SM |
3014 | my $name = {"and" => "while", "or" => "until"}->{$kid->name}; |
3015 | $cond = $self->deparse($kid->first, 1); | |
3016 | $head = "$name ($cond) "; | |
3017 | $body = $kid->first->sibling; | |
3f872cb9 | 3018 | } elsif ($kid->name eq "stub") { # bare and empty |
9d2c6865 | 3019 | return "{;}"; # {} could be a hashref |
6e90668e | 3020 | } |
58cccf98 | 3021 | # If there isn't a continue block, then the next pointer for the loop |
241416b8 | 3022 | # will point to the unstack, which is kid's last child, except |
58cccf98 | 3023 | # in a bare loop, when it will point to the leaveloop. When neither of |
241416b8 | 3024 | # these conditions hold, then the second-to-last child is the continue |
58cccf98 SM |
3025 | # block (or the last in a bare loop). |
3026 | my $cont_start = $enter->nextop; | |
3027 | my $cont; | |
241416b8 | 3028 | if ($$cont_start != $$op && ${$cont_start} != ${$body->last}) { |
58cccf98 SM |
3029 | if ($bare) { |
3030 | $cont = $body->last; | |
3031 | } else { | |
3032 | $cont = $body->first; | |
241416b8 | 3033 | while (!null($cont->sibling->sibling)) { |
58cccf98 SM |
3034 | $cont = $cont->sibling; |
3035 | } | |
3036 | } | |
3037 | my $state = $body->first; | |
3038 | my $cuddle = $self->{'cuddle'}; | |
3039 | my @states; | |
3040 | for (; $$state != $$cont; $state = $state->sibling) { | |
3041 | push @states, $state; | |
3042 | } | |
93a8ff62 | 3043 | $body = $self->lineseq(undef, 0, @states); |
58cccf98 SM |
3044 | if (defined $cond and not is_scope $cont and $self->{'expand'} < 3) { |
3045 | $head = "for ($init; $cond; " . $self->deparse($cont, 1) .") "; | |
3046 | $cont = "\cK"; | |
3047 | } else { | |
3048 | $cont = $cuddle . "continue {\n\t" . | |
3049 | $self->deparse($cont, 0) . "\n\b}\cK"; | |
6e90668e | 3050 | } |
6e90668e | 3051 | } else { |
7a9b44b9 | 3052 | return "" if !defined $body; |
c73811ab RH |
3053 | if (length $init) { |
3054 | $head = "for ($init; $cond;) "; | |
3055 | } | |
9d2c6865 | 3056 | $cont = "\cK"; |
58cccf98 | 3057 | $body = $self->deparse($body, 0); |
6e90668e | 3058 | } |
ce4e655d | 3059 | $body =~ s/;?$/;\n/; |
34a48b4b RH |
3060 | |
3061 | return $head . "{\n\t" . $body . "\b}" . $cont; | |
58cccf98 SM |
3062 | } |
3063 | ||
09d856fb | 3064 | sub pp_leaveloop { shift->loop_common(@_, "") } |
58cccf98 SM |
3065 | |
3066 | sub for_loop { | |
3067 | my $self = shift; | |
3068 | my($op, $cx) = @_; | |
3069 | my $init = $self->deparse($op, 1); | |
eae48c89 Z |
3070 | my $s = $op->sibling; |
3071 | my $ll = $s->name eq "unstack" ? $s->sibling : $s->first->sibling; | |
3072 | return $self->loop_common($ll, $cx, $init); | |
6e90668e SM |
3073 | } |
3074 | ||
3075 | sub pp_leavetry { | |
3076 | my $self = shift; | |
9d2c6865 | 3077 | return "eval {\n\t" . $self->pp_leave(@_) . "\n\b}"; |
bd0865ec | 3078 | } |
6e90668e | 3079 | |
a3ba843f FC |
3080 | BEGIN { for (qw[ const stringify rv2sv list glob ]) { |
3081 | eval "sub OP_\U$_ () { " . opnumber($_) . "}" | |
3082 | }} | |
f5aa8f4e | 3083 | |
a798dbf2 | 3084 | sub pp_null { |
6e90668e | 3085 | my $self = shift; |
d989cdac | 3086 | my($op, $cx) = @_; |
6e90668e | 3087 | if (class($op) eq "OP") { |
f4a44678 SM |
3088 | # old value is lost |
3089 | return $self->{'ex_const'} if $op->targ == OP_CONST; | |
3f872cb9 | 3090 | } elsif ($op->first->name eq "pushmark") { |
9d2c6865 | 3091 | return $self->pp_list($op, $cx); |
3f872cb9 | 3092 | } elsif ($op->first->name eq "enter") { |
9d2c6865 | 3093 | return $self->pp_leave($op, $cx); |
31c6271a RD |
3094 | } elsif ($op->first->name eq "leave") { |
3095 | return $self->pp_leave($op->first, $cx); | |
3096 | } elsif ($op->first->name eq "scope") { | |
3097 | return $self->pp_scope($op->first, $cx); | |
bd0865ec | 3098 | } elsif ($op->targ == OP_STRINGIFY) { |
6f611a1a | 3099 | return $self->dquote($op, $cx); |
f4002a4b FC |
3100 | } elsif ($op->targ == OP_GLOB) { |
3101 | return $self->pp_glob( | |
3102 | $op->first # entersub | |
3103 | ->first # ex-list | |
3104 | ->first # pushmark | |
3105 | ->sibling, # glob | |
3106 | $cx | |
3107 | ); | |
6e90668e | 3108 | } elsif (!null($op->first->sibling) and |
3f872cb9 | 3109 | $op->first->sibling->name eq "readline" and |
6e90668e | 3110 | $op->first->sibling->flags & OPf_STACKED) { |
9d2c6865 SM |
3111 | return $self->maybe_parens($self->deparse($op->first, 7) . " = " |
3112 | . $self->deparse($op->first->sibling, 7), | |
3113 | $cx, 7); | |
6e90668e | 3114 | } elsif (!null($op->first->sibling) and |
3f872cb9 | 3115 | $op->first->sibling->name eq "trans" and |
6e90668e | 3116 | $op->first->sibling->flags & OPf_STACKED) { |
9d2c6865 SM |
3117 | return $self->maybe_parens($self->deparse($op->first, 20) . " =~ " |
3118 | . $self->deparse($op->first->sibling, 20), | |
3119 | $cx, 20); | |
d989cdac SM |
3120 | } elsif ($op->flags & OPf_SPECIAL && $cx < 1 && !$op->targ) { |
3121 | return "do {\n\t". $self->deparse($op->first, $cx) ."\n\b};"; | |
ad8caead RGS |
3122 | } elsif (!null($op->first->sibling) and |
3123 | $op->first->sibling->name eq "null" and | |
3124 | class($op->first->sibling) eq "UNOP" and | |
3125 | $op->first->sibling->first->flags & OPf_STACKED and | |
3126 | $op->first->sibling->first->name eq "rcatline") { | |
3127 | return $self->maybe_parens($self->deparse($op->first, 18) . " .= " | |
3128 | . $self->deparse($op->first->sibling, 18), | |
3129 | $cx, 18); | |
6e90668e | 3130 | } else { |
9d2c6865 | 3131 | return $self->deparse($op->first, $cx); |
6e90668e | 3132 | } |
a798dbf2 MB |
3133 | } |
3134 | ||
6e90668e SM |
3135 | sub padname { |
3136 | my $self = shift; | |
3137 | my $targ = shift; | |
68223ea3 | 3138 | return $self->padname_sv($targ)->PVX; |
6e90668e SM |
3139 | } |
3140 | ||
3141 | sub padany { | |
3142 | my $self = shift; | |
3143 | my $op = shift; | |
3144 | return substr($self->padname($op->targ), 1); # skip $/@/% | |
3145 | } | |
3146 | ||
3147 | sub pp_padsv { | |
3148 | my $self = shift; | |
9d2c6865 SM |
3149 | my($op, $cx) = @_; |
3150 | return $self->maybe_my($op, $cx, $self->padname($op->targ)); | |
6e90668e SM |
3151 | } |
3152 | ||
3153 | sub pp_padav { pp_padsv(@_) } | |
3154 | sub pp_padhv { pp_padsv(@_) } | |
3155 | ||
e0ab66ad | 3156 | my @threadsv_names = B::threadsv_names; |
f6f9bdb7 SM |
3157 | sub pp_threadsv { |
3158 | my $self = shift; | |
9d2c6865 SM |
3159 | my($op, $cx) = @_; |
3160 | return $self->maybe_local($op, $cx, "\$" . $threadsv_names[$op->targ]); | |
d989cdac | 3161 | } |
f6f9bdb7 | 3162 | |
6f611a1a | 3163 | sub gv_or_padgv { |
18228111 GS |
3164 | my $self = shift; |
3165 | my $op = shift; | |
6f611a1a GS |
3166 | if (class($op) eq "PADOP") { |
3167 | return $self->padval($op->padix); | |
3168 | } else { # class($op) eq "SVOP" | |
3169 | return $op->gv; | |
18228111 | 3170 | } |
18228111 GS |
3171 | } |
3172 | ||
6e90668e SM |
3173 | sub pp_gvsv { |
3174 | my $self = shift; | |
9d2c6865 | 3175 | my($op, $cx) = @_; |
6f611a1a | 3176 | my $gv = $self->gv_or_padgv($op); |
8510e997 | 3177 | return $self->maybe_local($op, $cx, $self->stash_variable("\$", |
bb8996b8 | 3178 | $self->gv_name($gv), $cx)); |
6e90668e SM |
3179 | } |
3180 | ||
3181 | sub pp_gv { | |
3182 | my $self = shift; | |
9d2c6865 | 3183 | my($op, $cx) = @_; |
6f611a1a | 3184 | my $gv = $self->gv_or_padgv($op); |
18228111 | 3185 | return $self->gv_name($gv); |
6e90668e SM |
3186 | } |
3187 | ||
93bad3fd NC |
3188 | sub pp_aelemfast_lex { |
3189 | my $self = shift; | |
3190 | my($op, $cx) = @_; | |
3191 | my $name = $self->padname($op->targ); | |
3192 | $name =~ s/^@/\$/; | |
bc6b2ef6 | 3193 | return $name . "[" . ($op->private + $self->{'arybase'}) . "]"; |
93bad3fd NC |
3194 | } |
3195 | ||
6e90668e SM |
3196 | sub pp_aelemfast { |
3197 | my $self = shift; | |
9d2c6865 | 3198 | my($op, $cx) = @_; |
93bad3fd NC |
3199 | # optimised PADAV, pre 5.15 |
3200 | return $self->pp_aelemfast_lex(@_) if ($op->flags & OPf_SPECIAL); | |
ce4e655d | 3201 | |
93bad3fd | 3202 | my $gv = $self->gv_or_padgv($op); |
be6cf5cf FC |
3203 | my($name,$quoted) = $self->stash_variable_name('@',$gv); |
3204 | $name = $quoted ? "$name->" : '$' . $name; | |
bc6b2ef6 | 3205 | return $name . "[" . ($op->private + $self->{'arybase'}) . "]"; |
6e90668e SM |
3206 | } |
3207 | ||
3208 | sub rv2x { | |
3209 | my $self = shift; | |
9d2c6865 | 3210 | my($op, $cx, $type) = @_; |
90c0eb26 RH |
3211 | |
3212 | if (class($op) eq 'NULL' || !$op->can("first")) { | |
ff97752d | 3213 | carp("Unexpected op in pp_rv2x"); |
90c0eb26 RH |
3214 | return 'XXX'; |
3215 | } | |
6e90668e | 3216 | my $kid = $op->first; |
d989cdac | 3217 | if ($kid->name eq "gv") { |
bb8996b8 | 3218 | return $self->stash_variable($type, $self->deparse($kid, 0), $cx); |
d989cdac SM |
3219 | } elsif (is_scalar $kid) { |
3220 | my $str = $self->deparse($kid, 0); | |
3221 | if ($str =~ /^\$([^\w\d])\z/) { | |
3222 | # "$$+" isn't a legal way to write the scalar dereference | |
3223 | # of $+, since the lexer can't tell you aren't trying to | |
3224 | # do something like "$$ + 1" to get one more than your | |
3225 | # PID. Either "${$+}" or "$${+}" are workable | |
3226 | # disambiguations, but if the programmer did the former, | |
3227 | # they'd be in the "else" clause below rather than here. | |
3228 | # It's not clear if this should somehow be unified with | |
3229 | # the code in dq and re_dq that also adds lexer | |
3230 | # disambiguation braces. | |
3231 | $str = '$' . "{$1}"; #' | |
3232 | } | |
3233 | return $type . $str; | |
3234 | } else { | |
3235 | return $type . "{" . $self->deparse($kid, 0) . "}"; | |
3236 | } | |
6e90668e SM |
3237 | } |
3238 | ||
3239 | sub pp_rv2sv { maybe_local(@_, rv2x(@_, "\$")) } | |
3240 | sub pp_rv2hv { maybe_local(@_, rv2x(@_, "%")) } | |
3241 | sub pp_rv2gv { maybe_local(@_, rv2x(@_, "*")) } | |
3242 | ||
3243 | # skip rv2av | |
3244 | sub pp_av2arylen { | |
3245 | my $self = shift; | |
9d2c6865 | 3246 | my($op, $cx) = @_; |
3f872cb9 | 3247 | if ($op->first->name eq "padav") { |
9d2c6865 | 3248 | return $self->maybe_local($op, $cx, '$#' . $self->padany($op->first)); |
6e90668e | 3249 | } else { |
f5aa8f4e SM |
3250 | return $self->maybe_local($op, $cx, |
3251 | $self->rv2x($op->first, $cx, '$#')); | |
6e90668e SM |
3252 | } |
3253 | } | |
3254 | ||
3255 | # skip down to the old, ex-rv2cv | |
90c0eb26 RH |
3256 | sub pp_rv2cv { |
3257 | my ($self, $op, $cx) = @_; | |
3258 | if (!null($op->first) && $op->first->name eq 'null' && | |
3259 | $op->first->targ eq OP_LIST) | |
3260 | { | |
3261 | return $self->rv2x($op->first->first->sibling, $cx, "&") | |
3262 | } | |
3263 | else { | |
3264 | return $self->rv2x($op, $cx, "") | |
3265 | } | |
3266 | } | |
6e90668e | 3267 | |
d989cdac SM |
3268 | sub list_const { |
3269 | my $self = shift; | |
3270 | my($cx, @list) = @_; | |
3271 | my @a = map $self->const($_, 6), @list; | |
3272 | if (@a == 0) { | |
3273 | return "()"; | |
3274 | } elsif (@a == 1) { | |
3275 | return $a[0]; | |
3276 | } elsif ( @a > 2 and !grep(!/^-?\d+$/, @a)) { | |
3277 | # collapse (-1,0,1,2) into (-1..2) | |
3278 | my ($s, $e) = @a[0,-1]; | |
3279 | my $i = $s; | |
3280 | return $self->maybe_parens("$s..$e", $cx, 9) | |
3281 | unless grep $i++ != $_, @a; | |
3282 | } | |
3283 | return $self->maybe_parens(join(", ", @a), $cx, 6); | |
3284 | } | |
3285 | ||
6e90668e SM |
3286 | sub pp_rv2av { |
3287 | my $self = shift; | |
9d2c6865 | 3288 | my($op, $cx) = @_; |
6e90668e | 3289 | my $kid = $op->first; |
3f872cb9 | 3290 | if ($kid->name eq "const") { # constant list |
18228111 | 3291 | my $av = $self->const_sv($kid); |
d989cdac | 3292 | return $self->list_const($cx, $av->ARRAY); |
6e90668e | 3293 | } else { |
9d2c6865 | 3294 | return $self->maybe_local($op, $cx, $self->rv2x($op, $cx, "\@")); |
6e90668e SM |
3295 | } |
3296 | } | |
3297 | ||
3ed82cfc GS |
3298 | sub is_subscriptable { |
3299 | my $op = shift; | |
3300 | if ($op->name =~ /^[ahg]elem/) { | |
3301 | return 1; | |
3302 | } elsif ($op->name eq "entersub") { | |
3303 | my $kid = $op->first; | |
3304 | return 0 unless null $kid->sibling; | |
3305 | $kid = $kid->first; | |
3306 | $kid = $kid->sibling until null $kid->sibling; | |
3307 | return 0 if is_scope($kid); | |
3308 | $kid = $kid->first; | |
3309 | return 0 if $kid->name eq "gv"; | |
3310 | return 0 if is_scalar($kid); | |
3311 | return is_subscriptable($kid); | |
3312 | } else { | |
3313 | return 0; | |
3314 | } | |
3315 | } | |
6e90668e | 3316 | |
21b7468a BL |
3317 | sub elem_or_slice_array_name |
3318 | { | |
6e90668e | 3319 | my $self = shift; |
21b7468a BL |
3320 | my ($array, $left, $padname, $allow_arrow) = @_; |
3321 | ||
3f872cb9 | 3322 | if ($array->name eq $padname) { |
21b7468a | 3323 | return $self->padany($array); |
6e90668e | 3324 | } elsif (is_scope($array)) { # ${expr}[0] |
21b7468a | 3325 | return "{" . $self->deparse($array, 0) . "}"; |
ce4e655d | 3326 | } elsif ($array->name eq "gv") { |
10e8e32b FC |
3327 | ($array, my $quoted) = |
3328 | $self->stash_variable_name( | |
3329 | $left eq '[' ? '@' : '%', $self->gv_or_padgv($array) | |
3330 | ); | |
3331 | if (!$allow_arrow && $quoted) { | |
3332 | # This cannot happen. | |
3333 | die "Invalid variable name $array for slice"; | |
ce4e655d | 3334 | } |
10e8e32b | 3335 | return $quoted ? "$array->" : $array; |
21b7468a BL |
3336 | } elsif (!$allow_arrow || is_scalar $array) { # $x[0], $$x[0], ... |
3337 | return $self->deparse($array, 24); | |
6e90668e | 3338 | } else { |
21b7468a | 3339 | return undef; |
6e90668e | 3340 | } |
21b7468a BL |
3341 | } |
3342 | ||
3343 | sub elem_or_slice_single_index | |
3344 | { | |
3345 | my $self = shift; | |
3346 | my ($idx) = @_; | |
3347 | ||
9d2c6865 | 3348 | $idx = $self->deparse($idx, 1); |
7a9b44b9 RH |
3349 | |
3350 | # Outer parens in an array index will confuse perl | |
3351 | # if we're interpolating in a regular expression, i.e. | |
3352 | # /$x$foo[(-1)]/ is *not* the same as /$x$foo[-1]/ | |
3353 | # | |
3354 | # If $self->{parens}, then an initial '(' will | |
3355 | # definitely be paired with a final ')'. If | |
3356 | # !$self->{parens}, the misleading parens won't | |
3357 | # have been added in the first place. | |
3358 | # | |
3359 | # [You might think that we could get "(...)...(...)" | |
3360 | # where the initial and final parens do not match | |
3361 | # each other. But we can't, because the above would | |
3362 | # only happen if there's an infix binop between the | |
3363 | # two pairs of parens, and *that* means that the whole | |
3364 | # expression would be parenthesized as well.] | |
3365 | # | |
3366 | $idx =~ s/^\((.*)\)$/$1/ if $self->{'parens'}; | |
3367 | ||
098251bc RH |
3368 | # Hash-element braces will autoquote a bareword inside themselves. |
3369 | # We need to make sure that C<$hash{warn()}> doesn't come out as | |
3370 | # C<$hash{warn}>, which has a quite different meaning. Currently | |
3371 | # B::Deparse will always quote strings, even if the string was a | |
3372 | # bareword in the original (i.e. the OPpCONST_BARE flag is ignored | |
3373 | # for constant strings.) So we can cheat slightly here - if we see | |
3374 | # a bareword, we know that it is supposed to be a function call. | |
3375 | # | |
3376 | $idx =~ s/^([A-Za-z_]\w*)$/$1()/; | |
3377 | ||
21b7468a BL |
3378 | return $idx; |
3379 | } | |
3380 | ||
3381 | sub elem { | |
3382 | my $self = shift; | |
3383 | my ($op, $cx, $left, $right, $padname) = @_; | |
3384 | my($array, $idx) = ($op->first, $op->first->sibling); | |
3385 | ||
3386 | $idx = $self->elem_or_slice_single_index($idx); | |
3387 | ||
3388 | unless ($array->name eq $padname) { # Maybe this has been fixed | |
3389 | $array = $array->first; # skip rv2av (or ex-rv2av in _53+) | |
3390 | } | |
3391 | if (my $array_name=$self->elem_or_slice_array_name | |
3392 | ($array, $left, $padname, 1)) { | |
10e8e32b FC |
3393 | return ($array_name =~ /->\z/ ? $array_name : "\$" . $array_name) |
3394 | . $left . $idx . $right; | |
21b7468a BL |
3395 | } else { |
3396 | # $x[20][3]{hi} or expr->[20] | |
3397 | my $arrow = is_subscriptable($array) ? "" : "->"; | |
3398 | return $self->deparse($array, 24) . $arrow . $left . $idx . $right; | |
3399 | } | |
3400 | ||
6e90668e SM |
3401 | } |
3402 | ||
3f872cb9 GS |
3403 | sub pp_aelem { maybe_local(@_, elem(@_, "[", "]", "padav")) } |
3404 | sub pp_helem { maybe_local(@_, elem(@_, "{", "}", "padhv")) } | |
6e90668e SM |
3405 | |
3406 | sub pp_gelem { | |
3407 | my $self = shift; | |
9d2c6865 | 3408 | my($op, $cx) = @_; |
6e90668e SM |
3409 | my($glob, $part) = ($op->first, $op->last); |
3410 | $glob = $glob->first; # skip rv2gv | |
3f872cb9 | 3411 | $glob = $glob->first if $glob->name eq "rv2gv"; # this one's a bug |
9d2c6865 SM |
3412 | my $scope = is_scope($glob); |
3413 | $glob = $self->deparse($glob, 0); | |
3414 | $part = $self->deparse($part, 1); | |
6e90668e SM |
3415 | return "*" . ($scope ? "{$glob}" : $glob) . "{$part}"; |
3416 | } | |
3417 | ||
3418 | sub slice { | |
3419 | my $self = shift; | |
9d2c6865 | 3420 | my ($op, $cx, $left, $right, $regname, $padname) = @_; |
6e90668e SM |
3421 | my $last; |
3422 | my(@elems, $kid, $array, $list); | |
3423 | if (class($op) eq "LISTOP") { | |
3424 | $last = $op->last; | |
3425 | } else { # ex-hslice inside delete() | |
3426 | for ($kid = $op->first; !null $kid->sibling; $kid = $kid->sibling) {} | |
3427 | $last = $kid; | |
3428 | } | |
3429 | $array = $last; | |
3430 | $array = $array->first | |
3f872cb9 | 3431 | if $array->name eq $regname or $array->name eq "null"; |
21b7468a | 3432 | $array = $self->elem_or_slice_array_name($array,$left,$padname,0); |
6e90668e | 3433 | $kid = $op->first->sibling; # skip pushmark |
3f872cb9 | 3434 | if ($kid->name eq "list") { |
6e90668e SM |
3435 | $kid = $kid->first->sibling; # skip list, pushmark |
3436 | for (; !null $kid; $kid = $kid->sibling) { | |
9d2c6865 | 3437 | push @elems, $self->deparse($kid, 6); |
6e90668e SM |
3438 | } |
3439 | $list = join(", ", @elems); | |
3440 | } else { | |
21b7468a | 3441 | $list = $self->elem_or_slice_single_index($kid); |
6e90668e SM |
3442 | } |
3443 | return "\@" . $array . $left . $list . $right; | |
3444 | } | |
3445 | ||
3ed82cfc GS |
3446 | sub pp_aslice { maybe_local(@_, slice(@_, "[", "]", "rv2av", "padav")) } |
3447 | sub pp_hslice { maybe_local(@_, slice(@_, "{", "}", "rv2hv", "padhv")) } | |
6e90668e SM |
3448 | |
3449 | sub pp_lslice { | |
3450 | my $self = shift; | |
9d2c6865 | 3451 | my($op, $cx) = @_; |
6e90668e SM |
3452 | my $idx = $op->first; |
3453 | my $list = $op->last; | |
3454 | my(@elems, $kid); | |
9d2c6865 SM |
3455 | $list = $self->deparse($list, 1); |
3456 | $idx = $self->deparse($idx, 1); | |
3457 | return "($list)" . "[$idx]"; | |
6e90668e SM |
3458 | } |
3459 | ||
6e90668e SM |
3460 | sub want_scalar { |
3461 | my $op = shift; | |
3462 | return ($op->flags & OPf_WANT) == OPf_WANT_SCALAR; | |
3463 | } | |
3464 | ||
bd0865ec GS |
3465 | sub want_list { |
3466 | my $op = shift; | |
3467 | return ($op->flags & OPf_WANT) == OPf_WANT_LIST; | |
3468 | } | |
3469 | ||
09d856fb | 3470 | sub _method { |
6e90668e | 3471 | my $self = shift; |
9d2c6865 | 3472 | my($op, $cx) = @_; |
bd0865ec GS |
3473 | my $kid = $op->first->sibling; # skip pushmark |
3474 | my($meth, $obj, @exprs); | |
3f872cb9 | 3475 | if ($kid->name eq "list" and want_list $kid) { |
bd0865ec GS |
3476 | # When an indirect object isn't a bareword but the args are in |
3477 | # parens, the parens aren't part of the method syntax (the LLAFR | |
3478 | # doesn't apply), but they make a list with OPf_PARENS set that | |
3479 | # doesn't get flattened by the append_elem that adds the method, | |
3480 | # making a (object, arg1, arg2, ...) list where the object | |
d989cdac | 3481 | # usually is. This can be distinguished from |
e38ccfd9 | 3482 | # '($obj, $arg1, $arg2)->meth()' (which is legal if $arg2 is an |
bd0865ec GS |
3483 | # object) because in the later the list is in scalar context |
3484 | # as the left side of -> always is, while in the former | |
3485 | # the list is in list context as method arguments always are. | |
3486 | # (Good thing there aren't method prototypes!) | |
3ed82cfc | 3487 | $meth = $kid->sibling; |
bd0865ec GS |
3488 | $kid = $kid->first->sibling; # skip pushmark |
3489 | $obj = $kid; | |
6e90668e | 3490 | $kid = $kid->sibling; |
bd0865ec | 3491 | for (; not null $kid; $kid = $kid->sibling) { |
09d856fb | 3492 | push @exprs, $kid; |
6e90668e | 3493 | } |
bd0865ec GS |
3494 | } else { |
3495 | $obj = $kid; | |
3496 | $kid = $kid->sibling; | |
35a99a08 | 3497 | for (; !null ($kid->sibling) && $kid->name!~/^method(?:_named)?\z/; |
90c0eb26 | 3498 | $kid = $kid->sibling) { |
09d856fb | 3499 | push @exprs, $kid |
6e90668e | 3500 | } |
3ed82cfc | 3501 | $meth = $kid; |
bd0865ec | 3502 | } |
09d856fb | 3503 | |
3ed82cfc | 3504 | if ($meth->name eq "method_named") { |
18228111 | 3505 | $meth = $self->const_sv($meth)->PV; |
bd0865ec | 3506 | } else { |
3ed82cfc GS |
3507 | $meth = $meth->first; |
3508 | if ($meth->name eq "const") { | |
3509 | # As of 5.005_58, this case is probably obsoleted by the | |
3510 | # method_named case above | |
18228111 | 3511 | $meth = $self->const_sv($meth)->PV; # needs to be bare |
3ed82cfc | 3512 | } |
bd0865ec | 3513 | } |
09d856fb CK |
3514 | |
3515 | return { method => $meth, variable_method => ref($meth), | |
1bf8bbb0 FC |
3516 | object => $obj, args => \@exprs }, |
3517 | $cx; | |
09d856fb CK |
3518 | } |
3519 | ||
3520 | # compat function only | |
3521 | sub method { | |
3522 | my $self = shift; | |
3523 | my $info = $self->_method(@_); | |
3524 | return $self->e_method( $self->_method(@_) ); | |
3525 | } | |
3526 | ||
3527 | sub e_method { | |
1bf8bbb0 | 3528 | my ($self, $info, $cx) = @_; |
09d856fb CK |
3529 | my $obj = $self->deparse($info->{object}, 24); |
3530 | ||
3531 | my $meth = $info->{method}; | |
3532 | $meth = $self->deparse($meth, 1) if $info->{variable_method}; | |
3533 | my $args = join(", ", map { $self->deparse($_, 6) } @{$info->{args}} ); | |
1bf8bbb0 FC |
3534 | if ($info->{object}->name eq 'scope' && want_list $info->{object}) { |
3535 | # method { $object } | |
3536 | # This must be deparsed this way to preserve list context | |
3537 | # of $object. | |
3538 | my $need_paren = $cx >= 6; | |
3539 | return '(' x $need_paren | |
3540 | . $meth . substr($obj,2) # chop off the "do" | |
3541 | . " $args" | |
3542 | . ')' x $need_paren; | |
3543 | } | |
09d856fb | 3544 | my $kid = $obj . "->" . $meth; |
145eb477 | 3545 | if (length $args) { |
bd0865ec GS |
3546 | return $kid . "(" . $args . ")"; # parens mandatory |
3547 | } else { | |
3548 | return $kid; | |
3549 | } | |
3550 | } | |
3551 | ||
3552 | # returns "&" if the prototype doesn't match the args, | |
3553 | # or ("", $args_after_prototype_demunging) if it does. | |
3554 | sub check_proto { | |
3555 | my $self = shift; | |
acaaef34 | 3556 | return "&" if $self->{'noproto'}; |
bd0865ec GS |
3557 | my($proto, @args) = @_; |
3558 | my($arg, $real); | |
3559 | my $doneok = 0; | |
3560 | my @reals; | |
3561 | # An unbackslashed @ or % gobbles up the rest of the args | |
2ae48fff | 3562 | 1 while $proto =~ s/(?<!\\)([@%])[^\]]+$/$1/; |
bd0865ec | 3563 | while ($proto) { |
21b52158 | 3564 | $proto =~ s/^(\\?[\$\@&%*_]|\\\[[\$\@&%*]+\]|;)//; |
bd0865ec GS |
3565 | my $chr = $1; |
3566 | if ($chr eq "") { | |
3567 | return "&" if @args; | |
3568 | } elsif ($chr eq ";") { | |
3569 | $doneok = 1; | |
3570 | } elsif ($chr eq "@" or $chr eq "%") { | |
3571 | push @reals, map($self->deparse($_, 6), @args); | |
3572 | @args = (); | |
6e90668e | 3573 | } else { |
bd0865ec GS |
3574 | $arg = shift @args; |
3575 | last unless $arg; | |
21b52158 | 3576 | if ($chr eq "\$" || $chr eq "_") { |
bd0865ec GS |
3577 | if (want_scalar $arg) { |
3578 | push @reals, $self->deparse($arg, 6); | |
3579 | } else { | |
3580 | return "&"; | |
3581 | } | |
3582 | } elsif ($chr eq "&") { | |
3f872cb9 | 3583 | if ($arg->name =~ /^(s?refgen|undef)$/) { |
bd0865ec GS |
3584 | push @reals, $self->deparse($arg, 6); |
3585 | } else { | |
3586 | return "&"; | |
3587 | } | |
3588 | } elsif ($chr eq "*") { | |
3f872cb9 GS |
3589 | if ($arg->name =~ /^s?refgen$/ |
3590 | and $arg->first->first->name eq "rv2gv") | |
bd0865ec GS |
3591 | { |
3592 | $real = $arg->first->first; # skip refgen, null | |
3f872cb9 | 3593 | if ($real->first->name eq "gv") { |
bd0865ec GS |
3594 | push @reals, $self->deparse($real, 6); |
3595 | } else { | |
3596 | push @reals, $self->deparse($real->first, 6); | |
3597 | } | |
3598 | } else { | |
3599 | return "&"; | |
3600 | } | |
3601 | } elsif (substr($chr, 0, 1) eq "\\") { | |
2ae48fff | 3602 | $chr =~ tr/\\[]//d; |
3f872cb9 | 3603 | if ($arg->name =~ /^s?refgen$/ and |
bd0865ec | 3604 | !null($real = $arg->first) and |
2ae48fff RGS |
3605 | ($chr =~ /\$/ && is_scalar($real->first) |
3606 | or ($chr =~ /@/ | |
3607 | && class($real->first->sibling) ne 'NULL' | |
3f872cb9 GS |
3608 | && $real->first->sibling->name |
3609 | =~ /^(rv2|pad)av$/) | |
2ae48fff RGS |
3610 | or ($chr =~ /%/ |
3611 | && class($real->first->sibling) ne 'NULL' | |
3f872cb9 GS |
3612 | && $real->first->sibling->name |
3613 | =~ /^(rv2|pad)hv$/) | |
2ae48fff | 3614 | #or ($chr =~ /&/ # This doesn't work |
3f872cb9 | 3615 | # && $real->first->name eq "rv2cv") |
2ae48fff | 3616 | or ($chr =~ /\*/ |
3f872cb9 | 3617 | && $real->first->name eq "rv2gv"))) |
bd0865ec GS |
3618 | { |
3619 | push @reals, $self->deparse($real, 6); | |
3620 | } else { | |
3621 | return "&"; | |
3622 | } | |
3623 | } | |
3624 | } | |
9d2c6865 | 3625 | } |
e38ccfd9 | 3626 | return "&" if $proto and !$doneok; # too few args and no ';' |
bd0865ec GS |
3627 | return "&" if @args; # too many args |
3628 | return ("", join ", ", @reals); | |
3629 | } | |
3630 | ||
3631 | sub pp_entersub { | |
3632 | my $self = shift; | |
3633 | my($op, $cx) = @_; | |
09d856fb CK |
3634 | return $self->e_method($self->_method($op, $cx)) |
3635 | unless null $op->first->sibling; | |
bd0865ec GS |
3636 | my $prefix = ""; |
3637 | my $amper = ""; | |
3638 | my($kid, @exprs); | |
90c0eb26 | 3639 | if ($op->flags & OPf_SPECIAL && !($op->flags & OPf_MOD)) { |
9d2c6865 SM |
3640 | $prefix = "do "; |
3641 | } elsif ($op->private & OPpENTERSUB_AMPER) { | |
3642 | $amper = "&"; | |
3643 | } | |
3644 | $kid = $op->first; | |
3645 | $kid = $kid->first->sibling; # skip ex-list, pushmark | |
3646 | for (; not null $kid->sibling; $kid = $kid->sibling) { | |
3647 | push @exprs, $kid; | |
3648 | } | |
bd0865ec GS |
3649 | my $simple = 0; |
3650 | my $proto = undef; | |
9d2c6865 SM |
3651 | if (is_scope($kid)) { |
3652 | $amper = "&"; | |
3653 | $kid = "{" . $self->deparse($kid, 0) . "}"; | |
3f872cb9 | 3654 | } elsif ($kid->first->name eq "gv") { |
6f611a1a | 3655 | my $gv = $self->gv_or_padgv($kid->first); |
9d2c6865 SM |
3656 | if (class($gv->CV) ne "SPECIAL") { |
3657 | $proto = $gv->CV->PV if $gv->CV->FLAGS & SVf_POK; | |
3658 | } | |
bd0865ec | 3659 | $simple = 1; # only calls of named functions can be prototyped |
9d2c6865 | 3660 | $kid = $self->deparse($kid, 24); |
8b2d6640 FC |
3661 | if (!$amper) { |
3662 | if ($kid eq 'main::') { | |
3663 | $kid = '::'; | |
3664 | } elsif ($kid !~ /^(?:\w|::)(?:[\w\d]|::(?!\z))*\z/) { | |
3665 | $kid = single_delim("q", "'", $kid) . '->'; | |
3666 | } | |
3667 | } | |
90c0eb26 | 3668 | } elsif (is_scalar ($kid->first) && $kid->first->name ne 'rv2cv') { |
9d2c6865 SM |
3669 | $amper = "&"; |
3670 | $kid = $self->deparse($kid, 24); | |
3671 | } else { | |
3672 | $prefix = ""; | |
3ed82cfc GS |
3673 | my $arrow = is_subscriptable($kid->first) ? "" : "->"; |
3674 | $kid = $self->deparse($kid, 24) . $arrow; | |
9d2c6865 | 3675 | } |
0ca62a8e RH |
3676 | |
3677 | # Doesn't matter how many prototypes there are, if | |
3678 | # they haven't happened yet! | |
1d38190f RGS |
3679 | my $declared; |
3680 | { | |
3681 | no strict 'refs'; | |
3682 | no warnings 'uninitialized'; | |
3683 | $declared = exists $self->{'subs_declared'}{$kid} | |
d989cdac | 3684 | || ( |
840378f5 | 3685 | defined &{ ${$self->{'curstash'}."::"}{$kid} } |
1d38190f RGS |
3686 | && !exists |
3687 | $self->{'subs_deparsed'}{$self->{'curstash'}."::".$kid} | |
3688 | && defined prototype $self->{'curstash'}."::".$kid | |
3689 | ); | |
3690 | if (!$declared && defined($proto)) { | |
3691 | # Avoid "too early to check prototype" warning | |
3692 | ($amper, $proto) = ('&'); | |
3693 | } | |
e99ebc55 | 3694 | } |
0ca62a8e | 3695 | |
bd0865ec | 3696 | my $args; |
0ca62a8e | 3697 | if ($declared and defined $proto and not $amper) { |
bd0865ec GS |
3698 | ($amper, $args) = $self->check_proto($proto, @exprs); |
3699 | if ($amper eq "&") { | |
9d2c6865 SM |
3700 | $args = join(", ", map($self->deparse($_, 6), @exprs)); |
3701 | } | |
3702 | } else { | |
3703 | $args = join(", ", map($self->deparse($_, 6), @exprs)); | |
6e90668e | 3704 | } |
9d2c6865 | 3705 | if ($prefix or $amper) { |
1b38d782 | 3706 | if ($kid eq '&') { $kid = "{$kid}" } # &{&} cannot be written as && |
9d2c6865 SM |
3707 | if ($op->flags & OPf_STACKED) { |
3708 | return $prefix . $amper . $kid . "(" . $args . ")"; | |
3709 | } else { | |
3710 | return $prefix . $amper. $kid; | |
3711 | } | |
6e90668e | 3712 | } else { |
7969d523 | 3713 | # It's a syntax error to call CORE::GLOBAL::foo with a prefix, |
34a48b4b RH |
3714 | # so it must have been translated from a keyword call. Translate |
3715 | # it back. | |
3716 | $kid =~ s/^CORE::GLOBAL:://; | |
3717 | ||
d989cdac | 3718 | my $dproto = defined($proto) ? $proto : "undefined"; |
0ca62a8e RH |
3719 | if (!$declared) { |
3720 | return "$kid(" . $args . ")"; | |
d989cdac | 3721 | } elsif ($dproto eq "") { |
9d2c6865 | 3722 | return $kid; |
d989cdac SM |
3723 | } elsif ($dproto eq "\$" and is_scalar($exprs[0])) { |
3724 | # is_scalar is an excessively conservative test here: | |
3725 | # really, we should be comparing to the precedence of the | |
3726 | # top operator of $exprs[0] (ala unop()), but that would | |
3727 | # take some major code restructuring to do right. | |
9d2c6865 | 3728 | return $self->maybe_parens_func($kid, $args, $cx, 16); |
d989cdac | 3729 | } elsif ($dproto ne '$' and defined($proto) || $simple) { #' |
9d2c6865 SM |
3730 | return $self->maybe_parens_func($kid, $args, $cx, 5); |
3731 | } else { | |
3732 | return "$kid(" . $args . ")"; | |
3733 | } | |
6e90668e SM |
3734 | } |
3735 | } | |
3736 | ||
3737 | sub pp_enterwrite { unop(@_, "write") } | |
3738 | ||
3739 | # escape things that cause interpolation in double quotes, | |
3740 | # but not character escapes | |
3741 | sub uninterp { | |
3742 | my($str) = @_; | |
3f766ba3 | 3743 | $str =~ s/(^|\G|[^\\])((?:\\\\)*)([\$\@]|\\[uUlLQE])/$1$2\\$3/g; |
9d2c6865 SM |
3744 | return $str; |
3745 | } | |
3746 | ||
b7dad2dc RH |
3747 | { |
3748 | my $bal; | |
3749 | BEGIN { | |
cdf8218f RH |
3750 | use re "eval"; |
3751 | # Matches any string which is balanced with respect to {braces} | |
b7dad2dc | 3752 | $bal = qr( |
cdf8218f RH |
3753 | (?: |
3754 | [^\\{}] | |
3755 | | \\\\ | |
3756 | | \\[{}] | |
3757 | | \{(??{$bal})\} | |
3758 | )* | |
3759 | )x; | |
b7dad2dc RH |
3760 | } |
3761 | ||
3762 | # the same, but treat $|, $), $( and $ at the end of the string differently | |
3763 | sub re_uninterp { | |
3764 | my($str) = @_; | |
cdf8218f RH |
3765 | |
3766 | $str =~ s/ | |
3767 | ( ^|\G # $1 | |
3768 | | [^\\] | |
3769 | ) | |
3770 | ||
3771 | ( # $2 | |
3772 | (?:\\\\)* | |
3773 | ) | |
3774 | ||
3775 | ( # $3 | |
3776 | (\(\?\??\{$bal\}\)) # $4 | |
3777 | | [\$\@] | |
3778 | (?!\||\)|\(|$) | |
3779 | | \\[uUlLQE] | |
3780 | ) | |
3781 | ||
2621462b | 3782 | /defined($4) && length($4) ? "$1$2$4" : "$1$2\\$3"/xeg; |
cdf8218f | 3783 | |
6e90668e SM |
3784 | return $str; |
3785 | } | |
3786 | ||
b7dad2dc RH |
3787 | # This is for regular expressions with the /x modifier |
3788 | # We have to leave comments unmangled. | |
a9760014 | 3789 | sub re_uninterp_extended { |
b7dad2dc RH |
3790 | my($str) = @_; |
3791 | ||
3792 | $str =~ s/ | |
3793 | ( ^|\G # $1 | |
3794 | | [^\\] | |
3795 | ) | |
3796 | ||
3797 | ( # $2 | |
3798 | (?:\\\\)* | |
3799 | ) | |
3800 | ||
3801 | ( # $3 | |
3802 | ( \(\?\??\{$bal\}\) # $4 (skip over (?{}) and (??{}) blocks) | |
3803 | | \#[^\n]* # (skip over comments) | |
3804 | ) | |
3805 | | [\$\@] | |
9a58b761 | 3806 | (?!\||\)|\(|$|\s) |
b7dad2dc RH |
3807 | | \\[uUlLQE] |
3808 | ) | |
3809 | ||
c7de4c66 | 3810 | /defined($4) && length($4) ? "$1$2$4" : "$1$2\\$3"/xeg; |
b7dad2dc | 3811 | |
a9760014 RH |
3812 | return $str; |
3813 | } | |
b7dad2dc | 3814 | } |
a9760014 | 3815 | |
57aa3c4e JH |
3816 | my %unctrl = # portable to to EBCDIC |
3817 | ( | |
3818 | "\c@" => '\c@', # unused | |
3819 | "\cA" => '\cA', | |
3820 | "\cB" => '\cB', | |
3821 | "\cC" => '\cC', | |
3822 | "\cD" => '\cD', | |
3823 | "\cE" => '\cE', | |
3824 | "\cF" => '\cF', | |
3825 | "\cG" => '\cG', | |
3826 | "\cH" => '\cH', | |
3827 | "\cI" => '\cI', | |
3828 | "\cJ" => '\cJ', | |
3829 | "\cK" => '\cK', | |
3830 | "\cL" => '\cL', | |
3831 | "\cM" => '\cM', | |
3832 | "\cN" => '\cN', | |
3833 | "\cO" => '\cO', | |
3834 | "\cP" => '\cP', | |
3835 | "\cQ" => '\cQ', | |
3836 | "\cR" => '\cR', | |
3837 | "\cS" => '\cS', | |
3838 | "\cT" => '\cT', | |
3839 | "\cU" => '\cU', | |
3840 | "\cV" => '\cV', | |
3841 | "\cW" => '\cW', | |
3842 | "\cX" => '\cX', | |
3843 | "\cY" => '\cY', | |
3844 | "\cZ" => '\cZ', | |
3845 | "\c[" => '\c[', # unused | |
3846 | "\c\\" => '\c\\', # unused | |
3847 | "\c]" => '\c]', # unused | |
3848 | "\c_" => '\c_', # unused | |
3849 | ); | |
3850 | ||
6e90668e | 3851 | # character escapes, but not delimiters that might need to be escaped |
746698c5 | 3852 | sub escape_str { # ASCII, UTF8 |
6e90668e | 3853 | my($str) = @_; |
cef22867 | 3854 | $str =~ s/(.)/ord($1) > 255 ? sprintf("\\x{%x}", ord($1)) : $1/eg; |
6e90668e | 3855 | $str =~ s/\a/\\a/g; |
d989cdac | 3856 | # $str =~ s/\cH/\\b/g; # \b means something different in a regex |
6e90668e SM |
3857 | $str =~ s/\t/\\t/g; |
3858 | $str =~ s/\n/\\n/g; | |
3859 | $str =~ s/\e/\\e/g; | |
3860 | $str =~ s/\f/\\f/g; | |
3861 | $str =~ s/\r/\\r/g; | |
fc132725 | 3862 | $str =~ s/([\cA-\cZ])/$unctrl{$1}/ge; |
cef22867 | 3863 | $str =~ s/([[:^print:]])/sprintf("\\%03o", ord($1))/ge; |
6e90668e SM |
3864 | return $str; |
3865 | } | |
3866 | ||
b7dad2dc RH |
3867 | # For regexes with the /x modifier. |
3868 | # Leave whitespace unmangled. | |
a9760014 RH |
3869 | sub escape_extended_re { |
3870 | my($str) = @_; | |
cef22867 | 3871 | $str =~ s/(.)/ord($1) > 255 ? sprintf("\\x{%x}", ord($1)) : $1/eg; |
aee8fd8d RGS |
3872 | $str =~ s/([[:^print:]])/ |
3873 | ($1 =~ y! \t\n!!) ? $1 : sprintf("\\%03o", ord($1))/ge; | |
a9760014 RH |
3874 | $str =~ s/\n/\n\f/g; |
3875 | return $str; | |
3876 | } | |
3877 | ||
9d2c6865 SM |
3878 | # Don't do this for regexen |
3879 | sub unback { | |
3880 | my($str) = @_; | |
3881 | $str =~ s/\\/\\\\/g; | |
3882 | return $str; | |
3883 | } | |
3884 | ||
08c6f5ec RH |
3885 | # Remove backslashes which precede literal control characters, |
3886 | # to avoid creating ambiguity when we escape the latter. | |
3887 | sub re_unback { | |
3888 | my($str) = @_; | |
3889 | ||
3890 | # the insane complexity here is due to the behaviour of "\c\" | |
cef22867 | 3891 | $str =~ s/(^|[^\\]|\\c\\)(?<!\\c)\\(\\\\)*(?=[[:^print:]])/$1$2/g; |
08c6f5ec RH |
3892 | return $str; |
3893 | } | |
3894 | ||
6e90668e SM |
3895 | sub balanced_delim { |
3896 | my($str) = @_; | |
3897 | my @str = split //, $str; | |
80b7d6d2 | 3898 | my($ar, $open, $close, $fail, $c, $cnt, $last_bs); |
6e90668e SM |
3899 | for $ar (['[',']'], ['(',')'], ['<','>'], ['{','}']) { |
3900 | ($open, $close) = @$ar; | |
80b7d6d2 | 3901 | $fail = 0; $cnt = 0; $last_bs = 0; |
6e90668e SM |
3902 | for $c (@str) { |
3903 | if ($c eq $open) { | |
80b7d6d2 | 3904 | $fail = 1 if $last_bs; |
6e90668e SM |
3905 | $cnt++; |
3906 | } elsif ($c eq $close) { | |
80b7d6d2 | 3907 | $fail = 1 if $last_bs; |
6e90668e SM |
3908 | $cnt--; |
3909 | if ($cnt < 0) { | |
bd0865ec | 3910 | # qq()() isn't ")(" |
6e90668e SM |
3911 | $fail = 1; |
3912 | last; | |
3913 | } | |
3914 | } | |
80b7d6d2 | 3915 | $last_bs = $c eq '\\'; |
6e90668e SM |
3916 | } |
3917 | $fail = 1 if $cnt != 0; | |
3918 | return ($open, "$open$str$close") if not $fail; | |
3919 | } | |
3920 | return ("", $str); | |
3921 | } | |
3922 | ||
3923 | sub single_delim { | |
3924 | my($q, $default, $str) = @_; | |
90be192f | 3925 | return "$default$str$default" if $default and index($str, $default) == -1; |
8347ad86 RGS |
3926 | if ($q ne 'qr') { |
3927 | (my $succeed, $str) = balanced_delim($str); | |
3928 | return "$q$str" if $succeed; | |
3929 | } | |
3930 | for my $delim ('/', '"', '#') { | |
6e90668e SM |
3931 | return "$q$delim" . $str . $delim if index($str, $delim) == -1; |
3932 | } | |
90be192f SM |
3933 | if ($default) { |
3934 | $str =~ s/$default/\\$default/g; | |
3935 | return "$default$str$default"; | |
3936 | } else { | |
3937 | $str =~ s[/][\\/]g; | |
3938 | return "$q/$str/"; | |
3939 | } | |
6e90668e SM |
3940 | } |
3941 | ||
d989cdac SM |
3942 | my $max_prec; |
3943 | BEGIN { $max_prec = int(0.999 + 8*length(pack("F", 42))*log(2)/log(10)); } | |
3944 | ||
3945 | # Split a floating point number into an integer mantissa and a binary | |
3946 | # exponent. Assumes you've already made sure the number isn't zero or | |
3947 | # some weird infinity or NaN. | |
3948 | sub split_float { | |
3949 | my($f) = @_; | |
3950 | my $exponent = 0; | |
3951 | if ($f == int($f)) { | |
3952 | while ($f % 2 == 0) { | |
3953 | $f /= 2; | |
3954 | $exponent++; | |
3955 | } | |
3956 | } else { | |
3957 | while ($f != int($f)) { | |
3958 | $f *= 2; | |
3959 | $exponent--; | |
3960 | } | |
3961 | } | |
3962 | my $mantissa = sprintf("%.0f", $f); | |
3963 | return ($mantissa, $exponent); | |
3964 | } | |
3965 | ||
6e90668e | 3966 | sub const { |
d989cdac SM |
3967 | my $self = shift; |
3968 | my($sv, $cx) = @_; | |
3969 | if ($self->{'use_dumper'}) { | |
3970 | return $self->const_dumper($sv, $cx); | |
3971 | } | |
6e90668e | 3972 | if (class($sv) eq "SPECIAL") { |
d989cdac SM |
3973 | # sv_undef, sv_yes, sv_no |
3974 | return ('undef', '1', $self->maybe_parens("!1", $cx, 21))[$$sv-1]; | |
805b1011 DM |
3975 | } |
3976 | if (class($sv) eq "NULL") { | |
7a9b44b9 | 3977 | return 'undef'; |
805b1011 | 3978 | } |
127212b2 DM |
3979 | # convert a version object into the "v1.2.3" string in its V magic |
3980 | if ($sv->FLAGS & SVs_RMG) { | |
3981 | for (my $mg = $sv->MAGIC; $mg; $mg = $mg->MOREMAGIC) { | |
3982 | return $mg->PTR if $mg->TYPE eq 'V'; | |
3983 | } | |
3984 | } | |
3985 | ||
3986 | if ($sv->FLAGS & SVf_IOK) { | |
d989cdac SM |
3987 | my $str = $sv->int_value; |
3988 | $str = $self->maybe_parens($str, $cx, 21) if $str < 0; | |
3989 | return $str; | |
6e90668e | 3990 | } elsif ($sv->FLAGS & SVf_NOK) { |
d989cdac SM |
3991 | my $nv = $sv->NV; |
3992 | if ($nv == 0) { | |
3993 | if (pack("F", $nv) eq pack("F", 0)) { | |
3994 | # positive zero | |
3995 | return "0"; | |
3996 | } else { | |
3997 | # negative zero | |
3998 | return $self->maybe_parens("-.0", $cx, 21); | |
3999 | } | |
4000 | } elsif (1/$nv == 0) { | |
4001 | if ($nv > 0) { | |
4002 | # positive infinity | |
4003 | return $self->maybe_parens("9**9**9", $cx, 22); | |
4004 | } else { | |
4005 | # negative infinity | |
4006 | return $self->maybe_parens("-9**9**9", $cx, 21); | |
4007 | } | |
4008 | } elsif ($nv != $nv) { | |
4009 | # NaN | |
4010 | if (pack("F", $nv) eq pack("F", sin(9**9**9))) { | |
4011 | # the normal kind | |
4012 | return "sin(9**9**9)"; | |
4013 | } elsif (pack("F", $nv) eq pack("F", -sin(9**9**9))) { | |
4014 | # the inverted kind | |
4015 | return $self->maybe_parens("-sin(9**9**9)", $cx, 21); | |
4016 | } else { | |
4017 | # some other kind | |
4018 | my $hex = unpack("h*", pack("F", $nv)); | |
4019 | return qq'unpack("F", pack("h*", "$hex"))'; | |
4020 | } | |
fecea806 | 4021 | } |
d989cdac SM |
4022 | # first, try the default stringification |
4023 | my $str = "$nv"; | |
4024 | if ($str != $nv) { | |
4025 | # failing that, try using more precision | |
4026 | $str = sprintf("%.${max_prec}g", $nv); | |
4027 | # if (pack("F", $str) ne pack("F", $nv)) { | |
4028 | if ($str != $nv) { | |
4029 | # not representable in decimal with whatever sprintf() | |
4030 | # and atof() Perl is using here. | |
4031 | my($mant, $exp) = split_float($nv); | |
4032 | return $self->maybe_parens("$mant * 2**$exp", $cx, 19); | |
4033 | } | |
4034 | } | |
4035 | $str = $self->maybe_parens($str, $cx, 21) if $nv < 0; | |
4036 | return $str; | |
7a9b44b9 | 4037 | } elsif ($sv->FLAGS & SVf_ROK && $sv->can("RV")) { |
d989cdac SM |
4038 | my $ref = $sv->RV; |
4039 | if (class($ref) eq "AV") { | |
4040 | return "[" . $self->list_const(2, $ref->ARRAY) . "]"; | |
4041 | } elsif (class($ref) eq "HV") { | |
4042 | my %hash = $ref->ARRAY; | |
4043 | my @elts; | |
4044 | for my $k (sort keys %hash) { | |
4045 | push @elts, "$k => " . $self->const($hash{$k}, 6); | |
4046 | } | |
4047 | return "{" . join(", ", @elts) . "}"; | |
4048 | } elsif (class($ref) eq "CV") { | |
1a35f9ff | 4049 | BEGIN { |
86d8ec8a | 4050 | if ($] > 5.0150051) { |
1a35f9ff FC |
4051 | require overloading; |
4052 | unimport overloading; | |
86d8ec8a | 4053 | } |
1a35f9ff | 4054 | } |
86d8ec8a | 4055 | if ($] > 5.0150051 && $self->{curcv} && |
1a35f9ff FC |
4056 | $self->{curcv}->object_2svref == $ref->object_2svref) { |
4057 | return $self->keyword("__SUB__"); | |
4058 | } | |
d989cdac SM |
4059 | return "sub " . $self->deparse_sub($ref); |
4060 | } | |
4061 | if ($ref->FLAGS & SVs_SMG) { | |
4062 | for (my $mg = $ref->MAGIC; $mg; $mg = $mg->MOREMAGIC) { | |
4063 | if ($mg->TYPE eq 'r') { | |
4064 | my $re = re_uninterp(escape_str(re_unback($mg->precomp))); | |
4065 | return single_delim("qr", "", $re); | |
4066 | } | |
4067 | } | |
4068 | } | |
4069 | ||
4070 | return $self->maybe_parens("\\" . $self->const($ref, 20), $cx, 20); | |
34768ba5 | 4071 | } elsif ($sv->FLAGS & SVf_POK) { |
6e90668e | 4072 | my $str = $sv->PV; |
2f3efc97 | 4073 | if ($str =~ /[[:^print:]]/) { |
9d2c6865 | 4074 | return single_delim("qq", '"', uninterp escape_str unback $str); |
6e90668e | 4075 | } else { |
bd0865ec | 4076 | return single_delim("q", "'", unback $str); |
6e90668e | 4077 | } |
34768ba5 RH |
4078 | } else { |
4079 | return "undef"; | |
a798dbf2 MB |
4080 | } |
4081 | } | |
4082 | ||
d989cdac SM |
4083 | sub const_dumper { |
4084 | my $self = shift; | |
4085 | my($sv, $cx) = @_; | |
4086 | my $ref = $sv->object_2svref(); | |
4087 | my $dumper = Data::Dumper->new([$$ref], ['$v']); | |
4088 | $dumper->Purity(1)->Terse(1)->Deparse(1)->Indent(0)->Useqq(1)->Sortkeys(1); | |
4089 | my $str = $dumper->Dump(); | |
4090 | if ($str =~ /^\$v/) { | |
4091 | return '${my ' . $str . ' \$v}'; | |
4092 | } else { | |
4093 | return $str; | |
4094 | } | |
4095 | } | |
4096 | ||
18228111 GS |
4097 | sub const_sv { |
4098 | my $self = shift; | |
4099 | my $op = shift; | |
4100 | my $sv = $op->sv; | |
4101 | # the constant could be in the pad (under useithreads) | |
4102 | $sv = $self->padval($op->targ) unless $$sv; | |
4103 | return $sv; | |
4104 | } | |
4105 | ||
6e90668e SM |
4106 | sub pp_const { |
4107 | my $self = shift; | |
9d2c6865 | 4108 | my($op, $cx) = @_; |
bc6b2ef6 Z |
4109 | if ($op->private & OPpCONST_ARYBASE) { |
4110 | return '$['; | |
4111 | } | |
e38ccfd9 | 4112 | # if ($op->private & OPpCONST_BARE) { # trouble with '=>' autoquoting |
18228111 | 4113 | # return $self->const_sv($op)->PV; |
6e90668e | 4114 | # } |
18228111 | 4115 | my $sv = $self->const_sv($op); |
d989cdac | 4116 | return $self->const($sv, $cx); |
6e90668e SM |
4117 | } |
4118 | ||
4119 | sub dq { | |
4120 | my $self = shift; | |
4121 | my $op = shift; | |
3f872cb9 GS |
4122 | my $type = $op->name; |
4123 | if ($type eq "const") { | |
bc6b2ef6 | 4124 | return '$[' if $op->private & OPpCONST_ARYBASE; |
f3402b25 | 4125 | return uninterp(escape_str(unback($self->const_sv($op)->as_string))); |
3f872cb9 | 4126 | } elsif ($type eq "concat") { |
8fed1104 RH |
4127 | my $first = $self->dq($op->first); |
4128 | my $last = $self->dq($op->last); | |
44c6d2a1 | 4129 | |
333f3d7a | 4130 | # Disambiguate "${foo}bar", "${foo}{bar}", "${foo}[1]", "$foo\::bar" |
44c6d2a1 RH |
4131 | ($last =~ /^[A-Z\\\^\[\]_?]/ && |
4132 | $first =~ s/([\$@])\^$/${1}{^}/) # "${^}W" etc | |
d989cdac | 4133 | || ($last =~ /^[:'{\[\w_]/ && #' |
44c6d2a1 RH |
4134 | $first =~ s/([\$@])([A-Za-z_]\w*)$/${1}{$2}/); |
4135 | ||
8fed1104 | 4136 | return $first . $last; |
3f872cb9 | 4137 | } elsif ($type eq "uc") { |
6e90668e | 4138 | return '\U' . $self->dq($op->first->sibling) . '\E'; |
3f872cb9 | 4139 | } elsif ($type eq "lc") { |
6e90668e | 4140 | return '\L' . $self->dq($op->first->sibling) . '\E'; |
3f872cb9 | 4141 | } elsif ($type eq "ucfirst") { |
6e90668e | 4142 | return '\u' . $self->dq($op->first->sibling); |
3f872cb9 | 4143 | } elsif ($type eq "lcfirst") { |
6e90668e | 4144 | return '\l' . $self->dq($op->first->sibling); |
3f872cb9 | 4145 | } elsif ($type eq "quotemeta") { |
6e90668e | 4146 | return '\Q' . $self->dq($op->first->sibling) . '\E'; |
838f2281 BF |
4147 | } elsif ($type eq "fc") { |
4148 | return '\F' . $self->dq($op->first->sibling) . '\E'; | |
3f872cb9 | 4149 | } elsif ($type eq "join") { |
9d2c6865 | 4150 | return $self->deparse($op->last, 26); # was join($", @ary) |
a798dbf2 | 4151 | } else { |
9d2c6865 | 4152 | return $self->deparse($op, 26); |
6e90668e SM |
4153 | } |
4154 | } | |
4155 | ||
4156 | sub pp_backtick { | |
4157 | my $self = shift; | |
9d2c6865 | 4158 | my($op, $cx) = @_; |
85675c4c RGS |
4159 | # skip pushmark if it exists (readpipe() vs ``) |
4160 | my $child = $op->first->sibling->isa('B::NULL') | |
c53941b4 | 4161 | ? $op->first : $op->first->sibling; |
5d8c42c2 FC |
4162 | if ($self->pure_string($child)) { |
4163 | return single_delim("qx", '`', $self->dq($child, 1)); | |
4164 | } | |
4165 | unop($self, @_, "readpipe"); | |
6e90668e SM |
4166 | } |
4167 | ||
4168 | sub dquote { | |
4169 | my $self = shift; | |
6f611a1a | 4170 | my($op, $cx) = @_; |
3ed82cfc GS |
4171 | my $kid = $op->first->sibling; # skip ex-stringify, pushmark |
4172 | return $self->deparse($kid, $cx) if $self->{'unquote'}; | |
4173 | $self->maybe_targmy($kid, $cx, | |
4174 | sub {single_delim("qq", '"', $self->dq($_[1]))}); | |
6e90668e SM |
4175 | } |
4176 | ||
bd0865ec | 4177 | # OP_STRINGIFY is a listop, but it only ever has one arg |
3ed82cfc | 4178 | sub pp_stringify { maybe_targmy(@_, \&dquote) } |
6e90668e SM |
4179 | |
4180 | # tr/// and s/// (and tr[][], tr[]//, tr###, etc) | |
4181 | # note that tr(from)/to/ is OK, but not tr/from/(to) | |
4182 | sub double_delim { | |
4183 | my($from, $to) = @_; | |
4184 | my($succeed, $delim); | |
4185 | if ($from !~ m[/] and $to !~ m[/]) { | |
4186 | return "/$from/$to/"; | |
4187 | } elsif (($succeed, $from) = balanced_delim($from) and $succeed) { | |
4188 | if (($succeed, $to) = balanced_delim($to) and $succeed) { | |
4189 | return "$from$to"; | |
4190 | } else { | |
6b0bcbb1 | 4191 | for $delim ('/', '"', '#') { # note no "'" -- s''' is special |
6e90668e SM |
4192 | return "$from$delim$to$delim" if index($to, $delim) == -1; |
4193 | } | |
4194 | $to =~ s[/][\\/]g; | |
4195 | return "$from/$to/"; | |
4196 | } | |
4197 | } else { | |
4198 | for $delim ('/', '"', '#') { # note no ' | |
4199 | return "$delim$from$delim$to$delim" | |
4200 | if index($to . $from, $delim) == -1; | |
4201 | } | |
4202 | $from =~ s[/][\\/]g; | |
4203 | $to =~ s[/][\\/]g; | |
4204 | return "/$from/$to/"; | |
4205 | } | |
4206 | } | |
4207 | ||
2a9e2f8a | 4208 | # Only used by tr///, so backslashes hyphens |
6e90668e SM |
4209 | sub pchr { # ASCII |
4210 | my($n) = @_; | |
4211 | if ($n == ord '\\') { | |
4212 | return '\\\\'; | |
2a9e2f8a RH |
4213 | } elsif ($n == ord "-") { |
4214 | return "\\-"; | |
6e90668e SM |
4215 | } elsif ($n >= ord(' ') and $n <= ord('~')) { |
4216 | return chr($n); | |
4217 | } elsif ($n == ord "\a") { | |
4218 | return '\\a'; | |
4219 | } elsif ($n == ord "\b") { | |
4220 | return '\\b'; | |
4221 | } elsif ($n == ord "\t") { | |
4222 | return '\\t'; | |
4223 | } elsif ($n == ord "\n") { | |
4224 | return '\\n'; | |
4225 | } elsif ($n == ord "\e") { | |
4226 | return '\\e'; | |
4227 | } elsif ($n == ord "\f") { | |
4228 | return '\\f'; | |
4229 | } elsif ($n == ord "\r") { | |
4230 | return '\\r'; | |
4231 | } elsif ($n >= ord("\cA") and $n <= ord("\cZ")) { | |
4232 | return '\\c' . chr(ord("@") + $n); | |
4233 | } else { | |
4234 | # return '\x' . sprintf("%02x", $n); | |
4235 | return '\\' . sprintf("%03o", $n); | |
4236 | } | |
4237 | } | |
4238 | ||
4239 | sub collapse { | |
4240 | my(@chars) = @_; | |
23db111c | 4241 | my($str, $c, $tr) = (""); |
6e90668e SM |
4242 | for ($c = 0; $c < @chars; $c++) { |
4243 | $tr = $chars[$c]; | |
4244 | $str .= pchr($tr); | |
4245 | if ($c <= $#chars - 2 and $chars[$c + 1] == $tr + 1 and | |
4246 | $chars[$c + 2] == $tr + 2) | |
4247 | { | |
f4a44678 SM |
4248 | for (; $c <= $#chars-1 and $chars[$c + 1] == $chars[$c] + 1; $c++) |
4249 | {} | |
6e90668e SM |
4250 | $str .= "-"; |
4251 | $str .= pchr($chars[$c]); | |
4252 | } | |
4253 | } | |
4254 | return $str; | |
4255 | } | |
4256 | ||
f4a44678 SM |
4257 | sub tr_decode_byte { |
4258 | my($table, $flags) = @_; | |
2a9e2f8a | 4259 | my(@table) = unpack("s*", $table); |
bec89253 | 4260 | splice @table, 0x100, 1; # Number of subsequent elements |
6e90668e | 4261 | my($c, $tr, @from, @to, @delfrom, $delhyphen); |
d989cdac | 4262 | if ($table[ord "-"] != -1 and |
6e90668e SM |
4263 | $table[ord("-") - 1] == -1 || $table[ord("-") + 1] == -1) |
4264 | { | |
4265 | $tr = $table[ord "-"]; | |
4266 | $table[ord "-"] = -1; | |
4267 | if ($tr >= 0) { | |
4268 | @from = ord("-"); | |
4269 | @to = $tr; | |
4270 | } else { # -2 ==> delete | |
4271 | $delhyphen = 1; | |
4272 | } | |
4273 | } | |
2a9e2f8a | 4274 | for ($c = 0; $c < @table; $c++) { |
6e90668e SM |
4275 | $tr = $table[$c]; |
4276 | if ($tr >= 0) { | |
4277 | push @from, $c; push @to, $tr; | |
4278 | } elsif ($tr == -2) { | |
4279 | push @delfrom, $c; | |
4280 | } | |
4281 | } | |
6e90668e | 4282 | @from = (@from, @delfrom); |
f4a44678 | 4283 | if ($flags & OPpTRANS_COMPLEMENT) { |
6e90668e SM |
4284 | my @newfrom = (); |
4285 | my %from; | |
4286 | @from{@from} = (1) x @from; | |
4287 | for ($c = 0; $c < 256; $c++) { | |
4288 | push @newfrom, $c unless $from{$c}; | |
4289 | } | |
4290 | @from = @newfrom; | |
4291 | } | |
56d8b52c | 4292 | unless ($flags & OPpTRANS_DELETE || !@to) { |
6e90668e SM |
4293 | pop @to while $#to and $to[$#to] == $to[$#to -1]; |
4294 | } | |
6e90668e SM |
4295 | my($from, $to); |
4296 | $from = collapse(@from); | |
4297 | $to = collapse(@to); | |
4298 | $from .= "-" if $delhyphen; | |
f4a44678 SM |
4299 | return ($from, $to); |
4300 | } | |
4301 | ||
4302 | sub tr_chr { | |
4303 | my $x = shift; | |
4304 | if ($x == ord "-") { | |
4305 | return "\\-"; | |
2a9e2f8a RH |
4306 | } elsif ($x == ord "\\") { |
4307 | return "\\\\"; | |
f4a44678 SM |
4308 | } else { |
4309 | return chr $x; | |
4310 | } | |
4311 | } | |
4312 | ||
4313 | # XXX This doesn't yet handle all cases correctly either | |
4314 | ||
4315 | sub tr_decode_utf8 { | |
4316 | my($swash_hv, $flags) = @_; | |
4317 | my %swash = $swash_hv->ARRAY; | |
4318 | my $final = undef; | |
4319 | $final = $swash{'FINAL'}->IV if exists $swash{'FINAL'}; | |
4320 | my $none = $swash{"NONE"}->IV; | |
4321 | my $extra = $none + 1; | |
4322 | my(@from, @delfrom, @to); | |
4323 | my $line; | |
4324 | foreach $line (split /\n/, $swash{'LIST'}->PV) { | |
4325 | my($min, $max, $result) = split(/\t/, $line); | |
4326 | $min = hex $min; | |
4327 | if (length $max) { | |
4328 | $max = hex $max; | |
4329 | } else { | |
4330 | $max = $min; | |
4331 | } | |
4332 | $result = hex $result; | |
4333 | if ($result == $extra) { | |
d989cdac | 4334 | push @delfrom, [$min, $max]; |
f4a44678 SM |
4335 | } else { |
4336 | push @from, [$min, $max]; | |
4337 | push @to, [$result, $result + $max - $min]; | |
4338 | } | |
4339 | } | |
4340 | for my $i (0 .. $#from) { | |
4341 | if ($from[$i][0] == ord '-') { | |
4342 | unshift @from, splice(@from, $i, 1); | |
4343 | unshift @to, splice(@to, $i, 1); | |
4344 | last; | |
4345 | } elsif ($from[$i][1] == ord '-') { | |
4346 | $from[$i][1]--; | |
4347 | $to[$i][1]--; | |
4348 | unshift @from, ord '-'; | |
4349 | unshift @to, ord '-'; | |
4350 | last; | |
4351 | } | |
4352 | } | |
4353 | for my $i (0 .. $#delfrom) { | |
4354 | if ($delfrom[$i][0] == ord '-') { | |
4355 | push @delfrom, splice(@delfrom, $i, 1); | |
4356 | last; | |
4357 | } elsif ($delfrom[$i][1] == ord '-') { | |
4358 | $delfrom[$i][1]--; | |
4359 | push @delfrom, ord '-'; | |
4360 | last; | |
4361 | } | |
4362 | } | |
4363 | if (defined $final and $to[$#to][1] != $final) { | |
4364 | push @to, [$final, $final]; | |
4365 | } | |
4366 | push @from, @delfrom; | |
4367 | if ($flags & OPpTRANS_COMPLEMENT) { | |
4368 | my @newfrom; | |
4369 | my $next = 0; | |
4370 | for my $i (0 .. $#from) { | |
4371 | push @newfrom, [$next, $from[$i][0] - 1]; | |
4372 | $next = $from[$i][1] + 1; | |
4373 | } | |
4374 | @from = (); | |
4375 | for my $range (@newfrom) { | |
4376 | if ($range->[0] <= $range->[1]) { | |
4377 | push @from, $range; | |
4378 | } | |
4379 | } | |
4380 | } | |
4381 | my($from, $to, $diff); | |
4382 | for my $chunk (@from) { | |
4383 | $diff = $chunk->[1] - $chunk->[0]; | |
4384 | if ($diff > 1) { | |
4385 | $from .= tr_chr($chunk->[0]) . "-" . tr_chr($chunk->[1]); | |
4386 | } elsif ($diff == 1) { | |
4387 | $from .= tr_chr($chunk->[0]) . tr_chr($chunk->[1]); | |
4388 | } else { | |
4389 | $from .= tr_chr($chunk->[0]); | |
4390 | } | |
4391 | } | |
4392 | for my $chunk (@to) { | |
4393 | $diff = $chunk->[1] - $chunk->[0]; | |
4394 | if ($diff > 1) { | |
4395 | $to .= tr_chr($chunk->[0]) . "-" . tr_chr($chunk->[1]); | |
4396 | } elsif ($diff == 1) { | |
4397 | $to .= tr_chr($chunk->[0]) . tr_chr($chunk->[1]); | |
4398 | } else { | |
4399 | $to .= tr_chr($chunk->[0]); | |
4400 | } | |
4401 | } | |
4402 | #$final = sprintf("%04x", $final) if defined $final; | |
4403 | #$none = sprintf("%04x", $none) if defined $none; | |
d989cdac | 4404 | #$extra = sprintf("%04x", $extra) if defined $extra; |
f4a44678 SM |
4405 | #print STDERR "final: $final\n none: $none\nextra: $extra\n"; |
4406 | #print STDERR $swash{'LIST'}->PV; | |
4407 | return (escape_str($from), escape_str($to)); | |
4408 | } | |
4409 | ||
4410 | sub pp_trans { | |
4411 | my $self = shift; | |
4412 | my($op, $cx) = @_; | |
4413 | my($from, $to); | |
cb8157e3 | 4414 | my $class = class($op); |
b031d0e6 | 4415 | my $priv_flags = $op->private; |
cb8157e3 | 4416 | if ($class eq "PVOP") { |
b031d0e6 | 4417 | ($from, $to) = tr_decode_byte($op->pv, $priv_flags); |
cb8157e3 FC |
4418 | } elsif ($class eq "PADOP") { |
4419 | ($from, $to) | |
b031d0e6 | 4420 | = tr_decode_utf8($self->padval($op->padix)->RV, $priv_flags); |
f4a44678 | 4421 | } else { # class($op) eq "SVOP" |
b031d0e6 | 4422 | ($from, $to) = tr_decode_utf8($op->sv->RV, $priv_flags); |
f4a44678 SM |
4423 | } |
4424 | my $flags = ""; | |
42b824d2 FC |
4425 | $flags .= "c" if $priv_flags & OPpTRANS_COMPLEMENT; |
4426 | $flags .= "d" if $priv_flags & OPpTRANS_DELETE; | |
f4a44678 | 4427 | $to = "" if $from eq $to and $flags eq ""; |
42b824d2 | 4428 | $flags .= "s" if $priv_flags & OPpTRANS_SQUASH; |
6e90668e SM |
4429 | return "tr" . double_delim($from, $to) . $flags; |
4430 | } | |
4431 | ||
bb16bae8 FC |
4432 | sub pp_transr { &pp_trans . 'r' } |
4433 | ||
03b22f1b RGS |
4434 | sub re_dq_disambiguate { |
4435 | my ($first, $last) = @_; | |
4436 | # Disambiguate "${foo}bar", "${foo}{bar}", "${foo}[1]" | |
4437 | ($last =~ /^[A-Z\\\^\[\]_?]/ && | |
4438 | $first =~ s/([\$@])\^$/${1}{^}/) # "${^}W" etc | |
4439 | || ($last =~ /^[{\[\w_]/ && | |
4440 | $first =~ s/([\$@])([A-Za-z_]\w*)$/${1}{$2}/); | |
4441 | return $first . $last; | |
4442 | } | |
4443 | ||
6e90668e SM |
4444 | # Like dq(), but different |
4445 | sub re_dq { | |
4446 | my $self = shift; | |
a9760014 RH |
4447 | my ($op, $extended) = @_; |
4448 | ||
3f872cb9 GS |
4449 | my $type = $op->name; |
4450 | if ($type eq "const") { | |
bc6b2ef6 | 4451 | return '$[' if $op->private & OPpCONST_ARYBASE; |
a9760014 RH |
4452 | my $unbacked = re_unback($self->const_sv($op)->as_string); |
4453 | return re_uninterp_extended(escape_extended_re($unbacked)) | |
4454 | if $extended; | |
4455 | return re_uninterp(escape_str($unbacked)); | |
3f872cb9 | 4456 | } elsif ($type eq "concat") { |
a9760014 RH |
4457 | my $first = $self->re_dq($op->first, $extended); |
4458 | my $last = $self->re_dq($op->last, $extended); | |
03b22f1b | 4459 | return re_dq_disambiguate($first, $last); |
3f872cb9 | 4460 | } elsif ($type eq "uc") { |
a9760014 | 4461 | return '\U' . $self->re_dq($op->first->sibling, $extended) . '\E'; |
3f872cb9 | 4462 | } elsif ($type eq "lc") { |
a9760014 | 4463 | return '\L' . $self->re_dq($op->first->sibling, $extended) . '\E'; |
3f872cb9 | 4464 | } elsif ($type eq "ucfirst") { |
a9760014 | 4465 | return '\u' . $self->re_dq($op->first->sibling, $extended); |
3f872cb9 | 4466 | } elsif ($type eq "lcfirst") { |
a9760014 | 4467 | return '\l' . $self->re_dq($op->first->sibling, $extended); |
3f872cb9 | 4468 | } elsif ($type eq "quotemeta") { |
a9760014 | 4469 | return '\Q' . $self->re_dq($op->first->sibling, $extended) . '\E'; |
838f2281 BF |
4470 | } elsif ($type eq "fc") { |
4471 | return '\F' . $self->re_dq($op->first->sibling, $extended) . '\E'; | |
3f872cb9 | 4472 | } elsif ($type eq "join") { |
9d2c6865 | 4473 | return $self->deparse($op->last, 26); # was join($", @ary) |
6e90668e | 4474 | } else { |
337d7381 FC |
4475 | my $ret = $self->deparse($op, 26); |
4476 | $ret =~ s/^\$([(|)])\z/\${$1}/; # $( $| $) need braces | |
4477 | return $ret; | |
6e90668e SM |
4478 | } |
4479 | } | |
4480 | ||
a9760014 RH |
4481 | sub pure_string { |
4482 | my ($self, $op) = @_; | |
64b007ad | 4483 | return 0 if null $op; |
a9760014 RH |
4484 | my $type = $op->name; |
4485 | ||
36727b53 | 4486 | if ($type eq 'const' || $type eq 'av2arylen') { |
a9760014 RH |
4487 | return 1; |
4488 | } | |
838f2281 | 4489 | elsif ($type =~ /^(?:[ul]c(first)?|fc)$/ || $type eq 'quotemeta') { |
a9760014 RH |
4490 | return $self->pure_string($op->first->sibling); |
4491 | } | |
4492 | elsif ($type eq 'join') { | |
4493 | my $join_op = $op->first->sibling; # Skip pushmark | |
4494 | return 0 unless $join_op->name eq 'null' && $join_op->targ eq OP_RV2SV; | |
4495 | ||
4496 | my $gvop = $join_op->first; | |
4497 | return 0 unless $gvop->name eq 'gvsv'; | |
4498 | return 0 unless '"' eq $self->gv_name($self->gv_or_padgv($gvop)); | |
4499 | ||
4500 | return 0 unless ${$join_op->sibling} eq ${$op->last}; | |
21b7468a | 4501 | return 0 unless $op->last->name =~ /^(?:[ah]slice|(?:rv2|pad)av)$/; |
a9760014 RH |
4502 | } |
4503 | elsif ($type eq 'concat') { | |
4504 | return $self->pure_string($op->first) | |
4505 | && $self->pure_string($op->last); | |
4506 | } | |
d989cdac SM |
4507 | elsif (is_scalar($op) || $type =~ /^[ah]elem$/) { |
4508 | return 1; | |
4509 | } | |
8e0b04c7 | 4510 | elsif ($type eq "null" and $op->can('first') and not null $op->first and |
4b58603b | 4511 | ($op->first->name eq "null" and $op->first->can('first') |
8e0b04c7 | 4512 | and not null $op->first->first and |
4b58603b FC |
4513 | $op->first->first->name eq "aelemfast" |
4514 | or | |
4515 | $op->first->name =~ /^aelemfast(?:_lex)?\z/ | |
4516 | )) { | |
a9760014 RH |
4517 | return 1; |
4518 | } | |
4519 | else { | |
4520 | return 0; | |
4521 | } | |
4522 | ||
4523 | return 1; | |
4524 | } | |
4525 | ||
4526 | sub regcomp { | |
6e90668e | 4527 | my $self = shift; |
a9760014 | 4528 | my($op, $cx, $extended) = @_; |
6e90668e | 4529 | my $kid = $op->first; |
3f872cb9 GS |
4530 | $kid = $kid->first if $kid->name eq "regcmaybe"; |
4531 | $kid = $kid->first if $kid->name eq "regcreset"; | |
131b3ad0 DM |
4532 | if ($kid->name eq "null" and !null($kid->first) |
4533 | and $kid->first->name eq 'pushmark') | |
4534 | { | |
4535 | my $str = ''; | |
4536 | $kid = $kid->first->sibling; | |
4537 | while (!null($kid)) { | |
03b22f1b RGS |
4538 | my $first = $str; |
4539 | my $last = $self->re_dq($kid, $extended); | |
4540 | $str = re_dq_disambiguate($first, $last); | |
131b3ad0 DM |
4541 | $kid = $kid->sibling; |
4542 | } | |
4543 | return $str, 1; | |
4544 | } | |
4545 | ||
a9760014 RH |
4546 | return ($self->re_dq($kid, $extended), 1) if $self->pure_string($kid); |
4547 | return ($self->deparse($kid, $cx), 0); | |
4548 | } | |
4549 | ||
4550 | sub pp_regcomp { | |
4551 | my ($self, $op, $cx) = @_; | |
4552 | return (($self->regcomp($op, $cx, 0))[0]); | |
6e90668e SM |
4553 | } |
4554 | ||
c3ae113d FC |
4555 | sub re_flags { |
4556 | my ($self, $op) = @_; | |
4557 | my $flags = ''; | |
4558 | my $pmflags = $op->pmflags; | |
4559 | $flags .= "g" if $pmflags & PMf_GLOBAL; | |
4560 | $flags .= "i" if $pmflags & PMf_FOLD; | |
4561 | $flags .= "m" if $pmflags & PMf_MULTILINE; | |
4562 | $flags .= "o" if $pmflags & PMf_KEEP; | |
4563 | $flags .= "s" if $pmflags & PMf_SINGLELINE; | |
4564 | $flags .= "x" if $pmflags & PMf_EXTENDED; | |
4565 | $flags .= "p" if $pmflags & RXf_PMf_KEEPCOPY; | |
4566 | if (my $charset = $pmflags & RXf_PMf_CHARSET) { | |
4567 | # Hardcoding this is fragile, but B does not yet export the | |
4568 | # constants we need. | |
4569 | $flags .= qw(d l u a aa)[$charset >> 5] | |
4570 | } | |
4571 | # The /d flag is indicated by 0; only show it if necessary. | |
4572 | elsif ($self->{hinthash} and | |
4573 | $self->{hinthash}{reflags_charset} | |
dff5ffe4 | 4574 | || $self->{hinthash}{feature_unicode} |
149758b3 NC |
4575 | or $self->{hints} & $feature::hint_mask |
4576 | && ($self->{hints} & $feature::hint_mask) | |
4577 | != $feature::hint_mask | |
dff5ffe4 | 4578 | && do { |
dff5ffe4 FC |
4579 | $self->{hints} & $feature::hint_uni8bit; |
4580 | } | |
4581 | ) { | |
c3ae113d FC |
4582 | $flags .= 'd'; |
4583 | } | |
4584 | $flags; | |
4585 | } | |
4586 | ||
6e90668e SM |
4587 | # osmic acid -- see osmium tetroxide |
4588 | ||
4589 | my %matchwords; | |
4590 | map($matchwords{join "", sort split //, $_} = $_, 'cig', 'cog', 'cos', 'cogs', | |
d989cdac SM |
4591 | 'cox', 'go', 'is', 'ism', 'iso', 'mig', 'mix', 'osmic', 'ox', 'sic', |
4592 | 'sig', 'six', 'smog', 'so', 'soc', 'sog', 'xi'); | |
6e90668e | 4593 | |
90be192f | 4594 | sub matchop { |
6e90668e | 4595 | my $self = shift; |
90be192f | 4596 | my($op, $cx, $name, $delim) = @_; |
6e90668e | 4597 | my $kid = $op->first; |
9d2c6865 | 4598 | my ($binop, $var, $re) = ("", "", ""); |
6e90668e | 4599 | if ($op->flags & OPf_STACKED) { |
9d2c6865 SM |
4600 | $binop = 1; |
4601 | $var = $self->deparse($kid, 20); | |
6e90668e SM |
4602 | $kid = $kid->sibling; |
4603 | } | |
a9760014 | 4604 | my $quote = 1; |
86e39c78 FC |
4605 | my $pmflags = $op->pmflags; |
4606 | my $extended = ($pmflags & PMf_EXTENDED); | |
a539498a | 4607 | my $rhs_bound_to_defsv; |
6e90668e | 4608 | if (null $kid) { |
a9760014 RH |
4609 | my $unbacked = re_unback($op->precomp); |
4610 | if ($extended) { | |
4611 | $re = re_uninterp_extended(escape_extended_re($unbacked)); | |
4612 | } else { | |
4613 | $re = re_uninterp(escape_str(re_unback($op->precomp))); | |
4614 | } | |
4615 | } elsif ($kid->name ne 'regcomp') { | |
ff97752d | 4616 | carp("found ".$kid->name." where regcomp expected"); |
6e90668e | 4617 | } else { |
d989cdac | 4618 | ($re, $quote) = $self->regcomp($kid, 21, $extended); |
7fb31b92 DM |
4619 | my $matchop = $kid->first; |
4620 | if ($matchop->name eq 'regcrest') { | |
4621 | $matchop = $matchop->first; | |
4622 | } | |
5e5a1632 FC |
4623 | if ($matchop->name =~ /^(?:match|transr?|subst)\z/ |
4624 | && $matchop->flags & OPf_SPECIAL) { | |
4625 | $rhs_bound_to_defsv = 1; | |
4626 | } | |
6e90668e SM |
4627 | } |
4628 | my $flags = ""; | |
5992ca2b | 4629 | $flags .= "c" if $pmflags & PMf_CONTINUE; |
c3ae113d FC |
4630 | $flags .= $self->re_flags($op); |
4631 | $flags = join '', sort split //, $flags; | |
6e90668e | 4632 | $flags = $matchwords{$flags} if $matchwords{$flags}; |
5992ca2b | 4633 | if ($pmflags & PMf_ONCE) { # only one kind of delimiter works here |
6e90668e | 4634 | $re =~ s/\?/\\?/g; |
9d2c6865 | 4635 | $re = "?$re?"; |
a9760014 | 4636 | } elsif ($quote) { |
90be192f | 4637 | $re = single_delim($name, $delim, $re); |
9d2c6865 | 4638 | } |
a9760014 | 4639 | $re = $re . $flags if $quote; |
9d2c6865 | 4640 | if ($binop) { |
a539498a FC |
4641 | return |
4642 | $self->maybe_parens( | |
4643 | $rhs_bound_to_defsv | |
4644 | ? "$var =~ (\$_ =~ $re)" | |
4645 | : "$var =~ $re", | |
4646 | $cx, 20 | |
4647 | ); | |
9d2c6865 SM |
4648 | } else { |
4649 | return $re; | |
6e90668e | 4650 | } |
6e90668e SM |
4651 | } |
4652 | ||
90be192f SM |
4653 | sub pp_match { matchop(@_, "m", "/") } |
4654 | sub pp_pushre { matchop(@_, "m", "/") } | |
4655 | sub pp_qr { matchop(@_, "qr", "") } | |
6e90668e | 4656 | |
84ed0108 FC |
4657 | sub pp_runcv { unop(@_, "__SUB__"); } |
4658 | ||
6e90668e SM |
4659 | sub pp_split { |
4660 | my $self = shift; | |
9d2c6865 | 4661 | my($op, $cx) = @_; |
6e90668e SM |
4662 | my($kid, @exprs, $ary, $expr); |
4663 | $kid = $op->first; | |
c6e79e55 SM |
4664 | |
4665 | # For our kid (an OP_PUSHRE), pmreplroot is never actually the | |
4666 | # root of a replacement; it's either empty, or abused to point to | |
4667 | # the GV for an array we split into (an optimization to save | |
4668 | # assignment overhead). Depending on whether we're using ithreads, | |
4669 | # this OP* holds either a GV* or a PADOFFSET. Luckily, B.xs | |
4670 | # figures out for us which it is. | |
3eaf8b6c | 4671 | my $replroot = $kid->pmreplroot; |
c6e79e55 SM |
4672 | my $gv = 0; |
4673 | if (ref($replroot) eq "B::GV") { | |
4674 | $gv = $replroot; | |
4675 | } elsif (!ref($replroot) and $replroot > 0) { | |
4676 | $gv = $self->padval($replroot); | |
6e90668e | 4677 | } |
bb8996b8 | 4678 | $ary = $self->stash_variable('@', $self->gv_name($gv), $cx) if $gv; |
c6e79e55 | 4679 | |
6e90668e | 4680 | for (; !null($kid); $kid = $kid->sibling) { |
9d2c6865 | 4681 | push @exprs, $self->deparse($kid, 6); |
6e90668e | 4682 | } |
fcd95d64 | 4683 | |
f86ea535 | 4684 | # handle special case of split(), and split(' ') that compiles to /\s+/ |
06fc6867 | 4685 | # Under 5.10, the reflags may be undef if the split regexp isn't a constant |
5255171e | 4686 | # Under 5.17.5+, the special flag is on split itself. |
fcd95d64 | 4687 | $kid = $op->first; |
5255171e FC |
4688 | if ( $op->flags & OPf_SPECIAL |
4689 | or | |
4690 | $kid->flags & OPf_SPECIAL | |
7c1f70cb | 4691 | and ( $] < 5.009 ? $kid->pmflags & PMf_SKIPWHITE() |
06fc6867 | 4692 | : ($kid->reflags || 0) & RXf_SKIPWHITE() ) ) { |
f86ea535 | 4693 | $exprs[0] = "' '"; |
fcd95d64 DD |
4694 | } |
4695 | ||
6e90668e SM |
4696 | $expr = "split(" . join(", ", @exprs) . ")"; |
4697 | if ($ary) { | |
9d2c6865 | 4698 | return $self->maybe_parens("$ary = $expr", $cx, 7); |
6e90668e SM |
4699 | } else { |
4700 | return $expr; | |
4701 | } | |
4702 | } | |
4703 | ||
4704 | # oxime -- any of various compounds obtained chiefly by the action of | |
4705 | # hydroxylamine on aldehydes and ketones and characterized by the | |
4706 | # bivalent grouping C=NOH [Webster's Tenth] | |
4707 | ||
4708 | my %substwords; | |
4709 | map($substwords{join "", sort split //, $_} = $_, 'ego', 'egoism', 'em', | |
4710 | 'es', 'ex', 'exes', 'gee', 'go', 'goes', 'ie', 'ism', 'iso', 'me', | |
4711 | 'meese', 'meso', 'mig', 'mix', 'os', 'ox', 'oxime', 'see', 'seem', | |
ddc6eb27 | 4712 | 'seg', 'sex', 'sig', 'six', 'smog', 'sog', 'some', 'xi', 'rogue', |
4f4d7508 DC |
4713 | 'sir', 'rise', 'smore', 'more', 'seer', 'rome', 'gore', 'grim', 'grime', |
4714 | 'or', 'rose', 'rosie'); | |
6e90668e SM |
4715 | |
4716 | sub pp_subst { | |
4717 | my $self = shift; | |
9d2c6865 | 4718 | my($op, $cx) = @_; |
6e90668e | 4719 | my $kid = $op->first; |
9d2c6865 | 4720 | my($binop, $var, $re, $repl) = ("", "", "", ""); |
6e90668e | 4721 | if ($op->flags & OPf_STACKED) { |
9d2c6865 SM |
4722 | $binop = 1; |
4723 | $var = $self->deparse($kid, 20); | |
6e90668e SM |
4724 | $kid = $kid->sibling; |
4725 | } | |
d989cdac | 4726 | my $flags = ""; |
86e39c78 | 4727 | my $pmflags = $op->pmflags; |
6e90668e | 4728 | if (null($op->pmreplroot)) { |
ef90d20a | 4729 | $repl = $kid; |
6e90668e SM |
4730 | $kid = $kid->sibling; |
4731 | } else { | |
4732 | $repl = $op->pmreplroot->first; # skip substcont | |
ef90d20a FC |
4733 | } |
4734 | while ($repl->name eq "entereval") { | |
6e90668e SM |
4735 | $repl = $repl->first; |
4736 | $flags .= "e"; | |
ef90d20a FC |
4737 | } |
4738 | if ($pmflags & PMf_EVAL) { | |
d989cdac | 4739 | $repl = $self->deparse($repl->first, 0); |
ef90d20a | 4740 | } else { |
bd0865ec | 4741 | $repl = $self->dq($repl); |
6e90668e | 4742 | } |
86e39c78 | 4743 | my $extended = ($pmflags & PMf_EXTENDED); |
6e90668e | 4744 | if (null $kid) { |
b7dad2dc RH |
4745 | my $unbacked = re_unback($op->precomp); |
4746 | if ($extended) { | |
4747 | $re = re_uninterp_extended(escape_extended_re($unbacked)); | |
4748 | } | |
4749 | else { | |
4750 | $re = re_uninterp(escape_str($unbacked)); | |
4751 | } | |
6e90668e | 4752 | } else { |
b7dad2dc | 4753 | ($re) = $self->regcomp($kid, 1, $extended); |
a798dbf2 | 4754 | } |
86e39c78 FC |
4755 | $flags .= "r" if $pmflags & PMf_NONDESTRUCT; |
4756 | $flags .= "e" if $pmflags & PMf_EVAL; | |
c3ae113d FC |
4757 | $flags .= $self->re_flags($op); |
4758 | $flags = join '', sort split //, $flags; | |
6e90668e | 4759 | $flags = $substwords{$flags} if $substwords{$flags}; |
9d2c6865 SM |
4760 | if ($binop) { |
4761 | return $self->maybe_parens("$var =~ s" | |
4762 | . double_delim($re, $repl) . $flags, | |
4763 | $cx, 20); | |
4764 | } else { | |
4765 | return "s". double_delim($re, $repl) . $flags; | |
4766 | } | |
a798dbf2 MB |
4767 | } |
4768 | ||
4769 | 1; | |
f6f9bdb7 SM |
4770 | __END__ |
4771 | ||
4772 | =head1 NAME | |
4773 | ||
4774 | B::Deparse - Perl compiler backend to produce perl code | |
4775 | ||
4776 | =head1 SYNOPSIS | |
4777 | ||
d989cdac | 4778 | B<perl> B<-MO=Deparse>[B<,-d>][B<,-f>I<FILE>][B<,-p>][B<,-q>][B<,-l>] |
646bba82 | 4779 | [B<,-s>I<LETTERS>][B<,-x>I<LEVEL>] I<prog.pl> |
f6f9bdb7 SM |
4780 | |
4781 | =head1 DESCRIPTION | |
4782 | ||
4783 | B::Deparse is a backend module for the Perl compiler that generates | |
4784 | perl source code, based on the internal compiled structure that perl | |
d4963d04 | 4785 | itself creates after parsing a program. The output of B::Deparse won't |
f6f9bdb7 SM |
4786 | be exactly the same as the original source, since perl doesn't keep |
4787 | track of comments or whitespace, and there isn't a one-to-one | |
4788 | correspondence between perl's syntactical constructions and their | |
d4963d04 | 4789 | compiled form, but it will often be close. When you use the B<-p> |
9d2c6865 SM |
4790 | option, the output also includes parentheses even when they are not |
4791 | required by precedence, which can make it easy to see if perl is | |
4792 | parsing your expressions the way you intended. | |
f6f9bdb7 | 4793 | |
d989cdac SM |
4794 | While B::Deparse goes to some lengths to try to figure out what your |
4795 | original program was doing, some parts of the language can still trip | |
d4963d04 | 4796 | it up; it still fails even on some parts of Perl's own test suite. If |
d989cdac SM |
4797 | you encounter a failure other than the most common ones described in |
4798 | the BUGS section below, you can help contribute to B::Deparse's | |
4799 | ongoing development by submitting a bug report with a small | |
4800 | example. | |
f6f9bdb7 SM |
4801 | |
4802 | =head1 OPTIONS | |
4803 | ||
9d2c6865 SM |
4804 | As with all compiler backend options, these must follow directly after |
4805 | the '-MO=Deparse', separated by a comma but not any white space. | |
f6f9bdb7 SM |
4806 | |
4807 | =over 4 | |
4808 | ||
d989cdac SM |
4809 | =item B<-d> |
4810 | ||
4811 | Output data values (when they appear as constants) using Data::Dumper. | |
4812 | Without this option, B::Deparse will use some simple routines of its | |
d4963d04 | 4813 | own for the same purpose. Currently, Data::Dumper is better for some |
d989cdac SM |
4814 | kinds of data (such as complex structures with sharing and |
4815 | self-reference) while the built-in routines are better for others | |
4816 | (such as odd floating-point values). | |
4817 | ||
4818 | =item B<-f>I<FILE> | |
4819 | ||
4820 | Normally, B::Deparse deparses the main code of a program, and all the subs | |
d4963d04 FC |
4821 | defined in the same file. To include subs defined in |
4822 | other files, pass the B<-f> option with the filename. | |
4823 | You can pass the B<-f> option several times, to | |
d989cdac SM |
4824 | include more than one secondary file. (Most of the time you don't want to |
4825 | use it at all.) You can also use this option to include subs which are | |
4826 | defined in the scope of a B<#line> directive with two parameters. | |
4827 | ||
bd0865ec GS |
4828 | =item B<-l> |
4829 | ||
4830 | Add '#line' declarations to the output based on the line and file | |
4831 | locations of the original code. | |
4832 | ||
9d2c6865 SM |
4833 | =item B<-p> |
4834 | ||
d4963d04 | 4835 | Print extra parentheses. Without this option, B::Deparse includes |
9d2c6865 | 4836 | parentheses in its output only when they are needed, based on the |
d4963d04 FC |
4837 | structure of your program. With B<-p>, it uses parentheses (almost) |
4838 | whenever they would be legal. This can be useful if you are used to | |
4839 | LISP, or if you want to see how perl parses your input. If you say | |
9d2c6865 | 4840 | |
d989cdac | 4841 | if ($var & 0x7f == 65) {print "Gimme an A!"} |
9d2c6865 SM |
4842 | print ($which ? $a : $b), "\n"; |
4843 | $name = $ENV{USER} or "Bob"; | |
4844 | ||
4845 | C<B::Deparse,-p> will print | |
4846 | ||
4847 | if (($var & 0)) { | |
4848 | print('Gimme an A!') | |
4849 | }; | |
4850 | (print(($which ? $a : $b)), '???'); | |
4851 | (($name = $ENV{'USER'}) or '???') | |
4852 | ||
4853 | which probably isn't what you intended (the C<'???'> is a sign that | |
4854 | perl optimized away a constant value). | |
4855 | ||
acaaef34 RGS |
4856 | =item B<-P> |
4857 | ||
d4963d04 FC |
4858 | Disable prototype checking. With this option, all function calls are |
4859 | deparsed as if no prototype was defined for them. In other words, | |
acaaef34 RGS |
4860 | |
4861 | perl -MO=Deparse,-P -e 'sub foo (\@) { 1 } foo @x' | |
4862 | ||
4863 | will print | |
4864 | ||
4865 | sub foo (\@) { | |
4866 | 1; | |
4867 | } | |
4868 | &foo(\@x); | |
4869 | ||
4870 | making clear how the parameters are actually passed to C<foo>. | |
4871 | ||
bd0865ec GS |
4872 | =item B<-q> |
4873 | ||
4874 | Expand double-quoted strings into the corresponding combinations of | |
d4963d04 | 4875 | concatenation, uc, ucfirst, lc, lcfirst, quotemeta, and join. For |
bd0865ec GS |
4876 | instance, print |
4877 | ||
4878 | print "Hello, $world, @ladies, \u$gentlemen\E, \u\L$me!"; | |
4879 | ||
4880 | as | |
4881 | ||
4882 | print 'Hello, ' . $world . ', ' . join($", @ladies) . ', ' | |
4883 | . ucfirst($gentlemen) . ', ' . ucfirst(lc $me . '!'); | |
4884 | ||
4885 | Note that the expanded form represents the way perl handles such | |
4886 | constructions internally -- this option actually turns off the reverse | |
d4963d04 | 4887 | translation that B::Deparse usually does. On the other hand, note that |
bd0865ec GS |
4888 | C<$x = "$y"> is not the same as C<$x = $y>: the former makes the value |
4889 | of $y into a string before doing the assignment. | |
4890 | ||
9d2c6865 SM |
4891 | =item B<-s>I<LETTERS> |
4892 | ||
d4963d04 FC |
4893 | Tweak the style of B::Deparse's output. The letters should follow |
4894 | directly after the 's', with no space or punctuation. The following | |
f4a44678 | 4895 | options are available: |
9d2c6865 SM |
4896 | |
4897 | =over 4 | |
4898 | ||
4899 | =item B<C> | |
4900 | ||
d4963d04 | 4901 | Cuddle C<elsif>, C<else>, and C<continue> blocks. For example, print |
9d2c6865 SM |
4902 | |
4903 | if (...) { | |
4904 | ... | |
4905 | } else { | |
4906 | ... | |
4907 | } | |
4908 | ||
4909 | instead of | |
4910 | ||
4911 | if (...) { | |
4912 | ... | |
4913 | } | |
4914 | else { | |
4915 | ... | |
4916 | } | |
4917 | ||
4918 | The default is not to cuddle. | |
4919 | ||
f4a44678 SM |
4920 | =item B<i>I<NUMBER> |
4921 | ||
d4963d04 | 4922 | Indent lines by multiples of I<NUMBER> columns. The default is 4 columns. |
f4a44678 SM |
4923 | |
4924 | =item B<T> | |
4925 | ||
d4963d04 | 4926 | Use tabs for each 8 columns of indent. The default is to use only spaces. |
f4a44678 SM |
4927 | For instance, if the style options are B<-si4T>, a line that's indented |
4928 | 3 times will be preceded by one tab and four spaces; if the options were | |
4929 | B<-si8T>, the same line would be preceded by three tabs. | |
4930 | ||
4931 | =item B<v>I<STRING>B<.> | |
4932 | ||
4933 | Print I<STRING> for the value of a constant that can't be determined | |
4934 | because it was optimized away (mnemonic: this happens when a constant | |
d4963d04 | 4935 | is used in B<v>oid context). The end of the string is marked by a period. |
f4a44678 SM |
4936 | The string should be a valid perl expression, generally a constant. |
4937 | Note that unless it's a number, it probably needs to be quoted, and on | |
d4963d04 | 4938 | a command line quotes need to be protected from the shell. Some |
f4a44678 SM |
4939 | conventional values include 0, 1, 42, '', 'foo', and |
4940 | 'Useless use of constant omitted' (which may need to be | |
4941 | B<-sv"'Useless use of constant omitted'."> | |
d4963d04 | 4942 | or something similar depending on your shell). The default is '???'. |
f4a44678 SM |
4943 | If you're using B::Deparse on a module or other file that's require'd, |
4944 | you shouldn't use a value that evaluates to false, since the customary | |
4945 | true constant at the end of a module will be in void context when the | |
4946 | file is compiled as a main program. | |
4947 | ||
9d2c6865 SM |
4948 | =back |
4949 | ||
58cccf98 SM |
4950 | =item B<-x>I<LEVEL> |
4951 | ||
4952 | Expand conventional syntax constructions into equivalent ones that expose | |
d4963d04 FC |
4953 | their internal operation. I<LEVEL> should be a digit, with higher values |
4954 | meaning more expansion. As with B<-q>, this actually involves turning off | |
58cccf98 SM |
4955 | special cases in B::Deparse's normal operations. |
4956 | ||
d989cdac | 4957 | If I<LEVEL> is at least 3, C<for> loops will be translated into equivalent |
646bba82 | 4958 | while loops with continue blocks; for instance |
58cccf98 SM |
4959 | |
4960 | for ($i = 0; $i < 10; ++$i) { | |
4961 | print $i; | |
4962 | } | |
4963 | ||
4964 | turns into | |
4965 | ||
4966 | $i = 0; | |
4967 | while ($i < 10) { | |
4968 | print $i; | |
4969 | } continue { | |
4970 | ++$i | |
4971 | } | |
4972 | ||
4973 | Note that in a few cases this translation can't be perfectly carried back | |
646bba82 | 4974 | into the source code -- if the loop's initializer declares a my variable, |
58cccf98 SM |
4975 | for instance, it won't have the correct scope outside of the loop. |
4976 | ||
d989cdac SM |
4977 | If I<LEVEL> is at least 5, C<use> declarations will be translated into |
4978 | C<BEGIN> blocks containing calls to C<require> and C<import>; for | |
4979 | instance, | |
4980 | ||
4981 | use strict 'refs'; | |
4982 | ||
4983 | turns into | |
4984 | ||
4985 | sub BEGIN { | |
4986 | require strict; | |
4987 | do { | |
4988 | 'strict'->import('refs') | |
4989 | }; | |
4990 | } | |
4991 | ||
4992 | If I<LEVEL> is at least 7, C<if> statements will be translated into | |
4993 | equivalent expressions using C<&&>, C<?:> and C<do {}>; for instance | |
58cccf98 SM |
4994 | |
4995 | print 'hi' if $nice; | |
4996 | if ($nice) { | |
4997 | print 'hi'; | |
4998 | } | |
4999 | if ($nice) { | |
5000 | print 'hi'; | |
5001 | } else { | |
5002 | print 'bye'; | |
5003 | } | |
5004 | ||
5005 | turns into | |
5006 | ||
5007 | $nice and print 'hi'; | |
5008 | $nice and do { print 'hi' }; | |
5009 | $nice ? do { print 'hi' } : do { print 'bye' }; | |
5010 | ||
5011 | Long sequences of elsifs will turn into nested ternary operators, which | |
5012 | B::Deparse doesn't know how to indent nicely. | |
5013 | ||
f6f9bdb7 SM |
5014 | =back |
5015 | ||
f4a44678 SM |
5016 | =head1 USING B::Deparse AS A MODULE |
5017 | ||
5018 | =head2 Synopsis | |
5019 | ||
5020 | use B::Deparse; | |
5021 | $deparse = B::Deparse->new("-p", "-sC"); | |
5022 | $body = $deparse->coderef2text(\&func); | |
5023 | eval "sub func $body"; # the inverse operation | |
5024 | ||
5025 | =head2 Description | |
5026 | ||
5027 | B::Deparse can also be used on a sub-by-sub basis from other perl | |
5028 | programs. | |
5029 | ||
5030 | =head2 new | |
5031 | ||
5032 | $deparse = B::Deparse->new(OPTIONS) | |
5033 | ||
5034 | Create an object to store the state of a deparsing operation and any | |
d4963d04 | 5035 | options. The options are the same as those that can be given on the |
f4a44678 | 5036 | command line (see L</OPTIONS>); options that are separated by commas |
7a07078a | 5037 | after B<-MO=Deparse> should be given as separate strings. |
f4a44678 | 5038 | |
08c6f5ec RH |
5039 | =head2 ambient_pragmas |
5040 | ||
bc6b2ef6 | 5041 | $deparse->ambient_pragmas(strict => 'all', '$[' => $[); |
08c6f5ec RH |
5042 | |
5043 | The compilation of a subroutine can be affected by a few compiler | |
d4963d04 | 5044 | directives, B<pragmas>. These are: |
08c6f5ec RH |
5045 | |
5046 | =over 4 | |
5047 | ||
5048 | =item * | |
5049 | ||
5050 | use strict; | |
5051 | ||
5052 | =item * | |
5053 | ||
5054 | use warnings; | |
5055 | ||
5056 | =item * | |
5057 | ||
bc6b2ef6 Z |
5058 | Assigning to the special variable $[ |
5059 | ||
5060 | =item * | |
5061 | ||
08c6f5ec RH |
5062 | use integer; |
5063 | ||
a0405c92 RH |
5064 | =item * |
5065 | ||
5066 | use bytes; | |
5067 | ||
5068 | =item * | |
5069 | ||
5070 | use utf8; | |
5071 | ||
5072 | =item * | |
5073 | ||
5074 | use re; | |
5075 | ||
08c6f5ec RH |
5076 | =back |
5077 | ||
5078 | Ordinarily, if you use B::Deparse on a subroutine which has | |
5079 | been compiled in the presence of one or more of these pragmas, | |
5080 | the output will include statements to turn on the appropriate | |
d4963d04 | 5081 | directives. So if you then compile the code returned by coderef2text, |
08c6f5ec RH |
5082 | it will behave the same way as the subroutine which you deparsed. |
5083 | ||
5084 | However, you may know that you intend to use the results in a | |
d4963d04 | 5085 | particular context, where some pragmas are already in scope. In |
08c6f5ec RH |
5086 | this case, you use the B<ambient_pragmas> method to describe the |
5087 | assumptions you wish to make. | |
5088 | ||
d4963d04 | 5089 | Not all of the options currently have any useful effect. See |
995e581f RH |
5090 | L</BUGS> for more details. |
5091 | ||
08c6f5ec RH |
5092 | The parameters it accepts are: |
5093 | ||
5094 | =over 4 | |
5095 | ||
5096 | =item strict | |
5097 | ||
5098 | Takes a string, possibly containing several values separated | |
d4963d04 | 5099 | by whitespace. The special values "all" and "none" mean what you'd |
08c6f5ec RH |
5100 | expect. |
5101 | ||
5102 | $deparse->ambient_pragmas(strict => 'subs refs'); | |
5103 | ||
bc6b2ef6 Z |
5104 | =item $[ |
5105 | ||
5106 | Takes a number, the value of the array base $[. | |
5107 | Cannot be non-zero on Perl 5.15.3 or later. | |
5108 | ||
a0405c92 RH |
5109 | =item bytes |
5110 | ||
5111 | =item utf8 | |
5112 | ||
08c6f5ec RH |
5113 | =item integer |
5114 | ||
a0405c92 | 5115 | If the value is true, then the appropriate pragma is assumed to |
08c6f5ec RH |
5116 | be in the ambient scope, otherwise not. |
5117 | ||
a0405c92 RH |
5118 | =item re |
5119 | ||
5120 | Takes a string, possibly containing a whitespace-separated list of | |
d4963d04 | 5121 | values. The values "all" and "none" are special. It's also permissible |
a0405c92 RH |
5122 | to pass an array reference here. |
5123 | ||
5124 | $deparser->ambient_pragmas(re => 'eval'); | |
5125 | ||
5126 | ||
08c6f5ec RH |
5127 | =item warnings |
5128 | ||
5129 | Takes a string, possibly containing a whitespace-separated list of | |
d4963d04 | 5130 | values. The values "all" and "none" are special, again. It's also |
08c6f5ec RH |
5131 | permissible to pass an array reference here. |
5132 | ||
5133 | $deparser->ambient_pragmas(warnings => [qw[void io]]); | |
5134 | ||
5135 | If one of the values is the string "FATAL", then all the warnings | |
5136 | in that list will be considered fatal, just as with the B<warnings> | |
d4963d04 | 5137 | pragma itself. Should you need to specify that some warnings are |
08c6f5ec RH |
5138 | fatal, and others are merely enabled, you can pass the B<warnings> |
5139 | parameter twice: | |
5140 | ||
5141 | $deparser->ambient_pragmas( | |
5142 | warnings => 'all', | |
5143 | warnings => [FATAL => qw/void io/], | |
5144 | ); | |
5145 | ||
d989cdac | 5146 | See L<perllexwarn> for more information about lexical warnings. |
08c6f5ec RH |
5147 | |
5148 | =item hint_bits | |
5149 | ||
5150 | =item warning_bits | |
5151 | ||
5152 | These two parameters are used to specify the ambient pragmas in | |
5153 | the format used by the special variables $^H and ${^WARNING_BITS}. | |
5154 | ||
5155 | They exist principally so that you can write code like: | |
5156 | ||
5157 | { my ($hint_bits, $warning_bits); | |
5158 | BEGIN {($hint_bits, $warning_bits) = ($^H, ${^WARNING_BITS})} | |
5159 | $deparser->ambient_pragmas ( | |
5160 | hint_bits => $hint_bits, | |
5161 | warning_bits => $warning_bits, | |
bc6b2ef6 | 5162 | '$[' => 0 + $[ |
08c6f5ec RH |
5163 | ); } |
5164 | ||
5165 | which specifies that the ambient pragmas are exactly those which | |
5166 | are in scope at the point of calling. | |
5167 | ||
0ced6c29 RGS |
5168 | =item %^H |
5169 | ||
5170 | This parameter is used to specify the ambient pragmas which are | |
5171 | stored in the special hash %^H. | |
5172 | ||
08c6f5ec RH |
5173 | =back |
5174 | ||
f4a44678 SM |
5175 | =head2 coderef2text |
5176 | ||
5177 | $body = $deparse->coderef2text(\&func) | |
5178 | $body = $deparse->coderef2text(sub ($$) { ... }) | |
5179 | ||
5180 | Return source code for the body of a subroutine (a block, optionally | |
5181 | preceded by a prototype in parens), given a reference to the | |
d4963d04 | 5182 | sub. Because a subroutine can have no names, or more than one name, |
f4a44678 SM |
5183 | this method doesn't return a complete subroutine definition -- if you |
5184 | want to eval the result, you should prepend "sub subname ", or "sub " | |
d4963d04 | 5185 | for an anonymous function constructor. Unless the sub was defined in |
f4a44678 SM |
5186 | the main:: package, the code will include a package declaration. |
5187 | ||
f6f9bdb7 SM |
5188 | =head1 BUGS |
5189 | ||
995e581f RH |
5190 | =over 4 |
5191 | ||
5192 | =item * | |
5193 | ||
5194 | The only pragmas to be completely supported are: C<use warnings>, | |
b1b6de96 | 5195 | C<use strict>, C<use bytes>, C<use integer> |
33021019 | 5196 | and C<use feature>. (C<$[>, which |
bc6b2ef6 | 5197 | behaves like a pragma, is also supported.) |
995e581f RH |
5198 | |
5199 | Excepting those listed above, we're currently unable to guarantee that | |
5200 | B::Deparse will produce a pragma at the correct point in the program. | |
d989cdac SM |
5201 | (Specifically, pragmas at the beginning of a block often appear right |
5202 | before the start of the block instead.) | |
995e581f RH |
5203 | Since the effects of pragmas are often lexically scoped, this can mean |
5204 | that the pragma holds sway over a different portion of the program | |
5205 | than in the input file. | |
5206 | ||
5207 | =item * | |
5208 | ||
c7f67cde RH |
5209 | In fact, the above is a specific instance of a more general problem: |
5210 | we can't guarantee to produce BEGIN blocks or C<use> declarations in | |
d4963d04 | 5211 | exactly the right place. So if you use a module which affects compilation |
c7f67cde RH |
5212 | (such as by over-riding keywords, overloading constants or whatever) |
5213 | then the output code might not work as intended. | |
5214 | ||
d989cdac SM |
5215 | This is the most serious outstanding problem, and will require some help |
5216 | from the Perl core to fix. | |
c7f67cde RH |
5217 | |
5218 | =item * | |
5219 | ||
d989cdac SM |
5220 | Some constants don't print correctly either with or without B<-d>. |
5221 | For instance, neither B::Deparse nor Data::Dumper know how to print | |
5222 | dual-valued scalars correctly, as in: | |
b7dad2dc | 5223 | |
d989cdac | 5224 | use constant E2BIG => ($!=7); $y = E2BIG; print $y, 0+$y; |
b7dad2dc | 5225 | |
227375e1 RU |
5226 | use constant H => { "#" => 1 }; H->{"#"}; |
5227 | ||
2a9e2f8a RH |
5228 | =item * |
5229 | ||
5230 | An input file that uses source filtering probably won't be deparsed into | |
5231 | runnable code, because it will still include the B<use> declaration | |
5232 | for the source filtering module, even though the code that is | |
5233 | produced is already ordinary Perl which shouldn't be filtered again. | |
5234 | ||
5235 | =item * | |
5236 | ||
d4963d04 FC |
5237 | Optimised away statements are rendered as |
5238 | '???'. This includes statements that | |
b64ba24c RGS |
5239 | have a compile-time side-effect, such as the obscure |
5240 | ||
5241 | my $x if 0; | |
5242 | ||
5243 | which is not, consequently, deparsed correctly. | |
5244 | ||
227375e1 RU |
5245 | foreach my $i (@_) { 0 } |
5246 | => | |
5247 | foreach my $i (@_) { '???' } | |
5248 | ||
b64ba24c RGS |
5249 | =item * |
5250 | ||
8c1e32d8 | 5251 | Lexical (my) variables declared in scopes external to a subroutine |
d4963d04 | 5252 | appear in code2ref output text as package variables. This is a tricky |
c4a6f826 | 5253 | problem, as perl has no native facility for referring to a lexical variable |
8c1e32d8 DN |
5254 | defined within a different scope, although L<PadWalker> is a good start. |
5255 | ||
5256 | =item * | |
5257 | ||
2a9e2f8a RH |
5258 | There are probably many more bugs on non-ASCII platforms (EBCDIC). |
5259 | ||
995e581f | 5260 | =back |
f6f9bdb7 SM |
5261 | |
5262 | =head1 AUTHOR | |
5263 | ||
d989cdac SM |
5264 | Stephen McCamant <smcc@CSUA.Berkeley.EDU>, based on an earlier version |
5265 | by Malcolm Beattie <mbeattie@sable.ox.ac.uk>, with contributions from | |
5266 | Gisle Aas, James Duncan, Albert Dvornik, Robin Houston, Dave Mitchell, | |
5267 | Hugo van der Sanden, Gurusamy Sarathy, Nick Ing-Simmons, and Rafael | |
5268 | Garcia-Suarez. | |
f6f9bdb7 SM |
5269 | |
5270 | =cut |