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

Contents of /src/code/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.92 - (hide annotations)
Sat Apr 19 20:52:43 2003 UTC (11 years ago) by gerd
Branch: MAIN
CVS Tags: remove_negative_zero_not_zero
Changes since 1.91: +12 -10 lines
	Add destructuring support to &REST, &BODY, &WHOLE.  Detected
	by Paul Dietz' ANSI tests.

	* src/code/defmacro.lisp (parse-defmacro-lambda-list): Add
	&parse-body, replacing &body (<body> <decls> <doc>).  Add
	destructuring support to &rest, &body, &whole.

	* src/code/eval.lisp (lambda-list-keywords): Add &parse-body.

	* src/code/exports.lisp ("EXTENSIONS"): Export &parse-body.

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

  ViewVC Help
Powered by ViewVC 1.1.5