r1740: Added authorship for John DeSoi
[uffi.git] / src / mcl / primitives.cl
1 ;;;; -*- Mode: ANSI-Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
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
9 ;;;;
10 ;;;; $Id: primitives.cl,v 1.2 2002/04/06 19:11:15 kevin Exp $
11 ;;;;
12 ;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg
13 ;;;; and John DeSoi
14 ;;;;
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 ;;;; *************************************************************************
19
20 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
21 (in-package :uffi)
22
23
24 (defmacro def-constant (name value &key (export nil))
25   "Macro to define a constant and to export it"
26   `(eval-when (:compile-toplevel :load-toplevel :execute)
27      (defconstant ,name ,value)
28      ,(if export (list 'export `(quote ,name)) (values))))
29
30 (defmacro def-type (name type)
31   "Generates a (deftype) statement for CL. Currently, only CMUCL
32 supports takes advantage of this optimization."
33   (declare (ignore type))
34   `(deftype ,name () t))
35
36 (defmacro null-char-p (val)
37   "Returns T if character is NULL"
38   `(zerop ,val))
39       
40 (defmacro def-foreign-type (name type)
41  `(ccl::def-mactype ,name ,(ccl:find-mactype (convert-from-uffi-type type :type))))
42
43 (eval-when (:compile-toplevel :load-toplevel :execute)
44   (defvar +type-conversion-hash+ (make-hash-table :size 20)))
45
46
47 (defconstant +type-conversion-list+ 
48     '((* . :pointer) (:void . :void)
49       (:short . :short)
50       (:pointer-void . :pointer)
51       (:cstring . :string) 
52       (:char . :character)
53       (:unsigned-char . :unsigned-byte)
54       (:byte . :byte)
55       (:int . :integer) (:unsigned-int . :unsigned-integer) 
56       (:long . :long) (:unsigned-long . :unsigned-long)
57       (:float . :single-float) (:double . :double-float)
58       (:array . :array)))
59
60 (dolist (type +type-conversion-list+)
61   (setf (gethash (car type) +type-conversion-hash+) (cdr type)))
62
63
64 (defmethod ph (&optional (os *standard-output*))
65   (maphash #'(lambda (k v) (format os "~&~S => ~S" k v)) +type-conversion-hash+))
66
67 (defun convert-from-uffi-type (type context)
68   "Converts from a uffi type to an implementation specific type"
69   (if (atom type)
70       (cond
71        #+mcl
72        ((and (eq type :void) (eq context :return)) nil)
73        (t
74         (let ((found-type (gethash type +type-conversion-hash+)))
75           (if found-type
76               found-type
77             type))))
78     (cons (convert-from-uffi-type (first type) context) 
79           (convert-from-uffi-type (rest type) context))))
80
81
82
83
84
85