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