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

Contents of /slime/swank-lispworks.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5