function-lambda-expression: Return the name of a generic function.
[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 ;;;; Generalizing over SIMPLE-FUN, CLOSURE, and FUNCALLABLE-INSTANCEs
17
18 ;;; Underlying SIMPLE-FUN
19 (defun %fun-fun (function)
20   (declare (function function))
21   (typecase function
22     (simple-fun
23      function)
24     (closure
25      (%closure-fun function))
26     (funcallable-instance
27      (%fun-fun (funcallable-instance-fun function)))))
28
29 (defun %fun-lambda-list (function)
30   (typecase function
31     #!+sb-eval
32     (sb!eval:interpreted-function
33      (sb!eval:interpreted-function-debug-lambda-list function))
34     (t
35      (%simple-fun-arglist (%fun-fun function)))))
36
37 (defun (setf %fun-lambda-list) (new-value function)
38   (typecase function
39     #!+sb-eval
40     (sb!eval:interpreted-function
41      (setf (sb!eval:interpreted-function-debug-lambda-list function) new-value))
42     ;; FIXME: Eliding general funcallable-instances for now.
43     ((or simple-fun closure)
44      (setf (%simple-fun-arglist (%fun-fun function)) new-value)))
45   new-value)
46
47 (defun %fun-type (function)
48   (%simple-fun-type (%fun-fun function)))
49
50 ;;; a SETFable function to return the associated debug name for FUN
51 ;;; (i.e., the third value returned from CL:FUNCTION-LAMBDA-EXPRESSION),
52 ;;; or NIL if there's none
53 (defun %fun-name (function)
54   (typecase function
55     #!+sb-eval
56     (sb!eval:interpreted-function
57      (sb!eval:interpreted-function-debug-name function))
58     (t
59      (%simple-fun-name (%fun-fun function)))))
60
61 (defun (setf %fun-name) (new-value function)
62   (typecase function
63     #!+sb-eval
64     (sb!eval:interpreted-function
65      (setf (sb!eval:interpreted-function-debug-name function) new-value))
66     ;; FIXME: Eliding general funcallable-instances for now.
67     ((or simple-fun closure)
68      (setf (%simple-fun-name (%fun-fun function)) new-value)))
69   new-value)
70
71 (defun %fun-doc (function)
72   (typecase function
73     #!+sb-eval
74     (sb!eval:interpreted-function
75      (sb!eval:interpreted-function-documentation function))
76     (t
77      (%simple-fun-doc (%fun-fun function)))))
78
79 (defun (setf %fun-doc) (new-value function)
80   (declare (type (or null string) new-value))
81   (typecase function
82     #!+sb-eval
83     (sb!eval:interpreted-function
84      (setf (sb!eval:interpreted-function-documentation function) new-value))
85     ((or simple-fun closure)
86      (setf (%simple-fun-doc (%fun-fun function)) new-value)))
87   new-value)
88 \f
89 ;;; various environment inquiries
90
91 (defvar *features*
92   '#.(sort (copy-list sb-cold:*shebang-features*) #'string<)
93   #!+sb-doc
94   "a list of symbols that describe features provided by the
95    implementation")
96
97 (defun machine-instance ()
98   #!+sb-doc
99   "Return a string giving the name of the local machine."
100   #!+win32 (sb!win32::get-computer-name)
101   #!-win32 (sb!unix:unix-gethostname))
102
103 (defvar *machine-version*)
104
105 (defun machine-version ()
106   #!+sb-doc
107   "Return a string describing the version of the computer hardware we
108 are running on, or NIL if we can't find any useful information."
109   (unless (boundp '*machine-version*)
110     (setf *machine-version* (get-machine-version)))
111   *machine-version*)
112
113 ;;; FIXME: Don't forget to set these in a sample site-init file.
114 ;;; FIXME: Perhaps the functions could be SETFable instead of having the
115 ;;; interface be through special variables? As far as I can tell
116 ;;; from ANSI 11.1.2.1.1 "Constraints on the COMMON-LISP Package
117 ;;; for Conforming Implementations" it is kosher to add a SETF function for
118 ;;; a symbol in COMMON-LISP..
119 (defvar *short-site-name* nil
120   #!+sb-doc
121   "The value of SHORT-SITE-NAME.")
122 (defvar *long-site-name* nil
123   #!+sb-doc "the value of LONG-SITE-NAME")
124 (defun short-site-name ()
125   #!+sb-doc
126   "Return a string with the abbreviated site name, or NIL if not known."
127   *short-site-name*)
128 (defun long-site-name ()
129   #!+sb-doc
130   "Return a string with the long form of the site name, or NIL if not known."
131   *long-site-name*)
132 \f
133 ;;;; ED
134 (defvar *ed-functions* nil
135   "See function documentation for ED.")
136
137 (defun ed (&optional x)
138   "Starts the editor (on a file or a function if named).  Functions
139 from the list *ED-FUNCTIONS* are called in order with X as an argument
140 until one of them returns non-NIL; these functions are responsible for
141 signalling a FILE-ERROR to indicate failure to perform an operation on
142 the file system."
143   (dolist (fun *ed-functions*
144            (error 'extension-failure
145                   :format-control "Don't know how to ~S ~A"
146                   :format-arguments (list 'ed x)
147                   :references (list '(:sbcl :variable *ed-functions*))))
148     (when (funcall fun x)
149       (return t))))
150 \f
151 ;;;; dribble stuff
152
153 ;;; Each time we start dribbling to a new stream, we put it in
154 ;;; *DRIBBLE-STREAM*, and push a list of *DRIBBLE-STREAM*, *STANDARD-INPUT*,
155 ;;; *STANDARD-OUTPUT* and *ERROR-OUTPUT* in *PREVIOUS-DRIBBLE-STREAMS*.
156 ;;; *STANDARD-OUTPUT* and *ERROR-OUTPUT* is changed to a broadcast stream that
157 ;;; broadcasts to *DRIBBLE-STREAM* and to the old values of the variables.
158 ;;; *STANDARD-INPUT* is changed to an echo stream that echos input from the old
159 ;;; value of standard input to *DRIBBLE-STREAM*.
160 ;;;
161 ;;; When dribble is called with no arguments, *DRIBBLE-STREAM* is closed,
162 ;;; and the values of *DRIBBLE-STREAM*, *STANDARD-INPUT*, and
163 ;;; *STANDARD-OUTPUT* are popped from *PREVIOUS-DRIBBLE-STREAMS*.
164
165 (defvar *previous-dribble-streams* nil)
166 (defvar *dribble-stream* nil)
167
168 (defun dribble (&optional pathname &key (if-exists :append))
169   #!+sb-doc
170   "With a file name as an argument, dribble opens the file and sends a
171   record of further I/O to that file. Without an argument, it closes
172   the dribble file, and quits logging."
173   (cond (pathname
174          (let* ((new-dribble-stream
175                  (open pathname
176                        :direction :output
177                        :if-exists if-exists
178                        :if-does-not-exist :create))
179                 (new-standard-output
180                  (make-broadcast-stream *standard-output* new-dribble-stream))
181                 (new-error-output
182                  (make-broadcast-stream *error-output* new-dribble-stream))
183                 (new-standard-input
184                  (make-echo-stream *standard-input* new-dribble-stream)))
185            (push (list *dribble-stream* *standard-input* *standard-output*
186                        *error-output*)
187                  *previous-dribble-streams*)
188            (setf *dribble-stream* new-dribble-stream)
189            (setf *standard-input* new-standard-input)
190            (setf *standard-output* new-standard-output)
191            (setf *error-output* new-error-output)))
192         ((null *dribble-stream*)
193          (error "not currently dribbling"))
194         (t
195          (let ((old-streams (pop *previous-dribble-streams*)))
196            (close *dribble-stream*)
197            (setf *dribble-stream* (first old-streams))
198            (setf *standard-input* (second old-streams))
199            (setf *standard-output* (third old-streams))
200            (setf *error-output* (fourth old-streams)))))
201   (values))
202
203 (defun %byte-blt (src src-start dst dst-start dst-end)
204   (%byte-blt src src-start dst dst-start dst-end))
205
206 ;;;; some *LOAD-FOO* variables
207
208 (defvar *load-print* nil
209   #!+sb-doc
210   "the default for the :PRINT argument to LOAD")
211
212 (defvar *load-verbose* nil
213   ;; Note that CMU CL's default for this was T, and ANSI says it's
214   ;; implementation-dependent. We choose NIL on the theory that it's
215   ;; a nicer default behavior for Unix programs.
216   #!+sb-doc
217   "the default for the :VERBOSE argument to LOAD")