| 1 | # Common routines used by PP Functions | ||
| 2 | # Copyright (c) 2015-2018 Rocky Bernstein | ||
| 3 | # Copyright (c) 1998-2000, 2002, 2003, 2004, 2005, 2006 Stephen McCamant. | ||
| 4 | |||
| 5 | # All rights reserved. | ||
| 6 | # This module is free software; you can redistribute and/or modify | ||
| 7 | # it under the same terms as Perl itself. | ||
| 8 | |||
| 9 | # This is based on the module B::Deparse by Stephen McCamant. | ||
| 10 | # It has been extended save tree structure, and is addressible | ||
| 11 | # by opcode address. | ||
| 12 | |||
| 13 | # B::Parse in turn is based on the module of the same name by Malcolm Beattie, | ||
| 14 | # but essentially none of his code remains. | ||
| 15 | use strict; use warnings; | ||
| 16 | |||
| 17 | package B::DeparseTree::PPfns; | ||
| 18 | use Carp; | ||
| 19 | use B qw( | ||
| 20 | OPf_STACKED | ||
| 21 | OPf_SPECIAL | ||
| 22 | OPpCONST_BARE | ||
| 23 | OPpLVAL_INTRO | ||
| 24 | OPpREPEAT_DOLIST | ||
| 25 | OPpSORT_INTEGER | ||
| 26 | OPpSORT_NUMERIC | ||
| 27 | OPpSORT_REVERSE | ||
| 28 | opnumber | ||
| 29 | ); | ||
| 30 | |||
| 31 | use B::Deparse; | ||
| 32 | |||
| 33 | # Copy unchanged functions from B::Deparse | ||
| 34 | *balanced_delim = *B::Deparse::balanced_delim; | ||
| 35 | *double_delim = *B::Deparse::double_delim; | ||
| 36 | *escape_extended_re = *B::Deparse::escape_extended_re; | ||
| 37 | *escape_re = *B::Deparse::escape_re; | ||
| 38 | *lex_in_scope = *B::Deparse::lex_in_scope; | ||
| 39 | *rv2gv_or_string = *B::Deparse::rv2gv_or_string; | ||
| 40 | |||
| 41 | use B::DeparseTree::SyntaxTree; | ||
| 42 | |||
| 43 | # Various operator flag bits | ||
| 44 | use constant POSTFIX => 1; # operator can be used as postfix operator | ||
| 45 | use constant SWAP_CHILDREN => 1; # children of op should be reversed | ||
| 46 | use constant ASSIGN => 2; # has OP= variant | ||
| 47 | use constant LIST_CONTEXT => 4; # Assignment is in list context | ||
| 48 | |||
| 49 | |||
| 50 | |||
| 51 | our($VERSION, @EXPORT, @ISA); | ||
| 52 | $VERSION = '3.2.0'; | ||
| 53 | @ISA = qw(Exporter); | ||
| 54 | @EXPORT = qw( | ||
| 55 | %strict_bits | ||
| 56 | ASSIGN | ||
| 57 | LIST_CONTEXT | ||
| 58 | POSTFIX | ||
| 59 | SWAP_CHILDREN | ||
| 60 | ambient_pragmas | ||
| 61 | anon_hash_or_list | ||
| 62 | baseop | ||
| 63 | binop | ||
| 64 | code_list | ||
| 65 | concat | ||
| 66 | cops | ||
| 67 | dedup_parens_func | ||
| 68 | deparse_binop_left | ||
| 69 | deparse_binop_right | ||
| 70 | deparse_format | ||
| 71 | deparse_op_siblings | ||
| 72 | double_delim | ||
| 73 | dq | ||
| 74 | dq_unop | ||
| 75 | dquote | ||
| 76 | e_anoncode | ||
| 77 | elem | ||
| 78 | filetest | ||
| 79 | func_needs_parens | ||
| 80 | givwhen | ||
| 81 | indirop | ||
| 82 | is_list_newer | ||
| 83 | is_list_older | ||
| 84 | listop | ||
| 85 | logassignop | ||
| 86 | logop | ||
| 87 | loop_common | ||
| 88 | loopex | ||
| 89 | map_texts | ||
| 90 | mapop | ||
| 91 | matchop | ||
| 92 | maybe_local | ||
| 93 | maybe_local_str | ||
| 94 | maybe_my | ||
| 95 | maybe_parens | ||
| 96 | maybe_parens_func | ||
| 97 | maybe_parens_unop | ||
| 98 | maybe_qualify | ||
| 99 | maybe_targmy | ||
| 100 | null_newer | ||
| 101 | null_older | ||
| 102 | pfixop | ||
| 103 | range | ||
| 104 | repeat | ||
| 105 | rv2x | ||
| 106 | scopeop | ||
| 107 | single_delim | ||
| 108 | slice | ||
| 109 | subst_newer | ||
| 110 | subst_older | ||
| 111 | unop | ||
| 112 | ); | ||
| 113 | |||
| 114 | # The BEGIN {} is used here because otherwise this code isn't executed | ||
| 115 | # when you run B::Deparse on itself. | ||
| 116 | my %globalnames; | ||
| 117 | BEGIN { map($globalnames{$_}++, "SIG", "STDIN", "STDOUT", "STDERR", "INC", | ||
| 118 | "ENV", "ARGV", "ARGVOUT", "_"); } | ||
| 119 | |||
| 120 | BEGIN { | ||
| 121 | # List version-specific constants here. | ||
| 122 | # Easiest way to keep this code portable between version looks to | ||
| 123 | # be to fake up a dummy constant that will never actually be true. | ||
| 124 | foreach (qw( | ||
| 125 | CVf_LOCKED | ||
| 126 | OPpCONST_ARYBASE | ||
| 127 | OPpCONST_NOVER | ||
| 128 | OPpEVAL_BYTES | ||
| 129 | OPpITER_REVERSED | ||
| 130 | OPpOUR_INTRO | ||
| 131 | OPpPAD_STATE | ||
| 132 | OPpREVERSE_INPLACE | ||
| 133 | OPpSORT_DESCEND | ||
| 134 | OPpSORT_INPLACE | ||
| 135 | OPpTARGET_MY | ||
| 136 | OPpSUBSTR_REPL_FIRST | ||
| 137 | PMf_EVAL PMf_EXTENDED | ||
| 138 | PMf_NONDESTRUCT | ||
| 139 | PMf_SKIPWHITE | ||
| 140 | RXf_PMf_CHARSET | ||
| 141 | RXf_PMf_KEEPCOPY | ||
| 142 | RXf_SKIPWHITE | ||
| 143 | )) { | ||
| 144 | eval { import B $_ }; | ||
| 145 | no strict 'refs'; | ||
| 146 | *{$_} = sub () {0} unless *{$_}{CODE}; | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | my %strict_bits = do { | ||
| 151 | local $^H; | ||
| 152 | map +($_ => strict::bits($_)), qw/refs subs vars/ | ||
| 153 | }; | ||
| 154 | |||
| 155 | BEGIN { for (qw[ pushmark ]) { | ||
| 156 | eval "sub OP_\U$_ () { " . opnumber($_) . "}" | ||
| 157 | }} | ||
| 158 | |||
| 159 | { | ||
| 160 | # Mask out the bits that L<warnings::register> uses | ||
| 161 | my $WARN_MASK; | ||
| 162 | BEGIN { | ||
| 163 | $WARN_MASK = $warnings::Bits{all} | $warnings::DeadBits{all}; | ||
| 164 | } | ||
| 165 | sub WARN_MASK () { | ||
| 166 | return $WARN_MASK; | ||
| 167 | } | ||
| 168 | } | ||
| 169 | |||
| 170 | my(%left, %right); | ||
| 171 | |||
| 172 | sub ambient_pragmas { | ||
| 173 | my $self = shift; | ||
| 174 | my ($arybase, $hint_bits, $warning_bits, $hinthash) = (0, 0); | ||
| 175 | |||
| 176 | while (@_ > 1) { | ||
| 177 | my $name = shift(); | ||
| 178 | my $val = shift(); | ||
| 179 | |||
| 180 | if ($name eq 'strict') { | ||
| 181 | require strict; | ||
| 182 | |||
| 183 | if ($val eq 'none') { | ||
| 184 | $hint_bits &= $strict_bits{$_} for qw/refs subs vars/; | ||
| 185 | next(); | ||
| 186 | } | ||
| 187 | |||
| 188 | my @names; | ||
| 189 | if ($val eq "all") { | ||
| 190 | @names = qw/refs subs vars/; | ||
| 191 | } | ||
| 192 | elsif (ref $val) { | ||
| 193 | @names = @$val; | ||
| 194 | } | ||
| 195 | else { | ||
| 196 | @names = split' ', $val; | ||
| 197 | } | ||
| 198 | $hint_bits |= $strict_bits{$_} for @names; | ||
| 199 | } | ||
| 200 | |||
| 201 | elsif ($name eq '$[') { | ||
| 202 | if (OPpCONST_ARYBASE) { | ||
| 203 | $arybase = $val; | ||
| 204 | } else { | ||
| 205 | croak "\$[ can't be non-zero on this perl" unless $val == 0; | ||
| 206 | } | ||
| 207 | } | ||
| 208 | |||
| 209 | elsif ($name eq 'integer' | ||
| 210 | || $name eq 'bytes' | ||
| 211 | || $name eq 'utf8') { | ||
| 212 | require "$name.pm"; | ||
| 213 | if ($val) { | ||
| 214 | $hint_bits |= ${$::{"${name}::"}{"hint_bits"}}; | ||
| 215 | } | ||
| 216 | else { | ||
| 217 | $hint_bits &= ~${$::{"${name}::"}{"hint_bits"}}; | ||
| 218 | } | ||
| 219 | } | ||
| 220 | |||
| 221 | elsif ($name eq 're') { | ||
| 222 | require re; | ||
| 223 | if ($val eq 'none') { | ||
| 224 | $hint_bits &= ~re::bits(qw/taint eval/); | ||
| 225 | next(); | ||
| 226 | } | ||
| 227 | |||
| 228 | my @names; | ||
| 229 | if ($val eq 'all') { | ||
| 230 | @names = qw/taint eval/; | ||
| 231 | } | ||
| 232 | elsif (ref $val) { | ||
| 233 | @names = @$val; | ||
| 234 | } | ||
| 235 | else { | ||
| 236 | @names = split' ',$val; | ||
| 237 | } | ||
| 238 | $hint_bits |= re::bits(@names); | ||
| 239 | } | ||
| 240 | |||
| 241 | elsif ($name eq 'warnings') { | ||
| 242 | if ($val eq 'none') { | ||
| 243 | $warning_bits = $warnings::NONE; | ||
| 244 | next(); | ||
| 245 | } | ||
| 246 | |||
| 247 | my @names; | ||
| 248 | if (ref $val) { | ||
| 249 | @names = @$val; | ||
| 250 | } | ||
| 251 | else { | ||
| 252 | @names = split/\s+/, $val; | ||
| 253 | } | ||
| 254 | |||
| 255 | $warning_bits = $warnings::NONE if !defined ($warning_bits); | ||
| 256 | $warning_bits |= warnings::bits(@names); | ||
| 257 | } | ||
| 258 | |||
| 259 | elsif ($name eq 'warning_bits') { | ||
| 260 | $warning_bits = $val; | ||
| 261 | } | ||
| 262 | |||
| 263 | elsif ($name eq 'hint_bits') { | ||
| 264 | $hint_bits = $val; | ||
| 265 | } | ||
| 266 | |||
| 267 | elsif ($name eq '%^H') { | ||
| 268 | $hinthash = $val; | ||
| 269 | } | ||
| 270 | |||
| 271 | else { | ||
| 272 | croak "Unknown pragma type: $name"; | ||
| 273 | } | ||
| 274 | } | ||
| 275 | if (@_) { | ||
| 276 | croak "The ambient_pragmas method expects an even number of args"; | ||
| 277 | } | ||
| 278 | |||
| 279 | $self->{'ambient_arybase'} = $arybase; | ||
| 280 | $self->{'ambient_warnings'} = $warning_bits; | ||
| 281 | $self->{'ambient_hints'} = $hint_bits; | ||
| 282 | $self->{'ambient_hinthash'} = $hinthash; | ||
| 283 | } | ||
| 284 | |||
| 285 | sub anon_hash_or_list($$$) | ||
| 286 | { | ||
| 287 | my ($self, $op, $cx) = @_; | ||
| 288 | my $name = $op->name; | ||
| 289 | my($pre, $post) = @{{"anonlist" => ["[","]"], | ||
| 290 | "anonhash" => ["{","}"]}->{$name}}; | ||
| 291 | my($expr, @exprs); | ||
| 292 | my $other_ops = [$op->first]; | ||
| 293 | $op = $op->first->sibling; # skip pushmark | ||
| 294 | for (; !B::Deparse::null($op); $op = $op->sibling) { | ||
| 295 | $expr = $self->deparse($op, 6, $op); | ||
| 296 | push @exprs, [$expr, $op]; | ||
| 297 | } | ||
| 298 | if ($pre eq "{" and $cx < 1) { | ||
| 299 | # Disambiguate that it's not a block | ||
| 300 | $pre = "+{"; | ||
| 301 | } | ||
| 302 | my $texts = [$pre, $self->combine(", ", \@exprs), $post]; | ||
| 303 | return info_from_list($op, $self, $texts, '', $name, | ||
| 304 | {body => \@exprs, | ||
| 305 | other_ops => $other_ops | ||
| 306 | }); | ||
| 307 | } | ||
| 308 | |||
| 309 | sub assoc_class { | ||
| 310 | my $op = shift; | ||
| 311 | my $name = $op->name; | ||
| 312 | if ($name eq "concat" and $op->first->name eq "concat") { | ||
| 313 | # avoid spurious '=' -- see comment in pp_concat | ||
| 314 | return "concat"; | ||
| 315 | } | ||
| 316 | if ($name eq "null" and B::class($op) eq "UNOP" | ||
| 317 | and $op->first->name =~ /^(and|x?or)$/ | ||
| 318 | and B::Deparse::null $op->first->sibling) | ||
| 319 | { | ||
| 320 | # Like all conditional constructs, OP_ANDs and OP_ORs are topped | ||
| 321 | # with a null that's used as the common end point of the two | ||
| 322 | # flows of control. For precedence purposes, ignore it. | ||
| 323 | # (COND_EXPRs have these too, but we don't bother with | ||
| 324 | # their associativity). | ||
| 325 | return assoc_class($op->first); | ||
| 326 | } | ||
| 327 | return $name . ($op->flags & B::OPf_STACKED ? "=" : ""); | ||
| 328 | } | ||
| 329 | |||
| 330 | # routines implementing classes of ops | ||
| 331 | |||
| 332 | sub baseop | ||
| 333 | { | ||
| 334 | my($self, $op, $cx, $name) = @_; | ||
| 335 | return $self->info_from_string("baseop $name", $op, $self->keyword($name)); | ||
| 336 | } | ||
| 337 | |||
| 338 | # Handle binary operators like +, and assignment | ||
| 339 | sub binop | ||
| 340 | { | ||
| 341 | |||
| 342 | my ($self, $op, $cx, $opname, $prec) = @_; | ||
| 343 | my ($flags, $type) = (0, ''); | ||
| 344 | if (scalar(@_) > 5) { | ||
| 345 | $flags = $_[5]; | ||
| 346 | $type = $_[6] if (scalar(@_) > 6); | ||
| 347 | } | ||
| 348 | my $left = $op->first; | ||
| 349 | my $right = $op->last; | ||
| 350 | my $eq = ""; | ||
| 351 | if ($op->flags & B::OPf_STACKED && $flags & B::Deparse::ASSIGN) { | ||
| 352 | $eq = "="; | ||
| 353 | $prec = 7; | ||
| 354 | } | ||
| 355 | if ($flags & SWAP_CHILDREN) { | ||
| 356 | ($left, $right) = ($right, $left); | ||
| 357 | } | ||
| 358 | my $lhs = $self->deparse_binop_left($op, $left, $prec); | ||
| 359 | if ($flags & B::Deparse::LIST_CONTEXT | ||
| 360 | && $lhs->{text} !~ /^(my|our|local|)[\@\(]/) { | ||
| 361 | $lhs->{maybe_parens} ||= {}; | ||
| 362 | $lhs->{maybe_parens}{force} = 'true'; | ||
| 363 | $lhs->{text} = "($lhs->{text})"; | ||
| 364 | } | ||
| 365 | |||
| 366 | my $rhs = $self->deparse_binop_right($op, $right, $prec); | ||
| 367 | if ($flags & SWAP_CHILDREN) { | ||
| 368 | # Not sure why this is right | ||
| 369 | $lhs->{prev_expr} = $rhs; | ||
| 370 | } else { | ||
| 371 | $rhs->{prev_expr} = $lhs; | ||
| 372 | } | ||
| 373 | |||
| 374 | $type = $type || 'binary operator'; | ||
| 375 | $type .= " $opname$eq"; | ||
| 376 | my $node = $self->info_from_template($type, $op, "%c $opname$eq %c", | ||
| 377 | undef, [$lhs, $rhs], | ||
| 378 | {maybe_parens => [$self, $cx, $prec]}); | ||
| 379 | $node->{prev_expr} = $rhs; | ||
| 380 | return $node; | ||
| 381 | } | ||
| 382 | |||
| 383 | # Left associative operators, like '+', for which | ||
| 384 | # $a + $b + $c is equivalent to ($a + $b) + $c | ||
| 385 | |||
| 386 | BEGIN { | ||
| 387 | %left = ('multiply' => 19, 'i_multiply' => 19, | ||
| 388 | 'divide' => 19, 'i_divide' => 19, | ||
| 389 | 'modulo' => 19, 'i_modulo' => 19, | ||
| 390 | 'repeat' => 19, | ||
| 391 | 'add' => 18, 'i_add' => 18, | ||
| 392 | 'subtract' => 18, 'i_subtract' => 18, | ||
| 393 | 'concat' => 18, | ||
| 394 | 'left_shift' => 17, 'right_shift' => 17, | ||
| 395 | 'bit_and' => 13, | ||
| 396 | 'bit_or' => 12, 'bit_xor' => 12, | ||
| 397 | 'and' => 3, | ||
| 398 | 'or' => 2, 'xor' => 2, | ||
| 399 | ); | ||
| 400 | } | ||
| 401 | |||
| 402 | sub code_list { | ||
| 403 | my ($self, $op, $cv) = @_; | ||
| 404 | |||
| 405 | # localise stuff relating to the current sub | ||
| 406 | $cv and | ||
| 407 | local($self->{'curcv'}) = $cv, | ||
| 408 | local($self->{'curcvlex'}), | ||
| 409 | local(@$self{qw'curstash warnings hints hinthash curcop'}) | ||
| 410 | = @$self{qw'curstash warnings hints hinthash curcop'}; | ||
| 411 | |||
| 412 | my $re; | ||
| 413 | for ($op = $op->first->sibling; !B::Deparse::null($op); $op = $op->sibling) { | ||
| 414 | if ($op->name eq 'null' and $op->flags & OPf_SPECIAL) { | ||
| 415 | my $scope = $op->first; | ||
| 416 | # 0 context (last arg to scopeop) means statement context, so | ||
| 417 | # the contents of the block will not be wrapped in do{...}. | ||
| 418 | my $block = scopeop($scope->first->name eq "enter", $self, | ||
| 419 | $scope, 0); | ||
| 420 | # next op is the source code of the block | ||
| 421 | $op = $op->sibling; | ||
| 422 | $re .= ($self->const_sv($op)->PV =~ m|^(\(\?\??\{)|)[0]; | ||
| 423 | my $multiline = $block =~ /\n/; | ||
| 424 | $re .= $multiline ? "\n\t" : ' '; | ||
| 425 | $re .= $block; | ||
| 426 | $re .= $multiline ? "\n\b})" : " })"; | ||
| 427 | } else { | ||
| 428 | $re = B::Deparse::re_dq_disambiguate($re, $self->re_dq($op)); | ||
| 429 | } | ||
| 430 | } | ||
| 431 | $re; | ||
| 432 | } | ||
| 433 | |||
| 434 | # Concatenation or '.' is special because concats-of-concats are | ||
| 435 | # optimized to save copying by making all but the first concat | ||
| 436 | # stacked. The effect is as if the programmer had written: | ||
| 437 | # ($a . $b) .= $c' | ||
| 438 | # but the above is illegal. | ||
| 439 | |||
| 440 | sub concat { | ||
| 441 | my $self = shift; | ||
| 442 | my($op, $cx) = @_; | ||
| 443 | my $left = $op->first; | ||
| 444 | my $right = $op->last; | ||
| 445 | my $eq = ""; | ||
| 446 | my $prec = 18; | ||
| 447 | if ($op->flags & OPf_STACKED and $op->first->name ne "concat") { | ||
| 448 | $eq = "="; | ||
| 449 | $prec = 7; | ||
| 450 | } | ||
| 451 | my $lhs = $self->deparse_binop_left($op, $left, $prec); | ||
| 452 | my $rhs = $self->deparse_binop_right($op, $right, $prec); | ||
| 453 | return $self->bin_info_join_maybe_parens($op, $lhs, $rhs, ".$eq", " ", $cx, $prec, | ||
| 454 | 'concat'); | ||
| 455 | } | ||
| 456 | |||
| 457 | # Handle pp_dbstate, and pp_nextstate and COP ops. | ||
| 458 | # | ||
| 459 | # Notice how subs and formats are inserted between statements here; | ||
| 460 | # also $[ assignments and pragmas. | ||
| 461 | sub cops | ||
| 462 | { | ||
| 463 | my ($self, $op, $cx, $name) = @_; | ||
| 464 | $self->{'curcop'} = $op; | ||
| 465 | my @texts; | ||
| 466 | my $opts = {}; | ||
| 467 | my @args_spec = (); | ||
| 468 | my $fmt = '%;'; | ||
| 469 | |||
| 470 | push @texts, $self->B::Deparse::cop_subs($op); | ||
| 471 | if (@texts) { | ||
| 472 | # Special marker to swallow up the semicolon | ||
| 473 | $opts->{'omit_next_semicolon'} = 1; | ||
| 474 | } | ||
| 475 | |||
| 476 | my $stash = $op->stashpv; | ||
| 477 | if ($stash ne $self->{'curstash'}) { | ||
| 478 | push @texts, $self->keyword("package") . " $stash;"; | ||
| 479 | $self->{'curstash'} = $stash; | ||
| 480 | } | ||
| 481 | |||
| 482 | if (OPpCONST_ARYBASE && $self->{'arybase'} != $op->arybase) { | ||
| 483 | push @texts, '$[ = '. $op->arybase .";"; | ||
| 484 | $self->{'arybase'} = $op->arybase; | ||
| 485 | } | ||
| 486 | |||
| 487 | my $warnings = $op->warnings; | ||
| 488 | my $warning_bits; | ||
| 489 | if ($warnings->isa("B::SPECIAL") && $$warnings == 4) { | ||
| 490 | $warning_bits = $warnings::Bits{"all"} & WARN_MASK; | ||
| 491 | } | ||
| 492 | elsif ($warnings->isa("B::SPECIAL") && $$warnings == 5) { | ||
| 493 | $warning_bits = $warnings::NONE; | ||
| 494 | } | ||
| 495 | elsif ($warnings->isa("B::SPECIAL")) { | ||
| 496 | $warning_bits = undef; | ||
| 497 | } | ||
| 498 | else { | ||
| 499 | $warning_bits = $warnings->PV & WARN_MASK; | ||
| 500 | } | ||
| 501 | |||
| 502 | if (defined ($warning_bits) and | ||
| 503 | !defined($self->{warnings}) || $self->{'warnings'} ne $warning_bits) { | ||
| 504 | my @warnings = $self->declare_warnings($self->{'warnings'}, $warning_bits); | ||
| 505 | foreach my $warning (@warnings) { | ||
| 506 | push @texts, $warning; | ||
| 507 | } | ||
| 508 | $self->{'warnings'} = $warning_bits; | ||
| 509 | } | ||
| 510 | |||
| 511 | my $hints = $] < 5.008009 ? $op->private : $op->hints; | ||
| 512 | my $old_hints = $self->{'hints'}; | ||
| 513 | if ($self->{'hints'} != $hints) { | ||
| 514 | my @hints = $self->declare_hints($self->{'hints'}, $hints); | ||
| 515 | foreach my $hint (@hints) { | ||
| 516 | push @texts, $hint; | ||
| 517 | } | ||
| 518 | $self->{'hints'} = $hints; | ||
| 519 | } | ||
| 520 | |||
| 521 | my $newhh; | ||
| 522 | if ($] > 5.009) { | ||
| 523 | $newhh = $op->hints_hash->HASH; | ||
| 524 | } | ||
| 525 | |||
| 526 | if ($] >= 5.015006) { | ||
| 527 | # feature bundle hints | ||
| 528 | my $from = $old_hints & $feature::hint_mask; | ||
| 529 | my $to = $ hints & $feature::hint_mask; | ||
| 530 | if ($from != $to) { | ||
| 531 | if ($to == $feature::hint_mask) { | ||
| 532 | if ($self->{'hinthash'}) { | ||
| 533 | delete $self->{'hinthash'}{$_} | ||
| 534 | for grep /^feature_/, keys %{$self->{'hinthash'}}; | ||
| 535 | } | ||
| 536 | else { $self->{'hinthash'} = {} } | ||
| 537 | $self->{'hinthash'} | ||
| 538 | = B::Deparse::_features_from_bundle($from, | ||
| 539 | $self->{'hinthash'}); | ||
| 540 | } | ||
| 541 | else { | ||
| 542 | my $bundle = | ||
| 543 | $feature::hint_bundles[$to >> $feature::hint_shift]; | ||
| 544 | $bundle =~ s/(\d[13579])\z/$1+1/e; # 5.11 => 5.12 | ||
| 545 | push @texts, | ||
| 546 | $self->keyword("no") . " feature ':all'", | ||
| 547 | $self->keyword("use") . " feature ':$bundle'"; | ||
| 548 | } | ||
| 549 | } | ||
| 550 | } | ||
| 551 | |||
| 552 | if ($] > 5.009) { | ||
| 553 | # FIXME use format specifiers | ||
| 554 | my @hints = $self->declare_hinthash( | ||
| 555 | $self->{'hinthash'}, $newhh, 0, $self->{hints}); | ||
| 556 | foreach my $hint (@hints) { | ||
| 557 | push @texts, $hint; | ||
| 558 | } | ||
| 559 | $self->{'hinthash'} = $newhh; | ||
| 560 | } | ||
| 561 | |||
| 562 | |||
| 563 | # This should go after of any branches that add statements, to | ||
| 564 | # increase the chances that it refers to the same line it did in | ||
| 565 | # the original program. | ||
| 566 | if ($self->{'linenums'} && $cx != .5) { # $cx == .5 means in a format | ||
| 567 | my $line = sprintf("\n# line %s '%s'", $op->line, $op->file); | ||
| 568 | $line .= sprintf(" 0x%x", $$op) if $self->{'opaddr'}; | ||
| 569 | $opts->{'omit_next_semicolon'} = 1; | ||
| 570 | push @texts, $line; | ||
| 571 | } | ||
| 572 | |||
| 573 | if ($op->label) { | ||
| 574 | $fmt .= "%c\n"; | ||
| 575 | push @args_spec, scalar(@args_spec); | ||
| 576 | push @texts, $op->label . ": " ; | ||
| 577 | } | ||
| 578 | |||
| 579 | my $node = $self->info_from_template($name, $op, $fmt, | ||
| 580 | \@args_spec, \@texts, $opts); | ||
| 581 | return $node; | ||
| 582 | } | ||
| 583 | |||
| 584 | sub deparse_binop_left { | ||
| 585 | my $self = shift; | ||
| 586 | my($op, $left, $prec) = @_; | ||
| 587 | if ($left{assoc_class($op)} && $left{assoc_class($left)} | ||
| 588 | and $left{assoc_class($op)} == $left{assoc_class($left)}) | ||
| 589 | { | ||
| 590 | return $self->deparse($left, $prec - .00001, $op); | ||
| 591 | } else { | ||
| 592 | return $self->deparse($left, $prec, $op); | ||
| 593 | } | ||
| 594 | } | ||
| 595 | |||
| 596 | # Right associative operators, like '=', for which | ||
| 597 | # $a = $b = $c is equivalent to $a = ($b = $c) | ||
| 598 | |||
| 599 | BEGIN { | ||
| 600 | %right = ('pow' => 22, | ||
| 601 | 'sassign=' => 7, 'aassign=' => 7, | ||
| 602 | 'multiply=' => 7, 'i_multiply=' => 7, | ||
| 603 | 'divide=' => 7, 'i_divide=' => 7, | ||
| 604 | 'modulo=' => 7, 'i_modulo=' => 7, | ||
| 605 | 'repeat=' => 7, | ||
| 606 | 'add=' => 7, 'i_add=' => 7, | ||
| 607 | 'subtract=' => 7, 'i_subtract=' => 7, | ||
| 608 | 'concat=' => 7, | ||
| 609 | 'left_shift=' => 7, 'right_shift=' => 7, | ||
| 610 | 'bit_and=' => 7, | ||
| 611 | 'bit_or=' => 7, 'bit_xor=' => 7, | ||
| 612 | 'andassign' => 7, | ||
| 613 | 'orassign' => 7, | ||
| 614 | ); | ||
| 615 | } | ||
| 616 | |||
| 617 | sub deparse_format($$$) | ||
| 618 | { | ||
| 619 | my ($self, $form, $parent) = @_; | ||
| 620 | my @texts; | ||
| 621 | local($self->{'curcv'}) = $form; | ||
| 622 | local($self->{'curcvlex'}); | ||
| 623 | local($self->{'in_format'}) = 1; | ||
| 624 | local(@$self{qw'curstash warnings hints hinthash'}) | ||
| 625 | = @$self{qw'curstash warnings hints hinthash'}; | ||
| 626 | my $op = $form->ROOT; | ||
| 627 | local $B::overlay = {}; | ||
| 628 | $self->pessimise($op, $form->START); | ||
| 629 | my $info = { | ||
| 630 | op => $op, | ||
| 631 | parent => $parent, | ||
| 632 | cop => $self->{'curcop'} | ||
| 633 | }; | ||
| 634 | $self->{optree}{$$op} = $info; | ||
| 635 | |||
| 636 | if ($op->first->name eq 'stub' || $op->first->name eq 'nextstate') { | ||
| 637 | my $info->{text} = "\f."; | ||
| 638 | return $info; | ||
| 639 | } | ||
| 640 | |||
| 641 | $op->{other_ops} = [$op->first]; | ||
| 642 | $op = $op->first->first; # skip leavewrite, lineseq | ||
| 643 | my $kid; | ||
| 644 | while (not B::Deparse::null $op) { | ||
| 645 | push @{$op->{other_ops}}, $op; | ||
| 646 | $op = $op->sibling; # skip nextstate | ||
| 647 | my @body; | ||
| 648 | push @{$op->{other_ops}}, $op->first; | ||
| 649 | $kid = $op->first->sibling; # skip a pushmark | ||
| 650 | push @texts, "\f".$self->const_sv($kid)->PV; | ||
| 651 | push @{$op->{other_ops}}, $kid; | ||
| 652 | $kid = $kid->sibling; | ||
| 653 | for (; not B::Deparse::null $kid; $kid = $kid->sibling) { | ||
| 654 | push @body, $self->deparse($kid, -1, $op); | ||
| 655 | $body[-1] =~ s/;\z//; | ||
| 656 | } | ||
| 657 | push @texts, "\f".$self->combine2str("\n", \@body) if @body; | ||
| 658 | $op = $op->sibling; | ||
| 659 | } | ||
| 660 | |||
| 661 | $info->{text} = $self->combine2str(\@texts) . "\f."; | ||
| 662 | $info->{texts} = \@texts; | ||
| 663 | return $info; | ||
| 664 | } | ||
| 665 | |||
| 666 | sub dedup_parens_func($$$) | ||
| 667 | { | ||
| 668 | my $self = shift; | ||
| 669 | my $sub_info = shift; | ||
| 670 | my ($args_ref) = @_; | ||
| 671 | my @args = @$args_ref; | ||
| 672 | if (scalar @args == 1 && substr($args[0], 0, 1) eq '(' && | ||
| 673 | substr($args[0], -1, 1) eq ')') { | ||
| 674 | return ($sub_info, $self->combine(', ', \@args), ); | ||
| 675 | } else { | ||
| 676 | return ($sub_info, '(', $self->combine(', ', \@args), ')', ); | ||
| 677 | } | ||
| 678 | } | ||
| 679 | |||
| 680 | sub deparse_binop_right { | ||
| 681 | my $self = shift; | ||
| 682 | my($op, $right, $prec) = @_; | ||
| 683 | if ($right{assoc_class($op)} && $right{assoc_class($right)} | ||
| 684 | and $right{assoc_class($op)} == $right{assoc_class($right)}) | ||
| 685 | { | ||
| 686 | return $self->deparse($right, $prec - .00001, $op); | ||
| 687 | } else { | ||
| 688 | return $self->deparse($right, $prec, $op); | ||
| 689 | } | ||
| 690 | } | ||
| 691 | |||
| 692 | # Iterate via sibling links a list of OP nodes starting with | ||
| 693 | # $first. Each OP is deparsed, with $op and $precedence each to get a | ||
| 694 | # node. Then the "prev" field in the node is set, and finally it is | ||
| 695 | # pushed onto the end of the $exprs reference ARRAY. | ||
| 696 | sub deparse_op_siblings($$$$$) | ||
| 697 | { | ||
| 698 | my ($self, $exprs, $kid, $op, $precedence) = @_; | ||
| 699 | my $prev_expr = undef; | ||
| 700 | $prev_expr = $exprs->[-1] if scalar @{$exprs}; | ||
| 701 | for ( ; !B::Deparse::null($kid); $kid = $kid->sibling) { | ||
| 702 | my $expr = $self->deparse($kid, $precedence, $op); | ||
| 703 | if (defined $expr) { | ||
| 704 | $expr->{prev_expr} = $prev_expr; | ||
| 705 | $prev_expr = $expr; | ||
| 706 | push @$exprs, $expr; | ||
| 707 | } | ||
| 708 | } | ||
| 709 | } | ||
| 710 | |||
| 711 | |||
| 712 | # tr/// and s/// (and tr[][], tr[]//, tr###, etc) | ||
| 713 | # note that tr(from)/to/ is OK, but not tr/from/(to) | ||
| 714 | sub double_delim { | ||
| 715 | my($from, $to) = @_; | ||
| 716 | my($succeed, $delim); | ||
| 717 | if ($from !~ m[/] and $to !~ m[/]) { | ||
| 718 | return "/$from/$to/"; | ||
| 719 | } elsif (($succeed, $from) = B::Deparse::balanced_delim($from) and $succeed) { | ||
| 720 | if (($succeed, $to) = B::Deparse::balanced_delim($to) and $succeed) { | ||
| 721 | return "$from$to"; | ||
| 722 | } else { | ||
| 723 | for $delim ('/', '"', '#') { # note no "'" -- s''' is special | ||
| 724 | return "$from$delim$to$delim" if index($to, $delim) == -1; | ||
| 725 | } | ||
| 726 | $to =~ s[/][\\/]g; | ||
| 727 | return "$from/$to/"; | ||
| 728 | } | ||
| 729 | } else { | ||
| 730 | for $delim ('/', '"', '#') { # note no ' | ||
| 731 | return "$delim$from$delim$to$delim" | ||
| 732 | if index($to . $from, $delim) == -1; | ||
| 733 | } | ||
| 734 | $from =~ s[/][\\/]g; | ||
| 735 | $to =~ s[/][\\/]g; | ||
| 736 | return "/$from/$to/"; | ||
| 737 | } | ||
| 738 | } | ||
| 739 | |||
| 740 | sub dq($$$) | ||
| 741 | { | ||
| 742 | my ($self, $op, $parent) = @_; | ||
| 743 | my $type = $op->name; | ||
| 744 | my $info; | ||
| 745 | if ($type eq "const") { | ||
| 746 | return info_from_text($op, $self, '$[', 'dq constant ary', {}) if $op->private & OPpCONST_ARYBASE; | ||
| 747 | return info_from_text($op, $self, | ||
| 748 | B::Deparse::uninterp(B::Deparse::escape_str(B::Deparse::unback($self->const_sv($op)->as_string))), | ||
| 749 | 'dq constant', {}); | ||
| 750 | } elsif ($type eq "concat") { | ||
| 751 | my $first = $self->dq($op->first, $op); | ||
| 752 | my $last = $self->dq($op->last, $op); | ||
| 753 | |||
| 754 | # Disambiguate "${foo}bar", "${foo}{bar}", "${foo}[1]", "$foo\::bar" | ||
| 755 | ($last =~ /^[A-Z\\\^\[\]_?]/ && | ||
| 756 | $first =~ s/([\$@])\^$/${1}{^}/) # "${^}W" etc | ||
| 757 | || ($last =~ /^[:'{\[\w_]/ && #' | ||
| 758 | $first =~ s/([\$@])([A-Za-z_]\w*)$/${1}{$2}/); | ||
| 759 | |||
| 760 | return info_from_list($op, $self, [$first->{text}, $last->{text}], '', 'dq_concat', | ||
| 761 | {body => [$first, $last]}); | ||
| 762 | } elsif ($type eq "join") { | ||
| 763 | return $self->deparse($op->last, 26, $op); # was join($", @ary) | ||
| 764 | } else { | ||
| 765 | return $self->deparse($op, 26, $parent); | ||
| 766 | } | ||
| 767 | my $kid = $self->dq($op->first->sibling, $op); | ||
| 768 | my $kid_text = $kid->{text}; | ||
| 769 | if ($type eq "uc") { | ||
| 770 | $info = info_from_lists(['\U', $kid, '\E'], '', 'dq_uc', {}); | ||
| 771 | } elsif ($type eq "lc") { | ||
| 772 | $info = info_from_lists(['\L', $kid, '\E'], '', 'dq_lc', {}); | ||
| 773 | } elsif ($type eq "ucfirst") { | ||
| 774 | $info = info_from_lists(['\u', $kid, '\E'], '', 'dq_ucfirst', {}); | ||
| 775 | } elsif ($type eq "lcfirst") { | ||
| 776 | $info = info_from_lists(['\l', $kid, '\E'], '', 'dq_lcfirst', {}); | ||
| 777 | } elsif ($type eq "quotemeta") { | ||
| 778 | $info = info_from_lists(['\Q', $kid, '\E'], '', 'dq_quotemeta', {}); | ||
| 779 | } elsif ($type eq "fc") { | ||
| 780 | $info = info_from_lists(['\F', $kid, '\E'], '', 'dq_fc', {}); | ||
| 781 | } | ||
| 782 | $info->{body} = [$kid]; | ||
| 783 | return $info; | ||
| 784 | } | ||
| 785 | |||
| 786 | # Handle unary operators that can occur as pseudo-listops inside | ||
| 787 | # double quotes | ||
| 788 | sub dq_unop | ||
| 789 | { | ||
| 790 | my($self, $op, $cx, $name, $prec, $flags) = (@_, 0, 0); | ||
| 791 | my $kid; | ||
| 792 | if ($op->flags & B::OPf_KIDS) { | ||
| 793 | my $pushmark_op = undef; | ||
| 794 | $kid = $op->first; | ||
| 795 | if (not B::Deparse::null $kid->sibling) { | ||
| 796 | # If there's more than one kid, the first is an ex-pushmark. | ||
| 797 | $pushmark_op = $kid; | ||
| 798 | $kid = $kid->sibling; | ||
| 799 | } | ||
| 800 | my $info = $self->maybe_parens_unop($name, $kid, $cx, $op); | ||
| 801 | if ($pushmark_op) { | ||
| 802 | # For the pushmark opc we'll consider it the "name" portion | ||
| 803 | # of info. We examine that to get the text. | ||
| 804 | my $text = $info->{text}; | ||
| 805 | my $word_end = index($text, ' '); | ||
| 806 | $word_end = length($text) unless $word_end > 0; | ||
| 807 | my $pushmark_info = | ||
| 808 | $self->info_from_string("dq $name", $op, $text, | ||
| 809 | {position => [0, $word_end]}); | ||
| 810 | $info->{other_ops} = [$pushmark_info]; | ||
| 811 | # $info->{other_ops} = [$pushmark_op]; | ||
| 812 | } | ||
| 813 | return $info; | ||
| 814 | } else { | ||
| 815 | $name .= '()' if $op->flags & B::OPf_SPECIAL; | ||
| 816 | return $self->info_from_string("dq $name", $op, $name) | ||
| 817 | } | ||
| 818 | Carp::confess("unhandled condition in dq_unop"); | ||
| 819 | } | ||
| 820 | |||
| 821 | sub dquote | ||
| 822 | { | ||
| 823 | my($self, $op, $cx) = @_; | ||
| 824 | # FIXME figure out how to use this | ||
| 825 | my $skipped_ops = [$op->first]; | ||
| 826 | my $kid = $op->first->sibling; # skip ex-stringify, pushmark | ||
| 827 | return $self->deparse($kid, $cx, $op) if $self->{'unquote'}; | ||
| 828 | $self->maybe_targmy($kid, $cx, | ||
| 829 | sub {$self->single_delim($kid, "qq", '"', | ||
| 830 | $self->info2str($self->dq($_[1], $op)) | ||
| 831 | )}); | ||
| 832 | } | ||
| 833 | |||
| 834 | sub elem | ||
| 835 | { | ||
| 836 | my ($self, $op, $cx, $left, $right, $padname) = @_; | ||
| 837 | my($array, $idx) = ($op->first, $op->first->sibling); | ||
| 838 | |||
| 839 | my $idx_info = $self->elem_or_slice_single_index($idx, $op); | ||
| 840 | my $opts = {body => [$idx_info]}; | ||
| 841 | |||
| 842 | unless ($array->name eq $padname) { # Maybe this has been fixed | ||
| 843 | $opts->{other_ops} = [$array]; | ||
| 844 | $array = $array->first; # skip rv2av (or ex-rv2av in _53+) | ||
| 845 | } | ||
| 846 | my @texts = (); | ||
| 847 | my $info; | ||
| 848 | my $array_name=$self->elem_or_slice_array_name($array, $left, $padname, 1); | ||
| 849 | if ($array_name) { | ||
| 850 | if ($array_name !~ /->\z/) { | ||
| 851 | if ($array_name eq '#') { | ||
| 852 | $array_name = '${#}'; | ||
| 853 | } else { | ||
| 854 | $array_name = '$' . $array_name ; | ||
| 855 | } | ||
| 856 | } | ||
| 857 | push @texts, $array_name; | ||
| 858 | push @texts, $left if $left; | ||
| 859 | push @texts, $idx_info->{text}, $right; | ||
| 860 | return info_from_list($op, $self, \@texts, '', 'elem', $opts) | ||
| 861 | } else { | ||
| 862 | # $x[20][3]{hi} or expr->[20] | ||
| 863 | my $type; | ||
| 864 | my $array_info = $self->deparse($array, 24, $op); | ||
| 865 | push @{$info->{body}}, $array_info; | ||
| 866 | @texts = ($array_info->{text}); | ||
| 867 | if (is_subscriptable($array)) { | ||
| 868 | push @texts, $left, $idx_info->{text}, $right; | ||
| 869 | $type = 'elem_no_arrow'; | ||
| 870 | } else { | ||
| 871 | push @texts, '->', $left, $idx_info->{text}, $right; | ||
| 872 | $type = 'elem_arrow'; | ||
| 873 | } | ||
| 874 | return info_from_list($op, $self, \@texts, '', $type, $opts); | ||
| 875 | } | ||
| 876 | Carp::confess("unhandled condition in elem"); | ||
| 877 | } | ||
| 878 | |||
| 879 | sub e_anoncode($$) | ||
| 880 | { | ||
| 881 | my ($self, $info) = @_; | ||
| 882 | my $sub_info = $self->deparse_sub($info->{code}); | ||
| 883 | return $self->info_from_template('sub anonymous', $sub_info->{op}, | ||
| 884 | 'sub %c', [0], [$sub_info]); | ||
| 885 | } | ||
| 886 | |||
| 887 | # Handle filetest operators -r, stat, etc. | ||
| 888 | sub filetest | ||
| 889 | { | ||
| 890 | my($self, $op, $cx, $name) = @_; | ||
| 891 | if (B::class($op) eq "UNOP") { | ||
| 892 | # Genuine '-X' filetests are exempt from the LLAFR, but not | ||
| 893 | # l?stat() | ||
| 894 | if ($name =~ /^-/) { | ||
| 895 | my $kid = $self->deparse($op->first, 16, $op); | ||
| 896 | return $self->info_from_template("filetest $name", $op, | ||
| 897 | "$name %c", undef, [$kid], | ||
| 898 | {maybe_parens => [$self, $cx, 16]}); | ||
| 899 | } | ||
| 900 | return $self->maybe_parens_unop($name, $op->first, $cx, $op); | ||
| 901 | } elsif (B::class($op) =~ /^(SV|PAD)OP$/) { | ||
| 902 | # FIXME redo after maybe_parens_func returns a string. | ||
| 903 | my @list = $self->maybe_parens_func($name, $self->pp_gv($op, 1), $cx, 16); | ||
| 904 | return info_from_list($op, $self, \@list, ' ', "filetest list $name", {}); | ||
| 905 | } else { | ||
| 906 | # I don't think baseop filetests ever survive ck_filetest, but... | ||
| 907 | return info_from_text($op, $self, $name, 'unop', {}); | ||
| 908 | } | ||
| 909 | } | ||
| 910 | |||
| 911 | sub func_needs_parens($$$$) | ||
| 912 | { | ||
| 913 | my($self, $first_param, $cx, $prec) = @_; | ||
| 914 | return ($prec <= $cx and substr($first_param, 0, 1) ne "(") || $self->{'parens'}; | ||
| 915 | } | ||
| 916 | |||
| 917 | sub givwhen | ||
| 918 | { | ||
| 919 | my($self, $op, $cx, $give_when) = @_; | ||
| 920 | |||
| 921 | my @arg_spec = (); | ||
| 922 | my @nodes = (); | ||
| 923 | my $enterop = $op->first; | ||
| 924 | my $fmt; | ||
| 925 | my ($head, $block); | ||
| 926 | if ($enterop->flags & B::OPf_SPECIAL) { | ||
| 927 | $head = $self->keyword("default"); | ||
| 928 | $fmt = "$give_when ($head)\n\%+%c\n%-}\n"; | ||
| 929 | $block = $self->deparse($enterop->first, 0, $enterop, $op); | ||
| 930 | } | ||
| 931 | else { | ||
| 932 | my $cond = $enterop->first; | ||
| 933 | my $cond_node = $self->deparse($cond, 1, $enterop, $op); | ||
| 934 | push @nodes, $cond_node; | ||
| 935 | $fmt = "$give_when (%c)\n\%+%c\n%-}\n"; | ||
| 936 | $block = $self->deparse($cond->sibling, 0, $enterop, $op); | ||
| 937 | } | ||
| 938 | push @nodes, $block; | ||
| 939 | |||
| 940 | return $self->info_from_template("{} $give_when", | ||
| 941 | "%c\n\%+%c\n%-}\n", [0, 1], | ||
| 942 | \@nodes); | ||
| 943 | } | ||
| 944 | |||
| 945 | # Handles the indirect operators, print, say(), sort() | ||
| 946 | sub indirop | ||
| 947 | { | ||
| 948 | my($self, $op, $cx, $name) = @_; | ||
| 949 | my($expr, @exprs); | ||
| 950 | my $firstkid = my $kid = $op->first->sibling; | ||
| 951 | my $indir_info; | ||
| 952 | my $type = $name; | ||
| 953 | my $first_op = $op->first; | ||
| 954 | my @skipped_ops = ($first_op); | ||
| 955 | my @indir = (); | ||
| 956 | my @args_spec = (); | ||
| 957 | my $fmt = ''; | ||
| 958 | |||
| 959 | if ($op->flags & OPf_STACKED) { | ||
| 960 | push @skipped_ops, $kid; | ||
| 961 | my $indir_op = $kid->first; # skip rv2gv | ||
| 962 | if (B::Deparse::is_scope($indir_op)) { | ||
| 963 | $indir_info = $self->deparse($indir_op, 0, $op); | ||
| 964 | if ($indir_info->{text} eq '') { | ||
| 965 | $fmt = '{;}'; | ||
| 966 | } else { | ||
| 967 | $fmt = '{%c}'; | ||
| 968 | push @args_spec, $indir_info; | ||
| 969 | } | ||
| 970 | } elsif ($indir_op->name eq "const" && $indir_op->private & OPpCONST_BARE) { | ||
| 971 | $fmt = $self->const_sv($indir_op)->PV; | ||
| 972 | } else { | ||
| 973 | $indir_info = $self->deparse($indir_op, 24, $op); | ||
| 974 | $fmt = '%c'; | ||
| 975 | push @args_spec, $indir_info; | ||
| 976 | } | ||
| 977 | $fmt .= ' '; | ||
| 978 | $kid = $kid->sibling; | ||
| 979 | } | ||
| 980 | |||
| 981 | if ($name eq "sort" && $op->private & (OPpSORT_NUMERIC | OPpSORT_INTEGER)) { | ||
| 982 | $type = 'sort numeric or integer'; | ||
| 983 | $fmt = ($op->private & OPpSORT_DESCEND) | ||
| 984 | ? '{$b <=> $a} ': '{$a <=> $b} '; | ||
| 985 | } elsif ($name eq "sort" && $op->private & OPpSORT_DESCEND) { | ||
| 986 | $type = 'sort_descend'; | ||
| 987 | $fmt = '{$b cmp $a} '; | ||
| 988 | } | ||
| 989 | |||
| 990 | # FIXME: turn into a function; | ||
| 991 | my $prev_expr = $exprs[-1]; | ||
| 992 | for (; !B::Deparse::null($kid); $kid = $kid->sibling) { | ||
| 993 | # This prevents us from using deparse_op_siblings | ||
| 994 | my $operator_context; | ||
| 995 | if (!$fmt && $kid == $firstkid | ||
| 996 | && $name eq "sort" | ||
| 997 | && $firstkid->name =~ /^enter(xs)?sub/) { | ||
| 998 | $operator_context = 16; | ||
| 999 | } else { | ||
| 1000 | $operator_context = 6; | ||
| 1001 | } | ||
| 1002 | $expr = $self->deparse($kid, $operator_context, $op); | ||
| 1003 | if (defined $expr) { | ||
| 1004 | $expr->{prev_expr} = $prev_expr; | ||
| 1005 | $prev_expr = $expr; | ||
| 1006 | push @exprs, $expr; | ||
| 1007 | } | ||
| 1008 | } | ||
| 1009 | |||
| 1010 | # Extend $name possibly by adding "reverse". | ||
| 1011 | my $name2; | ||
| 1012 | if ($name eq "sort" && $op->private & OPpSORT_REVERSE) { | ||
| 1013 | $name2 = $self->keyword('reverse') . ' ' . $self->keyword('sort'); | ||
| 1014 | } else { | ||
| 1015 | $name2 = $self->keyword($name) | ||
| 1016 | } | ||
| 1017 | |||
| 1018 | if ($name eq "sort" && ($op->private & OPpSORT_INPLACE)) { | ||
| 1019 | $fmt = "%c = $name2 $fmt %c"; | ||
| 1020 | # FIXME: do better with skipped ops | ||
| 1021 | return $self->info_from_template($name2, $op, | ||
| 1022 | [0, 0], \@exprs, {other_ops => \@skipped_ops}); | ||
| 1023 | } | ||
| 1024 | |||
| 1025 | |||
| 1026 | my $node; | ||
| 1027 | if ($fmt ne "" && $name eq "sort") { | ||
| 1028 | # We don't want to say "sort(f 1, 2, 3)", since perl -w will | ||
| 1029 | # give bareword warnings in that case. Therefore if context | ||
| 1030 | # requires, we'll put parens around the outside "(sort f 1, 2, | ||
| 1031 | # 3)". Unfortunately, we'll currently think the parens are | ||
| 1032 | # necessary more often that they really are, because we don't | ||
| 1033 | # distinguish which side of an assignment we're on. | ||
| 1034 | $node = $self->info_from_template($name2, $op, | ||
| 1035 | "$name2 %C", | ||
| 1036 | [[0, $#exprs, ', ']], | ||
| 1037 | \@exprs, | ||
| 1038 | { | ||
| 1039 | other_ops => \@skipped_ops, | ||
| 1040 | maybe_parens => { | ||
| 1041 | context => $cx, | ||
| 1042 | precedence => 5}}); | ||
| 1043 | |||
| 1044 | } elsif (!$fmt && $name eq "sort" | ||
| 1045 | && !B::Deparse::null($op->first->sibling) | ||
| 1046 | && $op->first->sibling->name eq 'entersub' ) { | ||
| 1047 | # We cannot say sort foo(bar), as foo will be interpreted as a | ||
| 1048 | # comparison routine. We have to say sort(...) in that case. | ||
| 1049 | $node = $self->info_from_template("$name2()", $op, | ||
| 1050 | "$name2(%C)", | ||
| 1051 | [[0, $#exprs, ', ']], | ||
| 1052 | \@exprs, | ||
| 1053 | {other_ops => \@skipped_ops}); | ||
| 1054 | |||
| 1055 | } else { | ||
| 1056 | if (@exprs) { | ||
| 1057 | my $fmt; | ||
| 1058 | if ($self->func_needs_parens($exprs[0]->{text}, $cx, 5)) { | ||
| 1059 | $fmt = "$name2(%C)" | ||
| 1060 | } else { | ||
| 1061 | $fmt = "$name2 %C" | ||
| 1062 | } | ||
| 1063 | $node = $self->info_from_template($name2, $first_op, $fmt, | ||
| 1064 | [[0, $#exprs, ', ']], | ||
| 1065 | \@exprs, | ||
| 1066 | {other_ops => \@skipped_ops, | ||
| 1067 | maybe_parens => [$self, $cx, 5]}); | ||
| 1068 | |||
| 1069 | } else { | ||
| 1070 | $type="indirect $name2"; | ||
| 1071 | $type .= '()' if (7 < $cx); # FIXME - do with format specifier | ||
| 1072 | $node = $self->info_from_string($first_op, $name2, | ||
| 1073 | {other_ops => \@skipped_ops}) | ||
| 1074 | } | ||
| 1075 | } | ||
| 1076 | |||
| 1077 | # Handle skipped ops | ||
| 1078 | my @new_ops; | ||
| 1079 | my $position = [0, length($name2)]; | ||
| 1080 | my $str = $node->{text}; | ||
| 1081 | foreach my $skipped_op (@skipped_ops) { | ||
| 1082 | my $new_op = $self->info_from_string($op->name, $skipped_op, $str, | ||
| 1083 | {position => $position}); | ||
| 1084 | push @new_ops, $new_op; | ||
| 1085 | } | ||
| 1086 | $node->{other_ops} = \@new_ops; | ||
| 1087 | return $node; | ||
| 1088 | } | ||
| 1089 | |||
| 1090 | # The version of null_op_list after 5.22 | ||
| 1091 | # Note: this uses "op" not "kid" | ||
| 1092 | sub is_list_newer($$) { | ||
| 1093 | my ($self, $op) = @_; | ||
| 1094 | my $kid = $op->first; | ||
| 1095 | return 1 if $kid->name eq 'pushmark'; | ||
| 1096 | return ($kid->name eq 'null' | ||
| 1097 | && $kid->targ == OP_PUSHMARK | ||
| 1098 | && B::Deparse::_op_is_or_was($op, B::Deparse::OP_LIST)); | ||
| 1099 | } | ||
| 1100 | |||
| 1101 | |||
| 1102 | # The version of null_op_list before 5.22 | ||
| 1103 | # Note: this uses "kid", not "op" | ||
| 1104 | sub is_list_older($) { | ||
| 1105 | my ($self, $kid) = @_; | ||
| 1106 | # Something may be funky where without the convesion we are getting "" | ||
| 1107 | # as a return | ||
| 1108 | return ($kid->name eq 'pushmark') ? 1 : 0; | ||
| 1109 | } | ||
| 1110 | |||
| 1111 | # This handle logical ops: "if"/"until", "&&", "and", ... | ||
| 1112 | # The one-line "while"/"until" is handled in pp_leave. | ||
| 1113 | sub logop | ||
| 1114 | { | ||
| 1115 | my ($self, $op, $cx, $lowop, $lowprec, $highop, | ||
| 1116 | $highprec, $blockname) = @_; | ||
| 1117 | my $left = $op->first; | ||
| 1118 | my $right = $op->first->sibling; | ||
| 1119 | my ($lhs, $rhs, $type, $opname); | ||
| 1120 | my $opts = {}; | ||
| 1121 | if ($cx < 1 and B::Deparse::is_scope($right) and $blockname | ||
| 1122 | and $self->{'expand'} < 7) { | ||
| 1123 | # Is this branch used in 5.26 and above? | ||
| 1124 | # <if> ($a) {$b} | ||
| 1125 | my $if_cond_info = $self->deparse($left, 1, $op); | ||
| 1126 | my $if_body_info = $self->deparse($right, 0, $op); | ||
| 1127 | return $self->info_from_template("$blockname () {}", $op, | ||
| 1128 | "$blockname (%c) {\n%+%c\n%-}", | ||
| 1129 | [0, 1], | ||
| 1130 | [$if_cond_info, $if_body_info], $opts); | ||
| 1131 | } elsif ($cx < 1 and $blockname and not $self->{'parens'} | ||
| 1132 | and $self->{'expand'} < 7) { # $b if $a | ||
| 1133 | # Note: order of lhs and rhs is reversed | ||
| 1134 | $lhs = $self->deparse($right, 1, $op); | ||
| 1135 | $rhs = $self->deparse($left, 1, $op); | ||
| 1136 | $opname = $blockname; | ||
| 1137 | $type = "suffix $opname" | ||
| 1138 | } elsif ($cx > $lowprec and $highop) { | ||
| 1139 | # low-precedence operator like $a && $b | ||
| 1140 | $lhs = $self->deparse_binop_left($op, $left, $highprec); | ||
| 1141 | $rhs = $self->deparse_binop_right($op, $right, $highprec); | ||
| 1142 | $opname = $highop; | ||
| 1143 | $opts = {maybe_parens => [$self, $cx, $highprec]}; | ||
| 1144 | } else { | ||
| 1145 | # high-precedence operator like $a and $b | ||
| 1146 | $lhs = $self->deparse_binop_left($op, $left, $lowprec); | ||
| 1147 | $rhs = $self->deparse_binop_right($op, $right, $lowprec); | ||
| 1148 | $opname = $lowop; | ||
| 1149 | $opts = {maybe_parens => [$self, $cx, $lowprec]}; | ||
| 1150 | } | ||
| 1151 | $type ||= $opname; | ||
| 1152 | return $self->info_from_template($type, $op, "%c $opname %c", | ||
| 1153 | [0, 1], [$lhs, $rhs], $opts); | ||
| 1154 | } | ||
| 1155 | |||
| 1156 | # This handle list ops: "open", "pack", "return" ... | ||
| 1157 | sub listop | ||
| 1158 | { | ||
| 1159 | my($self, $op, $cx, $name, $kid, $nollafr) = @_; | ||
| 1160 | my(@exprs, @new_nodes, @skipped_ops); | ||
| 1161 | my $parens = ($cx >= 5) || $self->{'parens'}; | ||
| 1162 | |||
| 1163 | unless ($kid) { | ||
| 1164 | push @skipped_ops, $op->first; | ||
| 1165 | $kid = $op->first->sibling; | ||
| 1166 | } | ||
| 1167 | |||
| 1168 | # If there are no arguments, add final parentheses (or parenthesize the | ||
| 1169 | # whole thing if the llafr does not apply) to account for cases like | ||
| 1170 | # (return)+1 or setpgrp()+1. When the llafr does not apply, we use a | ||
| 1171 | # precedence of 6 (< comma), as "return, 1" does not need parentheses. | ||
| 1172 | if (B::Deparse::null $kid) { | ||
| 1173 | my $fullname = $self->keyword($name); | ||
| 1174 | my $text = $nollafr | ||
| 1175 | ? $self->maybe_parens($fullname, $cx, 7) | ||
| 1176 | : $fullname . '()' x (7 < $cx); | ||
| 1177 | return $self->info_from_string("listop $name", $op, $text); | ||
| 1178 | } | ||
| 1179 | my $first; | ||
| 1180 | my $fullname = $self->keyword($name); | ||
| 1181 | my $proto = prototype("CORE::$name"); | ||
| 1182 | if ( | ||
| 1183 | ( (defined $proto && $proto =~ /^;?\*/) | ||
| 1184 | || $name eq 'select' # select(F) doesn't have a proto | ||
| 1185 | ) | ||
| 1186 | && $kid->name eq "rv2gv" | ||
| 1187 | && !($kid->private & B::OPpLVAL_INTRO) | ||
| 1188 | ) { | ||
| 1189 | $first = $self->rv2gv_or_string($kid->first, $op); | ||
| 1190 | } | ||
| 1191 | else { | ||
| 1192 | $first = $self->deparse($kid, 6, $op); | ||
| 1193 | } | ||
| 1194 | if ($name eq "chmod" && $first->{text} =~ /^\d+$/) { | ||
| 1195 | my $transform_fn = sub {sprintf("%#o", $self->info2str(shift))}; | ||
| 1196 | $first = $self->info_from_template("chmod octal", undef, | ||
| 1197 | "%F", [[0, $transform_fn]], | ||
| 1198 | [$first], {'relink_children' => [0]}); | ||
| 1199 | push @new_nodes, $first; | ||
| 1200 | } | ||
| 1201 | |||
| 1202 | # FIXME: fold this into a template | ||
| 1203 | $first->{text} = "+" + $first->{text} | ||
| 1204 | if not $parens and not $nollafr and substr($first->{text}, 0, 1) eq "("; | ||
| 1205 | |||
| 1206 | push @exprs, $first; | ||
| 1207 | $kid = $kid->sibling; | ||
| 1208 | if (defined $proto && $proto =~ /^\*\*/ && $kid->name eq "rv2gv" | ||
| 1209 | && !($kid->private & B::OPpLVAL_INTRO)) { | ||
| 1210 | $first = $self->rv2gv_or_string($kid->first, $op); | ||
| 1211 | push @exprs, $first; | ||
| 1212 | $kid = $kid->sibling; | ||
| 1213 | } | ||
| 1214 | |||
| 1215 | $self->deparse_op_siblings(\@exprs, $kid, $op, 6); | ||
| 1216 | |||
| 1217 | if ($name eq "reverse" && ($op->private & B::OPpREVERSE_INPLACE)) { | ||
| 1218 | my $texts = [$exprs[0->{text}], '=', | ||
| 1219 | $fullname . ($parens ? "($exprs[0]->{text})" : " $exprs[0]->{text}")]; | ||
| 1220 | return info_from_list($op, $self, $texts, ' ', 'listop_reverse', {}); | ||
| 1221 | } | ||
| 1222 | |||
| 1223 | my $opts = {}; | ||
| 1224 | my $type; | ||
| 1225 | my $fmt; | ||
| 1226 | |||
| 1227 | if ($name =~ /^(system|exec)$/ | ||
| 1228 | && ($op->flags & B::OPf_STACKED) | ||
| 1229 | && @exprs > 1) | ||
| 1230 | { | ||
| 1231 | # handle the "system(prog a1, a2, ...)" form | ||
| 1232 | # where there is no ', ' between the first two arguments. | ||
| 1233 | if ($parens && $nollafr) { | ||
| 1234 | $fmt = "($fullname %c %C)"; | ||
| 1235 | $type = "listop ($fullname)"; | ||
| 1236 | } elsif ($parens) { | ||
| 1237 | $fmt = "$fullname(%c %C)"; | ||
| 1238 | $type = "listop $fullname()"; | ||
| 1239 | } else { | ||
| 1240 | $fmt = "$fullname %c %C"; | ||
| 1241 | $type = "listop $fullname"; | ||
| 1242 | } | ||
| 1243 | return $self->info_from_template($type, $op, $fmt, | ||
| 1244 | [0, [1, $#exprs, ', ']], \@exprs); | ||
| 1245 | |||
| 1246 | } | ||
| 1247 | |||
| 1248 | $fmt = "%c %C"; | ||
| 1249 | if ($parens && $nollafr) { | ||
| 1250 | # FIXME: do with parens mechanism | ||
| 1251 | $fmt = "($fullname %C)"; | ||
| 1252 | $type = "listop ($fullname)"; | ||
| 1253 | } elsif ($parens) { | ||
| 1254 | $fmt = "$fullname(%C)"; | ||
| 1255 | $type = "listop $fullname()"; | ||
| 1256 | } else { | ||
| 1257 | $fmt = "$fullname %C"; | ||
| 1258 | $type = "listop $fullname"; | ||
| 1259 | } | ||
| 1260 | $opts->{synthesized_nodes} = \@new_nodes if @new_nodes; | ||
| 1261 | my $node = $self->info_from_template($type, $op, $fmt, | ||
| 1262 | [[0, $#exprs, ', ']], \@exprs, | ||
| 1263 | $opts); | ||
| 1264 | if (@skipped_ops) { | ||
| 1265 | # if we have skipped ops like pushmark, we will use $full name | ||
| 1266 | # as the part it represents. | ||
| 1267 | ## FIXME | ||
| 1268 | my @new_ops; | ||
| 1269 | my $position = [0, length($fullname)]; | ||
| 1270 | my $str = $node->{text}; | ||
| 1271 | my @skipped_nodes; | ||
| 1272 | for my $skipped_op (@skipped_ops) { | ||
| 1273 | my $new_op = $self->info_from_string($op->name, $skipped_op, $str, | ||
| 1274 | {position => $position}); | ||
| 1275 | push @new_ops, $new_op; | ||
| 1276 | } | ||
| 1277 | $node->{other_ops} = \@new_ops; | ||
| 1278 | } | ||
| 1279 | return $node; | ||
| 1280 | } | ||
| 1281 | |||
| 1282 | sub loop_common | ||
| 1283 | { | ||
| 1284 | my $self = shift; | ||
| 1285 | my($op, $cx, $init) = @_; | ||
| 1286 | my $enter = $op->first; | ||
| 1287 | my $kid = $enter->sibling; | ||
| 1288 | |||
| 1289 | my @skipped_ops = ($enter); | ||
| 1290 | local(@$self{qw'curstash warnings hints hinthash'}) | ||
| 1291 | = @$self{qw'curstash warnings hints hinthash'}; | ||
| 1292 | |||
| 1293 | my ($body, @body); | ||
| 1294 | my @nodes = (); | ||
| 1295 | my ($bare, $cond_info) = (0, undef); | ||
| 1296 | my $fmt = ''; | ||
| 1297 | my $var_fmt; | ||
| 1298 | my @args_spec = (); | ||
| 1299 | my $opts = {}; | ||
| 1300 | my $type = 'loop'; | ||
| 1301 | |||
| 1302 | if ($kid->name eq "lineseq") { | ||
| 1303 | # bare or infinite loop | ||
| 1304 | $type .= ' while (1)'; | ||
| 1305 | |||
| 1306 | if ($kid->last->name eq "unstack") { # infinite | ||
| 1307 | $fmt .= 'while (1)'; | ||
| 1308 | } else { | ||
| 1309 | $bare = 1; | ||
| 1310 | } | ||
| 1311 | $body = $kid; | ||
| 1312 | } elsif ($enter->name eq "enteriter") { | ||
| 1313 | # foreach | ||
| 1314 | $type .= ' foreach'; | ||
| 1315 | |||
| 1316 | my $ary = $enter->first->sibling; # first was pushmark | ||
| 1317 | push @skipped_ops, $enter->first, $ary->first->sibling; | ||
| 1318 | my ($ary_fmt, $var_info); | ||
| 1319 | my $var = $ary->sibling; | ||
| 1320 | if (B::Deparse::null $var) { | ||
| 1321 | if (($enter->flags & B::OPf_SPECIAL) && ($] < 5.009)) { | ||
| 1322 | # thread special var, under 5005threads | ||
| 1323 | $var_fmt = $self->pp_threadsv($enter, 1); | ||
| 1324 | } else { # regular my() variable | ||
| 1325 | $var_info = $self->pp_padsv($enter, 1, 1); | ||
| 1326 | push @nodes, $var_info; | ||
| 1327 | $var_fmt = '%c'; | ||
| 1328 | push @args_spec, $#nodes; | ||
| 1329 | } | ||
| 1330 | } elsif ($var->name eq "rv2gv") { | ||
| 1331 | $var_info = $self->pp_rv2sv($var, 1); | ||
| 1332 | push @nodes, $var_info; | ||
| 1333 | if ($enter->private & B::OPpOUR_INTRO) { | ||
| 1334 | # "our" declarations don't have package names | ||
| 1335 | my $transform_fn = sub {$_[0] =~ s/^(.).*::/$1/}; | ||
| 1336 | $var_fmt = "our %F"; | ||
| 1337 | push @args_spec, [$#nodes, $transform_fn]; | ||
| 1338 | } else { | ||
| 1339 | $var_fmt = '%c'; | ||
| 1340 | push @args_spec, $#nodes; | ||
| 1341 | } | ||
| 1342 | } elsif ($var->name eq "gv") { | ||
| 1343 | $var_info = $self->deparse($var, 1, $op); | ||
| 1344 | push @nodes, $var_info; | ||
| 1345 | $var_fmt = '$%c'; | ||
| 1346 | push @args_spec, $#nodes; | ||
| 1347 | } | ||
| 1348 | |||
| 1349 | if ($ary->name eq 'null' and $enter->private & B::OPpITER_REVERSED) { | ||
| 1350 | # "reverse" was optimised away | ||
| 1351 | push @nodes, listop($self, $ary->first->sibling, 1, 'reverse'); | ||
| 1352 | $ary_fmt = "%c"; | ||
| 1353 | push @args_spec, $#nodes; | ||
| 1354 | } elsif ($enter->flags & B::OPf_STACKED | ||
| 1355 | and not B::Deparse::null $ary->first->sibling->sibling) { | ||
| 1356 | push @args_spec, scalar(@nodes), scalar(@nodes+1); | ||
| 1357 | push @nodes, ($self->deparse($ary->first->sibling, 9, $op), | ||
| 1358 | $self->deparse($ary->first->sibling->sibling, 9, $op)); | ||
| 1359 | $ary_fmt = '(%c .. %c)'; | ||
| 1360 | |||
| 1361 | } else { | ||
| 1362 | push @nodes, $self->deparse($ary, 1, $op); | ||
| 1363 | $ary_fmt = "%c"; | ||
| 1364 | push @args_spec, $#nodes; | ||
| 1365 | } | ||
| 1366 | |||
| 1367 | # skip OP_AND and OP_ITER | ||
| 1368 | push @skipped_ops, $kid->first, $kid->first->first; | ||
| 1369 | $body = $kid->first->first->sibling; | ||
| 1370 | |||
| 1371 | if (!B::Deparse::is_state $body->first | ||
| 1372 | and $body->first->name !~ /^(?:stub|leave|scope)$/) { | ||
| 1373 | # FIXME: | ||
| 1374 | # Carp::confess("var ne \$_") unless join('', @var_text) eq '$_'; | ||
| 1375 | push @skipped_ops, $body->first; | ||
| 1376 | $body = $body->first; | ||
| 1377 | my $body_info = $self->deparse($body, 2, $op); | ||
| 1378 | push @nodes, $body_info; | ||
| 1379 | return $self->info_from_template("foreach", $op, | ||
| 1380 | "$var_fmt foreach ($ary_fmt)", | ||
| 1381 | \@args_spec, \@nodes, | ||
| 1382 | {other_ops => \@skipped_ops}); | ||
| 1383 | } | ||
| 1384 | $fmt = "foreach $var_fmt $ary_fmt"; | ||
| 1385 | } elsif ($kid->name eq "null") { | ||
| 1386 | # while/until | ||
| 1387 | |||
| 1388 | $kid = $kid->first; | ||
| 1389 | my $name = {"and" => "while", "or" => "until"}->{$kid->name}; | ||
| 1390 | $type .= " $name"; | ||
| 1391 | $cond_info = $self->deparse($kid->first, 1, $op); | ||
| 1392 | $fmt = "$name (%c) "; | ||
| 1393 | push @nodes, $cond_info; | ||
| 1394 | $body = $kid->first->sibling; | ||
| 1395 | @args_spec = (0); | ||
| 1396 | } elsif ($kid->name eq "stub") { | ||
| 1397 | # bare and empty | ||
| 1398 | return info_from_text($op, $self, '{;}', 'empty loop', {}); | ||
| 1399 | } | ||
| 1400 | |||
| 1401 | # If there isn't a continue block, then the next pointer for the loop | ||
| 1402 | # will point to the unstack, which is kid's last child, except | ||
| 1403 | # in a bare loop, when it will point to the leaveloop. When neither of | ||
| 1404 | # these conditions hold, then the second-to-last child is the continue | ||
| 1405 | # block (or the last in a bare loop). | ||
| 1406 | my $cont_start = $enter->nextop; | ||
| 1407 | my ($cont, @cont_text, $body_info); | ||
| 1408 | my @cont = (); | ||
| 1409 | if ($$cont_start != $$op && ${$cont_start} != ${$body->last}) { | ||
| 1410 | $type .= ' continue'; | ||
| 1411 | |||
| 1412 | if ($bare) { | ||
| 1413 | $cont = $body->last; | ||
| 1414 | } else { | ||
| 1415 | $cont = $body->first; | ||
| 1416 | while (!B::Deparse::null($cont->sibling->sibling)) { | ||
| 1417 | $cont = $cont->sibling; | ||
| 1418 | } | ||
| 1419 | } | ||
| 1420 | my $state = $body->first; | ||
| 1421 | my $cuddle = " "; | ||
| 1422 | my @states; | ||
| 1423 | for (; $$state != $$cont; $state = $state->sibling) { | ||
| 1424 | push @states, $state; | ||
| 1425 | } | ||
| 1426 | $body_info = $self->lineseq(undef, 0, @states); | ||
| 1427 | if (defined $cond_info | ||
| 1428 | and not B::Deparse::is_scope($cont) | ||
| 1429 | and $self->{'expand'} < 3) { | ||
| 1430 | my $cont_info = $self->deparse($cont, 1, $op); | ||
| 1431 | my $init = defined($init) ? $init : ' '; | ||
| 1432 | @nodes = ($init, $cond_info, $cont_info); | ||
| 1433 | # @nodes_text = ('for', '(', "$init_text;", $cont_info->{text}, ')'); | ||
| 1434 | $fmt = 'for (%c; %c; %c) '; | ||
| 1435 | @args_spec = (0, 1, 2); | ||
| 1436 | $opts->{'omit_next_semicolon'} = 1; | ||
| 1437 | } else { | ||
| 1438 | my $cont_info = $self->deparse($cont, 0, $op); | ||
| 1439 | @nodes = ($init, $cont_info); | ||
| 1440 | @args_spec = (0, 1); | ||
| 1441 | $opts->{'omit_next_semicolon'} = 1; | ||
| 1442 | @cont_text = ($cuddle, 'continue', "{\n\t", | ||
| 1443 | $cont_info->{text} , "\n\b}"); | ||
| 1444 | } | ||
| 1445 | } else { | ||
| 1446 | return info_from_text($op, $self, '', 'loop_no_body', {}) | ||
| 1447 | if !defined $body; | ||
| 1448 | if (defined $init) { | ||
| 1449 | @nodes = ($init, $cond_info); | ||
| 1450 | $fmt = 'for (%c; %c;) '; | ||
| 1451 | @args_spec = (0, 1); | ||
| 1452 | } | ||
| 1453 | $opts->{'omit_next_semicolon'} = 1; | ||
| 1454 | $body_info = $self->deparse($body, 0, $op); | ||
| 1455 | } | ||
| 1456 | |||
| 1457 | # (my $body_text = $body_info->{text}) =~ s/;?$/;\n/; | ||
| 1458 | # my @texts = (@nodes_text, "{\n\t", $body_text, "\b}", @cont_text); | ||
| 1459 | |||
| 1460 | push @nodes, $body_info; | ||
| 1461 | push @args_spec, $#nodes; | ||
| 1462 | $fmt .= " {\n%+%c%-\n}"; | ||
| 1463 | if (@cont_text) { | ||
| 1464 | push @nodes, @cont_text; | ||
| 1465 | push @args_spec, $#nodes; | ||
| 1466 | $type .= ' cont'; | ||
| 1467 | $fmt .= '%c'; | ||
| 1468 | } | ||
| 1469 | return $self->info_from_template($type, $op, $fmt, \@args_spec, \@nodes, $opts) | ||
| 1470 | } | ||
| 1471 | |||
| 1472 | # loop expressions | ||
| 1473 | sub loopex | ||
| 1474 | { | ||
| 1475 | my ($self, $op, $cx, $name) = @_; | ||
| 1476 | my $opts = {maybe_parens => [$self, $cx, 7]}; | ||
| 1477 | if (B::class($op) eq "PVOP") { | ||
| 1478 | return info_from_list($op, $self, [$name, $op->pv], ' ', | ||
| 1479 | "loop $name $op->pv", $opts); | ||
| 1480 | } elsif (B::class($op) eq "OP") { | ||
| 1481 | # no-op | ||
| 1482 | return info_from_text($op, $self, $name, "loopex $name", $opts); | ||
| 1483 | } elsif (B::class($op) eq "UNOP") { | ||
| 1484 | (my $kid_info = $self->deparse($op->first, 7)) =~ s/^\cS//; | ||
| 1485 | # last foo() is a syntax error. So we might surround it with parens. | ||
| 1486 | my $transform_fn = sub { | ||
| 1487 | my $text = shift->{text}; | ||
| 1488 | $text = "($text)" if $text =~ /^(?!\d)\w/; | ||
| 1489 | return $text; | ||
| 1490 | }; | ||
| 1491 | return $self->info_from_template("loop $name", $op, "$name %F", | ||
| 1492 | undef, [$kid_info], $opts); | ||
| 1493 | } else { | ||
| 1494 | return info_from_text($op, $self, $name, "loop $name", $opts); | ||
| 1495 | } | ||
| 1496 | Carp::confess("unhandled condition in lopex"); | ||
| 1497 | } | ||
| 1498 | |||
| 1499 | # Logical assignment operations, e.g. ||= &&=, //= | ||
| 1500 | sub logassignop | ||
| 1501 | { | ||
| 1502 | my ($self, $op, $cx, $opname) = @_; | ||
| 1503 | my $left_op = $op->first; | ||
| 1504 | |||
| 1505 | my $sassign_op = $left_op->sibling; | ||
| 1506 | my $right_op = $sassign_op->first; # skip sassign | ||
| 1507 | my $left_node = $self->deparse($left_op, 7, $op); | ||
| 1508 | my $right_node = $self->deparse($right_op, 7, $op); | ||
| 1509 | my $node = $self->info_from_template( | ||
| 1510 | "logical assign $opname", $op, | ||
| 1511 | "%c $opname %c", undef, [$left_node, $right_node], | ||
| 1512 | {other_ops => [$op->first->sibling], | ||
| 1513 | maybe_parens => [$self, $cx, 7]}); | ||
| 1514 | |||
| 1515 | # Handle skipped sassign | ||
| 1516 | my $str = $node->{text}; | ||
| 1517 | my $position = [length($left_node->{text})+1, length($opname)]; | ||
| 1518 | my $new_op = $self->info_from_string($sassign_op->name, $sassign_op, $str, | ||
| 1519 | {position => $position}); | ||
| 1520 | $node->{other_ops} = [$new_op]; | ||
| 1521 | return $node; | ||
| 1522 | |||
| 1523 | } | ||
| 1524 | |||
| 1525 | sub mapop | ||
| 1526 | { | ||
| 1527 | my($self, $op, $cx, $name) = @_; | ||
| 1528 | my $kid = $op->first; # this is the (map|grep)start | ||
| 1529 | |||
| 1530 | my @skipped_ops = ($kid, $kid->first); | ||
| 1531 | $kid = $kid->first->sibling; # skip a pushmark | ||
| 1532 | |||
| 1533 | my $code_block = $kid->first; # skip a null | ||
| 1534 | |||
| 1535 | my ($code_block_node, @nodes); | ||
| 1536 | my ($fmt, $first_arg_fmt, $is_block); | ||
| 1537 | my $type = "map $name"; | ||
| 1538 | my @args_spec = (); | ||
| 1539 | |||
| 1540 | if (B::Deparse::is_scope $code_block) { | ||
| 1541 | $code_block_node = $self->deparse($code_block, 0, $op); | ||
| 1542 | my $transform_fn = sub { | ||
| 1543 | # remove first \n in block. | ||
| 1544 | ($_[0]->{text})=~ s/^\n\s*//; | ||
| 1545 | return $_[0]->{text}; | ||
| 1546 | }; | ||
| 1547 | push @args_spec, [0, $transform_fn]; | ||
| 1548 | $first_arg_fmt = '{ %F }'; | ||
| 1549 | |||
| 1550 | ## Alternate simpler form: | ||
| 1551 | # push @args_spec, 0; | ||
| 1552 | # $first_arg_fmt = '{ %c }'; | ||
| 1553 | $type .= " block"; | ||
| 1554 | $is_block = 1; | ||
| 1555 | |||
| 1556 | } else { | ||
| 1557 | $code_block_node = $self->deparse($code_block, 24, $op); | ||
| 1558 | push @args_spec, 0; | ||
| 1559 | $first_arg_fmt = '%c'; | ||
| 1560 | $type .= " expr"; | ||
| 1561 | $is_block = 0; | ||
| 1562 | } | ||
| 1563 | push @nodes, $code_block_node; | ||
| 1564 | |||
| 1565 | |||
| 1566 | push @skipped_ops, $kid; | ||
| 1567 | $kid = $kid->sibling; | ||
| 1568 | $self->deparse_op_siblings(\@nodes, $kid, $op, 6); | ||
| 1569 | push @args_spec, [1, $#nodes, ', ']; | ||
| 1570 | |||
| 1571 | if ($self->func_needs_parens($nodes[1]->{text}, $cx, 5)) { | ||
| 1572 | $fmt = "$name $first_arg_fmt (%C)"; | ||
| 1573 | } else { | ||
| 1574 | $fmt = "$name $first_arg_fmt %C"; | ||
| 1575 | } | ||
| 1576 | my $node = $self->info_from_template($type, $op, $fmt, | ||
| 1577 | \@args_spec, \@nodes, | ||
| 1578 | {other_ops => \@skipped_ops}); | ||
| 1579 | |||
| 1580 | # Handle skipped ops | ||
| 1581 | my @new_ops; | ||
| 1582 | my $str = $node->{text}; | ||
| 1583 | my $position; | ||
| 1584 | if ($is_block) { | ||
| 1585 | # Make the position be the position of the "{". | ||
| 1586 | $position = [length($name)+1, 1]; | ||
| 1587 | } else { | ||
| 1588 | # Make the position be the name portion | ||
| 1589 | $position = [0, length($name)]; | ||
| 1590 | } | ||
| 1591 | my @skipped_nodes; | ||
| 1592 | for my $skipped_op (@skipped_ops) { | ||
| 1593 | my $new_op = $self->info_from_string($op->name, $skipped_op, $str, | ||
| 1594 | {position => $position}); | ||
| 1595 | push @new_ops, $new_op; | ||
| 1596 | } | ||
| 1597 | $node->{other_ops} = \@new_ops; | ||
| 1598 | return $node; | ||
| 1599 | } | ||
| 1600 | |||
| 1601 | |||
| 1602 | # osmic acid -- see osmium tetroxide | ||
| 1603 | |||
| 1604 | my %matchwords; | ||
| 1605 | map($matchwords{join "", sort split //, $_} = $_, 'cig', 'cog', 'cos', 'cogs', | ||
| 1606 | 'cox', 'go', 'is', 'ism', 'iso', 'mig', 'mix', 'osmic', 'ox', 'sic', | ||
| 1607 | 'sig', 'six', 'smog', 'so', 'soc', 'sog', 'xi'); | ||
| 1608 | |||
| 1609 | sub matchop | ||
| 1610 | { | ||
| 1611 | $] < 5.022 ? matchop_older(@_) : matchop_newer(@_); | ||
| 1612 | } | ||
| 1613 | |||
| 1614 | # matchop for Perl 5.22 and later | ||
| 1615 | sub matchop_newer | ||
| 1616 | { | ||
| 1617 | my($self, $op, $cx, $name, $delim) = @_; | ||
| 1618 | my $kid = $op->first; | ||
| 1619 | my $info = {}; | ||
| 1620 | my @body = (); | ||
| 1621 | my ($binop, $var_str, $re_str) = ("", "", ""); | ||
| 1622 | my $var_node; | ||
| 1623 | my $re; | ||
| 1624 | if ($op->flags & B::OPf_STACKED) { | ||
| 1625 | $binop = 1; | ||
| 1626 | $var_node = $self->deparse($kid, 20, $op); | ||
| 1627 | $var_str = $var_node->{text}; | ||
| 1628 | push @body, $var_node; | ||
| 1629 | $kid = $kid->sibling; | ||
| 1630 | } | ||
| 1631 | # not $name; $name will be 'm' for both match and split | ||
| 1632 | elsif ($op->name eq 'match' and my $targ = $op->targ) { | ||
| 1633 | $binop = 1; | ||
| 1634 | $var_str = $self->padname($targ); | ||
| 1635 | } | ||
| 1636 | my $quote = 1; | ||
| 1637 | my $pmflags = $op->pmflags; | ||
| 1638 | my $rhs_bound_to_defsv; | ||
| 1639 | my ($cv, $bregexp); | ||
| 1640 | my $have_kid = !B::Deparse::null $kid; | ||
| 1641 | # Check for code blocks first | ||
| 1642 | if (not B::Deparse::null my $code_list = $op->code_list) { | ||
| 1643 | $re = $self->code_list($code_list, | ||
| 1644 | $op->name eq 'qr' | ||
| 1645 | ? $self->padval( | ||
| 1646 | $kid->first # ex-list | ||
| 1647 | ->first # pushmark | ||
| 1648 | ->sibling # entersub | ||
| 1649 | ->first # ex-list | ||
| 1650 | ->first # pushmark | ||
| 1651 | ->sibling # srefgen | ||
| 1652 | ->first # ex-list | ||
| 1653 | ->first # anoncode | ||
| 1654 | ->targ | ||
| 1655 | ) | ||
| 1656 | : undef); | ||
| 1657 | } elsif (${$bregexp = $op->pmregexp} && ${$cv = $bregexp->qr_anoncv}) { | ||
| 1658 | my $patop = $cv->ROOT # leavesub | ||
| 1659 | ->first # qr | ||
| 1660 | ->code_list;# list | ||
| 1661 | $re = $self->code_list($patop, $cv); | ||
| 1662 | } elsif (!$have_kid) { | ||
| 1663 | $re_str = B::Deparse::re_uninterp(B::Deparse::escape_str(B::Deparse::re_unback($op->precomp))); | ||
| 1664 | } elsif ($kid->name ne 'regcomp') { | ||
| 1665 | if ($op->name eq 'split') { | ||
| 1666 | # split has other kids, not just regcomp | ||
| 1667 | $re = re_uninterp(escape_re(re_unback($op->precomp))); | ||
| 1668 | } else { | ||
| 1669 | carp("found ".$kid->name." where regcomp expected"); | ||
| 1670 | } | ||
| 1671 | } else { | ||
| 1672 | ($re, $quote) = $self->regcomp($kid, 21); | ||
| 1673 | push @body, $re; | ||
| 1674 | $re_str = $re->{text}; | ||
| 1675 | my $matchop = $kid->first; | ||
| 1676 | if ($matchop->name eq 'regcrest') { | ||
| 1677 | $matchop = $matchop->first; | ||
| 1678 | } | ||
| 1679 | if ($matchop->name =~ /^(?:match|transr?|subst)\z/ | ||
| 1680 | && $matchop->flags & B::OPf_SPECIAL) { | ||
| 1681 | $rhs_bound_to_defsv = 1; | ||
| 1682 | } | ||
| 1683 | } | ||
| 1684 | my $flags = ''; | ||
| 1685 | $flags .= "c" if $pmflags & B::PMf_CONTINUE; | ||
| 1686 | $flags .= $self->re_flags($op); | ||
| 1687 | $flags = join '', sort split //, $flags; | ||
| 1688 | $flags = $matchwords{$flags} if $matchwords{$flags}; | ||
| 1689 | |||
| 1690 | if ($pmflags & B::PMf_ONCE) { | ||
| 1691 | # only one kind of delimiter works here | ||
| 1692 | $re_str =~ s/\?/\\?/g; | ||
| 1693 | # explicit 'm' is required | ||
| 1694 | $re_str = $self->keyword("m") . "?$re_str?"; | ||
| 1695 | } elsif ($quote) { | ||
| 1696 | my $re = $self->single_delim($kid, $name, $delim, $re_str); | ||
| 1697 | push @body, $re; | ||
| 1698 | $re_str = $re->{text}; | ||
| 1699 | } | ||
| 1700 | my $opts = {}; | ||
| 1701 | my @texts; | ||
| 1702 | $re_str .= $flags if $quote; | ||
| 1703 | my $type; | ||
| 1704 | if ($binop) { | ||
| 1705 | # FIXME: use template string | ||
| 1706 | if ($rhs_bound_to_defsv) { | ||
| 1707 | @texts = ($var_str, ' =~ ($_ =~ ', $re_str, ')'); | ||
| 1708 | } else { | ||
| 1709 | @texts = ($var_str, ' =~ ', $re_str); | ||
| 1710 | } | ||
| 1711 | $opts->{maybe_parens} = [$self, $cx, 20]; | ||
| 1712 | $type = 'binary match ~='; | ||
| 1713 | } else { | ||
| 1714 | @texts = ($re_str); | ||
| 1715 | $type = 'unary ($_) match'; | ||
| 1716 | } | ||
| 1717 | return info_from_list($op, $self, \@texts, '', $type, $opts); | ||
| 1718 | } | ||
| 1719 | |||
| 1720 | # matchop for Perl before 5.22 | ||
| 1721 | sub matchop_older | ||
| 1722 | { | ||
| 1723 | my($self, $op, $cx, $name, $delim) = @_; | ||
| 1724 | my $kid = $op->first; | ||
| 1725 | my $info = {}; | ||
| 1726 | my @body = (); | ||
| 1727 | my ($binop, $var, $re_str) = ("", "", ""); | ||
| 1728 | my $re; | ||
| 1729 | if ($op->flags & B::OPf_STACKED) { | ||
| 1730 | $binop = 1; | ||
| 1731 | $var = $self->deparse($kid, 20, $op); | ||
| 1732 | push @body, $var; | ||
| 1733 | $kid = $kid->sibling; | ||
| 1734 | } | ||
| 1735 | my $quote = 1; | ||
| 1736 | my $pmflags = $op->pmflags; | ||
| 1737 | my $extended = ($pmflags & B::PMf_EXTENDED); | ||
| 1738 | my $rhs_bound_to_defsv; | ||
| 1739 | if (B::Deparse::null $kid) { | ||
| 1740 | my $unbacked = B::Deparse::re_unback($op->precomp); | ||
| 1741 | if ($extended) { | ||
| 1742 | $re_str = B::Deparse::re_uninterp_extended(B::Deparse::escape_extended_re($unbacked)); | ||
| 1743 | } else { | ||
| 1744 | $re_str = B::Deparse::re_uninterp(B::Deparse::escape_str(B::Deparse::re_unback($op->precomp))); | ||
| 1745 | } | ||
| 1746 | } elsif ($kid->name ne 'regcomp') { | ||
| 1747 | carp("found ".$kid->name." where regcomp expected"); | ||
| 1748 | } else { | ||
| 1749 | ($re, $quote) = $self->regcomp($kid, 21, $extended); | ||
| 1750 | push @body, $re; | ||
| 1751 | $re_str = $re->{text}; | ||
| 1752 | my $matchop = $kid->first; | ||
| 1753 | if ($matchop->name eq 'regcrest') { | ||
| 1754 | $matchop = $matchop->first; | ||
| 1755 | } | ||
| 1756 | if ($matchop->name =~ /^(?:match|transr?|subst)\z/ | ||
| 1757 | && $matchop->flags & B::OPf_SPECIAL) { | ||
| 1758 | $rhs_bound_to_defsv = 1; | ||
| 1759 | } | ||
| 1760 | } | ||
| 1761 | my $flags = ''; | ||
| 1762 | $flags .= "c" if $pmflags & B::PMf_CONTINUE; | ||
| 1763 | $flags .= $self->re_flags($op); | ||
| 1764 | $flags = join '', sort split //, $flags; | ||
| 1765 | $flags = $matchwords{$flags} if $matchwords{$flags}; | ||
| 1766 | |||
| 1767 | if ($pmflags & B::PMf_ONCE) { # only one kind of delimiter works here | ||
| 1768 | $re_str =~ s/\?/\\?/g; | ||
| 1769 | $re_str = "?$re_str?"; | ||
| 1770 | } elsif ($quote) { | ||
| 1771 | my $re = $self->single_delim($kid, $name, $delim, $re_str); | ||
| 1772 | push @body, $re; | ||
| 1773 | $re_str = $re->{text}; | ||
| 1774 | } | ||
| 1775 | my $opts = {body => \@body}; | ||
| 1776 | my @texts; | ||
| 1777 | $re_str .= $flags if $quote; | ||
| 1778 | my $type; | ||
| 1779 | if ($binop) { | ||
| 1780 | if ($rhs_bound_to_defsv) { | ||
| 1781 | @texts = ($var->{text}, ' =~ ', "(", '$_', ' =~ ', $re_str, ')'); | ||
| 1782 | } else { | ||
| 1783 | @texts = ($var->{text}, ' =~ ', $re_str); | ||
| 1784 | } | ||
| 1785 | $opts->{maybe_parens} = [$self, $cx, 20]; | ||
| 1786 | $type = 'matchop_binop'; | ||
| 1787 | } else { | ||
| 1788 | @texts = ($re_str); | ||
| 1789 | $type = 'matchop_unnop'; | ||
| 1790 | } | ||
| 1791 | return info_from_list($op, $self, \@texts, '', $type, $opts); | ||
| 1792 | } | ||
| 1793 | |||
| 1794 | # FIXME: remove this | ||
| 1795 | sub map_texts($$) | ||
| 1796 | { | ||
| 1797 | my ($self, $args) = @_; | ||
| 1798 | my @result ; | ||
| 1799 | foreach my $expr (@$args) { | ||
| 1800 | if (ref $expr eq 'ARRAY' and scalar(@$expr) == 2) { | ||
| 1801 | # First item is hash and second item is op address. | ||
| 1802 | push @result, [$expr->[0]{text}, $expr->[1]]; | ||
| 1803 | } else { | ||
| 1804 | push @result, [$expr->{text}, $expr->{addr}]; | ||
| 1805 | } | ||
| 1806 | } | ||
| 1807 | return @result; | ||
| 1808 | } | ||
| 1809 | |||
| 1810 | # FIXME: This is weird. Regularize var_info | ||
| 1811 | sub maybe_local { | ||
| 1812 | my($self, $op, $cx, $var_info) = @_; | ||
| 1813 | $var_info->{parent} = $$op; | ||
| 1814 | return maybe_local_str($self, $op, $cx, $var_info->{text}); | ||
| 1815 | } | ||
| 1816 | |||
| 1817 | # Handles "our", "local", "my" variables (and possibly no | ||
| 1818 | # declaration of these) in scalar and array contexts. | ||
| 1819 | # The complications include stripping a package name on | ||
| 1820 | # "our" variables, and not including parenthesis when | ||
| 1821 | # not needed, unless there's a setting to always include | ||
| 1822 | # parenthesis. | ||
| 1823 | |||
| 1824 | sub maybe_local_str | ||
| 1825 | { | ||
| 1826 | my($self, $op, $cx, $info) = @_; | ||
| 1827 | my ($text, $is_node); | ||
| 1828 | if (ref $info && $info->isa("B::DeparseTree::Node")) { | ||
| 1829 | $text = $self->info2str($info); | ||
| 1830 | $is_node = 1; | ||
| 1831 | } else { | ||
| 1832 | $text = $info; | ||
| 1833 | $is_node = 0; | ||
| 1834 | } | ||
| 1835 | |||
| 1836 | my $our_intro = ($op->name =~ /^(gv|rv2)[ash]v$/) ? OPpOUR_INTRO : 0; | ||
| 1837 | my ($fmt, $type); | ||
| 1838 | if ($op->private & (OPpLVAL_INTRO|$our_intro) | ||
| 1839 | and not $self->{'avoid_local'}{$$op}) { | ||
| 1840 | my $our_local = ($op->private & OPpLVAL_INTRO) ? "local" : "our"; | ||
| 1841 | if( $our_local eq 'our' ) { | ||
| 1842 | # "our" variables needs to strip off package the prefix | ||
| 1843 | |||
| 1844 | if ( $text !~ /^\W(\w+::)*\w+\z/ | ||
| 1845 | and !utf8::decode($text) || $text !~ /^\W(\w+::)*\w+\z/ | ||
| 1846 | ) { | ||
| 1847 | Carp::confess("Unexpected our text $text"); | ||
| 1848 | } | ||
| 1849 | |||
| 1850 | if ($] >= 5.024) { | ||
| 1851 | if ($type = $self->B::Deparse::find_our_type($text)) { | ||
| 1852 | $our_local .= ' ' . $type; | ||
| 1853 | } | ||
| 1854 | } | ||
| 1855 | |||
| 1856 | if (!B::Deparse::want_scalar($op) | ||
| 1857 | && $self->func_needs_parens($text, $cx, 16)) { | ||
| 1858 | $type = "$our_local ()"; | ||
| 1859 | $fmt = "$our_local(%F)"; | ||
| 1860 | } else { | ||
| 1861 | $type = "$our_local"; | ||
| 1862 | $fmt = "$our_local %F"; | ||
| 1863 | } | ||
| 1864 | my $transform_fn = sub { | ||
| 1865 | my $text = $is_node ? $_[0]->{text} : $_[0]; | ||
| 1866 | # Strip possible package prefix | ||
| 1867 | $text =~ s/(\w+::)+//; | ||
| 1868 | return $text; | ||
| 1869 | }; | ||
| 1870 | # $info could be either a string or a node, %c covers both. | ||
| 1871 | return $self->info_from_template($type, $op, $fmt, | ||
| 1872 | [[0, $transform_fn]], [$info]); | ||
| 1873 | } | ||
| 1874 | |||
| 1875 | # Not an "our" declaration. | ||
| 1876 | if (B::Deparse::want_scalar($op)) { | ||
| 1877 | # $info could be either a string or a node, %c covers both | ||
| 1878 | return $self->info_from_template("scalar $our_local", $op, "$our_local %c", undef, [$info]); | ||
| 1879 | } else { | ||
| 1880 | if (!B::Deparse::want_scalar($op) | ||
| 1881 | && $self->func_needs_parens($text, $cx, 16)) { | ||
| 1882 | $fmt = "$our_local(%F)"; | ||
| 1883 | $type = "$our_local()"; | ||
| 1884 | } else { | ||
| 1885 | $fmt = "$our_local %F"; | ||
| 1886 | $type = "$our_local"; | ||
| 1887 | } | ||
| 1888 | return $self->info_from_template($type, $op, $fmt, undef, [$info]); | ||
| 1889 | } | ||
| 1890 | } else { | ||
| 1891 | if (ref $info && $info->isa("B::DeparseTree::Node")) { | ||
| 1892 | return $info; | ||
| 1893 | } else { | ||
| 1894 | return $self->info_from_string('not local', $op, $text); | ||
| 1895 | } | ||
| 1896 | } | ||
| 1897 | } | ||
| 1898 | |||
| 1899 | sub maybe_my { | ||
| 1900 | my $self = shift; | ||
| 1901 | my($op, $cx, $text, $forbid_parens) = @_; | ||
| 1902 | if ($op->private & OPpLVAL_INTRO and not $self->{'avoid_local'}{$$op}) { | ||
| 1903 | my $my_str = $op->private & OPpPAD_STATE | ||
| 1904 | ? $self->keyword("state") | ||
| 1905 | : "my"; | ||
| 1906 | if ($forbid_parens || B::Deparse::want_scalar($op)) { | ||
| 1907 | return $self->info_from_string('my', $op, "$my_str $text"); | ||
| 1908 | } else { | ||
| 1909 | return $self->info_from_string('my (maybe with parens)', $op, | ||
| 1910 | "$my_str $text", | ||
| 1911 | {maybe_parens => [$self, $cx, 16]}); | ||
| 1912 | } | ||
| 1913 | } else { | ||
| 1914 | return $self->info_from_string('not my', $op, $text); | ||
| 1915 | } | ||
| 1916 | } | ||
| 1917 | |||
| 1918 | # Possibly add () around $text depending on precedence $prec and | ||
| 1919 | # context $cx. We return a string. | ||
| 1920 | sub maybe_parens($$$$) | ||
| 1921 | { | ||
| 1922 | my($self, $text, $cx, $prec) = @_; | ||
| 1923 | if (B::DeparseTree::Node::parens_test($self, $cx, $prec)) { | ||
| 1924 | $text = "($text)"; | ||
| 1925 | # In a unop, let parent reuse our parens; see maybe_parens_unop | ||
| 1926 | # FIXME: | ||
| 1927 | $text = "\cS" . $text if $cx == 16; | ||
| 1928 | return $text; | ||
| 1929 | } else { | ||
| 1930 | return $text; | ||
| 1931 | } | ||
| 1932 | } | ||
| 1933 | |||
| 1934 | # FIXME: go back to default B::Deparse routine and return a string. | ||
| 1935 | sub maybe_parens_func($$$$$) | ||
| 1936 | { | ||
| 1937 | my($self, $func, $params, $cx, $prec) = @_; | ||
| 1938 | if ($prec <= $cx or substr($params, 0, 1) eq "(" or $self->{'parens'}) { | ||
| 1939 | return ($func, '(', $params, ')'); | ||
| 1940 | } else { | ||
| 1941 | return ($func, ' ', $params); | ||
| 1942 | } | ||
| 1943 | } | ||
| 1944 | |||
| 1945 | # Sort of like maybe_parens in that we may possibly add (). However we take | ||
| 1946 | # an op rather than text, and return a tree node. Also, we get around | ||
| 1947 | # the 'if it looks like a function' rule. | ||
| 1948 | sub maybe_parens_unop($$$$$) | ||
| 1949 | { | ||
| 1950 | my $self = shift; | ||
| 1951 | my($name, $op, $cx, $parent) = @_; | ||
| 1952 | my $info = $self->deparse($op, 1, $parent); | ||
| 1953 | my $fmt; | ||
| 1954 | my @exprs = ($info); | ||
| 1955 | if ($name eq "umask" && $info->{text} =~ /^\d+$/) { | ||
| 1956 | # Display umask numbers in octal. | ||
| 1957 | # FIXME: add as a info_node option to run a transformation function | ||
| 1958 | # such as the below | ||
| 1959 | $info->{text} = sprintf("%#o", $info->{text}); | ||
| 1960 | $exprs[0] = $info; | ||
| 1961 | } | ||
| 1962 | $name = $self->keyword($name); | ||
| 1963 | if ($cx > 16 or $self->{'parens'}) { | ||
| 1964 | return $self->info_from_template("$name()", $op, | ||
| 1965 | "$name(%c)",[0], \@exprs); | ||
| 1966 | } else { | ||
| 1967 | # FIXME: we don't do \cS | ||
| 1968 | # if (substr($text, 0, 1) eq "\cS") { | ||
| 1969 | # # use op's parens | ||
| 1970 | # return info_from_list($op, $self,[$name, substr($text, 1)], | ||
| 1971 | # '', 'maybe_parens_unop_cS', {body => [$info]}); | ||
| 1972 | # } else | ||
| 1973 | if (substr($info->{text}, 0, 1) eq "(") { | ||
| 1974 | # avoid looks-like-a-function trap with extra parens | ||
| 1975 | # ('+' can lead to ambiguities) | ||
| 1976 | return $self->info_from_template("$name(())", $op, | ||
| 1977 | "$name(%c)", [0], \@exprs); | ||
| 1978 | } else { | ||
| 1979 | return $self->info_from_template("$name <args>", $op, | ||
| 1980 | "$name %c", [0], \@exprs); | ||
| 1981 | } | ||
| 1982 | } | ||
| 1983 | Carp::confess("unhandled condition in maybe_parens_unop"); | ||
| 1984 | } | ||
| 1985 | |||
| 1986 | sub maybe_qualify { | ||
| 1987 | my ($self,$prefix,$name) = @_; | ||
| 1988 | my $v = ($prefix eq '$#' ? '@' : $prefix) . $name; | ||
| 1989 | return $name if !$prefix || $name =~ /::/; | ||
| 1990 | return $self->{'curstash'}.'::'. $name | ||
| 1991 | if | ||
| 1992 | $name =~ /^(?!\d)\w/ # alphabetic | ||
| 1993 | && $v !~ /^\$[ab]\z/ # not $a or $b | ||
| 1994 | && !$globalnames{$name} # not a global name | ||
| 1995 | && $self->{hints} & $strict_bits{vars} # strict vars | ||
| 1996 | && !$self->lex_in_scope($v,1) # no "our" | ||
| 1997 | or $self->lex_in_scope($v); # conflicts with "my" variable | ||
| 1998 | return $name; | ||
| 1999 | } | ||
| 2000 | |||
| 2001 | # FIXME: need a way to pass in skipped_ops | ||
| 2002 | sub maybe_targmy | ||
| 2003 | { | ||
| 2004 | my($self, $op, $cx, $func, @args) = @_; | ||
| 2005 | if ($op->private & OPpTARGET_MY) { | ||
| 2006 | my $var = $self->padname($op->targ); | ||
| 2007 | my $val = $func->($self, $op, 7, @args); | ||
| 2008 | my @texts = ($var, '=', $val); | ||
| 2009 | return $self->info_from_template("my", $op, | ||
| 2010 | "%c = %c", [0, 1], | ||
| 2011 | [$var, $val], | ||
| 2012 | {maybe_parens => [$self, $cx, 7]}); | ||
| 2013 | } else { | ||
| 2014 | return $self->$func($op, $cx, @args); | ||
| 2015 | } | ||
| 2016 | } | ||
| 2017 | |||
| 2018 | sub null_older | ||
| 2019 | { | ||
| 2020 | my($self, $op, $cx) = @_; | ||
| 2021 | my $info; | ||
| 2022 | if (B::class($op) eq "OP") { | ||
| 2023 | if ($op->targ == B::Deparse::OP_CONST) { | ||
| 2024 | # The Perl source constant value can't be recovered. | ||
| 2025 | # We'll use the 'ex_const' value as a substitute | ||
| 2026 | return $self->info_from_string('constant unrecoverable', $op, $self->{'ex_const'}); | ||
| 2027 | } else { | ||
| 2028 | # FIXME: look over. Is this right? | ||
| 2029 | return $self->info_from_string('constant ""', $op, ''); | ||
| 2030 | } | ||
| 2031 | } elsif (B::class ($op) eq "COP") { | ||
| 2032 | return $self->cops($op, $cx, $op->name); | ||
| 2033 | } | ||
| 2034 | my $kid = $op->first; | ||
| 2035 | if ($self->is_list_older($kid)) { | ||
| 2036 | my $node = $self->pp_list($op, $cx); | ||
| 2037 | $node->update_other_ops($kid); | ||
| 2038 | return $node; | ||
| 2039 | } elsif ($kid->name eq "enter") { | ||
| 2040 | return $self->pp_leave($op, $cx); | ||
| 2041 | } elsif ($kid->name eq "leave") { | ||
| 2042 | return $self->pp_leave($kid, $cx); | ||
| 2043 | } elsif ($kid->name eq "scope") { | ||
| 2044 | return $self->pp_scope($kid, $cx); | ||
| 2045 | } elsif ($op->targ == B::Deparse::OP_STRINGIFY) { | ||
| 2046 | return $self->dquote($op, $cx); | ||
| 2047 | } elsif ($op->targ == B::Deparse::OP_GLOB) { | ||
| 2048 | my @other_ops = ($kid, $kid->first, $kid->first->first); | ||
| 2049 | my $info = $self->pp_glob( | ||
| 2050 | $kid # entersub | ||
| 2051 | ->first # ex-list | ||
| 2052 | ->first # pushmark | ||
| 2053 | ->sibling, # glob | ||
| 2054 | $cx | ||
| 2055 | ); | ||
| 2056 | push @{$info->{other_ops}}, @other_ops; | ||
| 2057 | return $info; | ||
| 2058 | } elsif (!B::Deparse::null($kid->sibling) and | ||
| 2059 | $kid->sibling->name eq "readline" and | ||
| 2060 | $kid->sibling->flags & OPf_STACKED) { | ||
| 2061 | my $lhs = $self->deparse($kid, 7, $op); | ||
| 2062 | my $rhs = $self->deparse($kid->sibling, 7, $kid); | ||
| 2063 | return $self->info_from_template("readline = ", $op, | ||
| 2064 | "%c = %c", undef, [$lhs, $rhs], | ||
| 2065 | {maybe_parens => [$self, $cx, 7], | ||
| 2066 | prev_expr => $rhs}); | ||
| 2067 | } elsif (!B::Deparse::null($kid->sibling) and | ||
| 2068 | $kid->sibling->name eq "trans" and | ||
| 2069 | $kid->sibling->flags & OPf_STACKED) { | ||
| 2070 | my $lhs = $self->deparse($kid, 20, $op); | ||
| 2071 | my $rhs = $self->deparse($kid->sibling, 20, $op); | ||
| 2072 | return $self->info_from_template("trans =~",$op, | ||
| 2073 | "%c =~ %c", undef, [$lhs, $rhs], | ||
| 2074 | { maybe_parens => [$self, $cx, 7], | ||
| 2075 | prev_expr => $rhs }); | ||
| 2076 | } elsif ($op->flags & OPf_SPECIAL && $cx < 1 && !$op->targ) { | ||
| 2077 | my $kid_info = $self->deparse($kid, $cx, $op); | ||
| 2078 | return $self->info_from_template("do { }", $op, | ||
| 2079 | "do {\n%+%c\n%-}", undef, [$kid_info]); | ||
| 2080 | } elsif (!B::Deparse::null($kid->sibling) and | ||
| 2081 | $kid->sibling->name eq "null" and | ||
| 2082 | B::class($kid->sibling) eq "UNOP" and | ||
| 2083 | $kid->sibling->first->flags & OPf_STACKED and | ||
| 2084 | $kid->sibling->first->name eq "rcatline") { | ||
| 2085 | my $lhs = $self->deparse($kid, 18, $op); | ||
| 2086 | my $rhs = $self->deparse($kid->sibling, 18, $op); | ||
| 2087 | return $self->info_from_template("rcatline =",$op, | ||
| 2088 | "%c = %c", undef, [$lhs, $rhs], | ||
| 2089 | { maybe_parens => [$self, $cx, 20], | ||
| 2090 | prev_expr => $rhs }); | ||
| 2091 | } else { | ||
| 2092 | return $self->deparse($kid, $cx, $op); | ||
| 2093 | } | ||
| 2094 | Carp::confess("unhandled condition in null"); | ||
| 2095 | } | ||
| 2096 | |||
| 2097 | sub pushmark_position($) { | ||
| 2098 | my ($node) = @_; | ||
| 2099 | my $l = undef; | ||
| 2100 | if ($node->{parens}) { | ||
| 2101 | return [0, 1]; | ||
| 2102 | } elsif (exists $node->{fmt}) { | ||
| 2103 | # Match up to %c, %C, or %F after ( or { | ||
| 2104 | if ($node->{fmt} =~ /^(.*)%[cCF]/) { | ||
| 2105 | $l = length($1); | ||
| 2106 | } | ||
| 2107 | } else { | ||
| 2108 | # Match up to first ( or { | ||
| 2109 | if ($node->{text} =~ /^(.*)\W/) { | ||
| 2110 | $l = length($1); | ||
| 2111 | } | ||
| 2112 | } | ||
| 2113 | if (defined($l)) { | ||
| 2114 | $l = $l > 0 ? $l-1 : 0; | ||
| 2115 | return [$l, 1] | ||
| 2116 | } | ||
| 2117 | return undef; | ||
| 2118 | } | ||
| 2119 | |||
| 2120 | |||
| 2121 | # Note 5.26 and up | ||
| 2122 | sub null_newer | ||
| 2123 | { | ||
| 2124 | my($self, $op, $cx) = @_; | ||
| 2125 | my $node; | ||
| 2126 | if (B::class($op) eq "OP") { | ||
| 2127 | # If the Perl source constant value can't be recovered. | ||
| 2128 | # We'll use the 'ex_const' value as a substitute | ||
| 2129 | return $self->info_from_string("constant_unrecoverable",$op, $self->{'ex_const'}) | ||
| 2130 | if $op->targ == B::Deparse::OP_CONST; | ||
| 2131 | return $self->dquote($op, $cx) if $op->targ == B::Deparse::OP_STRINGIFY; | ||
| 2132 | } elsif (B::class($op) eq "COP") { | ||
| 2133 | return $self->cops($op, $cx, $op->name); | ||
| 2134 | } else { | ||
| 2135 | # All of these use $kid | ||
| 2136 | my $kid = $op->first; | ||
| 2137 | my $update_node = $kid; | ||
| 2138 | if ($self->is_list_newer($op)) { | ||
| 2139 | $node = $self->pp_list($op, $cx); | ||
| 2140 | } elsif ($kid->name eq "enter") { | ||
| 2141 | $node = $self->pp_leave($op, $cx); | ||
| 2142 | } elsif ($kid->name eq "leave") { | ||
| 2143 | $node = $self->pp_leave($kid, $cx); | ||
| 2144 | } elsif ($kid->name eq "scope") { | ||
| 2145 | $node = $self->pp_scope($kid, $cx); | ||
| 2146 | } elsif ($op->targ == B::Deparse::OP_STRINGIFY) { | ||
| 2147 | # This case is duplicated the below "else". Can it ever happen? | ||
| 2148 | $node = $self->dquote($op, $cx); | ||
| 2149 | } elsif ($op->targ == B::Deparse::OP_GLOB) { | ||
| 2150 | my @other_ops = ($kid, $kid->first, $kid->first->first); | ||
| 2151 | my $info = $self->pp_glob( | ||
| 2152 | $kid # entersub | ||
| 2153 | ->first # ex-list | ||
| 2154 | ->first # pushmark | ||
| 2155 | ->sibling, # glob | ||
| 2156 | $cx | ||
| 2157 | ); | ||
| 2158 | # FIXME: mark text. | ||
| 2159 | push @{$info->{other_ops}}, @other_ops; | ||
| 2160 | return $info; | ||
| 2161 | } elsif (!B::Deparse::null($kid->sibling) and | ||
| 2162 | $kid->sibling->name eq "readline" and | ||
| 2163 | $kid->sibling->flags & OPf_STACKED) { | ||
| 2164 | my $lhs = $self->deparse($kid, 7, $op); | ||
| 2165 | my $rhs = $self->deparse($kid->sibling, 7, $kid); | ||
| 2166 | $node = $self->info_from_template("readline = ", $op, | ||
| 2167 | "%c = %c", undef, [$lhs, $rhs], | ||
| 2168 | {maybe_parens => [$self, $cx, 7], | ||
| 2169 | prev_expr => $rhs}); | ||
| 2170 | } elsif (!B::Deparse::null($kid->sibling) and | ||
| 2171 | $kid->sibling->name =~ /^transr?\z/ and | ||
| 2172 | $kid->sibling->flags & OPf_STACKED) { | ||
| 2173 | my $lhs = $self->deparse($kid, 20, $op); | ||
| 2174 | my $rhs = $self->deparse($kid->sibling, 20, $op); | ||
| 2175 | $node = $self->info_from_template("trans =~",$op, | ||
| 2176 | "%c =~ %c", undef, [$lhs, $rhs], | ||
| 2177 | { maybe_parens => [$self, $cx, 7], | ||
| 2178 | prev_expr => $rhs }); | ||
| 2179 | } elsif ($op->flags & OPf_SPECIAL && $cx < 1 && !$op->targ) { | ||
| 2180 | my $kid_info = $self->deparse($kid, $cx, $op); | ||
| 2181 | $node = $self->info_from_template("do { }", $op, | ||
| 2182 | "do {\n%+%c\n%-}", undef, [$kid_info]); | ||
| 2183 | } elsif (!B::Deparse::null($kid->sibling) and | ||
| 2184 | $kid->sibling->name eq "null" and | ||
| 2185 | B::class($kid->sibling) eq "UNOP" and | ||
| 2186 | $kid->sibling->first->flags & OPf_STACKED and | ||
| 2187 | $kid->sibling->first->name eq "rcatline") { | ||
| 2188 | my $lhs = $self->deparse($kid, 18, $op); | ||
| 2189 | my $rhs = $self->deparse($kid->sibling, 18, $op); | ||
| 2190 | $node = $self->info_from_template("rcatline =",$op, | ||
| 2191 | "%c = %c", undef, [$lhs, $rhs], | ||
| 2192 | { maybe_parens => [$self, $cx, 20], | ||
| 2193 | prev_expr => $rhs }); | ||
| 2194 | } else { | ||
| 2195 | my $node = $self->deparse($kid, $cx, $op); | ||
| 2196 | return $self->info_from_template($op->name, $op, | ||
| 2197 | "%c", undef, [$node]); | ||
| 2198 | } | ||
| 2199 | my $position = pushmark_position($node); | ||
| 2200 | if ($position) { | ||
| 2201 | $update_node = | ||
| 2202 | $self->info_from_string($kid->name, $kid, | ||
| 2203 | $node->{text}, | ||
| 2204 | {position => $position}); | ||
| 2205 | } | ||
| 2206 | $node->update_other_ops($update_node); | ||
| 2207 | return $node; | ||
| 2208 | } | ||
| 2209 | Carp::confess("unhandled condition in null"); | ||
| 2210 | } | ||
| 2211 | |||
| 2212 | # This is the 5.26 version. It is different from earlier versions. | ||
| 2213 | # Is it compatable/ | ||
| 2214 | # | ||
| 2215 | # 'x' is weird when the left arg is a list | ||
| 2216 | sub repeat { | ||
| 2217 | my $self = shift; | ||
| 2218 | my($op, $cx) = @_; | ||
| 2219 | my $left = $op->first; | ||
| 2220 | my $right = $op->last; | ||
| 2221 | my $eq = ""; | ||
| 2222 | my $prec = 19; | ||
| 2223 | my @skipped_ops = (); | ||
| 2224 | my $left_fmt; | ||
| 2225 | my $type = "repeat"; | ||
| 2226 | my @args_spec = (); | ||
| 2227 | my @exprs = (); | ||
| 2228 | if ($op->flags & OPf_STACKED) { | ||
| 2229 | $eq = "="; | ||
| 2230 | $prec = 7; | ||
| 2231 | } | ||
| 2232 | |||
| 2233 | if (B::Deparse::null($right)) { | ||
| 2234 | # This branch occurs in 5.21.5 and earlier. | ||
| 2235 | # A list repeat; count is inside left-side ex-list | ||
| 2236 | $type = 'list repeat'; | ||
| 2237 | |||
| 2238 | my $kid = $left->first->sibling; # skip pushmark | ||
| 2239 | push @skipped_ops, $left->first, $kid; | ||
| 2240 | $self->deparse_op_siblings(\@exprs, $kid, $op, 6); | ||
| 2241 | $left_fmt = '(%C)'; | ||
| 2242 | @args_spec = ([0, $#exprs, ', '], scalar(@exprs)); | ||
| 2243 | } else { | ||
| 2244 | $type = 'repeat'; | ||
| 2245 | my $dolist = $op->private & OPpREPEAT_DOLIST; | ||
| 2246 | push @exprs, $self->deparse_binop_left($op, $left, $dolist ? 1 : $prec); | ||
| 2247 | $left_fmt = '%c'; | ||
| 2248 | if ($dolist) { | ||
| 2249 | $left_fmt = "(%c)"; | ||
| 2250 | } | ||
| 2251 | @args_spec = (0, 1); | ||
| 2252 | } | ||
| 2253 | push @exprs, $self->deparse_binop_right($op, $right, $prec); | ||
| 2254 | my $opname = "x$eq"; | ||
| 2255 | my $node = $self->info_from_template("$type $opname", | ||
| 2256 | $op, "$left_fmt $opname %c", | ||
| 2257 | \@args_spec, | ||
| 2258 | \@exprs, | ||
| 2259 | {maybe_parens => [$self, $cx, $prec], | ||
| 2260 | other_ops => \@skipped_ops}); | ||
| 2261 | |||
| 2262 | if (@skipped_ops) { | ||
| 2263 | # if we have skipped ops like pushmark, we will use the position | ||
| 2264 | # of the "x" as the part it represents. | ||
| 2265 | my @new_ops; | ||
| 2266 | my $str = $node->{text}; | ||
| 2267 | my $right_text = "$opname " . $exprs[-1]->{text}; | ||
| 2268 | my $start = rindex($str, $right_text); | ||
| 2269 | my $position; | ||
| 2270 | if ($start >= 0) { | ||
| 2271 | $position = [$start, length($opname)]; | ||
| 2272 | } else { | ||
| 2273 | $position = [0, length($str)]; | ||
| 2274 | } | ||
| 2275 | my @skipped_nodes; | ||
| 2276 | for my $skipped_op (@skipped_ops) { | ||
| 2277 | my $new_op = $self->info_from_string($op->name, $skipped_op, $str, | ||
| 2278 | {position => $position}); | ||
| 2279 | push @new_ops, $new_op; | ||
| 2280 | } | ||
| 2281 | $node->{other_ops} = \@new_ops; | ||
| 2282 | } | ||
| 2283 | |||
| 2284 | return $node; | ||
| 2285 | } | ||
| 2286 | |||
| 2287 | # Kind of silly, but we prefer, subst regexp flags joined together to | ||
| 2288 | # make words. For example: s/a/b/xo => s/a/b/ox | ||
| 2289 | |||
| 2290 | # oxime -- any of various compounds obtained chiefly by the action of | ||
| 2291 | # hydroxylamine on aldehydes and ketones and characterized by the | ||
| 2292 | # bivalent grouping C=NOH [Webster's Tenth] | ||
| 2293 | |||
| 2294 | my %substwords; | ||
| 2295 | map($substwords{join "", sort split //, $_} = $_, 'ego', 'egoism', 'em', | ||
| 2296 | 'es', 'ex', 'exes', 'gee', 'go', 'goes', 'ie', 'ism', 'iso', 'me', | ||
| 2297 | 'meese', 'meso', 'mig', 'mix', 'os', 'ox', 'oxime', 'see', 'seem', | ||
| 2298 | 'seg', 'sex', 'sig', 'six', 'smog', 'sog', 'some', 'xi', 'rogue', | ||
| 2299 | 'sir', 'rise', 'smore', 'more', 'seer', 'rome', 'gore', 'grim', 'grime', | ||
| 2300 | 'or', 'rose', 'rosie'); | ||
| 2301 | |||
| 2302 | # FIXME 522 and 526 could probably be combined or common parts pulled out. | ||
| 2303 | sub subst_older | ||
| 2304 | { | ||
| 2305 | my($self, $op, $cx) = @_; | ||
| 2306 | my $kid = $op->first; | ||
| 2307 | my($binop, $var, $re, @other_ops) = ("", "", "", ()); | ||
| 2308 | my ($repl, $repl_info); | ||
| 2309 | |||
| 2310 | if ($op->flags & OPf_STACKED) { | ||
| 2311 | $binop = 1; | ||
| 2312 | $var = $self->deparse($kid, 20, $op); | ||
| 2313 | $kid = $kid->sibling; | ||
| 2314 | } | ||
| 2315 | my $flags = ""; | ||
| 2316 | my $pmflags = $op->pmflags; | ||
| 2317 | if (B::Deparse::null($op->pmreplroot)) { | ||
| 2318 | $repl = $kid; | ||
| 2319 | $kid = $kid->sibling; | ||
| 2320 | } else { | ||
| 2321 | push @other_ops, $op->pmreplroot; | ||
| 2322 | $repl = $op->pmreplroot->first; # skip substcont | ||
| 2323 | } | ||
| 2324 | while ($repl->name eq "entereval") { | ||
| 2325 | push @other_ops, $repl; | ||
| 2326 | $repl = $repl->first; | ||
| 2327 | $flags .= "e"; | ||
| 2328 | } | ||
| 2329 | { | ||
| 2330 | local $self->{in_subst_repl} = 1; | ||
| 2331 | if ($pmflags & PMf_EVAL) { | ||
| 2332 | $repl_info = $self->deparse($repl->first, 0, $repl); | ||
| 2333 | } else { | ||
| 2334 | $repl_info = $self->dq($repl); | ||
| 2335 | } | ||
| 2336 | } | ||
| 2337 | my $extended = ($pmflags & PMf_EXTENDED); | ||
| 2338 | if (B::Deparse::null $kid) { | ||
| 2339 | my $unbacked = B::Deparse::re_unback($op->precomp); | ||
| 2340 | if ($extended) { | ||
| 2341 | $re = B::Deparse::re_uninterp_extended(escape_extended_re($unbacked)); | ||
| 2342 | } | ||
| 2343 | else { | ||
| 2344 | $re = B::Deparse::re_uninterp(B::Deparse::escape_str($unbacked)); | ||
| 2345 | } | ||
| 2346 | } else { | ||
| 2347 | my ($re_info, $junk) = $self->regcomp($kid, 1, $extended); | ||
| 2348 | $re = $re_info->{text}; | ||
| 2349 | } | ||
| 2350 | $flags .= "r" if $pmflags & PMf_NONDESTRUCT; | ||
| 2351 | $flags .= "e" if $pmflags & PMf_EVAL; | ||
| 2352 | $flags .= $self->re_flags($op); | ||
| 2353 | $flags = join '', sort split //, $flags; | ||
| 2354 | $flags = $substwords{$flags} if $substwords{$flags}; | ||
| 2355 | my $core_s = $self->keyword("s"); # maybe CORE::s | ||
| 2356 | |||
| 2357 | # FIXME: we need to attach the $repl_info someplace. | ||
| 2358 | my $repl_text = $repl_info->{text}; | ||
| 2359 | my $find_replace_re = double_delim($re, $repl_text); | ||
| 2360 | my $opts = {}; | ||
| 2361 | $opts->{other_ops} = \@other_ops if @other_ops; | ||
| 2362 | if ($binop) { | ||
| 2363 | return $self->info_from_template("=~ s///", $op, | ||
| 2364 | "%c =~ ${core_s}%c$flags", | ||
| 2365 | undef, | ||
| 2366 | [$var, $find_replace_re], | ||
| 2367 | {maybe_parens => [$self, $cx, 20]}); | ||
| 2368 | } else { | ||
| 2369 | return $self->info_from_string("s///", $op, "${core_s}${find_replace_re}$flags"); | ||
| 2370 | } | ||
| 2371 | Carp::confess("unhandled condition in pp_subst"); | ||
| 2372 | } | ||
| 2373 | |||
| 2374 | sub slice | ||
| 2375 | { | ||
| 2376 | my ($self, $op, $cx, $left, $right, $regname, $padname) = @_; | ||
| 2377 | my $last; | ||
| 2378 | my(@elems, $kid, $array); | ||
| 2379 | if (B::class($op) eq "LISTOP") { | ||
| 2380 | $last = $op->last; | ||
| 2381 | } else { | ||
| 2382 | # ex-hslice inside delete() | ||
| 2383 | for ($kid = $op->first; !B::Deparse::null $kid->sibling; $kid = $kid->sibling) { | ||
| 2384 | $last = $kid; | ||
| 2385 | } | ||
| 2386 | } | ||
| 2387 | $array = $last; | ||
| 2388 | $array = $array->first | ||
| 2389 | if $array->name eq $regname or $array->name eq "null"; | ||
| 2390 | my $array_info = $self->elem_or_slice_array_name($array, $left, $padname, 0); | ||
| 2391 | $kid = $op->first->sibling; # skip pushmark | ||
| 2392 | |||
| 2393 | if ($kid->name eq "list") { | ||
| 2394 | # FIXME: | ||
| 2395 | # skip list, pushmark | ||
| 2396 | $kid = $kid->first->sibling; | ||
| 2397 | for (; !B::Deparse::null $kid; $kid = $kid->sibling) { | ||
| 2398 | push @elems, $self->deparse($kid, 6, $op); | ||
| 2399 | } | ||
| 2400 | } else { | ||
| 2401 | @elems = ($self->elem_or_slice_single_index($kid, $op)); | ||
| 2402 | } | ||
| 2403 | my $lead = '@'; | ||
| 2404 | $lead = '%' if $op->name =~ /^kv/i; | ||
| 2405 | my ($fmt, $args_spec); | ||
| 2406 | my (@texts, $type); | ||
| 2407 | if ($array_info) { | ||
| 2408 | unshift @elems, $array_info; | ||
| 2409 | $fmt = "${lead}%c$left%C$right"; | ||
| 2410 | $args_spec = [0, [1, $#elems, ', ']]; | ||
| 2411 | $type = "$lead<var>$left .. $right"; | ||
| 2412 | } else { | ||
| 2413 | $fmt = "${lead}$left%C$right"; | ||
| 2414 | $args_spec = [0, $#elems, ', ']; | ||
| 2415 | $type = "${lead}$left .. $right"; | ||
| 2416 | } | ||
| 2417 | return $self->info_from_template($type, $op, $fmt, $args_spec, | ||
| 2418 | \@elems), | ||
| 2419 | } | ||
| 2420 | |||
| 2421 | sub subst_newer | ||
| 2422 | { | ||
| 2423 | my($self, $op, $cx) = @_; | ||
| 2424 | my $kid = $op->first; | ||
| 2425 | my($binop, $var, $re, @other_ops) = ("", "", "", ()); | ||
| 2426 | my ($repl, $repl_info); | ||
| 2427 | |||
| 2428 | if ($op->flags & OPf_STACKED) { | ||
| 2429 | $binop = 1; | ||
| 2430 | $var = $self->deparse($kid, 20, $op); | ||
| 2431 | $kid = $kid->sibling; | ||
| 2432 | } | ||
| 2433 | elsif (my $targ = $op->targ) { | ||
| 2434 | $binop = 1; | ||
| 2435 | $var = $self->padname($targ); | ||
| 2436 | } | ||
| 2437 | my $flags = ""; | ||
| 2438 | my $pmflags = $op->pmflags; | ||
| 2439 | if (B::Deparse::null($op->pmreplroot)) { | ||
| 2440 | $repl = $kid; | ||
| 2441 | $kid = $kid->sibling; | ||
| 2442 | } else { | ||
| 2443 | push @other_ops, $op->pmreplroot; | ||
| 2444 | $repl = $op->pmreplroot->first; # skip substcont | ||
| 2445 | } | ||
| 2446 | while ($repl->name eq "entereval") { | ||
| 2447 | push @other_ops, $repl; | ||
| 2448 | $repl = $repl->first; | ||
| 2449 | $flags .= "e"; | ||
| 2450 | } | ||
| 2451 | { | ||
| 2452 | local $self->{in_subst_repl} = 1; | ||
| 2453 | if ($pmflags & PMf_EVAL) { | ||
| 2454 | $repl_info = $self->deparse($repl->first, 0, $repl); | ||
| 2455 | } else { | ||
| 2456 | $repl_info = $self->dq($repl); | ||
| 2457 | } | ||
| 2458 | } | ||
| 2459 | if (not B::Deparse::null my $code_list = $op->code_list) { | ||
| 2460 | $re = $self->code_list($code_list); | ||
| 2461 | } elsif (B::Deparse::null $kid) { | ||
| 2462 | $re = B::Deparse::re_uninterp(escape_re(B::Deparse::re_unback($op->precomp))); | ||
| 2463 | } else { | ||
| 2464 | my ($re_info, $junk) = $self->regcomp($kid, 1); | ||
| 2465 | $re = $re_info->{text}; | ||
| 2466 | } | ||
| 2467 | $flags .= "r" if $pmflags & PMf_NONDESTRUCT; | ||
| 2468 | $flags .= "e" if $pmflags & PMf_EVAL; | ||
| 2469 | $flags .= $self->re_flags($op); | ||
| 2470 | $flags = join '', sort split //, $flags; | ||
| 2471 | $flags = $substwords{$flags} if $substwords{$flags}; | ||
| 2472 | my $core_s = $self->keyword("s"); # maybe CORE::s | ||
| 2473 | |||
| 2474 | # FIXME: we need to attach the $repl_info someplace. | ||
| 2475 | my $repl_text = $repl_info->{text}; | ||
| 2476 | my $opts->{other_ops} = \@other_ops if @other_ops; | ||
| 2477 | my $find_replace_re = double_delim($re, $repl_text); | ||
| 2478 | |||
| 2479 | if ($binop) { | ||
| 2480 | return $self->info_from_template("=~ s///", $op, | ||
| 2481 | "%c =~ ${core_s}%c$flags", | ||
| 2482 | undef, | ||
| 2483 | [$var, $find_replace_re], | ||
| 2484 | {maybe_parens => [$self, $cx, 20]}); | ||
| 2485 | } else { | ||
| 2486 | return $self->info_from_string("s///", $op, "${core_s}${find_replace_re}$flags"); | ||
| 2487 | } | ||
| 2488 | Carp::confess("unhandled condition in pp_subst"); | ||
| 2489 | } | ||
| 2490 | |||
| 2491 | # This handles the category of unary operators, e.g. alarm(), caller(), | ||
| 2492 | # close().. | ||
| 2493 | sub unop | ||
| 2494 | { | ||
| 2495 | my($self, $op, $cx, $name, $nollafr) = @_; | ||
| 2496 | my $kid; | ||
| 2497 | if ($op->flags & B::OPf_KIDS) { | ||
| 2498 | $kid = $op->first; | ||
| 2499 | if (not $name) { | ||
| 2500 | # this deals with 'boolkeys' right now | ||
| 2501 | return $self->deparse($kid, $cx, $op); | ||
| 2502 | } | ||
| 2503 | my $builtinname = $name; | ||
| 2504 | $builtinname =~ /^CORE::/ or $builtinname = "CORE::$name"; | ||
| 2505 | if (defined prototype($builtinname) | ||
| 2506 | && $builtinname ne 'CORE::readline' | ||
| 2507 | && prototype($builtinname) =~ /^;?\*/ | ||
| 2508 | && $kid->name eq "rv2gv") { | ||
| 2509 | $kid = $kid->first; | ||
| 2510 | } | ||
| 2511 | |||
| 2512 | if ($nollafr) { | ||
| 2513 | $kid = $self->deparse($kid, 16, $op); | ||
| 2514 | my $opts = { | ||
| 2515 | maybe_parens => [$self, $cx, 16], | ||
| 2516 | }; | ||
| 2517 | my $fullname = $self->keyword($name); | ||
| 2518 | return $self->info_from_template("unary operator $name noallafr", $op, | ||
| 2519 | "$fullname %c", undef, [$kid], $opts); | ||
| 2520 | } | ||
| 2521 | return $self->maybe_parens_unop($name, $kid, $cx, $op); | ||
| 2522 | } else { | ||
| 2523 | my $opts = {maybe_parens => [$self, $cx, 16]}; | ||
| 2524 | my $fullname = ($self->keyword($name)); | ||
| 2525 | my $fmt = "$fullname"; | ||
| 2526 | $fmt .= '()' if $op->flags & B::OPf_SPECIAL; | ||
| 2527 | return $self->info_from_template("unary operator $name", $op, $fmt, | ||
| 2528 | undef, [], $opts); | ||
| 2529 | } | ||
| 2530 | } | ||
| 2531 | |||
| 2532 | # This handles category of symbolic prefix and postfix unary operators, | ||
| 2533 | # e.g $x++, -r, +$x. | ||
| 2534 | sub pfixop | ||
| 2535 | { | ||
| 2536 | my $self = shift; | ||
| 2537 | my($op, $cx, $operator, $prec, $flags) = (@_, 0); | ||
| 2538 | my $operand = $self->deparse($op->first, $prec, $op); | ||
| 2539 | my ($type, $fmt); | ||
| 2540 | my @nodes; | ||
| 2541 | if ($flags & POSTFIX) { | ||
| 2542 | @nodes = ($operand, $operator); | ||
| 2543 | $type = "prefix $operator"; | ||
| 2544 | $fmt = "%c%c"; | ||
| 2545 | } elsif ($operator eq '-' && $operand->{text} =~ /^[a-zA-Z](?!\w)/) { | ||
| 2546 | # Add () around operator to disambiguate with filetest operator | ||
| 2547 | @nodes = ($operator, $operand); | ||
| 2548 | $type = "prefix non-filetest $operator"; | ||
| 2549 | $fmt = "%c(%c)"; | ||
| 2550 | } else { | ||
| 2551 | @nodes = ($operator, $operand); | ||
| 2552 | $type = "postfix $operator"; | ||
| 2553 | $fmt = "%c%c"; | ||
| 2554 | } | ||
| 2555 | |||
| 2556 | return $self->info_from_template($type, $op, $fmt, [0, 1], | ||
| 2557 | \@nodes, | ||
| 2558 | {maybe_parens => [$self, $cx, $prec]}) ; | ||
| 2559 | } | ||
| 2560 | |||
| 2561 | # Produce an node for a range (".." or "..." op) | ||
| 2562 | sub range { | ||
| 2563 | my $self = shift; | ||
| 2564 | my ($op, $cx, $type) = @_; | ||
| 2565 | my $left = $op->first; | ||
| 2566 | my $right = $left->sibling; | ||
| 2567 | $left = $self->deparse($left, 9, $op); | ||
| 2568 | $right = $self->deparse($right, 9, $op); | ||
| 2569 | return $self->info_from_template("range $type", $op, "%c${type}%c", | ||
| 2570 | undef, [$left, $right], | ||
| 2571 | {maybe_parens => [$self, $cx, 9]}); | ||
| 2572 | } | ||
| 2573 | |||
| 2574 | sub rv2x | ||
| 2575 | { | ||
| 2576 | my($self, $op, $cx, $type) = @_; | ||
| 2577 | if (B::class($op) eq 'NULL' || !$op->can("first")) { | ||
| 2578 | carp("Unexpected op in pp_rv2x"); | ||
| 2579 | return info_from_text($op, $self, 'XXX', 'bad_rv2x', {}); | ||
| 2580 | } | ||
| 2581 | my ($info, $kid_info); | ||
| 2582 | my $kid = $op->first; | ||
| 2583 | $kid_info = $self->deparse($kid, 0, $op); | ||
| 2584 | if ($kid->name eq "gv") { | ||
| 2585 | my $transform_fn = sub {$self->stash_variable($type, $self->info2str(shift), $cx)}; | ||
| 2586 | return $self->info_from_template("rv2x $type", undef, "%F", [[0, $transform_fn]], [$kid_info]) | ||
| 2587 | } elsif (B::Deparse::is_scalar $kid) { | ||
| 2588 | my $str = $self->info2str($kid_info); | ||
| 2589 | my $fmt = '%c'; | ||
| 2590 | my @args_spec = (0); | ||
| 2591 | if ($str =~ /^\$([^\w\d])\z/) { | ||
| 2592 | # "$$+" isn't a legal way to write the scalar dereference | ||
| 2593 | # of $+, since the lexer can't tell you aren't trying to | ||
| 2594 | # do something like "$$ + 1" to get one more than your | ||
| 2595 | # PID. Either "${$+}" or "$${+}" are workable | ||
| 2596 | # disambiguations, but if the programmer did the former, | ||
| 2597 | # they'd be in the "else" clause below rather than here. | ||
| 2598 | # It's not clear if this should somehow be unified with | ||
| 2599 | # the code in dq and re_dq that also adds lexer | ||
| 2600 | # disambiguation braces. | ||
| 2601 | my $transform = sub { $_[0] =~ /^\$([^\w\d])\z/; '$' . "{$1}"}; | ||
| 2602 | $fmt = '%F'; | ||
| 2603 | @args_spec = (0, $transform); | ||
| 2604 | } | ||
| 2605 | return $self->info_from_template("scalar $str", $op, $fmt, \@args_spec, {}) | ||
| 2606 | } else { | ||
| 2607 | my $str = "$type" . '{}'; | ||
| 2608 | return info_from_text($op, $self, $str, $str, {other_ops => [$kid_info]}); | ||
| 2609 | } | ||
| 2610 | Carp::confess("unhandled condition in rv2x"); | ||
| 2611 | } | ||
| 2612 | |||
| 2613 | sub scopeop | ||
| 2614 | { | ||
| 2615 | my($real_block, $self, $op, $cx) = @_; | ||
| 2616 | my $kid; | ||
| 2617 | my @kids; | ||
| 2618 | |||
| 2619 | local(@$self{qw'curstash warnings hints hinthash'}) | ||
| 2620 | = @$self{qw'curstash warnings hints hinthash'} if $real_block; | ||
| 2621 | if ($real_block) { | ||
| 2622 | $kid = $op->first->sibling; # skip enter | ||
| 2623 | if (B::Deparse::is_miniwhile($kid)) { | ||
| 2624 | my $top = $kid->first; | ||
| 2625 | my $name = $top->name; | ||
| 2626 | if ($name eq "and") { | ||
| 2627 | $name = $self->keyword("while"); | ||
| 2628 | } elsif ($name eq "or") { | ||
| 2629 | $name = $self->keyword("until"); | ||
| 2630 | } else { # no conditional -> while 1 or until 0 | ||
| 2631 | my $body = $self->deparse($top->first, 1, $top); | ||
| 2632 | return info_from_list $op, $self, [$body, 'while', '1'], | ||
| 2633 | ' ', "$name 1", {}; | ||
| 2634 | } | ||
| 2635 | my $cond = $top->first; | ||
| 2636 | my $skipped_ops = [$cond->sibling]; | ||
| 2637 | my $body = $cond->sibling->first; # skip lineseq | ||
| 2638 | my $cond_info = $self->deparse($cond, 1, $top); | ||
| 2639 | my $body_info = $self->deparse($body, 1, $top); | ||
| 2640 | return info_from_list($op, $self, | ||
| 2641 | [$body_info, $name, $cond_info], ' ', | ||
| 2642 | "$name", | ||
| 2643 | {other_ops => $skipped_ops}); | ||
| 2644 | } | ||
| 2645 | } else { | ||
| 2646 | $kid = $op->first; | ||
| 2647 | } | ||
| 2648 | for (; !B::Deparse::null($kid); $kid = $kid->sibling) { | ||
| 2649 | push @kids, $kid; | ||
| 2650 | } | ||
| 2651 | if ($cx > 0) { | ||
| 2652 | # inside an expression, (a do {} while for lineseq) | ||
| 2653 | my $body = $self->lineseq($op, 0, @kids); | ||
| 2654 | my $text; | ||
| 2655 | if (B::Deparse::is_lexical_subs(@kids)) { | ||
| 2656 | return $self->info_from_template("scoped do", $op, | ||
| 2657 | 'do {\n%+%c\n%-}', | ||
| 2658 | [0], [$body]); | ||
| 2659 | |||
| 2660 | } else { | ||
| 2661 | return $self->info_from_template("scoped expression", $op, | ||
| 2662 | '%c',[0], [$body]); | ||
| 2663 | } | ||
| 2664 | } else { | ||
| 2665 | return $self->lineseq($op, $cx, @kids); | ||
| 2666 | } | ||
| 2667 | } | ||
| 2668 | |||
| 2669 | sub single_delim($$$$$) { | ||
| 2670 | my($self, $op, $q, $default, $str) = @_; | ||
| 2671 | |||
| 2672 | return $self->info_from_template("string $default .. $default (default)", $op, | ||
| 2673 | "$default%c$default", [0], | ||
| 2674 | [$str]) | ||
| 2675 | if $default and index($str, $default) == -1; | ||
| 2676 | my $coreq = $self->keyword($q); # maybe CORE::q | ||
| 2677 | if ($q ne 'qr') { | ||
| 2678 | (my $succeed, $str) = balanced_delim($str); | ||
| 2679 | return $self->info_from_string("string $q", $op, "$coreq$str") | ||
| 2680 | if $succeed; | ||
| 2681 | } | ||
| 2682 | for my $delim ('/', '"', '#') { | ||
| 2683 | $self->info_from_string("string $q $delim$delim", $op, "qr$delim$str$delim") | ||
| 2684 | if index($str, $delim) == -1; | ||
| 2685 | } | ||
| 2686 | if ($default) { | ||
| 2687 | my $transform_fn = sub { | ||
| 2688 | s/$_[0]/\\$_[0]/g; | ||
| 2689 | return $_[0]; | ||
| 2690 | }; | ||
| 2691 | |||
| 2692 | return $self->info_from_template("string $q $default$default", | ||
| 2693 | $op, "$default%F$default", | ||
| 2694 | [[0, $transform_fn]], [$str]); | ||
| 2695 | } else { | ||
| 2696 | my $transform_fn = sub { | ||
| 2697 | $_[0] =~ s[/][\\/]g; | ||
| 2698 | return $_[0]; | ||
| 2699 | }; | ||
| 2700 | return $self->info_from_template("string $q //", | ||
| 2701 | $op, "$coreq/%F/", | ||
| 2702 | [[0, $transform_fn]], [$str]); | ||
| 2703 | } | ||
| 2704 | } | ||
| 2705 | |||
| 2706 | # Demo code | ||
| 2707 | unless(caller) { | ||
| 2708 | ; | ||
| 2709 | } | ||
| 2710 | |||
| 2711 | 1; |