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

Contents of /slime/swank-lispworks.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.132 - (hide annotations)
Wed Sep 2 17:21:15 2009 UTC (4 years, 7 months ago) by sboukarev
Branch: MAIN
Changes since 1.131: +14 -1 lines
* swank-lispworks.lisp (replace-strings-with-symbols): New function for
recursively interning and replacing strings in a list.
(arglist): Replace all strings in arglists with symbols.

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

  ViewVC Help
Powered by ViewVC 1.1.5