| 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 C::Type; | ||||||||||||||||||
| 6 | 2 | ▶ | use Carp; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 7 | |||||||||||||||||||
| 8 | # new C::Type(resolveobj,str) | ||||||||||||||||||
| 9 | |||||||||||||||||||
| 10 | sub new { | ||||||||||||||||||
| 11 | 7 | ▶ | my $this = bless {},shift; | ||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
| |||||||||||||||||||
| 12 | 7 | $this->{Resolve} = shift; | |||||||||||||||||
| 13 | 7 | ▶ | if(@_) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 14 | 7 | $this->parsefrom(shift); | |||||||||||||||||
| 15 | } | ||||||||||||||||||
| 16 | 7 | return $this; | |||||||||||||||||
| 17 | } | ||||||||||||||||||
| 18 | |||||||||||||||||||
| 19 | sub stripptrs { | ||||||||||||||||||
| 20 | 11 | ▶ | my($this,$str) = @_; | ||||||||||||||||
|
Subroutine
stripptrs: called CC=7 SCAR=27.2
| |||||||||||||||||||
| 21 | 11 | ▶ | if($str =~ /^\s*\w+\s*$/) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 22 | 7 | $str =~ s/\s//g; | |||||||||||||||||
| 23 | 7 | $this->{ProtoName} = $str; | |||||||||||||||||
| 24 | 7 | return []; | |||||||||||||||||
| 25 | } else { | ||||||||||||||||||
| 26 | # Now, recall the different C syntaxes. First priority is a pointer: | ||||||||||||||||||
| 27 | 4 | my $decl; | |||||||||||||||||
| 28 | 4 | ▶ | if($str =~ /^\s*\*(.*)$/) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 29 | 4 | $decl = $this->stripptrs($1); | |||||||||||||||||
| 30 | 4 | unshift @$decl,"PTR"; | |||||||||||||||||
| 31 | } elsif($str =~ /^\s*\(.*\)\s*$/) { | ||||||||||||||||||
| 32 | # XXX Should try to see if a funccall. | ||||||||||||||||||
| 33 | 0 | return $this->stripptrs($1); | |||||||||||||||||
| 34 | } elsif($str =~ /^(.*)\[([^]]+)\]\s*$/) { | ||||||||||||||||||
| 35 | 0 | my $siz = $2; | |||||||||||||||||
| 36 | 0 | ▶ | print "ARR($str): ($siz)\n" if $::PP_VERBOSE; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 37 | 0 | $decl = $this->stripptrs($1); | |||||||||||||||||
| 38 | 0 | unshift @$decl,"ARR($siz)"; | |||||||||||||||||
| 39 | 0 | ▶ | print "ARR($str): ($siz)\n" if $::PP_VERBOSE; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 40 | } else { | ||||||||||||||||||
| 41 | 0 | die("Invalid C type '$str'"); | |||||||||||||||||
| 42 | } | ||||||||||||||||||
| 43 | 4 | return $decl; | |||||||||||||||||
| 44 | } | ||||||||||||||||||
| 45 | } | ||||||||||||||||||
| 46 | |||||||||||||||||||
| 47 | # XXX Correct to *real* parsing. This is only a subset. | ||||||||||||||||||
| 48 | sub parsefrom { | ||||||||||||||||||
| 49 | 7 | ▶ | my($this,$str) = @_; | ||||||||||||||||
|
Subroutine
parsefrom: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 50 | # First, take the words in the beginning | ||||||||||||||||||
| 51 | 7 | $str =~ /^\s*((?:\w+\b\s*)+)([^[].*)$/; | |||||||||||||||||
| 52 | 7 | my $base = $1; my $decl = $2; | |||||||||||||||||
| 53 | 7 | my $foo = $this->stripptrs($decl); | |||||||||||||||||
| 54 | 7 | $this->{Base} = $base; | |||||||||||||||||
| 55 | 7 | $this->{Chain} = $foo; | |||||||||||||||||
| 56 | } | ||||||||||||||||||
| 57 | |||||||||||||||||||
| 58 | sub get_decl { | ||||||||||||||||||
| 59 | 9 | ▶ | my($this,$name,$opts) = @_; | ||||||||||||||||
|
Subroutine
get_decl: called CC=5 SCAR=22.5
| |||||||||||||||||||
| 60 | 9 | for(@{$this->{Chain}}) { | |||||||||||||||||
| 61 | 6 | ▶ | if($_ eq "PTR") {$name = "*$name"} | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 62 | elsif($_ =~/^ARR\((.*)\)$/) { | ||||||||||||||||||
| 63 | 0 | ▶ | if($opts->{VarArrays2Ptrs}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 64 | 0 | $name = "*$name"; | |||||||||||||||||
| 65 | } else { | ||||||||||||||||||
| 66 | 0 | $name = "($name)[$1]"; | |||||||||||||||||
| 67 | } | ||||||||||||||||||
| 68 | } | ||||||||||||||||||
| 69 | 0 | else { confess("Invalid decl") } | |||||||||||||||||
| 70 | } | ||||||||||||||||||
| 71 | 9 | return "$this->{Base} $name"; | |||||||||||||||||
| 72 | } | ||||||||||||||||||
| 73 | |||||||||||||||||||
| 74 | # Useful when parsing argument decls | ||||||||||||||||||
| 75 | 3 | ▶ | sub protoname { return shift->{ProtoName} } | ||||||||||||||||
|
Subroutine
protoname: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 76 | |||||||||||||||||||
| 77 | sub get_copy { | ||||||||||||||||||
| 78 | 0 | ▶ | my($this,$from,$to) = @_; | ||||||||||||||||
|
Subroutine
get_copy: not called CC=7 SCAR=40.3
| |||||||||||||||||||
| 79 | 0 | my ($prev,$close); | |||||||||||||||||
| 80 | 0 | ▶ | if($#{$this->{Chain}} >= 0) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 81 | # strdup loses portability :( | ||||||||||||||||||
| 82 | return "($to) = malloc(strlen($from)+1); strcpy($to,$from);" | ||||||||||||||||||
| 83 | 0 | ▶ | if $this->{Base} =~ /^\s*char\s*$/; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 84 | return "($to) = newSVsv($from);" | ||||||||||||||||||
| 85 | 0 | ▶ | if $this->{Base} =~ /^\s*SV\s*$/; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 86 | 0 | my $code = $this->get_malloc($to,$from); | |||||||||||||||||
| 87 | 0 | my ($deref0,$deref1) = ($from,$to); | |||||||||||||||||
| 88 | 0 | for(@{$this->{Chain}}) { | |||||||||||||||||
| 89 | 0 | ▶ | if($_ eq "PTR") {confess("Cannot alloc pointer, must be array");} | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 90 | elsif($_ =~/^ARR\((.*)\)$/) { | ||||||||||||||||||
| 91 | 0 | $no++; | |||||||||||||||||
| 92 | 0 | $prev .= " | |||||||||||||||||
| 93 | if(!$deref0) {$deref1=0;} | ||||||||||||||||||
| 94 | else {int __malloc_ind_$no; | ||||||||||||||||||
| 95 | for(__malloc_ind_$no = 0; | ||||||||||||||||||
| 96 | __malloc_ind_$no < $1; | ||||||||||||||||||
| 97 | __malloc_ind_$no ++) {"; | ||||||||||||||||||
| 98 | 0 | $deref0 = $deref0."[__malloc_ind_$no]"; | |||||||||||||||||
| 99 | 0 | $deref1 = $deref1."[__malloc_ind_$no]"; | |||||||||||||||||
| 100 | 0 | $close .= "}}"; | |||||||||||||||||
| 101 | 0 | } else { confess("Invalid decl $_") } | |||||||||||||||||
| 102 | } | ||||||||||||||||||
| 103 | 0 | $code .= "$prev $deref1 = $deref0; $close"; | |||||||||||||||||
| 104 | 0 | return $code; | |||||||||||||||||
| 105 | } | ||||||||||||||||||
| 106 | 0 | return "($to) = ($from);"; | |||||||||||||||||
| 107 | } | ||||||||||||||||||
| 108 | |||||||||||||||||||
| 109 | sub get_free { | ||||||||||||||||||
| 110 | 3 | ▶ | my($this,$from) = @_; | ||||||||||||||||
|
Subroutine
get_free: called CC=6 SCAR=31.3
| |||||||||||||||||||
| 111 | 3 | my ($prev,$close); | |||||||||||||||||
| 112 | 3 | ▶ | if($#{$this->{Chain}} >= 0) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 113 | return "free($from);" | ||||||||||||||||||
| 114 | 0 | ▶ | if $this->{Base} =~ /^\s*char\s*$/; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 115 | return "SvREFCNT_dec($from);" | ||||||||||||||||||
| 116 | 0 | ▶ | if $this->{Base} =~ /^\s*SV\s*$/; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 117 | 0 | my @mallocs; | |||||||||||||||||
| 118 | 0 | my $str = "{"; | |||||||||||||||||
| 119 | 0 | my $deref = "$from"; | |||||||||||||||||
| 120 | 0 | my $prev = undef; | |||||||||||||||||
| 121 | 0 | my $close = undef; | |||||||||||||||||
| 122 | 0 | my $no = 0; | |||||||||||||||||
| 123 | 0 | for(@{$this->{Chain}}) { | |||||||||||||||||
| 124 | 0 | $no++; | |||||||||||||||||
| 125 | 0 | ▶ | if($no > 1) {croak("Can only free one layer!\n");} | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 126 | # if($_ eq "PTR") {confess("Cannot free pointer, must be array ;) (FIX CType.pm)");} | ||||||||||||||||||
| 127 | 0 | return "free($from);\n "; | |||||||||||||||||
| 128 | } | ||||||||||||||||||
| 129 | } else { | ||||||||||||||||||
| 130 | 3 | ""; | |||||||||||||||||
| 131 | } | ||||||||||||||||||
| 132 | } | ||||||||||||||||||
| 133 | |||||||||||||||||||
| 134 | sub need_malloc { | ||||||||||||||||||
| 135 | 0 | ▶ | my($this) = @_; | ||||||||||||||||
|
Subroutine
need_malloc: not called CC=1 SCAR=6.9
| |||||||||||||||||||
| 136 | 0 | return scalar grep /(ARR|PTR)/,(@{$this->{Chain}}) | |||||||||||||||||
| 137 | } | ||||||||||||||||||
| 138 | |||||||||||||||||||
| 139 | # Just returns with the array string. | ||||||||||||||||||
| 140 | sub get_malloc { | ||||||||||||||||||
| 141 | 0 | ▶ | my($this,$assignto) = @_; | ||||||||||||||||
|
Subroutine
get_malloc: not called CC=4 SCAR=30.0
| |||||||||||||||||||
| 142 | 0 | my $str = "{"; | |||||||||||||||||
| 143 | 0 | my $deref = "$assignto"; | |||||||||||||||||
| 144 | 0 | my $prev = undef; | |||||||||||||||||
| 145 | 0 | my $close = undef; | |||||||||||||||||
| 146 | 0 | my $no = 0; | |||||||||||||||||
| 147 | 0 | for(@{$this->{Chain}}) { | |||||||||||||||||
| 148 | 0 | ▶ | if($_ eq "PTR") {confess("Cannot alloc pointer, must be array");} | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 149 | elsif($_ =~/^ARR\((.*)\)$/) { | ||||||||||||||||||
| 150 | 0 | $str .= "$prev $assignto = | |||||||||||||||||
| 151 | malloc(sizeof(* $assignto) * $1); | ||||||||||||||||||
| 152 | "; | ||||||||||||||||||
| 153 | 0 | $no++; | |||||||||||||||||
| 154 | 0 | $prev = "{int __malloc_ind_$no; | |||||||||||||||||
| 155 | for(__malloc_ind_$no = 0; | ||||||||||||||||||
| 156 | __malloc_ind_$no < $1; | ||||||||||||||||||
| 157 | __malloc_ind_$no ++) {"; | ||||||||||||||||||
| 158 | 0 | $deref = $deref."[__malloc_ind_$no]"; | |||||||||||||||||
| 159 | 0 | $close .= "}}"; | |||||||||||||||||
| 160 | 0 | } else { confess("Invalid decl $_") } | |||||||||||||||||
| 161 | } | ||||||||||||||||||
| 162 | 0 | $str .= "}"; | |||||||||||||||||
| 163 | 0 | return $str; | |||||||||||||||||
| 164 | } | ||||||||||||||||||
| 165 | |||||||||||||||||||
| 166 | ▶ | sub getvar { | |||||||||||||||||
|
Subroutine
getvar: not called CC=1 SCAR=0.0
| |||||||||||||||||||
| 167 | } | ||||||||||||||||||
| 168 | |||||||||||||||||||
| 169 | # Determine if everything constant and can just declare | ||||||||||||||||||
| 170 | ▶ | sub need_alloc { | |||||||||||||||||
|
Subroutine
need_alloc: not called CC=1 SCAR=0.0
| |||||||||||||||||||
| 171 | } | ||||||||||||||||||
| 172 | |||||||||||||||||||
| 173 | ▶ | sub alloccode { | |||||||||||||||||
|
Subroutine
alloccode: not called CC=1 SCAR=0.0
| |||||||||||||||||||
| 174 | } | ||||||||||||||||||
| 175 | |||||||||||||||||||
| 176 | ▶ | sub copycode { | |||||||||||||||||
|
Subroutine
copycode: not called CC=1 SCAR=0.0
| |||||||||||||||||||
| 177 | } | ||||||||||||||||||
| 178 | |||||||||||||||||||
| 179 | ▶ | sub freecode { | |||||||||||||||||
|
Subroutine
freecode: not called CC=1 SCAR=0.0
| |||||||||||||||||||
| 180 | } | ||||||||||||||||||
| 181 | |||||||||||||||||||
| 182 | 2 | 1; | |||||||||||||||||