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 (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)
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
))
44 (let ((ignore (sb!xc
:gensym
)))
45 `(multiple-value-call #'(lambda (&optional
,@(mapcar #'list vars
)
47 (declare (ignore ,ignore
))
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:
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
)
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
))
89 ;; THE to preserve non-toplevelness for FOO in
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
))
98 ,(when more
`(cond ,@more
))))))))))
100 (defmacro-mundanely when
(test &body forms
)
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
)
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
)
115 ;; Preserve non-toplevelness of the form!
116 `(the t
,(first forms
)))
122 (defmacro-mundanely or
(&rest forms
)
123 (cond ((endp forms
) nil
)
125 ;; Preserve non-toplevelness of the form!
126 `(the t
,(first forms
)))
128 (let ((n-result (gensym)))
129 `(let ((,n-result
,(first forms
)))
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
)
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
))
154 (defmacro-mundanely 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 (defmacro-mundanely defun
(&environment env name args
&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
)
186 (let* (;; stuff shared between LAMBDA and INLINE-LAMBDA and NAMED-LAMBDA
188 ,@(when doc
(list doc
))
190 (block ,(fun-name-block-name name
)
192 (lambda `(lambda ,@lambda-guts
))
193 (named-lambda `(named-lambda ,name
,@lambda-guts
))
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
)
200 #-sb-xc-host sb
!c
:maybe-compiler-notify
201 "lexical environment too hairy, can't inline DEFUN ~S"
205 (eval-when (:compile-toplevel
)
206 (sb!c
:%compiler-defun
',name
',inline-lambda t
))
207 ,@(when (typep name
'(cons (eql setf
)))
208 `((eval-when (:compile-toplevel
:execute
)
209 (sb!c
::warn-if-setf-macro
',name
))))
210 (%defun
',name
,named-lambda
(sb!c
:source-location
)
212 `(',inline-lambda
)))))))
215 (progn (defun %defun
(name def source-location
&optional inline-lambda
)
216 (declare (type function def
))
217 ;; should've been checked by DEFMACRO DEFUN
218 (aver (legal-fun-name-p name
))
219 (sb!c
:%compiler-defun
name inline-lambda nil
)
221 (warn 'redefinition-with-defun
222 :name name
:new-function def
:new-location source-location
))
223 (setf (sb!xc
:fdefinition name
) def
)
224 ;; %COMPILER-DEFUN doesn't do this except at compile-time, when it
225 ;; also checks package locks. By doing this here we let (SETF
226 ;; FDEFINITION) do the load-time package lock checking before
227 ;; we frob any existing inline expansions.
228 (sb!c
::%set-inline-expansion name nil inline-lambda
)
229 (sb!c
::note-name-defined name
:function
)
231 ;; During cold-init we don't touch the fdefinition.
232 (defun !%quietly-defun
(name inline-lambda
)
233 (sb!c
:%compiler-defun
name nil nil
) ; makes :WHERE-FROM = :DEFINED
234 (sb!c
::%set-inline-expansion name nil inline-lambda
)
235 ;; and no need to call NOTE-NAME-DEFINED. It would do nothing.
238 ;;;; DEFVAR and DEFPARAMETER
240 (defmacro-mundanely defvar
(var &optional
(val nil valp
) (doc nil docp
))
242 "Define a special variable at top level. Declare the variable
243 SPECIAL and, optionally, initialize it. If the variable already has a
244 value, the old value is not clobbered. The third argument is an optional
245 documentation string for the variable."
247 (eval-when (:compile-toplevel
)
248 (%compiler-defvar
',var
))
250 (sb!c
:source-location
)
252 `((unless (boundp ',var
) ,val
)))
256 (defmacro-mundanely defparameter
(var val
&optional
(doc nil docp
))
258 "Define a parameter that is not normally changed by the program,
259 but that may be changed without causing an error. Declare the
260 variable special and sets its value to VAL, overwriting any
261 previous value. The third argument is an optional documentation
262 string for the parameter."
264 (eval-when (:compile-toplevel
)
265 (%compiler-defvar
',var
))
266 (%defparameter
',var
,val
(sb!c
:source-location
)
270 (defun %compiler-defvar
(var)
271 (sb!xc
:proclaim
`(special ,var
)))
274 (defun %defvar
(var source-location
&optional
(val nil valp
) (doc nil docp
))
275 (%compiler-defvar var
)
280 (setf (fdocumentation var
'variable
) doc
))
281 (sb!c
:with-source-location
(source-location)
282 (setf (info :source-location
:variable var
) source-location
))
286 (defun %defparameter
(var val source-location
&optional
(doc nil docp
))
287 (%compiler-defvar var
)
290 (setf (fdocumentation var
'variable
) doc
))
291 (sb!c
:with-source-location
(source-location)
292 (setf (info :source-location
:variable var
) source-location
))
295 ;;;; iteration constructs
297 ;;; (These macros are defined in terms of a function FROB-DO-BODY which
298 ;;; is also used by SB!INT:DO-ANONYMOUS. Since these macros should not
299 ;;; be loaded on the cross-compilation host, but SB!INT:DO-ANONYMOUS
300 ;;; and FROB-DO-BODY should be, these macros can't conveniently be in
301 ;;; the same file as FROB-DO-BODY.)
302 (defmacro-mundanely do
(varlist endlist
&body body
)
304 "DO ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
305 Iteration construct. Each Var is initialized in parallel to the value of the
306 specified Init form. On subsequent iterations, the Vars are assigned the
307 value of the Step form (if any) in parallel. The Test is evaluated before
308 each evaluation of the body Forms. When the Test is true, the Exit-Forms
309 are evaluated as a PROGN, with the result being the value of the DO. A block
310 named NIL is established around the entire expansion, allowing RETURN to be
311 used as an alternate exit mechanism."
312 (frob-do-body varlist endlist body
'let
'psetq
'do nil
))
313 (defmacro-mundanely do
* (varlist endlist
&body body
)
315 "DO* ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
316 Iteration construct. Each Var is initialized sequentially (like LET*) to the
317 value of the specified Init form. On subsequent iterations, the Vars are
318 sequentially assigned the value of the Step form (if any). The Test is
319 evaluated before each evaluation of the body Forms. When the Test is true,
320 the Exit-Forms are evaluated as a PROGN, with the result being the value
321 of the DO. A block named NIL is established around the entire expansion,
322 allowing RETURN to be used as an alternate exit mechanism."
323 (frob-do-body varlist endlist body
'let
* 'setq
'do
* nil
))
325 ;;; DOTIMES and DOLIST could be defined more concisely using
326 ;;; destructuring macro lambda lists or DESTRUCTURING-BIND, but then
327 ;;; it'd be tricky to use them before those things were defined.
328 ;;; They're used enough times before destructuring mechanisms are
329 ;;; defined that it looks as though it's worth just implementing them
330 ;;; ASAP, at the cost of being unable to use the standard
331 ;;; destructuring mechanisms.
332 (defmacro-mundanely dotimes
((var count
&optional
(result nil
)) &body body
)
333 (cond ((integerp count
)
334 `(do ((,var
0 (1+ ,var
)))
335 ((>= ,var
,count
) ,result
)
336 (declare (type unsigned-byte
,var
))
339 (let ((c (gensym "COUNT")))
340 `(do ((,var
0 (1+ ,var
))
342 ((>= ,var
,c
) ,result
)
343 (declare (type unsigned-byte
,var
)
347 (defmacro-mundanely dolist
((var list
&optional
(result nil
)) &body body
&environment env
)
348 ;; We repeatedly bind the var instead of setting it so that we never
349 ;; have to give the var an arbitrary value such as NIL (which might
350 ;; conflict with a declaration). If there is a result form, we
351 ;; introduce a gratuitous binding of the variable to NIL without the
352 ;; declarations, then evaluate the result form in that
353 ;; environment. We spuriously reference the gratuitous variable,
354 ;; since we don't want to use IGNORABLE on what might be a special
356 (multiple-value-bind (forms decls
) (parse-body body
:doc-string-allowed nil
)
357 (let* ((n-list (gensym "N-LIST"))
358 (start (gensym "START")))
359 (multiple-value-bind (clist members clist-ok
)
360 (cond ((sb!xc
:constantp list env
)
361 (let ((value (constant-form-value list env
)))
362 (multiple-value-bind (all dot
) (list-members value
:max-length
20)
364 ;; Full warning is too much: the user may terminate the loop
365 ;; early enough. Contents are still right, though.
366 (style-warn "Dotted list ~S in DOLIST." value
))
368 (values value nil nil
)
369 (values value all t
)))))
370 ((and (consp list
) (eq 'list
(car list
))
371 (every (lambda (arg) (sb!xc
:constantp arg env
)) (cdr list
)))
372 (let ((values (mapcar (lambda (arg) (constant-form-value arg env
)) (cdr list
))))
373 (values values values t
)))
375 (values nil nil nil
)))
377 (let ((,n-list
,(if clist-ok
(list 'quote clist
) list
)))
380 (unless (endp ,n-list
)
381 (let ((,var
,(if clist-ok
382 `(truly-the (member ,@members
) (car ,n-list
))
385 (setq ,n-list
(cdr ,n-list
))
390 ;; Filter out TYPE declarations (VAR gets bound to NIL,
391 ;; and might have a conflicting type declaration) and
392 ;; IGNORE (VAR might be ignored in the loop body, but
393 ;; it's used in the result form).
394 ,@(filter-dolist-declarations decls
)
399 ;;;; conditions, handlers, restarts
401 ;;; KLUDGE: we PROCLAIM these special here so that we can use restart
402 ;;; macros in the compiler before the DEFVARs are compiled.
404 ;;; For an explanation of these data structures, see DEFVARs in
405 ;;; target-error.lisp.
406 (sb!xc
:proclaim
'(special *handler-clusters
* *restart-clusters
*))
408 ;;; Generated code need not check for unbound-marker in *HANDLER-CLUSTERS*
409 ;;; (resp *RESTART-). To elicit this we must poke at the info db.
410 ;;; SB!XC:PROCLAIM SPECIAL doesn't advise the host Lisp that *HANDLER-CLUSTERS*
411 ;;; is special and so it rightfully complains about a SETQ of the variable.
412 ;;; But I must SETQ if proclaming ALWAYS-BOUND because the xc asks the host
413 ;;; whether it's currently bound.
414 ;;; But the DEFVARs are in target-error. So it's one hack or another.
415 (setf (info :variable
:always-bound
'*handler-clusters
*)
416 #+sb-xc
:always-bound
#-sb-xc
:eventually
)
417 (setf (info :variable
:always-bound
'*restart-clusters
*)
418 #+sb-xc
:always-bound
#-sb-xc
:eventually
)
420 (defmacro-mundanely with-condition-restarts
421 (condition-form restarts-form
&body body
)
423 "Evaluates the BODY in a dynamic environment where the restarts in the list
424 RESTARTS-FORM are associated with the condition returned by CONDITION-FORM.
425 This allows FIND-RESTART, etc., to recognize restarts that are not related
426 to the error currently being debugged. See also RESTART-CASE."
427 (once-only ((condition-form condition-form
)
428 (restarts restarts-form
))
429 (with-unique-names (restart)
430 ;; FIXME: check the need for interrupt-safety.
433 (dolist (,restart
,restarts
)
434 (push ,condition-form
435 (restart-associated-conditions ,restart
)))
437 (dolist (,restart
,restarts
)
438 (pop (restart-associated-conditions ,restart
)))))))
440 (defmacro-mundanely restart-bind
(bindings &body forms
)
442 "(RESTART-BIND ({(case-name function {keyword value}*)}*) forms)
443 Executes forms in a dynamic context where the given bindings are in
444 effect. Users probably want to use RESTART-CASE. A case-name of NIL
445 indicates an anonymous restart. When bindings contain the same
446 restart name, FIND-RESTART will find the first such binding."
447 (flet ((parse-binding (binding)
448 (unless (>= (length binding
) 2)
449 (error "ill-formed restart binding: ~S" binding
))
450 (destructuring-bind (name function
451 &key interactive-function
455 (unless (or name report-function
)
456 (warn "Unnamed restart does not have a report function: ~
458 `(make-restart ',name
,function
459 ,@(and (or report-function
463 ,@(and (or interactive-function
465 `(,interactive-function
))
467 `(,test-function
))))))
468 `(let ((*restart-clusters
*
469 (cons (list ,@(mapcar #'parse-binding bindings
))
470 *restart-clusters
*)))
471 (declare (truly-dynamic-extent *restart-clusters
*))
474 ;;; Wrap the RESTART-CASE expression in a WITH-CONDITION-RESTARTS if
475 ;;; appropriate. Gross, but it's what the book seems to say...
476 (defun munge-restart-case-expression (expression env
)
477 (let ((exp (%macroexpand expression env
)))
479 (let* ((name (car exp
))
480 (args (if (eq name
'cerror
) (cddr exp
) (cdr exp
))))
481 (if (member name
'(signal error cerror warn
))
482 (once-only ((n-cond `(coerce-to-condition
486 (warn 'simple-warning
)
487 (signal 'simple-condition
)
490 `(with-condition-restarts
492 (car *restart-clusters
*)
493 ,(if (eq name
'cerror
)
494 `(cerror ,(second exp
) ,n-cond
)
499 (defmacro-mundanely restart-case
(expression &body clauses
&environment env
)
501 "(RESTART-CASE form {(case-name arg-list {keyword value}* body)}*)
502 The form is evaluated in a dynamic context where the clauses have
503 special meanings as points to which control may be transferred (see
504 INVOKE-RESTART). When clauses contain the same case-name,
505 FIND-RESTART will find the first such clause. If form is a call to
506 SIGNAL, ERROR, CERROR or WARN (or macroexpands into such) then the
507 signalled condition will be associated with the new restarts."
508 ;; PARSE-CLAUSE (which uses PARSE-KEYWORDS-AND-BODY) is used to
509 ;; parse all clauses into lists of the form
511 ;; (NAME TAG KEYWORDS LAMBDA-LIST BODY)
513 ;; where KEYWORDS are suitable keywords for use in HANDLER-BIND
514 ;; bindings. These lists are then passed to
515 ;; * MAKE-BINDING which generates bindings for the respective NAME
517 ;; * MAKE-APPLY-AND-RETURN which generates TAGBODY entries executing
518 ;; the respective BODY.
519 (let ((block-tag (sb!xc
:gensym
"BLOCK"))
521 (labels ((parse-keywords-and-body (keywords-and-body)
522 (do ((form keywords-and-body
(cddr form
))
524 (destructuring-bind (&optional key
(arg nil argp
) &rest rest
)
526 (declare (ignore rest
))
530 ((and (eq key
:report
) argp
)
531 (list :report-function
534 (write-string ,arg stream
))
536 ((and (eq key
:interactive
) argp
)
537 (list :interactive-function
`#',arg
))
538 ((and (eq key
:test
) argp
)
539 (list :test-function
`#',arg
))
541 (return (values result form
))))
543 (parse-clause (clause)
544 (unless (and (listp clause
) (>= (length clause
) 2)
545 (listp (second clause
)))
546 (error "ill-formed ~S clause, no lambda-list:~% ~S"
547 'restart-case clause
))
548 (destructuring-bind (name lambda-list
&body body
) clause
549 (multiple-value-bind (keywords body
)
550 (parse-keywords-and-body body
)
551 (list name
(sb!xc
:gensym
"TAG") keywords lambda-list body
))))
552 (make-binding (clause-data)
553 (destructuring-bind (name tag keywords lambda-list body
) clause-data
554 (declare (ignore body
))
556 (lambda ,(cond ((null lambda-list
)
558 ((and (null (cdr lambda-list
))
559 (not (member (car lambda-list
)
560 '(&optional
&key
&aux
))))
564 ,@(when lambda-list
`((setq ,temp-var temp
)))
565 (locally (declare (optimize (safety 0)))
568 (make-apply-and-return (clause-data)
569 (destructuring-bind (name tag keywords lambda-list body
) clause-data
570 (declare (ignore name keywords
))
571 `(,tag
(return-from ,block-tag
572 ,(cond ((null lambda-list
)
574 ((and (null (cdr lambda-list
))
575 (not (member (car lambda-list
)
576 '(&optional
&key
&aux
))))
577 `(funcall (lambda ,lambda-list
,@body
) ,temp-var
))
579 `(apply (lambda ,lambda-list
,@body
) ,temp-var
))))))))
580 (let ((clauses-data (mapcar #'parse-clause clauses
)))
582 (let ((,temp-var nil
))
583 (declare (ignorable ,temp-var
))
586 ,(mapcar #'make-binding clauses-data
)
587 (return-from ,block-tag
588 ,(munge-restart-case-expression expression env
)))
589 ,@(mapcan #'make-apply-and-return clauses-data
))))))))
591 (defmacro-mundanely with-simple-restart
((restart-name format-string
592 &rest format-arguments
)
595 "(WITH-SIMPLE-RESTART (restart-name format-string format-arguments)
597 If restart-name is not invoked, then all values returned by forms are
598 returned. If control is transferred to this restart, it immediately
599 returns the values NIL and T."
600 (let ((stream (sb!xc
:gensym
"STREAM")))
602 ;; If there's just one body form, then don't use PROGN. This allows
603 ;; RESTART-CASE to "see" calls to ERROR, etc.
604 ,(if (= (length forms
) 1) (car forms
) `(progn ,@forms
))
606 :report
(lambda (,stream
)
607 (declare (type stream
,stream
))
608 (format ,stream
,format-string
,@format-arguments
))
611 (defmacro-mundanely %handler-bind
(bindings form
&environment env
)
613 (return-from %handler-bind form
))
614 ;; As an optimization, this looks at the handler parts of BINDINGS
615 ;; and turns handlers of the forms (lambda ...) and (function
616 ;; (lambda ...)) into local, dynamic-extent functions.
618 ;; Type specifiers in BINDINGS which name classoids are parsed
619 ;; into the classoid, otherwise are translated local TYPEP wrappers.
621 ;; As a further optimization, it is possible to eliminate some runtime
622 ;; consing (which is a speed win if not a space win, since it's dx already)
623 ;; in special cases such as (HANDLER-BIND ((WARNING #'MUFFLE-WARNING)) ...).
624 ;; If all bindings are optimizable, then the runtime cost of making them
625 ;; is one dx cons cell for the whole cluster.
626 ;; Otherwise it takes 1+2N cons cells where N is the number of bindings.
628 (collect ((local-functions) (cluster-entries) (dummy-forms))
629 (flet ((const-cons (test handler
)
630 ;; If possible, render HANDLER as a load-time constant so that
631 ;; consing the test and handler is also load-time constant.
632 (let ((name (when (typep handler
633 '(cons (member function quote
)
636 (cond ((or (not name
)
637 (assq name
(local-functions))
638 (and (eq (car handler
) 'function
)
639 (sb!c
::fun-locally-defined-p name env
)))
640 `(cons ,(case (car test
)
641 ((named-lambda function
) test
)
642 (t `(load-time-value ,test t
)))
643 ,(if (typep handler
'(cons (eql function
)))
645 ;; Regardless of lexical policy, never allow
646 ;; a non-callable into handler-clusters.
648 (declare (optimize (safety 3)))
650 ((info :function
:info name
) ; known
651 ;; This takes care of CONTINUE,ABORT,MUFFLE-WARNING.
652 ;; #' will be evaluated in the null environment.
653 `(load-time-value (cons ,test
#',name
) t
))
655 ;; For each handler specified as #'F we must verify
656 ;; that F is fboundp upon entering the binding scope.
657 ;; Referencing #'F is enough to ensure a warning if the
658 ;; function isn't defined at compile-time, but the
659 ;; compiler considers it elidable unless something forces
660 ;; an apparent use of the form at runtime,
661 ;; so instead use SAFE-FDEFN-FUN on the fdefn.
662 (when (eq (car handler
) 'function
)
663 (dummy-forms `(sb!c
:safe-fdefn-fun
665 (find-or-create-fdefn ',name
) t
))))
666 ;; Resolve to an fdefn at load-time.
668 (cons ,test
(find-or-create-fdefn ',name
))
672 ;; If the resultant list is (LIST (L-T-V ...) (L-T-V ...) ...)
673 ;; then pull the L-T-V outside.
674 (if (every (lambda (x) (typep x
'(cons (eql load-time-value
))))
676 `(load-time-value (list ,@(mapcar #'second items
)) t
)
679 (dolist (binding bindings
)
680 (unless (proper-list-of-length-p binding
2)
681 (error "ill-formed handler binding: ~S" binding
))
682 (destructuring-bind (type handler
) binding
683 (setq type
(typexpand type env
))
684 ;; Simplify a singleton AND or OR.
685 (when (typep type
'(cons (member and or
) (cons t null
)))
686 (setf type
(second type
)))
689 ;; Compute the test expression
690 (cond ((member type
'(t condition
))
691 ;; Every signal is necesarily a CONDITION, so whether you
692 ;; wrote T or CONDITION, this is always an eligible handler.
694 ((typep type
'(cons (eql satisfies
) (cons t null
)))
695 ;; (SATISFIES F) => #'F but never a local definition of F.
696 ;; The predicate is used only if needed - it's not an error
697 ;; if not fboundp (though dangerously stupid) - so just
698 ;; reference #'F for the compiler to see the use of the name.
699 (let ((name (second type
)))
700 (dummy-forms `#',name
)
701 `(find-or-create-fdefn ',name
)))
703 (condition-classoid-p (find-classoid type nil
)))
704 ;; It's debatable whether we need to go through a
705 ;; classoid-cell instead of just using load-time-value
706 ;; on FIND-CLASS, but the extra indirection is
707 ;; safer, and no slower than what TYPEP does.
708 `(find-classoid-cell ',type
:create t
))
709 (t ; No runtime consing here- this is not a closure.
710 `(named-lambda (%handler-bind
,type
) (c)
711 (declare (optimize (sb!c
::verify-arg-count
0)))
713 ;; Compute the handler expression
714 (let ((lexpr (typecase handler
715 ((cons (eql lambda
)) handler
)
716 ((cons (eql function
)
717 (cons (cons (eql lambda
)) null
))
720 (let ((name (let ((sb!xc
:*gensym-counter
*
721 (length (cluster-entries))))
722 (sb!xc
:gensym
"H"))))
723 (local-functions `(,name
,@(rest lexpr
)))
727 `(dx-flet ,(local-functions)
729 (dx-let ((*handler-clusters
*
730 (cons ,(const-list (cluster-entries))
731 *handler-clusters
*)))
734 (defmacro-mundanely handler-bind
(bindings &body forms
)
736 "(HANDLER-BIND ( {(type handler)}* ) body)
738 Executes body in a dynamic context where the given handler bindings are in
739 effect. Each handler must take the condition being signalled as an argument.
740 The bindings are searched first to last in the event of a signalled
742 ;; Bindings which meet specific criteria can be established with
743 ;; slightly less runtime overhead than in general.
744 ;; To allow the optimization, TYPE must be either be (SATISFIES P)
745 ;; or a symbol naming a condition class at compile time,
746 ;; and HANDLER must be a global function specified as either 'F or #'F.
747 `(%handler-bind
,bindings
748 #!-x86
(progn ,@forms
)
749 ;; Need to catch FP errors here!
750 #!+x86
(multiple-value-prog1 (progn ,@forms
) (float-wait))))
752 (defmacro-mundanely handler-case
(form &rest cases
)
754 "(HANDLER-CASE form { (type ([var]) body) }* )
756 Execute FORM in a context with handlers established for the condition types. A
757 peculiar property allows type to be :NO-ERROR. If such a clause occurs, and
758 form returns normally, all its values are passed to this clause as if by
759 MULTIPLE-VALUE-CALL. The :NO-ERROR clause accepts more than one var
761 (let ((no-error-clause (assoc ':no-error cases
)))
763 (let ((normal-return (make-symbol "normal-return"))
764 (error-return (make-symbol "error-return")))
765 `(block ,error-return
766 (multiple-value-call (lambda ,@(cdr no-error-clause
))
767 (block ,normal-return
768 (return-from ,error-return
769 (handler-case (return-from ,normal-return
,form
)
770 ,@(remove no-error-clause cases
)))))))
771 (let* ((local-funs nil
)
773 (mapcar (lambda (case)
774 (with-unique-names (tag fun
)
775 (destructuring-bind (type ll
&body body
) case
776 (push `(,fun
,ll
,@body
) local-funs
)
777 (list tag type ll fun
))))
779 (with-unique-names (block cell form-fun
)
780 `(dx-flet ((,form-fun
()
782 ;; Need to catch FP errors here!
783 #!+x86
(multiple-value-prog1 ,form
(float-wait)))
784 ,@(reverse local-funs
))
785 (declare (optimize (sb!c
::check-tag-existence
0)))
787 ;; KLUDGE: We use a dx CONS cell instead of just assigning to
788 ;; the variable directly, so that we can stack allocate
789 ;; robustly: dx value cells don't work quite right, and it is
790 ;; possible to construct user code that should loop
791 ;; indefinitely, but instead eats up some stack each time
793 (dx-let ((,cell
(cons :condition nil
)))
794 (declare (ignorable ,cell
))
797 ,(mapcar (lambda (annotated-case)
798 (destructuring-bind (tag type ll fun-name
) annotated-case
799 (declare (ignore fun-name
))
803 `(setf (cdr ,cell
) temp
)
804 '(declare (ignore temp
)))
807 (return-from ,block
(,form-fun
)))
809 (lambda (annotated-case)
810 (destructuring-bind (tag type ll fun-name
) annotated-case
811 (declare (ignore type
))
815 `(,fun-name
(cdr ,cell
))
817 annotated-cases
))))))))))
821 (defmacro-mundanely return
(&optional
(value nil
))
822 `(return-from nil
,value
))
824 (defmacro-mundanely lambda
(&whole whole args
&body body
)
825 (declare (ignore args body
))
828 (defmacro-mundanely named-lambda
(&whole whole name args
&body body
)
829 (declare (ignore name args body
))
832 (defmacro-mundanely lambda-with-lexenv
(&whole whole
833 declarations macros symbol-macros
835 (declare (ignore declarations macros symbol-macros body
))