tag.el 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  1. ;;; semantic/tag.el --- tag creation and access
  2. ;; Copyright (C) 1999-2005, 2007-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric M. Ludlam <zappo@gnu.org>
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;; I. The core production of semantic is the list of tags produced by the
  18. ;; different parsers. This file provides 3 APIs related to tag access:
  19. ;;
  20. ;; 1) Primitive Tag Access
  21. ;; There is a set of common features to all tags. These access
  22. ;; functions can get these values.
  23. ;; 2) Standard Tag Access
  24. ;; A Standard Tag should be produced by most traditional languages
  25. ;; with standard styles common to typed object oriented languages.
  26. ;; These functions can access these data elements from a tag.
  27. ;; 3) Generic Tag Access
  28. ;; Access to tag structure in a more direct way.
  29. ;; ** May not be forward compatible.
  30. ;;
  31. ;; II. There is also an API for tag creation. Use `semantic-tag' to create
  32. ;; a new tag.
  33. ;;
  34. ;; III. Tag Comparison. Allows explicit or comparative tests to see
  35. ;; if two tags are the same.
  36. ;;; Code:
  37. ;;
  38. ;; Keep this only so long as we have obsolete fcns.
  39. (require 'semantic/fw)
  40. (require 'semantic/lex)
  41. (declare-function semantic-analyze-split-name "semantic/analyze/fcn")
  42. (declare-function semantic-fetch-tags "semantic")
  43. (declare-function semantic-clear-toplevel-cache "semantic")
  44. (defconst semantic-tag-version "2.0"
  45. "Version string of semantic tags made with this code.")
  46. (defconst semantic-tag-incompatible-version "1.0"
  47. "Version string of semantic tags which are not currently compatible.
  48. These old style tags may be loaded from a file with semantic db.
  49. In this case, we must flush the old tags and start over.")
  50. ;;; Primitive Tag access system:
  51. ;;
  52. ;; Raw tags in semantic are lists of 5 elements:
  53. ;;
  54. ;; (NAME CLASS ATTRIBUTES PROPERTIES OVERLAY)
  55. ;;
  56. ;; Where:
  57. ;;
  58. ;; - NAME is a string that represents the tag name.
  59. ;;
  60. ;; - CLASS is a symbol that represent the class of the tag (for
  61. ;; example, usual classes are `type', `function', `variable',
  62. ;; `include', `package', `code').
  63. ;;
  64. ;; - ATTRIBUTES is a public list of attributes that describes
  65. ;; language data represented by the tag (for example, a variable
  66. ;; can have a `:constant-flag' attribute, a function an `:arguments'
  67. ;; attribute, etc.).
  68. ;;
  69. ;; - PROPERTIES is a private list of properties used internally.
  70. ;;
  71. ;; - OVERLAY represent the location of data described by the tag.
  72. ;;
  73. (defsubst semantic-tag-name (tag)
  74. "Return the name of TAG.
  75. For functions, variables, classes, typedefs, etc., this is the identifier
  76. that is being defined. For tags without an obvious associated name, this
  77. may be the statement type, e.g., this may return @code{print} for python's
  78. print statement."
  79. (car tag))
  80. (defsubst semantic-tag-class (tag)
  81. "Return the class of TAG.
  82. That is, the symbol 'variable, 'function, 'type, or other.
  83. There is no limit to the symbols that may represent the class of a tag.
  84. Each parser generates tags with classes defined by it.
  85. For functional languages, typical tag classes are:
  86. @table @code
  87. @item type
  88. Data types, named map for a memory block.
  89. @item function
  90. A function or method, or named execution location.
  91. @item variable
  92. A variable, or named storage for data.
  93. @item include
  94. Statement that represents a file from which more tags can be found.
  95. @item package
  96. Statement that declares this file's package name.
  97. @item code
  98. Code that has not name or binding to any other symbol, such as in a script.
  99. @end table
  100. "
  101. (nth 1 tag))
  102. (defsubst semantic-tag-attributes (tag)
  103. "Return the list of public attributes of TAG.
  104. That is a property list: (ATTRIBUTE-1 VALUE-1 ATTRIBUTE-2 VALUE-2...)."
  105. (nth 2 tag))
  106. (defsubst semantic-tag-properties (tag)
  107. "Return the list of private properties of TAG.
  108. That is a property list: (PROPERTY-1 VALUE-1 PROPERTY-2 VALUE-2...)."
  109. (nth 3 tag))
  110. (defsubst semantic-tag-overlay (tag)
  111. "Return the OVERLAY part of TAG.
  112. That is, an overlay or an unloaded buffer representation.
  113. This function can also return an array of the form [ START END ].
  114. This occurs for tags that are not currently linked into a buffer."
  115. (nth 4 tag))
  116. (defsubst semantic--tag-overlay-cdr (tag)
  117. "Return the cons cell whose car is the OVERLAY part of TAG.
  118. That function is for internal use only."
  119. (nthcdr 4 tag))
  120. (defsubst semantic--tag-set-overlay (tag overlay)
  121. "Set the overlay part of TAG with OVERLAY.
  122. That function is for internal use only."
  123. (setcar (semantic--tag-overlay-cdr tag) overlay))
  124. (defsubst semantic-tag-start (tag)
  125. "Return the start location of TAG."
  126. (let ((o (semantic-tag-overlay tag)))
  127. (if (semantic-overlay-p o)
  128. (semantic-overlay-start o)
  129. (aref o 0))))
  130. (defsubst semantic-tag-end (tag)
  131. "Return the end location of TAG."
  132. (let ((o (semantic-tag-overlay tag)))
  133. (if (semantic-overlay-p o)
  134. (semantic-overlay-end o)
  135. (aref o 1))))
  136. (defsubst semantic-tag-bounds (tag)
  137. "Return the location (START END) of data TAG describes."
  138. (list (semantic-tag-start tag)
  139. (semantic-tag-end tag)))
  140. (defun semantic-tag-set-bounds (tag start end)
  141. "In TAG, set the START and END location of data it describes."
  142. (let ((o (semantic-tag-overlay tag)))
  143. (if (semantic-overlay-p o)
  144. (semantic-overlay-move o start end)
  145. (semantic--tag-set-overlay tag (vector start end)))))
  146. (defun semantic-tag-in-buffer-p (tag)
  147. "Return the buffer TAG resides in IFF tag is already in a buffer.
  148. If a tag is not in a buffer, return nil."
  149. (let ((o (semantic-tag-overlay tag)))
  150. ;; TAG is currently linked to a buffer, return it.
  151. (when (and (semantic-overlay-p o)
  152. (semantic-overlay-live-p o))
  153. (semantic-overlay-buffer o))))
  154. (defsubst semantic--tag-get-property (tag property)
  155. "From TAG, extract the value of PROPERTY.
  156. Return the value found, or nil if PROPERTY is not one of the
  157. properties of TAG.
  158. That function is for internal use only."
  159. (plist-get (semantic-tag-properties tag) property))
  160. (defun semantic-tag-buffer (tag)
  161. "Return the buffer TAG resides in.
  162. If TAG has an originating file, read that file into a (maybe new)
  163. buffer, and return it.
  164. Return nil if there is no buffer for this tag."
  165. (let ((buff (semantic-tag-in-buffer-p tag)))
  166. (if buff
  167. buff
  168. ;; TAG has an originating file, read that file into a buffer, and
  169. ;; return it.
  170. (if (semantic--tag-get-property tag :filename)
  171. (save-match-data
  172. (find-file-noselect (semantic--tag-get-property tag :filename)))
  173. ;; TAG is not in Emacs right now, no buffer is available.
  174. ))))
  175. (defun semantic-tag-mode (&optional tag)
  176. "Return the major mode active for TAG.
  177. TAG defaults to the tag at point in current buffer.
  178. If TAG has a :mode property return it.
  179. If point is inside TAG bounds, return the major mode active at point.
  180. Return the major mode active at beginning of TAG otherwise.
  181. See also the function `semantic-ctxt-current-mode'."
  182. (or tag (setq tag (semantic-current-tag)))
  183. (or (semantic--tag-get-property tag :mode)
  184. (let ((buffer (semantic-tag-buffer tag))
  185. (start (semantic-tag-start tag))
  186. (end (semantic-tag-end tag)))
  187. (save-excursion
  188. (and buffer (set-buffer buffer))
  189. ;; Unless point is inside TAG bounds, move it to the
  190. ;; beginning of TAG.
  191. (or (and (>= (point) start) (< (point) end))
  192. (goto-char start))
  193. (require 'semantic/ctxt)
  194. (semantic-ctxt-current-mode)))))
  195. (defsubst semantic--tag-attributes-cdr (tag)
  196. "Return the cons cell whose car is the ATTRIBUTES part of TAG.
  197. That function is for internal use only."
  198. (nthcdr 2 tag))
  199. (defsubst semantic-tag-put-attribute (tag attribute value)
  200. "Change value in TAG of ATTRIBUTE to VALUE.
  201. If ATTRIBUTE already exists, its value is set to VALUE, otherwise the
  202. new ATTRIBUTE VALUE pair is added.
  203. Return TAG.
  204. Use this function in a parser when not all attributes are known at the
  205. same time."
  206. (let* ((plist-cdr (semantic--tag-attributes-cdr tag)))
  207. (when (consp plist-cdr)
  208. (setcar plist-cdr
  209. (semantic-tag-make-plist
  210. (plist-put (car plist-cdr) attribute value))))
  211. tag))
  212. (defun semantic-tag-put-attribute-no-side-effect (tag attribute value)
  213. "Change value in TAG of ATTRIBUTE to VALUE without side effects.
  214. All cons cells in the attribute list are replicated so that there
  215. are no side effects if TAG is in shared lists.
  216. If ATTRIBUTE already exists, its value is set to VALUE, otherwise the
  217. new ATTRIBUTE VALUE pair is added.
  218. Return TAG."
  219. (let* ((plist-cdr (semantic--tag-attributes-cdr tag)))
  220. (when (consp plist-cdr)
  221. (setcar plist-cdr
  222. (semantic-tag-make-plist
  223. (plist-put (copy-sequence (car plist-cdr))
  224. attribute value))))
  225. tag))
  226. (defsubst semantic-tag-get-attribute (tag attribute)
  227. "From TAG, return the value of ATTRIBUTE.
  228. ATTRIBUTE is a symbol whose specification value to get.
  229. Return the value found, or nil if ATTRIBUTE is not one of the
  230. attributes of TAG."
  231. (plist-get (semantic-tag-attributes tag) attribute))
  232. ;; These functions are for internal use only!
  233. (defsubst semantic--tag-properties-cdr (tag)
  234. "Return the cons cell whose car is the PROPERTIES part of TAG.
  235. That function is for internal use only."
  236. (nthcdr 3 tag))
  237. (defun semantic--tag-put-property (tag property value)
  238. "Change value in TAG of PROPERTY to VALUE.
  239. If PROPERTY already exists, its value is set to VALUE, otherwise the
  240. new PROPERTY VALUE pair is added.
  241. Return TAG.
  242. That function is for internal use only."
  243. (let* ((plist-cdr (semantic--tag-properties-cdr tag)))
  244. (when (consp plist-cdr)
  245. (setcar plist-cdr
  246. (semantic-tag-make-plist
  247. (plist-put (car plist-cdr) property value))))
  248. tag))
  249. (defun semantic--tag-put-property-no-side-effect (tag property value)
  250. "Change value in TAG of PROPERTY to VALUE without side effects.
  251. All cons cells in the property list are replicated so that there
  252. are no side effects if TAG is in shared lists.
  253. If PROPERTY already exists, its value is set to VALUE, otherwise the
  254. new PROPERTY VALUE pair is added.
  255. Return TAG.
  256. That function is for internal use only."
  257. (let* ((plist-cdr (semantic--tag-properties-cdr tag)))
  258. (when (consp plist-cdr)
  259. (setcar plist-cdr
  260. (semantic-tag-make-plist
  261. (plist-put (copy-sequence (car plist-cdr))
  262. property value))))
  263. tag))
  264. (defun semantic-tag-file-name (tag)
  265. "Return the name of the file from which TAG originated.
  266. Return nil if that information can't be obtained.
  267. If TAG is from a loaded buffer, then that buffer's filename is used.
  268. If TAG is unlinked, but has a :filename property, then that is used."
  269. (let ((buffer (semantic-tag-in-buffer-p tag)))
  270. (if buffer
  271. (buffer-file-name buffer)
  272. (semantic--tag-get-property tag :filename))))
  273. ;;; Tag tests and comparisons.
  274. (defsubst semantic-tag-p (tag)
  275. "Return non-nil if TAG is most likely a semantic tag."
  276. (condition-case nil
  277. (and (consp tag)
  278. (stringp (car tag)) ; NAME
  279. (symbolp (nth 1 tag)) (nth 1 tag) ; TAG-CLASS
  280. (listp (nth 2 tag)) ; ATTRIBUTES
  281. (listp (nth 3 tag)) ; PROPERTIES
  282. )
  283. ;; If an error occurs, then it most certainly is not a tag.
  284. (error nil)))
  285. (defsubst semantic-tag-of-class-p (tag class)
  286. "Return non-nil if class of TAG is CLASS."
  287. (eq (semantic-tag-class tag) class))
  288. (defsubst semantic-tag-type-members (tag)
  289. "Return the members of the type that TAG describes.
  290. That is the value of the `:members' attribute."
  291. (semantic-tag-get-attribute tag :members))
  292. (defsubst semantic-tag-type (tag)
  293. "Return the value of the `:type' attribute of TAG.
  294. For a function it would be the data type of the return value.
  295. For a variable, it is the storage type of that variable.
  296. For a data type, the type is the style of datatype, such as
  297. struct or union."
  298. (semantic-tag-get-attribute tag :type))
  299. (defun semantic-tag-with-position-p (tag)
  300. "Return non-nil if TAG has positional information."
  301. (and (semantic-tag-p tag)
  302. (let ((o (semantic-tag-overlay tag)))
  303. (or (and (semantic-overlay-p o)
  304. (semantic-overlay-live-p o))
  305. (arrayp o)))))
  306. (defun semantic-equivalent-tag-p (tag1 tag2)
  307. "Compare TAG1 and TAG2 and return non-nil if they are equivalent.
  308. Use `equal' on elements the name, class, and position.
  309. Use this function if tags are being copied and regrouped to test
  310. for if two tags represent the same thing, but may be constructed
  311. of different cons cells."
  312. (and (equal (semantic-tag-name tag1) (semantic-tag-name tag2))
  313. (semantic-tag-of-class-p tag1 (semantic-tag-class tag2))
  314. (or (and (not (semantic-tag-overlay tag1))
  315. (not (semantic-tag-overlay tag2)))
  316. (and (semantic-tag-overlay tag1)
  317. (semantic-tag-overlay tag2)
  318. (equal (semantic-tag-bounds tag1)
  319. (semantic-tag-bounds tag2))))))
  320. (defun semantic-tag-similar-p (tag1 tag2 &rest ignorable-attributes)
  321. "Test to see if TAG1 and TAG2 are similar.
  322. Two tags are similar if their name, datatype, and various attributes
  323. are the same.
  324. Similar tags that have sub-tags such as arg lists or type members,
  325. are similar w/out checking the sub-list of tags.
  326. Optional argument IGNORABLE-ATTRIBUTES are attributes to ignore while comparing similarity."
  327. (let* ((A1 (and (equal (semantic-tag-name tag1) (semantic-tag-name tag2))
  328. (semantic-tag-of-class-p tag1 (semantic-tag-class tag2))
  329. (semantic-tag-of-type-p tag1 (semantic-tag-type tag2))))
  330. (attr1 (semantic-tag-attributes tag1))
  331. (A2 (= (length attr1) (length (semantic-tag-attributes tag2))))
  332. (A3 t)
  333. )
  334. (when (and (not A2) ignorable-attributes)
  335. (setq A2 t))
  336. (while (and A2 attr1 A3)
  337. (let ((a (car attr1))
  338. (v (car (cdr attr1))))
  339. (cond ((or (eq a :type) ;; already tested above.
  340. (memq a ignorable-attributes)) ;; Ignore them...
  341. nil)
  342. ;; Don't test sublists of tags
  343. ((and (listp v) (semantic-tag-p (car v)))
  344. nil)
  345. ;; The attributes are not the same?
  346. ((not (equal v (semantic-tag-get-attribute tag2 a)))
  347. (setq A3 nil))
  348. (t
  349. nil))
  350. )
  351. (setq attr1 (cdr (cdr attr1))))
  352. (and A1 A2 A3)
  353. ))
  354. (defun semantic-tag-similar-with-subtags-p (tag1 tag2 &rest ignorable-attributes)
  355. "Test to see if TAG1 and TAG2 are similar.
  356. Uses `semantic-tag-similar-p' but also recurses through sub-tags, such
  357. as argument lists and type members.
  358. Optional argument IGNORABLE-ATTRIBUTES is passed down to
  359. `semantic-tag-similar-p'."
  360. (let ((C1 (semantic-tag-components tag1))
  361. (C2 (semantic-tag-components tag2))
  362. )
  363. (if (or (/= (length C1) (length C2))
  364. (not (semantic-tag-similar-p tag1 tag2 ignorable-attributes))
  365. )
  366. ;; Basic test fails.
  367. nil
  368. ;; Else, check component lists.
  369. (catch 'component-dissimilar
  370. (while C1
  371. (if (not (semantic-tag-similar-with-subtags-p
  372. (car C1) (car C2) ignorable-attributes))
  373. (throw 'component-dissimilar nil))
  374. (setq C1 (cdr C1))
  375. (setq C2 (cdr C2))
  376. )
  377. ;; If we made it this far, we are ok.
  378. t) )))
  379. (defun semantic-tag-of-type-p (tag type)
  380. "Compare TAG's type against TYPE. Non nil if equivalent.
  381. TYPE can be a string, or a tag of class 'type.
  382. This can be complex since some tags might have a :type that is a tag,
  383. while other tags might just have a string. This function will also be
  384. return true of TAG's type is compared directly to the declaration of a
  385. data type."
  386. (let* ((tagtype (semantic-tag-type tag))
  387. (tagtypestring (cond ((stringp tagtype)
  388. tagtype)
  389. ((and (semantic-tag-p tagtype)
  390. (semantic-tag-of-class-p tagtype 'type))
  391. (semantic-tag-name tagtype))
  392. (t "")))
  393. (typestring (cond ((stringp type)
  394. type)
  395. ((and (semantic-tag-p type)
  396. (semantic-tag-of-class-p type 'type))
  397. (semantic-tag-name type))
  398. (t "")))
  399. )
  400. (and
  401. tagtypestring
  402. (or
  403. ;; Matching strings (input type is string)
  404. (and (stringp type)
  405. (string= tagtypestring type))
  406. ;; Matching strings (tag type is string)
  407. (and (stringp tagtype)
  408. (string= tagtype typestring))
  409. ;; Matching tokens, and the type of the type is the same.
  410. (and (string= tagtypestring typestring)
  411. (if (and (semantic-tag-type tagtype) (semantic-tag-type type))
  412. (equal (semantic-tag-type tagtype) (semantic-tag-type type))
  413. t))
  414. ))
  415. ))
  416. (defun semantic-tag-type-compound-p (tag)
  417. "Return non-nil the type of TAG is compound.
  418. Compound implies a structure or similar data type.
  419. Returns the list of tag members if it is compound."
  420. (let* ((tagtype (semantic-tag-type tag))
  421. )
  422. (when (and (semantic-tag-p tagtype)
  423. (semantic-tag-of-class-p tagtype 'type))
  424. ;; We have the potential of this being a nifty compound type.
  425. (semantic-tag-type-members tagtype)
  426. )))
  427. (defun semantic-tag-faux-p (tag)
  428. "Return non-nil if TAG is a FAUX tag.
  429. FAUX tags are created to represent a construct that is
  430. not known to exist in the code.
  431. Example: When the class browser sees methods to a class, but
  432. cannot find the class, it will create a faux tag to represent the
  433. class to store those methods."
  434. (semantic--tag-get-property tag :faux-flag))
  435. ;;; Tag creation
  436. ;;
  437. ;; Is this function still necessary?
  438. (defun semantic-tag-make-plist (args)
  439. "Create a property list with ARGS.
  440. Args is a property list of the form (KEY1 VALUE1 ... KEYN VALUEN).
  441. Where KEY is a symbol, and VALUE is the value for that symbol.
  442. The return value will be a new property list, with these KEY/VALUE
  443. pairs eliminated:
  444. - KEY associated to nil VALUE.
  445. - KEY associated to an empty string VALUE.
  446. - KEY associated to a zero VALUE."
  447. (let (plist key val)
  448. (while args
  449. (setq key (car args)
  450. val (nth 1 args)
  451. args (nthcdr 2 args))
  452. (or (member val '("" nil))
  453. (and (numberp val) (zerop val))
  454. (setq plist (cons key (cons val plist)))))
  455. ;; It is not useful to reverse the new plist.
  456. plist))
  457. (defsubst semantic-tag (name class &rest attributes)
  458. "Create a generic semantic tag.
  459. NAME is a string representing the name of this tag.
  460. CLASS is the symbol that represents the class of tag this is,
  461. such as 'variable, or 'function.
  462. ATTRIBUTES is a list of additional attributes belonging to this tag."
  463. (list name class (semantic-tag-make-plist attributes) nil nil))
  464. (defsubst semantic-tag-new-variable (name type &optional default-value &rest attributes)
  465. "Create a semantic tag of class 'variable.
  466. NAME is the name of this variable.
  467. TYPE is a string or semantic tag representing the type of this variable.
  468. Optional DEFAULT-VALUE is a string representing the default value of this
  469. variable. ATTRIBUTES is a list of additional attributes belonging to this
  470. tag."
  471. (apply 'semantic-tag name 'variable
  472. :type type
  473. :default-value default-value
  474. attributes))
  475. (defsubst semantic-tag-new-function (name type arg-list &rest attributes)
  476. "Create a semantic tag of class 'function.
  477. NAME is the name of this function.
  478. TYPE is a string or semantic tag representing the type of this function.
  479. ARG-LIST is a list of strings or semantic tags representing the
  480. arguments of this function.
  481. ATTRIBUTES is a list of additional attributes belonging to this tag."
  482. (apply 'semantic-tag name 'function
  483. :type type
  484. :arguments arg-list
  485. attributes))
  486. (defsubst semantic-tag-new-type (name type members parents &rest attributes)
  487. "Create a semantic tag of class 'type.
  488. NAME is the name of this type.
  489. TYPE is a string or semantic tag representing the type of this type.
  490. MEMBERS is a list of strings or semantic tags representing the
  491. elements that make up this type if it is a composite type.
  492. PARENTS is a cons cell. (EXPLICIT-PARENTS . INTERFACE-PARENTS)
  493. EXPLICIT-PARENTS can be a single string (Just one parent) or a
  494. list of parents (in a multiple inheritance situation). It can also
  495. be nil.
  496. INTERFACE-PARENTS is a list of strings representing the names of
  497. all INTERFACES, or abstract classes inherited from. It can also be
  498. nil.
  499. This slot can be interesting because the form:
  500. ( nil \"string\")
  501. is a valid parent where there is no explicit parent, and only an
  502. interface.
  503. ATTRIBUTES is a list of additional attributes belonging to this tag."
  504. (apply 'semantic-tag name 'type
  505. :type type
  506. :members members
  507. :superclasses (car parents)
  508. :interfaces (cdr parents)
  509. attributes))
  510. (defsubst semantic-tag-new-include (name system-flag &rest attributes)
  511. "Create a semantic tag of class 'include.
  512. NAME is the name of this include.
  513. SYSTEM-FLAG represents that we were able to identify this include as belonging
  514. to the system, as opposed to belonging to the local project.
  515. ATTRIBUTES is a list of additional attributes belonging to this tag."
  516. (apply 'semantic-tag name 'include
  517. :system-flag system-flag
  518. attributes))
  519. (defsubst semantic-tag-new-package (name detail &rest attributes)
  520. "Create a semantic tag of class 'package.
  521. NAME is the name of this package.
  522. DETAIL is extra information about this package, such as a location where
  523. it can be found.
  524. ATTRIBUTES is a list of additional attributes belonging to this tag."
  525. (apply 'semantic-tag name 'package
  526. :detail detail
  527. attributes))
  528. (defsubst semantic-tag-new-code (name detail &rest attributes)
  529. "Create a semantic tag of class 'code.
  530. NAME is a name for this code.
  531. DETAIL is extra information about the code.
  532. ATTRIBUTES is a list of additional attributes belonging to this tag."
  533. (apply 'semantic-tag name 'code
  534. :detail detail
  535. attributes))
  536. (defsubst semantic-tag-set-faux (tag)
  537. "Set TAG to be a new FAUX tag.
  538. FAUX tags represent constructs not found in the source code.
  539. You can identify a faux tag with `semantic-tag-faux-p'"
  540. (semantic--tag-put-property tag :faux-flag t))
  541. (defsubst semantic-tag-set-name (tag name)
  542. "Set TAG name to NAME."
  543. (setcar tag name))
  544. ;;; Copying and cloning tags.
  545. ;;
  546. (defsubst semantic-tag-clone (tag &optional name)
  547. "Clone TAG, creating a new TAG.
  548. If optional argument NAME is not nil it specifies a new name for the
  549. cloned tag."
  550. ;; Right now, TAG is a list.
  551. (list (or name (semantic-tag-name tag))
  552. (semantic-tag-class tag)
  553. (copy-sequence (semantic-tag-attributes tag))
  554. (copy-sequence (semantic-tag-properties tag))
  555. (semantic-tag-overlay tag)))
  556. (defun semantic-tag-copy (tag &optional name keep-file)
  557. "Return a copy of TAG unlinked from the originating buffer.
  558. If optional argument NAME is non-nil it specifies a new name for the
  559. copied tag.
  560. If optional argument KEEP-FILE is non-nil, and TAG was linked to a
  561. buffer, the originating buffer file name is kept in the `:filename'
  562. property of the copied tag.
  563. If KEEP-FILE is a string, and the originating buffer is NOT available,
  564. then KEEP-FILE is stored on the `:filename' property.
  565. This runs the tag hook `unlink-copy-hook`."
  566. ;; Right now, TAG is a list.
  567. (let ((copy (semantic-tag-clone tag name)))
  568. ;; Keep the filename if needed.
  569. (when keep-file
  570. (semantic--tag-put-property
  571. copy :filename (or (semantic-tag-file-name copy)
  572. (and (stringp keep-file)
  573. keep-file)
  574. )))
  575. (when (semantic-tag-with-position-p tag)
  576. ;; Convert the overlay to a vector, effectively 'unlinking' the tag.
  577. (semantic--tag-set-overlay
  578. copy (vector (semantic-tag-start copy) (semantic-tag-end copy)))
  579. ;; Force the children to be copied also.
  580. ;;(let ((chil (semantic--tag-copy-list
  581. ;; (semantic-tag-components-with-overlays tag)
  582. ;; keep-file)))
  583. ;;;; Put the list into TAG.
  584. ;;)
  585. ;; Call the unlink-copy hook. This should tell tools that
  586. ;; this tag is not part of any buffer.
  587. (when (semantic-overlay-p (semantic-tag-overlay tag))
  588. (semantic--tag-run-hooks copy 'unlink-copy-hook))
  589. )
  590. copy))
  591. ;;(defun semantic--tag-copy-list (tags &optional keep-file)
  592. ;; "Make copies of TAGS and return the list of TAGS."
  593. ;; (let ((out nil))
  594. ;; (dolist (tag tags out)
  595. ;; (setq out (cons (semantic-tag-copy tag nil keep-file)
  596. ;; out))
  597. ;; )))
  598. (defun semantic--tag-copy-properties (tag1 tag2)
  599. "Copy private properties from TAG1 to TAG2.
  600. Return TAG2.
  601. This function is for internal use only."
  602. (let ((plist (semantic-tag-properties tag1)))
  603. (while plist
  604. (semantic--tag-put-property tag2 (car plist) (nth 1 plist))
  605. (setq plist (nthcdr 2 plist)))
  606. tag2))
  607. ;;; DEEP COPIES
  608. ;;
  609. (defun semantic-tag-deep-copy-one-tag (tag &optional filter)
  610. "Make a deep copy of TAG, applying FILTER to each child-tag.
  611. No properties are copied except for :filename.
  612. Overlay will be a vector.
  613. FILTER takes TAG as an argument, and should return a `semantic-tag'.
  614. It is safe for FILTER to modify the input tag and return it."
  615. (when (not filter) (setq filter 'identity))
  616. (when (not (semantic-tag-p tag))
  617. (signal 'wrong-type-argument (list tag 'semantic-tag-p)))
  618. (let ((ol (semantic-tag-overlay tag))
  619. (fn (semantic-tag-file-name tag)))
  620. (funcall filter (list (semantic-tag-name tag)
  621. (semantic-tag-class tag)
  622. (semantic--tag-deep-copy-attributes
  623. (semantic-tag-attributes tag) filter)
  624. ;; Only copy the filename property
  625. (when fn (list :filename fn))
  626. ;; Only setup a vector if we had an overlay.
  627. (when ol (vector (semantic-tag-start tag)
  628. (semantic-tag-end tag)))))))
  629. (defun semantic--tag-deep-copy-attributes (attrs &optional filter)
  630. "Make a deep copy of ATTRS, applying FILTER to each child-tag.
  631. It is safe to modify ATTR, and return a permutation of that list.
  632. FILTER takes TAG as an argument, and should returns a semantic-tag.
  633. It is safe for FILTER to modify the input tag and return it."
  634. (when (car attrs)
  635. (when (not (symbolp (car attrs))) (error "Bad Attribute List in tag"))
  636. (cons (car attrs)
  637. (cons (semantic--tag-deep-copy-value (nth 1 attrs) filter)
  638. (semantic--tag-deep-copy-attributes (nthcdr 2 attrs) filter)))))
  639. (defun semantic--tag-deep-copy-value (value &optional filter)
  640. "Make a deep copy of VALUE, applying FILTER to each child-tag.
  641. It is safe to modify VALUE, and return a permutation of that list.
  642. FILTER takes TAG as an argument, and should returns a semantic-tag.
  643. It is safe for FILTER to modify the input tag and return it."
  644. (cond
  645. ;; Another tag.
  646. ((semantic-tag-p value)
  647. (semantic-tag-deep-copy-one-tag value filter))
  648. ;; A list of more tags
  649. ((and (listp value) (semantic-tag-p (car value)))
  650. (semantic--tag-deep-copy-tag-list value filter))
  651. ;; Some arbitrary data.
  652. (t value)))
  653. (defun semantic--tag-deep-copy-tag-list (tags &optional filter)
  654. "Make a deep copy of TAGS, applying FILTER to each child-tag.
  655. It is safe to modify the TAGS list, and return a permutation of that list.
  656. FILTER takes TAG as an argument, and should returns a semantic-tag.
  657. It is safe for FILTER to modify the input tag and return it."
  658. (when (car tags)
  659. (if (semantic-tag-p (car tags))
  660. (cons (semantic-tag-deep-copy-one-tag (car tags) filter)
  661. (semantic--tag-deep-copy-tag-list (cdr tags) filter))
  662. (cons (car tags) (semantic--tag-deep-copy-tag-list (cdr tags) filter)))))
  663. ;;; Standard Tag Access
  664. ;;
  665. ;;; Common
  666. ;;
  667. (defsubst semantic-tag-modifiers (tag)
  668. "Return the value of the `:typemodifiers' attribute of TAG."
  669. (semantic-tag-get-attribute tag :typemodifiers))
  670. (defun semantic-tag-docstring (tag &optional buffer)
  671. "Return the documentation of TAG.
  672. That is the value defined by the `:documentation' attribute.
  673. Optional argument BUFFER indicates where to get the text from.
  674. If not provided, then only the POSITION can be provided.
  675. If you want to get documentation for languages that do not store
  676. the documentation string in the tag itself, use
  677. `semantic-documentation-for-tag' instead."
  678. (let ((p (semantic-tag-get-attribute tag :documentation)))
  679. (cond
  680. ((stringp p) p) ;; it is the doc string.
  681. ((semantic-lex-token-with-text-p p)
  682. (semantic-lex-token-text p))
  683. ((and (semantic-lex-token-without-text-p p)
  684. buffer)
  685. (with-current-buffer buffer
  686. (semantic-lex-token-text (car (semantic-lex p (1+ p))))))
  687. (t nil))))
  688. ;;; Generic attributes for tags of any class.
  689. ;;
  690. (defsubst semantic-tag-named-parent (tag)
  691. "Return the parent of TAG.
  692. That is the value of the `:parent' attribute.
  693. If a definition can occur outside an actual parent structure, but
  694. refers to that parent by name, then the :parent attribute should be used."
  695. (semantic-tag-get-attribute tag :parent))
  696. ;;; Tags of class `type'
  697. (defun semantic-tag-type-superclasses (tag)
  698. "Return the list of superclass names of the type that TAG describes."
  699. (let ((supers (semantic-tag-get-attribute tag :superclasses)))
  700. (cond ((stringp supers)
  701. ;; If we have a string, make it a list.
  702. (list supers))
  703. ((semantic-tag-p supers)
  704. ;; If we have one tag, return just the name.
  705. (list (semantic-tag-name supers)))
  706. ((and (consp supers) (semantic-tag-p (car supers)))
  707. ;; If we have a tag list, then return the names.
  708. (mapcar (lambda (s) (semantic-tag-name s))
  709. supers))
  710. ((consp supers)
  711. ;; A list of something, return it.
  712. supers))))
  713. (defun semantic--tag-find-parent-by-name (name supers)
  714. "Find the superclass NAME in the list of SUPERS.
  715. If a simple search doesn't do it, try splitting up the names
  716. in SUPERS."
  717. (let ((stag nil))
  718. (setq stag (semantic-find-first-tag-by-name name supers))
  719. (when (not stag)
  720. (require 'semantic/analyze/fcn)
  721. (dolist (S supers)
  722. (let* ((sname (semantic-tag-name S))
  723. (splitparts (semantic-analyze-split-name sname))
  724. (parts (if (stringp splitparts)
  725. (list splitparts)
  726. (nreverse splitparts))))
  727. (when (string= name (car parts))
  728. (setq stag S))
  729. )))
  730. stag))
  731. (defun semantic-tag-type-superclass-protection (tag parentstring)
  732. "Return the inheritance protection in TAG from PARENTSTRING.
  733. PARENTSTRING is the name of the parent being inherited.
  734. The return protection is a symbol, 'public, 'protection, and 'private."
  735. (let ((supers (semantic-tag-get-attribute tag :superclasses)))
  736. (cond ((stringp supers)
  737. 'public)
  738. ((semantic-tag-p supers)
  739. (let ((prot (semantic-tag-get-attribute supers :protection)))
  740. (or (cdr (assoc prot '(("public" . public)
  741. ("protected" . protected)
  742. ("private" . private))))
  743. 'public)))
  744. ((and (consp supers) (stringp (car supers)))
  745. 'public)
  746. ((and (consp supers) (semantic-tag-p (car supers)))
  747. (let* ((stag (semantic--tag-find-parent-by-name parentstring supers))
  748. (prot (when stag
  749. (semantic-tag-get-attribute stag :protection))))
  750. (or (cdr (assoc prot '(("public" . public)
  751. ("protected" . protected)
  752. ("private" . private))))
  753. (when (equal prot "unspecified")
  754. (if (semantic-tag-of-type-p tag "class")
  755. 'private
  756. 'public))
  757. 'public))))
  758. ))
  759. (defsubst semantic-tag-type-interfaces (tag)
  760. "Return the list of interfaces of the type that TAG describes."
  761. ;; @todo - make this as robust as the above.
  762. (semantic-tag-get-attribute tag :interfaces))
  763. ;;; Tags of class `function'
  764. ;;
  765. (defsubst semantic-tag-function-arguments (tag)
  766. "Return the arguments of the function that TAG describes.
  767. That is the value of the `:arguments' attribute."
  768. (semantic-tag-get-attribute tag :arguments))
  769. (defsubst semantic-tag-function-throws (tag)
  770. "Return the exceptions the function that TAG describes can throw.
  771. That is the value of the `:throws' attribute."
  772. (semantic-tag-get-attribute tag :throws))
  773. (defsubst semantic-tag-function-parent (tag)
  774. "Return the parent of the function that TAG describes.
  775. That is the value of the `:parent' attribute.
  776. A function has a parent if it is a method of a class, and if the
  777. function does not appear in body of its parent class."
  778. (semantic-tag-named-parent tag))
  779. (defsubst semantic-tag-function-destructor-p (tag)
  780. "Return non-nil if TAG describes a destructor function.
  781. That is the value of the `:destructor-flag' attribute."
  782. (semantic-tag-get-attribute tag :destructor-flag))
  783. (defsubst semantic-tag-function-constructor-p (tag)
  784. "Return non-nil if TAG describes a constructor function.
  785. That is the value of the `:constructor-flag' attribute."
  786. (semantic-tag-get-attribute tag :constructor-flag))
  787. ;;; Tags of class `variable'
  788. ;;
  789. (defsubst semantic-tag-variable-default (tag)
  790. "Return the default value of the variable that TAG describes.
  791. That is the value of the attribute `:default-value'."
  792. (semantic-tag-get-attribute tag :default-value))
  793. (defsubst semantic-tag-variable-constant-p (tag)
  794. "Return non-nil if the variable that TAG describes is a constant.
  795. That is the value of the attribute `:constant-flag'."
  796. (semantic-tag-get-attribute tag :constant-flag))
  797. ;;; Tags of class `include'
  798. ;;
  799. (defsubst semantic-tag-include-system-p (tag)
  800. "Return non-nil if the include that TAG describes is a system include.
  801. That is the value of the attribute `:system-flag'."
  802. (semantic-tag-get-attribute tag :system-flag))
  803. (define-overloadable-function semantic-tag-include-filename (tag)
  804. "Return a filename representation of TAG.
  805. The default action is to return the `semantic-tag-name'.
  806. Some languages do not use full filenames in their include statements.
  807. Override this method to translate the code representation
  808. into a filename. (A relative filename if necessary.)
  809. See `semantic-dependency-tag-file' to expand an include
  810. tag to a full file name.")
  811. (defun semantic-tag-include-filename-default (tag)
  812. "Return a filename representation of TAG.
  813. Returns `semantic-tag-name'."
  814. (semantic-tag-name tag))
  815. ;;; Tags of class `code'
  816. ;;
  817. (defsubst semantic-tag-code-detail (tag)
  818. "Return detail information from code that TAG describes.
  819. That is the value of the attribute `:detail'."
  820. (semantic-tag-get-attribute tag :detail))
  821. ;;; Tags of class `alias'
  822. ;;
  823. (defsubst semantic-tag-new-alias (name meta-tag-class value &rest attributes)
  824. "Create a semantic tag of class alias.
  825. NAME is a name for this alias.
  826. META-TAG-CLASS is the class of the tag this tag is an alias.
  827. VALUE is the aliased definition.
  828. ATTRIBUTES is a list of additional attributes belonging to this tag."
  829. (apply 'semantic-tag name 'alias
  830. :aliasclass meta-tag-class
  831. :definition value
  832. attributes))
  833. (defsubst semantic-tag-alias-class (tag)
  834. "Return the class of tag TAG is an alias."
  835. (semantic-tag-get-attribute tag :aliasclass))
  836. (define-overloadable-function semantic-tag-alias-definition (tag)
  837. "Return the definition TAG is an alias.
  838. The returned value is a tag of the class that
  839. `semantic-tag-alias-class' returns for TAG.
  840. The default is to return the value of the :definition attribute.
  841. Return nil if TAG is not of class 'alias."
  842. (when (semantic-tag-of-class-p tag 'alias)
  843. (:override
  844. (semantic-tag-get-attribute tag :definition))))
  845. ;;; Language Specific Tag access via overload
  846. ;;
  847. ;;;###autoload
  848. (define-overloadable-function semantic-tag-components (tag)
  849. "Return a list of components for TAG.
  850. A Component is a part of TAG which itself may be a TAG.
  851. Examples include the elements of a structure in a
  852. tag of class `type, or the list of arguments to a
  853. tag of class 'function."
  854. )
  855. (defun semantic-tag-components-default (tag)
  856. "Return a list of components for TAG.
  857. Perform the described task in `semantic-tag-components'."
  858. (cond ((semantic-tag-of-class-p tag 'type)
  859. (semantic-tag-type-members tag))
  860. ((semantic-tag-of-class-p tag 'function)
  861. (semantic-tag-function-arguments tag))
  862. (t nil)))
  863. (define-overloadable-function semantic-tag-components-with-overlays (tag)
  864. "Return the list of top level components belonging to TAG.
  865. Children are any sub-tags which contain overlays.
  866. Default behavior is to get `semantic-tag-components' in addition
  867. to the components of an anonymous types (if applicable.)
  868. Note for language authors:
  869. If a mode defines a language tag that has tags in it with overlays
  870. you should still return them with this function.
  871. Ignoring this step will prevent several features from working correctly."
  872. )
  873. (defun semantic-tag-components-with-overlays-default (tag)
  874. "Return the list of top level components belonging to TAG.
  875. Children are any sub-tags which contain overlays.
  876. The default action collects regular components of TAG, in addition
  877. to any components belonging to an anonymous type."
  878. (let ((explicit-children (semantic-tag-components tag))
  879. (type (semantic-tag-type tag))
  880. (anon-type-children nil)
  881. (all-children nil))
  882. ;; Identify if this tag has an anonymous structure as
  883. ;; its type. This implies it may have children with overlays.
  884. (when (and type (semantic-tag-p type))
  885. (setq anon-type-children (semantic-tag-components type))
  886. ;; Add anonymous children
  887. (while anon-type-children
  888. (when (semantic-tag-with-position-p (car anon-type-children))
  889. (setq all-children (cons (car anon-type-children) all-children)))
  890. (setq anon-type-children (cdr anon-type-children))))
  891. ;; Add explicit children
  892. (while explicit-children
  893. (when (semantic-tag-with-position-p (car explicit-children))
  894. (setq all-children (cons (car explicit-children) all-children)))
  895. (setq explicit-children (cdr explicit-children)))
  896. ;; Return
  897. (nreverse all-children)))
  898. (defun semantic-tag-children-compatibility (tag &optional positiononly)
  899. "Return children of TAG.
  900. If POSITIONONLY is nil, use `semantic-tag-components'.
  901. If POSITIONONLY is non-nil, use `semantic-tag-components-with-overlays'.
  902. DO NOT use this fcn in new code. Use one of the above instead."
  903. (if positiononly
  904. (semantic-tag-components-with-overlays tag)
  905. (semantic-tag-components tag)))
  906. ;;; Tag Region
  907. ;;
  908. ;; A Tag represents a region in a buffer. You can narrow to that tag.
  909. ;;
  910. (defun semantic-narrow-to-tag (&optional tag)
  911. "Narrow to the region specified by the bounds of TAG.
  912. See `semantic-tag-bounds'."
  913. (interactive)
  914. (if (not tag) (setq tag (semantic-current-tag)))
  915. (narrow-to-region (semantic-tag-start tag)
  916. (semantic-tag-end tag)))
  917. (defmacro semantic-with-buffer-narrowed-to-current-tag (&rest body)
  918. "Execute BODY with the buffer narrowed to the current tag."
  919. `(save-restriction
  920. (semantic-narrow-to-tag (semantic-current-tag))
  921. ,@body))
  922. (put 'semantic-with-buffer-narrowed-to-current-tag 'lisp-indent-function 0)
  923. (add-hook 'edebug-setup-hook
  924. (lambda ()
  925. (def-edebug-spec semantic-with-buffer-narrowed-to-current-tag
  926. (def-body))))
  927. (defmacro semantic-with-buffer-narrowed-to-tag (tag &rest body)
  928. "Narrow to TAG, and execute BODY."
  929. `(save-restriction
  930. (semantic-narrow-to-tag ,tag)
  931. ,@body))
  932. (put 'semantic-with-buffer-narrowed-to-tag 'lisp-indent-function 1)
  933. (add-hook 'edebug-setup-hook
  934. (lambda ()
  935. (def-edebug-spec semantic-with-buffer-narrowed-to-tag
  936. (def-body))))
  937. ;;; Tag Hooks
  938. ;;
  939. ;; Semantic may want to provide special hooks when specific operations
  940. ;; are about to happen on a given tag. These routines allow for hook
  941. ;; maintenance on a tag.
  942. ;; Internal global variable used to manage tag hooks. For example,
  943. ;; some implementation of `remove-hook' checks that the hook variable
  944. ;; is `default-boundp'.
  945. (defvar semantic--tag-hook-value)
  946. (defun semantic-tag-add-hook (tag hook function &optional append)
  947. "Onto TAG, add to the value of HOOK the function FUNCTION.
  948. FUNCTION is added (if necessary) at the beginning of the hook list
  949. unless the optional argument APPEND is non-nil, in which case
  950. FUNCTION is added at the end.
  951. HOOK should be a symbol, and FUNCTION may be any valid function.
  952. See also the function `add-hook'."
  953. (let ((semantic--tag-hook-value (semantic--tag-get-property tag hook)))
  954. (add-hook 'semantic--tag-hook-value function append)
  955. (semantic--tag-put-property tag hook semantic--tag-hook-value)
  956. semantic--tag-hook-value))
  957. (defun semantic-tag-remove-hook (tag hook function)
  958. "Onto TAG, remove from the value of HOOK the function FUNCTION.
  959. HOOK should be a symbol, and FUNCTION may be any valid function. If
  960. FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in
  961. the list of hooks to run in HOOK, then nothing is done.
  962. See also the function `remove-hook'."
  963. (let ((semantic--tag-hook-value (semantic--tag-get-property tag hook)))
  964. (remove-hook 'semantic--tag-hook-value function)
  965. (semantic--tag-put-property tag hook semantic--tag-hook-value)
  966. semantic--tag-hook-value))
  967. (defun semantic--tag-run-hooks (tag hook &rest args)
  968. "Run for TAG all expressions saved on the property HOOK.
  969. Each hook expression must take at least one argument, the TAG.
  970. For any given situation, additional ARGS may be passed."
  971. (let ((semantic--tag-hook-value (semantic--tag-get-property tag hook))
  972. (arglist (cons tag args)))
  973. (condition-case err
  974. ;; If a hook bombs, ignore it! Usually this is tied into
  975. ;; some sort of critical system.
  976. (apply 'run-hook-with-args 'semantic--tag-hook-value arglist)
  977. (error (message "Error: %S" err)))))
  978. ;;; Tags and Overlays
  979. ;;
  980. ;; Overlays are used so that we can quickly identify tags from
  981. ;; buffer positions and regions using built in Emacs commands.
  982. ;;
  983. (defsubst semantic--tag-unlink-list-from-buffer (tags)
  984. "Convert TAGS from using an overlay to using an overlay proxy.
  985. This function is for internal use only."
  986. (mapcar 'semantic--tag-unlink-from-buffer tags))
  987. (defun semantic--tag-unlink-from-buffer (tag)
  988. "Convert TAG from using an overlay to using an overlay proxy.
  989. This function is for internal use only."
  990. (when (semantic-tag-p tag)
  991. (let ((o (semantic-tag-overlay tag)))
  992. (when (semantic-overlay-p o)
  993. (semantic--tag-set-overlay
  994. tag (vector (semantic-overlay-start o)
  995. (semantic-overlay-end o)))
  996. (semantic-overlay-delete o))
  997. ;; Look for a link hook on TAG.
  998. (semantic--tag-run-hooks tag 'unlink-hook)
  999. ;; Fix the sub-tags which contain overlays.
  1000. (semantic--tag-unlink-list-from-buffer
  1001. (semantic-tag-components-with-overlays tag)))))
  1002. (defsubst semantic--tag-link-list-to-buffer (tags)
  1003. "Convert TAGS from using an overlay proxy to using an overlay.
  1004. This function is for internal use only."
  1005. (mapc 'semantic--tag-link-to-buffer tags))
  1006. (defun semantic--tag-link-to-buffer (tag)
  1007. "Convert TAG from using an overlay proxy to using an overlay.
  1008. This function is for internal use only."
  1009. (when (semantic-tag-p tag)
  1010. (let ((o (semantic-tag-overlay tag)))
  1011. (when (and (vectorp o) (= (length o) 2))
  1012. (setq o (semantic-make-overlay (aref o 0) (aref o 1)
  1013. (current-buffer)))
  1014. (semantic--tag-set-overlay tag o)
  1015. (semantic-overlay-put o 'semantic tag)
  1016. ;; Clear the :filename property
  1017. (semantic--tag-put-property tag :filename nil))
  1018. ;; Look for a link hook on TAG.
  1019. (semantic--tag-run-hooks tag 'link-hook)
  1020. ;; Fix the sub-tags which contain overlays.
  1021. (semantic--tag-link-list-to-buffer
  1022. (semantic-tag-components-with-overlays tag)))))
  1023. (defun semantic--tag-unlink-cache-from-buffer ()
  1024. "Convert all tags in the current cache to use overlay proxies.
  1025. This function is for internal use only."
  1026. (require 'semantic)
  1027. (semantic--tag-unlink-list-from-buffer
  1028. ;; @todo- use fetch-tags-fast?
  1029. (semantic-fetch-tags)))
  1030. (defvar semantic--buffer-cache)
  1031. (defun semantic--tag-link-cache-to-buffer ()
  1032. "Convert all tags in the current cache to use overlays.
  1033. This function is for internal use only."
  1034. (require 'semantic)
  1035. (condition-case nil
  1036. ;; In this unique case, we cannot call the usual toplevel fn.
  1037. ;; because we don't want a reparse, we want the old overlays.
  1038. (semantic--tag-link-list-to-buffer
  1039. semantic--buffer-cache)
  1040. ;; Recover when there is an error restoring the cache.
  1041. (error (message "Error recovering tag list")
  1042. (semantic-clear-toplevel-cache)
  1043. nil)))
  1044. ;;; Tag Cooking
  1045. ;;
  1046. ;; Raw tags from a parser follow a different positional format than
  1047. ;; those used in the buffer cache. Raw tags need to be cooked into
  1048. ;; semantic cache friendly tags for use by the masses.
  1049. ;;
  1050. (defsubst semantic--tag-expanded-p (tag)
  1051. "Return non-nil if TAG is expanded.
  1052. This function is for internal use only.
  1053. See also the function `semantic--expand-tag'."
  1054. ;; In fact a cooked tag is actually a list of cooked tags
  1055. ;; because a raw tag can be expanded in several cooked ones!
  1056. (when (consp tag)
  1057. (while (and (semantic-tag-p (car tag))
  1058. (vectorp (semantic-tag-overlay (car tag))))
  1059. (setq tag (cdr tag)))
  1060. (null tag)))
  1061. (defvar semantic-tag-expand-function nil
  1062. "Function used to expand a tag.
  1063. It is passed each tag production, and must return a list of tags
  1064. derived from it, or nil if it does not need to be expanded.
  1065. Languages with compound definitions should use this function to expand
  1066. from one compound symbol into several. For example, in C or Java the
  1067. following definition is easily parsed into one tag:
  1068. int a, b;
  1069. This function should take this compound tag and turn it into two tags,
  1070. one for A, and the other for B.")
  1071. (make-variable-buffer-local 'semantic-tag-expand-function)
  1072. (defun semantic--tag-expand (tag)
  1073. "Convert TAG from a raw state to a cooked state, and expand it.
  1074. Returns a list of cooked tags.
  1075. The parser returns raw tags with positional data START END at the
  1076. end of the tag data structure (a list for now). We convert it from
  1077. that to a cooked state that uses an overlay proxy, that is, a vector
  1078. \[START END].
  1079. The raw tag is changed with side effects and maybe expanded in
  1080. several derived tags when the variable `semantic-tag-expand-function'
  1081. is set.
  1082. This function is for internal use only."
  1083. (if (semantic--tag-expanded-p tag)
  1084. ;; Just return TAG if it is already expanded (by a grammar
  1085. ;; semantic action), or if it isn't recognized as a valid
  1086. ;; semantic tag.
  1087. tag
  1088. ;; Try to cook the tag. This code will be removed when tag will
  1089. ;; be directly created with the right format.
  1090. (condition-case nil
  1091. (let ((ocdr (semantic--tag-overlay-cdr tag)))
  1092. ;; OCDR contains the sub-list of TAG whose car is the
  1093. ;; OVERLAY part of TAG. That is, a list (OVERLAY START END).
  1094. ;; Convert it into an overlay proxy ([START END]).
  1095. (semantic--tag-set-overlay
  1096. tag (vector (nth 1 ocdr) (nth 2 ocdr)))
  1097. ;; Remove START END positions at end of tag.
  1098. (setcdr ocdr nil)
  1099. ;; At this point (length TAG) must be 5!
  1100. ;;(unless (= (length tag) 5)
  1101. ;; (error "Tag expansion failed"))
  1102. )
  1103. (error
  1104. (message "A Rule must return a single tag-line list!")
  1105. (debug tag)
  1106. nil))
  1107. ;; Expand based on local configuration
  1108. (if semantic-tag-expand-function
  1109. (or (funcall semantic-tag-expand-function tag)
  1110. (list tag))
  1111. (list tag))))
  1112. ;; Foreign tags
  1113. ;;
  1114. (defmacro semantic-foreign-tag-invalid (tag)
  1115. "Signal that TAG is an invalid foreign tag."
  1116. `(signal 'wrong-type-argument '(semantic-foreign-tag-p ,tag)))
  1117. (defsubst semantic-foreign-tag-p (tag)
  1118. "Return non-nil if TAG is a foreign tag.
  1119. That is, a tag unlinked from the originating buffer, which carries the
  1120. originating buffer file name, and major mode."
  1121. (and (semantic-tag-p tag)
  1122. (semantic--tag-get-property tag :foreign-flag)))
  1123. (defsubst semantic-foreign-tag-check (tag)
  1124. "Check that TAG is a valid foreign tag.
  1125. Signal an error if not."
  1126. (or (semantic-foreign-tag-p tag)
  1127. (semantic-foreign-tag-invalid tag)))
  1128. (defun semantic-foreign-tag (&optional tag)
  1129. "Return a copy of TAG as a foreign tag, or nil if it can't be done.
  1130. TAG defaults to the tag at point in current buffer.
  1131. See also `semantic-foreign-tag-p'."
  1132. (or tag (setq tag (semantic-current-tag)))
  1133. (when (semantic-tag-p tag)
  1134. (let ((ftag (semantic-tag-copy tag nil t))
  1135. ;; Do extra work for the doc strings, since this is a
  1136. ;; common use case.
  1137. (doc (condition-case nil
  1138. (semantic-documentation-for-tag tag)
  1139. (error nil))))
  1140. ;; A foreign tag must carry its originating buffer file name!
  1141. (when (semantic--tag-get-property ftag :filename)
  1142. (semantic--tag-put-property ftag :mode (semantic-tag-mode tag))
  1143. (semantic--tag-put-property ftag :documentation doc)
  1144. (semantic--tag-put-property ftag :foreign-flag t)
  1145. ftag))))
  1146. ;; High level obtain/insert foreign tag overloads
  1147. (define-overloadable-function semantic-obtain-foreign-tag (&optional tag)
  1148. "Obtain a foreign tag from TAG.
  1149. TAG defaults to the tag at point in current buffer.
  1150. Return the obtained foreign tag or nil if failed."
  1151. (semantic-foreign-tag tag))
  1152. (defun semantic-insert-foreign-tag-default (foreign-tag)
  1153. "Insert FOREIGN-TAG into the current buffer.
  1154. The default behavior assumes the current buffer is a language file,
  1155. and attempts to insert a prototype/function call."
  1156. ;; Long term goal: Have a mechanism for a tempo-like template insert
  1157. ;; for the given tag.
  1158. (insert (semantic-format-tag-prototype foreign-tag)))
  1159. (define-overloadable-function semantic-insert-foreign-tag (foreign-tag)
  1160. "Insert FOREIGN-TAG into the current buffer.
  1161. Signal an error if FOREIGN-TAG is not a valid foreign tag.
  1162. This function is overridable with the symbol `insert-foreign-tag'."
  1163. (semantic-foreign-tag-check foreign-tag)
  1164. (:override)
  1165. (message (semantic-format-tag-summarize foreign-tag)))
  1166. ;;; Support log modes here
  1167. (define-mode-local-override semantic-insert-foreign-tag
  1168. log-edit-mode (foreign-tag)
  1169. "Insert foreign tags into log-edit mode."
  1170. (insert (concat "(" (semantic-format-tag-name foreign-tag) "): ")))
  1171. (define-mode-local-override semantic-insert-foreign-tag
  1172. change-log-mode (foreign-tag)
  1173. "Insert foreign tags into log-edit mode."
  1174. (insert (concat "(" (semantic-format-tag-name foreign-tag) "): ")))
  1175. ;;; Compatibility
  1176. ;;
  1177. (defconst semantic-token-version
  1178. semantic-tag-version)
  1179. (defconst semantic-token-incompatible-version
  1180. semantic-tag-incompatible-version)
  1181. (defsubst semantic-token-type-parent (tag)
  1182. "Return the parent of the type that TAG describes.
  1183. The return value is a list. A value of nil means no parents.
  1184. The `car' of the list is either the parent class, or a list
  1185. of parent classes. The `cdr' of the list is the list of
  1186. interfaces, or abstract classes which are parents of TAG."
  1187. (cons (semantic-tag-get-attribute tag :superclasses)
  1188. (semantic-tag-type-interfaces tag)))
  1189. (make-obsolete 'semantic-token-type-parent
  1190. "\
  1191. use `semantic-tag-type-superclass' \
  1192. and `semantic-tag-type-interfaces' instead" "23.2")
  1193. (semantic-alias-obsolete 'semantic-tag-make-assoc-list
  1194. 'semantic-tag-make-plist "23.2")
  1195. (semantic-varalias-obsolete 'semantic-expand-nonterminal
  1196. 'semantic-tag-expand-function "23.2")
  1197. (provide 'semantic/tag)
  1198. ;; Local variables:
  1199. ;; generated-autoload-file: "loaddefs.el"
  1200. ;; generated-autoload-load-name: "semantic/tag"
  1201. ;; End:
  1202. ;;; semantic/tag.el ends here