/[slime]/slime/swank-allegro.lisp
ViewVC logotype

Contents of /slime/swank-allegro.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.20 - (show annotations)
Tue Mar 9 12:46:27 2004 UTC (10 years, 1 month ago) by heller
Branch: MAIN
Changes since 1.19: +63 -126 lines
Merge package-split branch into main trunk.
1 ;;;; -*- Mode: lisp; indent-tabs-mode: nil; outline-regexp: ";;;;;*"; -*-
2 ;;;
3 ;;; swank-allegro.lisp --- Allegro CL specific code for SLIME.
4 ;;;
5 ;;; Created 2003, Helmut Eller
6 ;;;
7 ;;; This code has been placed in the Public Domain. All warranties
8 ;;; are disclaimed. This code was written for "Allegro CL Trial
9 ;;; Edition "5.0 [Linux/X86] (8/29/98 10:57)".
10 ;;;
11
12 (eval-when (:compile-toplevel :load-toplevel :execute)
13 (require :sock)
14 (require :process))
15
16 (in-package :swank-backend)
17
18 (import
19 '(excl:fundamental-character-output-stream
20 excl:stream-write-char
21 excl:stream-force-output
22 excl:fundamental-character-input-stream
23 excl:stream-read-char
24 excl:stream-listen
25 excl:stream-unread-char
26 excl:stream-clear-input
27 excl:stream-line-column
28 excl:stream-read-char-no-hang
29 ))
30
31 ;;;; TCP Server
32
33 (defimplementation preferred-communication-style ()
34 :spawn)
35
36 (defimplementation create-socket (host port)
37 (socket:make-socket :connect :passive :local-port port
38 :local-host host :reuse-address t))
39
40 (defimplementation local-port (socket)
41 (socket:local-port socket))
42
43 (defimplementation close-socket (socket)
44 (close socket))
45
46 (defimplementation accept-connection (socket)
47 (socket:accept-connection socket :wait t))
48
49 (defimplementation emacs-connected ())
50
51 ;;;; Unix signals
52
53 (defimplementation call-without-interrupts (fn)
54 (excl:without-interrupts (funcall fn)))
55
56 (defimplementation getpid ()
57 (excl::getpid))
58
59 (defimplementation lisp-implementation-type-name ()
60 "allegro")
61
62 ;;;; Misc
63
64 (defimplementation arglist (symbol)
65 (excl:arglist symbol))
66
67 (defimplementation describe-symbol-for-emacs (symbol)
68 (let ((result '()))
69 (flet ((doc (kind &optional (sym symbol))
70 (or (documentation sym kind) :not-documented))
71 (maybe-push (property value)
72 (when value
73 (setf result (list* property value result)))))
74 (maybe-push
75 :variable (when (boundp symbol)
76 (doc 'variable)))
77 (maybe-push
78 :function (if (fboundp symbol)
79 (doc 'function)))
80 (maybe-push
81 :class (if (find-class symbol nil)
82 (doc 'class)))
83 result)))
84
85 (defimplementation macroexpand-all (form)
86 (excl::walk form))
87
88 (defimplementation describe-definition (symbol namespace)
89 (ecase namespace
90 (:variable
91 (describe symbol))
92 ((:function :generic-function)
93 (describe (symbol-function symbol)))
94 (:class
95 (describe (find-class symbol)))))
96
97 ;;;; Debugger
98
99 (defvar *sldb-topframe*)
100
101 (defimplementation call-with-debugging-environment (debugger-loop-fn)
102 (let ((*sldb-topframe* (excl::int-newest-frame))
103 (excl::*break-hook* nil))
104 (funcall debugger-loop-fn)))
105
106 (defun format-restarts-for-emacs ()
107 (loop for restart in *sldb-restarts*
108 collect (list (princ-to-string (restart-name restart))
109 (princ-to-string restart))))
110
111 (defun nth-frame (index)
112 (do ((frame *sldb-topframe* (excl::int-next-older-frame frame))
113 (i index (1- i)))
114 ((zerop i) frame)))
115
116 (defimplementation compute-backtrace (start end)
117 (let ((end (or end most-positive-fixnum)))
118 (loop for f = (nth-frame start) then (excl::int-next-older-frame f)
119 for i from start below end
120 while f
121 collect f)))
122
123 (defimplementation print-frame (frame stream)
124 (debugger:output-frame stream frame :moderate))
125
126 (defimplementation frame-locals (index)
127 (let ((frame (nth-frame index)))
128 (loop for i from 0 below (debugger:frame-number-vars frame)
129 collect (list :name (debugger:frame-var-name frame i)
130 :id 0
131 :value (debugger:frame-var-value frame i)))))
132
133 (defimplementation frame-catch-tags (index)
134 (declare (ignore index))
135 nil)
136
137 (defimplementation frame-source-location-for-emacs (index)
138 (list :error (format nil "Cannot find source for frame: ~A"
139 (nth-frame index))))
140
141 (defimplementation eval-in-frame (form frame-number)
142 (debugger:eval-form-in-context
143 form
144 (debugger:environment-of-frame (nth-frame frame-number))))
145
146 (defimplementation return-from-frame (frame-number form)
147 (let ((frame (nth-frame frame-number)))
148 (multiple-value-call #'debugger:frame-return
149 frame (debugger:eval-form-in-context
150 form
151 (debugger:environment-of-frame frame)))))
152
153 ;;; XXX doens't work for frames with arguments
154 (defimplementation restart-frame (frame-number)
155 (let ((frame (nth-frame frame-number)))
156 (debugger:frame-retry frame (debugger:frame-function frame))))
157
158 ;;;; Compiler hooks
159
160 (defvar *buffer-name* nil)
161 (defvar *buffer-start-position*)
162 (defvar *buffer-string*)
163 (defvar *compile-filename*)
164
165 (defun handle-compiler-warning (condition)
166 (let ((loc (getf (slot-value condition 'excl::plist) :loc)))
167 (signal (make-condition
168 'compiler-condition
169 :original-condition condition
170 :severity :warning
171 :message (format nil "~A" condition)
172 :location (cond (*buffer-name*
173 (make-location
174 (list :buffer *buffer-name*)
175 (list :position *buffer-start-position*)))
176 (loc
177 (destructuring-bind (file . pos) loc
178 (make-location
179 (list :file (namestring (truename file)))
180 (list :position (1+ pos)))))
181 (t
182 (make-location
183 (list :file *compile-filename*)
184 (list :position 1))))))))
185
186 (defimplementation swank-compile-file (*compile-filename* load-p)
187 (handler-bind ((warning #'handle-compiler-warning))
188 (let ((*buffer-name* nil))
189 (compile-file *compile-filename* :load-after-compile load-p))))
190
191 (defimplementation swank-compile-string (string &key buffer position)
192 (handler-bind ((warning #'handle-compiler-warning))
193 (let ((*buffer-name* buffer)
194 (*buffer-start-position* position)
195 (*buffer-string* string))
196 (funcall (compile nil (read-from-string
197 (format nil "(CL:LAMBDA () ~A)" string)))))))
198
199 ;;;; Definition Finding
200
201 (defun find-fspec-location (fspec type)
202 (let ((file (excl::fspec-pathname fspec type)))
203 (etypecase file
204 (pathname
205 (let ((start (scm:find-definition-in-file fspec type file)))
206 (make-location (list :file (namestring (truename file)))
207 (if start
208 (list :position (1+ start))
209 (list :function-name (string fspec))))))
210 ((member :top-level)
211 (list :error (format nil "Defined at toplevel: ~A" fspec)))
212 (null
213 (list :error (format nil "Unkown source location for ~A" fspec))))))
214
215 (defun fspec-source-locations (fspec)
216 (let ((defs (excl::find-multiple-definitions fspec)))
217 (loop for (fspec type) in defs
218 collect (list fspec (find-fspec-location fspec type)))))
219
220 (defimplementation find-definitions (symbol)
221 (fspec-source-locations symbol))
222
223 ;;;; XREF
224
225 (defun xrefs (fspecs)
226 (loop for fspec in fspecs
227 nconc (loop for (ref location) in (fspec-source-locations fspec)
228 collect (list ref location))))
229
230 (defimplementation who-calls (name)
231 (xrefs (xref:get-relation :calls :wild name)))
232
233 (defimplementation who-references (name)
234 (xrefs (xref:get-relation :uses :wild name)))
235
236 (defimplementation who-binds (name)
237 (xrefs (xref:get-relation :binds :wild name)))
238
239 (defimplementation who-macroexpands (name)
240 (xrefs (xref:get-relation :macro-calls :wild name)))
241
242 (defimplementation who-sets (name)
243 (xrefs (xref:get-relation :sets :wild name)))
244
245 (defimplementation list-callees (name)
246 (xrefs (xref:get-relation :calls name :wild)))
247
248 ;;;; Inspecting
249
250 (defmethod inspected-parts (o)
251 (let* ((class (class-of o))
252 (slots (clos:class-slots class)))
253 (values (format nil "~A~% is a ~A" o class)
254 (mapcar (lambda (slot)
255 (let ((name (clos:slot-definition-name slot)))
256 (cons (princ-to-string name)
257 (slot-value o name))))
258 slots))))
259
260 ;;;; Multithreading
261
262 (defimplementation startup-multiprocessing ()
263 (mp:start-scheduler))
264
265 (defimplementation spawn (fn &key name)
266 (mp:process-run-function name fn))
267
268 (defimplementation thread-name (thread)
269 (mp:process-name thread))
270
271 (defimplementation thread-status (thread)
272 (format nil "~A ~D" (mp:process-whostate thread)
273 (mp:process-priority thread)))
274
275 (defimplementation make-lock (&key name)
276 (mp:make-process-lock :name name))
277
278 (defimplementation call-with-lock-held (lock function)
279 (mp:with-process-lock (lock) (funcall function)))
280
281 (defimplementation current-thread ()
282 mp:*current-process*)
283
284 (defimplementation all-threads ()
285 (copy-list mp:*all-processes*))
286
287 (defimplementation interrupt-thread (thread fn)
288 (mp:process-interrupt thread fn))
289
290 (defimplementation kill-thread (thread)
291 (mp:process-kill thread))
292
293 (defvar *mailbox-lock* (mp:make-process-lock :name "mailbox lock"))
294
295 (defstruct (mailbox (:conc-name mailbox.))
296 (mutex (mp:make-process-lock :name "process mailbox"))
297 (queue '() :type list))
298
299 (defun mailbox (thread)
300 "Return THREAD's mailbox."
301 (mp:with-process-lock (*mailbox-lock*)
302 (or (getf (mp:process-property-list thread) 'mailbox)
303 (setf (getf (mp:process-property-list thread) 'mailbox)
304 (make-mailbox)))))
305
306 (defimplementation send (thread message)
307 (let* ((mbox (mailbox thread))
308 (mutex (mailbox.mutex mbox)))
309 (mp:with-process-lock (mutex)
310 (setf (mailbox.queue mbox)
311 (nconc (mailbox.queue mbox) (list message))))))
312
313 (defimplementation receive ()
314 (let* ((mbox (mailbox mp:*current-process*))
315 (mutex (mailbox.mutex mbox)))
316 (mp:process-wait "receive" #'mailbox.queue mbox)
317 (mp:with-process-lock (mutex)
318 (pop (mailbox.queue mbox)))))

  ViewVC Help
Powered by ViewVC 1.1.5