Declaim types of %%data-vector-...%%.
[sbcl.git] / src / pcl / print-object.lisp
blob6b872de24e9592ce4f74cc8a1d6924b85e194913
1 ;;;; some basic PRINT-OBJECT functionality
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
6 ;;;; This software is derived from software originally released by Xerox
7 ;;;; Corporation. Copyright and release statements follow. Later modifications
8 ;;;; to the software are in the public domain and are provided with
9 ;;;; absolutely no warranty. See the COPYING and CREDITS files for more
10 ;;;; information.
12 ;;;; Some of the text in this file was originally taken from various files of
13 ;;;; the PCL system from Xerox Corporation, which carried the following
14 ;;;; copyright information:
15 ;;;;
16 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
17 ;;;; All rights reserved.
18 ;;;;
19 ;;;; Use and copying of this software and preparation of derivative works based
20 ;;;; upon this software are permitted. Any distribution of this software or
21 ;;;; derivative works must comply with all applicable United States export
22 ;;;; control laws.
23 ;;;;
24 ;;;; This software is made available AS IS, and Xerox Corporation makes no
25 ;;;; warranty about the software, its performance or its conformity to any
26 ;;;; specification.
28 (in-package "SB-PCL")
30 ;;;; the PRINT-OBJECT generic function
32 ;;; Blow away the old non-generic function placeholder which was used
33 ;;; by the printer doing bootstrapping, and immediately replace it
34 ;;; with some new printing logic, so that the Lisp printer stays
35 ;;; crippled only for the shortest necessary time.
36 (/show0 "about to replace placeholder PRINT-OBJECT with DEFGENERIC")
37 (let (;; (If we don't suppress /SHOW printing while the printer is
38 ;; crippled here, it becomes really easy to crash the bootstrap
39 ;; sequence by adding /SHOW statements e.g. to the compiler,
40 ;; which kinda defeats the purpose of /SHOW being a harmless
41 ;; tracing-style statement.)
42 #+sb-show (*/show* nil)
43 ;; (another workaround for the problem of debugging while the
44 ;; printer is disabled here)
45 (sb-impl::*print-object-is-disabled-p* t))
46 (fmakunbound 'print-object)
47 (defgeneric print-object (object stream))
48 (defmethod print-object ((x t) stream)
49 (if *print-pretty*
50 (pprint-logical-block (stream nil)
51 (print-unreadable-object (x stream :type t :identity t)))
52 (print-unreadable-object (x stream :type t :identity t)))))
53 (/show0 "done replacing placeholder PRINT-OBJECT with DEFGENERIC")
55 ;;;; a hook called by the printer to take care of dispatching to PRINT-OBJECT
56 ;;;; for appropriate FUNCALLABLE-INSTANCE objects
58 ;;; Now that CLOS is working, we can replace our old temporary placeholder code
59 ;;; for writing funcallable instances with permanent code:
60 (fmakunbound 'sb-impl::printed-as-funcallable-standard-class)
61 (defun sb-impl::printed-as-funcallable-standard-class (object stream)
62 (when (funcallable-standard-class-p (class-of object))
63 (print-object object stream)
64 t))
66 ;;;; PRINT-OBJECT methods for objects from PCL classes
67 ;;;;
68 ;;;; FIXME: Perhaps these should be moved back alongside the definitions of
69 ;;;; the classes they print. (Bootstrapping problems could be avoided by
70 ;;;; using DEF!METHOD to do this.)
72 (defmethod print-object ((method standard-method) stream)
73 (if (slot-boundp method '%generic-function)
74 (print-unreadable-object (method stream :type t :identity t)
75 (let ((generic-function (method-generic-function method))
76 (*print-length* 50))
77 (format stream "~:[~*~;~/sb-impl::print-symbol-with-prefix/ ~]~{~S ~}~:S"
78 generic-function
79 (and generic-function
80 (generic-function-name generic-function))
81 (method-qualifiers method)
82 (if generic-function
83 (unparse-specializers generic-function (method-specializers method))
84 (method-specializers method)))))
85 (call-next-method)))
87 (defmethod print-object ((method standard-accessor-method) stream)
88 (if (slot-boundp method '%generic-function)
89 (print-unreadable-object (method stream :type t :identity t)
90 (let ((generic-function (method-generic-function method)))
91 (format stream "~/sb-impl::print-symbol-with-prefix/, slot:~S, ~:S"
92 (and generic-function
93 (generic-function-name generic-function))
94 (accessor-method-slot-name method)
95 (if generic-function
96 (unparse-specializers generic-function (method-specializers method))
97 (method-specializers method)))))
98 (call-next-method)))
100 (defmethod print-object ((mc standard-method-combination) stream)
101 (print-unreadable-object (mc stream :type t :identity t)
102 (format stream
103 "~S ~S"
104 (slot-value-or-default mc 'type-name)
105 (slot-value-or-default mc 'options))))
107 (defun named-object-print-function (instance stream
108 &optional (properly-named-p t)
109 (extra nil extra-p))
110 (cond ((slot-boundp instance 'name) ; case (1): named
111 (let ((name (slot-value instance 'name)))
112 (print-unreadable-object
113 (instance stream :type t :identity (not properly-named-p))
114 (format stream "~/sb-impl::print-symbol-with-prefix/~:[~:; ~:S~]"
115 name extra-p extra))))
116 ((not extra-p) ; case (2): empty body to avoid an extra space
117 (print-unreadable-object (instance stream :type t :identity t)))
118 (t ; case (3). no name, but extra data - show #<unbound slot> and data
119 (print-unreadable-object (instance stream :type t :identity t)
120 (format stream "~S ~:S" *unbound-slot-value-marker* extra)))))
122 (defmethod print-object ((class class) stream)
123 ;; Use a similar concept as in OUTPUT-FUN.
124 (if (slot-boundp class 'name)
125 (let* ((name (class-name class))
126 (proper-p (and (symbolp name) (eq (find-class name nil) class))))
127 (print-unreadable-object (class stream :type t :identity (not proper-p))
128 (print-symbol-with-prefix stream name)))
129 ;; "#<CLASS #<unbound slot> {122D1141}>" is ugly. Don't show that.
130 (print-unreadable-object (class stream :type t :identity t))))
132 (defmethod print-object ((slotd slot-definition) stream)
133 (named-object-print-function slotd stream))
135 (defmethod print-object ((generic-function standard-generic-function) stream)
136 (multiple-value-call 'named-object-print-function
137 generic-function
138 stream
139 (and (slot-boundp generic-function 'name)
140 (let ((name (slot-value generic-function 'name)))
141 (and (legal-fun-name-p name)
142 (fboundp name)
143 (eq (fdefinition name) generic-function))))
144 (if (slot-boundp generic-function 'methods)
145 (list (length (generic-function-methods generic-function)))
146 (values))))
148 (defmethod print-object ((cache cache) stream)
149 (print-unreadable-object (cache stream :type t :identity t)
150 (multiple-value-bind (lines-used lines-total max-depth depth-limit)
151 (cache-statistics cache)
152 (format stream
153 "~D key~P, ~:[no value~;value~], ~D/~D lines, depth ~D/~D"
154 (cache-key-count cache)
155 (cache-key-count cache)
156 (cache-value cache)
157 lines-used
158 lines-total
159 max-depth
160 depth-limit))))
162 (defmethod print-object ((dfun-info dfun-info) stream)
163 (declare (type stream stream))
164 (print-unreadable-object (dfun-info stream :type t :identity t)))
166 (defmethod print-object ((ctor ctor) stream)
167 (print-unreadable-object (ctor stream :type t)
168 (format stream "~S ~:S" (ctor-class-or-name ctor) (ctor-initargs ctor)))
169 ctor)
171 (defmethod print-object ((obj class-precedence-description) stream)
172 (print-unreadable-object (obj stream :type t)
173 (format stream "~D" (cpd-count obj))))
175 (defmethod print-object ((self eql-specializer) stream)
176 (let ((have-obj (slot-boundp self 'object)))
177 (print-unreadable-object (self stream :type t :identity (not have-obj))
178 (when have-obj
179 (write (slot-value self 'object) :stream stream)))))