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

Contents of /src/code/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5