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