1.0.19.16: derive the type of (AREF (THE STRING X) Y) as CHARACTER
[sbcl.git] / contrib / sb-posix / macros.lisp
1 (in-package :sb-posix)
2
3 (define-designator filename c-string
4   (pathname
5    (sb-ext:native-namestring (translate-logical-pathname filename)
6                              :as-file t))
7   (string filename))
8
9 (define-designator file-descriptor (integer 32)
10   (file-stream (sb-sys:fd-stream-fd file-descriptor))
11   (fixnum file-descriptor))
12
13 (define-designator sap-or-nil sb-sys:system-area-pointer
14   (null (sb-sys:int-sap 0))
15   (sb-sys:system-area-pointer sap-or-nil))
16
17 (define-designator alien-pointer-to-anything-or-nil (* t)
18   (null (sb-alien:sap-alien (sb-sys:int-sap 0) (* t)))
19   ((alien (* t)) alien-pointer-to-anything-or-nil))
20
21 (defun lisp-for-c-symbol (name)
22   (etypecase name
23     (list
24      (lisp-for-c-symbol (car name)))
25     (string
26      (let ((root (if (eql #\_ (char name 0)) (subseq name 1) name)))
27        (intern (substitute #\- #\_ (string-upcase root)) :sb-posix)))))
28
29 ;; Note: this variable is set in interface.lisp.  defined here for
30 ;; clarity and so the real-c-name compile as desired.
31 (defparameter *c-functions-in-runtime* nil)
32
33 (defun real-c-name (name)
34   (let  ((maybe-name
35           (etypecase name
36             (list
37              (destructuring-bind (name &key c-name options) name
38                (if c-name
39                    c-name
40                    (cond #+largefile
41                          ((or (eql options :largefile)
42                               (member :largefile options))
43                           (format nil "~a_largefile" name))
44                          (t
45                           name)))))
46             (string
47              name))))
48     (if (member maybe-name *c-functions-in-runtime*
49                 :test #'string=)
50         (format nil "_~A" maybe-name)
51         maybe-name)))
52
53 (defmacro define-call-internally (lisp-name c-name return-type error-predicate
54                                   &rest arguments)
55   (if (sb-sys:find-foreign-symbol-address c-name)
56       `(progn
57         (declaim (inline ,lisp-name))
58         (defun ,lisp-name ,(mapcar #'car arguments)
59           (let ((r (alien-funcall
60                     (extern-alien
61                      ,c-name
62                      (function ,return-type
63                                ,@(mapcar
64                                   (lambda (x)
65                                     (gethash (cadr x)
66                                              *designator-types*
67                                              (cadr x)))
68                                   arguments)))
69                     ,@(mapcar (lambda (x)
70                                 (if (nth-value 1
71                                                (gethash (cadr x)
72                                                         *designator-types*))
73                                     `(,(intern (symbol-name (cadr x))
74                                                :sb-posix)
75                                       ,(car x))
76                                     (car x)))
77                               arguments))))
78             (if (,error-predicate r) (syscall-error) r))))
79       `(sb-int:style-warn "Didn't find definition for ~S" ,c-name)))
80
81 (defmacro define-call (name return-type error-predicate &rest arguments)
82   (let ((lisp-name (lisp-for-c-symbol name))
83         (real-c-name (real-c-name name)))
84     `(progn
85        (export ',lisp-name :sb-posix)
86        (define-call-internally ,lisp-name
87            ,real-c-name
88          ,return-type
89          ,error-predicate
90          ,@arguments))))
91
92 (defmacro define-entry-point (name arglist &body body)
93   (let ((lisp-name (lisp-for-c-symbol name)))
94     `(progn
95       (export ',lisp-name :sb-posix)
96       (declaim (inline ,lisp-name))
97       (defun ,lisp-name ,arglist
98         ,@body))))