Silence some warnings with CCL-hosted build.
[sbcl.git] / src / code / defboot.lisp
blob2d234a70365b31ffd2f645e4799a6a95e2b7b942
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 (sb!xc:proclaim '(special *package*))
27 (sb!xc:defmacro in-package (string-designator)
28 (let ((string (string string-designator)))
29 `(eval-when (:compile-toplevel :load-toplevel :execute)
30 (setq *package* (find-undeleted-package-or-lose ,string)))))
32 ;;;; MULTIPLE-VALUE-FOO
34 (flet ((validate-vars (vars)
35 (unless (and (listp vars) (every #'symbolp vars))
36 (error "Vars is not a list of symbols: ~S" vars))))
38 (sb!xc:defmacro multiple-value-bind (vars value-form &body body)
39 (validate-vars vars)
40 (if (= (length vars) 1)
41 ;; Not only does it look nicer to reduce to LET in this special case,
42 ;; if might produce better code or at least compile quicker.
43 ;; Certainly for the evaluator it's preferable.
44 `(let ((,(car vars) ,value-form))
45 ,@body)
46 (let ((ignore (sb!xc:gensym)))
47 `(multiple-value-call #'(lambda (&optional ,@(mapcar #'list vars)
48 &rest ,ignore)
49 (declare (ignore ,ignore))
50 ,@body)
51 ,value-form))))
53 (sb!xc:defmacro multiple-value-setq (vars value-form)
54 (validate-vars 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 (sb!xc:defmacro multiple-value-list (value-form)
64 `(multiple-value-call #'list ,value-form))
66 ;;;; various conditional constructs
68 ;;; COND defined in terms of IF
69 (sb!xc:defmacro 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 (and (eq test t)
89 (not more))
90 ;; THE to preserve non-toplevelness for FOO in
91 ;; (COND (T (FOO)))
92 `(the t (progn ,@forms))
93 `(if ,test
94 (progn ,@forms)
95 ,(when more `(cond ,@more))))))))))
97 (flet ((prognify (forms)
98 (cond ((singleton-p forms) (car forms))
99 ((not forms) nil)
100 (t `(progn ,@forms)))))
101 (sb!xc:defmacro when (test &body forms)
102 #!+sb-doc
103 "If the first argument is true, the rest of the forms are
104 evaluated as a PROGN."
105 `(if ,test ,(prognify forms)))
107 (sb!xc:defmacro unless (test &body forms)
108 #!+sb-doc
109 "If the first argument is not true, the rest of the forms are
110 evaluated as a PROGN."
111 `(if ,test nil ,(prognify forms))))
113 (sb!xc:defmacro and (&rest forms)
114 (cond ((endp forms) t)
115 ((endp (rest forms))
116 ;; Preserve non-toplevelness of the form!
117 `(the t ,(first forms)))
119 `(if ,(first forms)
120 (and ,@(rest forms))
121 nil))))
123 (sb!xc:defmacro or (&rest forms)
124 (cond ((endp forms) nil)
125 ((endp (rest forms))
126 ;; Preserve non-toplevelness of the form!
127 `(the t ,(first forms)))
129 (let ((n-result (gensym)))
130 `(let ((,n-result ,(first forms)))
131 (if ,n-result
132 ,n-result
133 (or ,@(rest forms))))))))
135 ;;;; various sequencing constructs
137 (flet ((prog-expansion-from-let (varlist body-decls let)
138 (multiple-value-bind (body decls) (parse-body body-decls nil)
139 `(block nil
140 (,let ,varlist
141 ,@decls
142 (tagbody ,@body))))))
143 (sb!xc:defmacro prog (varlist &body body-decls)
144 (prog-expansion-from-let varlist body-decls 'let))
145 (sb!xc:defmacro prog* (varlist &body body-decls)
146 (prog-expansion-from-let varlist body-decls 'let*)))
148 (sb!xc:defmacro prog1 (result &body body)
149 (let ((n-result (gensym)))
150 `(let ((,n-result ,result))
151 ,@body
152 ,n-result)))
154 (sb!xc:defmacro 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 (sb!xc:defmacro defun (&environment env name lambda-list &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 t)
186 (let* (;; stuff shared between LAMBDA and INLINE-LAMBDA and NAMED-LAMBDA
187 (lambda-guts `(,@decls (block ,(fun-name-block-name name) ,@forms)))
188 (lambda `(lambda ,lambda-list ,@lambda-guts))
189 (named-lambda `(named-lambda ,name ,lambda-list
190 ,@(when doc (list doc)) ,@lambda-guts))
191 (inline-thing
192 (or (sb!kernel::defstruct-generated-defn-p name lambda-list body)
193 (when (inline-fun-name-p name)
194 ;; we want to attempt to inline, so complain if we can't
195 (acond ((sb!c:maybe-inline-syntactic-closure lambda env)
196 (list 'quote it))
198 (#+sb-xc-host warn
199 #-sb-xc-host sb!c:maybe-compiler-notify
200 "lexical environment too hairy, can't inline DEFUN ~S"
201 name)
202 nil))))))
203 `(progn
204 (eval-when (:compile-toplevel)
205 (sb!c:%compiler-defun ',name ,inline-thing t))
206 (%defun ',name ,named-lambda (sb!c:source-location)
207 ,@(and inline-thing (list inline-thing)))
208 ;; This warning, if produced, comes after the DEFUN happens.
209 ;; When compiling, there's no real difference, but when interpreting,
210 ;; if there is a handler for style-warning that nonlocally exits,
211 ;; it's wrong to have skipped the DEFUN itself, since if there is no
212 ;; function, then the warning ought not to have been issued at all.
213 ,@(when (typep name '(cons (eql setf)))
214 `((eval-when (:compile-toplevel :execute)
215 (sb!c::warn-if-setf-macro ',name))
216 ',name))))))
218 #-sb-xc-host
219 (defun %defun (name def source-location &optional inline-lambda)
220 (declare (type function def))
221 ;; should've been checked by DEFMACRO DEFUN
222 (aver (legal-fun-name-p name))
223 (sb!c:%compiler-defun name inline-lambda nil)
224 (when (fboundp name)
225 (warn 'redefinition-with-defun
226 :name name :new-function def :new-location source-location))
227 (setf (sb!xc:fdefinition name) def)
228 ;; %COMPILER-DEFUN doesn't do this except at compile-time, when it
229 ;; also checks package locks. By doing this here we let (SETF
230 ;; FDEFINITION) do the load-time package lock checking before
231 ;; we frob any existing inline expansions.
232 (sb!c::%set-inline-expansion name nil inline-lambda)
233 (sb!c::note-name-defined name :function)
234 name)
236 ;;;; DEFVAR and DEFPARAMETER
238 (sb!xc:defmacro defvar (var &optional (val nil valp) (doc nil docp))
239 #!+sb-doc
240 "Define a special variable at top level. Declare the variable
241 SPECIAL and, optionally, initialize it. If the variable already has a
242 value, the old value is not clobbered. The third argument is an optional
243 documentation string for the variable."
244 `(progn
245 (eval-when (:compile-toplevel)
246 (%compiler-defvar ',var))
247 (%defvar ',var
248 (sb!c:source-location)
249 ,@(and valp
250 `((unless (boundp ',var) ,val)))
251 ,@(and docp
252 `(',doc)))))
254 (sb!xc:defmacro defparameter (var val &optional (doc nil docp))
255 #!+sb-doc
256 "Define a parameter that is not normally changed by the program,
257 but that may be changed without causing an error. Declare the
258 variable special and sets its value to VAL, overwriting any
259 previous value. The third argument is an optional documentation
260 string for the parameter."
261 `(progn
262 (eval-when (:compile-toplevel)
263 (%compiler-defvar ',var))
264 (%defparameter ',var ,val (sb!c:source-location)
265 ,@(and docp
266 `(',doc)))))
268 (defun %compiler-defvar (var)
269 (sb!xc:proclaim `(special ,var)))
271 #-sb-xc-host
272 (defun %defvar (var source-location &optional (val nil valp) (doc nil docp))
273 (%compiler-defvar var)
274 (when (and valp
275 (not (boundp var)))
276 (set var val))
277 (when docp
278 (setf (fdocumentation var 'variable) doc))
279 (when source-location
280 (setf (info :source-location :variable var) source-location))
281 var)
283 #-sb-xc-host
284 (defun %defparameter (var val source-location &optional (doc nil docp))
285 (%compiler-defvar var)
286 (set var val)
287 (when docp
288 (setf (fdocumentation var 'variable) doc))
289 (when source-location
290 (setf (info :source-location :variable var) source-location))
291 var)
293 ;;;; iteration constructs
295 (flet
296 ((frob-do-body (varlist endlist decls-and-code bind step name block)
297 ;; Check for illegal old-style DO.
298 (when (or (not (listp varlist)) (atom endlist))
299 (error "ill-formed ~S -- possibly illegal old style DO?" name))
300 (collect ((steps))
301 (let ((inits
302 (mapcar (lambda (var)
303 (or (cond ((symbolp var) var)
304 ((listp var)
305 (unless (symbolp (first var))
306 (error "~S step variable is not a symbol: ~S"
307 name (first var)))
308 (case (length var)
309 ((1 2) var)
310 (3 (steps (first var) (third var))
311 (list (first var) (second var))))))
312 (error "~S is an illegal form for a ~S varlist."
313 var name)))
314 varlist)))
315 (multiple-value-bind (code decls) (parse-body decls-and-code nil)
316 (let ((label-1 (sb!xc:gensym)) (label-2 (sb!xc:gensym)))
317 `(block ,block
318 (,bind ,inits
319 ,@decls
320 (tagbody
321 (go ,label-2)
322 ,label-1
323 (tagbody ,@code)
324 (,step ,@(steps))
325 ,label-2
326 (unless ,(first endlist) (go ,label-1))
327 (return-from ,block (progn ,@(rest endlist))))))))))))
329 ;; This is like DO, except it has no implicit NIL block.
330 (sb!xc:defmacro do-anonymous (varlist endlist &rest body)
331 (frob-do-body varlist endlist body 'let 'psetq 'do-anonymous (sb!xc:gensym)))
333 (sb!xc:defmacro do (varlist endlist &body body)
334 #!+sb-doc
335 "DO ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
336 Iteration construct. Each Var is initialized in parallel to the value of the
337 specified Init form. On subsequent iterations, the Vars are assigned the
338 value of the Step form (if any) in parallel. The Test is evaluated before
339 each evaluation of the body Forms. When the Test is true, the Exit-Forms
340 are evaluated as a PROGN, with the result being the value of the DO. A block
341 named NIL is established around the entire expansion, allowing RETURN to be
342 used as an alternate exit mechanism."
343 (frob-do-body varlist endlist body 'let 'psetq 'do nil))
345 (sb!xc:defmacro do* (varlist endlist &body body)
346 #!+sb-doc
347 "DO* ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
348 Iteration construct. Each Var is initialized sequentially (like LET*) to the
349 value of the specified Init form. On subsequent iterations, the Vars are
350 sequentially assigned the value of the Step form (if any). The Test is
351 evaluated before each evaluation of the body Forms. When the Test is true,
352 the Exit-Forms are evaluated as a PROGN, with the result being the value
353 of the DO. A block named NIL is established around the entire expansion,
354 allowing RETURN to be used as an alternate exit mechanism."
355 (frob-do-body varlist endlist body 'let* 'setq 'do* nil)))
357 (sb!xc:defmacro dotimes ((var count &optional (result nil)) &body body)
358 ;; A nice optimization would be that if VAR is never referenced,
359 ;; it's slightly more efficient to count backwards, but that's tricky.
360 (let ((c (if (integerp count) count (sb!xc:gensym))))
361 `(do ((,var 0 (1+ ,var))
362 ,@(if (symbolp c) `((,c (the integer ,count)))))
363 ((>= ,var ,c) ,result)
364 (declare (type unsigned-byte ,var))
365 ,@body)))
367 (sb!xc:defmacro dolist ((var list &optional (result nil)) &body body &environment env)
368 ;; We repeatedly bind the var instead of setting it so that we never
369 ;; have to give the var an arbitrary value such as NIL (which might
370 ;; conflict with a declaration). If there is a result form, we
371 ;; introduce a gratuitous binding of the variable to NIL without the
372 ;; declarations, then evaluate the result form in that
373 ;; environment. We spuriously reference the gratuitous variable,
374 ;; since we don't want to use IGNORABLE on what might be a special
375 ;; var.
376 (multiple-value-bind (forms decls) (parse-body body nil)
377 (let* ((n-list (gensym "N-LIST"))
378 (start (gensym "START")))
379 (multiple-value-bind (clist members clist-ok)
380 (cond ((sb!xc:constantp list env)
381 (let ((value (constant-form-value list env)))
382 (multiple-value-bind (all dot) (list-members value :max-length 20)
383 (when (eql dot t)
384 ;; Full warning is too much: the user may terminate the loop
385 ;; early enough. Contents are still right, though.
386 (style-warn "Dotted list ~S in DOLIST." value))
387 (if (eql dot :maybe)
388 (values value nil nil)
389 (values value all t)))))
390 ((and (consp list) (eq 'list (car list))
391 (every (lambda (arg) (sb!xc:constantp arg env)) (cdr list)))
392 (let ((values (mapcar (lambda (arg) (constant-form-value arg env)) (cdr list))))
393 (values values values t)))
395 (values nil nil nil)))
396 `(block nil
397 (let ((,n-list ,(if clist-ok (list 'quote clist) list)))
398 (tagbody
399 ,start
400 (unless (endp ,n-list)
401 (let ((,var ,(if clist-ok
402 `(truly-the (member ,@members) (car ,n-list))
403 `(car ,n-list))))
404 ,@decls
405 (setq ,n-list (cdr ,n-list))
406 (tagbody ,@forms))
407 (go ,start))))
408 ,(if result
409 `(let ((,var nil))
410 ;; Filter out TYPE declarations (VAR gets bound to NIL,
411 ;; and might have a conflicting type declaration) and
412 ;; IGNORE (VAR might be ignored in the loop body, but
413 ;; it's used in the result form).
414 ,@(filter-dolist-declarations decls)
415 ,var
416 ,result)
417 nil))))))
419 ;;;; conditions, handlers, restarts
421 ;;; KLUDGE: we PROCLAIM these special here so that we can use restart
422 ;;; macros in the compiler before the DEFVARs are compiled.
424 ;;; For an explanation of these data structures, see DEFVARs in
425 ;;; target-error.lisp.
426 (sb!xc:proclaim '(special *handler-clusters* *restart-clusters*))
428 ;;; Generated code need not check for unbound-marker in *HANDLER-CLUSTERS*
429 ;;; (resp *RESTART-). To elicit this we must poke at the info db.
430 ;;; SB!XC:PROCLAIM SPECIAL doesn't advise the host Lisp that *HANDLER-CLUSTERS*
431 ;;; is special and so it rightfully complains about a SETQ of the variable.
432 ;;; But I must SETQ if proclaming ALWAYS-BOUND because the xc asks the host
433 ;;; whether it's currently bound.
434 ;;; But the DEFVARs are in target-error. So it's one hack or another.
435 (setf (info :variable :always-bound '*handler-clusters*)
436 #+sb-xc :always-bound #-sb-xc :eventually)
437 (setf (info :variable :always-bound '*restart-clusters*)
438 #+sb-xc :always-bound #-sb-xc :eventually)
440 (sb!xc:defmacro with-condition-restarts
441 (condition-form restarts-form &body body)
442 #!+sb-doc
443 "Evaluates the BODY in a dynamic environment where the restarts in the list
444 RESTARTS-FORM are associated with the condition returned by CONDITION-FORM.
445 This allows FIND-RESTART, etc., to recognize restarts that are not related
446 to the error currently being debugged. See also RESTART-CASE."
447 (once-only ((condition-form condition-form)
448 (restarts restarts-form))
449 (with-unique-names (restart)
450 ;; FIXME: check the need for interrupt-safety.
451 `(unwind-protect
452 (progn
453 (dolist (,restart ,restarts)
454 (push ,condition-form
455 (restart-associated-conditions ,restart)))
456 ,@body)
457 (dolist (,restart ,restarts)
458 (pop (restart-associated-conditions ,restart)))))))
460 (sb!xc:defmacro restart-bind (bindings &body forms)
461 #!+sb-doc
462 "(RESTART-BIND ({(case-name function {keyword value}*)}*) forms)
463 Executes forms in a dynamic context where the given bindings are in
464 effect. Users probably want to use RESTART-CASE. A case-name of NIL
465 indicates an anonymous restart. When bindings contain the same
466 restart name, FIND-RESTART will find the first such binding."
467 (flet ((parse-binding (binding)
468 (unless (>= (length binding) 2)
469 (error "ill-formed restart binding: ~S" binding))
470 (destructuring-bind (name function
471 &key interactive-function
472 test-function
473 report-function)
474 binding
475 (unless (or name report-function)
476 (warn "Unnamed restart does not have a report function: ~
477 ~S" binding))
478 `(make-restart ',name ,function
479 ,@(and (or report-function
480 interactive-function
481 test-function)
482 `(,report-function))
483 ,@(and (or interactive-function
484 test-function)
485 `(,interactive-function))
486 ,@(and test-function
487 `(,test-function))))))
488 `(let ((*restart-clusters*
489 (cons (list ,@(mapcar #'parse-binding bindings))
490 *restart-clusters*)))
491 (declare (truly-dynamic-extent *restart-clusters*))
492 ,@forms)))
494 ;;; Wrap the RESTART-CASE expression in a WITH-CONDITION-RESTARTS if
495 ;;; appropriate. Gross, but it's what the book seems to say...
496 (defun munge-restart-case-expression (expression env)
497 (let ((exp (%macroexpand expression env)))
498 (if (consp exp)
499 (let* ((name (car exp))
500 (args (if (eq name 'cerror) (cddr exp) (cdr exp))))
501 (if (member name '(signal error cerror warn))
502 (once-only ((n-cond `(coerce-to-condition
503 ,(first args)
504 (list ,@(rest args))
505 ',(case name
506 (warn 'simple-warning)
507 (signal 'simple-condition)
508 (t 'simple-error))
509 ',name)))
510 `(with-condition-restarts
511 ,n-cond
512 (car *restart-clusters*)
513 ,(if (eq name 'cerror)
514 `(cerror ,(second exp) ,n-cond)
515 `(,name ,n-cond))))
516 expression))
517 expression)))
519 (sb!xc:defmacro restart-case (expression &body clauses &environment env)
520 #!+sb-doc
521 "(RESTART-CASE form {(case-name arg-list {keyword value}* body)}*)
522 The form is evaluated in a dynamic context where the clauses have
523 special meanings as points to which control may be transferred (see
524 INVOKE-RESTART). When clauses contain the same case-name,
525 FIND-RESTART will find the first such clause. If form is a call to
526 SIGNAL, ERROR, CERROR or WARN (or macroexpands into such) then the
527 signalled condition will be associated with the new restarts."
528 ;; PARSE-CLAUSE (which uses PARSE-KEYWORDS-AND-BODY) is used to
529 ;; parse all clauses into lists of the form
531 ;; (NAME TAG KEYWORDS LAMBDA-LIST BODY)
533 ;; where KEYWORDS are suitable keywords for use in HANDLER-BIND
534 ;; bindings. These lists are then passed to
535 ;; * MAKE-BINDING which generates bindings for the respective NAME
536 ;; for HANDLER-BIND
537 ;; * MAKE-APPLY-AND-RETURN which generates TAGBODY entries executing
538 ;; the respective BODY.
539 (let ((block-tag (sb!xc:gensym "BLOCK"))
540 (temp-var (gensym)))
541 (labels ((parse-keywords-and-body (keywords-and-body)
542 (do ((form keywords-and-body (cddr form))
543 (result '())) (nil)
544 (destructuring-bind (&optional key (arg nil argp) &rest rest)
545 form
546 (declare (ignore rest))
547 (setq result
548 (append
549 (cond
550 ((and (eq key :report) argp)
551 (list :report-function
552 (if (stringp arg)
553 `#'(lambda (stream)
554 (write-string ,arg stream))
555 `#',arg)))
556 ((and (eq key :interactive) argp)
557 (list :interactive-function `#',arg))
558 ((and (eq key :test) argp)
559 (list :test-function `#',arg))
561 (return (values result form))))
562 result)))))
563 (parse-clause (clause)
564 (unless (and (listp clause) (>= (length clause) 2)
565 (listp (second clause)))
566 (error "ill-formed ~S clause, no lambda-list:~% ~S"
567 'restart-case clause))
568 (destructuring-bind (name lambda-list &body body) clause
569 (multiple-value-bind (keywords body)
570 (parse-keywords-and-body body)
571 (list name (sb!xc:gensym "TAG") keywords lambda-list body))))
572 (make-binding (clause-data)
573 (destructuring-bind (name tag keywords lambda-list body) clause-data
574 (declare (ignore body))
575 `(,name
576 (lambda ,(cond ((null lambda-list)
578 ((and (null (cdr lambda-list))
579 (not (member (car lambda-list)
580 '(&optional &key &aux))))
581 '(temp))
583 '(&rest temp)))
584 ,@(when lambda-list `((setq ,temp-var temp)))
585 (locally (declare (optimize (safety 0)))
586 (go ,tag)))
587 ,@keywords)))
588 (make-apply-and-return (clause-data)
589 (destructuring-bind (name tag keywords lambda-list body) clause-data
590 (declare (ignore name keywords))
591 `(,tag (return-from ,block-tag
592 ,(cond ((null lambda-list)
593 `(progn ,@body))
594 ((and (null (cdr lambda-list))
595 (not (member (car lambda-list)
596 '(&optional &key &aux))))
597 `(funcall (lambda ,lambda-list ,@body) ,temp-var))
599 `(apply (lambda ,lambda-list ,@body) ,temp-var))))))))
600 (let ((clauses-data (mapcar #'parse-clause clauses)))
601 `(block ,block-tag
602 (let ((,temp-var nil))
603 (declare (ignorable ,temp-var))
604 (tagbody
605 (restart-bind
606 ,(mapcar #'make-binding clauses-data)
607 (return-from ,block-tag
608 ,(munge-restart-case-expression expression env)))
609 ,@(mapcan #'make-apply-and-return clauses-data))))))))
611 (sb!xc:defmacro with-simple-restart ((restart-name format-string
612 &rest format-arguments)
613 &body forms)
614 #!+sb-doc
615 "(WITH-SIMPLE-RESTART (restart-name format-string format-arguments)
616 body)
617 If restart-name is not invoked, then all values returned by forms are
618 returned. If control is transferred to this restart, it immediately
619 returns the values NIL and T."
620 (let ((stream (sb!xc:gensym "STREAM")))
621 `(restart-case
622 ;; If there's just one body form, then don't use PROGN. This allows
623 ;; RESTART-CASE to "see" calls to ERROR, etc.
624 ,(if (= (length forms) 1) (car forms) `(progn ,@forms))
625 (,restart-name ()
626 :report (lambda (,stream)
627 (declare (type stream ,stream))
628 (format ,stream ,format-string ,@format-arguments))
629 (values nil t)))))
631 (sb!xc:defmacro %handler-bind (bindings form &environment env)
632 (unless bindings
633 (return-from %handler-bind form))
634 ;; As an optimization, this looks at the handler parts of BINDINGS
635 ;; and turns handlers of the forms (lambda ...) and (function
636 ;; (lambda ...)) into local, dynamic-extent functions.
638 ;; Type specifiers in BINDINGS which name classoids are parsed
639 ;; into the classoid, otherwise are translated local TYPEP wrappers.
641 ;; As a further optimization, it is possible to eliminate some runtime
642 ;; consing (which is a speed win if not a space win, since it's dx already)
643 ;; in special cases such as (HANDLER-BIND ((WARNING #'MUFFLE-WARNING)) ...).
644 ;; If all bindings are optimizable, then the runtime cost of making them
645 ;; is one dx cons cell for the whole cluster.
646 ;; Otherwise it takes 1+2N cons cells where N is the number of bindings.
648 (collect ((local-functions) (cluster-entries) (dummy-forms))
649 (flet ((const-cons (test handler)
650 ;; If possible, render HANDLER as a load-time constant so that
651 ;; consing the test and handler is also load-time constant.
652 (let ((name (when (typep handler
653 '(cons (member function quote)
654 (cons symbol null)))
655 (cadr handler))))
656 (cond ((or (not name)
657 (assq name (local-functions))
658 (and (eq (car handler) 'function)
659 (sb!c::fun-locally-defined-p name env)))
660 `(cons ,(case (car test)
661 ((named-lambda function) test)
662 (t `(load-time-value ,test t)))
663 ,(if (typep handler '(cons (eql function)))
664 handler
665 ;; Regardless of lexical policy, never allow
666 ;; a non-callable into handler-clusters.
667 `(let ((x ,handler))
668 (declare (optimize (safety 3)))
669 (the callable x)))))
670 ((info :function :info name) ; known
671 ;; This takes care of CONTINUE,ABORT,MUFFLE-WARNING.
672 ;; #' will be evaluated in the null environment.
673 `(load-time-value (cons ,test #',name) t))
675 ;; For each handler specified as #'F we must verify
676 ;; that F is fboundp upon entering the binding scope.
677 ;; Referencing #'F is enough to ensure a warning if the
678 ;; function isn't defined at compile-time, but the
679 ;; compiler considers it elidable unless something forces
680 ;; an apparent use of the form at runtime,
681 ;; so instead use SAFE-FDEFN-FUN on the fdefn.
682 (when (eq (car handler) 'function)
683 (dummy-forms `(sb!c:safe-fdefn-fun
684 (load-time-value
685 (find-or-create-fdefn ',name) t))))
686 ;; Resolve to an fdefn at load-time.
687 `(load-time-value
688 (cons ,test (find-or-create-fdefn ',name))
689 t)))))
691 (const-list (items)
692 ;; If the resultant list is (LIST (L-T-V ...) (L-T-V ...) ...)
693 ;; then pull the L-T-V outside.
694 (if (every (lambda (x) (typep x '(cons (eql load-time-value))))
695 items)
696 `(load-time-value (list ,@(mapcar #'second items)) t)
697 `(list ,@items))))
699 (dolist (binding bindings)
700 (unless (proper-list-of-length-p binding 2)
701 (error "ill-formed handler binding: ~S" binding))
702 (destructuring-bind (type handler) binding
703 (setq type (typexpand type env))
704 ;; Simplify a singleton AND or OR.
705 (when (typep type '(cons (member and or) (cons t null)))
706 (setf type (second type)))
707 (cluster-entries
708 (const-cons
709 ;; Compute the test expression
710 (cond ((member type '(t condition))
711 ;; Every signal is necesarily a CONDITION, so whether you
712 ;; wrote T or CONDITION, this is always an eligible handler.
713 '#'constantly-t)
714 ((typep type '(cons (eql satisfies) (cons t null)))
715 ;; (SATISFIES F) => #'F but never a local definition of F.
716 ;; The predicate is used only if needed - it's not an error
717 ;; if not fboundp (though dangerously stupid) - so just
718 ;; reference #'F for the compiler to see the use of the name.
719 ;; But (KLUDGE): since the ref is to force a compile-time
720 ;; effect, the interpreter should not see that form,
721 ;; because there is no way for it to perform an unsafe ref,
722 ;; (and it wouldn't signal a style-warning anyway),
723 ;; and so it would actually fail immediately if predicate
724 ;; were not defined.
725 (let ((name (second type)))
726 (when (typep env 'lexenv)
727 (dummy-forms `#',name))
728 `(find-or-create-fdefn ',name)))
729 ((and (symbolp type)
730 (condition-classoid-p (find-classoid type nil)))
731 ;; It's debatable whether we need to go through a
732 ;; classoid-cell instead of just using load-time-value
733 ;; on FIND-CLASS, but the extra indirection is
734 ;; safer, and no slower than what TYPEP does.
735 `(find-classoid-cell ',type :create t))
736 (t ; No runtime consing here- this is not a closure.
737 `(named-lambda (%handler-bind ,type) (c)
738 (declare (optimize (sb!c::verify-arg-count 0)))
739 (typep c ',type))))
740 ;; Compute the handler expression
741 (let ((lexpr (typecase handler
742 ((cons (eql lambda)) handler)
743 ((cons (eql function)
744 (cons (cons (eql lambda)) null))
745 (cadr handler)))))
746 (if lexpr
747 (let ((name (let ((sb!xc:*gensym-counter*
748 (length (cluster-entries))))
749 (sb!xc:gensym "H"))))
750 (local-functions `(,name ,@(rest lexpr)))
751 `#',name)
752 handler))))))
754 `(dx-flet ,(local-functions)
755 ,@(dummy-forms)
756 (dx-let ((*handler-clusters*
757 (cons ,(const-list (cluster-entries))
758 *handler-clusters*)))
759 ,form)))))
761 (sb!xc:defmacro handler-bind (bindings &body forms)
762 #!+sb-doc
763 "(HANDLER-BIND ( {(type handler)}* ) body)
765 Executes body in a dynamic context where the given handler bindings are in
766 effect. Each handler must take the condition being signalled as an argument.
767 The bindings are searched first to last in the event of a signalled
768 condition."
769 ;; Bindings which meet specific criteria can be established with
770 ;; slightly less runtime overhead than in general.
771 ;; To allow the optimization, TYPE must be either be (SATISFIES P)
772 ;; or a symbol naming a condition class at compile time,
773 ;; and HANDLER must be a global function specified as either 'F or #'F.
774 `(%handler-bind ,bindings
775 #!-x86 (progn ,@forms)
776 ;; Need to catch FP errors here!
777 #!+x86 (multiple-value-prog1 (progn ,@forms) (float-wait))))
779 (sb!xc:defmacro handler-case (form &rest cases)
780 #!+sb-doc
781 "(HANDLER-CASE form { (type ([var]) body) }* )
783 Execute FORM in a context with handlers established for the condition types. A
784 peculiar property allows type to be :NO-ERROR. If such a clause occurs, and
785 form returns normally, all its values are passed to this clause as if by
786 MULTIPLE-VALUE-CALL. The :NO-ERROR clause accepts more than one var
787 specification."
788 (let ((no-error-clause (assoc ':no-error cases)))
789 (if no-error-clause
790 (let ((normal-return (make-symbol "normal-return"))
791 (error-return (make-symbol "error-return")))
792 `(block ,error-return
793 (multiple-value-call (lambda ,@(cdr no-error-clause))
794 (block ,normal-return
795 (return-from ,error-return
796 (handler-case (return-from ,normal-return ,form)
797 ,@(remove no-error-clause cases)))))))
798 (let* ((local-funs nil)
799 (annotated-cases
800 (mapcar (lambda (case)
801 (with-unique-names (tag fun)
802 (destructuring-bind (type ll &body body) case
803 (push `(,fun ,ll ,@body) local-funs)
804 (list tag type ll fun))))
805 cases)))
806 (with-unique-names (block cell form-fun)
807 `(dx-flet ((,form-fun ()
808 #!-x86 ,form
809 ;; Need to catch FP errors here!
810 #!+x86 (multiple-value-prog1 ,form (float-wait)))
811 ,@(reverse local-funs))
812 (declare (optimize (sb!c::check-tag-existence 0)))
813 (block ,block
814 ;; KLUDGE: We use a dx CONS cell instead of just assigning to
815 ;; the variable directly, so that we can stack allocate
816 ;; robustly: dx value cells don't work quite right, and it is
817 ;; possible to construct user code that should loop
818 ;; indefinitely, but instead eats up some stack each time
819 ;; around.
820 (dx-let ((,cell (cons :condition nil)))
821 (declare (ignorable ,cell))
822 (tagbody
823 (%handler-bind
824 ,(mapcar (lambda (annotated-case)
825 (destructuring-bind (tag type ll fun-name) annotated-case
826 (declare (ignore fun-name))
827 (list type
828 `(lambda (temp)
829 ,(if ll
830 `(setf (cdr ,cell) temp)
831 '(declare (ignore temp)))
832 (go ,tag)))))
833 annotated-cases)
834 (return-from ,block (,form-fun)))
835 ,@(mapcan
836 (lambda (annotated-case)
837 (destructuring-bind (tag type ll fun-name) annotated-case
838 (declare (ignore type))
839 (list tag
840 `(return-from ,block
841 ,(if ll
842 `(,fun-name (cdr ,cell))
843 `(,fun-name))))))
844 annotated-cases))))))))))
846 ;;;; miscellaneous
848 (sb!xc:defmacro return (&optional (value nil))
849 `(return-from nil ,value))
851 (sb!xc:defmacro lambda (&whole whole args &body body)
852 (declare (ignore args body))
853 `#',whole)
855 (sb!xc:defmacro named-lambda (&whole whole name args &body body)
856 (declare (ignore name args body))
857 `#',whole)
859 (sb!xc:defmacro lambda-with-lexenv (&whole whole
860 declarations macros symbol-macros
861 &body body)
862 (declare (ignore declarations macros symbol-macros body))
863 `#',whole)