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

Contents of /src/code/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5