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