c835264a1ef62004bf23faa84fdac62d5eda7542
[sbcl.git] / src / compiler / info-functions.lisp
1 ;;;; miscellaneous functions which use INFO
2 ;;;;
3 ;;;; (In CMU CL, these were in globaldb.lisp. They've been moved here
4 ;;;; because references to INFO can't be compiled correctly until
5 ;;;; globaldb initialization is complete, and the SBCL technique for
6 ;;;; initializing the global database in the cross-compiler isn't
7 ;;;; completed until load time.)
8
9 ;;;; This software is part of the SBCL system. See the README file for
10 ;;;; more information.
11 ;;;;
12 ;;;; This software is derived from the CMU CL system, which was
13 ;;;; written at Carnegie Mellon University and released into the
14 ;;;; public domain. The software is in the public domain and is
15 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
16 ;;;; files for more information.
17
18 (in-package "SB!C")
19 \f
20 ;;;; internal utilities defined in terms of INFO
21
22 ;;; Check that NAME is a valid function name, returning the name if
23 ;;; OK, and signalling an error if not. In addition to checking for
24 ;;; basic well-formedness, we also check that symbol names are not NIL
25 ;;; or the name of a special form.
26 (defun check-fun-name (name)
27   (typecase name
28     (list
29      (unless (legal-fun-name-p name)
30        (compiler-error "illegal function name: ~S" name)))
31     (symbol
32      (when (eq (info :function :kind name) :special-form)
33        (compiler-error "Special form is an illegal function name: ~S" name)))
34     (t
35      (compiler-error "illegal function name: ~S" name)))
36   (values))
37
38 ;;; Record a new function definition, and check its legality.
39 (defun proclaim-as-fun-name (name)
40
41   ;; legal name?
42   (check-fun-name name)
43
44   ;; scrubbing old data I: possible collision with old definition
45   (when (fboundp name)
46     (ecase (info :function :kind name)
47       (:function) ; happy case
48       ((nil)) ; another happy case
49       (:macro ; maybe-not-so-good case
50        (compiler-style-warn "~S was previously defined as a macro." name)
51        (setf (info :function :where-from name) :assumed)
52        (clear-info :function :macro-function name))))
53
54   ;; scrubbing old data II: dangling forward references
55   ;;
56   ;; (This could happen if someone executes PROCLAIM FTYPE at
57   ;; macroexpansion time, which is bad style, or at compile time, e.g.
58   ;; in EVAL-WHEN (:COMPILE) inside something like DEFSTRUCT, in which
59   ;; case it's reasonable style. Either way, NAME is no longer a free
60   ;; function.)
61   (when (boundp '*free-funs*) ; when compiling
62     (remhash name *free-funs*))
63
64   ;; recording the ordinary case
65   (setf (info :function :kind name) :function)
66   (note-if-setf-fun-and-macro name)
67
68   (values))
69
70 ;;; This is called to do something about SETF functions that overlap
71 ;;; with SETF macros. Perhaps we should interact with the user to see
72 ;;; whether the macro should be blown away, but for now just give a
73 ;;; warning. Due to the weak semantics of the (SETF FUNCTION) name, we
74 ;;; can't assume that they aren't just naming a function (SETF FOO)
75 ;;; for the heck of it. NAME is already known to be well-formed.
76 (defun note-if-setf-fun-and-macro (name)
77   (when (consp name)
78     (when (or (info :setf :inverse name)
79               (info :setf :expander name))
80       (compiler-style-warn
81        "defining as a SETF function a name that already has a SETF macro:~
82        ~%  ~S"
83        name)))
84   (values))
85
86 ;;; Make NAME no longer be a function name: clear everything back to
87 ;;; the default.
88 (defun undefine-fun-name (name)
89   (when name
90     (macrolet ((frob (type &optional val)
91                  `(unless (eq (info :function ,type name) ,val)
92                     (setf (info :function ,type name) ,val))))
93       (frob :info)
94       (frob :type (specifier-type 'function))
95       (frob :where-from :assumed)
96       (frob :inlinep)
97       (frob :kind)
98       (frob :inline-expansion-designator)
99       (frob :source-transform)
100       (frob :assumed-type)))
101   (values))
102
103 ;;; part of what happens with DEFUN, also with some PCL stuff: Make
104 ;;; NAME known to be a function definition.
105 (defun become-defined-fun-name (name)
106   (proclaim-as-fun-name name)
107   (when (eq (info :function :where-from name) :assumed)
108     (setf (info :function :where-from name) :defined)
109     (if (info :function :assumed-type name)
110         (setf (info :function :assumed-type name) nil))))
111
112 ;;; Decode any raw (INFO :FUNCTION :INLINE-EXPANSION-DESIGNATOR FUN-NAME)
113 ;;; value into a lambda expression, or return NIL if there is none.
114 (declaim (ftype (function ((or symbol cons)) list) fun-name-inline-expansion))
115 (defun fun-name-inline-expansion (fun-name)
116   (let ((info (info :function :inline-expansion-designator fun-name)))
117     (if (functionp info)
118         (funcall info)
119         info)))
120 \f
121 ;;;; ANSI Common Lisp functions which are defined in terms of the info
122 ;;;; database
123
124 (defun sb!xc:constantp (object &optional environment)
125   #!+sb-doc
126   "True of any Lisp object that has a constant value: types that eval to
127   themselves, keywords, constants, and list whose car is QUOTE."
128   ;; FIXME: Someday it would be nice to make the code recognize foldable
129   ;; functions and call itself recursively on their arguments, so that
130   ;; more of the examples in the ANSI CL definition are recognized.
131   ;; (e.g. (+ 3 2), (SQRT PI), and (LENGTH '(A B C)))
132   (declare (ignore environment))
133   (typecase object
134     ;; (Note that the following test on INFO catches KEYWORDs as well as
135     ;; explicitly DEFCONSTANT symbols.)
136     (symbol (eq (info :variable :kind object) :constant))
137     (list (and (eq (car object) 'quote)
138                (consp (cdr object))))
139     (t t)))
140
141 (defun constant-form-value (form)
142   (typecase form
143     (symbol (info :variable :constant-value form))
144     ((cons (eql quote) cons)
145      (second form))
146     (t form)))
147
148 (defun sb!xc:macro-function (symbol &optional env)
149   #!+sb-doc
150   "If SYMBOL names a macro in ENV, returns the expansion function,
151    else returns NIL. If ENV is unspecified or NIL, use the global
152    environment only."
153   (declare (symbol symbol))
154   (let* ((fenv (when env (sb!c::lexenv-funs env)))
155          (local-def (cdr (assoc symbol fenv))))
156     (cond (local-def
157            (if (and (consp local-def) (eq (car local-def) 'macro))
158                (cdr local-def)
159                nil))
160           ((eq (info :function :kind symbol) :macro)
161            (values (info :function :macro-function symbol)))
162           (t
163            nil))))
164
165 (defun (setf sb!xc:macro-function) (function symbol &optional environment)
166   (declare (symbol symbol) (type function function))
167   (when environment
168     ;; Note: Technically there could be an ENV optional argument to SETF
169     ;; MACRO-FUNCTION, but since ANSI says that the consequences of
170     ;; supplying a non-nil one are undefined, we don't allow it.
171     ;; (Thus our implementation of this unspecified behavior is to
172     ;; complain. SInce the behavior is unspecified, this is conforming.:-)
173     (error "Non-NIL environment argument in SETF of MACRO-FUNCTION ~S: ~S"
174            symbol environment))
175   (when (eq (info :function :kind symbol) :special-form)
176     (error "~S names a special form." symbol))
177   (setf (info :function :kind symbol) :macro)
178   (setf (info :function :macro-function symbol) function)
179   ;; This is a nice thing to have in the target SBCL, but in the
180   ;; cross-compilation host it's not nice to mess with
181   ;; (SYMBOL-FUNCTION FOO) where FOO might be a symbol in the
182   ;; cross-compilation host's COMMON-LISP package.
183   #-sb-xc-host
184   (setf (symbol-function symbol)
185         (lambda (&rest args)
186           (declare (ignore args))
187           ;; (ANSI specification of FUNCALL says that this should be
188           ;; an error of type UNDEFINED-FUNCTION, not just SIMPLE-ERROR.)
189           (error 'undefined-function :name symbol)))
190   function)
191
192 (defun sb!xc:compiler-macro-function (name &optional env)
193   #!+sb-doc
194   "If NAME names a compiler-macro in ENV, return the expansion function, else
195    return NIL. Can be set with SETF when ENV is NIL."
196   (declare (ignore env))
197   (legal-fun-name-or-type-error name)
198   ;; Note: CMU CL used to return NIL here when a NOTINLINE declaration
199   ;; was in force. That's fairly logical, given the specified effect
200   ;; of NOTINLINE declarations on compiler-macro expansion. However,
201   ;; (1) it doesn't seem to be consistent with the ANSI spec for
202   ;; COMPILER-MACRO-FUNCTION, and (2) it would give surprising
203   ;; behavior for (SETF (COMPILER-MACRO-FUNCTION FOO) ...) in the
204   ;; presence of a (PROCLAIM '(NOTINLINE FOO)). So we don't do it.
205   (values (info :function :compiler-macro-function name)))
206 (defun (setf sb!xc:compiler-macro-function) (function name &optional env)
207   (declare (type (or symbol list) name)
208            (type (or function null) function))
209   (when env
210     ;; ANSI says this operation is undefined.
211     (error "can't SETF COMPILER-MACRO-FUNCTION when ENV is non-NIL"))
212   (when (eq (info :function :kind name) :special-form)
213     (error "~S names a special form." name))
214   (with-single-package-locked-error
215       (:symbol name "setting the compiler-macro-function of ~A")
216     (setf (info :function :compiler-macro-function name) function)
217     function))
218 \f
219 ;;;; a subset of DOCUMENTATION functionality for bootstrapping
220
221 ;;; FDOCUMENTATION is like DOCUMENTATION, but with less functionality,
222 ;;; and implemented with DEFUN instead of DEFGENERIC so that it can
223 ;;; run before CLOS is set up. Supported DOC-TYPE values are
224 ;;;   FUNCTION
225 ;;;   SETF
226 ;;;   STRUCTURE
227 ;;;   T
228 ;;;   TYPE
229 ;;;   VARIABLE
230 ;;; FIXME: Other types end up in INFO :RANDOM-DOCUMENTATION :STUFF. I
231 ;;; should add some code to monitor this and make sure that nothing is
232 ;;; unintentionally being sent to never never land this way.
233 ;;; FIXME: Rename FDOCUMENTATION to BDOCUMENTATION, by analogy with
234 ;;; DEF!STRUCT and DEF!MACRO and so forth. And consider simply saving
235 ;;; all the BDOCUMENTATION entries in a *BDOCUMENTATION* hash table
236 ;;; and slamming them into PCL once PCL gets going.
237 (defun fdocumentation (x doc-type)
238   (flet ((try-cmucl-random-doc (x doc-type)
239            (declare (symbol doc-type))
240            (cdr (assoc doc-type
241                        (values (info :random-documentation :stuff x))))))
242     (case doc-type
243       (variable
244        (typecase x
245          (symbol (values (info :variable :documentation x)))))
246       (function
247        (cond ((functionp x)
248               (%fun-doc x))
249              ((legal-fun-name-p x)
250               ;; FIXME: Is it really right to make
251               ;; (DOCUMENTATION '(SETF FOO) 'FUNCTION) equivalent to
252               ;; (DOCUMENTATION 'FOO 'FUNCTION)? That's what CMU CL
253               ;; did, so we do it, but I'm not sure it's what ANSI wants.
254               (values (info :function :documentation
255                             (fun-name-block-name x))))))
256       (structure
257        (typecase x
258          (symbol (when (eq (info :type :kind x) :instance)
259                    (values (info :type :documentation x))))))
260       (type
261        (typecase x
262          (structure-class (values (info :type :documentation (class-name x))))
263          (t (and (typep x 'symbol) (values (info :type :documentation x))))))
264       (setf (values (info :setf :documentation x)))
265       ((t)
266        (typecase x
267          (function (%fun-doc x))
268          (package (package-doc-string x))
269          (structure-class (values (info :type :documentation (class-name x))))
270          (symbol (try-cmucl-random-doc x doc-type))))
271       (t
272        (typecase x
273          ;; FIXME: This code comes from CMU CL, but
274          ;; TRY-CMUCL-RANDOM-DOC doesn't seem to be defined anywhere
275          ;; in CMU CL. Perhaps it could be defined by analogy with the
276          ;; corresponding SETF FDOCUMENTATION code.
277          (symbol (try-cmucl-random-doc x doc-type)))))))
278 (defun (setf fdocumentation) (string name doc-type)
279   ;; FIXME: I think it should be possible to set documentation for
280   ;; things (e.g. compiler macros) named (SETF FOO). fndb.lisp
281   ;; declares DOC-TYPE to be a SYMBOL, which contradicts that. What
282   ;; should be done?
283   (case doc-type
284     (variable (setf (info :variable :documentation name) string))
285     (function (setf (info :function :documentation name) string))
286     (structure (if (eq (info :type :kind name) :instance)
287                    (setf (info :type :documentation name) string)
288                    (error "~S is not the name of a structure type." name)))
289     (type (setf (info :type :documentation name) string))
290     (setf (setf (info :setf :documentation name) string))
291     (t
292      (let ((pair (assoc doc-type (info :random-documentation :stuff name))))
293        (if pair
294            (setf (cdr pair) string)
295            (push (cons doc-type string)
296                  (info :random-documentation :stuff name))))))
297   string)