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

Contents of /src/code/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.31 - (hide annotations)
Thu Apr 2 02:01:40 1992 UTC (22 years ago) by wlott
Branch: MAIN
Changes since 1.30: +8 -12 lines
Fixed SETF of GETF to evaluate the various parts in the correct order as
per X3J13 cleanup SETF-SUB-METHODS:DELAYED-ACCESS-STORES.
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 wlott 1.31 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/macros.lisp,v 1.31 1992/04/02 02:01:40 wlott 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 wlott 1.30 (keeper (gensym "KEEPER-")))
447 ram 1.26 ;; We build DUMMY-LIST, a list of variables to bind to useless
448     ;; values, then we explicitly IGNORE those bindings and return
449 wlott 1.30 ;; KEEPER, the only thing we're really interested in right now.
450 ram 1.26 (dotimes (i n)
451 wlott 1.30 (push (gensym "IGNORE-") dummy-list))
452     `(multiple-value-bind (,@dummy-list ,keeper)
453 ram 1.26 ,form
454     (declare (ignore ,@dummy-list))
455 wlott 1.30 ,keeper))
456     (once-only ((n n))
457     `(case (the fixnum ,n)
458     (0 (nth-value 0 ,form))
459     (1 (nth-value 1 ,form))
460     (2 (nth-value 2 ,form))
461     (T (nth (the fixnum ,n) (multiple-value-list ,form)))))))
462 ram 1.1
463    
464     ;;;; SETF and friends.
465    
466     ;;; Note: The expansions for SETF and friends sometimes create needless
467     ;;; LET-bindings of argument values. The compiler will remove most of
468     ;;; these spurious bindings, so SETF doesn't worry too much about creating
469     ;;; them.
470    
471     ;;; The inverse for a generalized-variable reference function is stored in
472     ;;; one of two ways:
473     ;;;
474     ;;; A SETF-INVERSE property corresponds to the short form of DEFSETF. It is
475     ;;; the name of a function takes the same args as the reference form, plus a
476     ;;; new-value arg at the end.
477     ;;;
478     ;;; A SETF-METHOD-EXPANDER property is created by the long form of DEFSETF or
479     ;;; by DEFINE-SETF-METHOD. It is a function that is called on the reference
480     ;;; form and that produces five values: a list of temporary variables, a list
481     ;;; of value forms, a list of the single store-value form, a storing function,
482     ;;; and an accessing function.
483    
484 wlott 1.29 (defun get-setf-method-multiple-value (form &optional environment)
485 ram 1.1 "Returns five values needed by the SETF machinery: a list of temporary
486 wlott 1.29 variables, a list of values with which to fill them, a list of temporaries
487     for the new values, the setting function, and the accessing function."
488 ram 1.1 (let (temp)
489     (cond ((symbolp form)
490     (let ((new-var (gensym)))
491     (values nil nil (list new-var) `(setq ,form ,new-var) form)))
492 ram 1.25 ;;
493     ;; Local functions inhibit global setf methods...
494 ram 1.22 ((and environment
495 ram 1.25 (c::leaf-p (cdr (assoc (car form)
496     (c::lexenv-functions environment)))))
497 ram 1.22 (get-setf-method-inverse form `(funcall #'(setf ,(car form))) t))
498 ram 1.1 ((setq temp (info setf inverse (car form)))
499 wlott 1.10 (get-setf-method-inverse form `(,temp) nil))
500 ram 1.1 ((setq temp (info setf expander (car form)))
501     (funcall temp form environment))
502 ram 1.25 ;;
503     ;; If a macro, expand one level and try again. If not, go for the
504     ;; SETF function.
505 ram 1.1 (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 wlott 1.29 (defun get-setf-method (form &optional environment)
530     "Like Get-Setf-Method-Multiple-Value, but signal an error if there are
531     more than one new-value variables."
532     (multiple-value-bind
533     (temps value-forms store-vars store-form access-form)
534     (get-setf-method-multiple-value form environment)
535     (when (cdr store-vars)
536     (error "GET-SETF-METHOD used for a form with multiple store ~
537     variables:~% ~S" form))
538     (values temps value-forms store-vars store-form access-form)))
539 ram 1.1
540 wlott 1.29
541 wlott 1.16 (defun defsetter (fn rest)
542 wlott 1.14 (let ((arglist (car rest))
543     (arglist-var (gensym "ARGS-"))
544     (new-var (car (cadr rest))))
545     (multiple-value-bind
546     (body local-decs doc)
547 wlott 1.16 (parse-defmacro arglist arglist-var (cddr rest) fn 'defsetf)
548 ram 1.1 (values
549 wlott 1.14 `(lambda (,arglist-var ,new-var)
550     ,@local-decs
551 wlott 1.16 ,body)
552 wlott 1.15 doc))))
553 ram 1.1
554    
555 wlott 1.16 (defmacro defsetf (access-fn &rest rest)
556 ram 1.1 "Associates a SETF update function or macro with the specified access
557     function or macro. The format is complex. See the manual for
558     details."
559     (cond ((not (listp (car rest)))
560     `(eval-when (load compile eval)
561 ram 1.22 (%define-setf-macro ',access-fn nil ',(car rest)
562     ,(when (and (car rest) (stringp (cadr rest)))
563     `',(cadr rest)))))
564 wlott 1.29 ((and (cdr rest) (listp (cadr rest)))
565     (destructuring-bind
566     (lambda-list (&rest store-variables) &body body)
567     rest
568     (let ((arglist-var (gensym "ARGS-"))
569     (access-form-var (gensym "ACCESS-FORM-"))
570     (env-var (gensym "ENVIRONMENT-")))
571     (multiple-value-bind
572     (body local-decs doc)
573     (parse-defmacro `(,lambda-list ,@store-variables)
574     arglist-var body access-fn 'defsetf
575     :annonymousp t)
576     `(eval-when (load compile eval)
577     (%define-setf-macro
578     ',access-fn
579     #'(lambda (,access-form-var ,env-var)
580     (declare (ignore ,env-var))
581     (%defsetf ,access-form-var ,(length store-variables)
582     #'(lambda (,arglist-var)
583     ,@local-decs
584     (block ,access-fn
585     ,body))))
586     nil
587     ',doc))))))
588     (t
589     (error "Ill-formed DEFSETF for ~S." access-fn))))
590 ram 1.1
591 wlott 1.29 (defun %defsetf (orig-access-form num-store-vars expander)
592     (collect ((subform-vars) (store-vars))
593     (let ((subforms (cdr orig-access-form)))
594     (dolist (subform subforms)
595     (declare (ignore subform))
596     (subform-vars (gensym)))
597     (dotimes (i num-store-vars)
598     (store-vars (gensym)))
599     (values (subform-vars)
600     subforms
601     (store-vars)
602     (funcall expander (cons (subform-vars) (store-vars)))
603     `(,(car orig-access-form) ,@(subform-vars))))))
604    
605    
606 ram 1.23 ;;; SETF -- Public
607     ;;;
608     ;;; Except for atoms, we always call GET-SETF-METHOD, since it has some
609     ;;; non-trivial semantics. But when there is a setf inverse, and G-S-M uses
610     ;;; it, then we return a call to the inverse, rather than returning a hairy let
611     ;;; form. This is probably important mainly as a convenince in allowing the
612     ;;; use of setf inverses without the full interpreter.
613     ;;;
614 ram 1.1 (defmacro setf (&rest args &environment env)
615     "Takes pairs of arguments like SETQ. The first is a place and the second
616     is the value that is supposed to go into that place. Returns the last
617     value. The place argument may be any of the access forms for which SETF
618     knows a corresponding setting form."
619 ram 1.22 (let ((nargs (length args)))
620     (cond
621     ((= nargs 2)
622 ram 1.23 (let ((place (first args))
623     (value-form (second args)))
624     (if (atom place)
625     `(setq ,place ,value-form)
626     (multiple-value-bind (dummies vals newval setter getter)
627 wlott 1.29 (get-setf-method-multiple-value place env)
628 ram 1.23 (declare (ignore getter))
629     (let ((inverse (info setf inverse (car place))))
630     (if (and inverse (eq inverse (car setter)))
631     `(,inverse ,@(cdr place) ,value-form)
632 wlott 1.29 `(let* (,@(mapcar #'list dummies vals))
633     (multiple-value-bind ,newval ,value-form
634     ,setter))))))))
635 ram 1.22 ((oddp nargs)
636     (error "Odd number of args to SETF."))
637     (t
638     (do ((a args (cddr a)) (l nil))
639     ((null a) `(progn ,@(nreverse l)))
640     (setq l (cons (list 'setf (car a) (cadr a)) l)))))))
641 ram 1.1
642     (defmacro psetf (&rest args &environment env)
643     "This is to SETF as PSETQ is to SETQ. Args are alternating place
644     expressions and values to go into those places. All of the subforms and
645     values are determined, left to right, and only then are the locations
646     updated. Returns NIL."
647 wlott 1.29 (collect ((let*-bindings) (mv-bindings) (setters))
648     (do ((a args (cddr a)))
649     ((endp a))
650     (if (endp (cdr a))
651     (error "Odd number of args to PSETF."))
652     (multiple-value-bind
653     (dummies vals newval setter getter)
654     (get-setf-method-multiple-value (car a) env)
655     (declare (ignore getter))
656     (let*-bindings (mapcar #'list dummies vals))
657     (mv-bindings (list newval (cadr a)))
658     (setters setter)))
659     (labels ((thunk (let*-bindings mv-bindings)
660     (if let*-bindings
661     `(let* ,(car let*-bindings)
662     (multiple-value-bind ,@(car mv-bindings)
663     ,(thunk (cdr let*-bindings) (cdr mv-bindings))))
664     `(progn ,@(setters) nil))))
665     (thunk (let*-bindings) (mv-bindings)))))
666 ram 1.1
667    
668     (defmacro shiftf (&rest args &environment env)
669     "One or more SETF-style place expressions, followed by a single
670 wlott 1.29 value expression. Evaluates all of the expressions in turn, then
671     assigns the value of each expression to the place on its left,
672     returning the value of the leftmost."
673 ram 1.1 (if (< (length args) 2)
674     (error "Too few argument forms to a SHIFTF."))
675 wlott 1.29 (collect ((let*-bindings) (forms))
676     (do ((first t nil)
677     (a args (cdr a))
678     (prev-store-vars)
679     (prev-setter))
680     ((endp (cdr a))
681     (forms `(multiple-value-bind ,prev-store-vars ,(car a)
682     ,prev-setter)))
683     (multiple-value-bind
684     (temps exprs store-vars setter getter)
685     (get-setf-method-multiple-value (car a) env)
686     (loop
687     for temp in temps
688     for expr in exprs
689     do (let*-bindings `(,temp ,expr)))
690     (forms (if first
691     getter
692     `(multiple-value-bind ,prev-store-vars ,getter
693     ,prev-setter)))
694     (setf prev-store-vars store-vars)
695     (setf prev-setter setter)))
696     `(let* ,(let*-bindings)
697     (multiple-value-prog1
698     ,@(forms)))))
699 ram 1.1
700     (defmacro rotatef (&rest args &environment env)
701     "Takes any number of SETF-style place expressions. Evaluates all of the
702 wlott 1.29 expressions in turn, then assigns to each place the value of the form to
703     its right. The rightmost form gets the value of the leftmost.
704     Returns NIL."
705     (when args
706     (collect ((let*-bindings) (mv-bindings) (setters) (getters))
707     (dolist (arg args)
708     (multiple-value-bind
709     (temps subforms store-vars setter getter)
710     (get-setf-method-multiple-value arg env)
711     (loop
712     for temp in temps
713     for subform in subforms
714     do (let*-bindings `(,temp ,subform)))
715     (mv-bindings store-vars)
716     (setters setter)
717     (getters getter)))
718     (setters nil)
719     (getters (car (getters)))
720     (labels ((thunk (mv-bindings getters)
721     (if mv-bindings
722     `((multiple-value-bind
723     ,(car mv-bindings)
724     ,(car getters)
725     ,@(thunk (cdr mv-bindings) (cdr getters))))
726     (setters))))
727     `(let* ,(let*-bindings)
728     ,@(thunk (mv-bindings) (cdr (getters))))))))
729 ram 1.1
730    
731     (defmacro define-modify-macro (name lambda-list function &optional doc-string)
732     "Creates a new read-modify-write macro like PUSH or INCF."
733     (let ((other-args nil)
734     (rest-arg nil)
735     (env (gensym))
736     (reference (gensym)))
737    
738     ;; Parse out the variable names and rest arg from the lambda list.
739     (do ((ll lambda-list (cdr ll))
740     (arg nil))
741     ((null ll))
742     (setq arg (car ll))
743     (cond ((eq arg '&optional))
744     ((eq arg '&rest)
745     (if (symbolp (cadr ll))
746     (setq rest-arg (cadr ll))
747     (error "Non-symbol &rest arg in definition of ~S." name))
748     (if (null (cddr ll))
749     (return nil)
750     (error "Illegal stuff after &rest arg in Define-Modify-Macro.")))
751     ((memq arg '(&key &allow-other-keys &aux))
752     (error "~S not allowed in Define-Modify-Macro lambda list." arg))
753     ((symbolp arg)
754     (push arg other-args))
755     ((and (listp arg) (symbolp (car arg)))
756     (push (car arg) other-args))
757     (t (error "Illegal stuff in lambda list of Define-Modify-Macro."))))
758     (setq other-args (nreverse other-args))
759     `(defmacro ,name (,reference ,@lambda-list &environment ,env)
760     ,doc-string
761     (multiple-value-bind (dummies vals newval setter getter)
762 wlott 1.13 (get-setf-method ,reference ,env)
763 ram 1.1 (do ((d dummies (cdr d))
764     (v vals (cdr v))
765     (let-list nil (cons (list (car d) (car v)) let-list)))
766     ((null d)
767     (push
768     (list (car newval)
769     ,(if rest-arg
770     `(list* ',function getter ,@other-args ,rest-arg)
771     `(list ',function getter ,@other-args)))
772     let-list)
773     `(let* ,(nreverse let-list)
774     ,setter)))))))
775    
776    
777    
778     (defmacro push (obj place &environment env)
779     "Takes an object and a location holding a list. Conses the object onto
780     the list, returning the modified list."
781     (if (symbolp place)
782     `(setq ,place (cons ,obj ,place))
783     (multiple-value-bind (dummies vals newval setter getter)
784 wlott 1.13 (get-setf-method place env)
785 ram 1.1 (do* ((d dummies (cdr d))
786     (v vals (cdr v))
787     (let-list nil))
788     ((null d)
789     (push (list (car newval) `(cons ,obj ,getter))
790     let-list)
791     `(let* ,(nreverse let-list)
792     ,setter))
793     (push (list (car d) (car v)) let-list)))))
794    
795    
796     (defmacro pushnew (obj place &rest keys &environment env)
797     "Takes an object and a location holding a list. If the object is already
798     in the list, does nothing. Else, conses the object onto the list. Returns
799     NIL. If there is a :TEST keyword, this is used for the comparison."
800     (if (symbolp place)
801     `(setq ,place (adjoin ,obj ,place ,@keys))
802     (multiple-value-bind (dummies vals newval setter getter)
803 wlott 1.13 (get-setf-method place env)
804 ram 1.1 (do* ((d dummies (cdr d))
805     (v vals (cdr v))
806     (let-list nil))
807     ((null d)
808     (push (list (car newval) `(adjoin ,obj ,getter ,@keys))
809     let-list)
810     `(let* ,(nreverse let-list)
811     ,setter))
812     (push (list (car d) (car v)) let-list)))))
813    
814    
815     (defmacro pop (place &environment env)
816     "The argument is a location holding a list. Pops one item off the front
817     of the list and returns it."
818     (if (symbolp place)
819     `(prog1 (car ,place) (setq ,place (cdr ,place)))
820     (multiple-value-bind (dummies vals newval setter getter)
821 wlott 1.13 (get-setf-method place env)
822 ram 1.1 (do* ((d dummies (cdr d))
823     (v vals (cdr v))
824     (let-list nil))
825     ((null d)
826     (push (list (car newval) getter) let-list)
827     `(let* ,(nreverse let-list)
828     (prog1 (car ,(car newval))
829     (setq ,(car newval) (cdr ,(car newval)))
830     ,setter)))
831     (push (list (car d) (car v)) let-list)))))
832    
833    
834     (define-modify-macro incf (&optional (delta 1)) +
835     "The first argument is some location holding a number. This number is
836     incremented by the second argument, DELTA, which defaults to 1.")
837    
838    
839     (define-modify-macro decf (&optional (delta 1)) -
840     "The first argument is some location holding a number. This number is
841     decremented by the second argument, DELTA, which defaults to 1.")
842    
843    
844     (defmacro remf (place indicator &environment env)
845     "Place may be any place expression acceptable to SETF, and is expected
846     to hold a property list or (). This list is destructively altered to
847     remove the property specified by the indicator. Returns T if such a
848     property was present, NIL if not."
849     (multiple-value-bind (dummies vals newval setter getter)
850 wlott 1.13 (get-setf-method place env)
851 ram 1.1 (do* ((d dummies (cdr d))
852     (v vals (cdr v))
853     (let-list nil)
854     (ind-temp (gensym))
855     (local1 (gensym))
856     (local2 (gensym)))
857     ((null d)
858     (push (list (car newval) getter) let-list)
859     (push (list ind-temp indicator) let-list)
860     `(let* ,(nreverse let-list)
861     (do ((,local1 ,(car newval) (cddr ,local1))
862     (,local2 nil ,local1))
863     ((atom ,local1) nil)
864     (cond ((atom (cdr ,local1))
865     (error "Odd-length property list in REMF."))
866     ((eq (car ,local1) ,ind-temp)
867     (cond (,local2
868     (rplacd (cdr ,local2) (cddr ,local1))
869     (return t))
870     (t (setq ,(car newval) (cddr ,(car newval)))
871     ,setter
872     (return t))))))))
873     (push (list (car d) (car v)) let-list))))
874    
875    
876     ;;; The built-in DEFSETFs.
877    
878     (defsetf car %rplaca)
879     (defsetf cdr %rplacd)
880     (defsetf caar (x) (v) `(%rplaca (car ,x) ,v))
881     (defsetf cadr (x) (v) `(%rplaca (cdr ,x) ,v))
882     (defsetf cdar (x) (v) `(%rplacd (car ,x) ,v))
883     (defsetf cddr (x) (v) `(%rplacd (cdr ,x) ,v))
884     (defsetf caaar (x) (v) `(%rplaca (caar ,x) ,v))
885     (defsetf cadar (x) (v) `(%rplaca (cdar ,x) ,v))
886     (defsetf cdaar (x) (v) `(%rplacd (caar ,x) ,v))
887     (defsetf cddar (x) (v) `(%rplacd (cdar ,x) ,v))
888     (defsetf caadr (x) (v) `(%rplaca (cadr ,x) ,v))
889     (defsetf caddr (x) (v) `(%rplaca (cddr ,x) ,v))
890     (defsetf cdadr (x) (v) `(%rplacd (cadr ,x) ,v))
891     (defsetf cdddr (x) (v) `(%rplacd (cddr ,x) ,v))
892     (defsetf caaaar (x) (v) `(%rplaca (caaar ,x) ,v))
893     (defsetf cadaar (x) (v) `(%rplaca (cdaar ,x) ,v))
894     (defsetf cdaaar (x) (v) `(%rplacd (caaar ,x) ,v))
895     (defsetf cddaar (x) (v) `(%rplacd (cdaar ,x) ,v))
896     (defsetf caadar (x) (v) `(%rplaca (cadar ,x) ,v))
897     (defsetf caddar (x) (v) `(%rplaca (cddar ,x) ,v))
898     (defsetf cdadar (x) (v) `(%rplacd (cadar ,x) ,v))
899     (defsetf cdddar (x) (v) `(%rplacd (cddar ,x) ,v))
900     (defsetf caaadr (x) (v) `(%rplaca (caadr ,x) ,v))
901     (defsetf cadadr (x) (v) `(%rplaca (cdadr ,x) ,v))
902     (defsetf cdaadr (x) (v) `(%rplacd (caadr ,x) ,v))
903     (defsetf cddadr (x) (v) `(%rplacd (cdadr ,x) ,v))
904     (defsetf caaddr (x) (v) `(%rplaca (caddr ,x) ,v))
905     (defsetf cadddr (x) (v) `(%rplaca (cdddr ,x) ,v))
906     (defsetf cdaddr (x) (v) `(%rplacd (caddr ,x) ,v))
907     (defsetf cddddr (x) (v) `(%rplacd (cdddr ,x) ,v))
908    
909     (defsetf first %rplaca)
910     (defsetf second (x) (v) `(%rplaca (cdr ,x) ,v))
911     (defsetf third (x) (v) `(%rplaca (cddr ,x) ,v))
912     (defsetf fourth (x) (v) `(%rplaca (cdddr ,x) ,v))
913     (defsetf fifth (x) (v) `(%rplaca (cddddr ,x) ,v))
914     (defsetf sixth (x) (v) `(%rplaca (cdr (cddddr ,x)) ,v))
915     (defsetf seventh (x) (v) `(%rplaca (cddr (cddddr ,x)) ,v))
916     (defsetf eighth (x) (v) `(%rplaca (cdddr (cddddr ,x)) ,v))
917     (defsetf ninth (x) (v) `(%rplaca (cddddr (cddddr ,x)) ,v))
918     (defsetf tenth (x) (v) `(%rplaca (cdr (cddddr (cddddr ,x))) ,v))
919     (defsetf rest %rplacd)
920    
921     (defsetf elt %setelt)
922     (defsetf aref %aset)
923 wlott 1.10 (defsetf row-major-aref %set-row-major-aref)
924 ram 1.1 (defsetf svref %svset)
925     (defsetf char %charset)
926     (defsetf bit %bitset)
927     (defsetf schar %scharset)
928     (defsetf sbit %sbitset)
929 wlott 1.10 (defsetf %array-dimension %set-array-dimension)
930     (defsetf %raw-bits %set-raw-bits)
931 ram 1.1 (defsetf symbol-value set)
932 wlott 1.28 (defsetf symbol-function fset)
933     (defsetf symbol-plist %set-symbol-plist)
934 ram 1.1 (defsetf documentation %set-documentation)
935     (defsetf nth %setnth)
936     (defsetf fill-pointer %set-fill-pointer)
937     (defsetf search-list %set-search-list)
938    
939 wlott 1.10 (defsetf sap-ref-8 %set-sap-ref-8)
940 wlott 1.27 (defsetf signed-sap-ref-8 %set-signed-sap-ref-8)
941 wlott 1.10 (defsetf sap-ref-16 %set-sap-ref-16)
942 wlott 1.27 (defsetf signed-sap-ref-16 %set-signed-sap-ref-16)
943 wlott 1.10 (defsetf sap-ref-32 %set-sap-ref-32)
944 wlott 1.27 (defsetf signed-sap-ref-32 %set-signed-sap-ref-32)
945 wlott 1.10 (defsetf sap-ref-sap %set-sap-ref-sap)
946     (defsetf sap-ref-single %set-sap-ref-single)
947     (defsetf sap-ref-double %set-sap-ref-double)
948 ram 1.1
949     (define-setf-method getf (place prop &optional default &environment env)
950     (multiple-value-bind (temps values stores set get)
951 wlott 1.13 (get-setf-method place env)
952 ram 1.1 (let ((newval (gensym))
953     (ptemp (gensym))
954 wlott 1.31 (def-temp (if default (gensym))))
955     (values `(,@temps ,ptemp ,@(if default `(,def-temp)))
956     `(,@values ,prop ,@(if default `(,default)))
957 ram 1.1 `(,newval)
958 wlott 1.31 `(let ((,(car stores) (%putf ,get ,ptemp ,newval)))
959     ,set
960     ,newval)
961     `(getf ,get ,ptemp ,@(if default `(,def-temp)))))))
962 ram 1.1
963     (define-setf-method get (symbol prop &optional default)
964     (let ((symbol-temp (gensym))
965     (prop-temp (gensym))
966     (def-temp (gensym))
967     (newval (gensym)))
968     (values `(,symbol-temp ,prop-temp ,@(if default `(,def-temp)))
969     `(,symbol ,prop ,@(if default `(,default)))
970     (list newval)
971     `(%put ,symbol-temp ,prop-temp ,newval)
972     `(get ,symbol-temp ,prop-temp ,@(if default `(,def-temp))))))
973    
974     (define-setf-method gethash (key hashtable &optional default)
975     (let ((key-temp (gensym))
976     (hashtable-temp (gensym))
977     (default-temp (gensym))
978     (new-value-temp (gensym)))
979     (values
980     `(,key-temp ,hashtable-temp ,@(if default `(,default-temp)))
981     `(,key ,hashtable ,@(if default `(,default)))
982     `(,new-value-temp)
983     `(%puthash ,key-temp ,hashtable-temp ,new-value-temp)
984     `(gethash ,key-temp ,hashtable-temp ,@(if default `(,default-temp))))))
985    
986     (defsetf subseq (sequence start &optional (end nil)) (v)
987     `(progn (replace ,sequence ,v :start1 ,start :end1 ,end)
988     ,v))
989    
990    
991 ram 1.22 ;;; Evil hack invented by the gnomes of Vassar Street. The function arg must
992     ;;; be constant. Get a setf method for this function, pretending that the
993     ;;; final (list) arg to apply is just a normal arg. If the setting and access
994     ;;; forms produced in this way reference this arg at the end, then just splice
995     ;;; the APPLY back onto the front and the right thing happens.
996     ;;;
997     ;;; We special-case uses functions in the Lisp package so that APPLY AREF works
998     ;;; even though %ASET takes the new-value last. (there is (SETF AREF) as well
999     ;;; as a setf method, etc.)
1000     ;;;
1001 ram 1.1 (define-setf-method apply (function &rest args &environment env)
1002 ram 1.22 (unless (and (listp function)
1003     (= (list-length function) 2)
1004     (eq (first function) 'function)
1005     (symbolp (second function)))
1006     (error "Setf of Apply is only defined for function args like #'symbol."))
1007     (let ((function (second function)))
1008     (multiple-value-bind
1009     (dummies vals newval setter getter)
1010     (if (eq (symbol-package function) (symbol-package 'aref))
1011     (get-setf-method-inverse (cons function args) `((setf ,function)) t)
1012     (get-setf-method (cons function args) env))
1013     (unless (and (eq (car (last args)) (car (last vals)))
1014     (eq (car (last getter)) (car (last dummies)))
1015     (eq (car (last setter)) (car (last dummies))))
1016     (error "Apply of ~S not understood as a location for Setf." function))
1017     (values dummies vals newval
1018     `(apply (function ,(car setter)) ,@(cdr setter))
1019     `(apply (function ,(car getter)) ,@(cdr getter))))))
1020 ram 1.1
1021    
1022 wlott 1.10 ;;; Special-case a BYTE bytespec so that the compiler can recognize it.
1023     ;;;
1024 ram 1.1 (define-setf-method ldb (bytespec place &environment env)
1025     "The first argument is a byte specifier. The second is any place form
1026     acceptable to SETF. Replaces the specified byte of the number in this
1027     place with bits from the low-order end of the new value."
1028     (multiple-value-bind (dummies vals newval setter getter)
1029 wlott 1.13 (get-setf-method place env)
1030 wlott 1.10 (if (and (consp bytespec) (eq (car bytespec) 'byte))
1031     (let ((n-size (gensym))
1032     (n-pos (gensym))
1033     (n-new (gensym)))
1034     (values (list* n-size n-pos dummies)
1035     (list* (second bytespec) (third bytespec) vals)
1036     (list n-new)
1037     `(let ((,(car newval) (dpb ,n-new (byte ,n-size ,n-pos)
1038     ,getter)))
1039     ,setter
1040     ,n-new)
1041     `(ldb (byte ,n-size ,n-pos) ,getter)))
1042     (let ((btemp (gensym))
1043     (gnuval (gensym)))
1044     (values (cons btemp dummies)
1045     (cons bytespec vals)
1046     (list gnuval)
1047     `(let ((,(car newval) (dpb ,gnuval ,btemp ,getter)))
1048     ,setter
1049     ,gnuval)
1050     `(ldb ,btemp ,getter))))))
1051 ram 1.1
1052    
1053     (define-setf-method mask-field (bytespec place &environment env)
1054     "The first argument is a byte specifier. The second is any place form
1055     acceptable to SETF. Replaces the specified byte of the number in this place
1056     with bits from the corresponding position in the new value."
1057     (multiple-value-bind (dummies vals newval setter getter)
1058 wlott 1.13 (get-setf-method place env)
1059 ram 1.1 (let ((btemp (gensym))
1060     (gnuval (gensym)))
1061     (values (cons btemp dummies)
1062     (cons bytespec vals)
1063     (list gnuval)
1064     `(let ((,(car newval) (deposit-field ,gnuval ,btemp ,getter)))
1065     ,setter
1066     ,gnuval)
1067     `(mask-field ,btemp ,getter)))))
1068    
1069    
1070     (define-setf-method the (type place &environment env)
1071     (multiple-value-bind (dummies vals newval setter getter)
1072 wlott 1.13 (get-setf-method place env)
1073 ram 1.1 (values dummies
1074     vals
1075     newval
1076     (subst `(the ,type ,(car newval)) (car newval) setter)
1077     `(the ,type ,getter))))
1078    
1079    
1080     ;;;; CASE, TYPECASE, & Friends.
1081    
1082     (eval-when (compile load eval)
1083    
1084     ;;; CASE-BODY returns code for all the standard "case" macros. Name is the
1085     ;;; macro name, and keyform is the thing to case on. Multi-p indicates whether
1086     ;;; a branch may fire off a list of keys; otherwise, a key that is a list is
1087     ;;; interpreted in some way as a single key. When multi-p, test is applied to
1088     ;;; the value of keyform and each key for a given branch; otherwise, test is
1089     ;;; applied to the value of keyform and the entire first element, instead of
1090     ;;; each part, of the case branch. When errorp, no t or otherwise branch is
1091     ;;; permitted, and an ERROR form is generated. When proceedp, it is an error
1092     ;;; to omit errorp, and the ERROR form generated is executed within a
1093     ;;; RESTART-CASE allowing keyform to be set and retested.
1094     ;;;
1095     (defun case-body (name keyform cases multi-p test errorp proceedp)
1096     (let ((keyform-value (gensym))
1097     (clauses ())
1098     (keys ()))
1099     (dolist (case cases)
1100     (cond ((atom case)
1101     (error "~S -- Bad clause in ~S." case name))
1102     ((memq (car case) '(t otherwise))
1103     (if errorp
1104     (error "No default clause allowed in ~S: ~S" name case)
1105     (push `(t nil ,@(rest case)) clauses)))
1106     ((and multi-p (listp (first case)))
1107     (setf keys (append (first case) keys))
1108     (push `((or ,@(mapcar #'(lambda (key)
1109     `(,test ,keyform-value ',key))
1110     (first case)))
1111     nil ,@(rest case))
1112     clauses))
1113     (t
1114     (push (first case) keys)
1115     (push `((,test ,keyform-value
1116     ',(first case)) nil ,@(rest case)) clauses))))
1117     (case-body-aux name keyform keyform-value clauses keys errorp proceedp
1118     `(,(if multi-p 'member 'or) ,@keys))))
1119    
1120     ;;; CASE-BODY-AUX provides the expansion once CASE-BODY has groveled all the
1121     ;;; cases. Note: it is not necessary that the resulting code signal
1122     ;;; case-failure conditions, but that's what KMP's prototype code did. We call
1123     ;;; CASE-BODY-ERROR, because of how closures are compiled. RESTART-CASE has
1124     ;;; forms with closures that the compiler causes to be generated at the top of
1125     ;;; any function using the case macros, regardless of whether they are needed.
1126     ;;;
1127     (defun case-body-aux (name keyform keyform-value clauses keys
1128     errorp proceedp expected-type)
1129     (if proceedp
1130     (let ((block (gensym))
1131     (again (gensym)))
1132     `(let ((,keyform-value ,keyform))
1133     (block ,block
1134     (tagbody
1135     ,again
1136     (return-from
1137     ,block
1138     (cond ,@(nreverse clauses)
1139     (t
1140     (setf ,keyform-value
1141     (setf ,keyform
1142     (case-body-error
1143     ',name ',keyform ,keyform-value
1144     ',expected-type ',keys)))
1145     (go ,again))))))))
1146     `(let ((,keyform-value ,keyform))
1147     (cond
1148     ,@(nreverse clauses)
1149     ,@(if errorp
1150     `((t (error 'conditions::case-failure
1151     :name ',name
1152     :datum ,keyform-value
1153     :expected-type ',expected-type
1154     :possibilities ',keys))))))))
1155    
1156     ); eval-when
1157    
1158     (defun case-body-error (name keyform keyform-value expected-type keys)
1159     (restart-case
1160     (error 'conditions::case-failure
1161     :name name
1162     :datum keyform-value
1163     :expected-type expected-type
1164     :possibilities keys)
1165     (store-value (value)
1166     :report (lambda (stream)
1167     (format stream "Supply a new value for ~S." keyform))
1168     :interactive read-evaluated-form
1169     value)))
1170    
1171    
1172     (defmacro case (keyform &body cases)
1173     "CASE Keyform {({(Key*) | Key} Form*)}*
1174     Evaluates the Forms in the first clause with a Key EQL to the value of
1175     Keyform. If a singleton key is T then the clause is a default clause."
1176     (case-body 'case keyform cases t 'eql nil nil))
1177    
1178     (defmacro ccase (keyform &body cases)
1179     "CCASE Keyform {({(Key*) | Key} Form*)}*
1180     Evaluates the Forms in the first clause with a Key EQL to the value of
1181     Keyform. If none of the keys matches then a correctable error is
1182     signalled."
1183     (case-body 'ccase keyform cases t 'eql t t))
1184    
1185     (defmacro ecase (keyform &body cases)
1186     "ECASE Keyform {({(Key*) | Key} Form*)}*
1187     Evaluates the Forms in the first clause with a Key EQL to the value of
1188     Keyform. If none of the keys matches then an error is signalled."
1189     (case-body 'ecase keyform cases t 'eql t nil))
1190    
1191     (defmacro typecase (keyform &body cases)
1192     "TYPECASE Keyform {(Type Form*)}*
1193     Evaluates the Forms in the first clause for which TYPEP of Keyform and Type
1194     is true."
1195     (case-body 'typecase keyform cases nil 'typep nil nil))
1196    
1197     (defmacro ctypecase (keyform &body cases)
1198     "CTYPECASE Keyform {(Type Form*)}*
1199     Evaluates the Forms in the first clause for which TYPEP of Keyform and Type
1200     is true. If no form is satisfied then a correctable error is signalled."
1201     (case-body 'ctypecase keyform cases nil 'typep t t))
1202    
1203     (defmacro etypecase (keyform &body cases)
1204     "ETYPECASE Keyform {(Type Form*)}*
1205     Evaluates the Forms in the first clause for which TYPEP of Keyform and Type
1206     is true. If no form is satisfied then an error is signalled."
1207     (case-body 'etypecase keyform cases nil 'typep t nil))
1208    
1209    
1210     ;;;; ASSERT and CHECK-TYPE.
1211    
1212     ;;; ASSERT is written this way, to call ASSERT-ERROR, because of how closures
1213     ;;; are compiled. RESTART-CASE has forms with closures that the compiler
1214     ;;; causes to be generated at the top of any function using ASSERT, regardless
1215     ;;; of whether they are needed.
1216     ;;;
1217     (defmacro assert (test-form &optional places datum &rest arguments)
1218     "Signals an error if the value of test-form is nil. Continuing from this
1219     error using the CONTINUE restart will allow the user to alter the value of
1220     some locations known to SETF, starting over with test-form. Returns nil."
1221     `(loop
1222     (when ,test-form (return nil))
1223     (assert-error ',test-form ',places ,datum ,@arguments)
1224     ,@(mapcar #'(lambda (place)
1225     `(setf ,place (assert-prompt ',place ,place)))
1226     places)))
1227    
1228     (defun assert-error (test-form places datum &rest arguments)
1229     (restart-case (if datum
1230     (apply #'error datum arguments)
1231     (simple-assertion-failure test-form))
1232     (continue ()
1233     :report (lambda (stream) (assert-report places stream))
1234     nil)))
1235    
1236     (defun simple-assertion-failure (assertion)
1237     (error 'simple-type-error
1238     :datum assertion
1239     :expected-type nil ;this needs some work in next revision. -kmp
1240     :format-string "The assertion ~S failed."
1241     :format-arguments (list assertion)))
1242    
1243     (defun assert-report (names stream)
1244     (format stream "Retry assertion")
1245     (if names
1246     (format stream " with new value~P for ~{~S~^, ~}."
1247     (length names) names)
1248     (format stream ".")))
1249    
1250     (defun assert-prompt (name value)
1251     (cond ((y-or-n-p "The old value of ~S is ~S.~
1252     ~%Do you want to supply a new value? "
1253     name value)
1254     (format *query-io* "~&Type a form to be evaluated:~%")
1255     (flet ((read-it () (eval (read *query-io*))))
1256     (if (symbolp name) ;help user debug lexical variables
1257     (progv (list name) (list value) (read-it))
1258     (read-it))))
1259     (t value)))
1260    
1261    
1262     ;;; CHECK-TYPE is written this way, to call CHECK-TYPE-ERROR, because of how
1263     ;;; closures are compiled. RESTART-CASE has forms with closures that the
1264     ;;; compiler causes to be generated at the top of any function using
1265     ;;; CHECK-TYPE, regardless of whether they are needed. Because it would be
1266     ;;; nice if this were cheap to use, and some things can't afford this excessive
1267     ;;; consing (e.g., READ-CHAR), we bend backwards a little.
1268     ;;;
1269    
1270     (defmacro check-type (place type &optional type-string)
1271     "Signals an error of type type-error if the contents of place are not of the
1272     specified type. If an error is signaled, this can only return if
1273     STORE-VALUE is invoked. It will store into place and start over."
1274     (let ((place-value (gensym)))
1275     `(loop
1276     (let ((,place-value ,place))
1277     (when (typep ,place-value ',type) (return nil))
1278     (setf ,place
1279     (check-type-error ',place ,place-value ',type ,type-string))))))
1280    
1281     (defun check-type-error (place place-value type type-string)
1282     (restart-case (if type-string
1283     (error 'simple-type-error
1284     :datum place :expected-type type
1285     :format-string
1286     "The value of ~S is ~S, which is not ~A."
1287     :format-arguments
1288     (list place place-value type-string))
1289     (error 'simple-type-error
1290     :datum place :expected-type type
1291     :format-string
1292     "The value of ~S is ~S, which is not of type ~S."
1293     :format-arguments
1294     (list place place-value type)))
1295     (store-value (value)
1296     :report (lambda (stream)
1297     (format stream "Supply a new value of ~S."
1298     place))
1299     :interactive read-evaluated-form
1300     value)))
1301    
1302     ;;; READ-EVALUATED-FORM is used as the interactive method for restart cases
1303     ;;; setup by the Common Lisp "casing" (e.g., CCASE and CTYPECASE) macros
1304     ;;; and by CHECK-TYPE.
1305     ;;;
1306     (defun read-evaluated-form ()
1307     (format *query-io* "~&Type a form to be evaluated:~%")
1308     (list (eval (read *query-io*))))
1309    
1310    
1311     ;;;; With-XXX
1312    
1313     (defmacro with-open-file ((var &rest open-args) &body (forms decls))
1314 ram 1.5 "Bindspec is of the form (Stream File-Name . Options). The file whose
1315     name is File-Name is opened using the Options and bound to the variable
1316     Stream. If the call to open is unsuccessful, the forms are not
1317     evaluated. The Forms are executed, and when they terminate, normally or
1318     otherwise, the file is closed."
1319 ram 1.1 (let ((abortp (gensym)))
1320     `(let ((,var (open ,@open-args))
1321     (,abortp t))
1322     ,@decls
1323 ram 1.5 (when ,var
1324     (unwind-protect
1325     (multiple-value-prog1
1326     (progn ,@forms)
1327     (setq ,abortp nil))
1328 ram 1.1 (close ,var :abort ,abortp))))))
1329    
1330    
1331 ram 1.5
1332 ram 1.1 (defmacro with-open-stream ((var stream) &body (forms decls))
1333     "The form stream should evaluate to a stream. VAR is bound
1334     to the stream and the forms are evaluated as an implicit
1335     progn. The stream is closed upon exit."
1336     (let ((abortp (gensym)))
1337     `(let ((,var ,stream)
1338     (,abortp t))
1339     ,@decls
1340     (unwind-protect
1341     (multiple-value-prog1
1342     (progn ,@forms)
1343     (setq ,abortp nil))
1344     (when ,var
1345     (close ,var :abort ,abortp))))))
1346    
1347    
1348     (defmacro with-input-from-string ((var string &key index start end) &body (forms decls))
1349     "Binds the Var to an input stream that returns characters from String and
1350     executes the body. See manual for details."
1351     `(let ((,var
1352     ,(if end
1353     `(make-string-input-stream ,string ,(or start 0) ,end)
1354     `(make-string-input-stream ,string ,(or start 0)))))
1355     ,@decls
1356     (unwind-protect
1357     (progn ,@forms)
1358     (close ,var)
1359     ,@(if index `((setf ,index (string-input-stream-current ,var)))))))
1360    
1361    
1362     (defmacro with-output-to-string ((var &optional string) &body (forms decls))
1363 ram 1.5 "If *string* is specified, it must be a string with a fill pointer;
1364     the output is incrementally appended to the string (as if by use of
1365     VECTOR-PUSH-EXTEND)."
1366 ram 1.1 (if string
1367     `(let ((,var (make-fill-pointer-output-stream ,string)))
1368     ,@decls
1369     (unwind-protect
1370     (progn ,@forms)
1371     (close ,var)))
1372     `(let ((,var (make-string-output-stream)))
1373     ,@decls
1374     (unwind-protect
1375     (progn ,@forms)
1376     (close ,var))
1377     (get-output-stream-string ,var))))
1378    
1379    
1380     ;;;; Iteration macros:
1381    
1382     (defmacro dotimes ((var count &optional (result nil)) &body body)
1383     (cond ((numberp count)
1384     `(do ((,var 0 (1+ ,var)))
1385     ((>= ,var ,count) ,result)
1386 ram 1.8 (declare (type unsigned-byte ,var))
1387 ram 1.1 ,@body))
1388     (t (let ((v1 (gensym)))
1389     `(do ((,var 0 (1+ ,var)) (,v1 ,count))
1390     ((>= ,var ,v1) ,result)
1391 ram 1.8 (declare (type unsigned-byte ,var))
1392 ram 1.1 ,@body)))))
1393    
1394    
1395     ;;; We repeatedly bind the var instead of setting it so that we never give the
1396     ;;; var a random value such as NIL (which might conflict with a declaration).
1397     ;;; ### Might not be legal...
1398     ;;;
1399     (defmacro dolist ((var list &optional (result nil)) &body body)
1400     (let ((n-list (gensym)))
1401     `(do ((,n-list ,list (cdr ,n-list)))
1402     ((endp ,n-list)
1403     (let ((,var nil))
1404     (declare (ignorable ,var))
1405     ,result))
1406     (let ((,var (car ,n-list)))
1407     ,@body))))
1408    
1409    
1410     (defmacro do (varlist endlist &body (body decls))
1411     "DO ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
1412     Iteration construct. Each Var is initialized in parallel to the value of the
1413     specified Init form. On subsequent iterations, the Vars are assigned the
1414     value of the Step form (if any) in paralell. The Test is evaluated before
1415     each evaluation of the body Forms. When the Test is true, the the Exit-Forms
1416     are evaluated as a PROGN, with the result being the value of the DO. A block
1417     named NIL is established around the entire expansion, allowing RETURN to be
1418     used as an laternate exit mechanism."
1419    
1420     (do-do-body varlist endlist body decls 'let 'psetq 'do nil))
1421    
1422    
1423     (defmacro do* (varlist endlist &body (body decls))
1424     "DO* ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
1425     Iteration construct. Each Var is initialized sequentially (like LET*) to the
1426     value of the specified Init form. On subsequent iterations, the Vars are
1427     sequentially assigned the value of the Step form (if any). The Test is
1428     evaluated before each evaluation of the body Forms. When the Test is true,
1429     the the Exit-Forms are evaluated as a PROGN, with the result being the value
1430     of the DO. A block named NIL is established around the entire expansion,
1431     allowing RETURN to be used as an laternate exit mechanism."
1432     (do-do-body varlist endlist body decls 'let* 'setq 'do* nil))
1433    
1434    
1435     ;;;; Miscellaneous macros:
1436    
1437     (defmacro locally (&rest forms)
1438     "A form providing a container for locally-scoped variables."
1439     `(let () ,@forms))
1440    
1441     (defmacro psetq (&rest pairs)
1442     (do ((lets nil)
1443     (setqs nil)
1444     (pairs pairs (cddr pairs)))
1445     ((atom (cdr pairs))
1446     `(let ,(nreverse lets) (setq ,@(nreverse setqs))))
1447     (let ((gen (gensym)))
1448     (push `(,gen ,(cadr pairs)) lets)
1449     (push (car pairs) setqs)
1450     (push gen setqs))))
1451    
1452    
1453     ;;;; With-Compilation-Unit:
1454    
1455     ;;; True if we are within a With-Compilation-Unit form, which normally causes
1456     ;;; nested uses to be NOOPS.
1457     ;;;
1458     (defvar *in-compilation-unit* nil)
1459    
1460     ;;; Count of the number of compilation units dynamically enclosed by the
1461     ;;; current active WITH-COMPILATION-UNIT that were unwound out of.
1462     ;;;
1463     (defvar *aborted-compilation-units*)
1464    
1465 ram 1.21 (declaim (special c::*context-declarations*))
1466    
1467    
1468     ;;; EVALUATE-DECLARATION-CONTEXT -- Internal
1469     ;;;
1470     ;;; Recursively descend the context form, returning true if this subpart
1471     ;;; matches the specified context.
1472     ;;;
1473     (defun evaluate-declaration-context (context name parent)
1474 ram 1.24 (let* ((base (if (and (consp name) (consp (cdr name)))
1475     (cadr name)
1476     name))
1477     (package (and (symbolp base) (symbol-package base))))
1478 ram 1.21 (if (atom context)
1479 ram 1.24 (multiple-value-bind (ignore how)
1480     (if package
1481     (find-symbol (symbol-name base) package)
1482     (values nil nil))
1483     (declare (ignore ignore))
1484     (case context
1485     (:internal (eq how :internal))
1486     (:external (eq how :external))
1487     (:uninterned (and (symbolp base) (not package)))
1488     (:anonymous (not name))
1489     (:macro (eq parent 'defmacro))
1490     (:function (member parent '(defun labels flet function)))
1491     (:global (member parent '(defun defmacro function)))
1492     (:local (member parent '(labels flet)))
1493     (t
1494     (error "Unknown declaration context: ~S." context))))
1495 ram 1.21 (case (first context)
1496     (:or
1497     (loop for x in (rest context)
1498     thereis (evaluate-declaration-context x name parent)))
1499     (:and
1500     (loop for x in (rest context)
1501     always (evaluate-declaration-context x name parent)))
1502     (:not
1503     (evaluate-declaration-context (second context) name parent))
1504     (:member
1505     (member name (rest context) :test #'equal))
1506     (:match
1507     (let ((name (concatenate 'string "$" (string base) "$")))
1508     (loop for x in (rest context)
1509     thereis (search (string x) name))))
1510 ram 1.24 (:package
1511     (and package
1512     (loop for x in (rest context)
1513     thereis (eq (find-package (string x)) package))))
1514 ram 1.21 (t
1515     (error "Unknown declaration context: ~S." context))))))
1516    
1517    
1518     ;;; PROCESS-CONTEXT-DECLARATIONS -- Internal
1519     ;;;
1520     ;;; Given a list of context declaration specs, return a new value for
1521     ;;; C::*CONTEXT-DECLARATIONS*.
1522     ;;;
1523     (defun process-context-declarations (decls)
1524     (append
1525     (mapcar
1526     #'(lambda (decl)
1527     (unless (>= (length decl) 2)
1528     (error "Context declaration spec should have context and at ~
1529     least one DECLARE form:~% ~S" decl))
1530     #'(lambda (name parent)
1531     (when (evaluate-declaration-context (first decl) name parent)
1532     (rest decl))))
1533     decls)
1534     c::*context-declarations*))
1535    
1536    
1537 ram 1.1 ;;; With-Compilation-Unit -- Public
1538     ;;;
1539     (defmacro with-compilation-unit (options &body body)
1540 ram 1.21 "WITH-COMPILATION-UNIT ({Key Value}*) Form*
1541     This form affects compilations that take place within its dynamic extent. It
1542     is intended to be wrapped around the compilation of all files in the same
1543     system. These keywords are defined:
1544     :OVERRIDE Boolean-Form
1545     One of the effects of this form is to delay undefined warnings
1546     until the end of the form, instead of giving them at the end of each
1547     compilation. If OVERRIDE is NIL (the default), then the outermost
1548     WITH-COMPILATION-UNIT form grabs the undefined warnings. Specifying
1549     OVERRIDE true causes that form to grab any enclosed warnings, even if
1550     it is enclosed by another WITH-COMPILATION-UNIT.
1551     :OPTIMIZE Decl-Form
1552     Decl-Form should evaluate to an OPTIMIZE declaration specifier. This
1553     declaration changes the `global' policy for compilations within the
1554     body.
1555     :OPTIMIZE-INTERFACE Decl-Form
1556     Like OPTIMIZE, except that it specifies the value of the CMU extension
1557     OPTIMIZE-INTERFACE policy (which controls argument type and syntax
1558     checking.)
1559     :CONTEXT-DECLARATIONS List-of-Context-Decls-Form
1560     This is a CMU extension which allows compilation to be controlled
1561     by pattern matching on the context in which a definition appears. The
1562     argument should evaluate to a list of lists of the form:
1563     (Context-Spec Declare-Form+)
1564     In the indicated context, the specified declare forms are inserted at
1565     the head of each definition. The declare forms for all contexts that
1566     match are appended together, with earlier declarations getting
1567     predecence over later ones. A simple example:
1568     :context-declarations
1569     '((:external (declare (optimize (safety 2)))))
1570     This will cause all functions that are named by external symbols to be
1571     compiled with SAFETY 2. The full syntax of context specs is:
1572     :INTERNAL, :EXTERNAL
1573     True if the symbols is internal (external) in its home package.
1574     :UNINTERNED
1575     True if the symbol has no home package.
1576     :ANONYMOUS
1577     True if the function doesn't have any interesting name (not
1578     DEFMACRO, DEFUN, LABELS or FLET).
1579     :MACRO, :FUNCTION
1580     :MACRO is a global (DEFMACRO) macro. :FUNCTION is anything else.
1581     :LOCAL, :GLOBAL
1582     :LOCAL is a LABELS or FLET. :GLOBAL is anything else.
1583     (:OR Context-Spec*)
1584     True in any specified context.
1585     (:AND Context-Spec*)
1586     True only when all specs are true.
1587     (:NOT Context-Spec)
1588     True when the spec is false.
1589     (:MEMBER Name*)
1590     True when the name is one of these names (EQUAL test.)
1591     (:MATCH Pattern*)
1592     True when any of the patterns is a substring of the name. The name
1593     is wrapped with $'s, so $FOO matches names beginning with FOO,
1594     etc."
1595     (let ((override nil)
1596     (optimize nil)
1597     (optimize-interface nil)
1598     (context-declarations nil)
1599 ram 1.1 (n-fun (gensym))
1600     (n-abort-p (gensym)))
1601     (when (oddp (length options))
1602     (error "Odd number of key/value pairs: ~S." options))
1603     (do ((opt options (cddr opt)))
1604     ((null opt))
1605     (case (first opt)
1606 ram 1.21 (:override
1607     (setq override (second opt)))
1608     (:optimize
1609     (setq optimize (second opt)))
1610     (:optimize-interface
1611     (setq optimize-interface (second opt)))
1612     (:context-declarations
1613     (setq context-declarations (second opt)))
1614 ram 1.1 (t
1615     (warn "Ignoring unknown option: ~S." (first opt)))))
1616    
1617 ram 1.21 `(flet ((,n-fun ()
1618     (let (,@(when optimize
1619     `((c::*default-cookie*
1620     (c::process-optimize-declaration
1621     ,optimize c::*default-cookie*))))
1622     ,@(when optimize-interface
1623     `((c::*default-interface-cookie*
1624     (c::process-optimize-declaration
1625     ,optimize-interface
1626     c::*default-interface-cookie*))))
1627     ,@(when context-declarations
1628     `((c::*context-declarations*
1629     (process-context-declarations
1630     ,context-declarations)))))
1631     ,@body)))
1632     (if (or ,override (not *in-compilation-unit*))
1633 ram 1.6 (let ((c::*undefined-warnings* nil)
1634 ram 1.1 (c::*compiler-error-count* 0)
1635     (c::*compiler-warning-count* 0)
1636     (c::*compiler-note-count* 0)
1637     (*in-compilation-unit* t)
1638     (*aborted-compilation-units* 0)
1639     (,n-abort-p t))
1640 ram 1.6 (handler-bind ((c::parse-unknown-type
1641     #'(lambda (c)
1642     (c::note-undefined-reference
1643     (c::parse-unknown-type-specifier c)
1644     :type))))
1645     (unwind-protect
1646     (multiple-value-prog1
1647     (,n-fun)
1648     (setq ,n-abort-p nil))
1649     (c::print-summary ,n-abort-p *aborted-compilation-units*))))
1650 ram 1.1 (let ((,n-abort-p t))
1651     (unwind-protect
1652     (multiple-value-prog1
1653     (,n-fun)
1654     (setq ,n-abort-p nil))
1655     (when ,n-abort-p
1656     (incf *aborted-compilation-units*))))))))

  ViewVC Help
Powered by ViewVC 1.1.5