Remove some test noise. A drop in the ocean unfortunately.
[sbcl.git] / src / code / primordial-extensions.lisp
blob50614ebe17fee39db7dd04419df7f75e2ce14445
1 ;;;; various user-level definitions which need to be done particularly
2 ;;;; early
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
13 (in-package "SB!IMPL")
15 ;;;; target constants which need to appear as early as possible
17 ;;; an internal tag for marking empty slots, which needs to be defined
18 ;;; as early as possible because it appears in macroexpansions for
19 ;;; iteration over hash tables
20 ;;;
21 ;;; CMU CL 18b used :EMPTY for this purpose, which was somewhat nasty
22 ;;; since it's easily accessible to the user, so that e.g.
23 ;;; (DEFVAR *HT* (MAKE-HASH-TABLE))
24 ;;; (SETF (GETHASH :EMPTY *HT*) :EMPTY)
25 ;;; (MAPHASH (LAMBDA (K V) (FORMAT T "~&~S ~S~%" K V)))
26 ;;; gives no output -- oops!
27 ;;;
28 ;;; FIXME: It'd probably be good to use the unbound marker for this.
29 ;;; However, there might be some gotchas involving assumptions by
30 ;;; e.g. AREF that they're not going to return the unbound marker,
31 ;;; and there's also the noted-below problem that the C-level code
32 ;;; contains implicit assumptions about this marker.
33 ;;;
34 ;;; KLUDGE: Note that as of version 0.pre7 there's a dependence in the
35 ;;; gencgc.c code on this value being a symbol. (This is only one of
36 ;;; several nasty dependencies between that code and this, alas.)
37 ;;; -- WHN 2001-08-17
38 (eval-when (:compile-toplevel :load-toplevel :execute)
39 (def!constant +empty-ht-slot+ '%empty-ht-slot%))
40 ;;; We shouldn't need this mess now that EVAL-WHEN works.
42 ;;; KLUDGE: Using a private symbol still leaves us vulnerable to users
43 ;;; getting nonconforming behavior by messing around with
44 ;;; DO-ALL-SYMBOLS. That seems like a fairly obscure problem, so for
45 ;;; now we just don't worry about it. If for some reason it becomes
46 ;;; worrisome and the magic value needs replacement:
47 ;;; * The replacement value needs to be LOADable with EQL preserved,
48 ;;; so that the macroexpansion for WITH-HASH-TABLE-ITERATOR will
49 ;;; work when compiled into a file and loaded back into SBCL.
50 ;;; (Thus, just uninterning %EMPTY-HT-SLOT% doesn't work.)
51 ;;; * The replacement value needs to be acceptable to the
52 ;;; low-level gencgc.lisp hash table scavenging code.
53 ;;; * The change will break binary compatibility, since comparisons
54 ;;; against the value used at the time of compilation are wired
55 ;;; into FASL files.
56 ;;; -- WHN 20000622
58 ;;;; DO-related stuff which needs to be visible on the cross-compilation host
60 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
61 (defun frob-do-body (varlist endlist decls-and-code bind step name block)
62 (let* ((r-inits nil) ; accumulator for reversed list
63 (r-steps nil) ; accumulator for reversed list
64 (label-1 (gensym))
65 (label-2 (gensym)))
66 ;; Check for illegal old-style DO.
67 (when (or (not (listp varlist)) (atom endlist))
68 (error "ill-formed ~S -- possibly illegal old style DO?" name))
69 ;; Parse VARLIST to get R-INITS and R-STEPS.
70 (dolist (v varlist)
71 (flet (;; (We avoid using CL:PUSH here so that CL:PUSH can be
72 ;; defined in terms of CL:SETF, and CL:SETF can be
73 ;; defined in terms of CL:DO, and CL:DO can be defined
74 ;; in terms of the current function.)
75 (push-on-r-inits (x)
76 (setq r-inits (cons x r-inits)))
77 ;; common error-handling
78 (illegal-varlist ()
79 (error "~S is an illegal form for a ~S varlist." v name)))
80 (cond ((symbolp v) (push-on-r-inits v))
81 ((listp v)
82 (unless (symbolp (first v))
83 (error "~S step variable is not a symbol: ~S"
84 name
85 (first v)))
86 (let ((lv (length v)))
87 ;; (We avoid using CL:CASE here so that CL:CASE can
88 ;; be defined in terms of CL:SETF, and CL:SETF can
89 ;; be defined in terms of CL:DO, and CL:DO can be
90 ;; defined in terms of the current function.)
91 (cond ((= lv 1)
92 (push-on-r-inits (first v)))
93 ((= lv 2)
94 (push-on-r-inits v))
95 ((= lv 3)
96 (push-on-r-inits (list (first v) (second v)))
97 (setq r-steps (list* (third v) (first v) r-steps)))
98 (t (illegal-varlist)))))
99 (t (illegal-varlist)))))
100 ;; Construct the new form.
101 (multiple-value-bind (code decls)
102 (parse-body decls-and-code :doc-string-allowed nil)
103 `(block ,block
104 (,bind ,(nreverse r-inits)
105 ,@decls
106 (tagbody
107 (go ,label-2)
108 ,label-1
109 (tagbody ,@code)
110 (,step ,@(nreverse r-steps))
111 ,label-2
112 (unless ,(first endlist) (go ,label-1))
113 (return-from ,block (progn ,@(rest endlist))))))))))
115 ;; Define "exchanged subtract" So that DECF on a symbol requires no LET binding:
116 ;; (DECF I (EXPR)) -> (SETQ I (XSUBTRACT (EXPR) I))
117 ;; which meets the CLHS 5.1.3 requirement to eval (EXPR) prior to reading
118 ;; the old value of I. Formerly in 'setf' but too late to avoid full calls.
119 (declaim (inline xsubtract))
120 (defun xsubtract (a b) (- b a))
122 ;;;; GENSYM tricks
124 ;;; Automate an idiom often found in macros:
125 ;;; (LET ((FOO (GENSYM "FOO"))
126 ;;; (MAX-INDEX (GENSYM "MAX-INDEX-")))
127 ;;; ...)
129 ;;; "Good notation eliminates thought." -- Eric Siggia
131 ;;; Incidentally, this is essentially the same operator which
132 ;;; _On Lisp_ calls WITH-GENSYMS.
133 (defmacro with-unique-names (symbols &body body)
134 `(let ,(mapcar (lambda (symbol)
135 (let* ((symbol-name (symbol-name symbol))
136 (stem (if (every #'alpha-char-p symbol-name)
137 symbol-name
138 (concatenate 'string symbol-name "-"))))
139 `(,symbol (sb!xc:gensym ,stem))))
140 symbols)
141 ,@body))
143 ;;; Return a list of N gensyms. (This is a common suboperation in
144 ;;; macros and other code-manipulating code.)
145 (declaim (ftype (function (unsigned-byte &optional t) (values list &optional))
146 make-gensym-list))
147 (defun make-gensym-list (n &optional name)
148 (let ((arg (if name (string name) "G")))
149 (loop repeat n collect (sb!xc:gensym arg))))
151 ;;;; miscellany
153 ;;; Lots of code wants to get to the KEYWORD package or the
154 ;;; COMMON-LISP package without a lot of fuss, so we cache them in
155 ;;; variables on the host, or use L-T-V forms on the target.
156 (macrolet ((def-it (sym expr)
157 #+sb-xc-host
158 `(progn (declaim (type package ,sym))
159 (defglobal ,sym ,expr))
160 #-sb-xc-host
161 ;; We don't need to declaim the type. FIND-PACKAGE
162 ;; returns a package, and L-T-V propagates types.
163 ;; It's ugly how it achieves that, but it's a separate concern.
164 `(define-symbol-macro ,sym (load-time-value ,expr t))))
165 (def-it *cl-package* (find-package "COMMON-LISP"))
166 (def-it *keyword-package* (find-package "KEYWORD")))
168 ;;; Concatenate together the names of some strings and symbols,
169 ;;; producing a symbol in the current package.
170 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
171 (defun symbolicate (&rest things)
172 (declare (dynamic-extent things))
173 (values
174 (intern
175 (if (singleton-p things)
176 (string (first things))
177 (let* ((length (reduce #'+ things
178 :key (lambda (x) (length (string x)))))
179 (name (make-array length :element-type 'character))
180 (index 0))
181 (dolist (thing things name)
182 (let ((x (string thing)))
183 (replace name x :start1 index)
184 (incf index (length x))))))))))
186 (defun gensymify (x)
187 (if (symbolp x)
188 (sb!xc:gensym (symbol-name x))
189 (sb!xc:gensym)))
191 ;;; like SYMBOLICATE, but producing keywords
192 (defun keywordicate (&rest things)
193 (let ((*package* *keyword-package*))
194 (apply #'symbolicate things)))
196 ;;; Access *PACKAGE* in a way which lets us recover when someone has
197 ;;; done something silly like (SETF *PACKAGE* :CL-USER). (Such an
198 ;;; assignment is undefined behavior, so it's sort of reasonable for
199 ;;; it to cause the system to go totally insane afterwards, but it's a
200 ;;; fairly easy mistake to make, so let's try to recover gracefully
201 ;;; instead.)
202 ;;; This function is called while compiling this file because DO-ANONYMOUS
203 ;;; is a delayed-def!macro, the constructor for which calls SANE-PACKAGE.
204 (eval-when (:load-toplevel :execute #+sb-xc-host :compile-toplevel)
205 (defun sane-package ()
206 (let ((maybe-package *package*))
207 (cond ((and (packagep maybe-package)
208 ;; For good measure, we also catch the problem of
209 ;; *PACKAGE* being bound to a deleted package.
210 ;; Technically, this is not undefined behavior in itself,
211 ;; but it will immediately lead to undefined to behavior,
212 ;; since almost any operation on a deleted package is
213 ;; undefined.
214 #-sb-xc-host
215 (package-%name maybe-package))
216 maybe-package)
218 ;; We're in the undefined behavior zone. First, munge the
219 ;; system back into a defined state.
220 (let ((really-package (find-package :cl-user)))
221 (setf *package* really-package)
222 ;; Then complain.
223 (error 'simple-type-error
224 :datum maybe-package
225 :expected-type '(and package (satisfies package-name))
226 :format-control
227 "~@<~S can't be a ~A: ~2I~_~S has been reset to ~S.~:>"
228 :format-arguments (list '*package*
229 (if (packagep maybe-package)
230 "deleted package"
231 (type-of maybe-package))
232 '*package* really-package))))))))
234 ;;; Access *DEFAULT-PATHNAME-DEFAULTS*, issuing a warning if its value
235 ;;; is silly. (Unlike the vaguely-analogous SANE-PACKAGE, we don't
236 ;;; actually need to reset the variable when it's silly, since even
237 ;;; crazy values of *DEFAULT-PATHNAME-DEFAULTS* don't leave the system
238 ;;; in a state where it's hard to recover interactively.)
239 (defun sane-default-pathname-defaults ()
240 (let* ((dfd *default-pathname-defaults*)
241 (dfd-dir (pathname-directory dfd)))
242 ;; It's generally not good to use a relative pathname for
243 ;; *DEFAULT-PATHNAME-DEFAULTS*, since relative pathnames
244 ;; are defined by merging into a default pathname (which is,
245 ;; by default, *DEFAULT-PATHNAME-DEFAULTS*).
246 (when (and (consp dfd-dir)
247 (eql (first dfd-dir) :relative))
248 (warn
249 "~@<~S is a relative pathname. (But we'll try using it anyway.)~@:>"
250 '*default-pathname-defaults*))
251 dfd))
253 ;;; Compile a version of BODY for all TYPES, and dispatch to the
254 ;;; correct one based on the value of VAR. This was originally used
255 ;;; only for strings, hence the name. Renaming it to something more
256 ;;; generic might not be a bad idea.
257 (def!macro string-dispatch ((&rest types) var &body body)
258 (let ((fun (sb!xc:gensym "STRING-DISPATCH-FUN")))
259 `(flet ((,fun (,var)
260 ,@body))
261 (declare (inline ,fun))
262 (etypecase ,var
263 ,@(loop for type in types
264 ;; TRULY-THE allows transforms to take advantage of the type
265 ;; information without need for constraint propagation.
266 collect `(,type (,fun (truly-the ,type ,var))))))))
268 ;;; Give names to elements of a numeric sequence.
269 (defmacro defenum ((&key (start 0) (step 1))
270 &rest identifiers)
271 (let ((results nil)
272 (index 0)
273 (start (eval start))
274 (step (eval step)))
275 (dolist (id identifiers)
276 (when id
277 (multiple-value-bind (sym docs)
278 (if (consp id)
279 (values (car id) (cdr id))
280 (values id nil))
281 (push `(def!constant ,sym
282 ,(+ start (* step index))
283 ,@docs)
284 results)))
285 (incf index))
286 `(progn
287 ,@(nreverse results))))
289 ;;; generalization of DEFCONSTANT to values which are the same not
290 ;;; under EQL but under e.g. EQUAL or EQUALP
292 ;;; DEFCONSTANT-EQX is to be used instead of DEFCONSTANT for values
293 ;;; which are appropriately compared using the function given by the
294 ;;; EQX argument instead of EQL.
296 (defmacro defconstant-eqx (symbol expr eqx &optional doc)
297 `(def!constant ,symbol
298 (%defconstant-eqx-value ',symbol ,expr ,eqx)
299 ,@(when doc (list doc))))
300 (defun %defconstant-eqx-value (symbol expr eqx)
301 (declare (type function eqx))
302 (flet ((bummer (explanation)
303 (error "~@<bad DEFCONSTANT-EQX ~S ~2I~_~S: ~2I~_~A ~S~:>"
304 symbol
305 expr
306 explanation
307 (symbol-value symbol))))
308 (cond ((not (boundp symbol))
309 expr)
310 ((not (constantp symbol))
311 (bummer "already bound as a non-constant"))
312 ((not (funcall eqx (symbol-value symbol) expr))
313 (bummer "already bound as a different constant value"))
315 (symbol-value symbol)))))
317 ;;; a helper function for various macros which expect clauses of a
318 ;;; given length, etc.
320 ;;; Return true if X is a proper list whose length is between MIN and
321 ;;; MAX (inclusive).
322 (defun proper-list-of-length-p (x min &optional (max min))
323 ;; FIXME: This implementation will hang on circular list
324 ;; structure. Since this is an error-checking utility, i.e. its
325 ;; job is to deal with screwed-up input, it'd be good style to fix
326 ;; it so that it can deal with circular list structure.
327 (cond ((minusp max) nil)
328 ((null x) (zerop min))
329 ((consp x)
330 (and (plusp max)
331 (proper-list-of-length-p (cdr x)
332 (if (plusp (1- min))
333 (1- min)
335 (1- max))))
336 (t nil)))
338 (defun proper-list-p (x)
339 (unless (consp x)
340 (return-from proper-list-p (null x)))
341 (let ((rabbit (cdr x))
342 (turtle x))
343 (flet ((pop-rabbit ()
344 (when (eql rabbit turtle) ; circular
345 (return-from proper-list-p nil))
346 (when (atom rabbit)
347 (return-from proper-list-p (null rabbit)))
348 (pop rabbit)))
349 (loop (pop-rabbit)
350 (pop-rabbit)
351 (pop turtle)))))
353 ;;; Helpers for defining error-signalling NOP's for "not supported
354 ;;; here" operations.
355 (defmacro define-unsupported-fun (name &optional
356 (doc "Unsupported on this platform.")
357 (control
358 "~S is unsupported on this platform ~
359 (OS, CPU, whatever)."
360 controlp)
361 arguments)
362 (declare (ignorable doc))
363 `(defun ,name (&rest args)
364 #!+sb-doc
365 ,doc
366 (declare (ignore args))
367 (error 'unsupported-operator
368 :format-control ,control
369 :format-arguments (if ,controlp ',arguments (list ',name)))))
371 ;;; This is like DO, except it has no implicit NIL block.
372 (def!macro do-anonymous (varlist endlist &rest body)
373 (frob-do-body varlist endlist body 'let 'psetq 'do-anonymous (gensym)))