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

Contents of /src/code/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5