0.7.8.7:
[sbcl/lichteblau.git] / src / code / early-setf.lisp
blob4ee0528164ab343dce74e2d92e230b78422c0330
1 ;;;; SETF and friends (except for stuff defined with COLLECT, which
2 ;;;; comes later)
3 ;;;;
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.
11 ;;;;
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
21 ;;; one of two ways:
22 ;;;
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.
26 ;;;
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)
34 #!+sb-doc
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."
38 (let (temp)
39 (cond ((symbolp form)
40 (multiple-value-bind (expansion expanded)
41 (sb!xc:macroexpand-1 form environment)
42 (if expanded
43 (sb!xc:get-setf-expansion expansion environment)
44 (let ((new-var (gensym)))
45 (values nil nil (list new-var)
46 `(setq ,form ,new-var) form)))))
47 ;; Local functions inhibit global SETF methods.
48 ((and environment
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))))
53 (return t)))))
54 (expand-or-get-setf-inverse form environment))
55 ((setq temp (info :setf :inverse (car form)))
56 (get-setf-method-inverse form `(,temp) nil))
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
62 (funcall temp
63 form
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.
68 ;; -- WHN 19991128
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)
80 #!+sb-doc
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 ~
88 variables:~% ~S"
89 form))
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
93 ;;; SETF function.
94 (declaim (ftype (function (t sb!c::lexenv)) expand-or-get-setf-inverse))
95 (defun expand-or-get-setf-inverse (form environment)
96 (multiple-value-bind (expansion expanded)
97 (sb!xc:macroexpand-1 form environment)
98 (if expanded
99 (sb!xc:get-setf-expansion expansion environment)
100 (get-setf-method-inverse form
101 `(funcall #'(setf ,(car form)))
102 t))))
104 (defun get-setf-method-inverse (form inverse setf-fun)
105 (let ((new-var (gensym))
106 (vars nil)
107 (vals nil))
108 (dolist (x (cdr form))
109 (push (gensym) vars)
110 (push x vals))
111 (setq vals (nreverse vals))
112 (values vars vals (list new-var)
113 (if setf-fun
114 `(,@inverse ,new-var ,@vars)
115 `(,@inverse ,@vars ,new-var))
116 `(,(car form) ,@vars))))
118 ;;;; SETF itself
120 ;;; Except for atoms, we always call GET-SETF-EXPANSION, since it has
121 ;;; some non-trivial semantics. But when there is a setf inverse, and
122 ;;; G-S-E uses it, then we return a call to the inverse, rather than
123 ;;; returning a hairy LET form. This is probably important mainly as a
124 ;;; convenience in allowing the use of SETF inverses without the full
125 ;;; interpreter.
126 (defmacro-mundanely setf (&rest args &environment env)
127 #!+sb-doc
128 "Takes pairs of arguments like SETQ. The first is a place and the second
129 is the value that is supposed to go into that place. Returns the last
130 value. The place argument may be any of the access forms for which SETF
131 knows a corresponding setting form."
132 (let ((nargs (length args)))
133 (cond
134 ((= nargs 2)
135 (let ((place (first args))
136 (value-form (second args)))
137 (if (atom place)
138 `(setq ,place ,value-form)
139 (multiple-value-bind (dummies vals newval setter getter)
140 (sb!xc:get-setf-expansion place env)
141 (declare (ignore getter))
142 (let ((inverse (info :setf :inverse (car place))))
143 (if (and inverse (eq inverse (car setter)))
144 `(,inverse ,@(cdr place) ,value-form)
145 `(let* (,@(mapcar #'list dummies vals))
146 (multiple-value-bind ,newval ,value-form
147 ,setter))))))))
148 ((oddp nargs)
149 (error "odd number of args to SETF"))
151 (do ((a args (cddr a))
152 (reversed-setfs nil))
153 ((null a)
154 `(progn ,@(nreverse reversed-setfs)))
155 (push (list 'setf (car a) (cadr a)) reversed-setfs))))))
157 ;;;; various SETF-related macros
159 (defmacro-mundanely shiftf (&whole form &rest args &environment env)
160 #!+sb-doc
161 "One or more SETF-style place expressions, followed by a single
162 value expression. Evaluates all of the expressions in turn, then
163 assigns the value of each expression to the place on its left,
164 returning the value of the leftmost."
165 (when (< (length args) 2)
166 (error "~S called with too few arguments: ~S" 'shiftf form))
167 (let ((resultvar (gensym)))
168 (do ((arglist args (cdr arglist))
169 (bindlist nil)
170 (storelist nil)
171 (lastvar resultvar))
172 ((atom (cdr arglist))
173 (push `(,lastvar ,(first arglist)) bindlist)
174 `(let* ,(nreverse bindlist) ,@(nreverse storelist) ,resultvar))
175 (multiple-value-bind (sm1 sm2 sm3 sm4 sm5)
176 (get-setf-method (first arglist) env)
177 (mapc (lambda (var val)
178 (push `(,var ,val) bindlist))
180 sm2)
181 (push `(,lastvar ,sm5) bindlist)
182 (push sm4 storelist)
183 (setq lastvar (first sm3))))))
185 (defmacro-mundanely push (obj place &environment env)
186 #!+sb-doc
187 "Takes an object and a location holding a list. Conses the object onto
188 the list, returning the modified list. OBJ is evaluated before PLACE."
189 (if (symbolp place)
190 `(setq ,place (cons ,obj ,place))
191 (multiple-value-bind
192 (dummies vals newval setter getter)
193 (get-setf-method place env)
194 (let ((g (gensym)))
195 `(let* ((,g ,obj)
196 ,@(mapcar #'list dummies vals)
197 (,(car newval) (cons ,g ,getter)))
198 ,setter)))))
200 (defmacro-mundanely pushnew (obj place &rest keys &environment env)
201 #!+sb-doc
202 "Takes an object and a location holding a list. If the object is already
203 in the list, does nothing. Else, conses the object onto the list. Returns
204 NIL. If there is a :TEST keyword, this is used for the comparison."
205 (if (symbolp place)
206 `(setq ,place (adjoin ,obj ,place ,@keys))
207 (multiple-value-bind (dummies vals newval setter getter)
208 (get-setf-method place env)
209 (do* ((d dummies (cdr d))
210 (v vals (cdr v))
211 (let-list nil))
212 ((null d)
213 (push (list (car newval) `(adjoin ,obj ,getter ,@keys))
214 let-list)
215 `(let* ,(nreverse let-list)
216 ,setter))
217 (push (list (car d) (car v)) let-list)))))
219 (defmacro-mundanely pop (place &environment env)
220 #!+sb-doc
221 "The argument is a location holding a list. Pops one item off the front
222 of the list and returns it."
223 (if (symbolp place)
224 `(prog1 (car ,place) (setq ,place (cdr ,place)))
225 (multiple-value-bind (dummies vals newval setter getter)
226 (get-setf-method place env)
227 (do* ((d dummies (cdr d))
228 (v vals (cdr v))
229 (let-list nil))
230 ((null d)
231 (push (list (car newval) getter) let-list)
232 `(let* ,(nreverse let-list)
233 (prog1 (car ,(car newval))
234 (setq ,(car newval) (cdr ,(car newval)))
235 ,setter)))
236 (push (list (car d) (car v)) let-list)))))
238 (defmacro-mundanely remf (place indicator &environment env)
239 #!+sb-doc
240 "Place may be any place expression acceptable to SETF, and is expected
241 to hold a property list or (). This list is destructively altered to
242 remove the property specified by the indicator. Returns T if such a
243 property was present, NIL if not."
244 (multiple-value-bind (dummies vals newval setter getter)
245 (get-setf-method place env)
246 (do* ((d dummies (cdr d))
247 (v vals (cdr v))
248 (let-list nil)
249 (ind-temp (gensym))
250 (local1 (gensym))
251 (local2 (gensym)))
252 ((null d)
253 (push (list (car newval) getter) let-list)
254 (push (list ind-temp indicator) let-list)
255 `(let* ,(nreverse let-list)
256 (do ((,local1 ,(car newval) (cddr ,local1))
257 (,local2 nil ,local1))
258 ((atom ,local1) nil)
259 (cond ((atom (cdr ,local1))
260 (error "Odd-length property list in REMF."))
261 ((eq (car ,local1) ,ind-temp)
262 (cond (,local2
263 (rplacd (cdr ,local2) (cddr ,local1))
264 (return t))
265 (t (setq ,(car newval) (cddr ,(car newval)))
266 ,setter
267 (return t))))))))
268 (push (list (car d) (car v)) let-list))))
270 ;;;; DEFINE-MODIFY-MACRO stuff
272 (def!macro sb!xc:define-modify-macro (name lambda-list function &optional doc-string)
273 #!+sb-doc
274 "Creates a new read-modify-write macro like PUSH or INCF."
275 (let ((other-args nil)
276 (rest-arg nil)
277 (env (gensym))
278 (reference (gensym)))
279 ;; Parse out the variable names and &REST arg from the lambda list.
280 (do ((ll lambda-list (cdr ll))
281 (arg nil))
282 ((null ll))
283 (setq arg (car ll))
284 (cond ((eq arg '&optional))
285 ((eq arg '&rest)
286 (if (symbolp (cadr ll))
287 (setq rest-arg (cadr ll))
288 (error "Non-symbol &REST argument in definition of ~S." name))
289 (if (null (cddr ll))
290 (return nil)
291 (error "Illegal stuff after &REST argument.")))
292 ((memq arg '(&key &allow-other-keys &aux))
293 (error "~S not allowed in DEFINE-MODIFY-MACRO lambda list." arg))
294 ((symbolp arg)
295 (push arg other-args))
296 ((and (listp arg) (symbolp (car arg)))
297 (push (car arg) other-args))
298 (t (error "Illegal stuff in lambda list."))))
299 (setq other-args (nreverse other-args))
300 `(#-sb-xc-host sb!xc:defmacro
301 #+sb-xc-host defmacro-mundanely
302 ,name (,reference ,@lambda-list &environment ,env)
303 ,doc-string
304 (multiple-value-bind (dummies vals newval setter getter)
305 (get-setf-method ,reference ,env)
306 (do ((d dummies (cdr d))
307 (v vals (cdr v))
308 (let-list nil (cons (list (car d) (car v)) let-list)))
309 ((null d)
310 (push (list (car newval)
311 ,(if rest-arg
312 `(list* ',function getter ,@other-args ,rest-arg)
313 `(list ',function getter ,@other-args)))
314 let-list)
315 `(let* ,(nreverse let-list)
316 ,setter)))))))
318 (sb!xc:define-modify-macro incf (&optional (delta 1)) +
319 #!+sb-doc
320 "The first argument is some location holding a number. This number is
321 incremented by the second argument, DELTA, which defaults to 1.")
323 (sb!xc:define-modify-macro decf (&optional (delta 1)) -
324 #!+sb-doc
325 "The first argument is some location holding a number. This number is
326 decremented by the second argument, DELTA, which defaults to 1.")
328 ;;;; DEFSETF
330 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
331 ;;; Assign SETF macro information for NAME, making all appropriate checks.
332 (defun assign-setf-macro (name expander inverse doc)
333 (cond ((gethash name sb!c:*setf-assumed-fboundp*)
334 (warn
335 "defining setf macro for ~S when ~S was previously ~
336 treated as a function"
337 name
338 `(setf ,name)))
339 ((not (fboundp `(setf ,name)))
340 ;; All is well, we don't need any warnings.
341 (values))
342 ((not (eq (symbol-package name) (symbol-package 'aref)))
343 (style-warn "defining setf macro for ~S when ~S is fbound"
344 name `(setf ,name))))
345 (remhash name sb!c:*setf-assumed-fboundp*)
346 ;; FIXME: It's probably possible to join these checks into one form which
347 ;; is appropriate both on the cross-compilation host and on the target.
348 (when (or inverse (info :setf :inverse name))
349 (setf (info :setf :inverse name) inverse))
350 (when (or expander (info :setf :expander name))
351 (setf (info :setf :expander name) expander))
352 (when doc
353 (setf (fdocumentation name 'setf) doc))
354 name))
356 (def!macro sb!xc:defsetf (access-fn &rest rest)
357 #!+sb-doc
358 "Associates a SETF update function or macro with the specified access
359 function or macro. The format is complex. See the manual for details."
360 (cond ((not (listp (car rest)))
361 `(eval-when (:load-toplevel :compile-toplevel :execute)
362 (assign-setf-macro ',access-fn
364 ',(car rest)
365 ,(when (and (car rest) (stringp (cadr rest)))
366 `',(cadr rest)))))
367 ((and (cdr rest) (listp (cadr rest)))
368 (destructuring-bind
369 (lambda-list (&rest store-variables) &body body)
370 rest
371 (let ((arglist-var (gensym "ARGS-"))
372 (access-form-var (gensym "ACCESS-FORM-"))
373 (env-var (gensym "ENVIRONMENT-")))
374 (multiple-value-bind (body local-decs doc)
375 (parse-defmacro `(,lambda-list ,@store-variables)
376 arglist-var body access-fn 'defsetf
377 :anonymousp t)
378 `(eval-when (:compile-toplevel :load-toplevel :execute)
379 (assign-setf-macro
380 ',access-fn
381 (lambda (,access-form-var ,env-var)
382 (declare (ignore ,env-var))
383 (%defsetf ,access-form-var ,(length store-variables)
384 (lambda (,arglist-var)
385 ,@local-decs
386 (block ,access-fn
387 ,body))))
389 ',doc))))))
391 (error "ill-formed DEFSETF for ~S" access-fn))))
393 (defun %defsetf (orig-access-form num-store-vars expander)
394 (let (subforms
395 subform-vars
396 subform-exprs
397 store-vars)
398 (dolist (subform (cdr orig-access-form))
399 (if (constantp subform)
400 (push subform subforms)
401 (let ((var (gensym)))
402 (push var subforms)
403 (push var subform-vars)
404 (push subform subform-exprs))))
405 (dotimes (i num-store-vars)
406 (push (gensym) store-vars))
407 (let ((r-subforms (nreverse subforms))
408 (r-subform-vars (nreverse subform-vars))
409 (r-subform-exprs (nreverse subform-exprs))
410 (r-store-vars (nreverse store-vars)))
411 (values r-subform-vars
412 r-subform-exprs
413 r-store-vars
414 (funcall expander (cons r-subforms r-store-vars))
415 `(,(car orig-access-form) ,@r-subforms)))))
417 ;;;; DEFMACRO DEFINE-SETF-EXPANDER and various DEFINE-SETF-EXPANDERs
419 ;;; DEFINE-SETF-EXPANDER is a lot like DEFMACRO.
420 (def!macro sb!xc:define-setf-expander (access-fn lambda-list &body body)
421 #!+sb-doc
422 "Syntax like DEFMACRO, but creates a Setf-Method generator. The body
423 must be a form that returns the five magical values."
424 (unless (symbolp access-fn)
425 (error "DEFINE-SETF-EXPANDER access-function name ~S is not a symbol."
426 access-fn))
427 (let ((whole (gensym "WHOLE-"))
428 (environment (gensym "ENV-")))
429 (multiple-value-bind (body local-decs doc)
430 (parse-defmacro lambda-list whole body access-fn
431 'sb!xc:define-setf-expander
432 :environment environment)
433 `(eval-when (:compile-toplevel :load-toplevel :execute)
434 (assign-setf-macro ',access-fn
435 (lambda (,whole ,environment)
436 ,@local-decs
437 (block ,access-fn ,body))
439 ',doc)))))
441 (sb!xc:define-setf-expander getf (place prop
442 &optional default
443 &environment env)
444 (declare (type sb!c::lexenv env))
445 (multiple-value-bind (temps values stores set get)
446 (get-setf-method place env)
447 (let ((newval (gensym))
448 (ptemp (gensym))
449 (def-temp (if default (gensym))))
450 (values `(,@temps ,ptemp ,@(if default `(,def-temp)))
451 `(,@values ,prop ,@(if default `(,default)))
452 `(,newval)
453 `(let ((,(car stores) (%putf ,get ,ptemp ,newval)))
454 ,set
455 ,newval)
456 `(getf ,get ,ptemp ,@(if default `(,def-temp)))))))
458 (sb!xc:define-setf-expander get (symbol prop &optional default)
459 (let ((symbol-temp (gensym))
460 (prop-temp (gensym))
461 (def-temp (gensym))
462 (newval (gensym)))
463 (values `(,symbol-temp ,prop-temp ,@(if default `(,def-temp)))
464 `(,symbol ,prop ,@(if default `(,default)))
465 (list newval)
466 `(%put ,symbol-temp ,prop-temp ,newval)
467 `(get ,symbol-temp ,prop-temp ,@(if default `(,def-temp))))))
469 (sb!xc:define-setf-expander gethash (key hashtable &optional default)
470 (let ((key-temp (gensym))
471 (hashtable-temp (gensym))
472 (default-temp (gensym))
473 (new-value-temp (gensym)))
474 (values
475 `(,key-temp ,hashtable-temp ,@(if default `(,default-temp)))
476 `(,key ,hashtable ,@(if default `(,default)))
477 `(,new-value-temp)
478 `(%puthash ,key-temp ,hashtable-temp ,new-value-temp)
479 `(gethash ,key-temp ,hashtable-temp ,@(if default `(,default-temp))))))
481 (sb!xc:define-setf-expander logbitp (index int &environment env)
482 (declare (type sb!c::lexenv env))
483 (multiple-value-bind (temps vals stores store-form access-form)
484 (get-setf-method int env)
485 (let ((ind (gensym))
486 (store (gensym))
487 (stemp (first stores)))
488 (values `(,ind ,@temps)
489 `(,index
490 ,@vals)
491 (list store)
492 `(let ((,stemp
493 (dpb (if ,store 1 0) (byte 1 ,ind) ,access-form)))
494 ,store-form
495 ,store)
496 `(logbitp ,ind ,access-form)))))
498 ;;; CMU CL had a comment here that:
499 ;;; Evil hack invented by the gnomes of Vassar Street (though not as evil as
500 ;;; it used to be.) The function arg must be constant, and is converted to
501 ;;; an APPLY of the SETF function, which ought to exist.
503 ;;; It may not be clear (wasn't to me..) that this is a standard thing, but See
504 ;;; "5.1.2.5 APPLY Forms as Places" in the ANSI spec. I haven't actually
505 ;;; verified that this code has any correspondence to that code, but at least
506 ;;; ANSI has some place for SETF APPLY. -- WHN 19990604
507 (sb!xc:define-setf-expander apply (functionoid &rest args)
508 (unless (and (listp functionoid)
509 (= (length functionoid) 2)
510 (eq (first functionoid) 'function)
511 (symbolp (second functionoid)))
512 (error "SETF of APPLY is only defined for function args like #'SYMBOL."))
513 (let ((function (second functionoid))
514 (new-var (gensym))
515 (vars (make-gensym-list (length args))))
516 (values vars args (list new-var)
517 `(apply #'(setf ,function) ,new-var ,@vars)
518 `(apply #',function ,@vars))))
520 ;;; Special-case a BYTE bytespec so that the compiler can recognize it.
521 (sb!xc:define-setf-expander ldb (bytespec place &environment env)
522 #!+sb-doc
523 "The first argument is a byte specifier. The second is any place form
524 acceptable to SETF. Replace the specified byte of the number in this
525 place with bits from the low-order end of the new value."
526 (declare (type sb!c::lexenv env))
527 (multiple-value-bind (dummies vals newval setter getter)
528 (get-setf-method place env)
529 (if (and (consp bytespec) (eq (car bytespec) 'byte))
530 (let ((n-size (gensym))
531 (n-pos (gensym))
532 (n-new (gensym)))
533 (values (list* n-size n-pos dummies)
534 (list* (second bytespec) (third bytespec) vals)
535 (list n-new)
536 `(let ((,(car newval) (dpb ,n-new (byte ,n-size ,n-pos)
537 ,getter)))
538 ,setter
539 ,n-new)
540 `(ldb (byte ,n-size ,n-pos) ,getter)))
541 (let ((btemp (gensym))
542 (gnuval (gensym)))
543 (values (cons btemp dummies)
544 (cons bytespec vals)
545 (list gnuval)
546 `(let ((,(car newval) (dpb ,gnuval ,btemp ,getter)))
547 ,setter
548 ,gnuval)
549 `(ldb ,btemp ,getter))))))
551 (sb!xc:define-setf-expander mask-field (bytespec place &environment env)
552 #!+sb-doc
553 "The first argument is a byte specifier. The second is any place form
554 acceptable to SETF. Replaces the specified byte of the number in this place
555 with bits from the corresponding position in the new value."
556 (declare (type sb!c::lexenv env))
557 (multiple-value-bind (dummies vals newval setter getter)
558 (get-setf-method place env)
559 (let ((btemp (gensym))
560 (gnuval (gensym)))
561 (values (cons btemp dummies)
562 (cons bytespec vals)
563 (list gnuval)
564 `(let ((,(car newval) (deposit-field ,gnuval ,btemp ,getter)))
565 ,setter
566 ,gnuval)
567 `(mask-field ,btemp ,getter)))))
569 (sb!xc:define-setf-expander the (type place &environment env)
570 (declare (type sb!c::lexenv env))
571 (multiple-value-bind (dummies vals newval setter getter)
572 (get-setf-method place env)
573 (values dummies
574 vals
575 newval
576 (subst `(the ,type ,(car newval)) (car newval) setter)
577 `(the ,type ,getter))))