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

Contents of /src/code/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5