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

Contents of /src/code/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.86 - (hide annotations)
Wed Feb 5 11:08:43 2003 UTC (11 years, 2 months ago) by gerd
Branch: MAIN
Changes since 1.85: +7 -8 lines
	Generalized function names.

	* code/fdefinition.lisp (*valid-function-names*): New variable.
	(%define-function-name-syntax, valid-function-name-p): New
	functions.
	(define-function-name-syntax): New macro.
	(toplevel): Define the syntax of setf function names.
	(fdefinition-object): Use valid-function-name-p.

	* compiler/proclaim.lisp (check-function-name): Use
	valid-function-name-p.

	* compiler/ir1tran.lisp (function): Use valid-function-name-p.

	* code/profile.lisp (%report-times): Use valid-function-name-p.

	* code/macros.lisp (defun): Use valid-function-name-p.

	* code/eval.lisp (eval): Use valid-function-name-p.

	* code/describe.lisp (describe-aux, describe-function-name):
	Use valid-function-name-p.

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

  ViewVC Help
Powered by ViewVC 1.1.5