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

Contents of /src/code/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5