1 ;;; db-typecache.el --- Manage Datatypes
3 ;; Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <eric@siege-engine.com>
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/>.
24 ;; Manage a datatype cache.
26 ;; For typed languages like C++ collect all known types from various
27 ;; headers, merge namespaces, and expunge duplicates.
29 ;; It is likely this feature will only be needed for C/C++.
32 (require 'semantic/db)
33 (require 'semantic/db-find)
34 (require 'semantic/analyze/fcn)
36 ;; For semantic-find-tags-by-* macros
37 (eval-when-compile (require 'semantic/find))
39 (declare-function data-debug-insert-thing "data-debug")
40 (declare-function data-debug-new-buffer "data-debug")
41 (declare-function semantic-sort-tags-by-name-then-type-increasing "semantic/sort")
42 (declare-function semantic-scope-tag-clone-with-scope "semantic/scope")
48 (defclass semanticdb-typecache ()
49 ((filestream :initform nil
51 "Fully sorted/merged list of tags within this buffer.")
52 (includestream :initform nil
54 "Fully sorted/merged list of tags from this file's includes list.")
57 "The searchable tag stream for this cache.
58 NOTE: Can I get rid of this? Use a hashtable instead?")
59 (dependants :initform nil
61 "Any other object that is dependent on typecache results.
62 Said object must support `semantic-reset' methods.")
63 ;; @todo - add some sort of fast-hash.
64 ;; @note - Rebuilds in large projects already take a while, and the
65 ;; actual searches are pretty fast. Really needed?
67 "Structure for maintaining a typecache.")
69 (defmethod semantic-reset ((tc semanticdb-typecache))
70 "Reset the object IDX."
71 (oset tc filestream nil)
72 (oset tc includestream nil)
76 (mapc 'semantic-reset (oref tc dependants))
77 (oset tc dependants nil)
80 (defmethod semanticdb-typecache-notify-reset ((tc semanticdb-typecache))
81 "Do a reset from a notify from a table we depend on."
82 (oset tc includestream nil)
83 (mapc 'semantic-reset (oref tc dependants))
84 (oset tc dependants nil)
87 (defmethod semanticdb-partial-synchronize ((tc semanticdb-typecache)
89 "Reset the typecache based on a partial reparse."
90 (when (semantic-find-tags-by-class 'include new-tags)
91 (oset tc includestream nil)
92 (mapc 'semantic-reset (oref tc dependants))
93 (oset tc dependants nil)
96 (when (semantic-find-tags-by-class 'type new-tags)
98 (oset tc filestream nil)
99 t ;; Return true, our core file tags have changed in a relavant way.
105 (defun semanticdb-typecache-add-dependant (dep)
106 "Add into the local typecache a dependant DEP."
107 (let* ((table semanticdb-current-table)
108 ;;(idx (semanticdb-get-table-index table))
109 (cache (semanticdb-get-typecache table))
111 (object-add-to-list cache 'dependants dep)))
113 (defun semanticdb-typecache-length(thing)
116 (cond ((semanticdb-typecache-child-p thing)
117 (length (oref thing stream)))
118 ((semantic-tag-p thing)
119 (length (semantic-tag-type-members thing)))
120 ((and (listp thing) (semantic-tag-p (car thing)))
127 (defmethod semanticdb-get-typecache ((table semanticdb-abstract-table))
128 "Retrieve the typecache from the semanticdb TABLE.
129 If there is no table, create one, and fill it in."
130 (semanticdb-refresh-table table)
131 (let* ((idx (semanticdb-get-table-index table))
132 (cache (oref idx type-cache))
135 ;; Make sure we have a cache object in the DB index.
137 ;; The object won't change as we fill it with stuff.
138 (setq cache (semanticdb-typecache (semanticdb-full-filename table)))
139 (oset idx type-cache cache))
143 (defmethod semanticdb-have-typecache-p ((table semanticdb-abstract-table))
144 "Return non-nil (the typecache) if TABLE has a pre-calculated typecache."
145 (let* ((idx (semanticdb-get-table-index table)))
146 (oref idx type-cache)))
149 ;;; DATABASE TYPECACHE
151 ;; A full database can cache the types across its files.
153 ;; Unlike file based caches, this one is a bit simpler, and just needs
154 ;; to get reset when a table gets updated.
156 (defclass semanticdb-database-typecache (semanticdb-abstract-db-cache)
157 ((stream :initform nil
159 "The searchable tag stream for this cache.")
161 "Structure for maintaining a typecache.")
163 (defmethod semantic-reset ((tc semanticdb-database-typecache))
164 "Reset the object IDX."
168 (defmethod semanticdb-synchronize ((cache semanticdb-database-typecache)
170 "Synchronize a CACHE with some NEW-TAGS."
173 (defmethod semanticdb-partial-synchronize ((cache semanticdb-database-typecache)
175 "Synchronize a CACHE with some changed NEW-TAGS."
178 (defmethod semanticdb-get-typecache ((db semanticdb-project-database))
179 "Retrieve the typecache from the semantic database DB.
180 If there is no table, create one, and fill it in."
181 (semanticdb-cache-get db semanticdb-database-typecache)
185 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
189 ;; Managing long streams of tags representing data types.
191 (defun semanticdb-typecache-apply-filename (file stream)
192 "Apply the filename FILE to all tags in STREAM."
195 (setq new (cons (semantic-tag-copy (car stream) nil file)
197 ;The below is handled by the tag-copy fcn.
198 ;(semantic--tag-put-property (car new) :filename file)
199 (setq stream (cdr stream)))
203 (defsubst semanticdb-typecache-safe-tag-members (tag)
204 "Return a list of members for TAG that are safe to permute."
205 (let ((mem (semantic-tag-type-members tag))
206 (fname (semantic-tag-file-name tag)))
208 (setq mem (semanticdb-typecache-apply-filename fname mem))
209 (copy-sequence mem))))
211 (defsubst semanticdb-typecache-safe-tag-list (tags table)
212 "Make the tag list TAGS found in TABLE safe for the typecache.
213 Adds a filename and copies the tags."
214 (semanticdb-typecache-apply-filename
215 (semanticdb-full-filename table)
218 (defun semanticdb-typecache-merge-streams (cache1 cache2)
219 "Merge into CACHE1 and CACHE2 together. The Caches will be merged in place."
220 (if (or (and (not cache1) (not cache2))
221 (and (not (cdr cache1)) (not cache2))
222 (and (not cache1) (not (cdr cache2))))
223 ;; If all caches are empty OR
224 ;; cache1 is length 1 and no cache2 OR
225 ;; no cache1 and length 1 cache2
227 ;; then just return the cache, and skip all this merging stuff.
230 ;; Assume we always have datatypes, as this typecache isn't really
231 ;; useful without a typed language.
232 (require 'semantic/sort)
233 (let ((S (semantic-sort-tags-by-name-then-type-increasing
234 ;; I used to use append, but it copied cache1 but not cache2.
235 ;; Since sort was permuting cache2, I already had to make sure
236 ;; the caches were permute-safe. Might as well use nconc here.
237 (nconc cache1 cache2)))
242 ;; With all the tags in order, we can loop over them, and when
243 ;; two have the same name, we can either throw one away, or construct
244 ;; a fresh new tag merging the items together.
246 (setq prev (car ans))
249 ;; CASE 1 - First item
252 (not (string= (semantic-tag-name next)
253 (semantic-tag-name prev))))
254 (setq ans (cons next ans))
255 ;; ELSE - We have a NAME match.
256 (setq type (semantic-tag-type next))
257 (if (semantic-tag-of-type-p prev type) ; Are they the same datatype
258 ;; Same Class, we can do a merge.
260 ((and (semantic-tag-of-class-p next 'type)
261 (string= type "namespace"))
262 ;; Namespaces - merge the children together.
264 (semantic-tag-new-type
265 (semantic-tag-name prev) ; - they are the same
266 "namespace" ; - we know this as fact
267 (semanticdb-typecache-merge-streams
268 (semanticdb-typecache-safe-tag-members prev)
269 (semanticdb-typecache-safe-tag-members next))
270 nil ; - no attributes
272 ;; Make sure we mark this as a fake tag.
273 (semantic-tag-set-faux (car ans))
275 ((semantic-tag-prototype-p next)
276 ;; NEXT is a prototype... so keep previous.
277 nil ; - keep prev, do nothing
279 ((semantic-tag-prototype-p prev)
280 ;; PREV is a prototype, but not next.. so keep NEXT.
281 ;; setcar - set by side-effect on top of prev
285 ;;(message "Don't know how to merge %s. Keeping first entry." (semantic-tag-name next))
287 ;; Not same class... but same name
288 ;(message "Same name, different type: %s, %s!=%s"
289 ; (semantic-tag-name next)
290 ; (semantic-tag-type next)
291 ; (semantic-tag-type prev))
292 (setq ans (cons next ans))
297 ;;; Refresh / Query API
299 ;; Queries that can be made for the typecache.
300 (defmethod semanticdb-typecache-file-tags ((table semanticdb-abstract-table))
301 "No tags available from non-file based tables."
304 (defmethod semanticdb-typecache-file-tags ((table semanticdb-table))
305 "Update the typecache for TABLE, and return the file-tags.
306 File-tags are those that belong to this file only, and excludes
308 (let* (;(idx (semanticdb-get-table-index table))
309 (cache (semanticdb-get-typecache table))
312 ;; Make sure our file-tags list is up to date.
313 (when (not (oref cache filestream))
314 (let ((tags (semantic-find-tags-by-class 'type table)))
316 (setq tags (semanticdb-typecache-safe-tag-list tags table))
317 (oset cache filestream (semanticdb-typecache-merge-streams tags nil)))))
320 (oref cache filestream)
323 (defmethod semanticdb-typecache-include-tags ((table semanticdb-abstract-table))
324 "No tags available from non-file based tables."
327 (defmethod semanticdb-typecache-include-tags ((table semanticdb-table))
328 "Update the typecache for TABLE, and return the merged types from the include tags.
329 Include-tags are the tags brought in via includes, all merged together into
331 (let* ((cache (semanticdb-get-typecache table))
334 ;; Make sure our file-tags list is up to date.
335 (when (not (oref cache includestream))
336 (let (;; Calc the path first. This will have a nice side -effect of
337 ;; getting the cache refreshed if a refresh is needed. Most of the
338 ;; time this value is itself cached, so the query is fast.
339 (incpath (semanticdb-find-translate-path table nil))
341 ;; Get the translated path, and extract all the type tags, then merge
342 ;; them all together.
344 ;; don't include ourselves in this crazy list.
345 (when (and i (not (eq i table))
346 ;; @todo - This eieio fcn can be slow! Do I need it?
347 ;; (semanticdb-table-child-p i)
350 (semanticdb-typecache-merge-streams
352 ;; Getting the cache from this table will also cause this
353 ;; file to update it's cache from it's decendants.
355 ;; In theory, caches are only built for most includes
356 ;; only once (in the loop before this one), so this ends
357 ;; up being super fast as we edit our file.
359 (semanticdb-typecache-file-tags i))))
363 (oset cache includestream incstream)))
366 (oref cache includestream)
370 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
374 (define-overloadable-function semanticdb-typecache-find (type &optional path find-file-match)
375 "Search the typecache for TYPE in PATH.
376 If type is a string, split the string, and search for the parts.
377 If type is a list, treat the type as a pre-split string.
378 PATH can be nil for the current buffer, or a semanticdb table.
379 FIND-FILE-MATCH is non-nil to force all found tags to be loaded into a buffer.")
381 (defun semanticdb-typecache-find-default (type &optional path find-file-match)
382 "Default implementation of `semanticdb-typecache-find'.
383 TYPE is the datatype to find.
384 PATH is the search path.. which should be one table object.
385 If FIND-FILE-MATCH is non-nil, then force the file belonging to the
386 found tag to be loaded."
387 (semanticdb-typecache-find-method (or path semanticdb-current-table)
388 type find-file-match))
390 (defun semanticdb-typecache-find-by-name-helper (name table)
391 "Find the tag with NAME in TABLE, which is from a typecache.
392 If more than one tag has NAME in TABLE, we will prefer the tag that
394 (let* ((names (semantic-find-tags-by-name name table))
395 (types (semantic-find-tags-by-class 'type names)))
396 (or (car-safe types) (car-safe names))))
398 (defmethod semanticdb-typecache-find-method ((table semanticdb-abstract-table)
399 type find-file-match)
400 "Search the typecache in TABLE for the datatype TYPE.
401 If type is a string, split the string, and search for the parts.
402 If type is a list, treat the type as a pre-split string.
403 If FIND-FILE-MATCH is non-nil, then force the file belonging to the
404 found tag to be loaded."
405 ;; convert string to a list.
406 (when (stringp type) (setq type (semantic-analyze-split-name type)))
407 (when (stringp type) (setq type (list type)))
409 ;; Search for the list in our typecache.
410 (let* ((file (semanticdb-typecache-file-tags table))
411 (inc (semanticdb-typecache-include-tags table))
420 (calculated-scope nil)
422 ;; 1) Find first symbol in the two master lists and then merge
423 ;; the found streams.
425 ;; We stripped duplicates, so these will be super-fast!
426 (setq f-ans (semantic-find-first-tag-by-name (car type) file))
427 (setq i-ans (semantic-find-first-tag-by-name (car type) inc))
428 (if (and f-ans i-ans)
430 ;; This trick merges the two identified tags, making sure our lists are
431 ;; complete. The second find then gets the new 'master' from the list of 2.
432 (setq ans (semanticdb-typecache-merge-streams (list f-ans) (list i-ans)))
433 (setq ans (semantic-find-first-tag-by-name (car type) ans))
436 ;; The answers are already sorted and merged, so if one misses,
437 ;; no need to do any special work.
438 (setq ans (or f-ans i-ans)))
440 ;; 2) Loop over the remaining parts.
441 (while (and type notdone)
443 ;; For pass > 1, stream will be non-nil, so do a search, otherwise
444 ;; ans is from outside the loop.
446 (setq ans (semanticdb-typecache-find-by-name-helper (car type) stream))
448 ;; NOTE: The below test to make sure we get a type is only relevant
449 ;; for the SECOND pass or later. The first pass can only ever
450 ;; find a type/namespace because everything else is excluded.
452 ;; If this is not the last entry from the list, then it
453 ;; must be a type or a namespace. Lets double check.
456 ;; From above, there is only one tag in ans, and we prefer
458 (when (not (semantic-tag-of-class-p ans 'type))
463 (push ans calculated-scope)
465 ;; Track most recent file.
466 (setq thisfile (semantic-tag-file-name ans))
467 (when (and thisfile (stringp thisfile))
468 (setq lastfile thisfile))
470 ;; If we have a miss, exit, otherwise, update the stream to
471 ;; the next set of members.
474 (setq stream (semantic-tag-type-members ans)))
480 (if (or type (not notdone))
481 ;; If there is stuff left over, then we failed. Just return
485 ;; We finished, so return everything.
487 (if (and find-file-match lastfile)
488 ;; This won't liven up the tag since we have a copy, but
489 ;; we ought to be able to get there and go to the right line.
490 (find-file-noselect lastfile)
491 ;; We don't want to find-file match, so instead lets
492 ;; push the filename onto the return tag.
494 (setq lastans (semantic-tag-copy lastans nil lastfile))
495 ;; We used to do the below, but we would erroneously be putting
496 ;; attributes on tags being shred with other lists.
497 ;;(semantic--tag-put-property lastans :filename lastfile)
501 (if (and lastans calculated-scope)
503 ;; Put our discovered scope into the tag if we have a tag
505 (require 'semantic/scope)
506 (semantic-scope-tag-clone-with-scope
507 lastans (reverse (cdr calculated-scope))))
513 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
515 ;;; BRUTISH Typecache
517 ;; Routines for a typecache that crosses all tables in a given database
518 ;; for a matching major-mode.
519 (defmethod semanticdb-typecache-for-database ((db semanticdb-project-database)
521 "Return the typecache for the project database DB.
522 If there isn't one, create it.
524 (let ((lmode (or mode major-mode))
525 (cache (semanticdb-get-typecache db))
528 (dolist (table (semanticdb-get-database-tables db))
529 (when (eq lmode (oref table :major-mode))
531 (semanticdb-typecache-merge-streams
534 (semanticdb-typecache-file-tags table))))
536 (oset cache stream stream)
539 (defun semanticdb-typecache-refresh-for-buffer (buffer)
540 "Refresh the typecache for BUFFER."
543 (let* ((tab semanticdb-current-table)
544 ;(idx (semanticdb-get-table-index tab))
545 (tc (semanticdb-get-typecache tab)))
546 (semanticdb-typecache-file-tags tab)
547 (semanticdb-typecache-include-tags tab)
553 (defun semanticdb-typecache-complete-flush ()
554 "Flush all typecaches referenced by the current buffer."
556 (let* ((path (semanticdb-find-translate-path nil nil)))
558 (oset P pointmax nil)
559 (semantic-reset (semanticdb-get-typecache P)))))
561 (defun semanticdb-typecache-dump ()
562 "Dump the typecache for the current buffer."
564 (require 'data-debug)
565 (let* ((start (current-time))
566 (tc (semanticdb-typecache-refresh-for-buffer (current-buffer)))
569 (data-debug-new-buffer "*TypeCache ADEBUG*")
570 (message "Calculating Cache took %.2f seconds."
571 (semantic-elapsed-time start end))
573 (data-debug-insert-thing tc "]" "")
577 (defun semanticdb-db-typecache-dump ()
578 "Dump the typecache for the current buffer's database."
580 (require 'data-debug)
581 (let* ((tab semanticdb-current-table)
582 (idx (semanticdb-get-table-index tab))
583 (junk (oset idx type-cache nil)) ;; flush!
584 (start (current-time))
585 (tc (semanticdb-typecache-for-database (oref tab parent-db)))
588 (data-debug-new-buffer "*TypeCache ADEBUG*")
589 (message "Calculating Cache took %.2f seconds."
590 (semantic-elapsed-time start end))
592 (data-debug-insert-thing tc "]" "")
596 (provide 'semantic/db-typecache)
599 ;; generated-autoload-file: "loaddefs.el"
600 ;; generated-autoload-feature: semantic/loaddefs
601 ;; generated-autoload-load-name: "semantic/db-typecache"
604 ;;; semanticdb-typecache.el ends here