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