manual: add Clasp to "Implementation Support"
[cffi.git] / src / types.lisp
blob5dc0b6d812b7b88974c682fc455f6ece44b16009
1 ;;;; -*- Mode: lisp; indent-tabs-mode: nil -*-
2 ;;;
3 ;;; types.lisp --- User-defined CFFI types.
4 ;;;
5 ;;; Copyright (C) 2005-2006, James Bielman <jamesjb@jamesjb.com>
6 ;;; Copyright (C) 2005-2007, Luis Oliveira <loliveira@common-lisp.net>
7 ;;;
8 ;;; Permission is hereby granted, free of charge, to any person
9 ;;; obtaining a copy of this software and associated documentation
10 ;;; files (the "Software"), to deal in the Software without
11 ;;; restriction, including without limitation the rights to use, copy,
12 ;;; modify, merge, publish, distribute, sublicense, and/or sell copies
13 ;;; of the Software, and to permit persons to whom the Software is
14 ;;; furnished to do so, subject to the following conditions:
15 ;;;
16 ;;; The above copyright notice and this permission notice shall be
17 ;;; included in all copies or substantial portions of the Software.
18 ;;;
19 ;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 ;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 ;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 ;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23 ;;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24 ;;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 ;;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 ;;; DEALINGS IN THE SOFTWARE.
27 ;;;
29 (in-package #:cffi)
31 ;;;# Built-In Types
33 ;; NOTE: In the C standard there's a "signed-char":
34 ;; https://stackoverflow.com/questions/436513/char-signed-char-char-unsigned-char
35 ;; and "char" may be either signed or unsigned, i.e. treating it as a small int
36 ;; is not wise. At the level of CFFI we can safely ignore this and assume that
37 ;; :char is mapped to "signed-char" by the CL implementation under us.
38 (define-built-in-foreign-type :char)
39 (define-built-in-foreign-type :unsigned-char)
40 (define-built-in-foreign-type :short)
41 (define-built-in-foreign-type :unsigned-short)
42 (define-built-in-foreign-type :int)
43 (define-built-in-foreign-type :unsigned-int)
44 (define-built-in-foreign-type :long)
45 (define-built-in-foreign-type :unsigned-long)
46 (define-built-in-foreign-type :float)
47 (define-built-in-foreign-type :double)
48 (define-built-in-foreign-type :void)
50 #-cffi-sys::no-long-long
51 (progn
52 (define-built-in-foreign-type :long-long)
53 (define-built-in-foreign-type :unsigned-long-long))
55 ;;; Define emulated LONG-LONG types. Needs checking whether we're
56 ;;; using the right sizes on various platforms.
57 ;;;
58 ;;; A possibly better, certainly faster though more intrusive,
59 ;;; alternative is available here:
60 ;;; <http://article.gmane.org/gmane.lisp.cffi.devel/1091>
61 #+cffi-sys::no-long-long
62 (eval-when (:compile-toplevel :load-toplevel :execute)
63 (defclass emulated-llong-type (foreign-type) ())
64 (defmethod foreign-type-size ((tp emulated-llong-type)) 8)
65 (defmethod foreign-type-alignment ((tp emulated-llong-type))
66 ;; better than assuming that the alignment is 8
67 (foreign-type-alignment :long))
68 (defmethod aggregatep ((tp emulated-llong-type)) nil)
70 (define-foreign-type emulated-llong (emulated-llong-type)
72 (:simple-parser :long-long))
74 (define-foreign-type emulated-ullong (emulated-llong-type)
76 (:simple-parser :unsigned-long-long))
78 (defmethod canonicalize ((tp emulated-llong)) :long-long)
79 (defmethod unparse-type ((tp emulated-llong)) :long-long)
80 (defmethod canonicalize ((tp emulated-ullong)) :unsigned-long-long)
81 (defmethod unparse-type ((tp emulated-ullong)) :unsigned-long-long)
83 (defun %emulated-mem-ref-64 (ptr type offset)
84 (let ((value #+big-endian
85 (+ (ash (mem-ref ptr :unsigned-long offset) 32)
86 (mem-ref ptr :unsigned-long (+ offset 4)))
87 #+little-endian
88 (+ (mem-ref ptr :unsigned-long offset)
89 (ash (mem-ref ptr :unsigned-long (+ offset 4)) 32))))
90 (if (and (eq type :long-long) (logbitp 63 value))
91 (lognot (logxor value #xFFFFFFFFFFFFFFFF))
92 value)))
94 (defun %emulated-mem-set-64 (value ptr type offset)
95 (when (and (eq type :long-long) (minusp value))
96 (setq value (lognot (logxor value #xFFFFFFFFFFFFFFFF))))
97 (%mem-set (ldb (byte 32 0) value) ptr :unsigned-long
98 #+big-endian (+ offset 4) #+little-endian offset)
99 (%mem-set (ldb (byte 32 32) value) ptr :unsigned-long
100 #+big-endian offset #+little-endian (+ offset 4))
101 value))
103 ;;; When some lisp other than SCL supports :long-double we should
104 ;;; use #-cffi-sys::no-long-double here instead.
105 #+(and scl long-float) (define-built-in-foreign-type :long-double)
107 (defparameter *possible-float-types* '(:float :double :long-double))
109 (defparameter *other-builtin-types* '(:pointer :void)
110 "List of types other than integer or float built in to CFFI.")
112 (defparameter *built-in-integer-types*
113 (set-difference
114 cffi:*built-in-foreign-types*
115 (append *possible-float-types* *other-builtin-types*))
116 "List of integer types supported by CFFI.")
118 (defparameter *built-in-float-types*
119 (set-difference
120 cffi:*built-in-foreign-types*
121 (append *built-in-integer-types* *other-builtin-types*))
122 "List of real float types supported by CFFI.")
124 ;;;# Foreign Pointers
126 (define-modify-macro incf-pointer (&optional (offset 1)) inc-pointer)
128 (defun mem-ref (ptr type &optional (offset 0))
129 "Return the value of TYPE at OFFSET bytes from PTR. If TYPE is aggregate,
130 we don't return its 'value' but a pointer to it, which is PTR itself."
131 (let* ((parsed-type (parse-type type))
132 (ctype (canonicalize parsed-type)))
133 #+cffi-sys::no-long-long
134 (when (member ctype '(:long-long :unsigned-long-long))
135 (return-from mem-ref
136 (translate-from-foreign (%emulated-mem-ref-64 ptr ctype offset)
137 parsed-type)))
138 ;; normal branch
139 (if (aggregatep parsed-type)
140 (if (bare-struct-type-p parsed-type)
141 (inc-pointer ptr offset)
142 (translate-from-foreign (inc-pointer ptr offset) parsed-type))
143 (translate-from-foreign (%mem-ref ptr ctype offset) parsed-type))))
145 (define-compiler-macro mem-ref (&whole form ptr type &optional (offset 0))
146 "Compiler macro to open-code MEM-REF when TYPE is constant."
147 (if (constantp type)
148 (let* ((parsed-type (parse-type (eval type)))
149 (ctype (canonicalize parsed-type)))
150 ;; Bail out when using emulated long long types.
151 #+cffi-sys::no-long-long
152 (when (member ctype '(:long-long :unsigned-long-long))
153 (return-from mem-ref form))
154 (if (aggregatep parsed-type)
155 (if (bare-struct-type-p parsed-type)
156 `(inc-pointer ,ptr ,offset)
157 (expand-from-foreign `(inc-pointer ,ptr ,offset) parsed-type))
158 (expand-from-foreign `(%mem-ref ,ptr ,ctype ,offset) parsed-type)))
159 form))
161 (defun mem-set (value ptr type &optional (offset 0))
162 "Set the value of TYPE at OFFSET bytes from PTR to VALUE."
163 (let* ((ptype (parse-type type))
164 (ctype (canonicalize ptype)))
165 #+cffi-sys::no-long-long
166 (when (or (eq ctype :long-long) (eq ctype :unsigned-long-long))
167 (return-from mem-set
168 (%emulated-mem-set-64 (translate-to-foreign value ptype)
169 ptr ctype offset)))
170 (if (aggregatep ptype) ; XXX: backwards incompatible?
171 (translate-into-foreign-memory value ptype (inc-pointer ptr offset))
172 (%mem-set (translate-to-foreign value ptype) ptr ctype offset))))
174 (define-setf-expander mem-ref (ptr type &optional (offset 0) &environment env)
175 "SETF expander for MEM-REF that doesn't rebind TYPE.
176 This is necessary for the compiler macro on MEM-SET to be able
177 to open-code (SETF MEM-REF) forms."
178 (multiple-value-bind (dummies vals newval setter getter)
179 (get-setf-expansion ptr env)
180 (declare (ignore setter newval))
181 ;; if either TYPE or OFFSET are constant, we avoid rebinding them
182 ;; so that the compiler macros on MEM-SET and %MEM-SET work.
183 (with-unique-names (store type-tmp offset-tmp)
184 (values
185 (append (unless (constantp type) (list type-tmp))
186 (unless (constantp offset) (list offset-tmp))
187 dummies)
188 (append (unless (constantp type) (list type))
189 (unless (constantp offset) (list offset))
190 vals)
191 (list store)
192 `(progn
193 (mem-set ,store ,getter
194 ,@(if (constantp type) (list type) (list type-tmp))
195 ,@(if (constantp offset) (list offset) (list offset-tmp)))
196 ,store)
197 `(mem-ref ,getter
198 ,@(if (constantp type) (list type) (list type-tmp))
199 ,@(if (constantp offset) (list offset) (list offset-tmp)))))))
201 (define-compiler-macro mem-set
202 (&whole form value ptr type &optional (offset 0))
203 "Compiler macro to open-code (SETF MEM-REF) when type is constant."
204 (if (constantp type)
205 (let* ((parsed-type (parse-type (eval type)))
206 (ctype (canonicalize parsed-type)))
207 ;; Bail out when using emulated long long types.
208 #+cffi-sys::no-long-long
209 (when (member ctype '(:long-long :unsigned-long-long))
210 (return-from mem-set form))
211 (if (aggregatep parsed-type)
212 (expand-into-foreign-memory
213 value parsed-type `(inc-pointer ,ptr ,offset))
214 `(%mem-set ,(expand-to-foreign value parsed-type)
215 ,ptr ,ctype ,offset)))
216 form))
218 ;;;# Dereferencing Foreign Arrays
220 ;;; Maybe this should be named MEM-SVREF? [2007-02-28 LO]
221 (defun mem-aref (ptr type &optional (index 0))
222 "Like MEM-REF except for accessing 1d arrays."
223 (mem-ref ptr type (* index (foreign-type-size type))))
225 (define-compiler-macro mem-aref (&whole form ptr type &optional (index 0))
226 "Compiler macro to open-code MEM-AREF when TYPE (and eventually INDEX)."
227 (if (constantp type)
228 (if (constantp index)
229 `(mem-ref ,ptr ,type
230 ,(* (eval index) (foreign-type-size (eval type))))
231 `(mem-ref ,ptr ,type (* ,index ,(foreign-type-size (eval type)))))
232 form))
234 (define-setf-expander mem-aref (ptr type &optional (index 0) &environment env)
235 "SETF expander for MEM-AREF."
236 (multiple-value-bind (dummies vals newval setter getter)
237 (get-setf-expansion ptr env)
238 (declare (ignore setter newval))
239 ;; we avoid rebinding type and index, if possible (and if type is not
240 ;; constant, we don't bother about the index), so that the compiler macros
241 ;; on MEM-SET or %MEM-SET can work.
242 (with-unique-names (store type-tmp index-tmp)
243 (values
244 (append (unless (constantp type)
245 (list type-tmp))
246 (unless (and (constantp type) (constantp index))
247 (list index-tmp))
248 dummies)
249 (append (unless (constantp type)
250 (list type))
251 (unless (and (constantp type) (constantp index))
252 (list index))
253 vals)
254 (list store)
255 ;; Here we'll try to calculate the offset from the type and index,
256 ;; or if not possible at least get the type size early.
257 `(progn
258 ,(if (constantp type)
259 (if (constantp index)
260 `(mem-set ,store ,getter ,type
261 ,(* (eval index) (foreign-type-size (eval type))))
262 `(mem-set ,store ,getter ,type
263 (* ,index-tmp ,(foreign-type-size (eval type)))))
264 `(mem-set ,store ,getter ,type-tmp
265 (* ,index-tmp (foreign-type-size ,type-tmp))))
266 ,store)
267 `(mem-aref ,getter
268 ,@(if (constantp type)
269 (list type)
270 (list type-tmp))
271 ,@(if (and (constantp type) (constantp index))
272 (list index)
273 (list index-tmp)))))))
275 (defmethod translate-into-foreign-memory
276 (value (type foreign-pointer-type) pointer)
277 (setf (mem-aref pointer :pointer) value))
279 (defmethod translate-into-foreign-memory
280 (value (type foreign-built-in-type) pointer)
281 (setf (mem-aref pointer (unparse-type type)) value))
283 (defun mem-aptr (ptr type &optional (index 0))
284 "The pointer to the element."
285 (inc-pointer ptr (* index (foreign-type-size type))))
287 (define-compiler-macro mem-aptr (&whole form ptr type &optional (index 0))
288 "The pointer to the element."
289 (cond ((not (constantp type))
290 form)
291 ((not (constantp index))
292 `(inc-pointer ,ptr (* ,index ,(foreign-type-size (eval type)))))
293 ((zerop (eval index))
294 ptr)
296 `(inc-pointer ,ptr ,(* (eval index)
297 (foreign-type-size (eval type)))))))
299 (define-foreign-type foreign-array-type ()
300 ((dimensions :reader dimensions :initarg :dimensions)
301 (element-type :reader element-type :initarg :element-type))
302 (:actual-type :pointer))
304 (defmethod aggregatep ((type foreign-array-type))
307 (defmethod print-object ((type foreign-array-type) stream)
308 "Print a FOREIGN-ARRAY-TYPE instance to STREAM unreadably."
309 (print-unreadable-object (type stream :type t :identity nil)
310 (format stream "~S ~S" (element-type type) (dimensions type))))
312 (defun array-element-size (array-type)
313 (foreign-type-size (element-type array-type)))
315 (defmethod foreign-type-size ((type foreign-array-type))
316 (* (array-element-size type) (reduce #'* (dimensions type))))
318 (defmethod foreign-type-alignment ((type foreign-array-type))
319 (foreign-type-alignment (element-type type)))
321 (define-parse-method :array (element-type &rest dimensions)
322 (assert (plusp (length dimensions)))
323 (make-instance 'foreign-array-type
324 :element-type element-type
325 :dimensions dimensions))
327 (defun indexes-to-row-major-index (dimensions &rest subscripts)
328 (apply #'+ (maplist (lambda (x y)
329 (* (car x) (apply #'* (cdr y))))
330 subscripts
331 dimensions)))
333 (defun row-major-index-to-indexes (index dimensions)
334 (loop with idx = index
335 with rank = (length dimensions)
336 with indexes = (make-list rank)
337 for dim-index from (- rank 1) downto 0 do
338 (setf (values idx (nth dim-index indexes))
339 (floor idx (nth dim-index dimensions)))
340 finally (return indexes)))
342 (defun foreign-alloc (type &key (initial-element nil initial-element-p)
343 (initial-contents nil initial-contents-p)
344 (count 1 count-p) null-terminated-p)
345 "Allocate enough memory to hold COUNT objects of type TYPE. If
346 INITIAL-ELEMENT is supplied, each element of the newly allocated
347 memory is initialized with its value. If INITIAL-CONTENTS is supplied,
348 each of its elements will be used to initialize the contents of the
349 newly allocated memory."
350 (let (contents-length)
351 ;; Some error checking, etc...
352 (when (and null-terminated-p
353 (not (eq (canonicalize-foreign-type type) :pointer)))
354 (error "Cannot use :NULL-TERMINATED-P with non-pointer types."))
355 (when (and initial-element-p initial-contents-p)
356 (error "Cannot specify both :INITIAL-ELEMENT and :INITIAL-CONTENTS"))
357 (when initial-contents-p
358 (setq contents-length (length initial-contents))
359 (if count-p
360 (assert (>= count contents-length))
361 (setq count contents-length)))
362 ;; Everything looks good.
363 (let ((ptr (%foreign-alloc (* (foreign-type-size type)
364 (if null-terminated-p (1+ count) count)))))
365 (when initial-element-p
366 (dotimes (i count)
367 (setf (mem-aref ptr type i) initial-element)))
368 (when initial-contents-p
369 (dotimes (i contents-length)
370 (setf (mem-aref ptr type i) (elt initial-contents i))))
371 (when null-terminated-p
372 (setf (mem-aref ptr :pointer count) (null-pointer)))
373 ptr)))
375 ;;; Simple compiler macro that kicks in when TYPE is constant and only
376 ;;; the COUNT argument is passed. (Note: hard-coding the type's size
377 ;;; into the fasl will likely break CLISP fasl cross-platform
378 ;;; compatibilty.)
379 (define-compiler-macro foreign-alloc (&whole form type &rest args
380 &key (count 1 count-p) &allow-other-keys)
381 (if (or (and count-p (<= (length args) 2)) (null args))
382 (cond
383 ((and (constantp type) (constantp count))
384 `(%foreign-alloc ,(* (eval count) (foreign-type-size (eval type)))))
385 ((constantp type)
386 `(%foreign-alloc (* ,count ,(foreign-type-size (eval type)))))
387 (t form))
388 form))
390 (defun lisp-array-to-foreign (array pointer array-type)
391 "Copy elements from a Lisp array to POINTER."
392 (let* ((type (ensure-parsed-base-type array-type))
393 (el-type (element-type type))
394 (dimensions (dimensions type)))
395 (loop with foreign-type-size = (array-element-size type)
396 with size = (reduce #'* dimensions)
397 for i from 0 below size
398 for offset = (* i foreign-type-size)
399 for element = (apply #'aref array
400 (row-major-index-to-indexes i dimensions))
401 do (setf (mem-ref pointer el-type offset) element))))
403 (defun foreign-array-to-lisp (pointer array-type)
404 "Copy elements from ptr into a Lisp array. If POINTER is a null
405 pointer, returns NIL."
406 (unless (null-pointer-p pointer)
407 (let* ((type (ensure-parsed-base-type array-type))
408 (el-type (element-type type))
409 (dimensions (dimensions type))
410 (array (make-array dimensions)))
411 (loop with foreign-type-size = (array-element-size type)
412 with size = (reduce #'* dimensions)
413 for i from 0 below size
414 for offset = (* i foreign-type-size)
415 for element = (mem-ref pointer el-type offset)
416 do (setf (apply #'aref array
417 (row-major-index-to-indexes i dimensions))
418 element))
419 array)))
421 (defun foreign-array-alloc (array array-type)
422 "Allocate a foreign array containing the elements of lisp array.
423 The foreign array must be freed with foreign-array-free."
424 (check-type array array)
425 (let* ((type (ensure-parsed-base-type array-type))
426 (ptr (foreign-alloc (element-type type)
427 :count (reduce #'* (dimensions type)))))
428 (lisp-array-to-foreign array ptr array-type)
429 ptr))
431 (defun foreign-array-free (ptr)
432 "Free a foreign array allocated by foreign-array-alloc."
433 (foreign-free ptr))
435 (defmacro with-foreign-array ((var lisp-array array-type) &body body)
436 "Bind var to a foreign array containing lisp-array elements in body."
437 (with-unique-names (type)
438 `(let ((,type (ensure-parsed-base-type ,array-type)))
439 (with-foreign-pointer (,var (* (reduce #'* (dimensions ,type))
440 (array-element-size ,type)))
441 (lisp-array-to-foreign ,lisp-array ,var ,array-type)
442 ,@body))))
444 (defun foreign-aref (ptr array-type &rest indexes)
445 (let* ((type (ensure-parsed-base-type array-type))
446 (offset (* (array-element-size type)
447 (apply #'indexes-to-row-major-index
448 (dimensions type) indexes))))
449 (mem-ref ptr (element-type type) offset)))
451 (defun (setf foreign-aref) (value ptr array-type &rest indexes)
452 (let* ((type (ensure-parsed-base-type array-type))
453 (offset (* (array-element-size type)
454 (apply #'indexes-to-row-major-index
455 (dimensions type) indexes))))
456 (setf (mem-ref ptr (element-type type) offset) value)))
458 ;;; Automatic translations for the :ARRAY type. Notice that these
459 ;;; translators will also invoke the appropriate translators for for
460 ;;; each of the array's elements since that's the normal behaviour of
461 ;;; the FOREIGN-ARRAY-* operators, but there's a FIXME: **it doesn't
462 ;;; free them yet**
464 ;;; This used to be in a separate type but let's experiment with just
465 ;;; one type for a while. [2008-12-30 LO]
467 ;;; FIXME: those ugly invocations of UNPARSE-TYPE suggest that these
468 ;;; foreign array operators should take the type and dimention
469 ;;; arguments "unboxed". [2008-12-31 LO]
471 (defmethod translate-to-foreign (array (type foreign-array-type))
472 (foreign-array-alloc array (unparse-type type)))
474 (defmethod translate-aggregate-to-foreign (ptr value (type foreign-array-type))
475 (lisp-array-to-foreign value ptr (unparse-type type)))
477 (defmethod translate-from-foreign (pointer (type foreign-array-type))
478 (foreign-array-to-lisp pointer (unparse-type type)))
480 (defmethod free-translated-object (pointer (type foreign-array-type) param)
481 (declare (ignore param))
482 (foreign-array-free pointer))
484 ;;;# Foreign Structures
486 ;;;## Foreign Structure Slots
488 (defgeneric foreign-struct-slot-pointer (ptr slot)
489 (:documentation
490 "Get the address of SLOT relative to PTR."))
492 (defgeneric foreign-struct-slot-pointer-form (ptr slot)
493 (:documentation
494 "Return a form to get the address of SLOT in PTR."))
496 (defgeneric foreign-struct-slot-value (ptr slot)
497 (:documentation
498 "Return the value of SLOT in structure PTR."))
500 (defgeneric (setf foreign-struct-slot-value) (value ptr slot)
501 (:documentation
502 "Set the value of a SLOT in structure PTR."))
504 (defgeneric foreign-struct-slot-value-form (ptr slot)
505 (:documentation
506 "Return a form to get the value of SLOT in struct PTR."))
508 (defgeneric foreign-struct-slot-set-form (value ptr slot)
509 (:documentation
510 "Return a form to set the value of SLOT in struct PTR."))
512 (defclass foreign-struct-slot ()
513 ((name :initarg :name :reader slot-name)
514 (offset :initarg :offset :accessor slot-offset)
515 ;; FIXME: the type should probably be parsed?
516 (type :initarg :type :accessor slot-type))
517 (:documentation "Base class for simple and aggregate slots."))
519 (defmethod foreign-struct-slot-pointer (ptr (slot foreign-struct-slot))
520 "Return the address of SLOT relative to PTR."
521 (inc-pointer ptr (slot-offset slot)))
523 (defmethod foreign-struct-slot-pointer-form (ptr (slot foreign-struct-slot))
524 "Return a form to get the address of SLOT relative to PTR."
525 (let ((offset (slot-offset slot)))
526 (if (zerop offset)
528 `(inc-pointer ,ptr ,offset))))
530 (defun foreign-slot-names (type)
531 "Returns a list of TYPE's slot names in no particular order."
532 (loop for value being the hash-values
533 in (slots (ensure-parsed-base-type type))
534 collect (slot-name value)))
536 ;;;### Simple Slots
538 (defclass simple-struct-slot (foreign-struct-slot)
540 (:documentation "Non-aggregate structure slots."))
542 (defmethod foreign-struct-slot-value (ptr (slot simple-struct-slot))
543 "Return the value of a simple SLOT from a struct at PTR."
544 (mem-ref ptr (slot-type slot) (slot-offset slot)))
546 (defmethod foreign-struct-slot-value-form (ptr (slot simple-struct-slot))
547 "Return a form to get the value of a slot from PTR."
548 `(mem-ref ,ptr ',(slot-type slot) ,(slot-offset slot)))
550 (defmethod (setf foreign-struct-slot-value) (value ptr (slot simple-struct-slot))
551 "Set the value of a simple SLOT to VALUE in PTR."
552 (setf (mem-ref ptr (slot-type slot) (slot-offset slot)) value))
554 (defmethod foreign-struct-slot-set-form (value ptr (slot simple-struct-slot))
555 "Return a form to set the value of a simple structure slot."
556 `(setf (mem-ref ,ptr ',(slot-type slot) ,(slot-offset slot)) ,value))
558 ;;;### Aggregate Slots
560 (defclass aggregate-struct-slot (foreign-struct-slot)
561 ((count :initarg :count :accessor slot-count))
562 (:documentation "Aggregate structure slots."))
564 ;;; Since MEM-REF returns a pointer for struct types we are able to
565 ;;; chain together slot names when accessing slot values in nested
566 ;;; structures.
567 (defmethod foreign-struct-slot-value (ptr (slot aggregate-struct-slot))
568 "Return a pointer to SLOT relative to PTR."
569 (convert-from-foreign (inc-pointer ptr (slot-offset slot))
570 (slot-type slot)))
572 (defmethod foreign-struct-slot-value-form (ptr (slot aggregate-struct-slot))
573 "Return a form to get the value of SLOT relative to PTR."
574 `(convert-from-foreign (inc-pointer ,ptr ,(slot-offset slot))
575 ',(slot-type slot)))
577 (defmethod translate-aggregate-to-foreign (ptr value (type foreign-struct-type))
578 ;;; FIXME: use the block memory interface instead.
579 (loop for i below (foreign-type-size type)
580 do (%mem-set (%mem-ref value :char i) ptr :char i)))
582 (defmethod (setf foreign-struct-slot-value)
583 (value ptr (slot aggregate-struct-slot))
584 "Set the value of an aggregate SLOT to VALUE in PTR."
585 (translate-aggregate-to-foreign (inc-pointer ptr (slot-offset slot))
586 value
587 (parse-type (slot-type slot))))
589 (defmethod foreign-struct-slot-set-form (value ptr (slot aggregate-struct-slot))
590 "Return a form to get the value of an aggregate SLOT relative to PTR."
591 `(translate-aggregate-to-foreign (inc-pointer ,ptr ,(slot-offset slot))
592 ,value
593 ,(parse-type (slot-type slot))))
595 ;;;## Defining Foreign Structures
597 (defun make-struct-slot (name offset type count)
598 "Make the appropriate type of structure slot."
599 ;; If TYPE is an aggregate type or COUNT is >1, create an
600 ;; AGGREGATE-STRUCT-SLOT, otherwise a SIMPLE-STRUCT-SLOT.
601 (if (or (> count 1) (aggregatep (parse-type type)))
602 (make-instance 'aggregate-struct-slot :offset offset :type type
603 :name name :count count)
604 (make-instance 'simple-struct-slot :offset offset :type type
605 :name name)))
607 (defun parse-deprecated-struct-type (name struct-or-union)
608 (check-type struct-or-union (member :struct :union))
609 (let* ((struct-type-name `(,struct-or-union ,name))
610 (struct-type (parse-type struct-type-name)))
611 (simple-style-warning
612 "bare references to struct types are deprecated. ~
613 Please use ~S or ~S instead."
614 `(:pointer ,struct-type-name) struct-type-name)
615 (make-instance (class-of struct-type)
616 :alignment (alignment struct-type)
617 :size (size struct-type)
618 :slots (slots struct-type)
619 :name (name struct-type)
620 :bare t)))
622 ;;; Regarding structure alignment, the following ABIs were checked:
623 ;;; - System-V ABI: x86, x86-64, ppc, arm, mips and itanium. (more?)
624 ;;; - Mac OS X ABI Function Call Guide: ppc32, ppc64 and x86.
626 ;;; Rules used here:
628 ;;; 1. "An entire structure or union object is aligned on the same
629 ;;; boundary as its most strictly aligned member."
631 ;;; 2. "Each member is assigned to the lowest available offset with
632 ;;; the appropriate alignment. This may require internal
633 ;;; padding, depending on the previous member."
635 ;;; 3. "A structure's size is increased, if necessary, to make it a
636 ;;; multiple of the alignment. This may require tail padding,
637 ;;; depending on the last member."
639 ;;; Special cases from darwin/ppc32's ABI:
640 ;;; http://developer.apple.com/documentation/DeveloperTools/Conceptual/LowLevelABI/index.html
642 ;;; 4. "The embedding alignment of the first element in a data
643 ;;; structure is equal to the element's natural alignment."
645 ;;; 5. "For subsequent elements that have a natural alignment
646 ;;; greater than 4 bytes, the embedding alignment is 4, unless
647 ;;; the element is a vector." (note: this applies for
648 ;;; structures too)
650 ;; FIXME: get a better name for this. --luis
651 (defun get-alignment (type alignment-type firstp)
652 "Return alignment for TYPE according to ALIGNMENT-TYPE."
653 (declare (ignorable firstp))
654 (ecase alignment-type
655 (:normal #-(and darwin ppc)
656 (foreign-type-alignment type)
657 #+(and darwin ppc)
658 (if firstp
659 (foreign-type-alignment type)
660 (min 4 (foreign-type-alignment type))))))
662 (defun adjust-for-alignment (type offset alignment-type firstp)
663 "Return OFFSET aligned properly for TYPE according to ALIGNMENT-TYPE."
664 (let* ((align (get-alignment type alignment-type firstp))
665 (rem (mod offset align)))
666 (if (zerop rem)
667 offset
668 (+ offset (- align rem)))))
670 (defmacro with-tentative-type-definition ((name value namespace) &body body)
671 (once-only (name namespace)
672 `(unwind-protect-case ()
673 (progn
674 (notice-foreign-type ,name ,value ,namespace)
675 ,@body)
676 (:abort (undefine-foreign-type ,name ,namespace)))))
678 (defun notice-foreign-struct-definition (name options slots)
679 "Parse and install a foreign structure definition."
680 (destructuring-bind (&key size (class 'foreign-struct-type))
681 options
682 (let ((struct (make-instance class :name name))
683 (current-offset 0)
684 (max-align 1)
685 (firstp t))
686 (with-tentative-type-definition (name struct :struct)
687 ;; determine offsets
688 (dolist (slotdef slots)
689 (destructuring-bind (slotname type &key (count 1) offset) slotdef
690 (when (eq (canonicalize-foreign-type type) :void)
691 (simple-foreign-type-error type :struct
692 "In struct ~S: void type not allowed in field ~S"
693 name slotdef))
694 (setq current-offset
695 (or offset
696 (adjust-for-alignment type current-offset :normal firstp)))
697 (let* ((slot (make-struct-slot slotname current-offset type count))
698 (align (get-alignment (slot-type slot) :normal firstp)))
699 (setf (gethash slotname (slots struct)) slot)
700 (when (> align max-align)
701 (setq max-align align)))
702 (incf current-offset (* count (foreign-type-size type))))
703 (setq firstp nil))
704 ;; calculate padding and alignment
705 (setf (alignment struct) max-align) ; See point 1 above.
706 (let ((tail-padding (- max-align (rem current-offset max-align))))
707 (unless (= tail-padding max-align) ; See point 3 above.
708 (incf current-offset tail-padding)))
709 (setf (size struct) (or size current-offset))))))
711 (defun generate-struct-accessors (name conc-name slot-names)
712 (loop with pointer-arg = (symbolicate '#:pointer-to- name)
713 for slot in slot-names
714 for accessor = (symbolicate conc-name slot)
715 collect `(defun ,accessor (,pointer-arg)
716 (foreign-slot-value ,pointer-arg '(:struct ,name) ',slot))
717 collect `(defun (setf ,accessor) (value ,pointer-arg)
718 (foreign-slot-set value ,pointer-arg '(:struct ,name) ',slot))))
720 (define-parse-method :struct (name)
721 (funcall (find-type-parser name :struct)))
723 (defvar *defcstruct-hook* nil)
725 (defmacro defcstruct (name-and-options &body fields)
726 "Define the layout of a foreign structure."
727 (discard-docstring fields)
728 (destructuring-bind (name . options)
729 (ensure-list name-and-options)
730 (let ((conc-name (getf options :conc-name)))
731 (remf options :conc-name)
732 (unless (getf options :class) (setf (getf options :class) (symbolicate name '-tclass)))
733 `(eval-when (:compile-toplevel :load-toplevel :execute)
734 ;; m-f-s-t could do with this with mop:ensure-class.
735 ,(when-let (class (getf options :class))
736 `(defclass ,class (foreign-struct-type
737 translatable-foreign-type)
738 ()))
739 (notice-foreign-struct-definition ',name ',options ',fields)
740 ,@(when conc-name
741 (generate-struct-accessors name conc-name
742 (mapcar #'car fields)))
743 ,@(when *defcstruct-hook*
744 ;; If non-nil, *defcstruct-hook* should be a function
745 ;; of the arguments that returns NIL or a list of
746 ;; forms to include in the expansion.
747 (apply *defcstruct-hook* name-and-options fields))
748 (define-parse-method ,name ()
749 (parse-deprecated-struct-type ',name :struct))
750 '(:struct ,name)))))
752 ;;;## Accessing Foreign Structure Slots
754 (defun get-slot-info (type slot-name)
755 "Return the slot info for SLOT-NAME or raise an error."
756 (let* ((struct (ensure-parsed-base-type type))
757 (info (gethash slot-name (slots struct))))
758 (unless info
759 (simple-foreign-type-error type :struct
760 "Undefined slot ~A in foreign type ~A."
761 slot-name type))
762 info))
764 (defun foreign-slot-pointer (ptr type slot-name)
765 "Return the address of SLOT-NAME in the structure at PTR."
766 (foreign-struct-slot-pointer ptr (get-slot-info type slot-name)))
768 (define-compiler-macro foreign-slot-pointer (&whole whole ptr type slot-name)
769 (if (and (constantp type) (constantp slot-name))
770 (foreign-struct-slot-pointer-form
771 ptr (get-slot-info (eval type) (eval slot-name)))
772 whole))
774 (defun foreign-slot-type (type slot-name)
775 "Return the type of SLOT in a struct TYPE."
776 (slot-type (get-slot-info type slot-name)))
778 (defun foreign-slot-offset (type slot-name)
779 "Return the offset of SLOT in a struct TYPE."
780 (slot-offset (get-slot-info type slot-name)))
782 (defun foreign-slot-count (type slot-name)
783 "Return the number of items in SLOT in a struct TYPE."
784 (slot-count (get-slot-info type slot-name)))
786 (defun foreign-slot-value (ptr type slot-name)
787 "Return the value of SLOT-NAME in the foreign structure at PTR."
788 (foreign-struct-slot-value ptr (get-slot-info type slot-name)))
790 (define-compiler-macro foreign-slot-value (&whole form ptr type slot-name)
791 "Optimizer for FOREIGN-SLOT-VALUE when TYPE is constant."
792 (if (and (constantp type) (constantp slot-name))
793 (foreign-struct-slot-value-form
794 ptr (get-slot-info (eval type) (eval slot-name)))
795 form))
797 (define-setf-expander foreign-slot-value (ptr type slot-name &environment env)
798 "SETF expander for FOREIGN-SLOT-VALUE."
799 (multiple-value-bind (dummies vals newval setter getter)
800 (get-setf-expansion ptr env)
801 (declare (ignore setter newval))
802 (if (and (constantp type) (constantp slot-name))
803 ;; if TYPE and SLOT-NAME are constant we avoid rebinding them
804 ;; so that the compiler macro on FOREIGN-SLOT-SET works.
805 (with-unique-names (store)
806 (values
807 dummies
808 vals
809 (list store)
810 `(progn
811 (foreign-slot-set ,store ,getter ,type ,slot-name)
812 ,store)
813 `(foreign-slot-value ,getter ,type ,slot-name)))
814 ;; if not...
815 (with-unique-names (store slot-name-tmp type-tmp)
816 (values
817 (list* type-tmp slot-name-tmp dummies)
818 (list* type slot-name vals)
819 (list store)
820 `(progn
821 (foreign-slot-set ,store ,getter ,type-tmp ,slot-name-tmp)
822 ,store)
823 `(foreign-slot-value ,getter ,type-tmp ,slot-name-tmp))))))
825 (defun foreign-slot-set (value ptr type slot-name)
826 "Set the value of SLOT-NAME in a foreign structure."
827 (setf (foreign-struct-slot-value ptr (get-slot-info type slot-name)) value))
829 (define-compiler-macro foreign-slot-set
830 (&whole form value ptr type slot-name)
831 "Optimizer when TYPE and SLOT-NAME are constant."
832 (if (and (constantp type) (constantp slot-name))
833 (foreign-struct-slot-set-form
834 value ptr (get-slot-info (eval type) (eval slot-name)))
835 form))
837 (defmacro with-foreign-slots ((vars ptr type) &body body)
838 "Create local symbol macros for each var in VARS to reference
839 foreign slots in PTR of TYPE. Similar to WITH-SLOTS.
840 Each var can be of the form: slot-name - in which case slot-name will
841 be bound to the value of the slot or: (:pointer slot-name) - in which
842 case slot-name will be bound to the pointer to that slot."
843 (let ((ptr-var (gensym "PTR")))
844 `(let ((,ptr-var ,ptr))
845 (symbol-macrolet
846 ,(loop :for var :in vars
847 :collect
848 (if (listp var)
849 (if (eq (first var) :pointer)
850 `(,(second var) (foreign-slot-pointer
851 ,ptr-var ',type ',(second var)))
852 (error
853 "Malformed slot specification ~a; must be:`name' or `(:pointer name)'"
854 var))
855 `(,var (foreign-slot-value ,ptr-var ',type ',var))))
856 ,@body))))
858 ;;; We could add an option to define a struct instead of a class, in
859 ;;; the unlikely event someone needs something like that.
860 (defmacro define-c-struct-wrapper (class-and-type supers &optional slots)
861 "Define a new class with CLOS slots matching those of a foreign
862 struct type. An INITIALIZE-INSTANCE method is defined which
863 takes a :POINTER initarg that is used to store the slots of a
864 foreign object. This pointer is only used for initialization and
865 it is not retained.
867 CLASS-AND-TYPE is either a list of the form (class-name
868 struct-type) or a single symbol naming both. The class will
869 inherit SUPERS. If a list of SLOTS is specified, only those
870 slots will be defined and stored."
871 (destructuring-bind (class-name &optional (struct-type (list :struct class-name)))
872 (ensure-list class-and-type)
873 (let ((slots (or slots (foreign-slot-names struct-type))))
874 `(progn
875 (defclass ,class-name ,supers
876 ,(loop for slot in slots collect
877 `(,slot :reader ,(format-symbol t "~A-~A" class-name slot))))
878 ;; This could be done in a parent class by using
879 ;; FOREIGN-SLOT-NAMES when instantiating but then the compiler
880 ;; macros wouldn't kick in.
881 (defmethod initialize-instance :after ((inst ,class-name) &key pointer)
882 (with-foreign-slots (,slots pointer ,struct-type)
883 ,@(loop for slot in slots collect
884 `(setf (slot-value inst ',slot) ,slot))))
885 ',class-name))))
887 ;;;# Foreign Unions
889 ;;; A union is a subclass of FOREIGN-STRUCT-TYPE in which all slots
890 ;;; have an offset of zero.
892 ;;; See also the notes regarding ABI requirements in
893 ;;; NOTICE-FOREIGN-STRUCT-DEFINITION
894 (defun notice-foreign-union-definition (name-and-options slots)
895 "Parse and install a foreign union definition."
896 (destructuring-bind (name &key size)
897 (ensure-list name-and-options)
898 (let ((union (make-instance 'foreign-union-type :name name))
899 (max-size 0)
900 (max-align 0))
901 (with-tentative-type-definition (name union :union)
902 (dolist (slotdef slots)
903 (destructuring-bind (slotname type &key (count 1)) slotdef
904 (when (eq (canonicalize-foreign-type type) :void)
905 (simple-foreign-type-error name :struct
906 "In union ~S: void type not allowed in field ~S"
907 name slotdef))
908 (let* ((slot (make-struct-slot slotname 0 type count))
909 (size (* count (foreign-type-size type)))
910 (align (foreign-type-alignment (slot-type slot))))
911 (setf (gethash slotname (slots union)) slot)
912 (when (> size max-size)
913 (setf max-size size))
914 (when (> align max-align)
915 (setf max-align align)))))
916 (setf (size union) (or size max-size))
917 (setf (alignment union) max-align)))))
919 (define-parse-method :union (name)
920 (funcall (find-type-parser name :union)))
922 (defmacro defcunion (name-and-options &body fields)
923 "Define the layout of a foreign union."
924 (discard-docstring fields)
925 (destructuring-bind (name &key size)
926 (ensure-list name-and-options)
927 (declare (ignore size))
928 `(eval-when (:compile-toplevel :load-toplevel :execute)
929 (notice-foreign-union-definition ',name-and-options ',fields)
930 (define-parse-method ,name ()
931 (parse-deprecated-struct-type ',name :union))
932 '(:union ,name))))
934 ;;;# Operations on Types
936 (defmethod foreign-type-alignment (type)
937 "Return the alignment in bytes of a foreign type."
938 (foreign-type-alignment (parse-type type)))
940 (defmacro with-foreign-object ((var type &optional (count 1)) &body body)
941 "Bind VAR to a pointer to COUNT objects of TYPE during BODY.
942 The buffer has dynamic extent and may be stack allocated."
943 `(with-foreign-pointer
944 (,var ,(if (constantp type)
945 ;; with-foreign-pointer may benefit from constant folding:
946 (if (constantp count)
947 (* (eval count) (foreign-type-size (eval type)))
948 `(* ,count ,(foreign-type-size (eval type))))
949 `(* ,count (foreign-type-size ,type))))
950 ,@body))
952 (defmacro with-foreign-objects (bindings &body body)
953 (if bindings
954 `(with-foreign-object ,(car bindings)
955 (with-foreign-objects ,(cdr bindings)
956 ,@body))
957 `(progn ,@body)))
959 ;;;## Anonymous Type Translators
961 ;;; (:wrapper :to-c some-function :from-c another-function)
963 ;;; TODO: We will need to add a FREE function to this as well I think.
964 ;;; --james
966 (define-foreign-type foreign-type-wrapper ()
967 ((to-c :initarg :to-c :reader wrapper-to-c)
968 (from-c :initarg :from-c :reader wrapper-from-c))
969 (:documentation "Wrapper type."))
971 (define-parse-method :wrapper (base-type &key to-c from-c)
972 (make-instance 'foreign-type-wrapper
973 :actual-type (parse-type base-type)
974 :to-c (or to-c 'identity)
975 :from-c (or from-c 'identity)))
977 (defmethod translate-to-foreign (value (type foreign-type-wrapper))
978 (translate-to-foreign
979 (funcall (slot-value type 'to-c) value) (actual-type type)))
981 (defmethod translate-from-foreign (value (type foreign-type-wrapper))
982 (funcall (slot-value type 'from-c)
983 (translate-from-foreign value (actual-type type))))
985 ;;;# Other types
987 ;;; Boolean type. Maps to an :int by default. Only accepts integer types.
988 (define-foreign-type foreign-boolean-type ()
991 (define-parse-method :boolean (&optional (base-type :int))
992 (make-instance
993 'foreign-boolean-type :actual-type
994 (ecase (canonicalize-foreign-type base-type)
995 ((:char :unsigned-char :int :unsigned-int :long :unsigned-long
996 #-cffi-sys::no-long-long :long-long
997 #-cffi-sys::no-long-long :unsigned-long-long) base-type))))
999 (defmethod translate-to-foreign (value (type foreign-boolean-type))
1000 (if value 1 0))
1002 (defmethod translate-from-foreign (value (type foreign-boolean-type))
1003 (not (zerop value)))
1005 (defmethod expand-to-foreign (value (type foreign-boolean-type))
1006 "Optimization for the :boolean type."
1007 (if (constantp value)
1008 (if (eval value) 1 0)
1009 `(if ,value 1 0)))
1011 (defmethod expand-from-foreign (value (type foreign-boolean-type))
1012 "Optimization for the :boolean type."
1013 (if (constantp value) ; very unlikely, heh
1014 (not (zerop (eval value)))
1015 `(not (zerop ,value))))
1017 ;;; Boolean type that represents C99 _Bool
1018 (defctype :bool (:boolean :char))
1020 ;;;# Typedefs for built-in types.
1022 (defctype :uchar :unsigned-char)
1023 (defctype :ushort :unsigned-short)
1024 (defctype :uint :unsigned-int)
1025 (defctype :ulong :unsigned-long)
1026 (defctype :llong :long-long)
1027 (defctype :ullong :unsigned-long-long)
1029 ;;; We try to define the :[u]int{8,16,32,64} types by looking at
1030 ;;; the sizes of the built-in integer types and defining typedefs.
1031 (eval-when (:compile-toplevel :load-toplevel :execute)
1032 (macrolet
1033 ((match-types (sized-types mtypes)
1034 `(progn
1035 ,@(loop for (type . size-or-type) in sized-types
1036 for m = (car (member (if (keywordp size-or-type)
1037 (foreign-type-size size-or-type)
1038 size-or-type)
1039 mtypes :key #'foreign-type-size))
1040 when m collect `(defctype ,type ,m)))))
1041 ;; signed
1042 (match-types ((:int8 . 1) (:int16 . 2) (:int32 . 4) (:int64 . 8)
1043 (:intptr . :pointer))
1044 (:char :short :int :long :long-long))
1045 ;; unsigned
1046 (match-types ((:uint8 . 1) (:uint16 . 2) (:uint32 . 4) (:uint64 . 8)
1047 (:uintptr . :pointer))
1048 (:unsigned-char :unsigned-short :unsigned-int :unsigned-long
1049 :unsigned-long-long))))