| 1 | package FFI::TinyCC::Inline; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use 5.010; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use FFI::Platypus; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use FFI::TinyCC; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use base qw( Exporter ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 5 | our @EXPORT_OK = qw( tcc_inline tcc_eval ); | |||||||||||||||||||||||||||||||||||||||||
| 12 | 5 | our @EXPORT = @EXPORT_OK; | |||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | # ABSTRACT: Embed Tiny C code in your Perl program | ||||||||||||||||||||||||||||||||||||||||||
| 15 | 5 | our $VERSION = '0.28'; # VERSION | |||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 5 | my $ffi = FFI::Platypus->new; | |||||||||||||||||||||||||||||||||||||||||
| 19 | 5 | $ffi->load_custom_type( 'StringArray' => 'string_array' ); | |||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | # TODO: support platypus types like pointers and arrays | ||||||||||||||||||||||||||||||||||||||||||
| 22 | 5 | my %typemap = ( | |||||||||||||||||||||||||||||||||||||||||
| 23 | 'int' => 'int', | ||||||||||||||||||||||||||||||||||||||||||
| 24 | 'signed int' => 'signed int', | ||||||||||||||||||||||||||||||||||||||||||
| 25 | 'unsigned int' => 'unsigned int', | ||||||||||||||||||||||||||||||||||||||||||
| 26 | 'void' => 'void', | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 'short' => 'short', | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 'signed short' => 'signed short', | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 'unsigned short' => 'unsigned short', | ||||||||||||||||||||||||||||||||||||||||||
| 30 | 'long' => 'long', | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 'signed long' => 'signed long', | ||||||||||||||||||||||||||||||||||||||||||
| 32 | 'unsigned long' => 'unsigned long', | ||||||||||||||||||||||||||||||||||||||||||
| 33 | 'char' => 'char', | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 'signed char' => 'signed char', | ||||||||||||||||||||||||||||||||||||||||||
| 35 | 'unsigned char' => 'unsigned char', | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 'float' => 'float', | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 'double' => 'double', | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 'char *' => 'string', | ||||||||||||||||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | sub _typemap ($) | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 31 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_typemap: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 31 | my($type) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 44 | 31 | $type =~ s{^const }{}; | |||||||||||||||||||||||||||||||||||||||||
| 45 | return $typemap{$type} | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 31 | ▶ | if defined $typemap{$type}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 2 | ▶ | return 'opaque' if $type =~ /\*$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | croak "unknown type: $type"; | |||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | sub _generate_sub ($$$) | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 27 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_generate_sub: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 27 | my($func_name, $func, $tcc) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 27 | my $sub; | |||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 27 | my $address = $tcc->get_symbol($func_name); | |||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 27 | ▶ | if(@{ $func->{arg_types} } == 2 | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
@{$func->{"arg_types"};} == 2 and $func->{"arg_types"}[0] eq "int" and $func->{"arg_types"}[1] =~ /^(const |)char \*\*$/
@{$func->{"arg_types"};} == 2 and $func->{"arg_types"}[0] eq "int"
MC/DC33%1/3
@{$func->{"arg_types"};} == 2 and $func->{"arg_types"}[0] eq "int" and $func->{"arg_types"}[1] =~ /^(const |)char \*\*$/
@{$func->{"arg_types"};} == 2
$func->{"arg_types"}[0] eq "int"
$func->{"arg_types"}[1] =~ /^(const |)char \*\*$/
Truth table50%2/4
A and B and C
A= @{$func->{"arg_types"};} == 2B= $func->{"arg_types"}[0] eq "int"C= $func->{"arg_types"}[1] =~ /^(const |)char \*\*$/
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | && $func->{arg_types}->[0] eq 'int' | ||||||||||||||||||||||||||||||||||||||||||
| 60 | && $func->{arg_types}->[1] =~ /^(const |)char \*\*$/) | ||||||||||||||||||||||||||||||||||||||||||
| 61 | { | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 1 | my $f = $ffi->function($address => ['int','string_array'] => _typemap $func->{return_type}); | |||||||||||||||||||||||||||||||||||||||||
| 63 | 1 | ▶ | $sub = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 1 | $f->call(scalar @_, \@_); | |||||||||||||||||||||||||||||||||||||||||
| 65 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | else | ||||||||||||||||||||||||||||||||||||||||||
| 68 | { | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 26 | my $f = $ffi->function($address => [map { _typemap $_ } @{ $func->{arg_types} }] => _typemap $func->{return_type}); | |||||||||||||||||||||||||||||||||||||||||
| 70 | 26 | ▶ | $sub = sub { $f->call(@_) }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 27 | $sub; | |||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | sub import | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 10 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=6 SCAR=18.3
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 10 | my($class, @rest) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 10 | ▶ | if(defined $rest[0] && defined $rest[1] | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
defined $rest[0] and defined $rest[1] and $rest[0] eq "options"
defined $rest[0] and defined $rest[1]
MC/DC33%1/3
defined $rest[0] and defined $rest[1] and $rest[0] eq "options"
defined $rest[0]
defined $rest[1]
$rest[0] eq "options"
Truth table50%2/4
A and B and C
A= defined $rest[0]B= defined $rest[1]C= $rest[0] eq "options"
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | && $rest[0] eq 'options') | ||||||||||||||||||||||||||||||||||||||||||
| 83 | { | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 6 | ▶ | if($] >= 5.010) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | { | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 6 | shift @rest; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 6 | $^H{"FFI::TinyCC::Inline/options"} = shift @rest; | |||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | else | ||||||||||||||||||||||||||||||||||||||||||
| 90 | { | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | croak "options not supported on Perl 5.8"; | |||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 10 | ▶ | return unless @rest > 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 4 | @_ = ($class, @rest); | |||||||||||||||||||||||||||||||||||||||||
| 98 | 4 | goto &Exporter::import; | |||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | sub tcc_inline ($) | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 7 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tcc_inline: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 7 | my($code) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 105 | 7 | my $caller = caller; | |||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 7 | my $tcc = FFI::TinyCC->new(_no_free_store => 1); | |||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 7 | my $h = (caller(0))[10]; | |||||||||||||||||||||||||||||||||||||||||
| 110 | 7 | ▶ | if($h->{"FFI::TinyCC::Inline/options"}) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 5 | { $tcc->set_options($h->{"FFI::TinyCC::Inline/options"}) } | |||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | 7 | $tcc->compile_string($code); | |||||||||||||||||||||||||||||||||||||||||
| 114 | 6 | my $meta = FFI::TinyCC::Parser->extract_function_metadata($code); | |||||||||||||||||||||||||||||||||||||||||
| 115 | 6 | foreach my $func_name (keys %{ $meta->{functions} }) | |||||||||||||||||||||||||||||||||||||||||
| 116 | { | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 20 | my $sub = _generate_sub($func_name, $meta->{functions}->{$func_name}, $tcc); | |||||||||||||||||||||||||||||||||||||||||
| 118 | 5 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 20 | *{join '::', $caller, $func_name} = $sub; | |||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||
| 121 | 6 | (); | |||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | sub tcc_eval ($;@) | ||||||||||||||||||||||||||||||||||||||||||
| 126 | 8 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tcc_eval: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 8 | my($code, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 128 | 8 | my $tcc = FFI::TinyCC->new; | |||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | 8 | my $h = (caller(0))[10]; | |||||||||||||||||||||||||||||||||||||||||
| 131 | 8 | ▶ | if($h->{"FFI::TinyCC::Inline/options"}) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 5 | { $tcc->set_options($h->{"FFI::TinyCC::Inline/options"}) } | |||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | 8 | $tcc->compile_string($code); | |||||||||||||||||||||||||||||||||||||||||
| 135 | 7 | my $meta = FFI::TinyCC::Parser->extract_function_metadata($code); | |||||||||||||||||||||||||||||||||||||||||
| 136 | 7 | my $func = $meta->{functions}->{main}; | |||||||||||||||||||||||||||||||||||||||||
| 137 | 7 | ▶ | croak "no main function" unless defined $func; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 7 | my $sub = _generate_sub('main', $meta->{functions}->{main}, $tcc); | |||||||||||||||||||||||||||||||||||||||||
| 139 | 7 | $sub->(@args); | |||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | package | ||||||||||||||||||||||||||||||||||||||||||
| 143 | FFI::TinyCC::Parser; | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | # this parser code stolen shamelessly | ||||||||||||||||||||||||||||||||||||||||||
| 146 | # from XS::TCC, which I strongly suspect | ||||||||||||||||||||||||||||||||||||||||||
| 147 | # was itself shamelessly "borrowed" | ||||||||||||||||||||||||||||||||||||||||||
| 148 | # from Inline::C::Parser::RegExp | ||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | # Copyright 2002 Brian Ingerson | ||||||||||||||||||||||||||||||||||||||||||
| 151 | # Copyright 2008, 2010-2012 Sisyphus | ||||||||||||||||||||||||||||||||||||||||||
| 152 | # Copyright 2013 Steffen Muellero | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | # This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | # These regular expressions were derived from Regexp::Common v0.01. | ||||||||||||||||||||||||||||||||||||||||||
| 160 | 5 | my $RE_comment_C = q{(?:(?:\/\*)(?:(?:(?!\*\/)[\s\S])*)(?:\*\/))}; | |||||||||||||||||||||||||||||||||||||||||
| 161 | 5 | my $RE_comment_Cpp = q{(?:\/\*(?:(?!\*\/)[\s\S])*\*\/|\/\/[^\n]*\n)}; | |||||||||||||||||||||||||||||||||||||||||
| 162 | 5 | my $RE_quoted = (q{(?:(?:\")(?:[^\\\"]*(?:\\.[^\\\"]*)*)(?:\")} | |||||||||||||||||||||||||||||||||||||||||
| 163 | .q{|(?:\')(?:[^\\\']*(?:\\.[^\\\']*)*)(?:\'))}); | ||||||||||||||||||||||||||||||||||||||||||
| 164 | 5 | my $RE_balanced_brackets; | |||||||||||||||||||||||||||||||||||||||||
| 165 | $RE_balanced_brackets = | ||||||||||||||||||||||||||||||||||||||||||
| 166 | 5 | qr'(?:[{]((?:(?>[^{}]+)|(??{$RE_balanced_brackets}))*)[}])'; | |||||||||||||||||||||||||||||||||||||||||
| 167 | 5 | my $RE_balanced_parens; | |||||||||||||||||||||||||||||||||||||||||
| 168 | $RE_balanced_parens = | ||||||||||||||||||||||||||||||||||||||||||
| 169 | 5 | qr'(?:[(]((?:(?>[^()]+)|(??{$RE_balanced_parens}))*)[)])'; | |||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||
| 172 | 33 | ▶ | sub _normalize_type { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_normalize_type: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | # Normalize a type for lookup in a typemap. | ||||||||||||||||||||||||||||||||||||||||||
| 174 | 33 | my($type) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | # Remove "extern". | ||||||||||||||||||||||||||||||||||||||||||
| 177 | # But keep "static", "inline", "typedef", etc, | ||||||||||||||||||||||||||||||||||||||||||
| 178 | # to cause desirable typemap misses. | ||||||||||||||||||||||||||||||||||||||||||
| 179 | 33 | $type =~ s/\bextern\b//g; | |||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | # Whitespace: only single spaces, none leading or trailing. | ||||||||||||||||||||||||||||||||||||||||||
| 182 | 33 | $type =~ s/\s+/ /g; | |||||||||||||||||||||||||||||||||||||||||
| 183 | 33 | $type =~ s/^\s//; $type =~ s/\s$//; | |||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | # Adjacent "derivative characters" are not separated by whitespace, | ||||||||||||||||||||||||||||||||||||||||||
| 186 | # but _are_ separated from the adjoining text. | ||||||||||||||||||||||||||||||||||||||||||
| 187 | # [ Is really only * (and not ()[]) needed??? ] | ||||||||||||||||||||||||||||||||||||||||||
| 188 | 33 | $type =~ s/\*\s\*/\*\*/g; | |||||||||||||||||||||||||||||||||||||||||
| 189 | 33 | $type =~ s/(?<=[^ \*])\*/ \*/g; | |||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | 33 | return $type; | |||||||||||||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | 13 | ▶ | sub extract_function_metadata { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
extract_function_metadata: called CC=16 SCAR=32.1
| |||||||||||||||||||||||||||||||||||||||||||
| 195 | 13 | my (undef, $code) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | 13 | my $results = { | |||||||||||||||||||||||||||||||||||||||||
| 198 | function_names => [], | ||||||||||||||||||||||||||||||||||||||||||
| 199 | functions => {}, | ||||||||||||||||||||||||||||||||||||||||||
| 200 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | # First, we crush out anything potentially confusing. | ||||||||||||||||||||||||||||||||||||||||||
| 203 | # The order of these _does_ matter. | ||||||||||||||||||||||||||||||||||||||||||
| 204 | 13 | $code =~ s/$RE_comment_C/ /go; | |||||||||||||||||||||||||||||||||||||||||
| 205 | 13 | $code =~ s/$RE_comment_Cpp/ /go; | |||||||||||||||||||||||||||||||||||||||||
| 206 | 13 | $code =~ s/^\#.*(\\\n.*)*//mgo; | |||||||||||||||||||||||||||||||||||||||||
| 207 | #$code =~ s/$RE_quoted/\"\"/go; # Buggy, if included. | ||||||||||||||||||||||||||||||||||||||||||
| 208 | 13 | $code =~ s/$RE_balanced_brackets/{ }/go; | |||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | # The decision of what is an acceptable declaration was originally | ||||||||||||||||||||||||||||||||||||||||||
| 211 | # derived from Inline::C::grammar.pm version 0.30 (Inline 0.43). | ||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||
| 213 | 13 | my $re_plausible_place_to_begin_a_declaration = qr { | |||||||||||||||||||||||||||||||||||||||||
| 214 | # The beginning of a line, possibly indented. | ||||||||||||||||||||||||||||||||||||||||||
| 215 | # (Accepting indentation allows for C code to be aligned with | ||||||||||||||||||||||||||||||||||||||||||
| 216 | # its surrounding perl, and for backwards compatibility with | ||||||||||||||||||||||||||||||||||||||||||
| 217 | # Inline 0.43). | ||||||||||||||||||||||||||||||||||||||||||
| 218 | (?m: ^ ) \s* | ||||||||||||||||||||||||||||||||||||||||||
| 219 | }xo; | ||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||
| 221 | # Instead of using \s , we dont tolerate blank lines. | ||||||||||||||||||||||||||||||||||||||||||
| 222 | # This matches user expectation better than allowing arbitrary | ||||||||||||||||||||||||||||||||||||||||||
| 223 | # vertical whitespace. | ||||||||||||||||||||||||||||||||||||||||||
| 224 | 13 | my $sp = qr{[ \t]|\n(?![ \t]*\n)}; | |||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | 13 | my $re_type = qr {( | |||||||||||||||||||||||||||||||||||||||||
| 227 | (?: \w+ $sp* )+? # words | ||||||||||||||||||||||||||||||||||||||||||
| 228 | (?: \* $sp* )* # stars | ||||||||||||||||||||||||||||||||||||||||||
| 229 | )}xo; | ||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||
| 231 | 13 | my $re_identifier = qr{ (\w+) $sp* }xo; | |||||||||||||||||||||||||||||||||||||||||
| 232 | 13 | ▶ | while( $code =~ m{ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | $re_plausible_place_to_begin_a_declaration | ||||||||||||||||||||||||||||||||||||||||||
| 234 | ( $re_type $re_identifier $RE_balanced_parens $sp* (\;|\{) ) | ||||||||||||||||||||||||||||||||||||||||||
| 235 | }xgo) | ||||||||||||||||||||||||||||||||||||||||||
| 236 | { | ||||||||||||||||||||||||||||||||||||||||||
| 237 | 27 | my($type, $identifier, $args, $what) = ($2,$3,$4,$5); | |||||||||||||||||||||||||||||||||||||||||
| 238 | 27 | ▶ | $args = "" if $args =~ /^\s+$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | 27 | my $need_threading_context = 0; | |||||||||||||||||||||||||||||||||||||||||
| 241 | 27 | my $is_decl = $what eq ';'; | |||||||||||||||||||||||||||||||||||||||||
| 242 | 27 | my $function = $identifier; | |||||||||||||||||||||||||||||||||||||||||
| 243 | 27 | my $return_type = _normalize_type($type); | |||||||||||||||||||||||||||||||||||||||||
| 244 | 27 | my @arguments = split ',', $args; | |||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||
| 246 | #goto RESYNC if $is_decl && !$self->{data}{AUTOWRAP}; | ||||||||||||||||||||||||||||||||||||||||||
| 247 | 27 | ▶ | goto RESYNC if exists $results->{functions}{$function}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 248 | #goto RESYNC if !defined $self->{data}{typeconv}{valid_rtypes}{$return_type}; | ||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||
| 250 | 27 | my(@arg_names,@arg_types); | |||||||||||||||||||||||||||||||||||||||||
| 251 | 27 | my $dummy_name = 'arg1'; | |||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | 27 | my $argno = 0; | |||||||||||||||||||||||||||||||||||||||||
| 254 | 27 | foreach my $arg (@arguments) { | |||||||||||||||||||||||||||||||||||||||||
| 255 | # recognize threading context passing as part of first arg | ||||||||||||||||||||||||||||||||||||||||||
| 256 | 6 | ▶ | if ($argno++ == 0 and $arg =~ s/^\s*pTHX_?\s*//) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$argno++ == 0 and $arg =~ s/^\s*pTHX_?\s*//
MC/DC0%0/2
$argno++ == 0 and $arg =~ s/^\s*pTHX_?\s*//
$argno++ == 0
$arg =~ s/^\s*pTHX_?\s*//
Truth table66%2/3
A and B
A= $argno++ == 0B= $arg =~ s/^\s*pTHX_?\s*//
| |||||||||||||||||||||||||||||||||||||||||||
| 257 | 0 | $need_threading_context = 1; | |||||||||||||||||||||||||||||||||||||||||
| 258 | 0 | ▶ | next if $arg !~ /\S/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||
| 261 | 6 | my $arg_no_space = $arg; | |||||||||||||||||||||||||||||||||||||||||
| 262 | 6 | $arg_no_space =~ s/\s+//g; | |||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||
| 264 | # If $arg_no_space is 'void', there will be no identifier. | ||||||||||||||||||||||||||||||||||||||||||
| 265 | 6 | ▶ | if( my($type, $identifier) = | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 266 | $arg =~ /^\s*$re_type(?:$re_identifier)?\s*$/o ) | ||||||||||||||||||||||||||||||||||||||||||
| 267 | { | ||||||||||||||||||||||||||||||||||||||||||
| 268 | 6 | my $arg_name = $identifier; | |||||||||||||||||||||||||||||||||||||||||
| 269 | 6 | my $arg_type = _normalize_type($type); | |||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | 6 | ▶ | if((!defined $arg_name) && ($arg_no_space ne 'void')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $arg_name and $arg_no_space ne "void"
MC/DC0%0/2
not defined $arg_name and $arg_no_space ne "void"
not defined $arg_name
$arg_no_space ne "void"
Truth table33%1/3
not(A) and B
A= not defined $arg_nameB= $arg_no_space ne "void"
| |||||||||||||||||||||||||||||||||||||||||||
| 272 | 0 | ▶ | goto RESYNC if !$is_decl; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 273 | 0 | $arg_name = $dummy_name++; | |||||||||||||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||||||||||||
| 275 | #goto RESYNC if ((!defined | ||||||||||||||||||||||||||||||||||||||||||
| 276 | # $self->{data}{typeconv}{valid_types}{$arg_type}) && ($arg_no_space ne 'void')); | ||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||
| 278 | # Push $arg_name onto @arg_names iff it's defined. Otherwise ($arg_no_space | ||||||||||||||||||||||||||||||||||||||||||
| 279 | # was 'void'), push the empty string onto @arg_names (to avoid uninitialized | ||||||||||||||||||||||||||||||||||||||||||
| 280 | # warnings emanating from C.pm). | ||||||||||||||||||||||||||||||||||||||||||
| 281 | 6 | ▶ | defined($arg_name) ? push(@arg_names,$arg_name) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 282 | : push(@arg_names, ''); | ||||||||||||||||||||||||||||||||||||||||||
| 283 | 6 | ▶ | if($arg_name) {push(@arg_types,$arg_type)} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | else {push(@arg_types,'')} # $arg_no_space was 'void' - this push() avoids 'uninitialized' warnings from C.pm | |||||||||||||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||||||||||||
| 286 | elsif($arg =~ /^\s*\.\.\.\s*$/) { | ||||||||||||||||||||||||||||||||||||||||||
| 287 | 0 | push(@arg_names,'...'); | |||||||||||||||||||||||||||||||||||||||||
| 288 | 0 | push(@arg_types,'...'); | |||||||||||||||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||||||||||||||||
| 290 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 291 | 0 | goto RESYNC; | |||||||||||||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | # Commit. | ||||||||||||||||||||||||||||||||||||||||||
| 296 | 27 | push @{$results->{function_names}}, $function; | |||||||||||||||||||||||||||||||||||||||||
| 297 | 27 | $results->{functions}{$function}{return_type}= $return_type; | |||||||||||||||||||||||||||||||||||||||||
| 298 | 27 | $results->{functions}{$function}{arg_names} = [@arg_names]; | |||||||||||||||||||||||||||||||||||||||||
| 299 | 27 | $results->{functions}{$function}{arg_types} = [@arg_types]; | |||||||||||||||||||||||||||||||||||||||||
| 300 | 27 | ▶ | $results->{functions}{$function}{need_threading_context} = $need_threading_context if $need_threading_context; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | 27 | next; | |||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||
| 304 | 0 | RESYNC: # Skip the rest of the current line, and continue. | |||||||||||||||||||||||||||||||||||||||||
| 305 | $code =~ /\G[^\n]*\n/gc; | ||||||||||||||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||
| 308 | 13 | return $results; | |||||||||||||||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||
| 311 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||
| 313 | __END__ | ||||||||||||||||||||||||||||||||||||||||||