still fixing host most-fooative-fixnum leaks
[sbcl.git] / src / code / defboot.lisp
blobc19d6605e9620f621a9082dd2ad016e4a558ec0d
1 ;;;; bootstrapping fundamental machinery (e.g. DEFUN, DEFCONSTANT,
2 ;;;; DEFVAR) from special forms and primitive functions
3 ;;;;
4 ;;;; KLUDGE: The bootstrapping aspect of this is now obsolete. It was
5 ;;;; originally intended that this file file would be loaded into a
6 ;;;; Lisp image which had Common Lisp primitives defined, and DEFMACRO
7 ;;;; defined, and little else. Since then that approach has been
8 ;;;; dropped and this file has been modified somewhat to make it work
9 ;;;; more cleanly when used to predefine macros at
10 ;;;; build-the-cross-compiler time.
12 ;;;; This software is part of the SBCL system. See the README file for
13 ;;;; more information.
14 ;;;;
15 ;;;; This software is derived from the CMU CL system, which was
16 ;;;; written at Carnegie Mellon University and released into the
17 ;;;; public domain. The software is in the public domain and is
18 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
19 ;;;; files for more information.
21 (in-package "SB!IMPL")
24 ;;;; IN-PACKAGE
26 (defmacro-mundanely in-package (string-designator)
27 (let ((string (string string-designator)))
28 `(eval-when (:compile-toplevel :load-toplevel :execute)
29 (setq *package* (find-undeleted-package-or-lose ,string)))))
31 ;;;; MULTIPLE-VALUE-FOO
33 (defun list-of-symbols-p (x)
34 (and (listp x)
35 (every #'symbolp x)))
37 (defmacro-mundanely multiple-value-bind (vars value-form &body body)
38 (if (list-of-symbols-p vars)
39 ;; It's unclear why it would be important to special-case the LENGTH=1 case
40 ;; at this level, but the CMU CL code did it, so.. -- WHN 19990411
41 (if (= (length vars) 1)
42 `(let ((,(car vars) ,value-form))
43 ,@body)
44 (let ((ignore (sb!xc:gensym)))
45 `(multiple-value-call #'(lambda (&optional ,@(mapcar #'list vars)
46 &rest ,ignore)
47 (declare (ignore ,ignore))
48 ,@body)
49 ,value-form)))
50 (error "Vars is not a list of symbols: ~S" vars)))
52 (defmacro-mundanely multiple-value-setq (vars value-form)
53 (unless (list-of-symbols-p vars)
54 (error "Vars is not a list of symbols: ~S" vars))
55 ;; MULTIPLE-VALUE-SETQ is required to always return just the primary
56 ;; value of the value-from, even if there are no vars. (SETF VALUES)
57 ;; in turn is required to return as many values as there are
58 ;; value-places, hence this:
59 (if vars
60 `(values (setf (values ,@vars) ,value-form))
61 `(values ,value-form)))
63 (defmacro-mundanely multiple-value-list (value-form)
64 `(multiple-value-call #'list ,value-form))
66 ;;;; various conditional constructs
68 ;;; COND defined in terms of IF
69 (defmacro-mundanely cond (&rest clauses)
70 (if (endp clauses)
71 nil
72 (let ((clause (first clauses))
73 (more (rest clauses)))
74 (if (atom clause)
75 (error 'simple-type-error
76 :format-control "COND clause is not a ~S: ~S"
77 :format-arguments (list 'cons clause)
78 :expected-type 'cons
79 :datum clause)
80 (let ((test (first clause))
81 (forms (rest clause)))
82 (if (endp forms)
83 (let ((n-result (gensym)))
84 `(let ((,n-result ,test))
85 (if ,n-result
86 ,n-result
87 (cond ,@more))))
88 (if (eq t test)
89 ;; THE to preserve non-toplevelness for FOO in
90 ;; (COND (T (FOO)))
91 ;; FIXME: this hides all other possible stylistic issues,
92 ;; not the least of which is a code deletion note,
93 ;; if there are forms following the one whose head is T.
94 ;; This is not usually the SBCL preferred way.
95 `(the t (progn ,@forms))
96 `(if ,test
97 (progn ,@forms)
98 ,(when more `(cond ,@more))))))))))
100 (defmacro-mundanely when (test &body forms)
101 #!+sb-doc
102 "If the first argument is true, the rest of the forms are
103 evaluated as a PROGN."
104 `(if ,test (progn ,@forms) nil))
106 (defmacro-mundanely unless (test &body forms)
107 #!+sb-doc
108 "If the first argument is not true, the rest of the forms are
109 evaluated as a PROGN."
110 `(if ,test nil (progn ,@forms)))
112 (defmacro-mundanely and (&rest forms)
113 (cond ((endp forms) t)
114 ((endp (rest forms))
115 ;; Preserve non-toplevelness of the form!
116 `(the t ,(first forms)))
118 `(if ,(first forms)
119 (and ,@(rest forms))
120 nil))))
122 (defmacro-mundanely or (&rest forms)
123 (cond ((endp forms) nil)
124 ((endp (rest forms))
125 ;; Preserve non-toplevelness of the form!
126 `(the t ,(first forms)))
128 (let ((n-result (gensym)))
129 `(let ((,n-result ,(first forms)))
130 (if ,n-result
131 ,n-result
132 (or ,@(rest forms))))))))
134 ;;;; various sequencing constructs
136 (flet ((prog-expansion-from-let (varlist body-decls let)
137 (multiple-value-bind (body decls)
138 (parse-body body-decls :doc-string-allowed nil)
139 `(block nil
140 (,let ,varlist
141 ,@decls
142 (tagbody ,@body))))))
143 (defmacro-mundanely prog (varlist &body body-decls)
144 (prog-expansion-from-let varlist body-decls 'let))
145 (defmacro-mundanely prog* (varlist &body body-decls)
146 (prog-expansion-from-let varlist body-decls 'let*)))
148 (defmacro-mundanely prog1 (result &body body)
149 (let ((n-result (gensym)))
150 `(let ((,n-result ,result))
151 ,@body
152 ,n-result)))
154 (defmacro-mundanely prog2 (form1 result &body body)
155 `(prog1 (progn ,form1 ,result) ,@body))
157 ;;;; DEFUN
159 ;;; Should we save the inline expansion of the function named NAME?
160 (defun inline-fun-name-p (name)
162 ;; the normal reason for saving the inline expansion
163 (let ((inlinep (info :function :inlinep name)))
164 (member inlinep '(:inline :maybe-inline)))
165 ;; another reason for saving the inline expansion: If the
166 ;; ANSI-recommended idiom
167 ;; (DECLAIM (INLINE FOO))
168 ;; (DEFUN FOO ..)
169 ;; (DECLAIM (NOTINLINE FOO))
170 ;; has been used, and then we later do another
171 ;; (DEFUN FOO ..)
172 ;; without a preceding
173 ;; (DECLAIM (INLINE FOO))
174 ;; what should we do with the old inline expansion when we see the
175 ;; new DEFUN? Overwriting it with the new definition seems like
176 ;; the only unsurprising choice.
177 (info :function :inline-expansion-designator name)))
179 (defmacro-mundanely defun (&environment env name args &body body)
180 #!+sb-doc
181 "Define a function at top level."
182 #+sb-xc-host
183 (unless (symbol-package (fun-name-block-name name))
184 (warn "DEFUN of uninterned function name ~S (tricky for GENESIS)" name))
185 (multiple-value-bind (forms decls doc) (parse-body body)
186 (let* (;; stuff shared between LAMBDA and INLINE-LAMBDA and NAMED-LAMBDA
187 (lambda-guts `(,args
188 ,@decls
189 (block ,(fun-name-block-name name)
190 ,@forms)))
191 (lambda `(lambda ,@lambda-guts))
192 #-sb-xc-host
193 (named-lambda `(named-lambda ,name ,@lambda-guts))
194 (inline-lambda
195 (when (inline-fun-name-p name)
196 ;; we want to attempt to inline, so complain if we can't
197 (or (sb!c:maybe-inline-syntactic-closure lambda env)
198 (progn
199 (#+sb-xc-host warn
200 #-sb-xc-host sb!c:maybe-compiler-notify
201 "lexical environment too hairy, can't inline DEFUN ~S"
202 name)
203 nil)))))
204 `(progn
205 ;; In cross-compilation of toplevel DEFUNs, we arrange for
206 ;; the LAMBDA to be statically linked by GENESIS.
208 ;; It may seem strangely inconsistent not to use NAMED-LAMBDA
209 ;; here instead of LAMBDA. The reason is historical:
210 ;; COLD-FSET was written before NAMED-LAMBDA, and has special
211 ;; logic of its own to notify the compiler about NAME.
212 #+sb-xc-host
213 (cold-fset ,name ,lambda)
215 (eval-when (:compile-toplevel)
216 (sb!c:%compiler-defun ',name ',inline-lambda t))
217 (%defun ',name
218 ;; In normal compilation (not for cold load) this is
219 ;; where the compiled LAMBDA first appears. In
220 ;; cross-compilation, we manipulate the
221 ;; previously-statically-linked LAMBDA here.
222 #-sb-xc-host ,named-lambda
223 #+sb-xc-host (fdefinition ',name)
224 ,doc
225 ',inline-lambda
226 (sb!c:source-location))))))
228 ;; Approximately 20% of the output from #+sb-show is from lines associated with
229 ;; printing "redefining NAME in %DEFUN" and lines about how it is not possible
230 ;; to actually invoke WARN at that point.
231 ;; So FBOUNDP etc is useless because the warning is ignored during cold-init.
232 #-sb-xc-host
233 (macrolet
234 ((def-defun (name fboundp-check)
235 `(defun ,name (name def doc inline-lambda source-location)
236 (declare (type function def))
237 (declare (type (or null simple-string) doc))
238 ,@(unless fboundp-check
239 '((declare (ignore source-location))))
240 ;; should've been checked by DEFMACRO DEFUN
241 (aver (legal-fun-name-p name))
242 (sb!c:%compiler-defun name inline-lambda nil)
243 ,@(when fboundp-check
244 `((when (fboundp name)
245 (/show0 "redefining NAME in %DEFUN")
246 (warn 'redefinition-with-defun
247 :name name
248 :new-function def
249 :new-location source-location))))
250 (setf (sb!xc:fdefinition name) def)
251 ;; %COMPILER-DEFUN doesn't do this except at compile-time, when it
252 ;; also checks package locks. By doing this here we let (SETF
253 ;; FDEFINITION) do the load-time package lock checking before
254 ;; we frob any existing inline expansions.
255 (sb!c::%set-inline-expansion name nil inline-lambda)
257 (sb!c::note-name-defined name :function)
259 (when doc
260 (setf (%fun-doc def) doc))
262 name)))
263 (def-defun %defun t)
264 (def-defun !%quietly-defun nil))
266 ;;;; DEFVAR and DEFPARAMETER
268 (defmacro-mundanely defvar (var &optional (val nil valp) (doc nil docp))
269 #!+sb-doc
270 "Define a special variable at top level. Declare the variable
271 SPECIAL and, optionally, initialize it. If the variable already has a
272 value, the old value is not clobbered. The third argument is an optional
273 documentation string for the variable."
274 `(progn
275 (eval-when (:compile-toplevel)
276 (%compiler-defvar ',var))
277 (%defvar ',var (unless (boundp ',var) ,val)
278 ',valp ,doc ',docp
279 (sb!c:source-location))))
281 (defmacro-mundanely defparameter (var val &optional (doc nil docp))
282 #!+sb-doc
283 "Define a parameter that is not normally changed by the program,
284 but that may be changed without causing an error. Declare the
285 variable special and sets its value to VAL, overwriting any
286 previous value. The third argument is an optional documentation
287 string for the parameter."
288 `(progn
289 (eval-when (:compile-toplevel)
290 (%compiler-defvar ',var))
291 (%defparameter ',var ,val ,doc ',docp (sb!c:source-location))))
293 (defun %compiler-defvar (var)
294 (sb!xc:proclaim `(special ,var)))
296 #-sb-xc-host
297 (defun %defvar (var val valp doc docp source-location)
298 (%compiler-defvar var)
299 (when valp
300 (unless (boundp var)
301 (set var val)))
302 (when docp
303 (setf (fdocumentation var 'variable) doc))
304 (sb!c:with-source-location (source-location)
305 (setf (info :source-location :variable var) source-location))
306 var)
308 #-sb-xc-host
309 (defun %defparameter (var val doc docp source-location)
310 (%compiler-defvar var)
311 (set var val)
312 (when docp
313 (setf (fdocumentation var 'variable) doc))
314 (sb!c:with-source-location (source-location)
315 (setf (info :source-location :variable var) source-location))
316 var)
318 ;;;; iteration constructs
320 ;;; (These macros are defined in terms of a function FROB-DO-BODY which
321 ;;; is also used by SB!INT:DO-ANONYMOUS. Since these macros should not
322 ;;; be loaded on the cross-compilation host, but SB!INT:DO-ANONYMOUS
323 ;;; and FROB-DO-BODY should be, these macros can't conveniently be in
324 ;;; the same file as FROB-DO-BODY.)
325 (defmacro-mundanely do (varlist endlist &body body)
326 #!+sb-doc
327 "DO ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
328 Iteration construct. Each Var is initialized in parallel to the value of the
329 specified Init form. On subsequent iterations, the Vars are assigned the
330 value of the Step form (if any) in parallel. The Test is evaluated before
331 each evaluation of the body Forms. When the Test is true, the Exit-Forms
332 are evaluated as a PROGN, with the result being the value of the DO. A block
333 named NIL is established around the entire expansion, allowing RETURN to be
334 used as an alternate exit mechanism."
335 (frob-do-body varlist endlist body 'let 'psetq 'do nil))
336 (defmacro-mundanely do* (varlist endlist &body body)
337 #!+sb-doc
338 "DO* ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
339 Iteration construct. Each Var is initialized sequentially (like LET*) to the
340 value of the specified Init form. On subsequent iterations, the Vars are
341 sequentially assigned the value of the Step form (if any). The Test is
342 evaluated before each evaluation of the body Forms. When the Test is true,
343 the Exit-Forms are evaluated as a PROGN, with the result being the value
344 of the DO. A block named NIL is established around the entire expansion,
345 allowing RETURN to be used as an alternate exit mechanism."
346 (frob-do-body varlist endlist body 'let* 'setq 'do* nil))
348 ;;; DOTIMES and DOLIST could be defined more concisely using
349 ;;; destructuring macro lambda lists or DESTRUCTURING-BIND, but then
350 ;;; it'd be tricky to use them before those things were defined.
351 ;;; They're used enough times before destructuring mechanisms are
352 ;;; defined that it looks as though it's worth just implementing them
353 ;;; ASAP, at the cost of being unable to use the standard
354 ;;; destructuring mechanisms.
355 (defmacro-mundanely dotimes ((var count &optional (result nil)) &body body)
356 (cond ((integerp count)
357 `(do ((,var 0 (1+ ,var)))
358 ((>= ,var ,count) ,result)
359 (declare (type unsigned-byte ,var))
360 ,@body))
362 (let ((c (gensym "COUNT")))
363 `(do ((,var 0 (1+ ,var))
364 (,c ,count))
365 ((>= ,var ,c) ,result)
366 (declare (type unsigned-byte ,var)
367 (type integer ,c))
368 ,@body)))))
370 (defmacro-mundanely dolist ((var list &optional (result nil)) &body body &environment env)
371 ;; We repeatedly bind the var instead of setting it so that we never
372 ;; have to give the var an arbitrary value such as NIL (which might
373 ;; conflict with a declaration). If there is a result form, we
374 ;; introduce a gratuitous binding of the variable to NIL without the
375 ;; declarations, then evaluate the result form in that
376 ;; environment. We spuriously reference the gratuitous variable,
377 ;; since we don't want to use IGNORABLE on what might be a special
378 ;; var.
379 (multiple-value-bind (forms decls) (parse-body body :doc-string-allowed nil)
380 (let* ((n-list (gensym "N-LIST"))
381 (start (gensym "START")))
382 (multiple-value-bind (clist members clist-ok)
383 (cond ((sb!xc:constantp list env)
384 (let ((value (constant-form-value list env)))
385 (multiple-value-bind (all dot) (list-members value :max-length 20)
386 (when (eql dot t)
387 ;; Full warning is too much: the user may terminate the loop
388 ;; early enough. Contents are still right, though.
389 (style-warn "Dotted list ~S in DOLIST." value))
390 (if (eql dot :maybe)
391 (values value nil nil)
392 (values value all t)))))
393 ((and (consp list) (eq 'list (car list))
394 (every (lambda (arg) (sb!xc:constantp arg env)) (cdr list)))
395 (let ((values (mapcar (lambda (arg) (constant-form-value arg env)) (cdr list))))
396 (values values values t)))
398 (values nil nil nil)))
399 `(block nil
400 (let ((,n-list ,(if clist-ok (list 'quote clist) list)))
401 (tagbody
402 ,start
403 (unless (endp ,n-list)
404 (let ((,var ,(if clist-ok
405 `(truly-the (member ,@members) (car ,n-list))
406 `(car ,n-list))))
407 ,@decls
408 (setq ,n-list (cdr ,n-list))
409 (tagbody ,@forms))
410 (go ,start))))
411 ,(if result
412 `(let ((,var nil))
413 ;; Filter out TYPE declarations (VAR gets bound to NIL,
414 ;; and might have a conflicting type declaration) and
415 ;; IGNORE (VAR might be ignored in the loop body, but
416 ;; it's used in the result form).
417 ,@(filter-dolist-declarations decls)
418 ,var
419 ,result)
420 nil))))))
422 ;;;; conditions, handlers, restarts
424 ;;; KLUDGE: we PROCLAIM these special here so that we can use restart
425 ;;; macros in the compiler before the DEFVARs are compiled.
427 ;;; For an explanation of these data structures, see DEFVARs in
428 ;;; target-error.lisp.
429 (sb!xc:proclaim '(special *handler-clusters* *restart-clusters*))
431 ;;; Generated code need not check for unbound-marker in *HANDLER-CLUSTERS*
432 ;;; (resp *RESTART-). To elicit this we must poke at the info db.
433 ;;; SB!XC:PROCLAIM SPECIAL doesn't advise the host Lisp that *HANDLER-CLUSTERS*
434 ;;; is special and so it rightfully complains about a SETQ of the variable.
435 ;;; But I must SETQ if proclaming ALWAYS-BOUND because the xc asks the host
436 ;;; whether it's currently bound.
437 ;;; But the DEFVARs are in target-error. So it's one hack or another.
438 (setf (info :variable :always-bound '*handler-clusters*)
439 #+sb-xc :always-bound #-sb-xc :eventually)
440 (setf (info :variable :always-bound '*restart-clusters*)
441 #+sb-xc :always-bound #-sb-xc :eventually)
443 (defmacro-mundanely with-condition-restarts
444 (condition-form restarts-form &body body)
445 #!+sb-doc
446 "Evaluates the BODY in a dynamic environment where the restarts in the list
447 RESTARTS-FORM are associated with the condition returned by CONDITION-FORM.
448 This allows FIND-RESTART, etc., to recognize restarts that are not related
449 to the error currently being debugged. See also RESTART-CASE."
450 (once-only ((restarts restarts-form))
451 (with-unique-names (restart)
452 ;; FIXME: check the need for interrupt-safety.
453 `(unwind-protect
454 (progn
455 (dolist (,restart ,restarts)
456 (push ,condition-form
457 (restart-associated-conditions ,restart)))
458 ,@body)
459 (dolist (,restart ,restarts)
460 (pop (restart-associated-conditions ,restart)))))))
462 (defmacro-mundanely restart-bind (bindings &body forms)
463 #!+sb-doc
464 "(RESTART-BIND ({(case-name function {keyword value}*)}*) forms)
465 Executes forms in a dynamic context where the given bindings are in
466 effect. Users probably want to use RESTART-CASE. A case-name of NIL
467 indicates an anonymous restart. When bindings contain the same
468 restart name, FIND-RESTART will find the first such binding."
469 (flet ((parse-binding (binding)
470 (unless (>= (length binding) 2)
471 (error "ill-formed restart binding: ~S" binding))
472 (destructuring-bind (name function
473 &key interactive-function
474 test-function
475 report-function)
476 binding
477 (unless (or name report-function)
478 (warn "Unnamed restart does not have a report function: ~
479 ~S" binding))
480 `(make-restart ',name ,function
481 ,report-function
482 ,interactive-function
483 ,@(and test-function
484 `(,test-function))))))
485 `(let ((*restart-clusters*
486 (cons (list ,@(mapcar #'parse-binding bindings))
487 *restart-clusters*)))
488 ,@forms)))
490 ;;; Wrap the RESTART-CASE expression in a WITH-CONDITION-RESTARTS if
491 ;;; appropriate. Gross, but it's what the book seems to say...
492 (defun munge-restart-case-expression (expression env)
493 (let ((exp (%macroexpand expression env)))
494 (if (consp exp)
495 (let* ((name (car exp))
496 (args (if (eq name 'cerror) (cddr exp) (cdr exp))))
497 (if (member name '(signal error cerror warn))
498 (once-only ((n-cond `(coerce-to-condition
499 ,(first args)
500 (list ,@(rest args))
501 ',(case name
502 (warn 'simple-warning)
503 (signal 'simple-condition)
504 (t 'simple-error))
505 ',name)))
506 `(with-condition-restarts
507 ,n-cond
508 (car *restart-clusters*)
509 ,(if (eq name 'cerror)
510 `(cerror ,(second exp) ,n-cond)
511 `(,name ,n-cond))))
512 expression))
513 expression)))
515 (defmacro-mundanely restart-case (expression &body clauses &environment env)
516 #!+sb-doc
517 "(RESTART-CASE form {(case-name arg-list {keyword value}* body)}*)
518 The form is evaluated in a dynamic context where the clauses have
519 special meanings as points to which control may be transferred (see
520 INVOKE-RESTART). When clauses contain the same case-name,
521 FIND-RESTART will find the first such clause. If form is a call to
522 SIGNAL, ERROR, CERROR or WARN (or macroexpands into such) then the
523 signalled condition will be associated with the new restarts."
524 ;; PARSE-CLAUSE (which uses PARSE-KEYWORDS-AND-BODY) is used to
525 ;; parse all clauses into lists of the form
527 ;; (NAME TAG KEYWORDS LAMBDA-LIST BODY)
529 ;; where KEYWORDS are suitable keywords for use in HANDLER-BIND
530 ;; bindings. These lists are then passed to
531 ;; * MAKE-BINDING which generates bindings for the respective NAME
532 ;; for HANDLER-BIND
533 ;; * MAKE-APPLY-AND-RETURN which generates TAGBODY entries executing
534 ;; the respective BODY.
535 (let ((block-tag (sb!xc:gensym "BLOCK"))
536 (temp-var (gensym)))
537 (labels ((parse-keywords-and-body (keywords-and-body)
538 (do ((form keywords-and-body (cddr form))
539 (result '())) (nil)
540 (destructuring-bind (&optional key (arg nil argp) &rest rest)
541 form
542 (declare (ignore rest))
543 (setq result
544 (append
545 (cond
546 ((and (eq key :report) argp)
547 (list :report-function
548 (if (stringp arg)
549 `#'(lambda (stream)
550 (write-string ,arg stream))
551 `#',arg)))
552 ((and (eq key :interactive) argp)
553 (list :interactive-function `#',arg))
554 ((and (eq key :test) argp)
555 (list :test-function `#',arg))
557 (return (values result form))))
558 result)))))
559 (parse-clause (clause)
560 (unless (and (listp clause) (>= (length clause) 2)
561 (listp (second clause)))
562 (error "ill-formed ~S clause, no lambda-list:~% ~S"
563 'restart-case clause))
564 (destructuring-bind (name lambda-list &body body) clause
565 (multiple-value-bind (keywords body)
566 (parse-keywords-and-body body)
567 (list name (sb!xc:gensym "TAG") keywords lambda-list body))))
568 (make-binding (clause-data)
569 (destructuring-bind (name tag keywords lambda-list body) clause-data
570 (declare (ignore body))
571 `(,name
572 (lambda ,(cond ((null lambda-list)
574 ((and (null (cdr lambda-list))
575 (not (member (car lambda-list)
576 '(&optional &key &aux))))
577 '(temp))
579 '(&rest temp)))
580 ,@(when lambda-list `((setq ,temp-var temp)))
581 (locally (declare (optimize (safety 0)))
582 (go ,tag)))
583 ,@keywords)))
584 (make-apply-and-return (clause-data)
585 (destructuring-bind (name tag keywords lambda-list body) clause-data
586 (declare (ignore name keywords))
587 `(,tag (return-from ,block-tag
588 ,(cond ((null lambda-list)
589 `(progn ,@body))
590 ((and (null (cdr lambda-list))
591 (not (member (car lambda-list)
592 '(&optional &key &aux))))
593 `(funcall (lambda ,lambda-list ,@body) ,temp-var))
595 `(apply (lambda ,lambda-list ,@body) ,temp-var))))))))
596 (let ((clauses-data (mapcar #'parse-clause clauses)))
597 `(block ,block-tag
598 (let ((,temp-var nil))
599 (declare (ignorable ,temp-var))
600 (tagbody
601 (restart-bind
602 ,(mapcar #'make-binding clauses-data)
603 (return-from ,block-tag
604 ,(munge-restart-case-expression expression env)))
605 ,@(mapcan #'make-apply-and-return clauses-data))))))))
607 (defmacro-mundanely with-simple-restart ((restart-name format-string
608 &rest format-arguments)
609 &body forms)
610 #!+sb-doc
611 "(WITH-SIMPLE-RESTART (restart-name format-string format-arguments)
612 body)
613 If restart-name is not invoked, then all values returned by forms are
614 returned. If control is transferred to this restart, it immediately
615 returns the values NIL and T."
616 (let ((stream (sb!xc:gensym "STREAM")))
617 `(restart-case
618 ;; If there's just one body form, then don't use PROGN. This allows
619 ;; RESTART-CASE to "see" calls to ERROR, etc.
620 ,(if (= (length forms) 1) (car forms) `(progn ,@forms))
621 (,restart-name ()
622 :report (lambda (,stream)
623 (format ,stream ,format-string ,@format-arguments))
624 (values nil t)))))
626 (defmacro-mundanely %handler-bind (bindings form)
627 ;; As an optimization, this looks at the handler parts of BINDINGS
628 ;; and turns handlers of the forms (lambda ...) and (function
629 ;; (lambda ...)) into local, dynamic-extent functions.
630 (let ((local-functions '())
631 (cluster-entries '()))
632 (labels ((cons-form (type handler)
633 `(cons ',type ,handler))
634 (local-function (type lambda-form)
635 (let ((name (sb!xc:gensym "HANDLER")))
636 (push `(,name ,@(rest lambda-form)) local-functions)
637 (push (cons-form type `(function ,name)) cluster-entries)))
638 (process-binding (binding)
639 (unless (proper-list-of-length-p binding 2)
640 (error "ill-formed handler binding: ~S" binding))
641 (destructuring-bind (type handler) binding
642 (typecase handler
643 ((cons (eql lambda) t)
644 (local-function type handler))
645 ((cons (eql function)
646 (cons (cons (eql lambda) t) t))
647 (local-function type (second handler)))
649 (push (apply #'cons-form binding) cluster-entries))))))
650 (mapc #'process-binding bindings)
651 `(dx-flet (,@(reverse local-functions))
652 (let ((*handler-clusters*
653 (list* (list ,@(nreverse cluster-entries)) *handler-clusters*)))
654 #!+stack-allocatable-fixed-objects
655 (declare (truly-dynamic-extent *handler-clusters*))
656 (progn ,form))))))
658 (defmacro-mundanely handler-bind (bindings &body forms)
659 #!+sb-doc
660 "(HANDLER-BIND ( {(type handler)}* ) body)
662 Executes body in a dynamic context where the given handler bindings are in
663 effect. Each handler must take the condition being signalled as an argument.
664 The bindings are searched first to last in the event of a signalled
665 condition."
666 `(%handler-bind ,bindings
667 #!-x86 (progn ,@forms)
668 ;; Need to catch FP errors here!
669 #!+x86 (multiple-value-prog1 (progn ,@forms) (float-wait))))
671 (defmacro-mundanely handler-case (form &rest cases)
672 #!+sb-doc
673 "(HANDLER-CASE form { (type ([var]) body) }* )
675 Execute FORM in a context with handlers established for the condition types. A
676 peculiar property allows type to be :NO-ERROR. If such a clause occurs, and
677 form returns normally, all its values are passed to this clause as if by
678 MULTIPLE-VALUE-CALL. The :NO-ERROR clause accepts more than one var
679 specification."
680 (let ((no-error-clause (assoc ':no-error cases)))
681 (if no-error-clause
682 (let ((normal-return (make-symbol "normal-return"))
683 (error-return (make-symbol "error-return")))
684 `(block ,error-return
685 (multiple-value-call (lambda ,@(cdr no-error-clause))
686 (block ,normal-return
687 (return-from ,error-return
688 (handler-case (return-from ,normal-return ,form)
689 ,@(remove no-error-clause cases)))))))
690 (let* ((local-funs nil)
691 (annotated-cases
692 (mapcar (lambda (case)
693 (with-unique-names (tag fun)
694 (destructuring-bind (type ll &body body) case
695 (push `(,fun ,ll ,@body) local-funs)
696 (list tag type ll fun))))
697 cases)))
698 (with-unique-names (block cell form-fun)
699 `(dx-flet ((,form-fun ()
700 #!-x86 ,form
701 ;; Need to catch FP errors here!
702 #!+x86 (multiple-value-prog1 ,form (float-wait)))
703 ,@(reverse local-funs))
704 (declare (optimize (sb!c::check-tag-existence 0)))
705 (block ,block
706 ;; KLUDGE: We use a dx CONS cell instead of just assigning to
707 ;; the variable directly, so that we can stack allocate
708 ;; robustly: dx value cells don't work quite right, and it is
709 ;; possible to construct user code that should loop
710 ;; indefinitely, but instead eats up some stack each time
711 ;; around.
712 (dx-let ((,cell (cons :condition nil)))
713 (declare (ignorable ,cell))
714 (tagbody
715 (%handler-bind
716 ,(mapcar (lambda (annotated-case)
717 (destructuring-bind (tag type ll fun-name) annotated-case
718 (declare (ignore fun-name))
719 (list type
720 `(lambda (temp)
721 ,(if ll
722 `(setf (cdr ,cell) temp)
723 '(declare (ignore temp)))
724 (go ,tag)))))
725 annotated-cases)
726 (return-from ,block (,form-fun)))
727 ,@(mapcan
728 (lambda (annotated-case)
729 (destructuring-bind (tag type ll fun-name) annotated-case
730 (declare (ignore type))
731 (list tag
732 `(return-from ,block
733 ,(if ll
734 `(,fun-name (cdr ,cell))
735 `(,fun-name))))))
736 annotated-cases))))))))))
738 ;;;; miscellaneous
740 (defmacro-mundanely return (&optional (value nil))
741 `(return-from nil ,value))
743 (defmacro-mundanely psetq (&rest pairs)
744 #!+sb-doc
745 "PSETQ {var value}*
746 Set the variables to the values, like SETQ, except that assignments
747 happen in parallel, i.e. no assignments take place until all the
748 forms have been evaluated."
749 ;; Given the possibility of symbol-macros, we delegate to PSETF
750 ;; which knows how to deal with them, after checking that syntax is
751 ;; compatible with PSETQ.
752 (do ((pair pairs (cddr pair)))
753 ((endp pair) `(psetf ,@pairs))
754 (unless (symbolp (car pair))
755 (error 'simple-program-error
756 :format-control "variable ~S in PSETQ is not a SYMBOL"
757 :format-arguments (list (car pair))))))
759 (defmacro-mundanely lambda (&whole whole args &body body)
760 (declare (ignore args body))
761 `#',whole)
763 (defmacro-mundanely named-lambda (&whole whole name args &body body)
764 (declare (ignore name args body))
765 `#',whole)
767 (defmacro-mundanely lambda-with-lexenv (&whole whole
768 declarations macros symbol-macros
769 &body body)
770 (declare (ignore declarations macros symbol-macros body))
771 `#',whole)
773 ;;; this eliminates a whole bundle of unknown function STYLE-WARNINGs
774 ;;; when cross-compiling. It's not critical for behaviour, but is
775 ;;; aesthetically pleasing, except inasmuch as there's this list of
776 ;;; magic functions here. -- CSR, 2003-04-01
777 #+sb-xc-host
778 (sb!xc:proclaim '(ftype (function * *)
779 ;; functions appearing in fundamental defining
780 ;; macro expansions:
781 %compiler-deftype
782 %compiler-defvar
783 %defun
784 %defsetf
785 %defparameter
786 %defvar
787 sb!c:%compiler-defun
788 sb!c::%define-symbol-macro
789 sb!c::%defconstant
790 sb!c::%define-compiler-macro
791 sb!c::%defmacro
792 sb!kernel::%compiler-defstruct
793 sb!kernel::%compiler-define-condition
794 sb!kernel::%defstruct
795 sb!kernel::%define-condition
796 ;; miscellaneous functions commonly appearing
797 ;; as a result of macro expansions or compiler
798 ;; transformations:
799 sb!kernel::arg-count-error ; PARSE-DEFMACRO