tag.el 51 KB

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