6e49d87bfee409268c8df6c8720bf68627f4743e
[uffi.git] / src-mcl / primitives.cl
1 ;;;; -*- Mode: 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.1 2002/09/16 17:57:43 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 ; 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)))
26
27
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))))
33
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))
39
40 (defmacro null-char-p (val)
41   "Returns T if character is NULL"
42   `(zerop ,val))
43       
44
45 (defmacro def-foreign-type (name type)
46  `(def-mcl-type ,name (convert-from-uffi-type ,type :type)))
47
48
49 (eval-when (:compile-toplevel :load-toplevel :execute)
50   (defvar +type-conversion-hash+ (make-hash-table :size 20)))
51
52
53 (defconstant +type-conversion-list+ 
54     '((* . :pointer) (:void . :void)
55       (:short . :short)
56       (:pointer-void . :pointer)
57       (:cstring . :string) 
58       (:char . :character)
59       (:unsigned-char . :unsigned-byte)
60       (:byte . :byte)
61       (:int . :integer) (:unsigned-int . :unsigned-integer) 
62       (:long . :long) (:unsigned-long . :unsigned-long)
63       (:float . :single-float) (:double . :double-float)
64       (:array . :array)))
65
66 (dolist (type +type-conversion-list+)
67   (setf (gethash (car type) +type-conversion-hash+) (cdr type)))
68
69
70 (defmethod ph (&optional (os *standard-output*))
71   (maphash #'(lambda (k v) (format os "~&~S => ~S" k v)) +type-conversion-hash+))
72
73 (defun convert-from-uffi-type (type context)
74   "Converts from a uffi type to an implementation specific type"
75   (if (atom type)
76       (cond
77        #+mcl
78        ((and (eq type :void) (eq context :return)) nil)
79        (t
80         (let ((found-type (gethash type +type-conversion-hash+)))
81           (if found-type
82               found-type
83             type))))
84     (cons (convert-from-uffi-type (first type) context) 
85           (convert-from-uffi-type (rest type) context))))
86
87
88
89
90
91