1
1
#!/usr/bin/env python
2
2
3
- from function import Function , read_functions
4
3
import argparse
5
4
import os
6
5
import re
7
6
8
- select_re = re .compile ('LAPACK_(\w)_SELECT(\d)' )
7
+ from function import Function
8
+ from function import read_functions
9
9
10
- def is_const (name , cty ):
11
- return '*const' in cty
10
+ select_re = re .compile ('LAPACK_(\w)_SELECT(\d)' )
12
11
13
- def is_mut (name , cty ):
14
- return '*mut' in cty
15
12
16
13
def is_scalar (name , cty , f ):
17
14
return (
@@ -69,20 +66,8 @@ def is_scalar(name, cty, f):
69
66
name .startswith ('vers' )
70
67
)
71
68
72
- def translate_argument (name , cty , f ):
73
- if is_const (name , cty ):
74
- base = translate_type_base (cty , f )
75
- if is_scalar (name , cty , f ):
76
- return base
77
- else :
78
- return '&[{}]' .format (base )
79
- elif is_mut (name , cty ):
80
- base = translate_type_base (cty , f )
81
- if is_scalar (name , cty , f ):
82
- return '&mut {}' .format (base )
83
- else :
84
- return '&mut [{}]' .format (base )
85
69
70
+ def translate_argument (name , cty , f ):
86
71
m = select_re .match (cty )
87
72
if m is not None :
88
73
if m .group (1 ) == 'S' :
@@ -94,25 +79,47 @@ def translate_argument(name, cty, f):
94
79
elif m .group (1 ) == 'Z' :
95
80
return 'Select{}C64' .format (m .group (2 ))
96
81
97
- assert False , 'cannot translate `{}: {}`' .format (name , cty )
82
+ base = translate_type_base (cty )
83
+ if '*const' in cty :
84
+ if is_scalar (name , cty , f ):
85
+ return base
86
+ else :
87
+ return '&[{}]' .format (base )
88
+ elif '*mut' in cty :
89
+ if is_scalar (name , cty , f ):
90
+ return '&mut {}' .format (base )
91
+ else :
92
+ return '&mut [{}]' .format (base )
93
+
94
+ return base
95
+
96
+
97
+ def translate_type_base (cty ):
98
+ cty = cty .replace ('__BindgenComplex<f32>' , 'lapack_complex_float' )
99
+ cty = cty .replace ('__BindgenComplex<f64>' , 'lapack_complex_double' )
100
+ cty = cty .replace ('f32' , 'c_float' )
101
+ cty = cty .replace ('f64' , 'c_double' )
98
102
99
- def translate_type_base (cty , f ):
100
103
if 'c_char' in cty :
101
104
return 'u8'
102
- elif 'lapack_int' in cty or 'lapack_logical ' in cty :
105
+ elif 'c_int ' in cty :
103
106
return 'i32'
104
- elif 'lapack_complex_double' in cty :
105
- return 'c64'
106
- elif 'lapack_complex_float' in cty :
107
- return 'c32'
108
- elif 'c_double' in cty :
109
- return 'f64'
110
107
elif 'c_float' in cty :
111
108
return 'f32'
109
+ elif 'c_double' in cty :
110
+ return 'f64'
111
+ elif 'lapack_complex_float' in cty :
112
+ return 'c32'
113
+ elif 'lapack_complex_double' in cty :
114
+ return 'c64'
115
+
116
+ assert False , 'cannot translate `{}`' .format (cty )
112
117
113
- assert False , 'cannot translate `{}` in `{}`' .format (cty , f .name )
114
118
115
119
def translate_body_argument (name , rty ):
120
+ if rty .startswith ('Select' ):
121
+ return 'transmute({})' .format (name )
122
+
116
123
if rty == 'u8' :
117
124
return '&({} as c_char)' .format (name )
118
125
elif rty == '&mut u8' :
@@ -130,7 +137,7 @@ def translate_body_argument(name, rty):
130
137
elif rty .startswith ('f' ):
131
138
return '&{}' .format (name )
132
139
elif rty .startswith ('&mut f' ):
133
- return '{}' . format ( name )
140
+ return name
134
141
elif rty .startswith ('&[f' ):
135
142
return '{}.as_ptr()' .format (name )
136
143
elif rty .startswith ('&mut [f' ):
@@ -145,57 +152,69 @@ def translate_body_argument(name, rty):
145
152
elif rty .startswith ('&mut [c' ):
146
153
return '{}.as_mut_ptr() as *mut _' .format (name )
147
154
148
- if rty .startswith ('Select' ):
149
- return 'transmute({})' .format (name )
150
-
151
155
assert False , 'cannot translate `{}: {}`' .format (name , rty )
152
156
157
+
153
158
def translate_return_type (cty ):
154
- if cty == 'c_float' :
159
+ cty = cty .replace ('lapack_float_return' , 'c_float' )
160
+ cty = cty .replace ('f64' , 'c_double' )
161
+
162
+ if cty == 'c_int' :
163
+ return 'i32'
164
+ elif cty == 'c_float' :
155
165
return 'f32'
156
166
elif cty == 'c_double' :
157
167
return 'f64'
158
168
159
169
assert False , 'cannot translate `{}`' .format (cty )
160
170
171
+
161
172
def format_header (f ):
162
173
args = format_header_arguments (f )
163
174
if f .ret is None :
164
175
return 'pub unsafe fn {}({})' .format (f .name , args )
165
176
else :
166
- return 'pub unsafe fn {}({}) -> {}' .format (f .name , args , translate_return_type (f .ret ))
177
+ return 'pub unsafe fn {}({}) -> {}' .format (f .name , args ,
178
+ translate_return_type (f .ret ))
179
+
167
180
168
181
def format_body (f ):
169
182
return 'ffi::{}_({})' .format (f .name , format_body_arguments (f ))
170
183
184
+
171
185
def format_header_arguments (f ):
172
186
s = []
173
187
for arg in f .args :
174
188
s .append ('{}: {}' .format (arg [0 ], translate_argument (* arg , f = f )))
175
189
return ', ' .join (s )
176
190
191
+
177
192
def format_body_arguments (f ):
178
193
s = []
179
194
for arg in f .args :
180
195
rty = translate_argument (* arg , f = f )
181
196
s .append (translate_body_argument (arg [0 ], rty ))
182
197
return ', ' .join (s )
183
198
199
+
184
200
def prepare (code ):
185
- lines = filter (lambda line : not re .match (r'^\s*//.*' , line ), code .split ('\n ' ))
201
+ lines = filter (lambda line : not re .match (r'^\s*//.*' , line ),
202
+ code .split ('\n ' ))
186
203
lines = re .sub (r'\s+' , ' ' , '' .join (lines )).strip ().split (';' )
187
204
lines = filter (lambda line : not re .match (r'^\s*$' , line ), lines )
188
205
return [Function .parse (line ) for line in lines ]
189
206
207
+
190
208
def do (functions ):
191
209
for f in functions :
192
210
print ('\n #[inline]' )
193
211
print (format_header (f ) + ' {' )
194
212
print (' ' + format_body (f ) + '\n }' )
195
213
214
+
196
215
if __name__ == '__main__' :
197
216
parser = argparse .ArgumentParser ()
198
- parser .add_argument ('--sys' , required = True )
217
+ parser .add_argument ('--sys' , default = 'lapack-sys' )
199
218
arguments = parser .parse_args ()
200
- path = os .path .join (arguments .sys , 'src' , 'lib .rs' )
219
+ path = os .path .join (arguments .sys , 'src' , 'lapack .rs' )
201
220
do (prepare (read_functions (path )))
0 commit comments