lisp/cedet/semantic/analyze.el: Add local vars for autoloading.
[bpt/emacs.git] / lisp / cedet / semantic / tag.el
1 ;;; tag.el --- tag creation and access
2
3 ;;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007,
4 ;;; 2008, 2009 Free Software Foundation, Inc.
5
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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/>.
22
23 ;;; Commentary:
24 ;;
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:
27 ;;
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.
38 ;;
39 ;; II. There is also an API for tag creation. Use `semantic-tag' to create
40 ;; a new tag.
41 ;;
42 ;; III. Tag Comparison. Allows explicit or comparitive tests to see
43 ;; if two tags are the same.
44
45 ;;; History:
46 ;;
47
48 ;;; Code:
49 ;;
50
51 ;; Keep this only so long as we have obsolete fcns.
52 (require 'semantic/fw)
53 (require 'semantic/lex)
54
55 (declare-function semantic-analyze-split-name "semantic/analyze/fcn")
56 (declare-function semantic-fetch-tags "semantic")
57 (declare-function semantic-clear-toplevel-cache "semantic")
58
59 (defconst semantic-tag-version "2.0pre7"
60 "Version string of semantic tags made with this code.")
61
62 (defconst semantic-tag-incompatible-version "1.0"
63 "Version string of semantic tags which are not currently compatible.
64 These old style tags may be loaded from a file with semantic db.
65 In this case, we must flush the old tags and start over.")
66 \f
67 ;;; Primitive Tag access system:
68 ;;
69 ;; Raw tags in semantic are lists of 5 elements:
70 ;;
71 ;; (NAME CLASS ATTRIBUTES PROPERTIES OVERLAY)
72 ;;
73 ;; Where:
74 ;;
75 ;; - NAME is a string that represents the tag name.
76 ;;
77 ;; - CLASS is a symbol that represent the class of the tag (for
78 ;; example, usual classes are `type', `function', `variable',
79 ;; `include', `package', `code').
80 ;;
81 ;; - ATTRIBUTES is a public list of attributes that describes
82 ;; language data represented by the tag (for example, a variable
83 ;; can have a `:constant-flag' attribute, a function an `:arguments'
84 ;; attribute, etc.).
85 ;;
86 ;; - PROPERTIES is a private list of properties used internally.
87 ;;
88 ;; - OVERLAY represent the location of data described by the tag.
89 ;;
90
91 (defsubst semantic-tag-name (tag)
92 "Return the name of TAG.
93 For functions, variables, classes, typedefs, etc., this is the identifier
94 that is being defined. For tags without an obvious associated name, this
95 may be the statement type, e.g., this may return @code{print} for python's
96 print statement."
97 (car tag))
98
99 (defsubst semantic-tag-class (tag)
100 "Return the class of TAG.
101 That is, the symbol 'variable, 'function, 'type, or other.
102 There is no limit to the symbols that may represent the class of a tag.
103 Each parser generates tags with classes defined by it.
104
105 For functional languages, typical tag classes are:
106
107 @table @code
108 @item type
109 Data types, named map for a memory block.
110 @item function
111 A function or method, or named execution location.
112 @item variable
113 A variable, or named storage for data.
114 @item include
115 Statement that represents a file from which more tags can be found.
116 @item package
117 Statement that declairs this file's package name.
118 @item code
119 Code that has not name or binding to any other symbol, such as in a script.
120 @end table
121 "
122 (nth 1 tag))
123
124 (defsubst semantic-tag-attributes (tag)
125 "Return the list of public attributes of TAG.
126 That is a property list: (ATTRIBUTE-1 VALUE-1 ATTRIBUTE-2 VALUE-2...)."
127 (nth 2 tag))
128
129 (defsubst semantic-tag-properties (tag)
130 "Return the list of private properties of TAG.
131 That is a property list: (PROPERTY-1 VALUE-1 PROPERTY-2 VALUE-2...)."
132 (nth 3 tag))
133
134 (defsubst semantic-tag-overlay (tag)
135 "Return the OVERLAY part of TAG.
136 That is, an overlay or an unloaded buffer representation.
137 This function can also return an array of the form [ START END ].
138 This occurs for tags that are not currently linked into a buffer."
139 (nth 4 tag))
140
141 (defsubst semantic--tag-overlay-cdr (tag)
142 "Return the cons cell whose car is the OVERLAY part of TAG.
143 That function is for internal use only."
144 (nthcdr 4 tag))
145
146 (defsubst semantic--tag-set-overlay (tag overlay)
147 "Set the overlay part of TAG with OVERLAY.
148 That function is for internal use only."
149 (setcar (semantic--tag-overlay-cdr tag) overlay))
150
151 (defsubst semantic-tag-start (tag)
152 "Return the start location of TAG."
153 (let ((o (semantic-tag-overlay tag)))
154 (if (semantic-overlay-p o)
155 (semantic-overlay-start o)
156 (aref o 0))))
157
158 (defsubst semantic-tag-end (tag)
159 "Return the end location of TAG."
160 (let ((o (semantic-tag-overlay tag)))
161 (if (semantic-overlay-p o)
162 (semantic-overlay-end o)
163 (aref o 1))))
164
165 (defsubst semantic-tag-bounds (tag)
166 "Return the location (START END) of data TAG describes."
167 (list (semantic-tag-start tag)
168 (semantic-tag-end tag)))
169
170 (defun semantic-tag-set-bounds (tag start end)
171 "In TAG, set the START and END location of data it describes."
172 (let ((o (semantic-tag-overlay tag)))
173 (if (semantic-overlay-p o)
174 (semantic-overlay-move o start end)
175 (semantic--tag-set-overlay tag (vector start end)))))
176
177 (defun semantic-tag-in-buffer-p (tag)
178 "Return the buffer TAG resides in IFF tag is already in a buffer.
179 If a tag is not in a buffer, return nil."
180 (let ((o (semantic-tag-overlay tag)))
181 ;; TAG is currently linked to a buffer, return it.
182 (when (and (semantic-overlay-p o)
183 (semantic-overlay-live-p o))
184 (semantic-overlay-buffer o))))
185
186 (defsubst semantic--tag-get-property (tag property)
187 "From TAG, extract the value of PROPERTY.
188 Return the value found, or nil if PROPERTY is not one of the
189 properties of TAG.
190 That function is for internal use only."
191 (plist-get (semantic-tag-properties tag) property))
192
193 (defun semantic-tag-buffer (tag)
194 "Return the buffer TAG resides in.
195 If TAG has an originating file, read that file into a (maybe new)
196 buffer, and return it.
197 Return nil if there is no buffer for this tag."
198 (let ((buff (semantic-tag-in-buffer-p tag)))
199 (if buff
200 buff
201 ;; TAG has an originating file, read that file into a buffer, and
202 ;; return it.
203 (if (semantic--tag-get-property tag :filename)
204 (find-file-noselect (semantic--tag-get-property tag :filename))
205 ;; TAG is not in Emacs right now, no buffer is available.
206 ))))
207
208 (defun semantic-tag-mode (&optional tag)
209 "Return the major mode active for TAG.
210 TAG defaults to the tag at point in current buffer.
211 If TAG has a :mode property return it.
212 If point is inside TAG bounds, return the major mode active at point.
213 Return the major mode active at beginning of TAG otherwise.
214 See also the function `semantic-ctxt-current-mode'."
215 (or tag (setq tag (semantic-current-tag)))
216 (or (semantic--tag-get-property tag :mode)
217 (let ((buffer (semantic-tag-buffer tag))
218 (start (semantic-tag-start tag))
219 (end (semantic-tag-end tag)))
220 (save-excursion
221 (and buffer (set-buffer buffer))
222 ;; Unless point is inside TAG bounds, move it to the
223 ;; beginning of TAG.
224 (or (and (>= (point) start) (< (point) end))
225 (goto-char start))
226 (semantic-ctxt-current-mode)))))
227
228 (defsubst semantic--tag-attributes-cdr (tag)
229 "Return the cons cell whose car is the ATTRIBUTES part of TAG.
230 That function is for internal use only."
231 (nthcdr 2 tag))
232
233 (defsubst semantic-tag-put-attribute (tag attribute value)
234 "Change value in TAG of ATTRIBUTE to VALUE.
235 If ATTRIBUTE already exists, its value is set to VALUE, otherwise the
236 new ATTRIBUTE VALUE pair is added.
237 Return TAG.
238 Use this function in a parser when not all attributes are known at the
239 same time."
240 (let* ((plist-cdr (semantic--tag-attributes-cdr tag)))
241 (when (consp plist-cdr)
242 (setcar plist-cdr
243 (semantic-tag-make-plist
244 (plist-put (car plist-cdr) attribute value))))
245 tag))
246
247 (defun semantic-tag-put-attribute-no-side-effect (tag attribute value)
248 "Change value in TAG of ATTRIBUTE to VALUE without side effects.
249 All cons cells in the attribute list are replicated so that there
250 are no side effects if TAG is in shared lists.
251 If ATTRIBUTE already exists, its value is set to VALUE, otherwise the
252 new ATTRIBUTE VALUE pair is added.
253 Return TAG."
254 (let* ((plist-cdr (semantic--tag-attributes-cdr tag)))
255 (when (consp plist-cdr)
256 (setcar plist-cdr
257 (semantic-tag-make-plist
258 (plist-put (copy-sequence (car plist-cdr))
259 attribute value))))
260 tag))
261
262 (defsubst semantic-tag-get-attribute (tag attribute)
263 "From TAG, return the value of ATTRIBUTE.
264 ATTRIBUTE is a symbol whose specification value to get.
265 Return the value found, or nil if ATTRIBUTE is not one of the
266 attributes of TAG."
267 (plist-get (semantic-tag-attributes tag) attribute))
268
269 ;; These functions are for internal use only!
270 (defsubst semantic--tag-properties-cdr (tag)
271 "Return the cons cell whose car is the PROPERTIES part of TAG.
272 That function is for internal use only."
273 (nthcdr 3 tag))
274
275 (defun semantic--tag-put-property (tag property value)
276 "Change value in TAG of PROPERTY to VALUE.
277 If PROPERTY already exists, its value is set to VALUE, otherwise the
278 new PROPERTY VALUE pair is added.
279 Return TAG.
280 That function is for internal use only."
281 (let* ((plist-cdr (semantic--tag-properties-cdr tag)))
282 (when (consp plist-cdr)
283 (setcar plist-cdr
284 (semantic-tag-make-plist
285 (plist-put (car plist-cdr) property value))))
286 tag))
287
288 (defun semantic--tag-put-property-no-side-effect (tag property value)
289 "Change value in TAG of PROPERTY to VALUE without side effects.
290 All cons cells in the property list are replicated so that there
291 are no side effects if TAG is in shared lists.
292 If PROPERTY already exists, its value is set to VALUE, otherwise the
293 new PROPERTY VALUE pair is added.
294 Return TAG.
295 That function is for internal use only."
296 (let* ((plist-cdr (semantic--tag-properties-cdr tag)))
297 (when (consp plist-cdr)
298 (setcar plist-cdr
299 (semantic-tag-make-plist
300 (plist-put (copy-sequence (car plist-cdr))
301 property value))))
302 tag))
303
304 (defun semantic-tag-file-name (tag)
305 "Return the name of the file from which TAG originated.
306 Return nil if that information can't be obtained.
307 If TAG is from a loaded buffer, then that buffer's filename is used.
308 If TAG is unlinked, but has a :filename property, then that is used."
309 (let ((buffer (semantic-tag-in-buffer-p tag)))
310 (if buffer
311 (buffer-file-name buffer)
312 (semantic--tag-get-property tag :filename))))
313 \f
314 ;;; Tag tests and comparisons.
315 (defsubst semantic-tag-p (tag)
316 "Return non-nil if TAG is most likely a semantic tag."
317 (condition-case nil
318 (and (consp tag)
319 (stringp (car tag)) ; NAME
320 (symbolp (nth 1 tag)) (nth 1 tag) ; TAG-CLASS
321 (listp (nth 2 tag)) ; ATTRIBUTES
322 (listp (nth 3 tag)) ; PROPERTIES
323 )
324 ;; If an error occurs, then it most certainly is not a tag.
325 (error nil)))
326
327 (defsubst semantic-tag-of-class-p (tag class)
328 "Return non-nil if class of TAG is CLASS."
329 (eq (semantic-tag-class tag) class))
330
331 (defsubst semantic-tag-type-members (tag)
332 "Return the members of the type that TAG describes.
333 That is the value of the `:members' attribute."
334 (semantic-tag-get-attribute tag :members))
335
336 (defun semantic-tag-with-position-p (tag)
337 "Return non-nil if TAG has positional information."
338 (and (semantic-tag-p tag)
339 (let ((o (semantic-tag-overlay tag)))
340 (or (and (semantic-overlay-p o)
341 (semantic-overlay-live-p o))
342 (arrayp o)))))
343
344 (defun semantic-equivalent-tag-p (tag1 tag2)
345 "Compare TAG1 and TAG2 and return non-nil if they are equivalent.
346 Use `equal' on elements the name, class, and position.
347 Use this function if tags are being copied and regrouped to test
348 for if two tags represent the same thing, but may be constructed
349 of different cons cells."
350 (and (equal (semantic-tag-name tag1) (semantic-tag-name tag2))
351 (semantic-tag-of-class-p tag1 (semantic-tag-class tag2))
352 (or (and (not (semantic-tag-overlay tag1))
353 (not (semantic-tag-overlay tag2)))
354 (and (semantic-tag-overlay tag1)
355 (semantic-tag-overlay tag2)
356 (equal (semantic-tag-bounds tag1)
357 (semantic-tag-bounds tag2))))))
358
359 (defsubst semantic-tag-type (tag)
360 "Return the value of the `:type' attribute of TAG.
361 For a function it would be the data type of the return value.
362 For a variable, it is the storage type of that variable.
363 For a data type, the type is the style of datatype, such as
364 struct or union."
365 (semantic-tag-get-attribute tag :type))
366
367 (defun semantic-tag-similar-p (tag1 tag2 &rest ignorable-attributes)
368 "Test to see if TAG1 and TAG2 are similar.
369 Two tags are similar if their name, datatype, and various attributes
370 are the same.
371
372 Similar tags that have sub-tags such as arg lists or type members,
373 are similar w/out checking the sub-list of tags.
374 Optional argument IGNORABLE-ATTRIBUTES are attributes to ignore while comparing similarity."
375 (let* ((A1 (and (equal (semantic-tag-name tag1) (semantic-tag-name tag2))
376 (semantic-tag-of-class-p tag1 (semantic-tag-class tag2))
377 (semantic-tag-of-type-p tag1 (semantic-tag-type tag2))))
378 (attr1 (semantic-tag-attributes tag1))
379 (A2 (= (length attr1) (length (semantic-tag-attributes tag2))))
380 (A3 t)
381 )
382 (when (and (not A2) ignorable-attributes)
383 (setq A2 t))
384 (while (and A2 attr1 A3)
385 (let ((a (car attr1))
386 (v (car (cdr attr1))))
387
388 (cond ((or (eq a :type) ;; already tested above.
389 (memq a ignorable-attributes)) ;; Ignore them...
390 nil)
391
392 ;; Don't test sublists of tags
393 ((and (listp v) (semantic-tag-p (car v)))
394 nil)
395
396 ;; The attributes are not the same?
397 ((not (equal v (semantic-tag-get-attribute tag2 a)))
398 (setq A3 nil))
399 (t
400 nil))
401 )
402 (setq attr1 (cdr (cdr attr1))))
403
404 (and A1 A2 A3)
405 ))
406
407 (defun semantic-tag-similar-with-subtags-p (tag1 tag2 &rest ignorable-attributes)
408 "Test to see if TAG1 and TAG2 are similar.
409 Uses `semantic-tag-similar-p' but also recurses through sub-tags, such
410 as argument lists and type members.
411 Optional argument IGNORABLE-ATTRIBUTES is passed down to
412 `semantic-tag-similar-p'."
413 (let ((C1 (semantic-tag-components tag1))
414 (C2 (semantic-tag-components tag2))
415 )
416 (if (or (/= (length C1) (length C2))
417 (not (semantic-tag-similar-p tag1 tag2 ignorable-attributes))
418 )
419 ;; Basic test fails.
420 nil
421 ;; Else, check component lists.
422 (catch 'component-dissimilar
423 (while C1
424
425 (if (not (semantic-tag-similar-with-subtags-p
426 (car C1) (car C2) ignorable-attributes))
427 (throw 'component-dissimilar nil))
428
429 (setq C1 (cdr C1))
430 (setq C2 (cdr C2))
431 )
432 ;; If we made it this far, we are ok.
433 t) )))
434
435
436 (defun semantic-tag-of-type-p (tag type)
437 "Compare TAG's type against TYPE. Non nil if equivalent.
438 TYPE can be a string, or a tag of class 'type.
439 This can be complex since some tags might have a :type that is a tag,
440 while other tags might just have a string. This function will also be
441 return true of TAG's type is compared directly to the declaration of a
442 data type."
443 (let* ((tagtype (semantic-tag-type tag))
444 (tagtypestring (cond ((stringp tagtype)
445 tagtype)
446 ((and (semantic-tag-p tagtype)
447 (semantic-tag-of-class-p tagtype 'type))
448 (semantic-tag-name tagtype))
449 (t "")))
450 (typestring (cond ((stringp type)
451 type)
452 ((and (semantic-tag-p type)
453 (semantic-tag-of-class-p type 'type))
454 (semantic-tag-name type))
455 (t "")))
456 )
457 (and
458 tagtypestring
459 (or
460 ;; Matching strings (input type is string)
461 (and (stringp type)
462 (string= tagtypestring type))
463 ;; Matching strings (tag type is string)
464 (and (stringp tagtype)
465 (string= tagtype typestring))
466 ;; Matching tokens, and the type of the type is the same.
467 (and (string= tagtypestring typestring)
468 (if (and (semantic-tag-type tagtype) (semantic-tag-type type))
469 (equal (semantic-tag-type tagtype) (semantic-tag-type type))
470 t))
471 ))
472 ))
473
474 (defun semantic-tag-type-compound-p (tag)
475 "Return non-nil the type of TAG is compound.
476 Compound implies a structure or similar data type.
477 Returns the list of tag members if it is compound."
478 (let* ((tagtype (semantic-tag-type tag))
479 )
480 (when (and (semantic-tag-p tagtype)
481 (semantic-tag-of-class-p tagtype 'type))
482 ;; We have the potential of this being a nifty compound type.
483 (semantic-tag-type-members tagtype)
484 )))
485
486 (defun semantic-tag-faux-p (tag)
487 "Return non-nil if TAG is a FAUX tag.
488 FAUX tags are created to represent a construct that is
489 not known to exist in the code.
490
491 Example: When the class browser sees methods to a class, but
492 cannot find the class, it will create a faux tag to represent the
493 class to store those methods."
494 (semantic--tag-get-property tag :faux-flag))
495 \f
496 ;;; Tag creation
497 ;;
498
499 ;; Is this function still necessary?
500 (defun semantic-tag-make-plist (args)
501 "Create a property list with ARGS.
502 Args is a property list of the form (KEY1 VALUE1 ... KEYN VALUEN).
503 Where KEY is a symbol, and VALUE is the value for that symbol.
504 The return value will be a new property list, with these KEY/VALUE
505 pairs eliminated:
506
507 - KEY associated to nil VALUE.
508 - KEY associated to an empty string VALUE.
509 - KEY associated to a zero VALUE."
510 (let (plist key val)
511 (while args
512 (setq key (car args)
513 val (nth 1 args)
514 args (nthcdr 2 args))
515 (or (member val '("" nil))
516 (and (numberp val) (zerop val))
517 (setq plist (cons key (cons val plist)))))
518 ;; It is not useful to reverse the new plist.
519 plist))
520
521 (defsubst semantic-tag (name class &rest attributes)
522 "Create a generic semantic tag.
523 NAME is a string representing the name of this tag.
524 CLASS is the symbol that represents the class of tag this is,
525 such as 'variable, or 'function.
526 ATTRIBUTES is a list of additional attributes belonging to this tag."
527 (list name class (semantic-tag-make-plist attributes) nil nil))
528
529 (defsubst semantic-tag-new-variable (name type &optional default-value &rest attributes)
530 "Create a semantic tag of class 'variable.
531 NAME is the name of this variable.
532 TYPE is a string or semantic tag representing the type of this variable.
533 Optional DEFAULT-VALUE is a string representing the default value of this variable.
534 ATTRIBUTES is a list of additional attributes belonging to this tag."
535 (apply 'semantic-tag name 'variable
536 :type type
537 :default-value default-value
538 attributes))
539
540 (defsubst semantic-tag-new-function (name type arg-list &rest attributes)
541 "Create a semantic tag of class 'function.
542 NAME is the name of this function.
543 TYPE is a string or semantic tag representing the type of this function.
544 ARG-LIST is a list of strings or semantic tags representing the
545 arguments of this function.
546 ATTRIBUTES is a list of additional attributes belonging to this tag."
547 (apply 'semantic-tag name 'function
548 :type type
549 :arguments arg-list
550 attributes))
551
552 (defsubst semantic-tag-new-type (name type members parents &rest attributes)
553 "Create a semantic tag of class 'type.
554 NAME is the name of this type.
555 TYPE is a string or semantic tag representing the type of this type.
556 MEMBERS is a list of strings or semantic tags representing the
557 elements that make up this type if it is a composite type.
558 PARENTS is a cons cell. (EXPLICIT-PARENTS . INTERFACE-PARENTS)
559 EXPLICIT-PARENTS can be a single string (Just one parent) or a
560 list of parents (in a multiple inheritance situation). It can also
561 be nil.
562 INTERFACE-PARENTS is a list of strings representing the names of
563 all INTERFACES, or abstract classes inherited from. It can also be
564 nil.
565 This slot can be interesting because the form:
566 ( nil \"string\")
567 is a valid parent where there is no explicit parent, and only an
568 interface.
569 ATTRIBUTES is a list of additional attributes belonging to this tag."
570 (apply 'semantic-tag name 'type
571 :type type
572 :members members
573 :superclasses (car parents)
574 :interfaces (cdr parents)
575 attributes))
576
577 (defsubst semantic-tag-new-include (name system-flag &rest attributes)
578 "Create a semantic tag of class 'include.
579 NAME is the name of this include.
580 SYSTEM-FLAG represents that we were able to identify this include as belonging
581 to the system, as opposed to belonging to the local project.
582 ATTRIBUTES is a list of additional attributes belonging to this tag."
583 (apply 'semantic-tag name 'include
584 :system-flag system-flag
585 attributes))
586
587 (defsubst semantic-tag-new-package (name detail &rest attributes)
588 "Create a semantic tag of class 'package.
589 NAME is the name of this package.
590 DETAIL is extra information about this package, such as a location where
591 it can be found.
592 ATTRIBUTES is a list of additional attributes belonging to this tag."
593 (apply 'semantic-tag name 'package
594 :detail detail
595 attributes))
596
597 (defsubst semantic-tag-new-code (name detail &rest attributes)
598 "Create a semantic tag of class 'code.
599 NAME is a name for this code.
600 DETAIL is extra information about the code.
601 ATTRIBUTES is a list of additional attributes belonging to this tag."
602 (apply 'semantic-tag name 'code
603 :detail detail
604 attributes))
605
606 (defsubst semantic-tag-set-faux (tag)
607 "Set TAG to be a new FAUX tag.
608 FAUX tags represent constructs not found in the source code.
609 You can identify a faux tag with `semantic-tag-faux-p'"
610 (semantic--tag-put-property tag :faux-flag t))
611
612 (defsubst semantic-tag-set-name (tag name)
613 "Set TAG name to NAME."
614 (setcar tag name))
615
616 ;;; Copying and cloning tags.
617 ;;
618 (defsubst semantic-tag-clone (tag &optional name)
619 "Clone TAG, creating a new TAG.
620 If optional argument NAME is not nil it specifies a new name for the
621 cloned tag."
622 ;; Right now, TAG is a list.
623 (list (or name (semantic-tag-name tag))
624 (semantic-tag-class tag)
625 (copy-sequence (semantic-tag-attributes tag))
626 (copy-sequence (semantic-tag-properties tag))
627 (semantic-tag-overlay tag)))
628
629 (defun semantic-tag-copy (tag &optional name keep-file)
630 "Return a copy of TAG unlinked from the originating buffer.
631 If optional argument NAME is non-nil it specifies a new name for the
632 copied tag.
633 If optional argument KEEP-FILE is non-nil, and TAG was linked to a
634 buffer, the originating buffer file name is kept in the `:filename'
635 property of the copied tag.
636 If KEEP-FILE is a string, and the orginating buffer is NOT available,
637 then KEEP-FILE is stored on the `:filename' property.
638 This runs the tag hook `unlink-copy-hook`."
639 ;; Right now, TAG is a list.
640 (let ((copy (semantic-tag-clone tag name)))
641
642 ;; Keep the filename if needed.
643 (when keep-file
644 (semantic--tag-put-property
645 copy :filename (or (semantic-tag-file-name copy)
646 (and (stringp keep-file)
647 keep-file)
648 )))
649
650 (when (semantic-tag-with-position-p tag)
651 ;; Convert the overlay to a vector, effectively 'unlinking' the tag.
652 (semantic--tag-set-overlay
653 copy (vector (semantic-tag-start copy) (semantic-tag-end copy)))
654
655 ;; Force the children to be copied also.
656 ;;(let ((chil (semantic--tag-copy-list
657 ;; (semantic-tag-components-with-overlays tag)
658 ;; keep-file)))
659 ;;;; Put the list into TAG.
660 ;;)
661
662 ;; Call the unlink-copy hook. This should tell tools that
663 ;; this tag is not part of any buffer.
664 (when (semantic-overlay-p (semantic-tag-overlay tag))
665 (semantic--tag-run-hooks copy 'unlink-copy-hook))
666 )
667 copy))
668
669 ;;(defun semantic--tag-copy-list (tags &optional keep-file)
670 ;; "Make copies of TAGS and return the list of TAGS."
671 ;; (let ((out nil))
672 ;; (dolist (tag tags out)
673 ;; (setq out (cons (semantic-tag-copy tag nil keep-file)
674 ;; out))
675 ;; )))
676
677 (defun semantic--tag-copy-properties (tag1 tag2)
678 "Copy private properties from TAG1 to TAG2.
679 Return TAG2.
680 This function is for internal use only."
681 (let ((plist (semantic-tag-properties tag1)))
682 (while plist
683 (semantic--tag-put-property tag2 (car plist) (nth 1 plist))
684 (setq plist (nthcdr 2 plist)))
685 tag2))
686
687 ;;; DEEP COPIES
688 ;;
689 (defun semantic-tag-deep-copy-one-tag (tag &optional filter)
690 "Make a deep copy of TAG, applying FILTER to each child-tag.
691 Properties and overlay info are not copied.
692 FILTER takes TAG as an argument, and should returns a semantic-tag.
693 It is safe for FILTER to modify the input tag and return it."
694 (when (not filter) (setq filter 'identity))
695 (when (not (semantic-tag-p tag))
696 (signal 'wrong-type-argument (list tag 'semantic-tag-p)))
697 (funcall filter (list (semantic-tag-name tag)
698 (semantic-tag-class tag)
699 (semantic--tag-deep-copy-attributes
700 (semantic-tag-attributes tag) filter)
701 nil
702 nil)))
703
704 (defun semantic--tag-deep-copy-attributes (attrs &optional filter)
705 "Make a deep copy of ATTRS, applying FILTER to each child-tag.
706
707 It is safe to modify ATTR, and return a permutaion of that list.
708
709 FILTER takes TAG as an argument, and should returns a semantic-tag.
710 It is safe for FILTER to modify the input tag and return it."
711 (when (car attrs)
712 (when (not (symbolp (car attrs))) (error "Bad Attribute List in tag"))
713 (cons (car attrs)
714 (cons (semantic--tag-deep-copy-value (nth 1 attrs) filter)
715 (semantic--tag-deep-copy-attributes (nthcdr 2 attrs) filter)))))
716
717 (defun semantic--tag-deep-copy-value (value &optional filter)
718 "Make a deep copy of VALUE, applying FILTER to each child-tag.
719
720 It is safe to modify VALUE, and return a permutaion of that list.
721
722 FILTER takes TAG as an argument, and should returns a semantic-tag.
723 It is safe for FILTER to modify the input tag and return it."
724 (cond
725 ;; Another tag.
726 ((semantic-tag-p value)
727 (semantic-tag-deep-copy-one-tag value filter))
728
729 ;; A list of more tags
730 ((and (listp value) (semantic-tag-p (car value)))
731 (semantic--tag-deep-copy-tag-list value filter))
732
733 ;; Some arbitrary data.
734 (t value)))
735
736 (defun semantic--tag-deep-copy-tag-list (tags &optional filter)
737 "Make a deep copy of TAGS, applying FILTER to each child-tag.
738
739 It is safe to modify the TAGS list, and return a permutaion of that list.
740
741 FILTER takes TAG as an argument, and should returns a semantic-tag.
742 It is safe for FILTER to modify the input tag and return it."
743 (when (car tags)
744 (if (semantic-tag-p (car tags))
745 (cons (semantic-tag-deep-copy-one-tag (car tags) filter)
746 (semantic--tag-deep-copy-tag-list (cdr tags) filter))
747 (cons (car tags) (semantic--tag-deep-copy-tag-list (cdr tags) filter)))))
748
749 \f
750 ;;; Standard Tag Access
751 ;;
752
753 ;;; Common
754 ;;
755
756 (defsubst semantic-tag-modifiers (tag)
757 "Return the value of the `:typemodifiers' attribute of TAG."
758 (semantic-tag-get-attribute tag :typemodifiers))
759
760 (defun semantic-tag-docstring (tag &optional buffer)
761 "Return the documentation of TAG.
762 That is the value defined by the `:documentation' attribute.
763 Optional argument BUFFER indicates where to get the text from.
764 If not provided, then only the POSITION can be provided.
765
766 If you want to get documentation for languages that do not store
767 the documentation string in the tag itself, use
768 `semantic-documentation-for-tag' instead."
769 (let ((p (semantic-tag-get-attribute tag :documentation)))
770 (cond
771 ((stringp p) p) ;; it is the doc string.
772
773 ((semantic-lex-token-with-text-p p)
774 (semantic-lex-token-text p))
775
776 ((and (semantic-lex-token-without-text-p p)
777 buffer)
778 (with-current-buffer buffer
779 (semantic-lex-token-text (car (semantic-lex p (1+ p))))))
780
781 (t nil))))
782
783 ;;; Generic attributes for tags of any class.
784 ;;
785 (defsubst semantic-tag-named-parent (tag)
786 "Return the parent of TAG.
787 That is the value of the `:parent' attribute.
788 If a definition can occur outside an actual parent structure, but
789 refers to that parent by name, then the :parent attribute should be used."
790 (semantic-tag-get-attribute tag :parent))
791
792 ;;; Tags of class `type'
793
794 (defun semantic-tag-type-superclasses (tag)
795 "Return the list of superclass names of the type that TAG describes."
796 (let ((supers (semantic-tag-get-attribute tag :superclasses)))
797 (cond ((stringp supers)
798 ;; If we have a string, make it a list.
799 (list supers))
800 ((semantic-tag-p supers)
801 ;; If we have one tag, return just the name.
802 (list (semantic-tag-name supers)))
803 ((and (consp supers) (semantic-tag-p (car supers)))
804 ;; If we have a tag list, then return the names.
805 (mapcar (lambda (s) (semantic-tag-name s))
806 supers))
807 ((consp supers)
808 ;; A list of something, return it.
809 supers))))
810
811 (defun semantic--tag-find-parent-by-name (name supers)
812 "Find the superclass NAME in the list of SUPERS.
813 If a simple search doesn't do it, try splitting up the names
814 in SUPERS."
815 (let ((stag nil))
816 (setq stag (semantic-find-first-tag-by-name name supers))
817 (when (not stag)
818 (require 'semantic/analyze/fcn)
819 (dolist (S supers)
820 (let* ((sname (semantic-tag-name S))
821 (splitparts (semantic-analyze-split-name sname))
822 (parts (if (stringp splitparts)
823 (list splitparts)
824 (nreverse splitparts))))
825 (when (string= name (car parts))
826 (setq stag S))
827 )))
828
829 stag))
830
831 (defun semantic-tag-type-superclass-protection (tag parentstring)
832 "Return the inheritance protection in TAG from PARENTSTRING.
833 PARENTSTRING is the name of the parent being inherited.
834 The return protection is a symbol, 'public, 'protection, and 'private."
835 (let ((supers (semantic-tag-get-attribute tag :superclasses)))
836 (cond ((stringp supers)
837 'public)
838 ((semantic-tag-p supers)
839 (let ((prot (semantic-tag-get-attribute supers :protection)))
840 (or (cdr (assoc prot '(("public" . public)
841 ("protected" . protected)
842 ("private" . private))))
843 'public)))
844 ((and (consp supers) (stringp (car supers)))
845 'public)
846 ((and (consp supers) (semantic-tag-p (car supers)))
847 (let* ((stag (semantic--tag-find-parent-by-name parentstring supers))
848 (prot (when stag
849 (semantic-tag-get-attribute stag :protection))))
850 (or (cdr (assoc prot '(("public" . public)
851 ("protected" . protected)
852 ("private" . private))))
853 (when (equal prot "unspecified")
854 (if (semantic-tag-of-type-p tag "class")
855 'private
856 'public))
857 'public))))
858 ))
859
860 (defsubst semantic-tag-type-interfaces (tag)
861 "Return the list of interfaces of the type that TAG describes."
862 ;; @todo - make this as robust as the above.
863 (semantic-tag-get-attribute tag :interfaces))
864
865 ;;; Tags of class `function'
866 ;;
867 (defsubst semantic-tag-function-arguments (tag)
868 "Return the arguments of the function that TAG describes.
869 That is the value of the `:arguments' attribute."
870 (semantic-tag-get-attribute tag :arguments))
871
872 (defsubst semantic-tag-function-throws (tag)
873 "Return the exceptions the function that TAG describes can throw.
874 That is the value of the `:throws' attribute."
875 (semantic-tag-get-attribute tag :throws))
876
877 (defsubst semantic-tag-function-parent (tag)
878 "Return the parent of the function that TAG describes.
879 That is the value of the `:parent' attribute.
880 A function has a parent if it is a method of a class, and if the
881 function does not appear in body of it's parent class."
882 (semantic-tag-named-parent tag))
883
884 (defsubst semantic-tag-function-destructor-p (tag)
885 "Return non-nil if TAG describes a destructor function.
886 That is the value of the `:destructor-flag' attribute."
887 (semantic-tag-get-attribute tag :destructor-flag))
888
889 (defsubst semantic-tag-function-constructor-p (tag)
890 "Return non-nil if TAG describes a constructor function.
891 That is the value of the `:constructor-flag' attribute."
892 (semantic-tag-get-attribute tag :constructor-flag))
893
894 ;;; Tags of class `variable'
895 ;;
896 (defsubst semantic-tag-variable-default (tag)
897 "Return the default value of the variable that TAG describes.
898 That is the value of the attribute `:default-value'."
899 (semantic-tag-get-attribute tag :default-value))
900
901 (defsubst semantic-tag-variable-constant-p (tag)
902 "Return non-nil if the variable that TAG describes is a constant.
903 That is the value of the attribute `:constant-flag'."
904 (semantic-tag-get-attribute tag :constant-flag))
905
906 ;;; Tags of class `include'
907 ;;
908 (defsubst semantic-tag-include-system-p (tag)
909 "Return non-nil if the include that TAG describes is a system include.
910 That is the value of the attribute `:system-flag'."
911 (semantic-tag-get-attribute tag :system-flag))
912
913 (define-overloadable-function semantic-tag-include-filename (tag)
914 "Return a filename representation of TAG.
915 The default action is to return the `semantic-tag-name'.
916 Some languages do not use full filenames in their include statements.
917 Override this method to translate the code represenation
918 into a filename. (A relative filename if necessary.)
919
920 See `semantic-dependency-tag-file' to expand an include
921 tag to a full file name.")
922
923 (defun semantic-tag-include-filename-default (tag)
924 "Return a filename representation of TAG.
925 Returns `semantic-tag-name'."
926 (semantic-tag-name tag))
927
928 ;;; Tags of class `code'
929 ;;
930 (defsubst semantic-tag-code-detail (tag)
931 "Return detail information from code that TAG describes.
932 That is the value of the attribute `:detail'."
933 (semantic-tag-get-attribute tag :detail))
934
935 ;;; Tags of class `alias'
936 ;;
937 (defsubst semantic-tag-new-alias (name meta-tag-class value &rest attributes)
938 "Create a semantic tag of class alias.
939 NAME is a name for this alias.
940 META-TAG-CLASS is the class of the tag this tag is an alias.
941 VALUE is the aliased definition.
942 ATTRIBUTES is a list of additional attributes belonging to this tag."
943 (apply 'semantic-tag name 'alias
944 :aliasclass meta-tag-class
945 :definition value
946 attributes))
947
948 (defsubst semantic-tag-alias-class (tag)
949 "Return the class of tag TAG is an alias."
950 (semantic-tag-get-attribute tag :aliasclass))
951
952 (define-overloadable-function semantic-tag-alias-definition (tag)
953 "Return the definition TAG is an alias.
954 The returned value is a tag of the class that
955 `semantic-tag-alias-class' returns for TAG.
956 The default is to return the value of the :definition attribute.
957 Return nil if TAG is not of class 'alias."
958 (when (semantic-tag-of-class-p tag 'alias)
959 (:override
960 (semantic-tag-get-attribute tag :definition))))
961
962 ;;; Language Specific Tag access via overload
963 ;;
964 ;;;###autoload
965 (define-overloadable-function semantic-tag-components (tag)
966 "Return a list of components for TAG.
967 A Component is a part of TAG which itself may be a TAG.
968 Examples include the elements of a structure in a
969 tag of class `type, or the list of arguments to a
970 tag of class 'function."
971 )
972
973 (defun semantic-tag-components-default (tag)
974 "Return a list of components for TAG.
975 Perform the described task in `semantic-tag-components'."
976 (cond ((semantic-tag-of-class-p tag 'type)
977 (semantic-tag-type-members tag))
978 ((semantic-tag-of-class-p tag 'function)
979 (semantic-tag-function-arguments tag))
980 (t nil)))
981
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.
985
986 Default behavior is to get `semantic-tag-components' in addition
987 to the components of an anonymous types (if applicable.)
988
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."
993 )
994
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)))
1020
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)))
1029 \f
1030 ;;; Tag Region
1031 ;;
1032 ;; A Tag represents a region in a buffer. You can narrow to that tag.
1033 ;;
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)))
1041
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))))
1052
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))))
1063 \f
1064 ;;; Tag Hooks
1065 ;;
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.
1069
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)
1074
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))
1086
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))
1097
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)))))
1109 \f
1110 ;;; Tags and Overlays
1111 ;;
1112 ;; Overlays are used so that we can quickly identify tags from
1113 ;; buffer positions and regions using built in Emacs commands.
1114 ;;
1115
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))
1120
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)))))
1136
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))
1141
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)))))
1159
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 (require 'semantic)
1164 (semantic--tag-unlink-list-from-buffer
1165 ;; @todo- use fetch-tags-fast?
1166 (semantic-fetch-tags)))
1167
1168 (defvar semantic--buffer-cache)
1169
1170 (defun semantic--tag-link-cache-to-buffer ()
1171 "Convert all tags in the current cache to use overlays.
1172 This function is for internal use only."
1173 (require 'semantic)
1174 (condition-case nil
1175 ;; In this unique case, we cannot call the usual toplevel fn.
1176 ;; because we don't want a reparse, we want the old overlays.
1177 (semantic--tag-link-list-to-buffer
1178 semantic--buffer-cache)
1179 ;; Recover when there is an error restoring the cache.
1180 (error (message "Error recovering tag list")
1181 (semantic-clear-toplevel-cache)
1182 nil)))
1183 \f
1184 ;;; Tag Cooking
1185 ;;
1186 ;; Raw tags from a parser follow a different positional format than
1187 ;; those used in the buffer cache. Raw tags need to be cooked into
1188 ;; semantic cache friendly tags for use by the masses.
1189 ;;
1190 (defsubst semantic--tag-expanded-p (tag)
1191 "Return non-nil if TAG is expanded.
1192 This function is for internal use only.
1193 See also the function `semantic--expand-tag'."
1194 ;; In fact a cooked tag is actually a list of cooked tags
1195 ;; because a raw tag can be expanded in several cooked ones!
1196 (when (consp tag)
1197 (while (and (semantic-tag-p (car tag))
1198 (vectorp (semantic-tag-overlay (car tag))))
1199 (setq tag (cdr tag)))
1200 (null tag)))
1201
1202 (defvar semantic-tag-expand-function nil
1203 "Function used to expand a tag.
1204 It is passed each tag production, and must return a list of tags
1205 derived from it, or nil if it does not need to be expanded.
1206
1207 Languages with compound definitions should use this function to expand
1208 from one compound symbol into several. For example, in C or Java the
1209 following definition is easily parsed into one tag:
1210
1211 int a, b;
1212
1213 This function should take this compound tag and turn it into two tags,
1214 one for A, and the other for B.")
1215 (make-variable-buffer-local 'semantic-tag-expand-function)
1216
1217 (defun semantic--tag-expand (tag)
1218 "Convert TAG from a raw state to a cooked state, and expand it.
1219 Returns a list of cooked tags.
1220
1221 The parser returns raw tags with positional data START END at the
1222 end of the tag data structure (a list for now). We convert it from
1223 that to a cooked state that uses an overlay proxy, that is, a vector
1224 \[START END].
1225
1226 The raw tag is changed with side effects and maybe expanded in
1227 several derived tags when the variable `semantic-tag-expand-function'
1228 is set.
1229
1230 This function is for internal use only."
1231 (if (semantic--tag-expanded-p tag)
1232 ;; Just return TAG if it is already expanded (by a grammar
1233 ;; semantic action), or if it isn't recognized as a valid
1234 ;; semantic tag.
1235 tag
1236
1237 ;; Try to cook the tag. This code will be removed when tag will
1238 ;; be directly created with the right format.
1239 (condition-case nil
1240 (let ((ocdr (semantic--tag-overlay-cdr tag)))
1241 ;; OCDR contains the sub-list of TAG whose car is the
1242 ;; OVERLAY part of TAG. That is, a list (OVERLAY START END).
1243 ;; Convert it into an overlay proxy ([START END]).
1244 (semantic--tag-set-overlay
1245 tag (vector (nth 1 ocdr) (nth 2 ocdr)))
1246 ;; Remove START END positions at end of tag.
1247 (setcdr ocdr nil)
1248 ;; At this point (length TAG) must be 5!
1249 ;;(unless (= (length tag) 5)
1250 ;; (error "Tag expansion failed"))
1251 )
1252 (error
1253 (message "A Rule must return a single tag-line list!")
1254 (debug tag)
1255 nil))
1256
1257 ;; @todo - I think we've waited long enough. Lets find out.
1258 ;;
1259 ;; ;; Compatibility code to be removed in future versions.
1260 ;; (unless semantic-tag-expand-function
1261 ;; ;; This line throws a byte compiler warning.
1262 ;; (setq semantic-tag-expand-function semantic-expand-nonterminal)
1263 ;; )
1264
1265 ;; Expand based on local configuration
1266 (if semantic-tag-expand-function
1267 (or (funcall semantic-tag-expand-function tag)
1268 (list tag))
1269 (list tag))))
1270 \f
1271 ;; Foreign tags
1272 ;;
1273 (defmacro semantic-foreign-tag-invalid (tag)
1274 "Signal that TAG is an invalid foreign tag."
1275 `(signal 'wrong-type-argument '(semantic-foreign-tag-p ,tag)))
1276
1277 (defsubst semantic-foreign-tag-p (tag)
1278 "Return non-nil if TAG is a foreign tag.
1279 That is, a tag unlinked from the originating buffer, which carries the
1280 originating buffer file name, and major mode."
1281 (and (semantic-tag-p tag)
1282 (semantic--tag-get-property tag :foreign-flag)))
1283
1284 (defsubst semantic-foreign-tag-check (tag)
1285 "Check that TAG is a valid foreign tag.
1286 Signal an error if not."
1287 (or (semantic-foreign-tag-p tag)
1288 (semantic-foreign-tag-invalid tag)))
1289
1290 (defun semantic-foreign-tag (&optional tag)
1291 "Return a copy of TAG as a foreign tag, or nil if it can't be done.
1292 TAG defaults to the tag at point in current buffer.
1293 See also `semantic-foreign-tag-p'."
1294 (or tag (setq tag (semantic-current-tag)))
1295 (when (semantic-tag-p tag)
1296 (let ((ftag (semantic-tag-copy tag nil t))
1297 ;; Do extra work for the doc strings, since this is a
1298 ;; common use case.
1299 (doc (condition-case nil
1300 (semantic-documentation-for-tag tag)
1301 (error nil))))
1302 ;; A foreign tag must carry its originating buffer file name!
1303 (when (semantic--tag-get-property ftag :filename)
1304 (semantic--tag-put-property ftag :mode (semantic-tag-mode tag))
1305 (semantic--tag-put-property ftag :documentation doc)
1306 (semantic--tag-put-property ftag :foreign-flag t)
1307 ftag))))
1308
1309 ;; High level obtain/insert foreign tag overloads
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))
1315
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)))
1323
1324 (define-overloadable-function semantic-insert-foreign-tag (foreign-tag)
1325 "Insert FOREIGN-TAG into the current buffer.
1326 Signal an error if FOREIGN-TAG is not a valid foreign tag.
1327 This function is overridable with the symbol `insert-foreign-tag'."
1328 (semantic-foreign-tag-check foreign-tag)
1329 (:override)
1330 (message (semantic-format-tag-summarize foreign-tag)))
1331
1332 ;;; Support log modes here
1333 (define-mode-local-override semantic-insert-foreign-tag
1334 log-edit-mode (foreign-tag)
1335 "Insert foreign tags into log-edit mode."
1336 (insert (concat "(" (semantic-format-tag-name foreign-tag) "): ")))
1337
1338 (define-mode-local-override semantic-insert-foreign-tag
1339 change-log-mode (foreign-tag)
1340 "Insert foreign tags into log-edit mode."
1341 (insert (concat "(" (semantic-format-tag-name foreign-tag) "): ")))
1342
1343 \f
1344 ;;; EDEBUG display support
1345 ;;
1346 (eval-after-load "cedet-edebug"
1347 '(progn
1348 (cedet-edebug-add-print-override
1349 '(semantic-tag-p object)
1350 '(concat "#<TAG " (semantic-format-tag-name object) ">"))
1351 (cedet-edebug-add-print-override
1352 '(and (listp object) (semantic-tag-p (car object)))
1353 '(cedet-edebug-prin1-recurse object))
1354 ))
1355 \f
1356 ;;; Compatibility
1357 ;;
1358 (defconst semantic-token-version
1359 semantic-tag-version)
1360 (defconst semantic-token-incompatible-version
1361 semantic-tag-incompatible-version)
1362
1363 (semantic-alias-obsolete 'semantic-token-name
1364 'semantic-tag-name)
1365
1366 (semantic-alias-obsolete 'semantic-token-token
1367 'semantic-tag-class)
1368
1369 (semantic-alias-obsolete 'semantic-token-extra-specs
1370 'semantic-tag-attributes)
1371
1372 (semantic-alias-obsolete 'semantic-token-properties
1373 'semantic-tag-properties)
1374
1375 (semantic-alias-obsolete 'semantic-token-properties-cdr
1376 'semantic--tag-properties-cdr)
1377
1378 (semantic-alias-obsolete 'semantic-token-overlay
1379 'semantic-tag-overlay)
1380
1381 (semantic-alias-obsolete 'semantic-token-overlay-cdr
1382 'semantic--tag-overlay-cdr)
1383
1384 (semantic-alias-obsolete 'semantic-token-start
1385 'semantic-tag-start)
1386
1387 (semantic-alias-obsolete 'semantic-token-end
1388 'semantic-tag-end)
1389
1390 (semantic-alias-obsolete 'semantic-token-extent
1391 'semantic-tag-bounds)
1392
1393 (semantic-alias-obsolete 'semantic-token-buffer
1394 'semantic-tag-buffer)
1395
1396 (semantic-alias-obsolete 'semantic-token-put
1397 'semantic--tag-put-property)
1398
1399 (semantic-alias-obsolete 'semantic-token-put-no-side-effect
1400 'semantic--tag-put-property-no-side-effect)
1401
1402 (semantic-alias-obsolete 'semantic-token-get
1403 'semantic--tag-get-property)
1404
1405 (semantic-alias-obsolete 'semantic-token-add-extra-spec
1406 'semantic-tag-put-attribute)
1407
1408 (semantic-alias-obsolete 'semantic-token-extra-spec
1409 'semantic-tag-get-attribute)
1410
1411 (semantic-alias-obsolete 'semantic-token-type
1412 'semantic-tag-type)
1413
1414 (semantic-alias-obsolete 'semantic-token-modifiers
1415 'semantic-tag-modifiers)
1416
1417 (semantic-alias-obsolete 'semantic-token-docstring
1418 'semantic-tag-docstring)
1419
1420 (semantic-alias-obsolete 'semantic-token-type-parts
1421 'semantic-tag-type-members)
1422
1423 (defsubst semantic-token-type-parent (tag)
1424 "Return the parent of the type that TAG describes.
1425 The return value is a list. A value of nil means no parents.
1426 The `car' of the list is either the parent class, or a list
1427 of parent classes. The `cdr' of the list is the list of
1428 interfaces, or abstract classes which are parents of TAG."
1429 (cons (semantic-tag-get-attribute tag :superclasses)
1430 (semantic-tag-type-interfaces tag)))
1431 (make-obsolete 'semantic-token-type-parent
1432 "\
1433 use `semantic-tag-type-superclass' \
1434 and `semantic-tag-type-interfaces' instead")
1435
1436 (semantic-alias-obsolete 'semantic-token-type-parent-superclass
1437 'semantic-tag-type-superclasses)
1438
1439 (semantic-alias-obsolete 'semantic-token-type-parent-implement
1440 'semantic-tag-type-interfaces)
1441
1442 (semantic-alias-obsolete 'semantic-token-type-extra-specs
1443 'semantic-tag-attributes)
1444
1445 (semantic-alias-obsolete 'semantic-token-type-extra-spec
1446 'semantic-tag-get-attribute)
1447
1448 (semantic-alias-obsolete 'semantic-token-type-modifiers
1449 'semantic-tag-modifiers)
1450
1451 (semantic-alias-obsolete 'semantic-token-function-args
1452 'semantic-tag-function-arguments)
1453
1454 (semantic-alias-obsolete 'semantic-token-function-extra-specs
1455 'semantic-tag-attributes)
1456
1457 (semantic-alias-obsolete 'semantic-token-function-extra-spec
1458 'semantic-tag-get-attribute)
1459
1460 (semantic-alias-obsolete 'semantic-token-function-modifiers
1461 'semantic-tag-modifiers)
1462
1463 (semantic-alias-obsolete 'semantic-token-function-throws
1464 'semantic-tag-function-throws)
1465
1466 (semantic-alias-obsolete 'semantic-token-function-parent
1467 'semantic-tag-function-parent)
1468
1469 (semantic-alias-obsolete 'semantic-token-function-destructor
1470 'semantic-tag-function-destructor-p)
1471
1472 (semantic-alias-obsolete 'semantic-token-variable-default
1473 'semantic-tag-variable-default)
1474
1475 (semantic-alias-obsolete 'semantic-token-variable-extra-specs
1476 'semantic-tag-attributes)
1477
1478 (semantic-alias-obsolete 'semantic-token-variable-extra-spec
1479 'semantic-tag-get-attribute)
1480
1481 (semantic-alias-obsolete 'semantic-token-variable-modifiers
1482 'semantic-tag-modifiers)
1483
1484 (semantic-alias-obsolete 'semantic-token-variable-const
1485 'semantic-tag-variable-constant-p)
1486
1487 (semantic-alias-obsolete 'semantic-token-variable-optsuffix
1488 'semantic-tag-variable-optsuffix)
1489
1490 (semantic-alias-obsolete 'semantic-token-include-system
1491 'semantic-tag-include-system-p)
1492
1493 (semantic-alias-obsolete 'semantic-token-p
1494 'semantic-tag-p)
1495
1496 (semantic-alias-obsolete 'semantic-token-with-position-p
1497 'semantic-tag-with-position-p)
1498
1499 (semantic-alias-obsolete 'semantic-tag-make-assoc-list
1500 'semantic-tag-make-plist)
1501
1502 (semantic-alias-obsolete 'semantic-nonterminal-children
1503 'semantic-tag-children-compatibility)
1504
1505 (semantic-alias-obsolete 'semantic-narrow-to-token
1506 'semantic-narrow-to-tag)
1507
1508 (semantic-alias-obsolete 'semantic-with-buffer-narrowed-to-current-token
1509 'semantic-with-buffer-narrowed-to-current-tag)
1510
1511 (semantic-alias-obsolete 'semantic-with-buffer-narrowed-to-token
1512 'semantic-with-buffer-narrowed-to-tag)
1513
1514 (semantic-alias-obsolete 'semantic-deoverlay-token
1515 'semantic--tag-unlink-from-buffer)
1516
1517 (semantic-alias-obsolete 'semantic-overlay-token
1518 'semantic--tag-link-to-buffer)
1519
1520 (semantic-alias-obsolete 'semantic-deoverlay-list
1521 'semantic--tag-unlink-list-from-buffer)
1522
1523 (semantic-alias-obsolete 'semantic-overlay-list
1524 'semantic--tag-link-list-to-buffer)
1525
1526 (semantic-alias-obsolete 'semantic-deoverlay-cache
1527 'semantic--tag-unlink-cache-from-buffer)
1528
1529 (semantic-alias-obsolete 'semantic-overlay-cache
1530 'semantic--tag-link-cache-to-buffer)
1531
1532 (semantic-alias-obsolete 'semantic-cooked-token-p
1533 'semantic--tag-expanded-p)
1534
1535 (semantic-varalias-obsolete 'semantic-expand-nonterminal
1536 'semantic-tag-expand-function)
1537
1538 (semantic-alias-obsolete 'semantic-raw-to-cooked-token
1539 'semantic--tag-expand)
1540
1541 ;; Lets test this out during this short transition.
1542 (semantic-alias-obsolete 'semantic-clone-tag
1543 'semantic-tag-clone)
1544
1545 (semantic-alias-obsolete 'semantic-token
1546 'semantic-tag)
1547
1548 (semantic-alias-obsolete 'semantic-token-new-variable
1549 'semantic-tag-new-variable)
1550
1551 (semantic-alias-obsolete 'semantic-token-new-function
1552 'semantic-tag-new-function)
1553
1554 (semantic-alias-obsolete 'semantic-token-new-type
1555 'semantic-tag-new-type)
1556
1557 (semantic-alias-obsolete 'semantic-token-new-include
1558 'semantic-tag-new-include)
1559
1560 (semantic-alias-obsolete 'semantic-token-new-package
1561 'semantic-tag-new-package)
1562
1563 (semantic-alias-obsolete 'semantic-equivalent-tokens-p
1564 'semantic-equivalent-tag-p)
1565
1566 (provide 'semantic/tag)
1567
1568 ;; Local variables:
1569 ;; generated-autoload-file: "loaddefs.el"
1570 ;; generated-autoload-feature: semantic/loaddefs
1571 ;; End:
1572
1573 ;;; semantic-tag.el ends here