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

Contents of /src/code/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5