CEDET (development tools) package merged.
[bpt/emacs.git] / lisp / cedet / semantic / tag.el
1 ;;; semantic/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 ;;; Code:
46 ;;
47
48 ;; Keep this only so long as we have obsolete fcns.
49 (require 'semantic/fw)
50 (require 'semantic/lex)
51
52 (declare-function semantic-analyze-split-name "semantic/analyze/fcn")
53 (declare-function semantic-fetch-tags "semantic")
54 (declare-function semantic-clear-toplevel-cache "semantic")
55
56 (defconst semantic-tag-version "2.0pre7"
57 "Version string of semantic tags made with this code.")
58
59 (defconst semantic-tag-incompatible-version "1.0"
60 "Version string of semantic tags which are not currently compatible.
61 These old style tags may be loaded from a file with semantic db.
62 In this case, we must flush the old tags and start over.")
63 \f
64 ;;; Primitive Tag access system:
65 ;;
66 ;; Raw tags in semantic are lists of 5 elements:
67 ;;
68 ;; (NAME CLASS ATTRIBUTES PROPERTIES OVERLAY)
69 ;;
70 ;; Where:
71 ;;
72 ;; - NAME is a string that represents the tag name.
73 ;;
74 ;; - CLASS is a symbol that represent the class of the tag (for
75 ;; example, usual classes are `type', `function', `variable',
76 ;; `include', `package', `code').
77 ;;
78 ;; - ATTRIBUTES is a public list of attributes that describes
79 ;; language data represented by the tag (for example, a variable
80 ;; can have a `:constant-flag' attribute, a function an `:arguments'
81 ;; attribute, etc.).
82 ;;
83 ;; - PROPERTIES is a private list of properties used internally.
84 ;;
85 ;; - OVERLAY represent the location of data described by the tag.
86 ;;
87
88 (defsubst semantic-tag-name (tag)
89 "Return the name of TAG.
90 For functions, variables, classes, typedefs, etc., this is the identifier
91 that is being defined. For tags without an obvious associated name, this
92 may be the statement type, e.g., this may return @code{print} for python's
93 print statement."
94 (car tag))
95
96 (defsubst semantic-tag-class (tag)
97 "Return the class of TAG.
98 That is, the symbol 'variable, 'function, 'type, or other.
99 There is no limit to the symbols that may represent the class of a tag.
100 Each parser generates tags with classes defined by it.
101
102 For functional languages, typical tag classes are:
103
104 @table @code
105 @item type
106 Data types, named map for a memory block.
107 @item function
108 A function or method, or named execution location.
109 @item variable
110 A variable, or named storage for data.
111 @item include
112 Statement that represents a file from which more tags can be found.
113 @item package
114 Statement that declairs this file's package name.
115 @item code
116 Code that has not name or binding to any other symbol, such as in a script.
117 @end table
118 "
119 (nth 1 tag))
120
121 (defsubst semantic-tag-attributes (tag)
122 "Return the list of public attributes of TAG.
123 That is a property list: (ATTRIBUTE-1 VALUE-1 ATTRIBUTE-2 VALUE-2...)."
124 (nth 2 tag))
125
126 (defsubst semantic-tag-properties (tag)
127 "Return the list of private properties of TAG.
128 That is a property list: (PROPERTY-1 VALUE-1 PROPERTY-2 VALUE-2...)."
129 (nth 3 tag))
130
131 (defsubst semantic-tag-overlay (tag)
132 "Return the OVERLAY part of TAG.
133 That is, an overlay or an unloaded buffer representation.
134 This function can also return an array of the form [ START END ].
135 This occurs for tags that are not currently linked into a buffer."
136 (nth 4 tag))
137
138 (defsubst semantic--tag-overlay-cdr (tag)
139 "Return the cons cell whose car is the OVERLAY part of TAG.
140 That function is for internal use only."
141 (nthcdr 4 tag))
142
143 (defsubst semantic--tag-set-overlay (tag overlay)
144 "Set the overlay part of TAG with OVERLAY.
145 That function is for internal use only."
146 (setcar (semantic--tag-overlay-cdr tag) overlay))
147
148 (defsubst semantic-tag-start (tag)
149 "Return the start location of TAG."
150 (let ((o (semantic-tag-overlay tag)))
151 (if (semantic-overlay-p o)
152 (semantic-overlay-start o)
153 (aref o 0))))
154
155 (defsubst semantic-tag-end (tag)
156 "Return the end location of TAG."
157 (let ((o (semantic-tag-overlay tag)))
158 (if (semantic-overlay-p o)
159 (semantic-overlay-end o)
160 (aref o 1))))
161
162 (defsubst semantic-tag-bounds (tag)
163 "Return the location (START END) of data TAG describes."
164 (list (semantic-tag-start tag)
165 (semantic-tag-end tag)))
166
167 (defun semantic-tag-set-bounds (tag start end)
168 "In TAG, set the START and END location of data it describes."
169 (let ((o (semantic-tag-overlay tag)))
170 (if (semantic-overlay-p o)
171 (semantic-overlay-move o start end)
172 (semantic--tag-set-overlay tag (vector start end)))))
173
174 (defun semantic-tag-in-buffer-p (tag)
175 "Return the buffer TAG resides in IFF tag is already in a buffer.
176 If a tag is not in a buffer, return nil."
177 (let ((o (semantic-tag-overlay tag)))
178 ;; TAG is currently linked to a buffer, return it.
179 (when (and (semantic-overlay-p o)
180 (semantic-overlay-live-p o))
181 (semantic-overlay-buffer o))))
182
183 (defsubst semantic--tag-get-property (tag property)
184 "From TAG, extract the value of PROPERTY.
185 Return the value found, or nil if PROPERTY is not one of the
186 properties of TAG.
187 That function is for internal use only."
188 (plist-get (semantic-tag-properties tag) property))
189
190 (defun semantic-tag-buffer (tag)
191 "Return the buffer TAG resides in.
192 If TAG has an originating file, read that file into a (maybe new)
193 buffer, and return it.
194 Return nil if there is no buffer for this tag."
195 (let ((buff (semantic-tag-in-buffer-p tag)))
196 (if buff
197 buff
198 ;; TAG has an originating file, read that file into a buffer, and
199 ;; return it.
200 (if (semantic--tag-get-property tag :filename)
201 (save-match-data
202 (find-file-noselect (semantic--tag-get-property tag :filename)))
203 ;; TAG is not in Emacs right now, no buffer is available.
204 ))))
205
206 (defun semantic-tag-mode (&optional tag)
207 "Return the major mode active for TAG.
208 TAG defaults to the tag at point in current buffer.
209 If TAG has a :mode property return it.
210 If point is inside TAG bounds, return the major mode active at point.
211 Return the major mode active at beginning of TAG otherwise.
212 See also the function `semantic-ctxt-current-mode'."
213 (or tag (setq tag (semantic-current-tag)))
214 (or (semantic--tag-get-property tag :mode)
215 (let ((buffer (semantic-tag-buffer tag))
216 (start (semantic-tag-start tag))
217 (end (semantic-tag-end tag)))
218 (save-excursion
219 (and buffer (set-buffer buffer))
220 ;; Unless point is inside TAG bounds, move it to the
221 ;; beginning of TAG.
222 (or (and (>= (point) start) (< (point) end))
223 (goto-char start))
224 (semantic-ctxt-current-mode)))))
225
226 (defsubst semantic--tag-attributes-cdr (tag)
227 "Return the cons cell whose car is the ATTRIBUTES part of TAG.
228 That function is for internal use only."
229 (nthcdr 2 tag))
230
231 (defsubst semantic-tag-put-attribute (tag attribute value)
232 "Change value in TAG of ATTRIBUTE to VALUE.
233 If ATTRIBUTE already exists, its value is set to VALUE, otherwise the
234 new ATTRIBUTE VALUE pair is added.
235 Return TAG.
236 Use this function in a parser when not all attributes are known at the
237 same time."
238 (let* ((plist-cdr (semantic--tag-attributes-cdr tag)))
239 (when (consp plist-cdr)
240 (setcar plist-cdr
241 (semantic-tag-make-plist
242 (plist-put (car plist-cdr) attribute value))))
243 tag))
244
245 (defun semantic-tag-put-attribute-no-side-effect (tag attribute value)
246 "Change value in TAG of ATTRIBUTE to VALUE without side effects.
247 All cons cells in the attribute list are replicated so that there
248 are no side effects if TAG is in shared lists.
249 If ATTRIBUTE already exists, its value is set to VALUE, otherwise the
250 new ATTRIBUTE VALUE pair is added.
251 Return TAG."
252 (let* ((plist-cdr (semantic--tag-attributes-cdr tag)))
253 (when (consp plist-cdr)
254 (setcar plist-cdr
255 (semantic-tag-make-plist
256 (plist-put (copy-sequence (car plist-cdr))
257 attribute value))))
258 tag))
259
260 (defsubst semantic-tag-get-attribute (tag attribute)
261 "From TAG, return the value of ATTRIBUTE.
262 ATTRIBUTE is a symbol whose specification value to get.
263 Return the value found, or nil if ATTRIBUTE is not one of the
264 attributes of TAG."
265 (plist-get (semantic-tag-attributes tag) attribute))
266
267 ;; These functions are for internal use only!
268 (defsubst semantic--tag-properties-cdr (tag)
269 "Return the cons cell whose car is the PROPERTIES part of TAG.
270 That function is for internal use only."
271 (nthcdr 3 tag))
272
273 (defun semantic--tag-put-property (tag property value)
274 "Change value in TAG of PROPERTY to VALUE.
275 If PROPERTY already exists, its value is set to VALUE, otherwise the
276 new PROPERTY VALUE pair is added.
277 Return TAG.
278 That function is for internal use only."
279 (let* ((plist-cdr (semantic--tag-properties-cdr tag)))
280 (when (consp plist-cdr)
281 (setcar plist-cdr
282 (semantic-tag-make-plist
283 (plist-put (car plist-cdr) property value))))
284 tag))
285
286 (defun semantic--tag-put-property-no-side-effect (tag property value)
287 "Change value in TAG of PROPERTY to VALUE without side effects.
288 All cons cells in the property list are replicated so that there
289 are no side effects if TAG is in shared lists.
290 If PROPERTY already exists, its value is set to VALUE, otherwise the
291 new PROPERTY VALUE pair is added.
292 Return TAG.
293 That function is for internal use only."
294 (let* ((plist-cdr (semantic--tag-properties-cdr tag)))
295 (when (consp plist-cdr)
296 (setcar plist-cdr
297 (semantic-tag-make-plist
298 (plist-put (copy-sequence (car plist-cdr))
299 property value))))
300 tag))
301
302 (defun semantic-tag-file-name (tag)
303 "Return the name of the file from which TAG originated.
304 Return nil if that information can't be obtained.
305 If TAG is from a loaded buffer, then that buffer's filename is used.
306 If TAG is unlinked, but has a :filename property, then that is used."
307 (let ((buffer (semantic-tag-in-buffer-p tag)))
308 (if buffer
309 (buffer-file-name buffer)
310 (semantic--tag-get-property tag :filename))))
311 \f
312 ;;; Tag tests and comparisons.
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
321 )
322 ;; If an error occurs, then it most certainly is not a tag.
323 (error nil)))
324
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))
328
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))
333
334 (defsubst semantic-tag-type (tag)
335 "Return the value of the `:type' attribute of TAG.
336 For a function it would be the data type of the return value.
337 For a variable, it is the storage type of that variable.
338 For a data type, the type is the style of datatype, such as
339 struct or union."
340 (semantic-tag-get-attribute tag :type))
341
342 (defun semantic-tag-with-position-p (tag)
343 "Return non-nil if TAG has positional information."
344 (and (semantic-tag-p tag)
345 (let ((o (semantic-tag-overlay tag)))
346 (or (and (semantic-overlay-p o)
347 (semantic-overlay-live-p o))
348 (arrayp o)))))
349
350 (defun semantic-equivalent-tag-p (tag1 tag2)
351 "Compare TAG1 and TAG2 and return non-nil if they are equivalent.
352 Use `equal' on elements the name, class, and position.
353 Use this function if tags are being copied and regrouped to test
354 for if two tags represent the same thing, but may be constructed
355 of different cons cells."
356 (and (equal (semantic-tag-name tag1) (semantic-tag-name tag2))
357 (semantic-tag-of-class-p tag1 (semantic-tag-class tag2))
358 (or (and (not (semantic-tag-overlay tag1))
359 (not (semantic-tag-overlay tag2)))
360 (and (semantic-tag-overlay tag1)
361 (semantic-tag-overlay tag2)
362 (equal (semantic-tag-bounds tag1)
363 (semantic-tag-bounds tag2))))))
364
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.
369
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)
379 )
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))))
385
386 (cond ((or (eq a :type) ;; already tested above.
387 (memq a ignorable-attributes)) ;; Ignore them...
388 nil)
389
390 ;; Don't test sublists of tags
391 ((and (listp v) (semantic-tag-p (car v)))
392 nil)
393
394 ;; The attributes are not the same?
395 ((not (equal v (semantic-tag-get-attribute tag2 a)))
396 (setq A3 nil))
397 (t
398 nil))
399 )
400 (setq attr1 (cdr (cdr attr1))))
401
402 (and A1 A2 A3)
403 ))
404
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))
413 )
414 (if (or (/= (length C1) (length C2))
415 (not (semantic-tag-similar-p tag1 tag2 ignorable-attributes))
416 )
417 ;; Basic test fails.
418 nil
419 ;; Else, check component lists.
420 (catch 'component-dissimilar
421 (while C1
422
423 (if (not (semantic-tag-similar-with-subtags-p
424 (car C1) (car C2) ignorable-attributes))
425 (throw 'component-dissimilar nil))
426
427 (setq C1 (cdr C1))
428 (setq C2 (cdr C2))
429 )
430 ;; If we made it this far, we are ok.
431 t) )))
432
433
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 "")))
454 )
455 (and
456 tagtypestring
457 (or
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))
468 t))
469 ))
470 ))
471
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))
477 )
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)
482 )))
483
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.
488
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))
493 \f
494 ;;; Tag creation
495 ;;
496
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:
504
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))
518
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))
526
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))
537
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))
549
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))
574
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))
584
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))
594
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))
603
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))
609
610 (defsubst semantic-tag-set-name (tag name)
611 "Set TAG name to NAME."
612 (setcar tag name))
613
614 ;;; Copying and cloning tags.
615 ;;
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)))
626
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)))
639
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)
646 )))
647
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)))
652
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.
658 ;;)
659
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))
664 )
665 copy))
666
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 ;; )))
674
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))
684
685 ;;; DEEP COPIES
686 ;;
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)
699 nil
700 nil)))
701
702 (defun semantic--tag-deep-copy-attributes (attrs &optional filter)
703 "Make a deep copy of ATTRS, applying FILTER to each child-tag.
704
705 It is safe to modify ATTR, and return a permutaion of that list.
706
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)))))
714
715 (defun semantic--tag-deep-copy-value (value &optional filter)
716 "Make a deep copy of VALUE, applying FILTER to each child-tag.
717
718 It is safe to modify VALUE, and return a permutaion of that list.
719
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))
726
727 ;; A list of more tags
728 ((and (listp value) (semantic-tag-p (car value)))
729 (semantic--tag-deep-copy-tag-list value filter))
730
731 ;; Some arbitrary data.
732 (t value)))
733
734 (defun semantic--tag-deep-copy-tag-list (tags &optional filter)
735 "Make a deep copy of TAGS, applying FILTER to each child-tag.
736
737 It is safe to modify the TAGS list, and return a permutaion of that list.
738
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)))))
746
747 \f
748 ;;; Standard Tag Access
749 ;;
750
751 ;;; Common
752 ;;
753 (defsubst semantic-tag-modifiers (tag)
754 "Return the value of the `:typemodifiers' attribute of TAG."
755 (semantic-tag-get-attribute tag :typemodifiers))
756
757 (defun semantic-tag-docstring (tag &optional buffer)
758 "Return the documentation of TAG.
759 That is the value defined by the `:documentation' attribute.
760 Optional argument BUFFER indicates where to get the text from.
761 If not provided, then only the POSITION can be provided.
762
763 If you want to get documentation for languages that do not store
764 the documentation string in the tag itself, use
765 `semantic-documentation-for-tag' instead."
766 (let ((p (semantic-tag-get-attribute tag :documentation)))
767 (cond
768 ((stringp p) p) ;; it is the doc string.
769
770 ((semantic-lex-token-with-text-p p)
771 (semantic-lex-token-text p))
772
773 ((and (semantic-lex-token-without-text-p p)
774 buffer)
775 (with-current-buffer buffer
776 (semantic-lex-token-text (car (semantic-lex p (1+ p))))))
777
778 (t nil))))
779
780 ;;; Generic attributes for tags of any class.
781 ;;
782 (defsubst semantic-tag-named-parent (tag)
783 "Return the parent of TAG.
784 That is the value of the `:parent' attribute.
785 If a definition can occur outside an actual parent structure, but
786 refers to that parent by name, then the :parent attribute should be used."
787 (semantic-tag-get-attribute tag :parent))
788
789 ;;; Tags of class `type'
790
791 (defun semantic-tag-type-superclasses (tag)
792 "Return the list of superclass names of the type that TAG describes."
793 (let ((supers (semantic-tag-get-attribute tag :superclasses)))
794 (cond ((stringp supers)
795 ;; If we have a string, make it a list.
796 (list supers))
797 ((semantic-tag-p supers)
798 ;; If we have one tag, return just the name.
799 (list (semantic-tag-name supers)))
800 ((and (consp supers) (semantic-tag-p (car supers)))
801 ;; If we have a tag list, then return the names.
802 (mapcar (lambda (s) (semantic-tag-name s))
803 supers))
804 ((consp supers)
805 ;; A list of something, return it.
806 supers))))
807
808 (defun semantic--tag-find-parent-by-name (name supers)
809 "Find the superclass NAME in the list of SUPERS.
810 If a simple search doesn't do it, try splitting up the names
811 in SUPERS."
812 (let ((stag nil))
813 (setq stag (semantic-find-first-tag-by-name name supers))
814
815 (when (not stag)
816 (require 'semantic/analyze/fcn)
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))
825 )))
826
827 stag))
828
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))))
856 ))
857
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))
862
863 ;;; Tags of class `function'
864 ;;
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))
869
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))
874
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))
881
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))
886
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))
891
892 ;;; Tags of class `variable'
893 ;;
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))
898
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))
903
904 ;;; Tags of class `include'
905 ;;
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))
910
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.)
917
918 See `semantic-dependency-tag-file' to expand an include
919 tag to a full file name.")
920
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))
925
926 ;;; Tags of class `code'
927 ;;
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))
932
933 ;;; Tags of class `alias'
934 ;;
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))
945
946 (defsubst semantic-tag-alias-class (tag)
947 "Return the class of tag TAG is an alias."
948 (semantic-tag-get-attribute tag :aliasclass))
949
950 (define-overloadable-function semantic-tag-alias-definition (tag)
951 "Return the definition TAG is an alias.
952 The returned value is a tag of the class that
953 `semantic-tag-alias-class' returns for TAG.
954 The default is to return the value of the :definition attribute.
955 Return nil if TAG is not of class 'alias."
956 (when (semantic-tag-of-class-p tag 'alias)
957 (:override
958 (semantic-tag-get-attribute tag :definition))))
959
960 ;;; Language Specific Tag access via overload
961 ;;
962 ;;;###autoload
963 (define-overloadable-function semantic-tag-components (tag)
964 "Return a list of components for TAG.
965 A Component is a part of TAG which itself may be a TAG.
966 Examples include the elements of a structure in a
967 tag of class `type, or the list of arguments to a
968 tag of class 'function."
969 )
970
971 (defun semantic-tag-components-default (tag)
972 "Return a list of components for TAG.
973 Perform the described task in `semantic-tag-components'."
974 (cond ((semantic-tag-of-class-p tag 'type)
975 (semantic-tag-type-members tag))
976 ((semantic-tag-of-class-p tag 'function)
977 (semantic-tag-function-arguments tag))
978 (t nil)))
979
980 (define-overloadable-function semantic-tag-components-with-overlays (tag)
981 "Return the list of top level components belonging to TAG.
982 Children are any sub-tags which contain overlays.
983
984 Default behavior is to get `semantic-tag-components' in addition
985 to the components of an anonymous types (if applicable.)
986
987 Note for language authors:
988 If a mode defines a language tag that has tags in it with overlays
989 you should still return them with this function.
990 Ignoring this step will prevent several features from working correctly."
991 )
992
993 (defun semantic-tag-components-with-overlays-default (tag)
994 "Return the list of top level components belonging to TAG.
995 Children are any sub-tags which contain overlays.
996 The default action collects regular components of TAG, in addition
997 to any components beloning to an anonymous type."
998 (let ((explicit-children (semantic-tag-components tag))
999 (type (semantic-tag-type tag))
1000 (anon-type-children nil)
1001 (all-children nil))
1002 ;; Identify if this tag has an anonymous structure as
1003 ;; its type. This implies it may have children with overlays.
1004 (when (and type (semantic-tag-p type))
1005 (setq anon-type-children (semantic-tag-components type))
1006 ;; Add anonymous children
1007 (while anon-type-children
1008 (when (semantic-tag-with-position-p (car anon-type-children))
1009 (setq all-children (cons (car anon-type-children) all-children)))
1010 (setq anon-type-children (cdr anon-type-children))))
1011 ;; Add explicit children
1012 (while explicit-children
1013 (when (semantic-tag-with-position-p (car explicit-children))
1014 (setq all-children (cons (car explicit-children) all-children)))
1015 (setq explicit-children (cdr explicit-children)))
1016 ;; Return
1017 (nreverse all-children)))
1018
1019 (defun semantic-tag-children-compatibility (tag &optional positiononly)
1020 "Return children of TAG.
1021 If POSITIONONLY is nil, use `semantic-tag-components'.
1022 If POSITIONONLY is non-nil, use `semantic-tag-components-with-overlays'.
1023 DO NOT use this fcn in new code. Use one of the above instead."
1024 (if positiononly
1025 (semantic-tag-components-with-overlays tag)
1026 (semantic-tag-components tag)))
1027 \f
1028 ;;; Tag Region
1029 ;;
1030 ;; A Tag represents a region in a buffer. You can narrow to that tag.
1031 ;;
1032 (defun semantic-narrow-to-tag (&optional tag)
1033 "Narrow to the region specified by the bounds of TAG.
1034 See `semantic-tag-bounds'."
1035 (interactive)
1036 (if (not tag) (setq tag (semantic-current-tag)))
1037 (narrow-to-region (semantic-tag-start tag)
1038 (semantic-tag-end tag)))
1039
1040 (defmacro semantic-with-buffer-narrowed-to-current-tag (&rest body)
1041 "Execute BODY with the buffer narrowed to the current tag."
1042 `(save-restriction
1043 (semantic-narrow-to-tag (semantic-current-tag))
1044 ,@body))
1045 (put 'semantic-with-buffer-narrowed-to-current-tag 'lisp-indent-function 0)
1046 (add-hook 'edebug-setup-hook
1047 (lambda ()
1048 (def-edebug-spec semantic-with-buffer-narrowed-to-current-tag
1049 (def-body))))
1050
1051 (defmacro semantic-with-buffer-narrowed-to-tag (tag &rest body)
1052 "Narrow to TAG, and execute BODY."
1053 `(save-restriction
1054 (semantic-narrow-to-tag ,tag)
1055 ,@body))
1056 (put 'semantic-with-buffer-narrowed-to-tag 'lisp-indent-function 1)
1057 (add-hook 'edebug-setup-hook
1058 (lambda ()
1059 (def-edebug-spec semantic-with-buffer-narrowed-to-tag
1060 (def-body))))
1061 \f
1062 ;;; Tag Hooks
1063 ;;
1064 ;; Semantic may want to provide special hooks when specific operations
1065 ;; are about to happen on a given tag. These routines allow for hook
1066 ;; maintenance on a tag.
1067
1068 ;; Internal global variable used to manage tag hooks. For example,
1069 ;; some implementation of `remove-hook' checks that the hook variable
1070 ;; is `default-boundp'.
1071 (defvar semantic--tag-hook-value)
1072
1073 (defun semantic-tag-add-hook (tag hook function &optional append)
1074 "Onto TAG, add to the value of HOOK the function FUNCTION.
1075 FUNCTION is added (if necessary) at the beginning of the hook list
1076 unless the optional argument APPEND is non-nil, in which case
1077 FUNCTION is added at the end.
1078 HOOK should be a symbol, and FUNCTION may be any valid function.
1079 See also the function `add-hook'."
1080 (let ((semantic--tag-hook-value (semantic--tag-get-property tag hook)))
1081 (add-hook 'semantic--tag-hook-value function append)
1082 (semantic--tag-put-property tag hook semantic--tag-hook-value)
1083 semantic--tag-hook-value))
1084
1085 (defun semantic-tag-remove-hook (tag hook function)
1086 "Onto TAG, remove from the value of HOOK the function FUNCTION.
1087 HOOK should be a symbol, and FUNCTION may be any valid function. If
1088 FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in
1089 the list of hooks to run in HOOK, then nothing is done.
1090 See also the function `remove-hook'."
1091 (let ((semantic--tag-hook-value (semantic--tag-get-property tag hook)))
1092 (remove-hook 'semantic--tag-hook-value function)
1093 (semantic--tag-put-property tag hook semantic--tag-hook-value)
1094 semantic--tag-hook-value))
1095
1096 (defun semantic--tag-run-hooks (tag hook &rest args)
1097 "Run for TAG all expressions saved on the property HOOK.
1098 Each hook expression must take at least one argument, the TAG.
1099 For any given situation, additional ARGS may be passed."
1100 (let ((semantic--tag-hook-value (semantic--tag-get-property tag hook))
1101 (arglist (cons tag args)))
1102 (condition-case err
1103 ;; If a hook bombs, ignore it! Usually this is tied into
1104 ;; some sort of critical system.
1105 (apply 'run-hook-with-args 'semantic--tag-hook-value arglist)
1106 (error (message "Error: %S" err)))))
1107 \f
1108 ;;; Tags and Overlays
1109 ;;
1110 ;; Overlays are used so that we can quickly identify tags from
1111 ;; buffer positions and regions using built in Emacs commands.
1112 ;;
1113 (defsubst semantic--tag-unlink-list-from-buffer (tags)
1114 "Convert TAGS from using an overlay to using an overlay proxy.
1115 This function is for internal use only."
1116 (mapcar 'semantic--tag-unlink-from-buffer tags))
1117
1118 (defun semantic--tag-unlink-from-buffer (tag)
1119 "Convert TAG from using an overlay to using an overlay proxy.
1120 This function is for internal use only."
1121 (when (semantic-tag-p tag)
1122 (let ((o (semantic-tag-overlay tag)))
1123 (when (semantic-overlay-p o)
1124 (semantic--tag-set-overlay
1125 tag (vector (semantic-overlay-start o)
1126 (semantic-overlay-end o)))
1127 (semantic-overlay-delete o))
1128 ;; Look for a link hook on TAG.
1129 (semantic--tag-run-hooks tag 'unlink-hook)
1130 ;; Fix the sub-tags which contain overlays.
1131 (semantic--tag-unlink-list-from-buffer
1132 (semantic-tag-components-with-overlays tag)))))
1133
1134 (defsubst semantic--tag-link-list-to-buffer (tags)
1135 "Convert TAGS from using an overlay proxy to using an overlay.
1136 This function is for internal use only."
1137 (mapcar 'semantic--tag-link-to-buffer tags))
1138
1139 (defun semantic--tag-link-to-buffer (tag)
1140 "Convert TAG from using an overlay proxy to using an overlay.
1141 This function is for internal use only."
1142 (when (semantic-tag-p tag)
1143 (let ((o (semantic-tag-overlay tag)))
1144 (when (and (vectorp o) (= (length o) 2))
1145 (setq o (semantic-make-overlay (aref o 0) (aref o 1)
1146 (current-buffer)))
1147 (semantic--tag-set-overlay tag o)
1148 (semantic-overlay-put o 'semantic tag)
1149 ;; Clear the :filename property
1150 (semantic--tag-put-property tag :filename nil))
1151 ;; Look for a link hook on TAG.
1152 (semantic--tag-run-hooks tag 'link-hook)
1153 ;; Fix the sub-tags which contain overlays.
1154 (semantic--tag-link-list-to-buffer
1155 (semantic-tag-components-with-overlays tag)))))
1156
1157 (defun semantic--tag-unlink-cache-from-buffer ()
1158 "Convert all tags in the current cache to use overlay proxys.
1159 This function is for internal use only."
1160 (require 'semantic)
1161 (semantic--tag-unlink-list-from-buffer
1162 ;; @todo- use fetch-tags-fast?
1163 (semantic-fetch-tags)))
1164
1165 (defvar semantic--buffer-cache)
1166
1167 (defun semantic--tag-link-cache-to-buffer ()
1168 "Convert all tags in the current cache to use overlays.
1169 This function is for internal use only."
1170 (require 'semantic)
1171 (condition-case nil
1172 ;; In this unique case, we cannot call the usual toplevel fn.
1173 ;; because we don't want a reparse, we want the old overlays.
1174 (semantic--tag-link-list-to-buffer
1175 semantic--buffer-cache)
1176 ;; Recover when there is an error restoring the cache.
1177 (error (message "Error recovering tag list")
1178 (semantic-clear-toplevel-cache)
1179 nil)))
1180 \f
1181 ;;; Tag Cooking
1182 ;;
1183 ;; Raw tags from a parser follow a different positional format than
1184 ;; those used in the buffer cache. Raw tags need to be cooked into
1185 ;; semantic cache friendly tags for use by the masses.
1186 ;;
1187 (defsubst semantic--tag-expanded-p (tag)
1188 "Return non-nil if TAG is expanded.
1189 This function is for internal use only.
1190 See also the function `semantic--expand-tag'."
1191 ;; In fact a cooked tag is actually a list of cooked tags
1192 ;; because a raw tag can be expanded in several cooked ones!
1193 (when (consp tag)
1194 (while (and (semantic-tag-p (car tag))
1195 (vectorp (semantic-tag-overlay (car tag))))
1196 (setq tag (cdr tag)))
1197 (null tag)))
1198
1199 (defvar semantic-tag-expand-function nil
1200 "Function used to expand a tag.
1201 It is passed each tag production, and must return a list of tags
1202 derived from it, or nil if it does not need to be expanded.
1203
1204 Languages with compound definitions should use this function to expand
1205 from one compound symbol into several. For example, in C or Java the
1206 following definition is easily parsed into one tag:
1207
1208 int a, b;
1209
1210 This function should take this compound tag and turn it into two tags,
1211 one for A, and the other for B.")
1212 (make-variable-buffer-local 'semantic-tag-expand-function)
1213
1214 (defun semantic--tag-expand (tag)
1215 "Convert TAG from a raw state to a cooked state, and expand it.
1216 Returns a list of cooked tags.
1217
1218 The parser returns raw tags with positional data START END at the
1219 end of the tag data structure (a list for now). We convert it from
1220 that to a cooked state that uses an overlay proxy, that is, a vector
1221 \[START END].
1222
1223 The raw tag is changed with side effects and maybe expanded in
1224 several derived tags when the variable `semantic-tag-expand-function'
1225 is set.
1226
1227 This function is for internal use only."
1228 (if (semantic--tag-expanded-p tag)
1229 ;; Just return TAG if it is already expanded (by a grammar
1230 ;; semantic action), or if it isn't recognized as a valid
1231 ;; semantic tag.
1232 tag
1233
1234 ;; Try to cook the tag. This code will be removed when tag will
1235 ;; be directly created with the right format.
1236 (condition-case nil
1237 (let ((ocdr (semantic--tag-overlay-cdr tag)))
1238 ;; OCDR contains the sub-list of TAG whose car is the
1239 ;; OVERLAY part of TAG. That is, a list (OVERLAY START END).
1240 ;; Convert it into an overlay proxy ([START END]).
1241 (semantic--tag-set-overlay
1242 tag (vector (nth 1 ocdr) (nth 2 ocdr)))
1243 ;; Remove START END positions at end of tag.
1244 (setcdr ocdr nil)
1245 ;; At this point (length TAG) must be 5!
1246 ;;(unless (= (length tag) 5)
1247 ;; (error "Tag expansion failed"))
1248 )
1249 (error
1250 (message "A Rule must return a single tag-line list!")
1251 (debug tag)
1252 nil))
1253 ;; Expand based on local configuration
1254 (if semantic-tag-expand-function
1255 (or (funcall semantic-tag-expand-function tag)
1256 (list tag))
1257 (list tag))))
1258 \f
1259 ;; Foreign tags
1260 ;;
1261 (defmacro semantic-foreign-tag-invalid (tag)
1262 "Signal that TAG is an invalid foreign tag."
1263 `(signal 'wrong-type-argument '(semantic-foreign-tag-p ,tag)))
1264
1265 (defsubst semantic-foreign-tag-p (tag)
1266 "Return non-nil if TAG is a foreign tag.
1267 That is, a tag unlinked from the originating buffer, which carries the
1268 originating buffer file name, and major mode."
1269 (and (semantic-tag-p tag)
1270 (semantic--tag-get-property tag :foreign-flag)))
1271
1272 (defsubst semantic-foreign-tag-check (tag)
1273 "Check that TAG is a valid foreign tag.
1274 Signal an error if not."
1275 (or (semantic-foreign-tag-p tag)
1276 (semantic-foreign-tag-invalid tag)))
1277
1278 (defun semantic-foreign-tag (&optional tag)
1279 "Return a copy of TAG as a foreign tag, or nil if it can't be done.
1280 TAG defaults to the tag at point in current buffer.
1281 See also `semantic-foreign-tag-p'."
1282 (or tag (setq tag (semantic-current-tag)))
1283 (when (semantic-tag-p tag)
1284 (let ((ftag (semantic-tag-copy tag nil t))
1285 ;; Do extra work for the doc strings, since this is a
1286 ;; common use case.
1287 (doc (condition-case nil
1288 (semantic-documentation-for-tag tag)
1289 (error nil))))
1290 ;; A foreign tag must carry its originating buffer file name!
1291 (when (semantic--tag-get-property ftag :filename)
1292 (semantic--tag-put-property ftag :mode (semantic-tag-mode tag))
1293 (semantic--tag-put-property ftag :documentation doc)
1294 (semantic--tag-put-property ftag :foreign-flag t)
1295 ftag))))
1296
1297 ;; High level obtain/insert foreign tag overloads
1298 (define-overloadable-function semantic-obtain-foreign-tag (&optional tag)
1299 "Obtain a foreign tag from TAG.
1300 TAG defaults to the tag at point in current buffer.
1301 Return the obtained foreign tag or nil if failed."
1302 (semantic-foreign-tag tag))
1303
1304 (defun semantic-insert-foreign-tag-default (foreign-tag)
1305 "Insert FOREIGN-TAG into the current buffer.
1306 The default behavior assumes the current buffer is a language file,
1307 and attempts to insert a prototype/function call."
1308 ;; Long term goal: Have a mechanism for a tempo-like template insert
1309 ;; for the given tag.
1310 (insert (semantic-format-tag-prototype foreign-tag)))
1311
1312 (define-overloadable-function semantic-insert-foreign-tag (foreign-tag)
1313 "Insert FOREIGN-TAG into the current buffer.
1314 Signal an error if FOREIGN-TAG is not a valid foreign tag.
1315 This function is overridable with the symbol `insert-foreign-tag'."
1316 (semantic-foreign-tag-check foreign-tag)
1317 (:override)
1318 (message (semantic-format-tag-summarize foreign-tag)))
1319
1320 ;;; Support log modes here
1321 (define-mode-local-override semantic-insert-foreign-tag
1322 log-edit-mode (foreign-tag)
1323 "Insert foreign tags into log-edit mode."
1324 (insert (concat "(" (semantic-format-tag-name foreign-tag) "): ")))
1325
1326 (define-mode-local-override semantic-insert-foreign-tag
1327 change-log-mode (foreign-tag)
1328 "Insert foreign tags into log-edit mode."
1329 (insert (concat "(" (semantic-format-tag-name foreign-tag) "): ")))
1330 \f
1331 ;;; Compatibility
1332 ;;
1333 (defconst semantic-token-version
1334 semantic-tag-version)
1335 (defconst semantic-token-incompatible-version
1336 semantic-tag-incompatible-version)
1337
1338 (defsubst semantic-token-type-parent (tag)
1339 "Return the parent of the type that TAG describes.
1340 The return value is a list. A value of nil means no parents.
1341 The `car' of the list is either the parent class, or a list
1342 of parent classes. The `cdr' of the list is the list of
1343 interfaces, or abstract classes which are parents of TAG."
1344 (cons (semantic-tag-get-attribute tag :superclasses)
1345 (semantic-tag-type-interfaces tag)))
1346 (make-obsolete 'semantic-token-type-parent
1347 "\
1348 use `semantic-tag-type-superclass' \
1349 and `semantic-tag-type-interfaces' instead")
1350
1351 (semantic-alias-obsolete 'semantic-tag-make-assoc-list
1352 'semantic-tag-make-plist)
1353
1354 (semantic-varalias-obsolete 'semantic-expand-nonterminal
1355 'semantic-tag-expand-function)
1356
1357 (provide 'semantic/tag)
1358
1359 ;; Local variables:
1360 ;; generated-autoload-file: "loaddefs.el"
1361 ;; generated-autoload-feature: semantic/loaddefs
1362 ;; generated-autoload-load-name: "semantic/tag"
1363 ;; End:
1364
1365 ;;; semantic/tag.el ends here