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

Contents of /src/code/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.114 - (hide annotations)
Thu Mar 18 16:43:11 2010 UTC (4 years, 1 month ago) by rtoy
Branch: MAIN
CVS Tags: pre-merge-intl-branch
Changes since 1.113: +2 -2 lines
Make CMUCL signal a cerror if we try to redefine a slot accessor.  If
continued, the accessor is redefined.  Previously, a warning was
printed and the structure was (mostly) undefined.

compiler/proclaim.lisp:
o Add new function NOTE-IF-ACCESSOR to check if we're redefining a
  slot accessor.  If so, signal a cerror, and redefine if continued.
o Adjust DEFINE-FUNCTION-NAME to call NOTE-IF-ACCESSOR.

compiler/main.lisp:
o Make COMPILE-FIX-FUNCTION-NAME call NOTE-IF-ACCESSOR to catch
  attempts to redefine a slot-accessor.

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

  ViewVC Help
Powered by ViewVC 1.1.5