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

Contents of /slime/swank-lispworks.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5