blib/lib/ExtUtils/ParseXS/Node.pm

filter: Statementstmt 97.2% 1808 / 1860 Branchbran 88.5% 728 / 822 Conditioncond 86.4% 294 / 340 MC/DCmcdc 74.8% 143 / 191 Subroutinesub 98.8% 172 / 174 Podpod 0.0% 0 / 7 totaltotal 92.6% 3145 / 3394 CC 569 SCAR 27.4
CC
569
Cov
94%
as_concise
156.0
as_output_code
34.1
parse
28.7
CRAP 15.5
« blib/lib/ExtUtils/ParseXS.pm ↑ Summary blib/lib/ExtUtils/Typemaps.pm »
1 package ExtUtils::ParseXS::Node;
2 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 28 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 28 our $VERSION = '3.64';
7  
8 =head1 NAME
9  
10 ExtUtils::ParseXS::Node - Classes for nodes of an Abstract Syntax Tree
11  
12 =head1 SYNOPSIS
13  
14 # Create a node to represent the Foo part of an XS file; then
15 # top-down parse it into a subtree; then top-down emit the
16 # contents of the subtree as C code.
17  
18 my $foo = ExtUtils::ParseXS::Node::Foo->new();
19 $foo->parse(...)
20 or die;
21 $foo->as_code(...);
22 print STDERR $foo->as_concise(1); # for debugging
23  
24 =head1 DESCRIPTION
25  
26 This API is currently private and subject to change.
27  
28 The C<ExtUtils::ParseXS::Node> class, and its various subclasses, hold the
29 state for the nodes of an Abstract Syntax Tree (AST), which represents the
30 parsed state of an XS file.
31  
32 Each node is a hash of fields. Which field names are legal varies by the
33 node type. The hash keys and values can be accessed directly: there are no
34 getter/setter methods.
35  
36 Each node may have a C<kids> field which points to an array of all the
37 children of that node: this is what provides the tree structure. In
38 addition, some of those kids may also have direct links from fields for
39 quick access. For example, the C<xsub_decl> child object of an C<xsub>
40 object can be accessed in either of these ways:
41  
42 $xsub_object->{kids}[0]
43 $xsub_object->{decl}
44  
45 Most object-valued node fields within a tree point only to their direct
46 children; however, both C<INPUT_line> and C<OUTPUT_line> have an
47 C<ioparam> field which points to the C<IO_Param> object associated with
48 this line, which is located elsewhere in the tree.
49  
50 The various C<foo_part> nodes divide the parsing of the main body of an
51 XSUB into sections where different sets of keywords are allowable, and
52 where various bits of code can be conveniently emitted.
53  
54 =head2 Methods
55  
56 There are two main methods in addition to C<new()>, which are present in
57 all subclasses. First, C<parse()> consumes lines from the source to
58 satisfy the construct being parsed. It may itself create objects of
59 lower-level constructs and call parse on them. For example,
60 C<Node::xbody::parse()> may create a C<Node::input_part> node and call
61 C<parse()> on it, which will create C<Node::INPUT> or C<Node::PREINIT>
62 nodes as appropriate, and so on.
63  
64 Secondly, C<as_code()> descends its sub-tree, outputting the tree as C
65 code.
66  
67 The C<as_concise()> method returns a line-per-node string representation
68 of the node and any children. Most node classes just inherit this method
69 from the base C<Node> class. It is intended mainly for debugging.
70  
71 Some nodes also have an C<as_boot_code()> method for adding any code to
72 the boot XSUB. This returns two array refs, one containing a list of code
73 lines to be inserted early into the boot XSUB, and a second for later
74 lines.
75  
76 Finally, in the IO_Param subclass, C<as_code()> is replaced with
77 C<as_input_code> and C<as_output_code()>, since that node may need to
78 generate I<two> sets of C code; one to assign a Perl argument to a C
79 variable, and the other to return the value of a variable to Perl.
80  
81 Note that parsing and code-generation are done as two separate phases;
82 C<parse()> should only build a tree and never emit code.
83  
84 In addition to C<$self>, methods may commonly have some of these
85 parameters:
86  
87 =over
88  
89 =item C<$pxs>
90  
91 An C<ExtUtils::ParseXS> object which contains the overall processing
92 state. In particular, it has warning and croaking methods, and holds the
93 lines read in from the source file for the current paragraph.
94  
95 =item C<$xsub>
96  
97 For nodes related to parsing an XSUB, the current
98 C<ExtUtils::ParseXS::xsub> node being processed.
99  
100 =item C<$xbody>
101  
102 For nodes related to parsing an XSUB, the current
103 C<ExtUtils::ParseXS::xbody> node being processed. Note that in the
104 presence of a C<CASE> keyword, an XSUB can have multiple bodies.
105  
106 =back
107  
108 The C<parse()> and C<as_code()> methods for some subclasses may have
109 parameters in addition to those.
110  
111 Some subclasses may also have additional helper methods.
112  
113 =head2 Class Hierachy
114  
115 C<Node> and its sub-classes form the following inheritance hierarchy.
116 Various abstract classes are used by concrete subclasses where the
117 processing and/or fields are similar: for example, C<CODE>, C<PPCODE> etc
118 all consume a block of uninterpreted lines from the source file until the
119 next keyword, and emit that code, possibly wrapped in C<#line> directives.
120 This common behaviour is provided by the C<codeblock> class.
121  
122 Node
123 XS_file
124 preamble
125 C_part
126 C_part_POD
127 C_part_code
128 C_part_postamble
129 cpp_scope
130 global_cpp_line
131 BOOT
132 TYPEMAP
133 pre_boot
134 boot_xsub
135 xsub
136 xsub_decl
137 ReturnType
138 Param
139 IO_Param
140 Params
141 xbody
142 input_part
143 init_part
144 code_part
145 output_part
146 cleanup_part
147 autocall
148 oneline
149 MODULE
150 REQUIRE
151 FALLBACK
152 include
153 INCLUDE
154 INCLUDE_COMMAND
155 NOT_IMPLEMENTED_YET
156 CASE
157 enable
158 EXPORT_XSUB_SYMBOLS
159 PROTOTYPES
160 SCOPE
161 VERSIONCHECK
162 multiline
163 multiline_merged
164 C_ARGS
165 INTERFACE
166 INTERFACE_MACRO
167 OVERLOAD
168 ATTRS
169 PROTOTYPE
170 codeblock
171 CODE
172 CLEANUP
173 INIT
174 POSTCALL
175 PPCODE
176 PREINIT
177 keylines
178 ALIAS
179 INPUT
180 OUTPUT
181 keyline
182 ALIAS_line
183 INPUT_line
184 OUTPUT_line
185  
186  
187 =head2 Abstract Syntax Tree structure
188  
189 A typical XS file might compile to a tree with a node structure similar to
190 the following. Note that this is unrelated to the inheritance hierarchy
191 shown above. In this example, the XS file includes another file, and has a
192 couple of XSUBs within a C<#if/#else/#endif>. Note that a C<cpp_scope>
193 node is the parent of all the nodes within the same branch of an C<#if>,
194 or in the absence of C<#if>, within the same file.
195  
196 XS_file
197 preamble
198 C_part
199 C_part_POD
200 C_part_code
201 C_part_postamble
202 cpp_scope: type="main"
203 MODULE
204 PROTOTYPES
205 BOOT
206 TYPEMAP
207 INCLUDE
208 cpp_scope: type="include"
209 xsub
210 ...
211 global_cpp_line: directive="ifdef"
212 cpp_scope: type="if"
213 xsub
214 ...
215 global_cpp_line: directive="else"
216 cpp_scope: type="if"
217 xsub
218 ...
219 global_cpp_line: directive="endif"
220 xsub
221 ...
222 pre_boot
223 boot_xsub
224  
225 A typical XSUB might compile to a tree with a structure similar to the
226 following.
227  
228 xsub
229 xsub_decl
230 ReturnType
231 Params
232 Param
233 Param
234 ...
235 CASE # for when a CASE keyword being present implies multiple
236 # bodies; otherwise, just a bare xbody node.
237 xbody
238 # per-body copy of declaration Params, augmented by
239 # data from INPUT and OUTPUT sections
240 Params
241 IO_Param
242 IO_Param
243 ...
244 input_part
245 INPUT
246 INPUT_line
247 INPUT_line
248 ...
249 PREINIT
250 init_part
251 INIT
252 code_part
253 CODE
254 output_part
255 OUTPUT
256 OUTPUT_line
257 OUTPUT_line
258 ...
259 POSTCALL
260 cleanup_part
261 CLEANUP
262 CASE
263 xbody
264 ...
265  
266 =cut
267  
268 # store these in variables to hide them from brace-matching text editors
269 28 my $open_brace = '{';
270 28 my $close_brace = '}';
271  
272 # values for parse_keywords() flags
273 # (Can't assume 'constant.pm' is present yet)
274  
275 28 my $keywords_flag_MODULE = 1;
276 28 my $keywords_flag_NOT_IMPLEMENTED_YET = 2;
277  
278 # Utility sub to handle all the boilerplate of declaring a Node subclass,
279 # including setting up @INC and @FIELDS. Intended to be called from within
280 # BEGIN. (Created as a lexical sub ref to make it easily accessible to
281 # all subclasses in this file.)
282 #
283 # The first two args can optionally be ('-parent', 'Foo'), in which case
284 # the parent of this subclass will be ExtUtils::ParseXS::Node::Foo.
285 # If not specified, the parent will be ExtUtils::ParseXS::Node.
286 #
287 # Any remaining args are the names of fields. It also inherits the fields
288 # of its parent.
289  
290 28 my $USING_FIELDS;
291  
292 my $build_subclass;
293 28 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
294 1736 ▶ $build_subclass = sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
295 1736 my (@fields) = @_;
296  
297 1736 my $parent = 'ExtUtils::ParseXS::Node';
298 1736 ▶ if (@fields and $fields[0] eq '-parent') {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields and $fields[0] eq "-parent") 952 784 1736
Condition100%3/3
@fields and $fields[0] eq "-parent"
!ll&&!rl&&r
224560952
MC/DC100%2/2
@fields and $fields[0] eq "-parent"
@fields $fields[0] eq "-parent"
Truth table100%3/3
A and B
A= @fieldsB= $fields[0] eq "-parent"
ABresult
0X0
100
111
299 952 shift @fields;
300 952 my $p = shift @fields;
301 952 $parent .= "::$p";
302 }
303  
304 1736 my @bad = grep !/^\w+$/, @fields;
305 1736 ▶ die "Internal error: bad field name(s) in build_subclass: (@bad)\n"
Branch: 1 branch
BranchTrueFalseTotal
if @bad 0 1736 1736
306 if @bad;
307  
308 28 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
309  
310 1736 my $class = caller(0);
311 1736 @fields = (@{"${parent}::FIELDS"}, @fields);
312 1736 @{"${class}::ISA"} = $parent;
313 1736 @{"${class}::FIELDS"} = @fields;
314  
315 1736 ▶ if ($USING_FIELDS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($USING_FIELDS) 1736 0 1736
316 1736 ▶ eval qq{package $class; fields->import(\@fields); 1;}
Branch: 1 branch
BranchTrueFalseTotal
eval "package $class; fields->import(\@fields); 1;" or die $@ 1736 0 1736
317 or die $@;
318 }
319 28 };
320 };
321  
322  
323 # ======================================================================
324  
325 package ExtUtils::ParseXS::Node;
326  
327 # Base class for all the other node types.
328 #
329 # The 'use fields' enables compile-time or run-time errors if code
330 # attempts to use a key which isn't listed here.
331  
332 28 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.0
333 28 our @FIELDS = (
334 'line_no', # line number and ...
335 'file', # ... filename where this node appeared in src
336 'kids', # child nodes, if any
337 );
338  
339 # do 'use fields', except: fields needs Hash::Util which is XS, which
340 # needs us. So only 'use fields' on systems where Hash::Util has already
341 # been built.
342 28 ▶ if (eval 'require Hash::Util; 1;') {
Branch: 1 branch
BranchTrueFalseTotal
if (eval "require Hash::Util; 1;") 28 0 28
343 28 require fields;
344 28 $USING_FIELDS = 1;
345 28 fields->import(@FIELDS);
346 }
347 }
348  
349  
350 # new(): takes one optional arg, $args, which is a hash ref of key/value
351 # pairs to initialise the object with.
352  
353 12304 ▶sub new {
Subroutine new: called CC=3 SCAR=11.1
Pod undocumented
354 12304 my ($class, $args) = @_;
355 12304 ▶ $args = {} unless defined $args;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args 10285 2019 12304
356  
357 12304 my __PACKAGE__ $self = shift;
358  
359 12304 ▶ if ($USING_FIELDS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($USING_FIELDS) { } 12304 0 12304
360 12304 $self = fields::new($class);
361 12304 %$self = %$args;
362 }
363 else {
364 0 $self = bless { %$args } => $class;
365  
366 }
367 12304 return $self;
368 }
369  
370  
371 # A very generic parse method that just notes the current file/line no.
372 # Typically called first as a SUPER by the parse() method of real nodes.
373  
374 9002 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
Pod undocumented
375 9002 my __PACKAGE__ $self = shift;
376 9002 my ExtUtils::ParseXS $pxs = shift;
377  
378 9002 $self->{file} = $pxs->{in_pathname};
379 # account for the line array getting shifted
380 # as input lines are consumed, while line_no
381 # array isn't ever shifted
382 $self->{line_no} = $pxs->{line_no}->[
383 9002 @{$pxs->{line_no}} - @{$pxs->{line}}
384 ];
385 9002 1;
386 }
387  
388  
389 # Repeatedly look for keywords matching the pattern. For each found
390 # keyword, parse the text following them, and add any resultant nodes
391 # as kids to the current node. Returns a list of the successfully parsed
392 # and added kids.
393 # If $max is defined, it specifies the maximum number of keywords to
394 # process. This value is typically passed as undef (unlimited) or 1
395 # (just grab the next keyword).
396 # $flags can contain $keywords_flag_MODULE or
397 # keywords_flag_NOT_IMPLEMENTED_YET to indicate to match one of those
398 # keywords too (whose syntax is slightly different from 'KEY:' and
399 # so need special handling
400  
401 4828 ▶sub parse_keywords {
Subroutine parse_keywords: called CC=12 SCAR=24.9
Pod undocumented
402 4828 my __PACKAGE__ $self = shift;
403 4828 my ExtUtils::ParseXS $pxs = shift;
404 4828 my $xsub = shift;
405 4828 my $xbody = shift;
406 4828 my $max = shift; # max number of keywords to process
407 4828 my $pat = shift;
408 4828 my $flags = shift;
409  
410 4828 ▶ $flags = 0 unless defined $flags;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flags 2812 2016 4828
411  
412 4828 my $n = 0;
413 4828 my @kids;
414 4828 ▶ while (@{$pxs->{line}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$pxs->{"line"};}) 2570 3496 6066
415 2570 my $line = shift @{$pxs->{line}};
416 2570 ▶ next unless $line =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\S/ 1 2569 2570
417  
418 # extract/delete recognised keyword and any following text
419 2569 my $keyword;
420  
421 2569 ▶ if ( ($flags & $keywords_flag_MODULE)
Branch: 2 branches
BranchTrueFalseTotal
if ($flags & $keywords_flag_MODULE and ExtUtils::ParseXS::Utilities::looks_like_MODULE_line($line)) { } 441 2128 2569
elsif ($line =~ s/^(\s*)($pat)\s*:\s*(?:#.*)?/$1/s or $flags & $keywords_flag_NOT_IMPLEMENTED_YET and $line =~ s/^(\s*)(NOT_IMPLEMENTED_YET)/$1/) { } 985 1143 2128
Condition100%3/3
$flags & $keywords_flag_MODULE and ExtUtils::ParseXS::Utilities::looks_like_MODULE_line($line)
!ll&&!rl&&r
10191109441
MC/DC100%2/2
$flags & $keywords_flag_MODULE and ExtUtils::ParseXS::Utilities::looks_like_MODULE_line($line)
$flags & $keywords_flag_MODULE ExtUtils::ParseXS::Utilities::looks_like_MODULE_line($line)
Truth table100%3/3
A and B
A= $flags & $keywords_flag_MODULEB= ExtUtils::ParseXS::Utilities::looks_like_MODULE_line($line)
ABresult
0X0
100
111
422 && ExtUtils::ParseXS::Utilities::looks_like_MODULE_line($line)
423 )
424 {
425 441 $keyword = 'MODULE';
426 }
427 ▶ elsif ( $line =~ s/^(\s*)($pat)\s*:\s*(?:#.*)?/$1/s
Condition100%6/6
$line =~ s/^(\s*)($pat)\s*:\s*(?:#.*)?/$1/s or $flags & $keywords_flag_NOT_IMPLEMENTED_YET and $line =~ s/^(\s*)(NOT_IMPLEMENTED_YET)/$1/
l!l&&r!l&&!r
98321143
$flags & $keywords_flag_NOT_IMPLEMENTED_YET and $line =~ s/^(\s*)(NOT_IMPLEMENTED_YET)/$1/
!ll&&!rl&&r
1105382
MC/DC100%3/3
$line =~ s/^(\s*)($pat)\s*:\s*(?:#.*)?/$1/s or $flags & $keywords_flag_NOT_IMPLEMENTED_YET and $line =~ s/^(\s*)(NOT_IMPLEMENTED_YET)/$1/
$line =~ s/^(\s*)($pat)\s*:\s*(?:#.*)?/$1/s $flags & $keywords_flag_NOT_IMPLEMENTED_YET $line =~ s/^(\s*)(NOT_IMPLEMENTED_YET)/$1/
Truth table100%4/4
A or B and C
A= $line =~ s/^(\s*)($pat)\s*:\s*(?:#.*)?/$1/sB= $flags & $keywords_flag_NOT_IMPLEMENTED_YETC= $line =~ s/^(\s*)(NOT_IMPLEMENTED_YET)/$1/
ABCresult
1XX1
0111
00X0
0100
428 or ( ($flags & $keywords_flag_NOT_IMPLEMENTED_YET)
429 && $line =~ s/^(\s*)(NOT_IMPLEMENTED_YET)/$1/
430 )
431 )
432 {
433 985 $keyword = $2
434 }
435 else {
436 # stop at unrecognised line
437 1143 unshift @{$pxs->{line}}, $line;
438 1143 last;
439 }
440  
441 1426 unshift @{$pxs->{line}}, $line;
442 # create a node for the keyword and parse any lines associated
443 # with it.
444 1426 my $class = "ExtUtils::ParseXS::Node::$keyword";
445 1426 my $node = $class->new();
446 1426 ▶ if ($node->parse($pxs, $xsub, $xbody)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->parse($pxs, $xsub, $xbody)) 1389 0 1389
447 1389 push @{$self->{kids}}, $node;
448 1389 push @kids, $node;
449 }
450  
451 1389 $n++;
452 1389 ▶ last if defined $max and $max >= $n;
Branch: 1 branch
BranchTrueFalseTotal
if defined $max and $max >= $n 152 1237 1389
Condition66%2/3
defined $max and $max >= $n
!ll&&!rl&&r
12370152
MC/DC50%1/2
defined $max and $max >= $n
defined $max $max >= $n
Truth table66%2/3
A and B
A= defined $maxB= $max >= $n
ABresult
0X0
100
111
453 }
454  
455 4791 return @kids;
456 }
457  
458 1462 ▶sub as_code { }
Subroutine as_code: called CC=1 SCAR=0.0
Pod undocumented
459  
460 # Most node types inherit this: just continue walking the tree
461 # looking for any nodes which provide some boot code.
462 # It returns two array refs; one for lines of code to be injected early
463 # into the boot XSUB, the second for later code.
464  
465 3228 ▶sub as_boot_code {
Subroutine as_boot_code: called CC=3 SCAR=11.0
Pod undocumented
466 3228 my __PACKAGE__ $self = shift;
467 3228 my ExtUtils::ParseXS $pxs = shift;
468  
469 3228 my ($early, $later) = ([], []);
470 3228 my $kids = $self->{kids};
471 3228 ▶ if ($kids) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kids) 1143 2085 3228
472 1143 for (@$kids) {
473 3666 my ($e, $l) = $_->as_boot_code($pxs);
474 3666 push @$early, @$e;
475 3666 push @$later, @$l;
476 }
477 }
478 3228 return $early, $later;
479 }
480  
481  
482 # as_concise(): for debugging:
483 #
484 # Return a string representing a concise line-per-node representation
485 # of the node and any children, in the spirit of 'perl -MO=Concise'.
486 # Intended to be human- rather than machine-readable.
487 #
488 # The single optional parameter, depth, is for indentation purposes
489  
490 0 ▶sub as_concise {
Subroutine as_concise: not called CC=12 SCAR=50.5
Pod undocumented
491 0 my __PACKAGE__ $self = shift;
492 0 my $depth = shift;
493 0 ▶ $depth = 0 unless defined $depth;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $depth 0 0 0
494  
495 0 my $f = $self->{file};
496 0 ▶ $f = '??' unless defined $f;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $f 0 0 0
497 0 $f =~ s{^.*/}{};
498 0 ▶ substr($f,0,10) = '' if length($f) > 10;
Branch: 1 branch
BranchTrueFalseTotal
if length $f > 10 0 0 0
499  
500 0 my $l = $self->{line_no};
501 0 ▶ $l = defined $l ? sprintf("%-3d", $l) : '?? ';
Branch: 1 branch
BranchTrueFalseTotal
defined $l ? : 0 0 0
502  
503 0 my $s = sprintf "%-15s", "$f:$l";
504 0 $s .= (' ' x $depth);
505  
506 0 my $class = ref $self;
507 0 $class =~ s/^.*:://g;
508 0 $s .= "${class}: ";
509  
510 0 my @kv;
511  
512 0 for my $key (sort grep !/^(file|line_no|kids)$/, keys %$self) {
513 0 my $v = $self->{$key};
514  
515 # some basic pretty-printing
516  
517 0 ▶ if (!defined $v) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $v) { } 0 0 0
elsif (ref $v) { } 0 0 0
elsif ($v =~ /^-?\d+(\.\d+)?$/) { } 0 0 0
518 0 $v = '-';
519 }
520 elsif (ref $v) {
521 0 $v = "[ref]";
522 }
523 elsif ($v =~ /^-?\d+(\.\d+)?$/) {
524 # leave as-is
525 }
526 else {
527 0 $v = "$v";
528 0 $v =~ s/"/\\"/g;
529 0 my $max = 20;
530 0 ▶ substr($v, $max) = '...' if length($v) > $max;
Branch: 1 branch
BranchTrueFalseTotal
if length $v > $max 0 0 0
531 0 $v = qq("$v");
532 }
533  
534 0 push @kv, "$key=$v";
535 }
536  
537 0 $s .= join '; ', @kv;
538 0 $s .= "\n";
539  
540 0 ▶ if ($self->{kids}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"kids"}) 0 0 0
541 0 $s .= $_->as_concise($depth+1) for @{$self->{kids}};
542 }
543  
544 0 $s;
545 }
546  
547  
548 # Simple method wrapper for ExtUtils::ParseXS::Q
549  
550 3763 ▶sub Q {
Subroutine Q: called CC=1 SCAR=0.0
Pod undocumented
551 3763 my __PACKAGE__ $self = shift;
552 3763 my $text = shift;
553 3763 return ExtUtils::ParseXS::Q($text);
554 }
555  
556  
557 # ======================================================================
558  
559 package ExtUtils::ParseXS::Node::XS_file;
560  
561 # Top-level AST node representing an entire XS file
562  
563 28 ▶BEGIN { $build_subclass->(
Subroutine BEGIN: called CC=1 SCAR=0.0
564 'preamble', # Node::preamble object which emits preamble C code
565 'C_part', # the C part of the XS file, before the first MODULE
566 'C_part_postamble',# Node::C_part_postamble object which emits
567 # boilerplate code following the C code
568 'cpp_scope', # node holding all the XS part of the main file
569 'pre_boot', # node holding code after user XSUBs but before boot XSUB
570 'boot_xsub', # node holding code which generates the boot XSUB
571 )};
572  
573 434 ▶sub parse {
Subroutine parse: called CC=8 SCAR=20.9
574 434 my __PACKAGE__ $self = shift;
575 434 my ExtUtils::ParseXS $pxs = shift;
576  
577 434 $self->{line_no} = 1;
578 434 $self->{file} = $pxs->{in_pathname};
579  
580 # Hash of package name => package C name
581 434 $pxs->{map_overloaded_package_to_C_package} = {};
582  
583 # Hashref of package name => fallback setting
584 434 $pxs->{map_package_to_fallback_string} = {};
585  
586 434 $pxs->{error_count} = 0;
587  
588 # Initialise the sequence of guard defines used by cpp_scope
589 434 $pxs->{cpp_next_tmp_define} = 'XSubPPtmpAAAA';
590  
591 # "Parse" the start of the file. Doesn't actually consume any lines:
592 # just a placeholder for emitting preamble later
593  
594 434 my $preamble = ExtUtils::ParseXS::Node::preamble->new();
595 434 $self->{preamble} = $preamble;
596 434 ▶ $preamble->parse($pxs, $self)
Branch: 1 branch
BranchTrueFalseTotal
$preamble->parse($pxs, $self) or return 434 0 434
597 or return;
598 434 push @{$self->{kids}}, $preamble;
599  
600  
601 # Process the first (C language) half of the XS file, up until the first
602 # MODULE: line
603  
604 434 my $C_part = ExtUtils::ParseXS::Node::C_part->new();
605 434 $self->{C_part} = $C_part;
606 434 my $c_part_result = $C_part->parse($pxs, $self);
607 434 push @{$self->{kids}}, $C_part;
608  
609 # A failure when parsing the C part means that there wasn't a MODULE
610 # line. Don't try to parse the missing XS part, but still return
611 # success, passing through the lines from the C part.
612 434 ▶ return 1 unless $c_part_result;
Branch: 1 branch
BranchTrueFalseTotal
unless $c_part_result 1 433 434
613  
614 # "Parse" the bit following any C code. Doesn't actually consume any
615 # lines: just a placeholder for emitting postamble code.
616  
617 433 my $C_part_postamble = ExtUtils::ParseXS::Node::C_part_postamble->new();
618 433 $self->{C_part_postamble} = $C_part_postamble;
619 433 ▶ $C_part_postamble->parse($pxs, $self)
Branch: 1 branch
BranchTrueFalseTotal
$C_part_postamble->parse($pxs, $self) or return 433 0 433
620 or return;
621 433 push @{$self->{kids}}, $C_part_postamble;
622  
623 # Parse the XS half of the file
624  
625 433 my $cpp_scope = ExtUtils::ParseXS::Node::cpp_scope->new({type => 'main'});
626 433 $self->{cpp_scope} = $cpp_scope;
627 433 ▶ $cpp_scope->parse($pxs)
Branch: 1 branch
BranchTrueFalseTotal
$cpp_scope->parse($pxs) or return 371 0 371
628 or return;
629 371 push @{$self->{kids}}, $cpp_scope;
630  
631 # Now at EOF: all paragraphs (and thus XSUBs) have now been read in
632 # and processed. Do any final post-processing.
633  
634 # "Parse" the bit following any C code. Doesn't actually consume any
635 # lines: just a placeholder for emitting any code which should follow
636 # user XSUBs but which comes before the boot XSUB
637  
638 371 my $pre_boot = ExtUtils::ParseXS::Node::pre_boot->new();
639 371 $self->{pre_boot} = $pre_boot;
640 371 push @{$self->{kids}}, $pre_boot;
641 371 ▶ $pre_boot->parse($pxs)
Branch: 1 branch
BranchTrueFalseTotal
$pre_boot->parse($pxs) or return 371 0 371
642 or return;
643  
644 # Emit the boot XSUB initialization routine
645  
646 371 my $boot_xsub = ExtUtils::ParseXS::Node::boot_xsub->new();
647 371 $self->{boot_xsub} = $boot_xsub;
648 371 push @{$self->{kids}}, $boot_xsub;
649 371 ▶ $boot_xsub->parse($pxs)
Branch: 1 branch
BranchTrueFalseTotal
$boot_xsub->parse($pxs) or return 371 0 371
650 or return;
651  
652 warn( "Please specify prototyping behavior for "
653 . "$pxs->{in_filename} (see perlxs manual)\n")
654 371 ▶ unless $pxs->{proto_behaviour_specified};
Branch: 1 branch
BranchTrueFalseTotal
unless $pxs->{"proto_behaviour_specified"} 0 371 371
655  
656 371 1;
657 }
658  
659  
660 372 ▶sub as_code {
Subroutine as_code: called CC=2 SCAR=6.9
661 372 my __PACKAGE__ $self = shift;
662 372 my ExtUtils::ParseXS $pxs = shift;
663  
664 372 $_->as_code($pxs, $self) for @{$self->{kids}};
665  
666 }
667 # ======================================================================
668  
669 package ExtUtils::ParseXS::Node::preamble;
670  
671 # AST node representing the boilerplate C code preamble at the start of
672 # the file. Parsing doesn't actually consume any lines; it exists just for
673 # its as_code() method which emits the preamble into the C file.
674  
675 28 ▶BEGIN { $build_subclass->(
Subroutine BEGIN: called CC=1 SCAR=0.0
676 )};
677  
678 434 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
679 434 my __PACKAGE__ $self = shift;
680 434 my ExtUtils::ParseXS $pxs = shift;
681  
682 434 $self->{line_no} = 1;
683 434 $self->{file} = $pxs->{in_pathname};
684 434 1;
685 }
686  
687 372 ▶sub as_code {
Subroutine as_code: called CC=2 SCAR=6.9
688 372 my __PACKAGE__ $self = shift;
689 372 my ExtUtils::ParseXS $pxs = shift;
690  
691 # Emit preamble at start of C file, including the
692 # version it was generated by.
693  
694 372 print $self->Q(<<"EOM");
695 |/*
696 | * This file was generated automatically by ExtUtils::ParseXS version $ExtUtils::ParseXS::VERSION from the
697 | * contents of $pxs->{in_filename}. Do not edit this file, edit $pxs->{in_filename} instead.
698 | *
699 | * ANY CHANGES MADE HERE WILL BE LOST!
700 | *
701 | */
702 |
703 EOM
704  
705 print("#line 1 \"" .
706 ExtUtils::ParseXS::Utilities::escape_file_for_line_directive(
707 $self->{file}) . "\"\n")
708 372 ▶ if $pxs->{config_WantLineNumbers};
Branch: 1 branch
BranchTrueFalseTotal
if $pxs->{"config_WantLineNumbers"} 370 2 372
709 }
710  
711  
712 # ======================================================================
713  
714 package ExtUtils::ParseXS::Node::C_part;
715  
716 # A node representing the C part of the XS file - i.e. everything
717 # before the first MODULE line
718  
719 28 ▶BEGIN { $build_subclass->(
Subroutine BEGIN: called CC=1 SCAR=0.0
720 )};
721  
722 434 ▶sub parse {
Subroutine parse: called CC=5 SCAR=16.1
723 434 my __PACKAGE__ $self = shift;
724 434 my ExtUtils::ParseXS $pxs = shift;
725  
726 434 $self->{line_no} = 1;
727 434 $self->{file} = $pxs->{in_pathname};
728  
729 # Read in lines until the first MODULE line, creating a list of
730 # Node::C_part_code and Node::C_part_POD nodes as children.
731 # Returns with $pxs->{lastline} holding the next line (i.e. the MODULE
732 # line) or errors out if not found
733  
734 434 $pxs->{lastline} = readline($pxs->{in_fh});
735 434 $pxs->{lastline_no} = $.;
736  
737 434 ▶ while (defined $pxs->{lastline}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pxs->{"lastline"}) 442 1 443
738 442 ▶ if (ExtUtils::ParseXS::Utilities::looks_like_MODULE_line(
Branch: 1 branch
BranchTrueFalseTotal
if (ExtUtils::ParseXS::Utilities::looks_like_MODULE_line($pxs->{"lastline"})) 433 9 442
739 $pxs->{lastline}))
740 {
741 # the fetch_para() regime in place in the XS part of the file
742 # expects this to have been chomped
743 433 chomp $pxs->{lastline};
744 433 return 1;
745 }
746  
747 my $node =
748 9 ▶ $pxs->{lastline} =~ /^=/
Branch: 1 branch
BranchTrueFalseTotal
$pxs->{'lastline'} =~ /^=/ ? : 1 8 9
749 ? ExtUtils::ParseXS::Node::C_part_POD->new()
750 : ExtUtils::ParseXS::Node::C_part_code->new();
751  
752 # Read in next block of code or POD lines
753 9 ▶ $node->parse($pxs)
Branch: 1 branch
BranchTrueFalseTotal
$node->parse($pxs) or return 9 0 9
754 or return;
755 9 push @{$self->{kids}}, $node;
756 }
757  
758 1 warn "Warning: no MODULE line found in XS file $pxs->{in_filename}\n";
759 1 return;
760 }
761  
762  
763 372 ▶sub as_code {
Subroutine as_code: called CC=3 SCAR=11.0
764 372 my __PACKAGE__ $self = shift;
765 372 my ExtUtils::ParseXS $pxs = shift;
766  
767 372 $_->as_code($pxs, $self) for @{$self->{kids}};
768  
769 print 'ExtUtils::ParseXS::CountLines'->end_marker, "\n"
770 372 ▶ if $pxs->{config_WantLineNumbers};
Branch: 1 branch
BranchTrueFalseTotal
if $pxs->{"config_WantLineNumbers"} 370 2 372
771 }
772  
773  
774 # ======================================================================
775  
776 package ExtUtils::ParseXS::Node::C_part_POD;
777  
778 # A node representing a section of POD within the C part of the XS file
779  
780 28 ▶BEGIN { $build_subclass->(
Subroutine BEGIN: called CC=1 SCAR=0.0
781 'pod_lines', # array of lines containing pod, including start and end
782 # '=foo' lines
783 )};
784  
785 1 ▶sub parse {
Subroutine parse: called CC=3 SCAR=11.0
786 1 my __PACKAGE__ $self = shift;
787 1 my ExtUtils::ParseXS $pxs = shift;
788  
789 1 $self->{line_no} = $pxs->{lastline_no};
790 1 $self->{file} = $pxs->{in_pathname};
791  
792 # This method is called with $pxs->{lastline} holding the first line
793 # of POD and returns with $pxs->{lastline} holding the (unprocessed)
794 # next line following the =cut line
795  
796 1 my $cut;
797 1 while (1) {
798 5 push @{$self->{pod_lines}}, $pxs->{lastline};
799 5 $pxs->{lastline} = readline($pxs->{in_fh});
800 5 $pxs->{lastline_no} = $.;
801 5 ▶ return 1 if $cut;
Branch: 1 branch
BranchTrueFalseTotal
if $cut 1 4 5
802 4 ▶ last unless defined $pxs->{lastline};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pxs->{"lastline"} 0 4 4
803 4 $cut = $pxs->{lastline} =~ /^=cut\s*$/;
804 }
805  
806 # At this point $. is at end of file so die won't state the start
807 # of the problem, and as we haven't yet read any lines &death won't
808 # show the correct line in the message either.
809 0 die ( "Error: Unterminated pod in $pxs->{in_filename}, "
810 . "line $self->{line_no}\n");
811 }
812  
813  
814 1 ▶sub as_code {
Subroutine as_code: called CC=2 SCAR=7.0
815 1 my __PACKAGE__ $self = shift;
816 1 my ExtUtils::ParseXS $pxs = shift;
817  
818 # Emit something in the C file to indicate that a section of POD has
819 # been elided, while maintaining the correct lines numbers using
820 # #line.
821 #
822 # We can't just write out a /* */ comment, as our embedded POD might
823 # itself be in a comment. We can't put a /**/ comment inside #if 0, as
824 # the C standard says that the source file is decomposed into
825 # preprocessing characters in the stage before preprocessing commands
826 # are executed.
827 #
828 # I don't want to leave the text as barewords, because the spec isn't
829 # clear whether macros are expanded before or after preprocessing
830 # commands are executed, and someone pathological may just have
831 # defined one of the 3 words as a macro that does something strange.
832 # Multiline strings are illegal in C, so the "" we write must be a
833 # string literal. And they aren't concatenated until 2 steps later, so
834 # we are safe.
835 # - Nicholas Clark
836  
837 1 print $self->Q(<<"EOF");
838 |#if 0
839 | "Skipped embedded POD."
840 |#endif
841 EOF
842  
843 printf("#line %d \"%s\"\n",
844 1 $self->{line_no} + @{$self->{pod_lines}},
845 ExtUtils::ParseXS::Utilities::escape_file_for_line_directive(
846 $pxs->{in_pathname}))
847 1 ▶ if $pxs->{config_WantLineNumbers};
Branch: 1 branch
BranchTrueFalseTotal
if $pxs->{"config_WantLineNumbers"} 1 0 1
848 }
849  
850  
851 # ======================================================================
852  
853 package ExtUtils::ParseXS::Node::C_part_code;
854  
855 # A node representing a section of C code within the C part of the XS file
856  
857 28 ▶BEGIN { $build_subclass->(
Subroutine BEGIN: called CC=1 SCAR=0.0
858 'code_lines', # array of lines containing C code
859 )};
860  
861 8 ▶sub parse {
Subroutine parse: called CC=4 SCAR=13.9
862 8 my __PACKAGE__ $self = shift;
863 8 my ExtUtils::ParseXS $pxs = shift;
864  
865 8 $self->{line_no} = $pxs->{lastline_no};
866 8 $self->{file} = $pxs->{in_pathname};
867  
868 # This method is called with $pxs->{lastline} holding the first line
869 # of (possibly) C code and returns with $pxs->{lastline} holding the
870 # first (unprocessed) line which isn't C code (i.e. its the start of
871 # POD or a MODULE line)
872  
873 8 my $cut;
874 8 while (1) {
875 return 1 if ExtUtils::ParseXS::Utilities::looks_like_MODULE_line(
876 186 ▶ $pxs->{lastline});
Branch: 1 branch
BranchTrueFalseTotal
if ExtUtils::ParseXS::Utilities::looks_like_MODULE_line($pxs->{"lastline"}) 6 180 186
877 180 ▶ return 1 if $pxs->{lastline} =~ /^=/;
Branch: 1 branch
BranchTrueFalseTotal
if $pxs->{"lastline"} =~ /^=/ 1 179 180
878 179 push @{$self->{code_lines}}, $pxs->{lastline};
879 179 $pxs->{lastline} = readline($pxs->{in_fh});
880 179 $pxs->{lastline_no} = $.;
881 179 ▶ last unless defined $pxs->{lastline};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pxs->{"lastline"} 1 178 179
882 }
883  
884 1 1;
885 }
886  
887 8 ▶sub as_code {
Subroutine as_code: called CC=1 SCAR=0.0
888 8 my __PACKAGE__ $self = shift;
889 8 my ExtUtils::ParseXS $pxs = shift;
890  
891 8 print @{$self->{code_lines}};
892 }
893  
894  
895  
896 # ======================================================================
897  
898 package ExtUtils::ParseXS::Node::C_part_postamble;
899  
900 # AST node representing the boilerplate C code postamble following any
901 # initial C code contained within the C part of the XS file.
902 # This node's parse() method doesn't actually consume any lines; the node
903 # exists just for its as_code() method to emit the postamble into the C
904 # file.
905  
906 28 ▶BEGIN { $build_subclass->(
Subroutine BEGIN: called CC=1 SCAR=0.0
907 )};
908  
909 433 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
910 433 my __PACKAGE__ $self = shift;
911 433 my ExtUtils::ParseXS $pxs = shift;
912  
913 433 $self->{line_no} = $pxs->{lastline_no};
914 433 $self->{file} = $pxs->{in_pathname};
915 433 1;
916 }
917  
918 371 ▶sub as_code {
Subroutine as_code: called CC=2 SCAR=6.9
919 371 my __PACKAGE__ $self = shift;
920 371 my ExtUtils::ParseXS $pxs = shift;
921  
922 # Emit boilerplate postamble following any code passed through from
923 # the 'C' part of the XS file
924  
925 371 print $self->Q(<<'EOF');
926 |#ifndef PERL_UNUSED_VAR
927 |# define PERL_UNUSED_VAR(var) if (0) var = var
928 |#endif
929 |
930 |#ifndef dVAR
931 |# define dVAR dNOOP
932 |#endif
933 |
934 |
935 |/* This stuff is not part of the API! You have been warned. */
936 |#ifndef PERL_VERSION_DECIMAL
937 |# define PERL_VERSION_DECIMAL(r,v,s) (r*1000000 + v*1000 + s)
938 |#endif
939 |#ifndef PERL_DECIMAL_VERSION
940 |# define PERL_DECIMAL_VERSION \
941 | PERL_VERSION_DECIMAL(PERL_REVISION,PERL_VERSION,PERL_SUBVERSION)
942 |#endif
943 |#ifndef PERL_VERSION_GE
944 |# define PERL_VERSION_GE(r,v,s) \
945 | (PERL_DECIMAL_VERSION >= PERL_VERSION_DECIMAL(r,v,s))
946 |#endif
947 |#ifndef PERL_VERSION_LE
948 |# define PERL_VERSION_LE(r,v,s) \
949 | (PERL_DECIMAL_VERSION <= PERL_VERSION_DECIMAL(r,v,s))
950 |#endif
951 |
952 |/* XS_INTERNAL is the explicit static-linkage variant of the default
953 | * XS macro.
954 | *
955 | * XS_EXTERNAL is the same as XS_INTERNAL except it does not include
956 | * "static", ie. it exports XSUB symbols. You probably don't want that
957 | * for anything but the BOOT XSUB.
958 | *
959 | * See XSUB.h in core!
960 | */
961 |
962 |
963 |/* TODO: This might be compatible further back than 5.10.0. */
964 |#if PERL_VERSION_GE(5, 10, 0) && PERL_VERSION_LE(5, 15, 1)
965 |# undef XS_EXTERNAL
966 |# undef XS_INTERNAL
967 |# if defined(__CYGWIN__) && defined(USE_DYNAMIC_LOADING)
968 |# define XS_EXTERNAL(name) __declspec(dllexport) XSPROTO(name)
969 |# define XS_INTERNAL(name) static XSPROTO(name)
970 |# endif
971 |# if defined(__SYMBIAN32__)
972 |# define XS_EXTERNAL(name) EXPORT_C XSPROTO(name)
973 |# define XS_INTERNAL(name) EXPORT_C static XSPROTO(name)
974 |# endif
975 |# ifndef XS_EXTERNAL
976 |# if defined(HASATTRIBUTE_UNUSED) && !defined(__cplusplus)
977 |# define XS_EXTERNAL(name) void name(pTHX_ CV* cv __attribute__unused__)
978 |# define XS_INTERNAL(name) static void name(pTHX_ CV* cv __attribute__unused__)
979 |# else
980 |# ifdef __cplusplus
981 |# define XS_EXTERNAL(name) extern "C" XSPROTO(name)
982 |# define XS_INTERNAL(name) static XSPROTO(name)
983 |# else
984 |# define XS_EXTERNAL(name) XSPROTO(name)
985 |# define XS_INTERNAL(name) static XSPROTO(name)
986 |# endif
987 |# endif
988 |# endif
989 |#endif
990 |
991 |/* perl >= 5.10.0 && perl <= 5.15.1 */
992 |
993 |
994 |/* The XS_EXTERNAL macro is used for functions that must not be static
995 | * like the boot XSUB of a module. If perl didn't have an XS_EXTERNAL
996 | * macro defined, the best we can do is assume XS is the same.
997 | * Dito for XS_INTERNAL.
998 | */
999 |#ifndef XS_EXTERNAL
1000 |# define XS_EXTERNAL(name) XS(name)
1001 |#endif
1002 |#ifndef XS_INTERNAL
1003 |# define XS_INTERNAL(name) XS(name)
1004 |#endif
1005 |
1006 |/* Now, finally, after all this mess, we want an ExtUtils::ParseXS
1007 | * internal macro that we're free to redefine for varying linkage due
1008 | * to the EXPORT_XSUB_SYMBOLS XS keyword. This is internal, use
1009 | * XS_EXTERNAL(name) or XS_INTERNAL(name) in your code if you need to!
1010 | */
1011 |
1012 |#undef XS_EUPXS
1013 |#if defined(PERL_EUPXS_ALWAYS_EXPORT)
1014 |# define XS_EUPXS(name) XS_EXTERNAL(name)
1015 |#else
1016 | /* default to internal */
1017 |# define XS_EUPXS(name) XS_INTERNAL(name)
1018 |#endif
1019 |
1020 |#ifndef PERL_ARGS_ASSERT_CROAK_XS_USAGE
1021 |#define PERL_ARGS_ASSERT_CROAK_XS_USAGE assert(cv); assert(params)
1022 |
1023 |/* prototype to pass -Wmissing-prototypes */
1024 |static void
1025 |S_croak_xs_usage(const CV *const cv, const char *const params);
1026 |
1027 |static void
1028 |S_croak_xs_usage(const CV *const cv, const char *const params)
1029 |{
1030 | const GV *const gv = CvGV(cv);
1031 |
1032 | PERL_ARGS_ASSERT_CROAK_XS_USAGE;
1033 |
1034 | if (gv) {
1035 | const char *const gvname = GvNAME(gv);
1036 | const HV *const stash = GvSTASH(gv);
1037 | const char *const hvname = stash ? HvNAME(stash) : NULL;
1038 |
1039 | if (hvname)
1040 | Perl_croak_nocontext("Usage: %s::%s(%s)", hvname, gvname, params);
1041 | else
1042 | Perl_croak_nocontext("Usage: %s(%s)", gvname, params);
1043 | } else {
1044 | /* Pants. I don't think that it should be possible to get here. */
1045 | Perl_croak_nocontext("Usage: CODE(0x%" UVxf ")(%s)", PTR2UV(cv), params);
1046 | }
1047 |}
1048 |#undef PERL_ARGS_ASSERT_CROAK_XS_USAGE
1049 |
1050 |#define croak_xs_usage S_croak_xs_usage
1051 |
1052 |#endif
1053 |
1054 |/* NOTE: the prototype of newXSproto() is different in versions of perls,
1055 | * so we define a portable version of newXSproto()
1056 | */
1057 |#ifdef newXS_flags
1058 |#define newXSproto_portable(name, c_impl, file, proto) newXS_flags(name, c_impl, file, proto, 0)
1059 |#else
1060 |#define newXSproto_portable(name, c_impl, file, proto) (PL_Sv=(SV*)newXS(name, c_impl, file), sv_setpv(PL_Sv, proto), (CV*)PL_Sv)
1061 |#endif /* !defined(newXS_flags) */
1062 |
1063 |#if PERL_VERSION_LE(5, 21, 5)
1064 |# define newXS_deffile(a,b) Perl_newXS(aTHX_ a,b,file)
1065 |#else
1066 |# define newXS_deffile(a,b) Perl_newXS_deffile(aTHX_ a,b)
1067 |#endif
1068 |
1069 |/* simple backcompat versions of the TARGx() macros with no optimisation */
1070 |#ifndef TARGi
1071 |# define TARGi(iv, do_taint) sv_setiv_mg(TARG, iv)
1072 |# define TARGu(uv, do_taint) sv_setuv_mg(TARG, uv)
1073 |# define TARGn(nv, do_taint) sv_setnv_mg(TARG, nv)
1074 |#endif
1075 |
1076 EOF
1077  
1078 # Fix up line number reckoning
1079  
1080 print 'ExtUtils::ParseXS::CountLines'->end_marker, "\n"
1081 371 ▶ if $pxs->{config_WantLineNumbers};
Branch: 1 branch
BranchTrueFalseTotal
if $pxs->{"config_WantLineNumbers"} 369 2 371
1082 }
1083  
1084  
1085 # ======================================================================
1086  
1087 package ExtUtils::ParseXS::Node::cpp_scope;
1088  
1089 # Node representing a part of an XS file which is all in the same C
1090 # preprocessor scope as regards C preprocessor (CPP) conditionals, i.e.
1091 # #if/#elsif/#else/#endif etc.
1092 #
1093 # Note that this only considers file-scoped C preprocessor directives;
1094 # ones within a code block such as CODE or BOOT don't contribute to the
1095 # state maintained here.
1096 #
1097 # Initially the whole XS part of the main XS file is considered a single
1098 # scope, so the single main cpp_scope node would have, as children, all
1099 # the file-scoped nodes such as Node::PROTOTYPES and any Node::xsub's.
1100 #
1101 # After an INCLUDE, the new XS file is considered as being in a separate
1102 # scope, and gets its own child cpp_scope node.
1103 #
1104 # Once an XS file starts having file-scope CPP conditionals, then each
1105 # branch of the conditional is considered a separate scope and gets its
1106 # own cpp_scope node. Nested conditionals cause nested cpp_scope objects
1107 # in the AST.
1108 #
1109 # The main reason for this node type is to separate out the AST into
1110 # separate sections which can each have the same named XSUB without a
1111 # 'duplicate XSUB' warning, and where newXS()-type calls can be added to
1112 # to the boot code for *both* XSUBs, guarded by suitable #ifdef's.
1113 #
1114 # This node is the main high-level node where file-scoped parsing takes
1115 # place: its parse() method contains a fetch_para() loop which does all
1116 # the looking for file-scoped keywords, CPP directives, and XSUB
1117 # declarations. It implements a recursive-decent parser by creating child
1118 # cpp_scope nodes and recursing into that child's parse() method (which
1119 # does its own fetch_para() calls).
1120  
1121 28 ▶BEGIN { $build_subclass->(
Subroutine BEGIN: called CC=1 SCAR=0.0
1122 'type', # Str: what sort of scope: 'main', 'include' or 'if'
1123 'is_cmd', # Bool: for include type, it's INCLUDE_COMMAND
1124 'guard_name', # Str: the name of the XSubPPtmpAAAA guard define
1125 'seen_xsubs', # Hash: the names of any XSUBs seen in this scope
1126 )};
1127  
1128 462 ▶sub parse {
Subroutine parse: called CC=28 SCAR=33.6
1129 462 my __PACKAGE__ $self = shift;
1130 462 my ExtUtils::ParseXS $pxs = shift;
1131  
1132 # Main loop: for each iteration, parse the next 'thing' in the current
1133 # paragraph, such as a C preprocessor directive, a contiguous block of
1134 # file-scoped keywords, or an XSUB. When the current paragraph runs
1135 # out, read in another one. XSUBs, TYPEMAP and BOOT will consume
1136 # all lines until the end of the current paragraph.
1137 #
1138 # C preprocessor conditionals such as #if may trigger recursive
1139 # calls to process each branch until the matching #endif. These may
1140 # cross paragraph boundaries.
1141  
1142 462 ▶ while ( ($pxs->{line} && @{$pxs->{line}}) || $pxs->fetch_para())
Branch: 1 branch
BranchTrueFalseTotal
if ($pxs->{"line"} and @{$pxs->{"line"};} or $pxs->fetch_para) 1682 377 2059
Condition100%6/6
$pxs->{"line"} and @{$pxs->{"line"};} or $pxs->fetch_para
l!l&&r!l&&!r
41679377
$pxs->{"line"} and @{$pxs->{"line"};}
!ll&&!rl&&r
43116254
MC/DC66%2/3
$pxs->{"line"} and @{$pxs->{"line"};} or $pxs->fetch_para
$pxs->{"line"} @{$pxs->{"line"};} $pxs->fetch_para
Truth table80%4/5
A and B or C
A= $pxs->{"line"}B= @{$pxs->{"line"};}C= $pxs->fetch_para
ABCresult
11X1
0X11
1011
0X00
1000
1143 {
1144 1682 ▶ next unless @{$pxs->{line}}; # fetch_para() can return zero lines
Branch: 1 branch
BranchTrueFalseTotal
unless @{$pxs->{"line"};} 87 1595 1682
1145  
1146 1595 ▶ if ( !defined($self->{line_no})
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"line_no"} and defined $pxs->{"line_no"}[0]) 461 1134 1595
Condition66%2/3
not defined $self->{"line_no"} and defined $pxs->{"line_no"}[0]
!ll&&!rl&&r
11340461
MC/DC50%1/2
not defined $self->{"line_no"} and defined $pxs->{"line_no"}[0]
not defined $self->{"line_no"} defined $pxs->{"line_no"}[0]
Truth table66%2/3
not(A) and B
A= not defined $self->{"line_no"}B= defined $pxs->{"line_no"}[0]
ABresult
0X0
100
111
1147 && defined $pxs->{line_no}[0]
1148 ) {
1149 # set file/line_no after line number info is available:
1150 # typically after the first call to fetch_para()
1151 461 $self->SUPER::parse($pxs);
1152 }
1153  
1154 # skip blank line
1155 0 ▶ shift @{$pxs->{line}}, next if $pxs->{line}[0] !~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $pxs->{"line"}[0] =~ /\S/ 0 1595 1595
1156  
1157 # Process a C-preprocessor line. Note that any non-CPP lines
1158 # starting with '#' will already have been filtered out by
1159 # fetch_para().
1160 #
1161 # If its a #if or similar, then recursively process each branch
1162 # as a separate cpp_scope object until the matching #endif is
1163 # reached.
1164  
1165 1595 ▶ if ($pxs->{line}[0] =~ /^#/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pxs->{"line"}[0] =~ /^#/) 46 1549 1595
1166 46 my $node = ExtUtils::ParseXS::Node::global_cpp_line->new();
1167 46 ▶ $node->parse($pxs)
Branch: 1 branch
BranchTrueFalseTotal
$node->parse($pxs) or next 46 0 46
1168 or next;
1169 46 push @{$self->{kids}}, $node;
1170  
1171 46 ▶ next unless $node->{is_cond};
Branch: 1 branch
BranchTrueFalseTotal
unless $node->{"is_cond"} 6 40 46
1172  
1173 # Parse branches of a CPP conditionals within a nested scope
1174  
1175 40 ▶ if (not $node->{is_if}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($node->{"is_if"}) 23 17 40
1176 $pxs->death("Error: '#". $node->{directive}
1177 . "' with no matching '#if'")
1178 23 ▶ if $self->{type} ne 'if';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"type"} ne "if" 1 22 23
1179  
1180 # we should already be within a nested scope; this
1181 # CPP condition keyword just ends that scope. Our
1182 # (recursive) caller will handle processing any further
1183 # branches if it's an elif/else rather than endif
1184  
1185 22 return 1
1186 }
1187  
1188 # So it's an 'if'/'ifdef' etc node. Start a new
1189 # Node::cpp_scope sub-parse to handle that branch and then any
1190 # other branches of the same conditional.
1191  
1192 17 while (1) {
1193 # For each iteration, parse the next branch in a new scope
1194 23 my $scope = ExtUtils::ParseXS::Node::cpp_scope->new(
1195 {type => 'if'});
1196 23 ▶ $scope->parse($pxs)
Branch: 1 branch
BranchTrueFalseTotal
$scope->parse($pxs) or next 23 0 23
1197 or next;
1198  
1199 # Sub-parsing of that branch should have terminated
1200 # at an elif/endif line rather than falling off the
1201 # end of the file
1202 23 my $last = $scope->{kids}[-1];
1203 23 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $last and $last->isa("ExtUtils::ParseXS::Node::global_cpp_line") and $last->{"is_cond"} and not $last->{"is_if"}) 1 22 23
Condition66%6/9
defined $last and $last->isa("ExtUtils::ParseXS::Node::global_cpp_line") and $last->{"is_cond"} and not $last->{"is_if"}
!ll&&!rl&&r
1022
defined $last and $last->isa("ExtUtils::ParseXS::Node::global_cpp_line") and $last->{"is_cond"}
!ll&&!rl&&r
1022
defined $last and $last->isa("ExtUtils::ParseXS::Node::global_cpp_line")
!ll&&!rl&&r
0122
MC/DC25%1/4
defined $last and $last->isa("ExtUtils::ParseXS::Node::global_cpp_line") and $last->{"is_cond"} and not $last->{"is_if"}
defined $last $last->isa("ExtUtils::ParseXS::Node::global_cpp_line") $last->{"is_cond"} not $last->{"is_if"}
Truth table40%2/5
A and B and C and not(D)
A= defined $lastB= $last->isa("ExtUtils::ParseXS::Node::global_cpp_line")C= $last->{"is_cond"}D= not $last->{"is_if"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1204 defined $last
1205 && $last->isa(
1206 'ExtUtils::ParseXS::Node::global_cpp_line')
1207 && $last->{is_cond}
1208 && !$last->{is_if}
1209 ) {
1210 1 $pxs->death( "Error: Unterminated '#$node->{directive}'"
1211 . " from line $node->{line_no}")
1212 }
1213  
1214 # Move the CPP line which terminated the branch from
1215 # the end of the inner scope to the current scope
1216 22 pop @{$scope->{kids}};
1217 22 push @{$self->{kids}}, $scope, $last;
1218  
1219 22 ▶ if (grep { ref($_) !~ /::global_cpp_line$/ }
Branch: 1 branch
BranchTrueFalseTotal
if (grep {not ref($_) =~ /::global_cpp_line$/;} @{$$scope{"kids"};}) 20 2 22
1220 22 @{$scope->{kids}} )
1221 {
1222 # the inner scope has some content, so needs
1223 # a '#define XSubPPtmpAAAA 1'-style guard
1224 20 $scope->{guard_name} = $pxs->{cpp_next_tmp_define}++;
1225 }
1226  
1227 # any more branches to process of current if?
1228 22 ▶ last if $last->{is_endif};
Branch: 1 branch
BranchTrueFalseTotal
if $last->{"is_endif"} 16 6 22
1229 } # while 1
1230  
1231 16 next;
1232 }
1233  
1234 1549 my $file_scoped_keywords =
1235 "BOOT|REQUIRE|PROTOTYPES|EXPORT_XSUB_SYMBOLS|FALLBACK"
1236 . "|VERSIONCHECK|INCLUDE|INCLUDE_COMMAND|SCOPE|TYPEMAP";
1237  
1238 # Die if the next line is indented: all file-scoped things (CPP,
1239 # keywords, XSUB starts) are supposed to start on column 1
1240 # (although see the comment below about multiple parse_keywords()
1241 # iterations sneaking in indented keywords).
1242 #
1243  
1244 1549 ▶ if ($pxs->{line}[0] =~ /^\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pxs->{"line"}[0] =~ /^\s/) 3 1546 1549
1245 # Try to customise the error message based around why this
1246 # line is indented, to better hint to the user what the
1247 # problem is.
1248  
1249 3 ▶ if ($pxs->{line}[0] =~ /^\s+($file_scoped_keywords)\s*:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pxs->{"line"}[0] =~ /^\s+($file_scoped_keywords)\s*:/) 1 2 3
1250 1 $pxs->death(
1251 "Error: file-scoped keywords should not be indented");
1252 }
1253  
1254 # The text of the error message is based around a common reason
1255 # for an indented line to appear in file scope: this is due to an
1256 # XSUB being prematurely truncated by fetch_para(). For example in
1257 # the code below, the coder wants the foo and bar lines to both be
1258 # part of the same CODE block. But the XS parser sees the blank
1259 # line followed by the '#ifdef' on column 1 as terminating the
1260 # current XSUB. So the bar() line is treated as being in file
1261 # scope and dies because it is indented.
1262 #
1263 # |int f()
1264 # | CODE:
1265 # | foo();
1266 # |
1267 # |#ifdef USE_BAR
1268 # | bar();
1269 # |#endif
1270  
1271 $pxs->deathHint(
1272 2 "Error: file-scoped directives must not be indented",
1273 $self->Q(<<EOF))
1274 |If this line is supposed to be part of an XSUB rather than being
1275 |file-scoped, then it is possible that your XSUB has a blank line
1276 |followed by a line starting at column 1 which is being misinterpreted
1277 |as the end of the current XSUB.
1278 EOF
1279 }
1280  
1281 # The SCOPE keyword can appear both in file scope (just before an
1282 # XSUB) and as an XSUB keyword. This field maintains the state of the
1283 # former: reset it at the start of processing any file-scoped
1284 # keywords just before the XSUB (i.e. without any blank lines, e.g.
1285 # SCOPE: ENABLE
1286 # int
1287 # foo(...)
1288 # These semantics may not be particularly sensible, but they maintain
1289 # backwards compatibility for now.
1290  
1291 1546 $pxs->{file_SCOPE_enabled} = 0;
1292  
1293 # Process file-scoped keywords
1294 #
1295 # This loop repeatedly: skips any blank lines and then calls
1296 # the relevant Node::FOO::parse() method if it finds any of the
1297 # file-scoped keywords in the passed pattern.
1298 #
1299 # Note: due to the looping within parse_keywords() rather than
1300 # looping here, only the first keyword in a contiguous block
1301 # gets the 'start at column 1' check above enforced.
1302 # This is a bug, maintained for backwards compatibility: see the
1303 # comments below referring to SCOPE for other bits of code needed
1304 # to enforce this compatibility.
1305  
1306 1546 $self->parse_keywords(
1307 $pxs,
1308 undef, undef, # xsub and xbody: not needed for non XSUB keywords
1309 undef, # implies process as many keywords as possible
1310 $file_scoped_keywords,
1311 $keywords_flag_MODULE,
1312 );
1313 # XXX we could have an 'or next' here if not for SCOPE backcompat
1314 # and also delete the following 'skip blank line' and 'next unless
1315 # @line' lines
1316  
1317 # skip blank lines
1318 0 ▶ shift @{$pxs->{line}} while @{$pxs->{line}} && $pxs->{line}[0] !~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
@{$pxs->{"line"};} and not $pxs->{"line"}[0] =~ /\S/ and do { shift @{$$pxs{"line"};} } 0 1515 1515
Condition66%2/3
@{$pxs->{"line"};} and not $pxs->{"line"}[0] =~ /\S/
!ll&&!rl&&r
10524630
MC/DC0%0/2
@{$pxs->{"line"};} and not $pxs->{"line"}[0] =~ /\S/
@{$pxs->{"line"};} not $pxs->{"line"}[0] =~ /\S/
Truth table66%2/3
A and not(B)
A= @{$pxs->{"line"};}B= not $pxs->{"line"}[0] =~ /\S/
ABresult
0X0
100
111
1319  
1320 1515 ▶ next unless @{$pxs->{line}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$pxs->{"line"};} 1052 463 1515
1321  
1322 # Parse an XSUB
1323  
1324 463 my $xsub = ExtUtils::ParseXS::Node::xsub->new();
1325 463 ▶ $xsub->parse($pxs)
Branch: 1 branch
BranchTrueFalseTotal
$xsub->parse($pxs) or next 435 2 437
1326 or next;
1327 435 push @{$self->{kids}}, $xsub;
1328  
1329 # Check for a duplicate function definition in this scope
1330 {
1331 435 my $name = $xsub->{decl}{full_C_name};
1332 435 ▶ if ($self->{seen_xsubs}{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"seen_xsubs"}{$name}) 4 431 435
1333 4 (my $short = $name) =~ s/^$pxs->{PACKAGE_C_name}_//;
1334 4 $pxs->Warn( "Warning: duplicate function definition "
1335 . "'$short' detected");
1336 }
1337 435 $self->{seen_xsubs}{$name} = 1;
1338 }
1339  
1340 # xsub->parse() should have consumed all the remaining
1341 # lines in the current paragraph.
1342 die "Internal error: unexpectedly not at EOF\n"
1343 435 ▶ if @{$pxs->{line}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$pxs->{"line"};} 0 435 435
1344  
1345 435 $pxs->{seen_an_XSUB} = 1; # encountered at least one XSUB
1346  
1347 } # END main 'while' loop
1348  
1349 377 1;
1350 }
1351  
1352  
1353 397 ▶sub as_code {
Subroutine as_code: called CC=3 SCAR=11.0
1354 397 my __PACKAGE__ $self = shift;
1355 397 my ExtUtils::ParseXS $pxs = shift;
1356  
1357 397 my $g = $self->{guard_name};
1358 397 ▶ print "#define $g 1\n\n" if defined $g;
Branch: 1 branch
BranchTrueFalseTotal
if defined $g 20 377 397
1359 397 $_->as_code($pxs, $self) for @{$self->{kids}};
1360 }
1361  
1362  
1363 397 ▶sub as_boot_code {
Subroutine as_boot_code: called CC=2 SCAR=6.9
1364 397 my __PACKAGE__ $self = shift;
1365 397 my ExtUtils::ParseXS $pxs = shift;
1366  
1367 # accumulate all the newXS()'s in $early and the BOOT blocks in $later,
1368 397 my ($early, $later) = $self->SUPER::as_boot_code($pxs);
1369  
1370 # then wrap them all within '#if XSubPPtmpAAAA' guards
1371 397 my $g = $self->{guard_name};
1372 397 ▶ if (defined $g) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $g) 20 377 397
1373 20 unshift @$early, "#if $g\n";
1374 20 unshift @$later, "#if $g\n";
1375 20 push @$early, "#endif\n";
1376 20 push @$later, "#endif\n";
1377 }
1378  
1379 397 return $early, $later;
1380 }
1381  
1382  
1383 # ======================================================================
1384  
1385 package ExtUtils::ParseXS::Node::global_cpp_line;
1386  
1387 # AST node representing a single C-preprocessor line in file (global)
1388 # scope. (A "single" line can actually include embedded "\\\n"'s from line
1389 # continuations).
1390  
1391 28 ▶BEGIN { $build_subclass->(
Subroutine BEGIN: called CC=1 SCAR=0.0
1392 'cpp_line', # Str: the full text of the "# foo" CPP line
1393 'directive', # Str: one of 'define', 'endif' etc
1394 'rest', # Str: the rest of the line following the directive
1395 'is_cond', # Bool: it's an ifdef/else/endif etc
1396 'is_if', # Bool: it's an if/ifdef/ifndef
1397 'is_endif' # Bool: it's an endif
1398 )};
1399  
1400 46 ▶sub parse {
Subroutine parse: called CC=3 SCAR=11.0
1401 46 my __PACKAGE__ $self = shift;
1402 46 my ExtUtils::ParseXS $pxs = shift;
1403  
1404 46 $self->SUPER::parse($pxs); # set file/line_no
1405  
1406 46 my $line = shift @{$pxs->{line}};
1407  
1408 46 ▶ my ($directive, $rest) = $line =~ /^ \# \s* (\w+) (?:\s+ (.*) \s* $)?/sx
Branch: 1 branch
BranchTrueFalseTotal
() = $line =~ /^ \# \s* (\w+) (?:\s+ (.*) \s* $)?/sx or $pxs->death("Internal error: can't parse CPP line: $line\n") 46 0 46
1409 or $pxs->death("Internal error: can't parse CPP line: $line\n");
1410 46 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 22 24 46
1411 46 my $is_cond = $directive =~ /^(if|ifdef|ifndef|elif|else|endif)$/;
1412 46 my $is_if = $directive =~ /^(if|ifdef|ifndef)$/;
1413 46 my $is_endif = $directive =~ /^endif$/;
1414 46 @$self{qw(cpp_line directive rest is_cond is_if is_endif)}
1415 = ($line, $directive, $rest, $is_cond, $is_if, $is_endif);
1416  
1417 46 1;
1418 }
1419  
1420  
1421 42 ▶sub as_code {
Subroutine as_code: called CC=1 SCAR=0.0
1422 42 my __PACKAGE__ $self = shift;
1423 42 my ExtUtils::ParseXS $pxs = shift;
1424  
1425 42 print $self->{cpp_line}, "\n";
1426 }
1427  
1428  
1429 # ======================================================================
1430  
1431 package ExtUtils::ParseXS::Node::BOOT;
1432  
1433 # Store the code lines associated with the BOOT keyword
1434 #
1435 # Note that unlike other codeblock-like Node classes, BOOT consumes
1436 # *all* lines remaining in the current paragraph, rather than stopping
1437 # at the next keyword, if any.
1438 # It's also file-scoped rather than XSUB-scoped.
1439  
1440 28 ▶BEGIN { $build_subclass->(
Subroutine BEGIN: called CC=1 SCAR=0.0
1441 'lines', # Array ref of all code lines making up the BOOT
1442 )};
1443  
1444  
1445 # Consume all the remaining lines and store in @$lines.
1446  
1447 4 ▶sub parse {
Subroutine parse: called CC=3 SCAR=11.0
1448 4 my __PACKAGE__ $self = shift;
1449 4 my ExtUtils::ParseXS $pxs = shift;
1450  
1451 4 $self->SUPER::parse($pxs); # set file/line_no
1452  
1453 # Check all the @{$pxs->{line}} lines for balance: all the
1454 # #if, #else, #endif etc within the BOOT should balance out.
1455 4 ExtUtils::ParseXS::Utilities::check_conditional_preprocessor_statements();
1456  
1457 # Suck in all remaining lines
1458  
1459 4 $self->{lines} = [ @{$pxs->{line}} ];
1460 4 @{$pxs->{line}} = ();
1461  
1462 # Text following the keyword is ignored rather than being treated
1463 # as the first line of code.
1464 4 my $line0 = shift @{$self->{lines}};
1465 4 ▶ $pxs->Warn("Warning: text after keyword ignored: '$line0'")
Branch: 1 branch
BranchTrueFalseTotal
if defined $line0 and $line0 =~ /\S/ 2 2 4
Condition66%2/3
defined $line0 and $line0 =~ /\S/
!ll&&!rl&&r
022
MC/DC50%1/2
defined $line0 and $line0 =~ /\S/
defined $line0 $line0 =~ /\S/
Truth table66%2/3
A and B
A= defined $line0B= $line0 =~ /\S/
ABresult
0X0
100
111
1466 if defined $line0 && $line0 =~ /\S/;
1467  
1468 4 1;
1469 }
1470  
1471  
1472 4 ▶sub as_boot_code {
Subroutine as_boot_code: called CC=5 SCAR=17.3
1473 4 my __PACKAGE__ $self = shift;
1474 4 my ExtUtils::ParseXS $pxs = shift;
1475  
1476 4 my @lines;
1477  
1478 # Prepend a '#line' directive if not already present
1479 4 ▶ if ( $pxs->{config_WantLineNumbers}
Branch: 1 branch
BranchTrueFalseTotal
if ($pxs->{"config_WantLineNumbers"} and @{$self->{"lines"};} and not $self->{"lines"}[0] =~ /^\s*#\s*line\b/) 4 0 4
Condition33%2/6
$pxs->{"config_WantLineNumbers"} and @{$self->{"lines"};} and not $self->{"lines"}[0] =~ /^\s*#\s*line\b/
!ll&&!rl&&r
004
$pxs->{"config_WantLineNumbers"} and @{$self->{"lines"};}
!ll&&!rl&&r
004
MC/DC0%0/3
$pxs->{"config_WantLineNumbers"} and @{$self->{"lines"};} and not $self->{"lines"}[0] =~ /^\s*#\s*line\b/
$pxs->{"config_WantLineNumbers"} @{$self->{"lines"};} not $self->{"lines"}[0] =~ /^\s*#\s*line\b/
Truth table25%1/4
A and B and not(C)
A= $pxs->{"config_WantLineNumbers"}B= @{$self->{"lines"};}C= not $self->{"lines"}[0] =~ /^\s*#\s*line\b/
ABCresult
0XX0
10X0
1100
1111
1480 4 && @{$self->{lines}}
1481 && $self->{lines}[0] !~ /^\s*#\s*line\b/
1482 )
1483 {
1484 push @lines,
1485 sprintf "#line %d \"%s\"\n",
1486 $self->{line_no} + 1,
1487 ExtUtils::ParseXS::Utilities::escape_file_for_line_directive(
1488 4 $self->{file});
1489 }
1490  
1491 # Save all the BOOT lines (plus trailing empty line) to be emitted
1492 # later.
1493 4 push @lines, "$_\n" for @{$self->{lines}}, "";
1494  
1495 4 return [], \@lines;
1496 }
1497  
1498 # ======================================================================
1499  
1500 package ExtUtils::ParseXS::Node::TYPEMAP;
1501  
1502 # Process the lines associated with the TYPEMAP keyword
1503 #
1504 # fetch_para() will have already processed the <<EOF logic
1505 # and read all the lines up to, but not including, the EOF line.
1506  
1507 28 ▶BEGIN { $build_subclass->(
Subroutine BEGIN: called CC=1 SCAR=0.0
1508 'lines', # Array ref of all lines making up the TYPEMAP section
1509 )};
1510  
1511  
1512 # Feed all the lines to ExtUtils::Typemaps.
1513  
1514 168 ▶sub parse {
Subroutine parse: called CC=2 SCAR=6.9
1515 168 my __PACKAGE__ $self = shift;
1516 168 my ExtUtils::ParseXS $pxs = shift;
1517  
1518 168 $self->SUPER::parse($pxs); # set file/line_no
1519  
1520 168 shift @{$pxs->{line}}; # skip the 'TYPEMAP:' line
1521  
1522 # Suck in all remaining lines
1523 168 $self->{lines} = $pxs->{line};
1524 168 $pxs->{line} = [];
1525  
1526 my $tmap = ExtUtils::Typemaps->new(
1527 168 ▶ string => join("", map "$_\n", @{$self->{lines}}),
Condition50%1/2
$pxs->current_line_number || 0
l!l
1680
MC/DC0%0/1
$pxs->current_line_number || 0
$pxs->current_line_number
Truth table50%1/2
A
A= $pxs->current_line_number
Aresult
11
00
1528 lineno_offset => 1 + ($pxs->current_line_number() || 0),
1529 fake_filename => $pxs->{in_filename},
1530 168 );
1531  
1532 168 $pxs->{typemaps_object}->merge(typemap => $tmap, replace => 1);
1533  
1534 168 1;
1535 }
1536  
1537  
1538 # ======================================================================
1539  
1540 package ExtUtils::ParseXS::Node::pre_boot;
1541  
1542 # AST node representing C code that is emitted after all user-defined
1543 # XSUBs but before the boot XSUB. (This currently consists of
1544 # 'Foo::Bar::()' XSUBs for any packages which have overloading.)
1545 #
1546 # This node's parse() method doesn't actually consume any lines; the node
1547 # exists just for its as_code() method.
1548  
1549 28 ▶BEGIN { $build_subclass->(
Subroutine BEGIN: called CC=1 SCAR=0.0
1550 )};
1551  
1552 371 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
1553 371 my __PACKAGE__ $self = shift;
1554 371 my ExtUtils::ParseXS $pxs = shift;
1555  
1556 371 $self->SUPER::parse($pxs); # set file/line_no
1557 371 1;
1558 }
1559  
1560 371 ▶sub as_code {
Subroutine as_code: called CC=2 SCAR=6.9
1561 371 my __PACKAGE__ $self = shift;
1562 371 my ExtUtils::ParseXS $pxs = shift;
1563  
1564 # For each package FOO which has had at least one overloaded method
1565 # specified:
1566 # - create a stub XSUB in that package called nil;
1567 # - generate code to be added to the boot XSUB which links that XSUB
1568 # to the symbol table entry *{"FOO::()"}. This mimics the action in
1569 # overload::import() which creates the stub method as a quick way to
1570 # check whether an object is overloaded (including via inheritance),
1571 # by doing $self->can('()').
1572 # - Further down, we add a ${"FOO:()"} scalar containing the value of
1573 # 'fallback' (or undef if not specified).
1574 #
1575 # XXX In 5.18.0, this arrangement was changed in overload.pm, but hasn't
1576 # been updated here. The *() glob was being used for two different
1577 # purposes: a sub to do a quick check of overloadability, and a scalar
1578 # to indicate what 'fallback' value was specified (even if it wasn't
1579 # specified). The commits:
1580 # v5.16.0-87-g50853fa94f
1581 # v5.16.0-190-g3866ea3be5
1582 # v5.17.1-219-g79c9643d87
1583 # changed this so that overloadability is checked by &((, while fallback
1584 # is checked by $() (and not present unless specified by 'fallback'
1585 # as opposed to the always being present, but sometimes undef).
1586 # Except that, in the presence of fallback, &() is added too for
1587 # backcompat reasons (which I don't fully understand - DAPM).
1588 # See overload.pm's import() and OVERLOAD() methods for more detail.
1589 #
1590 # So this code (and the code in as_boot_code) needs updating to match.
1591  
1592 371 for my $package (sort keys %{$pxs->{map_overloaded_package_to_C_package}})
1593 {
1594 # make them findable with fetchmethod
1595 11 my $packid = $pxs->{map_overloaded_package_to_C_package}{$package};
1596 11 print $self->Q(<<"EOF");
1597 |XS_EUPXS(XS_${packid}_nil); /* prototype to pass -Wmissing-prototypes */
1598 |XS_EUPXS(XS_${packid}_nil)
1599 |{
1600 | dXSARGS;
1601 | PERL_UNUSED_VAR(items);
1602 | XSRETURN_EMPTY;
1603 |}
1604 |
1605 EOF
1606 }
1607 }
1608  
1609 371 ▶sub as_boot_code {
Subroutine as_boot_code: called CC=2 SCAR=6.9
1610 371 my __PACKAGE__ $self = shift;
1611 371 my ExtUtils::ParseXS $pxs = shift;
1612  
1613 371 my @early;
1614 371 for my $package (sort keys %{$pxs->{map_overloaded_package_to_C_package}})
1615 {
1616 11 my $packid = $pxs->{map_overloaded_package_to_C_package}{$package};
1617 11 push @early, $self->Q(<<"EOF");
1618 | /* Making a sub named "${package}::()" allows the package */
1619 | /* to be findable via fetchmethod(), and causes */
1620 | /* overload::Overloaded("$package") to return true. */
1621 | (void)newXS_deffile("${package}::()", XS_${packid}_nil);
1622 EOF
1623 }
1624 371 return \@early, [];
1625 }
1626  
1627  
1628 # ======================================================================
1629  
1630 package ExtUtils::ParseXS::Node::boot_xsub;
1631  
1632 # AST node representing C code that is emitted to create the boo XSUB.
1633 #
1634 # This node's parse() method doesn't actually consume any lines; the node
1635 # exists just for its as_code() method.
1636  
1637 28 ▶BEGIN { $build_subclass->(
Subroutine BEGIN: called CC=1 SCAR=0.0
1638 )};
1639  
1640 371 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
1641 371 my __PACKAGE__ $self = shift;
1642 371 my ExtUtils::ParseXS $pxs = shift;
1643  
1644 371 $self->SUPER::parse($pxs); # set file/line_no
1645 371 1;
1646 }
1647  
1648 371 ▶sub as_code {
Subroutine as_code: called CC=13 SCAR=25.7
1649 371 my __PACKAGE__ $self = shift;
1650 371 my ExtUtils::ParseXS $pxs = shift;
1651  
1652 # Walk the AST accumulating any boot code generated by
1653 # the various nodes' as_boot_code() methods.
1654 371 my ($early, $later) = $pxs->{AST}->as_boot_code($pxs);
1655  
1656 # Emit the boot_Foo__Bar() C function / XSUB
1657  
1658 371 print $self->Q(<<"EOF");
1659 |#ifdef __cplusplus
1660 |extern "C" $open_brace
1661 |#endif
1662 |XS_EXTERNAL(boot_$pxs->{MODULE_cname}); /* prototype to pass -Wmissing-prototypes */
1663 |XS_EXTERNAL(boot_$pxs->{MODULE_cname})
1664 |$open_brace
1665 |#if PERL_VERSION_LE(5, 21, 5)
1666 | dVAR; dXSARGS;
1667 |#else
1668 371 ▶ | dVAR; ${\($pxs->{VERSIONCHECK_value} ? 'dXSBOOTARGSXSAPIVERCHK;' : 'dXSBOOTARGSAPIVERCHK;')}
Branch: 1 branch
BranchTrueFalseTotal
$pxs->{'VERSIONCHECK_value'} ? : 370 1 371
1669 |#endif
1670 EOF
1671  
1672 # Declare a 'file' var for passing to newXS() and variants.
1673 #
1674 # If there is no $pxs->{seen_an_XSUB} then there are no xsubs
1675 # in this .xs so 'file' is unused, so silence warnings.
1676 #
1677 # 'file' can also be unused in other circumstances: in particular,
1678 # newXS_deffile() doesn't take a file parameter. So suppress any
1679 # 'unused var' warning always.
1680 #
1681 # Give it the correct 'const'ness: Under 5.8.x and lower, newXS() is
1682 # declared in proto.h as expecting a non-const file name argument. If
1683 # the wrong qualifier is used, it causes breakage with C++ compilers and
1684 # warnings with recent gcc.
1685  
1686 371 ▶ print $self->Q(<<"EOF") if $pxs->{seen_an_XSUB};
Branch: 1 branch
BranchTrueFalseTotal
if $pxs->{"seen_an_XSUB"} 364 7 371
1687 |#if PERL_VERSION_LE(5, 8, 999) /* PERL_VERSION_LT is 5.33+ */
1688 | char* file = __FILE__;
1689 |#else
1690 | const char* file = __FILE__;
1691 |#endif
1692 |
1693 | PERL_UNUSED_VAR(file);
1694 EOF
1695  
1696 # Emit assorted declarations
1697  
1698 371 print $self->Q(<<"EOF");
1699 |
1700 | PERL_UNUSED_VAR(cv); /* -W */
1701 | PERL_UNUSED_VAR(items); /* -W */
1702 EOF
1703  
1704 371 ▶ if ($pxs->{VERSIONCHECK_value}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pxs->{'VERSIONCHECK_value'}) { } 370 1 371
1705 370 print $self->Q(<<"EOF");
1706 |#if PERL_VERSION_LE(5, 21, 5)
1707 | XS_VERSION_BOOTCHECK;
1708 |# ifdef XS_APIVERSION_BOOTCHECK
1709 | XS_APIVERSION_BOOTCHECK;
1710 |# endif
1711 |#endif
1712 |
1713 EOF
1714 }
1715 else {
1716 1 print $self->Q(<<"EOF") ;
1717 |#if PERL_VERSION_LE(5, 21, 5) && defined(XS_APIVERSION_BOOTCHECK)
1718 | XS_APIVERSION_BOOTCHECK;
1719 |#endif
1720 |
1721 EOF
1722 }
1723  
1724 # Declare a 'cv' variable within a scope small enough to be visible
1725 # just to newXS() calls which need to do further processing of the cv:
1726 # in particular, when emitting one of:
1727 # XSANY.any_i32 = $value;
1728 # XSINTERFACE_FUNC_SET(cv, $value);
1729 #
1730 # It may be that all of the uses are guarded by false conditionals,
1731 # so make sure we don't get an unused variable warning.
1732  
1733 371 ▶ if ($pxs->{need_boot_cv}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pxs->{"need_boot_cv"}) 36 335 371
1734 36 print $self->Q(<<"EOF");
1735 | $open_brace
1736 | CV * cv;
1737 | PERL_UNUSED_VAR(cv);
1738 |
1739 EOF
1740 }
1741  
1742 # More overload stuff
1743  
1744 371 ▶ if (keys %{ $pxs->{map_overloaded_package_to_C_package} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$$pxs{"map_overloaded_package_to_C_package"};}) 10 361 371
1745 # Emit just once if any overloads:
1746 # Before 5.10, PL_amagic_generation used to need setting to at
1747 # least a non-zero value to tell perl that any overloading was
1748 # present.
1749 10 print $self->Q(<<"EOF");
1750 | /* register the overloading (type 'A') magic */
1751 |#if PERL_VERSION_LE(5, 8, 999) /* PERL_VERSION_LT is 5.33+ */
1752 | PL_amagic_generation++;
1753 |#endif
1754 EOF
1755  
1756 10 for my $package (
1757 10 sort keys %{ $pxs->{map_overloaded_package_to_C_package} })
1758 {
1759 # Emit once for each package with overloads:
1760 # Set ${'Foo::()'} to the fallback value for each overloaded
1761 # package 'Foo' (or undef if not specified).
1762 # But see the 'XXX' comments above about fallback and $().
1763  
1764 11 my $fallback = $pxs->{map_package_to_fallback_string}{$package};
1765 11 ▶ $fallback = 'UNDEF' unless defined $fallback;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fallback 7 4 11
1766 11 ▶ $fallback = $fallback eq 'TRUE' ? '&PL_sv_yes'
Branch: 2 branches
BranchTrueFalseTotal
$fallback eq 'TRUE' ? : 2 9 11
$fallback eq 'FALSE' ? : 1 8 9
1767 : $fallback eq 'FALSE' ? '&PL_sv_no'
1768 : '&PL_sv_undef';
1769  
1770 11 print $self->Q(<<"EOF");
1771 | /* The magic for overload gets a GV* via gv_fetchmeth as */
1772 | /* mentioned above, and looks in the SV* slot of it for */
1773 | /* the "fallback" status. */
1774 | sv_setsv(
1775 | get_sv( "${package}::()", TRUE ),
1776 | $fallback
1777 | );
1778 EOF
1779 }
1780 }
1781  
1782 # Emit any boot code associated with newXS().
1783  
1784 371 print @$early;
1785  
1786 # Emit closing scope for the 'CV *cv' declaration
1787  
1788 371 ▶ if ($pxs->{need_boot_cv}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pxs->{"need_boot_cv"}) 36 335 371
1789 36 print $self->Q(<<"EOF");
1790 | $close_brace
1791 EOF
1792 }
1793  
1794 # Emit any lines derived from BOOT: sections
1795  
1796 371 ▶ if (@$later) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$later) 14 357 371
1797 14 print $self->Q(<<"EOF");
1798 |
1799 | /* Initialisation Section */
1800 |
1801 EOF
1802  
1803 14 print @$later;
1804  
1805 print 'ExtUtils::ParseXS::CountLines'->end_marker, "\n"
1806 14 ▶ if $pxs->{config_WantLineNumbers};
Branch: 1 branch
BranchTrueFalseTotal
if $pxs->{"config_WantLineNumbers"} 14 0 14
1807  
1808 14 print $self->Q(<<"EOF");
1809 |
1810 | /* End of Initialisation Section */
1811 |
1812 EOF
1813 }
1814  
1815 # Emit code to call any UNITCHECK blocks and return true.
1816 # Since 5.22, this is been put into a separate function.
1817  
1818 371 print $self->Q(<<"EOF");
1819 |#if PERL_VERSION_LE(5, 21, 5)
1820 |# if PERL_VERSION_GE(5, 9, 0)
1821 | if (PL_unitcheckav)
1822 | call_list(PL_scopestack_ix, PL_unitcheckav);
1823 |# endif
1824 | XSRETURN_YES;
1825 |#else
1826 | Perl_xs_boot_epilog(aTHX_ ax);
1827 |#endif
1828 |$close_brace
1829 |
1830 |#ifdef __cplusplus
1831 |$close_brace
1832 |#endif
1833 EOF
1834 }
1835  
1836  
1837 # ======================================================================
1838  
1839 package ExtUtils::ParseXS::Node::xsub;
1840  
1841 # Process an entire XSUB definition
1842  
1843 28 ▶BEGIN { $build_subclass->(
Subroutine BEGIN: called CC=1 SCAR=0.0
1844 'decl', # Node::xsub_decl object holding this XSUB's declaration
1845  
1846 # Boolean flags: they indicate that at least one of each specified
1847 # keyword has been seen in this XSUB
1848 'seen_ALIAS',
1849 'seen_INTERFACE',
1850 'seen_INTERFACE_MACRO',
1851 'seen_PPCODE',
1852 'seen_PROTOTYPE',
1853 'seen_SCOPE',
1854  
1855 # These three fields indicate how many SVs are returned to the caller,
1856 # and so influence the emitting of 'EXTEND(n)', 'XSRETURN(n)', and
1857 # potentially, the value of n in 'ST(n) = ...'.
1858 #
1859 # XSRETURN_count_basic is 0 or 1 and indicates whether a basic return
1860 # value is pushed onto the stack. It is usually directly related to
1861 # whether the XSUB is declared void, but NO_RETURN and CODE_sets_ST0
1862 # can alter that.
1863 #
1864 # XSRETURN_count_extra indicates how many SVs will be returned in
1865 # addition the basic 0 or 1. These will be params declared as OUTLIST.
1866 #
1867 # CODE_sets_ST0 is a flag indicating that something within a CODE
1868 # block is doing 'ST(0) = ..' or similar. This is a workaround for
1869 # a bug: see the code comments "Horrible 'void' return arg count hack"
1870 # in Node::CODE::parse() for more details.
1871 'CODE_sets_ST0', # Bool
1872 'XSRETURN_count_basic', # Int
1873 'XSRETURN_count_extra', # Int
1874  
1875 # These maintain the alias parsing state across potentially multiple
1876 # ALIAS keywords and or lines:
1877  
1878 'map_alias_name_to_value', # Hash: maps seen alias names to their value
1879  
1880 'map_alias_value_to_name_seen_hash', # Hash of Hash of Bools:
1881 # indicates which alias names have been
1882 # used for each value.
1883  
1884 'alias_clash_hinted', # Bool: an ALIAS warn-hint has been emitted.
1885  
1886 # Maintain the INTERFACE parsing state across potentially multiple
1887 # INTERFACE keywords and/or lines:
1888  
1889 'map_interface_name_short_to_original', # Hash: for each INTERFACE
1890 # name, map the short (PREFIX removed) name
1891 # to the original name.
1892  
1893 # Maintain the OVERLOAD parsing state across potentially multiple
1894 # OVERLOAD keywords and/or lines:
1895  
1896 'overload_name_seen', # Hash of Bools: indicates overload method
1897 # names (such as '<=>') which have been
1898 # listed by OVERLOAD (for newXS boot code
1899 # emitting).
1900  
1901 # Maintain the ATTRS parsing state across potentially multiple
1902 # ATTRS keywords and or lines:
1903  
1904 'attributes', # Array of Strs: all ATTRIBUTE keywords
1905 # (possibly multiple space-separated
1906 # keywords per string).
1907  
1908 # INTERFACE_MACRO state
1909  
1910 'interface_macro', # Str: value of interface extraction macro.
1911 'interface_macro_set', # Str: value of interface setting macro.
1912  
1913 # PROTOTYPE value
1914  
1915 'prototype', # Str: is set to either the global PROTOTYPES
1916 # values (0 or 1), or to what's been
1917 # overridden for this XSUB with PROTOTYPE
1918 # "0": DISABLE
1919 # "1": ENABLE
1920 # "2": empty prototype
1921 # other: a specific prototype.
1922  
1923 # Misc
1924  
1925 'SCOPE_enabled', # Bool: "SCOPE: ENABLE" seen, in either the
1926 # file or XSUB part of the XS file
1927  
1928 'PACKAGE_name', # value of $pxs->{PACKAGE_name} at parse time
1929 'PACKAGE_C_name', # value of $pxs->{PACKAGE_C_name} at parse time
1930  
1931 )};
1932  
1933  
1934 463 ▶sub parse {
Subroutine parse: called CC=15 SCAR=27.1
1935 463 my __PACKAGE__ $self = shift;
1936 463 my ExtUtils::ParseXS $pxs = shift;
1937  
1938 463 $self->SUPER::parse($pxs); # set file/line_no
1939  
1940 # record what package we're in
1941 463 $self->{PACKAGE_name} = $pxs->{PACKAGE_name};
1942 463 $self->{PACKAGE_C_name} = $pxs->{PACKAGE_C_name};
1943  
1944 # Initially inherit the prototype behaviour for the XSUB from the
1945 # global PROTOTYPES default
1946 463 $self->{prototype} = $pxs->{PROTOTYPES_value};
1947  
1948 # inherit any SCOPE: value that immediately preceded the XSUB
1949 # declaration
1950 463 $self->{SCOPE_enabled} = $pxs->{file_SCOPE_enabled};
1951  
1952 # Parse the XSUB's declaration (return type, name, parameters)
1953  
1954 463 my $decl = ExtUtils::ParseXS::Node::xsub_decl->new();
1955 463 $self->{decl} = $decl;
1956 463 ▶ $decl->parse($pxs, $self)
Branch: 1 branch
BranchTrueFalseTotal
$decl->parse($pxs, $self) or return 458 2 460
1957 or return;
1958 458 push @{$self->{kids}}, $decl;
1959  
1960 # Check all the @{ $pxs->{line}} lines for balance: all the
1961 # #if, #else, #endif etc within the XSUB should balance out.
1962 458 ExtUtils::ParseXS::Utilities::check_conditional_preprocessor_statements();
1963  
1964 # ----------------------------------------------------------------
1965 # Each iteration of this loop will process 1 optional CASE: line,
1966 # followed by all the other blocks. In the absence of a CASE: line,
1967 # this loop is only iterated once.
1968 # ----------------------------------------------------------------
1969  
1970 458 my $num = 0; # the number of CASE+bodies seen
1971 458 my $seen_bare_xbody = 0; # seen a previous body without a CASE
1972 458 my $case_had_cond = 1; # the previous CASE had a condition
1973  
1974 # Repeatedly look for CASE or XSUB body.
1975 458 while (1) {
1976 # Parse a CASE statement if present.
1977 927 my ($case) =
1978 $self->parse_keywords(
1979 $pxs, $self, undef, # xbody not yet present so use undef
1980 1, # process maximum of one keyword
1981 "CASE",
1982 );
1983  
1984 927 ▶ if (defined $case) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $case) { } 41 886 927
1985 41 $case->{num} = ++$num;
1986  
1987 41 ▶ if ($seen_bare_xbody) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen_bare_xbody) 2 39 41
1988 2 $pxs->blurt("Error: no 'CASE:' at top of function");
1989 2 $seen_bare_xbody = 0;
1990 }
1991  
1992 41 ▶ unless ($case_had_cond) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($case_had_cond) 1 40 41
1993 1 $pxs->blurt("Error: 'CASE:' after unconditional 'CASE:'");
1994 }
1995 41 $case_had_cond = length $case->{cond};
1996 }
1997 else {
1998 886 $seen_bare_xbody = 1;
1999 886 ▶ if ($num++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num++) 445 441 886
2000 # After the first CASE+body, we should only encounter
2001 # further CASE+bodies or end-of-paragraph
2002 445 ▶ last unless @{$pxs->{line}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$pxs->{"line"};} 435 10 445
2003 10 my $l = $pxs->{line}[0];
2004 10 ▶ $pxs->death(
Branch: 1 branch
BranchTrueFalseTotal
$l =~ /^$ExtUtils::ParseXS::BLOCK_regexp/o ? : 9 1 10
2005 $l =~ /^$ExtUtils::ParseXS::BLOCK_regexp/o
2006 ? "Error: misplaced '$1:'"
2007 : qq{Error: junk at end of function: "$l"}
2008 );
2009 }
2010 }
2011  
2012 # Parse the XSUB's body
2013  
2014 482 my $xbody = ExtUtils::ParseXS::Node::xbody->new();
2015 482 ▶ $xbody->parse($pxs, $self)
Branch: 1 branch
BranchTrueFalseTotal
$xbody->parse($pxs, $self) or return 469 0 469
2016 or return;
2017  
2018 469 ▶ if (defined $case) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $case) { } 41 428 469
2019 # make the xbody a child of the CASE
2020 41 push @{$case->{kids}}, $xbody;
2021 41 $xbody = $case;
2022 }
2023 else {
2024 428 push @{$self->{kids}}, $xbody;
2025 }
2026 } # end while (@{ $pxs->{line} })
2027  
2028 # If any aliases have been declared, make the main sub name ix 0
2029 # if not specified.
2030  
2031 435 ▶ if ( $self->{map_alias_name_to_value}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"map_alias_name_to_value"} and keys %{$$self{"map_alias_name_to_value"};}) 20 415 435
Condition66%2/3
$self->{"map_alias_name_to_value"} and keys %{$$self{"map_alias_name_to_value"};}
!ll&&!rl&&r
415020
MC/DC50%1/2
$self->{"map_alias_name_to_value"} and keys %{$$self{"map_alias_name_to_value"};}
$self->{"map_alias_name_to_value"} keys %{$$self{"map_alias_name_to_value"};}
Truth table66%2/3
A and B
A= $self->{"map_alias_name_to_value"}B= keys %{$$self{"map_alias_name_to_value"};}
ABresult
0X0
100
111
2032 20 and keys %{ $self->{map_alias_name_to_value} })
2033 {
2034 20 my $pname = $self->{decl}{full_perl_name};
2035 $self->{map_alias_name_to_value}{$pname} = 0
2036 20 ▶ unless defined $self->{map_alias_name_to_value}{$pname};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"map_alias_name_to_value"}{$pname} 16 4 20
2037 }
2038  
2039 # per-XSUB sanity checks
2040  
2041 435 ▶ if ( $self->{map_interface_name_short_to_original}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"map_interface_name_short_to_original"} and $self->{"map_alias_name_to_value"}) 1 434 435
Condition100%3/3
$self->{"map_interface_name_short_to_original"} and $self->{"map_alias_name_to_value"}
!ll&&!rl&&r
418161
MC/DC100%2/2
$self->{"map_interface_name_short_to_original"} and $self->{"map_alias_name_to_value"}
$self->{"map_interface_name_short_to_original"} $self->{"map_alias_name_to_value"}
Truth table100%3/3
A and B
A= $self->{"map_interface_name_short_to_original"}B= $self->{"map_alias_name_to_value"}
ABresult
0X0
100
111
2042 && $self->{map_alias_name_to_value})
2043 {
2044 1 $pxs->blurt("Error: only one of ALIAS and INTERFACE can be used per XSUB");
2045 }
2046  
2047 435 1;
2048 }
2049  
2050  
2051 434 ▶sub as_code {
Subroutine as_code: called CC=16 SCAR=27.9
2052 434 my __PACKAGE__ $self = shift;
2053 434 my ExtUtils::ParseXS $pxs = shift;
2054  
2055 # ----------------------------------------------------------------
2056 # Emit initial C code for the XSUB
2057 # ----------------------------------------------------------------
2058  
2059 {
2060 434 my $extern = $self->{decl}{return_type}{extern_C}
2061 434 ▶ ? qq[extern "C"] : "";
Branch: 1 branch
BranchTrueFalseTotal
$self->{'decl'}{'return_type'}{'extern_C'} ? : 1 433 434
2062 434 my $cname = $self->{decl}{full_C_name};
2063  
2064 # Emit function header
2065 434 print $self->Q(<<"EOF");
2066 |$extern
2067 |XS_EUPXS(XS_$cname); /* prototype to pass -Wmissing-prototypes */
2068 |XS_EUPXS(XS_$cname)
2069 |$open_brace
2070 | dVAR; dXSARGS;
2071 EOF
2072 }
2073  
2074 434 ▶ print $self->Q(<<"EOF") if $self->{seen_ALIAS};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"seen_ALIAS"} 23 411 434
2075 | dXSI32;
2076 EOF
2077  
2078 434 ▶ if ($self->{seen_INTERFACE}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"seen_INTERFACE"}) 17 417 434
2079 17 my $type = $self->{decl}{return_type}{type};
2080 $type =~ tr/:/_/
2081 17 ▶ unless $pxs->{config_RetainCplusplusHierarchicalTypes};
Branch: 1 branch
BranchTrueFalseTotal
unless $pxs->{"config_RetainCplusplusHierarchicalTypes"} 17 0 17
2082 17 ▶ print $self->Q(<<"EOF") if $self->{seen_INTERFACE};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"seen_INTERFACE"} 17 0 17
2083 | dXSFUNCTION($type);
2084 EOF
2085 }
2086  
2087  
2088 {
2089 434 my $params = $self->{decl}{params};
2090 # the code to emit to determine whether the correct number of argument
2091 # have been passed
2092 my $condition_code =
2093 ExtUtils::ParseXS::set_cond($params->{seen_ellipsis},
2094 $params->{min_args},
2095 434 $params->{nargs});
2096  
2097 # "-except" cmd line switch
2098 434 ▶ print $self->Q(<<"EOF") if $pxs->{config_allow_exceptions};
Branch: 1 branch
BranchTrueFalseTotal
if $pxs->{"config_allow_exceptions"} 0 434 434
2099 | char errbuf[1024];
2100 | *errbuf = '\\0';
2101 EOF
2102  
2103 434 ▶ if ($condition_code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($condition_code) { } 430 4 434
2104 430 my $p = $params->usage_string();
2105 430 $p =~ s/"/\\"/g;
2106 430 print $self->Q(<<"EOF");
2107 | if ($condition_code)
2108 | croak_xs_usage(cv, "$p");
2109 EOF
2110 }
2111 else {
2112 # cv and items likely to be unused
2113 4 print $self->Q(<<"EOF");
2114 | PERL_UNUSED_VAR(cv); /* -W */
2115 | PERL_UNUSED_VAR(items); /* -W */
2116 EOF
2117 }
2118 }
2119  
2120 # gcc -Wall: if an XSUB has PPCODE, it is possible that none of ST,
2121 # XSRETURN or XSprePUSH macros are used. Hence 'ax' (setup by
2122 # dXSARGS) is unused.
2123 # XXX: could breakup the dXSARGS; into dSP;dMARK;dITEMS
2124 # but such a move could break third-party extensions
2125 434 ▶ print $self->Q(<<"EOF") if $self->{seen_PPCODE};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"seen_PPCODE"} 8 426 434
2126 | PERL_UNUSED_VAR(ax); /* -Wall */
2127 EOF
2128  
2129 434 ▶ print $self->Q(<<"EOF") if $self->{seen_PPCODE};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"seen_PPCODE"} 8 426 434
2130 | SP -= items;
2131 EOF
2132  
2133 # ----------------------------------------------------------------
2134 # Emit the main body of the XSUB (all the CASE statements + bodies
2135 # or a single body)
2136 # ----------------------------------------------------------------
2137  
2138 434 $_->as_code($pxs, $self) for @{$self->{kids}};
2139  
2140 # ----------------------------------------------------------------
2141 # All of the body of the XSUB (including all CASE variants) has now
2142 # been processed. Now emit any XSRETURN or similar, plus any closing
2143 # bracket.
2144 # ----------------------------------------------------------------
2145  
2146 434 ▶ print $self->Q(<<"EOF") if $pxs->{config_allow_exceptions};
Branch: 1 branch
BranchTrueFalseTotal
if $pxs->{"config_allow_exceptions"} 0 434 434
2147 | if (errbuf[0])
2148 | Perl_croak(aTHX_ errbuf);
2149 EOF
2150  
2151 # Emit XSRETURN(N) or XSRETURN_EMPTY. It's possible that the user's
2152 # CODE section rolled its own return, so this code may be
2153 # unreachable. So suppress any compiler warnings.
2154 # XXX Currently this is just for HP. Make more generic??
2155  
2156 # Suppress "statement is unreachable" warning on HPUX
2157 434 ▶ print "#if defined(__HP_cc) || defined(__HP_aCC)\n",
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "hpux" 0 434 434
2158 "#pragma diag_suppress 2128\n",
2159 "#endif\n"
2160 if $^O eq "hpux";
2161  
2162 434 ▶ unless ($self->{seen_PPCODE}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"seen_PPCODE"}) 426 8 434
2163 my $nret = $self->{XSRETURN_count_basic}
2164 426 + $self->{XSRETURN_count_extra};
2165  
2166 426 ▶ print $nret ? " XSRETURN($nret);\n"
Branch: 1 branch
BranchTrueFalseTotal
$nret ? : 248 178 426
2167 : " XSRETURN_EMPTY;\n";
2168 }
2169  
2170 # Suppress "statement is unreachable" warning on HPUX
2171 434 ▶ print "#if defined(__HP_cc) || defined(__HP_aCC)\n",
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "hpux" 0 434 434
2172 "#pragma diag_default 2128\n",
2173 "#endif\n"
2174 if $^O eq "hpux";
2175  
2176 # Emit final closing bracket for the XSUB.
2177 434 print "$close_brace\n\n";
2178 }
2179  
2180  
2181 # Return a list of boot code strings for the XSUB, including newXS()
2182 # call(s) plus any additional boot stuff like handling attributes or
2183 # storing an alias index in the XSUB's CV.
2184  
2185 434 ▶sub as_boot_code {
Subroutine as_boot_code: called CC=15 SCAR=27.1
2186 434 my __PACKAGE__ $self = shift;
2187 434 my ExtUtils::ParseXS $pxs = shift;
2188  
2189 # Depending on whether the XSUB has a prototype, work out how to
2190 # invoke one of the newXS() function variants. Set these:
2191 #
2192 434 my $newXS; # the newXS() variant to be called in the boot section
2193 my $file_arg; # an extra ', file' arg to be passed to newXS call
2194 434 my $proto_arg; # an extra e.g. ', "$@"' arg to be passed to newXS call
2195  
2196 434 my @code; # boot code for each alias etc
2197  
2198 434 $proto_arg = "";
2199  
2200 434 ▶ unless($self->{prototype}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'prototype'}) { } 42 392 434
2201 # no prototype
2202 392 $newXS = "newXS_deffile";
2203 392 $file_arg = "";
2204 }
2205 else {
2206 # needs prototype
2207 42 $newXS = "newXSproto_portable";
2208 42 $file_arg = ", file";
2209  
2210 42 ▶ if ($self->{prototype} eq 2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'prototype'} eq 2) { } 1 41 42
elsif ($self->{'prototype'} eq 1) { } 35 6 41
2211 # User has specified an empty prototype
2212 }
2213 elsif ($self->{prototype} eq 1) {
2214 # Protoype enabled, but to be auto-generated by us
2215 35 $proto_arg = $self->{decl}{params}->proto_string();
2216 35 $proto_arg =~ s{\\}{\\\\}g; # escape backslashes
2217 }
2218 else {
2219 # User has manually specified a prototype
2220 6 $proto_arg = $self->{prototype};
2221 }
2222  
2223 42 $proto_arg = qq{, "$proto_arg"};
2224 }
2225  
2226 # Now use those values to append suitable newXS() and other code
2227 # into @code, for later insertion into the boot sub.
2228  
2229 434 my $pname = $self->{decl}{full_perl_name};
2230 434 my $cname = $self->{decl}{full_C_name};
2231  
2232 434 ▶ if ( $self->{map_alias_name_to_value}
Branch: 4 branches
BranchTrueFalseTotal
if ($self->{'map_alias_name_to_value'} and keys %{$$self{'map_alias_name_to_value'};}) { } 20 414 434
elsif ($self->{'attributes'}) { } 4 410 414
elsif ($self->{'seen_INTERFACE'} or $self->{'seen_INTERFACE_MACRO'}) { } 16 394 410
elsif ($newXS eq 'newXS_deffile') { } 356 38 394
Condition66%2/3
$self->{"map_alias_name_to_value"} and keys %{$$self{"map_alias_name_to_value"};}
!ll&&!rl&&r
414020
MC/DC50%1/2
$self->{"map_alias_name_to_value"} and keys %{$$self{"map_alias_name_to_value"};}
$self->{"map_alias_name_to_value"} keys %{$$self{"map_alias_name_to_value"};}
Truth table66%2/3
A and B
A= $self->{"map_alias_name_to_value"}B= keys %{$$self{"map_alias_name_to_value"};}
ABresult
0X0
100
111
2233 20 and keys %{ $self->{map_alias_name_to_value} })
2234 {
2235 # For the main XSUB and for each alias name, generate a newXS() call
2236 # and 'XSANY.any_i32 = ix' line.
2237  
2238 20 foreach my $xname (sort keys
2239 20 %{ $self->{map_alias_name_to_value} })
2240 {
2241 66 my $value = $self->{map_alias_name_to_value}{$xname};
2242 66 push(@code, $self->Q(<<"EOF"));
2243 | cv = $newXS(\"$xname\", XS_$cname$file_arg$proto_arg);
2244 | XSANY.any_i32 = $value;
2245 EOF
2246 66 $pxs->{need_boot_cv} = 1;
2247 }
2248 }
2249 elsif ($self->{attributes}) {
2250 # Generate a standard newXS() call, plus a single call to
2251 # apply_attrs_string() call with the string of attributes.
2252 4 my $attrs = "@{$self->{attributes}}";
2253 4 push(@code, $self->Q(<<"EOF"));
2254 | cv = $newXS(\"$pname\", XS_$cname$file_arg$proto_arg);
2255 | apply_attrs_string("$self->{PACKAGE_name}", cv, "$attrs", 0);
2256 EOF
2257 4 $pxs->{need_boot_cv} = 1;
2258 }
2259 ▶ elsif ( $self->{seen_INTERFACE}
Condition66%2/3
$self->{"seen_INTERFACE"} or $self->{"seen_INTERFACE_MACRO"}
l!l&&r!l&&!r
160394
MC/DC50%1/2
$self->{"seen_INTERFACE"} or $self->{"seen_INTERFACE_MACRO"}
$self->{"seen_INTERFACE"} $self->{"seen_INTERFACE_MACRO"}
Truth table66%2/3
A or B
A= $self->{"seen_INTERFACE"}B= $self->{"seen_INTERFACE_MACRO"}
ABresult
1X1
011
000
2260 or $self->{seen_INTERFACE_MACRO})
2261 {
2262 # For each interface name, generate both a newXS() and
2263 # XSINTERFACE_FUNC_SET() call.
2264 16 foreach my $yname (sort keys
2265 16 %{ $self->{map_interface_name_short_to_original} })
2266 {
2267 20 my $value = $self->{map_interface_name_short_to_original}{$yname};
2268 20 ▶ $yname = "$self->{PACKAGE_name}\::$yname" unless $yname =~ /::/;
Branch: 1 branch
BranchTrueFalseTotal
unless $yname =~ /::/ 19 1 20
2269  
2270 20 my $macro = $self->{interface_macro_set};
2271 20 ▶ $macro = 'XSINTERFACE_FUNC_SET' unless defined $macro;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $macro 16 4 20
2272 20 push(@code, $self->Q(<<"EOF"));
2273 | cv = $newXS(\"$yname\", XS_$cname$file_arg$proto_arg);
2274 | $macro(cv,$value);
2275 EOF
2276 20 $pxs->{need_boot_cv} = 1;
2277 }
2278 }
2279 elsif ($newXS eq 'newXS_deffile'){
2280 # Modified default: generate a standard newXS() call; but
2281 # work around the CPAN 'P5NCI' distribution doing:
2282 # #undef newXS
2283 # #define newXS ;
2284 # by omitting the initial (void).
2285 # XXX DAPM 2024:
2286 # this branch was originally: "elsif ($newXS eq 'newXS')"
2287 # but when the standard name for the newXS variant changed in
2288 # xsubpp, it was changed here too. So this branch no longer actually
2289 # handles a workaround for '#define newXS ;'. I also don't
2290 # understand how just omitting the '(void)' fixed the problem.
2291 356 push(@code,
2292 " $newXS(\"$pname\", XS_$cname$file_arg$proto_arg);\n");
2293 }
2294 else {
2295 # Default: generate a standard newXS() call
2296 38 push(@code,
2297 " (void)$newXS(\"$pname\", XS_$cname$file_arg$proto_arg);\n");
2298 }
2299  
2300 # For every overload operator, generate an additional newXS()
2301 # call to add an alias such as "Foo::(<=>" for this XSUB.
2302  
2303 434 for my $operator (sort keys %{ $self->{overload_name_seen} })
2304 {
2305 91 my $overload = "$self->{PACKAGE_name}\::($operator";
2306 91 push(@code,
2307 " (void)$newXS(\"$overload\", XS_$cname$file_arg$proto_arg);\n");
2308 }
2309  
2310 434 return \@code, [];
2311 }
2312  
2313  
2314 # ======================================================================
2315  
2316 package ExtUtils::ParseXS::Node::xsub_decl;
2317  
2318 # Parse and store the complete declaration part of an XSUB, including
2319 # its parameters, name and return type.
2320  
2321 28 ▶BEGIN { $build_subclass->(
Subroutine BEGIN: called CC=1 SCAR=0.0
2322 'return_type', # ReturnType object representing e.g "NO_OUTPUT char *"
2323 'params', # Params object representing e.g "a, int b, c=0"
2324 'class', # Str: the 'Foo::Bar' part of an XSUB's name;
2325 # - if defined, this is a C++ method
2326 'name', # Str: the 'foo' XSUB name
2327 'full_perl_name', # Str: the 'Foo::Bar::foo' perl XSUB name
2328 'full_C_name', # Str: the 'Foo__Bar__foo' C XSUB name
2329 'is_const', # Bool: declaration had postfix C++ 'const' modifier
2330 )};
2331  
2332  
2333 # Parse the XSUB's declaration - i.e. return type, name and parameters.
2334  
2335 463 ▶sub parse {
Subroutine parse: called CC=10 SCAR=23.0
2336 463 my __PACKAGE__ $self = shift;
2337 463 my ExtUtils::ParseXS $pxs = shift;
2338 463 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
2339  
2340  
2341 463 $self->SUPER::parse($pxs); # set file/line_no
2342  
2343 # Parse return type
2344  
2345 463 my $return_type = ExtUtils::ParseXS::Node::ReturnType->new();
2346  
2347 463 ▶ $return_type->parse($pxs, $xsub)
Branch: 1 branch
BranchTrueFalseTotal
$return_type->parse($pxs, $xsub) or return 461 0 461
2348 or return;
2349  
2350 461 $self->{return_type} = $return_type;
2351 461 push @{$self->{kids}}, $return_type;
2352  
2353 # Decompose the function declaration: match a line like
2354 # Some::Class::foo_bar( args ) const ;
2355 # ----------- ------- ---- ----- --
2356 # $1 $2 $3 $4 $5
2357 #
2358 # where everything except $2 and $3 are optional and the 'const'
2359 # is for C++ functions.
2360  
2361 461 my $func_header = shift(@{ $pxs->{line} });
2362 461 ▶ $pxs->blurt("Error: cannot parse function definition from '$func_header'"),
Branch: 1 branch
BranchTrueFalseTotal
unless $func_header =~ /^(?:([\w:]*)::)?(\w+)\s*\(\s*(.*?)\s*\)\s*(const)?\s*(;\s*)?$/s 2 459 461
2363 return
2364 unless $func_header =~
2365 /^(?:([\w:]*)::)?(\w+)\s*\(\s*(.*?)\s*\)\s*(const)?\s*(;\s*)?$/s;
2366  
2367 459 my ($class, $name, $params_text, $const) = ($1, $2, $3, $4);
2368  
2369 459 ▶ if (defined $const and !defined $class) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $const and not defined $class) 1 458 459
Condition100%3/3
defined $const and not defined $class
!ll&&!rl&&r
45351
MC/DC100%2/2
defined $const and not defined $class
defined $const not defined $class
Truth table100%3/3
A and not(B)
A= defined $constB= not defined $class
ABresult
0X0
100
111
2370 1 $pxs->blurt(
2371 "Error: const modifier only allowed on XSUBs which are C++ methods");
2372 1 undef $const;
2373 }
2374  
2375 459 ▶ if ($return_type->{static} and !defined $class)
Branch: 1 branch
BranchTrueFalseTotal
if ($return_type->{"static"} and not defined $class) 2 457 459
Condition100%3/3
$return_type->{"static"} and not defined $class
!ll&&!rl&&r
45342
MC/DC100%2/2
$return_type->{"static"} and not defined $class
$return_type->{"static"} not defined $class
Truth table100%3/3
A and not(B)
A= $return_type->{"static"}B= not defined $class
ABresult
0X0
100
111
2376 {
2377 2 $pxs->Warn( "Warning: ignoring 'static' type modifier:"
2378 . " only valid with an XSUB name which includes a class");
2379 2 $return_type->{static} = 0;
2380 }
2381  
2382 459 (my $full_pname = $name) =~
2383 s/^($pxs->{PREFIX_pattern})?/$pxs->{PACKAGE_class}/;
2384  
2385 459 (my $clean_func_name = $name) =~ s/^$pxs->{PREFIX_pattern}//;
2386  
2387 459 my $full_cname = "$pxs->{PACKAGE_C_name}_$clean_func_name";
2388 459 ▶ $full_cname = $ExtUtils::ParseXS::VMS_SymSet->addsym($full_cname)
Branch: 1 branch
BranchTrueFalseTotal
if $ExtUtils::ParseXS::Is_VMS 0 459 459
2389 if $ExtUtils::ParseXS::Is_VMS;
2390  
2391 459 $self->{class} = $class;
2392 459 $self->{is_const} = defined $const;
2393 459 $self->{name} = $name;
2394 459 $self->{full_perl_name} = $full_pname;
2395 459 $self->{full_C_name} = $full_cname;
2396  
2397 # At this point, supposing that the input so far was:
2398 #
2399 # MODULE = ... PACKAGE = BAR::BAZ PREFIX = foo_
2400 # int
2401 # Some::Class::foo_bar(param1, param2, param3) const ;
2402 #
2403 # we should have:
2404 #
2405 # $self->{return_type} an object holding "int"
2406 # $self->{class} "Some::Class"
2407 # $self->{is_const} TRUE
2408 # $self->{name} "foo_bar"
2409 # $self->{full_perl_name} "BAR::BAZ::bar"
2410 # $self->{full_C_name} "BAR__BAZ_bar"
2411 # $params_text "param1, param2, param3"
2412  
2413 # ----------------------------------------------------------------
2414 # Process the XSUB's signature.
2415 #
2416 # Split $params_text into parameters, parse them, and store them as
2417 # Node::Param objects within the Node::Params object.
2418  
2419 459 my $params = $self->{params} = ExtUtils::ParseXS::Node::Params->new();
2420  
2421 459 ▶ $params->parse($pxs, $xsub, $params_text)
Branch: 1 branch
BranchTrueFalseTotal
$params->parse($pxs, $xsub, $params_text) or return 458 0 458
2422 or return;
2423 458 $self->{params} = $params;
2424 458 push @{$self->{kids}}, $params;
2425  
2426 # How many OUTLIST SVs get returned in addition to RETVAL
2427 $xsub->{XSRETURN_count_extra} =
2428 grep { defined $_->{in_out}
2429 757 ▶ && $_->{in_out} =~ /OUTLIST$/
Condition100%3/3
defined $_->{'in_out'} && $_->{'in_out'} =~ /OUTLIST$/
!ll&&!rl&&r
6951844
MC/DC100%2/2
defined $_->{'in_out'} && $_->{'in_out'} =~ /OUTLIST$/
defined $_->{'in_out'} $_->{'in_out'} =~ /OUTLIST$/
Truth table100%3/3
A && B
A= defined $_->{'in_out'}B= $_->{'in_out'} =~ /OUTLIST$/
ABresult
0X0
100
111
2430 }
2431 458 @{$self->{params}{kids}};
2432 458 1;
2433 }
2434  
2435  
2436 # ======================================================================
2437  
2438 package ExtUtils::ParseXS::Node::ReturnType;
2439  
2440 # Handle the 'return type' line at the start of an XSUB.
2441 # It mainly consists of the return type, but there are also
2442 # extra keywords to process, such as NO_RETURN.
2443  
2444 28 ▶BEGIN { $build_subclass->(
Subroutine BEGIN: called CC=1 SCAR=0.0
2445 'type', # Str: the XSUB's C return type
2446 'no_output', # Bool: saw 'NO_OUTPUT'
2447 'extern_C', # Bool: saw 'extern C'
2448 'static', # Bool: saw 'static'
2449 'use_early_targ', # Bool: emit an early dTARG for backcompat
2450 )};
2451  
2452  
2453 # Extract out the return type declaration from the start of an XSUB.
2454 # If the declaration and function name are on the same line, delete the
2455 # type part; else pop the first line.
2456  
2457 463 ▶sub parse {
Subroutine parse: called CC=12 SCAR=24.9
2458 463 my __PACKAGE__ $self = shift;
2459 463 my ExtUtils::ParseXS $pxs = shift;
2460 463 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
2461  
2462 463 $self->SUPER::parse($pxs); # set file/line_no
2463  
2464 # Whitespace-tidy the line containing the return type, plus possibly
2465 # the function name and arguments too.
2466 # XXX Tidying the latter was probably an unintended side-effect of
2467 # later allowing the return type and function to be on the same line.
2468  
2469 463 my $line = shift @{$pxs->{line}};
2470 463 $line = ExtUtils::Typemaps::tidy_type($line);
2471 463 my $type = $line;
2472  
2473 463 ▶ $self->{no_output} = 1 if $type =~ s/^NO_OUTPUT\s+//;
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ s/^NO_OUTPUT\s+// 10 453 463
2474  
2475 # Allow one-line declarations. This splits a single line like:
2476 # int foo(....)
2477 # into the two lines:
2478 # int
2479 # foo(...)
2480 #
2481 # Note that this splits both K&R-style 'foo(a, b)' and ANSI-style
2482 # 'foo(int a, int b)'. I don't know whether the former was intentional.
2483 # As of 5.40.0, the docs don't suggest that a 1-line K&R is legal. Was
2484 # added by 11416672a16, first appeared in 5.6.0.
2485 #
2486 # NB: $pxs->{config_allow_argtypes} is false if xsubpp was invoked
2487 # with -noargtypes
2488  
2489 67 unshift @{$pxs->{line}}, $2
2490 if $pxs->{config_allow_argtypes}
2491 463 ▶ and $type =~ s/^(.*?\w.*?) \s* \b (\w+\s*\(.*)/$1/sx;
Branch: 1 branch
BranchTrueFalseTotal
if $pxs->{"config_allow_argtypes"} and $type =~ s/^(.*?\w.*?) \s* \b (\w+\s*\(.*)/$1/sx 67 396 463
Condition100%3/3
$pxs->{"config_allow_argtypes"} and $type =~ s/^(.*?\w.*?) \s* \b (\w+\s*\(.*)/$1/sx
!ll&&!rl&&r
139567
MC/DC100%2/2
$pxs->{"config_allow_argtypes"} and $type =~ s/^(.*?\w.*?) \s* \b (\w+\s*\(.*)/$1/sx
$pxs->{"config_allow_argtypes"} $type =~ s/^(.*?\w.*?) \s* \b (\w+\s*\(.*)/$1/sx
Truth table100%3/3
A and B
A= $pxs->{"config_allow_argtypes"}B= $type =~ s/^(.*?\w.*?) \s* \b (\w+\s*\(.*)/$1/sx
ABresult
0X0
100
111
2492  
2493 # a function definition needs at least 2 lines
2494 463 ▶ unless (@{$pxs->{line}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$pxs->{"line"};}) 2 461 463
2495 2 ▶ if ($line =~ /^([A-Z][A-Z_]+):/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^([A-Z][A-Z_]+):/) 1 1 2
2496 # It's possibly a mistyped keyword: give a more specific
2497 # error message:
2498 1 $pxs->death("Error: unrecognised keyword '$1'");
2499 0 return;
2500 }
2501 # Generic error message:
2502 1 $pxs->deathHint("Error: unrecognised line: '$line'",
2503 "possible start of a truncated XSUB definition?");
2504 0 return;
2505 }
2506  
2507 461 ▶ $self->{extern_C} = 1 if $type =~ s/^extern "C"\s+//;
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ s/^extern "C"\s+// 1 460 461
2508 461 ▶ $self->{static} = 1 if $type =~ s/^static\s+//;
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ s/^static\s+// 6 455 461
2509 461 $self->{type} = $type;
2510  
2511 461 ▶ if ($type ne "void") {
Branch: 1 branch
BranchTrueFalseTotal
if ($type ne "void") 252 209 461
2512 # Set a flag indicating that, for backwards-compatibility reasons,
2513 # early dXSTARG should be emitted.
2514 # Recent code emits a dXSTARG in a tighter scope and under
2515 # additional circumstances, but some XS code relies on TARG
2516 # having been declared. So continue to declare it early under
2517 # the original circumstances.
2518 252 my $outputmap = $pxs->{typemaps_object}->get_outputmap(ctype => $type);
2519  
2520 252 ▶ if ( $pxs->{config_optimize}
Branch: 1 branch
BranchTrueFalseTotal
if ($pxs->{"config_optimize"} and $outputmap and $outputmap->targetable_legacy) 213 39 252
Condition83%5/6
$pxs->{"config_optimize"} and $outputmap and $outputmap->targetable_legacy
!ll&&!rl&&r
831213
$pxs->{"config_optimize"} and $outputmap
!ll&&!rl&&r
08244
MC/DC66%2/3
$pxs->{"config_optimize"} and $outputmap and $outputmap->targetable_legacy
$pxs->{"config_optimize"} $outputmap $outputmap->targetable_legacy
Truth table75%3/4
A and B and C
A= $pxs->{"config_optimize"}B= $outputmapC= $outputmap->targetable_legacy
ABCresult
0XX0
10X0
1100
1111
2521 and $outputmap
2522 and $outputmap->targetable_legacy)
2523 {
2524 213 $self->{use_early_targ} = 1;
2525 }
2526 }
2527  
2528 461 1;
2529 }
2530  
2531  
2532 # ======================================================================
2533  
2534 package ExtUtils::ParseXS::Node::Param;
2535  
2536 # Node subclass which holds the state of one XSUB parameter, based on the
2537 # just the XSUB's signature. See also the Node::IO_Param subclass, which
2538 # augments the parameter declaration with info from INPUT and OUTPUT
2539 # lines.
2540  
2541 28 ▶BEGIN { $build_subclass->(
Subroutine BEGIN: called CC=1 SCAR=0.0
2542 # values derived from the XSUB's signature
2543 'in_out', # Str: The IN/OUT/OUTLIST etc value (if any)
2544 'var', # Str: the name of the parameter
2545 'arg_num', # Int The arg number (starting at 1) mapped to this param
2546 'default', # Str: default value (if any)
2547 'default_usage', # Str: how to report default value in "usage:..." error
2548 'is_ansi', # Bool: param's type was specified in signature
2549 'is_length', # Bool: param is declared as 'length(foo)' in signature
2550 'length_param', # Obj: 'foo' param's matching 'length(foo)' parameter
2551 # node object, if any
2552 'len_name' , # Str: the 'foo' in 'length(foo)' in signature
2553 'is_synthetic', # Bool: var like 'THIS': we pretend it was in the sig
2554  
2555 # values derived from both the XSUB's signature and/or INPUT line
2556 'type', # Str: The C type of the parameter
2557 'no_init', # Bool: don't initialise the parameter
2558  
2559 # derived values calculated later
2560 'proto', # Str: overridden prototype char(s) (if any) from typemap
2561 )};
2562  
2563  
2564 # Parse a parameter. A parameter is of the general form:
2565 #
2566 # OUT char* foo = expression
2567 #
2568 # where:
2569 # IN/OUT/OUTLIST etc are only allowed under
2570 # $pxs->{config_allow_inout}
2571 #
2572 # a C type is only allowed under
2573 # $pxs->{config_allow_argtypes}
2574 #
2575 # foo can be a plain C variable name, or can be
2576 # length(foo) but only under $pxs->{config_allow_argtypes}
2577 #
2578 # = default default value - only allowed under
2579 # $pxs->{config_allow_argtypes}
2580  
2581 515 ▶sub parse {
Subroutine parse: called CC=26 SCAR=32.6
2582 515 my __PACKAGE__ $self = shift;
2583 515 my ExtUtils::ParseXS $pxs = shift;
2584 515 my $params = shift; # parent Params
2585 515 my $param_text = shift;
2586  
2587 515 $self->SUPER::parse($pxs); # set file/line_no
2588  
2589 # Decompose parameter into its components.
2590 # Note that $name can be either 'foo' or 'length(foo)'
2591  
2592 515 my ($out_type, $type, $name, $sp1, $sp2, $default) =
2593 $param_text =~
2594 /^
2595 (?:
2596 (IN|IN_OUT|IN_OUTLIST|OUT|OUTLIST)
2597 \b\s*
2598 )?
2599 (.*?) # optional type
2600 \s*
2601 \b
2602 ( \w+ # var
2603 | length\( \s*\w+\s* \) # length(var)
2604 )
2605 (?:
2606 (\s*) = (\s*) ( .*?) # default expr
2607 )?
2608 \s*
2609 $
2610 /x;
2611  
2612 515 ▶ unless (defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 7 508 515
2613 7 ▶ if ($param_text =~ /^ SV \s* \* $/x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param_text =~ /^ SV \s* \* $/x) 4 3 7
2614 # special-case SV* as a placeholder for backwards
2615 # compatibility.
2616 4 $self->{var} = 'SV *';
2617 4 return 1;
2618 }
2619 3 $pxs->blurt("Error: unparseable XSUB parameter: '$param_text'");
2620 3 return;
2621 }
2622  
2623 508 ▶ undef $type unless length($type) && $type =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless length $type and $type =~ /\S/ 148 360 508
Condition66%2/3
length $type and $type =~ /\S/
!ll&&!rl&&r
1480360
MC/DC50%1/2
length $type and $type =~ /\S/
length $type $type =~ /\S/
Truth table66%2/3
A and B
A= length $typeB= $type =~ /\S/
ABresult
0X0
100
111
2624 508 $self->{var} = $name;
2625  
2626 # Check for duplicates
2627  
2628 508 my $old_param = $params->{names}{$name};
2629 508 ▶ if ($old_param) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_param) 29 479 508
2630 # Normally a dup parameter is an error, but we allow RETVAL as
2631 # a real parameter, which overrides the synthetic one which
2632 # was added earlier if the return value isn't void.
2633 29 ▶ if ( $name eq 'RETVAL'
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq 'RETVAL' and $old_param->{'is_synthetic'} and not defined $old_param->{'arg_num'}) { } 16 13 29
Condition100%6/6
$name eq "RETVAL" and $old_param->{"is_synthetic"} and not defined $old_param->{"arg_num"}
!ll&&!rl&&r
11216
$name eq "RETVAL" and $old_param->{"is_synthetic"}
!ll&&!rl&&r
7418
MC/DC100%3/3
$name eq "RETVAL" and $old_param->{"is_synthetic"} and not defined $old_param->{"arg_num"}
$name eq "RETVAL" $old_param->{"is_synthetic"} not defined $old_param->{"arg_num"}
Truth table100%4/4
A and B and not(C)
A= $name eq "RETVAL"B= $old_param->{"is_synthetic"}C= not defined $old_param->{"arg_num"}
ABCresult
0XX0
10X0
1100
1111
2634 and $old_param->{is_synthetic}
2635 and !defined $old_param->{arg_num})
2636 {
2637 # RETVAL is currently fully synthetic. Now that it has
2638 # been declared as a parameter too, override any implicit
2639 # RETVAL declaration. Delete the original param from the
2640 # param list and later re-add it as a parameter in its
2641 # correct position.
2642 16 @{$params->{kids}} = grep $_ != $old_param, @{$params->{kids}};
2643 # If the param declaration includes a type, it becomes a
2644 # real parameter. Otherwise the param is kept as
2645 # 'semi-real' (synthetic, but with an arg_num) until such
2646 # time as it gets a type set in INPUT, which would remove
2647 # the synthetic/no_init.
2648 16 ▶ %$self = %$old_param unless defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 8 8 16
2649 }
2650 else {
2651 13 $pxs->blurt(
2652 "Error: duplicate definition of parameter '$name' ignored");
2653 13 return;
2654 }
2655 }
2656  
2657 # Process optional IN/OUT etc modifier
2658  
2659 495 my $orig_out_type = $out_type;
2660 495 ▶ if (defined $out_type) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $out_type) { } 73 422 495
2661 $pxs->blurt(
2662 "Error: parameter IN/OUT modifier not allowed under -noinout")
2663 73 ▶ unless $pxs->{config_allow_inout};
Branch: 1 branch
BranchTrueFalseTotal
unless $pxs->{"config_allow_inout"} 1 72 73
2664  
2665 73 ▶ $out_type = $out_type eq 'IN' ? '' : $out_type;
Branch: 1 branch
BranchTrueFalseTotal
$out_type eq 'IN' ? : 7 66 73
2666 }
2667 else {
2668 422 $out_type = '';
2669 }
2670  
2671 # Process optional type
2672  
2673 495 ▶ if (defined($type) && !$pxs->{config_allow_argtypes}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type and not $pxs->{"config_allow_argtypes"}) 2 493 495
Condition100%3/3
defined $type and not $pxs->{"config_allow_argtypes"}
!ll&&!rl&&r
1413522
MC/DC100%2/2
defined $type and not $pxs->{"config_allow_argtypes"}
defined $type not $pxs->{"config_allow_argtypes"}
Truth table100%3/3
A and not(B)
A= defined $typeB= not $pxs->{"config_allow_argtypes"}
ABresult
0X0
100
111
2674 2 $pxs->blurt("Error: parameter type not allowed under -noargtypes");
2675 2 undef $type;
2676 }
2677  
2678 # Process 'length(foo)' pseudo-parameter
2679  
2680 495 my $len_name;
2681  
2682 495 ▶ if ($name =~ /^length\( \s* (\w+) \s* \)\z/x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^length\( \s* (\w+) \s* \)\z/x) 34 461 495
2683 $pxs->blurt( "Error: length() pseudo-parameter not allowed "
2684 . "under -noargtypes")
2685 34 ▶ unless $pxs->{config_allow_argtypes};
Branch: 1 branch
BranchTrueFalseTotal
unless $pxs->{"config_allow_argtypes"} 1 33 34
2686  
2687 34 $len_name = $1;
2688  
2689 34 ▶ if (defined $default) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $default) 2 32 34
2690 2 $pxs->blurt( "Error: default value not allowed on "
2691 . "length() parameter '$len_name'");
2692 2 undef $default;
2693 }
2694 34 ▶ unless (defined $type) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 2 32 34
2695 2 $pxs->blurt(
2696 "Error: length($len_name) doesn't have a type specified");
2697 2 $type = 'STRLEN'; # stop cascading problems while
2698 # blurting the rest of the file
2699 }
2700  
2701 34 ▶ if (defined $orig_out_type) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $orig_out_type) 5 29 34
2702 # Ban IN_OUT etc with length().
2703 5 $pxs->blurt( "Error: '$orig_out_type' modifier can't be used"
2704 . " with length($len_name)");
2705 5 $out_type = ''; # avoid cascading errors
2706 }
2707  
2708 34 $self->{no_init} = 1;
2709 34 $self->{is_length} = 1;
2710 34 $self->{len_name} = $len_name;
2711 # This is the C variable which will have it's
2712 # value set to the length of the string,
2713 # then used as an arg in an autocall:
2714 34 $self->{var} = "XSauto_length_of_$len_name";
2715  
2716 # Note that cross-checking with the foo parameter associated with
2717 # length(foo) is done near the end of Node::Params::parse(), after
2718 # all params have been parsed.
2719 }
2720  
2721 # Mark ANSI params: those which have a type (including 'length(s)')
2722 # and which thus don't need a matching INPUT line.
2723  
2724 495 ▶ if (defined $type) { # 'int foo' or 'int length(foo)'
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type) 354 141 495
2725 354 $self->{type} = $type;
2726 354 $self->{is_ansi} = 1;
2727 }
2728  
2729 495 ▶ $self->{in_out} = $out_type if length $out_type;
Branch: 1 branch
BranchTrueFalseTotal
if length $out_type 62 433 495
2730 495 ▶ $self->{no_init} = 1 if $out_type =~ /^OUT/;
Branch: 1 branch
BranchTrueFalseTotal
if $out_type =~ /^OUT/ 46 449 495
2731  
2732 # Process the default expression, including making the text
2733 # to be used in "usage: ..." error messages.
2734  
2735 495 my $report_def = '';
2736 495 ▶ if (defined $default) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $default) 37 458 495
2737 37 ▶ $pxs->death("Error: missing default value expression for '$name'")
Branch: 1 branch
BranchTrueFalseTotal
unless $default =~ /\S/ 1 36 37
2738 unless $default =~ /\S/;
2739  
2740 # The default expression for reporting usage. For backcompat,
2741 # sometimes preserve the spaces either side of the '='
2742 36 ▶ $report_def = ($self->{is_ansi} ? '' : $sp1)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'is_ansi'} ? : 27 9 36
2743 . "=$sp2$default";
2744 36 $self->{default_usage} = $report_def;
2745 36 $self->{default} = $default;
2746 }
2747  
2748 494 1;
2749 }
2750  
2751  
2752 # Set the 'proto' field of the param. This is based on the value, if any,
2753 # of the proto method of the typemap for that param's type. It will
2754 # typically be a single character like '$'.
2755 #
2756 # Note that params can have different types (and thus different proto
2757 # chars) in different CASE branches.
2758  
2759 785 ▶sub set_proto {
Subroutine set_proto: called CC=6 SCAR=17.9
2760 785 my __PACKAGE__ $self = shift;
2761 785 my ExtUtils::ParseXS $pxs = shift;
2762  
2763 # only needed for real args that the caller may pass.
2764 785 ▶ return unless $self->{arg_num};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"arg_num"} 328 457 785
2765 457 my $type = $self->{type};
2766 457 ▶ return unless defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 36 421 457
2767 421 my $typemap = $pxs->{typemaps_object}->get_typemap(ctype => $type);
2768 421 ▶ return unless defined $typemap;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $typemap 7 414 421
2769 414 my $p = $typemap->proto;
2770 414 ▶ return unless defined $p && length $p;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p and length $p 410 4 414
Condition66%2/3
defined $p and length $p
!ll&&!rl&&r
04104
MC/DC50%1/2
defined $p and length $p
defined $p length $p
Truth table66%2/3
A and B
A= defined $pB= length $p
ABresult
0X0
100
111
2771 4 $self->{proto} = $p;
2772 }
2773  
2774  
2775 # ======================================================================
2776  
2777 package ExtUtils::ParseXS::Node::IO_Param;
2778  
2779 # Subclass of Node::Param which holds the state of one XSUB parameter,
2780 # based on the XSUB's signature, but also augmented by info from INPUT or
2781 # OUTPUT lines
2782  
2783 28 ▶BEGIN { $build_subclass->(-parent => 'Param',
Subroutine BEGIN: called CC=1 SCAR=0.0
2784 # values derived from the XSUB's INPUT line
2785  
2786 'init_op', # Str: initialisation type: one of =/+/;
2787 'init', # Str: initialisation template code
2788 'is_addr', # Bool: INPUT var declared as '&foo'
2789 'is_alien', # Bool: var declared in INPUT line, but not in signature
2790 'in_input', # Bool: the parameter has appeared in an INPUT statement
2791 'defer', # Str: deferred initialisation template code
2792  
2793 # values derived from the XSUB's OUTPUT line
2794 #
2795 'in_output', # Bool: the parameter has appeared in an OUTPUT statement
2796 'do_setmagic', # Bool: 'SETMAGIC: ENABLE' was active for this parameter
2797 'output_code', # Str: the optional setting-code for this parameter
2798  
2799 # ArrayRefs: results of looking up typemaps (which are done in the
2800 # parse phase, as the typemap definitions can in theory change
2801 # further down in the XS file). For now these just store
2802 # uninterpreted, the list returned by the call to
2803 # lookup_input_typemap() etc, for later use by the as_input_code()
2804 # etc methods.
2805 #
2806 'input_typemap_vals', # result of lookup_input_typemap()
2807 'output_typemap_vals', # result of lookup_output_typemap(...)
2808 'output_typemap_vals_outlist', # result of lookup_output_typemap(..., n)
2809 )};
2810  
2811  
2812 # Given a param with known type etc, extract its typemap INPUT template
2813 # and also create a hash of vars that can be used to eval that template.
2814 # An undef returned hash ref signifies that the returned template string
2815 # doesn't need to be evalled.
2816 #
2817 # Returns ($expr, $eval_vars, $is_template)
2818 # or empty list on failure.
2819 #
2820 # $expr: text like '$var = SvIV($arg)'
2821 # $eval_vars: hash ref like { var => 'foo', arg => 'ST(0)', ... }
2822 # $is_template: $expr has '$arg' etc and needs evalling
2823  
2824 751 ▶sub lookup_input_typemap {
Subroutine lookup_input_typemap: called CC=16 SCAR=27.8
2825 751 my __PACKAGE__ $self = shift;
2826 751 my ExtUtils::ParseXS $pxs = shift;
2827 751 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
2828 751 my $xbody = shift;
2829  
2830 my ($type, $arg_num, $var, $init, $no_init, $default)
2831 751 = @{$self}{qw(type arg_num var init no_init default)};
2832 751 my $arg = $pxs->ST($arg_num);
2833  
2834 # whitespace-tidy the type
2835 751 $type = ExtUtils::Typemaps::tidy_type($type);
2836  
2837 # Specify the environment for when the initialiser template is evaled.
2838 # Only the common ones are specified here. Other fields may be added
2839 # later.
2840 my $eval_vars = {
2841 type => $type,
2842 var => $var,
2843 num => $arg_num,
2844 arg => $arg,
2845 alias => $xsub->{seen_ALIAS},
2846 func_name => $xsub->{decl}{name},
2847 full_perl_name => $xsub->{decl}{full_perl_name},
2848 full_C_name => $xsub->{decl}{full_C_name},
2849 Package => $xsub->{PACKAGE_name},
2850 751 };
2851  
2852 # The type looked up in the eval is Foo__Bar rather than Foo::Bar
2853 $eval_vars->{type} =~ tr/:/_/
2854 751 ▶ unless $pxs->{config_RetainCplusplusHierarchicalTypes};
Branch: 1 branch
BranchTrueFalseTotal
unless $pxs->{"config_RetainCplusplusHierarchicalTypes"} 751 0 751
2855  
2856 751 my $init_template;
2857  
2858 751 ▶ if (defined $init) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $init) { } 13 738 751
elsif ($no_init) { } 338 400 738
2859 # Use the supplied code template rather than getting it from the
2860 # typemap
2861  
2862 13 ▶ $pxs->death(
Branch: 1 branch
BranchTrueFalseTotal
if $no_init 0 13 13
2863 "Internal error: ExtUtils::ParseXS::Node::Param::as_code(): "
2864 . "both init and no_init supplied")
2865 if $no_init;
2866  
2867 13 $eval_vars->{init} = $init;
2868 13 $init_template = "\$var = $init";
2869 }
2870 elsif ($no_init) {
2871 # don't add initialiser
2872 338 $init_template = "";
2873 }
2874 else {
2875 # Get the initialiser template from the typemap
2876  
2877 400 my $typemaps = $pxs->{typemaps_object};
2878  
2879 # Normalised type ('Foo *' becomes 'FooPtr): one of the valid vars
2880 # which can appear within a typemap template.
2881 400 (my $ntype = $type) =~ s/\s*\*/Ptr/g;
2882  
2883 # $subtype is really just for the T_ARRAY / DO_ARRAY_ELEM code below,
2884 # where it's the type of each array element. But it's also passed to
2885 # the typemap template (although undocumented and virtually unused).
2886 400 (my $subtype = $ntype) =~ s/(?:Array)?(?:Ptr)?$//;
2887  
2888 # look up the TYPEMAP entry for this C type and grab the corresponding
2889 # XS type name (e.g. $type of 'char *' gives $xstype of 'T_PV'
2890 400 my $typemap = $typemaps->get_typemap(ctype => $type);
2891 400 ▶ if (not $typemap) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($typemap) 3 397 400
2892 3 $pxs->report_typemap_failure($typemaps, $type);
2893 3 return;
2894 }
2895 397 my $xstype = $typemap->xstype;
2896  
2897 # An optimisation: for the typemaps which check that the dereferenced
2898 # item is blessed into the right class, skip the test for DESTROY()
2899 # methods, as more or less by definition, DESTROY() will be called
2900 # on an object of the right class. Basically, for T_foo_OBJ, use
2901 # T_foo_REF instead. T_REF_IV_PTR was added in v5.22.0.
2902 $xstype =~ s/OBJ$/REF/ || $xstype =~ s/^T_REF_IV_PTR$/T_PTRREF/
2903 397 ▶ if $xsub->{decl}{name} =~ /DESTROY$/;
Branch: 1 branch
BranchTrueFalseTotal
if $xsub->{"decl"}{"name"} =~ /DESTROY$/ 1 396 397
Condition50%3/6
$xsub->{'decl'}{'name'} =~ /DESTROY$/ && ($xstype =~ s/OBJ$/REF/ || $xstype =~ s/^T_REF_IV_PTR$/T_PTRREF/)
!ll&&!rl&&r
39610
$xstype =~ s/OBJ$/REF/ or $xstype =~ s/^T_REF_IV_PTR$/T_PTRREF/
l!l&&r!l&&!r
010
MC/DC0%0/3
$xsub->{'decl'}{'name'} =~ /DESTROY$/ && ($xstype =~ s/OBJ$/REF/ || $xstype =~ s/^T_REF_IV_PTR$/T_PTRREF/)
$xsub->{'decl'}{'name'} =~ /DESTROY$/ $xstype =~ s/OBJ$/REF/ $xstype =~ s/^T_REF_IV_PTR$/T_PTRREF/
Truth table0%0/4
A && B or C
A= $xsub->{'decl'}{'name'} =~ /DESTROY$/B= $xstype =~ s/OBJ$/REF/C= $xstype =~ s/^T_REF_IV_PTR$/T_PTRREF/
ABCresult
0XX0
1000
11X1
1011
2904  
2905 # Get the ExtUtils::Typemaps::InputMap object associated with the
2906 # xstype. This contains the template of the code to be embedded,
2907 # e.g. 'SvPV_nolen($arg)'
2908 397 my $inputmap = $typemaps->get_inputmap(xstype => $xstype);
2909 397 ▶ if (not defined $inputmap) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $inputmap) 3 394 397
2910 3 $pxs->blurt("Error: no INPUT definition for type '$type', typekind '$xstype' found");
2911 3 return;
2912 }
2913  
2914 # Get the text of the template, with a few transformations to make it
2915 # work better with fussy C compilers. In particular, strip trailing
2916 # semicolons and remove any leading white space before a '#'.
2917 394 my $expr = $inputmap->cleaned_code;
2918  
2919 394 my $argoff = $arg_num - 1;
2920  
2921 # Process DO_ARRAY_ELEM. This is an undocumented hack that makes the
2922 # horrible T_ARRAY typemap work. "DO_ARRAY_ELEM" appears as a token
2923 # in the INPUT and OUTPUT code for for T_ARRAY, within a "for each
2924 # element" loop, and the purpose of this branch is to substitute the
2925 # token for some real code which will process each element, based
2926 # on the type of the array elements (the $subtype).
2927 #
2928 # Note: This gruesome bit either needs heavy rethinking or
2929 # documentation. I vote for the former. --Steffen, 2011
2930 # Seconded, DAPM 2024.
2931 394 ▶ if ($expr =~ /\bDO_ARRAY_ELEM\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /\bDO_ARRAY_ELEM\b/) 6 388 394
2932 6 my $subtypemap = $typemaps->get_typemap(ctype => $subtype);
2933 6 ▶ if (not $subtypemap) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subtypemap) 1 5 6
2934 1 $pxs->report_typemap_failure($typemaps, $subtype);
2935 1 return;
2936 }
2937  
2938 5 my $subinputmap =
2939 $typemaps->get_inputmap(xstype => $subtypemap->xstype);
2940 5 ▶ if (not $subinputmap) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subinputmap) 1 4 5
2941 1 $pxs->blurt("Error: no INPUT definition for subtype "
2942 . "'$subtype', typekind '"
2943 . $subtypemap->xstype . "' found");
2944 1 return;
2945 }
2946  
2947 4 my $subexpr = $subinputmap->cleaned_code;
2948 4 $subexpr =~ s/\$type/\$subtype/g;
2949 4 $subexpr =~ s/ntype/subtype/g;
2950 4 $subexpr =~ s/\$arg/ST(ix_$var)/g;
2951 4 $subexpr =~ s/\n\t/\n\t\t/g;
2952 4 $subexpr =~ s/is not of (.*\")/[arg %d] is not of $1, ix_$var + 1/g;
2953 4 $subexpr =~ s/\$var/${var}\[ix_$var - $argoff]/;
2954 4 $expr =~ s/\bDO_ARRAY_ELEM\b/$subexpr/;
2955 }
2956  
2957 392 ▶ if ($expr =~ m#/\*.*scope.*\*/#i) { # "scope" in C comments
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ m[/\*.*scope.*\*/]i) 1 391 392
2958 1 $xsub->{SCOPE_enabled} = 1;
2959 }
2960  
2961 # Specify additional environment for when a template derived from a
2962 # *typemap* is evalled.
2963 392 @$eval_vars{qw(ntype subtype argoff)} = ($ntype, $subtype, $argoff);
2964 392 $init_template = $expr;
2965  
2966 # For a parameter foo, if length(foo) was also declared as a
2967 # pseudo-parameter, then try to modify the normal typemap, which
2968 # we would expect to contain SvPV_nolen(...) or similar, into this
2969 # form instead: SvPV(..., STRLEN_length_of_foo).
2970 # Croak if this isn't possible.
2971 # Just accept the typemap as-is if it already contains the correct
2972 # STRLEN_length_of_ entry.
2973  
2974 392 ▶ if ($self->{length_param}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"length_param"}) 29 363 392
2975 29 ▶ if ($expr =~ /\bSTRLEN_length_of_\$var\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /\bSTRLEN_length_of_\$var\b/) { } 1 28 29
2976 # leave as-is
2977 }
2978 else {
2979 28 ▶ unless ($expr =~
Branch: 1 branch
BranchTrueFalseTotal
unless ($expr =~ s/ \b (SvPV\w*)_nolen(\w*) \( \s*\$arg\s* \) /$1$2(\$arg, STRLEN_length_of_\$var)/x) 2 26 28
2980 s{ \b
2981 (SvPV\w*)_nolen(\w*)
2982 \(
2983 \s*\$arg\s*
2984 \)
2985 }
2986 {$1$2(\$arg, STRLEN_length_of_\$var)}x
2987 ) {
2988 2 $pxs->deathHint(
2989 "Error: can't modify input typemap for"
2990 . " length($self->{var})",
2991 <<EOF);
2992 The parameter '$self->{var}' has an associated length($self->{var})
2993 pseudo-parameter. In cases like this, the XS parser attempts to modify
2994 a typemap entry such as
2995  
2996 \$var = (\$type)SvPV_nolen(\$arg)
2997  
2998 into a similar one which also sets a length, such as
2999  
3000 \$var = (\$type)SvPV(\$arg, STRLEN_length_of_\$var)
3001  
3002 In this case, the following typemap did not contain a recognised
3003 SVPV_nolen() variant:
3004  
3005 $expr
3006 EOF
3007 }
3008 }
3009 }
3010  
3011 390 $init_template = $expr;
3012 }
3013  
3014 741 return ($init_template, $eval_vars, 1);
3015 }
3016  
3017  
3018  
3019 # Given a param with known type etc, extract its typemap OUTPUT template
3020 # and also create a hash of vars that can be used to eval that template.
3021 # An undef returned hash ref signifies that the returned template string
3022 # doesn't need to be evalled.
3023 # $out_num, if defined, signifies that this lookup is for an OUTLIST param
3024 #
3025 # Returns ($expr, $eval_vars, $is_template, $saw_DAE)
3026 # or empty list on failure.
3027 #
3028 # $expr: text like 'sv_setiv($arg, $var)'
3029 # $eval_vars: hash ref like { var => 'foo', arg => 'ST(0)', ... }
3030 # $is_template: $expr has '$arg' etc and needs evalling
3031 # $saw_DAE: DO_ARRAY_ELEM was encountered
3032 #
3033  
3034 318 ▶sub lookup_output_typemap {
Subroutine lookup_output_typemap: called CC=19 SCAR=29.4
3035 318 my __PACKAGE__ $self = shift;
3036 318 my ExtUtils::ParseXS $pxs = shift;
3037 318 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
3038 318 my $xbody = shift;
3039 318 my $out_num = shift;
3040  
3041 my ($type, $num, $var, $do_setmagic, $output_code)
3042 318 = @{$self}{qw(type arg_num var do_setmagic output_code)};
3043  
3044 # values to return
3045 318 my ($expr, $eval_vars, $is_template, $saw_DAE);
3046 318 $is_template = 1;
3047  
3048 318 ▶ if ($var eq 'RETVAL') {
Branch: 1 branch
BranchTrueFalseTotal
if ($var eq "RETVAL") 229 89 318
3049 # Do some preliminary RETVAL-specific checks and settings.
3050  
3051 # Only OUT/OUTPUT vars (which update one of the passed args) should be
3052 # calling set magic; RETVAL and OUTLIST should be setting the value of
3053 # a fresh mortal or TARG. Note that a param can be both OUTPUT and
3054 # OUTLIST - the value of $do_setmagic only applies to its use as an
3055 # OUTPUT (updating) value.
3056  
3057 229 ▶ $pxs->death("Internal error: do set magic requested on RETVAL")
Branch: 1 branch
BranchTrueFalseTotal
if $do_setmagic 0 229 229
3058 if $do_setmagic;
3059  
3060 # RETVAL normally has an undefined arg_num, although it can be
3061 # set to a real index if RETVAL is also declared as a parameter.
3062 # But when returning its value, it's always stored at ST(0).
3063 229 $num = 1;
3064  
3065 # It is possible for RETVAL to have multiple types, e.g.
3066 # int
3067 # foo(long RETVAL)
3068 #
3069 # In the above, 'long' is used for the RETVAL C var's declaration,
3070 # while 'int' is used to generate the return code (for backwards
3071 # compatibility).
3072 229 $type = $xsub->{decl}{return_type}{type};
3073 }
3074  
3075 # ------------------------------------------------------------------
3076 # Do initial processing of $type, including creating various derived
3077 # values
3078  
3079 318 ▶ unless (defined $type) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 3 315 318
3080 3 $pxs->blurt("Error: can't determine output type for '$var'");
3081 3 return;
3082 }
3083  
3084 # $ntype: normalised type ('Foo *' becomes 'FooPtr' etc): one of the
3085 # valid vars which can appear within a typemap template.
3086 315 (my $ntype = $type) =~ s/\s*\*/Ptr/g;
3087 315 $ntype =~ s/\(\)//g;
3088  
3089 # $subtype is really just for the T_ARRAY / DO_ARRAY_ELEM code below,
3090 # where it's the type of each array element. But it's also passed to
3091 # the typemap template (although undocumented and virtually unused).
3092 # Basically for a type like FooArray or FooArrayPtr, the subtype is Foo.
3093 315 (my $subtype = $ntype) =~ s/(?:Array)?(?:Ptr)?$//;
3094  
3095 # whitespace-tidy the type
3096 315 $type = ExtUtils::Typemaps::tidy_type($type);
3097  
3098 # The type as supplied to the eval is Foo__Bar rather than Foo::Bar
3099 315 my $eval_type = $type;
3100 $eval_type =~ tr/:/_/
3101 315 ▶ unless $pxs->{config_RetainCplusplusHierarchicalTypes};
Branch: 1 branch
BranchTrueFalseTotal
unless $pxs->{"config_RetainCplusplusHierarchicalTypes"} 315 0 315
3102  
3103 # We can be called twice for the same variable: once to update the
3104 # original arg (via an entry in OUTPUT) and once to push the param's
3105 # value (via OUTLIST). When doing the latter, any override code on an
3106 # OUTPUT line should not be used.
3107 315 ▶ undef $output_code if defined $out_num;
Branch: 1 branch
BranchTrueFalseTotal
if defined $out_num 47 268 315
3108  
3109 # ------------------------------------------------------------------
3110 # Find the template code (pre any eval) and store it in $expr.
3111 # This is typically obtained via a typemap lookup, but can be
3112 # overridden. Also set vars ready for evalling the typemap template.
3113  
3114 315 my $outputmap;
3115 315 my $typemaps = $pxs->{typemaps_object};
3116  
3117 315 ▶ if (defined $output_code) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $output_code) { } 11 304 315
elsif ($type =~ /^array\(([^,]*),(.*)\)/) { } 4 300 304
3118 # An override on an OUTPUT line: use that instead of the typemap.
3119 # Note that we don't set $expr here, because $expr holds a template
3120 # string pre-eval, while OUTPUT override code is *not*
3121 # template-expanded, so $output_code is effectively post-eval code.
3122 11 $is_template = 0;
3123 11 $expr = $output_code;
3124 }
3125 elsif ($type =~ /^array\(([^,]*),(.*)\)/) {
3126 # Specially handle the implicit array return type, "array(type, nlelem)"
3127 # rather than using a typemap entry. It returns a string SV whose
3128 # buffer is a copy of $var, which it assumes is a C array of
3129 # type 'type' with 'nelem' elements.
3130  
3131 4 my ($atype, $nitems) = ($1, $2);
3132  
3133 4 ▶ if ($var ne 'RETVAL') {
Branch: 1 branch
BranchTrueFalseTotal
if ($var ne "RETVAL") 2 2 4
3134 # This special type is intended for use only as the return type of
3135 # an XSUB
3136 2 ▶ $pxs->blurt( "Error: can't use array(type,nitems) type for "
Branch: 1 branch
BranchTrueFalseTotal
defined $out_num ? : 1 1 2
3137 . (defined $out_num ? "OUTLIST" : "OUT")
3138 . " parameter");
3139 2 return;
3140 }
3141  
3142 $expr =
3143 2 "\tsv_setpvn(\$arg, (char *)\$var, $nitems * sizeof($atype));\n";
3144 }
3145 else {
3146 # Handle a normal return type via a typemap.
3147  
3148 # Get the output map entry for this type; complain if not found.
3149 300 my $typemap = $typemaps->get_typemap(ctype => $type);
3150 300 ▶ if (not $typemap) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($typemap) 2 298 300
3151 2 $pxs->report_typemap_failure($typemaps, $type);
3152 2 return;
3153 }
3154  
3155 298 $outputmap = $typemaps->get_outputmap(xstype => $typemap->xstype);
3156 298 ▶ if (not $outputmap) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($outputmap) 3 295 298
3157 3 $pxs->blurt( "Error: no OUTPUT definition for type '$type', "
3158 . "typekind '" . $typemap->xstype . "' found");
3159 3 return;
3160 }
3161  
3162 # Get the text of the typemap template, with a few transformations to
3163 # make it work better with fussy C compilers. In particular, strip
3164 # trailing semicolons and remove any leading white space before a '#'.
3165  
3166 295 $expr = $outputmap->cleaned_code;
3167 }
3168  
3169 308 ▶ my $arg = $pxs->ST(defined $out_num ? $out_num + 1 : $num);
Branch: 1 branch
BranchTrueFalseTotal
defined $out_num ? : 46 262 308
3170  
3171 # Specify the environment for if/when the code template is evalled.
3172 $eval_vars =
3173 {
3174 num => $num,
3175 var => $var,
3176 do_setmagic => $do_setmagic,
3177 subtype => $subtype,
3178 ntype => $ntype,
3179 arg => $arg,
3180 type => $eval_type,
3181 alias => $xsub->{seen_ALIAS},
3182 func_name => $xsub->{decl}{name},
3183 full_perl_name => $xsub->{decl}{full_perl_name},
3184 full_C_name => $xsub->{decl}{full_C_name},
3185 Package => $xsub->{PACKAGE_name},
3186 308 };
3187  
3188  
3189 # ------------------------------------------------------------------
3190 # Handle DO_ARRAY_ELEM token as a very special case
3191  
3192 308 ▶ if (!defined $output_code and $expr =~ /\bDO_ARRAY_ELEM\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $output_code and $expr =~ /\bDO_ARRAY_ELEM\b/) 8 300 308
Condition100%3/3
not defined $output_code and $expr =~ /\bDO_ARRAY_ELEM\b/
!ll&&!rl&&r
112898
MC/DC100%2/2
not defined $output_code and $expr =~ /\bDO_ARRAY_ELEM\b/
not defined $output_code $expr =~ /\bDO_ARRAY_ELEM\b/
Truth table100%3/3
not(A) and B
A= not defined $output_codeB= $expr =~ /\bDO_ARRAY_ELEM\b/
ABresult
0X0
100
111
3193 # See the comments in ExtUtils::ParseXS::Node::Param::as_code() that
3194 # explain the similar code for the DO_ARRAY_ELEM hack there.
3195  
3196 8 ▶ if ($var ne 'RETVAL') {
Branch: 1 branch
BranchTrueFalseTotal
if ($var ne "RETVAL") 2 6 8
3197 # Typemap templates containing DO_ARRAY_ELEM are assumed to
3198 # contain a loop which explicitly stores a new mortal SV at
3199 # each of the locations ST(0) .. ST(n-1), and which then uses
3200 # the code from the typemap for the underlying array element
3201 # to set each SV's value.
3202 #
3203 # This is a horrible hack for RETVAL, which would probably
3204 # fail with OUTLIST due to stack offsets being wrong, and
3205 # definitely would fail with OUT, which is supposed to be
3206 # updating parameter SVs, not pushing anything on the stack.
3207 # So forbid all except RETVAL.
3208 2 ▶ $pxs->blurt("Error: can't use typemap containing DO_ARRAY_ELEM for "
Branch: 1 branch
BranchTrueFalseTotal
defined $out_num ? : 1 1 2
3209 . (defined $out_num ? "OUTLIST" : "OUT")
3210 . " parameter");
3211 2 return;
3212 }
3213  
3214 6 my $subtypemap = $typemaps->get_typemap(ctype => $subtype);
3215 6 ▶ if (not $subtypemap) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subtypemap) 1 5 6
3216 1 $pxs->report_typemap_failure($typemaps, $subtype);
3217 1 return;
3218 }
3219  
3220 5 my $suboutputmap =
3221 $typemaps->get_outputmap(xstype => $subtypemap->xstype);
3222  
3223 5 ▶ if (not $suboutputmap) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($suboutputmap) 1 4 5
3224 1 $pxs->blurt( "Error: no OUTPUT definition for subtype '$subtype', "
3225 . "typekind '" . $subtypemap->xstype . "' found");
3226 1 return;
3227 }
3228  
3229 4 my $subexpr = $suboutputmap->cleaned_code;
3230 4 $subexpr =~ s/ntype/subtype/g;
3231 4 $subexpr =~ s/\$arg/ST(ix_$var)/g;
3232 4 $subexpr =~ s/\$var/${var}\[ix_$var]/g;
3233 4 $subexpr =~ s/\n\t/\n\t\t/g;
3234 4 $expr =~ s/\bDO_ARRAY_ELEM\b/$subexpr/;
3235  
3236 4 $saw_DAE = 1;
3237 }
3238  
3239 304 return $expr, $eval_vars, $is_template, $saw_DAE;
3240 }
3241  
3242  
3243 # $self->as_input_code():
3244 #
3245 # Emit the param object as C code which declares and initialise the variable.
3246 # See also the as_output_code() method, which emits code to return the value
3247 # of that local var.
3248  
3249 737 ▶sub as_input_code {
Subroutine as_input_code: called CC=20 SCAR=30.0
3250 737 my __PACKAGE__ $self = shift;
3251 737 my ExtUtils::ParseXS $pxs = shift;
3252 737 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
3253 737 my $xbody = shift;
3254  
3255 my ($type, $arg_num, $var, $init, $no_init, $defer, $default)
3256 737 = @{$self}{qw(type arg_num var init no_init defer default)};
3257  
3258 737 my $arg = $pxs->ST($arg_num);
3259  
3260  
3261 # For this construct: 'foo(char *s, int length(s))', ...
3262  
3263 737 ▶ if ($self->{is_length}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'is_length'}) { } 32 705 737
elsif ($self->{'length_param'}) { } 29 676 705
3264 # ... the call for the 'length(s)' pseudo-parameter should
3265 # emit nothing ...
3266 32 return;
3267 }
3268 elsif ($self->{length_param}) {
3269 # ... while the call for the 's' parameter should emit a (possibly
3270 # modified) declaration and init as normal, but also some extra
3271 # lines. In total this parameter should emit code something like:
3272 #
3273 # STRLEN STRLEN_length_of_s;
3274 # int XSauto_length_of_s;
3275 # char * s = (char *)SvPV(ST(0), STRLEN_length_of_s);
3276 #
3277 # XSauto_length_of_s = STRLEN_length_of_s;
3278  
3279 29 my $lenp = $self->{length_param};
3280 29 my $xsauto_var = $lenp->{var};
3281 29 print "\tSTRLEN\tSTRLEN_length_of_$var;\n";
3282 29 print "\t$lenp->{type}\t$xsauto_var;\n";
3283  
3284 # The "var = SvPV()" line will be emitted by the main body of this
3285 # function. Note that the T_PV typemap entry will have already
3286 # been overridden in lookup_input_typemap() during parse time
3287 # to change SvPV_nolen() to SvPV() or similar.
3288 #
3289 # The final assign should be deferred to come after all
3290 # declarations.
3291 $xbody->{input_part}{deferred_code_lines} .=
3292 29 "\n\t$xsauto_var = STRLEN_length_of_$var;\n";
3293 }
3294  
3295 # Emit the variable's type and name.
3296 #
3297 # Includes special handling for function pointer types. An INPUT line
3298 # always has the C type followed by the variable name. The C code
3299 # which is emitted normally follows the same pattern. However for
3300 # function pointers, the code is different: the variable name has to
3301 # be embedded *within* the type. For example, these two INPUT lines:
3302 #
3303 # char * s
3304 # int (*)(int) fn_ptr
3305 #
3306 # cause the following lines of C to be emitted;
3307 #
3308 # char * s = [something from a typemap]
3309 # int (* fn_ptr)(int) = [something from a typemap]
3310 #
3311 # So handle specially the specific case of a type containing '(*)' by
3312 # embedding the variable name *within* rather than *after* the type.
3313  
3314  
3315 705 ▶ if ($type =~ / \( \s* \* \s* \) /x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ / \( \s* \* \s* \) /x) { } 3 702 705
3316 # for a fn ptr type, embed the var name in the type declaration
3317 3 print "\t" . $pxs->map_type($type, $var);
3318 }
3319 else {
3320 print "\t",
3321 702 ▶ ((defined($xsub->{decl}{class}) && $var eq 'CLASS')
Branch: 1 branch
BranchTrueFalseTotal
defined $xsub->{'decl'}{'class'} && $var eq 'CLASS' ? : 11 691 702
Condition100%3/3
defined $xsub->{"decl"}{"class"} and $var eq "CLASS"
!ll&&!rl&&r
6385311
MC/DC100%2/2
defined $xsub->{"decl"}{"class"} and $var eq "CLASS"
defined $xsub->{"decl"}{"class"} $var eq "CLASS"
Truth table100%3/3
A and B
A= defined $xsub->{"decl"}{"class"}B= $var eq "CLASS"
ABresult
0X0
100
111
3322 ? $type
3323 : $pxs->map_type($type, undef)),
3324 "\t$var";
3325 }
3326  
3327 # Result of parse-phase lookup of INPUT typemap for this param's type.
3328 705 my $lookup = $self->{input_typemap_vals};
3329 705 ▶ $pxs->death( "Internal error: parameter '$var' "
Branch: 1 branch
BranchTrueFalseTotal
unless $lookup 0 705 705
3330 . "doesn't have input_typemap_vals")
3331 unless $lookup;
3332  
3333 705 my ($init_template, $eval_vars, $is_template) = @$lookup;
3334  
3335 705 ▶ return unless defined $init_template; # an error occurred
Branch: 1 branch
BranchTrueFalseTotal
unless defined $init_template 8 697 705
3336  
3337 697 ▶ unless ($is_template) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_template) 0 697 697
3338 # template already expanded
3339 0 print " = $init_template\n";
3340 0 return;
3341 }
3342  
3343 # whitespace-tidy the type
3344 697 $type = ExtUtils::Typemaps::tidy_type($type);
3345  
3346 # Now finally, emit the actual variable declaration and initialisation
3347 # line(s). The variable type and name will already have been emitted.
3348  
3349 697 ▶ my $init_code =
Branch: 1 branch
BranchTrueFalseTotal
length $init_template ? : 402 295 697
3350 length $init_template
3351 ? $pxs->eval_input_typemap_code("qq\a$init_template\a", $eval_vars)
3352 : "";
3353  
3354  
3355 697 ▶ if (defined $default
Branch: 2 branches
BranchTrueFalseTotal
if (defined $default and not defined $init and not $no_init) { } 28 669 697
elsif ($xsub->{'SCOPE_enabled'} or not $init_code =~ /^\s*\Q$var\E =/) { } 305 364 669
Condition100%6/6
defined $default and not defined $init and not $no_init
!ll&&!rl&&r
667228
defined $default and not defined $init
!ll&&!rl&&r
665230
MC/DC100%3/3
defined $default and not defined $init and not $no_init
defined $default not defined $init not $no_init
Truth table100%4/4
A and not(B) and not(C)
A= defined $defaultB= not defined $initC= not $no_init
ABCresult
0XX0
10X0
1100
1111
3356 # XXX for now, for backcompat, ignore default if the
3357 # param has a typemap override
3358 && !(defined $init)
3359 # XXX for now, for backcompat, ignore default if the
3360 # param wouldn't otherwise get initialised
3361 && !$no_init
3362 ) {
3363 # Has a default value. Just terminate the variable declaration, and
3364 # defer the initialisation.
3365  
3366 28 print ";\n";
3367  
3368 # indent the code 1 step further
3369 28 $init_code =~ s/(\t+)/$1 /g;
3370 28 $init_code =~ s/ /\t/g;
3371  
3372 28 ▶ if ($default eq 'NO_INIT') {
Branch: 1 branch
BranchTrueFalseTotal
if ($default eq 'NO_INIT') { } 1 27 28
3373 # for foo(a, b = NO_INIT), add code to initialise later only if
3374 # an arg was supplied.
3375 $xbody->{input_part}{deferred_code_lines}
3376 1 .= sprintf "\n\tif (items >= %d) {\n%s;\n\t}\n",
3377 $arg_num, $init_code;
3378 }
3379 else {
3380 # for foo(a, b = default), add code to initialise later to either
3381 # the arg or default value
3382 27 ▶ my $else = $init_code =~ /\S/
Branch: 1 branch
BranchTrueFalseTotal
$init_code =~ /\S/ ? : 27 0 27
3383 ? "\telse {\n$init_code;\n\t}\n"
3384 : "";
3385  
3386 27 $default =~ s/"/\\"/g; # escape double quotes
3387 $xbody->{input_part}{deferred_code_lines}
3388 27 .= sprintf "\n\tif (items < %d)\n\t %s = %s;\n%s",
3389 $arg_num,
3390 $var,
3391 $pxs->eval_input_typemap_code("qq\a$default\a",
3392 $eval_vars),
3393 $else;
3394 }
3395 }
3396 ▶ elsif ($xsub->{SCOPE_enabled} or $init_code !~ /^\s*\Q$var\E =/) {
Condition100%3/3
$xsub->{"SCOPE_enabled"} or not $init_code =~ /^\s*\Q$var\E =/
l!l&&r!l&&!r
1304364
MC/DC100%2/2
$xsub->{"SCOPE_enabled"} or not $init_code =~ /^\s*\Q$var\E =/
$xsub->{"SCOPE_enabled"} not $init_code =~ /^\s*\Q$var\E =/
Truth table100%3/3
A or not(B)
A= $xsub->{"SCOPE_enabled"}B= not $init_code =~ /^\s*\Q$var\E =/
ABresult
1X1
011
000
3397 # The template is likely a full block rather than a '$var = ...'
3398 # expression. Just terminate the variable declaration, and defer the
3399 # initialisation.
3400 # Note that /\Q$var\E/ matches the string containing whatever $var
3401 # was expanded to in the eval.
3402  
3403 305 print ";\n";
3404  
3405 $xbody->{input_part}{deferred_code_lines}
3406 305 ▶ .= sprintf "\n%s;\n", $init_code
Branch: 1 branch
BranchTrueFalseTotal
if $init_code =~ /\S/ 10 295 305
3407 if $init_code =~ /\S/;
3408 }
3409 else {
3410 # The template starts with '$var = ...'. The variable name has already
3411 # been emitted, so remove it from the typemap before evalling it,
3412  
3413 364 ▶ $init_code =~ s/^\s*\Q$var\E(\s*=\s*)/$1/
Branch: 1 branch
BranchTrueFalseTotal
$init_code =~ s/^\s*\Q$var\E(\s*=\s*)/$1/ or $pxs->death("Internal error: typemap doesn't start with '\$var='\n") 364 0 364
3414 # we just checked above that it starts with var=, so this
3415 # should never happen
3416 or $pxs->death(
3417 "Internal error: typemap doesn't start with '\$var='\n");
3418  
3419 364 printf "%s;\n", $init_code;
3420 }
3421  
3422 697 ▶ if (defined $defer) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $defer) 6 691 697
3423 $xbody->{input_part}{deferred_code_lines}
3424 6 .= $pxs->eval_input_typemap_code("qq\a$defer\a", $eval_vars)
3425 . "\n";
3426 }
3427 }
3428  
3429  
3430 # $param->as_output_code($ParseXS_object, $out_num])
3431 #
3432 # Emit code to: possibly create, then set the value of, and possibly
3433 # push, an output SV, based on the values in the $param object.
3434 #
3435 # $out_num is optional and its presence indicates that an OUTLIST var is
3436 # being pushed: it indicates the position on the stack of that SV.
3437 #
3438 # This function emits code such as "sv_setiv(ST(0), (IV)foo)", based on
3439 # the typemap OUTPUT entry associated with $type. It passes the typemap
3440 # code through a double-quotish context eval first to expand variables
3441 # such as $arg and $var. It also tries to optimise the emitted code in
3442 # various ways, such as using TARG where available rather than calling
3443 # sv_newmortal() to obtain an SV to set to the return value.
3444 #
3445 # It expects to handle three categories of variable, with these general
3446 # actions:
3447 #
3448 # RETVAL, i.e. the return value
3449 #
3450 # Create a new SV; use the typemap to set its value to RETVAL; then
3451 # store it at ST(0).
3452 #
3453 # OUTLIST foo
3454 #
3455 # Create a new SV; use the typemap to set its value to foo; then store
3456 # it at ST($out_num-1).
3457 #
3458 # OUTPUT: foo / OUT foo
3459 #
3460 # Update the value of the passed arg ST($num-1), using the typemap to
3461 # set its value
3462 #
3463 # Note that it's possible for this function to be called *twice* for the
3464 # same variable: once for OUTLIST, and once for an 'OUTPUT:' entry.
3465 #
3466 # It treats output typemaps as falling into two basic categories,
3467 # exemplified by:
3468 #
3469 # sv_setFoo($arg, (Foo)$var));
3470 #
3471 # $arg = newFoo($var);
3472 #
3473 # The first form is the most general and can be used to set the SV value
3474 # for all of the three variable categories above. For the first two
3475 # categories it typically uses a new mortal, while for the last, it just
3476 # uses the passed arg SV.
3477 #
3478 # The assign form of the typemap can be considered an optimisation of
3479 # sv_setsv($arg, newFoo($var)), and is applicable when newFOO() is known
3480 # to return a new SV. So rather than copying it to yet another new SV,
3481 # just return as-is, possibly after mortalising it,
3482 #
3483 # Some typemaps evaluate to different code depending on whether the var is
3484 # RETVAL, e.g T_BOOL is currently defined as:
3485 #
3486 # ${"$var" eq "RETVAL" ? \"$arg = boolSV($var);"
3487 # : \"sv_setsv($arg, boolSV($var));"}
3488 #
3489 # So we examine the typemap *after* evaluation to determine whether it's
3490 # of the form '$arg = ' or not.
3491 #
3492 # Note that *currently* we generally end up with the pessimised option for
3493 # OUTLIST vars, since the typmaps onlt check for RETVAL.
3494 #
3495 # Currently RETVAL and 'OUTLIST var' mostly share the same code paths
3496 # below, so they both benefit from optimisations such as using TARG
3497 # instead of creating a new mortal, and using the RETVALSV C var to keep
3498 # track of the temp SV, rather than repeatedly retrieving it from ST(0)
3499 # etc. Note that RETVALSV is private and shouldn't be referenced within XS
3500 # code or typemaps.
3501  
3502 308 ▶sub as_output_code {
Subroutine as_output_code: called CC=34 SCAR=35.3
3503 308 my __PACKAGE__ $self = shift;
3504 308 my ExtUtils::ParseXS $pxs = shift;
3505 308 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
3506 308 my $xbody = shift;
3507 308 my $out_num = shift;
3508  
3509 my ($type, $var, $do_setmagic, $output_code)
3510 308 = @{$self}{qw(type var do_setmagic output_code)};
3511  
3512 308 ▶ if ($var eq 'RETVAL') {
Branch: 1 branch
BranchTrueFalseTotal
if ($var eq "RETVAL") 219 89 308
3513 # It is possible for RETVAL to have multiple types, e.g.
3514 # int
3515 # foo(long RETVAL)
3516 #
3517 # In the above, 'long' is used for the RETVAL C var's declaration,
3518 # while 'int' is used to generate the return code (for backwards
3519 # compatibility).
3520 219 $type = $xsub->{decl}{return_type}{type};
3521 }
3522  
3523 # whitespace-tidy the type
3524 308 $type = ExtUtils::Typemaps::tidy_type($type);
3525  
3526 # We can be called twice for the same variable: once to update the
3527 # original arg (via an entry in OUTPUT) and once to push the param's
3528 # value (via OUTLIST). When doing the latter, any override code on an
3529 # OUTPUT line should not be used.
3530 308 ▶ undef $output_code if defined $out_num;
Branch: 1 branch
BranchTrueFalseTotal
if defined $out_num 48 260 308
3531  
3532 # Result of parse-phase lookup of OUTPUT typemap for this param's type.
3533 my $lookup = defined $out_num
3534 ? $self->{output_typemap_vals_outlist}
3535 308 ▶ : $self->{output_typemap_vals};
Branch: 1 branch
BranchTrueFalseTotal
defined $out_num ? : 48 260 308
3536 308 ▶ $pxs->death( "Internal error: parameter '$var' "
Branch: 1 branch
BranchTrueFalseTotal
unless $lookup 0 308 308
3537 . "doesn't have output_typemap_vals")
3538 unless $lookup;
3539  
3540 308 my ($expr, $eval_vars, $is_template, $saw_DAE) = @$lookup;
3541  
3542 308 ▶ return unless defined $expr; # error
Branch: 1 branch
BranchTrueFalseTotal
unless defined $expr 14 294 308
3543  
3544 294 ▶ if ($saw_DAE) {
Branch: 2 branches
BranchTrueFalseTotal
if ($saw_DAE) { } 4 290 294
elsif (not $is_template) { } 11 279 290
3545 # We do our own code emitting and return here (rather than control
3546 # passing on to normal RETVAL processing) since that processing is
3547 # expecting to push a single temp onto the stack, while our code
3548 # pushes several temps.
3549 4 print $pxs->eval_output_typemap_code("qq\a$expr\a", $eval_vars);
3550 4 return;
3551 }
3552 elsif (!$is_template) {
3553 # $expr doesn't need evalling - use as-is
3554 11 $output_code = $expr;
3555 }
3556  
3557 290 my $ntype = $eval_vars->{ntype};
3558 290 my $num = $eval_vars->{num};
3559 290 my $arg = $eval_vars->{arg};
3560  
3561 # ------------------------------------------------------------------
3562 # Now emit code for the three types of return value:
3563 #
3564 # RETVAL - The usual case: store an SV at ST(0) which is set
3565 # to the value of RETVAL. This is typically a new
3566 # mortal, but may be optimised to use TARG.
3567 #
3568 # OUTLIST param - if $out_num is defined (and will be >= 0) Push
3569 # after any RETVAL, new mortal(s) containing the
3570 # current values of the local var set from that
3571 # parameter. (May also use TARG if not already used
3572 # by RETVAL).
3573 #
3574 # OUT/OUTPUT param - update passed arg SV at ST($num-1) (which
3575 # corresponds to param) with the current value of
3576 # the local var set from that parameter.
3577  
3578 290 ▶ if ($var ne 'RETVAL' and not defined $out_num) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var ne "RETVAL" and not defined $out_num) 36 254 290
Condition100%3/3
$var ne "RETVAL" and not defined $out_num
!ll&&!rl&&r
2094536
MC/DC100%2/2
$var ne "RETVAL" and not defined $out_num
$var ne "RETVAL" not defined $out_num
Truth table100%3/3
A and not(B)
A= $var ne "RETVAL"B= not defined $out_num
ABresult
0X0
100
111
3579 # This is a normal OUTPUT var: i.e. a named parameter whose
3580 # corresponding arg on the stack should be updated with the
3581 # parameter's current value by using the code contained in the
3582 # output typemap.
3583 #
3584 # Note that for args being *updated* (as opposed to replaced), this
3585 # branch relies on the typemap to Do The Right Thing. For example,
3586 # T_BOOL currently has this typemap entry:
3587 #
3588 # ${"$var" eq "RETVAL" ? \"$arg = boolSV($var);"
3589 # : \"sv_setsv($arg, boolSV($var));"}
3590 #
3591 # which means that if we hit this branch, $evalexpr will have been
3592 # expanded to something like "sv_setsv(ST(2), boolSV(foo))".
3593  
3594 36 ▶ unless (defined $num) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $num) 0 36 36
3595 0 $pxs->blurt(
3596 "Internal error: OUT parameter has undefined argument number");
3597 0 return;
3598 }
3599  
3600 # Use the code on the OUTPUT line if specified, otherwise use the
3601 # typemap
3602 36 ▶ my $code = defined $output_code
Branch: 1 branch
BranchTrueFalseTotal
defined $output_code ? : 3 33 36
3603 ? "\t$output_code\n"
3604 : $pxs->eval_output_typemap_code("qq\a$expr\a", $eval_vars);
3605 36 print $code;
3606  
3607 # For parameters in the OUTPUT section, honour the SETMAGIC in force
3608 # at the time. For parameters instead being output because of an OUT
3609 # keyword in the signature, assume set magic always.
3610 36 ▶ print "\tSvSETMAGIC($arg);\n" if !$self->{in_output} || $do_setmagic;
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"in_output"} or $do_setmagic 33 3 36
Condition100%3/3
not $self->{"in_output"} or $do_setmagic
l!l&&r!l&&!r
14193
MC/DC100%2/2
not $self->{"in_output"} or $do_setmagic
not $self->{"in_output"} $do_setmagic
Truth table100%3/3
not(A) or B
A= not $self->{"in_output"}B= $do_setmagic
ABresult
1X1
011
000
3611 36 return;
3612 }
3613  
3614 # ------------------------------------------------------------------
3615 # The rest of this main body handles RETVAL or "OUTLIST foo".
3616  
3617 254 ▶ if (defined $output_code and !defined $out_num) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $output_code and not defined $out_num) 8 246 254
Condition66%2/3
defined $output_code and not defined $out_num
!ll&&!rl&&r
24608
MC/DC50%1/2
defined $output_code and not defined $out_num
defined $output_code not defined $out_num
Truth table66%2/3
A and not(B)
A= defined $output_codeB= not defined $out_num
ABresult
0X0
100
111
3618 # Handle this (just emit overridden code as-is):
3619 # OUTPUT:
3620 # RETVAL output_code
3621 8 print "\t$output_code\n";
3622 8 ▶ print "\t++SP;\n" if $xbody->{output_part}{stack_was_reset};
Branch: 1 branch
BranchTrueFalseTotal
if $xbody->{"output_part"}{"stack_was_reset"} 0 8 8
3623 8 return;
3624 }
3625  
3626 # Emit a standard RETVAL/OUTLIST return
3627  
3628 # ------------------------------------------------------------------
3629 # First, evaluate the typemap, expanding any vars like $var and $arg,
3630 # for example,
3631 #
3632 # $arg = newFoo($var);
3633 # or
3634 # sv_setFoo($arg, $var);
3635 #
3636 # However, rather than using the actual destination (such as ST(0))
3637 # for the value of $arg, we instead set it initially to RETVALSV. This
3638 # is because often the SV will be used in more than one statement,
3639 # and so it is more efficient to temporarily store it in a C auto var.
3640 # So we normally emit code such as:
3641 #
3642 # {
3643 # SV *RETVALSV;
3644 # RETVALSV = newFoo(RETVAL);
3645 # RETVALSV = sv_2mortal(RETVALSV);
3646 # ST(0) = RETVALSV;
3647 # }
3648 #
3649 # Rather than
3650 #
3651 # ST(0) = newFoo(RETVAL);
3652 # sv_2mortal(ST(0));
3653 #
3654 # Later we sometimes modify the evalled typemap to change 'RETVALSV'
3655 # to some other value:
3656 # - back to e.g. 'ST(0)' if there is no other use of the SV;
3657 # - to TARG when we are using the OP_ENTERSUB's targ;
3658 # - to $var when then return type is SV* (and thus ntype is SVPtr)
3659 # and so the variable will already have been declared as type 'SV*'
3660 # and thus there is no need for a RETVALSV too.
3661 #
3662 # Note that we evaluate the typemap early here so that the various
3663 # regexes below such as /^\s*\Q$arg\E\s*=/ can be matched against
3664 # the *evalled* result of typemap entries such as
3665 #
3666 # ${ "$var" eq "RETVAL" ? \"$arg = $var;" : \"sv_setsv_mg($arg, $var);" }
3667 #
3668 # which may eval to something like "RETVALSV = RETVAL" and
3669 # subsequently match /^\s*\Q$arg\E =/ (where $arg is "RETVAL"), but
3670 # couldn't have matched against the original typemap.
3671 # This is why we *always* set $arg to 'RETVALSV' first and then modify
3672 # the typemap later - we don't know what final value we want for $arg
3673 # until after we've examined the evalled result.
3674  
3675 246 my $orig_arg = $arg;
3676 246 $eval_vars->{arg} = $arg = 'RETVALSV';
3677 246 my $evalexpr = $pxs->eval_output_typemap_code("qq\a$expr\a", $eval_vars);
3678  
3679 # ------------------------------------------------------------------
3680 # Examine the just-evalled typemap code to determine what optimisations
3681 # etc can be performed and what sort of code needs emitting. The two
3682 # halves of this following if/else examine the two forms of evalled
3683 # typemap:
3684 #
3685 # RETVALSV = newFoo((Foo)RETVAL);
3686 # and
3687 # sv_setFoo(RETVALSV, (Foo)RETVAL);
3688 #
3689 # In particular, the first form is assumed to be returning an SV which
3690 # the function has generated itself (e.g. newSVREF()) and which may
3691 # just need mortalising; while the second form generally needs a call
3692 # to sv_newmortal() first to create an SV which the function can then
3693 # set the value of.
3694  
3695 246 my $do_mortalize = 0; # Emit an sv_2mortal()
3696 246 my $want_newmortal = 0; # Emit an sv_newmortal()
3697 246 my $retvar = 'RETVALSV'; # The name of the C var which holds the SV
3698 # (likely tmp) to set to the value of the var
3699  
3700 246 ▶ if ($evalexpr =~ /^\s*\Q$arg\E\s*=/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($evalexpr =~ /^\s*\Q$arg\E\s*=/) { } 9 237 246
3701 # Handle this form: RETVALSV = newFoo((Foo)RETVAL);
3702 # newFoo creates its own SV: we just need to mortalise and return it
3703  
3704 # Is the SV one of the immortal SVs?
3705 9 ▶ if ($evalexpr =~
Branch: 1 branch
BranchTrueFalseTotal
if ($evalexpr =~ /^\s* \Q$arg\E \s*=\s* ( boolSV\(.*\) | &PL_sv_yes | &PL_sv_no | &PL_sv_undef | &PL_sv_zero ) \s*;\s*$ /x) { } 6 3 9
3706 /^\s*
3707 \Q$arg\E
3708 \s*=\s*
3709 ( boolSV\(.*\)
3710 | &PL_sv_yes
3711 | &PL_sv_no
3712 | &PL_sv_undef
3713 | &PL_sv_zero
3714 )
3715 \s*;\s*$
3716 /x)
3717 {
3718 # If so, we can skip mortalising it to stop it leaking.
3719 6 $retvar = $orig_arg; # just assign to ST(N) directly
3720 }
3721 else {
3722 # general '$arg = newFOO()' typemap
3723 3 $do_mortalize = 1;
3724  
3725 # If $var is already of type SV*, then use that instead of
3726 # declaring 'SV* RETVALSV' as an intermediate var.
3727 3 ▶ $retvar = $var if $ntype eq "SVPtr";
Branch: 1 branch
BranchTrueFalseTotal
if $ntype eq "SVPtr" 1 2 3
3728 }
3729 }
3730 else {
3731 # Handle this (eval-expanded) form of typemap:
3732 # sv_setFoo(RETVALSV, (Foo)var);
3733 # We generally need to supply a mortal SV for the typemap code to
3734 # set, and then return it on the stack,
3735  
3736 # First, see if we can use the targ (if any) attached to the current
3737 # OP_ENTERSUB, to avoid having to create a new mortal.
3738 #
3739 # The targetable() OutputMap class method looks at whether the code
3740 # snippet is of a form suitable for using TARG as the destination.
3741 # It looks for one of a known list of well-behaved setting function
3742 # calls, like sv_setiv() which will set the TARG to a value that
3743 # doesn't include magic, tieing, being a reference (which would leak
3744 # as the TARG is never freed), etc. If so, emit dXSTARG and replace
3745 # RETVALSV with TARG.
3746 #
3747 # For backwards-compatibility, dXSTARG may have already been emitted
3748 # early in the XSUB body, when a more restrictive set of targ-
3749 # compatible typemap entries were checked for. Note that dXSTARG is
3750 # defined as something like:
3751 #
3752 # SV * targ = (PL_op->op_private & OPpENTERSUB_HASTARG)
3753 # ? PAD_SV(PL_op->op_targ) : sv_newmortal()
3754  
3755 237 ▶ if ( $pxs->{config_optimize}
Branch: 1 branch
BranchTrueFalseTotal
if ($pxs->{'config_optimize'} and 'ExtUtils::Typemaps::OutputMap'->targetable($evalexpr) and not $xbody->{'output_part'}{'targ_used'}) { } 200 37 237
Condition83%5/6
$pxs->{"config_optimize"} and "ExtUtils::Typemaps::OutputMap"->targetable($evalexpr) and not $xbody->{"output_part"}{"targ_used"}
!ll&&!rl&&r
2017200
$pxs->{"config_optimize"} and "ExtUtils::Typemaps::OutputMap"->targetable($evalexpr)
!ll&&!rl&&r
020217
MC/DC66%2/3
$pxs->{"config_optimize"} and "ExtUtils::Typemaps::OutputMap"->targetable($evalexpr) and not $xbody->{"output_part"}{"targ_used"}
$pxs->{"config_optimize"} "ExtUtils::Typemaps::OutputMap"->targetable($evalexpr) not $xbody->{"output_part"}{"targ_used"}
Truth table75%3/4
A and B and not(C)
A= $pxs->{"config_optimize"}B= "ExtUtils::Typemaps::OutputMap"->targetable($evalexpr)C= not $xbody->{"output_part"}{"targ_used"}
ABCresult
0XX0
10X0
1100
1111
3756 && ExtUtils::Typemaps::OutputMap->targetable($evalexpr)
3757 && !$xbody->{output_part}{targ_used})
3758 {
3759 # So TARG is available for use.
3760 200 $retvar = 'TARG';
3761 # can only use TARG to return one value
3762 200 $xbody->{output_part}{targ_used} = 1;
3763  
3764 # Since we're using TARG for the return SV, see if we can use
3765 # the TARG[iun] macros as appropriate to speed up setting it.
3766 # If so, convert "sv_setiv(RETVALSV, val)" to "TARGi(val,1)"
3767 # and similarly for uv and nv. These macros skip a function
3768 # call for the common case where TARG is already a simple
3769 # IV/UV/NV. Convert the _mg forms too: since we're setting the
3770 # TARG, there shouldn't be set magic on it, so the _mg action
3771 # can be safely ignored.
3772  
3773 200 $evalexpr =~ s{
3774 ^
3775 (\s*)
3776 sv_set([iun])v(?:_mg)?
3777 \(
3778 \s* RETVALSV \s* ,
3779 \s* (.*)
3780 \)
3781 ( \s* ; \s*)
3782 $
3783 }
3784 {$1TARG$2($3, 1)$4}x;
3785 }
3786 else {
3787 # general typemap: give it a fresh SV to set the value of.
3788 37 $want_newmortal = 1;
3789 }
3790 }
3791  
3792 # ------------------------------------------------------------------
3793 # Now emit the return C code, based on the various flags and values
3794 # determined above.
3795  
3796 246 my $do_scope; # wrap code in a {} block
3797 my @lines; # Lines of code to eventually emit
3798  
3799 # Do any declarations first
3800  
3801 246 ▶ if ($retvar eq 'TARG' && !$xsub->{decl}{return_type}{use_early_targ}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($retvar eq 'TARG' and not $xsub->{'decl'}{'return_type'}{'use_early_targ'}) { } 21 225 246
elsif ($retvar eq 'RETVALSV') { } 39 186 225
Condition100%3/3
$retvar eq "TARG" and not $xsub->{"decl"}{"return_type"}{"use_early_targ"}
!ll&&!rl&&r
4617921
MC/DC100%2/2
$retvar eq "TARG" and not $xsub->{"decl"}{"return_type"}{"use_early_targ"}
$retvar eq "TARG" not $xsub->{"decl"}{"return_type"}{"use_early_targ"}
Truth table100%3/3
A and not(B)
A= $retvar eq "TARG"B= not $xsub->{"decl"}{"return_type"}{"use_early_targ"}
ABresult
0X0
100
111
3802 21 push @lines, "\tdXSTARG;\n";
3803 21 $do_scope = 1;
3804 }
3805 elsif ($retvar eq 'RETVALSV') {
3806 39 push @lines, "\tSV * $retvar;\n";
3807 39 $do_scope = 1;
3808 }
3809  
3810 246 ▶ push @lines, "\tRETVALSV = sv_newmortal();\n" if $want_newmortal;
Branch: 1 branch
BranchTrueFalseTotal
if $want_newmortal 37 209 246
3811  
3812 # Emit the typemap, while changing the name of the destination SV back
3813 # from RETVALSV to one of the other forms (varname/TARG/ST(N)) if was
3814 # determined earlier to be necessary.
3815 # Skip emitting it if it's of the trivial form "var = var", which is
3816 # generated when the typemap is of the form '$arg = $var' and the SVPtr
3817 # optimisation is using $var for the destination.
3818  
3819 246 ▶ $evalexpr =~ s/\bRETVALSV\b/$retvar/g if $retvar ne 'RETVALSV';
Branch: 1 branch
BranchTrueFalseTotal
if $retvar ne "RETVALSV" 207 39 246
3820  
3821 246 ▶ unless ($evalexpr =~ /^\s*\Q$var\E\s*=\s*\Q$var\E\s*;\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($evalexpr =~ /^\s*\Q$var\E\s*=\s*\Q$var\E\s*;\s*$/) 245 1 246
3822 245 push @lines, split /^/, $evalexpr
3823 }
3824  
3825 # Emit mortalisation on the result SV if needed
3826 246 ▶ push @lines, "\t$retvar = sv_2mortal($retvar);\n" if $do_mortalize;
Branch: 1 branch
BranchTrueFalseTotal
if $do_mortalize 3 243 246
3827  
3828 # Emit the final 'ST(n) = RETVALSV' or similar, unless ST(n)
3829 # was already assigned to earlier directly by the typemap.
3830 246 ▶ push @lines, "\t$orig_arg = $retvar;\n" unless $retvar eq $orig_arg;
Branch: 1 branch
BranchTrueFalseTotal
unless $retvar eq $orig_arg 240 6 246
3831  
3832 246 ▶ if ($do_scope) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_scope) 60 186 246
3833 # Add an extra 4-indent, then wrap the output code in a new block
3834 60 for (@lines) {
3835 219 s/\t/ /g; # break down all tabs into spaces
3836 219 s/^/ /; # add 4-space extra indent
3837 219 s/ /\t/g; # convert 8 spaces back to tabs
3838 }
3839 60 unshift @lines, "\t{\n";
3840 60 push @lines, "\t}\n";
3841 }
3842  
3843 246 print @lines;
3844 246 ▶ print "\t++SP;\n" if $xbody->{output_part}{stack_was_reset};
Branch: 1 branch
BranchTrueFalseTotal
if $xbody->{"output_part"}{"stack_was_reset"} 64 182 246
3845 }
3846  
3847  
3848 # ======================================================================
3849  
3850 package ExtUtils::ParseXS::Node::Params;
3851  
3852 # A Node subclass which holds a list of the parameters for an XSUB.
3853 # It is a mainly a list of Node::Param or Node::IO_Param kids, and is
3854 # used in two contexts.
3855 #
3856 # First, as a field of an xsub_decl node, where it holds a list of Param
3857 # objects which represent the individual parameters found within an XSUB's
3858 # signature, plus possibly extra synthetic ones such as THIS and RETVAL.
3859 #
3860 # Second, as a field of an xbody node, where it contains a copy of the
3861 # signature's Params object (and Param children), but where the children
3862 # are in fact IO_param objects and hold augmented information provided by
3863 # any INPUT and OUTPUT blocks within that XSUB body (of which there can be
3864 # more than one in the presence of CASE).
3865  
3866 28 ▶BEGIN { $build_subclass->(
Subroutine BEGIN: called CC=1 SCAR=0.0
3867  
3868 'names', # Hash ref mapping variable names to Node::Param
3869 # or Node::IO_Param objects
3870  
3871 'params_text', # Str: The original text of the sig, e.g.
3872 # "param1, int param2 = 0"
3873  
3874 'seen_ellipsis', # Bool: XSUB signature has ( ,...)
3875  
3876 'nargs', # Int: The number of args expected from caller
3877 'min_args', # Int: The minimum number of args allowed from caller
3878  
3879 'auto_function_sig_override', # Str: the C_ARGS value, if any
3880 )};
3881  
3882  
3883 # ----------------------------------------------------------------
3884 # Parse the parameter list of an XSUB's signature.
3885 #
3886 # Split the XSUB's parameter list on commas into parameters, while
3887 # allowing for things like '(a = ",", b)'.
3888 #
3889 # Then for each parameter, parse its various fields and store in a
3890 # ExtUtils::ParseXS::Node::Param object. Store those Param objects within
3891 # the Params object, plus any other state deduced from the signature, such
3892 # as min/max permitted number of args.
3893 #
3894 # A typical signature might look like:
3895 #
3896 # OUT char *s, \
3897 # int length(s), \
3898 # OUTLIST int size = 10)
3899 #
3900 # ----------------------------------------------------------------
3901  
3902 28 my ($C_group_rex, $C_arg);
3903  
3904 # Group in C (no support for comments or literals)
3905 #
3906 # DAPM 2024: I'm not entirely clear what this is supposed to match.
3907 # It appears to match balanced and possibly nested [], {} etc, with
3908 # similar but possibly unbalanced punctuation within. But the balancing
3909 # brackets don't have to correspond: so [} is just as valid as [] or {},
3910 # as is [{{{{] or even [}}}}}
3911  
3912 $C_group_rex = qr/ [({\[]
3913 (?: (?> [^()\[\]{}]+ ) | (??{ $C_group_rex }) )*
3914 28 [)}\]] /x;
3915  
3916 # $C_arg: match a chunk in C without comma at toplevel (no comments),
3917 # i.e. a single arg within an XS signature, such as
3918 # foo = ','
3919 #
3920 # DAPM 2024. This appears to match zero, one or more of:
3921 # a random collection of non-bracket/quote/comma chars (e.g, a word or
3922 # number or 'int *foo' etc), or
3923 # a balanced(ish) nested brackets, or
3924 # a "string literal", or
3925 # a 'c' char literal
3926 # So (I guess), it captures the next item in a function signature
3927  
3928 $C_arg = qr/ (?: (?> [^()\[\]{},"']+ )
3929 | (??{ $C_group_rex })
3930 | " (?: (?> [^\\"]+ )
3931 | \\.
3932 )* " # String literal
3933 | ' (?: (?> [^\\']+ ) | \\. )* ' # Char literal
3934 28 )* /xs;
3935  
3936  
3937 459 ▶sub parse {
Subroutine parse: called CC=24 SCAR=31.8
3938 459 my __PACKAGE__ $self = shift;
3939 459 my ExtUtils::ParseXS $pxs = shift;
3940 459 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
3941 459 my $params_text = shift;
3942  
3943 459 $self->SUPER::parse($pxs); # set file/line_no
3944  
3945 # remove line continuation chars (\)
3946 459 $params_text =~ s/\\\s*/ /g;
3947 459 $self->{params_text} = $params_text;
3948  
3949 459 my @param_texts;
3950 459 my $opt_args = 0; # how many params with default values seen
3951 459 my $nargs = 0; # how many args are expected
3952  
3953 # First, split signature into separate parameters
3954  
3955 459 ▶ if ($params_text =~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params_text =~ /\S/) { } 308 151 459
3956 308 my $sig_c = "$params_text ,";
3957 28 ▶ use re 'eval'; # needed for 5.16.0 and earlier
Subroutine BEGIN: called CC=1 SCAR=0.0
3958 308 my $can_use_regex = ($sig_c =~ /^( (??{ $C_arg }) , )* $ /x);
3959 28 ▶ no re 'eval';
Subroutine BEGIN: called CC=1 SCAR=0.0
3960  
3961 308 ▶ if ($can_use_regex) {
Branch: 1 branch
BranchTrueFalseTotal
if ($can_use_regex) { } 307 1 308
3962 # If the parameters are capable of being split by using the
3963 # fancy regex, do so. This splits the params on commas, but
3964 # can handle things like foo(a = ",", b)
3965 28 ▶ use re 'eval';
Subroutine BEGIN: called CC=1 SCAR=0.0
3966 307 @param_texts = ($sig_c =~ /\G ( (??{ $C_arg }) ) , /xg);
3967 }
3968 else {
3969 # This is the fallback parameter-splitting path for when the
3970 # $C_arg regex doesn't work. This code path should ideally
3971 # never be reached, and indicates a design weakness in $C_arg.
3972 1 @param_texts = split(/\s*,\s*/, $params_text);
3973 1 $pxs->Warn( "Warning: cannot parse parameter list "
3974 . "'$params_text', fallback to split");
3975 }
3976 }
3977 else {
3978 151 @param_texts = ();
3979 }
3980  
3981 # C++ methods get a fake object/class param at the start.
3982 # This affects arg numbering.
3983 459 ▶ if (defined($xsub->{decl}{class})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $xsub->{"decl"}{"class"}) 25 434 459
3984 my ($var, $type) =
3985 ( $xsub->{decl}{return_type}{static}
3986 or $xsub->{decl}{name} eq 'new'
3987 )
3988 ? ('CLASS', "char *")
3989 25 ▶ : ('THIS', ($xsub->{decl}{is_const} ? "const " : "")
Branch: 2 branches
BranchTrueFalseTotal
$xsub->{'decl'}{'return_type'}{'static'} || $xsub->{'decl'}{'name'} eq 'new' ? : 11 14 25
$xsub->{'decl'}{'is_const'} ? : 2 12 14
Condition100%3/3
$xsub->{"decl"}{"return_type"}{"static"} or $xsub->{"decl"}{"name"} eq "new"
l!l&&r!l&&!r
4714
MC/DC100%2/2
$xsub->{"decl"}{"return_type"}{"static"} or $xsub->{"decl"}{"name"} eq "new"
$xsub->{"decl"}{"return_type"}{"static"} $xsub->{"decl"}{"name"} eq "new"
Truth table100%3/3
A or B
A= $xsub->{"decl"}{"return_type"}{"static"}B= $xsub->{"decl"}{"name"} eq "new"
ABresult
1X1
011
000
3990 . "$xsub->{decl}{class} *");
3991  
3992 25 my ExtUtils::ParseXS::Node::Param $param
3993 = ExtUtils::ParseXS::Node::Param->new( {
3994 var => $var,
3995 type => $type,
3996 is_synthetic => 1,
3997 arg_num => ++$nargs,
3998 });
3999 25 push @{$self->{kids}}, $param;
4000 25 $self->{names}{$var} = $param;
4001 }
4002  
4003 # For non-void return types, add a fake RETVAL parameter. This triggers
4004 # the emitting of an 'int RETVAL;' declaration or similar, and (e.g. if
4005 # later flagged as in_output), triggers the emitting of code to return
4006 # RETVAL's value.
4007 #
4008 # Note that a RETVAL param can be in three main states:
4009 #
4010 # fully-synthetic What is being created here. RETVAL hasn't appeared
4011 # in a signature or INPUT.
4012 #
4013 # semi-real Same as fully-synthetic, but with a defined arg_num,
4014 # and with an updated position within
4015 # @{$self->{kids}}. A RETVAL has appeared in the
4016 # signature, but without a type yet specified, so it
4017 # continues to use $xsub->{decl}{return_type}{type}.
4018 #
4019 # real is_synthetic, no_init flags turned off. Its type
4020 # comes from the sig or INPUT line. This is just a
4021 # normal parameter now.
4022  
4023 459 ▶ if ($xsub->{decl}{return_type}{type} ne 'void') {
Branch: 1 branch
BranchTrueFalseTotal
if ($xsub->{"decl"}{"return_type"}{"type"} ne "void") 250 209 459
4024 my ExtUtils::ParseXS::Node::Param $param =
4025 ExtUtils::ParseXS::Node::Param->new( {
4026 var => 'RETVAL',
4027 type => $xsub->{decl}{return_type}{type},
4028 250 no_init => 1, # just declare the var, don't initialise it
4029 is_synthetic => 1,
4030 } );
4031  
4032 250 push @{$self->{kids}}, $param;
4033 250 $self->{names}{RETVAL} = $param;
4034 }
4035  
4036 459 for my $param_text (@param_texts) {
4037 # Parse each parameter.
4038  
4039 529 $param_text =~ s/^\s+//;
4040 529 $param_text =~ s/\s+$//;
4041  
4042 # Process ellipsis (...)
4043  
4044 $pxs->blurt("Error: further XSUB parameter seen after ellipsis (...)")
4045 529 ▶ if $self->{seen_ellipsis};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"seen_ellipsis"} 1 528 529
4046  
4047 529 ▶ if ($param_text eq '...') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param_text eq "...") 14 515 529
4048 14 $self->{seen_ellipsis} = 1;
4049 14 next;
4050 }
4051  
4052 515 my $param = ExtUtils::ParseXS::Node::Param->new();
4053 515 ▶ $param->parse($pxs, $self, $param_text)
Branch: 1 branch
BranchTrueFalseTotal
$param->parse($pxs, $self, $param_text) or next 498 16 514
4054 or next;
4055  
4056 498 push @{$self->{kids}}, $param;
4057 498 ▶ $self->{names}{$param->{var}} = $param unless $param->{var} eq 'SV *';
Branch: 1 branch
BranchTrueFalseTotal
unless $param->{"var"} eq "SV *" 494 4 498
4058 498 ▶ $opt_args++ if defined $param->{default};
Branch: 1 branch
BranchTrueFalseTotal
if defined $param->{"default"} 36 462 498
4059 # Give the param a number if it will consume one of the passed args
4060 $param->{arg_num} = ++$nargs
4061 unless ( defined $param->{in_out} && $param->{in_out} eq "OUTLIST"
4062 or $param->{is_length})
4063  
4064 498 ▶ } # for (@param_texts)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param->{"in_out"} and $param->{"in_out"} eq "OUTLIST" or $param->{"is_length"} 429 69 498
Condition100%6/6
defined $param->{"in_out"} and $param->{"in_out"} eq "OUTLIST" or $param->{"is_length"}
l!l&&r!l&&!r
3534429
defined $param->{"in_out"} and $param->{"in_out"} eq "OUTLIST"
!ll&&!rl&&r
4362735
MC/DC100%3/3
defined $param->{"in_out"} and $param->{"in_out"} eq "OUTLIST" or $param->{"is_length"}
defined $param->{"in_out"} $param->{"in_out"} eq "OUTLIST" $param->{"is_length"}
Truth table80%4/5
A and B or C
A= defined $param->{"in_out"}B= $param->{"in_out"} eq "OUTLIST"C= $param->{"is_length"}
ABCresult
11X1
0X11
1011
0X00
1000
4065  
4066 458 $self->{nargs} = $nargs;
4067 458 $self->{min_args} = $nargs - $opt_args;
4068  
4069 # for each parameter of the form 'length(foo)', set 'length_param' in
4070 # the corresponding 'foo' parameter to point to that length parameter
4071 # object, or error out if foo not found.
4072 458 for my $param (@{$self->{kids}}) {
4073 757 ▶ next unless $param->{is_length};
Branch: 1 branch
BranchTrueFalseTotal
unless $param->{"is_length"} 723 34 757
4074 34 my $name = $param->{len_name};
4075 34 ▶ if (exists $self->{names}{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'names'}{$name}) { } 33 1 34
4076 33 $self->{names}{$name}{length_param} = $param;
4077  
4078 $pxs->blurt("Error: length() on placeholder parameter '$name'")
4079 33 ▶ unless defined $self->{names}{$name}{type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"names"}{$name}{"type"} 2 31 33
4080  
4081 $pxs->blurt( "Error: default value for $name not allowed"
4082 . " when length($name) also present")
4083 33 ▶ if defined $self->{names}{$name}{default};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"names"}{$name}{"default"} 2 31 33
4084  
4085 33 my $in_out = $self->{names}{$name}{in_out};
4086 33 ▶ $pxs->blurt( "Error: '$in_out' modifier on '$name'"
Branch: 1 branch
BranchTrueFalseTotal
if defined $in_out and not $in_out =~ /^IN/ 2 31 33
Condition100%3/3
defined $in_out and not $in_out =~ /^IN/
!ll&&!rl&&r
2922
MC/DC100%2/2
defined $in_out and not $in_out =~ /^IN/
defined $in_out not $in_out =~ /^IN/
Truth table100%3/3
A and not(B)
A= defined $in_outB= not $in_out =~ /^IN/
ABresult
0X0
100
111
4087 . " can't be used with length()")
4088 if defined $in_out and $in_out !~ /^IN/;
4089 }
4090 else {
4091 1 $pxs->blurt("Error: length() on non-parameter '$name'");
4092 }
4093 }
4094  
4095 458 1;
4096 }
4097  
4098  
4099 # Return a string to be used in "usage: .." error messages.
4100  
4101 430 ▶sub usage_string {
Subroutine usage_string: called CC=3 SCAR=11.0
4102 430 my __PACKAGE__ $self = shift;
4103  
4104 my @args = map {
4105 $_->{var}
4106 . (defined $_->{default_usage}
4107 ?$_->{default_usage}
4108 437 ▶ : ''
Branch: 1 branch
BranchTrueFalseTotal
defined $_->{'default_usage'} ? : 33 404 437
4109 )
4110 }
4111 grep {
4112 defined $_->{arg_num},
4113 726 }
4114 430 @{$self->{kids}};
4115  
4116 430 ▶ push @args, '...' if $self->{seen_ellipsis};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"seen_ellipsis"} 10 420 430
4117 430 return join ', ', @args;
4118 }
4119  
4120  
4121 # $self->C_func_signature():
4122 #
4123 # return two arrays
4124 # the first contains the arguments to pass to an autocall C
4125 # function, e.g. ['a', '&b', 'c'];
4126 # the second contains the types of those args, for use in declaring
4127 # a function pointer type, e.g. ['int', 'char*', 'long'].
4128  
4129 340 ▶sub C_func_signature {
Subroutine C_func_signature: called CC=13 SCAR=25.6
4130 340 my __PACKAGE__ $self = shift;
4131 340 my ExtUtils::ParseXS $pxs = shift;
4132  
4133 340 my @args;
4134 my @types;
4135 340 for my $param (@{$self->{kids}}) {
4136 next if $param->{is_synthetic} # THIS/CLASS/RETVAL
4137 # if a synthetic RETVAL has acquired an arg_num, then
4138 # it's appeared in the signature (although without a
4139 # type) and has become semi-real.
4140 551 ▶ && !($param->{var} eq 'RETVAL' && defined($param->{arg_num}));
Branch: 1 branch
BranchTrueFalseTotal
if $param->{"is_synthetic"} and not $param->{"var"} eq "RETVAL" && defined $param->{"arg_num"} 201 350 551
Condition100%6/6
$param->{"is_synthetic"} and not $param->{"var"} eq "RETVAL" && defined $param->{"arg_num"}
!ll&&!rl&&r
3482201
$param->{'var'} eq 'RETVAL' && defined $param->{'arg_num'}
!ll&&!rl&&r
271742
MC/DC100%3/3
$param->{"is_synthetic"} and not $param->{"var"} eq "RETVAL" && defined $param->{"arg_num"}
$param->{"is_synthetic"} $param->{'var'} eq 'RETVAL' defined $param->{'arg_num'}
Truth table100%4/4
A and not(B && C)
A= $param->{"is_synthetic"}B= $param->{'var'} eq 'RETVAL'C= defined $param->{'arg_num'}
ABCresult
0XX0
1110
10X1
1101
4141  
4142 350 ▶ if ($param->{var} eq 'SV *') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{"var"} eq "SV *") 1 349 350
4143 #backcompat placeholder
4144 1 $pxs->blurt("Error: parameter 'SV *' not valid as a C argument");
4145 1 next;
4146 }
4147  
4148 349 my $io = $param->{in_out};
4149 349 ▶ $io = '' unless defined $io;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $io 296 53 349
4150  
4151 # Ignore fake/alien stuff, except an OUTLIST arg, which
4152 # isn't passed from perl (so no arg_num), but *is* passed to
4153 # the C function and then back to perl.
4154 next unless defined $param->{arg_num}
4155 or $io eq 'OUTLIST'
4156 349 ▶ or $param->{is_length};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param->{"arg_num"} or $io eq "OUTLIST" or $param->{"is_length"} 15 334 349
Condition100%6/6
defined $param->{"arg_num"} or $io eq "OUTLIST" or $param->{"is_length"}
l!l&&r!l&&!r
3033115
defined $param->{"arg_num"} or $io eq "OUTLIST"
l!l&&r!l&&!r
2733046
MC/DC100%3/3
defined $param->{"arg_num"} or $io eq "OUTLIST" or $param->{"is_length"}
defined $param->{"arg_num"} $io eq "OUTLIST" $param->{"is_length"}
Truth table100%4/4
A or B or C
A= defined $param->{"arg_num"}B= $io eq "OUTLIST"C= $param->{"is_length"}
ABCresult
1XX1
01X1
0011
0000
4157  
4158 334 my $a = $param->{var};
4159 334 ▶ my $t = defined $param->{type} ? $param->{type} : 'void*';
Branch: 1 branch
BranchTrueFalseTotal
defined $param->{'type'} ? : 315 19 334
4160 334 ▶ if ($param->{is_addr} or $io =~ /OUT/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{"is_addr"} or $io =~ /OUT/) 55 279 334
Condition100%3/3
$param->{"is_addr"} or $io =~ /OUT/
l!l&&r!l&&!r
253279
MC/DC100%2/2
$param->{"is_addr"} or $io =~ /OUT/
$param->{"is_addr"} $io =~ /OUT/
Truth table100%3/3
A or B
A= $param->{"is_addr"}B= $io =~ /OUT/
ABresult
1X1
011
000
4161 55 $a = "&$a";
4162 55 $t = "$t*";
4163 }
4164 334 push @args, $a;
4165 334 push @types, $t;
4166 }
4167  
4168 340 return \@args, \@types;
4169 }
4170  
4171  
4172 # $self->proto_string():
4173 #
4174 # return a string containing the perl prototype string for this XSUB,
4175 # e.g. '$$;$$@'.
4176  
4177 35 ▶sub proto_string {
Subroutine proto_string: called CC=5 SCAR=16.1
4178 35 my __PACKAGE__ $self = shift;
4179  
4180 # Generate a prototype entry for each param that's bound to a real
4181 # arg. Use '$' unless the typemap for that param has specified an
4182 # overridden entry.
4183 my @p = map defined $_->{proto} ? $_->{proto} : '$',
4184 grep defined $_->{arg_num} && $_->{arg_num} > 0,
4185 35 ▶ @{$self->{kids}};
Branch: 1 branch
BranchTrueFalseTotal
defined $_->{'proto'} ? : 67 1 68
Condition66%2/3
defined $_->{'arg_num'} && $_->{'arg_num'} > 0
!ll&&!rl&&r
25068
MC/DC50%1/2
defined $_->{'arg_num'} && $_->{'arg_num'} > 0
defined $_->{'arg_num'} $_->{'arg_num'} > 0
Truth table66%2/3
A && B
A= defined $_->{'arg_num'}B= $_->{'arg_num'} > 0
ABresult
0X0
100
111
4186  
4187 35 my @sep = (';'); # separator between required and optional args
4188 35 my $min = $self->{min_args};
4189 35 ▶ if ($min < $self->{nargs}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min < $self->{"nargs"}) 8 27 35
4190 # has some default vals
4191 8 splice (@p, $min, 0, ';');
4192 8 @sep = (); # separator already added
4193 }
4194 35 ▶ push @p, @sep, '@' if $self->{seen_ellipsis}; # '...'
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"seen_ellipsis"} 5 30 35
4195 35 return join '', @p;
4196 }
4197  
4198  
4199 # ======================================================================
4200  
4201 package ExtUtils::ParseXS::Node::xbody;
4202  
4203 # This node holds all the foo_part nodes which make up the body of an
4204 # XSUB. Note that in the presence of CASE: keywords, an XSUB may have
4205 # multiple xbodys, one per CASE.
4206 # This node doesn't contain the signature, and nor is it responsible
4207 # for emitting the code for the closing part of an XSUB e.g. the
4208 # XSRETURN(N); there is only one of those per XSUB, so is handled by a
4209 # higher-level node.
4210  
4211 28 ▶BEGIN { $build_subclass->(
Subroutine BEGIN: called CC=1 SCAR=0.0
4212 'ioparams', # Params object: per-body copy of params which accumulate
4213 # extra info from any INPUT and OUTPUT sections (which can
4214 # vary between different CASEs)
4215  
4216 # Node objects representing the various parts of an xbody. These
4217 # are aliases of the same objects in @{$self->{kids}} for easier
4218 # access.
4219 'input_part',
4220 'init_part',
4221 'code_part',
4222 'output_part',
4223 'cleanup_part',
4224  
4225 # Misc parse state
4226  
4227 'seen_RETVAL_in_CODE', # Bool: have seen 'RETVAL' within a CODE block
4228 'seen_autocall', # Bool: this xbody has an autocall node
4229 'OUTPUT_SETMAGIC_state', # Bool: most recent value of SETMAGIC in an
4230 # OUTPUT section.
4231  
4232 )};
4233  
4234  
4235 482 ▶sub parse {
Subroutine parse: called CC=4 SCAR=13.9
4236 482 my __PACKAGE__ $self = shift;
4237 482 my ExtUtils::ParseXS $pxs = shift;
4238 482 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
4239  
4240 482 $self->SUPER::parse($pxs); # set file/line_no
4241  
4242 {
4243 # Make a per-xbody copy of the Params object, which will
4244 # accumulate any extra info from (per-CASE) INPUT and OUTPUT
4245 # sections.
4246  
4247 482 my $orig = $xsub->{decl}{params};
4248  
4249 # make a shallow copy
4250 482 my $ioparams = ExtUtils::ParseXS::Node::Params->new($orig);
4251  
4252 # now duplicate (deep copy) any Param objects and regenerate a new
4253 # names-mapping hash
4254  
4255 482 $ioparams->{kids} = [];
4256 482 $ioparams->{names} = {};
4257  
4258 482 for my $op (@{$orig->{kids}}) {
4259 790 my $p = ExtUtils::ParseXS::Node::IO_Param->new($op);
4260 # don't copy the current proto state (from the most recent
4261 # CASE) into the new CASE.
4262 790 undef $p->{proto};
4263 790 push @{$ioparams->{kids}}, $p;
4264 790 $ioparams->{names}{$p->{var}} = $p;
4265 }
4266  
4267 482 $self->{ioparams} = $ioparams;
4268 }
4269  
4270 # by default, OUTPUT entries have SETMAGIC: ENABLE
4271 482 $self->{OUTPUT_SETMAGIC_state} = 1;
4272  
4273 482 for my $part (qw(input_part init_part code_part output_part cleanup_part)) {
4274 2360 my $kid = "ExtUtils::ParseXS::Node::$part"->new();
4275 2360 ▶ if ($kid->parse($pxs, $xsub, $self)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kid->parse($pxs, $xsub, $self)) 2347 0 2347
4276 2347 push @{$self->{kids}}, $kid;
4277 2347 $self->{$part} = $kid;
4278 }
4279 }
4280  
4281 469 1;
4282 }
4283  
4284  
4285 458 ▶sub as_code {
Subroutine as_code: called CC=7 SCAR=19.9
4286 458 my __PACKAGE__ $self = shift;
4287 458 my ExtUtils::ParseXS $pxs = shift;
4288 458 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
4289  
4290 # Emit opening brace. With cmd-line switch "-except", prefix it with 'TRY'
4291 458 ▶ print +($pxs->{config_allow_exceptions} ? ' TRY' : '')
Branch: 1 branch
BranchTrueFalseTotal
$pxs->{'config_allow_exceptions'} ? : 0 458 458
4292 . " $open_brace\n";
4293  
4294 458 ▶ if ($self->{kids}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"kids"}) 458 0 458
4295 458 $_->as_code($pxs, $xsub, $self) for @{$self->{kids}};
4296 }
4297  
4298 # ----------------------------------------------------------------
4299 # Emit trailers for the body of the XSUB
4300 # ----------------------------------------------------------------
4301  
4302 458 ▶ if ($xsub->{SCOPE_enabled}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xsub->{"SCOPE_enabled"}) 4 454 458
4303 # the matching opens were emitted in input_part->as_code()
4304 4 print " $close_brace\n";
4305 # PPCODE->as_code emits its own LEAVE and return, so this
4306 # line would never be reached.
4307 4 ▶ print " LEAVE;\n" unless $xsub->{seen_PPCODE};
Branch: 1 branch
BranchTrueFalseTotal
unless $xsub->{"seen_PPCODE"} 4 0 4
4308 }
4309  
4310 # matches the $open_brace at the start of this function
4311 458 print " $close_brace\n";
4312  
4313 458 ▶ print $self->Q(<<"EOF") if $pxs->{config_allow_exceptions};
Branch: 1 branch
BranchTrueFalseTotal
if $pxs->{"config_allow_exceptions"} 0 458 458
4314 | BEGHANDLERS
4315 | CATCHALL
4316 | sprintf(errbuf, "%s: %s\\tpropagated", Xname, Xreason);
4317 | ENDHANDLERS
4318 EOF
4319  
4320 }
4321  
4322  
4323 # ======================================================================
4324  
4325 package ExtUtils::ParseXS::Node::input_part;
4326  
4327 28 ▶BEGIN { $build_subclass->(
Subroutine BEGIN: called CC=1 SCAR=0.0
4328  
4329 # Str: used during code generation:
4330 # a multi-line string containing lines of code to be emitted *after*
4331 # all INPUT and PREINIT keywords have been processed.
4332 'deferred_code_lines',
4333 )};
4334  
4335  
4336 482 ▶sub parse {
Subroutine parse: called CC=12 SCAR=24.9
4337 482 my __PACKAGE__ $self = shift;
4338 482 my ExtUtils::ParseXS $pxs = shift;
4339 482 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
4340 482 my ExtUtils::ParseXS::Node::xbody $xbody = shift;
4341  
4342 482 $self->SUPER::parse($pxs); # set file/line_no
4343  
4344 # Process any implicit INPUT section.
4345 {
4346 482 my $input = ExtUtils::ParseXS::Node::INPUT->new();
4347 482 ▶ if ( $input->parse($pxs, $xsub, $xbody)
Branch: 1 branch
BranchTrueFalseTotal
if ($input->parse($pxs, $xsub, $xbody) and $input->{"kids"} and @{$input->{"kids"};}) 99 378 477
Condition66%4/6
$input->parse($pxs, $xsub, $xbody) and $input->{"kids"} and @{$input->{"kids"};}
!ll&&!rl&&r
378099
$input->parse($pxs, $xsub, $xbody) and $input->{"kids"}
!ll&&!rl&&r
037899
MC/DC33%1/3
$input->parse($pxs, $xsub, $xbody) and $input->{"kids"} and @{$input->{"kids"};}
$input->parse($pxs, $xsub, $xbody) $input->{"kids"} @{$input->{"kids"};}
Truth table50%2/4
A and B and C
A= $input->parse($pxs, $xsub, $xbody)B= $input->{"kids"}C= @{$input->{"kids"};}
ABCresult
0XX0
10X0
1100
1111
4348 && $input->{kids}
4349 99 && @{$input->{kids}})
4350 {
4351 99 $input->{implicit} = 1;
4352 99 push @{$self->{kids}}, $input;
4353 }
4354 }
4355  
4356 # Repeatedly look for INPUT or similar or generic keywords,
4357 # parse the text following them, and add any resultant nodes
4358 # as kids to the current node.
4359 $self->parse_keywords(
4360 477 $pxs, $xsub, $xbody,
4361 undef, # implies process as many keywords as possible
4362  
4363 "C_ARGS|INPUT|INTERFACE_MACRO|PREINIT|SCOPE|"
4364 . $ExtUtils::ParseXS::Constants::generic_xsub_keywords_alt,
4365 );
4366  
4367 # For each param, look up its INPUT typemap information now (at parse
4368 # time) and save the results for use later in as_input_code().
4369  
4370 472 for my $ioparam (@{$xbody->{ioparams}{kids}}) {
4371 # might be placeholder param which doesn't get emitted
4372 788 ▶ next unless defined $ioparam->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ioparam->{"type"} 37 751 788
4373 $ioparam->{input_typemap_vals} =
4374 751 [ $ioparam->lookup_input_typemap($pxs, $xsub, $xbody) ];
4375 }
4376  
4377 # Now that the type of each param is finalised, calculate its
4378 # overridden prototype character, if any.
4379 #
4380 # Note that the type of a param can change during parsing, so when to
4381 # call this method is significant. In particular:
4382 # - THIS's type may be set provisionally based on the XSUB's package,
4383 # then updated if it appears as a parameter or on an INPUT line.
4384 # - typemaps can be overridden using the TYPEMAP keyword, so
4385 # it's possible the typemap->proto() method will return something
4386 # different by the time the proto field is used to emit boot code.
4387 # - params can have different types (and thus typemap entries and
4388 # proto chars) per CASE branch.
4389 # So we calculate the per-case/xbody params' proto values here, and
4390 # also use that value to update the per-XSUB value, warning if the
4391 # value changes.
4392  
4393 470 for my $ioparam (@{$xbody->{ioparams}{kids}}) {
4394 785 $ioparam->set_proto($pxs);
4395 785 my $ioproto = $ioparam->{proto};
4396 785 my $name = $ioparam->{var};
4397 785 ▶ next unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 785 785
4398 785 ▶ next unless $ioparam->{arg_num};
Branch: 1 branch
BranchTrueFalseTotal
unless $ioparam->{"arg_num"} 328 457 785
4399  
4400 457 my $param = $$xsub{decl}{params}{names}{$name};
4401 457 my $proto = $param->{proto};
4402 457 ▶ $ioproto = '$' unless defined $ioproto;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ioproto 453 4 457
4403 457 ▶ if (defined $proto and $proto ne $ioproto) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $proto and $proto ne $ioproto) 2 455 457
Condition100%3/3
defined $proto and $proto ne $ioproto
!ll&&!rl&&r
441142
MC/DC100%2/2
defined $proto and $proto ne $ioproto
defined $proto $proto ne $ioproto
Truth table100%3/3
A and B
A= defined $protoB= $proto ne $ioproto
ABresult
0X0
100
111
4404 2 $pxs->Warn("Warning: prototype for '$name' varies: '$proto' versus '$ioproto'");
4405 }
4406 457 $param->{proto} = $ioproto;
4407 }
4408  
4409 470 1;
4410 }
4411  
4412  
4413 458 ▶sub as_code {
Subroutine as_code: called CC=7 SCAR=19.5
4414 458 my __PACKAGE__ $self = shift;
4415 458 my ExtUtils::ParseXS $pxs = shift;
4416 458 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
4417 458 my ExtUtils::ParseXS::Node::xbody $xbody = shift;
4418  
4419 458 my $ioparams = $xbody->{ioparams};
4420  
4421 # Lines to be emitted after PREINIT/INPUT. This may get populated
4422 # by the as_code() methods we call of our kids.
4423 458 $self->{deferred_code_lines} = "";
4424  
4425 458 ▶ if ($self->{kids}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"kids"}) 153 305 458
4426 153 $_->as_code($pxs, $xsub, $xbody) for @{$self->{kids}};
4427 }
4428  
4429 # The matching closes will be emitted in xbody->as_code()
4430 458 ▶ print $self->Q(<<"EOF") if $xsub->{SCOPE_enabled};
Branch: 1 branch
BranchTrueFalseTotal
if $xsub->{"SCOPE_enabled"} 4 454 458
4431 | ENTER;
4432 | $open_brace
4433 EOF
4434  
4435 # Emit any 'char * CLASS' or 'Foo::Bar *THIS' declaration if needed
4436  
4437 458 for my $ioparam (grep $_->{is_synthetic}, @{$ioparams->{kids}}) {
4438 261 $ioparam->as_input_code($pxs, $xsub, $xbody);
4439 }
4440  
4441 # Recent code emits a dXSTARG in a tighter scope and under
4442 # additional circumstances, but some XS code relies on TARG
4443 # having been declared. So continue to declare it early under
4444 # the original circumstances.
4445 458 ▶ if ($xsub->{decl}{return_type}{use_early_targ}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xsub->{"decl"}{"return_type"}{"use_early_targ"}) 214 244 458
4446 214 print "\tdXSTARG;\n";
4447 }
4448  
4449 # Emit declaration/init code for any parameters which were
4450 # declared with a type in the signature (rather than in INPUT).
4451  
4452 458 for my $ioparam (grep $_->{is_ansi}, @{$ioparams->{kids}}) {
4453 353 $ioparam->as_input_code($pxs, $xsub, $xbody);
4454 }
4455  
4456 # ----------------------------------------------------------------
4457 # All C variable declarations have now been emitted. It's now time
4458 # to emit any code which goes before the main body (i.e. the CODE:
4459 # etc or the implicit call to the wrapped function).
4460 # ----------------------------------------------------------------
4461  
4462 # Emit any code which has been deferred until all declarations
4463 # have been done. This is typically INPUT typemaps which don't
4464 # start with a simple '$var =' and so would not have been emitted
4465 # at the variable declaration stage.
4466 458 print $self->{deferred_code_lines};
4467 }
4468  
4469  
4470 # ======================================================================
4471  
4472 package ExtUtils::ParseXS::Node::init_part;
4473  
4474 28 ▶BEGIN { $build_subclass->(
Subroutine BEGIN: called CC=1 SCAR=0.0
4475 )};
4476  
4477  
4478 470 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
4479 470 my __PACKAGE__ $self = shift;
4480 470 my ExtUtils::ParseXS $pxs = shift;
4481 470 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
4482 470 my ExtUtils::ParseXS::Node::xbody $xbody = shift;
4483  
4484 470 $self->SUPER::parse($pxs); # set file/line_no
4485  
4486 # Repeatedly look for INIT or generic keywords,
4487 # parse the text following them, and add any resultant nodes
4488 # as kids to the current node.
4489 470 $self->parse_keywords(
4490 $pxs, $xsub, $xbody,
4491 undef, # implies process as many keywords as possible
4492  
4493 "C_ARGS|INIT|INTERFACE|INTERFACE_MACRO|"
4494 . $ExtUtils::ParseXS::Constants::generic_xsub_keywords_alt,
4495 );
4496  
4497 470 1;
4498 }
4499  
4500  
4501 458 ▶sub as_code {
Subroutine as_code: called CC=3 SCAR=11.0
4502 458 my __PACKAGE__ $self = shift;
4503 458 my ExtUtils::ParseXS $pxs = shift;
4504 458 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
4505 458 my ExtUtils::ParseXS::Node::xbody $xbody = shift;
4506  
4507 458 ▶ if ($self->{kids}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"kids"}) 23 435 458
4508 23 $_->as_code($pxs, $xsub, $xbody) for @{$self->{kids}};
4509 }
4510 }
4511  
4512  
4513 # ======================================================================
4514  
4515 package ExtUtils::ParseXS::Node::code_part;
4516  
4517 28 ▶BEGIN { $build_subclass->(
Subroutine BEGIN: called CC=1 SCAR=0.0
4518 )};
4519  
4520  
4521 470 ▶sub parse {
Subroutine parse: called CC=3 SCAR=11.0
4522 470 my __PACKAGE__ $self = shift;
4523 470 my ExtUtils::ParseXS $pxs = shift;
4524 470 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
4525 470 my ExtUtils::ParseXS::Node::xbody $xbody = shift;
4526  
4527 470 $self->SUPER::parse($pxs); # set file/line_no
4528  
4529 # Look for a CODE/PPCODE/NOT_IMPLEMENTED_YET keyword; if found, add
4530 # the kid to the current node.
4531 470 ▶ return 1 if $self->parse_keywords(
Branch: 1 branch
BranchTrueFalseTotal
if $self->parse_keywords($pxs, $xsub, $xbody, 1, "CODE|PPCODE", $keywords_flag_NOT_IMPLEMENTED_YET) 111 358 469
4532 $pxs, $xsub, $xbody,
4533 1, # match at most one keyword
4534 "CODE|PPCODE",
4535 $keywords_flag_NOT_IMPLEMENTED_YET,
4536 );
4537  
4538 # Didn't find a CODE keyword or similar, so auto-generate a call
4539 # to the same-named C library function.
4540  
4541 358 my $autocall = ExtUtils::ParseXS::Node::autocall->new();
4542 # mainly a NOOP, but sets line number etc and flags that autocall seen
4543 358 ▶ $autocall->parse($pxs, $xsub, $xbody)
Branch: 1 branch
BranchTrueFalseTotal
$autocall->parse($pxs, $xsub, $xbody) or return 358 0 358
4544 or return;
4545 358 push @{$self->{kids}}, $autocall;
4546  
4547 358 1;
4548 }
4549  
4550  
4551 458 ▶sub as_code {
Subroutine as_code: called CC=3 SCAR=11.0
4552 458 my __PACKAGE__ $self = shift;
4553 458 my ExtUtils::ParseXS $pxs = shift;
4554 458 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
4555 458 my ExtUtils::ParseXS::Node::xbody $xbody = shift;
4556  
4557 458 ▶ if ($self->{kids}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"kids"}) 458 0 458
4558 458 $_->as_code($pxs, $xsub, $xbody) for @{$self->{kids}};
4559 }
4560 }
4561  
4562  
4563 # ======================================================================
4564  
4565 package ExtUtils::ParseXS::Node::output_part;
4566  
4567 28 ▶BEGIN { $build_subclass->(
Subroutine BEGIN: called CC=1 SCAR=0.0
4568  
4569 # State during code emitting
4570  
4571 'targ_used', # Bool: the TARG has been allocated for this body,
4572 # so is no longer available for use.
4573  
4574 'stack_was_reset', # Bool: An XSprePUSH was emitted, so return values
4575 # should be PUSHed rather than just set.
4576 )};
4577  
4578  
4579 469 ▶sub parse {
Subroutine parse: called CC=18 SCAR=28.9
4580 469 my __PACKAGE__ $self = shift;
4581 469 my ExtUtils::ParseXS $pxs = shift;
4582 469 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
4583 469 my ExtUtils::ParseXS::Node::xbody $xbody = shift;
4584  
4585 469 $self->SUPER::parse($pxs); # set file/line_no
4586  
4587 # Repeatedly look for POSTCALL, OUTPUT or generic keywords,
4588 # parse the text following them, and add any resultant nodes
4589 # as kids to the current node.
4590 # XXX POSTCALL is documented to precede OUTPUT, but here we allow
4591 # them in any order and multiplicity.
4592 469 $self->parse_keywords(
4593 $pxs, $xsub, $xbody,
4594 undef, # implies process as many keywords as possible
4595 "POSTCALL|OUTPUT|"
4596 . $ExtUtils::ParseXS::Constants::generic_xsub_keywords_alt,
4597 );
4598  
4599 # Work out whether a RETVAL SV will be returned. Note that this should
4600 # be consistent across CASEs; we warn elsewhere if CODE_sets_ST0 isn't
4601 # consistent.
4602  
4603 $xsub->{XSRETURN_count_basic} =
4604 ( $xsub->{CODE_sets_ST0}
4605 or ( $xsub->{decl}{return_type}{type} ne "void"
4606 && !$xsub->{decl}{return_type}{no_output})
4607 469 ▶ )
Branch: 1 branch
BranchTrueFalseTotal
$xsub->{'CODE_sets_ST0'} || $xsub->{'decl'}{'return_type'}{'type'} ne 'void' && !$xsub->{'decl'}{'return_type'}{'no_output'} ? : 258 211 469
Condition100%6/6
$xsub->{"CODE_sets_ST0"} or $xsub->{"decl"}{"return_type"}{"type"} ne "void" and not $xsub->{"decl"}{"return_type"}{"no_output"}
l!l&&r!l&&!r
3255211
$xsub->{"decl"}{"return_type"}{"type"} ne "void" and not $xsub->{"decl"}{"return_type"}{"no_output"}
!ll&&!rl&&r
2038255
MC/DC100%3/3
$xsub->{"CODE_sets_ST0"} or $xsub->{"decl"}{"return_type"}{"type"} ne "void" and not $xsub->{"decl"}{"return_type"}{"no_output"}
$xsub->{"CODE_sets_ST0"} $xsub->{"decl"}{"return_type"}{"type"} ne "void" not $xsub->{"decl"}{"return_type"}{"no_output"}
Truth table100%4/4
A or B and not(C)
A= $xsub->{"CODE_sets_ST0"}B= $xsub->{"decl"}{"return_type"}{"type"} ne "void"C= not $xsub->{"decl"}{"return_type"}{"no_output"}
ABCresult
1XX1
0111
00X0
0100
4608 ? 1 : 0;
4609  
4610 # For each param, look up its OUTPUT typemap information now (at parse
4611 # time) and save the results for use later in as_output_code_().
4612  
4613 469 for my $ioparam (@{$xbody->{ioparams}{kids}}) {
4614 # might be placeholder param which doesn't get emitted
4615 # XXXX next unless defined $ioparam->{type};
4616  
4617 next unless
4618 # XXX simplify all this
4619 ( defined $ioparam->{in_out}
4620 && $ioparam->{in_out} =~ /OUT$/
4621 && !$ioparam->{in_output}
4622 )
4623 ||
4624  
4625 (
4626 $ioparam->{var} eq "RETVAL"
4627 && ( $ioparam->{in_output}
4628 or ( $xbody->{seen_autocall}
4629 && $xsub->{decl}{return_type}{type} ne "void"
4630 && !$xsub->{decl}{return_type}{no_output}
4631 )
4632 )
4633 )
4634 ||
4635 (
4636 $ioparam->{in_output}
4637 784 ▶ && $ioparam->{var} ne 'RETVAL'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ioparam->{"in_out"} and $ioparam->{"in_out"} =~ /OUT$/ and not $ioparam->{"in_output"} or $ioparam->{"var"} eq "RETVAL" and $ioparam->{"in_output"} || $xbody->{"seen_autocall"} && $xsub->{"decl"}{"return_type"}{"type"} ne "void" && !$xsub->{"decl"}{"return_type"}{"no_output"} or $ioparam->{"in_output"} and $ioparam->{"var"} ne "RETVAL" 514 270 784
Condition92%25/27
defined $ioparam->{"in_out"} and $ioparam->{"in_out"} =~ /OUT$/ and not $ioparam->{"in_output"} or $ioparam->{"var"} eq "RETVAL" and $ioparam->{"in_output"} || $xbody->{"seen_autocall"} && $xsub->{"decl"}{"return_type"}{"type"} ne "void" && !$xsub->{"decl"}{"return_type"}{"no_output"} or $ioparam->{"in_output"} and $ioparam->{"var"} ne "RETVAL"
l!l&&r!l&&!r
24723514
defined $ioparam->{"in_out"} and $ioparam->{"in_out"} =~ /OUT$/ and not $ioparam->{"in_output"} or $ioparam->{"var"} eq "RETVAL" and $ioparam->{"in_output"} || $xbody->{"seen_autocall"} && $xsub->{"decl"}{"return_type"}{"type"} ne "void" && !$xsub->{"decl"}{"return_type"}{"no_output"}
l!l&&r!l&&!r
18229537
defined $ioparam->{"in_out"} and $ioparam->{"in_out"} =~ /OUT$/ and not $ioparam->{"in_output"}
!ll&&!rl&&r
766018
defined $ioparam->{"in_out"} and $ioparam->{"in_out"} =~ /OUT$/
!ll&&!rl&&r
7184818
$ioparam->{"var"} eq "RETVAL" and $ioparam->{"in_output"} || $xbody->{"seen_autocall"} && $xsub->{"decl"}{"return_type"}{"type"} ne "void" && !$xsub->{"decl"}{"return_type"}{"no_output"}
!ll&&!rl&&r
49245229
$ioparam->{"in_output"} or $xbody->{"seen_autocall"} and $xsub->{"decl"}{"return_type"}{"type"} ne "void" and not $xsub->{"decl"}{"return_type"}{"no_output"}
l!l&&r!l&&!r
4518445
$xbody->{"seen_autocall"} and $xsub->{"decl"}{"return_type"}{"type"} ne "void" and not $xsub->{"decl"}{"return_type"}{"no_output"}
!ll&&!rl&&r
414184
$xbody->{"seen_autocall"} and $xsub->{"decl"}{"return_type"}{"type"} ne "void"
!ll&&!rl&&r
338188
$ioparam->{"in_output"} and $ioparam->{"var"} ne "RETVAL"
!ll&&!rl&&r
514023
MC/DC80%8/10
defined $ioparam->{"in_out"} and $ioparam->{"in_out"} =~ /OUT$/ and not $ioparam->{"in_output"} or $ioparam->{"var"} eq "RETVAL" and $ioparam->{"in_output"} || $xbody->{"seen_autocall"} && $xsub->{"decl"}{"return_type"}{"type"} ne "void" && !$xsub->{"decl"}{"return_type"}{"no_output"} or $ioparam->{"in_output"} and $ioparam->{"var"} ne "RETVAL"
defined $ioparam->{"in_out"} $ioparam->{"in_out"} =~ /OUT$/ not $ioparam->{"in_output"} $ioparam->{"var"} eq "RETVAL" $ioparam->{"in_output"} $xbody->{"seen_autocall"} $xsub->{"decl"}{"return_type"}{"type"} ne "void" not $xsub->{"decl"}{"return_type"}{"no_output"} $ioparam->{"in_output"} $ioparam->{"var"} ne "RETVAL"
Truth table23%10/43
A and B and not(C) or D and E or F and G and not(H) or I and J
A= defined $ioparam->{"in_out"}B= $ioparam->{"in_out"} =~ /OUT$/C= not $ioparam->{"in_output"}D= $ioparam->{"var"} eq "RETVAL"E= $ioparam->{"in_output"}F= $xbody->{"seen_autocall"}G= $xsub->{"decl"}{"return_type"}{"type"} ne "void"H= not $xsub->{"decl"}{"return_type"}{"no_output"}I= $ioparam->{"in_output"}J= $ioparam->{"var"} ne "RETVAL"
ABCDEFGHIJresult
111XXXXXXX1
0XX11XXXXX1
0XX10111XX1
10X11XXXXX1
10X10111XX1
11011XXXXX1
11010111XX1
0XX0XXXX111
0XX100XX111
0XX1010X111
0XX10110111
10X0XXXX111
10X100XX111
10X1010X111
10X10110111
1100XXXX111
110100XX111
1101010X111
11010110111
0XX0XXXX0X0
0XX0XXXX100
0XX100XX0X0
0XX100XX100
0XX1010X0X0
0XX1010X100
0XX101100X0
0XX10110100
10X0XXXX0X0
10X0XXXX100
10X100XX0X0
10X100XX100
10X1010X0X0
10X1010X100
10X101100X0
10X10110100
1100XXXX0X0
1100XXXX100
110100XX0X0
110100XX100
1101010X0X0
1101010X100
110101100X0
11010110100
4638 )
4639 ;
4640  
4641 $ioparam->{output_typemap_vals} =
4642 270 [ $ioparam->lookup_output_typemap($pxs, $xsub, $xbody) ];
4643 }
4644  
4645 469 my $out_num = $xsub->{XSRETURN_count_basic};
4646  
4647 469 for my $ioparam (@{$xbody->{ioparams}{kids}}) {
4648 next unless defined $ioparam->{in_out}
4649 784 ▶ && $ioparam->{in_out} =~ /OUTLIST$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ioparam->{"in_out"} and $ioparam->{"in_out"} =~ /OUTLIST$/ 736 48 784
Condition100%3/3
defined $ioparam->{"in_out"} and $ioparam->{"in_out"} =~ /OUTLIST$/
!ll&&!rl&&r
7181848
MC/DC100%2/2
defined $ioparam->{"in_out"} and $ioparam->{"in_out"} =~ /OUTLIST$/
defined $ioparam->{"in_out"} $ioparam->{"in_out"} =~ /OUTLIST$/
Truth table100%3/3
A and B
A= defined $ioparam->{"in_out"}B= $ioparam->{"in_out"} =~ /OUTLIST$/
ABresult
0X0
100
111
4650 $ioparam->{output_typemap_vals_outlist} =
4651 48 [ $ioparam->lookup_output_typemap($pxs, $xsub, $xbody, $out_num++) ];
4652 }
4653  
4654 469 1;
4655 }
4656  
4657  
4658 458 ▶sub as_code {
Subroutine as_code: called CC=19 SCAR=29.4
4659 458 my __PACKAGE__ $self = shift;
4660 458 my ExtUtils::ParseXS $pxs = shift;
4661 458 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
4662 458 my ExtUtils::ParseXS::Node::xbody $xbody = shift;
4663  
4664 # TARG is available for use within this body.
4665 458 $self->{targ_used} = 0;
4666  
4667 # SP still pointing at top arg
4668 458 $self->{stack_was_reset} = 0;
4669  
4670 458 ▶ if ($self->{kids}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"kids"}) 70 388 458
4671 70 $_->as_code($pxs, $xsub, $xbody) for @{$self->{kids}};
4672 }
4673  
4674 458 my $ioparams = $xbody->{ioparams};
4675  
4676 458 my $retval = $ioparams->{names}{RETVAL};
4677  
4678 # A CODE section using RETVAL must also have an OUTPUT entry
4679 458 ▶ if ( $xbody->{seen_RETVAL_in_CODE}
Branch: 1 branch
BranchTrueFalseTotal
if ($xbody->{"seen_RETVAL_in_CODE"} and not $retval && $retval->{"in_output"} and $xsub->{"decl"}{"return_type"}{"type"} ne "void") 3 455 458
Condition100%9/9
$xbody->{"seen_RETVAL_in_CODE"} and not $retval && $retval->{"in_output"} and $xsub->{"decl"}{"return_type"}{"type"} ne "void"
!ll&&!rl&&r
45413
$xbody->{"seen_RETVAL_in_CODE"} and not $retval && $retval->{"in_output"}
!ll&&!rl&&r
417374
$retval && $retval->{'in_output'}
!ll&&!rl&&r
1337
MC/DC50%2/4
$xbody->{"seen_RETVAL_in_CODE"} and not $retval && $retval->{"in_output"} and $xsub->{"decl"}{"return_type"}{"type"} ne "void"
$xbody->{"seen_RETVAL_in_CODE"} $retval $retval->{'in_output'} $xsub->{"decl"}{"return_type"}{"type"} ne "void"
Truth table66%4/6
A and not(B && C) and D
A= $xbody->{"seen_RETVAL_in_CODE"}B= $retvalC= $retval->{'in_output'}D= $xsub->{"decl"}{"return_type"}{"type"} ne "void"
ABCDresult
0XXX0
111X0
10X00
11000
10X11
11011
4680 and not ($retval && $retval->{in_output})
4681 and $xsub->{decl}{return_type}{type} ne 'void')
4682 {
4683 3 $pxs->Warn( "Warning: found a 'CODE' section which seems to be "
4684 . "using 'RETVAL' but no 'OUTPUT' section.");
4685 }
4686  
4687 # Process any OUT vars: i.e. vars that are declared OUT in
4688 # the XSUB's signature rather than in an OUTPUT section.
4689  
4690 458 for my $param (
4691 grep {
4692 defined $_->{in_out}
4693 && $_->{in_out} =~ /OUT$/
4694 && !$_->{in_output}
4695 773 ▶ }
Condition83%5/6
defined $_->{'in_out'} && $_->{'in_out'} =~ /OUT$/ && !$_->{'in_output'}
!ll&&!rl&&r
755018
defined $_->{"in_out"} and $_->{"in_out"} =~ /OUT$/
!ll&&!rl&&r
7074818
MC/DC66%2/3
defined $_->{'in_out'} && $_->{'in_out'} =~ /OUT$/ && !$_->{'in_output'}
defined $_->{"in_out"} $_->{"in_out"} =~ /OUT$/ !$_->{'in_output'}
Truth table75%3/4
A and B && not(C)
A= defined $_->{"in_out"}B= $_->{"in_out"} =~ /OUT$/C= !$_->{'in_output'}
ABCresult
0XX0
10X0
1100
1111
4696 458 @{$ioparams->{kids}})
4697 {
4698 18 $param->as_output_code($pxs, $xsub, $xbody);
4699 }
4700  
4701 458 my $basic = $xsub->{XSRETURN_count_basic};
4702 458 my $extra = $xsub->{XSRETURN_count_extra};
4703  
4704 458 ▶ if ($extra) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extra) 39 419 458
4705 # If there are any OUTLIST vars to be returned, we reset SP to
4706 # the base of the stack frame and then PUSH any return values.
4707 39 print "\tXSprePUSH;\n";
4708 39 $self->{stack_was_reset} = 1;
4709 }
4710  
4711 # Extend the stack if we're going to return more values than were
4712 # passed to us: which would consist of the GV or CV on the stack
4713 # plus at least min_args at the time ENTERSUB was called.
4714  
4715 458 my $n = $basic + $extra;
4716 print "\tEXTEND(SP,$n);\n"
4717 458 ▶ if $n > $ioparams->{min_args} + 1;
Branch: 1 branch
BranchTrueFalseTotal
if $n > $ioparams->{"min_args"} + 1 19 439 458
4718  
4719 # All OUTPUT done; now handle an implicit or deferred RETVAL:
4720 # - OUTPUT_line::as_code() will have skipped/deferred any RETVAL line,
4721 # - non-void CODE-less XSUBs have an implicit 'OUTPUT: RETVAL'
4722  
4723 458 ▶ if ( ($retval && $retval->{in_output})
Branch: 1 branch
BranchTrueFalseTotal
if ($retval and $retval->{"in_output"} or $xbody->{"seen_autocall"} and $xsub->{"decl"}{"return_type"}{"type"} ne "void" and not $xsub->{"decl"}{"return_type"}{"no_output"}) 219 239 458
Condition100%12/12
$retval and $retval->{"in_output"} or $xbody->{"seen_autocall"} and $xsub->{"decl"}{"return_type"}{"type"} ne "void" and not $xsub->{"decl"}{"return_type"}{"no_output"}
l!l&&r!l&&!r
45174239
$retval and $retval->{"in_output"}
!ll&&!rl&&r
19421945
$xbody->{"seen_autocall"} and $xsub->{"decl"}{"return_type"}{"type"} ne "void" and not $xsub->{"decl"}{"return_type"}{"no_output"}
!ll&&!rl&&r
2354174
$xbody->{"seen_autocall"} and $xsub->{"decl"}{"return_type"}{"type"} ne "void"
!ll&&!rl&&r
70165178
MC/DC100%5/5
$retval and $retval->{"in_output"} or $xbody->{"seen_autocall"} and $xsub->{"decl"}{"return_type"}{"type"} ne "void" and not $xsub->{"decl"}{"return_type"}{"no_output"}
$retval $retval->{"in_output"} $xbody->{"seen_autocall"} $xsub->{"decl"}{"return_type"}{"type"} ne "void" not $xsub->{"decl"}{"return_type"}{"no_output"}
Truth table77%7/9
A and B or C and D and not(E)
A= $retvalB= $retval->{"in_output"}C= $xbody->{"seen_autocall"}D= $xsub->{"decl"}{"return_type"}{"type"} ne "void"E= not $xsub->{"decl"}{"return_type"}{"no_output"}
ABCDEresult
11XXX1
0X1111
101111
0X0XX0
0X10X0
0X1100
100XX0
1010X0
101100
4724 or ( $xbody->{seen_autocall}
4725 && $xsub->{decl}{return_type}{type} ne "void"
4726 && !$xsub->{decl}{return_type}{no_output}
4727 )
4728 )
4729 {
4730 # emit a deferred RETVAL from OUTPUT or implicit RETVAL
4731 219 $retval->as_output_code($pxs, $xsub, $xbody);
4732 }
4733  
4734 # Now that RETVAL is on the stack, also push any OUTLIST vars too
4735 458 for my $param (grep { defined $_->{in_out}
4736 773 ▶ && $_->{in_out} =~ /OUTLIST$/
Condition100%3/3
defined $_->{'in_out'} && $_->{'in_out'} =~ /OUTLIST$/
!ll&&!rl&&r
7071848
MC/DC100%2/2
defined $_->{'in_out'} && $_->{'in_out'} =~ /OUTLIST$/
defined $_->{'in_out'} $_->{'in_out'} =~ /OUTLIST$/
Truth table100%3/3
A && B
A= defined $_->{'in_out'}B= $_->{'in_out'} =~ /OUTLIST$/
ABresult
0X0
100
111
4737 }
4738 458 @{$ioparams->{kids}}
4739 ) {
4740 48 $param->as_output_code($pxs, $xsub, $xbody, $basic++);
4741 }
4742 }
4743  
4744  
4745 # ======================================================================
4746  
4747 package ExtUtils::ParseXS::Node::cleanup_part;
4748  
4749 28 ▶BEGIN { $build_subclass->(
Subroutine BEGIN: called CC=1 SCAR=0.0
4750 )};
4751  
4752  
4753 469 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
4754 469 my __PACKAGE__ $self = shift;
4755 469 my ExtUtils::ParseXS $pxs = shift;
4756 469 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
4757 469 my ExtUtils::ParseXS::Node::xbody $xbody = shift;
4758  
4759 469 $self->SUPER::parse($pxs); # set file/line_no
4760  
4761 # Repeatedly look for CLEANUP or generic keywords,
4762 # parse the text following them, and add any resultant nodes
4763 # as kids to the current node.
4764 469 $self->parse_keywords(
4765 $pxs, $xsub, $xbody,
4766 undef, # implies process as many keywords as possible
4767 "CLEANUP|"
4768 . $ExtUtils::ParseXS::Constants::generic_xsub_keywords_alt,
4769 );
4770  
4771 469 1;
4772 }
4773  
4774  
4775 458 ▶sub as_code {
Subroutine as_code: called CC=3 SCAR=11.0
4776 458 my __PACKAGE__ $self = shift;
4777 458 my ExtUtils::ParseXS $pxs = shift;
4778 458 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
4779 458 my ExtUtils::ParseXS::Node::xbody $xbody = shift;
4780  
4781 458 ▶ if ($self->{kids}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"kids"}) 4 454 458
4782 4 $_->as_code($pxs, $xsub, $xbody) for @{$self->{kids}};
4783 }
4784 }
4785  
4786  
4787 # ======================================================================
4788  
4789 package ExtUtils::ParseXS::Node::oneline;
4790  
4791 # Generic base class for keyword Nodes which consume only a single source
4792 # line, such as 'SCOPE: ENABLE'.
4793 # On entry, $self->lines[0] will be any text (on the same line) which
4794 # follows the keyword.
4795  
4796 28 ▶BEGIN { $build_subclass->(
Subroutine BEGIN: called CC=1 SCAR=0.0
4797 'text', # Str: any text following the keyword
4798 )};
4799  
4800  
4801 965 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
4802 965 my __PACKAGE__ $self = shift;
4803 965 my ExtUtils::ParseXS $pxs = shift;
4804  
4805 965 $self->SUPER::parse($pxs); # set file/line_no
4806 965 my $s = shift @{$pxs->{line}};
4807 965 ExtUtils::ParseXS::Utilities::trim_whitespace($s);
4808 965 $self->{text} = $s;
4809 965 1;
4810 }
4811  
4812  
4813 # ======================================================================
4814  
4815 package ExtUtils::ParseXS::Node::MODULE;
4816  
4817 # Process a MODULE keyword, e.g.
4818 #
4819 # MODULE = Foo PACKAGE = Foo::Bar PREFIX = foo_
4820  
4821 28 ▶BEGIN { $build_subclass->(-parent => 'oneline',
Subroutine BEGIN: called CC=1 SCAR=0.0
4822 'module', # Str
4823 'package', # Str: may be ''
4824 'prefix', # Str: may be ''
4825 )};
4826  
4827  
4828 441 ▶sub parse {
Subroutine parse: called CC=5 SCAR=16.1
4829 441 my __PACKAGE__ $self = shift;
4830 441 my ExtUtils::ParseXS $pxs = shift;
4831  
4832 441 $self->SUPER::parse($pxs); # set file/line_no
4833  
4834 441 my $line = $self->{text};
4835 441 ▶ my ($module, $pkg, $prefix) = $line =~
Branch: 1 branch
BranchTrueFalseTotal
() = $line =~ /^ MODULE \s* = \s* ([\w:]+) (?: \s+ PACKAGE \s* = \s* ([\w:]+))? (?: \s+ PREFIX \s* = \s* (\S+))? \s* $/x or $pxs->death("Error: unparseable MODULE line: '${line}'") 437 4 441
4836 /^
4837 MODULE \s* = \s* ([\w:]+)
4838 (?: \s+ PACKAGE \s* = \s* ([\w:]+))?
4839 (?: \s+ PREFIX \s* = \s* (\S+))?
4840 \s*
4841 $/x
4842 or $pxs->death("Error: unparseable MODULE line: '$line'");
4843  
4844 437 $self->{module} = $module;
4845 437 ($pxs->{MODULE_cname} = $module) =~ s/\W/_/g;
4846  
4847 437 ▶ $self->{package} = $pxs->{PACKAGE_name} = defined($pkg) ? $pkg : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $pkg ? : 437 0 437
4848  
4849 437 ▶ $self->{prefix} = $prefix = defined($prefix) ? $prefix : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $prefix ? : 10 427 437
4850 437 $pxs->{PREFIX_pattern} = quotemeta($prefix);
4851  
4852 437 ($pxs->{PACKAGE_C_name} = $pxs->{PACKAGE_name}) =~ tr/:/_/;
4853  
4854 437 $pxs->{PACKAGE_class} = $pxs->{PACKAGE_name};
4855 437 ▶ $pxs->{PACKAGE_class} .= "::" if $pxs->{PACKAGE_class} ne "";
Branch: 1 branch
BranchTrueFalseTotal
if $pxs->{"PACKAGE_class"} ne "" 437 0 437
4856  
4857 437 1;
4858 }
4859  
4860  
4861 # ======================================================================
4862  
4863 package ExtUtils::ParseXS::Node::NOT_IMPLEMENTED_YET;
4864  
4865 # Handle NOT_IMPLEMENTED_YET pseudo-keyword
4866  
4867 28 ▶BEGIN { $build_subclass->(-parent => 'oneline',
Subroutine BEGIN: called CC=1 SCAR=0.0
4868 )};
4869  
4870 2 ▶sub as_code {
Subroutine as_code: called CC=1 SCAR=0.0
4871 2 my __PACKAGE__ $self = shift;
4872 2 my ExtUtils::ParseXS $pxs = shift;
4873 2 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
4874  
4875 2 print "\n"
4876 . "\tPerl_croak(aTHX_ \"$xsub->{decl}{full_perl_name}: "
4877 . "not implemented yet\");\n";
4878 }
4879  
4880  
4881 # ======================================================================
4882  
4883 package ExtUtils::ParseXS::Node::CASE;
4884  
4885 # Process the 'CASE:' keyword
4886  
4887 28 ▶BEGIN { $build_subclass->(-parent => 'oneline',
Subroutine BEGIN: called CC=1 SCAR=0.0
4888 'cond', # Str: the C code of the condition for the CASE, or ''
4889 'num', # Int: which CASE number this is (starting at 1)
4890 )};
4891  
4892  
4893 41 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
4894 41 my __PACKAGE__ $self = shift;
4895 41 my ExtUtils::ParseXS $pxs = shift;
4896  
4897 41 $self->SUPER::parse($pxs); # set file/line_no/text
4898 41 $self->{cond} = $self->{text};
4899 # Note that setting num, and consistency checking (like "else"
4900 # without "if") is done by the caller, Node::xsub.
4901 41 1;
4902 }
4903  
4904  
4905 40 ▶sub as_code {
Subroutine as_code: called CC=4 SCAR=13.9
4906 40 my __PACKAGE__ $self = shift;
4907 40 my ExtUtils::ParseXS $pxs = shift;
4908 40 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
4909  
4910 40 my $cond = $self->{cond};
4911 40 ▶ $cond = " if ($cond)" if length $cond;
Branch: 1 branch
BranchTrueFalseTotal
if length $cond 36 4 40
4912 40 ▶ print " ", ($self->{num} > 1 ? " else" : ""), $cond, "\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{'num'} > 1 ? : 24 16 40
4913 40 $_->as_code($pxs, $xsub) for @{$self->{kids}};
4914 }
4915  
4916  
4917 # ======================================================================
4918  
4919 package ExtUtils::ParseXS::Node::autocall;
4920  
4921 # Handle an empty XSUB body (i.e. no CODE or PPCODE)
4922 # by auto-generating a call to a C library function of the same
4923 # name
4924  
4925 28 ▶BEGIN { $build_subclass->(
Subroutine BEGIN: called CC=1 SCAR=0.0
4926 'args', # Str: text to use for auto function call arguments
4927 'types', # Str: text to use for auto function type declaration
4928 )};
4929  
4930  
4931 358 ▶sub parse {
Subroutine parse: called CC=11 SCAR=24.1
4932 358 my __PACKAGE__ $self = shift;
4933 358 my ExtUtils::ParseXS $pxs = shift;
4934 358 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
4935 358 my ExtUtils::ParseXS::Node::xbody $xbody = shift;
4936  
4937 358 $self->SUPER::parse($pxs); # set file/line_no
4938  
4939 358 $xbody->{seen_autocall} = 1;
4940  
4941 358 my $ioparams = $xbody->{ioparams};
4942 358 my ($args, $types);
4943 358 $args = $ioparams->{auto_function_sig_override}; # C_ARGS
4944 358 ▶ if (defined $args) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args) { } 18 340 358
4945 # Try to determine the C_ARGS types; for example, with
4946 #
4947 # foo(short s, int i, long l)
4948 # C_ARGS: s, l
4949 #
4950 # set $types to ['short', 'long']. May give the wrong results if
4951 # C_ARGS isn't just a simple list of parameter names
4952 18 for my $var (split /,/, $args) {
4953 37 $var =~ s/^\s+//;
4954 37 $var =~ s/\s+$//;
4955 37 my $param = $ioparams->{names}{$var};
4956 # 'void*' is a desperate guess if no such parameter
4957 my $type = $param && defined $param->{type}
4958 37 ▶ ? $param->{type} : 'void*';
Branch: 1 branch
BranchTrueFalseTotal
$param && defined $param->{'type'} ? : 16 21 37
Condition66%2/3
$param and defined $param->{"type"}
!ll&&!rl&&r
21016
MC/DC50%1/2
$param and defined $param->{"type"}
$param defined $param->{"type"}
Truth table66%2/3
A and B
A= $paramB= defined $param->{"type"}
ABresult
0X0
100
111
4959 $type .= '*' if $param && $param->{is_addr}
4960 37 ▶ || (defined $param->{in_out} && $param->{in_out} =~ /OUT/);
Branch: 1 branch
BranchTrueFalseTotal
if $param and $param->{"is_addr"} or defined $param->{"in_out"} and $param->{"in_out"} =~ /OUT/ 2 35 37
Condition66%6/9
$param and $param->{"is_addr"} or defined $param->{"in_out"} and $param->{"in_out"} =~ /OUT/
l!l&&r!l&&!r
2035
$param and $param->{"is_addr"}
!ll&&!rl&&r
21142
defined $param->{"in_out"} and $param->{"in_out"} =~ /OUT/
!ll&&!rl&&r
3500
MC/DC50%2/4
$param and $param->{"is_addr"} or defined $param->{"in_out"} and $param->{"in_out"} =~ /OUT/
$param $param->{"is_addr"} defined $param->{"in_out"} $param->{"in_out"} =~ /OUT/
Truth table42%3/7
A and B or C and D
A= $paramB= $param->{"is_addr"}C= defined $param->{"in_out"}D= $param->{"in_out"} =~ /OUT/
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
4961 37 push @$types, $type;
4962 }
4963 18 $self->{args} = $args;
4964 }
4965 else {
4966 340 ($args, $types) = $ioparams->C_func_signature($pxs);
4967 340 $self->{args} = join ', ', @$args;
4968 }
4969  
4970 358 ▶ unless ($pxs->{config_RetainCplusplusHierarchicalTypes}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pxs->{"config_RetainCplusplusHierarchicalTypes"}) 358 0 358
4971 358 s/:/_/g for @$types;
4972 }
4973 358 $self->{types} = join ', ', @$types;
4974  
4975 358 1;
4976 }
4977  
4978  
4979 348 ▶sub as_code {
Subroutine as_code: called CC=12 SCAR=24.9
4980 348 my __PACKAGE__ $self = shift;
4981 348 my ExtUtils::ParseXS $pxs = shift;
4982 348 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
4983 348 my ExtUtils::ParseXS::Node::xbody $xbody = shift;
4984  
4985 348 my $class = $xsub->{decl}{class};
4986 348 my $name = $xsub->{decl}{name};
4987  
4988 348 ▶ if ( defined $class
Branch: 1 branch
BranchTrueFalseTotal
if (defined $class and $name eq 'DESTROY') { } 1 347 348
Condition100%3/3
defined $class and $name eq "DESTROY"
!ll&&!rl&&r
321261
MC/DC100%2/2
defined $class and $name eq "DESTROY"
defined $class $name eq "DESTROY"
Truth table100%3/3
A and B
A= defined $classB= $name eq "DESTROY"
ABresult
0X0
100
111
4989 and $name eq "DESTROY")
4990 {
4991 # Emit a default body for a C++ DESTROY method: "delete THIS;"
4992 1 print "\n\t";
4993 1 print "delete THIS;\n";
4994  
4995 }
4996 else {
4997 # Emit a default body: this will be a call to the function being
4998 # wrapped. Typically:
4999 # RETVAL = foo(args);
5000 # with the function name being appropriately modified when it's
5001 # a C++ new() method etc.
5002  
5003 347 print "\n\t";
5004  
5005 347 my $ret_type = $xsub->{decl}{return_type}{type};
5006 347 ▶ if ($ret_type ne "void") {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret_type ne "void") 183 164 347
5007 183 print "RETVAL = ";
5008 }
5009  
5010 347 ▶ if (defined $class) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $class) 26 321 347
5011 26 ▶ if ($xsub->{decl}{return_type}{static}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xsub->{'decl'}{'return_type'}{'static'}) { } 4 22 26
5012 # it has a return type of 'static foo'
5013 4 ▶ if ($name eq 'new') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq 'new') { } 2 2 4
5014 2 $name = "$class";
5015 }
5016 else {
5017 2 print "${class}::";
5018 }
5019 }
5020 else {
5021 22 ▶ if ($name eq 'new') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq 'new') { } 7 15 22
5022 7 $name .= " $class";
5023 }
5024 else {
5025 15 print "THIS->";
5026 }
5027 }
5028 }
5029  
5030 # Handle "xsubpp -s=strip_prefix" hack
5031 347 my $strip = $pxs->{config_strip_c_func_prefix};
5032 347 ▶ $name =~ s/^\Q$strip//
Branch: 1 branch
BranchTrueFalseTotal
if defined $strip 0 347 347
5033 if defined $strip;
5034  
5035 347 ▶ if ( $xsub->{seen_INTERFACE}
Branch: 1 branch
BranchTrueFalseTotal
if ($xsub->{"seen_INTERFACE"} or $xsub->{"seen_INTERFACE_MACRO"}) 17 330 347
Condition66%2/3
$xsub->{"seen_INTERFACE"} or $xsub->{"seen_INTERFACE_MACRO"}
l!l&&r!l&&!r
170330
MC/DC50%1/2
$xsub->{"seen_INTERFACE"} or $xsub->{"seen_INTERFACE_MACRO"}
$xsub->{"seen_INTERFACE"} $xsub->{"seen_INTERFACE_MACRO"}
Truth table66%2/3
A or B
A= $xsub->{"seen_INTERFACE"}B= $xsub->{"seen_INTERFACE_MACRO"}
ABresult
1X1
011
000
5036 or $xsub->{seen_INTERFACE_MACRO})
5037 {
5038 $ret_type =~ s/:/_/g
5039 17 ▶ unless $pxs->{config_RetainCplusplusHierarchicalTypes};
Branch: 1 branch
BranchTrueFalseTotal
unless $pxs->{"config_RetainCplusplusHierarchicalTypes"} 17 0 17
5040 17 $name = "(($ret_type (*)($self->{types}))(XSFUNCTION))";
5041 }
5042  
5043 347 print "$name($self->{args});\n";
5044  
5045 }
5046 }
5047  
5048  
5049 # ======================================================================
5050  
5051 package ExtUtils::ParseXS::Node::FALLBACK;
5052  
5053 # Process the 'FALLBACK' keyword.
5054 # Its main effect is to update $pxs->{map_package_to_fallback_string} with
5055 # the fallback value for the current package. That is later used to plant
5056 # boot code to set ${package}::() to a true/false/undef value.
5057  
5058 28 ▶BEGIN { $build_subclass->(-parent => 'oneline',
Subroutine BEGIN: called CC=1 SCAR=0.0
5059 'value', # Str: TRUE, FALSE or UNDEF
5060 )};
5061  
5062  
5063 9 ▶sub parse {
Subroutine parse: called CC=5 SCAR=16.1
5064 9 my __PACKAGE__ $self = shift;
5065 9 my ExtUtils::ParseXS $pxs = shift;
5066  
5067 9 $self->SUPER::parse($pxs); # set file/line_no/text
5068  
5069 # The rest of the current line should contain either TRUE,
5070 # FALSE or UNDEF, but we also secretly allow 0 or 1 and lower/mixed
5071 # case.
5072  
5073 9 my $s = $self->{text};
5074  
5075 9 ▶ $s = 'TRUE' if $s eq '1';
Branch: 1 branch
BranchTrueFalseTotal
if $s eq 1 0 9 9
5076 9 ▶ $s = 'FALSE' if $s eq '0';
Branch: 1 branch
BranchTrueFalseTotal
if $s eq 0 0 9 9
5077 9 $s = uc($s);
5078  
5079 9 ▶ $pxs->death("Error: FALLBACK: invalid value '$s' (should be TRUE/FALSE/UNDEF)")
Branch: 1 branch
BranchTrueFalseTotal
unless $s =~ /^(TRUE|FALSE|UNDEF)$/ 1 8 9
5080 unless $s =~ /^(TRUE|FALSE|UNDEF)$/;
5081  
5082 8 $self->{value} = $s;
5083  
5084 # Only one FALLBACK allowed per package
5085  
5086 $pxs->Warn("Warning: duplicate FALLBACK: entry")
5087 8 ▶ if exists $pxs->{map_package_to_fallback_string}{$pxs->{PACKAGE_name}};
Branch: 1 branch
BranchTrueFalseTotal
if exists $pxs->{"map_package_to_fallback_string"}{$pxs->{"PACKAGE_name"}} 1 7 8
5088  
5089 8 $pxs->{map_package_to_fallback_string}{$pxs->{PACKAGE_name}} = $s;
5090  
5091 8 1;
5092 }
5093  
5094  
5095 # ======================================================================
5096  
5097 package ExtUtils::ParseXS::Node::REQUIRE;
5098  
5099 # Process the 'REQUIRE' keyword.
5100  
5101 28 ▶BEGIN { $build_subclass->(-parent => 'oneline',
Subroutine BEGIN: called CC=1 SCAR=0.0
5102 'version', # Str: the minimum version allowed, e.g.'1.23'
5103 )};
5104  
5105  
5106 7 ▶sub parse {
Subroutine parse: called CC=4 SCAR=13.9
5107 7 my __PACKAGE__ $self = shift;
5108 7 my ExtUtils::ParseXS $pxs = shift;
5109  
5110 7 $self->SUPER::parse($pxs); # set file/line_no/text
5111  
5112 7 my $ver = $self->{text};
5113  
5114 7 ▶ $pxs->death("Error: REQUIRE expects a version number")
Branch: 1 branch
BranchTrueFalseTotal
unless length $ver 1 6 7
5115 unless length $ver;
5116  
5117 # check that the version number is of the form n.n
5118 6 ▶ $pxs->death("Error: REQUIRE: expected a MMM(.NNN) number, got '$ver'")
Branch: 1 branch
BranchTrueFalseTotal
unless $ver =~ /^\d+(\.\d*)?$/ 2 4 6
5119 unless $ver =~ /^\d+(\.\d*)?$/;
5120  
5121 4 my $got = $ExtUtils::ParseXS::VERSION;
5122 4 ▶ $pxs->death("Error: xsubpp $ver (or better) required--this is only $got.")
Branch: 1 branch
BranchTrueFalseTotal
unless $got >= $ver 1 3 4
5123 unless $got >= $ver;
5124  
5125 3 $self->{version} = $ver;
5126  
5127 3 1;
5128 }
5129  
5130  
5131 # ======================================================================
5132  
5133 package ExtUtils::ParseXS::Node::include;
5134  
5135 # Common base class for the 'INCLUDE' and 'INCLUDE_COMMAND' keywords
5136  
5137 28 ▶BEGIN { $build_subclass->(-parent => 'oneline',
Subroutine BEGIN: called CC=1 SCAR=0.0
5138 'is_cmd', # Bool: is INCLUDE_COMMAND
5139 'inc_filename', # Str: the file/command to be included
5140 'old_filename', # Str: the previous file
5141 )};
5142  
5143  
5144 13 ▶sub parse {
Subroutine parse: called CC=26 SCAR=32.8
5145 13 my __PACKAGE__ $self = shift;
5146 13 my ExtUtils::ParseXS $pxs = shift;
5147  
5148 13 $self->SUPER::parse($pxs); # set file/line_no/text
5149  
5150 13 my $f = $self->{text};
5151 13 my $is_cmd = $self->{is_cmd};
5152 13 ▶ my $key = $is_cmd ? 'INCLUDE_COMMAND' : 'INCLUDE';
Branch: 1 branch
BranchTrueFalseTotal
$is_cmd ? : 6 7 13
5153  
5154 13 ▶ if ($is_cmd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_cmd) { } 6 7 13
5155 6 ▶ $f = $self->QuoteArgs($f) if $^O eq 'VMS';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "VMS" 0 6 6
5156  
5157 6 ▶ $pxs->death("Error: INCLUDE_COMMAND: command missing")
Branch: 1 branch
BranchTrueFalseTotal
unless length $f 1 5 6
5158 unless length $f;
5159  
5160 5 ▶ $pxs->death("Error: INCLUDE_COMMAND: pipes are illegal")
Branch: 1 branch
BranchTrueFalseTotal
if $f =~ /^\s*\|/ or $f =~ /\|\s*$/ 2 3 5
Condition100%3/3
$f =~ /^\s*\|/ or $f =~ /\|\s*$/
l!l&&r!l&&!r
113
MC/DC100%2/2
$f =~ /^\s*\|/ or $f =~ /\|\s*$/
$f =~ /^\s*\|/ $f =~ /\|\s*$/
Truth table100%3/3
A or B
A= $f =~ /^\s*\|/B= $f =~ /\|\s*$/
ABresult
1X1
011
000
5161 if $f =~ /^\s*\|/ or $f =~ /\|\s*$/;
5162 }
5163 else {
5164 7 ▶ $pxs->death("Error: INCLUDE: filename missing")
Branch: 1 branch
BranchTrueFalseTotal
unless length $f 1 6 7
5165 unless length $f;
5166  
5167 6 ▶ $pxs->death("Error: INCLUDE: output pipe is illegal")
Branch: 1 branch
BranchTrueFalseTotal
if $f =~ /^\s*\|/ 1 5 6
5168 if $f =~ /^\s*\|/;
5169  
5170 # simple minded recursion detector
5171 $pxs->death("Error: INCLUDE: loop detected")
5172 5 ▶ if $pxs->{IncludedFiles}{$f};
Branch: 1 branch
BranchTrueFalseTotal
if $pxs->{"IncludedFiles"}{$f} 1 4 5
5173  
5174 4 ▶ ++$pxs->{IncludedFiles}->{$f} unless $f =~ /\|\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $f =~ /\|\s*$/ 4 0 4
5175  
5176 4 ▶ if ($f =~ /\|\s*$/ && $f =~ /^\s*perl\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f =~ /\|\s*$/ and $f =~ /^\s*perl\s/) 0 4 4
Condition33%1/3
$f =~ /\|\s*$/ and $f =~ /^\s*perl\s/
!ll&&!rl&&r
400
MC/DC0%0/2
$f =~ /\|\s*$/ and $f =~ /^\s*perl\s/
$f =~ /\|\s*$/ $f =~ /^\s*perl\s/
Truth table33%1/3
A and B
A= $f =~ /\|\s*$/B= $f =~ /^\s*perl\s/
ABresult
0X0
100
111
5177 0 $pxs->WarnHint(
5178 "Note: the INCLUDE directive with a command is discouraged",
5179 <<'HINT');
5180 Use INCLUDE_COMMAND instead! In particular, using 'perl' in an
5181 'INCLUDE: ... |' directive is not guaranteed to pick up the correct perl.
5182 The INCLUDE_COMMAND directive allows the use of $^X as the currently
5183 running perl, see 'perldoc perlxs' for details.
5184 HINT
5185 }
5186 }
5187  
5188 # Save the current file context.
5189  
5190 7 my @save_keys = qw(in_fh in_filename in_pathname
5191 lastline lastline_no line line_no);
5192 7 my @saved = @$pxs{@save_keys};
5193  
5194 7 ▶ my $isPipe = $is_cmd || $pxs->{in_filename} =~ /\|\s*$/;
Condition66%2/3
$is_cmd || $pxs->{'in_filename'} =~ /\|\s*$/
l!l&&r!l&&!r
304
MC/DC50%1/2
$is_cmd || $pxs->{'in_filename'} =~ /\|\s*$/
$is_cmd $pxs->{'in_filename'} =~ /\|\s*$/
Truth table66%2/3
A || B
A= $is_cmdB= $pxs->{'in_filename'} =~ /\|\s*$/
ABresult
1X1
011
000
5195  
5196 7 $pxs->{line} = [];
5197 7 $pxs->{line_no} = [];
5198  
5199 # Open the new file / pipe
5200  
5201 7 $pxs->{in_fh} = Symbol::gensym();
5202  
5203 7 ▶ if ($is_cmd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_cmd) { } 3 4 7
5204 # Expand the special token '$^X' into the full path of the
5205 # currently running perl interpreter
5206 3 my $X = $pxs->_safe_quote($^X); # quotes if has spaces
5207 3 $f =~ s/^\s*\$\^X/$X/;
5208  
5209 3 ▶ open ($pxs->{in_fh}, "-|", $f)
Branch: 1 branch
BranchTrueFalseTotal
open $pxs->{"in_fh"}, "-|", $f or $pxs->death("Error: INCLUDE_COMMAND: cannot run command '${f}' to include its output: $!") 3 0 3
5210 or $pxs->death(
5211 "Error: INCLUDE_COMMAND: cannot run command '$f' to include its output: $!");
5212 }
5213 else {
5214 4 ▶ open($pxs->{in_fh}, $f)
Branch: 1 branch
BranchTrueFalseTotal
open $pxs->{"in_fh"}, $f or $pxs->death("Error: INCLUDE: cannot open '${f}': $!") 3 1 4
5215 or $pxs->death("Error: INCLUDE: cannot open '$f': $!");
5216 }
5217  
5218 6 $self->{old_filename} = $pxs->{in_filename};
5219 6 $self->{inc_filename} = $f;
5220 6 $pxs->{in_filename} = $f;
5221  
5222 6 my $path = $f;
5223 6 ▶ if ($is_cmd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_cmd) { } 3 3 6
5224 #$path =~ s/\"/\\"/g; # Fails? See CPAN RT #53938: MinGW Broken after 2.21
5225 3 $path =~ s/\\/\\\\/g; # Works according to reporter of #53938
5226 }
5227 else {
5228 $path = ($^O =~ /^mswin/i)
5229 # See CPAN RT #61908: gcc doesn't like
5230 # backslashes on win32?
5231 ? "$pxs->{dir}/$path"
5232 3 ▶ : File::Spec->catfile($pxs->{dir}, $path);
Branch: 1 branch
BranchTrueFalseTotal
$^O =~ /^mswin/i ? : 0 3 3
5233 }
5234 6 $pxs->{in_pathname} = $self->{file} = $path;
5235  
5236 # Prime the pump by reading the first non-blank line
5237 6 ▶ while (readline($pxs->{in_fh})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $pxs->{"in_fh"})) 9 1 10
5238 9 ▶ last unless /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^\s*$/ 5 4 9
5239 }
5240  
5241 6 $pxs->{lastline} = $_;
5242 6 ▶ chomp $pxs->{lastline} if defined $pxs->{lastline};
Branch: 1 branch
BranchTrueFalseTotal
if defined $pxs->{"lastline"} 5 1 6
5243 6 $pxs->{lastline_no} = $self->{line_no} = $.;
5244  
5245 # Parse included file
5246  
5247 my $cpp_scope = ExtUtils::ParseXS::Node::cpp_scope->new({
5248 type => 'include',
5249 is_cmd => $self->{is_cmd},
5250 6 });
5251 6 ▶ $cpp_scope->parse($pxs)
Branch: 1 branch
BranchTrueFalseTotal
$cpp_scope->parse($pxs) or return 5 0 5
5252 or return;
5253 5 push @{$self->{kids}}, $cpp_scope;
5254  
5255 --$pxs->{IncludedFiles}->{$pxs->{in_filename}}
5256 5 ▶ unless $isPipe;
Branch: 1 branch
BranchTrueFalseTotal
unless $isPipe 2 3 5
5257  
5258 5 close $pxs->{in_fh};
5259  
5260 # Restore the current file context.
5261  
5262 5 @$pxs{@save_keys} = @saved;
5263  
5264 5 ▶ if ($isPipe and $?) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isPipe and $?) 1 4 5
Condition100%3/3
$isPipe and $?
!ll&&!rl&&r
221
MC/DC100%2/2
$isPipe and $?
$isPipe $?
Truth table100%3/3
A and B
A= $isPipeB= $?
ABresult
0X0
100
111
5265 $pxs->death(sprintf
5266 "Error: %s: got return code 0x%04x when reading from pipe '%s'",
5267 $key, $?, $self->{inc_filename}
5268 1 );
5269 }
5270  
5271 4 1;
5272 }
5273  
5274  
5275 4 ▶sub as_code {
Subroutine as_code: called CC=3 SCAR=11.0
5276 4 my __PACKAGE__ $self = shift;
5277 4 my ExtUtils::ParseXS $pxs = shift;
5278  
5279 my $comment = $self->{is_cmd}
5280 4 ▶ ? "INCLUDE_COMMAND: Including output of"
Branch: 1 branch
BranchTrueFalseTotal
$self->{'is_cmd'} ? : 2 2 4
5281 : "INCLUDE: Including";
5282  
5283 4 $comment .= " '$self->{inc_filename}' from '$self->{old_filename}'";
5284  
5285 4 print $self->Q(<<"EOF");
5286 |
5287 |/* $comment */
5288 |
5289 EOF
5290  
5291 4 $_->as_code($pxs) for @{$self->{kids}};
5292  
5293 4 print $self->Q(<<"EOF");
5294 |
5295 |/* INCLUDE: Returning to '$self->{old_filename}' from '$self->{inc_filename}' */
5296 |
5297 EOF
5298  
5299 }
5300  
5301  
5302 # ======================================================================
5303  
5304 package ExtUtils::ParseXS::Node::INCLUDE;
5305  
5306 # Process the 'INCLUDE' keyword. Most processing is actually done by the
5307 # parent 'include' class which handles INCLUDE_COMMAND too.
5308  
5309 28 ▶BEGIN { $build_subclass->(-parent => 'include',
Subroutine BEGIN: called CC=1 SCAR=0.0
5310 )};
5311  
5312  
5313 7 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
5314 7 my __PACKAGE__ $self = shift;
5315 7 my ExtUtils::ParseXS $pxs = shift;
5316  
5317 7 $self->{is_cmd} = 0;
5318 7 $self->SUPER::parse($pxs); # main parsing done by Node::include
5319 2 1;
5320 }
5321  
5322  
5323 # ======================================================================
5324  
5325 package ExtUtils::ParseXS::Node::INCLUDE_COMMAND;
5326  
5327 # Process the 'INCLUDE_COMMAND' keyword. Most processing is actually done
5328 # by the parent 'include' class which handles INCLUDE too.
5329  
5330 28 ▶BEGIN { $build_subclass->(-parent => 'include',
Subroutine BEGIN: called CC=1 SCAR=0.0
5331 )};
5332  
5333  
5334 6 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
5335 6 my __PACKAGE__ $self = shift;
5336 6 my ExtUtils::ParseXS $pxs = shift;
5337  
5338 6 $self->{is_cmd} = 1;
5339 6 $self->SUPER::parse($pxs); # main parsing done by Node::include
5340 2 1;
5341 }
5342  
5343  
5344 # ======================================================================
5345  
5346 package ExtUtils::ParseXS::Node::enable;
5347  
5348 # Base class for keywords which accept ENABLE/DISABLE as an argument
5349  
5350 28 ▶BEGIN { $build_subclass->(-parent => 'oneline',
Subroutine BEGIN: called CC=1 SCAR=0.0
5351 'enable', # Bool
5352 )};
5353  
5354  
5355 452 ▶sub parse {
Subroutine parse: called CC=8 SCAR=20.8
5356 452 my __PACKAGE__ $self = shift;
5357 452 my ExtUtils::ParseXS $pxs = shift;
5358  
5359 452 $self->SUPER::parse($pxs); # set file/line_no, self->{text}
5360 452 my $s = $self->{text};
5361  
5362 452 my ($keyword) = ($self =~ /(\w+)=/); # final component of class name
5363  
5364 452 ▶ if ($keyword eq 'PROTOTYPES') {
Branch: 1 branch
BranchTrueFalseTotal
if ($keyword eq 'PROTOTYPES') { } 432 20 452
5365 # For backwards compatibility, parsing the PROTOTYPES
5366 # keyword's value is very lax: in particular, anything that
5367 # didn't match 'ENABLE' (such as 'Enabled' or 'ENABLED') used to
5368 # be treated as valid but false. Continue to use this
5369 # interpretation for backcomp, but warn.
5370  
5371 432 ▶ unless ($s =~ /^ ((ENABLE|DISABLE) D? ;?) \s* $ /xi) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($s =~ /^ ((ENABLE|DISABLE) D? ;?) \s* $ /xi) 3 429 432
5372 3 $pxs->death("Error: $keyword: invalid value '$s' (should be ENABLE/DISABLE)")
5373 }
5374 429 my ($value, $en_dis) = ($1, $2);
5375 429 ▶ $self->{enable} = $en_dis eq 'ENABLE' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$en_dis eq 'ENABLE' ? : 26 403 429
5376 429 ▶ unless ($value =~ /^(ENABLE|DISABLE)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /^(ENABLE|DISABLE)$/) 4 425 429
5377 $pxs->Warn("Warning: invalid PROTOTYPES value '$value' interpreted as "
5378 4 ▶ . ($self->{enable} ? 'ENABLE' : 'DISABLE'));
Branch: 1 branch
BranchTrueFalseTotal
$self->{'enable'} ? : 2 2 4
5379 }
5380 }
5381 else {
5382 # SCOPE / VERSIONCHECK / EXPORT_XSUB_SYMBOLS
5383 20 ▶ $s =~ /^(ENABLE|DISABLE)\s*$/
Branch: 1 branch
BranchTrueFalseTotal
$s =~ /^(ENABLE|DISABLE)\s*$/ or $pxs->death("Error: ${keyword}: invalid value '${s}' (should be ENABLE/DISABLE)") 7 13 20
5384 or $pxs->death(
5385 "Error: $keyword: invalid value '$s' (should be ENABLE/DISABLE)"
5386 );
5387 7 ▶ $self->{enable} = $1 eq 'ENABLE' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq 'ENABLE' ? : 5 2 7
5388 }
5389  
5390 436 1;
5391 }
5392  
5393  
5394 # ======================================================================
5395  
5396 package ExtUtils::ParseXS::Node::EXPORT_XSUB_SYMBOLS;
5397  
5398 # Handle EXPORT_XSUB_SYMBOLS keyword
5399 #
5400 # Note that this keyword can appear both inside of and outside of an
5401 # XSUB; for the latter, it it is currently created as a temporary
5402 # object where as_code() is called immediately after parse() and then
5403 # the object is discarded.
5404  
5405 28 ▶BEGIN { $build_subclass->(-parent => 'enable',
Subroutine BEGIN: called CC=1 SCAR=0.0
5406 )};
5407  
5408  
5409 3 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.2
5410 3 my __PACKAGE__ $self = shift;
5411 3 my ExtUtils::ParseXS $pxs = shift;
5412  
5413 3 $self->SUPER::parse($pxs); # set file/line_no, self->{enable}
5414 0 1;
5415 }
5416  
5417  
5418 0 ▶sub as_code {
Subroutine as_code: not called CC=2 SCAR=17.9
5419 0 my __PACKAGE__ $self = shift;
5420 0 my ExtUtils::ParseXS $pxs = shift;
5421 0 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
5422 0 my ExtUtils::ParseXS::Node::xbody $xbody = shift;
5423  
5424 0 ▶ my $xs_impl = $self->{enable} ? 'XS_EXTERNAL' : 'XS_INTERNAL';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'enable'} ? : 0 0 0
5425  
5426 # Change the definition of XS_EUPXS, so that any subsequent
5427 # XS_EUPXS(fXS_Foo_foo) XSUB declarations will expand to
5428 # XS_EXTERNAL/XS_INTERNAL as appropriate
5429  
5430 0 print $self->Q(<<"EOF");
5431 |#undef XS_EUPXS
5432 |#if defined(PERL_EUPXS_ALWAYS_EXPORT)
5433 |# define XS_EUPXS(name) XS_EXTERNAL(name)
5434 |#elif defined(PERL_EUPXS_NEVER_EXPORT)
5435 |# define XS_EUPXS(name) XS_INTERNAL(name)
5436 |#else
5437 |# define XS_EUPXS(name) $xs_impl(name)
5438 |#endif
5439 EOF
5440 }
5441  
5442  
5443 # ======================================================================
5444  
5445 package ExtUtils::ParseXS::Node::PROTOTYPES;
5446  
5447 # Handle PROTOTYPES keyword
5448 #
5449 # Note that this keyword can appear both inside of and outside of an XSUB.
5450  
5451 28 ▶BEGIN { $build_subclass->(-parent => 'enable',
Subroutine BEGIN: called CC=1 SCAR=0.0
5452 )};
5453  
5454  
5455 432 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
5456 432 my __PACKAGE__ $self = shift;
5457 432 my ExtUtils::ParseXS $pxs = shift;
5458  
5459 432 $self->SUPER::parse($pxs); # set file/line_no, self->{enable}
5460 429 $pxs->{PROTOTYPES_value} = $self->{enable};
5461 429 $pxs->{proto_behaviour_specified} = 1;
5462 429 1;
5463 }
5464  
5465  
5466 # ======================================================================
5467  
5468 package ExtUtils::ParseXS::Node::SCOPE;
5469  
5470 # Handle SCOPE keyword
5471 #
5472 # Note that this keyword can appear both inside of and outside of an XSUB.
5473  
5474 28 ▶BEGIN { $build_subclass->(-parent => 'enable',
Subroutine BEGIN: called CC=1 SCAR=0.0
5475 )};
5476  
5477  
5478 12 ▶sub parse {
Subroutine parse: called CC=4 SCAR=13.9
5479 12 my __PACKAGE__ $self = shift;
5480 12 my ExtUtils::ParseXS $pxs = shift;
5481 12 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
5482 12 my ExtUtils::ParseXS::Node::xbody $xbody = shift;
5483  
5484 12 $self->SUPER::parse($pxs); # set file/line_no, self->{enable}
5485  
5486 # $xsub not defined for file-scoped SCOPE
5487 6 ▶ if ($xsub) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xsub) 4 2 6
5488 $pxs->blurt("Error: only one SCOPE declaration allowed per XSUB")
5489 4 ▶ if $xsub->{seen_SCOPE};
Branch: 1 branch
BranchTrueFalseTotal
if $xsub->{"seen_SCOPE"} 1 3 4
5490 4 $xsub->{seen_SCOPE} = 1;
5491 }
5492  
5493 # Note that currently this parse method can be called either while
5494 # parsing an XSUB, or while processing file-scoped keywords
5495 # just before an XSUB declaration. So potentially set both types of
5496 # state.
5497 6 ▶ $xsub->{SCOPE_enabled} = $self->{enable} if $xsub;
Branch: 1 branch
BranchTrueFalseTotal
if $xsub 4 2 6
5498 6 $pxs->{file_SCOPE_enabled} = $self->{enable};
5499 6 1;
5500 }
5501  
5502  
5503 # ======================================================================
5504  
5505 package ExtUtils::ParseXS::Node::VERSIONCHECK;
5506  
5507 # Handle VERSIONCHECK keyword
5508 #
5509 # Note that this keyword can appear both inside of and outside of an XSUB.
5510  
5511 28 ▶BEGIN { $build_subclass->(-parent => 'enable',
Subroutine BEGIN: called CC=1 SCAR=0.0
5512 )};
5513  
5514  
5515 5 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
5516 5 my __PACKAGE__ $self = shift;
5517 5 my ExtUtils::ParseXS $pxs = shift;
5518  
5519 5 $self->SUPER::parse($pxs); # set file/line_no, self->{enable}
5520 1 $pxs->{VERSIONCHECK_value} = $self->{enable};
5521 1 1;
5522 }
5523  
5524  
5525 # ======================================================================
5526  
5527 package ExtUtils::ParseXS::Node::multiline;
5528  
5529 # Generic base class for keyword Nodes which can contain multiple lines,
5530 # e.g. C code or other data: so anything from ALIAS to PPCODE.
5531 # On entry, $self->lines[0] will be any text (on the same line) which
5532 # follows the keyword.
5533  
5534 28 ▶BEGIN { $build_subclass->(
Subroutine BEGIN: called CC=1 SCAR=0.0
5535 'lines', # Array ref of all lines until the next keyword
5536 )};
5537  
5538  
5539 # Consume all the lines up until the next directive and store in @$lines.
5540  
5541 197 ▶sub parse {
Subroutine parse: called CC=3 SCAR=11.0
5542 197 my __PACKAGE__ $self = shift;
5543 197 my ExtUtils::ParseXS $pxs = shift;
5544  
5545 197 $self->SUPER::parse($pxs); # set file/line_no
5546  
5547 197 my @lines;
5548  
5549 # Consume lines until the next directive
5550 197 ▶ while( @{$pxs->{line}}
Branch: 1 branch
BranchTrueFalseTotal
if (@{$pxs->{"line"};} and not $pxs->{"line"}[0] =~ /^$ExtUtils::ParseXS::BLOCK_regexp/o) 374 197 571
Condition100%3/3
@{$pxs->{"line"};} and not $pxs->{"line"}[0] =~ /^$ExtUtils::ParseXS::BLOCK_regexp/o
!ll&&!rl&&r
10295374
MC/DC100%2/2
@{$pxs->{"line"};} and not $pxs->{"line"}[0] =~ /^$ExtUtils::ParseXS::BLOCK_regexp/o
@{$pxs->{"line"};} not $pxs->{"line"}[0] =~ /^$ExtUtils::ParseXS::BLOCK_regexp/o
Truth table100%3/3
A and not(B)
A= @{$pxs->{"line"};}B= not $pxs->{"line"}[0] =~ /^$ExtUtils::ParseXS::BLOCK_regexp/o
ABresult
0X0
100
111
5551 && $pxs->{line}[0] !~ /^$ExtUtils::ParseXS::BLOCK_regexp/o)
5552 {
5553 374 push @lines, shift @{$pxs->{line}};
5554 }
5555  
5556 197 $self->{lines} = \@lines;
5557 197 1;
5558 }
5559  
5560 # No as_code() method - we rely on the sub-classes for that
5561  
5562  
5563 # ======================================================================
5564  
5565 package ExtUtils::ParseXS::Node::multiline_merged;
5566  
5567 # Generic base class for keyword Nodes which can contain multiple lines.
5568 # It's the same is is parent class, :Node::multiline, except that in
5569 # addition, leading blank lines are skipped and the remainder concatenated
5570 # into a single line, 'text'.
5571  
5572 28 ▶BEGIN { $build_subclass->(-parent => 'multiline',
Subroutine BEGIN: called CC=1 SCAR=0.0
5573 'text', # Str: singe string containing all concatenated lines
5574 )};
5575  
5576  
5577 # Consume all the lines up until the next directive and store in
5578 # @$lines, and in addition, concatenate and store in $text
5579  
5580 49 ▶sub parse {
Subroutine parse: called CC=3 SCAR=11.0
5581 49 my __PACKAGE__ $self = shift;
5582 49 my ExtUtils::ParseXS $pxs = shift;
5583  
5584 49 $self->SUPER::parse($pxs); # set file/line_no, read lines
5585  
5586 49 my @lines = @{$self->{lines}};
5587 49 ▶ shift @lines while @lines && $lines[0] !~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
@lines and not $lines[0] =~ /\S/ and do { shift @lines } 6 49 55
Condition100%3/3
@lines and not $lines[0] =~ /\S/
!ll&&!rl&&r
1486
MC/DC100%2/2
@lines and not $lines[0] =~ /\S/
@lines not $lines[0] =~ /\S/
Truth table100%3/3
A and not(B)
A= @linesB= not $lines[0] =~ /\S/
ABresult
0X0
100
111
5588 # XXX ParseXS originally didn't include a trailing \n,
5589 # so we carry on doing the same.
5590 49 $self->{text} = join "\n", @lines;
5591 49 ExtUtils::ParseXS::Utilities::trim_whitespace($self->{text});
5592 49 1;
5593 }
5594  
5595 # No as_code() method - we rely on the sub-classes for that
5596  
5597  
5598 # ======================================================================
5599  
5600 package ExtUtils::ParseXS::Node::C_ARGS;
5601  
5602 # Handle C_ARGS keyword
5603  
5604 28 ▶BEGIN { $build_subclass->(-parent => 'multiline_merged',
Subroutine BEGIN: called CC=1 SCAR=0.0
5605 )};
5606  
5607  
5608 18 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
5609 18 my __PACKAGE__ $self = shift;
5610 18 my ExtUtils::ParseXS $pxs = shift;
5611 18 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
5612 18 my ExtUtils::ParseXS::Node::xbody $xbody = shift;
5613  
5614 18 $self->SUPER::parse($pxs); # set file/line_no, get lines, set text
5615 18 $xbody->{ioparams}{auto_function_sig_override} = $self->{text};
5616 18 1;
5617 }
5618  
5619  
5620 # ======================================================================
5621  
5622 package ExtUtils::ParseXS::Node::INTERFACE;
5623  
5624 # Handle INTERFACE keyword
5625  
5626 28 ▶BEGIN { $build_subclass->(-parent => 'multiline_merged',
Subroutine BEGIN: called CC=1 SCAR=0.0
5627 )};
5628  
5629  
5630 17 ▶sub parse {
Subroutine parse: called CC=3 SCAR=11.0
5631 17 my __PACKAGE__ $self = shift;
5632 17 my ExtUtils::ParseXS $pxs = shift;
5633 17 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
5634 17 my ExtUtils::ParseXS::Node::xbody $xbody = shift;
5635  
5636 17 $self->SUPER::parse($pxs); # set file/line_no, get lines, set text
5637 17 $xsub->{seen_INTERFACE} = 1;
5638  
5639 17 foreach (split /[\s,]+/, $self->{text}) {
5640 22 my $short = $_;
5641 22 $short =~ s/^$pxs->{PREFIX_pattern}//;
5642 $pxs->blurt("Error: duplicate INTERFACE name: '$short'")
5643 22 ▶ if exists $xsub->{map_interface_name_short_to_original}{$short};
Branch: 1 branch
BranchTrueFalseTotal
if exists $xsub->{"map_interface_name_short_to_original"}{$short} 1 21 22
5644  
5645 22 $xsub->{map_interface_name_short_to_original}{$short} = $_;
5646 }
5647  
5648 17 1;
5649 }
5650  
5651  
5652 17 ▶sub as_code {
Subroutine as_code: called CC=3 SCAR=11.0
5653 17 my __PACKAGE__ $self = shift;
5654 17 my ExtUtils::ParseXS $pxs = shift;
5655 17 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
5656 17 my ExtUtils::ParseXS::Node::xbody $xbody = shift;
5657  
5658 17 my $macro = $xsub->{interface_macro};
5659 17 ▶ $macro = 'XSINTERFACE_FUNC' unless defined $macro;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $macro 15 2 17
5660  
5661 17 my $type = $xsub->{decl}{return_type}{type};
5662 $type =~ tr/:/_/
5663 17 ▶ unless $pxs->{config_RetainCplusplusHierarchicalTypes};
Branch: 1 branch
BranchTrueFalseTotal
unless $pxs->{"config_RetainCplusplusHierarchicalTypes"} 17 0 17
5664 17 print <<"EOF";
5665 XSFUNCTION = $macro($type,cv,XSANY.any_dptr);
5666 EOF
5667 }
5668  
5669  
5670 # ======================================================================
5671  
5672 package ExtUtils::ParseXS::Node::INTERFACE_MACRO;
5673  
5674 # Handle INTERFACE_MACRO keyword
5675  
5676 28 ▶BEGIN { $build_subclass->(-parent => 'multiline_merged',
Subroutine BEGIN: called CC=1 SCAR=0.0
5677 'get_macro', # Str: name of macro to get interface
5678 'set_macro', # Str: name of macro to set interface
5679 )};
5680  
5681  
5682 2 ▶sub parse {
Subroutine parse: called CC=2 SCAR=7.0
5683 2 my __PACKAGE__ $self = shift;
5684 2 my ExtUtils::ParseXS $pxs = shift;
5685 2 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
5686 2 my ExtUtils::ParseXS::Node::xbody $xbody = shift;
5687  
5688 2 $self->SUPER::parse($pxs); # set file/line_no, get lines, set text
5689  
5690 2 $xsub->{seen_INTERFACE_MACRO} = 1;
5691  
5692 2 my $s = $self->{text};
5693 2 my ($m1, $m2);
5694 2 ▶ if ($s =~ /\s/) { # two macros
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\s/) { } 2 0 2
5695 2 ($m1, $m2) = split ' ', $s;
5696 }
5697 else {
5698 # XXX rather than using a fake macro name which will probably
5699 # give a compile error later, we should really warn/die here?
5700 0 ($m1, $m2) = ($s, 'UNKNOWN_CVT');
5701 }
5702  
5703 2 $self->{get_macro} = $xsub->{interface_macro} = $m1;
5704 2 $self->{set_macro} = $xsub->{interface_macro_set} = $m2;
5705  
5706 2 1;
5707 }
5708  
5709  
5710 # ======================================================================
5711  
5712 package ExtUtils::ParseXS::Node::OVERLOAD;
5713  
5714 # Handle OVERLOAD keyword
5715  
5716 28 ▶BEGIN { $build_subclass->(-parent => 'multiline_merged',
Subroutine BEGIN: called CC=1 SCAR=0.0
5717 'ops', # Hash ref of seen overloaded op names
5718 )};
5719  
5720 # Add all overload method names, like 'cmp', '<=>', etc, (possibly
5721 # multiple ones per line) until the next keyword line, as 'seen' keys to
5722 # the $xsub->{overload_name_seen} hash.
5723  
5724 12 ▶sub parse {
Subroutine parse: called CC=4 SCAR=13.9
5725 12 my __PACKAGE__ $self = shift;
5726 12 my ExtUtils::ParseXS $pxs = shift;
5727 12 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
5728 12 my ExtUtils::ParseXS::Node::xbody $xbody = shift;
5729  
5730 12 $self->SUPER::parse($pxs); # set file/line_no, get lines, set text
5731  
5732 12 my $s = $self->{text};
5733  
5734 # Note that this doesn't forbid invalid overload op names, to allow
5735 # for forwards compatibility. It does warn/skip ones which don't
5736 # seem to be even vaguely syntactically correct.
5737 12 for my $op (split ' ', $s) {
5738 93 ▶ if ($op !~ m{^
Branch: 1 branch
BranchTrueFalseTotal
unless ($op =~ m[^ ^ ( [\w:"\\)+\-*/%<>.&|^!~{}=\@\$]+ ) $ ]x) 1 92 93
5739 ^
5740 ( [\w:"\\)+\-*/%<>.&|^!~{}=\@\$]+ )
5741 $
5742 }x)
5743  
5744 {
5745 1 $pxs->Warn("Warning: unrecognised OVERLOAD op name '$op' ignored");
5746 1 next;
5747 }
5748  
5749 92 $self->{ops}{$op} = 1;
5750 $pxs->Warn("Warning: duplicate OVERLOAD op name: '$op'")
5751 92 ▶ if exists $xsub->{overload_name_seen}{$op};
Branch: 1 branch
BranchTrueFalseTotal
if exists $xsub->{"overload_name_seen"}{$op} 1 91 92
5752 92 $xsub->{overload_name_seen}{$op} = 1;
5753 }
5754  
5755 # Mark the current package as being overloaded
5756 $pxs->{map_overloaded_package_to_C_package}->{$xsub->{PACKAGE_name}}
5757 12 = $xsub->{PACKAGE_C_name};
5758  
5759 12 1;
5760 }
5761  
5762  
5763 # ======================================================================
5764  
5765 package ExtUtils::ParseXS::Node::ATTRS;
5766  
5767 # Handle ATTRS keyword
5768  
5769 28 ▶BEGIN { $build_subclass->(-parent => 'multiline',
Subroutine BEGIN: called CC=1 SCAR=0.0
5770 )};
5771  
5772  
5773 # Read each lines's worth of attributes into a string that is pushed
5774 # to the $xsub->{attributes} array. Note that it doesn't matter that multiple
5775 # space-separated attributes on the same line are stored as a single
5776 # string; later, all the attribute lines are joined together into a single
5777 # string to pass to apply_attrs_string().
5778  
5779 5 ▶sub parse {
Subroutine parse: called CC=2 SCAR=6.9
5780 5 my __PACKAGE__ $self = shift;
5781 5 my ExtUtils::ParseXS $pxs = shift;
5782 5 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
5783 5 my ExtUtils::ParseXS::Node::xbody $xbody = shift;
5784  
5785 5 $self->SUPER::parse($pxs); # set file/line_no, get lines
5786 5 for (@{$self->{lines}}) {
5787 6 ExtUtils::ParseXS::Utilities::trim_whitespace($_);
5788 6 push @{$xsub->{attributes}}, $_;
5789 }
5790 5 1;
5791 }
5792  
5793  
5794 # ======================================================================
5795  
5796 package ExtUtils::ParseXS::Node::PROTOTYPE;
5797  
5798 # Handle PROTOTYPE keyword
5799  
5800 28 ▶BEGIN { $build_subclass->(-parent => 'multiline',
Subroutine BEGIN: called CC=1 SCAR=0.0
5801 'prototype', # Str: 0 (disable), 1 (enable), 2 ("") or "$$@" etc
5802 )};
5803  
5804  
5805 # PROTOTYPE: Process one or more lines of the form
5806 # DISABLE
5807 # ENABLE
5808 # $$@ # a literal prototype
5809 # <blank> # an empty prototype - equivalent to foo() { ...}
5810 #
5811 # The last line takes precedence.
5812 # XXX It's a design flaw that more than one line can be processed.
5813  
5814 12 ▶sub parse {
Subroutine parse: called CC=8 SCAR=20.8
5815 12 my __PACKAGE__ $self = shift;
5816 12 my ExtUtils::ParseXS $pxs = shift;
5817 12 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
5818 12 my ExtUtils::ParseXS::Node::xbody $xbody = shift;
5819  
5820 12 $self->SUPER::parse($pxs); # set file/line_no, get lines
5821  
5822 12 my $proto;
5823  
5824 $pxs->death("Error: only one PROTOTYPE definition allowed per xsub")
5825 12 ▶ if $xsub->{seen_PROTOTYPE};
Branch: 1 branch
BranchTrueFalseTotal
if $xsub->{"seen_PROTOTYPE"} 1 11 12
5826 11 $xsub->{seen_PROTOTYPE} = 1;
5827  
5828 11 for (@{$self->{lines}}) {
5829 18 ▶ next unless /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless /\S/ 6 12 18
5830 12 ExtUtils::ParseXS::Utilities::trim_whitespace($_);
5831  
5832 12 ▶ if ($_ eq 'DISABLE') {
Branch: 2 branches
BranchTrueFalseTotal
if ($_ eq 'DISABLE') { } 2 10 12
elsif ($_ eq 'ENABLE') { } 1 9 10
5833 2 $proto = 0;
5834 }
5835 elsif ($_ eq 'ENABLE') {
5836 1 $proto = 1;
5837 }
5838 else {
5839 9 s/\s+//g; # remove any whitespace
5840 9 ▶ $pxs->death("Error: invalid prototype '$_'")
Branch: 1 branch
BranchTrueFalseTotal
unless ExtUtils::ParseXS::Utilities::valid_proto_string($_) 1 8 9
5841 unless ExtUtils::ParseXS::Utilities::valid_proto_string($_);
5842 8 $proto = ExtUtils::ParseXS::Utilities::C_string($_);
5843 }
5844 }
5845  
5846 # If no prototype specified, then assume empty prototype ""
5847 10 ▶ $proto = 2 unless defined $proto;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $proto 1 9 10
5848  
5849 10 $self->{prototype} = $proto;
5850 10 $xsub->{prototype} = $proto;
5851  
5852 10 $pxs->{proto_behaviour_specified} = 1;
5853 10 1;
5854 }
5855  
5856  
5857 # ======================================================================
5858  
5859 package ExtUtils::ParseXS::Node::codeblock;
5860  
5861 # Base class for Nodes which contain lines of literal C code
5862 # (such as PREINIT: and CODE:)
5863  
5864 28 ▶BEGIN { $build_subclass->(-parent => 'multiline',
Subroutine BEGIN: called CC=1 SCAR=0.0
5865 )};
5866  
5867 131 ▶sub parse {
Subroutine parse: called CC=3 SCAR=11.0
5868 131 my __PACKAGE__ $self = shift;
5869 131 my ExtUtils::ParseXS $pxs = shift;
5870 131 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
5871 131 my ExtUtils::ParseXS::Node::xbody $xbody = shift;
5872  
5873 131 $self->SUPER::parse($pxs); # use multiline::parse()
5874  
5875 # Text following the keyword is ignored rather than being treated
5876 # as the first line of code.
5877 131 my $line0 = shift @{$self->{lines}};
5878 131 ▶ $pxs->Warn("Warning: text after keyword ignored: '$line0'")
Branch: 1 branch
BranchTrueFalseTotal
if defined $line0 and $line0 =~ /\S/ 6 125 131
Condition66%2/3
defined $line0 and $line0 =~ /\S/
!ll&&!rl&&r
01256
MC/DC50%1/2
defined $line0 and $line0 =~ /\S/
defined $line0 $line0 =~ /\S/
Truth table66%2/3
A and B
A= defined $line0B= $line0 =~ /\S/
ABresult
0X0
100
111
5879 if defined $line0 && $line0 =~ /\S/;
5880 131 return 1;
5881 }
5882  
5883 # Emit the lines of code, skipping any initial blank lines,
5884 # and possibly wrapping in '#line' directives.
5885  
5886 129 ▶sub as_code {
Subroutine as_code: called CC=9 SCAR=22.6
5887 129 my __PACKAGE__ $self = shift;
5888 129 my ExtUtils::ParseXS $pxs = shift;
5889 129 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
5890 129 my ExtUtils::ParseXS::Node::xbody $xbody = shift;
5891  
5892 129 my @lines = map "$_\n", @{$self->{lines}};
5893  
5894 129 my $n = 1;
5895  
5896 # strip leading blank lines
5897 129 ▶ $n++, shift @lines while @lines && $lines[0] !~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
@lines and not $lines[0] =~ /\S/ and do { ++$n, shift @lines } 0 129 129
Condition66%2/3
@lines and not $lines[0] =~ /\S/
!ll&&!rl&&r
51240
MC/DC0%0/2
@lines and not $lines[0] =~ /\S/
@lines not $lines[0] =~ /\S/
Truth table66%2/3
A and not(B)
A= @linesB= not $lines[0] =~ /\S/
ABresult
0X0
100
111
5898  
5899 # Add a leading '#line' if needed.
5900 # The XSubPPtmp test is a bit of a hack - it skips synthetic blocks
5901 # added to boot etc which may not have line numbers.
5902 129 my $line0 = $lines[0];
5903 129 ▶ if ( $pxs->{config_WantLineNumbers}
Branch: 1 branch
BranchTrueFalseTotal
if ($pxs->{"config_WantLineNumbers"} and not defined $line0 && ($line0 =~ /^\s*#\s*line\b/ || $line0 =~ /^#if XSubPPtmp/)) 115 14 129
Condition55%5/9
$pxs->{"config_WantLineNumbers"} and not defined $line0 && ($line0 =~ /^\s*#\s*line\b/ || $line0 =~ /^#if XSubPPtmp/)
!ll&&!rl&&r
140115
defined $line0 && ($line0 =~ /^\s*#\s*line\b/ || $line0 =~ /^#if XSubPPtmp/)
!ll&&!rl&&r
51100
$line0 =~ /^\s*#\s*line\b/ or $line0 =~ /^#if XSubPPtmp/
l!l&&r!l&&!r
00110
MC/DC25%1/4
$pxs->{"config_WantLineNumbers"} and not defined $line0 && ($line0 =~ /^\s*#\s*line\b/ || $line0 =~ /^#if XSubPPtmp/)
$pxs->{"config_WantLineNumbers"} defined $line0 $line0 =~ /^\s*#\s*line\b/ $line0 =~ /^#if XSubPPtmp/
Truth table60%3/5
A and not(B && C or D)
A= $pxs->{"config_WantLineNumbers"}B= defined $line0C= $line0 =~ /^\s*#\s*line\b/D= $line0 =~ /^#if XSubPPtmp/
ABCDresult
0XXX0
111X0
11010
10XX1
11001
5904 && ! ( defined $line0
5905 && ( $line0 =~ /^\s*#\s*line\b/
5906 || $line0 =~ /^#if XSubPPtmp/
5907 )
5908 )
5909 ) {
5910 unshift @lines,
5911 "#line "
5912 . ($self->{line_no} + $n)
5913 . " \""
5914 . ExtUtils::ParseXS::Utilities::escape_file_for_line_directive(
5915 $self->{file})
5916 115 . "\"\n";
5917 }
5918  
5919 # Add a final "restoring" '#line'
5920 push @lines, 'ExtUtils::ParseXS::CountLines'->end_marker . "\n"
5921 129 ▶ if $pxs->{config_WantLineNumbers};
Branch: 1 branch
BranchTrueFalseTotal
if $pxs->{"config_WantLineNumbers"} 115 14 129
5922  
5923 129 print for @lines;
5924 }
5925  
5926  
5927 # ======================================================================
5928  
5929 package ExtUtils::ParseXS::Node::CODE;
5930  
5931 # Store the code lines associated with the CODE keyword
5932  
5933 28 ▶BEGIN { $build_subclass->(-parent => 'codeblock',
Subroutine BEGIN: called CC=1 SCAR=0.0
5934 )};
5935  
5936 101 ▶sub parse {
Subroutine parse: called CC=4 SCAR=13.9
5937 101 my __PACKAGE__ $self = shift;
5938 101 my ExtUtils::ParseXS $pxs = shift;
5939 101 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
5940 101 my ExtUtils::ParseXS::Node::xbody $xbody = shift;
5941  
5942 101 $self->SUPER::parse($pxs); # set file/line_no/lines
5943  
5944 # Check if the code block includes "RETVAL". This check is for later
5945 # use to warn if RETVAL is used but no OUTPUT block is present.
5946 # Ignore if its only being used in an 'ignore this var' situation.
5947 101 my $code = join "\n", @{$self->{lines}};
5948 $xbody->{seen_RETVAL_in_CODE} =
5949 101 ▶ $code =~ /\bRETVAL\b/
Condition100%3/3
$code =~ /\bRETVAL\b/ && !($code =~ /\bPERL_UNUSED_VAR\(RETVAL\)/)
!ll&&!rl&&r
56441
MC/DC100%2/2
$code =~ /\bRETVAL\b/ && !($code =~ /\bPERL_UNUSED_VAR\(RETVAL\)/)
$code =~ /\bRETVAL\b/ !($code =~ /\bPERL_UNUSED_VAR\(RETVAL\)/)
Truth table100%3/3
A && not(B)
A= $code =~ /\bRETVAL\b/B= !($code =~ /\bPERL_UNUSED_VAR\(RETVAL\)/)
ABresult
0X0
100
111
5950 && $code !~ /\b\QPERL_UNUSED_VAR(RETVAL)/;
5951  
5952 # Horrible 'void' return arg count hack.
5953 #
5954 # Until about 1996, xsubpp always emitted 'XSRETURN(1)', even for a
5955 # void XSUB. This was fixed for CODE-less void XSUBs simply by
5956 # actually honouring the 'void' type and emitting 'XSRETURN_EMPTY'
5957 # instead. However, for CODE blocks, the documentation had already
5958 # endorsed a coding style along the lines of
5959 #
5960 # void
5961 # foo(...)
5962 # CODE:
5963 # ST(0) = sv_newmortal();
5964 #
5965 # i.e. the XSUB returns an SV even when the return type is 'void'.
5966 # In 2024 there is still lots of code of this style out in the wild,
5967 # even in the distros bundled with perl.
5968 #
5969 # So honouring the void type here breaks lots of existing code. Thus
5970 # this hack specifically looks for: void XSUBs with a CODE block that
5971 # appears to put stuff on the stack via 'ST(n)=' or 'XST_m()', and if
5972 # so, emits 'XSRETURN(1)' rather than the 'XSRETURN_EMPTY' implied by
5973 # the 'void' return type.
5974 #
5975 # So set a flag which indicates that a CODE block sets ST(0). This
5976 # will be used later when deciding how/whether to emit EXTEND(n) and
5977 # XSRETURN(n).
5978  
5979 101 my $st0 =
5980 $code =~ m{ ( \b ST \s* \( [^;]* = )
5981 | ( \b XST_m\w+\s* \( ) }x;
5982  
5983 $pxs->Warn("Warning: ST(0) isn't consistently set in every CASE's CODE block")
5984 if defined $xsub->{CODE_sets_ST0}
5985 101 ▶ && $xsub->{CODE_sets_ST0} ne $st0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $xsub->{"CODE_sets_ST0"} and $xsub->{"CODE_sets_ST0"} ne $st0 1 100 101
Condition100%3/3
defined $xsub->{"CODE_sets_ST0"} and $xsub->{"CODE_sets_ST0"} ne $st0
!ll&&!rl&&r
89111
MC/DC100%2/2
defined $xsub->{"CODE_sets_ST0"} and $xsub->{"CODE_sets_ST0"} ne $st0
defined $xsub->{"CODE_sets_ST0"} $xsub->{"CODE_sets_ST0"} ne $st0
Truth table100%3/3
A and B
A= defined $xsub->{"CODE_sets_ST0"}B= $xsub->{"CODE_sets_ST0"} ne $st0
ABresult
0X0
100
111
5986 101 $xsub->{CODE_sets_ST0} = $st0;
5987  
5988 101 1;
5989 }
5990  
5991  
5992 # ======================================================================
5993  
5994 package ExtUtils::ParseXS::Node::CLEANUP;
5995  
5996 # Store the code lines associated with the CLEANUP: keyword
5997  
5998 28 ▶BEGIN { $build_subclass->(-parent => 'codeblock',
Subroutine BEGIN: called CC=1 SCAR=0.0
5999 )};
6000  
6001 # Currently all methods are just inherited.
6002  
6003  
6004 # ======================================================================
6005  
6006 package ExtUtils::ParseXS::Node::INIT;
6007  
6008 # Store the code lines associated with the INIT: keyword
6009  
6010 28 ▶BEGIN { $build_subclass->(-parent => 'codeblock',
Subroutine BEGIN: called CC=1 SCAR=0.0
6011 )};
6012  
6013 # Currently all methods are just inherited.
6014  
6015  
6016 # ======================================================================
6017  
6018 package ExtUtils::ParseXS::Node::POSTCALL;
6019  
6020 # Store the code lines associated with the POSTCALL: keyword
6021  
6022 28 ▶BEGIN { $build_subclass->(-parent => 'codeblock',
Subroutine BEGIN: called CC=1 SCAR=0.0
6023 )};
6024  
6025 # Currently all methods are just inherited.
6026  
6027  
6028 # ======================================================================
6029  
6030 package ExtUtils::ParseXS::Node::PPCODE;
6031  
6032 # Store the code lines associated with the PPCODE keyword
6033  
6034 28 ▶BEGIN { $build_subclass->(-parent => 'codeblock',
Subroutine BEGIN: called CC=1 SCAR=0.0
6035 )};
6036  
6037 9 ▶sub parse {
Subroutine parse: called CC=2 SCAR=6.9
6038 9 my __PACKAGE__ $self = shift;
6039 9 my ExtUtils::ParseXS $pxs = shift;
6040 9 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
6041 9 my ExtUtils::ParseXS::Node::xbody $xbody = shift;
6042  
6043 9 $self->SUPER::parse($pxs); # set file/line_no/lines
6044 9 $xsub->{seen_PPCODE} = 1;
6045 9 ▶ $pxs->death("Error: PPCODE must be the last thing") if @{$pxs->{line}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$pxs->{"line"};} 1 8 9
6046 8 1;
6047 }
6048  
6049  
6050 8 ▶sub as_code {
Subroutine as_code: called CC=4 SCAR=14.2
6051 8 my __PACKAGE__ $self = shift;
6052 8 my ExtUtils::ParseXS $pxs = shift;
6053 8 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
6054 8 my ExtUtils::ParseXS::Node::xbody $xbody = shift;
6055  
6056 # Just emit the code block and then code to do PUTBACK and return.
6057 # The # user of PPCODE is supposed to have done all the return stack
6058 # manipulation themselves.
6059 # Note that PPCODE blocks often include a XSRETURN(1) or
6060 # similar, so any final code we emit after that is in danger of
6061 # triggering a "statement is unreachable" warning.
6062  
6063 8 $self->SUPER::as_code($pxs, $xsub, $xbody); # emit code block
6064  
6065 8 ▶ print "\tLEAVE;\n" if $xsub->{SCOPE_enabled};
Branch: 1 branch
BranchTrueFalseTotal
if $xsub->{"SCOPE_enabled"} 0 8 8
6066  
6067 # Suppress "statement is unreachable" warning on HPUX
6068 8 ▶ print "#if defined(__HP_cc) || defined(__HP_aCC)\n",
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "hpux" 0 8 8
6069 "#pragma diag_suppress 2111\n",
6070 "#endif\n"
6071 if $^O eq "hpux";
6072  
6073 8 print "\tPUTBACK;\n\treturn;\n";
6074  
6075 # Suppress "statement is unreachable" warning on HPUX
6076 8 ▶ print "#if defined(__HP_cc) || defined(__HP_aCC)\n",
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "hpux" 0 8 8
6077 "#pragma diag_default 2111\n",
6078 "#endif\n"
6079 if $^O eq "hpux";
6080 }
6081  
6082  
6083 # ======================================================================
6084  
6085 package ExtUtils::ParseXS::Node::PREINIT;
6086  
6087 # Store the code lines associated with the PREINIT: keyword
6088  
6089 28 ▶BEGIN { $build_subclass->(-parent => 'codeblock',
Subroutine BEGIN: called CC=1 SCAR=0.0
6090 )};
6091  
6092 # Currently all methods are just inherited.
6093  
6094  
6095 # ======================================================================
6096  
6097 package ExtUtils::ParseXS::Node::keylines;
6098  
6099 # Base class for keyword FOO nodes which have a FOO_line kid node for
6100 # each line making up the keyword - such as OUTPUT etc.
6101  
6102 28 ▶BEGIN { $build_subclass->(
Subroutine BEGIN: called CC=1 SCAR=0.0
6103 'lines', # Array ref of all lines until the next keyword
6104 )};
6105  
6106  
6107 # Process each line on and following the keyword line.
6108 # For each line, create a FOO_line kid and call its parse() method.
6109  
6110 574 ▶sub parse {
Subroutine parse: called CC=7 SCAR=19.5
6111 574 my __PACKAGE__ $self = shift;
6112 574 my ExtUtils::ParseXS $pxs = shift;
6113 574 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
6114 574 my ExtUtils::ParseXS::Node::xbody $xbody = shift;
6115 574 my $do_notimplemented = shift;
6116  
6117 574 $self->SUPER::parse($pxs); # set file/line_no
6118  
6119 # Consume and process lines until the next directive.
6120 574 ▶ while( @{$pxs->{line}}
Branch: 1 branch
BranchTrueFalseTotal
if (@{$pxs->{"line"};} and not $pxs->{"line"}[0] =~ /^$ExtUtils::ParseXS::BLOCK_regexp/o) 353 567 920
Condition100%3/3
@{$pxs->{"line"};} and not $pxs->{"line"}[0] =~ /^$ExtUtils::ParseXS::BLOCK_regexp/o
!ll&&!rl&&r
333234353
MC/DC100%2/2
@{$pxs->{"line"};} and not $pxs->{"line"}[0] =~ /^$ExtUtils::ParseXS::BLOCK_regexp/o
@{$pxs->{"line"};} not $pxs->{"line"}[0] =~ /^$ExtUtils::ParseXS::BLOCK_regexp/o
Truth table100%3/3
A and not(B)
A= @{$pxs->{"line"};}B= not $pxs->{"line"}[0] =~ /^$ExtUtils::ParseXS::BLOCK_regexp/o
ABresult
0X0
100
111
6121 && $pxs->{line}[0] !~ /^$ExtUtils::ParseXS::BLOCK_regexp/o)
6122 {
6123 353 ▶ if ($do_notimplemented) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_notimplemented) 153 200 353
6124 # treat NOT_IMPLEMENTED_YET as another block separator, in
6125 # addition to $BLOCK_regexp.
6126 153 ▶ last if $pxs->{line}[0] =~ /^\s*NOT_IMPLEMENTED_YET/;
Branch: 1 branch
BranchTrueFalseTotal
if $pxs->{"line"}[0] =~ /^\s*NOT_IMPLEMENTED_YET/ 2 151 153
6127 }
6128  
6129 351 ▶ unless ($pxs->{line}[0] =~ /\S/) { # skip blank lines
Branch: 1 branch
BranchTrueFalseTotal
unless ($pxs->{"line"}[0] =~ /\S/) 69 282 351
6130 69 shift @{$pxs->{line}};
6131 69 next;
6132 }
6133  
6134 282 push @{$self->{lines}}, $pxs->{line}[0];
6135  
6136 282 my $class = ref($self) . '_line';
6137 282 my $kid = $class->new();
6138 # Keep the current line in $self->{lines} for now so that the
6139 # parse() method below sees the right line number. We rely on that
6140 # method to actually pop the line.
6141 282 ▶ if ($kid->parse($pxs, $xsub, $xbody, $self)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kid->parse($pxs, $xsub, $xbody, $self)) 252 25 277
6142 252 push @{$self->{kids}}, $kid;
6143 }
6144 }
6145  
6146 569 1;
6147 }
6148  
6149  
6150 # call as_code() on any kids which have that method
6151  
6152 191 ▶sub as_code {
Subroutine as_code: called CC=3 SCAR=11.0
6153 191 my __PACKAGE__ $self = shift;
6154 191 my ExtUtils::ParseXS $pxs = shift;
6155 191 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
6156 191 my ExtUtils::ParseXS::Node::xbody $xbody = shift;
6157  
6158 191 ▶ return unless $self->{kids};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"kids"} 8 183 191
6159 183 $_->as_code($pxs, $xsub, $xbody) for @{$self->{kids}};
6160 }
6161  
6162  
6163 # ======================================================================
6164  
6165 package ExtUtils::ParseXS::Node::keyline;
6166  
6167 # Base class for FOO_line nodes which have a FOO node as
6168 # their parent.
6169  
6170 28 ▶BEGIN { $build_subclass->(
Subroutine BEGIN: called CC=1 SCAR=0.0
6171 'line', # Str: text of current line
6172 )};
6173  
6174  
6175 # The two jobs of this parse method are to grab the next line, and also to
6176 # set the right line number for any warning or error messages triggered by
6177 # the current line. It is called as a SUPER by the parse() methods of its
6178 # concrete subclasses.
6179  
6180 282 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
6181 282 my __PACKAGE__ $self = shift;
6182 282 my ExtUtils::ParseXS $pxs = shift;
6183  
6184 282 $self->SUPER::parse($pxs); # set file/line_no
6185 # By shifting *now*, the line above gets the correct line number of
6186 # this src line, while subsequent processing gives the right line
6187 # number for warnings etc, since the warn/err methods assume the line
6188 # being processed has already been popped.
6189 282 my $line = shift @{$pxs->{line}}; # line of text to be processed
6190 282 $self->{line} = $line;
6191 282 1;
6192 }
6193  
6194  
6195 # ======================================================================
6196  
6197 package ExtUtils::ParseXS::Node::ALIAS;
6198  
6199 # Handle ALIAS keyword
6200  
6201 28 ▶BEGIN { $build_subclass->(-parent => 'keylines',
Subroutine BEGIN: called CC=1 SCAR=0.0
6202 'aliases', # hashref of all alias => value pairs.
6203 # Populated by ALIAS_line::parse()
6204 )};
6205  
6206 23 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
6207 23 my __PACKAGE__ $self = shift;
6208 23 my ExtUtils::ParseXS $pxs = shift;
6209 23 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
6210 23 my ExtUtils::ParseXS::Node::xbody $xbody = shift;
6211  
6212 23 $xsub->{seen_ALIAS} = 1;
6213 23 $self->SUPER::parse($pxs, $xsub, $xbody);
6214 }
6215  
6216  
6217 # ======================================================================
6218  
6219 package ExtUtils::ParseXS::Node::ALIAS_line;
6220  
6221 # Handle one line from an ALIAS keyword block
6222  
6223 28 ▶BEGIN { $build_subclass->(-parent => 'keyline',
Subroutine BEGIN: called CC=1 SCAR=0.0
6224 )};
6225  
6226  
6227 # Parse one line from an ALIAS block
6228 #
6229 # Each line can have zero or more definitions, separated by white space.
6230 # Each definition is of one of the two forms:
6231 #
6232 # name = value
6233 # name => other_name
6234 #
6235 # where 'value' is a positive integer (or C macro) and the names are
6236 # simple or qualified perl function names. E.g.
6237 #
6238 # foo = 1 Bar::foo = 2 Bar::baz => Bar::foo
6239 #
6240 # The RHS of a '=>' is the name of an existing alias
6241 #
6242 # The results are added to a hash in the parent ALIAS node, as well as
6243 # to a couple of per-xsub hashes which accumulate the results across
6244 # possibly multiple ALIAS keywords.
6245 #
6246 # Updates:
6247 # $parent->{aliases}{$alias} = $value;
6248 # $xsub->{map_alias_name_to_value}{$alias} = $value;
6249 # $xsub->{map_alias_value_to_name_seen_hash}{$value}{$alias}++;
6250  
6251  
6252 53 ▶sub parse {
Subroutine parse: called CC=20 SCAR=30.0
6253 53 my __PACKAGE__ $self = shift;
6254 53 my ExtUtils::ParseXS $pxs = shift;
6255 53 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
6256 53 my ExtUtils::ParseXS::Node::xbody $xbody = shift;
6257 53 my ExtUtils::ParseXS::Node::ALIAS $parent = shift; # parent ALIAS node
6258  
6259 53 $self->SUPER::parse($pxs); # set file/line_no/line
6260 53 my $line = $self->{line}; # line of text to be processed
6261  
6262 53 ExtUtils::ParseXS::Utilities::trim_whitespace($line);
6263  
6264 53 my $orig = $line; # keep full line for error messages
6265  
6266 # we use this later for symbolic aliases
6267 53 my $fname = $pxs->{PACKAGE_class} . $xsub->{decl}{name};
6268  
6269 # chop out and process one alias entry from $line
6270  
6271 53 ▶ while ($line =~ s/^\s*([\w:]+)\s*=(>?)\s*([\w:]+)\s*//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ s/^\s*([\w:]+)\s*=(>?)\s*([\w:]+)\s*//) 53 53 106
6272 53 my ($alias, $is_symbolic, $value) = ($1, $2, $3);
6273 53 my $orig_alias = $alias;
6274  
6275 53 ▶ $pxs->blurt( "Error: in alias definition for '$alias' the value "
Branch: 1 branch
BranchTrueFalseTotal
if not $is_symbolic and $value =~ /:/ 1 52 53
Condition100%3/3
not $is_symbolic and $value =~ /:/
!ll&&!rl&&r
7451
MC/DC100%2/2
not $is_symbolic and $value =~ /:/
not $is_symbolic $value =~ /:/
Truth table100%3/3
not(A) and B
A= not $is_symbolicB= $value =~ /:/
ABresult
0X0
100
111
6276 . "may not contain ':' unless it is symbolic.")
6277 if !$is_symbolic and $value=~/:/;
6278  
6279 # check for optional package definition in the alias
6280 53 ▶ $alias = $pxs->{PACKAGE_class} . $alias if $alias !~ /::/;
Branch: 1 branch
BranchTrueFalseTotal
unless $alias =~ /::/ 49 4 53
6281  
6282 53 ▶ if ($is_symbolic) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_symbolic) 7 46 53
6283 7 my $orig_value = $value;
6284 7 ▶ $value = $pxs->{PACKAGE_class} . $value if $value !~ /::/;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /::/ 5 2 7
6285 7 ▶ if (defined $xsub->{map_alias_name_to_value}{$value}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $xsub->{'map_alias_name_to_value'}{$value}) { } 6 1 7
elsif ($value eq $fname) { } 0 1 1
6286 6 $value = $xsub->{map_alias_name_to_value}{$value};
6287 } elsif ($value eq $fname) {
6288 0 $value = 0;
6289 } else {
6290 1 $pxs->blurt( "Error: unknown alias '$value' in "
6291 . "symbolic definition for '$orig_alias'");
6292 }
6293 }
6294  
6295 # check for duplicate alias name & duplicate value
6296 53 my $prev_value = $xsub->{map_alias_name_to_value}{$alias};
6297 53 ▶ if (defined $prev_value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prev_value) 3 50 53
6298 3 ▶ if ($prev_value eq $value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev_value eq $value) { } 1 2 3
6299 1 $pxs->Warn("Warning: ignoring duplicate alias '$orig_alias'")
6300 } else {
6301 2 $pxs->Warn( "Warning: conflicting duplicate alias "
6302 . "'$orig_alias' changes definition "
6303 . "from '$prev_value' to '$value'");
6304 delete $xsub->{map_alias_value_to_name_seen_hash}
6305 2 ->{$prev_value}{$alias};
6306 }
6307 }
6308  
6309 # Check and see if this alias results in two aliases having the same
6310 # value, we only check non-symbolic definitions as the whole point of
6311 # symbolic definitions is to say we want to duplicate the value and
6312 # it is NOT a mistake.
6313 53 ▶ unless ($is_symbolic) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_symbolic) 46 7 53
6314 46 my @keys= sort keys %{$xsub->
6315 46 ▶ {map_alias_value_to_name_seen_hash}->{$value}||{}};
Condition100%2/2
$xsub->{'map_alias_value_to_name_seen_hash'}{$value} || {}
l!l
739
MC/DC100%1/1
$xsub->{'map_alias_value_to_name_seen_hash'}{$value} || {}
$xsub->{'map_alias_value_to_name_seen_hash'}{$value}
Truth table100%2/2
A
A= $xsub->{'map_alias_value_to_name_seen_hash'}{$value}
Aresult
11
00
6316 # deal with an alias of 0, which might not be in the aliases
6317 # dataset yet as 0 is the default for the base function ($fname)
6318 push @keys, $fname
6319 if $value eq "0" and
6320 46 ▶ !defined $xsub->{map_alias_name_to_value}{$fname};
Branch: 1 branch
BranchTrueFalseTotal
if $value eq 0 and not defined $xsub->{"map_alias_name_to_value"}{$fname} 3 43 46
Condition100%3/3
$value eq 0 and not defined $xsub->{"map_alias_name_to_value"}{$fname}
!ll&&!rl&&r
4213
MC/DC100%2/2
$value eq 0 and not defined $xsub->{"map_alias_name_to_value"}{$fname}
$value eq 0 not defined $xsub->{"map_alias_name_to_value"}{$fname}
Truth table100%3/3
A and not(B)
A= $value eq 0B= not defined $xsub->{"map_alias_name_to_value"}{$fname}
ABresult
0X0
100
111
6321 46 ▶ if (@keys and $pxs->{config_author_warnings}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@keys and $pxs->{"config_author_warnings"}) 8 38 46
Condition100%3/3
@keys and $pxs->{"config_author_warnings"}
!ll&&!rl&&r
3718
MC/DC100%2/2
@keys and $pxs->{"config_author_warnings"}
@keys $pxs->{"config_author_warnings"}
Truth table100%3/3
A and B
A= @keysB= $pxs->{"config_author_warnings"}
ABresult
0X0
100
111
6322 # We do not warn about value collisions unless author_warnings
6323 # are enabled. They aren't helpful to a module consumer, only
6324 # the module author.
6325 13 @keys= map { "'$_'" }
6326 8 map { my $copy= $_;
6327 13 $copy=~s/^$pxs->{PACKAGE_class}//;
6328 13 $copy
6329 } @keys;
6330 $pxs->WarnHint(
6331 "Warning: aliases '$orig_alias' and "
6332 . join(", ", @keys)
6333 . " have identical values of $value"
6334 . ( $value eq "0"
6335 ? " - the base function"
6336 : "" ),
6337 8 ▶ !$xsub->{alias_clash_hinted}++
Branch: 2 branches
BranchTrueFalseTotal
$value eq '0' ? : 4 4 8
!$xsub->{'alias_clash_hinted'}++ ? : 5 3 8
6338 ? "If this is deliberate use a "
6339 . "symbolic alias instead."
6340 : undef
6341 );
6342 }
6343 }
6344  
6345 53 $parent->{aliases}{$alias} = $value;
6346 53 $xsub->{map_alias_name_to_value}->{$alias} = $value;
6347 53 $xsub->{map_alias_value_to_name_seen_hash}{$value}{$alias}++;
6348 }
6349  
6350 53 ▶ $pxs->blurt("Error: cannot parse ALIAS definitions from '$orig'")
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\S/ 2 51 53
6351 if $line =~ /\S/;
6352  
6353 53 1;
6354 }
6355  
6356  
6357 # ======================================================================
6358  
6359 package ExtUtils::ParseXS::Node::INPUT;
6360  
6361 # Handle an explicit INPUT: block, or any implicit INPUT
6362 # block which can follow an xsub signature or CASE keyword.
6363  
6364 28 ▶BEGIN { $build_subclass->(-parent => 'keylines',
Subroutine BEGIN: called CC=1 SCAR=0.0
6365 'implicit', # Bool: this is an INPUT section at the start of the
6366 # XSUB/CASE, without an explicit 'INPUT' keyword
6367 )};
6368  
6369 # The inherited parse() method will call INPUT_line->parse() for each line
6370  
6371  
6372 485 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
6373 485 my __PACKAGE__ $self = shift;
6374 485 my ExtUtils::ParseXS $pxs = shift;
6375 485 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
6376 485 my ExtUtils::ParseXS::Node::xbody $xbody = shift;
6377  
6378 # Call the SUPER parse method, which will call INPUT_line->parse()
6379 # for each INPUT line. The '1' bool arg indicates to treat
6380 # NOT_IMPLEMENTED_YET as another block separator, in addition to
6381 # $BLOCK_regexp.
6382 485 $self->SUPER::parse($pxs, $xsub, $xbody, 1);
6383  
6384 480 1;
6385 }
6386  
6387  
6388 # ======================================================================
6389  
6390 package ExtUtils::ParseXS::Node::INPUT_line;
6391  
6392 # Handle one line from an INPUT keyword block
6393  
6394 28 ▶BEGIN { $build_subclass->(-parent => 'keyline',
Subroutine BEGIN: called CC=1 SCAR=0.0
6395 'ioparam', # The IO_Param object associated with this INPUT line.
6396  
6397 # The parsed components of this INPUT line:
6398 'type', # Str: char *
6399 'is_addr', # Bool: &
6400 'name', # Str: foo
6401 'init_op', # Str: =
6402 'init', # Str: SvIv($arg)
6403 )};
6404  
6405  
6406 # Parse one line in an INPUT block. This method does two main things:
6407 #
6408 # It parses the line and stores its components in the fields of the
6409 # INPUT_line object (which aren't further used for parsing or code
6410 # generation)
6411 #
6412 # It also uses those values to create/update the IO_Param object
6413 # associated with this variable. For example with
6414 #
6415 # void
6416 # foo(a = 0)
6417 # int a
6418 #
6419 # a IO_Param object will already have been created with the name 'a' and
6420 # default value '0' when the signature was parsed. Parsing the 'int a'
6421 # line will set the INPUT_line object's fields to (type => 'int',
6422 # name => 'a'), while the IO_Param object will have its type field set to
6423 # 'int'. The INPUT_line object also stores a ref to the IO_Param object.
6424 #
6425  
6426 146 ▶sub parse {
Subroutine parse: called CC=21 SCAR=30.4
6427 146 my __PACKAGE__ $self = shift;
6428 146 my ExtUtils::ParseXS $pxs = shift;
6429 146 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
6430 146 my ExtUtils::ParseXS::Node::xbody $xbody = shift;
6431 146 my ExtUtils::ParseXS::Node::INPUT $parent = shift; # parent INPUT node
6432  
6433 146 $self->SUPER::parse($pxs); # set file/line_no/line
6434 146 my $line = $self->{line}; # line of text to be processed
6435  
6436 146 ExtUtils::ParseXS::Utilities::trim_whitespace($line);
6437  
6438 # remove any trailing semicolon, except for initialisations
6439 146 ▶ $line =~ s/\s*;$//g unless $line =~ /[=;+].*\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /[=;+].*\S/ 122 24 146
6440  
6441 # Extract optional initialisation code (which overrides the
6442 # normal typemap), such as 'int foo = ($type)SvIV($arg)'
6443 146 my $var_init = '';
6444 146 my $init_op;
6445 146 ▶ if ($line =~ s/\s* ([=;+]) \s* (.*) $//xs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ s/\s* ([=;+]) \s* (.*) $//sx) 26 120 146
6446 26 ($init_op, $var_init) = ($1, $2);
6447 26 ▶ $pxs->death("Error: missing '$init_op' initialiser value")
Branch: 1 branch
BranchTrueFalseTotal
unless $var_init =~ /\S/ and not $var_init =~ /^\s*;\s*$/ 5 21 26
Condition100%3/3
$var_init =~ /\S/ and not $var_init =~ /^\s*;\s*$/
!ll&&!rl&&r
2321
MC/DC100%2/2
$var_init =~ /\S/ and not $var_init =~ /^\s*;\s*$/
$var_init =~ /\S/ not $var_init =~ /^\s*;\s*$/
Truth table100%3/3
A and not(B)
A= $var_init =~ /\S/B= not $var_init =~ /^\s*;\s*$/
ABresult
0X0
100
111
6448 unless $var_init =~ /\S/ && $var_init !~ /^\s*;\s*$/;
6449 }
6450  
6451 141 $line =~ s/\s+/ /g;
6452  
6453 # Split 'char * &foo' into ('char *', '&', 'foo')
6454 # skip to next INPUT line if not valid.
6455 #
6456 # Note that this pattern has a very liberal sense of what is "valid",
6457 # since we don't fully parse C types. For example:
6458 #
6459 # int foo(a)
6460 # int a XYZ
6461 #
6462 # would be interpreted as an "alien" (i.e. not in the signature)
6463 # variable called "XYZ", with a type of "int a". And because it's
6464 # alien the initialiser is skipped, so 'int a' is never looked up in
6465 # a typemap, so we don't detect anything wrong. Later on, the C
6466 # compiler is likely to trip over on the emitted declaration
6467 # however:
6468 # int a XYZ;
6469  
6470 my ($var_type, $var_addr, $var_name) =
6471 $line =~ /^
6472 ( .*? [^&\s] ) # type
6473 \s*
6474 (\&?) # addr
6475 \s* \b
6476 (\w+ | length\(\w+\)) # name or length(name)
6477 $
6478 /xs
6479 141 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
() = $line =~ /^ ( .*? [^&\s] ) # type \s* (\&?) # addr \s* \b (\w+ | length\(\w+\)) # name or length(name) $ /sx or do { $pxs->blurt(qq[Error: invalid parameter declaration '$self->{"line"}']); return } 140 1 141
6480 1 $pxs->blurt("Error: invalid parameter declaration '$self->{line}'");
6481 1 return;
6482 };
6483  
6484 # length(s) is only allowed in the XSUB's signature.
6485 140 ▶ if ($var_name =~ /^length\((\w+)\)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var_name =~ /^length\((\w+)\)$/) 2 138 140
6486 2 $pxs->blurt("Error: length() not permitted in INPUT section");
6487 2 return;
6488 }
6489  
6490 138 my ($var_num, $is_alien);
6491  
6492 138 my $ioparams = $xbody->{ioparams};
6493  
6494 my ExtUtils::ParseXS::Node::IO_Param $ioparam =
6495 138 $ioparams->{names}{$var_name};
6496  
6497 138 ▶ if (defined $ioparam) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ioparam) { } 128 10 138
6498 # The var appeared in the signature too.
6499  
6500 # Check for duplicate definitions of a particular parameter name.
6501 # This can be either because it has appeared in multiple INPUT
6502 # lines, or because the type was already defined in the signature,
6503 # and thus shouldn't be defined again. The exception to this are
6504 # synthetic params like THIS, which are assigned a provisional type
6505 # which can be overridden.
6506 128 ▶ if ( $ioparam->{in_input}
Branch: 1 branch
BranchTrueFalseTotal
if ($ioparam->{"in_input"} or not $ioparam->{"is_synthetic"} and defined $ioparam->{"type"}) 7 121 128
Condition100%6/6
$ioparam->{"in_input"} or not $ioparam->{"is_synthetic"} and defined $ioparam->{"type"}
l!l&&r!l&&!r
43121
not $ioparam->{"is_synthetic"} and defined $ioparam->{"type"}
!ll&&!rl&&r
191023
MC/DC100%3/3
$ioparam->{"in_input"} or not $ioparam->{"is_synthetic"} and defined $ioparam->{"type"}
$ioparam->{"in_input"} not $ioparam->{"is_synthetic"} defined $ioparam->{"type"}
Truth table100%4/4
A or not(B) and C
A= $ioparam->{"in_input"}B= not $ioparam->{"is_synthetic"}C= defined $ioparam->{"type"}
ABCresult
1XX1
0111
00X0
0100
6507 or (!$ioparam->{is_synthetic} and defined $ioparam->{type})
6508 ) {
6509 7 $pxs->blurt(
6510 "Error: duplicate definition of parameter '$var_name' ignored");
6511 7 return;
6512 }
6513  
6514 121 ▶ if ($var_name eq 'RETVAL' and $ioparam->{is_synthetic}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var_name eq "RETVAL" and $ioparam->{"is_synthetic"}) 11 110 121
Condition100%3/3
$var_name eq "RETVAL" and $ioparam->{"is_synthetic"}
!ll&&!rl&&r
107311
MC/DC100%2/2
$var_name eq "RETVAL" and $ioparam->{"is_synthetic"}
$var_name eq "RETVAL" $ioparam->{"is_synthetic"}
Truth table100%3/3
A and B
A= $var_name eq "RETVAL"B= $ioparam->{"is_synthetic"}
ABresult
0X0
100
111
6515 # Convert a synthetic RETVAL into a real parameter
6516 11 delete $ioparam->{is_synthetic};
6517 11 delete $ioparam->{no_init};
6518 11 ▶ if (! defined $ioparam->{arg_num}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ioparam->{"arg_num"}) 6 5 11
6519 # if has arg_num, RETVAL has appeared in signature but with no
6520 # type, and has already been moved to the correct position;
6521 # otherwise, it's an alien var that didn't appear in the
6522 # signature; move to the correct position.
6523 6 @{$ioparams->{kids}} =
6524 6 grep $_ != $ioparam, @{$ioparams->{kids}};
6525 6 push @{$ioparams->{kids}}, $ioparam;
6526 6 $is_alien = 1;
6527 6 $ioparam->{is_alien} = 1;
6528 }
6529 }
6530  
6531 121 $ioparam->{in_input} = 1;
6532 121 $var_num = $ioparam->{arg_num};
6533 }
6534 else {
6535 # The var is in an INPUT line, but not in signature. Treat it as a
6536 # general var declaration (which really should have been in a
6537 # PREINIT section). Legal but nasty: flag is as 'alien'
6538 10 $is_alien = 1;
6539 10 $ioparam = ExtUtils::ParseXS::Node::IO_Param->new({
6540 var => $var_name,
6541 is_alien => 1,
6542 });
6543  
6544 10 push @{$ioparams->{kids}}, $ioparam;
6545 10 $ioparams->{names}{$var_name} = $ioparam;
6546 }
6547  
6548 # Parse the initialisation part of the INPUT line (if any)
6549  
6550 131 my ($init, $defer);
6551 131 my $no_init = $ioparam->{no_init}; # may have had OUT in signature
6552  
6553 131 ▶ if (!$no_init && defined $init_op) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $no_init and defined $init_op) { } 20 111 131
Condition100%3/3
not $no_init and defined $init_op
!ll&&!rl&&r
210920
MC/DC100%2/2
not $no_init and defined $init_op
not $no_init defined $init_op
Truth table100%3/3
not(A) and B
A= not $no_initB= defined $init_op
ABresult
0X0
100
111
6554 # Use the init code based on overridden $var_init, which was
6555 # preceded by /[=;+]/ which has been extracted into $init_op
6556  
6557 20 ▶ if ( $init_op =~ /^[=;]$/
Branch: 2 branches
BranchTrueFalseTotal
if ($init_op =~ /^[=;]$/ and $var_init =~ /^NO_INIT\s*;?\s*$/) { } 1 19 20
elsif ($init_op eq '=') { } 13 6 19
Condition100%3/3
$init_op =~ /^[=;]$/ and $var_init =~ /^NO_INIT\s*;?\s*$/
!ll&&!rl&&r
3161
MC/DC100%2/2
$init_op =~ /^[=;]$/ and $var_init =~ /^NO_INIT\s*;?\s*$/
$init_op =~ /^[=;]$/ $var_init =~ /^NO_INIT\s*;?\s*$/
Truth table100%3/3
A and B
A= $init_op =~ /^[=;]$/B= $var_init =~ /^NO_INIT\s*;?\s*$/
ABresult
0X0
100
111
6558 and $var_init =~ /^NO_INIT\s*;?\s*$/
6559 ) {
6560 # NO_INIT: skip initialisation
6561 1 $no_init = 1;
6562 }
6563 elsif ($init_op eq '=') {
6564 # Overridden typemap, such as '= ($type)SvUV($arg)'
6565 13 $var_init =~ s/;\s*$//;
6566 13 $init = $var_init,
6567 }
6568 else {
6569 # "; extra code" or "+ extra code" :
6570 # append the extra code (after passing through eval) after all the
6571 # INPUT and PREINIT blocks have been processed, indirectly using
6572 # the $input_part->{deferred_code_lines} mechanism.
6573 # In addition, for '+', also generate the normal initialisation
6574 # code from the standard typemap - assuming that it's a real
6575 # parameter that appears in the signature as well as the INPUT
6576 # line.
6577 6 ▶ $no_init = !($init_op eq '+' && !$is_alien);
Condition66%2/3
$init_op eq '+' && !$is_alien
!ll&&!rl&&r
303
MC/DC50%1/2
$init_op eq '+' && !$is_alien
$init_op eq '+' !$is_alien
Truth table66%2/3
A && not(B)
A= $init_op eq '+'B= !$is_alien
ABresult
0X0
100
111
6578 # But in either case, add the deferred code
6579 6 $defer = $var_init;
6580 }
6581 }
6582 else {
6583 # no initialiser: emit var and init code based on typemap entry,
6584 # unless: it's alien (so no stack arg to bind to it)
6585 111 ▶ $no_init = 1 if $is_alien;
Branch: 1 branch
BranchTrueFalseTotal
if $is_alien 9 102 111
6586 }
6587  
6588 # Save the basic information parsed from this line
6589  
6590 $self->{type} = $var_type,
6591 $self->{is_addr} = !!$var_addr,
6592 $self->{name} = $var_name,
6593 $self->{init_op} = $init_op,
6594 $self->{init} = $var_init,
6595 131 $self->{ioparam} = $ioparam;
6596  
6597 # and also update the ioparam object using that information
6598  
6599 131 %$ioparam = (
6600 %$ioparam,
6601 type => $var_type,
6602 arg_num => $var_num,
6603 var => $var_name,
6604 defer => $defer,
6605 init => $init,
6606 init_op => $init_op,
6607 no_init => $no_init,
6608 is_addr => !!$var_addr,
6609 );
6610  
6611 131 1;
6612 }
6613  
6614  
6615 131 ▶sub as_code {
Subroutine as_code: called CC=2 SCAR=6.9
6616 131 my __PACKAGE__ $self = shift;
6617 131 my ExtUtils::ParseXS $pxs = shift;
6618 131 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
6619 131 my ExtUtils::ParseXS::Node::xbody $xbody = shift;
6620  
6621 # Emit "type var" declaration and possibly various forms of
6622 # initialiser code.
6623  
6624 131 my $ioparam = $self->{ioparam};
6625  
6626 # Synthetic params like THIS will be emitted later - they
6627 # are treated like ANSI params, except the type can overridden
6628 # within an INPUT statement
6629 131 ▶ return if $ioparam->{is_synthetic};
Branch: 1 branch
BranchTrueFalseTotal
if $ioparam->{"is_synthetic"} 8 123 131
6630  
6631 # The ioparam object contains data from both the INPUT line and
6632 # the XSUB signature.
6633 123 $ioparam->as_input_code($pxs, $xsub, $xbody);
6634 }
6635  
6636  
6637 # ======================================================================
6638  
6639 package ExtUtils::ParseXS::Node::OUTPUT;
6640  
6641 # Handle an OUTPUT: block
6642  
6643 28 ▶BEGIN { $build_subclass->(-parent => 'keylines',
Subroutine BEGIN: called CC=1 SCAR=0.0
6644 )};
6645  
6646 # The inherited parse() method will call OUTPUT_line->parse() for each line
6647  
6648  
6649 # ======================================================================
6650  
6651 package ExtUtils::ParseXS::Node::OUTPUT_line;
6652  
6653 # Handle one line from an OUTPUT keyword block
6654  
6655 28 ▶BEGIN { $build_subclass->(-parent => 'keyline',
Subroutine BEGIN: called CC=1 SCAR=0.0
6656 'ioparam', # the IO_Param object associated with this OUTPUT line.
6657 'is_setmagic', # Bool: the line is a SETMAGIC: line
6658 'do_setmagic', # Bool: the current SETMAGIC state
6659 'name', # Str: name of the parameter to output
6660 'code', # Str: optional setting code
6661 )};
6662  
6663  
6664 # Parse one line from an OUTPUT block
6665  
6666 83 ▶sub parse {
Subroutine parse: called CC=14 SCAR=26.4
6667 83 my __PACKAGE__ $self = shift;
6668 83 my ExtUtils::ParseXS $pxs = shift;
6669 83 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
6670 83 my ExtUtils::ParseXS::Node::xbody $xbody = shift;
6671 83 my ExtUtils::ParseXS::Node::OUTPUT $parent = shift; # parent OUTPUT node
6672  
6673 83 $self->SUPER::parse($pxs); # set file/line_no/line
6674 83 my $line = $self->{line}; # line of text to be processed
6675  
6676 83 ▶ return unless $line =~ /\S/; # skip blank lines
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\S/ 0 83 83
6677  
6678 # set some sane default values in case we do one of the early returns
6679 # below
6680  
6681 83 $self->{do_setmagic} = $xbody->{OUTPUT_SETMAGIC_state};
6682 83 $self->{is_setmagic} = 0;
6683  
6684 83 ▶ if ($line =~ /^\s*SETMAGIC\s*:\s*(.*?)\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*SETMAGIC\s*:\s*(.*?)\s*$/) 7 76 83
6685 7 my $arg = $1;
6686 7 ▶ unless ($arg =~ /^(ENABLE|DISABLE)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg =~ /^(ENABLE|DISABLE)$/) 1 6 7
6687 1 $pxs->blurt("Error: SETMAGIC: invalid value '$arg' (should be ENABLE/DISABLE)");
6688 1 return;
6689 }
6690 6 ▶ $xbody->{OUTPUT_SETMAGIC_state} = ($arg eq "ENABLE" ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$arg eq 'ENABLE' ? : 3 3 6
6691 6 $self->{do_setmagic} = $xbody->{OUTPUT_SETMAGIC_state};
6692 6 $self->{is_setmagic} = 1;
6693 6 return;
6694 }
6695  
6696 # Expect lines of the two forms
6697 # SomeVar
6698 # SomeVar sv_setsv(....);
6699 #
6700 76 my ($outarg, $outcode) = $line =~ /^\s*(\S+)\s*(.*?)\s*$/s;
6701  
6702 76 $self->{name} = $outarg;
6703  
6704 my ExtUtils::ParseXS::Node::IO_Param $ioparam =
6705 76 $xbody->{ioparams}{names}{$outarg};
6706 76 $self->{ioparam} = $ioparam;
6707  
6708 76 ▶ if ($ioparam && $ioparam->{in_output}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ioparam and $ioparam->{"in_output"}) 2 74 76
Condition100%3/3
$ioparam and $ioparam->{"in_output"}
!ll&&!rl&&r
3712
MC/DC100%2/2
$ioparam and $ioparam->{"in_output"}
$ioparam $ioparam->{"in_output"}
Truth table100%3/3
A and B
A= $ioparamB= $ioparam->{"in_output"}
ABresult
0X0
100
111
6709 2 $pxs->blurt("Error: duplicate OUTPUT parameter '$outarg' ignored");
6710 2 return;
6711 }
6712  
6713 74 ▶ if ( $outarg eq "RETVAL"
Branch: 1 branch
BranchTrueFalseTotal
if ($outarg eq "RETVAL" and $xsub->{"decl"}{"return_type"}{"no_output"}) 3 71 74
Condition100%3/3
$outarg eq "RETVAL" and $xsub->{"decl"}{"return_type"}{"no_output"}
!ll&&!rl&&r
25463
MC/DC100%2/2
$outarg eq "RETVAL" and $xsub->{"decl"}{"return_type"}{"no_output"}
$outarg eq "RETVAL" $xsub->{"decl"}{"return_type"}{"no_output"}
Truth table100%3/3
A and B
A= $outarg eq "RETVAL"B= $xsub->{"decl"}{"return_type"}{"no_output"}
ABresult
0X0
100
111
6714 and $xsub->{decl}{return_type}{no_output})
6715 {
6716 3 $pxs->blurt( "Error: can't use RETVAL in OUTPUT "
6717 . "when NO_OUTPUT declared");
6718 3 return;
6719 }
6720  
6721 71 ▶ if ( !$ioparam # no such param or, for RETVAL, RETVAL was void;
Branch: 1 branch
BranchTrueFalseTotal
if (not $ioparam or $outarg ne "RETVAL" and not $ioparam->{"arg_num"}) 3 68 71
Condition66%4/6
not $ioparam or $outarg ne "RETVAL" and not $ioparam->{"arg_num"}
l!l&&r!l&&!r
3068
$outarg ne "RETVAL" and not $ioparam->{"arg_num"}
!ll&&!rl&&r
45230
MC/DC33%1/3
not $ioparam or $outarg ne "RETVAL" and not $ioparam->{"arg_num"}
not $ioparam $outarg ne "RETVAL" not $ioparam->{"arg_num"}
Truth table75%3/4
not(A) or B and not(C)
A= not $ioparamB= $outarg ne "RETVAL"C= not $ioparam->{"arg_num"}
ABCresult
1XX1
0111
00X0
0100
6722 # not bound to an arg which can be updated
6723 or $outarg ne "RETVAL" && !$ioparam->{arg_num})
6724 {
6725 3 $pxs->blurt("Error: OUTPUT $outarg not a parameter");
6726 3 return;
6727 }
6728  
6729 68 $ioparam->{in_output} = 1;
6730 $ioparam->{do_setmagic} = $outarg eq 'RETVAL'
6731 ? 0 # RETVAL never needs magic setting
6732 68 ▶ : $xbody->{OUTPUT_SETMAGIC_state};
Branch: 1 branch
BranchTrueFalseTotal
$outarg eq 'RETVAL' ? : 45 23 68
6733 68 ▶ $self->{code} = $ioparam->{output_code} = $outcode if length $outcode;
Branch: 1 branch
BranchTrueFalseTotal
if length $outcode 11 57 68
6734  
6735 68 1;
6736 }
6737  
6738  
6739 68 ▶sub as_code {
Subroutine as_code: called CC=3 SCAR=11.0
6740 68 my __PACKAGE__ $self = shift;
6741 68 my ExtUtils::ParseXS $pxs = shift;
6742 68 my ExtUtils::ParseXS::Node::xsub $xsub = shift;
6743 68 my ExtUtils::ParseXS::Node::xbody $xbody = shift;
6744  
6745 # An OUTPUT: line serves two logically distinct purposes. First, any
6746 # parameters listed are updated; i.e. the perl equivalent of
6747 #
6748 # my $foo = $_[0];
6749 # # maybe $foo's value gets changed here
6750 # $_[0] = $foo; # update caller's arg with current value
6751 #
6752 # The code for updating such OUTPUT vars is emitted here, in the
6753 # same order they appear in OUTPUT lines, and preserving the order
6754 # of any intermixed POSTCALL etc blocks.
6755 #
6756 # Second, it can be used to indicate that an SV should be created,
6757 # set to the current value of RETVAL, and pushed on the stack; i.e
6758 # the perl equivalent of
6759 #
6760 # my $RETVAL;
6761 # # maybe $RETVAL's value gets set here
6762 # return $RETVAL;
6763 #
6764 # The code to return RETVAL is emitted later, after all other
6765 # processing for XSUB is complete apart from any final CLEANUP block.
6766 # It is done at the same time as any emitting for params declared as
6767 # OUT or OUTLIST in the signature.
6768 #
6769 # There isn't any particularly strong reason to do things in this
6770 # exact order; but the ordering was the result of how xsubpp was
6771 # originally written and subsequently modified, and changing things
6772 # now might break existing XS code which has come to rely on the
6773 # ordering.
6774  
6775 68 ▶ return if $self->{name} eq 'RETVAL';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"name"} eq "RETVAL" 45 23 68
6776  
6777 23 my $ioparam = $self->{ioparam};
6778 23 ▶ return unless $ioparam; # might be an ENABLE line with no param to emit
Branch: 1 branch
BranchTrueFalseTotal
unless $ioparam 0 23 23
6779  
6780 23 $ioparam->as_output_code($pxs);
6781 }
6782  
6783  
6784 # ======================================================================
6785  
6786  
6787 28 1;
6788  
6789 # vim: ts=4 sts=4 sw=4: et:
« blib/lib/ExtUtils/ParseXS.pm ↑ Summary blib/lib/ExtUtils/Typemaps.pm »