Remove some test noise. A drop in the ocean unfortunately.
[sbcl.git] / src / pcl / documentation.lisp
blob299d13e7c5127f9120f0b04b3ffbb3af94caf619
1 ;;;; implementation of CL:DOCUMENTATION
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
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.
9 (in-package "SB-PCL")
11 (defun fun-doc (x)
12 (if (typep x 'generic-function)
13 (slot-value x '%documentation)
14 (%fun-doc x)))
16 (defun (setf fun-doc) (new-value x)
17 (if (typep x 'generic-function)
18 (setf (slot-value x '%documentation) new-value)
19 (setf (%fun-doc x) new-value)))
21 ;;; FIXME: There's already fun-name in code/describe.lisp, but it's
22 ;;; loaded after PCL, so it cannot be used, because we set
23 ;;; some documentation at the end of this file.
24 (defun fun-name (x)
25 (if (typep x 'generic-function)
26 (sb-pcl:generic-function-name x)
27 (%fun-name x)))
29 (defun real-function-name (name)
30 ;; Resolve the actual name of the function named by NAME
31 ;; e.g. (setf (name-function 'x) #'car)
32 ;; (real-function-name 'x) => CAR
33 (cond ((not (fboundp name))
34 nil)
35 ((and (symbolp name)
36 (special-operator-p name))
37 (%fun-name (fdefinition name)))
38 ((and (symbolp name)
39 (macro-function name))
40 (let ((name (%fun-name (macro-function name))))
41 (and (consp name)
42 (eq (car name) 'macro-function)
43 (cadr name))))
45 (fun-name (fdefinition name)))))
47 (defun ignore-nil-doc (type)
48 (style-warn "Ignoring documentation of type ~a for ~a."
49 type nil))
51 (defun set-function-name-documentation (name documentation)
52 (cond ((not name)
53 (ignore-nil-doc 'function))
54 ((not (legal-fun-name-p name))
55 nil)
56 ((not (equal (real-function-name name) name))
57 (setf (random-documentation name 'function) documentation))
59 (setf (fun-doc (or (and (symbolp name)
60 (macro-function name))
61 (fdefinition name)))
62 documentation)))
63 documentation)
65 ;;; functions, macros, and special forms
66 (defmethod documentation ((x function) (doc-type (eql 't)))
67 (fun-doc x))
69 (defmethod documentation ((x function) (doc-type (eql 'function)))
70 (fun-doc x))
72 (defmethod documentation ((x list) (doc-type (eql 'compiler-macro)))
73 (awhen (compiler-macro-function x)
74 (documentation it t)))
76 (defmethod documentation ((x list) (doc-type (eql 'function)))
77 (when (legal-fun-name-p x)
78 (or (random-documentation x 'function)
79 (when (fboundp x)
80 (fun-doc (fdefinition x))))))
82 (defmethod documentation ((x symbol) (doc-type (eql 'function)))
83 (when (legal-fun-name-p x)
84 (or (random-documentation x 'function)
85 ;; Nothing under the name, check the function object.
86 (when (fboundp x)
87 (fun-doc (if (special-operator-p x)
88 (fdefinition x)
89 (or (macro-function x)
90 (fdefinition x))))))))
92 (defmethod documentation ((x symbol) (doc-type (eql 'compiler-macro)))
93 (awhen (compiler-macro-function x)
94 (documentation it t)))
96 (defmethod documentation ((x symbol) (doc-type (eql 'setf)))
97 (fdocumentation x 'setf))
99 (defmethod documentation ((x symbol) (doc-type (eql 'optimize)))
100 (random-documentation x 'optimize))
102 (defmethod (setf documentation) (new-value (x function) (doc-type (eql 't)))
103 (setf (fun-doc x) new-value))
105 (defmethod (setf documentation) (new-value (x function) (doc-type (eql 'function)))
106 (setf (fun-doc x) new-value))
108 (defmethod (setf documentation) (new-value (x list) (doc-type (eql 'function)))
109 (set-function-name-documentation x new-value))
111 (defmethod (setf documentation) (new-value (x list) (doc-type (eql 'compiler-macro)))
112 (awhen (compiler-macro-function x)
113 (setf (documentation it t) new-value)))
115 (defmethod (setf documentation) (new-value (x symbol) (doc-type (eql 'function)))
116 (set-function-name-documentation x new-value))
118 (defmethod (setf documentation) (new-value (x symbol) (doc-type (eql 'compiler-macro)))
119 (cond (x
120 (awhen (compiler-macro-function x)
121 (setf (documentation it t) new-value)))
123 (ignore-nil-doc 'compiler-macro)
124 new-value)))
126 (defmethod (setf documentation) (new-value (x symbol) (doc-type (eql 'setf)))
127 (cond (x
128 (setf (fdocumentation x 'setf) new-value))
130 (ignore-nil-doc 'setf)
131 new-value)))
133 ;;; method combinations
134 (defmethod documentation ((x method-combination) (doc-type (eql 't)))
135 (slot-value x '%documentation))
137 (defmethod documentation
138 ((x method-combination) (doc-type (eql 'method-combination)))
139 (slot-value x '%documentation))
141 (defmethod documentation ((x symbol) (doc-type (eql 'method-combination)))
142 (random-documentation x 'method-combination))
144 (defmethod (setf documentation)
145 (new-value (x method-combination) (doc-type (eql 't)))
146 (setf (slot-value x '%documentation) new-value))
148 (defmethod (setf documentation)
149 (new-value (x method-combination) (doc-type (eql 'method-combination)))
150 (setf (slot-value x '%documentation) new-value))
152 (defmethod (setf documentation)
153 (new-value (x symbol) (doc-type (eql 'method-combination)))
154 (cond (x
155 (setf (random-documentation x 'method-combination) new-value))
157 (ignore-nil-doc 'method-combination)
158 new-value)))
160 ;;; methods
161 (defmethod documentation ((x standard-method) (doc-type (eql 't)))
162 (slot-value x '%documentation))
164 (defmethod (setf documentation)
165 (new-value (x standard-method) (doc-type (eql 't)))
166 (setf (slot-value x '%documentation) new-value))
168 ;;; packages
170 ;;; KLUDGE: It's nasty having things like this accessor
171 ;;; (PACKAGE-DOC-STRING) floating around out in this mostly-unrelated
172 ;;; source file. Perhaps it would be better to support WARM-INIT-FORMS
173 ;;; by analogy with the existing !COLD-INIT-FORMS and have them be
174 ;;; EVAL'ed after basic warm load is done? That way things like this
175 ;;; could be defined alongside the other code which does low-level
176 ;;; hacking of packages.. -- WHN 19991203
178 (defmethod documentation ((x package) (doc-type (eql 't)))
179 (package-doc-string x))
181 (defmethod (setf documentation) (new-value (x package) (doc-type (eql 't)))
182 (setf (package-doc-string x) new-value))
184 ;;; types, classes, and structure names
185 (defmethod documentation ((x structure-class) (doc-type (eql 't)))
186 (fdocumentation (class-name x) 'type))
188 (defmethod documentation ((x structure-class) (doc-type (eql 'type)))
189 (fdocumentation (class-name x) 'type))
191 (defmethod documentation ((x class) (doc-type (eql 't)))
192 (slot-value x '%documentation))
194 (defmethod documentation ((x class) (doc-type (eql 'type))) ; TODO setf
195 (slot-value x '%documentation))
197 (defmethod (setf documentation) (new-value
198 (x class)
199 (doc-type (eql 't)))
200 (setf (slot-value x '%documentation) new-value))
202 (defmethod (setf documentation) (new-value
203 (x class)
204 (doc-type (eql 'type)))
205 (setf (slot-value x '%documentation) new-value))
207 ;;; although the CLHS doesn't mention this, it is reasonable to assume
208 ;;; that parallel treatment of condition-class was intended (if
209 ;;; condition-class is in fact not implemented as a standard-class or
210 ;;; structure-class).
211 (defmethod documentation ((x condition-class) (doc-type (eql 't)))
212 (fdocumentation (class-name x) 'type))
214 (defmethod documentation ((x condition-class) (doc-type (eql 'type)))
215 (fdocumentation (class-name x) 'type))
217 (defmethod documentation ((x symbol) (doc-type (eql 'type)))
218 (or (fdocumentation x 'type)
219 (let ((class (find-class x nil)))
220 (when class
221 (slot-value class '%documentation)))))
223 (defmethod documentation ((x symbol) (doc-type (eql 'structure)))
224 (fdocumentation x 'structure))
226 (defmethod (setf documentation) (new-value
227 (x structure-class)
228 (doc-type (eql 't)))
229 (setf (fdocumentation (class-name x) 'type) new-value))
231 (defmethod (setf documentation) (new-value
232 (x structure-class)
233 (doc-type (eql 'type)))
234 (setf (fdocumentation (class-name x) 'type) new-value))
236 (defmethod (setf documentation) (new-value
237 (x condition-class)
238 (doc-type (eql 't)))
239 (setf (fdocumentation (class-name x) 'type) new-value))
241 (defmethod (setf documentation) (new-value
242 (x condition-class)
243 (doc-type (eql 'type)))
244 (setf (fdocumentation (class-name x) 'type) new-value))
246 (defmethod (setf documentation) (new-value (x symbol) (doc-type (eql 'type)))
247 (if (or (structure-type-p x) (condition-type-p x))
248 (setf (fdocumentation x 'type) new-value)
249 (let ((class (find-class x nil)))
250 (if class
251 (setf (slot-value class '%documentation) new-value)
252 (setf (fdocumentation x 'type) new-value)))))
254 (defmethod (setf documentation) (new-value
255 (x symbol)
256 (doc-type (eql 'structure)))
257 (cond (x
258 (setf (fdocumentation x 'structure) new-value))
260 (ignore-nil-doc 'structure)
261 new-value)))
263 ;;; variables
264 (defmethod documentation ((x symbol) (doc-type (eql 'variable)))
265 (fdocumentation x 'variable))
267 (defmethod (setf documentation) (new-value
268 (x symbol)
269 (doc-type (eql 'variable)))
270 (cond (x
271 (setf (fdocumentation x 'variable) new-value))
273 (ignore-nil-doc 'variable)
274 new-value)))
276 ;;; default if DOC-TYPE doesn't match one of the specified types
277 (defmethod documentation (object doc-type)
278 (warn "unsupported DOCUMENTATION: type ~S for object of type ~S"
279 doc-type
280 (type-of object))
281 nil)
283 ;;; default if DOC-TYPE doesn't match one of the specified types
284 (defmethod (setf documentation) (new-value object doc-type)
285 ;; CMU CL made this an error, but since ANSI says that even for supported
286 ;; doc types an implementation is permitted to discard docs at any time
287 ;; for any reason, this feels to me more like a warning. -- WHN 19991214
288 (warn "discarding unsupported DOCUMENTATION of type ~S for object ~S"
289 doc-type
290 (type-of object))
291 new-value)
293 ;;; extra-standard methods, for getting at slot documentation
294 (defmethod documentation ((slotd standard-slot-definition) (doc-type (eql 't)))
295 (declare (ignore doc-type))
296 (slot-value slotd '%documentation))
298 (defmethod (setf documentation)
299 (new-value (slotd standard-slot-definition) (doc-type (eql 't)))
300 (declare (ignore doc-type))
301 (setf (slot-value slotd '%documentation) new-value))
303 ;;; Now that we have created the machinery for setting documentation, we can
304 ;;; set the documentation for the machinery for setting documentation.
305 #+sb-doc
306 (setf (documentation 'documentation 'function)
307 "Return the documentation string of Doc-Type for X, or NIL if none
308 exists. System doc-types are VARIABLE, FUNCTION, STRUCTURE, TYPE, SETF, and T.
310 Function documentation is stored separately for function names and objects:
311 DEFUN, LAMBDA, &co create function objects with the specified documentation
312 strings.
314 \(SETF (DOCUMENTATION NAME 'FUNCTION) STRING)
316 sets the documentation string stored under the specified name, and
318 \(SETF (DOCUMENTATION FUNC T) STRING)
320 sets the documentation string stored in the function object.
322 \(DOCUMENTATION NAME 'FUNCTION)
324 returns the documentation stored under the function name if any, and
325 falls back on the documentation in the function object if necessary.")