+(defmethod describe-object ((package package) stream)
+ (print-standard-describe-header package stream)
+ (pprint-logical-block (stream nil)
+ (describe-documentation package t stream)
+ (flet ((humanize (list)
+ (sort (mapcar (lambda (x)
+ (if (packagep x)
+ (package-name x)
+ x))
+ list)
+ #'string<))
+ (out (label list)
+ (describe-stuff label list stream :escape nil)))
+ (let ((implemented (humanize (package-implemented-by-list package)))
+ (implements (humanize (package-implements-list package)))
+ (nicks (humanize (package-nicknames package)))
+ (uses (humanize (package-use-list package)))
+ (used (humanize (package-used-by-list package)))
+ (shadows (humanize (package-shadowing-symbols package)))
+ (this (list (package-name package)))
+ (exports nil))
+ (do-external-symbols (ext package)
+ (push ext exports))
+ (setf exports (humanize exports))
+ (when (package-locked-p package)
+ (format stream "~@:_Locked."))
+ (when (set-difference implemented this :test #'string=)
+ (out "Implemented-by-list" implemented))
+ (when (set-difference implements this :test #'string=)
+ (out "Implements-list" implements))
+ (out "Nicknames" nicks)
+ (out "Use-list" uses)
+ (out "Used-by-list" used)
+ (out "Shadows" shadows)
+ (out "Exports" exports)
+ (format stream "~@:_~S internal symbols."
+ (package-internal-symbol-count package))))
+ (terpri stream)))
+\f
+;;;; Helpers to deal with shared functionality
+
+(defun describe-class (name class stream)
+ (let* ((by-name (not class))
+ (name (if class (class-name class) name))
+ (class (if class class (find-class name nil))))
+ (when class
+ (let ((metaclass-name (class-name (class-of class))))
+ (pprint-logical-block (stream nil)
+ (when by-name
+ (format stream "~@:_~A names the ~(~A~) ~S:"
+ name
+ metaclass-name
+ class)
+ (pprint-indent :block 2 stream))
+ (describe-documentation class t stream)
+ (when (sb-mop:class-finalized-p class)
+ (describe-stuff "Class precedence-list"
+ (mapcar #'class-name-or-class (sb-mop:class-precedence-list class))
+ stream))
+ (describe-stuff "Direct superclasses"
+ (mapcar #'class-name-or-class (sb-mop:class-direct-superclasses class))
+ stream)
+ (let ((subs (mapcar #'class-name-or-class (sb-mop:class-direct-subclasses class))))
+ (if subs
+ (describe-stuff "Direct subclasses" subs stream)
+ (format stream "~@:_No subclasses.")))
+ (unless (sb-mop:class-finalized-p class)
+ (format stream "~@:_Not yet finalized."))
+ (if (eq 'structure-class metaclass-name)
+ (let* ((dd (find-defstruct-description name))
+ (slots (dd-slots dd)))
+ (if slots
+ (format stream "~@:_Slots:~:{~@:_ ~S~
+ ~@:_ Type: ~A ~@[~A~]~
+ ~@:_ Initform: ~S~}"
+ (mapcar (lambda (dsd)
+ (list
+ (dsd-name dsd)
+ (dsd-type dsd)
+ (unless (eq t (dsd-raw-type dsd))
+ "(unboxed)")
+ (dsd-default dsd)))
+ slots))
+ (format stream "~@:_No slots.")))
+ (let ((slots (sb-mop:class-direct-slots class)))
+ (if slots
+ (format stream "~@:_Direct slots:~:{~@:_ ~S~
+ ~@[~@:_ Type: ~S~]~
+ ~@[~@:_ Allocation: ~S~]~
+ ~@[~@:_ Initargs: ~{~S~^, ~}~]~
+ ~@[~@:_ Initform: ~S~]~
+ ~@[~@:_ Readers: ~{~S~^, ~}~]~
+ ~@[~@:_ Writers: ~{~S~^, ~}~]~
+ ~@[~@:_ Documentation:~@:_ ~@<~@;~A~:>~]~}"
+ (mapcar (lambda (slotd)
+ (list (sb-mop:slot-definition-name slotd)
+ (let ((type (sb-mop:slot-definition-type slotd)))
+ (unless (eq t type) type))
+ (let ((alloc (sb-mop:slot-definition-allocation slotd)))
+ (unless (eq :instance alloc) alloc))
+ (sb-mop:slot-definition-initargs slotd)
+ (sb-mop:slot-definition-initform slotd)
+ (sb-mop:slot-definition-readers slotd)
+ (sb-mop:slot-definition-writers slotd)
+ ;; FIXME: does this get the prefix right?
+ (quiet-doc slotd t)))
+ slots))
+ (format stream "~@:_No direct slots."))))
+ (pprint-indent :block 0 stream)
+ (pprint-newline :mandatory stream))))))
+
+(defun describe-instance (object stream)
+ (let* ((class (class-of object))
+ (slotds (sb-mop:class-slots class))
+ (max-slot-name-length 0)
+ (plist nil))
+
+ ;; Figure out a good width for the slot-name column.
+ (flet ((adjust-slot-name-length (name)
+ (setf max-slot-name-length
+ (max max-slot-name-length (length (symbol-name name))))))
+ (dolist (slotd slotds)
+ (adjust-slot-name-length (sb-mop:slot-definition-name slotd))
+ (push slotd (getf plist (sb-mop:slot-definition-allocation slotd))))
+ (setf max-slot-name-length (min (+ max-slot-name-length 3) 30)))
+
+ ;; Now that we know the width, we can print.
+ (flet ((describe-slot (name value)
+ (format stream "~% ~A~VT = ~A" name max-slot-name-length
+ (prin1-to-line value))))
+ (sb-pcl::doplist (allocation slots) plist
+ (format stream "~%Slots with ~S allocation:" allocation)
+ (dolist (slotd (nreverse slots))
+ (describe-slot
+ (sb-mop:slot-definition-name slotd)
+ (sb-pcl::slot-value-or-default object (sb-mop:slot-definition-name slotd))))))
+ (unless slotds
+ (format stream "~@:_No slots."))
+ (terpri stream)))
+
+(defun quiet-doc (object type)
+ (handler-bind ((warning #'muffle-warning))
+ (documentation object type)))
+
+(defun describe-documentation (object type stream &optional undoc newline)
+ (let ((doc (quiet-doc object type)))
+ (cond (doc
+ (format stream "~@:_Documentation:~@:_")
+ (pprint-logical-block (stream nil :per-line-prefix " ")
+ (princ doc stream)))
+ (undoc
+ (format stream "~@:_(undocumented)")))
+ (when newline
+ (pprint-newline :mandatory stream))))
+
+(defun describe-stuff (label list stream &key (escape t))
+ (when list
+ (if escape
+ (format stream "~@:_~A:~@<~;~{ ~S~^,~:_~}~;~:>" label list)
+ (format stream "~@:_~A:~@<~;~{ ~A~^,~:_~}~;~:>" label list))))
+
+(defun describe-lambda-list (lambda-list stream)
+ (format stream "~@:_Lambda-list: ~:A" lambda-list))
+
+(defun describe-function-source (function stream)
+ (if (compiled-function-p function)
+ (let* ((code (fun-code-header (%fun-fun function)))
+ (info (sb-kernel:%code-debug-info code)))
+ (when info
+ (let ((source (sb-c::debug-info-source info)))
+ (when source
+ (let ((namestring (sb-c::debug-source-namestring source)))
+ ;; This used to also report the times the source was created
+ ;; and compiled, but that seems more like noise than useful
+ ;; information -- but FWIW that are to be had as
+ ;; SB-C::DEBUG-SOUCE-CREATED/COMPILED.
+ (cond (namestring
+ (format stream "~@:_Source file: ~A" namestring))
+ ((sb-di:debug-source-form source)
+ (format stream "~@:_Source form:~@:_ ~S"
+ (sb-di:debug-source-form source)))))))))
+ #+sb-eval
+ (let ((source (sb-eval:interpreted-function-source-location function)))
+ (when source
+ (let ((namestring (sb-c:definition-source-location-namestring source)))
+ (when namestring
+ (format stream "~@:_Source file: ~A" namestring)))))))
+
+(defun describe-function (name function stream)
+ (let ((name (if function (fun-name function) name)))
+ (if (not (or function (and (legal-fun-name-p name) (fboundp name))))
+ ;; Not defined, but possibly the type is declared, or we have
+ ;; compiled calls to it.
+ (when (legal-fun-name-p name)
+ (multiple-value-bind (from sure) (info :function :where-from name)
+ (when (or (eq :declared from) (and sure (eq :assumed from)))
+ (pprint-logical-block (stream nil)
+ (format stream "~%~A names an undefined function" name)
+ (pprint-indent :block 2 stream)
+ (format stream "~@:_~:(~A~) type: ~S"
+ from
+ (type-specifier (info :function :type name)))))))
+ ;; Defined.
+ (multiple-value-bind (fun what lambda-list ftype from inline
+ methods)
+ (cond ((and (not function) (symbolp name) (special-operator-p name))
+ (let ((fun (symbol-function name)))
+ (values fun "a special operator" (%fun-lambda-list fun))))
+ ((and (not function) (symbolp name) (macro-function name))
+ (let ((fun (macro-function name)))
+ (values fun "a macro" (%fun-lambda-list fun))))
+ (t
+ (let ((fun (or function (fdefinition name))))
+ (multiple-value-bind (ftype from)
+ (if function
+ (values (%fun-type function) "Derived")
+ (let ((ctype (info :function :type name)))
+ (values (when ctype (type-specifier ctype))
+ (when ctype
+ ;; Ensure lazy pickup of information
+ ;; from methods.
+ (sb-c::maybe-update-info-for-gf name)
+ (ecase (info :function :where-from name)
+ (:declared "Declared")
+ ;; This is hopefully clearer to users
+ ((:defined-method :defined) "Derived"))))))
+ (if (typep fun 'generic-function)
+ (values fun
+ "a generic function"
+ (sb-mop:generic-function-lambda-list fun)
+ ftype
+ from
+ nil
+ (or (sb-mop:generic-function-methods fun)
+ :none))
+ (values fun
+ (if (compiled-function-p fun)
+ "a compiled function"
+ "an interpreted function")
+ (%fun-lambda-list fun)
+ ftype
+ from
+ (unless function
+ (cons
+ (info :function :inlinep name)
+ (info :function :inline-expansion-designator name)))))))))
+ (pprint-logical-block (stream nil)
+ (unless function
+ (format stream "~%~A names ~A:" name what)
+ (pprint-indent :block 2 stream))
+ (describe-lambda-list lambda-list stream)
+ (when (and ftype from)
+ (format stream "~@:_~A type: ~S" from ftype))
+ (describe-documentation name 'function stream)
+ (when (car inline)
+ (format stream "~@:_Inline proclamation: ~A (~:[no ~;~]inline expansion available)"
+ (car inline)
+ (cdr inline)))
+ (when methods
+ (format stream "~@:_Method-combination: ~S"
+ (sb-pcl::method-combination-type-name
+ (sb-pcl:generic-function-method-combination fun)))
+ (cond ((eq :none methods)
+ (format stream "~@:_No methods."))
+ (t
+ (pprint-newline :mandatory stream)
+ (pprint-logical-block (stream nil)
+ (format stream "Methods:")
+ (dolist (method methods)
+ (pprint-indent :block 2 stream)
+ (format stream "~@:_(~A ~{~S ~}~:S)"
+ name
+ (method-qualifiers method)
+ (sb-pcl::unparse-specializers fun (sb-mop:method-specializers method)))
+ (pprint-indent :block 4 stream)
+ (describe-documentation method t stream nil))))))
+ (describe-function-source fun stream)
+ (terpri stream)))))
+ (unless function
+ (awhen (and (legal-fun-name-p name) (compiler-macro-function name))
+ (pprint-logical-block (stream nil)
+ (format stream "~@:_~A has a compiler-macro:" name)
+ (pprint-indent :block 2 stream)
+ (describe-documentation it t stream)
+ (describe-function-source it stream))
+ (terpri stream))
+ (when (and (consp name) (eq 'setf (car name)) (not (cddr name)))
+ (let* ((name2 (second name))
+ (inverse (info :setf :inverse name2))
+ (expander (info :setf :expander name2)))
+ (cond (inverse
+ (pprint-logical-block (stream nil)
+ (format stream "~&~A has setf-expansion: ~S"
+ name inverse)
+ (pprint-indent :block 2 stream)
+ (describe-documentation name2 'setf stream))
+ (terpri stream))
+ (expander
+ (pprint-logical-block (stream nil)
+ (format stream "~&~A has a complex setf-expansion:"
+ name)
+ (pprint-indent :block 2 stream)
+ (describe-lambda-list (%fun-lambda-list expander) stream)
+ (describe-documentation name2 'setf stream t)
+ (describe-function-source expander stream))
+ (terpri stream)))))
+ (when (symbolp name)
+ (describe-function `(setf ,name) nil stream))))