blib/lib/ExtUtils/XSpp/Node/Function.pm

filter: Statementstmt 95.6% 221 / 231 Branchbran 87.0% 87 / 100 Conditioncond 82.2% 51 / 62 MC/DCmcdc 63.1% 24 / 38 Subroutinesub 86.8% 33 / 38 Podpod 90.0% 27 / 30 totaltotal 88.7% 443 / 499 CC 82 SCAR 31.0
CC
82
Cov
91%
print
49.1
add_exception_handlers
7.0
init
6.1
CRAP 22.3
« blib/lib/ExtUtils/XSpp/Grammar.pm ↑ Summary blib/lib/ExtUtils/XSpp/Driver.pm »
1 package ExtUtils::XSpp::Node::Function;
2 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 21 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 21 ▶use base 'ExtUtils::XSpp::Node';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 ExtUtils::XSpp::Node::Function - Node representing a function
10  
11 =head1 DESCRIPTION
12  
13 An L<ExtUtils::XSpp::Node> subclass representing a single function declaration
14 such as
15  
16 int foo();
17  
18 More importantly, L<ExtUtils::XSpp::Node::Method> inherits from this class,
19 so all in here equally applies to method nodes.
20  
21 =head1 METHODS
22  
23 =head2 new
24  
25 Creates a new C<ExtUtils::XSpp::Node::Function>.
26  
27 Named parameters: C<cpp_name> indicating the C++ name of the function,
28 C<perl_name> indicating the Perl name of the function (defaults to the
29 same as C<cpp_name>), C<arguments> can be a reference to an
30 array of C<ExtUtils::XSpp::Node::Argument> objects and finally
31 C<ret_type> indicates the (C++) return type of the function.
32  
33 Additionally, there are several optional decorators for a function
34 declaration (see L<ExtUtils::XSpp> for a list). These can be
35 passed to the constructor as C<code>, C<cleanup>, C<postcall>,
36 and C<catch>. C<catch> is special in that it must be a reference
37 to an array of class names.
38  
39 =cut
40  
41 113 ▶sub init {
Subroutine init: called CC=6 SCAR=18.0
Pod documented
42 113 my $this = shift;
43 113 my %args = @_;
44  
45 113 $this->{CPP_NAME} = $args{cpp_name};
46 113 ▶ $this->{PERL_NAME} = $args{perl_name} || $args{cpp_name};
Condition66%2/3
$args{'perl_name'} || $args{'cpp_name'}
l!l&&r!l&&!r
121010
MC/DC0%0/2
$args{'perl_name'} || $args{'cpp_name'}
$args{'perl_name'} $args{'cpp_name'}
Truth table66%2/3
A || B
A= $args{'perl_name'}B= $args{'cpp_name'}
ABresult
1X1
011
000
47 113 ▶ $this->{ARGUMENTS} = $args{arguments} || [];
Condition100%2/2
$args{'arguments'} || []
l!l
8528
MC/DC100%1/1
$args{'arguments'} || []
$args{'arguments'}
Truth table100%2/2
A
A= $args{'arguments'}
Aresult
11
00
48 113 $this->{RET_TYPE} = $args{ret_type};
49 113 $this->{CODE} = $args{code};
50 113 $this->{CALL_CODE} = $args{call_code};
51 113 $this->{CLEANUP} = $args{cleanup};
52 113 $this->{POSTCALL} = $args{postcall};
53 113 $this->{CLASS} = $args{class};
54 113 $this->{CATCH} = $args{catch};
55 113 $this->{CONDITION} = $args{condition};
56 113 $this->{ALIAS} = $args{alias};
57 113 $this->{TAGS} = $args{tags};
58 113 $this->{EMIT_CONDITION} = $args{emit_condition};
59  
60 113 my $index = 0;
61 113 foreach my $arg ( @{$this->{ARGUMENTS}} ) {
62 123 $arg->{FUNCTION} = $this;
63 123 $arg->{INDEX} = $index;
64 123 ++$index;
65 }
66  
67 113 ▶ if (@{$this->catch} > 1
Branch: 1 branch
BranchTrueFalseTotal
if (@{$this->catch;} > 1 and grep {$_ eq "nothing";} @{$$this{"CATCH"};}) 0 113 113
Condition66%2/3
@{$this->catch;} > 1 and grep {$_ eq "nothing";} @{$$this{"CATCH"};}
!ll&&!rl&&r
11030
MC/DC0%0/2
@{$this->catch;} > 1 and grep {$_ eq "nothing";} @{$$this{"CATCH"};}
@{$this->catch;} > 1 grep {$_ eq "nothing";} @{$$this{"CATCH"};}
Truth table66%2/3
A and B
A= @{$this->catch;} > 1B= grep {$_ eq "nothing";} @{$$this{"CATCH"};}
ABresult
0X0
100
111
68 7 and grep {$_ eq 'nothing'} @{$this->{CATCH}})
69 {
70 Carp::croak( ref($this) . " '" . $this->{CPP_NAME}
71 . "' is supposed to catch no exceptions, yet"
72 . " there are exception handlers ("
73 0 . join(", ", @{$this->{CATCH}}) . ")" );
74 }
75 113 return $this;
76 }
77  
78 =head2 resolve_typemaps
79  
80 Fetches the L<ExtUtils::XSpp::Typemap> object for
81 the return type and the arguments from the typemap registry
82 and stores a reference to those objects.
83  
84 =cut
85  
86 113 ▶sub resolve_typemaps {
Subroutine resolve_typemaps: called CC=5 SCAR=16.2
Pod documented
87 113 my $this = shift;
88 113 my $index = 0;
89  
90 113 ▶ if( $this->ret_type ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->ret_type) 110 3 113
91 $this->{TYPEMAPS}{RET_TYPE} ||=
92 110 ▶ ExtUtils::XSpp::Typemap::get_typemap_for_type( $this->ret_type );
Condition66%2/3
$this->{"TYPEMAPS"}{"RET_TYPE"} ||= ExtUtils::XSpp::Typemap::get_typemap_for_type($this->ret_type)
l!l&&r!l&&!r
91010
MC/DC0%0/2
$this->{"TYPEMAPS"}{"RET_TYPE"} ||= ExtUtils::XSpp::Typemap::get_typemap_for_type($this->ret_type)
$this->{"TYPEMAPS"}{"RET_TYPE"} ExtUtils::XSpp::Typemap::get_typemap_for_type($this->ret_type)
Truth table66%2/3
A ||= B
A= $this->{"TYPEMAPS"}{"RET_TYPE"}B= ExtUtils::XSpp::Typemap::get_typemap_for_type($this->ret_type)
ABresult
1X1
011
000
93 }
94 113 foreach my $a ( @{$this->arguments} ) {
95 123 ▶ $this->{TYPEMAPS}{ARGUMENTS}[$index++] ||=
Condition66%2/3
$this->{"TYPEMAPS"}{"ARGUMENTS"}[$index++] ||= ExtUtils::XSpp::Typemap::get_typemap_for_type($a->type)
l!l&&r!l&&!r
91140
MC/DC0%0/2
$this->{"TYPEMAPS"}{"ARGUMENTS"}[$index++] ||= ExtUtils::XSpp::Typemap::get_typemap_for_type($a->type)
$this->{"TYPEMAPS"}{"ARGUMENTS"}[$index++] ExtUtils::XSpp::Typemap::get_typemap_for_type($a->type)
Truth table66%2/3
A ||= B
A= $this->{"TYPEMAPS"}{"ARGUMENTS"}[$index++]B= ExtUtils::XSpp::Typemap::get_typemap_for_type($a->type)
ABresult
1X1
011
000
96 ExtUtils::XSpp::Typemap::get_typemap_for_type( $a->type );
97 }
98 }
99  
100  
101 =head2 resolve_exceptions
102  
103 Fetches the L<ExtUtils::XSpp::Exception> object for
104 the C<%catch> directives associated with this function.
105  
106 =cut
107  
108 95 ▶sub resolve_exceptions {
Subroutine resolve_exceptions: called CC=5 SCAR=16.1
Pod documented
109 95 my $this = shift;
110  
111 95 my @catch = @{$this->catch};
112  
113 95 my @exceptions;
114  
115 # If this method is not hard-wired to catch nothing...
116 95 ▶ if (not grep {$_ eq 'nothing'} @catch) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq "nothing";} @catch) 92 3 95
117 92 my %seen;
118 92 foreach my $catch (@catch) {
119 18 ▶ next if $seen{$catch}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$catch}++ 1 17 18
120 17 push @exceptions,
121 ExtUtils::XSpp::Exception->get_exception_for_name($catch);
122 }
123  
124 # If nothing else, catch std::exceptions nicely
125 92 ▶ if (not @exceptions) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@exceptions) 81 11 92
126 81 my $typenode = ExtUtils::XSpp::Node::Type->new(base => 'std::exception');
127 81 push @exceptions,
128 ExtUtils::XSpp::Exception::stdmessage->new( name => 'default',
129 type => $typenode );
130 }
131 }
132  
133 # Always catch the rest with an unspecific error message.
134 # If the method is hard-wired to catch nothing, we lie to the user
135 # for his own safety! (FIXME: debate this)
136 95 push @exceptions,
137 ExtUtils::XSpp::Exception::unknown->new( name => '', type => '' );
138  
139 95 $this->{EXCEPTIONS} = \@exceptions;
140 }
141  
142 18 ▶sub disable_exceptions {
Subroutine disable_exceptions: called CC=1 SCAR=0.0
Pod undocumented
143 18 my $this = shift;
144  
145 18 $this->{EXCEPTIONS} = [];
146 }
147  
148 =head2 add_exception_handlers
149  
150 Adds a list of exception names to the list of exception handlers.
151 This is mainly called by a class' C<add_methods> method.
152 If the function is hard-wired to have no exception handlers,
153 any extra handlers from the class are ignored.
154  
155 =cut
156  
157  
158 51 ▶sub add_exception_handlers {
Subroutine add_exception_handlers: called CC=7 SCAR=19.5
Pod documented
159 51 my $this = shift;
160  
161 # ignore class %catch'es if overridden with "nothing" in the method
162 51 ▶ if (@{$this->catch} == 1 and $this->{CATCH}[0] eq 'nothing') {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$this->catch;} == 1 and $this->{"CATCH"}[0] eq "nothing") 2 49 51
Condition100%3/3
@{$this->catch;} == 1 and $this->{"CATCH"}[0] eq "nothing"
!ll&&!rl&&r
4722
MC/DC100%2/2
@{$this->catch;} == 1 and $this->{"CATCH"}[0] eq "nothing"
@{$this->catch;} == 1 $this->{"CATCH"}[0] eq "nothing"
Truth table100%3/3
A and B
A= @{$this->catch;} == 1B= $this->{"CATCH"}[0] eq "nothing"
ABresult
0X0
100
111
163 2 return();
164 }
165  
166 # ignore class %catch{nothing} if overridden in the method
167 49 ▶ if (@_ == 1 and $_[0] eq 'nothing' and @{$this->catch}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and $_[0] eq "nothing" and @{$this->catch;}) 1 48 49
Condition83%5/6
@_ == 1 and $_[0] eq "nothing" and @{$this->catch;}
!ll&&!rl&&r
4711
@_ == 1 and $_[0] eq "nothing"
!ll&&!rl&&r
4702
MC/DC66%2/3
@_ == 1 and $_[0] eq "nothing" and @{$this->catch;}
@_ == 1 $_[0] eq "nothing" @{$this->catch;}
Truth table75%3/4
A and B and C
A= @_ == 1B= $_[0] eq "nothing"C= @{$this->catch;}
ABCresult
0XX0
10X0
1100
1111
168 1 return();
169 }
170  
171 48 ▶ $this->{CATCH} ||= [];
Condition50%1/2
$this->{"CATCH"} ||= []
l!l
480
MC/DC0%0/1
$this->{"CATCH"} ||= []
$this->{"CATCH"}
Truth table50%1/2
A
A= $this->{"CATCH"}
Aresult
11
00
172 48 push @{$this->{CATCH}}, @_;
173  
174 48 return();
175 }
176  
177 # Depending on argument style, this produces either: (style=kr)
178 #
179 # return_type
180 # class_name::function_name( args = def, ... )
181 # type arg
182 # type arg
183 # PREINIT:
184 # aux vars
185 # [ALIAS:
186 # ID = INTEGER...]
187 # [PP]CODE:
188 # RETVAL = new Foo( THIS->method( arg1, *arg2 ) );
189 # POSTCALL:
190 # /* anything */
191 # OUTPUT:
192 # RETVAL
193 # CLEANUP:
194 # /* anything */
195 #
196 # Or: (style=ansi)
197 #
198 # return_type
199 # class_name::function_name( type arg1 = def, type arg2 = def, ... )
200 # PREINIT:
201 # (rest as above)
202  
203 109 ▶sub print {
Subroutine print: called CC=49 SCAR=38.9
Pod documented
204 109 my $this = shift;
205 109 my $state = shift;
206  
207 109 my $out = '';
208 109 my $fname = $this->perl_function_name;
209 109 my $args = $this->arguments;
210 109 my $ret_type = $this->ret_type;
211 109 my $ret_typemap = $this->{TYPEMAPS}{RET_TYPE};
212 109 ▶ my $aliases = $this->{ALIAS} || {};
Condition100%2/2
$this->{'ALIAS'} || {}
l!l
8425
MC/DC100%1/1
$this->{'ALIAS'} || {}
$this->{'ALIAS'}
Truth table100%2/2
A
A= $this->{'ALIAS'}
Aresult
11
00
213  
214 109 my $has_aliases = scalar(keys %$aliases);
215  
216 109 ▶ $out .= '#if ' . $this->emit_condition . "\n" if $this->emit_condition;
Branch: 1 branch
BranchTrueFalseTotal
if $this->emit_condition 0 109 109
217  
218 109 my( $init, $arg_list, $call_arg_list, $code, $output, $cleanup,
219 $postcall, $precall, $alias ) = ( ('') x 9 );
220  
221 # compute the precall code, XS argument list and C++ argument list using
222 # the typemap information
223 109 ▶ if( $args && @$args ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args and @$args) 85 24 109
Condition66%2/3
$args and @$args
!ll&&!rl&&r
02485
MC/DC50%1/2
$args and @$args
$args @$args
Truth table66%2/3
A and B
A= $argsB= @$args
ABresult
0X0
100
111
224 85 ▶ my $has_self = $this->is_method ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$this->is_method ? : 44 41 85
225 85 my( @arg_list, @call_arg_list );
226 85 foreach my $i ( 0 .. $#$args ) {
227 123 my $arg = ${$args}[$i];
228 123 my $t = $this->{TYPEMAPS}{ARGUMENTS}[$i];
229 123 my $pc = $t->precall_code( sprintf( 'ST(%d)', $i + $has_self ),
230 $arg->name );
231  
232 123 ▶ push @arg_list, $t->cpp_type . ' ' . $arg->name .
Branch: 1 branch
BranchTrueFalseTotal
$arg->has_default ? : 4 119 123
233 ( $arg->has_default ? ' = ' . $arg->default : '' );
234  
235 123 my $call_code = $t->call_parameter_code( $arg->name );
236 123 ▶ push @call_arg_list, defined( $call_code ) ? $call_code : $arg->name;
Branch: 1 branch
BranchTrueFalseTotal
defined $call_code ? : 5 118 123
237 123 ▶ $precall .= $pc . ";\n" if $pc
Branch: 1 branch
BranchTrueFalseTotal
if $pc 3 120 123
238 }
239  
240 85 $arg_list = ' ' . join( ', ', @arg_list ) . ' ';
241 85 $call_arg_list = ' ' . join( ', ', @call_arg_list ) . ' ';
242 }
243  
244 # If there's %alias{foo = 123} definitions, generate ALIAS section
245 109 ▶ if ($has_aliases) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_aliases) 4 105 109
246 # order by ordinal for consistent hash-order-independent output
247 my @alias_list = map " $_ = $aliases->{$_}\n",
248 4 sort {$aliases->{$a} <=> $aliases->{$b}}
249 keys %$aliases;
250 4 $alias = " ALIAS:\n" . join("", @alias_list);
251 }
252  
253 109 ▶ my $retstr = $ret_typemap ? $ret_typemap->cpp_type : 'void';
Branch: 1 branch
BranchTrueFalseTotal
$ret_typemap ? : 106 3 109
254  
255 # special case: constructors with name different from 'new'
256 # need to be declared 'static' in XS
257 109 ▶ if( $this->isa( 'ExtUtils::XSpp::Node::Constructor' ) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($this->isa("ExtUtils::XSpp::Node::Constructor") and $this->perl_name ne $this->cpp_name) 3 106 109
Condition100%3/3
$this->isa("ExtUtils::XSpp::Node::Constructor") and $this->perl_name ne $this->cpp_name
!ll&&!rl&&r
10513
MC/DC100%2/2
$this->isa("ExtUtils::XSpp::Node::Constructor") and $this->perl_name ne $this->cpp_name
$this->isa("ExtUtils::XSpp::Node::Constructor") $this->perl_name ne $this->cpp_name
Truth table100%3/3
A and B
A= $this->isa("ExtUtils::XSpp::Node::Constructor")B= $this->perl_name ne $this->cpp_name
ABresult
0X0
100
111
258 $this->perl_name ne $this->cpp_name ) {
259 3 $retstr = "static $retstr";
260 }
261  
262 109 ▶ my $has_ret = $ret_typemap && !$ret_typemap->type->is_void;
Condition100%3/3
$ret_typemap && !$ret_typemap->type->is_void
!ll&&!rl&&r
33076
MC/DC100%2/2
$ret_typemap && !$ret_typemap->type->is_void
$ret_typemap !$ret_typemap->type->is_void
Truth table100%3/3
A && not(B)
A= $ret_typemapB= !$ret_typemap->type->is_void
ABresult
0X0
100
111
263  
264 109 ▶ my $ppcode = $has_ret && $ret_typemap->output_list( '' ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$has_ret && $ret_typemap->output_list('') ? : 1 108 109
Condition100%3/3
$has_ret and $ret_typemap->output_list("")
!ll&&!rl&&r
33751
MC/DC100%2/2
$has_ret and $ret_typemap->output_list("")
$has_ret $ret_typemap->output_list("")
Truth table100%3/3
A and B
A= $has_retB= $ret_typemap->output_list("")
ABresult
0X0
100
111
265 109 ▶ my $code_type = $ppcode ? "PPCODE" : "CODE";
Branch: 1 branch
BranchTrueFalseTotal
$ppcode ? : 1 108 109
266 109 my $ccode = $this->_call_code( $call_arg_list );
267 109 ▶ if ($this->{CALL_CODE}) {
Branch: 5 branches
BranchTrueFalseTotal
if ($this->{'CALL_CODE'}) { } 18 91 109
elsif ($this->isa('ExtUtils::XSpp::Node::Destructor')) { } 3 88 91
elsif ($has_ret and defined $ret_typemap->call_function_code('', '')) { } 2 86 88
elsif ($has_ret) { } 65 21 86
elsif ($has_aliases) { } 0 21 21
268 18 $ccode = join( "\n", @{$this->{CALL_CODE}} );
269 } elsif ($this->isa('ExtUtils::XSpp::Node::Destructor')) {
270 3 $ccode = 'delete THIS';
271 3 $has_ret = 0;
272 ▶ } elsif( $has_ret && defined $ret_typemap->call_function_code( '', '' ) ) {
Condition100%3/3
$has_ret and defined $ret_typemap->call_function_code("", "")
!ll&&!rl&&r
21652
MC/DC100%2/2
$has_ret and defined $ret_typemap->call_function_code("", "")
$has_ret defined $ret_typemap->call_function_code("", "")
Truth table100%3/3
A and B
A= $has_retB= defined $ret_typemap->call_function_code("", "")
ABresult
0X0
100
111
273 2 $ccode = $ret_typemap->call_function_code( $ccode, 'RETVAL' );
274 } elsif( $has_ret ) {
275 65 ▶ if ($has_aliases) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_aliases) { } 4 61 65
276 4 $ccode = $this->_generate_alias_conditionals($call_arg_list, 1); # 1 == use RETVAL
277 } else {
278 61 $ccode = "RETVAL = $ccode";
279 }
280 } elsif( $has_aliases ) { # aliases but no RETVAL
281 0 $ccode = $this->_generate_alias_conditionals($call_arg_list, 0); # 0 == no RETVAL
282 }
283  
284 109 my @catchers = @{$this->{EXCEPTIONS}};
285 109 $code .= " $code_type:\n";
286 109 ▶ $code .= " try {\n" if @catchers;
Branch: 1 branch
BranchTrueFalseTotal
if @catchers 91 18 109
287 109 ▶ if ($precall) {
Branch: 1 branch
BranchTrueFalseTotal
if ($precall) 2 107 109
288 2 $code .= ' ' . $precall;
289 }
290 109 ▶ $code .= (@catchers ? ' ' : '') . ' ' . $ccode . ";\n";
Branch: 1 branch
BranchTrueFalseTotal
@catchers ? : 91 18 109
291 109 ▶ if( $has_ret && defined $ret_typemap->output_code( '', '' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_ret and defined $ret_typemap->output_code("", "")) 1 108 109
Condition100%3/3
$has_ret and defined $ret_typemap->output_code("", "")
!ll&&!rl&&r
33751
MC/DC100%2/2
$has_ret and defined $ret_typemap->output_code("", "")
$has_ret defined $ret_typemap->output_code("", "")
Truth table100%3/3
A and B
A= $has_retB= defined $ret_typemap->output_code("", "")
ABresult
0X0
100
111
292 1 my $retcode = $ret_typemap->output_code( 'ST(0)', 'RETVAL' );
293 1 $code .= ' ' . $retcode . ";\n";
294 }
295 109 ▶ if( $has_ret && defined $ret_typemap->output_list( '' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_ret and defined $ret_typemap->output_list("")) 1 108 109
Condition100%3/3
$has_ret and defined $ret_typemap->output_list("")
!ll&&!rl&&r
33751
MC/DC100%2/2
$has_ret and defined $ret_typemap->output_list("")
$has_ret defined $ret_typemap->output_list("")
Truth table100%3/3
A and B
A= $has_retB= defined $ret_typemap->output_list("")
ABresult
0X0
100
111
296 1 my $retcode = $ret_typemap->output_list( 'RETVAL' );
297 1 $code .= ' ' . $retcode . ";\n";
298 }
299 109 ▶ $code .= " }\n" if @catchers;
Branch: 1 branch
BranchTrueFalseTotal
if @catchers 91 18 109
300 109 foreach my $exception_handler (@catchers) {
301 185 my $handler_code = $exception_handler->handler_code;
302 185 $code .= $handler_code;
303 }
304  
305 109 ▶ $output = " OUTPUT: RETVAL\n" if $has_ret;
Branch: 1 branch
BranchTrueFalseTotal
if $has_ret 76 33 109
306  
307 109 ▶ if( $has_ret && defined $ret_typemap->cleanup_code( '', '' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_ret and defined $ret_typemap->cleanup_code("", "")) 2 107 109
Condition100%3/3
$has_ret and defined $ret_typemap->cleanup_code("", "")
!ll&&!rl&&r
33742
MC/DC100%2/2
$has_ret and defined $ret_typemap->cleanup_code("", "")
$has_ret defined $ret_typemap->cleanup_code("", "")
Truth table100%3/3
A and B
A= $has_retB= defined $ret_typemap->cleanup_code("", "")
ABresult
0X0
100
111
308 2 $cleanup .= " CLEANUP:\n";
309 2 my $cleanupcode = $ret_typemap->cleanup_code( 'ST(0)', 'RETVAL' );
310 2 $cleanup .= ' ' . $cleanupcode . ";\n";
311 }
312  
313 109 ▶ if( $this->code ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->code) 11 98 109
314 11 $code = " $code_type:\n " . join( "\n", @{$this->code} ) . "\n";
315 11 ▶ $output = " OUTPUT: RETVAL\n" if $code =~ m/\bRETVAL\b/;
Branch: 1 branch
BranchTrueFalseTotal
if $code =~ /\bRETVAL\b/ 4 7 11
316 }
317 109 ▶ if( $this->postcall ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->postcall) 3 106 109
318 3 $postcall = " POSTCALL:\n " . join( "\n", @{$this->postcall} ) . "\n";
319 3 ▶ $output ||= " OUTPUT: RETVAL\n" if $has_ret;
Branch: 1 branch
BranchTrueFalseTotal
if $has_ret 2 1 3
Condition50%1/2
$output ||= " OUTPUT: RETVAL\n"
l!l
20
MC/DC0%0/1
$output ||= " OUTPUT: RETVAL\n"
$output
Truth table50%1/2
A
A= $output
Aresult
11
00
320 }
321 109 ▶ if( $this->cleanup ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->cleanup) 3 106 109
322 3 ▶ $cleanup ||= " CLEANUP:\n";
Condition50%1/2
$cleanup ||= " CLEANUP:\n"
l!l
03
MC/DC0%0/1
$cleanup ||= " CLEANUP:\n"
$cleanup
Truth table50%1/2
A
A= $cleanup
Aresult
11
00
323 3 my $clcode = join( "\n", @{$this->cleanup} );
324 3 $cleanup .= " $clcode\n";
325 }
326 109 ▶ if( $ppcode ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ppcode) 1 108 109
327 1 $output = '';
328 }
329  
330 109 ▶ if( !$this->is_method && $fname =~ /^(.*)::(\w+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $this->is_method and $fname =~ /^(.*)::(\w+)$/) 1 108 109
Condition100%3/3
not $this->is_method and $fname =~ /^(.*)::(\w+)$/
!ll&&!rl&&r
67411
MC/DC100%2/2
not $this->is_method and $fname =~ /^(.*)::(\w+)$/
not $this->is_method $fname =~ /^(.*)::(\w+)$/
Truth table100%3/3
not(A) and B
A= not $this->is_methodB= $fname =~ /^(.*)::(\w+)$/
ABresult
0X0
100
111
331 1 my $pcname = $1;
332 1 $fname = $2;
333 1 my $cur_module = $state->{current_module}->to_string;
334 1 $out .= <<EOT;
335 $cur_module PACKAGE=$pcname
336  
337 EOT
338 }
339  
340 109 my $head = "$retstr\n"
341 . "$fname($arg_list)\n";
342 109 my $body = $alias . $init . $code . $postcall . $output . $cleanup;
343  
344 # cleanup potential multiple newlines because they break XSUBs
345 109 $body =~ s/^\s*\n//mg;
346 109 $body .= "\n";
347  
348 109 ▶ $this->_munge_code(\$body) if $this->has_argument_with_length;
Branch: 1 branch
BranchTrueFalseTotal
if $this->has_argument_with_length 3 106 109
349  
350 109 $out .= $head . $body;
351 109 ▶ $out .= '#endif // ' . $this->emit_condition . "\n" if $this->emit_condition;
Branch: 1 branch
BranchTrueFalseTotal
if $this->emit_condition 0 109 109
352  
353 109 return $out;
354 }
355  
356 # This replaces the use of "length(varname)" with
357 # the proper name of the XS variable that is auto-generated in
358 # case of the XS length() feature. The Argument's take care of
359 # this and do nothing if they're not of the "length" type.
360 # Any additional checking "$this->_munge_code(\$code) if $using_length"
361 # is just an optimization!
362 3 ▶sub _munge_code {
Subroutine _munge_code: called CC=2 SCAR=6.9
363 3 my $this = shift;
364 3 my $code = shift;
365
366 3 foreach my $arg (@{$this->{ARGUMENTS}}) {
367 6 $$code = $arg->fix_name_in_code($$code);
368 }
369 }
370  
371 =head2 print_declaration
372  
373 Returns a string with a C++ method declaration for the node.
374  
375 =cut
376  
377 0 ▶sub print_declaration {
Subroutine print_declaration: not called CC=2 SCAR=17.9
Pod documented
378 0 my( $this ) = @_;
379  
380 return $this->ret_type->print . ' ' . $this->cpp_name . '( ' .
381 0 ▶ join( ', ', map $_->print, @{$this->arguments} ) . ')' .
Branch: 1 branch
BranchTrueFalseTotal
$this->const ? : 0 0 0
382 ( $this->const ? ' const' : '' );
383 }
384  
385 =head2 perl_function_name
386  
387 Returns the name of the Perl function to generate.
388  
389 =cut
390  
391 42 ▶sub perl_function_name { $_[0]->perl_name }
Subroutine perl_function_name: called CC=1 SCAR=0.0
Pod documented
392  
393 =head2 is_method
394  
395 Returns whether the object at hand is a method. Hard-wired
396 to be false for C<ExtUtils::XSpp::Node::Function> object,
397 but overridden in the L<ExtUtils::XSpp::Node::Method> sub-class.
398  
399 =cut
400  
401 83 ▶sub is_method { 0 }
Subroutine is_method: called CC=1 SCAR=0.0
Pod documented
402  
403 =head2 has_argument_with_length
404  
405 Returns true if the function has any argument that uses the XS length
406 feature.
407  
408 =cut
409  
410 109 ▶sub has_argument_with_length {
Subroutine has_argument_with_length: called CC=3 SCAR=11.0
Pod documented
411 109 my $this = shift;
412 109 foreach my $arg (@{$this->{ARGUMENTS}}) {
413 123 ▶ return 1 if $arg->uses_length;
Branch: 1 branch
BranchTrueFalseTotal
if $arg->uses_length 3 120 123
414 }
415 106 return();
416 }
417  
418  
419 =begin documentation
420  
421 ExtUtils::XSpp::Function::_call_code( argument_string )
422  
423 Return something like "foo( $argument_string )".
424  
425 =end documentation
426  
427 =cut
428  
429 45 ▶sub _call_code { return $_[0]->cpp_name . '(' . $_[1] . ')'; }
Subroutine _call_code: called CC=1 SCAR=0.0
430  
431 =begin documentation
432  
433 ExtUtils::XSpp::Function::_call_code_aliased( function_alias_name, argument_string )
434  
435 Return something like "$function_alias_name( $argument_string )".
436  
437 =end documentation
438  
439 =cut
440  
441 4 ▶sub _call_code_aliased { return $_[1] . '(' . $_[2] . ')'; }
Subroutine _call_code_aliased: called CC=1 SCAR=0.0
442  
443 =begin documentation
444  
445 ExtUtils::XSpp::Function::_generate_alias_conditionals( argument_string, use_retval_bool )
446  
447 Generates if()else if()else block for XS function name aliasing (cf. the XS manual and the ix
448 variable). If use_retval_bool is true, each included function call will contain an
449 assignment to RETVAL.
450  
451 Returns the generated code.
452  
453 =end documentation
454  
455 =cut
456 4 ▶sub _generate_alias_conditionals {
Subroutine _generate_alias_conditionals: called CC=3 SCAR=11.0
457 4 my ($this, $call_arg_list, $use_retval) = @_;
458 4 my $aliases = $this->{ALIAS};
459  
460 4 ▶ my $retval_code = $use_retval ? "RETVAL = " : "";
Branch: 1 branch
BranchTrueFalseTotal
$use_retval ? : 4 0 4
461 4 my $buf = "if (ix == 0) {\n $retval_code"
462 . $this->_call_code($call_arg_list)
463 . ";\n}\n";
464 # order by ordinal for consistent hash-order-independent output
465 4 foreach my $alias (sort {$aliases->{$a} <=> $aliases->{$b}} keys %$aliases)
466 {
467 5 $buf .= "else if (ix == $aliases->{$alias}) {\n "
468 . $retval_code . $this->_call_code_aliased($alias, $call_arg_list)
469 . ";\n}\n";
470 }
471 4 $buf .= "else\n croak(\"Panic: Invalid invocation of function alias number %i!\", (int)ix))";
472  
473 # indent
474 4 $buf =~ s/^/ /gm;
475 4 $buf =~ s/^\s+//; # first line will get special treatment...
476  
477 4 return $buf;
478 }
479  
480  
481 =head1 ACCESSORS
482  
483 =head2 cpp_name
484  
485 Returns the C++ name of the function.
486  
487 =head2 perl_name
488  
489 Returns the Perl name of the function (defaults to same as C++).
490  
491 =head2 set_perl_name
492  
493 Sets the Perl name of the function.
494  
495 =head2 arguments
496  
497 Returns the internal array reference of L<ExtUtils::XSpp::Node::Argument>
498 objects that represent the function arguments.
499  
500 =head2 ret_type
501  
502 Returns the C++ return type.
503  
504 =head2 code
505  
506 Returns the C<%code> decorator if any.
507  
508 =head2 set_code
509  
510 Sets the implementation for the method call (equivalent to using
511 C<%code>); takes the code as an array reference containing the lines.
512  
513 =head2 cleanup
514  
515 Returns the C<%cleanup> decorator if any.
516  
517 =head2 postcall
518  
519 Returns the C<%postcall> decorator if any.
520  
521 =head2 catch
522  
523 Returns the set of exception types that were associated
524 with the function via C<%catch>. (array reference)
525  
526 =head2 aliases
527  
528 Returns a hashref of C<name =E<gt> position>
529 function name aliases (see %alias and L<perlxs> ALIAS keyword).
530 Does not include the main function name.
531  
532 =cut
533  
534 129 ▶sub cpp_name { $_[0]->{CPP_NAME} }
Subroutine cpp_name: called CC=1 SCAR=0.0
Pod documented
535 0 ▶sub set_cpp_name { $_[0]->{CPP_NAME} = $_[1] }
Subroutine set_cpp_name: not called CC=1 SCAR=6.9
Pod undocumented
536 114 ▶sub perl_name { $_[0]->{PERL_NAME} }
Subroutine perl_name: called CC=1 SCAR=0.0
Pod documented
537 10 ▶sub set_perl_name { $_[0]->{PERL_NAME} = $_[1] }
Subroutine set_perl_name: called CC=1 SCAR=0.0
Pod documented
538 317 ▶sub arguments { $_[0]->{ARGUMENTS} }
Subroutine arguments: called CC=1 SCAR=0.0
Pod documented
539 305 ▶sub ret_type { $_[0]->{RET_TYPE} }
Subroutine ret_type: called CC=1 SCAR=0.0
Pod documented
540 120 ▶sub code { $_[0]->{CODE} }
Subroutine code: called CC=1 SCAR=0.0
Pod documented
541 0 ▶sub set_code { $_[0]->{CODE} = $_[1] }
Subroutine set_code: not called CC=1 SCAR=6.9
Pod documented
542 112 ▶sub cleanup { $_[0]->{CLEANUP} }
Subroutine cleanup: called CC=1 SCAR=0.0
Pod documented
543 112 ▶sub postcall { $_[0]->{POSTCALL} }
Subroutine postcall: called CC=1 SCAR=0.0
Pod documented
544 261 ▶sub catch { $_[0]->{CATCH} ? $_[0]->{CATCH} : [] }
Subroutine catch: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'CATCH'} ? : 243 18 261
545 0 ▶sub aliases { $_[0]->{ALIAS} ? $_[0]->{ALIAS} : {} }
Subroutine aliases: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'ALIAS'} ? : 0 0 0
546 95 ▶sub tags { $_[0]->{TAGS} }
Subroutine tags: called CC=1 SCAR=0.0
Pod undocumented
547  
548 =head2 set_static
549  
550 Sets the C<static>-ness attribute of the function.
551 Can be either undef (i.e. not static), C<"package_static">,
552 or C<"class_static">.
553  
554 =head2 package_static
555  
556 Returns whether the function is package static. A package static
557 function can be invoked as:
558  
559 My::Package::Function( ... );
560  
561 =head2 class_static
562  
563 Returns whether the function is class static. A class static function
564 can be invoked as:
565  
566 My::Package->Function( ... );
567  
568 =cut
569  
570 1 ▶sub set_static { $_[0]->{STATIC} = $_[1] }
Subroutine set_static: called CC=1 SCAR=0.0
Pod documented
571 125 ▶sub package_static { ( $_[0]->{STATIC} || '' ) eq 'package_static' }
Subroutine package_static: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
$_[0]{'STATIC'} || ''
l!l
2123
MC/DC100%1/1
$_[0]{'STATIC'} || ''
$_[0]{'STATIC'}
Truth table100%2/2
A
A= $_[0]{'STATIC'}
Aresult
11
00
572 0 ▶sub class_static { ( $_[0]->{STATIC} || '' ) eq 'class_static' }
Subroutine class_static: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
$_[0]{'STATIC'} || ''
l!l
00
MC/DC0%0/1
$_[0]{'STATIC'} || ''
$_[0]{'STATIC'}
Truth table0%0/2
A
A= $_[0]{'STATIC'}
Aresult
11
00
573  
574 =head2 ret_typemap
575  
576 Returns the typemap for the return value of the function.
577  
578 =head2 set_ret_typemap( typemap )
579  
580 Sets the typemap for the return value of the function.
581  
582 =head2 arg_typemap( index )
583  
584 Returns the typemap for one function arguments.
585  
586 =head2 set_arg_typemap( index, typemap )
587  
588 Sets the typemap for one function argument.
589  
590 =cut
591  
592 1 ▶sub ret_typemap {
Subroutine ret_typemap: called CC=2 SCAR=7.1
Pod documented
593 1 my ($this) = @_;
594  
595 1 ▶ die "Typemap not available yet" unless $this->{TYPEMAPS}{RET_TYPE};
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"TYPEMAPS"}{"RET_TYPE"} 0 1 1
596 1 return $this->{TYPEMAPS}{RET_TYPE};
597 }
598  
599 10 ▶sub set_ret_typemap {
Subroutine set_ret_typemap: called CC=1 SCAR=0.0
Pod documented
600 10 my ($this, $typemap) = @_;
601  
602 10 $this->{TYPEMAPS}{RET_TYPE} = $typemap;
603 }
604  
605 1 ▶sub arg_typemap {
Subroutine arg_typemap: called CC=3 SCAR=11.3
Pod documented
606 1 my ($this, $index) = @_;
607  
608 1 ▶ die "Invalid index" unless $index < @{$this->{ARGUMENTS}};
Branch: 1 branch
BranchTrueFalseTotal
unless $index < @{$this->{"ARGUMENTS"};} 0 1 1
609 1 ▶ die "Typemap not available yet" unless $this->{TYPEMAPS}{ARGUMENTS};
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"TYPEMAPS"}{"ARGUMENTS"} 0 1 1
610 1 return $this->{TYPEMAPS}{ARGUMENTS}[$index];
611 }
612  
613 10 ▶sub set_arg_typemap {
Subroutine set_arg_typemap: called CC=2 SCAR=7.0
Pod documented
614 10 my ($this, $index, $typemap) = @_;
615  
616 10 ▶ die "Invalid index" unless $index < @{$this->{ARGUMENTS}};
Branch: 1 branch
BranchTrueFalseTotal
unless $index < @{$this->{"ARGUMENTS"};} 0 10 10
617 10 $this->{TYPEMAPS}{ARGUMENTS}[$index] = $typemap;
618 }
619  
620 21 1;
« blib/lib/ExtUtils/XSpp/Grammar.pm ↑ Summary blib/lib/ExtUtils/XSpp/Driver.pm »