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