5ec2cd34dc31e4e8da748979eca43cd513f1a19f
[uffi.git] / src / 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 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Feb 2002
9 ;;;;
10 ;;;; $Id: primitives.cl,v 1.18 2002/05/17 16:51:08 kevin 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 (defmacro def-constant (name value &key (export nil))
23   "Macro to define a constant and to export it"
24   `(eval-when (:compile-toplevel :load-toplevel :execute)
25      (defconstant ,name ,value)
26      ,(when export (list 'export `(quote ,name)))
27     ',name))
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   #+(or lispworks allegro)
33   (declare (ignore type))
34   #+(or lispworks allegro)
35   `(deftype ,name () t)
36   #+cmu
37   `(deftype ,name () '(alien:alien ,(convert-from-uffi-type type :declare)))
38   )
39
40 (defmacro null-char-p (val)
41   "Returns T if character is NULL"
42   `(zerop ,val))
43       
44 (defmacro def-foreign-type (name type)
45   #+lispworks `(fli:define-c-typedef ,name ,(convert-from-uffi-type type :type))
46   #+allegro `(ff:def-foreign-type ,name ,(convert-from-uffi-type type :type))
47   #+cmu `(alien:def-alien-type ,name ,(convert-from-uffi-type type :type))
48   )
49
50 (eval-when (:compile-toplevel :load-toplevel :execute)
51   (defvar +type-conversion-hash+ (make-hash-table :size 20))
52   #+cmu (defvar +cmu-def-type-hash+ (make-hash-table :size 20))
53   )
54
55 #+cmu
56 (defconstant +cmu-def-type-list+
57     '((:char . (alien:signed 8))
58       (:unsigned-char . (alien:unsigned 8))
59       (:byte . (alien:unsigned 8))
60       (:short . (alien:signed 16))
61       (:unsigned-short . (alien:unsigned 16))
62       (:int . (alien:signed 32))
63       (:unsigned-int . (alien:unsigned 32))
64       (:long . (alien:signed 32))
65       (:unsigned-long . (alien:unsigned 32))
66       (:float . alien:single-float)
67       (:double . alien:double-float)
68       )
69   "Conversions in CMUCL or def-foreign-type are different thatn in def-function")
70
71 (defparameter +type-conversion-list+ nil)
72
73 #+cmu
74 (setq +type-conversion-list+
75     '((* . *) (:void . c-call:void) 
76       (:short . c-call:short)
77       (:pointer-void . (* t))
78       (:cstring . c-call:c-string)
79       (:char . c-call:char) 
80       (:unsigned-char . (alien:unsigned 8))
81       (:byte . (alien:unsigned 8))
82       (:short . c-call:unsigned-short) 
83       (:unsigned-short . c-call:unsigned-short)
84       (:int . alien:integer) (:unsigned-int . c-call:unsigned-int) 
85       (:long . c-call:long) (:unsigned-long . c-call:unsigned-long)
86       (:float . c-call:float) (:double . c-call:double)
87       (:array . alien:array)))
88 #+allegro
89 (setq +type-conversion-list+
90     '((* . *) (:void . :void)
91       (:short . :short)
92       (:pointer-void . (* :void))
93       (:cstring . (* :unsigned-char))
94       (:char . :char)
95       (:unsigned-char . :unsigned-char)
96       (:byte . :byte)
97       (:int . :int) (:unsigned-int . :unsigned-int) 
98       (:long . :long) (:unsigned-long . :unsigned-long)
99       (:float . :float) (:double . :double)
100       (:array . :array)))
101 #+lispworks
102 (setq +type-conversion-list+
103     '((* . :pointer) (:void . :void) 
104       (:short . :short)
105       (:pointer-void . (:pointer :void))
106       (:cstring . (:reference-pass (:ef-mb-string :external-format :latin-1)
107                                    :allow-null t))
108       (:cstring-returning . (:reference (:ef-mb-string :external-format :latin-1) :allow-null t))
109       (:char . :char)
110       (:byte :byte)
111       (:unsigned-char . (:unsigned :char))
112       (:int . :int) (:unsigned-int . (:unsigned :int))
113       (:long . :long) (:unsigned-long . (:unsigned :long))
114       (:float . :float) (:double . :double)
115       (:array . :c-array)))
116
117 (dolist (type +type-conversion-list+)
118   (setf (gethash (car type) +type-conversion-hash+) (cdr type)))
119
120 #+cmu
121 (dolist (type +cmu-def-type-list+)
122   (setf (gethash (car type) +cmu-def-type-hash+) (cdr type)))
123
124 (defun basic-convert-from-uffi-type (type)
125   (let ((found-type (gethash type +type-conversion-hash+)))
126     (if found-type
127         found-type
128         type)))
129
130 (defun convert-from-uffi-type (type context)
131   "Converts from a uffi type to an implementation specific type"
132   (if (atom type)
133       (cond
134        #+allegro 
135        ((and (or (eq context :routine) (eq context :return))
136              (eq type :cstring))
137         (setq type '((* :char) integer)))
138        #+cmu 
139        ((eq context :type)
140         (let ((cmu-type (gethash type +cmu-def-type-hash+)))
141           (if cmu-type
142               cmu-type
143               (basic-convert-from-uffi-type type))))
144        #+lispworks
145        ((and (eq context :return)
146              (eq type :cstring))
147         (basic-convert-from-uffi-type :cstring-returning))
148        (t
149         (basic-convert-from-uffi-type type)))
150       (cons (convert-from-uffi-type (first type) context) 
151             (convert-from-uffi-type (rest type) context))))
152
153
154
155
156
157