40c0d3700acb8c451810596f1e9a995295101a0f
[sbcl.git] / src / compiler / xref.lisp
1 ;;;; xref facility
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
11
12 (in-package "SB!C")
13
14 (defvar *xref-kinds* '(:binds :calls :sets :references :macroexpands))
15
16 (defun record-component-xrefs (component)
17   (declare (type component component))
18   (when (policy *lexenv* (zerop store-xref-data))
19     (return-from record-component-xrefs))
20   (do ((block (block-next (component-head component)) (block-next block)))
21       ((null (block-next block)))
22     (let* ((this-cont (block-start block))
23            (last (block-last block)))
24       (flet ((handle-node (functional)
25                ;; Record xref information for all nodes in the block.
26                ;; Note that this code can get executed several times
27                ;; for the same block, if the functional is referenced
28                ;; from multiple XEPs.
29                (loop for node = (ctran-next this-cont) then (ctran-next (node-next node))
30                      until (eq node last)
31                      do (record-node-xrefs node functional))
32                ;; Properly record the deferred macroexpansion information
33                ;; that's been stored in the block.
34                (dolist (xref-data (block-macroexpands block))
35                  (record-xref :macroexpands
36                               (car xref-data)
37                               ;; We use the debug-name of the functional
38                               ;; as an identifier. This works quite nicely,
39                               ;; except for (fast/slow)-methods with non-symbol,
40                               ;; non-number eql specializers, for which
41                               ;; the debug-name doesn't map exactly
42                               ;; to the fdefinition of the method.
43                               functional
44                               nil
45                               (cdr xref-data)))))
46         (call-with-block-external-functionals block #'handle-node)))))
47
48 (defun call-with-block-external-functionals (block fun)
49   (let* ((functional (block-home-lambda block))
50          (seen nil))
51     (labels ((local-function-name-p (name)
52                (and (consp name)
53                     (member (car name)
54                             '(flet labels lambda))))
55              (handle-functional (functional)
56                ;; If a functional looks like a global function (has a
57                ;; XEP, isn't a local function or a lambda) record xref
58                ;; information for it. Otherwise recurse on the
59                ;; home-lambdas of all references to the functional.
60                (when (eq (functional-kind functional) :external)
61                  (let ((entry (functional-entry-fun functional)))
62                    (when entry
63                      (let ((name (functional-debug-name entry)))
64                        (unless (local-function-name-p name)
65                          (return-from handle-functional
66                            (funcall fun entry)))))))
67                ;; Recurse only if we haven't already seen the
68                ;; functional.
69                (unless (member functional seen)
70                  (push functional seen)
71                  (dolist (ref (functional-refs functional))
72                    (handle-functional (node-home-lambda ref))))))
73       (unless (or (eq :deleted (functional-kind functional))
74                   ;; If the block came from an inlined global
75                   ;; function, ignore it.
76                   (and (functional-inlinep functional)
77                        (symbolp (functional-debug-name functional))))
78         (handle-functional functional)))))
79
80 (defun record-node-xrefs (node context)
81   (declare (type node node))
82   (etypecase node
83     ((or creturn cif entry combination mv-combination cast))
84     (ref
85      (let ((leaf (ref-leaf node)))
86        (typecase leaf
87          (global-var
88           (let* ((name (leaf-debug-name leaf)))
89             (case (global-var-kind leaf)
90               ;; Reading a special
91               (:special
92                (record-xref :references name context node nil))
93               ;; Calling a function
94               (:global-function
95                (record-xref :calls name context node nil)))))
96          ;; Inlined global function
97          (clambda
98           (when (functional-inlinep leaf)
99             (let ((name (leaf-debug-name leaf)))
100               ;; FIXME: we should store the original var into the
101               ;; functional when creating inlined-functionals, so that
102               ;; we could just check whether it was a global-var,
103               ;; rather then needing to guess based on the debug-name.
104               (when (or (symbolp name)
105                         ;; Any non-SETF non-symbol names will
106                         ;; currently be either non-functions or
107                         ;; internals.
108                         (and (consp name)
109                              (equal (car name) 'setf)))
110                 ;; TODO: a WHO-INLINES xref-kind could be useful
111                 (record-xref :calls name context node nil)))))
112          ;; Reading a constant
113          (constant
114           (let* ((name (constant-%source-name leaf)))
115             (record-xref :references name context node nil))))))
116     ;; Setting a special variable
117     (cset
118      (let* ((var (set-var node)))
119        (when (and (global-var-p var)
120                   (eq :special (global-var-kind var)))
121          (record-xref :sets
122                       (leaf-debug-name var)
123                       context
124                       node
125                       nil))))
126     ;; Binding a special variable
127     (bind
128      (let ((vars (lambda-vars (bind-lambda node))))
129        (dolist (var vars)
130          (when (lambda-var-specvar var)
131            (record-xref :binds
132                         (lambda-var-%source-name var)
133                         context
134                         node
135                         nil)))))))
136
137 (defun internal-name-p (what)
138   ;; Don't store XREF information for internals. We define as internal
139   ;; anything named only by symbols from either implementation
140   ;; packages, COMMON-LISP or KEYWORD. The last one is useful for
141   ;; example when dealing with ctors.
142   (typecase what
143     (list
144      (every #'internal-name-p what))
145     (symbol
146      (member (symbol-package what)
147              (load-time-value (list* (find-package "COMMON-LISP")
148                                      (find-package "KEYWORD")
149                                      (remove-if-not
150                                       (lambda (package)
151                                         (= (mismatch "SB!"
152                                                      (package-name package))
153                                            3))
154                                       (list-all-packages))))))
155     (t t)))
156
157 (defun record-xref (kind what context node path)
158   (unless (internal-name-p what)
159     (let ((path (reverse
160                  (source-path-original-source
161                   (or path
162                       (node-source-path node))))))
163       (push (list what path)
164             (getf (functional-xref context) kind)))))
165
166 (defun record-macroexpansion (what block path)
167   (unless (internal-name-p what)
168     (push (cons what path) (block-macroexpands block))))
169
170 ;;; Pack the xref table that was stored for a functional into a more
171 ;;; space-efficient form, and return that packed form.
172 (defun pack-xref-data (xref-data)
173   (when xref-data
174     (let ((array (make-array (length *xref-kinds*))))
175       (loop for key in *xref-kinds*
176             for i from 0
177             for values = (remove-duplicates (getf xref-data key)
178                                             :test #'equal)
179             for flattened = (reduce #'append values :from-end t)
180             collect (setf (aref array i)
181                           (when flattened
182                             (make-array (length flattened)
183                                         :initial-contents flattened))))
184       array)))