PageRenderTime 47ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/local-lisp/slime/swank-lispworks.lisp

https://bitbucket.org/sakito/dot.emacs.d/
Lisp | 916 lines | 735 code | 144 blank | 37 comment | 14 complexity | a155c0cdcd8aefb1b5eeeebaa63466fe MD5 | raw file
Possible License(s): GPL-3.0, CC-BY-SA-4.0, GPL-2.0, Unlicense
  1. ;;; -*- indent-tabs-mode: nil -*-
  2. ;;;
  3. ;;; swank-lispworks.lisp --- LispWorks specific code for SLIME.
  4. ;;;
  5. ;;; Created 2003, Helmut Eller
  6. ;;;
  7. ;;; This code has been placed in the Public Domain. All warranties
  8. ;;; are disclaimed.
  9. ;;;
  10. (in-package :swank-backend)
  11. (eval-when (:compile-toplevel :load-toplevel :execute)
  12. (require "comm")
  13. (import-from :stream *gray-stream-symbols* :swank-backend))
  14. (import-swank-mop-symbols :clos '(:slot-definition-documentation
  15. :slot-boundp-using-class
  16. :slot-value-using-class
  17. :slot-makunbound-using-class
  18. :eql-specializer
  19. :eql-specializer-object
  20. :compute-applicable-methods-using-classes))
  21. (defun swank-mop:slot-definition-documentation (slot)
  22. (documentation slot t))
  23. (defun swank-mop:slot-boundp-using-class (class object slotd)
  24. (clos:slot-boundp-using-class class object
  25. (clos:slot-definition-name slotd)))
  26. (defun swank-mop:slot-value-using-class (class object slotd)
  27. (clos:slot-value-using-class class object
  28. (clos:slot-definition-name slotd)))
  29. (defun (setf swank-mop:slot-value-using-class) (value class object slotd)
  30. (setf (clos:slot-value-using-class class object
  31. (clos:slot-definition-name slotd))
  32. value))
  33. (defun swank-mop:slot-makunbound-using-class (class object slotd)
  34. (clos:slot-makunbound-using-class class object
  35. (clos:slot-definition-name slotd)))
  36. (defun swank-mop:compute-applicable-methods-using-classes (gf classes)
  37. (clos::compute-applicable-methods-from-classes gf classes))
  38. ;; lispworks doesn't have the eql-specializer class, it represents
  39. ;; them as a list of `(EQL ,OBJECT)
  40. (deftype swank-mop:eql-specializer () 'cons)
  41. (defun swank-mop:eql-specializer-object (eql-spec)
  42. (second eql-spec))
  43. (eval-when (:compile-toplevel :execute :load-toplevel)
  44. (defvar *original-defimplementation* (macro-function 'defimplementation))
  45. (defmacro defimplementation (&whole whole name args &body body
  46. &environment env)
  47. (declare (ignore args body))
  48. `(progn
  49. (dspec:record-definition '(defun ,name) (dspec:location)
  50. :check-redefinition-p nil)
  51. ,(funcall *original-defimplementation* whole env))))
  52. ;;; TCP server
  53. (defimplementation preferred-communication-style ()
  54. :spawn)
  55. (defun socket-fd (socket)
  56. (etypecase socket
  57. (fixnum socket)
  58. (comm:socket-stream (comm:socket-stream-socket socket))))
  59. (defimplementation create-socket (host port)
  60. (multiple-value-bind (socket where errno)
  61. #-(or lispworks4.1 (and macosx lispworks4.3))
  62. (comm::create-tcp-socket-for-service port :address host)
  63. #+(or lispworks4.1 (and macosx lispworks4.3))
  64. (comm::create-tcp-socket-for-service port)
  65. (cond (socket socket)
  66. (t (error 'network-error
  67. :format-control "~A failed: ~A (~D)"
  68. :format-arguments (list where
  69. (list #+unix (lw:get-unix-error errno))
  70. errno))))))
  71. (defimplementation local-port (socket)
  72. (nth-value 1 (comm:get-socket-address (socket-fd socket))))
  73. (defimplementation close-socket (socket)
  74. (comm::close-socket (socket-fd socket)))
  75. (defimplementation accept-connection (socket
  76. &key external-format buffering timeout)
  77. (declare (ignore buffering))
  78. (let* ((fd (comm::get-fd-from-socket socket)))
  79. (assert (/= fd -1))
  80. (assert (valid-external-format-p external-format))
  81. (cond ((member (first external-format) '(:latin-1 :ascii))
  82. (make-instance 'comm:socket-stream
  83. :socket fd
  84. :direction :io
  85. :read-timeout timeout
  86. :element-type 'base-char))
  87. (t
  88. (make-flexi-stream
  89. (make-instance 'comm:socket-stream
  90. :socket fd
  91. :direction :io
  92. :read-timeout timeout
  93. :element-type '(unsigned-byte 8))
  94. external-format)))))
  95. (defun make-flexi-stream (stream external-format)
  96. (unless (member :flexi-streams *features*)
  97. (error "Cannot use external format ~A without having installed flexi-streams in the inferior-lisp."
  98. external-format))
  99. (funcall (read-from-string "FLEXI-STREAMS:MAKE-FLEXI-STREAM")
  100. stream
  101. :external-format
  102. (apply (read-from-string "FLEXI-STREAMS:MAKE-EXTERNAL-FORMAT")
  103. external-format)))
  104. ;;; Coding Systems
  105. (defun valid-external-format-p (external-format)
  106. (member external-format *external-format-to-coding-system*
  107. :test #'equal :key #'car))
  108. (defvar *external-format-to-coding-system*
  109. '(((:latin-1 :eol-style :lf)
  110. "latin-1-unix" "iso-latin-1-unix" "iso-8859-1-unix")
  111. ((:latin-1)
  112. "latin-1" "iso-latin-1" "iso-8859-1")
  113. ((:utf-8) "utf-8")
  114. ((:utf-8 :eol-style :lf) "utf-8-unix")
  115. ((:euc-jp) "euc-jp")
  116. ((:euc-jp :eol-style :lf) "euc-jp-unix")
  117. ((:ascii) "us-ascii")
  118. ((:ascii :eol-style :lf) "us-ascii-unix")))
  119. (defimplementation find-external-format (coding-system)
  120. (car (rassoc-if (lambda (x) (member coding-system x :test #'equal))
  121. *external-format-to-coding-system*)))
  122. ;;; Unix signals
  123. (defun sigint-handler ()
  124. (with-simple-restart (continue "Continue from SIGINT handler.")
  125. (invoke-debugger "SIGINT")))
  126. (defun make-sigint-handler (process)
  127. (lambda (&rest args)
  128. (declare (ignore args))
  129. (mp:process-interrupt process #'sigint-handler)))
  130. (defun set-sigint-handler ()
  131. ;; Set SIGINT handler on Swank request handler thread.
  132. #-win32
  133. (sys::set-signal-handler +sigint+
  134. (make-sigint-handler mp:*current-process*)))
  135. #-win32
  136. (defimplementation install-sigint-handler (handler)
  137. (sys::set-signal-handler +sigint+
  138. (let ((self mp:*current-process*))
  139. (lambda (&rest args)
  140. (declare (ignore args))
  141. (mp:process-interrupt self handler)))))
  142. (defimplementation getpid ()
  143. #+win32 (win32:get-current-process-id)
  144. #-win32 (system::getpid))
  145. (defimplementation lisp-implementation-type-name ()
  146. "lispworks")
  147. (defimplementation set-default-directory (directory)
  148. (namestring (hcl:change-directory directory)))
  149. ;;;; Documentation
  150. (defun map-list (function list)
  151. "Map over proper and not proper lists."
  152. (loop for (car . cdr) on list
  153. collect (funcall function car) into result
  154. when (null cdr) return result
  155. when (atom cdr) return (nconc result (funcall function cdr))))
  156. (defun replace-strings-with-symbols (tree)
  157. (map-list
  158. (lambda (x)
  159. (typecase x
  160. (list
  161. (replace-strings-with-symbols x))
  162. (symbol
  163. x)
  164. (string
  165. (intern x))
  166. (t
  167. (intern (write-to-string x)))))
  168. tree))
  169. (defimplementation arglist (symbol-or-function)
  170. (let ((arglist (lw:function-lambda-list symbol-or-function)))
  171. (etypecase arglist
  172. ((member :dont-know)
  173. :not-available)
  174. (list
  175. (replace-strings-with-symbols arglist)))))
  176. (defimplementation function-name (function)
  177. (nth-value 2 (function-lambda-expression function)))
  178. (defimplementation macroexpand-all (form)
  179. (walker:walk-form form))
  180. (defun generic-function-p (object)
  181. (typep object 'generic-function))
  182. (defimplementation describe-symbol-for-emacs (symbol)
  183. "Return a plist describing SYMBOL.
  184. Return NIL if the symbol is unbound."
  185. (let ((result '()))
  186. (labels ((first-line (string)
  187. (let ((pos (position #\newline string)))
  188. (if (null pos) string (subseq string 0 pos))))
  189. (doc (kind &optional (sym symbol))
  190. (let ((string (or (documentation sym kind))))
  191. (if string
  192. (first-line string)
  193. :not-documented)))
  194. (maybe-push (property value)
  195. (when value
  196. (setf result (list* property value result)))))
  197. (maybe-push
  198. :variable (when (boundp symbol)
  199. (doc 'variable)))
  200. (maybe-push
  201. :generic-function (if (and (fboundp symbol)
  202. (generic-function-p (fdefinition symbol)))
  203. (doc 'function)))
  204. (maybe-push
  205. :function (if (and (fboundp symbol)
  206. (not (generic-function-p (fdefinition symbol))))
  207. (doc 'function)))
  208. (maybe-push
  209. :setf (let ((setf-name (sys:underlying-setf-name `(setf ,symbol))))
  210. (if (fboundp setf-name)
  211. (doc 'setf))))
  212. (maybe-push
  213. :class (if (find-class symbol nil)
  214. (doc 'class)))
  215. result)))
  216. (defimplementation describe-definition (symbol type)
  217. (ecase type
  218. (:variable (describe-symbol symbol))
  219. (:class (describe (find-class symbol)))
  220. ((:function :generic-function) (describe-function symbol))
  221. (:setf (describe-function (sys:underlying-setf-name `(setf ,symbol))))))
  222. (defun describe-function (symbol)
  223. (cond ((fboundp symbol)
  224. (format t "(~A ~/pprint-fill/)~%~%~:[(not documented)~;~:*~A~]~%"
  225. symbol
  226. (lispworks:function-lambda-list symbol)
  227. (documentation symbol 'function))
  228. (describe (fdefinition symbol)))
  229. (t (format t "~S is not fbound" symbol))))
  230. (defun describe-symbol (sym)
  231. (format t "~A is a symbol in package ~A." sym (symbol-package sym))
  232. (when (boundp sym)
  233. (format t "~%~%Value: ~A" (symbol-value sym)))
  234. (let ((doc (documentation sym 'variable)))
  235. (when doc
  236. (format t "~%~%Variable documentation:~%~A" doc)))
  237. (when (fboundp sym)
  238. (describe-function sym)))
  239. ;;; Debugging
  240. (defclass slime-env (env:environment)
  241. ((debugger-hook :initarg :debugger-hoook)))
  242. (defun slime-env (hook io-bindings)
  243. (make-instance 'slime-env :name "SLIME Environment"
  244. :io-bindings io-bindings
  245. :debugger-hoook hook))
  246. (defmethod env-internals:environment-display-notifier
  247. ((env slime-env) &key restarts condition)
  248. (declare (ignore restarts condition))
  249. (funcall (swank-sym :swank-debugger-hook) condition *debugger-hook*)
  250. ;; nil
  251. )
  252. (defmethod env-internals:environment-display-debugger ((env slime-env))
  253. *debug-io*)
  254. (defmethod env-internals:confirm-p ((e slime-env) &optional msg &rest args)
  255. (apply (swank-sym :y-or-n-p-in-emacs) msg args))
  256. (defimplementation call-with-debugger-hook (hook fun)
  257. (let ((*debugger-hook* hook))
  258. (env:with-environment ((slime-env hook '()))
  259. (funcall fun))))
  260. (defimplementation install-debugger-globally (function)
  261. (setq *debugger-hook* function)
  262. (setf (env:environment) (slime-env function '())))
  263. (defvar *sldb-top-frame*)
  264. (defun interesting-frame-p (frame)
  265. (cond ((or (dbg::call-frame-p frame)
  266. (dbg::derived-call-frame-p frame)
  267. (dbg::foreign-frame-p frame)
  268. (dbg::interpreted-call-frame-p frame))
  269. t)
  270. ((dbg::catch-frame-p frame) dbg:*print-catch-frames*)
  271. ((dbg::binding-frame-p frame) dbg:*print-binding-frames*)
  272. ((dbg::handler-frame-p frame) dbg:*print-handler-frames*)
  273. ((dbg::restart-frame-p frame) dbg:*print-restart-frames*)
  274. (t nil)))
  275. (defun nth-next-frame (frame n)
  276. "Unwind FRAME N times."
  277. (do ((frame frame (dbg::frame-next frame))
  278. (i n (if (interesting-frame-p frame) (1- i) i)))
  279. ((or (not frame)
  280. (and (interesting-frame-p frame) (zerop i)))
  281. frame)))
  282. (defun nth-frame (index)
  283. (nth-next-frame *sldb-top-frame* index))
  284. (defun find-top-frame ()
  285. "Return the most suitable top-frame for the debugger."
  286. (or (do ((frame (dbg::debugger-stack-current-frame dbg::*debugger-stack*)
  287. (nth-next-frame frame 1)))
  288. ((or (null frame) ; no frame found!
  289. (and (dbg::call-frame-p frame)
  290. (eq (dbg::call-frame-function-name frame)
  291. 'invoke-debugger)))
  292. (nth-next-frame frame 1)))
  293. ;; if we can't find a invoke-debugger frame, take any old frame at the top
  294. (dbg::debugger-stack-current-frame dbg::*debugger-stack*)))
  295. (defimplementation call-with-debugging-environment (fn)
  296. (dbg::with-debugger-stack ()
  297. (let ((*sldb-top-frame* (find-top-frame)))
  298. (funcall fn))))
  299. (defimplementation compute-backtrace (start end)
  300. (let ((end (or end most-positive-fixnum))
  301. (backtrace '()))
  302. (do ((frame (nth-frame start) (dbg::frame-next frame))
  303. (i start))
  304. ((or (not frame) (= i end)) (nreverse backtrace))
  305. (when (interesting-frame-p frame)
  306. (incf i)
  307. (push frame backtrace)))))
  308. (defun frame-actual-args (frame)
  309. (let ((*break-on-signals* nil))
  310. (mapcar (lambda (arg)
  311. (case arg
  312. ((&rest &optional &key) arg)
  313. (t
  314. (handler-case (dbg::dbg-eval arg frame)
  315. (error (e) (format nil "<~A>" arg))))))
  316. (dbg::call-frame-arglist frame))))
  317. (defimplementation print-frame (frame stream)
  318. (cond ((dbg::call-frame-p frame)
  319. (format stream "~S ~S"
  320. (dbg::call-frame-function-name frame)
  321. (frame-actual-args frame)))
  322. (t (princ frame stream))))
  323. (defun frame-vars (frame)
  324. (first (dbg::frame-locals-format-list frame #'list 75 0)))
  325. (defimplementation frame-locals (n)
  326. (let ((frame (nth-frame n)))
  327. (if (dbg::call-frame-p frame)
  328. (mapcar (lambda (var)
  329. (destructuring-bind (name value symbol location) var
  330. (declare (ignore name location))
  331. (list :name symbol :id 0
  332. :value value)))
  333. (frame-vars frame)))))
  334. (defimplementation frame-var-value (frame var)
  335. (let ((frame (nth-frame frame)))
  336. (destructuring-bind (_n value _s _l) (nth var (frame-vars frame))
  337. (declare (ignore _n _s _l))
  338. value)))
  339. (defimplementation frame-source-location (frame)
  340. (let ((frame (nth-frame frame))
  341. (callee (if (plusp frame) (nth-frame (1- frame)))))
  342. (if (dbg::call-frame-p frame)
  343. (let ((dspec (dbg::call-frame-function-name frame))
  344. (cname (and (dbg::call-frame-p callee)
  345. (dbg::call-frame-function-name callee))))
  346. (if dspec
  347. (frame-location dspec cname))))))
  348. (defimplementation eval-in-frame (form frame-number)
  349. (let ((frame (nth-frame frame-number)))
  350. (dbg::dbg-eval form frame)))
  351. (defimplementation return-from-frame (frame-number form)
  352. (let* ((frame (nth-frame frame-number))
  353. (return-frame (dbg::find-frame-for-return frame)))
  354. (dbg::dbg-return-from-call-frame frame form return-frame
  355. dbg::*debugger-stack*)))
  356. (defimplementation restart-frame (frame-number)
  357. (let ((frame (nth-frame frame-number)))
  358. (dbg::restart-frame frame :same-args t)))
  359. (defimplementation disassemble-frame (frame-number)
  360. (let* ((frame (nth-frame frame-number)))
  361. (when (dbg::call-frame-p frame)
  362. (let ((function (dbg::get-call-frame-function frame)))
  363. (disassemble function)))))
  364. ;;; Definition finding
  365. (defun frame-location (dspec callee-name)
  366. (let ((infos (dspec:find-dspec-locations dspec)))
  367. (cond (infos
  368. (destructuring-bind ((rdspec location) &rest _) infos
  369. (declare (ignore _))
  370. (let ((name (and callee-name (symbolp callee-name)
  371. (string callee-name))))
  372. (make-dspec-location rdspec location
  373. `(:call-site ,name)))))
  374. (t
  375. (list :error (format nil "Source location not available for: ~S"
  376. dspec))))))
  377. (defimplementation find-definitions (name)
  378. (let ((locations (dspec:find-name-locations dspec:*dspec-classes* name)))
  379. (loop for (dspec location) in locations
  380. collect (list dspec (make-dspec-location dspec location)))))
  381. ;;; Compilation
  382. (defmacro with-swank-compilation-unit ((location &rest options) &body body)
  383. (lw:rebinding (location)
  384. `(let ((compiler::*error-database* '()))
  385. (with-compilation-unit ,options
  386. (multiple-value-prog1 (progn ,@body)
  387. (signal-error-data-base compiler::*error-database*
  388. ,location)
  389. (signal-undefined-functions compiler::*unknown-functions*
  390. ,location))))))
  391. (defimplementation swank-compile-file (input-file output-file
  392. load-p external-format
  393. &key policy)
  394. (declare (ignore policy))
  395. (with-swank-compilation-unit (input-file)
  396. (compile-file input-file
  397. :output-file output-file
  398. :load load-p
  399. :external-format external-format)))
  400. (defvar *within-call-with-compilation-hooks* nil
  401. "Whether COMPILE-FILE was called from within CALL-WITH-COMPILATION-HOOKS.")
  402. (defvar *undefined-functions-hash* nil
  403. "Hash table to map info about undefined functions to pathnames.")
  404. (lw:defadvice (compile-file compile-file-and-collect-notes :around)
  405. (pathname &rest rest)
  406. (multiple-value-prog1 (apply #'lw:call-next-advice pathname rest)
  407. (when *within-call-with-compilation-hooks*
  408. (maphash (lambda (unfun dspecs)
  409. (dolist (dspec dspecs)
  410. (let ((unfun-info (list unfun dspec)))
  411. (unless (gethash unfun-info *undefined-functions-hash*)
  412. (setf (gethash unfun-info *undefined-functions-hash*)
  413. pathname)))))
  414. compiler::*unknown-functions*))))
  415. (defimplementation call-with-compilation-hooks (function)
  416. (let ((compiler::*error-database* '())
  417. (*undefined-functions-hash* (make-hash-table :test 'equal))
  418. (*within-call-with-compilation-hooks* t))
  419. (with-compilation-unit ()
  420. (prog1 (funcall function)
  421. (signal-error-data-base compiler::*error-database*)
  422. (signal-undefined-functions compiler::*unknown-functions*)))))
  423. (defun map-error-database (database fn)
  424. (loop for (filename . defs) in database do
  425. (loop for (dspec . conditions) in defs do
  426. (dolist (c conditions)
  427. (funcall fn filename dspec (if (consp c) (car c) c))))))
  428. (defun lispworks-severity (condition)
  429. (cond ((not condition) :warning)
  430. (t (etypecase condition
  431. (error :error)
  432. (style-warning :warning)
  433. (warning :warning)))))
  434. (defun signal-compiler-condition (message location condition)
  435. (check-type message string)
  436. (signal
  437. (make-instance 'compiler-condition :message message
  438. :severity (lispworks-severity condition)
  439. :location location
  440. :original-condition condition)))
  441. (defvar *temp-file-format* '(:utf-8 :eol-style :lf))
  442. (defun compile-from-temp-file (string filename)
  443. (unwind-protect
  444. (progn
  445. (with-open-file (s filename :direction :output
  446. :if-exists :supersede
  447. :external-format *temp-file-format*)
  448. (write-string string s)
  449. (finish-output s))
  450. (multiple-value-bind (binary-filename warnings? failure?)
  451. (compile-file filename :load t
  452. :external-format *temp-file-format*)
  453. (declare (ignore warnings?))
  454. (when binary-filename
  455. (delete-file binary-filename))
  456. (not failure?)))
  457. (delete-file filename)))
  458. (defun dspec-function-name-position (dspec fallback)
  459. (etypecase dspec
  460. (cons (let ((name (dspec:dspec-primary-name dspec)))
  461. (typecase name
  462. ((or symbol string)
  463. (list :function-name (string name)))
  464. (t fallback))))
  465. (null fallback)
  466. (symbol (list :function-name (string dspec)))))
  467. (defmacro with-fairly-standard-io-syntax (&body body)
  468. "Like WITH-STANDARD-IO-SYNTAX but preserve *PACKAGE* and *READTABLE*."
  469. (let ((package (gensym))
  470. (readtable (gensym)))
  471. `(let ((,package *package*)
  472. (,readtable *readtable*))
  473. (with-standard-io-syntax
  474. (let ((*package* ,package)
  475. (*readtable* ,readtable))
  476. ,@body)))))
  477. (defun skip-comments (stream)
  478. (let ((pos0 (file-position stream)))
  479. (cond ((equal (ignore-errors (list (read-delimited-list #\( stream)))
  480. '(()))
  481. (file-position stream (1- (file-position stream))))
  482. (t (file-position stream pos0)))))
  483. #-(or lispworks4.1 lispworks4.2) ; no dspec:parse-form-dspec prior to 4.3
  484. (defun dspec-stream-position (stream dspec)
  485. (with-fairly-standard-io-syntax
  486. (loop (let* ((pos (progn (skip-comments stream) (file-position stream)))
  487. (form (read stream nil '#1=#:eof)))
  488. (when (eq form '#1#)
  489. (return nil))
  490. (labels ((check-dspec (form)
  491. (when (consp form)
  492. (let ((operator (car form)))
  493. (case operator
  494. ((progn)
  495. (mapcar #'check-dspec
  496. (cdr form)))
  497. ((eval-when locally macrolet symbol-macrolet)
  498. (mapcar #'check-dspec
  499. (cddr form)))
  500. ((in-package)
  501. (let ((package (find-package (second form))))
  502. (when package
  503. (setq *package* package))))
  504. (otherwise
  505. (let ((form-dspec (dspec:parse-form-dspec form)))
  506. (when (dspec:dspec-equal dspec form-dspec)
  507. (return pos)))))))))
  508. (check-dspec form))))))
  509. (defun dspec-file-position (file dspec)
  510. (let* ((*compile-file-pathname* (pathname file))
  511. (*compile-file-truename* (truename *compile-file-pathname*))
  512. (*load-pathname* *compile-file-pathname*)
  513. (*load-truename* *compile-file-truename*))
  514. (with-open-file (stream file)
  515. (let ((pos
  516. #-(or lispworks4.1 lispworks4.2)
  517. (dspec-stream-position stream dspec)))
  518. (if pos
  519. (list :position (1+ pos))
  520. (dspec-function-name-position dspec `(:position 1)))))))
  521. (defun emacs-buffer-location-p (location)
  522. (and (consp location)
  523. (eq (car location) :emacs-buffer)))
  524. (defun make-dspec-location (dspec location &optional hints)
  525. (etypecase location
  526. ((or pathname string)
  527. (multiple-value-bind (file err)
  528. (ignore-errors (namestring (truename location)))
  529. (if err
  530. (list :error (princ-to-string err))
  531. (make-location `(:file ,file)
  532. (dspec-file-position file dspec)
  533. hints))))
  534. (symbol
  535. `(:error ,(format nil "Cannot resolve location: ~S" location)))
  536. ((satisfies emacs-buffer-location-p)
  537. (destructuring-bind (_ buffer offset string) location
  538. (declare (ignore _ string))
  539. (make-location `(:buffer ,buffer)
  540. (dspec-function-name-position dspec `(:offset ,offset 0))
  541. hints)))))
  542. (defun make-dspec-progenitor-location (dspec location)
  543. (let ((canon-dspec (dspec:canonicalize-dspec dspec)))
  544. (make-dspec-location
  545. (if canon-dspec
  546. (if (dspec:local-dspec-p canon-dspec)
  547. (dspec:dspec-progenitor canon-dspec)
  548. canon-dspec)
  549. nil)
  550. location)))
  551. (defun signal-error-data-base (database &optional location)
  552. (map-error-database
  553. database
  554. (lambda (filename dspec condition)
  555. (signal-compiler-condition
  556. (format nil "~A" condition)
  557. (make-dspec-progenitor-location dspec (or location filename))
  558. condition))))
  559. (defun unmangle-unfun (symbol)
  560. "Converts symbols like 'SETF::|\"CL-USER\" \"GET\"| to
  561. function names like \(SETF GET)."
  562. (cond ((sys::setf-symbol-p symbol)
  563. (sys::setf-pair-from-underlying-name symbol))
  564. (t symbol)))
  565. (defun signal-undefined-functions (htab &optional filename)
  566. (maphash (lambda (unfun dspecs)
  567. (dolist (dspec dspecs)
  568. (signal-compiler-condition
  569. (format nil "Undefined function ~A" (unmangle-unfun unfun))
  570. (make-dspec-progenitor-location dspec
  571. (or filename
  572. (gethash (list unfun dspec)
  573. *undefined-functions-hash*)))
  574. nil)))
  575. htab))
  576. (defimplementation swank-compile-string (string &key buffer position filename
  577. policy)
  578. (declare (ignore filename policy))
  579. (assert buffer)
  580. (assert position)
  581. (let* ((location (list :emacs-buffer buffer position string))
  582. (tmpname (hcl:make-temp-file nil "lisp")))
  583. (with-swank-compilation-unit (location)
  584. (compile-from-temp-file
  585. (with-output-to-string (s)
  586. (let ((*print-radix* t))
  587. (print `(eval-when (:compile-toplevel)
  588. (setq dspec::*location* (list ,@location)))
  589. s))
  590. (write-string string s))
  591. tmpname))))
  592. ;;; xref
  593. (defmacro defxref (name function)
  594. `(defimplementation ,name (name)
  595. (xref-results (,function name))))
  596. (defxref who-calls hcl:who-calls)
  597. (defxref who-macroexpands hcl:who-calls) ; macros are in the calls table too
  598. (defxref calls-who hcl:calls-who)
  599. (defxref list-callers list-callers-internal)
  600. #+lispworks6
  601. (defxref list-callees list-callees-internal)
  602. #-lispworks6
  603. (defun list-callers-internal (name)
  604. (let ((callers (make-array 100
  605. :fill-pointer 0
  606. :adjustable t)))
  607. (hcl:sweep-all-objects
  608. #'(lambda (object)
  609. (when (and #+Harlequin-PC-Lisp (low:compiled-code-p object)
  610. #-Harlequin-PC-Lisp (sys::callablep object)
  611. (system::find-constant$funcallable name object))
  612. (vector-push-extend object callers))))
  613. ;; Delay dspec:object-dspec until after sweep-all-objects
  614. ;; to reduce allocation problems.
  615. (loop for object across callers
  616. collect (if (symbolp object)
  617. (list 'function object)
  618. (or (dspec:object-dspec object) object)))))
  619. #+lispworks6
  620. (defun list-callers-internal (name)
  621. ;; Delay dspec:object-dspec until after sweep-all-objects
  622. ;; to reduce allocation problems.
  623. (loop for object in (hcl::who-calls name)
  624. collect (if (symbolp object)
  625. (list 'function object)
  626. (or (dspec:object-dspec object) object))))
  627. #+lispworks6
  628. (defun list-callees-internal (name)
  629. ;; Delay dspec:object-dspec until after sweep-all-objects
  630. ;; to reduce allocation problems.
  631. (loop for object in (hcl::calls-who name)
  632. collect (if (symbolp object)
  633. (list 'function object)
  634. (or (dspec:object-dspec object) object))))
  635. ;; only for lispworks 4.2 and above
  636. #-lispworks4.1
  637. (progn
  638. (defxref who-references hcl:who-references)
  639. (defxref who-binds hcl:who-binds)
  640. (defxref who-sets hcl:who-sets))
  641. (defimplementation who-specializes (classname)
  642. (let ((methods (clos:class-direct-methods (find-class classname))))
  643. (xref-results (mapcar #'dspec:object-dspec methods))))
  644. (defun xref-results (dspecs)
  645. (flet ((frob-locs (dspec locs)
  646. (cond (locs
  647. (loop for (name loc) in locs
  648. collect (list name (make-dspec-location name loc))))
  649. (t `((,dspec (:error "Source location not available")))))))
  650. (loop for dspec in dspecs
  651. append (frob-locs dspec (dspec:dspec-definition-locations dspec)))))
  652. ;;; Inspector
  653. (defmethod emacs-inspect ((o t))
  654. (lispworks-inspect o))
  655. (defmethod emacs-inspect ((o function))
  656. (lispworks-inspect o))
  657. ;; FIXME: slot-boundp-using-class in LW works with names so we can't
  658. ;; use our method in swank.lisp.
  659. (defmethod emacs-inspect ((o standard-object))
  660. (lispworks-inspect o))
  661. (defun lispworks-inspect (o)
  662. (multiple-value-bind (names values _getter _setter type)
  663. (lw:get-inspector-values o nil)
  664. (declare (ignore _getter _setter))
  665. (append
  666. (label-value-line "Type" type)
  667. (loop for name in names
  668. for value in values
  669. append (label-value-line name value)))))
  670. ;;; Miscellaneous
  671. (defimplementation quit-lisp ()
  672. (lispworks:quit))
  673. ;;; Tracing
  674. (defun parse-fspec (fspec)
  675. "Return a dspec for FSPEC."
  676. (ecase (car fspec)
  677. ((:defmethod) `(method ,(cdr fspec)))))
  678. (defun tracedp (dspec)
  679. (member dspec (eval '(trace)) :test #'equal))
  680. (defun toggle-trace-aux (dspec)
  681. (cond ((tracedp dspec)
  682. (eval `(untrace ,dspec))
  683. (format nil "~S is now untraced." dspec))
  684. (t
  685. (eval `(trace (,dspec)))
  686. (format nil "~S is now traced." dspec))))
  687. (defimplementation toggle-trace (fspec)
  688. (toggle-trace-aux (parse-fspec fspec)))
  689. ;;; Multithreading
  690. (defimplementation initialize-multiprocessing (continuation)
  691. (cond ((not mp::*multiprocessing*)
  692. (push (list "Initialize SLIME" '() continuation)
  693. mp:*initial-processes*)
  694. (mp:initialize-multiprocessing))
  695. (t (funcall continuation))))
  696. (defimplementation spawn (fn &key name)
  697. (mp:process-run-function name () fn))
  698. (defvar *id-lock* (mp:make-lock))
  699. (defvar *thread-id-counter* 0)
  700. (defimplementation thread-id (thread)
  701. (mp:with-lock (*id-lock*)
  702. (or (getf (mp:process-plist thread) 'id)
  703. (setf (getf (mp:process-plist thread) 'id)
  704. (incf *thread-id-counter*)))))
  705. (defimplementation find-thread (id)
  706. (find id (mp:list-all-processes)
  707. :key (lambda (p) (getf (mp:process-plist p) 'id))))
  708. (defimplementation thread-name (thread)
  709. (mp:process-name thread))
  710. (defimplementation thread-status (thread)
  711. (format nil "~A ~D"
  712. (mp:process-whostate thread)
  713. (mp:process-priority thread)))
  714. (defimplementation make-lock (&key name)
  715. (mp:make-lock :name name))
  716. (defimplementation call-with-lock-held (lock function)
  717. (mp:with-lock (lock) (funcall function)))
  718. (defimplementation current-thread ()
  719. mp:*current-process*)
  720. (defimplementation all-threads ()
  721. (mp:list-all-processes))
  722. (defimplementation interrupt-thread (thread fn)
  723. (mp:process-interrupt thread fn))
  724. (defimplementation kill-thread (thread)
  725. (mp:process-kill thread))
  726. (defimplementation thread-alive-p (thread)
  727. (mp:process-alive-p thread))
  728. (defstruct (mailbox (:conc-name mailbox.))
  729. (mutex (mp:make-lock :name "thread mailbox"))
  730. (queue '() :type list))
  731. (defvar *mailbox-lock* (mp:make-lock))
  732. (defun mailbox (thread)
  733. (mp:with-lock (*mailbox-lock*)
  734. (or (getf (mp:process-plist thread) 'mailbox)
  735. (setf (getf (mp:process-plist thread) 'mailbox)
  736. (make-mailbox)))))
  737. (defimplementation receive-if (test &optional timeout)
  738. (let* ((mbox (mailbox mp:*current-process*))
  739. (lock (mailbox.mutex mbox)))
  740. (assert (or (not timeout) (eq timeout t)))
  741. (loop
  742. (check-slime-interrupts)
  743. (mp:with-lock (lock "receive-if/try")
  744. (let* ((q (mailbox.queue mbox))
  745. (tail (member-if test q)))
  746. (when tail
  747. (setf (mailbox.queue mbox) (nconc (ldiff q tail) (cdr tail)))
  748. (return (car tail)))))
  749. (when (eq timeout t) (return (values nil t)))
  750. (mp:process-wait-with-timeout
  751. "receive-if" 0.3 (lambda () (some test (mailbox.queue mbox)))))))
  752. (defimplementation send (thread message)
  753. (let ((mbox (mailbox thread)))
  754. (mp:with-lock ((mailbox.mutex mbox))
  755. (setf (mailbox.queue mbox)
  756. (nconc (mailbox.queue mbox) (list message))))))
  757. (defimplementation set-default-initial-binding (var form)
  758. (setq mp:*process-initial-bindings*
  759. (acons var `(eval (quote ,form))
  760. mp:*process-initial-bindings* )))
  761. (defimplementation thread-attributes (thread)
  762. (list :priority (mp:process-priority thread)
  763. :idle (mp:process-idle-time thread)))
  764. ;;; Some intergration with the lispworks environment
  765. (defun swank-sym (name) (find-symbol (string name) :swank))
  766. ;;;; Weak hashtables
  767. (defimplementation make-weak-key-hash-table (&rest args)
  768. (apply #'make-hash-table :weak-kind :key args))
  769. (defimplementation make-weak-value-hash-table (&rest args)
  770. (apply #'make-hash-table :weak-kind :value args))