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

Contents of /slime/swank-lispworks.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5