/[cmucl]/src/pcl/macros.lisp
ViewVC logotype

Contents of /src/pcl/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (show annotations)
Thu Feb 6 21:24:14 1997 UTC (17 years, 2 months ago) by pw
Branch: MAIN
CVS Tags: RELEASE_18a
Branch point for: RELENG_18
Changes since 1.6: +47 -7 lines
This commit brings the PCL repository up to release 17f and also includes
the changes in the X86 1.03.7 kit. Although the 1.03.7 changes were
already in the repository, the other files appear to be of early vintage
and do not work with a 17f system. Hopefully all is correctly synched now.
1 ;;;-*-Mode:LISP; Package:(PCL (LISP WALKER)); Base:10; Syntax:Common-lisp -*-
2 ;;;
3 ;;; *************************************************************************
4 ;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
5 ;;; All rights reserved.
6 ;;;
7 ;;; Use and copying of this software and preparation of derivative works
8 ;;; based upon this software are permitted. Any distribution of this
9 ;;; software or derivative works must comply with all applicable United
10 ;;; States export control laws.
11 ;;;
12 ;;; This software is made available AS IS, and Xerox Corporation makes no
13 ;;; warranty about the software, its performance or its conformity to any
14 ;;; specification.
15 ;;;
16 ;;; Any person obtaining a copy of this software is requested to send their
17 ;;; name and post office or electronic mail address to:
18 ;;; CommonLoops Coordinator
19 ;;; Xerox PARC
20 ;;; 3333 Coyote Hill Rd.
21 ;;; Palo Alto, CA 94304
22 ;;; (or send Arpanet mail to CommonLoops-Coordinator.pa@Xerox.arpa)
23 ;;;
24 ;;; Suggestions, comments and requests for improvements are also welcome.
25 ;;; *************************************************************************
26 ;;;
27 ;;; Macros global variable definitions, and other random support stuff used
28 ;;; by the rest of the system.
29 ;;;
30 ;;; For simplicity (not having to use eval-when a lot), this file must be
31 ;;; loaded before it can be compiled.
32 ;;;
33
34 (in-package :pcl)
35
36 (proclaim '(declaration
37 #-Genera values ;I use this so that Zwei can remind
38 ;me what values a function returns.
39
40 #-Genera arglist ;Tells me what the pretty arglist
41 ;of something (which probably takes
42 ;&rest args) is.
43
44 #-Genera indentation ;Tells ZWEI how to indent things
45 ;like defclass.
46 class
47 variable-rebinding
48 pcl-fast-call
49 method-name
50 method-lambda-list
51 ))
52
53 ;;; Age old functions which CommonLisp cleaned-up away. They probably exist
54 ;;; in other packages in all CommonLisp implementations, but I will leave it
55 ;;; to the compiler to optimize into calls to them.
56 ;;;
57 ;;; Common Lisp BUG:
58 ;;; Some Common Lisps define these in the Lisp package which causes
59 ;;; all sorts of lossage. Common Lisp should explictly specify which
60 ;;; symbols appear in the Lisp package.
61 ;;;
62 (eval-when (compile load eval)
63
64 (defmacro memq (item list) `(member ,item ,list :test #'eq))
65 (defmacro assq (item list) `(assoc ,item ,list :test #'eq))
66 (defmacro rassq (item list) `(rassoc ,item ,list :test #'eq))
67 (defmacro delq (item list) `(delete ,item ,list :test #'eq))
68 (defmacro posq (item list) `(position ,item ,list :test #'eq))
69 (defmacro neq (x y) `(not (eq ,x ,y)))
70
71
72 (defun make-caxr (n form)
73 (if (< n 4)
74 `(,(nth n '(car cadr caddr cadddr)) ,form)
75 (make-caxr (- n 4) `(cddddr ,form))))
76
77 (defun make-cdxr (n form)
78 (cond ((zerop n) form)
79 ((< n 5) `(,(nth n '(identity cdr cddr cdddr cddddr)) ,form))
80 (t (make-cdxr (- n 4) `(cddddr ,form)))))
81 )
82
83 (defun true (&rest ignore) (declare (ignore ignore)) t)
84 (defun false (&rest ignore) (declare (ignore ignore)) nil)
85 (defun zero (&rest ignore) (declare (ignore ignore)) 0)
86
87 (defun make-plist (keys vals)
88 (if (null vals)
89 ()
90 (list* (car keys)
91 (car vals)
92 (make-plist (cdr keys) (cdr vals)))))
93
94 (defun remtail (list tail)
95 (if (eq list tail) () (cons (car list) (remtail (cdr list) tail))))
96
97 ;;; ONCE-ONLY does the same thing as it does in zetalisp. I should have just
98 ;;; lifted it from there but I am honest. Not only that but this one is
99 ;;; written in Common Lisp. I feel a lot like bootstrapping, or maybe more
100 ;;; like rebuilding Rome.
101 (defmacro once-only (vars &body body)
102 (let ((gensym-var (gensym))
103 (run-time-vars (gensym))
104 (run-time-vals (gensym))
105 (expand-time-val-forms ()))
106 (dolist (var vars)
107 (push `(if (or (symbolp ,var)
108 (numberp ,var)
109 (and (listp ,var)
110 (member (car ,var) '(quote function))))
111 ,var
112 (let ((,gensym-var (gensym)))
113 (push ,gensym-var ,run-time-vars)
114 (push ,var ,run-time-vals)
115 ,gensym-var))
116 expand-time-val-forms))
117 `(let* (,run-time-vars
118 ,run-time-vals
119 (wrapped-body
120 (let ,(mapcar #'list vars (reverse expand-time-val-forms))
121 ,@body)))
122 `(let ,(mapcar #'list (reverse ,run-time-vars)
123 (reverse ,run-time-vals))
124 ,wrapped-body))))
125
126 (eval-when (compile load eval)
127 (defun extract-declarations (body &optional environment)
128 ;;(declare (values documentation declarations body))
129 (let (documentation declarations form)
130 (when (and (stringp (car body))
131 (cdr body))
132 (setq documentation (pop body)))
133 (block outer
134 (loop
135 (when (null body) (return-from outer nil))
136 (setq form (car body))
137 (when (block inner
138 (loop (cond ((not (listp form))
139 (return-from outer nil))
140 ((eq (car form) 'declare)
141 (return-from inner 't))
142 (t
143 (multiple-value-bind (newform macrop)
144 (macroexpand-1 form environment)
145 (if (or (not (eq newform form)) macrop)
146 (setq form newform)
147 (return-from outer nil)))))))
148 (pop body)
149 (dolist (declaration (cdr form))
150 (push declaration declarations)))))
151 (values documentation
152 (and declarations `((declare ,.(nreverse declarations))))
153 body)))
154 )
155
156 (defun get-declaration (name declarations &optional default)
157 (dolist (d declarations default)
158 (dolist (form (cdr d))
159 (when (and (consp form) (eq (car form) name))
160 (return-from get-declaration (cdr form))))))
161
162
163 #+Lucid
164 (eval-when (compile load eval)
165 (eval `(defstruct ,(intern "FASLESCAPE" (find-package 'lucid)))))
166
167 (defvar *keyword-package* (find-package 'keyword))
168
169 (defun make-keyword (symbol)
170 (intern (symbol-name symbol) *keyword-package*))
171
172 (eval-when (compile load eval)
173
174 (defun string-append (&rest strings)
175 (setq strings (copy-list strings)) ;The explorer can't even
176 ;rplaca an &rest arg?
177 (do ((string-loc strings (cdr string-loc)))
178 ((null string-loc)
179 (apply #'concatenate 'string strings))
180 (rplaca string-loc (string (car string-loc)))))
181 )
182
183 (defun symbol-append (sym1 sym2 &optional (package *package*))
184 (intern (string-append sym1 sym2) package))
185
186 (defmacro check-member (place list &key (test #'eql) (pretty-name place))
187 (once-only (place list)
188 `(or (member ,place ,list :test ,test)
189 (error "The value of ~A, ~S is not one of ~S."
190 ',pretty-name ,place ,list))))
191
192 (defmacro alist-entry (alist key make-entry-fn)
193 (once-only (alist key)
194 `(or (assq ,key ,alist)
195 (progn (setf ,alist (cons (,make-entry-fn ,key) ,alist))
196 (car ,alist)))))
197
198 ;;; A simple version of destructuring-bind.
199
200 ;;; This does no more error checking than CAR and CDR themselves do. Some
201 ;;; attempt is made to be smart about preserving intermediate values. It
202 ;;; could be better, although the only remaining case should be easy for
203 ;;; the compiler to spot since it compiles to PUSH POP.
204 ;;;
205 ;;; Common Lisp BUG:
206 ;;; Common Lisp should have destructuring-bind.
207 ;;;
208 (defmacro destructuring-bind (pattern form &body body)
209 (multiple-value-bind (ignore declares body)
210 (extract-declarations body)
211 (declare (ignore ignore))
212 (multiple-value-bind (setqs binds)
213 (destructure pattern form)
214 `(let ,binds
215 ,@declares
216 ,@setqs
217 (progn .destructure-form.)
218 . ,body))))
219
220 (eval-when (compile load eval)
221 (defun destructure (pattern form)
222 ;;(declare (values setqs binds))
223 (let ((*destructure-vars* ())
224 (setqs ()))
225 (declare (special *destructure-vars*))
226 (setq *destructure-vars* '(.destructure-form.)
227 setqs (list `(setq .destructure-form. ,form))
228 form '.destructure-form.)
229 (values (nconc setqs (nreverse (destructure-internal pattern form)))
230 (delete nil *destructure-vars*))))
231
232 (defun destructure-internal (pattern form)
233 ;; When we are called, pattern must be a list. Form should be a symbol
234 ;; which we are free to setq containing the value to be destructured.
235 ;; Optimizations are performed for the last element of pattern cases.
236 ;; we assume that the compiler is smart about gensyms which are bound
237 ;; but only for a short period of time.
238 (declare (special *destructure-vars*))
239 (let ((gensym (gensym))
240 (pending-pops 0)
241 (var nil)
242 (setqs ()))
243 (labels
244 ((make-pop (var form pop-into)
245 (prog1
246 (cond ((zerop pending-pops)
247 `(progn ,(and var `(setq ,var (car ,form)))
248 ,(and pop-into `(setq ,pop-into (cdr ,form)))))
249 ((null pop-into)
250 (and var `(setq ,var ,(make-caxr pending-pops form))))
251 (t
252 `(progn (setq ,pop-into ,(make-cdxr pending-pops form))
253 ,(and var `(setq ,var (pop ,pop-into))))))
254 (setq pending-pops 0))))
255 (do ((pat pattern (cdr pat)))
256 ((null pat) ())
257 (if (symbolp (setq var (car pat)))
258 (progn
259 #-:coral (unless (memq var '(nil ignore))
260 (push var *destructure-vars*))
261 #+:coral (push var *destructure-vars*)
262 (cond ((null (cdr pat))
263 (push (make-pop var form ()) setqs))
264 ((symbolp (cdr pat))
265 (push (make-pop var form (cdr pat)) setqs)
266 (push (cdr pat) *destructure-vars*)
267 (return ()))
268 #-:coral
269 ((memq var '(nil ignore)) (incf pending-pops))
270 #-:coral
271 ((memq (cadr pat) '(nil ignore))
272 (push (make-pop var form ()) setqs)
273 (incf pending-pops 1))
274 (t
275 (push (make-pop var form form) setqs))))
276 (progn
277 (push `(let ((,gensym ()))
278 ,(make-pop gensym
279 form
280 (if (symbolp (cdr pat)) (cdr pat) form))
281 ,@(nreverse
282 (destructure-internal
283 (if (consp pat) (car pat) pat)
284 gensym)))
285 setqs)
286 (when (symbolp (cdr pat))
287 (push (cdr pat) *destructure-vars*)
288 (return)))))
289 setqs)))
290 )
291
292
293 (defmacro collecting-once (&key initial-value)
294 `(let* ((head ,initial-value)
295 (tail ,(and initial-value `(last head))))
296 (values #'(lambda (value)
297 (if (null head)
298 (setq head (setq tail (list value)))
299 (unless (memq value head)
300 (setq tail
301 (cdr (rplacd tail (list value)))))))
302 #'(lambda nil head))))
303
304 (defmacro doplist ((key val) plist &body body &environment env)
305 (multiple-value-bind (doc decls bod)
306 (extract-declarations body env)
307 (declare (ignore doc))
308 `(let ((.plist-tail. ,plist) ,key ,val)
309 ,@decls
310 (loop (when (null .plist-tail.) (return nil))
311 (setq ,key (pop .plist-tail.))
312 (when (null .plist-tail.)
313 (error "Malformed plist in doplist, odd number of elements."))
314 (setq ,val (pop .plist-tail.))
315 (progn ,@bod)))))
316
317 (defmacro if* (condition true &rest false)
318 `(if ,condition ,true (progn ,@false)))
319
320 (defmacro dolist-carefully ((var list improper-list-handler) &body body)
321 `(let ((,var nil)
322 (.dolist-carefully. ,list))
323 (loop (when (null .dolist-carefully.) (return nil))
324 (if (consp .dolist-carefully.)
325 (progn
326 (setq ,var (pop .dolist-carefully.))
327 ,@body)
328 (,improper-list-handler)))))
329
330 ;;
331 ;;;;;; printing-random-thing
332 ;;
333 ;;; Similar to printing-random-object in the lisp machine but much simpler
334 ;;; and machine independent.
335 (defmacro printing-random-thing ((thing stream) &body body)
336 #+cmu17
337 `(print-unreadable-object (,thing ,stream :identity t) ,@body)
338 #-cmu17
339 (once-only (thing stream)
340 `(progn
341 (when *print-readably*
342 (error "~S cannot be printed readably." thing))
343 (format ,stream "#<")
344 ,@body
345 (format ,stream " ")
346 (printing-random-thing-internal ,thing ,stream)
347 (format ,stream ">"))))
348
349 (defun printing-random-thing-internal (thing stream)
350 (declare (ignore thing stream))
351 nil)
352
353 ;;
354 ;;;;;;
355 ;;
356
357 (defun capitalize-words (string &optional (dashes-p t))
358 (let ((string (copy-seq (string string))))
359 (declare (string string))
360 (do* ((flag t flag)
361 (length (length string) length)
362 (char nil char)
363 (i 0 (+ i 1)))
364 ((= i length) string)
365 (setq char (elt string i))
366 (cond ((both-case-p char)
367 (if flag
368 (and (setq flag (lower-case-p char))
369 (setf (elt string i) (char-upcase char)))
370 (and (not flag) (setf (elt string i) (char-downcase char))))
371 (setq flag nil))
372 ((char-equal char #\-)
373 (setq flag t)
374 (unless dashes-p (setf (elt string i) #\space)))
375 (t (setq flag nil))))))
376
377 #-(or lucid kcl)
378 (eval-when (compile load eval)
379 ;(warn "****** Things will go faster if you fix define-compiler-macro")
380 )
381
382 #-cmu
383 (defmacro define-compiler-macro (name arglist &body body)
384 #+(or lucid kcl)
385 `(#+lucid lcl:def-compiler-macro #+kcl si::define-compiler-macro
386 ,name ,arglist
387 ,@body)
388 #-(or kcl lucid)
389 (declare (ignore name arglist body))
390 #-(or kcl lucid)
391 nil)
392
393
394 ;;;
395 ;;; FIND-CLASS
396 ;;;
397 ;;; This is documented in the CLOS specification.
398 ;;;
399 (defvar *find-class* (make-hash-table :test #'eq))
400
401 (defun make-constant-function (value)
402 #'(lambda (object)
403 (declare (ignore object))
404 value))
405
406 (defun function-returning-nil (x)
407 (declare (ignore x))
408 nil)
409
410 (defun function-returning-t (x)
411 (declare (ignore x))
412 t)
413
414 (defmacro find-class-cell-class (cell)
415 `(car ,cell))
416
417 (defmacro find-class-cell-predicate (cell)
418 `(cadr ,cell))
419
420 (defmacro find-class-cell-make-instance-function-keys (cell)
421 `(cddr ,cell))
422
423 (defmacro make-find-class-cell (class-name)
424 (declare (ignore class-name))
425 '(list* nil #'function-returning-nil nil))
426
427 (defun find-class-cell (symbol &optional dont-create-p)
428 (or (gethash symbol *find-class*)
429 (unless dont-create-p
430 (unless (legal-class-name-p symbol)
431 (error "~S is not a legal class name." symbol))
432 (setf (gethash symbol *find-class*) (make-find-class-cell symbol)))))
433
434 (defvar *create-classes-from-internal-structure-definitions-p* t)
435
436 (defun find-class-from-cell (symbol cell &optional (errorp t))
437 (or (find-class-cell-class cell)
438 (and *create-classes-from-internal-structure-definitions-p*
439 (structure-type-p symbol)
440 (find-structure-class symbol))
441 (cond ((null errorp) nil)
442 ((legal-class-name-p symbol)
443 (error "No class named: ~S." symbol))
444 (t
445 (error "~S is not a legal class name." symbol)))))
446
447 (defun find-class-predicate-from-cell (symbol cell &optional (errorp t))
448 (unless (find-class-cell-class cell)
449 (find-class-from-cell symbol cell errorp))
450 (find-class-cell-predicate cell))
451
452 (defun legal-class-name-p (x)
453 (and (symbolp x)
454 (not (keywordp x))))
455
456 (defun find-class (symbol &optional (errorp t) environment)
457 (declare (ignore environment))
458 (find-class-from-cell
459 symbol (find-class-cell symbol errorp) errorp))
460
461 (defun find-class-predicate (symbol &optional (errorp t) environment)
462 (declare (ignore environment))
463 (find-class-predicate-from-cell
464 symbol (find-class-cell symbol errorp) errorp))
465
466 (defvar *boot-state* nil) ; duplicate defvar to defs.lisp
467
468 ; Use this definition in any CL implementation supporting
469 ; both define-compiler-macro and load-time-value.
470 #+cmu ; Note that in CMU, lisp:find-class /= pcl:find-class
471 (define-compiler-macro find-class (&whole form
472 symbol &optional (errorp t) environment)
473 (declare (ignore environment))
474 (if (and (constantp symbol)
475 (legal-class-name-p (eval symbol))
476 (constantp errorp)
477 (member *boot-state* '(braid complete)))
478 (let ((symbol (eval symbol))
479 (errorp (not (null (eval errorp))))
480 (class-cell (make-symbol "CLASS-CELL")))
481 `(let ((,class-cell (load-time-value (find-class-cell ',symbol))))
482 (or (find-class-cell-class ,class-cell)
483 #-cmu17
484 (find-class-from-cell ',symbol ,class-cell ,errorp)
485 #+cmu17
486 ,(if errorp
487 `(find-class-from-cell ',symbol ,class-cell t)
488 `(and (kernel:class-cell-class
489 ',(kernel:find-class-cell symbol))
490 (find-class-from-cell ',symbol ,class-cell nil))))))
491 form))
492
493 #-setf
494 (defsetf find-class (symbol &optional (errorp t) environment) (new-value)
495 (declare (ignore errorp environment))
496 `(SETF\ PCL\ FIND-CLASS ,new-value ,symbol))
497
498 (defun #-setf SETF\ PCL\ FIND-CLASS #+setf (setf find-class) (new-value symbol)
499 (if (legal-class-name-p symbol)
500 (let ((cell (find-class-cell symbol)))
501 (setf (find-class-cell-class cell) new-value)
502 (when (or (eq *boot-state* 'complete)
503 (eq *boot-state* 'braid))
504 #+cmu17
505 (let ((lclass (kernel:layout-class (class-wrapper new-value))))
506 (setf (lisp:class-name lclass) (class-name new-value))
507 (unless (eq (lisp:find-class symbol nil) lclass)
508 (setf (lisp:find-class symbol) lclass)))
509
510 (setf (find-class-cell-predicate cell)
511 (symbol-function (class-predicate-name new-value)))
512 (when (and new-value (not (forward-referenced-class-p new-value)))
513
514 (dolist (keys+aok (find-class-cell-make-instance-function-keys cell))
515 (update-initialize-info-internal
516 (initialize-info new-value (car keys+aok) nil (cdr keys+aok))
517 'make-instance-function)))))
518 (error "~S is not a legal class name." symbol)))
519
520 #-setf
521 (defsetf find-class-predicate (symbol &optional (errorp t) environment) (new-value)
522 (declare (ignore errorp environment))
523 `(SETF\ PCL\ FIND-CLASS-PREDICATE ,new-value ,symbol))
524
525 (defun #-setf SETF\ PCL\ FIND-CLASS-PREDICATE #+setf (setf find-class-predicate)
526 (new-value symbol)
527 (if (legal-class-name-p symbol)
528 (setf (find-class-cell-predicate (find-class-cell symbol)) new-value)
529 (error "~S is not a legal class name." symbol)))
530
531 (defun find-wrapper (symbol)
532 (class-wrapper (find-class symbol)))
533
534 #|| ; Anything that used this should use eval instead.
535 (defun reduce-constant (old)
536 (let ((new (eval old)))
537 (if (eq new old)
538 new
539 (if (constantp new)
540 (reduce-constant new)
541 new))))
542 ||#
543
544 (defmacro gathering1 (gatherer &body body)
545 `(gathering ((.gathering1. ,gatherer))
546 (macrolet ((gather1 (x) `(gather ,x .gathering1.)))
547 ,@body)))
548
549 ;;;
550 ;;;
551 ;;;
552 (defmacro vectorizing (&key (size 0))
553 `(let* ((limit ,size)
554 (result (make-array limit))
555 (index 0))
556 (values #'(lambda (value)
557 (if (= index limit)
558 (error "vectorizing more elements than promised.")
559 (progn
560 (setf (svref result index) value)
561 (incf index)
562 value)))
563 #'(lambda () result))))
564
565 ;;;
566 ;;; These are augmented definitions of list-elements and list-tails from
567 ;;; iterate.lisp. These versions provide the extra :by keyword which can
568 ;;; be used to specify the step function through the list.
569 ;;;
570 (defmacro *list-elements (list &key (by #'cdr))
571 `(let ((tail ,list))
572 #'(lambda (finish)
573 (if (endp tail)
574 (funcall finish)
575 (prog1 (car tail)
576 (setq tail (funcall ,by tail)))))))
577
578 (defmacro *list-tails (list &key (by #'cdr))
579 `(let ((tail ,list))
580 #'(lambda (finish)
581 (prog1 (if (endp tail)
582 (funcall finish)
583 tail)
584 (setq tail (funcall ,by tail))))))
585
586 (defmacro function-funcall (form &rest args)
587 #-cmu `(funcall ,form ,@args)
588 #+cmu `(funcall (the function ,form) ,@args))
589
590 (defmacro function-apply (form &rest args)
591 #-cmu `(apply ,form ,@args)
592 #+cmu `(apply (the function ,form) ,@args))
593
594
595 ;;;
596 ;;; Convert a function name to its standard setf function name. We have to
597 ;;; do this hack because not all Common Lisps have yet converted to having
598 ;;; setf function specs.
599 ;;;
600 ;;; In a port that does have setf function specs you can use those just by
601 ;;; making the obvious simple changes to these functions. The rest of PCL
602 ;;; believes that there are function names like (SETF <foo>), this is the
603 ;;; only place that knows about this hack.
604 ;;;
605 (eval-when (compile load eval)
606 ; In 15e (and also 16c), using the built in setf mechanism costs
607 ; a hash table lookup every time a setf function is called.
608 ; Uncomment the next line to use the built in setf mechanism.
609 ;#+cmu (pushnew :setf *features*)
610 )
611
612 (eval-when (compile load eval)
613
614 #-setf
615 (defvar *setf-function-names* (make-hash-table :size 200 :test #'eq))
616
617 (defun get-setf-function-name (name)
618 #+setf `(setf ,name)
619 #-setf
620 (or (gethash name *setf-function-names*)
621 (setf (gethash name *setf-function-names*)
622 (let ((pkg (symbol-package name)))
623 (if pkg
624 (intern (format nil
625 "SETF ~A ~A"
626 (package-name pkg)
627 (symbol-name name))
628 *the-pcl-package*)
629 (make-symbol (format nil "SETF ~A" (symbol-name name))))))))
630
631 ;;;
632 ;;; Call this to define a setf macro for a function with the same behavior as
633 ;;; specified by the SETF function cleanup proposal. Specifically, this will
634 ;;; cause: (SETF (FOO a b) x) to expand to (|SETF FOO| x a b).
635 ;;;
636 ;;; do-standard-defsetf A macro interface for use at top level
637 ;;; in files. Unfortunately, users may
638 ;;; have to use this for a while.
639 ;;;
640 ;;; do-standard-defsetfs-for-defclass A special version called by defclass.
641 ;;;
642 ;;; do-standard-defsetf-1 A functional interface called by the
643 ;;; above, defmethod and defgeneric.
644 ;;; Since this is all a crock anyways,
645 ;;; users are free to call this as well.
646 ;;;
647 (defmacro do-standard-defsetf (&rest function-names)
648 `(eval-when (compile load eval)
649 (dolist (fn-name ',function-names) (do-standard-defsetf-1 fn-name))))
650
651 (defun do-standard-defsetfs-for-defclass (accessors)
652 (dolist (name accessors) (do-standard-defsetf-1 name)))
653
654 (defun do-standard-defsetf-1 (function-name)
655 #+setf
656 (declare (ignore function-name))
657 #+setf nil
658 #-setf
659 (unless (and (setfboundp function-name)
660 (get function-name 'standard-setf))
661 (setf (get function-name 'standard-setf) t)
662 (let* ((setf-function-name (get-setf-function-name function-name)))
663
664 #+Genera
665 (let ((fn #'(lambda (form)
666 (lt::help-defsetf
667 '(&rest accessor-args) '(new-value) function-name 'nil
668 `(`(,',setf-function-name ,new-value .,accessor-args))
669 form))))
670 (setf (get function-name 'lt::setf-method) fn
671 (get function-name 'lt::setf-method-internal) fn))
672
673 #+Lucid
674 (lucid::set-simple-setf-method
675 function-name
676 #'(lambda (form new-value)
677 (let* ((bindings (mapcar #'(lambda (x) `(,(gensym) ,x))
678 (cdr form)))
679 (vars (mapcar #'car bindings)))
680 ;; This may wrap spurious LET bindings around some form,
681 ;; but the PQC compiler will unwrap then.
682 `(LET (,.bindings)
683 (,setf-function-name ,new-value . ,vars)))))
684
685 #+kcl
686 (let ((helper (gensym)))
687 (setf (macro-function helper)
688 #'(lambda (form env)
689 (declare (ignore env))
690 (let* ((loc-args (butlast (cdr form)))
691 (bindings (mapcar #'(lambda (x) `(,(gensym) ,x)) loc-args))
692 (vars (mapcar #'car bindings)))
693 `(let ,bindings
694 (,setf-function-name ,(car (last form)) ,@vars)))))
695 (eval `(defsetf ,function-name ,helper)))
696 #+Xerox
697 (flet ((setf-expander (body env)
698 (declare (ignore env))
699 (let ((temps
700 (mapcar #'(lambda (x) (declare (ignore x)) (gensym))
701 (cdr body)))
702 (forms (cdr body))
703 (vars (list (gensym))))
704 (values temps
705 forms
706 vars
707 `(,setf-function-name ,@vars ,@temps)
708 `(,function-name ,@temps)))))
709 (let ((setf-method-expander (intern (concatenate 'string
710 (symbol-name function-name)
711 "-setf-expander")
712 (symbol-package function-name))))
713 (setf (get function-name :setf-method-expander) setf-method-expander
714 (symbol-function setf-method-expander) #'setf-expander)))
715
716 #-(or Genera Lucid kcl Xerox)
717 (eval `(defsetf ,function-name (&rest accessor-args) (new-value)
718 (let* ((bindings (mapcar #'(lambda (x) `(,(gensym) ,x)) accessor-args))
719 (vars (mapcar #'car bindings)))
720 `(let ,bindings
721 (,',setf-function-name ,new-value ,@vars)))))
722
723 )))
724
725 (defun setfboundp (symbol)
726 #+Genera (not (null (get-properties (symbol-plist symbol)
727 'lt::(derived-setf-function trivial-setf-method
728 setf-equivalence setf-method))))
729 #+Lucid (locally
730 (declare (special lucid::*setf-inverse-table*
731 lucid::*simple-setf-method-table*
732 lucid::*setf-method-expander-table*))
733 (or (gethash symbol lucid::*setf-inverse-table*)
734 (gethash symbol lucid::*simple-setf-method-table*)
735 (gethash symbol lucid::*setf-method-expander-table*)))
736 #+kcl (or (get symbol 'si::setf-method)
737 (get symbol 'si::setf-update-fn)
738 (get symbol 'si::setf-lambda))
739 #+Xerox (or (get symbol :setf-inverse)
740 (get symbol 'il:setf-inverse)
741 (get symbol 'il:setfn)
742 (get symbol :shared-setf-inverse)
743 (get symbol :setf-method-expander)
744 (get symbol 'il:setf-method-expander))
745 #+:coral (or (get symbol 'ccl::setf-inverse)
746 (get symbol 'ccl::setf-method-expander))
747 #+cmu (fboundp `(setf ,symbol))
748 #-(or Genera Lucid KCL Xerox :coral cmu) nil)
749
750 );eval-when
751
752
753 ;;;
754 ;;; PCL, like user code, must endure the fact that we don't have a properly
755 ;;; working setf. Many things work because they get mentioned by a defclass
756 ;;; or defmethod before they are used, but others have to be done by hand.
757 ;;;
758 (do-standard-defsetf
759 class-wrapper ;***
760 generic-function-name
761 method-function-plist
762 method-function-get
763 plist-value
764 object-plist
765 gdefinition
766 slot-value-using-class
767 )
768
769 (defsetf slot-value set-slot-value)
770
771 (defvar *redefined-functions* nil)
772
773 (defmacro original-definition (name)
774 `(get ,name ':definition-before-pcl))
775
776 (defun redefine-function (name new)
777 (pushnew name *redefined-functions*)
778 (unless (original-definition name)
779 (setf (original-definition name)
780 (symbol-function name)))
781 (setf (symbol-function name)
782 (symbol-function new)))
783

  ViewVC Help
Powered by ViewVC 1.1.5