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