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

Contents of /slime/swank-lispworks.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5