Declare EXPLICIT-CHECK on CONCATENATE, MAKE-STRING, SET-PPRINT-DISPATCH.
[sbcl.git] / src / code / target-extensions.lisp
blobda62517a7ec3bae2a1d34492d3750d0f2cb1ce6c
1 ;;;; This file contains things for the extensions packages (SB-EXT and
2 ;;;; also "internal extensions" SB-INT) which can't be built at
3 ;;;; cross-compile time, and perhaps also some things which might as
4 ;;;; well not be built at cross-compile time because they're not
5 ;;;; needed then. Things which can't be built at cross-compile time
6 ;;;; (e.g. because they need machinery which only exists inside SBCL's
7 ;;;; implementation of the LISP package) do not belong in this file.
9 ;;;; This software is part of the SBCL system. See the README file for
10 ;;;; more information.
11 ;;;;
12 ;;;; This software is derived from the CMU CL system, which was
13 ;;;; written at Carnegie Mellon University and released into the
14 ;;;; public domain. The software is in the public domain and is
15 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
16 ;;;; files for more information.
18 (in-package "SB!IMPL")
20 ;;;; variables initialization and shutdown sequences
22 ;; (Most of the save-a-core functionality is defined later, in its
23 ;; own file, but we'd like to have these symbols declared special
24 ;; and initialized ASAP.)
25 (defvar *save-hooks* nil
26 #!+sb-doc
27 "This is a list of functions which are called in an unspecified
28 order before creating a saved core image. Unused by SBCL itself:
29 reserved for user and applications.")
31 (defvar *init-hooks* nil
32 #!+sb-doc
33 "This is a list of functions which are called in an unspecified
34 order when a saved core image starts up, after the system itself has
35 been initialized. Unused by SBCL itself: reserved for user and
36 applications.")
38 (defvar *exit-hooks* nil
39 #!+sb-doc
40 "This is a list of functions which are called in an unspecified
41 order when SBCL process exits. Unused by SBCL itself: reserved for
42 user and applications. Using (SB-EXT:EXIT :ABORT T), or calling
43 exit(3) directly will circumvent these hooks.")
46 ;;; Binary search for simple vectors
47 (defun binary-search (value seq &key (key #'identity))
48 (declare (simple-vector seq))
49 (labels ((recurse (start end)
50 (when (< start end)
51 (let* ((i (+ start (truncate (- end start) 2)))
52 (elt (svref seq i))
53 (key-value (funcall key elt)))
54 (cond ((< value key-value)
55 (recurse start i))
56 ((> value key-value)
57 (recurse (1+ i) end))
59 elt))))))
60 (recurse 0 (length seq))))
62 (defun double-vector-binary-search (value vector)
63 (declare (simple-vector vector)
64 (optimize speed)
65 (integer value))
66 (labels ((recurse (start end)
67 (declare (type index start end))
68 (when (< start end)
69 (let* ((i (+ start (truncate (- end start) 2)))
70 (elt (svref vector (truly-the index (* 2 i)))))
71 (declare (type integer elt)
72 (type index i))
73 (cond ((< value elt)
74 (recurse start i))
75 ((> value elt)
76 (recurse (1+ i) end))
78 (svref vector (truly-the index (1+ (* 2 i))))))))))
79 (recurse 0 (truncate (length vector) 2))))
82 ;;; like LISTEN, but any whitespace in the input stream will be flushed
83 (defun listen-skip-whitespace (&optional (stream *standard-input*))
84 (do ((char (read-char-no-hang stream nil nil nil)
85 (read-char-no-hang stream nil nil nil)))
86 ((null char) nil)
87 (cond ((not (whitespace[1]p char))
88 (unread-char char stream)
89 (return t)))))
91 ;;;; helpers for C library calls
93 ;;; Signal a SIMPLE-CONDITION/ERROR condition associated with an ANSI C
94 ;;; errno problem, arranging for the condition's print representation
95 ;;; to be similar to the ANSI C perror(3) style.
96 (defun simple-perror (prefix-string
97 &key
98 (errno (get-errno))
99 (simple-error 'simple-error)
100 other-condition-args)
101 (declare (type symbol simple-error))
102 (aver (subtypep simple-error 'simple-condition))
103 (aver (subtypep simple-error 'error))
104 (apply #'error
105 simple-error
106 :format-control "~@<~A: ~2I~_~A~:>"
107 :format-arguments (list prefix-string (strerror errno))
108 other-condition-args))
110 ;;; Constructing shortish strings one character at a time. More efficient then
111 ;;; a string-stream, as can directly use simple-base-strings when applicable,
112 ;;; and if the maximum size is know doesn't need to copy the result at all --
113 ;;; but if the result is going to be HUGE, string-streams will win.
114 (defmacro with-push-char ((&key (element-type 'character) (initial-size 28)) &body body)
115 (with-unique-names (string size pointer)
116 `(let* ((,size ,initial-size)
117 (,string (make-array ,size :element-type ',element-type))
118 (,pointer 0))
119 (declare (type (integer 0 ,sb!xc:array-dimension-limit) ,size)
120 (type (integer 0 ,(1- sb!xc:array-dimension-limit)) ,pointer)
121 (type (simple-array ,element-type (*)) ,string))
122 (flet ((push-char (char)
123 (declare (optimize (sb!c::insert-array-bounds-checks 0)))
124 (when (= ,pointer ,size)
125 (let ((old ,string))
126 (setf ,size (* 2 (+ ,size 2))
127 ,string (make-array ,size :element-type ',element-type))
128 (replace ,string old)))
129 (setf (char ,string ,pointer) char)
130 (incf ,pointer))
131 (get-pushed-string ()
132 (let ((string ,string)
133 (size ,pointer))
134 (setf ,size 0
135 ,pointer 0
136 ,string ,(coerce "" `(simple-array ,element-type (*))))
137 ;; This is really local, so we can be destructive!
138 (%shrink-vector string size)
139 string)))
140 ,@body))))
142 ;;; The smallest power of two that is equal to or greater than X.
143 (defun power-of-two-ceiling (x)
144 (declare (index x))
145 (ash 1 (integer-length (1- x))))
147 ;;; Signalling an error when trying to print an error condition is
148 ;;; generally a PITA, so whatever the failure encountered when
149 ;;; wondering about FILE-POSITION within a condition printer, 'tis
150 ;;; better silently to give up than to try to complain.
151 (defun file-position-or-nil-for-error (stream &optional (pos nil posp))
152 ;; Arguably FILE-POSITION shouldn't be signalling errors at all; but
153 ;; "NIL if this cannot be determined" in the ANSI spec doesn't seem
154 ;; absolutely unambiguously to prohibit errors when, e.g., STREAM
155 ;; has been closed so that FILE-POSITION is a nonsense question. So
156 ;; my (WHN) impression is that the conservative approach is to
157 ;; IGNORE-ERRORS. (I encountered this failure from within a homebrew
158 ;; defsystemish operation where the ERROR-STREAM had been CL:CLOSEd,
159 ;; I think by nonlocally exiting through a WITH-OPEN-FILE, by the
160 ;; time an error was reported.)
161 (ignore-errors
162 (if posp
163 (file-position stream pos)
164 (file-position stream))))
166 (defun stream-error-position-info (stream &optional position)
167 (when (and (not position) (form-tracking-stream-p stream))
168 (let ((line/col (line/col-from-charpos stream)))
169 (return-from stream-error-position-info
170 `((:line ,(car line/col))
171 (:column ,(cdr line/col))
172 ,@(let ((position (file-position-or-nil-for-error stream)))
173 ;; FIXME: 1- is technically broken for multi-byte external
174 ;; encodings, albeit bug-compatible with the broken code in
175 ;; the general case (below) for non-form-tracking-streams.
176 ;; i.e. If you position to this byte, it might not be the
177 ;; first byte of any character.
178 (when position `((:file-position ,(1- position)))))))))
180 ;; Give up early for interactive streams and non-character stream.
181 (when (or (ignore-errors (interactive-stream-p stream))
182 (not (subtypep (ignore-errors (stream-element-type stream))
183 'character)))
184 (return-from stream-error-position-info))
186 (flet ((read-content (old-position position)
187 "Read the content of STREAM into a buffer in order to count
188 lines and columns."
189 (unless (and old-position position
190 (< position sb!xc:array-dimension-limit))
191 (return-from read-content))
192 (let ((content
193 (make-string position :element-type (stream-element-type stream))))
194 (when (and (file-position-or-nil-for-error stream :start)
195 (eql position (ignore-errors (read-sequence content stream))))
196 (file-position-or-nil-for-error stream old-position)
197 content)))
198 ;; Lines count from 1, columns from 0. It's stupid and
199 ;; traditional.
200 (line (string)
201 (1+ (count #\Newline string)))
202 (column (string position)
203 (- position (or (position #\Newline string :from-end t) 0))))
204 (let* ((stream-position (file-position-or-nil-for-error stream))
205 (position (or position
206 ;; FILE-POSITION is the next character --
207 ;; error is at the previous one.
208 (and stream-position (plusp stream-position)
209 (1- stream-position))))
210 (content (read-content stream-position position)))
211 `(,@(when content `((:line ,(line content))
212 (:column ,(column content position))))
213 ,@(when position `((:file-position ,position)))))))