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