/
target-error.lisp
2540 lines (2281 loc) · 110 KB
/
target-error.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;;; that part of the condition system which can or should come early
;;;; (mostly macro-related)
;;;; This software is part of the SBCL system. See the README file for
;;;; more information.
;;;;
;;;; This software is derived from the CMU CL system, which was
;;;; written at Carnegie Mellon University and released into the
;;;; public domain. The software is in the public domain and is
;;;; provided with absolutely no warranty. See the COPYING and CREDITS
;;;; files for more information.
(in-package "SB-KERNEL")
(declaim (global *type-system-initialized*))
(defun decode-internal-error-args (sap trap-number &optional error-number)
(let ((error-number (cond (error-number)
((>= trap-number sb-vm:error-trap)
(prog1
(- trap-number sb-vm:error-trap)
(setf trap-number sb-vm:error-trap)))
(t
(prog1 (sap-ref-8 sap 0)
(setf sap (sap+ sap 1)))))))
(let ((length (error-length error-number)))
(declare (type (unsigned-byte 8) length))
(values error-number
(loop with index = 0
repeat length
collect (sb-c:sap-read-var-integerf sap index))
trap-number))))
(defun muffle-warning-p (warning)
(declare (special *muffled-warnings*))
(typep warning *muffled-warnings*))
;;; Each cluster is an alist of the form
;;;
;;; ((TYPE-TEST1 . HANDLER1) (TYPE-TEST2 . HANDLER2) ...)
;;;
;;; where TYPE-TESTN are functions of one argument which test a given
;;; condition instance for the type required by the corresponding
;;; HANDLERN. HANDLERN are function designators.
;;;
;;; Newly established handlers are added at the beginning of the
;;; list. Elements to the left of the alist take precedence over
;;; elements to the right.
;;;
;;; Lists to which *HANDLER-CLUSTERS* is bound generally have dynamic
;;; extent.
(defmethod print-object ((restart restart) stream)
(if *print-escape*
(print-unreadable-object (restart stream :type t :identity t)
(prin1 (restart-name restart) stream))
(restart-report restart stream)))
(setf (documentation 'restart-name 'function)
"Return the name of the given restart object.")
(defun restart-report (restart stream)
(if (restart-report-function restart)
(funcall (truly-the function (restart-report-function restart))
stream)
(prin1 (or (restart-name restart)
restart)
stream)))
(defvar *restart-test-stack* nil)
;; Call FUNCTION with all restarts in the current dynamic environment,
;; 1) that are associated to CONDITION (when CONDITION is NIL, all
;; restarts are processed)
;; 2) and for which the restart test returns non-NIL for CONDITION.
;; When CALL-TEST-P is non-NIL, all restarts are processed.
(defun map-restarts (function &optional condition (call-test-p t))
(declare (function function))
(let ((stack *restart-test-stack*))
(dolist (restart-cluster *restart-clusters*)
(dolist (restart restart-cluster)
(when (and (or (not condition)
(null (restart-associated-conditions restart))
(memq condition (restart-associated-conditions restart)))
;; A call to COMPUTE-RESTARTS -- from an error,
;; from user code, whatever -- inside the test
;; function would cause infinite recursion here, so
;; we disable each restart using
;; *restart-test-stack* for the duration of the
;; test call.
(not (memq restart stack))
(or (not call-test-p)
(let ((*restart-test-stack* (cons restart stack)))
(declare (dynamic-extent *restart-test-stack*))
(funcall (restart-test-function restart) condition))))
(funcall function restart))))))
(defun compute-restarts (&optional condition)
"Return a list of all the currently active restarts ordered from most recently
established to less recently established. If CONDITION is specified, then only
restarts associated with CONDITION (or with no condition) will be returned."
(collect ((result))
(map-restarts (lambda (restart) (result restart)) condition)
(result)))
(defun %find-restart (identifier condition &optional (call-test-p t))
(flet ((eq-restart-p (restart)
(when (eq identifier restart)
(return-from %find-restart restart)))
(named-restart-p (restart)
(when (eq identifier (restart-name restart))
(return-from %find-restart restart))))
;; KLUDGE: can the compiler infer this dx automatically?
(declare (dynamic-extent #'eq-restart-p #'named-restart-p))
(if (typep identifier 'restart)
;; The code under #+previous-... below breaks the abstraction
;; introduced by MAP-RESTARTS, but is about twice as
;; fast as #+equivalent-... . Also, it is a common case due to
;;
;; (INVOKE-RESTART RESTART)
;; -> (FIND-RESTART-OR-CONTROL-ERROR RESTART)
;; -> (FIND-RESTART RESTART)
;;
;; However, both #+previous-... and #+equivalent-... may be
;; wrong altogether because of
;; https://bugs.launchpad.net/sbcl/+bug/774410:
;; The behavior expected in that report can be achieved by the
;; following line (which is, of course, the slowest of all
;; possibilities):
(map-restarts #'eq-restart-p condition call-test-p)
#+equivalent-to-previous-sbcl-behavior--faster-but-see-bug-774410
(map-restarts #'eq-restart-p nil nil)
#+previous-behavior--fastest-but-see-bug-774410
(and (find-if (lambda (cluster) (find identifier cluster)) *restart-clusters*)
identifier)
(map-restarts #'named-restart-p condition call-test-p))))
(defun find-restart (identifier &optional condition)
"Return the first restart identified by IDENTIFIER. If IDENTIFIER is a symbol,
then the innermost applicable restart with that name is returned. If IDENTIFIER
is a restart, it is returned if it is currently active. Otherwise NIL is
returned. If CONDITION is specified and not NIL, then only restarts associated
with that condition (or with no condition) will be returned."
;; Calls MAP-RESTARTS such that restart test functions are
;; respected.
(%find-restart identifier condition))
;;; helper for the various functions which are ANSI-spec'ed to do
;;; something with a restart or signal CONTROL-ERROR if there is none
(define-error-wrapper find-restart-or-control-error (identifier &optional condition (call-test-p t))
(or (%find-restart identifier condition call-test-p)
(error 'simple-control-error
:format-control "No restart ~S is active~@[ for ~S~]."
:format-arguments (list identifier condition))))
(defun invoke-restart (restart &rest values)
"Calls the function associated with the given restart, passing any given
arguments. If the argument restart is not a restart or a currently active
non-nil restart name, then a CONTROL-ERROR is signalled."
(/show "entering INVOKE-RESTART" restart)
;; The following code calls MAP-RESTARTS (through
;; FIND-RESTART-OR-CONTROL-ERROR -> %FIND-RESTART) such that restart
;; test functions are respected when RESTART is a symbol, but not
;; when RESTART is a RESTART instance.
;;
;; Without disabling test functions for the RESTART instance case,
;; the following problem would arise:
;;
;; (restart-case
;; (handler-bind
;; ((some-condition (lambda (c)
;; (invoke-restart (find-restart 'foo c)) ; a)
;; (invoke-restart 'foo) ; b)
;; )))
;; (signal 'some-condition))
;; (foo ()
;; :test (lambda (c) (typep c 'some-condition))))
;;
;; In case a), INVOKE-RESTART receives the RESTART instance, but
;; cannot supply the condition instance needed by the test. In case
;; b) INVOKE-RESTART calls FIND-RESTART, but again cannot supply the
;; condition instance. As a result, the restart would be impossible
;; the invoke.
(let ((real-restart (find-restart-or-control-error
restart nil (symbolp restart))))
(apply (restart-function real-restart) values)))
(defun interactive-restart-arguments (real-restart)
(let ((interactive-function (restart-interactive-function real-restart)))
(if interactive-function
(funcall interactive-function)
'())))
(defun invoke-restart-interactively (restart)
"Calls the function associated with the given restart, prompting for any
necessary arguments. If the argument restart is not a restart or a
currently active non-NIL restart name, then a CONTROL-ERROR is signalled."
;; For an explanation of the call to FIND-RESTART-OR-CONTROL-ERROR,
;; see comment in INVOKE-RESTART.
(let* ((real-restart (find-restart-or-control-error
restart nil (symbolp restart)))
(args (interactive-restart-arguments real-restart)))
(apply (restart-function real-restart) args)))
;;; To reduce expansion size of RESTART-CASE
(defun with-simple-condition-restarts (function cerror-arg datum &rest arguments)
(let ((sb-debug:*stack-top-hint* (or sb-debug:*stack-top-hint*
'with-simple-condition-restarts))
(condition (apply #'coerce-to-condition datum
(case function
(warn 'simple-warning)
(signal 'simple-condition)
(t 'simple-error))
function
arguments)))
(with-condition-restarts condition (car *restart-clusters*)
(if (eq function 'cerror)
(cerror cerror-arg condition)
(funcall function condition)))))
;;;; Conditions.
(!defstruct-with-alternate-metaclass condition
:slot-names (assigned-slots)
:constructor nil
:superclass-name t
:metaclass-name condition-classoid
:metaclass-constructor make-condition-classoid
:dd-type structure)
;;; Needed for !CALL-A-METHOD to pick out CONDITIONs
(defun !condition-p (x) (typep x 'condition))
(defstruct (condition-slot (:copier nil))
(name (missing-arg) :type symbol)
;; list of all applicable initargs
(initargs (missing-arg) :type list)
;; names of reader and writer functions
(readers (missing-arg) :type list)
(writers (missing-arg) :type list)
;; true if :INITFORM was specified
(initform-p (missing-arg) :type (member t nil))
;; the initform if :INITFORM was specified, otherwise NIL
(initform nil :type t)
;; if this is a function, call it with no args to get the initform value
(initfunction (missing-arg) :type t)
;; allocation of this slot, or NIL until defaulted
(allocation nil :type (member :instance :class nil))
;; If ALLOCATION is :CLASS, this is a cons whose car holds the value
(cell nil :type (or cons null))
;; slot documentation
(documentation nil :type (or string null)))
(eval-when (:compile-toplevel :load-toplevel :execute)
(let ((condition-class (find-classoid 'condition)))
(setf (condition-classoid-cpl condition-class)
(list condition-class))))
(setf (condition-classoid-report (find-classoid 'condition))
(lambda (cond stream)
(format stream "Condition ~/sb-impl:print-type-specifier/ was signalled."
(type-of cond))))
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun find-condition-layout (name parent-types)
(let* ((cpl (remove-duplicates
(reverse
(reduce #'append
(mapcar (lambda (x)
(condition-classoid-cpl
(find-classoid x)))
parent-types)))))
(cond-layout (info :type :compiler-layout 'condition))
(olayout (info :type :compiler-layout name))
;; FIXME: Does this do the right thing in case of multiple
;; inheritance? A quick look at DEFINE-CONDITION didn't make
;; it obvious what ANSI intends to be done in the case of
;; multiple inheritance, so it's not actually clear what the
;; right thing is..
(new-inherits
(order-layout-inherits (concatenate 'simple-vector
(layout-inherits cond-layout)
(mapcar #'classoid-layout cpl)))))
(if (and olayout
(not (mismatch (layout-inherits olayout) new-inherits)))
olayout
;; All condition classoid layouts carry the same LAYOUT-INFO - the defstruct
;; description for CONDITION - which is a representation of the primitive object
;; and not the lisp-level object.
(make-layout (hash-layout-name name)
(make-undefined-classoid name)
:info (layout-info cond-layout)
:flags (logior +condition-layout-flag+ +strictly-boxed-flag+)
:inherits new-inherits
:depthoid -1
:length (layout-length cond-layout)))))
) ; EVAL-WHEN
;;;; slots of CONDITION objects
(defun find-slot-default (condition classoid slot &optional boundp)
(multiple-value-bind (value found) (find-slot-default-initarg classoid slot)
;; When CLASSOID or a superclass has a default initarg for SLOT, use
;; that.
(cond (found
value)
;; Otherwise use the initform of SLOT, if there is one.
((condition-slot-initform-p slot)
(let ((initfun (condition-slot-initfunction slot)))
(aver (functionp initfun))
(funcall initfun)))
;; if we're computing SLOT-BOUNDP, return an unbound marker
(boundp sb-pcl:+slot-unbound+)
;; if we're computing SLOT-VALUE, call SLOT-UNBOUND
(t
(let ((class (classoid-pcl-class classoid))
(name (condition-slot-name slot)))
(values (slot-unbound class condition name)))))))
(defun find-slot-default-initarg (classoid slot)
(let ((initargs (condition-slot-initargs slot))
(cpl (condition-classoid-cpl classoid)))
(dolist (classoid cpl)
(let ((direct-default-initargs
(condition-classoid-direct-default-initargs classoid)))
(dolist (initarg initargs)
(let ((initfunction (third (assoc initarg direct-default-initargs))))
(when initfunction
(return-from find-slot-default-initarg
(values (funcall initfunction) t)))))))
(values nil nil)))
(defun find-condition-class-slot (condition-class slot-name)
(dolist (sclass
(condition-classoid-cpl condition-class)
(error "There is no slot named ~S in ~S."
slot-name condition-class))
(dolist (slot (condition-classoid-slots sclass))
(when (eq (condition-slot-name slot) slot-name)
(return-from find-condition-class-slot slot)))))
;;;; MAKE-CONDITION
;;; Pre-scan INITARGS to see whether any are stack-allocated.
;;; If not, then life is easy. If any are, then depending on whether the
;;; condition is a TYPE-ERROR, call TYPE-OF on the bad datum, so that
;;; if the condition outlives the extent of the object, and someone tries
;;; to print the condition, we don't crash.
;;; Putting a placeholder in for the datum would work, but seems a bit evil,
;;; since the user might actually want to know what it was. And we shouldn't
;;; assume that the object would definitely escape its dynamic-extent.
(defun allocate-condition (designator &rest initargs)
(when (oddp (length initargs))
(error 'simple-error
:format-control "odd-length initializer list: ~S."
;; Passing the initargs to LIST avoids consing them into
;; a list except when this error is signaled.
:format-arguments (list (apply #'list initargs))))
;; I am going to assume that people are not somehow getting to here
;; with a CLASSOID, which is not strictly legal as a designator,
;; but which is accepted because it is actually the desired thing.
;; It doesn't seem worth sweating over that detail, and in any event
;; we could say that it's a supported extension.
(let ((classoid (named-let lookup ((designator designator))
(typecase designator
(symbol (find-classoid designator nil))
(class (lookup (class-name designator)))
(t designator)))))
(unless (condition-classoid-p classoid)
(error 'simple-type-error
:datum designator
:expected-type 'sb-pcl::condition-class
:format-control "~S does not designate a condition class."
:format-arguments (list designator)))
(flet ((stream-err-p (layout)
(let ((stream-err-layout (load-time-value (find-layout 'stream-error))))
(or (eq layout stream-err-layout)
(find stream-err-layout (layout-inherits layout)))))
(type-err-p (layout)
(let ((type-err-layout (load-time-value (find-layout 'type-error))))
(or (eq layout type-err-layout)
(find type-err-layout (layout-inherits layout)))))
;; avoid full calls to STACK-ALLOCATED-P here
(stackp (x)
(let ((addr (get-lisp-obj-address x)))
(and (sb-vm:is-lisp-pointer addr)
(<= (get-lisp-obj-address sb-vm:*control-stack-start*) addr)
(< addr (get-lisp-obj-address sb-vm:*control-stack-end*))))))
(let* ((any-dx
(loop for arg-index from 1 below (length initargs) by 2
thereis (stackp (fast-&rest-nth arg-index initargs))))
(layout (classoid-layout classoid))
(extra (if (and any-dx (type-err-p layout)) 2 0)) ; space for secret initarg
(instance (%new-instance layout
(+ sb-vm:instance-data-start
1 ; ASSIGNED-SLOTS
(length initargs)
extra)))
(data-index (1+ sb-vm:instance-data-start))
(arg-index 0)
(have-type-error-datum)
(type-error-datum))
(setf (condition-assigned-slots instance) nil)
(macrolet ((store-pair (key val)
`(progn (%instance-set instance data-index ,key)
(%instance-set instance (1+ data-index) ,val))))
(cond ((not any-dx)
;; uncomplicated way
(loop (when (>= arg-index (length initargs)) (return))
(store-pair (fast-&rest-nth arg-index initargs)
(fast-&rest-nth (1+ arg-index) initargs))
(incf data-index 2)
(incf arg-index 2)))
(t
(loop (when (>= arg-index (length initargs)) (return))
(let ((key (fast-&rest-nth arg-index initargs))
(val (fast-&rest-nth (1+ arg-index) initargs)))
(when (and (eq key :datum)
(not have-type-error-datum)
(type-err-p layout))
(setq type-error-datum val
have-type-error-datum t))
(if (and (eq key :stream) (stream-err-p layout) (stackp val))
(store-pair key (sb-impl::make-stub-stream val))
(store-pair key val)))
(incf data-index 2)
(incf arg-index 2))
(when (and have-type-error-datum (/= extra 0))
;; We can get into serious trouble here if the
;; datum is already stack garbage!
(let ((actual-type (type-of type-error-datum)))
(store-pair 'dx-object-type actual-type))))))
(values instance classoid)))))
;;; Access the type of type-error-datum if the datum can't be accessed.
;;; Testing the stack pointer when rendering the condition is a heuristic
;;; that might work, but more likely, the erring frame has been exited
;;; and then the stack pointer changed again to make it seems like the
;;; object pointer is valid. I'm not sure what to do, but we can leave
;;; that decision for later.
(defun type-error-datum-stored-type (condition)
(do ((i (- (%instance-length condition) 2) (- i 2)))
((<= i (1+ sb-vm:instance-data-start))
(make-unbound-marker))
(when (eq (%instance-ref condition i) 'dx-object-type)
(return (%instance-ref condition (1+ i))))))
(defun make-condition (type &rest initargs)
"Make an instance of a condition object using the specified initargs."
;; Note: While ANSI specifies no exceptional situations in this function,
;; ALLOCATE-CONDITION will signal a type error if TYPE does not designate
;; a condition class. This seems fair enough.
(declare (explicit-check))
;; FIXME: the compiler should have a way to make GETF operate on a &MORE arg
;; so that the initargs are never listified.
(declare (dynamic-extent initargs))
(multiple-value-bind (condition classoid)
(apply #'allocate-condition type initargs)
;; Set any class slots with initargs present in this call.
(dolist (cslot (condition-classoid-class-slots classoid))
(loop for (key value) on initargs by #'cddr
when (memq key (condition-slot-initargs cslot))
do (setf (car (condition-slot-cell cslot)) value)
(return)
finally
(multiple-value-bind (value found)
(find-slot-default-initarg classoid cslot)
(when found
(setf (car (condition-slot-cell cslot)) value)))))
;; Default any slots with non-constant defaults now.
(dolist (hslot (condition-classoid-hairy-slots classoid))
(when (dolist (initarg (condition-slot-initargs hslot) t)
(unless (unbound-marker-p (getf initargs initarg sb-pcl:+slot-unbound+))
(return nil)))
(push (cons (condition-slot-name hslot)
(find-slot-default condition classoid hslot))
(condition-assigned-slots condition))))
condition))
;;;; DEFINE-CONDITION
(define-load-time-global *define-condition-hooks* nil)
(defun %set-condition-report (name report)
(setf (condition-classoid-report (find-classoid name))
report))
;;; Early definitions of slot accessor creators.
;;;
;;; Slot accessors must be generic functions, but ANSI does not seem
;;; to specify any of them, and we cannot support it before end of
;;; warm init. So we use ordinary functions inside SBCL, and switch to
;;; GFs only at the end of building.
(declaim (notinline install-condition-slot-reader
install-condition-slot-writer))
(defun install-condition-slot-reader (name condition slot-name)
(declare (ignore condition))
(setf (fdefinition name)
(set-closure-name
(lambda (condition) (condition-slot-value condition slot-name))
t
`(condition-slot-reader ,name))))
(defun install-condition-slot-writer (name condition slot-name)
(declare (ignore condition))
(setf (fdefinition name)
(set-closure-name
(lambda (new-value condition)
(set-condition-slot-value condition new-value slot-name))
t
`(condition-slot-writer ,name))))
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun %%compiler-define-condition (name direct-supers layout readers writers)
(declare (notinline find-classoid))
(preinform-compiler-about-class-type name nil)
(preinform-compiler-about-accessors 'condition readers writers)
(multiple-value-bind (class old-layout)
(insured-find-classoid name
#'condition-classoid-p
#'make-condition-classoid)
(setf (layout-classoid layout) class)
(setf (classoid-direct-superclasses class)
(mapcar #'find-classoid direct-supers))
(cond ((not old-layout)
(register-layout layout))
((not *type-system-initialized*)
(setf (layout-classoid old-layout) class)
(setq layout old-layout)
(unless (eq (classoid-layout class) layout)
(register-layout layout)))
((warn-if-altered-layout "current"
old-layout
"new"
(layout-length layout)
(layout-inherits layout)
(layout-depthoid layout)
(layout-bitmap layout))
(register-layout layout :invalidate t))
((not (classoid-layout class))
(register-layout layout)))
(setf (find-classoid name) class)
;; Initialize CPL slot.
(setf (condition-classoid-cpl class)
(remove-if-not #'condition-classoid-p
(std-compute-class-precedence-list class)))))
(defun %compiler-define-condition (name direct-supers layout readers writers)
(call-with-defining-class
'condition name
(lambda ()
(%%compiler-define-condition name direct-supers layout readers writers))))
) ; EVAL-WHEN
(defun %define-condition (name parent-types layout slots
direct-default-initargs all-readers all-writers
source-location &optional documentation)
(call-with-defining-class
'condition name
(lambda ()
(%%compiler-define-condition name parent-types layout all-readers all-writers)
(let ((classoid (find-classoid name)))
(when source-location
(setf (classoid-source-location classoid) source-location))
(setf (condition-classoid-slots classoid) slots
(condition-classoid-direct-default-initargs classoid) direct-default-initargs
(documentation name 'type) documentation)
(dolist (slot slots)
;; Set up reader and writer functions.
(let ((slot-name (condition-slot-name slot)))
(dolist (reader (condition-slot-readers slot))
(install-condition-slot-reader reader name slot-name))
(dolist (writer (condition-slot-writers slot))
(install-condition-slot-writer writer name slot-name))))
;; Compute effective slots and set up the class and hairy slots
;; (subsets of the effective slots.)
(setf (condition-classoid-class-slots classoid) '()
(condition-classoid-hairy-slots classoid) '())
(let ((eslots (compute-effective-slots classoid))
(e-def-initargs
(reduce #'append
(mapcar #'condition-classoid-direct-default-initargs
(condition-classoid-cpl classoid)))))
(dolist (slot eslots)
(ecase (condition-slot-allocation slot)
(:class
(unless (condition-slot-cell slot)
(setf (condition-slot-cell slot)
(list (if (condition-slot-initform-p slot)
(let ((initfun (condition-slot-initfunction slot)))
(aver (functionp initfun))
(funcall initfun))
sb-pcl:+slot-unbound+))))
(push slot (condition-classoid-class-slots classoid)))
((:instance nil)
(setf (condition-slot-allocation slot) :instance)
;; FIXME: isn't this "always hairy"?
(when (or (functionp (condition-slot-initfunction slot))
(dolist (initarg (condition-slot-initargs slot) nil)
(when (functionp (third (assoc initarg e-def-initargs)))
(return t))))
(push slot (condition-classoid-hairy-slots classoid)))))))
(when *type-system-initialized*
(dolist (fun *define-condition-hooks*)
(funcall fun classoid))))))
name)
(defmacro define-condition (name (&rest parent-types) (&rest slot-specs)
&body options)
"DEFINE-CONDITION Name (Parent-Type*) (Slot-Spec*) Option*
Define NAME as a condition type. This new type inherits slots and its
report function from the specified PARENT-TYPEs. A slot spec is a list of:
(slot-name :reader <rname> :initarg <iname> {Option Value}*
The DEFINE-CLASS slot options :ALLOCATION, :INITFORM, [slot] :DOCUMENTATION
and :TYPE and the overall options :DEFAULT-INITARGS and
[type] :DOCUMENTATION are also allowed.
The :REPORT option is peculiar to DEFINE-CONDITION. Its argument is either
a string or a two-argument lambda or function name. If a function, the
function is called with the condition and stream to report the condition.
If a string, the string is printed.
Condition types are classes, but (as allowed by ANSI and not as described in
CLtL2) are neither STANDARD-OBJECTs nor STRUCTURE-OBJECTs. WITH-SLOTS and
SLOT-VALUE may not be used on condition objects."
(check-designator name 'define-condition)
(let* ((parent-types (or parent-types '(condition)))
(layout (find-condition-layout name parent-types))
(documentation nil)
(report nil)
(direct-default-initargs ()))
(collect ((slots)
(all-readers nil append)
(all-writers nil append))
(dolist (spec slot-specs)
(with-current-source-form (spec)
(when (keywordp spec)
(warn "Keyword slot name indicates probable syntax error:~% ~S"
spec))
(let* ((spec (if (consp spec) spec (list spec)))
(slot-name (first spec))
(allocation :instance)
(initform-p nil)
documentation
initform)
(collect ((initargs)
(readers)
(writers))
(do ((options (rest spec) (cddr options)))
((null options))
(unless (and (consp options) (consp (cdr options)))
(error "malformed condition slot spec:~% ~S." spec))
(let ((arg (second options)))
(case (first options)
(:reader (readers arg))
(:writer (writers arg))
(:accessor
(readers arg)
(writers `(setf ,arg)))
(:initform
(when initform-p
(error "more than one :INITFORM in ~S" spec))
(setq initform-p t)
(setq initform arg))
(:initarg (initargs arg))
(:allocation
(setq allocation arg))
(:documentation
(when documentation
(error "more than one :DOCUMENTATION in ~S" spec))
(unless (stringp arg)
(error "slot :DOCUMENTATION argument is not a string: ~S"
arg))
(setq documentation arg))
(:type
(check-slot-type-specifier
arg slot-name (cons 'define-condition name)))
(t
(error "unknown slot option:~% ~S" (first options))))))
(all-readers (readers))
(all-writers (writers))
(slots `(make-condition-slot
:name ',slot-name
:initargs ',(initargs)
:readers ',(readers)
:writers ',(writers)
:initform-p ',initform-p
:documentation ',documentation
:initform ,(when initform-p `',initform)
:initfunction ,(when initform-p
`#'(lambda () ,initform))
:allocation ',allocation))))))
(dolist (option options)
(unless (consp option)
(error "bad option:~% ~S" option))
(case (first option)
(:documentation (setq documentation (second option)))
(:report
(let ((arg (second option)))
(setq report
`#'(named-lambda (condition-report ,name) (condition stream)
(declare (type condition condition)
(type stream stream))
,@(if (stringp arg)
`((declare (ignore condition))
(write-string ,arg stream))
`((funcall #',arg condition stream)))))))
(:default-initargs
(doplist (initarg initform) (rest option)
(push ``(,',initarg ,',initform ,#'(lambda () ,initform))
direct-default-initargs)))
(t
(error "unknown option: ~S" (first option)))))
;; Maybe kill docstring, but only under the cross-compiler.
#+(and (not sb-doc) sb-xc-host) (setq documentation nil)
`(progn
,@(when *top-level-form-p*
;; Avoid dumping uninitialized layouts, for sb-fasl::dump-layout
`((eval-when (:compile-toplevel)
(%compiler-define-condition ',name ',parent-types ,layout
',(all-readers) ',(all-writers)))))
(%define-condition ',name
',parent-types
,(if *top-level-form-p*
layout
`(find-condition-layout ',name ',parent-types))
(list ,@(slots))
(list ,@direct-default-initargs)
',(all-readers)
',(all-writers)
(sb-c:source-location)
,@(and documentation
`(,documentation)))
;; This needs to be after %DEFINE-CONDITION in case :REPORT
;; is a lambda referring to condition slot accessors:
;; they're not proclaimed as functions before it has run if
;; we're under EVAL or loaded as source.
(%set-condition-report ',name ,report)
',name))))
;;; Compute the effective slots of CLASS, copying inherited slots and
;;; destructively modifying direct slots.
;;;
;;; FIXME: It'd be nice to explain why it's OK to destructively modify
;;; direct slots. Presumably it follows from the semantics of
;;; inheritance and redefinition of conditions, but finding the cite
;;; and documenting it here would be good. (Or, if this is not in fact
;;; ANSI-compliant, fixing it would also be good.:-)
(defun compute-effective-slots (class)
(collect ((res (copy-list (condition-classoid-slots class))))
(dolist (sclass (cdr (condition-classoid-cpl class)))
(dolist (sslot (condition-classoid-slots sclass))
(let ((found (find (condition-slot-name sslot) (res)
:key #'condition-slot-name)))
(cond (found
(setf (condition-slot-initargs found)
(union (condition-slot-initargs found)
(condition-slot-initargs sslot)))
(unless (condition-slot-initform-p found)
(setf (condition-slot-initform-p found)
(condition-slot-initform-p sslot))
(setf (condition-slot-initform found)
(condition-slot-initform sslot))
(setf (condition-slot-initfunction found)
(condition-slot-initfunction sslot)))
(unless (condition-slot-allocation found)
(setf (condition-slot-allocation found)
(condition-slot-allocation sslot))))
(t
(res (copy-structure sslot)))))))
(res)))
;;;; various CONDITIONs specified by ANSI
(define-condition serious-condition (condition) ())
(define-condition error (serious-condition) ())
(define-condition warning (condition) ())
(define-condition style-warning (warning) ())
(defun simple-condition-printer (condition stream)
(let ((control (simple-condition-format-control condition)))
(if control
(apply #'format stream
control
(simple-condition-format-arguments condition))
(error "No format-control for ~S" condition))))
(define-condition simple-condition ()
((format-control :reader simple-condition-format-control
:initarg :format-control
:initform nil
:type format-control)
(format-arguments :reader simple-condition-format-arguments
:initarg :format-arguments
:initform nil
:type list))
(:report simple-condition-printer))
(define-condition simple-warning (simple-condition warning) ())
(define-condition simple-error (simple-condition error) ())
(define-condition storage-condition (serious-condition) ())
(defun decode-type-error-context (context type)
(typecase context
(cons
(case (car context)
(struct-context
(format nil "when setting slot ~s of structure ~s"
(cddr context) (cadr context)))
(t context)))
((eql sb-c::aref-context)
(let (*print-circle*)
(format nil "when setting an element of (ARRAY ~s)"
type)))
((eql sb-c::ftype-context)
"from the function type declaration.")
((member map)
(format nil "for the result type of ~a." context))
((and symbol
(not null))
(format nil "when binding ~s" context))
(t
context)))
(define-condition type-error (error)
((datum :reader type-error-datum :initarg :datum)
(expected-type :reader type-error-expected-type :initarg :expected-type)
(context :initform nil :reader type-error-context :initarg :context))
(:report report-general-type-error))
(defun report-general-type-error (condition stream)
(let ((type (type-error-expected-type condition))
(context (type-error-context condition)))
(if (eq context :multiple-values)
(format stream "~@<The values ~
~@:_~2@T~S ~
~@:_are not of type ~
~@:_~2@T~/sb-impl:print-type-specifier/~:@>"
(type-error-datum condition)
type)
(format stream "~@<The value ~
~@:_~2@T~S ~
~@:_is not of type ~
~@:_~2@T~/sb-impl:print-type-specifier/~@[ ~
~@:_~a~]~:@>"
(type-error-datum condition)
type
(decode-type-error-context (type-error-context condition)
type)))))
;;; not specified by ANSI, but too useful not to have around.
(define-condition simple-style-warning (simple-condition style-warning) ())
(define-condition simple-type-error (simple-condition type-error) ())
(define-condition program-error (error) ())
(define-condition parse-error (error) ())
(define-condition control-error (error) ())
(define-condition stream-error (error)
((stream :reader stream-error-stream :initarg :stream)))
(define-condition end-of-file (stream-error) ()
(:report
(lambda (condition stream)
(format stream
"end of file on ~S"
(stream-error-stream condition)))))
(define-condition closed-stream-error (stream-error) ()
(:report
(lambda (condition stream)
(format stream "~S is closed" (stream-error-stream condition)))))
(define-condition closed-saved-stream-error (closed-stream-error) ()
(:report
(lambda (condition stream)
(format stream "~S was closed by SB-EXT:SAVE-LISP-AND-DIE" (stream-error-stream condition)))))
(define-condition file-error (error)
((pathname :reader file-error-pathname :initarg :pathname))
(:report
(lambda (condition stream)
(format stream "error on file ~S" (file-error-pathname condition)))))
(define-condition package-error (error)
((package :reader package-error-package :initarg :package)))
(define-condition cell-error (error)
((name :reader cell-error-name :initarg :name)))
(define-condition values-list-argument-error (type-error)
()
(:report
(lambda (condition stream)
(format stream "~@<Attempt to use ~S on a dotted list or non-list: ~
~2I~_~S~:>"
'values-list (type-error-datum condition)))))
(define-condition unbound-variable (cell-error)
((not-yet-loaded :initform nil :reader not-yet-loaded :initarg :not-yet-loaded))
(:report
(lambda (condition stream)
(format stream
"~@<The variable ~S is unbound.~@?~@:>"
(cell-error-name condition)
(case (not-yet-loaded condition)
(:local
"~:@_It is a local variable ~
not available at compile-time.")
(t
""))))))
(define-condition undefined-function (cell-error)
((not-yet-loaded :initform nil :reader not-yet-loaded :initarg :not-yet-loaded))
(:report
(lambda (condition stream)
(let ((name (cell-error-name condition)))
(format stream
(if (and (symbolp name) (macro-function name))
(sb-format:tokens "~@<~/sb-ext:print-symbol-with-prefix/ is a macro, ~
not a function.~@:>")
(sb-format:tokens "~@<The function ~/sb-ext:print-symbol-with-prefix/ ~
is undefined.~@?~@:>"))
name
(case (not-yet-loaded condition)
(:local
(sb-format:tokens "~:@_It is a local function ~
not available at compile-time."))
((t) (sb-format:tokens "~:@_It is defined earlier in the ~
file but is not available at compile-time."))
(t
"")))))))
(define-condition retry-undefined-function
(simple-condition undefined-function) ())
(define-condition special-form-function (undefined-function) ()
(:report
(lambda (condition stream)
(format stream
"Cannot FUNCALL the SYMBOL-FUNCTION of special operator ~S."
(cell-error-name condition)))))
(define-condition arithmetic-error (error)
((operation :reader arithmetic-error-operation
:initarg :operation
:initform nil)
(operands :reader arithmetic-error-operands
:initarg :operands))
(:report (lambda (condition stream)
(format stream
"arithmetic error ~S signalled"
(type-of condition))
(when (arithmetic-error-operation condition)
(format stream
"~%Operation was (~S ~{~S~^ ~})."
(arithmetic-error-operation condition)
(arithmetic-error-operands condition))))))
(define-condition division-by-zero (arithmetic-error) ())
(define-condition floating-point-overflow (arithmetic-error) ())
(define-condition floating-point-underflow (arithmetic-error) ())
(define-condition floating-point-inexact (arithmetic-error) ())
(define-condition floating-point-invalid-operation (arithmetic-error) ())
(define-condition illegal-class-name-error (error)
((name :initarg :name :reader illegal-class-name-error-name))
(:default-initargs :name (missing-arg))
(:report (lambda (condition stream)
(format stream "~@<~S is not a legal class name.~@:>"
(illegal-class-name-error-name condition)))))
(define-condition print-not-readable (error)
((object :reader print-not-readable-object :initarg :object))
(:report
(lambda (condition stream)
(let ((obj (print-not-readable-object condition))
(*print-array* nil))