1 ;;;; bootstrapping fundamental machinery (e.g. DEFUN, DEFCONSTANT,
2 ;;;; DEFVAR) from special forms and primitive functions
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.
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")
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
)
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
))
46 (let ((ignore (sb!xc
:gensym
)))
47 `(multiple-value-call #'(lambda (&optional
,@(mapcar #'list vars
)
49 (declare (ignore ,ignore
))
53 (sb!xc
:defmacro multiple-value-setq
(vars value-form
)
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:
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
)
72 (let ((clause (first clauses
))
73 (more (rest clauses
)))
75 (error 'simple-type-error
76 :format-control
"COND clause is not a ~S: ~S"
77 :format-arguments
(list 'cons clause
)
80 (let ((test (first clause
))
81 (forms (rest clause
)))
83 (let ((n-result (gensym)))
84 `(let ((,n-result
,test
))
90 ;; THE to preserve non-toplevelness for FOO in
92 `(the t
(progn ,@forms
))
95 ,(when more
`(cond ,@more
))))))))))
97 (flet ((prognify (forms)
98 (cond ((singleton-p forms
) (car forms
))
100 (t `(progn ,@forms
)))))
101 (sb!xc
:defmacro when
(test &body forms
)
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
)
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
)
116 ;; Preserve non-toplevelness of the form!
117 `(the t
,(first forms
)))
123 (sb!xc
:defmacro or
(&rest forms
)
124 (cond ((endp forms
) nil
)
126 ;; Preserve non-toplevelness of the form!
127 `(the t
,(first forms
)))
129 (let ((n-result (gensym)))
130 `(let ((,n-result
,(first forms
)))
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
)
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
))
154 (sb!xc
:defmacro prog2
(form1 result
&body body
)
155 `(prog1 (progn ,form1
,result
) ,@body
))
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))
169 ;; (DECLAIM (NOTINLINE FOO))
170 ;; has been used, and then we later do another
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
)
181 "Define a function at top level."
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
))
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
)
199 #-sb-xc-host sb
!c
:maybe-compiler-notify
200 "lexical environment too hairy, can't inline DEFUN ~S"
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
))
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
)
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
)
236 ;;;; DEFVAR and DEFPARAMETER
238 (sb!xc
:defmacro defvar
(var &optional
(val nil valp
) (doc nil docp
))
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."
245 (eval-when (:compile-toplevel
)
246 (%compiler-defvar
',var
))
248 (sb!c
:source-location
)
250 `((unless (boundp ',var
) ,val
)))
254 (sb!xc
:defmacro defparameter
(var val
&optional
(doc nil docp
))
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."
262 (eval-when (:compile-toplevel
)
263 (%compiler-defvar
',var
))
264 (%defparameter
',var
,val
(sb!c
:source-location
)
268 (defun %compiler-defvar
(var)
269 (sb!xc
:proclaim
`(special ,var
)))
272 (defun %defvar
(var source-location
&optional
(val nil valp
) (doc nil docp
))
273 (%compiler-defvar var
)
278 (setf (fdocumentation var
'variable
) doc
))
279 (when source-location
280 (setf (info :source-location
:variable var
) source-location
))
284 (defun %defparameter
(var val source-location
&optional
(doc nil docp
))
285 (%compiler-defvar var
)
288 (setf (fdocumentation var
'variable
) doc
))
289 (when source-location
290 (setf (info :source-location
:variable var
) source-location
))
293 ;;;; iteration constructs
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
))
302 (mapcar (lambda (var)
303 (or (cond ((symbolp var
) var
)
305 (unless (symbolp (first var
))
306 (error "~S step variable is not a symbol: ~S"
310 (3 (steps (first var
) (third var
))
311 (list (first var
) (second var
))))))
312 (error "~S is an illegal form for a ~S 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
)))
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
)
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
)
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
))
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
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)
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
))
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
)))
397 (let ((,n-list
,(if clist-ok
(list 'quote clist
) list
)))
400 (unless (endp ,n-list
)
401 (let ((,var
,(if clist-ok
402 `(truly-the (member ,@members
) (car ,n-list
))
405 (setq ,n-list
(cdr ,n-list
))
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
)
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
)
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.
453 (dolist (,restart
,restarts
)
454 (push ,condition-form
455 (restart-associated-conditions ,restart
)))
457 (dolist (,restart
,restarts
)
458 (pop (restart-associated-conditions ,restart
)))))))
460 (sb!xc
:defmacro restart-bind
(bindings &body forms
)
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
475 (unless (or name report-function
)
476 (warn "Unnamed restart does not have a report function: ~
478 `(make-restart ',name
,function
479 ,@(and (or report-function
483 ,@(and (or interactive-function
485 `(,interactive-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
*))
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
)))
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
506 (warn 'simple-warning
)
507 (signal 'simple-condition
)
510 `(with-condition-restarts
512 (car *restart-clusters
*)
513 ,(if (eq name
'cerror
)
514 `(cerror ,(second exp
) ,n-cond
)
519 (sb!xc
:defmacro restart-case
(expression &body clauses
&environment env
)
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
537 ;; * MAKE-APPLY-AND-RETURN which generates TAGBODY entries executing
538 ;; the respective BODY.
539 (let ((block-tag (sb!xc
:gensym
"BLOCK"))
541 (labels ((parse-keywords-and-body (keywords-and-body)
542 (do ((form keywords-and-body
(cddr form
))
544 (destructuring-bind (&optional key
(arg nil argp
) &rest rest
)
546 (declare (ignore rest
))
550 ((and (eq key
:report
) argp
)
551 (list :report-function
554 (write-string ,arg stream
))
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
))))
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
))
576 (lambda ,(cond ((null lambda-list
)
578 ((and (null (cdr lambda-list
))
579 (not (member (car lambda-list
)
580 '(&optional
&key
&aux
))))
584 ,@(when lambda-list
`((setq ,temp-var temp
)))
585 (locally (declare (optimize (safety 0)))
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
)
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
)))
602 (let ((,temp-var nil
))
603 (declare (ignorable ,temp-var
))
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
)
615 "(WITH-SIMPLE-RESTART (restart-name format-string format-arguments)
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")))
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
))
626 :report
(lambda (,stream
)
627 (declare (type stream
,stream
))
628 (format ,stream
,format-string
,@format-arguments
))
631 (sb!xc
:defmacro %handler-bind
(bindings form
&environment env
)
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
)
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
)))
665 ;; Regardless of lexical policy, never allow
666 ;; a non-callable into handler-clusters.
668 (declare (optimize (safety 3)))
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
685 (find-or-create-fdefn ',name
) t
))))
686 ;; Resolve to an fdefn at load-time.
688 (cons ,test
(find-or-create-fdefn ',name
))
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
))))
696 `(load-time-value (list ,@(mapcar #'second items
)) t
)
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
)))
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.
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
725 (let ((name (second type
)))
726 (when (typep env
'lexenv
)
727 (dummy-forms `#',name
))
728 `(find-or-create-fdefn ',name
)))
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)))
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
))
747 (let ((name (let ((sb!xc
:*gensym-counter
*
748 (length (cluster-entries))))
749 (sb!xc
:gensym
"H"))))
750 (local-functions `(,name
,@(rest lexpr
)))
754 `(dx-flet ,(local-functions)
756 (dx-let ((*handler-clusters
*
757 (cons ,(const-list (cluster-entries))
758 *handler-clusters
*)))
761 (sb!xc
:defmacro handler-bind
(bindings &body forms
)
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
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
)
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
788 (let ((no-error-clause (assoc ':no-error cases
)))
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
)
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
))))
806 (with-unique-names (block cell form-fun
)
807 `(dx-flet ((,form-fun
()
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)))
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
820 (dx-let ((,cell
(cons :condition nil
)))
821 (declare (ignorable ,cell
))
824 ,(mapcar (lambda (annotated-case)
825 (destructuring-bind (tag type ll fun-name
) annotated-case
826 (declare (ignore fun-name
))
830 `(setf (cdr ,cell
) temp
)
831 '(declare (ignore temp
)))
834 (return-from ,block
(,form-fun
)))
836 (lambda (annotated-case)
837 (destructuring-bind (tag type ll fun-name
) annotated-case
838 (declare (ignore type
))
842 `(,fun-name
(cdr ,cell
))
844 annotated-cases
))))))))))
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
))
855 (sb!xc
:defmacro named-lambda
(&whole whole name args
&body body
)
856 (declare (ignore name args body
))
859 (sb!xc
:defmacro lambda-with-lexenv
(&whole whole
860 declarations macros symbol-macros
862 (declare (ignore declarations macros symbol-macros body
))