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

Contents of /slime/swank-lispworks.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5