blib/lib/B/DeparseTree/PPfns.pm untested

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