| 1 | # B::Deparse.pm |
| 2 | # Copyright (c) 1998-2000, 2002, 2003, 2004, 2005, 2006 Stephen McCamant. |
| 3 | # All rights reserved. |
| 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 | |
| 10 | package B::Deparse; |
| 11 | use Carp; |
| 12 | use B qw(class main_root main_start main_cv svref_2object opnumber perlstring |
| 13 | OPf_WANT OPf_WANT_VOID OPf_WANT_SCALAR OPf_WANT_LIST |
| 14 | OPf_KIDS OPf_REF OPf_STACKED OPf_SPECIAL OPf_MOD OPf_PARENS |
| 15 | OPpLVAL_INTRO OPpOUR_INTRO OPpENTERSUB_AMPER OPpSLICE OPpKVSLICE |
| 16 | OPpCONST_BARE |
| 17 | OPpTRANS_SQUASH OPpTRANS_DELETE OPpTRANS_COMPLEMENT OPpTARGET_MY |
| 18 | OPpEXISTS_SUB OPpSORT_NUMERIC OPpSORT_INTEGER OPpREPEAT_DOLIST |
| 19 | OPpSORT_REVERSE OPpMULTIDEREF_EXISTS OPpMULTIDEREF_DELETE |
| 20 | OPpSPLIT_ASSIGN OPpSPLIT_LEX |
| 21 | OPpPADHV_ISKEYS OPpRV2HV_ISKEYS |
| 22 | OPpCONCAT_NESTED |
| 23 | OPpMULTICONCAT_APPEND OPpMULTICONCAT_STRINGIFY OPpMULTICONCAT_FAKE |
| 24 | OPpTRUEBOOL OPpINDEX_BOOLNEG |
| 25 | SVf_IOK SVf_NOK SVf_ROK SVf_POK SVpad_OUR SVf_FAKE SVs_RMG SVs_SMG |
| 26 | SVs_PADTMP SVpad_TYPED |
| 27 | CVf_METHOD CVf_LVALUE |
| 28 | PMf_KEEP PMf_GLOBAL PMf_CONTINUE PMf_EVAL PMf_ONCE |
| 29 | PMf_MULTILINE PMf_SINGLELINE PMf_FOLD PMf_EXTENDED PMf_EXTENDED_MORE |
| 30 | PADNAMEt_OUTER |
| 31 | MDEREF_reload |
| 32 | MDEREF_AV_pop_rv2av_aelem |
| 33 | MDEREF_AV_gvsv_vivify_rv2av_aelem |
| 34 | MDEREF_AV_padsv_vivify_rv2av_aelem |
| 35 | MDEREF_AV_vivify_rv2av_aelem |
| 36 | MDEREF_AV_padav_aelem |
| 37 | MDEREF_AV_gvav_aelem |
| 38 | MDEREF_HV_pop_rv2hv_helem |
| 39 | MDEREF_HV_gvsv_vivify_rv2hv_helem |
| 40 | MDEREF_HV_padsv_vivify_rv2hv_helem |
| 41 | MDEREF_HV_vivify_rv2hv_helem |
| 42 | MDEREF_HV_padhv_helem |
| 43 | MDEREF_HV_gvhv_helem |
| 44 | MDEREF_ACTION_MASK |
| 45 | MDEREF_INDEX_none |
| 46 | MDEREF_INDEX_const |
| 47 | MDEREF_INDEX_padsv |
| 48 | MDEREF_INDEX_gvsv |
| 49 | MDEREF_INDEX_MASK |
| 50 | MDEREF_FLAG_last |
| 51 | MDEREF_MASK |
| 52 | MDEREF_SHIFT |
| 53 | ); |
| 54 | |
| 55 | $VERSION = '1.46'; |
| 56 | use strict; |
| 57 | our $AUTOLOAD; |
| 58 | use warnings (); |
| 59 | require feature; |
| 60 | |
| 61 | BEGIN { |
| 62 | # List version-specific constants here. |
| 63 | # Easiest way to keep this code portable between version looks to |
| 64 | # be to fake up a dummy constant that will never actually be true. |
| 65 | foreach (qw(OPpSORT_INPLACE OPpSORT_DESCEND OPpITER_REVERSED OPpCONST_NOVER |
| 66 | OPpPAD_STATE PMf_SKIPWHITE RXf_SKIPWHITE |
| 67 | PMf_CHARSET PMf_KEEPCOPY PMf_NOCAPTURE CVf_ANONCONST |
| 68 | CVf_LOCKED OPpREVERSE_INPLACE OPpSUBSTR_REPL_FIRST |
| 69 | PMf_NONDESTRUCT OPpCONST_ARYBASE OPpEVAL_BYTES |
| 70 | OPpLVREF_TYPE OPpLVREF_SV OPpLVREF_AV OPpLVREF_HV |
| 71 | OPpLVREF_CV OPpLVREF_ELEM SVpad_STATE)) { |
| 72 | eval { B->import($_) }; |
| 73 | no strict 'refs'; |
| 74 | *{$_} = sub () {0} unless *{$_}{CODE}; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | # Todo: |
| 79 | # (See also BUGS section at the end of this file) |
| 80 | # |
| 81 | # - finish tr/// changes |
| 82 | # - add option for even more parens (generalize \&foo change) |
| 83 | # - left/right context |
| 84 | # - copy comments (look at real text with $^P?) |
| 85 | # - avoid semis in one-statement blocks |
| 86 | # - associativity of &&=, ||=, ?: |
| 87 | # - ',' => '=>' (auto-unquote?) |
| 88 | # - break long lines ("\r" as discretionary break?) |
| 89 | # - configurable syntax highlighting: ANSI color, HTML, TeX, etc. |
| 90 | # - more style options: brace style, hex vs. octal, quotes, ... |
| 91 | # - print big ints as hex/octal instead of decimal (heuristic?) |
| 92 | # - handle 'my $x if 0'? |
| 93 | # - version using op_next instead of op_first/sibling? |
| 94 | # - avoid string copies (pass arrays, one big join?) |
| 95 | # - here-docs? |
| 96 | |
| 97 | # Current test.deparse failures |
| 98 | # comp/hints 6 - location of BEGIN blocks wrt. block openings |
| 99 | # run/switchI 1 - missing -I switches entirely |
| 100 | # perl -Ifoo -e 'print @INC' |
| 101 | # op/caller 2 - warning mask propagates backwards before warnings::register |
| 102 | # 'use warnings; BEGIN {${^WARNING_BITS} eq "U"x12;} use warnings::register' |
| 103 | # op/getpid 2 - can't assign to shared my() declaration (threads only) |
| 104 | # 'my $x : shared = 5' |
| 105 | # op/override 7 - parens on overridden require change v-string interpretation |
| 106 | # 'BEGIN{*CORE::GLOBAL::require=sub {}} require v5.6' |
| 107 | # c.f. 'BEGIN { *f = sub {0} }; f 2' |
| 108 | # op/pat 774 - losing Unicode-ness of Latin1-only strings |
| 109 | # 'use charnames ":short"; $x="\N{latin:a with acute}"' |
| 110 | # op/recurse 12 - missing parens on recursive call makes it look like method |
| 111 | # 'sub f { f($x) }' |
| 112 | # op/subst 90 - inconsistent handling of utf8 under "use utf8" |
| 113 | # op/taint 29 - "use re 'taint'" deparsed in the wrong place wrt. block open |
| 114 | # op/tiehandle compile - "use strict" deparsed in the wrong place |
| 115 | # uni/tr_ several |
| 116 | # ext/B/t/xref 11 - line numbers when we add newlines to one-line subs |
| 117 | # ext/Data/Dumper/t/dumper compile |
| 118 | # ext/DB_file/several |
| 119 | # ext/Encode/several |
| 120 | # ext/Ernno/Errno warnings |
| 121 | # ext/IO/lib/IO/t/io_sel 23 |
| 122 | # ext/PerlIO/t/encoding compile |
| 123 | # ext/POSIX/t/posix 6 |
| 124 | # ext/Socket/Socket 8 |
| 125 | # ext/Storable/t/croak compile |
| 126 | # lib/Attribute/Handlers/t/multi compile |
| 127 | # lib/bignum/ several |
| 128 | # lib/charnames 35 |
| 129 | # lib/constant 32 |
| 130 | # lib/English 40 |
| 131 | # lib/ExtUtils/t/bytes 4 |
| 132 | # lib/File/DosGlob compile |
| 133 | # lib/Filter/Simple/t/data 1 |
| 134 | # lib/Math/BigInt/t/constant 1 |
| 135 | # lib/Net/t/config Deparse-warning |
| 136 | # lib/overload compile |
| 137 | # lib/Switch/ several |
| 138 | # lib/Symbol 4 |
| 139 | # lib/Test/Simple several |
| 140 | # lib/Term/Complete |
| 141 | # lib/Tie/File/t/29_downcopy 5 |
| 142 | # lib/vars 22 |
| 143 | |
| 144 | # Object fields: |
| 145 | # |
| 146 | # in_coderef2text: |
| 147 | # True when deparsing via $deparse->coderef2text; false when deparsing the |
| 148 | # main program. |
| 149 | # |
| 150 | # avoid_local: |
| 151 | # (local($a), local($b)) and local($a, $b) have the same internal |
| 152 | # representation but the short form looks better. We notice we can |
| 153 | # use a large-scale local when checking the list, but need to prevent |
| 154 | # individual locals too. This hash holds the addresses of OPs that |
| 155 | # have already had their local-ness accounted for. The same thing |
| 156 | # is done with my(). |
| 157 | # |
| 158 | # curcv: |
| 159 | # CV for current sub (or main program) being deparsed |
| 160 | # |
| 161 | # curcvlex: |
| 162 | # Cached hash of lexical variables for curcv: keys are |
| 163 | # names prefixed with "m" or "o" (representing my/our), and |
| 164 | # each value is an array with two elements indicating the cop_seq |
| 165 | # of scopes in which a var of that name is valid and a third ele- |
| 166 | # ment referencing the pad name. |
| 167 | # |
| 168 | # curcop: |
| 169 | # COP for statement being deparsed |
| 170 | # |
| 171 | # curstash: |
| 172 | # name of the current package for deparsed code |
| 173 | # |
| 174 | # subs_todo: |
| 175 | # array of [cop_seq, CV, is_format?, name] for subs and formats we still |
| 176 | # want to deparse. The fourth element is a pad name thingy for lexical |
| 177 | # subs or a string for special blocks. For other subs, it is undef. For |
| 178 | # lexical subs, CV may be undef, indicating a stub declaration. |
| 179 | # |
| 180 | # protos_todo: |
| 181 | # as above, but [name, prototype] for subs that never got a GV |
| 182 | # |
| 183 | # subs_done, forms_done: |
| 184 | # keys are addresses of GVs for subs and formats we've already |
| 185 | # deparsed (or at least put into subs_todo) |
| 186 | # |
| 187 | # subs_declared |
| 188 | # keys are names of subs for which we've printed declarations. |
| 189 | # That means we can omit parentheses from the arguments. It also means we |
| 190 | # need to put CORE:: on core functions of the same name. |
| 191 | # |
| 192 | # in_subst_repl |
| 193 | # True when deparsing the replacement part of a substitution. |
| 194 | # |
| 195 | # in_refgen |
| 196 | # True when deparsing the argument to \. |
| 197 | # |
| 198 | # parens: -p |
| 199 | # linenums: -l |
| 200 | # unquote: -q |
| 201 | # cuddle: ' ' or '\n', depending on -sC |
| 202 | # indent_size: -si |
| 203 | # use_tabs: -sT |
| 204 | # ex_const: -sv |
| 205 | |
| 206 | # A little explanation of how precedence contexts and associativity |
| 207 | # work: |
| 208 | # |
| 209 | # deparse() calls each per-op subroutine with an argument $cx (short |
| 210 | # for context, but not the same as the cx* in the perl core), which is |
| 211 | # a number describing the op's parents in terms of precedence, whether |
| 212 | # they're inside an expression or at statement level, etc. (see |
| 213 | # chart below). When ops with children call deparse on them, they pass |
| 214 | # along their precedence. Fractional values are used to implement |
| 215 | # associativity ('($x + $y) + $z' => '$x + $y + $y') and related |
| 216 | # parentheses hacks. The major disadvantage of this scheme is that |
| 217 | # it doesn't know about right sides and left sides, so say if you |
| 218 | # assign a listop to a variable, it can't tell it's allowed to leave |
| 219 | # the parens off the listop. |
| 220 | |
| 221 | # Precedences: |
| 222 | # 26 [TODO] inside interpolation context ("") |
| 223 | # 25 left terms and list operators (leftward) |
| 224 | # 24 left -> |
| 225 | # 23 nonassoc ++ -- |
| 226 | # 22 right ** |
| 227 | # 21 right ! ~ \ and unary + and - |
| 228 | # 20 left =~ !~ |
| 229 | # 19 left * / % x |
| 230 | # 18 left + - . |
| 231 | # 17 left << >> |
| 232 | # 16 nonassoc named unary operators |
| 233 | # 15 nonassoc < > <= >= lt gt le ge |
| 234 | # 14 nonassoc == != <=> eq ne cmp |
| 235 | # 13 left & |
| 236 | # 12 left | ^ |
| 237 | # 11 left && |
| 238 | # 10 left || |
| 239 | # 9 nonassoc .. ... |
| 240 | # 8 right ?: |
| 241 | # 7 right = += -= *= etc. |
| 242 | # 6 left , => |
| 243 | # 5 nonassoc list operators (rightward) |
| 244 | # 4 right not |
| 245 | # 3 left and |
| 246 | # 2 left or xor |
| 247 | # 1 statement modifiers |
| 248 | # 0.5 statements, but still print scopes as do { ... } |
| 249 | # 0 statement level |
| 250 | # -1 format body |
| 251 | |
| 252 | # Nonprinting characters with special meaning: |
| 253 | # \cS - steal parens (see maybe_parens_unop) |
| 254 | # \n - newline and indent |
| 255 | # \t - increase indent |
| 256 | # \b - decrease indent ('outdent') |
| 257 | # \f - flush left (no indent) |
| 258 | # \cK - kill following semicolon, if any |
| 259 | |
| 260 | # Semicolon handling: |
| 261 | # - Individual statements are not deparsed with trailing semicolons. |
| 262 | # (If necessary, \cK is tacked on to the end.) |
| 263 | # - Whatever code joins statements together or emits them (lineseq, |
| 264 | # scopeop, deparse_root) is responsible for adding semicolons where |
| 265 | # necessary. |
| 266 | # - use statements are deparsed with trailing semicolons because they are |
| 267 | # immediately concatenated with the following statement. |
| 268 | # - indent() removes semicolons wherever it sees \cK. |
| 269 | |
| 270 | |
| 271 | BEGIN { for (qw[ const stringify rv2sv list glob pushmark null aelem |
| 272 | kvaslice kvhslice padsv |
| 273 | nextstate dbstate rv2av rv2hv helem custom ]) { |
| 274 | eval "sub OP_\U$_ () { " . opnumber($_) . "}" |
| 275 | }} |
| 276 | |
| 277 | # _pessimise_walk(): recursively walk the optree of a sub, |
| 278 | # possibly undoing optimisations along the way. |
| 279 | |
| 280 | sub _pessimise_walk { |
| 281 | my ($self, $startop) = @_; |
| 282 | |
| 283 | return unless $$startop; |
| 284 | my ($op, $prevop); |
| 285 | for ($op = $startop; $$op; $prevop = $op, $op = $op->sibling) { |
| 286 | my $ppname = $op->name; |
| 287 | |
| 288 | # pessimisations start here |
| 289 | |
| 290 | if ($ppname eq "padrange") { |
| 291 | # remove PADRANGE: |
| 292 | # the original optimisation either (1) changed this: |
| 293 | # pushmark -> (various pad and list and null ops) -> the_rest |
| 294 | # or (2), for the = @_ case, changed this: |
| 295 | # pushmark -> gv[_] -> rv2av -> (pad stuff) -> the_rest |
| 296 | # into this: |
| 297 | # padrange ----------------------------------------> the_rest |
| 298 | # so we just need to convert the padrange back into a |
| 299 | # pushmark, and in case (1), set its op_next to op_sibling, |
| 300 | # which is the head of the original chain of optimised-away |
| 301 | # pad ops, or for (2), set it to sibling->first, which is |
| 302 | # the original gv[_]. |
| 303 | |
| 304 | $B::overlay->{$$op} = { |
| 305 | type => OP_PUSHMARK, |
| 306 | name => 'pushmark', |
| 307 | private => ($op->private & OPpLVAL_INTRO), |
| 308 | }; |
| 309 | } |
| 310 | |
| 311 | # pessimisations end here |
| 312 | |
| 313 | if (class($op) eq 'PMOP') { |
| 314 | if (ref($op->pmreplroot) |
| 315 | && ${$op->pmreplroot} |
| 316 | && $op->pmreplroot->isa( 'B::OP' )) |
| 317 | { |
| 318 | $self-> _pessimise_walk($op->pmreplroot); |
| 319 | } |
| 320 | |
| 321 | # pessimise any /(?{...})/ code blocks |
| 322 | my ($re, $cv); |
| 323 | my $code_list = $op->code_list; |
| 324 | if ($$code_list) { |
| 325 | $self->_pessimise_walk($code_list); |
| 326 | } |
| 327 | elsif (${$re = $op->pmregexp} && ${$cv = $re->qr_anoncv}) { |
| 328 | $code_list = $cv->ROOT # leavesub |
| 329 | ->first # qr |
| 330 | ->code_list; # list |
| 331 | $self->_pessimise_walk($code_list); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | if ($op->flags & OPf_KIDS) { |
| 336 | $self-> _pessimise_walk($op->first); |
| 337 | } |
| 338 | |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | |
| 343 | # _pessimise_walk_exe(): recursively walk the op_next chain of a sub, |
| 344 | # possibly undoing optimisations along the way. |
| 345 | |
| 346 | sub _pessimise_walk_exe { |
| 347 | my ($self, $startop, $visited) = @_; |
| 348 | |
| 349 | no warnings 'recursion'; |
| 350 | |
| 351 | return unless $$startop; |
| 352 | return if $visited->{$$startop}; |
| 353 | my ($op, $prevop); |
| 354 | for ($op = $startop; $$op; $prevop = $op, $op = $op->next) { |
| 355 | last if $visited->{$$op}; |
| 356 | $visited->{$$op} = 1; |
| 357 | my $ppname = $op->name; |
| 358 | if ($ppname =~ |
| 359 | /^((and|d?or)(assign)?|(map|grep)while|range|cond_expr|once)$/ |
| 360 | # entertry is also a logop, but its op_other invariably points |
| 361 | # into the same chain as the main execution path, so we skip it |
| 362 | ) { |
| 363 | $self->_pessimise_walk_exe($op->other, $visited); |
| 364 | } |
| 365 | elsif ($ppname eq "subst") { |
| 366 | $self->_pessimise_walk_exe($op->pmreplstart, $visited); |
| 367 | } |
| 368 | elsif ($ppname =~ /^(enter(loop|iter))$/) { |
| 369 | # redoop and nextop will already be covered by the main block |
| 370 | # of the loop |
| 371 | $self->_pessimise_walk_exe($op->lastop, $visited); |
| 372 | } |
| 373 | |
| 374 | # pessimisations start here |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | # Go through an optree and "remove" some optimisations by using an |
| 379 | # overlay to selectively modify or un-null some ops. Deparsing in the |
| 380 | # absence of those optimisations is then easier. |
| 381 | # |
| 382 | # Note that older optimisations are not removed, as Deparse was already |
| 383 | # written to recognise them before the pessimise/overlay system was added. |
| 384 | |
| 385 | sub pessimise { |
| 386 | my ($self, $root, $start) = @_; |
| 387 | |
| 388 | no warnings 'recursion'; |
| 389 | # walk tree in root-to-branch order |
| 390 | $self->_pessimise_walk($root); |
| 391 | |
| 392 | my %visited; |
| 393 | # walk tree in execution order |
| 394 | $self->_pessimise_walk_exe($start, \%visited); |
| 395 | } |
| 396 | |
| 397 | |
| 398 | sub null { |
| 399 | my $op = shift; |
| 400 | return class($op) eq "NULL"; |
| 401 | } |
| 402 | |
| 403 | |
| 404 | # Add a CV to the list of subs that still need deparsing. |
| 405 | |
| 406 | sub todo { |
| 407 | my $self = shift; |
| 408 | my($cv, $is_form, $name) = @_; |
| 409 | my $cvfile = $cv->FILE//''; |
| 410 | return unless ($cvfile eq $0 || exists $self->{files}{$cvfile}); |
| 411 | my $seq; |
| 412 | if ($cv->OUTSIDE_SEQ) { |
| 413 | $seq = $cv->OUTSIDE_SEQ; |
| 414 | } elsif (!null($cv->START) and is_state($cv->START)) { |
| 415 | $seq = $cv->START->cop_seq; |
| 416 | } else { |
| 417 | $seq = 0; |
| 418 | } |
| 419 | my $stash = $cv->STASH; |
| 420 | if (class($stash) eq 'HV') { |
| 421 | $self->{packs}{$stash->NAME}++; |
| 422 | } |
| 423 | push @{$self->{'subs_todo'}}, [$seq, $cv, $is_form, $name]; |
| 424 | } |
| 425 | |
| 426 | |
| 427 | # Pop the next sub from the todo list and deparse it |
| 428 | |
| 429 | sub next_todo { |
| 430 | my $self = shift; |
| 431 | my $ent = shift @{$self->{'subs_todo'}}; |
| 432 | my ($seq, $cv, $is_form, $name) = @$ent; |
| 433 | |
| 434 | # any 'use strict; package foo' that should come before the sub |
| 435 | # declaration to sync with the first COP of the sub |
| 436 | my $pragmata = ''; |
| 437 | if ($cv and !null($cv->START) and is_state($cv->START)) { |
| 438 | $pragmata = $self->pragmata($cv->START); |
| 439 | } |
| 440 | |
| 441 | if (ref $name) { # lexical sub |
| 442 | # emit the sub. |
| 443 | my @text; |
| 444 | my $flags = $name->FLAGS; |
| 445 | push @text, |
| 446 | !$cv || $seq <= $name->COP_SEQ_RANGE_LOW |
| 447 | ? $self->keyword($flags & SVpad_OUR |
| 448 | ? "our" |
| 449 | : $flags & SVpad_STATE |
| 450 | ? "state" |
| 451 | : "my") . " " |
| 452 | : ""; |
| 453 | # XXX We would do $self->keyword("sub"), but ‘my CORE::sub’ |
| 454 | # doesn’t work and ‘my sub’ ignores a &sub in scope. I.e., |
| 455 | # we have a core bug here. |
| 456 | push @text, "sub " . substr $name->PVX, 1; |
| 457 | if ($cv) { |
| 458 | # my sub foo { } |
| 459 | push @text, " " . $self->deparse_sub($cv); |
| 460 | $text[-1] =~ s/ ;$/;/; |
| 461 | } |
| 462 | else { |
| 463 | # my sub foo; |
| 464 | push @text, ";\n"; |
| 465 | } |
| 466 | return $pragmata . join "", @text; |
| 467 | } |
| 468 | |
| 469 | my $gv = $cv->GV; |
| 470 | $name //= $self->gv_name($gv); |
| 471 | if ($is_form) { |
| 472 | return $pragmata . $self->keyword("format") . " $name =\n" |
| 473 | . $self->deparse_format($cv). "\n"; |
| 474 | } else { |
| 475 | my $use_dec; |
| 476 | if ($name eq "BEGIN") { |
| 477 | $use_dec = $self->begin_is_use($cv); |
| 478 | if (defined ($use_dec) and $self->{'expand'} < 5) { |
| 479 | return $pragmata if 0 == length($use_dec); |
| 480 | |
| 481 | # XXX bit of a hack: Test::More's use_ok() method |
| 482 | # builds a fake use statement which deparses as, e.g. |
| 483 | # use Net::Ping (@{$args[0];}); |
| 484 | # As well as being superfluous (the use_ok() is deparsed |
| 485 | # too) and ugly, it fails under use strict and otherwise |
| 486 | # makes use of a lexical var that's not in scope. |
| 487 | # So strip it out. |
| 488 | return $pragmata |
| 489 | if $use_dec =~ |
| 490 | m/ |
| 491 | \A |
| 492 | use \s \S+ \s \(\@\{ |
| 493 | ( |
| 494 | \s*\#line\ \d+\ \".*"\s* |
| 495 | )? |
| 496 | \$args\[0\];\}\); |
| 497 | \n |
| 498 | \Z |
| 499 | /x; |
| 500 | |
| 501 | $use_dec =~ s/^(use|no)\b/$self->keyword($1)/e; |
| 502 | } |
| 503 | } |
| 504 | my $l = ''; |
| 505 | if ($self->{'linenums'}) { |
| 506 | my $line = $gv->LINE; |
| 507 | my $file = $gv->FILE; |
| 508 | $l = "\n\f#line $line \"$file\"\n"; |
| 509 | } |
| 510 | my $p = ''; |
| 511 | my $stash; |
| 512 | if (class($cv->STASH) ne "SPECIAL") { |
| 513 | $stash = $cv->STASH->NAME; |
| 514 | if ($stash ne $self->{'curstash'}) { |
| 515 | $p = $self->keyword("package") . " $stash;\n"; |
| 516 | $name = "$self->{'curstash'}::$name" unless $name =~ /::/; |
| 517 | $self->{'curstash'} = $stash; |
| 518 | } |
| 519 | } |
| 520 | if ($use_dec) { |
| 521 | return "$pragmata$p$l$use_dec"; |
| 522 | } |
| 523 | if ( $name !~ /::/ and $self->lex_in_scope("&$name") |
| 524 | || $self->lex_in_scope("&$name", 1) ) |
| 525 | { |
| 526 | $name = "$self->{'curstash'}::$name"; |
| 527 | } elsif (defined $stash) { |
| 528 | $name =~ s/^\Q$stash\E::(?!\z|.*::)//; |
| 529 | } |
| 530 | my $ret = "$pragmata${p}${l}" . $self->keyword("sub") . " $name " |
| 531 | . $self->deparse_sub($cv); |
| 532 | $self->{'subs_declared'}{$name} = 1; |
| 533 | return $ret; |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | |
| 538 | # Return a "use" declaration for this BEGIN block, if appropriate |
| 539 | sub begin_is_use { |
| 540 | my ($self, $cv) = @_; |
| 541 | my $root = $cv->ROOT; |
| 542 | local @$self{qw'curcv curcvlex'} = ($cv); |
| 543 | local $B::overlay = {}; |
| 544 | $self->pessimise($root, $cv->START); |
| 545 | #require B::Debug; |
| 546 | #B::walkoptree($cv->ROOT, "debug"); |
| 547 | my $lineseq = $root->first; |
| 548 | return if $lineseq->name ne "lineseq"; |
| 549 | |
| 550 | my $req_op = $lineseq->first->sibling; |
| 551 | return if $req_op->name ne "require"; |
| 552 | |
| 553 | # maybe it's C<require expr> rather than C<require 'foo'> |
| 554 | return if ($req_op->first->name ne 'const'); |
| 555 | |
| 556 | my $module; |
| 557 | if ($req_op->first->private & OPpCONST_BARE) { |
| 558 | # Actually it should always be a bareword |
| 559 | $module = $self->const_sv($req_op->first)->PV; |
| 560 | $module =~ s[/][::]g; |
| 561 | $module =~ s/.pm$//; |
| 562 | } |
| 563 | else { |
| 564 | $module = $self->const($self->const_sv($req_op->first), 6); |
| 565 | } |
| 566 | |
| 567 | my $version; |
| 568 | my $version_op = $req_op->sibling; |
| 569 | return if class($version_op) eq "NULL"; |
| 570 | if ($version_op->name eq "lineseq") { |
| 571 | # We have a version parameter; skip nextstate & pushmark |
| 572 | my $constop = $version_op->first->next->next; |
| 573 | |
| 574 | return unless $self->const_sv($constop)->PV eq $module; |
| 575 | $constop = $constop->sibling; |
| 576 | $version = $self->const_sv($constop); |
| 577 | if (class($version) eq "IV") { |
| 578 | $version = $version->int_value; |
| 579 | } elsif (class($version) eq "NV") { |
| 580 | $version = $version->NV; |
| 581 | } elsif (class($version) ne "PVMG") { |
| 582 | # Includes PVIV and PVNV |
| 583 | $version = $version->PV; |
| 584 | } else { |
| 585 | # version specified as a v-string |
| 586 | $version = 'v'.join '.', map ord, split //, $version->PV; |
| 587 | } |
| 588 | $constop = $constop->sibling; |
| 589 | return if $constop->name ne "method_named"; |
| 590 | return if $self->meth_sv($constop)->PV ne "VERSION"; |
| 591 | } |
| 592 | |
| 593 | $lineseq = $version_op->sibling; |
| 594 | return if $lineseq->name ne "lineseq"; |
| 595 | my $entersub = $lineseq->first->sibling; |
| 596 | if ($entersub->name eq "stub") { |
| 597 | return "use $module $version ();\n" if defined $version; |
| 598 | return "use $module ();\n"; |
| 599 | } |
| 600 | return if $entersub->name ne "entersub"; |
| 601 | |
| 602 | # See if there are import arguments |
| 603 | my $args = ''; |
| 604 | |
| 605 | my $svop = $entersub->first->sibling; # Skip over pushmark |
| 606 | return unless $self->const_sv($svop)->PV eq $module; |
| 607 | |
| 608 | # Pull out the arguments |
| 609 | for ($svop=$svop->sibling; index($svop->name, "method_") != 0; |
| 610 | $svop = $svop->sibling) { |
| 611 | $args .= ", " if length($args); |
| 612 | $args .= $self->deparse($svop, 6); |
| 613 | } |
| 614 | |
| 615 | my $use = 'use'; |
| 616 | my $method_named = $svop; |
| 617 | return if $method_named->name ne "method_named"; |
| 618 | my $method_name = $self->meth_sv($method_named)->PV; |
| 619 | |
| 620 | if ($method_name eq "unimport") { |
| 621 | $use = 'no'; |
| 622 | } |
| 623 | |
| 624 | # Certain pragmas are dealt with using hint bits, |
| 625 | # so we ignore them here |
| 626 | if ($module eq 'strict' || $module eq 'integer' |
| 627 | || $module eq 'bytes' || $module eq 'warnings' |
| 628 | || $module eq 'feature') { |
| 629 | return ""; |
| 630 | } |
| 631 | |
| 632 | if (defined $version && length $args) { |
| 633 | return "$use $module $version ($args);\n"; |
| 634 | } elsif (defined $version) { |
| 635 | return "$use $module $version;\n"; |
| 636 | } elsif (length $args) { |
| 637 | return "$use $module ($args);\n"; |
| 638 | } else { |
| 639 | return "$use $module;\n"; |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | sub stash_subs { |
| 644 | my ($self, $pack, $seen) = @_; |
| 645 | my (@ret, $stash); |
| 646 | if (!defined $pack) { |
| 647 | $pack = ''; |
| 648 | $stash = \%::; |
| 649 | } |
| 650 | else { |
| 651 | $pack =~ s/(::)?$/::/; |
| 652 | no strict 'refs'; |
| 653 | $stash = \%{"main::$pack"}; |
| 654 | } |
| 655 | return |
| 656 | if ($seen ||= {})->{ |
| 657 | $INC{"overload.pm"} ? overload::StrVal($stash) : $stash |
| 658 | }++; |
| 659 | my $stashobj = svref_2object($stash); |
| 660 | my %stash = $stashobj->ARRAY; |
| 661 | while (my ($key, $val) = each %stash) { |
| 662 | my $flags = $val->FLAGS; |
| 663 | if ($flags & SVf_ROK) { |
| 664 | # A reference. Dump this if it is a reference to a CV. If it |
| 665 | # is a constant acting as a proxy for a full subroutine, then |
| 666 | # we may or may not have to dump it. If some form of perl- |
| 667 | # space visible code must have created it, be it a use |
| 668 | # statement, or some direct symbol-table manipulation code that |
| 669 | # we will deparse, then we don’t want to dump it. If it is the |
| 670 | # result of a declaration like sub f () { 42 } then we *do* |
| 671 | # want to dump it. The only way to distinguish these seems |
| 672 | # to be the SVs_PADTMP flag on the constant, which is admit- |
| 673 | # tedly a hack. |
| 674 | my $class = class(my $referent = $val->RV); |
| 675 | if ($class eq "CV") { |
| 676 | $self->todo($referent, 0); |
| 677 | } elsif ( |
| 678 | $class !~ /^(AV|HV|CV|FM|IO|SPECIAL)\z/ |
| 679 | # A more robust way to write that would be this, but B does |
| 680 | # not provide the SVt_ constants: |
| 681 | # ($referent->FLAGS & B::SVTYPEMASK) < B::SVt_PVAV |
| 682 | and $referent->FLAGS & SVs_PADTMP |
| 683 | ) { |
| 684 | push @{$self->{'protos_todo'}}, [$pack . $key, $val]; |
| 685 | } |
| 686 | } elsif ($flags & (SVf_POK|SVf_IOK)) { |
| 687 | # Just a prototype. As an ugly but fairly effective way |
| 688 | # to find out if it belongs here is to see if the AUTOLOAD |
| 689 | # (if any) for the stash was defined in one of our files. |
| 690 | my $A = $stash{"AUTOLOAD"}; |
| 691 | if (defined ($A) && class($A) eq "GV" && defined($A->CV) |
| 692 | && class($A->CV) eq "CV") { |
| 693 | my $AF = $A->FILE; |
| 694 | next unless $AF eq $0 || exists $self->{'files'}{$AF}; |
| 695 | } |
| 696 | push @{$self->{'protos_todo'}}, |
| 697 | [$pack . $key, $flags & SVf_POK ? $val->PV: undef]; |
| 698 | } elsif (class($val) eq "GV") { |
| 699 | if (class(my $cv = $val->CV) ne "SPECIAL") { |
| 700 | next if $self->{'subs_done'}{$$val}++; |
| 701 | |
| 702 | # Ignore imposters (aliases etc) |
| 703 | my $name = $cv->NAME_HEK; |
| 704 | if(defined $name) { |
| 705 | # avoid using $cv->GV here because if the $val GV is |
| 706 | # an alias, CvGV() could upgrade the real stash entry |
| 707 | # from an RV to a GV |
| 708 | next unless $name eq $key; |
| 709 | next unless $$stashobj == ${$cv->STASH}; |
| 710 | } |
| 711 | else { |
| 712 | next if $$val != ${$cv->GV}; |
| 713 | } |
| 714 | |
| 715 | $self->todo($cv, 0); |
| 716 | } |
| 717 | if (class(my $cv = $val->FORM) ne "SPECIAL") { |
| 718 | next if $self->{'forms_done'}{$$val}++; |
| 719 | next if $$val != ${$cv->GV}; # Ignore imposters |
| 720 | $self->todo($cv, 1); |
| 721 | } |
| 722 | if (class($val->HV) ne "SPECIAL" && $key =~ /::$/) { |
| 723 | $self->stash_subs($pack . $key, $seen); |
| 724 | } |
| 725 | } |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | sub print_protos { |
| 730 | my $self = shift; |
| 731 | my $ar; |
| 732 | my @ret; |
| 733 | foreach $ar (@{$self->{'protos_todo'}}) { |
| 734 | if (ref $ar->[1]) { |
| 735 | # Only print a constant if it occurs in the same package as a |
| 736 | # dumped sub. This is not perfect, but a heuristic that will |
| 737 | # hopefully work most of the time. Ideally we would use |
| 738 | # CvFILE, but a constant stub has no CvFILE. |
| 739 | my $pack = ($ar->[0] =~ /(.*)::/)[0]; |
| 740 | next if $pack and !$self->{packs}{$pack} |
| 741 | } |
| 742 | my $body = defined $ar->[1] |
| 743 | ? ref $ar->[1] |
| 744 | ? " () {\n " . $self->const($ar->[1]->RV,0) . ";\n}" |
| 745 | : " (". $ar->[1] . ");" |
| 746 | : ";"; |
| 747 | push @ret, "sub " . $ar->[0] . "$body\n"; |
| 748 | } |
| 749 | delete $self->{'protos_todo'}; |
| 750 | return @ret; |
| 751 | } |
| 752 | |
| 753 | sub style_opts { |
| 754 | my $self = shift; |
| 755 | my $opts = shift; |
| 756 | my $opt; |
| 757 | while (length($opt = substr($opts, 0, 1))) { |
| 758 | if ($opt eq "C") { |
| 759 | $self->{'cuddle'} = " "; |
| 760 | $opts = substr($opts, 1); |
| 761 | } elsif ($opt eq "i") { |
| 762 | $opts =~ s/^i(\d+)//; |
| 763 | $self->{'indent_size'} = $1; |
| 764 | } elsif ($opt eq "T") { |
| 765 | $self->{'use_tabs'} = 1; |
| 766 | $opts = substr($opts, 1); |
| 767 | } elsif ($opt eq "v") { |
| 768 | $opts =~ s/^v([^.]*)(.|$)//; |
| 769 | $self->{'ex_const'} = $1; |
| 770 | } |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | sub new { |
| 775 | my $class = shift; |
| 776 | my $self = bless {}, $class; |
| 777 | $self->{'cuddle'} = "\n"; |
| 778 | $self->{'curcop'} = undef; |
| 779 | $self->{'curstash'} = "main"; |
| 780 | $self->{'ex_const'} = "'???'"; |
| 781 | $self->{'expand'} = 0; |
| 782 | $self->{'files'} = {}; |
| 783 | $self->{'packs'} = {}; |
| 784 | $self->{'indent_size'} = 4; |
| 785 | $self->{'linenums'} = 0; |
| 786 | $self->{'parens'} = 0; |
| 787 | $self->{'subs_todo'} = []; |
| 788 | $self->{'unquote'} = 0; |
| 789 | $self->{'use_dumper'} = 0; |
| 790 | $self->{'use_tabs'} = 0; |
| 791 | |
| 792 | $self->{'ambient_arybase'} = 0; |
| 793 | $self->{'ambient_warnings'} = undef; # Assume no lexical warnings |
| 794 | $self->{'ambient_hints'} = 0; |
| 795 | $self->{'ambient_hinthash'} = undef; |
| 796 | $self->init(); |
| 797 | |
| 798 | while (my $arg = shift @_) { |
| 799 | if ($arg eq "-d") { |
| 800 | $self->{'use_dumper'} = 1; |
| 801 | require Data::Dumper; |
| 802 | } elsif ($arg =~ /^-f(.*)/) { |
| 803 | $self->{'files'}{$1} = 1; |
| 804 | } elsif ($arg eq "-l") { |
| 805 | $self->{'linenums'} = 1; |
| 806 | } elsif ($arg eq "-p") { |
| 807 | $self->{'parens'} = 1; |
| 808 | } elsif ($arg eq "-P") { |
| 809 | $self->{'noproto'} = 1; |
| 810 | } elsif ($arg eq "-q") { |
| 811 | $self->{'unquote'} = 1; |
| 812 | } elsif (substr($arg, 0, 2) eq "-s") { |
| 813 | $self->style_opts(substr $arg, 2); |
| 814 | } elsif ($arg =~ /^-x(\d)$/) { |
| 815 | $self->{'expand'} = $1; |
| 816 | } |
| 817 | } |
| 818 | return $self; |
| 819 | } |
| 820 | |
| 821 | { |
| 822 | # Mask out the bits that L<warnings::register> uses |
| 823 | my $WARN_MASK; |
| 824 | BEGIN { |
| 825 | $WARN_MASK = $warnings::Bits{all} | $warnings::DeadBits{all}; |
| 826 | } |
| 827 | sub WARN_MASK () { |
| 828 | return $WARN_MASK; |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | # Initialise the contextual information, either from |
| 833 | # defaults provided with the ambient_pragmas method, |
| 834 | # or from perl's own defaults otherwise. |
| 835 | sub init { |
| 836 | my $self = shift; |
| 837 | |
| 838 | $self->{'arybase'} = $self->{'ambient_arybase'}; |
| 839 | $self->{'warnings'} = defined ($self->{'ambient_warnings'}) |
| 840 | ? $self->{'ambient_warnings'} & WARN_MASK |
| 841 | : undef; |
| 842 | $self->{'hints'} = $self->{'ambient_hints'}; |
| 843 | $self->{'hinthash'} = $self->{'ambient_hinthash'}; |
| 844 | |
| 845 | # also a convenient place to clear out subs_declared |
| 846 | delete $self->{'subs_declared'}; |
| 847 | } |
| 848 | |
| 849 | sub compile { |
| 850 | my(@args) = @_; |
| 851 | return sub { |
| 852 | my $self = B::Deparse->new(@args); |
| 853 | # First deparse command-line args |
| 854 | if (defined $^I) { # deparse -i |
| 855 | print q(BEGIN { $^I = ).perlstring($^I).qq(; }\n); |
| 856 | } |
| 857 | if ($^W) { # deparse -w |
| 858 | print qq(BEGIN { \$^W = $^W; }\n); |
| 859 | } |
| 860 | if ($/ ne "\n" or defined $O::savebackslash) { # deparse -l and -0 |
| 861 | my $fs = perlstring($/) || 'undef'; |
| 862 | my $bs = perlstring($O::savebackslash) || 'undef'; |
| 863 | print qq(BEGIN { \$/ = $fs; \$\\ = $bs; }\n); |
| 864 | } |
| 865 | my @BEGINs = B::begin_av->isa("B::AV") ? B::begin_av->ARRAY : (); |
| 866 | my @UNITCHECKs = B::unitcheck_av->isa("B::AV") |
| 867 | ? B::unitcheck_av->ARRAY |
| 868 | : (); |
| 869 | my @CHECKs = B::check_av->isa("B::AV") ? B::check_av->ARRAY : (); |
| 870 | my @INITs = B::init_av->isa("B::AV") ? B::init_av->ARRAY : (); |
| 871 | my @ENDs = B::end_av->isa("B::AV") ? B::end_av->ARRAY : (); |
| 872 | my @names = qw(BEGIN UNITCHECK CHECK INIT END); |
| 873 | my @blocks = \(@BEGINs, @UNITCHECKs, @CHECKs, @INITs, @ENDs); |
| 874 | while (@names) { |
| 875 | my ($name, $blocks) = (shift @names, shift @blocks); |
| 876 | for my $block (@$blocks) { |
| 877 | $self->todo($block, 0, $name); |
| 878 | } |
| 879 | } |
| 880 | $self->stash_subs(); |
| 881 | local($SIG{"__DIE__"}) = |
| 882 | sub { |
| 883 | if ($self->{'curcop'}) { |
| 884 | my $cop = $self->{'curcop'}; |
| 885 | my($line, $file) = ($cop->line, $cop->file); |
| 886 | print STDERR "While deparsing $file near line $line,\n"; |
| 887 | } |
| 888 | }; |
| 889 | $self->{'curcv'} = main_cv; |
| 890 | $self->{'curcvlex'} = undef; |
| 891 | print $self->print_protos; |
| 892 | @{$self->{'subs_todo'}} = |
| 893 | sort {$a->[0] <=> $b->[0]} @{$self->{'subs_todo'}}; |
| 894 | my $root = main_root; |
| 895 | local $B::overlay = {}; |
| 896 | unless (null $root) { |
| 897 | $self->pad_subs($self->{'curcv'}); |
| 898 | # Check for a stub-followed-by-ex-cop, resulting from a program |
| 899 | # consisting solely of sub declarations. For backward-compati- |
| 900 | # bility (and sane output) we don’t want to emit the stub. |
| 901 | # leave |
| 902 | # enter |
| 903 | # stub |
| 904 | # ex-nextstate (or ex-dbstate) |
| 905 | my $kid; |
| 906 | if ( $root->name eq 'leave' |
| 907 | and ($kid = $root->first)->name eq 'enter' |
| 908 | and !null($kid = $kid->sibling) and $kid->name eq 'stub' |
| 909 | and !null($kid = $kid->sibling) and $kid->name eq 'null' |
| 910 | and class($kid) eq 'COP' and null $kid->sibling ) |
| 911 | { |
| 912 | # ignore |
| 913 | } else { |
| 914 | $self->pessimise($root, main_start); |
| 915 | print $self->indent($self->deparse_root($root)), "\n"; |
| 916 | } |
| 917 | } |
| 918 | my @text; |
| 919 | while (scalar(@{$self->{'subs_todo'}})) { |
| 920 | push @text, $self->next_todo; |
| 921 | } |
| 922 | print $self->indent(join("", @text)), "\n" if @text; |
| 923 | |
| 924 | # Print __DATA__ section, if necessary |
| 925 | no strict 'refs'; |
| 926 | my $laststash = defined $self->{'curcop'} |
| 927 | ? $self->{'curcop'}->stash->NAME : $self->{'curstash'}; |
| 928 | if (defined *{$laststash."::DATA"}{IO}) { |
| 929 | print $self->keyword("package") . " $laststash;\n" |
| 930 | unless $laststash eq $self->{'curstash'}; |
| 931 | print $self->keyword("__DATA__") . "\n"; |
| 932 | print readline(*{$laststash."::DATA"}); |
| 933 | } |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | sub coderef2text { |
| 938 | my $self = shift; |
| 939 | my $sub = shift; |
| 940 | croak "Usage: ->coderef2text(CODEREF)" unless UNIVERSAL::isa($sub, "CODE"); |
| 941 | |
| 942 | $self->init(); |
| 943 | local $self->{in_coderef2text} = 1; |
| 944 | return $self->indent($self->deparse_sub(svref_2object($sub))); |
| 945 | } |
| 946 | |
| 947 | my %strict_bits = do { |
| 948 | local $^H; |
| 949 | map +($_ => strict::bits($_)), qw/refs subs vars/ |
| 950 | }; |
| 951 | |
| 952 | sub ambient_pragmas { |
| 953 | my $self = shift; |
| 954 | my ($arybase, $hint_bits, $warning_bits, $hinthash) = (0, 0); |
| 955 | |
| 956 | while (@_ > 1) { |
| 957 | my $name = shift(); |
| 958 | my $val = shift(); |
| 959 | |
| 960 | if ($name eq 'strict') { |
| 961 | require strict; |
| 962 | |
| 963 | if ($val eq 'none') { |
| 964 | $hint_bits &= $strict_bits{$_} for qw/refs subs vars/; |
| 965 | next(); |
| 966 | } |
| 967 | |
| 968 | my @names; |
| 969 | if ($val eq "all") { |
| 970 | @names = qw/refs subs vars/; |
| 971 | } |
| 972 | elsif (ref $val) { |
| 973 | @names = @$val; |
| 974 | } |
| 975 | else { |
| 976 | @names = split' ', $val; |
| 977 | } |
| 978 | $hint_bits |= $strict_bits{$_} for @names; |
| 979 | } |
| 980 | |
| 981 | elsif ($name eq '$[') { |
| 982 | if (OPpCONST_ARYBASE) { |
| 983 | $arybase = $val; |
| 984 | } else { |
| 985 | croak "\$[ can't be non-zero on this perl" unless $val == 0; |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | elsif ($name eq 'integer' |
| 990 | || $name eq 'bytes' |
| 991 | || $name eq 'utf8') { |
| 992 | require "$name.pm"; |
| 993 | if ($val) { |
| 994 | $hint_bits |= ${$::{"${name}::"}{"hint_bits"}}; |
| 995 | } |
| 996 | else { |
| 997 | $hint_bits &= ~${$::{"${name}::"}{"hint_bits"}}; |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | elsif ($name eq 're') { |
| 1002 | require re; |
| 1003 | if ($val eq 'none') { |
| 1004 | $hint_bits &= ~re::bits(qw/taint eval/); |
| 1005 | next(); |
| 1006 | } |
| 1007 | |
| 1008 | my @names; |
| 1009 | if ($val eq 'all') { |
| 1010 | @names = qw/taint eval/; |
| 1011 | } |
| 1012 | elsif (ref $val) { |
| 1013 | @names = @$val; |
| 1014 | } |
| 1015 | else { |
| 1016 | @names = split' ',$val; |
| 1017 | } |
| 1018 | $hint_bits |= re::bits(@names); |
| 1019 | } |
| 1020 | |
| 1021 | elsif ($name eq 'warnings') { |
| 1022 | if ($val eq 'none') { |
| 1023 | $warning_bits = $warnings::NONE; |
| 1024 | next(); |
| 1025 | } |
| 1026 | |
| 1027 | my @names; |
| 1028 | if (ref $val) { |
| 1029 | @names = @$val; |
| 1030 | } |
| 1031 | else { |
| 1032 | @names = split/\s+/, $val; |
| 1033 | } |
| 1034 | |
| 1035 | $warning_bits = $warnings::NONE if !defined ($warning_bits); |
| 1036 | $warning_bits |= warnings::bits(@names); |
| 1037 | } |
| 1038 | |
| 1039 | elsif ($name eq 'warning_bits') { |
| 1040 | $warning_bits = $val; |
| 1041 | } |
| 1042 | |
| 1043 | elsif ($name eq 'hint_bits') { |
| 1044 | $hint_bits = $val; |
| 1045 | } |
| 1046 | |
| 1047 | elsif ($name eq '%^H') { |
| 1048 | $hinthash = $val; |
| 1049 | } |
| 1050 | |
| 1051 | else { |
| 1052 | croak "Unknown pragma type: $name"; |
| 1053 | } |
| 1054 | } |
| 1055 | if (@_) { |
| 1056 | croak "The ambient_pragmas method expects an even number of args"; |
| 1057 | } |
| 1058 | |
| 1059 | $self->{'ambient_arybase'} = $arybase; |
| 1060 | $self->{'ambient_warnings'} = $warning_bits; |
| 1061 | $self->{'ambient_hints'} = $hint_bits; |
| 1062 | $self->{'ambient_hinthash'} = $hinthash; |
| 1063 | } |
| 1064 | |
| 1065 | # This method is the inner loop, so try to keep it simple |
| 1066 | sub deparse { |
| 1067 | my $self = shift; |
| 1068 | my($op, $cx) = @_; |
| 1069 | |
| 1070 | Carp::confess("Null op in deparse") if !defined($op) |
| 1071 | || class($op) eq "NULL"; |
| 1072 | my $meth = "pp_" . $op->name; |
| 1073 | return $self->$meth($op, $cx); |
| 1074 | } |
| 1075 | |
| 1076 | sub indent { |
| 1077 | my $self = shift; |
| 1078 | my $txt = shift; |
| 1079 | # \cK also swallows a preceding line break when followed by a |
| 1080 | # semicolon. |
| 1081 | $txt =~ s/\n\cK;//g; |
| 1082 | my @lines = split(/\n/, $txt); |
| 1083 | my $leader = ""; |
| 1084 | my $level = 0; |
| 1085 | my $line; |
| 1086 | for $line (@lines) { |
| 1087 | my $cmd = substr($line, 0, 1); |
| 1088 | if ($cmd eq "\t" or $cmd eq "\b") { |
| 1089 | $level += ($cmd eq "\t" ? 1 : -1) * $self->{'indent_size'}; |
| 1090 | if ($self->{'use_tabs'}) { |
| 1091 | $leader = "\t" x ($level / 8) . " " x ($level % 8); |
| 1092 | } else { |
| 1093 | $leader = " " x $level; |
| 1094 | } |
| 1095 | $line = substr($line, 1); |
| 1096 | } |
| 1097 | if (index($line, "\f") > 0) { |
| 1098 | $line =~ s/\f/\n/; |
| 1099 | } |
| 1100 | if (substr($line, 0, 1) eq "\f") { |
| 1101 | $line = substr($line, 1); # no indent |
| 1102 | } else { |
| 1103 | $line = $leader . $line; |
| 1104 | } |
| 1105 | $line =~ s/\cK;?//g; |
| 1106 | } |
| 1107 | return join("\n", @lines); |
| 1108 | } |
| 1109 | |
| 1110 | sub pad_subs { |
| 1111 | my ($self, $cv) = @_; |
| 1112 | my $padlist = $cv->PADLIST; |
| 1113 | my @names = $padlist->ARRAYelt(0)->ARRAY; |
| 1114 | my @values = $padlist->ARRAYelt(1)->ARRAY; |
| 1115 | my @todo; |
| 1116 | PADENTRY: |
| 1117 | for my $ix (0.. $#names) { for $_ ($names[$ix]) { |
| 1118 | next if class($_) eq "SPECIAL"; |
| 1119 | my $name = $_->PVX; |
| 1120 | if (defined $name && $name =~ /^&./) { |
| 1121 | my $low = $_->COP_SEQ_RANGE_LOW; |
| 1122 | my $flags = $_->FLAGS; |
| 1123 | my $outer = $flags & PADNAMEt_OUTER; |
| 1124 | if ($flags & SVpad_OUR) { |
| 1125 | push @todo, [$low, undef, 0, $_] |
| 1126 | # [seq, no cv, not format, padname] |
| 1127 | unless $outer; |
| 1128 | next; |
| 1129 | } |
| 1130 | my $protocv = $flags & SVpad_STATE |
| 1131 | ? $values[$ix] |
| 1132 | : $_->PROTOCV; |
| 1133 | if (class ($protocv) ne 'CV') { |
| 1134 | my $flags = $flags; |
| 1135 | my $cv = $cv; |
| 1136 | my $name = $_; |
| 1137 | while ($flags & PADNAMEt_OUTER && class ($protocv) ne 'CV') |
| 1138 | { |
| 1139 | $cv = $cv->OUTSIDE; |
| 1140 | next PADENTRY if class($cv) eq 'SPECIAL'; # XXX freed? |
| 1141 | my $padlist = $cv->PADLIST; |
| 1142 | my $ix = $name->PARENT_PAD_INDEX; |
| 1143 | $name = $padlist->NAMES->ARRAYelt($ix); |
| 1144 | $flags = $name->FLAGS; |
| 1145 | $protocv = $flags & SVpad_STATE |
| 1146 | ? $padlist->ARRAYelt(1)->ARRAYelt($ix) |
| 1147 | : $name->PROTOCV; |
| 1148 | } |
| 1149 | } |
| 1150 | my $defined_in_this_sub = ${$protocv->OUTSIDE} == $$cv || do { |
| 1151 | my $other = $protocv->PADLIST; |
| 1152 | $$other && $other->outid == $padlist->id; |
| 1153 | }; |
| 1154 | if ($flags & PADNAMEt_OUTER) { |
| 1155 | next unless $defined_in_this_sub; |
| 1156 | push @todo, [$protocv->OUTSIDE_SEQ, $protocv, 0, $_]; |
| 1157 | next; |
| 1158 | } |
| 1159 | my $outseq = $protocv->OUTSIDE_SEQ; |
| 1160 | if ($outseq <= $low) { |
| 1161 | # defined before its name is visible, so it’s gotta be |
| 1162 | # declared and defined at once: my sub foo { ... } |
| 1163 | push @todo, [$low, $protocv, 0, $_]; |
| 1164 | } |
| 1165 | else { |
| 1166 | # declared and defined separately: my sub f; sub f { ... } |
| 1167 | push @todo, [$low, undef, 0, $_]; |
| 1168 | push @todo, [$outseq, $protocv, 0, $_] |
| 1169 | if $defined_in_this_sub; |
| 1170 | } |
| 1171 | } |
| 1172 | }} |
| 1173 | @{$self->{'subs_todo'}} = |
| 1174 | sort {$a->[0] <=> $b->[0]} @{$self->{'subs_todo'}}, @todo |
| 1175 | } |
| 1176 | |
| 1177 | |
| 1178 | # deparse_argops(): deparse, if possible, a sequence of argcheck + argelem |
| 1179 | # ops into a subroutine signature. If successful, return the first op |
| 1180 | # following the signature ops plus the signature string; else return the |
| 1181 | # empty list. |
| 1182 | # |
| 1183 | # Normally a bunch of argelem ops will have been generated by the |
| 1184 | # signature parsing, but it's possible that ops have been added manually |
| 1185 | # or altered. In this case we "return ()" and fall back to general |
| 1186 | # deparsing of the individual sigelems as 'my $x = $_[N]' etc. |
| 1187 | # |
| 1188 | # We're only called if the first two ops are nextstate and argcheck. |
| 1189 | |
| 1190 | sub deparse_argops { |
| 1191 | my ($self, $firstop, $cv) = @_; |
| 1192 | |
| 1193 | my @sig; |
| 1194 | my $o = $firstop; |
| 1195 | return if $o->label; #first nextstate; |
| 1196 | |
| 1197 | # OP_ARGCHECK |
| 1198 | |
| 1199 | $o = $o->sibling; |
| 1200 | my ($params, $opt_params, $slurpy) = $o->aux_list($cv); |
| 1201 | my $mandatory = $params - $opt_params; |
| 1202 | my $seen_slurpy = 0; |
| 1203 | my $last_ix = -1; |
| 1204 | |
| 1205 | # keep looking for valid nextstate + argelem pairs |
| 1206 | |
| 1207 | while (1) { |
| 1208 | # OP_NEXTSTATE |
| 1209 | $o = $o->sibling; |
| 1210 | last unless $$o; |
| 1211 | last unless $o->name =~ /^(next|db)state$/; |
| 1212 | last if $o->label; |
| 1213 | |
| 1214 | # OP_ARGELEM |
| 1215 | my $o2 = $o->sibling; |
| 1216 | last unless $$o2; |
| 1217 | |
| 1218 | if ($o2->name eq 'argelem') { |
| 1219 | my $ix = $o2->string($cv); |
| 1220 | while (++$last_ix < $ix) { |
| 1221 | push @sig, $last_ix < $mandatory ? '$' : '$='; |
| 1222 | } |
| 1223 | my $var = $self->padname($o2->targ); |
| 1224 | if ($var =~ /^[@%]/) { |
| 1225 | return if $seen_slurpy; |
| 1226 | $seen_slurpy = 1; |
| 1227 | return if $ix != $params or !$slurpy |
| 1228 | or substr($var,0,1) ne $slurpy; |
| 1229 | } |
| 1230 | else { |
| 1231 | return if $ix >= $params; |
| 1232 | } |
| 1233 | if ($o2->flags & OPf_KIDS) { |
| 1234 | my $kid = $o2->first; |
| 1235 | return unless $$kid and $kid->name eq 'argdefelem'; |
| 1236 | my $def = $self->deparse($kid->first, 7); |
| 1237 | $def = "($def)" if $kid->first->flags & OPf_PARENS; |
| 1238 | $var .= " = $def"; |
| 1239 | } |
| 1240 | push @sig, $var; |
| 1241 | } |
| 1242 | elsif ($o2->name eq 'null' |
| 1243 | and ($o2->flags & OPf_KIDS) |
| 1244 | and $o2->first->name eq 'argdefelem') |
| 1245 | { |
| 1246 | # special case - a void context default expression: $ = expr |
| 1247 | |
| 1248 | my $defop = $o2->first; |
| 1249 | my $ix = $defop->targ; |
| 1250 | while (++$last_ix < $ix) { |
| 1251 | push @sig, $last_ix < $mandatory ? '$' : '$='; |
| 1252 | } |
| 1253 | return if $last_ix >= $params |
| 1254 | or $last_ix < $mandatory; |
| 1255 | my $def = $self->deparse($defop->first, 7); |
| 1256 | $def = "($def)" if $defop->first->flags & OPf_PARENS; |
| 1257 | push @sig, '$ = ' . $def; |
| 1258 | } |
| 1259 | else { |
| 1260 | last; |
| 1261 | } |
| 1262 | |
| 1263 | $o = $o2; |
| 1264 | } |
| 1265 | |
| 1266 | while (++$last_ix < $params) { |
| 1267 | push @sig, $last_ix < $mandatory ? '$' : '$='; |
| 1268 | } |
| 1269 | push @sig, $slurpy if $slurpy and !$seen_slurpy; |
| 1270 | |
| 1271 | return ($o, join(', ', @sig)); |
| 1272 | } |
| 1273 | |
| 1274 | # Deparse a sub. Returns everything except the 'sub foo', |
| 1275 | # e.g. ($$) : method { ...; } |
| 1276 | # or ($a, $b) : prototype($$) lvalue; |
| 1277 | |
| 1278 | sub deparse_sub { |
| 1279 | my $self = shift; |
| 1280 | my $cv = shift; |
| 1281 | my @attrs; |
| 1282 | my $protosig; # prototype or signature (what goes in the (....)) |
| 1283 | |
| 1284 | Carp::confess("NULL in deparse_sub") if !defined($cv) || $cv->isa("B::NULL"); |
| 1285 | Carp::confess("SPECIAL in deparse_sub") if $cv->isa("B::SPECIAL"); |
| 1286 | local $self->{'curcop'} = $self->{'curcop'}; |
| 1287 | |
| 1288 | my $has_sig = $self->{hinthash}{feature_signatures}; |
| 1289 | if ($cv->FLAGS & SVf_POK) { |
| 1290 | my $proto = $cv->PV; |
| 1291 | if ($has_sig) { |
| 1292 | push @attrs, "prototype($proto)"; |
| 1293 | } |
| 1294 | else { |
| 1295 | $protosig = $proto; |
| 1296 | } |
| 1297 | } |
| 1298 | if ($cv->CvFLAGS & (CVf_METHOD|CVf_LOCKED|CVf_LVALUE|CVf_ANONCONST)) { |
| 1299 | push @attrs, "lvalue" if $cv->CvFLAGS & CVf_LVALUE; |
| 1300 | push @attrs, "method" if $cv->CvFLAGS & CVf_METHOD; |
| 1301 | push @attrs, "const" if $cv->CvFLAGS & CVf_ANONCONST; |
| 1302 | } |
| 1303 | |
| 1304 | local($self->{'curcv'}) = $cv; |
| 1305 | local($self->{'curcvlex'}); |
| 1306 | local(@$self{qw'curstash warnings hints hinthash'}) |
| 1307 | = @$self{qw'curstash warnings hints hinthash'}; |
| 1308 | my $body; |
| 1309 | my $root = $cv->ROOT; |
| 1310 | local $B::overlay = {}; |
| 1311 | if (not null $root) { |
| 1312 | $self->pad_subs($cv); |
| 1313 | $self->pessimise($root, $cv->START); |
| 1314 | my $lineseq = $root->first; |
| 1315 | if ($lineseq->name eq "lineseq") { |
| 1316 | my $firstop = $lineseq->first; |
| 1317 | |
| 1318 | if ($has_sig) { |
| 1319 | my $o2; |
| 1320 | # try to deparse first few ops as a signature if possible |
| 1321 | if ( $$firstop |
| 1322 | and $firstop->name =~ /^(next|db)state$/ |
| 1323 | and (($o2 = $firstop->sibling)) |
| 1324 | and $$o2) |
| 1325 | { |
| 1326 | if ($o2->name eq 'argcheck') { |
| 1327 | my ($nexto, $sig) = $self->deparse_argops($firstop, $cv); |
| 1328 | if (defined $nexto) { |
| 1329 | $firstop = $nexto; |
| 1330 | $protosig = $sig; |
| 1331 | } |
| 1332 | } |
| 1333 | } |
| 1334 | } |
| 1335 | |
| 1336 | my @ops; |
| 1337 | for (my $o = $firstop; $$o; $o=$o->sibling) { |
| 1338 | push @ops, $o; |
| 1339 | } |
| 1340 | $body = $self->lineseq(undef, 0, @ops).";"; |
| 1341 | if (!$has_sig and $ops[-1]->name =~ /^(next|db)state$/) { |
| 1342 | # this handles void context in |
| 1343 | # use feature signatures; sub ($=1) {} |
| 1344 | $body .= "\n()"; |
| 1345 | } |
| 1346 | my $scope_en = $self->find_scope_en($lineseq); |
| 1347 | if (defined $scope_en) { |
| 1348 | my $subs = join"", $self->seq_subs($scope_en); |
| 1349 | $body .= ";\n$subs" if length($subs); |
| 1350 | } |
| 1351 | } |
| 1352 | else { |
| 1353 | $body = $self->deparse($root->first, 0); |
| 1354 | } |
| 1355 | $body = "{\n\t$body\n\b}"; |
| 1356 | } |
| 1357 | else { |
| 1358 | my $sv = $cv->const_sv; |
| 1359 | if ($$sv) { |
| 1360 | # uh-oh. inlinable sub... format it differently |
| 1361 | $body = "{ " . $self->const($sv, 0) . " }\n"; |
| 1362 | } else { # XSUB? (or just a declaration) |
| 1363 | $body = ';' |
| 1364 | } |
| 1365 | } |
| 1366 | $protosig = defined $protosig ? "($protosig) " : ""; |
| 1367 | my $attrs = ''; |
| 1368 | $attrs = ': ' . join('', map "$_ ", @attrs) if @attrs; |
| 1369 | return "$protosig$attrs$body\n"; |
| 1370 | } |
| 1371 | |
| 1372 | sub deparse_format { |
| 1373 | my $self = shift; |
| 1374 | my $form = shift; |
| 1375 | my @text; |
| 1376 | local($self->{'curcv'}) = $form; |
| 1377 | local($self->{'curcvlex'}); |
| 1378 | local($self->{'in_format'}) = 1; |
| 1379 | local(@$self{qw'curstash warnings hints hinthash'}) |
| 1380 | = @$self{qw'curstash warnings hints hinthash'}; |
| 1381 | my $op = $form->ROOT; |
| 1382 | local $B::overlay = {}; |
| 1383 | $self->pessimise($op, $form->START); |
| 1384 | my $kid; |
| 1385 | return "\f." if $op->first->name eq 'stub' |
| 1386 | || $op->first->name eq 'nextstate'; |
| 1387 | $op = $op->first->first; # skip leavewrite, lineseq |
| 1388 | while (not null $op) { |
| 1389 | $op = $op->sibling; # skip nextstate |
| 1390 | my @exprs; |
| 1391 | $kid = $op->first->sibling; # skip pushmark |
| 1392 | push @text, "\f".$self->const_sv($kid)->PV; |
| 1393 | $kid = $kid->sibling; |
| 1394 | for (; not null $kid; $kid = $kid->sibling) { |
| 1395 | push @exprs, $self->deparse($kid, -1); |
| 1396 | $exprs[-1] =~ s/;\z//; |
| 1397 | } |
| 1398 | push @text, "\f".join(", ", @exprs)."\n" if @exprs; |
| 1399 | $op = $op->sibling; |
| 1400 | } |
| 1401 | return join("", @text) . "\f."; |
| 1402 | } |
| 1403 | |
| 1404 | sub is_scope { |
| 1405 | my $op = shift; |
| 1406 | return $op->name eq "leave" || $op->name eq "scope" |
| 1407 | || $op->name eq "lineseq" |
| 1408 | || ($op->name eq "null" && class($op) eq "UNOP" |
| 1409 | && (is_scope($op->first) || $op->first->name eq "enter")); |
| 1410 | } |
| 1411 | |
| 1412 | sub is_state { |
| 1413 | my $name = $_[0]->name; |
| 1414 | return $name eq "nextstate" || $name eq "dbstate" || $name eq "setstate"; |
| 1415 | } |
| 1416 | |
| 1417 | sub is_miniwhile { # check for one-line loop ('foo() while $y--') |
| 1418 | my $op = shift; |
| 1419 | return (!null($op) and null($op->sibling) |
| 1420 | and $op->name eq "null" and class($op) eq "UNOP" |
| 1421 | and (($op->first->name =~ /^(and|or)$/ |
| 1422 | and $op->first->first->sibling->name eq "lineseq") |
| 1423 | or ($op->first->name eq "lineseq" |
| 1424 | and not null $op->first->first->sibling |
| 1425 | and $op->first->first->sibling->name eq "unstack") |
| 1426 | )); |
| 1427 | } |
| 1428 | |
| 1429 | # Check if the op and its sibling are the initialization and the rest of a |
| 1430 | # for (..;..;..) { ... } loop |
| 1431 | sub is_for_loop { |
| 1432 | my $op = shift; |
| 1433 | # This OP might be almost anything, though it won't be a |
| 1434 | # nextstate. (It's the initialization, so in the canonical case it |
| 1435 | # will be an sassign.) The sibling is (old style) a lineseq whose |
| 1436 | # first child is a nextstate and whose second is a leaveloop, or |
| 1437 | # (new style) an unstack whose sibling is a leaveloop. |
| 1438 | my $lseq = $op->sibling; |
| 1439 | return 0 unless !is_state($op) and !null($lseq); |
| 1440 | if ($lseq->name eq "lineseq") { |
| 1441 | if ($lseq->first && !null($lseq->first) && is_state($lseq->first) |
| 1442 | && (my $sib = $lseq->first->sibling)) { |
| 1443 | return (!null($sib) && $sib->name eq "leaveloop"); |
| 1444 | } |
| 1445 | } elsif ($lseq->name eq "unstack" && ($lseq->flags & OPf_SPECIAL)) { |
| 1446 | my $sib = $lseq->sibling; |
| 1447 | return $sib && !null($sib) && $sib->name eq "leaveloop"; |
| 1448 | } |
| 1449 | return 0; |
| 1450 | } |
| 1451 | |
| 1452 | sub is_scalar { |
| 1453 | my $op = shift; |
| 1454 | return ($op->name eq "rv2sv" or |
| 1455 | $op->name eq "padsv" or |
| 1456 | $op->name eq "gv" or # only in array/hash constructs |
| 1457 | $op->flags & OPf_KIDS && !null($op->first) |
| 1458 | && $op->first->name eq "gvsv"); |
| 1459 | } |
| 1460 | |
| 1461 | sub maybe_parens { |
| 1462 | my $self = shift; |
| 1463 | my($text, $cx, $prec) = @_; |
| 1464 | if ($prec < $cx # unary ops nest just fine |
| 1465 | or $prec == $cx and $cx != 4 and $cx != 16 and $cx != 21 |
| 1466 | or $self->{'parens'}) |
| 1467 | { |
| 1468 | $text = "($text)"; |
| 1469 | # In a unop, let parent reuse our parens; see maybe_parens_unop |
| 1470 | $text = "\cS" . $text if $cx == 16; |
| 1471 | return $text; |
| 1472 | } else { |
| 1473 | return $text; |
| 1474 | } |
| 1475 | } |
| 1476 | |
| 1477 | # same as above, but get around the 'if it looks like a function' rule |
| 1478 | sub maybe_parens_unop { |
| 1479 | my $self = shift; |
| 1480 | my($name, $kid, $cx) = @_; |
| 1481 | if ($cx > 16 or $self->{'parens'}) { |
| 1482 | $kid = $self->deparse($kid, 1); |
| 1483 | if ($name eq "umask" && $kid =~ /^\d+$/) { |
| 1484 | $kid = sprintf("%#o", $kid); |
| 1485 | } |
| 1486 | return $self->keyword($name) . "($kid)"; |
| 1487 | } else { |
| 1488 | $kid = $self->deparse($kid, 16); |
| 1489 | if ($name eq "umask" && $kid =~ /^\d+$/) { |
| 1490 | $kid = sprintf("%#o", $kid); |
| 1491 | } |
| 1492 | $name = $self->keyword($name); |
| 1493 | if (substr($kid, 0, 1) eq "\cS") { |
| 1494 | # use kid's parens |
| 1495 | return $name . substr($kid, 1); |
| 1496 | } elsif (substr($kid, 0, 1) eq "(") { |
| 1497 | # avoid looks-like-a-function trap with extra parens |
| 1498 | # ('+' can lead to ambiguities) |
| 1499 | return "$name(" . $kid . ")"; |
| 1500 | } else { |
| 1501 | return "$name $kid"; |
| 1502 | } |
| 1503 | } |
| 1504 | } |
| 1505 | |
| 1506 | sub maybe_parens_func { |
| 1507 | my $self = shift; |
| 1508 | my($func, $text, $cx, $prec) = @_; |
| 1509 | if ($prec <= $cx or substr($text, 0, 1) eq "(" or $self->{'parens'}) { |
| 1510 | return "$func($text)"; |
| 1511 | } else { |
| 1512 | return "$func $text"; |
| 1513 | } |
| 1514 | } |
| 1515 | |
| 1516 | sub find_our_type { |
| 1517 | my ($self, $name) = @_; |
| 1518 | $self->populate_curcvlex() if !defined $self->{'curcvlex'}; |
| 1519 | my $seq = $self->{'curcop'} ? $self->{'curcop'}->cop_seq : 0; |
| 1520 | for my $a (@{$self->{'curcvlex'}{"o$name"}}) { |
| 1521 | my ($st, undef, $padname) = @$a; |
| 1522 | if ($st >= $seq && $padname->FLAGS & SVpad_TYPED) { |
| 1523 | return $padname->SvSTASH->NAME; |
| 1524 | } |
| 1525 | } |
| 1526 | return ''; |
| 1527 | } |
| 1528 | |
| 1529 | sub maybe_local { |
| 1530 | my $self = shift; |
| 1531 | my($op, $cx, $text) = @_; |
| 1532 | my $name = $op->name; |
| 1533 | my $our_intro = ($name =~ /^(?:(?:gv|rv2)[ash]v|split|refassign |
| 1534 | |lv(?:av)?ref)$/x) |
| 1535 | ? OPpOUR_INTRO |
| 1536 | : 0; |
| 1537 | my $lval_intro = $name eq 'split' ? 0 : OPpLVAL_INTRO; |
| 1538 | # The @a in \(@a) isn't in ref context, but only when the |
| 1539 | # parens are there. |
| 1540 | my $need_parens = $self->{'in_refgen'} && $name =~ /[ah]v\z/ |
| 1541 | && ($op->flags & (OPf_PARENS|OPf_REF)) == OPf_PARENS; |
| 1542 | if ((my $priv = $op->private) & ($lval_intro|$our_intro)) { |
| 1543 | my @our_local; |
| 1544 | push @our_local, "local" if $priv & $lval_intro; |
| 1545 | push @our_local, "our" if $priv & $our_intro; |
| 1546 | my $our_local = join " ", map $self->keyword($_), @our_local; |
| 1547 | if( $our_local[-1] eq 'our' ) { |
| 1548 | if ( $text !~ /^\W(\w+::)*\w+\z/ |
| 1549 | and !utf8::decode($text) || $text !~ /^\W(\w+::)*\w+\z/ |
| 1550 | ) { |
| 1551 | die "Unexpected our($text)\n"; |
| 1552 | } |
| 1553 | $text =~ s/(\w+::)+//; |
| 1554 | |
| 1555 | if (my $type = $self->find_our_type($text)) { |
| 1556 | $our_local .= ' ' . $type; |
| 1557 | } |
| 1558 | } |
| 1559 | return $need_parens ? "($text)" : $text |
| 1560 | if $self->{'avoid_local'}{$$op}; |
| 1561 | if ($need_parens) { |
| 1562 | return "$our_local($text)"; |
| 1563 | } elsif (want_scalar($op)) { |
| 1564 | return "$our_local $text"; |
| 1565 | } else { |
| 1566 | return $self->maybe_parens_func("$our_local", $text, $cx, 16); |
| 1567 | } |
| 1568 | } else { |
| 1569 | return $need_parens ? "($text)" : $text; |
| 1570 | } |
| 1571 | } |
| 1572 | |
| 1573 | sub maybe_targmy { |
| 1574 | my $self = shift; |
| 1575 | my($op, $cx, $func, @args) = @_; |
| 1576 | if ($op->private & OPpTARGET_MY) { |
| 1577 | my $var = $self->padname($op->targ); |
| 1578 | my $val = $func->($self, $op, 7, @args); |
| 1579 | return $self->maybe_parens("$var = $val", $cx, 7); |
| 1580 | } else { |
| 1581 | return $func->($self, $op, $cx, @args); |
| 1582 | } |
| 1583 | } |
| 1584 | |
| 1585 | sub padname_sv { |
| 1586 | my $self = shift; |
| 1587 | my $targ = shift; |
| 1588 | return $self->{'curcv'}->PADLIST->ARRAYelt(0)->ARRAYelt($targ); |
| 1589 | } |
| 1590 | |
| 1591 | sub maybe_my { |
| 1592 | my $self = shift; |
| 1593 | my($op, $cx, $text, $padname, $forbid_parens) = @_; |
| 1594 | # The @a in \(@a) isn't in ref context, but only when the |
| 1595 | # parens are there. |
| 1596 | my $need_parens = !$forbid_parens && $self->{'in_refgen'} |
| 1597 | && $op->name =~ /[ah]v\z/ |
| 1598 | && ($op->flags & (OPf_PARENS|OPf_REF)) == OPf_PARENS; |
| 1599 | # The @a in \my @a must not have parens. |
| 1600 | if (!$need_parens && $self->{'in_refgen'}) { |
| 1601 | $forbid_parens = 1; |
| 1602 | } |
| 1603 | if ($op->private & OPpLVAL_INTRO and not $self->{'avoid_local'}{$$op}) { |
| 1604 | # Check $padname->FLAGS for statehood, rather than $op->private, |
| 1605 | # because enteriter ops do not carry the flag. |
| 1606 | my $my = |
| 1607 | $self->keyword($padname->FLAGS & SVpad_STATE ? "state" : "my"); |
| 1608 | if ($padname->FLAGS & SVpad_TYPED) { |
| 1609 | $my .= ' ' . $padname->SvSTASH->NAME; |
| 1610 | } |
| 1611 | if ($need_parens) { |
| 1612 | return "$my($text)"; |
| 1613 | } elsif ($forbid_parens || want_scalar($op)) { |
| 1614 | return "$my $text"; |
| 1615 | } else { |
| 1616 | return $self->maybe_parens_func($my, $text, $cx, 16); |
| 1617 | } |
| 1618 | } else { |
| 1619 | return $need_parens ? "($text)" : $text; |
| 1620 | } |
| 1621 | } |
| 1622 | |
| 1623 | # The following OPs don't have functions: |
| 1624 | |
| 1625 | # pp_padany -- does not exist after parsing |
| 1626 | |
| 1627 | sub AUTOLOAD { |
| 1628 | if ($AUTOLOAD =~ s/^.*::pp_//) { |
| 1629 | warn "unexpected OP_". |
| 1630 | ($_[1]->type == OP_CUSTOM ? "CUSTOM ($AUTOLOAD)" : uc $AUTOLOAD); |
| 1631 | return "XXX"; |
| 1632 | } else { |
| 1633 | die "Undefined subroutine $AUTOLOAD called"; |
| 1634 | } |
| 1635 | } |
| 1636 | |
| 1637 | sub DESTROY {} # Do not AUTOLOAD |
| 1638 | |
| 1639 | # $root should be the op which represents the root of whatever |
| 1640 | # we're sequencing here. If it's undefined, then we don't append |
| 1641 | # any subroutine declarations to the deparsed ops, otherwise we |
| 1642 | # append appropriate declarations. |
| 1643 | sub lineseq { |
| 1644 | my($self, $root, $cx, @ops) = @_; |
| 1645 | my($expr, @exprs); |
| 1646 | |
| 1647 | my $out_cop = $self->{'curcop'}; |
| 1648 | my $out_seq = defined($out_cop) ? $out_cop->cop_seq : undef; |
| 1649 | my $limit_seq; |
| 1650 | if (defined $root) { |
| 1651 | $limit_seq = $out_seq; |
| 1652 | my $nseq; |
| 1653 | $nseq = $self->find_scope_st($root->sibling) if ${$root->sibling}; |
| 1654 | $limit_seq = $nseq if !defined($limit_seq) |
| 1655 | or defined($nseq) && $nseq < $limit_seq; |
| 1656 | } |
| 1657 | $limit_seq = $self->{'limit_seq'} |
| 1658 | if defined($self->{'limit_seq'}) |
| 1659 | && (!defined($limit_seq) || $self->{'limit_seq'} < $limit_seq); |
| 1660 | local $self->{'limit_seq'} = $limit_seq; |
| 1661 | |
| 1662 | $self->walk_lineseq($root, \@ops, |
| 1663 | sub { push @exprs, $_[0]} ); |
| 1664 | |
| 1665 | my $sep = $cx ? '; ' : ";\n"; |
| 1666 | my $body = join($sep, grep {length} @exprs); |
| 1667 | my $subs = ""; |
| 1668 | if (defined $root && defined $limit_seq && !$self->{'in_format'}) { |
| 1669 | $subs = join "\n", $self->seq_subs($limit_seq); |
| 1670 | } |
| 1671 | return join($sep, grep {length} $body, $subs); |
| 1672 | } |
| 1673 | |
| 1674 | sub scopeop { |
| 1675 | my($real_block, $self, $op, $cx) = @_; |
| 1676 | my $kid; |
| 1677 | my @kids; |
| 1678 | |
| 1679 | local(@$self{qw'curstash warnings hints hinthash'}) |
| 1680 | = @$self{qw'curstash warnings hints hinthash'} if $real_block; |
| 1681 | if ($real_block) { |
| 1682 | $kid = $op->first->sibling; # skip enter |
| 1683 | if (is_miniwhile($kid)) { |
| 1684 | my $top = $kid->first; |
| 1685 | my $name = $top->name; |
| 1686 | if ($name eq "and") { |
| 1687 | $name = $self->keyword("while"); |
| 1688 | } elsif ($name eq "or") { |
| 1689 | $name = $self->keyword("until"); |
| 1690 | } else { # no conditional -> while 1 or until 0 |
| 1691 | return $self->deparse($top->first, 1) . " " |
| 1692 | . $self->keyword("while") . " 1"; |
| 1693 | } |
| 1694 | my $cond = $top->first; |
| 1695 | my $body = $cond->sibling->first; # skip lineseq |
| 1696 | $cond = $self->deparse($cond, 1); |
| 1697 | $body = $self->deparse($body, 1); |
| 1698 | return "$body $name $cond"; |
| 1699 | } |
| 1700 | } else { |
| 1701 | $kid = $op->first; |
| 1702 | } |
| 1703 | for (; !null($kid); $kid = $kid->sibling) { |
| 1704 | push @kids, $kid; |
| 1705 | } |
| 1706 | if ($cx > 0) { # inside an expression, (a do {} while for lineseq) |
| 1707 | my $body = $self->lineseq($op, 0, @kids); |
| 1708 | return is_lexical_subs(@kids) |
| 1709 | ? $body |
| 1710 | : ($self->lex_in_scope("&do") ? "CORE::do" : "do") |
| 1711 | . " {\n\t$body\n\b}"; |
| 1712 | } else { |
| 1713 | my $lineseq = $self->lineseq($op, $cx, @kids); |
| 1714 | return (length ($lineseq) ? "$lineseq;" : ""); |
| 1715 | } |
| 1716 | } |
| 1717 | |
| 1718 | sub pp_scope { scopeop(0, @_); } |
| 1719 | sub pp_lineseq { scopeop(0, @_); } |
| 1720 | sub pp_leave { scopeop(1, @_); } |
| 1721 | |
| 1722 | # This is a special case of scopeop and lineseq, for the case of the |
| 1723 | # main_root. The difference is that we print the output statements as |
| 1724 | # soon as we get them, for the sake of impatient users. |
| 1725 | sub deparse_root { |
| 1726 | my $self = shift; |
| 1727 | my($op) = @_; |
| 1728 | local(@$self{qw'curstash warnings hints hinthash'}) |
| 1729 | = @$self{qw'curstash warnings hints hinthash'}; |
| 1730 | my @kids; |
| 1731 | return if null $op->first; # Can happen, e.g., for Bytecode without -k |
| 1732 | for (my $kid = $op->first->sibling; !null($kid); $kid = $kid->sibling) { |
| 1733 | push @kids, $kid; |
| 1734 | } |
| 1735 | $self->walk_lineseq($op, \@kids, |
| 1736 | sub { return unless length $_[0]; |
| 1737 | print $self->indent($_[0].';'); |
| 1738 | print "\n" |
| 1739 | unless $_[1] == $#kids; |
| 1740 | }); |
| 1741 | } |
| 1742 | |
| 1743 | sub walk_lineseq { |
| 1744 | my ($self, $op, $kids, $callback) = @_; |
| 1745 | my @kids = @$kids; |
| 1746 | for (my $i = 0; $i < @kids; $i++) { |
| 1747 | my $expr = ""; |
| 1748 | if (is_state $kids[$i]) { |
| 1749 | $expr = $self->deparse($kids[$i++], 0); |
| 1750 | if ($i > $#kids) { |
| 1751 | $callback->($expr, $i); |
| 1752 | last; |
| 1753 | } |
| 1754 | } |
| 1755 | if (is_for_loop($kids[$i])) { |
| 1756 | $callback->($expr . $self->for_loop($kids[$i], 0), |
| 1757 | $i += $kids[$i]->sibling->name eq "unstack" ? 2 : 1); |
| 1758 | next; |
| 1759 | } |
| 1760 | my $expr2 = $self->deparse($kids[$i], (@kids != 1)/2); |
| 1761 | $expr2 =~ s/^sub :(?!:)/+sub :/; # statement label otherwise |
| 1762 | $expr .= $expr2; |
| 1763 | $callback->($expr, $i); |
| 1764 | } |
| 1765 | } |
| 1766 | |
| 1767 | # The BEGIN {} is used here because otherwise this code isn't executed |
| 1768 | # when you run B::Deparse on itself. |
| 1769 | my %globalnames; |
| 1770 | BEGIN { map($globalnames{$_}++, "SIG", "STDIN", "STDOUT", "STDERR", "INC", |
| 1771 | "ENV", "ARGV", "ARGVOUT", "_"); } |
| 1772 | |
| 1773 | sub gv_name { |
| 1774 | my $self = shift; |
| 1775 | my $gv = shift; |
| 1776 | my $raw = shift; |
| 1777 | #Carp::confess() unless ref($gv) eq "B::GV"; |
| 1778 | my $cv = $gv->FLAGS & SVf_ROK ? $gv->RV : 0; |
| 1779 | my $stash = ($cv || $gv)->STASH->NAME; |
| 1780 | my $name = $raw |
| 1781 | ? $cv ? $cv->NAME_HEK || $cv->GV->NAME : $gv->NAME |
| 1782 | : $cv |
| 1783 | ? B::safename($cv->NAME_HEK || $cv->GV->NAME) |
| 1784 | : $gv->SAFENAME; |
| 1785 | if ($stash eq 'main' && $name =~ /^::/) { |
| 1786 | $stash = '::'; |
| 1787 | } |
| 1788 | elsif (($stash eq 'main' |
| 1789 | && ($globalnames{$name} || $name =~ /^[^A-Za-z_:]/)) |
| 1790 | or ($stash eq $self->{'curstash'} && !$globalnames{$name} |
| 1791 | && ($stash eq 'main' || $name !~ /::/)) |
| 1792 | ) |
| 1793 | { |
| 1794 | $stash = ""; |
| 1795 | } else { |
| 1796 | $stash = $stash . "::"; |
| 1797 | } |
| 1798 | if (!$raw and $name =~ /^(\^..|{)/) { |
| 1799 | $name = "{$name}"; # ${^WARNING_BITS}, etc and ${ |
| 1800 | } |
| 1801 | return $stash . $name; |
| 1802 | } |
| 1803 | |
| 1804 | # Return the name to use for a stash variable. |
| 1805 | # If a lexical with the same name is in scope, or |
| 1806 | # if strictures are enabled, it may need to be |
| 1807 | # fully-qualified. |
| 1808 | sub stash_variable { |
| 1809 | my ($self, $prefix, $name, $cx) = @_; |
| 1810 | |
| 1811 | return $prefix.$self->maybe_qualify($prefix, $name) if $name =~ /::/; |
| 1812 | |
| 1813 | unless ($prefix eq '$' || $prefix eq '@' || $prefix eq '&' || #' |
| 1814 | $prefix eq '%' || $prefix eq '$#') { |
| 1815 | return "$prefix$name"; |
| 1816 | } |
| 1817 | |
| 1818 | if ($name =~ /^[^[:alpha:]_+-]$/) { |
| 1819 | if (defined $cx && $cx == 26) { |
| 1820 | if ($prefix eq '@') { |
| 1821 | return "$prefix\{$name}"; |
| 1822 | } |
| 1823 | elsif ($name eq '#') { return '${#}' } # "${#}a" vs "$#a" |
| 1824 | } |
| 1825 | if ($prefix eq '$#') { |
| 1826 | return "\$#{$name}"; |
| 1827 | } |
| 1828 | } |
| 1829 | |
| 1830 | return $prefix . $self->maybe_qualify($prefix, $name); |
| 1831 | } |
| 1832 | |
| 1833 | my %unctrl = # portable to EBCDIC |
| 1834 | ( |
| 1835 | "\c@" => '@', # unused |
| 1836 | "\cA" => 'A', |
| 1837 | "\cB" => 'B', |
| 1838 | "\cC" => 'C', |
| 1839 | "\cD" => 'D', |
| 1840 | "\cE" => 'E', |
| 1841 | "\cF" => 'F', |
| 1842 | "\cG" => 'G', |
| 1843 | "\cH" => 'H', |
| 1844 | "\cI" => 'I', |
| 1845 | "\cJ" => 'J', |
| 1846 | "\cK" => 'K', |
| 1847 | "\cL" => 'L', |
| 1848 | "\cM" => 'M', |
| 1849 | "\cN" => 'N', |
| 1850 | "\cO" => 'O', |
| 1851 | "\cP" => 'P', |
| 1852 | "\cQ" => 'Q', |
| 1853 | "\cR" => 'R', |
| 1854 | "\cS" => 'S', |
| 1855 | "\cT" => 'T', |
| 1856 | "\cU" => 'U', |
| 1857 | "\cV" => 'V', |
| 1858 | "\cW" => 'W', |
| 1859 | "\cX" => 'X', |
| 1860 | "\cY" => 'Y', |
| 1861 | "\cZ" => 'Z', |
| 1862 | "\c[" => '[', # unused |
| 1863 | "\c\\" => '\\', # unused |
| 1864 | "\c]" => ']', # unused |
| 1865 | "\c_" => '_', # unused |
| 1866 | ); |
| 1867 | |
| 1868 | # Return just the name, without the prefix. It may be returned as a quoted |
| 1869 | # string. The second return value is a boolean indicating that. |
| 1870 | sub stash_variable_name { |
| 1871 | my($self, $prefix, $gv) = @_; |
| 1872 | my $name = $self->gv_name($gv, 1); |
| 1873 | $name = $self->maybe_qualify($prefix,$name); |
| 1874 | if ($name =~ /^(?:\S|(?!\d)[\ca-\cz]?(?:\w|::)*|\d+)\z/) { |
| 1875 | $name =~ s/^([\ca-\cz])/'^' . $unctrl{$1}/e; |
| 1876 | $name =~ /^(\^..|{)/ and $name = "{$name}"; |
| 1877 | return $name, 0; # not quoted |
| 1878 | } |
| 1879 | else { |
| 1880 | single_delim("q", "'", $name, $self), 1; |
| 1881 | } |
| 1882 | } |
| 1883 | |
| 1884 | sub maybe_qualify { |
| 1885 | my ($self,$prefix,$name) = @_; |
| 1886 | my $v = ($prefix eq '$#' ? '@' : $prefix) . $name; |
| 1887 | if ($prefix eq "") { |
| 1888 | $name .= "::" if $name =~ /(?:\ACORE::[^:]*|::)\z/; |
| 1889 | return $name; |
| 1890 | } |
| 1891 | return $name if $name =~ /::/; |
| 1892 | return $self->{'curstash'}.'::'. $name |
| 1893 | if |
| 1894 | $name =~ /^(?!\d)\w/ # alphabetic |
| 1895 | && $v !~ /^\$[ab]\z/ # not $a or $b |
| 1896 | && $v =~ /\A[\$\@\%\&]/ # scalar, array, hash, or sub |
| 1897 | && !$globalnames{$name} # not a global name |
| 1898 | && $self->{hints} & $strict_bits{vars} # strict vars |
| 1899 | && !$self->lex_in_scope($v,1) # no "our" |
| 1900 | or $self->lex_in_scope($v); # conflicts with "my" variable |
| 1901 | return $name; |
| 1902 | } |
| 1903 | |
| 1904 | sub lex_in_scope { |
| 1905 | my ($self, $name, $our) = @_; |
| 1906 | substr $name, 0, 0, = $our ? 'o' : 'm'; # our/my |
| 1907 | $self->populate_curcvlex() if !defined $self->{'curcvlex'}; |
| 1908 | |
| 1909 | return 0 if !defined($self->{'curcop'}); |
| 1910 | my $seq = $self->{'curcop'}->cop_seq; |
| 1911 | return 0 if !exists $self->{'curcvlex'}{$name}; |
| 1912 | for my $a (@{$self->{'curcvlex'}{$name}}) { |
| 1913 | my ($st, $en) = @$a; |
| 1914 | return 1 if $seq > $st && $seq <= $en; |
| 1915 | } |
| 1916 | return 0; |
| 1917 | } |
| 1918 | |
| 1919 | sub populate_curcvlex { |
| 1920 | my $self = shift; |
| 1921 | for (my $cv = $self->{'curcv'}; class($cv) eq "CV"; $cv = $cv->OUTSIDE) { |
| 1922 | my $padlist = $cv->PADLIST; |
| 1923 | # an undef CV still in lexical chain |
| 1924 | next if class($padlist) eq "SPECIAL"; |
| 1925 | my @padlist = $padlist->ARRAY; |
| 1926 | my @ns = $padlist[0]->ARRAY; |
| 1927 | |
| 1928 | for (my $i=0; $i<@ns; ++$i) { |
| 1929 | next if class($ns[$i]) eq "SPECIAL"; |
| 1930 | if (class($ns[$i]) eq "PV") { |
| 1931 | # Probably that pesky lexical @_ |
| 1932 | next; |
| 1933 | } |
| 1934 | my $name = $ns[$i]->PVX; |
| 1935 | next unless defined $name; |
| 1936 | my ($seq_st, $seq_en) = |
| 1937 | ($ns[$i]->FLAGS & SVf_FAKE) |
| 1938 | ? (0, 999999) |
| 1939 | : ($ns[$i]->COP_SEQ_RANGE_LOW, $ns[$i]->COP_SEQ_RANGE_HIGH); |
| 1940 | |
| 1941 | push @{$self->{'curcvlex'}{ |
| 1942 | ($ns[$i]->FLAGS & SVpad_OUR ? 'o' : 'm') . $name |
| 1943 | }}, [$seq_st, $seq_en, $ns[$i]]; |
| 1944 | } |
| 1945 | } |
| 1946 | } |
| 1947 | |
| 1948 | sub find_scope_st { ((find_scope(@_))[0]); } |
| 1949 | sub find_scope_en { ((find_scope(@_))[1]); } |
| 1950 | |
| 1951 | # Recurses down the tree, looking for pad variable introductions and COPs |
| 1952 | sub find_scope { |
| 1953 | my ($self, $op, $scope_st, $scope_en) = @_; |
| 1954 | carp("Undefined op in find_scope") if !defined $op; |
| 1955 | return ($scope_st, $scope_en) unless $op->flags & OPf_KIDS; |
| 1956 | |
| 1957 | my @queue = ($op); |
| 1958 | while(my $op = shift @queue ) { |
| 1959 | for (my $o=$op->first; $$o; $o=$o->sibling) { |
| 1960 | if ($o->name =~ /^pad.v$/ && $o->private & OPpLVAL_INTRO) { |
| 1961 | my $s = int($self->padname_sv($o->targ)->COP_SEQ_RANGE_LOW); |
| 1962 | my $e = $self->padname_sv($o->targ)->COP_SEQ_RANGE_HIGH; |
| 1963 | $scope_st = $s if !defined($scope_st) || $s < $scope_st; |
| 1964 | $scope_en = $e if !defined($scope_en) || $e > $scope_en; |
| 1965 | return ($scope_st, $scope_en); |
| 1966 | } |
| 1967 | elsif (is_state($o)) { |
| 1968 | my $c = $o->cop_seq; |
| 1969 | $scope_st = $c if !defined($scope_st) || $c < $scope_st; |
| 1970 | $scope_en = $c if !defined($scope_en) || $c > $scope_en; |
| 1971 | return ($scope_st, $scope_en); |
| 1972 | } |
| 1973 | elsif ($o->flags & OPf_KIDS) { |
| 1974 | unshift (@queue, $o); |
| 1975 | } |
| 1976 | } |
| 1977 | } |
| 1978 | |
| 1979 | return ($scope_st, $scope_en); |
| 1980 | } |
| 1981 | |
| 1982 | # Returns a list of subs which should be inserted before the COP |
| 1983 | sub cop_subs { |
| 1984 | my ($self, $op, $out_seq) = @_; |
| 1985 | my $seq = $op->cop_seq; |
| 1986 | $seq = $out_seq if defined($out_seq) && $out_seq < $seq; |
| 1987 | return $self->seq_subs($seq); |
| 1988 | } |
| 1989 | |
| 1990 | sub seq_subs { |
| 1991 | my ($self, $seq) = @_; |
| 1992 | my @text; |
| 1993 | #push @text, "# ($seq)\n"; |
| 1994 | |
| 1995 | return "" if !defined $seq; |
| 1996 | my @pending; |
| 1997 | while (scalar(@{$self->{'subs_todo'}}) |
| 1998 | and $seq > $self->{'subs_todo'}[0][0]) { |
| 1999 | my $cv = $self->{'subs_todo'}[0][1]; |
| 2000 | # Skip the OUTSIDE check for lexical subs. We may be deparsing a |
| 2001 | # cloned anon sub with lexical subs declared in it, in which case |
| 2002 | # the OUTSIDE pointer points to the anon protosub. |
| 2003 | my $lexical = ref $self->{'subs_todo'}[0][3]; |
| 2004 | my $outside = !$lexical && $cv && $cv->OUTSIDE; |
| 2005 | if (!$lexical and $cv |
| 2006 | and ${$cv->OUTSIDE || \0} != ${$self->{'curcv'}}) |
| 2007 | { |
| 2008 | push @pending, shift @{$self->{'subs_todo'}}; |
| 2009 | next; |
| 2010 | } |
| 2011 | push @text, $self->next_todo; |
| 2012 | } |
| 2013 | unshift @{$self->{'subs_todo'}}, @pending; |
| 2014 | return @text; |
| 2015 | } |
| 2016 | |
| 2017 | sub _features_from_bundle { |
| 2018 | my ($hints, $hh) = @_; |
| 2019 | foreach (@{$feature::feature_bundle{@feature::hint_bundles[$hints >> $feature::hint_shift]}}) { |
| 2020 | $hh->{$feature::feature{$_}} = 1; |
| 2021 | } |
| 2022 | return $hh; |
| 2023 | } |
| 2024 | |
| 2025 | # generate any pragmas, 'package foo' etc needed to synchronise |
| 2026 | # with the given cop |
| 2027 | |
| 2028 | sub pragmata { |
| 2029 | my $self = shift; |
| 2030 | my($op) = @_; |
| 2031 | |
| 2032 | my @text; |
| 2033 | |
| 2034 | my $stash = $op->stashpv; |
| 2035 | if ($stash ne $self->{'curstash'}) { |
| 2036 | push @text, $self->keyword("package") . " $stash;\n"; |
| 2037 | $self->{'curstash'} = $stash; |
| 2038 | } |
| 2039 | |
| 2040 | if (OPpCONST_ARYBASE && $self->{'arybase'} != $op->arybase) { |
| 2041 | push @text, '$[ = '. $op->arybase .";\n"; |
| 2042 | $self->{'arybase'} = $op->arybase; |
| 2043 | } |
| 2044 | |
| 2045 | my $warnings = $op->warnings; |
| 2046 | my $warning_bits; |
| 2047 | if ($warnings->isa("B::SPECIAL") && $$warnings == 4) { |
| 2048 | $warning_bits = $warnings::Bits{"all"} & WARN_MASK; |
| 2049 | } |
| 2050 | elsif ($warnings->isa("B::SPECIAL") && $$warnings == 5) { |
| 2051 | $warning_bits = $warnings::NONE; |
| 2052 | } |
| 2053 | elsif ($warnings->isa("B::SPECIAL")) { |
| 2054 | $warning_bits = undef; |
| 2055 | } |
| 2056 | else { |
| 2057 | $warning_bits = $warnings->PV & WARN_MASK; |
| 2058 | } |
| 2059 | |
| 2060 | if (defined ($warning_bits) and |
| 2061 | !defined($self->{warnings}) || $self->{'warnings'} ne $warning_bits) { |
| 2062 | push @text, |
| 2063 | $self->declare_warnings($self->{'warnings'}, $warning_bits); |
| 2064 | $self->{'warnings'} = $warning_bits; |
| 2065 | } |
| 2066 | |
| 2067 | my $hints = $op->hints; |
| 2068 | my $old_hints = $self->{'hints'}; |
| 2069 | if ($self->{'hints'} != $hints) { |
| 2070 | push @text, $self->declare_hints($self->{'hints'}, $hints); |
| 2071 | $self->{'hints'} = $hints; |
| 2072 | } |
| 2073 | |
| 2074 | my $newhh; |
| 2075 | $newhh = $op->hints_hash->HASH; |
| 2076 | |
| 2077 | { |
| 2078 | # feature bundle hints |
| 2079 | my $from = $old_hints & $feature::hint_mask; |
| 2080 | my $to = $ hints & $feature::hint_mask; |
| 2081 | if ($from != $to) { |
| 2082 | if ($to == $feature::hint_mask) { |
| 2083 | if ($self->{'hinthash'}) { |
| 2084 | delete $self->{'hinthash'}{$_} |
| 2085 | for grep /^feature_/, keys %{$self->{'hinthash'}}; |
| 2086 | } |
| 2087 | else { $self->{'hinthash'} = {} } |
| 2088 | $self->{'hinthash'} |
| 2089 | = _features_from_bundle($from, $self->{'hinthash'}); |
| 2090 | } |
| 2091 | else { |
| 2092 | my $bundle = |
| 2093 | $feature::hint_bundles[$to >> $feature::hint_shift]; |
| 2094 | $bundle =~ s/(\d[13579])\z/$1+1/e; # 5.11 => 5.12 |
| 2095 | push @text, |
| 2096 | $self->keyword("no") . " feature ':all';\n", |
| 2097 | $self->keyword("use") . " feature ':$bundle';\n"; |
| 2098 | } |
| 2099 | } |
| 2100 | } |
| 2101 | |
| 2102 | { |
| 2103 | push @text, $self->declare_hinthash( |
| 2104 | $self->{'hinthash'}, $newhh, |
| 2105 | $self->{indent_size}, $self->{hints}, |
| 2106 | ); |
| 2107 | $self->{'hinthash'} = $newhh; |
| 2108 | } |
| 2109 | |
| 2110 | return join("", @text); |
| 2111 | } |
| 2112 | |
| 2113 | |
| 2114 | # Notice how subs and formats are inserted between statements here; |
| 2115 | # also $[ assignments and pragmas. |
| 2116 | sub pp_nextstate { |
| 2117 | my $self = shift; |
| 2118 | my($op, $cx) = @_; |
| 2119 | $self->{'curcop'} = $op; |
| 2120 | |
| 2121 | my @text; |
| 2122 | |
| 2123 | my @subs = $self->cop_subs($op); |
| 2124 | if (@subs) { |
| 2125 | # Special marker to swallow up the semicolon |
| 2126 | push @subs, "\cK"; |
| 2127 | } |
| 2128 | push @text, @subs; |
| 2129 | |
| 2130 | push @text, $self->pragmata($op); |
| 2131 | |
| 2132 | |
| 2133 | # This should go after of any branches that add statements, to |
| 2134 | # increase the chances that it refers to the same line it did in |
| 2135 | # the original program. |
| 2136 | if ($self->{'linenums'} && $cx != .5) { # $cx == .5 means in a format |
| 2137 | push @text, "\f#line " . $op->line . |
| 2138 | ' "' . $op->file, qq'"\n'; |
| 2139 | } |
| 2140 | |
| 2141 | push @text, $op->label . ": " if $op->label; |
| 2142 | |
| 2143 | return join("", @text); |
| 2144 | } |
| 2145 | |
| 2146 | sub declare_warnings { |
| 2147 | my ($self, $from, $to) = @_; |
| 2148 | $from //= ''; |
| 2149 | my $all = (warnings::bits("all") & WARN_MASK); |
| 2150 | unless ((($from & WARN_MASK) & ~$all) =~ /[^\0]/) { |
| 2151 | # no FATAL bits need turning off |
| 2152 | if ( ($to & WARN_MASK) eq $all) { |
| 2153 | return $self->keyword("use") . " warnings;\n"; |
| 2154 | } |
| 2155 | elsif (($to & WARN_MASK) eq ("\0"x length($to) & WARN_MASK)) { |
| 2156 | return $self->keyword("no") . " warnings;\n"; |
| 2157 | } |
| 2158 | } |
| 2159 | |
| 2160 | return "BEGIN {\${^WARNING_BITS} = \"" |
| 2161 | . join("", map { sprintf("\\x%02x", ord $_) } split "", $to) |
| 2162 | . "\"}\n\cK"; |
| 2163 | } |
| 2164 | |
| 2165 | sub declare_hints { |
| 2166 | my ($self, $from, $to) = @_; |
| 2167 | my $use = $to & ~$from; |
| 2168 | my $no = $from & ~$to; |
| 2169 | my $decls = ""; |
| 2170 | for my $pragma (hint_pragmas($use)) { |
| 2171 | $decls .= $self->keyword("use") . " $pragma;\n"; |
| 2172 | } |
| 2173 | for my $pragma (hint_pragmas($no)) { |
| 2174 | $decls .= $self->keyword("no") . " $pragma;\n"; |
| 2175 | } |
| 2176 | return $decls; |
| 2177 | } |
| 2178 | |
| 2179 | # Internal implementation hints that the core sets automatically, so don't need |
| 2180 | # (or want) to be passed back to the user |
| 2181 | my %ignored_hints = ( |
| 2182 | 'open<' => 1, |
| 2183 | 'open>' => 1, |
| 2184 | ':' => 1, |
| 2185 | 'strict/refs' => 1, |
| 2186 | 'strict/subs' => 1, |
| 2187 | 'strict/vars' => 1, |
| 2188 | ); |
| 2189 | |
| 2190 | my %rev_feature; |
| 2191 | |
| 2192 | sub declare_hinthash { |
| 2193 | my ($self, $from, $to, $indent, $hints) = @_; |
| 2194 | my $doing_features = |
| 2195 | ($hints & $feature::hint_mask) == $feature::hint_mask; |
| 2196 | my @decls; |
| 2197 | my @features; |
| 2198 | my @unfeatures; # bugs? |
| 2199 | for my $key (sort keys %$to) { |
| 2200 | next if $ignored_hints{$key}; |
| 2201 | my $is_feature = $key =~ /^feature_/; |
| 2202 | next if $is_feature and not $doing_features; |
| 2203 | if (!exists $from->{$key} or $from->{$key} ne $to->{$key}) { |
| 2204 | push(@features, $key), next if $is_feature; |
| 2205 | push @decls, |
| 2206 | qq(\$^H{) . single_delim("q", "'", $key, $self) . qq(} = ) |
| 2207 | . ( |
| 2208 | defined $to->{$key} |
| 2209 | ? single_delim("q", "'", $to->{$key}, $self) |
| 2210 | : 'undef' |
| 2211 | ) |
| 2212 | . qq(;); |
| 2213 | } |
| 2214 | } |
| 2215 | for my $key (sort keys %$from) { |
| 2216 | next if $ignored_hints{$key}; |
| 2217 | my $is_feature = $key =~ /^feature_/; |
| 2218 | next if $is_feature and not $doing_features; |
| 2219 | if (!exists $to->{$key}) { |
| 2220 | push(@unfeatures, $key), next if $is_feature; |
| 2221 | push @decls, qq(delete \$^H{'$key'};); |
| 2222 | } |
| 2223 | } |
| 2224 | my @ret; |
| 2225 | if (@features || @unfeatures) { |
| 2226 | if (!%rev_feature) { %rev_feature = reverse %feature::feature } |
| 2227 | } |
| 2228 | if (@features) { |
| 2229 | push @ret, $self->keyword("use") . " feature " |
| 2230 | . join(", ", map "'$rev_feature{$_}'", @features) . ";\n"; |
| 2231 | } |
| 2232 | if (@unfeatures) { |
| 2233 | push @ret, $self->keyword("no") . " feature " |
| 2234 | . join(", ", map "'$rev_feature{$_}'", @unfeatures) |
| 2235 | . ";\n"; |
| 2236 | } |
| 2237 | @decls and |
| 2238 | push @ret, |
| 2239 | join("\n" . (" " x $indent), "BEGIN {", @decls) . "\n}\n\cK"; |
| 2240 | return @ret; |
| 2241 | } |
| 2242 | |
| 2243 | sub hint_pragmas { |
| 2244 | my ($bits) = @_; |
| 2245 | my (@pragmas, @strict); |
| 2246 | push @pragmas, "integer" if $bits & 0x1; |
| 2247 | for (sort keys %strict_bits) { |
| 2248 | push @strict, "'$_'" if $bits & $strict_bits{$_}; |
| 2249 | } |
| 2250 | if (@strict == keys %strict_bits) { |
| 2251 | push @pragmas, "strict"; |
| 2252 | } |
| 2253 | elsif (@strict) { |
| 2254 | push @pragmas, "strict " . join ', ', @strict; |
| 2255 | } |
| 2256 | push @pragmas, "bytes" if $bits & 0x8; |
| 2257 | return @pragmas; |
| 2258 | } |
| 2259 | |
| 2260 | sub pp_dbstate { pp_nextstate(@_) } |
| 2261 | sub pp_setstate { pp_nextstate(@_) } |
| 2262 | |
| 2263 | sub pp_unstack { return "" } # see also leaveloop |
| 2264 | |
| 2265 | my %feature_keywords = ( |
| 2266 | # keyword => 'feature', |
| 2267 | state => 'state', |
| 2268 | say => 'say', |
| 2269 | given => 'switch', |
| 2270 | when => 'switch', |
| 2271 | default => 'switch', |
| 2272 | break => 'switch', |
| 2273 | evalbytes=>'evalbytes', |
| 2274 | __SUB__ => '__SUB__', |
| 2275 | fc => 'fc', |
| 2276 | ); |
| 2277 | |
| 2278 | # keywords that are strong and also have a prototype |
| 2279 | # |
| 2280 | my %strong_proto_keywords = map { $_ => 1 } qw( |
| 2281 | pos |
| 2282 | prototype |
| 2283 | scalar |
| 2284 | study |
| 2285 | undef |
| 2286 | ); |
| 2287 | |
| 2288 | sub feature_enabled { |
| 2289 | my($self,$name) = @_; |
| 2290 | my $hh; |
| 2291 | my $hints = $self->{hints} & $feature::hint_mask; |
| 2292 | if ($hints && $hints != $feature::hint_mask) { |
| 2293 | $hh = _features_from_bundle($hints); |
| 2294 | } |
| 2295 | elsif ($hints) { $hh = $self->{'hinthash'} } |
| 2296 | return $hh && $hh->{"feature_$feature_keywords{$name}"} |
| 2297 | } |
| 2298 | |
| 2299 | sub keyword { |
| 2300 | my $self = shift; |
| 2301 | my $name = shift; |
| 2302 | return $name if $name =~ /^CORE::/; # just in case |
| 2303 | if (exists $feature_keywords{$name}) { |
| 2304 | return "CORE::$name" if not $self->feature_enabled($name); |
| 2305 | } |
| 2306 | # This sub may be called for a program that has no nextstate ops. In |
| 2307 | # that case we may have a lexical sub named no/use/sub in scope but |
| 2308 | # but $self->lex_in_scope will return false because it depends on the |
| 2309 | # current nextstate op. So we need this alternate method if there is |
| 2310 | # no current cop. |
| 2311 | if (!$self->{'curcop'}) { |
| 2312 | $self->populate_curcvlex() if !defined $self->{'curcvlex'}; |
| 2313 | return "CORE::$name" if exists $self->{'curcvlex'}{"m&$name"} |
| 2314 | || exists $self->{'curcvlex'}{"o&$name"}; |
| 2315 | } elsif ($self->lex_in_scope("&$name") |
| 2316 | || $self->lex_in_scope("&$name", 1)) { |
| 2317 | return "CORE::$name"; |
| 2318 | } |
| 2319 | if ($strong_proto_keywords{$name} |
| 2320 | || ($name !~ /^(?:chom?p|do|exec|glob|s(?:elect|ystem))\z/ |
| 2321 | && !defined eval{prototype "CORE::$name"}) |
| 2322 | ) { return $name } |
| 2323 | if ( |
| 2324 | exists $self->{subs_declared}{$name} |
| 2325 | or |
| 2326 | exists &{"$self->{curstash}::$name"} |
| 2327 | ) { |
| 2328 | return "CORE::$name" |
| 2329 | } |
| 2330 | return $name; |
| 2331 | } |
| 2332 | |
| 2333 | sub baseop { |
| 2334 | my $self = shift; |
| 2335 | my($op, $cx, $name) = @_; |
| 2336 | return $self->keyword($name); |
| 2337 | } |
| 2338 | |
| 2339 | sub pp_stub { "()" } |
| 2340 | sub pp_wantarray { baseop(@_, "wantarray") } |
| 2341 | sub pp_fork { baseop(@_, "fork") } |
| 2342 | sub pp_wait { maybe_targmy(@_, \&baseop, "wait") } |
| 2343 | sub pp_getppid { maybe_targmy(@_, \&baseop, "getppid") } |
| 2344 | sub pp_time { maybe_targmy(@_, \&baseop, "time") } |
| 2345 | sub pp_tms { baseop(@_, "times") } |
| 2346 | sub pp_ghostent { baseop(@_, "gethostent") } |
| 2347 | sub pp_gnetent { baseop(@_, "getnetent") } |
| 2348 | sub pp_gprotoent { baseop(@_, "getprotoent") } |
| 2349 | sub pp_gservent { baseop(@_, "getservent") } |
| 2350 | sub pp_ehostent { baseop(@_, "endhostent") } |
| 2351 | sub pp_enetent { baseop(@_, "endnetent") } |
| 2352 | sub pp_eprotoent { baseop(@_, "endprotoent") } |
| 2353 | sub pp_eservent { baseop(@_, "endservent") } |
| 2354 | sub pp_gpwent { baseop(@_, "getpwent") } |
| 2355 | sub pp_spwent { baseop(@_, "setpwent") } |
| 2356 | sub pp_epwent { baseop(@_, "endpwent") } |
| 2357 | sub pp_ggrent { baseop(@_, "getgrent") } |
| 2358 | sub pp_sgrent { baseop(@_, "setgrent") } |
| 2359 | sub pp_egrent { baseop(@_, "endgrent") } |
| 2360 | sub pp_getlogin { baseop(@_, "getlogin") } |
| 2361 | |
| 2362 | sub POSTFIX () { 1 } |
| 2363 | |
| 2364 | # I couldn't think of a good short name, but this is the category of |
| 2365 | # symbolic unary operators with interesting precedence |
| 2366 | |
| 2367 | sub pfixop { |
| 2368 | my $self = shift; |
| 2369 | my($op, $cx, $name, $prec, $flags) = (@_, 0); |
| 2370 | my $kid = $op->first; |
| 2371 | $kid = $self->deparse($kid, $prec); |
| 2372 | return $self->maybe_parens(($flags & POSTFIX) |
| 2373 | ? "$kid$name" |
| 2374 | # avoid confusion with filetests |
| 2375 | : $name eq '-' |
| 2376 | && $kid =~ /^[a-zA-Z](?!\w)/ |
| 2377 | ? "$name($kid)" |
| 2378 | : "$name$kid", |
| 2379 | $cx, $prec); |
| 2380 | } |
| 2381 | |
| 2382 | sub pp_preinc { pfixop(@_, "++", 23) } |
| 2383 | sub pp_predec { pfixop(@_, "--", 23) } |
| 2384 | sub pp_postinc { maybe_targmy(@_, \&pfixop, "++", 23, POSTFIX) } |
| 2385 | sub pp_postdec { maybe_targmy(@_, \&pfixop, "--", 23, POSTFIX) } |
| 2386 | sub pp_i_preinc { pfixop(@_, "++", 23) } |
| 2387 | sub pp_i_predec { pfixop(@_, "--", 23) } |
| 2388 | sub pp_i_postinc { maybe_targmy(@_, \&pfixop, "++", 23, POSTFIX) } |
| 2389 | sub pp_i_postdec { maybe_targmy(@_, \&pfixop, "--", 23, POSTFIX) } |
| 2390 | sub pp_complement { maybe_targmy(@_, \&pfixop, "~", 21) } |
| 2391 | *pp_ncomplement = *pp_complement; |
| 2392 | sub pp_scomplement { maybe_targmy(@_, \&pfixop, "~.", 21) } |
| 2393 | |
| 2394 | sub pp_negate { maybe_targmy(@_, \&real_negate) } |
| 2395 | sub real_negate { |
| 2396 | my $self = shift; |
| 2397 | my($op, $cx) = @_; |
| 2398 | if ($op->first->name =~ /^(i_)?negate$/) { |
| 2399 | # avoid --$x |
| 2400 | $self->pfixop($op, $cx, "-", 21.5); |
| 2401 | } else { |
| 2402 | $self->pfixop($op, $cx, "-", 21); |
| 2403 | } |
| 2404 | } |
| 2405 | sub pp_i_negate { pp_negate(@_) } |
| 2406 | |
| 2407 | sub pp_not { |
| 2408 | my $self = shift; |
| 2409 | my($op, $cx) = @_; |
| 2410 | if ($cx <= 4) { |
| 2411 | $self->listop($op, $cx, "not", $op->first); |
| 2412 | } else { |
| 2413 | $self->pfixop($op, $cx, "!", 21); |
| 2414 | } |
| 2415 | } |
| 2416 | |
| 2417 | sub unop { |
| 2418 | my $self = shift; |
| 2419 | my($op, $cx, $name, $nollafr) = @_; |
| 2420 | my $kid; |
| 2421 | if ($op->flags & OPf_KIDS) { |
| 2422 | $kid = $op->first; |
| 2423 | if (not $name) { |
| 2424 | # this deals with 'boolkeys' right now |
| 2425 | return $self->deparse($kid,$cx); |
| 2426 | } |
| 2427 | my $builtinname = $name; |
| 2428 | $builtinname =~ /^CORE::/ or $builtinname = "CORE::$name"; |
| 2429 | if (defined prototype($builtinname) |
| 2430 | && $builtinname ne 'CORE::readline' |
| 2431 | && prototype($builtinname) =~ /^;?\*/ |
| 2432 | && $kid->name eq "rv2gv") { |
| 2433 | $kid = $kid->first; |
| 2434 | } |
| 2435 | |
| 2436 | if ($nollafr) { |
| 2437 | if (($kid = $self->deparse($kid, 16)) !~ s/^\cS//) { |
| 2438 | # require foo() is a syntax error. |
| 2439 | $kid =~ /^(?!\d)\w/ and $kid = "($kid)"; |
| 2440 | } |
| 2441 | return $self->maybe_parens( |
| 2442 | $self->keyword($name) . " $kid", $cx, 16 |
| 2443 | ); |
| 2444 | } |
| 2445 | return $self->maybe_parens_unop($name, $kid, $cx); |
| 2446 | } else { |
| 2447 | return $self->maybe_parens( |
| 2448 | $self->keyword($name) . ($op->flags & OPf_SPECIAL ? "()" : ""), |
| 2449 | $cx, 16, |
| 2450 | ); |
| 2451 | } |
| 2452 | } |
| 2453 | |
| 2454 | sub pp_chop { maybe_targmy(@_, \&unop, "chop") } |
| 2455 | sub pp_chomp { maybe_targmy(@_, \&unop, "chomp") } |
| 2456 | sub pp_schop { maybe_targmy(@_, \&unop, "chop") } |
| 2457 | sub pp_schomp { maybe_targmy(@_, \&unop, "chomp") } |
| 2458 | sub pp_defined { unop(@_, "defined") } |
| 2459 | sub pp_undef { unop(@_, "undef") } |
| 2460 | sub pp_study { unop(@_, "study") } |
| 2461 | sub pp_ref { unop(@_, "ref") } |
| 2462 | sub pp_pos { maybe_local(@_, unop(@_, "pos")) } |
| 2463 | |
| 2464 | sub pp_sin { maybe_targmy(@_, \&unop, "sin") } |
| 2465 | sub pp_cos { maybe_targmy(@_, \&unop, "cos") } |
| 2466 | sub pp_rand { maybe_targmy(@_, \&unop, "rand") } |
| 2467 | sub pp_srand { unop(@_, "srand") } |
| 2468 | sub pp_exp { maybe_targmy(@_, \&unop, "exp") } |
| 2469 | sub pp_log { maybe_targmy(@_, \&unop, "log") } |
| 2470 | sub pp_sqrt { maybe_targmy(@_, \&unop, "sqrt") } |
| 2471 | sub pp_int { maybe_targmy(@_, \&unop, "int") } |
| 2472 | sub pp_hex { maybe_targmy(@_, \&unop, "hex") } |
| 2473 | sub pp_oct { maybe_targmy(@_, \&unop, "oct") } |
| 2474 | sub pp_abs { maybe_targmy(@_, \&unop, "abs") } |
| 2475 | |
| 2476 | sub pp_length { maybe_targmy(@_, \&unop, "length") } |
| 2477 | sub pp_ord { maybe_targmy(@_, \&unop, "ord") } |
| 2478 | sub pp_chr { maybe_targmy(@_, \&unop, "chr") } |
| 2479 | |
| 2480 | sub pp_each { unop(@_, "each") } |
| 2481 | sub pp_values { unop(@_, "values") } |
| 2482 | sub pp_keys { unop(@_, "keys") } |
| 2483 | { no strict 'refs'; *{"pp_r$_"} = *{"pp_$_"} for qw< keys each values >; } |
| 2484 | sub pp_boolkeys { |
| 2485 | # no name because its an optimisation op that has no keyword |
| 2486 | unop(@_,""); |
| 2487 | } |
| 2488 | sub pp_aeach { unop(@_, "each") } |
| 2489 | sub pp_avalues { unop(@_, "values") } |
| 2490 | sub pp_akeys { unop(@_, "keys") } |
| 2491 | sub pp_pop { unop(@_, "pop") } |
| 2492 | sub pp_shift { unop(@_, "shift") } |
| 2493 | |
| 2494 | sub pp_caller { unop(@_, "caller") } |
| 2495 | sub pp_reset { unop(@_, "reset") } |
| 2496 | sub pp_exit { unop(@_, "exit") } |
| 2497 | sub pp_prototype { unop(@_, "prototype") } |
| 2498 | |
| 2499 | sub pp_close { unop(@_, "close") } |
| 2500 | sub pp_fileno { unop(@_, "fileno") } |
| 2501 | sub pp_umask { unop(@_, "umask") } |
| 2502 | sub pp_untie { unop(@_, "untie") } |
| 2503 | sub pp_tied { unop(@_, "tied") } |
| 2504 | sub pp_dbmclose { unop(@_, "dbmclose") } |
| 2505 | sub pp_getc { unop(@_, "getc") } |
| 2506 | sub pp_eof { unop(@_, "eof") } |
| 2507 | sub pp_tell { unop(@_, "tell") } |
| 2508 | sub pp_getsockname { unop(@_, "getsockname") } |
| 2509 | sub pp_getpeername { unop(@_, "getpeername") } |
| 2510 | |
| 2511 | sub pp_chdir { |
| 2512 | my ($self, $op, $cx) = @_; |
| 2513 | if (($op->flags & (OPf_SPECIAL|OPf_KIDS)) == (OPf_SPECIAL|OPf_KIDS)) { |
| 2514 | my $kw = $self->keyword("chdir"); |
| 2515 | my $kid = $self->const_sv($op->first)->PV; |
| 2516 | my $code = $kw |
| 2517 | . ($cx >= 16 || $self->{'parens'} ? "($kid)" : " $kid"); |
| 2518 | maybe_targmy(@_, sub { $_[3] }, $code); |
| 2519 | } else { |
| 2520 | maybe_targmy(@_, \&unop, "chdir") |
| 2521 | } |
| 2522 | } |
| 2523 | |
| 2524 | sub pp_chroot { maybe_targmy(@_, \&unop, "chroot") } |
| 2525 | sub pp_readlink { unop(@_, "readlink") } |
| 2526 | sub pp_rmdir { maybe_targmy(@_, \&unop, "rmdir") } |
| 2527 | sub pp_readdir { unop(@_, "readdir") } |
| 2528 | sub pp_telldir { unop(@_, "telldir") } |
| 2529 | sub pp_rewinddir { unop(@_, "rewinddir") } |
| 2530 | sub pp_closedir { unop(@_, "closedir") } |
| 2531 | sub pp_getpgrp { maybe_targmy(@_, \&unop, "getpgrp") } |
| 2532 | sub pp_localtime { unop(@_, "localtime") } |
| 2533 | sub pp_gmtime { unop(@_, "gmtime") } |
| 2534 | sub pp_alarm { unop(@_, "alarm") } |
| 2535 | sub pp_sleep { maybe_targmy(@_, \&unop, "sleep") } |
| 2536 | |
| 2537 | sub pp_dofile { |
| 2538 | my $code = unop(@_, "do", 1); # llafr does not apply |
| 2539 | if ($code =~ s/^((?:CORE::)?do) \{/$1({/) { $code .= ')' } |
| 2540 | $code; |
| 2541 | } |
| 2542 | sub pp_entereval { |
| 2543 | unop( |
| 2544 | @_, |
| 2545 | $_[1]->private & OPpEVAL_BYTES ? 'evalbytes' : "eval" |
| 2546 | ) |
| 2547 | } |
| 2548 | |
| 2549 | sub pp_ghbyname { unop(@_, "gethostbyname") } |
| 2550 | sub pp_gnbyname { unop(@_, "getnetbyname") } |
| 2551 | sub pp_gpbyname { unop(@_, "getprotobyname") } |
| 2552 | sub pp_shostent { unop(@_, "sethostent") } |
| 2553 | sub pp_snetent { unop(@_, "setnetent") } |
| 2554 | sub pp_sprotoent { unop(@_, "setprotoent") } |
| 2555 | sub pp_sservent { unop(@_, "setservent") } |
| 2556 | sub pp_gpwnam { unop(@_, "getpwnam") } |
| 2557 | sub pp_gpwuid { unop(@_, "getpwuid") } |
| 2558 | sub pp_ggrnam { unop(@_, "getgrnam") } |
| 2559 | sub pp_ggrgid { unop(@_, "getgrgid") } |
| 2560 | |
| 2561 | sub pp_lock { unop(@_, "lock") } |
| 2562 | |
| 2563 | sub pp_continue { unop(@_, "continue"); } |
| 2564 | sub pp_break { unop(@_, "break"); } |
| 2565 | |
| 2566 | sub givwhen { |
| 2567 | my $self = shift; |
| 2568 | my($op, $cx, $givwhen) = @_; |
| 2569 | |
| 2570 | my $enterop = $op->first; |
| 2571 | my ($head, $block); |
| 2572 | if ($enterop->flags & OPf_SPECIAL) { |
| 2573 | $head = $self->keyword("default"); |
| 2574 | $block = $self->deparse($enterop->first, 0); |
| 2575 | } |
| 2576 | else { |
| 2577 | my $cond = $enterop->first; |
| 2578 | my $cond_str = $self->deparse($cond, 1); |
| 2579 | $head = "$givwhen ($cond_str)"; |
| 2580 | $block = $self->deparse($cond->sibling, 0); |
| 2581 | } |
| 2582 | |
| 2583 | return "$head {\n". |
| 2584 | "\t$block\n". |
| 2585 | "\b}\cK"; |
| 2586 | } |
| 2587 | |
| 2588 | sub pp_leavegiven { givwhen(@_, $_[0]->keyword("given")); } |
| 2589 | sub pp_leavewhen { givwhen(@_, $_[0]->keyword("when")); } |
| 2590 | |
| 2591 | sub pp_exists { |
| 2592 | my $self = shift; |
| 2593 | my($op, $cx) = @_; |
| 2594 | my $arg; |
| 2595 | my $name = $self->keyword("exists"); |
| 2596 | if ($op->private & OPpEXISTS_SUB) { |
| 2597 | # Checking for the existence of a subroutine |
| 2598 | return $self->maybe_parens_func($name, |
| 2599 | $self->pp_rv2cv($op->first, 16), $cx, 16); |
| 2600 | } |
| 2601 | if ($op->flags & OPf_SPECIAL) { |
| 2602 | # Array element, not hash element |
| 2603 | return $self->maybe_parens_func($name, |
| 2604 | $self->pp_aelem($op->first, 16), $cx, 16); |
| 2605 | } |
| 2606 | return $self->maybe_parens_func($name, $self->pp_helem($op->first, 16), |
| 2607 | $cx, 16); |
| 2608 | } |
| 2609 | |
| 2610 | sub pp_delete { |
| 2611 | my $self = shift; |
| 2612 | my($op, $cx) = @_; |
| 2613 | my $arg; |
| 2614 | my $name = $self->keyword("delete"); |
| 2615 | if ($op->private & (OPpSLICE|OPpKVSLICE)) { |
| 2616 | if ($op->flags & OPf_SPECIAL) { |
| 2617 | # Deleting from an array, not a hash |
| 2618 | return $self->maybe_parens_func($name, |
| 2619 | $self->pp_aslice($op->first, 16), |
| 2620 | $cx, 16); |
| 2621 | } |
| 2622 | return $self->maybe_parens_func($name, |
| 2623 | $self->pp_hslice($op->first, 16), |
| 2624 | $cx, 16); |
| 2625 | } else { |
| 2626 | if ($op->flags & OPf_SPECIAL) { |
| 2627 | # Deleting from an array, not a hash |
| 2628 | return $self->maybe_parens_func($name, |
| 2629 | $self->pp_aelem($op->first, 16), |
| 2630 | $cx, 16); |
| 2631 | } |
| 2632 | return $self->maybe_parens_func($name, |
| 2633 | $self->pp_helem($op->first, 16), |
| 2634 | $cx, 16); |
| 2635 | } |
| 2636 | } |
| 2637 | |
| 2638 | sub pp_require { |
| 2639 | my $self = shift; |
| 2640 | my($op, $cx) = @_; |
| 2641 | my $opname = $op->flags & OPf_SPECIAL ? 'CORE::require' : 'require'; |
| 2642 | my $kid = $op->first; |
| 2643 | if ($kid->name eq 'const') { |
| 2644 | my $priv = $kid->private; |
| 2645 | my $sv = $self->const_sv($kid); |
| 2646 | my $arg; |
| 2647 | if ($priv & OPpCONST_BARE) { |
| 2648 | $arg = $sv->PV; |
| 2649 | $arg =~ s[/][::]g; |
| 2650 | $arg =~ s/\.pm//g; |
| 2651 | } elsif ($priv & OPpCONST_NOVER) { |
| 2652 | $opname = $self->keyword('no'); |
| 2653 | $arg = $self->const($sv, 16); |
| 2654 | } elsif ((my $tmp = $self->const($sv, 16)) =~ /^v/) { |
| 2655 | $arg = $tmp; |
| 2656 | } |
| 2657 | if ($arg) { |
| 2658 | return $self->maybe_parens("$opname $arg", $cx, 16); |
| 2659 | } |
| 2660 | } |
| 2661 | $self->unop( |
| 2662 | $op, $cx, |
| 2663 | $opname, |
| 2664 | 1, # llafr does not apply |
| 2665 | ); |
| 2666 | } |
| 2667 | |
| 2668 | sub pp_scalar { |
| 2669 | my $self = shift; |
| 2670 | my($op, $cx) = @_; |
| 2671 | my $kid = $op->first; |
| 2672 | if (not null $kid->sibling) { |
| 2673 | # XXX Was a here-doc |
| 2674 | return $self->dquote($op); |
| 2675 | } |
| 2676 | $self->unop(@_, "scalar"); |
| 2677 | } |
| 2678 | |
| 2679 | |
| 2680 | sub padval { |
| 2681 | my $self = shift; |
| 2682 | my $targ = shift; |
| 2683 | return $self->{'curcv'}->PADLIST->ARRAYelt(1)->ARRAYelt($targ); |
| 2684 | } |
| 2685 | |
| 2686 | sub anon_hash_or_list { |
| 2687 | my $self = shift; |
| 2688 | my($op, $cx) = @_; |
| 2689 | |
| 2690 | my($pre, $post) = @{{"anonlist" => ["[","]"], |
| 2691 | "anonhash" => ["{","}"]}->{$op->name}}; |
| 2692 | my($expr, @exprs); |
| 2693 | $op = $op->first->sibling; # skip pushmark |
| 2694 | for (; !null($op); $op = $op->sibling) { |
| 2695 | $expr = $self->deparse($op, 6); |
| 2696 | push @exprs, $expr; |
| 2697 | } |
| 2698 | if ($pre eq "{" and $cx < 1) { |
| 2699 | # Disambiguate that it's not a block |
| 2700 | $pre = "+{"; |
| 2701 | } |
| 2702 | return $pre . join(", ", @exprs) . $post; |
| 2703 | } |
| 2704 | |
| 2705 | sub pp_anonlist { |
| 2706 | my $self = shift; |
| 2707 | my ($op, $cx) = @_; |
| 2708 | if ($op->flags & OPf_SPECIAL) { |
| 2709 | return $self->anon_hash_or_list($op, $cx); |
| 2710 | } |
| 2711 | warn "Unexpected op pp_" . $op->name() . " without OPf_SPECIAL"; |
| 2712 | return 'XXX'; |
| 2713 | } |
| 2714 | |
| 2715 | *pp_anonhash = \&pp_anonlist; |
| 2716 | |
| 2717 | sub pp_refgen { |
| 2718 | my $self = shift; |
| 2719 | my($op, $cx) = @_; |
| 2720 | my $kid = $op->first; |
| 2721 | if ($kid->name eq "null") { |
| 2722 | my $anoncode = $kid = $kid->first; |
| 2723 | if ($anoncode->name eq "anonconst") { |
| 2724 | $anoncode = $anoncode->first->first->sibling; |
| 2725 | } |
| 2726 | if ($anoncode->name eq "anoncode" |
| 2727 | or !null($anoncode = $kid->sibling) and |
| 2728 | $anoncode->name eq "anoncode") { |
| 2729 | return $self->e_anoncode({ code => $self->padval($anoncode->targ) }); |
| 2730 | } elsif ($kid->name eq "pushmark") { |
| 2731 | my $sib_name = $kid->sibling->name; |
| 2732 | if ($sib_name eq 'entersub') { |
| 2733 | my $text = $self->deparse($kid->sibling, 1); |
| 2734 | # Always show parens for \(&func()), but only with -p otherwise |
| 2735 | $text = "($text)" if $self->{'parens'} |
| 2736 | or $kid->sibling->private & OPpENTERSUB_AMPER; |
| 2737 | return "\\$text"; |
| 2738 | } |
| 2739 | } |
| 2740 | } |
| 2741 | local $self->{'in_refgen'} = 1; |
| 2742 | $self->pfixop($op, $cx, "\\", 20); |
| 2743 | } |
| 2744 | |
| 2745 | sub e_anoncode { |
| 2746 | my ($self, $info) = @_; |
| 2747 | my $text = $self->deparse_sub($info->{code}); |
| 2748 | return $self->keyword("sub") . " $text"; |
| 2749 | } |
| 2750 | |
| 2751 | sub pp_srefgen { pp_refgen(@_) } |
| 2752 | |
| 2753 | sub pp_readline { |
| 2754 | my $self = shift; |
| 2755 | my($op, $cx) = @_; |
| 2756 | my $kid = $op->first; |
| 2757 | if (is_scalar($kid) |
| 2758 | and $op->flags & OPf_SPECIAL |
| 2759 | and $self->deparse($kid, 1) eq 'ARGV') |
| 2760 | { |
| 2761 | return '<<>>'; |
| 2762 | } |
| 2763 | return $self->unop($op, $cx, "readline"); |
| 2764 | } |
| 2765 | |
| 2766 | sub pp_rcatline { |
| 2767 | my $self = shift; |
| 2768 | my($op) = @_; |
| 2769 | return "<" . $self->gv_name($self->gv_or_padgv($op)) . ">"; |
| 2770 | } |
| 2771 | |
| 2772 | # Unary operators that can occur as pseudo-listops inside double quotes |
| 2773 | sub dq_unop { |
| 2774 | my $self = shift; |
| 2775 | my($op, $cx, $name, $prec, $flags) = (@_, 0, 0); |
| 2776 | my $kid; |
| 2777 | if ($op->flags & OPf_KIDS) { |
| 2778 | $kid = $op->first; |
| 2779 | # If there's more than one kid, the first is an ex-pushmark. |
| 2780 | $kid = $kid->sibling if not null $kid->sibling; |
| 2781 | return $self->maybe_parens_unop($name, $kid, $cx); |
| 2782 | } else { |
| 2783 | return $name . ($op->flags & OPf_SPECIAL ? "()" : ""); |
| 2784 | } |
| 2785 | } |
| 2786 | |
| 2787 | sub pp_ucfirst { dq_unop(@_, "ucfirst") } |
| 2788 | sub pp_lcfirst { dq_unop(@_, "lcfirst") } |
| 2789 | sub pp_uc { dq_unop(@_, "uc") } |
| 2790 | sub pp_lc { dq_unop(@_, "lc") } |
| 2791 | sub pp_quotemeta { maybe_targmy(@_, \&dq_unop, "quotemeta") } |
| 2792 | sub pp_fc { dq_unop(@_, "fc") } |
| 2793 | |
| 2794 | sub loopex { |
| 2795 | my $self = shift; |
| 2796 | my ($op, $cx, $name) = @_; |
| 2797 | if (class($op) eq "PVOP") { |
| 2798 | $name .= " " . $op->pv; |
| 2799 | } elsif (class($op) eq "OP") { |
| 2800 | # no-op |
| 2801 | } elsif (class($op) eq "UNOP") { |
| 2802 | (my $kid = $self->deparse($op->first, 7)) =~ s/^\cS//; |
| 2803 | # last foo() is a syntax error. |
| 2804 | $kid =~ /^(?!\d)\w/ and $kid = "($kid)"; |
| 2805 | $name .= " $kid"; |
| 2806 | } |
| 2807 | return $self->maybe_parens($name, $cx, 7); |
| 2808 | } |
| 2809 | |
| 2810 | sub pp_last { loopex(@_, "last") } |
| 2811 | sub pp_next { loopex(@_, "next") } |
| 2812 | sub pp_redo { loopex(@_, "redo") } |
| 2813 | sub pp_goto { loopex(@_, "goto") } |
| 2814 | sub pp_dump { loopex(@_, "CORE::dump") } |
| 2815 | |
| 2816 | sub ftst { |
| 2817 | my $self = shift; |
| 2818 | my($op, $cx, $name) = @_; |
| 2819 | if (class($op) eq "UNOP") { |
| 2820 | # Genuine '-X' filetests are exempt from the LLAFR, but not |
| 2821 | # l?stat() |
| 2822 | if ($name =~ /^-/) { |
| 2823 | (my $kid = $self->deparse($op->first, 16)) =~ s/^\cS//; |
| 2824 | return $self->maybe_parens("$name $kid", $cx, 16); |
| 2825 | } |
| 2826 | return $self->maybe_parens_unop($name, $op->first, $cx); |
| 2827 | } elsif (class($op) =~ /^(SV|PAD)OP$/) { |
| 2828 | return $self->maybe_parens_func($name, $self->pp_gv($op, 1), $cx, 16); |
| 2829 | } else { # I don't think baseop filetests ever survive ck_ftst, but... |
| 2830 | return $name; |
| 2831 | } |
| 2832 | } |
| 2833 | |
| 2834 | sub pp_lstat { ftst(@_, "lstat") } |
| 2835 | sub pp_stat { ftst(@_, "stat") } |
| 2836 | sub pp_ftrread { ftst(@_, "-R") } |
| 2837 | sub pp_ftrwrite { ftst(@_, "-W") } |
| 2838 | sub pp_ftrexec { ftst(@_, "-X") } |
| 2839 | sub pp_fteread { ftst(@_, "-r") } |
| 2840 | sub pp_ftewrite { ftst(@_, "-w") } |
| 2841 | sub pp_fteexec { ftst(@_, "-x") } |
| 2842 | sub pp_ftis { ftst(@_, "-e") } |
| 2843 | sub pp_fteowned { ftst(@_, "-O") } |
| 2844 | sub pp_ftrowned { ftst(@_, "-o") } |
| 2845 | sub pp_ftzero { ftst(@_, "-z") } |
| 2846 | sub pp_ftsize { ftst(@_, "-s") } |
| 2847 | sub pp_ftmtime { ftst(@_, "-M") } |
| 2848 | sub pp_ftatime { ftst(@_, "-A") } |
| 2849 | sub pp_ftctime { ftst(@_, "-C") } |
| 2850 | sub pp_ftsock { ftst(@_, "-S") } |
| 2851 | sub pp_ftchr { ftst(@_, "-c") } |
| 2852 | sub pp_ftblk { ftst(@_, "-b") } |
| 2853 | sub pp_ftfile { ftst(@_, "-f") } |
| 2854 | sub pp_ftdir { ftst(@_, "-d") } |
| 2855 | sub pp_ftpipe { ftst(@_, "-p") } |
| 2856 | sub pp_ftlink { ftst(@_, "-l") } |
| 2857 | sub pp_ftsuid { ftst(@_, "-u") } |
| 2858 | sub pp_ftsgid { ftst(@_, "-g") } |
| 2859 | sub pp_ftsvtx { ftst(@_, "-k") } |
| 2860 | sub pp_fttty { ftst(@_, "-t") } |
| 2861 | sub pp_fttext { ftst(@_, "-T") } |
| 2862 | sub pp_ftbinary { ftst(@_, "-B") } |
| 2863 | |
| 2864 | sub SWAP_CHILDREN () { 1 } |
| 2865 | sub ASSIGN () { 2 } # has OP= variant |
| 2866 | sub LIST_CONTEXT () { 4 } # Assignment is in list context |
| 2867 | |
| 2868 | my(%left, %right); |
| 2869 | |
| 2870 | sub assoc_class { |
| 2871 | my $op = shift; |
| 2872 | my $name = $op->name; |
| 2873 | if ($name eq "concat" and $op->first->name eq "concat") { |
| 2874 | # avoid spurious '=' -- see comment in pp_concat |
| 2875 | return "concat"; |
| 2876 | } |
| 2877 | if ($name eq "null" and class($op) eq "UNOP" |
| 2878 | and $op->first->name =~ /^(and|x?or)$/ |
| 2879 | and null $op->first->sibling) |
| 2880 | { |
| 2881 | # Like all conditional constructs, OP_ANDs and OP_ORs are topped |
| 2882 | # with a null that's used as the common end point of the two |
| 2883 | # flows of control. For precedence purposes, ignore it. |
| 2884 | # (COND_EXPRs have these too, but we don't bother with |
| 2885 | # their associativity). |
| 2886 | return assoc_class($op->first); |
| 2887 | } |
| 2888 | return $name . ($op->flags & OPf_STACKED ? "=" : ""); |
| 2889 | } |
| 2890 | |
| 2891 | # Left associative operators, like '+', for which |
| 2892 | # $a + $b + $c is equivalent to ($a + $b) + $c |
| 2893 | |
| 2894 | BEGIN { |
| 2895 | %left = ('multiply' => 19, 'i_multiply' => 19, |
| 2896 | 'divide' => 19, 'i_divide' => 19, |
| 2897 | 'modulo' => 19, 'i_modulo' => 19, |
| 2898 | 'repeat' => 19, |
| 2899 | 'add' => 18, 'i_add' => 18, |
| 2900 | 'subtract' => 18, 'i_subtract' => 18, |
| 2901 | 'concat' => 18, |
| 2902 | 'left_shift' => 17, 'right_shift' => 17, |
| 2903 | 'bit_and' => 13, 'nbit_and' => 13, 'sbit_and' => 13, |
| 2904 | 'bit_or' => 12, 'bit_xor' => 12, |
| 2905 | 'sbit_or' => 12, 'sbit_xor' => 12, |
| 2906 | 'nbit_or' => 12, 'nbit_xor' => 12, |
| 2907 | 'and' => 3, |
| 2908 | 'or' => 2, 'xor' => 2, |
| 2909 | ); |
| 2910 | } |
| 2911 | |
| 2912 | sub deparse_binop_left { |
| 2913 | my $self = shift; |
| 2914 | my($op, $left, $prec) = @_; |
| 2915 | if ($left{assoc_class($op)} && $left{assoc_class($left)} |
| 2916 | and $left{assoc_class($op)} == $left{assoc_class($left)}) |
| 2917 | { |
| 2918 | return $self->deparse($left, $prec - .00001); |
| 2919 | } else { |
| 2920 | return $self->deparse($left, $prec); |
| 2921 | } |
| 2922 | } |
| 2923 | |
| 2924 | # Right associative operators, like '=', for which |
| 2925 | # $a = $b = $c is equivalent to $a = ($b = $c) |
| 2926 | |
| 2927 | BEGIN { |
| 2928 | %right = ('pow' => 22, |
| 2929 | 'sassign=' => 7, 'aassign=' => 7, |
| 2930 | 'multiply=' => 7, 'i_multiply=' => 7, |
| 2931 | 'divide=' => 7, 'i_divide=' => 7, |
| 2932 | 'modulo=' => 7, 'i_modulo=' => 7, |
| 2933 | 'repeat=' => 7, 'refassign' => 7, 'refassign=' => 7, |
| 2934 | 'add=' => 7, 'i_add=' => 7, |
| 2935 | 'subtract=' => 7, 'i_subtract=' => 7, |
| 2936 | 'concat=' => 7, |
| 2937 | 'left_shift=' => 7, 'right_shift=' => 7, |
| 2938 | 'bit_and=' => 7, 'sbit_and=' => 7, 'nbit_and=' => 7, |
| 2939 | 'nbit_or=' => 7, 'nbit_xor=' => 7, |
| 2940 | 'sbit_or=' => 7, 'sbit_xor=' => 7, |
| 2941 | 'andassign' => 7, |
| 2942 | 'orassign' => 7, |
| 2943 | ); |
| 2944 | } |
| 2945 | |
| 2946 | sub deparse_binop_right { |
| 2947 | my $self = shift; |
| 2948 | my($op, $right, $prec) = @_; |
| 2949 | if ($right{assoc_class($op)} && $right{assoc_class($right)} |
| 2950 | and $right{assoc_class($op)} == $right{assoc_class($right)}) |
| 2951 | { |
| 2952 | return $self->deparse($right, $prec - .00001); |
| 2953 | } else { |
| 2954 | return $self->deparse($right, $prec); |
| 2955 | } |
| 2956 | } |
| 2957 | |
| 2958 | sub binop { |
| 2959 | my $self = shift; |
| 2960 | my ($op, $cx, $opname, $prec, $flags) = (@_, 0); |
| 2961 | my $left = $op->first; |
| 2962 | my $right = $op->last; |
| 2963 | my $eq = ""; |
| 2964 | if ($op->flags & OPf_STACKED && $flags & ASSIGN) { |
| 2965 | $eq = "="; |
| 2966 | $prec = 7; |
| 2967 | } |
| 2968 | if ($flags & SWAP_CHILDREN) { |
| 2969 | ($left, $right) = ($right, $left); |
| 2970 | } |
| 2971 | my $leftop = $left; |
| 2972 | $left = $self->deparse_binop_left($op, $left, $prec); |
| 2973 | $left = "($left)" if $flags & LIST_CONTEXT |
| 2974 | and $left !~ /^(my|our|local|state|)\s*[\@%\(]/ |
| 2975 | || do { |
| 2976 | # Parenthesize if the left argument is a |
| 2977 | # lone repeat op. |
| 2978 | my $left = $leftop->first->sibling; |
| 2979 | $left->name eq 'repeat' |
| 2980 | && null($left->sibling); |
| 2981 | }; |
| 2982 | $right = $self->deparse_binop_right($op, $right, $prec); |
| 2983 | return $self->maybe_parens("$left $opname$eq $right", $cx, $prec); |
| 2984 | } |
| 2985 | |
| 2986 | sub pp_add { maybe_targmy(@_, \&binop, "+", 18, ASSIGN) } |
| 2987 | sub pp_multiply { maybe_targmy(@_, \&binop, "*", 19, ASSIGN) } |
| 2988 | sub pp_subtract { maybe_targmy(@_, \&binop, "-",18, ASSIGN) } |
| 2989 | sub pp_divide { maybe_targmy(@_, \&binop, "/", 19, ASSIGN) } |
| 2990 | sub pp_modulo { maybe_targmy(@_, \&binop, "%", 19, ASSIGN) } |
| 2991 | sub pp_i_add { maybe_targmy(@_, \&binop, "+", 18, ASSIGN) } |
| 2992 | sub pp_i_multiply { maybe_targmy(@_, \&binop, "*", 19, ASSIGN) } |
| 2993 | sub pp_i_subtract { maybe_targmy(@_, \&binop, "-", 18, ASSIGN) } |
| 2994 | sub pp_i_divide { maybe_targmy(@_, \&binop, "/", 19, ASSIGN) } |
| 2995 | sub pp_i_modulo { maybe_targmy(@_, \&binop, "%", 19, ASSIGN) } |
| 2996 | sub pp_pow { maybe_targmy(@_, \&binop, "**", 22, ASSIGN) } |
| 2997 | |
| 2998 | sub pp_left_shift { maybe_targmy(@_, \&binop, "<<", 17, ASSIGN) } |
| 2999 | sub pp_right_shift { maybe_targmy(@_, \&binop, ">>", 17, ASSIGN) } |
| 3000 | sub pp_bit_and { maybe_targmy(@_, \&binop, "&", 13, ASSIGN) } |
| 3001 | sub pp_bit_or { maybe_targmy(@_, \&binop, "|", 12, ASSIGN) } |
| 3002 | sub pp_bit_xor { maybe_targmy(@_, \&binop, "^", 12, ASSIGN) } |
| 3003 | *pp_nbit_and = *pp_bit_and; |
| 3004 | *pp_nbit_or = *pp_bit_or; |
| 3005 | *pp_nbit_xor = *pp_bit_xor; |
| 3006 | sub pp_sbit_and { maybe_targmy(@_, \&binop, "&.", 13, ASSIGN) } |
| 3007 | sub pp_sbit_or { maybe_targmy(@_, \&binop, "|.", 12, ASSIGN) } |
| 3008 | sub pp_sbit_xor { maybe_targmy(@_, \&binop, "^.", 12, ASSIGN) } |
| 3009 | |
| 3010 | sub pp_eq { binop(@_, "==", 14) } |
| 3011 | sub pp_ne { binop(@_, "!=", 14) } |
| 3012 | sub pp_lt { binop(@_, "<", 15) } |
| 3013 | sub pp_gt { binop(@_, ">", 15) } |
| 3014 | sub pp_ge { binop(@_, ">=", 15) } |
| 3015 | sub pp_le { binop(@_, "<=", 15) } |
| 3016 | sub pp_ncmp { binop(@_, "<=>", 14) } |
| 3017 | sub pp_i_eq { binop(@_, "==", 14) } |
| 3018 | sub pp_i_ne { binop(@_, "!=", 14) } |
| 3019 | sub pp_i_lt { binop(@_, "<", 15) } |
| 3020 | sub pp_i_gt { binop(@_, ">", 15) } |
| 3021 | sub pp_i_ge { binop(@_, ">=", 15) } |
| 3022 | sub pp_i_le { binop(@_, "<=", 15) } |
| 3023 | sub pp_i_ncmp { maybe_targmy(@_, \&binop, "<=>", 14) } |
| 3024 | |
| 3025 | sub pp_seq { binop(@_, "eq", 14) } |
| 3026 | sub pp_sne { binop(@_, "ne", 14) } |
| 3027 | sub pp_slt { binop(@_, "lt", 15) } |
| 3028 | sub pp_sgt { binop(@_, "gt", 15) } |
| 3029 | sub pp_sge { binop(@_, "ge", 15) } |
| 3030 | sub pp_sle { binop(@_, "le", 15) } |
| 3031 | sub pp_scmp { maybe_targmy(@_, \&binop, "cmp", 14) } |
| 3032 | |
| 3033 | sub pp_sassign { binop(@_, "=", 7, SWAP_CHILDREN) } |
| 3034 | sub pp_aassign { binop(@_, "=", 7, SWAP_CHILDREN | LIST_CONTEXT) } |
| 3035 | |
| 3036 | sub pp_smartmatch { |
| 3037 | my ($self, $op, $cx) = @_; |
| 3038 | if (($op->flags & OPf_SPECIAL) && $self->{expand} < 2) { |
| 3039 | return $self->deparse($op->last, $cx); |
| 3040 | } |
| 3041 | else { |
| 3042 | binop(@_, "~~", 14); |
| 3043 | } |
| 3044 | } |
| 3045 | |
| 3046 | # '.' is special because concats-of-concats are optimized to save copying |
| 3047 | # by making all but the first concat stacked. The effect is as if the |
| 3048 | # programmer had written '($a . $b) .= $c', except legal. |
| 3049 | sub pp_concat { maybe_targmy(@_, \&real_concat) } |
| 3050 | sub real_concat { |
| 3051 | my $self = shift; |
| 3052 | my($op, $cx) = @_; |
| 3053 | my $left = $op->first; |
| 3054 | my $right = $op->last; |
| 3055 | my $eq = ""; |
| 3056 | my $prec = 18; |
| 3057 | if (($op->flags & OPf_STACKED) and !($op->private & OPpCONCAT_NESTED)) { |
| 3058 | # '.=' rather than optimised '.' |
| 3059 | $eq = "="; |
| 3060 | $prec = 7; |
| 3061 | } |
| 3062 | $left = $self->deparse_binop_left($op, $left, $prec); |
| 3063 | $right = $self->deparse_binop_right($op, $right, $prec); |
| 3064 | return $self->maybe_parens("$left .$eq $right", $cx, $prec); |
| 3065 | } |
| 3066 | |
| 3067 | sub pp_repeat { maybe_targmy(@_, \&repeat) } |
| 3068 | |
| 3069 | # 'x' is weird when the left arg is a list |
| 3070 | sub repeat { |
| 3071 | my $self = shift; |
| 3072 | my($op, $cx) = @_; |
| 3073 | my $left = $op->first; |
| 3074 | my $right = $op->last; |
| 3075 | my $eq = ""; |
| 3076 | my $prec = 19; |
| 3077 | if ($op->flags & OPf_STACKED) { |
| 3078 | $eq = "="; |
| 3079 | $prec = 7; |
| 3080 | } |
| 3081 | if (null($right)) { # list repeat; count is inside left-side ex-list |
| 3082 | # in 5.21.5 and earlier |
| 3083 | my $kid = $left->first->sibling; # skip pushmark |
| 3084 | my @exprs; |
| 3085 | for (; !null($kid->sibling); $kid = $kid->sibling) { |
| 3086 | push @exprs, $self->deparse($kid, 6); |
| 3087 | } |
| 3088 | $right = $kid; |
| 3089 | $left = "(" . join(", ", @exprs). ")"; |
| 3090 | } else { |
| 3091 | my $dolist = $op->private & OPpREPEAT_DOLIST; |
| 3092 | $left = $self->deparse_binop_left($op, $left, $dolist ? 1 : $prec); |
| 3093 | if ($dolist) { |
| 3094 | $left = "($left)"; |
| 3095 | } |
| 3096 | } |
| 3097 | $right = $self->deparse_binop_right($op, $right, $prec); |
| 3098 | return $self->maybe_parens("$left x$eq $right", $cx, $prec); |
| 3099 | } |
| 3100 | |
| 3101 | sub range { |
| 3102 | my $self = shift; |
| 3103 | my ($op, $cx, $type) = @_; |
| 3104 | my $left = $op->first; |
| 3105 | my $right = $left->sibling; |
| 3106 | $left = $self->deparse($left, 9); |
| 3107 | $right = $self->deparse($right, 9); |
| 3108 | return $self->maybe_parens("$left $type $right", $cx, 9); |
| 3109 | } |
| 3110 | |
| 3111 | sub pp_flop { |
| 3112 | my $self = shift; |
| 3113 | my($op, $cx) = @_; |
| 3114 | my $flip = $op->first; |
| 3115 | my $type = ($flip->flags & OPf_SPECIAL) ? "..." : ".."; |
| 3116 | return $self->range($flip->first, $cx, $type); |
| 3117 | } |
| 3118 | |
| 3119 | # one-line while/until is handled in pp_leave |
| 3120 | |
| 3121 | sub logop { |
| 3122 | my $self = shift; |
| 3123 | my ($op, $cx, $lowop, $lowprec, $highop, $highprec, $blockname) = @_; |
| 3124 | my $left = $op->first; |
| 3125 | my $right = $op->first->sibling; |
| 3126 | $blockname &&= $self->keyword($blockname); |
| 3127 | if ($cx < 1 and is_scope($right) and $blockname |
| 3128 | and $self->{'expand'} < 7) |
| 3129 | { # if ($a) {$b} |
| 3130 | $left = $self->deparse($left, 1); |
| 3131 | $right = $self->deparse($right, 0); |
| 3132 | return "$blockname ($left) {\n\t$right\n\b}\cK"; |
| 3133 | } elsif ($cx < 1 and $blockname and not $self->{'parens'} |
| 3134 | and $self->{'expand'} < 7) { # $b if $a |
| 3135 | $right = $self->deparse($right, 1); |
| 3136 | $left = $self->deparse($left, 1); |
| 3137 | return "$right $blockname $left"; |
| 3138 | } elsif ($cx > $lowprec and $highop) { # $a && $b |
| 3139 | $left = $self->deparse_binop_left($op, $left, $highprec); |
| 3140 | $right = $self->deparse_binop_right($op, $right, $highprec); |
| 3141 | return $self->maybe_parens("$left $highop $right", $cx, $highprec); |
| 3142 | } else { # $a and $b |
| 3143 | $left = $self->deparse_binop_left($op, $left, $lowprec); |
| 3144 | $right = $self->deparse_binop_right($op, $right, $lowprec); |
| 3145 | return $self->maybe_parens("$left $lowop $right", $cx, $lowprec); |
| 3146 | } |
| 3147 | } |
| 3148 | |
| 3149 | sub pp_and { logop(@_, "and", 3, "&&", 11, "if") } |
| 3150 | sub pp_or { logop(@_, "or", 2, "||", 10, "unless") } |
| 3151 | sub pp_dor { logop(@_, "//", 10) } |
| 3152 | |
| 3153 | # xor is syntactically a logop, but it's really a binop (contrary to |
| 3154 | # old versions of opcode.pl). Syntax is what matters here. |
| 3155 | sub pp_xor { logop(@_, "xor", 2, "", 0, "") } |
| 3156 | |
| 3157 | sub logassignop { |
| 3158 | my $self = shift; |
| 3159 | my ($op, $cx, $opname) = @_; |
| 3160 | my $left = $op->first; |
| 3161 | my $right = $op->first->sibling->first; # skip sassign |
| 3162 | $left = $self->deparse($left, 7); |
| 3163 | $right = $self->deparse($right, 7); |
| 3164 | return $self->maybe_parens("$left $opname $right", $cx, 7); |
| 3165 | } |
| 3166 | |
| 3167 | sub pp_andassign { logassignop(@_, "&&=") } |
| 3168 | sub pp_orassign { logassignop(@_, "||=") } |
| 3169 | sub pp_dorassign { logassignop(@_, "//=") } |
| 3170 | |
| 3171 | sub rv2gv_or_string { |
| 3172 | my($self,$op) = @_; |
| 3173 | if ($op->name eq "gv") { # could be open("open") or open("###") |
| 3174 | my($name,$quoted) = |
| 3175 | $self->stash_variable_name("", $self->gv_or_padgv($op)); |
| 3176 | $quoted ? $name : "*$name"; |
| 3177 | } |
| 3178 | else { |
| 3179 | $self->deparse($op, 6); |
| 3180 | } |
| 3181 | } |
| 3182 | |
| 3183 | sub listop { |
| 3184 | my $self = shift; |
| 3185 | my($op, $cx, $name, $kid, $nollafr) = @_; |
| 3186 | my(@exprs); |
| 3187 | my $parens = ($cx >= 5) || $self->{'parens'}; |
| 3188 | $kid ||= $op->first->sibling; |
| 3189 | # If there are no arguments, add final parentheses (or parenthesize the |
| 3190 | # whole thing if the llafr does not apply) to account for cases like |
| 3191 | # (return)+1 or setpgrp()+1. When the llafr does not apply, we use a |
| 3192 | # precedence of 6 (< comma), as "return, 1" does not need parentheses. |
| 3193 | if (null $kid) { |
| 3194 | return $nollafr |
| 3195 | ? $self->maybe_parens($self->keyword($name), $cx, 7) |
| 3196 | : $self->keyword($name) . '()' x (7 < $cx); |
| 3197 | } |
| 3198 | my $first; |
| 3199 | my $fullname = $self->keyword($name); |
| 3200 | my $proto = prototype("CORE::$name"); |
| 3201 | if ( |
| 3202 | ( (defined $proto && $proto =~ /^;?\*/) |
| 3203 | || $name eq 'select' # select(F) doesn't have a proto |
| 3204 | ) |
| 3205 | && $kid->name eq "rv2gv" |
| 3206 | && !($kid->private & OPpLVAL_INTRO) |
| 3207 | ) { |
| 3208 | $first = $self->rv2gv_or_string($kid->first); |
| 3209 | } |
| 3210 | else { |
| 3211 | $first = $self->deparse($kid, 6); |
| 3212 | } |
| 3213 | if ($name eq "chmod" && $first =~ /^\d+$/) { |
| 3214 | $first = sprintf("%#o", $first); |
| 3215 | } |
| 3216 | $first = "+$first" |
| 3217 | if not $parens and not $nollafr and substr($first, 0, 1) eq "("; |
| 3218 | push @exprs, $first; |
| 3219 | $kid = $kid->sibling; |
| 3220 | if (defined $proto && $proto =~ /^\*\*/ && $kid->name eq "rv2gv" |
| 3221 | && !($kid->private & OPpLVAL_INTRO)) { |
| 3222 | push @exprs, $first = $self->rv2gv_or_string($kid->first); |
| 3223 | $kid = $kid->sibling; |
| 3224 | } |
| 3225 | for (; !null($kid); $kid = $kid->sibling) { |
| 3226 | push @exprs, $self->deparse($kid, 6); |
| 3227 | } |
| 3228 | if ($name eq "reverse" && ($op->private & OPpREVERSE_INPLACE)) { |
| 3229 | return "$exprs[0] = $fullname" |
| 3230 | . ($parens ? "($exprs[0])" : " $exprs[0]"); |
| 3231 | } |
| 3232 | |
| 3233 | if ($parens && $nollafr) { |
| 3234 | return "($fullname " . join(", ", @exprs) . ")"; |
| 3235 | } elsif ($parens) { |
| 3236 | return "$fullname(" . join(", ", @exprs) . ")"; |
| 3237 | } else { |
| 3238 | return "$fullname " . join(", ", @exprs); |
| 3239 | } |
| 3240 | } |
| 3241 | |
| 3242 | sub pp_bless { listop(@_, "bless") } |
| 3243 | sub pp_atan2 { maybe_targmy(@_, \&listop, "atan2") } |
| 3244 | sub pp_substr { |
| 3245 | my ($self,$op,$cx) = @_; |
| 3246 | if ($op->private & OPpSUBSTR_REPL_FIRST) { |
| 3247 | return |
| 3248 | listop($self, $op, 7, "substr", $op->first->sibling->sibling) |
| 3249 | . " = " |
| 3250 | . $self->deparse($op->first->sibling, 7); |
| 3251 | } |
| 3252 | maybe_local(@_, listop(@_, "substr")) |
| 3253 | } |
| 3254 | |
| 3255 | sub pp_index { |
| 3256 | # Also handles pp_rindex. |
| 3257 | # |
| 3258 | # The body of this function includes an unrolled maybe_targmy(), |
| 3259 | # since the two parts of that sub's actions need to have have the |
| 3260 | # '== -1' bit in between |
| 3261 | |
| 3262 | my($self, $op, $cx) = @_; |
| 3263 | |
| 3264 | my $lex = ($op->private & OPpTARGET_MY); |
| 3265 | my $bool = ($op->private & OPpTRUEBOOL); |
| 3266 | |
| 3267 | my $val = $self->listop($op, ($bool ? 14 : $lex ? 7 : $cx), $op->name); |
| 3268 | |
| 3269 | # (index() == -1) has op_eq and op_const optimised away |
| 3270 | if ($bool) { |
| 3271 | $val .= ($op->private & OPpINDEX_BOOLNEG) ? " == -1" : " != -1"; |
| 3272 | $val = "($val)" if ($op->flags & OPf_PARENS); |
| 3273 | } |
| 3274 | if ($lex) { |
| 3275 | my $var = $self->padname($op->targ); |
| 3276 | $val = $self->maybe_parens("$var = $val", $cx, 7); |
| 3277 | } |
| 3278 | $val; |
| 3279 | } |
| 3280 | |
| 3281 | sub pp_rindex { pp_index(@_); } |
| 3282 | sub pp_vec { maybe_targmy(@_, \&maybe_local, listop(@_, "vec")) } |
| 3283 | sub pp_sprintf { maybe_targmy(@_, \&listop, "sprintf") } |
| 3284 | sub pp_formline { listop(@_, "formline") } # see also deparse_format |
| 3285 | sub pp_crypt { maybe_targmy(@_, \&listop, "crypt") } |
| 3286 | sub pp_unpack { listop(@_, "unpack") } |
| 3287 | sub pp_pack { listop(@_, "pack") } |
| 3288 | sub pp_join { maybe_targmy(@_, \&listop, "join") } |
| 3289 | sub pp_splice { listop(@_, "splice") } |
| 3290 | sub pp_push { maybe_targmy(@_, \&listop, "push") } |
| 3291 | sub pp_unshift { maybe_targmy(@_, \&listop, "unshift") } |
| 3292 | sub pp_reverse { listop(@_, "reverse") } |
| 3293 | sub pp_warn { listop(@_, "warn") } |
| 3294 | sub pp_die { listop(@_, "die") } |
| 3295 | sub pp_return { listop(@_, "return", undef, 1) } # llafr does not apply |
| 3296 | sub pp_open { listop(@_, "open") } |
| 3297 | sub pp_pipe_op { listop(@_, "pipe") } |
| 3298 | sub pp_tie { listop(@_, "tie") } |
| 3299 | sub pp_binmode { listop(@_, "binmode") } |
| 3300 | sub pp_dbmopen { listop(@_, "dbmopen") } |
| 3301 | sub pp_sselect { listop(@_, "select") } |
| 3302 | sub pp_select { listop(@_, "select") } |
| 3303 | sub pp_read { listop(@_, "read") } |
| 3304 | sub pp_sysopen { listop(@_, "sysopen") } |
| 3305 | sub pp_sysseek { listop(@_, "sysseek") } |
| 3306 | sub pp_sysread { listop(@_, "sysread") } |
| 3307 | sub pp_syswrite { listop(@_, "syswrite") } |
| 3308 | sub pp_send { listop(@_, "send") } |
| 3309 | sub pp_recv { listop(@_, "recv") } |
| 3310 | sub pp_seek { listop(@_, "seek") } |
| 3311 | sub pp_fcntl { listop(@_, "fcntl") } |
| 3312 | sub pp_ioctl { listop(@_, "ioctl") } |
| 3313 | sub pp_flock { maybe_targmy(@_, \&listop, "flock") } |
| 3314 | sub pp_socket { listop(@_, "socket") } |
| 3315 | sub pp_sockpair { listop(@_, "socketpair") } |
| 3316 | sub pp_bind { listop(@_, "bind") } |
| 3317 | sub pp_connect { listop(@_, "connect") } |
| 3318 | sub pp_listen { listop(@_, "listen") } |
| 3319 | sub pp_accept { listop(@_, "accept") } |
| 3320 | sub pp_shutdown { listop(@_, "shutdown") } |
| 3321 | sub pp_gsockopt { listop(@_, "getsockopt") } |
| 3322 | sub pp_ssockopt { listop(@_, "setsockopt") } |
| 3323 | sub pp_chown { maybe_targmy(@_, \&listop, "chown") } |
| 3324 | sub pp_unlink { maybe_targmy(@_, \&listop, "unlink") } |
| 3325 | sub pp_chmod { maybe_targmy(@_, \&listop, "chmod") } |
| 3326 | sub pp_utime { maybe_targmy(@_, \&listop, "utime") } |
| 3327 | sub pp_rename { maybe_targmy(@_, \&listop, "rename") } |
| 3328 | sub pp_link { maybe_targmy(@_, \&listop, "link") } |
| 3329 | sub pp_symlink { maybe_targmy(@_, \&listop, "symlink") } |
| 3330 | sub pp_mkdir { maybe_targmy(@_, \&listop, "mkdir") } |
| 3331 | sub pp_open_dir { listop(@_, "opendir") } |
| 3332 | sub pp_seekdir { listop(@_, "seekdir") } |
| 3333 | sub pp_waitpid { maybe_targmy(@_, \&listop, "waitpid") } |
| 3334 | sub pp_system { maybe_targmy(@_, \&indirop, "system") } |
| 3335 | sub pp_exec { maybe_targmy(@_, \&indirop, "exec") } |
| 3336 | sub pp_kill { maybe_targmy(@_, \&listop, "kill") } |
| 3337 | sub pp_setpgrp { maybe_targmy(@_, \&listop, "setpgrp") } |
| 3338 | sub pp_getpriority { maybe_targmy(@_, \&listop, "getpriority") } |
| 3339 | sub pp_setpriority { maybe_targmy(@_, \&listop, "setpriority") } |
| 3340 | sub pp_shmget { listop(@_, "shmget") } |
| 3341 | sub pp_shmctl { listop(@_, "shmctl") } |
| 3342 | sub pp_shmread { listop(@_, "shmread") } |
| 3343 | sub pp_shmwrite { listop(@_, "shmwrite") } |
| 3344 | sub pp_msgget { listop(@_, "msgget") } |
| 3345 | sub pp_msgctl { listop(@_, "msgctl") } |
| 3346 | sub pp_msgsnd { listop(@_, "msgsnd") } |
| 3347 | sub pp_msgrcv { listop(@_, "msgrcv") } |
| 3348 | sub pp_semget { listop(@_, "semget") } |
| 3349 | sub pp_semctl { listop(@_, "semctl") } |
| 3350 | sub pp_semop { listop(@_, "semop") } |
| 3351 | sub pp_ghbyaddr { listop(@_, "gethostbyaddr") } |
| 3352 | sub pp_gnbyaddr { listop(@_, "getnetbyaddr") } |
| 3353 | sub pp_gpbynumber { listop(@_, "getprotobynumber") } |
| 3354 | sub pp_gsbyname { listop(@_, "getservbyname") } |
| 3355 | sub pp_gsbyport { listop(@_, "getservbyport") } |
| 3356 | sub pp_syscall { listop(@_, "syscall") } |
| 3357 | |
| 3358 | sub pp_glob { |
| 3359 | my $self = shift; |
| 3360 | my($op, $cx) = @_; |
| 3361 | my $kid = $op->first->sibling; # skip pushmark |
| 3362 | my $keyword = |
| 3363 | $op->flags & OPf_SPECIAL ? 'glob' : $self->keyword('glob'); |
| 3364 | my $text = $self->deparse($kid); |
| 3365 | return $cx >= 5 || $self->{'parens'} |
| 3366 | ? "$keyword($text)" |
| 3367 | : "$keyword $text"; |
| 3368 | } |
| 3369 | |
| 3370 | # Truncate is special because OPf_SPECIAL makes a bareword first arg |
| 3371 | # be a filehandle. This could probably be better fixed in the core |
| 3372 | # by moving the GV lookup into ck_truc. |
| 3373 | |
| 3374 | sub pp_truncate { |
| 3375 | my $self = shift; |
| 3376 | my($op, $cx) = @_; |
| 3377 | my(@exprs); |
| 3378 | my $parens = ($cx >= 5) || $self->{'parens'}; |
| 3379 | my $kid = $op->first->sibling; |
| 3380 | my $fh; |
| 3381 | if ($op->flags & OPf_SPECIAL) { |
| 3382 | # $kid is an OP_CONST |
| 3383 | $fh = $self->const_sv($kid)->PV; |
| 3384 | } else { |
| 3385 | $fh = $self->deparse($kid, 6); |
| 3386 | $fh = "+$fh" if not $parens and substr($fh, 0, 1) eq "("; |
| 3387 | } |
| 3388 | my $len = $self->deparse($kid->sibling, 6); |
| 3389 | my $name = $self->keyword('truncate'); |
| 3390 | if ($parens) { |
| 3391 | return "$name($fh, $len)"; |
| 3392 | } else { |
| 3393 | return "$name $fh, $len"; |
| 3394 | } |
| 3395 | } |
| 3396 | |
| 3397 | sub indirop { |
| 3398 | my $self = shift; |
| 3399 | my($op, $cx, $name) = @_; |
| 3400 | my($expr, @exprs); |
| 3401 | my $firstkid = my $kid = $op->first->sibling; |
| 3402 | my $indir = ""; |
| 3403 | if ($op->flags & OPf_STACKED) { |
| 3404 | $indir = $kid; |
| 3405 | $indir = $indir->first; # skip rv2gv |
| 3406 | if (is_scope($indir)) { |
| 3407 | $indir = "{" . $self->deparse($indir, 0) . "}"; |
| 3408 | $indir = "{;}" if $indir eq "{}"; |
| 3409 | } elsif ($indir->name eq "const" && $indir->private & OPpCONST_BARE) { |
| 3410 | $indir = $self->const_sv($indir)->PV; |
| 3411 | } else { |
| 3412 | $indir = $self->deparse($indir, 24); |
| 3413 | } |
| 3414 | $indir = $indir . " "; |
| 3415 | $kid = $kid->sibling; |
| 3416 | } |
| 3417 | if ($name eq "sort" && $op->private & (OPpSORT_NUMERIC | OPpSORT_INTEGER)) { |
| 3418 | $indir = ($op->private & OPpSORT_DESCEND) ? '{$b <=> $a} ' |
| 3419 | : '{$a <=> $b} '; |
| 3420 | } |
| 3421 | elsif ($name eq "sort" && $op->private & OPpSORT_DESCEND) { |
| 3422 | $indir = '{$b cmp $a} '; |
| 3423 | } |
| 3424 | for (; !null($kid); $kid = $kid->sibling) { |
| 3425 | $expr = $self->deparse($kid, !$indir && $kid == $firstkid && $name eq "sort" && $firstkid->name eq "entersub" ? 16 : 6); |
| 3426 | push @exprs, $expr; |
| 3427 | } |
| 3428 | my $name2; |
| 3429 | if ($name eq "sort" && $op->private & OPpSORT_REVERSE) { |
| 3430 | $name2 = $self->keyword('reverse') . ' ' . $self->keyword('sort'); |
| 3431 | } |
| 3432 | else { $name2 = $self->keyword($name) } |
| 3433 | if ($name eq "sort" && ($op->private & OPpSORT_INPLACE)) { |
| 3434 | return "$exprs[0] = $name2 $indir $exprs[0]"; |
| 3435 | } |
| 3436 | |
| 3437 | my $args = $indir . join(", ", @exprs); |
| 3438 | if ($indir ne "" && $name eq "sort") { |
| 3439 | # We don't want to say "sort(f 1, 2, 3)", since perl -w will |
| 3440 | # give bareword warnings in that case. Therefore if context |
| 3441 | # requires, we'll put parens around the outside "(sort f 1, 2, |
| 3442 | # 3)". Unfortunately, we'll currently think the parens are |
| 3443 | # necessary more often that they really are, because we don't |
| 3444 | # distinguish which side of an assignment we're on. |
| 3445 | if ($cx >= 5) { |
| 3446 | return "($name2 $args)"; |
| 3447 | } else { |
| 3448 | return "$name2 $args"; |
| 3449 | } |
| 3450 | } elsif ( |
| 3451 | !$indir && $name eq "sort" |
| 3452 | && !null($op->first->sibling) |
| 3453 | && $op->first->sibling->name eq 'entersub' |
| 3454 | ) { |
| 3455 | # We cannot say sort foo(bar), as foo will be interpreted as a |
| 3456 | # comparison routine. We have to say sort(...) in that case. |
| 3457 | return "$name2($args)"; |
| 3458 | } else { |
| 3459 | return length $args |
| 3460 | ? $self->maybe_parens_func($name2, $args, $cx, 5) |
| 3461 | : $name2 . '()' x (7 < $cx); |
| 3462 | } |
| 3463 | |
| 3464 | } |
| 3465 | |
| 3466 | sub pp_prtf { indirop(@_, "printf") } |
| 3467 | sub pp_print { indirop(@_, "print") } |
| 3468 | sub pp_say { indirop(@_, "say") } |
| 3469 | sub pp_sort { indirop(@_, "sort") } |
| 3470 | |
| 3471 | sub mapop { |
| 3472 | my $self = shift; |
| 3473 | my($op, $cx, $name) = @_; |
| 3474 | my($expr, @exprs); |
| 3475 | my $kid = $op->first; # this is the (map|grep)start |
| 3476 | $kid = $kid->first->sibling; # skip a pushmark |
| 3477 | my $code = $kid->first; # skip a null |
| 3478 | if (is_scope $code) { |
| 3479 | $code = "{" . $self->deparse($code, 0) . "} "; |
| 3480 | } else { |
| 3481 | $code = $self->deparse($code, 24); |
| 3482 | $code .= ", " if !null($kid->sibling); |
| 3483 | } |
| 3484 | $kid = $kid->sibling; |
| 3485 | for (; !null($kid); $kid = $kid->sibling) { |
| 3486 | $expr = $self->deparse($kid, 6); |
| 3487 | push @exprs, $expr if defined $expr; |
| 3488 | } |
| 3489 | return $self->maybe_parens_func($self->keyword($name), |
| 3490 | $code . join(", ", @exprs), $cx, 5); |
| 3491 | } |
| 3492 | |
| 3493 | sub pp_mapwhile { mapop(@_, "map") } |
| 3494 | sub pp_grepwhile { mapop(@_, "grep") } |
| 3495 | sub pp_mapstart { baseop(@_, "map") } |
| 3496 | sub pp_grepstart { baseop(@_, "grep") } |
| 3497 | |
| 3498 | my %uses_intro; |
| 3499 | BEGIN { |
| 3500 | @uses_intro{ |
| 3501 | eval { require B::Op_private } |
| 3502 | ? @{$B::Op_private::ops_using{OPpLVAL_INTRO}} |
| 3503 | : qw(gvsv rv2sv rv2hv rv2gv rv2av aelem helem aslice |
| 3504 | hslice delete padsv padav padhv enteriter entersub padrange |
| 3505 | pushmark cond_expr refassign list) |
| 3506 | } = (); |
| 3507 | delete @uses_intro{qw( lvref lvrefslice lvavref entersub )}; |
| 3508 | } |
| 3509 | |
| 3510 | |
| 3511 | # Look for a my/state attribute declaration in a list or ex-list. |
| 3512 | # Returns undef if not found, 'my($x, @a) :Foo(bar)' etc otherwise. |
| 3513 | # |
| 3514 | # There are three basic tree structs that are expected: |
| 3515 | # |
| 3516 | # my $x :foo; |
| 3517 | # <1> ex-list vK/LVINTRO ->c |
| 3518 | # <0> ex-pushmark v ->3 |
| 3519 | # <1> entersub[t2] vKRS*/TARG ->b |
| 3520 | # .... |
| 3521 | # <0> padsv[$x:64,65] vM/LVINTRO ->c |
| 3522 | # |
| 3523 | # my @a :foo; |
| 3524 | # my %h :foo; |
| 3525 | # |
| 3526 | # <1> ex-list vK ->c |
| 3527 | # <0> ex-pushmark v ->3 |
| 3528 | # <0> padav[@a:64,65] vM/LVINTRO ->4 |
| 3529 | # <1> entersub[t2] vKRS*/TARG ->c |
| 3530 | # .... |
| 3531 | # |
| 3532 | # my ($x,@a,%h) :foo; |
| 3533 | # |
| 3534 | # <;> nextstate(main 64 -e:1) v:{ ->3 |
| 3535 | # <@> list vKP ->w |
| 3536 | # <0> pushmark vM/LVINTRO ->4 |
| 3537 | # <0> padsv[$x:64,65] vM/LVINTRO ->5 |
| 3538 | # <0> padav[@a:64,65] vM/LVINTRO ->6 |
| 3539 | # <0> padhv[%h:64,65] vM/LVINTRO ->7 |
| 3540 | # <1> entersub[t4] vKRS*/TARG ->f |
| 3541 | # .... |
| 3542 | # <1> entersub[t5] vKRS*/TARG ->n |
| 3543 | # .... |
| 3544 | # <1> entersub[t6] vKRS*/TARG ->v |
| 3545 | # .... |
| 3546 | # where the entersub in all cases looks like |
| 3547 | # <1> entersub[t2] vKRS*/TARG ->c |
| 3548 | # <0> pushmark s ->5 |
| 3549 | # <$> const[PV "attributes"] sM ->6 |
| 3550 | # <$> const[PV "main"] sM ->7 |
| 3551 | # <1> srefgen sKM/1 ->9 |
| 3552 | # <1> ex-list lKRM ->8 |
| 3553 | # <0> padsv[@a:64,65] sRM ->8 |
| 3554 | # <$> const[PV "foo"] sM ->a |
| 3555 | # <.> method_named[PV "import"] ->b |
| 3556 | |
| 3557 | sub maybe_var_attr { |
| 3558 | my ($self, $op, $cx) = @_; |
| 3559 | |
| 3560 | my $kid = $op->first->sibling; # skip pushmark |
| 3561 | return if class($kid) eq 'NULL'; |
| 3562 | |
| 3563 | my $lop; |
| 3564 | my $type; |
| 3565 | |
| 3566 | # Extract out all the pad ops and entersub ops into |
| 3567 | # @padops and @entersubops. Return if anything else seen. |
| 3568 | # Also determine what class (if any) all the pad vars belong to |
| 3569 | my $class; |
| 3570 | my $decl; # 'my' or 'state' |
| 3571 | my (@padops, @entersubops); |
| 3572 | for ($lop = $kid; !null($lop); $lop = $lop->sibling) { |
| 3573 | my $lopname = $lop->name; |
| 3574 | my $loppriv = $lop->private; |
| 3575 | if ($lopname =~ /^pad[sah]v$/) { |
| 3576 | return unless $loppriv & OPpLVAL_INTRO; |
| 3577 | |
| 3578 | my $padname = $self->padname_sv($lop->targ); |
| 3579 | my $thisclass = ($padname->FLAGS & SVpad_TYPED) |
| 3580 | ? $padname->SvSTASH->NAME : 'main'; |
| 3581 | |
| 3582 | # all pad vars must be in the same class |
| 3583 | $class //= $thisclass; |
| 3584 | return unless $thisclass eq $class; |
| 3585 | |
| 3586 | # all pad vars must be the same sort of declaration |
| 3587 | # (all my, all state, etc) |
| 3588 | my $this = ($loppriv & OPpPAD_STATE) ? 'state' : 'my'; |
| 3589 | if (defined $decl) { |
| 3590 | return unless $this eq $decl; |
| 3591 | } |
| 3592 | $decl = $this; |
| 3593 | |
| 3594 | push @padops, $lop; |
| 3595 | } |
| 3596 | elsif ($lopname eq 'entersub') { |
| 3597 | push @entersubops, $lop; |
| 3598 | } |
| 3599 | else { |
| 3600 | return; |
| 3601 | } |
| 3602 | } |
| 3603 | |
| 3604 | return unless @padops && @padops == @entersubops; |
| 3605 | |
| 3606 | # there should be a balance: each padop has a corresponding |
| 3607 | # 'attributes'->import() method call, in the same order. |
| 3608 | |
| 3609 | my @varnames; |
| 3610 | my $attr_text; |
| 3611 | |
| 3612 | for my $i (0..$#padops) { |
| 3613 | my $padop = $padops[$i]; |
| 3614 | my $esop = $entersubops[$i]; |
| 3615 | |
| 3616 | push @varnames, $self->padname($padop->targ); |
| 3617 | |
| 3618 | return unless ($esop->flags & OPf_KIDS); |
| 3619 | |
| 3620 | my $kid = $esop->first; |
| 3621 | return unless $kid->type == OP_PUSHMARK; |
| 3622 | |
| 3623 | $kid = $kid->sibling; |
| 3624 | return unless $$kid && $kid->type == OP_CONST; |
| 3625 | return unless $self->const_sv($kid)->PV eq 'attributes'; |
| 3626 | |
| 3627 | $kid = $kid->sibling; |
| 3628 | return unless $$kid && $kid->type == OP_CONST; # __PACKAGE__ |
| 3629 | |
| 3630 | $kid = $kid->sibling; |
| 3631 | return unless $$kid |
| 3632 | && $kid->name eq "srefgen" |
| 3633 | && ($kid->flags & OPf_KIDS) |
| 3634 | && ($kid->first->flags & OPf_KIDS) |
| 3635 | && $kid->first->first->name =~ /^pad[sah]v$/ |
| 3636 | && $kid->first->first->targ == $padop->targ; |
| 3637 | |
| 3638 | $kid = $kid->sibling; |
| 3639 | my @attr; |
| 3640 | while ($$kid) { |
| 3641 | last if ($kid->type != OP_CONST); |
| 3642 | push @attr, $self->const_sv($kid)->PV; |
| 3643 | $kid = $kid->sibling; |
| 3644 | } |
| 3645 | return unless @attr; |
| 3646 | my $thisattr = ":" . join(' ', @attr); |
| 3647 | $attr_text //= $thisattr; |
| 3648 | # all import calls must have the same list of attributes |
| 3649 | return unless $attr_text eq $thisattr; |
| 3650 | |
| 3651 | return unless $kid->name eq 'method_named'; |
| 3652 | return unless $self->meth_sv($kid)->PV eq 'import'; |
| 3653 | |
| 3654 | $kid = $kid->sibling; |
| 3655 | return if $$kid; |
| 3656 | } |
| 3657 | |
| 3658 | my $res = $decl; |
| 3659 | $res .= " $class " if $class ne 'main'; |
| 3660 | $res .= |
| 3661 | (@varnames > 1) |
| 3662 | ? "(" . join(', ', @varnames) . ')' |
| 3663 | : " $varnames[0]"; |
| 3664 | |
| 3665 | return "$res $attr_text"; |
| 3666 | } |
| 3667 | |
| 3668 | |
| 3669 | sub pp_list { |
| 3670 | my $self = shift; |
| 3671 | my($op, $cx) = @_; |
| 3672 | |
| 3673 | { |
| 3674 | # might be my ($s,@a,%h) :Foo(bar); |
| 3675 | my $my_attr = maybe_var_attr($self, $op, $cx); |
| 3676 | return $my_attr if defined $my_attr; |
| 3677 | } |
| 3678 | |
| 3679 | my($expr, @exprs); |
| 3680 | my $kid = $op->first->sibling; # skip pushmark |
| 3681 | return '' if class($kid) eq 'NULL'; |
| 3682 | my $lop; |
| 3683 | my $local = "either"; # could be local(...), my(...), state(...) or our(...) |
| 3684 | my $type; |
| 3685 | for ($lop = $kid; !null($lop); $lop = $lop->sibling) { |
| 3686 | my $lopname = $lop->name; |
| 3687 | my $loppriv = $lop->private; |
| 3688 | my $newtype; |
| 3689 | if ($lopname =~ /^pad[ash]v$/ && $loppriv & OPpLVAL_INTRO) { |
| 3690 | if ($loppriv & OPpPAD_STATE) { # state() |
| 3691 | ($local = "", last) if $local !~ /^(?:either|state)$/; |
| 3692 | $local = "state"; |
| 3693 | } else { # my() |
| 3694 | ($local = "", last) if $local !~ /^(?:either|my)$/; |
| 3695 | $local = "my"; |
| 3696 | } |
| 3697 | my $padname = $self->padname_sv($lop->targ); |
| 3698 | if ($padname->FLAGS & SVpad_TYPED) { |
| 3699 | $newtype = $padname->SvSTASH->NAME; |
| 3700 | } |
| 3701 | } elsif ($lopname =~ /^(?:gv|rv2)([ash])v$/ |
| 3702 | && $loppriv & OPpOUR_INTRO |
| 3703 | or $lopname eq "null" && class($lop) eq 'UNOP' |
| 3704 | && $lop->first->name eq "gvsv" |
| 3705 | && $lop->first->private & OPpOUR_INTRO) { # our() |
| 3706 | my $newlocal = "local " x !!($loppriv & OPpLVAL_INTRO) . "our"; |
| 3707 | ($local = "", last) |
| 3708 | if $local ne 'either' && $local ne $newlocal; |
| 3709 | $local = $newlocal; |
| 3710 | my $funny = !$1 || $1 eq 's' ? '$' : $1 eq 'a' ? '@' : '%'; |
| 3711 | if (my $t = $self->find_our_type( |
| 3712 | $funny . $self->gv_or_padgv($lop->first)->NAME |
| 3713 | )) { |
| 3714 | $newtype = $t; |
| 3715 | } |
| 3716 | } elsif ($lopname ne 'undef' |
| 3717 | and !($loppriv & OPpLVAL_INTRO) |
| 3718 | || !exists $uses_intro{$lopname eq 'null' |
| 3719 | ? substr B::ppname($lop->targ), 3 |
| 3720 | : $lopname}) |
| 3721 | { |
| 3722 | $local = ""; # or not |
| 3723 | last; |
| 3724 | } elsif ($lopname ne "undef") |
| 3725 | { |
| 3726 | # local() |
| 3727 | ($local = "", last) if $local !~ /^(?:either|local)$/; |
| 3728 | $local = "local"; |
| 3729 | } |
| 3730 | if (defined $type && defined $newtype && $newtype ne $type) { |
| 3731 | $local = ''; |
| 3732 | last; |
| 3733 | } |
| 3734 | $type = $newtype; |
| 3735 | } |
| 3736 | $local = "" if $local eq "either"; # no point if it's all undefs |
| 3737 | $local &&= join ' ', map $self->keyword($_), split / /, $local; |
| 3738 | $local .= " $type " if $local && length $type; |
| 3739 | return $self->deparse($kid, $cx) if null $kid->sibling and not $local; |
| 3740 | for (; !null($kid); $kid = $kid->sibling) { |
| 3741 | if ($local) { |
| 3742 | if (class($kid) eq "UNOP" and $kid->first->name eq "gvsv") { |
| 3743 | $lop = $kid->first; |
| 3744 | } else { |
| 3745 | $lop = $kid; |
| 3746 | } |
| 3747 | $self->{'avoid_local'}{$$lop}++; |
| 3748 | $expr = $self->deparse($kid, 6); |
| 3749 | delete $self->{'avoid_local'}{$$lop}; |
| 3750 | } else { |
| 3751 | $expr = $self->deparse($kid, 6); |
| 3752 | } |
| 3753 | push @exprs, $expr; |
| 3754 | } |
| 3755 | if ($local) { |
| 3756 | if (@exprs == 1 && ($local eq 'state' || $local eq 'CORE::state')) { |
| 3757 | # 'state @a = ...' is legal, while 'state(@a) = ...' currently isn't |
| 3758 | return "$local $exprs[0]"; |
| 3759 | } |
| 3760 | return "$local(" . join(", ", @exprs) . ")"; |
| 3761 | } else { |
| 3762 | return $self->maybe_parens( join(", ", @exprs), $cx, 6); |
| 3763 | } |
| 3764 | } |
| 3765 | |
| 3766 | sub is_ifelse_cont { |
| 3767 | my $op = shift; |
| 3768 | return ($op->name eq "null" and class($op) eq "UNOP" |
| 3769 | and $op->first->name =~ /^(and|cond_expr)$/ |
| 3770 | and is_scope($op->first->first->sibling)); |
| 3771 | } |
| 3772 | |
| 3773 | sub pp_cond_expr { |
| 3774 | my $self = shift; |
| 3775 | my($op, $cx) = @_; |
| 3776 | my $cond = $op->first; |
| 3777 | my $true = $cond->sibling; |
| 3778 | my $false = $true->sibling; |
| 3779 | my $cuddle = $self->{'cuddle'}; |
| 3780 | unless ($cx < 1 and (is_scope($true) and $true->name ne "null") and |
| 3781 | (is_scope($false) || is_ifelse_cont($false)) |
| 3782 | and $self->{'expand'} < 7) { |
| 3783 | $cond = $self->deparse($cond, 8); |
| 3784 | $true = $self->deparse($true, 6); |
| 3785 | $false = $self->deparse($false, 8); |
| 3786 | return $self->maybe_parens("$cond ? $true : $false", $cx, 8); |
| 3787 | } |
| 3788 | |
| 3789 | $cond = $self->deparse($cond, 1); |
| 3790 | $true = $self->deparse($true, 0); |
| 3791 | my $head = $self->keyword("if") . " ($cond) {\n\t$true\n\b}"; |
| 3792 | my @elsifs; |
| 3793 | my $elsif; |
| 3794 | while (!null($false) and is_ifelse_cont($false)) { |
| 3795 | my $newop = $false->first; |
| 3796 | my $newcond = $newop->first; |
| 3797 | my $newtrue = $newcond->sibling; |
| 3798 | $false = $newtrue->sibling; # last in chain is OP_AND => no else |
| 3799 | if ($newcond->name eq "lineseq") |
| 3800 | { |
| 3801 | # lineseq to ensure correct line numbers in elsif() |
| 3802 | # Bug #37302 fixed by change #33710. |
| 3803 | $newcond = $newcond->first->sibling; |
| 3804 | } |
| 3805 | $newcond = $self->deparse($newcond, 1); |
| 3806 | $newtrue = $self->deparse($newtrue, 0); |
| 3807 | $elsif ||= $self->keyword("elsif"); |
| 3808 | push @elsifs, "$elsif ($newcond) {\n\t$newtrue\n\b}"; |
| 3809 | } |
| 3810 | if (!null($false)) { |
| 3811 | $false = $cuddle . $self->keyword("else") . " {\n\t" . |
| 3812 | $self->deparse($false, 0) . "\n\b}\cK"; |
| 3813 | } else { |
| 3814 | $false = "\cK"; |
| 3815 | } |
| 3816 | return $head . join($cuddle, "", @elsifs) . $false; |
| 3817 | } |
| 3818 | |
| 3819 | sub pp_once { |
| 3820 | my ($self, $op, $cx) = @_; |
| 3821 | my $cond = $op->first; |
| 3822 | my $true = $cond->sibling; |
| 3823 | |
| 3824 | my $ret = $self->deparse($true, $cx); |
| 3825 | $ret =~ s/^(\(?)\$/$1 . $self->keyword("state") . ' $'/e; |
| 3826 | $ret; |
| 3827 | } |
| 3828 | |
| 3829 | sub loop_common { |
| 3830 | my $self = shift; |
| 3831 | my($op, $cx, $init) = @_; |
| 3832 | my $enter = $op->first; |
| 3833 | my $kid = $enter->sibling; |
| 3834 | local(@$self{qw'curstash warnings hints hinthash'}) |
| 3835 | = @$self{qw'curstash warnings hints hinthash'}; |
| 3836 | my $head = ""; |
| 3837 | my $bare = 0; |
| 3838 | my $body; |
| 3839 | my $cond = undef; |
| 3840 | my $name; |
| 3841 | if ($kid->name eq "lineseq") { # bare or infinite loop |
| 3842 | if ($kid->last->name eq "unstack") { # infinite |
| 3843 | $head = "while (1) "; # Can't use for(;;) if there's a continue |
| 3844 | $cond = ""; |
| 3845 | } else { |
| 3846 | $bare = 1; |
| 3847 | } |
| 3848 | $body = $kid; |
| 3849 | } elsif ($enter->name eq "enteriter") { # foreach |
| 3850 | my $ary = $enter->first->sibling; # first was pushmark |
| 3851 | my $var = $ary->sibling; |
| 3852 | if ($ary->name eq 'null' and $enter->private & OPpITER_REVERSED) { |
| 3853 | # "reverse" was optimised away |
| 3854 | $ary = listop($self, $ary->first->sibling, 1, 'reverse'); |
| 3855 | } elsif ($enter->flags & OPf_STACKED |
| 3856 | and not null $ary->first->sibling->sibling) |
| 3857 | { |
| 3858 | $ary = $self->deparse($ary->first->sibling, 9) . " .. " . |
| 3859 | $self->deparse($ary->first->sibling->sibling, 9); |
| 3860 | } else { |
| 3861 | $ary = $self->deparse($ary, 1); |
| 3862 | } |
| 3863 | if (null $var) { |
| 3864 | $var = $self->pp_padsv($enter, 1, 1); |
| 3865 | } elsif ($var->name eq "rv2gv") { |
| 3866 | $var = $self->pp_rv2sv($var, 1); |
| 3867 | if ($enter->private & OPpOUR_INTRO) { |
| 3868 | # our declarations don't have package names |
| 3869 | $var =~ s/^(.).*::/$1/; |
| 3870 | $var = "our $var"; |
| 3871 | } |
| 3872 | } elsif ($var->name eq "gv") { |
| 3873 | $var = "\$" . $self->deparse($var, 1); |
| 3874 | } else { |
| 3875 | $var = $self->deparse($var, 1); |
| 3876 | } |
| 3877 | $body = $kid->first->first->sibling; # skip OP_AND and OP_ITER |
| 3878 | if (!is_state $body->first and $body->first->name !~ /^(?:stub|leave|scope)$/) { |
| 3879 | confess unless $var eq '$_'; |
| 3880 | $body = $body->first; |
| 3881 | return $self->deparse($body, 2) . " " |
| 3882 | . $self->keyword("foreach") . " ($ary)"; |
| 3883 | } |
| 3884 | $head = "foreach $var ($ary) "; |
| 3885 | } elsif ($kid->name eq "null") { # while/until |
| 3886 | $kid = $kid->first; |
| 3887 | $name = {"and" => "while", "or" => "until"}->{$kid->name}; |
| 3888 | $cond = $kid->first; |
| 3889 | $body = $kid->first->sibling; |
| 3890 | } elsif ($kid->name eq "stub") { # bare and empty |
| 3891 | return "{;}"; # {} could be a hashref |
| 3892 | } |
| 3893 | # If there isn't a continue block, then the next pointer for the loop |
| 3894 | # will point to the unstack, which is kid's last child, except |
| 3895 | # in a bare loop, when it will point to the leaveloop. When neither of |
| 3896 | # these conditions hold, then the second-to-last child is the continue |
| 3897 | # block (or the last in a bare loop). |
| 3898 | my $cont_start = $enter->nextop; |
| 3899 | my $cont; |
| 3900 | my $precond; |
| 3901 | my $postcond; |
| 3902 | if ($$cont_start != $$op && ${$cont_start} != ${$body->last}) { |
| 3903 | if ($bare) { |
| 3904 | $cont = $body->last; |
| 3905 | } else { |
| 3906 | $cont = $body->first; |
| 3907 | while (!null($cont->sibling->sibling)) { |
| 3908 | $cont = $cont->sibling; |
| 3909 | } |
| 3910 | } |
| 3911 | my $state = $body->first; |
| 3912 | my $cuddle = $self->{'cuddle'}; |
| 3913 | my @states; |
| 3914 | for (; $$state != $$cont; $state = $state->sibling) { |
| 3915 | push @states, $state; |
| 3916 | } |
| 3917 | $body = $self->lineseq(undef, 0, @states); |
| 3918 | if (defined $cond and not is_scope $cont and $self->{'expand'} < 3) { |
| 3919 | $precond = "for ($init; "; |
| 3920 | $postcond = "; " . $self->deparse($cont, 1) .") "; |
| 3921 | $cont = "\cK"; |
| 3922 | } else { |
| 3923 | $cont = $cuddle . "continue {\n\t" . |
| 3924 | $self->deparse($cont, 0) . "\n\b}\cK"; |
| 3925 | } |
| 3926 | } else { |
| 3927 | return "" if !defined $body; |
| 3928 | if (length $init) { |
| 3929 | $precond = "for ($init; "; |
| 3930 | $postcond = ";) "; |
| 3931 | } |
| 3932 | $cont = "\cK"; |
| 3933 | $body = $self->deparse($body, 0); |
| 3934 | } |
| 3935 | if ($precond) { # for(;;) |
| 3936 | $cond &&= $name eq 'until' |
| 3937 | ? listop($self, undef, 1, "not", $cond->first) |
| 3938 | : $self->deparse($cond, 1); |
| 3939 | $head = "$precond$cond$postcond"; |
| 3940 | } |
| 3941 | if ($name && !$head) { |
| 3942 | ref $cond and $cond = $self->deparse($cond, 1); |
| 3943 | $head = "$name ($cond) "; |
| 3944 | } |
| 3945 | $head =~ s/^(for(?:each)?|while|until)/$self->keyword($1)/e; |
| 3946 | $body =~ s/;?$/;\n/; |
| 3947 | |
| 3948 | return $head . "{\n\t" . $body . "\b}" . $cont; |
| 3949 | } |
| 3950 | |
| 3951 | sub pp_leaveloop { shift->loop_common(@_, "") } |
| 3952 | |
| 3953 | sub for_loop { |
| 3954 | my $self = shift; |
| 3955 | my($op, $cx) = @_; |
| 3956 | my $init = $self->deparse($op, 1); |
| 3957 | my $s = $op->sibling; |
| 3958 | my $ll = $s->name eq "unstack" ? $s->sibling : $s->first->sibling; |
| 3959 | return $self->loop_common($ll, $cx, $init); |
| 3960 | } |
| 3961 | |
| 3962 | sub pp_leavetry { |
| 3963 | my $self = shift; |
| 3964 | return "eval {\n\t" . $self->pp_leave(@_) . "\n\b}"; |
| 3965 | } |
| 3966 | |
| 3967 | sub _op_is_or_was { |
| 3968 | my ($op, $expect_type) = @_; |
| 3969 | my $type = $op->type; |
| 3970 | return($type == $expect_type |
| 3971 | || ($type == OP_NULL && $op->targ == $expect_type)); |
| 3972 | } |
| 3973 | |
| 3974 | sub pp_null { |
| 3975 | my($self, $op, $cx) = @_; |
| 3976 | |
| 3977 | # might be 'my $s :Foo(bar);' |
| 3978 | if ($op->targ == OP_LIST) { |
| 3979 | my $my_attr = maybe_var_attr($self, $op, $cx); |
| 3980 | return $my_attr if defined $my_attr; |
| 3981 | } |
| 3982 | |
| 3983 | if (class($op) eq "OP") { |
| 3984 | # old value is lost |
| 3985 | return $self->{'ex_const'} if $op->targ == OP_CONST; |
| 3986 | } elsif (class ($op) eq "COP") { |
| 3987 | return &pp_nextstate; |
| 3988 | } elsif ($op->first->name eq 'pushmark' |
| 3989 | or $op->first->name eq 'null' |
| 3990 | && $op->first->targ == OP_PUSHMARK |
| 3991 | && _op_is_or_was($op, OP_LIST)) { |
| 3992 | return $self->pp_list($op, $cx); |
| 3993 | } elsif ($op->first->name eq "enter") { |
| 3994 | return $self->pp_leave($op, $cx); |
| 3995 | } elsif ($op->first->name eq "leave") { |
| 3996 | return $self->pp_leave($op->first, $cx); |
| 3997 | } elsif ($op->first->name eq "scope") { |
| 3998 | return $self->pp_scope($op->first, $cx); |
| 3999 | } elsif ($op->targ == OP_STRINGIFY) { |
| 4000 | return $self->dquote($op, $cx); |
| 4001 | } elsif ($op->targ == OP_GLOB) { |
| 4002 | return $self->pp_glob( |
| 4003 | $op->first # entersub |
| 4004 | ->first # ex-list |
| 4005 | ->first # pushmark |
| 4006 | ->sibling, # glob |
| 4007 | $cx |
| 4008 | ); |
| 4009 | } elsif (!null($op->first->sibling) and |
| 4010 | $op->first->sibling->name eq "readline" and |
| 4011 | $op->first->sibling->flags & OPf_STACKED) { |
| 4012 | return $self->maybe_parens($self->deparse($op->first, 7) . " = " |
| 4013 | . $self->deparse($op->first->sibling, 7), |
| 4014 | $cx, 7); |
| 4015 | } elsif (!null($op->first->sibling) and |
| 4016 | $op->first->sibling->name =~ /^transr?\z/ and |
| 4017 | $op->first->sibling->flags & OPf_STACKED) { |
| 4018 | return $self->maybe_parens($self->deparse($op->first, 20) . " =~ " |
| 4019 | . $self->deparse($op->first->sibling, 20), |
| 4020 | $cx, 20); |
| 4021 | } elsif ($op->flags & OPf_SPECIAL && $cx < 1 && !$op->targ) { |
| 4022 | return ($self->lex_in_scope("&do") ? "CORE::do" : "do") |
| 4023 | . " {\n\t". $self->deparse($op->first, $cx) ."\n\b};"; |
| 4024 | } elsif (!null($op->first->sibling) and |
| 4025 | $op->first->sibling->name eq "null" and |
| 4026 | class($op->first->sibling) eq "UNOP" and |
| 4027 | $op->first->sibling->first->flags & OPf_STACKED and |
| 4028 | $op->first->sibling->first->name eq "rcatline") { |
| 4029 | return $self->maybe_parens($self->deparse($op->first, 18) . " .= " |
| 4030 | . $self->deparse($op->first->sibling, 18), |
| 4031 | $cx, 18); |
| 4032 | } else { |
| 4033 | return $self->deparse($op->first, $cx); |
| 4034 | } |
| 4035 | } |
| 4036 | |
| 4037 | sub padname { |
| 4038 | my $self = shift; |
| 4039 | my $targ = shift; |
| 4040 | return $self->padname_sv($targ)->PVX; |
| 4041 | } |
| 4042 | |
| 4043 | sub padany { |
| 4044 | my $self = shift; |
| 4045 | my $op = shift; |
| 4046 | return substr($self->padname($op->targ), 1); # skip $/@/% |
| 4047 | } |
| 4048 | |
| 4049 | sub pp_padsv { |
| 4050 | my $self = shift; |
| 4051 | my($op, $cx, $forbid_parens) = @_; |
| 4052 | my $targ = $op->targ; |
| 4053 | return $self->maybe_my($op, $cx, $self->padname($targ), |
| 4054 | $self->padname_sv($targ), |
| 4055 | $forbid_parens); |
| 4056 | } |
| 4057 | |
| 4058 | sub pp_padav { pp_padsv(@_) } |
| 4059 | |
| 4060 | # prepend 'keys' where its been optimised away, with suitable handling |
| 4061 | # of CORE:: and parens |
| 4062 | |
| 4063 | sub add_keys_keyword { |
| 4064 | my ($self, $str, $cx) = @_; |
| 4065 | $str = $self->maybe_parens($str, $cx, 16); |
| 4066 | # 'keys %h' versus 'keys(%h)' |
| 4067 | $str = " $str" unless $str =~ /^\(/; |
| 4068 | return $self->keyword("keys") . $str; |
| 4069 | } |
| 4070 | |
| 4071 | sub pp_padhv { |
| 4072 | my ($self, $op, $cx) = @_; |
| 4073 | my $str = pp_padsv(@_); |
| 4074 | # with OPpPADHV_ISKEYS the keys op is optimised away, except |
| 4075 | # in scalar context the old op is kept (but not executed) so its targ |
| 4076 | # can be used. |
| 4077 | if ( ($op->private & OPpPADHV_ISKEYS) |
| 4078 | && !(($op->flags & OPf_WANT) == OPf_WANT_SCALAR)) |
| 4079 | { |
| 4080 | $str = $self->add_keys_keyword($str, $cx); |
| 4081 | } |
| 4082 | $str; |
| 4083 | } |
| 4084 | |
| 4085 | sub gv_or_padgv { |
| 4086 | my $self = shift; |
| 4087 | my $op = shift; |
| 4088 | if (class($op) eq "PADOP") { |
| 4089 | return $self->padval($op->padix); |
| 4090 | } else { # class($op) eq "SVOP" |
| 4091 | return $op->gv; |
| 4092 | } |
| 4093 | } |
| 4094 | |
| 4095 | sub pp_gvsv { |
| 4096 | my $self = shift; |
| 4097 | my($op, $cx) = @_; |
| 4098 | my $gv = $self->gv_or_padgv($op); |
| 4099 | return $self->maybe_local($op, $cx, $self->stash_variable("\$", |
| 4100 | $self->gv_name($gv), $cx)); |
| 4101 | } |
| 4102 | |
| 4103 | sub pp_gv { |
| 4104 | my $self = shift; |
| 4105 | my($op, $cx) = @_; |
| 4106 | my $gv = $self->gv_or_padgv($op); |
| 4107 | return $self->maybe_qualify("", $self->gv_name($gv)); |
| 4108 | } |
| 4109 | |
| 4110 | sub pp_aelemfast_lex { |
| 4111 | my $self = shift; |
| 4112 | my($op, $cx) = @_; |
| 4113 | my $name = $self->padname($op->targ); |
| 4114 | $name =~ s/^@/\$/; |
| 4115 | my $i = $op->private; |
| 4116 | $i -= 256 if $i > 127; |
| 4117 | return $name . "[" . ($i + $self->{'arybase'}) . "]"; |
| 4118 | } |
| 4119 | |
| 4120 | sub pp_aelemfast { |
| 4121 | my $self = shift; |
| 4122 | my($op, $cx) = @_; |
| 4123 | # optimised PADAV, pre 5.15 |
| 4124 | return $self->pp_aelemfast_lex(@_) if ($op->flags & OPf_SPECIAL); |
| 4125 | |
| 4126 | my $gv = $self->gv_or_padgv($op); |
| 4127 | my($name,$quoted) = $self->stash_variable_name('@',$gv); |
| 4128 | $name = $quoted ? "$name->" : '$' . $name; |
| 4129 | my $i = $op->private; |
| 4130 | $i -= 256 if $i > 127; |
| 4131 | return $name . "[" . ($i + $self->{'arybase'}) . "]"; |
| 4132 | } |
| 4133 | |
| 4134 | sub rv2x { |
| 4135 | my $self = shift; |
| 4136 | my($op, $cx, $type) = @_; |
| 4137 | |
| 4138 | if (class($op) eq 'NULL' || !$op->can("first")) { |
| 4139 | carp("Unexpected op in pp_rv2x"); |
| 4140 | return 'XXX'; |
| 4141 | } |
| 4142 | my $kid = $op->first; |
| 4143 | if ($kid->name eq "gv") { |
| 4144 | return $self->stash_variable($type, |
| 4145 | $self->gv_name($self->gv_or_padgv($kid)), $cx); |
| 4146 | } elsif (is_scalar $kid) { |
| 4147 | my $str = $self->deparse($kid, 0); |
| 4148 | if ($str =~ /^\$([^\w\d])\z/) { |
| 4149 | # "$$+" isn't a legal way to write the scalar dereference |
| 4150 | # of $+, since the lexer can't tell you aren't trying to |
| 4151 | # do something like "$$ + 1" to get one more than your |
| 4152 | # PID. Either "${$+}" or "$${+}" are workable |
| 4153 | # disambiguations, but if the programmer did the former, |
| 4154 | # they'd be in the "else" clause below rather than here. |
| 4155 | # It's not clear if this should somehow be unified with |
| 4156 | # the code in dq and re_dq that also adds lexer |
| 4157 | # disambiguation braces. |
| 4158 | $str = '$' . "{$1}"; #' |
| 4159 | } |
| 4160 | return $type . $str; |
| 4161 | } else { |
| 4162 | return $type . "{" . $self->deparse($kid, 0) . "}"; |
| 4163 | } |
| 4164 | } |
| 4165 | |
| 4166 | sub pp_rv2sv { maybe_local(@_, rv2x(@_, "\$")) } |
| 4167 | sub pp_rv2gv { maybe_local(@_, rv2x(@_, "*")) } |
| 4168 | |
| 4169 | sub pp_rv2hv { |
| 4170 | my ($self, $op, $cx) = @_; |
| 4171 | my $str = rv2x(@_, "%"); |
| 4172 | if ($op->private & OPpRV2HV_ISKEYS) { |
| 4173 | $str = $self->add_keys_keyword($str, $cx); |
| 4174 | } |
| 4175 | return maybe_local(@_, $str); |
| 4176 | } |
| 4177 | |
| 4178 | # skip rv2av |
| 4179 | sub pp_av2arylen { |
| 4180 | my $self = shift; |
| 4181 | my($op, $cx) = @_; |
| 4182 | if ($op->first->name eq "padav") { |
| 4183 | return $self->maybe_local($op, $cx, '$#' . $self->padany($op->first)); |
| 4184 | } else { |
| 4185 | return $self->maybe_local($op, $cx, |
| 4186 | $self->rv2x($op->first, $cx, '$#')); |
| 4187 | } |
| 4188 | } |
| 4189 | |
| 4190 | # skip down to the old, ex-rv2cv |
| 4191 | sub pp_rv2cv { |
| 4192 | my ($self, $op, $cx) = @_; |
| 4193 | if (!null($op->first) && $op->first->name eq 'null' && |
| 4194 | $op->first->targ == OP_LIST) |
| 4195 | { |
| 4196 | return $self->rv2x($op->first->first->sibling, $cx, "&") |
| 4197 | } |
| 4198 | else { |
| 4199 | return $self->rv2x($op, $cx, "") |
| 4200 | } |
| 4201 | } |
| 4202 | |
| 4203 | sub list_const { |
| 4204 | my $self = shift; |
| 4205 | my($cx, @list) = @_; |
| 4206 | my @a = map $self->const($_, 6), @list; |
| 4207 | if (@a == 0) { |
| 4208 | return "()"; |
| 4209 | } elsif (@a == 1) { |
| 4210 | return $a[0]; |
| 4211 | } elsif ( @a > 2 and !grep(!/^-?\d+$/, @a)) { |
| 4212 | # collapse (-1,0,1,2) into (-1..2) |
| 4213 | my ($s, $e) = @a[0,-1]; |
| 4214 | my $i = $s; |
| 4215 | return $self->maybe_parens("$s..$e", $cx, 9) |
| 4216 | unless grep $i++ != $_, @a; |
| 4217 | } |
| 4218 | return $self->maybe_parens(join(", ", @a), $cx, 6); |
| 4219 | } |
| 4220 | |
| 4221 | sub pp_rv2av { |
| 4222 | my $self = shift; |
| 4223 | my($op, $cx) = @_; |
| 4224 | my $kid = $op->first; |
| 4225 | if ($kid->name eq "const") { # constant list |
| 4226 | my $av = $self->const_sv($kid); |
| 4227 | return $self->list_const($cx, $av->ARRAY); |
| 4228 | } else { |
| 4229 | return $self->maybe_local($op, $cx, $self->rv2x($op, $cx, "\@")); |
| 4230 | } |
| 4231 | } |
| 4232 | |
| 4233 | sub is_subscriptable { |
| 4234 | my $op = shift; |
| 4235 | if ($op->name =~ /^([ahg]elem|multideref$)/) { |
| 4236 | return 1; |
| 4237 | } elsif ($op->name eq "entersub") { |
| 4238 | my $kid = $op->first; |
| 4239 | return 0 unless null $kid->sibling; |
| 4240 | $kid = $kid->first; |
| 4241 | $kid = $kid->sibling until null $kid->sibling; |
| 4242 | return 0 if is_scope($kid); |
| 4243 | $kid = $kid->first; |
| 4244 | return 0 if $kid->name eq "gv" || $kid->name eq "padcv"; |
| 4245 | return 0 if is_scalar($kid); |
| 4246 | return is_subscriptable($kid); |
| 4247 | } else { |
| 4248 | return 0; |
| 4249 | } |
| 4250 | } |
| 4251 | |
| 4252 | sub elem_or_slice_array_name |
| 4253 | { |
| 4254 | my $self = shift; |
| 4255 | my ($array, $left, $padname, $allow_arrow) = @_; |
| 4256 | |
| 4257 | if ($array->name eq $padname) { |
| 4258 | return $self->padany($array); |
| 4259 | } elsif (is_scope($array)) { # ${expr}[0] |
| 4260 | return "{" . $self->deparse($array, 0) . "}"; |
| 4261 | } elsif ($array->name eq "gv") { |
| 4262 | ($array, my $quoted) = |
| 4263 | $self->stash_variable_name( |
| 4264 | $left eq '[' ? '@' : '%', $self->gv_or_padgv($array) |
| 4265 | ); |
| 4266 | if (!$allow_arrow && $quoted) { |
| 4267 | # This cannot happen. |
| 4268 | die "Invalid variable name $array for slice"; |
| 4269 | } |
| 4270 | return $quoted ? "$array->" : $array; |
| 4271 | } elsif (!$allow_arrow || is_scalar $array) { # $x[0], $$x[0], ... |
| 4272 | return $self->deparse($array, 24); |
| 4273 | } else { |
| 4274 | return undef; |
| 4275 | } |
| 4276 | } |
| 4277 | |
| 4278 | sub elem_or_slice_single_index |
| 4279 | { |
| 4280 | my $self = shift; |
| 4281 | my ($idx) = @_; |
| 4282 | |
| 4283 | $idx = $self->deparse($idx, 1); |
| 4284 | |
| 4285 | # Outer parens in an array index will confuse perl |
| 4286 | # if we're interpolating in a regular expression, i.e. |
| 4287 | # /$x$foo[(-1)]/ is *not* the same as /$x$foo[-1]/ |
| 4288 | # |
| 4289 | # If $self->{parens}, then an initial '(' will |
| 4290 | # definitely be paired with a final ')'. If |
| 4291 | # !$self->{parens}, the misleading parens won't |
| 4292 | # have been added in the first place. |
| 4293 | # |
| 4294 | # [You might think that we could get "(...)...(...)" |
| 4295 | # where the initial and final parens do not match |
| 4296 | # each other. But we can't, because the above would |
| 4297 | # only happen if there's an infix binop between the |
| 4298 | # two pairs of parens, and *that* means that the whole |
| 4299 | # expression would be parenthesized as well.] |
| 4300 | # |
| 4301 | $idx =~ s/^\((.*)\)$/$1/ if $self->{'parens'}; |
| 4302 | |
| 4303 | # Hash-element braces will autoquote a bareword inside themselves. |
| 4304 | # We need to make sure that C<$hash{warn()}> doesn't come out as |
| 4305 | # C<$hash{warn}>, which has a quite different meaning. Currently |
| 4306 | # B::Deparse will always quote strings, even if the string was a |
| 4307 | # bareword in the original (i.e. the OPpCONST_BARE flag is ignored |
| 4308 | # for constant strings.) So we can cheat slightly here - if we see |
| 4309 | # a bareword, we know that it is supposed to be a function call. |
| 4310 | # |
| 4311 | $idx =~ s/^([A-Za-z_]\w*)$/$1()/; |
| 4312 | |
| 4313 | return $idx; |
| 4314 | } |
| 4315 | |
| 4316 | sub elem { |
| 4317 | my $self = shift; |
| 4318 | my ($op, $cx, $left, $right, $padname) = @_; |
| 4319 | my($array, $idx) = ($op->first, $op->first->sibling); |
| 4320 | |
| 4321 | $idx = $self->elem_or_slice_single_index($idx); |
| 4322 | |
| 4323 | unless ($array->name eq $padname) { # Maybe this has been fixed |
| 4324 | $array = $array->first; # skip rv2av (or ex-rv2av in _53+) |
| 4325 | } |
| 4326 | if (my $array_name=$self->elem_or_slice_array_name |
| 4327 | ($array, $left, $padname, 1)) { |
| 4328 | return ($array_name =~ /->\z/ |
| 4329 | ? $array_name |
| 4330 | : $array_name eq '#' ? '${#}' : "\$" . $array_name) |
| 4331 | . $left . $idx . $right; |
| 4332 | } else { |
| 4333 | # $x[20][3]{hi} or expr->[20] |
| 4334 | my $arrow = is_subscriptable($array) ? "" : "->"; |
| 4335 | return $self->deparse($array, 24) . $arrow . $left . $idx . $right; |
| 4336 | } |
| 4337 | |
| 4338 | } |
| 4339 | |
| 4340 | # a simplified version of elem_or_slice_array_name() |
| 4341 | # for the use of pp_multideref |
| 4342 | |
| 4343 | sub multideref_var_name { |
| 4344 | my $self = shift; |
| 4345 | my ($gv, $is_hash) = @_; |
| 4346 | |
| 4347 | my ($name, $quoted) = |
| 4348 | $self->stash_variable_name( $is_hash ? '%' : '@', $gv); |
| 4349 | return $quoted ? "$name->" |
| 4350 | : $name eq '#' |
| 4351 | ? '${#}' # avoid ${#}[1] => $#[1] |
| 4352 | : '$' . $name; |
| 4353 | } |
| 4354 | |
| 4355 | |
| 4356 | # deparse an OP_MULTICONCAT. If $in_dq is 1, we're within |
| 4357 | # a double-quoted string, so for example. |
| 4358 | # "abc\Qdef$x\Ebar" |
| 4359 | # might get compiled as |
| 4360 | # multiconcat("abc", metaquote(multiconcat("def", $x)), "bar") |
| 4361 | # and the inner multiconcat should be deparsed as C<def$x> rather than |
| 4362 | # the normal C<def . $x> |
| 4363 | # Ditto if $in_dq is 2, handle qr/...\Qdef$x\E.../. |
| 4364 | |
| 4365 | sub do_multiconcat { |
| 4366 | my $self = shift; |
| 4367 | my($op, $cx, $in_dq) = @_; |
| 4368 | |
| 4369 | my $kid; |
| 4370 | my @kids; |
| 4371 | my $assign; |
| 4372 | my $append; |
| 4373 | my $lhs = ""; |
| 4374 | |
| 4375 | for ($kid = $op->first; !null $kid; $kid = $kid->sibling) { |
| 4376 | # skip the consts and/or padsv we've optimised away |
| 4377 | push @kids, $kid |
| 4378 | unless $kid->type == OP_NULL |
| 4379 | && ( $kid->targ == OP_PADSV |
| 4380 | || $kid->targ == OP_CONST |
| 4381 | || $kid->targ == OP_PUSHMARK); |
| 4382 | } |
| 4383 | |
| 4384 | $append = ($op->private & OPpMULTICONCAT_APPEND); |
| 4385 | |
| 4386 | if ($op->private & OPpTARGET_MY) { |
| 4387 | # '$lex = ...' or '$lex .= ....' or 'my $lex = ' |
| 4388 | $lhs = $self->padname($op->targ); |
| 4389 | $lhs = "my $lhs" if ($op->private & OPpLVAL_INTRO); |
| 4390 | $assign = 1; |
| 4391 | } |
| 4392 | elsif ($op->flags & OPf_STACKED) { |
| 4393 | # 'expr = ...' or 'expr .= ....' |
| 4394 | my $expr = $append ? shift(@kids) : pop(@kids); |
| 4395 | $lhs = $self->deparse($expr, 7); |
| 4396 | $assign = 1; |
| 4397 | } |
| 4398 | |
| 4399 | if ($assign) { |
| 4400 | $lhs .= $append ? ' .= ' : ' = '; |
| 4401 | } |
| 4402 | |
| 4403 | my ($nargs, $const_str, @const_lens) = $op->aux_list($self->{curcv}); |
| 4404 | |
| 4405 | my @consts; |
| 4406 | my $i = 0; |
| 4407 | for (@const_lens) { |
| 4408 | if ($_ == -1) { |
| 4409 | push @consts, undef; |
| 4410 | } |
| 4411 | else { |
| 4412 | push @consts, substr($const_str, $i, $_); |
| 4413 | my @args; |
| 4414 | $i += $_; |
| 4415 | } |
| 4416 | } |
| 4417 | |
| 4418 | my $rhs = ""; |
| 4419 | |
| 4420 | if ( $in_dq |
| 4421 | || (($op->private & OPpMULTICONCAT_STRINGIFY) && !$self->{'unquote'})) |
| 4422 | { |
| 4423 | # "foo=$foo bar=$bar " |
| 4424 | my $not_first; |
| 4425 | while (@consts) { |
| 4426 | if ($not_first) { |
| 4427 | my $s = $self->dq(shift(@kids), 18); |
| 4428 | # don't deparse "a${$}b" as "a$$b" |
| 4429 | $s = '${$}' if $s eq '$$'; |
| 4430 | $rhs = dq_disambiguate($rhs, $s); |
| 4431 | } |
| 4432 | $not_first = 1; |
| 4433 | my $c = shift @consts; |
| 4434 | if (defined $c) { |
| 4435 | if ($in_dq == 2) { |
| 4436 | # in pattern: don't convert newline to '\n' etc etc |
| 4437 | my $s = re_uninterp(escape_re(re_unback($c))); |
| 4438 | $rhs = re_dq_disambiguate($rhs, $s) |
| 4439 | } |
| 4440 | else { |
| 4441 | my $s = uninterp(escape_str(unback($c))); |
| 4442 | $rhs = dq_disambiguate($rhs, $s) |
| 4443 | } |
| 4444 | } |
| 4445 | } |
| 4446 | return $rhs if $in_dq; |
| 4447 | $rhs = single_delim("qq", '"', $rhs, $self); |
| 4448 | } |
| 4449 | elsif ($op->private & OPpMULTICONCAT_FAKE) { |
| 4450 | # sprintf("foo=%s bar=%s ", $foo, $bar) |
| 4451 | |
| 4452 | my @all; |
| 4453 | @consts = map { $_ //= ''; s/%/%%/g; $_ } @consts; |
| 4454 | my $fmt = join '%s', @consts; |
| 4455 | push @all, $self->quoted_const_str($fmt); |
| 4456 | |
| 4457 | # the following is a stripped down copy of sub listop {} |
| 4458 | my $parens = $assign || ($cx >= 5) || $self->{'parens'}; |
| 4459 | my $fullname = $self->keyword('sprintf'); |
| 4460 | push @all, map $self->deparse($_, 6), @kids; |
| 4461 | |
| 4462 | $rhs = $parens |
| 4463 | ? "$fullname(" . join(", ", @all) . ")" |
| 4464 | : "$fullname " . join(", ", @all); |
| 4465 | } |
| 4466 | else { |
| 4467 | # "foo=" . $foo . " bar=" . $bar |
| 4468 | my @all; |
| 4469 | my $not_first; |
| 4470 | while (@consts) { |
| 4471 | push @all, $self->deparse(shift(@kids), 18) if $not_first; |
| 4472 | $not_first = 1; |
| 4473 | my $c = shift @consts; |
| 4474 | if (defined $c) { |
| 4475 | push @all, $self->quoted_const_str($c); |
| 4476 | } |
| 4477 | } |
| 4478 | $rhs .= join ' . ', @all; |
| 4479 | } |
| 4480 | |
| 4481 | my $text = $lhs . $rhs; |
| 4482 | |
| 4483 | $text = "($text)" if ($cx >= (($assign) ? 7 : 18+1)) |
| 4484 | || $self->{'parens'}; |
| 4485 | |
| 4486 | return $text; |
| 4487 | } |
| 4488 | |
| 4489 | |
| 4490 | sub pp_multiconcat { |
| 4491 | my $self = shift; |
| 4492 | $self->do_multiconcat(@_, 0); |
| 4493 | } |
| 4494 | |
| 4495 | |
| 4496 | sub pp_multideref { |
| 4497 | my $self = shift; |
| 4498 | my($op, $cx) = @_; |
| 4499 | my $text = ""; |
| 4500 | |
| 4501 | if ($op->private & OPpMULTIDEREF_EXISTS) { |
| 4502 | $text = $self->keyword("exists"). " "; |
| 4503 | } |
| 4504 | elsif ($op->private & OPpMULTIDEREF_DELETE) { |
| 4505 | $text = $self->keyword("delete"). " "; |
| 4506 | } |
| 4507 | elsif ($op->private & OPpLVAL_INTRO) { |
| 4508 | $text = $self->keyword("local"). " "; |
| 4509 | } |
| 4510 | |
| 4511 | if ($op->first && ($op->first->flags & OPf_KIDS)) { |
| 4512 | # arbitrary initial expression, e.g. f(1,2,3)->[...] |
| 4513 | my $expr = $self->deparse($op->first, 24); |
| 4514 | # stop "exists (expr)->{...}" being interpreted as |
| 4515 | #"(exists (expr))->{...}" |
| 4516 | $expr = "+$expr" if $expr =~ /^\(/; |
| 4517 | $text .= $expr; |
| 4518 | } |
| 4519 | |
| 4520 | my @items = $op->aux_list($self->{curcv}); |
| 4521 | my $actions = shift @items; |
| 4522 | |
| 4523 | my $is_hash; |
| 4524 | my $derefs = 0; |
| 4525 | |
| 4526 | while (1) { |
| 4527 | if (($actions & MDEREF_ACTION_MASK) == MDEREF_reload) { |
| 4528 | $actions = shift @items; |
| 4529 | next; |
| 4530 | } |
| 4531 | |
| 4532 | $is_hash = ( |
| 4533 | ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_pop_rv2hv_helem |
| 4534 | || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_gvsv_vivify_rv2hv_helem |
| 4535 | || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_padsv_vivify_rv2hv_helem |
| 4536 | || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_vivify_rv2hv_helem |
| 4537 | || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_padhv_helem |
| 4538 | || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_gvhv_helem |
| 4539 | ); |
| 4540 | |
| 4541 | if ( ($actions & MDEREF_ACTION_MASK) == MDEREF_AV_padav_aelem |
| 4542 | || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_padhv_helem) |
| 4543 | { |
| 4544 | $derefs = 1; |
| 4545 | $text .= '$' . substr($self->padname(shift @items), 1); |
| 4546 | } |
| 4547 | elsif ( ($actions & MDEREF_ACTION_MASK) == MDEREF_AV_gvav_aelem |
| 4548 | || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_gvhv_helem) |
| 4549 | { |
| 4550 | $derefs = 1; |
| 4551 | $text .= $self->multideref_var_name(shift @items, $is_hash); |
| 4552 | } |
| 4553 | else { |
| 4554 | if ( ($actions & MDEREF_ACTION_MASK) == |
| 4555 | MDEREF_AV_padsv_vivify_rv2av_aelem |
| 4556 | || ($actions & MDEREF_ACTION_MASK) == |
| 4557 | MDEREF_HV_padsv_vivify_rv2hv_helem) |
| 4558 | { |
| 4559 | $text .= $self->padname(shift @items); |
| 4560 | } |
| 4561 | elsif ( ($actions & MDEREF_ACTION_MASK) == |
| 4562 | MDEREF_AV_gvsv_vivify_rv2av_aelem |
| 4563 | || ($actions & MDEREF_ACTION_MASK) == |
| 4564 | MDEREF_HV_gvsv_vivify_rv2hv_helem) |
| 4565 | { |
| 4566 | $text .= $self->multideref_var_name(shift @items, $is_hash); |
| 4567 | } |
| 4568 | elsif ( ($actions & MDEREF_ACTION_MASK) == |
| 4569 | MDEREF_AV_pop_rv2av_aelem |
| 4570 | || ($actions & MDEREF_ACTION_MASK) == |
| 4571 | MDEREF_HV_pop_rv2hv_helem) |
| 4572 | { |
| 4573 | if ( ($op->flags & OPf_KIDS) |
| 4574 | && ( _op_is_or_was($op->first, OP_RV2AV) |
| 4575 | || _op_is_or_was($op->first, OP_RV2HV)) |
| 4576 | && ($op->first->flags & OPf_KIDS) |
| 4577 | && ( _op_is_or_was($op->first->first, OP_AELEM) |
| 4578 | || _op_is_or_was($op->first->first, OP_HELEM)) |
| 4579 | ) |
| 4580 | { |
| 4581 | $derefs++; |
| 4582 | } |
| 4583 | } |
| 4584 | |
| 4585 | $text .= '->' if !$derefs++; |
| 4586 | } |
| 4587 | |
| 4588 | |
| 4589 | if (($actions & MDEREF_INDEX_MASK) == MDEREF_INDEX_none) { |
| 4590 | last; |
| 4591 | } |
| 4592 | |
| 4593 | $text .= $is_hash ? '{' : '['; |
| 4594 | |
| 4595 | if (($actions & MDEREF_INDEX_MASK) == MDEREF_INDEX_const) { |
| 4596 | my $key = shift @items; |
| 4597 | if ($is_hash) { |
| 4598 | $text .= $self->const($key, $cx); |
| 4599 | } |
| 4600 | else { |
| 4601 | $text .= $key; |
| 4602 | } |
| 4603 | } |
| 4604 | elsif (($actions & MDEREF_INDEX_MASK) == MDEREF_INDEX_padsv) { |
| 4605 | $text .= $self->padname(shift @items); |
| 4606 | } |
| 4607 | elsif (($actions & MDEREF_INDEX_MASK) == MDEREF_INDEX_gvsv) { |
| 4608 | $text .= '$' . ($self->stash_variable_name('$', shift @items))[0]; |
| 4609 | } |
| 4610 | |
| 4611 | $text .= $is_hash ? '}' : ']'; |
| 4612 | |
| 4613 | if ($actions & MDEREF_FLAG_last) { |
| 4614 | last; |
| 4615 | } |
| 4616 | $actions >>= MDEREF_SHIFT; |
| 4617 | } |
| 4618 | |
| 4619 | return $text; |
| 4620 | } |
| 4621 | |
| 4622 | |
| 4623 | sub pp_aelem { maybe_local(@_, elem(@_, "[", "]", "padav")) } |
| 4624 | sub pp_helem { maybe_local(@_, elem(@_, "{", "}", "padhv")) } |
| 4625 | |
| 4626 | sub pp_gelem { |
| 4627 | my $self = shift; |
| 4628 | my($op, $cx) = @_; |
| 4629 | my($glob, $part) = ($op->first, $op->last); |
| 4630 | $glob = $glob->first; # skip rv2gv |
| 4631 | $glob = $glob->first if $glob->name eq "rv2gv"; # this one's a bug |
| 4632 | my $scope = is_scope($glob); |
| 4633 | $glob = $self->deparse($glob, 0); |
| 4634 | $part = $self->deparse($part, 1); |
| 4635 | $glob =~ s/::\z// unless $scope; |
| 4636 | return "*" . ($scope ? "{$glob}" : $glob) . "{$part}"; |
| 4637 | } |
| 4638 | |
| 4639 | sub slice { |
| 4640 | my $self = shift; |
| 4641 | my ($op, $cx, $left, $right, $regname, $padname) = @_; |
| 4642 | my $last; |
| 4643 | my(@elems, $kid, $array, $list); |
| 4644 | if (class($op) eq "LISTOP") { |
| 4645 | $last = $op->last; |
| 4646 | } else { # ex-hslice inside delete() |
| 4647 | for ($kid = $op->first; !null $kid->sibling; $kid = $kid->sibling) {} |
| 4648 | $last = $kid; |
| 4649 | } |
| 4650 | $array = $last; |
| 4651 | $array = $array->first |
| 4652 | if $array->name eq $regname or $array->name eq "null"; |
| 4653 | $array = $self->elem_or_slice_array_name($array,$left,$padname,0); |
| 4654 | $kid = $op->first->sibling; # skip pushmark |
| 4655 | if ($kid->name eq "list") { |
| 4656 | $kid = $kid->first->sibling; # skip list, pushmark |
| 4657 | for (; !null $kid; $kid = $kid->sibling) { |
| 4658 | push @elems, $self->deparse($kid, 6); |
| 4659 | } |
| 4660 | $list = join(", ", @elems); |
| 4661 | } else { |
| 4662 | $list = $self->elem_or_slice_single_index($kid); |
| 4663 | } |
| 4664 | my $lead = ( _op_is_or_was($op, OP_KVHSLICE) |
| 4665 | || _op_is_or_was($op, OP_KVASLICE)) |
| 4666 | ? '%' : '@'; |
| 4667 | return $lead . $array . $left . $list . $right; |
| 4668 | } |
| 4669 | |
| 4670 | sub pp_aslice { maybe_local(@_, slice(@_, "[", "]", "rv2av", "padav")) } |
| 4671 | sub pp_kvaslice { slice(@_, "[", "]", "rv2av", "padav") } |
| 4672 | sub pp_hslice { maybe_local(@_, slice(@_, "{", "}", "rv2hv", "padhv")) } |
| 4673 | sub pp_kvhslice { slice(@_, "{", "}", "rv2hv", "padhv") } |
| 4674 | |
| 4675 | sub pp_lslice { |
| 4676 | my $self = shift; |
| 4677 | my($op, $cx) = @_; |
| 4678 | my $idx = $op->first; |
| 4679 | my $list = $op->last; |
| 4680 | my(@elems, $kid); |
| 4681 | $list = $self->deparse($list, 1); |
| 4682 | $idx = $self->deparse($idx, 1); |
| 4683 | return "($list)" . "[$idx]"; |
| 4684 | } |
| 4685 | |
| 4686 | sub want_scalar { |
| 4687 | my $op = shift; |
| 4688 | return ($op->flags & OPf_WANT) == OPf_WANT_SCALAR; |
| 4689 | } |
| 4690 | |
| 4691 | sub want_list { |
| 4692 | my $op = shift; |
| 4693 | return ($op->flags & OPf_WANT) == OPf_WANT_LIST; |
| 4694 | } |
| 4695 | |
| 4696 | sub _method { |
| 4697 | my $self = shift; |
| 4698 | my($op, $cx) = @_; |
| 4699 | my $kid = $op->first->sibling; # skip pushmark |
| 4700 | my($meth, $obj, @exprs); |
| 4701 | if ($kid->name eq "list" and want_list $kid) { |
| 4702 | # When an indirect object isn't a bareword but the args are in |
| 4703 | # parens, the parens aren't part of the method syntax (the LLAFR |
| 4704 | # doesn't apply), but they make a list with OPf_PARENS set that |
| 4705 | # doesn't get flattened by the append_elem that adds the method, |
| 4706 | # making a (object, arg1, arg2, ...) list where the object |
| 4707 | # usually is. This can be distinguished from |
| 4708 | # '($obj, $arg1, $arg2)->meth()' (which is legal if $arg2 is an |
| 4709 | # object) because in the later the list is in scalar context |
| 4710 | # as the left side of -> always is, while in the former |
| 4711 | # the list is in list context as method arguments always are. |
| 4712 | # (Good thing there aren't method prototypes!) |
| 4713 | $meth = $kid->sibling; |
| 4714 | $kid = $kid->first->sibling; # skip pushmark |
| 4715 | $obj = $kid; |
| 4716 | $kid = $kid->sibling; |
| 4717 | for (; not null $kid; $kid = $kid->sibling) { |
| 4718 | push @exprs, $kid; |
| 4719 | } |
| 4720 | } else { |
| 4721 | $obj = $kid; |
| 4722 | $kid = $kid->sibling; |
| 4723 | for (; !null ($kid->sibling) && $kid->name!~/^method(?:_named)?\z/; |
| 4724 | $kid = $kid->sibling) { |
| 4725 | push @exprs, $kid |
| 4726 | } |
| 4727 | $meth = $kid; |
| 4728 | } |
| 4729 | |
| 4730 | if ($meth->name eq "method_named") { |
| 4731 | $meth = $self->meth_sv($meth)->PV; |
| 4732 | } elsif ($meth->name eq "method_super") { |
| 4733 | $meth = "SUPER::".$self->meth_sv($meth)->PV; |
| 4734 | } elsif ($meth->name eq "method_redir") { |
| 4735 | $meth = $self->meth_rclass_sv($meth)->PV.'::'.$self->meth_sv($meth)->PV; |
| 4736 | } elsif ($meth->name eq "method_redir_super") { |
| 4737 | $meth = $self->meth_rclass_sv($meth)->PV.'::SUPER::'. |
| 4738 | $self->meth_sv($meth)->PV; |
| 4739 | } else { |
| 4740 | $meth = $meth->first; |
| 4741 | if ($meth->name eq "const") { |
| 4742 | # As of 5.005_58, this case is probably obsoleted by the |
| 4743 | # method_named case above |
| 4744 | $meth = $self->const_sv($meth)->PV; # needs to be bare |
| 4745 | } |
| 4746 | } |
| 4747 | |
| 4748 | return { method => $meth, variable_method => ref($meth), |
| 4749 | object => $obj, args => \@exprs }, |
| 4750 | $cx; |
| 4751 | } |
| 4752 | |
| 4753 | # compat function only |
| 4754 | sub method { |
| 4755 | my $self = shift; |
| 4756 | my $info = $self->_method(@_); |
| 4757 | return $self->e_method( $self->_method(@_) ); |
| 4758 | } |
| 4759 | |
| 4760 | sub e_method { |
| 4761 | my ($self, $info, $cx) = @_; |
| 4762 | my $obj = $self->deparse($info->{object}, 24); |
| 4763 | |
| 4764 | my $meth = $info->{method}; |
| 4765 | $meth = $self->deparse($meth, 1) if $info->{variable_method}; |
| 4766 | my $args = join(", ", map { $self->deparse($_, 6) } @{$info->{args}} ); |
| 4767 | if ($info->{object}->name eq 'scope' && want_list $info->{object}) { |
| 4768 | # method { $object } |
| 4769 | # This must be deparsed this way to preserve list context |
| 4770 | # of $object. |
| 4771 | my $need_paren = $cx >= 6; |
| 4772 | return '(' x $need_paren |
| 4773 | . $meth . substr($obj,2) # chop off the "do" |
| 4774 | . " $args" |
| 4775 | . ')' x $need_paren; |
| 4776 | } |
| 4777 | my $kid = $obj . "->" . $meth; |
| 4778 | if (length $args) { |
| 4779 | return $kid . "(" . $args . ")"; # parens mandatory |
| 4780 | } else { |
| 4781 | return $kid; |
| 4782 | } |
| 4783 | } |
| 4784 | |
| 4785 | # returns "&" if the prototype doesn't match the args, |
| 4786 | # or ("", $args_after_prototype_demunging) if it does. |
| 4787 | sub check_proto { |
| 4788 | my $self = shift; |
| 4789 | return "&" if $self->{'noproto'}; |
| 4790 | my($proto, @args) = @_; |
| 4791 | my($arg, $real); |
| 4792 | my $doneok = 0; |
| 4793 | my @reals; |
| 4794 | # An unbackslashed @ or % gobbles up the rest of the args |
| 4795 | 1 while $proto =~ s/(?<!\\)([@%])[^\]]+$/$1/; |
| 4796 | $proto =~ s/^\s*//; |
| 4797 | while ($proto) { |
| 4798 | $proto =~ s/^(\\?[\$\@&%*_]|\\\[[\$\@&%*]+\]|;|)\s*//; |
| 4799 | my $chr = $1; |
| 4800 | if ($chr eq "") { |
| 4801 | return "&" if @args; |
| 4802 | } elsif ($chr eq ";") { |
| 4803 | $doneok = 1; |
| 4804 | } elsif ($chr eq "@" or $chr eq "%") { |
| 4805 | push @reals, map($self->deparse($_, 6), @args); |
| 4806 | @args = (); |
| 4807 | } else { |
| 4808 | $arg = shift @args; |
| 4809 | last unless $arg; |
| 4810 | if ($chr eq "\$" || $chr eq "_") { |
| 4811 | if (want_scalar $arg) { |
| 4812 | push @reals, $self->deparse($arg, 6); |
| 4813 | } else { |
| 4814 | return "&"; |
| 4815 | } |
| 4816 | } elsif ($chr eq "&") { |
| 4817 | if ($arg->name =~ /^(s?refgen|undef)$/) { |
| 4818 | push @reals, $self->deparse($arg, 6); |
| 4819 | } else { |
| 4820 | return "&"; |
| 4821 | } |
| 4822 | } elsif ($chr eq "*") { |
| 4823 | if ($arg->name =~ /^s?refgen$/ |
| 4824 | and $arg->first->first->name eq "rv2gv") |
| 4825 | { |
| 4826 | $real = $arg->first->first; # skip refgen, null |
| 4827 | if ($real->first->name eq "gv") { |
| 4828 | push @reals, $self->deparse($real, 6); |
| 4829 | } else { |
| 4830 | push @reals, $self->deparse($real->first, 6); |
| 4831 | } |
| 4832 | } else { |
| 4833 | return "&"; |
| 4834 | } |
| 4835 | } elsif (substr($chr, 0, 1) eq "\\") { |
| 4836 | $chr =~ tr/\\[]//d; |
| 4837 | if ($arg->name =~ /^s?refgen$/ and |
| 4838 | !null($real = $arg->first) and |
| 4839 | ($chr =~ /\$/ && is_scalar($real->first) |
| 4840 | or ($chr =~ /@/ |
| 4841 | && class($real->first->sibling) ne 'NULL' |
| 4842 | && $real->first->sibling->name |
| 4843 | =~ /^(rv2|pad)av$/) |
| 4844 | or ($chr =~ /%/ |
| 4845 | && class($real->first->sibling) ne 'NULL' |
| 4846 | && $real->first->sibling->name |
| 4847 | =~ /^(rv2|pad)hv$/) |
| 4848 | #or ($chr =~ /&/ # This doesn't work |
| 4849 | # && $real->first->name eq "rv2cv") |
| 4850 | or ($chr =~ /\*/ |
| 4851 | && $real->first->name eq "rv2gv"))) |
| 4852 | { |
| 4853 | push @reals, $self->deparse($real, 6); |
| 4854 | } else { |
| 4855 | return "&"; |
| 4856 | } |
| 4857 | } |
| 4858 | } |
| 4859 | } |
| 4860 | return "&" if $proto and !$doneok; # too few args and no ';' |
| 4861 | return "&" if @args; # too many args |
| 4862 | return ("", join ", ", @reals); |
| 4863 | } |
| 4864 | |
| 4865 | sub retscalar { |
| 4866 | my $name = $_[0]->name; |
| 4867 | # XXX There has to be a better way of doing this scalar-op check. |
| 4868 | # Currently PL_opargs is not exposed. |
| 4869 | if ($name eq 'null') { |
| 4870 | $name = substr B::ppname($_[0]->targ), 3 |
| 4871 | } |
| 4872 | $name =~ /^(?:scalar|pushmark|wantarray|const|gvsv|gv|padsv|rv2gv |
| 4873 | |rv2sv|av2arylen|anoncode|prototype|srefgen|ref|bless |
| 4874 | |regcmaybe|regcreset|regcomp|qr|subst|substcont|trans |
| 4875 | |transr|sassign|chop|schop|chomp|schomp|defined|undef |
| 4876 | |study|pos|preinc|i_preinc|predec|i_predec|postinc |
| 4877 | |i_postinc|postdec|i_postdec|pow|multiply|i_multiply |
| 4878 | |divide|i_divide|modulo|i_modulo|add|i_add|subtract |
| 4879 | |i_subtract|concat|multiconcat|stringify|left_shift|right_shift|lt |
| 4880 | |i_lt|gt|i_gt|le|i_le|ge|i_ge|eq|i_eq|ne|i_ne|ncmp|i_ncmp |
| 4881 | |slt|sgt|sle|sge|seq|sne|scmp|[sn]?bit_(?:and|x?or)|negate |
| 4882 | |i_negate|not|[sn]?complement|smartmatch|atan2|sin|cos |
| 4883 | |rand|srand|exp|log|sqrt|int|hex|oct|abs|length|substr |
| 4884 | |vec|index|rindex|sprintf|formline|ord|chr|crypt|ucfirst |
| 4885 | |lcfirst|uc|lc|quotemeta|aelemfast|aelem|exists|helem |
| 4886 | |pack|join|anonlist|anonhash|push|pop|shift|unshift|xor |
| 4887 | |andassign|orassign|dorassign|warn|die|reset|nextstate |
| 4888 | |dbstate|unstack|last|next|redo|dump|goto|exit|open|close |
| 4889 | |pipe_op|fileno|umask|binmode|tie|untie|tied|dbmopen |
| 4890 | |dbmclose|select|getc|read|enterwrite|prtf|print|say |
| 4891 | |sysopen|sysseek|sysread|syswrite|eof|tell|seek|truncate |
| 4892 | |fcntl|ioctl|flock|send|recv|socket|sockpair|bind|connect |
| 4893 | |listen|accept|shutdown|gsockopt|ssockopt|getsockname |
| 4894 | |getpeername|ftrread|ftrwrite|ftrexec|fteread|ftewrite |
| 4895 | |fteexec|ftis|ftsize|ftmtime|ftatime|ftctime|ftrowned |
| 4896 | |fteowned|ftzero|ftsock|ftchr|ftblk|ftfile|ftdir|ftpipe |
| 4897 | |ftsuid|ftsgid|ftsvtx|ftlink|fttty|fttext|ftbinary|chdir |
| 4898 | |chown|chroot|unlink|chmod|utime|rename|link|symlink |
| 4899 | |readlink|mkdir|rmdir|open_dir|telldir|seekdir|rewinddir |
| 4900 | |closedir|fork|wait|waitpid|system|exec|kill|getppid |
| 4901 | |getpgrp|setpgrp|getpriority|setpriority|time|alarm|sleep |
| 4902 | |shmget|shmctl|shmread|shmwrite|msgget|msgctl|msgsnd |
| 4903 | |msgrcv|semop|semget|semctl|hintseval|shostent|snetent |
| 4904 | |sprotoent|sservent|ehostent|enetent|eprotoent|eservent |
| 4905 | |spwent|epwent|sgrent|egrent|getlogin|syscall|lock|runcv |
| 4906 | |fc)\z/x |
| 4907 | } |
| 4908 | |
| 4909 | sub pp_entersub { |
| 4910 | my $self = shift; |
| 4911 | my($op, $cx) = @_; |
| 4912 | return $self->e_method($self->_method($op, $cx)) |
| 4913 | unless null $op->first->sibling; |
| 4914 | my $prefix = ""; |
| 4915 | my $amper = ""; |
| 4916 | my($kid, @exprs); |
| 4917 | if ($op->flags & OPf_SPECIAL && !($op->flags & OPf_MOD)) { |
| 4918 | $prefix = "do "; |
| 4919 | } elsif ($op->private & OPpENTERSUB_AMPER) { |
| 4920 | $amper = "&"; |
| 4921 | } |
| 4922 | $kid = $op->first; |
| 4923 | $kid = $kid->first->sibling; # skip ex-list, pushmark |
| 4924 | for (; not null $kid->sibling; $kid = $kid->sibling) { |
| 4925 | push @exprs, $kid; |
| 4926 | } |
| 4927 | my $simple = 0; |
| 4928 | my $proto = undef; |
| 4929 | my $lexical; |
| 4930 | if (is_scope($kid)) { |
| 4931 | $amper = "&"; |
| 4932 | $kid = "{" . $self->deparse($kid, 0) . "}"; |
| 4933 | } elsif ($kid->first->name eq "gv") { |
| 4934 | my $gv = $self->gv_or_padgv($kid->first); |
| 4935 | my $cv; |
| 4936 | if (class($gv) eq 'GV' && class($cv = $gv->CV) ne "SPECIAL" |
| 4937 | || $gv->FLAGS & SVf_ROK && class($cv = $gv->RV) eq 'CV') { |
| 4938 | $proto = $cv->PV if $cv->FLAGS & SVf_POK; |
| 4939 | } |
| 4940 | $simple = 1; # only calls of named functions can be prototyped |
| 4941 | $kid = $self->maybe_qualify("!", $self->gv_name($gv)); |
| 4942 | my $fq; |
| 4943 | # Fully qualify any sub name that conflicts with a lexical. |
| 4944 | if ($self->lex_in_scope("&$kid") |
| 4945 | || $self->lex_in_scope("&$kid", 1)) |
| 4946 | { |
| 4947 | $fq++; |
| 4948 | } elsif (!$amper) { |
| 4949 | if ($kid eq 'main::') { |
| 4950 | $kid = '::'; |
| 4951 | } |
| 4952 | else { |
| 4953 | if ($kid !~ /::/ && $kid ne 'x') { |
| 4954 | # Fully qualify any sub name that is also a keyword. While |
| 4955 | # we could check the import flag, we cannot guarantee that |
| 4956 | # the code deparsed so far would set that flag, so we qual- |
| 4957 | # ify the names regardless of importation. |
| 4958 | if (exists $feature_keywords{$kid}) { |
| 4959 | $fq++ if $self->feature_enabled($kid); |
| 4960 | } elsif (do { local $@; local $SIG{__DIE__}; |
| 4961 | eval { () = prototype "CORE::$kid"; 1 } }) { |
| 4962 | $fq++ |
| 4963 | } |
| 4964 | } |
| 4965 | if ($kid !~ /^(?:\w|::)(?:[\w\d]|::(?!\z))*\z/) { |
| 4966 | $kid = single_delim("q", "'", $kid, $self) . '->'; |
| 4967 | } |
| 4968 | } |
| 4969 | } |
| 4970 | $fq and substr $kid, 0, 0, = $self->{'curstash'}.'::'; |
| 4971 | } elsif (is_scalar ($kid->first) && $kid->first->name ne 'rv2cv') { |
| 4972 | $amper = "&"; |
| 4973 | $kid = $self->deparse($kid, 24); |
| 4974 | } else { |
| 4975 | $prefix = ""; |
| 4976 | my $grandkid = $kid->first; |
| 4977 | my $arrow = ($lexical = $grandkid->name eq "padcv") |
| 4978 | || is_subscriptable($grandkid) |
| 4979 | ? "" |
| 4980 | : "->"; |
| 4981 | $kid = $self->deparse($kid, 24) . $arrow; |
| 4982 | if ($lexical) { |
| 4983 | my $padlist = $self->{'curcv'}->PADLIST; |
| 4984 | my $padoff = $grandkid->targ; |
| 4985 | my $padname = $padlist->ARRAYelt(0)->ARRAYelt($padoff); |
| 4986 | my $protocv = $padname->FLAGS & SVpad_STATE |
| 4987 | ? $padlist->ARRAYelt(1)->ARRAYelt($padoff) |
| 4988 | : $padname->PROTOCV; |
| 4989 | if ($protocv->FLAGS & SVf_POK) { |
| 4990 | $proto = $protocv->PV |
| 4991 | } |
| 4992 | $simple = 1; |
| 4993 | } |
| 4994 | } |
| 4995 | |
| 4996 | # Doesn't matter how many prototypes there are, if |
| 4997 | # they haven't happened yet! |
| 4998 | my $declared = $lexical || exists $self->{'subs_declared'}{$kid}; |
| 4999 | if (not $declared and $self->{'in_coderef2text'}) { |
| 5000 | no strict 'refs'; |
| 5001 | no warnings 'uninitialized'; |
| 5002 | $declared = |
| 5003 | ( |
| 5004 | defined &{ ${$self->{'curstash'}."::"}{$kid} } |
| 5005 | && !exists |
| 5006 | $self->{'subs_deparsed'}{$self->{'curstash'}."::".$kid} |
| 5007 | && defined prototype $self->{'curstash'}."::".$kid |
| 5008 | ); |
| 5009 | } |
| 5010 | if (!$declared && defined($proto)) { |
| 5011 | # Avoid "too early to check prototype" warning |
| 5012 | ($amper, $proto) = ('&'); |
| 5013 | } |
| 5014 | |
| 5015 | my $args; |
| 5016 | my $listargs = 1; |
| 5017 | if ($declared and defined $proto and not $amper) { |
| 5018 | ($amper, $args) = $self->check_proto($proto, @exprs); |
| 5019 | $listargs = $amper; |
| 5020 | } |
| 5021 | if ($listargs) { |
| 5022 | $args = join(", ", map( |
| 5023 | ($_->flags & OPf_WANT) == OPf_WANT_SCALAR |
| 5024 | && !retscalar($_) |
| 5025 | ? $self->maybe_parens_unop('scalar', $_, 6) |
| 5026 | : $self->deparse($_, 6), |
| 5027 | @exprs |
| 5028 | )); |
| 5029 | } |
| 5030 | if ($prefix or $amper) { |
| 5031 | if ($kid eq '&') { $kid = "{$kid}" } # &{&} cannot be written as && |
| 5032 | if ($op->flags & OPf_STACKED) { |
| 5033 | return $prefix . $amper . $kid . "(" . $args . ")"; |
| 5034 | } else { |
| 5035 | return $prefix . $amper. $kid; |
| 5036 | } |
| 5037 | } else { |
| 5038 | # It's a syntax error to call CORE::GLOBAL::foo with a prefix, |
| 5039 | # so it must have been translated from a keyword call. Translate |
| 5040 | # it back. |
| 5041 | $kid =~ s/^CORE::GLOBAL:://; |
| 5042 | |
| 5043 | my $dproto = defined($proto) ? $proto : "undefined"; |
| 5044 | my $scalar_proto = $dproto =~ /^;*(?:[\$*_+]|\\.|\\\[[^]]\])\z/; |
| 5045 | if (!$declared) { |
| 5046 | return "$kid(" . $args . ")"; |
| 5047 | } elsif ($dproto =~ /^\s*\z/) { |
| 5048 | return $kid; |
| 5049 | } elsif ($scalar_proto and is_scalar($exprs[0])) { |
| 5050 | # is_scalar is an excessively conservative test here: |
| 5051 | # really, we should be comparing to the precedence of the |
| 5052 | # top operator of $exprs[0] (ala unop()), but that would |
| 5053 | # take some major code restructuring to do right. |
| 5054 | return $self->maybe_parens_func($kid, $args, $cx, 16); |
| 5055 | } elsif (not $scalar_proto and defined($proto) || $simple) { #' |
| 5056 | return $self->maybe_parens_func($kid, $args, $cx, 5); |
| 5057 | } else { |
| 5058 | return "$kid(" . $args . ")"; |
| 5059 | } |
| 5060 | } |
| 5061 | } |
| 5062 | |
| 5063 | sub pp_enterwrite { unop(@_, "write") } |
| 5064 | |
| 5065 | # escape things that cause interpolation in double quotes, |
| 5066 | # but not character escapes |
| 5067 | sub uninterp { |
| 5068 | my($str) = @_; |
| 5069 | $str =~ s/(^|\G|[^\\])((?:\\\\)*)([\$\@]|\\[uUlLQE])/$1$2\\$3/g; |
| 5070 | return $str; |
| 5071 | } |
| 5072 | |
| 5073 | { |
| 5074 | my $bal; |
| 5075 | BEGIN { |
| 5076 | use re "eval"; |
| 5077 | # Matches any string which is balanced with respect to {braces} |
| 5078 | $bal = qr( |
| 5079 | (?: |
| 5080 | [^\\{}] |
| 5081 | | \\\\ |
| 5082 | | \\[{}] |
| 5083 | | \{(??{$bal})\} |
| 5084 | )* |
| 5085 | )x; |
| 5086 | } |
| 5087 | |
| 5088 | # the same, but treat $|, $), $( and $ at the end of the string differently |
| 5089 | # and leave comments unmangled for the sake of /x and (?x). |
| 5090 | sub re_uninterp { |
| 5091 | my($str) = @_; |
| 5092 | |
| 5093 | $str =~ s/ |
| 5094 | ( ^|\G # $1 |
| 5095 | | [^\\] |
| 5096 | ) |
| 5097 | |
| 5098 | ( # $2 |
| 5099 | (?:\\\\)* |
| 5100 | ) |
| 5101 | |
| 5102 | ( # $3 |
| 5103 | ( \(\?\??\{$bal\}\) # $4 (skip over (?{}) and (??{}) blocks) |
| 5104 | | \#[^\n]* # (skip over comments) |
| 5105 | ) |
| 5106 | | [\$\@] |
| 5107 | (?!\||\)|\(|$|\s) |
| 5108 | | \\[uUlLQE] |
| 5109 | ) |
| 5110 | |
| 5111 | /defined($4) && length($4) ? "$1$2$4" : "$1$2\\$3"/xeg; |
| 5112 | |
| 5113 | return $str; |
| 5114 | } |
| 5115 | } |
| 5116 | |
| 5117 | # character escapes, but not delimiters that might need to be escaped |
| 5118 | sub escape_str { # ASCII, UTF8 |
| 5119 | my($str) = @_; |
| 5120 | $str =~ s/(.)/ord($1) > 255 ? sprintf("\\x{%x}", ord($1)) : $1/eg; |
| 5121 | $str =~ s/\a/\\a/g; |
| 5122 | # $str =~ s/\cH/\\b/g; # \b means something different in a regex; and \cH |
| 5123 | # isn't a backspace in EBCDIC |
| 5124 | $str =~ s/\t/\\t/g; |
| 5125 | $str =~ s/\n/\\n/g; |
| 5126 | $str =~ s/\e/\\e/g; |
| 5127 | $str =~ s/\f/\\f/g; |
| 5128 | $str =~ s/\r/\\r/g; |
| 5129 | $str =~ s/([\cA-\cZ])/'\\c' . $unctrl{$1}/ge; |
| 5130 | $str =~ s/([[:^print:]])/sprintf("\\%03o", ord($1))/age; |
| 5131 | return $str; |
| 5132 | } |
| 5133 | |
| 5134 | # For regexes. Leave whitespace unmangled in case of /x or (?x). |
| 5135 | sub escape_re { |
| 5136 | my($str) = @_; |
| 5137 | $str =~ s/(.)/ord($1) > 255 ? sprintf("\\x{%x}", ord($1)) : $1/eg; |
| 5138 | $str =~ s/([[:^print:]])/ |
| 5139 | ($1 =~ y! \t\n!!) ? $1 : sprintf("\\%03o", ord($1))/age; |
| 5140 | $str =~ s/\n/\n\f/g; |
| 5141 | return $str; |
| 5142 | } |
| 5143 | |
| 5144 | # Don't do this for regexen |
| 5145 | sub unback { |
| 5146 | my($str) = @_; |
| 5147 | $str =~ s/\\/\\\\/g; |
| 5148 | return $str; |
| 5149 | } |
| 5150 | |
| 5151 | # Remove backslashes which precede literal control characters, |
| 5152 | # to avoid creating ambiguity when we escape the latter. |
| 5153 | # |
| 5154 | # Don't remove a backslash from escaped whitespace: where the T represents |
| 5155 | # a literal tab character, /T/x is not equivalent to /\T/x |
| 5156 | |
| 5157 | sub re_unback { |
| 5158 | my($str) = @_; |
| 5159 | |
| 5160 | # the insane complexity here is due to the behaviour of "\c\" |
| 5161 | $str =~ s/ |
| 5162 | # these two lines ensure that the backslash we're about to |
| 5163 | # remove isn't preceeded by something which makes it part |
| 5164 | # of a \c |
| 5165 | |
| 5166 | (^ | [^\\] | \\c\\) # $1 |
| 5167 | (?<!\\c) |
| 5168 | |
| 5169 | # the backslash to remove |
| 5170 | \\ |
| 5171 | |
| 5172 | # keep pairs of backslashes |
| 5173 | (\\\\)* # $2 |
| 5174 | |
| 5175 | # only remove if the thing following is a control char |
| 5176 | (?=[[:^print:]]) |
| 5177 | # and not whitespace |
| 5178 | (?=\S) |
| 5179 | /$1$2/xg; |
| 5180 | return $str; |
| 5181 | } |
| 5182 | |
| 5183 | sub balanced_delim { |
| 5184 | my($str) = @_; |
| 5185 | my @str = split //, $str; |
| 5186 | my($ar, $open, $close, $fail, $c, $cnt, $last_bs); |
| 5187 | for $ar (['[',']'], ['(',')'], ['<','>'], ['{','}']) { |
| 5188 | ($open, $close) = @$ar; |
| 5189 | $fail = 0; $cnt = 0; $last_bs = 0; |
| 5190 | for $c (@str) { |
| 5191 | if ($c eq $open) { |
| 5192 | $fail = 1 if $last_bs; |
| 5193 | $cnt++; |
| 5194 | } elsif ($c eq $close) { |
| 5195 | $fail = 1 if $last_bs; |
| 5196 | $cnt--; |
| 5197 | if ($cnt < 0) { |
| 5198 | # qq()() isn't ")(" |
| 5199 | $fail = 1; |
| 5200 | last; |
| 5201 | } |
| 5202 | } |
| 5203 | $last_bs = $c eq '\\'; |
| 5204 | } |
| 5205 | $fail = 1 if $cnt != 0; |
| 5206 | return ($open, "$open$str$close") if not $fail; |
| 5207 | } |
| 5208 | return ("", $str); |
| 5209 | } |
| 5210 | |
| 5211 | sub single_delim { |
| 5212 | my($q, $default, $str, $self) = @_; |
| 5213 | return "$default$str$default" if $default and index($str, $default) == -1; |
| 5214 | my $coreq = $self->keyword($q); # maybe CORE::q |
| 5215 | if ($q ne 'qr') { |
| 5216 | (my $succeed, $str) = balanced_delim($str); |
| 5217 | return "$coreq$str" if $succeed; |
| 5218 | } |
| 5219 | for my $delim ('/', '"', '#') { |
| 5220 | return "$coreq$delim" . $str . $delim if index($str, $delim) == -1; |
| 5221 | } |
| 5222 | if ($default) { |
| 5223 | $str =~ s/$default/\\$default/g; |
| 5224 | return "$default$str$default"; |
| 5225 | } else { |
| 5226 | $str =~ s[/][\\/]g; |
| 5227 | return "$coreq/$str/"; |
| 5228 | } |
| 5229 | } |
| 5230 | |
| 5231 | my $max_prec; |
| 5232 | BEGIN { $max_prec = int(0.999 + 8*length(pack("F", 42))*log(2)/log(10)); } |
| 5233 | |
| 5234 | # Split a floating point number into an integer mantissa and a binary |
| 5235 | # exponent. Assumes you've already made sure the number isn't zero or |
| 5236 | # some weird infinity or NaN. |
| 5237 | sub split_float { |
| 5238 | my($f) = @_; |
| 5239 | my $exponent = 0; |
| 5240 | if ($f == int($f)) { |
| 5241 | while ($f % 2 == 0) { |
| 5242 | $f /= 2; |
| 5243 | $exponent++; |
| 5244 | } |
| 5245 | } else { |
| 5246 | while ($f != int($f)) { |
| 5247 | $f *= 2; |
| 5248 | $exponent--; |
| 5249 | } |
| 5250 | } |
| 5251 | my $mantissa = sprintf("%.0f", $f); |
| 5252 | return ($mantissa, $exponent); |
| 5253 | } |
| 5254 | |
| 5255 | |
| 5256 | # suitably single- or double-quote a literal constant string |
| 5257 | |
| 5258 | sub quoted_const_str { |
| 5259 | my ($self, $str) =@_; |
| 5260 | if ($str =~ /[[:^print:]]/a) { |
| 5261 | return single_delim("qq", '"', |
| 5262 | uninterp(escape_str unback $str), $self); |
| 5263 | } else { |
| 5264 | return single_delim("q", "'", unback($str), $self); |
| 5265 | } |
| 5266 | } |
| 5267 | |
| 5268 | |
| 5269 | sub const { |
| 5270 | my $self = shift; |
| 5271 | my($sv, $cx) = @_; |
| 5272 | if ($self->{'use_dumper'}) { |
| 5273 | return $self->const_dumper($sv, $cx); |
| 5274 | } |
| 5275 | if (class($sv) eq "SPECIAL") { |
| 5276 | # sv_undef, sv_yes, sv_no |
| 5277 | return $$sv == 3 ? $self->maybe_parens("!1", $cx, 21) |
| 5278 | : ('undef', '1')[$$sv-1]; |
| 5279 | } |
| 5280 | if (class($sv) eq "NULL") { |
| 5281 | return 'undef'; |
| 5282 | } |
| 5283 | # convert a version object into the "v1.2.3" string in its V magic |
| 5284 | if ($sv->FLAGS & SVs_RMG) { |
| 5285 | for (my $mg = $sv->MAGIC; $mg; $mg = $mg->MOREMAGIC) { |
| 5286 | return $mg->PTR if $mg->TYPE eq 'V'; |
| 5287 | } |
| 5288 | } |
| 5289 | |
| 5290 | if ($sv->FLAGS & SVf_IOK) { |
| 5291 | my $str = $sv->int_value; |
| 5292 | $str = $self->maybe_parens($str, $cx, 21) if $str < 0; |
| 5293 | return $str; |
| 5294 | } elsif ($sv->FLAGS & SVf_NOK) { |
| 5295 | my $nv = $sv->NV; |
| 5296 | if ($nv == 0) { |
| 5297 | if (pack("F", $nv) eq pack("F", 0)) { |
| 5298 | # positive zero |
| 5299 | return "0"; |
| 5300 | } else { |
| 5301 | # negative zero |
| 5302 | return $self->maybe_parens("-.0", $cx, 21); |
| 5303 | } |
| 5304 | } elsif (1/$nv == 0) { |
| 5305 | if ($nv > 0) { |
| 5306 | # positive infinity |
| 5307 | return $self->maybe_parens("9**9**9", $cx, 22); |
| 5308 | } else { |
| 5309 | # negative infinity |
| 5310 | return $self->maybe_parens("-9**9**9", $cx, 21); |
| 5311 | } |
| 5312 | } elsif ($nv != $nv) { |
| 5313 | # NaN |
| 5314 | if (pack("F", $nv) eq pack("F", sin(9**9**9))) { |
| 5315 | # the normal kind |
| 5316 | return "sin(9**9**9)"; |
| 5317 | } elsif (pack("F", $nv) eq pack("F", -sin(9**9**9))) { |
| 5318 | # the inverted kind |
| 5319 | return $self->maybe_parens("-sin(9**9**9)", $cx, 21); |
| 5320 | } else { |
| 5321 | # some other kind |
| 5322 | my $hex = unpack("h*", pack("F", $nv)); |
| 5323 | return qq'unpack("F", pack("h*", "$hex"))'; |
| 5324 | } |
| 5325 | } |
| 5326 | # first, try the default stringification |
| 5327 | my $str = "$nv"; |
| 5328 | if ($str != $nv) { |
| 5329 | # failing that, try using more precision |
| 5330 | $str = sprintf("%.${max_prec}g", $nv); |
| 5331 | # if (pack("F", $str) ne pack("F", $nv)) { |
| 5332 | if ($str != $nv) { |
| 5333 | # not representable in decimal with whatever sprintf() |
| 5334 | # and atof() Perl is using here. |
| 5335 | my($mant, $exp) = split_float($nv); |
| 5336 | return $self->maybe_parens("$mant * 2**$exp", $cx, 19); |
| 5337 | } |
| 5338 | } |
| 5339 | $str = $self->maybe_parens($str, $cx, 21) if $nv < 0; |
| 5340 | return $str; |
| 5341 | } elsif ($sv->FLAGS & SVf_ROK && $sv->can("RV")) { |
| 5342 | my $ref = $sv->RV; |
| 5343 | my $class = class($ref); |
| 5344 | if ($class eq "AV") { |
| 5345 | return "[" . $self->list_const(2, $ref->ARRAY) . "]"; |
| 5346 | } elsif ($class eq "HV") { |
| 5347 | my %hash = $ref->ARRAY; |
| 5348 | my @elts; |
| 5349 | for my $k (sort keys %hash) { |
| 5350 | push @elts, "$k => " . $self->const($hash{$k}, 6); |
| 5351 | } |
| 5352 | return "{" . join(", ", @elts) . "}"; |
| 5353 | } elsif ($class eq "CV") { |
| 5354 | no overloading; |
| 5355 | if ($self->{curcv} && |
| 5356 | $self->{curcv}->object_2svref == $ref->object_2svref) { |
| 5357 | return $self->keyword("__SUB__"); |
| 5358 | } |
| 5359 | return "sub " . $self->deparse_sub($ref); |
| 5360 | } |
| 5361 | if ($class ne 'SPECIAL' and $ref->FLAGS & SVs_SMG) { |
| 5362 | for (my $mg = $ref->MAGIC; $mg; $mg = $mg->MOREMAGIC) { |
| 5363 | if ($mg->TYPE eq 'r') { |
| 5364 | my $re = re_uninterp(escape_re(re_unback($mg->precomp))); |
| 5365 | return single_delim("qr", "", $re, $self); |
| 5366 | } |
| 5367 | } |
| 5368 | } |
| 5369 | |
| 5370 | my $const = $self->const($ref, 20); |
| 5371 | if ($self->{in_subst_repl} && $const =~ /^[0-9]/) { |
| 5372 | $const = "($const)"; |
| 5373 | } |
| 5374 | return $self->maybe_parens("\\$const", $cx, 20); |
| 5375 | } elsif ($sv->FLAGS & SVf_POK) { |
| 5376 | my $str = $sv->PV; |
| 5377 | return $self->quoted_const_str($str); |
| 5378 | } else { |
| 5379 | return "undef"; |
| 5380 | } |
| 5381 | } |
| 5382 | |
| 5383 | sub const_dumper { |
| 5384 | my $self = shift; |
| 5385 | my($sv, $cx) = @_; |
| 5386 | my $ref = $sv->object_2svref(); |
| 5387 | my $dumper = Data::Dumper->new([$$ref], ['$v']); |
| 5388 | $dumper->Purity(1)->Terse(1)->Deparse(1)->Indent(0)->Useqq(1)->Sortkeys(1); |
| 5389 | my $str = $dumper->Dump(); |
| 5390 | if ($str =~ /^\$v/) { |
| 5391 | return '${my ' . $str . ' \$v}'; |
| 5392 | } else { |
| 5393 | return $str; |
| 5394 | } |
| 5395 | } |
| 5396 | |
| 5397 | sub const_sv { |
| 5398 | my $self = shift; |
| 5399 | my $op = shift; |
| 5400 | my $sv = $op->sv; |
| 5401 | # the constant could be in the pad (under useithreads) |
| 5402 | $sv = $self->padval($op->targ) unless $$sv; |
| 5403 | return $sv; |
| 5404 | } |
| 5405 | |
| 5406 | sub meth_sv { |
| 5407 | my $self = shift; |
| 5408 | my $op = shift; |
| 5409 | my $sv = $op->meth_sv; |
| 5410 | # the constant could be in the pad (under useithreads) |
| 5411 | $sv = $self->padval($op->targ) unless $$sv; |
| 5412 | return $sv; |
| 5413 | } |
| 5414 | |
| 5415 | sub meth_rclass_sv { |
| 5416 | my $self = shift; |
| 5417 | my $op = shift; |
| 5418 | my $sv = $op->rclass; |
| 5419 | # the constant could be in the pad (under useithreads) |
| 5420 | $sv = $self->padval($sv) unless ref $sv; |
| 5421 | return $sv; |
| 5422 | } |
| 5423 | |
| 5424 | sub pp_const { |
| 5425 | my $self = shift; |
| 5426 | my($op, $cx) = @_; |
| 5427 | if ($op->private & OPpCONST_ARYBASE) { |
| 5428 | return '$['; |
| 5429 | } |
| 5430 | # if ($op->private & OPpCONST_BARE) { # trouble with '=>' autoquoting |
| 5431 | # return $self->const_sv($op)->PV; |
| 5432 | # } |
| 5433 | my $sv = $self->const_sv($op); |
| 5434 | return $self->const($sv, $cx); |
| 5435 | } |
| 5436 | |
| 5437 | |
| 5438 | # Join two components of a double-quoted string, disambiguating |
| 5439 | # "${foo}bar", "${foo}{bar}", "${foo}[1]", "$foo\::bar" |
| 5440 | |
| 5441 | sub dq_disambiguate { |
| 5442 | my ($first, $last) = @_; |
| 5443 | ($last =~ /^[A-Z\\\^\[\]_?]/ && |
| 5444 | $first =~ s/([\$@])\^$/${1}{^}/) # "${^}W" etc |
| 5445 | || ($last =~ /^[:'{\[\w_]/ && #' |
| 5446 | $first =~ s/([\$@])([A-Za-z_]\w*)$/${1}{$2}/); |
| 5447 | return $first . $last; |
| 5448 | } |
| 5449 | |
| 5450 | |
| 5451 | # Deparse a double-quoted optree. For example, "$a[0]\Q$b\Efo\"o" gets |
| 5452 | # compiled to concat(concat($[0],quotemeta($b)),const("fo\"o")), and this |
| 5453 | # sub deparses it back to $a[0]\Q$b\Efo"o |
| 5454 | # (It does not add delimiters) |
| 5455 | |
| 5456 | sub dq { |
| 5457 | my $self = shift; |
| 5458 | my $op = shift; |
| 5459 | my $type = $op->name; |
| 5460 | if ($type eq "const") { |
| 5461 | return '$[' if $op->private & OPpCONST_ARYBASE; |
| 5462 | return uninterp(escape_str(unback($self->const_sv($op)->as_string))); |
| 5463 | } elsif ($type eq "concat") { |
| 5464 | return dq_disambiguate($self->dq($op->first), $self->dq($op->last)); |
| 5465 | } elsif ($type eq "multiconcat") { |
| 5466 | return $self->do_multiconcat($op, 26, 1); |
| 5467 | } elsif ($type eq "uc") { |
| 5468 | return '\U' . $self->dq($op->first->sibling) . '\E'; |
| 5469 | } elsif ($type eq "lc") { |
| 5470 | return '\L' . $self->dq($op->first->sibling) . '\E'; |
| 5471 | } elsif ($type eq "ucfirst") { |
| 5472 | return '\u' . $self->dq($op->first->sibling); |
| 5473 | } elsif ($type eq "lcfirst") { |
| 5474 | return '\l' . $self->dq($op->first->sibling); |
| 5475 | } elsif ($type eq "quotemeta") { |
| 5476 | return '\Q' . $self->dq($op->first->sibling) . '\E'; |
| 5477 | } elsif ($type eq "fc") { |
| 5478 | return '\F' . $self->dq($op->first->sibling) . '\E'; |
| 5479 | } elsif ($type eq "join") { |
| 5480 | return $self->deparse($op->last, 26); # was join($", @ary) |
| 5481 | } else { |
| 5482 | return $self->deparse($op, 26); |
| 5483 | } |
| 5484 | } |
| 5485 | |
| 5486 | sub pp_backtick { |
| 5487 | my $self = shift; |
| 5488 | my($op, $cx) = @_; |
| 5489 | # skip pushmark if it exists (readpipe() vs ``) |
| 5490 | my $child = $op->first->sibling->isa('B::NULL') |
| 5491 | ? $op->first : $op->first->sibling; |
| 5492 | if ($self->pure_string($child)) { |
| 5493 | return single_delim("qx", '`', $self->dq($child, 1), $self); |
| 5494 | } |
| 5495 | unop($self, @_, "readpipe"); |
| 5496 | } |
| 5497 | |
| 5498 | sub dquote { |
| 5499 | my $self = shift; |
| 5500 | my($op, $cx) = @_; |
| 5501 | my $kid = $op->first->sibling; # skip ex-stringify, pushmark |
| 5502 | return $self->deparse($kid, $cx) if $self->{'unquote'}; |
| 5503 | $self->maybe_targmy($kid, $cx, |
| 5504 | sub {single_delim("qq", '"', $self->dq($_[1]), |
| 5505 | $self)}); |
| 5506 | } |
| 5507 | |
| 5508 | # OP_STRINGIFY is a listop, but it only ever has one arg |
| 5509 | sub pp_stringify { |
| 5510 | my ($self, $op, $cx) = @_; |
| 5511 | my $kid = $op->first->sibling; |
| 5512 | while ($kid->name eq 'null' && !null($kid->first)) { |
| 5513 | $kid = $kid->first; |
| 5514 | } |
| 5515 | if ($kid->name =~ /^(?:const|padsv|rv2sv|av2arylen|gvsv|multideref |
| 5516 | |aelemfast(?:_lex)?|[ah]elem|join|concat)\z/x) { |
| 5517 | maybe_targmy(@_, \&dquote); |
| 5518 | } |
| 5519 | else { |
| 5520 | # Actually an optimised join. |
| 5521 | my $result = listop(@_,"join"); |
| 5522 | $result =~ s/join([( ])/join$1$self->{'ex_const'}, /; |
| 5523 | $result; |
| 5524 | } |
| 5525 | } |
| 5526 | |
| 5527 | # tr/// and s/// (and tr[][], tr[]//, tr###, etc) |
| 5528 | # note that tr(from)/to/ is OK, but not tr/from/(to) |
| 5529 | sub double_delim { |
| 5530 | my($from, $to) = @_; |
| 5531 | my($succeed, $delim); |
| 5532 | if ($from !~ m[/] and $to !~ m[/]) { |
| 5533 | return "/$from/$to/"; |
| 5534 | } elsif (($succeed, $from) = balanced_delim($from) and $succeed) { |
| 5535 | if (($succeed, $to) = balanced_delim($to) and $succeed) { |
| 5536 | return "$from$to"; |
| 5537 | } else { |
| 5538 | for $delim ('/', '"', '#') { # note no "'" -- s''' is special |
| 5539 | return "$from$delim$to$delim" if index($to, $delim) == -1; |
| 5540 | } |
| 5541 | $to =~ s[/][\\/]g; |
| 5542 | return "$from/$to/"; |
| 5543 | } |
| 5544 | } else { |
| 5545 | for $delim ('/', '"', '#') { # note no ' |
| 5546 | return "$delim$from$delim$to$delim" |
| 5547 | if index($to . $from, $delim) == -1; |
| 5548 | } |
| 5549 | $from =~ s[/][\\/]g; |
| 5550 | $to =~ s[/][\\/]g; |
| 5551 | return "/$from/$to/"; |
| 5552 | } |
| 5553 | } |
| 5554 | |
| 5555 | # Only used by tr///, so backslashes hyphens |
| 5556 | sub pchr { # ASCII |
| 5557 | my($n) = @_; |
| 5558 | if ($n == ord '\\') { |
| 5559 | return '\\\\'; |
| 5560 | } elsif ($n == ord "-") { |
| 5561 | return "\\-"; |
| 5562 | } elsif (utf8::native_to_unicode($n) >= utf8::native_to_unicode(ord(' ')) |
| 5563 | and utf8::native_to_unicode($n) <= utf8::native_to_unicode(ord('~'))) |
| 5564 | { |
| 5565 | # I'm presuming a regex is not ok here, otherwise we could have used |
| 5566 | # /[[:print:]]/a to get here |
| 5567 | return chr($n); |
| 5568 | } elsif ($n == ord "\a") { |
| 5569 | return '\\a'; |
| 5570 | } elsif ($n == ord "\b") { |
| 5571 | return '\\b'; |
| 5572 | } elsif ($n == ord "\t") { |
| 5573 | return '\\t'; |
| 5574 | } elsif ($n == ord "\n") { |
| 5575 | return '\\n'; |
| 5576 | } elsif ($n == ord "\e") { |
| 5577 | return '\\e'; |
| 5578 | } elsif ($n == ord "\f") { |
| 5579 | return '\\f'; |
| 5580 | } elsif ($n == ord "\r") { |
| 5581 | return '\\r'; |
| 5582 | } elsif ($n >= ord("\cA") and $n <= ord("\cZ")) { |
| 5583 | return '\\c' . $unctrl{chr $n}; |
| 5584 | } else { |
| 5585 | # return '\x' . sprintf("%02x", $n); |
| 5586 | return '\\' . sprintf("%03o", $n); |
| 5587 | } |
| 5588 | } |
| 5589 | |
| 5590 | sub collapse { |
| 5591 | my(@chars) = @_; |
| 5592 | my($str, $c, $tr) = (""); |
| 5593 | for ($c = 0; $c < @chars; $c++) { |
| 5594 | $tr = $chars[$c]; |
| 5595 | $str .= pchr($tr); |
| 5596 | if ($c <= $#chars - 2 and $chars[$c + 1] == $tr + 1 and |
| 5597 | $chars[$c + 2] == $tr + 2) |
| 5598 | { |
| 5599 | for (; $c <= $#chars-1 and $chars[$c + 1] == $chars[$c] + 1; $c++) |
| 5600 | {} |
| 5601 | $str .= "-"; |
| 5602 | $str .= pchr($chars[$c]); |
| 5603 | } |
| 5604 | } |
| 5605 | return $str; |
| 5606 | } |
| 5607 | |
| 5608 | sub tr_decode_byte { |
| 5609 | my($table, $flags) = @_; |
| 5610 | my(@table) = unpack("s*", $table); |
| 5611 | splice @table, 0x100, 1; # Number of subsequent elements |
| 5612 | my($c, $tr, @from, @to, @delfrom, $delhyphen); |
| 5613 | if ($table[ord "-"] != -1 and |
| 5614 | $table[ord("-") - 1] == -1 || $table[ord("-") + 1] == -1) |
| 5615 | { |
| 5616 | $tr = $table[ord "-"]; |
| 5617 | $table[ord "-"] = -1; |
| 5618 | if ($tr >= 0) { |
| 5619 | @from = ord("-"); |
| 5620 | @to = $tr; |
| 5621 | } else { # -2 ==> delete |
| 5622 | $delhyphen = 1; |
| 5623 | } |
| 5624 | } |
| 5625 | for ($c = 0; $c < @table; $c++) { |
| 5626 | $tr = $table[$c]; |
| 5627 | if ($tr >= 0) { |
| 5628 | push @from, $c; push @to, $tr; |
| 5629 | } elsif ($tr == -2) { |
| 5630 | push @delfrom, $c; |
| 5631 | } |
| 5632 | } |
| 5633 | @from = (@from, @delfrom); |
| 5634 | if ($flags & OPpTRANS_COMPLEMENT) { |
| 5635 | my @newfrom = (); |
| 5636 | my %from; |
| 5637 | @from{@from} = (1) x @from; |
| 5638 | for ($c = 0; $c < 256; $c++) { |
| 5639 | push @newfrom, $c unless $from{$c}; |
| 5640 | } |
| 5641 | @from = @newfrom; |
| 5642 | } |
| 5643 | unless ($flags & OPpTRANS_DELETE || !@to) { |
| 5644 | pop @to while $#to and $to[$#to] == $to[$#to -1]; |
| 5645 | } |
| 5646 | my($from, $to); |
| 5647 | $from = collapse(@from); |
| 5648 | $to = collapse(@to); |
| 5649 | $from .= "-" if $delhyphen; |
| 5650 | return ($from, $to); |
| 5651 | } |
| 5652 | |
| 5653 | sub tr_chr { |
| 5654 | my $x = shift; |
| 5655 | if ($x == ord "-") { |
| 5656 | return "\\-"; |
| 5657 | } elsif ($x == ord "\\") { |
| 5658 | return "\\\\"; |
| 5659 | } else { |
| 5660 | return chr $x; |
| 5661 | } |
| 5662 | } |
| 5663 | |
| 5664 | # XXX This doesn't yet handle all cases correctly either |
| 5665 | |
| 5666 | sub tr_decode_utf8 { |
| 5667 | my($swash_hv, $flags) = @_; |
| 5668 | my %swash = $swash_hv->ARRAY; |
| 5669 | my $final = undef; |
| 5670 | $final = $swash{'FINAL'}->IV if exists $swash{'FINAL'}; |
| 5671 | my $none = $swash{"NONE"}->IV; |
| 5672 | my $extra = $none + 1; |
| 5673 | my(@from, @delfrom, @to); |
| 5674 | my $line; |
| 5675 | foreach $line (split /\n/, $swash{'LIST'}->PV) { |
| 5676 | my($min, $max, $result) = split(/\t/, $line); |
| 5677 | $min = hex $min; |
| 5678 | if (length $max) { |
| 5679 | $max = hex $max; |
| 5680 | } else { |
| 5681 | $max = $min; |
| 5682 | } |
| 5683 | $result = hex $result; |
| 5684 | if ($result == $extra) { |
| 5685 | push @delfrom, [$min, $max]; |
| 5686 | } else { |
| 5687 | push @from, [$min, $max]; |
| 5688 | push @to, [$result, $result + $max - $min]; |
| 5689 | } |
| 5690 | } |
| 5691 | for my $i (0 .. $#from) { |
| 5692 | if ($from[$i][0] == ord '-') { |
| 5693 | unshift @from, splice(@from, $i, 1); |
| 5694 | unshift @to, splice(@to, $i, 1); |
| 5695 | last; |
| 5696 | } elsif ($from[$i][1] == ord '-') { |
| 5697 | $from[$i][1]--; |
| 5698 | $to[$i][1]--; |
| 5699 | unshift @from, ord '-'; |
| 5700 | unshift @to, ord '-'; |
| 5701 | last; |
| 5702 | } |
| 5703 | } |
| 5704 | for my $i (0 .. $#delfrom) { |
| 5705 | if ($delfrom[$i][0] == ord '-') { |
| 5706 | push @delfrom, splice(@delfrom, $i, 1); |
| 5707 | last; |
| 5708 | } elsif ($delfrom[$i][1] == ord '-') { |
| 5709 | $delfrom[$i][1]--; |
| 5710 | push @delfrom, ord '-'; |
| 5711 | last; |
| 5712 | } |
| 5713 | } |
| 5714 | if (defined $final and $to[$#to][1] != $final) { |
| 5715 | push @to, [$final, $final]; |
| 5716 | } |
| 5717 | push @from, @delfrom; |
| 5718 | if ($flags & OPpTRANS_COMPLEMENT) { |
| 5719 | my @newfrom; |
| 5720 | my $next = 0; |
| 5721 | for my $i (0 .. $#from) { |
| 5722 | push @newfrom, [$next, $from[$i][0] - 1]; |
| 5723 | $next = $from[$i][1] + 1; |
| 5724 | } |
| 5725 | @from = (); |
| 5726 | for my $range (@newfrom) { |
| 5727 | if ($range->[0] <= $range->[1]) { |
| 5728 | push @from, $range; |
| 5729 | } |
| 5730 | } |
| 5731 | } |
| 5732 | my($from, $to, $diff); |
| 5733 | for my $chunk (@from) { |
| 5734 | $diff = $chunk->[1] - $chunk->[0]; |
| 5735 | if ($diff > 1) { |
| 5736 | $from .= tr_chr($chunk->[0]) . "-" . tr_chr($chunk->[1]); |
| 5737 | } elsif ($diff == 1) { |
| 5738 | $from .= tr_chr($chunk->[0]) . tr_chr($chunk->[1]); |
| 5739 | } else { |
| 5740 | $from .= tr_chr($chunk->[0]); |
| 5741 | } |
| 5742 | } |
| 5743 | for my $chunk (@to) { |
| 5744 | $diff = $chunk->[1] - $chunk->[0]; |
| 5745 | if ($diff > 1) { |
| 5746 | $to .= tr_chr($chunk->[0]) . "-" . tr_chr($chunk->[1]); |
| 5747 | } elsif ($diff == 1) { |
| 5748 | $to .= tr_chr($chunk->[0]) . tr_chr($chunk->[1]); |
| 5749 | } else { |
| 5750 | $to .= tr_chr($chunk->[0]); |
| 5751 | } |
| 5752 | } |
| 5753 | #$final = sprintf("%04x", $final) if defined $final; |
| 5754 | #$none = sprintf("%04x", $none) if defined $none; |
| 5755 | #$extra = sprintf("%04x", $extra) if defined $extra; |
| 5756 | #print STDERR "final: $final\n none: $none\nextra: $extra\n"; |
| 5757 | #print STDERR $swash{'LIST'}->PV; |
| 5758 | return (escape_str($from), escape_str($to)); |
| 5759 | } |
| 5760 | |
| 5761 | sub pp_trans { |
| 5762 | my $self = shift; |
| 5763 | my($op, $cx, $morflags) = @_; |
| 5764 | my($from, $to); |
| 5765 | my $class = class($op); |
| 5766 | my $priv_flags = $op->private; |
| 5767 | if ($class eq "PVOP") { |
| 5768 | ($from, $to) = tr_decode_byte($op->pv, $priv_flags); |
| 5769 | } elsif ($class eq "PADOP") { |
| 5770 | ($from, $to) |
| 5771 | = tr_decode_utf8($self->padval($op->padix)->RV, $priv_flags); |
| 5772 | } else { # class($op) eq "SVOP" |
| 5773 | ($from, $to) = tr_decode_utf8($op->sv->RV, $priv_flags); |
| 5774 | } |
| 5775 | my $flags = ""; |
| 5776 | $flags .= "c" if $priv_flags & OPpTRANS_COMPLEMENT; |
| 5777 | $flags .= "d" if $priv_flags & OPpTRANS_DELETE; |
| 5778 | $to = "" if $from eq $to and $flags eq ""; |
| 5779 | $flags .= "s" if $priv_flags & OPpTRANS_SQUASH; |
| 5780 | $flags .= $morflags if defined $morflags; |
| 5781 | my $ret = $self->keyword("tr") . double_delim($from, $to) . $flags; |
| 5782 | if (my $targ = $op->targ) { |
| 5783 | return $self->maybe_parens($self->padname($targ) . " =~ $ret", |
| 5784 | $cx, 20); |
| 5785 | } |
| 5786 | return $ret; |
| 5787 | } |
| 5788 | |
| 5789 | sub pp_transr { push @_, 'r'; goto &pp_trans } |
| 5790 | |
| 5791 | # Join two components of a double-quoted re, disambiguating |
| 5792 | # "${foo}bar", "${foo}{bar}", "${foo}[1]". |
| 5793 | |
| 5794 | sub re_dq_disambiguate { |
| 5795 | my ($first, $last) = @_; |
| 5796 | ($last =~ /^[A-Z\\\^\[\]_?]/ && |
| 5797 | $first =~ s/([\$@])\^$/${1}{^}/) # "${^}W" etc |
| 5798 | || ($last =~ /^[{\[\w_]/ && |
| 5799 | $first =~ s/([\$@])([A-Za-z_]\w*)$/${1}{$2}/); |
| 5800 | return $first . $last; |
| 5801 | } |
| 5802 | |
| 5803 | # Like dq(), but different |
| 5804 | sub re_dq { |
| 5805 | my $self = shift; |
| 5806 | my ($op) = @_; |
| 5807 | |
| 5808 | my $type = $op->name; |
| 5809 | if ($type eq "const") { |
| 5810 | return '$[' if $op->private & OPpCONST_ARYBASE; |
| 5811 | my $unbacked = re_unback($self->const_sv($op)->as_string); |
| 5812 | return re_uninterp(escape_re($unbacked)); |
| 5813 | } elsif ($type eq "concat") { |
| 5814 | my $first = $self->re_dq($op->first); |
| 5815 | my $last = $self->re_dq($op->last); |
| 5816 | return re_dq_disambiguate($first, $last); |
| 5817 | } elsif ($type eq "multiconcat") { |
| 5818 | return $self->do_multiconcat($op, 26, 2); |
| 5819 | } elsif ($type eq "uc") { |
| 5820 | return '\U' . $self->re_dq($op->first->sibling) . '\E'; |
| 5821 | } elsif ($type eq "lc") { |
| 5822 | return '\L' . $self->re_dq($op->first->sibling) . '\E'; |
| 5823 | } elsif ($type eq "ucfirst") { |
| 5824 | return '\u' . $self->re_dq($op->first->sibling); |
| 5825 | } elsif ($type eq "lcfirst") { |
| 5826 | return '\l' . $self->re_dq($op->first->sibling); |
| 5827 | } elsif ($type eq "quotemeta") { |
| 5828 | return '\Q' . $self->re_dq($op->first->sibling) . '\E'; |
| 5829 | } elsif ($type eq "fc") { |
| 5830 | return '\F' . $self->re_dq($op->first->sibling) . '\E'; |
| 5831 | } elsif ($type eq "join") { |
| 5832 | return $self->deparse($op->last, 26); # was join($", @ary) |
| 5833 | } else { |
| 5834 | my $ret = $self->deparse($op, 26); |
| 5835 | $ret =~ s/^\$([(|)])\z/\${$1}/ # $( $| $) need braces |
| 5836 | or $ret =~ s/^\@([-+])\z/\@{$1}/; # @- @+ need braces |
| 5837 | return $ret; |
| 5838 | } |
| 5839 | } |
| 5840 | |
| 5841 | sub pure_string { |
| 5842 | my ($self, $op) = @_; |
| 5843 | return 0 if null $op; |
| 5844 | my $type = $op->name; |
| 5845 | |
| 5846 | if ($type eq 'const' || $type eq 'av2arylen') { |
| 5847 | return 1; |
| 5848 | } |
| 5849 | elsif ($type =~ /^(?:[ul]c(first)?|fc)$/ || $type eq 'quotemeta') { |
| 5850 | return $self->pure_string($op->first->sibling); |
| 5851 | } |
| 5852 | elsif ($type eq 'join') { |
| 5853 | my $join_op = $op->first->sibling; # Skip pushmark |
| 5854 | return 0 unless $join_op->name eq 'null' && $join_op->targ == OP_RV2SV; |
| 5855 | |
| 5856 | my $gvop = $join_op->first; |
| 5857 | return 0 unless $gvop->name eq 'gvsv'; |
| 5858 | return 0 unless '"' eq $self->gv_name($self->gv_or_padgv($gvop)); |
| 5859 | |
| 5860 | return 0 unless ${$join_op->sibling} eq ${$op->last}; |
| 5861 | return 0 unless $op->last->name =~ /^(?:[ah]slice|(?:rv2|pad)av)$/; |
| 5862 | } |
| 5863 | elsif ($type eq 'concat') { |
| 5864 | return $self->pure_string($op->first) |
| 5865 | && $self->pure_string($op->last); |
| 5866 | } |
| 5867 | elsif ($type eq 'multiconcat') { |
| 5868 | my ($kid, @kids); |
| 5869 | for ($kid = $op->first; !null $kid; $kid = $kid->sibling) { |
| 5870 | # skip the consts and/or padsv we've optimised away |
| 5871 | push @kids, $kid |
| 5872 | unless $kid->type == OP_NULL |
| 5873 | && ( $kid->targ == OP_PADSV |
| 5874 | || $kid->targ == OP_CONST |
| 5875 | || $kid->targ == OP_PUSHMARK); |
| 5876 | } |
| 5877 | |
| 5878 | if ($op->flags & OPf_STACKED) { |
| 5879 | # remove expr from @kids where 'expr = ...' or 'expr .= ....' |
| 5880 | if ($op->private & OPpMULTICONCAT_APPEND) { |
| 5881 | shift(@kids); |
| 5882 | } |
| 5883 | else { |
| 5884 | pop(@kids); |
| 5885 | } |
| 5886 | } |
| 5887 | for (@kids) { |
| 5888 | return 0 unless $self->pure_string($_); |
| 5889 | } |
| 5890 | return 1; |
| 5891 | } |
| 5892 | elsif (is_scalar($op) || $type =~ /^[ah]elem$/) { |
| 5893 | return 1; |
| 5894 | } |
| 5895 | elsif ($type eq "null" and $op->can('first') and not null $op->first) { |
| 5896 | my $first = $op->first; |
| 5897 | |
| 5898 | return 1 if $first->name eq "multideref"; |
| 5899 | return 1 if $first->name eq "aelemfast_lex"; |
| 5900 | |
| 5901 | if ( $first->name eq "null" |
| 5902 | and $first->can('first') |
| 5903 | and not null $first->first |
| 5904 | and $first->first->name eq "aelemfast" |
| 5905 | ) |
| 5906 | { |
| 5907 | return 1; |
| 5908 | } |
| 5909 | } |
| 5910 | |
| 5911 | return 0; |
| 5912 | } |
| 5913 | |
| 5914 | sub code_list { |
| 5915 | my ($self,$op,$cv) = @_; |
| 5916 | |
| 5917 | # localise stuff relating to the current sub |
| 5918 | $cv and |
| 5919 | local($self->{'curcv'}) = $cv, |
| 5920 | local($self->{'curcvlex'}), |
| 5921 | local(@$self{qw'curstash warnings hints hinthash curcop'}) |
| 5922 | = @$self{qw'curstash warnings hints hinthash curcop'}; |
| 5923 | |
| 5924 | my $re; |
| 5925 | for ($op = $op->first->sibling; !null($op); $op = $op->sibling) { |
| 5926 | if ($op->name eq 'null' and $op->flags & OPf_SPECIAL) { |
| 5927 | my $scope = $op->first; |
| 5928 | # 0 context (last arg to scopeop) means statement context, so |
| 5929 | # the contents of the block will not be wrapped in do{...}. |
| 5930 | my $block = scopeop($scope->first->name eq "enter", $self, |
| 5931 | $scope, 0); |
| 5932 | # next op is the source code of the block |
| 5933 | $op = $op->sibling; |
| 5934 | $re .= ($self->const_sv($op)->PV =~ m|^(\(\?\??\{)|)[0]; |
| 5935 | my $multiline = $block =~ /\n/; |
| 5936 | $re .= $multiline ? "\n\t" : ' '; |
| 5937 | $re .= $block; |
| 5938 | $re .= $multiline ? "\n\b})" : " })"; |
| 5939 | } else { |
| 5940 | $re = re_dq_disambiguate($re, $self->re_dq($op)); |
| 5941 | } |
| 5942 | } |
| 5943 | $re; |
| 5944 | } |
| 5945 | |
| 5946 | sub regcomp { |
| 5947 | my $self = shift; |
| 5948 | my($op, $cx) = @_; |
| 5949 | my $kid = $op->first; |
| 5950 | $kid = $kid->first if $kid->name eq "regcmaybe"; |
| 5951 | $kid = $kid->first if $kid->name eq "regcreset"; |
| 5952 | my $kname = $kid->name; |
| 5953 | if ($kname eq "null" and !null($kid->first) |
| 5954 | and $kid->first->name eq 'pushmark') |
| 5955 | { |
| 5956 | my $str = ''; |
| 5957 | $kid = $kid->first->sibling; |
| 5958 | while (!null($kid)) { |
| 5959 | my $first = $str; |
| 5960 | my $last = $self->re_dq($kid); |
| 5961 | $str = re_dq_disambiguate($first, $last); |
| 5962 | $kid = $kid->sibling; |
| 5963 | } |
| 5964 | return $str, 1; |
| 5965 | } |
| 5966 | |
| 5967 | return ($self->re_dq($kid), 1) |
| 5968 | if $kname =~ /^(?:rv2|pad)av/ or $self->pure_string($kid); |
| 5969 | return ($self->deparse($kid, $cx), 0); |
| 5970 | } |
| 5971 | |
| 5972 | sub pp_regcomp { |
| 5973 | my ($self, $op, $cx) = @_; |
| 5974 | return (($self->regcomp($op, $cx, 0))[0]); |
| 5975 | } |
| 5976 | |
| 5977 | sub re_flags { |
| 5978 | my ($self, $op) = @_; |
| 5979 | my $flags = ''; |
| 5980 | my $pmflags = $op->pmflags; |
| 5981 | if (!$pmflags) { |
| 5982 | my $re = $op->pmregexp; |
| 5983 | if ($$re) { |
| 5984 | $pmflags = $re->compflags; |
| 5985 | } |
| 5986 | } |
| 5987 | $flags .= "g" if $pmflags & PMf_GLOBAL; |
| 5988 | $flags .= "i" if $pmflags & PMf_FOLD; |
| 5989 | $flags .= "m" if $pmflags & PMf_MULTILINE; |
| 5990 | $flags .= "o" if $pmflags & PMf_KEEP; |
| 5991 | $flags .= "s" if $pmflags & PMf_SINGLELINE; |
| 5992 | $flags .= "x" if $pmflags & PMf_EXTENDED; |
| 5993 | $flags .= "x" if $pmflags & PMf_EXTENDED_MORE; |
| 5994 | $flags .= "p" if $pmflags & PMf_KEEPCOPY; |
| 5995 | $flags .= "n" if $pmflags & PMf_NOCAPTURE; |
| 5996 | if (my $charset = $pmflags & PMf_CHARSET) { |
| 5997 | # Hardcoding this is fragile, but B does not yet export the |
| 5998 | # constants we need. |
| 5999 | $flags .= qw(d l u a aa)[$charset >> 7] |
| 6000 | } |
| 6001 | # The /d flag is indicated by 0; only show it if necessary. |
| 6002 | elsif ($self->{hinthash} and |
| 6003 | $self->{hinthash}{reflags_charset} |
| 6004 | || $self->{hinthash}{feature_unicode} |
| 6005 | or $self->{hints} & $feature::hint_mask |
| 6006 | && ($self->{hints} & $feature::hint_mask) |
| 6007 | != $feature::hint_mask |
| 6008 | && $self->{hints} & $feature::hint_uni8bit |
| 6009 | ) { |
| 6010 | $flags .= 'd'; |
| 6011 | } |
| 6012 | $flags; |
| 6013 | } |
| 6014 | |
| 6015 | # osmic acid -- see osmium tetroxide |
| 6016 | |
| 6017 | my %matchwords; |
| 6018 | map($matchwords{join "", sort split //, $_} = $_, 'cig', 'cog', 'cos', 'cogs', |
| 6019 | 'cox', 'go', 'is', 'ism', 'iso', 'mig', 'mix', 'osmic', 'ox', 'sic', |
| 6020 | 'sig', 'six', 'smog', 'so', 'soc', 'sog', 'xi', 'soup', 'soupmix'); |
| 6021 | |
| 6022 | # When deparsing a regular expression with code blocks, we have to look in |
| 6023 | # various places to find the blocks. |
| 6024 | # |
| 6025 | # For qr/(?{...})/ without interpolation, the CV is under $qr->qr_anoncv |
| 6026 | # and the code list (list of blocks and constants, maybe vars) is under |
| 6027 | # $cv->ROOT->first->code_list: |
| 6028 | # ./perl -Ilib -MB -e 'use O "Concise", B::svref_2object(sub {qr/(?{die})/})->ROOT->first->first->sibling->pmregexp->qr_anoncv->object_2svref' |
| 6029 | # |
| 6030 | # For qr/$a(?{...})/ with interpolation, the code list is more accessible, |
| 6031 | # under $pmop->code_list, but the $cv is something you have to dig for in |
| 6032 | # the regcomp op’s kids: |
| 6033 | # ./perl -Ilib -mO=Concise -e 'qr/$a(?{die})/' |
| 6034 | # |
| 6035 | # For m// and split //, things are much simpler. There is no CV. The code |
| 6036 | # list is under $pmop->code_list. |
| 6037 | |
| 6038 | sub matchop { |
| 6039 | my $self = shift; |
| 6040 | my($op, $cx, $name, $delim) = @_; |
| 6041 | my $kid = $op->first; |
| 6042 | my ($binop, $var, $re) = ("", "", ""); |
| 6043 | if ($op->name ne 'split' && $op->flags & OPf_STACKED) { |
| 6044 | $binop = 1; |
| 6045 | $var = $self->deparse($kid, 20); |
| 6046 | $kid = $kid->sibling; |
| 6047 | } |
| 6048 | # not $name; $name will be 'm' for both match and split |
| 6049 | elsif ($op->name eq 'match' and my $targ = $op->targ) { |
| 6050 | $binop = 1; |
| 6051 | $var = $self->padname($targ); |
| 6052 | } |
| 6053 | my $quote = 1; |
| 6054 | my $pmflags = $op->pmflags; |
| 6055 | my $rhs_bound_to_defsv; |
| 6056 | my ($cv, $bregexp); |
| 6057 | my $have_kid = !null $kid; |
| 6058 | # Check for code blocks first |
| 6059 | if (not null my $code_list = $op->code_list) { |
| 6060 | $re = $self->code_list($code_list, |
| 6061 | $op->name eq 'qr' |
| 6062 | ? $self->padval( |
| 6063 | $kid->first # ex-list |
| 6064 | ->first # pushmark |
| 6065 | ->sibling # entersub |
| 6066 | ->first # ex-list |
| 6067 | ->first # pushmark |
| 6068 | ->sibling # srefgen |
| 6069 | ->first # ex-list |
| 6070 | ->first # anoncode |
| 6071 | ->targ |
| 6072 | ) |
| 6073 | : undef); |
| 6074 | } elsif (${$bregexp = $op->pmregexp} && ${$cv = $bregexp->qr_anoncv}) { |
| 6075 | my $patop = $cv->ROOT # leavesub |
| 6076 | ->first # qr |
| 6077 | ->code_list;# list |
| 6078 | $re = $self->code_list($patop, $cv); |
| 6079 | } elsif (!$have_kid) { |
| 6080 | $re = re_uninterp(escape_re(re_unback($op->precomp))); |
| 6081 | } elsif ($kid->name ne 'regcomp') { |
| 6082 | if ($op->name eq 'split') { |
| 6083 | # split has other kids, not just regcomp |
| 6084 | $re = re_uninterp(escape_re(re_unback($op->precomp))); |
| 6085 | } |
| 6086 | else { |
| 6087 | carp("found ".$kid->name." where regcomp expected"); |
| 6088 | } |
| 6089 | } else { |
| 6090 | ($re, $quote) = $self->regcomp($kid, 21); |
| 6091 | } |
| 6092 | if ($have_kid and $kid->name eq 'regcomp') { |
| 6093 | my $matchop = $kid->first; |
| 6094 | if ($matchop->name eq 'regcreset') { |
| 6095 | $matchop = $matchop->first; |
| 6096 | } |
| 6097 | if ($matchop->name =~ /^(?:match|transr?|subst)\z/ |
| 6098 | && $matchop->flags & OPf_SPECIAL) { |
| 6099 | $rhs_bound_to_defsv = 1; |
| 6100 | } |
| 6101 | } |
| 6102 | my $flags = ""; |
| 6103 | $flags .= "c" if $pmflags & PMf_CONTINUE; |
| 6104 | $flags .= $self->re_flags($op); |
| 6105 | $flags = join '', sort split //, $flags; |
| 6106 | $flags = $matchwords{$flags} if $matchwords{$flags}; |
| 6107 | if ($pmflags & PMf_ONCE) { # only one kind of delimiter works here |
| 6108 | $re =~ s/\?/\\?/g; |
| 6109 | $re = $self->keyword("m") . "?$re?"; # explicit 'm' is required |
| 6110 | } elsif ($quote) { |
| 6111 | $re = single_delim($name, $delim, $re, $self); |
| 6112 | } |
| 6113 | $re = $re . $flags if $quote; |
| 6114 | if ($binop) { |
| 6115 | return |
| 6116 | $self->maybe_parens( |
| 6117 | $rhs_bound_to_defsv |
| 6118 | ? "$var =~ (\$_ =~ $re)" |
| 6119 | : "$var =~ $re", |
| 6120 | $cx, 20 |
| 6121 | ); |
| 6122 | } else { |
| 6123 | return $re; |
| 6124 | } |
| 6125 | } |
| 6126 | |
| 6127 | sub pp_match { matchop(@_, "m", "/") } |
| 6128 | sub pp_qr { matchop(@_, "qr", "") } |
| 6129 | |
| 6130 | sub pp_runcv { unop(@_, "__SUB__"); } |
| 6131 | |
| 6132 | sub pp_split { |
| 6133 | my $self = shift; |
| 6134 | my($op, $cx) = @_; |
| 6135 | my($kid, @exprs, $ary, $expr); |
| 6136 | my $stacked = $op->flags & OPf_STACKED; |
| 6137 | |
| 6138 | $kid = $op->first; |
| 6139 | $kid = $kid->sibling if $kid->name eq 'regcomp'; |
| 6140 | for (; !null($kid); $kid = $kid->sibling) { |
| 6141 | push @exprs, $self->deparse($kid, 6); |
| 6142 | } |
| 6143 | |
| 6144 | unshift @exprs, $self->matchop($op, $cx, "m", "/"); |
| 6145 | |
| 6146 | if ($op->private & OPpSPLIT_ASSIGN) { |
| 6147 | # With C<@array = split(/pat/, str);>, |
| 6148 | # array is stored in split's pmreplroot; either |
| 6149 | # as an integer index into the pad (for a lexical array) |
| 6150 | # or as GV for a package array (which will be a pad index |
| 6151 | # on threaded builds) |
| 6152 | # With my/our @array = split(/pat/, str), the array is instead |
| 6153 | # accessed via an extra padav/rv2av op at the end of the |
| 6154 | # split's kid ops. |
| 6155 | |
| 6156 | if ($stacked) { |
| 6157 | $ary = pop @exprs; |
| 6158 | } |
| 6159 | else { |
| 6160 | if ($op->private & OPpSPLIT_LEX) { |
| 6161 | $ary = $self->padname($op->pmreplroot); |
| 6162 | } |
| 6163 | else { |
| 6164 | # union with op_pmtargetoff, op_pmtargetgv |
| 6165 | my $gv = $op->pmreplroot; |
| 6166 | $gv = $self->padval($gv) if !ref($gv); |
| 6167 | $ary = $self->maybe_local(@_, |
| 6168 | $self->stash_variable('@', |
| 6169 | $self->gv_name($gv), |
| 6170 | $cx)) |
| 6171 | } |
| 6172 | if ($op->private & OPpLVAL_INTRO) { |
| 6173 | $ary = $op->private & OPpSPLIT_LEX ? "my $ary" : "local $ary"; |
| 6174 | } |
| 6175 | } |
| 6176 | } |
| 6177 | |
| 6178 | # handle special case of split(), and split(' ') that compiles to /\s+/ |
| 6179 | $exprs[0] = q{' '} if ($op->reflags // 0) & RXf_SKIPWHITE(); |
| 6180 | |
| 6181 | $expr = "split(" . join(", ", @exprs) . ")"; |
| 6182 | if ($ary) { |
| 6183 | return $self->maybe_parens("$ary = $expr", $cx, 7); |
| 6184 | } else { |
| 6185 | return $expr; |
| 6186 | } |
| 6187 | } |
| 6188 | |
| 6189 | # oxime -- any of various compounds obtained chiefly by the action of |
| 6190 | # hydroxylamine on aldehydes and ketones and characterized by the |
| 6191 | # bivalent grouping C=NOH [Webster's Tenth] |
| 6192 | |
| 6193 | my %substwords; |
| 6194 | map($substwords{join "", sort split //, $_} = $_, 'ego', 'egoism', 'em', |
| 6195 | 'es', 'ex', 'exes', 'gee', 'go', 'goes', 'ie', 'ism', 'iso', 'me', |
| 6196 | 'meese', 'meso', 'mig', 'mix', 'os', 'ox', 'oxime', 'see', 'seem', |
| 6197 | 'seg', 'sex', 'sig', 'six', 'smog', 'sog', 'some', 'xi', 'rogue', |
| 6198 | 'sir', 'rise', 'smore', 'more', 'seer', 'rome', 'gore', 'grim', 'grime', |
| 6199 | 'or', 'rose', 'rosie'); |
| 6200 | |
| 6201 | sub pp_subst { |
| 6202 | my $self = shift; |
| 6203 | my($op, $cx) = @_; |
| 6204 | my $kid = $op->first; |
| 6205 | my($binop, $var, $re, $repl) = ("", "", "", ""); |
| 6206 | if ($op->flags & OPf_STACKED) { |
| 6207 | $binop = 1; |
| 6208 | $var = $self->deparse($kid, 20); |
| 6209 | $kid = $kid->sibling; |
| 6210 | } |
| 6211 | elsif (my $targ = $op->targ) { |
| 6212 | $binop = 1; |
| 6213 | $var = $self->padname($targ); |
| 6214 | } |
| 6215 | my $flags = ""; |
| 6216 | my $pmflags = $op->pmflags; |
| 6217 | if (null($op->pmreplroot)) { |
| 6218 | $repl = $kid; |
| 6219 | $kid = $kid->sibling; |
| 6220 | } else { |
| 6221 | $repl = $op->pmreplroot->first; # skip substcont |
| 6222 | } |
| 6223 | while ($repl->name eq "entereval") { |
| 6224 | $repl = $repl->first; |
| 6225 | $flags .= "e"; |
| 6226 | } |
| 6227 | { |
| 6228 | local $self->{in_subst_repl} = 1; |
| 6229 | if ($pmflags & PMf_EVAL) { |
| 6230 | $repl = $self->deparse($repl->first, 0); |
| 6231 | } else { |
| 6232 | $repl = $self->dq($repl); |
| 6233 | } |
| 6234 | } |
| 6235 | if (not null my $code_list = $op->code_list) { |
| 6236 | $re = $self->code_list($code_list); |
| 6237 | } elsif (null $kid) { |
| 6238 | $re = re_uninterp(escape_re(re_unback($op->precomp))); |
| 6239 | } else { |
| 6240 | ($re) = $self->regcomp($kid, 1); |
| 6241 | } |
| 6242 | $flags .= "r" if $pmflags & PMf_NONDESTRUCT; |
| 6243 | $flags .= "e" if $pmflags & PMf_EVAL; |
| 6244 | $flags .= $self->re_flags($op); |
| 6245 | $flags = join '', sort split //, $flags; |
| 6246 | $flags = $substwords{$flags} if $substwords{$flags}; |
| 6247 | my $core_s = $self->keyword("s"); # maybe CORE::s |
| 6248 | if ($binop) { |
| 6249 | return $self->maybe_parens("$var =~ $core_s" |
| 6250 | . double_delim($re, $repl) . $flags, |
| 6251 | $cx, 20); |
| 6252 | } else { |
| 6253 | return "$core_s". double_delim($re, $repl) . $flags; |
| 6254 | } |
| 6255 | } |
| 6256 | |
| 6257 | sub is_lexical_subs { |
| 6258 | my (@ops) = shift; |
| 6259 | for my $op (@ops) { |
| 6260 | return 0 if $op->name !~ /\A(?:introcv|clonecv)\z/; |
| 6261 | } |
| 6262 | return 1; |
| 6263 | } |
| 6264 | |
| 6265 | # Pretend these two ops do not exist. The perl parser adds them to the |
| 6266 | # beginning of any block containing my-sub declarations, whereas we handle |
| 6267 | # the subs in pad_subs and next_todo. |
| 6268 | *pp_clonecv = *pp_introcv; |
| 6269 | sub pp_introcv { |
| 6270 | my $self = shift; |
| 6271 | my($op, $cx) = @_; |
| 6272 | # For now, deparsing doesn't worry about the distinction between introcv |
| 6273 | # and clonecv, so pretend this op doesn't exist: |
| 6274 | return ''; |
| 6275 | } |
| 6276 | |
| 6277 | sub pp_padcv { |
| 6278 | my $self = shift; |
| 6279 | my($op, $cx) = @_; |
| 6280 | return $self->padany($op); |
| 6281 | } |
| 6282 | |
| 6283 | my %lvref_funnies = ( |
| 6284 | OPpLVREF_SV, => '$', |
| 6285 | OPpLVREF_AV, => '@', |
| 6286 | OPpLVREF_HV, => '%', |
| 6287 | OPpLVREF_CV, => '&', |
| 6288 | ); |
| 6289 | |
| 6290 | sub pp_refassign { |
| 6291 | my ($self, $op, $cx) = @_; |
| 6292 | my $left; |
| 6293 | if ($op->private & OPpLVREF_ELEM) { |
| 6294 | $left = $op->first->sibling; |
| 6295 | $left = maybe_local(@_, elem($self, $left, undef, |
| 6296 | $left->targ == OP_AELEM |
| 6297 | ? qw([ ] padav) |
| 6298 | : qw({ } padhv))); |
| 6299 | } elsif ($op->flags & OPf_STACKED) { |
| 6300 | $left = maybe_local(@_, |
| 6301 | $lvref_funnies{$op->private & OPpLVREF_TYPE} |
| 6302 | . $self->deparse($op->first->sibling)); |
| 6303 | } else { |
| 6304 | $left = &pp_padsv; |
| 6305 | } |
| 6306 | my $right = $self->deparse_binop_right($op, $op->first, 7); |
| 6307 | return $self->maybe_parens("\\$left = $right", $cx, 7); |
| 6308 | } |
| 6309 | |
| 6310 | sub pp_lvref { |
| 6311 | my ($self, $op, $cx) = @_; |
| 6312 | my $code; |
| 6313 | if ($op->private & OPpLVREF_ELEM) { |
| 6314 | $code = $op->first->name =~ /av\z/ ? &pp_aelem : &pp_helem; |
| 6315 | } elsif ($op->flags & OPf_STACKED) { |
| 6316 | $code = maybe_local(@_, |
| 6317 | $lvref_funnies{$op->private & OPpLVREF_TYPE} |
| 6318 | . $self->deparse($op->first)); |
| 6319 | } else { |
| 6320 | $code = &pp_padsv; |
| 6321 | } |
| 6322 | "\\$code"; |
| 6323 | } |
| 6324 | |
| 6325 | sub pp_lvrefslice { |
| 6326 | my ($self, $op, $cx) = @_; |
| 6327 | '\\' . ($op->last->name =~ /av\z/ ? &pp_aslice : &pp_hslice); |
| 6328 | } |
| 6329 | |
| 6330 | sub pp_lvavref { |
| 6331 | my ($self, $op, $cx) = @_; |
| 6332 | '\\(' . ($op->flags & OPf_STACKED |
| 6333 | ? maybe_local(@_, rv2x(@_, "\@")) |
| 6334 | : &pp_padsv) . ')' |
| 6335 | } |
| 6336 | |
| 6337 | |
| 6338 | sub pp_argcheck { |
| 6339 | my $self = shift; |
| 6340 | my($op, $cx) = @_; |
| 6341 | my ($params, $opt_params, $slurpy) = $op->aux_list($self->{curcv}); |
| 6342 | my $mandatory = $params - $opt_params; |
| 6343 | my $check = ''; |
| 6344 | |
| 6345 | $check .= <<EOF if !$slurpy; |
| 6346 | die sprintf("Too many arguments for subroutine at %s line %d.\\n", (caller)[1, 2]) unless \@_ <= $params; |
| 6347 | EOF |
| 6348 | |
| 6349 | $check .= <<EOF if $mandatory > 0; |
| 6350 | die sprintf("Too few arguments for subroutine at %s line %d.\\n", (caller)[1, 2]) unless \@_ >= $mandatory; |
| 6351 | EOF |
| 6352 | |
| 6353 | my $cond = ($params & 1) ? 'unless' : 'if'; |
| 6354 | $check .= <<EOF if $slurpy eq '%'; |
| 6355 | die sprintf("Odd name/value argument for subroutine at %s line %d.\\n", (caller)[1, 2]) if \@_ > $params && ((\@_ - $params) & 1); |
| 6356 | EOF |
| 6357 | |
| 6358 | $check =~ s/;\n\z//; |
| 6359 | return $check; |
| 6360 | } |
| 6361 | |
| 6362 | |
| 6363 | sub pp_argelem { |
| 6364 | my $self = shift; |
| 6365 | my($op, $cx) = @_; |
| 6366 | my $var = $self->padname($op->targ); |
| 6367 | my $ix = $op->string($self->{curcv}); |
| 6368 | my $expr; |
| 6369 | if ($op->flags & OPf_KIDS) { |
| 6370 | $expr = $self->deparse($op->first, 7); |
| 6371 | } |
| 6372 | elsif ($var =~ /^[@%]/) { |
| 6373 | $expr = $ix ? "\@_[$ix .. \$#_]" : '@_'; |
| 6374 | } |
| 6375 | else { |
| 6376 | $expr = "\$_[$ix]"; |
| 6377 | } |
| 6378 | return "my $var = $expr"; |
| 6379 | } |
| 6380 | |
| 6381 | |
| 6382 | sub pp_argdefelem { |
| 6383 | my $self = shift; |
| 6384 | my($op, $cx) = @_; |
| 6385 | my $ix = $op->targ; |
| 6386 | my $expr = "\@_ >= " . ($ix+1) . " ? \$_[$ix] : "; |
| 6387 | my $def = $self->deparse($op->first, 7); |
| 6388 | $def = "($def)" if $op->first->flags & OPf_PARENS; |
| 6389 | $expr .= $self->deparse($op->first, $cx); |
| 6390 | return $expr; |
| 6391 | } |
| 6392 | |
| 6393 | |
| 6394 | 1; |
| 6395 | __END__ |
| 6396 | |
| 6397 | =head1 NAME |
| 6398 | |
| 6399 | B::Deparse - Perl compiler backend to produce perl code |
| 6400 | |
| 6401 | =head1 SYNOPSIS |
| 6402 | |
| 6403 | B<perl> B<-MO=Deparse>[B<,-d>][B<,-f>I<FILE>][B<,-p>][B<,-q>][B<,-l>] |
| 6404 | [B<,-s>I<LETTERS>][B<,-x>I<LEVEL>] I<prog.pl> |
| 6405 | |
| 6406 | =head1 DESCRIPTION |
| 6407 | |
| 6408 | B::Deparse is a backend module for the Perl compiler that generates |
| 6409 | perl source code, based on the internal compiled structure that perl |
| 6410 | itself creates after parsing a program. The output of B::Deparse won't |
| 6411 | be exactly the same as the original source, since perl doesn't keep |
| 6412 | track of comments or whitespace, and there isn't a one-to-one |
| 6413 | correspondence between perl's syntactical constructions and their |
| 6414 | compiled form, but it will often be close. When you use the B<-p> |
| 6415 | option, the output also includes parentheses even when they are not |
| 6416 | required by precedence, which can make it easy to see if perl is |
| 6417 | parsing your expressions the way you intended. |
| 6418 | |
| 6419 | While B::Deparse goes to some lengths to try to figure out what your |
| 6420 | original program was doing, some parts of the language can still trip |
| 6421 | it up; it still fails even on some parts of Perl's own test suite. If |
| 6422 | you encounter a failure other than the most common ones described in |
| 6423 | the BUGS section below, you can help contribute to B::Deparse's |
| 6424 | ongoing development by submitting a bug report with a small |
| 6425 | example. |
| 6426 | |
| 6427 | =head1 OPTIONS |
| 6428 | |
| 6429 | As with all compiler backend options, these must follow directly after |
| 6430 | the '-MO=Deparse', separated by a comma but not any white space. |
| 6431 | |
| 6432 | =over 4 |
| 6433 | |
| 6434 | =item B<-d> |
| 6435 | |
| 6436 | Output data values (when they appear as constants) using Data::Dumper. |
| 6437 | Without this option, B::Deparse will use some simple routines of its |
| 6438 | own for the same purpose. Currently, Data::Dumper is better for some |
| 6439 | kinds of data (such as complex structures with sharing and |
| 6440 | self-reference) while the built-in routines are better for others |
| 6441 | (such as odd floating-point values). |
| 6442 | |
| 6443 | =item B<-f>I<FILE> |
| 6444 | |
| 6445 | Normally, B::Deparse deparses the main code of a program, and all the subs |
| 6446 | defined in the same file. To include subs defined in |
| 6447 | other files, pass the B<-f> option with the filename. |
| 6448 | You can pass the B<-f> option several times, to |
| 6449 | include more than one secondary file. (Most of the time you don't want to |
| 6450 | use it at all.) You can also use this option to include subs which are |
| 6451 | defined in the scope of a B<#line> directive with two parameters. |
| 6452 | |
| 6453 | =item B<-l> |
| 6454 | |
| 6455 | Add '#line' declarations to the output based on the line and file |
| 6456 | locations of the original code. |
| 6457 | |
| 6458 | =item B<-p> |
| 6459 | |
| 6460 | Print extra parentheses. Without this option, B::Deparse includes |
| 6461 | parentheses in its output only when they are needed, based on the |
| 6462 | structure of your program. With B<-p>, it uses parentheses (almost) |
| 6463 | whenever they would be legal. This can be useful if you are used to |
| 6464 | LISP, or if you want to see how perl parses your input. If you say |
| 6465 | |
| 6466 | if ($var & 0x7f == 65) {print "Gimme an A!"} |
| 6467 | print ($which ? $a : $b), "\n"; |
| 6468 | $name = $ENV{USER} or "Bob"; |
| 6469 | |
| 6470 | C<B::Deparse,-p> will print |
| 6471 | |
| 6472 | if (($var & 0)) { |
| 6473 | print('Gimme an A!') |
| 6474 | }; |
| 6475 | (print(($which ? $a : $b)), '???'); |
| 6476 | (($name = $ENV{'USER'}) or '???') |
| 6477 | |
| 6478 | which probably isn't what you intended (the C<'???'> is a sign that |
| 6479 | perl optimized away a constant value). |
| 6480 | |
| 6481 | =item B<-P> |
| 6482 | |
| 6483 | Disable prototype checking. With this option, all function calls are |
| 6484 | deparsed as if no prototype was defined for them. In other words, |
| 6485 | |
| 6486 | perl -MO=Deparse,-P -e 'sub foo (\@) { 1 } foo @x' |
| 6487 | |
| 6488 | will print |
| 6489 | |
| 6490 | sub foo (\@) { |
| 6491 | 1; |
| 6492 | } |
| 6493 | &foo(\@x); |
| 6494 | |
| 6495 | making clear how the parameters are actually passed to C<foo>. |
| 6496 | |
| 6497 | =item B<-q> |
| 6498 | |
| 6499 | Expand double-quoted strings into the corresponding combinations of |
| 6500 | concatenation, uc, ucfirst, lc, lcfirst, quotemeta, and join. For |
| 6501 | instance, print |
| 6502 | |
| 6503 | print "Hello, $world, @ladies, \u$gentlemen\E, \u\L$me!"; |
| 6504 | |
| 6505 | as |
| 6506 | |
| 6507 | print 'Hello, ' . $world . ', ' . join($", @ladies) . ', ' |
| 6508 | . ucfirst($gentlemen) . ', ' . ucfirst(lc $me . '!'); |
| 6509 | |
| 6510 | Note that the expanded form represents the way perl handles such |
| 6511 | constructions internally -- this option actually turns off the reverse |
| 6512 | translation that B::Deparse usually does. On the other hand, note that |
| 6513 | C<$x = "$y"> is not the same as C<$x = $y>: the former makes the value |
| 6514 | of $y into a string before doing the assignment. |
| 6515 | |
| 6516 | =item B<-s>I<LETTERS> |
| 6517 | |
| 6518 | Tweak the style of B::Deparse's output. The letters should follow |
| 6519 | directly after the 's', with no space or punctuation. The following |
| 6520 | options are available: |
| 6521 | |
| 6522 | =over 4 |
| 6523 | |
| 6524 | =item B<C> |
| 6525 | |
| 6526 | Cuddle C<elsif>, C<else>, and C<continue> blocks. For example, print |
| 6527 | |
| 6528 | if (...) { |
| 6529 | ... |
| 6530 | } else { |
| 6531 | ... |
| 6532 | } |
| 6533 | |
| 6534 | instead of |
| 6535 | |
| 6536 | if (...) { |
| 6537 | ... |
| 6538 | } |
| 6539 | else { |
| 6540 | ... |
| 6541 | } |
| 6542 | |
| 6543 | The default is not to cuddle. |
| 6544 | |
| 6545 | =item B<i>I<NUMBER> |
| 6546 | |
| 6547 | Indent lines by multiples of I<NUMBER> columns. The default is 4 columns. |
| 6548 | |
| 6549 | =item B<T> |
| 6550 | |
| 6551 | Use tabs for each 8 columns of indent. The default is to use only spaces. |
| 6552 | For instance, if the style options are B<-si4T>, a line that's indented |
| 6553 | 3 times will be preceded by one tab and four spaces; if the options were |
| 6554 | B<-si8T>, the same line would be preceded by three tabs. |
| 6555 | |
| 6556 | =item B<v>I<STRING>B<.> |
| 6557 | |
| 6558 | Print I<STRING> for the value of a constant that can't be determined |
| 6559 | because it was optimized away (mnemonic: this happens when a constant |
| 6560 | is used in B<v>oid context). The end of the string is marked by a period. |
| 6561 | The string should be a valid perl expression, generally a constant. |
| 6562 | Note that unless it's a number, it probably needs to be quoted, and on |
| 6563 | a command line quotes need to be protected from the shell. Some |
| 6564 | conventional values include 0, 1, 42, '', 'foo', and |
| 6565 | 'Useless use of constant omitted' (which may need to be |
| 6566 | B<-sv"'Useless use of constant omitted'."> |
| 6567 | or something similar depending on your shell). The default is '???'. |
| 6568 | If you're using B::Deparse on a module or other file that's require'd, |
| 6569 | you shouldn't use a value that evaluates to false, since the customary |
| 6570 | true constant at the end of a module will be in void context when the |
| 6571 | file is compiled as a main program. |
| 6572 | |
| 6573 | =back |
| 6574 | |
| 6575 | =item B<-x>I<LEVEL> |
| 6576 | |
| 6577 | Expand conventional syntax constructions into equivalent ones that expose |
| 6578 | their internal operation. I<LEVEL> should be a digit, with higher values |
| 6579 | meaning more expansion. As with B<-q>, this actually involves turning off |
| 6580 | special cases in B::Deparse's normal operations. |
| 6581 | |
| 6582 | If I<LEVEL> is at least 3, C<for> loops will be translated into equivalent |
| 6583 | while loops with continue blocks; for instance |
| 6584 | |
| 6585 | for ($i = 0; $i < 10; ++$i) { |
| 6586 | print $i; |
| 6587 | } |
| 6588 | |
| 6589 | turns into |
| 6590 | |
| 6591 | $i = 0; |
| 6592 | while ($i < 10) { |
| 6593 | print $i; |
| 6594 | } continue { |
| 6595 | ++$i |
| 6596 | } |
| 6597 | |
| 6598 | Note that in a few cases this translation can't be perfectly carried back |
| 6599 | into the source code -- if the loop's initializer declares a my variable, |
| 6600 | for instance, it won't have the correct scope outside of the loop. |
| 6601 | |
| 6602 | If I<LEVEL> is at least 5, C<use> declarations will be translated into |
| 6603 | C<BEGIN> blocks containing calls to C<require> and C<import>; for |
| 6604 | instance, |
| 6605 | |
| 6606 | use strict 'refs'; |
| 6607 | |
| 6608 | turns into |
| 6609 | |
| 6610 | sub BEGIN { |
| 6611 | require strict; |
| 6612 | do { |
| 6613 | 'strict'->import('refs') |
| 6614 | }; |
| 6615 | } |
| 6616 | |
| 6617 | If I<LEVEL> is at least 7, C<if> statements will be translated into |
| 6618 | equivalent expressions using C<&&>, C<?:> and C<do {}>; for instance |
| 6619 | |
| 6620 | print 'hi' if $nice; |
| 6621 | if ($nice) { |
| 6622 | print 'hi'; |
| 6623 | } |
| 6624 | if ($nice) { |
| 6625 | print 'hi'; |
| 6626 | } else { |
| 6627 | print 'bye'; |
| 6628 | } |
| 6629 | |
| 6630 | turns into |
| 6631 | |
| 6632 | $nice and print 'hi'; |
| 6633 | $nice and do { print 'hi' }; |
| 6634 | $nice ? do { print 'hi' } : do { print 'bye' }; |
| 6635 | |
| 6636 | Long sequences of elsifs will turn into nested ternary operators, which |
| 6637 | B::Deparse doesn't know how to indent nicely. |
| 6638 | |
| 6639 | =back |
| 6640 | |
| 6641 | =head1 USING B::Deparse AS A MODULE |
| 6642 | |
| 6643 | =head2 Synopsis |
| 6644 | |
| 6645 | use B::Deparse; |
| 6646 | $deparse = B::Deparse->new("-p", "-sC"); |
| 6647 | $body = $deparse->coderef2text(\&func); |
| 6648 | eval "sub func $body"; # the inverse operation |
| 6649 | |
| 6650 | =head2 Description |
| 6651 | |
| 6652 | B::Deparse can also be used on a sub-by-sub basis from other perl |
| 6653 | programs. |
| 6654 | |
| 6655 | =head2 new |
| 6656 | |
| 6657 | $deparse = B::Deparse->new(OPTIONS) |
| 6658 | |
| 6659 | Create an object to store the state of a deparsing operation and any |
| 6660 | options. The options are the same as those that can be given on the |
| 6661 | command line (see L</OPTIONS>); options that are separated by commas |
| 6662 | after B<-MO=Deparse> should be given as separate strings. |
| 6663 | |
| 6664 | =head2 ambient_pragmas |
| 6665 | |
| 6666 | $deparse->ambient_pragmas(strict => 'all', '$[' => $[); |
| 6667 | |
| 6668 | The compilation of a subroutine can be affected by a few compiler |
| 6669 | directives, B<pragmas>. These are: |
| 6670 | |
| 6671 | =over 4 |
| 6672 | |
| 6673 | =item * |
| 6674 | |
| 6675 | use strict; |
| 6676 | |
| 6677 | =item * |
| 6678 | |
| 6679 | use warnings; |
| 6680 | |
| 6681 | =item * |
| 6682 | |
| 6683 | Assigning to the special variable $[ |
| 6684 | |
| 6685 | =item * |
| 6686 | |
| 6687 | use integer; |
| 6688 | |
| 6689 | =item * |
| 6690 | |
| 6691 | use bytes; |
| 6692 | |
| 6693 | =item * |
| 6694 | |
| 6695 | use utf8; |
| 6696 | |
| 6697 | =item * |
| 6698 | |
| 6699 | use re; |
| 6700 | |
| 6701 | =back |
| 6702 | |
| 6703 | Ordinarily, if you use B::Deparse on a subroutine which has |
| 6704 | been compiled in the presence of one or more of these pragmas, |
| 6705 | the output will include statements to turn on the appropriate |
| 6706 | directives. So if you then compile the code returned by coderef2text, |
| 6707 | it will behave the same way as the subroutine which you deparsed. |
| 6708 | |
| 6709 | However, you may know that you intend to use the results in a |
| 6710 | particular context, where some pragmas are already in scope. In |
| 6711 | this case, you use the B<ambient_pragmas> method to describe the |
| 6712 | assumptions you wish to make. |
| 6713 | |
| 6714 | Not all of the options currently have any useful effect. See |
| 6715 | L</BUGS> for more details. |
| 6716 | |
| 6717 | The parameters it accepts are: |
| 6718 | |
| 6719 | =over 4 |
| 6720 | |
| 6721 | =item strict |
| 6722 | |
| 6723 | Takes a string, possibly containing several values separated |
| 6724 | by whitespace. The special values "all" and "none" mean what you'd |
| 6725 | expect. |
| 6726 | |
| 6727 | $deparse->ambient_pragmas(strict => 'subs refs'); |
| 6728 | |
| 6729 | =item $[ |
| 6730 | |
| 6731 | Takes a number, the value of the array base $[. |
| 6732 | Obsolete: cannot be non-zero. |
| 6733 | |
| 6734 | =item bytes |
| 6735 | |
| 6736 | =item utf8 |
| 6737 | |
| 6738 | =item integer |
| 6739 | |
| 6740 | If the value is true, then the appropriate pragma is assumed to |
| 6741 | be in the ambient scope, otherwise not. |
| 6742 | |
| 6743 | =item re |
| 6744 | |
| 6745 | Takes a string, possibly containing a whitespace-separated list of |
| 6746 | values. The values "all" and "none" are special. It's also permissible |
| 6747 | to pass an array reference here. |
| 6748 | |
| 6749 | $deparser->ambient_pragmas(re => 'eval'); |
| 6750 | |
| 6751 | |
| 6752 | =item warnings |
| 6753 | |
| 6754 | Takes a string, possibly containing a whitespace-separated list of |
| 6755 | values. The values "all" and "none" are special, again. It's also |
| 6756 | permissible to pass an array reference here. |
| 6757 | |
| 6758 | $deparser->ambient_pragmas(warnings => [qw[void io]]); |
| 6759 | |
| 6760 | If one of the values is the string "FATAL", then all the warnings |
| 6761 | in that list will be considered fatal, just as with the B<warnings> |
| 6762 | pragma itself. Should you need to specify that some warnings are |
| 6763 | fatal, and others are merely enabled, you can pass the B<warnings> |
| 6764 | parameter twice: |
| 6765 | |
| 6766 | $deparser->ambient_pragmas( |
| 6767 | warnings => 'all', |
| 6768 | warnings => [FATAL => qw/void io/], |
| 6769 | ); |
| 6770 | |
| 6771 | See L<warnings> for more information about lexical warnings. |
| 6772 | |
| 6773 | =item hint_bits |
| 6774 | |
| 6775 | =item warning_bits |
| 6776 | |
| 6777 | These two parameters are used to specify the ambient pragmas in |
| 6778 | the format used by the special variables $^H and ${^WARNING_BITS}. |
| 6779 | |
| 6780 | They exist principally so that you can write code like: |
| 6781 | |
| 6782 | { my ($hint_bits, $warning_bits); |
| 6783 | BEGIN {($hint_bits, $warning_bits) = ($^H, ${^WARNING_BITS})} |
| 6784 | $deparser->ambient_pragmas ( |
| 6785 | hint_bits => $hint_bits, |
| 6786 | warning_bits => $warning_bits, |
| 6787 | '$[' => 0 + $[ |
| 6788 | ); } |
| 6789 | |
| 6790 | which specifies that the ambient pragmas are exactly those which |
| 6791 | are in scope at the point of calling. |
| 6792 | |
| 6793 | =item %^H |
| 6794 | |
| 6795 | This parameter is used to specify the ambient pragmas which are |
| 6796 | stored in the special hash %^H. |
| 6797 | |
| 6798 | =back |
| 6799 | |
| 6800 | =head2 coderef2text |
| 6801 | |
| 6802 | $body = $deparse->coderef2text(\&func) |
| 6803 | $body = $deparse->coderef2text(sub ($$) { ... }) |
| 6804 | |
| 6805 | Return source code for the body of a subroutine (a block, optionally |
| 6806 | preceded by a prototype in parens), given a reference to the |
| 6807 | sub. Because a subroutine can have no names, or more than one name, |
| 6808 | this method doesn't return a complete subroutine definition -- if you |
| 6809 | want to eval the result, you should prepend "sub subname ", or "sub " |
| 6810 | for an anonymous function constructor. Unless the sub was defined in |
| 6811 | the main:: package, the code will include a package declaration. |
| 6812 | |
| 6813 | =head1 BUGS |
| 6814 | |
| 6815 | =over 4 |
| 6816 | |
| 6817 | =item * |
| 6818 | |
| 6819 | The only pragmas to |
| 6820 | be completely supported are: C<use warnings>, |
| 6821 | C<use strict>, C<use bytes>, C<use integer> |
| 6822 | and C<use feature>. |
| 6823 | |
| 6824 | Excepting those listed above, we're currently unable to guarantee that |
| 6825 | B::Deparse will produce a pragma at the correct point in the program. |
| 6826 | (Specifically, pragmas at the beginning of a block often appear right |
| 6827 | before the start of the block instead.) |
| 6828 | Since the effects of pragmas are often lexically scoped, this can mean |
| 6829 | that the pragma holds sway over a different portion of the program |
| 6830 | than in the input file. |
| 6831 | |
| 6832 | =item * |
| 6833 | |
| 6834 | In fact, the above is a specific instance of a more general problem: |
| 6835 | we can't guarantee to produce BEGIN blocks or C<use> declarations in |
| 6836 | exactly the right place. So if you use a module which affects compilation |
| 6837 | (such as by over-riding keywords, overloading constants or whatever) |
| 6838 | then the output code might not work as intended. |
| 6839 | |
| 6840 | =item * |
| 6841 | |
| 6842 | Some constants don't print correctly either with or without B<-d>. |
| 6843 | For instance, neither B::Deparse nor Data::Dumper know how to print |
| 6844 | dual-valued scalars correctly, as in: |
| 6845 | |
| 6846 | use constant E2BIG => ($!=7); $y = E2BIG; print $y, 0+$y; |
| 6847 | |
| 6848 | use constant H => { "#" => 1 }; H->{"#"}; |
| 6849 | |
| 6850 | =item * |
| 6851 | |
| 6852 | An input file that uses source filtering probably won't be deparsed into |
| 6853 | runnable code, because it will still include the B<use> declaration |
| 6854 | for the source filtering module, even though the code that is |
| 6855 | produced is already ordinary Perl which shouldn't be filtered again. |
| 6856 | |
| 6857 | =item * |
| 6858 | |
| 6859 | Optimized-away statements are rendered as |
| 6860 | '???'. This includes statements that |
| 6861 | have a compile-time side-effect, such as the obscure |
| 6862 | |
| 6863 | my $x if 0; |
| 6864 | |
| 6865 | which is not, consequently, deparsed correctly. |
| 6866 | |
| 6867 | foreach my $i (@_) { 0 } |
| 6868 | => |
| 6869 | foreach my $i (@_) { '???' } |
| 6870 | |
| 6871 | =item * |
| 6872 | |
| 6873 | Lexical (my) variables declared in scopes external to a subroutine |
| 6874 | appear in coderef2text output text as package variables. This is a tricky |
| 6875 | problem, as perl has no native facility for referring to a lexical variable |
| 6876 | defined within a different scope, although L<PadWalker> is a good start. |
| 6877 | |
| 6878 | See also L<Data::Dump::Streamer>, which combines B::Deparse and |
| 6879 | L<PadWalker> to serialize closures properly. |
| 6880 | |
| 6881 | =item * |
| 6882 | |
| 6883 | There are probably many more bugs on non-ASCII platforms (EBCDIC). |
| 6884 | |
| 6885 | =back |
| 6886 | |
| 6887 | =head1 AUTHOR |
| 6888 | |
| 6889 | Stephen McCamant <smcc@CSUA.Berkeley.EDU>, based on an earlier version |
| 6890 | by Malcolm Beattie <mbeattie@sable.ox.ac.uk>, with contributions from |
| 6891 | Gisle Aas, James Duncan, Albert Dvornik, Robin Houston, Dave Mitchell, |
| 6892 | Hugo van der Sanden, Gurusamy Sarathy, Nick Ing-Simmons, and Rafael |
| 6893 | Garcia-Suarez. |
| 6894 | |
| 6895 | =cut |