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

Contents of /src/code/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.97 - (hide annotations)
Fri Aug 8 11:32:52 2003 UTC (10 years, 8 months ago) by emarsden
Branch: MAIN
CVS Tags: snapshot-2003-10, dynamic-extent-base, mod-arith-base, sparc_gencgc_merge, snapshot-2003-11, snapshot-2003-12, snapshot-2004-04, lisp-executable-base
Branch point for: mod-arith-branch, dynamic-extent, lisp-executable
Changes since 1.96: +10 -4 lines
Add a function EXT:UNLOCK-ALL-PACKAGES, that disables both the
structural and the definition locks for all existing packages. Add an
additional restart to the PACKAGE-LOCKED-ERROR signaling points that
allows the user to disable all packages then continue.

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

  ViewVC Help
Powered by ViewVC 1.1.5