1.0.31.23: OAOOize external-format support
[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   (list 'c-string
34         :external-format (alien-c-string-type-external-format type)
35         :element-type (alien-c-string-type-element-type type)))
36
37 (define-alien-type-method (c-string :lisp-rep) (type)
38   (declare (ignore type))
39   '(or simple-string null (alien (* char)) (simple-array (unsigned-byte 8))))
40
41 (define-alien-type-method (c-string :deport-pin-p) (type)
42   (declare (ignore type))
43   t)
44
45 (defun c-string-needs-conversion-p (type)
46   #+sb-xc-host
47   (declare (ignore type))
48   #+sb-xc-host
49   t
50   #-sb-xc-host
51   (let ((external-format (sb!impl::get-external-format
52                           ;; Can't use C-STRING-EXTERNAL-FORMAT here,
53                           ;; since the meaning of :DEFAULT can change
54                           ;; when *DEFAULT-C-STRING-EXTERNAL-FORMAT*
55                           ;; changes.
56                           (alien-c-string-type-external-format type))))
57     (not (and external-format
58               (or (eq (first (sb!impl::ef-names external-format)) :ascii)
59                   ;; On non-SB-UNICODE all latin-1 codepoints will fit
60                   ;; into a base-char, on SB-UNICODE they won't.
61                   #!-sb-unicode
62                   (eq (first (sb!impl::ef-names external-format)) :latin-1))))))
63
64 (define-alien-type-method (c-string :naturalize-gen) (type alien)
65   `(if (zerop (sap-int ,alien))
66        nil
67        ;; Check whether we need to do a full external-format
68        ;; conversion, or whether we can just do a cheap byte-by-byte
69        ;; copy of the c-string data.
70        ;;
71        ;; On SB-UNICODE we can never do the cheap copy, even if the
72        ;; external format and element-type are suitable, since
73        ;; simple-base-strings may not contain ISO-8859-1 characters.
74        ;; If we need to check for non-ascii data in the input, we
75        ;; might as well go through the usual external-format machinery
76        ;; instead of rewriting another version of it.
77        ,(if #!+sb-unicode t
78             #!-sb-unicode (c-string-needs-conversion-p type)
79             `(sb!alien::c-string-to-string ,alien
80                                            (c-string-external-format ,type)
81                                            (alien-c-string-type-element-type
82                                             ,type))
83             `(%naturalize-c-string ,alien))))
84
85 (define-alien-type-method (c-string :deport-gen) (type value)
86   (declare (ignore type))
87   ;; This SAP taking is safe as DEPORT callers pin the VALUE when
88   ;; necessary.
89   `(etypecase ,value
90      (null (int-sap 0))
91      ((alien (* char)) (alien-sap ,value))
92      (vector (vector-sap ,value))))
93
94 (define-alien-type-method (c-string :deport-alloc-gen) (type value)
95   `(etypecase ,value
96      (null nil)
97      ((alien (* char)) ,value)
98      (simple-base-string
99       ,(if (c-string-needs-conversion-p type)
100            ;; If the alien type is not ascii-compatible (+SB-UNICODE)
101            ;; or latin-1-compatible (-SB-UNICODE), we need to do
102            ;; external format conversion.
103            `(string-to-c-string ,value
104                                 (c-string-external-format ,type))
105            ;; Otherwise we can just pass it uncopied.
106            value))
107      (simple-string
108       (string-to-c-string ,value
109                           (c-string-external-format ,type)))))
110
111 (/show0 "host-c-call.lisp end of file")