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

filter: Statementstmt 0.0% 0 / 348 Branchbran 0.0% 0 / 114 Conditioncond 0.0% 0 / 90 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 73 Podpod 0.0% 0 / 62 totaltotal 0.0% 0 / 687 CC 58 SCAR 56.1
CC
58
Cov
0%
pp_entersub
930.0
pp_nextstate
380.0
pp_cond_expr
20.0
CRAP 272.9
1 # Common PP opcodes. Specifc Perl versions can override these.
2  
3 # Copyright (c) 2015, 2018 Rocky Bernstein
4 #
5 use strict;
6 use warnings ();
7 require feature;
8  
9 my %feature_keywords = (
10 # keyword => 'feature',
11 state => 'state',
12 say => 'say',
13 given => 'switch',
14 when => 'switch',
15 default => 'switch',
16 break => 'switch',
17 evalbytes=>'evalbytes',
18 __SUB__ => '__SUB__',
19 fc => 'fc',
20 );
21  
22 use rlib '../..';
23  
24 package B::DeparseTree::PP;
25  
26 use B::DeparseTree::Common;
27 use B::DeparseTree::Node;
28 use B::Deparse;
29  
30 *is_ifelse_cont = *B::Deparse::is_ifelse_cont;
31 *real_negate = *B::Deparse::real_negate;
32 *pp_negate = *B::Deparse::pp_negate;
33 *pp_i_negate = *B::Deparse::pp_i_negate;
34  
35 use B qw(
36 class
37 OPf_MOD OPpENTERSUB_AMPER
38 OPf_SPECIAL
39 OPf_STACKED
40 OPpEXISTS_SUB
41 SVf_POK
42 SVf_ROK
43 );
44  
45 our($VERSION, @EXPORT, @ISA);
46 $VERSION = '1.0.0';
47  
48 @ISA = qw(Exporter B::Deparse);
49 @EXPORT = qw(
50  
51 pp_and
52 pp_chomp
53 pp_chop
54 pp_complement
55 pp_cond_expr
56 pp_const
57 pp_dbstate
58 pp_defined
59 pp_dor
60 pp_egrent pp_ehostent pp_enetent
61 pp_entersub
62 pp_eprotoent pp_epwent pp_eservent
63 pp_exists
64 pp_fork pp_getlogin pp_ggrent
65 pp_getppid
66 pp_ghostent pp_gnetent pp_gprotoent
67 pp_gpwent pp_grepstart pp_gservent
68 pp_grepwhile
69 pp_i_negate
70 pp_i_predec
71 pp_i_preinc
72 pp_leave pp_lineseq
73 pp_mapstart
74 pp_mapwhile
75 pp_negate
76 pp_nextstate
77 pp_null
78 pp_once
79 pp_or
80 pp_pos
81 pp_postdec
82 pp_postinc
83 pp_predec
84 pp_preinc
85 pp_print
86 pp_prtf
87 pp_ref
88 pp_repeat
89 pp_say
90 pp_schomp
91 pp_schop
92 pp_scope
93 pp_setstate
94 pp_sgrent
95 pp_sort
96 pp_spwent
97 pp_stub
98 pp_study
99 pp_time
100 pp_tms
101 pp_undef
102 pp_wait
103 pp_wantarray
104 pp_xor
105 );
106  
107 BEGIN {
108 # List version-specific constants here.
109 # Easiest way to keep this code portable between version looks to
110 # be to fake up a dummy constant that will never actually be true.
111 foreach (qw(OPpCONST_ARYBASE OPpEVAL_BYTES)) {
112 eval { import B $_ };
113 no strict 'refs';
114 *{$_} = sub () {0} unless *{$_}{CODE};
115 }
116 }
117  
118 sub pp_chomp { maybe_targmy(@_, \&unop, "chomp") }
119 sub pp_chop { maybe_targmy(@_, \&unop, "chop") }
120 sub pp_defined { unop(@_, "defined") }
121 sub pp_egrent { baseop(@_, "endgrent") }
122 sub pp_ehostent { baseop(@_, "endhostent") }
123 sub pp_enetent { baseop(@_, "endnetent") }
124 sub pp_eprotoent { baseop(@_, "endprotoent") }
125 sub pp_epwent { baseop(@_, "endpwent") }
126 sub pp_eservent { baseop(@_, "endservent") }
127 sub pp_fork { baseop(@_, "fork") }
128 sub pp_getlogin { baseop(@_, "getlogin") }
129 sub pp_ggrent { baseop(@_, "getgrent") }
130 sub pp_ghostent { baseop(@_, "gethostent") }
131 sub pp_gnetent { baseop(@_, "getnetent") }
132 sub pp_gprotoent { baseop(@_, "getprotoent") }
133 sub pp_gpwent { baseop(@_, "getpwent") }
134 sub pp_grepstart { baseop(@_, "grep") }
135 sub pp_gservent { baseop(@_, "getservent") }
136 sub pp_leave { scopeop(1, @_); }
137 sub pp_lineseq { scopeop(0, @_); }
138 sub pp_mapstart { baseop(@_, "map") }
139 sub pp_ref { unop(@_, "ref") }
140 sub pp_schomp { maybe_targmy(@_, \&unop, "chomp") }
141 sub pp_schop { maybe_targmy(@_, \&unop, "chop") }
142 sub pp_scope { scopeop(0, @_); }
143 sub pp_sgrent { baseop(@_, "setgrent") }
144 sub pp_spwent { baseop(@_, "setpwent") }
145 sub pp_study { unop(@_, "study") }
146 sub pp_tms { baseop(@_, "times") }
147 sub pp_undef { unop(@_, "undef") }
148 sub pp_wantarray { baseop(@_, "wantarray") }
149  
150 # Notice how subs and formats are inserted between statements here;
151 # also $[ assignments and pragmas.
152 sub pp_nextstate {
153 my $self = shift;
154 my($op, $cx) = @_;
155 $self->{'curcop'} = $op;
156 my @texts;
157 push @texts, $self->cop_subs($op);
158 if (@texts) {
159 # Special marker to swallow up the semicolon
160 push @texts, "\cK";
161 }
162  
163 my $stash = $op->stashpv;
164 if ($stash ne $self->{'curstash'}) {
165 push @texts, $self->keyword("package") . " $stash;\n";
166 $self->{'curstash'} = $stash;
167 }
168  
169 if (OPpCONST_ARYBASE && $self->{'arybase'} != $op->arybase) {
170 push @texts, '$[ = '. $op->arybase .";\n";
171 $self->{'arybase'} = $op->arybase;
172 }
173  
174 my $warnings = $op->warnings;
175 my $warning_bits;
176 if ($warnings->isa("B::SPECIAL") && $$warnings == 4) {
177 $warning_bits = $warnings::Bits{"all"} & WARN_MASK;
178 }
179 elsif ($warnings->isa("B::SPECIAL") && $$warnings == 5) {
180 $warning_bits = $warnings::NONE;
181 }
182 elsif ($warnings->isa("B::SPECIAL")) {
183 $warning_bits = undef;
184 }
185 else {
186 $warning_bits = $warnings->PV & WARN_MASK;
187 }
188  
189 if (defined ($warning_bits) and
190 !defined($self->{warnings}) || $self->{'warnings'} ne $warning_bits) {
191 push @texts,
192 $self->declare_warnings($self->{'warnings'}, $warning_bits);
193 $self->{'warnings'} = $warning_bits;
194 }
195  
196 my $hints = $] < 5.008009 ? $op->private : $op->hints;
197 my $old_hints = $self->{'hints'};
198 if ($self->{'hints'} != $hints) {
199 push @texts, $self->declare_hints($self->{'hints'}, $hints);
200 $self->{'hints'} = $hints;
201 }
202  
203 my $newhh;
204 if ($] > 5.009) {
205 $newhh = $op->hints_hash->HASH;
206 }
207  
208 if ($] >= 5.015006) {
209 # feature bundle hints
210 my $from = $old_hints & $feature::hint_mask;
211 my $to = $ hints & $feature::hint_mask;
212 if ($from != $to) {
213 if ($to == $feature::hint_mask) {
214 if ($self->{'hinthash'}) {
215 delete $self->{'hinthash'}{$_}
216 for grep /^feature_/, keys %{$self->{'hinthash'}};
217 }
218 else { $self->{'hinthash'} = {} }
219 $self->{'hinthash'}
220 = _features_from_bundle($from, $self->{'hinthash'});
221 }
222 else {
223 my $bundle =
224 $feature::hint_bundles[$to >> $feature::hint_shift];
225 $bundle =~ s/(\d[13579])\z/$1+1/e; # 5.11 => 5.12
226 push @texts,
227 $self->keyword("no") . " feature ':all';\n",
228 $self->keyword("use") . " feature ':$bundle';\n";
229 }
230 }
231 }
232  
233 if ($] > 5.009) {
234 push @texts, $self->declare_hinthash(
235 $self->{'hinthash'}, $newhh,
236 $self->{indent_size}, $self->{hints},
237 );
238 $self->{'hinthash'} = $newhh;
239 }
240  
241  
242 # This should go after of any branches that add statements, to
243 # increase the chances that it refers to the same line it did in
244 # the original program.
245 if ($self->{'linenums'} && $cx != .5) { # $cx == .5 means in a format
246 my $line = sprintf("\n# line %s '%s'", $op->line, $op->file);
247 $line .= sprintf(" 0x%x", $$op) if $self->{'opaddr'};
248 push @texts, $line . "\cK\n";
249 }
250  
251 push @texts, $op->label . ": " if $op->label;
252  
253 my $info = B::DeparseTree::Node->new($op, $self->{deparse},
254 \@texts, '', 'pp_nextstate', {});
255 return $info;
256 }
257  
258 sub pp_and { logop(@_, "and", 3, "&&", 11, "if") }
259  
260  
261 sub pp_cond_expr
262 {
263 my $self = shift;
264 my($op, $cx) = @_;
265 my $cond = $op->first;
266 my $true = $cond->sibling;
267 my $false = $true->sibling;
268 my $cuddle = $self->{'cuddle'};
269 unless ($cx < 1 and (is_scope($true) and $true->name ne "null") and
270 (is_scope($false) || is_ifelse_cont($false))
271 and $self->{'expand'} < 7) {
272 my $cond_info = $self->deparse($cond, 8, $op);
273 my $true_info = $self->deparse($true, 6, $op);
274 my $false_info = $self->deparse($false, 8, $op);
275 my @texts = ($cond_info, '?', $true_info, ':', $false_info);
276 return info_from_list($op, $self, \@texts, ' ', 'ternary ?',
277 {maybe_parens => [$self, $cx, 8]});
278 }
279  
280 my $cond_info = $self->deparse($cond, 1, $op);
281 my $true_info = $self->deparse($true, 0, $op);
282 my @head = ('if ', '(', $cond_info, ') ', "{\n\t", $true_info, "\n\b}");
283 my @elsifs;
284  
285 while (!null($false) and is_ifelse_cont($false)) {
286 my $newop = $false->first;
287 my $newcond = $newop->first;
288 my $newtrue = $newcond->sibling;
289 $false = $newtrue->sibling; # last in chain is OP_AND => no else
290 if ($newcond->name eq "lineseq")
291 {
292 # lineseq to ensure correct line numbers in elsif()
293 # Bug #37302 fixed by change #33710.
294 $newcond = $newcond->first->sibling;
295 }
296 my $newcond_info = $self->deparse($newcond, 1, $op);
297 my $newtrue_info = $self->deparse($newtrue, 0, $op);
298 push @elsifs, ('', "elsif (", $newcond_info, ")",
299 "{\n\t",
300 $newtrue_info,
301 "\n\b}");
302 }
303 my $false_info;
304 my $type;
305 if (!null($false)) {
306 $false_info = $self->deparse($false, 0, $op);
307 $false_info->{text} = $cuddle . "else {\n\t" . $false_info->{text} . "\n\b}\cK";
308 $type = 'if else';
309 } else {
310 $false_info->{text} = "\cK";
311 $type = 'if';
312 }
313 my @texts = (@head, @elsifs, $false_info->{text});
314 my $text = join('', @head) . join($cuddle, @elsifs) . $false_info->{text};
315 return info_from_list($op, $self, \@texts, '', $type, {});
316 }
317  
318 sub pp_const {
319 my $self = shift;
320 my($op, $cx) = @_;
321 if ($op->private & OPpCONST_ARYBASE) {
322 return info_from_text($op, $self, '$[', 'const_ary', {});
323 }
324 # if ($op->private & OPpCONST_BARE) { # trouble with '=>' autoquoting
325 # return $self->const_sv($op)->PV;
326 # }
327 my $sv = $self->const_sv($op);
328 return $self->const($sv, $cx);;
329 }
330  
331 sub pp_dbstate { pp_nextstate(@_) }
332  
333 sub pp_entersub
334 {
335 my($self, $op, $cx) = @_;
336 return $self->e_method($op, $self->_method($op, $cx))
337 unless null $op->first->sibling;
338 my $prefix = "";
339 my $amper = "";
340 my($kid, @exprs);
341 if ($op->flags & OPf_SPECIAL && !($op->flags & OPf_MOD)) {
342 $prefix = "do ";
343 } elsif ($op->private & OPpENTERSUB_AMPER) {
344 $amper = "&";
345 }
346 $kid = $op->first;
347  
348 my $other_ops = [$kid, $kid->first];
349 $kid = $kid->first->sibling; # skip ex-list, pushmark
350  
351 for (; not null $kid->sibling; $kid = $kid->sibling) {
352 push @exprs, $kid;
353 }
354 my ($simple, $proto, $subname_info) = (0, undef, undef);
355 if (is_scope($kid)) {
356 $amper = "&";
357 $subname_info = $self->deparse($kid, 0, $op);
358 $subname_info->{texts} = ['{', $subname_info->texts, '}'];
359 $subname_info->{text} = join('', @$subname_info->{texts});
360 } elsif ($kid->first->name eq "gv") {
361 my $gv = $self->gv_or_padgv($kid->first);
362 my $cv;
363 if (class($gv) eq 'GV' && class($cv = $gv->CV) ne "SPECIAL"
364 || $gv->FLAGS & SVf_ROK && class($cv = $gv->RV) eq 'CV') {
365 $proto = $cv->PV if $cv->FLAGS & SVf_POK;
366 }
367 $simple = 1; # only calls of named functions can be prototyped
368 $subname_info = $self->deparse($kid, 24, $op);
369 my $fq;
370 # Fully qualify any sub name that conflicts with a lexical.
371 if ($self->lex_in_scope("&$kid")
372 || $self->lex_in_scope("&$kid", 1))
373 {
374 $fq++;
375 } elsif (!$amper) {
376 if ($subname_info->{text} eq 'main::') {
377 $subname_info->{text} = '::';
378 } else {
379 if ($kid !~ /::/ && $kid ne 'x') {
380 # Fully qualify any sub name that is also a keyword. While
381 # we could check the import flag, we cannot guarantee that
382 # the code deparsed so far would set that flag, so we qual-
383 # ify the names regardless of importation.
384 if (exists $feature_keywords{$kid}) {
385 $fq++ if $self->feature_enabled($kid);
386 } elsif (do { local $@; local $SIG{__DIE__};
387 eval { () = prototype "CORE::$kid"; 1 } }) {
388 $fq++
389 }
390 }
391 }
392 if ($subname_info->{text} !~ /^(?:\w|::)(?:[\w\d]|::(?!\z))*\z/) {
393 $subname_info->{text} = $self->single_delim($$kid, "q", "'", $kid) . '->';
394 }
395 }
396 } elsif (is_scalar ($kid->first) && $kid->first->name ne 'rv2cv') {
397 $amper = "&";
398 $subname_info = $self->deparse($kid, 24, $op);
399 } else {
400 $prefix = "";
401 my $arrow = is_subscriptable($kid->first) || $kid->first->name eq "padcv" ? "" : "->";
402 $subname_info = $self->deparse($kid, 24, $op);
403 $subname_info->{text} .= $arrow;
404 }
405  
406 # Doesn't matter how many prototypes there are, if
407 # they haven't happened yet!
408 my $declared;
409 my $sub_name = $subname_info->{text};
410 {
411 no strict 'refs';
412 no warnings 'uninitialized';
413 $declared = exists $self->{'subs_declared'}{$sub_name}
414 || (
415 defined &{ ${$self->{'curstash'}."::"}{$sub_name} }
416 && !exists
417 $self->{'subs_deparsed'}{$self->{'curstash'}."::" . $sub_name}
418 && defined prototype $self->{'curstash'}."::" . $sub_name
419 );
420 if (!$declared && defined($proto)) {
421 # Avoid "too early to check prototype" warning
422 ($amper, $proto) = ('&');
423 }
424 }
425  
426 my (@texts, @body, $type);
427 @body = ();
428 if ($declared and defined $proto and not $amper) {
429 my $args;
430 ($amper, $args) = $self->check_proto($op, $proto, @exprs);
431 if ($amper eq "&") {
432 @body = map($self->deparse($_, 6, $op), @exprs);
433 } else {
434 @body = @$args if @$args;
435 }
436 } else {
437 @body = map($self->deparse($_, 6, $op), @exprs);
438 }
439  
440 if ($prefix or $amper) {
441 if ($sub_name eq '&') {
442 # &{&} cannot be written as &&
443 $subname_info->{texts} = ["{", @{$subname_info->{texts}}, "}"];
444 $subname_info->{text} = join('', $subname_info->{texts});
445 }
446 if ($op->flags & OPf_STACKED) {
447 $type = 'prefix- or &-stacked call()';
448 @texts = ($prefix, $amper, $subname_info, "(", $self->combine2str(', ', \@body), ")");
449 } else {
450 $type = 'prefix or &- call';
451 @texts = ($prefix, $amper, $subname_info);
452 }
453 } else {
454 # It's a syntax error to call CORE::GLOBAL::foo with a prefix,
455 # so it must have been translated from a keyword call. Translate
456 # it back.
457 $subname_info->{text} =~ s/^CORE::GLOBAL:://;
458 my $dproto = defined($proto) ? $proto : "undefined";
459 if (!$declared) {
460 $type = 'call undefined';
461 @texts = dedup_parens_func($self, $subname_info, \@body);
462 } elsif ($dproto =~ /^\s*\z/) {
463 $type = 'call no protype';
464 @texts = ($subname_info);
465 } elsif ($dproto eq "\$" and is_scalar($exprs[0])) {
466 $type = 'call - $ prototype';
467 # is_scalar is an excessively conservative test here:
468 # really, we should be comparing to the precedence of the
469 # top operator of $exprs[0] (ala unop()), but that would
470 # take some major code restructuring to do right.
471 @texts = $self->maybe_parens_func($sub_name, $self->combine2str(', ', \@body), $cx, 16);
472 } elsif ($dproto ne '$' and defined($proto) || $simple) { #'
473 $type = 'call with prototype';
474 @texts = $self->maybe_parens_func($sub_name, $self->combine2str(', ', \@body), $cx, 5);
475 } else {
476 $type = 'call';
477 @texts = dedup_parens_func($self, $subname_info, \@body);
478 }
479 }
480 my $info = B::DeparseTree::Node->new($op, $self->{deparse}, \@texts,
481 '', $type,
482 {other_ops => $other_ops});
483 return $info;
484 }
485  
486 sub pp_once
487 {
488 my ($self, $op, $cx) = @_;
489 my $cond = $op->first;
490 my $true = $cond->sibling;
491  
492 return $self->deparse($true, $cx);
493 }
494  
495 sub pp_print { indirop(@_, "print") }
496 sub pp_prtf { indirop(@_, "printf") }
497  
498 # 'x' is weird when the left arg is a list
499 sub pp_repeat {
500 my $self = shift;
501 my($op, $cx) = @_;
502 my $left = $op->first;
503 my $right = $op->last;
504 my $eq = "";
505 my $prec = 19;
506 my $other_ops = undef;
507 if ($op->flags & OPf_STACKED) {
508 $eq = "=";
509 $prec = 7;
510 }
511 my @exprs = ();
512 my ($left_info, @body);
513 if (null($right)) {
514 # list repeat; count is inside left-side ex-list
515 $other_ops = [$left->first];
516 my $kid = $left->first->sibling; # skip pushmark
517 for (my $i=0; !null($kid->sibling); $kid = $kid->sibling) {
518 my $expr = $self->deparse($kid, 6, $op);
519 push @exprs, $expr;
520 }
521 $right = $kid;
522 @body = @exprs;
523 $left_info = info_from_list($op, $self,
524 ["(", @exprs, ")"], '', 'repeat_left', {});
525 } else {
526 $left_info = $self->deparse_binop_left($op, $left, $prec);
527 }
528 my $right_info = $self->deparse_binop_right($op, $right, $prec);
529 my $texts = [$left_info, "x$eq", $right_info];
530 my $info = info_from_list($op, $self, $texts, ' ', 'repeat',
531 {maybe_parens => [$self, $cx, $prec]});
532 $info->{other_ops} = $other_ops if $other_ops;
533 return $info
534 }
535  
536 sub pp_say { indirop(@_, "say") }
537  
538 sub pp_setstate { pp_nextstate(@_) }
539  
540 sub pp_sort { indirop(@_, "sort") }
541  
542  
543 sub pp_or { logop(@_, "or", 2, "||", 10, "unless") }
544 sub pp_dor { logop(@_, "//", 10) }
545  
546 # xor is syntactically a logop, but it's really a binop (contrary to
547 # old versions of opcode.pl). Syntax is what matters here.
548 sub pp_xor { logop(@_, "xor", 2, "", 0, "") }
549  
550 sub pp_mapwhile { mapop(@_, "map") }
551 sub pp_grepwhile { mapop(@_, "grep") }
552  
553 sub pp_complement { maybe_targmy(@_, \&pfixop, "~", 21) }
554 sub pp_getppid { maybe_targmy(@_, \&baseop, "getppid") }
555 sub pp_postdec { maybe_targmy(@_, \&pfixop, "--", 23, POSTFIX) }
556 sub pp_postinc { maybe_targmy(@_, \&pfixop, "++", 23, POSTFIX) }
557 sub pp_time { maybe_targmy(@_, \&baseop, "time") }
558 sub pp_wait { maybe_targmy(@_, \&baseop, "wait") }
559  
560 sub pp_preinc { pfixop(@_, "++", 23) }
561 sub pp_predec { pfixop(@_, "--", 23) }
562 sub pp_i_preinc { pfixop(@_, "++", 23) }
563 sub pp_i_predec { pfixop(@_, "--", 23) }
564  
565 # FIXME:
566 # Different between 5.20 and 5.20. We've used 5.22 tough
567 # Go over and make sure this is okay.
568 sub pp_stub {
569 my ($self, $op) = @_;
570 info_from_list($op, $self, ["(", ")"], '', 'stub', {})
571 };
572  
573 sub pp_exists
574 {
575 my($self, $op, $cx) = @_;
576 my ($info, $type);
577 my $name = $self->keyword("exists");
578 if ($op->private & OPpEXISTS_SUB) {
579 # Checking for the existence of a subroutine
580 $info = $self->pp_rv2cv($op->first, 16);
581 $type = 'exists_sub';
582 } elsif ($op->flags & OPf_SPECIAL) {
583 # Array element, not hash helement
584 $info = $self->pp_aelem($op->first, 16);
585 $type = 'info_array';
586 } else {
587 $info = $self->pp_helem($op->first, 16);
588 $type = 'info_hash';
589 }
590 my @texts = $self->maybe_parens_func($name, $info->{text}, $cx, 16);
591 return info_from_list($op, $self, \@texts, '', $type, {body=>[$info]});
592 }