Fix renamed filenames in file headers.
[emacs.git] / lisp / cedet / semantic / tag.el
blobf46eae99c3875cbb07c55ee0c81386a0597fb6c2
1 ;;; tag.el --- tag creation and access
3 ;;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007,
4 ;;; 2008, 2009 Free Software Foundation, Inc.
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; I. The core production of semantic is the list of tags produced by the
26 ;; different parsers. This file provides 3 APIs related to tag access:
28 ;; 1) Primitive Tag Access
29 ;; There is a set of common features to all tags. These access
30 ;; functions can get these values.
31 ;; 2) Standard Tag Access
32 ;; A Standard Tag should be produced by most traditional languages
33 ;; with standard styles common to typed object oriented languages.
34 ;; These functions can access these data elements from a tag.
35 ;; 3) Generic Tag Access
36 ;; Access to tag structure in a more direct way.
37 ;; ** May not be forward compatible.
39 ;; II. There is also an API for tag creation. Use `semantic-tag' to create
40 ;; a new tag.
42 ;; III. Tag Comparison. Allows explicit or comparitive tests to see
43 ;; if two tags are the same.
45 ;;; History:
48 ;;; Code:
51 ;; Keep this only so long as we have obsolete fcns.
52 (require 'semantic/fw)
54 (defconst semantic-tag-version semantic-version
55 "Version string of semantic tags made with this code.")
57 (defconst semantic-tag-incompatible-version "1.0"
58 "Version string of semantic tags which are not currently compatible.
59 These old style tags may be loaded from a file with semantic db.
60 In this case, we must flush the old tags and start over.")
62 ;;; Primitive Tag access system:
64 ;; Raw tags in semantic are lists of 5 elements:
66 ;; (NAME CLASS ATTRIBUTES PROPERTIES OVERLAY)
68 ;; Where:
70 ;; - NAME is a string that represents the tag name.
72 ;; - CLASS is a symbol that represent the class of the tag (for
73 ;; example, usual classes are `type', `function', `variable',
74 ;; `include', `package', `code').
76 ;; - ATTRIBUTES is a public list of attributes that describes
77 ;; language data represented by the tag (for example, a variable
78 ;; can have a `:constant-flag' attribute, a function an `:arguments'
79 ;; attribute, etc.).
81 ;; - PROPERTIES is a private list of properties used internally.
83 ;; - OVERLAY represent the location of data described by the tag.
86 (defsubst semantic-tag-name (tag)
87 "Return the name of TAG.
88 For functions, variables, classes, typedefs, etc., this is the identifier
89 that is being defined. For tags without an obvious associated name, this
90 may be the statement type, e.g., this may return @code{print} for python's
91 print statement."
92 (car tag))
94 (defsubst semantic-tag-class (tag)
95 "Return the class of TAG.
96 That is, the symbol 'variable, 'function, 'type, or other.
97 There is no limit to the symbols that may represent the class of a tag.
98 Each parser generates tags with classes defined by it.
100 For functional languages, typical tag classes are:
102 @table @code
103 @item type
104 Data types, named map for a memory block.
105 @item function
106 A function or method, or named execution location.
107 @item variable
108 A variable, or named storage for data.
109 @item include
110 Statement that represents a file from which more tags can be found.
111 @item package
112 Statement that declairs this file's package name.
113 @item code
114 Code that has not name or binding to any other symbol, such as in a script.
115 @end table
117 (nth 1 tag))
119 (defsubst semantic-tag-attributes (tag)
120 "Return the list of public attributes of TAG.
121 That is a property list: (ATTRIBUTE-1 VALUE-1 ATTRIBUTE-2 VALUE-2...)."
122 (nth 2 tag))
124 (defsubst semantic-tag-properties (tag)
125 "Return the list of private properties of TAG.
126 That is a property list: (PROPERTY-1 VALUE-1 PROPERTY-2 VALUE-2...)."
127 (nth 3 tag))
129 (defsubst semantic-tag-overlay (tag)
130 "Return the OVERLAY part of TAG.
131 That is, an overlay or an unloaded buffer representation.
132 This function can also return an array of the form [ START END ].
133 This occurs for tags that are not currently linked into a buffer."
134 (nth 4 tag))
136 (defsubst semantic--tag-overlay-cdr (tag)
137 "Return the cons cell whose car is the OVERLAY part of TAG.
138 That function is for internal use only."
139 (nthcdr 4 tag))
141 (defsubst semantic--tag-set-overlay (tag overlay)
142 "Set the overlay part of TAG with OVERLAY.
143 That function is for internal use only."
144 (setcar (semantic--tag-overlay-cdr tag) overlay))
146 (defsubst semantic-tag-start (tag)
147 "Return the start location of TAG."
148 (let ((o (semantic-tag-overlay tag)))
149 (if (semantic-overlay-p o)
150 (semantic-overlay-start o)
151 (aref o 0))))
153 (defsubst semantic-tag-end (tag)
154 "Return the end location of TAG."
155 (let ((o (semantic-tag-overlay tag)))
156 (if (semantic-overlay-p o)
157 (semantic-overlay-end o)
158 (aref o 1))))
160 (defsubst semantic-tag-bounds (tag)
161 "Return the location (START END) of data TAG describes."
162 (list (semantic-tag-start tag)
163 (semantic-tag-end tag)))
165 (defun semantic-tag-set-bounds (tag start end)
166 "In TAG, set the START and END location of data it describes."
167 (let ((o (semantic-tag-overlay tag)))
168 (if (semantic-overlay-p o)
169 (semantic-overlay-move o start end)
170 (semantic--tag-set-overlay tag (vector start end)))))
172 (defun semantic-tag-in-buffer-p (tag)
173 "Return the buffer TAG resides in IFF tag is already in a buffer.
174 If a tag is not in a buffer, return nil."
175 (let ((o (semantic-tag-overlay tag)))
176 ;; TAG is currently linked to a buffer, return it.
177 (when (and (semantic-overlay-p o)
178 (semantic-overlay-live-p o))
179 (semantic-overlay-buffer o))))
181 (defsubst semantic--tag-get-property (tag property)
182 "From TAG, extract the value of PROPERTY.
183 Return the value found, or nil if PROPERTY is not one of the
184 properties of TAG.
185 That function is for internal use only."
186 (plist-get (semantic-tag-properties tag) property))
188 (defun semantic-tag-buffer (tag)
189 "Return the buffer TAG resides in.
190 If TAG has an originating file, read that file into a (maybe new)
191 buffer, and return it.
192 Return nil if there is no buffer for this tag."
193 (let ((buff (semantic-tag-in-buffer-p tag)))
194 (if buff
195 buff
196 ;; TAG has an originating file, read that file into a buffer, and
197 ;; return it.
198 (if (semantic--tag-get-property tag :filename)
199 (find-file-noselect (semantic--tag-get-property tag :filename))
200 ;; TAG is not in Emacs right now, no buffer is available.
201 ))))
203 (defun semantic-tag-mode (&optional tag)
204 "Return the major mode active for TAG.
205 TAG defaults to the tag at point in current buffer.
206 If TAG has a :mode property return it.
207 If point is inside TAG bounds, return the major mode active at point.
208 Return the major mode active at beginning of TAG otherwise.
209 See also the function `semantic-ctxt-current-mode'."
210 (or tag (setq tag (semantic-current-tag)))
211 (or (semantic--tag-get-property tag :mode)
212 (let ((buffer (semantic-tag-buffer tag))
213 (start (semantic-tag-start tag))
214 (end (semantic-tag-end tag)))
215 (save-excursion
216 (and buffer (set-buffer buffer))
217 ;; Unless point is inside TAG bounds, move it to the
218 ;; beginning of TAG.
219 (or (and (>= (point) start) (< (point) end))
220 (goto-char start))
221 (require 'semantic/ctxt)
222 (semantic-ctxt-current-mode)))))
224 (defsubst semantic--tag-attributes-cdr (tag)
225 "Return the cons cell whose car is the ATTRIBUTES part of TAG.
226 That function is for internal use only."
227 (nthcdr 2 tag))
229 (defsubst semantic-tag-put-attribute (tag attribute value)
230 "Change value in TAG of ATTRIBUTE to VALUE.
231 If ATTRIBUTE already exists, its value is set to VALUE, otherwise the
232 new ATTRIBUTE VALUE pair is added.
233 Return TAG.
234 Use this function in a parser when not all attributes are known at the
235 same time."
236 (let* ((plist-cdr (semantic--tag-attributes-cdr tag)))
237 (when (consp plist-cdr)
238 (setcar plist-cdr
239 (semantic-tag-make-plist
240 (plist-put (car plist-cdr) attribute value))))
241 tag))
243 (defun semantic-tag-put-attribute-no-side-effect (tag attribute value)
244 "Change value in TAG of ATTRIBUTE to VALUE without side effects.
245 All cons cells in the attribute list are replicated so that there
246 are no side effects if TAG is in shared lists.
247 If ATTRIBUTE already exists, its value is set to VALUE, otherwise the
248 new ATTRIBUTE VALUE pair is added.
249 Return TAG."
250 (let* ((plist-cdr (semantic--tag-attributes-cdr tag)))
251 (when (consp plist-cdr)
252 (setcar plist-cdr
253 (semantic-tag-make-plist
254 (plist-put (copy-sequence (car plist-cdr))
255 attribute value))))
256 tag))
258 (defsubst semantic-tag-get-attribute (tag attribute)
259 "From TAG, return the value of ATTRIBUTE.
260 ATTRIBUTE is a symbol whose specification value to get.
261 Return the value found, or nil if ATTRIBUTE is not one of the
262 attributes of TAG."
263 (plist-get (semantic-tag-attributes tag) attribute))
265 ;; These functions are for internal use only!
266 (defsubst semantic--tag-properties-cdr (tag)
267 "Return the cons cell whose car is the PROPERTIES part of TAG.
268 That function is for internal use only."
269 (nthcdr 3 tag))
271 (defun semantic--tag-put-property (tag property value)
272 "Change value in TAG of PROPERTY to VALUE.
273 If PROPERTY already exists, its value is set to VALUE, otherwise the
274 new PROPERTY VALUE pair is added.
275 Return TAG.
276 That function is for internal use only."
277 (let* ((plist-cdr (semantic--tag-properties-cdr tag)))
278 (when (consp plist-cdr)
279 (setcar plist-cdr
280 (semantic-tag-make-plist
281 (plist-put (car plist-cdr) property value))))
282 tag))
284 (defun semantic--tag-put-property-no-side-effect (tag property value)
285 "Change value in TAG of PROPERTY to VALUE without side effects.
286 All cons cells in the property list are replicated so that there
287 are no side effects if TAG is in shared lists.
288 If PROPERTY already exists, its value is set to VALUE, otherwise the
289 new PROPERTY VALUE pair is added.
290 Return TAG.
291 That function is for internal use only."
292 (let* ((plist-cdr (semantic--tag-properties-cdr tag)))
293 (when (consp plist-cdr)
294 (setcar plist-cdr
295 (semantic-tag-make-plist
296 (plist-put (copy-sequence (car plist-cdr))
297 property value))))
298 tag))
300 (defun semantic-tag-file-name (tag)
301 "Return the name of the file from which TAG originated.
302 Return nil if that information can't be obtained.
303 If TAG is from a loaded buffer, then that buffer's filename is used.
304 If TAG is unlinked, but has a :filename property, then that is used."
305 (let ((buffer (semantic-tag-in-buffer-p tag)))
306 (if buffer
307 (buffer-file-name buffer)
308 (semantic--tag-get-property tag :filename))))
310 ;;; Tag tests and comparisons.
312 ;;;###autoload
313 (defsubst semantic-tag-p (tag)
314 "Return non-nil if TAG is most likely a semantic tag."
315 (condition-case nil
316 (and (consp tag)
317 (stringp (car tag)) ; NAME
318 (symbolp (nth 1 tag)) (nth 1 tag) ; TAG-CLASS
319 (listp (nth 2 tag)) ; ATTRIBUTES
320 (listp (nth 3 tag)) ; PROPERTIES
322 ;; If an error occurs, then it most certainly is not a tag.
323 (error nil)))
325 (defsubst semantic-tag-of-class-p (tag class)
326 "Return non-nil if class of TAG is CLASS."
327 (eq (semantic-tag-class tag) class))
329 (defsubst semantic-tag-type-members (tag)
330 "Return the members of the type that TAG describes.
331 That is the value of the `:members' attribute."
332 (semantic-tag-get-attribute tag :members))
334 (defun semantic-tag-with-position-p (tag)
335 "Return non-nil if TAG has positional information."
336 (and (semantic-tag-p tag)
337 (let ((o (semantic-tag-overlay tag)))
338 (or (and (semantic-overlay-p o)
339 (semantic-overlay-live-p o))
340 (arrayp o)))))
342 (defun semantic-equivalent-tag-p (tag1 tag2)
343 "Compare TAG1 and TAG2 and return non-nil if they are equivalent.
344 Use `equal' on elements the name, class, and position.
345 Use this function if tags are being copied and regrouped to test
346 for if two tags represent the same thing, but may be constructed
347 of different cons cells."
348 (and (equal (semantic-tag-name tag1) (semantic-tag-name tag2))
349 (semantic-tag-of-class-p tag1 (semantic-tag-class tag2))
350 (or (and (not (semantic-tag-overlay tag1))
351 (not (semantic-tag-overlay tag2)))
352 (and (semantic-tag-overlay tag1)
353 (semantic-tag-overlay tag2)
354 (equal (semantic-tag-bounds tag1)
355 (semantic-tag-bounds tag2))))))
357 (defsubst semantic-tag-type (tag)
358 "Return the value of the `:type' attribute of TAG.
359 For a function it would be the data type of the return value.
360 For a variable, it is the storage type of that variable.
361 For a data type, the type is the style of datatype, such as
362 struct or union."
363 (semantic-tag-get-attribute tag :type))
365 (defun semantic-tag-similar-p (tag1 tag2 &rest ignorable-attributes)
366 "Test to see if TAG1 and TAG2 are similar.
367 Two tags are similar if their name, datatype, and various attributes
368 are the same.
370 Similar tags that have sub-tags such as arg lists or type members,
371 are similar w/out checking the sub-list of tags.
372 Optional argument IGNORABLE-ATTRIBUTES are attributes to ignore while comparing similarity."
373 (let* ((A1 (and (equal (semantic-tag-name tag1) (semantic-tag-name tag2))
374 (semantic-tag-of-class-p tag1 (semantic-tag-class tag2))
375 (semantic-tag-of-type-p tag1 (semantic-tag-type tag2))))
376 (attr1 (semantic-tag-attributes tag1))
377 (A2 (= (length attr1) (length (semantic-tag-attributes tag2))))
378 (A3 t)
380 (when (and (not A2) ignorable-attributes)
381 (setq A2 t))
382 (while (and A2 attr1 A3)
383 (let ((a (car attr1))
384 (v (car (cdr attr1))))
386 (cond ((or (eq a :type) ;; already tested above.
387 (memq a ignorable-attributes)) ;; Ignore them...
388 nil)
390 ;; Don't test sublists of tags
391 ((and (listp v) (semantic-tag-p (car v)))
392 nil)
394 ;; The attributes are not the same?
395 ((not (equal v (semantic-tag-get-attribute tag2 a)))
396 (setq A3 nil))
398 nil))
400 (setq attr1 (cdr (cdr attr1))))
402 (and A1 A2 A3)
405 (defun semantic-tag-similar-with-subtags-p (tag1 tag2 &rest ignorable-attributes)
406 "Test to see if TAG1 and TAG2 are similar.
407 Uses `semantic-tag-similar-p' but also recurses through sub-tags, such
408 as argument lists and type members.
409 Optional argument IGNORABLE-ATTRIBUTES is passed down to
410 `semantic-tag-similar-p'."
411 (let ((C1 (semantic-tag-components tag1))
412 (C2 (semantic-tag-components tag2))
414 (if (or (/= (length C1) (length C2))
415 (not (semantic-tag-similar-p tag1 tag2 ignorable-attributes))
417 ;; Basic test fails.
419 ;; Else, check component lists.
420 (catch 'component-dissimilar
421 (while C1
423 (if (not (semantic-tag-similar-with-subtags-p
424 (car C1) (car C2) ignorable-attributes))
425 (throw 'component-dissimilar nil))
427 (setq C1 (cdr C1))
428 (setq C2 (cdr C2))
430 ;; If we made it this far, we are ok.
431 t) )))
434 (defun semantic-tag-of-type-p (tag type)
435 "Compare TAG's type against TYPE. Non nil if equivalent.
436 TYPE can be a string, or a tag of class 'type.
437 This can be complex since some tags might have a :type that is a tag,
438 while other tags might just have a string. This function will also be
439 return true of TAG's type is compared directly to the declaration of a
440 data type."
441 (let* ((tagtype (semantic-tag-type tag))
442 (tagtypestring (cond ((stringp tagtype)
443 tagtype)
444 ((and (semantic-tag-p tagtype)
445 (semantic-tag-of-class-p tagtype 'type))
446 (semantic-tag-name tagtype))
447 (t "")))
448 (typestring (cond ((stringp type)
449 type)
450 ((and (semantic-tag-p type)
451 (semantic-tag-of-class-p type 'type))
452 (semantic-tag-name type))
453 (t "")))
455 (and
456 tagtypestring
458 ;; Matching strings (input type is string)
459 (and (stringp type)
460 (string= tagtypestring type))
461 ;; Matching strings (tag type is string)
462 (and (stringp tagtype)
463 (string= tagtype typestring))
464 ;; Matching tokens, and the type of the type is the same.
465 (and (string= tagtypestring typestring)
466 (if (and (semantic-tag-type tagtype) (semantic-tag-type type))
467 (equal (semantic-tag-type tagtype) (semantic-tag-type type))
472 (defun semantic-tag-type-compound-p (tag)
473 "Return non-nil the type of TAG is compound.
474 Compound implies a structure or similar data type.
475 Returns the list of tag members if it is compound."
476 (let* ((tagtype (semantic-tag-type tag))
478 (when (and (semantic-tag-p tagtype)
479 (semantic-tag-of-class-p tagtype 'type))
480 ;; We have the potential of this being a nifty compound type.
481 (semantic-tag-type-members tagtype)
484 (defun semantic-tag-faux-p (tag)
485 "Return non-nil if TAG is a FAUX tag.
486 FAUX tags are created to represent a construct that is
487 not known to exist in the code.
489 Example: When the class browser sees methods to a class, but
490 cannot find the class, it will create a faux tag to represent the
491 class to store those methods."
492 (semantic--tag-get-property tag :faux-flag))
494 ;;; Tag creation
497 ;; Is this function still necessary?
498 (defun semantic-tag-make-plist (args)
499 "Create a property list with ARGS.
500 Args is a property list of the form (KEY1 VALUE1 ... KEYN VALUEN).
501 Where KEY is a symbol, and VALUE is the value for that symbol.
502 The return value will be a new property list, with these KEY/VALUE
503 pairs eliminated:
505 - KEY associated to nil VALUE.
506 - KEY associated to an empty string VALUE.
507 - KEY associated to a zero VALUE."
508 (let (plist key val)
509 (while args
510 (setq key (car args)
511 val (nth 1 args)
512 args (nthcdr 2 args))
513 (or (member val '("" nil))
514 (and (numberp val) (zerop val))
515 (setq plist (cons key (cons val plist)))))
516 ;; It is not useful to reverse the new plist.
517 plist))
519 (defsubst semantic-tag (name class &rest attributes)
520 "Create a generic semantic tag.
521 NAME is a string representing the name of this tag.
522 CLASS is the symbol that represents the class of tag this is,
523 such as 'variable, or 'function.
524 ATTRIBUTES is a list of additional attributes belonging to this tag."
525 (list name class (semantic-tag-make-plist attributes) nil nil))
527 (defsubst semantic-tag-new-variable (name type &optional default-value &rest attributes)
528 "Create a semantic tag of class 'variable.
529 NAME is the name of this variable.
530 TYPE is a string or semantic tag representing the type of this variable.
531 Optional DEFAULT-VALUE is a string representing the default value of this variable.
532 ATTRIBUTES is a list of additional attributes belonging to this tag."
533 (apply 'semantic-tag name 'variable
534 :type type
535 :default-value default-value
536 attributes))
538 (defsubst semantic-tag-new-function (name type arg-list &rest attributes)
539 "Create a semantic tag of class 'function.
540 NAME is the name of this function.
541 TYPE is a string or semantic tag representing the type of this function.
542 ARG-LIST is a list of strings or semantic tags representing the
543 arguments of this function.
544 ATTRIBUTES is a list of additional attributes belonging to this tag."
545 (apply 'semantic-tag name 'function
546 :type type
547 :arguments arg-list
548 attributes))
550 (defsubst semantic-tag-new-type (name type members parents &rest attributes)
551 "Create a semantic tag of class 'type.
552 NAME is the name of this type.
553 TYPE is a string or semantic tag representing the type of this type.
554 MEMBERS is a list of strings or semantic tags representing the
555 elements that make up this type if it is a composite type.
556 PARENTS is a cons cell. (EXPLICIT-PARENTS . INTERFACE-PARENTS)
557 EXPLICIT-PARENTS can be a single string (Just one parent) or a
558 list of parents (in a multiple inheritance situation). It can also
559 be nil.
560 INTERFACE-PARENTS is a list of strings representing the names of
561 all INTERFACES, or abstract classes inherited from. It can also be
562 nil.
563 This slot can be interesting because the form:
564 ( nil \"string\")
565 is a valid parent where there is no explicit parent, and only an
566 interface.
567 ATTRIBUTES is a list of additional attributes belonging to this tag."
568 (apply 'semantic-tag name 'type
569 :type type
570 :members members
571 :superclasses (car parents)
572 :interfaces (cdr parents)
573 attributes))
575 (defsubst semantic-tag-new-include (name system-flag &rest attributes)
576 "Create a semantic tag of class 'include.
577 NAME is the name of this include.
578 SYSTEM-FLAG represents that we were able to identify this include as belonging
579 to the system, as opposed to belonging to the local project.
580 ATTRIBUTES is a list of additional attributes belonging to this tag."
581 (apply 'semantic-tag name 'include
582 :system-flag system-flag
583 attributes))
585 (defsubst semantic-tag-new-package (name detail &rest attributes)
586 "Create a semantic tag of class 'package.
587 NAME is the name of this package.
588 DETAIL is extra information about this package, such as a location where
589 it can be found.
590 ATTRIBUTES is a list of additional attributes belonging to this tag."
591 (apply 'semantic-tag name 'package
592 :detail detail
593 attributes))
595 (defsubst semantic-tag-new-code (name detail &rest attributes)
596 "Create a semantic tag of class 'code.
597 NAME is a name for this code.
598 DETAIL is extra information about the code.
599 ATTRIBUTES is a list of additional attributes belonging to this tag."
600 (apply 'semantic-tag name 'code
601 :detail detail
602 attributes))
604 (defsubst semantic-tag-set-faux (tag)
605 "Set TAG to be a new FAUX tag.
606 FAUX tags represent constructs not found in the source code.
607 You can identify a faux tag with `semantic-tag-faux-p'"
608 (semantic--tag-put-property tag :faux-flag t))
610 (defsubst semantic-tag-set-name (tag name)
611 "Set TAG name to NAME."
612 (setcar tag name))
614 ;;; Copying and cloning tags.
616 (defsubst semantic-tag-clone (tag &optional name)
617 "Clone TAG, creating a new TAG.
618 If optional argument NAME is not nil it specifies a new name for the
619 cloned tag."
620 ;; Right now, TAG is a list.
621 (list (or name (semantic-tag-name tag))
622 (semantic-tag-class tag)
623 (copy-sequence (semantic-tag-attributes tag))
624 (copy-sequence (semantic-tag-properties tag))
625 (semantic-tag-overlay tag)))
627 (defun semantic-tag-copy (tag &optional name keep-file)
628 "Return a copy of TAG unlinked from the originating buffer.
629 If optional argument NAME is non-nil it specifies a new name for the
630 copied tag.
631 If optional argument KEEP-FILE is non-nil, and TAG was linked to a
632 buffer, the originating buffer file name is kept in the `:filename'
633 property of the copied tag.
634 If KEEP-FILE is a string, and the orginating buffer is NOT available,
635 then KEEP-FILE is stored on the `:filename' property.
636 This runs the tag hook `unlink-copy-hook`."
637 ;; Right now, TAG is a list.
638 (let ((copy (semantic-tag-clone tag name)))
640 ;; Keep the filename if needed.
641 (when keep-file
642 (semantic--tag-put-property
643 copy :filename (or (semantic-tag-file-name copy)
644 (and (stringp keep-file)
645 keep-file)
648 (when (semantic-tag-with-position-p tag)
649 ;; Convert the overlay to a vector, effectively 'unlinking' the tag.
650 (semantic--tag-set-overlay
651 copy (vector (semantic-tag-start copy) (semantic-tag-end copy)))
653 ;; Force the children to be copied also.
654 ;;(let ((chil (semantic--tag-copy-list
655 ;; (semantic-tag-components-with-overlays tag)
656 ;; keep-file)))
657 ;;;; Put the list into TAG.
660 ;; Call the unlink-copy hook. This should tell tools that
661 ;; this tag is not part of any buffer.
662 (when (semantic-overlay-p (semantic-tag-overlay tag))
663 (semantic--tag-run-hooks copy 'unlink-copy-hook))
665 copy))
667 ;;(defun semantic--tag-copy-list (tags &optional keep-file)
668 ;; "Make copies of TAGS and return the list of TAGS."
669 ;; (let ((out nil))
670 ;; (dolist (tag tags out)
671 ;; (setq out (cons (semantic-tag-copy tag nil keep-file)
672 ;; out))
673 ;; )))
675 (defun semantic--tag-copy-properties (tag1 tag2)
676 "Copy private properties from TAG1 to TAG2.
677 Return TAG2.
678 This function is for internal use only."
679 (let ((plist (semantic-tag-properties tag1)))
680 (while plist
681 (semantic--tag-put-property tag2 (car plist) (nth 1 plist))
682 (setq plist (nthcdr 2 plist)))
683 tag2))
685 ;;; DEEP COPIES
687 (defun semantic-tag-deep-copy-one-tag (tag &optional filter)
688 "Make a deep copy of TAG, applying FILTER to each child-tag.
689 Properties and overlay info are not copied.
690 FILTER takes TAG as an argument, and should returns a semantic-tag.
691 It is safe for FILTER to modify the input tag and return it."
692 (when (not filter) (setq filter 'identity))
693 (when (not (semantic-tag-p tag))
694 (signal 'wrong-type-argument (list tag 'semantic-tag-p)))
695 (funcall filter (list (semantic-tag-name tag)
696 (semantic-tag-class tag)
697 (semantic--tag-deep-copy-attributes
698 (semantic-tag-attributes tag) filter)
700 nil)))
702 (defun semantic--tag-deep-copy-attributes (attrs &optional filter)
703 "Make a deep copy of ATTRS, applying FILTER to each child-tag.
705 It is safe to modify ATTR, and return a permutaion of that list.
707 FILTER takes TAG as an argument, and should returns a semantic-tag.
708 It is safe for FILTER to modify the input tag and return it."
709 (when (car attrs)
710 (when (not (symbolp (car attrs))) (error "Bad Attribute List in tag"))
711 (cons (car attrs)
712 (cons (semantic--tag-deep-copy-value (nth 1 attrs) filter)
713 (semantic--tag-deep-copy-attributes (nthcdr 2 attrs) filter)))))
715 (defun semantic--tag-deep-copy-value (value &optional filter)
716 "Make a deep copy of VALUE, applying FILTER to each child-tag.
718 It is safe to modify VALUE, and return a permutaion of that list.
720 FILTER takes TAG as an argument, and should returns a semantic-tag.
721 It is safe for FILTER to modify the input tag and return it."
722 (cond
723 ;; Another tag.
724 ((semantic-tag-p value)
725 (semantic-tag-deep-copy-one-tag value filter))
727 ;; A list of more tags
728 ((and (listp value) (semantic-tag-p (car value)))
729 (semantic--tag-deep-copy-tag-list value filter))
731 ;; Some arbitrary data.
732 (t value)))
734 (defun semantic--tag-deep-copy-tag-list (tags &optional filter)
735 "Make a deep copy of TAGS, applying FILTER to each child-tag.
737 It is safe to modify the TAGS list, and return a permutaion of that list.
739 FILTER takes TAG as an argument, and should returns a semantic-tag.
740 It is safe for FILTER to modify the input tag and return it."
741 (when (car tags)
742 (if (semantic-tag-p (car tags))
743 (cons (semantic-tag-deep-copy-one-tag (car tags) filter)
744 (semantic--tag-deep-copy-tag-list (cdr tags) filter))
745 (cons (car tags) (semantic--tag-deep-copy-tag-list (cdr tags) filter)))))
748 ;;; Standard Tag Access
751 ;;; Common
754 (defsubst semantic-tag-modifiers (tag)
755 "Return the value of the `:typemodifiers' attribute of TAG."
756 (semantic-tag-get-attribute tag :typemodifiers))
758 (defun semantic-tag-docstring (tag &optional buffer)
759 "Return the documentation of TAG.
760 That is the value defined by the `:documentation' attribute.
761 Optional argument BUFFER indicates where to get the text from.
762 If not provided, then only the POSITION can be provided.
764 If you want to get documentation for languages that do not store
765 the documentation string in the tag itself, use
766 `semantic-documentation-for-tag' instead."
767 (let ((p (semantic-tag-get-attribute tag :documentation)))
768 (cond
769 ((stringp p) p) ;; it is the doc string.
771 ((semantic-lex-token-with-text-p p)
772 (semantic-lex-token-text p))
774 ((and (semantic-lex-token-without-text-p p)
775 buffer)
776 (with-current-buffer buffer
777 (semantic-lex-token-text (car (semantic-lex p (1+ p))))))
779 (t nil))))
781 ;;; Generic attributes for tags of any class.
783 (defsubst semantic-tag-named-parent (tag)
784 "Return the parent of TAG.
785 That is the value of the `:parent' attribute.
786 If a definition can occur outside an actual parent structure, but
787 refers to that parent by name, then the :parent attribute should be used."
788 (semantic-tag-get-attribute tag :parent))
790 ;;; Tags of class `type'
792 (defun semantic-tag-type-superclasses (tag)
793 "Return the list of superclass names of the type that TAG describes."
794 (let ((supers (semantic-tag-get-attribute tag :superclasses)))
795 (cond ((stringp supers)
796 ;; If we have a string, make it a list.
797 (list supers))
798 ((semantic-tag-p supers)
799 ;; If we have one tag, return just the name.
800 (list (semantic-tag-name supers)))
801 ((and (consp supers) (semantic-tag-p (car supers)))
802 ;; If we have a tag list, then return the names.
803 (mapcar (lambda (s) (semantic-tag-name s))
804 supers))
805 ((consp supers)
806 ;; A list of something, return it.
807 supers))))
809 (defun semantic--tag-find-parent-by-name (name supers)
810 "Find the superclass NAME in the list of SUPERS.
811 If a simple search doesn't do it, try splitting up the names
812 in SUPERS."
813 (let ((stag nil))
814 (setq stag (semantic-find-first-tag-by-name name supers))
816 (when (not stag)
817 (dolist (S supers)
818 (let* ((sname (semantic-tag-name S))
819 (splitparts (semantic-analyze-split-name sname))
820 (parts (if (stringp splitparts)
821 (list splitparts)
822 (nreverse splitparts))))
823 (when (string= name (car parts))
824 (setq stag S))
827 stag))
829 (defun semantic-tag-type-superclass-protection (tag parentstring)
830 "Return the inheritance protection in TAG from PARENTSTRING.
831 PARENTSTRING is the name of the parent being inherited.
832 The return protection is a symbol, 'public, 'protection, and 'private."
833 (let ((supers (semantic-tag-get-attribute tag :superclasses)))
834 (cond ((stringp supers)
835 'public)
836 ((semantic-tag-p supers)
837 (let ((prot (semantic-tag-get-attribute supers :protection)))
838 (or (cdr (assoc prot '(("public" . public)
839 ("protected" . protected)
840 ("private" . private))))
841 'public)))
842 ((and (consp supers) (stringp (car supers)))
843 'public)
844 ((and (consp supers) (semantic-tag-p (car supers)))
845 (let* ((stag (semantic--tag-find-parent-by-name parentstring supers))
846 (prot (when stag
847 (semantic-tag-get-attribute stag :protection))))
848 (or (cdr (assoc prot '(("public" . public)
849 ("protected" . protected)
850 ("private" . private))))
851 (when (equal prot "unspecified")
852 (if (semantic-tag-of-type-p tag "class")
853 'private
854 'public))
855 'public))))
858 (defsubst semantic-tag-type-interfaces (tag)
859 "Return the list of interfaces of the type that TAG describes."
860 ;; @todo - make this as robust as the above.
861 (semantic-tag-get-attribute tag :interfaces))
863 ;;; Tags of class `function'
865 (defsubst semantic-tag-function-arguments (tag)
866 "Return the arguments of the function that TAG describes.
867 That is the value of the `:arguments' attribute."
868 (semantic-tag-get-attribute tag :arguments))
870 (defsubst semantic-tag-function-throws (tag)
871 "Return the exceptions the function that TAG describes can throw.
872 That is the value of the `:throws' attribute."
873 (semantic-tag-get-attribute tag :throws))
875 (defsubst semantic-tag-function-parent (tag)
876 "Return the parent of the function that TAG describes.
877 That is the value of the `:parent' attribute.
878 A function has a parent if it is a method of a class, and if the
879 function does not appear in body of it's parent class."
880 (semantic-tag-named-parent tag))
882 (defsubst semantic-tag-function-destructor-p (tag)
883 "Return non-nil if TAG describes a destructor function.
884 That is the value of the `:destructor-flag' attribute."
885 (semantic-tag-get-attribute tag :destructor-flag))
887 (defsubst semantic-tag-function-constructor-p (tag)
888 "Return non-nil if TAG describes a constructor function.
889 That is the value of the `:constructor-flag' attribute."
890 (semantic-tag-get-attribute tag :constructor-flag))
892 ;;; Tags of class `variable'
894 (defsubst semantic-tag-variable-default (tag)
895 "Return the default value of the variable that TAG describes.
896 That is the value of the attribute `:default-value'."
897 (semantic-tag-get-attribute tag :default-value))
899 (defsubst semantic-tag-variable-constant-p (tag)
900 "Return non-nil if the variable that TAG describes is a constant.
901 That is the value of the attribute `:constant-flag'."
902 (semantic-tag-get-attribute tag :constant-flag))
904 ;;; Tags of class `include'
906 (defsubst semantic-tag-include-system-p (tag)
907 "Return non-nil if the include that TAG describes is a system include.
908 That is the value of the attribute `:system-flag'."
909 (semantic-tag-get-attribute tag :system-flag))
911 (define-overloadable-function semantic-tag-include-filename (tag)
912 "Return a filename representation of TAG.
913 The default action is to return the `semantic-tag-name'.
914 Some languages do not use full filenames in their include statements.
915 Override this method to translate the code represenation
916 into a filename. (A relative filename if necessary.)
918 See `semantic-dependency-tag-file' to expand an include
919 tag to a full file name.")
921 (defun semantic-tag-include-filename-default (tag)
922 "Return a filename representation of TAG.
923 Returns `semantic-tag-name'."
924 (semantic-tag-name tag))
926 ;;; Tags of class `code'
928 (defsubst semantic-tag-code-detail (tag)
929 "Return detail information from code that TAG describes.
930 That is the value of the attribute `:detail'."
931 (semantic-tag-get-attribute tag :detail))
933 ;;; Tags of class `alias'
935 (defsubst semantic-tag-new-alias (name meta-tag-class value &rest attributes)
936 "Create a semantic tag of class alias.
937 NAME is a name for this alias.
938 META-TAG-CLASS is the class of the tag this tag is an alias.
939 VALUE is the aliased definition.
940 ATTRIBUTES is a list of additional attributes belonging to this tag."
941 (apply 'semantic-tag name 'alias
942 :aliasclass meta-tag-class
943 :definition value
944 attributes))
946 (defsubst semantic-tag-alias-class (tag)
947 "Return the class of tag TAG is an alias."
948 (semantic-tag-get-attribute tag :aliasclass))
950 ;;;###autoload
951 (define-overloadable-function semantic-tag-alias-definition (tag)
952 "Return the definition TAG is an alias.
953 The returned value is a tag of the class that
954 `semantic-tag-alias-class' returns for TAG.
955 The default is to return the value of the :definition attribute.
956 Return nil if TAG is not of class 'alias."
957 (when (semantic-tag-of-class-p tag 'alias)
958 (:override
959 (semantic-tag-get-attribute tag :definition))))
961 ;;; Language Specific Tag access via overload
963 ;;;###autoload
964 (define-overloadable-function semantic-tag-components (tag)
965 "Return a list of components for TAG.
966 A Component is a part of TAG which itself may be a TAG.
967 Examples include the elements of a structure in a
968 tag of class `type, or the list of arguments to a
969 tag of class 'function."
972 (defun semantic-tag-components-default (tag)
973 "Return a list of components for TAG.
974 Perform the described task in `semantic-tag-components'."
975 (cond ((semantic-tag-of-class-p tag 'type)
976 (semantic-tag-type-members tag))
977 ((semantic-tag-of-class-p tag 'function)
978 (semantic-tag-function-arguments tag))
979 (t nil)))
981 ;;;###autoload
982 (define-overloadable-function semantic-tag-components-with-overlays (tag)
983 "Return the list of top level components belonging to TAG.
984 Children are any sub-tags which contain overlays.
986 Default behavior is to get `semantic-tag-components' in addition
987 to the components of an anonymous types (if applicable.)
989 Note for language authors:
990 If a mode defines a language tag that has tags in it with overlays
991 you should still return them with this function.
992 Ignoring this step will prevent several features from working correctly."
995 (defun semantic-tag-components-with-overlays-default (tag)
996 "Return the list of top level components belonging to TAG.
997 Children are any sub-tags which contain overlays.
998 The default action collects regular components of TAG, in addition
999 to any components beloning to an anonymous type."
1000 (let ((explicit-children (semantic-tag-components tag))
1001 (type (semantic-tag-type tag))
1002 (anon-type-children nil)
1003 (all-children nil))
1004 ;; Identify if this tag has an anonymous structure as
1005 ;; its type. This implies it may have children with overlays.
1006 (when (and type (semantic-tag-p type))
1007 (setq anon-type-children (semantic-tag-components type))
1008 ;; Add anonymous children
1009 (while anon-type-children
1010 (when (semantic-tag-with-position-p (car anon-type-children))
1011 (setq all-children (cons (car anon-type-children) all-children)))
1012 (setq anon-type-children (cdr anon-type-children))))
1013 ;; Add explicit children
1014 (while explicit-children
1015 (when (semantic-tag-with-position-p (car explicit-children))
1016 (setq all-children (cons (car explicit-children) all-children)))
1017 (setq explicit-children (cdr explicit-children)))
1018 ;; Return
1019 (nreverse all-children)))
1021 (defun semantic-tag-children-compatibility (tag &optional positiononly)
1022 "Return children of TAG.
1023 If POSITIONONLY is nil, use `semantic-tag-components'.
1024 If POSITIONONLY is non-nil, use `semantic-tag-components-with-overlays'.
1025 DO NOT use this fcn in new code. Use one of the above instead."
1026 (if positiononly
1027 (semantic-tag-components-with-overlays tag)
1028 (semantic-tag-components tag)))
1030 ;;; Tag Region
1032 ;; A Tag represents a region in a buffer. You can narrow to that tag.
1034 (defun semantic-narrow-to-tag (&optional tag)
1035 "Narrow to the region specified by the bounds of TAG.
1036 See `semantic-tag-bounds'."
1037 (interactive)
1038 (if (not tag) (setq tag (semantic-current-tag)))
1039 (narrow-to-region (semantic-tag-start tag)
1040 (semantic-tag-end tag)))
1042 (defmacro semantic-with-buffer-narrowed-to-current-tag (&rest body)
1043 "Execute BODY with the buffer narrowed to the current tag."
1044 `(save-restriction
1045 (semantic-narrow-to-tag (semantic-current-tag))
1046 ,@body))
1047 (put 'semantic-with-buffer-narrowed-to-current-tag 'lisp-indent-function 0)
1048 (add-hook 'edebug-setup-hook
1049 (lambda ()
1050 (def-edebug-spec semantic-with-buffer-narrowed-to-current-tag
1051 (def-body))))
1053 (defmacro semantic-with-buffer-narrowed-to-tag (tag &rest body)
1054 "Narrow to TAG, and execute BODY."
1055 `(save-restriction
1056 (semantic-narrow-to-tag ,tag)
1057 ,@body))
1058 (put 'semantic-with-buffer-narrowed-to-tag 'lisp-indent-function 1)
1059 (add-hook 'edebug-setup-hook
1060 (lambda ()
1061 (def-edebug-spec semantic-with-buffer-narrowed-to-tag
1062 (def-body))))
1064 ;;; Tag Hooks
1066 ;; Semantic may want to provide special hooks when specific operations
1067 ;; are about to happen on a given tag. These routines allow for hook
1068 ;; maintenance on a tag.
1070 ;; Internal global variable used to manage tag hooks. For example,
1071 ;; some implementation of `remove-hook' checks that the hook variable
1072 ;; is `default-boundp'.
1073 (defvar semantic--tag-hook-value)
1075 (defun semantic-tag-add-hook (tag hook function &optional append)
1076 "Onto TAG, add to the value of HOOK the function FUNCTION.
1077 FUNCTION is added (if necessary) at the beginning of the hook list
1078 unless the optional argument APPEND is non-nil, in which case
1079 FUNCTION is added at the end.
1080 HOOK should be a symbol, and FUNCTION may be any valid function.
1081 See also the function `add-hook'."
1082 (let ((semantic--tag-hook-value (semantic--tag-get-property tag hook)))
1083 (add-hook 'semantic--tag-hook-value function append)
1084 (semantic--tag-put-property tag hook semantic--tag-hook-value)
1085 semantic--tag-hook-value))
1087 (defun semantic-tag-remove-hook (tag hook function)
1088 "Onto TAG, remove from the value of HOOK the function FUNCTION.
1089 HOOK should be a symbol, and FUNCTION may be any valid function. If
1090 FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in
1091 the list of hooks to run in HOOK, then nothing is done.
1092 See also the function `remove-hook'."
1093 (let ((semantic--tag-hook-value (semantic--tag-get-property tag hook)))
1094 (remove-hook 'semantic--tag-hook-value function)
1095 (semantic--tag-put-property tag hook semantic--tag-hook-value)
1096 semantic--tag-hook-value))
1098 (defun semantic--tag-run-hooks (tag hook &rest args)
1099 "Run for TAG all expressions saved on the property HOOK.
1100 Each hook expression must take at least one argument, the TAG.
1101 For any given situation, additional ARGS may be passed."
1102 (let ((semantic--tag-hook-value (semantic--tag-get-property tag hook))
1103 (arglist (cons tag args)))
1104 (condition-case err
1105 ;; If a hook bombs, ignore it! Usually this is tied into
1106 ;; some sort of critical system.
1107 (apply 'run-hook-with-args 'semantic--tag-hook-value arglist)
1108 (error (message "Error: %S" err)))))
1110 ;;; Tags and Overlays
1112 ;; Overlays are used so that we can quickly identify tags from
1113 ;; buffer positions and regions using built in Emacs commands.
1116 (defsubst semantic--tag-unlink-list-from-buffer (tags)
1117 "Convert TAGS from using an overlay to using an overlay proxy.
1118 This function is for internal use only."
1119 (mapcar 'semantic--tag-unlink-from-buffer tags))
1121 (defun semantic--tag-unlink-from-buffer (tag)
1122 "Convert TAG from using an overlay to using an overlay proxy.
1123 This function is for internal use only."
1124 (when (semantic-tag-p tag)
1125 (let ((o (semantic-tag-overlay tag)))
1126 (when (semantic-overlay-p o)
1127 (semantic--tag-set-overlay
1128 tag (vector (semantic-overlay-start o)
1129 (semantic-overlay-end o)))
1130 (semantic-overlay-delete o))
1131 ;; Look for a link hook on TAG.
1132 (semantic--tag-run-hooks tag 'unlink-hook)
1133 ;; Fix the sub-tags which contain overlays.
1134 (semantic--tag-unlink-list-from-buffer
1135 (semantic-tag-components-with-overlays tag)))))
1137 (defsubst semantic--tag-link-list-to-buffer (tags)
1138 "Convert TAGS from using an overlay proxy to using an overlay.
1139 This function is for internal use only."
1140 (mapcar 'semantic--tag-link-to-buffer tags))
1142 (defun semantic--tag-link-to-buffer (tag)
1143 "Convert TAG from using an overlay proxy to using an overlay.
1144 This function is for internal use only."
1145 (when (semantic-tag-p tag)
1146 (let ((o (semantic-tag-overlay tag)))
1147 (when (and (vectorp o) (= (length o) 2))
1148 (setq o (semantic-make-overlay (aref o 0) (aref o 1)
1149 (current-buffer)))
1150 (semantic--tag-set-overlay tag o)
1151 (semantic-overlay-put o 'semantic tag)
1152 ;; Clear the :filename property
1153 (semantic--tag-put-property tag :filename nil))
1154 ;; Look for a link hook on TAG.
1155 (semantic--tag-run-hooks tag 'link-hook)
1156 ;; Fix the sub-tags which contain overlays.
1157 (semantic--tag-link-list-to-buffer
1158 (semantic-tag-components-with-overlays tag)))))
1160 (defun semantic--tag-unlink-cache-from-buffer ()
1161 "Convert all tags in the current cache to use overlay proxys.
1162 This function is for internal use only."
1163 (semantic--tag-unlink-list-from-buffer
1164 ;; @todo- use fetch-tags-fast?
1165 (semantic-fetch-tags)))
1167 (defvar semantic--buffer-cache)
1169 (defun semantic--tag-link-cache-to-buffer ()
1170 "Convert all tags in the current cache to use overlays.
1171 This function is for internal use only."
1172 (condition-case nil
1173 ;; In this unique case, we cannot call the usual toplevel fn.
1174 ;; because we don't want a reparse, we want the old overlays.
1175 (semantic--tag-link-list-to-buffer
1176 semantic--buffer-cache)
1177 ;; Recover when there is an error restoring the cache.
1178 (error (message "Error recovering tag list")
1179 (semantic-clear-toplevel-cache)
1180 nil)))
1182 ;;; Tag Cooking
1184 ;; Raw tags from a parser follow a different positional format than
1185 ;; those used in the buffer cache. Raw tags need to be cooked into
1186 ;; semantic cache friendly tags for use by the masses.
1188 (defsubst semantic--tag-expanded-p (tag)
1189 "Return non-nil if TAG is expanded.
1190 This function is for internal use only.
1191 See also the function `semantic--expand-tag'."
1192 ;; In fact a cooked tag is actually a list of cooked tags
1193 ;; because a raw tag can be expanded in several cooked ones!
1194 (when (consp tag)
1195 (while (and (semantic-tag-p (car tag))
1196 (vectorp (semantic-tag-overlay (car tag))))
1197 (setq tag (cdr tag)))
1198 (null tag)))
1200 (defvar semantic-tag-expand-function nil
1201 "Function used to expand a tag.
1202 It is passed each tag production, and must return a list of tags
1203 derived from it, or nil if it does not need to be expanded.
1205 Languages with compound definitions should use this function to expand
1206 from one compound symbol into several. For example, in C or Java the
1207 following definition is easily parsed into one tag:
1209 int a, b;
1211 This function should take this compound tag and turn it into two tags,
1212 one for A, and the other for B.")
1213 (make-variable-buffer-local 'semantic-tag-expand-function)
1215 (defun semantic--tag-expand (tag)
1216 "Convert TAG from a raw state to a cooked state, and expand it.
1217 Returns a list of cooked tags.
1219 The parser returns raw tags with positional data START END at the
1220 end of the tag data structure (a list for now). We convert it from
1221 that to a cooked state that uses an overlay proxy, that is, a vector
1222 \[START END].
1224 The raw tag is changed with side effects and maybe expanded in
1225 several derived tags when the variable `semantic-tag-expand-function'
1226 is set.
1228 This function is for internal use only."
1229 (if (semantic--tag-expanded-p tag)
1230 ;; Just return TAG if it is already expanded (by a grammar
1231 ;; semantic action), or if it isn't recognized as a valid
1232 ;; semantic tag.
1235 ;; Try to cook the tag. This code will be removed when tag will
1236 ;; be directly created with the right format.
1237 (condition-case nil
1238 (let ((ocdr (semantic--tag-overlay-cdr tag)))
1239 ;; OCDR contains the sub-list of TAG whose car is the
1240 ;; OVERLAY part of TAG. That is, a list (OVERLAY START END).
1241 ;; Convert it into an overlay proxy ([START END]).
1242 (semantic--tag-set-overlay
1243 tag (vector (nth 1 ocdr) (nth 2 ocdr)))
1244 ;; Remove START END positions at end of tag.
1245 (setcdr ocdr nil)
1246 ;; At this point (length TAG) must be 5!
1247 ;;(unless (= (length tag) 5)
1248 ;; (error "Tag expansion failed"))
1250 (error
1251 (message "A Rule must return a single tag-line list!")
1252 (debug tag)
1253 nil))
1255 ;; @todo - I think we've waited long enough. Lets find out.
1257 ;; ;; Compatibility code to be removed in future versions.
1258 ;; (unless semantic-tag-expand-function
1259 ;; ;; This line throws a byte compiler warning.
1260 ;; (setq semantic-tag-expand-function semantic-expand-nonterminal)
1261 ;; )
1263 ;; Expand based on local configuration
1264 (if semantic-tag-expand-function
1265 (or (funcall semantic-tag-expand-function tag)
1266 (list tag))
1267 (list tag))))
1269 ;; Foreign tags
1271 (defmacro semantic-foreign-tag-invalid (tag)
1272 "Signal that TAG is an invalid foreign tag."
1273 `(signal 'wrong-type-argument '(semantic-foreign-tag-p ,tag)))
1275 (defsubst semantic-foreign-tag-p (tag)
1276 "Return non-nil if TAG is a foreign tag.
1277 That is, a tag unlinked from the originating buffer, which carries the
1278 originating buffer file name, and major mode."
1279 (and (semantic-tag-p tag)
1280 (semantic--tag-get-property tag :foreign-flag)))
1282 (defsubst semantic-foreign-tag-check (tag)
1283 "Check that TAG is a valid foreign tag.
1284 Signal an error if not."
1285 (or (semantic-foreign-tag-p tag)
1286 (semantic-foreign-tag-invalid tag)))
1288 (defun semantic-foreign-tag (&optional tag)
1289 "Return a copy of TAG as a foreign tag, or nil if it can't be done.
1290 TAG defaults to the tag at point in current buffer.
1291 See also `semantic-foreign-tag-p'."
1292 (or tag (setq tag (semantic-current-tag)))
1293 (when (semantic-tag-p tag)
1294 (let ((ftag (semantic-tag-copy tag nil t))
1295 ;; Do extra work for the doc strings, since this is a
1296 ;; common use case.
1297 (doc (condition-case nil
1298 (semantic-documentation-for-tag tag)
1299 (error nil))))
1300 ;; A foreign tag must carry its originating buffer file name!
1301 (when (semantic--tag-get-property ftag :filename)
1302 (semantic--tag-put-property ftag :mode (semantic-tag-mode tag))
1303 (semantic--tag-put-property ftag :documentation doc)
1304 (semantic--tag-put-property ftag :foreign-flag t)
1305 ftag))))
1307 ;; High level obtain/insert foreign tag overloads
1309 ;;;###autoload
1310 (define-overloadable-function semantic-obtain-foreign-tag (&optional tag)
1311 "Obtain a foreign tag from TAG.
1312 TAG defaults to the tag at point in current buffer.
1313 Return the obtained foreign tag or nil if failed."
1314 (semantic-foreign-tag tag))
1316 (defun semantic-insert-foreign-tag-default (foreign-tag)
1317 "Insert FOREIGN-TAG into the current buffer.
1318 The default behavior assumes the current buffer is a language file,
1319 and attempts to insert a prototype/function call."
1320 ;; Long term goal: Have a mechanism for a tempo-like template insert
1321 ;; for the given tag.
1322 (insert (semantic-format-tag-prototype foreign-tag)))
1324 ;;;###autoload
1325 (define-overloadable-function semantic-insert-foreign-tag (foreign-tag)
1326 "Insert FOREIGN-TAG into the current buffer.
1327 Signal an error if FOREIGN-TAG is not a valid foreign tag.
1328 This function is overridable with the symbol `insert-foreign-tag'."
1329 (semantic-foreign-tag-check foreign-tag)
1330 (:override)
1331 (message (semantic-format-tag-summarize foreign-tag)))
1333 ;;; Support log modes here
1334 (define-mode-local-override semantic-insert-foreign-tag
1335 log-edit-mode (foreign-tag)
1336 "Insert foreign tags into log-edit mode."
1337 (insert (concat "(" (semantic-format-tag-name foreign-tag) "): ")))
1339 (define-mode-local-override semantic-insert-foreign-tag
1340 change-log-mode (foreign-tag)
1341 "Insert foreign tags into log-edit mode."
1342 (insert (concat "(" (semantic-format-tag-name foreign-tag) "): ")))
1345 ;;; EDEBUG display support
1347 (eval-after-load "cedet-edebug"
1348 '(progn
1349 (cedet-edebug-add-print-override
1350 '(semantic-tag-p object)
1351 '(concat "#<TAG " (semantic-format-tag-name object) ">"))
1352 (cedet-edebug-add-print-override
1353 '(and (listp object) (semantic-tag-p (car object)))
1354 '(cedet-edebug-prin1-recurse object))
1357 ;;; Compatibility
1359 (defconst semantic-token-version
1360 semantic-tag-version)
1361 (defconst semantic-token-incompatible-version
1362 semantic-tag-incompatible-version)
1364 (semantic-alias-obsolete 'semantic-token-name
1365 'semantic-tag-name)
1367 (semantic-alias-obsolete 'semantic-token-token
1368 'semantic-tag-class)
1370 (semantic-alias-obsolete 'semantic-token-extra-specs
1371 'semantic-tag-attributes)
1373 (semantic-alias-obsolete 'semantic-token-properties
1374 'semantic-tag-properties)
1376 (semantic-alias-obsolete 'semantic-token-properties-cdr
1377 'semantic--tag-properties-cdr)
1379 (semantic-alias-obsolete 'semantic-token-overlay
1380 'semantic-tag-overlay)
1382 (semantic-alias-obsolete 'semantic-token-overlay-cdr
1383 'semantic--tag-overlay-cdr)
1385 (semantic-alias-obsolete 'semantic-token-start
1386 'semantic-tag-start)
1388 (semantic-alias-obsolete 'semantic-token-end
1389 'semantic-tag-end)
1391 (semantic-alias-obsolete 'semantic-token-extent
1392 'semantic-tag-bounds)
1394 (semantic-alias-obsolete 'semantic-token-buffer
1395 'semantic-tag-buffer)
1397 (semantic-alias-obsolete 'semantic-token-put
1398 'semantic--tag-put-property)
1400 (semantic-alias-obsolete 'semantic-token-put-no-side-effect
1401 'semantic--tag-put-property-no-side-effect)
1403 (semantic-alias-obsolete 'semantic-token-get
1404 'semantic--tag-get-property)
1406 (semantic-alias-obsolete 'semantic-token-add-extra-spec
1407 'semantic-tag-put-attribute)
1409 (semantic-alias-obsolete 'semantic-token-extra-spec
1410 'semantic-tag-get-attribute)
1412 (semantic-alias-obsolete 'semantic-token-type
1413 'semantic-tag-type)
1415 (semantic-alias-obsolete 'semantic-token-modifiers
1416 'semantic-tag-modifiers)
1418 (semantic-alias-obsolete 'semantic-token-docstring
1419 'semantic-tag-docstring)
1421 (semantic-alias-obsolete 'semantic-token-type-parts
1422 'semantic-tag-type-members)
1424 (defsubst semantic-token-type-parent (tag)
1425 "Return the parent of the type that TAG describes.
1426 The return value is a list. A value of nil means no parents.
1427 The `car' of the list is either the parent class, or a list
1428 of parent classes. The `cdr' of the list is the list of
1429 interfaces, or abstract classes which are parents of TAG."
1430 (cons (semantic-tag-get-attribute tag :superclasses)
1431 (semantic-tag-type-interfaces tag)))
1432 (make-obsolete 'semantic-token-type-parent
1434 use `semantic-tag-type-superclass' \
1435 and `semantic-tag-type-interfaces' instead")
1437 (semantic-alias-obsolete 'semantic-token-type-parent-superclass
1438 'semantic-tag-type-superclasses)
1440 (semantic-alias-obsolete 'semantic-token-type-parent-implement
1441 'semantic-tag-type-interfaces)
1443 (semantic-alias-obsolete 'semantic-token-type-extra-specs
1444 'semantic-tag-attributes)
1446 (semantic-alias-obsolete 'semantic-token-type-extra-spec
1447 'semantic-tag-get-attribute)
1449 (semantic-alias-obsolete 'semantic-token-type-modifiers
1450 'semantic-tag-modifiers)
1452 (semantic-alias-obsolete 'semantic-token-function-args
1453 'semantic-tag-function-arguments)
1455 (semantic-alias-obsolete 'semantic-token-function-extra-specs
1456 'semantic-tag-attributes)
1458 (semantic-alias-obsolete 'semantic-token-function-extra-spec
1459 'semantic-tag-get-attribute)
1461 (semantic-alias-obsolete 'semantic-token-function-modifiers
1462 'semantic-tag-modifiers)
1464 (semantic-alias-obsolete 'semantic-token-function-throws
1465 'semantic-tag-function-throws)
1467 (semantic-alias-obsolete 'semantic-token-function-parent
1468 'semantic-tag-function-parent)
1470 (semantic-alias-obsolete 'semantic-token-function-destructor
1471 'semantic-tag-function-destructor-p)
1473 (semantic-alias-obsolete 'semantic-token-variable-default
1474 'semantic-tag-variable-default)
1476 (semantic-alias-obsolete 'semantic-token-variable-extra-specs
1477 'semantic-tag-attributes)
1479 (semantic-alias-obsolete 'semantic-token-variable-extra-spec
1480 'semantic-tag-get-attribute)
1482 (semantic-alias-obsolete 'semantic-token-variable-modifiers
1483 'semantic-tag-modifiers)
1485 (semantic-alias-obsolete 'semantic-token-variable-const
1486 'semantic-tag-variable-constant-p)
1488 (semantic-alias-obsolete 'semantic-token-variable-optsuffix
1489 'semantic-tag-variable-optsuffix)
1491 (semantic-alias-obsolete 'semantic-token-include-system
1492 'semantic-tag-include-system-p)
1494 (semantic-alias-obsolete 'semantic-token-p
1495 'semantic-tag-p)
1497 (semantic-alias-obsolete 'semantic-token-with-position-p
1498 'semantic-tag-with-position-p)
1500 (semantic-alias-obsolete 'semantic-tag-make-assoc-list
1501 'semantic-tag-make-plist)
1503 (semantic-alias-obsolete 'semantic-nonterminal-children
1504 'semantic-tag-children-compatibility)
1506 (semantic-alias-obsolete 'semantic-narrow-to-token
1507 'semantic-narrow-to-tag)
1509 (semantic-alias-obsolete 'semantic-with-buffer-narrowed-to-current-token
1510 'semantic-with-buffer-narrowed-to-current-tag)
1512 (semantic-alias-obsolete 'semantic-with-buffer-narrowed-to-token
1513 'semantic-with-buffer-narrowed-to-tag)
1515 (semantic-alias-obsolete 'semantic-deoverlay-token
1516 'semantic--tag-unlink-from-buffer)
1518 (semantic-alias-obsolete 'semantic-overlay-token
1519 'semantic--tag-link-to-buffer)
1521 (semantic-alias-obsolete 'semantic-deoverlay-list
1522 'semantic--tag-unlink-list-from-buffer)
1524 (semantic-alias-obsolete 'semantic-overlay-list
1525 'semantic--tag-link-list-to-buffer)
1527 (semantic-alias-obsolete 'semantic-deoverlay-cache
1528 'semantic--tag-unlink-cache-from-buffer)
1530 (semantic-alias-obsolete 'semantic-overlay-cache
1531 'semantic--tag-link-cache-to-buffer)
1533 (semantic-alias-obsolete 'semantic-cooked-token-p
1534 'semantic--tag-expanded-p)
1536 (semantic-varalias-obsolete 'semantic-expand-nonterminal
1537 'semantic-tag-expand-function)
1539 (semantic-alias-obsolete 'semantic-raw-to-cooked-token
1540 'semantic--tag-expand)
1542 ;; Lets test this out during this short transition.
1543 (semantic-alias-obsolete 'semantic-clone-tag
1544 'semantic-tag-clone)
1546 (semantic-alias-obsolete 'semantic-token
1547 'semantic-tag)
1549 (semantic-alias-obsolete 'semantic-token-new-variable
1550 'semantic-tag-new-variable)
1552 (semantic-alias-obsolete 'semantic-token-new-function
1553 'semantic-tag-new-function)
1555 (semantic-alias-obsolete 'semantic-token-new-type
1556 'semantic-tag-new-type)
1558 (semantic-alias-obsolete 'semantic-token-new-include
1559 'semantic-tag-new-include)
1561 (semantic-alias-obsolete 'semantic-token-new-package
1562 'semantic-tag-new-package)
1564 (semantic-alias-obsolete 'semantic-equivalent-tokens-p
1565 'semantic-equivalent-tag-p)
1567 (provide 'semantic/tag)
1569 ;;; semantic-tag.el ends here