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

Contents of /src/code/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.111 - (show annotations)
Thu Jan 3 11:41:51 2008 UTC (6 years, 3 months ago) by cshapiro
Branch: MAIN
CVS Tags: snapshot-2008-05, snapshot-2008-06, snapshot-2008-01, snapshot-2008-02, snapshot-2008-03, release-19e, snapshot-2008-04, release-19e-pre1, release-19e-pre2, unicode-utf16-string-support, release-19e-base, unicode-utf16-base
Branch point for: unicode-utf16-branch, release-19e-branch
Changes since 1.110: +2 -2 lines
Switch the FreeBSD port to use the common floating point trap handling
code.  Rather than introduce a new FreeBSD case to the x86 sigcontext
member accessor routines, collapse all of the system specific routines
down to a common set of routines.

* code/debug-int.lisp - Disable some Darwin-specific code to debug
  NULL mcontext pointers.

* code/float-trap.lisp - Remove ancient FreeBSD-specific code for
  handling floating point signals.

* code/macros.lisp, code/sap.lisp, compiler/saptran.lisp - Include the
  SAP-REF-LONG setter by default on the x86.

* code/x86-vm.lisp - Remove operating system specific sigcontext
  definitions and sigcontext accessors.  Define the alien sigcontext
  as a system area pointer.  Replace the sigcontext accessors with
  foreign function calls that mask the complexity of the underlying
  sigcontext member access.

* compiler/x86/float.lisp - Unconditionally define STORE-LONG-FLOAT.
  This function is used by the %SET-SAP-REF-LONG VOP that underlies
  the SAP-REF-LONG setter.

* compiler/x86/sap.lisp - Unconditionally define %SET-SAP-REF-LONG.
  In the case where there is not a distinct LONG-FLOAT type, admit
  DOUBLE-FLOAT values instead.  The x87 automatically widens values
  pushed onto stack.  This mirrors the behavior of the SAP-REF-LONG
  VOP.

* lisp/Darwin-os.c, lisp/Linux-os.c - Define functions to access
  sigcontext members of interest to Lisp.  Delete the sc_reg function
  and replace its uses with os_sigcontext_reg which is more suitably
  typed.

* lisp/FreeBSD-os.c - Define functions to access sigcontext members of
  interest to Lisp.  We need to be careful about the SSE and non-SSE
  cases for retrieving x87 registers from the saved machine state.
  Define a low-level SIGFPE handler to intercept floating point traps
  and restore the cleared status word bits based on the signal code.
  Get rid of sc_reg for the reasons noted above.

* lisp/Darwin-os.h, lisp/FreeBSD-os.h - Declare the restore_fpu
  function and define a specialized RESTORE_FPU macro.  Remove the
  sc_reg prototype.

* lisp/Linux-os.h - Remove the sc_reg prototype.

* lisp/os.h - Add prototypes for the new os_sigcontext functions.

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

  ViewVC Help
Powered by ViewVC 1.1.5