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

Contents of /src/code/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5