r10608: update license
[uffi.git] / src / primitives.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          primitives.lisp
6 ;;;; Purpose:       UFFI source to handle immediate types
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Feb 2002
9 ;;;;
10 ;;;; $Id$
11 ;;;;
12 ;;;; This file, part of UFFI, is Copyright (c) 2002-2005 by Kevin M. Rosenberg
13 ;;;;
14 ;;;; *************************************************************************
15
16 (in-package #:uffi)
17
18 #+mcl
19 (defvar *keyword-package* (find-package "KEYWORD"))
20
21 #+mcl
22 ; MCL and OpenMCL expect a lot of FFI elements to be keywords (e.g. struct field names in OpenMCL)
23 ; So this provides a function to convert any quoted symbols to keywords.
24 (defun keyword (obj)
25   (cond ((keywordp obj) 
26          obj)
27         ((null obj)
28          nil)
29         ((symbolp obj)
30          (intern (symbol-name obj) *keyword-package*))
31         ((and (listp obj) (eq (car obj) 'cl:quote))
32          (keyword (cadr obj)))
33         ((stringp obj)
34          (intern obj *keyword-package*))
35         (t 
36          obj)))
37
38 ; Wrapper for unexported function we have to use
39 #+(and mcl (not openmcl))
40 (defmacro def-mcl-type (name type)
41   `(ccl::def-mactype ,(keyword name) (ccl:find-mactype ,type)))
42
43 (defmacro def-constant (name value &key (export nil))
44   "Macro to define a constant and to export it"
45   `(eval-when (:compile-toplevel :load-toplevel :execute)
46      (defconstant ,name ,value)
47      ,(when export (list 'export `(quote ,name)))
48     ',name))
49
50 (defmacro def-type (name type)
51   "Generates a (deftype) statement for CL. Currently, only CMUCL
52 supports takes advantage of this optimization."
53   #+(or lispworks allegro mcl cormanlisp)  (declare (ignore type))
54   #+(or lispworks allegro mcl cormanlisp) `(deftype ,name () t)
55   #+(or cmu scl)
56   `(deftype ,name () '(alien:alien ,(convert-from-uffi-type type :declare)))
57   #+sbcl
58   `(deftype ,name () '(sb-alien:alien ,(convert-from-uffi-type type :declare)))
59   )
60
61 (defmacro null-char-p (val)
62   "Returns T if character is NULL"
63   `(zerop ,val))
64       
65 (defmacro def-foreign-type (name type)
66   #+lispworks `(fli:define-c-typedef ,name ,(convert-from-uffi-type type :type))
67   #+allegro `(ff:def-foreign-type ,name ,(convert-from-uffi-type type :type))
68   #+(or cmu scl) `(alien:def-alien-type ,name ,(convert-from-uffi-type type :type))
69   #+sbcl `(sb-alien:define-alien-type ,name ,(convert-from-uffi-type type :type))
70   #+cormanlisp `(ct:defctype ,name ,(convert-from-uffi-type type :type))
71   #+mcl
72   (let ((mcl-type (convert-from-uffi-type type :type)))
73     (unless (or (keywordp mcl-type) (consp mcl-type))
74       (setf mcl-type `(quote ,mcl-type)))
75     #+(and mcl (not openmcl))
76     `(def-mcl-type ,(keyword name) ,mcl-type)
77     #+openmcl
78     `(ccl::def-foreign-type ,(keyword name) ,mcl-type))  
79   )
80
81 (eval-when (:compile-toplevel :load-toplevel :execute)
82   (defvar +type-conversion-hash+ (make-hash-table :size 20 :test #'eq))
83   #+(or cmu sbcl scl) (defvar *cmu-def-type-hash*
84                         (make-hash-table :size 20 :test #'eq))
85   )
86
87 #+(or cmu scl)
88 (defvar *cmu-sbcl-def-type-list*
89     '((:char . (alien:signed 8))
90       (:unsigned-char . (alien:unsigned 8))
91       (:byte . (alien:signed 8))
92       (:unsigned-byte . (alien:unsigned 8))
93       (:short . (alien:signed 16))
94       (:unsigned-short . (alien:unsigned 16))
95       (:int . (alien:signed 32))
96       (:unsigned-int . (alien:unsigned 32))
97       #-x86-64 (:long . (alien:signed 32))
98       #-x86-64 (:unsigned-long . (alien:unsigned 32))
99       #+x86-64 (:long . (alien:signed 64))
100       #+x86-64 (:unsigned-long . (alien:unsigned 64))
101       (:float . alien:single-float)
102       (:double . alien:double-float)
103       (:void . t)
104       )
105   "Conversions in CMUCL for def-foreign-type are different than in def-function")
106
107 #+sbcl
108 (defvar *cmu-sbcl-def-type-list*
109     '((:char . (sb-alien:signed 8))
110       (:unsigned-char . (sb-alien:unsigned 8))
111       (:byte . (sb-alien:signed 8))
112       (:unsigned-byte . (sb-alien:unsigned 8))
113       (:short . (sb-alien:signed 16))
114       (:unsigned-short . (sb-alien:unsigned 16))
115       (:int . (sb-alien:signed 32))
116       (:unsigned-int . (sb-alien:unsigned 32))
117       #-x86-64 (:long . (sb-alien:signed 32))
118       #-x86-64 (:unsigned-long . (sb-alien:unsigned 32))
119       #+x86-64 (:long . (sb-alien:signed 64))
120       #+x86-64 (:unsigned-long . (sb-alien:unsigned 64))
121       (:float . sb-alien:single-float)
122       (:double . sb-alien:double-float)
123       (:void . t)
124       )
125   "Conversions in SBCL for def-foreign-type are different than in def-function")
126
127 (defvar *type-conversion-list* nil)
128
129 #+(or cmu scl)
130 (setq *type-conversion-list*
131     '((* . *) (:void . c-call:void) 
132       (:pointer-void . (* t))
133       (:cstring . c-call:c-string)
134       (:char . c-call:char) 
135       (:unsigned-char . (alien:unsigned 8))
136       (:byte . (alien:signed 8))
137       (:unsigned-byte . (alien:unsigned 8))
138       (:short . c-call:short)
139       (:unsigned-short . c-call:unsigned-short)
140       (:int . alien:integer) (:unsigned-int . c-call:unsigned-int) 
141       (:long . c-call:long) (:unsigned-long . c-call:unsigned-long)
142       (:float . c-call:float) (:double . c-call:double)
143       (:array . alien:array)))
144
145 #+sbcl
146 (setq *type-conversion-list*
147     '((* . *) (:void . sb-alien:void) 
148       (:pointer-void . (* t))
149       #-sb-unicode(:cstring . sb-alien:c-string)
150       #+sb-unicode(:cstring . sb-alien:utf8-string)
151       (:char . sb-alien:char) 
152       (:unsigned-char . (sb-alien:unsigned 8))
153       (:byte . (sb-alien:signed 8))
154       (:unsigned-byte . (sb-alien:unsigned 8))
155       (:short . sb-alien:short)
156       (:unsigned-short . sb-alien:unsigned-short)
157       (:int . sb-alien:int) (:unsigned-int . sb-alien:unsigned-int) 
158       (:long . sb-alien:long) (:unsigned-long . sb-alien:unsigned-long)
159       (:float . sb-alien:float) (:double . sb-alien:double)
160       (:array . sb-alien:array)))
161
162 #+(or allegro cormanlisp)
163 (setq *type-conversion-list*
164     '((* . *) (:void . :void)
165       (:short . :short)
166       (:pointer-void . (* :void))
167       (:cstring . (* :unsigned-char))
168       (:byte . :char)
169       (:unsigned-byte . :unsigned-char)
170       (:char . :char)
171       (:unsigned-char . :unsigned-char)
172       (:int . :int) (:unsigned-int . :unsigned-int) 
173       (:long . :long) (:unsigned-long . :unsigned-long)
174       (:float . :float) (:double . :double)
175       (:array . :array)))
176
177 #+lispworks
178 (setq *type-conversion-list*
179     '((* . :pointer) (:void . :void) 
180       (:short . :short)
181       (:pointer-void . (:pointer :void))
182       (:cstring . (:reference-pass (:ef-mb-string :external-format
183                                     (:latin-1 :eol-style :lf))
184                    :allow-null t))
185       (:cstring-returning . (:reference (:ef-mb-string :external-format
186                                          (:latin-1 :eol-style :lf))
187                              :allow-null t))
188       (:byte . :byte)
189       (:unsigned-byte . (:unsigned :byte))
190       (:char . :char)
191       (:unsigned-char . (:unsigned :char))
192       (:int . :int) (:unsigned-int . (:unsigned :int))
193       (:long . :long) (:unsigned-long . (:unsigned :long))
194       (:float . :float) (:double . :double)
195       (:array . :c-array)))
196
197 #+(and mcl (not openmcl))
198 (setq *type-conversion-list*
199      '((* . :pointer) (:void . :void)
200        (:short . :short) (:unsigned-short . :unsigned-short)
201        (:pointer-void . :pointer)
202        (:cstring . :string)
203        (:char . :character)
204        (:unsigned-char . :unsigned-byte)
205        (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte)
206        (:int . :long) (:unsigned-int . :unsigned-long)
207        (:long . :long) (:unsigned-long . :unsigned-long)
208        (:float . :single-float) (:double . :double-float)
209        (:array . :array)))
210
211 #+openmcl
212 (setq *type-conversion-list*
213      '((* . :address) (:void . :void)
214        (:short . :short) (:unsigned-short . :unsigned-short)
215        (:pointer-void . :address)
216        (:cstring . :address)
217        (:char . :signed-char)
218        (:unsigned-char . :unsigned-char)
219        (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte)
220        (:int . :int) (:unsigned-int . :unsigned-int)
221        (:long . :long) (:unsigned-long . :unsigned-long)
222        (:long-long . :signed-doubleword) (:unsigned-long-long . :unsigned-doubleword)
223        (:float . :single-float) (:double . :double-float)
224        (:array . :array)))
225
226 (dolist (type *type-conversion-list*)
227   (setf (gethash (car type) +type-conversion-hash+) (cdr type)))
228
229 #+(or cmu sbcl scl)
230 (dolist (type *cmu-sbcl-def-type-list*)
231   (setf (gethash (car type) *cmu-def-type-hash*) (cdr type)))
232
233 (defun basic-convert-from-uffi-type (type)
234   (let ((found-type (gethash type +type-conversion-hash+)))
235     (if found-type
236         found-type
237       #-mcl type
238       #+mcl (keyword type))))
239
240 (defun %convert-from-uffi-type (type context)
241   "Converts from a uffi type to an implementation specific type"
242   (if (atom type)
243       (cond
244        #+(or allegro cormanlisp)
245        ((and (or (eq context :routine) (eq context :return))
246              (eq type :cstring))
247         (setq type '((* :char) integer)))
248        #+(or cmu sbcl scl)
249        ((eq context :type)
250         (let ((cmu-type (gethash type *cmu-def-type-hash*)))
251           (if cmu-type
252               cmu-type
253               (basic-convert-from-uffi-type type))))
254        #+lispworks
255        ((and (eq context :return)
256              (eq type :cstring))
257         (basic-convert-from-uffi-type :cstring-returning))
258        #+(and mcl (not openmcl))
259        ((and (eq type :void) (eq context :return)) nil)
260        (t
261         (basic-convert-from-uffi-type type)))
262     (let ((sub-type (car type)))
263       (case sub-type
264         (cl:quote
265          (convert-from-uffi-type (cadr type) context))
266         (:struct-pointer
267          #+mcl `(:* (:struct ,(%convert-from-uffi-type (cadr type) :struct)))
268          #-mcl (%convert-from-uffi-type (list '* (cadr type)) :struct)
269          )
270         (:struct
271          #+mcl `(:struct ,(%convert-from-uffi-type (cadr type) :struct))
272          #-mcl (%convert-from-uffi-type (cadr type) :struct)
273          )
274        (:union
275         #+mcl `(:union ,(%convert-from-uffi-type (cadr type) :union))
276         #-mcl (%convert-from-uffi-type (cadr type) :union)
277         )
278        (t
279         (cons (%convert-from-uffi-type (first type) context) 
280               (%convert-from-uffi-type (rest type) context)))))))
281
282 (defun convert-from-uffi-type (type context)
283   (let ((result (%convert-from-uffi-type type context)))
284     (cond
285      ((atom result) result)
286      #+openmcl
287      ((eq (car result) :address)
288       (if (eq context :struct)
289           (append '(:*) (cdr result))
290         :address))
291      #+(and mcl (not openmcl))
292      ((and (eq (car result) :pointer) (eq context :allocation) :pointer))
293      (t result))))
294
295 (eval-when (:compile-toplevel :load-toplevel :execute)
296   (when (char= #\a (schar (symbol-name '#:a) 0))
297     (pushnew :uffi-lowercase-reader *features*))
298   (when (not (string= (symbol-name '#:a)
299                       (symbol-name '#:A)))
300     (pushnew :uffi-case-sensitive *features*)))
301
302 (defun make-lisp-name (name)
303   (let ((converted (substitute #\- #\_ name)))
304      (intern 
305       #+uffi-case-sensitive converted
306       #+(and (not uffi-lowercase-reader) (not uffi-case-sensitive)) (string-upcase converted)
307       #+(and uffi-lowercase-reader (not uffi-case-sensitive)) (string-downcase converted))))
308
309 (eval-when (:compile-toplevel :load-toplevel :execute)
310   (setq cl:*features* (delete :uffi-lowercase-reader *features*))
311   (setq cl:*features* (delete :uffi-case-sensitive *features*)))