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

Contents of /src/code/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5