line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package B::Utils1; |
2
|
|
|
|
|
|
|
|
3
|
8
|
|
|
8
|
|
197290
|
use 5.006; |
|
8
|
|
|
|
|
42
|
|
4
|
8
|
|
|
8
|
|
51
|
use strict; |
|
8
|
|
|
|
|
16
|
|
|
8
|
|
|
|
|
198
|
|
5
|
8
|
|
|
8
|
|
59
|
use warnings; |
|
8
|
|
|
|
|
20
|
|
|
8
|
|
|
|
|
597
|
|
6
|
8
|
|
|
|
|
1128
|
use vars qw( @EXPORT_OK %EXPORT_TAGS |
7
|
8
|
|
|
8
|
|
41
|
@bad_stashes $TRACE_FH $file $line $sub $trace_removed ); |
|
8
|
|
|
|
|
13
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use subs ( |
10
|
8
|
|
|
|
|
48
|
qw( all_starts all_roots anon_sub recalc_sub_cache ), |
11
|
|
|
|
|
|
|
qw( walkoptree_simple walkoptree_filtered ), |
12
|
|
|
|
|
|
|
qw( walkallops_simple walkallops_filtered ), |
13
|
|
|
|
|
|
|
qw( opgrep op_or ), |
14
|
8
|
|
|
8
|
|
7048
|
); |
|
8
|
|
|
|
|
158
|
|
15
|
|
|
|
|
|
|
sub croak (@); |
16
|
|
|
|
|
|
|
sub carp (@); |
17
|
|
|
|
|
|
|
|
18
|
8
|
|
|
8
|
|
982
|
use Scalar::Util qw( weaken blessed ); |
|
8
|
|
|
|
|
16
|
|
|
8
|
|
|
|
|
1097
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 NAME |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
B::Utils1 - Helper functions for op tree manipulation |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=cut |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head1 VERSION |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
1.05 |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=cut |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
our $VERSION = '1.05'; |
33
|
|
|
|
|
|
|
|
34
|
8
|
|
|
8
|
|
48
|
use base 'DynaLoader'; |
|
8
|
|
|
|
|
14
|
|
|
8
|
|
|
|
|
1238
|
|
35
|
|
|
|
|
|
|
bootstrap B::Utils1 $VERSION; |
36
|
8
|
|
|
8
|
1
|
2288
|
sub dl_load_flags {0x01} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=head1 SYNOPSIS |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
use B::Utils1; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=cut |
43
|
|
|
|
|
|
|
|
44
|
8
|
|
|
8
|
|
42
|
use B qw( OPf_KIDS main_start main_root walksymtable class main_cv ppname ); |
|
8
|
|
|
|
|
33
|
|
|
8
|
|
|
|
|
788
|
|
45
|
|
|
|
|
|
|
|
46
|
8
|
|
|
8
|
|
40
|
use Exporter (); |
|
8
|
|
|
|
|
16
|
|
|
8
|
|
|
|
|
615
|
|
47
|
|
|
|
|
|
|
@EXPORT_OK = qw(all_starts all_roots anon_subs |
48
|
|
|
|
|
|
|
walkoptree_simple walkoptree_filtered |
49
|
|
|
|
|
|
|
walkallops_simple walkallops_filtered |
50
|
|
|
|
|
|
|
recalc_sub_cache |
51
|
|
|
|
|
|
|
opgrep op_or ); |
52
|
|
|
|
|
|
|
%EXPORT_TAGS = ( all => \@EXPORT_OK ); |
53
|
|
|
|
|
|
|
*import = \&Exporter::import; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
@bad_stashes |
56
|
|
|
|
|
|
|
= qw(B Carp Exporter warnings Cwd Config CORE blib strict DynaLoader vars XSLoader AutoLoader base); |
57
|
|
|
|
|
|
|
|
58
|
8
|
|
|
8
|
|
131
|
use List::Util qw( shuffle ); |
|
8
|
|
|
|
|
15
|
|
|
8
|
|
|
|
|
893
|
|
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
BEGIN { |
61
|
|
|
|
|
|
|
# Fake up a TRACE constant and set $TRACE_FH |
62
|
8
|
|
|
8
|
|
167
|
BEGIN { $^W = 0 } |
63
|
8
|
|
|
8
|
|
45
|
no warnings; |
|
8
|
|
|
|
|
16
|
|
|
8
|
|
|
|
|
640
|
|
64
|
8
|
|
|
8
|
|
500
|
eval 'sub _TRACE () {' . ( 0 + $ENV{B_UTILS_TRACE} ) . '}'; |
65
|
8
|
50
|
|
|
|
44
|
die $@ if $@; |
66
|
8
|
|
50
|
|
|
3000
|
$TRACE_FH ||= \*STDOUT; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
sub _TRUE () { !!1 } |
69
|
|
|
|
|
|
|
sub _FALSE () { !!0 } |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=head1 OP METHODS |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=over 4 |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=cut |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# The following functions have been removed because it turns out that |
78
|
|
|
|
|
|
|
# this breaks stuff like B::Concise which depends on ops lacking |
79
|
|
|
|
|
|
|
# methods they wouldn't normally have. |
80
|
|
|
|
|
|
|
# |
81
|
|
|
|
|
|
|
# =pod |
82
|
|
|
|
|
|
|
# |
83
|
|
|
|
|
|
|
# =item C<$op-Efirst> |
84
|
|
|
|
|
|
|
# |
85
|
|
|
|
|
|
|
# =item C<$oo-Elast> |
86
|
|
|
|
|
|
|
# |
87
|
|
|
|
|
|
|
# =item C<$op-Eother> |
88
|
|
|
|
|
|
|
# |
89
|
|
|
|
|
|
|
# Normally if you call first, last or other on anything which is not an |
90
|
|
|
|
|
|
|
# UNOP, BINOP or LOGOP respectively it will die. This leads to lots of |
91
|
|
|
|
|
|
|
# code like: |
92
|
|
|
|
|
|
|
# |
93
|
|
|
|
|
|
|
# $op->first if $op->can('first'); |
94
|
|
|
|
|
|
|
# |
95
|
|
|
|
|
|
|
# B::Utils1 provided every op with first, last and other methods which |
96
|
|
|
|
|
|
|
# will simply returned nothing if it isn't relevent. But this broke B::Concise |
97
|
|
|
|
|
|
|
# |
98
|
|
|
|
|
|
|
# =cut |
99
|
|
|
|
|
|
|
# |
100
|
|
|
|
|
|
|
# sub B::OP::first { $_[0]->can("SUPER::first") ? $_[0]->SUPER::first() : () } |
101
|
|
|
|
|
|
|
# sub B::OP::last { $_[0]->can("SUPER::last") ? $_[0]->SUPER::last() : () } |
102
|
|
|
|
|
|
|
# sub B::OP::other { $_[0]->can("SUPER::other") ? $_[0]->SUPER::other() : () } |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=item C<$op-Eoldname> |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
Returns the name of the op, even if it is currently optimized to null. |
107
|
|
|
|
|
|
|
This helps you understand the structure of the op tree. |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=cut |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub B::OP::oldname { |
112
|
48538
|
|
|
48538
|
|
59237
|
my $op = shift; |
113
|
48538
|
|
|
|
|
145700
|
my $name = $op->name; |
114
|
48538
|
|
|
|
|
113522
|
my $targ = $op->targ; |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# This is a an operation which *used* to be a real op but was |
117
|
|
|
|
|
|
|
# optimized away. Fetch the old value and ignore the leading pp_. |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
# I forget why the original pp # is located in the targ field. |
120
|
48538
|
100
|
100
|
|
|
280289
|
return $name eq 'null' && $targ |
121
|
|
|
|
|
|
|
? substr( ppname($targ), 3 ) |
122
|
|
|
|
|
|
|
: $name; |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=item C<$op-Ekids> |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
Returns an array of all this op's non-null children, in order. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=cut |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
sub B::OP::kids { |
133
|
35010
|
|
|
35010
|
|
42003
|
my $op = shift; |
134
|
35010
|
50
|
|
|
|
61189
|
return unless defined wantarray; |
135
|
|
|
|
|
|
|
|
136
|
35010
|
|
|
|
|
36647
|
my @kids; |
137
|
35010
|
100
|
33
|
|
|
246255
|
if ( ref $op and $$op and $op->flags & OPf_KIDS ) { |
|
|
|
66
|
|
|
|
|
138
|
30586
|
|
|
|
|
133157
|
for (my $kid = $op->first; $$kid; $kid = $kid->sibling) { |
139
|
61891
|
|
|
|
|
264050
|
push @kids, $kid; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
### Assert: $op->children == @kids |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
else { |
144
|
4424
|
100
|
|
|
|
27243
|
@kids = ( |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
145
|
|
|
|
|
|
|
( $op->can('first') ? $op->first : () ), |
146
|
|
|
|
|
|
|
( $op->can('last') ? $op->last : () ), |
147
|
|
|
|
|
|
|
( $op->can('other') ? $op->other : () ) |
148
|
|
|
|
|
|
|
); |
149
|
|
|
|
|
|
|
} |
150
|
35010
|
|
|
|
|
102003
|
return @kids; |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=item C<$op-Eparent> |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
Returns the parent node in the op tree, if possible. Currently |
156
|
|
|
|
|
|
|
"possible" means "if the tree has already been optimized"; that is, if |
157
|
|
|
|
|
|
|
we're during a C block. (and hence, if we have valid C |
158
|
|
|
|
|
|
|
pointers.) |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
In the future, it may be possible to search for the parent before we |
161
|
|
|
|
|
|
|
have the C pointers in place, but it'll take me a while to |
162
|
|
|
|
|
|
|
figure out how to do that. |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
Warning: Since 5.21.2 B comes with it's own version of B::OP::parent |
165
|
|
|
|
|
|
|
which returns either B::NULL or the real parent when ccflags contains |
166
|
|
|
|
|
|
|
-DPERL_OP_PARENT. |
167
|
|
|
|
|
|
|
We patch away this broken B::OP::parent and return again undef if no parent |
168
|
|
|
|
|
|
|
exists. Note that L returns B::NULL instead. |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=cut |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
BEGIN { |
173
|
8
|
50
|
33
|
8
|
|
81
|
if ($] >= 5.021002 and exists &B::OP::parent) { |
174
|
8
|
100
|
66
|
8
|
|
478
|
eval q[ |
|
8
|
|
|
92
|
|
47
|
|
|
8
|
|
|
|
|
12
|
|
|
8
|
|
|
|
|
589
|
|
|
92
|
|
|
|
|
4036
|
|
|
92
|
|
|
|
|
297
|
|
|
92
|
|
|
|
|
705
|
|
175
|
|
|
|
|
|
|
no warnings 'redefine'; |
176
|
|
|
|
|
|
|
sub B::OP::parent { |
177
|
|
|
|
|
|
|
my $op = shift; |
178
|
|
|
|
|
|
|
my $parent = $op->_parent_impl( $op, "" ); |
179
|
|
|
|
|
|
|
return $parent && ref $parent ne 'B::NULL' ? $parent : undef; |
180
|
|
|
|
|
|
|
}]; |
181
|
|
|
|
|
|
|
} else { |
182
|
0
|
|
|
|
|
0
|
eval q[ |
183
|
|
|
|
|
|
|
sub B::OP::parent { |
184
|
|
|
|
|
|
|
my $op = shift; |
185
|
|
|
|
|
|
|
return $op->_parent_impl( $op, "" ); |
186
|
|
|
|
|
|
|
}]; |
187
|
|
|
|
|
|
|
} |
188
|
8
|
50
|
|
|
|
42
|
if ($] >= 5.021002) { |
189
|
8
|
|
|
0
|
|
29152
|
eval q[ |
190
|
|
|
|
|
|
|
sub B::NULL::kids { } |
191
|
|
|
|
|
|
|
]; |
192
|
|
|
|
|
|
|
} |
193
|
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
4582
|
|
|
sub B::NULL::_parent_impl { } |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
sub B::OP::_parent_impl { |
198
|
8847
|
|
|
8847
|
|
14838
|
my ( $op, $target, $cx ) = @_; |
199
|
|
|
|
|
|
|
|
200
|
8847
|
100
|
|
|
|
97056
|
return if $cx =~ /\b$$op\b/; |
201
|
|
|
|
|
|
|
|
202
|
8627
|
|
|
|
|
17944
|
for ( $op->kids ) { |
203
|
8055
|
100
|
|
|
|
19753
|
if ( $$_ == $$target ) { |
204
|
91
|
|
|
|
|
3785
|
return $op; |
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
return ( |
209
|
8536
|
|
66
|
|
|
43803
|
$op->sibling->_parent_impl( $target, "$cx$$op S " ) |
210
|
|
|
|
|
|
|
|| ( |
211
|
|
|
|
|
|
|
$cx =~ /^(?:-?\d+ S )*(?:-?\d+ N )*$/ |
212
|
|
|
|
|
|
|
? $op->next->_parent_impl( $target, "$cx$$op N " ) |
213
|
|
|
|
|
|
|
: () |
214
|
|
|
|
|
|
|
) |
215
|
|
|
|
|
|
|
|| ( |
216
|
|
|
|
|
|
|
$op->can('first') |
217
|
|
|
|
|
|
|
? $op->first->_parent_impl( $target, "$cx$$op F " ) |
218
|
|
|
|
|
|
|
: () |
219
|
|
|
|
|
|
|
) |
220
|
|
|
|
|
|
|
); |
221
|
|
|
|
|
|
|
} |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=item C<$op-Eancestors> |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
Returns all parents of this node, recursively. The list is ordered |
226
|
|
|
|
|
|
|
from younger/closer parents to older/farther parents. |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
=cut |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
sub B::OP::ancestors { |
231
|
0
|
|
|
0
|
|
0
|
my @nodes = shift; |
232
|
|
|
|
|
|
|
|
233
|
0
|
|
|
|
|
0
|
my $parent; |
234
|
0
|
|
|
|
|
0
|
push @nodes, $parent while $parent = $nodes[-1]->parent; |
235
|
0
|
|
|
|
|
0
|
shift @nodes; |
236
|
|
|
|
|
|
|
|
237
|
0
|
|
|
|
|
0
|
return @nodes; |
238
|
|
|
|
|
|
|
} |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
=item C<$op-Edescendants> |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
Returns all children of this node, recursively. The list is unordered. |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
=cut |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
sub B::OP::descendants { |
247
|
0
|
|
|
0
|
|
0
|
my $node = shift; |
248
|
0
|
|
|
|
|
0
|
my @nodes; |
249
|
|
|
|
|
|
|
walkoptree_simple( $node, |
250
|
0
|
0
|
|
0
|
|
0
|
sub { push @nodes, $_ if ${ $_[0] } != $$node } ); |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
251
|
0
|
|
|
|
|
0
|
return shuffle @nodes; |
252
|
|
|
|
|
|
|
} |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
=item C<$op-Esiblings> |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
Returns all younger siblings of this node. The list is ordered from |
257
|
|
|
|
|
|
|
younger/closer siblings to older/farther siblings. |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
=cut |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
sub B::OP::siblings { |
262
|
0
|
|
|
0
|
|
0
|
my @siblings = $_[0]; |
263
|
|
|
|
|
|
|
|
264
|
0
|
|
|
|
|
0
|
my $sibling; |
265
|
0
|
|
|
|
|
0
|
push @siblings, $siblings[-1]->sibling while $siblings[-1]->can('sibling'); |
266
|
0
|
|
|
|
|
0
|
shift @siblings; |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
# Remove any undefined or B::NULL objects |
269
|
|
|
|
|
|
|
pop @siblings while |
270
|
|
|
|
|
|
|
@siblings |
271
|
|
|
|
|
|
|
&& !( defined $siblings[-1] |
272
|
0
|
|
0
|
|
|
0
|
&& ${$siblings[-1]} ); |
|
|
|
0
|
|
|
|
|
273
|
|
|
|
|
|
|
|
274
|
0
|
|
|
|
|
0
|
return @siblings; |
275
|
|
|
|
|
|
|
} |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
=item C<$op-Eprevious> |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
Like C< $op-Enext >, but not quite. |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
=cut |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
## sub B::OP::previous { |
284
|
|
|
|
|
|
|
## return unless defined wantarray; |
285
|
|
|
|
|
|
|
## |
286
|
|
|
|
|
|
|
## my $target = shift; |
287
|
|
|
|
|
|
|
## |
288
|
|
|
|
|
|
|
## my $start = $target; |
289
|
|
|
|
|
|
|
## my (%deadend, $search); |
290
|
|
|
|
|
|
|
## $search = sub { |
291
|
|
|
|
|
|
|
## my $node = $_[0]; |
292
|
|
|
|
|
|
|
## |
293
|
|
|
|
|
|
|
## unless ( defined $node ) { |
294
|
|
|
|
|
|
|
## # If I've been asked to search nothing, just return. The |
295
|
|
|
|
|
|
|
## # ->parent call might do this to me. |
296
|
|
|
|
|
|
|
## return _FALSE; |
297
|
|
|
|
|
|
|
## } |
298
|
|
|
|
|
|
|
## elsif ( exists $deadend{$node} ) { |
299
|
|
|
|
|
|
|
## # If this node has been seen already, try again as its |
300
|
|
|
|
|
|
|
## # parent. |
301
|
|
|
|
|
|
|
## return $search->( $node->parent ); |
302
|
|
|
|
|
|
|
## } |
303
|
|
|
|
|
|
|
## elsif ( eval { ${$node->next} == $$target } ) { |
304
|
|
|
|
|
|
|
## return $node; |
305
|
|
|
|
|
|
|
## } |
306
|
|
|
|
|
|
|
## |
307
|
|
|
|
|
|
|
## # When searching the children, do it in reverse order because |
308
|
|
|
|
|
|
|
## # pointers back up are more likely to be farther down the |
309
|
|
|
|
|
|
|
## # stack. This works without reversing but I can avoid some |
310
|
|
|
|
|
|
|
## # work by ordering the work this way. |
311
|
|
|
|
|
|
|
## my @kids = reverse $node->kids; |
312
|
|
|
|
|
|
|
## |
313
|
|
|
|
|
|
|
## # Search this node's direct children for the ->next pointer |
314
|
|
|
|
|
|
|
## # that points to this node. |
315
|
|
|
|
|
|
|
## eval { ${$_->can('next')} == $$target } and return $_->next |
316
|
|
|
|
|
|
|
## for @kids; |
317
|
|
|
|
|
|
|
## |
318
|
|
|
|
|
|
|
## # For each child, check it for a match. |
319
|
|
|
|
|
|
|
## my $found; |
320
|
|
|
|
|
|
|
## $found = $search->($_) and return $found |
321
|
|
|
|
|
|
|
## for @kids; |
322
|
|
|
|
|
|
|
## |
323
|
|
|
|
|
|
|
## # Not in this subtree. |
324
|
|
|
|
|
|
|
## $deadend{$node} = _TRUE; |
325
|
|
|
|
|
|
|
## return _FALSE; |
326
|
|
|
|
|
|
|
## }; |
327
|
|
|
|
|
|
|
## |
328
|
|
|
|
|
|
|
## my $next = $target; |
329
|
|
|
|
|
|
|
## while ( eval { $next = $next->next } ) { |
330
|
|
|
|
|
|
|
## my $result; |
331
|
|
|
|
|
|
|
## $result = $search->( $next ) |
332
|
|
|
|
|
|
|
## and return $result; |
333
|
|
|
|
|
|
|
## } |
334
|
|
|
|
|
|
|
## |
335
|
|
|
|
|
|
|
## return _FALSE; |
336
|
|
|
|
|
|
|
## } |
337
|
|
|
|
|
|
|
|
338
|
|
|
|
|
|
|
=item C<$op-Estringify> |
339
|
|
|
|
|
|
|
|
340
|
|
|
|
|
|
|
Returns a nice stringification of an opcode. |
341
|
|
|
|
|
|
|
|
342
|
|
|
|
|
|
|
=cut |
343
|
|
|
|
|
|
|
|
344
|
|
|
|
|
|
|
sub B::OP::stringify { |
345
|
183
|
|
|
183
|
|
1365
|
my $op = shift; |
346
|
|
|
|
|
|
|
|
347
|
183
|
|
|
|
|
3061
|
return sprintf "%s-%s=(0x%07x)", $op->name, class($op), $$op; |
348
|
|
|
|
|
|
|
} |
349
|
|
|
|
|
|
|
|
350
|
|
|
|
|
|
|
=item C<$op-Eas_opgrep_pattern(%options)> |
351
|
|
|
|
|
|
|
|
352
|
|
|
|
|
|
|
From the op tree it is called on, C |
353
|
|
|
|
|
|
|
generates a data structure suitable for use as a condition pattern |
354
|
|
|
|
|
|
|
for the C function described below in detail. |
355
|
|
|
|
|
|
|
I: When using such generated patterns, there may be |
356
|
|
|
|
|
|
|
false positives: The pattern will most likely not match I |
357
|
|
|
|
|
|
|
the op tree it was generated from since by default, not all properties |
358
|
|
|
|
|
|
|
of the op are reproduced. |
359
|
|
|
|
|
|
|
|
360
|
|
|
|
|
|
|
You can control which properties of the op to include in the pattern |
361
|
|
|
|
|
|
|
by passing named arguments. The default behaviour is as if you |
362
|
|
|
|
|
|
|
passed in the following options: |
363
|
|
|
|
|
|
|
|
364
|
|
|
|
|
|
|
my $pattern = $op->as_opgrep_pattern( |
365
|
|
|
|
|
|
|
attributes => [qw(name flags)], |
366
|
|
|
|
|
|
|
max_recursion_depth => undef, |
367
|
|
|
|
|
|
|
); |
368
|
|
|
|
|
|
|
|
369
|
|
|
|
|
|
|
So obviously, you can set C to a number to |
370
|
|
|
|
|
|
|
limit the maximum depth of recursion into the op tree. Setting |
371
|
|
|
|
|
|
|
it to C<0> will limit the dump to the current op. |
372
|
|
|
|
|
|
|
|
373
|
|
|
|
|
|
|
C is a list of attributes to include in the produced |
374
|
|
|
|
|
|
|
pattern. The attributes that can be checked against in this way |
375
|
|
|
|
|
|
|
are: |
376
|
|
|
|
|
|
|
|
377
|
|
|
|
|
|
|
name targ type seq flags private pmflags pmpermflags. |
378
|
|
|
|
|
|
|
|
379
|
|
|
|
|
|
|
=cut |
380
|
|
|
|
|
|
|
|
381
|
|
|
|
|
|
|
sub B::OP::as_opgrep_pattern { |
382
|
0
|
|
|
0
|
|
0
|
my $op = shift; |
383
|
0
|
0
|
0
|
|
|
0
|
my $opt = (@_ == 1 and ref($_[0]) eq 'HASH') ? shift() : {@_}; |
384
|
|
|
|
|
|
|
|
385
|
0
|
|
|
|
|
0
|
my $attribs = $opt->{attributes}; |
386
|
0
|
|
0
|
|
|
0
|
$attribs ||= [qw(name flags)]; |
387
|
|
|
|
|
|
|
|
388
|
0
|
|
|
|
|
0
|
my $pattern = {}; |
389
|
0
|
|
|
|
|
0
|
foreach my $attr (@$attribs) { |
390
|
0
|
0
|
|
|
|
0
|
$pattern->{$attr} = $op->$attr() if $op->can($attr); |
391
|
|
|
|
|
|
|
} |
392
|
|
|
|
|
|
|
|
393
|
0
|
|
|
|
|
0
|
my $recursion_limit = $opt->{max_recursion_depth}; |
394
|
0
|
0
|
0
|
|
|
0
|
if ( (not defined $recursion_limit or $recursion_limit > 0) |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
395
|
|
|
|
|
|
|
and ref($op) |
396
|
|
|
|
|
|
|
and $$op |
397
|
|
|
|
|
|
|
and $op->flags & OPf_KIDS |
398
|
|
|
|
|
|
|
) { |
399
|
0
|
0
|
|
|
|
0
|
$opt->{max_recursion_depth}-- if defined $recursion_limit; |
400
|
|
|
|
|
|
|
|
401
|
|
|
|
|
|
|
$pattern->{kids} = [ |
402
|
0
|
|
|
|
|
0
|
map { $_->as_opgrep_pattern($opt) } $op->kids() |
|
0
|
|
|
|
|
0
|
|
403
|
|
|
|
|
|
|
]; |
404
|
|
|
|
|
|
|
} |
405
|
|
|
|
|
|
|
|
406
|
|
|
|
|
|
|
# reset the option structure in case we got a hash ref passed in. |
407
|
|
|
|
|
|
|
$opt->{max_recursion_depth} = $recursion_limit |
408
|
0
|
0
|
|
|
|
0
|
if exists $opt->{max_recursion_depth}; |
409
|
|
|
|
|
|
|
|
410
|
0
|
|
|
|
|
0
|
return $pattern; |
411
|
|
|
|
|
|
|
} |
412
|
|
|
|
|
|
|
|
413
|
|
|
|
|
|
|
=back |
414
|
|
|
|
|
|
|
|
415
|
|
|
|
|
|
|
=head1 EXPORTABLE FUNCTIONS |
416
|
|
|
|
|
|
|
|
417
|
|
|
|
|
|
|
=over 4 |
418
|
|
|
|
|
|
|
|
419
|
|
|
|
|
|
|
=item C |
420
|
|
|
|
|
|
|
|
421
|
|
|
|
|
|
|
=item C |
422
|
|
|
|
|
|
|
|
423
|
|
|
|
|
|
|
Returns a hash of all of the starting ops or root ops of optrees, keyed |
424
|
|
|
|
|
|
|
to subroutine name; the optree for main program is simply keyed to C<__MAIN__>. |
425
|
|
|
|
|
|
|
|
426
|
|
|
|
|
|
|
B: Certain "dangerous" stashes are not scanned for subroutines: |
427
|
|
|
|
|
|
|
the list of such stashes can be found in |
428
|
|
|
|
|
|
|
C<@B::Utils1::bad_stashes>. Feel free to examine and/or modify this to |
429
|
|
|
|
|
|
|
suit your needs. The intention is that a simple program which uses no |
430
|
|
|
|
|
|
|
modules other than C and C would show no addition |
431
|
|
|
|
|
|
|
symbols. |
432
|
|
|
|
|
|
|
|
433
|
|
|
|
|
|
|
This does B return the details of ops in anonymous subroutines |
434
|
|
|
|
|
|
|
compiled at compile time. For instance, given |
435
|
|
|
|
|
|
|
|
436
|
|
|
|
|
|
|
$a = sub { ... }; |
437
|
|
|
|
|
|
|
|
438
|
|
|
|
|
|
|
the subroutine will not appear in the hash. This is just as well, |
439
|
|
|
|
|
|
|
since they're anonymous... If you want to get at them, use... |
440
|
|
|
|
|
|
|
|
441
|
|
|
|
|
|
|
=cut |
442
|
|
|
|
|
|
|
|
443
|
|
|
|
|
|
|
my ( %starts, %roots ); |
444
|
2
|
100
|
|
2
|
|
1481
|
sub all_starts { _init_sub_cache(); wantarray ? %starts : \%starts } |
|
2
|
|
|
|
|
906
|
|
445
|
3
|
100
|
|
3
|
|
961
|
sub all_roots { _init_sub_cache(); wantarray ? %roots : \%roots } |
|
3
|
|
|
|
|
899
|
|
446
|
|
|
|
|
|
|
|
447
|
|
|
|
|
|
|
=item C |
448
|
|
|
|
|
|
|
|
449
|
|
|
|
|
|
|
This returns an array of hash references. Each element has the keys |
450
|
|
|
|
|
|
|
"start" and "root". These are the starting and root ops of all of the |
451
|
|
|
|
|
|
|
anonymous subroutines in the program. |
452
|
|
|
|
|
|
|
|
453
|
|
|
|
|
|
|
=cut |
454
|
|
|
|
|
|
|
|
455
|
|
|
|
|
|
|
my @anon_subs; |
456
|
2
|
100
|
|
2
|
1
|
1020
|
sub anon_subs { _init_sub_cache(); wantarray ? @anon_subs : \@anon_subs } |
|
2
|
|
|
|
|
19
|
|
457
|
|
|
|
|
|
|
|
458
|
|
|
|
|
|
|
=item C |
459
|
|
|
|
|
|
|
|
460
|
|
|
|
|
|
|
If PL_sub_generation has changed or you have some other reason to want |
461
|
|
|
|
|
|
|
to force the re-examination of the optrees, everywhere, call this |
462
|
|
|
|
|
|
|
function. |
463
|
|
|
|
|
|
|
|
464
|
|
|
|
|
|
|
=cut |
465
|
|
|
|
|
|
|
|
466
|
|
|
|
|
|
|
my $subs_cached = _FALSE; |
467
|
|
|
|
|
|
|
|
468
|
|
|
|
|
|
|
sub recalc_sub_cache { |
469
|
0
|
|
|
0
|
|
0
|
$subs_cached = _FALSE; |
470
|
|
|
|
|
|
|
|
471
|
0
|
|
|
|
|
0
|
%starts = %roots = @anon_subs = (); |
472
|
|
|
|
|
|
|
|
473
|
0
|
|
|
|
|
0
|
_init_sub_cache(); |
474
|
0
|
|
|
|
|
0
|
return; |
475
|
|
|
|
|
|
|
} |
476
|
|
|
|
|
|
|
|
477
|
|
|
|
|
|
|
sub _init_sub_cache { |
478
|
|
|
|
|
|
|
|
479
|
|
|
|
|
|
|
# Allow this function to be run only once. |
480
|
8
|
100
|
|
8
|
|
112
|
return if $subs_cached; |
481
|
|
|
|
|
|
|
|
482
|
5
|
|
|
|
|
57
|
%starts = ( __MAIN__ => main_start() ); |
483
|
5
|
|
|
|
|
38
|
%roots = ( __MAIN__ => main_root() ); |
484
|
|
|
|
|
|
|
|
485
|
|
|
|
|
|
|
# Through the magic of B::'s ugly callback system, %starts and |
486
|
|
|
|
|
|
|
# %roots will be populated. |
487
|
|
|
|
|
|
|
walksymtable( |
488
|
|
|
|
|
|
|
\%main::, |
489
|
|
|
|
|
|
|
_B_Utils_init_sub_cache => sub { |
490
|
|
|
|
|
|
|
|
491
|
|
|
|
|
|
|
# Do not eat our own children! |
492
|
690
|
|
100
|
690
|
|
11179
|
$_[0] eq "$_\::" && return _FALSE for @bad_stashes; |
493
|
|
|
|
|
|
|
|
494
|
625
|
|
|
|
|
11102
|
return _TRUE; |
495
|
|
|
|
|
|
|
}, |
496
|
5
|
|
|
|
|
1190
|
'' |
497
|
|
|
|
|
|
|
); |
498
|
|
|
|
|
|
|
|
499
|
|
|
|
|
|
|
# Some sort of file-scoped anonymous code refs are found here. In |
500
|
|
|
|
|
|
|
# general, when a function has anonymous functions, they can be |
501
|
|
|
|
|
|
|
# found in the scratchpad. |
502
|
5
|
100
|
|
|
|
387
|
push @anon_subs, |
503
|
|
|
|
|
|
|
map( ( |
504
|
|
|
|
|
|
|
'CV' eq class($_) |
505
|
|
|
|
|
|
|
? { root => $_->ROOT, |
506
|
|
|
|
|
|
|
start => $_->START |
507
|
|
|
|
|
|
|
} |
508
|
|
|
|
|
|
|
: () |
509
|
|
|
|
|
|
|
), |
510
|
|
|
|
|
|
|
main_cv()->PADLIST->ARRAY->ARRAY ); |
511
|
|
|
|
|
|
|
|
512
|
5
|
|
|
|
|
22
|
$subs_cached = _TRUE; |
513
|
5
|
|
|
|
|
10
|
return; |
514
|
|
|
|
|
|
|
} |
515
|
|
|
|
|
|
|
|
516
|
|
|
|
|
|
|
sub B::GV::_B_Utils_init_sub_cache { |
517
|
|
|
|
|
|
|
|
518
|
|
|
|
|
|
|
# This is a callback function called from B::Utils1::_init via |
519
|
|
|
|
|
|
|
# B::walksymtable. |
520
|
|
|
|
|
|
|
|
521
|
8911
|
|
|
8911
|
|
10708
|
my $gv = shift; |
522
|
8911
|
|
|
|
|
18804
|
my $cv = $gv->CV; |
523
|
|
|
|
|
|
|
|
524
|
|
|
|
|
|
|
# If the B::CV object is a pointer to nothing, ignore it. |
525
|
8911
|
100
|
|
|
|
31956
|
return unless $$cv; |
526
|
|
|
|
|
|
|
|
527
|
|
|
|
|
|
|
# Simon was originally using $gv->SAFENAME but I don't think |
528
|
|
|
|
|
|
|
# that's a "correct" decision because then oddly named functions |
529
|
|
|
|
|
|
|
# can't be disambiguated. If a function were actually named ^G, I |
530
|
|
|
|
|
|
|
# couldn't tell it apart from one named after the control |
531
|
|
|
|
|
|
|
# character ^G. |
532
|
6288
|
|
|
|
|
24919
|
my $name = $gv->STASH->NAME . "::" . $gv->NAME; |
533
|
|
|
|
|
|
|
|
534
|
|
|
|
|
|
|
# When does a CV not fulfill ->ARRAY->ARRAY? Some time during |
535
|
|
|
|
|
|
|
# initialization? |
536
|
6288
|
100
|
66
|
|
|
65943
|
if ( $cv->can('PADLIST') |
|
|
|
100
|
|
|
|
|
537
|
|
|
|
|
|
|
and $cv->PADLIST->can('ARRAY') |
538
|
|
|
|
|
|
|
and $cv->PADLIST->ARRAY->can('ARRAY') ) |
539
|
|
|
|
|
|
|
{ |
540
|
3510
|
100
|
|
|
|
160775
|
push @anon_subs, |
541
|
|
|
|
|
|
|
map( ( |
542
|
|
|
|
|
|
|
'CV' eq class($_) |
543
|
|
|
|
|
|
|
? { root => $_->ROOT, |
544
|
|
|
|
|
|
|
start => $_->START |
545
|
|
|
|
|
|
|
} |
546
|
|
|
|
|
|
|
: () |
547
|
|
|
|
|
|
|
), |
548
|
|
|
|
|
|
|
$cv->PADLIST->ARRAY->ARRAY ); |
549
|
|
|
|
|
|
|
} |
550
|
|
|
|
|
|
|
|
551
|
6288
|
50
|
33
|
|
|
47255
|
return unless ( ( my $start = $cv->START ) |
552
|
|
|
|
|
|
|
and ( my $root = $cv->ROOT ) ); |
553
|
|
|
|
|
|
|
|
554
|
6288
|
|
|
|
|
15576
|
$starts{$name} = $start; |
555
|
6288
|
|
|
|
|
10304
|
$roots{$name} = $root; |
556
|
|
|
|
|
|
|
|
557
|
|
|
|
|
|
|
# return _TRUE; |
558
|
6288
|
|
|
|
|
51567
|
return; |
559
|
|
|
|
|
|
|
} |
560
|
|
|
|
|
|
|
|
561
|
|
|
|
|
|
|
# sub B::SPECIAL::_B_Utils_init_sub_cache { |
562
|
|
|
|
|
|
|
# |
563
|
|
|
|
|
|
|
# # This is a callback function called from B::Utils1::_init via |
564
|
|
|
|
|
|
|
# # B::walksymtable. |
565
|
|
|
|
|
|
|
# |
566
|
|
|
|
|
|
|
# # JJ: I'm not sure why this callback function exists. |
567
|
|
|
|
|
|
|
# |
568
|
|
|
|
|
|
|
# return _TRUE; |
569
|
|
|
|
|
|
|
# } |
570
|
|
|
|
|
|
|
|
571
|
|
|
|
|
|
|
=item C |
572
|
|
|
|
|
|
|
|
573
|
|
|
|
|
|
|
The C module provides various functions to walk the op tree, but |
574
|
|
|
|
|
|
|
they're all rather difficult to use, requiring you to inject methods |
575
|
|
|
|
|
|
|
into the C class. This is a very simple op tree walker with |
576
|
|
|
|
|
|
|
more expected semantics. |
577
|
|
|
|
|
|
|
|
578
|
|
|
|
|
|
|
All the C functions set C<$B::Utils1::file>, C<$B::Utils::line>, |
579
|
|
|
|
|
|
|
and C<$B::Utils1::sub> to the appropriate values of file, line number, |
580
|
|
|
|
|
|
|
and sub name in the program being examined. |
581
|
|
|
|
|
|
|
Sets C<$B::Utils::trace_removed> when the nextstate COPs that contained |
582
|
|
|
|
|
|
|
that line was optimized away. Such lines won't normally be |
583
|
|
|
|
|
|
|
step-able or breakpoint-able in a debugger without special work. |
584
|
|
|
|
|
|
|
|
585
|
|
|
|
|
|
|
=cut |
586
|
|
|
|
|
|
|
|
587
|
|
|
|
|
|
|
$B::Utils1::file = '__none__'; |
588
|
|
|
|
|
|
|
$B::Utils1::line = 0; |
589
|
|
|
|
|
|
|
$B::Utils1::sub = undef; |
590
|
|
|
|
|
|
|
|
591
|
|
|
|
|
|
|
sub walkoptree_simple { |
592
|
1261
|
|
|
1261
|
|
4044
|
$B::Utils1::file = '__none__'; |
593
|
1261
|
|
|
|
|
1288
|
$B::Utils1::line = 0; |
594
|
|
|
|
|
|
|
|
595
|
1261
|
|
|
|
|
2408
|
_walkoptree_simple( {}, @_ ); |
596
|
|
|
|
|
|
|
|
597
|
1261
|
|
|
|
|
9979
|
return _TRUE; |
598
|
|
|
|
|
|
|
} |
599
|
|
|
|
|
|
|
|
600
|
|
|
|
|
|
|
sub _walkoptree_simple { |
601
|
54942
|
|
|
54942
|
|
79226
|
my ( $visited, $op, $callback, $data ) = @_; |
602
|
|
|
|
|
|
|
|
603
|
54942
|
50
|
|
|
|
184356
|
return if $visited->{$$op}++; |
604
|
|
|
|
|
|
|
|
605
|
54942
|
100
|
66
|
|
|
452066
|
if ( ref $op and $op->isa("B::COP") ) { |
|
|
50
|
66
|
|
|
|
|
606
|
5850
|
|
|
|
|
18904
|
$B::Utils1::file = $op->file; |
607
|
5850
|
|
|
|
|
14703
|
$B::Utils1::line = $op->line; |
608
|
5850
|
|
|
|
|
7936
|
$B::Utils1::trace_removed = _FALSE; |
609
|
|
|
|
|
|
|
} elsif ( !$op->isa('B::NULL') and $op->oldname =~ /^(next|db)state$/) { |
610
|
|
|
|
|
|
|
# COP nextstate has been optimized away. However by turning |
611
|
|
|
|
|
|
|
# this locally back into a COP we can retrieve the file and line |
612
|
|
|
|
|
|
|
# values. |
613
|
0
|
|
|
|
|
0
|
my $cop = $op; |
614
|
0
|
|
|
|
|
0
|
bless $cop, 'B::COP'; |
615
|
0
|
|
|
|
|
0
|
$B::Utils1::file = $cop->file; |
616
|
0
|
|
|
|
|
0
|
$B::Utils1::line = $cop->line; |
617
|
0
|
|
|
|
|
0
|
$B::Utils1::trace_removed = _TRUE; |
618
|
|
|
|
|
|
|
} |
619
|
|
|
|
|
|
|
|
620
|
54942
|
|
|
|
|
118344
|
$callback->( $op, $data ); |
621
|
54942
|
100
|
|
|
|
660535
|
return if $op->isa('B::NULL'); |
622
|
54388
|
100
|
|
|
|
185469
|
if ( $op->flags & OPf_KIDS ) { |
623
|
|
|
|
|
|
|
# for (my $kid = $op->first; $$kid; $kid = $kid->sibling) { |
624
|
|
|
|
|
|
|
# _walkoptree_simple( $visited, $kid, $callback, $data ); |
625
|
|
|
|
|
|
|
# } |
626
|
26292
|
|
|
|
|
47975
|
_walkoptree_simple( $visited, $_, $callback, $data ) for $op->kids; |
627
|
|
|
|
|
|
|
} |
628
|
54388
|
100
|
|
|
|
178947
|
if ( $op->isa('B::PMOP') ) { |
629
|
349
|
|
|
|
|
970
|
my $maybe_root = $op->pmreplroot; |
630
|
349
|
100
|
66
|
|
|
2286
|
if (ref($maybe_root) and $maybe_root->isa("B::OP")) { |
631
|
|
|
|
|
|
|
# It really is the root of the replacement, not something |
632
|
|
|
|
|
|
|
# else stored here for lack of space elsewhere |
633
|
15
|
|
|
|
|
36
|
_walkoptree_simple( $visited, $maybe_root, $callback, $data ); |
634
|
|
|
|
|
|
|
} |
635
|
|
|
|
|
|
|
} |
636
|
|
|
|
|
|
|
|
637
|
54388
|
|
|
|
|
117523
|
return; |
638
|
|
|
|
|
|
|
|
639
|
|
|
|
|
|
|
} |
640
|
|
|
|
|
|
|
|
641
|
|
|
|
|
|
|
=item C |
642
|
|
|
|
|
|
|
|
643
|
|
|
|
|
|
|
This is much the same as C, but will only call the |
644
|
|
|
|
|
|
|
callback if the C returns true. The C is passed the |
645
|
|
|
|
|
|
|
op in question as a parameter; the C function is fantastic |
646
|
|
|
|
|
|
|
for building your own filters. |
647
|
|
|
|
|
|
|
|
648
|
|
|
|
|
|
|
=cut |
649
|
|
|
|
|
|
|
|
650
|
|
|
|
|
|
|
sub walkoptree_filtered { |
651
|
1297
|
|
|
1297
|
|
1656
|
$B::Utils1::file = '__none__'; |
652
|
1297
|
|
|
|
|
1243
|
$B::Utils1::line = 0; |
653
|
|
|
|
|
|
|
|
654
|
1297
|
|
|
|
|
2621
|
_walkoptree_filtered( {}, @_ );; |
655
|
|
|
|
|
|
|
|
656
|
1297
|
|
|
|
|
4094
|
return _TRUE; |
657
|
|
|
|
|
|
|
} |
658
|
|
|
|
|
|
|
|
659
|
|
|
|
|
|
|
sub _walkoptree_filtered { |
660
|
55971
|
|
|
55971
|
|
83129
|
my ( $visited, $op, $filter, $callback, $data ) = @_; |
661
|
|
|
|
|
|
|
|
662
|
55971
|
100
|
|
|
|
196887
|
if ( $op->isa("B::COP") ) { |
663
|
5978
|
|
|
|
|
20258
|
$B::Utils1::file = $op->file; |
664
|
5978
|
|
|
|
|
15716
|
$B::Utils1::line = $op->line; |
665
|
|
|
|
|
|
|
} |
666
|
|
|
|
|
|
|
|
667
|
55971
|
50
|
|
|
|
124056
|
$callback->( $op, $data ) if $filter->($op); |
668
|
|
|
|
|
|
|
|
669
|
55971
|
100
|
66
|
|
|
487784
|
if ( ref $op |
|
|
|
100
|
|
|
|
|
670
|
|
|
|
|
|
|
and $$op |
671
|
|
|
|
|
|
|
and $op->flags & OPf_KIDS ) |
672
|
|
|
|
|
|
|
{ |
673
|
|
|
|
|
|
|
|
674
|
26799
|
|
|
|
|
80818
|
my $kid = $op->first; |
675
|
26799
|
|
66
|
|
|
119060
|
while ( ref $kid |
676
|
|
|
|
|
|
|
and $$kid ) |
677
|
|
|
|
|
|
|
{ |
678
|
54674
|
|
|
|
|
101681
|
_walkoptree_filtered( $visited, $kid, $filter, $callback, $data ); |
679
|
|
|
|
|
|
|
|
680
|
54674
|
|
|
|
|
360112
|
$kid = $kid->sibling; |
681
|
|
|
|
|
|
|
} |
682
|
|
|
|
|
|
|
} |
683
|
|
|
|
|
|
|
|
684
|
55971
|
|
|
|
|
84202
|
return _TRUE; |
685
|
|
|
|
|
|
|
} |
686
|
|
|
|
|
|
|
|
687
|
|
|
|
|
|
|
=item C |
688
|
|
|
|
|
|
|
|
689
|
|
|
|
|
|
|
This combines C with C and C |
690
|
|
|
|
|
|
|
to examine every op in the program. C<$B::Utils1::sub> is set to the |
691
|
|
|
|
|
|
|
subroutine name if you're in a subroutine, C<__MAIN__> if you're in |
692
|
|
|
|
|
|
|
the main program and C<__ANON__> if you're in an anonymous subroutine. |
693
|
|
|
|
|
|
|
|
694
|
|
|
|
|
|
|
=cut |
695
|
|
|
|
|
|
|
|
696
|
|
|
|
|
|
|
sub walkallops_simple { |
697
|
0
|
|
|
0
|
|
0
|
$B::Utils1::sub = undef; |
698
|
|
|
|
|
|
|
|
699
|
0
|
|
|
|
|
0
|
&_walkallops_simple; |
700
|
|
|
|
|
|
|
|
701
|
0
|
|
|
|
|
0
|
return _TRUE; |
702
|
|
|
|
|
|
|
} |
703
|
|
|
|
|
|
|
|
704
|
|
|
|
|
|
|
sub _walkallops_simple { |
705
|
0
|
|
|
0
|
|
0
|
my ( $callback, $data ) = @_; |
706
|
|
|
|
|
|
|
|
707
|
0
|
|
|
|
|
0
|
_init_sub_cache(); |
708
|
|
|
|
|
|
|
|
709
|
0
|
|
|
|
|
0
|
for my $sub_name (sort keys %roots) { |
710
|
0
|
|
|
|
|
0
|
$B::Utils1::sub = $sub_name; |
711
|
0
|
|
|
|
|
0
|
my $root = $roots{$sub_name}; |
712
|
0
|
|
|
|
|
0
|
walkoptree_simple( $root, $callback, $data ); |
713
|
|
|
|
|
|
|
} |
714
|
|
|
|
|
|
|
|
715
|
0
|
|
|
|
|
0
|
$B::Utils1::sub = "__ANON__"; |
716
|
0
|
|
|
|
|
0
|
walkoptree_simple( $_->{root}, $callback, $data ) for @anon_subs; |
717
|
|
|
|
|
|
|
|
718
|
0
|
|
|
|
|
0
|
return _TRUE; |
719
|
|
|
|
|
|
|
} |
720
|
|
|
|
|
|
|
|
721
|
|
|
|
|
|
|
=item C |
722
|
|
|
|
|
|
|
|
723
|
|
|
|
|
|
|
Same as above, but filtered. |
724
|
|
|
|
|
|
|
|
725
|
|
|
|
|
|
|
=cut |
726
|
|
|
|
|
|
|
|
727
|
|
|
|
|
|
|
sub walkallops_filtered { |
728
|
1
|
|
|
1
|
|
1064
|
$B::Utils1::sub = undef; |
729
|
|
|
|
|
|
|
|
730
|
1
|
|
|
|
|
31
|
&_walkallops_filtered; |
731
|
|
|
|
|
|
|
|
732
|
1
|
|
|
|
|
4
|
return _TRUE; |
733
|
|
|
|
|
|
|
} |
734
|
|
|
|
|
|
|
|
735
|
|
|
|
|
|
|
sub _walkallops_filtered { |
736
|
1
|
|
|
1
|
|
2
|
my ( $filter, $callback, $data ) = @_; |
737
|
|
|
|
|
|
|
|
738
|
1
|
|
|
|
|
4
|
_init_sub_cache(); |
739
|
|
|
|
|
|
|
|
740
|
1
|
|
|
|
|
72
|
walkoptree_filtered( $_, $filter, $callback, $data ) for values %roots; |
741
|
|
|
|
|
|
|
|
742
|
1
|
|
|
|
|
3
|
$B::Utils1::sub = "__ANON__"; |
743
|
|
|
|
|
|
|
|
744
|
|
|
|
|
|
|
walkoptree_filtered( $_->{root}, $filter, $callback, $data ) |
745
|
1
|
|
|
|
|
7
|
for @anon_subs; |
746
|
|
|
|
|
|
|
|
747
|
1
|
|
|
|
|
2
|
return _TRUE; |
748
|
|
|
|
|
|
|
} |
749
|
|
|
|
|
|
|
|
750
|
|
|
|
|
|
|
=item C |
751
|
|
|
|
|
|
|
|
752
|
|
|
|
|
|
|
Returns the ops which meet the given conditions. The conditions should |
753
|
|
|
|
|
|
|
be specified like this: |
754
|
|
|
|
|
|
|
|
755
|
|
|
|
|
|
|
@barewords = opgrep( |
756
|
|
|
|
|
|
|
{ name => "const", private => OPpCONST_BARE }, |
757
|
|
|
|
|
|
|
@ops |
758
|
|
|
|
|
|
|
); |
759
|
|
|
|
|
|
|
|
760
|
|
|
|
|
|
|
where the first argument to C is the condition to be matched against the |
761
|
|
|
|
|
|
|
op structure. We'll henceforth refer to it as an op-pattern. |
762
|
|
|
|
|
|
|
|
763
|
|
|
|
|
|
|
You can specify alternation by giving an arrayref of values: |
764
|
|
|
|
|
|
|
|
765
|
|
|
|
|
|
|
@svs = opgrep ( { name => ["padsv", "gvsv"] }, @ops) |
766
|
|
|
|
|
|
|
|
767
|
|
|
|
|
|
|
And you can specify inversion by making the first element of the |
768
|
|
|
|
|
|
|
arrayref a "!". (Hint: if you want to say "anything", say "not |
769
|
|
|
|
|
|
|
nothing": C<["!"]>) |
770
|
|
|
|
|
|
|
|
771
|
|
|
|
|
|
|
You may also specify the conditions to be matched in nearby ops as nested patterns. |
772
|
|
|
|
|
|
|
|
773
|
|
|
|
|
|
|
walkallops_filtered( |
774
|
|
|
|
|
|
|
sub { opgrep( {name => "exec", |
775
|
|
|
|
|
|
|
next => { |
776
|
|
|
|
|
|
|
name => "nextstate", |
777
|
|
|
|
|
|
|
sibling => { name => [qw(! exit warn die)] } |
778
|
|
|
|
|
|
|
} |
779
|
|
|
|
|
|
|
}, @_)}, |
780
|
|
|
|
|
|
|
sub { |
781
|
|
|
|
|
|
|
carp("Statement unlikely to be reached"); |
782
|
|
|
|
|
|
|
carp("\t(Maybe you meant system() when you said exec()?)\n"); |
783
|
|
|
|
|
|
|
} |
784
|
|
|
|
|
|
|
) |
785
|
|
|
|
|
|
|
|
786
|
|
|
|
|
|
|
Get that? |
787
|
|
|
|
|
|
|
|
788
|
|
|
|
|
|
|
Here are the things that can be tested in this way: |
789
|
|
|
|
|
|
|
|
790
|
|
|
|
|
|
|
name targ type seq flags private pmflags pmpermflags |
791
|
|
|
|
|
|
|
first other last sibling next pmreplroot pmreplstart pmnext |
792
|
|
|
|
|
|
|
|
793
|
|
|
|
|
|
|
Additionally, you can use the C keyword with an array reference |
794
|
|
|
|
|
|
|
to match the result of a call to C<$op-Ekids()>. An example use is |
795
|
|
|
|
|
|
|
given in the documentation for C below. |
796
|
|
|
|
|
|
|
|
797
|
|
|
|
|
|
|
For debugging, you can have many properties of an op that is currently being |
798
|
|
|
|
|
|
|
matched against a given condition dumped to STDERR |
799
|
|
|
|
|
|
|
by specifying C 1> in the condition's hash reference. |
800
|
|
|
|
|
|
|
|
801
|
|
|
|
|
|
|
If you match a complex condition against an op tree, you may want to extract |
802
|
|
|
|
|
|
|
a specific piece of information from the tree if the condition matches. |
803
|
|
|
|
|
|
|
This normally entails manually walking the tree a second time down to |
804
|
|
|
|
|
|
|
the op you wish to extract, investigate or modify. Since this is tedious |
805
|
|
|
|
|
|
|
duplication of code and information, you can specify a special property |
806
|
|
|
|
|
|
|
in the pattern of the op you wish to extract to capture the sub-op |
807
|
|
|
|
|
|
|
of interest. Example: |
808
|
|
|
|
|
|
|
|
809
|
|
|
|
|
|
|
my ($result) = opgrep( |
810
|
|
|
|
|
|
|
{ name => "exec", |
811
|
|
|
|
|
|
|
next => { name => "nextstate", |
812
|
|
|
|
|
|
|
sibling => { name => [qw(! exit warn die)] |
813
|
|
|
|
|
|
|
capture => "notreached", |
814
|
|
|
|
|
|
|
}, |
815
|
|
|
|
|
|
|
} |
816
|
|
|
|
|
|
|
}, |
817
|
|
|
|
|
|
|
$root_op |
818
|
|
|
|
|
|
|
); |
819
|
|
|
|
|
|
|
|
820
|
|
|
|
|
|
|
if ($result) { |
821
|
|
|
|
|
|
|
my $name = $result->{notreached}->name; # result is *not* the root op |
822
|
|
|
|
|
|
|
carp("Statement unlikely to be reached (op name: $name)"); |
823
|
|
|
|
|
|
|
carp("\t(Maybe you meant system() when you said exec()?)\n"); |
824
|
|
|
|
|
|
|
} |
825
|
|
|
|
|
|
|
|
826
|
|
|
|
|
|
|
While the above is a terribly contrived example, consider the win for a |
827
|
|
|
|
|
|
|
deeply nested pattern or worse yet, a pattern with many disjunctions. |
828
|
|
|
|
|
|
|
If a C property is found anywhere in |
829
|
|
|
|
|
|
|
the op pattern, C returns an unblessed hash reference on success |
830
|
|
|
|
|
|
|
instead of the tested op. You can tell them apart using L's |
831
|
|
|
|
|
|
|
C. That hash reference contains all captured ops plus the |
832
|
|
|
|
|
|
|
tested root up as the hash entry C<$result-E{op}>. Note that you cannot |
833
|
|
|
|
|
|
|
use this feature with C since that function was |
834
|
|
|
|
|
|
|
specifically documented to pass the tested op itself to the callback. |
835
|
|
|
|
|
|
|
|
836
|
|
|
|
|
|
|
You cannot capture disjunctions, but that doesn't really make sense anyway. |
837
|
|
|
|
|
|
|
|
838
|
|
|
|
|
|
|
=item C |
839
|
|
|
|
|
|
|
|
840
|
|
|
|
|
|
|
Same as above, except that you don't have to chain the conditions |
841
|
|
|
|
|
|
|
yourself. If you pass an array-ref, opgrep will chain the conditions |
842
|
|
|
|
|
|
|
for you using C. |
843
|
|
|
|
|
|
|
The conditions can either be strings (taken as op-names), or |
844
|
|
|
|
|
|
|
hash-refs, with the same testable conditions as given above. |
845
|
|
|
|
|
|
|
|
846
|
|
|
|
|
|
|
=cut |
847
|
|
|
|
|
|
|
|
848
|
|
|
|
|
|
|
sub opgrep { |
849
|
55971
|
50
|
|
55971
|
|
355854
|
return unless defined wantarray; |
850
|
|
|
|
|
|
|
|
851
|
55971
|
|
|
|
|
62455
|
my $conds_ref = shift; |
852
|
55971
|
50
|
|
|
|
109610
|
$conds_ref = _opgrep_helper($conds_ref) |
853
|
|
|
|
|
|
|
if 'ARRAY' eq ref $conds_ref; |
854
|
|
|
|
|
|
|
|
855
|
55971
|
|
|
|
|
58075
|
my @grep_ops; |
856
|
|
|
|
|
|
|
|
857
|
|
|
|
|
|
|
# Check whether we're dealing with a disjunction of patterns: |
858
|
55971
|
50
|
|
|
|
134691
|
my @conditions = exists($conds_ref->{disjunction}) ? @{$conds_ref->{disjunction}} : ($conds_ref); |
|
0
|
|
|
|
|
0
|
|
859
|
|
|
|
|
|
|
|
860
|
|
|
|
|
|
|
OP: |
861
|
55971
|
|
|
|
|
83011
|
for my $op (@_) { |
862
|
55971
|
100
|
66
|
|
|
222777
|
next unless ref $op and $$op; |
863
|
|
|
|
|
|
|
|
864
|
|
|
|
|
|
|
# only one condition by default, but if we have a disjunction, there will |
865
|
|
|
|
|
|
|
# be several |
866
|
|
|
|
|
|
|
CONDITION: |
867
|
55417
|
|
|
|
|
79502
|
foreach my $condition (@conditions) { |
868
|
|
|
|
|
|
|
# nested disjunctions? naughty user! |
869
|
|
|
|
|
|
|
# $foo or ($bar or $baz) is $foo or $bar or $baz! |
870
|
|
|
|
|
|
|
# ==> flatten |
871
|
55417
|
50
|
|
|
|
102002
|
if (exists($condition->{disjunction})) { |
872
|
0
|
|
|
|
|
0
|
push @conditions, @{$condition->{disjunction}}; |
|
0
|
|
|
|
|
0
|
|
873
|
0
|
|
|
|
|
0
|
next CONDITION; |
874
|
|
|
|
|
|
|
} |
875
|
|
|
|
|
|
|
|
876
|
|
|
|
|
|
|
# structure to hold captured information |
877
|
55417
|
|
|
|
|
77465
|
my $capture = {}; |
878
|
|
|
|
|
|
|
|
879
|
|
|
|
|
|
|
# Debugging aid |
880
|
55417
|
50
|
|
|
|
102170
|
if (exists $condition->{'dump'}) { |
881
|
|
|
|
|
|
|
($op->can($_) |
882
|
|
|
|
|
|
|
or next) |
883
|
|
|
|
|
|
|
and warn "$_: " . $op->$_ . "\n" |
884
|
0
|
|
0
|
|
|
0
|
for |
|
|
|
0
|
|
|
|
|
885
|
|
|
|
|
|
|
qw( first other last pmreplroot pmreplstart pmnext pmflags pmpermflags name targ type seq flags private kids); |
886
|
|
|
|
|
|
|
} |
887
|
|
|
|
|
|
|
|
888
|
|
|
|
|
|
|
# special disjunction case. undef in a disjunction => (child) does not exist |
889
|
55417
|
50
|
|
|
|
108290
|
if (not defined $condition) { |
890
|
0
|
0
|
0
|
|
|
0
|
return _TRUE if not defined $op and not wantarray(); |
891
|
0
|
|
|
|
|
0
|
return(); |
892
|
|
|
|
|
|
|
} |
893
|
|
|
|
|
|
|
|
894
|
|
|
|
|
|
|
# save the op if the user wants flat access to it |
895
|
55417
|
50
|
|
|
|
102184
|
if ($condition->{capture}) { |
896
|
0
|
|
|
|
|
0
|
$capture->{ $condition->{capture} } = $op; |
897
|
|
|
|
|
|
|
} |
898
|
|
|
|
|
|
|
|
899
|
|
|
|
|
|
|
# First, let's skim off ops of the wrong type. If they require |
900
|
|
|
|
|
|
|
# something that isn't implemented for this kind of object, it |
901
|
|
|
|
|
|
|
# must be wrong. These tests are cheap |
902
|
|
|
|
|
|
|
exists $condition->{$_} |
903
|
|
|
|
|
|
|
and !$op->can($_) |
904
|
|
|
|
|
|
|
and next |
905
|
55417
|
|
66
|
|
|
1326250
|
for |
|
|
|
50
|
|
|
|
|
906
|
|
|
|
|
|
|
qw( first other last pmreplroot pmreplstart pmnext pmflags pmpermflags name targ type seq flags private kids); |
907
|
|
|
|
|
|
|
|
908
|
|
|
|
|
|
|
# # Check alternations |
909
|
|
|
|
|
|
|
# ( ref( $condition->{$_} ) |
910
|
|
|
|
|
|
|
# ? ( "!" eq $condition->{$_}[0] |
911
|
|
|
|
|
|
|
# ? () |
912
|
|
|
|
|
|
|
# : () |
913
|
|
|
|
|
|
|
# ) |
914
|
|
|
|
|
|
|
# : ( $op->can($_) && $op->$_ eq $condition->{$_} or next ) |
915
|
|
|
|
|
|
|
# ) |
916
|
|
|
|
|
|
|
# for qw( name targ type seq flags private pmflags pmpermflags ); |
917
|
|
|
|
|
|
|
|
918
|
55417
|
|
|
|
|
73509
|
for my $test ( |
919
|
|
|
|
|
|
|
qw(name targ type seq flags private pmflags pmpermflags)) |
920
|
|
|
|
|
|
|
{ |
921
|
55417
|
50
|
|
|
|
107054
|
next unless exists $condition->{$test}; |
922
|
55417
|
|
|
|
|
196736
|
my $val = $op->$test; |
923
|
|
|
|
|
|
|
|
924
|
55417
|
50
|
|
|
|
159265
|
if ( 'ARRAY' eq ref $condition->{$test} ) { |
|
|
50
|
|
|
|
|
|
925
|
|
|
|
|
|
|
|
926
|
|
|
|
|
|
|
# Test a list of valid/invalid values. |
927
|
0
|
0
|
|
|
|
0
|
if ( '!' eq $condition->{$test}[0] ) { |
928
|
|
|
|
|
|
|
|
929
|
|
|
|
|
|
|
# Fail if any entries match. |
930
|
|
|
|
|
|
|
$_ ne $val |
931
|
|
|
|
|
|
|
or next CONDITION |
932
|
0
|
|
0
|
|
|
0
|
for @{ $condition->{$test} } |
|
0
|
|
|
|
|
0
|
|
933
|
0
|
|
|
|
|
0
|
[ 1 .. $#{ $condition->{$test} } ]; |
934
|
|
|
|
|
|
|
} |
935
|
|
|
|
|
|
|
else { |
936
|
|
|
|
|
|
|
|
937
|
|
|
|
|
|
|
# Fail if no entries match. |
938
|
0
|
|
|
|
|
0
|
my $okay = 0; |
939
|
|
|
|
|
|
|
|
940
|
|
|
|
|
|
|
$_ eq $val and $okay = 1, last |
941
|
0
|
|
0
|
|
|
0
|
for @{ $condition->{$test} }; |
|
0
|
|
|
|
|
0
|
|
942
|
|
|
|
|
|
|
|
943
|
0
|
0
|
|
|
|
0
|
next CONDITION if not $okay; |
944
|
|
|
|
|
|
|
} |
945
|
|
|
|
|
|
|
} |
946
|
|
|
|
|
|
|
elsif ( 'CODE' eq ref $condition->{$test} ) { |
947
|
0
|
|
|
|
|
0
|
local $_ = $val; |
948
|
0
|
0
|
|
|
|
0
|
$condition->{$test}($op) |
949
|
|
|
|
|
|
|
or next CONDITION; |
950
|
|
|
|
|
|
|
} |
951
|
|
|
|
|
|
|
else { |
952
|
|
|
|
|
|
|
|
953
|
|
|
|
|
|
|
# Test a single value. |
954
|
55417
|
50
|
|
|
|
309672
|
$condition->{$test} eq $op->$test |
955
|
|
|
|
|
|
|
or next CONDITION; |
956
|
|
|
|
|
|
|
} |
957
|
|
|
|
|
|
|
} # end for test |
958
|
|
|
|
|
|
|
|
959
|
|
|
|
|
|
|
# We know it ->can because that was tested above. It is an |
960
|
|
|
|
|
|
|
# error to have anything in this list of tests that isn't |
961
|
|
|
|
|
|
|
# tested for ->can above. |
962
|
0
|
|
|
|
|
0
|
foreach ( |
963
|
|
|
|
|
|
|
qw( first other last sibling next pmreplroot pmreplstart pmnext ) |
964
|
|
|
|
|
|
|
) { |
965
|
0
|
0
|
|
|
|
0
|
next unless exists $condition->{$_}; |
966
|
0
|
|
|
|
|
0
|
my ($result) = opgrep( $condition->{$_}, $op->$_ ); |
967
|
0
|
0
|
|
|
|
0
|
next CONDITION if not $result; |
968
|
|
|
|
|
|
|
|
969
|
0
|
0
|
|
|
|
0
|
if (not blessed($result)) { |
970
|
|
|
|
|
|
|
# copy over the captured data/ops from the recursion |
971
|
0
|
|
|
|
|
0
|
$capture->{$_} = $result->{$_} foreach keys %$result; |
972
|
|
|
|
|
|
|
} |
973
|
|
|
|
|
|
|
} |
974
|
|
|
|
|
|
|
|
975
|
|
|
|
|
|
|
# Apply all kids conditions. We $op->can(kids) (see above). |
976
|
0
|
0
|
|
|
|
0
|
if (exists $condition->{kids}) { |
977
|
0
|
|
|
|
|
0
|
my $kidno = 0; |
978
|
0
|
|
|
|
|
0
|
my $kidconditions = $condition->{kids}; |
979
|
|
|
|
|
|
|
|
980
|
0
|
0
|
|
|
|
0
|
next CONDITION if not @{$kidconditions} == @{$condition->{kids}}; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
981
|
|
|
|
|
|
|
|
982
|
0
|
|
|
|
|
0
|
foreach my $kid ($op->kids()) { |
983
|
|
|
|
|
|
|
# if you put undef in your kid conditions list, we skip one kid |
984
|
0
|
0
|
|
|
|
0
|
next if not defined $kidconditions->[$kidno]; |
985
|
|
|
|
|
|
|
|
986
|
0
|
|
|
|
|
0
|
my ($result) = opgrep( $kidconditions->[$kidno++], $kid ); |
987
|
0
|
0
|
|
|
|
0
|
next CONDITION if not $result; |
988
|
|
|
|
|
|
|
|
989
|
0
|
0
|
|
|
|
0
|
if (not blessed($result)) { |
990
|
|
|
|
|
|
|
# copy over the captured data/ops from the recursion |
991
|
0
|
|
|
|
|
0
|
$capture->{$_} = $result->{$_} foreach keys %$result; |
992
|
|
|
|
|
|
|
} |
993
|
|
|
|
|
|
|
} |
994
|
|
|
|
|
|
|
} |
995
|
|
|
|
|
|
|
|
996
|
|
|
|
|
|
|
# Attempt to quit early if possible. |
997
|
0
|
0
|
|
|
|
0
|
if (wantarray) { |
|
|
0
|
|
|
|
|
|
998
|
0
|
0
|
|
|
|
0
|
if (keys %$capture) { |
999
|
|
|
|
|
|
|
# save all captured information and the main op |
1000
|
0
|
|
|
|
|
0
|
$capture->{op} = $op; |
1001
|
0
|
|
|
|
|
0
|
push @grep_ops, $capture; |
1002
|
|
|
|
|
|
|
} |
1003
|
|
|
|
|
|
|
else { |
1004
|
|
|
|
|
|
|
# save main op |
1005
|
0
|
|
|
|
|
0
|
push @grep_ops, $op; |
1006
|
|
|
|
|
|
|
} |
1007
|
0
|
|
|
|
|
0
|
last; |
1008
|
|
|
|
|
|
|
} |
1009
|
|
|
|
|
|
|
elsif ( defined wantarray ) { |
1010
|
0
|
|
|
|
|
0
|
return _TRUE; |
1011
|
|
|
|
|
|
|
} |
1012
|
|
|
|
|
|
|
} # end for @conditions |
1013
|
|
|
|
|
|
|
# end of conditions loop should be end of op test |
1014
|
|
|
|
|
|
|
} |
1015
|
|
|
|
|
|
|
|
1016
|
|
|
|
|
|
|
# Either this was called in list context and then I want to just |
1017
|
|
|
|
|
|
|
# return everything possible or this is in scalar/void context and |
1018
|
|
|
|
|
|
|
# @grep_ops will be empty and thus "false." |
1019
|
55971
|
|
|
|
|
150302
|
return @grep_ops; |
1020
|
|
|
|
|
|
|
} |
1021
|
|
|
|
|
|
|
|
1022
|
|
|
|
|
|
|
sub _opgrep_helper { |
1023
|
|
|
|
|
|
|
my @conds = |
1024
|
0
|
0
|
|
0
|
|
|
map ref() ? {%$_} : { name => $_ }, @{ $_[0] }; |
|
0
|
|
|
|
|
|
|
1025
|
|
|
|
|
|
|
|
1026
|
|
|
|
|
|
|
# Wire this into a list of entries, all ->next |
1027
|
0
|
|
|
|
|
|
for ( 1 .. $#conds ) { |
1028
|
0
|
|
|
|
|
|
$conds[ $_ - 1 ]{next} = $conds[$_]; |
1029
|
|
|
|
|
|
|
} |
1030
|
|
|
|
|
|
|
|
1031
|
|
|
|
|
|
|
# This is a linked list now so I can return only the head. |
1032
|
0
|
|
|
|
|
|
return $conds[0]; |
1033
|
|
|
|
|
|
|
} |
1034
|
|
|
|
|
|
|
|
1035
|
|
|
|
|
|
|
=item C |
1036
|
|
|
|
|
|
|
|
1037
|
|
|
|
|
|
|
Unlike the chaining of conditions done by C itself if there are multiple |
1038
|
|
|
|
|
|
|
conditions, this function creates a disjunction (C<$cond1 || $cond2 || ...>) of |
1039
|
|
|
|
|
|
|
the conditions and returns a structure (hash reference) that can be passed to |
1040
|
|
|
|
|
|
|
opgrep as a single condition. |
1041
|
|
|
|
|
|
|
|
1042
|
|
|
|
|
|
|
Example: |
1043
|
|
|
|
|
|
|
|
1044
|
|
|
|
|
|
|
my $sub_structure = { |
1045
|
|
|
|
|
|
|
name => 'helem', |
1046
|
|
|
|
|
|
|
first => { name => 'rv2hv', }, |
1047
|
|
|
|
|
|
|
'last' => { name => 'const', }, |
1048
|
|
|
|
|
|
|
}; |
1049
|
|
|
|
|
|
|
|
1050
|
|
|
|
|
|
|
my @ops = opgrep( { |
1051
|
|
|
|
|
|
|
name => 'leavesub', |
1052
|
|
|
|
|
|
|
first => { |
1053
|
|
|
|
|
|
|
name => 'lineseq', |
1054
|
|
|
|
|
|
|
kids => [, |
1055
|
|
|
|
|
|
|
{ name => 'nextstate', }, |
1056
|
|
|
|
|
|
|
op_or( |
1057
|
|
|
|
|
|
|
{ |
1058
|
|
|
|
|
|
|
name => 'return', |
1059
|
|
|
|
|
|
|
first => { name => 'pushmark' }, |
1060
|
|
|
|
|
|
|
last => $sub_structure, |
1061
|
|
|
|
|
|
|
}, |
1062
|
|
|
|
|
|
|
$sub_structure, |
1063
|
|
|
|
|
|
|
), |
1064
|
|
|
|
|
|
|
], |
1065
|
|
|
|
|
|
|
}, |
1066
|
|
|
|
|
|
|
}, $op_obj ); |
1067
|
|
|
|
|
|
|
|
1068
|
|
|
|
|
|
|
This example matches the code in a typical simplest-possible |
1069
|
|
|
|
|
|
|
accessor method (albeit not down to the last bit): |
1070
|
|
|
|
|
|
|
|
1071
|
|
|
|
|
|
|
sub get_foo { $_[0]->{foo} } |
1072
|
|
|
|
|
|
|
|
1073
|
|
|
|
|
|
|
But by adding an alternation |
1074
|
|
|
|
|
|
|
we can also match optional op layers. In this case, we optionally |
1075
|
|
|
|
|
|
|
match a return statement, so the following implementation is also |
1076
|
|
|
|
|
|
|
recognized: |
1077
|
|
|
|
|
|
|
|
1078
|
|
|
|
|
|
|
sub get_foo { return $_[0]->{foo} } |
1079
|
|
|
|
|
|
|
|
1080
|
|
|
|
|
|
|
Essentially, this is syntactic sugar for the following structure |
1081
|
|
|
|
|
|
|
recognized by C: |
1082
|
|
|
|
|
|
|
|
1083
|
|
|
|
|
|
|
{ disjunction => [@conditions] } |
1084
|
|
|
|
|
|
|
|
1085
|
|
|
|
|
|
|
=cut |
1086
|
|
|
|
|
|
|
|
1087
|
|
|
|
|
|
|
sub op_or { |
1088
|
0
|
|
|
0
|
|
|
my @conditions = @_; |
1089
|
0
|
|
|
|
|
|
return({ disjunction => [@conditions] }); |
1090
|
|
|
|
|
|
|
} |
1091
|
|
|
|
|
|
|
|
1092
|
|
|
|
|
|
|
# TODO |
1093
|
|
|
|
|
|
|
# sub op_pattern_match { |
1094
|
|
|
|
|
|
|
# my $op = shift; |
1095
|
|
|
|
|
|
|
# my $pattern = shift; |
1096
|
|
|
|
|
|
|
# |
1097
|
|
|
|
|
|
|
# my $ret = {}; |
1098
|
|
|
|
|
|
|
# |
1099
|
|
|
|
|
|
|
# |
1100
|
|
|
|
|
|
|
# return $ret; |
1101
|
|
|
|
|
|
|
# } |
1102
|
|
|
|
|
|
|
|
1103
|
|
|
|
|
|
|
=item C |
1104
|
|
|
|
|
|
|
|
1105
|
|
|
|
|
|
|
=item C |
1106
|
|
|
|
|
|
|
|
1107
|
|
|
|
|
|
|
Warn and die, respectively, from the perspective of the position of |
1108
|
|
|
|
|
|
|
the op in the program. Sounds complicated, but it's exactly the kind |
1109
|
|
|
|
|
|
|
of error reporting you expect when you're grovelling through an op |
1110
|
|
|
|
|
|
|
tree. |
1111
|
|
|
|
|
|
|
|
1112
|
|
|
|
|
|
|
=cut |
1113
|
|
|
|
|
|
|
|
1114
|
0
|
|
|
0
|
1
|
|
sub carp (@) { CORE::warn( _preparewarn(@_) ) } |
1115
|
0
|
|
|
0
|
1
|
|
sub croak (@) { CORE::die( _preparewarn(@_) ) } |
1116
|
|
|
|
|
|
|
|
1117
|
|
|
|
|
|
|
=item C |
1118
|
|
|
|
|
|
|
|
1119
|
|
|
|
|
|
|
Override L default to force global loading. |
1120
|
|
|
|
|
|
|
|
1121
|
|
|
|
|
|
|
=cut |
1122
|
|
|
|
|
|
|
|
1123
|
|
|
|
|
|
|
sub _preparewarn { |
1124
|
0
|
|
|
0
|
|
|
my $args = join '', @_; |
1125
|
0
|
0
|
|
|
|
|
$args = "Something's wrong " unless $args; |
1126
|
0
|
0
|
|
|
|
|
if ( "\n" ne substr $args, -1, 1 ) { |
1127
|
0
|
|
|
|
|
|
$args .= " at $B::Utils1::file line $B::Utils::line.\n"; |
1128
|
|
|
|
|
|
|
} |
1129
|
0
|
|
|
|
|
|
return $args; |
1130
|
|
|
|
|
|
|
} |
1131
|
|
|
|
|
|
|
|
1132
|
|
|
|
|
|
|
=back |
1133
|
|
|
|
|
|
|
|
1134
|
|
|
|
|
|
|
=head2 EXPORT |
1135
|
|
|
|
|
|
|
|
1136
|
|
|
|
|
|
|
None by default. |
1137
|
|
|
|
|
|
|
|
1138
|
|
|
|
|
|
|
=head2 XS EXPORT |
1139
|
|
|
|
|
|
|
|
1140
|
|
|
|
|
|
|
This modules uses L to export some useful functions |
1141
|
|
|
|
|
|
|
for XS modules to use. To use those, include in your Makefile.PL: |
1142
|
|
|
|
|
|
|
|
1143
|
|
|
|
|
|
|
my $pkg = ExtUtils::Depends->new("Your::XSModule", "B::Utils1"); |
1144
|
|
|
|
|
|
|
WriteMakefile( |
1145
|
|
|
|
|
|
|
... # your normal makefile flags |
1146
|
|
|
|
|
|
|
$pkg->get_makefile_vars, |
1147
|
|
|
|
|
|
|
); |
1148
|
|
|
|
|
|
|
|
1149
|
|
|
|
|
|
|
Your XS module can now include F. To see |
1150
|
|
|
|
|
|
|
document for the functions provided, use: |
1151
|
|
|
|
|
|
|
|
1152
|
|
|
|
|
|
|
perldoc -m B::Utils1::Install::BUtils.h |
1153
|
|
|
|
|
|
|
|
1154
|
|
|
|
|
|
|
=head1 INSTALLATION |
1155
|
|
|
|
|
|
|
|
1156
|
|
|
|
|
|
|
To install this module, you may want to run the following commands: |
1157
|
|
|
|
|
|
|
|
1158
|
|
|
|
|
|
|
perl Makefile.PL |
1159
|
|
|
|
|
|
|
make test |
1160
|
|
|
|
|
|
|
sudo make install |
1161
|
|
|
|
|
|
|
|
1162
|
|
|
|
|
|
|
=head1 AUTHOR |
1163
|
|
|
|
|
|
|
|
1164
|
|
|
|
|
|
|
Maintained by Reini Urban C. |
1165
|
|
|
|
|
|
|
|
1166
|
|
|
|
|
|
|
Originally written by Simon Cozens, C as B::Utils. |
1167
|
|
|
|
|
|
|
|
1168
|
|
|
|
|
|
|
Previously maintained by Joshua ben Jore, C and Karen |
1169
|
|
|
|
|
|
|
Etheridge as B::Utils. |
1170
|
|
|
|
|
|
|
|
1171
|
|
|
|
|
|
|
Contributions from Mattia Barbon, Jim Cromie, Steffen Mueller, and |
1172
|
|
|
|
|
|
|
Chia-liang Kao, Alexandr Ciornii. |
1173
|
|
|
|
|
|
|
|
1174
|
|
|
|
|
|
|
=head1 LICENSE |
1175
|
|
|
|
|
|
|
|
1176
|
|
|
|
|
|
|
This module is free software; you can redistribute it and/or modify it |
1177
|
|
|
|
|
|
|
under the same terms as Perl itself. |
1178
|
|
|
|
|
|
|
|
1179
|
|
|
|
|
|
|
=head1 SEE ALSO |
1180
|
|
|
|
|
|
|
|
1181
|
|
|
|
|
|
|
L, L, L. |
1182
|
|
|
|
|
|
|
|
1183
|
|
|
|
|
|
|
=cut |
1184
|
|
|
|
|
|
|
|
1185
|
|
|
|
|
|
|
1; |