1 ;;;; This software is part of the SBCL system. See the README file for
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.
10 (in-package "SB!ALIEN")
12 (/show0 "host-c-call.lisp 12")
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))
19 (define-alien-type-translator c-string
20 (&key (external-format :default)
21 (element-type 'character)
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
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)
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))
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))
45 (list :not-null t)))))
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)
54 `(or null ,@possibilities))))
56 (define-alien-type-method (c-string :deport-pin-p) (type)
57 (declare (ignore type))
60 (defun c-string-needs-conversion-p (type)
62 (declare (ignore type))
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*
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.
77 (eq (first (sb!impl::ef-names external-format)) :latin-1))))))
79 (declaim (ftype (sfunction (t) nil) null-error))
80 (defun null-error (type)
81 (aver (alien-c-string-type-not-null type))
83 :expected-type `(alien ,(unparse-alien-type type))
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)
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.
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.
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
107 `(%naturalize-c-string ,alien))))
109 (define-alien-type-method (c-string :deport-gen) (type value)
110 ;; This SAP taking is safe as DEPORT callers pin the VALUE when
114 ,(if (alien-c-string-type-not-null type)
117 ((alien (* char)) (alien-sap ,value))
118 (vector (vector-sap ,value))))
120 (define-alien-type-method (c-string :deport-alloc-gen) (type value)
123 ,(if (alien-c-string-type-not-null type)
126 ((alien (* char)) ,value)
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.
137 (string-to-c-string ,value
138 (c-string-external-format ,type)))))
140 (/show0 "host-c-call.lisp end of file")