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