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

Contents of /src/code/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.93 - (hide annotations)
Mon May 12 16:30:41 2003 UTC (10 years, 11 months ago) by emarsden
Branch: MAIN
Changes since 1.92: +32 -4 lines
Add package locks. Bootfile boot8.lisp allows this to build without
a cross-compile (or just select the CLOBBER-IT restart).

   - two extra PACKAGE-LOCK and PACKAGE-DEFINITION-LOCK slots added to
     the package structure. These can be modified using the
     EXT:PACKAGE-LOCK and EXT:PACKAGE-DEFINITION-LOCK accessors.

   - macro EXT:WITHOUT-PACKAGE-LOCKS that evaluates forms with all
     package locks disabled (this is done by binding the global
     variable CL::*ENABLE-PACKAGE-LOCKED-ERRORS*)

   - new PACKAGE-LOCKED-ERROR condition

   - in SETF-FDEFINITION-HOOK and in the DEFMACRO, DEFSTRUCT, DEFTYPE
     and DEFCLASS defining forms, check whether the definition would
     modify a package whose definition-lock is enabled, and signal a
     package-locked-error condition with restarts that allow you to
     unlock the package or ignore the lock

   - in EXPORT, UNEXPORT etc check whether the target package is
     guarded by a package-lock, and signal an error

   - disable package locks when loading a subsystem

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

  ViewVC Help
Powered by ViewVC 1.1.5