blib/lib/ExtUtils/Typemaps.pm

filter: Statementstmt 94.1% 352 / 374 Branchbran 74.1% 132 / 178 Conditioncond 69.3% 43 / 62 MC/DCmcdc 22.5% 9 / 40 Subroutinesub 93.9% 31 / 33 Podpod 95.2% 20 / 21 totaltotal 82.9% 587 / 708 CC 124 SCAR 22.7
CC
124
Cov
86%
_parse
25.1
merge
11.5
get_inputmap
10.3
CRAP 9.7
« blib/lib/ExtUtils/ParseXS/Node.pm ↑ Summary blib/lib/ExtUtils/Typemaps/Type.pm »
1 package ExtUtils::Typemaps;
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 24 require ExtUtils::ParseXS;
8 24 require ExtUtils::ParseXS::Constants;
9 24 require ExtUtils::Typemaps::InputMap;
10 24 require ExtUtils::Typemaps::OutputMap;
11 24 require ExtUtils::Typemaps::Type;
12  
13 =head1 NAME
14  
15 ExtUtils::Typemaps - Read/Write/Modify Perl/XS typemap files
16  
17 =head1 SYNOPSIS
18  
19 # read/create file
20 my $typemap = ExtUtils::Typemaps->new(file => 'typemap');
21 # alternatively create an in-memory typemap
22 # $typemap = ExtUtils::Typemaps->new();
23 # alternatively create an in-memory typemap by parsing a string
24 # $typemap = ExtUtils::Typemaps->new(string => $sometypemap);
25  
26 # add a mapping
27 $typemap->add_typemap(ctype => 'NV', xstype => 'T_NV');
28 $typemap->add_inputmap(
29 xstype => 'T_NV', code => '$var = ($type)SvNV($arg);'
30 );
31 $typemap->add_outputmap(
32 xstype => 'T_NV', code => 'sv_setnv($arg, (NV)$var);'
33 );
34 $typemap->add_string(string => $typemapstring);
35 # will be parsed and merged
36  
37 # remove a mapping (same for remove_typemap and remove_outputmap...)
38 $typemap->remove_inputmap(xstype => 'SomeType');
39  
40 # save a typemap to a file
41 $typemap->write(file => 'anotherfile.map');
42  
43 # merge the other typemap into this one
44 $typemap->merge(typemap => $another_typemap);
45  
46 =head1 DESCRIPTION
47  
48 This module can read, modify, create and write Perl XS typemap files. If you don't know
49 what a typemap is, please confer the L<perlxstut> and L<perlxs> manuals.
50  
51 The module is not entirely round-trip safe: For example it currently simply strips all comments.
52 The order of entries in the maps is, however, preserved.
53  
54 We check for duplicate entries in the typemap, but do not check for missing
55 C<TYPEMAP> entries for C<INPUTMAP> or C<OUTPUTMAP> entries since these might be hidden
56 in a different typemap.
57  
58 =head1 METHODS
59  
60 =cut
61  
62 =head2 new
63  
64 Returns a new typemap object. Takes an optional C<file> parameter.
65 If set, the given file will be read. If the file doesn't exist, an empty typemap
66 is returned.
67  
68 Alternatively, if the C<string> parameter is given, the supplied
69 string will be parsed instead of a file.
70  
71 =cut
72  
73 1520 ▶sub new {
Subroutine new: called CC=3 SCAR=11.4
Pod documented
74 1520 my $class = shift;
75 1520 my %args = @_;
76  
77 1520 ▶ if (defined $args{file} and defined $args{string}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"file"} and defined $args{"string"}) 0 1520 1520
Condition66%2/3
defined $args{"file"} and defined $args{"string"}
!ll&&!rl&&r
6278930
MC/DC0%0/2
defined $args{"file"} and defined $args{"string"}
defined $args{"file"} defined $args{"string"}
Truth table66%2/3
A and B
A= defined $args{"file"}B= defined $args{"string"}
ABresult
0X0
100
111
78 0 die("Cannot handle both 'file' and 'string' arguments to constructor");
79 }
80  
81 1520 my $self = bless {
82 file => undef,
83 %args,
84 typemap_section => [],
85 typemap_lookup => {},
86 input_section => [],
87 input_lookup => {},
88 output_section => [],
89 output_lookup => {},
90 } => $class;
91  
92 1520 $self->_init();
93  
94 1520 return $self;
95 }
96  
97 1520 ▶sub _init {
Subroutine _init: called CC=5 SCAR=16.2
98 1520 my $self = shift;
99 1520 ▶ if (defined $self->{string}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'string'}) { } 171 1349 1520
elsif (defined $self->{'file'} and -e $self->{'file'}) { } 893 912 1805
100 171 $self->_parse(\($self->{string}), $self->{lineno_offset}, $self->{fake_filename});
101 171 delete $self->{string};
102 }
103 ▶ elsif (defined $self->{file} and -e $self->{file}) {
Condition66%2/3
defined $self->{"file"} and -e $self->{"file"}
!ll&&!rl&&r
4560893
MC/DC50%1/2
defined $self->{"file"} and -e $self->{"file"}
defined $self->{"file"} -e $self->{"file"}
Truth table66%2/3
A and B
A= defined $self->{"file"}B= -e $self->{"file"}
ABresult
0X0
100
111
104 open my $fh, '<', $self->{file}
105 or die "Cannot open typemap file '"
106 893 ▶ . $self->{file} . "' for reading: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $self->{"file"} or die q[Cannot open typemap file '] . $self->{"file"} . "' for reading: $!" 893 0 893
107 893 local $/ = undef;
108 893 my $string = <$fh>;
109 893 $self->_parse(\$string, $self->{lineno_offset}, $self->{file});
110 }
111 }
112  
113  
114 =head2 file
115  
116 Get/set the file that the typemap is written to when the
117 C<write> method is called.
118  
119 =cut
120  
121 0 ▶sub file {
Subroutine file: not called CC=2 SCAR=17.9
Pod documented
122 0 ▶ $_[0]->{file} = $_[1] if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 0 0
123 $_[0]->{file}
124 0 }
125  
126 =head2 add_typemap
127  
128 Add a C<TYPEMAP> entry to the typemap.
129  
130 Required named arguments: The C<ctype> (e.g. C<ctype =E<gt> 'double'>)
131 and the C<xstype> (e.g. C<xstype =E<gt> 'T_NV'>).
132  
133 Optional named arguments: C<replace =E<gt> 1> forces removal/replacement of
134 existing C<TYPEMAP> entries of the same C<ctype>. C<skip =E<gt> 1>
135 triggers a I<"first come first serve"> logic by which new entries that conflict
136 with existing entries are silently ignored.
137  
138 As an alternative to the named parameters usage, you may pass in
139 an C<ExtUtils::Typemaps::Type> object as first argument, a copy of which will be
140 added to the typemap. In that case, only the C<replace> or C<skip> named parameters
141 may be used after the object. Example:
142  
143 $map->add_typemap($type_obj, replace => 1);
144  
145 =cut
146  
147 90228 ▶sub add_typemap {
Subroutine add_typemap: called CC=9 SCAR=22.3
Pod documented
148 90228 my $self = shift;
149 90228 my $type;
150 my %args;
151  
152 90228 ▶ if ((@_ % 2) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2 == 1) { } 90211 17 90228
153 90211 my $orig = shift;
154 90211 $type = $orig->new();
155 90211 %args = @_;
156 }
157 else {
158 17 %args = @_;
159 17 my $ctype = $args{ctype};
160 17 ▶ die("Need ctype argument") if not defined $ctype;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ctype 0 17 17
161 17 my $xstype = $args{xstype};
162 17 ▶ die("Need xstype argument") if not defined $xstype;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $xstype 0 17 17
163  
164 $type = ExtUtils::Typemaps::Type->new(
165 xstype => $xstype,
166 17 'prototype' => $args{'prototype'},
167 ctype => $ctype,
168 );
169 }
170  
171 90228 ▶ if ($args{skip} and $args{replace}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"skip"} and $args{"replace"}) 0 90228 90228
Condition66%2/3
$args{"skip"} and $args{"replace"}
!ll&&!rl&&r
9022710
MC/DC0%0/2
$args{"skip"} and $args{"replace"}
$args{"skip"} $args{"replace"}
Truth table66%2/3
A and B
A= $args{"skip"}B= $args{"replace"}
ABresult
0X0
100
111
172 0 die("Cannot use both 'skip' and 'replace'");
173 }
174  
175 90228 ▶ if ($args{replace}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'replace'}) { } 89641 587 90228
elsif ($args{'skip'}) { } 1 586 587
176 89641 $self->remove_typemap(ctype => $type->ctype);
177 }
178 elsif ($args{skip}) {
179 1 ▶ return() if exists $self->{typemap_lookup}{$type->ctype};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"typemap_lookup"}{$type->ctype} 1 0 1
180 }
181 else {
182 586 $self->validate(typemap_xstype => $type->xstype, ctype => $type->ctype);
183 }
184  
185 # store
186 90226 push @{$self->{typemap_section}}, $type;
187 # remember type for lookup, too.
188 90226 $self->{typemap_lookup}{$type->tidy_ctype} = $#{$self->{typemap_section}};
189  
190 90226 return 1;
191 }
192  
193 =head2 add_inputmap
194  
195 Add an C<INPUT> entry to the typemap.
196  
197 Required named arguments:
198 The C<xstype> (e.g. C<xstype =E<gt> 'T_NV'>)
199 and the C<code> to associate with it for input.
200  
201 Optional named arguments: C<replace =E<gt> 1> forces removal/replacement of
202 existing C<INPUT> entries of the same C<xstype>. C<skip =E<gt> 1>
203 triggers a I<"first come first serve"> logic by which new entries that conflict
204 with existing entries are silently ignored.
205  
206 As an alternative to the named parameters usage, you may pass in
207 an C<ExtUtils::Typemaps::InputMap> object as first argument, a copy of which will be
208 added to the typemap. In that case, only the C<replace> or C<skip> named parameters
209 may be used after the object. Example:
210  
211 $map->add_inputmap($type_obj, replace => 1);
212  
213 =cut
214  
215 75224 ▶sub add_inputmap {
Subroutine add_inputmap: called CC=9 SCAR=22.3
Pod documented
216 75224 my $self = shift;
217 75224 my $input;
218 my %args;
219  
220 75224 ▶ if ((@_ % 2) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2 == 1) { } 75214 10 75224
221 75214 my $orig = shift;
222 75214 $input = $orig->new();
223 75214 %args = @_;
224 }
225 else {
226 10 %args = @_;
227 10 my $xstype = $args{xstype};
228 10 ▶ die("Need xstype argument") if not defined $xstype;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $xstype 0 10 10
229 10 my $code = $args{code};
230 10 ▶ die("Need code argument") if not defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 0 10 10
231  
232 10 $input = ExtUtils::Typemaps::InputMap->new(
233 xstype => $xstype,
234 code => $code,
235 );
236 }
237  
238 75224 ▶ if ($args{skip} and $args{replace}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"skip"} and $args{"replace"}) 0 75224 75224
Condition66%2/3
$args{"skip"} and $args{"replace"}
!ll&&!rl&&r
7522310
MC/DC0%0/2
$args{"skip"} and $args{"replace"}
$args{"skip"} $args{"replace"}
Truth table66%2/3
A and B
A= $args{"skip"}B= $args{"replace"}
ABresult
0X0
100
111
239 0 die("Cannot use both 'skip' and 'replace'");
240 }
241  
242 75224 ▶ if ($args{replace}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'replace'}) { } 75063 161 75224
elsif ($args{'skip'}) { } 1 160 161
243 75063 $self->remove_inputmap(xstype => $input->xstype);
244 }
245 elsif ($args{skip}) {
246 1 ▶ return() if exists $self->{input_lookup}{$input->xstype};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"input_lookup"}{$input->xstype} 0 1 1
247 }
248 else {
249 160 $self->validate(inputmap_xstype => $input->xstype);
250 }
251  
252 # store
253 75224 push @{$self->{input_section}}, $input;
254 # remember type for lookup, too.
255 75224 $self->{input_lookup}{$input->xstype} = $#{$self->{input_section}};
256  
257 75224 return 1;
258 }
259  
260 =head2 add_outputmap
261  
262 Add an C<OUTPUT> entry to the typemap.
263 Works exactly the same as C<add_inputmap>.
264  
265 =cut
266  
267 75317 ▶sub add_outputmap {
Subroutine add_outputmap: called CC=9 SCAR=23.3
Pod documented
268 75317 my $self = shift;
269 75317 my $output;
270 my %args;
271  
272 75317 ▶ if ((@_ % 2) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2 == 1) { } 75309 8 75317
273 75309 my $orig = shift;
274 75309 $output = $orig->new();
275 75309 %args = @_;
276 }
277 else {
278 8 %args = @_;
279 8 my $xstype = $args{xstype};
280 8 ▶ die("Need xstype argument") if not defined $xstype;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $xstype 0 8 8
281 8 my $code = $args{code};
282 8 ▶ die("Need code argument") if not defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 0 8 8
283  
284 8 $output = ExtUtils::Typemaps::OutputMap->new(
285 xstype => $xstype,
286 code => $code,
287 );
288 }
289  
290 75317 ▶ if ($args{skip} and $args{replace}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"skip"} and $args{"replace"}) 0 75317 75317
Condition33%1/3
$args{"skip"} and $args{"replace"}
!ll&&!rl&&r
7531700
MC/DC0%0/2
$args{"skip"} and $args{"replace"}
$args{"skip"} $args{"replace"}
Truth table33%1/3
A and B
A= $args{"skip"}B= $args{"replace"}
ABresult
0X0
100
111
291 0 die("Cannot use both 'skip' and 'replace'");
292 }
293  
294 75317 ▶ if ($args{replace}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'replace'}) { } 75104 213 75317
elsif ($args{'skip'}) { } 0 213 213
295 75104 $self->remove_outputmap(xstype => $output->xstype);
296 }
297 elsif ($args{skip}) {
298 0 ▶ return() if exists $self->{output_lookup}{$output->xstype};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"output_lookup"}{$output->xstype} 0 0 0
299 }
300 else {
301 213 $self->validate(outputmap_xstype => $output->xstype);
302 }
303  
304 # store
305 75317 push @{$self->{output_section}}, $output;
306 # remember type for lookup, too.
307 75317 $self->{output_lookup}{$output->xstype} = $#{$self->{output_section}};
308  
309 75317 return 1;
310 }
311  
312 =head2 add_string
313  
314 Parses a string as a typemap and merge it into the typemap object.
315  
316 Required named argument: C<string> to specify the string to parse.
317  
318 =cut
319  
320 2 ▶sub add_string {
Subroutine add_string: called CC=2 SCAR=7.0
Pod documented
321 2 my $self = shift;
322 2 my %args = @_;
323 2 ▶ die("Need 'string' argument") if not defined $args{string};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"string"} 0 2 2
324  
325 # no, this is not elegant.
326 2 my $other = ExtUtils::Typemaps->new(string => $args{string});
327 2 $self->merge(typemap => $other);
328 }
329  
330 =head2 remove_typemap
331  
332 Removes a C<TYPEMAP> entry from the typemap.
333  
334 Required named argument: C<ctype> to specify the entry to remove from the typemap.
335  
336 Alternatively, you may pass a single C<ExtUtils::Typemaps::Type> object.
337  
338 =cut
339  
340 89641 ▶sub remove_typemap {
Subroutine remove_typemap: called CC=3 SCAR=11.3
Pod documented
341 89641 my $self = shift;
342 89641 my $ctype;
343 89641 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) { } 89641 0 89641
344 89641 my %args = @_;
345 89641 $ctype = $args{ctype};
346 89641 ▶ die("Need ctype argument") if not defined $ctype;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ctype 0 89641 89641
347 89641 $ctype = tidy_type($ctype);
348 }
349 else {
350 0 $ctype = $_[0]->tidy_ctype;
351 }
352  
353 89641 return $self->_remove($ctype, $self->{typemap_section}, $self->{typemap_lookup});
354 }
355  
356 =head2 remove_inputmap
357  
358 Removes an C<INPUT> entry from the typemap.
359  
360 Required named argument: C<xstype> to specify the entry to remove from the typemap.
361  
362 Alternatively, you may pass a single C<ExtUtils::Typemaps::InputMap> object.
363  
364 =cut
365  
366 75063 ▶sub remove_inputmap {
Subroutine remove_inputmap: called CC=3 SCAR=11.4
Pod documented
367 75063 my $self = shift;
368 75063 my $xstype;
369 75063 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) { } 75063 0 75063
370 75063 my %args = @_;
371 75063 $xstype = $args{xstype};
372 75063 ▶ die("Need xstype argument") if not defined $xstype;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $xstype 0 75063 75063
373 }
374 else {
375 0 $xstype = $_[0]->xstype;
376 }
377
378 75063 return $self->_remove($xstype, $self->{input_section}, $self->{input_lookup});
379 }
380  
381 =head2 remove_outputmap
382  
383 Removes an C<OUTPUT> entry from the typemap.
384  
385 Required named argument: C<xstype> to specify the entry to remove from the typemap.
386  
387 Alternatively, you may pass a single C<ExtUtils::Typemaps::OutputMap> object.
388  
389 =cut
390  
391 75104 ▶sub remove_outputmap {
Subroutine remove_outputmap: called CC=3 SCAR=11.4
Pod documented
392 75104 my $self = shift;
393 75104 my $xstype;
394 75104 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) { } 75104 0 75104
395 75104 my %args = @_;
396 75104 $xstype = $args{xstype};
397 75104 ▶ die("Need xstype argument") if not defined $xstype;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $xstype 0 75104 75104
398 }
399 else {
400 0 $xstype = $_[0]->xstype;
401 }
402
403 75104 return $self->_remove($xstype, $self->{output_section}, $self->{output_lookup});
404 }
405  
406 239808 ▶sub _remove {
Subroutine _remove: called CC=4 SCAR=13.9
407 239808 my $self = shift;
408 239808 my $rm = shift;
409 239808 my $array = shift;
410 239808 my $lookup = shift;
411  
412 # Just fetch the index of the item from the lookup table
413 239808 my $index = $lookup->{$rm};
414 239808 ▶ return() if not defined $index;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 180294 59514 239808
415  
416 # Nuke the item from storage
417 59514 splice(@$array, $index, 1);
418  
419 # Decrement the storage position of all items thereafter
420 59514 foreach my $key (keys %$lookup) {
421 2737439 ▶ if ($lookup->{$key} > $index) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lookup->{$key} > $index) 2675853 61586 2737439
422 2675853 $lookup->{$key}--;
423 }
424 }
425 59514 return();
426 }
427  
428 =head2 get_typemap
429  
430 Fetches an entry of the TYPEMAP section of the typemap.
431  
432 Mandatory named arguments: The C<ctype> of the entry.
433  
434 Returns the C<ExtUtils::Typemaps::Type>
435 object for the entry if found.
436  
437 =cut
438  
439 1400 ▶sub get_typemap {
Subroutine get_typemap: called CC=4 SCAR=13.9
Pod documented
440 1400 my $self = shift;
441 1400 ▶ die("Need named parameters, got uneven number") if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 1 1399 1400
442  
443 1399 my %args = @_;
444 1399 my $ctype = $args{ctype};
445 1399 ▶ die("Need ctype argument") if not defined $ctype;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ctype 0 1399 1399
446 1399 $ctype = tidy_type($ctype);
447  
448 1399 my $index = $self->{typemap_lookup}{$ctype};
449 1399 ▶ return() if not defined $index;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 24 1375 1399
450 1375 return $self->{typemap_section}[$index];
451 }
452  
453 =head2 get_inputmap
454  
455 Fetches an entry of the INPUT section of the
456 typemap.
457  
458 Mandatory named arguments: The C<xstype> of the
459 entry or the C<ctype> of the typemap that can be used to find
460 the C<xstype>. To wit, the following pieces of code
461 are equivalent:
462  
463 my $type = $typemap->get_typemap(ctype => $ctype)
464 my $input_map = $typemap->get_inputmap(xstype => $type->xstype);
465  
466 my $input_map = $typemap->get_inputmap(ctype => $ctype);
467  
468 Returns the C<ExtUtils::Typemaps::InputMap>
469 object for the entry if found.
470  
471 =cut
472  
473 407 ▶sub get_inputmap {
Subroutine get_inputmap: called CC=10 SCAR=23.3
Pod documented
474 407 my $self = shift;
475 407 ▶ die("Need named parameters, got uneven number") if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 1 406 407
476  
477 406 my %args = @_;
478 406 my $xstype = $args{xstype};
479 406 my $ctype = $args{ctype};
480 406 ▶ die("Need xstype or ctype argument")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $xstype or defined $ctype 0 406 406
Condition66%2/3
defined $xstype or defined $ctype
l!l&&r!l&&!r
40420
MC/DC0%0/2
defined $xstype or defined $ctype
defined $xstype defined $ctype
Truth table66%2/3
A or B
A= defined $xstypeB= defined $ctype
ABresult
1X1
011
000
481 if not defined $xstype
482 and not defined $ctype;
483 406 ▶ die("Need xstype OR ctype arguments, not both")
Branch: 1 branch
BranchTrueFalseTotal
if defined $xstype and defined $ctype 0 406 406
Condition66%2/3
defined $xstype and defined $ctype
!ll&&!rl&&r
24040
MC/DC0%0/2
defined $xstype and defined $ctype
defined $xstype defined $ctype
Truth table66%2/3
A and B
A= defined $xstypeB= defined $ctype
ABresult
0X0
100
111
484 if defined $xstype and defined $ctype;
485  
486 406 ▶ if (defined $ctype) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ctype) 2 404 406
487 2 my $tm = $self->get_typemap(ctype => $ctype);
488 2 ▶ $xstype = $tm && $tm->xstype;
Condition66%2/3
$tm && $tm->xstype
!ll&&!rl&&r
101
MC/DC50%1/2
$tm && $tm->xstype
$tm $tm->xstype
Truth table66%2/3
A && B
A= $tmB= $tm->xstype
ABresult
0X0
100
111
489 2 ▶ return() if not defined $xstype;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $xstype 1 1 2
490 }
491  
492 405 my $index = $self->{input_lookup}{$xstype};
493 405 ▶ return() if not defined $index;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 5 400 405
494 400 return $self->{input_section}[$index];
495 }
496  
497 =head2 get_outputmap
498  
499 Fetches an entry of the OUTPUT section of the
500 typemap.
501  
502 Mandatory named arguments: The C<xstype> of the
503 entry or the C<ctype> of the typemap that can be used to
504 resolve the C<xstype>. (See above for an example.)
505  
506 Returns the C<ExtUtils::Typemaps::InputMap>
507 object for the entry if found.
508  
509 =cut
510  
511 561 ▶sub get_outputmap {
Subroutine get_outputmap: called CC=10 SCAR=23.3
Pod documented
512 561 my $self = shift;
513 561 ▶ die("Need named parameters, got uneven number") if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 1 560 561
514  
515 560 my %args = @_;
516 560 my $xstype = $args{xstype};
517 560 my $ctype = $args{ctype};
518 560 ▶ die("Need xstype or ctype argument")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $xstype or defined $ctype 0 560 560
Condition66%2/3
defined $xstype or defined $ctype
l!l&&r!l&&!r
3072530
MC/DC0%0/2
defined $xstype or defined $ctype
defined $xstype defined $ctype
Truth table66%2/3
A or B
A= defined $xstypeB= defined $ctype
ABresult
1X1
011
000
519 if not defined $xstype
520 and not defined $ctype;
521 560 ▶ die("Need xstype OR ctype arguments, not both")
Branch: 1 branch
BranchTrueFalseTotal
if defined $xstype and defined $ctype 0 560 560
Condition66%2/3
defined $xstype and defined $ctype
!ll&&!rl&&r
2533070
MC/DC0%0/2
defined $xstype and defined $ctype
defined $xstype defined $ctype
Truth table66%2/3
A and B
A= defined $xstypeB= defined $ctype
ABresult
0X0
100
111
522 if defined $xstype and defined $ctype;
523  
524 560 ▶ if (defined $ctype) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ctype) 253 307 560
525 253 my $tm = $self->get_typemap(ctype => $ctype);
526 253 ▶ $xstype = $tm && $tm->xstype;
Condition66%2/3
$tm && $tm->xstype
!ll&&!rl&&r
60247
MC/DC50%1/2
$tm && $tm->xstype
$tm $tm->xstype
Truth table66%2/3
A && B
A= $tmB= $tm->xstype
ABresult
0X0
100
111
527 253 ▶ return() if not defined $xstype;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $xstype 6 247 253
528 }
529  
530 554 my $index = $self->{output_lookup}{$xstype};
531 554 ▶ return() if not defined $index;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 8 546 554
532 546 return $self->{output_section}[$index];
533 }
534  
535 =head2 write
536  
537 Write the typemap to a file. Optionally takes a C<file> argument. If given, the
538 typemap will be written to the specified file. If not, the typemap is written
539 to the currently stored file name (see L</file> above, this defaults to the file
540 it was read from if any).
541  
542 =cut
543  
544 1 ▶sub write {
Subroutine write: called CC=4 SCAR=14.3
Pod documented
545 1 my $self = shift;
546 1 my %args = @_;
547 1 ▶ my $file = defined $args{file} ? $args{file} : $self->file();
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'file'} ? : 1 0 1
548 1 ▶ die("write() needs a file argument (or set the file name of the typemap using the 'file' method)")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 1 1
549 if not defined $file;
550  
551 1 ▶ open my $fh, '>', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file or die "Cannot open typemap file '${file}' for writing: $!" 1 0 1
552 or die "Cannot open typemap file '$file' for writing: $!";
553 1 print $fh $self->as_string();
554 1 close $fh;
555 }
556  
557 =head2 as_string
558  
559 Generates and returns the string form of the typemap.
560  
561 =cut
562  
563 24 ▶sub as_string {
Subroutine as_string: called CC=7 SCAR=19.5
Pod documented
564 24 my $self = shift;
565 24 my $typemap = $self->{typemap_section};
566 24 my @code;
567 24 push @code, "TYPEMAP\n";
568 24 foreach my $entry (@$typemap) {
569 # type kind proto
570 # /^(.*?\S)\s+(\S+)\s*($ExtUtils::ParseXS::Constants::PrototypeRegexp*)$/o
571 38 ▶ push @code, $entry->ctype . "\t" . $entry->xstype
Branch: 1 branch
BranchTrueFalseTotal
$entry->proto ne '' ? : 0 38 38
572 . ($entry->proto ne '' ? "\t".$entry->proto : '') . "\n";
573 }
574  
575 24 my $input = $self->{input_section};
576 24 ▶ if (@$input) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$input) 17 7 24
577 17 push @code, "\nINPUT\n";
578 17 foreach my $entry (@$input) {
579 33 push @code, $entry->xstype, "\n", $entry->code, "\n";
580 }
581 }
582  
583 24 my $output = $self->{output_section};
584 24 ▶ if (@$output) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$output) 15 9 24
585 15 push @code, "\nOUTPUT\n";
586 15 foreach my $entry (@$output) {
587 29 push @code, $entry->xstype, "\n", $entry->code, "\n";
588 }
589 }
590 24 return join '', @code;
591 }
592  
593 =head2 as_embedded_typemap
594  
595 Generates and returns the string form of the typemap with the
596 appropriate prefix around it for verbatim inclusion into an
597 XS file as an embedded typemap. This will return a string like
598  
599 TYPEMAP: <<END_OF_TYPEMAP
600 ... typemap here (see as_string) ...
601 END_OF_TYPEMAP
602  
603 The method takes care not to use a HERE-doc end marker that
604 appears in the typemap string itself.
605  
606 =cut
607  
608 6 ▶sub as_embedded_typemap {
Subroutine as_embedded_typemap: called CC=3 SCAR=13.2
Pod documented
609 6 my $self = shift;
610 6 my $string = $self->as_string;
611  
612 6 my @ident_cand = qw(END_TYPEMAP END_OF_TYPEMAP END);
613 6 my $icand = 0;
614 6 my $cand_suffix = "";
615 6 ▶ while ($string =~ /^\Q$ident_cand[$icand]$cand_suffix\E\s*$/m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^\Q$ident_cand[$icand]$cand_suffix\E\s*$/m) 0 6 6
616 0 $icand++;
617 0 ▶ if ($icand == @ident_cand) {
Branch: 1 branch
BranchTrueFalseTotal
if ($icand == @ident_cand) 0 0 0
618 0 $icand = 0;
619 0 ++$cand_suffix;
620 }
621 }
622  
623 6 my $marker = "$ident_cand[$icand]$cand_suffix";
624 6 return "TYPEMAP: <<$marker;\n$string\n$marker\n";
625 }
626  
627 =head2 merge
628  
629 Merges a given typemap into the object. Note that a failed merge
630 operation leaves the object in an inconsistent state so clone it if necessary.
631  
632 Mandatory named arguments: Either C<typemap =E<gt> $another_typemap_obj>
633 or C<file =E<gt> $path_to_typemap_file> but not both.
634  
635 Optional arguments: C<replace =E<gt> 1> to force replacement
636 of existing typemap entries without warning or C<skip =E<gt> 1>
637 to skip entries that exist already in the typemap.
638  
639 =cut
640  
641 1055 ▶sub merge {
Subroutine merge: called CC=11 SCAR=24.4
Pod documented
642 1055 my $self = shift;
643 1055 my %args = @_;
644  
645 1055 ▶ if (exists $args{typemap} and exists $args{file}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $args{'typemap'} and exists $args{'file'}) { } 0 1055 1055
elsif (not exists $args{'typemap'} || exists $args{'file'}) { } 0 1055 1055
Condition66%2/3
exists $args{"typemap"} and exists $args{"file"}
!ll&&!rl&&r
8791760
MC/DC0%0/2
exists $args{"typemap"} and exists $args{"file"}
exists $args{"typemap"} exists $args{"file"}
Truth table66%2/3
A and B
A= exists $args{"typemap"}B= exists $args{"file"}
ABresult
0X0
100
111
646 0 die("Need {file} OR {typemap} argument. Not both!");
647 }
648 ▶ elsif (not exists $args{typemap} and not exists $args{file}) {
Condition66%2/3
exists $args{'typemap'} || exists $args{'file'}
l!l&&r!l&&!r
1768790
MC/DC0%0/2
exists $args{'typemap'} || exists $args{'file'}
exists $args{'typemap'} exists $args{'file'}
Truth table66%2/3
A || B
A= exists $args{'typemap'}B= exists $args{'file'}
ABresult
1X1
011
000
649 0 die("Need {file} or {typemap} argument!");
650 }
651  
652 1055 my @params;
653 1055 ▶ push @params, 'replace' => $args{replace} if exists $args{replace};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"replace"} 1047 8 1055
654 1055 ▶ push @params, 'skip' => $args{skip} if exists $args{skip};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"skip"} 1 1054 1055
655  
656 1055 my $typemap = $args{typemap};
657 1055 ▶ if (not defined $typemap) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $typemap) 879 176 1055
658 879 $typemap = ref($self)->new(file => $args{file}, @params);
659 }
660  
661 # FIXME breaking encapsulation. Add accessor code.
662 1055 foreach my $entry (@{$typemap->{typemap_section}}) {
663 45097 $self->add_typemap( $entry, @params );
664 }
665  
666 1054 foreach my $entry (@{$typemap->{input_section}}) {
667 37597 $self->add_inputmap( $entry, @params );
668 }
669  
670 1054 foreach my $entry (@{$typemap->{output_section}}) {
671 37645 $self->add_outputmap( $entry, @params );
672 }
673  
674 1054 return 1;
675 }
676  
677 =head2 is_empty
678  
679 Returns a bool indicating whether this typemap is entirely empty.
680  
681 =cut
682  
683 5 ▶sub is_empty {
Subroutine is_empty: called CC=3 SCAR=11.0
Pod documented
684 5 my $self = shift;
685  
686 5 return @{ $self->{typemap_section} } == 0
687 3 && @{ $self->{input_section} } == 0
688 5 ▶ && @{ $self->{output_section} } == 0;
Condition100%6/6
@{$self->{'typemap_section'};} == 0 && @{$self->{'input_section'};} == 0 && @{$self->{'output_section'};} == 0
!ll&&!rl&&r
311
@{$self->{"typemap_section"};} == 0 and @{$self->{"input_section"};} == 0
!ll&&!rl&&r
212
MC/DC100%3/3
@{$self->{'typemap_section'};} == 0 && @{$self->{'input_section'};} == 0 && @{$self->{'output_section'};} == 0
@{$self->{"typemap_section"};} == 0 @{$self->{"input_section"};} == 0 @{$self->{'output_section'};} == 0
Truth table100%4/4
A and B && C
A= @{$self->{"typemap_section"};} == 0B= @{$self->{"input_section"};} == 0C= @{$self->{'output_section'};} == 0
ABCresult
0XX0
10X0
1100
1111
689 }
690  
691 =head2 list_mapped_ctypes
692  
693 Returns a list of the C types that are mappable by
694 this typemap object.
695  
696 =cut
697  
698 7 ▶sub list_mapped_ctypes {
Subroutine list_mapped_ctypes: called CC=1 SCAR=0.0
Pod documented
699 7 my $self = shift;
700 7 return sort keys %{ $self->{typemap_lookup} };
701 }
702  
703 =head2 _get_typemap_hash
704  
705 Returns a hash mapping the C types to the XS types:
706  
707 {
708 'char **' => 'T_PACKEDARRAY',
709 'bool_t' => 'T_IV',
710 'AV *' => 'T_AVREF',
711 'InputStream' => 'T_IN',
712 'double' => 'T_DOUBLE',
713 # ...
714 }
715  
716 This is documented because it is used by C<ExtUtils::ParseXS>,
717 but it's not intended for general consumption. May be removed
718 at any time.
719  
720 =cut
721  
722 4 ▶sub _get_typemap_hash {
Subroutine _get_typemap_hash: called CC=2 SCAR=6.9
723 4 my $self = shift;
724 4 my $lookup = $self->{typemap_lookup};
725 4 my $storage = $self->{typemap_section};
726  
727 4 my %rv;
728 4 foreach my $ctype (keys %$lookup) {
729 197 $rv{$ctype} = $storage->[ $lookup->{$ctype} ]->xstype;
730 }
731  
732 4 return \%rv;
733 }
734  
735 =head2 _get_inputmap_hash
736  
737 Returns a hash mapping the XS types (identifiers) to the
738 corresponding INPUT code:
739  
740 {
741 'T_CALLBACK' => ' $var = make_perl_cb_$type($arg)
742 ',
743 'T_OUT' => ' $var = IoOFP(sv_2io($arg))
744 ',
745 'T_REF_IV_PTR' => ' if (sv_isa($arg, \\"${ntype}\\")) {
746 # ...
747 }
748  
749 This is documented because it is used by C<ExtUtils::ParseXS>,
750 but it's not intended for general consumption. May be removed
751 at any time.
752  
753 =cut
754  
755 4 ▶sub _get_inputmap_hash {
Subroutine _get_inputmap_hash: called CC=2 SCAR=6.9
756 4 my $self = shift;
757 4 my $lookup = $self->{input_lookup};
758 4 my $storage = $self->{input_section};
759  
760 4 my %rv;
761 4 foreach my $xstype (keys %$lookup) {
762 97 $rv{$xstype} = $storage->[ $lookup->{$xstype} ]->code;
763  
764 # Squash trailing whitespace to one line break
765 # This isn't strictly necessary, but makes the output more similar
766 # to the original ExtUtils::ParseXS.
767 97 $rv{$xstype} =~ s/\s*\z/\n/;
768 }
769  
770 4 return \%rv;
771 }
772  
773  
774 =head2 _get_outputmap_hash
775  
776 Returns a hash mapping the XS types (identifiers) to the
777 corresponding OUTPUT code:
778  
779 {
780 'T_CALLBACK' => ' sv_setpvn($arg, $var.context.value().chp(),
781 $var.context.value().size());
782 ',
783 'T_OUT' => ' {
784 GV *gv = (GV *)sv_newmortal();
785 gv_init_pvn(gv, gv_stashpvs("$Package",1),
786 "__ANONIO__",10,0);
787 if ( do_open(gv, "+>&", 3, FALSE, 0, 0, $var) )
788 sv_setsv(
789 $arg,
790 sv_bless(newRV((SV*)gv), gv_stashpv("$Package",1))
791 );
792 else
793 $arg = &PL_sv_undef;
794 }
795 ',
796 # ...
797 }
798  
799 This is documented because it is used by C<ExtUtils::ParseXS>,
800 but it's not intended for general consumption. May be removed
801 at any time.
802  
803 =cut
804  
805 4 ▶sub _get_outputmap_hash {
Subroutine _get_outputmap_hash: called CC=2 SCAR=6.9
806 4 my $self = shift;
807 4 my $lookup = $self->{output_lookup};
808 4 my $storage = $self->{output_section};
809  
810 4 my %rv;
811 4 foreach my $xstype (keys %$lookup) {
812 92 $rv{$xstype} = $storage->[ $lookup->{$xstype} ]->code;
813  
814 # Squash trailing whitespace to one line break
815 # This isn't strictly necessary, but makes the output more similar
816 # to the original ExtUtils::ParseXS.
817 92 $rv{$xstype} =~ s/\s*\z/\n/;
818 }
819  
820 4 return \%rv;
821 }
822  
823 =head2 _get_prototype_hash
824  
825 Returns a hash mapping the C types of the typemap to their
826 corresponding prototypes.
827  
828 {
829 'char **' => '$',
830 'bool_t' => '$',
831 'AV *' => '$',
832 'InputStream' => '$',
833 'double' => '$',
834 # ...
835 }
836  
837 This is documented because it is used by C<ExtUtils::ParseXS>,
838 but it's not intended for general consumption. May be removed
839 at any time.
840  
841 =cut
842  
843 4 ▶sub _get_prototype_hash {
Subroutine _get_prototype_hash: called CC=3 SCAR=11.0
844 4 my $self = shift;
845 4 my $lookup = $self->{typemap_lookup};
846 4 my $storage = $self->{typemap_section};
847  
848 4 my %rv;
849 4 foreach my $ctype (keys %$lookup) {
850 197 ▶ $rv{$ctype} = $storage->[ $lookup->{$ctype} ]->proto || '$';
Condition50%1/2
$$storage[$lookup->{$ctype}]->proto || '$'
l!l
0197
MC/DC0%0/1
$$storage[$lookup->{$ctype}]->proto || '$'
$$storage[$lookup->{$ctype}]->proto
Truth table50%1/2
A
A= $$storage[$lookup->{$ctype}]->proto
Aresult
11
00
851 }
852  
853 4 return \%rv;
854 }
855  
856  
857  
858 # make sure that the provided types wouldn't collide with what's
859 # in the object already.
860 959 ▶sub validate {
Subroutine validate: called CC=7 SCAR=20.5
Pod undocumented
861 959 my $self = shift;
862 959 my %args = @_;
863  
864 959 ▶ if ( exists $args{ctype}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"ctype"} and exists $self->{"typemap_lookup"}{tidy_type($args{"ctype"})}) 1 958 959
Condition100%3/3
exists $args{"ctype"} and exists $self->{"typemap_lookup"}{tidy_type($args{"ctype"})}
!ll&&!rl&&r
3735851
MC/DC100%2/2
exists $args{"ctype"} and exists $self->{"typemap_lookup"}{tidy_type($args{"ctype"})}
exists $args{"ctype"} exists $self->{"typemap_lookup"}{tidy_type($args{"ctype"})}
Truth table100%3/3
A and B
A= exists $args{"ctype"}B= exists $self->{"typemap_lookup"}{tidy_type($args{"ctype"})}
ABresult
0X0
100
111
865 and exists $self->{typemap_lookup}{tidy_type($args{ctype})} )
866 {
867 1 die("Multiple definition of ctype '$args{ctype}' in TYPEMAP section");
868 }
869  
870 958 ▶ if ( exists $args{inputmap_xstype}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"inputmap_xstype"} and exists $self->{"input_lookup"}{$args{"inputmap_xstype"}}) 0 958 958
Condition66%2/3
exists $args{"inputmap_xstype"} and exists $self->{"input_lookup"}{$args{"inputmap_xstype"}}
!ll&&!rl&&r
7981600
MC/DC0%0/2
exists $args{"inputmap_xstype"} and exists $self->{"input_lookup"}{$args{"inputmap_xstype"}}
exists $args{"inputmap_xstype"} exists $self->{"input_lookup"}{$args{"inputmap_xstype"}}
Truth table66%2/3
A and B
A= exists $args{"inputmap_xstype"}B= exists $self->{"input_lookup"}{$args{"inputmap_xstype"}}
ABresult
0X0
100
111
871 and exists $self->{input_lookup}{$args{inputmap_xstype}} )
872 {
873 0 die("Multiple definition of xstype '$args{inputmap_xstype}' in INPUTMAP section");
874 }
875  
876 958 ▶ if ( exists $args{outputmap_xstype}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"outputmap_xstype"} and exists $self->{"output_lookup"}{$args{"outputmap_xstype"}}) 0 958 958
Condition66%2/3
exists $args{"outputmap_xstype"} and exists $self->{"output_lookup"}{$args{"outputmap_xstype"}}
!ll&&!rl&&r
7452130
MC/DC0%0/2
exists $args{"outputmap_xstype"} and exists $self->{"output_lookup"}{$args{"outputmap_xstype"}}
exists $args{"outputmap_xstype"} exists $self->{"output_lookup"}{$args{"outputmap_xstype"}}
Truth table66%2/3
A and B
A= exists $args{"outputmap_xstype"}B= exists $self->{"output_lookup"}{$args{"outputmap_xstype"}}
ABresult
0X0
100
111
877 and exists $self->{output_lookup}{$args{outputmap_xstype}} )
878 {
879 0 die("Multiple definition of xstype '$args{outputmap_xstype}' in OUTPUTMAP section");
880 }
881  
882 958 return 1;
883 }
884  
885 =head2 clone
886  
887 Creates and returns a clone of a full typemaps object.
888  
889 Takes named parameters: If C<shallow> is true,
890 the clone will share the actual individual type/input/outputmap objects,
891 but not share their storage. Use with caution. Without C<shallow>,
892 the clone will be fully independent.
893  
894 =cut
895  
896 2 ▶sub clone {
Subroutine clone: called CC=2 SCAR=6.9
Pod documented
897 2 my $proto = shift;
898 2 my %args = @_;
899  
900 2 my $self;
901 2 ▶ if ($args{shallow}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'shallow'}) { } 1 1 2
902 $self = bless( {
903 %$proto,
904 1 typemap_section => [@{$proto->{typemap_section}}],
905 1 typemap_lookup => {%{$proto->{typemap_lookup}}},
906 1 input_section => [@{$proto->{input_section}}],
907 1 input_lookup => {%{$proto->{input_lookup}}},
908 1 output_section => [@{$proto->{output_section}}],
909 1 output_lookup => {%{$proto->{output_lookup}}},
910 } => ref($proto) );
911 }
912 else {
913 $self = bless( {
914 %$proto,
915 1 typemap_section => [map $_->new, @{$proto->{typemap_section}}],
916 1 typemap_lookup => {%{$proto->{typemap_lookup}}},
917 1 input_section => [map $_->new, @{$proto->{input_section}}],
918 1 input_lookup => {%{$proto->{input_lookup}}},
919 1 output_section => [map $_->new, @{$proto->{output_section}}],
920 1 output_lookup => {%{$proto->{output_lookup}}},
921 } => ref($proto) );
922 }
923  
924 2 return $self;
925 }
926  
927 =head2 tidy_type
928  
929 Function to (heuristically) canonicalize a C type in terms of white space.
930 Works to some degree with C++ types. For example,
931  
932 $halfway_canonical_type = tidy_type(' int * * const * ');
933  
934 returns C<'int ** const *'>.
935  
936 Moved from C<ExtUtils::ParseXS>.
937  
938 =cut
939  
940 229744 ▶sub tidy_type {
Subroutine tidy_type: called CC=2 SCAR=6.9
Pod documented
941 229744 local $_ = shift;
942  
943 # for templated C++ types, do some bit of flawed canonicalization
944 # wrt. templates at least
945 229744 ▶ if (/[<>]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/[<>]/) 4 229740 229744
946 4 s/\s*([<>])\s*/$1/g;
947 4 s/>>/> >/g;
948 }
949  
950 # rationalise any '*' by joining them into bunches and removing whitespace
951 229744 s#\s*(\*+)\s*#$1#g;
952 229744 s#(\*+)# $1 #g ;
953  
954 # trim leading & trailing whitespace
955 229744 s/^\s+//; s/\s+$//;
956  
957 # change multiple whitespace into a single space
958 229744 s/\s+/ /g;
959  
960 229744 $_;
961 }
962  
963  
964  
965 1064 ▶sub _parse {
Subroutine _parse: called CC=25 SCAR=32.2
966 1064 my $self = shift;
967 1064 my $stringref = shift;
968 1064 my $lineno_offset = shift;
969 1064 ▶ $lineno_offset = 0 if not defined $lineno_offset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lineno_offset 896 168 1064
970 1064 my $filename = shift;
971 1064 ▶ $filename = '<string>' if not defined $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filename 3 1061 1064
972  
973 1064 my $replace = $self->{replace};
974 1064 my $skip = $self->{skip};
975 1064 ▶ die "Can only replace OR skip" if $replace and $skip;
Branch: 1 branch
BranchTrueFalseTotal
if $replace and $skip 0 1064 1064
Condition66%2/3
$replace and $skip
!ll&&!rl&&r
1868780
MC/DC0%0/2
$replace and $skip
$replace $skip
Truth table66%2/3
A and B
A= $replaceB= $skip
ABresult
0X0
100
111
976 1064 my @add_params;
977 1064 ▶ push @add_params, replace => 1 if $replace;
Branch: 1 branch
BranchTrueFalseTotal
if $replace 878 186 1064
978 1064 ▶ push @add_params, skip => 1 if $skip;
Branch: 1 branch
BranchTrueFalseTotal
if $skip 0 1064 1064
979  
980 # TODO comments should round-trip, currently ignoring
981 # TODO order of sections, multiple sections of same type
982 # Heavily influenced by ExtUtils::ParseXS
983 1064 my $section = 'typemap';
984 1064 my $lineno = $lineno_offset;
985 1064 my $junk = "";
986 1064 my $current = \$junk;
987 1064 my @input_expr;
988 my @output_expr;
989 1064 ▶ while ($$stringref =~ /^(.*)$/gcm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$stringref =~ /^(.*)$/cgm) 417575 1064 418639
990 417575 local $_ = $1;
991 417575 ++$lineno;
992 417575 chomp;
993 417575 ▶ next if /^\s*#/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*#/ 12174 405401 417575
994 405401 ▶ if (/^INPUT\s*$/) {
Branch: 3 branches
BranchTrueFalseTotal
if (/^INPUT\s*$/) { } 1002 404399 405401
elsif (/^OUTPUT\s*$/) { } 1004 403395 404399
elsif (/^TYPEMAP\s*$/) { } 24 403371 403395
995 1002 $section = 'input';
996 1002 $current = \$junk;
997 1002 next;
998 }
999 elsif (/^OUTPUT\s*$/) {
1000 1004 $section = 'output';
1001 1004 $current = \$junk;
1002 1004 next;
1003 }
1004 elsif (/^TYPEMAP\s*$/) {
1005 24 $section = 'typemap';
1006 24 $current = \$junk;
1007 24 next;
1008 }
1009
1010 403371 ▶ if ($section eq 'typemap') {
Branch: 4 branches
BranchTrueFalseTotal
if ($section eq 'typemap') { } 48877 354494 403371
elsif (/^\s/) { } 279005 75489 354494
elsif ($_ eq '') { } 208 75281 75489
elsif ($section eq 'input') { } 37617 37664 75281
1011 48877 my $line = $_;
1012 48877 s/^\s+//; s/\s+$//;
1013 48877 ▶ next if $_ eq '' or /^#/;
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "" or /^#/ 3763 45114 48877
Condition66%2/3
$_ eq "" or /^#/
l!l&&r!l&&!r
3763045114
MC/DC50%1/2
$_ eq "" or /^#/
$_ eq "" /^#/
Truth table66%2/3
A or B
A= $_ eq ""B= /^#/
ABresult
1X1
011
000
1014 45114 ▶ my($type, $kind, $proto) = /^(.*?\S)\s+(\S+)\s*($ExtUtils::ParseXS::Constants::PrototypeRegexp*)$/o
Branch: 1 branch
BranchTrueFalseTotal
() = /^(.*?\S)\s+(\S+)\s*($ExtUtils::ParseXS::Constants::PrototypeRegexp*)$/o or warn("Warning: File '${filename}' Line $lineno '${line}' TYPEMAP entry needs 2 or 3 columns\n"), next 45114 0 45114
1015 or warn("Warning: File '$filename' Line $lineno '$line' TYPEMAP entry needs 2 or 3 columns\n"),
1016 next;
1017  
1018 45114 ▶ if (length($proto)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $proto) 110 45004 45114
1019 110 ▶ warn("Warning: File '$filename' Line $lineno '$line' Invalid prototype '$proto'\n")
Branch: 1 branch
BranchTrueFalseTotal
unless _valid_proto_string($proto) 0 110 110
1020 unless _valid_proto_string($proto);
1021 }
1022 $self->add_typemap(
1023 45114 ▶ ExtUtils::Typemaps::Type->new(
Branch: 1 branch
BranchTrueFalseTotal
length $proto ? : 110 45004 45114
1024 xstype => $kind,
1025 (length($proto) ? (prototype => $proto) : ()),
1026 ctype => $type
1027 ),
1028 @add_params
1029 );
1030 } elsif (/^\s/) {
1031 279005 s/\s+$//;
1032 279005 ▶ $$current .= $$current eq '' ? $_ : "\n".$_;
Branch: 1 branch
BranchTrueFalseTotal
$$current eq '' ? : 75281 203724 279005
1033 } elsif ($_ eq '') {
1034 208 next;
1035 } elsif ($section eq 'input') {
1036 37617 s/\s+$//;
1037 37617 push @input_expr, {xstype => $_, code => ''};
1038 37617 $current = \$input_expr[-1]{code};
1039 } else { # output section
1040 37664 s/\s+$//;
1041 37664 push @output_expr, {xstype => $_, code => ''};
1042 37664 $current = \$output_expr[-1]{code};
1043 }
1044  
1045 } # end while lines
1046  
1047 1064 foreach my $inexpr (@input_expr) {
1048 37617 $self->add_inputmap( ExtUtils::Typemaps::InputMap->new(%$inexpr), @add_params );
1049 }
1050 1064 foreach my $outexpr (@output_expr) {
1051 37664 $self->add_outputmap( ExtUtils::Typemaps::OutputMap->new(%$outexpr), @add_params );
1052 }
1053  
1054 1064 return 1;
1055 }
1056  
1057 # taken from ExtUtils::ParseXS
1058 110 ▶sub _valid_proto_string {
Subroutine _valid_proto_string: called CC=2 SCAR=7.6
1059 110 my $string = shift;
1060 110 ▶ if ($string =~ /^$ExtUtils::ParseXS::Constants::PrototypeRegexp+$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^$ExtUtils::ParseXS::Constants::PrototypeRegexp+$/o) 110 0 110
1061 110 return $string;
1062 }
1063  
1064 0 return 0 ;
1065 }
1066  
1067 # taken from ExtUtils::ParseXS (C_string)
1068 0 ▶sub _escape_backslashes {
Subroutine _escape_backslashes: not called CC=1 SCAR=6.9
1069 0 my $string = shift;
1070 0 $string =~ s[\\][\\\\]g;
1071 0 $string;
1072 }
1073  
1074 =head1 CAVEATS
1075  
1076 Inherits some evil code from C<ExtUtils::ParseXS>.
1077  
1078 =head1 SEE ALSO
1079  
1080 The parser is heavily inspired from the one in L<ExtUtils::ParseXS>.
1081  
1082 For details on typemaps: L<perlxstut>, L<perlxs>.
1083  
1084 =head1 AUTHOR
1085  
1086 Steffen Mueller C<<smueller@cpan.org>>
1087  
1088 =head1 COPYRIGHT & LICENSE
1089  
1090 Copyright 2009, 2010, 2011, 2012, 2013 Steffen Mueller
1091  
1092 This program is free software; you can redistribute it and/or
1093 modify it under the same terms as Perl itself.
1094  
1095 =cut
1096  
1097 24 1;
1098  
« blib/lib/ExtUtils/ParseXS/Node.pm ↑ Summary blib/lib/ExtUtils/Typemaps/Type.pm »