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