Propagate (nth-value 1 truncate)+typep.
[sbcl.git] / src / code / setf.lisp
blobd9e474a7bf7102f3efb699ff73a73c5a4f6d2880
1 ;;;; SETF and friends
2 ;;;;
3 ;;;; Note: The expansions for SETF and friends sometimes create
4 ;;;; needless LET-bindings of argument values. The compiler will
5 ;;;; remove most of these spurious bindings, so SETF doesn't worry too
6 ;;;; much about creating them.
8 ;;;; This software is part of the SBCL system. See the README file for
9 ;;;; more information.
10 ;;;;
11 ;;;; This software is derived from the CMU CL system, which was
12 ;;;; written at Carnegie Mellon University and released into the
13 ;;;; public domain. The software is in the public domain and is
14 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
15 ;;;; files for more information.
17 (in-package "SB-IMPL")
19 ;;; Return T if FUN names a DEFSTRUCT slot accessor that we should
20 ;;; transform from SETF into %INSTANCE-SET directly - bypassing
21 ;;; #'(SETF MYSLOT) - which requires that the slot be read/writable.
22 ;;; A local function named (SETF MYSLOT) inhibits the transform,
23 ;;; though technically need not, as it is unspecified how SETF
24 ;;; of a structure slot expands. It is likewise unportable to
25 ;;; expect that a NOTINLINE does anything, but we'll check anyway.
26 (defun transformable-struct-setf-p (form env)
27 (when (singleton-p (cdr form))
28 (let* ((fun (car form))
29 (slot-info (structure-instance-accessor-p fun)))
30 (when (and slot-info (not (dsd-read-only (cdr slot-info))))
31 (dx-let ((setter `(setf ,fun)))
32 (when (and (not (sb-c::fun-locally-defined-p setter env))
33 (not (sb-c::fun-lexically-notinline-p setter env)))
34 slot-info)))))) ; caller needs the (DD . DSD) pair
36 ;;; The inverse for a generalized-variable reference function is stored in
37 ;;; one of two ways:
38 ;;;
39 ;;; A SETF inverse property corresponds to the short form of DEFSETF. It is
40 ;;; the name of a function takes the same args as the reference form, plus a
41 ;;; new-value arg at the end.
42 ;;;
43 ;;; A SETF method expander is created by the long form of DEFSETF or
44 ;;; by DEFINE-SETF-EXPANDER. It is a function that is called on the reference
45 ;;; form and that produces five values: a list of temporary variables, a list
46 ;;; of value forms, a list of the single store-value form, a storing function,
47 ;;; and an accessing function.
48 (declaim (ftype (function (t &optional lexenv-designator))
49 get-setf-expansion))
50 (defun get-setf-expansion (form &optional environment)
51 "Return five values needed by the SETF machinery: a list of temporary
52 variables, a list of values with which to fill them, a list of temporaries
53 for the new values, the setting function, and the accessing function."
54 (named-let retry ((form form))
55 (labels ((newvals (count)
56 (make-gensym-list count "NEW"))
57 ;; Produce the expansion of a SETF form that calls either
58 ;; #'(SETF name) or an inverse given by short form DEFSETF.
59 (call (call arg-maker &aux (vals (newvals 1)))
60 (multiple-value-bind (temp-vars temp-vals args)
61 (collect-setf-temps (cdr form) environment nil)
62 (values temp-vars temp-vals vals
63 `(,.call ,@(funcall arg-maker (car vals) args))
64 `(,(car form) ,@args)))))
65 (declare (ftype (sfunction (t) list) newvals))
66 (if (atom form)
67 (multiple-value-bind (expansion expanded)
68 ;; Previously this called %MACROEXPAND, but the two operations
69 ;; are equivalent on atoms, so do the one that is "less".
70 (macroexpand-1 form environment)
71 (if expanded
72 (retry expansion)
73 (let ((vals (newvals 1)))
74 (values nil nil vals `(setq ,form ,(car vals)) form))))
75 (let ((fname (car form)))
76 ;; Local functions inhibit global SETF methods.
77 (unless (sb-c::fun-locally-defined-p fname environment)
78 ;; There are 3 possibilities for the expander:
79 ;; #<fun> - define-setf-expander
80 ;; (symbol doc . source-loc) - defsetf short form
81 ;; (integer . #<fun>) - defsetf long form
82 (awhen (info :setf :expander fname)
83 (return-from retry
84 (typecase it
85 (function ; DEFINE-SETF-EXPANDER
86 (funcall it form environment))
87 ((cons symbol) ; short form DEFSETF
88 (call `(,(car it)) (lambda (new args) `(,@args ,new))))
89 (t ; long form DEFSETF
90 (binding* ((newvals (newvals (car it)))
91 (expander (the function (cdr it)))
92 ((tempvars tempvals call-args)
93 (collect-setf-temps
94 (cdr form) environment
95 ;; NAME-HINTS affect aesthetics only
96 (or #+sb-xc (%fun-lambda-list expander)))))
97 (values tempvars tempvals newvals
98 (apply expander call-args environment newvals)
99 `(,fname ,@call-args)))))))
100 (awhen (transformable-struct-setf-p form environment)
101 (let ((instance (make-symbol "OBJ"))
102 (vals (newvals 1)))
103 (return-from retry
104 (values (list instance) (list (cadr form)) vals
105 (slot-access-transform :setf (list instance (car vals)) it)
106 (slot-access-transform :read (list instance) it))))))
107 (multiple-value-bind (expansion expanded)
108 (%macroexpand-1 form environment)
109 (if expanded
110 (retry expansion) ; if a macro, we start over
111 (call `(funcall #'(setf ,fname)) #'cons))))))))
113 ;; Expand PLACE until it is a form that SETF might know something about.
114 ;; Macros are expanded only when no SETF expander (or inverse) exists.
115 ;; Symbol-macros are always expanded because there are no SETF expanders
116 ;; for them. This is useful mainly when a symbol-macro or ordinary macro
117 ;; expands to a "mundane" lexical or special variable.
118 (defun macroexpand-for-setf (place environment)
119 (loop
120 (when (and (listp place) (info :setf :expander (car place)))
121 (return place))
122 (multiple-value-bind (expansion macro-p) (%macroexpand-1 place environment)
123 (if macro-p
124 (setq place expansion) ; iterate
125 (return place)))))
127 ;;;; SETF itself
129 ;; Code shared by SETF, PSETF, SHIFTF attempting to minimize the expansion.
130 ;; This has significant speed+space benefit to a non-preprocessing interpreter,
131 ;; and to some degree a preprocessing interpreter.
132 (labels ((gen-let* (bindings body-forms)
133 (cond ((not bindings) body-forms)
135 (when (and (singleton-p body-forms)
136 (listp (car body-forms))
137 (eq (caar body-forms) 'let*))
138 (let ((nested (cdar body-forms))) ; extract the nested LET*
139 (setq bindings (append bindings (car nested))
140 body-forms (cdr nested))))
141 `((let* ,bindings ,@body-forms)))))
142 (gen-mv-bind (stores values body-forms)
143 (if (singleton-p stores)
144 (gen-let* `((,(car stores) ,values)) body-forms)
145 `((multiple-value-bind ,stores ,values ,@body-forms))))
146 (forms-list (form)
147 (if (and (consp form) (eq (car form) 'progn))
148 (cdr form)
149 (list form)))
150 ;; Instead of emitting (PROGN (VALUES (SETQ ...) (SETQ ...)) NIL)
151 ;; the SETQs can be lifted into the PROGN. This is unimportant
152 ;; for compiled code, but it helps the interpreter not needlessly
153 ;; collect arguments to call VALUES; and it's more human-readable.
154 (de-values-ify (forms)
155 (mapcan (lambda (form)
156 (if (and (listp form) (eq (car form) 'values))
157 (copy-list (cdr form))
158 (list form)))
159 forms)))
161 (sb-xc:defmacro setf (&whole form &rest args &environment env)
162 "Takes pairs of arguments like SETQ. The first is a place and the second
163 is the value that is supposed to go into that place. Returns the last
164 value. The place argument may be any of the access forms for which SETF
165 knows a corresponding setting form."
166 (unless args
167 (return-from setf nil))
168 (destructuring-bind (place value-form . more) args
169 (when more
170 (return-from setf `(progn ,@(sb-c::explode-setq form 'error))))
171 (when (atom (setq place (macroexpand-for-setf place env)))
172 (return-from setf `(setq ,place ,value-form)))
174 (let ((fun (car place)))
175 (when (and (symbolp fun)
176 ;; Local definition of FUN precludes global knowledge.
177 (not (sb-c::fun-locally-defined-p fun env)))
178 (let ((inverse (info :setf :expander fun)))
179 ;; NIL is not a valid setf inverse name, for two reasons:
180 ;; 1. you can't define a function named NIL,
181 ;; 2. (DEFSETF THING () ...) is the long form DEFSETF syntax.
182 (when (typep inverse '(cons symbol))
183 (return-from setf `(,(car inverse) ,@(cdr place) ,value-form))))
184 (awhen (transformable-struct-setf-p place env)
185 (return-from setf
186 (slot-access-transform :setf (list (cadr place) value-form) it)))))
188 (multiple-value-bind (temps vals newval setter)
189 (get-setf-expansion place env)
190 (car (gen-let* (mapcar #'list temps vals)
191 (gen-mv-bind newval value-form (forms-list setter)))))))
193 ;; various SETF-related macros
195 (sb-xc:defmacro shiftf (&whole form &rest args &environment env)
196 "One or more SETF-style place expressions, followed by a single
197 value expression. Evaluates all of the expressions in turn, then
198 assigns the value of each expression to the place on its left,
199 returning the value of the leftmost."
200 (when (< (length args) 2)
201 (error "~S called with too few arguments: ~S" 'shiftf form))
202 (collect ((let*-bindings) (mv-bindings) (setters) (getters))
203 (dolist (arg (butlast args))
204 (multiple-value-bind (temps subforms store-vars setter getter)
205 (get-setf-expansion arg env)
206 (let*-bindings (mapcar #'list temps subforms))
207 (mv-bindings store-vars)
208 (setters setter)
209 (getters getter)))
210 ;; Handle the last arg specially here. The getter is just the last
211 ;; arg itself.
212 (getters (car (last args)))
213 (labels ((thunk (mv-bindings getters setters)
214 (if mv-bindings
215 (gen-mv-bind (car mv-bindings) (car getters)
216 (thunk (cdr mv-bindings) (cdr getters) setters))
217 setters)))
218 (let ((outputs (loop for i below (length (car (mv-bindings)))
219 collect (gensym "OUT"))))
220 (car (gen-let* (reduce #'nconc (let*-bindings))
221 (gen-mv-bind outputs (car (getters))
222 (thunk (mv-bindings) (cdr (getters))
223 `(,@(de-values-ify (setters))
224 (values ,@outputs))))))))))
226 (labels
227 ((expand (args env operator single-op)
228 (cond ((singleton-p (cdr args)) ; commonest case probably
229 (return-from expand `(progn (,single-op ,@args) nil)))
230 ((not args)
231 (return-from expand nil)))
232 (collect ((let*-bindings) (mv-bindings) (setters))
233 (do ((a args (cddr a)))
234 ((endp a))
235 (when (endp (cdr a))
236 (error "Odd number of args to ~S." operator))
237 (let ((place (car a))
238 (value-form (cadr a)))
239 (when (and (not (symbolp place)) (eq operator 'psetq))
240 (%program-error "Place ~S in PSETQ is not a SYMBOL" place))
241 (multiple-value-bind (temps vals stores setter)
242 (get-setf-expansion place env)
243 (let*-bindings (mapcar #'list temps vals))
244 (mv-bindings (cons stores value-form))
245 (setters setter))))
246 (car (build (let*-bindings) (mv-bindings)
247 (de-values-ify (setters))))))
248 (build (let*-bindings mv-bindings setters)
249 (if let*-bindings
250 (gen-let* (car let*-bindings)
251 (gen-mv-bind (caar mv-bindings) (cdar mv-bindings)
252 (build (cdr let*-bindings) (cdr mv-bindings)
253 setters)))
254 `(,@setters nil))))
256 (sb-xc:defmacro psetf (&rest pairs &environment env)
257 "This is to SETF as PSETQ is to SETQ. Args are alternating place
258 expressions and values to go into those places. All of the subforms and
259 values are determined, left to right, and only then are the locations
260 updated. Returns NIL."
261 (expand pairs env 'psetf 'setf))
263 (sb-xc:defmacro psetq (&rest pairs &environment env)
264 "PSETQ {var value}*
265 Set the variables to the values, like SETQ, except that assignments
266 happen in parallel, i.e. no assignments take place until all the
267 forms have been evaluated."
268 (expand pairs env 'psetq 'setq))))
270 (sb-xc:defmacro rotatef (&rest args &environment env)
271 "Takes any number of SETF-style place expressions. Evaluates all of the
272 expressions in turn, then assigns to each place the value of the form to
273 its right. The rightmost form gets the value of the leftmost.
274 Returns NIL."
275 (when args
276 (collect ((let*-bindings) (mv-bindings) (setters) (getters))
277 (dolist (arg args)
278 (multiple-value-bind (temps subforms store-vars setter getter)
279 (get-setf-expansion arg env)
280 (let*-bindings (mapcar #'list temps subforms))
281 (mv-bindings store-vars)
282 (setters setter)
283 (getters getter)))
284 (setters nil)
285 (getters (car (getters)))
286 (labels ((thunk (mv-bindings getters)
287 (if mv-bindings
288 `((multiple-value-bind ,(car mv-bindings) ,(car getters)
289 ,@(thunk (cdr mv-bindings) (cdr getters))))
290 (setters))))
291 `(let* ,(reduce #'append (let*-bindings))
292 ,@(thunk (mv-bindings) (cdr (getters))))))))
294 (sb-xc:defmacro push (obj place &environment env)
295 "Takes an object and a location holding a list. Conses the object onto
296 the list, returning the modified list. OBJ is evaluated before PLACE."
297 ;; If PLACE has multiple store locations, what should we do?
298 ;; In other Lisp implementations:
299 ;; - One errs, says "Multiple store variables not expected"
300 ;; - One pushes multiple values produced by OBJ form into multiple places.
301 ;; - At least two produce an incorrect expansion that doesn't even work.
302 (expand-rmw-macro 'cons (list obj) place '() nil env '(item)))
304 (sb-xc:defmacro pushnew (obj place &rest keys &environment env)
305 "Takes an object and a location holding a list. If the object is
306 already in the list, does nothing; otherwise, conses the object onto
307 the list. Keyword arguments are accepted as per the ADJOIN function."
308 ;; Can't specify the actual keywords above since, apparently,
309 ;; non-constant keywords should be accepted.
310 (declare (sb-c::lambda-list (obj place &key key test test-not)))
311 ;; Passing AFTER-ARGS-BINDP = NIL causes the forms subsequent to PLACE
312 ;; to be inserted literally as-is, giving the (apparently) desired behavior
313 ;; of *not* evaluating them before the Read/Modify/Write of PLACE, which
314 ;; seems to be an exception to the 5.1.3 exception on L-to-R evaluation.
315 ;; The spec only mentions that ITEM is eval'd before PLACE.
316 (expand-rmw-macro 'adjoin (list obj) place keys nil env '(item)))
318 (sb-xc:defmacro pop (place &environment env)
319 "The argument is a location holding a list. Pops one item off the front
320 of the list and returns it."
321 (multiple-value-bind (temps vals stores setter getter)
322 (get-setf-expansion place env)
323 (let ((list (copy-symbol 'list))
324 (ret (copy-symbol 'car)))
325 `(let* (,@(mapcar #'list temps vals)
326 (,list ,getter)
327 (,ret (car ,list))
328 (,(car stores) (cdr ,list))
329 ,@(cdr stores))
330 ,setter
331 ,ret))))
333 (sb-xc:defmacro remf (place indicator &environment env)
334 "Place may be any place expression acceptable to SETF, and is expected
335 to hold a property list or (). This list is destructively altered to
336 remove the property specified by the indicator. Returns T if such a
337 property was present, NIL if not."
338 (multiple-value-bind (temps vals newval setter getter)
339 (get-setf-expansion place env)
340 (let* ((flag (make-symbol "FLAG"))
341 (body `(multiple-value-bind (,(car newval) ,flag)
342 ;; See ANSI 5.1.3 for why we do out-of-order evaluation
343 (truly-the (values list boolean)
344 (%remf ,indicator ,getter))
345 ,(if (cdr newval) `(let ,(cdr newval) ,setter) setter)
346 ,flag)))
347 (if temps `(let* ,(mapcar #'list temps vals) ,body) body))))
349 ;; Perform the work of REMF.
350 (defun %remf (indicator plist)
351 (let ((tail plist) (predecessor))
352 (loop
353 (when (endp tail) (return (values plist nil)))
354 (let ((key (pop tail)))
355 (when (atom tail)
356 (error (if tail
357 "Improper list in REMF."
358 "Odd-length list in REMF.")))
359 (let ((next (cdr tail)))
360 (when (eq key indicator)
361 ;; This function is strict in its return type!
362 (the list next) ; for effect
363 (return (values (cond (predecessor
364 (setf (cdr predecessor) next)
365 plist)
367 next))
368 t)))
369 (setq predecessor tail tail next))))))
371 ;;; INCF and DECF have a straightforward expansion, avoiding temp vars,
372 ;;; when the PLACE is a non-macro symbol. Otherwise we do the generalized
373 ;;; SETF-like thing. The compiler doesn't care either way, but this
374 ;;; reduces the incentive to treat some macros as special-forms when
375 ;;; squeezing more performance from a Lisp interpreter.
376 ;;; DEFINE-MODIFY-MACRO could be used, but this expands more compactly.
377 (flet ((expand (place delta env operator)
378 (if (symbolp (setq place (macroexpand-for-setf place env)))
379 `(setq ,place (,operator ,delta ,place))
380 (multiple-value-bind (dummies vals newval setter getter)
381 (get-setf-expansion place env)
382 `(let* (,@(mapcar #'list dummies vals)
383 (,(car newval) (,operator ,delta ,getter))
384 ,@(cdr newval))
385 ,setter)))))
386 (sb-xc:defmacro incf (place &optional (delta 1) &environment env)
387 "The first argument is some location holding a number. This number is
388 incremented by the second argument, DELTA, which defaults to 1."
389 (expand place delta env '+))
391 (sb-xc:defmacro decf (place &optional (delta 1) &environment env)
392 "The first argument is some location holding a number. This number is
393 decremented by the second argument, DELTA, which defaults to 1."
394 (expand place delta env 'xsubtract)))
396 ;;;; DEFINE-MODIFY-MACRO stuff
398 (sb-xc:defmacro define-modify-macro (name lambda-list function &optional doc-string)
399 "Creates a new read-modify-write macro like PUSH or INCF."
400 (check-designator name 'define-modify-macro)
401 (binding* (((nil required optional rest)
402 (parse-lambda-list
403 lambda-list
404 :accept (lambda-list-keyword-mask '(&optional &rest))
405 :context "a DEFINE-MODIFY-MACRO lambda list"))
406 (args (append required
407 (mapcar (lambda (x) (if (listp x) (car x) x))
408 optional)))
409 (place (make-symbol "PLACE"))
410 (env (make-symbol "ENV")))
411 `(sb-xc:defmacro ,name (,place ,@lambda-list &environment ,env)
412 ,@(when doc-string (list (the string doc-string)))
413 (expand-rmw-macro ',function '() ,place
414 (list* ,@args ,(car rest)) t ,env ',args))))
416 ;;;; DEFSETF
418 ;;; Assign SETF macro information for NAME, making all appropriate checks.
419 (defun %defsetf (name expander)
420 (with-single-package-locked-error
421 (:symbol name "defining a setf-expander for ~A"))
422 (let ((setf-fn-name `(setf ,name)))
423 (multiple-value-bind (where-from present-p)
424 (info :function :where-from setf-fn-name)
425 ;; One might think that :DECLARED merits a style warning, but SBCL
426 ;; provides ~58 standard accessors as both (SETF F) and a macro.
427 ;; So allow the user to declaim an FTYPE and we'll hush up.
428 ;; What's good for the the goose is good for the gander.
429 (case where-from
430 (:assumed
431 ;; This indicates probable user error. Compilation assumed something
432 ;; to be functional; a macro says otherwise. Because :where-from's
433 ;; default can be :assumed, PRESENT-P disambiguates "defaulted" from
434 ;; "known" to have made an existence assumption.
435 (when present-p
436 (warn "defining setf macro for ~S when ~S was previously ~
437 treated as a function" name setf-fn-name)))
438 ;; This is a useless and unavoidable warning during self-build.
439 ;; cf. similar disabling of warning in WARN-IF-SETF-MACRO.
440 #-sb-xc-host
441 (:defined
442 ;; Somebody defined (SETF F) but then also said F has a macro.
443 ;; A soft warning seems appropriate because in this case it's
444 ;; at least in theory not wrong to call the function.
445 ;; The user can declare an FTYPE if both things are intentional.
446 (style-warn "defining setf macro for ~S when ~S is also defined"
447 name setf-fn-name)))))
448 (setf (info :setf :expander name) expander)
449 name)
451 ;;; This is pretty broken if there are keyword arguments (lp#1452947)
452 ;;; but the bug seems to be due to irreconcilable problems in the spec.
453 ;;; Everybody seems to interpret the spec the way we do though.
454 (sb-xc:defmacro defsetf (access-fn &rest rest)
455 "Associates a SETF update function or macro with the specified access
456 function or macro. The format is complex. See the manual for details."
457 (check-designator access-fn 'defsetf
458 #'symbolp "symbol" "access-function")
459 (typecase rest
460 ((cons (and symbol (not null)) (or null (cons string null)))
461 `(eval-when (:load-toplevel :compile-toplevel :execute)
462 (%defsetf ',access-fn
463 (list* ',(car rest) ,(cadr rest) (sb-c:source-location)))))
464 ((cons list (cons list))
465 (destructuring-bind (lambda-list (&rest stores) &body body) rest
466 (binding* (((llks req opt rest key aux env)
467 (parse-lambda-list
468 lambda-list
469 :accept (lambda-list-keyword-mask
470 '(&optional &rest &key &allow-other-keys
471 &environment))
472 :context "a DEFSETF lambda list"))
473 ((forms decls doc) (parse-body body t))
474 ((outer-decls inner-decls)
475 (extract-var-decls decls (append env stores)))
476 (subforms (copy-symbol 'subforms))
477 (env-var (if env (car env) (copy-symbol 'env)))
478 (lambda-list (make-lambda-list llks nil req opt rest key)))
479 (declare (ignore aux))
480 `(eval-when (:compile-toplevel :load-toplevel :execute)
481 (%defsetf ',access-fn
482 (cons ,(length stores)
483 (named-lambda (%defsetf ,access-fn) (,subforms ,env-var ,@stores)
484 (declare (sb-c::lambda-list ,lambda-list))
485 ,@(if doc (list doc))
486 ,@(if outer-decls (list outer-decls))
487 ,@(unless env `((declare (ignore ,env-var))))
488 (apply (lambda ,lambda-list
489 ,@inner-decls (block ,access-fn ,@forms))
490 ,subforms))))))))
492 (error "Ill-formed DEFSETF for ~S" access-fn))))
494 ;; Given SEXPRS which is a list of things to evaluate, return four values:
495 ;; - a list of uninterned symbols to bind to any non-constant sexpr
496 ;; - a list of things to bind those symbols to
497 ;; - a list parallel to SEXPRS with each non-constant element
498 ;; replaced by its temporary variable from the first list.
499 ;; - a bitmask over the sexprs containing a 1 for each non-constant.
500 ;; Uninterned symbols are named according to the NAME-HINTS so that
501 ;; expansions use variables resembling the DEFSETF whence they came.
503 (defun collect-setf-temps (sexprs environment name-hints)
504 (labels ((next-name-hint ()
505 ;; OK if list was nil or :UNKNOWN
506 (let ((sym (and (listp name-hints) (pop name-hints))))
507 (case sym
508 (&optional (next-name-hint))
509 ((&key &rest) (setq name-hints nil))
510 (t (if (listp sym) (car sym) sym)))))
511 (nice-tempname (form)
512 (acond ((next-name-hint) (copy-symbol it))
513 (t (gensymify form)))))
514 (collect ((temp-vars) (temp-vals) (call-arguments))
515 (let ((mask 0) (bit 1))
516 (dolist (form sexprs (values (temp-vars) (temp-vals) (call-arguments)
517 mask))
518 (call-arguments (if (constantp form environment)
519 (progn (next-name-hint) form) ; Skip one hint.
520 (let ((temp (nice-tempname form)))
521 (setq mask (logior mask bit))
522 (temp-vars temp)
523 (temp-vals form)
524 temp)))
525 (setq bit (ash bit 1)))))))
527 ;; Expand a macro defined by DEFINE-MODIFY-MACRO.
528 ;; The generated call resembles (FUNCTION <before-args> PLACE <after-args>)
529 ;; but the read/write of PLACE is done after all {BEFORE,AFTER}-ARG-FORMS are
530 ;; evaluated. Subforms of PLACE are evaluated in the usual order.
532 ;; Exception: See comment at PUSHNEW for the effect of AFTER-ARGS-BINDP = NIL.
533 (defun expand-rmw-macro (function before-arg-forms place after-arg-forms
534 after-args-bindp environment name-hints)
535 ;; Note that NAME-HINTS do the wrong thing if you have both "before" and
536 ;; "after" args. In that case it is probably best to specify them as ().
537 (binding* (((before-temps before-vals before-args)
538 (collect-setf-temps before-arg-forms environment name-hints))
539 ((place-temps place-subforms stores setter getter)
540 (get-setf-expansion place environment))
541 ((after-temps after-vals after-args)
542 (if after-args-bindp
543 (collect-setf-temps after-arg-forms environment name-hints)
544 (values nil nil after-arg-forms)))
545 (compute `(,function ,@before-args ,getter ,@after-args))
546 (set-fn (and (listp setter) (car setter)))
547 (newval-temp (car stores))
548 (newval-binding `((,newval-temp ,compute))))
549 ;; Elide the binding of NEWVAL-TEMP if it is ref'd exactly once
550 ;; and all the call arguments are temporaries and/or constants.
551 (when (and (= (count newval-temp setter) 1)
552 (or (eq set-fn 'setq)
553 (and (eq (info :function :kind set-fn) :function)
554 (every (lambda (x)
555 (or (member x place-temps)
556 (eq x newval-temp)
557 (constantp x environment)))
558 (cdr setter)))))
559 (setq newval-binding nil
560 setter (substitute compute newval-temp setter)))
561 (let ((bindings
562 (flet ((zip (list1 list2) (mapcar #'list list1 list2)))
563 (append (zip before-temps before-vals)
564 (zip place-temps place-subforms)
565 (zip after-temps after-vals)
566 newval-binding
567 (cdr stores)))))
568 (if bindings `(let* ,bindings ,setter) setter))))
570 ;;;; DEFMACRO DEFINE-SETF-EXPANDER and various DEFINE-SETF-EXPANDERs
572 ;;; DEFINE-SETF-EXPANDER is a lot like DEFMACRO.
573 (sb-xc:defmacro define-setf-expander (access-fn lambda-list &body body)
574 "Syntax like DEFMACRO, but creates a setf expander function. The body
575 of the definition must be a form that returns five appropriate values."
576 (check-designator access-fn 'define-setf-expander
577 #'symbolp "symbol" "access-function")
578 `(eval-when (:compile-toplevel :load-toplevel :execute)
579 (%defsetf ',access-fn
580 ,(make-macro-lambda `(setf-expander ,access-fn) lambda-list body
581 'define-setf-expander access-fn
582 :doc-string-allowed :internal))))