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