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

Contents of /src/code/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5