0.8.12.7: Merge package locks, AKA "what can go wrong with a 3783 line patch?"
[sbcl.git] / contrib / sb-aclrepl / inspect.lisp
1 ;;;; Inspector for sb-aclrepl
2 ;;;;
3 ;;;; The documentation, which may or may not apply in its entirety at
4 ;;;; any given time, for this functionality is on the ACL website:
5 ;;;;   <http://www.franz.com/support/documentation/6.2/doc/inspector.htm>.
6 ;;;;
7 ;;;; A summary of inspector navigation is contained in the below *INSPECT-HELP*
8 ;;;; variable.
9
10 (cl:in-package #:sb-aclrepl)
11
12 (eval-when (:compile-toplevel :load-toplevel :execute)
13   (defconstant +default-inspect-length+ 20))
14
15 (defstruct (%inspect (:constructor make-inspect)
16                      (:conc-name inspect-))
17   ;; stack of parents of inspected object
18   object-stack 
19   ;;  a stack of indices of parent object components
20   select-stack)
21
22 ;; FIXME - raw mode isn't currently used in object display
23 (defparameter *current-inspect* nil
24   "current inspect") 
25 (defparameter *inspect-raw* nil
26   "Raw mode for object display.")
27 (defparameter *inspect-length* +default-inspect-length+
28   "maximum number of components to print") 
29 (defparameter *skip-address-display* nil
30   "Skip displaying addresses of objects.")
31
32 (defvar *inspect-help*
33   ":istep takes between 0 to 3 arguments.
34 The commands are:
35 :i             redisplay current object
36 :i =           redisplay current object
37 :i nil         redisplay current object
38 :i ?           display this help
39 :i *           inspect the current * value
40 :i + <form>    inspect the (eval form)
41 :i slot <name> inspect component of object, even if name is an istep cmd
42 :i <index>     inspect the numbered component of object
43 :i <name>      inspect the named component of object
44 :i <form>      evaluation and inspect form
45 :i -           inspect parent
46 :i ^           inspect parent
47 :i <           inspect previous parent component
48 :i >           inspect next parent component
49 :i set <index> <form> set indexed component to evalated form
50 :i print <max> set the maximum number of components to print
51 :i skip <n>    skip a number of components when printing
52 :i tree        print inspect stack
53 ")
54
55 ;;; When *INSPECT-UNBOUND-OBJECT-MARKER* occurs in a parts list, it
56 ;;; indicates that that a slot is unbound.
57 (eval-when (:compile-toplevel :load-toplevel :execute)
58   (defvar *inspect-unbound-object-marker* (gensym "INSPECT-UNBOUND-OBJECT-")))
59
60
61 (defun inspector-fun (object input-stream output-stream)
62   (let ((*current-inspect* nil)
63         (*inspect-raw* nil)
64         (*inspect-length* *inspect-length*)
65         (*skip-address-display* nil))
66     (setq *current-inspect* (make-inspect))
67     (reset-stack object "(inspect ...)")
68     (redisplay output-stream)
69     (let ((*input* input-stream)
70           (*output* output-stream))
71       (repl :inspect t)))
72   (values))
73
74 (setq sb-impl::*inspect-fun* #'inspector-fun)
75
76 (defun istep (args stream)
77   (unless *current-inspect*
78     (setq *current-inspect* (make-inspect)))
79   (istep-dispatch args
80                   (first args)
81                   (when (first args) (read-from-string (first args)))
82                   stream))
83
84 (defun istep-dispatch (args option-string option stream)
85   (cond
86     ((or (string= "=" option-string) (zerop (length args)))
87      (istep-cmd-redisplay stream))
88     ((or (string= "-" option-string) (string= "^" option-string))
89      (istep-cmd-parent stream))
90     ((string= "*" option-string)
91      (istep-cmd-inspect-* stream))
92     ((string= "+" option-string)
93      (istep-cmd-inspect-new-form (read-from-string (second args)) stream))
94     ((or (string= "<" option-string)
95          (string= ">" option-string))
96      (istep-cmd-select-parent-component option-string stream))
97     ((string-equal "set" option-string)
98      (istep-cmd-set (second args) (third args) stream))
99     ((string-equal "raw" option-string)
100      (istep-cmd-set-raw (second args) stream))
101     ((string-equal "q" option-string)
102      (istep-cmd-reset))
103     ((string-equal "?" option-string)
104      (istep-cmd-help stream))
105     ((string-equal "skip" option-string)
106      (istep-cmd-skip (second args) stream))
107     ((string-equal "tree" option-string)
108      (istep-cmd-tree stream)) 
109     ((string-equal "print" option-string)
110      (istep-cmd-print (second args) stream))
111     ((string-equal "slot" option-string)
112      (istep-cmd-select-component (read-from-string (second args)) stream))
113     ((or (symbolp option)
114          (integerp option))
115      (istep-cmd-select-component option stream))
116     (t
117      (istep-cmd-set-stack option stream))))
118
119 (defun set-current-inspect (inspect)
120   (setq *current-inspect* inspect))
121
122 (defun reset-stack (&optional object label)
123   (cond
124     ((null label)
125      (setf (inspect-object-stack *current-inspect*) nil)
126      (setf (inspect-select-stack *current-inspect*) nil))
127     (t
128      (setf (inspect-object-stack *current-inspect*) (list object))
129      (setf (inspect-select-stack *current-inspect*) (list label)))))
130
131 (defun output-inspect-note (stream note &rest args)
132   (apply #'format stream note args)
133   (princ #\Newline stream))
134
135 (defun stack ()
136   (inspect-object-stack *current-inspect*))
137
138 (defun redisplay (stream &optional (skip 0))
139   (display-current stream *inspect-length* skip))
140
141 ;;;
142 ;;; istep command processing
143 ;;;
144
145 (defun istep-cmd-redisplay (stream)
146   (redisplay stream))
147
148 (defun istep-cmd-parent (stream)
149   (cond
150     ((> (length (inspect-object-stack *current-inspect*)) 1)
151      (setf (inspect-object-stack *current-inspect*)
152            (cdr (inspect-object-stack *current-inspect*)))
153      (setf (inspect-select-stack *current-inspect*)
154            (cdr (inspect-select-stack *current-inspect*)))
155      (redisplay stream))
156     ((stack)
157        (output-inspect-note stream "Object has no parent"))
158     (t
159      (no-object-msg stream))))
160
161 (defun istep-cmd-inspect-* (stream)
162   (reset-stack * "(inspect *)")
163   (redisplay stream))
164
165 (defun istep-cmd-inspect-new-form (form stream)
166   (inspector-fun (eval form) nil stream))
167
168 (defun istep-cmd-select-parent-component (option stream)
169   (if (stack)
170       (if (eql (length (stack)) 1)
171           (output-inspect-note stream "Object does not have a parent")
172           (let ((parent (second (stack)))
173                 (id (car (inspect-select-stack *current-inspect*))))
174             (multiple-value-bind (position parts)
175                 (find-part-id parent id)
176               (let ((new-position (if (string= ">" option)
177                                       (1+ position)
178                                       (1- position))))
179                 (if (< -1 new-position (parts-count parts))
180                     (let* ((value (component-at parts new-position)))
181                       (setf (car (inspect-object-stack *current-inspect*))
182                             value)
183                       (setf (car (inspect-select-stack *current-inspect*))
184                             (id-at parts new-position))
185                       (redisplay stream))
186                     (output-inspect-note stream
187                                          "Parent has no selectable component indexed by ~d"
188                                          new-position))))))
189       (no-object-msg stream)))
190
191 (defun istep-cmd-set-raw (option-string stream)
192   (when (inspect-object-stack *current-inspect*)
193     (cond
194       ((null option-string)
195        (setq *inspect-raw* t))
196       ((eq (read-from-string option-string) t)
197        (setq *inspect-raw* t))
198       ((eq (read-from-string option-string) nil)
199        (setq *inspect-raw* nil)))
200     (redisplay stream)))
201
202 (defun istep-cmd-reset ()
203   (reset-stack)
204   (throw 'repl-catcher (values :inspect nil)))
205
206 (defun istep-cmd-help (stream)
207   (format stream *inspect-help*))
208
209 (defun istep-cmd-skip (option-string stream)
210   (if option-string
211       (let ((len (read-from-string option-string)))
212         (if (and (integerp len) (>= len 0))
213             (redisplay stream len)
214             (output-inspect-note stream "Skip length invalid")))
215       (output-inspect-note stream "Skip length missing")))
216
217 (defun istep-cmd-print (option-string stream)
218   (if option-string
219       (let ((len (read-from-string option-string)))
220         (if (and (integerp len) (plusp len))
221             (setq *inspect-length* len)
222             (output-inspect-note stream "Cannot set print limit to ~A~%" len)))
223       (output-inspect-note stream "Print length missing")))
224
225 (defun select-description (select)
226   (typecase select
227     (integer
228      (format nil "which is componenent number ~d of" select))
229     (symbol
230      (format nil "which is the ~a component of" select))
231     (string
232      (format nil "which was selected by ~A" select))
233     (t
234      (write-to-string select))))
235
236 (defun istep-cmd-tree (stream)
237   (let ((stack (inspect-object-stack *current-inspect*)))
238     (if stack
239         (progn
240           (output-inspect-note stream "The current object is:")
241           (dotimes (i (length stack))
242             (output-inspect-note
243                stream "~A, ~A"
244                (inspected-description (nth i stack))
245                (select-description
246                 (nth i (inspect-select-stack *current-inspect*))))))
247         (no-object-msg stream))))
248
249 (defun istep-cmd-set (id-string value-string stream)
250   (if (stack)
251       (let ((id (when id-string (read-from-string id-string))))
252         (multiple-value-bind (position parts)
253             (find-part-id (car (stack)) id)
254           (if parts
255               (if position
256                   (when value-string
257                     (let ((new-value (eval (read-from-string value-string))))
258                       (let ((result (set-component-value (car (stack))
259                                                          id
260                                                          new-value
261                                                          (component-at
262                                                           parts position))))
263                         (typecase result
264                           (string
265                            (output-inspect-note stream result))
266                           (t
267                            (redisplay stream))))))
268                   (output-inspect-note
269                    stream
270                    "Object has no selectable component named by ~A" id))
271               (output-inspect-note stream
272                                    "Object has no selectable components"))))
273       (no-object-msg stream)))
274
275 (defun istep-cmd-select-component (id stream)
276   (if (stack)
277       (multiple-value-bind (position parts)
278           (find-part-id (car (stack)) id)
279         (cond
280           ((integerp position)
281            (let* ((value (component-at parts position)))
282              (cond ((eq value *inspect-unbound-object-marker*)
283                     (output-inspect-note stream "That slot is unbound"))
284                    (t
285                     (push value (inspect-object-stack *current-inspect*))
286                     (push id (inspect-select-stack *current-inspect*))
287                     (redisplay stream)))))
288           ((null parts)
289            (output-inspect-note stream "Object does not contain any subobjects"))
290           (t
291            (typecase id
292              (symbol
293               (output-inspect-note
294                stream "Object has no selectable component named ~A"
295                id))
296              (integer
297               (output-inspect-note
298                stream "Object has no selectable component indexed by ~d"
299                id))))))
300       (no-object-msg stream)))
301
302 (defun istep-cmd-set-stack (form stream)
303   (reset-stack (eval form) ":i ...")
304   (redisplay stream))
305
306
307 (defun no-object-msg (s)
308   (output-inspect-note s "No object is being inspected"))
309
310 (defun display-current (s length skip)
311   (if (stack)
312       (let ((inspected (car (stack))))
313         (setq cl:* inspected)
314         (display-inspect inspected s length skip))
315       (no-object-msg s)))
316
317
318 ;;;
319 ;;; aclrepl-specific inspection display
320 ;;;
321
322 (defun display-inspect (object stream &optional length (skip 0))
323   (multiple-value-bind (elements labels count)
324       (inspected-elements object length skip)
325     (fresh-line stream)
326     (format stream "~A" (inspected-description object))
327     (unless (or *skip-address-display*
328                 (eq object *inspect-unbound-object-marker*)
329                 (characterp object) (typep object 'fixnum))
330       (format stream " at #x~X" (logand
331                                  (sb-kernel:get-lisp-obj-address object)
332                                  (lognot sb-vm:lowtag-mask)))) 
333     (dotimes (i count)
334       (fresh-line stream)
335       (display-labeled-element (elt elements i) (elt labels i) stream))))
336   
337 (defun hex32-label-p (label)
338   (and (consp label) (eq (cdr label) :hex32)))
339
340 (defun array-label-p (label)
341   (and (consp label)
342        (stringp (cdr label))
343        (char= (char (cdr label) 0) #\[)))
344
345 (defun named-or-array-label-p (label)
346   (and (consp label)
347        (not (hex32-label-p label))))
348
349 (defun display-labeled-element (element label stream)
350   (cond
351     ((eq label :ellipses)
352      (format stream "   ..."))
353     ((eq label :tail)
354      (format stream "tail-> ~A" (inspected-description element)))
355     ((named-or-array-label-p label)
356      (format stream
357              (if (array-label-p label)
358                  "~4,' D ~A-> ~A"
359                  "~4,' D ~16,1,1,'-A> ~A")
360              (car label)
361              (format nil "~A " (cdr label))
362              (inspected-description element)))
363     ((hex32-label-p label)
364      (format stream "~4,' D-> #x~8,'0X" (car label) element))
365     (t
366      (format stream "~4,' D-> ~A" label (inspected-description element)))))
367
368 ;;; THE BEGINNINGS OF AN INSPECTOR API
369 ;;; which can be used to retrieve object descriptions as component values/labels and also
370 ;;; process print length and skip selectors
371 ;;;
372 ;;; FUNCTIONS TO CONSIDER FOR EXPORT
373 ;;;   FIND-PART-ID
374 ;;;   COMPONENT-AT
375 ;;;   ID-AT
376 ;;;   INSPECTED-ELEMENTS
377 ;;;   INSPECTED-DESCRIPTION
378 ;;;
379 ;;; will also need hooks
380 ;;;    *inspect-start-inspection*
381 ;;;       (maybe. Would setup a window for a GUI inspector)
382 ;;;    *inspect-prompt-fun*
383 ;;;    *inspect-read-cmd*
384 ;;;
385 ;;; and, either an *inspect-process-cmd*, or *inspect-display* hook
386 ;;; That'll depend if choose to have standardized inspector commands such that
387 ;;; (funcall *inspect-read-cmd*) will return a standard command that SBCL will
388 ;;; process and then call the *inspect-display* hook, or if the
389 ;;; *inspect-read-cmd* will return an impl-dependent cmd that sbcl will
390 ;;; send to the contributed inspector for processing and display.
391
392 (defun find-part-id (object id)
393   "COMPONENT-ID can be an integer or a name of a id.
394 Returns (VALUES POSITION PARTS).
395 POSITION is NIL if the id is invalid or not found."
396   (let* ((parts (inspected-parts object))
397          (name (if (symbolp id) (symbol-name id) id)))
398     (values
399      (cond
400        ((and (numberp id)
401              (< -1 id (parts-count parts))
402              (not (eq (parts-seq-type parts) :bignum)))
403         id)
404        (t
405         (case (parts-seq-type parts)
406           (:named
407            (position name (the list (parts-components parts))
408                      :key #'car :test #'string-equal))
409           ((:dotted-list :cyclic-list)
410            (when (string-equal name "tail")
411              (1- (parts-count parts)))))))
412      parts)))
413
414 (defun component-at (parts position)
415   (let ((count (parts-count parts))
416         (components (parts-components parts)))
417     (when (< -1 position count)
418       (case (parts-seq-type parts)
419         (:dotted-list
420          (if (= position (1- count))
421              (cdr (last components))
422              (elt components position)))
423         (:cyclic-list
424          (if (= position (1- count))
425              components
426              (elt components position)))
427         (:named
428          (cdr (elt components position)))
429         (:array
430          (aref (the array components) position))
431         (:bignum
432          (bignum-component-at components position))
433         (t
434          (elt components position))))))
435
436 (defun id-at (parts position)
437   (let ((count (parts-count parts)))
438     (when (< -1 position count)
439       (case (parts-seq-type parts)
440         ((:dotted-list :cyclic-list)
441          (if (= position (1- count))
442              :tail
443              position))
444         (:array
445          (array-index-string position parts))
446         (:named
447          (car (elt (parts-components parts) position)))
448         (t
449          position)))))
450
451 (defun inspected-elements (object &optional length (skip 0))
452   "Returns elements of an object that have been trimmed and labeled based on
453 length and skip. Returns (VALUES ELEMENTS LABELS ELEMENT-COUNT)
454 where ELEMENTS and LABELS are vectors containing ELEMENT-COUNT items.
455 LABELS elements may be a string, number, cons pair, :tail, or :ellipses.
456 This function may return an ELEMENT-COUNT of up to (+ 3 length) which would
457 include an :ellipses at the beginning, :ellipses at the end,
458 and the last element."
459   (let* ((parts (inspected-parts object))
460          (print-length (if length length (parts-count parts)))
461          (last-part (last-part parts))
462          (last-requested (last-requested parts print-length skip))
463          (element-count (compute-elements-count parts print-length skip))
464          (first-to (if (first-element-ellipses-p parts skip) 1 0))
465          (elements (when (plusp element-count) (make-array element-count)))
466          (labels (when (plusp element-count) (make-array element-count))))
467     (when (plusp element-count)
468       ;; possible first ellipses
469       (when (first-element-ellipses-p parts skip)
470         (set-element-values elements labels 0 nil :ellipses))
471       ;; main elements
472       (do* ((i 0 (1+ i)))
473            ((> i (- last-requested skip)))
474         (set-element elements labels parts (+ i first-to) (+ i skip)))
475       ;; last parts value if needed
476       (when (< last-requested last-part) 
477         (set-element elements labels parts (- element-count 1) last-part))
478       ;; ending ellipses or next to last parts value if needed
479       (when (< last-requested (1- last-part)) 
480         (if (= last-requested (- last-part 2))
481             (set-element elements labels parts (- element-count 2) (1- last-part)) 
482             (set-element-values elements labels (- element-count 2) nil :ellipses))))
483     (values elements labels element-count)))
484
485 (defun last-requested (parts print skip)
486   (min (1- (parts-count parts)) (+ skip print -1)))
487
488 (defun last-part (parts)
489   (1- (parts-count parts)))
490
491 (defun compute-elements-count (parts length skip)
492   "Compute the number of elements in parts given the print length and skip." 
493   (let ((element-count (min (parts-count parts) length
494                             (max 0 (- (parts-count parts) skip)))))
495     (when (and (plusp (parts-count parts)) (plusp skip)) ; starting ellipses
496       (incf element-count))
497     (when (< (last-requested parts length skip)
498              (last-part parts)) ; last value
499       (incf element-count) 
500       (when (< (last-requested parts length skip)
501                (1- (last-part parts))) ; ending ellipses
502         (incf element-count)))
503     element-count))
504
505 (defun set-element (elements labels parts to-index from-index)
506   (set-element-values elements labels to-index (component-at parts from-index)
507                       (label-at parts from-index)))
508
509 (defun set-element-values (elements labels index element label)
510   (setf (aref elements index) element)
511   (setf (aref labels index) label))
512
513 (defun first-element-ellipses-p (parts skip)
514   (and (parts-count parts) (plusp skip)))
515
516 (defun label-at (parts position)
517   "Helper function for inspected-elements. Conses the
518 position with the label if the label is a string."
519   (let ((id (id-at parts position)))
520     (cond
521       ((stringp id)
522        (cons position id))
523       ((eq (parts-seq-type parts) :bignum)
524        (cons position :hex32))
525       (t
526         id))))
527
528 (defun array-index-string (index parts)
529   "Formats an array index in row major format."
530   (let ((rev-dimensions (parts-seq-hint parts)))
531     (if (null rev-dimensions)
532         "[]"
533         (let ((list nil))
534           (dolist (dim rev-dimensions)
535             (multiple-value-bind (q r) (floor index dim)
536               (setq index q)
537               (push r list)))
538           (format nil "[~W~{,~W~}]" (car list) (cdr list))))))
539
540 \f
541 ;;; INSPECTED-DESCRIPTION
542 ;;;
543 ;;; Accepts an object and returns
544 ;;;   DESCRIPTION is a summary description of the destructured object,
545 ;;;   e.g. "the object is a CONS".
546
547 (defgeneric inspected-description (object))
548
549 (defmethod inspected-description ((object symbol))
550   (format nil "the symbol ~A" object))
551
552 (defmethod inspected-description ((object structure-object))
553   (format nil "~W" (find-class (type-of object))))
554
555 (defmethod inspected-description ((object package))
556   (format nil "the ~A package" (package-name object)))
557
558 (defmethod inspected-description ((object standard-object))
559   (format nil "~W" (class-of object)))
560
561 (defmethod inspected-description ((object sb-kernel:funcallable-instance))
562   (format nil "a funcallable-instance of type ~S" (type-of object)))
563
564 (defmethod inspected-description ((object function))
565   (format nil "~S" object) nil)
566
567 (defmethod inspected-description ((object vector))
568   (declare (vector object))
569   (format nil "a ~:[~;displaced ~]vector (~W)"
570           (and (sb-kernel:array-header-p object)
571                (sb-kernel:%array-displaced-p object))
572           (length object)))
573
574 (defmethod inspected-description ((object simple-vector))
575   (declare (simple-vector object))
576   (format nil "a simple ~A vector (~D)"
577           (array-element-type object)
578           (length object)))
579
580 (defmethod inspected-description ((object array))
581   (declare (array object))
582   (format nil "~:[A displaced~;An~] array of ~A with dimensions ~W"
583           (and (sb-kernel:array-header-p object)
584                (sb-kernel:%array-displaced-p object))
585           (array-element-type object)
586           (array-dimensions object)))
587
588 (defun simple-cons-pair-p (object)
589   (atom (cdr object)))
590
591 (defmethod inspected-description ((object cons))
592   (if (simple-cons-pair-p object)
593       "a cons cell"
594       (inspected-description-of-nontrivial-list object)))
595
596 (defun cons-safe-length (object)
597   "Returns (VALUES LENGTH LIST-TYPE) where length is the number of
598 cons cells and LIST-TYPE is :normal, :dotted, or :cyclic"
599     (do ((length 1 (1+ length))
600          (lst (cdr object) (cdr lst)))
601         ((or (not (consp lst))
602              (eq object lst))
603          (cond
604            ((null lst)
605             (values length :normal))
606            ((atom lst)
607             (values length :dotted))
608            ((eq object lst)
609             (values length :cyclic))))
610       ;; nothing to do in body
611       ))
612
613 (defun inspected-description-of-nontrivial-list (object)
614   (multiple-value-bind (length list-type) (cons-safe-length object)
615     (format nil "a ~A list with ~D element~:*~P~A"
616             (string-downcase (symbol-name list-type)) length
617             (ecase list-type
618               ((:dotted :cyclic) "+tail")
619               (:normal "")))))
620
621 (defun ref32-hexstr (obj &optional (offset 0))
622   (format nil "~8,'0X" (ref32 obj offset)))
623
624 (defun ref32 (obj &optional (offset 0))
625   (sb-sys::without-gcing
626    (sb-sys:sap-ref-32
627     (sb-sys:int-sap
628      (logand (sb-kernel:get-lisp-obj-address obj) (lognot sb-vm:lowtag-mask)))
629     offset)))
630
631 (defun description-maybe-internals (fmt objects internal-fmt &rest args)
632   (let ((base (apply #'format nil fmt objects)))
633     (if *skip-address-display*
634         base
635         (concatenate 'string
636                      base " " (apply #'format nil internal-fmt args)))))
637               
638 (defmethod inspected-description ((object double-float))
639   (description-maybe-internals "double-float ~W" (list object)
640                                "[#~A ~A]"
641                                (ref32-hexstr object 12)
642                                (ref32-hexstr object 8)))
643
644 (defmethod inspected-description ((object single-float))
645   (description-maybe-internals "single-float ~W" (list object)
646                                "[#x~A]"
647                                (ref32-hexstr object 4)))
648
649 (defmethod inspected-description ((object fixnum))
650   (description-maybe-internals "fixnum ~W" (list object)
651                                "[#x~8,'0X]"
652                                (ash object (1- sb-vm:n-lowtag-bits))))
653
654 (defmethod inspected-description ((object complex))
655   (format nil "complex number ~W" object))
656
657 (defmethod inspected-description ((object simple-string))
658   (format nil "a simple-string (~W) ~W" (length object) object))
659
660 (defun bignum-words (bignum)
661   "Return the number of 32-bit words in a bignum"
662   (ash
663    (logand (ref32 bignum)
664            (lognot sb-vm:widetag-mask))
665    (- sb-vm:n-widetag-bits))) 
666
667 (defun bignum-component-at (bignum offset)
668   "Return the 32-bit word at 32-bit wide offset"
669   (ref32 bignum (* 4 (1+ offset))))
670
671 (defmethod inspected-description ((object bignum))
672   (format nil  "bignum ~W with ~D 32-bit word~:*~P" object
673           (bignum-words object)))
674
675 (defmethod inspected-description ((object ratio))
676   (format nil "ratio ~W" object))
677
678 (defmethod inspected-description ((object character))
679   ;; FIXME: This will need to change as and when we get more characters
680   ;; than just the 256 we have today.
681   (description-maybe-internals "character ~W char-code #x~2,'0X"
682                                (list object (char-code object))
683                                "[#x~8,'0X]"
684                                (logior sb-vm:base-char-widetag 
685                                        (ash (char-code object)
686                                             sb-vm:n-widetag-bits))))
687
688 (defmethod inspected-description ((object t))
689   (format nil "a generic object ~W" object))
690
691 (defmethod inspected-description ((object (eql *inspect-unbound-object-marker*)))
692   "..unbound..")
693
694 \f
695 ;;; INSPECTED-PARTS
696 ;;;
697 ;;; Accepts the arguments OBJECT LENGTH SKIP and returns,
698 ;;;   (LIST COMPONENTS SEQ-TYPE COUNT SEQ-HINT)
699 ;;; where..
700 ;;;
701 ;;;   COMPONENTS are the component parts of OBJECT (whose
702 ;;;   representation is determined by SEQ-TYPE). Except for the
703 ;;;   SEQ-TYPE :named and :array, components is just the OBJECT itself
704 ;;;
705 ;;;   SEQ-TYPE determines what representation is used for components
706 ;;;   of COMPONENTS.
707 ;;;      If SEQ-TYPE is :named, then each element is (CONS NAME VALUE)
708 ;;;      If SEQ-TYPE is :dotted-list, then each element is just value,
709 ;;;        but the last element must be retrieved by
710 ;;;        (cdr (last components))
711 ;;;      If SEQ-TYPE is :cylic-list, then each element is just value,
712 ;;;      If SEQ-TYPE is :list, then each element is a value of an array
713 ;;;      If SEQ-TYPE is :vector, then each element is a value of an vector
714 ;;;      If SEQ-TYPE is :array, then each element is a value of an array
715 ;;;        with rank >= 2. The 
716 ;;;      If SEQ-TYPE is :bignum, then object is just a bignum and not a
717 ;;;        a sequence 
718 ;;;
719 ;;;   COUNT is the total number of components in the OBJECT
720 ;;;
721 ;;; SEQ-HINT is a seq-type dependent hint. Used by SEQ-TYPE :array
722 ;;; to hold the reverse-dimensions of the orignal array.
723
724 (declaim (inline parts-components))
725 (defun parts-components (parts)
726   (first parts))
727
728 (declaim (inline parts-count))
729 (defun parts-count (parts)
730   (second parts))
731
732 (declaim (inline parts-seq-type))
733 (defun parts-seq-type (parts)
734   (third parts))
735
736 (declaim (inline parts-seq-hint))
737 (defun parts-seq-hint (parts)
738   (fourth parts))
739
740 (defgeneric inspected-parts (object)
741   )
742
743 (defmethod inspected-parts ((object symbol))
744   (let ((components
745          (list (cons "NAME" (symbol-name object))
746                (cons "PACKAGE" (symbol-package object))
747                (cons "VALUE" (if (boundp object)
748                                  (symbol-value object)
749                                  *inspect-unbound-object-marker*))
750                (cons "FUNCTION" (if (fboundp object)
751                                     (symbol-function object)
752                                     *inspect-unbound-object-marker*))
753                (cons "PLIST" (symbol-plist object)))))
754     (list components (length components) :named nil)))
755
756 (defun inspected-structure-parts (object)
757   (let ((components-list '())
758         (info (sb-kernel:layout-info (sb-kernel:layout-of object))))
759     (when (sb-kernel::defstruct-description-p info)
760       (dolist (dd-slot (sb-kernel:dd-slots info) (nreverse components-list))
761         (push (cons (string (sb-kernel:dsd-name dd-slot))
762                     (funcall (sb-kernel:dsd-accessor-name dd-slot) object))
763               components-list)))))
764
765 (defmethod inspected-parts ((object structure-object))
766   (let ((components (inspected-structure-parts object)))
767     (list components (length components) :named nil)))
768
769 (defun inspected-standard-object-parts (object)
770   (let ((components nil)
771         (class-slots (sb-pcl::class-slots (class-of object))))
772     (dolist (class-slot class-slots components)
773       (let* ((slot-name (slot-value class-slot 'sb-pcl::name))
774              (slot-value (if (slot-boundp object slot-name)
775                              (slot-value object slot-name)
776                              *inspect-unbound-object-marker*)))
777         (push (cons (symbol-name slot-name) slot-value) components)))))
778
779
780 (defmethod inspected-parts ((object standard-object))
781   (let ((components (inspected-standard-object-parts object)))
782     (list components (length components) :named nil)))
783
784 (defmethod inspected-parts ((object sb-kernel:funcallable-instance))
785   (let ((components (inspected-standard-object-parts object)))
786     (list components (length components) :named nil)))
787
788 (defmethod inspected-parts ((object condition))
789   (let ((components (inspected-standard-object-parts object)))
790     (list components (length components) :named nil)))
791
792 (defmethod inspected-parts ((object function))
793   (let* ((type (sb-kernel:widetag-of object))
794          (object (if (= type sb-vm:closure-header-widetag)
795                      (sb-kernel:%closure-fun object)
796                      object))
797          (components (list (cons "arglist"
798                                (sb-kernel:%simple-fun-arglist object)))))
799     (list components (length components) :named nil)))
800
801 (defmethod inspected-parts ((object vector))
802   (list object (length object) :vector nil))
803
804 (defmethod inspected-parts ((object array))
805   (let ((size (array-total-size object)))
806     (list (make-array size :displaced-to object)
807             size
808             :array
809             (reverse (array-dimensions object)))))
810
811 (defmethod inspected-parts ((object cons))
812   (if (simple-cons-pair-p object)
813       (inspected-parts-of-simple-cons object)
814       (inspected-parts-of-nontrivial-list object)))
815
816 (defun inspected-parts-of-simple-cons (object)
817   (let ((components (list (cons "car" (car object))
818                         (cons "cdr" (cdr object)))))
819     (list components 2 :named nil)))
820
821 (defun inspected-parts-of-nontrivial-list (object)
822     (multiple-value-bind (count list-type) (cons-safe-length object)
823       (case list-type
824         (:normal
825          (list object count :list nil))
826         (:cyclic
827          (list object (1+ count) :cyclic-list nil))
828         (:dotted
829          ;; count tail element
830          (list object (1+ count) :dotted-list nil)))))
831
832 (defmethod inspected-parts ((object complex))
833   (let ((components (list (cons "real" (realpart object))
834                         (cons "imag" (imagpart object)))))
835     (list components (length components) :named nil)))
836
837 (defmethod inspected-parts ((object ratio))
838   (let ((components (list (cons "numerator" (numerator object))
839                         (cons "denominator" (denominator object)))))
840     (list components (length components) :named nil)))
841
842 (defmethod inspected-parts ((object bignum))
843     (list object (bignum-words object) :bignum nil))
844
845 (defmethod inspected-parts ((object t))
846   (list nil 0 nil nil))
847
848
849 ;; FIXME - implement setting of component values
850
851 (defgeneric set-component-value (object component-id value element))
852
853 (defmethod set-component-value ((object cons) id value element)
854   (format nil "Cons object does not support setting of component ~A" id))
855
856 (defmethod set-component-value ((object array) id value element)
857   (format nil "Array object does not support setting of component ~A" id))
858
859 (defmethod set-component-value ((object symbol) id value element)
860   (format nil "Symbol object does not support setting of component ~A" id))
861
862 (defmethod set-component-value ((object structure-object) id value element)
863   (format nil "Structure object does not support setting of component ~A" id))
864
865 (defmethod set-component-value ((object standard-object) id value element)
866   (format nil "Standard object does not support setting of component ~A" id))
867
868 (defmethod set-component-value ((object t) id value element)
869   (format nil "Object does not support setting of component ~A" id))