560af5c5 |
1 | ;; Common Lisp bindings for GTK+ v2.0 |
5cae32e1 |
2 | ;; Copyright (C) 1999-2001 Espen S. Johnsen <esj@stud.cs.uit.no> |
560af5c5 |
3 | ;; |
4 | ;; This library is free software; you can redistribute it and/or |
5 | ;; modify it under the terms of the GNU Lesser General Public |
6 | ;; License as published by the Free Software Foundation; either |
7 | ;; version 2 of the License, or (at your option) any later version. |
8 | ;; |
9 | ;; This library is distributed in the hope that it will be useful, |
10 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | ;; Lesser General Public License for more details. |
13 | ;; |
14 | ;; You should have received a copy of the GNU Lesser General Public |
15 | ;; License along with this library; if not, write to the Free Software |
16 | ;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
17 | |
9f4b70ee |
18 | ;; $Id: gforeign.lisp,v 1.10 2001-05-31 21:52:15 espen Exp $ |
560af5c5 |
19 | |
20 | (in-package "GLIB") |
21 | |
22 | ;;;; Type methods |
23 | |
24 | (defvar *type-methods* (make-hash-table)) |
25 | |
26 | (defun ensure-type-method-fun (fname) |
27 | (unless (fboundp fname) |
28 | (setf |
29 | (symbol-function fname) |
30 | #'(lambda (type-spec &rest args) |
31 | (apply |
32 | (find-applicable-type-method type-spec fname) type-spec args))))) |
33 | |
34 | (defmacro define-type-method-fun (fname lambda-list) |
35 | (declare (ignore lambda-list)) |
36 | `(defun ,fname (type-spec &rest args) |
37 | (apply |
38 | (find-applicable-type-method type-spec ',fname) type-spec args))) |
39 | |
40 | |
41 | (defun ensure-type-name (type) |
42 | (etypecase type |
43 | (symbol type) |
44 | (pcl::class (class-name type)))) |
45 | |
46 | (defun add-type-method (type fname function) |
47 | (push |
48 | (cons fname function) |
49 | (gethash (ensure-type-name type) *type-methods*))) |
50 | |
51 | (defun find-type-method (type fname) |
52 | (cdr (assoc fname (gethash (ensure-type-name type) *type-methods*)))) |
53 | |
54 | (defun find-applicable-type-method (type-spec fname &optional (error t)) |
55 | (flet ((find-superclass-method (class) |
56 | (when class |
9f4b70ee |
57 | (unless (class-finalized-p class) |
58 | (finalize-inheritance class)) |
560af5c5 |
59 | (dolist (super (cdr (pcl::class-precedence-list class))) |
60 | (return-if (find-type-method super fname))))) |
61 | (find-expanded-type-method (type-spec) |
62 | (multiple-value-bind (expanded-type-spec expanded-p) |
63 | (type-expand-1 type-spec) |
64 | (cond |
65 | (expanded-p |
66 | (find-applicable-type-method expanded-type-spec fname nil)) |
67 | ((neq type-spec t) |
68 | (find-applicable-type-method t fname nil)))))) |
69 | |
70 | (or |
71 | (typecase type-spec |
72 | (pcl::class |
73 | (or |
74 | (find-type-method type-spec fname) |
75 | (find-superclass-method type-spec))) |
76 | (symbol |
77 | (or |
78 | (find-type-method type-spec fname) |
79 | (find-expanded-type-method type-spec) |
80 | (find-superclass-method (find-class type-spec nil)))) |
81 | (cons |
82 | (or |
83 | (find-type-method (first type-spec) fname) |
84 | (find-expanded-type-method type-spec))) |
85 | (t |
86 | (error "Invalid type specifier ~A" type-spec))) |
87 | (and |
88 | error |
89 | (error |
90 | "No applicable method for ~A when called with type specifier ~A" |
91 | fname type-spec))))) |
92 | |
93 | (defmacro deftype-method (fname type lambda-list &body body) |
94 | `(progn |
95 | (ensure-type-method-fun ',fname) |
96 | (add-type-method ',type ',fname #'(lambda ,lambda-list ,@body)) |
97 | ',fname)) |
98 | |
5cae32e1 |
99 | ;; To make the compiler happy |
560af5c5 |
100 | (eval-when (:compile-toplevel :load-toplevel :execute) |
101 | (define-type-method-fun translate-type-spec (type-spec)) |
f5747cee |
102 | (define-type-method-fun size-of (type-spec)) |
5cae32e1 |
103 | (define-type-method-fun translate-to-alien (type-spec expr &optional weak-ref)) |
104 | (define-type-method-fun translate-from-alien (type-spec expr &optional weak-ref)) |
105 | (define-type-method-fun cleanup-alien (type-spec sap &otional weak-ref)) |
106 | (define-type-method-fun unreference-alien (type-spec sap))) |
107 | |
560af5c5 |
108 | |
109 | ;;;; |
110 | |
111 | (defvar *type-function-cache* (make-hash-table :test #'equal)) |
112 | |
113 | (defun get-cached-function (type-spec fname) |
114 | (cdr (assoc fname (gethash type-spec *type-function-cache*)))) |
115 | |
116 | (defun set-cached-function (type-spec fname function) |
117 | (push (cons fname function) (gethash type-spec *type-function-cache*)) |
118 | function) |
119 | |
120 | |
5cae32e1 |
121 | (defun intern-argument-translator (type-spec) |
560af5c5 |
122 | (or |
5cae32e1 |
123 | (get-cached-function type-spec 'argument-translator) |
124 | (set-cached-function type-spec 'argument-translator |
560af5c5 |
125 | (compile |
126 | nil |
127 | `(lambda (object) |
128 | (declare (ignorable object)) |
5cae32e1 |
129 | ,(translate-to-alien type-spec 'object t)))))) |
560af5c5 |
130 | |
5cae32e1 |
131 | (defun intern-return-value-translator (type-spec) |
560af5c5 |
132 | (or |
5cae32e1 |
133 | (get-cached-function type-spec 'return-value-translator) |
134 | (set-cached-function type-spec 'return-value-translator |
560af5c5 |
135 | (compile |
136 | nil |
137 | `(lambda (alien) |
138 | (declare (ignorable alien)) |
5cae32e1 |
139 | ,(translate-from-alien type-spec 'alien nil)))))) |
560af5c5 |
140 | |
5cae32e1 |
141 | (defun intern-cleanup-function (type-spec) |
560af5c5 |
142 | (or |
143 | (get-cached-function type-spec 'cleanup-function) |
144 | (set-cached-function type-spec 'cleanup-function |
145 | (compile |
146 | nil |
147 | `(lambda (alien) |
148 | (declare (ignorable alien)) |
5cae32e1 |
149 | ,(cleanup-alien type-spec 'alien t)))))) |
560af5c5 |
150 | |
151 | |
152 | |
5cae32e1 |
153 | ;; Returns a function to write an object of the specified type |
154 | ;; to a memory location |
155 | (defun intern-writer-function (type-spec) |
560af5c5 |
156 | (or |
157 | (get-cached-function type-spec 'writer-function) |
158 | (set-cached-function type-spec 'writer-function |
159 | (compile |
160 | nil |
161 | `(lambda (value sap offset) |
162 | (declare (ignorable value sap offset)) |
163 | (setf |
164 | (,(sap-ref-fname type-spec) sap offset) |
5cae32e1 |
165 | ,(translate-to-alien type-spec 'value nil))))))) |
560af5c5 |
166 | |
5cae32e1 |
167 | ;; Returns a function to read an object of the specified type |
168 | ;; from a memory location |
169 | (defun intern-reader-function (type-spec) |
560af5c5 |
170 | (or |
171 | (get-cached-function type-spec 'reader-function) |
172 | (set-cached-function type-spec 'reader-function |
173 | (compile |
174 | nil |
175 | `(lambda (sap offset) |
176 | (declare (ignorable sap offset)) |
177 | ,(translate-from-alien |
5cae32e1 |
178 | type-spec `(,(sap-ref-fname type-spec) sap offset) t)))))) |
560af5c5 |
179 | |
5cae32e1 |
180 | (defun intern-destroy-function (type-spec) |
181 | (if (atomic-type-p type-spec) |
182 | #'(lambda (sap offset) |
183 | (declare (ignore sap offset))) |
184 | (or |
185 | (get-cached-function type-spec 'destroy-function) |
186 | (set-cached-function type-spec 'destroy-function |
187 | (compile |
188 | nil |
189 | `(lambda (sap offset) |
190 | (declare (ignorable sap offset)) |
191 | ,(unreference-alien |
192 | type-spec `(,(sap-ref-fname type-spec) sap offset)))))))) |
560af5c5 |
193 | |
194 | |
195 | |
196 | ;;;; |
197 | |
f5747cee |
198 | (defconstant +bits-per-unit+ 8 |
199 | "Number of bits in an addressable unit (byte)") |
200 | |
201 | ;; Sizes of fundamental C types in addressable units |
202 | (defconstant +size-of-short+ 2) |
560af5c5 |
203 | (defconstant +size-of-int+ 4) |
f5747cee |
204 | (defconstant +size-of-long+ 4) |
560af5c5 |
205 | (defconstant +size-of-sap+ 4) |
206 | (defconstant +size-of-float+ 4) |
207 | (defconstant +size-of-double+ 8) |
208 | |
209 | (defun sap-ref-unsigned (sap offset) |
210 | (sap-ref-32 sap offset)) |
211 | |
212 | (defun sap-ref-signed (sap offset) |
213 | (signed-sap-ref-32 sap offset)) |
214 | |
215 | (defun sap-ref-fname (type-spec) |
216 | (let ((alien-type-spec (mklist (translate-type-spec type-spec)))) |
217 | (ecase (first alien-type-spec) |
218 | (unsigned |
219 | (ecase (second alien-type-spec) |
220 | (8 'sap-ref-8) |
221 | (16 'sap-ref-16) |
222 | (32 'sap-ref-32) |
223 | (64 'sap-ref-64))) |
224 | (signed |
225 | (ecase (second alien-type-spec) |
226 | (8 'signed-sap-ref-8) |
227 | (16 'signed-sap-ref-16) |
228 | (32 'signed-sap-ref-32) |
229 | (64 'signed-sap-ref-64))) |
230 | (system-area-pointer 'sap-ref-sap) |
231 | (single-float 'sap-ref-single) |
232 | (double-float 'sap-ref-double)))) |
233 | |
234 | |
560af5c5 |
235 | ;;;; Foreign function call interface |
236 | |
237 | (defvar *package-prefix* nil) |
238 | |
239 | (defun set-package-prefix (prefix &optional (package *package*)) |
240 | (let ((package (find-package package))) |
241 | (delete-if #'(lambda (assoc) (eq (car assoc) package)) *package-prefix*) |
242 | (push (cons package prefix) *package-prefix*)) |
243 | prefix) |
244 | |
245 | (defun package-prefix (&optional (package *package*)) |
246 | (let ((package (find-package package))) |
247 | (or |
248 | (cdr (assoc package *package-prefix*)) |
249 | (substitute #\_ #\- (string-downcase (package-name package)))))) |
250 | |
5cae32e1 |
251 | (defun find-prefix-package (prefix) |
252 | (or |
253 | (car (rassoc (string-downcase prefix) *package-prefix* :test #'string=)) |
254 | (find-package (string-upcase prefix)))) |
255 | |
560af5c5 |
256 | (defmacro use-prefix (prefix &optional (package *package*)) |
257 | `(eval-when (:compile-toplevel :load-toplevel :execute) |
258 | (set-package-prefix ,prefix ,package))) |
259 | |
260 | |
5cae32e1 |
261 | (defun default-alien-fname (lisp-name) |
560af5c5 |
262 | (let* ((lisp-name-string |
263 | (if (char= (char (the simple-string (string lisp-name)) 0) #\%) |
264 | (subseq (the simple-string (string lisp-name)) 1) |
265 | (string lisp-name))) |
266 | (prefix (package-prefix *package*)) |
267 | (name (substitute #\_ #\- (string-downcase lisp-name-string)))) |
268 | (if (or (not prefix) (string= prefix "")) |
269 | name |
270 | (format nil "~A_~A" prefix name)))) |
271 | |
5cae32e1 |
272 | (defun default-alien-type-name (type-name) |
273 | (let ((prefix (package-prefix *package*))) |
274 | (apply |
275 | #'concatenate |
276 | 'string |
277 | (mapcar |
278 | #'string-capitalize |
279 | (cons prefix (split-string (symbol-name type-name) #\-)))))) |
280 | |
281 | (defun default-type-name (alien-name) |
282 | (let ((parts |
283 | (mapcar |
284 | #'string-upcase |
285 | (split-string-if alien-name #'upper-case-p)))) |
286 | (intern |
287 | (concatenate-strings |
288 | (rest parts) #\-) (find-prefix-package (first parts))))) |
289 | |
290 | |
291 | (defmacro defbinding (name lambda-list return-type-spec &rest docs/args) |
fff7bb35 |
292 | (multiple-value-bind (lisp-name c-name) |
560af5c5 |
293 | (if (atom name) |
fff7bb35 |
294 | (values name (default-alien-fname name)) |
295 | (values-list name)) |
296 | |
560af5c5 |
297 | (let ((supplied-lambda-list lambda-list) |
298 | (docs nil) |
299 | (args nil)) |
300 | (dolist (doc/arg docs/args) |
301 | (if (stringp doc/arg) |
302 | (push doc/arg docs) |
303 | (progn |
304 | (destructuring-bind (expr type &optional (style :in)) doc/arg |
a27ed65c |
305 | (unless (member style '(:in :out :in-out)) |
560af5c5 |
306 | (error "Bogus argument style ~S in ~S." style doc/arg)) |
a27ed65c |
307 | (when (and |
308 | (not supplied-lambda-list) |
309 | (namep expr) (member style '(:in :in-out))) |
560af5c5 |
310 | (push expr lambda-list)) |
311 | (push |
312 | (list (if (namep expr) expr (gensym)) expr type style) args))))) |
313 | |
5cae32e1 |
314 | (%defbinding |
560af5c5 |
315 | c-name lisp-name (or supplied-lambda-list (nreverse lambda-list)) |
316 | return-type-spec (reverse docs) (reverse args))))) |
317 | |
560af5c5 |
318 | #+cmu |
5cae32e1 |
319 | (defun %defbinding (foreign-name lisp-name lambda-list |
320 | return-type-spec docs args) |
560af5c5 |
321 | (ext:collect ((alien-types) (alien-bindings) (alien-parameters) |
f5747cee |
322 | (alien-values) (alien-deallocators)) |
560af5c5 |
323 | (dolist (arg args) |
324 | (destructuring-bind (var expr type-spec style) arg |
325 | (let ((declaration (translate-type-spec type-spec)) |
9f4b70ee |
326 | (deallocation (cleanup-alien type-spec var t))) |
560af5c5 |
327 | (cond |
a27ed65c |
328 | ((member style '(:out :in-out)) |
560af5c5 |
329 | (alien-types `(* ,declaration)) |
330 | (alien-parameters `(addr ,var)) |
a27ed65c |
331 | (alien-bindings |
332 | `(,var ,declaration |
333 | ,@(when (eq style :in-out) |
5cae32e1 |
334 | (list (translate-to-alien type-spec expr t))))) |
335 | (alien-values (translate-from-alien type-spec var nil))) |
560af5c5 |
336 | (deallocation |
337 | (alien-types declaration) |
338 | (alien-bindings |
5cae32e1 |
339 | `(,var ,declaration ,(translate-to-alien type-spec expr t))) |
560af5c5 |
340 | (alien-parameters var) |
f5747cee |
341 | (alien-deallocators deallocation)) |
560af5c5 |
342 | (t |
343 | (alien-types declaration) |
5cae32e1 |
344 | (alien-parameters (translate-to-alien type-spec expr t))))))) |
560af5c5 |
345 | |
346 | (let ((alien-funcall `(alien-funcall ,lisp-name ,@(alien-parameters)))) |
347 | `(defun ,lisp-name ,lambda-list |
348 | ,@docs |
349 | (with-alien ((,lisp-name |
350 | (function |
351 | ,(translate-type-spec return-type-spec) |
352 | ,@(alien-types)) |
353 | :extern ,foreign-name) |
354 | ,@(alien-bindings)) |
355 | ,(if return-type-spec |
356 | `(let ((result |
5cae32e1 |
357 | ,(translate-from-alien return-type-spec alien-funcall nil))) |
f5747cee |
358 | ,@(alien-deallocators) |
560af5c5 |
359 | (values result ,@(alien-values))) |
360 | `(progn |
361 | ,alien-funcall |
f5747cee |
362 | ,@(alien-deallocators) |
560af5c5 |
363 | (values ,@(alien-values))))))))) |
364 | |
7479d92c |
365 | |
366 | (defun mkbinding (name rettype &rest types) |
367 | (declare (optimize (ext:inhibit-warnings 3))) |
368 | (let* ((ftype |
369 | `(function ,@(mapcar #'translate-type-spec (cons rettype types)))) |
370 | (alien |
371 | (alien::%heap-alien |
372 | (alien::make-heap-alien-info |
373 | :type (alien::parse-alien-type ftype) |
374 | :sap-form (system:foreign-symbol-address name)))) |
375 | (translate-arguments (mapcar #'intern-return-value-translator types)) |
376 | (translate-return-value (intern-return-value-translator rettype)) |
377 | (cleanup-arguments (mapcar #'intern-cleanup-function types))) |
378 | |
379 | #'(lambda (&rest args) |
380 | (map-into args #'funcall translate-arguments args) |
381 | (prog1 |
382 | (funcall |
383 | translate-return-value (apply #'alien:alien-funcall alien args)) |
384 | (mapc #'funcall cleanup-arguments args))))) |
560af5c5 |
385 | |
386 | |
f5747cee |
387 | ;;;; Definitons and translations of fundamental types |
560af5c5 |
388 | |
5cae32e1 |
389 | (deftype long (&optional (min '*) (max '*)) `(integer ,min ,max)) |
390 | (deftype unsigned-long (&optional (min '*) (max '*)) `(integer ,min ,max)) |
391 | (deftype int (&optional (min '*) (max '*)) `(long ,min ,max)) |
392 | (deftype unsigned-int (&optional (min '*) (max '*)) `(unsigned-long ,min ,max)) |
393 | (deftype short (&optional (min '*) (max '*)) `(int ,min ,max)) |
394 | (deftype unsigned-short (&optional (min '*) (max '*)) `(unsigned-int ,min ,max)) |
395 | (deftype signed (&optional (size '*)) `(signed-byte ,size)) |
396 | (deftype unsigned (&optional (size '*)) `(signed-byte ,size)) |
397 | (deftype char () 'base-char) |
398 | (deftype pointer () 'system-area-pointer) |
399 | (deftype boolean (&optional (size '*)) |
560af5c5 |
400 | (declare (ignore size)) |
401 | `(member t nil)) |
5cae32e1 |
402 | (deftype invalid () nil) |
560af5c5 |
403 | |
5cae32e1 |
404 | (defun atomic-type-p (type-spec) |
405 | (or |
406 | (eq type-spec 'pointer) |
407 | (not (eq (translate-type-spec type-spec) 'system-area-pointer)))) |
560af5c5 |
408 | |
f5747cee |
409 | |
5cae32e1 |
410 | (deftype-method cleanup-alien t (type-spec sap &optional weak-ref) |
411 | (declare (ignore type-spec sap weak-ref)) |
560af5c5 |
412 | nil) |
413 | |
414 | |
5cae32e1 |
415 | (deftype-method translate-to-alien integer (type-spec number &optional weak-ref) |
416 | (declare (ignore type-spec weak-ref)) |
560af5c5 |
417 | number) |
418 | |
5cae32e1 |
419 | (deftype-method translate-from-alien integer (type-spec number &optional weak-ref) |
420 | (declare (ignore type-spec weak-ref)) |
560af5c5 |
421 | number) |
422 | |
423 | |
424 | (deftype-method translate-type-spec fixnum (type-spec) |
425 | (declare (ignore type-spec)) |
f5747cee |
426 | (translate-type-spec 'signed)) |
427 | |
428 | (deftype-method size-of fixnum (type-spec) |
429 | (declare (ignore type-spec)) |
430 | (size-of 'signed)) |
560af5c5 |
431 | |
5cae32e1 |
432 | (deftype-method translate-to-alien fixnum (type-spec number &optional weak-ref) |
433 | (declare (ignore type-spec weak-ref)) |
560af5c5 |
434 | number) |
435 | |
5cae32e1 |
436 | (deftype-method translate-from-alien fixnum (type-spec number &optional weak-ref) |
437 | (declare (ignore type-spec weak-ref)) |
560af5c5 |
438 | number) |
439 | |
440 | |
441 | (deftype-method translate-type-spec long (type-spec) |
442 | (declare (ignore type-spec)) |
f5747cee |
443 | `(signed ,(* +bits-per-unit+ +size-of-long+))) |
444 | |
445 | (deftype-method size-of long (type-spec) |
446 | (declare (ignore type-spec)) |
447 | +size-of-long+) |
560af5c5 |
448 | |
449 | |
450 | (deftype-method translate-type-spec unsigned-long (type-spec) |
451 | (declare (ignore type-spec)) |
f5747cee |
452 | `(unsigned ,(* +bits-per-unit+ +size-of-long+))) |
453 | |
454 | (deftype-method size-of unsigned-long (type-spec) |
455 | (declare (ignore type-spec)) |
456 | +size-of-long+) |
457 | |
458 | |
459 | (deftype-method translate-type-spec int (type-spec) |
460 | (declare (ignore type-spec)) |
461 | `(signed ,(* +bits-per-unit+ +size-of-int+))) |
462 | |
463 | (deftype-method size-of int (type-spec) |
464 | (declare (ignore type-spec)) |
465 | +size-of-int+) |
466 | |
467 | |
468 | (deftype-method translate-type-spec unsigned-int (type-spec) |
469 | (declare (ignore type-spec)) |
5cae32e1 |
470 | `(unsigned ,(* +bits-per-unit+ +size-of-int+))) |
f5747cee |
471 | |
472 | (deftype-method size-of unsigned-int (type-spec) |
473 | (declare (ignore type-spec)) |
474 | +size-of-int+) |
560af5c5 |
475 | |
476 | |
477 | (deftype-method translate-type-spec short (type-spec) |
478 | (declare (ignore type-spec)) |
f5747cee |
479 | `(signed ,(* +bits-per-unit+ +size-of-short+))) |
480 | |
481 | (deftype-method size-of short (type-spec) |
482 | (declare (ignore type-spec)) |
483 | +size-of-short+) |
560af5c5 |
484 | |
485 | |
486 | (deftype-method translate-type-spec unsigned-short (type-spec) |
487 | (declare (ignore type-spec)) |
f5747cee |
488 | `(unsigned ,(* +bits-per-unit+ +size-of-short+))) |
489 | |
490 | (deftype-method size-of unsigned-short (type-spec) |
491 | (declare (ignore type-spec)) |
492 | +size-of-short+) |
560af5c5 |
493 | |
494 | |
495 | (deftype-method translate-type-spec signed-byte (type-spec) |
f5747cee |
496 | (let ((size (second (mklist (type-expand-to 'signed-byte type-spec))))) |
497 | `(signed |
498 | ,(cond |
499 | ((member size '(nil *)) (* +bits-per-unit+ +size-of-int+)) |
500 | (t size))))) |
501 | |
502 | (deftype-method size-of signed-byte (type-spec) |
503 | (let ((size (second (mklist (type-expand-to 'signed-byte type-spec))))) |
504 | (cond |
505 | ((member size '(nil *)) +size-of-int+) |
506 | (t (/ size +bits-per-unit+))))) |
560af5c5 |
507 | |
5cae32e1 |
508 | (deftype-method translate-to-alien signed-byte (type-spec number &optional weak-ref) |
509 | (declare (ignore type-spec weak-ref)) |
560af5c5 |
510 | number) |
511 | |
f5747cee |
512 | (deftype-method translate-from-alien signed-byte |
5cae32e1 |
513 | (type-spec number &optional weak-ref) |
514 | (declare (ignore type-spec weak-ref)) |
560af5c5 |
515 | number) |
516 | |
517 | |
518 | (deftype-method translate-type-spec unsigned-byte (type-spec) |
f5747cee |
519 | (let ((size (second (mklist (type-expand-to 'unsigned-byte type-spec))))) |
520 | `(signed |
521 | ,(cond |
522 | ((member size '(nil *)) (* +bits-per-unit+ +size-of-int+)) |
523 | (t size))))) |
524 | |
525 | (deftype-method size-of unsigned-byte (type-spec) |
526 | (let ((size (second (mklist (type-expand-to 'unsigned-byte type-spec))))) |
527 | (cond |
528 | ((member size '(nil *)) +size-of-int+) |
529 | (t (/ size +bits-per-unit+))))) |
530 | |
5cae32e1 |
531 | (deftype-method translate-to-alien unsigned-byte (type-spec number &optional weak-ref) |
532 | (declare (ignore type-spec weak-ref)) |
560af5c5 |
533 | number) |
534 | |
f5747cee |
535 | (deftype-method translate-from-alien unsigned-byte |
5cae32e1 |
536 | (type-spec number &optional weak-ref) |
537 | (declare (ignore type-spec weak-ref)) |
560af5c5 |
538 | number) |
539 | |
540 | |
541 | (deftype-method translate-type-spec single-float (type-spec) |
542 | (declare (ignore type-spec)) |
543 | 'single-float) |
544 | |
f5747cee |
545 | (deftype-method size-of single-float (type-spec) |
546 | (declare (ignore type-spec)) |
547 | +size-of-float+) |
548 | |
5cae32e1 |
549 | (deftype-method translate-to-alien single-float (type-spec number &optional weak-ref) |
550 | (declare (ignore type-spec weak-ref)) |
560af5c5 |
551 | number) |
552 | |
f5747cee |
553 | (deftype-method translate-from-alien single-float |
5cae32e1 |
554 | (type-spec number &optional weak-ref) |
555 | (declare (ignore type-spec weak-ref)) |
560af5c5 |
556 | number) |
557 | |
558 | |
559 | (deftype-method translate-type-spec double-float (type-spec) |
560 | (declare (ignore type-spec)) |
561 | 'double-float) |
562 | |
f5747cee |
563 | (deftype-method size-of double-float (type-spec) |
564 | (declare (ignore type-spec)) |
565 | +size-of-double+) |
566 | |
5cae32e1 |
567 | (deftype-method translate-to-alien double-float (type-spec number &optional weak-ref) |
568 | (declare (ignore type-spec weak-ref)) |
560af5c5 |
569 | number) |
570 | |
f5747cee |
571 | (deftype-method translate-from-alien double-float |
5cae32e1 |
572 | (type-spec number &optional weak-ref) |
573 | (declare (ignore type-spec weak-ref)) |
560af5c5 |
574 | number) |
575 | |
576 | |
577 | (deftype-method translate-type-spec base-char (type-spec) |
578 | (declare (ignore type-spec)) |
5cae32e1 |
579 | `(unsigned ,+bits-per-unit+)) |
f5747cee |
580 | |
581 | (deftype-method size-of base-char (type-spec) |
582 | (declare (ignore type-spec)) |
583 | 1) |
560af5c5 |
584 | |
5cae32e1 |
585 | (deftype-method translate-to-alien base-char (type-spec char &optional weak-ref) |
586 | (declare (ignore type-spec weak-ref)) |
560af5c5 |
587 | `(char-code ,char)) |
588 | |
5cae32e1 |
589 | (deftype-method translate-from-alien base-char (type-spec code &optional weak-ref) |
590 | (declare (ignore type-spec weak-ref)) |
560af5c5 |
591 | `(code-char ,code)) |
592 | |
593 | |
594 | (deftype-method translate-type-spec string (type-spec) |
595 | (declare (ignore type-spec)) |
596 | 'system-area-pointer) |
597 | |
f5747cee |
598 | (deftype-method size-of string (type-spec) |
599 | (declare (ignore type-spec)) |
600 | +size-of-sap+) |
601 | |
5cae32e1 |
602 | (deftype-method translate-to-alien string (type-spec string &optional weak-ref) |
603 | (declare (ignore type-spec weak-ref)) |
604 | `(let ((string ,string)) |
605 | ;; Always copy strings to prevent seg fault due to GC |
606 | (copy-memory |
607 | (make-pointer (1+ (kernel:get-lisp-obj-address string))) |
608 | (1+ (length string))))) |
560af5c5 |
609 | |
f5747cee |
610 | (deftype-method translate-from-alien string |
5cae32e1 |
611 | (type-spec c-string &optional weak-ref) |
560af5c5 |
612 | (declare (ignore type-spec)) |
5cae32e1 |
613 | `(let ((c-string ,c-string)) |
614 | (unless (null-pointer-p c-string) |
560af5c5 |
615 | (prog1 |
5cae32e1 |
616 | (c-call::%naturalize-c-string c-string) |
617 | ;,(unless weak-ref `(deallocate-memory c-string)) |
9bb7e4a2 |
618 | )))) |
560af5c5 |
619 | |
5cae32e1 |
620 | (deftype-method cleanup-alien string (type-spec c-string &optional weak-ref) |
5cae32e1 |
621 | (when weak-ref |
622 | (unreference-alien type-spec c-string))) |
560af5c5 |
623 | |
5cae32e1 |
624 | (deftype-method unreference-alien string (type-spec c-string) |
7479d92c |
625 | (declare (ignore type-spec)) |
5cae32e1 |
626 | `(let ((c-string ,c-string)) |
627 | (unless (null-pointer-p c-string) |
628 | (deallocate-memory c-string)))) |
629 | |
560af5c5 |
630 | |
631 | (deftype-method translate-type-spec boolean (type-spec) |
f5747cee |
632 | (translate-type-spec |
633 | (cons 'unsigned (cdr (mklist (type-expand-to 'boolean type-spec)))))) |
634 | |
635 | (deftype-method size-of boolean (type-spec) |
636 | (size-of |
637 | (cons 'unsigned (cdr (mklist (type-expand-to 'boolean type-spec)))))) |
560af5c5 |
638 | |
5cae32e1 |
639 | (deftype-method translate-to-alien boolean (type-spec boolean &optional weak-ref) |
640 | (declare (ignore type-spec weak-ref)) |
560af5c5 |
641 | `(if ,boolean 1 0)) |
642 | |
5cae32e1 |
643 | (deftype-method translate-from-alien boolean (type-spec int &optional weak-ref) |
644 | (declare (ignore type-spec weak-ref)) |
560af5c5 |
645 | `(not (zerop ,int))) |
646 | |
647 | |
f5747cee |
648 | (deftype-method translate-type-spec or (union-type) |
649 | (let* ((member-types (cdr (type-expand-to 'or union-type))) |
650 | (alien-type (translate-type-spec (first member-types)))) |
651 | (dolist (type (cdr member-types)) |
652 | (unless (eq alien-type (translate-type-spec type)) |
653 | (error "No common alien type specifier for union type: ~A" union-type))) |
654 | alien-type)) |
655 | |
656 | (deftype-method size-of or (union-type) |
657 | (size-of (first (cdr (type-expand-to 'or union-type))))) |
560af5c5 |
658 | |
5cae32e1 |
659 | (deftype-method translate-to-alien or (union-type-spec expr &optional weak-ref) |
560af5c5 |
660 | (destructuring-bind (name &rest type-specs) |
661 | (type-expand-to 'or union-type-spec) |
662 | (declare (ignore name)) |
663 | `(let ((value ,expr)) |
664 | (etypecase value |
665 | ,@(map |
666 | 'list |
9bb7e4a2 |
667 | #'(lambda (type-spec) |
5cae32e1 |
668 | (list type-spec (translate-to-alien type-spec 'value weak-ref))) |
9bb7e4a2 |
669 | type-specs))))) |
560af5c5 |
670 | |
671 | |
560af5c5 |
672 | (deftype-method translate-type-spec system-area-pointer (type-spec) |
673 | (declare (ignore type-spec)) |
674 | 'system-area-pointer) |
675 | |
f5747cee |
676 | (deftype-method size-of system-area-pointer (type-spec) |
677 | (declare (ignore type-spec)) |
678 | +size-of-sap+) |
679 | |
5cae32e1 |
680 | (deftype-method translate-to-alien system-area-pointer (type-spec sap &optional weak-ref) |
681 | (declare (ignore type-spec weak-ref)) |
560af5c5 |
682 | sap) |
683 | |
f5747cee |
684 | (deftype-method translate-from-alien system-area-pointer |
5cae32e1 |
685 | (type-spec sap &optional weak-ref) |
686 | (declare (ignore type-spec weak-ref)) |
560af5c5 |
687 | sap) |
688 | |
689 | |
690 | (deftype-method translate-type-spec null (type-spec) |
691 | (declare (ignore type-spec)) |
692 | 'system-area-pointer) |
693 | |
5cae32e1 |
694 | (deftype-method translate-to-alien null (type-spec expr &optional weak-ref) |
695 | (declare (ignore type-spec expr weak-ref)) |
560af5c5 |
696 | `(make-pointer 0)) |
697 | |
698 | |
699 | (deftype-method translate-type-spec nil (type-spec) |
700 | (declare (ignore type-spec)) |
701 | 'void) |
7479d92c |
702 | |
703 | (deftype-method translate-from-alien nil (type-spec expr &optional weak-ref) |
704 | (declare (ignore type-spec weak-ref)) |
705 | `(progn |
706 | ,expr |
707 | (values))) |