1.0.7.26: asymptotically faster FIND-SLOT-DEFINITION
[sbcl.git] / src / pcl / documentation.lisp
1 ;;;; implementation of CL:DOCUMENTATION
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5
6 ;;;; This software is in the public domain and is provided with absolutely no
7 ;;;; warranty. See the COPYING and CREDITS files for more information.
8
9 (in-package "SB-PCL")
10
11 ;;; FIXME: Lots of bare calls to INFO here could be handled
12 ;;; more cleanly by calling the FDOCUMENTATION function instead.
13
14 (defun fun-doc (x)
15   (etypecase x
16     (generic-function
17      (slot-value x '%documentation))
18     #+sb-eval
19     (sb-eval:interpreted-function
20      (sb-eval:interpreted-function-documentation x))
21     (function
22      (%fun-doc x))))
23
24 ;;; functions, macros, and special forms
25 (defmethod documentation ((x function) (doc-type (eql 't)))
26   (fun-doc x))
27
28 (defmethod documentation ((x function) (doc-type (eql 'function)))
29   (fun-doc x))
30
31 (defmethod documentation ((x list) (doc-type (eql 'function)))
32   (and (legal-fun-name-p x)
33        (fboundp x)
34        (documentation (fdefinition x) t)))
35
36 (defmethod documentation ((x list) (doc-type (eql 'compiler-macro)))
37   (random-documentation x 'compiler-macro))
38
39 (defmethod documentation ((x symbol) (doc-type (eql 'function)))
40   (or (values (info :function :documentation x))
41       ;; Try the pcl function documentation.
42       (and (fboundp x) (documentation (fdefinition x) t))))
43
44 (defmethod documentation ((x symbol) (doc-type (eql 'compiler-macro)))
45   (random-documentation x 'compiler-macro))
46
47 (defmethod documentation ((x symbol) (doc-type (eql 'setf)))
48   (values (info :setf :documentation x)))
49
50 (defun (setf fun-doc) (new-value x)
51   (etypecase x
52     (generic-function
53      (setf (slot-value x '%documentation) new-value))
54     #+sb-eval
55     (sb-eval:interpreted-function
56      (setf (sb-eval:interpreted-function-documentation x)
57            new-value))
58     (function
59      (let ((name (%fun-name x)))
60        (when (and name (typep name '(or symbol cons)))
61          (setf (info :function :documentation name) new-value)))))
62   new-value)
63
64
65 (defmethod (setf documentation) (new-value (x function) (doc-type (eql 't)))
66   (setf (fun-doc x) new-value))
67
68 (defmethod (setf documentation) (new-value
69                                  (x function)
70                                  (doc-type (eql 'function)))
71   (setf (fun-doc x) new-value))
72
73 (defmethod (setf documentation) (new-value (x list) (doc-type (eql 'function)))
74   (setf (info :function :documentation x) new-value))
75
76 (defmethod (setf documentation)
77     (new-value (x list) (doc-type (eql 'compiler-macro)))
78   (setf (random-documentation x 'compiler-macro) new-value))
79
80 (defmethod (setf documentation) (new-value
81                                  (x symbol)
82                                  (doc-type (eql 'function)))
83   (setf (info :function :documentation x) new-value))
84
85 (defmethod (setf documentation)
86     (new-value (x symbol) (doc-type (eql 'compiler-macro)))
87   (setf (random-documentation x 'compiler-macro) new-value))
88
89 (defmethod (setf documentation) (new-value (x symbol) (doc-type (eql 'setf)))
90   (setf (info :setf :documentation x) new-value))
91 \f
92 ;;; method combinations
93 (defmethod documentation ((x method-combination) (doc-type (eql 't)))
94   (slot-value x '%documentation))
95
96 (defmethod documentation
97     ((x method-combination) (doc-type (eql 'method-combination)))
98   (slot-value x '%documentation))
99
100 (defmethod documentation ((x symbol) (doc-type (eql 'method-combination)))
101   (random-documentation x 'method-combination))
102
103 (defmethod (setf documentation)
104     (new-value (x method-combination) (doc-type (eql 't)))
105   (setf (slot-value x '%documentation) new-value))
106
107 (defmethod (setf documentation)
108     (new-value (x method-combination) (doc-type (eql 'method-combination)))
109   (setf (slot-value x '%documentation) new-value))
110
111 (defmethod (setf documentation)
112     (new-value (x symbol) (doc-type (eql 'method-combination)))
113   (setf (random-documentation x 'method-combination) new-value))
114 \f
115 ;;; methods
116 (defmethod documentation ((x standard-method) (doc-type (eql 't)))
117   (slot-value x '%documentation))
118
119 (defmethod (setf documentation)
120     (new-value (x standard-method) (doc-type (eql 't)))
121   (setf (slot-value x '%documentation) new-value))
122 \f
123 ;;; packages
124
125 ;;; KLUDGE: It's nasty having things like this accessor
126 ;;; (PACKAGE-DOC-STRING) floating around out in this mostly-unrelated
127 ;;; source file. Perhaps it would be better to support WARM-INIT-FORMS
128 ;;; by analogy with the existing !COLD-INIT-FORMS and have them be
129 ;;; EVAL'ed after basic warm load is done? That way things like this
130 ;;; could be defined alongside the other code which does low-level
131 ;;; hacking of packages.. -- WHN 19991203
132
133 (defmethod documentation ((x package) (doc-type (eql 't)))
134   (package-doc-string x))
135
136 (defmethod (setf documentation) (new-value (x package) (doc-type (eql 't)))
137   (setf (package-doc-string x) new-value))
138 \f
139 ;;; types, classes, and structure names
140 (defmethod documentation ((x structure-class) (doc-type (eql 't)))
141   (values (info :type :documentation (class-name x))))
142
143 (defmethod documentation ((x structure-class) (doc-type (eql 'type)))
144   (values (info :type :documentation (class-name x))))
145
146 (defmethod documentation ((x standard-class) (doc-type (eql 't)))
147   (slot-value x '%documentation))
148
149 (defmethod documentation ((x standard-class) (doc-type (eql 'type)))
150   (slot-value x '%documentation))
151
152 ;;; although the CLHS doesn't mention this, it is reasonable to assume
153 ;;; that parallel treatment of condition-class was intended (if
154 ;;; condition-class is in fact not implemented as a standard-class or
155 ;;; structure-class).
156 (defmethod documentation ((x condition-class) (doc-type (eql 't)))
157   (values (info :type :documentation (class-name x))))
158
159 (defmethod documentation ((x condition-class) (doc-type (eql 'type)))
160   (values (info :type :documentation (class-name x))))
161
162 (defmethod documentation ((x symbol) (doc-type (eql 'type)))
163   (or (values (info :type :documentation x))
164       (let ((class (find-class x nil)))
165         (when class
166           (slot-value class '%documentation)))))
167
168 (defmethod documentation ((x symbol) (doc-type (eql 'structure)))
169   (cond
170     ((structure-type-p x)
171      (values (info :type :documentation x)))
172     ((info :typed-structure :info x)
173      (values (info :typed-structure :documentation x)))
174     (t nil)))
175
176 (defmethod (setf documentation) (new-value
177                                  (x structure-class)
178                                  (doc-type (eql 't)))
179   (setf (info :type :documentation (class-name x)) new-value))
180
181 (defmethod (setf documentation) (new-value
182                                  (x structure-class)
183                                  (doc-type (eql 'type)))
184   (setf (info :type :documentation (class-name x)) new-value))
185
186 (defmethod (setf documentation) (new-value
187                                  (x standard-class)
188                                  (doc-type (eql 't)))
189   (setf (slot-value x '%documentation) new-value))
190
191 (defmethod (setf documentation) (new-value
192                                  (x standard-class)
193                                  (doc-type (eql 'type)))
194   (setf (slot-value x '%documentation) new-value))
195
196 (defmethod (setf documentation) (new-value
197                                  (x condition-class)
198                                  (doc-type (eql 't)))
199   (setf (info :type :documentation (class-name x)) new-value))
200
201 (defmethod (setf documentation) (new-value
202                                  (x condition-class)
203                                  (doc-type (eql 'type)))
204   (setf (info :type :documentation (class-name x)) new-value))
205
206 (defmethod (setf documentation) (new-value (x symbol) (doc-type (eql 'type)))
207   (if (or (structure-type-p x) (condition-type-p x))
208       (setf (info :type :documentation x) new-value)
209       (let ((class (find-class x nil)))
210         (if class
211             (setf (slot-value class '%documentation) new-value)
212             (setf (info :type :documentation x) new-value)))))
213
214 (defmethod (setf documentation) (new-value
215                                  (x symbol)
216                                  (doc-type (eql 'structure)))
217   (cond
218     ((structure-type-p x)
219      (setf (info :type :documentation x) new-value))
220     ((info :typed-structure :info x)
221      (setf (info :typed-structure :documentation x) new-value))
222     (t new-value)))
223 \f
224 ;;; variables
225 (defmethod documentation ((x symbol) (doc-type (eql 'variable)))
226   (values (info :variable :documentation x)))
227
228 (defmethod (setf documentation) (new-value
229                                  (x symbol)
230                                  (doc-type (eql 'variable)))
231   (setf (info :variable :documentation x) new-value))
232 \f
233 ;;; default if DOC-TYPE doesn't match one of the specified types
234 (defmethod documentation (object doc-type)
235   (warn "unsupported DOCUMENTATION: type ~S for object ~S"
236         doc-type
237         (type-of object))
238   nil)
239
240 ;;; default if DOC-TYPE doesn't match one of the specified types
241 (defmethod (setf documentation) (new-value object doc-type)
242   ;; CMU CL made this an error, but since ANSI says that even for supported
243   ;; doc types an implementation is permitted to discard docs at any time
244   ;; for any reason, this feels to me more like a warning. -- WHN 19991214
245   (warn "discarding unsupported DOCUMENTATION of type ~S for object ~S"
246         doc-type
247         (type-of object))
248   new-value)
249
250 ;;; extra-standard methods, for getting at slot documentation
251 (defmethod documentation ((slotd standard-slot-definition) (doc-type (eql 't)))
252   (declare (ignore doc-type))
253   (slot-value slotd '%documentation))
254
255 (defmethod (setf documentation)
256     (new-value (slotd standard-slot-definition) (doc-type (eql 't)))
257   (declare (ignore doc-type))
258   (setf (slot-value slotd '%documentation) new-value))
259 \f
260 ;;; Now that we have created the machinery for setting documentation, we can
261 ;;; set the documentation for the machinery for setting documentation.
262 #+sb-doc
263 (setf (documentation 'documentation 'function)
264       "Return the documentation string of Doc-Type for X, or NIL if
265   none exists. System doc-types are VARIABLE, FUNCTION, STRUCTURE, TYPE,
266   SETF, and T.")