1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: primitives.cl
6 ;;;; Purpose: UFFI source to handle immediate types
7 ;;;; Programmers: Kevin M. Rosenberg and John DeSoi
8 ;;;; Date Started: Feb 2002
10 ;;;; $Id: primitives.cl,v 1.4 2002/04/23 00:35:47 desoi Exp $
12 ;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg
15 ;;;; UFFI users are granted the rights to distribute and use this software
16 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
17 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
18 ;;;; *************************************************************************
20 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
23 ; Wrapper for unexported function we have to use
24 (defmacro def-mcl-type (name type)
25 `(ccl::def-mactype (quote ,name) (ccl:find-mactype ,type)))
28 (defmacro def-constant (name value &key (export nil))
29 "Macro to define a constant and to export it"
30 `(eval-when (:compile-toplevel :load-toplevel :execute)
31 (defconstant ,name ,value)
32 ,(if export (list 'export `(quote ,name)) (values))))
34 (defmacro def-type (name type)
35 "Generates a (deftype) statement for CL. Currently, only CMUCL
36 supports takes advantage of this optimization."
37 (declare (ignore type))
38 `(deftype ,name () t))
40 (defmacro null-char-p (val)
41 "Returns T if character is NULL"
45 (defmacro def-foreign-type (name type)
46 `(def-mcl-type ,name (convert-from-uffi-type ,type :type)))
49 (eval-when (:compile-toplevel :load-toplevel :execute)
50 (defvar +type-conversion-hash+ (make-hash-table :size 20)))
53 (defconstant +type-conversion-list+
54 '((* . :pointer) (:void . :void)
56 (:pointer-void . :pointer)
59 (:unsigned-char . :unsigned-byte)
61 (:int . :integer) (:unsigned-int . :unsigned-integer)
62 (:long . :long) (:unsigned-long . :unsigned-long)
63 (:float . :single-float) (:double . :double-float)
66 (dolist (type +type-conversion-list+)
67 (setf (gethash (car type) +type-conversion-hash+) (cdr type)))
70 (defmethod ph (&optional (os *standard-output*))
71 (maphash #'(lambda (k v) (format os "~&~S => ~S" k v)) +type-conversion-hash+))
73 (defun convert-from-uffi-type (type context)
74 "Converts from a uffi type to an implementation specific type"
78 ((and (eq type :void) (eq context :return)) nil)
80 (let ((found-type (gethash type +type-conversion-hash+)))
84 (cons (convert-from-uffi-type (first type) context)
85 (convert-from-uffi-type (rest type) context))))