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

Contents of /slime/swank-lispworks.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.122 - (hide annotations)
Sun Oct 19 20:03:34 2008 UTC (5 years, 5 months ago) by heller
Branch: MAIN
Changes since 1.121: +0 -4 lines
* swank.lisp (frame-locals-and-catch-tags): New function.
Fetch locals and catch tags with a single RPC.

* slime.el (sldb-frame-details): Use it.

* swank-backend.lisp (frame-catch-tags): Provide a default
implementation.  Delete the dummy defs in various backends.
1 heller 1.74 ;;; -*- indent-tabs-mode: nil -*-
2 heller 1.1 ;;;
3     ;;; swank-lispworks.lisp --- LispWorks 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.
9     ;;;
10    
11 heller 1.30 (in-package :swank-backend)
12 heller 1.1
13     (eval-when (:compile-toplevel :load-toplevel :execute)
14 heller 1.79 (require "comm")
15     (import-from :stream *gray-stream-symbols* :swank-backend))
16 heller 1.1
17 heller 1.62 (import-swank-mop-symbols :clos '(:slot-definition-documentation
18     :eql-specializer
19 heller 1.65 :eql-specializer-object
20     :compute-applicable-methods-using-classes))
21 msimmons 1.57
22     (defun swank-mop:slot-definition-documentation (slot)
23     (documentation slot t))
24 mbaringer 1.59
25 heller 1.65 (defun swank-mop:compute-applicable-methods-using-classes (gf classes)
26     (clos::compute-applicable-methods-from-classes gf classes))
27    
28     ;; lispworks doesn't have the eql-specializer class, it represents
29     ;; them as a list of `(EQL ,OBJECT)
30 mbaringer 1.59 (deftype swank-mop:eql-specializer () 'cons)
31    
32     (defun swank-mop:eql-specializer-object (eql-spec)
33     (second eql-spec))
34 msimmons 1.57
35 heller 1.111 (eval-when (:compile-toplevel :execute :load-toplevel)
36     (defvar *original-defimplementation* (macro-function 'defimplementation))
37     (defmacro defimplementation (&whole whole name args &body body
38     &environment env)
39     (declare (ignore args body))
40     `(progn
41     (dspec:record-definition '(defun ,name) (dspec:location)
42     :check-redefinition-p nil)
43     ,(funcall *original-defimplementation* whole env))))
44 heller 1.49
45 heller 1.13 ;;; TCP server
46 lgorrie 1.12
47 heller 1.30 (defimplementation preferred-communication-style ()
48     :spawn)
49 heller 1.18
50 heller 1.13 (defun socket-fd (socket)
51     (etypecase socket
52     (fixnum socket)
53     (comm:socket-stream (comm:socket-stream-socket socket))))
54    
55 heller 1.22 (defimplementation create-socket (host port)
56 heller 1.13 (multiple-value-bind (socket where errno)
57 msimmons 1.64 #-(or lispworks4.1 (and macosx lispworks4.3))
58     (comm::create-tcp-socket-for-service port :address host)
59     #+(or lispworks4.1 (and macosx lispworks4.3))
60     (comm::create-tcp-socket-for-service port)
61 heller 1.13 (cond (socket socket)
62 heller 1.14 (t (error 'network-error
63 heller 1.13 :format-control "~A failed: ~A (~D)"
64     :format-arguments (list where
65     (list #+unix (lw:get-unix-error errno))
66 heller 1.14 errno))))))
67 heller 1.13
68 lgorrie 1.17 (defimplementation local-port (socket)
69 heller 1.13 (nth-value 1 (comm:get-socket-address (socket-fd socket))))
70    
71 lgorrie 1.17 (defimplementation close-socket (socket)
72 heller 1.13 (comm::close-socket (socket-fd socket)))
73    
74 heller 1.63 (defimplementation accept-connection (socket
75 heller 1.85 &key external-format buffering timeout)
76 heller 1.118 (declare (ignore buffering))
77 heller 1.61 (let* ((fd (comm::get-fd-from-socket socket)))
78 heller 1.13 (assert (/= fd -1))
79 heller 1.118 (assert (valid-external-format-p external-format))
80     (cond ((member (first external-format) '(:latin-1 :ascii))
81     (make-instance 'comm:socket-stream
82     :socket fd
83     :direction :io
84     :read-timeout timeout
85     :element-type 'base-char))
86     (t
87     (make-flexi-stream
88     (make-instance 'comm:socket-stream
89     :socket fd
90     :direction :io
91     :read-timeout timeout
92     :element-type '(unsigned-byte 8))
93     external-format)))))
94    
95     (defun make-flexi-stream (stream external-format)
96     (unless (member :flexi-streams *features*)
97     (error "Cannot use external format ~A without having installed flexi-streams in the inferior-lisp."
98     external-format))
99     (funcall (read-from-string "FLEXI-STREAMS:MAKE-FLEXI-STREAM")
100     stream
101     :external-format
102     (apply (read-from-string "FLEXI-STREAMS:MAKE-EXTERNAL-FORMAT")
103     external-format)))
104 heller 1.13
105 heller 1.37 (defun set-sigint-handler ()
106     ;; Set SIGINT handler on Swank request handler thread.
107     #-win32
108     (sys::set-signal-handler +sigint+
109     (make-sigint-handler mp:*current-process*)))
110    
111 heller 1.88 ;;; Coding Systems
112    
113 heller 1.118 (defun valid-external-format-p (external-format)
114     (member external-format *external-format-to-coding-system*
115     :test #'equal :key #'car))
116    
117 heller 1.88 (defvar *external-format-to-coding-system*
118     '(((:latin-1 :eol-style :lf)
119     "latin-1-unix" "iso-latin-1-unix" "iso-8859-1-unix")
120     ((:latin-1)
121     "latin-1" "iso-latin-1" "iso-8859-1")
122     ((:utf-8) "utf-8")
123     ((:utf-8 :eol-style :lf) "utf-8-unix")
124     ((:euc-jp) "euc-jp")
125     ((:euc-jp :eol-style :lf) "euc-jp-unix")
126     ((:ascii) "us-ascii")
127     ((:ascii :eol-style :lf) "us-ascii-unix")))
128    
129     (defimplementation find-external-format (coding-system)
130     (car (rassoc-if (lambda (x) (member coding-system x :test #'equal))
131     *external-format-to-coding-system*)))
132    
133 heller 1.15 ;;; Unix signals
134    
135 heller 1.18 (defun sigint-handler ()
136 heller 1.16 (with-simple-restart (continue "Continue from SIGINT handler.")
137     (invoke-debugger "SIGINT")))
138 heller 1.1
139 heller 1.18 (defun make-sigint-handler (process)
140     (lambda (&rest args)
141     (declare (ignore args))
142     (mp:process-interrupt process #'sigint-handler)))
143    
144 heller 1.38 (defimplementation call-without-interrupts (fn)
145 heller 1.37 (lw:without-interrupts (funcall fn)))
146 heller 1.48
147 heller 1.29 (defimplementation getpid ()
148     #+win32 (win32:get-current-process-id)
149     #-win32 (system::getpid))
150 heller 1.1
151 heller 1.23 (defimplementation lisp-implementation-type-name ()
152     "lispworks")
153    
154 heller 1.36 (defimplementation set-default-directory (directory)
155     (namestring (hcl:change-directory directory)))
156 heller 1.40
157     ;;;; Documentation
158 heller 1.36
159 msimmons 1.57 (defimplementation arglist (symbol-or-function)
160     (let ((arglist (lw:function-lambda-list symbol-or-function)))
161 heller 1.30 (etypecase arglist
162 heller 1.39 ((member :dont-know)
163     :not-available)
164     (list
165     arglist))))
166 msimmons 1.57
167     (defimplementation function-name (function)
168     (nth-value 2 (function-lambda-expression function)))
169 heller 1.1
170 lgorrie 1.17 (defimplementation macroexpand-all (form)
171 heller 1.1 (walker:walk-form form))
172    
173 heller 1.37 (defun generic-function-p (object)
174 heller 1.36 (typep object 'generic-function))
175    
176 lgorrie 1.17 (defimplementation describe-symbol-for-emacs (symbol)
177 heller 1.1 "Return a plist describing SYMBOL.
178     Return NIL if the symbol is unbound."
179     (let ((result '()))
180     (labels ((first-line (string)
181     (let ((pos (position #\newline string)))
182     (if (null pos) string (subseq string 0 pos))))
183     (doc (kind &optional (sym symbol))
184 heller 1.116 (let ((string (or (documentation sym kind))))
185 heller 1.1 (if string
186     (first-line string)
187     :not-documented)))
188     (maybe-push (property value)
189     (when value
190     (setf result (list* property value result)))))
191     (maybe-push
192     :variable (when (boundp symbol)
193     (doc 'variable)))
194     (maybe-push
195 heller 1.36 :generic-function (if (and (fboundp symbol)
196 heller 1.37 (generic-function-p (fdefinition symbol)))
197 heller 1.36 (doc 'function)))
198     (maybe-push
199     :function (if (and (fboundp symbol)
200 heller 1.37 (not (generic-function-p (fdefinition symbol))))
201 heller 1.1 (doc 'function)))
202     (maybe-push
203 heller 1.49 :setf (let ((setf-name (sys:underlying-setf-name `(setf ,symbol))))
204     (if (fboundp setf-name)
205     (doc 'setf))))
206     (maybe-push
207 heller 1.1 :class (if (find-class symbol nil)
208     (doc 'class)))
209 heller 1.30 result)))
210 heller 1.1
211 heller 1.30 (defimplementation describe-definition (symbol type)
212     (ecase type
213     (:variable (describe-symbol symbol))
214     (:class (describe (find-class symbol)))
215 heller 1.49 ((:function :generic-function) (describe-function symbol))
216     (:setf (describe-function (sys:underlying-setf-name `(setf ,symbol))))))
217 heller 1.30
218     (defun describe-function (symbol)
219     (cond ((fboundp symbol)
220     (format t "~%(~A~{ ~A~})~%~%~:[(not documented)~;~:*~A~]~%"
221     (string-downcase symbol)
222     (mapcar #'string-upcase
223     (lispworks:function-lambda-list symbol))
224     (documentation symbol 'function))
225 heller 1.38 (describe (fdefinition symbol)))
226 heller 1.30 (t (format t "~S is not fbound" symbol))))
227 heller 1.4
228 heller 1.30 (defun describe-symbol (sym)
229 heller 1.1 (format t "~A is a symbol in package ~A." sym (symbol-package sym))
230     (when (boundp sym)
231     (format t "~%~%Value: ~A" (symbol-value sym)))
232     (let ((doc (documentation sym 'variable)))
233     (when doc
234     (format t "~%~%Variable documentation:~%~A" doc)))
235     (when (fboundp sym)
236 heller 1.30 (describe-function sym)))
237 heller 1.1
238     ;;; Debugging
239    
240 heller 1.66 (defclass slime-env (env:environment)
241     ((debugger-hook :initarg :debugger-hoook)))
242    
243     (defun slime-env (hook io-bindings)
244     (make-instance 'slime-env :name "SLIME Environment"
245     :io-bindings io-bindings
246     :debugger-hoook hook))
247    
248 heller 1.111 (defmethod env-internals:environment-display-notifier
249 heller 1.66 ((env slime-env) &key restarts condition)
250 heller 1.111 (declare (ignore restarts condition))
251 heller 1.113 (funcall (swank-sym :swank-debugger-hook) condition *debugger-hook*)
252     ;; nil
253 heller 1.111 )
254 heller 1.66
255     (defmethod env-internals:environment-display-debugger ((env slime-env))
256     *debug-io*)
257    
258 heller 1.111 (defmethod env-internals:confirm-p ((e slime-env) &optional msg &rest args)
259     (apply (swank-sym :y-or-n-p-in-emacs) msg args))
260    
261 heller 1.110 (defimplementation call-with-debugger-hook (hook fun)
262     (let ((*debugger-hook* hook))
263     (env:with-environment ((slime-env hook '()))
264     (funcall fun))))
265 heller 1.66
266 msimmons 1.100 (defimplementation install-debugger-globally (function)
267     (setq *debugger-hook* function)
268     (setf (env:environment) (slime-env function '())))
269    
270 heller 1.26 (defvar *sldb-top-frame*)
271 heller 1.1
272     (defun interesting-frame-p (frame)
273 heller 1.36 (cond ((or (dbg::call-frame-p frame)
274     (dbg::derived-call-frame-p frame)
275     (dbg::foreign-frame-p frame)
276     (dbg::interpreted-call-frame-p frame))
277     t)
278     ((dbg::catch-frame-p frame) dbg:*print-catch-frames*)
279     ((dbg::binding-frame-p frame) dbg:*print-binding-frames*)
280     ((dbg::handler-frame-p frame) dbg:*print-handler-frames*)
281     ((dbg::restart-frame-p frame) dbg:*print-restart-frames*)
282     ((dbg::open-frame-p frame) dbg:*print-open-frames*)
283     (t nil)))
284 heller 1.1
285 heller 1.41 (defun nth-next-frame (frame n)
286     "Unwind FRAME N times."
287     (do ((frame frame (dbg::frame-next frame))
288     (i n (if (interesting-frame-p frame) (1- i) i)))
289 heller 1.48 ((or (not frame)
290     (and (interesting-frame-p frame) (zerop i)))
291     frame)))
292 heller 1.41
293     (defun nth-frame (index)
294     (nth-next-frame *sldb-top-frame* index))
295    
296     (defun find-top-frame ()
297     "Return the most suitable top-frame for the debugger."
298 mbaringer 1.69 (or (do ((frame (dbg::debugger-stack-current-frame dbg::*debugger-stack*)
299     (nth-next-frame frame 1)))
300     ((or (null frame) ; no frame found!
301     (and (dbg::call-frame-p frame)
302     (eq (dbg::call-frame-function-name frame)
303     'invoke-debugger)))
304     (nth-next-frame frame 1)))
305     ;; if we can't find a invoke-debugger frame, take any old frame at the top
306     (dbg::debugger-stack-current-frame dbg::*debugger-stack*)))
307 heller 1.41
308     (defimplementation call-with-debugging-environment (fn)
309     (dbg::with-debugger-stack ()
310     (let ((*sldb-top-frame* (find-top-frame)))
311     (funcall fn))))
312 heller 1.1
313 heller 1.30 (defimplementation compute-backtrace (start end)
314 heller 1.1 (let ((end (or end most-positive-fixnum))
315     (backtrace '()))
316     (do ((frame (nth-frame start) (dbg::frame-next frame))
317     (i start))
318     ((or (not frame) (= i end)) (nreverse backtrace))
319     (when (interesting-frame-p frame)
320     (incf i)
321 heller 1.121 (push frame backtrace)))))
322 heller 1.1
323 heller 1.36 (defun frame-actual-args (frame)
324 heller 1.50 (let ((*break-on-signals* nil))
325     (mapcar (lambda (arg)
326     (case arg
327     ((&rest &optional &key) arg)
328     (t
329     (handler-case (dbg::dbg-eval arg frame)
330 msimmons 1.53 (error (e) (format nil "<~A>" arg))))))
331 heller 1.50 (dbg::call-frame-arglist frame))))
332 heller 1.36
333 heller 1.121 (defimplementation print-frame (frame stream)
334     (cond ((dbg::call-frame-p frame)
335     (format stream "~S ~S"
336     (dbg::call-frame-function-name frame)
337     (frame-actual-args frame)))
338     (t (princ frame stream))))
339 heller 1.1
340 heller 1.47 (defun frame-vars (frame)
341     (first (dbg::frame-locals-format-list frame #'list 75 0)))
342    
343 lgorrie 1.17 (defimplementation frame-locals (n)
344 heller 1.29 (let ((frame (nth-frame n)))
345 heller 1.1 (if (dbg::call-frame-p frame)
346 heller 1.47 (mapcar (lambda (var)
347     (destructuring-bind (name value symbol location) var
348     (declare (ignore name location))
349     (list :name symbol :id 0
350     :value value)))
351     (frame-vars frame)))))
352    
353     (defimplementation frame-var-value (frame var)
354     (let ((frame (nth-frame frame)))
355     (destructuring-bind (_n value _s _l) (nth var (frame-vars frame))
356     (declare (ignore _n _s _l))
357     value)))
358 heller 1.1
359 lgorrie 1.17 (defimplementation frame-source-location-for-emacs (frame)
360 heller 1.60 (let ((frame (nth-frame frame))
361     (callee (if (plusp frame) (nth-frame (1- frame)))))
362 heller 1.1 (if (dbg::call-frame-p frame)
363 heller 1.60 (let ((dspec (dbg::call-frame-function-name frame))
364     (cname (and (dbg::call-frame-p callee)
365     (dbg::call-frame-function-name callee))))
366     (if dspec
367     (frame-location dspec cname))))))
368 heller 1.26
369     (defimplementation eval-in-frame (form frame-number)
370     (let ((frame (nth-frame frame-number)))
371     (dbg::dbg-eval form frame)))
372    
373     (defimplementation return-from-frame (frame-number form)
374     (let* ((frame (nth-frame frame-number))
375 heller 1.30 (return-frame (dbg::find-frame-for-return frame)))
376 heller 1.26 (dbg::dbg-return-from-call-frame frame form return-frame
377     dbg::*debugger-stack*)))
378    
379     (defimplementation restart-frame (frame-number)
380     (let ((frame (nth-frame frame-number)))
381     (dbg::restart-frame frame :same-args t)))
382 heller 1.1
383 heller 1.109 (defimplementation disassemble-frame (frame-number)
384     (let* ((frame (nth-frame frame-number)))
385     (when (dbg::call-frame-p frame)
386     (let ((function (dbg::get-call-frame-function frame)))
387     (disassemble function)))))
388    
389 heller 1.18 ;;; Definition finding
390    
391 heller 1.60 (defun frame-location (dspec callee-name)
392     (let ((infos (dspec:find-dspec-locations dspec)))
393     (cond (infos
394     (destructuring-bind ((rdspec location) &rest _) infos
395     (declare (ignore _))
396     (let ((name (and callee-name (symbolp callee-name)
397     (string callee-name))))
398     (make-dspec-location rdspec location
399     `(:call-site ,name)))))
400     (t
401     (list :error (format nil "Source location not available for: ~S"
402     dspec))))))
403 heller 1.4
404 heller 1.33 (defimplementation find-definitions (name)
405 heller 1.26 (let ((locations (dspec:find-name-locations dspec:*dspec-classes* name)))
406 heller 1.33 (loop for (dspec location) in locations
407     collect (list dspec (make-dspec-location dspec location)))))
408 heller 1.6
409 msimmons 1.56
410 heller 1.18 ;;; Compilation
411    
412 msimmons 1.42 (defmacro with-swank-compilation-unit ((location &rest options) &body body)
413     (lw:rebinding (location)
414     `(let ((compiler::*error-database* '()))
415     (with-compilation-unit ,options
416 heller 1.120 (multiple-value-prog1 (progn ,@body)
417     (signal-error-data-base compiler::*error-database*
418     ,location)
419     (signal-undefined-functions compiler::*unknown-functions*
420     ,location))))))
421 msimmons 1.42
422 heller 1.88 (defimplementation swank-compile-file (filename load-p external-format)
423 msimmons 1.42 (with-swank-compilation-unit (filename)
424 heller 1.119 (compile-file filename :load load-p
425     :external-format external-format)))
426 msimmons 1.56
427 eweitz 1.70 (defvar *within-call-with-compilation-hooks* nil
428     "Whether COMPILE-FILE was called from within CALL-WITH-COMPILATION-HOOKS.")
429    
430     (defvar *undefined-functions-hash* nil
431     "Hash table to map info about undefined functions to pathnames.")
432    
433     (lw:defadvice (compile-file compile-file-and-collect-notes :around)
434     (pathname &rest rest)
435 msimmons 1.75 (multiple-value-prog1 (apply #'lw:call-next-advice pathname rest)
436 eweitz 1.70 (when *within-call-with-compilation-hooks*
437     (maphash (lambda (unfun dspecs)
438     (dolist (dspec dspecs)
439     (let ((unfun-info (list unfun dspec)))
440     (unless (gethash unfun-info *undefined-functions-hash*)
441     (setf (gethash unfun-info *undefined-functions-hash*)
442     pathname)))))
443     compiler::*unknown-functions*))))
444    
445 msimmons 1.56 (defimplementation call-with-compilation-hooks (function)
446 eweitz 1.70 (let ((compiler::*error-database* '())
447     (*undefined-functions-hash* (make-hash-table :test 'equal))
448     (*within-call-with-compilation-hooks* t))
449     (with-compilation-unit ()
450     (prog1 (funcall function)
451     (signal-error-data-base compiler::*error-database*)
452     (signal-undefined-functions compiler::*unknown-functions*)))))
453 heller 1.1
454     (defun map-error-database (database fn)
455     (loop for (filename . defs) in database do
456     (loop for (dspec . conditions) in defs do
457     (dolist (c conditions)
458 msimmons 1.98 (funcall fn filename dspec (if (consp c) (car c) c))))))
459 heller 1.1
460     (defun lispworks-severity (condition)
461     (cond ((not condition) :warning)
462     (t (etypecase condition
463 heller 1.6 (error :error)
464 heller 1.1 (style-warning :warning)
465     (warning :warning)))))
466    
467     (defun signal-compiler-condition (message location condition)
468     (check-type message string)
469     (signal
470     (make-instance 'compiler-condition :message message
471     :severity (lispworks-severity condition)
472     :location location
473     :original-condition condition)))
474    
475 heller 1.118 (defvar *temp-file-format* '(:utf-8 :eol-style :lf))
476    
477 heller 1.1 (defun compile-from-temp-file (string filename)
478     (unwind-protect
479     (progn
480 heller 1.118 (with-open-file (s filename :direction :output
481     :if-exists :supersede
482     :external-format *temp-file-format*)
483    
484 heller 1.1 (write-string string s)
485     (finish-output s))
486 heller 1.120 (multiple-value-bind (binary-filename warnings? failure?)
487     (compile-file filename :load t
488     :external-format *temp-file-format*)
489     (declare (ignore warnings?))
490 heller 1.6 (when binary-filename
491 heller 1.120 (delete-file binary-filename))
492     (not failure?)))
493 heller 1.1 (delete-file filename)))
494    
495 heller 1.117 (defun dspec-function-name-position (dspec fallback)
496 heller 1.34 (etypecase dspec
497     (cons (let ((name (dspec:dspec-primary-name dspec)))
498 heller 1.37 (typecase name
499 heller 1.34 ((or symbol string)
500     (list :function-name (string name)))
501 heller 1.117 (t fallback))))
502     (null fallback)
503 heller 1.34 (symbol (list :function-name (string dspec)))))
504 heller 1.18
505 lgorrie 1.45 (defmacro with-fairly-standard-io-syntax (&body body)
506     "Like WITH-STANDARD-IO-SYNTAX but preserve *PACKAGE* and *READTABLE*."
507     (let ((package (gensym))
508     (readtable (gensym)))
509     `(let ((,package *package*)
510     (,readtable *readtable*))
511     (with-standard-io-syntax
512     (let ((*package* ,package)
513     (*readtable* ,readtable))
514     ,@body)))))
515    
516 heller 1.117 (defun skip-comments (stream)
517     (let ((pos0 (file-position stream)))
518     (cond ((equal (ignore-errors (list (read-delimited-list #\( stream)))
519     '(()))
520     (file-position stream (1- (file-position stream))))
521     (t (file-position stream pos0)))))
522    
523 heller 1.52 #-(or lispworks4.1 lispworks4.2) ; no dspec:parse-form-dspec prior to 4.3
524 msimmons 1.42 (defun dspec-stream-position (stream dspec)
525 lgorrie 1.45 (with-fairly-standard-io-syntax
526 heller 1.117 (loop (let* ((pos (progn (skip-comments stream) (file-position stream)))
527 lgorrie 1.44 (form (read stream nil '#1=#:eof)))
528     (when (eq form '#1#)
529     (return nil))
530     (labels ((check-dspec (form)
531     (when (consp form)
532     (let ((operator (car form)))
533     (case operator
534     ((progn)
535     (mapcar #'check-dspec
536     (cdr form)))
537     ((eval-when locally macrolet symbol-macrolet)
538     (mapcar #'check-dspec
539     (cddr form)))
540     ((in-package)
541     (let ((package (find-package (second form))))
542     (when package
543     (setq *package* package))))
544     (otherwise
545     (let ((form-dspec (dspec:parse-form-dspec form)))
546     (when (dspec:dspec-equal dspec form-dspec)
547     (return pos)))))))))
548     (check-dspec form))))))
549 msimmons 1.42
550 heller 1.52 (defun dspec-file-position (file dspec)
551 mbaringer 1.90 (let* ((*compile-file-pathname* (pathname file))
552     (*compile-file-truename* (truename *compile-file-pathname*))
553     (*load-pathname* *compile-file-pathname*)
554     (*load-truename* *compile-file-truename*))
555     (with-open-file (stream file)
556     (let ((pos
557     #-(or lispworks4.1 lispworks4.2)
558     (dspec-stream-position stream dspec)))
559     (if pos
560 heller 1.117 (list :position (1+ pos))
561     (dspec-function-name-position dspec `(:position 1)))))))
562 heller 1.52
563 heller 1.25 (defun emacs-buffer-location-p (location)
564     (and (consp location)
565     (eq (car location) :emacs-buffer)))
566    
567 heller 1.60 (defun make-dspec-location (dspec location &optional hints)
568 heller 1.52 (etypecase location
569     ((or pathname string)
570     (multiple-value-bind (file err)
571     (ignore-errors (namestring (truename location)))
572     (if err
573     (list :error (princ-to-string err))
574     (make-location `(:file ,file)
575 heller 1.60 (dspec-file-position file dspec)
576     hints))))
577 heller 1.52 (symbol
578     `(:error ,(format nil "Cannot resolve location: ~S" location)))
579     ((satisfies emacs-buffer-location-p)
580     (destructuring-bind (_ buffer offset string) location
581     (declare (ignore _ string))
582     (make-location `(:buffer ,buffer)
583 heller 1.117 (dspec-function-name-position dspec `(:offset ,offset 0))
584 heller 1.60 hints)))))
585 heller 1.1
586 msimmons 1.42 (defun make-dspec-progenitor-location (dspec location)
587     (let ((canon-dspec (dspec:canonicalize-dspec dspec)))
588     (make-dspec-location
589     (if canon-dspec
590     (if (dspec:local-dspec-p canon-dspec)
591     (dspec:dspec-progenitor canon-dspec)
592     canon-dspec)
593     nil)
594     location)))
595    
596 eweitz 1.70 (defun signal-error-data-base (database &optional location)
597 heller 1.1 (map-error-database
598     database
599     (lambda (filename dspec condition)
600     (signal-compiler-condition
601     (format nil "~A" condition)
602 eweitz 1.70 (make-dspec-progenitor-location dspec (or location filename))
603 heller 1.1 condition))))
604    
605 eweitz 1.71 (defun unmangle-unfun (symbol)
606     "Converts symbols like 'SETF::|\"CL-USER\" \"GET\"| to
607     function names like \(SETF GET)."
608 heller 1.93 (cond ((sys::setf-symbol-p symbol)
609     (sys::setf-pair-from-underlying-name symbol))
610     (t symbol)))
611    
612 eweitz 1.70 (defun signal-undefined-functions (htab &optional filename)
613 heller 1.1 (maphash (lambda (unfun dspecs)
614     (dolist (dspec dspecs)
615     (signal-compiler-condition
616 eweitz 1.71 (format nil "Undefined function ~A" (unmangle-unfun unfun))
617 eweitz 1.70 (make-dspec-progenitor-location dspec
618     (or filename
619     (gethash (list unfun dspec)
620     *undefined-functions-hash*)))
621 heller 1.1 nil)))
622     htab))
623 heller 1.2
624 heller 1.99 (defimplementation swank-compile-string (string &key buffer position directory
625     debug)
626     (declare (ignore directory debug))
627 heller 1.1 (assert buffer)
628     (assert position)
629 heller 1.30 (let* ((location (list :emacs-buffer buffer position string))
630 heller 1.26 (tmpname (hcl:make-temp-file nil "lisp")))
631 msimmons 1.42 (with-swank-compilation-unit (location)
632 heller 1.26 (compile-from-temp-file
633 heller 1.68 (with-output-to-string (s)
634     (let ((*print-radix* t))
635     (print `(eval-when (:compile-toplevel)
636     (setq dspec::*location* (list ,@location)))
637     s))
638     (write-string string s))
639 msimmons 1.42 tmpname))))
640 heller 1.1
641 heller 1.3 ;;; xref
642    
643 heller 1.31 (defmacro defxref (name function)
644     `(defimplementation ,name (name)
645     (xref-results (,function name))))
646    
647     (defxref who-calls hcl:who-calls)
648 msimmons 1.42 (defxref who-macroexpands hcl:who-calls) ; macros are in the calls table too
649 heller 1.67 (defxref calls-who hcl:calls-who)
650 msimmons 1.42 (defxref list-callers list-callers-internal)
651 heller 1.68 ;; (defxref list-callees list-callees-internal)
652 msimmons 1.42
653     (defun list-callers-internal (name)
654     (let ((callers (make-array 100
655     :fill-pointer 0
656     :adjustable t)))
657     (hcl:sweep-all-objects
658     #'(lambda (object)
659     (when (and #+Harlequin-PC-Lisp (low:compiled-code-p object)
660     #-Harlequin-PC-Lisp (sys::callablep object)
661     (system::find-constant$funcallable name object))
662     (vector-push-extend object callers))))
663     ;; Delay dspec:object-dspec until after sweep-all-objects
664     ;; to reduce allocation problems.
665     (loop for object across callers
666     collect (if (symbolp object)
667     (list 'function object)
668 heller 1.63 (or (dspec:object-dspec object) object)))))
669 heller 1.36
670 heller 1.37 ;; only for lispworks 4.2 and above
671     #-lispworks4.1
672     (progn
673     (defxref who-references hcl:who-references)
674     (defxref who-binds hcl:who-binds)
675     (defxref who-sets hcl:who-sets))
676    
677 heller 1.36 (defimplementation who-specializes (classname)
678     (let ((methods (clos:class-direct-methods (find-class classname))))
679     (xref-results (mapcar #'dspec:object-dspec methods))))
680 heller 1.31
681     (defun xref-results (dspecs)
682 heller 1.63 (flet ((frob-locs (dspec locs)
683     (cond (locs
684     (loop for (name loc) in locs
685     collect (list name (make-dspec-location name loc))))
686     (t `((,dspec (:error "Source location not available")))))))
687     (loop for dspec in dspecs
688     append (frob-locs dspec (dspec:dspec-definition-locations dspec)))))
689 heller 1.66
690 heller 1.25 ;;; Inspector
691 mbaringer 1.58
692 heller 1.95 (defmethod emacs-inspect ((o t))
693 heller 1.60 (lispworks-inspect o))
694    
695 heller 1.95 (defmethod emacs-inspect ((o function))
696 heller 1.60 (lispworks-inspect o))
697    
698 heller 1.82 ;; FIXME: slot-boundp-using-class in LW works with names so we can't
699     ;; use our method in swank.lisp.
700 heller 1.95 (defmethod emacs-inspect ((o standard-object))
701 heller 1.82 (lispworks-inspect o))
702    
703 heller 1.60 (defun lispworks-inspect (o)
704 heller 1.25 (multiple-value-bind (names values _getter _setter type)
705     (lw:get-inspector-values o nil)
706     (declare (ignore _getter _setter))
707 heller 1.60 (append
708     (label-value-line "Type" type)
709 msimmons 1.91 (loop for name in names
710     for value in values
711 heller 1.96 append (label-value-line name value)))))
712 heller 1.25
713 lgorrie 1.46 ;;; Miscellaneous
714    
715     (defimplementation quit-lisp ()
716     (lispworks:quit))
717    
718 heller 1.66 ;;; Tracing
719    
720     (defun parse-fspec (fspec)
721     "Return a dspec for FSPEC."
722     (ecase (car fspec)
723 heller 1.67 ((:defmethod) `(method ,(cdr fspec)))))
724 heller 1.66
725     (defun tracedp (dspec)
726     (member dspec (eval '(trace)) :test #'equal))
727    
728 heller 1.67 (defun toggle-trace-aux (dspec)
729 heller 1.66 (cond ((tracedp dspec)
730     (eval `(untrace ,dspec))
731     (format nil "~S is now untraced." dspec))
732     (t
733     (eval `(trace (,dspec)))
734     (format nil "~S is now traced." dspec))))
735    
736 heller 1.67 (defimplementation toggle-trace (fspec)
737     (toggle-trace-aux (parse-fspec fspec)))
738 heller 1.66
739 heller 1.16 ;;; Multithreading
740    
741 mbaringer 1.86 (defimplementation initialize-multiprocessing (continuation)
742 heller 1.87 (cond ((not mp::*multiprocessing*)
743     (push (list "Initialize SLIME" '() continuation)
744     mp:*initial-processes*)
745     (mp:initialize-multiprocessing))
746     (t (funcall continuation))))
747 heller 1.16
748 heller 1.18 (defimplementation spawn (fn &key name)
749 heller 1.38 (let ((mp:*process-initial-bindings*
750     (remove (find-package :cl)
751     mp:*process-initial-bindings*
752     :key (lambda (x) (symbol-package (car x))))))
753     (mp:process-run-function name () fn)))
754 heller 1.48
755     (defvar *id-lock* (mp:make-lock))
756     (defvar *thread-id-counter* 0)
757    
758     (defimplementation thread-id (thread)
759     (mp:with-lock (*id-lock*)
760     (or (getf (mp:process-plist thread) 'id)
761     (setf (getf (mp:process-plist thread) 'id)
762     (incf *thread-id-counter*)))))
763    
764     (defimplementation find-thread (id)
765     (find id (mp:list-all-processes)
766     :key (lambda (p) (getf (mp:process-plist p) 'id))))
767 heller 1.16
768 heller 1.21 (defimplementation thread-name (thread)
769     (mp:process-name thread))
770 heller 1.16
771 heller 1.21 (defimplementation thread-status (thread)
772     (format nil "~A ~D"
773     (mp:process-whostate thread)
774     (mp:process-priority thread)))
775 heller 1.16
776 heller 1.18 (defimplementation make-lock (&key name)
777 heller 1.16 (mp:make-lock :name name))
778    
779 heller 1.18 (defimplementation call-with-lock-held (lock function)
780 heller 1.16 (mp:with-lock (lock) (funcall function)))
781 heller 1.20
782     (defimplementation current-thread ()
783     mp:*current-process*)
784 heller 1.21
785     (defimplementation all-threads ()
786     (mp:list-all-processes))
787 heller 1.20
788     (defimplementation interrupt-thread (thread fn)
789     (mp:process-interrupt thread fn))
790 heller 1.25
791     (defimplementation kill-thread (thread)
792     (mp:process-kill thread))
793 heller 1.27
794     (defimplementation thread-alive-p (thread)
795     (mp:process-alive-p thread))
796 heller 1.20
797 heller 1.102 (defstruct (mailbox (:conc-name mailbox.))
798     (mutex (mp:make-lock :name "thread mailbox"))
799     (queue '() :type list))
800    
801 heller 1.20 (defvar *mailbox-lock* (mp:make-lock))
802    
803     (defun mailbox (thread)
804     (mp:with-lock (*mailbox-lock*)
805     (or (getf (mp:process-plist thread) 'mailbox)
806     (setf (getf (mp:process-plist thread) 'mailbox)
807 heller 1.102 (make-mailbox)))))
808 heller 1.20
809 heller 1.112 (defimplementation receive-if (test &optional timeout)
810 heller 1.102 (let* ((mbox (mailbox mp:*current-process*))
811     (lock (mailbox.mutex mbox)))
812 heller 1.112 (assert (or (not timeout) (eq timeout t)))
813 heller 1.102 (loop
814 heller 1.106 (check-slime-interrupts)
815     (mp:with-lock (lock "receive-if/try")
816     (let* ((q (mailbox.queue mbox))
817     (tail (member-if test q)))
818     (when tail
819     (setf (mailbox.queue mbox) (nconc (ldiff q tail) (cdr tail)))
820     (return (car tail)))))
821 heller 1.112 (when (eq timeout t) (return (values nil t)))
822 heller 1.106 (mp:process-wait-with-timeout
823     "receive-if" 0.2 (lambda () (some test (mailbox.queue mbox)))))))
824 heller 1.102
825     (defimplementation send (thread message)
826     (let ((mbox (mailbox thread)))
827 heller 1.106 (mp:with-lock ((mailbox.mutex mbox))
828     (setf (mailbox.queue mbox)
829     (nconc (mailbox.queue mbox) (list message))))))
830 heller 1.3
831 heller 1.66 ;;; Some intergration with the lispworks environment
832    
833 heller 1.74 (defun swank-sym (name) (find-symbol (string name) :swank))
834 heller 1.66
835     (defimplementation emacs-connected ()
836     (when (eq (eval (swank-sym :*communication-style*))
837     nil)
838 heller 1.110 (set-sigint-handler)))
839 mkoeppe 1.77
840 eweitz 1.89
841     ;;;; Weak hashtables
842    
843     (defimplementation make-weak-key-hash-table (&rest args)
844     (apply #'make-hash-table :weak-kind :key args))
845    
846     (defimplementation make-weak-value-hash-table (&rest args)
847     (apply #'make-hash-table :weak-kind :value args))

  ViewVC Help
Powered by ViewVC 1.1.5