Cosmetic improvements in PCL code
[sbcl.git] / src / code / setf.lisp
blobca6688d3785ab7094e8dd1ddad887d1f524b3b35
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 sb!xc:get-setf-expansion))
50 (defun sb!xc:get-setf-expansion (form &optional environment)
51 #!+sb-doc
52 "Return five values needed by the SETF machinery: a list of temporary
53 variables, a list of values with which to fill them, a list of temporaries
54 for the new values, the setting function, and the accessing function."
55 (named-let retry ((form form))
56 (labels ((newvals (count)
57 (let ((sb!xc:*gensym-counter* 1))
58 (make-gensym-list count "NEW")))
59 ;; Produce the expansion of a SETF form that calls either
60 ;; #'(SETF name) or an inverse given by short form DEFSETF.
61 (call (call arg-maker &aux (vals (newvals 1)))
62 (multiple-value-bind (temp-vars temp-vals args)
63 (collect-setf-temps (cdr form) environment nil)
64 (values temp-vars temp-vals vals
65 `(,.call ,@(funcall arg-maker (car vals) args))
66 `(,(car form) ,@args)))))
67 (declare (ftype (sfunction (t) list) newvals))
68 (if (atom form)
69 (multiple-value-bind (expansion expanded)
70 ;; Previously this called %MACROEXPAND, but the two operations
71 ;; are equivalent on atoms, so do the one that is "less".
72 (sb!xc:macroexpand-1 form environment)
73 (if expanded
74 (retry expansion)
75 (let ((vals (newvals 1)))
76 (values nil nil vals `(setq ,form ,(car vals)) form))))
77 (let ((fname (car form)))
78 ;; Local functions inhibit global SETF methods.
79 (unless (sb!c::fun-locally-defined-p fname environment)
80 (awhen (info :setf :expander fname)
81 (return-from retry
82 (typecase it
83 (symbol ; short DEFSETF
84 (call `(,it) (lambda (new args) `(,@args ,new))))
85 (list ; long DEFSETF
86 (binding* ((newvals (newvals (car it)))
87 (expander (the function (cdr it)))
88 ((tempvars tempvals call-args)
89 (collect-setf-temps
90 (cdr form) environment
91 ;; NAME-HINTS affect aesthetics only
92 (or #+sb-xc (%fun-lambda-list expander)))))
93 (values tempvars tempvals newvals
94 (apply expander call-args environment newvals)
95 `(,fname ,@call-args))))
96 ;; DEFINE-SETF-EXPANDER
97 (function (funcall it form environment)))))
98 (awhen (transformable-struct-setf-p form environment)
99 (let ((instance (make-symbol "OBJ"))
100 (vals (newvals 1)))
101 (return-from retry
102 (values (list instance) (list (cadr form)) vals
103 (slot-access-transform
104 :setf (list instance (car vals)) it)
105 (slot-access-transform
106 :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 #!+sb-doc
163 "Takes pairs of arguments like SETQ. The first is a place and the second
164 is the value that is supposed to go into that place. Returns the last
165 value. The place argument may be any of the access forms for which SETF
166 knows a corresponding setting form."
167 (unless args
168 (return-from setf nil))
169 (destructuring-bind (place value-form . more) args
170 (when more
171 (return-from setf `(progn ,@(sb!c::explode-setq form 'error))))
172 (when (atom (setq place (macroexpand-for-setf place env)))
173 (return-from setf `(setq ,place ,value-form)))
175 (let ((fun (car place)))
176 (when (and (symbolp fun)
177 ;; Local definition of FUN precludes global knowledge.
178 (not (sb!c::fun-locally-defined-p fun env)))
179 (let ((inverse (info :setf :expander fun)))
180 ;; NIL is not a valid setf inverse name, for two reasons:
181 ;; 1. you can't define a function named NIL,
182 ;; 2. (DEFSETF THING () ...) is the long form DEFSETF syntax.
183 (when (typep inverse '(and symbol (not null)))
184 (return-from setf `(,inverse ,@(cdr place) ,value-form))))
185 (awhen (transformable-struct-setf-p place env)
186 (return-from setf
187 (slot-access-transform
188 :setf (list (cadr place) value-form) it)))))
190 (multiple-value-bind (temps vals newval setter)
191 (sb!xc:get-setf-expansion place env)
192 (car (gen-let* (mapcar #'list temps vals)
193 (gen-mv-bind newval value-form (forms-list setter)))))))
195 ;; various SETF-related macros
197 (sb!xc:defmacro shiftf (&whole form &rest args &environment env)
198 #!+sb-doc
199 "One or more SETF-style place expressions, followed by a single
200 value expression. Evaluates all of the expressions in turn, then
201 assigns the value of each expression to the place on its left,
202 returning the value of the leftmost."
203 (when (< (length args) 2)
204 (error "~S called with too few arguments: ~S" 'shiftf form))
205 (collect ((let*-bindings) (mv-bindings) (setters) (getters))
206 (dolist (arg (butlast args))
207 (multiple-value-bind (temps subforms store-vars setter getter)
208 (sb!xc:get-setf-expansion arg env)
209 (let*-bindings (mapcar #'list temps subforms))
210 (mv-bindings store-vars)
211 (setters setter)
212 (getters getter)))
213 ;; Handle the last arg specially here. The getter is just the last
214 ;; arg itself.
215 (getters (car (last args)))
216 (labels ((thunk (mv-bindings getters setters)
217 (if mv-bindings
218 (gen-mv-bind (car mv-bindings) (car getters)
219 (thunk (cdr mv-bindings) (cdr getters) setters))
220 setters)))
221 (let ((outputs (loop for i below (length (car (mv-bindings)))
222 collect (sb!xc:gensym "OUT"))))
223 (car (gen-let* (reduce #'nconc (let*-bindings))
224 (gen-mv-bind outputs (car (getters))
225 (thunk (mv-bindings) (cdr (getters))
226 `(,@(de-values-ify (setters))
227 (values ,@outputs))))))))))
229 (labels
230 ((expand (args env operator single-op)
231 (cond ((singleton-p (cdr args)) ; commonest case probably
232 (return-from expand `(progn (,single-op ,@args) nil)))
233 ((not args)
234 (return-from expand nil)))
235 (collect ((let*-bindings) (mv-bindings) (setters))
236 (do ((a args (cddr a)))
237 ((endp a))
238 (when (endp (cdr a))
239 (error "Odd number of args to ~S." operator))
240 (let ((place (car a))
241 (value-form (cadr a)))
242 (when (and (not (symbolp place)) (eq operator 'psetq))
243 (error 'simple-program-error
244 :format-control "Place ~S in PSETQ is not a SYMBOL"
245 :format-arguments (list place)))
246 (multiple-value-bind (temps vals stores setter)
247 (sb!xc:get-setf-expansion place env)
248 (let*-bindings (mapcar #'list temps vals))
249 (mv-bindings (cons stores value-form))
250 (setters setter))))
251 (car (build (let*-bindings) (mv-bindings)
252 (de-values-ify (setters))))))
253 (build (let*-bindings mv-bindings setters)
254 (if let*-bindings
255 (gen-let* (car let*-bindings)
256 (gen-mv-bind (caar mv-bindings) (cdar mv-bindings)
257 (build (cdr let*-bindings) (cdr mv-bindings)
258 setters)))
259 `(,@setters nil))))
261 (sb!xc:defmacro psetf (&rest pairs &environment env)
262 #!+sb-doc
263 "This is to SETF as PSETQ is to SETQ. Args are alternating place
264 expressions and values to go into those places. All of the subforms and
265 values are determined, left to right, and only then are the locations
266 updated. Returns NIL."
267 (expand pairs env 'psetf 'setf))
269 (sb!xc:defmacro psetq (&rest pairs &environment env)
270 #!+sb-doc
271 "PSETQ {var value}*
272 Set the variables to the values, like SETQ, except that assignments
273 happen in parallel, i.e. no assignments take place until all the
274 forms have been evaluated."
275 (expand pairs env 'psetq 'setq))))
277 (sb!xc:defmacro rotatef (&rest args &environment env)
278 #!+sb-doc
279 "Takes any number of SETF-style place expressions. Evaluates all of the
280 expressions in turn, then assigns to each place the value of the form to
281 its right. The rightmost form gets the value of the leftmost.
282 Returns NIL."
283 (when args
284 (collect ((let*-bindings) (mv-bindings) (setters) (getters))
285 (dolist (arg args)
286 (multiple-value-bind (temps subforms store-vars setter getter)
287 (sb!xc:get-setf-expansion arg env)
288 (let*-bindings (mapcar #'list temps subforms))
289 (mv-bindings store-vars)
290 (setters setter)
291 (getters getter)))
292 (setters nil)
293 (getters (car (getters)))
294 (labels ((thunk (mv-bindings getters)
295 (if mv-bindings
296 `((multiple-value-bind ,(car mv-bindings) ,(car getters)
297 ,@(thunk (cdr mv-bindings) (cdr getters))))
298 (setters))))
299 `(let* ,(reduce #'append (let*-bindings))
300 ,@(thunk (mv-bindings) (cdr (getters))))))))
302 (sb!xc:defmacro push (obj place &environment env)
303 #!+sb-doc
304 "Takes an object and a location holding a list. Conses the object onto
305 the list, returning the modified list. OBJ is evaluated before PLACE."
306 ;; If PLACE has multiple store locations, what should we do?
307 ;; In other Lisp implementations:
308 ;; - One errs, says "Multiple store variables not expected"
309 ;; - One pushes multiple values produced by OBJ form into multiple places.
310 ;; - At least two produce an incorrect expansion that doesn't even work.
311 (expand-rmw-macro 'cons (list obj) place '() nil env '(item)))
313 (sb!xc:defmacro pushnew (obj place &rest keys &environment env)
314 #!+sb-doc
315 "Takes an object and a location holding a list. If the object is
316 already in the list, does nothing; otherwise, conses the object onto
317 the list. Keyword arguments are accepted as per the ADJOIN function."
318 ;; Passing AFTER-ARGS-BINDP = NIL causes the forms subsequent to PLACE
319 ;; to be inserted literally as-is, giving the (apparently) desired behavior
320 ;; of *not* evaluating them before the Read/Modify/Write of PLACE, which
321 ;; seems to be an exception to the 5.1.3 exception on L-to-R evaluation.
322 ;; The spec only mentions that ITEM is eval'd before PLACE.
323 (expand-rmw-macro 'adjoin (list obj) place keys nil env '(item)))
325 (sb!xc:defmacro pop (place &environment env)
326 #!+sb-doc
327 "The argument is a location holding a list. Pops one item off the front
328 of the list and returns it."
329 (if (symbolp (setq place (macroexpand-for-setf place env)))
330 `(prog1 (car ,place) (setq ,place (cdr ,place)))
331 (multiple-value-bind (temps vals stores setter getter)
332 (sb!xc:get-setf-expansion place env)
333 (let ((list (copy-symbol 'list))
334 (ret (copy-symbol 'car)))
335 `(let* (,@(mapcar #'list temps vals)
336 (,list ,getter)
337 (,ret (car ,list))
338 (,(car stores) (cdr ,list))
339 ,@(cdr stores))
340 ,setter
341 ,ret)))))
343 (sb!xc:defmacro remf (place indicator &environment env)
344 #!+sb-doc
345 "Place may be any place expression acceptable to SETF, and is expected
346 to hold a property list or (). This list is destructively altered to
347 remove the property specified by the indicator. Returns T if such a
348 property was present, NIL if not."
349 (multiple-value-bind (temps vals newval setter getter)
350 (sb!xc:get-setf-expansion place env)
351 (let* ((flag (make-symbol "FLAG"))
352 (body `(multiple-value-bind (,(car newval) ,flag)
353 ;; See ANSI 5.1.3 for why we do out-of-order evaluation
354 (truly-the (values list boolean)
355 (%remf ,indicator ,getter))
356 ,(if (cdr newval) `(let ,(cdr newval) ,setter) setter)
357 ,flag)))
358 (if temps `(let* ,(mapcar #'list temps vals) ,body) body))))
360 ;; Perform the work of REMF.
361 (defun %remf (indicator plist)
362 (let ((tail plist) (predecessor))
363 (loop
364 (when (endp tail) (return (values plist nil)))
365 (let ((key (pop tail)))
366 (when (atom tail)
367 (error (if tail
368 "Improper list in REMF."
369 "Odd-length list in REMF.")))
370 (let ((next (cdr tail)))
371 (when (eq key indicator)
372 ;; This function is strict in its return type!
373 (the list next) ; for effect
374 (return (values (cond (predecessor
375 (setf (cdr predecessor) next)
376 plist)
378 next))
379 t)))
380 (setq predecessor tail tail next))))))
382 ;;; INCF and DECF have a straightforward expansion, avoiding temp vars,
383 ;;; when the PLACE is a non-macro symbol. Otherwise we do the generalized
384 ;;; SETF-like thing. The compiler doesn't care either way, but this
385 ;;; reduces the incentive to treat some macros as special-forms when
386 ;;; squeezing more performance from a Lisp interpreter.
387 ;;; DEFINE-MODIFY-MACRO could be used, but this expands more compactly.
388 (flet ((expand (place delta env operator)
389 (if (symbolp (setq place (macroexpand-for-setf place env)))
390 `(setq ,place (,operator ,delta ,place))
391 (multiple-value-bind (dummies vals newval setter getter)
392 (sb!xc:get-setf-expansion place env)
393 `(let* (,@(mapcar #'list dummies vals)
394 (,(car newval) (,operator ,delta ,getter))
395 ,@(cdr newval))
396 ,setter)))))
397 (sb!xc:defmacro incf (place &optional (delta 1) &environment env)
398 #!+sb-doc
399 "The first argument is some location holding a number. This number is
400 incremented by the second argument, DELTA, which defaults to 1."
401 (expand place delta env '+))
403 (sb!xc:defmacro decf (place &optional (delta 1) &environment env)
404 #!+sb-doc
405 "The first argument is some location holding a number. This number is
406 decremented by the second argument, DELTA, which defaults to 1."
407 (expand place delta env 'xsubtract)))
409 ;;;; DEFINE-MODIFY-MACRO stuff
411 (sb!xc:defmacro sb!xc:define-modify-macro (name lambda-list function &optional doc-string)
412 #!+sb-doc
413 "Creates a new read-modify-write macro like PUSH or INCF."
414 (binding* (((nil required optional rest)
415 (parse-lambda-list
416 lambda-list
417 :accept (lambda-list-keyword-mask '(&optional &rest))
418 :context "a DEFINE-MODIFY-MACRO lambda list"))
419 (args (append required
420 (mapcar (lambda (x) (if (listp x) (car x) x))
421 optional)))
422 (place (make-symbol "PLACE"))
423 (env (make-symbol "ENV")))
424 `(sb!xc:defmacro ,name (,place ,@lambda-list &environment ,env)
425 ,@(when doc-string (list (the string doc-string)))
426 (expand-rmw-macro ',function '() ,place
427 (list* ,@args ,(car rest)) t ,env ',args))))
429 ;;;; DEFSETF
431 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
432 ;;; Assign SETF macro information for NAME, making all appropriate checks.
433 (defun %defsetf (name expander &optional doc)
434 (with-single-package-locked-error
435 (:symbol name "defining a setf-expander for ~A"))
436 (let ((setf-fn-name `(setf ,name)))
437 (multiple-value-bind (where-from present-p)
438 (info :function :where-from setf-fn-name)
439 ;; One might think that :DECLARED merits a style warning, but SBCL
440 ;; provides ~58 standard accessors as both (SETF F) and a macro.
441 ;; So allow the user to declaim an FTYPE and we'll hush up.
442 ;; What's good for the the goose is good for the gander.
443 (case where-from
444 (:assumed
445 ;; This indicates probable user error. Compilation assumed something
446 ;; to be functional; a macro says otherwise. Because :where-from's
447 ;; default can be :assumed, PRESENT-P disambiguates "defaulted" from
448 ;; "known" to have made an existence assumption.
449 (when present-p
450 (warn "defining setf macro for ~S when ~S was previously ~
451 treated as a function" name setf-fn-name)))
452 ;; This is a useless and unavoidable warning during self-build.
453 ;; cf. similar disabling of warning in WARN-IF-SETF-MACRO.
454 #-sb-xc-host
455 (:defined
456 ;; Somebody defined (SETF F) but then also said F has a macro.
457 ;; A soft warning seems appropriate because in this case it's
458 ;; at least in theory not wrong to call the function.
459 ;; The user can declare an FTYPE if both things are intentional.
460 (style-warn "defining setf macro for ~S when ~S is also defined"
461 name setf-fn-name)))))
462 (setf (info :setf :expander name) expander)
463 (when doc
464 (setf (fdocumentation name 'setf) doc))
465 name))
467 ;;; This is pretty broken if there are keyword arguments (lp#1452947)
468 ;;; but the bug seems to be due to irreconcilable problems in the spec.
469 ;;; Everybody seems to interpret the spec the way we do though.
470 (sb!xc:defmacro sb!xc:defsetf (access-fn &rest rest)
471 #!+sb-doc
472 "Associates a SETF update function or macro with the specified access
473 function or macro. The format is complex. See the manual for details."
474 (unless (symbolp access-fn)
475 (error "~S access-function name ~S is not a symbol."
476 'sb!xc:defsetf access-fn))
477 (typecase rest
478 ((cons (and symbol (not null)) (or null (cons string null)))
479 `(eval-when (:load-toplevel :compile-toplevel :execute)
480 (%defsetf ',access-fn ',(car rest) ,@(cdr rest))))
481 ((cons list (cons list))
482 (destructuring-bind (lambda-list (&rest stores) &body body) rest
483 (binding* (((llks req opt rest key aux env)
484 (parse-lambda-list
485 lambda-list
486 :accept (lambda-list-keyword-mask
487 '(&optional &rest &key &allow-other-keys
488 &environment))
489 :context "a DEFSETF lambda list"))
490 ((forms decls doc) (parse-body body t))
491 ((outer-decls inner-decls)
492 (extract-var-decls decls (append env stores)))
493 (subforms (copy-symbol 'subforms))
494 (env-var (if env (car env) (copy-symbol 'env)))
495 (lambda-list (make-lambda-list llks nil req opt rest key)))
496 (declare (ignore aux))
497 `(eval-when (:compile-toplevel :load-toplevel :execute)
498 (%defsetf ',access-fn
499 (cons ,(length stores)
500 (named-lambda (%defsetf ,access-fn)
501 (,subforms ,env-var ,@stores)
502 (declare (sb!c::lambda-list ,lambda-list))
503 ,@(if outer-decls (list outer-decls))
504 ,@(unless env `((declare (ignore ,env-var))))
505 (apply (lambda ,lambda-list
506 ,@inner-decls (block ,access-fn ,@forms))
507 ,subforms)))
508 ,@(and doc `(,doc)))))))
510 (error "Ill-formed DEFSETF for ~S" access-fn))))
512 ;; Given SEXPRS which is a list of things to evaluate, return four values:
513 ;; - a list of uninterned symbols to bind to any non-constant sexpr
514 ;; - a list of things to bind those symbols to
515 ;; - a list parallel to SEXPRS with each non-constant element
516 ;; replaced by its temporary variable from the first list.
517 ;; - a bitmask over the sexprs containing a 1 for each non-constant.
518 ;; Uninterned symbols are named according to the NAME-HINTS so that
519 ;; expansions use variables resembling the DEFSETF whence they came.
521 (defun collect-setf-temps (sexprs environment name-hints)
522 (labels ((next-name-hint ()
523 (let ((sym (pop name-hints))) ; OK if list was nil
524 (case sym
525 (&optional (next-name-hint))
526 ((&key &rest) (setq name-hints nil))
527 (t (if (listp sym) (car sym) sym)))))
528 (nice-tempname (form)
529 (acond ((next-name-hint) (copy-symbol it))
530 (t (gensymify form)))))
531 (collect ((temp-vars) (temp-vals) (call-arguments))
532 (let ((mask 0) (bit 1))
533 (dolist (form sexprs (values (temp-vars) (temp-vals) (call-arguments)
534 mask))
535 (call-arguments (if (sb!xc:constantp form environment)
536 (progn (next-name-hint) form) ; Skip one hint.
537 (let ((temp (nice-tempname form)))
538 (setq mask (logior mask bit))
539 (temp-vars temp)
540 (temp-vals form)
541 temp)))
542 (setq bit (ash bit 1)))))))
544 ;; Expand a macro defined by DEFINE-MODIFY-MACRO.
545 ;; The generated call resembles (FUNCTION <before-args> PLACE <after-args>)
546 ;; but the read/write of PLACE is done after all {BEFORE,AFTER}-ARG-FORMS are
547 ;; evaluated. Subforms of PLACE are evaluated in the usual order.
549 ;; Exception: See comment at PUSHNEW for the effect of AFTER-ARGS-BINDP = NIL.
550 (defun expand-rmw-macro (function before-arg-forms place after-arg-forms
551 after-args-bindp environment name-hints)
552 ;; Note that NAME-HINTS do the wrong thing if you have both "before" and
553 ;; "after" args. In that case it is probably best to specify them as ().
554 (binding* (((before-temps before-vals before-args)
555 (collect-setf-temps before-arg-forms environment name-hints))
556 ((place-temps place-subforms stores setter getter)
557 (sb!xc:get-setf-expansion place environment))
558 ((after-temps after-vals after-args)
559 (if after-args-bindp
560 (collect-setf-temps after-arg-forms environment name-hints)
561 (values nil nil after-arg-forms)))
562 (compute `(,function ,@before-args ,getter ,@after-args))
563 (set-fn (and (listp setter) (car setter)))
564 (newval-temp (car stores))
565 (newval-binding `((,newval-temp ,compute))))
566 ;; Elide the binding of NEWVAL-TEMP if it is ref'd exactly once
567 ;; and all the call arguments are temporaries and/or constants.
568 (when (and (= (count newval-temp setter) 1)
569 (or (eq set-fn 'setq)
570 (and (eq (info :function :kind set-fn) :function)
571 (every (lambda (x)
572 (or (member x place-temps)
573 (eq x newval-temp)
574 (sb!xc:constantp x environment)))
575 (cdr setter)))))
576 (setq newval-binding nil
577 setter (substitute compute newval-temp setter)))
578 (let ((bindings
579 (flet ((zip (list1 list2) (mapcar #'list list1 list2)))
580 (append (zip before-temps before-vals)
581 (zip place-temps place-subforms)
582 (zip after-temps after-vals)
583 newval-binding
584 (cdr stores)))))
585 (if bindings `(let* ,bindings ,setter) setter))))
587 ;;;; DEFMACRO DEFINE-SETF-EXPANDER and various DEFINE-SETF-EXPANDERs
589 ;;; DEFINE-SETF-EXPANDER is a lot like DEFMACRO.
590 (sb!xc:defmacro sb!xc:define-setf-expander (access-fn lambda-list &body body)
591 #!+sb-doc
592 "Syntax like DEFMACRO, but creates a setf expander function. The body
593 of the definition must be a form that returns five appropriate values."
594 (unless (symbolp access-fn)
595 (error "~S access-function name ~S is not a symbol."
596 'sb!xc:define-setf-expander access-fn))
597 (multiple-value-bind (def doc)
598 ;; Perhaps it would be more elegant to keep the docstring attached
599 ;; to the expander function, as for CAS?
600 (make-macro-lambda `(setf-expander ,access-fn) lambda-list body
601 'sb!xc:define-setf-expander access-fn
602 :doc-string-allowed :external)
603 `(eval-when (:compile-toplevel :load-toplevel :execute)
604 (%defsetf ',access-fn ,def ,@(and doc `(,doc))))))