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