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

Contents of /src/code/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.69 - (hide annotations)
Sun Jun 3 14:11:16 2001 UTC (12 years, 10 months ago) by pw
Branch: MAIN
Changes since 1.68: +12 -3 lines
From Tim Moore:

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

  ViewVC Help
Powered by ViewVC 1.1.5