Update copyright notices for 2013.
[emacs.git] / lisp / cedet / semantic / analyze / refs.el
blob93dd710a67d422a6a2c4a41898ccb3a76f9ea152
1 ;;; semantic/analyze/refs.el --- Analysis of the references between tags.
3 ;; Copyright (C) 2008-2013 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;; Analyze the references between tags.
26 ;; The original purpose of these analysis is to provide a way to jump
27 ;; between a prototype and implementation.
29 ;; Finding all prototype/impl matches is hard because you have to search
30 ;; through the entire set of allowed databases to capture all possible
31 ;; refs. The core analysis class stores basic starting point, and then
32 ;; entire raw search data, which is expensive to calculate.
34 ;; Once the raw data is available, queries for impl, prototype, or
35 ;; perhaps other things become cheap.
37 (require 'semantic)
38 (require 'semantic/analyze)
39 (require 'semantic/db-find)
40 (eval-when-compile (require 'semantic/find))
42 (declare-function data-debug-new-buffer "data-debug")
43 (declare-function data-debug-insert-object-slots "eieio-datadebug")
44 (declare-function semantic-momentary-highlight-tag "semantic/decorate")
46 ;;; Code:
47 (defclass semantic-analyze-references ()
48 ((tag :initarg :tag
49 :type semantic-tag
50 :documentation
51 "The starting TAG we are providing references analysis for.")
52 (tagdb :initarg :tagdb
53 :documentation
54 "The database that tag can be found in.")
55 (scope :initarg :scope
56 :documentation "A Scope object.")
57 (rawsearchdata :initarg :rawsearchdata
58 :documentation
59 "The raw search data for TAG's name across all databases.")
60 ;; Note: Should I cache queried data here? I expect that searching
61 ;; through rawsearchdata will be super-fast, so why bother?
63 "Class containing data from a semantic analysis.")
65 (define-overloadable-function semantic-analyze-tag-references (tag &optional db)
66 "Analyze the references for TAG.
67 Returns a class with information about TAG.
69 Optional argument DB is a database. It will be used to help
70 locate TAG.
72 Use `semantic-analyze-current-tag' to debug this fcn.")
74 (defun semantic-analyze-tag-references-default (tag &optional db)
75 "Analyze the references for TAG.
76 Returns a class with information about TAG.
78 Optional argument DB is a database. It will be used to help
79 locate TAG.
81 Use `semantic-analyze-current-tag' to debug this fcn."
82 (when (not (semantic-tag-p tag)) (signal 'wrong-type-argument (list 'semantic-tag-p tag)))
83 (let ((allhits nil)
84 (scope nil)
86 (save-excursion
87 (semantic-go-to-tag tag db)
88 (setq scope (semantic-calculate-scope))
90 (setq allhits (semantic--analyze-refs-full-lookup tag scope t))
92 (semantic-analyze-references (semantic-tag-name tag)
93 :tag tag
94 :tagdb db
95 :scope scope
96 :rawsearchdata allhits)
97 )))
99 ;;; METHODS
101 ;; These accessor methods will calculate the useful bits from the context, and cache values
102 ;; into the context.
103 (defmethod semantic-analyze-refs-impl ((refs semantic-analyze-references) &optional in-buffer)
104 "Return the implementations derived in the reference analyzer REFS.
105 Optional argument IN-BUFFER indicates that the returned tag should be in an active buffer."
106 (let ((allhits (oref refs rawsearchdata))
107 (tag (oref refs :tag))
108 (impl nil)
110 (semanticdb-find-result-mapc
111 (lambda (T DB)
112 "Examine T in the database DB, and sont it."
113 (let* ((ans (semanticdb-normalize-one-tag DB T))
114 (aT (cdr ans))
115 (aDB (car ans))
117 (when (and (not (semantic-tag-prototype-p aT))
118 (semantic-tag-similar-p tag aT
119 :prototype-flag
120 :parent
121 :typemodifiers))
122 (when in-buffer (save-excursion (semantic-go-to-tag aT aDB)))
123 (push aT impl))))
124 allhits)
125 impl))
127 (defmethod semantic-analyze-refs-proto ((refs semantic-analyze-references) &optional in-buffer)
128 "Return the prototypes derived in the reference analyzer REFS.
129 Optional argument IN-BUFFER indicates that the returned tag should be in an active buffer."
130 (let ((allhits (oref refs rawsearchdata))
131 (tag (oref refs :tag))
132 (proto nil))
133 (semanticdb-find-result-mapc
134 (lambda (T DB)
135 "Examine T in the database DB, and sort it."
136 (let* ((ans (semanticdb-normalize-one-tag DB T))
137 (aT (cdr ans))
138 (aDB (car ans))
140 (when (and (semantic-tag-prototype-p aT)
141 (semantic-tag-similar-p tag aT
142 :prototype-flag
143 :parent
144 :typemodifiers))
145 (when in-buffer (save-excursion (semantic-go-to-tag aT aDB)))
146 (push aT proto))))
147 allhits)
148 proto))
150 ;;; LOOKUP
152 (defun semantic--analyze-refs-full-lookup (tag scope &optional noerror)
153 "Perform a full lookup for all occurrences of TAG in the current project.
154 TAG should be the tag currently under point.
155 SCOPE is the scope the cursor is in. From this a list of parents is
156 derived. If SCOPE does not have parents, then only a simple lookup is done.
157 Optional argument NOERROR means don't error if the lookup fails."
158 (if (not (oref scope parents))
159 ;; If this tag has some named parent, but is not
160 (semantic--analyze-refs-full-lookup-simple tag noerror)
162 ;; We have some sort of lineage we need to consider when we do
163 ;; our side lookup of tags.
164 (semantic--analyze-refs-full-lookup-with-parents tag scope)
167 (defun semantic--analyze-refs-find-child-in-find-results (find-results name class)
168 "Find in FIND-RESULT a tag NAME which is a child of a tag in FIND-RESULTS.
169 CLASS is the class of the tag that ought to be returned."
170 (let ((ans nil)
171 (subans nil))
172 ;; Loop over each segment of the find results.
173 (dolist (FDB find-results)
174 (setq subans nil)
175 ;; Loop over each tag in the find results.
176 (dolist (T (cdr FDB))
177 ;; For each tag, get the children.
178 (let* ((chil (semantic-tag-type-members T))
179 (match (semantic-find-tags-by-name name chil)))
180 ;; Go over the matches, looking for matching tag class.
181 (dolist (M match)
182 (when (semantic-tag-of-class-p M class)
183 (push M subans)))))
184 ;; Store current matches into a new find results.
185 (when subans
186 (push (cons (car FDB) subans) ans))
188 ans))
190 (defun semantic--analyze-refs-find-tags-with-parent (find-results parents)
191 "Find in FIND-RESULTS all tags with PARENTS.
192 NAME is the name of the tag needing finding.
193 PARENTS is a list of names."
194 (let ((ans nil) (usingnames nil))
195 ;; Loop over the find-results passed in.
196 (semanticdb-find-result-mapc
197 (lambda (tag db)
198 (let* ((p (semantic-tag-named-parent tag))
199 (ps (when (stringp p) (semantic-analyze-split-name p))))
200 (when (stringp ps) (setq ps (list ps)))
201 (when ps
202 ;; If there is a perfect match, then use it.
203 (if (equal ps parents)
204 (push (list db tag) ans))
205 ;; No match, find something from our list of using names.
206 ;; Do we need to split UN?
207 (save-excursion
208 (semantic-go-to-tag tag db)
209 (setq usingnames nil)
210 (let ((imports (semantic-ctxt-imported-packages)))
211 ;; Derive the names from all the using statements.
212 (mapc (lambda (T)
213 (setq usingnames
214 (cons (semantic-format-tag-name-from-anything T) usingnames)))
215 imports))
216 (dolist (UN usingnames)
217 (when (equal (cons UN ps) parents)
218 (push (list db tag) ans)
219 (setq usingnames (cdr usingnames))))
220 ))))
221 find-results)
222 ans))
224 (defun semantic--analyze-refs-full-lookup-with-parents (tag scope)
225 "Perform a lookup for all occurrences of TAG based on TAG's SCOPE.
226 TAG should be the tag currently under point."
227 (let* ((classmatch (semantic-tag-class tag))
228 (plist (mapcar (lambda (T) (semantic-tag-name T)) (oref scope parents)))
229 ;; The first item in the parent list
230 (name (car plist))
231 ;; Stuff from the simple list.
232 (simple (semantic--analyze-refs-full-lookup-simple tag t))
233 ;; Find all hits for the first parent name.
234 (brute (semanticdb-find-tags-collector
235 (lambda (table tags)
236 (semanticdb-deep-find-tags-by-name-method table name tags)
238 nil nil t))
239 ;; Prime the answer.
240 (answer (semantic--analyze-refs-find-tags-with-parent simple plist))
242 ;; First parent is already search to initialize "brute".
243 (setq plist (cdr plist))
245 ;; Go through the list of parents, and try to find matches.
246 ;; As we cycle through plist, for each level look for NAME,
247 ;; and compare the named-parent, and also dive into the next item of
248 ;; plist.
249 (while (and plist brute)
251 ;; Find direct matches
252 (let* ((direct (semantic--analyze-refs-find-child-in-find-results
253 brute (semantic-tag-name tag) classmatch))
254 (pdirect (semantic--analyze-refs-find-tags-with-parent
255 direct plist)))
256 (setq answer (append pdirect answer)))
258 ;; The next set of search items.
259 (setq brute (semantic--analyze-refs-find-child-in-find-results
260 brute (car plist) 'type))
262 (setq plist (cdr plist)))
264 ;; Brute now has the children from the very last match.
265 (let* ((direct (semantic--analyze-refs-find-child-in-find-results
266 brute (semantic-tag-name tag) classmatch))
268 (setq answer (append direct answer)))
270 answer))
272 (defun semantic--analyze-refs-full-lookup-simple (tag &optional noerror)
273 "Perform a simple lookup for occurrences of TAG in the current project.
274 TAG should be the tag currently under point.
275 Optional NOERROR means don't throw errors on failure to find something.
276 This only compares the tag name, and does not infer any matches in namespaces,
277 or parts of some other data structure.
278 Only works for tags in the global namespace."
279 (let* ((name (semantic-tag-name tag))
280 (brute (semanticdb-find-tags-collector
281 (lambda (table tags)
282 (semanticdb-find-tags-by-name-method table name tags)
284 nil ;; This may need to be the entire project??
285 nil t))
288 (when (and (not brute) (not noerror))
289 ;; An error, because tag under point ought to be found.
290 (error "Cannot find any references to %s in wide search" name))
292 (let* ((classmatch (semantic-tag-class tag))
293 (RES
294 (semanticdb-find-tags-collector
295 (lambda (table tags)
296 (semantic-find-tags-by-class classmatch tags)
297 ;; @todo - Add parent check also.
299 brute nil)))
301 (when (and (not RES) (not noerror))
302 (error "Cannot find any definitions for %s in wide search"
303 (semantic-tag-name tag)))
305 ;; Return the matching tags and databases.
306 RES)))
309 ;;; USER COMMANDS
311 ;;;###autoload
312 (defun semantic-analyze-current-tag ()
313 "Analyze the tag under point."
314 (interactive)
315 (let* ((tag (semantic-current-tag))
316 (start (current-time))
317 (sac (semantic-analyze-tag-references tag))
318 (end (current-time))
320 (message "Analysis took %.2f seconds." (semantic-elapsed-time start end))
321 (if sac
322 (progn
323 (require 'eieio-datadebug)
324 (data-debug-new-buffer "*Analyzer Reference ADEBUG*")
325 (data-debug-insert-object-slots sac "]"))
326 (message "No Context to analyze here."))))
328 ;;;###autoload
329 (defun semantic-analyze-proto-impl-toggle ()
330 "Toggle between the implementation, and a prototype of tag under point."
331 (interactive)
332 (require 'semantic/decorate)
333 (semantic-fetch-tags)
334 (let* ((tag (semantic-current-tag))
335 (sar (if tag
336 (semantic-analyze-tag-references tag)
337 (error "Point must be in a declaration")))
338 (target (if (semantic-tag-prototype-p tag)
339 (car (semantic-analyze-refs-impl sar t))
340 (car (semantic-analyze-refs-proto sar t))))
343 (when (not target)
344 (error "Could not find suitable %s"
345 (if (semantic-tag-prototype-p tag) "implementation" "prototype")))
347 (push-mark)
348 (semantic-go-to-tag target)
349 (switch-to-buffer (current-buffer))
350 (semantic-momentary-highlight-tag target))
353 (provide 'semantic/analyze/refs)
355 ;; Local variables:
356 ;; generated-autoload-file: "../loaddefs.el"
357 ;; generated-autoload-load-name: "semantic/analyze/refs"
358 ;; End:
360 ;;; semantic/analyze/refs.el ends here