| 1 |
;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: SYSTEM -*- |
| 2 |
;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab: |
| 3 |
|
| 4 |
;;;; |
| 5 |
;;;; Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya. |
| 6 |
;;;; Copyright (c) 1990, Giuseppe Attardi. |
| 7 |
;;;; Copyright (c) 2001, Juan Jose Garcia Ripoll. |
| 8 |
;;;; Copyright (c) 2015, Daniel Kochmański. |
| 9 |
;;;; |
| 10 |
;;;; See file 'LICENSE' for the copyright details. |
| 11 |
|
| 12 |
;;;; setf routines |
| 13 |
|
| 14 |
(in-package "SYSTEM") |
| 15 |
|
| 16 |
(defun do-setf-method-expansion (name lambda args &optional (stores-no 1)) |
| 17 |
(declare (si::c-local)) |
| 18 |
(let* ((vars '()) |
| 19 |
(inits '()) |
| 20 |
(all '()) |
| 21 |
(stores '())) |
| 22 |
(dolist (item args) |
| 23 |
(unless (or (fixnump item) (keywordp item)) |
| 24 |
(push item inits) |
| 25 |
(setq item (gensym)) |
| 26 |
(push item vars)) |
| 27 |
(push item all)) |
| 28 |
(dotimes (i stores-no) |
| 29 |
(push (gensym) stores)) |
| 30 |
(let* ((all (nreverse all))) |
| 31 |
(values (nreverse vars) |
| 32 |
(nreverse inits) |
| 33 |
stores |
| 34 |
(if lambda |
| 35 |
(apply lambda (append stores all)) |
| 36 |
`(funcall #'(setf ,name) ,@stores ,@all)) |
| 37 |
(cons name all))))) |
| 38 |
|
| 39 |
(defun do-defsetf (access-fn function &optional (stores-no 1)) |
| 40 |
(declare (type-assertions nil)) |
| 41 |
(if (symbolp function) |
| 42 |
(do-defsetf access-fn |
| 43 |
#'(lambda (store &rest args) |
| 44 |
`(,function ,@args ,store)) |
| 45 |
stores-no) |
| 46 |
(do-define-setf-method access-fn |
| 47 |
#'(lambda (args env) |
| 48 |
(declare (ignore env)) |
| 49 |
(do-setf-method-expansion access-fn function (cdr args) stores-no))))) |
| 50 |
|
| 51 |
(defun do-define-setf-method (access-fn function) |
| 52 |
(declare (type-assertions nil)) |
| 53 |
(put-sysprop access-fn 'SETF-METHOD function)) |
| 54 |
|
| 55 |
;;; DEFSETF macro. |
| 56 |
(defmacro defsetf (&whole whole access-fn &rest rest) |
| 57 |
"Syntax: (defsetf symbol update-fun [doc]) |
| 58 |
or |
| 59 |
(defsetf symbol lambda-list (store-var*) {decl | doc}* {form}*) |
| 60 |
Defines an expansion |
| 61 |
(setf (SYMBOL arg1 ... argn) value) |
| 62 |
=> (UPDATE-FUN arg1 ... argn value) |
| 63 |
or |
| 64 |
(let* ((temp ARG)*) |
| 65 |
(multiple-value-bind (temp-s*) |
| 66 |
values-form |
| 67 |
rest) |
| 68 |
where REST is the value of the last FORM with parameters in |
| 69 |
LAMBDA-LIST bound to the symbols TEMP* and with STORE-VAR* bound to |
| 70 |
the symbols TEMP-S*. The doc-string DOC, if supplied, is saved as a |
| 71 |
SETF doc and can be retrieved by (documentation 'SYMBOL 'setf)." |
| 72 |
(let (function documentation stores) |
| 73 |
(if (and (car rest) (or (symbolp (car rest)) (functionp (car rest)))) |
| 74 |
(setq function `',(car rest) |
| 75 |
documentation (cadr rest) |
| 76 |
stores `(,(gensym))) |
| 77 |
(let* ((args (first rest)) |
| 78 |
(body (cddr rest)) |
| 79 |
decls) |
| 80 |
(multiple-value-setq (decls body documentation) |
| 81 |
(find-declarations body t)) |
| 82 |
(setq stores (second rest) |
| 83 |
function `#'(lambda (,@stores ,@args) |
| 84 |
,@decls |
| 85 |
(block ,access-fn ,@body))))) |
| 86 |
`(eval-when (:compile-toplevel :load-toplevel :execute) |
| 87 |
,(ext:register-with-pde whole `(do-defsetf ',access-fn ,function ,(length stores))) |
| 88 |
,@(si::expand-set-documentation access-fn 'setf documentation) |
| 89 |
',access-fn))) |
| 90 |
|
| 91 |
|
| 92 |
;;; DEFINE-SETF-METHOD macro. |
| 93 |
(defmacro define-setf-expander (access-fn args &rest body) |
| 94 |
"Syntax: (define-setf-expander symbol defmacro-lambda-list {decl | doc}* |
| 95 |
{form}*) |
| 96 |
Defines the SETF-method for generalized-variables (SYMBOL ...). |
| 97 |
When a form (setf (SYMBOL arg1 ... argn) value-form) is evaluated, the FORMs |
| 98 |
given in the DEFINE-SETF-EXPANDER are evaluated in order with the parameters in |
| 99 |
DEFMACRO-LAMBDA-LIST bound to ARG1 ... ARGn. The last FORM must return five |
| 100 |
values |
| 101 |
(var1 ... vark) |
| 102 |
(form1 ... formk) |
| 103 |
(value-var) |
| 104 |
storing-form |
| 105 |
access-form |
| 106 |
in order. These values are collectively called the five gangs of the |
| 107 |
generalized variable (SYMBOL arg1 ... argn). The whole SETF form is then |
| 108 |
expanded into |
| 109 |
(let* ((var1 from1) ... (vark formk) |
| 110 |
(value-var value-form)) |
| 111 |
storing-form) |
| 112 |
The doc-string DOC, if supplied, is saved as a SETF doc and can be retrieved |
| 113 |
by (DOCUMENTATION 'SYMBOL 'SETF)." |
| 114 |
(multiple-value-bind (function pprint documentation) |
| 115 |
(sys::expand-defmacro access-fn args body 'define-setf-expander) |
| 116 |
(declare (ignore pprint)) |
| 117 |
`(eval-when (:compile-toplevel :load-toplevel :execute) |
| 118 |
(do-define-setf-method ',access-fn #',function) |
| 119 |
,@(si::expand-set-documentation access-fn 'setf documentation) |
| 120 |
',access-fn))) |
| 121 |
|
| 122 |
|
| 123 |
;;;; get-setf-expansion. |
| 124 |
|
| 125 |
(defun get-setf-expansion (form &optional env &aux f) |
| 126 |
"Args: (form) |
| 127 |
Returns the 'five gangs' (see DEFINE-SETF-EXPANDER) for PLACE as five values. |
| 128 |
Does not check if the third gang is a single-element list." |
| 129 |
(declare (check-arguments-type nil)) |
| 130 |
;; Note that macroexpansion of SETF arguments can only be done via |
| 131 |
;; MACROEXPAND-1 [ANSI 5.1.2.7] |
| 132 |
(cond ((symbolp form) |
| 133 |
(if (and (setq f (macroexpand-1 form env)) (not (equal f form))) |
| 134 |
(get-setf-expansion f env) |
| 135 |
(let ((store (gensym))) |
| 136 |
(values nil nil (list store) `(setq ,form ,store) form)))) |
| 137 |
((or (not (consp form)) (not (symbolp (car form)))) |
| 138 |
(error "Cannot get the setf-method of ~S." form)) |
| 139 |
((setq f (get-sysprop (car form) 'SETF-METHOD)) |
| 140 |
(funcall f form env)) |
| 141 |
((and (setq f (macroexpand-1 form env)) (not (equal f form))) |
| 142 |
(get-setf-expansion f env)) |
| 143 |
(t |
| 144 |
(do-setf-method-expansion (car form) nil (cdr form))))) |
| 145 |
|
| 146 |
;;;; SETF definitions. |
| 147 |
|
| 148 |
(defsetf car (x) (y) `(progn (rplaca ,x ,y) ,y)) |
| 149 |
(defsetf cdr (x) (y) `(progn (rplacd ,x ,y), y)) |
| 150 |
(defsetf caar (x) (y) `(progn (rplaca (car ,x) ,y) ,y)) |
| 151 |
(defsetf cdar (x) (y) `(progn (rplacd (car ,x) ,y) ,y)) |
| 152 |
(defsetf cadr (x) (y) `(progn (rplaca (cdr ,x) ,y) ,y)) |
| 153 |
(defsetf cddr (x) (y) `(progn (rplacd (cdr ,x) ,y) ,y)) |
| 154 |
(defsetf caaar (x) (y) `(progn (rplaca (caar ,x) ,y) ,y)) |
| 155 |
(defsetf cdaar (x) (y) `(progn (rplacd (caar ,x) ,y) ,y)) |
| 156 |
(defsetf cadar (x) (y) `(progn (rplaca (cdar ,x) ,y) ,y)) |
| 157 |
(defsetf cddar (x) (y) `(progn (rplacd (cdar ,x) ,y) ,y)) |
| 158 |
(defsetf caadr (x) (y) `(progn (rplaca (cadr ,x) ,y) ,y)) |
| 159 |
(defsetf cdadr (x) (y) `(progn (rplacd (cadr ,x) ,y) ,y)) |
| 160 |
(defsetf caddr (x) (y) `(progn (rplaca (cddr ,x) ,y) ,y)) |
| 161 |
(defsetf cdddr (x) (y) `(progn (rplacd (cddr ,x) ,y) ,y)) |
| 162 |
(defsetf caaaar (x) (y) `(progn (rplaca (caaar ,x) ,y) ,y)) |
| 163 |
(defsetf cdaaar (x) (y) `(progn (rplacd (caaar ,x) ,y) ,y)) |
| 164 |
(defsetf cadaar (x) (y) `(progn (rplaca (cdaar ,x) ,y) ,y)) |
| 165 |
(defsetf cddaar (x) (y) `(progn (rplacd (cdaar ,x) ,y) ,y)) |
| 166 |
(defsetf caadar (x) (y) `(progn (rplaca (cadar ,x) ,y) ,y)) |
| 167 |
(defsetf cdadar (x) (y) `(progn (rplacd (cadar ,x) ,y) ,y)) |
| 168 |
(defsetf caddar (x) (y) `(progn (rplaca (cddar ,x) ,y) ,y)) |
| 169 |
(defsetf cdddar (x) (y) `(progn (rplacd (cddar ,x) ,y) ,y)) |
| 170 |
(defsetf caaadr (x) (y) `(progn (rplaca (caadr ,x) ,y) ,y)) |
| 171 |
(defsetf cdaadr (x) (y) `(progn (rplacd (caadr ,x) ,y) ,y)) |
| 172 |
(defsetf cadadr (x) (y) `(progn (rplaca (cdadr ,x) ,y) ,y)) |
| 173 |
(defsetf cddadr (x) (y) `(progn (rplacd (cdadr ,x) ,y) ,y)) |
| 174 |
(defsetf caaddr (x) (y) `(progn (rplaca (caddr ,x) ,y) ,y)) |
| 175 |
(defsetf cdaddr (x) (y) `(progn (rplacd (caddr ,x) ,y) ,y)) |
| 176 |
(defsetf cadddr (x) (y) `(progn (rplaca (cdddr ,x) ,y) ,y)) |
| 177 |
(defsetf cddddr (x) (y) `(progn (rplacd (cdddr ,x) ,y) ,y)) |
| 178 |
(defsetf first (x) (y) `(progn (rplaca ,x ,y) ,y)) |
| 179 |
(defsetf second (x) (y) `(progn (rplaca (cdr ,x) ,y) ,y)) |
| 180 |
(defsetf third (x) (y) `(progn (rplaca (cddr ,x) ,y) ,y)) |
| 181 |
(defsetf fourth (x) (y) `(progn (rplaca (cdddr ,x) ,y) ,y)) |
| 182 |
(defsetf fifth (x) (y) `(progn (rplaca (cddddr ,x) ,y) ,y)) |
| 183 |
(defsetf sixth (x) (y) `(progn (rplaca (nthcdr 5 ,x) ,y) ,y)) |
| 184 |
(defsetf seventh (x) (y) `(progn (rplaca (nthcdr 6 ,x) ,y) ,y)) |
| 185 |
(defsetf eighth (x) (y) `(progn (rplaca (nthcdr 7 ,x) ,y) ,y)) |
| 186 |
(defsetf ninth (x) (y) `(progn (rplaca (nthcdr 8 ,x) ,y) ,y)) |
| 187 |
(defsetf tenth (x) (y) `(progn (rplaca (nthcdr 9 ,x) ,y) ,y)) |
| 188 |
(defsetf rest (x) (y) `(progn (rplacd ,x ,y) ,y)) |
| 189 |
(defsetf svref sys:svset) |
| 190 |
(defsetf elt sys:elt-set) |
| 191 |
(defsetf symbol-value set) |
| 192 |
(defsetf symbol-function sys:fset) |
| 193 |
(defsetf fdefinition sys:fset) |
| 194 |
(defsetf macro-function (s &optional env) (v) (declare (ignore env)) `(sys:fset ,s ,v t)) |
| 195 |
(defsetf aref si::aset) |
| 196 |
(defsetf row-major-aref sys:row-major-aset) |
| 197 |
(defsetf get (s p &optional d) (v) |
| 198 |
(if d `(progn ,d (sys:putprop ,s ,v ,p)) `(sys:putprop ,s ,v ,p))) |
| 199 |
(defsetf get-sysprop put-sysprop) |
| 200 |
(defsetf nth (n l) (v) `(progn (rplaca (nthcdr ,n ,l) ,v) ,v)) |
| 201 |
(defsetf char sys:char-set) |
| 202 |
(defsetf schar sys:schar-set) |
| 203 |
(defsetf bit sys:aset) |
| 204 |
(defsetf sbit sys:aset) |
| 205 |
(defsetf fill-pointer sys:fill-pointer-set) |
| 206 |
(defsetf symbol-plist sys:set-symbol-plist) |
| 207 |
(defsetf gethash (k h &optional d) (v) (declare (ignore d)) `(sys:hash-set ,k ,h ,v)) |
| 208 |
#-clos |
| 209 |
(defsetf documentation sys::set-documentation) |
| 210 |
#+clos |
| 211 |
(defsetf sys:instance-ref sys:instance-set) |
| 212 |
(defsetf compiler-macro-function (fname) (function) |
| 213 |
`(sys::put-sysprop ,fname 'sys::compiler-macro ,function)) |
| 214 |
(defsetf readtable-case sys:readtable-case-set) |
| 215 |
(defsetf stream-external-format sys::stream-external-format-set) |
| 216 |
|
| 217 |
(define-setf-expander getf (&environment env place indicator |
| 218 |
&optional (default nil default-p)) |
| 219 |
(multiple-value-bind (vars vals stores store-form access-form) |
| 220 |
(get-setf-expansion place env) |
| 221 |
(let* ((itemp (gensym)) (store (gensym)) (def (gensym))) |
| 222 |
(values `(,@vars ,itemp ,@(if default-p (list def) nil)) |
| 223 |
`(,@vals ,indicator ,@(and default-p (list default))) |
| 224 |
`(,store) |
| 225 |
`(let ((,(car stores) (sys:put-f ,access-form ,store ,itemp))) |
| 226 |
,store-form |
| 227 |
,store) |
| 228 |
`(getf ,access-form ,itemp ,default))))) |
| 229 |
|
| 230 |
(defsetf subseq (sequence1 start1 &optional end1) |
| 231 |
(sequence2) |
| 232 |
`(PROGN (REPLACE ,sequence1 ,sequence2 :START1 ,start1 :END1 ,end1) |
| 233 |
,sequence2)) |
| 234 |
|
| 235 |
(define-setf-expander THE (&environment env type place) |
| 236 |
(multiple-value-bind (vars vals stores store-form access-form) |
| 237 |
(get-setf-expansion place env) |
| 238 |
(values vars vals stores |
| 239 |
(subst `(THE ,type ,(first stores)) (first stores) store-form) |
| 240 |
`(THE ,type ,access-form)))) |
| 241 |
|
| 242 |
#| |
| 243 |
(define-setf-expander apply (&environment env fn &rest rest) |
| 244 |
(unless (and (consp fn) (eq (car fn) 'FUNCTION) (symbolp (cadr fn)) |
| 245 |
(null (cddr fn))) |
| 246 |
(error "Can't get the setf-method of ~S." fn)) |
| 247 |
(multiple-value-bind (vars vals stores store-form access-form) |
| 248 |
(get-setf-expansion (cons (cadr fn) rest) env) |
| 249 |
(unless (eq (car (last store-form)) (car (last vars))) |
| 250 |
(error "Can't get the setf-method of ~S." fn)) |
| 251 |
(values vars vals stores |
| 252 |
`(apply #',(car store-form) ,@(cdr store-form)) |
| 253 |
`(apply #',(cadr fn) ,@(cdr access-form))))) |
| 254 |
|# |
| 255 |
|
| 256 |
(define-setf-expander apply (&environment env fn &rest rest) |
| 257 |
(unless (and (consp fn) |
| 258 |
(or (eq (car fn) 'FUNCTION) (eq (car fn) 'QUOTE)) |
| 259 |
(symbolp (cadr fn)) |
| 260 |
(null (cddr fn))) |
| 261 |
(error "Can't get the setf-method of ~S." fn)) |
| 262 |
(multiple-value-bind (vars vals stores store-form access-form) |
| 263 |
(get-setf-expansion (cons (cadr fn) rest) env) |
| 264 |
(cond ((eq (car (last store-form)) (car (last vars))) |
| 265 |
(values vars vals stores |
| 266 |
`(apply #',(car store-form) ,@(cdr store-form)) |
| 267 |
`(apply #',(cadr fn) ,@(cdr access-form)))) |
| 268 |
((eq (car (last (butlast store-form))) (car (last vars))) |
| 269 |
(values vars vals stores |
| 270 |
`(apply #',(car store-form) |
| 271 |
,@(cdr (butlast store-form 2)) |
| 272 |
(append ,(car (last (butlast store-form))) |
| 273 |
(list ,(car (last store-form))))) |
| 274 |
`(apply #',(cadr fn) ,@(cdr access-form)))) |
| 275 |
(t (error "Can't get the setf-method of ~S." fn))))) |
| 276 |
|
| 277 |
(define-setf-expander ldb (&environment env bytespec int) |
| 278 |
(multiple-value-bind (temps vals stores store-form access-form) |
| 279 |
(get-setf-expansion int env) |
| 280 |
(let* ((btemp (gensym)) |
| 281 |
(store (gensym)) |
| 282 |
(stemp (first stores))) |
| 283 |
(values `(,btemp ,@temps) |
| 284 |
`(,bytespec ,@vals) |
| 285 |
`(,store) |
| 286 |
`(let ((,stemp (dpb ,store ,btemp ,access-form))) |
| 287 |
,store-form ,store) |
| 288 |
`(ldb ,btemp ,access-form))))) |
| 289 |
|
| 290 |
(define-setf-expander mask-field (&environment env bytespec int) |
| 291 |
(multiple-value-bind (temps vals stores store-form access-form) |
| 292 |
(get-setf-expansion int env) |
| 293 |
(let* ((btemp (gensym)) |
| 294 |
(store (gensym)) |
| 295 |
(stemp (first stores))) |
| 296 |
(values `(,btemp ,@temps) |
| 297 |
`(,bytespec ,@vals) |
| 298 |
`(,store) |
| 299 |
`(let ((,stemp (deposit-field ,store ,btemp ,access-form))) |
| 300 |
,store-form ,store) |
| 301 |
`(mask-field ,btemp ,access-form))))) |
| 302 |
|
| 303 |
(defun trivial-setf-form (place vars stores store-form access-form) |
| 304 |
(declare (si::c-local) |
| 305 |
(optimize (speed 3) (safety 0))) |
| 306 |
(and (atom place) |
| 307 |
(null vars) |
| 308 |
(eq access-form place) |
| 309 |
(= (length stores) 1) |
| 310 |
(listp store-form) |
| 311 |
(= (length store-form) 3) |
| 312 |
(member (first store-form) '(setq setf)) |
| 313 |
(eq (second store-form) place) |
| 314 |
(eq (third store-form) (first stores)) |
| 315 |
)) |
| 316 |
|
| 317 |
(defun try-simpler-expansion (place vars vals stores newvalue store-form) |
| 318 |
;; When the store form contains all the original arguments in order |
| 319 |
;; followed by a single stored value, we can produce an expansion |
| 320 |
;; without LET forms. |
| 321 |
(declare (si::c-local) |
| 322 |
(optimize (speed 3) (safety 0))) |
| 323 |
(when (and (consp place) |
| 324 |
(consp store-form) |
| 325 |
(= (length place) (truly-the fixnum (1- (length store-form))))) |
| 326 |
(let ((function (pop store-form)) |
| 327 |
(output '()) |
| 328 |
v) |
| 329 |
(dolist (i (rest place) |
| 330 |
(when (eq (first stores) (first store-form)) |
| 331 |
(list* function |
| 332 |
(nreverse (cons newvalue output))))) |
| 333 |
(unless (consp store-form) |
| 334 |
(return nil)) |
| 335 |
(setq v (car (truly-the cons store-form)) |
| 336 |
store-form (cdr (truly-the cons store-form))) |
| 337 |
;; This checks that the argument at this position coincides with |
| 338 |
;; the corresponding value in the original list. Note that the |
| 339 |
;; variable list need not be in order. |
| 340 |
(unless (or (eq v i) |
| 341 |
(and (eq v (pop vars)) |
| 342 |
(eq (pop vals) i))) |
| 343 |
(return nil)) |
| 344 |
(push i output))))) |
| 345 |
|
| 346 |
;;; The expansion function for SETF. |
| 347 |
(defun setf-expand-1 (place newvalue env) |
| 348 |
(declare (si::c-local) |
| 349 |
(notinline mapcar)) |
| 350 |
(multiple-value-bind (vars vals stores store-form access-form) |
| 351 |
(get-setf-expansion place env) |
| 352 |
(cond ((trivial-setf-form place vars stores store-form access-form) |
| 353 |
(list 'setq place newvalue)) |
| 354 |
((try-simpler-expansion place vars vals stores newvalue store-form)) |
| 355 |
(t |
| 356 |
`(let* ,(mapcar #'list vars vals) |
| 357 |
;; Unfortunately we cannot do this because there is code out |
| 358 |
;; there that changes the variables and values |
| 359 |
;; (declare (:read-only ,@vars)) |
| 360 |
(multiple-value-bind ,stores ,newvalue |
| 361 |
;; Same here |
| 362 |
;; (declare (:read-only ,@stores)) |
| 363 |
,store-form)))))) |
| 364 |
|
| 365 |
(defun setf-expand (l env) |
| 366 |
(declare (si::c-local)) |
| 367 |
(cond ((endp l) nil) |
| 368 |
((endp (cdr l)) (error "~S is an illegal SETF form." l)) |
| 369 |
(t |
| 370 |
(cons (setf-expand-1 (car l) (cadr l) env) |
| 371 |
(setf-expand (cddr l) env))))) |
| 372 |
|
| 373 |
;;; SETF macro. |
| 374 |
(defmacro setf (&environment env &rest rest) |
| 375 |
"Syntax: (setf {place form}*) |
| 376 |
Evaluates each FORM and assigns the value to the corresponding PLACE in order. |
| 377 |
Returns the value of the last FORM. |
| 378 |
Each PLACE may be any one of the following: |
| 379 |
* A symbol that names a variable. |
| 380 |
* A function call form whose first element is the name of the following |
| 381 |
functions: |
| 382 |
nth elt subseq rest first ... tenth |
| 383 |
c?r c??r c???r c????r |
| 384 |
aref svref char schar bit sbit fill-pointer |
| 385 |
get getf documentation symbol-value symbol-function |
| 386 |
symbol-plist macro-function gethash fdefinition |
| 387 |
char-bit ldb mask-field |
| 388 |
apply slot-value |
| 389 |
where '?' stands for either 'a' or 'd'. |
| 390 |
* A function call form whose first element is: |
| 391 |
1. an access function for a structure slot |
| 392 |
2. an accessor method for a CLOS object |
| 393 |
* the form (THE type place) with PLACE being a place recognized by SETF. |
| 394 |
* a macro call which expands to a place recognized by SETF. |
| 395 |
* any form for which a DEFSETF or DEFINE-SETF-EXPANDER declaration has been |
| 396 |
made." |
| 397 |
(cond ((endp rest) nil) |
| 398 |
((endp (cdr rest)) (error "~S is an illegal SETF form." rest)) |
| 399 |
((endp (cddr rest)) (setf-expand-1 (car rest) (cadr rest) env)) |
| 400 |
(t (cons 'progn (setf-expand rest env))))) |
| 401 |
|
| 402 |
;;; PSTEF/SHIFTF/ROTATEF base macros |
| 403 |
|
| 404 |
;; WITH-SETF-EXPANSIONS |
| 405 |
(defmacro with-setf-expansions |
| 406 |
((pairs stores store-forms access-forms) |
| 407 |
(args &optional env) &body body) |
| 408 |
"Syntax: (with-setf-expansions (pairs stores store-forms access-forms) |
| 409 |
({place}* &optional env) {form}*) |
| 410 |
Map setf-expansions of places into list of bindings in PAIRS, list of |
| 411 |
stores in STORES, list of store-form in STORE-FORMS and list of |
| 412 |
access-form in ACCESS-FORMS. Creates new lexical scope composed of |
| 413 |
these variables." |
| 414 |
(declare (notinline mapcar)) |
| 415 |
`(destructuring-bind (,pairs ,stores ,store-forms ,access-forms) |
| 416 |
(reduce (lambda (x y) |
| 417 |
(list |
| 418 |
(cons (mapcar #'list (first y) (second y)) |
| 419 |
(first x)) |
| 420 |
(cons (third y) (second x)) |
| 421 |
(cons (fourth y) (third x)) |
| 422 |
(cons (fifth y) (fourth x)))) |
| 423 |
(mapcar (lambda (place) |
| 424 |
(multiple-value-list |
| 425 |
(get-setf-expansion place ,env))) |
| 426 |
(reverse ,args)) |
| 427 |
:initial-value '(nil nil nil nil)) |
| 428 |
,@body)) |
| 429 |
|
| 430 |
;;; PSETF macro. |
| 431 |
(defmacro psetf (&environment env &rest args) |
| 432 |
"Syntax: (psetf {place form}*) |
| 433 |
Similar to SETF, but evaluates all FORMs first, and then assigns each value to |
| 434 |
the corresponding PLACE. Returns NIL." |
| 435 |
(multiple-value-bind (places forms) |
| 436 |
(do* ((args args (cddr args)) |
| 437 |
places forms) |
| 438 |
((endp args) |
| 439 |
(values (nreverse places) |
| 440 |
(nreverse forms))) |
| 441 |
(when (endp (cdr args)) |
| 442 |
(error "~S is an illegal PSETF form (odd number of arguments)." args)) |
| 443 |
(push (car args) places) |
| 444 |
(push (cadr args) forms)) |
| 445 |
(with-setf-expansions (pairs stores store-forms access-forms) (places env) |
| 446 |
(declare (ignore access-forms)) |
| 447 |
(labels ((thunk (pairs stores access-forms) |
| 448 |
(if stores |
| 449 |
`((let ,(car pairs) |
| 450 |
(multiple-value-bind ,(car stores) ,(car access-forms) |
| 451 |
,@(thunk (cdr pairs) (cdr stores) (cdr access-forms))))) |
| 452 |
store-forms))) |
| 453 |
`(prog1 nil |
| 454 |
,@(thunk pairs stores forms)))))) |
| 455 |
|
| 456 |
;; WITH-EXPANSION-SETTER |
| 457 |
(defmacro with-expansion-setter ((name store-forms) &body body) |
| 458 |
"Syntax: (with-expansion-setter (name store-forms) {form}*) |
| 459 |
Macro creates lexically-scoped function NAME, which takes two lists as |
| 460 |
arguments, and expands into nested MULTIPLE-VALUE-BINDs of ACCESS-FORMS |
| 461 |
into STORES. Finally inside bindings it expands STORE-FORMS." |
| 462 |
`(labels ((,name (stores access-forms) |
| 463 |
(if stores |
| 464 |
`((multiple-value-bind ,(car stores) ,(car access-forms) |
| 465 |
,@(,name (cdr stores) (cdr access-forms)))) |
| 466 |
`(,@,store-forms)))) |
| 467 |
,@body)) |
| 468 |
|
| 469 |
;;; SHIFTF macro. |
| 470 |
(defmacro shiftf (&environment env &rest args) |
| 471 |
"Syntax: (shiftf {place}+ form) |
| 472 |
Saves the values of PLACE and FORM, and then assigns the value of each PLACE |
| 473 |
to the PLACE on its left. The rightmost PLACE gets the value of FORM. |
| 474 |
Returns the original value of the leftmost PLACE." |
| 475 |
(with-setf-expansions (pairs stores store-forms access-forms) |
| 476 |
((butlast args) env) |
| 477 |
(with-expansion-setter (thunk store-forms) |
| 478 |
`(let* ,(reduce #'append pairs) |
| 479 |
(multiple-value-prog1 ,(car access-forms) |
| 480 |
,@(thunk stores |
| 481 |
(append (cdr access-forms) |
| 482 |
(last args)))))))) |
| 483 |
|
| 484 |
;;; ROTATEF macro. |
| 485 |
(defmacro rotatef (&environment env &rest args) |
| 486 |
"Syntax: (rotatef {place}*) |
| 487 |
Saves the values of PLACEs, and then assigns to each PLACE the saved value of |
| 488 |
the PLACE to its right. The rightmost PLACE gets the value of the leftmost |
| 489 |
PLACE. Returns NIL." |
| 490 |
(with-setf-expansions (pairs stores store-forms access-forms) |
| 491 |
(args env) |
| 492 |
(with-expansion-setter (thunk store-forms) |
| 493 |
`(let* ,(reduce #'append pairs) |
| 494 |
,@(thunk stores |
| 495 |
(append (cdr access-forms) |
| 496 |
(list (car access-forms)))) |
| 497 |
nil)))) |
| 498 |
|
| 499 |
;;; DEFINE-MODIFY-MACRO macro, by Bruno Haible. |
| 500 |
(defmacro define-modify-macro (name lambdalist function &optional docstring) |
| 501 |
"Syntax: (define-modify-macro symbol lambda-list function-name [doc]) |
| 502 |
Defines a read-modify-write macro like INCF. The defined macro will expand |
| 503 |
a form (SYMBOL place form1 ... formn) into a form that in effect SETFs the |
| 504 |
value of (FUNCTION-NAME place arg1 ... argm) into PLACE, where ARG1 ... ARGm |
| 505 |
are parameters in LAMBDA-LIST which are bound to FORM1 ... FORMn. For |
| 506 |
example, INCF could be defined as |
| 507 |
(define-modify-macro incf (&optional (x 1)) +) |
| 508 |
The doc-string DOC, if supplied, is saved as a FUNCTION doc and can be |
| 509 |
retrieved by (DOCUMENTATION 'SYMBOL 'FUNCTION)." |
| 510 |
(let* ((varlist nil) |
| 511 |
(restvar nil)) |
| 512 |
(do* ((lambdalistr lambdalist (cdr lambdalistr)) |
| 513 |
(next)) |
| 514 |
((null lambdalistr)) |
| 515 |
(setq next (first lambdalistr)) |
| 516 |
(cond ((eq next '&OPTIONAL)) |
| 517 |
((eq next '&REST) |
| 518 |
(if (symbolp (second lambdalistr)) |
| 519 |
(setq restvar (second lambdalistr)) |
| 520 |
(error "In the definition of ~S: &REST variable ~S should be a symbol." |
| 521 |
name (second lambdalistr))) |
| 522 |
(if (null (cddr lambdalistr)) |
| 523 |
(return) |
| 524 |
(error "Only one variable is allowed after &REST, not ~S" |
| 525 |
lambdalistr))) |
| 526 |
((or (eq next '&KEY) (eq next '&ALLOW-OTHER-KEYS) (eq next '&AUX)) |
| 527 |
(error "Illegal in a DEFINE-MODIFY-MACRO lambda list: ~S" |
| 528 |
next)) |
| 529 |
((symbolp next) (push next varlist)) |
| 530 |
((and (listp next) (symbolp (first next))) |
| 531 |
(push (first next) varlist)) |
| 532 |
(t (error "lambda list may only contain symbols and lists, not ~S" |
| 533 |
next)))) |
| 534 |
(setq varlist (nreverse varlist)) |
| 535 |
`(DEFMACRO ,name (&ENVIRONMENT ENV %REFERENCE ,@lambdalist) |
| 536 |
,@(and docstring (list docstring)) |
| 537 |
(DECLARE (NOTINLINE MAPCAR)) |
| 538 |
(MULTIPLE-VALUE-BIND (VARS VALS STORES SETTER GETTER) |
| 539 |
(GET-SETF-EXPANSION %REFERENCE ENV) |
| 540 |
(LET ((ALL-VARS (MAPCAR #'(LAMBDA (V) (LIST (GENSYM) V)) (LIST* ,@varlist ,restvar)))) |
| 541 |
(IF (SYMBOLP GETTER) |
| 542 |
(SUBST (LIST* (QUOTE ,function) GETTER (MAPCAR #'CAR ALL-VARS)) |
| 543 |
(CAR STORES) |
| 544 |
`(LET* ,ALL-VARS |
| 545 |
(DECLARE (:READ-ONLY ,@(mapcar #'first all-vars))) |
| 546 |
,SETTER)) |
| 547 |
(DO ((D VARS (CDR D)) |
| 548 |
(V VALS (CDR V)) |
| 549 |
(LET-LIST NIL (CONS (LIST (CAR D) (CAR V)) LET-LIST))) |
| 550 |
((NULL D) |
| 551 |
(SETQ LET-LIST |
| 552 |
(LIST* |
| 553 |
(LIST |
| 554 |
(CAR STORES) |
| 555 |
(LIST* (QUOTE ,function) GETTER (MAPCAR #'CAR ALL-VARS))) |
| 556 |
(APPEND ALL-VARS LET-LIST))) |
| 557 |
`(LET* ,(NREVERSE LET-LIST) |
| 558 |
(DECLARE (:READ-ONLY ,@(mapcar #'first all-vars) |
| 559 |
,@vars)) |
| 560 |
,SETTER))))))))) |
| 561 |
|
| 562 |
#+(or) |
| 563 |
(defmacro define-modify-macro (name lambda-list function &optional doc-string) |
| 564 |
(let ((update-form |
| 565 |
(do ((l lambda-list (cdr l)) |
| 566 |
(vs nil)) |
| 567 |
((null l) `(list ',function access-form ,@(nreverse vs))) |
| 568 |
(unless (eq (car l) '&optional) |
| 569 |
(if (eq (car l) '&rest) |
| 570 |
(return `(list* ',function |
| 571 |
access-form |
| 572 |
,@(nreverse vs) |
| 573 |
,(cadr l)))) |
| 574 |
(if (symbolp (car l)) |
| 575 |
(setq vs (cons (car l) vs)) |
| 576 |
(setq vs (cons (caar l) vs))))))) |
| 577 |
`(defmacro ,name (&environment env reference . ,lambda-list) |
| 578 |
,@(if doc-string (list doc-string)) |
| 579 |
(when (symbolp reference) |
| 580 |
(return-from ,name |
| 581 |
(let ((access-form reference)) |
| 582 |
(list 'setq reference ,update-form)))) |
| 583 |
(multiple-value-bind (vars vals stores store-form access-form) |
| 584 |
(get-setf-expansion reference env) |
| 585 |
`(let* ,(mapcar #'list |
| 586 |
(append vars stores) |
| 587 |
(append vals (list ,update-form))) |
| 588 |
(declare (:read-only ,@stores)) ; Beppe |
| 589 |
,store-form))))) |
| 590 |
|
| 591 |
;;; Some macro definitions. |
| 592 |
|
| 593 |
(defmacro remf (&environment env place indicator) |
| 594 |
"Syntax: (remf place form) |
| 595 |
Removes the property specified by FORM from the property list stored in PLACE. |
| 596 |
Returns T if the property list had the specified property; NIL otherwise." |
| 597 |
(declare (notinline mapcar)) |
| 598 |
(multiple-value-bind (vars vals stores store-form access-form) |
| 599 |
(get-setf-expansion place env) |
| 600 |
(let ((s (gensym))) |
| 601 |
`(let* (,@(mapcar #'list vars vals) (,s ,indicator)) |
| 602 |
(declare (:read-only ,@vars)) ; Beppe |
| 603 |
(multiple-value-bind (,(car stores) flag) |
| 604 |
(sys:rem-f ,access-form ,s) |
| 605 |
,store-form |
| 606 |
flag))))) |
| 607 |
|
| 608 |
(define-modify-macro incf (&optional (delta 1)) + |
| 609 |
"Syntax: (incf place [form]) |
| 610 |
Increments the value of PLACE by the value of FORM. FORM defaults to 1.") |
| 611 |
|
| 612 |
(define-modify-macro decf (&optional (delta 1)) - |
| 613 |
"Syntax: (decf place [form]) |
| 614 |
Decrements the value of PLACE by the value of FORM. FORM defaults to 1.") |
| 615 |
|
| 616 |
(defmacro push (&environment env item place) |
| 617 |
"Syntax: (push form place) |
| 618 |
Evaluates FORM, conses the value of FORM to the value stored in PLACE, and |
| 619 |
makes it the new value of PLACE. Returns the new value of PLACE." |
| 620 |
(declare (notinline mapcar)) |
| 621 |
(multiple-value-bind (vars vals stores store-form access-form) |
| 622 |
(get-setf-expansion place env) |
| 623 |
(when (trivial-setf-form place vars stores store-form access-form) |
| 624 |
(return-from push `(setq ,place (cons ,item ,place)))) |
| 625 |
;; The item to be pushed has to be evaluated before the destination |
| 626 |
(unless (constantp item env) |
| 627 |
(setq vals (cons item vals) |
| 628 |
item (gensym) |
| 629 |
vars (cons item vars))) |
| 630 |
`(let* ,(mapcar #'list |
| 631 |
(append vars stores) |
| 632 |
(append vals (list (list 'cons item access-form)))) |
| 633 |
(declare (:read-only ,@vars)) ; Beppe |
| 634 |
,store-form))) |
| 635 |
|
| 636 |
(defmacro pushnew (&environment env item place &rest rest) |
| 637 |
"Syntax: (pushnew form place {keyword-form value-form}*) |
| 638 |
Evaluates FORM first. If the value is already in the list stored in PLACE, |
| 639 |
does nothing. Else, conses the value onto the list and makes the result the |
| 640 |
new value of PLACE. Returns NIL. KEYWORD-FORMs and VALUE-FORMs are used to |
| 641 |
check if the value of FORM is already in PLACE as if their values are passed |
| 642 |
to MEMBER." |
| 643 |
(declare (notinline mapcar)) |
| 644 |
(multiple-value-bind (vars vals stores store-form access-form) |
| 645 |
(get-setf-expansion place env) |
| 646 |
(when (trivial-setf-form place vars stores store-form access-form) |
| 647 |
(return-from pushnew `(setq ,place (adjoin ,item ,place ,@rest)))) |
| 648 |
;; The item to be pushed has to be evaluated before the destination |
| 649 |
(unless (constantp item env) |
| 650 |
(setq vals (cons item vals) |
| 651 |
item (gensym) |
| 652 |
vars (cons item vars))) |
| 653 |
`(let* ,(mapcar #'list |
| 654 |
(append vars stores) |
| 655 |
(append vals |
| 656 |
(list (list* 'adjoin item access-form rest)))) |
| 657 |
(declare (:read-only ,@vars)) ; Beppe |
| 658 |
,store-form))) |
| 659 |
|
| 660 |
(defmacro pop (&environment env place) |
| 661 |
"Syntax: (pop place) |
| 662 |
Gets the cdr of the value stored in PLACE and makes it the new value of PLACE. |
| 663 |
Returns the car of the old value in PLACE." |
| 664 |
(declare (notinline mapcar)) |
| 665 |
(multiple-value-bind (vars vals stores store-form access-form) |
| 666 |
(get-setf-expansion place env) |
| 667 |
(let ((store-var (first stores))) |
| 668 |
`(let* ,(mapcar #'list |
| 669 |
(append vars stores) |
| 670 |
(append vals (list access-form))) |
| 671 |
(declare (:read-only ,@vars)) ; Beppe |
| 672 |
(prog1 (car ,store-var) |
| 673 |
(setq ,store-var (cdr (truly-the list ,store-var))) |
| 674 |
,store-form))))) |
| 675 |
|
| 676 |
(define-setf-expander values (&rest values &environment env) |
| 677 |
(let ((all-vars '()) |
| 678 |
(all-vals '()) |
| 679 |
(all-stores '()) |
| 680 |
(all-storing-forms '()) |
| 681 |
(all-get-forms '())) |
| 682 |
(dolist (item (reverse values)) |
| 683 |
(multiple-value-bind (vars vals stores storing-form get-form) |
| 684 |
(get-setf-expansion item env) |
| 685 |
;; If a place has more than one store variable, the other ones |
| 686 |
;; are set to nil. |
| 687 |
(let ((extra (rest stores))) |
| 688 |
(unless (endp extra) |
| 689 |
(setf vars (append extra vars) |
| 690 |
vals (append (make-list (length extra)) vals) |
| 691 |
stores (list (first stores))))) |
| 692 |
(setf all-vars (append vars all-vars) |
| 693 |
all-vals (append vals all-vals) |
| 694 |
all-stores (append stores all-stores) |
| 695 |
all-storing-forms (cons storing-form all-storing-forms) |
| 696 |
all-get-forms (cons get-form all-get-forms)))) |
| 697 |
(values all-vars all-vals all-stores `(values ,@all-storing-forms) |
| 698 |
`(values ,@all-get-forms)))) |
| 699 |
#| |
| 700 |
;;; Proposed extension: |
| 701 |
; Expansion of (SETF (VALUES place1 ... placek) form) |
| 702 |
; --> (MULTIPLE-VALUE-BIND (dummy1 ... dummyk) form |
| 703 |
; (SETF place1 dummy1 ... placek dummyk) |
| 704 |
; (VALUES dummy1 ... dummyk)) |
| 705 |
(define-setf-expander VALUES (&environment env &rest subplaces) |
| 706 |
(do ((temps) (vals) (stores) |
| 707 |
(storeforms) (accessforms) |
| 708 |
(placesr subplaces)) |
| 709 |
((atom placesr) |
| 710 |
(setq temps (nreverse temps) |
| 711 |
vals (nreverse vals) |
| 712 |
stores (nreverse stores) |
| 713 |
storeforms (nreverse storeforms) |
| 714 |
accessforms (nreverse accessforms)) |
| 715 |
(values temps |
| 716 |
vals |
| 717 |
stores |
| 718 |
`(VALUES ,@storeforms) |
| 719 |
`(VALUES ,@accessforms))) |
| 720 |
(multiple-value-bind (SM1 SM2 SM3 SM4 SM5) |
| 721 |
(get-setf-expansion (pop placesr) env) |
| 722 |
(setq temps (revappend SM1 temps) |
| 723 |
vals (revappend SM2 vals) |
| 724 |
stores (revappend SM3 stores) |
| 725 |
storeforms (cons SM4 storeforms) |
| 726 |
accessforms (cons SM5 accessforms))))) |
| 727 |
|# |