| 1 | # B::DeparseTree::P526.pm | ||
| 2 | # Copyright (c) 1998-2000, 2002, 2003, 2004, 2005, 2006 Stephen McCamant. | ||
| 3 | # Copyright (c) 2015, 2017, 2018 Rocky Bernstein | ||
| 4 | # All rights reserved. | ||
| 5 | # This module is free software; you can redistribute and/or modify | ||
| 6 | # it under the same terms as Perl itself. | ||
| 7 | |||
| 8 | # This is based on the module B::Deparse (for perl 5.22) by Stephen McCamant. | ||
| 9 | # It has been extended save tree structure, and is addressible | ||
| 10 | # by opcode address. | ||
| 11 | |||
| 12 | # B::Parse in turn is based on the module of the same name by Malcolm Beattie, | ||
| 13 | # but essentially none of his code remains. | ||
| 14 | |||
| 15 | 7 | ▶ | use v5.26; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | |||
| 17 | 7 | ▶ | use rlib '../..'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 18 | |||
| 19 | package B::DeparseTree::P526; | ||
| 20 | 7 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | |||
| 22 | 7 | use B qw(class main_root main_start main_cv svref_2object opnumber perlstring | |
| 23 | OPf_KIDS OPf_REF OPf_STACKED OPf_SPECIAL OPf_MOD OPf_PARENS | ||
| 24 | OPf_WANT OPf_WANT_VOID OPf_WANT_SCALAR OPf_WANT_LIST | ||
| 25 | OPpEXISTS_SUB OPpSORT_NUMERIC OPpSORT_INTEGER OPpREPEAT_DOLIST | ||
| 26 | OPpLVAL_INTRO OPpOUR_INTRO OPpENTERSUB_AMPER OPpSLICE OPpCONST_BARE | ||
| 27 | OPpSORT_REVERSE OPpMULTIDEREF_EXISTS OPpMULTIDEREF_DELETE | ||
| 28 | OPpTRANS_SQUASH OPpTRANS_DELETE OPpTRANS_COMPLEMENT OPpTARGET_MY | ||
| 29 | PADNAMEt_OUTER | ||
| 30 | PMf_KEEP PMf_GLOBAL PMf_CONTINUE PMf_EVAL PMf_ONCE | ||
| 31 | PMf_MULTILINE PMf_SINGLELINE PMf_FOLD PMf_EXTENDED PMf_EXTENDED_MORE | ||
| 32 | SVf_ROK SVpad_OUR SVf_FAKE SVs_RMG SVs_SMG | ||
| 33 | SVpad_TYPED | ||
| 34 | CVf_METHOD | ||
| 35 | MDEREF_ACTION_MASK | ||
| 36 | MDEREF_AV_gvav_aelem | ||
| 37 | MDEREF_AV_gvsv_vivify_rv2av_aelem | ||
| 38 | MDEREF_AV_padav_aelem | ||
| 39 | MDEREF_AV_padsv_vivify_rv2av_aelem | ||
| 40 | MDEREF_AV_pop_rv2av_aelem | ||
| 41 | MDEREF_AV_vivify_rv2av_aelem | ||
| 42 | MDEREF_FLAG_last | ||
| 43 | MDEREF_HV_gvhv_helem | ||
| 44 | MDEREF_HV_gvsv_vivify_rv2hv_helem | ||
| 45 | MDEREF_HV_padhv_helem | ||
| 46 | MDEREF_HV_padsv_vivify_rv2hv_helem | ||
| 47 | MDEREF_HV_pop_rv2hv_helem | ||
| 48 | MDEREF_HV_vivify_rv2hv_helem | ||
| 49 | MDEREF_INDEX_MASK | ||
| 50 | MDEREF_INDEX_const | ||
| 51 | MDEREF_INDEX_gvsv | ||
| 52 | MDEREF_INDEX_none | ||
| 53 | MDEREF_INDEX_padsv | ||
| 54 | MDEREF_MASK | ||
| 55 | MDEREF_SHIFT | ||
| 56 | MDEREF_reload | ||
| 57 | OPpPADRANGE_COUNTSHIFT | ||
| 58 | OPpSPLIT_ASSIGN OPpSPLIT_LEX | ||
| 59 | 7 | ▶ | ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 60 | |||
| 61 | use B::DeparseTree::Common; | ||
| 62 | use B::DeparseTree::PP; | ||
| 63 | use B::Deparse; | ||
| 64 | |||
| 65 | # Copy unchanged functions from B::Deparse | ||
| 66 | *begin_is_use = *B::Deparse::begin_is_use; | ||
| 67 | *const_sv = *B::Deparse::const_sv; | ||
| 68 | *escape_re = *B::Deparse::escape_re; | ||
| 69 | *meth_pad_subs = *B::Deparse::pad_subs; | ||
| 70 | *meth_rclass_sv = *B::Deparse::meth_rclass_sv; | ||
| 71 | *meth_sv = *B::Deparse::meth_sv; | ||
| 72 | *padname_sv = *B::Deparse::padname_sv; | ||
| 73 | *rv2x = *B::Deparse::rv2x; | ||
| 74 | |||
| 75 | use strict; | ||
| 76 | use vars qw/$AUTOLOAD/; | ||
| 77 | use warnings (); | ||
| 78 | require feature; | ||
| 79 | |||
| 80 | our(@EXPORT, @ISA); | ||
| 81 | our $VERSION = '3.0.0'; | ||
| 82 | |||
| 83 | @ISA = qw(Exporter B::DeparseTree::Common); | ||
| 84 | @EXPORT = qw(compile); | ||
| 85 | |||
| 86 | BEGIN { | ||
| 87 | # List version-specific constants here. | ||
| 88 | # Easiest way to keep this code portable between version looks to | ||
| 89 | # be to fake up a dummy constant that will never actually be true. | ||
| 90 | foreach (qw(OPpSORT_INPLACE OPpSORT_DESCEND OPpITER_REVERSED OPpCONST_NOVER | ||
| 91 | OPpPAD_STATE PMf_SKIPWHITE RXf_SKIPWHITE | ||
| 92 | PMf_CHARSET PMf_KEEPCOPY PMf_NOCAPTURE CVf_ANONCONST | ||
| 93 | CVf_LOCKED OPpREVERSE_INPLACE OPpSUBSTR_REPL_FIRST | ||
| 94 | PMf_NONDESTRUCT OPpCONST_ARYBASE OPpEVAL_BYTES | ||
| 95 | OPpLVREF_TYPE OPpLVREF_SV OPpLVREF_AV OPpLVREF_HV | ||
| 96 | OPpLVREF_CV OPpLVREF_ELEM SVpad_STATE)) { | ||
| 97 | eval { B->import($_) }; | ||
| 98 | no strict 'refs'; | ||
| 99 | *{$_} = sub () {0} unless *{$_}{CODE}; | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | # See older source for inline comments recording changes prior to 2.0 | ||
| 104 | |||
| 105 | # In order to test modulie, we run this over Perl test suite. | ||
| 106 | # Then we run the test on the deparsed code. Slick, eh? | ||
| 107 | |||
| 108 | # Todo: | ||
| 109 | # (See also BUGS section at the end of this file) | ||
| 110 | # | ||
| 111 | # - cperl: AELEM_U, AELEMFAST_LEX_U, PERL_FAKE_SIGNATURE | ||
| 112 | # - finish tr/// changes | ||
| 113 | # - add option for even more parens (generalize \&foo change) | ||
| 114 | # - left/right context | ||
| 115 | # - copy comments (look at real text with $^P?) | ||
| 116 | # - avoid semis in one-statement blocks | ||
| 117 | # - associativity of &&=, ||=, ?: | ||
| 118 | # - ',' => '=>' (auto-unquote?) | ||
| 119 | # - break long lines ("\r" as discretionary break?) | ||
| 120 | # - configurable syntax highlighting: ANSI color, HTML, TeX, etc. | ||
| 121 | # - more style options: brace style, hex vs. octal, quotes, ... | ||
| 122 | # - print big ints as hex/octal instead of decimal (heuristic?) | ||
| 123 | # - handle 'my $x if 0'? | ||
| 124 | # - version using op_next instead of op_first/sibling? | ||
| 125 | # - avoid string copies (pass arrays, one big join?) | ||
| 126 | # - here-docs? | ||
| 127 | |||
| 128 | # Here are B::DeparseTree failure notes: | ||
| 129 | # Current test.deparse failures | ||
| 130 | # comp/hints 6 - location of BEGIN blocks wrt. block openings | ||
| 131 | # run/switchI 1 - missing -I switches entirely | ||
| 132 | # perl -Ifoo -e 'print @INC' | ||
| 133 | # op/caller 2 - warning mask propagates backwards before warnings::register | ||
| 134 | # 'use warnings; BEGIN {${^WARNING_BITS} eq "U"x12;} use warnings::register' | ||
| 135 | # op/getpid 2 - can't assign to shared my() declaration (threads only) | ||
| 136 | # 'my $x : shared = 5' | ||
| 137 | # op/override 7 - parens on overridden require change v-string interpretation | ||
| 138 | # 'BEGIN{*CORE::GLOBAL::require=sub {}} require v5.6' | ||
| 139 | # c.f. 'BEGIN { *f = sub {0} }; f 2' | ||
| 140 | # op/pat 774 - losing Unicode-ness of Latin1-only strings | ||
| 141 | # 'use charnames ":short"; $x="\N{latin:a with acute}"' | ||
| 142 | # op/recurse 12 - missing parens on recursive call makes it look like method | ||
| 143 | # 'sub f { f($x) }' | ||
| 144 | # op/subst 90 - inconsistent handling of utf8 under "use utf8" | ||
| 145 | # op/taint 29 - "use re 'taint'" deparsed in the wrong place wrt. block open | ||
| 146 | # op/tiehandle compile - "use strict" deparsed in the wrong place | ||
| 147 | # uni/tr_ several | ||
| 148 | # ext/B/t/xref 11 - line numbers when we add newlines to one-line subs | ||
| 149 | # ext/Data/Dumper/t/dumper compile | ||
| 150 | # ext/DB_file/several | ||
| 151 | # ext/Encode/several | ||
| 152 | # ext/Ernno/Errno warnings | ||
| 153 | # ext/IO/lib/IO/t/io_sel 23 | ||
| 154 | # ext/PerlIO/t/encoding compile | ||
| 155 | # ext/POSIX/t/posix 6 | ||
| 156 | # ext/Socket/Socket 8 | ||
| 157 | # dist/Storable/t/croak compile | ||
| 158 | # lib/Attribute/Handlers/t/multi compile | ||
| 159 | # lib/bignum/ several | ||
| 160 | # lib/charnames 35 | ||
| 161 | # lib/constant 32 | ||
| 162 | # lib/English 40 | ||
| 163 | # lib/ExtUtils/t/bytes 4 | ||
| 164 | # lib/File/DosGlob compile | ||
| 165 | # lib/Filter/Simple/t/data 1 | ||
| 166 | # lib/Math/BigInt/t/constant 1 | ||
| 167 | # lib/Net/t/config Deparse-warning | ||
| 168 | # lib/overload compile | ||
| 169 | # lib/Switch/ several | ||
| 170 | # lib/Symbol 4 | ||
| 171 | # lib/Test/Simple several | ||
| 172 | # lib/Term/Complete | ||
| 173 | # lib/Tie/File/t/29_downcopy 5 | ||
| 174 | # lib/vars 22 | ||
| 175 | |||
| 176 | # Object fields: | ||
| 177 | # | ||
| 178 | # in_coderef2text: | ||
| 179 | # True when deparsing via $deparse->coderef2text; false when deparsing the | ||
| 180 | # main program. | ||
| 181 | # | ||
| 182 | # avoid_local: | ||
| 183 | # (local($a), local($b)) and local($a, $b) have the same internal | ||
| 184 | # representation but the short form looks better. We notice we can | ||
| 185 | # use a large-scale local when checking the list, but need to prevent | ||
| 186 | # individual locals too. This hash holds the addresses of OPs that | ||
| 187 | # have already had their local-ness accounted for. The same thing | ||
| 188 | # is done with my(). | ||
| 189 | # | ||
| 190 | # curcv: | ||
| 191 | # CV for current sub (or main program) being deparsed | ||
| 192 | # | ||
| 193 | # curcvlex: | ||
| 194 | # Cached hash of lexical variables for curcv: keys are | ||
| 195 | # names prefixed with "m" or "o" (representing my/our), and | ||
| 196 | # each value is an array with two elements indicating the cop_seq | ||
| 197 | # of scopes in which a var of that name is valid and a third ele- | ||
| 198 | # ment referencing the pad name. | ||
| 199 | # | ||
| 200 | # curcop: | ||
| 201 | # COP for statement being deparsed | ||
| 202 | # | ||
| 203 | # curstash: | ||
| 204 | # name of the current package for deparsed code | ||
| 205 | # | ||
| 206 | # subs_todo: | ||
| 207 | # array of [cop_seq, CV, is_format?, name] for subs and formats we still | ||
| 208 | # want to deparse. The fourth element is a pad name thingy for lexical | ||
| 209 | # subs or a string for special blocks. For other subs, it is undef. For | ||
| 210 | # lexical subs, CV may be undef, indicating a stub declaration. | ||
| 211 | # | ||
| 212 | # protos_todo: | ||
| 213 | # as above, but [name, prototype] for subs that never got a GV | ||
| 214 | # | ||
| 215 | # subs_done, forms_done: | ||
| 216 | # keys are addresses of GVs for subs and formats we've already | ||
| 217 | # deparsed (or at least put into subs_todo) | ||
| 218 | # | ||
| 219 | # subs_declared | ||
| 220 | # keys are names of subs for which we've printed declarations. | ||
| 221 | # That means we can omit parentheses from the arguments. It also means we | ||
| 222 | # need to put CORE:: on core functions of the same name. | ||
| 223 | # | ||
| 224 | # in_subst_repl | ||
| 225 | # True when deparsing the replacement part of a substitution. | ||
| 226 | # | ||
| 227 | # in_refgen | ||
| 228 | # True when deparsing the argument to \. | ||
| 229 | # | ||
| 230 | # parens: -p | ||
| 231 | # linenums: -l | ||
| 232 | # unquote: -q | ||
| 233 | # cuddle: ' ' or '\n', depending on -sC | ||
| 234 | # indent_size: -si | ||
| 235 | # use_tabs: -sT | ||
| 236 | # ex_const: -sv | ||
| 237 | |||
| 238 | # A little explanation of how precedence contexts and associativity | ||
| 239 | # work: | ||
| 240 | # | ||
| 241 | # deparse() calls each per-op subroutine with an argument $cx (short | ||
| 242 | # for context, but not the same as the cx* in the perl core), which is | ||
| 243 | # a number describing the op's parents in terms of precedence, whether | ||
| 244 | # they're inside an expression or at statement level, etc. (see | ||
| 245 | # chart below). When ops with children call deparse on them, they pass | ||
| 246 | # along their precedence. Fractional values are used to implement | ||
| 247 | # associativity ('($x + $y) + $z' => '$x + $y + $y') and related | ||
| 248 | # parentheses hacks. The major disadvantage of this scheme is that | ||
| 249 | # it doesn't know about right sides and left sides, so say if you | ||
| 250 | # assign a listop to a variable, it can't tell it's allowed to leave | ||
| 251 | # the parens off the listop. | ||
| 252 | |||
| 253 | # Precedences: | ||
| 254 | # 26 [TODO] inside interpolation context ("") | ||
| 255 | # 25 left terms and list operators (leftward) | ||
| 256 | # 24 left -> | ||
| 257 | # 23 nonassoc ++ -- | ||
| 258 | # 22 right ** | ||
| 259 | # 21 right ! ~ \ and unary + and - | ||
| 260 | # 20 left =~ !~ | ||
| 261 | # 19 left * / % x | ||
| 262 | # 18 left + - . | ||
| 263 | # 17 left << >> | ||
| 264 | # 16 nonassoc named unary operators | ||
| 265 | # 15 nonassoc < > <= >= lt gt le ge | ||
| 266 | # 14 nonassoc == != <=> eq ne cmp | ||
| 267 | # 13 left & | ||
| 268 | # 12 left | ^ | ||
| 269 | # 11 left && | ||
| 270 | # 10 left || | ||
| 271 | # 9 nonassoc .. ... | ||
| 272 | # 8 right ?: | ||
| 273 | # 7 right = += -= *= etc. | ||
| 274 | # 6 left , => | ||
| 275 | # 5 nonassoc list operators (rightward) | ||
| 276 | # 4 right not | ||
| 277 | # 3 left and | ||
| 278 | # 2 left or xor | ||
| 279 | # 1 statement modifiers | ||
| 280 | # 0.5 statements, but still print scopes as do { ... } | ||
| 281 | # 0 statement level | ||
| 282 | # -1 format body | ||
| 283 | |||
| 284 | # Nonprinting characters with special meaning: | ||
| 285 | # \cS - steal parens (see maybe_parens_unop) | ||
| 286 | # \n - newline and indent | ||
| 287 | # \t - increase indent | ||
| 288 | # \b - decrease indent ('outdent') | ||
| 289 | # \f - flush left (no indent) | ||
| 290 | # \cK - kill following semicolon, if any | ||
| 291 | |||
| 292 | # Semicolon handling: | ||
| 293 | # - Individual statements are not deparsed with trailing semicolons. | ||
| 294 | # (If necessary, \cK is tacked on to the end.) | ||
| 295 | # - Whatever code joins statements together or emits them (lineseq, | ||
| 296 | # scopeop, deparse_root) is responsible for adding semicolons where | ||
| 297 | # necessary. | ||
| 298 | # - use statements are deparsed with trailing semicolons because they are | ||
| 299 | # immediately concatenated with the following statement. | ||
| 300 | # - indent() removes semicolons wherever it sees \cK. | ||
| 301 | |||
| 302 | |||
| 303 | BEGIN { for (qw[ const stringify rv2sv list glob pushmark null aelem | ||
| 304 | nextstate dbstate rv2av rv2hv helem custom ]) { | ||
| 305 | eval "sub OP_\U$_ () { " . opnumber($_) . "}" | ||
| 306 | }} | ||
| 307 | |||
| 308 | sub deparse_format($$$) | ||
| 309 | { | ||
| 310 | my ($self, $form, $parent) = @_; | ||
| 311 | my @texts; | ||
| 312 | local($self->{'curcv'}) = $form; | ||
| 313 | local($self->{'curcvlex'}); | ||
| 314 | local($self->{'in_format'}) = 1; | ||
| 315 | local(@$self{qw'curstash warnings hints hinthash'}) | ||
| 316 | = @$self{qw'curstash warnings hints hinthash'}; | ||
| 317 | my $op = $form->ROOT; | ||
| 318 | local $B::overlay = {}; | ||
| 319 | $self->pessimise($op, $form->START); | ||
| 320 | my $info = { | ||
| 321 | op => $op, | ||
| 322 | parent => $parent, | ||
| 323 | cop => $self->{'curcop'} | ||
| 324 | }; | ||
| 325 | $self->{optree}{$$op} = $info; | ||
| 326 | |||
| 327 | if ($op->first->name eq 'stub' || $op->first->name eq 'nextstate') { | ||
| 328 | my $info->{text} = "\f."; | ||
| 329 | return $info; | ||
| 330 | } | ||
| 331 | |||
| 332 | $op->{other_ops} = [$op->first]; | ||
| 333 | $op = $op->first->first; # skip leavewrite, lineseq | ||
| 334 | my $kid; | ||
| 335 | while (not null $op) { | ||
| 336 | push @{$op->{other_ops}}, $op; | ||
| 337 | $op = $op->sibling; # skip nextstate | ||
| 338 | my @body; | ||
| 339 | push @{$op->{other_ops}}, $op->first; | ||
| 340 | $kid = $op->first->sibling; # skip a pushmark | ||
| 341 | push @texts, "\f".$self->const_sv($kid)->PV; | ||
| 342 | push @{$op->{other_ops}}, $kid; | ||
| 343 | $kid = $kid->sibling; | ||
| 344 | for (; not null $kid; $kid = $kid->sibling) { | ||
| 345 | push @body, $self->deparse($kid, -1, $op); | ||
| 346 | $body[-1] =~ s/;\z//; | ||
| 347 | } | ||
| 348 | push @texts, "\f".$self->combine2str("\n", \@body) if @body; | ||
| 349 | $op = $op->sibling; | ||
| 350 | } | ||
| 351 | |||
| 352 | $info->{text} = $self->combine2str(\@texts) . "\f."; | ||
| 353 | $info->{texts} = \@texts; | ||
| 354 | return $info; | ||
| 355 | } | ||
| 356 | |||
| 357 | sub ambient_pragmas { | ||
| 358 | my $self = shift; | ||
| 359 | my ($arybase, $hint_bits, $warning_bits, $hinthash) = (0, 0); | ||
| 360 | |||
| 361 | while (@_ > 1) { | ||
| 362 | my $name = shift(); | ||
| 363 | my $val = shift(); | ||
| 364 | |||
| 365 | if ($name eq 'strict') { | ||
| 366 | require strict; | ||
| 367 | |||
| 368 | if ($val eq 'none') { | ||
| 369 | $hint_bits &= $strict_bits{$_} for qw/refs subs vars/; | ||
| 370 | next(); | ||
| 371 | } | ||
| 372 | |||
| 373 | my @names; | ||
| 374 | if ($val eq "all") { | ||
| 375 | @names = qw/refs subs vars/; | ||
| 376 | } | ||
| 377 | elsif (ref $val) { | ||
| 378 | @names = @$val; | ||
| 379 | } | ||
| 380 | else { | ||
| 381 | @names = split' ', $val; | ||
| 382 | } | ||
| 383 | $hint_bits |= $strict_bits{$_} for @names; | ||
| 384 | } | ||
| 385 | |||
| 386 | elsif ($name eq '$[') { | ||
| 387 | if (OPpCONST_ARYBASE) { | ||
| 388 | $arybase = $val; | ||
| 389 | } else { | ||
| 390 | croak "\$[ can't be non-zero on this perl" unless $val == 0; | ||
| 391 | } | ||
| 392 | } | ||
| 393 | |||
| 394 | elsif ($name eq 'integer' | ||
| 395 | || $name eq 'bytes' | ||
| 396 | || $name eq 'utf8') { | ||
| 397 | require "$name.pm"; | ||
| 398 | if ($val) { | ||
| 399 | $hint_bits |= ${$::{"${name}::"}{"hint_bits"}}; | ||
| 400 | } | ||
| 401 | else { | ||
| 402 | $hint_bits &= ~${$::{"${name}::"}{"hint_bits"}}; | ||
| 403 | } | ||
| 404 | } | ||
| 405 | |||
| 406 | elsif ($name eq 're') { | ||
| 407 | require re; | ||
| 408 | if ($val eq 'none') { | ||
| 409 | $hint_bits &= ~re::bits(qw/taint eval/); | ||
| 410 | next(); | ||
| 411 | } | ||
| 412 | |||
| 413 | my @names; | ||
| 414 | if ($val eq 'all') { | ||
| 415 | @names = qw/taint eval/; | ||
| 416 | } | ||
| 417 | elsif (ref $val) { | ||
| 418 | @names = @$val; | ||
| 419 | } | ||
| 420 | else { | ||
| 421 | @names = split' ',$val; | ||
| 422 | } | ||
| 423 | $hint_bits |= re::bits(@names); | ||
| 424 | } | ||
| 425 | |||
| 426 | elsif ($name eq 'warnings') { | ||
| 427 | if ($val eq 'none') { | ||
| 428 | $warning_bits = $warnings::NONE; | ||
| 429 | next(); | ||
| 430 | } | ||
| 431 | |||
| 432 | my @names; | ||
| 433 | if (ref $val) { | ||
| 434 | @names = @$val; | ||
| 435 | } | ||
| 436 | else { | ||
| 437 | @names = split/\s+/, $val; | ||
| 438 | } | ||
| 439 | |||
| 440 | $warning_bits = $warnings::NONE if !defined ($warning_bits); | ||
| 441 | $warning_bits |= warnings::bits(@names); | ||
| 442 | } | ||
| 443 | |||
| 444 | elsif ($name eq 'warning_bits') { | ||
| 445 | $warning_bits = $val; | ||
| 446 | } | ||
| 447 | |||
| 448 | elsif ($name eq 'hint_bits') { | ||
| 449 | $hint_bits = $val; | ||
| 450 | } | ||
| 451 | |||
| 452 | elsif ($name eq '%^H') { | ||
| 453 | $hinthash = $val; | ||
| 454 | } | ||
| 455 | |||
| 456 | else { | ||
| 457 | croak "Unknown pragma type: $name"; | ||
| 458 | } | ||
| 459 | } | ||
| 460 | if (@_) { | ||
| 461 | croak "The ambient_pragmas method expects an even number of args"; | ||
| 462 | } | ||
| 463 | |||
| 464 | $self->{'ambient_arybase'} = $arybase; | ||
| 465 | $self->{'ambient_warnings'} = $warning_bits; | ||
| 466 | $self->{'ambient_hints'} = $hint_bits; | ||
| 467 | $self->{'ambient_hinthash'} = $hinthash; | ||
| 468 | } | ||
| 469 | |||
| 470 | # Sort of like maybe_parens in that we may possibly add (). However we take | ||
| 471 | # an op rather than text, and return a tree node. Also, we get around | ||
| 472 | # the 'if it looks like a function' rule. | ||
| 473 | sub maybe_parens_unop($$$$$) | ||
| 474 | { | ||
| 475 | my $self = shift; | ||
| 476 | my($name, $op, $cx, $parent) = @_; | ||
| 477 | my $info = $self->deparse($op, 1, $parent); | ||
| 478 | my $text = $info->{text}; | ||
| 479 | if ($name eq "umask" && $info->{text} =~ /^\d+$/) { | ||
| 480 | $text = sprintf("%#o", $text); | ||
| 481 | } | ||
| 482 | if ($cx > 16 or $self->{'parens'}) { | ||
| 483 | return info_from_list($op, $self, [$self->keyword($name), '(', $text, ')'], | ||
| 484 | '', 'maybe_parens_unop_parens', {body => [$info]}); | ||
| 485 | } else { | ||
| 486 | $name = $self->keyword($name); | ||
| 487 | if (substr($text, 0, 1) eq "\cS") { | ||
| 488 | # use op's parens | ||
| 489 | return info_from_list($op, $self,[$name, substr($text, 1)], | ||
| 490 | '', 'maybe_parens_unop_cS', {body => [$info]}); | ||
| 491 | } elsif (substr($text, 0, 1) eq "(") { | ||
| 492 | # avoid looks-like-a-function trap with extra parens | ||
| 493 | # ('+' can lead to ambiguities) | ||
| 494 | return info_from_list($op, $self, [$name, '(', $text, ')'], | ||
| 495 | '', "maybe_parens_unop_fn", {}); | ||
| 496 | } else { | ||
| 497 | return info_from_list($op, $self,[$name, $text], ' ', | ||
| 498 | 'maybe_parens_unop', {body => [$info]}); | ||
| 499 | } | ||
| 500 | } | ||
| 501 | Carp::confess("unhandled condition in maybe_parens_unop"); | ||
| 502 | } | ||
| 503 | |||
| 504 | sub maybe_parens_func($$$$$) | ||
| 505 | { | ||
| 506 | my($self, $func, $params, $cx, $prec) = @_; | ||
| 507 | if ($prec <= $cx or substr($params, 0, 1) eq "(" or $self->{'parens'}) { | ||
| 508 | return ($func, '(', $params, ')'); | ||
| 509 | } else { | ||
| 510 | return ($func, ' ', $params); | ||
| 511 | } | ||
| 512 | } | ||
| 513 | |||
| 514 | # FIXME this doesn't return a String! | ||
| 515 | sub maybe_local_str | ||
| 516 | { | ||
| 517 | my($self, $op, $cx, $text) = @_; | ||
| 518 | my $our_intro = ($op->name =~ /^(gv|rv2)[ash]v$/) ? OPpOUR_INTRO : 0; | ||
| 519 | if ($op->private & (OPpLVAL_INTRO|$our_intro) | ||
| 520 | and not $self->{'avoid_local'}{$$op}) { | ||
| 521 | my $our_local = ($op->private & OPpLVAL_INTRO) ? "local" : "our"; | ||
| 522 | if( $our_local eq 'our' ) { | ||
| 523 | if ( $text !~ /^\W(\w+::)*\w+\z/ | ||
| 524 | and !utf8::decode($text) || $text !~ /^\W(\w+::)*\w+\z/ | ||
| 525 | ) { | ||
| 526 | die "Unexpected our($text)\n"; | ||
| 527 | } | ||
| 528 | $text =~ s/(\w+::)+//; | ||
| 529 | } | ||
| 530 | if (want_scalar($op)) { | ||
| 531 | return info_from_list($op, $self, [$our_local, $text], ' ', | ||
| 532 | 'maybe_local_scalar', {}); | ||
| 533 | } else { | ||
| 534 | my @texts = $self->maybe_parens_func($our_local, $text, $cx, 16); | ||
| 535 | return info_from_list($op, $self, \@texts, '', 'maybe_local_array', | ||
| 536 | {}); | ||
| 537 | } | ||
| 538 | } else { | ||
| 539 | return info_from_text($op, $self, $text, 'maybe_local', {}); | ||
| 540 | } | ||
| 541 | } | ||
| 542 | |||
| 543 | # FIXME: This is weird. Regularize var_info | ||
| 544 | sub maybe_local { | ||
| 545 | my($self, $op, $cx, $var_info) = @_; | ||
| 546 | $var_info->{parent} = $$op; | ||
| 547 | return maybe_local_str($self, $op, $cx, $var_info->{text}); | ||
| 548 | } | ||
| 549 | |||
| 550 | sub maybe_my { | ||
| 551 | my $self = shift; | ||
| 552 | my($op, $cx, $text, $forbid_parens) = @_; | ||
| 553 | if ($op->private & OPpLVAL_INTRO and not $self->{'avoid_local'}{$$op}) { | ||
| 554 | my $my_str = $op->private & OPpPAD_STATE | ||
| 555 | ? $self->keyword("state") | ||
| 556 | : "my"; | ||
| 557 | if ($forbid_parens || want_scalar($op)) { | ||
| 558 | return info_from_list($op, $self, [$my_str, $text], ' ', | ||
| 559 | 'maybe_my_no_parens', {}); | ||
| 560 | } else { | ||
| 561 | return info_from_list($op, $self, [$my_str, $text], ' ', | ||
| 562 | 'maybe_my_parens', | ||
| 563 | {maybe_parens => [$self, $cx, 16]}); | ||
| 564 | } | ||
| 565 | } else { | ||
| 566 | return info_from_text($op, $self, $text, 'maybe_my_avoid_local', {}); | ||
| 567 | } | ||
| 568 | } | ||
| 569 | |||
| 570 | # The following OPs don't have functions: | ||
| 571 | |||
| 572 | # pp_padany -- does not exist after parsing | ||
| 573 | |||
| 574 | sub AUTOLOAD { | ||
| 575 | if ($AUTOLOAD =~ s/^.*::pp_//) { | ||
| 576 | warn "unexpected OP_". | ||
| 577 | ($_[1]->type == OP_CUSTOM ? "CUSTOM ($AUTOLOAD)" : uc $AUTOLOAD); | ||
| 578 | return "XXX"; | ||
| 579 | } else { | ||
| 580 | Carp::confess "Undefined subroutine $AUTOLOAD called"; | ||
| 581 | } | ||
| 582 | } | ||
| 583 | |||
| 584 | sub DESTROY {} # Do not AUTOLOAD | ||
| 585 | |||
| 586 | # The BEGIN {} is used here because otherwise this code isn't executed | ||
| 587 | # when you run B::Deparse on itself. | ||
| 588 | my %globalnames; | ||
| 589 | BEGIN { map($globalnames{$_}++, "SIG", "STDIN", "STDOUT", "STDERR", "INC", | ||
| 590 | "ENV", "ARGV", "ARGVOUT", "_"); } | ||
| 591 | |||
| 592 | sub gv_name { | ||
| 593 | my $self = shift; | ||
| 594 | my $gv = shift; | ||
| 595 | my $raw = shift; | ||
| 596 | # Carp::confess() unless ref($gv) eq "B::GV"; | ||
| 597 | my $cv = $gv->FLAGS & SVf_ROK ? $gv->RV : 0; | ||
| 598 | my $stash = ($cv || $gv)->STASH->NAME; | ||
| 599 | my $name = $raw | ||
| 600 | ? $cv ? $cv->NAME_HEK || $cv->GV->NAME : $gv->NAME | ||
| 601 | : $cv | ||
| 602 | ? B::safename($cv->NAME_HEK || $cv->GV->NAME) | ||
| 603 | : $gv->SAFENAME; | ||
| 604 | if ($stash eq 'main' && $name =~ /^::/) { | ||
| 605 | $stash = '::'; | ||
| 606 | } | ||
| 607 | elsif (($stash eq 'main' | ||
| 608 | && ($globalnames{$name} || $name =~ /^[^A-Za-z_:]/)) | ||
| 609 | or ($stash eq $self->{'curstash'} && !$globalnames{$name} | ||
| 610 | && ($stash eq 'main' || $name !~ /::/)) | ||
| 611 | ) | ||
| 612 | { | ||
| 613 | $stash = ""; | ||
| 614 | } else { | ||
| 615 | $stash = $stash . "::"; | ||
| 616 | } | ||
| 617 | if (!$raw and $name =~ /^(\^..|{)/) { | ||
| 618 | $name = "{$name}"; # ${^WARNING_BITS}, etc and ${ | ||
| 619 | } | ||
| 620 | return $stash . $name; | ||
| 621 | } | ||
| 622 | |||
| 623 | # Return the name to use for a stash variable. | ||
| 624 | # If a lexical with the same name is in scope, or | ||
| 625 | # if strictures are enabled, it may need to be | ||
| 626 | # fully-qualified. | ||
| 627 | sub stash_variable { | ||
| 628 | my ($self, $prefix, $name, $cx) = @_; | ||
| 629 | |||
| 630 | $name = $self->info2str($name); | ||
| 631 | return "$prefix$name" if $name =~ /::/; | ||
| 632 | |||
| 633 | unless ($prefix eq '$' || $prefix eq '@' || $prefix eq '&' || #' | ||
| 634 | $prefix eq '%' || $prefix eq '$#') { | ||
| 635 | return "$prefix$name"; | ||
| 636 | } | ||
| 637 | |||
| 638 | if ($name =~ /^[^[:alpha:]_+-]$/) { | ||
| 639 | if (defined $cx && $cx == 26) { | ||
| 640 | if ($prefix eq '@') { | ||
| 641 | return "$prefix\{$name}"; | ||
| 642 | } | ||
| 643 | elsif ($name eq '#') { return '${#}' } # "${#}a" vs "$#a" | ||
| 644 | } | ||
| 645 | if ($prefix eq '$#') { | ||
| 646 | return "\$#{$name}"; | ||
| 647 | } | ||
| 648 | } | ||
| 649 | |||
| 650 | return $prefix . $self->maybe_qualify($prefix, $name); | ||
| 651 | } | ||
| 652 | |||
| 653 | sub lex_in_scope { | ||
| 654 | my ($self, $name, $our) = @_; | ||
| 655 | substr $name, 0, 0, = $our ? 'o' : 'm'; # our/my | ||
| 656 | $self->populate_curcvlex() if !defined $self->{'curcvlex'}; | ||
| 657 | |||
| 658 | return 0 if !defined($self->{'curcop'}); | ||
| 659 | my $seq = $self->{'curcop'}->cop_seq; | ||
| 660 | return 0 if !exists $self->{'curcvlex'}{$name}; | ||
| 661 | for my $a (@{$self->{'curcvlex'}{$name}}) { | ||
| 662 | my ($st, $en) = @$a; | ||
| 663 | return 1 if $seq > $st && $seq <= $en; | ||
| 664 | } | ||
| 665 | return 0; | ||
| 666 | } | ||
| 667 | |||
| 668 | sub populate_curcvlex { | ||
| 669 | my $self = shift; | ||
| 670 | for (my $cv = $self->{'curcv'}; class($cv) eq "CV"; $cv = $cv->OUTSIDE) { | ||
| 671 | my $padlist = $cv->PADLIST; | ||
| 672 | # an undef CV still in lexical chain | ||
| 673 | next if class($padlist) eq "SPECIAL"; | ||
| 674 | my @padlist = $padlist->ARRAY; | ||
| 675 | my @ns = $padlist[0]->ARRAY; | ||
| 676 | |||
| 677 | for (my $i=0; $i<@ns; ++$i) { | ||
| 678 | next if class($ns[$i]) eq "SPECIAL"; | ||
| 679 | if (class($ns[$i]) eq "PV") { | ||
| 680 | # Probably that pesky lexical @_ | ||
| 681 | next; | ||
| 682 | } | ||
| 683 | my $name = $ns[$i]->PVX; | ||
| 684 | next unless defined $name; | ||
| 685 | my ($seq_st, $seq_en) = | ||
| 686 | ($ns[$i]->FLAGS & SVf_FAKE) | ||
| 687 | ? (0, 999999) | ||
| 688 | : ($ns[$i]->COP_SEQ_RANGE_LOW, $ns[$i]->COP_SEQ_RANGE_HIGH); | ||
| 689 | |||
| 690 | push @{$self->{'curcvlex'}{ | ||
| 691 | ($ns[$i]->FLAGS & SVpad_OUR ? 'o' : 'm') . $name | ||
| 692 | }}, [$seq_st, $seq_en, $ns[$i]]; | ||
| 693 | } | ||
| 694 | } | ||
| 695 | } | ||
| 696 | |||
| 697 | sub find_scope_st { ((find_scope(@_))[0]); } | ||
| 698 | sub find_scope_en { ((find_scope(@_))[1]); } | ||
| 699 | |||
| 700 | # Recurses down the tree, looking for pad variable introductions and COPs | ||
| 701 | sub find_scope { | ||
| 702 | my ($self, $op, $scope_st, $scope_en) = @_; | ||
| 703 | carp("Undefined op in find_scope") if !defined $op; | ||
| 704 | return ($scope_st, $scope_en) unless $op->flags & OPf_KIDS; | ||
| 705 | |||
| 706 | my @queue = ($op); | ||
| 707 | while(my $op = shift @queue ) { | ||
| 708 | for (my $o=$op->first; $$o; $o=$o->sibling) { | ||
| 709 | if ($o->name =~ /^pad.v$/ && $o->private & OPpLVAL_INTRO) { | ||
| 710 | my $s = int($self->padname_sv($o->targ)->COP_SEQ_RANGE_LOW); | ||
| 711 | my $e = $self->padname_sv($o->targ)->COP_SEQ_RANGE_HIGH; | ||
| 712 | $scope_st = $s if !defined($scope_st) || $s < $scope_st; | ||
| 713 | $scope_en = $e if !defined($scope_en) || $e > $scope_en; | ||
| 714 | return ($scope_st, $scope_en); | ||
| 715 | } | ||
| 716 | elsif (is_state($o)) { | ||
| 717 | my $c = $o->cop_seq; | ||
| 718 | $scope_st = $c if !defined($scope_st) || $c < $scope_st; | ||
| 719 | $scope_en = $c if !defined($scope_en) || $c > $scope_en; | ||
| 720 | return ($scope_st, $scope_en); | ||
| 721 | } | ||
| 722 | elsif ($o->flags & OPf_KIDS) { | ||
| 723 | unshift (@queue, $o); | ||
| 724 | } | ||
| 725 | } | ||
| 726 | } | ||
| 727 | |||
| 728 | return ($scope_st, $scope_en); | ||
| 729 | } | ||
| 730 | |||
| 731 | # Returns a list of subs which should be inserted before the COP | ||
| 732 | sub cop_subs { | ||
| 733 | my ($self, $op, $out_seq) = @_; | ||
| 734 | my $seq = $op->cop_seq; | ||
| 735 | # If we have nephews, then our sequence number indicates | ||
| 736 | # the cop_seq of the end of some sort of scope. | ||
| 737 | if (class($op->sibling) ne "NULL" && $op->sibling->flags & OPf_KIDS | ||
| 738 | and my $nseq = $self->find_scope_st($op->sibling) ) { | ||
| 739 | $seq = $nseq; | ||
| 740 | } | ||
| 741 | $seq = $out_seq if defined($out_seq) && $out_seq < $seq; | ||
| 742 | return $self->seq_subs($seq); | ||
| 743 | } | ||
| 744 | |||
| 745 | my %feature_keywords = ( | ||
| 746 | # keyword => 'feature', | ||
| 747 | state => 'state', | ||
| 748 | say => 'say', | ||
| 749 | given => 'switch', | ||
| 750 | when => 'switch', | ||
| 751 | default => 'switch', | ||
| 752 | break => 'switch', | ||
| 753 | evalbytes=>'evalbytes', | ||
| 754 | __SUB__ => '__SUB__', | ||
| 755 | fc => 'fc', | ||
| 756 | ); | ||
| 757 | |||
| 758 | # keywords that are strong and also have a prototype | ||
| 759 | # | ||
| 760 | my %strong_proto_keywords = map { $_ => 1 } qw( | ||
| 761 | pos | ||
| 762 | prototype | ||
| 763 | scalar | ||
| 764 | study | ||
| 765 | undef | ||
| 766 | ); | ||
| 767 | |||
| 768 | sub feature_enabled { | ||
| 769 | my($self,$name) = @_; | ||
| 770 | my $hh; | ||
| 771 | my $hints = $self->{hints} & $feature::hint_mask; | ||
| 772 | if ($hints && $hints != $feature::hint_mask) { | ||
| 773 | $hh = _features_from_bundle($hints); | ||
| 774 | } | ||
| 775 | elsif ($hints) { $hh = $self->{'hinthash'} } | ||
| 776 | return $hh && $hh->{"feature_$feature_keywords{$name}"} | ||
| 777 | } | ||
| 778 | |||
| 779 | sub keyword { | ||
| 780 | my $self = shift; | ||
| 781 | my $name = shift; | ||
| 782 | return $name if $name =~ /^CORE::/; # just in case | ||
| 783 | if (exists $feature_keywords{$name}) { | ||
| 784 | return "CORE::$name" if not $self->feature_enabled($name); | ||
| 785 | } | ||
| 786 | # This sub may be called for a program that has no nextstate ops. In | ||
| 787 | # that case we may have a lexical sub named no/use/sub in scope but | ||
| 788 | # but $self->lex_in_scope will return false because it depends on the | ||
| 789 | # current nextstate op. So we need this alternate method if there is | ||
| 790 | # no current cop. | ||
| 791 | if (!$self->{'curcop'}) { | ||
| 792 | $self->populate_curcvlex() if !defined $self->{'curcvlex'}; | ||
| 793 | return "CORE::$name" if exists $self->{'curcvlex'}{"m&$name"} | ||
| 794 | || exists $self->{'curcvlex'}{"o&$name"}; | ||
| 795 | } elsif ($self->lex_in_scope("&$name") | ||
| 796 | || $self->lex_in_scope("&$name", 1)) { | ||
| 797 | return "CORE::$name"; | ||
| 798 | } | ||
| 799 | if ($strong_proto_keywords{$name} | ||
| 800 | || ($name !~ /^(?:chom?p|do|exec|glob|s(?:elect|ystem))\z/ | ||
| 801 | && !defined eval{prototype "CORE::$name"}) | ||
| 802 | ) { return $name } | ||
| 803 | if ( | ||
| 804 | exists $self->{subs_declared}{$name} | ||
| 805 | or | ||
| 806 | exists &{"$self->{curstash}::$name"} | ||
| 807 | ) { | ||
| 808 | return "CORE::$name" | ||
| 809 | } | ||
| 810 | return $name; | ||
| 811 | } | ||
| 812 | |||
| 813 | sub pp_not | ||
| 814 | { | ||
| 815 | my($self, $op, $cx) = @_; | ||
| 816 | if ($cx <= 4) { | ||
| 817 | $self->listop($op, $cx, "not", $op->first); | ||
| 818 | } else { | ||
| 819 | $self->pfixop($op, $cx, "!", 21); | ||
| 820 | } | ||
| 821 | } | ||
| 822 | |||
| 823 | # Note: maybe_local things can't be moved to PP yet. | ||
| 824 | sub pp_pos { maybe_local(@_, unop(@_, "pos")) } | ||
| 825 | sub pp_length { maybe_targmy(@_, \&unop, "length") } | ||
| 826 | sub pp_ord { maybe_targmy(@_, \&unop, "ord") } | ||
| 827 | sub pp_chr { maybe_targmy(@_, \&unop, "chr") } | ||
| 828 | |||
| 829 | sub pp_each { unop(@_, "each") } | ||
| 830 | sub pp_values { unop(@_, "values") } | ||
| 831 | sub pp_keys { unop(@_, "keys") } | ||
| 832 | { no strict 'refs'; *{"pp_r$_"} = *{"pp_$_"} for qw< keys each values >; } | ||
| 833 | sub pp_boolkeys | ||
| 834 | { | ||
| 835 | # no name because its an optimisation op that has no keyword | ||
| 836 | unop(@_,""); | ||
| 837 | } | ||
| 838 | sub pp_aeach { unop(@_, "each") } | ||
| 839 | sub pp_avalues { unop(@_, "values") } | ||
| 840 | sub pp_akeys { unop(@_, "keys") } | ||
| 841 | sub pp_pop { unop(@_, "pop") } | ||
| 842 | sub pp_shift { unop(@_, "shift") } | ||
| 843 | |||
| 844 | sub pp_caller { unop(@_, "caller") } | ||
| 845 | sub pp_reset { unop(@_, "reset") } | ||
| 846 | sub pp_exit { unop(@_, "exit") } | ||
| 847 | sub pp_prototype { unop(@_, "prototype") } | ||
| 848 | |||
| 849 | sub pp_close { unop(@_, "close") } | ||
| 850 | sub pp_fileno { unop(@_, "fileno") } | ||
| 851 | sub pp_umask { unop(@_, "umask") } | ||
| 852 | sub pp_untie { unop(@_, "untie") } | ||
| 853 | sub pp_tied { unop(@_, "tied") } | ||
| 854 | sub pp_dbmclose { unop(@_, "dbmclose") } | ||
| 855 | sub pp_getc { unop(@_, "getc") } | ||
| 856 | sub pp_eof { unop(@_, "eof") } | ||
| 857 | sub pp_tell { unop(@_, "tell") } | ||
| 858 | sub pp_getsockname { unop(@_, "getsockname") } | ||
| 859 | sub pp_getpeername { unop(@_, "getpeername") } | ||
| 860 | |||
| 861 | sub pp_alarm { unop(@_, "alarm") } | ||
| 862 | |||
| 863 | # FIXME: | ||
| 864 | # Different in 5.20. Go over differences to see if okay in 5.20. | ||
| 865 | sub pp_chdir { | ||
| 866 | my ($self, $op, $cx) = @_; | ||
| 867 | if (($op->flags & (OPf_SPECIAL|OPf_KIDS)) == (OPf_SPECIAL|OPf_KIDS)) { | ||
| 868 | my $kw = $self->keyword("chdir"); | ||
| 869 | my $kid = $self->const_sv($op->first)->PV; | ||
| 870 | my $code = $kw | ||
| 871 | . ($cx >= 16 || $self->{'parens'} ? "($kid)" : " $kid"); | ||
| 872 | maybe_targmy(@_, sub { $_[3] }, $code); | ||
| 873 | } else { | ||
| 874 | maybe_targmy(@_, \&unop, "chdir") | ||
| 875 | } | ||
| 876 | } | ||
| 877 | |||
| 878 | sub pp_chroot { maybe_targmy(@_, \&unop, "chroot") } | ||
| 879 | sub pp_closedir { unop(@_, "closedir") } | ||
| 880 | |||
| 881 | sub pp_entereval { | ||
| 882 | unop( | ||
| 883 | @_, | ||
| 884 | $_[1]->private & OPpEVAL_BYTES ? 'evalbytes' : "eval" | ||
| 885 | ) | ||
| 886 | } | ||
| 887 | |||
| 888 | sub pp_getpgrp { maybe_targmy(@_, \&unop, "getpgrp") } | ||
| 889 | sub pp_gmtime { unop(@_, "gmtime") } | ||
| 890 | sub pp_localtime { unop(@_, "localtime") } | ||
| 891 | sub pp_readdir { unop(@_, "readdir") } | ||
| 892 | sub pp_readlink { unop(@_, "readlink") } | ||
| 893 | sub pp_rewinddir { unop(@_, "rewinddir") } | ||
| 894 | sub pp_rmdir { maybe_targmy(@_, \&unop, "rmdir") } | ||
| 895 | sub pp_sleep { maybe_targmy(@_, \&unop, "sleep") } | ||
| 896 | sub pp_telldir { unop(@_, "telldir") } | ||
| 897 | |||
| 898 | sub pp_dofile | ||
| 899 | { | ||
| 900 | my $code = unop(@_, "do", 1); # llafr does not apply | ||
| 901 | if ($code =~ s/^((?:CORE::)?do) \{/$1({/) { $code .= ')' } | ||
| 902 | $code; | ||
| 903 | } | ||
| 904 | sub pp_ghbyname { unop(@_, "gethostbyname") } | ||
| 905 | sub pp_gnbyname { unop(@_, "getnetbyname") } | ||
| 906 | sub pp_gpbyname { unop(@_, "getprotobyname") } | ||
| 907 | sub pp_shostent { unop(@_, "sethostent") } | ||
| 908 | sub pp_snetent { unop(@_, "setnetent") } | ||
| 909 | sub pp_sprotoent { unop(@_, "setprotoent") } | ||
| 910 | sub pp_sservent { unop(@_, "setservent") } | ||
| 911 | sub pp_gpwnam { unop(@_, "getpwnam") } | ||
| 912 | sub pp_gpwuid { unop(@_, "getpwuid") } | ||
| 913 | sub pp_ggrnam { unop(@_, "getgrnam") } | ||
| 914 | sub pp_ggrgid { unop(@_, "getgrgid") } | ||
| 915 | |||
| 916 | sub pp_lock { unop(@_, "lock") } | ||
| 917 | |||
| 918 | sub pp_continue { unop(@_, "continue"); } | ||
| 919 | sub pp_break { unop(@_, "break"); } | ||
| 920 | |||
| 921 | sub givwhen | ||
| 922 | { | ||
| 923 | my($self, $op, $cx, $givwhen) = @_; | ||
| 924 | |||
| 925 | my $enterop = $op->first; | ||
| 926 | my ($head, $block); | ||
| 927 | if ($enterop->flags & OPf_SPECIAL) { | ||
| 928 | $head = $self->keyword("default"); | ||
| 929 | $block = $self->deparse($enterop->first, 0, $enterop, $op); | ||
| 930 | } | ||
| 931 | else { | ||
| 932 | my $cond = $enterop->first; | ||
| 933 | my $cond_str = $self->deparse($cond, 1, $enterop, $op); | ||
| 934 | $head = "$givwhen ($cond_str)"; | ||
| 935 | $block = $self->deparse($cond->sibling, 0, $enterop, $op); | ||
| 936 | } | ||
| 937 | |||
| 938 | return info_from_list($op, $self, [$head, "{", | ||
| 939 | "\n\t", $block->{text}, "\n\b", | ||
| 940 | "}\cK"], '', 'givwhen', | ||
| 941 | {body => [$block]}); | ||
| 942 | } | ||
| 943 | |||
| 944 | sub pp_leavegiven { givwhen(@_, $_[0]->keyword("given")); } | ||
| 945 | sub pp_leavewhen { givwhen(@_, $_[0]->keyword("when")); } | ||
| 946 | |||
| 947 | sub pp_delete | ||
| 948 | { | ||
| 949 | my($self, $op, $cx) = @_; | ||
| 950 | my $arg; | ||
| 951 | my ($info, $body, $type); | ||
| 952 | if ($op->private & OPpSLICE) { | ||
| 953 | if ($op->flags & OPf_SPECIAL) { | ||
| 954 | # Deleting from an array, not a hash | ||
| 955 | $info = $self->pp_aslice($op->first, 16); | ||
| 956 | $type = 'delete_slice'; | ||
| 957 | } | ||
| 958 | } else { | ||
| 959 | if ($op->flags & OPf_SPECIAL) { | ||
| 960 | # Deleting from an array, not a hash | ||
| 961 | $info = $self->pp_aelem($op->first, 16); | ||
| 962 | $type = 'delete_array' | ||
| 963 | } else { | ||
| 964 | $info = $self->pp_helem($op->first, 16); | ||
| 965 | $type = 'delete_hash'; | ||
| 966 | } | ||
| 967 | } | ||
| 968 | my @texts = $self->maybe_parens_func("delete", | ||
| 969 | $info->{text}, $cx, 16); | ||
| 970 | return info_from_list($op, $self, \@texts, '', $type, {body => [$info]}); | ||
| 971 | } | ||
| 972 | |||
| 973 | sub pp_require | ||
| 974 | { | ||
| 975 | my($self, $op, $cx) = @_; | ||
| 976 | my $opname = $op->flags & OPf_SPECIAL ? 'CORE::require' : 'require'; | ||
| 977 | if (class($op) eq "UNOP" and $op->first->name eq "const" | ||
| 978 | and $op->first->private & OPpCONST_BARE) { | ||
| 979 | my $name = $self->const_sv($op->first)->PV; | ||
| 980 | $name =~ s[/][::]g; | ||
| 981 | $name =~ s/\.pm//g; | ||
| 982 | return info_from_list($op, $self, [$opname, $name], ' ', | ||
| 983 | 'require', | ||
| 984 | {maybe_parens => [$self, $cx, 16]}); | ||
| 985 | } else { | ||
| 986 | return $self->unop( | ||
| 987 | $op, $cx, | ||
| 988 | $op->first->name eq 'const' | ||
| 989 | && $op->first->private & OPpCONST_NOVER | ||
| 990 | ? "no" | ||
| 991 | : $opname, | ||
| 992 | 1, # llafr does not apply | ||
| 993 | ); | ||
| 994 | } | ||
| 995 | Carp::confess("unhandled condition in pp_require"); | ||
| 996 | } | ||
| 997 | |||
| 998 | # Note 5.20 and up | ||
| 999 | sub pp_null | ||
| 1000 | { | ||
| 1001 | my($self, $op, $cx) = @_; | ||
| 1002 | my $info; | ||
| 1003 | if (class($op) eq "OP") { | ||
| 1004 | if ($op->targ == B::Deparse::OP_CONST) { | ||
| 1005 | # The Perl source constant value can't be recovered. | ||
| 1006 | # We'll use the 'ex_const' value as a substitute | ||
| 1007 | return info_from_text($op, $self, $self->{'ex_const'}, 'constant unrecoverable', {}) | ||
| 1008 | } else { | ||
| 1009 | return info_from_text($op, $self, '', 'constant ""', {}); | ||
| 1010 | } | ||
| 1011 | } elsif (class ($op) eq "COP") { | ||
| 1012 | return $self->pp_nextstate($op, $cx); | ||
| 1013 | } | ||
| 1014 | my $kid = $op->first; | ||
| 1015 | if ($op->first->name eq 'pushmark' | ||
| 1016 | or $op->first->name eq 'null' | ||
| 1017 | && $op->first->targ == B::Deparse::OP_PUSHMARK | ||
| 1018 | && B::Deparse::_op_is_or_was($op, B::Deparse::OP_LIST)) { | ||
| 1019 | return $self->pp_list($op, $cx); | ||
| 1020 | } elsif ($kid->name eq "enter") { | ||
| 1021 | return $self->pp_leave($op, $cx); | ||
| 1022 | } elsif ($kid->name eq "leave") { | ||
| 1023 | return $self->pp_leave($kid, $cx); | ||
| 1024 | } elsif ($kid->name eq "scope") { | ||
| 1025 | return $self->pp_scope($kid, $cx); | ||
| 1026 | } elsif ($op->targ == B::Deparse::OP_STRINGIFY) { | ||
| 1027 | return $self->dquote($op, $cx); | ||
| 1028 | } elsif ($op->targ == B::Deparse::OP_GLOB) { | ||
| 1029 | my @other_ops = ($kid, $kid->first, $kid->first->first); | ||
| 1030 | my $info = $self->pp_glob( | ||
| 1031 | $kid # entersub | ||
| 1032 | ->first # ex-list | ||
| 1033 | ->first # pushmark | ||
| 1034 | ->sibling, # glob | ||
| 1035 | $cx | ||
| 1036 | ); | ||
| 1037 | push @{$info->{other_ops}}, @other_ops; | ||
| 1038 | return $info; | ||
| 1039 | } elsif (!null($kid->sibling) and | ||
| 1040 | $kid->sibling->name eq "readline" and | ||
| 1041 | $kid->sibling->flags & OPf_STACKED) { | ||
| 1042 | my $lhs = $self->deparse($kid, 7, $op); | ||
| 1043 | my $rhs = $self->deparse($kid->sibling, 7, $kid); | ||
| 1044 | return $self->bin_info_join_maybe_parens($op, $lhs, $rhs, '=', " ", $cx, 7, | ||
| 1045 | 'readline'); | ||
| 1046 | } elsif (!null($kid->sibling) and | ||
| 1047 | $kid->sibling->name eq "trans" and | ||
| 1048 | $kid->sibling->flags & OPf_STACKED) { | ||
| 1049 | my $lhs = $self->deparse($kid, 20, $op); | ||
| 1050 | my $rhs = $self->deparse($kid->sibling, 20, $op); | ||
| 1051 | return $self->bin_info_join_maybe_parens($op, $lhs, $rhs, '=~', " ", $cx, 20, | ||
| 1052 | 'trans'); | ||
| 1053 | } elsif ($op->flags & OPf_SPECIAL && $cx < 1 && !$op->targ) { | ||
| 1054 | my $kid_info = $self->deparse($kid, $cx, $op); | ||
| 1055 | return info_from_list($op, $self, ['do', "{\n\t", $kid_info->{text}, | ||
| 1056 | "\n\b};"], '', 'null_special', | ||
| 1057 | {body => [$kid_info]}); | ||
| 1058 | } elsif (!null($kid->sibling) and | ||
| 1059 | $kid->sibling->name eq "null" and | ||
| 1060 | class($kid->sibling) eq "UNOP" and | ||
| 1061 | $kid->sibling->first->flags & OPf_STACKED and | ||
| 1062 | $kid->sibling->first->name eq "rcatline") { | ||
| 1063 | my $lhs = $self->deparse($kid, 18, $op); | ||
| 1064 | my $rhs = $self->deparse($kid->sibling, 18, $op); | ||
| 1065 | return $self->bin_info_join_maybe_parens($op, $lhs, $rhs, '=', " ", $cx, 20, | ||
| 1066 | 'null_rcatline'); | ||
| 1067 | } else { | ||
| 1068 | return $self->deparse($kid, $cx, $op); | ||
| 1069 | } | ||
| 1070 | Carp::confess("unhandled condition in null"); | ||
| 1071 | } | ||
| 1072 | |||
| 1073 | sub pp_scalar | ||
| 1074 | { | ||
| 1075 | my($self, $op, $cx) = @_; | ||
| 1076 | my $kid = $op->first; | ||
| 1077 | if (not null $kid->sibling) { | ||
| 1078 | # XXX Was a here-doc | ||
| 1079 | return $self->dquote($op); | ||
| 1080 | } | ||
| 1081 | $self->unop($op, $cx, "scalar"); | ||
| 1082 | } | ||
| 1083 | |||
| 1084 | |||
| 1085 | sub padval | ||
| 1086 | { | ||
| 1087 | my $self = shift; | ||
| 1088 | my $targ = shift; | ||
| 1089 | return $self->{'curcv'}->PADLIST->ARRAYelt(1)->ARRAYelt($targ); | ||
| 1090 | } | ||
| 1091 | |||
| 1092 | sub anon_hash_or_list | ||
| 1093 | { | ||
| 1094 | my $self = shift; | ||
| 1095 | my($op, $cx) = @_; | ||
| 1096 | |||
| 1097 | my $name = $op->name; | ||
| 1098 | my($pre, $post) = @{{"anonlist" => ["[","]"], | ||
| 1099 | "anonhash" => ["{","}"]}->{$name}}; | ||
| 1100 | my($expr, @exprs); | ||
| 1101 | my $other_ops = [$op->first]; | ||
| 1102 | $op = $op->first->sibling; # skip pushmark | ||
| 1103 | for (; !null($op); $op = $op->sibling) { | ||
| 1104 | $expr = $self->deparse($op, 6, $op); | ||
| 1105 | push @exprs, [$expr, $op]; | ||
| 1106 | } | ||
| 1107 | if ($pre eq "{" and $cx < 1) { | ||
| 1108 | # Disambiguate that it's not a block | ||
| 1109 | $pre = "+{"; | ||
| 1110 | } | ||
| 1111 | my $texts = [$pre, $self->combine(", ", \@exprs), $post]; | ||
| 1112 | return info_from_list($op, $self, $texts, '', $name, | ||
| 1113 | {body => \@exprs, | ||
| 1114 | other_ops => $other_ops | ||
| 1115 | }); | ||
| 1116 | } | ||
| 1117 | |||
| 1118 | sub pp_anonlist { | ||
| 1119 | my $self = shift; | ||
| 1120 | my ($op, $cx) = @_; | ||
| 1121 | if ($op->flags & OPf_SPECIAL) { | ||
| 1122 | return $self->anon_hash_or_list($op, $cx); | ||
| 1123 | } | ||
| 1124 | warn "Unexpected op pp_" . $op->name() . " without OPf_SPECIAL"; | ||
| 1125 | return info_from_text($op, $self, 'XXX', 'bad_anonlist', {}); | ||
| 1126 | } | ||
| 1127 | |||
| 1128 | *pp_anonhash = \&pp_anonlist; | ||
| 1129 | |||
| 1130 | sub e_anoncode($$) | ||
| 1131 | { | ||
| 1132 | my ($self, $info) = @_; | ||
| 1133 | my $sub_info = $self->deparse_sub($info->{code}); | ||
| 1134 | return info_from_list($sub_info->{op}, $self, | ||
| 1135 | ['sub', $sub_info->{text}], ' ', 'e_anoncode', | ||
| 1136 | {body=> [$sub_info]}); | ||
| 1137 | } | ||
| 1138 | |||
| 1139 | sub pp_refgen | ||
| 1140 | { | ||
| 1141 | my($self, $op, $cx) = @_; | ||
| 1142 | my $kid = $op->first; | ||
| 1143 | if ($kid->name eq "null") { | ||
| 1144 | my $other_ops = [$kid]; | ||
| 1145 | my $anoncode = $kid = $kid->first; | ||
| 1146 | if ($anoncode->name eq "anonconst") { | ||
| 1147 | $anoncode = $anoncode->first->first->sibling; | ||
| 1148 | } | ||
| 1149 | if ($anoncode->name eq "anoncode" | ||
| 1150 | or !null($anoncode = $kid->sibling) and | ||
| 1151 | $anoncode->name eq "anoncode") { | ||
| 1152 | return $self->e_anoncode({ code => $self->padval($anoncode->targ) }); | ||
| 1153 | } elsif ($kid->name eq "pushmark") { | ||
| 1154 | my $sib_name = $kid->sibling->name; | ||
| 1155 | if ($sib_name =~ /^enter(xs)?sub/) { | ||
| 1156 | my $kid_info = $self->deparse($kid->sibling, 1, $op); | ||
| 1157 | # Always show parens for \(&func()), but only with -p otherwise | ||
| 1158 | my @texts = ('\\', $kid_info->{text}); | ||
| 1159 | if ($self->{'parens'} or $kid->sibling->private & OPpENTERSUB_AMPER) { | ||
| 1160 | @texts = ('(', "\\", $kid_info->{text}, ')'); | ||
| 1161 | } | ||
| 1162 | return info_from_list($op, $self, \@texts, '', 'refgen_entersub', | ||
| 1163 | {body => [$kid_info], | ||
| 1164 | other_ops => $other_ops}); | ||
| 1165 | } | ||
| 1166 | } | ||
| 1167 | } | ||
| 1168 | local $self->{'in_refgen'} = 1; | ||
| 1169 | $self->pfixop($op, $cx, "\\", 20); | ||
| 1170 | } | ||
| 1171 | |||
| 1172 | sub pp_srefgen { pp_refgen(@_) } | ||
| 1173 | |||
| 1174 | sub pp_readline { | ||
| 1175 | my $self = shift; | ||
| 1176 | my($op, $cx) = @_; | ||
| 1177 | my $kid = $op->first; | ||
| 1178 | $kid = $kid->first if $kid->name eq "rv2gv"; # <$fh> | ||
| 1179 | if (is_scalar($kid) | ||
| 1180 | and $op->flags & OPf_SPECIAL | ||
| 1181 | and $self->deparse($kid, 1) eq 'ARGV') { | ||
| 1182 | my $body = [$self->deparse($kid, 1, $op)]; | ||
| 1183 | return info_from_list($op, $self, ['<', $body->[0]{text}, '>'], '', | ||
| 1184 | 'readline_scalar', {body=>$body}); | ||
| 1185 | } | ||
| 1186 | return $self->unop($op, $cx, "readline"); | ||
| 1187 | } | ||
| 1188 | |||
| 1189 | sub pp_rcatline { | ||
| 1190 | my $self = shift; | ||
| 1191 | my($op) = @_; | ||
| 1192 | return info_from_list($op, $self, ["<", $self->gv_name($self->gv_or_padgv($op)), ">"], | ||
| 1193 | '', 'rcatline', {}); | ||
| 1194 | } | ||
| 1195 | |||
| 1196 | # Unary operators that can occur as pseudo-listops inside double quotes | ||
| 1197 | sub dq_unop | ||
| 1198 | { | ||
| 1199 | my($self, $op, $cx, $name, $prec, $flags) = (@_, 0, 0); | ||
| 1200 | my $kid; | ||
| 1201 | if ($op->flags & OPf_KIDS) { | ||
| 1202 | my $other_ops = undef; | ||
| 1203 | $kid = $op->first; | ||
| 1204 | if (not null $kid->sibling) { | ||
| 1205 | # If there's more than one kid, the first is an ex-pushmark. | ||
| 1206 | $other_ops = [$kid]; | ||
| 1207 | $kid = $kid->sibling; | ||
| 1208 | } | ||
| 1209 | my $info = $self->maybe_parens_unop($name, $kid, $cx, $op); | ||
| 1210 | $info->{other_ops} = $other_ops if $other_ops; | ||
| 1211 | return $info; | ||
| 1212 | } else { | ||
| 1213 | my @texts = ($name); | ||
| 1214 | push @texts, '(', ')' if $op->flags & OPf_SPECIAL; | ||
| 1215 | return info_from_list($op, $self, \@texts, '', 'dq', {}); | ||
| 1216 | } | ||
| 1217 | Carp::confess("unhandled condition in dq_unop"); | ||
| 1218 | } | ||
| 1219 | |||
| 1220 | sub pp_ucfirst { dq_unop(@_, "ucfirst") } | ||
| 1221 | sub pp_lcfirst { dq_unop(@_, "lcfirst") } | ||
| 1222 | sub pp_uc { dq_unop(@_, "uc") } | ||
| 1223 | sub pp_lc { dq_unop(@_, "lc") } | ||
| 1224 | sub pp_quotemeta { maybe_targmy(@_, \&dq_unop, "quotemeta") } | ||
| 1225 | sub pp_fc { dq_unop(@_, "fc") } | ||
| 1226 | |||
| 1227 | # loop expressions | ||
| 1228 | sub loopex | ||
| 1229 | { | ||
| 1230 | my ($self, $op, $cx, $name) = @_; | ||
| 1231 | my $opts = {maybe_parens => [$self, $cx, 7]}; | ||
| 1232 | my ($type, $body); | ||
| 1233 | if (class($op) eq "PVOP") { | ||
| 1234 | return info_from_list($op, $self, [$name, $op->pv], ' ', 'loopex_pvop', {}); | ||
| 1235 | } elsif (class($op) eq "OP") { | ||
| 1236 | # no-op | ||
| 1237 | $type = 'loopex_op'; | ||
| 1238 | return info_from_text($op, $self, $name, 'loopex_op', $opts); | ||
| 1239 | } elsif (class($op) eq "UNOP") { | ||
| 1240 | (my $kid_info = $self->deparse($op->first, 7, $op)) =~ s/^\cS//; | ||
| 1241 | $opts->{body} = [$kid_info]; | ||
| 1242 | return info_from_list($op, $self, [$name, $op->pv], ' ', 'loopex_unop', $opts); | ||
| 1243 | } else { | ||
| 1244 | return info_from_text($op, $self, $name, 'loopex', $opts); | ||
| 1245 | } | ||
| 1246 | Carp::confess("unhandled condition in lopex"); | ||
| 1247 | } | ||
| 1248 | |||
| 1249 | sub pp_last { loopex(@_, "last") } | ||
| 1250 | sub pp_next { loopex(@_, "next") } | ||
| 1251 | sub pp_redo { loopex(@_, "redo") } | ||
| 1252 | sub pp_goto { loopex(@_, "goto") } | ||
| 1253 | sub pp_dump { loopex(@_, "CORE::dump") } | ||
| 1254 | |||
| 1255 | sub ftst | ||
| 1256 | { | ||
| 1257 | my($self, $op, $cx, $name) = @_; | ||
| 1258 | if (class($op) eq "UNOP") { | ||
| 1259 | # Genuine '-X' filetests are exempt from the LLAFR, but not | ||
| 1260 | # l?stat() | ||
| 1261 | if ($name =~ /^-/) { | ||
| 1262 | (my $kid = $self->deparse($op->first, 16, $op)) =~ s/^\cS//; | ||
| 1263 | return info_from_list($op, $self, [$name, $kid->{text}], ' ', | ||
| 1264 | 'ftst_unop_dash', | ||
| 1265 | {body => [$kid], | ||
| 1266 | maybe_parens => [$self, $cx, 16]}); | ||
| 1267 | } | ||
| 1268 | return $self->maybe_parens_unop($name, $op->first, $cx, $op); | ||
| 1269 | } elsif (class($op) =~ /^(SV|PAD)OP$/) { | ||
| 1270 | my @list = $self->maybe_parens_func($name, $self->pp_gv($op, 1), $cx, 16); | ||
| 1271 | return info_from_list($op, $self, \@list, ' ', 'ftst_list', {}); | ||
| 1272 | } else { # I don't think baseop filetests ever survive ck_ftst, but... | ||
| 1273 | return info_from_text($op, $self, $name, 'unop', {}); | ||
| 1274 | } | ||
| 1275 | } | ||
| 1276 | |||
| 1277 | sub pp_lstat { ftst(@_, "lstat") } | ||
| 1278 | sub pp_stat { ftst(@_, "stat") } | ||
| 1279 | sub pp_ftrread { ftst(@_, "-R") } | ||
| 1280 | sub pp_ftrwrite { ftst(@_, "-W") } | ||
| 1281 | sub pp_ftrexec { ftst(@_, "-X") } | ||
| 1282 | sub pp_fteread { ftst(@_, "-r") } | ||
| 1283 | sub pp_ftewrite { ftst(@_, "-w") } | ||
| 1284 | sub pp_fteexec { ftst(@_, "-x") } | ||
| 1285 | sub pp_ftis { ftst(@_, "-e") } | ||
| 1286 | sub pp_fteowned { ftst(@_, "-O") } | ||
| 1287 | sub pp_ftrowned { ftst(@_, "-o") } | ||
| 1288 | sub pp_ftzero { ftst(@_, "-z") } | ||
| 1289 | sub pp_ftsize { ftst(@_, "-s") } | ||
| 1290 | sub pp_ftmtime { ftst(@_, "-M") } | ||
| 1291 | sub pp_ftatime { ftst(@_, "-A") } | ||
| 1292 | sub pp_ftctime { ftst(@_, "-C") } | ||
| 1293 | sub pp_ftsock { ftst(@_, "-S") } | ||
| 1294 | sub pp_ftchr { ftst(@_, "-c") } | ||
| 1295 | sub pp_ftblk { ftst(@_, "-b") } | ||
| 1296 | sub pp_ftfile { ftst(@_, "-f") } | ||
| 1297 | sub pp_ftdir { ftst(@_, "-d") } | ||
| 1298 | sub pp_ftpipe { ftst(@_, "-p") } | ||
| 1299 | sub pp_ftlink { ftst(@_, "-l") } | ||
| 1300 | sub pp_ftsuid { ftst(@_, "-u") } | ||
| 1301 | sub pp_ftsgid { ftst(@_, "-g") } | ||
| 1302 | sub pp_ftsvtx { ftst(@_, "-k") } | ||
| 1303 | sub pp_fttty { ftst(@_, "-t") } | ||
| 1304 | sub pp_fttext { ftst(@_, "-T") } | ||
| 1305 | sub pp_ftbinary { ftst(@_, "-B") } | ||
| 1306 | |||
| 1307 | sub SWAP_CHILDREN () { 1 } | ||
| 1308 | sub ASSIGN () { 2 } # has OP= variant | ||
| 1309 | sub LIST_CONTEXT () { 4 } # Assignment is in list context | ||
| 1310 | |||
| 1311 | my(%left, %right); | ||
| 1312 | |||
| 1313 | sub assoc_class { | ||
| 1314 | my $op = shift; | ||
| 1315 | my $name = $op->name; | ||
| 1316 | if ($name eq "concat" and $op->first->name eq "concat") { | ||
| 1317 | # avoid spurious '=' -- see comment in pp_concat | ||
| 1318 | return "concat"; | ||
| 1319 | } | ||
| 1320 | if ($name eq "null" and class($op) eq "UNOP" | ||
| 1321 | and $op->first->name =~ /^(and|x?or)$/ | ||
| 1322 | and null $op->first->sibling) | ||
| 1323 | { | ||
| 1324 | # Like all conditional constructs, OP_ANDs and OP_ORs are topped | ||
| 1325 | # with a null that's used as the common end point of the two | ||
| 1326 | # flows of control. For precedence purposes, ignore it. | ||
| 1327 | # (COND_EXPRs have these too, but we don't bother with | ||
| 1328 | # their associativity). | ||
| 1329 | return assoc_class($op->first); | ||
| 1330 | } | ||
| 1331 | return $name . ($op->flags & OPf_STACKED ? "=" : ""); | ||
| 1332 | } | ||
| 1333 | |||
| 1334 | # Left associative operators, like '+', for which | ||
| 1335 | # $a + $b + $c is equivalent to ($a + $b) + $c | ||
| 1336 | |||
| 1337 | BEGIN { | ||
| 1338 | %left = ('multiply' => 19, 'i_multiply' => 19, | ||
| 1339 | 'divide' => 19, 'i_divide' => 19, | ||
| 1340 | 'modulo' => 19, 'i_modulo' => 19, | ||
| 1341 | 'repeat' => 19, | ||
| 1342 | 'add' => 18, 'i_add' => 18, | ||
| 1343 | 'subtract' => 18, 'i_subtract' => 18, | ||
| 1344 | 'concat' => 18, | ||
| 1345 | 'left_shift' => 17, 'right_shift' => 17, | ||
| 1346 | 'bit_and' => 13, | ||
| 1347 | 'bit_or' => 12, 'bit_xor' => 12, | ||
| 1348 | 'and' => 3, | ||
| 1349 | 'or' => 2, 'xor' => 2, | ||
| 1350 | ); | ||
| 1351 | } | ||
| 1352 | |||
| 1353 | sub deparse_binop_left { | ||
| 1354 | my $self = shift; | ||
| 1355 | my($op, $left, $prec) = @_; | ||
| 1356 | if ($left{assoc_class($op)} && $left{assoc_class($left)} | ||
| 1357 | and $left{assoc_class($op)} == $left{assoc_class($left)}) | ||
| 1358 | { | ||
| 1359 | return $self->deparse($left, $prec - .00001, $op); | ||
| 1360 | } else { | ||
| 1361 | return $self->deparse($left, $prec, $op); | ||
| 1362 | } | ||
| 1363 | } | ||
| 1364 | |||
| 1365 | # Right associative operators, like '=', for which | ||
| 1366 | # $a = $b = $c is equivalent to $a = ($b = $c) | ||
| 1367 | |||
| 1368 | BEGIN { | ||
| 1369 | %right = ('pow' => 22, | ||
| 1370 | 'sassign=' => 7, 'aassign=' => 7, | ||
| 1371 | 'multiply=' => 7, 'i_multiply=' => 7, | ||
| 1372 | 'divide=' => 7, 'i_divide=' => 7, | ||
| 1373 | 'modulo=' => 7, 'i_modulo=' => 7, | ||
| 1374 | 'repeat=' => 7, 'refassign' => 7, 'refassign=' => 7, | ||
| 1375 | 'add=' => 7, 'i_add=' => 7, | ||
| 1376 | 'subtract=' => 7, 'i_subtract=' => 7, | ||
| 1377 | 'concat=' => 7, | ||
| 1378 | 'left_shift=' => 7, 'right_shift=' => 7, | ||
| 1379 | 'bit_and=' => 7, 'sbit_and=' => 7, 'nbit_and=' => 7, | ||
| 1380 | 'nbit_or=' => 7, 'nbit_xor=' => 7, | ||
| 1381 | 'sbit_or=' => 7, 'sbit_xor=' => 7, | ||
| 1382 | 'andassign' => 7, | ||
| 1383 | 'orassign' => 7, | ||
| 1384 | ); | ||
| 1385 | } | ||
| 1386 | |||
| 1387 | sub deparse_binop_right { | ||
| 1388 | my $self = shift; | ||
| 1389 | my($op, $right, $prec) = @_; | ||
| 1390 | if ($right{assoc_class($op)} && $right{assoc_class($right)} | ||
| 1391 | and $right{assoc_class($op)} == $right{assoc_class($right)}) | ||
| 1392 | { | ||
| 1393 | return $self->deparse($right, $prec - .00001, $op); | ||
| 1394 | } else { | ||
| 1395 | return $self->deparse($right, $prec, $op); | ||
| 1396 | } | ||
| 1397 | } | ||
| 1398 | |||
| 1399 | sub pp_add { maybe_targmy(@_, \&binop, "+", 18, ASSIGN) } | ||
| 1400 | sub pp_multiply { maybe_targmy(@_, \&binop, "*", 19, ASSIGN) } | ||
| 1401 | sub pp_subtract { maybe_targmy(@_, \&binop, "-",18, ASSIGN) } | ||
| 1402 | sub pp_divide { maybe_targmy(@_, \&binop, "/", 19, ASSIGN) } | ||
| 1403 | sub pp_modulo { maybe_targmy(@_, \&binop, "%", 19, ASSIGN) } | ||
| 1404 | sub pp_i_add { maybe_targmy(@_, \&binop, "+", 18, ASSIGN) } | ||
| 1405 | sub pp_i_multiply { maybe_targmy(@_, \&binop, "*", 19, ASSIGN) } | ||
| 1406 | sub pp_i_subtract { maybe_targmy(@_, \&binop, "-", 18, ASSIGN) } | ||
| 1407 | sub pp_i_divide { maybe_targmy(@_, \&binop, "/", 19, ASSIGN) } | ||
| 1408 | sub pp_i_modulo { maybe_targmy(@_, \&binop, "%", 19, ASSIGN) } | ||
| 1409 | sub pp_pow { maybe_targmy(@_, \&binop, "**", 22, ASSIGN) } | ||
| 1410 | |||
| 1411 | sub pp_left_shift { maybe_targmy(@_, \&binop, "<<", 17, ASSIGN) } | ||
| 1412 | sub pp_right_shift { maybe_targmy(@_, \&binop, ">>", 17, ASSIGN) } | ||
| 1413 | sub pp_bit_and { maybe_targmy(@_, \&binop, "&", 13, ASSIGN) } | ||
| 1414 | sub pp_bit_or { maybe_targmy(@_, \&binop, "|", 12, ASSIGN) } | ||
| 1415 | sub pp_bit_xor { maybe_targmy(@_, \&binop, "^", 12, ASSIGN) } | ||
| 1416 | *pp_nbit_and = *pp_bit_and; | ||
| 1417 | *pp_nbit_or = *pp_bit_or; | ||
| 1418 | *pp_nbit_xor = *pp_bit_xor; | ||
| 1419 | sub pp_sbit_and { maybe_targmy(@_, \&binop, "&.", 13, ASSIGN) } | ||
| 1420 | sub pp_sbit_or { maybe_targmy(@_, \&binop, "|.", 12, ASSIGN) } | ||
| 1421 | sub pp_sbit_xor { maybe_targmy(@_, \&binop, "^.", 12, ASSIGN) } | ||
| 1422 | |||
| 1423 | sub pp_eq { binop(@_, "==", 14) } | ||
| 1424 | sub pp_ne { binop(@_, "!=", 14) } | ||
| 1425 | sub pp_lt { binop(@_, "<", 15) } | ||
| 1426 | sub pp_gt { binop(@_, ">", 15) } | ||
| 1427 | sub pp_ge { binop(@_, ">=", 15) } | ||
| 1428 | sub pp_le { binop(@_, "<=", 15) } | ||
| 1429 | sub pp_cmp { binop(@_, "<=>", 14) } | ||
| 1430 | sub pp_i_eq { binop(@_, "==", 14) } | ||
| 1431 | sub pp_i_ne { binop(@_, "!=", 14) } | ||
| 1432 | sub pp_i_lt { binop(@_, "<", 15) } | ||
| 1433 | sub pp_i_gt { binop(@_, ">", 15) } | ||
| 1434 | sub pp_i_ge { binop(@_, ">=", 15) } | ||
| 1435 | sub pp_i_le { binop(@_, "<=", 15) } | ||
| 1436 | sub pp_i_cmp { maybe_targmy(@_, \&binop, "<=>", 14) } | ||
| 1437 | |||
| 1438 | sub pp_seq { binop(@_, "eq", 14) } | ||
| 1439 | sub pp_sne { binop(@_, "ne", 14) } | ||
| 1440 | sub pp_slt { binop(@_, "lt", 15) } | ||
| 1441 | sub pp_sgt { binop(@_, "gt", 15) } | ||
| 1442 | sub pp_sge { binop(@_, "ge", 15) } | ||
| 1443 | sub pp_sle { binop(@_, "le", 15) } | ||
| 1444 | sub pp_scmp { binop(@_, "cmp", 14) } | ||
| 1445 | |||
| 1446 | sub pp_sassign { binop(@_, "=", 7, SWAP_CHILDREN) } | ||
| 1447 | sub pp_aassign { binop(@_, "=", 7, SWAP_CHILDREN | LIST_CONTEXT) } | ||
| 1448 | |||
| 1449 | sub pp_smartmatch { | ||
| 1450 | my ($self, $op, $cx) = @_; | ||
| 1451 | if ($op->flags & OPf_SPECIAL) { | ||
| 1452 | return $self->deparse($op->last, $cx, $op); | ||
| 1453 | } | ||
| 1454 | else { | ||
| 1455 | binop(@_, "~~", 14); | ||
| 1456 | } | ||
| 1457 | } | ||
| 1458 | |||
| 1459 | sub bin_info_join($$$$$$$) { | ||
| 1460 | my ($self, $op, $lhs, $rhs, $mid, $sep, $type) = @_; | ||
| 1461 | my $texts = [$lhs->{text}, $mid, $rhs->{text}]; | ||
| 1462 | return info_from_list($op, $self, $texts, ' ', $type, {}) | ||
| 1463 | } | ||
| 1464 | |||
| 1465 | sub bin_info_join_maybe_parens($$$$$$$$$) { | ||
| 1466 | my ($self, $op, $lhs, $rhs, $mid, $sep, $cx, $prec, $type) = @_; | ||
| 1467 | my $info = bin_info_join($self, $op, $lhs, $rhs, $mid, $sep, $type); | ||
| 1468 | $info->{text} = $self->maybe_parens($info->{text}, $cx, $prec); | ||
| 1469 | return $info; | ||
| 1470 | } | ||
| 1471 | |||
| 1472 | # '.' is special because concats-of-concats are optimized to save copying | ||
| 1473 | # by making all but the first concat stacked. The effect is as if the | ||
| 1474 | # programmer had written '($a . $b) .= $c', except legal. | ||
| 1475 | sub pp_concat { maybe_targmy(@_, \&real_concat) } | ||
| 1476 | sub real_concat { | ||
| 1477 | my $self = shift; | ||
| 1478 | my($op, $cx) = @_; | ||
| 1479 | my $left = $op->first; | ||
| 1480 | my $right = $op->last; | ||
| 1481 | my $eq = ""; | ||
| 1482 | my $prec = 18; | ||
| 1483 | if ($op->flags & OPf_STACKED and $op->first->name ne "concat") { | ||
| 1484 | $eq = "="; | ||
| 1485 | $prec = 7; | ||
| 1486 | } | ||
| 1487 | my $lhs = $self->deparse_binop_left($op, $left, $prec); | ||
| 1488 | my $rhs = $self->deparse_binop_right($op, $right, $prec); | ||
| 1489 | return $self->bin_info_join_maybe_parens($op, $lhs, $rhs, ".$eq", " ", $cx, $prec, | ||
| 1490 | 'real_concat'); | ||
| 1491 | } | ||
| 1492 | |||
| 1493 | sub range { | ||
| 1494 | my $self = shift; | ||
| 1495 | my ($op, $cx, $type) = @_; | ||
| 1496 | my $left = $op->first; | ||
| 1497 | my $right = $left->sibling; | ||
| 1498 | $left = $self->deparse($left, 9, $op); | ||
| 1499 | $right = $self->deparse($right, 9, $op); | ||
| 1500 | return info_from_list($op, $self, [$left, $type, $right], ' ', 'range', | ||
| 1501 | {maybe_parens => [$self, $cx, 9]}); | ||
| 1502 | } | ||
| 1503 | |||
| 1504 | sub pp_flop | ||
| 1505 | { | ||
| 1506 | my $self = shift; | ||
| 1507 | my($op, $cx) = @_; | ||
| 1508 | my $flip = $op->first; | ||
| 1509 | my $type = ($flip->flags & OPf_SPECIAL) ? "..." : ".."; | ||
| 1510 | return info_from_text($op, $self, $self->range($flip->first, $cx, $type), 'pp_flop', {}); | ||
| 1511 | } | ||
| 1512 | |||
| 1513 | sub logassignop | ||
| 1514 | { | ||
| 1515 | my ($self, $op, $cx, $opname) = @_; | ||
| 1516 | my $left = $op->first; | ||
| 1517 | |||
| 1518 | my $right = $op->first->sibling->first; # skip sassign | ||
| 1519 | $left = $self->deparse($left, 7, $op); | ||
| 1520 | $right = $self->deparse($right, 7, $op); | ||
| 1521 | return info_from_list($op, $self, [$left->{text}, $opname, $right->{text}], ' ', | ||
| 1522 | 'logassignop', | ||
| 1523 | {other_ops => [$op->first->sibling], | ||
| 1524 | body => [$left, $right], | ||
| 1525 | maybe_parens => [$self, $cx, 7]}); | ||
| 1526 | } | ||
| 1527 | |||
| 1528 | sub pp_andassign { logassignop(@_, "&&=") } | ||
| 1529 | sub pp_orassign { logassignop(@_, "||=") } | ||
| 1530 | sub pp_dorassign { logassignop(@_, "//=") } | ||
| 1531 | |||
| 1532 | sub rv2gv_or_string { | ||
| 1533 | my($self,$op, $parent) = @_; | ||
| 1534 | if ($op->name eq "gv") { # could be open("open") or open("###") | ||
| 1535 | my($name,$quoted) = | ||
| 1536 | $self->stash_variable_name("", $self->gv_or_padgv($op)); | ||
| 1537 | return info_from_text($op, $self, $quoted ? $name : "*$name", 'r2gv_or_string', {}); | ||
| 1538 | } | ||
| 1539 | else { | ||
| 1540 | return $self->deparse($op, 6, $parent); | ||
| 1541 | } | ||
| 1542 | } | ||
| 1543 | |||
| 1544 | sub is_ifelse_cont | ||
| 1545 | { | ||
| 1546 | my $op = shift; | ||
| 1547 | return ($op->name eq "null" and class($op) eq "UNOP" | ||
| 1548 | and $op->first->name =~ /^(and|cond_expr)$/ | ||
| 1549 | and is_scope($op->first->first->sibling)); | ||
| 1550 | } | ||
| 1551 | |||
| 1552 | sub loop_common | ||
| 1553 | { | ||
| 1554 | my $self = shift; | ||
| 1555 | my($op, $cx, $init) = @_; | ||
| 1556 | my $enter = $op->first; | ||
| 1557 | my $kid = $enter->sibling; | ||
| 1558 | local(@$self{qw'curstash warnings hints hinthash'}) | ||
| 1559 | = @$self{qw'curstash warnings hints hinthash'}; | ||
| 1560 | |||
| 1561 | my ($body, @body, @other_ops, $type); | ||
| 1562 | my (@head, @head_text) = ((), ()); | ||
| 1563 | my ($bare, $cond_info) = (0, undef); | ||
| 1564 | |||
| 1565 | if ($kid->name eq "lineseq") { | ||
| 1566 | # bare or infinite loop | ||
| 1567 | $type = 'loop_bare_or_infinite'; | ||
| 1568 | |||
| 1569 | if ($kid->last->name eq "unstack") { # infinite | ||
| 1570 | @head_text = ("while", "(1)"); # Can't use for(;;) if there's a continue | ||
| 1571 | } else { | ||
| 1572 | $bare = 1; | ||
| 1573 | } | ||
| 1574 | $body = $kid; | ||
| 1575 | } elsif ($enter->name eq "enteriter") { | ||
| 1576 | # foreach | ||
| 1577 | $type = 'loop_foreach'; | ||
| 1578 | |||
| 1579 | push @other_ops, $enter->first; | ||
| 1580 | my $ary = $enter->first->sibling; # first was pushmark | ||
| 1581 | my @ary_info; | ||
| 1582 | my @ary_text; | ||
| 1583 | my $var = $ary->sibling; | ||
| 1584 | if ($ary->name eq 'null' and $enter->private & OPpITER_REVERSED) { | ||
| 1585 | # "reverse" was optimised away | ||
| 1586 | @ary_info = (listop($self, $ary->first->sibling, 1, 'reverse')); | ||
| 1587 | @ary_text = ($ary_info[0]->{text}); | ||
| 1588 | } elsif ($enter->flags & OPf_STACKED | ||
| 1589 | and not null $ary->first->sibling->sibling) { | ||
| 1590 | @ary_info = ($self->deparse($ary->first->sibling, 9, $op), | ||
| 1591 | $self->deparse($ary->first->sibling->sibling, 9, $op)); | ||
| 1592 | @ary_text = ($ary_info[0]->{text}, "..", $ary_info[1]->{text}); | ||
| 1593 | |||
| 1594 | } else { | ||
| 1595 | @ary_info = ($self->deparse($ary, 1, $op)); | ||
| 1596 | @ary_text = $ary_info[0]->{text}; | ||
| 1597 | } | ||
| 1598 | |||
| 1599 | my @var_info = (); | ||
| 1600 | my @var_text; | ||
| 1601 | if (null $var) { | ||
| 1602 | if (($enter->flags & OPf_SPECIAL) && ($] < 5.009)) { | ||
| 1603 | # thread special var, under 5005threads | ||
| 1604 | @var_text = ($self->pp_threadsv($enter, 1)); | ||
| 1605 | } else { # regular my() variable | ||
| 1606 | my $var_info = $self->pp_padsv($enter, 1, 1); | ||
| 1607 | @var_info =($var_info); | ||
| 1608 | @var_text = ($var_info->{text}); | ||
| 1609 | } | ||
| 1610 | } elsif ($var->name eq "rv2gv") { | ||
| 1611 | @var_info = ($self->pp_rv2sv($var, 1)); | ||
| 1612 | @var_text = ($var_info[0]->{text}); | ||
| 1613 | if ($enter->private & OPpOUR_INTRO) { | ||
| 1614 | # our declarations don't have package names | ||
| 1615 | $var_text[0] =~ s/^(.).*::/$1/; | ||
| 1616 | unshift @var_text, 'our'; | ||
| 1617 | } | ||
| 1618 | } elsif ($var->name eq "gv") { | ||
| 1619 | @var_info = ($self->deparse($var, 1, $op)); | ||
| 1620 | @var_text = ("\$" . $var_info[0]->{text}); | ||
| 1621 | } | ||
| 1622 | |||
| 1623 | # skip OP_AND and OP_ITER | ||
| 1624 | push @other_ops, $kid->first, $kid->first->first; | ||
| 1625 | $body = $kid->first->first->sibling; | ||
| 1626 | |||
| 1627 | @head = (@ary_info, @var_info); | ||
| 1628 | if (!is_state $body->first and $body->first->name !~ /^(?:stub|leave|scope)$/) { | ||
| 1629 | Carp::confess("var ne \$_") unless join('', @var_text) eq '$_'; | ||
| 1630 | $body = $body->first; | ||
| 1631 | my $body_info = $self->deparse($body, 2, $op); | ||
| 1632 | push @head, $body_info; | ||
| 1633 | my @texts = ($body_info->{text}, "foreach", '(', @ary_text, ')'); | ||
| 1634 | return info_from_list($body->{op}, $self, \@texts, ' ', 'loop_foreach_ary', | ||
| 1635 | {other_ops => \@other_ops}); | ||
| 1636 | } | ||
| 1637 | @head_text = ("foreach", @var_text, '(', @ary_text, ')'); | ||
| 1638 | } elsif ($kid->name eq "null") { | ||
| 1639 | # while/until | ||
| 1640 | $type = 'loop_while_until'; | ||
| 1641 | |||
| 1642 | $kid = $kid->first; | ||
| 1643 | my $name = {"and" => "while", "or" => "until"}->{$kid->name}; | ||
| 1644 | $cond_info = $self->deparse($kid->first, 1, $op); | ||
| 1645 | @head_text = ($name, "($cond_info->{text})"); | ||
| 1646 | $body = $kid->first->sibling; | ||
| 1647 | } elsif ($kid->name eq "stub") { | ||
| 1648 | # bare and empty | ||
| 1649 | return info_from_list($op, $self, ['{', ';', '}'], '', 'loop_stub', {}); | ||
| 1650 | } | ||
| 1651 | |||
| 1652 | # If there isn't a continue block, then the next pointer for the loop | ||
| 1653 | # will point to the unstack, which is kid's last child, except | ||
| 1654 | # in a bare loop, when it will point to the leaveloop. When neither of | ||
| 1655 | # these conditions hold, then the second-to-last child is the continue | ||
| 1656 | # block (or the last in a bare loop). | ||
| 1657 | my $cont_start = $enter->nextop; | ||
| 1658 | my ($cont, @cont_text, $body_info); | ||
| 1659 | my @cont = (); | ||
| 1660 | if ($$cont_start != $$op && ${$cont_start} != ${$body->last}) { | ||
| 1661 | $type .= '_continue'; | ||
| 1662 | |||
| 1663 | if ($bare) { | ||
| 1664 | $cont = $body->last; | ||
| 1665 | } else { | ||
| 1666 | $cont = $body->first; | ||
| 1667 | while (!null($cont->sibling->sibling)) { | ||
| 1668 | $cont = $cont->sibling; | ||
| 1669 | } | ||
| 1670 | } | ||
| 1671 | my $state = $body->first; | ||
| 1672 | my $cuddle = " "; | ||
| 1673 | my @states; | ||
| 1674 | for (; $$state != $$cont; $state = $state->sibling) { | ||
| 1675 | push @states, $state; | ||
| 1676 | } | ||
| 1677 | $body_info = $self->lineseq(undef, 0, @states); | ||
| 1678 | if (defined $cond_info and not is_scope $cont and $self->{'expand'} < 3) { | ||
| 1679 | my $cont_info = $self->deparse($cont, 1, $op); | ||
| 1680 | push @head, $cont_info; | ||
| 1681 | my $init_text = defined($init) ? $init->{text} : ' '; | ||
| 1682 | @head_text = ('for', '(', "$init_text;", $cont_info->{text}, ')'); | ||
| 1683 | @cont_text = ("\cK"); | ||
| 1684 | } else { | ||
| 1685 | my $cont_info = $self->deparse($cont, 0, $op); | ||
| 1686 | push @head, $cont_info; | ||
| 1687 | @cont_text = ($cuddle, 'continue', "{\n\t", | ||
| 1688 | $cont_info->{text} , "\n\b}\cK"); | ||
| 1689 | } | ||
| 1690 | } else { | ||
| 1691 | return info_from_text($op, $self, [''], 'loop_no_body', {}) if !defined $body; | ||
| 1692 | if (defined $init) { | ||
| 1693 | @head_text = ('for', '(', "$init->{text};", "$cond_info->{text};", ")"); | ||
| 1694 | } | ||
| 1695 | @cont_text = ("\cK"); | ||
| 1696 | $body_info = $self->deparse($body, 0, $op); | ||
| 1697 | } | ||
| 1698 | (my $body_text = $body_info->{text}) =~ s/;?$/;\n/; | ||
| 1699 | |||
| 1700 | my @texts = (@head_text, "{\n\t", $body_text, "\b}", @cont_text); | ||
| 1701 | return info_from_list($op, $self, \@texts, ' ', $type, | ||
| 1702 | {body => [$body_info]}); | ||
| 1703 | } | ||
| 1704 | |||
| 1705 | sub pp_leaveloop { | ||
| 1706 | shift->loop_common(@_, undef) | ||
| 1707 | } | ||
| 1708 | |||
| 1709 | sub for_loop { | ||
| 1710 | my $self = shift; | ||
| 1711 | my($op, $cx, $parent) = @_; | ||
| 1712 | my $init = $self->deparse($op, 1, $parent); | ||
| 1713 | my $s = $op->sibling; | ||
| 1714 | my $ll = $s->name eq "unstack" ? $s->sibling : $s->first->sibling; | ||
| 1715 | return $self->loop_common($ll, $cx, $init); | ||
| 1716 | } | ||
| 1717 | |||
| 1718 | sub pp_leavetry { | ||
| 1719 | my ($self, $op, $cx) = @_; | ||
| 1720 | my $leave_info = $self->pp_leave($op, $cx); | ||
| 1721 | return info_from_list($op, $self, ['eval', '{\n\t"', $leave_info->{text}, "\n\b}"], | ||
| 1722 | ' ', 'leavetry', {body=>[$leave_info]}); | ||
| 1723 | } | ||
| 1724 | |||
| 1725 | sub _op_is_or_was { | ||
| 1726 | my ($op, $expect_type) = @_; | ||
| 1727 | my $type = $op->type; | ||
| 1728 | return($type == $expect_type | ||
| 1729 | || ($type == OP_NULL && $op->targ == $expect_type)); | ||
| 1730 | } | ||
| 1731 | |||
| 1732 | sub padname { | ||
| 1733 | my $self = shift; | ||
| 1734 | my $targ = shift; | ||
| 1735 | return $self->padname_sv($targ)->PVX; | ||
| 1736 | } | ||
| 1737 | |||
| 1738 | sub padany { | ||
| 1739 | my $self = shift; | ||
| 1740 | my $op = shift; | ||
| 1741 | return substr($self->padname($op->targ), 1); # skip $/@/% | ||
| 1742 | } | ||
| 1743 | |||
| 1744 | sub pp_padsv { | ||
| 1745 | my $self = shift; | ||
| 1746 | my($op, $cx, $forbid_parens) = @_; | ||
| 1747 | return $self->maybe_my($op, $cx, $self->padname($op->targ), | ||
| 1748 | $forbid_parens); | ||
| 1749 | } | ||
| 1750 | |||
| 1751 | sub pp_padav { pp_padsv(@_) } | ||
| 1752 | sub pp_padhv { pp_padsv(@_) } | ||
| 1753 | |||
| 1754 | my @threadsv_names = B::threadsv_names; | ||
| 1755 | sub pp_threadsv { | ||
| 1756 | my $self = shift; | ||
| 1757 | my($op, $cx) = @_; | ||
| 1758 | return $self->maybe_local_str($op, $cx, "\$" . $threadsv_names[$op->targ]); | ||
| 1759 | } | ||
| 1760 | |||
| 1761 | sub gv_or_padgv { | ||
| 1762 | my $self = shift; | ||
| 1763 | my $op = shift; | ||
| 1764 | if (class($op) eq "PADOP") { | ||
| 1765 | return $self->padval($op->padix); | ||
| 1766 | } else { # class($op) eq "SVOP" | ||
| 1767 | return $op->gv; | ||
| 1768 | } | ||
| 1769 | } | ||
| 1770 | |||
| 1771 | # FIXME: adjust use of maybe_local_str | ||
| 1772 | sub pp_gvsv | ||
| 1773 | { | ||
| 1774 | my($self, $op, $cx) = @_; | ||
| 1775 | my $gv = $self->gv_or_padgv($op); | ||
| 1776 | return $self->maybe_local_str($op, $cx, | ||
| 1777 | $self->stash_variable("\$", | ||
| 1778 | $self->gv_name($gv), $cx)); | ||
| 1779 | } | ||
| 1780 | |||
| 1781 | sub pp_gv | ||
| 1782 | { | ||
| 1783 | my($self, $op, $cx) = @_; | ||
| 1784 | my $gv = $self->gv_or_padgv($op); | ||
| 1785 | return info_from_text($op, $self, $self->gv_name($gv), | ||
| 1786 | 'global variable', {}); | ||
| 1787 | } | ||
| 1788 | |||
| 1789 | sub pp_aelemfast_lex | ||
| 1790 | { | ||
| 1791 | my($self, $op, $cx) = @_; | ||
| 1792 | my $name = $self->padname($op->targ); | ||
| 1793 | $name =~ s/^@/\$/; | ||
| 1794 | return info_from_list($op, $self, [$name, "[", ($op->private + $self->{'arybase'}), "]"], | ||
| 1795 | '', 'pp_aelemfast_lex', {}); | ||
| 1796 | } | ||
| 1797 | |||
| 1798 | sub pp_aelemfast | ||
| 1799 | { | ||
| 1800 | my($self, $op, $cx) = @_; | ||
| 1801 | # optimised PADAV, pre 5.15 | ||
| 1802 | return $self->pp_aelemfast_lex(@_) if ($op->flags & OPf_SPECIAL); | ||
| 1803 | |||
| 1804 | my $gv = $self->gv_or_padgv($op); | ||
| 1805 | my($name,$quoted) = $self->stash_variable_name('@',$gv); | ||
| 1806 | $name = $quoted ? "$name->" : '$' . $name; | ||
| 1807 | my $i = $op->private; | ||
| 1808 | $i -= 256 if $i > 127; | ||
| 1809 | return info_from_list($op, $self, [$name, "[", ($op->private + $self->{'arybase'}), "]"], | ||
| 1810 | '', 'pp_aelemfast', {}); | ||
| 1811 | } | ||
| 1812 | |||
| 1813 | sub pp_rv2sv { maybe_local_str(@_, rv2x(@_, "\$")) } | ||
| 1814 | sub pp_rv2hv { maybe_local_str(@_, rv2x(@_, "%")) } | ||
| 1815 | sub pp_rv2gv { maybe_local_str(@_, rv2x(@_, "*")) } | ||
| 1816 | |||
| 1817 | # skip rv2av | ||
| 1818 | sub pp_av2arylen { | ||
| 1819 | my $self = shift; | ||
| 1820 | my($op, $cx) = @_; | ||
| 1821 | if ($op->first->name eq "padav") { | ||
| 1822 | return $self->maybe_local_str($op, $cx, '$#' . $self->padany($op->first)); | ||
| 1823 | } else { | ||
| 1824 | return $self->maybe_local_str($op, $cx, | ||
| 1825 | $self->rv2x($op->first, $cx, '$#')); | ||
| 1826 | } | ||
| 1827 | } | ||
| 1828 | |||
| 1829 | # skip down to the old, ex-rv2cv | ||
| 1830 | sub pp_rv2cv { | ||
| 1831 | my ($self, $op, $cx) = @_; | ||
| 1832 | my ($type, $str); | ||
| 1833 | if (!null($op->first) && $op->first->name eq 'null' && | ||
| 1834 | $op->first->targ == OP_LIST) | ||
| 1835 | { | ||
| 1836 | $str = $self->rv2x($op->first->first->sibling, $cx, "&"); | ||
| 1837 | $type = 'rv2cv_list'; | ||
| 1838 | } | ||
| 1839 | else { | ||
| 1840 | $str = $self->rv2x($op, $cx, ""); | ||
| 1841 | $type = 'rv2cv'; | ||
| 1842 | } | ||
| 1843 | return info_from_text($op, $self, $str, $type, {}); | ||
| 1844 | } | ||
| 1845 | |||
| 1846 | sub list_const($$$) { | ||
| 1847 | my $self = shift; | ||
| 1848 | my($op, $cx, @list) = @_; | ||
| 1849 | my @a = map $self->const($_, 6), @list; | ||
| 1850 | my @texts = $self->map_texts(\@a); | ||
| 1851 | my $type = 'list_const'; | ||
| 1852 | my $prec = 6; | ||
| 1853 | if (@texts == 0) { | ||
| 1854 | return info_from_list($op, $self, ['(', ')'], '', 'list_const_null', {}); | ||
| 1855 | } elsif (@texts == 1) { | ||
| 1856 | return info_from_text($op, $self, $texts[0], 'list_const_one', | ||
| 1857 | {body => \@a}); | ||
| 1858 | } elsif ( @texts > 2 and !grep(!/^-?\d+$/, @texts)) { | ||
| 1859 | # collapse (-1,0,1,2) into (-1..2) | ||
| 1860 | my ($s, $e) = @texts[0,-1]; | ||
| 1861 | my $i = $s; | ||
| 1862 | unless (grep $i++ != $_, @texts) { | ||
| 1863 | @texts = ($s, '..', $e); | ||
| 1864 | $type = 'list_const_range'; | ||
| 1865 | $prec = 9; | ||
| 1866 | } | ||
| 1867 | } | ||
| 1868 | return info_from_list($op, $self, \@texts, '', $type, | ||
| 1869 | {maybe_parens => [$self, $cx, $prec]}); | ||
| 1870 | } | ||
| 1871 | |||
| 1872 | sub pp_rv2av { | ||
| 1873 | my $self = shift; | ||
| 1874 | my($op, $cx) = @_; | ||
| 1875 | my $kid = $op->first; | ||
| 1876 | if ($kid->name eq "const") { # constant list | ||
| 1877 | my $av = $self->const_sv($kid); | ||
| 1878 | return $self->list_const($kid, $cx, $av->ARRAY); | ||
| 1879 | } else { | ||
| 1880 | return $self->maybe_local_str($op, $cx, | ||
| 1881 | $self->rv2x($op, $cx, "\@")); | ||
| 1882 | } | ||
| 1883 | } | ||
| 1884 | |||
| 1885 | sub elem_or_slice_array_name | ||
| 1886 | { | ||
| 1887 | my $self = shift; | ||
| 1888 | my ($array, $left, $padname, $allow_arrow) = @_; | ||
| 1889 | |||
| 1890 | if ($array->name eq $padname) { | ||
| 1891 | return $self->padany($array); | ||
| 1892 | } elsif (is_scope($array)) { # ${expr}[0] | ||
| 1893 | return "{" . $self->deparse($array, 0) . "}"; | ||
| 1894 | } elsif ($array->name eq "gv") { | ||
| 1895 | ($array, my $quoted) = | ||
| 1896 | $self->stash_variable_name( | ||
| 1897 | $left eq '[' ? '@' : '%', $self->gv_or_padgv($array) | ||
| 1898 | ); | ||
| 1899 | if (!$allow_arrow && $quoted) { | ||
| 1900 | # This cannot happen. | ||
| 1901 | die "Invalid variable name $array for slice"; | ||
| 1902 | } | ||
| 1903 | return $quoted ? "$array->" : $array; | ||
| 1904 | } elsif (!$allow_arrow || is_scalar $array) { # $x[0], $$x[0], ... | ||
| 1905 | return $self->deparse($array, 24)->{text}; | ||
| 1906 | } else { | ||
| 1907 | return undef; | ||
| 1908 | } | ||
| 1909 | } | ||
| 1910 | |||
| 1911 | sub elem_or_slice_single_index($$) | ||
| 1912 | { | ||
| 1913 | my ($self, $idx, $parent) = @_; | ||
| 1914 | |||
| 1915 | my $idx_info = $self->deparse($idx, 1, $parent); | ||
| 1916 | my $idx_str = $idx_info->{text}; | ||
| 1917 | |||
| 1918 | # Outer parens in an array index will confuse perl | ||
| 1919 | # if we're interpolating in a regular expression, i.e. | ||
| 1920 | # /$x$foo[(-1)]/ is *not* the same as /$x$foo[-1]/ | ||
| 1921 | # | ||
| 1922 | # If $self->{parens}, then an initial '(' will | ||
| 1923 | # definitely be paired with a final ')'. If | ||
| 1924 | # !$self->{parens}, the misleading parens won't | ||
| 1925 | # have been added in the first place. | ||
| 1926 | # | ||
| 1927 | # [You might think that we could get "(...)...(...)" | ||
| 1928 | # where the initial and final parens do not match | ||
| 1929 | # each other. But we can't, because the above would | ||
| 1930 | # only happen if there's an infix binop between the | ||
| 1931 | # two pairs of parens, and *that* means that the whole | ||
| 1932 | # expression would be parenthesized as well.] | ||
| 1933 | # | ||
| 1934 | $idx_str =~ s/^\((.*)\)$/$1/ if $self->{'parens'}; | ||
| 1935 | |||
| 1936 | # Hash-element braces will autoquote a bareword inside themselves. | ||
| 1937 | # We need to make sure that C<$hash{warn()}> doesn't come out as | ||
| 1938 | # C<$hash{warn}>, which has a quite different meaning. Currently | ||
| 1939 | # B::Deparse will always quote strings, even if the string was a | ||
| 1940 | # bareword in the original (i.e. the OPpCONST_BARE flag is ignored | ||
| 1941 | # for constant strings.) So we can cheat slightly here - if we see | ||
| 1942 | # a bareword, we know that it is supposed to be a function call. | ||
| 1943 | # | ||
| 1944 | $idx_str =~ s/^([A-Za-z_]\w*)$/$1()/; | ||
| 1945 | |||
| 1946 | return info_from_text($idx_info->{op}, $self, $idx_str, | ||
| 1947 | 'elem_or_slice_single_index', | ||
| 1948 | {body => [$idx_info]}); | ||
| 1949 | } | ||
| 1950 | |||
| 1951 | sub elem | ||
| 1952 | { | ||
| 1953 | my ($self, $op, $cx, $left, $right, $padname) = @_; | ||
| 1954 | my($array, $idx) = ($op->first, $op->first->sibling); | ||
| 1955 | |||
| 1956 | my $idx_info = $self->elem_or_slice_single_index($idx, $op); | ||
| 1957 | my $opts = {body => [$idx_info]}; | ||
| 1958 | |||
| 1959 | unless ($array->name eq $padname) { # Maybe this has been fixed | ||
| 1960 | $opts->{other_ops} = [$array]; | ||
| 1961 | $array = $array->first; # skip rv2av (or ex-rv2av in _53+) | ||
| 1962 | } | ||
| 1963 | my @texts = (); | ||
| 1964 | my $info; | ||
| 1965 | my $array_name=$self->elem_or_slice_array_name($array, $left, $padname, 1); | ||
| 1966 | if ($array_name) { | ||
| 1967 | if ($array_name !~ /->\z/) { | ||
| 1968 | if ($array_name eq '#') { | ||
| 1969 | $array_name = '${#}'; | ||
| 1970 | } else { | ||
| 1971 | $array_name = '$' . $array_name ; | ||
| 1972 | } | ||
| 1973 | } | ||
| 1974 | push @texts, $array_name; | ||
| 1975 | push @texts, $left if $left; | ||
| 1976 | push @texts, $idx_info->{text}, $right; | ||
| 1977 | return info_from_list($op, $self, \@texts, '', 'elem', $opts) | ||
| 1978 | } else { | ||
| 1979 | # $x[20][3]{hi} or expr->[20] | ||
| 1980 | my $type; | ||
| 1981 | my $array_info = $self->deparse($array, 24, $op); | ||
| 1982 | push @{$info->{body}}, $array_info; | ||
| 1983 | @texts = ($array_info->{text}); | ||
| 1984 | if (is_subscriptable($array)) { | ||
| 1985 | push @texts, $left, $idx_info->{text}, $right; | ||
| 1986 | $type = 'elem_no_arrow'; | ||
| 1987 | } else { | ||
| 1988 | push @texts, '->', $left, $idx_info->{text}, $right; | ||
| 1989 | $type = 'elem_arrow'; | ||
| 1990 | } | ||
| 1991 | return info_from_list($op, $self, \@texts, '', $type, $opts); | ||
| 1992 | } | ||
| 1993 | Carp::confess("unhandled condition in elem"); | ||
| 1994 | } | ||
| 1995 | |||
| 1996 | # a simplified version of elem_or_slice_array_name() | ||
| 1997 | # for the use of pp_multideref | ||
| 1998 | |||
| 1999 | sub multideref_var_name($$$) | ||
| 2000 | { | ||
| 2001 | my ($self, $gv, $is_hash) = @_; | ||
| 2002 | |||
| 2003 | my ($name, $quoted) = | ||
| 2004 | $self->stash_variable_name( $is_hash ? '%' : '@', $gv); | ||
| 2005 | return $quoted ? "$name->" | ||
| 2006 | : $name eq '#' | ||
| 2007 | ? '${#}' # avoid ${#}[1] => $#[1] | ||
| 2008 | : '$' . $name; | ||
| 2009 | } | ||
| 2010 | |||
| 2011 | sub pp_multideref | ||
| 2012 | { | ||
| 2013 | my($self, $op, $cx) = @_; | ||
| 2014 | my @texts = (); | ||
| 2015 | |||
| 2016 | if ($op->private & OPpMULTIDEREF_EXISTS) { | ||
| 2017 | @texts = ($self->keyword("exists"), ' '); | ||
| 2018 | } | ||
| 2019 | elsif ($op->private & OPpMULTIDEREF_DELETE) { | ||
| 2020 | @texts = ($self->keyword("delete"), ' ') | ||
| 2021 | } | ||
| 2022 | elsif ($op->private & OPpLVAL_INTRO) { | ||
| 2023 | @texts = ($self->keyword("local"), ' ') | ||
| 2024 | } | ||
| 2025 | |||
| 2026 | if ($op->first && ($op->first->flags & OPf_KIDS)) { | ||
| 2027 | # arbitrary initial expression, e.g. f(1,2,3)->[...] | ||
| 2028 | my $first = $self->deparse($op->first, 24, $op); | ||
| 2029 | push @texts, $first->{text}; | ||
| 2030 | } | ||
| 2031 | |||
| 2032 | my @items = $op->aux_list($self->{curcv}); | ||
| 2033 | my $actions = shift @items; | ||
| 2034 | |||
| 2035 | my $is_hash; | ||
| 2036 | my $derefs = 0; | ||
| 2037 | |||
| 2038 | while (1) { | ||
| 2039 | if (($actions & MDEREF_ACTION_MASK) == MDEREF_reload) { | ||
| 2040 | $actions = shift @items; | ||
| 2041 | next; | ||
| 2042 | } | ||
| 2043 | |||
| 2044 | $is_hash = ( | ||
| 2045 | ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_pop_rv2hv_helem | ||
| 2046 | || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_gvsv_vivify_rv2hv_helem | ||
| 2047 | || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_padsv_vivify_rv2hv_helem | ||
| 2048 | || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_vivify_rv2hv_helem | ||
| 2049 | || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_padhv_helem | ||
| 2050 | || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_gvhv_helem | ||
| 2051 | ); | ||
| 2052 | |||
| 2053 | if ( ($actions & MDEREF_ACTION_MASK) == MDEREF_AV_padav_aelem | ||
| 2054 | || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_padhv_helem) | ||
| 2055 | { | ||
| 2056 | $derefs = 1; | ||
| 2057 | push @texts, '$' . substr($self->padname(shift @items), 1); | ||
| 2058 | } | ||
| 2059 | elsif ( ($actions & MDEREF_ACTION_MASK) == MDEREF_AV_gvav_aelem | ||
| 2060 | || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_gvhv_helem) | ||
| 2061 | { | ||
| 2062 | $derefs = 1; | ||
| 2063 | push @texts, $self->multideref_var_name(shift @items, $is_hash); | ||
| 2064 | } | ||
| 2065 | else { | ||
| 2066 | if ( ($actions & MDEREF_ACTION_MASK) == | ||
| 2067 | MDEREF_AV_padsv_vivify_rv2av_aelem | ||
| 2068 | || ($actions & MDEREF_ACTION_MASK) == | ||
| 2069 | MDEREF_HV_padsv_vivify_rv2hv_helem) | ||
| 2070 | { | ||
| 2071 | push @texts, $self->padname(shift @items); | ||
| 2072 | } | ||
| 2073 | elsif ( ($actions & MDEREF_ACTION_MASK) == | ||
| 2074 | MDEREF_AV_gvsv_vivify_rv2av_aelem | ||
| 2075 | || ($actions & MDEREF_ACTION_MASK) == | ||
| 2076 | MDEREF_HV_gvsv_vivify_rv2hv_helem) | ||
| 2077 | { | ||
| 2078 | push @texts, $self->multideref_var_name(shift @items, $is_hash); | ||
| 2079 | } | ||
| 2080 | elsif ( ($actions & MDEREF_ACTION_MASK) == | ||
| 2081 | MDEREF_AV_pop_rv2av_aelem | ||
| 2082 | || ($actions & MDEREF_ACTION_MASK) == | ||
| 2083 | MDEREF_HV_pop_rv2hv_helem) | ||
| 2084 | { | ||
| 2085 | if ( ($op->flags & OPf_KIDS) | ||
| 2086 | && ( _op_is_or_was($op->first, OP_RV2AV) | ||
| 2087 | || _op_is_or_was($op->first, OP_RV2HV)) | ||
| 2088 | && ($op->first->flags & OPf_KIDS) | ||
| 2089 | && ( _op_is_or_was($op->first->first, OP_AELEM) | ||
| 2090 | || _op_is_or_was($op->first->first, OP_HELEM)) | ||
| 2091 | ) | ||
| 2092 | { | ||
| 2093 | $derefs++; | ||
| 2094 | } | ||
| 2095 | } | ||
| 2096 | |||
| 2097 | push(@texts, '->') if !$derefs++; | ||
| 2098 | } | ||
| 2099 | |||
| 2100 | |||
| 2101 | if (($actions & MDEREF_INDEX_MASK) == MDEREF_INDEX_none) { | ||
| 2102 | last; | ||
| 2103 | } | ||
| 2104 | |||
| 2105 | push(@texts, $is_hash ? '{' : '['); | ||
| 2106 | |||
| 2107 | if (($actions & MDEREF_INDEX_MASK) == MDEREF_INDEX_const) { | ||
| 2108 | my $key = shift @items; | ||
| 2109 | if ($is_hash) { | ||
| 2110 | push @texts, $self->const($key, $cx)->{text}; | ||
| 2111 | } | ||
| 2112 | else { | ||
| 2113 | push @texts, $key; | ||
| 2114 | } | ||
| 2115 | } | ||
| 2116 | elsif (($actions & MDEREF_INDEX_MASK) == MDEREF_INDEX_padsv) { | ||
| 2117 | push @texts, $self->padname(shift @items); | ||
| 2118 | } | ||
| 2119 | elsif (($actions & MDEREF_INDEX_MASK) == MDEREF_INDEX_gvsv) { | ||
| 2120 | push @texts,('$' . ($self->stash_variable_name('$', shift @items))[0]); | ||
| 2121 | } | ||
| 2122 | |||
| 2123 | push(@texts, $is_hash ? '}' : ']'); | ||
| 2124 | |||
| 2125 | if ($actions & MDEREF_FLAG_last) { | ||
| 2126 | last; | ||
| 2127 | } | ||
| 2128 | $actions >>= MDEREF_SHIFT; | ||
| 2129 | } | ||
| 2130 | |||
| 2131 | return info_from_list($op, $self, \@texts, '', 'multideref', {}); | ||
| 2132 | } | ||
| 2133 | |||
| 2134 | sub pp_aelem { maybe_local(@_, elem(@_, "[", "]", "padav")) } | ||
| 2135 | sub pp_helem { maybe_local(@_, elem(@_, "{", "}", "padhv")) } | ||
| 2136 | |||
| 2137 | sub pp_gelem | ||
| 2138 | { | ||
| 2139 | my($self, $op, $cx) = @_; | ||
| 2140 | my($glob, $part) = ($op->first, $op->last); | ||
| 2141 | $glob = $glob->first; # skip rv2gv | ||
| 2142 | $glob = $glob->first if $glob->name eq "rv2gv"; # this one's a bug | ||
| 2143 | my $scope = is_scope($glob); | ||
| 2144 | $glob = $self->deparse($glob, 0); | ||
| 2145 | $part = $self->deparse($part, 1); | ||
| 2146 | return "*" . ($scope ? "{$glob}" : $glob) . "{$part}"; | ||
| 2147 | } | ||
| 2148 | |||
| 2149 | sub slice | ||
| 2150 | { | ||
| 2151 | my ($self, $op, $cx, $left, $right, $regname, $padname) = @_; | ||
| 2152 | my $last; | ||
| 2153 | my(@elems, $kid, $array); | ||
| 2154 | if (class($op) eq "LISTOP") { | ||
| 2155 | $last = $op->last; | ||
| 2156 | } else { # ex-hslice inside delete() | ||
| 2157 | for ($kid = $op->first; !null $kid->sibling; $kid = $kid->sibling) {} | ||
| 2158 | $last = $kid; | ||
| 2159 | } | ||
| 2160 | $array = $last; | ||
| 2161 | $array = $array->first | ||
| 2162 | if $array->name eq $regname or $array->name eq "null"; | ||
| 2163 | my $array_info = $self->elem_or_slice_array_name($array, $left, $padname, 0); | ||
| 2164 | $kid = $op->first->sibling; # skip pushmark | ||
| 2165 | |||
| 2166 | if ($kid->name eq "list") { | ||
| 2167 | # skip list, pushmark | ||
| 2168 | $kid = $kid->first->sibling; | ||
| 2169 | for (; !null $kid; $kid = $kid->sibling) { | ||
| 2170 | push @elems, $self->deparse($kid, 6, $op); | ||
| 2171 | } | ||
| 2172 | } else { | ||
| 2173 | @elems = ($self->elem_or_slice_single_index($kid, $op)); | ||
| 2174 | } | ||
| 2175 | my $list = join(', ', map($_->{text}, @elems)); | ||
| 2176 | my $lead = '@'; | ||
| 2177 | $lead = '%' if $op->name =~ /^kv/i; | ||
| 2178 | my (@texts, $type); | ||
| 2179 | if ($array_info) { | ||
| 2180 | @texts = ($lead, $array_info, $left, $list, $right); | ||
| 2181 | $type='slice1'; | ||
| 2182 | } else { | ||
| 2183 | @texts = ($lead, $left, $list, $right); | ||
| 2184 | $type='slice'; | ||
| 2185 | } | ||
| 2186 | return info_from_list($op, $self, \@texts, '', $type, {body => \@elems}); | ||
| 2187 | } | ||
| 2188 | |||
| 2189 | sub pp_aslice { maybe_local(@_, slice(@_, "[", "]", "rv2av", "padav")) } | ||
| 2190 | sub pp_kvaslice { slice(@_, "[", "]", "rv2av", "padav") } | ||
| 2191 | sub pp_hslice { maybe_local(@_, slice(@_, "{", "}", "rv2hv", "padhv")) } | ||
| 2192 | sub pp_kvhslice { slice(@_, "{", "}", "rv2hv", "padhv") } | ||
| 2193 | |||
| 2194 | sub pp_lslice | ||
| 2195 | { | ||
| 2196 | my ($self, $op, $cs) = @_; | ||
| 2197 | my $idx = $op->first; | ||
| 2198 | my $list = $op->last; | ||
| 2199 | my(@elems, $kid); | ||
| 2200 | my $list_info = $self->deparse($list, 1, $op); | ||
| 2201 | my $idx_info = $self->deparse($idx, 1, $op); | ||
| 2202 | return info_from_list($op, $self, ['(', $list_info->{text}, ')', '[', $idx_info->{text}, ']'], | ||
| 2203 | '', 'lslice', {body=>[$list_info, $idx_info]}); | ||
| 2204 | } | ||
| 2205 | |||
| 2206 | sub want_scalar | ||
| 2207 | { | ||
| 2208 | my $op = shift; | ||
| 2209 | return ($op->flags & OPf_WANT) == OPf_WANT_SCALAR; | ||
| 2210 | } | ||
| 2211 | |||
| 2212 | sub want_list | ||
| 2213 | { | ||
| 2214 | my $op = shift; | ||
| 2215 | return ($op->flags & OPf_WANT) == OPf_WANT_LIST; | ||
| 2216 | } | ||
| 2217 | |||
| 2218 | sub _method | ||
| 2219 | { | ||
| 2220 | my($self, $op, $cx) = @_; | ||
| 2221 | my @other_ops = ($op->first); | ||
| 2222 | my $kid = $op->first->sibling; # skip pushmark | ||
| 2223 | my($meth, $obj, @exprs); | ||
| 2224 | if ($kid->name eq "list" and want_list $kid) { | ||
| 2225 | # When an indirect object isn't a bareword but the args are in | ||
| 2226 | # parens, the parens aren't part of the method syntax (the LLAFR | ||
| 2227 | # doesn't apply), but they make a list with OPf_PARENS set that | ||
| 2228 | # doesn't get flattened by the append_elem that adds the method, | ||
| 2229 | # making a (object, arg1, arg2, ...) list where the object | ||
| 2230 | # usually is. This can be distinguished from | ||
| 2231 | # '($obj, $arg1, $arg2)->meth()' (which is legal if $arg2 is an | ||
| 2232 | # object) because in the later the list is in scalar context | ||
| 2233 | # as the left side of -> always is, while in the former | ||
| 2234 | # the list is in list context as method arguments always are. | ||
| 2235 | # (Good thing there aren't method prototypes!) | ||
| 2236 | $meth = $kid->sibling; | ||
| 2237 | push @other_ops, $kid->first; | ||
| 2238 | $kid = $kid->first->sibling; # skip pushmark | ||
| 2239 | $obj = $kid; | ||
| 2240 | $kid = $kid->sibling; | ||
| 2241 | for (; not null $kid; $kid = $kid->sibling) { | ||
| 2242 | push @exprs, $kid; | ||
| 2243 | } | ||
| 2244 | } else { | ||
| 2245 | $obj = $kid; | ||
| 2246 | $kid = $kid->sibling; | ||
| 2247 | for (; !null ($kid->sibling) && $kid->name!~/^method(?:_named)?\z/; | ||
| 2248 | $kid = $kid->sibling) { | ||
| 2249 | push @exprs, $kid | ||
| 2250 | } | ||
| 2251 | $meth = $kid; | ||
| 2252 | } | ||
| 2253 | |||
| 2254 | if ($meth->name eq "method_named") { | ||
| 2255 | $meth = $self->meth_sv($meth)->PV; | ||
| 2256 | } elsif ($meth->name eq "method_super") { | ||
| 2257 | $meth = "SUPER::".$self->meth_sv($meth)->PV; | ||
| 2258 | } elsif ($meth->name eq "method_redir") { | ||
| 2259 | $meth = $self->meth_rclass_sv($meth)->PV.'::'.$self->meth_sv($meth)->PV; | ||
| 2260 | } elsif ($meth->name eq "method_redir_super") { | ||
| 2261 | $meth = $self->meth_rclass_sv($meth)->PV.'::SUPER::'. | ||
| 2262 | $self->meth_sv($meth)->PV; | ||
| 2263 | } else { | ||
| 2264 | $meth = $meth->first; | ||
| 2265 | if ($meth->name eq "const") { | ||
| 2266 | # As of 5.005_58, this case is probably obsoleted by the | ||
| 2267 | # method_named case above | ||
| 2268 | $meth = $self->const_sv($meth)->PV; # needs to be bare | ||
| 2269 | } | ||
| 2270 | } | ||
| 2271 | |||
| 2272 | return { | ||
| 2273 | method => $meth, | ||
| 2274 | variable_method => ref($meth), | ||
| 2275 | object => $obj, | ||
| 2276 | args => \@exprs, | ||
| 2277 | other_ops => \@other_ops | ||
| 2278 | }, $cx; | ||
| 2279 | } | ||
| 2280 | |||
| 2281 | sub e_method { | ||
| 2282 | my ($self, $op, $minfo, $cx) = @_; | ||
| 2283 | my $obj = $self->deparse($minfo->{object}, 24, $op); | ||
| 2284 | my @body = ($obj); | ||
| 2285 | my $other_ops = $minfo->{other_ops}; | ||
| 2286 | |||
| 2287 | my $meth = $minfo->{method}; | ||
| 2288 | my $meth_info; | ||
| 2289 | if ($minfo->{variable_method}) { | ||
| 2290 | $meth_info = $self->deparse($meth, 1, $op); | ||
| 2291 | push @body, $meth_info; | ||
| 2292 | } | ||
| 2293 | my @args = map { $self->deparse($_, 6, $op) } @{$minfo->{args}}; | ||
| 2294 | push @body, @args; | ||
| 2295 | my @args_texts = map $_->{text}, @args; | ||
| 2296 | my $args = join(", ", @args_texts); | ||
| 2297 | |||
| 2298 | my $opts = {body => \@body, other_ops => $other_ops}; | ||
| 2299 | my @texts = (); | ||
| 2300 | my $type; | ||
| 2301 | |||
| 2302 | if ($minfo->{object}->name eq 'scope' && want_list $minfo->{object}) { | ||
| 2303 | # method { $object } | ||
| 2304 | # This must be deparsed this way to preserve list context | ||
| 2305 | # of $object. | ||
| 2306 | my $need_paren = $cx >= 6; | ||
| 2307 | if ($need_paren) { | ||
| 2308 | @texts = ('(', $meth, substr($obj,2), | ||
| 2309 | $args, ')'); | ||
| 2310 | $type = 'e_method_list_paren'; | ||
| 2311 | } else { | ||
| 2312 | @texts = ($meth, substr($obj,2), $args); | ||
| 2313 | $type = 'e_method_list'; | ||
| 2314 | } | ||
| 2315 | return info_from_list($op, $self, \@texts, '', $type, $opts); | ||
| 2316 | } | ||
| 2317 | if (length $args) { | ||
| 2318 | @texts = ($obj->{text}, '->', $meth, '(', $args, ')'); | ||
| 2319 | $type = 'e_method_args'; | ||
| 2320 | } else { | ||
| 2321 | @texts = ($obj->{text}, '->', $meth); | ||
| 2322 | $type = 'e_method_null'; | ||
| 2323 | } | ||
| 2324 | return info_from_list($op, $self, \@texts, '', $type, $opts); | ||
| 2325 | } | ||
| 2326 | |||
| 2327 | # returns "&" and the argument bodies if the prototype doesn't match the args, | ||
| 2328 | # or ("", $args_after_prototype_demunging) if it does. | ||
| 2329 | sub check_proto { | ||
| 2330 | my $self = shift; | ||
| 2331 | my $op = shift; | ||
| 2332 | return ('&', []) if $self->{'noproto'}; | ||
| 2333 | my($proto, @args) = @_; | ||
| 2334 | my($arg, $real); | ||
| 2335 | my $doneok = 0; | ||
| 2336 | my @reals; | ||
| 2337 | # An unbackslashed @ or % gobbles up the rest of the args | ||
| 2338 | 1 while $proto =~ s/(?<!\\)([@%])[^\]]+$/$1/; | ||
| 2339 | $proto =~ s/^\s*//; | ||
| 2340 | while ($proto) { | ||
| 2341 | $proto =~ s/^(\\?[\$\@&%*_]|\\\[[\$\@&%*]+\]|;)\s*//; | ||
| 2342 | my $chr = $1; | ||
| 2343 | if ($chr eq "") { | ||
| 2344 | return ('&', []) if @args; | ||
| 2345 | } elsif ($chr eq ";") { | ||
| 2346 | $doneok = 1; | ||
| 2347 | } elsif ($chr eq "@" or $chr eq "%") { | ||
| 2348 | push @reals, map($self->deparse($_, 6), @args, $op); | ||
| 2349 | @args = (); | ||
| 2350 | } else { | ||
| 2351 | $arg = shift @args; | ||
| 2352 | last unless $arg; | ||
| 2353 | if ($chr eq "\$" || $chr eq "_") { | ||
| 2354 | if (want_scalar $arg) { | ||
| 2355 | push @reals, $self->deparse($arg, 6, $op); | ||
| 2356 | } else { | ||
| 2357 | return ('&', []); | ||
| 2358 | } | ||
| 2359 | } elsif ($chr eq "&") { | ||
| 2360 | if ($arg->name =~ /^(s?refgen|undef)$/) { | ||
| 2361 | push @reals, $self->deparse($arg, 6, $op); | ||
| 2362 | } else { | ||
| 2363 | return ('&', []); | ||
| 2364 | } | ||
| 2365 | } elsif ($chr eq "*") { | ||
| 2366 | if ($arg->name =~ /^s?refgen$/ | ||
| 2367 | and $arg->first->first->name eq "rv2gv") | ||
| 2368 | { | ||
| 2369 | $real = $arg->first->first; # skip refgen, null | ||
| 2370 | if ($real->first->name eq "gv") { | ||
| 2371 | push @reals, $self->deparse($real, 6, $op); | ||
| 2372 | } else { | ||
| 2373 | push @reals, $self->deparse($real->first, 6, $op); | ||
| 2374 | } | ||
| 2375 | } else { | ||
| 2376 | return ('&', []); | ||
| 2377 | } | ||
| 2378 | } elsif (substr($chr, 0, 1) eq "\\") { | ||
| 2379 | $chr =~ tr/\\[]//d; | ||
| 2380 | if ($arg->name =~ /^s?refgen$/ and | ||
| 2381 | !null($real = $arg->first) and | ||
| 2382 | ($chr =~ /\$/ && is_scalar($real->first) | ||
| 2383 | or ($chr =~ /@/ | ||
| 2384 | && class($real->first->sibling) ne 'NULL' | ||
| 2385 | && $real->first->sibling->name | ||
| 2386 | =~ /^(rv2|pad)av$/) | ||
| 2387 | or ($chr =~ /%/ | ||
| 2388 | && class($real->first->sibling) ne 'NULL' | ||
| 2389 | && $real->first->sibling->name | ||
| 2390 | =~ /^(rv2|pad)hv$/) | ||
| 2391 | #or ($chr =~ /&/ # This doesn't work | ||
| 2392 | # && $real->first->name eq "rv2cv") | ||
| 2393 | or ($chr =~ /\*/ | ||
| 2394 | && $real->first->name eq "rv2gv"))) | ||
| 2395 | { | ||
| 2396 | push @reals, $self->deparse($real, 6, $op); | ||
| 2397 | } else { | ||
| 2398 | return ('&', []); | ||
| 2399 | } | ||
| 2400 | } | ||
| 2401 | } | ||
| 2402 | } | ||
| 2403 | return ('&', []) if $proto and !$doneok; # too few args and no ';' | ||
| 2404 | return ('&', []) if @args; # too many args | ||
| 2405 | return ('', \@reals); | ||
| 2406 | } | ||
| 2407 | |||
| 2408 | sub pp_enterwrite { unop(@_, "write") } | ||
| 2409 | |||
| 2410 | # Split a floating point number into an integer mantissa and a binary | ||
| 2411 | # exponent. Assumes you've already made sure the number isn't zero or | ||
| 2412 | # some weird infinity or NaN. | ||
| 2413 | sub split_float { | ||
| 2414 | my($f) = @_; | ||
| 2415 | my $exponent = 0; | ||
| 2416 | if ($f == int($f)) { | ||
| 2417 | while ($f % 2 == 0) { | ||
| 2418 | $f /= 2; | ||
| 2419 | $exponent++; | ||
| 2420 | } | ||
| 2421 | } else { | ||
| 2422 | while ($f != int($f)) { | ||
| 2423 | $f *= 2; | ||
| 2424 | $exponent--; | ||
| 2425 | } | ||
| 2426 | } | ||
| 2427 | my $mantissa = sprintf("%.0f", $f); | ||
| 2428 | return ($mantissa, $exponent); | ||
| 2429 | } | ||
| 2430 | |||
| 2431 | sub dq | ||
| 2432 | { | ||
| 2433 | my ($self, $op, $parent) = @_; | ||
| 2434 | my $type = $op->name; | ||
| 2435 | my $info; | ||
| 2436 | if ($type eq "const") { | ||
| 2437 | return info_from_text($op, $self, '$[', 'dq_const_ary', {}) if $op->private & OPpCONST_ARYBASE; | ||
| 2438 | return info_from_text($op, $self, | ||
| 2439 | B::Deparse::uninterp(B::Deparse::escape_str(B::Deparse::unback($self->const_sv($op)->as_string))), | ||
| 2440 | 'dq_const', {}); | ||
| 2441 | } elsif ($type eq "concat") { | ||
| 2442 | my $first = $self->dq($op->first, $op); | ||
| 2443 | my $last = $self->dq($op->last, $op); | ||
| 2444 | |||
| 2445 | # Disambiguate "${foo}bar", "${foo}{bar}", "${foo}[1]", "$foo\::bar" | ||
| 2446 | ($last =~ /^[A-Z\\\^\[\]_?]/ && | ||
| 2447 | $first =~ s/([\$@])\^$/${1}{^}/) # "${^}W" etc | ||
| 2448 | || ($last =~ /^[:'{\[\w_]/ && #' | ||
| 2449 | $first =~ s/([\$@])([A-Za-z_]\w*)$/${1}{$2}/); | ||
| 2450 | |||
| 2451 | return info_from_list($op, $self, [$first->{text}, $last->{text}], '', 'dq_concat', | ||
| 2452 | {body => [$first, $last]}); | ||
| 2453 | } elsif ($type eq "join") { | ||
| 2454 | return $self->deparse($op->last, 26, $op); # was join($", @ary) | ||
| 2455 | } else { | ||
| 2456 | return $self->deparse($op, 26, $parent); | ||
| 2457 | } | ||
| 2458 | my $kid = $self->dq($op->first->sibling, $op); | ||
| 2459 | my $kid_text = $kid->{text}; | ||
| 2460 | if ($type eq "uc") { | ||
| 2461 | $info = info_from_lists(['\U', $kid, '\E'], '', 'dq_uc', {}); | ||
| 2462 | } elsif ($type eq "lc") { | ||
| 2463 | $info = info_from_lists(['\L', $kid, '\E'], '', 'dq_lc', {}); | ||
| 2464 | } elsif ($type eq "ucfirst") { | ||
| 2465 | $info = info_from_lists(['\u', $kid, '\E'], '', 'dq_ucfirst', {}); | ||
| 2466 | } elsif ($type eq "lcfirst") { | ||
| 2467 | $info = info_from_lists(['\l', $kid, '\E'], '', 'dq_lcfirst', {}); | ||
| 2468 | } elsif ($type eq "quotemeta") { | ||
| 2469 | $info = info_from_lists(['\Q', $kid, '\E'], '', 'dq_quotemeta', {}); | ||
| 2470 | } elsif ($type eq "fc") { | ||
| 2471 | $info = info_from_lists(['\F', $kid, '\E'], '', 'dq_fc', {}); | ||
| 2472 | } | ||
| 2473 | $info->{body} = [$kid]; | ||
| 2474 | return $info; | ||
| 2475 | } | ||
| 2476 | |||
| 2477 | sub pp_backtick | ||
| 2478 | { | ||
| 2479 | my($self, $op, $cx) = @_; | ||
| 2480 | # skip pushmark if it exists (readpipe() vs ``) | ||
| 2481 | my $child = $op->first->sibling->isa('B::NULL') | ||
| 2482 | ? $op->first : $op->first->sibling; | ||
| 2483 | if ($self->pure_string($child)) { | ||
| 2484 | return $self->single_delim($op, "qx", '`', $self->dq($child, 1)->{text}); | ||
| 2485 | } | ||
| 2486 | unop($self, $op, $cx, "readpipe"); | ||
| 2487 | } | ||
| 2488 | |||
| 2489 | # OP_STRINGIFY is a listop, but it only ever has one arg | ||
| 2490 | sub pp_stringify { | ||
| 2491 | my ($self, $op, $cx) = @_; | ||
| 2492 | my $kid = $op->first->sibling; | ||
| 2493 | my @other_ops = (); | ||
| 2494 | while ($kid->name eq 'null' && !null($kid->first)) { | ||
| 2495 | push(@other_ops, $kid); | ||
| 2496 | $kid = $kid->first; | ||
| 2497 | } | ||
| 2498 | my $info; | ||
| 2499 | if ($kid->name =~ /^(?:const|padsv|rv2sv|av2arylen|gvsv|multideref | ||
| 2500 | |aelemfast(?:_lex)?|[ah]elem|join|concat)\z/x) { | ||
| 2501 | $info = maybe_targmy(@_, \&dquote); | ||
| 2502 | } | ||
| 2503 | else { | ||
| 2504 | # Actually an optimised join. | ||
| 2505 | my $info = listop(@_,"join"); | ||
| 2506 | $info->{text} =~ s/join([( ])/join$1$self->{'ex_const'}, /; | ||
| 2507 | } | ||
| 2508 | push @{$info->{other_ops}}, @other_ops; | ||
| 2509 | return $info; | ||
| 2510 | } | ||
| 2511 | |||
| 2512 | |||
| 2513 | # tr/// and s/// (and tr[][], tr[]//, tr###, etc) | ||
| 2514 | # note that tr(from)/to/ is OK, but not tr/from/(to) | ||
| 2515 | sub double_delim { | ||
| 2516 | my($from, $to) = @_; | ||
| 2517 | my($succeed, $delim); | ||
| 2518 | if ($from !~ m[/] and $to !~ m[/]) { | ||
| 2519 | return "/$from/$to/"; | ||
| 2520 | } elsif (($succeed, $from) = balanced_delim($from) and $succeed) { | ||
| 2521 | if (($succeed, $to) = balanced_delim($to) and $succeed) { | ||
| 2522 | return "$from$to"; | ||
| 2523 | } else { | ||
| 2524 | for $delim ('/', '"', '#') { # note no "'" -- s''' is special | ||
| 2525 | return "$from$delim$to$delim" if index($to, $delim) == -1; | ||
| 2526 | } | ||
| 2527 | $to =~ s[/][\\/]g; | ||
| 2528 | return "$from/$to/"; | ||
| 2529 | } | ||
| 2530 | } else { | ||
| 2531 | for $delim ('/', '"', '#') { # note no ' | ||
| 2532 | return "$delim$from$delim$to$delim" | ||
| 2533 | if index($to . $from, $delim) == -1; | ||
| 2534 | } | ||
| 2535 | $from =~ s[/][\\/]g; | ||
| 2536 | $to =~ s[/][\\/]g; | ||
| 2537 | return "/$from/$to/"; | ||
| 2538 | } | ||
| 2539 | } | ||
| 2540 | |||
| 2541 | # Only used by tr///, so backslashes hyphens | ||
| 2542 | sub pchr { # ASCII | ||
| 2543 | my($n) = @_; | ||
| 2544 | if ($n == ord '\\') { | ||
| 2545 | return '\\\\'; | ||
| 2546 | } elsif ($n == ord "-") { | ||
| 2547 | return "\\-"; | ||
| 2548 | } elsif ($n >= ord(' ') and $n <= ord('~')) { | ||
| 2549 | return chr($n); | ||
| 2550 | } elsif ($n == ord "\a") { | ||
| 2551 | return '\\a'; | ||
| 2552 | } elsif ($n == ord "\b") { | ||
| 2553 | return '\\b'; | ||
| 2554 | } elsif ($n == ord "\t") { | ||
| 2555 | return '\\t'; | ||
| 2556 | } elsif ($n == ord "\n") { | ||
| 2557 | return '\\n'; | ||
| 2558 | } elsif ($n == ord "\e") { | ||
| 2559 | return '\\e'; | ||
| 2560 | } elsif ($n == ord "\f") { | ||
| 2561 | return '\\f'; | ||
| 2562 | } elsif ($n == ord "\r") { | ||
| 2563 | return '\\r'; | ||
| 2564 | } elsif ($n >= ord("\cA") and $n <= ord("\cZ")) { | ||
| 2565 | return '\\c' . chr(ord("@") + $n); | ||
| 2566 | } else { | ||
| 2567 | # return '\x' . sprintf("%02x", $n); | ||
| 2568 | return '\\' . sprintf("%03o", $n); | ||
| 2569 | } | ||
| 2570 | } | ||
| 2571 | |||
| 2572 | sub collapse { | ||
| 2573 | my(@chars) = @_; | ||
| 2574 | my($str, $c, $tr) = (""); | ||
| 2575 | for ($c = 0; $c < @chars; $c++) { | ||
| 2576 | $tr = $chars[$c]; | ||
| 2577 | $str .= pchr($tr); | ||
| 2578 | if ($c <= $#chars - 2 and $chars[$c + 1] == $tr + 1 and | ||
| 2579 | $chars[$c + 2] == $tr + 2) | ||
| 2580 | { | ||
| 2581 | for (; $c <= $#chars-1 and $chars[$c + 1] == $chars[$c] + 1; $c++) | ||
| 2582 | {} | ||
| 2583 | $str .= "-"; | ||
| 2584 | $str .= pchr($chars[$c]); | ||
| 2585 | } | ||
| 2586 | } | ||
| 2587 | return $str; | ||
| 2588 | } | ||
| 2589 | |||
| 2590 | sub tr_decode_byte { | ||
| 2591 | my($table, $flags) = @_; | ||
| 2592 | my(@table) = unpack("s*", $table); | ||
| 2593 | splice @table, 0x100, 1; # Number of subsequent elements | ||
| 2594 | my($c, $tr, @from, @to, @delfrom, $delhyphen); | ||
| 2595 | if ($table[ord "-"] != -1 and | ||
| 2596 | $table[ord("-") - 1] == -1 || $table[ord("-") + 1] == -1) | ||
| 2597 | { | ||
| 2598 | $tr = $table[ord "-"]; | ||
| 2599 | $table[ord "-"] = -1; | ||
| 2600 | if ($tr >= 0) { | ||
| 2601 | @from = ord("-"); | ||
| 2602 | @to = $tr; | ||
| 2603 | } else { # -2 ==> delete | ||
| 2604 | $delhyphen = 1; | ||
| 2605 | } | ||
| 2606 | } | ||
| 2607 | for ($c = 0; $c < @table; $c++) { | ||
| 2608 | $tr = $table[$c]; | ||
| 2609 | if ($tr >= 0) { | ||
| 2610 | push @from, $c; push @to, $tr; | ||
| 2611 | } elsif ($tr == -2) { | ||
| 2612 | push @delfrom, $c; | ||
| 2613 | } | ||
| 2614 | } | ||
| 2615 | @from = (@from, @delfrom); | ||
| 2616 | if ($flags & OPpTRANS_COMPLEMENT) { | ||
| 2617 | my @newfrom = (); | ||
| 2618 | my %from; | ||
| 2619 | @from{@from} = (1) x @from; | ||
| 2620 | for ($c = 0; $c < 256; $c++) { | ||
| 2621 | push @newfrom, $c unless $from{$c}; | ||
| 2622 | } | ||
| 2623 | @from = @newfrom; | ||
| 2624 | } | ||
| 2625 | unless ($flags & OPpTRANS_DELETE || !@to) { | ||
| 2626 | pop @to while $#to and $to[$#to] == $to[$#to -1]; | ||
| 2627 | } | ||
| 2628 | my($from, $to); | ||
| 2629 | $from = collapse(@from); | ||
| 2630 | $to = collapse(@to); | ||
| 2631 | $from .= "-" if $delhyphen; | ||
| 2632 | return ($from, $to); | ||
| 2633 | } | ||
| 2634 | |||
| 2635 | sub tr_chr { | ||
| 2636 | my $x = shift; | ||
| 2637 | if ($x == ord "-") { | ||
| 2638 | return "\\-"; | ||
| 2639 | } elsif ($x == ord "\\") { | ||
| 2640 | return "\\\\"; | ||
| 2641 | } else { | ||
| 2642 | return chr $x; | ||
| 2643 | } | ||
| 2644 | } | ||
| 2645 | |||
| 2646 | # XXX This doesn't yet handle all cases correctly either | ||
| 2647 | |||
| 2648 | sub tr_decode_utf8 { | ||
| 2649 | my($swash_hv, $flags) = @_; | ||
| 2650 | my %swash = $swash_hv->ARRAY; | ||
| 2651 | my $final = undef; | ||
| 2652 | $final = $swash{'FINAL'}->IV if exists $swash{'FINAL'}; | ||
| 2653 | my $none = $swash{"NONE"}->IV; | ||
| 2654 | my $extra = $none + 1; | ||
| 2655 | my(@from, @delfrom, @to); | ||
| 2656 | my $line; | ||
| 2657 | foreach $line (split /\n/, $swash{'LIST'}->PV) { | ||
| 2658 | my($min, $max, $result) = split(/\t/, $line); | ||
| 2659 | $min = hex $min; | ||
| 2660 | if (length $max) { | ||
| 2661 | $max = hex $max; | ||
| 2662 | } else { | ||
| 2663 | $max = $min; | ||
| 2664 | } | ||
| 2665 | $result = hex $result; | ||
| 2666 | if ($result == $extra) { | ||
| 2667 | push @delfrom, [$min, $max]; | ||
| 2668 | } else { | ||
| 2669 | push @from, [$min, $max]; | ||
| 2670 | push @to, [$result, $result + $max - $min]; | ||
| 2671 | } | ||
| 2672 | } | ||
| 2673 | for my $i (0 .. $#from) { | ||
| 2674 | if ($from[$i][0] == ord '-') { | ||
| 2675 | unshift @from, splice(@from, $i, 1); | ||
| 2676 | unshift @to, splice(@to, $i, 1); | ||
| 2677 | last; | ||
| 2678 | } elsif ($from[$i][1] == ord '-') { | ||
| 2679 | $from[$i][1]--; | ||
| 2680 | $to[$i][1]--; | ||
| 2681 | unshift @from, ord '-'; | ||
| 2682 | unshift @to, ord '-'; | ||
| 2683 | last; | ||
| 2684 | } | ||
| 2685 | } | ||
| 2686 | for my $i (0 .. $#delfrom) { | ||
| 2687 | if ($delfrom[$i][0] == ord '-') { | ||
| 2688 | push @delfrom, splice(@delfrom, $i, 1); | ||
| 2689 | last; | ||
| 2690 | } elsif ($delfrom[$i][1] == ord '-') { | ||
| 2691 | $delfrom[$i][1]--; | ||
| 2692 | push @delfrom, ord '-'; | ||
| 2693 | last; | ||
| 2694 | } | ||
| 2695 | } | ||
| 2696 | if (defined $final and $to[$#to][1] != $final) { | ||
| 2697 | push @to, [$final, $final]; | ||
| 2698 | } | ||
| 2699 | push @from, @delfrom; | ||
| 2700 | if ($flags & OPpTRANS_COMPLEMENT) { | ||
| 2701 | my @newfrom; | ||
| 2702 | my $next = 0; | ||
| 2703 | for my $i (0 .. $#from) { | ||
| 2704 | push @newfrom, [$next, $from[$i][0] - 1]; | ||
| 2705 | $next = $from[$i][1] + 1; | ||
| 2706 | } | ||
| 2707 | @from = (); | ||
| 2708 | for my $range (@newfrom) { | ||
| 2709 | if ($range->[0] <= $range->[1]) { | ||
| 2710 | push @from, $range; | ||
| 2711 | } | ||
| 2712 | } | ||
| 2713 | } | ||
| 2714 | my($from, $to, $diff); | ||
| 2715 | for my $chunk (@from) { | ||
| 2716 | $diff = $chunk->[1] - $chunk->[0]; | ||
| 2717 | if ($diff > 1) { | ||
| 2718 | $from .= tr_chr($chunk->[0]) . "-" . tr_chr($chunk->[1]); | ||
| 2719 | } elsif ($diff == 1) { | ||
| 2720 | $from .= tr_chr($chunk->[0]) . tr_chr($chunk->[1]); | ||
| 2721 | } else { | ||
| 2722 | $from .= tr_chr($chunk->[0]); | ||
| 2723 | } | ||
| 2724 | } | ||
| 2725 | for my $chunk (@to) { | ||
| 2726 | $diff = $chunk->[1] - $chunk->[0]; | ||
| 2727 | if ($diff > 1) { | ||
| 2728 | $to .= tr_chr($chunk->[0]) . "-" . tr_chr($chunk->[1]); | ||
| 2729 | } elsif ($diff == 1) { | ||
| 2730 | $to .= tr_chr($chunk->[0]) . tr_chr($chunk->[1]); | ||
| 2731 | } else { | ||
| 2732 | $to .= tr_chr($chunk->[0]); | ||
| 2733 | } | ||
| 2734 | } | ||
| 2735 | #$final = sprintf("%04x", $final) if defined $final; | ||
| 2736 | #$none = sprintf("%04x", $none) if defined $none; | ||
| 2737 | #$extra = sprintf("%04x", $extra) if defined $extra; | ||
| 2738 | #print STDERR "final: $final\n none: $none\nextra: $extra\n"; | ||
| 2739 | #print STDERR $swash{'LIST'}->PV; | ||
| 2740 | return (B::Deparse::escape_str($from), B::Deparse::escape_str($to)); | ||
| 2741 | } | ||
| 2742 | |||
| 2743 | sub pp_trans { | ||
| 2744 | my $self = shift; | ||
| 2745 | my($op, $cx) = @_; | ||
| 2746 | my($from, $to); | ||
| 2747 | my $class = class($op); | ||
| 2748 | my $priv_flags = $op->private; | ||
| 2749 | if ($class eq "PVOP") { | ||
| 2750 | ($from, $to) = tr_decode_byte($op->pv, $priv_flags); | ||
| 2751 | } elsif ($class eq "PADOP") { | ||
| 2752 | ($from, $to) | ||
| 2753 | = tr_decode_utf8($self->padval($op->padix)->RV, $priv_flags); | ||
| 2754 | } else { # class($op) eq "SVOP" | ||
| 2755 | ($from, $to) = tr_decode_utf8($op->sv->RV, $priv_flags); | ||
| 2756 | } | ||
| 2757 | my $flags = ""; | ||
| 2758 | $flags .= "c" if $priv_flags & OPpTRANS_COMPLEMENT; | ||
| 2759 | $flags .= "d" if $priv_flags & OPpTRANS_DELETE; | ||
| 2760 | $to = "" if $from eq $to and $flags eq ""; | ||
| 2761 | $flags .= "s" if $priv_flags & OPpTRANS_SQUASH; | ||
| 2762 | return info_from_list($op, $self, ['tr', double_delim($from, $to), $flags], | ||
| 2763 | '', 'pp_trans', {}); | ||
| 2764 | } | ||
| 2765 | |||
| 2766 | sub pp_transr { | ||
| 2767 | my $self = $_[0]; | ||
| 2768 | my $op = $_[1]; | ||
| 2769 | my $info = pp_trans(@_); | ||
| 2770 | return info_from_text($op, $self, $info->{text} . 'r', 'pp_transr', | ||
| 2771 | {body => [$info]}); | ||
| 2772 | } | ||
| 2773 | |||
| 2774 | sub re_dq_disambiguate { | ||
| 2775 | my ($first, $last) = @_; | ||
| 2776 | # Disambiguate "${foo}bar", "${foo}{bar}", "${foo}[1]" | ||
| 2777 | ($last =~ /^[A-Z\\\^\[\]_?]/ && | ||
| 2778 | $first =~ s/([\$@])\^$/${1}{^}/) # "${^}W" etc | ||
| 2779 | || ($last =~ /^[{\[\w_]/ && | ||
| 2780 | $first =~ s/([\$@])([A-Za-z_]\w*)$/${1}{$2}/); | ||
| 2781 | return $first . $last; | ||
| 2782 | } | ||
| 2783 | |||
| 2784 | # Like dq(), but different | ||
| 2785 | sub re_dq { | ||
| 2786 | my $self = shift; | ||
| 2787 | my ($op) = @_; | ||
| 2788 | |||
| 2789 | my $type = $op->name; | ||
| 2790 | if ($type eq "const") { | ||
| 2791 | return '$[' if $op->private & OPpCONST_ARYBASE; | ||
| 2792 | my $unbacked = B::Deparse::re_unback($self->const_sv($op)->as_string); | ||
| 2793 | return B::Deparse::re_uninterp(escape_re($unbacked)); | ||
| 2794 | } elsif ($type eq "concat") { | ||
| 2795 | my $first = $self->re_dq($op->first); | ||
| 2796 | my $last = $self->re_dq($op->last); | ||
| 2797 | return re_dq_disambiguate($first, $last); | ||
| 2798 | } elsif ($type eq "uc") { | ||
| 2799 | return '\U' . $self->re_dq($op->first->sibling) . '\E'; | ||
| 2800 | } elsif ($type eq "lc") { | ||
| 2801 | return '\L' . $self->re_dq($op->first->sibling) . '\E'; | ||
| 2802 | } elsif ($type eq "ucfirst") { | ||
| 2803 | return '\u' . $self->re_dq($op->first->sibling); | ||
| 2804 | } elsif ($type eq "lcfirst") { | ||
| 2805 | return '\l' . $self->re_dq($op->first->sibling); | ||
| 2806 | } elsif ($type eq "quotemeta") { | ||
| 2807 | return '\Q' . $self->re_dq($op->first->sibling) . '\E'; | ||
| 2808 | } elsif ($type eq "fc") { | ||
| 2809 | return '\F' . $self->re_dq($op->first->sibling) . '\E'; | ||
| 2810 | } elsif ($type eq "join") { | ||
| 2811 | return $self->deparse($op->last, 26); # was join($", @ary) | ||
| 2812 | } else { | ||
| 2813 | my $ret = $self->deparse($op, 26); | ||
| 2814 | $ret =~ s/^\$([(|)])\z/\${$1}/ # $( $| $) need braces | ||
| 2815 | or $ret =~ s/^\@([-+])\z/\@{$1}/; # @- @+ need braces | ||
| 2816 | return $ret; | ||
| 2817 | } | ||
| 2818 | } | ||
| 2819 | |||
| 2820 | sub pure_string { | ||
| 2821 | my ($self, $op) = @_; | ||
| 2822 | return 0 if null $op; | ||
| 2823 | my $type = $op->name; | ||
| 2824 | |||
| 2825 | if ($type eq 'const' || $type eq 'av2arylen') { | ||
| 2826 | return 1; | ||
| 2827 | } | ||
| 2828 | elsif ($type =~ /^(?:[ul]c(first)?|fc)$/ || $type eq 'quotemeta') { | ||
| 2829 | return $self->pure_string($op->first->sibling); | ||
| 2830 | } | ||
| 2831 | elsif ($type eq 'join') { | ||
| 2832 | my $join_op = $op->first->sibling; # Skip pushmark | ||
| 2833 | return 0 unless $join_op->name eq 'null' && $join_op->targ == OP_RV2SV; | ||
| 2834 | |||
| 2835 | my $gvop = $join_op->first; | ||
| 2836 | return 0 unless $gvop->name eq 'gvsv'; | ||
| 2837 | return 0 unless '"' eq $self->gv_name($self->gv_or_padgv($gvop)); | ||
| 2838 | |||
| 2839 | return 0 unless ${$join_op->sibling} eq ${$op->last}; | ||
| 2840 | return 0 unless $op->last->name =~ /^(?:[ah]slice|(?:rv2|pad)av)$/; | ||
| 2841 | } | ||
| 2842 | elsif ($type eq 'concat') { | ||
| 2843 | return $self->pure_string($op->first) | ||
| 2844 | && $self->pure_string($op->last); | ||
| 2845 | } | ||
| 2846 | elsif (is_scalar($op) || $type =~ /^[ah]elem$/) { | ||
| 2847 | return 1; | ||
| 2848 | } | ||
| 2849 | elsif ($type eq "null" and $op->can('first') and not null $op->first and | ||
| 2850 | ($op->first->name eq "null" and $op->first->can('first') | ||
| 2851 | and not null $op->first->first and | ||
| 2852 | $op->first->first->name eq "aelemfast" | ||
| 2853 | or | ||
| 2854 | $op->first->name =~ /^aelemfast(?:_lex)?\z/ | ||
| 2855 | )) { | ||
| 2856 | return 1; | ||
| 2857 | } | ||
| 2858 | else { | ||
| 2859 | return 0; | ||
| 2860 | } | ||
| 2861 | |||
| 2862 | return 1; | ||
| 2863 | } | ||
| 2864 | |||
| 2865 | sub regcomp | ||
| 2866 | { | ||
| 2867 | my($self, $op, $cx, $extended) = @_; | ||
| 2868 | my @other_ops = (); | ||
| 2869 | my $kid = $op->first; | ||
| 2870 | if ($kid->name eq "regcmaybe") { | ||
| 2871 | push @other_ops, $kid; | ||
| 2872 | $kid = $kid->first; | ||
| 2873 | } | ||
| 2874 | if ($kid->name eq "regcreset") { | ||
| 2875 | push @other_ops, $kid; | ||
| 2876 | $kid = $kid->first; | ||
| 2877 | } | ||
| 2878 | if ($kid->name eq "null" and !null($kid->first) | ||
| 2879 | and $kid->first->name eq 'pushmark') { | ||
| 2880 | my $str = ''; | ||
| 2881 | push(@other_ops, $kid); | ||
| 2882 | $kid = $kid->first->sibling; | ||
| 2883 | my @body = (); | ||
| 2884 | while (!null($kid)) { | ||
| 2885 | my $first = $str; | ||
| 2886 | my $last = $self->re_dq($kid, $extended); | ||
| 2887 | push @body, $last; | ||
| 2888 | push(@other_ops, $kid); | ||
| 2889 | $str = re_dq_disambiguate($first, $last->{text}); | ||
| 2890 | $kid = $kid->sibling; | ||
| 2891 | } | ||
| 2892 | return (info_from_text($op, $self, $str, 'regcomp', | ||
| 2893 | {other_ops => \@other_ops, | ||
| 2894 | body => \@body}), 1); | ||
| 2895 | } | ||
| 2896 | |||
| 2897 | if ($self->pure_string($kid)) { | ||
| 2898 | my $info = $self->re_dq($kid, $extended); | ||
| 2899 | my @kid_ops = $info->{other_ops} ? @{$info->{other_ops}} : (); | ||
| 2900 | push @other_ops, @kid_ops; | ||
| 2901 | $info->{other_ops} = \@other_ops; | ||
| 2902 | return ($info, 1); | ||
| 2903 | } | ||
| 2904 | return ($self->deparse($kid, $cx, $op), 0, $op); | ||
| 2905 | } | ||
| 2906 | |||
| 2907 | sub pp_regcomp | ||
| 2908 | { | ||
| 2909 | my ($self, $op, $cx) = @_; | ||
| 2910 | return (($self->regcomp($op, $cx, 0))[0]); | ||
| 2911 | } | ||
| 2912 | |||
| 2913 | sub re_flags | ||
| 2914 | { | ||
| 2915 | my ($self, $op) = @_; | ||
| 2916 | my $flags = ''; | ||
| 2917 | my $pmflags = $op->pmflags; | ||
| 2918 | if (!$pmflags) { | ||
| 2919 | my $re = $op->pmregexp; | ||
| 2920 | if ($$re) { | ||
| 2921 | $pmflags = $re->compflags; | ||
| 2922 | } | ||
| 2923 | } | ||
| 2924 | $flags .= "g" if $pmflags & PMf_GLOBAL; | ||
| 2925 | $flags .= "i" if $pmflags & PMf_FOLD; | ||
| 2926 | $flags .= "m" if $pmflags & PMf_MULTILINE; | ||
| 2927 | $flags .= "o" if $pmflags & PMf_KEEP; | ||
| 2928 | $flags .= "s" if $pmflags & PMf_SINGLELINE; | ||
| 2929 | $flags .= "x" if $pmflags & PMf_EXTENDED; | ||
| 2930 | $flags .= "x" if $pmflags & PMf_EXTENDED_MORE; | ||
| 2931 | $flags .= "p" if $pmflags & PMf_KEEPCOPY; | ||
| 2932 | $flags .= "n" if $pmflags & PMf_NOCAPTURE; | ||
| 2933 | if (my $charset = $pmflags & PMf_CHARSET) { | ||
| 2934 | # Hardcoding this is fragile, but B does not yet export the | ||
| 2935 | # constants we need. | ||
| 2936 | $flags .= qw(d l u a aa)[$charset >> 7] | ||
| 2937 | } | ||
| 2938 | # The /d flag is indicated by 0; only show it if necessary. | ||
| 2939 | elsif ($self->{hinthash} and | ||
| 2940 | $self->{hinthash}{reflags_charset} | ||
| 2941 | || $self->{hinthash}{feature_unicode} | ||
| 2942 | or $self->{hints} & $feature::hint_mask | ||
| 2943 | && ($self->{hints} & $feature::hint_mask) | ||
| 2944 | != $feature::hint_mask | ||
| 2945 | && $self->{hints} & $feature::hint_uni8bit | ||
| 2946 | ) { | ||
| 2947 | $flags .= 'd'; | ||
| 2948 | } | ||
| 2949 | $flags; | ||
| 2950 | } | ||
| 2951 | |||
| 2952 | # osmic acid -- see osmium tetroxide | ||
| 2953 | |||
| 2954 | my %matchwords; | ||
| 2955 | map($matchwords{join "", sort split //, $_} = $_, 'cig', 'cog', 'cos', 'cogs', | ||
| 2956 | 'cox', 'go', 'is', 'ism', 'iso', 'mig', 'mix', 'osmic', 'ox', 'sic', | ||
| 2957 | 'sig', 'six', 'smog', 'so', 'soc', 'sog', 'xi'); | ||
| 2958 | |||
| 2959 | sub matchop | ||
| 2960 | { | ||
| 2961 | my($self, $op, $cx, $name, $delim) = @_; | ||
| 2962 | my $kid = $op->first; | ||
| 2963 | my $info = {}; | ||
| 2964 | my @body = (); | ||
| 2965 | my ($binop, $var, $re_str) = ("", "", ""); | ||
| 2966 | my $re; | ||
| 2967 | if ($op->flags & OPf_STACKED) { | ||
| 2968 | $binop = 1; | ||
| 2969 | $var = $self->deparse($kid, 20, $op); | ||
| 2970 | push @body, $var; | ||
| 2971 | $kid = $kid->sibling; | ||
| 2972 | } | ||
| 2973 | my $quote = 1; | ||
| 2974 | my $pmflags = $op->pmflags; | ||
| 2975 | my $extended = ($pmflags & PMf_EXTENDED); | ||
| 2976 | my $rhs_bound_to_defsv; | ||
| 2977 | if (null $kid) { | ||
| 2978 | my $unbacked = B::Deparse::re_unback($op->precomp); | ||
| 2979 | if ($extended) { | ||
| 2980 | $re_str = B::Deparse::re_uninterp_extended(B::Deparse::escape_extended_re($unbacked)); | ||
| 2981 | } else { | ||
| 2982 | $re_str = B::Deparse::re_uninterp(B::Deparse::escape_str(B::Deparse::re_unback($op->precomp))); | ||
| 2983 | } | ||
| 2984 | } elsif ($kid->name ne 'regcomp') { | ||
| 2985 | carp("found ".$kid->name." where regcomp expected"); | ||
| 2986 | } else { | ||
| 2987 | ($re, $quote) = $self->regcomp($kid, 21, $extended); | ||
| 2988 | push @body, $re; | ||
| 2989 | $re_str = $re->{text}; | ||
| 2990 | my $matchop = $kid->first; | ||
| 2991 | if ($matchop->name eq 'regcrest') { | ||
| 2992 | $matchop = $matchop->first; | ||
| 2993 | } | ||
| 2994 | if ($matchop->name =~ /^(?:match|transr?|subst)\z/ | ||
| 2995 | && $matchop->flags & OPf_SPECIAL) { | ||
| 2996 | $rhs_bound_to_defsv = 1; | ||
| 2997 | } | ||
| 2998 | } | ||
| 2999 | my $flags = ''; | ||
| 3000 | $flags .= "c" if $pmflags & PMf_CONTINUE; | ||
| 3001 | $flags .= $self->re_flags($op); | ||
| 3002 | $flags = join '', sort split //, $flags; | ||
| 3003 | $flags = $matchwords{$flags} if $matchwords{$flags}; | ||
| 3004 | |||
| 3005 | if ($pmflags & PMf_ONCE) { # only one kind of delimiter works here | ||
| 3006 | $re_str =~ s/\?/\\?/g; | ||
| 3007 | $re_str = "?$re_str?"; | ||
| 3008 | } elsif ($quote) { | ||
| 3009 | my $re = $self->single_delim($kid, $name, $delim, $re_str); | ||
| 3010 | push @body, $re; | ||
| 3011 | $re_str = $re->{text}; | ||
| 3012 | } | ||
| 3013 | my $opts = {body => \@body}; | ||
| 3014 | my @texts; | ||
| 3015 | $re_str .= $flags if $quote; | ||
| 3016 | my $type; | ||
| 3017 | if ($binop) { | ||
| 3018 | if ($rhs_bound_to_defsv) { | ||
| 3019 | @texts = ($var->{text}, ' =~ ', "(", '$_', ' =~ ', $re_str, ')'); | ||
| 3020 | } else { | ||
| 3021 | @texts = ($var->{text}, ' =~ ', $re_str); | ||
| 3022 | } | ||
| 3023 | $opts->{maybe_parens} = [$self, $cx, 20]; | ||
| 3024 | $type = 'matchop_binop'; | ||
| 3025 | } else { | ||
| 3026 | @texts = ($re_str); | ||
| 3027 | $type = 'matchop_unnop'; | ||
| 3028 | } | ||
| 3029 | return info_from_list($op, $self, \@texts, '', $type, $opts); | ||
| 3030 | } | ||
| 3031 | |||
| 3032 | sub pp_match { matchop(@_, "m", "/") } | ||
| 3033 | sub pp_pushre { matchop(@_, "m", "/") } | ||
| 3034 | sub pp_qr { matchop(@_, "qr", "") } | ||
| 3035 | |||
| 3036 | sub pp_runcv { unop(@_, "__SUB__"); } | ||
| 3037 | |||
| 3038 | sub pp_split | ||
| 3039 | { | ||
| 3040 | my($self, $op, $cx) = @_; | ||
| 3041 | my($kid, @exprs, $ary_info, $expr); | ||
| 3042 | my $stacked = $op->flags & OPf_STACKED; | ||
| 3043 | my $ary = ''; | ||
| 3044 | my @body = (); | ||
| 3045 | my @other_ops = (); | ||
| 3046 | $kid = $op->first; | ||
| 3047 | |||
| 3048 | $kid = $op->first; | ||
| 3049 | $kid = $kid->sibling if $kid->name eq 'regcomp'; | ||
| 3050 | for (; !null($kid); $kid = $kid->sibling) { | ||
| 3051 | push @exprs, $self->deparse($kid, 6, $op); | ||
| 3052 | } | ||
| 3053 | |||
| 3054 | unshift @exprs, $self->matchop($op, $cx, "m", "/"); | ||
| 3055 | |||
| 3056 | if ($op->private & OPpSPLIT_ASSIGN) { | ||
| 3057 | # With C<@array = split(/pat/, str);>, | ||
| 3058 | # array is stored in split's pmreplroot; either | ||
| 3059 | # as an integer index into the pad (for a lexical array) | ||
| 3060 | # or as GV for a package array (which will be a pad index | ||
| 3061 | # on threaded builds) | ||
| 3062 | # With my/our @array = split(/pat/, str), the array is instead | ||
| 3063 | # accessed via an extra padav/rv2av op at the end of the | ||
| 3064 | # split's kid ops. | ||
| 3065 | |||
| 3066 | if ($stacked) { | ||
| 3067 | $ary = pop @exprs; | ||
| 3068 | } | ||
| 3069 | else { | ||
| 3070 | if ($op->private & OPpSPLIT_LEX) { | ||
| 3071 | $ary = $self->padname($op->pmreplroot); | ||
| 3072 | } | ||
| 3073 | else { | ||
| 3074 | # union with op_pmtargetoff, op_pmtargetgv | ||
| 3075 | my $gv = $op->pmreplroot; | ||
| 3076 | $gv = $self->padval($gv) if !ref($gv); | ||
| 3077 | $ary = $self->maybe_local(@_, | ||
| 3078 | $self->stash_variable('@', | ||
| 3079 | $self->gv_name($gv), | ||
| 3080 | $cx)) | ||
| 3081 | } | ||
| 3082 | if ($op->private & OPpLVAL_INTRO) { | ||
| 3083 | $ary = $op->private & OPpSPLIT_LEX ? "my $ary" : "local $ary"; | ||
| 3084 | } | ||
| 3085 | } | ||
| 3086 | } | ||
| 3087 | |||
| 3088 | push @body, @exprs; | ||
| 3089 | my $opts = {body => \@exprs}; | ||
| 3090 | |||
| 3091 | # handle special case of split(), and split(' ') that compiles to /\s+/ | ||
| 3092 | if (($op->reflags // 0) & RXf_SKIPWHITE()) { | ||
| 3093 | my $expr0 = $exprs[0]; | ||
| 3094 | my $expr0b0 = $expr0->{body}[0]; | ||
| 3095 | my $bsep = $expr0b0->{sep}; | ||
| 3096 | my $sep = $expr0->{sep}; | ||
| 3097 | $expr0b0->{texts}[1] = ' '; | ||
| 3098 | substr($expr0b0->{text}, 1, 0) = ' '; | ||
| 3099 | substr($expr0->{texts}[0], 1, 0) = ' '; | ||
| 3100 | substr($expr0->{text}, 1, 0) = ' '; | ||
| 3101 | } | ||
| 3102 | my @args_texts = map $_->{text}, @exprs; | ||
| 3103 | |||
| 3104 | my $sep = ''; | ||
| 3105 | my $type; | ||
| 3106 | my @expr_texts; | ||
| 3107 | if ($ary) { | ||
| 3108 | @expr_texts = ("$ary", '=', join(', ', @args_texts)); | ||
| 3109 | $sep = ' '; | ||
| 3110 | $type = 'split_array'; | ||
| 3111 | $opts->{maybe_parens} = [$self, $cx, 7]; | ||
| 3112 | } else { | ||
| 3113 | @expr_texts = ('split', '(', join(', ', @args_texts), ')'); | ||
| 3114 | $type = 'split'; | ||
| 3115 | |||
| 3116 | } | ||
| 3117 | return info_from_list($op, $self, \@expr_texts, $sep, $type, $opts); | ||
| 3118 | } | ||
| 3119 | |||
| 3120 | # Kind of silly, but we prefer, subst regexp flags joined together to | ||
| 3121 | # make words. For example: s/a/b/xo => s/a/b/ox | ||
| 3122 | |||
| 3123 | # oxime -- any of various compounds obtained chiefly by the action of | ||
| 3124 | # hydroxylamine on aldehydes and ketones and characterized by the | ||
| 3125 | # bivalent grouping C=NOH [Webster's Tenth] | ||
| 3126 | |||
| 3127 | my %substwords; | ||
| 3128 | map($substwords{join "", sort split //, $_} = $_, 'ego', 'egoism', 'em', | ||
| 3129 | 'es', 'ex', 'exes', 'gee', 'go', 'goes', 'ie', 'ism', 'iso', 'me', | ||
| 3130 | 'meese', 'meso', 'mig', 'mix', 'os', 'ox', 'oxime', 'see', 'seem', | ||
| 3131 | 'seg', 'sex', 'sig', 'six', 'smog', 'sog', 'some', 'xi', 'rogue', | ||
| 3132 | 'sir', 'rise', 'smore', 'more', 'seer', 'rome', 'gore', 'grim', 'grime', | ||
| 3133 | 'or', 'rose', 'rosie'); | ||
| 3134 | |||
| 3135 | sub pp_subst | ||
| 3136 | { | ||
| 3137 | my($self, $op, $cx) = @_; | ||
| 3138 | my $kid = $op->first; | ||
| 3139 | my($binop, $var, $re, @other_ops) = ("", "", "", ()); | ||
| 3140 | my @body = (); | ||
| 3141 | my ($repl, $repl_info); | ||
| 3142 | if ($op->flags & OPf_STACKED) { | ||
| 3143 | $binop = 1; | ||
| 3144 | $var = $self->deparse($kid, 20, $op); | ||
| 3145 | $kid = $kid->sibling; | ||
| 3146 | } | ||
| 3147 | elsif (my $targ = $op->targ) { | ||
| 3148 | $binop = 1; | ||
| 3149 | $var = $self->padname($targ); | ||
| 3150 | } | ||
| 3151 | my $flags = ""; | ||
| 3152 | my $pmflags = $op->pmflags; | ||
| 3153 | if (null($op->pmreplroot)) { | ||
| 3154 | $repl = $kid; | ||
| 3155 | $kid = $kid->sibling; | ||
| 3156 | } else { | ||
| 3157 | push @other_ops, $op->pmreplroot; | ||
| 3158 | $repl = $op->pmreplroot->first; # skip substcont | ||
| 3159 | } | ||
| 3160 | while ($repl->name eq "entereval") { | ||
| 3161 | push @other_ops, $repl; | ||
| 3162 | $repl = $repl->first; | ||
| 3163 | $flags .= "e"; | ||
| 3164 | } | ||
| 3165 | { | ||
| 3166 | local $self->{in_subst_repl} = 1; | ||
| 3167 | if ($pmflags & PMf_EVAL) { | ||
| 3168 | $repl_info = $self->deparse($repl->first, 0, $repl); | ||
| 3169 | } else { | ||
| 3170 | $repl_info = $self->dq($repl); | ||
| 3171 | } | ||
| 3172 | } | ||
| 3173 | if (not null my $code_list = $op->code_list) { | ||
| 3174 | $re = $self->code_list($code_list); | ||
| 3175 | } elsif (null $kid) { | ||
| 3176 | $re = B::Deparse::re_uninterp(escape_re(B::Deparse::re_unback($op->precomp))); | ||
| 3177 | } else { | ||
| 3178 | my ($re_info, $junk) = $self->regcomp($kid, 1); | ||
| 3179 | push @body, $re_info; | ||
| 3180 | $re = $re_info->{text}; | ||
| 3181 | } | ||
| 3182 | $flags .= "r" if $pmflags & PMf_NONDESTRUCT; | ||
| 3183 | $flags .= "e" if $pmflags & PMf_EVAL; | ||
| 3184 | $flags .= $self->re_flags($op); | ||
| 3185 | $flags = join '', sort split //, $flags; | ||
| 3186 | $flags = $substwords{$flags} if $substwords{$flags}; | ||
| 3187 | my $core_s = $self->keyword("s"); # maybe CORE::s | ||
| 3188 | my $info; | ||
| 3189 | push @body, $repl_info; | ||
| 3190 | my $repl_text = $repl_info->{text}; | ||
| 3191 | my $opts = {body => \@body}; | ||
| 3192 | $opts->{other_ops} = \@other_ops if @other_ops; | ||
| 3193 | if ($binop) { | ||
| 3194 | my @texts = ($var->{text}, " ", "=~", " ", "s", | ||
| 3195 | double_delim($re, $repl_text), $flags); | ||
| 3196 | $opts->{maybe_parens} = [$self, $cx, 20]; | ||
| 3197 | return info_from_list($op, $self, \@texts, '', 'subst_binop', $opts); | ||
| 3198 | } else { | ||
| 3199 | return info_from_list($op, $self, ["$core_s", double_delim($re, $repl_text), $flags], '', 'subst', | ||
| 3200 | $opts); | ||
| 3201 | } | ||
| 3202 | Carp::confess("unhandled condition in pp_subst"); | ||
| 3203 | } | ||
| 3204 | |||
| 3205 | sub pp_introcv | ||
| 3206 | { | ||
| 3207 | my($self, $op, $cx) = @_; | ||
| 3208 | # For now, deparsing doesn't worry about the distinction between introcv | ||
| 3209 | # and clonecv, so pretend this op doesn't exist: | ||
| 3210 | return info_from_text($op, $self, '', 'introcv', {}); | ||
| 3211 | } | ||
| 3212 | |||
| 3213 | sub pp_clonecv { | ||
| 3214 | my $self = shift; | ||
| 3215 | my($op, $cx) = @_; | ||
| 3216 | my $sv = $self->padname_sv($op->targ); | ||
| 3217 | my $name = substr $sv->PVX, 1; # skip &/$/@/%, like $self->padany | ||
| 3218 | return info_from_list($op, $self, ['my', 'sub', $name], ' ', 'clonev', {}); | ||
| 3219 | } | ||
| 3220 | |||
| 3221 | sub pp_padcv { | ||
| 3222 | my($self, $op, $cx) = @_; | ||
| 3223 | return info_from_text($op, $self, $self->padany($op), 'padcv', {}); | ||
| 3224 | } | ||
| 3225 | |||
| 3226 | unless (caller) { | ||
| 3227 | eval "use Data::Printer;"; | ||
| 3228 | |||
| 3229 | eval { | ||
| 3230 | sub fib($) { | ||
| 3231 | my $x = shift; | ||
| 3232 | return 1 if $x <= 1; | ||
| 3233 | return(fib($x-1) + fib($x-2)) | ||
| 3234 | } | ||
| 3235 | sub baz { | ||
| 3236 | no strict; | ||
| 3237 | CORE::wait; | ||
| 3238 | } | ||
| 3239 | }; | ||
| 3240 | |||
| 3241 | # use B::Deparse; | ||
| 3242 | # my $deparse_old = B::Deparse->new("-l", "-sC"); | ||
| 3243 | # print $deparse_old->coderef2text(\&baz); | ||
| 3244 | # exit 1; | ||
| 3245 | my $deparse = __PACKAGE__->new("-l", "-c", "-sC"); | ||
| 3246 | my $info = $deparse->coderef2info(\&baz); | ||
| 3247 | import Data::Printer colored => 0; | ||
| 3248 | Data::Printer::p($info); | ||
| 3249 | print "\n", '=' x 30, "\n"; | ||
| 3250 | # print $deparse->indent($deparse->deparse_subname('fib')->{text}); | ||
| 3251 | # print "\n", '=' x 30, "\n"; | ||
| 3252 | # print "\n", '-' x 30, "\n"; | ||
| 3253 | while (my($key, $value) = each %{$deparse->{optree}}) { | ||
| 3254 | my $parent_op_name = 'undef'; | ||
| 3255 | if ($value->{parent}) { | ||
| 3256 | my $parent = $deparse->{optree}{$value->{parent}}; | ||
| 3257 | $parent_op_name = $parent->{op}->name if $parent->{op}; | ||
| 3258 | } | ||
| 3259 | printf("0x%x %s/%s of %s |\n%s", | ||
| 3260 | $key, $value->{op}->name, $value->{type}, | ||
| 3261 | $parent_op_name, $deparse->indent($value->{text})); | ||
| 3262 | printf " ## line %s\n", $value->{cop} ? $value->{cop}->line : 'undef'; | ||
| 3263 | print '-' x 30, "\n"; | ||
| 3264 | } | ||
| 3265 | } | ||
| 3266 | |||
| 3267 | # Not in Perl 5.20 and presumeably < 5.20. No harm in adding to 5.20? | ||
| 3268 | *pp_ncomplement = *pp_complement; | ||
| 3269 | sub pp_scomplement { maybe_targmy(@_, \&pfixop, "~.", 21) } | ||
| 3270 | |||
| 3271 | unless (caller) { | ||
| 3272 | eval "use Data::Printer;"; | ||
| 3273 | |||
| 3274 | eval { | ||
| 3275 | our($Fileparse_fstype); | ||
| 3276 | sub fib($) { | ||
| 3277 | my $x = shift; | ||
| 3278 | return 1 if $x <= 1; | ||
| 3279 | return(fib($x-1) + fib($x-2)) | ||
| 3280 | } | ||
| 3281 | sub fileparse { | ||
| 3282 | no strict; | ||
| 3283 | # my($fullname,@suffices) = @_; | ||
| 3284 | |||
| 3285 | my $tail = ''; | ||
| 3286 | $tail = $1 . $tail; | ||
| 3287 | |||
| 3288 | # Ensure taint is propagated from the path to its pieces. | ||
| 3289 | $tail .= $taint; | ||
| 3290 | wantarray ? ($basename .= $taint, $dirpath .= $taint, $tail) | ||
| 3291 | : ($basename .= $taint); | ||
| 3292 | } | ||
| 3293 | sub baz { | ||
| 3294 | no strict; | ||
| 3295 | if ($basename =~ s/$pat//s) { | ||
| 3296 | } | ||
| 3297 | } | ||
| 3298 | }; | ||
| 3299 | |||
| 3300 | my $deparse = __PACKAGE__->new("-l", "-c"); | ||
| 3301 | my $info = $deparse->coderef2info(\&fileparse); | ||
| 3302 | # my $info = $deparse->coderef2info(\&baz); | ||
| 3303 | import Data::Printer colored => 0; | ||
| 3304 | Data::Printer::p($info); | ||
| 3305 | print "\n", '=' x 30, "\n"; | ||
| 3306 | # print $deparse->indent($deparse->deparse_subname('fib')->{text}); | ||
| 3307 | # print "\n", '=' x 30, "\n"; | ||
| 3308 | # print "\n", '-' x 30, "\n"; | ||
| 3309 | while (my($key, $value) = each %{$deparse->{optree}}) { | ||
| 3310 | my $parent_op_name = 'undef'; | ||
| 3311 | if ($value->{parent}) { | ||
| 3312 | my $parent = $deparse->{optree}{$value->{parent}}; | ||
| 3313 | $parent_op_name = $parent->{op}->name if $parent->{op}; | ||
| 3314 | } | ||
| 3315 | if (eval{$value->{op}->name}) { | ||
| 3316 | printf("0x%x %s/%s of %s |\n%s", | ||
| 3317 | $key, $value->{op}->name, $value->{type}, | ||
| 3318 | $parent_op_name, $deparse->indent($value->{text})); | ||
| 3319 | } else { | ||
| 3320 | printf("0x%x %s of %s |\n%s", | ||
| 3321 | $key, $value->{type}, | ||
| 3322 | $parent_op_name, $deparse->indent($value->{text})); | ||
| 3323 | } | ||
| 3324 | printf " ## line %s\n", $value->{cop} ? $value->{cop}->line : 'undef'; | ||
| 3325 | print '-' x 30, "\n"; | ||
| 3326 | } | ||
| 3327 | # use B::Deparse; | ||
| 3328 | # my $deparse_old = B::Deparse->new("-l", "-sC"); | ||
| 3329 | # print $deparse_old->coderef2text(\&baz); | ||
| 3330 | } | ||
| 3331 | |||
| 3332 | 1; | ||