| 1 | # Copyright (c) 2015-2018 Rocky Bernstein | ||
| 2 | # Copyright (c) 1998-2000, 2002, 2003, 2004, 2005, 2006 Stephen McCamant. | ||
| 3 | |||
| 4 | # All rights reserved. | ||
| 5 | # This module is free software; you can redistribute and/or modify | ||
| 6 | # it under the same terms as Perl itself. | ||
| 7 | |||
| 8 | # This is based on the module B::Deparse by Stephen McCamant. | ||
| 9 | # It has been extended save tree structure, and is addressible | ||
| 10 | # by opcode address. | ||
| 11 | |||
| 12 | # B::Parse in turn is based on the module of the same name by Malcolm Beattie, | ||
| 13 | # but essentially none of his code remains. | ||
| 14 | |||
| 15 | # The is the main entrypoint for DeparseTree objects and routines. | ||
| 16 | # In the future there may be a StringMain which is like this | ||
| 17 | # but doesn't save copious tree information but instead just gathers | ||
| 18 | # strings in the same way B::Deparse does. | ||
| 19 | use strict; use warnings; | ||
| 20 | |||
| 21 | package B::DeparseTree; | ||
| 22 | |||
| 23 | use B qw(class | ||
| 24 | CVf_LVALUE | ||
| 25 | CVf_METHOD | ||
| 26 | OPf_KIDS | ||
| 27 | OPf_SPECIAL | ||
| 28 | OPpLVAL_INTRO | ||
| 29 | OPpTARGET_MY | ||
| 30 | SVf_IOK | ||
| 31 | SVf_NOK | ||
| 32 | SVf_POK | ||
| 33 | SVf_ROK | ||
| 34 | SVs_RMG | ||
| 35 | SVs_SMG | ||
| 36 | main_cv main_root main_start | ||
| 37 | opnumber | ||
| 38 | perlstring | ||
| 39 | svref_2object | ||
| 40 | ); | ||
| 41 | |||
| 42 | use Carp; | ||
| 43 | use B::Deparse; | ||
| 44 | use B::DeparseTree::PP_OPtable; | ||
| 45 | use B::DeparseTree::SyntaxTree; | ||
| 46 | |||
| 47 | # Copy unchanged functions from B::Deparse | ||
| 48 | *find_scope_en = *B::Deparse::find_scope_en; | ||
| 49 | *find_scope_st = *B::Deparse::find_scope_st; | ||
| 50 | *gv_name = *B::Deparse::gv_name; | ||
| 51 | *lex_in_scope = *B::Deparse::lex_in_scope; | ||
| 52 | *padname = *B::Deparse::padname; | ||
| 53 | *rv2gv_or_string = *B::Deparse::rv2gv_or_string; | ||
| 54 | *stash_subs = *B::Deparse::stash_subs; | ||
| 55 | *stash_variable = *B::Deparse::stash_variable; | ||
| 56 | |||
| 57 | our($VERSION, @EXPORT, @ISA); | ||
| 58 | $VERSION = '3.2.0'; | ||
| 59 | @ISA = qw(Exporter); | ||
| 60 | @EXPORT = qw( | ||
| 61 | %globalnames | ||
| 62 | %ignored_hints | ||
| 63 | %rev_feature | ||
| 64 | WARN_MASK | ||
| 65 | coderef2info | ||
| 66 | coderef2text | ||
| 67 | const | ||
| 68 | declare_hinthash | ||
| 69 | declare_hints | ||
| 70 | declare_warnings | ||
| 71 | deparse_sub($$$$) | ||
| 72 | deparse_subname($$) | ||
| 73 | new | ||
| 74 | next_todo | ||
| 75 | pragmata | ||
| 76 | print_protos | ||
| 77 | seq_subs | ||
| 78 | style_opts | ||
| 79 | ); | ||
| 80 | |||
| 81 | use Config; | ||
| 82 | my $is_cperl = $Config::Config{usecperl}; | ||
| 83 | |||
| 84 | my $module; | ||
| 85 | if ($] >= 5.016 and $] < 5.018) { | ||
| 86 | # 5.16 and 5.18 are the same for now | ||
| 87 | $module = "P518"; | ||
| 88 | } elsif ($] >= 5.018 and $] < 5.020) { | ||
| 89 | $module = "P518"; | ||
| 90 | } elsif ($] >= 5.020 and $] < 5.022) { | ||
| 91 | $module = "P520"; | ||
| 92 | } elsif ($] >= 5.022 and $] < 5.024) { | ||
| 93 | $module = "P522"; | ||
| 94 | } elsif ($] >= 5.024 and $] < 5.026) { | ||
| 95 | $module = "P524"; | ||
| 96 | } elsif ($] >= 5.026) { | ||
| 97 | $module = "P526"; | ||
| 98 | } else { | ||
| 99 | die "Can only handle Perl 5.16..5.26"; | ||
| 100 | } | ||
| 101 | |||
| 102 | $module .= 'c' if $is_cperl; | ||
| 103 | @ISA = ("Exporter", "B::DeparseTree::$module"); | ||
| 104 | |||
| 105 | require "B/DeparseTree/${module}.pm"; | ||
| 106 | |||
| 107 | # The BEGIN {} is used here because otherwise this code isn't executed | ||
| 108 | # when you run B::Deparse on itself. | ||
| 109 | my %globalnames; | ||
| 110 | BEGIN { map($globalnames{$_}++, "SIG", "STDIN", "STDOUT", "STDERR", "INC", | ||
| 111 | "ENV", "ARGV", "ARGVOUT", "_"); } | ||
| 112 | |||
| 113 | my $max_prec; | ||
| 114 | BEGIN { $max_prec = int(0.999 + 8*length(pack("F", 42))*log(2)/log(10)); } | ||
| 115 | |||
| 116 | BEGIN { | ||
| 117 | # List version-specific constants here. | ||
| 118 | # Easiest way to keep this code portable between version looks to | ||
| 119 | # be to fake up a dummy constant that will never actually be true. | ||
| 120 | foreach (qw(OPpSORT_INPLACE OPpSORT_DESCEND OPpITER_REVERSED | ||
| 121 | OPpCONST_NOVER OPpPAD_STATE PMf_SKIPWHITE RXf_SKIPWHITE | ||
| 122 | RXf_PMf_CHARSET RXf_PMf_KEEPCOPY | ||
| 123 | CVf_LOCKED OPpREVERSE_INPLACE OPpSUBSTR_REPL_FIRST | ||
| 124 | PMf_NONDESTRUCT OPpCONST_ARYBASE OPpEVAL_BYTES)) { | ||
| 125 | eval { import B $_ }; | ||
| 126 | no strict 'refs'; | ||
| 127 | *{$_} = sub () {0} unless *{$_}{CODE}; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | sub new { | ||
| 132 | my $class = shift; | ||
| 133 | my $self = bless {}, $class; | ||
| 134 | $self->{'cuddle'} = " "; #\n%| is another alternative | ||
| 135 | $self->{'curcop'} = undef; | ||
| 136 | $self->{'curstash'} = "main"; | ||
| 137 | $self->{'ex_const'} = "'?unrecoverable constant?'"; | ||
| 138 | $self->{'expand'} = 0; | ||
| 139 | $self->{'files'} = {}; | ||
| 140 | |||
| 141 | # How many spaces per indent nesting? | ||
| 142 | $self->{'indent_size'} = 4; | ||
| 143 | |||
| 144 | $self->{'opaddr'} = 0; | ||
| 145 | $self->{'linenums'} = 0; | ||
| 146 | $self->{'parens'} = 0; | ||
| 147 | $self->{'subs_todo'} = []; | ||
| 148 | $self->{'unquote'} = 0; | ||
| 149 | $self->{'use_dumper'} = 0; | ||
| 150 | |||
| 151 | # Compress spaces with tabs? 1 tab = 8 spaces | ||
| 152 | $self->{'use_tabs'} = 0; | ||
| 153 | |||
| 154 | # Indentation level | ||
| 155 | $self->{'level'} = 0; | ||
| 156 | |||
| 157 | $self->{'ambient_arybase'} = 0; | ||
| 158 | $self->{'ambient_warnings'} = undef; # Assume no lexical warnings | ||
| 159 | $self->{'ambient_hints'} = 0; | ||
| 160 | $self->{'ambient_hinthash'} = undef; | ||
| 161 | |||
| 162 | # Given an opcode address, get the accumulated OP tree | ||
| 163 | # OP for that. FIXME: remove this | ||
| 164 | $self->{optree} = {}; | ||
| 165 | |||
| 166 | # Extra opcode information: parent_op | ||
| 167 | $self->{ops} = {}; | ||
| 168 | |||
| 169 | # For B::DeparseTree::Node's that are created and don't have real OPs associated | ||
| 170 | # with them, we assign a fake address; | ||
| 171 | $self->{'last_fake_addr'} = 0; | ||
| 172 | |||
| 173 | $self->init(); | ||
| 174 | |||
| 175 | while (my $arg = shift @_) { | ||
| 176 | if ($arg eq "-d") { | ||
| 177 | $self->{'use_dumper'} = 1; | ||
| 178 | require Data::Dumper; | ||
| 179 | } elsif ($arg =~ /^-f(.*)/) { | ||
| 180 | $self->{'files'}{$1} = 1; | ||
| 181 | } elsif ($arg eq "-l") { | ||
| 182 | $self->{'linenums'} = 1; | ||
| 183 | } elsif ($arg eq "-a") { | ||
| 184 | $self->{'linenums'} = 1; | ||
| 185 | $self->{'opaddr'} = 1; | ||
| 186 | } elsif ($arg eq "-p") { | ||
| 187 | $self->{'parens'} = 1; | ||
| 188 | } elsif ($arg eq "-P") { | ||
| 189 | $self->{'noproto'} = 1; | ||
| 190 | } elsif ($arg eq "-q") { | ||
| 191 | $self->{'unquote'} = 1; | ||
| 192 | } elsif (substr($arg, 0, 2) eq "-s") { | ||
| 193 | $self->style_opts(substr $arg, 2); | ||
| 194 | } elsif ($arg =~ /^-x(\d)$/) { | ||
| 195 | $self->{'expand'} = $1; | ||
| 196 | } | ||
| 197 | } | ||
| 198 | return $self; | ||
| 199 | } | ||
| 200 | |||
| 201 | { | ||
| 202 | # Mask out the bits that L<warnings::register> uses | ||
| 203 | my $WARN_MASK; | ||
| 204 | BEGIN { | ||
| 205 | $WARN_MASK = $warnings::Bits{all} | $warnings::DeadBits{all}; | ||
| 206 | } | ||
| 207 | sub WARN_MASK () { | ||
| 208 | return $WARN_MASK; | ||
| 209 | } | ||
| 210 | } | ||
| 211 | |||
| 212 | # Initialize the contextual information, either from | ||
| 213 | # defaults provided with the ambient_pragmas method, | ||
| 214 | # or from Perl's own defaults otherwise. | ||
| 215 | sub init { | ||
| 216 | my $self = shift; | ||
| 217 | |||
| 218 | $self->{'arybase'} = $self->{'ambient_arybase'}; | ||
| 219 | $self->{'warnings'} = defined ($self->{'ambient_warnings'}) | ||
| 220 | ? $self->{'ambient_warnings'} & WARN_MASK | ||
| 221 | : undef; | ||
| 222 | $self->{'hints'} = $self->{'ambient_hints'}; | ||
| 223 | $self->{'hints'} &= 0xFF if $] < 5.009; | ||
| 224 | $self->{'hinthash'} = $self->{'ambient_hinthash'}; | ||
| 225 | |||
| 226 | # also a convenient place to clear out subs_declared | ||
| 227 | delete $self->{'subs_declared'}; | ||
| 228 | } | ||
| 229 | |||
| 230 | BEGIN { for (qw[ pushmark ]) { | ||
| 231 | eval "sub OP_\U$_ () { " . opnumber($_) . "}" | ||
| 232 | }} | ||
| 233 | |||
| 234 | sub main2info | ||
| 235 | { | ||
| 236 | my $self = shift; | ||
| 237 | $self->{'curcv'} = B::main_cv; | ||
| 238 | $self->pessimise(B::main_root, B::main_start); | ||
| 239 | return $self->deparse_root(B::main_root); | ||
| 240 | } | ||
| 241 | |||
| 242 | sub coderef2info | ||
| 243 | { | ||
| 244 | my ($self, $coderef, $start_op) = @_; | ||
| 245 | croak "Usage: ->coderef2info(CODEREF)" unless UNIVERSAL::isa($coderef, "CODE"); | ||
| 246 | $self->init(); | ||
| 247 | return $self->deparse_sub(svref_2object($coderef), $start_op); | ||
| 248 | } | ||
| 249 | |||
| 250 | sub coderef2text | ||
| 251 | { | ||
| 252 | my ($self, $func) = @_; | ||
| 253 | croak "Usage: ->coderef2text(CODEREF)" unless UNIVERSAL::isa($func, "CODE"); | ||
| 254 | |||
| 255 | $self->init(); | ||
| 256 | my $info = $self->coderef2info($func); | ||
| 257 | return $self->info2str($info); | ||
| 258 | } | ||
| 259 | |||
| 260 | sub const { | ||
| 261 | my $self = shift; | ||
| 262 | my($sv, $cx) = @_; | ||
| 263 | if ($self->{'use_dumper'}) { | ||
| 264 | return $self->const_dumper($sv, $cx); | ||
| 265 | } | ||
| 266 | if (class($sv) eq "SPECIAL") { | ||
| 267 | # sv_undef, sv_yes, sv_no | ||
| 268 | my $text = ('undef', '1', $self->maybe_parens("!1", $cx, 21))[$$sv-1]; | ||
| 269 | return info_from_text($sv, $self, $text, 'const_special', {}); | ||
| 270 | } | ||
| 271 | if (class($sv) eq "NULL") { | ||
| 272 | return info_from_text($sv, $self, 'undef', 'const_NULL', {}); | ||
| 273 | } | ||
| 274 | # convert a version object into the "v1.2.3" string in its V magic | ||
| 275 | if ($sv->FLAGS & SVs_RMG) { | ||
| 276 | for (my $mg = $sv->MAGIC; $mg; $mg = $mg->MOREMAGIC) { | ||
| 277 | if ($mg->TYPE eq 'V') { | ||
| 278 | return info_from_text($sv, $self, $mg->PTR, 'const_magic', {}); | ||
| 279 | } | ||
| 280 | } | ||
| 281 | } | ||
| 282 | |||
| 283 | if ($sv->FLAGS & SVf_IOK) { | ||
| 284 | my $str = $sv->int_value; | ||
| 285 | $str = $self->maybe_parens($str, $cx, 21) if $str < 0; | ||
| 286 | return $self->info_from_string("integer constant $str", $sv, $str); | ||
| 287 | } elsif ($sv->FLAGS & SVf_NOK) { | ||
| 288 | my $nv = $sv->NV; | ||
| 289 | if ($nv == 0) { | ||
| 290 | if (pack("F", $nv) eq pack("F", 0)) { | ||
| 291 | # positive zero | ||
| 292 | return info_from_text($sv, $self, "0", 'constant float positive 0', {}); | ||
| 293 | } else { | ||
| 294 | # negative zero | ||
| 295 | return info_from_text($sv, $self, $self->maybe_parens("-.0", $cx, 21), | ||
| 296 | 'constant float negative 0', {}); | ||
| 297 | } | ||
| 298 | } elsif (1/$nv == 0) { | ||
| 299 | if ($nv > 0) { | ||
| 300 | # positive infinity | ||
| 301 | return info_from_text($sv, $self, $self->maybe_parens("9**9**9", $cx, 22), | ||
| 302 | 'constant float +infinity', {}); | ||
| 303 | } else { | ||
| 304 | # negative infinity | ||
| 305 | return info_from_text($sv, $self, $self->maybe_parens("-9**9**9", $cx, 21), | ||
| 306 | 'constant float -infinity', {}); | ||
| 307 | } | ||
| 308 | } elsif ($nv != $nv) { | ||
| 309 | # NaN | ||
| 310 | if (pack("F", $nv) eq pack("F", sin(9**9**9))) { | ||
| 311 | # the normal kind | ||
| 312 | return info_from_text($sv, $self, "sin(9**9**9)", 'const_Nan', {}); | ||
| 313 | } elsif (pack("F", $nv) eq pack("F", -sin(9**9**9))) { | ||
| 314 | # the inverted kind | ||
| 315 | return info_from_text($sv, $self, $self->maybe_parens("-sin(9**9**9)", $cx, 21), | ||
| 316 | 'constant float Nan invert', {}); | ||
| 317 | } else { | ||
| 318 | # some other kind | ||
| 319 | my $hex = unpack("h*", pack("F", $nv)); | ||
| 320 | return info_from_text($sv, $self, qq'unpack("F", pack("h*", "$hex"))', | ||
| 321 | 'constant Na na na', {}); | ||
| 322 | } | ||
| 323 | } | ||
| 324 | # first, try the default stringification | ||
| 325 | my $str = "$nv"; | ||
| 326 | if ($str != $nv) { | ||
| 327 | # failing that, try using more precision | ||
| 328 | $str = sprintf("%.${max_prec}g", $nv); | ||
| 329 | # if (pack("F", $str) ne pack("F", $nv)) { | ||
| 330 | if ($str != $nv) { | ||
| 331 | # not representable in decimal with whatever sprintf() | ||
| 332 | # and atof() Perl is using here. | ||
| 333 | my($mant, $exp) = split_float($nv); | ||
| 334 | return info_from_text($sv, $self, $self->maybe_parens("$mant * 2**$exp", $cx, 19), | ||
| 335 | 'constant float not-sprintf/atof-able', {}); | ||
| 336 | } | ||
| 337 | } | ||
| 338 | $str = $self->maybe_parens($str, $cx, 21) if $nv < 0; | ||
| 339 | return info_from_text($sv, $self, $str, 'constant nv', {}); | ||
| 340 | } elsif ($sv->FLAGS & SVf_ROK && $sv->can("RV")) { | ||
| 341 | my $ref = $sv->RV; | ||
| 342 | if (class($ref) eq "AV") { | ||
| 343 | my $list_info = $self->list_const($sv, 2, $ref->ARRAY); | ||
| 344 | return info_from_list($sv, $self, ['[', $list_info->{text}, ']'], '', 'const_av', | ||
| 345 | {body => [$list_info]}); | ||
| 346 | } elsif (class($ref) eq "HV") { | ||
| 347 | my %hash = $ref->ARRAY; | ||
| 348 | my @elts; | ||
| 349 | for my $k (sort keys %hash) { | ||
| 350 | push @elts, "$k => " . $self->const($hash{$k}, 6); | ||
| 351 | } | ||
| 352 | return info_from_list($sv, $self, ["{", join(", ", @elts), "}"], '', | ||
| 353 | 'constant hash value', {}); | ||
| 354 | } elsif (class($ref) eq "CV") { | ||
| 355 | BEGIN { | ||
| 356 | if ($] > 5.0150051) { | ||
| 357 | require overloading; | ||
| 358 | unimport overloading; | ||
| 359 | } | ||
| 360 | } | ||
| 361 | if ($] > 5.0150051 && $self->{curcv} && | ||
| 362 | $self->{curcv}->object_2svref == $ref->object_2svref) { | ||
| 363 | return $self->info_from_string('sub __SUB__', $sv, | ||
| 364 | $self->keyword("__SUB__")); | ||
| 365 | } | ||
| 366 | my $sub_info = $self->deparse_sub($ref); | ||
| 367 | return info_from_list($sub_info->{op}, $self, ["sub ", $sub_info->{text}], '', | ||
| 368 | 'constant sub 2', | ||
| 369 | {body => [$sub_info]}); | ||
| 370 | } | ||
| 371 | if ($ref->FLAGS & SVs_SMG) { | ||
| 372 | for (my $mg = $ref->MAGIC; $mg; $mg = $mg->MOREMAGIC) { | ||
| 373 | if ($mg->TYPE eq 'r') { | ||
| 374 | my $re = B::Deparse::re_uninterp(B::Deparse::escape_str(B::Deparse::re_unback($mg->precomp))); | ||
| 375 | return $self->single_delim($sv, "qr", "", $re); | ||
| 376 | } | ||
| 377 | } | ||
| 378 | } | ||
| 379 | |||
| 380 | my $const = $self->const($ref, 20); | ||
| 381 | if ($self->{in_subst_repl} && $const =~ /^[0-9]/) { | ||
| 382 | $const = "($const)"; | ||
| 383 | } | ||
| 384 | my @texts = ("\\", $const); | ||
| 385 | return info_from_list($sv, $self, \@texts, '', 'const_rv', | ||
| 386 | {maybe_parens => [$self, $cx, 20]}); | ||
| 387 | |||
| 388 | } elsif ($sv->FLAGS & SVf_POK) { | ||
| 389 | my $str = $sv->PV; | ||
| 390 | if ($str =~ /[[:^print:]]/) { | ||
| 391 | return $self->single_delim($sv, "qq", '"', | ||
| 392 | B::Deparse::uninterp B::Deparse::escape_str B::Deparse::unback $str); | ||
| 393 | } else { | ||
| 394 | return $self->single_delim($sv, "q", "'", B::Deparse::unback $str); | ||
| 395 | } | ||
| 396 | } else { | ||
| 397 | return info_from_text($sv, $self, "undef", 'constant undef', {}); | ||
| 398 | } | ||
| 399 | } | ||
| 400 | |||
| 401 | sub const_dumper | ||
| 402 | { | ||
| 403 | my $self = shift; | ||
| 404 | my($sv, $cx) = @_; | ||
| 405 | my $ref = $sv->object_2svref(); | ||
| 406 | my $dumper = Data::Dumper->new([$$ref], ['$v']); | ||
| 407 | $dumper->Purity(1)->Terse(1)->Deparse(1)->Indent(0)->Useqq(1)->Sortkeys(1); | ||
| 408 | my $str = $dumper->Dump(); | ||
| 409 | if ($str =~ /^\$v/) { | ||
| 410 | return info_from_text($sv, $self, ['${my', $str, '\$v}'], 'const_dumper_my', {}); | ||
| 411 | } else { | ||
| 412 | return info_from_text($sv, $self, $str, 'constant dumper', {}); | ||
| 413 | } | ||
| 414 | } | ||
| 415 | |||
| 416 | # This is a special case of scopeop and lineseq, for the case of the | ||
| 417 | # main_root. | ||
| 418 | sub deparse_root { | ||
| 419 | my $self = shift; | ||
| 420 | my($op) = @_; | ||
| 421 | local(@$self{qw'curstash warnings hints hinthash'}) | ||
| 422 | = @$self{qw'curstash warnings hints hinthash'}; | ||
| 423 | my @ops; | ||
| 424 | return if B::Deparse::null $op->first; # Can happen, e.g., for Bytecode without -k | ||
| 425 | for (my $kid = $op->first->sibling; !B::Deparse::null($kid); $kid = $kid->sibling) { | ||
| 426 | push @ops, $kid; | ||
| 427 | } | ||
| 428 | my $fn = sub { | ||
| 429 | my ($exprs, $i, $info, $parent) = @_; | ||
| 430 | my $text = $info->{text}; | ||
| 431 | my $op = $ops[$i]; | ||
| 432 | $text =~ s/\f//; | ||
| 433 | $text =~ s/\n$//; | ||
| 434 | $text =~ s/;\n?\z//; | ||
| 435 | $text =~ s/^\((.+)\)$/$1/; | ||
| 436 | $info->{type} = $op->name; | ||
| 437 | $info->{op} = $op; | ||
| 438 | |||
| 439 | # FIXME: this is going away... | ||
| 440 | $self->{optree}{$$op} = $info; | ||
| 441 | # in favor of... | ||
| 442 | $self->{ops}{$$op}{info} = $info; | ||
| 443 | |||
| 444 | $info->{text} = $text; | ||
| 445 | $info->{parent} = $$parent if $parent; | ||
| 446 | push @$exprs, $info; | ||
| 447 | }; | ||
| 448 | my $info = $self->walk_lineseq($op, \@ops, $fn); | ||
| 449 | my @skipped_ops; | ||
| 450 | if (exists $info->{other_ops}) { | ||
| 451 | @skipped_ops = @{$info->{other_ops}}; | ||
| 452 | push @skipped_ops, $op->first; | ||
| 453 | } else { | ||
| 454 | @skipped_ops = ($op->first); | ||
| 455 | } | ||
| 456 | $info->{other_ops} = \@skipped_ops; | ||
| 457 | return $info; | ||
| 458 | |||
| 459 | } | ||
| 460 | |||
| 461 | sub update_node($$$$) | ||
| 462 | { | ||
| 463 | my ($self, $node, $prev_expr, $op) = @_; | ||
| 464 | $node->{prev_expr} = $prev_expr; | ||
| 465 | $self->{optree}{$$op} = $node if $op; | ||
| 466 | $self->{ops}{$$op}{info} = $node if $op; | ||
| 467 | } | ||
| 468 | |||
| 469 | sub walk_lineseq | ||
| 470 | { | ||
| 471 | my ($self, $op, $kids, $callback) = @_; | ||
| 472 | my @kids = @$kids; | ||
| 473 | my @body = (); # Accumulated node structures | ||
| 474 | my $expr; | ||
| 475 | my $prev_expr = undef; | ||
| 476 | my $fix_cop = undef; | ||
| 477 | for (my $i = 0; $i < @kids; $i++) { | ||
| 478 | if (B::Deparse::is_state $kids[$i]) { | ||
| 479 | $expr = ($self->deparse($kids[$i], 0, $op)); | ||
| 480 | $callback->(\@body, $i, $expr, $op); | ||
| 481 | $self->update_node($expr, $prev_expr, $op); | ||
| 482 | $prev_expr = $expr; | ||
| 483 | if ($fix_cop) { | ||
| 484 | $fix_cop->{text} = $expr->{text}; | ||
| 485 | } | ||
| 486 | |||
| 487 | $i++; | ||
| 488 | if ($i > $#kids) { | ||
| 489 | last; | ||
| 490 | } | ||
| 491 | } | ||
| 492 | if (B::Deparse::is_for_loop($kids[$i])) { | ||
| 493 | my $loop_expr = $self->for_loop($kids[$i], 0); | ||
| 494 | $callback->(\@body, | ||
| 495 | $i += $kids[$i]->sibling->name eq "unstack" ? 2 : 1, | ||
| 496 | $loop_expr); | ||
| 497 | $self->update_node($expr, $prev_expr, $op); | ||
| 498 | $prev_expr = $expr; | ||
| 499 | next; | ||
| 500 | } | ||
| 501 | $expr = $self->deparse($kids[$i], (@kids != 1)/2, $op); | ||
| 502 | |||
| 503 | # Perform semantic action on $expr accumulating the result | ||
| 504 | # in @body. $op is the parent, and $i is the child position | ||
| 505 | $callback->(\@body, $i, $expr, $op); | ||
| 506 | $self->update_node($expr, $prev_expr, $op); | ||
| 507 | $prev_expr = $expr; | ||
| 508 | if ($fix_cop) { | ||
| 509 | $fix_cop->{text} = $expr->{text}; | ||
| 510 | } | ||
| 511 | |||
| 512 | # If the text portion of a COP is empty, set up to fill it in | ||
| 513 | # from the text portion of the next node. | ||
| 514 | if (B::class($op) eq "COP" && !$expr->{text}) { | ||
| 515 | $fix_cop = $op; | ||
| 516 | } else { | ||
| 517 | $fix_cop = undef; | ||
| 518 | } | ||
| 519 | } | ||
| 520 | |||
| 521 | # Add semicolons between statements. Don't null statements | ||
| 522 | # (which can happen for nexstate which doesn't have source code | ||
| 523 | # associated with it. | ||
| 524 | $expr = $self->info_from_template("statements", $op, "%;", [], \@body); | ||
| 525 | $self->update_node($expr, $prev_expr, $op); | ||
| 526 | return $expr; | ||
| 527 | } | ||
| 528 | |||
| 529 | # $root should be the op which represents the root of whatever | ||
| 530 | # we're sequencing here. If it's undefined, then we don't append | ||
| 531 | # any subroutine declarations to the deparsed ops, otherwise we | ||
| 532 | # append appropriate declarations. | ||
| 533 | sub lineseq { | ||
| 534 | my($self, $root, $cx, @ops) = @_; | ||
| 535 | |||
| 536 | my $out_cop = $self->{'curcop'}; | ||
| 537 | my $out_seq = defined($out_cop) ? $out_cop->cop_seq : undef; | ||
| 538 | my $limit_seq; | ||
| 539 | if (defined $root) { | ||
| 540 | $limit_seq = $out_seq; | ||
| 541 | my $nseq; | ||
| 542 | $nseq = $self->find_scope_st($root->sibling) if ${$root->sibling}; | ||
| 543 | $limit_seq = $nseq if !defined($limit_seq) | ||
| 544 | or defined($nseq) && $nseq < $limit_seq; | ||
| 545 | } | ||
| 546 | $limit_seq = $self->{'limit_seq'} | ||
| 547 | if defined($self->{'limit_seq'}) | ||
| 548 | && (!defined($limit_seq) || $self->{'limit_seq'} < $limit_seq); | ||
| 549 | local $self->{'limit_seq'} = $limit_seq; | ||
| 550 | |||
| 551 | my $fn = sub { | ||
| 552 | my ($exprs, $i, $info, $parent) = @_; | ||
| 553 | my $op = $ops[$i]; | ||
| 554 | $info->{type} = $op->name unless $info->{type}; | ||
| 555 | $info->{child_pos} = $i; | ||
| 556 | $info->{op} = $op; | ||
| 557 | if ($parent) { | ||
| 558 | Carp::confess("nonref parent, op: $op->name") if !ref($parent); | ||
| 559 | $info->{parent} = $$parent ; | ||
| 560 | } | ||
| 561 | |||
| 562 | # FIXME: remove optree? | ||
| 563 | $self->{optree}{$$op} = $info; | ||
| 564 | $self->{ops}{$$op}{info} = $info; | ||
| 565 | |||
| 566 | push @$exprs, $info; | ||
| 567 | }; | ||
| 568 | return $self->walk_lineseq($root, \@ops, $fn); | ||
| 569 | } | ||
| 570 | |||
| 571 | sub todo | ||
| 572 | { | ||
| 573 | my $self = shift; | ||
| 574 | my($cv, $is_form, $name) = @_; | ||
| 575 | my $cvfile = $cv->FILE//''; | ||
| 576 | return unless ($cvfile eq $0 || exists $self->{files}{$cvfile}); | ||
| 577 | my $seq; | ||
| 578 | if ($cv->OUTSIDE_SEQ) { | ||
| 579 | $seq = $cv->OUTSIDE_SEQ; | ||
| 580 | } elsif (!B::Deparse::null($cv->START) and B::Deparse::is_state($cv->START)) { | ||
| 581 | $seq = $cv->START->cop_seq; | ||
| 582 | } else { | ||
| 583 | $seq = 0; | ||
| 584 | } | ||
| 585 | push @{$self->{'subs_todo'}}, [$seq, $cv, $is_form, $name]; | ||
| 586 | } | ||
| 587 | |||
| 588 | # _pessimise_walk(): recursively walk the optree of a sub, | ||
| 589 | # possibly undoing optimisations along the way. | ||
| 590 | # walk tree in root-to-branch order | ||
| 591 | # We add parent pointers in the process. | ||
| 592 | |||
| 593 | sub _pessimise_walk { | ||
| 594 | my ($self, $startop) = @_; | ||
| 595 | |||
| 596 | return unless $$startop; | ||
| 597 | my ($op, $parent_op); | ||
| 598 | |||
| 599 | for ($op = $startop; $$op; $op = $op->sibling) { | ||
| 600 | my $ppname = $op->name; | ||
| 601 | |||
| 602 | $self->{ops}{$$op} ||= {}; | ||
| 603 | $self->{ops}{$$op}{op} = $op; | ||
| 604 | $self->{ops}{$$op}{parent_op} = $startop; | ||
| 605 | |||
| 606 | # pessimisations start here | ||
| 607 | |||
| 608 | if ($ppname eq "padrange") { | ||
| 609 | # remove PADRANGE: | ||
| 610 | # the original optimisation either (1) changed this: | ||
| 611 | # pushmark -> (various pad and list and null ops) -> the_rest | ||
| 612 | # or (2), for the = @_ case, changed this: | ||
| 613 | # pushmark -> gv[_] -> rv2av -> (pad stuff) -> the_rest | ||
| 614 | # into this: | ||
| 615 | # padrange ----------------------------------------> the_rest | ||
| 616 | # so we just need to convert the padrange back into a | ||
| 617 | # pushmark, and in case (1), set its op_next to op_sibling, | ||
| 618 | # which is the head of the original chain of optimised-away | ||
| 619 | # pad ops, or for (2), set it to sibling->first, which is | ||
| 620 | # the original gv[_]. | ||
| 621 | |||
| 622 | $B::overlay->{$$op} = { | ||
| 623 | type => OP_PUSHMARK, | ||
| 624 | name => 'pushmark', | ||
| 625 | private => ($op->private & OPpLVAL_INTRO), | ||
| 626 | }; | ||
| 627 | } | ||
| 628 | |||
| 629 | # pessimisations end here | ||
| 630 | |||
| 631 | if (class($op) eq 'PMOP' | ||
| 632 | && ref($op->pmreplroot) | ||
| 633 | && ${$op->pmreplroot} | ||
| 634 | && $op->pmreplroot->isa( 'B::OP' )) | ||
| 635 | { | ||
| 636 | $self-> _pessimise_walk($op->pmreplroot); | ||
| 637 | } | ||
| 638 | |||
| 639 | if ($op->flags & OPf_KIDS) { | ||
| 640 | $self-> _pessimise_walk($op->first); | ||
| 641 | } | ||
| 642 | |||
| 643 | } | ||
| 644 | } | ||
| 645 | |||
| 646 | |||
| 647 | # _pessimise_walk_exe(): recursively walk the op_next chain of a sub, | ||
| 648 | # possibly undoing optimisations along the way. | ||
| 649 | # walk tree in execution order | ||
| 650 | |||
| 651 | sub _pessimise_walk_exe { | ||
| 652 | my ($self, $startop, $visited) = @_; | ||
| 653 | |||
| 654 | return unless $$startop; | ||
| 655 | return if $visited->{$$startop}; | ||
| 656 | my $op; | ||
| 657 | for ($op = $startop; $$op; $op = $op->next) { | ||
| 658 | last if $visited->{$$op}; | ||
| 659 | $visited->{$$op} = 1; | ||
| 660 | |||
| 661 | $self->{ops}{$$op} ||= {}; | ||
| 662 | $self->{ops}{$$op}{op} = $op; | ||
| 663 | |||
| 664 | my $ppname = $op->name; | ||
| 665 | if ($ppname =~ | ||
| 666 | /^((and|d?or)(assign)?|(map|grep)while|range|cond_expr|once)$/ | ||
| 667 | # entertry is also a logop, but its op_other invariably points | ||
| 668 | # into the same chain as the main execution path, so we skip it | ||
| 669 | ) { | ||
| 670 | $self->_pessimise_walk_exe($op->other, $visited); | ||
| 671 | } | ||
| 672 | elsif ($ppname eq "subst") { | ||
| 673 | $self->_pessimise_walk_exe($op->pmreplstart, $visited); | ||
| 674 | } | ||
| 675 | elsif ($ppname =~ /^(enter(loop|iter))$/) { | ||
| 676 | # redoop and nextop will already be covered by the main block | ||
| 677 | # of the loop | ||
| 678 | $self->_pessimise_walk_exe($op->lastop, $visited); | ||
| 679 | } | ||
| 680 | |||
| 681 | # pessimisations start here | ||
| 682 | } | ||
| 683 | } | ||
| 684 | |||
| 685 | # Go through an optree and "remove" some optimisations by using an | ||
| 686 | # overlay to selectively modify or un-null some ops. Deparsing in the | ||
| 687 | # absence of those optimisations is then easier. | ||
| 688 | # | ||
| 689 | # Note that older optimisations are not removed, as Deparse was already | ||
| 690 | # written to recognise them before the pessimise/overlay system was added. | ||
| 691 | |||
| 692 | sub pessimise { | ||
| 693 | my ($self, $root, $start) = @_; | ||
| 694 | |||
| 695 | no warnings 'recursion'; | ||
| 696 | # walk tree in root-to-branch order | ||
| 697 | $self->_pessimise_walk($root); | ||
| 698 | |||
| 699 | my %visited; | ||
| 700 | # walk tree in execution order | ||
| 701 | $self->_pessimise_walk_exe($start, \%visited); | ||
| 702 | } | ||
| 703 | |||
| 704 | sub print_protos { | ||
| 705 | my $self = shift; | ||
| 706 | my $ar; | ||
| 707 | my @ret; | ||
| 708 | foreach $ar (@{$self->{'protos_todo'}}) { | ||
| 709 | my $proto = defined $ar->[1] | ||
| 710 | ? ref $ar->[1] | ||
| 711 | ? " () {\n " . $self->const($ar->[1]->RV,0) . ";\n}" | ||
| 712 | : " (". $ar->[1] . ");" | ||
| 713 | : ";"; | ||
| 714 | push @ret, "sub " . $ar->[0] . "$proto\n"; | ||
| 715 | } | ||
| 716 | delete $self->{'protos_todo'}; | ||
| 717 | return @ret; | ||
| 718 | } | ||
| 719 | |||
| 720 | sub style_opts | ||
| 721 | { | ||
| 722 | my ($self, $opts) = @_; | ||
| 723 | my $opt; | ||
| 724 | while (length($opt = substr($opts, 0, 1))) { | ||
| 725 | if ($opt eq "C") { | ||
| 726 | $self->{'cuddle'} = " "; | ||
| 727 | $opts = substr($opts, 1); | ||
| 728 | } elsif ($opt eq "i") { | ||
| 729 | $opts =~ s/^i(\d+)//; | ||
| 730 | $self->{'indent_size'} = $1; | ||
| 731 | } elsif ($opt eq "T") { | ||
| 732 | $self->{'use_tabs'} = 1; | ||
| 733 | $opts = substr($opts, 1); | ||
| 734 | } elsif ($opt eq "v") { | ||
| 735 | $opts =~ s/^v([^.]*)(.|$)//; | ||
| 736 | $self->{'ex_const'} = $1; | ||
| 737 | } | ||
| 738 | } | ||
| 739 | } | ||
| 740 | |||
| 741 | # This gets called automatically when option: | ||
| 742 | # -MO="DeparseTree,sC" is added | ||
| 743 | # Running this prints out the program text. | ||
| 744 | sub compile { | ||
| 745 | my(@args) = @_; | ||
| 746 | return sub { | ||
| 747 | my $self = B::DeparseTree->new(@args); | ||
| 748 | # First deparse command-line args | ||
| 749 | if (defined $^I) { # deparse -i | ||
| 750 | print q(BEGIN { $^I = ).perlstring($^I).qq(; }\n); | ||
| 751 | } | ||
| 752 | if ($^W) { # deparse -w | ||
| 753 | print qq(BEGIN { \$^W = $^W; }\n); | ||
| 754 | } | ||
| 755 | if ($/ ne "\n" or defined $O::savebackslash) { # deparse -l and -0 | ||
| 756 | my $fs = perlstring($/) || 'undef'; | ||
| 757 | my $bs = perlstring($O::savebackslash) || 'undef'; | ||
| 758 | print qq(BEGIN { \$/ = $fs; \$\\ = $bs; }\n); | ||
| 759 | } | ||
| 760 | my @BEGINs = B::begin_av->isa("B::AV") ? B::begin_av->ARRAY : (); | ||
| 761 | my @UNITCHECKs = B::unitcheck_av->isa("B::AV") | ||
| 762 | ? B::unitcheck_av->ARRAY | ||
| 763 | : (); | ||
| 764 | my @CHECKs = B::check_av->isa("B::AV") ? B::check_av->ARRAY : (); | ||
| 765 | my @INITs = B::init_av->isa("B::AV") ? B::init_av->ARRAY : (); | ||
| 766 | my @ENDs = B::end_av->isa("B::AV") ? B::end_av->ARRAY : (); | ||
| 767 | if ($] < 5.020) { | ||
| 768 | for my $block (@BEGINs, @UNITCHECKs, @CHECKs, @INITs, @ENDs) { | ||
| 769 | $self->todo($block, 0); | ||
| 770 | } | ||
| 771 | } else { | ||
| 772 | my @names = qw(BEGIN UNITCHECK CHECK INIT END); | ||
| 773 | my @blocks = (\@BEGINs, \@UNITCHECKs, \@CHECKs, \@INITs, \@ENDs); | ||
| 774 | while (@names) { | ||
| 775 | my ($name, $blocks) = (shift @names, shift @blocks); | ||
| 776 | for my $block (@$blocks) { | ||
| 777 | $self->todo($block, 0, $name); | ||
| 778 | } | ||
| 779 | } | ||
| 780 | } | ||
| 781 | $self->stash_subs(); | ||
| 782 | local($SIG{"__DIE__"}) = | ||
| 783 | sub { | ||
| 784 | if ($self->{'curcop'}) { | ||
| 785 | my $cop = $self->{'curcop'}; | ||
| 786 | my($line, $file) = ($cop->line, $cop->file); | ||
| 787 | print STDERR "While deparsing $file near line $line,\n"; | ||
| 788 | } | ||
| 789 | use Data::Printer; | ||
| 790 | my @bt = caller(1); | ||
| 791 | p @bt; | ||
| 792 | }; | ||
| 793 | $self->{'curcv'} = main_cv; | ||
| 794 | $self->{'curcvlex'} = undef; | ||
| 795 | print $self->print_protos; | ||
| 796 | @{$self->{'subs_todo'}} = | ||
| 797 | sort {$a->[0] <=> $b->[0]} @{$self->{'subs_todo'}}; | ||
| 798 | my $root = main_root; | ||
| 799 | local $B::overlay = {}; | ||
| 800 | |||
| 801 | if ($] < 5.021) { | ||
| 802 | unless (B::Deparse::null $root) { | ||
| 803 | $self->pessimise($root, main_start); | ||
| 804 | # Print deparsed program | ||
| 805 | print $self->deparse_root($root)->{text}, "\n"; | ||
| 806 | } | ||
| 807 | } else { | ||
| 808 | unless (B::Deparse::null $root) { | ||
| 809 | $self->B::Deparse::pad_subs($self->{'curcv'}); | ||
| 810 | # Check for a stub-followed-by-ex-cop, resulting from a program | ||
| 811 | # consisting solely of sub declarations. For backward-compati- | ||
| 812 | # bility (and sane output) we don’t want to emit the stub. | ||
| 813 | # leave | ||
| 814 | # enter | ||
| 815 | # stub | ||
| 816 | # ex-nextstate (or ex-dbstate) | ||
| 817 | my $kid; | ||
| 818 | if ( $root->name eq 'leave' | ||
| 819 | and ($kid = $root->first)->name eq 'enter' | ||
| 820 | and !B::Deparse::null($kid = $kid->sibling) and $kid->name eq 'stub' | ||
| 821 | and !B::Deparse::null($kid = $kid->sibling) and $kid->name eq 'null' | ||
| 822 | and class($kid) eq 'COP' and B::Deparse::null $kid->sibling ) | ||
| 823 | { | ||
| 824 | # ignore deparsing routine | ||
| 825 | } else { | ||
| 826 | $self->pessimise($root, main_start); | ||
| 827 | # Print deparsed program | ||
| 828 | my $root_tree = $self->deparse_root($root); | ||
| 829 | print $root_tree->{text}, "\n"; | ||
| 830 | } | ||
| 831 | } | ||
| 832 | } | ||
| 833 | my @text; | ||
| 834 | while (scalar(@{$self->{'subs_todo'}})) { | ||
| 835 | push @text, $self->next_todo->{text}; | ||
| 836 | } | ||
| 837 | print join("", @text), "\n" if @text; | ||
| 838 | |||
| 839 | # Print __DATA__ section, if necessary | ||
| 840 | no strict 'refs'; | ||
| 841 | my $laststash = defined $self->{'curcop'} | ||
| 842 | ? $self->{'curcop'}->stash->NAME : $self->{'curstash'}; | ||
| 843 | if (defined *{$laststash."::DATA"}{IO}) { | ||
| 844 | print $self->keyword("package") . " $laststash;\n" | ||
| 845 | unless $laststash eq $self->{'curstash'}; | ||
| 846 | print $self->keyword("__DATA__") . "\n"; | ||
| 847 | print readline(*{$laststash."::DATA"}); | ||
| 848 | } | ||
| 849 | } | ||
| 850 | } | ||
| 851 | |||
| 852 | # "deparse()" is the main function to call to produces a depare tree | ||
| 853 | # for a give B::OP. This method is the inner loop. | ||
| 854 | |||
| 855 | # Rocky's comment with respect to: | ||
| 856 | # so try to keep it simple | ||
| 857 | # | ||
| 858 | # Most normal Perl programs really aren't that big. Yeah, I know there | ||
| 859 | # are a couple of big pigs like the B::Deparse code itself. The perl5 | ||
| 860 | # debugger comes to mind too. But what's the likelihood of anyone wanting | ||
| 861 | # to decompile all of this? | ||
| 862 | # | ||
| 863 | # On the other hand, error checking is too valuable to throw out here. | ||
| 864 | # Also, in trying to use and modularize this code, I see there is | ||
| 865 | # a lot of repetition in subroutine parsing routines. That's | ||
| 866 | # why I added the above PP_MAPFNS table. I'm not going to trade off | ||
| 867 | # table lookup and interpetation for a huge amount of subroutine | ||
| 868 | # bloat. | ||
| 869 | |||
| 870 | # That said it is useful to note that this is inner-most loop | ||
| 871 | # interpeter loop as it is called for each node in the B::OP tree. | ||
| 872 | # | ||
| 873 | sub deparse | ||
| 874 | { | ||
| 875 | my($self, $op, $cx, $parent) = @_; | ||
| 876 | |||
| 877 | Carp::confess("deparse called on an invalid op $op") | ||
| 878 | unless $op->can('name'); | ||
| 879 | |||
| 880 | my $name = $op->name; | ||
| 881 | print "YYY $name\n" if $ENV{'DEBUG_DEPARSETREE'}; | ||
| 882 | my ($info, $meth); | ||
| 883 | |||
| 884 | if (exists($PP_MAPFNS{$name})) { | ||
| 885 | # Interpret method calls for our PP_MAPFNS table | ||
| 886 | if (ref($PP_MAPFNS{$name}) eq 'ARRAY') { | ||
| 887 | my @args = @{$PP_MAPFNS{$name}}; | ||
| 888 | $meth = shift @args; | ||
| 889 | if ($meth eq 'maybe_targmy') { | ||
| 890 | # FIXME: This is an inline version of targmy. | ||
| 891 | # Can we dedup it? do we want to? | ||
| 892 | $meth = shift @args; | ||
| 893 | unshift @args, $name unless @args; | ||
| 894 | if ($op->private & OPpTARGET_MY) { | ||
| 895 | my $var = $self->padname($op->targ); | ||
| 896 | my $val = $self->$meth($op, 7, @args); | ||
| 897 | my @texts = ($var, '=', $val); | ||
| 898 | $info = $self->info_from_template("my", $op, | ||
| 899 | "%c = %c", [0, 1], | ||
| 900 | [$var, $val], | ||
| 901 | {maybe_parens => [$self, $cx, 7]}); | ||
| 902 | } else { | ||
| 903 | $info = $self->$meth($op, $cx, @args); | ||
| 904 | } | ||
| 905 | } else { | ||
| 906 | $info = $self->$meth($op, $cx, @args); | ||
| 907 | } | ||
| 908 | } else { | ||
| 909 | # Simple case: one simple call of the | ||
| 910 | # the method in the table. Call this | ||
| 911 | # passing arguments $op, $cx, and $name. | ||
| 912 | # Some functions might not use these, | ||
| 913 | # but that's okay. | ||
| 914 | $meth = $PP_MAPFNS{$name}; | ||
| 915 | $info = $self->$meth($op, $cx, $name); | ||
| 916 | } | ||
| 917 | } else { | ||
| 918 | # Tried and true fallback method: | ||
| 919 | # a method has been defined for this pp_op special. | ||
| 920 | # call that. | ||
| 921 | $meth = "pp_" . $name; | ||
| 922 | $info = $self->$meth($op, $cx); | ||
| 923 | } | ||
| 924 | |||
| 925 | Carp::confess("nonref return for $meth deparse: $info") if !ref($info); | ||
| 926 | Carp::confess("not B::DeparseTree:Node returned for $meth: $info") | ||
| 927 | if !$info->isa("B::DeparseTree::Node"); | ||
| 928 | $info->{parent} = $$parent if $parent; | ||
| 929 | $info->{cop} = $self->{'curcop'}; | ||
| 930 | my $got_op = $info->{op}; | ||
| 931 | if ($got_op) { | ||
| 932 | if ($got_op != $op) { | ||
| 933 | # Do something here? | ||
| 934 | # printf("XX final op 0x%x is not requested 0x%x\n", | ||
| 935 | # $$op, $$got_op); | ||
| 936 | } | ||
| 937 | } else { | ||
| 938 | $info->{op} = $op; | ||
| 939 | } | ||
| 940 | $self->{optree}{$$op} = $info; | ||
| 941 | if ($info->{other_ops}) { | ||
| 942 | foreach my $other (@{$info->{other_ops}}) { | ||
| 943 | if (!ref $other) { | ||
| 944 | Carp::confess "$meth returns invalid other $other"; | ||
| 945 | } elsif ($other->isa("B::DeparseTree::Node")) { | ||
| 946 | # "$other" has been set up to mark a particular portion | ||
| 947 | # of the info. | ||
| 948 | $self->{optree}{$other->{addr}} = $other; | ||
| 949 | $other->{parent} = $$op; | ||
| 950 | } else { | ||
| 951 | # "$other" is just the OP. Have it mark everything | ||
| 952 | # or "info". | ||
| 953 | $self->{optree}{$$other} = $info; | ||
| 954 | } | ||
| 955 | } | ||
| 956 | } | ||
| 957 | return $info; | ||
| 958 | } | ||
| 959 | |||
| 960 | # Deparse a subroutine | ||
| 961 | sub deparse_sub($$$$) | ||
| 962 | { | ||
| 963 | my ($self, $cv, $start_op) = @_; | ||
| 964 | |||
| 965 | # Sanity checks.. | ||
| 966 | Carp::confess("NULL in deparse_sub") if !defined($cv) || $cv->isa("B::NULL"); | ||
| 967 | Carp::confess("SPECIAL in deparse_sub") if $cv->isa("B::SPECIAL"); | ||
| 968 | |||
| 969 | # First get protype and sub attribute information | ||
| 970 | local $self->{'curcop'} = $self->{'curcop'}; | ||
| 971 | my $proto = ''; | ||
| 972 | if ($cv->FLAGS & SVf_POK) { | ||
| 973 | $proto .= "(". $cv->PV . ")"; | ||
| 974 | } | ||
| 975 | if ($cv->CvFLAGS & (CVf_METHOD|CVf_LOCKED|CVf_LVALUE)) { | ||
| 976 | $proto .= ":"; | ||
| 977 | $proto .= " lvalue" if $cv->CvFLAGS & CVf_LVALUE; | ||
| 978 | $proto .= " locked" if $cv->CvFLAGS & CVf_LOCKED; | ||
| 979 | $proto .= " method" if $cv->CvFLAGS & CVf_METHOD; | ||
| 980 | } | ||
| 981 | |||
| 982 | local($self->{'curcv'}) = $cv; | ||
| 983 | local($self->{'curcvlex'}); | ||
| 984 | local(@$self{qw'curstash warnings hints hinthash'}) | ||
| 985 | = @$self{qw'curstash warnings hints hinthash'}; | ||
| 986 | |||
| 987 | # Now deparse subroutine body | ||
| 988 | |||
| 989 | my $root = $cv->ROOT; | ||
| 990 | my ($body, $node); | ||
| 991 | |||
| 992 | local $B::overlay = {}; | ||
| 993 | if (not B::Deparse::null $root) { | ||
| 994 | $self->pessimise($root, $cv->START); | ||
| 995 | my $lineseq = $root->first; | ||
| 996 | if ($lineseq->name eq "lineseq") { | ||
| 997 | my @ops; | ||
| 998 | for(my $o=$lineseq->first; $$o; $o=$o->sibling) { | ||
| 999 | push @ops, $o; | ||
| 1000 | } | ||
| 1001 | $body = $self->lineseq($root, 0, @ops); | ||
| 1002 | my $scope_en = $self->find_scope_en($lineseq); | ||
| 1003 | } | ||
| 1004 | elsif ($start_op) { | ||
| 1005 | $body = $self->deparse($start_op, 0, $root); | ||
| 1006 | } else { | ||
| 1007 | $body = $self->deparse($root->first, 0, $root); | ||
| 1008 | } | ||
| 1009 | |||
| 1010 | my $fn_name = $cv->GV->NAME; | ||
| 1011 | $node = $self->info_from_template("sub $fn_name$proto", | ||
| 1012 | $root, | ||
| 1013 | "$proto\n%|{\n%+%c\n%-}", | ||
| 1014 | [0], [$body]); | ||
| 1015 | |||
| 1016 | $self->{optree}{$$lineseq} = $node; | ||
| 1017 | |||
| 1018 | } else { | ||
| 1019 | my $sv = $cv->const_sv; | ||
| 1020 | if ($$sv) { | ||
| 1021 | # uh-oh. inlinable sub... format it differently | ||
| 1022 | $node = $self->info_from_template('inline sub', $sv, | ||
| 1023 | "$proto\n%|{\n%+%c\n%-}", | ||
| 1024 | [0], [$self->const($sv, 0)]); | ||
| 1025 | } else { | ||
| 1026 | # XSUB? (or just a declaration) | ||
| 1027 | $node = $self->info_from_string("XSUB or sub declaration", $proto); | ||
| 1028 | } | ||
| 1029 | } | ||
| 1030 | |||
| 1031 | |||
| 1032 | # Add additional DeparseTree tracking info | ||
| 1033 | if ($start_op) { | ||
| 1034 | $node->{op} = $start_op; | ||
| 1035 | $self->{'optree'}{$$start_op} = $node; | ||
| 1036 | } | ||
| 1037 | $node->{cop} = undef; | ||
| 1038 | $node->{'parent'} = $cv; | ||
| 1039 | return $node; | ||
| 1040 | } | ||
| 1041 | |||
| 1042 | # We have a TODO list of things that must be handled | ||
| 1043 | # at the top level. There are things like | ||
| 1044 | # format statements, "BEGIN" and "use" statements. | ||
| 1045 | # Here we handle the next one. | ||
| 1046 | sub next_todo | ||
| 1047 | { | ||
| 1048 | my ($self, $parent) = @_; | ||
| 1049 | my $ent = shift @{$self->{'subs_todo'}}; | ||
| 1050 | my $cv = $ent->[1]; | ||
| 1051 | my $gv = $cv->GV; | ||
| 1052 | my $name = $self->gv_name($gv); | ||
| 1053 | if ($ent->[2]) { | ||
| 1054 | my $node = $self->deparse_format($ent->[1], $cv); | ||
| 1055 | return $self->info_from_template("format $name", | ||
| 1056 | "format $name = %c", | ||
| 1057 | undef, [$node]) | ||
| 1058 | } else { | ||
| 1059 | my ($fmt, $type); | ||
| 1060 | $self->{'subs_declared'}{$name} = 1; | ||
| 1061 | if ($name eq "BEGIN") { | ||
| 1062 | my $use_dec = $self->begin_is_use($cv); | ||
| 1063 | if (defined ($use_dec) and $self->{'expand'} < 5) { | ||
| 1064 | if (0 == length($use_dec)) { | ||
| 1065 | $self->info_from_string('BEGIN', $cv, ''); | ||
| 1066 | } else { | ||
| 1067 | $self->info_from_string('use', $cv, $use_dec); | ||
| 1068 | } | ||
| 1069 | } | ||
| 1070 | } | ||
| 1071 | my $l = ''; | ||
| 1072 | if ($self->{'linenums'}) { | ||
| 1073 | my $line = $gv->LINE; | ||
| 1074 | my $file = $gv->FILE; | ||
| 1075 | $l = "\n# line $line \"$file\"\n"; | ||
| 1076 | } | ||
| 1077 | if (class($cv->STASH) ne "SPECIAL") { | ||
| 1078 | my $stash = $cv->STASH->NAME; | ||
| 1079 | if ($stash ne $self->{'curstash'}) { | ||
| 1080 | $fmt = "package $stash;\n"; | ||
| 1081 | $type = "package $stash"; | ||
| 1082 | $name = "$self->{'curstash'}::$name" unless $name =~ /::/; | ||
| 1083 | $self->{'curstash'} = $stash; | ||
| 1084 | } | ||
| 1085 | $name =~ s/^\Q$stash\E::(?!\z|.*::)//; | ||
| 1086 | $fmt .= "sub $name"; | ||
| 1087 | $type .= "sub $name"; | ||
| 1088 | } | ||
| 1089 | my $node = $self->deparse_sub($cv, $parent); | ||
| 1090 | $fmt .= '%c'; | ||
| 1091 | return $self->info_from_template($type, $cv, $fmt, [0], [$node]); | ||
| 1092 | } | ||
| 1093 | } | ||
| 1094 | |||
| 1095 | # Deparse a subroutine by name | ||
| 1096 | sub deparse_subname($$) | ||
| 1097 | { | ||
| 1098 | my ($self, $funcname) = @_; | ||
| 1099 | my $cv = svref_2object(\&$funcname); | ||
| 1100 | my $info = $self->deparse_sub($cv); | ||
| 1101 | return $self->info_from_template("sub $funcname", $cv, "sub $funcname %c", | ||
| 1102 | undef, [$info]); | ||
| 1103 | } | ||
| 1104 | |||
| 1105 | # Return a list of info nodes for "use" and "no" pragmas. | ||
| 1106 | sub declare_hints | ||
| 1107 | { | ||
| 1108 | my ($self, $from, $to) = @_; | ||
| 1109 | my $use = $to & ~$from; | ||
| 1110 | my $no = $from & ~$to; | ||
| 1111 | |||
| 1112 | my @decls = (); | ||
| 1113 | for my $pragma (B::Deparse::hint_pragmas($use)) { | ||
| 1114 | my $type = $self->keyword("use") . " $pragma"; | ||
| 1115 | push @decls, $self->info_from_template($type, undef, "$type", [], []); | ||
| 1116 | } | ||
| 1117 | for my $pragma (B::Deparse::hint_pragmas($no)) { | ||
| 1118 | my $type = $self->keyword("no") . " $pragma"; | ||
| 1119 | push @decls, $self->info_from_template($type, undef, "$type", [], []); | ||
| 1120 | } | ||
| 1121 | return @decls; | ||
| 1122 | } | ||
| 1123 | |||
| 1124 | # Internal implementation hints that the core sets automatically, so don't need | ||
| 1125 | # (or want) to be passed back to the user | ||
| 1126 | my %ignored_hints = ( | ||
| 1127 | 'open<' => 1, | ||
| 1128 | 'open>' => 1, | ||
| 1129 | ':' => 1, | ||
| 1130 | 'strict/refs' => 1, | ||
| 1131 | 'strict/subs' => 1, | ||
| 1132 | 'strict/vars' => 1, | ||
| 1133 | ); | ||
| 1134 | |||
| 1135 | my %rev_feature; | ||
| 1136 | |||
| 1137 | sub declare_hinthash { | ||
| 1138 | my ($self, $from, $to, $indent, $hints) = @_; | ||
| 1139 | my $doing_features = | ||
| 1140 | ($hints & $feature::hint_mask) == $feature::hint_mask; | ||
| 1141 | my @decls; | ||
| 1142 | my @features; | ||
| 1143 | my @unfeatures; # bugs? | ||
| 1144 | for my $key (sort keys %$to) { | ||
| 1145 | next if $ignored_hints{$key}; | ||
| 1146 | my $is_feature = $key =~ /^feature_/ && $^V ge 5.15.6; | ||
| 1147 | next if $is_feature and not $doing_features; | ||
| 1148 | if (!exists $from->{$key} or $from->{$key} ne $to->{$key}) { | ||
| 1149 | push(@features, $key), next if $is_feature; | ||
| 1150 | push @decls, | ||
| 1151 | qq(\$^H{) . single_delim($self, "q", "'", $key, "'") . qq(} = ) | ||
| 1152 | . ( | ||
| 1153 | defined $to->{$key} | ||
| 1154 | ? single_delim($self, "q", "'", $to->{$key}, "'") | ||
| 1155 | : 'undef' | ||
| 1156 | ) | ||
| 1157 | . qq(;); | ||
| 1158 | } | ||
| 1159 | } | ||
| 1160 | for my $key (sort keys %$from) { | ||
| 1161 | next if $ignored_hints{$key}; | ||
| 1162 | my $is_feature = $key =~ /^feature_/ && $^V ge 5.15.6; | ||
| 1163 | next if $is_feature and not $doing_features; | ||
| 1164 | if (!exists $to->{$key}) { | ||
| 1165 | push(@unfeatures, $key), next if $is_feature; | ||
| 1166 | push @decls, qq(delete \$^H{'$key'};); | ||
| 1167 | } | ||
| 1168 | } | ||
| 1169 | my @ret; | ||
| 1170 | if (@features || @unfeatures) { | ||
| 1171 | if (!%rev_feature) { %rev_feature = reverse %feature::feature } | ||
| 1172 | } | ||
| 1173 | if (@features) { | ||
| 1174 | push @ret, $self->keyword("use") . " feature " | ||
| 1175 | . join(", ", map "'$rev_feature{$_}'", @features) . ";\n"; | ||
| 1176 | } | ||
| 1177 | if (@unfeatures) { | ||
| 1178 | push @ret, $self->keyword("no") . " feature " | ||
| 1179 | . join(", ", map "'$rev_feature{$_}'", @unfeatures) | ||
| 1180 | . ";\n"; | ||
| 1181 | } | ||
| 1182 | @decls and | ||
| 1183 | push @ret, | ||
| 1184 | join("\n" . (" " x $indent), "BEGIN {", @decls) . "\n}\n"; | ||
| 1185 | return @ret; | ||
| 1186 | } | ||
| 1187 | |||
| 1188 | # generate any pragmas, 'package foo' etc needed to synchronise | ||
| 1189 | # with the given cop | ||
| 1190 | |||
| 1191 | sub pragmata { | ||
| 1192 | my $self = shift; | ||
| 1193 | my($op) = @_; | ||
| 1194 | |||
| 1195 | my @text; | ||
| 1196 | |||
| 1197 | my $stash = $op->stashpv; | ||
| 1198 | if ($stash ne $self->{'curstash'}) { | ||
| 1199 | push @text, $self->keyword("package") . " $stash;\n"; | ||
| 1200 | $self->{'curstash'} = $stash; | ||
| 1201 | } | ||
| 1202 | |||
| 1203 | if (OPpCONST_ARYBASE && $self->{'arybase'} != $op->arybase) { | ||
| 1204 | push @text, '$[ = '. $op->arybase .";\n"; | ||
| 1205 | $self->{'arybase'} = $op->arybase; | ||
| 1206 | } | ||
| 1207 | |||
| 1208 | my $warnings = $op->warnings; | ||
| 1209 | my $warning_bits; | ||
| 1210 | if ($warnings->isa("B::SPECIAL") && $$warnings == 4) { | ||
| 1211 | $warning_bits = $warnings::Bits{"all"} & WARN_MASK; | ||
| 1212 | } | ||
| 1213 | elsif ($warnings->isa("B::SPECIAL") && $$warnings == 5) { | ||
| 1214 | $warning_bits = $warnings::NONE; | ||
| 1215 | } | ||
| 1216 | elsif ($warnings->isa("B::SPECIAL")) { | ||
| 1217 | $warning_bits = undef; | ||
| 1218 | } | ||
| 1219 | else { | ||
| 1220 | $warning_bits = $warnings->PV & WARN_MASK; | ||
| 1221 | } | ||
| 1222 | |||
| 1223 | if (defined ($warning_bits) and | ||
| 1224 | !defined($self->{warnings}) || $self->{'warnings'} ne $warning_bits) { | ||
| 1225 | push @text, | ||
| 1226 | $self->declare_warnings($self->{'warnings'}, $warning_bits); | ||
| 1227 | $self->{'warnings'} = $warning_bits; | ||
| 1228 | } | ||
| 1229 | |||
| 1230 | my $hints = $] < 5.008009 ? $op->private : $op->hints; | ||
| 1231 | my $old_hints = $self->{'hints'}; | ||
| 1232 | if ($self->{'hints'} != $hints) { | ||
| 1233 | push @text, $self->declare_hints($self->{'hints'}, $hints); | ||
| 1234 | $self->{'hints'} = $hints; | ||
| 1235 | } | ||
| 1236 | |||
| 1237 | my $newhh; | ||
| 1238 | if ($] > 5.009) { | ||
| 1239 | $newhh = $op->hints_hash->HASH; | ||
| 1240 | } | ||
| 1241 | |||
| 1242 | if ($] >= 5.015006) { | ||
| 1243 | # feature bundle hints | ||
| 1244 | my $from = $old_hints & $feature::hint_mask; | ||
| 1245 | my $to = $ hints & $feature::hint_mask; | ||
| 1246 | if ($from != $to) { | ||
| 1247 | if ($to == $feature::hint_mask) { | ||
| 1248 | if ($self->{'hinthash'}) { | ||
| 1249 | delete $self->{'hinthash'}{$_} | ||
| 1250 | for grep /^feature_/, keys %{$self->{'hinthash'}}; | ||
| 1251 | } | ||
| 1252 | else { $self->{'hinthash'} = {} } | ||
| 1253 | $self->{'hinthash'} | ||
| 1254 | = _features_from_bundle($from, $self->{'hinthash'}); | ||
| 1255 | } | ||
| 1256 | else { | ||
| 1257 | my $bundle = | ||
| 1258 | $feature::hint_bundles[$to >> $feature::hint_shift]; | ||
| 1259 | $bundle =~ s/(\d[13579])\z/$1+1/e; # 5.11 => 5.12 | ||
| 1260 | push @text, | ||
| 1261 | $self->keyword("no") . " feature ':all';\n", | ||
| 1262 | $self->keyword("use") . " feature ':$bundle';\n"; | ||
| 1263 | } | ||
| 1264 | } | ||
| 1265 | } | ||
| 1266 | |||
| 1267 | if ($] > 5.009) { | ||
| 1268 | push @text, $self->declare_hinthash( | ||
| 1269 | $self->{'hinthash'}, $newhh, | ||
| 1270 | $self->{indent_size}, $self->{hints}, | ||
| 1271 | ); | ||
| 1272 | $self->{'hinthash'} = $newhh; | ||
| 1273 | } | ||
| 1274 | |||
| 1275 | return join("", @text); | ||
| 1276 | } | ||
| 1277 | |||
| 1278 | |||
| 1279 | # Create a "use", "no", or "BEGIN" block to set warnings. | ||
| 1280 | sub declare_warnings | ||
| 1281 | { | ||
| 1282 | my ($self, $from, $to) = @_; | ||
| 1283 | if (($to & WARN_MASK) eq (warnings::bits("all") & WARN_MASK)) { | ||
| 1284 | my $type = $self->keyword("use") . " warnings"; | ||
| 1285 | return $self->info_from_template($type, undef, "$type;\n", | ||
| 1286 | [], []); | ||
| 1287 | } | ||
| 1288 | elsif (($to & WARN_MASK) eq ("\0"x length($to) & WARN_MASK)) { | ||
| 1289 | my $type = $self->keyword("no") . " warnings"; | ||
| 1290 | return $self->info_from_template($type, undef, "$type;\n", | ||
| 1291 | [], []); | ||
| 1292 | } | ||
| 1293 | my $bit_expr = join('', map { sprintf("\\x%02x", ord $_) } split "", $to); | ||
| 1294 | my $str = "BEGIN {\n%+\${^WARNING_BITS} = \"$bit_expr;\n%-"; | ||
| 1295 | return $self->info_from_template('warning bits begin', undef, | ||
| 1296 | "%|$str\n", [], [], {omit_next_semicolon=>1}); | ||
| 1297 | } | ||
| 1298 | |||
| 1299 | # Iterate over $self->{subs_todo} picking up the | ||
| 1300 | # text of of $self->next_todo. | ||
| 1301 | # We return an array of strings. The calling | ||
| 1302 | # routine will join these together | ||
| 1303 | sub seq_subs { | ||
| 1304 | my ($self, $seq) = @_; | ||
| 1305 | my @texts; | ||
| 1306 | |||
| 1307 | return () if !defined $seq; | ||
| 1308 | my @pending; | ||
| 1309 | while (scalar(@{$self->{'subs_todo'}}) | ||
| 1310 | and $seq > $self->{'subs_todo'}[0][0]) { | ||
| 1311 | my $cv = $self->{'subs_todo'}[0][1]; | ||
| 1312 | # Skip the OUTSIDE check for lexical subs. We may be deparsing a | ||
| 1313 | # cloned anon sub with lexical subs declared in it, in which case | ||
| 1314 | # the OUTSIDE pointer points to the anon protosub. | ||
| 1315 | my $lexical = ref $self->{'subs_todo'}[0][3]; | ||
| 1316 | my $outside = !$lexical && $cv && $cv->OUTSIDE; | ||
| 1317 | if (!$lexical and $cv | ||
| 1318 | and ${$cv->OUTSIDE || \0} != ${$self->{'curcv'}}) | ||
| 1319 | { | ||
| 1320 | # rocky: What do we do with @pending? | ||
| 1321 | push @pending, shift @{$self->{'subs_todo'}}; | ||
| 1322 | next; | ||
| 1323 | } | ||
| 1324 | push @texts, $self->next_todo; | ||
| 1325 | } | ||
| 1326 | return @texts; | ||
| 1327 | } | ||
| 1328 | |||
| 1329 | # FIXME: this code has to be here. Find out why and fix. | ||
| 1330 | # Truncate is special because OPf_SPECIAL makes a bareword first arg | ||
| 1331 | # be a filehandle. This could probably be better fixed in the core | ||
| 1332 | # by moving the GV lookup into ck_truc. | ||
| 1333 | |||
| 1334 | # Demo code | ||
| 1335 | unless(caller) { | ||
| 1336 | my @texts = ('a', 'b', 'c'); | ||
| 1337 | my $deparse = __PACKAGE__->new(); | ||
| 1338 | my $info = info_from_list('op', $deparse, \@texts, ', ', 'test', {}); | ||
| 1339 | |||
| 1340 | use Data::Printer; | ||
| 1341 | my $str = $deparse->template_engine("%c", [0], ["16"]); | ||
| 1342 | p $str; | ||
| 1343 | my $str2 = $deparse->template_engine("%F", [[0, sub {'0x' . sprintf "%x", shift}]], [$str]); | ||
| 1344 | p $str2; | ||
| 1345 | |||
| 1346 | # print $deparse->template_engine("100%% "), "\n"; | ||
| 1347 | # print $deparse->template_engine("%c,\n%+%c\n%|%c %c!", | ||
| 1348 | # [1, 0, 2, 3], | ||
| 1349 | # ["is", "now", "the", "time"]), "\n"; | ||
| 1350 | |||
| 1351 | # $info = $deparse->info_from_template("demo", undef, "%C", | ||
| 1352 | # [[0, 1, ";\n%|"]], | ||
| 1353 | # ['$x=1', '$y=2']); | ||
| 1354 | |||
| 1355 | # @texts = ("use warnings;", "use strict", "my(\$a)"); | ||
| 1356 | # $info = $deparse->info_from_template("demo", undef, "%;", [], \@texts); | ||
| 1357 | |||
| 1358 | # $info = $deparse->info_from_template("list", undef, | ||
| 1359 | # "%C", [[0, $#texts, ', ']], | ||
| 1360 | # \@texts); | ||
| 1361 | |||
| 1362 | # p $info; | ||
| 1363 | |||
| 1364 | |||
| 1365 | # @texts = (['a', 1], ['b', 2], 'c'); | ||
| 1366 | # $info = info_from_list('op', $deparse, \@texts, ', ', 'test', {}); | ||
| 1367 | # p $info; | ||
| 1368 | } | ||
| 1369 | |||
| 1370 | 1; |