1.0.32.12: Fix slot-value on specialized parameters in SVUC methods
[sbcl.git] / src / code / target-misc.lisp
1 ;;;; Environment query functions, DOCUMENTATION and DRIBBLE.
2 ;;;;
3 ;;;; FIXME: If there are exactly three things in here, it could be
4 ;;;; exactly three files named e.g. equery.lisp, doc.lisp, and dribble.lisp.
5
6 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; more information.
8 ;;;;
9 ;;;; This software is derived from the CMU CL system, which was
10 ;;;; written at Carnegie Mellon University and released into the
11 ;;;; public domain. The software is in the public domain and is
12 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
13 ;;;; files for more information.
14
15 (in-package "SB!IMPL")
16 \f
17 ;;;; function names and documentation
18
19 ;;;; the ANSI interface to function names (and to other stuff too)
20 ;;; Note: this function gets called by the compiler (as of 1.0.17.x,
21 ;;; in MAYBE-INLINE-SYNTACTIC-CLOSURE), and so although ANSI says
22 ;;; we're allowed to return NIL here freely, it seems plausible that
23 ;;; small changes to the circumstances under which this function
24 ;;; returns non-NIL might have subtle consequences on the compiler.
25 ;;; So it might be desirable to have the compiler not rely on this
26 ;;; function, eventually.
27 (defun function-lambda-expression (fun)
28   "Return (VALUES DEFINING-LAMBDA-EXPRESSION CLOSURE-P NAME), where
29   DEFINING-LAMBDA-EXPRESSION is NIL if unknown, or a suitable argument
30   to COMPILE otherwise, CLOSURE-P is non-NIL if the function's definition
31   might have been enclosed in some non-null lexical environment, and
32   NAME is some name (for debugging only) or NIL if there is no name."
33   (declare (type function fun))
34   (etypecase fun
35     #!+sb-eval
36     (sb!eval:interpreted-function
37      (let ((name (sb!eval:interpreted-function-name fun))
38            (lambda-list (sb!eval:interpreted-function-lambda-list fun))
39            (body (sb!eval:interpreted-function-body fun)))
40        (values `(lambda ,lambda-list ,@body)
41                t name)))
42     (function
43      (let* ((fun (%simple-fun-self (%fun-fun fun)))
44             (name (%fun-name fun))
45             (code (sb!di::fun-code-header fun))
46             (info (sb!kernel:%code-debug-info code)))
47        (if info
48            (let ((source (sb!c::debug-info-source info)))
49              (cond ((and (sb!c::debug-source-form source)
50                          (eq (sb!c::debug-source-function source) fun))
51                     (values (sb!c::debug-source-form source)
52                             nil
53                             name))
54                    ((legal-fun-name-p name)
55                     (let ((exp (fun-name-inline-expansion name)))
56                       (values exp (not exp) name)))
57                    (t
58                     (values nil t name))))
59            (values nil t name))))))
60
61 ;;;; Generalizing over SIMPLE-FUN, CLOSURE, and FUNCALLABLE-INSTANCEs
62
63 ;;; Underlying SIMPLE-FUN
64 (defun %fun-fun (function)
65   (declare (function function))
66   (typecase function
67     (simple-fun
68      function)
69     (closure
70      (%closure-fun function))
71     (funcallable-instance
72      (%fun-fun (funcallable-instance-fun function)))))
73
74 (defun %fun-lambda-list (function)
75   (typecase function
76     #!+sb-eval
77     (sb!eval:interpreted-function
78      (sb!eval:interpreted-function-debug-lambda-list function))
79     (t
80      (%simple-fun-arglist (%fun-fun function)))))
81
82 (defun (setf %fun-lambda-list) (new-value function)
83   (typecase function
84     #!+sb-eval
85     (sb!eval:interpreted-function
86      (setf (sb!eval:interpreted-function-debug-lambda-list function) new-value))
87     ;; FIXME: Eliding general funcallable-instances for now.
88     ((or simple-fun closure)
89      (setf (%simple-fun-arglist (%fun-fun function)) new-value)))
90   new-value)
91
92 (defun %fun-type (function)
93   (%simple-fun-type (%fun-fun function)))
94
95 ;;; a SETFable function to return the associated debug name for FUN
96 ;;; (i.e., the third value returned from CL:FUNCTION-LAMBDA-EXPRESSION),
97 ;;; or NIL if there's none
98 (defun %fun-name (function)
99   (typecase function
100     #!+sb-eval
101     (sb!eval:interpreted-function
102      (sb!eval:interpreted-function-debug-name function))
103     (t
104      (%simple-fun-name (%fun-fun function)))))
105
106 (defun (setf %fun-name) (new-value function)
107   (typecase function
108     #!+sb-eval
109     (sb!eval:interpreted-function
110      (setf (sb!eval:interpreted-function-debug-name function) new-value))
111     ;; FIXME: Eliding general funcallable-instances for now.
112     ((or simple-fun closure)
113      (setf (%simple-fun-name (%fun-fun function)) new-value)))
114   new-value)
115
116 (defun %fun-doc (function)
117   (typecase function
118     #!+sb-eval
119     (sb!eval:interpreted-function
120      (sb!eval:interpreted-function-documentation function))
121     (t
122      (%simple-fun-doc (%fun-fun function)))))
123
124 (defun (setf %fun-doc) (new-value function)
125   (declare (type (or null string) new-value))
126   (typecase function
127     #!+sb-eval
128     (sb!eval:interpreted-function
129      (setf (sb!eval:interpreted-function-documentation function) new-value))
130     ((or simple-fun closure)
131      (setf (%simple-fun-doc (%fun-fun function)) new-value)))
132   new-value)
133 \f
134 ;;; various environment inquiries
135
136 (defvar *features* '#.sb-cold:*shebang-features*
137   #!+sb-doc
138   "a list of symbols that describe features provided by the
139    implementation")
140
141 (defun machine-instance ()
142   #!+sb-doc
143   "Return a string giving the name of the local machine."
144   #!+win32 (sb!win32::get-computer-name)
145   #!-win32 (sb!unix:unix-gethostname))
146
147 (defvar *machine-version*)
148
149 (defun machine-version ()
150   #!+sb-doc
151   "Return a string describing the version of the computer hardware we
152 are running on, or NIL if we can't find any useful information."
153   (unless (boundp '*machine-version*)
154     (setf *machine-version* (get-machine-version)))
155   *machine-version*)
156
157 ;;; FIXME: Don't forget to set these in a sample site-init file.
158 ;;; FIXME: Perhaps the functions could be SETFable instead of having the
159 ;;; interface be through special variables? As far as I can tell
160 ;;; from ANSI 11.1.2.1.1 "Constraints on the COMMON-LISP Package
161 ;;; for Conforming Implementations" it is kosher to add a SETF function for
162 ;;; a symbol in COMMON-LISP..
163 (defvar *short-site-name* nil
164   #!+sb-doc
165   "The value of SHORT-SITE-NAME.")
166 (defvar *long-site-name* nil
167   #!+sb-doc "the value of LONG-SITE-NAME")
168 (defun short-site-name ()
169   #!+sb-doc
170   "Return a string with the abbreviated site name, or NIL if not known."
171   *short-site-name*)
172 (defun long-site-name ()
173   #!+sb-doc
174   "Return a string with the long form of the site name, or NIL if not known."
175   *long-site-name*)
176 \f
177 ;;;; ED
178 (defvar *ed-functions* nil
179   "See function documentation for ED.")
180
181 (defun ed (&optional x)
182   "Starts the editor (on a file or a function if named).  Functions
183 from the list *ED-FUNCTIONS* are called in order with X as an argument
184 until one of them returns non-NIL; these functions are responsible for
185 signalling a FILE-ERROR to indicate failure to perform an operation on
186 the file system."
187   (dolist (fun *ed-functions*
188            (error 'extension-failure
189                   :format-control "Don't know how to ~S ~A"
190                   :format-arguments (list 'ed x)
191                   :references (list '(:sbcl :variable *ed-functions*))))
192     (when (funcall fun x)
193       (return t))))
194 \f
195 ;;;; dribble stuff
196
197 ;;; Each time we start dribbling to a new stream, we put it in
198 ;;; *DRIBBLE-STREAM*, and push a list of *DRIBBLE-STREAM*, *STANDARD-INPUT*,
199 ;;; *STANDARD-OUTPUT* and *ERROR-OUTPUT* in *PREVIOUS-DRIBBLE-STREAMS*.
200 ;;; *STANDARD-OUTPUT* and *ERROR-OUTPUT* is changed to a broadcast stream that
201 ;;; broadcasts to *DRIBBLE-STREAM* and to the old values of the variables.
202 ;;; *STANDARD-INPUT* is changed to an echo stream that echos input from the old
203 ;;; value of standard input to *DRIBBLE-STREAM*.
204 ;;;
205 ;;; When dribble is called with no arguments, *DRIBBLE-STREAM* is closed,
206 ;;; and the values of *DRIBBLE-STREAM*, *STANDARD-INPUT*, and
207 ;;; *STANDARD-OUTPUT* are popped from *PREVIOUS-DRIBBLE-STREAMS*.
208
209 (defvar *previous-dribble-streams* nil)
210 (defvar *dribble-stream* nil)
211
212 (defun dribble (&optional pathname &key (if-exists :append))
213   #!+sb-doc
214   "With a file name as an argument, dribble opens the file and sends a
215   record of further I/O to that file. Without an argument, it closes
216   the dribble file, and quits logging."
217   (cond (pathname
218          (let* ((new-dribble-stream
219                  (open pathname
220                        :direction :output
221                        :if-exists if-exists
222                        :if-does-not-exist :create))
223                 (new-standard-output
224                  (make-broadcast-stream *standard-output* new-dribble-stream))
225                 (new-error-output
226                  (make-broadcast-stream *error-output* new-dribble-stream))
227                 (new-standard-input
228                  (make-echo-stream *standard-input* new-dribble-stream)))
229            (push (list *dribble-stream* *standard-input* *standard-output*
230                        *error-output*)
231                  *previous-dribble-streams*)
232            (setf *dribble-stream* new-dribble-stream)
233            (setf *standard-input* new-standard-input)
234            (setf *standard-output* new-standard-output)
235            (setf *error-output* new-error-output)))
236         ((null *dribble-stream*)
237          (error "not currently dribbling"))
238         (t
239          (let ((old-streams (pop *previous-dribble-streams*)))
240            (close *dribble-stream*)
241            (setf *dribble-stream* (first old-streams))
242            (setf *standard-input* (second old-streams))
243            (setf *standard-output* (third old-streams))
244            (setf *error-output* (fourth old-streams)))))
245   (values))
246
247 (defun %byte-blt (src src-start dst dst-start dst-end)
248   (%byte-blt src src-start dst dst-start dst-end))
249
250 ;;;; some *LOAD-FOO* variables
251
252 (defvar *load-print* nil
253   #!+sb-doc
254   "the default for the :PRINT argument to LOAD")
255
256 (defvar *load-verbose* nil
257   ;; Note that CMU CL's default for this was T, and ANSI says it's
258   ;; implementation-dependent. We choose NIL on the theory that it's
259   ;; a nicer default behavior for Unix programs.
260   #!+sb-doc
261   "the default for the :VERBOSE argument to LOAD")