blib/lib/ExtUtils/Typemaps/OutputMap.pm

filter: Statementstmt 98.3% 58 / 59 Branchbran 88.8% 16 / 18 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 11 / 11 Podpod 100% 6 / 6 totaltotal 90.3% 94 / 104 CC 12 SCAR 14.2
CC
12
Cov
93%
new
8.4
targetable_legacy
3.0
code
2.1
CRAP 4.2
« blib/lib/ExtUtils/Typemaps/InputMap.pm ↑ Summary blib/lib/ExtUtils/ParseXS/Utilities.pm »
1 package ExtUtils::Typemaps::OutputMap;
2 24 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 24 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 24 our $VERSION = '3.64';
6  
7 =head1 NAME
8  
9 ExtUtils::Typemaps::OutputMap - Entry in the OUTPUT section of a typemap
10  
11 =head1 SYNOPSIS
12  
13 use ExtUtils::Typemaps;
14 ...
15 my $output = $typemap->get_output_map('T_NV');
16 my $code = $output->code();
17 $output->code("...");
18  
19 =head1 DESCRIPTION
20  
21 Refer to L<ExtUtils::Typemaps> for details.
22  
23 =head1 METHODS
24  
25 =cut
26  
27 =head2 new
28  
29 Requires C<xstype> and C<code> parameters.
30  
31 =cut
32  
33 113024 ▶sub new {
Subroutine new: called CC=8 SCAR=21.3
Pod documented
34 113024 my $prot = shift;
35 113024 ▶ my $class = ref($prot)||$prot;
Condition66%2/3
ref $prot || $prot
l!l&&r!l&&!r
75311377130
MC/DC0%0/2
ref $prot || $prot
ref $prot $prot
Truth table66%2/3
A || B
A= ref $protB= $prot
ABresult
1X1
011
000
36 113024 my %args = @_;
37  
38 113024 ▶ if (!ref($prot)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $prot) 37713 75311 113024
39 37713 ▶ if (not defined $args{xstype} or not defined $args{code}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"xstype"} and defined $args{"code"}) 0 37713 37713
Condition33%1/3
defined $args{"xstype"} and defined $args{"code"}
!ll&&!rl&&r
0037713
MC/DC0%0/2
defined $args{"xstype"} and defined $args{"code"}
defined $args{"xstype"} defined $args{"code"}
Truth table33%1/3
A and B
A= defined $args{"xstype"}B= defined $args{"code"}
ABresult
0X0
100
111
40 0 die("Need xstype and code parameters");
41 }
42 }
43  
44 113024 ▶ my $self = bless(
Branch: 1 branch
BranchTrueFalseTotal
ref $prot ? : 75311 37713 113024
45 (ref($prot) ? {%$prot} : {})
46 => $class
47 );
48  
49 113024 ▶ $self->{xstype} = $args{xstype} if defined $args{xstype};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"xstype"} 37713 75311 113024
50 113024 ▶ $self->{code} = $args{code} if defined $args{code};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"code"} 37713 75311 113024
51 113024 $self->{code} =~ s/^(?=\S)/\t/mg;
52  
53 113024 return $self;
54 }
55  
56 =head2 code
57  
58 Returns or sets the OUTPUT mapping code for this entry.
59  
60 =cut
61  
62 665 ▶sub code {
Subroutine code: called CC=2 SCAR=7.2
Pod documented
63 665 ▶ $_[0]->{code} = $_[1] if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 665 665
64 665 return $_[0]->{code};
65 }
66  
67 =head2 xstype
68  
69 Returns the name of the XS type of the OUTPUT map.
70  
71 =cut
72  
73 150666 ▶sub xstype {
Subroutine xstype: called CC=1 SCAR=0.0
Pod documented
74 150666 return $_[0]->{xstype};
75 }
76  
77 =head2 cleaned_code
78  
79 Returns a cleaned-up copy of the code to which certain transformations
80 have been applied to make it more ANSI compliant.
81  
82 =cut
83  
84 299 ▶sub cleaned_code {
Subroutine cleaned_code: called CC=1 SCAR=0.0
Pod documented
85 299 my $self = shift;
86 299 my $code = $self->code;
87  
88 # Move C pre-processor instructions to column 1 to be strictly ANSI
89 # conformant. Some pre-processors are fussy about this.
90 299 $code =~ s/^\s+#/#/mg;
91 299 $code =~ s/\s*\z/\n/;
92  
93 299 return $code;
94 }
95  
96 =head2 targetable_legacy
97  
98 Do not use for new code.
99  
100 This is the original version of the targetable() method, whose behaviour
101 has been frozen for backwards compatibility. It is used to determine
102 whether to emit an early C<dXSTARG>, which will be in scope for most of
103 the XSUB. More recent XSUB code generation emits a C<dXSTARG> in a tighter
104 scope if one has not already been emitted. Some XS code assumes that
105 C<TARG> has been declared, so continue to declare it under the same
106 conditions as before. The newer C<targetable> method may be true under
107 additional circumstances.
108  
109 If the optimization can not be applied, this returns undef. If it can be
110 applied, this method returns a hash reference containing the following
111 information:
112  
113 type: Any of the characters i, u, n, p
114 with_size: Bool indicating whether this is the sv_setpvn variant
115 what: The code that actually evaluates to the output scalar
116 what_size: If "with_size", this has the string length (as code,
117 not constant, including leading comma)
118  
119  
120 =cut
121  
122 285 ▶sub targetable_legacy {
Subroutine targetable_legacy: called CC=3 SCAR=11.0
Pod documented
123 285 my $self = shift;
124 285 ▶ return $self->{targetable_legacy} if exists $self->{targetable_legacy};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"targetable_legacy"} 40 245 285
125  
126 245 our $bal; # ()-balanced
127 $bal = qr[
128 (?:
129 (?>[^()]+)
130 |
131 \( (??{ $bal }) \)
132 )*
133 245 ]x;
134 my $bal_no_comma = qr[
135 (?:
136 (?>[^(),]+)
137 |
138 \( (??{ $bal }) \)
139 )+
140 245 ]x;
141  
142 # matches variations on (SV*)
143 245 my $sv_cast = qr[
144 (?:
145 \( \s* SV \s* \* \s* \) \s*
146 )?
147 ]x;
148  
149 my $size = qr[ # Third arg (to setpvn)
150 , \s* (??{ $bal })
151 245 ]xo;
152  
153 245 my $code = $self->code;
154  
155 # We can still bootstrap compile 're', because in code re.pm is
156 # available to miniperl, and does not attempt to load the XS code.
157 24 ▶ use re 'eval';
Subroutine BEGIN: called CC=1 SCAR=0.0
158  
159 245 my ($type, $with_size, $arg, $sarg) =
160 ($code =~
161 m[^
162 \s+
163 sv_set([iunp])v(n)? # Type, is_setpvn
164 \s*
165 \( \s*
166 $sv_cast \$arg \s* , \s*
167 ( $bal_no_comma ) # Set from
168 ( $size )? # Possible sizeof set-from
169 \s* \) \s* ; \s* $
170 ]xo
171 );
172  
173 245 my $rv = undef;
174 245 ▶ if ($type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type) 197 48 245
175 197 $rv = {
176 type => $type,
177 with_size => $with_size,
178 what => $arg,
179 what_size => $sarg,
180 };
181 }
182 245 $self->{targetable_legacy} = $rv;
183 245 return $rv;
184 }
185  
186 =head2 targetable
187  
188 Class method.
189  
190 Return a boolean indicating whether the supplied code snippet is suitable
191 for using TARG as the destination SV rather than an new mortal.
192  
193 In principle most things are, except expressions which would set the SV
194 to a ref value. That can cause the referred value to never be freed, as
195 targs aren't freed (at least for the lifetime of their CV). So in
196 practice, we restrict it to an approved list of sv_setfoo() forms, and
197 only where there is no extra code following the sv_setfoo() (so we have to
198 match the closing bracket, allowing for nested brackets etc within).
199  
200 =cut
201  
202 24 my %targetable_cache;
203  
204 273 ▶sub targetable {
Subroutine targetable: called CC=2 SCAR=6.9
Pod documented
205 273 my ($class, $code) = @_;
206  
207 273 ▶ return $targetable_cache{$code} if exists $targetable_cache{$code};
Branch: 1 branch
BranchTrueFalseTotal
if exists $targetable_cache{$code} 188 85 273
208  
209 # Match a string with zero or more balanced/nested parentheses
210 # within it, e.g.
211 #
212 # "aa,bb(cc,dd)ee(ff,gg(hh,ii)jj,kk)ll"
213  
214 85 our $bal;
215 $bal = qr[
216 (?:
217 (?>[^()]+)
218 |
219 " ([^"] | \\")* "
220 |
221 \( (??{ $bal }) \)
222 )*
223 85 ]x;
224  
225 # Like $bal, but doesn't allow commas at the *top* level; e.g.
226 #
227 # "aabb(cc,dd)ee(ff,gg(hh,ii)jj,kk)ll"
228 #
229 # Something like "aa,bb(cc,dd)" will just match/consume the "aa"
230 # part of the string.
231  
232 my $bal_no_comma = qr[
233 (?:
234 (?>[^(),]+)
235 |
236 " ([^"] | \\")* "
237 |
238 \( (??{ $bal }) \)
239 )+
240 85 ]x;
241  
242 # the SV whose value is to be set (typically arg 1)
243 # Note that currently ParseXS will always call with $arg expanded
244 # to 'RETVALSV', but also match other possibilities too for future
245 # use.
246  
247 85 my $target = qr[
248 (?:
249 \( \s* SV \s* \* \s* \) \s* # optional SV* cast
250 )?
251 (?:
252 \$arg
253 |
254 RETVAL
255 |
256 RETVALSV
257 |
258 ST\(\d+\)
259 )
260 \s*
261 ]x;
262  
263 # We can still bootstrap compile 're', because in code re.pm is
264 # available to miniperl, and does not attempt to load the XS code.
265 24 ▶ use re 'eval';
Subroutine BEGIN: called CC=1 SCAR=0.0
266  
267 85 my $match =
268 ($code =~
269 m[^
270 \s*
271 (?:
272 # 1-arg functions
273 sv_set_(?:undef|true|false)
274 \s*
275 \( \s*
276 $target # arg 1: SV to set
277 |
278 # 2-arg functions
279 sv_set(?:iv|iv_mg|uv|uv_mg|nv|nv_mg|pv|pv_mg|_bool)
280 \s*
281 \( \s*
282 $target # arg 1: SV to set
283 , \s*
284 $bal_no_comma # arg 2: value to use
285 |
286 # 3-arg functions
287 sv_set(?:pvn|pvn_mg)
288 \s*
289 \( \s*
290 $target # arg 1: SV to set
291 , \s*
292 $bal_no_comma # arg 2: value to use
293 , \s*
294 $bal_no_comma # arg 3: length
295 )
296 \s* \)
297 \s* ; \s*
298 $
299 ]xo
300 );
301  
302 85 $targetable_cache{$code} = $match;
303 85 return $match;
304 }
305  
306 =head1 SEE ALSO
307  
308 L<ExtUtils::Typemaps>
309  
310 =head1 AUTHOR
311  
312 Steffen Mueller C<<smueller@cpan.org>>
313  
314 =head1 COPYRIGHT & LICENSE
315  
316 Copyright 2009, 2010, 2011, 2012 Steffen Mueller
317  
318 This program is free software; you can redistribute it and/or
319 modify it under the same terms as Perl itself.
320  
321 =cut
322  
323 24 1;
324  
« blib/lib/ExtUtils/Typemaps/InputMap.pm ↑ Summary blib/lib/ExtUtils/ParseXS/Utilities.pm »