summaryrefslogtreecommitdiff
path: root/Apps/Clouseau/src/objects/function.lisp
blob: acaf0ccaf72d6bd1b8cbcf6b26597fcc536904dd (plain)
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
;;; ---------------------------------------------------------------------------
;;;   License: LGPL-2.1+ (See file 'Copyright' for details).
;;; ---------------------------------------------------------------------------
;;;
;;;  (c) copyright 2018-2020 Jan Moringen <jmoringe@techfak.uni-bielefeld.de>
;;;
;;; ---------------------------------------------------------------------------
;;;
;;; Places, inspection methods and commands for ordinary functions,
;;; closures, generic functions and methods.
;;;

(cl:in-package #:clouseau)

;;; Utilities

(defun safe-function-lambda-expression (function)
  ;; SBCL can signal UNBOUND-SLOT if FUNCTION is the prototype of a
  ;; funcallable instance class.
  #+sbcl (handler-case
             (function-lambda-expression function)
           (unbound-slot () nil))
  #-sbcl (function-lambda-expression function))

(defun function-name (function)
  (when-let ((name (nth-value 2 (safe-function-lambda-expression function))))
    (unless (typep name '(cons (eql lambda)))
      name)))

(defun function-lambda-list
    (function &optional (expression (safe-function-lambda-expression function)))
  #+sbcl (declare (ignore expression))
  (values #+sbcl (sb-introspect:function-lambda-list function) #+sbcl t
          #-sbcl (second expression) #-sbcl expression))

(defun function-closure-p (function)
  (and (nth-value 1 (safe-function-lambda-expression function))
       #+sbcl (sb-kernel:closurep function)))

(defun function-traced-p (function)
  #-sbcl nil
  #+sbcl (gethash function sb-debug::*traced-funs*))

#+sbcl
(defun trace-function (function)
  (sb-debug::trace-1 (function-name function)
                     (sb-debug::make-trace-info)
                     function))

#+sbcl
(defun untrace-function (function)
  (sb-debug::untrace-1 (function-name function)))

;;; `method-place'

(defclass method-place (read-only-place)
  ())

(defmethod supportsp ((place method-place) (operation (eql 'remove-value)))
  t)

(defmethod value ((place method-place))
  (cell place))

(defmethod remove-value ((place method-place))
  (remove-method (container place) (value place)))

(defmethod qualifiers ((place method-place))
  (method-qualifiers (cell place)))

(defmethod specializers ((place method-place))
  (c2mop:method-specializers (cell place)))

;;; `closed-over-place'

(defclass closed-over-place (read-only-place)
  ())

#+sbcl
(defmethod value ((place closed-over-place))
  (let ((closure (container place))
        (index   (cell place)))
    (sb-kernel:%closure-index-ref closure index)))

;;; Object states

(defclass inspected-function (inspected-identity-object-mixin
                              inspected-object)
  ((%disassembly-style :initarg  :disassembly-style
                       :type     (member nil t) ; could be text vs. graph later
                       :accessor disassembly-style
                       :initform nil)))

(defmethod object-state-class ((object function) (place t))
  'inspected-function)

(defclass inspected-funcallable-standard-object (inspected-function
                                                 inspected-instance)
  ())

(defmethod object-state-class ((object c2mop:funcallable-standard-object)
                               (place  t))
  'inspected-funcallable-standard-object)

(defclass inspected-generic-function (inspected-funcallable-standard-object)
  ()
  (:default-initargs
   :slot-style nil))

(defmethod object-state-class ((object generic-function) (place t))
  'inspected-generic-function)

(defclass inspected-method (inspected-instance
                            remembered-collapsed-style-mixin)
  ()
  (:default-initargs
   :slot-style nil))

(defmethod object-state-class ((object method) (place t))
  'inspected-method)

(defmethod make-object-state ((object method) (place method-place))
  (let ((class (object-state-class object place)))
    (make-instance class :place place :style :class-only)))

;;; Object inspection methods

;;; `function'

(defmethod inspect-object-using-state ((object function)
                                       (state  inspected-function)
                                       (style  (eql :expanded-header))
                                       (stream t))
  (inspect-class-as-name (class-of object) stream)

  (write-char #\Space stream)
  (let ((name (function-name object)))
    (with-placeholder-if-empty (stream)
      ((not name) "no name")
      (t          (princ name stream)))))

(defmethod inspect-object-using-state :after ((object function)
                                              (state  inspected-function)
                                              (style  (eql :badges))
                                              (stream t))
  ;; Closure
  (when (function-closure-p object)
    (write-char #\Space stream)
    (badge stream "closure"))
  ;; Traced
  (when (function-traced-p object)
    (write-string " " stream)
    (badge stream "traced"))
  ;; Interpreted
  (unless (compiled-function-p object)
    (write-string " " stream)
    (badge stream "interpreted")))

(defun inspect-closure-cells (function stream)
  (formatting-table (stream)
    #+sbcl
    (loop :for i :below (1- (sb-kernel:get-closure-length function))
          :do (format-place-row stream function 'closed-over-place i))))

(defmethod inspect-object-using-state ((object function)
                                       (state  inspected-function)
                                       (style  (eql :expanded-body))
                                       (stream t))
  (multiple-value-bind (expression closurep)
      (safe-function-lambda-expression object)
    (with-preserved-cursor-x (stream)
      (formatting-table (stream)
        (multiple-value-bind (lambda-list lambda-list-p)
            (function-lambda-list object expression)
          ;; Name
          (format-place-row stream object 'reader-place 'function-name
                            :label "Name")
          ;; Lambda list
          (formatting-row (stream)
            (formatting-place (object 'pseudo-place lambda-list present inspect)
              (with-style (stream :slot-like)
                (formatting-cell (stream) (write-string "Lambda list" stream))
                (formatting-cell (stream) (present stream)))
              (formatting-cell (stream)
                (with-placeholder-if-empty (stream)
                  ((not lambda-list-p)
                   "not available")
                  (t
                   (inspect stream)))))))
        ;; Type
        #+sbcl
        (format-place-row stream object 'reader-place 'sb-introspect:function-type
                          :label "Type")))
    ;; Documentation
    (print-documentation object stream)
    ;; Close cells
    (when (and closurep #+sbcl (sb-kernel:closurep object))
      (with-section (stream) "Closure cells"
        (inspect-closure-cells object stream)))
    ;; Disassembly
    (when (disassembly-style state)
      (display-disassembly object stream))))

;;; `funcallable-standard-object'

(defmethod inspect-object-using-state :after ((object function)
                                              (state  inspected-funcallable-standard-object)
                                              (style  (eql :badges))
                                              (stream t))
  (write-string " " stream)
  (badge stream "funcallable"))

(defmethod inspect-object-using-state ((object c2mop:funcallable-standard-object)
                                       (state  inspected-funcallable-standard-object)
                                       (style  (eql :expanded-body))
                                       (stream t))
  ;; Function
  (call-next-method)

  ;; Slots
  (with-section (stream) "Slots"
    (inspect-slots object (slot-style state) stream)))

;;; `generic-function'

(defun max-specializers (methods)
  (reduce #'max methods :key (compose #'length #'c2mop:method-specializers)))

(defun inspect-method-list (generic-function methods stream
                            &key generic-function-name
                                 (specializer-column-count
                                  (max-specializers methods)))
  (formatting-table (stream)
    (formatting-row (stream)
      (if generic-function-name
          (formatting-header (stream) "Generic function" "Qualifiers" "Specializers")
          (formatting-header (stream)                    "Qualifiers" "Specializers")))
    (map nil (lambda (method)
               (let ((generic-function (or generic-function
                                           (c2mop:method-generic-function method))))
                 (formatting-place (generic-function 'method-place method
                                    present inspect :place-var place)
                   (formatting-row (stream)
                     (with-style (stream :slot-like)
                       (when generic-function-name
                         (formatting-cell (stream)
                           (princ (c2mop:generic-function-name generic-function) ; TODO present
                                  stream)))
                       (formatting-cell (stream)
                         (format-items (qualifiers place) :stream stream))
                       (let* ((specializers (specializers place))
                              (missing      (- specializer-column-count
                                               (length specializers))))
                         (map nil (lambda (specializer)
                                    (formatting-cell (stream)
                                      (when specializer
                                        (with-print-error-handling (stream)
                                          (typecase specializer
                                            (class (inspect-class-as-name specializer stream))
                                            (t     (prin1 `(eql ,(c2mop:eql-specializer-object specializer)) stream))))))) ; TODO should be inspectable
                              (append specializers (make-list missing)))))
                     (formatting-cell (stream)
                       (present stream))
                     (formatting-cell (stream)
                       (inspect stream))))))
         methods)))

(defmethod inspect-object-using-state ((object generic-function)
                                       (state  inspected-generic-function)
                                       (style  (eql :expanded-body))
                                       (stream t))
  ;; Funcallable standard object
  (call-next-method)

  ;; method class
  ;; method combination
  ;; Methods
  (with-section (stream) "Methods"
    (let ((methods (c2mop:generic-function-methods object)))
      (with-placeholder-if-empty (stream)
        ((null methods)
         "No methods~%")
        (t
         (inspect-method-list object methods stream))))))

;; `method'

(defmethod inspect-object-using-state ((object method)
                                       (state  inspected-method)
                                       (style  (eql :class-only))
                                       (stream t))
  (princ (class-name (class-of object)) stream))

(defmethod inspect-object-using-state ((object method)
                                       (state  inspected-method)
                                       (style  (eql :expanded-body))
                                       (stream t))
  ;; Documentation
  (print-documentation object stream)
  ;; Slots
  (call-next-method))

;;; Commands

;;; Generic functions

(define-command (com-remove-methods :command-table inspector-command-table
                                    :name          "Remove all Methods")
    ((object 'inspected-generic-function))
  (with-command-error-handling ("Could not remove all methods")
      (let ((object (object object)))
        (map nil (curry #'remove-method object)
             (c2mop:generic-function-methods object)))))

(define-presentation-to-command-translator
    inspected-generic-function->com-remove-methods
    (inspected-generic-function com-remove-methods inspector-command-table
     :tester ((object)
              (not (null (ignore-errors
                          (c2mop:generic-function-methods (object object))))))
     :priority -1
     :documentation "Remove all methods"
     :pointer-documentation ((object stream)
                             (with-print-error-handling (stream)
                               (with-safe-and-terse-printing (stream)
                                 (format stream "~@<Remove methods ~
                                                 from the generic ~
                                                 function ~A~@:>"
                                        (object object))))))
    (object)
  (list object))

;;; Tracing

#+sbcl
(define-command (com-trace :command-table inspector-command-table
                           :name          "Trace Function")
    ((object 'inspected-function))
  (let ((function (object object)))
    (with-command-error-handling ("Could not trace ~A" function)
      (trace-function function))))

#+sbcl
(define-presentation-to-command-translator inspected-function->com-trace
    (inspected-function com-trace inspector-command-table
     :tester ((object)
              (let ((function (object object)))
                (and (function-name function)
                     (not (function-traced-p function)))))
     :priority -1
     :documentation "Trace function"
     :pointer-documentation ((object stream)
                             (with-print-error-handling (stream)
                               (with-safe-and-terse-printing (stream)
                                 (format stream "~@<Trace the function ~A~@:>"
                                         (object object))))))
    (object)
  (list object))

#+sbcl
(define-command (com-untrace :command-table inspector-command-table
                             :name          "Untrace Function")
    ((object 'inspected-function))
  (let ((function (object object)))
    (with-command-error-handling ("Could not untrace ~A" object)
      (untrace-function function))))

#+sbcl
(define-presentation-to-command-translator inspected-function->com-untrace
    (inspected-function com-untrace inspector-command-table
     :tester ((object)
              (let ((function (object object)))
                (and (function-name function)
                     (function-traced-p function))))
     :priority -1
     :documentation "Untrace function"
     :pointer-documentation ((object stream)
                             (with-print-error-handling (stream)
                               (with-safe-and-terse-printing (stream)
                                 (format stream "~@<Untrace the function ~A~@:>"
                                         (object object))))))
  (object)
  (list object))

;;; Disassembling

(define-command (com-show-disassembly :command-table inspector-command-table
                                      :name          t)
    ((object 'inspected-function))
  (setf (disassembly-style object) t))

(define-presentation-to-command-translator inspected-function->com-show-disassembly
    (inspected-function com-show-disassembly inspector-command-table
     :tester ((object)
              (and (eq (style object) :expanded)
                   (not (disassembly-style object))))
     :priority -1
     :documentation "Show disassembly"
     :pointer-documentation ((object stream)
                             (with-print-error-handling (stream)
                               (with-safe-and-terse-printing (stream)
                                 (format stream "~@<Show disassembly ~
                                                 for function ~A~@:>"
                                         (object object))))))
    (object)
  (list object))

(define-command (com-hide-disassembly :command-table inspector-command-table
                                      :name          t)
    ((object 'inspected-function))
  (setf (disassembly-style object) nil))

(define-presentation-to-command-translator inspected-function->com-hide-disassembly
    (inspected-function com-hide-disassembly inspector-command-table
     :tester ((object)
              (and (eq (style object) :expanded)
                   (disassembly-style object)))
     :priority -1
     :documentation "Hide disassembly"
     :pointer-documentation ((object stream)
                             (with-print-error-handling (stream)
                               (with-safe-and-terse-printing (stream)
                                 (format stream "~@<Hide disassembly ~
                                                 for function ~A~@:>"
                                         (object object))))))
    (object)
  (list object))