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

Contents of /src/code/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5