1 ;;;; SETF and friends (except for stuff defined with COLLECT, which
4 ;;;; Note: The expansions for SETF and friends sometimes create
5 ;;;; needless LET-bindings of argument values. The compiler will
6 ;;;; remove most of these spurious bindings, so SETF doesn't worry too
7 ;;;; much about creating them.
9 ;;;; This software is part of the SBCL system. See the README file for
10 ;;;; more information.
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 ;;; The inverse for a generalized-variable reference function is stored in
23 ;;; A SETF inverse property corresponds to the short form of DEFSETF. It is
24 ;;; the name of a function takes the same args as the reference form, plus a
25 ;;; new-value arg at the end.
27 ;;; A SETF method expander is created by the long form of DEFSETF or
28 ;;; by DEFINE-SETF-EXPANDER. It is a function that is called on the reference
29 ;;; form and that produces five values: a list of temporary variables, a list
30 ;;; of value forms, a list of the single store-value form, a storing function,
31 ;;; and an accessing function.
32 (declaim (ftype (function (t &optional (or null sb!c::lexenv))) sb!xc:get-setf-expansion))
33 (defun sb!xc:get-setf-expansion (form &optional environment)
35 "Return five values needed by the SETF machinery: a list of temporary
36 variables, a list of values with which to fill them, a list of temporaries
37 for the new values, the setting function, and the accessing function."
40 (multiple-value-bind (expansion expanded)
41 (sb!xc:macroexpand-1 form environment)
43 (sb!xc:get-setf-expansion expansion environment)
44 (let ((new-var (sb!xc:gensym "NEW")))
45 (values nil nil (list new-var)
46 `(setq ,form ,new-var) form)))))
47 ;; Local functions inhibit global SETF methods.
49 (let ((name (car form)))
50 (dolist (x (sb!c::lexenv-funs environment))
51 (when (and (eq (car x) name)
52 (not (sb!c::defined-fun-p (cdr x))))
54 (expand-or-get-setf-inverse form environment))
55 ((setq temp (info :setf :inverse (car form)))
56 (get-setf-method-inverse form `(,temp) nil environment))
57 ((setq temp (info :setf :expander (car form)))
58 ;; KLUDGE: It may seem as though this should go through
59 ;; *MACROEXPAND-HOOK*, but the ANSI spec seems fairly explicit
60 ;; that *MACROEXPAND-HOOK* is a hook for MACROEXPAND-1, not
61 ;; for macroexpansion in general. -- WHN 19991128
64 ;; As near as I can tell from the ANSI spec,
65 ;; macroexpanders have a right to expect an actual
66 ;; lexical environment, not just a NIL which is to
67 ;; be interpreted as a null lexical environment.
69 (coerce-to-lexenv environment)))
71 (expand-or-get-setf-inverse form environment)))))
73 ;;; GET-SETF-METHOD existed in pre-ANSI Common Lisp, and various code inherited
74 ;;; from CMU CL uses it repeatedly, so rather than rewrite a lot of code to not
75 ;;; use it, we just define it in terms of ANSI's GET-SETF-EXPANSION (or
76 ;;; actually, the cross-compiler version of that, i.e.
77 ;;; SB!XC:GET-SETF-EXPANSION).
78 (declaim (ftype (function (t &optional (or null sb!c::lexenv))) get-setf-method))
79 (defun get-setf-method (form &optional environment)
81 "This is a specialized-for-one-value version of GET-SETF-EXPANSION (and
82 a relic from pre-ANSI Common Lisp). Portable ANSI code should use
83 GET-SETF-EXPANSION directly."
84 (multiple-value-bind (temps value-forms store-vars store-form access-form)
85 (sb!xc:get-setf-expansion form environment)
86 (when (cdr store-vars)
87 (error "GET-SETF-METHOD used for a form with multiple store ~
90 (values temps value-forms store-vars store-form access-form)))
92 ;;; If a macro, expand one level and try again. If not, go for the
94 (declaim (ftype (function (t (or null sb!c::lexenv)))
95 expand-or-get-setf-inverse))
96 (defun expand-or-get-setf-inverse (form environment)
97 (multiple-value-bind (expansion expanded)
98 (sb!xc:macroexpand-1 form environment)
100 (sb!xc:get-setf-expansion expansion environment)
101 (get-setf-method-inverse form
102 `(funcall #'(setf ,(car form)))
106 (defun get-setf-method-inverse (form inverse setf-fun environment)
107 (let ((new-var (sb!xc:gensym "NEW"))
111 (dolist (x (reverse (cdr form)))
112 (cond ((sb!xc:constantp x environment)
115 (let ((temp (gensym "TMP")))
123 `(,@inverse ,new-var ,@args)
124 `(,@inverse ,@args ,new-var))
125 `(,(car form) ,@args))))
129 ;;; Except for atoms, we always call GET-SETF-EXPANSION, since it has
130 ;;; some non-trivial semantics. But when there is a setf inverse, and
131 ;;; G-S-E uses it, then we return a call to the inverse, rather than
132 ;;; returning a hairy LET form. This is probably important mainly as a
133 ;;; convenience in allowing the use of SETF inverses without the full
135 (defmacro-mundanely setf (&rest args &environment env)
137 "Takes pairs of arguments like SETQ. The first is a place and the second
138 is the value that is supposed to go into that place. Returns the last
139 value. The place argument may be any of the access forms for which SETF
140 knows a corresponding setting form."
141 (let ((nargs (length args)))
144 (let ((place (first args))
145 (value-form (second args)))
147 `(setq ,place ,value-form)
148 (multiple-value-bind (dummies vals newval setter getter)
149 (sb!xc:get-setf-expansion place env)
150 (declare (ignore getter))
151 (let ((inverse (info :setf :inverse (car place))))
152 (if (and inverse (eq inverse (car setter)))
153 `(,inverse ,@(cdr place) ,value-form)
154 `(let* (,@(mapcar #'list dummies vals))
155 (multiple-value-bind ,newval ,value-form
158 (error "odd number of args to SETF"))
160 (do ((a args (cddr a))
161 (reversed-setfs nil))
163 `(progn ,@(nreverse reversed-setfs)))
164 (push (list 'setf (car a) (cadr a)) reversed-setfs))))))
166 ;;;; various SETF-related macros
168 (defmacro-mundanely shiftf (&whole form &rest args &environment env)
170 "One or more SETF-style place expressions, followed by a single
171 value expression. Evaluates all of the expressions in turn, then
172 assigns the value of each expression to the place on its left,
173 returning the value of the leftmost."
174 (when (< (length args) 2)
175 (error "~S called with too few arguments: ~S" 'shiftf form))
176 (let (let*-bindings mv-bindings setters getters)
177 (dolist (arg (butlast args))
178 (multiple-value-bind (temps subforms store-vars setter getter)
179 (sb!xc:get-setf-expansion arg env)
180 (mapc (lambda (tmp form)
181 (push `(,tmp ,form) let*-bindings))
184 (push store-vars mv-bindings)
185 (push setter setters)
186 (push getter getters)))
187 ;; Handle the last arg specially here. The getter is just the last
189 (push (car (last args)) getters)
191 ;; Reverse the collected lists so last bit looks nicer.
192 (setf let*-bindings (nreverse let*-bindings)
193 mv-bindings (nreverse mv-bindings)
194 setters (nreverse setters)
195 getters (nreverse getters))
197 (labels ((thunk (mv-bindings getters)
199 `((multiple-value-bind
202 ,@(thunk (cdr mv-bindings) (cdr getters))))
205 (multiple-value-bind ,(car mv-bindings)
207 ,@(thunk mv-bindings (cdr getters))
208 (values ,@(car mv-bindings)))))))
210 (defmacro-mundanely push (obj place &environment env)
212 "Takes an object and a location holding a list. Conses the object onto
213 the list, returning the modified list. OBJ is evaluated before PLACE."
214 (multiple-value-bind (dummies vals newval setter getter)
215 (get-setf-method place env)
218 ,@(mapcar #'list dummies vals)
219 (,(car newval) (cons ,g ,getter)))
222 (defmacro-mundanely pushnew (obj place &rest keys
223 &key key test test-not &environment env)
225 "Takes an object and a location holding a list. If the object is
226 already in the list, does nothing; otherwise, conses the object onto
227 the list. Returns the modified list. If there is a :TEST keyword, this
228 is used for the comparison."
229 (declare (ignore key test test-not))
230 (multiple-value-bind (dummies vals newval setter getter)
231 (get-setf-method place env)
234 ,@(mapcar #'list dummies vals)
235 (,(car newval) (adjoin ,g ,getter ,@keys)))
238 (defmacro-mundanely pop (place &environment env)
240 "The argument is a location holding a list. Pops one item off the front
241 of the list and returns it."
242 (multiple-value-bind (dummies vals newval setter getter)
243 (get-setf-method place env)
244 (do* ((d dummies (cdr d))
248 (push (list (car newval) getter) let-list)
249 `(let* ,(nreverse let-list)
250 (prog1 (car ,(car newval))
251 (setq ,(car newval) (cdr ,(car newval)))
253 (push (list (car d) (car v)) let-list))))
255 (defmacro-mundanely remf (place indicator &environment env)
257 "Place may be any place expression acceptable to SETF, and is expected
258 to hold a property list or (). This list is destructively altered to
259 remove the property specified by the indicator. Returns T if such a
260 property was present, NIL if not."
261 (multiple-value-bind (dummies vals newval setter getter)
262 (get-setf-method place env)
263 (do* ((d dummies (cdr d))
270 ;; See ANSI 5.1.3 for why we do out-of-order evaluation
271 (push (list ind-temp indicator) let-list)
272 (push (list (car newval) getter) let-list)
273 `(let* ,(nreverse let-list)
274 (do ((,local1 ,(car newval) (cddr ,local1))
275 (,local2 nil ,local1))
277 (cond ((atom (cdr ,local1))
278 (error "Odd-length property list in REMF."))
279 ((eq (car ,local1) ,ind-temp)
281 (rplacd (cdr ,local2) (cddr ,local1))
283 (t (setq ,(car newval) (cddr ,(car newval)))
286 (push (list (car d) (car v)) let-list))))
288 ;;; we can't use DEFINE-MODIFY-MACRO because of ANSI 5.1.3
289 (defmacro-mundanely incf (place &optional (delta 1) &environment env)
291 "The first argument is some location holding a number. This number is
292 incremented by the second argument, DELTA, which defaults to 1."
293 (multiple-value-bind (dummies vals newval setter getter)
294 (get-setf-method place env)
296 `(let* (,@(mapcar #'list dummies vals)
298 (,(car newval) (+ ,getter ,d)))
301 (defmacro-mundanely decf (place &optional (delta 1) &environment env)
303 "The first argument is some location holding a number. This number is
304 decremented by the second argument, DELTA, which defaults to 1."
305 (multiple-value-bind (dummies vals newval setter getter)
306 (get-setf-method place env)
308 `(let* (,@(mapcar #'list dummies vals)
310 (,(car newval) (- ,getter ,d)))
313 ;;;; DEFINE-MODIFY-MACRO stuff
315 (def!macro sb!xc:define-modify-macro (name lambda-list function &optional doc-string)
317 "Creates a new read-modify-write macro like PUSH or INCF."
318 (let ((other-args nil)
320 (env (make-symbol "ENV")) ; To beautify resulting arglist.
321 (reference (make-symbol "PLACE"))) ; Note that these will be nonexistent
322 ; in the final expansion anyway.
323 ;; Parse out the variable names and &REST arg from the lambda list.
324 (do ((ll lambda-list (cdr ll))
328 (cond ((eq arg '&optional))
330 (if (symbolp (cadr ll))
331 (setq rest-arg (cadr ll))
332 (error "Non-symbol &REST argument in definition of ~S." name))
335 (error "Illegal stuff after &REST argument.")))
336 ((memq arg '(&key &allow-other-keys &aux))
337 (error "~S not allowed in DEFINE-MODIFY-MACRO lambda list." arg))
339 (push arg other-args))
340 ((and (listp arg) (symbolp (car arg)))
341 (push (car arg) other-args))
342 (t (error "Illegal stuff in lambda list."))))
343 (setq other-args (nreverse other-args))
344 `(#-sb-xc-host sb!xc:defmacro
345 #+sb-xc-host defmacro-mundanely
346 ,name (,reference ,@lambda-list &environment ,env)
348 (multiple-value-bind (dummies vals newval setter getter)
349 (get-setf-method ,reference ,env)
350 (do ((d dummies (cdr d))
352 (let-list nil (cons (list (car d) (car v)) let-list)))
354 (push (list (car newval)
356 `(list* ',function getter ,@other-args ,rest-arg)
357 `(list ',function getter ,@other-args)))
359 `(let* ,(nreverse let-list)
364 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
365 ;;; Assign SETF macro information for NAME, making all appropriate checks.
366 (defun assign-setf-macro (name expander inverse doc)
367 (with-single-package-locked-error
368 (:symbol name "defining a setf-expander for ~A"))
369 (cond ((gethash name sb!c:*setf-assumed-fboundp*)
371 "defining setf macro for ~S when ~S was previously ~
372 treated as a function"
375 ((not (fboundp `(setf ,name)))
376 ;; All is well, we don't need any warnings.
378 ((not (eq (symbol-package name) (symbol-package 'aref)))
379 (style-warn "defining setf macro for ~S when ~S is fbound"
380 name `(setf ,name))))
381 (remhash name sb!c:*setf-assumed-fboundp*)
382 ;; FIXME: It's probably possible to join these checks into one form which
383 ;; is appropriate both on the cross-compilation host and on the target.
384 (when (or inverse (info :setf :inverse name))
385 (setf (info :setf :inverse name) inverse))
386 (when (or expander (info :setf :expander name))
387 (setf (info :setf :expander name) expander))
389 (setf (fdocumentation name 'setf) doc))
392 (def!macro sb!xc:defsetf (access-fn &rest rest)
394 "Associates a SETF update function or macro with the specified access
395 function or macro. The format is complex. See the manual for details."
396 (cond ((not (listp (car rest)))
397 `(eval-when (:load-toplevel :compile-toplevel :execute)
398 (assign-setf-macro ',access-fn
401 ,(when (and (car rest) (stringp (cadr rest)))
403 ((and (cdr rest) (listp (cadr rest)))
405 (lambda-list (&rest store-variables) &body body)
407 (with-unique-names (whole access-form environment)
408 (multiple-value-bind (body local-decs doc)
409 (parse-defmacro `(,lambda-list ,@store-variables)
410 whole body access-fn 'defsetf
411 :environment environment
413 `(eval-when (:compile-toplevel :load-toplevel :execute)
416 (lambda (,access-form ,environment)
418 (%defsetf ,access-form ,(length store-variables)
424 (error "ill-formed DEFSETF for ~S" access-fn))))
426 (defun %defsetf (orig-access-form num-store-vars expander)
427 (declare (type function expander))
432 (dolist (subform (cdr orig-access-form))
433 (if (constantp subform)
434 (push subform subforms)
435 (let ((var (gensym)))
437 (push var subform-vars)
438 (push subform subform-exprs))))
439 (dotimes (i num-store-vars)
440 (push (gensym) store-vars))
441 (let ((r-subforms (nreverse subforms))
442 (r-subform-vars (nreverse subform-vars))
443 (r-subform-exprs (nreverse subform-exprs))
444 (r-store-vars (nreverse store-vars)))
445 (values r-subform-vars
448 (funcall expander (cons r-subforms r-store-vars))
449 `(,(car orig-access-form) ,@r-subforms)))))
451 ;;;; DEFMACRO DEFINE-SETF-EXPANDER and various DEFINE-SETF-EXPANDERs
453 ;;; DEFINE-SETF-EXPANDER is a lot like DEFMACRO.
454 (def!macro sb!xc:define-setf-expander (access-fn lambda-list &body body)
456 "Syntax like DEFMACRO, but creates a setf expander function. The body
457 of the definition must be a form that returns five appropriate values."
458 (unless (symbolp access-fn)
459 (error "~S access-function name ~S is not a symbol."
460 'sb!xc:define-setf-expander access-fn))
461 (with-unique-names (whole environment)
462 (multiple-value-bind (body local-decs doc)
463 (parse-defmacro lambda-list whole body access-fn
464 'sb!xc:define-setf-expander
465 :environment environment)
466 `(eval-when (:compile-toplevel :load-toplevel :execute)
467 (assign-setf-macro ',access-fn
468 (lambda (,whole ,environment)
474 (sb!xc:define-setf-expander getf (place prop
477 (declare (type sb!c::lexenv env))
478 (multiple-value-bind (temps values stores set get)
479 (get-setf-method place env)
480 (let ((newval (gensym))
482 (def-temp (if default (gensym))))
483 (values `(,@temps ,ptemp ,@(if default `(,def-temp)))
484 `(,@values ,prop ,@(if default `(,default)))
486 `(let ((,(car stores) (%putf ,get ,ptemp ,newval)))
489 `(getf ,get ,ptemp ,@(if default `(,def-temp)))))))
491 (sb!xc:define-setf-expander get (symbol prop &optional default)
492 (let ((symbol-temp (gensym))
496 (values `(,symbol-temp ,prop-temp ,@(if default `(,def-temp)))
497 `(,symbol ,prop ,@(if default `(,default)))
499 `(%put ,symbol-temp ,prop-temp ,newval)
500 `(get ,symbol-temp ,prop-temp ,@(if default `(,def-temp))))))
502 (sb!xc:define-setf-expander gethash (key hashtable &optional default)
503 (let ((key-temp (gensym))
504 (hashtable-temp (gensym))
505 (default-temp (gensym))
506 (new-value-temp (gensym)))
508 `(,key-temp ,hashtable-temp ,@(if default `(,default-temp)))
509 `(,key ,hashtable ,@(if default `(,default)))
511 `(%puthash ,key-temp ,hashtable-temp ,new-value-temp)
512 `(gethash ,key-temp ,hashtable-temp ,@(if default `(,default-temp))))))
514 (sb!xc:define-setf-expander logbitp (index int &environment env)
515 (declare (type sb!c::lexenv env))
516 (multiple-value-bind (temps vals stores store-form access-form)
517 (get-setf-method int env)
520 (stemp (first stores)))
521 (values `(,ind ,@temps)
526 (dpb (if ,store 1 0) (byte 1 ,ind) ,access-form)))
529 `(logbitp ,ind ,access-form)))))
531 ;;; CMU CL had a comment here that:
532 ;;; Evil hack invented by the gnomes of Vassar Street (though not as evil as
533 ;;; it used to be.) The function arg must be constant, and is converted to
534 ;;; an APPLY of the SETF function, which ought to exist.
536 ;;; It may not be clear (wasn't to me..) that this is a standard thing, but See
537 ;;; "5.1.2.5 APPLY Forms as Places" in the ANSI spec. I haven't actually
538 ;;; verified that this code has any correspondence to that code, but at least
539 ;;; ANSI has some place for SETF APPLY. -- WHN 19990604
540 (sb!xc:define-setf-expander apply (functionoid &rest args)
541 (unless (and (listp functionoid)
542 (= (length functionoid) 2)
543 (eq (first functionoid) 'function)
544 (symbolp (second functionoid)))
545 (error "SETF of APPLY is only defined for function args like #'SYMBOL."))
546 (let ((function (second functionoid))
548 (vars (make-gensym-list (length args))))
549 (values vars args (list new-var)
550 `(apply #'(setf ,function) ,new-var ,@vars)
551 `(apply #',function ,@vars))))
553 ;;; Special-case a BYTE bytespec so that the compiler can recognize it.
554 (sb!xc:define-setf-expander ldb (bytespec place &environment env)
556 "The first argument is a byte specifier. The second is any place form
557 acceptable to SETF. Replace the specified byte of the number in this
558 place with bits from the low-order end of the new value."
559 (declare (type sb!c::lexenv env))
560 (multiple-value-bind (dummies vals newval setter getter)
561 (get-setf-method place env)
562 (if (and (consp bytespec) (eq (car bytespec) 'byte))
563 (let ((n-size (gensym))
566 (values (list* n-size n-pos dummies)
567 (list* (second bytespec) (third bytespec) vals)
569 `(let ((,(car newval) (dpb ,n-new (byte ,n-size ,n-pos)
573 `(ldb (byte ,n-size ,n-pos) ,getter)))
574 (let ((btemp (gensym))
576 (values (cons btemp dummies)
579 `(let ((,(car newval) (dpb ,gnuval ,btemp ,getter)))
582 `(ldb ,btemp ,getter))))))
584 (sb!xc:define-setf-expander mask-field (bytespec place &environment env)
586 "The first argument is a byte specifier. The second is any place form
587 acceptable to SETF. Replaces the specified byte of the number in this place
588 with bits from the corresponding position in the new value."
589 (declare (type sb!c::lexenv env))
590 (multiple-value-bind (dummies vals newval setter getter)
591 (get-setf-method place env)
592 (let ((btemp (gensym))
594 (values (cons btemp dummies)
597 `(let ((,(car newval) (deposit-field ,gnuval ,btemp ,getter)))
600 `(mask-field ,btemp ,getter)))))
602 (sb!xc:define-setf-expander the (type place &environment env)
603 (declare (type sb!c::lexenv env))
604 (multiple-value-bind (temps subforms store-vars setter getter)
605 (sb!xc:get-setf-expansion place env)
606 (values temps subforms store-vars
607 `(multiple-value-bind ,store-vars
608 (the ,type (values ,@store-vars))
610 `(the ,type ,getter))))