1 | ;;; setf.lisp |
---|
2 | ;;; |
---|
3 | ;;; Copyright (C) 2003-2006 Peter Graves |
---|
4 | ;;; $Id: setf.lisp 12516 2010-03-03 21:05:41Z astalla $ |
---|
5 | ;;; |
---|
6 | ;;; This program is free software; you can redistribute it and/or |
---|
7 | ;;; modify it under the terms of the GNU General Public License |
---|
8 | ;;; as published by the Free Software Foundation; either version 2 |
---|
9 | ;;; of the License, or (at your option) any later version. |
---|
10 | ;;; |
---|
11 | ;;; This program is distributed in the hope that it will be useful, |
---|
12 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | ;;; GNU General Public License for more details. |
---|
15 | ;;; |
---|
16 | ;;; You should have received a copy of the GNU General Public License |
---|
17 | ;;; along with this program; if not, write to the Free Software |
---|
18 | ;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
19 | ;;; |
---|
20 | ;;; As a special exception, the copyright holders of this library give you |
---|
21 | ;;; permission to link this library with independent modules to produce an |
---|
22 | ;;; executable, regardless of the license terms of these independent |
---|
23 | ;;; modules, and to copy and distribute the resulting executable under |
---|
24 | ;;; terms of your choice, provided that you also meet, for each linked |
---|
25 | ;;; independent module, the terms and conditions of the license of that |
---|
26 | ;;; module. An independent module is a module which is not derived from |
---|
27 | ;;; or based on this library. If you modify this library, you may extend |
---|
28 | ;;; this exception to your version of the library, but you are not |
---|
29 | ;;; obligated to do so. If you do not wish to do so, delete this |
---|
30 | ;;; exception statement from your version. |
---|
31 | |
---|
32 | (in-package #:system) |
---|
33 | |
---|
34 | (defun get-setf-method-inverse (form inverse setf-function) |
---|
35 | (let ((new-var (gensym)) |
---|
36 | (vars nil) |
---|
37 | (vals nil)) |
---|
38 | (dolist (x (cdr form)) |
---|
39 | (push (gensym) vars) |
---|
40 | (push x vals)) |
---|
41 | (setq vals (nreverse vals)) |
---|
42 | (values vars vals (list new-var) |
---|
43 | (if setf-function |
---|
44 | `(,@inverse ,new-var ,@vars) |
---|
45 | (if (functionp (car inverse)) |
---|
46 | `(funcall ,@inverse ,@vars ,new-var) |
---|
47 | `(,@inverse ,@vars ,new-var))) |
---|
48 | `(,(car form) ,@vars)))) |
---|
49 | |
---|
50 | ;;; If a macro, expand one level and try again. If not, go for the |
---|
51 | ;;; SETF function. |
---|
52 | (defun expand-or-get-setf-inverse (form environment) |
---|
53 | (multiple-value-bind (expansion expanded) |
---|
54 | (macroexpand-1 form environment) |
---|
55 | (if expanded |
---|
56 | (get-setf-expansion expansion environment) |
---|
57 | (get-setf-method-inverse form `(funcall #'(setf ,(car form))) |
---|
58 | t)))) |
---|
59 | |
---|
60 | (defun get-setf-expansion (form &optional environment) |
---|
61 | (when (and (consp form) |
---|
62 | (autoloadp (%car form))) |
---|
63 | (resolve (%car form))) |
---|
64 | (let (temp) |
---|
65 | (cond ((symbolp form) |
---|
66 | (multiple-value-bind (expansion expanded) |
---|
67 | (macroexpand-1 form environment) |
---|
68 | (if expanded |
---|
69 | (get-setf-expansion expansion environment) |
---|
70 | (let ((new-var (gensym))) |
---|
71 | (values nil nil (list new-var) |
---|
72 | `(setq ,form ,new-var) form))))) |
---|
73 | ((setq temp (get (car form) 'setf-inverse)) |
---|
74 | (get-setf-method-inverse form `(,temp) nil)) |
---|
75 | ((setq temp (get (car form) 'setf-expander)) |
---|
76 | (funcall temp form environment)) |
---|
77 | (t |
---|
78 | (expand-or-get-setf-inverse form environment))))) |
---|
79 | |
---|
80 | (defmacro setf (&rest args &environment environment) |
---|
81 | (let ((numargs (length args))) |
---|
82 | (cond |
---|
83 | ((= numargs 2) |
---|
84 | (let ((place (first args)) |
---|
85 | (value-form (second args))) |
---|
86 | (if (atom place) |
---|
87 | `(setq ,place ,value-form) |
---|
88 | (progn |
---|
89 | (when (symbolp (%car place)) |
---|
90 | (resolve (%car place))) |
---|
91 | (multiple-value-bind (dummies vals store-vars setter getter) |
---|
92 | (get-setf-expansion place environment) |
---|
93 | (let ((inverse (get (car place) 'setf-inverse))) |
---|
94 | (if (and inverse (eq inverse (car setter))) |
---|
95 | (if (functionp inverse) |
---|
96 | `(funcall ,inverse ,@(cdr place) ,value-form) |
---|
97 | `(,inverse ,@(cdr place) ,value-form)) |
---|
98 | (if (or (null store-vars) (cdr store-vars)) |
---|
99 | `(let* (,@(mapcar #'list dummies vals)) |
---|
100 | (multiple-value-bind ,store-vars ,value-form |
---|
101 | ,setter)) |
---|
102 | `(let* (,@(mapcar #'list dummies vals) |
---|
103 | ,(list (car store-vars) value-form)) |
---|
104 | ,setter))))))))) |
---|
105 | ((oddp numargs) |
---|
106 | (error "Odd number of arguments to SETF.")) |
---|
107 | (t |
---|
108 | (do ((a args (cddr a)) (l nil)) |
---|
109 | ((null a) `(progn ,@(nreverse l))) |
---|
110 | (setq l (cons (list 'setf (car a) (cadr a)) l))))))) |
---|
111 | |
---|
112 | ;;; Redefined in define-modify-macro.lisp. |
---|
113 | (defmacro incf (place &optional (delta 1)) |
---|
114 | `(setf ,place (+ ,place ,delta))) |
---|
115 | |
---|
116 | ;;; Redefined in define-modify-macro.lisp. |
---|
117 | (defmacro decf (place &optional (delta 1)) |
---|
118 | `(setf ,place (- ,place ,delta))) |
---|
119 | |
---|
120 | ;; (defsetf subseq (sequence start &optional (end nil)) (v) |
---|
121 | ;; `(progn (replace ,sequence ,v :start1 ,start :end1 ,end) |
---|
122 | ;; ,v)) |
---|
123 | (defun %set-subseq (sequence start &rest rest) |
---|
124 | (let ((end nil) v) |
---|
125 | (ecase (length rest) |
---|
126 | (1 |
---|
127 | (setq v (car rest))) |
---|
128 | (2 |
---|
129 | (setq end (car rest) |
---|
130 | v (cadr rest)))) |
---|
131 | (progn |
---|
132 | (replace sequence v :start1 start :end1 end) |
---|
133 | v))) |
---|
134 | |
---|
135 | (defun %define-setf-macro (name expander inverse doc) |
---|
136 | (declare (ignore doc)) ; FIXME |
---|
137 | (when inverse |
---|
138 | (put name 'setf-inverse inverse)) |
---|
139 | (when expander |
---|
140 | (put name 'setf-expander expander)) |
---|
141 | name) |
---|
142 | |
---|
143 | (defmacro defsetf (access-function update-function) |
---|
144 | `(eval-when (:load-toplevel :compile-toplevel :execute) |
---|
145 | (put ',access-function 'setf-inverse ',update-function))) |
---|
146 | |
---|
147 | (defun %set-caar (x v) (set-car (car x) v)) |
---|
148 | (defun %set-cadr (x v) (set-car (cdr x) v)) |
---|
149 | (defun %set-cdar (x v) (set-cdr (car x) v)) |
---|
150 | (defun %set-cddr (x v) (set-cdr (cdr x) v)) |
---|
151 | (defun %set-caaar (x v) (set-car (caar x) v)) |
---|
152 | (defun %set-cadar (x v) (set-car (cdar x) v)) |
---|
153 | (defun %set-cdaar (x v) (set-cdr (caar x) v)) |
---|
154 | (defun %set-cddar (x v) (set-cdr (cdar x) v)) |
---|
155 | (defun %set-caadr (x v) (set-car (cadr x) v)) |
---|
156 | (defun %set-caddr (x v) (set-car (cddr x) v)) |
---|
157 | (defun %set-cdadr (x v) (set-cdr (cadr x) v)) |
---|
158 | (defun %set-cdddr (x v) (set-cdr (cddr x) v)) |
---|
159 | (defun %set-caaaar (x v) (set-car (caaar x) v)) |
---|
160 | (defun %set-cadaar (x v) (set-car (cdaar x) v)) |
---|
161 | (defun %set-cdaaar (x v) (set-cdr (caaar x) v)) |
---|
162 | (defun %set-cddaar (x v) (set-cdr (cdaar x) v)) |
---|
163 | (defun %set-caadar (x v) (set-car (cadar x) v)) |
---|
164 | (defun %set-caddar (x v) (set-car (cddar x) v)) |
---|
165 | (defun %set-cdadar (x v) (set-cdr (cadar x) v)) |
---|
166 | (defun %set-cdddar (x v) (set-cdr (cddar x) v)) |
---|
167 | (defun %set-caaadr (x v) (set-car (caadr x) v)) |
---|
168 | (defun %set-cadadr (x v) (set-car (cdadr x) v)) |
---|
169 | (defun %set-cdaadr (x v) (set-cdr (caadr x) v)) |
---|
170 | (defun %set-cddadr (x v) (set-cdr (cdadr x) v)) |
---|
171 | (defun %set-caaddr (x v) (set-car (caddr x) v)) |
---|
172 | (defun %set-cadddr (x v) (set-car (cdddr x) v)) |
---|
173 | (defun %set-cdaddr (x v) (set-cdr (caddr x) v)) |
---|
174 | (defun %set-cddddr (x v) (set-cdr (cdddr x) v)) |
---|
175 | |
---|
176 | (defsetf car set-car) |
---|
177 | (defsetf cdr set-cdr) |
---|
178 | (defsetf caar %set-caar) |
---|
179 | (defsetf cadr %set-cadr) |
---|
180 | (defsetf cdar %set-cdar) |
---|
181 | (defsetf cddr %set-cddr) |
---|
182 | (defsetf caaar %set-caaar) |
---|
183 | (defsetf cadar %set-cadar) |
---|
184 | (defsetf cdaar %set-cdaar) |
---|
185 | (defsetf cddar %set-cddar) |
---|
186 | (defsetf caadr %set-caadr) |
---|
187 | (defsetf caddr %set-caddr) |
---|
188 | (defsetf cdadr %set-cdadr) |
---|
189 | (defsetf cdddr %set-cdddr) |
---|
190 | (defsetf caaaar %set-caaaar) |
---|
191 | (defsetf cadaar %set-cadaar) |
---|
192 | (defsetf cdaaar %set-cdaaar) |
---|
193 | (defsetf cddaar %set-cddaar) |
---|
194 | (defsetf caadar %set-caadar) |
---|
195 | (defsetf caddar %set-caddar) |
---|
196 | (defsetf cdadar %set-cdadar) |
---|
197 | (defsetf cdddar %set-cdddar) |
---|
198 | (defsetf caaadr %set-caaadr) |
---|
199 | (defsetf cadadr %set-cadadr) |
---|
200 | (defsetf cdaadr %set-cdaadr) |
---|
201 | (defsetf cddadr %set-cddadr) |
---|
202 | (defsetf caaddr %set-caaddr) |
---|
203 | (defsetf cadddr %set-cadddr) |
---|
204 | (defsetf cdaddr %set-cdaddr) |
---|
205 | (defsetf cddddr %set-cddddr) |
---|
206 | |
---|
207 | (defsetf first set-car) |
---|
208 | (defsetf second %set-cadr) |
---|
209 | (defsetf third %set-caddr) |
---|
210 | (defsetf fourth %set-cadddr) |
---|
211 | (defun %set-fifth (x v) (set-car (cddddr x) v)) |
---|
212 | (defsetf fifth %set-fifth) |
---|
213 | (defun %set-sixth (x v) (set-car (cdr (cddddr x)) v)) |
---|
214 | (defsetf sixth %set-sixth) |
---|
215 | (defun %set-seventh (x v) (set-car (cddr (cddddr x)) v)) |
---|
216 | (defsetf seventh %set-seventh) |
---|
217 | (defun %set-eighth (x v) (set-car (cdddr (cddddr x)) v)) |
---|
218 | (defsetf eighth %set-eighth) |
---|
219 | (defun %set-ninth (x v) (set-car (cddddr (cddddr x)) v)) |
---|
220 | (defsetf ninth %set-ninth) |
---|
221 | (defun %set-tenth (x v) (set-car (cdr (cddddr (cddddr x))) v)) |
---|
222 | (defsetf tenth %set-tenth) |
---|
223 | |
---|
224 | (defsetf rest set-cdr) |
---|
225 | ;;Redefined in extensible-sequences-base.lisp |
---|
226 | (defsetf elt %set-elt) |
---|
227 | (defsetf nth %set-nth) |
---|
228 | (defsetf svref svset) |
---|
229 | (defsetf fill-pointer %set-fill-pointer) |
---|
230 | (defsetf subseq %set-subseq) |
---|
231 | (defsetf symbol-value set) |
---|
232 | (defsetf symbol-function %set-symbol-function) |
---|
233 | (defsetf symbol-plist %set-symbol-plist) |
---|
234 | (defsetf get put) |
---|
235 | (defsetf gethash puthash) |
---|
236 | (defsetf char set-char) |
---|
237 | (defsetf schar set-schar) |
---|
238 | (defsetf logical-pathname-translations %set-logical-pathname-translations) |
---|
239 | (defsetf readtable-case %set-readtable-case) |
---|
240 | |
---|
241 | (defsetf function-info %set-function-info) |
---|
242 | |
---|
243 | (defsetf stream-external-format %set-stream-external-format) |
---|