/[cmucl]/src/code/macros.lisp
ViewVC logotype

Contents of /src/code/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.22 - (hide annotations)
Wed May 8 16:30:04 1991 UTC (22 years, 11 months ago) by ram
Branch: MAIN
Changes since 1.21: +111 -105 lines
Changed DEFSETF and DEFINE-SETF-METHOD to call a common subfunction,
%DEFINE-SETF-MACRO, which checks for any existing definition as a slot accessor
or other setf function.

Changed GET-SETF-METHOD to ignore setf macros (always global) when there is a
local macro or function.

Changed SETF to always call GET-SETF-METHOD, instead of special-casing setf
inverses.  I was tired to keeping the duplicated code consistent.

Generalized the special casing in SETF of APPLY.  We now go for the SETF
function when called on any symbol in the Lisp package.  Now SETF of APPLY will
work on all Common Lisp functions, instead of just two.
1 ram 1.1 ;;; -*- Log: code.log; Package: Lisp -*-
2     ;;;
3     ;;; **********************************************************************
4 ram 1.20 ;;; This code was written as part of the CMU Common Lisp project at
5     ;;; Carnegie Mellon University, and has been placed in the public domain.
6     ;;; If you want to use this code or any part of CMU Common Lisp, please contact
7     ;;; Scott Fahlman or slisp-group@cs.cmu.edu.
8     ;;;
9     (ext:file-comment
10 ram 1.22 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/macros.lisp,v 1.22 1991/05/08 16:30:04 ram Exp $")
11 ram 1.20 ;;;
12 ram 1.1 ;;; **********************************************************************
13     ;;;
14     ;;; This file contains the macros that are part of the standard
15     ;;; Spice Lisp environment.
16     ;;;
17     ;;; Written by Scott Fahlman and Rob MacLachlan.
18 ram 1.22 ;;; Modified by Bill Chiles to adhere to the wall.
19 ram 1.1 ;;;
20 wlott 1.10 (in-package "LISP")
21 wlott 1.13 (export '(defvar defparameter defconstant when unless setf
22 ram 1.1 defsetf define-setf-method psetf shiftf rotatef push pushnew pop
23     incf decf remf case typecase with-open-file
24     with-open-stream with-input-from-string with-output-to-string
25     locally etypecase ctypecase ecase ccase
26     get-setf-method get-setf-method-multiple-value
27 ram 1.18 define-modify-macro destructuring-bind nth-value
28 ram 1.1 otherwise)) ; Sacred to CASE and related macros.
29    
30     (in-package "EXTENSIONS")
31     (export '(do-anonymous collect iterate))
32    
33     (in-package "LISP")
34    
35    
36     ;;; Parse-Body -- Public
37     ;;;
38     ;;; Parse out declarations and doc strings, *not* expanding macros.
39     ;;; Eventually the environment arg should be flushed, since macros can't expand
40     ;;; into declarations anymore.
41     ;;;
42     (defun parse-body (body environment &optional (doc-string-allowed t))
43     "This function is to parse the declarations and doc-string out of the body of
44     a defun-like form. Body is the list of stuff which is to be parsed.
45     Environment is ignored. If Doc-String-Allowed is true, then a doc string
46     will be parsed out of the body and returned. If it is false then a string
47     will terminate the search for declarations. Three values are returned: the
48     tail of Body after the declarations and doc strings, a list of declare forms,
49     and the doc-string, or NIL if none."
50     (declare (ignore environment))
51     (let ((decls ())
52     (doc nil))
53     (do ((tail body (cdr tail)))
54     ((endp tail)
55     (values tail (nreverse decls) doc))
56     (let ((form (car tail)))
57     (cond ((and (stringp form) (cdr tail))
58     (if doc-string-allowed
59     (setq doc form)
60     (return (values tail (nreverse decls) doc))))
61     ((not (and (consp form) (symbolp (car form))))
62     (return (values tail (nreverse decls) doc)))
63     ((eq (car form) 'declare)
64     (push form decls))
65     (t
66     (return (values tail (nreverse decls) doc))))))))
67    
68    
69     ;;;; DEFMACRO:
70    
71     ;;; Defmacro -- Public
72     ;;;
73     ;;; Parse the definition and make an expander function. The actual
74     ;;; definition is done by %defmacro which we expand into.
75     ;;;
76     (defmacro defmacro (name lambda-list &body body)
77 wlott 1.13 (let ((whole (gensym "WHOLE-"))
78     (environment (gensym "ENV-")))
79 ram 1.1 (multiple-value-bind
80     (body local-decs doc)
81 wlott 1.13 (parse-defmacro lambda-list whole body name 'defmacro
82     :environment environment)
83 ram 1.1 (let ((def `(lambda (,whole ,environment)
84     ,@local-decs
85     (block ,name
86     ,body))))
87 wlott 1.13 `(c::%defmacro ',name #',def ',lambda-list ,doc)))))
88 ram 1.1
89    
90     ;;; %Defmacro, %%Defmacro -- Internal
91     ;;;
92     ;;; Defmacro expands into %Defmacro which is a function that is treated
93     ;;; magically the compiler. After the compiler has gotten the information it
94     ;;; wants out of macro definition, it compiles a call to %%Defmacro which
95     ;;; happens at load time. We have a %Defmacro function which just calls
96     ;;; %%Defmacro in order to keep the interpreter happy.
97     ;;;
98     ;;; Eventually %%Defmacro should deal with clearing old compiler information
99     ;;; for the functional value.
100     ;;;
101     (defun c::%defmacro (name definition lambda-list doc)
102 wlott 1.13 (assert (eval:interpreted-function-p definition))
103     (setf (eval:interpreted-function-name definition)
104     (format nil "DEFMACRO ~S" name))
105     (setf (eval:interpreted-function-arglist definition) lambda-list)
106 ram 1.1 (c::%%defmacro name definition doc))
107     ;;;
108     (defun c::%%defmacro (name definition doc)
109     (clear-info function where-from name)
110 wlott 1.10 (setf (macro-function name) definition)
111 ram 1.1 (setf (documentation name 'function) doc)
112     name)
113    
114    
115     ;;; DEFTYPE is a lot like DEFMACRO.
116    
117     (defmacro deftype (name arglist &body body)
118     "Syntax like DEFMACRO, but defines a new type."
119     (unless (symbolp name)
120     (error "~S -- Type name not a symbol." name))
121    
122 wlott 1.13 (let ((whole (gensym "WHOLE-")))
123 ram 1.1 (multiple-value-bind (body local-decs doc)
124 wlott 1.13 (parse-defmacro arglist whole body name 'deftype
125     :default-default ''*)
126     `(eval-when (compile load eval)
127     (%deftype ',name
128     #'(lambda (,whole)
129     ,@local-decs
130     (block ,name ,body))
131     ,@(when doc `(,doc)))))))
132 ram 1.7 ;;;
133     (defun %deftype (name expander &optional doc)
134 ram 1.12 (ecase (info type kind name)
135     (:primitive
136     (error "Illegal to redefine standard type: ~S." name))
137     (:structure
138     (warn "Redefining structure type ~S with DEFTYPE." name)
139     (c::undefine-structure (info type structure-info name)))
140     ((nil :defined)))
141 wlott 1.13 (setf (info type kind name) :defined)
142     (setf (info type expander name) expander)
143 ram 1.7 (when doc
144     (setf (documentation name 'type) doc))
145 wlott 1.10 ;; ### Bootstrap hack -- we need to define types before %note-type-defined
146     ;; is defined.
147 ram 1.12 (when (fboundp 'c::%note-type-defined)
148 wlott 1.10 (c::%note-type-defined name))
149 ram 1.7 name)
150    
151 ram 1.1
152     ;;; And so is DEFINE-SETF-METHOD.
153    
154     (defparameter defsetf-error-string "Setf expander for ~S cannot be called with ~S args.")
155    
156     (defmacro define-setf-method (access-fn lambda-list &body body)
157     "Syntax like DEFMACRO, but creates a Setf-Method generator. The body
158     must be a form that returns the five magical values."
159     (unless (symbolp access-fn)
160     (error "~S -- Access-function name not a symbol in DEFINE-SETF-METHOD."
161     access-fn))
162    
163 wlott 1.13 (let ((whole (gensym "WHOLE-"))
164     (environment (gensym "ENV-")))
165 ram 1.1 (multiple-value-bind (body local-decs doc)
166     (parse-defmacro lambda-list whole body access-fn
167 wlott 1.13 'define-setf-method
168     :environment environment)
169 ram 1.1 `(eval-when (load compile eval)
170 ram 1.22 (%define-setf-macro
171     ',access-fn
172     #'(lambda (,whole ,environment)
173     ,@local-decs
174     (block ,access-fn ,body))
175     nil
176     ',doc)))))
177 ram 1.1
178 ram 1.22
179     ;;; %DEFINE-SETF-MACRO -- Internal
180     ;;;
181     ;;; Do stuff for defining a setf macro.
182     ;;;
183     (defun %define-setf-macro (name expander inverse doc)
184     (cond ((not (fboundp `(setf ,name))))
185     ((info function accessor-for name)
186     (warn "Defining setf macro for destruct slot accessor; redefining as ~
187     a normal function:~% ~S"
188     name)
189     (c::define-function-name name))
190     ((not (eq (symbol-package name) (symbol-package 'aref)))
191     (warn "Defining setf macro for ~S, but ~S is fbound."
192     name `(setf ,name))))
193     (when (or inverse (info setf inverse name))
194     (setf (info setf inverse name) inverse))
195     (when (or expander (info setf expander name))
196     (setf (info setf expander name) expander))
197     (when doc
198     (setf (documentation name 'setf) doc))
199     name)
200    
201 wlott 1.13
202     ;;;; Destructuring-bind
203 ram 1.1
204 wlott 1.13 (defmacro destructuring-bind (lambda-list arg-list &rest body)
205     "Bind the variables in LAMBDA-LIST to the contents of ARG-LIST."
206     (let* ((arg-list-name (gensym "ARG-LIST-")))
207     (multiple-value-bind
208 wlott 1.14 (body local-decls)
209 wlott 1.17 (parse-defmacro lambda-list arg-list-name body nil 'destructuring-bind
210 wlott 1.13 :annonymousp t :doc-string-allowed nil)
211     `(let ((,arg-list-name ,arg-list))
212 wlott 1.14 ,@local-decls
213 wlott 1.13 ,body))))
214    
215 ram 1.1
216     ;;;; Defun, Defvar, Defparameter, Defconstant:
217    
218     ;;; Defun -- Public
219     ;;;
220     ;;; Very similar to Defmacro, but simpler. We don't have to parse the
221     ;;; lambda-list.
222     ;;;
223 wlott 1.13 (defmacro defun (&whole source name lambda-list &body (body decls doc))
224 ram 1.1 (let ((def `(lambda ,lambda-list
225     ,@decls
226     (block ,(if (and (consp name) (eq (car name) 'setf))
227     (cadr name)
228     name)
229     ,@body))))
230     `(c::%defun ',name #',def ,doc ',source)))
231    
232    
233     ;;; %Defun, %%Defun -- Internal
234     ;;;
235     ;;; Similar to %Defmacro, ...
236     ;;;
237     (defun c::%%defun (name def doc &optional inline-expansion)
238     (setf (fdefinition name) def)
239     (when doc
240     (if (and (consp name) (eq (first name) 'setf))
241     (setf (documentation (second name) 'setf) doc)
242     (setf (documentation name 'function) doc)))
243 ram 1.12 (c::define-function-name name)
244     (when (eq (info function where-from name) :assumed)
245     (setf (info function where-from name) :defined)
246     (when (info function assumed-type name)
247     (setf (info function assumed-type name) nil)))
248 ram 1.1 (when (or inline-expansion
249     (info function inline-expansion name))
250     (setf (info function inline-expansion name) inline-expansion))
251     name)
252     ;;;
253     (defun c::%defun (name def doc source)
254     (declare (ignore source))
255 ram 1.3 (assert (eval:interpreted-function-p def))
256     (setf (eval:interpreted-function-name def) name)
257 ram 1.1 (c::%%defun name def doc))
258    
259    
260     ;;; DEFCONSTANT -- Public
261     ;;;
262     (defmacro defconstant (var val &optional doc)
263     "For defining global constants at top level. The DEFCONSTANT says that the
264     value is constant and may be compiled into code. If the variable already has
265     a value, and this is not equal to the init, an error is signalled. The third
266     argument is an optional documentation string for the variable."
267     `(c::%defconstant ',var ,val ',doc))
268    
269     ;;; %Defconstant, %%Defconstant -- Internal
270     ;;;
271     ;;; Like the other %mumbles except that we currently actually do something
272     ;;; interesting at load time, namely checking if the constant is being
273     ;;; redefined.
274     ;;;
275     (defun c::%defconstant (name value doc)
276     (c::%%defconstant name value doc))
277     ;;;
278     (defun c::%%defconstant (name value doc)
279     (when doc
280     (setf (documentation name 'variable) doc))
281     (when (boundp name)
282     (unless (equalp (symbol-value name) value)
283     (cerror "Go ahead and change the value."
284     "Constant ~S being redefined." name)))
285     (setf (symbol-value name) value)
286     (setf (info variable kind name) :constant)
287     (clear-info variable constant-value name)
288     name)
289    
290    
291     (defmacro defvar (var &optional (val nil valp) (doc nil docp))
292     "For defining global variables at top level. Declares the variable
293     SPECIAL and, optionally, initializes it. If the variable already has a
294     value, the old value is not clobbered. The third argument is an optional
295     documentation string for the variable."
296     `(progn
297     (proclaim '(special ,var))
298     ,@(when valp
299     `((unless (boundp ',var)
300     (setq ,var ,val))))
301     ,@(when docp
302     `((setf (documentation ',var 'variable) ',doc)))
303     ',var))
304    
305     (defmacro defparameter (var val &optional (doc nil docp))
306     "Defines a parameter that is not normally changed by the program,
307     but that may be changed without causing an error. Declares the
308     variable special and sets its value to VAL. The third argument is
309     an optional documentation string for the parameter."
310     `(progn
311     (proclaim '(special ,var))
312     (setq ,var ,val)
313     ,@(when docp
314     `((setf (documentation ',var 'variable) ',doc)))
315     ',var))
316    
317    
318     ;;;; ASSORTED CONTROL STRUCTURES
319    
320    
321     (defmacro when (test &body forms)
322     "First arg is a predicate. If it is non-null, the rest of the forms are
323     evaluated as a PROGN."
324     `(cond (,test nil ,@forms)))
325    
326     (defmacro unless (test &rest forms)
327     "First arg is a predicate. If it is null, the rest of the forms are
328     evaluated as a PROGN."
329     `(cond ((not ,test) nil ,@forms)))
330    
331    
332     (defmacro return (&optional (value nil))
333     `(return-from nil ,value))
334    
335     (defmacro prog (varlist &body (body decls))
336     `(block nil
337     (let ,varlist
338     ,@decls
339     (tagbody ,@body))))
340    
341     (defmacro prog* (varlist &body (body decls))
342     `(block nil
343     (let* ,varlist
344     ,@decls
345     (tagbody ,@body))))
346    
347    
348     ;;; Prog1, Prog2 -- Public
349     ;;;
350     ;;; These just turn into a Let.
351     ;;;
352     (defmacro prog1 (result &rest body)
353     (let ((n-result (gensym)))
354     `(let ((,n-result ,result))
355     ,@body
356     ,n-result)))
357     ;;;
358     (defmacro prog2 (form1 result &rest body)
359     `(prog1 (progn ,form1 ,result) ,@body))
360    
361    
362     ;;; And, Or -- Public
363     ;;;
364     ;;; AND and OR are defined in terms of IF.
365     ;;;
366     (defmacro and (&rest forms)
367     (cond ((endp forms) t)
368     ((endp (rest forms)) (first forms))
369     (t
370     `(if ,(first forms)
371     (and ,@(rest forms))
372     nil))))
373     ;;;
374     (defmacro or (&rest forms)
375     (cond ((endp forms) nil)
376     ((endp (rest forms)) (first forms))
377     (t
378     (let ((n-result (gensym)))
379     `(let ((,n-result ,(first forms)))
380     (if ,n-result
381     ,n-result
382     (or ,@(rest forms))))))))
383    
384    
385     ;;; Cond -- Public
386     ;;;
387     ;;; COND also turns into IF.
388     ;;;
389     (defmacro cond (&rest clauses)
390     (if (endp clauses)
391     nil
392     (let ((clause (first clauses)))
393     (when (atom clause)
394     (error "Cond clause is not a list: ~S." clause))
395     (let ((test (first clause))
396     (forms (rest clause)))
397     (if (endp forms)
398     (let ((n-result (gensym)))
399     `(let ((,n-result ,test))
400     (if ,n-result
401     ,n-result
402     (cond ,@(rest clauses)))))
403     `(if ,test
404     (progn ,@forms)
405     (cond ,@(rest clauses))))))))
406    
407    
408     ;;;; Multiple value macros:
409    
410     ;;; Multiple-Value-XXX -- Public
411     ;;;
412     ;;; All the multiple-value receiving forms are defined in terms of
413     ;;; Multiple-Value-Call.
414     ;;;
415     (defmacro multiple-value-setq (varlist value-form)
416     (unless (and (listp varlist) (every #'symbolp varlist))
417     (error "Varlist is not a list of symbols: ~S." varlist))
418     (let ((temps (mapcar #'(lambda (x) (declare (ignore x)) (gensym)) varlist)))
419     `(multiple-value-bind ,temps ,value-form
420     ,@(mapcar #'(lambda (var temp)
421     `(setq ,var ,temp))
422     varlist temps)
423     ,(car temps))))
424     ;;;
425     (defmacro multiple-value-bind (varlist value-form &body body)
426     (unless (and (listp varlist) (every #'symbolp varlist))
427     (error "Varlist is not a list of symbols: ~S." varlist))
428     (if (= (length varlist) 1)
429     `(let ((,(car varlist) ,value-form))
430     ,@body)
431     (let ((ignore (gensym)))
432     `(multiple-value-call #'(lambda (&optional ,@varlist &rest ,ignore)
433     (declare (ignore ,ignore))
434     ,@body)
435     ,value-form))))
436     ;;;
437     (defmacro multiple-value-list (value-form)
438     `(multiple-value-call #'list ,value-form))
439 ram 1.18
440    
441     (defmacro nth-value (n form)
442     "Evaluates FORM and returns the Nth value (zero based). This involves no
443     consing when N is a trivial constant integer."
444     (if (integerp n)
445     (let ((dummy-list nil)
446     (wendy (gensym)))
447     ;; We build DUMMY-LIST, a list of variables to bind to useless
448     ;; values, then we explicitly IGNORE those bindings and return
449     ;; WENDY, the only thing we're really interested in right now.
450     (dotimes (i n)
451     (push (gensym) dummy-list))
452     `(multiple-value-bind (,@dummy-list ,wendy)
453     ,form
454 ram 1.19 (declare (ignore ,@dummy-list))
455 ram 1.18 ,wendy))
456     `(nth ,n (multiple-value-list ,form))))
457 ram 1.1
458    
459     ;;;; SETF and friends.
460    
461     ;;; Note: The expansions for SETF and friends sometimes create needless
462     ;;; LET-bindings of argument values. The compiler will remove most of
463     ;;; these spurious bindings, so SETF doesn't worry too much about creating
464     ;;; them.
465    
466     ;;; The inverse for a generalized-variable reference function is stored in
467     ;;; one of two ways:
468     ;;;
469     ;;; A SETF-INVERSE property corresponds to the short form of DEFSETF. It is
470     ;;; the name of a function takes the same args as the reference form, plus a
471     ;;; new-value arg at the end.
472     ;;;
473     ;;; A SETF-METHOD-EXPANDER property is created by the long form of DEFSETF or
474     ;;; by DEFINE-SETF-METHOD. It is a function that is called on the reference
475     ;;; form and that produces five values: a list of temporary variables, a list
476     ;;; of value forms, a list of the single store-value form, a storing function,
477     ;;; and an accessing function.
478    
479 wlott 1.13 ;;; Left over in case someone is still trying to call this.
480     (defun foo-get-setf-method (form &optional environment)
481     (get-setf-method form environment))
482 wlott 1.11
483     (eval-when (compile load eval)
484 ram 1.1 ;;;
485 wlott 1.13 (defun get-setf-method (form &optional environment)
486 ram 1.1 "Returns five values needed by the SETF machinery: a list of temporary
487     variables, a list of values with which to fill them, the temporary for the
488     new value in a list, the setting function, and the accessing function."
489     (let (temp)
490     (cond ((symbolp form)
491     (let ((new-var (gensym)))
492     (values nil nil (list new-var) `(setq ,form ,new-var) form)))
493 ram 1.22 ((and environment
494     (assoc (car form) (c::lexenv-functions environment)))
495     (get-setf-method-inverse form `(funcall #'(setf ,(car form))) t))
496 ram 1.1 ;;
497     ;; ### Bootstrap hack...
498     ;; Ignore any DEFSETF info for structure accessors.
499     ((info function accessor-for (car form))
500 wlott 1.10 (get-setf-method-inverse form `(funcall #'(setf ,(car form))) t))
501 ram 1.1 ((setq temp (info setf inverse (car form)))
502 wlott 1.10 (get-setf-method-inverse form `(,temp) nil))
503 ram 1.1 ((setq temp (info setf expander (car form)))
504     (funcall temp form environment))
505     (t
506     (multiple-value-bind (res win)
507     (macroexpand-1 form environment)
508     (if win
509 wlott 1.13 (get-setf-method res environment)
510 wlott 1.10 (get-setf-method-inverse form
511     `(funcall #'(setf ,(car form)))
512     t)))))))
513 ram 1.1
514 wlott 1.10 (defun get-setf-method-inverse (form inverse setf-function)
515 ram 1.1 (let ((new-var (gensym))
516     (vars nil)
517     (vals nil))
518     (dolist (x (cdr form))
519     (push (gensym) vars)
520     (push x vals))
521     (setq vals (nreverse vals))
522     (values vars vals (list new-var)
523 wlott 1.10 (if setf-function
524     `(,@inverse ,new-var ,@vars)
525     `(,@inverse ,@vars ,new-var))
526 ram 1.1 `(,(car form) ,@vars))))
527    
528    
529     (defun get-setf-method-multiple-value (form &optional environment)
530     "Like Get-Setf-Method, but may return multiple new-value variables."
531     (get-setf-method form environment))
532    
533 wlott 1.16 (defun defsetter (fn rest)
534 wlott 1.14 (let ((arglist (car rest))
535     (arglist-var (gensym "ARGS-"))
536     (new-var (car (cadr rest))))
537     (multiple-value-bind
538     (body local-decs doc)
539 wlott 1.16 (parse-defmacro arglist arglist-var (cddr rest) fn 'defsetf)
540 ram 1.1 (values
541 wlott 1.14 `(lambda (,arglist-var ,new-var)
542     ,@local-decs
543 wlott 1.16 ,body)
544 wlott 1.15 doc))))
545 ram 1.1
546     ) ; End of Eval-When.
547    
548    
549 wlott 1.16 (defmacro defsetf (access-fn &rest rest)
550 ram 1.1 "Associates a SETF update function or macro with the specified access
551     function or macro. The format is complex. See the manual for
552     details."
553     (cond ((not (listp (car rest)))
554     `(eval-when (load compile eval)
555 ram 1.22 (%define-setf-macro ',access-fn nil ',(car rest)
556     ,(when (and (car rest) (stringp (cadr rest)))
557     `',(cadr rest)))))
558 ram 1.1 ((and (listp (car rest)) (cdr rest) (listp (cadr rest)))
559     (if (not (= (length (cadr rest)) 1))
560     (cerror "Ignore the extra items in the list."
561     "Only one new-value variable allowed in DEFSETF."))
562     (multiple-value-bind (setting-form-generator doc)
563 wlott 1.16 (defsetter access-fn rest)
564 ram 1.1 `(eval-when (load compile eval)
565 ram 1.22 (%define-setf-macro
566     ',access-fn
567     #'(lambda (access-form environment)
568     (declare (ignore environment))
569     (do* ((args (cdr access-form) (cdr args))
570     (dummies nil (cons (gensym) dummies))
571     (newval-var (gensym))
572     (new-access-form nil))
573     ((atom args)
574     (setq new-access-form
575     (cons (car access-form) dummies))
576     (values
577     dummies
578     (cdr access-form)
579     (list newval-var)
580     (funcall (function ,setting-form-generator)
581     new-access-form newval-var)
582     new-access-form))))
583     nil
584     ',doc))))
585 ram 1.1 (t (error "Ill-formed DEFSETF for ~S." access-fn))))
586    
587     (defmacro setf (&rest args &environment env)
588     "Takes pairs of arguments like SETQ. The first is a place and the second
589     is the value that is supposed to go into that place. Returns the last
590     value. The place argument may be any of the access forms for which SETF
591     knows a corresponding setting form."
592 ram 1.22 (let ((nargs (length args)))
593     (cond
594     ((= nargs 2)
595     (if (atom (car args))
596     `(setq ,(car args) ,(cadr args))
597     (multiple-value-bind (dummies vals newval setter getter)
598     (get-setf-method (car args) env)
599     (declare (ignore getter))
600     (do* ((d dummies (cdr d))
601     (v vals (cdr v))
602     (let-list nil))
603     ((null d)
604     (setq let-list
605     (nreverse (cons (list (car newval)
606     (cadr args))
607     let-list)))
608     `(let* ,let-list ,setter))
609     (setq let-list
610     (cons (list (car d) (car v)) let-list))))))
611     ((oddp nargs)
612     (error "Odd number of args to SETF."))
613     (t
614     (do ((a args (cddr a)) (l nil))
615     ((null a) `(progn ,@(nreverse l)))
616     (setq l (cons (list 'setf (car a) (cadr a)) l)))))))
617 ram 1.1
618    
619     (defmacro psetf (&rest args &environment env)
620     "This is to SETF as PSETQ is to SETQ. Args are alternating place
621     expressions and values to go into those places. All of the subforms and
622     values are determined, left to right, and only then are the locations
623     updated. Returns NIL."
624     (do ((a args (cddr a))
625     (let-list nil)
626     (setf-list nil))
627     ((atom a)
628     `(let* ,(nreverse let-list) ,@(nreverse setf-list) nil))
629     (if (atom (cdr a))
630     (error "Odd number of args to PSETF."))
631     (multiple-value-bind (dummies vals newval setter getter)
632 wlott 1.13 (get-setf-method (car a) env)
633 ram 1.1 (declare (ignore getter))
634     (do* ((d dummies (cdr d))
635     (v vals (cdr v)))
636     ((null d))
637     (push (list (car d) (car v)) let-list))
638     (push (list (car newval) (cadr a)) let-list)
639     (push setter setf-list))))
640    
641    
642    
643     (defmacro shiftf (&rest args &environment env)
644     "One or more SETF-style place expressions, followed by a single
645     value expression. Evaluates all of the expressions in turn, then
646     assigns the value of each expression to the place on its left,
647     returning the value of the leftmost."
648     (if (< (length args) 2)
649     (error "Too few argument forms to a SHIFTF."))
650     (let ((leftmost (gensym)))
651     (do ((a args (cdr a))
652     (let-list nil)
653     (setf-list nil)
654     (next-var leftmost))
655     ((atom (cdr a))
656     (push (list next-var (car a)) let-list)
657     `(let* ,(nreverse let-list) ,@(nreverse setf-list) ,leftmost))
658     (multiple-value-bind (dummies vals newval setter getter)
659 wlott 1.13 (get-setf-method (car a) env)
660 ram 1.1 (do* ((d dummies (cdr d))
661     (v vals (cdr v)))
662     ((null d))
663     (push (list (car d) (car v)) let-list))
664     (push (list next-var getter) let-list)
665     (push setter setf-list)
666     (setq next-var (car newval))))))
667    
668    
669     (defmacro rotatef (&rest args &environment env)
670     "Takes any number of SETF-style place expressions. Evaluates all of the
671     expressions in turn, then assigns to each place the value of the form to
672     its right. The rightmost form gets the value of the leftmost. Returns NIL."
673     (cond ((null args) nil)
674     ((null (cdr args)) `(progn ,(car args) nil))
675     (t (do ((a args (cdr a))
676     (let-list nil)
677     (setf-list nil)
678     (next-var nil)
679     (fix-me nil))
680     ((atom a)
681     (rplaca fix-me next-var)
682     `(let* ,(nreverse let-list) ,@(nreverse setf-list) nil))
683     (multiple-value-bind (dummies vals newval setter getter)
684 wlott 1.13 (get-setf-method (car a) env)
685 ram 1.1 (do ((d dummies (cdr d))
686     (v vals (cdr v)))
687     ((null d))
688     (push (list (car d) (car v)) let-list))
689     (push (list next-var getter) let-list)
690     ;; We don't know the newval variable for the last form yet,
691     ;; so fake it for the first getter and fix it at the end.
692     (unless fix-me (setq fix-me (car let-list)))
693     (push setter setf-list)
694     (setq next-var (car newval)))))))
695    
696    
697     (defmacro define-modify-macro (name lambda-list function &optional doc-string)
698     "Creates a new read-modify-write macro like PUSH or INCF."
699     (let ((other-args nil)
700     (rest-arg nil)
701     (env (gensym))
702     (reference (gensym)))
703    
704     ;; Parse out the variable names and rest arg from the lambda list.
705     (do ((ll lambda-list (cdr ll))
706     (arg nil))
707     ((null ll))
708     (setq arg (car ll))
709     (cond ((eq arg '&optional))
710     ((eq arg '&rest)
711     (if (symbolp (cadr ll))
712     (setq rest-arg (cadr ll))
713     (error "Non-symbol &rest arg in definition of ~S." name))
714     (if (null (cddr ll))
715     (return nil)
716     (error "Illegal stuff after &rest arg in Define-Modify-Macro.")))
717     ((memq arg '(&key &allow-other-keys &aux))
718     (error "~S not allowed in Define-Modify-Macro lambda list." arg))
719     ((symbolp arg)
720     (push arg other-args))
721     ((and (listp arg) (symbolp (car arg)))
722     (push (car arg) other-args))
723     (t (error "Illegal stuff in lambda list of Define-Modify-Macro."))))
724     (setq other-args (nreverse other-args))
725     `(defmacro ,name (,reference ,@lambda-list &environment ,env)
726     ,doc-string
727     (multiple-value-bind (dummies vals newval setter getter)
728 wlott 1.13 (get-setf-method ,reference ,env)
729 ram 1.1 (do ((d dummies (cdr d))
730     (v vals (cdr v))
731     (let-list nil (cons (list (car d) (car v)) let-list)))
732     ((null d)
733     (push
734     (list (car newval)
735     ,(if rest-arg
736     `(list* ',function getter ,@other-args ,rest-arg)
737     `(list ',function getter ,@other-args)))
738     let-list)
739     `(let* ,(nreverse let-list)
740     ,setter)))))))
741    
742    
743    
744     (defmacro push (obj place &environment env)
745     "Takes an object and a location holding a list. Conses the object onto
746     the list, returning the modified list."
747     (if (symbolp place)
748     `(setq ,place (cons ,obj ,place))
749     (multiple-value-bind (dummies vals newval setter getter)
750 wlott 1.13 (get-setf-method place env)
751 ram 1.1 (do* ((d dummies (cdr d))
752     (v vals (cdr v))
753     (let-list nil))
754     ((null d)
755     (push (list (car newval) `(cons ,obj ,getter))
756     let-list)
757     `(let* ,(nreverse let-list)
758     ,setter))
759     (push (list (car d) (car v)) let-list)))))
760    
761    
762     (defmacro pushnew (obj place &rest keys &environment env)
763     "Takes an object and a location holding a list. If the object is already
764     in the list, does nothing. Else, conses the object onto the list. Returns
765     NIL. If there is a :TEST keyword, this is used for the comparison."
766     (if (symbolp place)
767     `(setq ,place (adjoin ,obj ,place ,@keys))
768     (multiple-value-bind (dummies vals newval setter getter)
769 wlott 1.13 (get-setf-method place env)
770 ram 1.1 (do* ((d dummies (cdr d))
771     (v vals (cdr v))
772     (let-list nil))
773     ((null d)
774     (push (list (car newval) `(adjoin ,obj ,getter ,@keys))
775     let-list)
776     `(let* ,(nreverse let-list)
777     ,setter))
778     (push (list (car d) (car v)) let-list)))))
779    
780    
781     (defmacro pop (place &environment env)
782     "The argument is a location holding a list. Pops one item off the front
783     of the list and returns it."
784     (if (symbolp place)
785     `(prog1 (car ,place) (setq ,place (cdr ,place)))
786     (multiple-value-bind (dummies vals newval setter getter)
787 wlott 1.13 (get-setf-method place env)
788 ram 1.1 (do* ((d dummies (cdr d))
789     (v vals (cdr v))
790     (let-list nil))
791     ((null d)
792     (push (list (car newval) getter) let-list)
793     `(let* ,(nreverse let-list)
794     (prog1 (car ,(car newval))
795     (setq ,(car newval) (cdr ,(car newval)))
796     ,setter)))
797     (push (list (car d) (car v)) let-list)))))
798    
799    
800     (define-modify-macro incf (&optional (delta 1)) +
801     "The first argument is some location holding a number. This number is
802     incremented by the second argument, DELTA, which defaults to 1.")
803    
804    
805     (define-modify-macro decf (&optional (delta 1)) -
806     "The first argument is some location holding a number. This number is
807     decremented by the second argument, DELTA, which defaults to 1.")
808    
809    
810     (defmacro remf (place indicator &environment env)
811     "Place may be any place expression acceptable to SETF, and is expected
812     to hold a property list or (). This list is destructively altered to
813     remove the property specified by the indicator. Returns T if such a
814     property was present, NIL if not."
815     (multiple-value-bind (dummies vals newval setter getter)
816 wlott 1.13 (get-setf-method place env)
817 ram 1.1 (do* ((d dummies (cdr d))
818     (v vals (cdr v))
819     (let-list nil)
820     (ind-temp (gensym))
821     (local1 (gensym))
822     (local2 (gensym)))
823     ((null d)
824     (push (list (car newval) getter) let-list)
825     (push (list ind-temp indicator) let-list)
826     `(let* ,(nreverse let-list)
827     (do ((,local1 ,(car newval) (cddr ,local1))
828     (,local2 nil ,local1))
829     ((atom ,local1) nil)
830     (cond ((atom (cdr ,local1))
831     (error "Odd-length property list in REMF."))
832     ((eq (car ,local1) ,ind-temp)
833     (cond (,local2
834     (rplacd (cdr ,local2) (cddr ,local1))
835     (return t))
836     (t (setq ,(car newval) (cddr ,(car newval)))
837     ,setter
838     (return t))))))))
839     (push (list (car d) (car v)) let-list))))
840    
841    
842     ;;; The built-in DEFSETFs.
843    
844     (defsetf car %rplaca)
845     (defsetf cdr %rplacd)
846     (defsetf caar (x) (v) `(%rplaca (car ,x) ,v))
847     (defsetf cadr (x) (v) `(%rplaca (cdr ,x) ,v))
848     (defsetf cdar (x) (v) `(%rplacd (car ,x) ,v))
849     (defsetf cddr (x) (v) `(%rplacd (cdr ,x) ,v))
850     (defsetf caaar (x) (v) `(%rplaca (caar ,x) ,v))
851     (defsetf cadar (x) (v) `(%rplaca (cdar ,x) ,v))
852     (defsetf cdaar (x) (v) `(%rplacd (caar ,x) ,v))
853     (defsetf cddar (x) (v) `(%rplacd (cdar ,x) ,v))
854     (defsetf caadr (x) (v) `(%rplaca (cadr ,x) ,v))
855     (defsetf caddr (x) (v) `(%rplaca (cddr ,x) ,v))
856     (defsetf cdadr (x) (v) `(%rplacd (cadr ,x) ,v))
857     (defsetf cdddr (x) (v) `(%rplacd (cddr ,x) ,v))
858     (defsetf caaaar (x) (v) `(%rplaca (caaar ,x) ,v))
859     (defsetf cadaar (x) (v) `(%rplaca (cdaar ,x) ,v))
860     (defsetf cdaaar (x) (v) `(%rplacd (caaar ,x) ,v))
861     (defsetf cddaar (x) (v) `(%rplacd (cdaar ,x) ,v))
862     (defsetf caadar (x) (v) `(%rplaca (cadar ,x) ,v))
863     (defsetf caddar (x) (v) `(%rplaca (cddar ,x) ,v))
864     (defsetf cdadar (x) (v) `(%rplacd (cadar ,x) ,v))
865     (defsetf cdddar (x) (v) `(%rplacd (cddar ,x) ,v))
866     (defsetf caaadr (x) (v) `(%rplaca (caadr ,x) ,v))
867     (defsetf cadadr (x) (v) `(%rplaca (cdadr ,x) ,v))
868     (defsetf cdaadr (x) (v) `(%rplacd (caadr ,x) ,v))
869     (defsetf cddadr (x) (v) `(%rplacd (cdadr ,x) ,v))
870     (defsetf caaddr (x) (v) `(%rplaca (caddr ,x) ,v))
871     (defsetf cadddr (x) (v) `(%rplaca (cdddr ,x) ,v))
872     (defsetf cdaddr (x) (v) `(%rplacd (caddr ,x) ,v))
873     (defsetf cddddr (x) (v) `(%rplacd (cdddr ,x) ,v))
874    
875     (defsetf first %rplaca)
876     (defsetf second (x) (v) `(%rplaca (cdr ,x) ,v))
877     (defsetf third (x) (v) `(%rplaca (cddr ,x) ,v))
878     (defsetf fourth (x) (v) `(%rplaca (cdddr ,x) ,v))
879     (defsetf fifth (x) (v) `(%rplaca (cddddr ,x) ,v))
880     (defsetf sixth (x) (v) `(%rplaca (cdr (cddddr ,x)) ,v))
881     (defsetf seventh (x) (v) `(%rplaca (cddr (cddddr ,x)) ,v))
882     (defsetf eighth (x) (v) `(%rplaca (cdddr (cddddr ,x)) ,v))
883     (defsetf ninth (x) (v) `(%rplaca (cddddr (cddddr ,x)) ,v))
884     (defsetf tenth (x) (v) `(%rplaca (cdr (cddddr (cddddr ,x))) ,v))
885     (defsetf rest %rplacd)
886    
887     (defsetf elt %setelt)
888     (defsetf aref %aset)
889 wlott 1.10 (defsetf row-major-aref %set-row-major-aref)
890 ram 1.1 (defsetf svref %svset)
891     (defsetf char %charset)
892     (defsetf bit %bitset)
893     (defsetf schar %scharset)
894     (defsetf sbit %sbitset)
895 wlott 1.10 (defsetf %array-dimension %set-array-dimension)
896     (defsetf %raw-bits %set-raw-bits)
897 ram 1.1 (defsetf symbol-value set)
898     (defsetf symbol-function %sp-set-definition)
899     (defsetf symbol-plist %sp-set-plist)
900     (defsetf documentation %set-documentation)
901     (defsetf nth %setnth)
902     (defsetf fill-pointer %set-fill-pointer)
903     (defsetf search-list %set-search-list)
904    
905 wlott 1.10 (defsetf sap-ref-8 %set-sap-ref-8)
906     (defsetf signed-sap-ref-8 %set-sap-ref-8)
907     (defsetf sap-ref-16 %set-sap-ref-16)
908     (defsetf signed-sap-ref-16 %set-sap-ref-16)
909     (defsetf sap-ref-32 %set-sap-ref-32)
910     (defsetf signed-sap-ref-32 %set-sap-ref-32)
911     (defsetf sap-ref-sap %set-sap-ref-sap)
912     (defsetf sap-ref-single %set-sap-ref-single)
913     (defsetf sap-ref-double %set-sap-ref-double)
914 ram 1.1
915     (define-setf-method getf (place prop &optional default &environment env)
916     (multiple-value-bind (temps values stores set get)
917 wlott 1.13 (get-setf-method place env)
918 ram 1.1 (let ((newval (gensym))
919     (ptemp (gensym))
920     (def-temp (gensym)))
921     (values `(,@temps ,(car stores) ,ptemp ,@(if default `(,def-temp)))
922     `(,@values ,get ,prop ,@(if default `(,default)))
923     `(,newval)
924     `(progn (setq ,(car stores)
925 ram 1.9 (%putf ,(car stores) ,ptemp ,newval))
926 ram 1.1 ,set
927     ,newval)
928     `(getf ,(car stores) ,ptemp ,@(if default `(,def-temp)))))))
929    
930     (define-setf-method get (symbol prop &optional default)
931     "Get turns into %put. Don't put in the default unless it really is supplied and
932     non-nil, so that we can transform into the get instruction whenever possible."
933     (let ((symbol-temp (gensym))
934     (prop-temp (gensym))
935     (def-temp (gensym))
936     (newval (gensym)))
937     (values `(,symbol-temp ,prop-temp ,@(if default `(,def-temp)))
938     `(,symbol ,prop ,@(if default `(,default)))
939     (list newval)
940     `(%put ,symbol-temp ,prop-temp ,newval)
941     `(get ,symbol-temp ,prop-temp ,@(if default `(,def-temp))))))
942    
943     (define-setf-method gethash (key hashtable &optional default)
944     (let ((key-temp (gensym))
945     (hashtable-temp (gensym))
946     (default-temp (gensym))
947     (new-value-temp (gensym)))
948     (values
949     `(,key-temp ,hashtable-temp ,@(if default `(,default-temp)))
950     `(,key ,hashtable ,@(if default `(,default)))
951     `(,new-value-temp)
952     `(%puthash ,key-temp ,hashtable-temp ,new-value-temp)
953     `(gethash ,key-temp ,hashtable-temp ,@(if default `(,default-temp))))))
954    
955     (defsetf subseq (sequence start &optional (end nil)) (v)
956     `(progn (replace ,sequence ,v :start1 ,start :end1 ,end)
957     ,v))
958    
959    
960 ram 1.22 ;;; Evil hack invented by the gnomes of Vassar Street. The function arg must
961     ;;; be constant. Get a setf method for this function, pretending that the
962     ;;; final (list) arg to apply is just a normal arg. If the setting and access
963     ;;; forms produced in this way reference this arg at the end, then just splice
964     ;;; the APPLY back onto the front and the right thing happens.
965     ;;;
966     ;;; We special-case uses functions in the Lisp package so that APPLY AREF works
967     ;;; even though %ASET takes the new-value last. (there is (SETF AREF) as well
968     ;;; as a setf method, etc.)
969     ;;;
970 ram 1.1 (define-setf-method apply (function &rest args &environment env)
971 ram 1.22 (unless (and (listp function)
972     (= (list-length function) 2)
973     (eq (first function) 'function)
974     (symbolp (second function)))
975     (error "Setf of Apply is only defined for function args like #'symbol."))
976     (let ((function (second function)))
977     (multiple-value-bind
978     (dummies vals newval setter getter)
979     (if (eq (symbol-package function) (symbol-package 'aref))
980     (get-setf-method-inverse (cons function args) `((setf ,function)) t)
981     (get-setf-method (cons function args) env))
982     (unless (and (eq (car (last args)) (car (last vals)))
983     (eq (car (last getter)) (car (last dummies)))
984     (eq (car (last setter)) (car (last dummies))))
985     (error "Apply of ~S not understood as a location for Setf." function))
986     (values dummies vals newval
987     `(apply (function ,(car setter)) ,@(cdr setter))
988     `(apply (function ,(car getter)) ,@(cdr getter))))))
989 ram 1.1
990    
991 wlott 1.10 ;;; Special-case a BYTE bytespec so that the compiler can recognize it.
992     ;;;
993 ram 1.1 (define-setf-method ldb (bytespec place &environment env)
994     "The first argument is a byte specifier. The second is any place form
995     acceptable to SETF. Replaces the specified byte of the number in this
996     place with bits from the low-order end of the new value."
997     (multiple-value-bind (dummies vals newval setter getter)
998 wlott 1.13 (get-setf-method place env)
999 wlott 1.10 (if (and (consp bytespec) (eq (car bytespec) 'byte))
1000     (let ((n-size (gensym))
1001     (n-pos (gensym))
1002     (n-new (gensym)))
1003     (values (list* n-size n-pos dummies)
1004     (list* (second bytespec) (third bytespec) vals)
1005     (list n-new)
1006     `(let ((,(car newval) (dpb ,n-new (byte ,n-size ,n-pos)
1007     ,getter)))
1008     ,setter
1009     ,n-new)
1010     `(ldb (byte ,n-size ,n-pos) ,getter)))
1011     (let ((btemp (gensym))
1012     (gnuval (gensym)))
1013     (values (cons btemp dummies)
1014     (cons bytespec vals)
1015     (list gnuval)
1016     `(let ((,(car newval) (dpb ,gnuval ,btemp ,getter)))
1017     ,setter
1018     ,gnuval)
1019     `(ldb ,btemp ,getter))))))
1020 ram 1.1
1021    
1022     (define-setf-method mask-field (bytespec place &environment env)
1023     "The first argument is a byte specifier. The second is any place form
1024     acceptable to SETF. Replaces the specified byte of the number in this place
1025     with bits from the corresponding position in the new value."
1026     (multiple-value-bind (dummies vals newval setter getter)
1027 wlott 1.13 (get-setf-method place env)
1028 ram 1.1 (let ((btemp (gensym))
1029     (gnuval (gensym)))
1030     (values (cons btemp dummies)
1031     (cons bytespec vals)
1032     (list gnuval)
1033     `(let ((,(car newval) (deposit-field ,gnuval ,btemp ,getter)))
1034     ,setter
1035     ,gnuval)
1036     `(mask-field ,btemp ,getter)))))
1037    
1038    
1039     (define-setf-method the (type place &environment env)
1040     (multiple-value-bind (dummies vals newval setter getter)
1041 wlott 1.13 (get-setf-method place env)
1042 ram 1.1 (values dummies
1043     vals
1044     newval
1045     (subst `(the ,type ,(car newval)) (car newval) setter)
1046     `(the ,type ,getter))))
1047    
1048    
1049    
1050     ;;;; CASE, TYPECASE, & Friends.
1051    
1052     (eval-when (compile load eval)
1053    
1054     ;;; CASE-BODY returns code for all the standard "case" macros. Name is the
1055     ;;; macro name, and keyform is the thing to case on. Multi-p indicates whether
1056     ;;; a branch may fire off a list of keys; otherwise, a key that is a list is
1057     ;;; interpreted in some way as a single key. When multi-p, test is applied to
1058     ;;; the value of keyform and each key for a given branch; otherwise, test is
1059     ;;; applied to the value of keyform and the entire first element, instead of
1060     ;;; each part, of the case branch. When errorp, no t or otherwise branch is
1061     ;;; permitted, and an ERROR form is generated. When proceedp, it is an error
1062     ;;; to omit errorp, and the ERROR form generated is executed within a
1063     ;;; RESTART-CASE allowing keyform to be set and retested.
1064     ;;;
1065     (defun case-body (name keyform cases multi-p test errorp proceedp)
1066     (let ((keyform-value (gensym))
1067     (clauses ())
1068     (keys ()))
1069     (dolist (case cases)
1070     (cond ((atom case)
1071     (error "~S -- Bad clause in ~S." case name))
1072     ((memq (car case) '(t otherwise))
1073     (if errorp
1074     (error "No default clause allowed in ~S: ~S" name case)
1075     (push `(t nil ,@(rest case)) clauses)))
1076     ((and multi-p (listp (first case)))
1077     (setf keys (append (first case) keys))
1078     (push `((or ,@(mapcar #'(lambda (key)
1079     `(,test ,keyform-value ',key))
1080     (first case)))
1081     nil ,@(rest case))
1082     clauses))
1083     (t
1084     (push (first case) keys)
1085     (push `((,test ,keyform-value
1086     ',(first case)) nil ,@(rest case)) clauses))))
1087     (case-body-aux name keyform keyform-value clauses keys errorp proceedp
1088     `(,(if multi-p 'member 'or) ,@keys))))
1089    
1090     ;;; CASE-BODY-AUX provides the expansion once CASE-BODY has groveled all the
1091     ;;; cases. Note: it is not necessary that the resulting code signal
1092     ;;; case-failure conditions, but that's what KMP's prototype code did. We call
1093     ;;; CASE-BODY-ERROR, because of how closures are compiled. RESTART-CASE has
1094     ;;; forms with closures that the compiler causes to be generated at the top of
1095     ;;; any function using the case macros, regardless of whether they are needed.
1096     ;;;
1097     (defun case-body-aux (name keyform keyform-value clauses keys
1098     errorp proceedp expected-type)
1099     (if proceedp
1100     (let ((block (gensym))
1101     (again (gensym)))
1102     `(let ((,keyform-value ,keyform))
1103     (block ,block
1104     (tagbody
1105     ,again
1106     (return-from
1107     ,block
1108     (cond ,@(nreverse clauses)
1109     (t
1110     (setf ,keyform-value
1111     (setf ,keyform
1112     (case-body-error
1113     ',name ',keyform ,keyform-value
1114     ',expected-type ',keys)))
1115     (go ,again))))))))
1116     `(let ((,keyform-value ,keyform))
1117     (cond
1118     ,@(nreverse clauses)
1119     ,@(if errorp
1120     `((t (error 'conditions::case-failure
1121     :name ',name
1122     :datum ,keyform-value
1123     :expected-type ',expected-type
1124     :possibilities ',keys))))))))
1125    
1126     ); eval-when
1127    
1128     (defun case-body-error (name keyform keyform-value expected-type keys)
1129     (restart-case
1130     (error 'conditions::case-failure
1131     :name name
1132     :datum keyform-value
1133     :expected-type expected-type
1134     :possibilities keys)
1135     (store-value (value)
1136     :report (lambda (stream)
1137     (format stream "Supply a new value for ~S." keyform))
1138     :interactive read-evaluated-form
1139     value)))
1140    
1141    
1142    
1143     (defmacro case (keyform &body cases)
1144     "CASE Keyform {({(Key*) | Key} Form*)}*
1145     Evaluates the Forms in the first clause with a Key EQL to the value of
1146     Keyform. If a singleton key is T then the clause is a default clause."
1147     (case-body 'case keyform cases t 'eql nil nil))
1148    
1149     (defmacro ccase (keyform &body cases)
1150     "CCASE Keyform {({(Key*) | Key} Form*)}*
1151     Evaluates the Forms in the first clause with a Key EQL to the value of
1152     Keyform. If none of the keys matches then a correctable error is
1153     signalled."
1154     (case-body 'ccase keyform cases t 'eql t t))
1155    
1156     (defmacro ecase (keyform &body cases)
1157     "ECASE Keyform {({(Key*) | Key} Form*)}*
1158     Evaluates the Forms in the first clause with a Key EQL to the value of
1159     Keyform. If none of the keys matches then an error is signalled."
1160     (case-body 'ecase keyform cases t 'eql t nil))
1161    
1162     (defmacro typecase (keyform &body cases)
1163     "TYPECASE Keyform {(Type Form*)}*
1164     Evaluates the Forms in the first clause for which TYPEP of Keyform and Type
1165     is true."
1166     (case-body 'typecase keyform cases nil 'typep nil nil))
1167    
1168     (defmacro ctypecase (keyform &body cases)
1169     "CTYPECASE Keyform {(Type Form*)}*
1170     Evaluates the Forms in the first clause for which TYPEP of Keyform and Type
1171     is true. If no form is satisfied then a correctable error is signalled."
1172     (case-body 'ctypecase keyform cases nil 'typep t t))
1173    
1174     (defmacro etypecase (keyform &body cases)
1175     "ETYPECASE Keyform {(Type Form*)}*
1176     Evaluates the Forms in the first clause for which TYPEP of Keyform and Type
1177     is true. If no form is satisfied then an error is signalled."
1178     (case-body 'etypecase keyform cases nil 'typep t nil))
1179    
1180    
1181     ;;;; ASSERT and CHECK-TYPE.
1182    
1183     ;;; ASSERT is written this way, to call ASSERT-ERROR, because of how closures
1184     ;;; are compiled. RESTART-CASE has forms with closures that the compiler
1185     ;;; causes to be generated at the top of any function using ASSERT, regardless
1186     ;;; of whether they are needed.
1187     ;;;
1188     (defmacro assert (test-form &optional places datum &rest arguments)
1189     "Signals an error if the value of test-form is nil. Continuing from this
1190     error using the CONTINUE restart will allow the user to alter the value of
1191     some locations known to SETF, starting over with test-form. Returns nil."
1192     `(loop
1193     (when ,test-form (return nil))
1194     (assert-error ',test-form ',places ,datum ,@arguments)
1195     ,@(mapcar #'(lambda (place)
1196     `(setf ,place (assert-prompt ',place ,place)))
1197     places)))
1198    
1199     (defun assert-error (test-form places datum &rest arguments)
1200     (restart-case (if datum
1201     (apply #'error datum arguments)
1202     (simple-assertion-failure test-form))
1203     (continue ()
1204     :report (lambda (stream) (assert-report places stream))
1205     nil)))
1206    
1207     (defun simple-assertion-failure (assertion)
1208     (error 'simple-type-error
1209     :datum assertion
1210     :expected-type nil ;this needs some work in next revision. -kmp
1211     :format-string "The assertion ~S failed."
1212     :format-arguments (list assertion)))
1213    
1214     (defun assert-report (names stream)
1215     (format stream "Retry assertion")
1216     (if names
1217     (format stream " with new value~P for ~{~S~^, ~}."
1218     (length names) names)
1219     (format stream ".")))
1220    
1221     (defun assert-prompt (name value)
1222     (cond ((y-or-n-p "The old value of ~S is ~S.~
1223     ~%Do you want to supply a new value? "
1224     name value)
1225     (format *query-io* "~&Type a form to be evaluated:~%")
1226     (flet ((read-it () (eval (read *query-io*))))
1227     (if (symbolp name) ;help user debug lexical variables
1228     (progv (list name) (list value) (read-it))
1229     (read-it))))
1230     (t value)))
1231    
1232    
1233     ;;; CHECK-TYPE is written this way, to call CHECK-TYPE-ERROR, because of how
1234     ;;; closures are compiled. RESTART-CASE has forms with closures that the
1235     ;;; compiler causes to be generated at the top of any function using
1236     ;;; CHECK-TYPE, regardless of whether they are needed. Because it would be
1237     ;;; nice if this were cheap to use, and some things can't afford this excessive
1238     ;;; consing (e.g., READ-CHAR), we bend backwards a little.
1239     ;;;
1240    
1241     (defmacro check-type (place type &optional type-string)
1242     "Signals an error of type type-error if the contents of place are not of the
1243     specified type. If an error is signaled, this can only return if
1244     STORE-VALUE is invoked. It will store into place and start over."
1245     (let ((place-value (gensym)))
1246     `(loop
1247     (let ((,place-value ,place))
1248     (when (typep ,place-value ',type) (return nil))
1249     (setf ,place
1250     (check-type-error ',place ,place-value ',type ,type-string))))))
1251    
1252     (defun check-type-error (place place-value type type-string)
1253     (restart-case (if type-string
1254     (error 'simple-type-error
1255     :datum place :expected-type type
1256     :format-string
1257     "The value of ~S is ~S, which is not ~A."
1258     :format-arguments
1259     (list place place-value type-string))
1260     (error 'simple-type-error
1261     :datum place :expected-type type
1262     :format-string
1263     "The value of ~S is ~S, which is not of type ~S."
1264     :format-arguments
1265     (list place place-value type)))
1266     (store-value (value)
1267     :report (lambda (stream)
1268     (format stream "Supply a new value of ~S."
1269     place))
1270     :interactive read-evaluated-form
1271     value)))
1272    
1273     ;;; READ-EVALUATED-FORM is used as the interactive method for restart cases
1274     ;;; setup by the Common Lisp "casing" (e.g., CCASE and CTYPECASE) macros
1275     ;;; and by CHECK-TYPE.
1276     ;;;
1277     (defun read-evaluated-form ()
1278     (format *query-io* "~&Type a form to be evaluated:~%")
1279     (list (eval (read *query-io*))))
1280    
1281    
1282     ;;;; With-XXX
1283    
1284     (defmacro with-open-file ((var &rest open-args) &body (forms decls))
1285 ram 1.5 "Bindspec is of the form (Stream File-Name . Options). The file whose
1286     name is File-Name is opened using the Options and bound to the variable
1287     Stream. If the call to open is unsuccessful, the forms are not
1288     evaluated. The Forms are executed, and when they terminate, normally or
1289     otherwise, the file is closed."
1290 ram 1.1 (let ((abortp (gensym)))
1291     `(let ((,var (open ,@open-args))
1292     (,abortp t))
1293     ,@decls
1294 ram 1.5 (when ,var
1295     (unwind-protect
1296     (multiple-value-prog1
1297     (progn ,@forms)
1298     (setq ,abortp nil))
1299 ram 1.1 (close ,var :abort ,abortp))))))
1300    
1301    
1302 ram 1.5
1303 ram 1.1 (defmacro with-open-stream ((var stream) &body (forms decls))
1304     "The form stream should evaluate to a stream. VAR is bound
1305     to the stream and the forms are evaluated as an implicit
1306     progn. The stream is closed upon exit."
1307     (let ((abortp (gensym)))
1308     `(let ((,var ,stream)
1309     (,abortp t))
1310     ,@decls
1311     (unwind-protect
1312     (multiple-value-prog1
1313     (progn ,@forms)
1314     (setq ,abortp nil))
1315     (when ,var
1316     (close ,var :abort ,abortp))))))
1317    
1318    
1319     (defmacro with-input-from-string ((var string &key index start end) &body (forms decls))
1320     "Binds the Var to an input stream that returns characters from String and
1321     executes the body. See manual for details."
1322     `(let ((,var
1323     ,(if end
1324     `(make-string-input-stream ,string ,(or start 0) ,end)
1325     `(make-string-input-stream ,string ,(or start 0)))))
1326     ,@decls
1327     (unwind-protect
1328     (progn ,@forms)
1329     (close ,var)
1330     ,@(if index `((setf ,index (string-input-stream-current ,var)))))))
1331    
1332    
1333     (defmacro with-output-to-string ((var &optional string) &body (forms decls))
1334 ram 1.5 "If *string* is specified, it must be a string with a fill pointer;
1335     the output is incrementally appended to the string (as if by use of
1336     VECTOR-PUSH-EXTEND)."
1337 ram 1.1 (if string
1338     `(let ((,var (make-fill-pointer-output-stream ,string)))
1339     ,@decls
1340     (unwind-protect
1341     (progn ,@forms)
1342     (close ,var)))
1343     `(let ((,var (make-string-output-stream)))
1344     ,@decls
1345     (unwind-protect
1346     (progn ,@forms)
1347     (close ,var))
1348     (get-output-stream-string ,var))))
1349    
1350    
1351     ;;;; Iteration macros:
1352    
1353     (defmacro dotimes ((var count &optional (result nil)) &body body)
1354     (cond ((numberp count)
1355     `(do ((,var 0 (1+ ,var)))
1356     ((>= ,var ,count) ,result)
1357 ram 1.8 (declare (type unsigned-byte ,var))
1358 ram 1.1 ,@body))
1359     (t (let ((v1 (gensym)))
1360     `(do ((,var 0 (1+ ,var)) (,v1 ,count))
1361     ((>= ,var ,v1) ,result)
1362 ram 1.8 (declare (type unsigned-byte ,var))
1363 ram 1.1 ,@body)))))
1364    
1365    
1366     ;;; We repeatedly bind the var instead of setting it so that we never give the
1367     ;;; var a random value such as NIL (which might conflict with a declaration).
1368     ;;; ### Might not be legal...
1369     ;;;
1370     (defmacro dolist ((var list &optional (result nil)) &body body)
1371     (let ((n-list (gensym)))
1372     `(do ((,n-list ,list (cdr ,n-list)))
1373     ((endp ,n-list)
1374     (let ((,var nil))
1375     (declare (ignorable ,var))
1376     ,result))
1377     (let ((,var (car ,n-list)))
1378     ,@body))))
1379    
1380    
1381     (defmacro do (varlist endlist &body (body decls))
1382     "DO ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
1383     Iteration construct. Each Var is initialized in parallel to the value of the
1384     specified Init form. On subsequent iterations, the Vars are assigned the
1385     value of the Step form (if any) in paralell. The Test is evaluated before
1386     each evaluation of the body Forms. When the Test is true, the the Exit-Forms
1387     are evaluated as a PROGN, with the result being the value of the DO. A block
1388     named NIL is established around the entire expansion, allowing RETURN to be
1389     used as an laternate exit mechanism."
1390    
1391     (do-do-body varlist endlist body decls 'let 'psetq 'do nil))
1392    
1393    
1394     (defmacro do* (varlist endlist &body (body decls))
1395     "DO* ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
1396     Iteration construct. Each Var is initialized sequentially (like LET*) to the
1397     value of the specified Init form. On subsequent iterations, the Vars are
1398     sequentially assigned the value of the Step form (if any). The Test is
1399     evaluated before each evaluation of the body Forms. When the Test is true,
1400     the the Exit-Forms are evaluated as a PROGN, with the result being the value
1401     of the DO. A block named NIL is established around the entire expansion,
1402     allowing RETURN to be used as an laternate exit mechanism."
1403     (do-do-body varlist endlist body decls 'let* 'setq 'do* nil))
1404    
1405    
1406     ;;;; Miscellaneous macros:
1407    
1408     (defmacro locally (&rest forms)
1409     "A form providing a container for locally-scoped variables."
1410     `(let () ,@forms))
1411    
1412     (defmacro psetq (&rest pairs)
1413     (do ((lets nil)
1414     (setqs nil)
1415     (pairs pairs (cddr pairs)))
1416     ((atom (cdr pairs))
1417     `(let ,(nreverse lets) (setq ,@(nreverse setqs))))
1418     (let ((gen (gensym)))
1419     (push `(,gen ,(cadr pairs)) lets)
1420     (push (car pairs) setqs)
1421     (push gen setqs))))
1422    
1423    
1424     ;;;; With-Compilation-Unit:
1425    
1426     ;;; True if we are within a With-Compilation-Unit form, which normally causes
1427     ;;; nested uses to be NOOPS.
1428     ;;;
1429     (defvar *in-compilation-unit* nil)
1430    
1431     ;;; Count of the number of compilation units dynamically enclosed by the
1432     ;;; current active WITH-COMPILATION-UNIT that were unwound out of.
1433     ;;;
1434     (defvar *aborted-compilation-units*)
1435    
1436 ram 1.21 (declaim (special c::*context-declarations*))
1437    
1438    
1439     ;;; EVALUATE-DECLARATION-CONTEXT -- Internal
1440     ;;;
1441     ;;; Recursively descend the context form, returning true if this subpart
1442     ;;; matches the specified context.
1443     ;;;
1444     (defun evaluate-declaration-context (context name parent)
1445     (let ((base (if (and (consp name) (consp (cdr name)))
1446     (cadr name)
1447     name)))
1448     (if (atom context)
1449     (let ((package (and (symbolp base) (symbol-package base))))
1450     (multiple-value-bind (ignore how)
1451     (if package
1452     (find-symbol (symbol-name base) package)
1453     (values nil nil))
1454     (declare (ignore ignore))
1455     (case context
1456     (:internal (eq how :internal))
1457     (:external (eq how :external))
1458     (:uninterned (and (symbolp base) (not package)))
1459     (:anonymous (not name))
1460     (:macro (eq parent 'defmacro))
1461     (:function (member parent '(defun labels flet function)))
1462     (:global (member parent '(defun defmacro function)))
1463     (:local (member parent '(labels flet)))
1464     (t
1465     (error "Unknown declaration context: ~S." context)))))
1466     (case (first context)
1467     (:or
1468     (loop for x in (rest context)
1469     thereis (evaluate-declaration-context x name parent)))
1470     (:and
1471     (loop for x in (rest context)
1472     always (evaluate-declaration-context x name parent)))
1473     (:not
1474     (evaluate-declaration-context (second context) name parent))
1475     (:member
1476     (member name (rest context) :test #'equal))
1477     (:match
1478     (let ((name (concatenate 'string "$" (string base) "$")))
1479     (loop for x in (rest context)
1480     thereis (search (string x) name))))
1481     (t
1482     (error "Unknown declaration context: ~S." context))))))
1483    
1484    
1485     ;;; PROCESS-CONTEXT-DECLARATIONS -- Internal
1486     ;;;
1487     ;;; Given a list of context declaration specs, return a new value for
1488     ;;; C::*CONTEXT-DECLARATIONS*.
1489     ;;;
1490     (defun process-context-declarations (decls)
1491     (append
1492     (mapcar
1493     #'(lambda (decl)
1494     (unless (>= (length decl) 2)
1495     (error "Context declaration spec should have context and at ~
1496     least one DECLARE form:~% ~S" decl))
1497     #'(lambda (name parent)
1498     (when (evaluate-declaration-context (first decl) name parent)
1499     (rest decl))))
1500     decls)
1501     c::*context-declarations*))
1502    
1503    
1504 ram 1.1 ;;; With-Compilation-Unit -- Public
1505     ;;;
1506     (defmacro with-compilation-unit (options &body body)
1507 ram 1.21 "WITH-COMPILATION-UNIT ({Key Value}*) Form*
1508     This form affects compilations that take place within its dynamic extent. It
1509     is intended to be wrapped around the compilation of all files in the same
1510     system. These keywords are defined:
1511     :OVERRIDE Boolean-Form
1512     One of the effects of this form is to delay undefined warnings
1513     until the end of the form, instead of giving them at the end of each
1514     compilation. If OVERRIDE is NIL (the default), then the outermost
1515     WITH-COMPILATION-UNIT form grabs the undefined warnings. Specifying
1516     OVERRIDE true causes that form to grab any enclosed warnings, even if
1517     it is enclosed by another WITH-COMPILATION-UNIT.
1518     :OPTIMIZE Decl-Form
1519     Decl-Form should evaluate to an OPTIMIZE declaration specifier. This
1520     declaration changes the `global' policy for compilations within the
1521     body.
1522     :OPTIMIZE-INTERFACE Decl-Form
1523     Like OPTIMIZE, except that it specifies the value of the CMU extension
1524     OPTIMIZE-INTERFACE policy (which controls argument type and syntax
1525     checking.)
1526     :CONTEXT-DECLARATIONS List-of-Context-Decls-Form
1527     This is a CMU extension which allows compilation to be controlled
1528     by pattern matching on the context in which a definition appears. The
1529     argument should evaluate to a list of lists of the form:
1530     (Context-Spec Declare-Form+)
1531     In the indicated context, the specified declare forms are inserted at
1532     the head of each definition. The declare forms for all contexts that
1533     match are appended together, with earlier declarations getting
1534     predecence over later ones. A simple example:
1535     :context-declarations
1536     '((:external (declare (optimize (safety 2)))))
1537     This will cause all functions that are named by external symbols to be
1538     compiled with SAFETY 2. The full syntax of context specs is:
1539     :INTERNAL, :EXTERNAL
1540     True if the symbols is internal (external) in its home package.
1541     :UNINTERNED
1542     True if the symbol has no home package.
1543     :ANONYMOUS
1544     True if the function doesn't have any interesting name (not
1545     DEFMACRO, DEFUN, LABELS or FLET).
1546     :MACRO, :FUNCTION
1547     :MACRO is a global (DEFMACRO) macro. :FUNCTION is anything else.
1548     :LOCAL, :GLOBAL
1549     :LOCAL is a LABELS or FLET. :GLOBAL is anything else.
1550     (:OR Context-Spec*)
1551     True in any specified context.
1552     (:AND Context-Spec*)
1553     True only when all specs are true.
1554     (:NOT Context-Spec)
1555     True when the spec is false.
1556     (:MEMBER Name*)
1557     True when the name is one of these names (EQUAL test.)
1558     (:MATCH Pattern*)
1559     True when any of the patterns is a substring of the name. The name
1560     is wrapped with $'s, so $FOO matches names beginning with FOO,
1561     etc."
1562     (let ((override nil)
1563     (optimize nil)
1564     (optimize-interface nil)
1565     (context-declarations nil)
1566 ram 1.1 (n-fun (gensym))
1567     (n-abort-p (gensym)))
1568     (when (oddp (length options))
1569     (error "Odd number of key/value pairs: ~S." options))
1570     (do ((opt options (cddr opt)))
1571     ((null opt))
1572     (case (first opt)
1573 ram 1.21 (:override
1574     (setq override (second opt)))
1575     (:optimize
1576     (setq optimize (second opt)))
1577     (:optimize-interface
1578     (setq optimize-interface (second opt)))
1579     (:context-declarations
1580     (setq context-declarations (second opt)))
1581 ram 1.1 (t
1582     (warn "Ignoring unknown option: ~S." (first opt)))))
1583    
1584 ram 1.21 `(flet ((,n-fun ()
1585     (let (,@(when optimize
1586     `((c::*default-cookie*
1587     (c::process-optimize-declaration
1588     ,optimize c::*default-cookie*))))
1589     ,@(when optimize-interface
1590     `((c::*default-interface-cookie*
1591     (c::process-optimize-declaration
1592     ,optimize-interface
1593     c::*default-interface-cookie*))))
1594     ,@(when context-declarations
1595     `((c::*context-declarations*
1596     (process-context-declarations
1597     ,context-declarations)))))
1598     ,@body)))
1599     (if (or ,override (not *in-compilation-unit*))
1600 ram 1.6 (let ((c::*undefined-warnings* nil)
1601 ram 1.1 (c::*compiler-error-count* 0)
1602     (c::*compiler-warning-count* 0)
1603     (c::*compiler-note-count* 0)
1604     (*in-compilation-unit* t)
1605     (*aborted-compilation-units* 0)
1606     (,n-abort-p t))
1607 ram 1.6 (handler-bind ((c::parse-unknown-type
1608     #'(lambda (c)
1609     (c::note-undefined-reference
1610     (c::parse-unknown-type-specifier c)
1611     :type))))
1612     (unwind-protect
1613     (multiple-value-prog1
1614     (,n-fun)
1615     (setq ,n-abort-p nil))
1616     (c::print-summary ,n-abort-p *aborted-compilation-units*))))
1617 ram 1.1 (let ((,n-abort-p t))
1618     (unwind-protect
1619     (multiple-value-prog1
1620     (,n-fun)
1621     (setq ,n-abort-p nil))
1622     (when ,n-abort-p
1623     (incf *aborted-compilation-units*))))))))

  ViewVC Help
Powered by ViewVC 1.1.5