1 ;;;; various extensions (including SB-INT "internal extensions")
2 ;;;; available both in the cross-compilation host Lisp and in the
5 ;;;; This software is part of the SBCL system. See the README file for
8 ;;;; This software is derived from the CMU CL system, which was
9 ;;;; written at Carnegie Mellon University and released into the
10 ;;;; public domain. The software is in the public domain and is
11 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
12 ;;;; files for more information.
14 (in-package "SB!IMPL")
16 ;;; Lots of code wants to get to the KEYWORD package or the
17 ;;; COMMON-LISP package without a lot of fuss, so we cache them in
18 ;;; variables. TO DO: How much does this actually buy us? It sounds
19 ;;; sensible, but I don't know for sure that it saves space or time..
22 ;;; (The initialization forms here only matter on the cross-compilation
23 ;;; host; In the target SBCL, these variables are set in cold init.)
24 (declaim (type package *cl-package* *keyword-package*))
25 (defvar *cl-package* (find-package "COMMON-LISP"))
26 (defvar *keyword-package* (find-package "KEYWORD"))
28 ;;; something not EQ to anything we might legitimately READ
29 (defparameter *eof-object* (make-symbol "EOF-OBJECT"))
31 ;;; a type used for indexing into arrays, and for related quantities
32 ;;; like lengths of lists
34 ;;; It's intentionally limited to one less than the
35 ;;; ARRAY-DIMENSION-LIMIT for efficiency reasons, because in SBCL
36 ;;; ARRAY-DIMENSION-LIMIT is MOST-POSITIVE-FIXNUM, and staying below
37 ;;; that lets the system know it can increment a value of this type
38 ;;; without having to worry about using a bignum to represent the
41 ;;; (It should be safe to use ARRAY-DIMENSION-LIMIT as an exclusive
42 ;;; bound because ANSI specifies it as an exclusive bound.)
43 (def!type index () `(integer 0 (,sb!xc:array-dimension-limit)))
45 ;;; like INDEX, but augmented with -1 (useful when using the index
46 ;;; to count downwards to 0, e.g. LOOP FOR I FROM N DOWNTO 0, with
47 ;;; an implementation which terminates the loop by testing for the
48 ;;; index leaving the loop range)
49 (def!type index-or-minus-1 () `(integer -1 (,sb!xc:array-dimension-limit)))
51 ;;; the default value used for initializing character data. The ANSI
52 ;;; spec says this is arbitrary. CMU CL used #\NULL, which we avoid
53 ;;; because it's not in the ANSI table of portable characters.
54 (defconstant default-init-char #\space)
56 ;;; CHAR-CODE values for ASCII characters which we care about but
57 ;;; which aren't defined in section "2.1.3 Standard Characters" of the
58 ;;; ANSI specification for Lisp
60 ;;; KLUDGE: These are typically used in the idiom (CODE-CHAR
61 ;;; FOO-CHAR-CODE). I suspect that the current implementation is
62 ;;; expanding this idiom into a full call to CODE-CHAR, which is an
63 ;;; annoying overhead. I should check whether this is happening, and
64 ;;; if so, perhaps implement a DEFTRANSFORM or something to stop it.
65 ;;; (or just find a nicer way of expressing characters portably?) --
67 (defconstant bell-char-code 7)
68 (defconstant tab-char-code 9)
69 (defconstant form-feed-char-code 12)
70 (defconstant return-char-code 13)
71 (defconstant escape-char-code 27)
72 (defconstant rubout-char-code 127)
74 ;;;; type-ish predicates
76 ;;; a helper function for various macros which expect clauses of a
77 ;;; given length, etc.
79 ;;; FIXME: This implementation will hang on circular list structure.
80 ;;; Since this is an error-checking utility, i.e. its job is to deal
81 ;;; with screwed-up input, it'd be good style to fix it so that it can
82 ;;; deal with circular list structure.
83 (eval-when (:compile-toplevel :load-toplevel :execute)
84 ;; Return true if X is a proper list whose length is between MIN and
86 (defun proper-list-of-length-p (x min &optional (max min))
93 (proper-list-of-length-p (cdr x)
100 ;;; Is X a circular list?
101 (defun circular-list-p (x)
103 (labels ((safe-cddr (x) (if (listp (cdr x)) (cddr x))))
104 (do ((y x (safe-cddr y))
107 ((not (and (consp z) (consp y))) nil)
108 (when (and started-p (eq y z))
111 ;;; Is X a (possibly-improper) list of at least N elements?
112 (declaim (ftype (function (t index)) list-of-length-at-least-p))
113 (defun list-of-length-at-least-p (x n)
114 (or (zerop n) ; since anything can be considered an improper list of length 0
116 (list-of-length-at-least-p (cdr x) (1- n)))))
118 ;;;; the COLLECT macro
120 ;;;; comment from CMU CL: "the ultimate collection macro..."
122 ;;; helper functions for COLLECT, which become the expanders of the
123 ;;; MACROLET definitions created by COLLECT
125 ;;; COLLECT-NORMAL-EXPANDER handles normal collection macros.
127 ;;; COLLECT-LIST-EXPANDER handles the list collection case. N-TAIL
128 ;;; is the pointer to the current tail of the list, or NIL if the list
130 (eval-when (:compile-toplevel :load-toplevel :execute)
131 (defun collect-normal-expander (n-value fun forms)
133 ,@(mapcar (lambda (form) `(setq ,n-value (,fun ,form ,n-value))) forms)
135 (defun collect-list-expander (n-value n-tail forms)
136 (let ((n-res (gensym)))
138 ,@(mapcar (lambda (form)
139 `(let ((,n-res (cons ,form nil)))
141 (setf (cdr ,n-tail) ,n-res)
142 (setq ,n-tail ,n-res))
144 (setq ,n-tail ,n-res ,n-value ,n-res)))))
148 ;;; Collect some values somehow. Each of the collections specifies a
149 ;;; bunch of things which collected during the evaluation of the body
150 ;;; of the form. The name of the collection is used to define a local
151 ;;; macro, a la MACROLET. Within the body, this macro will evaluate
152 ;;; each of its arguments and collect the result, returning the
153 ;;; current value after the collection is done. The body is evaluated
154 ;;; as a PROGN; to get the final values when you are done, just call
155 ;;; the collection macro with no arguments.
157 ;;; INITIAL-VALUE is the value that the collection starts out with,
158 ;;; which defaults to NIL. FUNCTION is the function which does the
159 ;;; collection. It is a function which will accept two arguments: the
160 ;;; value to be collected and the current collection. The result of
161 ;;; the function is made the new value for the collection. As a
162 ;;; totally magical special-case, FUNCTION may be COLLECT, which tells
163 ;;; us to build a list in forward order; this is the default. If an
164 ;;; INITIAL-VALUE is supplied for Collect, the stuff will be RPLACD'd
165 ;;; onto the end. Note that FUNCTION may be anything that can appear
166 ;;; in the functional position, including macros and lambdas.
167 (defmacro collect (collections &body body)
170 (dolist (spec collections)
171 (unless (proper-list-of-length-p spec 1 3)
172 (error "malformed collection specifier: ~S." spec))
173 (let* ((name (first spec))
174 (default (second spec))
175 (kind (or (third spec) 'collect))
176 (n-value (gensym (concatenate 'string
179 (push `(,n-value ,default) binds)
180 (if (eq kind 'collect)
181 (let ((n-tail (gensym (concatenate 'string
185 (push `(,n-tail (last ,n-value)) binds)
187 (push `(,name (&rest args)
188 (collect-list-expander ',n-value ',n-tail args))
190 (push `(,name (&rest args)
191 (collect-normal-expander ',n-value ',kind args))
193 `(macrolet ,macros (let* ,(nreverse binds) ,@body))))
195 ;;;; some old-fashioned functions. (They're not just for old-fashioned
196 ;;;; code, they're also used as optimized forms of the corresponding
197 ;;;; general functions when the compiler can prove that they're
200 ;;; like (MEMBER ITEM LIST :TEST #'EQ)
201 (defun memq (item list)
203 "Returns tail of LIST beginning with first element EQ to ITEM."
204 ;; KLUDGE: These could be and probably should be defined as
205 ;; (MEMBER ITEM LIST :TEST #'EQ)),
206 ;; but when I try to cross-compile that, I get an error from
207 ;; LTN-ANALYZE-KNOWN-CALL, "Recursive known function definition". The
208 ;; comments for that error say it "is probably a botched interpreter stub".
209 ;; Rather than try to figure that out, I just rewrote this function from
210 ;; scratch. -- WHN 19990512
211 (do ((i list (cdr i)))
213 (when (eq (car i) item)
216 ;;; like (ASSOC ITEM ALIST :TEST #'EQ):
217 ;;; Return the first pair of ALIST where ITEM is EQ to the key of
219 (defun assq (item alist)
220 ;; KLUDGE: CMU CL defined this with
221 ;; (DECLARE (INLINE ASSOC))
222 ;; (ASSOC ITEM ALIST :TEST #'EQ))
223 ;; which is pretty, but which would have required adding awkward
224 ;; build order constraints on SBCL (or figuring out some way to make
225 ;; inline definitions installable at build-the-cross-compiler time,
226 ;; which was too ambitious for now). Rather than mess with that, we
227 ;; just define ASSQ explicitly in terms of more primitive
230 (when (eq (car pair) item)
233 ;;; like (DELETE .. :TEST #'EQ):
234 ;;; Delete all LIST entries EQ to ITEM (destructively modifying
235 ;;; LIST), and return the modified LIST.
236 (defun delq (item list)
238 (do ((x list (cdr x))
241 (cond ((eq item (car x))
244 (rplacd splice (cdr x))))
245 (t (setq splice x)))))) ; Move splice along to include element.
248 ;;; like (POSITION .. :TEST #'EQ):
249 ;;; Return the position of the first element EQ to ITEM.
250 (defun posq (item list)
251 (do ((i list (cdr i))
254 (when (eq (car i) item)
257 (declaim (inline neq))
261 ;;;; miscellaneous iteration extensions
263 ;;; "the ultimate iteration macro"
265 ;;; note for Schemers: This seems to be identical to Scheme's "named LET".
266 (defmacro named-let (name binds &body body)
269 (unless (proper-list-of-length-p x 2)
270 (error "malformed NAMED-LET variable spec: ~S" x)))
271 `(labels ((,name ,(mapcar #'first binds) ,@body))
272 (,name ,@(mapcar #'second binds))))
274 ;;; just like DOLIST, but with one-dimensional arrays
275 (defmacro dovector ((elt vector &optional result) &rest forms)
276 (let ((index (gensym))
279 `(let ((,vec ,vector))
280 (declare (type vector ,vec))
281 (do ((,index 0 (1+ ,index))
282 (,length (length ,vec)))
283 ((>= ,index ,length) ,result)
284 (let ((,elt (aref ,vec ,index)))
287 ;;; Iterate over the entries in a HASH-TABLE.
288 (defmacro dohash ((key-var value-var table &optional result) &body body)
289 (multiple-value-bind (forms decls) (parse-body body nil)
292 `(with-hash-table-iterator (,gen ,table)
294 (multiple-value-bind (,n-more ,key-var ,value-var) (,gen)
296 (unless ,n-more (return ,result))
299 ;;;; hash cache utility
301 (eval-when (:compile-toplevel :load-toplevel :execute)
302 (defvar *profile-hash-cache* nil))
304 ;;; a flag for whether it's too early in cold init to use caches so
305 ;;; that we have a better chance of recovering so that we have a
306 ;;; better chance of getting the system running so that we have a
307 ;;; better chance of diagnosing the problem which caused us to use the
310 (defvar *hash-caches-initialized-p*)
312 ;;; Define a hash cache that associates some number of argument values
313 ;;; with a result value. The TEST-FUNCTION paired with each ARG-NAME
314 ;;; is used to compare the value for that arg in a cache entry with a
315 ;;; supplied arg. The TEST-FUNCTION must not error when passed NIL as
316 ;;; its first arg, but need not return any particular value.
317 ;;; TEST-FUNCTION may be any thing that can be placed in CAR position.
319 ;;; NAME is used to define these functions:
320 ;;; <name>-CACHE-LOOKUP Arg*
321 ;;; See whether there is an entry for the specified ARGs in the
322 ;;; cache. If not present, the :DEFAULT keyword (default NIL)
323 ;;; determines the result(s).
324 ;;; <name>-CACHE-ENTER Arg* Value*
325 ;;; Encache the association of the specified args with VALUE.
326 ;;; <name>-CACHE-CLEAR
327 ;;; Reinitialize the cache, invalidating all entries and allowing
328 ;;; the arguments and result values to be GC'd.
330 ;;; These other keywords are defined:
332 ;;; The size of the cache as a power of 2.
333 ;;; :HASH-FUNCTION function
334 ;;; Some thing that can be placed in CAR position which will compute
335 ;;; a value between 0 and (1- (expt 2 <hash-bits>)).
337 ;;; the number of return values cached for each function call
338 ;;; :INIT-WRAPPER <name>
339 ;;; The code for initializing the cache is wrapped in a form with
340 ;;; the specified name. (:INIT-WRAPPER is set to COLD-INIT-FORMS
341 ;;; in type system definitions so that caches will be created
342 ;;; before top-level forms run.)
343 (defmacro define-hash-cache (name args &key hash-function hash-bits default
344 (init-wrapper 'progn)
346 (let* ((var-name (symbolicate "*" name "-CACHE-VECTOR*"))
347 (nargs (length args))
348 (entry-size (+ nargs values))
349 (size (ash 1 hash-bits))
350 (total-size (* entry-size size))
351 (default-values (if (and (consp default) (eq (car default) 'values))
357 (unless (= (length default-values) values)
358 (error "The number of default values ~S differs from :VALUES ~D."
370 (values-indices `(+ ,n-index ,(+ nargs i)))
371 (values-names (gensym)))
374 (unless (= (length arg) 2)
375 (error "bad argument spec: ~S" arg))
376 (let ((arg-name (first arg))
379 (tests `(,test (svref ,n-cache (+ ,n-index ,n)) ,arg-name))
380 (sets `(setf (svref ,n-cache (+ ,n-index ,n)) ,arg-name)))
383 (when *profile-hash-cache*
384 (let ((n-probe (symbolicate "*" name "-CACHE-PROBES*"))
385 (n-miss (symbolicate "*" name "-CACHE-MISSES*")))
386 (inits `(setq ,n-probe 0))
387 (inits `(setq ,n-miss 0))
388 (forms `(defvar ,n-probe))
389 (forms `(defvar ,n-miss))
390 (forms `(declaim (fixnum ,n-miss ,n-probe)))))
392 (let ((fun-name (symbolicate name "-CACHE-LOOKUP")))
395 `(defun ,fun-name ,(arg-vars)
396 ,@(when *profile-hash-cache*
397 `((incf ,(symbolicate "*" name "-CACHE-PROBES*"))))
398 (let ((,n-index (* (,hash-function ,@(arg-vars)) ,entry-size))
399 (,n-cache ,var-name))
400 (declare (type fixnum ,n-index))
401 (cond ((and ,@(tests))
402 (values ,@(mapcar (lambda (x) `(svref ,n-cache ,x))
405 ,@(when *profile-hash-cache*
406 `((incf ,(symbolicate "*" name "-CACHE-MISSES*"))))
409 (let ((fun-name (symbolicate name "-CACHE-ENTER")))
412 `(defun ,fun-name (,@(arg-vars) ,@(values-names))
413 (let ((,n-index (* (,hash-function ,@(arg-vars)) ,entry-size))
414 (,n-cache ,var-name))
415 (declare (type fixnum ,n-index))
417 ,@(mapcar #'(lambda (i val)
418 `(setf (svref ,n-cache ,i) ,val))
423 (let ((fun-name (symbolicate name "-CACHE-CLEAR")))
426 (do ((,n-index ,(- total-size entry-size) (- ,n-index ,entry-size))
427 (,n-cache ,var-name))
429 (declare (type fixnum ,n-index))
430 ,@(collect ((arg-sets))
432 (arg-sets `(setf (svref ,n-cache (+ ,n-index ,i)) nil)))
434 ,@(mapcar #'(lambda (i val)
435 `(setf (svref ,n-cache ,i) ,val))
439 (forms `(,fun-name)))
441 (inits `(unless (boundp ',var-name)
442 (setq ,var-name (make-array ,total-size))))
443 #!+sb-show (inits `(setq *hash-caches-initialized-p* t))
447 (declaim (type (simple-vector ,total-size) ,var-name))
448 #!-sb-fluid (declaim (inline ,@(inlines)))
449 (,init-wrapper ,@(inits))
453 ;;; some syntactic sugar for defining a function whose values are
454 ;;; cached by DEFINE-HASH-CACHE
455 (defmacro defun-cached ((name &rest options &key (values 1) default
457 args &body body-decls-doc)
458 (let ((default-values (if (and (consp default) (eq (car default) 'values))
461 (arg-names (mapcar #'car args)))
462 (collect ((values-names))
464 (values-names (gensym)))
465 (multiple-value-bind (body decls doc) (parse-body body-decls-doc)
467 (define-hash-cache ,name ,args ,@options)
468 (defun ,name ,arg-names
472 ((not (boundp '*hash-caches-initialized-p*))
473 ;; This shouldn't happen, but it did happen to me
474 ;; when revising the type system, and it's a lot
475 ;; easier to figure out what what's going on with
476 ;; that kind of problem if the system can be kept
477 ;; alive until cold boot is complete. The recovery
478 ;; mechanism should definitely be conditional on
479 ;; some debugging feature (e.g. SB-SHOW) because
480 ;; it's big, duplicating all the BODY code. -- WHN
481 (/show0 ,name " too early in cold init, uncached")
482 (/show0 ,(first arg-names) "=..")
483 (/hexstr ,(first arg-names))
486 (multiple-value-bind ,(values-names)
487 (,(symbolicate name "-CACHE-LOOKUP") ,@arg-names)
488 (if (and ,@(mapcar (lambda (val def)
490 (values-names) default-values))
491 (multiple-value-bind ,(values-names)
493 (,(symbolicate name "-CACHE-ENTER") ,@arg-names
495 (values ,@(values-names)))
496 (values ,@(values-names))))))))))))
500 ;;; Note: Almost always you want to use FIND-UNDELETED-PACKAGE-OR-LOSE
501 ;;; instead of this function. (The distinction only actually matters when
502 ;;; PACKAGE-DESIGNATOR is actually a deleted package, and in that case
503 ;;; you generally do want to signal an error instead of proceeding.)
504 (defun %find-package-or-lose (package-designator)
505 (or (find-package package-designator)
506 (error 'sb!kernel:simple-package-error
507 :package package-designator
508 :format-control "The name ~S does not designate any package."
509 :format-arguments (list package-designator))))
511 ;;; ANSI specifies (in the section for FIND-PACKAGE) that the
512 ;;; consequences of most operations on deleted packages are
513 ;;; unspecified. We try to signal errors in such cases.
514 (defun find-undeleted-package-or-lose (package-designator)
515 (let ((maybe-result (%find-package-or-lose package-designator)))
516 (if (package-name maybe-result) ; if not deleted
518 (error 'sb!kernel:simple-package-error
519 :package maybe-result
520 :format-control "The package ~S has been deleted."
521 :format-arguments (list maybe-result)))))
523 ;;;; various operations on names
525 ;;; Is NAME a legal function name?
526 (defun legal-function-name-p (name)
529 (eq (car name) 'setf)
531 (symbolp (cadr name))
532 (null (cddr name)))))
534 ;;; Given a function name, return the name for the BLOCK which
535 ;;; encloses its body (e.g. in DEFUN, DEFINE-COMPILER-MACRO, or FLET).
536 (declaim (ftype (function ((or symbol cons)) symbol) function-name-block-name))
537 (defun function-name-block-name (function-name)
538 (cond ((symbolp function-name)
540 ((and (consp function-name)
541 (= (length function-name) 2)
542 (eq (first function-name) 'setf))
543 (second function-name))
545 (error "not legal as a function name: ~S" function-name))))
547 (defun looks-like-name-of-special-var-p (x)
549 (let ((name (symbol-name x)))
550 (and (> (length name) 2) ; to exclude '* and '**
551 (char= #\* (aref name 0))
552 (char= #\* (aref name (1- (length name))))))))
554 ;;; ANSI guarantees that some symbols are self-evaluating. This
555 ;;; function is to be called just before a change which would affect
556 ;;; that. (We don't absolutely have to call this function before such
557 ;;; changes, since such changes are given as undefined behavior. In
558 ;;; particular, we don't if the runtime cost would be annoying. But
559 ;;; otherwise it's nice to do so.)
560 (defun about-to-modify (symbol)
561 (declare (type symbol symbol))
563 (error "Veritas aeterna. (can't change T)"))
565 (error "Nihil ex nihil. (can't change NIL)"))
567 (error "Keyword values can't be changed."))
568 ;; (Just because a value is CONSTANTP is not a good enough
569 ;; reason to complain here, because we want DEFCONSTANT to
570 ;; be able to use this function, and it's legal to DEFCONSTANT
571 ;; a constant as long as the new value is EQL to the old
577 ;;;; "The macro ONCE-ONLY has been around for a long time on various
578 ;;;; systems [..] if you can understand how to write and when to use
579 ;;;; ONCE-ONLY, then you truly understand macro." -- Peter Norvig,
580 ;;;; _Paradigms of Artificial Intelligence Programming: Case Studies
581 ;;;; in Common Lisp_, p. 853
583 ;;; ONCE-ONLY is a utility useful in writing source transforms and
584 ;;; macros. It provides a concise way to wrap a LET around some code
585 ;;; to ensure that some forms are only evaluated once.
587 ;;; Create a LET* which evaluates each value expression, binding a
588 ;;; temporary variable to the result, and wrapping the LET* around the
589 ;;; result of the evaluation of BODY. Within the body, each VAR is
590 ;;; bound to the corresponding temporary variable.
591 (defmacro once-only (specs &body body)
592 (named-let frob ((specs specs)
596 (let ((spec (first specs)))
597 ;; FIXME: should just be DESTRUCTURING-BIND of SPEC
598 (unless (proper-list-of-length-p spec 2)
599 (error "malformed ONCE-ONLY binding spec: ~S" spec))
600 (let* ((name (first spec))
601 (exp-temp (gensym (symbol-name name))))
602 `(let ((,exp-temp ,(second spec))
603 (,name (gensym "ONCE-ONLY-")))
604 `(let ((,,name ,,exp-temp))
605 ,,(frob (rest specs) body))))))))
607 ;;;; various error-checking utilities
609 ;;; This function can be used as the default value for keyword
610 ;;; arguments that must be always be supplied. Since it is known by
611 ;;; the compiler to never return, it will avoid any compile-time type
612 ;;; warnings that would result from a default value inconsistent with
613 ;;; the declared type. When this function is called, it signals an
614 ;;; error indicating that a required &KEY argument was not supplied.
615 ;;; This function is also useful for DEFSTRUCT slot defaults
616 ;;; corresponding to required arguments.
617 (declaim (ftype (function () nil) required-argument))
618 (defun required-argument ()
620 (/show0 "entering REQUIRED-ARGUMENT")
621 (error "A required &KEY argument was not supplied."))
623 ;;; like CL:ASSERT and CL:CHECK-TYPE, but lighter-weight
625 ;;; (As of sbcl-0.6.11.20, we were using some 400 calls to CL:ASSERT.
626 ;;; The CL:ASSERT restarts and whatnot expand into a significant
627 ;;; amount of code when you multiply them by 400, so replacing them
628 ;;; with this should reduce the size of the system by enough to be
629 ;;; worthwhile. ENFORCE-TYPE is much less common, but might still be
630 ;;; worthwhile, and since I don't really like CERROR stuff deep in the
631 ;;; guts of complex systems anyway, I replaced it too.)
632 (defmacro aver (expr)
634 (%failed-aver ,(let ((*package* (find-package :keyword)))
635 (format nil "~S" expr)))))
636 (defun %failed-aver (expr-as-string)
637 (error "~@<internal error, failed AVER: ~2I~_~S~:>" expr-as-string))
638 (defmacro enforce-type (value type)
639 (once-only ((value value))
640 `(unless (typep ,value ',type)
641 (%failed-enforce-type ,value ',type))))
642 (defun %failed-enforce-type (value type)
643 (error 'simple-type-error
646 :format-string "~@<~S ~_is not a ~_~S~:>"
647 :format-arguments (list value type)))
649 ;;; Return a list of N gensyms. (This is a common suboperation in
650 ;;; macros and other code-manipulating code.)
651 (declaim (ftype (function (index) list) make-gensym-list))
652 (defun make-gensym-list (n)
653 (loop repeat n collect (gensym)))
655 ;;; Return a function like FUN, but expecting its (two) arguments in
656 ;;; the opposite order that FUN does.
657 (declaim (inline swapped-args-fun))
658 (defun swapped-args-fun (fun)
659 (declare (type function fun))
663 ;;; Return the numeric value of a type bound, i.e. an interval bound
664 ;;; more or less in the format of bounds in ANSI's type specifiers,
665 ;;; where a bare numeric value is a closed bound and a list of a
666 ;;; single numeric value is an open bound.
668 ;;; The "more or less" bit is that the no-bound-at-all case is
669 ;;; represented by NIL (not by * as in ANSI type specifiers); and in
670 ;;; this case we return NIL.
671 (defun type-bound-number (x)
673 (destructuring-bind (result) x result)
676 ;;; some commonly-occuring CONSTANTLY forms
677 (macrolet ((def-constantly-fun (name constant-expr)
678 `(setf (symbol-function ',name)
679 (constantly ,constant-expr))))
680 (def-constantly-fun constantly-t t)
681 (def-constantly-fun constantly-nil nil)
682 (def-constantly-fun constantly-0 0))
684 ;;; If X is an atom, see whether it is present in *FEATURES*. Also
685 ;;; handle arbitrary combinations of atoms using NOT, AND, OR.
691 (error "too many subexpressions in feature expression: ~S" x)
692 (not (featurep (cadr x)))))
693 ((:and and) (every #'featurep (cdr x)))
694 ((:or or) (some #'featurep (cdr x)))
696 (error "unknown operator in feature expression: ~S." x)))
697 (not (null (memq x *features*)))))
699 ;;; Given a list of keyword substitutions `(,OLD ,NEW), and a
700 ;;; &KEY-argument-list-style list of alternating keywords and
701 ;;; arbitrary values, return a new &KEY-argument-list-style list with
702 ;;; all substitutions applied to it.
704 ;;; Note: If efficiency mattered, we could do less consing. (But if
705 ;;; efficiency mattered, why would we be using &KEY arguments at
706 ;;; all, much less renaming &KEY arguments?)
708 ;;; KLUDGE: It would probably be good to get rid of this. -- WHN 19991201
709 (defun rename-key-args (rename-list key-args)
710 (declare (type list rename-list key-args))
711 ;; Walk through RENAME-LIST modifying RESULT as per each element in
713 (do ((result (copy-list key-args))) ; may be modified below
714 ((null rename-list) result)
715 (destructuring-bind (old new) (pop rename-list)
716 ;; ANSI says &KEY arg names aren't necessarily KEYWORDs.
717 (declare (type symbol old new))
718 ;; Walk through RESULT renaming any OLD key argument to NEW.
719 (do ((in-result result (cddr in-result)))
721 (declare (type list in-result))
722 (when (eq (car in-result) old)
723 (setf (car in-result) new))))))
725 ;;; ANSI Common Lisp's READ-SEQUENCE function, unlike most of the
726 ;;; other ANSI input functions, is defined to communicate end of file
727 ;;; status with its return value, not by signalling. That is not the
728 ;;; behavior that we usually want. This function is a wrapper which
729 ;;; restores the behavior that we usually want, causing READ-SEQUENCE
730 ;;; to communicate end-of-file status by signalling.
731 (defun read-sequence-or-die (sequence stream &key start end)
732 ;; implementation using READ-SEQUENCE
733 #-no-ansi-read-sequence
734 (let ((read-end (read-sequence sequence
738 (unless (= read-end end)
739 (error 'end-of-file :stream stream))
741 ;; workaround for broken READ-SEQUENCE
742 #+no-ansi-read-sequence
744 (aver (<= start end))
745 (let ((etype (stream-element-type stream)))
746 (cond ((equal etype '(unsigned-byte 8))
747 (do ((i start (1+ i)))
750 (setf (aref sequence i)
751 (read-byte stream))))
752 (t (error "unsupported element type ~S" etype))))))
754 ;;;; utilities for two-VALUES predicates
756 ;;; sort of like ANY and EVERY, except:
757 ;;; * We handle two-VALUES predicate functions, as SUBTYPEP does.
758 ;;; (And if the result is uncertain, then we return (VALUES NIL NIL),
759 ;;; as SUBTYPEP does.)
760 ;;; * THING is just an atom, and we apply OP (an arity-2 function)
761 ;;; successively to THING and each element of LIST.
762 (defun any/type (op thing list)
763 (declare (type function op))
765 (dolist (i list (values nil certain?))
766 (multiple-value-bind (sub-value sub-certain?) (funcall op thing i)
768 (when sub-value (return (values t t)))
769 (setf certain? nil))))))
770 (defun every/type (op thing list)
771 (declare (type function op))
773 (dolist (i list (if certain? (values t t) (values nil nil)))
774 (multiple-value-bind (sub-value sub-certain?) (funcall op thing i)
776 (unless sub-value (return (values nil t)))
777 (setf certain? nil))))))
781 ;;; These functions are called by the expansion of the DEFPRINTER
782 ;;; macro to do the actual printing.
783 (declaim (ftype (function (symbol t stream) (values))
784 defprinter-prin1 defprinter-princ))
785 (defun defprinter-prin1 (name value stream)
786 (defprinter-prinx #'prin1 name value stream))
787 (defun defprinter-princ (name value stream)
788 (defprinter-prinx #'princ name value stream))
789 (defun defprinter-prinx (prinx name value stream)
790 (declare (type function prinx))
792 (pprint-newline :linear stream))
793 (format stream ":~A " name)
794 (funcall prinx value stream)
796 (defun defprinter-print-space (stream)
797 (write-char #\space stream))
799 ;;; Define some kind of reasonable PRINT-OBJECT method for a
800 ;;; STRUCTURE-OBJECT class.
802 ;;; NAME is the name of the structure class, and CONC-NAME is the same
805 ;;; The SLOT-DESCS describe how each slot should be printed. Each
806 ;;; SLOT-DESC can be a slot name, indicating that the slot should
807 ;;; simply be printed. A SLOT-DESC may also be a list of a slot name
808 ;;; and other stuff. The other stuff is composed of keywords followed
809 ;;; by expressions. The expressions are evaluated with the variable
810 ;;; which is the slot name bound to the value of the slot. These
811 ;;; keywords are defined:
813 ;;; :PRIN1 Print the value of the expression instead of the slot value.
814 ;;; :PRINC Like :PRIN1, only PRINC the value
815 ;;; :TEST Only print something if the test is true.
817 ;;; If no printing thing is specified then the slot value is printed
820 ;;; The structure being printed is bound to STRUCTURE and the stream
821 ;;; is bound to STREAM.
822 (defmacro defprinter ((name &key (conc-name (concatenate 'simple-string
828 (reversed-prints nil)
829 (stream (gensym "STREAM")))
830 (flet ((sref (slot-name)
831 `(,(symbolicate conc-name slot-name) structure)))
832 (dolist (slot-desc slot-descs)
834 (setf maybe-print-space nil
836 (setf maybe-print-space `(defprinter-print-space ,stream)))
837 (cond ((atom slot-desc)
838 (push maybe-print-space reversed-prints)
839 (push `(defprinter-prin1 ',slot-desc ,(sref slot-desc) ,stream)
842 (let ((sname (first slot-desc))
845 (do ((option (rest slot-desc) (cddr option)))
847 (push `(let ((,sname ,(sref sname)))
852 ',sname ,sname ,stream)))))
856 (stuff `(defprinter-prin1
857 ',sname ,(second option) ,stream)))
859 (stuff `(defprinter-princ
860 ',sname ,(second option) ,stream)))
861 (:test (setq test (second option)))
863 (error "bad option: ~S" (first option)))))))))))
864 `(def!method print-object ((structure ,name) ,stream)
865 ;; FIXME: should probably be byte-compiled
866 (pprint-logical-block (,stream nil)
867 (print-unreadable-object (structure ,stream :type t)
868 ,@(nreverse reversed-prints))))))
872 ;;; Given a pathname, return a corresponding physical pathname.
873 (defun physicalize-pathname (possibly-logical-pathname)
874 (if (typep possibly-logical-pathname 'logical-pathname)
875 (translate-logical-pathname possibly-logical-pathname)
876 possibly-logical-pathname))
879 ;;; REMOVEME when done testing byte cross-compiler
880 (defun byte-compiled-foo (x y)
881 (declare (optimize (speed 0) (debug 1)))