Debian Codegraph @

xs/TypeParser.xs

raw
1 MODULE = FFI::Platypus PACKAGE = FFI::Platypus::TypeParser
2
3 BOOT:
4 {
5 HV *bt = get_hv("FFI::Platypus::TypeParser::basic_type", GV_ADD);
6 hv_stores(bt, "void", newSViv(FFI_PL_TYPE_VOID));
7 hv_stores(bt, "sint8", newSViv(FFI_PL_TYPE_SINT8));
8 hv_stores(bt, "sint16", newSViv(FFI_PL_TYPE_SINT16));
9 hv_stores(bt, "sint32", newSViv(FFI_PL_TYPE_SINT32));
10 hv_stores(bt, "sint64", newSViv(FFI_PL_TYPE_SINT64));
11 hv_stores(bt, "uint8", newSViv(FFI_PL_TYPE_UINT8));
12 hv_stores(bt, "uint16", newSViv(FFI_PL_TYPE_UINT16));
13 hv_stores(bt, "uint32", newSViv(FFI_PL_TYPE_UINT32));
14 hv_stores(bt, "uint64", newSViv(FFI_PL_TYPE_UINT64));
15
16 hv_stores(bt, "float", newSViv(FFI_PL_TYPE_FLOAT));
17 hv_stores(bt, "double", newSViv(FFI_PL_TYPE_DOUBLE));
18 hv_stores(bt, "string", newSViv(FFI_PL_TYPE_STRING));
19 hv_stores(bt, "opaque", newSViv(FFI_PL_TYPE_OPAQUE));
20 #ifdef FFI_PL_PROBE_LONGDOUBLE
21 hv_stores(bt, "longdouble", newSViv(FFI_PL_TYPE_LONG_DOUBLE));
22 #endif
23 #ifdef FFI_PL_PROBE_COMPLEX
24 hv_stores(bt, "complex_float", newSViv(FFI_PL_TYPE_COMPLEX_FLOAT));
25 hv_stores(bt, "complex_double", newSViv(FFI_PL_TYPE_COMPLEX_DOUBLE));
26 #endif
27 }
28
29 ffi_pl_type *
30 create_type_basic(self, type_code)
31 SV *self
32 int type_code
33 PREINIT:
34 ffi_pl_type *type;
35 CODE:
36 (void)self;
37 type = ffi_pl_type_new(0);
38 type->type_code |= type_code;
39 RETVAL = type;
40 OUTPUT:
41 RETVAL
42
43 ffi_pl_type *
44 create_type_record(self, is_by_value, size, record_class=NULL, meta=NULL)
45 SV *self
46 int is_by_value
47 size_t size
48 ffi_pl_string record_class
49 void *meta
50 PREINIT:
51 ffi_pl_type *type;
52 CODE:
53 (void)self;
54 type = ffi_pl_type_new(sizeof(ffi_pl_type_extra_record));
55 type->type_code |= is_by_value ? FFI_PL_TYPE_RECORD_VALUE : FFI_PL_TYPE_RECORD;
56 type->extra[0].record.size = size;
57 if(record_class != NULL)
58 {
59 size = strlen(record_class)+1;
60 type->extra[0].record.class = malloc(size);
61 memcpy(type->extra[0].record.class, record_class, size);
62 }
63 else
64 {
65 type->extra[0].record.class = NULL;
66 }
67 type->extra[0].record.meta = meta;
68 RETVAL = type;
69 OUTPUT:
70 RETVAL
71
72 ffi_pl_type*
73 create_type_object(self, type_code, class)
74 SV *self
75 int type_code
76 ffi_pl_string class
77 PREINIT:
78 ffi_pl_type *type;
79 size_t size;
80 CODE:
81 (void)self;
82 type = ffi_pl_type_new(sizeof(ffi_pl_type_extra_object));
83 size = strlen(class)+1;
84 type->extra[0].object.class = malloc(size);
85 memcpy(type->extra[0].object.class, class, size);
86 type->type_code |= type_code;
87 type->type_code |= FFI_PL_SHAPE_OBJECT;
88 RETVAL = type;
89 OUTPUT:
90 RETVAL
91
92 ffi_pl_type *
93 create_type_string(self, rw)
94 SV *self
95 int rw
96 PREINIT:
97 ffi_pl_type *type;
98 CODE:
99 (void)self;
100 type = ffi_pl_type_new(0);
101 type->type_code = FFI_PL_TYPE_STRING;
102 if(rw)
103 type->sub_type = FFI_PL_TYPE_STRING_RW;
104 else
105 type->sub_type = FFI_PL_TYPE_STRING_RO;
106 RETVAL = type;
107 OUTPUT:
108 RETVAL
109
110 ffi_pl_type *
111 create_type_array(self, type_code, size)
112 SV *self
113 int type_code
114 size_t size
115 PREINIT:
116 ffi_pl_type *type;
117 CODE:
118 (void)self;
119 type = ffi_pl_type_new(sizeof(ffi_pl_type_extra_array));
120 type->type_code |= FFI_PL_SHAPE_ARRAY | type_code;
121 type->extra[0].array.element_count = size;
122 RETVAL = type;
123 OUTPUT:
124 RETVAL
125
126 ffi_pl_type*
127 create_type_pointer(self, type_code)
128 SV *self
129 int type_code
130 PREINIT:
131 ffi_pl_type *type;
132 CODE:
133 (void)self;
134 type = ffi_pl_type_new(0);
135 type->type_code |= FFI_PL_SHAPE_POINTER | type_code;
136 RETVAL = type;
137 OUTPUT:
138 RETVAL
139
140 ffi_pl_type *
141 _create_type_custom(self, basis, perl_to_native, native_to_perl, perl_to_native_post, argument_count)
142 SV *self
143 ffi_pl_type* basis
144 SV *perl_to_native
145 SV *native_to_perl
146 SV *perl_to_native_post
147 int argument_count
148 PREINIT:
149 ffi_pl_type *type;
150 int type_code;
151 ffi_pl_type_extra_custom_perl *custom;
152 ffi_pl_type_extra_record *record;
153 size_t size;
154 CODE:
155 (void)self;
156 type = ffi_pl_type_new(sizeof(ffi_pl_type_extra_custom_perl));
157 type->type_code = FFI_PL_SHAPE_CUSTOM_PERL | basis->type_code;
158
159 type->extra[0].record.class = NULL;
160 if( (basis->type_code & FFI_PL_BASE_MASK) == (FFI_PL_TYPE_RECORD & FFI_PL_BASE_MASK)
161 || (basis->type_code & FFI_PL_BASE_MASK) == (FFI_PL_TYPE_RECORD_VALUE & FFI_PL_BASE_MASK))
162 {
163 type->extra[0].record.size = basis->extra[0].record.size;
164 type->extra[0].record.meta = basis->extra[0].record.meta;
165 if(basis->extra[0].record.class)
166 {
167 size = strlen(basis->extra[0].record.class) + 1;
168 type->extra[0].record.class = malloc(size);
169 memcpy(type->extra[0].record.class, basis->extra[0].record.class, size);
170 }
171 }
172
173 custom = &type->extra[0].custom_perl;
174 custom->perl_to_native = SvOK(perl_to_native) ? SvREFCNT_inc_simple_NN(perl_to_native) : NULL;
175 custom->perl_to_native_post = SvOK(perl_to_native_post) ? SvREFCNT_inc_simple_NN(perl_to_native_post) : NULL;
176 custom->native_to_perl = SvOK(native_to_perl) ? SvREFCNT_inc_simple_NN(native_to_perl) : NULL;
177 custom->argument_count = argument_count-1;
178
179 RETVAL = type;
180 OUTPUT:
181 RETVAL
182
183
184 ffi_pl_type *
185 create_type_closure(self, abi, return_type, ...)
186 SV *self
187 int abi
188 ffi_pl_type *return_type
189 PREINIT:
190 ffi_pl_type *type;
191 int i;
192 SV *arg;
193 ffi_type *ffi_return_type;
194 ffi_type **ffi_argument_types;
195 ffi_status ffi_status;
196 CODE:
197 (void)self;
198 switch(return_type->type_code)
199 {
200 case FFI_PL_TYPE_VOID:
201 ffi_return_type = &ffi_type_void;
202 break;
203 case FFI_PL_TYPE_SINT8:
204 ffi_return_type = &ffi_type_sint8;
205 break;
206 case FFI_PL_TYPE_SINT16:
207 ffi_return_type = &ffi_type_sint16;
208 break;
209 case FFI_PL_TYPE_SINT32:
210 ffi_return_type = &ffi_type_sint32;
211 break;
212 case FFI_PL_TYPE_SINT64:
213 ffi_return_type = &ffi_type_sint64;
214 break;
215 case FFI_PL_TYPE_UINT8:
216 ffi_return_type = &ffi_type_uint8;
217 break;
218 case FFI_PL_TYPE_UINT16:
219 ffi_return_type = &ffi_type_uint16;
220 break;
221 case FFI_PL_TYPE_UINT32:
222 ffi_return_type = &ffi_type_uint32;
223 break;
224 case FFI_PL_TYPE_UINT64:
225 ffi_return_type = &ffi_type_uint64;
226 break;
227 case FFI_PL_TYPE_FLOAT:
228 ffi_return_type = &ffi_type_float;
229 break;
230 case FFI_PL_TYPE_DOUBLE:
231 ffi_return_type = &ffi_type_double;
232 break;
233 case FFI_PL_TYPE_OPAQUE:
234 ffi_return_type = &ffi_type_pointer;
235 break;
236 case FFI_PL_TYPE_RECORD_VALUE:
237 if(return_type->extra[0].record.meta == NULL)
238 croak("Only native types are supported as closure return types (%d)", return_type->type_code);
239 if(!return_type->extra[0].record.meta->can_return_from_closure)
240 croak("Record return type contains types that cannot be returned from a closure");
241 ffi_return_type = &return_type->extra[0].record.meta->ffi_type;
242 break;
243 default:
244 croak("Only native types are supported as closure return types (%d)", return_type->type_code);
245 break;
246 }
247
248 Newx(ffi_argument_types, items-3, ffi_type*);
249 type = ffi_pl_type_new(sizeof(ffi_pl_type_extra_closure) + sizeof(ffi_pl_type)*(items-3));
250 type->type_code = FFI_PL_TYPE_CLOSURE;
251
252 type->extra[0].closure.return_type = return_type;
253 type->extra[0].closure.flags = 0;
254
255 for(i=0; i<(items-3); i++)
256 {
257 arg = ST(3+i);
258 type->extra[0].closure.argument_types[i] = INT2PTR(ffi_pl_type*, SvIV((SV*)SvRV(arg)));
259 switch(type->extra[0].closure.argument_types[i]->type_code)
260 {
261 case FFI_PL_TYPE_VOID:
262 ffi_argument_types[i] = &ffi_type_void;
263 break;
264 case FFI_PL_TYPE_SINT8:
265 ffi_argument_types[i] = &ffi_type_sint8;
266 break;
267 case FFI_PL_TYPE_SINT16:
268 ffi_argument_types[i] = &ffi_type_sint16;
269 break;
270 case FFI_PL_TYPE_SINT32:
271 ffi_argument_types[i] = &ffi_type_sint32;
272 break;
273 case FFI_PL_TYPE_SINT64:
274 ffi_argument_types[i] = &ffi_type_sint64;
275 break;
276 case FFI_PL_TYPE_UINT8:
277 ffi_argument_types[i] = &ffi_type_uint8;
278 break;
279 case FFI_PL_TYPE_UINT16:
280 ffi_argument_types[i] = &ffi_type_uint16;
281 break;
282 case FFI_PL_TYPE_UINT32:
283 ffi_argument_types[i] = &ffi_type_uint32;
284 break;
285 case FFI_PL_TYPE_UINT64:
286 ffi_argument_types[i] = &ffi_type_uint64;
287 break;
288 case FFI_PL_TYPE_FLOAT:
289 ffi_argument_types[i] = &ffi_type_float;
290 break;
291 case FFI_PL_TYPE_DOUBLE:
292 ffi_argument_types[i] = &ffi_type_double;
293 break;
294 case FFI_PL_TYPE_OPAQUE:
295 case FFI_PL_TYPE_STRING:
296 case FFI_PL_TYPE_RECORD:
297 ffi_argument_types[i] = &ffi_type_pointer;
298 break;
299 case FFI_PL_TYPE_RECORD_VALUE:
300 if(type->extra[0].closure.argument_types[i]->extra[0].record.meta == NULL)
301 {
302 Safefree(ffi_argument_types);
303 croak("Only native types and strings are supported as closure argument types (%d)", type->extra[0].closure.argument_types[i]->type_code);
304 }
305 ffi_argument_types[i] = &type->extra[0].closure.argument_types[i]->extra[0].record.meta->ffi_type;
306 break;
307 default:
308 Safefree(ffi_argument_types);
309 croak("Only native types and strings are supported as closure argument types (%d)", type->extra[0].closure.argument_types[i]->type_code);
310 break;
311 }
312 }
313
314 ffi_status = ffi_prep_cif(
315 &type->extra[0].closure.ffi_cif,
316 abi == -1 ? FFI_DEFAULT_ABI : abi,
317 items-3,
318 ffi_return_type,
319 ffi_argument_types
320 );
321
322 if(ffi_status != FFI_OK)
323 {
324 Safefree(type);
325 Safefree(ffi_argument_types);
326 if(ffi_status == FFI_BAD_TYPEDEF)
327 croak("bad typedef");
328 else if(ffi_status == FFI_BAD_ABI)
329 croak("bad abi");
330 else
331 croak("unknown error with ffi_prep_cif");
332 }
333
334 if( items-3 == 0 )
335 {
336 type->extra[0].closure.flags |= G_NOARGS;
337 }
338
339 if(type->extra[0].closure.return_type->type_code == FFI_PL_TYPE_VOID)
340 {
341 type->extra[0].closure.flags |= G_DISCARD | G_VOID;
342 }
343 else
344 {
345 type->extra[0].closure.flags |= G_SCALAR;
346 }
347
348 RETVAL = type;
349
350 OUTPUT:
351 RETVAL
352
353