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

Contents of /slime/swank-abcl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.59 - (show annotations)
Sun Oct 19 20:03:22 2008 UTC (5 years, 6 months ago) by heller
Branch: MAIN
Changes since 1.58: +6 -5 lines
* swank-abcl.lisp (handle-compiler-warning): Report source
location position when we can.
Use NAMESTRING for *compile-filename*.
1 ;;;; -*- indent-tabs-mode: nil; outline-regexp: ";;;;;*"; -*-
2 ;;;
3 ;;; swank-abcl.lisp --- Armedbear CL specific code for SLIME.
4 ;;;
5 ;;; Adapted from swank-acl.lisp, Andras Simon, 2004
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 :collect) ;just so that it doesn't spoil the flying letters
15 (require :pprint))
16
17 (defun sys::break (&optional (format-control "BREAK called")
18 &rest format-arguments)
19 (let ((*saved-backtrace* (backtrace-as-list-ignoring-swank-calls)))
20 (with-simple-restart (continue "Return from BREAK.")
21 (invoke-debugger
22 (sys::%make-condition 'simple-condition
23 (list :format-control format-control
24 :format-arguments format-arguments))))
25 nil))
26
27 (defimplementation make-output-stream (write-string)
28 (ext:make-slime-output-stream write-string))
29
30 (defimplementation make-input-stream (read-string)
31 (ext:make-slime-input-stream read-string
32 (make-synonym-stream '*standard-output*)))
33
34 (defimplementation call-with-compilation-hooks (function)
35 (funcall function))
36
37 ;;; swank-mop
38
39 ;;dummies and definition
40
41 (defclass standard-slot-definition ()())
42
43 ;(defun class-finalized-p (class) t)
44
45 (defun slot-definition-documentation (slot) #+nil (documentation slot 't))
46 (defun slot-definition-type (slot) t)
47 (defun class-prototype (class))
48 (defun generic-function-declarations (gf))
49 (defun specializer-direct-methods (spec) (mop::class-direct-methods spec))
50
51 (defun slot-definition-name (slot)
52 (mop::%slot-definition-name slot))
53
54 (defun class-slots (class)
55 (mop::%class-slots class))
56
57 (defun method-generic-function (method)
58 (mop::%method-generic-function method))
59
60 (defun method-function (method)
61 (mop::%method-function method))
62
63 (defun slot-boundp-using-class (class object slotdef)
64 (system::slot-boundp object (slot-definition-name slotdef)))
65
66 (defun slot-value-using-class (class object slotdef)
67 (system::slot-value object (slot-definition-name slotdef)))
68
69 (import-to-swank-mop
70 '( ;; classes
71 cl:standard-generic-function
72 standard-slot-definition ;;dummy
73 cl:method
74 cl:standard-class
75 ;; standard-class readers
76 mop::class-default-initargs
77 mop::class-direct-default-initargs
78 mop::class-direct-slots
79 mop::class-direct-subclasses
80 mop::class-direct-superclasses
81 mop::eql-specializer
82 mop::class-finalized-p
83 cl:class-name
84 mop::class-precedence-list
85 class-prototype ;;dummy
86 class-slots
87 specializer-direct-methods
88 ;; eql-specializer accessors
89 mop::eql-specializer-object
90 ;; generic function readers
91 mop::generic-function-argument-precedence-order
92 generic-function-declarations ;;dummy
93 mop::generic-function-lambda-list
94 mop::generic-function-methods
95 mop::generic-function-method-class
96 mop::generic-function-method-combination
97 mop::generic-function-name
98 ;; method readers
99 method-generic-function
100 method-function
101 mop::method-lambda-list
102 mop::method-specializers
103 mop::method-qualifiers
104 ;; slot readers
105 mop::slot-definition-allocation
106 slot-definition-documentation ;;dummy
107 mop::slot-definition-initargs
108 mop::slot-definition-initform
109 mop::slot-definition-initfunction
110 slot-definition-name
111 slot-definition-type ;;dummy
112 mop::slot-definition-readers
113 mop::slot-definition-writers
114 slot-boundp-using-class
115 slot-value-using-class
116 ))
117
118 ;;;; TCP Server
119
120
121 (defimplementation preferred-communication-style ()
122 nil)
123
124 (defimplementation create-socket (host port)
125 (ext:make-server-socket port))
126
127 (defimplementation local-port (socket)
128 (java:jcall (java:jmethod "java.net.ServerSocket" "getLocalPort") socket))
129
130 (defimplementation close-socket (socket)
131 (ext:server-socket-close socket))
132
133 (defimplementation accept-connection (socket
134 &key external-format buffering timeout)
135 (declare (ignore buffering timeout external-format))
136 (ext:get-socket-stream (ext:socket-accept socket)))
137
138 ;;;; Unix signals
139
140 (defimplementation call-without-interrupts (fn)
141 (funcall fn))
142
143 (defimplementation getpid ()
144 (handler-case
145 (let* ((runtime
146 (java:jstatic "getRuntime" "java.lang.Runtime"))
147 (command
148 (java:jnew-array-from-array
149 "java.lang.String" #("sh" "-c" "echo $PPID")))
150 (runtime-exec-jmethod
151 ;; Complicated because java.lang.Runtime.exec() is
152 ;; overloaded on a non-primitive type (array of
153 ;; java.lang.String), so we have to use the actual
154 ;; parameter instance to get java.lang.Class
155 (java:jmethod "java.lang.Runtime" "exec"
156 (java:jcall
157 (java:jmethod "java.lang.Object" "getClass")
158 command)))
159 (process
160 (java:jcall runtime-exec-jmethod runtime command))
161 (output
162 (java:jcall (java:jmethod "java.lang.Process" "getInputStream")
163 process)))
164 (java:jcall (java:jmethod "java.lang.Process" "waitFor")
165 process)
166 (loop :with b :do
167 (setq b
168 (java:jcall (java:jmethod "java.io.InputStream" "read")
169 output))
170 :until (member b '(-1 #x0a)) ; Either EOF or LF
171 :collecting (code-char b) :into result
172 :finally (return
173 (parse-integer (coerce result 'string)))))
174 (t () 0)))
175
176 (defimplementation lisp-implementation-type-name ()
177 "armedbear")
178
179 (defimplementation set-default-directory (directory)
180 (let ((dir (sys::probe-directory directory)))
181 (when dir (setf *default-pathname-defaults* dir))
182 (namestring dir)))
183
184
185 ;;;; Misc
186
187 (defimplementation arglist (fun)
188 (cond ((symbolp fun)
189 (multiple-value-bind (arglist present) (sys::arglist fun)
190 (if present arglist :not-available)))
191 (t :not-available)))
192
193 (defimplementation function-name (function)
194 (nth-value 2 (function-lambda-expression function)))
195
196 (defimplementation macroexpand-all (form)
197 (macroexpand form))
198
199 (defimplementation describe-symbol-for-emacs (symbol)
200 (let ((result '()))
201 (flet ((doc (kind &optional (sym symbol))
202 (or (documentation sym kind) :not-documented))
203 (maybe-push (property value)
204 (when value
205 (setf result (list* property value result)))))
206 (maybe-push
207 :variable (when (boundp symbol)
208 (doc 'variable)))
209 (maybe-push
210 :function (if (fboundp symbol)
211 (doc 'function)))
212 (maybe-push
213 :class (if (find-class symbol nil)
214 (doc 'class)))
215 result)))
216
217
218 (defimplementation describe-definition (symbol namespace)
219 (ecase namespace
220 (:variable
221 (describe symbol))
222 ((:function :generic-function)
223 (describe (symbol-function symbol)))
224 (:class
225 (describe (find-class symbol)))))
226
227 (defimplementation describe-definition (symbol namespace)
228 (ecase namespace
229 (:variable
230 (describe symbol))
231 ((:function :generic-function)
232 (describe (symbol-function symbol)))
233 (:class
234 (describe (find-class symbol)))))
235
236
237 ;;;; Debugger
238
239 (defvar *sldb-topframe*)
240
241 (defun backtrace-as-list-ignoring-swank-calls ()
242 (let ((list (ext:backtrace-as-list)))
243 (subseq list (1+ (or (position (intern "SWANK-DEBUGGER-HOOK" 'swank) list :key 'car) -1)))))
244
245 (defimplementation call-with-debugging-environment (debugger-loop-fn)
246 (let ((*sldb-topframe* (car (backtrace-as-list-ignoring-swank-calls)) #+nil (excl::int-newest-frame)))
247 (funcall debugger-loop-fn)))
248
249 (defun nth-frame (index)
250 (nth index (backtrace-as-list-ignoring-swank-calls)))
251
252 (defimplementation compute-backtrace (start end)
253 (let ((end (or end most-positive-fixnum)))
254 (loop for f in (subseq (backtrace-as-list-ignoring-swank-calls) start end)
255 collect f)))
256
257 (defimplementation print-frame (frame stream)
258 (write-string (string-trim '(#\space #\newline)
259 (prin1-to-string frame))
260 stream))
261
262 (defimplementation frame-locals (index)
263 `(,(list :name "??" :id 0 :value "??")))
264
265 (defimplementation frame-catch-tags (index)
266 (declare (ignore index))
267 nil)
268
269 #+nil
270 (defimplementation disassemble-frame (index)
271 (disassemble (debugger:frame-function (nth-frame index))))
272
273 (defimplementation frame-source-location-for-emacs (index)
274 (list :error (format nil "Cannot find source for frame: ~A"
275 (nth-frame index))))
276
277 #+nil
278 (defimplementation eval-in-frame (form frame-number)
279 (debugger:eval-form-in-context
280 form
281 (debugger:environment-of-frame (nth-frame frame-number))))
282
283 #+nil
284 (defimplementation return-from-frame (frame-number form)
285 (let ((frame (nth-frame frame-number)))
286 (multiple-value-call #'debugger:frame-return
287 frame (debugger:eval-form-in-context
288 form
289 (debugger:environment-of-frame frame)))))
290
291 ;;; XXX doesn't work for frames with arguments
292 #+nil
293 (defimplementation restart-frame (frame-number)
294 (let ((frame (nth-frame frame-number)))
295 (debugger:frame-retry frame (debugger:frame-function frame))))
296
297 ;;;; Compiler hooks
298
299 (defvar *buffer-name* nil)
300 (defvar *buffer-start-position*)
301 (defvar *buffer-string*)
302 (defvar *compile-filename*)
303
304 (in-package :swank-backend)
305
306 (defun handle-compiler-warning (condition)
307 (let ((loc (when (and jvm::*compile-file-pathname*
308 system::*source-position*)
309 (cons jvm::*compile-file-pathname* system::*source-position*))))
310 ;; filter condition signaled more than once.
311 (unless (member condition *abcl-signaled-conditions*)
312 (push condition *abcl-signaled-conditions*)
313 (signal (make-condition
314 'compiler-condition
315 :original-condition condition
316 :severity :warning
317 :message (format nil "~A" condition)
318 :location (cond (*buffer-name*
319 (make-location
320 (list :buffer *buffer-name*)
321 (list :offset *buffer-start-position* 0)))
322 (loc
323 (destructuring-bind (file . pos) loc
324 (make-location
325 (list :file (namestring (truename file)))
326 (list :position (1+ pos)))))
327 (t
328 (make-location
329 (list :file (namestring *compile-filename*))
330 (list :position 1)))))))))
331
332 (defvar *abcl-signaled-conditions*)
333
334 (defimplementation swank-compile-file (filename load-p external-format)
335 (declare (ignore external-format))
336 (let ((jvm::*resignal-compiler-warnings* t)
337 (*abcl-signaled-conditions* nil))
338 (handler-bind ((warning #'handle-compiler-warning))
339 (let ((*buffer-name* nil)
340 (*compile-filename* filename))
341 (multiple-value-bind (fn warn fail) (compile-file filename)
342 (values fn warn
343 (or fail
344 (and load-p
345 (not (load fn))))))))))
346
347 (defimplementation swank-compile-string (string &key buffer position directory
348 debug)
349 (declare (ignore directory debug))
350 (let ((jvm::*resignal-compiler-warnings* t)
351 (*abcl-signaled-conditions* nil))
352 (handler-bind ((warning #'handle-compiler-warning))
353 (let ((*buffer-name* buffer)
354 (*buffer-start-position* position)
355 (*buffer-string* string))
356 (funcall (compile nil (read-from-string
357 (format nil "(~S () ~A)" 'lambda string))))
358 t))))
359
360 #|
361 ;;;; Definition Finding
362
363 (defun find-fspec-location (fspec type)
364 (let ((file (excl::fspec-pathname fspec type)))
365 (etypecase file
366 (pathname
367 (let ((start (scm:find-definition-in-file fspec type file)))
368 (make-location (list :file (namestring (truename file)))
369 (if start
370 (list :position (1+ start))
371 (list :function-name (string fspec))))))
372 ((member :top-level)
373 (list :error (format nil "Defined at toplevel: ~A" fspec)))
374 (null
375 (list :error (format nil "Unkown source location for ~A" fspec))))))
376
377 (defun fspec-definition-locations (fspec)
378 (let ((defs (excl::find-multiple-definitions fspec)))
379 (loop for (fspec type) in defs
380 collect (list fspec (find-fspec-location fspec type)))))
381
382 (defimplementation find-definitions (symbol)
383 (fspec-definition-locations symbol))
384
385 |#
386
387 (defun source-location (symbol)
388 (when (pathnamep (ext:source-pathname symbol))
389 `(((,symbol)
390 (:location
391 (:file ,(namestring (ext:source-pathname symbol)))
392 (:position ,(or (ext:source-file-position symbol) 1))
393 (:align t))))))
394
395
396 (defimplementation find-definitions (symbol)
397 (source-location symbol))
398
399 #|
400 Uncomment this if you have patched xref.lisp, as in
401 http://article.gmane.org/gmane.lisp.slime.devel/2425
402 Also, make sure that xref.lisp is loaded by modifying the armedbear
403 part of *sysdep-pathnames* in swank.loader.lisp.
404
405 ;;;; XREF
406 (setq pxref:*handle-package-forms* '(cl:in-package))
407
408 (defmacro defxref (name function)
409 `(defimplementation ,name (name)
410 (xref-results (,function name))))
411
412 (defxref who-calls pxref:list-callers)
413 (defxref who-references pxref:list-readers)
414 (defxref who-binds pxref:list-setters)
415 (defxref who-sets pxref:list-setters)
416 (defxref list-callers pxref:list-callers)
417 (defxref list-callees pxref:list-callees)
418
419 (defun xref-results (symbols)
420 (let ((xrefs '()))
421 (dolist (symbol symbols)
422 (push (list symbol (cadar (source-location symbol))) xrefs))
423 xrefs))
424 |#
425
426 ;;;; Inspecting
427
428 (defmethod emacs-inspect ((slot mop::slot-definition))
429 `("Name: " (:value ,(mop::%slot-definition-name slot))
430 (:newline)
431 "Documentation:" (:newline)
432 ,@(when (slot-definition-documentation slot)
433 `((:value ,(slot-definition-documentation slot)) (:newline)))
434 "Initialization:" (:newline)
435 " Args: " (:value ,(mop::%slot-definition-initargs slot)) (:newline)
436 " Form: " ,(if (mop::%slot-definition-initfunction slot)
437 `(:value ,(mop::%slot-definition-initform slot))
438 "#<unspecified>") (:newline)
439 " Function: " (:value ,(mop::%slot-definition-initfunction slot))
440 (:newline)))
441
442 (defmethod emacs-inspect ((f function))
443 `(,@(when (function-name f)
444 `("Name: "
445 ,(princ-to-string (function-name f)) (:newline)))
446 ,@(multiple-value-bind (args present)
447 (sys::arglist f)
448 (when present `("Argument list: " ,(princ-to-string args) (:newline))))
449 (:newline)
450 #+nil,@(when (documentation f t)
451 `("Documentation:" (:newline) ,(documentation f t) (:newline)))
452 ,@(when (function-lambda-expression f)
453 `("Lambda expression:"
454 (:newline) ,(princ-to-string (function-lambda-expression f)) (:newline)))))
455
456 #|
457
458 (defmethod emacs-inspect ((o t))
459 (let* ((class (class-of o))
460 (slots (mop::class-slots class)))
461 (mapcar (lambda (slot)
462 (let ((name (mop::slot-definition-name slot)))
463 (cons (princ-to-string name)
464 (slot-value o name))))
465 slots)))
466 |#
467
468 ;;;; Multithreading
469
470 (defimplementation startup-multiprocessing ()
471 #+nil(mp:start-scheduler))
472
473 (defimplementation spawn (fn &key name)
474 (ext:make-thread (lambda () (funcall fn)) :name name))
475
476 (defvar *thread-props-lock* (ext:make-thread-lock))
477
478 (defvar *thread-props* (make-hash-table) ; should be a weak table
479 "A hashtable mapping threads to a plist.")
480
481 (defvar *thread-id-counter* 0)
482
483 (defimplementation thread-id (thread)
484 (ext:with-thread-lock (*thread-props-lock*)
485 (or (getf (gethash thread *thread-props*) 'id)
486 (setf (getf (gethash thread *thread-props*) 'id)
487 (incf *thread-id-counter*)))))
488
489 (defimplementation find-thread (id)
490 (find id (all-threads)
491 :key (lambda (thread)
492 (getf (gethash thread *thread-props*) 'id))))
493
494 (defimplementation thread-name (thread)
495 (ext:thread-name thread))
496
497 (defimplementation thread-status (thread)
498 (format nil "Thread is ~:[dead~;alive~]" (ext:thread-alive-p thread)))
499
500 (defimplementation make-lock (&key name)
501 (ext:make-thread-lock))
502
503 (defimplementation call-with-lock-held (lock function)
504 (ext:with-thread-lock (lock) (funcall function)))
505
506 (defimplementation current-thread ()
507 (ext:current-thread))
508
509 (defimplementation all-threads ()
510 (copy-list (ext:mapcar-threads #'identity)))
511
512 (defimplementation interrupt-thread (thread fn)
513 (ext:interrupt-thread thread fn))
514
515 (defimplementation kill-thread (thread)
516 (ext:destroy-thread thread))
517
518 (defstruct mailbox
519 (mutex (ext:make-mutex))
520 (queue '()))
521
522 (defun mailbox (thread)
523 "Return THREAD's mailbox."
524 (ext:with-thread-lock (*thread-props-lock*)
525 (or (getf (gethash thread *thread-props*) 'mailbox)
526 (setf (getf (gethash thread *thread-props*) 'mailbox)
527 (make-mailbox)))))
528
529 (defimplementation send (thread object)
530 (let ((mbox (mailbox thread)))
531 (ext:with-mutex ((mailbox-mutex mbox))
532 (setf (mailbox-queue mbox)
533 (nconc (mailbox-queue mbox) (list message))))))
534
535 #+(or)
536 (defimplementation receive-if (thread &optional timeout)
537 (let* ((mbox (mailbox (current-thread))))
538 (assert (or (not timeout) (eq timeout t)))
539 (loop
540 (check-slime-interrupts)
541 (ext:with-mutex ((mailbox-mutex mbox))
542 (let* ((q (mailbox-queue mbox))
543 (tail (member-if test q)))
544 (when tail
545 (setf (mailbox-queue mbox) (nconc (ldiff q tail) (cdr tail)))
546 (return (car tail))))
547 (when (eq timeout t) (return (values nil t)))
548 ;;(java:jcall (java:jmethod "java.lang.Object" "wait")
549 ;; (mailbox-mutex mbox) 1000)
550 ))))
551
552 (defimplementation quit-lisp ()
553 (ext:exit))
554
555 ;; WORKAROUND: call/initialize accessors at load time
556 (let ((c (make-condition 'compiler-condition
557 :original-condition nil
558 :severity ':note :message "" :location nil))
559 (slots `(severity message short-message references location)))
560 (dolist (slot slots)
561 (funcall slot c)))

  ViewVC Help
Powered by ViewVC 1.1.5