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

Contents of /slime/swank-lispworks.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5