blib/lib/Inline/CPP.pm

filter: Statementstmt 83.9% 329 / 392 Branchbran 72.7% 128 / 176 Conditioncond 77.1% 91 / 118 MC/DCmcdc 60.2% 44 / 73 Subroutinesub 94.7% 36 / 38 Podpod 7.6% 1 / 13 totaltotal 77.6% 629 / 810 CC 147 SCAR 29.9
CC
147
Cov
80%
info
84.2
wrap
31.0
_generate_member_xs_wrappers
18.2
CRAP 19.9
↑ Summary blib/lib/Inline/CPP/Parser/RecDescent.pm »
1 package Inline::CPP;
2  
3 43 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 43 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 43 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 43 ▶use Fcntl qw( :DEFAULT :flock );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 43 require Inline::C;
9 43 require Inline::CPP::Parser::RecDescent;
10 43 require Inline::CPP::Config;
11  
12 # Note: Parse::RecDescent 'require'd within get_parser().
13  
14 43 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # use base doesn't work because Inline::C cannot be "use"d directly.
17 43 our @ISA = qw( Inline::C ); ## no critic (ISA)
18  
19 # Development releases will have a _0xx version suffix.
20 # We eval the version number to accommodate dev. version numbering, as
21 # described in perldoc perlmodstyle.
22 43 our $VERSION = '0.79';
23 # $VERSION = eval $VERSION; ## no critic (eval)
24  
25 43 my $TYPEMAP_KIND;
26 {
27 43 ▶ no warnings 'once'; ## no critic (warnings)
Subroutine BEGIN: called CC=1 SCAR=0.0
28 43 $TYPEMAP_KIND = $Inline::CPP::Parser::RecDescent::TYPEMAP_KIND;
29 }
30  
31 #============================================================================
32 # Register Inline::CPP as an Inline language module
33 #============================================================================
34 sub register {
35 43 ▶ use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 return {
37 language => 'CPP',
38 aliases => ['cpp', 'C++', 'c++', 'Cplusplus', 'cplusplus', 'CXX', 'cxx'],
39 type => 'compiled',
40 suffix => $Config{dlext},
41 1 ▶ };
Subroutine register: called CC=1 SCAR=0.0
Pod undocumented
42 } ### Tested.
43  
44 #============================================================================
45 # Validate the C++ config options: Now mostly done in Inline::C
46 #============================================================================
47 sub validate {
48 75 ▶ my ($o, @config_options) = @_;
Subroutine validate: called CC=6 SCAR=17.9
Pod undocumented
49 75 my ($flavor_defs, $iostream);
50  
51 { # "used only once" warning. We know it's ok.
52 43 ▶ no warnings 'once'; ## no critic (warnings)
Subroutine BEGIN: called CC=1 SCAR=0.0
53 ## no critic (package variable)
54  
55 # Set default compiler and libraries.
56 75 ▶ $o->{ILSM}{MAKEFILE}{CC} ||= $Inline::CPP::Config::compiler;
Condition66%2/3
$o->{"ILSM"}{"MAKEFILE"}{"CC"} ||= $Inline::CPP::Config::compiler
l!l&&r!l&&!r
32430
MC/DC0%0/2
$o->{"ILSM"}{"MAKEFILE"}{"CC"} ||= $Inline::CPP::Config::compiler
$o->{"ILSM"}{"MAKEFILE"}{"CC"} $Inline::CPP::Config::compiler
Truth table66%2/3
A ||= B
A= $o->{"ILSM"}{"MAKEFILE"}{"CC"}B= $Inline::CPP::Config::compiler
ABresult
1X1
011
000
57 75 ▶ $o->{ILSM}{MAKEFILE}{LIBS} ||= _make_arrayref($Inline::CPP::Config::libs);
Condition66%2/3
$o->{"ILSM"}{"MAKEFILE"}{"LIBS"} ||= _make_arrayref($Inline::CPP::Config::libs)
l!l&&r!l&&!r
32430
MC/DC0%0/2
$o->{"ILSM"}{"MAKEFILE"}{"LIBS"} ||= _make_arrayref($Inline::CPP::Config::libs)
$o->{"ILSM"}{"MAKEFILE"}{"LIBS"} _make_arrayref($Inline::CPP::Config::libs)
Truth table66%2/3
A ||= B
A= $o->{"ILSM"}{"MAKEFILE"}{"LIBS"}B= _make_arrayref($Inline::CPP::Config::libs)
ABresult
1X1
011
000
58  
59 75 $flavor_defs = $Inline::CPP::Config::cpp_flavor_defs; # "Standard"?
60 75 $iostream = $Inline::CPP::Config::iostream_fn; # iostream filename.
61 }
62  
63 # I haven't traced it out yet, but $o->{STRUCT} gets set before getting
64 # properly set from Inline::C's validate().
65 75 ▶ $o->{STRUCT} ||= {'.macros' => q{}, '.any' => 0, '.xs' => q{}, '.all' => 0,};
Condition100%2/2
$o->{"STRUCT"} ||= {".macros", "", ".any", 0, ".xs", "", ".all", 0}
l!l
3243
MC/DC100%1/1
$o->{"STRUCT"} ||= {".macros", "", ".any", 0, ".xs", "", ".all", 0}
$o->{"STRUCT"}
Truth table100%2/2
A
A= $o->{"STRUCT"}
Aresult
11
00
66  
67 75 _build_auto_include($o, $flavor_defs, $iostream);
68  
69 $o->{ILSM}{PRESERVE_ELLIPSIS} = 0
70 75 ▶ unless defined $o->{ILSM}{PRESERVE_ELLIPSIS};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $o->{"ILSM"}{"PRESERVE_ELLIPSIS"} 43 32 75
71  
72 # Filter out the parameters we treat differently than Inline::C,
73 # forwarding unknown requests back up to Inline::C.
74 75 my @propagate = _handle_config_options($o, @config_options);
75 71 ▶ $o->SUPER::validate(@propagate) if @propagate;
Branch: 1 branch
BranchTrueFalseTotal
if @propagate 36 35 71
76  
77 70 return;
78 }
79  
80  
81 sub _build_auto_include {
82 75 ▶ my ($o, $flavor_defs, $iostream) = @_;
Subroutine _build_auto_include: called CC=2 SCAR=7.0
83 75 my $auto_include = <<'END';
84 #define __INLINE_CPP 1
85 #ifndef bool
86 #include <%iostream%>
87 #endif
88 extern "C" {
89 #include "EXTERN.h"
90 #include "perl.h"
91 #include "XSUB.h"
92 #include "INLINE.h"
93 }
94 #ifdef bool
95 #undef bool
96 #include <%iostream%>
97 #endif
98  
99 END
100  
101 75 ▶ $o->{ILSM}{AUTO_INCLUDE} ||= $auto_include;
Condition66%2/3
$o->{"ILSM"}{"AUTO_INCLUDE"} ||= $auto_include
l!l&&r!l&&!r
32430
MC/DC0%0/2
$o->{"ILSM"}{"AUTO_INCLUDE"} ||= $auto_include
$o->{"ILSM"}{"AUTO_INCLUDE"} $auto_include
Truth table66%2/3
A ||= B
A= $o->{"ILSM"}{"AUTO_INCLUDE"}B= $auto_include
ABresult
1X1
011
000
102 75 $o->{ILSM}{AUTO_INCLUDE} = $flavor_defs . $o->{ILSM}{AUTO_INCLUDE};
103  
104 # Replace %iostream% with the correct iostream library
105 75 $o->{ILSM}{AUTO_INCLUDE} =~ s{%iostream%}{$iostream}xg;
106 75 return;
107 }
108  
109  
110 sub _handle_config_options {
111 75 ▶ my ($o, @config_options) = @_;
Subroutine _handle_config_options: called CC=10 SCAR=26.7
112 75 my @propagate;
113  
114 75 ▶ while (@config_options) {
Branch: 1 branch
BranchTrueFalseTotal
if (@config_options) 61 71 132
115 61 my ($key, $value) = (shift @config_options, shift @config_options);
116 61 $key = uc $key;
117 61 ▶ if ($key eq 'NAMESPACE') {
Branch: 5 branches
BranchTrueFalseTotal
if ($key eq 'NAMESPACE') { } 12 49 61
elsif ($key eq 'CLASSES') { } 12 37 49
elsif ($key eq 'LIBS') { } 1 36 37
elsif ($key eq 'ALTLIBS') { } 0 36 36
elsif ($key eq 'PRESERVE_ELLIPSIS' or $key eq 'STD_IOSTREAM') { } 0 36 36
118 12 _handle_namespace_cfg_option($o, $value);
119 }
120 elsif ($key eq 'CLASSES') {
121 12 _handle_classes_cfg_option($o, $value);
122 }
123 elsif ($key eq 'LIBS') {
124 1 _handle_libs_cfg_option($o, $value);
125 }
126 elsif ($key eq 'ALTLIBS') {
127 0 _handle_altlibs_cfg_option($o, $value);
128 }
129 ▶ elsif ($key eq 'PRESERVE_ELLIPSIS' or $key eq 'STD_IOSTREAM') {
Condition33%1/3
$key eq "PRESERVE_ELLIPSIS" or $key eq "STD_IOSTREAM"
l!l&&r!l&&!r
0036
MC/DC0%0/2
$key eq "PRESERVE_ELLIPSIS" or $key eq "STD_IOSTREAM"
$key eq "PRESERVE_ELLIPSIS" $key eq "STD_IOSTREAM"
Truth table33%1/3
A or B
A= $key eq "PRESERVE_ELLIPSIS"B= $key eq "STD_IOSTREAM"
ABresult
1X1
011
000
130 0 ▶ croak "Argument to $key must be 0 or 1"
Branch: 1 branch
BranchTrueFalseTotal
unless $value == 0 or $value == 1 0 0 0
Condition0%0/3
$value == 0 or $value == 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$value == 0 or $value == 1
$value == 0 $value == 1
Truth table0%0/3
A or B
A= $value == 0B= $value == 1
ABresult
1X1
011
000
131 unless $value == 0 or $value == 1;
132 0 $o->{ILSM}{$key} = $value;
133 }
134 else {
135 36 push @propagate, $key, $value;
136 }
137 }
138 71 return @propagate;
139 }
140  
141 sub _handle_namespace_cfg_option {
142 12 ▶ my ($o, $value) = @_;
Subroutine _handle_namespace_cfg_option: called CC=5 SCAR=16.1
143 12 $value =~ s/^::|::$//g;
144
145 # Perl 5.12 indroduced \p{XID_Start} and \p{XID_Continue}. Prior to that
146 # we should downgrade gracefully.
147 12 ▶ my $ident = $] ge '5.0120'
Branch: 1 branch
BranchTrueFalseTotal
$] ge '5.0120' ? : 12 0 12
148 ? qr{[\p{XID_Start}_][\p{XID_Continue}_]*}
149 : qr{[\p{Alpha}_][\p{Alpha}\p{Digit}_]*};
150  
151 12 ▶ croak "$value is an invalid package name."
Branch: 1 branch
BranchTrueFalseTotal
unless length $value == 0 or $value =~ / \A $ident (?:::$ident)* \z /x 1 11 12
Condition100%3/3
length $value == 0 or $value =~ / \A $ident (?:::$ident)* \z /x
l!l&&r!l&&!r
651
MC/DC100%2/2
length $value == 0 or $value =~ / \A $ident (?:::$ident)* \z /x
length $value == 0 $value =~ / \A $ident (?:::$ident)* \z /x
Truth table100%3/3
A or B
A= length $value == 0B= $value =~ / \A $ident (?:::$ident)* \z /x
ABresult
1X1
011
000
152 unless length $value == 0
153 || $value =~ m/
154 \A
155 $ident
156 (?:::$ident)*
157 \z
158 /x;
159 11 ▶ $value ||= 'main';
Condition100%2/2
$value ||= "main"
l!l
56
MC/DC100%1/1
$value ||= "main"
$value
Truth table100%2/2
A
A= $value
Aresult
11
00
160 11 $o->{API}{pkg} = $value;
161 11 return;
162 }
163  
164  
165 sub _handle_classes_cfg_option {
166 12 ▶ my ($o, $value) = @_;
Subroutine _handle_classes_cfg_option: called CC=10 SCAR=23.2
167 12 my $ref_value = ref($value);
168 12 ▶ croak 'CLASSES config option is not a valid code reference or hash '
Branch: 1 branch
BranchTrueFalseTotal
unless $ref_value eq "CODE" or $ref_value eq "HASH" 1 11 12
Condition100%3/3
$ref_value eq "CODE" or $ref_value eq "HASH"
l!l&&r!l&&!r
381
MC/DC100%2/2
$ref_value eq "CODE" or $ref_value eq "HASH"
$ref_value eq "CODE" $ref_value eq "HASH"
Truth table100%3/3
A or B
A= $ref_value eq "CODE"B= $ref_value eq "HASH"
ABresult
1X1
011
000
169 . 'reference of class mappings.'
170 unless (($ref_value eq 'CODE') or ($ref_value eq 'HASH'));
171  
172 11 ▶ if ($ref_value eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref_value eq "HASH") 8 3 11
173 8 foreach my $cpp_class (keys %{$value}) {
174 croak "$cpp_class is not a supported C++ class."
175 12 ▶ unless defined $value->{$cpp_class}
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value->{$cpp_class} and length $cpp_class != 0 and $cpp_class =~ /[a-zA-Z]\w*/x 1 11 12
Condition50%3/6
defined $value->{$cpp_class} and length $cpp_class != 0 and $cpp_class =~ /[a-zA-Z]\w*/x
!ll&&!rl&&r
0111
defined $value->{$cpp_class} and length $cpp_class != 0
!ll&&!rl&&r
0012
MC/DC33%1/3
defined $value->{$cpp_class} and length $cpp_class != 0 and $cpp_class =~ /[a-zA-Z]\w*/x
defined $value->{$cpp_class} length $cpp_class != 0 $cpp_class =~ /[a-zA-Z]\w*/x
Truth table50%2/4
A and B and C
A= defined $value->{$cpp_class}B= length $cpp_class != 0C= $cpp_class =~ /[a-zA-Z]\w*/x
ABCresult
0XX0
10X0
1100
1111
176 && length $cpp_class != 0
177 && $cpp_class =~ m/[a-zA-Z]\w*/x;
178 11 my $perl_class = $value->{$cpp_class};
179 11 ▶ croak "$perl_class is not a supported Perl class."
Branch: 1 branch
BranchTrueFalseTotal
unless length $perl_class != 0 and $perl_class =~ /[a-zA-Z]\w*/x 1 10 11
Condition66%2/3
length $perl_class != 0 and $perl_class =~ /[a-zA-Z]\w*/x
!ll&&!rl&&r
0110
MC/DC50%1/2
length $perl_class != 0 and $perl_class =~ /[a-zA-Z]\w*/x
length $perl_class != 0 $perl_class =~ /[a-zA-Z]\w*/x
Truth table66%2/3
A and B
A= length $perl_class != 0B= $perl_class =~ /[a-zA-Z]\w*/x
ABresult
0X0
100
111
180 unless length $perl_class != 0 && $perl_class =~ m/[a-zA-Z]\w*/x;
181 }
182 }
183  
184 9 $o->{API}{classes_override} = $value;
185 9 return;
186 }
187  
188  
189 sub _handle_libs_cfg_option {
190 1 ▶ my ($o, $value) = @_;
Subroutine _handle_libs_cfg_option: called CC=1 SCAR=0.0
191 1 $value = _make_arrayref($value);
192 1 _add_libs($o, $value);
193 1 return;
194 }
195  
196  
197 sub _handle_altlibs_cfg_option {
198 0 ▶ my ($o, $value) = @_;
Subroutine _handle_altlibs_cfg_option: not called CC=1 SCAR=6.9
199 0 $value = _make_arrayref($value);
200 0 push @{$o->{ILSM}{MAKEFILE}{LIBS}}, q{};
201 0 _add_libs($o, $value);
202 0 return;
203 }
204  
205  
206 sub _make_arrayref {
207 44 ▶ my $value = shift;
Subroutine _make_arrayref: called CC=2 SCAR=7.1
208 44 ▶ $value = [$value] unless ref $value eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "ARRAY" 44 0 44
209 44 return $value;
210 }
211  
212 sub _add_libs {
213 1 ▶ my ($o, $libs) = @_;
Subroutine _add_libs: called CC=2 SCAR=6.9
214 1 my $num = scalar @{$o->{ILSM}{MAKEFILE}{LIBS}} - 1;
215 1 $o->{ILSM}{MAKEFILE}{LIBS}[$num] .= q{ } . $_ for @{$libs};
216 1 return;
217 }
218  
219  
220 #============================================================================
221 # Print a small report if PRINT_INFO option is set
222 #============================================================================
223 sub info {
224 1 ▶ my $o = shift;
Subroutine info: called CC=18 SCAR=44.3
Pod documented
225 1 my $info = $o->SUPER::info;
226  
227 1 ▶ $o->parse unless $o->{ILSM}{parser};
Branch: 1 branch
BranchTrueFalseTotal
unless $o->{"ILSM"}{"parser"} 0 1 1
228 1 my $data = $o->{ILSM}{parser}{data};
229  
230 1 my (@class, @func);
231 1 ▶ if (defined $data->{classes}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data->{"classes"}) 0 1 1
232 0 for my $class (sort @{$data->{classes}}) {
233 my @parents
234 0 = grep { $_->{thing} eq 'inherits' } @{$data->{class}{$class}};
235 0 push @class, "\tclass $class";
236 push @class,
237 0 ▶ (' : ' . join(', ', map { $_->{scope} . q{ } . $_->{name} } @parents))
Branch: 1 branch
BranchTrueFalseTotal
if @parents 0 0 0
238 if @parents;
239 0 push @class, " {\n";
240 0 for my $thing (sort { $a->{name} cmp $b->{name} }
241 0 @{$data->{class}{$class}})
242 {
243 0 my ($name, $scope, $type) = @{$thing}{qw(name scope thing)};
244 0 ▶ next unless $scope eq 'public' and $type eq 'method';
Branch: 1 branch
BranchTrueFalseTotal
unless $scope eq "public" and $type eq "method" 0 0 0
Condition0%0/3
$scope eq "public" and $type eq "method"
!ll&&!rl&&r
000
MC/DC0%0/2
$scope eq "public" and $type eq "method"
$scope eq "public" $type eq "method"
Truth table0%0/3
A and B
A= $scope eq "public"B= $type eq "method"
ABresult
0X0
100
111
245 next
246 0 ▶ unless $o->check_type($thing, $name eq $class, $name eq "~$class",);
Branch: 1 branch
BranchTrueFalseTotal
unless $o->check_type($thing, $name eq $class, $name eq "~$class") 0 0 0
247 0 ▶ my $rtype = $thing->{return_type} || q{};
Condition0%0/2
$thing->{'return_type'} || ''
l!l
00
MC/DC0%0/1
$thing->{'return_type'} || ''
$thing->{'return_type'}
Truth table0%0/2
A
A= $thing->{'return_type'}
Aresult
11
00
248 0 ▶ push @class, "\t\t$rtype" . ($rtype ? q{ } : q{});
Branch: 1 branch
BranchTrueFalseTotal
$rtype ? : 0 0 0
249 0 push @class, $class . "::$name(";
250 0 push @class, _get_args($thing);
251 0 push @class, ");\n";
252 }
253 0 push @class, "\t};\n";
254 }
255 }
256 1 ▶ if (defined $data->{functions}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data->{"functions"}) 1 0 1
257 1 for my $function (sort @{$data->{functions}}) {
258 1 my $func = $data->{function}{$function};
259 1 ▶ next if $function =~ m/::/x;
Branch: 1 branch
BranchTrueFalseTotal
if $function =~ /::/x 0 1 1
260 1 ▶ next unless $o->check_type($func, 0, 0);
Branch: 1 branch
BranchTrueFalseTotal
unless $o->check_type($func, 0, 0) 0 1 1
261 1 push @func, "\t" . $func->{return_type} . q{ };
262 1 push @func, $func->{name} . '(';
263 1 push @func, _get_args($func);
264 1 push @func, ");\n";
265 }
266 }
267  
268 # Report:
269 {
270 1 local $" = q{};
271 1 ▶ $info .= "The following classes have been bound to Perl:\n@class\n"
Branch: 1 branch
BranchTrueFalseTotal
if @class 0 1 1
272 if @class;
273 1 ▶ $info .= "The following functions have been bound to Perl:\n@func\n"
Branch: 1 branch
BranchTrueFalseTotal
if @func 1 0 1
274 if @func;
275 }
276 1 ▶ $info .= Inline::Struct::info($o) if $o->{STRUCT}{'.any'};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"STRUCT"}{".any"} 0 1 1
277 1 return $info;
278 }
279  
280 sub _get_args {
281 1 ▶ my ($thing) = @_;
Subroutine _get_args: called CC=2 SCAR=7.0
282 1 my $count = @{ $thing->{arg_names} };
283 1 my @rarg_indexes = grep $thing->{arg_names}[$_] ne '...', 0..$count-1;
284 1 my $ellipsis = ($count - scalar @rarg_indexes) != 0;
285 1 ▶ join ', ', (map "$thing->{arg_types}[$_] $thing->{arg_names}[$_]", @rarg_indexes), $ellipsis ? '...' : ();
Branch: 1 branch
BranchTrueFalseTotal
$ellipsis ? : 0 1 1
286 }
287  
288 #============================================================================
289 # Generate a C++ parser
290 #============================================================================
291 sub get_parser {
292 38 ▶ my $o = shift;
Subroutine get_parser: called CC=1 SCAR=0.0
Pod undocumented
293 38 return Inline::CPP::Parser::RecDescent::get_parser_recdescent($o);
294 }
295  
296 #============================================================================
297 # Intercept xs_generate and create the typemap file
298 #============================================================================
299 sub xs_generate {
300 38 ▶ my $o = shift;
Subroutine xs_generate: called CC=1 SCAR=0.0
Pod undocumented
301 38 $o->write_typemap;
302 38 return $o->SUPER::xs_generate;
303 }
304  
305 #============================================================================
306 # Return bindings for functions and classes
307 #============================================================================
308 sub xs_bindings {
309 38 ▶ my $o = shift;
Subroutine xs_bindings: called CC=11 SCAR=25.2
Pod undocumented
310  
311 # What is modfname, and why are we taking it from a slice but not using it?
312 38 my ($pkg, $module) = @{$o->{API}}{qw(pkg module)};
313 38 my $data = $o->{ILSM}{parser}{data};
314 38 my @XS;
315  
316 warn "Warning: No Inline C++ functions or classes bound to Perl\n"
317 . "Check your C++ for Inline compatibility.\n\n"
318 38 ▶ if !defined $data->{classes} && !defined $data->{functions} && $^W;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $data->{"classes"} || defined $data->{"functions"} and $^W 0 38 38
Condition50%3/6
not defined $data->{"classes"} || defined $data->{"functions"} and $^W
!ll&&!rl&&r
3800
defined $data->{'classes'} || defined $data->{'functions'}
l!l&&r!l&&!r
3260
MC/DC0%0/3
not defined $data->{"classes"} || defined $data->{"functions"} and $^W
defined $data->{'classes'} defined $data->{'functions'} $^W
Truth table50%2/4
not(A || B) and C
A= defined $data->{'classes'}B= defined $data->{'functions'}C= $^W
ABCresult
1XX0
01X0
0000
0011
319 38 for my $class (@{$data->{classes}}) {
320 43 my $proper_pkg;
321  
322 # Possibly override package and class names
323 43 ▶ if (exists $o->{API}{classes_override}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $o->{'API'}{'classes_override'}) { } 13 30 43
324 13 my $ref_classes_override = ref($o->{API}{classes_override});
325 13 ▶ if ($ref_classes_override eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref_classes_override eq 'HASH') { } 5 8 13
elsif ($ref_classes_override eq 'CODE') { } 8 0 8
326 5 ▶ if (exists $o->{API}{classes_override}->{$class})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $o->{'API'}{'classes_override'}{$class}) { } 5 0 5
327 { # Override class name only
328 5 $proper_pkg = $pkg . '::' . $o->{API}{classes_override}->{$class};
329 }
330 else {
331 # Do not override package or class names
332 0 $proper_pkg = $pkg . '::' . $class;
333 }
334 }
335 elsif ($ref_classes_override eq 'CODE')
336 {
337 # Override both package and class names
338 8 $proper_pkg = &{$o->{API}{classes_override}}($class);
339 8 ▶ if ($proper_pkg eq '') { $proper_pkg = 'main'; }
Branch: 1 branch
BranchTrueFalseTotal
if ($proper_pkg eq "") 0 8 8
340 }
341 }
342 else { # Do not override package or class names
343 30 $proper_pkg = $pkg . '::' . $class;
344 }
345  
346 # Strip main:: from packages. There cannot be a package main::Foo!
347 43 $proper_pkg =~ s/^main::(.+)/$1/;
348  
349 # Set up the proper namespace
350 43 push @XS, _build_namespace($module, $proper_pkg);
351 43 push @XS, _generate_member_xs_wrappers($o, $pkg, $class, $proper_pkg);
352 }
353  
354 38 push @XS, _remove_xs_prefixes($o, $module, $pkg);
355 38 push @XS, _generate_nonmember_xs_wrappers($o);
356  
357 38 for (@{$data->{enums}}) {
358  
359 # Global enums.
360 0 $o->{ILSM}{XS}{BOOT} .= make_enum($pkg, @{$_}{qw( name body )});
361 }
362 38 return join q{}, @XS;
363 }
364  
365  
366 # Set up the proper namespace.
367 sub _build_namespace {
368 43 ▶ my ($module, $proper_pkg) = @_;
Subroutine _build_namespace: called CC=1 SCAR=0.0
369 43 return <<"END";
370  
371 MODULE = $module PACKAGE = $proper_pkg
372  
373 PROTOTYPES: DISABLE
374  
375 END
376 }
377  
378  
379 sub _generate_member_xs_wrappers {
380 43 ▶ my ($o, $pkg, $class, $proper_pkg) = @_;
Subroutine _generate_member_xs_wrappers: called CC=18 SCAR=29.0
381 43 my @XS;
382 43 my $data = $o->{ILSM}{parser}{data};
383 43 my ($ctor, $dtor) = (0, 0); ## no critic (ambiguous)
384  
385 # Look ahead to see if the class is abstract, i.e., has unimplemented
386 # virtual methods and therefore can never be instantiated directly
387 my $abstract = grep $_->{thing} eq 'method' && $_->{abstract},
388 43 ▶ @{$data->{class}{$class}};
Condition100%3/3
$_->{'thing'} eq 'method' && $_->{'abstract'}
!ll&&!rl&&r
531251
MC/DC100%2/2
$_->{'thing'} eq 'method' && $_->{'abstract'}
$_->{'thing'} eq 'method' $_->{'abstract'}
Truth table100%3/3
A && B
A= $_->{'thing'} eq 'method'B= $_->{'abstract'}
ABresult
0X0
100
111
389  
390 43 for my $thing (@{$data->{class}{$class}}) {
391 179 my ($name, $scope, $type) = @{$thing}{qw| name scope thing |};
392  
393 179 _handle_inheritance($o, $type, $scope, $pkg, $class, $name);
394  
395 # Get/set methods will go here:
396 # Cases we skip:
397 179 ▶ next if $abstract && ($name eq $class || $name eq "~$class");
Branch: 1 branch
BranchTrueFalseTotal
if $abstract and $name eq $class || $name eq "~$class" 1 178 179
Condition66%4/6
$abstract and $name eq $class || $name eq "~$class"
!ll&&!rl&&r
17540
$name eq $class or $name eq "~$class"
l!l&&r!l&&!r
103
MC/DC0%0/3
$abstract and $name eq $class || $name eq "~$class"
$abstract $name eq $class $name eq "~$class"
Truth table50%2/4
A and B or C
A= $abstractB= $name eq $classC= $name eq "~$class"
ABCresult
0XX0
1000
11X1
1011
398 178 ▶ next if ($type eq 'method' and $thing->{abstract});
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "method" and $thing->{"abstract"} 1 177 178
Condition100%3/3
$type eq "method" and $thing->{"abstract"}
!ll&&!rl&&r
531241
MC/DC100%2/2
$type eq "method" and $thing->{"abstract"}
$type eq "method" $thing->{"abstract"}
Truth table100%3/3
A and B
A= $type eq "method"B= $thing->{"abstract"}
ABresult
0X0
100
111
399 177 ▶ next if $scope ne 'public';
Branch: 1 branch
BranchTrueFalseTotal
if $scope ne "public" 46 131 177
400 131 ▶ if ($type eq 'enum') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'enum') { } 0 131 131
elsif ($type eq 'method' and not $name =~ /operator/) { } 119 24 143
401 0 $o->{ILSM}{XS}{BOOT} .= make_enum($proper_pkg, $name, $thing->{body});
402 }
403 ▶ elsif ($type eq 'method' and $name !~ m/operator/) {
Condition66%2/3
$type eq "method" and not $name =~ /operator/
!ll&&!rl&&r
120119
MC/DC50%1/2
$type eq "method" and not $name =~ /operator/
$type eq "method" not $name =~ /operator/
Truth table66%2/3
A and not(B)
A= $type eq "method"B= not $name =~ /operator/
ABresult
0X0
100
111
404  
405 # generate an XS wrapper
406 119 ▶ $ctor ||= ($name eq $class);
Condition100%3/3
$ctor ||= $name eq $class
l!l&&r!l&&!r
70418
MC/DC100%2/2
$ctor ||= $name eq $class
$ctor $name eq $class
Truth table100%3/3
A ||= B
A= $ctorB= $name eq $class
ABresult
1X1
011
000
407 119 ▶ $dtor ||= ($name eq "~$class");
Condition100%3/3
$dtor ||= $name eq "~$class"
l!l&&r!l&&!r
272567
MC/DC100%2/2
$dtor ||= $name eq "~$class"
$dtor $name eq "~$class"
Truth table100%3/3
A ||= B
A= $dtorB= $name eq "~$class"
ABresult
1X1
011
000
408 119 push @XS, $o->wrap($thing, $name, $class);
409 }
410 }
411  
412 # Provide default constructor and destructor:
413 43 ▶ push @XS, "$class *\n${class}::new()\n\n" unless ($ctor or $abstract);
Branch: 1 branch
BranchTrueFalseTotal
unless $ctor or $abstract 1 42 43
Condition100%3/3
$ctor or $abstract
l!l&&r!l&&!r
4111
MC/DC100%2/2
$ctor or $abstract
$ctor $abstract
Truth table100%3/3
A or B
A= $ctorB= $abstract
ABresult
1X1
011
000
414  
415 43 ▶ push @XS, "void\n${class}::DESTROY()\n\n" unless ($dtor or $abstract);
Branch: 1 branch
BranchTrueFalseTotal
unless $dtor or $abstract 17 26 43
Condition100%3/3
$dtor or $abstract
l!l&&r!l&&!r
25117
MC/DC100%2/2
$dtor or $abstract
$dtor $abstract
Truth table100%3/3
A or B
A= $dtorB= $abstract
ABresult
1X1
011
000
416 43 return @XS;
417 }
418  
419  
420 # Let Perl handle inheritance.
421 sub _handle_inheritance {
422 179 ▶ my ($o, $type, $scope, $pkg, $class, $name) = @_;
Subroutine _handle_inheritance: called CC=9 SCAR=24.0
423 179 ▶ if ($type eq 'inherits' and $scope eq 'public') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "inherits" and $scope eq "public") 9 170 179
Condition66%2/3
$type eq "inherits" and $scope eq "public"
!ll&&!rl&&r
17009
MC/DC50%1/2
$type eq "inherits" and $scope eq "public"
$type eq "inherits" $scope eq "public"
Truth table66%2/3
A and B
A= $type eq "inherits"B= $scope eq "public"
ABresult
0X0
100
111
424 9 ▶ $o->{ILSM}{XS}{BOOT} ||= q{};
Condition100%2/2
$o->{"ILSM"}{"XS"}{"BOOT"} ||= ""
l!l
54
MC/DC100%1/1
$o->{"ILSM"}{"XS"}{"BOOT"} ||= ""
$o->{"ILSM"}{"XS"}{"BOOT"}
Truth table100%2/2
A
A= $o->{"ILSM"}{"XS"}{"BOOT"}
Aresult
11
00
425
426 9 my $ISA_name;
427 my $parent;
428  
429 # Possibly override package and class names
430 9 ▶ if (exists $o->{API}{classes_override}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $o->{'API'}{'classes_override'}) { } 3 6 9
431 3 my $ref_classes_override = ref($o->{API}{classes_override});
432 3 ▶ if ($ref_classes_override eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref_classes_override eq 'HASH') { } 0 3 3
elsif ($ref_classes_override eq 'CODE') { } 3 0 3
433 0 ▶ if (exists $o->{API}{classes_override}->{$class})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $o->{'API'}{'classes_override'}{$class}) { } 0 0 0
434 { # Override class name only
435 0 $ISA_name = $pkg . '::' . $o->{API}{classes_override}->{$class} . '::ISA';
436 0 $parent = $pkg . '::' . $o->{API}{classes_override}->{$name};
437 }
438 else {
439 # Do not override package or class names
440 0 $ISA_name = $pkg . '::' . $class . '::ISA';
441 0 $parent = $pkg . '::' . $name;
442 }
443 }
444 elsif ($ref_classes_override eq 'CODE')
445 {
446 # Override both package and class names
447 3 $ISA_name = &{$o->{API}{classes_override}}($class) . '::ISA';
448 3 $parent = &{$o->{API}{classes_override}}($name);
449 3 ▶ if ($ISA_name eq '') { croak 'Have empty string for \$ISA_name, croaking' }
Branch: 1 branch
BranchTrueFalseTotal
if ($ISA_name eq "") 0 3 3
450 }
451 }
452 else { # Do not override package or class names
453 6 $ISA_name = $pkg . '::' . $class . '::ISA';
454 6 $parent = $pkg . '::' . $name;
455 }
456  
457 # Strip main:: from packages. There cannot be a package main::Foo!
458 9 $ISA_name =~ s/^main::(.+)/$1/;
459 9 $parent =~ s/^main::(.+)/$1/;
460
461 9 $o->{ILSM}{XS}{BOOT} .= <<"END";
462 {
463 #ifndef get_av
464 AV *isa = perl_get_av("$ISA_name", 1);
465 #else
466 AV *isa = get_av("$ISA_name", 1);
467 #endif
468 av_push(isa, newSVpv("$parent", 0));
469 }
470 END
471 }
472 179 return;
473 }
474  
475  
476 sub _generate_nonmember_xs_wrappers {
477 38 ▶ my $o = shift;
Subroutine _generate_nonmember_xs_wrappers: called CC=6 SCAR=18.0
478 38 my $data = $o->{ILSM}{parser}{data};
479 38 my @XS;
480 38 for my $function (@{$data->{functions}}) {
481  
482 # lose constructor defs outside class decls (and "implicit int")
483 31 ▶ next if $data->{function}{$function}{return_type} eq q{};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"function"}{$function}{"return_type"} eq "" 6 25 31
484 25 ▶ next if $data->{function}{$function}{return_type} =~ m/static/; #specl case
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"function"}{$function}{"return_type"} =~ /static/ 0 25 25
485 25 ▶ next if $function =~ m/::/x; # skip member functions.
Branch: 1 branch
BranchTrueFalseTotal
if $function =~ /::/x 9 16 25
486 16 ▶ next if $function =~ m/operator/; # and operators.
Branch: 1 branch
BranchTrueFalseTotal
if $function =~ /operator/ 0 16 16
487 16 push @XS, $o->wrap($data->{function}{$function}, $function);
488 }
489 38 return @XS;
490 }
491  
492  
493 # Generate XS code to remove prefixes from function names.
494 sub _remove_xs_prefixes {
495 38 ▶ my ($o, $module, $pkg) = @_;
Subroutine _remove_xs_prefixes: called CC=2 SCAR=6.9
496 my $prefix
497 38 ▶ = ($o->{ILSM}{XS}{PREFIX} ? "PREFIX = $o->{ILSM}{XS}{PREFIX}" : q{});
Branch: 1 branch
BranchTrueFalseTotal
$o->{'ILSM'}{'XS'}{'PREFIX'} ? : 2 36 38
498 38 return <<"END";
499  
500 MODULE = $module PACKAGE = $pkg $prefix
501  
502 PROTOTYPES: DISABLE
503  
504 END
505  
506 }
507  
508  
509 #============================================================================
510 # Generate an XS wrapper around anything: a C++ method or function
511 #============================================================================
512 sub wrap {
513 135 ▶ my ($o, $thing, $name, $class) = @_;
Subroutine wrap: called CC=31 SCAR=34.4
Pod undocumented
514 135 ▶ $class ||= q{};
Condition100%2/2
$class ||= ""
l!l
11916
MC/DC100%1/1
$class ||= ""
$class
Truth table100%2/2
A
A= $class
Aresult
11
00
515 135 my $t = q{ } x 4; # indents in 4-space increments.
516 135 my (@XS, @PREINIT, @CODE);
517  
518 135 my ($XS, $ctor, $dtor) = _map_subnames_cpp_to_perl($thing, $name, $class);
519  
520 135 push @XS, $XS;
521  
522 135 ▶ return q{} unless $o->check_type($thing, $ctor, $dtor);
Branch: 1 branch
BranchTrueFalseTotal
unless $o->check_type($thing, $ctor, $dtor) 0 135 135
523  
524 # Filter out optional subroutine arguments
525 135 my (@args, @opts, $ellipsis, $void);
526  
527 $thing->{arg_optional}[$_] ? push @opts, $_ : push @args, $_
528 135 ▶ for 0..$#{$thing->{arg_names}};
Branch: 1 branch
BranchTrueFalseTotal
$thing->{'arg_optional'}[$_] ? : 11 29 40
529  
530 135 ▶ if (@args and $thing->{arg_names}[$args[-1]] eq '...') {
Branch: 1 branch
BranchTrueFalseTotal
if (@args and $thing->{"arg_names"}[$args[-1]] eq "...") 1 134 135
Condition100%3/3
@args and $thing->{"arg_names"}[$args[-1]] eq "..."
!ll&&!rl&&r
115191
MC/DC100%2/2
@args and $thing->{"arg_names"}[$args[-1]] eq "..."
@args $thing->{"arg_names"}[$args[-1]] eq "..."
Truth table100%3/3
A and B
A= @argsB= $thing->{"arg_names"}[$args[-1]] eq "..."
ABresult
0X0
100
111
531 1 $ellipsis = 1;
532 1 pop @args;
533 }
534  
535 135 ▶ $void = ($thing->{return_type} and $thing->{return_type} eq 'void');
Condition100%3/3
$thing->{'return_type'} && $thing->{'return_type'} eq 'void'
!ll&&!rl&&r
66627
MC/DC100%2/2
$thing->{'return_type'} && $thing->{'return_type'} eq 'void'
$thing->{'return_type'} $thing->{'return_type'} eq 'void'
Truth table100%3/3
A && B
A= $thing->{'return_type'}B= $thing->{'return_type'} eq 'void'
ABresult
0X0
100
111
536  
537 push @XS, join q{},
538 (
539 '(',
540 join(', ',
541 135 ▶ (map $thing->{arg_names}[$_], @args),
Branch: 1 branch
BranchTrueFalseTotal
scalar @opts || $ellipsis ? : 7 128 135
Condition100%3/3
scalar @opts or $ellipsis
l!l&&r!l&&!r
61128
MC/DC100%2/2
scalar @opts or $ellipsis
scalar @opts $ellipsis
Truth table100%3/3
A or B
A= scalar @optsB= $ellipsis
ABresult
1X1
011
000
542 (scalar @opts or $ellipsis) ? '...' : ()),
543 ")\n",
544 );
545  
546 # Declare the non-optional arguments for XS type-checking
547 135 push @XS, "\t$thing->{arg_types}[$_]\t$thing->{arg_names}[$_]\n" for @args;
548  
549 # Wrap "complicated" subs in stack-checking code
550 135 ▶ if ($void or $ellipsis) {
Branch: 1 branch
BranchTrueFalseTotal
if ($void or $ellipsis) 8 127 135
Condition100%3/3
$void or $ellipsis
l!l&&r!l&&!r
71127
MC/DC100%2/2
$void or $ellipsis
$void $ellipsis
Truth table100%3/3
A or B
A= $voidB= $ellipsis
ABresult
1X1
011
000
551 8 push @PREINIT, "\tI32 *\t__temp_markstack_ptr;\n";
552 8 push @CODE, "\t__temp_markstack_ptr = PL_markstack_ptr++;\n";
553 }
554  
555 135 ▶ if (@opts) {
Branch: 3 branches
BranchTrueFalseTotal
if (@opts) { } 6 129 135
elsif ($void) { } 7 122 129
elsif ($ellipsis or $thing->{'rconst'}) { } 4 118 122
556 push @PREINIT, "\t$thing->{arg_types}[$_]\t$thing->{arg_names}[$_];\n"
557 6 for @opts;
558 6 ▶ push @CODE, 'switch(items' . ($class ? '-1' : q{}) . ") {\n";
Branch: 1 branch
BranchTrueFalseTotal
$class ? : 5 1 6
559  
560 6 my $offset = scalar @args; # which is the first optional?
561 6 my $total = $offset + scalar @opts;
562 6 for my $i ($offset .. $total - 1) {
563 11 push @CODE, 'case ' . ($i + 1) . ":\n";
564 11 my @tmp;
565 11 for my $j ($offset .. $i) {
566 18 my $targ = $thing->{arg_names}[$opts[$j - $offset]];
567 18 my $type = $thing->{arg_types}[$opts[$j - $offset]];
568 18 my $src = "ST($j)";
569 18 my $conv = $o->typeconv($targ, $src, $type, 'input_expr');
570 18 push @CODE, $conv . ";\n";
571 18 push @tmp, $targ;
572 }
573 11 ▶ push @CODE, "\tRETVAL = " unless $void;
Branch: 1 branch
BranchTrueFalseTotal
unless $void 11 0 11
574 push @CODE,
575 call_or_instantiate($name, $ctor, $dtor, $class, $thing->{rconst},
576 11 $thing->{return_type}, (map $thing->{arg_names}[$_], @args), @tmp);
577 11 push @CODE, "\tbreak; /* case " . ($i + 1) . " */\n";
578 }
579 6 push @CODE, "default:\n";
580 6 ▶ push @CODE, "\tRETVAL = " unless $void;
Branch: 1 branch
BranchTrueFalseTotal
unless $void 6 0 6
581 push @CODE,
582 call_or_instantiate($name, $ctor, $dtor, $class, $thing->{rconst},
583 6 $thing->{return_type}, map $thing->{arg_names}[$_], @args);
584 6 push @CODE, "} /* switch(items) */ \n";
585 }
586 elsif ($void) {
587 7 push @CODE, "\t";
588 push @CODE,
589 call_or_instantiate($name, $ctor, $dtor, $class, 0, q{},
590 7 map $thing->{arg_names}[$_], @args);
591 }
592 ▶ elsif ($ellipsis or $thing->{rconst}) {
Condition100%3/3
$ellipsis or $thing->{"rconst"}
l!l&&r!l&&!r
13118
MC/DC100%2/2
$ellipsis or $thing->{"rconst"}
$ellipsis $thing->{"rconst"}
Truth table100%3/3
A or B
A= $ellipsisB= $thing->{"rconst"}
ABresult
1X1
011
000
593 4 push @CODE, "\t";
594 4 push @CODE, 'RETVAL = ';
595 push @CODE,
596 call_or_instantiate($name, $ctor, $dtor, $class, $thing->{rconst},
597 4 $thing->{return_type}, map $thing->{arg_names}[$_], @args);
598 }
599 135 ▶ if ($void) {
Branch: 2 branches
BranchTrueFalseTotal
if ($void) { } 7 128 135
elsif ($ellipsis) { } 1 127 128
600 7 push @CODE, <<'END';
601 if (PL_markstack_ptr != __temp_markstack_ptr) {
602 /* truly void, because dXSARGS not invoked */
603 PL_markstack_ptr = __temp_markstack_ptr;
604 XSRETURN_EMPTY; /* return empty stack */
605 }
606 /* must have used dXSARGS; list context implied */
607 return; /* assume stack size is correct */
608 END
609 }
610 elsif ($ellipsis) {
611 1 push @CODE, "\tPL_markstack_ptr = __temp_markstack_ptr;\n";
612 }
613  
614 # The actual function:
615 135 local $" = q{};
616 135 ▶ push @XS, "${t}PREINIT:\n@PREINIT" if @PREINIT;
Branch: 1 branch
BranchTrueFalseTotal
if @PREINIT 14 121 135
617 135 push @XS, $t;
618 135 ▶ push @XS, 'PP' if $void and @CODE;
Branch: 1 branch
BranchTrueFalseTotal
if $void and @CODE 7 128 135
Condition66%2/3
$void and @CODE
!ll&&!rl&&r
12807
MC/DC50%1/2
$void and @CODE
$void @CODE
Truth table66%2/3
A and B
A= $voidB= @CODE
ABresult
0X0
100
111
619 135 ▶ push @XS, "CODE:\n@CODE" if @CODE;
Branch: 1 branch
BranchTrueFalseTotal
if @CODE 17 118 135
620 135 ▶ push @XS, "${t}OUTPUT:\nRETVAL\n" if @CODE and not $void;
Branch: 1 branch
BranchTrueFalseTotal
if @CODE and not $void 10 125 135
Condition100%3/3
@CODE and not $void
!ll&&!rl&&r
118710
MC/DC100%2/2
@CODE and not $void
@CODE not $void
Truth table100%3/3
A and not(B)
A= @CODEB= not $void
ABresult
0X0
100
111
621 135 push @XS, "\n";
622 135 return "@XS";
623 }
624  
625  
626 sub _map_subnames_cpp_to_perl {
627 135 ▶ my ($thing, $name, $class) = @_;
Subroutine _map_subnames_cpp_to_perl: called CC=4 SCAR=13.9
628 135 my ($XS, $ctor, $dtor) = (q{}, 0, 0);
629  
630 135 ▶ if ($name eq $class) { # ctor
Branch: 3 branches
BranchTrueFalseTotal
if ($name eq $class) { } 41 94 135
elsif ($name eq "~$class") { } 25 69 94
elsif ($class) { } 53 16 69
631 41 $XS = $class . " *\n" . $class . '::new';
632 41 $ctor = 1;
633 }
634 elsif ($name eq "~$class") { # dtor
635 25 $XS = "void\n$class" . '::DESTROY';
636 25 $dtor = 1;
637 }
638 elsif ($class) { # method
639 53 $XS = "$thing->{return_type}\n$class" . "::$thing->{name}";
640 }
641 else { # function
642 16 $XS = "$thing->{return_type}\n$thing->{name}";
643 }
644 135 return ($XS, $ctor, $dtor);
645 }
646  
647  
648 sub call_or_instantiate {
649 42 ▶ my ($name, $ctor, $dtor, $class, $const, $type, @args) = @_;
Subroutine call_or_instantiate: called CC=6 SCAR=17.9
Pod undocumented
650  
651 # Create an rvalue (which might be const-casted later).
652 42 my $rval = q{};
653 42 ▶ $rval .= 'new ' if $ctor;
Branch: 1 branch
BranchTrueFalseTotal
if $ctor 6 36 42
654 42 ▶ $rval .= 'delete ' if $dtor;
Branch: 1 branch
BranchTrueFalseTotal
if $dtor 1 41 42
655 42 ▶ $rval .= 'THIS->' if ($class and not($ctor or $dtor));
Branch: 1 branch
BranchTrueFalseTotal
if $class and not $ctor || $dtor 27 15 42
Condition100%6/6
$class and not $ctor || $dtor
!ll&&!rl&&r
8727
$ctor || $dtor
l!l&&r!l&&!r
6127
MC/DC100%3/3
$class and not $ctor || $dtor
$class $ctor $dtor
Truth table100%4/4
A and not(B || C)
A= $classB= $ctorC= $dtor
ABCresult
0XX0
11X0
1010
1001
656 42 $rval .= $name . '(' . join(q{,}, @args) . ')';
657  
658 42 return const_cast($rval, $const, $type) . ";\n";
659 } ### Tested.
660  
661 sub const_cast {
662 55 ▶ my ($value, $const, $type) = @_;
Subroutine const_cast: called CC=3 SCAR=11.0
Pod undocumented
663 55 ▶ return $value unless $const and $type =~ m/[*&]/x;
Branch: 1 branch
BranchTrueFalseTotal
unless $const and $type =~ /[*&]/x 49 6 55
Condition100%3/3
$const and $type =~ /[*&]/x
!ll&&!rl&&r
4636
MC/DC100%2/2
$const and $type =~ /[*&]/x
$const $type =~ /[*&]/x
Truth table100%3/3
A and B
A= $constB= $type =~ /[*&]/x
ABresult
0X0
100
111
664 6 return "const_cast<$type>($value)";
665 } ### Tested.
666  
667 sub write_typemap {
668 38 ▶ my $o = shift;
Subroutine write_typemap: called CC=7 SCAR=19.7
Pod undocumented
669 38 my $filename = "$o->{API}{build_dir}/CPP.map";
670 38 my $type_kind = $o->{ILSM}{typeconv}{type_kind};
671 38 my $typemap = q{};
672 $typemap .= $_ . "\t" x 2 . $TYPEMAP_KIND . "\n"
673 38 for grep { $type_kind->{$_} eq $TYPEMAP_KIND } keys %{$type_kind};
674 38 ▶ return unless length $typemap;
Branch: 1 branch
BranchTrueFalseTotal
unless length $typemap 6 32 38
675  
676 32 my $tm_output = <<"END";
677 TYPEMAP
678 $typemap
679 OUTPUT
680 $TYPEMAP_KIND
681 $o->{ILSM}{typeconv}{output_expr}{$TYPEMAP_KIND}
682 INPUT
683 $TYPEMAP_KIND
684 $o->{ILSM}{typeconv}{input_expr}{$TYPEMAP_KIND}
685 END
686  
687  
688 # Open an output file, create if necessary, then lock, then truncate.
689 # This replaces the following, which wasn't lock-safe:
690  
691 32 ▶ sysopen(my $TYPEMAP_FH, $filename, O_WRONLY | O_CREAT)
Branch: 1 branch
BranchTrueFalseTotal
sysopen my $TYPEMAP_FH, $filename, 65 or croak("Error: Can't write to ${filename}: $!") 0 32 32
692 or croak "Error: Can't write to $filename: $!";
693  
694 # Flock and truncate (truncating to zero length to simulate '>' mode).
695 32 ▶ flock $TYPEMAP_FH, LOCK_EX
Branch: 1 branch
BranchTrueFalseTotal
flock $TYPEMAP_FH, 2 or croak("Error: Can't obtain lock for ${filename}: $!") 0 32 32
696 or croak "Error: Can't obtain lock for $filename: $!";
697 32 ▶ truncate $TYPEMAP_FH, 0 or croak "Error: Can't truncate $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
truncate $TYPEMAP_FH, 0 or croak("Error: Can't truncate ${filename}: $!") 0 32 32
698  
699 # End of new lock-safe code.
700  
701 32 print {$TYPEMAP_FH} $tm_output;
702  
703 32 ▶ close $TYPEMAP_FH or croak "Error: Can't close $filename after write: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $TYPEMAP_FH or croak("Error: Can't close $filename after write: $!") 0 32 32
704  
705 32 $o->validate(TYPEMAPS => $filename);
706 32 return;
707 }
708  
709 # Generate type conversion code: perl2c or c2perl.
710 sub typeconv {
711 122 ▶ my ($o, $var, $arg, $type, $dir, $preproc) = @_;
Subroutine typeconv: called CC=3 SCAR=11.1
Pod undocumented
712 122 my $tkind = $o->{ILSM}{typeconv}{type_kind}{$type};
713 122 my $ret;
714 {
715 43 ▶ no strict; ## no critic (strict)
Subroutine BEGIN: called CC=1 SCAR=0.0
716 # The conditional avoids uninitialized warnings if user passes
717 # a C++ function with 'void' as param.
718 122 ▶ if (defined $tkind) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tkind) { } 122 0 122
719  
720 # eval of typemap gives "Uninit"
721 43 ▶ no warnings 'uninitialized'; ## no critic (warnings)
Subroutine BEGIN: called CC=1 SCAR=0.0
722 # Even without the conditional this line must remain.
723 122 $ret = eval ## no critic (eval)
724 qq{qq{$o->{ILSM}{typeconv}{$dir}{$tkind}}};
725 }
726 else {
727 0 $ret = q{};
728 }
729 }
730 122 chomp $ret;
731 122 ▶ $ret =~ s/\n/\\\n/xg if $preproc;
Branch: 1 branch
BranchTrueFalseTotal
if $preproc 0 122 122
732 122 return $ret;
733 }
734  
735 # Verify that the return type and all arguments can be bound to Perl.
736 sub check_type {
737 136 ▶ my ($o, $thing, $ctor, $dtor) = @_;
Subroutine check_type: called CC=8 SCAR=23.2
Pod undocumented
738 136 my $badtype;
739  
740 # strip "useless" modifiers so the type is found in typemap:
741 136 BADTYPE: while (1) {
742 136 ▶ if (!($ctor || $dtor)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ctor or $dtor) 70 66 136
Condition100%3/3
$ctor or $dtor
l!l&&r!l&&!r
412570
MC/DC100%2/2
$ctor or $dtor
$ctor $dtor
Truth table100%3/3
A or B
A= $ctorB= $dtor
ABresult
1X1
011
000
743 70 my $t = $thing->{return_type};
744 70 $t =~ s/^(\s|const|virtual|static)+//xg;
745 70 ▶ if ($t ne 'void' && !$o->typeconv(q{}, q{}, $t, 'output_expr')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t ne "void" and not $o->typeconv("", "", $t, "output_expr")) 0 70 70
Condition66%2/3
$t ne "void" and not $o->typeconv("", "", $t, "output_expr")
!ll&&!rl&&r
7630
MC/DC0%0/2
$t ne "void" and not $o->typeconv("", "", $t, "output_expr")
$t ne "void" not $o->typeconv("", "", $t, "output_expr")
Truth table66%2/3
A and not(B)
A= $t ne "void"B= not $o->typeconv("", "", $t, "output_expr")
ABresult
0X0
100
111
746 0 $badtype = $t;
747 0 last BADTYPE;
748 }
749 }
750 136 foreach (@{ $thing->{arg_types} }) {
751 42 s/^(?:const|\s)+//xgo;
752 42 ▶ if ($_ ne '...' && !$o->typeconv(q{}, q{}, $_, 'input_expr')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ ne "..." and not $o->typeconv("", "", $_, "input_expr")) 0 42 42
Condition66%2/3
$_ ne "..." and not $o->typeconv("", "", $_, "input_expr")
!ll&&!rl&&r
1410
MC/DC0%0/2
$_ ne "..." and not $o->typeconv("", "", $_, "input_expr")
$_ ne "..." not $o->typeconv("", "", $_, "input_expr")
Truth table66%2/3
A and not(B)
A= $_ ne "..."B= not $o->typeconv("", "", $_, "input_expr")
ABresult
0X0
100
111
753 0 $badtype = $_;
754 0 last BADTYPE;
755 }
756 }
757 136 return 1;
758 }
759  
760 # I don't really like this verbosity. This is what 'info' is for. Maybe we
761 # should ask Brian for an Inline=DEBUG option.
762 warn "No typemap for type $badtype. "
763 . "Skipping $thing->{return_type} $thing->{name}("
764 0 . join(', ', @{ $thing->{arg_types} }) . ")\n"
765 if 1;
766 0 return 0;
767 }
768  
769 # Generate boot-code for enumeration constants:
770 sub make_enum {
771 0 ▶ my ($class, $name, $body) = @_;
Subroutine make_enum: not called CC=3 SCAR=24.8
Pod undocumented
772 0 my @enum;
773 0 push @enum, <<"END";
774 \t{
775 \t HV * pkg = gv_stashpv(\"$class\", 1);
776 \t if (pkg == NULL)
777 \t croak("Can't find package '$class'\\n");
778 END
779 0 my $val = 0;
780 0 foreach (@{$body}) {
781 0 my ($k, $v) = @{$_};
782 0 ▶ $val = $v if defined $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 0 0 0
783 0 push @enum, "\tnewCONSTSUB(pkg, \"$k\", newSViv($val));\n";
784 0 ++$val;
785 }
786 0 push @enum, "\t}\n";
787 0 return join q{}, @enum;
788 }
789  
790  
791 43 1;
792  
793 __END__
↑ Summary blib/lib/Inline/CPP/Parser/RecDescent.pm »