e4187dbf73265018d2e87cdd78941d8de1d9e9dd
[sbcl.git] / src / code / host-c-call.lisp
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
3 ;;;;
4 ;;;; This software is derived from the CMU CL system, which was
5 ;;;; written at Carnegie Mellon University and released into the
6 ;;;; public domain. The software is in the public domain and is
7 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
8 ;;;; files for more information.
9
10 (in-package "SB!ALIEN")
11
12 (/show0 "host-c-call.lisp 12")
13
14 (define-alien-type-class (c-string :include pointer :include-args (to))
15   (external-format :default :type keyword)
16   (element-type 'character :type (member character base-char)))
17
18 (define-alien-type-translator c-string
19     (&key (external-format :default)
20           (element-type 'character))
21   (make-alien-c-string-type
22    :to (parse-alien-type 'char (sb!kernel:make-null-lexenv))
23    :element-type element-type
24    :external-format external-format))
25
26 (defun c-string-external-format (type)
27   (let ((external-format (alien-c-string-type-external-format type)))
28     (if (eq external-format :default)
29         (default-c-string-external-format)
30         external-format)))
31
32 (define-alien-type-method (c-string :unparse) (type)
33   (let* ((external-format (alien-c-string-type-external-format type))
34          (element-type (alien-c-string-type-element-type type))
35          (tail
36           (append (unless (eq :default external-format)
37                     (list :external-format external-format))
38                   (unless (eq 'character element-type)
39                     (list :element-type element-type))) ))
40     (if tail
41         (cons 'c-string tail)
42         'c-string)))
43
44 (define-alien-type-method (c-string :lisp-rep) (type)
45   (declare (ignore type))
46   '(or simple-string null (alien (* char)) (simple-array (unsigned-byte 8))))
47
48 (define-alien-type-method (c-string :deport-pin-p) (type)
49   (declare (ignore type))
50   t)
51
52 (defun c-string-needs-conversion-p (type)
53   #+sb-xc-host
54   (declare (ignore type))
55   #+sb-xc-host
56   t
57   #-sb-xc-host
58   (let ((external-format (sb!impl::get-external-format
59                           ;; Can't use C-STRING-EXTERNAL-FORMAT here,
60                           ;; since the meaning of :DEFAULT can change
61                           ;; when *DEFAULT-C-STRING-EXTERNAL-FORMAT*
62                           ;; changes.
63                           (alien-c-string-type-external-format type))))
64     (not (and external-format
65               (or (eq (first (sb!impl::ef-names external-format)) :ascii)
66                   ;; On non-SB-UNICODE all latin-1 codepoints will fit
67                   ;; into a base-char, on SB-UNICODE they won't.
68                   #!-sb-unicode
69                   (eq (first (sb!impl::ef-names external-format)) :latin-1))))))
70
71 (define-alien-type-method (c-string :naturalize-gen) (type alien)
72   `(if (zerop (sap-int ,alien))
73        nil
74        ;; Check whether we need to do a full external-format
75        ;; conversion, or whether we can just do a cheap byte-by-byte
76        ;; copy of the c-string data.
77        ;;
78        ;; On SB-UNICODE we can never do the cheap copy, even if the
79        ;; external format and element-type are suitable, since
80        ;; simple-base-strings may not contain ISO-8859-1 characters.
81        ;; If we need to check for non-ascii data in the input, we
82        ;; might as well go through the usual external-format machinery
83        ;; instead of rewriting another version of it.
84        ,(if #!+sb-unicode t
85             #!-sb-unicode (c-string-needs-conversion-p type)
86             `(sb!alien::c-string-to-string ,alien
87                                            (c-string-external-format ,type)
88                                            (alien-c-string-type-element-type
89                                             ,type))
90             `(%naturalize-c-string ,alien))))
91
92 (define-alien-type-method (c-string :deport-gen) (type value)
93   (declare (ignore type))
94   ;; This SAP taking is safe as DEPORT callers pin the VALUE when
95   ;; necessary.
96   `(etypecase ,value
97      (null (int-sap 0))
98      ((alien (* char)) (alien-sap ,value))
99      (vector (vector-sap ,value))))
100
101 (define-alien-type-method (c-string :deport-alloc-gen) (type value)
102   `(etypecase ,value
103      (null nil)
104      ((alien (* char)) ,value)
105      (simple-base-string
106       ,(if (c-string-needs-conversion-p type)
107            ;; If the alien type is not ascii-compatible (+SB-UNICODE)
108            ;; or latin-1-compatible (-SB-UNICODE), we need to do
109            ;; external format conversion.
110            `(string-to-c-string ,value
111                                 (c-string-external-format ,type))
112            ;; Otherwise we can just pass it uncopied.
113            value))
114      (simple-string
115       (string-to-c-string ,value
116                           (c-string-external-format ,type)))))
117
118 (/show0 "host-c-call.lisp end of file")