Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2670 lines
75 KiB

  1. # B::Deparse.pm
  2. # Copyright (c) 1998 Stephen McCamant. All rights reserved.
  3. # This module is free software; you can redistribute and/or modify
  4. # it under the same terms as Perl itself.
  5. # This is based on the module of the same name by Malcolm Beattie,
  6. # but essentially none of his code remains.
  7. package B::Deparse;
  8. use Carp 'cluck';
  9. use B qw(class main_root main_start main_cv svref_2object);
  10. $VERSION = 0.56;
  11. use strict;
  12. # Changes between 0.50 and 0.51:
  13. # - fixed nulled leave with live enter in sort { }
  14. # - fixed reference constants (\"str")
  15. # - handle empty programs gracefully
  16. # - handle infinte loops (for (;;) {}, while (1) {})
  17. # - differentiate between `for my $x ...' and `my $x; for $x ...'
  18. # - various minor cleanups
  19. # - moved globals into an object
  20. # - added `-u', like B::C
  21. # - package declarations using cop_stash
  22. # - subs, formats and code sorted by cop_seq
  23. # Changes between 0.51 and 0.52:
  24. # - added pp_threadsv (special variables under USE_THREADS)
  25. # - added documentation
  26. # Changes between 0.52 and 0.53
  27. # - many changes adding precedence contexts and associativity
  28. # - added `-p' and `-s' output style options
  29. # - various other minor fixes
  30. # Changes between 0.53 and 0.54
  31. # - added support for new `for (1..100)' optimization,
  32. # thanks to Gisle Aas
  33. # Changes between 0.54 and 0.55
  34. # - added support for new qr// construct
  35. # - added support for new pp_regcreset OP
  36. # Changes between 0.55 and 0.56
  37. # - tested on base/*.t, cmd/*.t, comp/*.t, io/*.t
  38. # - fixed $# on non-lexicals broken in last big rewrite
  39. # - added temporary fix for change in opcode of OP_STRINGIFY
  40. # - fixed problem in 0.54's for() patch in `for (@ary)'
  41. # - fixed precedence in conditional of ?:
  42. # - tweaked list paren elimination in `my($x) = @_'
  43. # - made continue-block detection trickier wrt. null ops
  44. # - fixed various prototype problems in pp_entersub
  45. # - added support for sub prototypes that never get GVs
  46. # - added unquoting for special filehandle first arg in truncate
  47. # - print doubled rv2gv (a bug) as `*{*GV}' instead of illegal `**GV'
  48. # - added semicolons at the ends of blocks
  49. # - added -l `#line' declaration option -- fixes cmd/subval.t 27,28
  50. # Todo:
  51. # - {} around variables in strings ("${var}letters")
  52. # base/lex.t 25-27
  53. # comp/term.t 11
  54. # - generate symbolic constants directly from core source
  55. # - left/right context
  56. # - avoid semis in one-statement blocks
  57. # - associativity of &&=, ||=, ?:
  58. # - ',' => '=>' (auto-unquote?)
  59. # - break long lines ("\r" as discretionary break?)
  60. # - include values of variables (e.g. set in BEGIN)
  61. # - coordinate with Data::Dumper (both directions? see previous)
  62. # - version using op_next instead of op_first/sibling?
  63. # - avoid string copies (pass arrays, one big join?)
  64. # - auto-apply `-u'?
  65. # - while{} with one-statement continue => for(; XXX; XXX) {}?
  66. # - -uPackage:: descend recursively?
  67. # - here-docs?
  68. # - <DATA>?
  69. # Tests that will always fail:
  70. # comp/redef.t -- all (redefinition happens at compile time)
  71. # Object fields (were globals):
  72. #
  73. # avoid_local:
  74. # (local($a), local($b)) and local($a, $b) have the same internal
  75. # representation but the short form looks better. We notice we can
  76. # use a large-scale local when checking the list, but need to prevent
  77. # individual locals too. This hash holds the addresses of OPs that
  78. # have already had their local-ness accounted for. The same thing
  79. # is done with my().
  80. #
  81. # curcv:
  82. # CV for current sub (or main program) being deparsed
  83. #
  84. # curstash:
  85. # name of the current package for deparsed code
  86. #
  87. # subs_todo:
  88. # array of [cop_seq, GV, is_format?] for subs and formats we still
  89. # want to deparse
  90. #
  91. # protos_todo:
  92. # as above, but [name, prototype] for subs that never got a GV
  93. #
  94. # subs_done, forms_done:
  95. # keys are addresses of GVs for subs and formats we've already
  96. # deparsed (or at least put into subs_todo)
  97. #
  98. # parens: -p
  99. # linenums: -l
  100. # cuddle: ` ' or `\n', depending on -sC
  101. # A little explanation of how precedence contexts and associativity
  102. # work:
  103. #
  104. # deparse() calls each per-op subroutine with an argument $cx (short
  105. # for context, but not the same as the cx* in the perl core), which is
  106. # a number describing the op's parents in terms of precedence, whether
  107. # they're inside an expression or at statement level, etc. (see
  108. # chart below). When ops with children call deparse on them, they pass
  109. # along their precedence. Fractional values are used to implement
  110. # associativity (`($x + $y) + $z' => `$x + $y + $y') and related
  111. # parentheses hacks. The major disadvantage of this scheme is that
  112. # it doesn't know about right sides and left sides, so say if you
  113. # assign a listop to a variable, it can't tell it's allowed to leave
  114. # the parens off the listop.
  115. # Precedences:
  116. # 26 [TODO] inside interpolation context ("")
  117. # 25 left terms and list operators (leftward)
  118. # 24 left ->
  119. # 23 nonassoc ++ --
  120. # 22 right **
  121. # 21 right ! ~ \ and unary + and -
  122. # 20 left =~ !~
  123. # 19 left * / % x
  124. # 18 left + - .
  125. # 17 left << >>
  126. # 16 nonassoc named unary operators
  127. # 15 nonassoc < > <= >= lt gt le ge
  128. # 14 nonassoc == != <=> eq ne cmp
  129. # 13 left &
  130. # 12 left | ^
  131. # 11 left &&
  132. # 10 left ||
  133. # 9 nonassoc .. ...
  134. # 8 right ?:
  135. # 7 right = += -= *= etc.
  136. # 6 left , =>
  137. # 5 nonassoc list operators (rightward)
  138. # 4 right not
  139. # 3 left and
  140. # 2 left or xor
  141. # 1 statement modifiers
  142. # 0 statement level
  143. # Nonprinting characters with special meaning:
  144. # \cS - steal parens (see maybe_parens_unop)
  145. # \n - newline and indent
  146. # \t - increase indent
  147. # \b - decrease indent (`outdent')
  148. # \f - flush left (no indent)
  149. # \cK - kill following semicolon, if any
  150. sub null {
  151. my $op = shift;
  152. return class($op) eq "NULL";
  153. }
  154. sub todo {
  155. my $self = shift;
  156. my($gv, $cv, $is_form) = @_;
  157. my $seq;
  158. if (!null($cv->START) and is_state($cv->START)) {
  159. $seq = $cv->START->cop_seq;
  160. } else {
  161. $seq = 0;
  162. }
  163. push @{$self->{'subs_todo'}}, [$seq, $gv, $is_form];
  164. }
  165. sub next_todo {
  166. my $self = shift;
  167. my $ent = shift @{$self->{'subs_todo'}};
  168. my $name = $self->gv_name($ent->[1]);
  169. if ($ent->[2]) {
  170. return "format $name =\n"
  171. . $self->deparse_format($ent->[1]->FORM). "\n";
  172. } else {
  173. return "sub $name " .
  174. $self->deparse_sub($ent->[1]->CV);
  175. }
  176. }
  177. sub OPf_KIDS () { 4 }
  178. sub walk_tree {
  179. my($op, $sub) = @_;
  180. $sub->($op);
  181. if ($op->flags & OPf_KIDS) {
  182. my $kid;
  183. for ($kid = $op->first; not null $kid; $kid = $kid->sibling) {
  184. walk_tree($kid, $sub);
  185. }
  186. }
  187. }
  188. sub walk_sub {
  189. my $self = shift;
  190. my $cv = shift;
  191. my $op = $cv->ROOT;
  192. $op = shift if null $op;
  193. return if !$op or null $op;
  194. walk_tree($op, sub {
  195. my $op = shift;
  196. if ($op->ppaddr eq "pp_gv") {
  197. if ($op->next->ppaddr eq "pp_entersub") {
  198. next if $self->{'subs_done'}{$ {$op->gv}}++;
  199. next if class($op->gv->CV) eq "SPECIAL";
  200. $self->todo($op->gv, $op->gv->CV, 0);
  201. $self->walk_sub($op->gv->CV);
  202. } elsif ($op->next->ppaddr eq "pp_enterwrite"
  203. or ($op->next->ppaddr eq "pp_rv2gv"
  204. and $op->next->next->ppaddr eq "pp_enterwrite")) {
  205. next if $self->{'forms_done'}{$ {$op->gv}}++;
  206. next if class($op->gv->FORM) eq "SPECIAL";
  207. $self->todo($op->gv, $op->gv->FORM, 1);
  208. $self->walk_sub($op->gv->FORM);
  209. }
  210. }
  211. });
  212. }
  213. sub stash_subs {
  214. my $self = shift;
  215. my $pack = shift;
  216. my(%stash, @ret);
  217. { no strict 'refs'; %stash = svref_2object(\%{$pack . "::"})->ARRAY }
  218. if ($pack eq "main") {
  219. $pack = "";
  220. } else {
  221. $pack = $pack . "::";
  222. }
  223. my($key, $val);
  224. while (($key, $val) = each %stash) {
  225. my $class = class($val);
  226. if ($class eq "PV") {
  227. # Just a prototype
  228. push @{$self->{'protos_todo'}}, [$pack . $key, $val->PV];
  229. } elsif ($class eq "IV") {
  230. # Just a name
  231. push @{$self->{'protos_todo'}}, [$pack . $key, undef];
  232. } elsif ($class eq "GV") {
  233. if (class($val->CV) ne "SPECIAL") {
  234. next if $self->{'subs_done'}{$$val}++;
  235. $self->todo($val, $val->CV, 0);
  236. $self->walk_sub($val->CV);
  237. }
  238. if (class($val->FORM) ne "SPECIAL") {
  239. next if $self->{'forms_done'}{$$val}++;
  240. $self->todo($val, $val->FORM, 1);
  241. $self->walk_sub($val->FORM);
  242. }
  243. }
  244. }
  245. }
  246. sub print_protos {
  247. my $self = shift;
  248. my $ar;
  249. my @ret;
  250. foreach $ar (@{$self->{'protos_todo'}}) {
  251. my $proto = (defined $ar->[1] ? " (". $ar->[1] . ")" : "");
  252. push @ret, "sub " . $ar->[0] . "$proto;\n";
  253. }
  254. delete $self->{'protos_todo'};
  255. return @ret;
  256. }
  257. sub style_opts {
  258. my $self = shift;
  259. my $opts = shift;
  260. my $opt;
  261. while (length($opt = substr($opts, 0, 1))) {
  262. if ($opt eq "C") {
  263. $self->{'cuddle'} = " ";
  264. }
  265. $opts = substr($opts, 1);
  266. }
  267. }
  268. sub compile {
  269. my(@args) = @_;
  270. return sub {
  271. my $self = bless {};
  272. my $arg;
  273. $self->{'subs_todo'} = [];
  274. $self->stash_subs("main");
  275. $self->{'curcv'} = main_cv;
  276. $self->{'curstash'} = "main";
  277. $self->{'cuddle'} = "\n";
  278. while ($arg = shift @args) {
  279. if (substr($arg, 0, 2) eq "-u") {
  280. $self->stash_subs(substr($arg, 2));
  281. } elsif ($arg eq "-p") {
  282. $self->{'parens'} = 1;
  283. } elsif ($arg eq "-l") {
  284. $self->{'linenums'} = 1;
  285. } elsif (substr($arg, 0, 2) eq "-s") {
  286. $self->style_opts(substr $arg, 2);
  287. }
  288. }
  289. $self->walk_sub(main_cv, main_start);
  290. print $self->print_protos;
  291. @{$self->{'subs_todo'}} =
  292. sort {$a->[0] <=> $b->[0]} @{$self->{'subs_todo'}};
  293. print indent($self->deparse(main_root, 0)), "\n" unless null main_root;
  294. my @text;
  295. while (scalar(@{$self->{'subs_todo'}})) {
  296. push @text, $self->next_todo;
  297. }
  298. print indent(join("", @text)), "\n" if @text;
  299. }
  300. }
  301. sub deparse {
  302. my $self = shift;
  303. my($op, $cx) = @_;
  304. # cluck if class($op) eq "NULL";
  305. my $meth = $op->ppaddr;
  306. return $self->$meth($op, $cx);
  307. }
  308. sub indent {
  309. my $txt = shift;
  310. my @lines = split(/\n/, $txt);
  311. my $leader = "";
  312. my $line;
  313. for $line (@lines) {
  314. if (substr($line, 0, 1) eq "\t") {
  315. $leader = $leader . " ";
  316. $line = substr($line, 1);
  317. } elsif (substr($line, 0, 1) eq "\b") {
  318. $leader = substr($leader, 0, length($leader) - 4);
  319. $line = substr($line, 1);
  320. }
  321. if (substr($line, 0, 1) eq "\f") {
  322. $line = substr($line, 1); # no indent
  323. } else {
  324. $line = $leader . $line;
  325. }
  326. $line =~ s/\cK;?//g;
  327. }
  328. return join("\n", @lines);
  329. }
  330. sub SVf_POK () {0x40000}
  331. sub deparse_sub {
  332. my $self = shift;
  333. my $cv = shift;
  334. my $proto = "";
  335. if ($cv->FLAGS & SVf_POK) {
  336. $proto = "(". $cv->PV . ") ";
  337. }
  338. local($self->{'curcv'}) = $cv;
  339. local($self->{'curstash'}) = $self->{'curstash'};
  340. if (not null $cv->ROOT) {
  341. # skip leavesub
  342. return $proto . "{\n\t" .
  343. $self->deparse($cv->ROOT->first, 0) . "\n\b}\n";
  344. } else { # XSUB?
  345. return $proto . "{}\n";
  346. }
  347. }
  348. sub deparse_format {
  349. my $self = shift;
  350. my $form = shift;
  351. my @text;
  352. local($self->{'curcv'}) = $form;
  353. local($self->{'curstash'}) = $self->{'curstash'};
  354. my $op = $form->ROOT;
  355. my $kid;
  356. $op = $op->first->first; # skip leavewrite, lineseq
  357. while (not null $op) {
  358. $op = $op->sibling; # skip nextstate
  359. my @exprs;
  360. $kid = $op->first->sibling; # skip pushmark
  361. push @text, $kid->sv->PV;
  362. $kid = $kid->sibling;
  363. for (; not null $kid; $kid = $kid->sibling) {
  364. push @exprs, $self->deparse($kid, 0);
  365. }
  366. push @text, join(", ", @exprs)."\n" if @exprs;
  367. $op = $op->sibling;
  368. }
  369. return join("", @text) . ".";
  370. }
  371. # the aassign in-common check messes up SvCUR (always setting it
  372. # to a value >= 100), but it's probably safe to assume there
  373. # won't be any NULs in the names of my() variables. (with
  374. # stash variables, I wouldn't be so sure)
  375. sub padname_fix {
  376. my $str = shift;
  377. $str = substr($str, 0, index($str, "\0")) if index($str, "\0") != -1;
  378. return $str;
  379. }
  380. sub is_scope {
  381. my $op = shift;
  382. return $op->ppaddr eq "pp_leave" || $op->ppaddr eq "pp_scope"
  383. || $op->ppaddr eq "pp_lineseq"
  384. || ($op->ppaddr eq "pp_null" && class($op) eq "UNOP"
  385. && (is_scope($op->first) || $op->first->ppaddr eq "pp_enter"));
  386. }
  387. sub is_state {
  388. my $name = $_[0]->ppaddr;
  389. return $name eq "pp_nextstate" || $name eq "pp_dbstate";
  390. }
  391. sub is_miniwhile { # check for one-line loop (`foo() while $y--')
  392. my $op = shift;
  393. return (!null($op) and null($op->sibling)
  394. and $op->ppaddr eq "pp_null" and class($op) eq "UNOP"
  395. and (($op->first->ppaddr =~ /^pp_(and|or)$/
  396. and $op->first->first->sibling->ppaddr eq "pp_lineseq")
  397. or ($op->first->ppaddr eq "pp_lineseq"
  398. and not null $op->first->first->sibling
  399. and $op->first->first->sibling->ppaddr eq "pp_unstack")
  400. ));
  401. }
  402. sub is_scalar {
  403. my $op = shift;
  404. return ($op->ppaddr eq "pp_rv2sv" or
  405. $op->ppaddr eq "pp_padsv" or
  406. $op->ppaddr eq "pp_gv" or # only in array/hash constructs
  407. !null($op->first) && $op->first->ppaddr eq "pp_gvsv");
  408. }
  409. sub maybe_parens {
  410. my $self = shift;
  411. my($text, $cx, $prec) = @_;
  412. if ($prec < $cx # unary ops nest just fine
  413. or $prec == $cx and $cx != 4 and $cx != 16 and $cx != 21
  414. or $self->{'parens'})
  415. {
  416. $text = "($text)";
  417. # In a unop, let parent reuse our parens; see maybe_parens_unop
  418. $text = "\cS" . $text if $cx == 16;
  419. return $text;
  420. } else {
  421. return $text;
  422. }
  423. }
  424. # same as above, but get around the `if it looks like a function' rule
  425. sub maybe_parens_unop {
  426. my $self = shift;
  427. my($name, $kid, $cx) = @_;
  428. if ($cx > 16 or $self->{'parens'}) {
  429. return "$name(" . $self->deparse($kid, 1) . ")";
  430. } else {
  431. $kid = $self->deparse($kid, 16);
  432. if (substr($kid, 0, 1) eq "\cS") {
  433. # use kid's parens
  434. return $name . substr($kid, 1);
  435. } elsif (substr($kid, 0, 1) eq "(") {
  436. # avoid looks-like-a-function trap with extra parens
  437. # (`+' can lead to ambiguities)
  438. return "$name(" . $kid . ")";
  439. } else {
  440. return "$name $kid";
  441. }
  442. }
  443. }
  444. sub maybe_parens_func {
  445. my $self = shift;
  446. my($func, $text, $cx, $prec) = @_;
  447. if ($prec <= $cx or substr($text, 0, 1) eq "(" or $self->{'parens'}) {
  448. return "$func($text)";
  449. } else {
  450. return "$func $text";
  451. }
  452. }
  453. sub OPp_LVAL_INTRO () { 128 }
  454. sub maybe_local {
  455. my $self = shift;
  456. my($op, $cx, $text) = @_;
  457. if ($op->private & OPp_LVAL_INTRO and not $self->{'avoid_local'}{$$op}) {
  458. return $self->maybe_parens_func("local", $text, $cx, 16);
  459. } else {
  460. return $text;
  461. }
  462. }
  463. sub padname_sv {
  464. my $self = shift;
  465. my $targ = shift;
  466. return (($self->{'curcv'}->PADLIST->ARRAY)[0]->ARRAY)[$targ];
  467. }
  468. sub maybe_my {
  469. my $self = shift;
  470. my($op, $cx, $text) = @_;
  471. if ($op->private & OPp_LVAL_INTRO and not $self->{'avoid_local'}{$$op}) {
  472. return $self->maybe_parens_func("my", $text, $cx, 16);
  473. } else {
  474. return $text;
  475. }
  476. }
  477. # The following OPs don't have functions:
  478. # pp_padany -- does not exist after parsing
  479. # pp_rcatline -- does not exist
  480. sub pp_enter { # see also leave
  481. cluck "unexpected OP_ENTER";
  482. return "XXX";
  483. }
  484. sub pp_pushmark { # see also list
  485. cluck "unexpected OP_PUSHMARK";
  486. return "XXX";
  487. }
  488. sub pp_leavesub { # see also deparse_sub
  489. cluck "unexpected OP_LEAVESUB";
  490. return "XXX";
  491. }
  492. sub pp_leavewrite { # see also deparse_format
  493. cluck "unexpected OP_LEAVEWRITE";
  494. return "XXX";
  495. }
  496. sub pp_method { # see also entersub
  497. cluck "unexpected OP_METHOD";
  498. return "XXX";
  499. }
  500. sub pp_regcmaybe { # see also regcomp
  501. cluck "unexpected OP_REGCMAYBE";
  502. return "XXX";
  503. }
  504. sub pp_regcreset { # see also regcomp
  505. cluck "unexpected OP_REGCRESET";
  506. return "XXX";
  507. }
  508. sub pp_substcont { # see also subst
  509. cluck "unexpected OP_SUBSTCONT";
  510. return "XXX";
  511. }
  512. sub pp_grepstart { # see also grepwhile
  513. cluck "unexpected OP_GREPSTART";
  514. return "XXX";
  515. }
  516. sub pp_mapstart { # see also mapwhile
  517. cluck "unexpected OP_MAPSTART";
  518. return "XXX";
  519. }
  520. sub pp_flip { # see also flop
  521. cluck "unexpected OP_FLIP";
  522. return "XXX";
  523. }
  524. sub pp_iter { # see also leaveloop
  525. cluck "unexpected OP_ITER";
  526. return "XXX";
  527. }
  528. sub pp_enteriter { # see also leaveloop
  529. cluck "unexpected OP_ENTERITER";
  530. return "XXX";
  531. }
  532. sub pp_enterloop { # see also leaveloop
  533. cluck "unexpected OP_ENTERLOOP";
  534. return "XXX";
  535. }
  536. sub pp_leaveeval { # see also entereval
  537. cluck "unexpected OP_LEAVEEVAL";
  538. return "XXX";
  539. }
  540. sub pp_entertry { # see also leavetry
  541. cluck "unexpected OP_ENTERTRY";
  542. return "XXX";
  543. }
  544. # leave and scope/lineseq should probably share code
  545. sub pp_leave {
  546. my $self = shift;
  547. my($op, $cx) = @_;
  548. my ($kid, $expr);
  549. my @exprs;
  550. local($self->{'curstash'}) = $self->{'curstash'};
  551. $kid = $op->first->sibling; # skip enter
  552. if (is_miniwhile($kid)) {
  553. my $top = $kid->first;
  554. my $name = $top->ppaddr;
  555. if ($name eq "pp_and") {
  556. $name = "while";
  557. } elsif ($name eq "pp_or") {
  558. $name = "until";
  559. } else { # no conditional -> while 1 or until 0
  560. return $self->deparse($top->first, 1) . " while 1";
  561. }
  562. my $cond = $top->first;
  563. my $body = $cond->sibling->first; # skip lineseq
  564. $cond = $self->deparse($cond, 1);
  565. $body = $self->deparse($body, 1);
  566. return "$body $name $cond";
  567. }
  568. for (; !null($kid); $kid = $kid->sibling) {
  569. $expr = "";
  570. if (is_state $kid) {
  571. $expr = $self->deparse($kid, 0);
  572. $kid = $kid->sibling;
  573. last if null $kid;
  574. }
  575. $expr .= $self->deparse($kid, 0);
  576. push @exprs, $expr if $expr;
  577. }
  578. if ($cx > 0) { # inside an expression
  579. return "do { " . join(";\n", @exprs) . " }";
  580. } else {
  581. return join(";\n", @exprs) . ";";
  582. }
  583. }
  584. sub pp_scope {
  585. my $self = shift;
  586. my($op, $cx) = @_;
  587. my ($kid, $expr);
  588. my @exprs;
  589. for ($kid = $op->first; !null($kid); $kid = $kid->sibling) {
  590. $expr = "";
  591. if (is_state $kid) {
  592. $expr = $self->deparse($kid, 0);
  593. $kid = $kid->sibling;
  594. last if null $kid;
  595. }
  596. $expr .= $self->deparse($kid, 0);
  597. push @exprs, $expr if $expr;
  598. }
  599. if ($cx > 0) { # inside an expression, (a do {} while for lineseq)
  600. return "do { " . join(";\n", @exprs) . " }";
  601. } else {
  602. return join(";\n", @exprs) . ";";
  603. }
  604. }
  605. sub pp_lineseq { pp_scope(@_) }
  606. # The BEGIN {} is used here because otherwise this code isn't executed
  607. # when you run B::Deparse on itself.
  608. my %globalnames;
  609. BEGIN { map($globalnames{$_}++, "SIG", "STDIN", "STDOUT", "STDERR", "INC",
  610. "ENV", "ARGV", "ARGVOUT", "_"); }
  611. sub gv_name {
  612. my $self = shift;
  613. my $gv = shift;
  614. my $stash = $gv->STASH->NAME;
  615. my $name = $gv->NAME;
  616. if ($stash eq $self->{'curstash'} or $globalnames{$name}
  617. or $name =~ /^[^A-Za-z_]/)
  618. {
  619. $stash = "";
  620. } else {
  621. $stash = $stash . "::";
  622. }
  623. if ($name =~ /^([\cA-\cZ])$/) {
  624. $name = "^" . chr(64 + ord($1));
  625. }
  626. return $stash . $name;
  627. }
  628. # Notice how subs and formats are inserted between statements here
  629. sub pp_nextstate {
  630. my $self = shift;
  631. my($op, $cx) = @_;
  632. my @text;
  633. @text = $op->label . ": " if $op->label;
  634. my $seq = $op->cop_seq;
  635. while (scalar(@{$self->{'subs_todo'}})
  636. and $seq > $self->{'subs_todo'}[0][0]) {
  637. push @text, $self->next_todo;
  638. }
  639. my $stash = $op->stash->NAME;
  640. if ($stash ne $self->{'curstash'}) {
  641. push @text, "package $stash;\n";
  642. $self->{'curstash'} = $stash;
  643. }
  644. if ($self->{'linenums'}) {
  645. push @text, "\f#line " . $op->line .
  646. ' "' . substr($op->filegv->NAME, 2), qq'"\n';
  647. }
  648. return join("", @text);
  649. }
  650. sub pp_dbstate { pp_nextstate(@_) }
  651. sub pp_unstack { return "" } # see also leaveloop
  652. sub baseop {
  653. my $self = shift;
  654. my($op, $cx, $name) = @_;
  655. return $name;
  656. }
  657. sub pp_stub { baseop(@_, "()") }
  658. sub pp_wantarray { baseop(@_, "wantarray") }
  659. sub pp_fork { baseop(@_, "fork") }
  660. sub pp_wait { baseop(@_, "wait") }
  661. sub pp_getppid { baseop(@_, "getppid") }
  662. sub pp_time { baseop(@_, "time") }
  663. sub pp_tms { baseop(@_, "times") }
  664. sub pp_ghostent { baseop(@_, "gethostent") }
  665. sub pp_gnetent { baseop(@_, "getnetent") }
  666. sub pp_gprotoent { baseop(@_, "getprotoent") }
  667. sub pp_gservent { baseop(@_, "getservent") }
  668. sub pp_ehostent { baseop(@_, "endhostent") }
  669. sub pp_enetent { baseop(@_, "endnetent") }
  670. sub pp_eprotoent { baseop(@_, "endprotoent") }
  671. sub pp_eservent { baseop(@_, "endservent") }
  672. sub pp_gpwent { baseop(@_, "getpwent") }
  673. sub pp_spwent { baseop(@_, "setpwent") }
  674. sub pp_epwent { baseop(@_, "endpwent") }
  675. sub pp_ggrent { baseop(@_, "getgrent") }
  676. sub pp_sgrent { baseop(@_, "setgrent") }
  677. sub pp_egrent { baseop(@_, "endgrent") }
  678. sub pp_getlogin { baseop(@_, "getlogin") }
  679. sub POSTFIX () { 1 }
  680. # I couldn't think of a good short name, but this is the category of
  681. # symbolic unary operators with interesting precedence
  682. sub pfixop {
  683. my $self = shift;
  684. my($op, $cx, $name, $prec, $flags) = (@_, 0);
  685. my $kid = $op->first;
  686. $kid = $self->deparse($kid, $prec);
  687. return $self->maybe_parens(($flags & POSTFIX) ? "$kid$name" : "$name$kid",
  688. $cx, $prec);
  689. }
  690. sub pp_preinc { pfixop(@_, "++", 23) }
  691. sub pp_predec { pfixop(@_, "--", 23) }
  692. sub pp_postinc { pfixop(@_, "++", 23, POSTFIX) }
  693. sub pp_postdec { pfixop(@_, "--", 23, POSTFIX) }
  694. sub pp_i_preinc { pfixop(@_, "++", 23) }
  695. sub pp_i_predec { pfixop(@_, "--", 23) }
  696. sub pp_i_postinc { pfixop(@_, "++", 23, POSTFIX) }
  697. sub pp_i_postdec { pfixop(@_, "--", 23, POSTFIX) }
  698. sub pp_complement { pfixop(@_, "~", 21) }
  699. sub pp_negate {
  700. my $self = shift;
  701. my($op, $cx) = @_;
  702. if ($op->first->ppaddr =~ /^pp_(i_)?negate$/) {
  703. # avoid --$x
  704. $self->pfixop($op, $cx, "-", 21.5);
  705. } else {
  706. $self->pfixop($op, $cx, "-", 21);
  707. }
  708. }
  709. sub pp_i_negate { pp_negate(@_) }
  710. sub pp_not {
  711. my $self = shift;
  712. my($op, $cx) = @_;
  713. if ($cx <= 4) {
  714. $self->pfixop($op, $cx, "not ", 4);
  715. } else {
  716. $self->pfixop($op, $cx, "!", 21);
  717. }
  718. }
  719. sub OPf_SPECIAL () { 128 }
  720. sub unop {
  721. my $self = shift;
  722. my($op, $cx, $name, $prec, $flags) = (@_, 0, 0);
  723. my $kid;
  724. if ($op->flags & OPf_KIDS) {
  725. $kid = $op->first;
  726. return $self->maybe_parens_unop($name, $kid, $cx);
  727. } else {
  728. return $name . ($op->flags & OPf_SPECIAL ? "()" : "");
  729. }
  730. }
  731. sub pp_chop { unop(@_, "chop") }
  732. sub pp_chomp { unop(@_, "chomp") }
  733. sub pp_schop { unop(@_, "chop") }
  734. sub pp_schomp { unop(@_, "chomp") }
  735. sub pp_defined { unop(@_, "defined") }
  736. sub pp_undef { unop(@_, "undef") }
  737. sub pp_study { unop(@_, "study") }
  738. sub pp_ref { unop(@_, "ref") }
  739. sub pp_pos { maybe_local(@_, unop(@_, "pos")) }
  740. sub pp_sin { unop(@_, "sin") }
  741. sub pp_cos { unop(@_, "cos") }
  742. sub pp_rand { unop(@_, "rand") }
  743. sub pp_srand { unop(@_, "srand") }
  744. sub pp_exp { unop(@_, "exp") }
  745. sub pp_log { unop(@_, "log") }
  746. sub pp_sqrt { unop(@_, "sqrt") }
  747. sub pp_int { unop(@_, "int") }
  748. sub pp_hex { unop(@_, "hex") }
  749. sub pp_oct { unop(@_, "oct") }
  750. sub pp_abs { unop(@_, "abs") }
  751. sub pp_length { unop(@_, "length") }
  752. sub pp_ord { unop(@_, "ord") }
  753. sub pp_chr { unop(@_, "chr") }
  754. sub pp_ucfirst { unop(@_, "ucfirst") }
  755. sub pp_lcfirst { unop(@_, "lcfirst") }
  756. sub pp_uc { unop(@_, "uc") }
  757. sub pp_lc { unop(@_, "lc") }
  758. sub pp_quotemeta { unop(@_, "quotemeta") }
  759. sub pp_each { unop(@_, "each") }
  760. sub pp_values { unop(@_, "values") }
  761. sub pp_keys { unop(@_, "keys") }
  762. sub pp_pop { unop(@_, "pop") }
  763. sub pp_shift { unop(@_, "shift") }
  764. sub pp_caller { unop(@_, "caller") }
  765. sub pp_reset { unop(@_, "reset") }
  766. sub pp_exit { unop(@_, "exit") }
  767. sub pp_prototype { unop(@_, "prototype") }
  768. sub pp_close { unop(@_, "close") }
  769. sub pp_fileno { unop(@_, "fileno") }
  770. sub pp_umask { unop(@_, "umask") }
  771. sub pp_binmode { unop(@_, "binmode") }
  772. sub pp_untie { unop(@_, "untie") }
  773. sub pp_tied { unop(@_, "tied") }
  774. sub pp_dbmclose { unop(@_, "dbmclose") }
  775. sub pp_getc { unop(@_, "getc") }
  776. sub pp_eof { unop(@_, "eof") }
  777. sub pp_tell { unop(@_, "tell") }
  778. sub pp_getsockname { unop(@_, "getsockname") }
  779. sub pp_getpeername { unop(@_, "getpeername") }
  780. sub pp_chdir { unop(@_, "chdir") }
  781. sub pp_chroot { unop(@_, "chroot") }
  782. sub pp_readlink { unop(@_, "readlink") }
  783. sub pp_rmdir { unop(@_, "rmdir") }
  784. sub pp_readdir { unop(@_, "readdir") }
  785. sub pp_telldir { unop(@_, "telldir") }
  786. sub pp_rewinddir { unop(@_, "rewinddir") }
  787. sub pp_closedir { unop(@_, "closedir") }
  788. sub pp_getpgrp { unop(@_, "getpgrp") }
  789. sub pp_localtime { unop(@_, "localtime") }
  790. sub pp_gmtime { unop(@_, "gmtime") }
  791. sub pp_alarm { unop(@_, "alarm") }
  792. sub pp_sleep { unop(@_, "sleep") }
  793. sub pp_dofile { unop(@_, "do") }
  794. sub pp_entereval { unop(@_, "eval") }
  795. sub pp_ghbyname { unop(@_, "gethostbyname") }
  796. sub pp_gnbyname { unop(@_, "getnetbyname") }
  797. sub pp_gpbyname { unop(@_, "getprotobyname") }
  798. sub pp_shostent { unop(@_, "sethostent") }
  799. sub pp_snetent { unop(@_, "setnetent") }
  800. sub pp_sprotoent { unop(@_, "setprotoent") }
  801. sub pp_sservent { unop(@_, "setservent") }
  802. sub pp_gpwnam { unop(@_, "getpwnam") }
  803. sub pp_gpwuid { unop(@_, "getpwuid") }
  804. sub pp_ggrnam { unop(@_, "getgrnam") }
  805. sub pp_ggrgid { unop(@_, "getgrgid") }
  806. sub pp_lock { unop(@_, "lock") }
  807. sub pp_exists {
  808. my $self = shift;
  809. my($op, $cx) = @_;
  810. return $self->maybe_parens_func("exists", $self->pp_helem($op->first, 16),
  811. $cx, 16);
  812. }
  813. sub OPpSLICE () { 64 }
  814. sub pp_delete {
  815. my $self = shift;
  816. my($op, $cx) = @_;
  817. my $arg;
  818. if ($op->private & OPpSLICE) {
  819. return $self->maybe_parens_func("delete",
  820. $self->pp_hslice($op->first, 16),
  821. $cx, 16);
  822. } else {
  823. return $self->maybe_parens_func("delete",
  824. $self->pp_helem($op->first, 16),
  825. $cx, 16);
  826. }
  827. }
  828. sub OPp_CONST_BARE () { 64 }
  829. sub pp_require {
  830. my $self = shift;
  831. my($op, $cx) = @_;
  832. if (class($op) eq "UNOP" and $op->first->ppaddr eq "pp_const"
  833. and $op->first->private & OPp_CONST_BARE)
  834. {
  835. my $name = $op->first->sv->PV;
  836. $name =~ s[/][::]g;
  837. $name =~ s/\.pm//g;
  838. return "require($name)";
  839. } else {
  840. $self->unop($op, $cx, "require");
  841. }
  842. }
  843. sub pp_scalar {
  844. my $self = shift;
  845. my($op, $cv) = @_;
  846. my $kid = $op->first;
  847. if (not null $kid->sibling) {
  848. # XXX Was a here-doc
  849. return $self->dquote($op);
  850. }
  851. $self->unop(@_, "scalar");
  852. }
  853. sub padval {
  854. my $self = shift;
  855. my $targ = shift;
  856. return (($self->{'curcv'}->PADLIST->ARRAY)[1]->ARRAY)[$targ];
  857. }
  858. sub OPf_REF () { 16 }
  859. sub pp_refgen {
  860. my $self = shift;
  861. my($op, $cx) = @_;
  862. my $kid = $op->first;
  863. if ($kid->ppaddr eq "pp_null") {
  864. $kid = $kid->first;
  865. if ($kid->ppaddr eq "pp_anonlist" || $kid->ppaddr eq "pp_anonhash") {
  866. my($pre, $post) = @{{"pp_anonlist" => ["[","]"],
  867. "pp_anonhash" => ["{","}"]}->{$kid->ppaddr}};
  868. my($expr, @exprs);
  869. $kid = $kid->first->sibling; # skip pushmark
  870. for (; !null($kid); $kid = $kid->sibling) {
  871. $expr = $self->deparse($kid, 6);
  872. push @exprs, $expr;
  873. }
  874. return $pre . join(", ", @exprs) . $post;
  875. } elsif (!null($kid->sibling) and
  876. $kid->sibling->ppaddr eq "pp_anoncode") {
  877. return "sub " .
  878. $self->deparse_sub($self->padval($kid->sibling->targ));
  879. } elsif ($kid->ppaddr eq "pp_pushmark"
  880. and $kid->sibling->ppaddr =~ /^pp_(pad|rv2)[ah]v$/
  881. and not $kid->sibling->flags & OPf_REF) {
  882. # The @a in \(@a) isn't in ref context, but only when the
  883. # parens are there.
  884. return "\\(" . $self->deparse($kid->sibling, 1) . ")";
  885. }
  886. }
  887. $self->pfixop($op, $cx, "\\", 20);
  888. }
  889. sub pp_srefgen { pp_refgen(@_) }
  890. sub pp_readline {
  891. my $self = shift;
  892. my($op, $cx) = @_;
  893. my $kid = $op->first;
  894. $kid = $kid->first if $kid->ppaddr eq "pp_rv2gv"; # <$fh>
  895. if ($kid->ppaddr eq "pp_rv2gv") {
  896. $kid = $kid->first;
  897. }
  898. return "<" . $self->deparse($kid, 1) . ">";
  899. }
  900. sub loopex {
  901. my $self = shift;
  902. my ($op, $cx, $name) = @_;
  903. if (class($op) eq "PVOP") {
  904. return "$name " . $op->pv;
  905. } elsif (class($op) eq "OP") {
  906. return $name;
  907. } elsif (class($op) eq "UNOP") {
  908. # Note -- loop exits are actually exempt from the
  909. # looks-like-a-func rule, but a few extra parens won't hurt
  910. return $self->maybe_parens_unop($name, $op->first, $cx);
  911. }
  912. }
  913. sub pp_last { loopex(@_, "last") }
  914. sub pp_next { loopex(@_, "next") }
  915. sub pp_redo { loopex(@_, "redo") }
  916. sub pp_goto { loopex(@_, "goto") }
  917. sub pp_dump { loopex(@_, "dump") }
  918. sub ftst {
  919. my $self = shift;
  920. my($op, $cx, $name) = @_;
  921. if (class($op) eq "UNOP") {
  922. # Genuine `-X' filetests are exempt from the LLAFR, but not
  923. # l?stat(); for the sake of clarity, give'em all parens
  924. return $self->maybe_parens_unop($name, $op->first, $cx);
  925. } elsif (class($op) eq "GVOP") {
  926. return $self->maybe_parens_func($name, $self->pp_gv($op, 1), $cx, 16);
  927. } else { # I don't think baseop filetests ever survive ck_ftst, but...
  928. return $name;
  929. }
  930. }
  931. sub pp_lstat { ftst(@_, "lstat") }
  932. sub pp_stat { ftst(@_, "stat") }
  933. sub pp_ftrread { ftst(@_, "-R") }
  934. sub pp_ftrwrite { ftst(@_, "-W") }
  935. sub pp_ftrexec { ftst(@_, "-X") }
  936. sub pp_fteread { ftst(@_, "-r") }
  937. sub pp_ftewrite { ftst(@_, "-r") }
  938. sub pp_fteexec { ftst(@_, "-r") }
  939. sub pp_ftis { ftst(@_, "-e") }
  940. sub pp_fteowned { ftst(@_, "-O") }
  941. sub pp_ftrowned { ftst(@_, "-o") }
  942. sub pp_ftzero { ftst(@_, "-z") }
  943. sub pp_ftsize { ftst(@_, "-s") }
  944. sub pp_ftmtime { ftst(@_, "-M") }
  945. sub pp_ftatime { ftst(@_, "-A") }
  946. sub pp_ftctime { ftst(@_, "-C") }
  947. sub pp_ftsock { ftst(@_, "-S") }
  948. sub pp_ftchr { ftst(@_, "-c") }
  949. sub pp_ftblk { ftst(@_, "-b") }
  950. sub pp_ftfile { ftst(@_, "-f") }
  951. sub pp_ftdir { ftst(@_, "-d") }
  952. sub pp_ftpipe { ftst(@_, "-p") }
  953. sub pp_ftlink { ftst(@_, "-l") }
  954. sub pp_ftsuid { ftst(@_, "-u") }
  955. sub pp_ftsgid { ftst(@_, "-g") }
  956. sub pp_ftsvtx { ftst(@_, "-k") }
  957. sub pp_fttty { ftst(@_, "-t") }
  958. sub pp_fttext { ftst(@_, "-T") }
  959. sub pp_ftbinary { ftst(@_, "-B") }
  960. sub SWAP_CHILDREN () { 1 }
  961. sub ASSIGN () { 2 } # has OP= variant
  962. sub OPf_STACKED () { 64 }
  963. my(%left, %right);
  964. sub assoc_class {
  965. my $op = shift;
  966. my $name = $op->ppaddr;
  967. if ($name eq "pp_concat" and $op->first->ppaddr eq "pp_concat") {
  968. # avoid spurious `=' -- see comment in pp_concat
  969. return "pp_concat";
  970. }
  971. if ($name eq "pp_null" and class($op) eq "UNOP"
  972. and $op->first->ppaddr =~ /^pp_(and|x?or)$/
  973. and null $op->first->sibling)
  974. {
  975. # Like all conditional constructs, OP_ANDs and OP_ORs are topped
  976. # with a null that's used as the common end point of the two
  977. # flows of control. For precedence purposes, ignore it.
  978. # (COND_EXPRs have these too, but we don't bother with
  979. # their associativity).
  980. return assoc_class($op->first);
  981. }
  982. return $name . ($op->flags & OPf_STACKED ? "=" : "");
  983. }
  984. # Left associative operators, like `+', for which
  985. # $a + $b + $c is equivalent to ($a + $b) + $c
  986. BEGIN {
  987. %left = ('pp_multiply' => 19, 'pp_i_multiply' => 19,
  988. 'pp_divide' => 19, 'pp_i_divide' => 19,
  989. 'pp_modulo' => 19, 'pp_i_modulo' => 19,
  990. 'pp_repeat' => 19,
  991. 'pp_add' => 18, 'pp_i_add' => 18,
  992. 'pp_subtract' => 18, 'pp_i_subtract' => 18,
  993. 'pp_concat' => 18,
  994. 'pp_left_shift' => 17, 'pp_right_shift' => 17,
  995. 'pp_bit_and' => 13,
  996. 'pp_bit_or' => 12, 'pp_bit_xor' => 12,
  997. 'pp_and' => 3,
  998. 'pp_or' => 2, 'pp_xor' => 2,
  999. );
  1000. }
  1001. sub deparse_binop_left {
  1002. my $self = shift;
  1003. my($op, $left, $prec) = @_;
  1004. if ($left{assoc_class($op)}
  1005. and $left{assoc_class($op)} == $left{assoc_class($left)})
  1006. {
  1007. return $self->deparse($left, $prec - .00001);
  1008. } else {
  1009. return $self->deparse($left, $prec);
  1010. }
  1011. }
  1012. # Right associative operators, like `=', for which
  1013. # $a = $b = $c is equivalent to $a = ($b = $c)
  1014. BEGIN {
  1015. %right = ('pp_pow' => 22,
  1016. 'pp_sassign=' => 7, 'pp_aassign=' => 7,
  1017. 'pp_multiply=' => 7, 'pp_i_multiply=' => 7,
  1018. 'pp_divide=' => 7, 'pp_i_divide=' => 7,
  1019. 'pp_modulo=' => 7, 'pp_i_modulo=' => 7,
  1020. 'pp_repeat=' => 7,
  1021. 'pp_add=' => 7, 'pp_i_add=' => 7,
  1022. 'pp_subtract=' => 7, 'pp_i_subtract=' => 7,
  1023. 'pp_concat=' => 7,
  1024. 'pp_left_shift=' => 7, 'pp_right_shift=' => 7,
  1025. 'pp_bit_and=' => 7,
  1026. 'pp_bit_or=' => 7, 'pp_bit_xor=' => 7,
  1027. 'pp_andassign' => 7,
  1028. 'pp_orassign' => 7,
  1029. );
  1030. }
  1031. sub deparse_binop_right {
  1032. my $self = shift;
  1033. my($op, $right, $prec) = @_;
  1034. if ($right{assoc_class($op)}
  1035. and $right{assoc_class($op)} == $right{assoc_class($right)})
  1036. {
  1037. return $self->deparse($right, $prec - .00001);
  1038. } else {
  1039. return $self->deparse($right, $prec);
  1040. }
  1041. }
  1042. sub binop {
  1043. my $self = shift;
  1044. my ($op, $cx, $opname, $prec, $flags) = (@_, 0);
  1045. my $left = $op->first;
  1046. my $right = $op->last;
  1047. my $eq = "";
  1048. if ($op->flags & OPf_STACKED && $flags & ASSIGN) {
  1049. $eq = "=";
  1050. $prec = 7;
  1051. }
  1052. if ($flags & SWAP_CHILDREN) {
  1053. ($left, $right) = ($right, $left);
  1054. }
  1055. $left = $self->deparse_binop_left($op, $left, $prec);
  1056. $right = $self->deparse_binop_right($op, $right, $prec);
  1057. return $self->maybe_parens("$left $opname$eq $right", $cx, $prec);
  1058. }
  1059. sub pp_add { binop(@_, "+", 18, ASSIGN) }
  1060. sub pp_multiply { binop(@_, "*", 19, ASSIGN) }
  1061. sub pp_subtract { binop(@_, "-",18, ASSIGN) }
  1062. sub pp_divide { binop(@_, "/", 19, ASSIGN) }
  1063. sub pp_modulo { binop(@_, "%", 19, ASSIGN) }
  1064. sub pp_i_add { binop(@_, "+", 18, ASSIGN) }
  1065. sub pp_i_multiply { binop(@_, "*", 19, ASSIGN) }
  1066. sub pp_i_subtract { binop(@_, "-", 18, ASSIGN) }
  1067. sub pp_i_divide { binop(@_, "/", 19, ASSIGN) }
  1068. sub pp_i_modulo { binop(@_, "%", 19, ASSIGN) }
  1069. sub pp_pow { binop(@_, "**", 22, ASSIGN) }
  1070. sub pp_left_shift { binop(@_, "<<", 17, ASSIGN) }
  1071. sub pp_right_shift { binop(@_, ">>", 17, ASSIGN) }
  1072. sub pp_bit_and { binop(@_, "&", 13, ASSIGN) }
  1073. sub pp_bit_or { binop(@_, "|", 12, ASSIGN) }
  1074. sub pp_bit_xor { binop(@_, "^", 12, ASSIGN) }
  1075. sub pp_eq { binop(@_, "==", 14) }
  1076. sub pp_ne { binop(@_, "!=", 14) }
  1077. sub pp_lt { binop(@_, "<", 15) }
  1078. sub pp_gt { binop(@_, ">", 15) }
  1079. sub pp_ge { binop(@_, ">=", 15) }
  1080. sub pp_le { binop(@_, "<=", 15) }
  1081. sub pp_ncmp { binop(@_, "<=>", 14) }
  1082. sub pp_i_eq { binop(@_, "==", 14) }
  1083. sub pp_i_ne { binop(@_, "!=", 14) }
  1084. sub pp_i_lt { binop(@_, "<", 15) }
  1085. sub pp_i_gt { binop(@_, ">", 15) }
  1086. sub pp_i_ge { binop(@_, ">=", 15) }
  1087. sub pp_i_le { binop(@_, "<=", 15) }
  1088. sub pp_i_ncmp { binop(@_, "<=>", 14) }
  1089. sub pp_seq { binop(@_, "eq", 14) }
  1090. sub pp_sne { binop(@_, "ne", 14) }
  1091. sub pp_slt { binop(@_, "lt", 15) }
  1092. sub pp_sgt { binop(@_, "gt", 15) }
  1093. sub pp_sge { binop(@_, "ge", 15) }
  1094. sub pp_sle { binop(@_, "le", 15) }
  1095. sub pp_scmp { binop(@_, "cmp", 14) }
  1096. sub pp_sassign { binop(@_, "=", 7, SWAP_CHILDREN) }
  1097. sub pp_aassign { binop(@_, "=", 7, SWAP_CHILDREN) }
  1098. # `.' is special because concats-of-concats are optimized to save copying
  1099. # by making all but the first concat stacked. The effect is as if the
  1100. # programmer had written `($a . $b) .= $c', except legal.
  1101. sub pp_concat {
  1102. my $self = shift;
  1103. my($op, $cx) = @_;
  1104. my $left = $op->first;
  1105. my $right = $op->last;
  1106. my $eq = "";
  1107. my $prec = 18;
  1108. if ($op->flags & OPf_STACKED and $op->first->ppaddr ne "pp_concat") {
  1109. $eq = "=";
  1110. $prec = 7;
  1111. }
  1112. $left = $self->deparse_binop_left($op, $left, $prec);
  1113. $right = $self->deparse_binop_right($op, $right, $prec);
  1114. return $self->maybe_parens("$left .$eq $right", $cx, $prec);
  1115. }
  1116. # `x' is weird when the left arg is a list
  1117. sub pp_repeat {
  1118. my $self = shift;
  1119. my($op, $cx) = @_;
  1120. my $left = $op->first;
  1121. my $right = $op->last;
  1122. my $eq = "";
  1123. my $prec = 19;
  1124. if ($op->flags & OPf_STACKED) {
  1125. $eq = "=";
  1126. $prec = 7;
  1127. }
  1128. if (null($right)) { # list repeat; count is inside left-side ex-list
  1129. my $kid = $left->first->sibling; # skip pushmark
  1130. my @exprs;
  1131. for (; !null($kid->sibling); $kid = $kid->sibling) {
  1132. push @exprs, $self->deparse($kid, 6);
  1133. }
  1134. $right = $kid;
  1135. $left = "(" . join(", ", @exprs). ")";
  1136. } else {
  1137. $left = $self->deparse_binop_left($op, $left, $prec);
  1138. }
  1139. $right = $self->deparse_binop_right($op, $right, $prec);
  1140. return $self->maybe_parens("$left x$eq $right", $cx, $prec);
  1141. }
  1142. sub range {
  1143. my $self = shift;
  1144. my ($op, $cx, $type) = @_;
  1145. my $left = $op->first;
  1146. my $right = $left->sibling;
  1147. $left = $self->deparse($left, 9);
  1148. $right = $self->deparse($right, 9);
  1149. return $self->maybe_parens("$left $type $right", $cx, 9);
  1150. }
  1151. sub pp_flop {
  1152. my $self = shift;
  1153. my($op, $cx) = @_;
  1154. my $flip = $op->first;
  1155. my $type = ($flip->flags & OPf_SPECIAL) ? "..." : "..";
  1156. return $self->range($flip->first, $cx, $type);
  1157. }
  1158. # one-line while/until is handled in pp_leave
  1159. sub logop {
  1160. my $self = shift;
  1161. my ($op, $cx, $lowop, $lowprec, $highop, $highprec, $blockname) = @_;
  1162. my $left = $op->first;
  1163. my $right = $op->first->sibling;
  1164. if ($cx == 0 and is_scope($right) and $blockname) { # if ($a) {$b}
  1165. $left = $self->deparse($left, 1);
  1166. $right = $self->deparse($right, 0);
  1167. return "$blockname ($left) {\n\t$right\n\b}\cK";
  1168. } elsif ($cx == 0 and $blockname and not $self->{'parens'}) { # $b if $a
  1169. $right = $self->deparse($right, 1);
  1170. $left = $self->deparse($left, 1);
  1171. return "$right $blockname $left";
  1172. } elsif ($cx > $lowprec and $highop) { # $a && $b
  1173. $left = $self->deparse_binop_left($op, $left, $highprec);
  1174. $right = $self->deparse_binop_right($op, $right, $highprec);
  1175. return $self->maybe_parens("$left $highop $right", $cx, $highprec);
  1176. } else { # $a and $b
  1177. $left = $self->deparse_binop_left($op, $left, $lowprec);
  1178. $right = $self->deparse_binop_right($op, $right, $lowprec);
  1179. return $self->maybe_parens("$left $lowop $right", $cx, $lowprec);
  1180. }
  1181. }
  1182. sub pp_and { logop(@_, "and", 3, "&&", 11, "if") }
  1183. sub pp_or { logop(@_, "or", 2, "||", 10, "unless") }
  1184. sub pp_xor { logop(@_, "xor", 2, "", 0, "") }
  1185. sub logassignop {
  1186. my $self = shift;
  1187. my ($op, $cx, $opname) = @_;
  1188. my $left = $op->first;
  1189. my $right = $op->first->sibling->first; # skip sassign
  1190. $left = $self->deparse($left, 7);
  1191. $right = $self->deparse($right, 7);
  1192. return $self->maybe_parens("$left $opname $right", $cx, 7);
  1193. }
  1194. sub pp_andassign { logassignop(@_, "&&=") }
  1195. sub pp_orassign { logassignop(@_, "||=") }
  1196. sub listop {
  1197. my $self = shift;
  1198. my($op, $cx, $name) = @_;
  1199. my(@exprs);
  1200. my $parens = ($cx >= 5) || $self->{'parens'};
  1201. my $kid = $op->first->sibling;
  1202. return $name if null $kid;
  1203. my $first = $self->deparse($kid, 6);
  1204. $first = "+$first" if not $parens and substr($first, 0, 1) eq "(";
  1205. push @exprs, $first;
  1206. $kid = $kid->sibling;
  1207. for (; !null($kid); $kid = $kid->sibling) {
  1208. push @exprs, $self->deparse($kid, 6);
  1209. }
  1210. if ($parens) {
  1211. return "$name(" . join(", ", @exprs) . ")";
  1212. } else {
  1213. return "$name " . join(", ", @exprs);
  1214. }
  1215. }
  1216. sub pp_bless { listop(@_, "bless") }
  1217. sub pp_atan2 { listop(@_, "atan2") }
  1218. sub pp_substr { maybe_local(@_, listop(@_, "substr")) }
  1219. sub pp_vec { maybe_local(@_, listop(@_, "vec")) }
  1220. sub pp_index { listop(@_, "index") }
  1221. sub pp_rindex { listop(@_, "rindex") }
  1222. sub pp_sprintf { listop(@_, "sprintf") }
  1223. sub pp_formline { listop(@_, "formline") } # see also deparse_format
  1224. sub pp_crypt { listop(@_, "crypt") }
  1225. sub pp_unpack { listop(@_, "unpack") }
  1226. sub pp_pack { listop(@_, "pack") }
  1227. sub pp_join { listop(@_, "join") }
  1228. sub pp_splice { listop(@_, "splice") }
  1229. sub pp_push { listop(@_, "push") }
  1230. sub pp_unshift { listop(@_, "unshift") }
  1231. sub pp_reverse { listop(@_, "reverse") }
  1232. sub pp_warn { listop(@_, "warn") }
  1233. sub pp_die { listop(@_, "die") }
  1234. # Actually, return is exempt from the LLAFR (see examples in this very
  1235. # module!), but for consistency's sake, ignore that fact
  1236. sub pp_return { listop(@_, "return") }
  1237. sub pp_open { listop(@_, "open") }
  1238. sub pp_pipe_op { listop(@_, "pipe") }
  1239. sub pp_tie { listop(@_, "tie") }
  1240. sub pp_dbmopen { listop(@_, "dbmopen") }
  1241. sub pp_sselect { listop(@_, "select") }
  1242. sub pp_select { listop(@_, "select") }
  1243. sub pp_read { listop(@_, "read") }
  1244. sub pp_sysopen { listop(@_, "sysopen") }
  1245. sub pp_sysseek { listop(@_, "sysseek") }
  1246. sub pp_sysread { listop(@_, "sysread") }
  1247. sub pp_syswrite { listop(@_, "syswrite") }
  1248. sub pp_send { listop(@_, "send") }
  1249. sub pp_recv { listop(@_, "recv") }
  1250. sub pp_seek { listop(@_, "seek") }
  1251. sub pp_fcntl { listop(@_, "fcntl") }
  1252. sub pp_ioctl { listop(@_, "ioctl") }
  1253. sub pp_flock { listop(@_, "flock") }
  1254. sub pp_socket { listop(@_, "socket") }
  1255. sub pp_sockpair { listop(@_, "sockpair") }
  1256. sub pp_bind { listop(@_, "bind") }
  1257. sub pp_connect { listop(@_, "connect") }
  1258. sub pp_listen { listop(@_, "listen") }
  1259. sub pp_accept { listop(@_, "accept") }
  1260. sub pp_shutdown { listop(@_, "shutdown") }
  1261. sub pp_gsockopt { listop(@_, "getsockopt") }
  1262. sub pp_ssockopt { listop(@_, "setsockopt") }
  1263. sub pp_chown { listop(@_, "chown") }
  1264. sub pp_unlink { listop(@_, "unlink") }
  1265. sub pp_chmod { listop(@_, "chmod") }
  1266. sub pp_utime { listop(@_, "utime") }
  1267. sub pp_rename { listop(@_, "rename") }
  1268. sub pp_link { listop(@_, "link") }
  1269. sub pp_symlink { listop(@_, "symlink") }
  1270. sub pp_mkdir { listop(@_, "mkdir") }
  1271. sub pp_open_dir { listop(@_, "opendir") }
  1272. sub pp_seekdir { listop(@_, "seekdir") }
  1273. sub pp_waitpid { listop(@_, "waitpid") }
  1274. sub pp_system { listop(@_, "system") }
  1275. sub pp_exec { listop(@_, "exec") }
  1276. sub pp_kill { listop(@_, "kill") }
  1277. sub pp_setpgrp { listop(@_, "setpgrp") }
  1278. sub pp_getpriority { listop(@_, "getpriority") }
  1279. sub pp_setpriority { listop(@_, "setpriority") }
  1280. sub pp_shmget { listop(@_, "shmget") }
  1281. sub pp_shmctl { listop(@_, "shmctl") }
  1282. sub pp_shmread { listop(@_, "shmread") }
  1283. sub pp_shmwrite { listop(@_, "shmwrite") }
  1284. sub pp_msgget { listop(@_, "msgget") }
  1285. sub pp_msgctl { listop(@_, "msgctl") }
  1286. sub pp_msgsnd { listop(@_, "msgsnd") }
  1287. sub pp_msgrcv { listop(@_, "msgrcv") }
  1288. sub pp_semget { listop(@_, "semget") }
  1289. sub pp_semctl { listop(@_, "semctl") }
  1290. sub pp_semop { listop(@_, "semop") }
  1291. sub pp_ghbyaddr { listop(@_, "gethostbyaddr") }
  1292. sub pp_gnbyaddr { listop(@_, "getnetbyaddr") }
  1293. sub pp_gpbynumber { listop(@_, "getprotobynumber") }
  1294. sub pp_gsbyname { listop(@_, "getservbyname") }
  1295. sub pp_gsbyport { listop(@_, "getservbyport") }
  1296. sub pp_syscall { listop(@_, "syscall") }
  1297. sub pp_glob {
  1298. my $self = shift;
  1299. my($op, $cx) = @_;
  1300. my $text = $self->dq($op->first->sibling); # skip pushmark
  1301. if ($text =~ /^\$?(\w|::|\`)+$/ # could look like a readline
  1302. or $text =~ /[<>]/) {
  1303. return 'glob(' . single_delim('qq', '"', $text) . ')';
  1304. } else {
  1305. return '<' . $text . '>';
  1306. }
  1307. }
  1308. # Truncate is special because OPf_SPECIAL makes a bareword first arg
  1309. # be a filehandle. This could probably be better fixed in the core
  1310. # by moving the GV lookup into ck_truc.
  1311. sub pp_truncate {
  1312. my $self = shift;
  1313. my($op, $cx) = @_;
  1314. my(@exprs);
  1315. my $parens = ($cx >= 5) || $self->{'parens'};
  1316. my $kid = $op->first->sibling;
  1317. my($fh, $len);
  1318. if ($op->flags & OPf_SPECIAL) {
  1319. # $kid is an OP_CONST
  1320. $fh = $kid->sv->PV;
  1321. } else {
  1322. $fh = $self->deparse($kid, 6);
  1323. $fh = "+$fh" if not $parens and substr($fh, 0, 1) eq "(";
  1324. }
  1325. my $len = $self->deparse($kid->sibling, 6);
  1326. if ($parens) {
  1327. return "truncate($fh, $len)";
  1328. } else {
  1329. return "truncate $fh, $len";
  1330. }
  1331. }
  1332. sub indirop {
  1333. my $self = shift;
  1334. my($op, $cx, $name) = @_;
  1335. my($expr, @exprs);
  1336. my $kid = $op->first->sibling;
  1337. my $indir = "";
  1338. if ($op->flags & OPf_STACKED) {
  1339. $indir = $kid;
  1340. $indir = $indir->first; # skip rv2gv
  1341. if (is_scope($indir)) {
  1342. $indir = "{" . $self->deparse($indir, 0) . "}";
  1343. } else {
  1344. $indir = $self->deparse($indir, 24);
  1345. }
  1346. $indir = $indir . " ";
  1347. $kid = $kid->sibling;
  1348. }
  1349. for (; !null($kid); $kid = $kid->sibling) {
  1350. $expr = $self->deparse($kid, 6);
  1351. push @exprs, $expr;
  1352. }
  1353. return $self->maybe_parens_func($name,
  1354. $indir . join(", ", @exprs),
  1355. $cx, 5);
  1356. }
  1357. sub pp_prtf { indirop(@_, "printf") }
  1358. sub pp_print { indirop(@_, "print") }
  1359. sub pp_sort { indirop(@_, "sort") }
  1360. sub mapop {
  1361. my $self = shift;
  1362. my($op, $cx, $name) = @_;
  1363. my($expr, @exprs);
  1364. my $kid = $op->first; # this is the (map|grep)start
  1365. $kid = $kid->first->sibling; # skip a pushmark
  1366. my $code = $kid->first; # skip a null
  1367. if (is_scope $code) {
  1368. $code = "{" . $self->deparse($code, 1) . "} ";
  1369. } else {
  1370. $code = $self->deparse($code, 24) . ", ";
  1371. }
  1372. $kid = $kid->sibling;
  1373. for (; !null($kid); $kid = $kid->sibling) {
  1374. $expr = $self->deparse($kid, 6);
  1375. push @exprs, $expr if $expr;
  1376. }
  1377. return $self->maybe_parens_func($name, $code . join(", ", @exprs), $cx, 5);
  1378. }
  1379. sub pp_mapwhile { mapop(@_, "map") }
  1380. sub pp_grepwhile { mapop(@_, "grep") }
  1381. sub pp_list {
  1382. my $self = shift;
  1383. my($op, $cx) = @_;
  1384. my($expr, @exprs);
  1385. my $kid = $op->first->sibling; # skip pushmark
  1386. my $lop;
  1387. my $local = "either"; # could be local(...) or my(...)
  1388. for ($lop = $kid; !null($lop); $lop = $lop->sibling) {
  1389. # This assumes that no other private flags equal 128, and that
  1390. # OPs that store things other than flags in their op_private,
  1391. # like OP_AELEMFAST, won't be immediate children of a list.
  1392. unless ($lop->private & OPp_LVAL_INTRO or $lop->ppaddr eq "pp_undef")
  1393. {
  1394. $local = ""; # or not
  1395. last;
  1396. }
  1397. if ($lop->ppaddr =~ /^pp_pad[ash]v$/) { # my()
  1398. ($local = "", last) if $local eq "local";
  1399. $local = "my";
  1400. } elsif ($lop->ppaddr ne "pp_undef") { # local()
  1401. ($local = "", last) if $local eq "my";
  1402. $local = "local";
  1403. }
  1404. }
  1405. $local = "" if $local eq "either"; # no point if it's all undefs
  1406. return $self->deparse($kid, $cx) if null $kid->sibling and not $local;
  1407. for (; !null($kid); $kid = $kid->sibling) {
  1408. if ($local) {
  1409. if (class($kid) eq "UNOP" and $kid->first->ppaddr eq "pp_gvsv") {
  1410. $lop = $kid->first;
  1411. } else {
  1412. $lop = $kid;
  1413. }
  1414. $self->{'avoid_local'}{$$lop}++;
  1415. $expr = $self->deparse($kid, 6);
  1416. delete $self->{'avoid_local'}{$$lop};
  1417. } else {
  1418. $expr = $self->deparse($kid, 6);
  1419. }
  1420. push @exprs, $expr;
  1421. }
  1422. if ($local) {
  1423. return "$local(" . join(", ", @exprs) . ")";
  1424. } else {
  1425. return $self->maybe_parens( join(", ", @exprs), $cx, 6);
  1426. }
  1427. }
  1428. sub pp_cond_expr {
  1429. my $self = shift;
  1430. my($op, $cx) = @_;
  1431. my $cond = $op->first;
  1432. my $true = $cond->sibling;
  1433. my $false = $true->sibling;
  1434. my $cuddle = $self->{'cuddle'};
  1435. unless ($cx == 0 and is_scope($true) and is_scope($false)) {
  1436. $cond = $self->deparse($cond, 8);
  1437. $true = $self->deparse($true, 8);
  1438. $false = $self->deparse($false, 8);
  1439. return $self->maybe_parens("$cond ? $true : $false", $cx, 8);
  1440. }
  1441. $cond = $self->deparse($cond, 1);
  1442. $true = $self->deparse($true, 0);
  1443. if ($false->ppaddr eq "pp_lineseq") { # braces w/o scope => elsif
  1444. my $head = "if ($cond) {\n\t$true\n\b}";
  1445. my @elsifs;
  1446. while (!null($false) and $false->ppaddr eq "pp_lineseq") {
  1447. my $newop = $false->first->sibling->first;
  1448. my $newcond = $newop->first;
  1449. my $newtrue = $newcond->sibling;
  1450. $false = $newtrue->sibling; # last in chain is OP_AND => no else
  1451. $newcond = $self->deparse($newcond, 1);
  1452. $newtrue = $self->deparse($newtrue, 0);
  1453. push @elsifs, "elsif ($newcond) {\n\t$newtrue\n\b}";
  1454. }
  1455. if (!null($false)) {
  1456. $false = $cuddle . "else {\n\t" .
  1457. $self->deparse($false, 0) . "\n\b}\cK";
  1458. } else {
  1459. $false = "\cK";
  1460. }
  1461. return $head . join($cuddle, "", @elsifs) . $false;
  1462. }
  1463. $false = $self->deparse($false, 0);
  1464. return "if ($cond) {\n\t$true\n\b}${cuddle}else {\n\t$false\n\b}\cK";
  1465. }
  1466. sub pp_leaveloop {
  1467. my $self = shift;
  1468. my($op, $cx) = @_;
  1469. my $enter = $op->first;
  1470. my $kid = $enter->sibling;
  1471. local($self->{'curstash'}) = $self->{'curstash'};
  1472. my $head = "";
  1473. my $bare = 0;
  1474. if ($kid->ppaddr eq "pp_lineseq") { # bare or infinite loop
  1475. if (is_state $kid->last) { # infinite
  1476. $head = "for (;;) "; # shorter than while (1)
  1477. } else {
  1478. $bare = 1;
  1479. }
  1480. } elsif ($enter->ppaddr eq "pp_enteriter") { # foreach
  1481. my $ary = $enter->first->sibling; # first was pushmark
  1482. my $var = $ary->sibling;
  1483. if ($enter->flags & OPf_STACKED
  1484. and not null $ary->first->sibling->sibling)
  1485. {
  1486. $ary = $self->deparse($ary->first->sibling, 9) . " .. " .
  1487. $self->deparse($ary->first->sibling->sibling, 9);
  1488. } else {
  1489. $ary = $self->deparse($ary, 1);
  1490. }
  1491. if (null $var) {
  1492. if ($enter->flags & OPf_SPECIAL) { # thread special var
  1493. $var = $self->pp_threadsv($enter, 1);
  1494. } else { # regular my() variable
  1495. $var = $self->pp_padsv($enter, 1);
  1496. if ($self->padname_sv($enter->targ)->IVX ==
  1497. $kid->first->first->sibling->last->cop_seq)
  1498. {
  1499. # If the scope of this variable closes at the last
  1500. # statement of the loop, it must have been
  1501. # declared here.
  1502. $var = "my " . $var;
  1503. }
  1504. }
  1505. } elsif ($var->ppaddr eq "pp_rv2gv") {
  1506. $var = $self->pp_rv2sv($var, 1);
  1507. } elsif ($var->ppaddr eq "pp_gv") {
  1508. $var = "\$" . $self->deparse($var, 1);
  1509. }
  1510. $head = "foreach $var ($ary) ";
  1511. $kid = $kid->first->first->sibling; # skip OP_AND and OP_ITER
  1512. } elsif ($kid->ppaddr eq "pp_null") { # while/until
  1513. $kid = $kid->first;
  1514. my $name = {"pp_and" => "while", "pp_or" => "until"}
  1515. ->{$kid->ppaddr};
  1516. $head = "$name (" . $self->deparse($kid->first, 1) . ") ";
  1517. $kid = $kid->first->sibling;
  1518. } elsif ($kid->ppaddr eq "pp_stub") { # bare and empty
  1519. return "{;}"; # {} could be a hashref
  1520. }
  1521. # The third-to-last kid is the continue block if the pointer used
  1522. # by `next BLOCK' points to its first OP, which happens to be the
  1523. # the op_next of the head of the _previous_ statement.
  1524. # Unless it's a bare loop, in which case it's last, since there's
  1525. # no unstack or extra nextstate.
  1526. # Except if the previous head isn't null but the first kid is
  1527. # (because it's a nulled out nextstate in a scope), in which
  1528. # case the head's next is advanced past the null but the nextop's
  1529. # isn't, so we need to try nextop->next.
  1530. my($cont, $precont);
  1531. if ($bare) {
  1532. $cont = $kid->first;
  1533. while (!null($cont->sibling)) {
  1534. $precont = $cont;
  1535. $cont = $cont->sibling;
  1536. }
  1537. } else {
  1538. $cont = $kid->first;
  1539. while (!null($cont->sibling->sibling->sibling)) {
  1540. $precont = $cont;
  1541. $cont = $cont->sibling;
  1542. }
  1543. }
  1544. if ($precont and $ {$precont->next} == $ {$enter->nextop}
  1545. || $ {$precont->next} == $ {$enter->nextop->next} )
  1546. {
  1547. my $state = $kid->first;
  1548. my $cuddle = $self->{'cuddle'};
  1549. my($expr, @exprs);
  1550. for (; $$state != $$cont; $state = $state->sibling) {
  1551. $expr = "";
  1552. if (is_state $state) {
  1553. $expr = $self->deparse($state, 0);
  1554. $state = $state->sibling;
  1555. last if null $kid;
  1556. }
  1557. $expr .= $self->deparse($state, 0);
  1558. push @exprs, $expr if $expr;
  1559. }
  1560. $kid = join(";\n", @exprs);
  1561. $cont = $cuddle . "continue {\n\t" .
  1562. $self->deparse($cont, 0) . "\n\b}\cK";
  1563. } else {
  1564. $cont = "\cK";
  1565. $kid = $self->deparse($kid, 0);
  1566. }
  1567. return $head . "{\n\t" . $kid . "\n\b}" . $cont;
  1568. }
  1569. sub pp_leavetry {
  1570. my $self = shift;
  1571. return "eval {\n\t" . $self->pp_leave(@_) . "\n\b}";
  1572. }
  1573. sub OP_CONST () { 5 }
  1574. # XXX need a better way to do this
  1575. sub OP_STRINGIFY () { $] > 5.004_72 ? 67 : 65 }
  1576. sub pp_null {
  1577. my $self = shift;
  1578. my($op, $cx) = @_;
  1579. if (class($op) eq "OP") {
  1580. return "'???'" if $op->targ == OP_CONST; # old value is lost
  1581. } elsif ($op->first->ppaddr eq "pp_pushmark") {
  1582. return $self->pp_list($op, $cx);
  1583. } elsif ($op->first->ppaddr eq "pp_enter") {
  1584. return $self->pp_leave($op, $cx);
  1585. } elsif ($op->targ == OP_STRINGIFY) {
  1586. return $self->dquote($op);
  1587. } elsif (!null($op->first->sibling) and
  1588. $op->first->sibling->ppaddr eq "pp_readline" and
  1589. $op->first->sibling->flags & OPf_STACKED) {
  1590. return $self->maybe_parens($self->deparse($op->first, 7) . " = "
  1591. . $self->deparse($op->first->sibling, 7),
  1592. $cx, 7);
  1593. } elsif (!null($op->first->sibling) and
  1594. $op->first->sibling->ppaddr eq "pp_trans" and
  1595. $op->first->sibling->flags & OPf_STACKED) {
  1596. return $self->maybe_parens($self->deparse($op->first, 20) . " =~ "
  1597. . $self->deparse($op->first->sibling, 20),
  1598. $cx, 20);
  1599. } else {
  1600. return $self->deparse($op->first, $cx);
  1601. }
  1602. }
  1603. sub padname {
  1604. my $self = shift;
  1605. my $targ = shift;
  1606. my $str = $self->padname_sv($targ)->PV;
  1607. return padname_fix($str);
  1608. }
  1609. sub padany {
  1610. my $self = shift;
  1611. my $op = shift;
  1612. return substr($self->padname($op->targ), 1); # skip $/@/%
  1613. }
  1614. sub pp_padsv {
  1615. my $self = shift;
  1616. my($op, $cx) = @_;
  1617. return $self->maybe_my($op, $cx, $self->padname($op->targ));
  1618. }
  1619. sub pp_padav { pp_padsv(@_) }
  1620. sub pp_padhv { pp_padsv(@_) }
  1621. my @threadsv_names;
  1622. BEGIN {
  1623. @threadsv_names = ("_", "1", "2", "3", "4", "5", "6", "7", "8", "9",
  1624. "&", "`", "'", "+", "/", ".", ",", "\\", '"', ";",
  1625. "^", "-", "%", "=", "|", "~", ":", "^A", "^E",
  1626. "!", "@");
  1627. }
  1628. sub pp_threadsv {
  1629. my $self = shift;
  1630. my($op, $cx) = @_;
  1631. return $self->maybe_local($op, $cx, "\$" . $threadsv_names[$op->targ]);
  1632. }
  1633. sub pp_gvsv {
  1634. my $self = shift;
  1635. my($op, $cx) = @_;
  1636. return $self->maybe_local($op, $cx, "\$" . $self->gv_name($op->gv));
  1637. }
  1638. sub pp_gv {
  1639. my $self = shift;
  1640. my($op, $cx) = @_;
  1641. return $self->gv_name($op->gv);
  1642. }
  1643. sub pp_aelemfast {
  1644. my $self = shift;
  1645. my($op, $cx) = @_;
  1646. my $gv = $op->gv;
  1647. return "\$" . $self->gv_name($gv) . "[" . $op->private . "]";
  1648. }
  1649. sub rv2x {
  1650. my $self = shift;
  1651. my($op, $cx, $type) = @_;
  1652. my $kid = $op->first;
  1653. my $str = $self->deparse($kid, 0);
  1654. return $type . (is_scalar($kid) ? $str : "{$str}");
  1655. }
  1656. sub pp_rv2sv { maybe_local(@_, rv2x(@_, "\$")) }
  1657. sub pp_rv2hv { maybe_local(@_, rv2x(@_, "%")) }
  1658. sub pp_rv2gv { maybe_local(@_, rv2x(@_, "*")) }
  1659. # skip rv2av
  1660. sub pp_av2arylen {
  1661. my $self = shift;
  1662. my($op, $cx) = @_;
  1663. if ($op->first->ppaddr eq "pp_padav") {
  1664. return $self->maybe_local($op, $cx, '$#' . $self->padany($op->first));
  1665. } else {
  1666. return $self->maybe_local($op, $cx,
  1667. $self->rv2x($op->first, $cx, '$#'));
  1668. }
  1669. }
  1670. # skip down to the old, ex-rv2cv
  1671. sub pp_rv2cv { $_[0]->rv2x($_[1]->first->first->sibling, $_[2], "&") }
  1672. sub pp_rv2av {
  1673. my $self = shift;
  1674. my($op, $cx) = @_;
  1675. my $kid = $op->first;
  1676. if ($kid->ppaddr eq "pp_const") { # constant list
  1677. my $av = $kid->sv;
  1678. return "(" . join(", ", map(const($_), $av->ARRAY)) . ")";
  1679. } else {
  1680. return $self->maybe_local($op, $cx, $self->rv2x($op, $cx, "\@"));
  1681. }
  1682. }
  1683. sub elem {
  1684. my $self = shift;
  1685. my ($op, $cx, $left, $right, $padname) = @_;
  1686. my($array, $idx) = ($op->first, $op->first->sibling);
  1687. unless ($array->ppaddr eq $padname) { # Maybe this has been fixed
  1688. $array = $array->first; # skip rv2av (or ex-rv2av in _53+)
  1689. }
  1690. if ($array->ppaddr eq $padname) {
  1691. $array = $self->padany($array);
  1692. } elsif (is_scope($array)) { # ${expr}[0]
  1693. $array = "{" . $self->deparse($array, 0) . "}";
  1694. } elsif (is_scalar $array) { # $x[0], $$x[0], ...
  1695. $array = $self->deparse($array, 24);
  1696. } else {
  1697. # $x[20][3]{hi} or expr->[20]
  1698. my $arrow;
  1699. $arrow = "->" if $array->ppaddr !~ /^pp_[ah]elem$/;
  1700. return $self->deparse($array, 24) . $arrow .
  1701. $left . $self->deparse($idx, 1) . $right;
  1702. }
  1703. $idx = $self->deparse($idx, 1);
  1704. return "\$" . $array . $left . $idx . $right;
  1705. }
  1706. sub pp_aelem { maybe_local(@_, elem(@_, "[", "]", "pp_padav")) }
  1707. sub pp_helem { maybe_local(@_, elem(@_, "{", "}", "pp_padhv")) }
  1708. sub pp_gelem {
  1709. my $self = shift;
  1710. my($op, $cx) = @_;
  1711. my($glob, $part) = ($op->first, $op->last);
  1712. $glob = $glob->first; # skip rv2gv
  1713. $glob = $glob->first if $glob->ppaddr eq "pp_rv2gv"; # this one's a bug
  1714. my $scope = is_scope($glob);
  1715. $glob = $self->deparse($glob, 0);
  1716. $part = $self->deparse($part, 1);
  1717. return "*" . ($scope ? "{$glob}" : $glob) . "{$part}";
  1718. }
  1719. sub slice {
  1720. my $self = shift;
  1721. my ($op, $cx, $left, $right, $regname, $padname) = @_;
  1722. my $last;
  1723. my(@elems, $kid, $array, $list);
  1724. if (class($op) eq "LISTOP") {
  1725. $last = $op->last;
  1726. } else { # ex-hslice inside delete()
  1727. for ($kid = $op->first; !null $kid->sibling; $kid = $kid->sibling) {}
  1728. $last = $kid;
  1729. }
  1730. $array = $last;
  1731. $array = $array->first
  1732. if $array->ppaddr eq $regname or $array->ppaddr eq "pp_null";
  1733. if (is_scope($array)) {
  1734. $array = "{" . $self->deparse($array, 0) . "}";
  1735. } elsif ($array->ppaddr eq $padname) {
  1736. $array = $self->padany($array);
  1737. } else {
  1738. $array = $self->deparse($array, 24);
  1739. }
  1740. $kid = $op->first->sibling; # skip pushmark
  1741. if ($kid->ppaddr eq "pp_list") {
  1742. $kid = $kid->first->sibling; # skip list, pushmark
  1743. for (; !null $kid; $kid = $kid->sibling) {
  1744. push @elems, $self->deparse($kid, 6);
  1745. }
  1746. $list = join(", ", @elems);
  1747. } else {
  1748. $list = $self->deparse($kid, 1);
  1749. }
  1750. return "\@" . $array . $left . $list . $right;
  1751. }
  1752. sub pp_aslice { maybe_local(@_, slice(@_, "[", "]",
  1753. "pp_rv2av", "pp_padav")) }
  1754. sub pp_hslice { maybe_local(@_, slice(@_, "{", "}",
  1755. "pp_rv2hv", "pp_padhv")) }
  1756. sub pp_lslice {
  1757. my $self = shift;
  1758. my($op, $cx) = @_;
  1759. my $idx = $op->first;
  1760. my $list = $op->last;
  1761. my(@elems, $kid);
  1762. $list = $self->deparse($list, 1);
  1763. $idx = $self->deparse($idx, 1);
  1764. return "($list)" . "[$idx]";
  1765. }
  1766. sub OPpENTERSUB_AMPER () { 8 }
  1767. sub OPf_WANT () { 3 }
  1768. sub OPf_WANT_VOID () { 1 }
  1769. sub OPf_WANT_SCALAR () { 2 }
  1770. sub OPf_WANT_LIST () { 2 }
  1771. sub want_scalar {
  1772. my $op = shift;
  1773. return ($op->flags & OPf_WANT) == OPf_WANT_SCALAR;
  1774. }
  1775. sub pp_entersub {
  1776. my $self = shift;
  1777. my($op, $cx) = @_;
  1778. my $prefix = "";
  1779. my $amper = "";
  1780. my $proto = undef;
  1781. my $simple = 0;
  1782. my($kid, $args, @exprs);
  1783. if (not null $op->first->sibling) { # method
  1784. $kid = $op->first->sibling; # skip pushmark
  1785. my $obj = $self->deparse($kid, 24);
  1786. $kid = $kid->sibling;
  1787. for (; not null $kid->sibling; $kid = $kid->sibling) {
  1788. push @exprs, $self->deparse($kid, 6);
  1789. }
  1790. my $meth = $kid->first;
  1791. if ($meth->ppaddr eq "pp_const") {
  1792. $meth = $meth->sv->PV; # needs to be bare
  1793. } else {
  1794. $meth = $self->deparse($meth, 1);
  1795. }
  1796. $args = join(", ", @exprs);
  1797. $kid = $obj . "->" . $meth;
  1798. if ($args) {
  1799. return $kid . "(" . $args . ")"; # parens mandatory
  1800. } else {
  1801. return $kid; # toke.c fakes parens
  1802. }
  1803. }
  1804. # else, not a method
  1805. if ($op->flags & OPf_SPECIAL) {
  1806. $prefix = "do ";
  1807. } elsif ($op->private & OPpENTERSUB_AMPER) {
  1808. $amper = "&";
  1809. }
  1810. $kid = $op->first;
  1811. $kid = $kid->first->sibling; # skip ex-list, pushmark
  1812. for (; not null $kid->sibling; $kid = $kid->sibling) {
  1813. push @exprs, $kid;
  1814. }
  1815. if (is_scope($kid)) {
  1816. $amper = "&";
  1817. $kid = "{" . $self->deparse($kid, 0) . "}";
  1818. } elsif ($kid->first->ppaddr eq "pp_gv") {
  1819. my $gv = $kid->first->gv;
  1820. if (class($gv->CV) ne "SPECIAL") {
  1821. $proto = $gv->CV->PV if $gv->CV->FLAGS & SVf_POK;
  1822. }
  1823. $simple = 1;
  1824. $kid = $self->deparse($kid, 24);
  1825. } elsif (is_scalar $kid->first) {
  1826. $amper = "&";
  1827. $kid = $self->deparse($kid, 24);
  1828. } else {
  1829. $prefix = "";
  1830. $kid = $self->deparse($kid, 24) . "->";
  1831. }
  1832. if (defined $proto and not $amper) {
  1833. my($arg, $real);
  1834. my $doneok = 0;
  1835. my @args = @exprs;
  1836. my @reals;
  1837. my $p = $proto;
  1838. $p =~ s/([^\\]|^)([@%])(.*)$/$1$2/;
  1839. while ($p) {
  1840. $p =~ s/^ *([\\]?[\$\@&%*]|;)//;
  1841. my $chr = $1;
  1842. if ($chr eq "") {
  1843. undef $proto if @args;
  1844. } elsif ($chr eq ";") {
  1845. $doneok = 1;
  1846. } elsif ($chr eq "@" or $chr eq "%") {
  1847. push @reals, map($self->deparse($_, 6), @args);
  1848. @args = ();
  1849. } else {
  1850. $arg = shift @args;
  1851. last unless $arg;
  1852. if ($chr eq "\$") {
  1853. if (want_scalar $arg) {
  1854. push @reals, $self->deparse($arg, 6);
  1855. } else {
  1856. undef $proto;
  1857. }
  1858. } elsif ($chr eq "&") {
  1859. if ($arg->ppaddr =~ /pp_(s?refgen|undef)/) {
  1860. push @reals, $self->deparse($arg, 6);
  1861. } else {
  1862. undef $proto;
  1863. }
  1864. } elsif ($chr eq "*") {
  1865. if ($arg->ppaddr =~ /^pp_s?refgen$/
  1866. and $arg->first->first->ppaddr eq "pp_rv2gv")
  1867. {
  1868. $real = $arg->first->first; # skip refgen, null
  1869. if ($real->first->ppaddr eq "pp_gv") {
  1870. push @reals, $self->deparse($real, 6);
  1871. } else {
  1872. push @reals, $self->deparse($real->first, 6);
  1873. }
  1874. } else {
  1875. undef $proto;
  1876. }
  1877. } elsif (substr($chr, 0, 1) eq "\\") {
  1878. $chr = substr($chr, 1);
  1879. if ($arg->ppaddr =~ /^pp_s?refgen$/ and
  1880. !null($real = $arg->first) and
  1881. ($chr eq "\$" && is_scalar($real->first)
  1882. or ($chr eq "\@"
  1883. && $real->first->sibling->ppaddr
  1884. =~ /^pp_(rv2|pad)av$/)
  1885. or ($chr eq "%"
  1886. && $real->first->sibling->ppaddr
  1887. =~ /^pp_(rv2|pad)hv$/)
  1888. #or ($chr eq "&" # This doesn't work
  1889. # && $real->first->ppaddr eq "pp_rv2cv")
  1890. or ($chr eq "*"
  1891. && $real->first->ppaddr eq "pp_rv2gv")))
  1892. {
  1893. push @reals, $self->deparse($real, 6);
  1894. } else {
  1895. undef $proto;
  1896. }
  1897. }
  1898. }
  1899. }
  1900. undef $proto if $p and !$doneok;
  1901. undef $proto if @args;
  1902. $args = join(", ", @reals);
  1903. $amper = "";
  1904. unless (defined $proto) {
  1905. $amper = "&";
  1906. $args = join(", ", map($self->deparse($_, 6), @exprs));
  1907. }
  1908. } else {
  1909. $args = join(", ", map($self->deparse($_, 6), @exprs));
  1910. }
  1911. if ($prefix or $amper) {
  1912. if ($op->flags & OPf_STACKED) {
  1913. return $prefix . $amper . $kid . "(" . $args . ")";
  1914. } else {
  1915. return $prefix . $amper. $kid;
  1916. }
  1917. } else {
  1918. if (defined $proto and $proto eq "") {
  1919. return $kid;
  1920. } elsif ($proto eq "\$") {
  1921. return $self->maybe_parens_func($kid, $args, $cx, 16);
  1922. } elsif ($proto or $simple) {
  1923. return $self->maybe_parens_func($kid, $args, $cx, 5);
  1924. } else {
  1925. return "$kid(" . $args . ")";
  1926. }
  1927. }
  1928. }
  1929. sub pp_enterwrite { unop(@_, "write") }
  1930. # escape things that cause interpolation in double quotes,
  1931. # but not character escapes
  1932. sub uninterp {
  1933. my($str) = @_;
  1934. $str =~ s/(^|[^\\])([\$\@]|\\[uUlLQE])/$1\\$2/g;
  1935. return $str;
  1936. }
  1937. # the same, but treat $|, $), and $ at the end of the string differently
  1938. sub re_uninterp {
  1939. my($str) = @_;
  1940. $str =~ s/(^|[^\\])(\@|\\[uUlLQE])/$1\\$2/g;
  1941. $str =~ s/(^|[^\\])(\$[^)|])/$1\\$2/g;
  1942. return $str;
  1943. }
  1944. # character escapes, but not delimiters that might need to be escaped
  1945. sub escape_str { # ASCII
  1946. my($str) = @_;
  1947. $str =~ s/\a/\\a/g;
  1948. # $str =~ s/\cH/\\b/g; # \b means someting different in a regex
  1949. $str =~ s/\t/\\t/g;
  1950. $str =~ s/\n/\\n/g;
  1951. $str =~ s/\e/\\e/g;
  1952. $str =~ s/\f/\\f/g;
  1953. $str =~ s/\r/\\r/g;
  1954. $str =~ s/([\cA-\cZ])/'\\c' . chr(ord('@') + ord($1))/ge;
  1955. $str =~ s/([\0\033-\037\177-\377])/'\\' . sprintf("%03o", ord($1))/ge;
  1956. return $str;
  1957. }
  1958. # Don't do this for regexen
  1959. sub unback {
  1960. my($str) = @_;
  1961. $str =~ s/\\/\\\\/g;
  1962. return $str;
  1963. }
  1964. sub balanced_delim {
  1965. my($str) = @_;
  1966. my @str = split //, $str;
  1967. my($ar, $open, $close, $fail, $c, $cnt);
  1968. for $ar (['[',']'], ['(',')'], ['<','>'], ['{','}']) {
  1969. ($open, $close) = @$ar;
  1970. $fail = 0; $cnt = 0;
  1971. for $c (@str) {
  1972. if ($c eq $open) {
  1973. $cnt++;
  1974. } elsif ($c eq $close) {
  1975. $cnt--;
  1976. if ($cnt < 0) {
  1977. $fail = 1;
  1978. last;
  1979. }
  1980. }
  1981. }
  1982. $fail = 1 if $cnt != 0;
  1983. return ($open, "$open$str$close") if not $fail;
  1984. }
  1985. return ("", $str);
  1986. }
  1987. sub single_delim {
  1988. my($q, $default, $str) = @_;
  1989. return "$default$str$default" if $default and index($str, $default) == -1;
  1990. my($succeed, $delim);
  1991. ($succeed, $str) = balanced_delim($str);
  1992. return "$q$str" if $succeed;
  1993. for $delim ('/', '"', '#') {
  1994. return "$q$delim" . $str . $delim if index($str, $delim) == -1;
  1995. }
  1996. if ($default) {
  1997. $str =~ s/$default/\\$default/g;
  1998. return "$default$str$default";
  1999. } else {
  2000. $str =~ s[/][\\/]g;
  2001. return "$q/$str/";
  2002. }
  2003. }
  2004. sub SVf_IOK () {0x10000}
  2005. sub SVf_NOK () {0x20000}
  2006. sub SVf_ROK () {0x80000}
  2007. sub const {
  2008. my $sv = shift;
  2009. if (class($sv) eq "SPECIAL") {
  2010. return ('undef', '1', '0')[$$sv-1];
  2011. } elsif ($sv->FLAGS & SVf_IOK) {
  2012. return $sv->IV;
  2013. } elsif ($sv->FLAGS & SVf_NOK) {
  2014. return $sv->NV;
  2015. } elsif ($sv->FLAGS & SVf_ROK) {
  2016. return "\\(" . const($sv->RV) . ")"; # constant folded
  2017. } else {
  2018. my $str = $sv->PV;
  2019. if ($str =~ /[^ -~]/) { # ASCII
  2020. return single_delim("qq", '"', uninterp escape_str unback $str);
  2021. } else {
  2022. $str =~ s/\\/\\\\/g;
  2023. return single_delim("q", "'", $str);
  2024. }
  2025. }
  2026. }
  2027. sub pp_const {
  2028. my $self = shift;
  2029. my($op, $cx) = @_;
  2030. # if ($op->private & OPp_CONST_BARE) { # trouble with `=>' autoquoting
  2031. # return $op->sv->PV;
  2032. # }
  2033. return const($op->sv);
  2034. }
  2035. sub dq {
  2036. my $self = shift;
  2037. my $op = shift;
  2038. my $type = $op->ppaddr;
  2039. if ($type eq "pp_const") {
  2040. return uninterp(escape_str(unback($op->sv->PV)));
  2041. } elsif ($type eq "pp_concat") {
  2042. return $self->dq($op->first) . $self->dq($op->last);
  2043. } elsif ($type eq "pp_uc") {
  2044. return '\U' . $self->dq($op->first->sibling) . '\E';
  2045. } elsif ($type eq "pp_lc") {
  2046. return '\L' . $self->dq($op->first->sibling) . '\E';
  2047. } elsif ($type eq "pp_ucfirst") {
  2048. return '\u' . $self->dq($op->first->sibling);
  2049. } elsif ($type eq "pp_lcfirst") {
  2050. return '\l' . $self->dq($op->first->sibling);
  2051. } elsif ($type eq "pp_quotemeta") {
  2052. return '\Q' . $self->dq($op->first->sibling) . '\E';
  2053. } elsif ($type eq "pp_join") {
  2054. return $self->deparse($op->last, 26); # was join($", @ary)
  2055. } else {
  2056. return $self->deparse($op, 26);
  2057. }
  2058. }
  2059. sub pp_backtick {
  2060. my $self = shift;
  2061. my($op, $cx) = @_;
  2062. # skip pushmark
  2063. return single_delim("qx", '`', $self->dq($op->first->sibling));
  2064. }
  2065. sub dquote {
  2066. my $self = shift;
  2067. my $op = shift;
  2068. # skip ex-stringify, pushmark
  2069. return single_delim("qq", '"', $self->dq($op->first->sibling));
  2070. }
  2071. # OP_STRINGIFY is a listop, but it only ever has one arg (?)
  2072. sub pp_stringify { dquote(@_) }
  2073. # tr/// and s/// (and tr[][], tr[]//, tr###, etc)
  2074. # note that tr(from)/to/ is OK, but not tr/from/(to)
  2075. sub double_delim {
  2076. my($from, $to) = @_;
  2077. my($succeed, $delim);
  2078. if ($from !~ m[/] and $to !~ m[/]) {
  2079. return "/$from/$to/";
  2080. } elsif (($succeed, $from) = balanced_delim($from) and $succeed) {
  2081. if (($succeed, $to) = balanced_delim($to) and $succeed) {
  2082. return "$from$to";
  2083. } else {
  2084. for $delim ('/', '"', '#') { # note no `'' -- s''' is special
  2085. return "$from$delim$to$delim" if index($to, $delim) == -1;
  2086. }
  2087. $to =~ s[/][\\/]g;
  2088. return "$from/$to/";
  2089. }
  2090. } else {
  2091. for $delim ('/', '"', '#') { # note no '
  2092. return "$delim$from$delim$to$delim"
  2093. if index($to . $from, $delim) == -1;
  2094. }
  2095. $from =~ s[/][\\/]g;
  2096. $to =~ s[/][\\/]g;
  2097. return "/$from/$to/";
  2098. }
  2099. }
  2100. sub pchr { # ASCII
  2101. my($n) = @_;
  2102. if ($n == ord '\\') {
  2103. return '\\\\';
  2104. } elsif ($n >= ord(' ') and $n <= ord('~')) {
  2105. return chr($n);
  2106. } elsif ($n == ord "\a") {
  2107. return '\\a';
  2108. } elsif ($n == ord "\b") {
  2109. return '\\b';
  2110. } elsif ($n == ord "\t") {
  2111. return '\\t';
  2112. } elsif ($n == ord "\n") {
  2113. return '\\n';
  2114. } elsif ($n == ord "\e") {
  2115. return '\\e';
  2116. } elsif ($n == ord "\f") {
  2117. return '\\f';
  2118. } elsif ($n == ord "\r") {
  2119. return '\\r';
  2120. } elsif ($n >= ord("\cA") and $n <= ord("\cZ")) {
  2121. return '\\c' . chr(ord("@") + $n);
  2122. } else {
  2123. # return '\x' . sprintf("%02x", $n);
  2124. return '\\' . sprintf("%03o", $n);
  2125. }
  2126. }
  2127. sub collapse {
  2128. my(@chars) = @_;
  2129. my($c, $str, $tr);
  2130. for ($c = 0; $c < @chars; $c++) {
  2131. $tr = $chars[$c];
  2132. $str .= pchr($tr);
  2133. if ($c <= $#chars - 2 and $chars[$c + 1] == $tr + 1 and
  2134. $chars[$c + 2] == $tr + 2)
  2135. {
  2136. for (; $c <= $#chars and $chars[$c + 1] == $chars[$c] + 1; $c++) {}
  2137. $str .= "-";
  2138. $str .= pchr($chars[$c]);
  2139. }
  2140. }
  2141. return $str;
  2142. }
  2143. sub OPpTRANS_SQUASH () { 16 }
  2144. sub OPpTRANS_DELETE () { 32 }
  2145. sub OPpTRANS_COMPLEMENT () { 64 }
  2146. sub pp_trans {
  2147. my $self = shift;
  2148. my($op, $cx) = @_;
  2149. my(@table) = unpack("s256", $op->pv);
  2150. my($c, $tr, @from, @to, @delfrom, $delhyphen);
  2151. if ($table[ord "-"] != -1 and
  2152. $table[ord("-") - 1] == -1 || $table[ord("-") + 1] == -1)
  2153. {
  2154. $tr = $table[ord "-"];
  2155. $table[ord "-"] = -1;
  2156. if ($tr >= 0) {
  2157. @from = ord("-");
  2158. @to = $tr;
  2159. } else { # -2 ==> delete
  2160. $delhyphen = 1;
  2161. }
  2162. }
  2163. for ($c = 0; $c < 256; $c++) {
  2164. $tr = $table[$c];
  2165. if ($tr >= 0) {
  2166. push @from, $c; push @to, $tr;
  2167. } elsif ($tr == -2) {
  2168. push @delfrom, $c;
  2169. }
  2170. }
  2171. my $flags;
  2172. @from = (@from, @delfrom);
  2173. if ($op->private & OPpTRANS_COMPLEMENT) {
  2174. $flags .= "c";
  2175. my @newfrom = ();
  2176. my %from;
  2177. @from{@from} = (1) x @from;
  2178. for ($c = 0; $c < 256; $c++) {
  2179. push @newfrom, $c unless $from{$c};
  2180. }
  2181. @from = @newfrom;
  2182. }
  2183. if ($op->private & OPpTRANS_DELETE) {
  2184. $flags .= "d";
  2185. } else {
  2186. pop @to while $#to and $to[$#to] == $to[$#to -1];
  2187. }
  2188. $flags .= "s" if $op->private & OPpTRANS_SQUASH;
  2189. my($from, $to);
  2190. $from = collapse(@from);
  2191. $to = collapse(@to);
  2192. $from .= "-" if $delhyphen;
  2193. return "tr" . double_delim($from, $to) . $flags;
  2194. }
  2195. # Like dq(), but different
  2196. sub re_dq {
  2197. my $self = shift;
  2198. my $op = shift;
  2199. my $type = $op->ppaddr;
  2200. if ($type eq "pp_const") {
  2201. return uninterp($op->sv->PV);
  2202. } elsif ($type eq "pp_concat") {
  2203. return $self->re_dq($op->first) . $self->re_dq($op->last);
  2204. } elsif ($type eq "pp_uc") {
  2205. return '\U' . $self->re_dq($op->first->sibling) . '\E';
  2206. } elsif ($type eq "pp_lc") {
  2207. return '\L' . $self->re_dq($op->first->sibling) . '\E';
  2208. } elsif ($type eq "pp_ucfirst") {
  2209. return '\u' . $self->re_dq($op->first->sibling);
  2210. } elsif ($type eq "pp_lcfirst") {
  2211. return '\l' . $self->re_dq($op->first->sibling);
  2212. } elsif ($type eq "pp_quotemeta") {
  2213. return '\Q' . $self->re_dq($op->first->sibling) . '\E';
  2214. } elsif ($type eq "pp_join") {
  2215. return $self->deparse($op->last, 26); # was join($", @ary)
  2216. } else {
  2217. return $self->deparse($op, 26);
  2218. }
  2219. }
  2220. sub pp_regcomp {
  2221. my $self = shift;
  2222. my($op, $cx) = @_;
  2223. my $kid = $op->first;
  2224. $kid = $kid->first if $kid->ppaddr eq "pp_regcmaybe";
  2225. $kid = $kid->first if $kid->ppaddr eq "pp_regcreset";
  2226. return $self->re_dq($kid);
  2227. }
  2228. sub OPp_RUNTIME () { 64 }
  2229. sub PMf_ONCE () { 0x2 }
  2230. sub PMf_SKIPWHITE () { 0x10 }
  2231. sub PMf_CONST () { 0x40 }
  2232. sub PMf_KEEP () { 0x80 }
  2233. sub PMf_GLOBAL () { 0x100 }
  2234. sub PMf_CONTINUE () { 0x200 }
  2235. sub PMf_EVAL () { 0x400 }
  2236. sub PMf_LOCALE () { 0x800 }
  2237. sub PMf_MULTILINE () { 0x1000 }
  2238. sub PMf_SINGLELINE () { 0x2000 }
  2239. sub PMf_FOLD () { 0x4000 }
  2240. sub PMf_EXTENDED () { 0x8000 }
  2241. # osmic acid -- see osmium tetroxide
  2242. my %matchwords;
  2243. map($matchwords{join "", sort split //, $_} = $_, 'cig', 'cog', 'cos', 'cogs',
  2244. 'cox', 'go', 'is', 'ism', 'iso', 'mig', 'mix', 'osmic', 'ox', 'sic',
  2245. 'sig', 'six', 'smog', 'so', 'soc', 'sog', 'xi');
  2246. sub matchop {
  2247. my $self = shift;
  2248. my($op, $cx, $name, $delim) = @_;
  2249. my $kid = $op->first;
  2250. my ($binop, $var, $re) = ("", "", "");
  2251. if ($op->flags & OPf_STACKED) {
  2252. $binop = 1;
  2253. $var = $self->deparse($kid, 20);
  2254. $kid = $kid->sibling;
  2255. }
  2256. if (null $kid) {
  2257. $re = re_uninterp(escape_str($op->precomp));
  2258. } else {
  2259. $re = $self->deparse($kid, 1);
  2260. }
  2261. my $flags = "";
  2262. $flags .= "c" if $op->pmflags & PMf_CONTINUE;
  2263. $flags .= "g" if $op->pmflags & PMf_GLOBAL;
  2264. $flags .= "i" if $op->pmflags & PMf_FOLD;
  2265. $flags .= "m" if $op->pmflags & PMf_MULTILINE;
  2266. $flags .= "o" if $op->pmflags & PMf_KEEP;
  2267. $flags .= "s" if $op->pmflags & PMf_SINGLELINE;
  2268. $flags .= "x" if $op->pmflags & PMf_EXTENDED;
  2269. $flags = $matchwords{$flags} if $matchwords{$flags};
  2270. if ($op->pmflags & PMf_ONCE) { # only one kind of delimiter works here
  2271. $re =~ s/\?/\\?/g;
  2272. $re = "?$re?";
  2273. } else {
  2274. $re = single_delim($name, $delim, $re);
  2275. }
  2276. $re = $re . $flags;
  2277. if ($binop) {
  2278. return $self->maybe_parens("$var =~ $re", $cx, 20);
  2279. } else {
  2280. return $re;
  2281. }
  2282. }
  2283. sub pp_match { matchop(@_, "m", "/") }
  2284. sub pp_pushre { matchop(@_, "m", "/") }
  2285. sub pp_qr { matchop(@_, "qr", "") }
  2286. sub pp_split {
  2287. my $self = shift;
  2288. my($op, $cx) = @_;
  2289. my($kid, @exprs, $ary, $expr);
  2290. $kid = $op->first;
  2291. if ($ {$kid->pmreplroot}) {
  2292. $ary = '@' . $self->gv_name($kid->pmreplroot);
  2293. }
  2294. for (; !null($kid); $kid = $kid->sibling) {
  2295. push @exprs, $self->deparse($kid, 6);
  2296. }
  2297. $expr = "split(" . join(", ", @exprs) . ")";
  2298. if ($ary) {
  2299. return $self->maybe_parens("$ary = $expr", $cx, 7);
  2300. } else {
  2301. return $expr;
  2302. }
  2303. }
  2304. # oxime -- any of various compounds obtained chiefly by the action of
  2305. # hydroxylamine on aldehydes and ketones and characterized by the
  2306. # bivalent grouping C=NOH [Webster's Tenth]
  2307. my %substwords;
  2308. map($substwords{join "", sort split //, $_} = $_, 'ego', 'egoism', 'em',
  2309. 'es', 'ex', 'exes', 'gee', 'go', 'goes', 'ie', 'ism', 'iso', 'me',
  2310. 'meese', 'meso', 'mig', 'mix', 'os', 'ox', 'oxime', 'see', 'seem',
  2311. 'seg', 'sex', 'sig', 'six', 'smog', 'sog', 'some', 'xi');
  2312. sub pp_subst {
  2313. my $self = shift;
  2314. my($op, $cx) = @_;
  2315. my $kid = $op->first;
  2316. my($binop, $var, $re, $repl) = ("", "", "", "");
  2317. if ($op->flags & OPf_STACKED) {
  2318. $binop = 1;
  2319. $var = $self->deparse($kid, 20);
  2320. $kid = $kid->sibling;
  2321. }
  2322. my $flags = "";
  2323. if (null($op->pmreplroot)) {
  2324. $repl = $self->dq($kid);
  2325. $kid = $kid->sibling;
  2326. } else {
  2327. $repl = $op->pmreplroot->first; # skip substcont
  2328. while ($repl->ppaddr eq "pp_entereval") {
  2329. $repl = $repl->first;
  2330. $flags .= "e";
  2331. }
  2332. $repl = $self->dq($repl);
  2333. }
  2334. if (null $kid) {
  2335. $re = re_uninterp(escape_str($op->precomp));
  2336. } else {
  2337. $re = $self->deparse($kid, 1);
  2338. }
  2339. $flags .= "e" if $op->pmflags & PMf_EVAL;
  2340. $flags .= "g" if $op->pmflags & PMf_GLOBAL;
  2341. $flags .= "i" if $op->pmflags & PMf_FOLD;
  2342. $flags .= "m" if $op->pmflags & PMf_MULTILINE;
  2343. $flags .= "o" if $op->pmflags & PMf_KEEP;
  2344. $flags .= "s" if $op->pmflags & PMf_SINGLELINE;
  2345. $flags .= "x" if $op->pmflags & PMf_EXTENDED;
  2346. $flags = $substwords{$flags} if $substwords{$flags};
  2347. if ($binop) {
  2348. return $self->maybe_parens("$var =~ s"
  2349. . double_delim($re, $repl) . $flags,
  2350. $cx, 20);
  2351. } else {
  2352. return "s". double_delim($re, $repl) . $flags;
  2353. }
  2354. }
  2355. 1;
  2356. __END__
  2357. =head1 NAME
  2358. B::Deparse - Perl compiler backend to produce perl code
  2359. =head1 SYNOPSIS
  2360. B<perl> B<-MO=Deparse>[B<,-u>I<PACKAGE>][B<,-p>][B<,-l>][B<,-s>I<LETTERS>] I<prog.pl>
  2361. =head1 DESCRIPTION
  2362. B::Deparse is a backend module for the Perl compiler that generates
  2363. perl source code, based on the internal compiled structure that perl
  2364. itself creates after parsing a program. The output of B::Deparse won't
  2365. be exactly the same as the original source, since perl doesn't keep
  2366. track of comments or whitespace, and there isn't a one-to-one
  2367. correspondence between perl's syntactical constructions and their
  2368. compiled form, but it will often be close. When you use the B<-p>
  2369. option, the output also includes parentheses even when they are not
  2370. required by precedence, which can make it easy to see if perl is
  2371. parsing your expressions the way you intended.
  2372. Please note that this module is mainly new and untested code and is
  2373. still under development, so it may change in the future.
  2374. =head1 OPTIONS
  2375. As with all compiler backend options, these must follow directly after
  2376. the '-MO=Deparse', separated by a comma but not any white space.
  2377. =over 4
  2378. =item B<-p>
  2379. Print extra parentheses. Without this option, B::Deparse includes
  2380. parentheses in its output only when they are needed, based on the
  2381. structure of your program. With B<-p>, it uses parentheses (almost)
  2382. whenever they would be legal. This can be useful if you are used to
  2383. LISP, or if you want to see how perl parses your input. If you say
  2384. if ($var & 0x7f == 65) {print "Gimme an A!"}
  2385. print ($which ? $a : $b), "\n";
  2386. $name = $ENV{USER} or "Bob";
  2387. C<B::Deparse,-p> will print
  2388. if (($var & 0)) {
  2389. print('Gimme an A!')
  2390. };
  2391. (print(($which ? $a : $b)), '???');
  2392. (($name = $ENV{'USER'}) or '???')
  2393. which probably isn't what you intended (the C<'???'> is a sign that
  2394. perl optimized away a constant value).
  2395. =item B<-u>I<PACKAGE>
  2396. Normally, B::Deparse deparses the main code of a program, all the subs
  2397. called by the main program (and all the subs called by them,
  2398. recursively), and any other subs in the main:: package. To include
  2399. subs in other packages that aren't called directly, such as AUTOLOAD,
  2400. DESTROY, other subs called automatically by perl, and methods, which
  2401. aren't resolved to subs until runtime, use the B<-u> option. The
  2402. argument to B<-u> is the name of a package, and should follow directly
  2403. after the 'u'. Multiple B<-u> options may be given, separated by
  2404. commas. Note that unlike some other backends, B::Deparse doesn't
  2405. (yet) try to guess automatically when B<-u> is needed -- you must
  2406. invoke it yourself.
  2407. =item B<-l>
  2408. Add '#line' declarations to the output based on the line and file
  2409. locations of the original code.
  2410. =item B<-s>I<LETTERS>
  2411. Tweak the style of B::Deparse's output. At the moment, only one style
  2412. option is implemented:
  2413. =over 4
  2414. =item B<C>
  2415. Cuddle C<elsif>, C<else>, and C<continue> blocks. For example, print
  2416. if (...) {
  2417. ...
  2418. } else {
  2419. ...
  2420. }
  2421. instead of
  2422. if (...) {
  2423. ...
  2424. }
  2425. else {
  2426. ...
  2427. }
  2428. The default is not to cuddle.
  2429. =back
  2430. =back
  2431. =head1 BUGS
  2432. See the 'to do' list at the beginning of the module file.
  2433. =head1 AUTHOR
  2434. Stephen McCamant <[email protected]>, based on an earlier version by
  2435. Malcolm Beattie <[email protected]>.
  2436. =cut