1.0.46.19: add :NOT-NULL option to C-STRING type
[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   (not-null nil :type boolean))
18
19 (define-alien-type-translator c-string
20     (&key (external-format :default)
21           (element-type 'character)
22           (not-null nil))
23   (make-alien-c-string-type
24    :to (parse-alien-type 'char (sb!kernel:make-null-lexenv))
25    :element-type element-type
26    :external-format external-format
27    :not-null not-null))
28
29 (defun c-string-external-format (type)
30   (let ((external-format (alien-c-string-type-external-format type)))
31     (if (eq external-format :default)
32         (default-c-string-external-format)
33         external-format)))
34
35 (define-alien-type-method (c-string :unparse) (type)
36   (let* ((external-format (alien-c-string-type-external-format type))
37          (element-type (alien-c-string-type-element-type type))
38          (not-null (alien-c-string-type-not-null type))
39          (tail
40           (append (unless (eq :default external-format)
41                     (list :external-format external-format))
42                   (unless (eq 'character element-type)
43                     (list :element-type element-type))
44                   (when not-null
45                     (list :not-null t)))))
46     (if tail
47         (cons 'c-string tail)
48         'c-string)))
49
50 (define-alien-type-method (c-string :lisp-rep) (type)
51   (let ((possibilities '(simple-string (alien (* char)) (simple-array (unsigned-byte 8)))))
52     (if (alien-c-string-type-not-null type)
53         `(or ,@possibilities)
54         `(or null ,@possibilities))))
55
56 (define-alien-type-method (c-string :deport-pin-p) (type)
57   (declare (ignore type))
58   t)
59
60 (defun c-string-needs-conversion-p (type)
61   #+sb-xc-host
62   (declare (ignore type))
63   #+sb-xc-host
64   t
65   #-sb-xc-host
66   (let ((external-format (sb!impl::get-external-format
67                           ;; Can't use C-STRING-EXTERNAL-FORMAT here,
68                           ;; since the meaning of :DEFAULT can change
69                           ;; when *DEFAULT-C-STRING-EXTERNAL-FORMAT*
70                           ;; changes.
71                           (alien-c-string-type-external-format type))))
72     (not (and external-format
73               (or (eq (first (sb!impl::ef-names external-format)) :ascii)
74                   ;; On non-SB-UNICODE all latin-1 codepoints will fit
75                   ;; into a base-char, on SB-UNICODE they won't.
76                   #!-sb-unicode
77                   (eq (first (sb!impl::ef-names external-format)) :latin-1))))))
78
79 (declaim (ftype (sfunction (t) nil) null-error))
80 (defun null-error (type)
81   (aver (alien-c-string-type-not-null type))
82   (error 'type-error
83          :expected-type `(alien ,(unparse-alien-type type))
84          :datum nil))
85
86 (define-alien-type-method (c-string :naturalize-gen) (type alien)
87   `(if (zerop (sap-int ,alien))
88        ,(if (alien-c-string-type-not-null type)
89             `(null-error ',type)
90             nil)
91        ;; Check whether we need to do a full external-format
92        ;; conversion, or whether we can just do a cheap byte-by-byte
93        ;; copy of the c-string data.
94        ;;
95        ;; On SB-UNICODE we can never do the cheap copy, even if the
96        ;; external format and element-type are suitable, since
97        ;; simple-base-strings may not contain ISO-8859-1 characters.
98        ;; If we need to check for non-ascii data in the input, we
99        ;; might as well go through the usual external-format machinery
100        ;; instead of rewriting another version of it.
101        ,(if #!+sb-unicode t
102             #!-sb-unicode (c-string-needs-conversion-p type)
103             `(sb!alien::c-string-to-string ,alien
104                                            (c-string-external-format ,type)
105                                            (alien-c-string-type-element-type
106                                             ,type))
107             `(%naturalize-c-string ,alien))))
108
109 (define-alien-type-method (c-string :deport-gen) (type value)
110   ;; This SAP taking is safe as DEPORT callers pin the VALUE when
111   ;; necessary.
112   `(etypecase ,value
113      (null
114       ,(if (alien-c-string-type-not-null type)
115            `(null-error ',type)
116            `(int-sap 0)))
117      ((alien (* char)) (alien-sap ,value))
118      (vector (vector-sap ,value))))
119
120 (define-alien-type-method (c-string :deport-alloc-gen) (type value)
121   `(etypecase ,value
122      (null
123       ,(if (alien-c-string-type-not-null type)
124            `(null-error ',type)
125            nil))
126      ((alien (* char)) ,value)
127      (simple-base-string
128       ,(if (c-string-needs-conversion-p type)
129            ;; If the alien type is not ascii-compatible (+SB-UNICODE)
130            ;; or latin-1-compatible (-SB-UNICODE), we need to do
131            ;; external format conversion.
132            `(string-to-c-string ,value
133                                 (c-string-external-format ,type))
134            ;; Otherwise we can just pass it uncopied.
135            value))
136      (simple-string
137       (string-to-c-string ,value
138                           (c-string-external-format ,type)))))
139
140 (/show0 "host-c-call.lisp end of file")