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