1 ;;;; macros, global variable definitions, and other miscellaneous support stuff
2 ;;;; used by the rest of the PCL subsystem
4 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; This software is derived from software originally released by Xerox
8 ;;;; Corporation. Copyright and release statements follow. Later modifications
9 ;;;; to the software are in the public domain and are provided with
10 ;;;; absolutely no warranty. See the COPYING and CREDITS files for more
13 ;;;; copyright information from original PCL sources:
15 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
16 ;;;; All rights reserved.
18 ;;;; Use and copying of this software and preparation of derivative works based
19 ;;;; upon this software are permitted. Any distribution of this software or
20 ;;;; derivative works must comply with all applicable United States export
23 ;;;; This software is made available AS IS, and Xerox Corporation makes no
24 ;;;; warranty about the software, its performance or its conformity to any
30 ;; These three nonstandard declarations seem to be used
31 ;; privately within PCL itself to pass information around,
32 ;; so we can't just delete them.
36 ;; This declaration may also be used within PCL to pass
37 ;; information around, I'm not sure. -- WHN 2000-12-30
40 ;;; comment from CMU CL PCL:
41 ;;; These are age-old functions which CommonLisp cleaned-up away. They
42 ;;; probably exist in other packages in all CommonLisp
43 ;;; implementations, but I will leave it to the compiler to optimize
44 ;;; into calls to them.
46 ;;; FIXME: MEMQ, ASSQ, and DELQ are already defined in SBCL, and we should
47 ;;; use those. POSQ and NEQ aren't defined in SBCL, and are used too often
48 ;;; in PCL to make it appealing to hand expand all uses and then delete
49 ;;; the macros, so they should be boosted up to SBCL to stand by MEMQ,
51 (defmacro memq (item list) `(member ,item ,list :test #'eq))
52 (defmacro assq (item list) `(assoc ,item ,list :test #'eq))
53 (defmacro delq (item list) `(delete ,item ,list :test #'eq))
54 (defmacro posq (item list) `(position ,item ,list :test #'eq))
55 (defmacro neq (x y) `(not (eq ,x ,y)))
57 ;;; FIXME: Rename these to CONSTANTLY-T, CONSTANTLY-NIL, and
58 ;;; CONSTANTLY-0, and boost them up to SB-INT.
59 (defun true (&rest ignore) (declare (ignore ignore)) t)
60 (defun false (&rest ignore) (declare (ignore ignore)) nil)
61 (defun zero (&rest ignore) (declare (ignore ignore)) 0)
63 ;;; comment from original CMU CL PCL: ONCE-ONLY does the same thing as
64 ;;; it does in zetalisp. I should have just lifted it from there but I
65 ;;; am honest. Not only that but this one is written in Common Lisp. I
66 ;;; feel a lot like bootstrapping, or maybe more like rebuilding Rome.
68 ;;; FIXME: We should only need one ONCE-ONLY in SBCL, and there's one
69 ;;; in SB-INT already. Can we use only one of these in both places?
70 (defmacro once-only (vars &body body)
71 (let ((gensym-var (gensym))
72 (run-time-vars (gensym))
73 (run-time-vals (gensym))
74 (expand-time-val-forms ()))
76 (push `(if (or (symbolp ,var)
79 (member (car ,var) '(quote function))))
81 (let ((,gensym-var (gensym)))
82 (push ,gensym-var ,run-time-vars)
83 (push ,var ,run-time-vals)
85 expand-time-val-forms))
86 `(let* (,run-time-vars
89 (let ,(mapcar #'list vars (reverse expand-time-val-forms))
91 `(let ,(mapcar #'list (reverse ,run-time-vars)
92 (reverse ,run-time-vals))
95 ;;; FIXME: This looks like SBCL's PARSE-BODY, and should be shared.
96 (eval-when (:compile-toplevel :load-toplevel :execute)
97 (defun extract-declarations (body &optional environment)
98 ;;(declare (values documentation declarations body))
102 (when (and (stringp (car body))
104 (setq documentation (pop body)))
107 (when (null body) (return-from outer nil))
108 (setq form (car body))
110 (loop (cond ((not (listp form))
111 (return-from outer nil))
112 ((eq (car form) 'declare)
113 (return-from inner 't))
115 (multiple-value-bind (newform macrop)
116 (macroexpand-1 form environment)
117 (if (or (not (eq newform form)) macrop)
119 (return-from outer nil)))))))
121 (dolist (declaration (cdr form))
122 (push declaration declarations)))))
123 (values documentation
124 (and declarations `((declare ,.(nreverse declarations))))
128 (defun get-declaration (name declarations &optional default)
129 (dolist (d declarations default)
130 (dolist (form (cdr d))
131 (when (and (consp form) (eq (car form) name))
132 (return-from get-declaration (cdr form))))))
134 (defmacro collecting-once (&key initial-value)
135 `(let* ((head ,initial-value)
136 (tail ,(and initial-value `(last head))))
137 (values #'(lambda (value)
139 (setq head (setq tail (list value)))
140 (unless (memq value head)
142 (cdr (rplacd tail (list value)))))))
143 #'(lambda nil head))))
145 (defmacro doplist ((key val) plist &body body &environment env)
146 (multiple-value-bind (doc decls bod)
147 (extract-declarations body env)
148 (declare (ignore doc))
149 `(let ((.plist-tail. ,plist) ,key ,val)
151 (loop (when (null .plist-tail.) (return nil))
152 (setq ,key (pop .plist-tail.))
153 (when (null .plist-tail.)
154 (error "malformed plist, odd number of elements"))
155 (setq ,val (pop .plist-tail.))
158 (defmacro dolist-carefully ((var list improper-list-handler) &body body)
160 (.dolist-carefully. ,list))
161 (loop (when (null .dolist-carefully.) (return nil))
162 (if (consp .dolist-carefully.)
164 (setq ,var (pop .dolist-carefully.))
166 (,improper-list-handler)))))
168 ;;; FIXME: Do we really need this? It seems to be used only
169 ;;; for class names. Why not just the default ALL-CAPS?
170 (defun capitalize-words (string &optional (dashes-p t))
171 (let ((string (copy-seq (string string))))
172 (declare (string string))
174 (length (length string) length)
177 ((= i length) string)
178 (setq char (elt string i))
179 (cond ((both-case-p char)
181 (and (setq flag (lower-case-p char))
182 (setf (elt string i) (char-upcase char)))
183 (and (not flag) (setf (elt string i) (char-downcase char))))
185 ((char-equal char #\-)
187 (unless dashes-p (setf (elt string i) #\space)))
188 (t (setq flag nil))))))
192 ;;;; This is documented in the CLOS specification.
193 ;;;; KLUDGE: Except that SBCL deviates from the spec by having CL:FIND-CLASS
194 ;;;; distinct from PCL:FIND-CLASS, alas. -- WHN 19991203
196 (defvar *find-class* (make-hash-table :test 'eq))
198 (defun function-returning-nil (x)
202 (defun function-returning-t (x)
206 (defmacro find-class-cell-class (cell)
209 (defmacro find-class-cell-predicate (cell)
212 (defmacro find-class-cell-make-instance-function-keys (cell)
215 (defmacro make-find-class-cell (class-name)
216 (declare (ignore class-name))
217 '(list* nil #'function-returning-nil nil))
219 (defun find-class-cell (symbol &optional dont-create-p)
220 (or (gethash symbol *find-class*)
221 (unless dont-create-p
222 (unless (legal-class-name-p symbol)
223 (error "~S is not a legal class name." symbol))
224 (setf (gethash symbol *find-class*) (make-find-class-cell symbol)))))
226 (defvar *create-classes-from-internal-structure-definitions-p* t)
228 (defun find-class-from-cell (symbol cell &optional (errorp t))
229 (or (find-class-cell-class cell)
230 (and *create-classes-from-internal-structure-definitions-p*
231 (structure-type-p symbol)
232 (find-structure-class symbol))
233 (cond ((null errorp) nil)
234 ((legal-class-name-p symbol)
235 (error "There is no class named ~S." symbol))
237 (error "~S is not a legal class name." symbol)))))
239 (defun find-class-predicate-from-cell (symbol cell &optional (errorp t))
240 (unless (find-class-cell-class cell)
241 (find-class-from-cell symbol cell errorp))
242 (find-class-cell-predicate cell))
244 (defun legal-class-name-p (x)
248 (defun find-class (symbol &optional (errorp t) environment)
249 (declare (ignore environment))
250 (find-class-from-cell symbol
251 (find-class-cell symbol errorp)
254 (defun find-class-predicate (symbol &optional (errorp t) environment)
255 (declare (ignore environment))
256 (find-class-predicate-from-cell symbol
257 (find-class-cell symbol errorp)
260 ;;; This DEFVAR was originally in defs.lisp, now moved here.
262 ;;; Possible values are NIL, EARLY, BRAID, or COMPLETE.
264 ;;; KLUDGE: This should probably become
265 ;;; (DECLAIM (TYPE (MEMBER NIL :EARLY :BRAID :COMPLETE) *BOOT-STATE*))
266 (defvar *boot-state* nil)
268 ;;; Note that in SBCL as in CMU CL,
269 ;;; COMMON-LISP:FIND-CLASS /= SB-PCL:FIND-CLASS.
270 ;;; (Yes, this is a KLUDGE!)
271 (define-compiler-macro find-class (&whole form
272 symbol &optional (errorp t) environment)
273 (declare (ignore environment))
274 (if (and (constantp symbol)
275 (legal-class-name-p (eval symbol))
277 (member *boot-state* '(braid complete)))
278 (let ((symbol (eval symbol))
279 (errorp (not (null (eval errorp))))
280 (class-cell (make-symbol "CLASS-CELL")))
281 `(let ((,class-cell (load-time-value (find-class-cell ',symbol))))
282 (or (find-class-cell-class ,class-cell)
284 `(find-class-from-cell ',symbol ,class-cell t)
285 `(and (sb-kernel:class-cell-class
286 ',(sb-kernel:find-class-cell symbol))
287 (find-class-from-cell ',symbol ,class-cell nil))))))
290 ;;; FIXME: These #-SETF forms are pretty ugly. Could they please go away?
292 (defsetf find-class (symbol &optional (errorp t) environment) (new-value)
293 (declare (ignore errorp environment))
294 `(SETF\ SB-PCL\ FIND-CLASS ,new-value ,symbol))
296 (defun #-setf SETF\ SB-PCL\ FIND-CLASS #+setf (setf find-class) (new-value
298 (if (legal-class-name-p symbol)
299 (let ((cell (find-class-cell symbol)))
300 (setf (find-class-cell-class cell) new-value)
301 (when (or (eq *boot-state* 'complete)
302 (eq *boot-state* 'braid))
303 (when (and new-value (class-wrapper new-value))
304 (setf (find-class-cell-predicate cell)
305 (symbol-function (class-predicate-name new-value))))
306 (when (and new-value (not (forward-referenced-class-p new-value)))
308 (dolist (keys+aok (find-class-cell-make-instance-function-keys cell))
309 (update-initialize-info-internal
310 (initialize-info new-value (car keys+aok) nil (cdr keys+aok))
311 'make-instance-function))))
313 (error "~S is not a legal class name." symbol)))
316 (defsetf find-class-predicate (symbol &optional (errorp t) environment) (new-value)
317 (declare (ignore errorp environment))
318 `(SETF\ SB-PCL\ FIND-CLASS-PREDICATE ,new-value ,symbol))
320 (defun #-setf SETF\ SB-PCL\ FIND-CLASS-PREDICATE
321 #+setf (setf find-class-predicate)
323 (if (legal-class-name-p symbol)
324 (setf (find-class-cell-predicate (find-class-cell symbol)) new-value)
325 (error "~S is not a legal class name." symbol)))
327 (defun find-wrapper (symbol)
328 (class-wrapper (find-class symbol)))
330 (defmacro gathering1 (gatherer &body body)
331 `(gathering ((.gathering1. ,gatherer))
332 (macrolet ((gather1 (x) `(gather ,x .gathering1.)))
335 (defmacro vectorizing (&key (size 0))
336 `(let* ((limit ,size)
337 (result (make-array limit))
339 (values #'(lambda (value)
341 (error "vectorizing more elements than promised")
343 (setf (svref result index) value)
346 #'(lambda () result))))
348 ;;; These are augmented definitions of list-elements and list-tails from
349 ;;; iterate.lisp. These versions provide the extra :by keyword which can
350 ;;; be used to specify the step function through the list.
351 (defmacro *list-elements (list &key (by #'cdr))
357 (setq tail (funcall ,by tail)))))))
359 (defmacro *list-tails (list &key (by #'cdr))
362 (prog1 (if (endp tail)
365 (setq tail (funcall ,by tail))))))
367 (defmacro function-funcall (form &rest args)
368 `(funcall (the function ,form) ,@args))
370 (defmacro function-apply (form &rest args)
371 `(apply (the function ,form) ,@args))
373 ;;;; various nastiness to work around nonstandardness of SETF when PCL
376 ;;; Convert a function name to its standard SETF function name. We
377 ;;; have to do this hack because not all Common Lisps have yet
378 ;;; converted to having SETF function specs.
380 ;;; KLUDGE: We probably don't have to do this any more. But in Debian
381 ;;; cmucl 2.4.8 the :SETF feature isn't set (?). Perhaps it's because of
382 ;;; the comment ca. 10 lines down about how the built-in setf mechanism
383 ;;; takes a hash table lookup each time? It would be nice to go one
384 ;;; way or another on this, perhaps some benchmarking would be in order..
385 ;;; (Oh, more info: In debian src/pcl/notes.text, which looks like stale
386 ;;; documentation from 1992, it says TO DO: When CMU CL improves its
387 ;;; SETF handling, remove the comment in macros.lisp beginning the line
388 ;;; #+CMU (PUSHNEW :SETF *FEATURES*). So since CMU CL's (and now SBCL's)
389 ;;; SETF handling seems OK to me these days, there's a fairly decent chance
390 ;;; this would work.) -- WHN 19991203
392 ;;; In a port that does have SETF function specs you can use those just by
393 ;;; making the obvious simple changes to these functions. The rest of PCL
394 ;;; believes that there are function names like (SETF <foo>), this is the
395 ;;; only place that knows about this hack.
396 (eval-when (:compile-toplevel :load-toplevel :execute)
397 ; In 15e (and also 16c), using the built-in SETF mechanism costs
398 ; a hash table lookup every time a SETF function is called.
399 ; Uncomment the next line to use the built in SETF mechanism.
400 ;#+cmu (pushnew :setf *features*)
403 (eval-when (:compile-toplevel :load-toplevel :execute)
406 (defvar *setf-function-names* (make-hash-table :size 200 :test 'eq))
408 (defun get-setf-function-name (name)
411 (or (gethash name *setf-function-names*)
412 (setf (gethash name *setf-function-names*)
413 (let ((pkg (symbol-package name)))
420 (make-symbol (format nil "SETF ~A" (symbol-name name))))))))
422 ;;; Call this to define a setf macro for a function with the same behavior as
423 ;;; specified by the SETF function cleanup proposal. Specifically, this will
424 ;;; cause: (SETF (FOO a b) x) to expand to (|SETF FOO| x a b).
426 ;;; do-standard-defsetf A macro interface for use at top level
427 ;;; in files. Unfortunately, users may
428 ;;; have to use this for a while.
430 ;;; do-standard-defsetfs-for-defclass A special version called by defclass.
432 ;;; do-standard-defsetf-1 A functional interface called by the
433 ;;; above, defmethod and defgeneric.
434 ;;; Since this is all a crock anyways,
435 ;;; users are free to call this as well.
437 ;;; FIXME: Once we fix up SETF, a lot of stuff around here should evaporate.
438 (defmacro do-standard-defsetf (&rest function-names)
439 `(eval-when (:compile-toplevel :load-toplevel :execute)
440 (dolist (fn-name ',function-names) (do-standard-defsetf-1 fn-name))))
442 (defun do-standard-defsetfs-for-defclass (accessors)
443 (dolist (name accessors) (do-standard-defsetf-1 name)))
445 (defun do-standard-defsetf-1 (function-name)
447 (declare (ignore function-name))
450 (unless (and (setfboundp function-name)
451 (get function-name 'standard-setf))
452 (setf (get function-name 'standard-setf) t)
453 (let* ((setf-function-name (get-setf-function-name function-name)))
454 (eval `(defsetf ,function-name (&rest accessor-args) (new-value)
455 (let* ((bindings (mapcar #'(lambda (x) `(,(gensym) ,x)) accessor-args))
456 (vars (mapcar #'car bindings)))
458 (,',setf-function-name ,new-value ,@vars))))))))
460 (defun setfboundp (symbol)
461 (fboundp `(setf ,symbol)))
465 ;;; PCL, like user code, must endure the fact that we don't have a
466 ;;; properly working SETF. Many things work because they get mentioned
467 ;;; by a DEFCLASS or DEFMETHOD before they are used, but others have
468 ;;; to be done by hand.
470 ;;; FIXME: We don't have to do this stuff any more, do we?
473 generic-function-name
474 method-function-plist
479 slot-value-using-class)
481 (defsetf slot-value set-slot-value)