| 1 | # Represent any C type. | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # Type contains the size of arrays, which is either constant | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # or resolved (into an object) from resolveobj. | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | package # hide from PAUSE/MetaCPAN | ||||||||||||||||||||||||||||||||||||||||||
| 6 | PDL::PP::CType; | ||||||||||||||||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | # new PDL::PP::CType(resolveobj,str) | ||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 215 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 215 | my $this = bless {},shift; | |||||||||||||||||||||||||||||||||||||||||
| 15 | 215 | ▶ | $this->parsefrom(shift) if @_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 215 | return $this; | |||||||||||||||||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 367 | ▶ | sub stripptrs { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
stripptrs: called CC=6 SCAR=18.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 367 | my($this,$str) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 21 | 367 | ▶ | $this->{WasDollar} = 1 if $str =~ s/^\$//; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 367 | ▶ | if($str =~ s/^\s*(\w+)\s*$/$1/g) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 215 | $this->{ProtoName} = $str; | |||||||||||||||||||||||||||||||||||||||||
| 24 | 215 | return []; | |||||||||||||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||||||||||||||
| 26 | # Now, recall the different C syntaxes. First priority is a pointer: | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 152 | ▶ | return [["PTR"], @{$this->stripptrs($1)}] if $str =~ /^\s*\*(.*)$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 6 | ▶ | return $this->stripptrs($1) if $str =~ /^\s*\(.*\)\s*$/; # XXX Should try to see if a funccall. | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 6 | ▶ | return [["ARR",$2], @{$this->stripptrs($1)}] if $str =~ /^(.*)\[([^]]*)\]\s*$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | Carp::confess("Invalid C type '$str'"); | |||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | # XXX Correct to *real* parsing. This is only a subset. | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 215 | ▶ | sub parsefrom { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parsefrom: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 215 | my($this,$str) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 36 | # First, take the words in the beginning | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 215 | $str =~ /^\s*((?:\w+\b\s*)+)([^[].*)$/; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 215 | @$this{qw(Base Chain)} = ($1, $this->stripptrs($2)); | |||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 394 | ▶ | sub get_decl { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_decl: called CC=6 SCAR=18.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 394 | my($this,$name,$opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 394 | for(@{$this->{Chain}}) { | |||||||||||||||||||||||||||||||||||||||||
| 44 | 294 | my ($type, $arg) = @$_; | |||||||||||||||||||||||||||||||||||||||||
| 45 | 294 | ▶ | if($type eq "PTR") {$name = "*$name"} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | elsif($type eq "ARR") { | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 13 | ▶ | if($opts->{VarArrays2Ptrs}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 13 | $name = "*$name"; | |||||||||||||||||||||||||||||||||||||||||
| 49 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | $name = "($name)[$arg]"; | |||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | } else { confess("Invalid decl @$_") } | |||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 394 | ▶ | $name = "*$name" if $opts->{AddIndirect}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 394 | return "$this->{Base} $name"; | |||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | # Useful when parsing argument decls | ||||||||||||||||||||||||||||||||||||||||||
| 59 | 85 | ▶ | sub protoname { return shift->{ProtoName} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
protoname: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 34 | ▶ | sub get_copy { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_copy: called CC=10 SCAR=23.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 34 | my($this,$from,$to) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 34 | ▶ | return "($to) = ($from); /* CType.get_copy */" if !@{$this->{Chain}}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | # strdup loses portability :( | ||||||||||||||||||||||||||||||||||||||||||
| 65 | return "($to) = malloc(strlen($from)+1); strcpy($to,$from); /* CType.get_copy */" | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 7 | ▶ | if $this->{Base} =~ /^\s*char\s*$/ and @{$this->{Chain}} == 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$this->{"Base"} =~ /^\s*char\s*$/ and @{$this->{"Chain"};} == 1
MC/DC50%1/2
$this->{"Base"} =~ /^\s*char\s*$/ and @{$this->{"Chain"};} == 1
$this->{"Base"} =~ /^\s*char\s*$/
@{$this->{"Chain"};} == 1
Truth table66%2/3
A and B
A= $this->{"Base"} =~ /^\s*char\s*$/B= @{$this->{"Chain"};} == 1
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 5 | ▶ | return "($to) = newSVsv($from); /* CType.get_copy */" if $this->{Base} =~ /^\s*SV\s*$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 4 | my $code = $this->get_malloc($to,$from); | |||||||||||||||||||||||||||||||||||||||||
| 69 | 4 | ▶ | return "($to) = ($from); /* CType.get_copy */" if !defined $code; # pointer | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 1 | my ($deref0,$deref1,$prev,$close) = ($from,$to); | |||||||||||||||||||||||||||||||||||||||||
| 71 | 1 | my $no = 0; | |||||||||||||||||||||||||||||||||||||||||
| 72 | 1 | for(@{$this->{Chain}}) { | |||||||||||||||||||||||||||||||||||||||||
| 73 | 1 | my ($type, $arg) = @$_; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | if($type eq "PTR") {confess("Cannot copy pointer, must be array");} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | elsif($type eq "ARR") { | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 1 | $no++; | |||||||||||||||||||||||||||||||||||||||||
| 77 | 1 | ▶ | $arg = "$this->{ProtoName}_count" if $this->is_array; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 1 | $prev .= " | |||||||||||||||||||||||||||||||||||||||||
| 79 | if(!$deref0) {$deref1=0;} /* CType.get_copy */ | ||||||||||||||||||||||||||||||||||||||||||
| 80 | else {int __malloc_ind_$no; | ||||||||||||||||||||||||||||||||||||||||||
| 81 | for(__malloc_ind_$no = 0; | ||||||||||||||||||||||||||||||||||||||||||
| 82 | __malloc_ind_$no < $arg; | ||||||||||||||||||||||||||||||||||||||||||
| 83 | __malloc_ind_$no ++) {"; | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 1 | $deref0 .= "[__malloc_ind_$no]"; | |||||||||||||||||||||||||||||||||||||||||
| 85 | 1 | $deref1 .= "[__malloc_ind_$no]"; | |||||||||||||||||||||||||||||||||||||||||
| 86 | 1 | $close .= "}}"; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | } else { confess("Invalid decl @$_") } | |||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 1 | $code .= "$prev $deref1 = $deref0; $close"; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 1 | return $code; | |||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 34 | ▶ | sub get_free { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_free: called CC=9 SCAR=22.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 34 | my($this,$from) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 34 | ▶ | my $single_ptr = @{$this->{Chain}} == 1 && $this->{Chain}[0][0] eq 'PTR'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
@{$this->{'Chain'};} == 1 && $this->{'Chain'}[0][0] eq 'PTR'
MC/DC100%2/2
@{$this->{'Chain'};} == 1 && $this->{'Chain'}[0][0] eq 'PTR'
@{$this->{'Chain'};} == 1
$this->{'Chain'}[0][0] eq 'PTR'
Truth table100%3/3
A && B
A= @{$this->{'Chain'};} == 1B= $this->{'Chain'}[0][0] eq 'PTR'
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 34 | ▶ | return "SvREFCNT_dec($from); /* CType.get_free */\n" if $this->{Base} =~ /^\s*SV\s*$/ and $single_ptr; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$this->{"Base"} =~ /^\s*SV\s*$/ and $single_ptr
MC/DC50%1/2
$this->{"Base"} =~ /^\s*SV\s*$/ and $single_ptr
$this->{"Base"} =~ /^\s*SV\s*$/
$single_ptr
Truth table66%2/3
A and B
A= $this->{"Base"} =~ /^\s*SV\s*$/B= $single_ptr
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 33 | ▶ | return "free($from); /* CType.get_free */\n" if $this->{Base} =~ /^\s*char\s*$/ and $single_ptr; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$this->{"Base"} =~ /^\s*char\s*$/ and $single_ptr
MC/DC50%1/2
$this->{"Base"} =~ /^\s*char\s*$/ and $single_ptr
$this->{"Base"} =~ /^\s*char\s*$/
$single_ptr
Truth table66%2/3
A and B
A= $this->{"Base"} =~ /^\s*char\s*$/B= $single_ptr
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 31 | ▶ | return "" if !@{$this->{Chain}} or $this->{Chain}[0][0] eq 'PTR'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not @{$this->{"Chain"};} or $this->{"Chain"}[0][0] eq "PTR"
MC/DC100%2/2
not @{$this->{"Chain"};} or $this->{"Chain"}[0][0] eq "PTR"
not @{$this->{"Chain"};}
$this->{"Chain"}[0][0] eq "PTR"
Truth table100%3/3
not(A) or B
A= not @{$this->{"Chain"};}B= $this->{"Chain"}[0][0] eq "PTR"
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 1 | ▶ | croak("Can only free one layer!\n") if @{$this->{Chain}} > 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 1 | "free($from); /* CType.get_free */\n"; | |||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | sub need_malloc { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
need_malloc: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | my($this) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | grep /(ARR|PTR)/, map $_->[0], @{$this->{Chain}}; | |||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | # returns with the array string - undef if a pointer not needing malloc | ||||||||||||||||||||||||||||||||||||||||||
| 109 | 4 | ▶ | sub get_malloc { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_malloc: called CC=5 SCAR=16.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 4 | my($this,$assignto) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 111 | 4 | my $str = ""; | |||||||||||||||||||||||||||||||||||||||||
| 112 | 4 | for(@{$this->{Chain}}) { | |||||||||||||||||||||||||||||||||||||||||
| 113 | 4 | my ($type, $arg) = @$_; | |||||||||||||||||||||||||||||||||||||||||
| 114 | 4 | ▶ | if($type eq "PTR") {return} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | elsif($type eq "ARR") { | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 1 | ▶ | $arg = "$this->{ProtoName}_count" if $this->is_array; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 1 | $str .= "$assignto = malloc(sizeof(*$assignto) * $arg); /* CType.get_malloc */\n"; | |||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | } else { confess("Invalid decl (@$_)") } | |||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | 1 | return $str; | |||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | 537 | ▶ | sub is_array { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_array: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 537 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 125 | 537 | @{$self->{Chain}} && | |||||||||||||||||||||||||||||||||||||||||
| 126 | 131 | @{$self->{Chain}[-1]} && | |||||||||||||||||||||||||||||||||||||||||
| 127 | $self->{Chain}[-1][0] eq 'ARR' && | ||||||||||||||||||||||||||||||||||||||||||
| 128 | 537 | ▶ | !$self->{Chain}[-1][1]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
@{$self->{"Chain"};} and @{$self->{"Chain"}[-1];} and $self->{"Chain"}[-1][0] eq "ARR"
@{$self->{"Chain"};} and @{$self->{"Chain"}[-1];}
MC/DC66%2/3
@{$self->{"Chain"};} and @{$self->{"Chain"}[-1];} and $self->{"Chain"}[-1][0] eq "ARR"
@{$self->{"Chain"};}
@{$self->{"Chain"}[-1];}
$self->{"Chain"}[-1][0] eq "ARR"
Truth table75%3/4
A and B and C
A= @{$self->{"Chain"};}B= @{$self->{"Chain"}[-1];}C= $self->{"Chain"}[-1][0] eq "ARR"
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||