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

Contents of /slime/swank-lispworks.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.153 - (hide annotations)
Fri Feb 8 14:11:34 2013 UTC (14 months, 1 week ago) by heller
Branch: MAIN
Changes since 1.152: +17 -1 lines
* swank-lispworks.lisp (frame-package): Implemented.
(function-name-package): New.
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 heller 1.149 (ef:encode-lisp-string string '(:utf-8 :eol-style :lf)))
69 heller 1.143
70     (defimplementation utf8-to-string (octets)
71 heller 1.149 (ef:decode-external-string octets '(:utf-8 :eol-style :lf)))
72 heller 1.143
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 sboukarev 1.152 (defimplementation type-specifier-p (symbol)
301     (or (ignore-errors
302     (subtypep nil symbol))
303     (not (eq (type-specifier-arglist symbol) :not-available))))
304    
305 heller 1.1 ;;; Debugging
306    
307 heller 1.66 (defclass slime-env (env:environment)
308     ((debugger-hook :initarg :debugger-hoook)))
309    
310     (defun slime-env (hook io-bindings)
311     (make-instance 'slime-env :name "SLIME Environment"
312     :io-bindings io-bindings
313     :debugger-hoook hook))
314    
315 heller 1.111 (defmethod env-internals:environment-display-notifier
316 heller 1.66 ((env slime-env) &key restarts condition)
317 heller 1.111 (declare (ignore restarts condition))
318 heller 1.113 (funcall (swank-sym :swank-debugger-hook) condition *debugger-hook*)
319     ;; nil
320 heller 1.111 )
321 heller 1.66
322     (defmethod env-internals:environment-display-debugger ((env slime-env))
323     *debug-io*)
324    
325 heller 1.111 (defmethod env-internals:confirm-p ((e slime-env) &optional msg &rest args)
326     (apply (swank-sym :y-or-n-p-in-emacs) msg args))
327    
328 heller 1.110 (defimplementation call-with-debugger-hook (hook fun)
329     (let ((*debugger-hook* hook))
330     (env:with-environment ((slime-env hook '()))
331     (funcall fun))))
332 heller 1.66
333 msimmons 1.100 (defimplementation install-debugger-globally (function)
334     (setq *debugger-hook* function)
335     (setf (env:environment) (slime-env function '())))
336    
337 heller 1.26 (defvar *sldb-top-frame*)
338 heller 1.1
339     (defun interesting-frame-p (frame)
340 heller 1.36 (cond ((or (dbg::call-frame-p frame)
341     (dbg::derived-call-frame-p frame)
342     (dbg::foreign-frame-p frame)
343     (dbg::interpreted-call-frame-p frame))
344     t)
345     ((dbg::catch-frame-p frame) dbg:*print-catch-frames*)
346     ((dbg::binding-frame-p frame) dbg:*print-binding-frames*)
347     ((dbg::handler-frame-p frame) dbg:*print-handler-frames*)
348     ((dbg::restart-frame-p frame) dbg:*print-restart-frames*)
349     (t nil)))
350 heller 1.1
351 heller 1.41 (defun nth-next-frame (frame n)
352     "Unwind FRAME N times."
353     (do ((frame frame (dbg::frame-next frame))
354     (i n (if (interesting-frame-p frame) (1- i) i)))
355 heller 1.48 ((or (not frame)
356     (and (interesting-frame-p frame) (zerop i)))
357     frame)))
358 heller 1.41
359     (defun nth-frame (index)
360     (nth-next-frame *sldb-top-frame* index))
361 heller 1.139
362 heller 1.41 (defun find-top-frame ()
363     "Return the most suitable top-frame for the debugger."
364 mbaringer 1.69 (or (do ((frame (dbg::debugger-stack-current-frame dbg::*debugger-stack*)
365     (nth-next-frame frame 1)))
366     ((or (null frame) ; no frame found!
367     (and (dbg::call-frame-p frame)
368     (eq (dbg::call-frame-function-name frame)
369     'invoke-debugger)))
370     (nth-next-frame frame 1)))
371 heller 1.151 ;; if we can't find a invoke-debugger frame, take any old frame
372     ;; at the top
373 mbaringer 1.69 (dbg::debugger-stack-current-frame dbg::*debugger-stack*)))
374 heller 1.41
375     (defimplementation call-with-debugging-environment (fn)
376     (dbg::with-debugger-stack ()
377     (let ((*sldb-top-frame* (find-top-frame)))
378     (funcall fn))))
379 heller 1.1
380 heller 1.30 (defimplementation compute-backtrace (start end)
381 heller 1.1 (let ((end (or end most-positive-fixnum))
382     (backtrace '()))
383     (do ((frame (nth-frame start) (dbg::frame-next frame))
384     (i start))
385     ((or (not frame) (= i end)) (nreverse backtrace))
386     (when (interesting-frame-p frame)
387     (incf i)
388 heller 1.121 (push frame backtrace)))))
389 heller 1.1
390 heller 1.36 (defun frame-actual-args (frame)
391 msimmons 1.141 (let ((*break-on-signals* nil)
392     (kind nil))
393     (loop for arg in (dbg::call-frame-arglist frame)
394     if (eq kind '&rest)
395     nconc (handler-case
396     (dbg::dbg-eval arg frame)
397     (error (e) (list (format nil "<~A>" arg))))
398     and do (loop-finish)
399     else
400     if (member arg '(&rest &optional &key))
401     do (setq kind arg)
402     else
403     nconc
404     (handler-case
405     (nconc (and (eq kind '&key)
406     (list (cond ((symbolp arg)
407     (intern (symbol-name arg) :keyword))
408     ((and (consp arg) (symbolp (car arg)))
409 heller 1.151 (intern (symbol-name (car arg))
410     :keyword))
411 msimmons 1.141 (t (caar arg)))))
412     (list (dbg::dbg-eval
413     (cond ((symbolp arg) arg)
414     ((and (consp arg) (symbolp (car arg)))
415     (car arg))
416     (t (cadar arg)))
417     frame)))
418     (error (e) (list (format nil "<~A>" arg)))))))
419 heller 1.36
420 heller 1.121 (defimplementation print-frame (frame stream)
421     (cond ((dbg::call-frame-p frame)
422 msimmons 1.141 (prin1 (cons (dbg::call-frame-function-name frame)
423     (frame-actual-args frame))
424     stream))
425 heller 1.121 (t (princ frame stream))))
426 heller 1.1
427 heller 1.47 (defun frame-vars (frame)
428     (first (dbg::frame-locals-format-list frame #'list 75 0)))
429    
430 lgorrie 1.17 (defimplementation frame-locals (n)
431 heller 1.29 (let ((frame (nth-frame n)))
432 heller 1.1 (if (dbg::call-frame-p frame)
433 heller 1.47 (mapcar (lambda (var)
434     (destructuring-bind (name value symbol location) var
435     (declare (ignore name location))
436     (list :name symbol :id 0
437     :value value)))
438     (frame-vars frame)))))
439    
440     (defimplementation frame-var-value (frame var)
441     (let ((frame (nth-frame frame)))
442     (destructuring-bind (_n value _s _l) (nth var (frame-vars frame))
443     (declare (ignore _n _s _l))
444     value)))
445 heller 1.1
446 heller 1.130 (defimplementation frame-source-location (frame)
447 heller 1.60 (let ((frame (nth-frame frame))
448     (callee (if (plusp frame) (nth-frame (1- frame)))))
449 heller 1.1 (if (dbg::call-frame-p frame)
450 heller 1.60 (let ((dspec (dbg::call-frame-function-name frame))
451     (cname (and (dbg::call-frame-p callee)
452 heller 1.139 (dbg::call-frame-function-name callee)))
453     (path (and (dbg::call-frame-p frame)
454     (dbg::call-frame-edit-path frame))))
455 heller 1.60 (if dspec
456 heller 1.139 (frame-location dspec cname path))))))
457 heller 1.26
458     (defimplementation eval-in-frame (form frame-number)
459     (let ((frame (nth-frame frame-number)))
460     (dbg::dbg-eval form frame)))
461    
462 heller 1.153 (defun function-name-package (name)
463     (typecase name
464     (null nil)
465     (symbol (symbol-package name))
466     ((cons (eql hcl:subfunction))
467     (destructuring-bind (name parent) (cdr name)
468     (declare (ignore name))
469     (function-name-package parent)))
470     ((cons (eql lw:top-level-form)) nil)
471     (t nil)))
472    
473     (defimplementation frame-package (frame-number)
474     (let ((frame (nth-frame frame-number)))
475     (if (dbg::call-frame-p frame)
476     (function-name-package (dbg::call-frame-function-name frame)))))
477    
478 heller 1.26 (defimplementation return-from-frame (frame-number form)
479     (let* ((frame (nth-frame frame-number))
480 heller 1.30 (return-frame (dbg::find-frame-for-return frame)))
481 heller 1.153 (dbg::dbg-return-from-call-frame frame form return-frame
482 heller 1.26 dbg::*debugger-stack*)))
483    
484     (defimplementation restart-frame (frame-number)
485     (let ((frame (nth-frame frame-number)))
486     (dbg::restart-frame frame :same-args t)))
487 heller 1.1
488 heller 1.109 (defimplementation disassemble-frame (frame-number)
489     (let* ((frame (nth-frame frame-number)))
490     (when (dbg::call-frame-p frame)
491     (let ((function (dbg::get-call-frame-function frame)))
492     (disassemble function)))))
493    
494 heller 1.18 ;;; Definition finding
495    
496 heller 1.139 (defun frame-location (dspec callee-name edit-path)
497 heller 1.60 (let ((infos (dspec:find-dspec-locations dspec)))
498     (cond (infos
499     (destructuring-bind ((rdspec location) &rest _) infos
500     (declare (ignore _))
501     (let ((name (and callee-name (symbolp callee-name)
502 heller 1.139 (string callee-name)))
503     (path (edit-path-to-cmucl-source-path edit-path)))
504     (make-dspec-location rdspec location
505     `(:call-site ,name :edit-path ,path)))))
506 heller 1.60 (t
507     (list :error (format nil "Source location not available for: ~S"
508     dspec))))))
509 heller 1.4
510 heller 1.139 ;; dbg::call-frame-edit-path is not documented but lets assume the
511     ;; binary representation of the integer EDIT-PATH should be
512     ;; interpreted as a sequence of CAR or CDR. #b1111010 is roughly the
513     ;; same as cadadddr. Something is odd with the highest bit.
514     (defun edit-path-to-cmucl-source-path (edit-path)
515     (and edit-path
516     (cons 0
517     (let ((n -1))
518     (loop for i from (1- (integer-length edit-path)) downto 0
519     if (logbitp i edit-path) do (incf n)
520     else collect (prog1 n (setq n 0)))))))
521    
522     ;; (edit-path-to-cmucl-source-path #b1111010) => (0 3 1)
523    
524 heller 1.33 (defimplementation find-definitions (name)
525 heller 1.26 (let ((locations (dspec:find-name-locations dspec:*dspec-classes* name)))
526 heller 1.33 (loop for (dspec location) in locations
527     collect (list dspec (make-dspec-location dspec location)))))
528 heller 1.6
529 msimmons 1.56
530 heller 1.18 ;;; Compilation
531    
532 msimmons 1.42 (defmacro with-swank-compilation-unit ((location &rest options) &body body)
533     (lw:rebinding (location)
534     `(let ((compiler::*error-database* '()))
535     (with-compilation-unit ,options
536 heller 1.120 (multiple-value-prog1 (progn ,@body)
537     (signal-error-data-base compiler::*error-database*
538     ,location)
539     (signal-undefined-functions compiler::*unknown-functions*
540     ,location))))))
541 msimmons 1.42
542 heller 1.128 (defimplementation swank-compile-file (input-file output-file
543 sboukarev 1.136 load-p external-format
544     &key policy)
545     (declare (ignore policy))
546 heller 1.128 (with-swank-compilation-unit (input-file)
547     (compile-file input-file
548     :output-file output-file
549     :load load-p
550 heller 1.119 :external-format external-format)))
551 msimmons 1.56
552 eweitz 1.70 (defvar *within-call-with-compilation-hooks* nil
553     "Whether COMPILE-FILE was called from within CALL-WITH-COMPILATION-HOOKS.")
554    
555     (defvar *undefined-functions-hash* nil
556     "Hash table to map info about undefined functions to pathnames.")
557    
558     (lw:defadvice (compile-file compile-file-and-collect-notes :around)
559     (pathname &rest rest)
560 msimmons 1.75 (multiple-value-prog1 (apply #'lw:call-next-advice pathname rest)
561 eweitz 1.70 (when *within-call-with-compilation-hooks*
562     (maphash (lambda (unfun dspecs)
563     (dolist (dspec dspecs)
564     (let ((unfun-info (list unfun dspec)))
565     (unless (gethash unfun-info *undefined-functions-hash*)
566     (setf (gethash unfun-info *undefined-functions-hash*)
567     pathname)))))
568     compiler::*unknown-functions*))))
569    
570 msimmons 1.56 (defimplementation call-with-compilation-hooks (function)
571 eweitz 1.70 (let ((compiler::*error-database* '())
572     (*undefined-functions-hash* (make-hash-table :test 'equal))
573     (*within-call-with-compilation-hooks* t))
574     (with-compilation-unit ()
575     (prog1 (funcall function)
576     (signal-error-data-base compiler::*error-database*)
577     (signal-undefined-functions compiler::*unknown-functions*)))))
578 heller 1.1
579     (defun map-error-database (database fn)
580     (loop for (filename . defs) in database do
581     (loop for (dspec . conditions) in defs do
582 heller 1.140 (dolist (c conditions)
583     (multiple-value-bind (condition path)
584     (if (consp c) (values (car c) (cdr c)) (values c nil))
585     (funcall fn filename dspec condition path))))))
586 heller 1.1
587     (defun lispworks-severity (condition)
588     (cond ((not condition) :warning)
589     (t (etypecase condition
590 heller 1.6 (error :error)
591 heller 1.1 (style-warning :warning)
592     (warning :warning)))))
593    
594     (defun signal-compiler-condition (message location condition)
595     (check-type message string)
596     (signal
597     (make-instance 'compiler-condition :message message
598     :severity (lispworks-severity condition)
599     :location location
600     :original-condition condition)))
601    
602 heller 1.118 (defvar *temp-file-format* '(:utf-8 :eol-style :lf))
603    
604 heller 1.1 (defun compile-from-temp-file (string filename)
605     (unwind-protect
606     (progn
607 heller 1.118 (with-open-file (s filename :direction :output
608     :if-exists :supersede
609     :external-format *temp-file-format*)
610    
611 heller 1.1 (write-string string s)
612     (finish-output s))
613 heller 1.120 (multiple-value-bind (binary-filename warnings? failure?)
614     (compile-file filename :load t
615     :external-format *temp-file-format*)
616     (declare (ignore warnings?))
617 heller 1.6 (when binary-filename
618 heller 1.120 (delete-file binary-filename))
619     (not failure?)))
620 heller 1.1 (delete-file filename)))
621    
622 heller 1.117 (defun dspec-function-name-position (dspec fallback)
623 heller 1.34 (etypecase dspec
624     (cons (let ((name (dspec:dspec-primary-name dspec)))
625 heller 1.37 (typecase name
626 heller 1.34 ((or symbol string)
627     (list :function-name (string name)))
628 heller 1.117 (t fallback))))
629     (null fallback)
630 heller 1.34 (symbol (list :function-name (string dspec)))))
631 heller 1.18
632 lgorrie 1.45 (defmacro with-fairly-standard-io-syntax (&body body)
633     "Like WITH-STANDARD-IO-SYNTAX but preserve *PACKAGE* and *READTABLE*."
634     (let ((package (gensym))
635     (readtable (gensym)))
636     `(let ((,package *package*)
637     (,readtable *readtable*))
638     (with-standard-io-syntax
639     (let ((*package* ,package)
640     (*readtable* ,readtable))
641     ,@body)))))
642    
643 heller 1.117 (defun skip-comments (stream)
644     (let ((pos0 (file-position stream)))
645     (cond ((equal (ignore-errors (list (read-delimited-list #\( stream)))
646     '(()))
647     (file-position stream (1- (file-position stream))))
648     (t (file-position stream pos0)))))
649    
650 heller 1.52 #-(or lispworks4.1 lispworks4.2) ; no dspec:parse-form-dspec prior to 4.3
651 msimmons 1.42 (defun dspec-stream-position (stream dspec)
652 lgorrie 1.45 (with-fairly-standard-io-syntax
653 heller 1.117 (loop (let* ((pos (progn (skip-comments stream) (file-position stream)))
654 lgorrie 1.44 (form (read stream nil '#1=#:eof)))
655     (when (eq form '#1#)
656     (return nil))
657     (labels ((check-dspec (form)
658     (when (consp form)
659     (let ((operator (car form)))
660     (case operator
661     ((progn)
662     (mapcar #'check-dspec
663     (cdr form)))
664     ((eval-when locally macrolet symbol-macrolet)
665     (mapcar #'check-dspec
666     (cddr form)))
667     ((in-package)
668     (let ((package (find-package (second form))))
669     (when package
670     (setq *package* package))))
671     (otherwise
672     (let ((form-dspec (dspec:parse-form-dspec form)))
673     (when (dspec:dspec-equal dspec form-dspec)
674     (return pos)))))))))
675     (check-dspec form))))))
676 msimmons 1.42
677 heller 1.52 (defun dspec-file-position (file dspec)
678 mbaringer 1.90 (let* ((*compile-file-pathname* (pathname file))
679     (*compile-file-truename* (truename *compile-file-pathname*))
680     (*load-pathname* *compile-file-pathname*)
681     (*load-truename* *compile-file-truename*))
682     (with-open-file (stream file)
683     (let ((pos
684     #-(or lispworks4.1 lispworks4.2)
685 msimmons 1.148 (ignore-errors (dspec-stream-position stream dspec))))
686 mbaringer 1.90 (if pos
687 heller 1.117 (list :position (1+ pos))
688     (dspec-function-name-position dspec `(:position 1)))))))
689 heller 1.52
690 heller 1.25 (defun emacs-buffer-location-p (location)
691     (and (consp location)
692     (eq (car location) :emacs-buffer)))
693    
694 heller 1.60 (defun make-dspec-location (dspec location &optional hints)
695 heller 1.52 (etypecase location
696     ((or pathname string)
697     (multiple-value-bind (file err)
698     (ignore-errors (namestring (truename location)))
699     (if err
700     (list :error (princ-to-string err))
701     (make-location `(:file ,file)
702 heller 1.60 (dspec-file-position file dspec)
703     hints))))
704 heller 1.52 (symbol
705     `(:error ,(format nil "Cannot resolve location: ~S" location)))
706     ((satisfies emacs-buffer-location-p)
707 heller 1.150 (destructuring-bind (_ buffer offset) location
708     (declare (ignore _))
709 heller 1.52 (make-location `(:buffer ,buffer)
710 heller 1.117 (dspec-function-name-position dspec `(:offset ,offset 0))
711 heller 1.60 hints)))))
712 heller 1.1
713 heller 1.140 (defun make-dspec-progenitor-location (dspec location edit-path)
714 msimmons 1.42 (let ((canon-dspec (dspec:canonicalize-dspec dspec)))
715     (make-dspec-location
716     (if canon-dspec
717     (if (dspec:local-dspec-p canon-dspec)
718     (dspec:dspec-progenitor canon-dspec)
719 heller 1.140 canon-dspec)
720     nil)
721     location
722     (if edit-path
723     (list :edit-path (edit-path-to-cmucl-source-path edit-path))))))
724 msimmons 1.42
725 eweitz 1.70 (defun signal-error-data-base (database &optional location)
726 heller 1.1 (map-error-database
727     database
728 heller 1.140 (lambda (filename dspec condition edit-path)
729 heller 1.1 (signal-compiler-condition
730     (format nil "~A" condition)
731 heller 1.140 (make-dspec-progenitor-location dspec (or location filename) edit-path)
732 heller 1.1 condition))))
733    
734 eweitz 1.71 (defun unmangle-unfun (symbol)
735     "Converts symbols like 'SETF::|\"CL-USER\" \"GET\"| to
736     function names like \(SETF GET)."
737 heller 1.93 (cond ((sys::setf-symbol-p symbol)
738     (sys::setf-pair-from-underlying-name symbol))
739     (t symbol)))
740    
741 eweitz 1.70 (defun signal-undefined-functions (htab &optional filename)
742 heller 1.1 (maphash (lambda (unfun dspecs)
743     (dolist (dspec dspecs)
744     (signal-compiler-condition
745 eweitz 1.71 (format nil "Undefined function ~A" (unmangle-unfun unfun))
746 heller 1.140 (make-dspec-progenitor-location
747     dspec
748     (or filename
749     (gethash (list unfun dspec) *undefined-functions-hash*))
750     nil)
751 heller 1.1 nil)))
752     htab))
753 heller 1.2
754 heller 1.126 (defimplementation swank-compile-string (string &key buffer position filename
755     policy)
756     (declare (ignore filename policy))
757 heller 1.1 (assert buffer)
758     (assert position)
759 heller 1.150 (let* ((location (list :emacs-buffer buffer position))
760 heller 1.26 (tmpname (hcl:make-temp-file nil "lisp")))
761 msimmons 1.42 (with-swank-compilation-unit (location)
762 heller 1.26 (compile-from-temp-file
763 heller 1.68 (with-output-to-string (s)
764     (let ((*print-radix* t))
765     (print `(eval-when (:compile-toplevel)
766     (setq dspec::*location* (list ,@location)))
767     s))
768     (write-string string s))
769 msimmons 1.42 tmpname))))
770 heller 1.1
771 heller 1.3 ;;; xref
772    
773 heller 1.31 (defmacro defxref (name function)
774     `(defimplementation ,name (name)
775     (xref-results (,function name))))
776    
777     (defxref who-calls hcl:who-calls)
778 msimmons 1.42 (defxref who-macroexpands hcl:who-calls) ; macros are in the calls table too
779 heller 1.67 (defxref calls-who hcl:calls-who)
780 msimmons 1.42 (defxref list-callers list-callers-internal)
781 sboukarev 1.137 (defxref list-callees list-callees-internal)
782 msimmons 1.42
783     (defun list-callers-internal (name)
784     (let ((callers (make-array 100
785     :fill-pointer 0
786     :adjustable t)))
787     (hcl:sweep-all-objects
788     #'(lambda (object)
789     (when (and #+Harlequin-PC-Lisp (low:compiled-code-p object)
790 msimmons 1.138 #+Harlequin-Unix-Lisp (sys:callablep object)
791 heller 1.151 #-(or Harlequin-PC-Lisp Harlequin-Unix-Lisp)
792     (sys:compiled-code-p object)
793 msimmons 1.42 (system::find-constant$funcallable name object))
794     (vector-push-extend object callers))))
795     ;; Delay dspec:object-dspec until after sweep-all-objects
796     ;; to reduce allocation problems.
797     (loop for object across callers
798     collect (if (symbolp object)
799     (list 'function object)
800 heller 1.63 (or (dspec:object-dspec object) object)))))
801 heller 1.36
802 sboukarev 1.137 (defun list-callees-internal (name)
803 msimmons 1.138 (let ((callees '()))
804     (system::find-constant$funcallable
805     'junk name
806     :test #'(lambda (junk constant)
807     (declare (ignore junk))
808     (when (and (symbolp constant)
809     (fboundp constant))
810     (pushnew (list 'function constant) callees :test 'equal))
811     ;; Return nil so we iterate over all constants.
812     nil))
813     callees))
814 sboukarev 1.137
815 heller 1.37 ;; only for lispworks 4.2 and above
816     #-lispworks4.1
817     (progn
818     (defxref who-references hcl:who-references)
819     (defxref who-binds hcl:who-binds)
820     (defxref who-sets hcl:who-sets))
821    
822 heller 1.36 (defimplementation who-specializes (classname)
823     (let ((methods (clos:class-direct-methods (find-class classname))))
824     (xref-results (mapcar #'dspec:object-dspec methods))))
825 heller 1.31
826     (defun xref-results (dspecs)
827 heller 1.63 (flet ((frob-locs (dspec locs)
828     (cond (locs
829     (loop for (name loc) in locs
830     collect (list name (make-dspec-location name loc))))
831     (t `((,dspec (:error "Source location not available")))))))
832     (loop for dspec in dspecs
833     append (frob-locs dspec (dspec:dspec-definition-locations dspec)))))
834 heller 1.66
835 heller 1.25 ;;; Inspector
836 mbaringer 1.58
837 heller 1.95 (defmethod emacs-inspect ((o t))
838 heller 1.60 (lispworks-inspect o))
839    
840 heller 1.95 (defmethod emacs-inspect ((o function))
841 heller 1.60 (lispworks-inspect o))
842    
843 heller 1.82 ;; FIXME: slot-boundp-using-class in LW works with names so we can't
844     ;; use our method in swank.lisp.
845 heller 1.95 (defmethod emacs-inspect ((o standard-object))
846 heller 1.82 (lispworks-inspect o))
847    
848 heller 1.60 (defun lispworks-inspect (o)
849 heller 1.25 (multiple-value-bind (names values _getter _setter type)
850     (lw:get-inspector-values o nil)
851     (declare (ignore _getter _setter))
852 heller 1.60 (append
853     (label-value-line "Type" type)
854 msimmons 1.91 (loop for name in names
855     for value in values
856 heller 1.96 append (label-value-line name value)))))
857 heller 1.25
858 lgorrie 1.46 ;;; Miscellaneous
859    
860     (defimplementation quit-lisp ()
861     (lispworks:quit))
862    
863 heller 1.66 ;;; Tracing
864    
865     (defun parse-fspec (fspec)
866     "Return a dspec for FSPEC."
867     (ecase (car fspec)
868 heller 1.67 ((:defmethod) `(method ,(cdr fspec)))))
869 heller 1.66
870     (defun tracedp (dspec)
871     (member dspec (eval '(trace)) :test #'equal))
872    
873 heller 1.67 (defun toggle-trace-aux (dspec)
874 heller 1.66 (cond ((tracedp dspec)
875     (eval `(untrace ,dspec))
876     (format nil "~S is now untraced." dspec))
877     (t
878     (eval `(trace (,dspec)))
879     (format nil "~S is now traced." dspec))))
880    
881 heller 1.67 (defimplementation toggle-trace (fspec)
882     (toggle-trace-aux (parse-fspec fspec)))
883 heller 1.66
884 heller 1.16 ;;; Multithreading
885    
886 mbaringer 1.86 (defimplementation initialize-multiprocessing (continuation)
887 heller 1.87 (cond ((not mp::*multiprocessing*)
888     (push (list "Initialize SLIME" '() continuation)
889     mp:*initial-processes*)
890     (mp:initialize-multiprocessing))
891     (t (funcall continuation))))
892 heller 1.16
893 heller 1.18 (defimplementation spawn (fn &key name)
894 heller 1.127 (mp:process-run-function name () fn))
895 heller 1.48
896     (defvar *id-lock* (mp:make-lock))
897     (defvar *thread-id-counter* 0)
898    
899     (defimplementation thread-id (thread)
900     (mp:with-lock (*id-lock*)
901     (or (getf (mp:process-plist thread) 'id)
902     (setf (getf (mp:process-plist thread) 'id)
903     (incf *thread-id-counter*)))))
904    
905     (defimplementation find-thread (id)
906     (find id (mp:list-all-processes)
907     :key (lambda (p) (getf (mp:process-plist p) 'id))))
908 heller 1.16
909 heller 1.21 (defimplementation thread-name (thread)
910     (mp:process-name thread))
911 heller 1.16
912 heller 1.21 (defimplementation thread-status (thread)
913     (format nil "~A ~D"
914     (mp:process-whostate thread)
915     (mp:process-priority thread)))
916 heller 1.16
917 heller 1.18 (defimplementation make-lock (&key name)
918 heller 1.16 (mp:make-lock :name name))
919    
920 heller 1.18 (defimplementation call-with-lock-held (lock function)
921 heller 1.16 (mp:with-lock (lock) (funcall function)))
922 heller 1.20
923     (defimplementation current-thread ()
924     mp:*current-process*)
925 heller 1.21
926     (defimplementation all-threads ()
927     (mp:list-all-processes))
928 heller 1.20
929     (defimplementation interrupt-thread (thread fn)
930     (mp:process-interrupt thread fn))
931 heller 1.25
932     (defimplementation kill-thread (thread)
933     (mp:process-kill thread))
934 heller 1.27
935     (defimplementation thread-alive-p (thread)
936     (mp:process-alive-p thread))
937 heller 1.20
938 heller 1.102 (defstruct (mailbox (:conc-name mailbox.))
939     (mutex (mp:make-lock :name "thread mailbox"))
940     (queue '() :type list))
941    
942 heller 1.20 (defvar *mailbox-lock* (mp:make-lock))
943    
944     (defun mailbox (thread)
945     (mp:with-lock (*mailbox-lock*)
946     (or (getf (mp:process-plist thread) 'mailbox)
947     (setf (getf (mp:process-plist thread) 'mailbox)
948 heller 1.102 (make-mailbox)))))
949 heller 1.20
950 heller 1.112 (defimplementation receive-if (test &optional timeout)
951 heller 1.102 (let* ((mbox (mailbox mp:*current-process*))
952     (lock (mailbox.mutex mbox)))
953 heller 1.112 (assert (or (not timeout) (eq timeout t)))
954 heller 1.102 (loop
955 heller 1.106 (check-slime-interrupts)
956     (mp:with-lock (lock "receive-if/try")
957     (let* ((q (mailbox.queue mbox))
958     (tail (member-if test q)))
959     (when tail
960     (setf (mailbox.queue mbox) (nconc (ldiff q tail) (cdr tail)))
961     (return (car tail)))))
962 heller 1.112 (when (eq timeout t) (return (values nil t)))
963 heller 1.106 (mp:process-wait-with-timeout
964 heller 1.125 "receive-if" 0.3 (lambda () (some test (mailbox.queue mbox)))))))
965 heller 1.102
966     (defimplementation send (thread message)
967     (let ((mbox (mailbox thread)))
968 heller 1.106 (mp:with-lock ((mailbox.mutex mbox))
969     (setf (mailbox.queue mbox)
970     (nconc (mailbox.queue mbox) (list message))))))
971 heller 1.3
972 heller 1.147 (let ((alist '())
973     (lock (mp:make-lock :name "register-thread")))
974    
975     (defimplementation register-thread (name thread)
976     (declare (type symbol name))
977     (mp:with-lock (lock)
978     (etypecase thread
979     (null
980     (setf alist (delete name alist :key #'car)))
981     (mp:process
982     (let ((probe (assoc name alist)))
983     (cond (probe (setf (cdr probe) thread))
984     (t (setf alist (acons name thread alist))))))))
985     nil)
986    
987     (defimplementation find-registered (name)
988     (mp:with-lock (lock)
989     (cdr (assoc name alist)))))
990    
991    
992 heller 1.127 (defimplementation set-default-initial-binding (var form)
993     (setq mp:*process-initial-bindings*
994     (acons var `(eval (quote ,form))
995     mp:*process-initial-bindings* )))
996    
997 heller 1.131 (defimplementation thread-attributes (thread)
998     (list :priority (mp:process-priority thread)
999     :idle (mp:process-idle-time thread)))
1000    
1001 heller 1.66 ;;; Some intergration with the lispworks environment
1002    
1003 heller 1.74 (defun swank-sym (name) (find-symbol (string name) :swank))
1004 mkoeppe 1.77
1005 eweitz 1.89
1006     ;;;; Weak hashtables
1007    
1008     (defimplementation make-weak-key-hash-table (&rest args)
1009     (apply #'make-hash-table :weak-kind :key args))
1010    
1011     (defimplementation make-weak-value-hash-table (&rest args)
1012     (apply #'make-hash-table :weak-kind :value args))

  ViewVC Help
Powered by ViewVC 1.1.5