r2784: *** empty log message ***
[uffi.git] / src-mcl / primitives.cl
index 6e49d87bfee409268c8df6c8720bf68627f4743e..0f85ab16768c97d0a247bf6f845d787b76082936 100644 (file)
@@ -7,7 +7,7 @@
 ;;;; Programmers:   Kevin M. Rosenberg and John DeSoi
 ;;;; Date Started:  Feb 2002
 ;;;;
-;;;; $Id: primitives.cl,v 1.1 2002/09/16 17:57:43 kevin Exp $
+;;;; $Id: primitives.cl,v 1.2 2002/09/20 04:51:14 kevin Exp $
 ;;;;
 ;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg
 ;;;; and John DeSoi
 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
 (in-package :uffi)
 
+
+(defvar *keyword-package* (find-package "KEYWORD"))
+
+; MCL and OpenMCL expect a lot of FFI elements to be keywords (e.g. struct field names in OpenMCL)
+; So this provides a function to convert any quoted symbols to keywords.
+(defun keyword (obj)
+  (cond ((keywordp obj) 
+         obj)
+        ((null obj)
+         nil)
+        ((symbolp obj)
+         (intern (symbol-name obj) *keyword-package*))
+        ((and (listp obj) (eq (car obj) 'cl:quote))
+         (keyword (cadr obj)))
+        ((stringp obj)
+         (intern obj *keyword-package*))
+        (t 
+         obj)))
+
+
 ; Wrapper for unexported function we have to use
+#-openmcl
 (defmacro def-mcl-type (name type)
-  `(ccl::def-mactype (quote ,name) (ccl:find-mactype ,type)))
+  `(ccl::def-mactype ,(keyword name) (ccl:find-mactype ,type)))
 
 
 (defmacro def-constant (name value &key (export nil))
@@ -42,14 +63,20 @@ supports takes advantage of this optimization."
   `(zerop ,val))
       
 
-(defmacro def-foreign-type (name type)
- `(def-mcl-type ,name (convert-from-uffi-type ,type :type)))
+(defmacro def-foreign-type (name uffi-type)
+  (let ((type (convert-from-uffi-type uffi-type :type)))
+    (unless (keywordp type)
+      (setf type `(quote ,type)))
+    #-openmcl
+    `(def-mcl-type ,(keyword name) ,type)
+    #+openmcl
+    `(ccl::def-foreign-type ,(keyword name) ,type)))
 
 
 (eval-when (:compile-toplevel :load-toplevel :execute)
   (defvar +type-conversion-hash+ (make-hash-table :size 20)))
 
-
+#-openmcl
 (defconstant +type-conversion-list+ 
     '((* . :pointer) (:void . :void)
       (:short . :short)
@@ -57,35 +84,60 @@ supports takes advantage of this optimization."
       (:cstring . :string) 
       (:char . :character)
       (:unsigned-char . :unsigned-byte)
-      (:byte . :byte)
+      (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte)
       (:int . :integer) (:unsigned-int . :unsigned-integer) 
       (:long . :long) (:unsigned-long . :unsigned-long)
       (:float . :single-float) (:double . :double-float)
       (:array . :array)))
 
+#+openmcl
+(defconstant +type-conversion-list+ 
+    '((* . :address) (:void . :void)
+      (:short . :short)
+      (:pointer-void . :address)
+      (:cstring . :address) 
+      (:char . :signed-char)
+      (:unsigned-char . :unsigned-char)
+      (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte)
+      (:int . :int) (:unsigned-int . :unsigned-int) 
+      (:long . :long) (:unsigned-long . :unsigned-long)
+      (:long-long . :signed-doubleword) (:unsigned-long-long . :unsigned-doubleword) 
+      (:float . :single-float) (:double . :double-float)
+      (:array . :array)))
+
 (dolist (type +type-conversion-list+)
   (setf (gethash (car type) +type-conversion-hash+) (cdr type)))
 
 
 (defmethod ph (&optional (os *standard-output*))
   (maphash #'(lambda (k v) (format os "~&~S => ~S" k v)) +type-conversion-hash+))
-
-(defun convert-from-uffi-type (type context)
-  "Converts from a uffi type to an implementation specific type"
-  (if (atom type)
-      (cond
-       #+mcl
-       ((and (eq type :void) (eq context :return)) nil)
-       (t
-       (let ((found-type (gethash type +type-conversion-hash+)))
-         (if found-type
-             found-type
-           type))))
-    (cons (convert-from-uffi-type (first type) context) 
-         (convert-from-uffi-type (rest type) context))))
-
-
+        
 
 
+(defun basic-convert-from-uffi-type (type)
+  (let ((found-type (gethash type +type-conversion-hash+)))
+    (if found-type
+       found-type
+       (keyword type))))
 
+(defun %convert-from-uffi-type (type context)
+  "Converts from a uffi type to an implementation specific type"
+  (if (atom type)
+    (cond
+     #-openmcl
+     ((and (eq type :void) (eq context :return)) nil)
+     (t (basic-convert-from-uffi-type type)))
+    (if (eq (car type) 'cl:quote)
+      (%convert-from-uffi-type (cadr type) context)
+      (cons (%convert-from-uffi-type (first type) context) 
+            (%convert-from-uffi-type (rest type) context)))))
 
+(defun convert-from-uffi-type (type context)
+  (let ((result (%convert-from-uffi-type type context)))
+    (cond
+     ((atom result) result)
+     #+openmcl
+     ((eq (car result) :address) :address)
+     #-openmcl
+     ((and (eq (car result) :pointer) (eq context :allocation) :pointer))
+     (t result))))