-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
utils.lisp
299 lines (245 loc) · 11.4 KB
/
utils.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
(in-package #:jupyter/common-lisp)
#+sbcl (require :sb-introspect)
(defun lambda-list (sym)
#+ccl
(ccl:arglist sym)
#+clisp
(system::arglist sym)
#+(or clasp ecl)
(ext:function-lambda-list sym)
#+sbcl
(sb-introspect:function-lambda-list sym)
#+lispworks
(lispworks:function-lambda-list sym)
#-(or ccl clisp clasp ecl lispworks sbcl)
(second (function-lambda-expression (or (macro-function sym)
(fdefinition sym)))))
(defun visible-symbol-p (sym)
(let ((pkg (symbol-package sym)))
(or (eq *package* pkg)
(multiple-value-bind (s status)
(find-symbol (symbol-name sym) pkg)
(declare (ignore s))
(eql :external status)))))
(defun find-generics (cls)
(remove-duplicates
(mapcan (lambda (cls)
(unless (eql t (class-name cls))
(remove-if-not (lambda (gf)
(let ((name (closer-mop:generic-function-name gf)))
(or (and (symbolp name)
(visible-symbol-p name))
(and (listp name)
(eql 'setf (car name))
(every #'visible-symbol-p (cdr name))))))
(closer-mop:specializer-direct-generic-functions cls))))
(closer-mop:class-precedence-list cls))))
(defun find-methods (cls)
(remove-duplicates
(mapcan (lambda (cls)
(unless (eql t (class-name cls))
(remove-if-not (lambda (method)
(let ((name (closer-mop:generic-function-name (closer-mop:method-generic-function method))))
(or (and (symbolp name)
(visible-symbol-p name))
(and (listp name)
(eql 'setf (car name))
(every #'visible-symbol-p (cdr name))))))
(closer-mop:specializer-direct-methods cls))))
(closer-mop:class-precedence-list cls))))
(defun method-specialized-lambda-list (method)
(do* ((lambda-list (closer-mop:method-lambda-list method) (cdr lambda-list))
(specializers (closer-mop:method-specializers method) (cdr specializers))
specialized-lambda-list)
((null lambda-list) (nreverse specialized-lambda-list))
(cond
((and specializers
(typep (car specializers) 'closer-mop:eql-specializer))
(push (list (car lambda-list)
(list 'eql (closer-mop:eql-specializer-object (car specializers))))
specialized-lambda-list))
((and specializers
(eql t (class-name (car specializers))))
(push (car lambda-list)
specialized-lambda-list))
(specializers
(push (list (car lambda-list) (class-name (car specializers)))
specialized-lambda-list))
(t
(push (car lambda-list)
specialized-lambda-list)))))
(defclass tracking-stream (trivial-gray-streams:fundamental-character-input-stream)
((stream
:reader tracking-stream-stream
:initarg :stream)
(line
:accessor tracking-stream-line
:initform 1)
(column
:accessor tracking-stream-column
:initform 0)
(previous-column
:accessor tracking-stream-previous-column
:initform 1)))
(defmethod trivial-gray-streams:stream-file-position ((stream tracking-stream))
(file-position (tracking-stream-stream stream)))
(defmethod trivial-gray-streams:stream-listen ((stream tracking-stream))
(listen (tracking-stream-stream stream)))
(defmethod trivial-gray-streams:stream-read-char ((stream tracking-stream))
(let ((ch (read-char (tracking-stream-stream stream) nil :eof)))
(cond
((eq :eof ch))
((char= ch #\Newline)
(incf (tracking-stream-line stream))
(setf (tracking-stream-previous-column stream) (tracking-stream-column stream)
(tracking-stream-column stream) 1))
((char= ch #\Tab)
(incf (tracking-stream-column stream) 8))
((graphic-char-p ch)
(incf (tracking-stream-column stream))))
ch))
(defmethod trivial-gray-streams:stream-peek-char ((stream tracking-stream))
(peek-char nil (tracking-stream-stream stream) nil :eof))
(defmethod trivial-gray-streams:stream-unread-char ((stream tracking-stream) char)
(cond
((char= char #\Newline)
(decf (tracking-stream-line stream))
(setf (tracking-stream-column stream) (tracking-stream-previous-column stream)))
((char= char #\Tab)
(decf (tracking-stream-column stream) 8))
((graphic-char-p char)
(decf (tracking-stream-column stream))))
(unread-char char (tracking-stream-stream stream)))
(defmacro with-tracking-stream ((stream &rest rest) &body body)
`(let ((,stream (make-instance 'tracking-stream :stream (open ,@rest))))
(unwind-protect
(progn
,@body)
(close (tracking-stream-stream ,stream)))))
(defparameter *source-maps* (make-hash-table :test #'equal))
(defun reset-source-map (pathname &aux (truename (namestring (truename pathname))))
(setf (gethash truename *source-maps*)
(make-array 32 :fill-pointer 0 :adjustable t)))
(defun get-source-map (pathname &aux (truename (namestring (truename pathname))))
#+(or ccl cmucl ecl sbcl)
(multiple-value-bind (source-map present)
(gethash truename *source-maps*)
(if present
source-map
#+(or cmucl sbcl) (handler-bind
((error #'eclector.base:recover))
(with-tracking-stream (stream truename)
(do* ((client (make-instance 'tracking-client))
(source-map (make-array 32 :adjustable t :fill-pointer 0))
(form-map (eclector.parse-result:read client stream nil :eof)
(eclector.parse-result:read client stream nil :eof)))
((eq :eof form-map) (setf (gethash truename *source-maps*) source-map))
(vector-push-extend form-map source-map))))
#+(or ccl ecl) (with-tracking-stream (stream truename)
(prog (char
(left 0)
(right 0)
(source-map (make-array 32 :adjustable t :fill-pointer 0)))
next
(setf char (peek-char nil stream nil))
(when (or (null char)
(char= #\Tab char)
(char= #\Newline char))
(vector-push-extend (list left
right
(tracking-stream-line stream)
(1+ (- (tracking-stream-column stream) right)))
source-map)
(setf left right))
(unless char
(return (setf (gethash truename *source-maps*) source-map)))
(read-char stream nil)
(setf right (file-position stream))
(go next))))))
(defun source-line-column (pathname position1 #+(or cmucl sbcl) position2)
(handler-case
#+(or cmucl sbcl) (values-list (elt (elt (get-source-map pathname) position1) position2))
#+(or ccl ecl) (let ((record (find-if (lambda (record)
(< (1- (first record)) position1 (second record)))
(get-source-map pathname))))
(when record
(values (third record)
(+ (fourth record) position1))))
(error (condition)
(declare (ignore condition)))))
(defun file-position-to-line-column (pathname position)
(when (and pathname position)
(with-tracking-stream (stream pathname)
(prog ((index 0) char)
next
(setf char (peek-char nil stream nil))
(unless char
(return (values 0 0)))
(when (and (>= index position)
(not (member char '(#\Newline #\Return #\Space #\Tab) :test #'equal)))
(return (values (tracking-stream-line stream)
(tracking-stream-column stream))))
(read-char stream nil)
(incf index)
(go next)))))
#-clasp
(defmethod eclector.base:source-position (client (stream tracking-stream))
(list (tracking-stream-line stream)
(tracking-stream-column stream)))
#-clasp
(defclass tracking-client (eclector.parse-result:parse-result-client)
())
#-clasp
(defmethod eclector.parse-result:make-expression-result ((client tracking-client) (result (eql nil)) children source)
nil)
#-clasp
(defmethod eclector.parse-result:make-expression-result ((client tracking-client) result children source)
nil)
#-clasp
(defmethod eclector.parse-result:make-expression-result ((client tracking-client) (result list) children source)
(cons (car source)
(remove-if #'null (apply #'append children))))
#-clasp
(defun form-offset-to-line-column (pathname form-offset form-number)
(handler-bind
((error #'eclector.base:recover))
(when (and pathname form-offset form-number)
(with-tracking-stream (stream pathname)
(let ((client (make-instance 'tracking-client)))
(dotimes (count form-offset)
(eclector.parse-result:read client stream nil))
(let ((source-map (nreverse (eclector.parse-result:read client stream nil))))
(when (< form-number (length source-map))
(values-list (elt source-map form-number)))))))))
#+sbcl
(defvar *only-block-start-locations* nil)
#+sbcl
(defun first-code-location (debug-block)
(let ((found nil)
(first-code-location nil))
(sb-di:do-debug-block-locations (code-location debug-block)
(unless found
(setf first-code-location code-location)
(setf found t)))
first-code-location))
#+sbcl
(defun possible-breakpoints (function)
(let ((possible-breakpoints nil))
(sb-di:do-debug-fun-blocks (debug-block (sb-di::fun-debug-fun function))
(unless (sb-di:debug-block-elsewhere-p debug-block)
(if *only-block-start-locations*
(push (first-code-location debug-block) possible-breakpoints)
(sb-di:do-debug-block-locations (code-location debug-block)
(when (not (member (sb-di:code-location-kind code-location)
'(:call-site :internal-error)))
(push code-location possible-breakpoints))))))
(sort possible-breakpoints
(lambda (x y)
(or (< (sb-di:code-location-toplevel-form-offset x)
(sb-di:code-location-toplevel-form-offset y))
(< (sb-di:code-location-form-number x)
(sb-di:code-location-form-number y)))))))
(defun eval-with-bindings (form bindings)
(eval `(let ,(loop for (var . val) in bindings collect `(,var ',val))
(declare (ignorable ,@(mapcar #'car bindings)))
,form)))