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

Contents of /src/code/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.98.2.2 - (hide annotations)
Tue May 18 14:36:56 2004 UTC (9 years, 11 months ago) by rtoy
Branch: release-19a-branch
CVS Tags: release-19a-pre3, release-19a-pre2, release-19a
Changes since 1.98.2.1: +4 -4 lines
Merge fixes from HEAD for MULTIPLE-VALUE-BIND.
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.98.2.2 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/macros.lisp,v 1.98.2.2 2004/05/18 14:36:56 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.98.2.1 (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.98.2.1 (c::%%defconstant name value doc nil))
403 ram 1.1 ;;;
404 rtoy 1.98.2.1 (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.98.2.1 (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.98.2.1 (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.98.2.1 (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.98.2.2 (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.98.2.2 (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.98.2.2 `(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     (define-modify-macro incf (&optional (delta 1)) +
1015     "The first argument is some location holding a number. This number is
1016     incremented by the second argument, DELTA, which defaults to 1.")
1017    
1018    
1019     (define-modify-macro decf (&optional (delta 1)) -
1020     "The first argument is some location holding a number. This number is
1021     decremented by the second argument, DELTA, which defaults to 1.")
1022    
1023    
1024     (defmacro remf (place indicator &environment env)
1025     "Place may be any place expression acceptable to SETF, and is expected
1026     to hold a property list or (). This list is destructively altered to
1027     remove the property specified by the indicator. Returns T if such a
1028     property was present, NIL if not."
1029     (multiple-value-bind (dummies vals newval setter getter)
1030 wlott 1.13 (get-setf-method place env)
1031 ram 1.1 (do* ((d dummies (cdr d))
1032     (v vals (cdr v))
1033     (let-list nil)
1034     (ind-temp (gensym))
1035     (local1 (gensym))
1036     (local2 (gensym)))
1037     ((null d)
1038     (push (list (car newval) getter) let-list)
1039     (push (list ind-temp indicator) let-list)
1040     `(let* ,(nreverse let-list)
1041     (do ((,local1 ,(car newval) (cddr ,local1))
1042     (,local2 nil ,local1))
1043     ((atom ,local1) nil)
1044     (cond ((atom (cdr ,local1))
1045     (error "Odd-length property list in REMF."))
1046     ((eq (car ,local1) ,ind-temp)
1047     (cond (,local2
1048     (rplacd (cdr ,local2) (cddr ,local1))
1049     (return t))
1050     (t (setq ,(car newval) (cddr ,(car newval)))
1051     ,setter
1052     (return t))))))))
1053     (push (list (car d) (car v)) let-list))))
1054    
1055    
1056     ;;; The built-in DEFSETFs.
1057    
1058     (defsetf car %rplaca)
1059     (defsetf cdr %rplacd)
1060     (defsetf caar (x) (v) `(%rplaca (car ,x) ,v))
1061     (defsetf cadr (x) (v) `(%rplaca (cdr ,x) ,v))
1062     (defsetf cdar (x) (v) `(%rplacd (car ,x) ,v))
1063     (defsetf cddr (x) (v) `(%rplacd (cdr ,x) ,v))
1064     (defsetf caaar (x) (v) `(%rplaca (caar ,x) ,v))
1065     (defsetf cadar (x) (v) `(%rplaca (cdar ,x) ,v))
1066     (defsetf cdaar (x) (v) `(%rplacd (caar ,x) ,v))
1067     (defsetf cddar (x) (v) `(%rplacd (cdar ,x) ,v))
1068     (defsetf caadr (x) (v) `(%rplaca (cadr ,x) ,v))
1069     (defsetf caddr (x) (v) `(%rplaca (cddr ,x) ,v))
1070     (defsetf cdadr (x) (v) `(%rplacd (cadr ,x) ,v))
1071     (defsetf cdddr (x) (v) `(%rplacd (cddr ,x) ,v))
1072     (defsetf caaaar (x) (v) `(%rplaca (caaar ,x) ,v))
1073     (defsetf cadaar (x) (v) `(%rplaca (cdaar ,x) ,v))
1074     (defsetf cdaaar (x) (v) `(%rplacd (caaar ,x) ,v))
1075     (defsetf cddaar (x) (v) `(%rplacd (cdaar ,x) ,v))
1076     (defsetf caadar (x) (v) `(%rplaca (cadar ,x) ,v))
1077     (defsetf caddar (x) (v) `(%rplaca (cddar ,x) ,v))
1078     (defsetf cdadar (x) (v) `(%rplacd (cadar ,x) ,v))
1079     (defsetf cdddar (x) (v) `(%rplacd (cddar ,x) ,v))
1080     (defsetf caaadr (x) (v) `(%rplaca (caadr ,x) ,v))
1081     (defsetf cadadr (x) (v) `(%rplaca (cdadr ,x) ,v))
1082     (defsetf cdaadr (x) (v) `(%rplacd (caadr ,x) ,v))
1083     (defsetf cddadr (x) (v) `(%rplacd (cdadr ,x) ,v))
1084     (defsetf caaddr (x) (v) `(%rplaca (caddr ,x) ,v))
1085     (defsetf cadddr (x) (v) `(%rplaca (cdddr ,x) ,v))
1086     (defsetf cdaddr (x) (v) `(%rplacd (caddr ,x) ,v))
1087     (defsetf cddddr (x) (v) `(%rplacd (cdddr ,x) ,v))
1088    
1089     (defsetf first %rplaca)
1090     (defsetf second (x) (v) `(%rplaca (cdr ,x) ,v))
1091     (defsetf third (x) (v) `(%rplaca (cddr ,x) ,v))
1092     (defsetf fourth (x) (v) `(%rplaca (cdddr ,x) ,v))
1093     (defsetf fifth (x) (v) `(%rplaca (cddddr ,x) ,v))
1094     (defsetf sixth (x) (v) `(%rplaca (cdr (cddddr ,x)) ,v))
1095     (defsetf seventh (x) (v) `(%rplaca (cddr (cddddr ,x)) ,v))
1096     (defsetf eighth (x) (v) `(%rplaca (cdddr (cddddr ,x)) ,v))
1097     (defsetf ninth (x) (v) `(%rplaca (cddddr (cddddr ,x)) ,v))
1098     (defsetf tenth (x) (v) `(%rplaca (cdr (cddddr (cddddr ,x))) ,v))
1099     (defsetf rest %rplacd)
1100    
1101     (defsetf elt %setelt)
1102     (defsetf aref %aset)
1103 wlott 1.10 (defsetf row-major-aref %set-row-major-aref)
1104 ram 1.1 (defsetf svref %svset)
1105     (defsetf char %charset)
1106     (defsetf bit %bitset)
1107     (defsetf schar %scharset)
1108     (defsetf sbit %sbitset)
1109 wlott 1.10 (defsetf %array-dimension %set-array-dimension)
1110     (defsetf %raw-bits %set-raw-bits)
1111 ram 1.1 (defsetf symbol-value set)
1112 wlott 1.28 (defsetf symbol-function fset)
1113     (defsetf symbol-plist %set-symbol-plist)
1114 ram 1.1 (defsetf nth %setnth)
1115     (defsetf fill-pointer %set-fill-pointer)
1116     (defsetf search-list %set-search-list)
1117    
1118 wlott 1.10 (defsetf sap-ref-8 %set-sap-ref-8)
1119 wlott 1.27 (defsetf signed-sap-ref-8 %set-signed-sap-ref-8)
1120 wlott 1.10 (defsetf sap-ref-16 %set-sap-ref-16)
1121 wlott 1.27 (defsetf signed-sap-ref-16 %set-signed-sap-ref-16)
1122 wlott 1.10 (defsetf sap-ref-32 %set-sap-ref-32)
1123 wlott 1.27 (defsetf signed-sap-ref-32 %set-signed-sap-ref-32)
1124 hallgren 1.44 (defsetf sap-ref-64 %set-sap-ref-64)
1125     (defsetf signed-sap-ref-64 %set-signed-sap-ref-64)
1126 wlott 1.10 (defsetf sap-ref-sap %set-sap-ref-sap)
1127     (defsetf sap-ref-single %set-sap-ref-single)
1128     (defsetf sap-ref-double %set-sap-ref-double)
1129 dtc 1.54 #+long-float
1130     (defsetf sap-ref-long %set-sap-ref-long)
1131 ram 1.1
1132 dtc 1.58 (define-setf-expander getf (place prop &optional default &environment env)
1133 ram 1.1 (multiple-value-bind (temps values stores set get)
1134 wlott 1.13 (get-setf-method place env)
1135 ram 1.1 (let ((newval (gensym))
1136     (ptemp (gensym))
1137 wlott 1.31 (def-temp (if default (gensym))))
1138     (values `(,@temps ,ptemp ,@(if default `(,def-temp)))
1139     `(,@values ,prop ,@(if default `(,default)))
1140 ram 1.1 `(,newval)
1141 wlott 1.31 `(let ((,(car stores) (%putf ,get ,ptemp ,newval)))
1142     ,set
1143     ,newval)
1144     `(getf ,get ,ptemp ,@(if default `(,def-temp)))))))
1145 ram 1.1
1146 dtc 1.58 (define-setf-expander get (symbol prop &optional default)
1147 ram 1.1 (let ((symbol-temp (gensym))
1148     (prop-temp (gensym))
1149     (def-temp (gensym))
1150     (newval (gensym)))
1151     (values `(,symbol-temp ,prop-temp ,@(if default `(,def-temp)))
1152     `(,symbol ,prop ,@(if default `(,default)))
1153     (list newval)
1154     `(%put ,symbol-temp ,prop-temp ,newval)
1155     `(get ,symbol-temp ,prop-temp ,@(if default `(,def-temp))))))
1156    
1157 dtc 1.58 (define-setf-expander gethash (key hashtable &optional default)
1158 ram 1.1 (let ((key-temp (gensym))
1159     (hashtable-temp (gensym))
1160     (default-temp (gensym))
1161     (new-value-temp (gensym)))
1162     (values
1163     `(,key-temp ,hashtable-temp ,@(if default `(,default-temp)))
1164     `(,key ,hashtable ,@(if default `(,default)))
1165     `(,new-value-temp)
1166     `(%puthash ,key-temp ,hashtable-temp ,new-value-temp)
1167     `(gethash ,key-temp ,hashtable-temp ,@(if default `(,default-temp))))))
1168    
1169     (defsetf subseq (sequence start &optional (end nil)) (v)
1170     `(progn (replace ,sequence ,v :start1 ,start :end1 ,end)
1171     ,v))
1172    
1173    
1174 ram 1.36 ;;; Evil hack invented by the gnomes of Vassar Street (though not as evil as
1175     ;;; it used to be.) The function arg must be constant, and is converted to an
1176     ;;; APPLY of ther SETF function, which ought to exist.
1177 ram 1.22 ;;;
1178 dtc 1.58 (define-setf-expander apply (function &rest args)
1179 ram 1.22 (unless (and (listp function)
1180     (= (list-length function) 2)
1181     (eq (first function) 'function)
1182     (symbolp (second function)))
1183     (error "Setf of Apply is only defined for function args like #'symbol."))
1184 ram 1.36 (let ((function (second function))
1185     (new-var (gensym))
1186     (vars nil))
1187     (dolist (x args)
1188     (declare (ignore x))
1189     (push (gensym) vars))
1190     (values vars args (list new-var)
1191     `(apply #'(setf ,function) ,new-var ,@vars)
1192     `(apply #',function ,@vars))))
1193 ram 1.1
1194    
1195 wlott 1.10 ;;; Special-case a BYTE bytespec so that the compiler can recognize it.
1196     ;;;
1197 dtc 1.58 (define-setf-expander ldb (bytespec place &environment env)
1198 ram 1.1 "The first argument is a byte specifier. The second is any place form
1199     acceptable to SETF. Replaces the specified byte of the number in this
1200     place with bits from the low-order end of the new value."
1201     (multiple-value-bind (dummies vals newval setter getter)
1202 wlott 1.13 (get-setf-method place env)
1203 wlott 1.10 (if (and (consp bytespec) (eq (car bytespec) 'byte))
1204     (let ((n-size (gensym))
1205     (n-pos (gensym))
1206     (n-new (gensym)))
1207     (values (list* n-size n-pos dummies)
1208     (list* (second bytespec) (third bytespec) vals)
1209     (list n-new)
1210     `(let ((,(car newval) (dpb ,n-new (byte ,n-size ,n-pos)
1211     ,getter)))
1212     ,setter
1213     ,n-new)
1214     `(ldb (byte ,n-size ,n-pos) ,getter)))
1215     (let ((btemp (gensym))
1216     (gnuval (gensym)))
1217     (values (cons btemp dummies)
1218     (cons bytespec vals)
1219     (list gnuval)
1220     `(let ((,(car newval) (dpb ,gnuval ,btemp ,getter)))
1221     ,setter
1222     ,gnuval)
1223     `(ldb ,btemp ,getter))))))
1224 ram 1.1
1225    
1226 dtc 1.58 (define-setf-expander mask-field (bytespec place &environment env)
1227 ram 1.1 "The first argument is a byte specifier. The second is any place form
1228     acceptable to SETF. Replaces the specified byte of the number in this place
1229     with bits from the corresponding position in the new value."
1230     (multiple-value-bind (dummies vals newval setter getter)
1231 wlott 1.13 (get-setf-method place env)
1232 ram 1.1 (let ((btemp (gensym))
1233     (gnuval (gensym)))
1234     (values (cons btemp dummies)
1235     (cons bytespec vals)
1236     (list gnuval)
1237     `(let ((,(car newval) (deposit-field ,gnuval ,btemp ,getter)))
1238     ,setter
1239     ,gnuval)
1240     `(mask-field ,btemp ,getter)))))
1241    
1242    
1243 dtc 1.58 (define-setf-expander the (type place &environment env)
1244 ram 1.1 (multiple-value-bind (dummies vals newval setter getter)
1245 wlott 1.13 (get-setf-method place env)
1246 ram 1.1 (values dummies
1247     vals
1248     newval
1249     (subst `(the ,type ,(car newval)) (car newval) setter)
1250     `(the ,type ,getter))))
1251 dtc 1.58
1252     (define-setf-expander values (&rest places &environment env)
1253     (collect ((setters) (getters))
1254     (let ((all-dummies '())
1255     (all-vals '())
1256     (newvals '()))
1257     (dolist (place places)
1258     (multiple-value-bind (dummies vals newval setter getter)
1259     (get-setf-expansion place env)
1260     (setf all-dummies (append all-dummies dummies))
1261     (setf all-vals (append all-vals vals))
1262     (setf newvals (append newvals newval))
1263     (setters setter)
1264     (getters getter)))
1265     (values all-dummies all-vals newvals
1266     `(values ,@(setters)) `(values ,@(getters))))))
1267 ram 1.1
1268    
1269     ;;;; CASE, TYPECASE, & Friends.
1270    
1271 toy 1.85 (eval-when (:compile-toplevel :load-toplevel :execute)
1272 ram 1.1
1273     ;;; CASE-BODY returns code for all the standard "case" macros. Name is the
1274     ;;; macro name, and keyform is the thing to case on. Multi-p indicates whether
1275     ;;; a branch may fire off a list of keys; otherwise, a key that is a list is
1276     ;;; interpreted in some way as a single key. When multi-p, test is applied to
1277     ;;; the value of keyform and each key for a given branch; otherwise, test is
1278     ;;; applied to the value of keyform and the entire first element, instead of
1279     ;;; each part, of the case branch. When errorp, no t or otherwise branch is
1280     ;;; permitted, and an ERROR form is generated. When proceedp, it is an error
1281     ;;; to omit errorp, and the ERROR form generated is executed within a
1282     ;;; RESTART-CASE allowing keyform to be set and retested.
1283     ;;;
1284 toy 1.74 ;;; If ALLOW-OTHERWISE, then we allow T and OTHERWISE clauses and also
1285     ;;; generate an ERROR form. (This is for CCASE and ECASE which allow
1286     ;;; using T and OTHERWISE as regular keys.)
1287     ;;;
1288     (defun case-body (name keyform cases multi-p test errorp proceedp &optional allow-otherwise)
1289 ram 1.1 (let ((keyform-value (gensym))
1290     (clauses ())
1291     (keys ()))
1292 toy 1.88 (do* ((case-list cases (cdr case-list))
1293     (case (first case-list) (first case-list)))
1294     ((null case-list))
1295 ram 1.1 (cond ((atom case)
1296     (error "~S -- Bad clause in ~S." case name))
1297 toy 1.74 ((and (not allow-otherwise)
1298 toy 1.88 (memq (car case) '(t otherwise)))
1299 rtoy 1.98 (cond ((null (cdr case-list))
1300     ;; The CLHS says OTHERWISE clause is an OTHERWISE clause
1301     ;; only if it's the last case. Otherwise, it's just a
1302     ;; normal clause.
1303     (if errorp
1304     (error "No default clause allowed in ~S: ~S" name case)
1305     (push `(t nil ,@(rest case)) clauses)))
1306     ((and (eq name 'case))
1307     (error "T and OTHERWISE may not be used as key designators for ~A" name))))
1308 ram 1.1 ((and multi-p (listp (first case)))
1309     (setf keys (append (first case) keys))
1310     (push `((or ,@(mapcar #'(lambda (key)
1311     `(,test ,keyform-value ',key))
1312     (first case)))
1313     nil ,@(rest case))
1314     clauses))
1315     (t
1316 toy 1.74 (when (and allow-otherwise
1317     (memq (car case) '(t otherwise)))
1318     (warn "Bad style to use T or OTHERWISE in ECASE or CCASE"))
1319 ram 1.1 (push (first case) keys)
1320     (push `((,test ,keyform-value
1321     ',(first case)) nil ,@(rest case)) clauses))))
1322     (case-body-aux name keyform keyform-value clauses keys errorp proceedp
1323 toy 1.74 allow-otherwise
1324 ram 1.1 `(,(if multi-p 'member 'or) ,@keys))))
1325    
1326     ;;; CASE-BODY-AUX provides the expansion once CASE-BODY has groveled all the
1327     ;;; cases. Note: it is not necessary that the resulting code signal
1328     ;;; case-failure conditions, but that's what KMP's prototype code did. We call
1329     ;;; CASE-BODY-ERROR, because of how closures are compiled. RESTART-CASE has
1330     ;;; forms with closures that the compiler causes to be generated at the top of
1331     ;;; any function using the case macros, regardless of whether they are needed.
1332     ;;;
1333     (defun case-body-aux (name keyform keyform-value clauses keys
1334 toy 1.74 errorp proceedp allow-otherwise expected-type)
1335 ram 1.1 (if proceedp
1336     (let ((block (gensym))
1337     (again (gensym)))
1338     `(let ((,keyform-value ,keyform))
1339     (block ,block
1340     (tagbody
1341     ,again
1342     (return-from
1343     ,block
1344     (cond ,@(nreverse clauses)
1345     (t
1346     (setf ,keyform-value
1347     (setf ,keyform
1348     (case-body-error
1349     ',name ',keyform ,keyform-value
1350     ',expected-type ',keys)))
1351     (go ,again))))))))
1352     `(let ((,keyform-value ,keyform))
1353 pw 1.49 ,keyform-value ; prevent warnings when key not used eg (case key (t))
1354 ram 1.1 (cond
1355     ,@(nreverse clauses)
1356 toy 1.74 ,@(if (or errorp allow-otherwise)
1357 ram 1.1 `((t (error 'conditions::case-failure
1358     :name ',name
1359     :datum ,keyform-value
1360     :expected-type ',expected-type
1361     :possibilities ',keys))))))))
1362    
1363     ); eval-when
1364    
1365     (defun case-body-error (name keyform keyform-value expected-type keys)
1366     (restart-case
1367     (error 'conditions::case-failure
1368     :name name
1369     :datum keyform-value
1370     :expected-type expected-type
1371     :possibilities keys)
1372     (store-value (value)
1373     :report (lambda (stream)
1374     (format stream "Supply a new value for ~S." keyform))
1375     :interactive read-evaluated-form
1376     value)))
1377    
1378    
1379     (defmacro case (keyform &body cases)
1380     "CASE Keyform {({(Key*) | Key} Form*)}*
1381     Evaluates the Forms in the first clause with a Key EQL to the value of
1382     Keyform. If a singleton key is T then the clause is a default clause."
1383     (case-body 'case keyform cases t 'eql nil nil))
1384    
1385     (defmacro ccase (keyform &body cases)
1386     "CCASE Keyform {({(Key*) | Key} Form*)}*
1387     Evaluates the Forms in the first clause with a Key EQL to the value of
1388     Keyform. If none of the keys matches then a correctable error is
1389     signalled."
1390 toy 1.74 (case-body 'ccase keyform cases t 'eql nil t t))
1391 ram 1.1
1392     (defmacro ecase (keyform &body cases)
1393     "ECASE Keyform {({(Key*) | Key} Form*)}*
1394     Evaluates the Forms in the first clause with a Key EQL to the value of
1395     Keyform. If none of the keys matches then an error is signalled."
1396 toy 1.74 (case-body 'ecase keyform cases t 'eql nil nil t))
1397 ram 1.1
1398     (defmacro typecase (keyform &body cases)
1399     "TYPECASE Keyform {(Type Form*)}*
1400     Evaluates the Forms in the first clause for which TYPEP of Keyform and Type
1401     is true."
1402     (case-body 'typecase keyform cases nil 'typep nil nil))
1403    
1404     (defmacro ctypecase (keyform &body cases)
1405     "CTYPECASE Keyform {(Type Form*)}*
1406     Evaluates the Forms in the first clause for which TYPEP of Keyform and Type
1407     is true. If no form is satisfied then a correctable error is signalled."
1408 toy 1.75 (case-body 'ctypecase keyform cases nil 'typep nil t t))
1409 ram 1.1
1410     (defmacro etypecase (keyform &body cases)
1411     "ETYPECASE Keyform {(Type Form*)}*
1412     Evaluates the Forms in the first clause for which TYPEP of Keyform and Type
1413     is true. If no form is satisfied then an error is signalled."
1414 toy 1.75 (case-body 'etypecase keyform cases nil 'typep nil nil t))
1415 ram 1.1
1416    
1417     ;;;; ASSERT and CHECK-TYPE.
1418    
1419     ;;; ASSERT is written this way, to call ASSERT-ERROR, because of how closures
1420     ;;; are compiled. RESTART-CASE has forms with closures that the compiler
1421     ;;; causes to be generated at the top of any function using ASSERT, regardless
1422     ;;; of whether they are needed.
1423     ;;;
1424     (defmacro assert (test-form &optional places datum &rest arguments)
1425     "Signals an error if the value of test-form is nil. Continuing from this
1426     error using the CONTINUE restart will allow the user to alter the value of
1427     some locations known to SETF, starting over with test-form. Returns nil."
1428     `(loop
1429     (when ,test-form (return nil))
1430     (assert-error ',test-form ',places ,datum ,@arguments)
1431     ,@(mapcar #'(lambda (place)
1432     `(setf ,place (assert-prompt ',place ,place)))
1433     places)))
1434    
1435 ram 1.42 (defun assert-error (assertion places datum &rest arguments)
1436 ram 1.41 (let ((cond (if datum
1437     (conditions::coerce-to-condition
1438     datum arguments
1439     'simple-error 'error)
1440     (make-condition 'simple-error
1441     :format-control "The assertion ~S failed."
1442     :format-arguments (list assertion)))))
1443     (restart-case (error cond)
1444 ram 1.1 (continue ()
1445     :report (lambda (stream) (assert-report places stream))
1446 ram 1.41 nil))))
1447 ram 1.1
1448    
1449     (defun assert-report (names stream)
1450     (format stream "Retry assertion")
1451     (if names
1452     (format stream " with new value~P for ~{~S~^, ~}."
1453     (length names) names)
1454     (format stream ".")))
1455    
1456     (defun assert-prompt (name value)
1457     (cond ((y-or-n-p "The old value of ~S is ~S.~
1458     ~%Do you want to supply a new value? "
1459     name value)
1460     (format *query-io* "~&Type a form to be evaluated:~%")
1461     (flet ((read-it () (eval (read *query-io*))))
1462     (if (symbolp name) ;help user debug lexical variables
1463     (progv (list name) (list value) (read-it))
1464     (read-it))))
1465     (t value)))
1466    
1467    
1468     ;;; CHECK-TYPE is written this way, to call CHECK-TYPE-ERROR, because of how
1469     ;;; closures are compiled. RESTART-CASE has forms with closures that the
1470     ;;; compiler causes to be generated at the top of any function using
1471     ;;; CHECK-TYPE, regardless of whether they are needed. Because it would be
1472     ;;; nice if this were cheap to use, and some things can't afford this excessive
1473     ;;; consing (e.g., READ-CHAR), we bend backwards a little.
1474     ;;;
1475    
1476     (defmacro check-type (place type &optional type-string)
1477     "Signals an error of type type-error if the contents of place are not of the
1478     specified type. If an error is signaled, this can only return if
1479     STORE-VALUE is invoked. It will store into place and start over."
1480     (let ((place-value (gensym)))
1481     `(loop
1482     (let ((,place-value ,place))
1483     (when (typep ,place-value ',type) (return nil))
1484     (setf ,place
1485     (check-type-error ',place ,place-value ',type ,type-string))))))
1486    
1487     (defun check-type-error (place place-value type type-string)
1488 ram 1.41 (let ((cond (if type-string
1489     (make-condition 'simple-type-error
1490     :datum place :expected-type type
1491     :format-control
1492     "The value of ~S is ~S, which is not ~A."
1493     :format-arguments
1494     (list place place-value type-string))
1495     (make-condition 'simple-type-error
1496     :datum place :expected-type type
1497     :format-control
1498     "The value of ~S is ~S, which is not of type ~S."
1499     :format-arguments
1500     (list place place-value type)))))
1501     (restart-case (error cond)
1502     (store-value (value)
1503     :report (lambda (stream)
1504     (format stream "Supply a new value of ~S."
1505     place))
1506     :interactive read-evaluated-form
1507     value))))
1508 ram 1.1
1509     ;;; READ-EVALUATED-FORM is used as the interactive method for restart cases
1510     ;;; setup by the Common Lisp "casing" (e.g., CCASE and CTYPECASE) macros
1511     ;;; and by CHECK-TYPE.
1512     ;;;
1513     (defun read-evaluated-form ()
1514     (format *query-io* "~&Type a form to be evaluated:~%")
1515     (list (eval (read *query-io*))))
1516    
1517    
1518     ;;;; With-XXX
1519 gerd 1.92 (defmacro with-open-file ((var &rest open-args) &parse-body (forms decls))
1520 ram 1.5 "Bindspec is of the form (Stream File-Name . Options). The file whose
1521     name is File-Name is opened using the Options and bound to the variable
1522 pw 1.47 Stream. If the call to open is unsuccessful, the forms are not
1523     evaluated. The Forms are executed, and when they
1524     terminate, normally or otherwise, the file is closed."
1525 ram 1.1 (let ((abortp (gensym)))
1526     `(let ((,var (open ,@open-args))
1527     (,abortp t))
1528     ,@decls
1529 pw 1.47 (unwind-protect
1530     (multiple-value-prog1
1531     (progn ,@forms)
1532     (setq ,abortp nil))
1533     (when ,var
1534 ram 1.1 (close ,var :abort ,abortp))))))
1535    
1536 ram 1.5
1537 gerd 1.92 (defmacro with-open-stream ((var stream) &parse-body (forms decls))
1538 ram 1.1 "The form stream should evaluate to a stream. VAR is bound
1539     to the stream and the forms are evaluated as an implicit
1540     progn. The stream is closed upon exit."
1541     (let ((abortp (gensym)))
1542     `(let ((,var ,stream)
1543     (,abortp t))
1544     ,@decls
1545     (unwind-protect
1546     (multiple-value-prog1
1547     (progn ,@forms)
1548     (setq ,abortp nil))
1549     (when ,var
1550     (close ,var :abort ,abortp))))))
1551    
1552    
1553 gerd 1.92 (defmacro with-input-from-string ((var string &key index start end)
1554     &parse-body (forms decls))
1555 ram 1.1 "Binds the Var to an input stream that returns characters from String and
1556     executes the body. See manual for details."
1557 pw 1.52 ;; The once-only inhibits compiler note for unreachable code when 'end' is true.
1558     (once-only ((string string))
1559     `(let ((,var
1560     ,(cond ((null end)
1561     `(make-string-input-stream ,string ,(or start 0)))
1562     ((symbolp end)
1563     `(if ,end
1564     (make-string-input-stream ,string ,(or start 0) ,end)
1565     (make-string-input-stream ,string ,(or start 0))))
1566     (t
1567     `(make-string-input-stream ,string ,(or start 0) ,end)))))
1568     ,@decls
1569     (unwind-protect
1570     (progn ,@forms)
1571     (close ,var)
1572     ,@(if index `((setf ,index (string-input-stream-current ,var))))))))
1573 ram 1.1
1574    
1575 gerd 1.96 (defmacro with-output-to-string ((var &optional string &key element-type)
1576 gerd 1.92 &parse-body (forms decls))
1577 toy 1.85 "If STRING is specified, it must be a string with a fill pointer;
1578 ram 1.5 the output is incrementally appended to the string (as if by use of
1579     VECTOR-PUSH-EXTEND)."
1580 gerd 1.96 (declare (ignore element-type))
1581 ram 1.1 (if string
1582     `(let ((,var (make-fill-pointer-output-stream ,string)))
1583     ,@decls
1584     (unwind-protect
1585     (progn ,@forms)
1586     (close ,var)))
1587     `(let ((,var (make-string-output-stream)))
1588     ,@decls
1589     (unwind-protect
1590     (progn ,@forms)
1591     (close ,var))
1592     (get-output-stream-string ,var))))
1593    
1594    
1595     ;;;; Iteration macros:
1596    
1597     (defmacro dotimes ((var count &optional (result nil)) &body body)
1598     (cond ((numberp count)
1599     `(do ((,var 0 (1+ ,var)))
1600     ((>= ,var ,count) ,result)
1601 toy 1.71 (declare (type (integer 0 ,count) ,var))
1602 ram 1.1 ,@body))
1603     (t (let ((v1 (gensym)))
1604     `(do ((,var 0 (1+ ,var)) (,v1 ,count))
1605     ((>= ,var ,v1) ,result)
1606 ram 1.8 (declare (type unsigned-byte ,var))
1607 ram 1.1 ,@body)))))
1608    
1609    
1610     ;;; We repeatedly bind the var instead of setting it so that we never give the
1611     ;;; var a random value such as NIL (which might conflict with a declaration).
1612 ram 1.35 ;;; If there is a result form, we introduce a gratitous binding of the variable
1613     ;;; to NIL w/o the declarations, then evaluate the result form in that
1614     ;;; environment. We spuriously reference the gratuitous variable, since we
1615     ;;; don't want to use IGNORABLE on what might be a special var.
1616 ram 1.1 ;;;
1617     (defmacro dolist ((var list &optional (result nil)) &body body)
1618 toy 1.80 (multiple-value-bind (forms decls)
1619     (parse-body body nil nil)
1620     (let ((n-list (gensym)))
1621     `(do* ((,n-list ,list (cdr ,n-list)))
1622 gerd 1.89 ((endp ,n-list)
1623     ,@(if (constantp result)
1624     `(,result)
1625     `((let ((,var nil))
1626     ,@decls
1627     ,var
1628     ,result))))
1629 toy 1.80 (let ((,var (car ,n-list)))
1630     ,@decls
1631     (tagbody
1632     ,@forms))))))
1633 ram 1.1
1634    
1635 gerd 1.92 (defmacro do (varlist endlist &parse-body (body decls))
1636 ram 1.1 "DO ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
1637     Iteration construct. Each Var is initialized in parallel to the value of the
1638     specified Init form. On subsequent iterations, the Vars are assigned the
1639     value of the Step form (if any) in paralell. The Test is evaluated before
1640 dtc 1.53 each evaluation of the body Forms. When the Test is true, the Exit-Forms
1641 ram 1.1 are evaluated as a PROGN, with the result being the value of the DO. A block
1642     named NIL is established around the entire expansion, allowing RETURN to be
1643     used as an laternate exit mechanism."
1644    
1645     (do-do-body varlist endlist body decls 'let 'psetq 'do nil))
1646    
1647    
1648 gerd 1.92 (defmacro do* (varlist endlist &parse-body (body decls))
1649 ram 1.1 "DO* ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
1650     Iteration construct. Each Var is initialized sequentially (like LET*) to the
1651     value of the specified Init form. On subsequent iterations, the Vars are
1652     sequentially assigned the value of the Step form (if any). The Test is
1653     evaluated before each evaluation of the body Forms. When the Test is true,
1654 dtc 1.53 the Exit-Forms are evaluated as a PROGN, with the result being the value
1655 ram 1.1 of the DO. A block named NIL is established around the entire expansion,
1656     allowing RETURN to be used as an laternate exit mechanism."
1657     (do-do-body varlist endlist body decls 'let* 'setq 'do* nil))
1658    
1659    
1660     ;;;; Miscellaneous macros:
1661    
1662     (defmacro psetq (&rest pairs)
1663 toy 1.84 "PSETQ {var value}*
1664     Set the variables to the values, like SETQ, except that assignments
1665     happen in parallel, i.e. no assignments take place until all the
1666     forms have been evaluated."
1667     ;; Given the possibility of symbol-macros, we delegate to PSETF
1668     ;; which knows how to deal with them, after checking that syntax is
1669     ;; compatible with PSETQ.
1670     (do ((pair pairs (cddr pair)))
1671     ((endp pair) `(psetf ,@pairs))
1672     (unless (symbolp (car pair))
1673     (error 'simple-program-error
1674     :format-control "variable ~S in PSETQ is not a SYMBOL"
1675     :format-arguments (list (car pair))))))
1676    
1677 pw 1.48
1678     ;;; LAMBDA -- from the ANSI spec.
1679     ;;;
1680     (defmacro lambda (&whole form &rest bvl-decls-and-body)
1681     (declare (ignore bvl-decls-and-body))
1682     `#',form)
1683    
1684 ram 1.1
1685    
1686     ;;;; With-Compilation-Unit:
1687    
1688 toy 1.85 ;;; True if we are within a WITH-COMPILATION-UNIT form, which normally causes
1689 ram 1.1 ;;; nested uses to be NOOPS.
1690     ;;;
1691     (defvar *in-compilation-unit* nil)
1692    
1693     ;;; Count of the number of compilation units dynamically enclosed by the
1694     ;;; current active WITH-COMPILATION-UNIT that were unwound out of.
1695     ;;;
1696     (defvar *aborted-compilation-units*)
1697    
1698 ram 1.21 (declaim (special c::*context-declarations*))
1699    
1700    
1701     ;;; EVALUATE-DECLARATION-CONTEXT -- Internal
1702     ;;;
1703     ;;; Recursively descend the context form, returning true if this subpart
1704     ;;; matches the specified context.
1705     ;;;
1706     (defun evaluate-declaration-context (context name parent)
1707 gerd 1.87 (multiple-value-bind (valid base)
1708     (valid-function-name-p name)
1709     (let ((package (and valid (symbolp base) (symbol-package base))))
1710     (if (atom context)
1711     (multiple-value-bind (ignore how)
1712     (if package
1713     (find-symbol (symbol-name base) package)
1714     (values nil nil))
1715     (declare (ignore ignore))
1716     (case context
1717     (:internal (eq how :internal))
1718     (:external (eq how :external))
1719     (:uninterned (and (symbolp base) (not package)))
1720     (:anonymous (not name))
1721     (:macro (eq parent 'defmacro))
1722     (:function (member parent '(defun labels flet function)))
1723     (:global (member parent '(defun defmacro function)))
1724     (:local (member parent '(labels flet)))
1725     (t
1726     (error "Unknown declaration context: ~S." context))))
1727     (case (first context)
1728     (:or
1729     (loop for x in (rest context)
1730     thereis (evaluate-declaration-context x name parent)))
1731     (:and
1732     (loop for x in (rest context)
1733     always (evaluate-declaration-context x name parent)))
1734     (:not
1735     (evaluate-declaration-context (second context) name parent))
1736     (:member
1737     (member name (rest context) :test #'equal))
1738     (:match
1739     (let ((name (concatenate 'string "$" (string base) "$")))
1740     (loop for x in (rest context)
1741     thereis (search (string x) name))))
1742     (:package
1743     (and package
1744     (loop for x in (rest context)
1745     thereis (eq (find-package (string x)) package))))
1746 ram 1.24 (t
1747 gerd 1.87 (error "Unknown declaration context: ~S." context)))))))
1748 ram 1.21
1749    
1750     ;;; PROCESS-CONTEXT-DECLARATIONS -- Internal
1751     ;;;
1752     ;;; Given a list of context declaration specs, return a new value for
1753     ;;; C::*CONTEXT-DECLARATIONS*.
1754     ;;;
1755     (defun process-context-declarations (decls)
1756     (append
1757     (mapcar
1758     #'(lambda (decl)
1759     (unless (>= (length decl) 2)
1760     (error "Context declaration spec should have context and at ~
1761     least one DECLARE form:~% ~S" decl))
1762     #'(lambda (name parent)
1763     (when (evaluate-declaration-context (first decl) name parent)
1764     (rest decl))))
1765     decls)
1766     c::*context-declarations*))
1767    
1768    
1769 ram 1.1 ;;; With-Compilation-Unit -- Public
1770     ;;;
1771     (defmacro with-compilation-unit (options &body body)
1772 ram 1.21 "WITH-COMPILATION-UNIT ({Key Value}*) Form*
1773     This form affects compilations that take place within its dynamic extent. It
1774     is intended to be wrapped around the compilation of all files in the same
1775     system. These keywords are defined:
1776     :OVERRIDE Boolean-Form
1777     One of the effects of this form is to delay undefined warnings
1778     until the end of the form, instead of giving them at the end of each
1779     compilation. If OVERRIDE is NIL (the default), then the outermost
1780     WITH-COMPILATION-UNIT form grabs the undefined warnings. Specifying
1781     OVERRIDE true causes that form to grab any enclosed warnings, even if
1782     it is enclosed by another WITH-COMPILATION-UNIT.
1783     :OPTIMIZE Decl-Form
1784     Decl-Form should evaluate to an OPTIMIZE declaration specifier. This
1785     declaration changes the `global' policy for compilations within the
1786     body.
1787     :OPTIMIZE-INTERFACE Decl-Form
1788     Like OPTIMIZE, except that it specifies the value of the CMU extension
1789     OPTIMIZE-INTERFACE policy (which controls argument type and syntax
1790     checking.)
1791     :CONTEXT-DECLARATIONS List-of-Context-Decls-Form
1792     This is a CMU extension which allows compilation to be controlled
1793     by pattern matching on the context in which a definition appears. The
1794     argument should evaluate to a list of lists of the form:
1795     (Context-Spec Declare-Form+)
1796     In the indicated context, the specified declare forms are inserted at
1797     the head of each definition. The declare forms for all contexts that
1798     match are appended together, with earlier declarations getting
1799     predecence over later ones. A simple example:
1800     :context-declarations
1801     '((:external (declare (optimize (safety 2)))))
1802     This will cause all functions that are named by external symbols to be
1803     compiled with SAFETY 2. The full syntax of context specs is:
1804     :INTERNAL, :EXTERNAL
1805     True if the symbols is internal (external) in its home package.
1806     :UNINTERNED
1807     True if the symbol has no home package.
1808     :ANONYMOUS
1809     True if the function doesn't have any interesting name (not
1810     DEFMACRO, DEFUN, LABELS or FLET).
1811     :MACRO, :FUNCTION
1812     :MACRO is a global (DEFMACRO) macro. :FUNCTION is anything else.
1813     :LOCAL, :GLOBAL
1814     :LOCAL is a LABELS or FLET. :GLOBAL is anything else.
1815     (:OR Context-Spec*)
1816     True in any specified context.
1817     (:AND Context-Spec*)
1818     True only when all specs are true.
1819     (:NOT Context-Spec)
1820     True when the spec is false.
1821     (:MEMBER Name*)
1822     True when the name is one of these names (EQUAL test.)
1823     (:MATCH Pattern*)
1824     True when any of the patterns is a substring of the name. The name
1825     is wrapped with $'s, so $FOO matches names beginning with FOO,
1826     etc."
1827     (let ((override nil)
1828     (optimize nil)
1829     (optimize-interface nil)
1830     (context-declarations nil)
1831 ram 1.1 (n-fun (gensym))
1832     (n-abort-p (gensym)))
1833     (when (oddp (length options))
1834     (error "Odd number of key/value pairs: ~S." options))
1835     (do ((opt options (cddr opt)))
1836     ((null opt))
1837     (case (first opt)
1838 ram 1.21 (:override
1839     (setq override (second opt)))
1840     (:optimize
1841     (setq optimize (second opt)))
1842     (:optimize-interface
1843     (setq optimize-interface (second opt)))
1844     (:context-declarations
1845     (setq context-declarations (second opt)))
1846 ram 1.1 (t
1847     (warn "Ignoring unknown option: ~S." (first opt)))))
1848    
1849 ram 1.21 `(flet ((,n-fun ()
1850     (let (,@(when optimize
1851     `((c::*default-cookie*
1852     (c::process-optimize-declaration
1853     ,optimize c::*default-cookie*))))
1854     ,@(when optimize-interface
1855     `((c::*default-interface-cookie*
1856     (c::process-optimize-declaration
1857     ,optimize-interface
1858     c::*default-interface-cookie*))))
1859     ,@(when context-declarations
1860     `((c::*context-declarations*
1861     (process-context-declarations
1862     ,context-declarations)))))
1863     ,@body)))
1864     (if (or ,override (not *in-compilation-unit*))
1865 ram 1.6 (let ((c::*undefined-warnings* nil)
1866 ram 1.1 (c::*compiler-error-count* 0)
1867     (c::*compiler-warning-count* 0)
1868     (c::*compiler-note-count* 0)
1869     (*in-compilation-unit* t)
1870     (*aborted-compilation-units* 0)
1871     (,n-abort-p t))
1872 ram 1.6 (handler-bind ((c::parse-unknown-type
1873     #'(lambda (c)
1874     (c::note-undefined-reference
1875 emarsden 1.97 (kernel:parse-unknown-type-specifier c)
1876 ram 1.6 :type))))
1877     (unwind-protect
1878     (multiple-value-prog1
1879     (,n-fun)
1880     (setq ,n-abort-p nil))
1881     (c::print-summary ,n-abort-p *aborted-compilation-units*))))
1882 ram 1.1 (let ((,n-abort-p t))
1883     (unwind-protect
1884     (multiple-value-prog1
1885     (,n-fun)
1886     (setq ,n-abort-p nil))
1887     (when ,n-abort-p
1888     (incf *aborted-compilation-units*))))))))

  ViewVC Help
Powered by ViewVC 1.1.5