Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
;;;; -*- Mode: Lisp; Syntax: Common-Lisp; Package: SYSTEM -*-
;;;;
;;;; Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya.
;;;; Copyright (c) 1990, Giuseppe Attardi.
;;;;
;;;; This program is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 3 of the License, or (at your option) any later version.
;;;;
;;;; See file '../../Copyright' for full details.
;;;; setf routines
(in-package "SYSTEM")
(defun check-stores-number (context stores-list n)
(unless (= (length stores-list) n)
(error "~d store-variables expected in setf form ~a." n context)))
;;; DEFSETF macro.
(defmacro defsetf (access-fn &rest rest)
"Syntax: (defsetf symbol update-fun [doc])
or
(defsetf symbol lambda-list (store-var) {decl | doc}* {form}*)
Defines an expansion
(setf (SYMBOL arg1 ... argn) value)
=> (UPDATE-FUN arg1 ... argn value)
or
(let* ((temp1 ARG1) ... (tempn ARGn) (temp0 value)) rest)
where REST is the value of the last FORM with parameters in LAMBDA-LIST bound
to the symbols TEMP1 ... TEMPn and with STORE-VAR bound to the symbol TEMP0.
The doc-string DOC, if supplied, is saved as a SETF doc and can be retrieved
by (documentation 'SYMBOL 'setf)."
(cond ((and (car rest) (or (symbolp (car rest)) (functionp (car rest))))
`(define-when (compile load eval)
(put-sysprop ',access-fn 'SETF-UPDATE-FN ',(car rest))
(rem-sysprop ',access-fn 'SETF-LAMBDA)
(rem-sysprop ',access-fn 'SETF-METHOD)
(rem-sysprop ',access-fn 'SETF-SYMBOL)
,@(si::expand-set-documentation access-fn 'setf (cadr rest))
',access-fn))
(t
(let* ((store (second rest))
(args (first rest))
(body+ (cddr rest))
)
(multiple-value-bind (decls body doc)
(process-declarations body+ t)
(check-stores-number 'DEFSETF store 1)
`(define-when (compile load eval)
(put-sysprop ',access-fn 'SETF-LAMBDA #'(lambda (,@store ,@args) (declare ,@decls) (block ,access-fn ,@body)))
(rem-sysprop ',access-fn 'SETF-UPDATE-FN)
(rem-sysprop ',access-fn 'SETF-METHOD)
(rem-sysprop ',access-fn 'SETF-SYMBOL)
,@(si::expand-set-documentation access-fn 'setf doc)
',access-fn))))))
;;; DEFINE-SETF-METHOD macro.
(defmacro define-setf-expander (access-fn args &rest body+)
"Syntax: (define-setf-expander symbol defmacro-lambda-list {decl | doc}*
{form}*)
Defines the SETF-method for generalized-variables (SYMBOL ...).
When a form (setf (SYMBOL arg1 ... argn) value-form) is evaluated, the FORMs
given in the DEFINE-SETF-EXPANDER are evaluated in order with the parameters in
DEFMACRO-LAMBDA-LIST bound to ARG1 ... ARGn. The last FORM must return five
values
(var1 ... vark)
(form1 ... formk)
(value-var)
storing-form
access-form
in order. These values are collectively called the five gangs of the
generalized variable (SYMBOL arg1 ... argn). The whole SETF form is then
expanded into
(let* ((var1 from1) ... (vark formk)
(value-var value-form))
storing-form)
The doc-string DOC, if supplied, is saved as a SETF doc and can be retrieved
by (DOCUMENTATION 'SYMBOL 'SETF)."
(let ((env (member '&environment args :test #'eq)))
(if env
(setq args (cons (second env)
(nconc (ldiff args env) (cddr env))))
(progn
(setq env (gensym))
(setq args (cons env args))
(push `(declare (ignore ,env)) body+))))
(multiple-value-bind (decls body doc)
(process-declarations body+ t)
`(define-when (compile load eval)
(put-sysprop ',access-fn 'SETF-METHOD #'(lambda ,args (declare ,@decls) (block ,access-fn ,@body)))
(rem-sysprop ',access-fn 'SETF-LAMBDA)
(rem-sysprop ',access-fn 'SETF-UPDATE-FN)
(rem-sysprop ',access-fn 'SETF-SYMBOL)
,@(si::expand-set-documentation access-fn 'setf doc)
',access-fn)))
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
;;;; get-setf-expansion.
(defun get-setf-expansion (form &optional env &aux f)
"Args: (form)
Returns the 'five gangs' (see DEFINE-SETF-EXPANDER) for PLACE as five values.
Does not check if the third gang is a single-element list."
(flet ((rename-arguments (vars &aux names values all-args)
(dolist (item vars)
(unless (or (fixnump item) (keywordp item))
(push item values)
(setq item (gensym))
(push item names))
(push item all-args))
(values (gensym) (nreverse names) (nreverse values) (nreverse all-args))))
;; Note that macroexpansion of SETF arguments can only be done via
;; MACROEXPAND-1 [ANSI 5.1.2.7]
(cond ((symbolp form)
(if (and (setq f (macroexpand-1 form env)) (not (equal f form)))
(get-setf-expansion f env)
(let ((store (gensym)))
(values nil nil (list store) `(setq ,form ,store) form))))
((or (not (consp form)) (not (symbolp (car form))))
(error "Cannot get the setf-method of ~S." form))
((setq f (get-sysprop (car form) 'SETF-METHOD))
(apply f env (cdr form)))
(t
(let* ((name (car form)) writer)
(multiple-value-bind (store vars inits all)
(rename-arguments (cdr form))
(setq writer
(cond ((setq f (get-sysprop name 'SETF-UPDATE-FN))
`(,f ,@all ,store))
((setq f (get-sysprop name 'STRUCTURE-ACCESS))
(setf-structure-access (car all) (car f) (cdr f) store))
((setq f (get-sysprop (car form) 'SETF-LAMBDA))
(apply f store all))
((and (setq f (macroexpand-1 form env)) (not (equal f form)))
(return-from get-setf-expansion
(get-setf-expansion f env)))
(t
`(funcall #'(SETF ,name) ,store ,@all))))
(values vars inits (list store) writer (cons name all))))))))
;;;; SETF definitions.
(defsetf car (x) (y) `(progn (rplaca ,x ,y) ,y))
(defsetf cdr (x) (y) `(progn (rplacd ,x ,y), y))
(defsetf caar (x) (y) `(progn (rplaca (car ,x) ,y) ,y))
(defsetf cdar (x) (y) `(progn (rplacd (car ,x) ,y) ,y))
(defsetf cadr (x) (y) `(progn (rplaca (cdr ,x) ,y) ,y))
(defsetf cddr (x) (y) `(progn (rplacd (cdr ,x) ,y) ,y))
(defsetf caaar (x) (y) `(progn (rplaca (caar ,x) ,y) ,y))
(defsetf cdaar (x) (y) `(progn (rplacd (caar ,x) ,y) ,y))
(defsetf cadar (x) (y) `(progn (rplaca (cdar ,x) ,y) ,y))
(defsetf cddar (x) (y) `(progn (rplacd (cdar ,x) ,y) ,y))
(defsetf caadr (x) (y) `(progn (rplaca (cadr ,x) ,y) ,y))
(defsetf cdadr (x) (y) `(progn (rplacd (cadr ,x) ,y) ,y))
(defsetf caddr (x) (y) `(progn (rplaca (cddr ,x) ,y) ,y))
(defsetf cdddr (x) (y) `(progn (rplacd (cddr ,x) ,y) ,y))
(defsetf caaaar (x) (y) `(progn (rplaca (caaar ,x) ,y) ,y))
(defsetf cdaaar (x) (y) `(progn (rplacd (caaar ,x) ,y) ,y))
(defsetf cadaar (x) (y) `(progn (rplaca (cdaar ,x) ,y) ,y))
(defsetf cddaar (x) (y) `(progn (rplacd (cdaar ,x) ,y) ,y))
(defsetf caadar (x) (y) `(progn (rplaca (cadar ,x) ,y) ,y))
(defsetf cdadar (x) (y) `(progn (rplacd (cadar ,x) ,y) ,y))
(defsetf caddar (x) (y) `(progn (rplaca (cddar ,x) ,y) ,y))
(defsetf cdddar (x) (y) `(progn (rplacd (cddar ,x) ,y) ,y))
(defsetf caaadr (x) (y) `(progn (rplaca (caadr ,x) ,y) ,y))
(defsetf cdaadr (x) (y) `(progn (rplacd (caadr ,x) ,y) ,y))
(defsetf cadadr (x) (y) `(progn (rplaca (cdadr ,x) ,y) ,y))
(defsetf cddadr (x) (y) `(progn (rplacd (cdadr ,x) ,y) ,y))
(defsetf caaddr (x) (y) `(progn (rplaca (caddr ,x) ,y) ,y))
(defsetf cdaddr (x) (y) `(progn (rplacd (caddr ,x) ,y) ,y))
(defsetf cadddr (x) (y) `(progn (rplaca (cdddr ,x) ,y) ,y))
(defsetf cddddr (x) (y) `(progn (rplacd (cdddr ,x) ,y) ,y))
(defsetf first (x) (y) `(progn (rplaca ,x ,y) ,y))
(defsetf second (x) (y) `(progn (rplaca (cdr ,x) ,y) ,y))
(defsetf third (x) (y) `(progn (rplaca (cddr ,x) ,y) ,y))
(defsetf fourth (x) (y) `(progn (rplaca (cdddr ,x) ,y) ,y))
(defsetf fifth (x) (y) `(progn (rplaca (cddddr ,x) ,y) ,y))
(defsetf sixth (x) (y) `(progn (rplaca (nthcdr 5 ,x) ,y) ,y))
(defsetf seventh (x) (y) `(progn (rplaca (nthcdr 6 ,x) ,y) ,y))
(defsetf eighth (x) (y) `(progn (rplaca (nthcdr 7 ,x) ,y) ,y))
(defsetf ninth (x) (y) `(progn (rplaca (nthcdr 8 ,x) ,y) ,y))
(defsetf tenth (x) (y) `(progn (rplaca (nthcdr 9 ,x) ,y) ,y))
(defsetf rest (x) (y) `(progn (rplacd ,x ,y) ,y))
(defsetf svref sys:svset)
(defsetf elt sys:elt-set)
(defsetf symbol-value set)
(defsetf symbol-function sys:fset)
(defsetf fdefinition sys:fset)
(defsetf macro-function (s &optional env) (v) (declare (ignore env)) `(sys:fset ,s ,v t))
(defsetf aref (a &rest il) (v) `(sys:aset ,v ,a ,@il))
(defsetf row-major-aref (a i) (v) `(sys:row-major-aset ,a ,i ,v))
(defsetf get (s p &optional d) (v)
(if d `(progn ,d (sys:putprop ,s ,v ,p)) `(sys:putprop ,s ,v ,p)))
(defsetf get-sysprop put-sysprop)
(defsetf nth (n l) (v) `(progn (rplaca (nthcdr ,n ,l) ,v) ,v))
(defsetf char sys:char-set)
(defsetf schar sys:schar-set)
(defsetf bit (a &rest il) (v) `(sys:bit-set ,v ,a ,@il))
(defsetf sbit (a &rest il) (v) `(sys:sbit-set ,v ,a ,@il))
(defsetf fill-pointer sys:fill-pointer-set)
(defsetf symbol-plist sys:set-symbol-plist)
(defsetf gethash (k h &optional d) (v) (declare (ignore d)) `(sys:hash-set ,k ,h ,v))
#|
#-clos
(defsetf documentation (s d) (v) `(sys::set-documentation ,s ,d ,v))
|#
(defsetf sys:instance-ref sys:instance-set)
(defsetf compiler-macro-function (fname) (function)
`(sys::put-sysprop ,fname 'sys::compiler-macro ,function))
(defsetf readtable-case sys:readtable-case-set)
(defsetf stream-external-format si:stream-external-format-set)
(defsetf si:closure-level-var si:closure-level-set-var)
(define-setf-expander getf (&environment env place indicator &optional default)
(multiple-value-bind (vars vals stores store-form access-form)
(get-setf-expansion place env)
(let* ((itemp (gensym)) (store (gensym)) (def (gensym)))
(values `(,@vars ,itemp ,def)
`(,@vals ,indicator ,default)
`(,store)
`(let ((,(car stores) (sys:put-f ,access-form ,store ,itemp)))
,store-form
,store)
`(getf ,access-form ,itemp ,default)))))
(defsetf subseq (sequence1 start1 &optional end1)
(sequence2)
`(PROGN (REPLACE ,sequence1 ,sequence2 :START1 ,start1 :END1 ,end1)
,sequence2))
(define-setf-expander THE (&environment env type place)
(multiple-value-bind (vars vals stores store-form access-form)
(get-setf-expansion place env)
(values vars vals stores
(subst `(THE ,type ,(first stores)) (first stores) store-form)
`(THE ,type ,access-form))))
(define-setf-expander apply (&environment env fn &rest rest)
(unless (and (consp fn)
(or (eq (car fn) 'FUNCTION) (eq (car fn) 'QUOTE))
(symbolp (cadr fn))
(null (cddr fn)))
(error "Can't get the setf-method of ~S." fn))
(multiple-value-bind (vars vals stores store-form access-form)
(get-setf-expansion (cons (cadr fn) rest) env)
(cond ((eq (car (last store-form)) (car (last vars)))
(values vars vals stores
`(apply #',(car store-form) ,@(cdr store-form))
`(apply #',(cadr fn) ,@(cdr access-form))))
((eq (car (last (butlast store-form))) (car (last vars)))
(values vars vals stores
`(apply #',(car store-form)
,@(cdr (butlast store-form 2))
(append ,(car (last (butlast store-form)))
(list ,(car (last store-form)))))
`(apply #',(cadr fn) ,@(cdr access-form))))
(t (error "Can't get the setf-method of ~S." fn)))))
(define-setf-expander ldb (&environment env bytespec int)
(multiple-value-bind (temps vals stores store-form access-form)
(get-setf-expansion int env)
(let* ((btemp (gensym))
(store (gensym))
(stemp (first stores)))
(values `(,btemp ,@temps)
`(,bytespec ,@vals)
`(,store)
`(let ((,stemp (dpb ,store ,btemp ,access-form)))
,store-form ,store)
`(ldb ,btemp ,access-form)))))
(define-setf-expander mask-field (&environment env bytespec int)
(multiple-value-bind (temps vals stores store-form access-form)
(get-setf-expansion int env)
(let* ((btemp (gensym))
(store (gensym))
(stemp (first stores)))
(values `(,btemp ,@temps)
`(,bytespec ,@vals)
`(,store)
`(let ((,stemp (deposit-field ,store ,btemp ,access-form)))
,store-form ,store)
`(mask-field ,btemp ,access-form)))))
;;; The expansion function for SETF.
(defun setf-expand-1 (place newvalue env)
(multiple-value-bind (vars vals stores store-form access-form)
(get-setf-expansion place env)
(declare (ignore access-form))
(let ((declaration `(declare (:read-only ,@(append vars stores)))))
(if (= (length stores) 1)
`(let* ,(mapcar #'list
(append vars stores)
(append vals (list newvalue)))
,declaration
,store-form)
`(let* ,(mapcar #'list vars vals)
(multiple-value-bind ,stores ,newvalue
,declaration
,store-form))))))
(defun setf-structure-access (struct type index newvalue)
(cond
((or (eq type 'list) (eq type 'vector))
`(sys:elt-set ,struct ,index ,newvalue))
((consp type)
`(si::aset ,newvalue (the ,type ,struct) ,index))
(t `(sys::structure-set ,struct ',type ,index ,newvalue))))
(defun setf-expand (l env)
(cond ((endp l) nil)
((endp (cdr l)) (error "~S is an illegal SETF form." l))
(t
(cons (setf-expand-1 (car l) (cadr l) env)
(setf-expand (cddr l) env)))))
;;; SETF macro.
(defmacro setf (&environment env &rest rest)
"Syntax: (setf {place form}*)
Evaluates each FORM and assigns the value to the corresponding PLACE in order.
Returns the value of the last FORM.
Each PLACE may be any one of the following:
* A symbol that names a variable.
* A function call form whose first element is the name of the following
functions:
nth elt subseq rest first ... tenth
c?r c??r c???r c????r
aref svref char schar bit sbit fill-pointer
get getf documentation symbol-value symbol-function
symbol-plist macro-function gethash fdefinition
char-bit ldb mask-field
apply slot-value
where '?' stands for either 'a' or 'd'.
* A function call form whose first element is:
1. an access function for a structure slot
1. an accessor method for a CLOS object
* the form (THE type place) with PLACE being a place recognized by SETF.
* a macro call which expands to a place recognized by SETF.
* any form for which a DEFSETF or DEFINE-SETF-EXPANDER declaration has been
made."
(cond ((endp rest) nil)
((endp (cdr rest)) (error "~S is an illegal SETF form." rest))
((endp (cddr rest)) (setf-expand-1 (car rest) (cadr rest) env))
(t (cons 'progn (setf-expand rest env)))))
;;; PSETF macro.
(defmacro psetf (&environment env &rest rest)
"Syntax: (psetf {place form}*)
Similar to SETF, but evaluates all FORMs first, and then assigns each value to
the corresponding PLACE. Returns NIL."
(cond ((endp rest) nil)
((endp (cdr rest)) (error "~S is an illegal PSETF form." rest))
((endp (cddr rest))
`(progn ,(setf-expand-1 (car rest) (cadr rest) env)
nil))
(t
(do ((r rest (cddr r))
(pairs nil)
(store-forms nil))
((endp r)
`(let* ,pairs
,@(nreverse store-forms)
nil))
(when (endp (cdr r)) (error "~S is an illegal PSETF form." rest))
(multiple-value-bind (vars vals stores store-form access-form)
(get-setf-expansion (car r) env)
(declare (ignore access-form))
(setq store-forms (cons store-form store-forms))
(setq pairs
(nconc pairs
(mapcar #'list
(append vars stores)
(append vals (list (cadr r)))))))))))
;;; SHIFTF macro.
(defmacro shiftf (&environment env &rest rest)
"Syntax: (shiftf {place}+ form)
Saves the values of PLACE and FORM, and then assigns the value of each PLACE
to the PLACE on its left. The rightmost PLACE gets the value of FORM.
Returns the original value of the leftmost PLACE."
(do ((r rest (cdr r))
(pairs nil)
(stores nil)
(store-forms nil)
(g (gensym))
(access-forms nil))
((endp (cdr r))
(setq stores (nreverse stores))
(setq store-forms (nreverse store-forms))
(setq access-forms (nreverse access-forms))
`(let* ,(nconc pairs
(list (list g (car access-forms)))
(mapcar #'list stores (cdr access-forms))
(list (list (car (last stores)) (car r))))
,@store-forms
,g))
(multiple-value-bind (vars vals stores1 store-form access-form)
(get-setf-expansion (car r) env)
(setq pairs (nconc pairs (mapcar #'list vars vals)))
(setq stores (cons (car stores1) stores))
(setq store-forms (cons store-form store-forms))
(setq access-forms (cons access-form access-forms)))))
;;; ROTATEF macro.
(defmacro rotatef (&environment env &rest rest)
"Syntax: (rotatef {place}*)
Saves the values of PLACEs, and then assigns to each PLACE the saved value of
the PLACE to its right. The rightmost PLACE gets the value of the leftmost
PLACE. Returns NIL."
(do ((r rest (cdr r))
(pairs nil)
(stores nil)
(store-forms nil)
(access-forms nil))
((endp r)
(setq stores (nreverse stores))
(setq store-forms (nreverse store-forms))
(setq access-forms (nreverse access-forms))
`(let* ,(nconc pairs
(mapcar #'list stores (cdr access-forms))
(list (list (car (last stores)) (car access-forms))))
,@store-forms
nil))
(multiple-value-bind (vars vals stores1 store-form access-form)
(get-setf-expansion (car r) env)
(setq pairs (nconc pairs (mapcar #'list vars vals)))
(setq stores (cons (car stores1) stores))
(setq store-forms (cons store-form store-forms))
(setq access-forms (cons access-form access-forms)))))
;;; DEFINE-MODIFY-MACRO macro, by Bruno Haible.
(defmacro define-modify-macro (name lambdalist function &optional docstring)
"Syntax: (define-modify-macro symbol lambda-list function-name [doc])
Defines a read-modify-write macro like INCF. The defined macro will expand
a form (SYMBOL place form1 ... formn) into a form that in effect SETFs the
value of (FUNCTION-NAME place arg1 ... argm) into PLACE, where ARG1 ... ARGm
are parameters in LAMBDA-LIST which are bound to FORM1 ... FORMn. For
example, INCF could be defined as
(define-modify-macro incf (&optional (x 1)) +)
The doc-string DOC, if supplied, is saved as a FUNCTION doc and can be
retrieved by (DOCUMENTATION 'SYMBOL 'FUNCTION)."
(let* ((varlist nil)
(restvar nil))
(do* ((lambdalistr lambdalist (cdr lambdalistr))
(next))
((null lambdalistr))
(setq next (first lambdalistr))
(cond ((eq next '&OPTIONAL))
((eq next '&REST)
(if (symbolp (second lambdalistr))
(setq restvar (second lambdalistr))
(error "In the definition of ~S: &REST variable ~S should be a symbol."
name (second lambdalistr)
) )
(if (null (cddr lambdalistr))
(return)
(error "Only one variable is allowed after &REST, not ~S"
lambdalistr
)) )
((or (eq next '&KEY) (eq next '&ALLOW-OTHER-KEYS) (eq next '&AUX))
(error "Illegal in a DEFINE-MODIFY-MACRO lambda list: ~S"
next
))
((symbolp next) (push next varlist))
((and (listp next) (symbolp (first next)))
(push (first next) varlist)
)
(t (error "lambda list may only contain symbols and lists, not ~S"
next
) )
) )
(setq varlist (nreverse varlist))
`(DEFMACRO ,name (&ENVIRONMENT ENV %REFERENCE ,@lambdalist) ,docstring
(MULTIPLE-VALUE-BIND (VARS VALS STORES SETTER GETTER)
(GET-SETF-EXPANSION %REFERENCE ENV)
(LET ((ALL-VARS (MAPCAR #'(LAMBDA (V) (LIST (GENSYM) V)) (LIST* ,@varlist ,restvar))))
(IF (SYMBOLP GETTER)
(SUBST (LIST* (QUOTE ,function) GETTER (MAPCAR #'CAR ALL-VARS))
(CAR STORES)
`(LET* ,ALL-VARS
(DECLARE (:READ-ONLY ,@(mapcar #'first all-vars)))
,SETTER))
(DO ((D VARS (CDR D))
(V VALS (CDR V))
(LET-LIST NIL (CONS (LIST (CAR D) (CAR V)) LET-LIST)))
((NULL D)
(SETQ LET-LIST (APPEND (NREVERSE ALL-VARS) LET-LIST))
(PUSH
(LIST
(CAR STORES)
(IF (AND (LISTP %REFERENCE) (EQ (CAR %REFERENCE) 'THE))
(LIST 'THE (CADR %REFERENCE)
(LIST* (QUOTE ,function) GETTER ,@varlist ,restvar))
(LIST* (QUOTE ,function) GETTER (MAPCAR #'CAR ALL-VARS))))
LET-LIST)
`(LET* ,(NREVERSE LET-LIST)
(DECLARE (:READ-ONLY ,@(mapcar #'first all-vars)
,@vars))
,SETTER)))))))))
;;; Some macro definitions.
(defmacro remf (&environment env place indicator)
"Syntax: (remf place form)
Removes the property specified by FORM from the property list stored in PLACE.
Returns T if the property list had the specified property; NIL otherwise."
(multiple-value-bind (vars vals stores store-form access-form)
(get-setf-expansion place env)
(let ((s (gensym)))
`(let* (,@(mapcar #'list vars vals) (,s ,indicator))
(declare (:read-only ,@vars)) ; Beppe
(multiple-value-bind (,(car stores) flag)
(sys:rem-f ,access-form ,s)
,store-form
flag)))))
(define-modify-macro incf (&optional (delta 1)) +
"Syntax: (incf place [form])
Increments the value of PLACE by the value of FORM. FORM defaults to 1.")
(define-modify-macro decf (&optional (delta 1)) -
"Syntax: (decf place [form])
Decrements the value of PLACE by the value of FORM. FORM defaults to 1.")
(defmacro push (&environment env item place)
"Syntax: (push form place)
Evaluates FORM, conses the value of FORM to the value stored in PLACE, and
makes it the new value of PLACE. Returns the new value of PLACE."
(multiple-value-bind (vars vals stores store-form access-form)
(get-setf-expansion place env)
(when (and (null vars) (eq access-form place))
(return-from push `(setq ,place (cons ,item ,place))))
;; The item to be pushed has to be evaluated before the destination
(unless (constantp item)
(setq vals (cons item vals)
item (gensym)
vars (cons item vars)))
`(let* ,(mapcar #'list
(append vars stores)
(append vals (list (list 'cons item access-form))))
(declare (:read-only ,@vars)) ; Beppe
,store-form)))
(defmacro pushnew (&environment env item place &rest rest)
"Syntax: (pushnew form place {keyword-form value-form}*)
Evaluates FORM first. If the value is already in the list stored in PLACE,
does nothing. Else, conses the value onto the list and makes the result the
new value of PLACE. Returns NIL. KEYWORD-FORMs and VALUE-FORMs are used to
check if the value of FORM is already in PLACE as if their values are passed
to MEMBER."
(multiple-value-bind (vars vals stores store-form access-form)
(get-setf-expansion place env)
(when (and (null vars) (eq access-form place))
(return-from pushnew `(setq ,place (adjoin ,item ,place ,@rest))))
;; The item to be pushed has to be evaluated before the destination
(unless (constantp item)
(setq vals (cons item vals)
item (gensym)
vars (cons item vars)))
`(let* ,(mapcar #'list
(append vars stores)
(append vals
(list (list* 'adjoin item access-form rest))))
(declare (:read-only ,@vars)) ; Beppe
,store-form)))
(defmacro pop (&environment env place)
"Syntax: (pop place)
Gets the cdr of the value stored in PLACE and makes it the new value of PLACE.
Returns the car of the old value in PLACE."
(multiple-value-bind (vars vals stores store-form access-form)
(get-setf-expansion place env)
(if (and (null vars) (eq access-form place))
`(prog1 (car ,place) (setq ,place (cdr ,place)))
`(let* ,(mapcar #'list
(append vars stores)
(append vals (list (list 'cdr access-form))))
(declare (:read-only ,@vars)) ; Beppe
(prog1 (car ,access-form)
,store-form)))))
(define-setf-expander values (&rest values &environment env)
(let ((all-vars '())
(all-vals '())
(all-stores '())
(all-storing-forms '())
(all-get-forms '()))
(dolist (item (reverse values))
(multiple-value-bind (vars vals stores storing-form get-form)
(get-setf-expansion item env)
;; If a place has more than one store variable, the other ones
;; are set to nil.
(let ((extra (rest stores)))
(unless (endp extra)
(setf vars (append extra vars)
vals (append (make-list (length extra)) vals)
stores (list (first stores)))))
(setf all-vars (append vars all-vars)
all-vals (append vals all-vals)
all-stores (append stores all-stores)
all-storing-forms (cons storing-form all-storing-forms)
all-get-forms (cons get-form all-get-forms))))
(values all-vars all-vals all-stores `(values ,@all-storing-forms)
`(values ,@all-get-forms))))