1 ;;;; implementation of CL:DOCUMENTATION
3 ;;;; This software is part of the SBCL system. See the README file for
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.
12 (if (typep x 'generic-function)
13 (slot-value x '%documentation)
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.
25 (if (typep x 'generic-function)
26 (sb-pcl:generic-function-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))
36 (special-operator-p name))
37 (%fun-name (fdefinition name)))
39 (macro-function name))
40 (let ((name (%fun-name (macro-function name))))
42 (eq (car name) 'macro-function)
45 (fun-name (fdefinition name)))))
47 (defun ignore-nil-doc (type)
48 (style-warn "Ignoring documentation of type ~a for ~a."
51 (defun set-function-name-documentation (name documentation)
53 (ignore-nil-doc 'function))
54 ((not (legal-fun-name-p name))
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))
65 ;;; functions, macros, and special forms
66 (defmethod documentation ((x function) (doc-type (eql 't)))
69 (defmethod documentation ((x function) (doc-type (eql 'function)))
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)
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.
87 (fun-doc (if (special-operator-p 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)))
120 (awhen (compiler-macro-function x)
121 (setf (documentation it t) new-value)))
123 (ignore-nil-doc 'compiler-macro)
126 (defmethod (setf documentation) (new-value (x symbol) (doc-type (eql 'setf)))
128 (setf (fdocumentation x 'setf) new-value))
130 (ignore-nil-doc 'setf)
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)))
155 (setf (random-documentation x 'method-combination) new-value))
157 (ignore-nil-doc 'method-combination)
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))
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 standard-class) (doc-type (eql 't)))
192 (slot-value x '%documentation))
194 (defmethod documentation ((x standard-class) (doc-type (eql 'type)))
195 (slot-value x '%documentation))
197 ;;; although the CLHS doesn't mention this, it is reasonable to assume
198 ;;; that parallel treatment of condition-class was intended (if
199 ;;; condition-class is in fact not implemented as a standard-class or
200 ;;; structure-class).
201 (defmethod documentation ((x condition-class) (doc-type (eql 't)))
202 (fdocumentation (class-name x) 'type))
204 (defmethod documentation ((x condition-class) (doc-type (eql 'type)))
205 (fdocumentation (class-name x) 'type))
207 (defmethod documentation ((x symbol) (doc-type (eql 'type)))
208 (or (fdocumentation x 'type)
209 (let ((class (find-class x nil)))
211 (slot-value class '%documentation)))))
213 (defmethod documentation ((x symbol) (doc-type (eql 'structure)))
214 (fdocumentation x 'structure))
216 (defmethod (setf documentation) (new-value
219 (setf (fdocumentation (class-name x) 'type) new-value))
221 (defmethod (setf documentation) (new-value
223 (doc-type (eql 'type)))
224 (setf (fdocumentation (class-name x) 'type) new-value))
226 (defmethod (setf documentation) (new-value
229 (setf (slot-value x '%documentation) new-value))
231 (defmethod (setf documentation) (new-value
233 (doc-type (eql 'type)))
234 (setf (slot-value x '%documentation) new-value))
236 (defmethod (setf documentation) (new-value
239 (setf (fdocumentation (class-name x) 'type) new-value))
241 (defmethod (setf documentation) (new-value
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)))
251 (setf (slot-value class '%documentation) new-value)
252 (setf (fdocumentation x 'type) new-value)))))
254 (defmethod (setf documentation) (new-value
256 (doc-type (eql 'structure)))
258 (setf (fdocumentation x 'structure) new-value))
260 (ignore-nil-doc 'structure)
264 (defmethod documentation ((x symbol) (doc-type (eql 'variable)))
265 (fdocumentation x 'variable))
267 (defmethod (setf documentation) (new-value
269 (doc-type (eql 'variable)))
271 (setf (fdocumentation x 'variable) new-value))
273 (ignore-nil-doc 'variable)
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"
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"
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.
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
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.")