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

filter: Statementstmt 0.0% 0 / 739 Branchbran 0.0% 0 / 326 Conditioncond 0.0% 0 / 215 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 74 Podpod 0.0% 0 / 59 totaltotal 0.0% 0 / 1413 SCAR 102.1
CC
164
Cov
0%
pp_multideref
462.0
check_proto
342.0
tr_decode_utf8
240.0
CRAP 27060.0
1 # B::DeparseTree::P526.pm
2 # Copyright (c) 1998-2000, 2002, 2003, 2004, 2005, 2006 Stephen McCamant.
3 # Copyright (c) 2015, 2017, 2018 Rocky Bernstein
4 # All rights reserved.
5 # This module is free software; you can redistribute and/or modify
6 # it under the same terms as Perl itself.
7  
8 # This is based on the module B::Deparse (for perl 5.22) by Stephen McCamant.
9 # It has been extended save tree structure, and is addressible
10 # by opcode address.
11  
12 # B::Parse in turn is based on the module of the same name by Malcolm Beattie,
13 # but essentially none of his code remains.
14  
15 use v5.26;
16  
17 use rlib '../..';
18  
19 package B::DeparseTree::P526c;
20 use Carp;
21  
22 use B qw(class main_root main_start main_cv svref_2object opnumber perlstring
23  
24 CVf_METHOD
25 MDEREF_ACTION_MASK
26 MDEREF_AV_gvav_aelem
27 MDEREF_AV_gvsv_vivify_rv2av_aelem
28 MDEREF_AV_padav_aelem
29 MDEREF_AV_padsv_vivify_rv2av_aelem
30 MDEREF_AV_pop_rv2av_aelem
31 MDEREF_AV_vivify_rv2av_aelem
32 MDEREF_FLAG_last
33 MDEREF_HV_gvhv_helem
34 MDEREF_HV_gvsv_vivify_rv2hv_helem
35 MDEREF_HV_padhv_helem
36 MDEREF_HV_padsv_vivify_rv2hv_helem
37 MDEREF_HV_pop_rv2hv_helem
38 MDEREF_HV_vivify_rv2hv_helem
39 MDEREF_INDEX_MASK
40 MDEREF_INDEX_const
41 MDEREF_INDEX_gvsv
42 MDEREF_INDEX_none
43 MDEREF_INDEX_padsv
44 MDEREF_MASK
45 MDEREF_SHIFT
46 MDEREF_reload
47 OPf_KIDS
48 OPf_MOD OPf_PARENS
49 OPf_REF
50 OPf_SPECIAL
51 OPf_STACKED
52 OPpCONST_BARE
53 OPpENTERSUB_AMPER
54 OPpSLICE
55 OPpLVAL_INTRO
56 OPpMULTIDEREF_DELETE
57 OPpMULTIDEREF_EXISTS
58 OPpOUR_INTRO
59 OPpPADRANGE_COUNTSHIFT
60 OPpSIGNATURE_FAKE
61 OPpSPLIT_ASSIGN
62 OPpSPLIT_LEX
63 OPpTRANS_COMPLEMENT
64 OPpTRANS_DELETE
65 OPpTRANS_SQUASH
66 PMf_CONTINUE
67 PMf_EVAL PMf_ONCE
68 PMf_EXTENDED_MORE
69 PMf_FOLD PMf_EXTENDED
70 PMf_KEEP PMf_GLOBAL
71 PMf_MULTILINE
72 PMf_SINGLELINE
73 SIGNATURE_ACTION_MASK
74 SIGNATURE_FLAG_skip
75 SIGNATURE_MASK
76 SIGNATURE_SHIFT
77 SIGNATURE_arg
78 SIGNATURE_arg_default_0
79 SIGNATURE_arg_default_1
80 SIGNATURE_arg_default_const
81 SIGNATURE_arg_default_gvsv
82 SIGNATURE_arg_default_iv
83 SIGNATURE_arg_default_none
84 SIGNATURE_arg_default_op
85 SIGNATURE_arg_default_padsv
86 SIGNATURE_arg_default_undef
87 SIGNATURE_array
88 SIGNATURE_end
89 SIGNATURE_hash
90 SIGNATURE_padintro
91 SIGNATURE_reload
92 SVf_FAKE SVs_RMG
93 SVf_ROK SVpad_OUR
94 SVpad_TYPED
95 SVs_SMG
96 );
97  
98 use B::DeparseTree::PPfns;
99 use B::DeparseTree::SyntaxTree;
100 use B::DeparseTree::PP;
101 use B::Deparse;
102  
103 # Copy unchanged functions from B::Deparse
104 *begin_is_use = *B::Deparse::begin_is_use;
105 *const_sv = *B::Deparse::const_sv;
106 *escape_re = *B::Deparse::escape_re;
107 *find_scope_st = *B::Deparse::find_scope_st;
108 *gv_name = *B::Deparse::gv_name;
109 *keyword = *B::Deparse::keyword;
110 *meth_pad_subs = *B::Deparse::pad_subs;
111 *meth_rclass_sv = *B::Deparse::meth_rclass_sv;
112 *meth_sv = *B::Deparse::meth_sv;
113 *padany = *B::Deparse::padany;
114 *padname = *B::Deparse::padname;
115 *padname_sv = *B::Deparse::padname_sv;
116 *re_flags = *B::Deparse::re_flags;
117 *stash_variable = *B::Deparse::stash_variable;
118 *stash_variable_name = *B::Deparse::stash_variable_name;
119 *tr_chr = *B::Deparse::tr_chr;
120  
121 use strict;
122 use vars qw/$AUTOLOAD @ISA @EXPORT/;
123 use warnings ();
124 require feature;
125 use types;
126  
127 our $VERSION = '3.2.0';
128  
129 our @ISA = qw(Exporter);
130  
131 BEGIN {
132 # List version-specific constants here.
133 # Easiest way to keep this code portable between version looks to
134 # be to fake up a dummy constant that will never actually be true.
135 foreach (qw(OPpSORT_INPLACE OPpSORT_DESCEND OPpITER_REVERSED OPpCONST_NOVER
136 OPpPAD_STATE PMf_SKIPWHITE RXf_SKIPWHITE
137 PMf_CHARSET PMf_KEEPCOPY PMf_NOCAPTURE CVf_ANONCONST
138 CVf_LOCKED OPpREVERSE_INPLACE OPpSUBSTR_REPL_FIRST
139 PMf_NONDESTRUCT OPpCONST_ARYBASE OPpEVAL_BYTES
140 OPpLVREF_TYPE OPpLVREF_SV OPpLVREF_AV OPpLVREF_HV
141 OPpLVREF_CV OPpLVREF_ELEM SVpad_STATE)) {
142 eval { B->import($_) };
143 no strict 'refs';
144 *{$_} = sub () {0} unless *{$_}{CODE};
145 }
146 }
147  
148 BEGIN { for (qw[ rv2sv aelem
149 rv2av rv2hv helem custom ]) {
150 eval "sub OP_\U$_ () { " . opnumber($_) . "}"
151 }}
152  
153 # The following OPs don't have functions:
154  
155 # pp_padany -- does not exist after parsing
156  
157 sub AUTOLOAD {
158 if ($AUTOLOAD =~ s/^.*::pp_//) {
159 warn "unexpected OP_".
160 ($_[1]->type == OP_CUSTOM ? "CUSTOM ($AUTOLOAD)" : uc $AUTOLOAD);
161 return "XXX";
162 } else {
163 Carp::confess "Undefined subroutine $AUTOLOAD called";
164 }
165 }
166  
167 sub DESTROY {} # Do not AUTOLOAD
168  
169 # The BEGIN {} is used here because otherwise this code isn't executed
170 # when you run B::Deparse on itself.
171 my %globalnames;
172 BEGIN { map($globalnames{$_}++, "SIG", "STDIN", "STDOUT", "STDERR", "INC",
173 "ENV", "ARGV", "ARGVOUT", "_"); }
174  
175 { no strict 'refs'; *{"pp_r$_"} = *{"pp_$_"} for qw< keys each values >; }
176  
177 # FIXME:
178 # Different in 5.20. Go over differences to see if okay in 5.20.
179 sub pp_chdir {
180 my ($self, $op, $cx) = @_;
181 if (($op->flags & (OPf_SPECIAL|OPf_KIDS)) == (OPf_SPECIAL|OPf_KIDS)) {
182 my $kw = $self->keyword("chdir");
183 my $kid = $self->const_sv($op->first)->PV;
184 my $code = $kw
185 . ($cx >= 16 || $self->{'parens'} ? "($kid)" : " $kid");
186 maybe_targmy(@_, sub { $_[3] }, $code);
187 } else {
188 maybe_targmy(@_, \&unop, "chdir")
189 }
190 }
191  
192 sub pp_require
193 {
194 my($self, $op, $cx) = @_;
195 my $opname = $op->flags & OPf_SPECIAL ? 'CORE::require' : 'require';
196 if (class($op) eq "UNOP" and $op->first->name eq "const"
197 and $op->first->private & OPpCONST_BARE) {
198 my $name = $self->const_sv($op->first)->PV;
199 $name =~ s[/][::]g;
200 $name =~ s/\.pm//g;
201 return info_from_list($op, $self, [$opname, $name], ' ',
202 'require',
203 {maybe_parens => [$self, $cx, 16]});
204 } else {
205 return $self->unop(
206 $op, $cx,
207 $op->first->name eq 'const'
208 && $op->first->private & OPpCONST_NOVER
209 ? "no"
210 : $opname,
211 1, # llafr does not apply
212 );
213 }
214 Carp::confess("unhandled condition in pp_require");
215 }
216  
217 sub pp_readline {
218 my $self = shift;
219 my($op, $cx) = @_;
220 my $kid = $op->first;
221 $kid = $kid->first if $kid->name eq "rv2gv"; # <$fh>
222 if (B::Deparse::is_scalar($kid)
223 and $op->flags & OPf_SPECIAL
224 and $self->deparse($kid, 1) eq 'ARGV') {
225 my $body = [$self->deparse($kid, 1, $op)];
226 return info_from_list($op, $self, ['<', $body->[0]{text}, '>'], '',
227 'readline_scalar', {body=>$body});
228 }
229 return $self->unop($op, $cx, "readline");
230 }
231  
232 sub pp_rcatline {
233 my $self = shift;
234 my($op) = @_;
235 return info_from_list($op, $self, ["<", $self->gv_name($self->gv_or_padgv($op)), ">"],
236 '', 'rcatline', {});
237 }
238  
239 sub ASSIGN () { 2 } # has OP= variant
240  
241 sub pp_smartmatch {
242 my ($self, $op, $cx) = @_;
243 if ($op->flags & OPf_SPECIAL) {
244 return $self->deparse($op->last, $cx, $op);
245 }
246 else {
247 binop(@_, "~~", 14);
248 }
249 }
250  
251 sub bin_info_join($$$$$$$) {
252 my ($self, $op, $lhs, $rhs, $mid, $sep, $type) = @_;
253 my $texts = [$lhs->{text}, $mid, $rhs->{text}];
254 return info_from_list($op, $self, $texts, ' ', $type, {})
255 }
256  
257 sub bin_info_join_maybe_parens($$$$$$$$$) {
258 my ($self, $op, $lhs, $rhs, $mid, $sep, $cx, $prec, $type) = @_;
259 my $info = bin_info_join($self, $op, $lhs, $rhs, $mid, $sep, $type);
260 $info->{text} = $self->maybe_parens($info->{text}, $cx, $prec);
261 return $info;
262 }
263  
264 sub range {
265 my $self = shift;
266 my ($op, $cx, $type) = @_;
267 my $left = $op->first;
268 my $right = $left->sibling;
269 $left = $self->deparse($left, 9, $op);
270 $right = $self->deparse($right, 9, $op);
271 return info_from_list($op, $self, [$left, $type, $right], ' ', 'range',
272 {maybe_parens => [$self, $cx, 9]});
273 }
274  
275 sub for_loop {
276 my $self = shift;
277 my($op, $cx, $parent) = @_;
278 my $init = $self->deparse($op, 1, $parent);
279 my $s = $op->sibling;
280 my $ll = $s->name eq "unstack" ? $s->sibling : $s->first->sibling;
281 return $self->loop_common($ll, $cx, $init);
282 }
283  
284 sub pp_padsv {
285 my $self = shift;
286 my($op, $cx, $forbid_parens) = @_;
287 return $self->maybe_my($op, $cx, $self->padname($op->targ),
288 $forbid_parens);
289 }
290  
291 my @threadsv_names = B::threadsv_names;
292 sub pp_threadsv {
293 my $self = shift;
294 my($op, $cx) = @_;
295 return $self->maybe_local_str($op, $cx, "\$" . $threadsv_names[$op->targ]);
296 }
297  
298 sub gv_or_padgv {
299 my $self = shift;
300 my $op = shift;
301 if (class($op) eq "PADOP") {
302 return $self->padval($op->padix);
303 } else { # class($op) eq "SVOP"
304 return $op->gv;
305 }
306 }
307  
308 sub pp_aelemfast_lex
309 {
310 my($self, $op, $cx) = @_;
311 my $name = $self->padname($op->targ);
312 $name =~ s/^@/\$/;
313 return info_from_list($op, $self, [$name, "[", ($op->private + $self->{'arybase'}), "]"],
314 '', 'pp_aelemfast_lex', {});
315 }
316  
317 sub pp_aelemfast
318 {
319 my($self, $op, $cx) = @_;
320 # optimised PADAV, pre 5.15
321 return $self->pp_aelemfast_lex(@_) if ($op->flags & OPf_SPECIAL);
322  
323 my $gv = $self->gv_or_padgv($op);
324 my($name,$quoted) = $self->stash_variable_name('@',$gv);
325 $name = $quoted ? "$name->" : '$' . $name;
326 my $i = $op->private;
327 $i -= 256 if $i > 127;
328 return info_from_list($op, $self, [$name, "[", ($op->private + $self->{'arybase'}), "]"],
329 '', 'pp_aelemfast', {});
330 }
331  
332 sub pp_rv2sv { maybe_local_str(@_, rv2x(@_, "\$")) }
333 sub pp_rv2hv { maybe_local_str(@_, rv2x(@_, "%")) }
334 sub pp_rv2gv { maybe_local_str(@_, rv2x(@_, "*")) }
335  
336 # skip rv2av
337 sub pp_av2arylen {
338 my $self = shift;
339 my($op, $cx) = @_;
340 if ($op->first->name eq "padav") {
341 return $self->maybe_local_str($op, $cx, '$#' . $self->padany($op->first));
342 } else {
343 return $self->maybe_local_str($op, $cx,
344 $self->rv2x($op->first, $cx, '$#'));
345 }
346 }
347  
348 sub list_const {
349 my $self = shift;
350 my($op, $cx, @list) = @_;
351 my @a = map $self->const($_, 6), @list;
352 my @texts = $self->map_texts(\@a);
353 my $type = 'list_const';
354 my $prec = 6;
355 if (@texts == 0) {
356 return info_from_list($op, $self, ['(', ')'], '', 'list_const_null', {});
357 } elsif (@texts == 1) {
358 return info_from_text($op, $self, $texts[0], 'list_const_one',
359 {body => \@a});
360 } elsif ( @texts > 2 and !grep(!/^-?\d+$/, @texts)) {
361 # collapse (-1,0,1,2) into (-1..2)
362 my ($s, $e) = @texts[0,-1];
363 my $i = $s;
364 unless (grep $i++ != $_, @texts) {
365 @texts = ($s, '..', $e);
366 $type = 'list_const_range';
367 $prec = 9;
368 }
369 }
370 return info_from_list($op, $self, \@texts, '', $type,
371 {maybe_parens => [$self, $cx, $prec]});
372 }
373  
374 sub pp_rv2av {
375 my $self = shift;
376 my($op, $cx) = @_;
377 my $kid = $op->first;
378 if ($kid->name eq "const") { # constant list
379 my $av = $self->const_sv($kid);
380 return $self->list_const($kid, $cx, $av->ARRAY);
381 } else {
382 return $self->maybe_local_str($op, $cx,
383 $self->rv2x($op, $cx, "\@"));
384 }
385 }
386  
387 sub elem_or_slice_array_name
388 {
389 my $self = shift;
390 my ($array, $left, $padname, $allow_arrow) = @_;
391  
392 if ($array->name eq $padname) {
393 return $self->padany($array);
394 } elsif (B::Deparse::is_scope($array)) { # ${expr}[0]
395 return "{" . $self->deparse($array, 0) . "}";
396 } elsif ($array->name eq "gv") {
397 ($array, my $quoted) =
398 $self->stash_variable_name(
399 $left eq '[' ? '@' : '%', $self->gv_or_padgv($array)
400 );
401 if (!$allow_arrow && $quoted) {
402 # This cannot happen.
403 die "Invalid variable name $array for slice";
404 }
405 return $quoted ? "$array->" : $array;
406 } elsif (!$allow_arrow || B::Deparse::is_scalar $array) {
407 # $x[0], $$x[0], ...
408 return $self->deparse($array, 24)->{text};
409 } else {
410 return undef;
411 }
412 }
413  
414 sub elem_or_slice_single_index($$)
415 {
416 my ($self, $idx, $parent) = @_;
417  
418 my $idx_info = $self->deparse($idx, 1, $parent);
419 my $idx_str = $idx_info->{text};
420  
421 # Outer parens in an array index will confuse perl
422 # if we're interpolating in a regular expression, i.e.
423 # /$x$foo[(-1)]/ is *not* the same as /$x$foo[-1]/
424 #
425 # If $self->{parens}, then an initial '(' will
426 # definitely be paired with a final ')'. If
427 # !$self->{parens}, the misleading parens won't
428 # have been added in the first place.
429 #
430 # [You might think that we could get "(...)...(...)"
431 # where the initial and final parens do not match
432 # each other. But we can't, because the above would
433 # only happen if there's an infix binop between the
434 # two pairs of parens, and *that* means that the whole
435 # expression would be parenthesized as well.]
436 #
437 $idx_str =~ s/^\((.*)\)$/$1/ if $self->{'parens'};
438  
439 # Hash-element braces will autoquote a bareword inside themselves.
440 # We need to make sure that C<$hash{warn()}> doesn't come out as
441 # C<$hash{warn}>, which has a quite different meaning. Currently
442 # B::Deparse will always quote strings, even if the string was a
443 # bareword in the original (i.e. the OPpCONST_BARE flag is ignored
444 # for constant strings.) So we can cheat slightly here - if we see
445 # a bareword, we know that it is supposed to be a function call.
446 #
447 $idx_str =~ s/^([A-Za-z_]\w*)$/$1()/;
448  
449 return info_from_text($idx_info->{op}, $self, $idx_str,
450 'elem_or_slice_single_index',
451 {body => [$idx_info]});
452 }
453  
454 # a simplified version of elem_or_slice_array_name()
455 # for the use of pp_multideref
456  
457 sub multideref_var_name($$$)
458 {
459 my ($self, $gv, $is_hash) = @_;
460  
461 my ($name, $quoted) =
462 $self->stash_variable_name( $is_hash ? '%' : '@', $gv);
463 return $quoted ? "$name->"
464 : $name eq '#'
465 ? '${#}' # avoid ${#}[1] => $#[1]
466 : '$' . $name;
467 }
468  
469 sub pp_multideref
470 {
471 my($self, $op, $cx) = @_;
472 my @texts = ();
473  
474 if ($op->private & OPpMULTIDEREF_EXISTS) {
475 @texts = ($self->keyword("exists"), ' ');
476 }
477 elsif ($op->private & OPpMULTIDEREF_DELETE) {
478 @texts = ($self->keyword("delete"), ' ')
479 }
480 elsif ($op->private & OPpLVAL_INTRO) {
481 @texts = ($self->keyword("local"), ' ')
482 }
483  
484 if ($op->first && ($op->first->flags & OPf_KIDS)) {
485 # arbitrary initial expression, e.g. f(1,2,3)->[...]
486 my $first = $self->deparse($op->first, 24, $op);
487 push @texts, $first->{text};
488 }
489  
490 my @items = $op->aux_list($self->{curcv});
491 my $actions = shift @items;
492  
493 my $is_hash;
494 my $derefs = 0;
495  
496 while (1) {
497 if (($actions & MDEREF_ACTION_MASK) == MDEREF_reload) {
498 $actions = shift @items;
499 next;
500 }
501  
502 $is_hash = (
503 ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_pop_rv2hv_helem
504 || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_gvsv_vivify_rv2hv_helem
505 || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_padsv_vivify_rv2hv_helem
506 || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_vivify_rv2hv_helem
507 || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_padhv_helem
508 || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_gvhv_helem
509 );
510  
511 if ( ($actions & MDEREF_ACTION_MASK) == MDEREF_AV_padav_aelem
512 || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_padhv_helem)
513 {
514 $derefs = 1;
515 push @texts, '$' . substr($self->padname(shift @items), 1);
516 }
517 elsif ( ($actions & MDEREF_ACTION_MASK) == MDEREF_AV_gvav_aelem
518 || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_gvhv_helem)
519 {
520 $derefs = 1;
521 push @texts, $self->multideref_var_name(shift @items, $is_hash);
522 }
523 else {
524 if ( ($actions & MDEREF_ACTION_MASK) ==
525 MDEREF_AV_padsv_vivify_rv2av_aelem
526 || ($actions & MDEREF_ACTION_MASK) ==
527 MDEREF_HV_padsv_vivify_rv2hv_helem)
528 {
529 push @texts, $self->padname(shift @items);
530 }
531 elsif ( ($actions & MDEREF_ACTION_MASK) ==
532 MDEREF_AV_gvsv_vivify_rv2av_aelem
533 || ($actions & MDEREF_ACTION_MASK) ==
534 MDEREF_HV_gvsv_vivify_rv2hv_helem)
535 {
536 push @texts, $self->multideref_var_name(shift @items, $is_hash);
537 }
538 elsif ( ($actions & MDEREF_ACTION_MASK) ==
539 MDEREF_AV_pop_rv2av_aelem
540 || ($actions & MDEREF_ACTION_MASK) ==
541 MDEREF_HV_pop_rv2hv_helem)
542 {
543 if ( ($op->flags & OPf_KIDS)
544 && ( B::Deparse::_op_is_or_was($op->first, OP_RV2AV)
545 || B::Deparse::_op_is_or_was($op->first, OP_RV2HV))
546 && ($op->first->flags & OPf_KIDS)
547 && ( B::Deparse::_op_is_or_was($op->first->first, OP_AELEM)
548 || B::Deparse::_op_is_or_was($op->first->first, OP_HELEM))
549 )
550 {
551 $derefs++;
552 }
553 }
554  
555 push(@texts, '->') if !$derefs++;
556 }
557  
558  
559 if (($actions & MDEREF_INDEX_MASK) == MDEREF_INDEX_none) {
560 last;
561 }
562  
563 push(@texts, $is_hash ? '{' : '[');
564  
565 if (($actions & MDEREF_INDEX_MASK) == MDEREF_INDEX_const) {
566 my $key = shift @items;
567 if ($is_hash) {
568 push @texts, $self->const($key, $cx)->{text};
569 }
570 else {
571 push @texts, $key;
572 }
573 }
574 elsif (($actions & MDEREF_INDEX_MASK) == MDEREF_INDEX_padsv) {
575 push @texts, $self->padname(shift @items);
576 }
577 elsif (($actions & MDEREF_INDEX_MASK) == MDEREF_INDEX_gvsv) {
578 push @texts,('$' . ($self->stash_variable_name('$', shift @items))[0]);
579 }
580  
581 push(@texts, $is_hash ? '}' : ']');
582  
583 if ($actions & MDEREF_FLAG_last) {
584 last;
585 }
586 $actions >>= MDEREF_SHIFT;
587 }
588  
589 return info_from_list($op, $self, \@texts, '', 'multideref', {});
590 }
591  
592 # placeholder; signatures are handled specially in deparse_sub()
593 sub pp_signature {
594 my($self, $op, $cx) = @_;
595 return $self->info_from_string("signature", $op, '');
596 }
597  
598 sub pp_gelem
599 {
600 my($self, $op, $cx) = @_;
601 my($glob, $part) = ($op->first, $op->last);
602 $glob = $glob->first; # skip rv2gv
603 $glob = $glob->first if $glob->name eq "rv2gv"; # this one's a bug
604 my $scope = B::Deparse::is_scope($glob);
605 $glob = $self->deparse($glob, 0);
606 $part = $self->deparse($part, 1);
607 return "*" . ($scope ? "{$glob}" : $glob) . "{$part}";
608 }
609  
610 sub pp_lslice
611 {
612 my ($self, $op, $cs) = @_;
613 my $idx = $op->first;
614 my $list = $op->last;
615 my(@elems, $kid);
616 my $list_info = $self->deparse($list, 1, $op);
617 my $idx_info = $self->deparse($idx, 1, $op);
618 return info_from_list($op, $self, ['(', $list_info->{text}, ')', '[', $idx_info->{text}, ']'],
619 '', 'lslice', {body=>[$list_info, $idx_info]});
620 }
621  
622 sub _method
623 {
624 my($self, $op, $cx) = @_;
625 my @other_ops = ($op->first);
626 my $kid = $op->first->sibling; # skip pushmark
627 my($meth, $obj, @exprs);
628 if ($kid->name eq "list" and B::Deparse::want_list $kid) {
629 # When an indirect object isn't a bareword but the args are in
630 # parens, the parens aren't part of the method syntax (the LLAFR
631 # doesn't apply), but they make a list with OPf_PARENS set that
632 # doesn't get flattened by the append_elem that adds the method,
633 # making a (object, arg1, arg2, ...) list where the object
634 # usually is. This can be distinguished from
635 # '($obj, $arg1, $arg2)->meth()' (which is legal if $arg2 is an
636 # object) because in the later the list is in scalar context
637 # as the left side of -> always is, while in the former
638 # the list is in list context as method arguments always are.
639 # (Good thing there aren't method prototypes!)
640 $meth = $kid->sibling;
641 push @other_ops, $kid->first;
642 $kid = $kid->first->sibling; # skip pushmark
643 $obj = $kid;
644 $kid = $kid->sibling;
645 for (; not B::Deparse::null $kid; $kid = $kid->sibling) {
646 push @exprs, $kid;
647 }
648 } else {
649 $obj = $kid;
650 $kid = $kid->sibling;
651 for (; !B::Deparse::null ($kid->sibling) && $kid->name!~/^method(?:_named)?\z/;
652 $kid = $kid->sibling) {
653 push @exprs, $kid
654 }
655 $meth = $kid;
656 }
657  
658 if ($meth->name eq "method_named") {
659 $meth = $self->meth_sv($meth)->PV;
660 } elsif ($meth->name eq "method_super") {
661 $meth = "SUPER::".$self->meth_sv($meth)->PV;
662 } elsif ($meth->name eq "method_redir") {
663 $meth = $self->meth_rclass_sv($meth)->PV.'::'.$self->meth_sv($meth)->PV;
664 } elsif ($meth->name eq "method_redir_super") {
665 $meth = $self->meth_rclass_sv($meth)->PV.'::SUPER::'.
666 $self->meth_sv($meth)->PV;
667 } else {
668 $meth = $meth->first;
669 if ($meth->name eq "const") {
670 # As of 5.005_58, this case is probably obsoleted by the
671 # method_named case above
672 $meth = $self->const_sv($meth)->PV; # needs to be bare
673 }
674 }
675  
676 return {
677 method => $meth,
678 variable_method => ref($meth),
679 object => $obj,
680 args => \@exprs,
681 other_ops => \@other_ops
682 }, $cx;
683 }
684  
685 sub e_method {
686 my ($self, $op, $minfo, $cx) = @_;
687 my $obj = $self->deparse($minfo->{object}, 24, $op);
688 my @body = ($obj);
689 my $other_ops = $minfo->{other_ops};
690  
691 my $meth = $minfo->{method};
692 my $meth_info;
693 if ($minfo->{variable_method}) {
694 $meth_info = $self->deparse($meth, 1, $op);
695 push @body, $meth_info;
696 }
697 my @args = map { $self->deparse($_, 6, $op) } @{$minfo->{args}};
698 push @body, @args;
699 my @args_texts = map $_->{text}, @args;
700 my $args = join(", ", @args_texts);
701  
702 my $opts = {body => \@body, other_ops => $other_ops};
703 my @texts = ();
704 my $type;
705  
706 if ($minfo->{object}->name eq 'scope' && B::Deparse::want_list $minfo->{object}) {
707 # method { $object }
708 # This must be deparsed this way to preserve list context
709 # of $object.
710 my $need_paren = $cx >= 6;
711 if ($need_paren) {
712 @texts = ('(', $meth, substr($obj,2),
713 $args, ')');
714 $type = 'e_method_list_paren';
715 } else {
716 @texts = ($meth, substr($obj,2), $args);
717 $type = 'e_method_list';
718 }
719 return info_from_list($op, $self, \@texts, '', $type, $opts);
720 }
721 if (length $args) {
722 @texts = ($obj->{text}, '->', $meth, '(', $args, ')');
723 $type = 'e_method_args';
724 } else {
725 @texts = ($obj->{text}, '->', $meth);
726 $type = 'e_method_null';
727 }
728 return info_from_list($op, $self, \@texts, '', $type, $opts);
729 }
730  
731 # returns "&" and the argument bodies if the prototype doesn't match the args,
732 # or ("", $args_after_prototype_demunging) if it does.
733 sub check_proto {
734 my $self = shift;
735 my $op = shift;
736 return ('&', []) if $self->{'noproto'};
737 my($proto, @args) = @_;
738 my($arg, $real);
739 my $doneok = 0;
740 my @reals;
741 # An unbackslashed @ or % gobbles up the rest of the args
742 1 while $proto =~ s/(?<!\\)([@%])[^\]]+$/$1/;
743 $proto =~ s/^\s*//;
744 while ($proto) {
745 $proto =~ s/^(\\?[\$\@&%*_]|\\\[[\$\@&%*]+\]|;)\s*//;
746 my $chr = $1;
747 if ($chr eq "") {
748 return ('&', []) if @args;
749 } elsif ($chr eq ";") {
750 $doneok = 1;
751 } elsif ($chr eq "@" or $chr eq "%") {
752 push @reals, map($self->deparse($_, 6), @args, $op);
753 @args = ();
754 } else {
755 $arg = shift @args;
756 last unless $arg;
757 if ($chr eq "\$" || $chr eq "_") {
758 if (B::Deparse::want_scalar $arg) {
759 push @reals, $self->deparse($arg, 6, $op);
760 } else {
761 return ('&', []);
762 }
763 } elsif ($chr eq "&") {
764 if ($arg->name =~ /^(s?refgen|undef)$/) {
765 push @reals, $self->deparse($arg, 6, $op);
766 } else {
767 return ('&', []);
768 }
769 } elsif ($chr eq "*") {
770 if ($arg->name =~ /^s?refgen$/
771 and $arg->first->first->name eq "rv2gv")
772 {
773 $real = $arg->first->first; # skip refgen, null
774 if ($real->first->name eq "gv") {
775 push @reals, $self->deparse($real, 6, $op);
776 } else {
777 push @reals, $self->deparse($real->first, 6, $op);
778 }
779 } else {
780 return ('&', []);
781 }
782 } elsif (substr($chr, 0, 1) eq "\\") {
783 $chr =~ tr/\\[]//d;
784 if ($arg->name =~ /^s?refgen$/ and
785 !B::Deparse::null($real = $arg->first) and
786 ($chr =~ /\$/ && B::Deparse::is_scalar($real->first)
787 or ($chr =~ /@/
788 && class($real->first->sibling) ne 'NULL'
789 && $real->first->sibling->name
790 =~ /^(rv2|pad)av$/)
791 or ($chr =~ /%/
792 && class($real->first->sibling) ne 'NULL'
793 && $real->first->sibling->name
794 =~ /^(rv2|pad)hv$/)
795 #or ($chr =~ /&/ # This doesn't work
796 # && $real->first->name eq "rv2cv")
797 or ($chr =~ /\*/
798 && $real->first->name eq "rv2gv")))
799 {
800 push @reals, $self->deparse($real, 6, $op);
801 } else {
802 return ('&', []);
803 }
804 }
805 }
806 }
807 return ('&', []) if $proto and !$doneok; # too few args and no ';'
808 return ('&', []) if @args; # too many args
809 return ('', \@reals);
810 }
811  
812 sub retscalar {
813 my $name = $_[0]->name;
814 # XXX There has to be a better way of doing this scalar-op check.
815 # Currently PL_opargs is not exposed.
816 if ($name eq 'null') {
817 $name = substr B::ppname($_[0]->targ), 3
818 }
819 $name =~ /^(?:scalar|pushmark|wantarray|const|gvsv|gv|padsv|rv2gv
820 |rv2sv|av2arylen|anoncode|prototype|srefgen|ref|bless
821 |regcmaybe|regcreset|regcomp|qr|subst|substcont|trans
822 |transr|sassign|chop|schop|chomp|schomp|defined|undef
823 |study|pos|preinc|i_preinc|predec|i_predec|postinc
824 |i_postinc|postdec|i_postdec|pow|multiply|i_multiply
825 |divide|i_divide|modulo|i_modulo|add|i_add|subtract
826 |i_subtract|concat|stringify|left_shift|right_shift|lt
827 |i_lt|gt|i_gt|le|i_le|ge|i_ge|eq|i_eq|ne|i_ne|n_cmp|i_cmp
828 |s_lt|s_gt|s_le|s_ge|s_eq|s_ne|s_cmp|([isn]_)?bit_(?:and|x?or)|negate
829 |i_negate|not|([isn]_)?complement|smartmatch|atan2|sin|cos
830 |rand|srand|exp|log|sqrt|int|hex|oct|abs|length|substr
831 |vec|index|rindex|sprintf|formline|ord|chr|crypt|ucfirst
832 |lcfirst|uc|lc|quotemeta|aelemfast|aelem|exists|helem
833 |pack|join|anonlist|anonhash|push|pop|shift|unshift|xor
834 |andassign|orassign|dorassign|warn|die|reset|nextstate
835 |dbstate|unstack|last|next|redo|dump|goto|exit|open|close
836 |pipe_op|fileno|umask|binmode|tie|untie|tied|dbmopen
837 |dbmclose|select|getc|read|enterwrite|prtf|print|say
838 |sysopen|sysseek|sysread|syswrite|eof|tell|seek|truncate
839 |fcntl|ioctl|flock|send|recv|socket|sockpair|bind|connect
840 |listen|accept|shutdown|gsockopt|ssockopt|getsockname
841 |getpeername|ftrread|ftrwrite|ftrexec|fteread|ftewrite
842 |fteexec|ftis|ftsize|ftmtime|ftatime|ftctime|ftrowned
843 |fteowned|ftzero|ftsock|ftchr|ftblk|ftfile|ftdir|ftpipe
844 |ftsuid|ftsgid|ftsvtx|ftlink|fttty|fttext|ftbinary|chdir
845 |chown|chroot|unlink|chmod|utime|rename|link|symlink
846 |readlink|mkdir|rmdir|open_dir|telldir|seekdir|rewinddir
847 |closedir|fork|wait|waitpid|system|exec|kill|getppid
848 |getpgrp|setpgrp|getpriority|setpriority|time|alarm|sleep
849 |shmget|shmctl|shmread|shmwrite|msgget|msgctl|msgsnd
850 |msgrcv|semop|semget|semctl|hintseval|shostent|snetent
851 |sprotoent|sservent|ehostent|enetent|eprotoent|eservent
852 |spwent|epwent|sgrent|egrent|getlogin|syscall|lock|runcv
853 |i_aelem|n_aelem|s_aelem|aelem_u|i_aelem_u|n_aelem_u|s_aelem_u
854 |u_add|u_multiply|u_subtract
855 |fc)\z/x
856 }
857  
858 sub pp_enterxssub { goto &pp_entersub; }
859  
860 # FIXME: go over
861 # sub pp_entersub {
862 # my $self = shift;
863 # my($op, $cx) = @_;
864 # return $self->e_method($self->_method($op, $cx))
865 # unless null $op->first->sibling;
866 # my $prefix = "";
867 # my $amper = "";
868 # my($kid, @exprs);
869 # if ($op->flags & OPf_SPECIAL && !($op->flags & OPf_MOD)) {
870 # $prefix = "do ";
871 # } elsif ($op->private & OPpENTERSUB_AMPER) {
872 # $amper = "&";
873 # }
874 # $kid = $op->first;
875 # $kid = $kid->first->sibling; # skip ex-list, pushmark
876 # for (; not null $kid->sibling; $kid = $kid->sibling) {
877 # push @exprs, $kid;
878 # }
879 # my $simple = 0;
880 # my $proto = undef;
881 # my $lexical;
882 # if (is_scope($kid)) {
883 # $amper = "&";
884 # $kid = "{" . $self->deparse($kid, 0) . "}";
885 # } elsif ($kid->first->name eq "gv") {
886 # my $gv = $self->gv_or_padgv($kid->first);
887 # my $cv;
888 # if (class($gv) eq 'GV' && class($cv = $gv->CV) ne "SPECIAL"
889 # || $gv->FLAGS & SVf_ROK && class($cv = $gv->RV) eq 'CV') {
890 # $proto = $cv->PV if $cv->FLAGS & SVf_POK;
891 # }
892 # $simple = 1; # only calls of named functions can be prototyped
893 # $kid = $self->deparse($kid, 24);
894 # my $fq;
895 # # Fully qualify any sub name that conflicts with a lexical.
896 # if ($self->lex_in_scope("&$kid")
897 # || $self->lex_in_scope("&$kid", 1))
898 # {
899 # $fq++;
900 # } elsif (!$amper) {
901 # if ($kid eq 'main::') {
902 # $kid = '::';
903 # }
904 # else {
905 # if ($kid !~ /::/ && $kid ne 'x') {
906 # # Fully qualify any sub name that is also a keyword. While
907 # # we could check the import flag, we cannot guarantee that
908 # # the code deparsed so far would set that flag, so we qual-
909 # # ify the names regardless of importation.
910 # if (exists $feature_keywords{$kid}) {
911 # $fq++ if $self->feature_enabled($kid);
912 # } elsif (do { local $@; local $SIG{__DIE__};
913 # eval { () = prototype "CORE::$kid"; 1 } }) {
914 # $fq++
915 # }
916 # }
917 # if ($kid !~ /^(?:\w|::)(?:[\w\d]|::(?!\z))*\z/) {
918 # $kid = single_delim("q", "'", $kid, $self) . '->';
919 # }
920 # }
921 # }
922 # $fq and substr $kid, 0, 0, = $self->{'curstash'}.'::';
923 # } elsif (is_scalar ($kid->first) && $kid->first->name ne 'rv2cv') {
924 # $amper = "&";
925 # $kid = $self->deparse($kid, 24);
926 # } else {
927 # $prefix = "";
928 # my $grandkid = $kid->first;
929 # my $arrow = ($lexical = $grandkid->name eq "padcv")
930 # || is_subscriptable($grandkid)
931 # ? ""
932 # : "->";
933 # $kid = $self->deparse($kid, 24) . $arrow;
934 # if ($lexical) {
935 # my $padlist = $self->{'curcv'}->PADLIST;
936 # my $padoff = $grandkid->targ;
937 # my $padname = $padlist->ARRAYelt(0)->ARRAYelt($padoff);
938 # my $protocv = $padname->FLAGS & SVpad_STATE
939 # ? $padlist->ARRAYelt(1)->ARRAYelt($padoff)
940 # : $padname->PROTOCV;
941 # if ($protocv->FLAGS & SVf_POK) {
942 # $proto = $protocv->PV
943 # }
944 # $simple = 1;
945 # }
946 # }
947  
948 # # Doesn't matter how many prototypes there are, if
949 # # they haven't happened yet!
950 # my $declared = $lexical || exists $self->{'subs_declared'}{$kid};
951 # if (not $declared and $self->{'in_coderef2text'}) {
952 # no strict 'refs';
953 # no warnings 'uninitialized';
954 # $declared =
955 # (
956 # defined &{ ${$self->{'curstash'}."::"}{$kid} }
957 # && !exists
958 # $self->{'subs_deparsed'}{$self->{'curstash'}."::".$kid}
959 # && defined prototype $self->{'curstash'}."::".$kid
960 # );
961 # }
962 # if (!$declared && defined($proto)) {
963 # # Avoid "too early to check prototype" warning
964 # ($amper, $proto) = ('&');
965 # }
966  
967 # my $args;
968 # my $listargs = 1;
969 # if ($declared and defined $proto and not $amper) {
970 # ($amper, $args) = $self->check_proto($proto, @exprs);
971 # $listargs = $amper;
972 # }
973 # if ($listargs) {
974 # $args = join(", ", map(
975 # ($_->flags & OPf_WANT) == OPf_WANT_SCALAR
976 # && !retscalar($_)
977 # ? $self->maybe_parens_unop('scalar', $_, 6)
978 # : $self->deparse($_, 6),
979 # @exprs
980 # ));
981 # }
982 # if ($prefix or $amper) {
983 # if ($kid eq '&') { $kid = "{$kid}" } # &{&} cannot be written as &&
984 # if ($op->flags & OPf_STACKED) {
985 # return $prefix . $amper . $kid . "(" . $args . ")";
986 # } else {
987 # return $prefix . $amper. $kid;
988 # }
989 # } else {
990 # # It's a syntax error to call CORE::GLOBAL::foo with a prefix,
991 # # so it must have been translated from a keyword call. Translate
992 # # it back.
993 # $kid =~ s/^CORE::GLOBAL:://;
994  
995 # my $dproto = defined($proto) ? $proto : "undefined";
996 # my $scalar_proto = $dproto =~ /^;*(?:[\$*_+]|\\.|\\\[[^]]\])\z/;
997 # if (!$declared) {
998 # return "$kid(" . $args . ")";
999 # } elsif ($dproto =~ /^\s*\z/) {
1000 # return $kid;
1001 # } elsif ($scalar_proto and is_scalar($exprs[0])) {
1002 # # is_scalar is an excessively conservative test here:
1003 # # really, we should be comparing to the precedence of the
1004 # # top operator of $exprs[0] (ala unop()), but that would
1005 # # take some major code restructuring to do right.
1006 # return $self->maybe_parens_func($kid, $args, $cx, 16);
1007 # } elsif (not $scalar_proto and defined($proto) || $simple) { #'
1008 # return $self->maybe_parens_func($kid, $args, $cx, 5);
1009 # } else {
1010 # return "$kid(" . $args . ")";
1011 # }
1012 # }
1013 # }
1014  
1015 sub pp_enterwrite { unop(@_, "write") }
1016  
1017 # Split a floating point number into an integer mantissa and a binary
1018 # exponent. Assumes you've already made sure the number isn't zero or
1019 # some weird infinity or NaN.
1020 sub split_float {
1021 my($f) = @_;
1022 my $exponent = 0;
1023 if ($f == int($f)) {
1024 while ($f % 2 == 0) {
1025 $f /= 2;
1026 $exponent++;
1027 }
1028 } else {
1029 while ($f != int($f)) {
1030 $f *= 2;
1031 $exponent--;
1032 }
1033 }
1034 my $mantissa = sprintf("%.0f", $f);
1035 return ($mantissa, $exponent);
1036 }
1037  
1038 # OP_STRINGIFY is a listop, but it only ever has one arg
1039 sub pp_stringify {
1040 my ($self, $op, $cx) = @_;
1041 my $kid = $op->first->sibling;
1042 my @other_ops = ();
1043 while ($kid->name eq 'null' && !B::Deparse::null($kid->first)) {
1044 push(@other_ops, $kid);
1045 $kid = $kid->first;
1046 }
1047 my $info;
1048 if ($kid->name =~ /^(?:const|padsv|rv2sv|av2arylen|gvsv|multideref
1049 |aelemfast(?:_lex)?|[ah]elem|join|concat)\z/x) {
1050 $info = maybe_targmy(@_, \&dquote);
1051 }
1052 else {
1053 # Actually an optimised join.
1054 my $info = listop(@_,"join");
1055 $info->{text} =~ s/join([( ])/join$1$self->{'ex_const'}, /;
1056 }
1057 push @{$info->{other_ops}}, @other_ops;
1058 return $info;
1059 }
1060  
1061 # Only used by tr///, so backslashes hyphens
1062 sub pchr { # ASCII
1063 my($n) = @_;
1064 if ($n == ord '\\') {
1065 return '\\\\';
1066 } elsif ($n == ord "-") {
1067 return "\\-";
1068 } elsif ($n >= ord(' ') and $n <= ord('~')) {
1069 return chr($n);
1070 } elsif ($n == ord "\a") {
1071 return '\\a';
1072 } elsif ($n == ord "\b") {
1073 return '\\b';
1074 } elsif ($n == ord "\t") {
1075 return '\\t';
1076 } elsif ($n == ord "\n") {
1077 return '\\n';
1078 } elsif ($n == ord "\e") {
1079 return '\\e';
1080 } elsif ($n == ord "\f") {
1081 return '\\f';
1082 } elsif ($n == ord "\r") {
1083 return '\\r';
1084 } elsif ($n >= ord("\cA") and $n <= ord("\cZ")) {
1085 return '\\c' . chr(ord("@") + $n);
1086 } else {
1087 # return '\x' . sprintf("%02x", $n);
1088 return '\\' . sprintf("%03o", $n);
1089 }
1090 }
1091  
1092 sub collapse {
1093 my(@chars) = @_;
1094 my($str, $c, $tr) = ("");
1095 for ($c = 0; $c < @chars; $c++) {
1096 $tr = $chars[$c];
1097 $str .= pchr($tr);
1098 if ($c <= $#chars - 2 and $chars[$c + 1] == $tr + 1 and
1099 $chars[$c + 2] == $tr + 2)
1100 {
1101 for (; $c <= $#chars-1 and $chars[$c + 1] == $chars[$c] + 1; $c++)
1102 {}
1103 $str .= "-";
1104 $str .= pchr($chars[$c]);
1105 }
1106 }
1107 return $str;
1108 }
1109  
1110 sub tr_decode_byte {
1111 my($table, $flags) = @_;
1112 my(@table) = unpack("s*", $table);
1113 splice @table, 0x100, 1; # Number of subsequent elements
1114 my($c, $tr, @from, @to, @delfrom, $delhyphen);
1115 if ($table[ord "-"] != -1 and
1116 $table[ord("-") - 1] == -1 || $table[ord("-") + 1] == -1)
1117 {
1118 $tr = $table[ord "-"];
1119 $table[ord "-"] = -1;
1120 if ($tr >= 0) {
1121 @from = ord("-");
1122 @to = $tr;
1123 } else { # -2 ==> delete
1124 $delhyphen = 1;
1125 }
1126 }
1127 for ($c = 0; $c < @table; $c++) {
1128 $tr = $table[$c];
1129 if ($tr >= 0) {
1130 push @from, $c; push @to, $tr;
1131 } elsif ($tr == -2) {
1132 push @delfrom, $c;
1133 }
1134 }
1135 @from = (@from, @delfrom);
1136 if ($flags & OPpTRANS_COMPLEMENT) {
1137 my @newfrom = ();
1138 my %from;
1139 @from{@from} = (1) x @from;
1140 for ($c = 0; $c < 256; $c++) {
1141 push @newfrom, $c unless $from{$c};
1142 }
1143 @from = @newfrom;
1144 }
1145 unless ($flags & OPpTRANS_DELETE || !@to) {
1146 pop @to while $#to and $to[$#to] == $to[$#to -1];
1147 }
1148 my($from, $to);
1149 $from = collapse(@from);
1150 $to = collapse(@to);
1151 $from .= "-" if $delhyphen;
1152 return ($from, $to);
1153 }
1154  
1155 # XXX This doesn't yet handle all cases correctly either
1156  
1157 sub tr_decode_utf8 {
1158 my($swash_hv, $flags) = @_;
1159 my %swash = $swash_hv->ARRAY;
1160 my $final = undef;
1161 $final = $swash{'FINAL'}->IV if exists $swash{'FINAL'};
1162 my $none = $swash{"NONE"}->IV;
1163 my $extra = $none + 1;
1164 my(@from, @delfrom, @to);
1165 my $line;
1166 foreach $line (split /\n/, $swash{'LIST'}->PV) {
1167 my($min, $max, $result) = split(/\t/, $line);
1168 $min = hex $min;
1169 if (length $max) {
1170 $max = hex $max;
1171 } else {
1172 $max = $min;
1173 }
1174 $result = hex $result;
1175 if ($result == $extra) {
1176 push @delfrom, [$min, $max];
1177 } else {
1178 push @from, [$min, $max];
1179 push @to, [$result, $result + $max - $min];
1180 }
1181 }
1182 for my $i (0 .. $#from) {
1183 if ($from[$i][0] == ord '-') {
1184 unshift @from, splice(@from, $i, 1);
1185 unshift @to, splice(@to, $i, 1);
1186 last;
1187 } elsif ($from[$i][1] == ord '-') {
1188 $from[$i][1]--;
1189 $to[$i][1]--;
1190 unshift @from, ord '-';
1191 unshift @to, ord '-';
1192 last;
1193 }
1194 }
1195 for my $i (0 .. $#delfrom) {
1196 if ($delfrom[$i][0] == ord '-') {
1197 push @delfrom, splice(@delfrom, $i, 1);
1198 last;
1199 } elsif ($delfrom[$i][1] == ord '-') {
1200 $delfrom[$i][1]--;
1201 push @delfrom, ord '-';
1202 last;
1203 }
1204 }
1205 if (defined $final and $to[$#to][1] != $final) {
1206 push @to, [$final, $final];
1207 }
1208 push @from, @delfrom;
1209 if ($flags & OPpTRANS_COMPLEMENT) {
1210 my @newfrom;
1211 my $next = 0;
1212 for my $i (0 .. $#from) {
1213 push @newfrom, [$next, $from[$i][0] - 1];
1214 $next = $from[$i][1] + 1;
1215 }
1216 @from = ();
1217 for my $range (@newfrom) {
1218 if ($range->[0] <= $range->[1]) {
1219 push @from, $range;
1220 }
1221 }
1222 }
1223 my($from, $to, $diff);
1224 for my $chunk (@from) {
1225 $diff = $chunk->[1] - $chunk->[0];
1226 if ($diff > 1) {
1227 $from .= tr_chr($chunk->[0]) . "-" . tr_chr($chunk->[1]);
1228 } elsif ($diff == 1) {
1229 $from .= tr_chr($chunk->[0]) . tr_chr($chunk->[1]);
1230 } else {
1231 $from .= tr_chr($chunk->[0]);
1232 }
1233 }
1234 for my $chunk (@to) {
1235 $diff = $chunk->[1] - $chunk->[0];
1236 if ($diff > 1) {
1237 $to .= tr_chr($chunk->[0]) . "-" . tr_chr($chunk->[1]);
1238 } elsif ($diff == 1) {
1239 $to .= tr_chr($chunk->[0]) . tr_chr($chunk->[1]);
1240 } else {
1241 $to .= tr_chr($chunk->[0]);
1242 }
1243 }
1244 #$final = sprintf("%04x", $final) if defined $final;
1245 #$none = sprintf("%04x", $none) if defined $none;
1246 #$extra = sprintf("%04x", $extra) if defined $extra;
1247 #print STDERR "final: $final\n none: $none\nextra: $extra\n";
1248 #print STDERR $swash{'LIST'}->PV;
1249 return (B::Deparse::escape_str($from), B::Deparse::escape_str($to));
1250 }
1251  
1252 sub pp_trans {
1253 my $self = shift;
1254 my($op, $cx) = @_;
1255 my($from, $to);
1256 my $class = class($op);
1257 my $priv_flags = $op->private;
1258 if ($class eq "PVOP") {
1259 ($from, $to) = tr_decode_byte($op->pv, $priv_flags);
1260 } elsif ($class eq "PADOP") {
1261 ($from, $to)
1262 = tr_decode_utf8($self->padval($op->padix)->RV, $priv_flags);
1263 } else { # class($op) eq "SVOP"
1264 ($from, $to) = tr_decode_utf8($op->sv->RV, $priv_flags);
1265 }
1266 my $flags = "";
1267 $flags .= "c" if $priv_flags & OPpTRANS_COMPLEMENT;
1268 $flags .= "d" if $priv_flags & OPpTRANS_DELETE;
1269 $to = "" if $from eq $to and $flags eq "";
1270 $flags .= "s" if $priv_flags & OPpTRANS_SQUASH;
1271 return info_from_list($op, $self, ['tr', double_delim($from, $to), $flags],
1272 '', 'pp_trans', {});
1273 }
1274  
1275 sub pp_transr {
1276 my $self = $_[0];
1277 my $op = $_[1];
1278 my $info = pp_trans(@_);
1279 return info_from_text($op, $self, $info->{text} . 'r', 'pp_transr',
1280 {body => [$info]});
1281 }
1282  
1283 # Like dq(), but different
1284 sub re_dq {
1285 my $self = shift;
1286 my ($op) = @_;
1287 my ($re_dq_info, $fmt);
1288  
1289 my $type = $op->name;
1290 if ($type eq "const") {
1291 return '$[' if $op->private & OPpCONST_ARYBASE;
1292 my $unbacked = B::Deparse::re_unback($self->const_sv($op)->as_string);
1293 return B::Deparse::re_uninterp(escape_re($unbacked));
1294 } elsif ($type eq "concat") {
1295 my $first = $self->re_dq($op->first);
1296 my $last = $self->re_dq($op->last);
1297 return B::Deparse::re_dq_disambiguate($first, $last);
1298 } elsif ($type eq "uc") {
1299 $re_dq_info = $self->re_dq($op->first->sibling);
1300 $fmt = '\U%c\E';
1301 $type .= ' uc';
1302 } elsif ($type eq "lc") {
1303 $re_dq_info = $self->re_dq($op->first->sibling);
1304 $fmt = '\L%c\E';
1305 $type .= ' lc';
1306 } elsif ($type eq "ucfirst") {
1307 $re_dq_info = $self->re_dq($op->first->sibling);
1308 $fmt = '\u%c';
1309 $type .= ' ucfirst';
1310 } elsif ($type eq "lcfirst") {
1311 $re_dq_info = $self->re_dq($op->first->sibling);
1312 $fmt = '\u%c';
1313 $type .= ' lcfirst';
1314 } elsif ($type eq "quotemeta") {
1315 $re_dq_info = $self->re_dq($op->first->sibling);
1316 $fmt = '\Q%c\E';
1317 $type .= ' quotemeta';
1318 } elsif ($type eq "fc") {
1319 $re_dq_info = $self->re_dq($op->first->sibling);
1320 $fmt = '\F%c\E';
1321 $type .= ' fc';
1322 } elsif ($type eq "join") {
1323 return $self->deparse($op->last, 26); # was join($", @ary)
1324 } else {
1325 my $ret = $self->deparse($op, 26);
1326 $ret =~ s/^\$([(|)])\z/\${$1}/ # $( $| $) need braces
1327 or $ret =~ s/^\@([-+])\z/\@{$1}/; # @- @+ need braces
1328 return $ret;
1329 }
1330 return $self->info_from_template($type, $op->first->sibling,
1331 $fmt, [$re_dq_info], [0]);
1332 }
1333  
1334 sub pure_string {
1335 my ($self, $op) = @_;
1336 return 0 if B::Deparse::null $op;
1337 my $type = $op->name;
1338  
1339 if ($type eq 'const' || $type eq 'av2arylen') {
1340 return 1;
1341 }
1342 elsif ($type =~ /^(?:[ul]c(first)?|fc)$/ || $type eq 'quotemeta') {
1343 return $self->pure_string($op->first->sibling);
1344 }
1345 elsif ($type eq 'join') {
1346 my $join_op = $op->first->sibling; # Skip pushmark
1347 return 0 unless $join_op->name eq 'null' && $join_op->targ == OP_RV2SV;
1348  
1349 my $gvop = $join_op->first;
1350 return 0 unless $gvop->name eq 'gvsv';
1351 return 0 unless '"' eq $self->gv_name($self->gv_or_padgv($gvop));
1352  
1353 return 0 unless ${$join_op->sibling} eq ${$op->last};
1354 return 0 unless $op->last->name =~ /^(?:[ah]slice|(?:rv2|pad)av)$/;
1355 }
1356 elsif ($type eq 'concat') {
1357 return $self->pure_string($op->first)
1358 && $self->pure_string($op->last);
1359 }
1360 elsif (B::Deparse::is_scalar($op) || $type =~ /^[ah]elem$/) {
1361 return 1;
1362 }
1363 elsif ($type eq "null" and $op->can('first') and not B::Deparse::null $op->first and
1364 ($op->first->name eq "null" and $op->first->can('first')
1365 and not B::Deparse::null $op->first->first and
1366 $op->first->first->name eq "aelemfast"
1367 or
1368 $op->first->name =~ /^aelemfast(?:_lex)?\z/
1369 )) {
1370 return 1;
1371 }
1372 else {
1373 return 0;
1374 }
1375  
1376 return 1;
1377 }
1378  
1379 sub regcomp
1380 {
1381 my($self, $op, $cx, $extended) = @_;
1382 my @other_ops = ();
1383 my $kid = $op->first;
1384 if ($kid->name eq "regcmaybe") {
1385 push @other_ops, $kid;
1386 $kid = $kid->first;
1387 }
1388 if ($kid->name eq "regcreset") {
1389 push @other_ops, $kid;
1390 $kid = $kid->first;
1391 }
1392 if ($kid->name eq "null" and !B::Deparse::null($kid->first)
1393 and $kid->first->name eq 'pushmark') {
1394 my $str = '';
1395 push(@other_ops, $kid);
1396 $kid = $kid->first->sibling;
1397 my @body = ();
1398 while (!B::Deparse::null($kid)) {
1399 my $first = $str;
1400 my $last = $self->re_dq($kid, $extended);
1401 push @body, $last;
1402 push(@other_ops, $kid);
1403 $str = B::Deparse::re_dq_disambiguate($first,
1404 $self->info2str($last));
1405 $kid = $kid->sibling;
1406 }
1407 return (info_from_text($op, $self, $str, 'regcomp',
1408 {other_ops => \@other_ops,
1409 body => \@body}), 1);
1410 }
1411  
1412 if ($self->pure_string($kid)) {
1413 my $info = $self->re_dq($kid, $extended);
1414 my @kid_ops = $info->{other_ops} ? @{$info->{other_ops}} : ();
1415 push @other_ops, @kid_ops;
1416 $info->{other_ops} = \@other_ops;
1417 return ($info, 1);
1418 }
1419 return ($self->deparse($kid, $cx, $op), 0, $op);
1420 }
1421  
1422 sub pp_match { matchop(@_, "m", "/") }
1423 sub pp_pushre { matchop(@_, "m", "/") }
1424 sub pp_qr { matchop(@_, "qr", "") }
1425  
1426 # FIXME: I guessed these. Are they right?
1427 sub pp_s_cmp { binop(@_, "cmp", 14) }
1428 sub pp_s_eq { binop(@_, "cmp", 14) }
1429 sub pp_s_ne { binop(@_, "cmp", 14) }
1430  
1431 sub pp_split
1432 {
1433 my($self, $op, $cx) = @_;
1434 my($kid, @exprs, $ary_info, $expr);
1435 my $stacked = $op->flags & OPf_STACKED;
1436 my $ary = '';
1437 my @body = ();
1438 my @other_ops = ();
1439 $kid = $op->first;
1440  
1441 $kid = $op->first;
1442 $kid = $kid->sibling if $kid->name eq 'regcomp';
1443 for (; !B::Deparse::null($kid); $kid = $kid->sibling) {
1444 push @exprs, $self->deparse($kid, 6, $op);
1445 }
1446  
1447 unshift @exprs, $self->matchop($op, $cx, "m", "/");
1448  
1449 if ($op->private & OPpSPLIT_ASSIGN) {
1450 # With C<@array = split(/pat/, str);>,
1451 # array is stored in split's pmreplroot; either
1452 # as an integer index into the pad (for a lexical array)
1453 # or as GV for a package array (which will be a pad index
1454 # on threaded builds)
1455 # With my/our @array = split(/pat/, str), the array is instead
1456 # accessed via an extra padav/rv2av op at the end of the
1457 # split's kid ops.
1458  
1459 if ($stacked) {
1460 $ary = pop @exprs;
1461 }
1462 else {
1463 if ($op->private & OPpSPLIT_LEX) {
1464 $ary = $self->padname($op->pmreplroot);
1465 }
1466 else {
1467 # union with op_pmtargetoff, op_pmtargetgv
1468 my $gv = $op->pmreplroot;
1469 $gv = $self->padval($gv) if !ref($gv);
1470 $ary = $self->maybe_local(@_,
1471 $self->stash_variable('@',
1472 $self->gv_name($gv),
1473 $cx))
1474 }
1475 if ($op->private & OPpLVAL_INTRO) {
1476 $ary = $op->private & OPpSPLIT_LEX ? "my $ary" : "local $ary";
1477 }
1478 }
1479 }
1480  
1481 push @body, @exprs;
1482 my $opts = {body => \@exprs};
1483  
1484 # handle special case of split(), and split(' ') that compiles to /\s+/
1485 if (($op->reflags // 0) & RXf_SKIPWHITE()) {
1486 my $expr0 = $exprs[0];
1487 my $expr0b0 = $expr0->{body}[0];
1488 my $bsep = $expr0b0->{sep};
1489 my $sep = $expr0->{sep};
1490 $expr0b0->{texts}[1] = ' ';
1491 substr($expr0b0->{text}, 1, 0) = ' ';
1492 substr($expr0->{texts}[0], 1, 0) = ' ';
1493 substr($expr0->{text}, 1, 0) = ' ';
1494 }
1495 my @args_texts = map $_->{text}, @exprs;
1496  
1497 my $sep = '';
1498 my $type;
1499 my @expr_texts;
1500 if ($ary) {
1501 @expr_texts = ("$ary", '=', join(', ', @args_texts));
1502 $sep = ' ';
1503 $type = 'split_array';
1504 $opts->{maybe_parens} = [$self, $cx, 7];
1505 } else {
1506 @expr_texts = ('split', '(', join(', ', @args_texts), ')');
1507 $type = 'split';
1508  
1509 }
1510 return info_from_list($op, $self, \@expr_texts, $sep, $type, $opts);
1511 }
1512  
1513 # Kind of silly, but we prefer, subst regexp flags joined together to
1514 # make words. For example: s/a/b/xo => s/a/b/ox
1515  
1516 # oxime -- any of various compounds obtained chiefly by the action of
1517 # hydroxylamine on aldehydes and ketones and characterized by the
1518 # bivalent grouping C=NOH [Webster's Tenth]
1519  
1520 my %substwords;
1521 map($substwords{join "", sort split //, $_} = $_, 'ego', 'egoism', 'em',
1522 'es', 'ex', 'exes', 'gee', 'go', 'goes', 'ie', 'ism', 'iso', 'me',
1523 'meese', 'meso', 'mig', 'mix', 'os', 'ox', 'oxime', 'see', 'seem',
1524 'seg', 'sex', 'sig', 'six', 'smog', 'sog', 'some', 'xi', 'rogue',
1525 'sir', 'rise', 'smore', 'more', 'seer', 'rome', 'gore', 'grim', 'grime',
1526 'or', 'rose', 'rosie');
1527  
1528 # sub pp_subst
1529 # {
1530 # my($self, $op, $cx) = @_;
1531 # my $kid = $op->first;
1532 # my($binop, $var, $re, @other_ops) = ("", "", "", ());
1533 # my @body = ();
1534 # my ($repl, $repl_info);
1535  
1536 # if ($op->flags & OPf_STACKED) {
1537 # $binop = 1;
1538 # $var = $self->deparse($kid, 20, $op);
1539 # $kid = $kid->sibling;
1540 # }
1541 # elsif (my $targ = $op->targ) {
1542 # $binop = 1;
1543 # $var = $self->padname($targ);
1544 # }
1545 # my $flags = "";
1546 # my $pmflags = $op->pmflags;
1547 # if (B::Deparse::null($op->pmreplroot)) {
1548 # $repl = $kid;
1549 # $kid = $kid->sibling;
1550 # } else {
1551 # push @other_ops, $op->pmreplroot;
1552 # $repl = $op->pmreplroot->first; # skip substcont
1553 # }
1554 # while ($repl->name eq "entereval") {
1555 # push @other_ops, $repl;
1556 # $repl = $repl->first;
1557 # $flags .= "e";
1558 # }
1559 # {
1560 # local $self->{in_subst_repl} = 1;
1561 # if ($pmflags & PMf_EVAL) {
1562 # $repl_info = $self->deparse($repl->first, 0, $repl);
1563 # } else {
1564 # $repl_info = $self->dq($repl);
1565 # }
1566 # }
1567 # my $extended = ($pmflags & PMf_EXTENDED);
1568 # if (B::Deparse::null $kid) {
1569 # my $unbacked = B::Deparse::re_unback($op->precomp);
1570 # if ($extended) {
1571 # $re = B::Deparse::re_uninterp_extended(B::Deparse::escape_extended_re($unbacked));
1572 # }
1573 # else {
1574 # $re = B::Deparse::re_uninterp(B::Deparse::escape_str($unbacked));
1575 # }
1576 # } else {
1577 # my ($re_info, $junk) = $self->regcomp($kid, 1, $extended);
1578 # push @body, $re_info;
1579 # $re = $re_info->{text};
1580 # }
1581 # $flags .= "r" if $pmflags & PMf_NONDESTRUCT;
1582 # $flags .= "e" if $pmflags & PMf_EVAL;
1583 # $flags .= $self->re_flags($op);
1584 # $flags = join '', sort split //, $flags;
1585 # $flags = $substwords{$flags} if $substwords{$flags};
1586 # my $core_s = $self->keyword("s"); # maybe CORE::s
1587 # my $info;
1588 # push @body, $repl_info;
1589 # my $repl_text = $repl_info->{text};
1590 # my $opts = {body => \@body};
1591 # my $opts->{other_ops} = \@other_ops if @other_ops;
1592 # my $find_replace_re = double_delim($re, $repl_text);
1593 # my $args = [$var, $find_replace_re];
1594 # my $args_spec = [0, 1];
1595 # if ($binop) {
1596 # my $fmt = "%c =~ $core_s%c$flags";
1597 # return $self->info_from_template("=~ s///", $op, $fmt, $args_spec, $args,
1598 # {maybe_parens => [$self, $cx, 20]});
1599 # } else {
1600 # my $fmt = "$core_s%c$flags";
1601 # return $self->info_from_template("s///", $op, $fmt, $args_spec, $args, {});
1602 # }
1603 # Carp::confess("unhandled condition in pp_subst");
1604 # }
1605  
1606 # FIXME:
1607 # Different in 5.20. Go over differences to see if okay in 5.20.
1608 sub pp_chdir {
1609 my ($self, $op, $cx) = @_;
1610 if (($op->flags & (OPf_SPECIAL|OPf_KIDS)) == (OPf_SPECIAL|OPf_KIDS)) {
1611 my $kw = $self->keyword("chdir");
1612 my $kid = $self->const_sv($op->first)->PV;
1613 my $code = $kw
1614 . ($cx >= 16 || $self->{'parens'} ? "($kid)" : " $kid");
1615 maybe_targmy(@_, sub { $_[3] }, $code);
1616 } else {
1617 maybe_targmy(@_, \&unop, "chdir")
1618 }
1619 }
1620  
1621 # Not in Perl 5.20 and presumeably < 5.20. No harm in adding to 5.20?
1622 *pp_ncomplement = *pp_complement;
1623 sub pp_scomplement { maybe_targmy(@_, \&pfixop, "~.", 21) }
1624  
1625 1;