1 ;;;; the DESCRIBE system
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 ;;; SB-IMPL, not SB!IMPL, since we're built in warm load.
13 (in-package "SB-IMPL")
15 ;;;; Utils, move elsewhere.
17 (defun class-name-or-class (class)
18 (let ((name (class-name class)))
19 (if (eq class (find-class name nil))
24 (if (typep x 'generic-function)
25 (sb-pcl:generic-function-name x)
28 ;;; Prints X on a single line, limiting output length by *PRINT-RIGHT-MARGIN*
29 ;;; -- good for printing object parts, etc.
30 (defun prin1-to-line (x &key (columns 1) (reserve 0))
31 (let* ((line (write-to-string x :escape t :readably nil :lines 2 :circle t))
32 (p (position #\newline line))
33 (limit (truncate (- *print-right-margin* reserve) columns)))
34 (flet ((trunc (&optional end)
35 (let ((line-end (- limit 2)))
36 (with-output-to-string (s)
37 (write-string line s :end (if end
40 (write-string ".." s)))))
43 ((> (length line) limit)
48 (defun describe (object &optional (stream-designator *standard-output*))
50 "Print a description of OBJECT to STREAM-DESIGNATOR."
51 (let ((stream (out-synonym-of stream-designator))
52 (*print-right-margin* (or *print-right-margin* 72)))
53 ;; Until sbcl-0.8.0.x, we did
54 ;; (FRESH-LINE STREAM)
55 ;; (PPRINT-LOGICAL-BLOCK (STREAM NIL)
57 ;; here. However, ANSI's specification of DEFUN DESCRIBE,
58 ;; DESCRIBE exists as an interface primarily to manage argument
59 ;; defaulting (including conversion of arguments T and NIL into
60 ;; stream objects) and to inhibit any return values from
62 ;; doesn't mention either FRESH-LINEing or PPRINT-LOGICAL-BLOCKing,
63 ;; and the example of typical DESCRIBE-OBJECT behavior in ANSI's
64 ;; specification of DESCRIBE-OBJECT will work poorly if we do them
65 ;; here. (The example method for DESCRIBE-OBJECT does its own
66 ;; FRESH-LINEing, which is a physical directive which works poorly
67 ;; inside a pretty-printer logical block.)
68 (describe-object object stream)
69 ;; We don't TERPRI here either (any more since sbcl-0.8.0.x), because
70 ;; again ANSI's specification of DESCRIBE doesn't mention it and
71 ;; ANSI's example of DESCRIBE-OBJECT does its own final TERPRI.
78 ;;;; * Each interesting class has a primary method of its own.
80 ;;;; * Output looks like
82 ;;;; object-self-string
83 ;;;; [object-type-string]
92 ;;;; * The newline policy that gets the whitespace right is for
93 ;;;; each block to both start and end with a newline.
95 (defgeneric object-self-string (x))
97 (defmethod object-self-string (x)
100 (defmethod object-self-string ((x symbol))
101 (let ((*package* (find-package :keyword)))
102 (prin1-to-string x)))
104 (defgeneric object-type-string (x))
106 (defmethod object-type-string (x)
107 (let ((type (class-name-or-class (class-of x))))
109 (string-downcase type)
110 (prin1-to-string type))))
112 (defmethod object-type-string ((x cons))
113 (if (listp (cdr x)) "list" "cons"))
115 (defmethod object-type-string ((x hash-table))
118 (defmethod object-type-string ((x condition))
121 (defmethod object-type-string ((x structure-object))
124 (defmethod object-type-string ((x standard-object))
127 (defmethod object-type-string ((x function))
129 (simple-fun "compiled function")
130 (closure "compiled closure")
132 (sb-eval:interpreted-function
133 "interpreted function")
137 "funcallable-instance")))
139 (defmethod object-type-string ((x stream))
142 (defmethod object-type-string ((x sb-gray:fundamental-stream))
145 (defmethod object-type-string ((x package))
148 (defmethod object-type-string ((x array))
149 (cond ((or (stringp x) (bit-vector-p x))
150 (format nil "~@[simple-~*~]~A"
151 (typep x 'simple-array)
153 (base-string "base-string")
157 (if (simple-vector-p x)
159 (format nil "~@[simple ~*~]~@[specialized ~*~]~:[array~;vector~]"
160 (typep x 'simple-array)
161 (neq t (array-element-type x))
164 (defmethod object-type-string ((x character))
166 (standard-char "standard-char")
167 (base-char "base-char")
170 (defun print-standard-describe-header (x stream)
171 (format stream "~&~A~% [~A]~%"
172 (object-self-string x)
173 (object-type-string x)))
175 (defgeneric describe-object (x stream))
179 (defmethod describe-object ((x t) s)
180 (print-standard-describe-header x s))
182 (defmethod describe-object ((x cons) s)
183 (print-standard-describe-header x s)
184 (describe-function x nil s))
186 (defmethod describe-object ((x function) s)
187 (print-standard-describe-header x s)
188 (describe-function nil x s))
190 (defmethod describe-object ((x class) s)
191 (print-standard-describe-header x s)
192 (describe-class nil x s)
193 (describe-instance x s))
195 (defmethod describe-object ((x sb-pcl::slot-object) s)
196 (print-standard-describe-header x s)
197 (describe-instance x s))
199 (defmethod describe-object ((x character) s)
200 (print-standard-describe-header x s)
201 (format s "~%:_Char-code: ~S" (char-code x))
202 (format s "~%:_Char-name: ~A~%_" (char-name x)))
204 (defmethod describe-object ((x array) s)
205 (print-standard-describe-header x s)
206 (format s "~%Element-type: ~S" (array-element-type x))
208 (if (array-has-fill-pointer-p x)
209 (format s "~%Fill-pointer: ~S~%Size: ~S"
211 (array-total-size x))
212 (format s "~%Length: ~S" (length x)))
213 (format s "~%Dimensions: ~S" (array-dimensions x)))
214 (let ((*print-array* nil))
215 (unless (typep x 'simple-array)
216 (format s "~%Adjustable: ~A" (if (adjustable-array-p x) "yes" "no"))
217 (multiple-value-bind (to offset) (array-displacement x)
218 (if (format s "~%Displaced-to: ~A~%Displaced-offset: ~S"
221 (format s "~%Displaced: no"))))
222 (when (and (not (array-displacement x)) (array-header-p x))
223 (format s "~%Storage vector: ~A"
224 (prin1-to-line (array-storage-vector x))))
227 (defmethod describe-object ((x hash-table) s)
228 (print-standard-describe-header x s)
229 ;; Don't print things which are already apparent from the printed
230 ;; representation -- COUNT, TEST, and WEAKNESS
231 (format s "~%Occupancy: ~,1F" (float (/ (hash-table-count x)
232 (hash-table-size x))))
233 (format s "~%Rehash-threshold: ~S" (hash-table-rehash-threshold x))
234 (format s "~%Rehash-size: ~S" (hash-table-rehash-size x))
235 (format s "~%Size: ~S" (hash-table-size x))
236 (format s "~%Synchronized: ~A" (if (hash-table-synchronized-p x) "yes" "no"))
239 (defmethod describe-object ((symbol symbol) stream)
240 (print-standard-describe-header symbol stream)
241 ;; Describe the value cell.
242 (let* ((kind (info :variable :kind symbol))
244 (:special "a special variable")
245 (:macro "a symbol macro")
246 (:constant "a constant variable")
247 (:global "a global variable")
248 (:unknown "an undefined variable")
249 (:alien "an alien variable"))))
250 (when (or (not (eq :unknown kind)) (boundp symbol))
251 (pprint-logical-block (stream nil)
252 (format stream "~%~A names ~A:" symbol wot)
253 (pprint-indent :block 2 stream)
254 (when (eq (info :variable :where-from symbol) :declared)
255 (format stream "~@:_Declared type: ~S"
256 (type-specifier (info :variable :type symbol))))
257 (when (info :variable :always-bound symbol)
258 (format stream "~@:_Declared always-bound."))
261 (let ((info (info :variable :alien-info symbol)))
262 (format stream "~@:_Value: ~S" (eval symbol))
263 (format stream "~@:_Type: ~S"
264 (sb-alien-internals:unparse-alien-type
265 (sb-alien::heap-alien-info-type info)))
266 (format stream "~@:_Address: #x~8,'0X"
267 (sap-int (eval (sb-alien::heap-alien-info-sap-form info))))))
269 (let ((expansion (info :variable :macro-expansion symbol)))
270 (format stream "~@:_Expansion: ~S" expansion)))
272 (format stream "~:@_Value: ~S" (symbol-value symbol)))
273 ((not (eq kind :unknown))
274 (format stream "~:@_Currently unbound.")))
275 (describe-documentation symbol 'variable stream)
278 ;; TODO: We could grovel over all packages looking for and
279 ;; reporting other phenomena, e.g. IMPORT and SHADOW, or
280 ;; availability in some package even after (SYMBOL-PACKAGE SYMBOL) has
283 ;; TODO: It might also be nice to describe (find-package symbol)
284 ;; if one exists. Maybe not all the exports, etc, but the package
286 (describe-function symbol nil stream)
287 (describe-class symbol nil stream)
290 (let* ((kind (info :type :kind symbol))
293 (or (info :type :expander symbol) t))
295 (or (info :type :translator symbol) t)))))
297 (pprint-newline :mandatory stream)
298 (pprint-logical-block (stream nil)
299 (pprint-indent :block 2 stream)
300 (format stream "~A names a ~@[primitive~* ~]type-specifier:"
302 (eq kind :primitive))
303 (describe-documentation symbol 'type stream (eq t fun))
305 (describe-lambda-list (if (eq :primitive kind)
306 (%fun-lambda-list fun)
307 (info :type :lambda-list symbol))
309 (when (eq (%fun-fun fun) (%fun-fun (constant-type-expander t)))
310 (format stream "~@:_Expansion: ~S" (funcall fun (list symbol))))))
313 (when (or (member symbol sb-c::*policy-qualities*)
314 (assoc symbol sb-c::*policy-dependent-qualities*))
315 (pprint-logical-block (stream nil)
316 (pprint-newline :mandatory stream)
317 (pprint-indent :block 2 stream)
318 (format stream "~A names a~:[ dependent~;n~] optimization policy quality:"
320 (member symbol sb-c::*policy-qualities*))
321 (describe-documentation symbol 'optimize stream t))
324 ;; Print out properties.
325 (let ((plist (symbol-plist symbol)))
327 (pprint-logical-block (stream nil)
328 (format stream "~%Symbol-plist:")
329 (pprint-indent :block 2 stream)
330 (sb-pcl::doplist (key value) plist
331 (format stream "~@:_~A -> ~A"
332 (prin1-to-line key :columns 2 :reserve 5)
333 (prin1-to-line value :columns 2 :reserve 5))))
336 (defmethod describe-object ((package package) stream)
337 (print-standard-describe-header package stream)
338 (pprint-logical-block (stream nil)
339 (describe-documentation package t stream)
340 (flet ((humanize (list)
341 (sort (mapcar (lambda (x)
348 (describe-stuff label list stream :escape nil)))
349 (let ((implemented (humanize (package-implemented-by-list package)))
350 (implements (humanize (package-implements-list package)))
351 (nicks (humanize (package-nicknames package)))
352 (uses (humanize (package-use-list package)))
353 (used (humanize (package-used-by-list package)))
354 (shadows (humanize (package-shadowing-symbols package)))
355 (this (list (package-name package)))
357 (do-external-symbols (ext package)
359 (setf exports (humanize exports))
360 (when (package-locked-p package)
361 (format stream "~@:_Locked."))
362 (when (set-difference implemented this :test #'string=)
363 (out "Implemented-by-list" implemented))
364 (when (set-difference implements this :test #'string=)
365 (out "Implements-list" implements))
366 (out "Nicknames" nicks)
367 (out "Use-list" uses)
368 (out "Used-by-list" used)
369 (out "Shadows" shadows)
370 (out "Exports" exports)
371 (format stream "~@:_~S internal symbols."
372 (package-internal-symbol-count package))))
375 ;;;; Helpers to deal with shared functionality
377 (defun describe-class (name class stream)
378 (let* ((by-name (not class))
379 (name (if class (class-name class) name))
380 (class (if class class (find-class name nil))))
382 (let ((metaclass-name (class-name (class-of class))))
383 (pprint-logical-block (stream nil)
385 (format stream "~%~A names the ~(~A~) ~S:"
389 (pprint-indent :block 2 stream))
390 (describe-documentation class t stream)
391 (when (sb-mop:class-finalized-p class)
392 (describe-stuff "Class precedence-list"
393 (mapcar #'class-name-or-class (sb-mop:class-precedence-list class))
395 (describe-stuff "Direct superclasses"
396 (mapcar #'class-name-or-class (sb-mop:class-direct-superclasses class))
398 (let ((subs (mapcar #'class-name-or-class (sb-mop:class-direct-subclasses class))))
400 (describe-stuff "Direct subclasses" subs stream)
401 (format stream "~@:_No subclasses.")))
402 (unless (sb-mop:class-finalized-p class)
403 (format stream "~@:_Not yet finalized."))
404 (if (eq 'structure-class metaclass-name)
405 (let* ((dd (find-defstruct-description name))
406 (slots (dd-slots dd)))
408 (format stream "~@:_Slots:~:{~@:_ ~S~
409 ~@:_ Type: ~A ~@[~A~]~
411 (mapcar (lambda (dsd)
415 (unless (eq t (dsd-raw-type dsd))
419 (format stream "~@:_No slots.")))
420 (let ((slots (sb-mop:class-direct-slots class)))
422 (format stream "~@:_Direct slots:~:{~@:_ ~S~
424 ~@[~@:_ Allocation: ~S~]~
425 ~@[~@:_ Initargs: ~{~S~^, ~}~]~
426 ~@[~@:_ Initform: ~S~]~
427 ~@[~@:_ Readers: ~{~S~^, ~}~]~
428 ~@[~@:_ Writers: ~{~S~^, ~}~]~
429 ~@[~@:_ Documentation:~@:_ ~@<~@;~A~:>~]~}"
430 (mapcar (lambda (slotd)
431 (list (sb-mop:slot-definition-name slotd)
432 (let ((type (sb-mop:slot-definition-type slotd)))
433 (unless (eq t type) type))
434 (let ((alloc (sb-mop:slot-definition-allocation slotd)))
435 (unless (eq :instance alloc) alloc))
436 (sb-mop:slot-definition-initargs slotd)
437 (sb-mop:slot-definition-initform slotd)
438 (sb-mop:slot-definition-readers slotd)
439 (sb-mop:slot-definition-writers slotd)
440 ;; FIXME: does this get the prefix right?
441 (quiet-doc slotd t)))
443 (format stream "~@:_No direct slots."))))
444 (pprint-newline :mandatory stream))))))
446 (defun describe-instance (object stream)
447 (let* ((class (class-of object))
448 (slotds (sb-mop:class-slots class))
449 (max-slot-name-length 0)
452 ;; Figure out a good width for the slot-name column.
453 (flet ((adjust-slot-name-length (name)
454 (setf max-slot-name-length
455 (max max-slot-name-length (length (symbol-name name))))))
456 (dolist (slotd slotds)
457 (adjust-slot-name-length (sb-mop:slot-definition-name slotd))
458 (push slotd (getf plist (sb-mop:slot-definition-allocation slotd))))
459 (setf max-slot-name-length (min (+ max-slot-name-length 3) 30)))
461 ;; Now that we know the width, we can print.
462 (flet ((describe-slot (name value)
463 (format stream "~% ~A~VT = ~A" name max-slot-name-length
464 (prin1-to-line value))))
465 (sb-pcl::doplist (allocation slots) plist
466 (format stream "~%Slots with ~S allocation:" allocation)
467 (dolist (slotd (nreverse slots))
469 (sb-mop:slot-definition-name slotd)
470 (sb-pcl::slot-value-or-default object (sb-mop:slot-definition-name slotd))))))
472 (format stream "~@:_No slots."))
475 (defun quiet-doc (object type)
476 (handler-bind ((warning #'muffle-warning))
477 (documentation object type)))
479 (defun describe-documentation (object type stream &optional undoc newline)
480 (let ((doc (quiet-doc object type)))
482 (format stream "~@:_Documentation:~@:_")
483 (pprint-logical-block (stream nil :per-line-prefix " ")
486 (format stream "~@:_(undocumented)")))
488 (pprint-newline :mandatory stream))))
490 (defun describe-stuff (label list stream &key (escape t))
493 (format stream "~@:_~A:~@<~;~{ ~S~^,~:_~}~;~:>" label list)
494 (format stream "~@:_~A:~@<~;~{ ~A~^,~:_~}~;~:>" label list))))
496 (defun describe-lambda-list (lambda-list stream)
497 (format stream "~@:_Lambda-list: ~:A" lambda-list))
499 (defun describe-function-source (function stream)
500 (if (compiled-function-p function)
501 (let* ((code (fun-code-header (%fun-fun function)))
502 (info (sb-kernel:%code-debug-info code)))
504 (let ((source (sb-c::debug-info-source info)))
506 (let ((namestring (sb-c::debug-source-namestring source)))
507 ;; This used to also report the times the source was created
508 ;; and compiled, but that seems more like noise than useful
509 ;; information -- but FWIW that are to be had as
510 ;; SB-C::DEBUG-SOUCE-CREATED/COMPILED.
512 (format stream "~@:_Source file: ~A" namestring))
513 ((sb-di:debug-source-form source)
514 (format stream "~@:_Source form:~@:_ ~S"
515 (sb-di:debug-source-form source)))))))))
517 (let ((source (sb-eval:interpreted-function-source-location function)))
519 (let ((namestring (sb-c:definition-source-location-namestring source)))
521 (format stream "~@:_Source file: ~A" namestring)))))))
523 (defun describe-function (name function stream)
524 (let ((name (if function (fun-name function) name)))
525 (if (not (or function (and (legal-fun-name-p name) (fboundp name))))
526 ;; Not defined, but possibly the type is declared, or we have
527 ;; compiled calls to it.
528 (when (legal-fun-name-p name)
529 (multiple-value-bind (from sure) (info :function :where-from name)
530 (when (or (eq :declared from) (and sure (eq :assumed from)))
531 (pprint-logical-block (stream nil)
532 (format stream "~%~A names an undefined function" name)
533 (pprint-indent :block 2 stream)
534 (format stream "~@:_~:(~A~) type: ~S"
536 (type-specifier (info :function :type name)))))))
538 (multiple-value-bind (fun what lambda-list ftype from inline
540 (cond ((and (not function) (symbolp name) (special-operator-p name))
541 (let ((fun (symbol-function name)))
542 (values fun "a special operator" (%fun-lambda-list fun))))
543 ((and (not function) (symbolp name) (macro-function name))
544 (let ((fun (macro-function name)))
545 (values fun "a macro" (%fun-lambda-list fun))))
547 (let ((fun (or function (fdefinition name))))
548 (multiple-value-bind (ftype from)
550 (values (%fun-type function) "Derived")
551 (let ((ctype (info :function :type name)))
552 (values (when ctype (type-specifier ctype))
554 ;; Ensure lazy pickup of information
556 (sb-c::maybe-update-info-for-gf name)
557 (ecase (info :function :where-from name)
558 (:declared "Declared")
559 ;; This is hopefully clearer to users
560 ((:defined-method :defined) "Derived"))))))
561 (if (typep fun 'generic-function)
564 (sb-mop:generic-function-lambda-list fun)
568 (or (sb-mop:generic-function-methods fun)
571 (if (compiled-function-p fun)
572 "a compiled function"
573 "an interpreted function")
574 (%fun-lambda-list fun)
579 (info :function :inlinep name)
580 (info :function :inline-expansion-designator name)))))))))
581 (pprint-logical-block (stream nil)
583 (format stream "~%~A names ~A:" name what)
584 (pprint-indent :block 2 stream))
585 (describe-lambda-list lambda-list stream)
586 (when (and ftype from)
587 (format stream "~@:_~A type: ~S" from ftype))
588 (describe-documentation name 'function stream)
590 (format stream "~@:_Inline proclamation: ~A (~:[no ~;~]inline expansion available)"
594 (format stream "~@:_Method-combination: ~S"
595 (sb-pcl::method-combination-type-name
596 (sb-pcl:generic-function-method-combination fun)))
597 (cond ((eq :none methods)
598 (format stream "~@:_No methods."))
600 (pprint-newline :mandatory stream)
601 (pprint-logical-block (stream nil)
602 (format stream "Methods:")
603 (dolist (method methods)
604 (pprint-indent :block 2 stream)
605 (format stream "~@:_(~A ~{~S ~}~:S)"
607 (method-qualifiers method)
608 (sb-pcl::unparse-specializers fun (sb-mop:method-specializers method)))
609 (pprint-indent :block 4 stream)
610 (describe-documentation method t stream nil))))))
611 (describe-function-source fun stream)
614 (awhen (and (legal-fun-name-p name) (compiler-macro-function name))
615 (pprint-logical-block (stream nil)
616 (format stream "~@:_~A has a compiler-macro:" name)
617 (pprint-indent :block 2 stream)
618 (describe-documentation it t stream)
619 (describe-function-source it stream))
621 (when (and (consp name) (eq 'setf (car name)) (not (cddr name)))
622 (let* ((name2 (second name))
623 (inverse (info :setf :inverse name2))
624 (expander (info :setf :expander name2)))
626 (pprint-logical-block (stream nil)
627 (format stream "~&~A has setf-expansion: ~S"
629 (pprint-indent :block 2 stream)
630 (describe-documentation name2 'setf stream))
633 (pprint-logical-block (stream nil)
634 (format stream "~&~A has a complex setf-expansion:"
636 (pprint-indent :block 2 stream)
637 (describe-documentation name2 'setf stream t))
640 (describe-function `(setf ,name) nil stream))))