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

Contents of /slime/swank-lispworks.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.63 - (show annotations)
Mon Nov 29 17:33:04 2004 UTC (9 years, 4 months ago) by heller
Branch: MAIN
Changes since 1.62: +10 -8 lines
(list-callers-internal): Return the function if dspec:object-dspec
returns nil.
(xref-results): Previously, functions for which
dspec:dspec-definition-locations returned nil were ignored.
Include them with a unknown source-location.

(accept-connection): Add default for external-format.
1 ;;; -*- Mode: lisp; indent-tabs-mode: nil -*-
2 ;;;
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 (in-package :swank-backend)
12
13 (eval-when (:compile-toplevel :load-toplevel :execute)
14 (require "comm"))
15
16 (import
17 '(stream:fundamental-character-output-stream
18 stream:stream-write-char
19 stream:stream-force-output
20 stream:fundamental-character-input-stream
21 stream:stream-read-char
22 stream:stream-listen
23 stream:stream-unread-char
24 stream:stream-clear-input
25 stream:stream-line-column
26 ))
27
28 (import-swank-mop-symbols :clos '(:slot-definition-documentation
29 :eql-specializer
30 :eql-specializer-object))
31
32 (defun swank-mop:slot-definition-documentation (slot)
33 (documentation slot t))
34
35 ;;;; lispworks doesn't have the eql-specializer class, it represents
36 ;;;; them as a list of `(EQL ,OBJECT)
37 (deftype swank-mop:eql-specializer () 'cons)
38
39 (defun swank-mop:eql-specializer-object (eql-spec)
40 (second eql-spec))
41
42 (when (fboundp 'dspec::define-dspec-alias)
43 (dspec::define-dspec-alias defimplementation (name args &rest body)
44 `(defmethod ,name ,args ,@body)))
45
46 ;;; TCP server
47
48 (defimplementation preferred-communication-style ()
49 :spawn)
50
51 (defun socket-fd (socket)
52 (etypecase socket
53 (fixnum socket)
54 (comm:socket-stream (comm:socket-stream-socket socket))))
55
56 (defimplementation create-socket (host port)
57 (multiple-value-bind (socket where errno)
58 #-lispworks4.1(comm::create-tcp-socket-for-service port :address host)
59 #+lispworks4.1(comm::create-tcp-socket-for-service port)
60 (cond (socket socket)
61 (t (error 'network-error
62 :format-control "~A failed: ~A (~D)"
63 :format-arguments (list where
64 (list #+unix (lw:get-unix-error errno))
65 errno))))))
66
67 (defimplementation local-port (socket)
68 (nth-value 1 (comm:get-socket-address (socket-fd socket))))
69
70 (defimplementation close-socket (socket)
71 (comm::close-socket (socket-fd socket)))
72
73 (defimplementation accept-connection (socket
74 &key (external-format :iso-latin-1-unix))
75 (assert (eq external-format :iso-latin-1-unix))
76 (let* ((fd (comm::get-fd-from-socket socket)))
77 (assert (/= fd -1))
78 (make-instance 'comm:socket-stream :socket fd :direction :io
79 :element-type 'base-char)))
80
81 (defun set-sigint-handler ()
82 ;; Set SIGINT handler on Swank request handler thread.
83 #-win32
84 (sys::set-signal-handler +sigint+
85 (make-sigint-handler mp:*current-process*)))
86
87 (defimplementation emacs-connected ()
88 (declare (ignore stream))
89 (when (eq nil (symbol-value
90 (find-symbol (string :*communication-style*) :swank)))
91 (set-sigint-handler))
92 (let ((lw:*handle-warn-on-redefinition* :warn))
93 (defmethod env-internals:environment-display-notifier
94 (env &key restarts condition)
95 (declare (ignore restarts))
96 (funcall (find-symbol (string :swank-debugger-hook) :swank)
97 condition *debugger-hook*))
98 (defmethod env-internals:environment-display-debugger
99 (env)
100 *debug-io*)))
101
102 (defimplementation make-stream-interactive (stream)
103 (let ((lw:*handle-warn-on-redefinition* :warn))
104 (defmethod stream:stream-soft-force-output ((o (eql stream)))
105 (force-output o))))
106
107 ;;; Unix signals
108
109 (defun sigint-handler ()
110 (with-simple-restart (continue "Continue from SIGINT handler.")
111 (invoke-debugger "SIGINT")))
112
113 (defun make-sigint-handler (process)
114 (lambda (&rest args)
115 (declare (ignore args))
116 (mp:process-interrupt process #'sigint-handler)))
117
118 (defimplementation call-without-interrupts (fn)
119 (lw:without-interrupts (funcall fn)))
120
121 (defimplementation getpid ()
122 #+win32 (win32:get-current-process-id)
123 #-win32 (system::getpid))
124
125 (defimplementation lisp-implementation-type-name ()
126 "lispworks")
127
128 (defimplementation set-default-directory (directory)
129 (namestring (hcl:change-directory directory)))
130
131 ;;;; Documentation
132
133 (defimplementation arglist (symbol-or-function)
134 (let ((arglist (lw:function-lambda-list symbol-or-function)))
135 (etypecase arglist
136 ((member :dont-know)
137 :not-available)
138 (list
139 arglist))))
140
141 (defimplementation function-name (function)
142 (nth-value 2 (function-lambda-expression function)))
143
144 (defimplementation macroexpand-all (form)
145 (walker:walk-form form))
146
147 (defun generic-function-p (object)
148 (typep object 'generic-function))
149
150 (defimplementation describe-symbol-for-emacs (symbol)
151 "Return a plist describing SYMBOL.
152 Return NIL if the symbol is unbound."
153 (let ((result '()))
154 (labels ((first-line (string)
155 (let ((pos (position #\newline string)))
156 (if (null pos) string (subseq string 0 pos))))
157 (doc (kind &optional (sym symbol))
158 (let ((string (documentation sym kind)))
159 (if string
160 (first-line string)
161 :not-documented)))
162 (maybe-push (property value)
163 (when value
164 (setf result (list* property value result)))))
165 (maybe-push
166 :variable (when (boundp symbol)
167 (doc 'variable)))
168 (maybe-push
169 :generic-function (if (and (fboundp symbol)
170 (generic-function-p (fdefinition symbol)))
171 (doc 'function)))
172 (maybe-push
173 :function (if (and (fboundp symbol)
174 (not (generic-function-p (fdefinition symbol))))
175 (doc 'function)))
176 (maybe-push
177 :setf (let ((setf-name (sys:underlying-setf-name `(setf ,symbol))))
178 (if (fboundp setf-name)
179 (doc 'setf))))
180 (maybe-push
181 :class (if (find-class symbol nil)
182 (doc 'class)))
183 result)))
184
185 (defimplementation describe-definition (symbol type)
186 (ecase type
187 (:variable (describe-symbol symbol))
188 (:class (describe (find-class symbol)))
189 ((:function :generic-function) (describe-function symbol))
190 (:setf (describe-function (sys:underlying-setf-name `(setf ,symbol))))))
191
192 (defun describe-function (symbol)
193 (cond ((fboundp symbol)
194 (format t "~%(~A~{ ~A~})~%~%~:[(not documented)~;~:*~A~]~%"
195 (string-downcase symbol)
196 (mapcar #'string-upcase
197 (lispworks:function-lambda-list symbol))
198 (documentation symbol 'function))
199 (describe (fdefinition symbol)))
200 (t (format t "~S is not fbound" symbol))))
201
202 (defun describe-symbol (sym)
203 (format t "~A is a symbol in package ~A." sym (symbol-package sym))
204 (when (boundp sym)
205 (format t "~%~%Value: ~A" (symbol-value sym)))
206 (let ((doc (documentation sym 'variable)))
207 (when doc
208 (format t "~%~%Variable documentation:~%~A" doc)))
209 (when (fboundp sym)
210 (describe-function sym)))
211
212 ;;; Debugging
213
214 (defvar *sldb-top-frame*)
215
216 (defun interesting-frame-p (frame)
217 (cond ((or (dbg::call-frame-p frame)
218 (dbg::derived-call-frame-p frame)
219 (dbg::foreign-frame-p frame)
220 (dbg::interpreted-call-frame-p frame))
221 t)
222 ((dbg::catch-frame-p frame) dbg:*print-catch-frames*)
223 ((dbg::binding-frame-p frame) dbg:*print-binding-frames*)
224 ((dbg::handler-frame-p frame) dbg:*print-handler-frames*)
225 ((dbg::restart-frame-p frame) dbg:*print-restart-frames*)
226 ((dbg::open-frame-p frame) dbg:*print-open-frames*)
227 (t nil)))
228
229 (defun nth-next-frame (frame n)
230 "Unwind FRAME N times."
231 (do ((frame frame (dbg::frame-next frame))
232 (i n (if (interesting-frame-p frame) (1- i) i)))
233 ((or (not frame)
234 (and (interesting-frame-p frame) (zerop i)))
235 frame)))
236
237 (defun nth-frame (index)
238 (nth-next-frame *sldb-top-frame* index))
239
240 (defun find-top-frame ()
241 "Return the most suitable top-frame for the debugger."
242 (do ((frame (dbg::debugger-stack-current-frame dbg::*debugger-stack*)
243 (nth-next-frame frame 1)))
244 ((and (dbg::call-frame-p frame)
245 (eq (dbg::call-frame-function-name frame)
246 'invoke-debugger))
247 (nth-next-frame frame 1))))
248
249 (defimplementation call-with-debugging-environment (fn)
250 (dbg::with-debugger-stack ()
251 (let ((*sldb-top-frame* (find-top-frame)))
252 (funcall fn))))
253
254 (defimplementation compute-backtrace (start end)
255 (let ((end (or end most-positive-fixnum))
256 (backtrace '()))
257 (do ((frame (nth-frame start) (dbg::frame-next frame))
258 (i start))
259 ((or (not frame) (= i end)) (nreverse backtrace))
260 (when (interesting-frame-p frame)
261 (incf i)
262 (push frame backtrace)))))
263
264 (defun frame-actual-args (frame)
265 (let ((*break-on-signals* nil))
266 (mapcar (lambda (arg)
267 (case arg
268 ((&rest &optional &key) arg)
269 (t
270 (handler-case (dbg::dbg-eval arg frame)
271 (error (e) (format nil "<~A>" arg))))))
272 (dbg::call-frame-arglist frame))))
273
274 (defimplementation print-frame (frame stream)
275 (cond ((dbg::call-frame-p frame)
276 (format stream "~S ~S"
277 (dbg::call-frame-function-name frame)
278 (frame-actual-args frame)))
279 (t (princ frame stream))))
280
281 (defun frame-vars (frame)
282 (first (dbg::frame-locals-format-list frame #'list 75 0)))
283
284 (defimplementation frame-locals (n)
285 (let ((frame (nth-frame n)))
286 (if (dbg::call-frame-p frame)
287 (mapcar (lambda (var)
288 (destructuring-bind (name value symbol location) var
289 (declare (ignore name location))
290 (list :name symbol :id 0
291 :value value)))
292 (frame-vars frame)))))
293
294 (defimplementation frame-var-value (frame var)
295 (let ((frame (nth-frame frame)))
296 (destructuring-bind (_n value _s _l) (nth var (frame-vars frame))
297 (declare (ignore _n _s _l))
298 value)))
299
300 (defimplementation frame-catch-tags (index)
301 (declare (ignore index))
302 nil)
303
304 (defimplementation frame-source-location-for-emacs (frame)
305 (let ((frame (nth-frame frame))
306 (callee (if (plusp frame) (nth-frame (1- frame)))))
307 (if (dbg::call-frame-p frame)
308 (let ((dspec (dbg::call-frame-function-name frame))
309 (cname (and (dbg::call-frame-p callee)
310 (dbg::call-frame-function-name callee))))
311 (if dspec
312 (frame-location dspec cname))))))
313
314 (defimplementation eval-in-frame (form frame-number)
315 (let ((frame (nth-frame frame-number)))
316 (dbg::dbg-eval form frame)))
317
318 (defimplementation return-from-frame (frame-number form)
319 (let* ((frame (nth-frame frame-number))
320 (return-frame (dbg::find-frame-for-return frame)))
321 (dbg::dbg-return-from-call-frame frame form return-frame
322 dbg::*debugger-stack*)))
323
324 (defimplementation restart-frame (frame-number)
325 (let ((frame (nth-frame frame-number)))
326 (dbg::restart-frame frame :same-args t)))
327
328 ;;; Definition finding
329
330 (defun frame-location (dspec callee-name)
331 (let ((infos (dspec:find-dspec-locations dspec)))
332 (cond (infos
333 (destructuring-bind ((rdspec location) &rest _) infos
334 (declare (ignore _))
335 (let ((name (and callee-name (symbolp callee-name)
336 (string callee-name))))
337 (make-dspec-location rdspec location
338 `(:call-site ,name)))))
339 (t
340 (list :error (format nil "Source location not available for: ~S"
341 dspec))))))
342
343 (defimplementation find-definitions (name)
344 (let ((locations (dspec:find-name-locations dspec:*dspec-classes* name)))
345 (loop for (dspec location) in locations
346 collect (list dspec (make-dspec-location dspec location)))))
347
348
349 ;;; Compilation
350
351 (defmacro with-swank-compilation-unit ((location &rest options) &body body)
352 (lw:rebinding (location)
353 `(let ((compiler::*error-database* '()))
354 (with-compilation-unit ,options
355 ,@body
356 (signal-error-data-base compiler::*error-database* ,location)
357 (signal-undefined-functions compiler::*unknown-functions* ,location)))))
358
359 (defimplementation swank-compile-file (filename load-p)
360 (with-swank-compilation-unit (filename)
361 (compile-file filename :load load-p)))
362
363 (defimplementation call-with-compilation-hooks (function)
364 ;; #'pray instead of #'handler-bind
365 (funcall function))
366
367 (defun map-error-database (database fn)
368 (loop for (filename . defs) in database do
369 (loop for (dspec . conditions) in defs do
370 (dolist (c conditions)
371 (funcall fn filename dspec c)))))
372
373 (defun lispworks-severity (condition)
374 (cond ((not condition) :warning)
375 (t (etypecase condition
376 (error :error)
377 (style-warning :warning)
378 (warning :warning)))))
379
380 (defun signal-compiler-condition (message location condition)
381 (check-type message string)
382 (signal
383 (make-instance 'compiler-condition :message message
384 :severity (lispworks-severity condition)
385 :location location
386 :original-condition condition)))
387
388 (defun compile-from-temp-file (string filename)
389 (unwind-protect
390 (progn
391 (with-open-file (s filename :direction :output :if-exists :supersede)
392 (write-string string s)
393 (finish-output s))
394 (let ((binary-filename (compile-file filename :load t)))
395 (when binary-filename
396 (delete-file binary-filename))))
397 (delete-file filename)))
398
399 (defun dspec-buffer-position (dspec offset)
400 (etypecase dspec
401 (cons (let ((name (dspec:dspec-primary-name dspec)))
402 (typecase name
403 ((or symbol string)
404 (list :function-name (string name)))
405 (t (list :position offset)))))
406 (null (list :position offset))
407 (symbol (list :function-name (string dspec)))))
408
409 (defmacro with-fairly-standard-io-syntax (&body body)
410 "Like WITH-STANDARD-IO-SYNTAX but preserve *PACKAGE* and *READTABLE*."
411 (let ((package (gensym))
412 (readtable (gensym)))
413 `(let ((,package *package*)
414 (,readtable *readtable*))
415 (with-standard-io-syntax
416 (let ((*package* ,package)
417 (*readtable* ,readtable))
418 ,@body)))))
419
420 #-(or lispworks4.1 lispworks4.2) ; no dspec:parse-form-dspec prior to 4.3
421 (defun dspec-stream-position (stream dspec)
422 (with-fairly-standard-io-syntax
423 (loop (let* ((pos (file-position stream))
424 (form (read stream nil '#1=#:eof)))
425 (when (eq form '#1#)
426 (return nil))
427 (labels ((check-dspec (form)
428 (when (consp form)
429 (let ((operator (car form)))
430 (case operator
431 ((progn)
432 (mapcar #'check-dspec
433 (cdr form)))
434 ((eval-when locally macrolet symbol-macrolet)
435 (mapcar #'check-dspec
436 (cddr form)))
437 ((in-package)
438 (let ((package (find-package (second form))))
439 (when package
440 (setq *package* package))))
441 (otherwise
442 (let ((form-dspec (dspec:parse-form-dspec form)))
443 (when (dspec:dspec-equal dspec form-dspec)
444 (return pos)))))))))
445 (check-dspec form))))))
446
447 (defun dspec-file-position (file dspec)
448 (with-open-file (stream file)
449 (let ((pos
450 #-(or lispworks4.1 lispworks4.2)
451 (dspec-stream-position stream dspec)))
452 (if pos
453 (list :position (1+ pos) t)
454 (dspec-buffer-position dspec 1)))))
455
456 (defun emacs-buffer-location-p (location)
457 (and (consp location)
458 (eq (car location) :emacs-buffer)))
459
460 (defun make-dspec-location (dspec location &optional hints)
461 (etypecase location
462 ((or pathname string)
463 (multiple-value-bind (file err)
464 (ignore-errors (namestring (truename location)))
465 (if err
466 (list :error (princ-to-string err))
467 (make-location `(:file ,file)
468 (dspec-file-position file dspec)
469 hints))))
470 (symbol
471 `(:error ,(format nil "Cannot resolve location: ~S" location)))
472 ((satisfies emacs-buffer-location-p)
473 (destructuring-bind (_ buffer offset string) location
474 (declare (ignore _ string))
475 (make-location `(:buffer ,buffer)
476 (dspec-buffer-position dspec offset)
477 hints)))))
478
479 (defun make-dspec-progenitor-location (dspec location)
480 (let ((canon-dspec (dspec:canonicalize-dspec dspec)))
481 (make-dspec-location
482 (if canon-dspec
483 (if (dspec:local-dspec-p canon-dspec)
484 (dspec:dspec-progenitor canon-dspec)
485 canon-dspec)
486 nil)
487 location)))
488
489 (defun signal-error-data-base (database location)
490 (map-error-database
491 database
492 (lambda (filename dspec condition)
493 (declare (ignore filename))
494 (signal-compiler-condition
495 (format nil "~A" condition)
496 (make-dspec-progenitor-location dspec location)
497 condition))))
498
499 (defun signal-undefined-functions (htab filename)
500 (maphash (lambda (unfun dspecs)
501 (dolist (dspec dspecs)
502 (signal-compiler-condition
503 (format nil "Undefined function ~A" unfun)
504 (make-dspec-progenitor-location dspec filename)
505 nil)))
506 htab))
507
508 (defimplementation swank-compile-string (string &key buffer position directory)
509 (declare (ignore directory))
510 (assert buffer)
511 (assert position)
512 (let* ((location (list :emacs-buffer buffer position string))
513 (tmpname (hcl:make-temp-file nil "lisp")))
514 (with-swank-compilation-unit (location)
515 (compile-from-temp-file
516 (format nil "~S~%~A" `(eval-when (:compile-toplevel)
517 (setq dspec::*location* (list ,@location)))
518 string)
519 tmpname))))
520
521 ;;; xref
522
523 (defmacro defxref (name function)
524 `(defimplementation ,name (name)
525 (xref-results (,function name))))
526
527 (defxref who-calls hcl:who-calls)
528 (defxref who-macroexpands hcl:who-calls) ; macros are in the calls table too
529 (defxref list-callees hcl:calls-who)
530 (defxref list-callers list-callers-internal)
531
532 (defun list-callers-internal (name)
533 (let ((callers (make-array 100
534 :fill-pointer 0
535 :adjustable t)))
536 (hcl:sweep-all-objects
537 #'(lambda (object)
538 (when (and #+Harlequin-PC-Lisp (low:compiled-code-p object)
539 #-Harlequin-PC-Lisp (sys::callablep object)
540 (system::find-constant$funcallable name object))
541 (vector-push-extend object callers))))
542 ;; Delay dspec:object-dspec until after sweep-all-objects
543 ;; to reduce allocation problems.
544 (loop for object across callers
545 collect (if (symbolp object)
546 (list 'function object)
547 (or (dspec:object-dspec object) object)))))
548
549 ;; only for lispworks 4.2 and above
550 #-lispworks4.1
551 (progn
552 (defxref who-references hcl:who-references)
553 (defxref who-binds hcl:who-binds)
554 (defxref who-sets hcl:who-sets))
555
556 (defimplementation who-specializes (classname)
557 (let ((methods (clos:class-direct-methods (find-class classname))))
558 (xref-results (mapcar #'dspec:object-dspec methods))))
559
560 (defun xref-results (dspecs)
561 (flet ((frob-locs (dspec locs)
562 (cond (locs
563 (loop for (name loc) in locs
564 collect (list name (make-dspec-location name loc))))
565 (t `((,dspec (:error "Source location not available")))))))
566 (loop for dspec in dspecs
567 append (frob-locs dspec (dspec:dspec-definition-locations dspec)))))
568 ;;; Inspector
569 (defclass lispworks-inspector (inspector)
570 ())
571
572 (defimplementation make-default-inspector ()
573 (make-instance 'lispworks-inspector))
574
575 (defimplementation inspect-for-emacs ((o t) (inspector lispworks-inspector))
576 (declare (ignore inspector))
577 (lispworks-inspect o))
578
579 (defimplementation inspect-for-emacs ((o function)
580 (inspector lispworks-inspector))
581 (declare (ignore inspector))
582 (lispworks-inspect o))
583
584 (defun lispworks-inspect (o)
585 (multiple-value-bind (names values _getter _setter type)
586 (lw:get-inspector-values o nil)
587 (declare (ignore _getter _setter))
588 (values "A value."
589 (append
590 (label-value-line "Type" type)
591 (mapcan #'label-value-line names values)))))
592
593 ;;; Miscellaneous
594
595 (defimplementation quit-lisp ()
596 (lispworks:quit))
597
598 ;;; Multithreading
599
600 (defimplementation startup-multiprocessing ()
601 (mp:initialize-multiprocessing))
602
603 (defimplementation spawn (fn &key name)
604 (let ((mp:*process-initial-bindings*
605 (remove (find-package :cl)
606 mp:*process-initial-bindings*
607 :key (lambda (x) (symbol-package (car x))))))
608 (mp:process-run-function name () fn)))
609
610 (defvar *id-lock* (mp:make-lock))
611 (defvar *thread-id-counter* 0)
612
613 (defimplementation thread-id (thread)
614 (mp:with-lock (*id-lock*)
615 (or (getf (mp:process-plist thread) 'id)
616 (setf (getf (mp:process-plist thread) 'id)
617 (incf *thread-id-counter*)))))
618
619 (defimplementation find-thread (id)
620 (find id (mp:list-all-processes)
621 :key (lambda (p) (getf (mp:process-plist p) 'id))))
622
623 (defimplementation thread-name (thread)
624 (mp:process-name thread))
625
626 (defimplementation thread-status (thread)
627 (format nil "~A ~D"
628 (mp:process-whostate thread)
629 (mp:process-priority thread)))
630
631 (defimplementation make-lock (&key name)
632 (mp:make-lock :name name))
633
634 (defimplementation call-with-lock-held (lock function)
635 (mp:with-lock (lock) (funcall function)))
636
637 (defimplementation current-thread ()
638 mp:*current-process*)
639
640 (defimplementation all-threads ()
641 (mp:list-all-processes))
642
643 (defimplementation interrupt-thread (thread fn)
644 (mp:process-interrupt thread fn))
645
646 (defimplementation kill-thread (thread)
647 (mp:process-kill thread))
648
649 (defimplementation thread-alive-p (thread)
650 (mp:process-alive-p thread))
651
652 (defvar *mailbox-lock* (mp:make-lock))
653
654 (defun mailbox (thread)
655 (mp:with-lock (*mailbox-lock*)
656 (or (getf (mp:process-plist thread) 'mailbox)
657 (setf (getf (mp:process-plist thread) 'mailbox)
658 (mp:make-mailbox)))))
659
660 (defimplementation receive ()
661 (mp:mailbox-read (mailbox mp:*current-process*)))
662
663 (defimplementation send (thread object)
664 (mp:mailbox-send (mailbox thread) object))
665

  ViewVC Help
Powered by ViewVC 1.1.5