1 ;;; semantic/scope.el --- Analyzer Scope Calculations
3 ;; Copyright (C) 2007-2017 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <eric@siege-engine.com>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
24 ;; Calculate information about the current scope.
26 ;; Manages the current scope as a structure that can be cached on a
27 ;; per-file basis and recycled between different occurrences of
28 ;; analysis on different parts of a file.
30 ;; Pattern for Scope Calculation
32 ;; Step 1: Calculate DataTypes in Scope:
34 ;; a) What is in scope via using statements or local namespaces
35 ;; b) Lineage of current context. Some names drawn from step 1.
37 ;; Step 2: Convert type names into lists of concrete tags
39 ;; a) Convert each datatype into the real datatype tag
40 ;; b) Convert namespaces into the list of contents of the namespace.
41 ;; c) Merge all existing scopes together into one search list.
43 ;; Step 3: Local variables
45 ;; a) Local variables are in the master search list.
48 (require 'semantic
/db
)
49 (require 'semantic
/analyze
/fcn
)
50 (require 'semantic
/ctxt
)
52 (eval-when-compile (require 'semantic
/find
))
54 (declare-function data-debug-show
"eieio-datadebug")
55 (declare-function semantic-analyze-find-tag
"semantic/analyze")
56 (declare-function semantic-analyze-princ-sequence
"semantic/analyze")
57 (declare-function semanticdb-typecache-merge-streams
"semantic/db-typecache")
58 (declare-function semanticdb-typecache-add-dependant
"semantic/db-typecache")
59 (declare-function semantic-tag-similar-p
"semantic/tag-ls")
63 (defclass semantic-scope-cache
(semanticdb-abstract-cache)
66 "The tag this scope was calculated for.")
67 (scopetypes :initform nil
69 "The list of types currently in scope.
70 For C++, this would contain anonymous namespaces known, and
71 anything labeled by a `using' statement.")
72 (parents :initform nil
74 "List of parents in scope w/in the body of this function.
75 Presumably, the members of these parent classes are available for access
76 based on private:, or public: style statements.")
77 (parentinheritance :initform nil
78 :documentation
"Alist of parents by inheritance.
79 Each entry is ( PARENT . PROTECTION ), where PARENT is a type, and
80 PROTECTION is a symbol representing the level of inheritance, such as 'private, or 'protected.")
83 "Items in scope due to the scopetypes or parents.")
84 (fullscope :initform nil
86 "All the other stuff on one master list you can search.")
87 (localargs :initform nil
89 "The arguments to the function tag.")
90 (localvar :initform nil
92 "The local variables.")
93 (typescope :initform nil
95 "Slot to save intermediate scope while metatypes are dereferenced.")
97 "Cache used for storage of the current scope by the Semantic Analyzer.
98 Saves scoping information between runs of the analyzer.")
102 ;; Methods for basic management of the structure in semanticdb.
104 (cl-defmethod semantic-reset ((obj semantic-scope-cache
))
105 "Reset OBJ back to it's empty settings."
107 (oset obj scopetypes nil
)
108 (oset obj parents nil
)
109 (oset obj parentinheritance nil
)
111 (oset obj fullscope nil
)
112 (oset obj localargs nil
)
113 (oset obj localvar nil
)
114 (oset obj typescope nil
)
117 (cl-defmethod semanticdb-synchronize ((cache semantic-scope-cache
)
119 "Synchronize a CACHE with some NEW-TAGS."
120 (semantic-reset cache
))
123 (cl-defmethod semanticdb-partial-synchronize ((cache semantic-scope-cache
)
125 "Synchronize a CACHE with some changed NEW-TAGS."
126 ;; If there are any includes or datatypes changed, then clear.
127 (if (or (semantic-find-tags-by-class 'include new-tags
)
128 (semantic-find-tags-by-class 'type new-tags
)
129 (semantic-find-tags-by-class 'using new-tags
))
130 (semantic-reset cache
))
133 (defun semantic-scope-reset-cache ()
134 "Get the current cached scope, and reset it."
135 (when semanticdb-current-table
136 (let ((co (semanticdb-cache-get semanticdb-current-table
137 'semantic-scope-cache
)))
138 (semantic-reset co
))))
140 (cl-defmethod semantic-scope-set-typecache ((cache semantic-scope-cache
)
142 "Set the :typescope property on CACHE to some types.
143 TYPES-IN-SCOPE is a list of type tags whos members are
144 currently in scope. For each type in TYPES-IN-SCOPE,
145 add those members to the types list.
146 If nil, then the typescope is reset."
147 (let ((newts nil
)) ;; New Type Scope
148 (dolist (onetype types-in-scope
)
149 (setq newts
(append (semantic-tag-type-members onetype
)
152 (oset cache typescope newts
)))
156 ;; These fcns should be used by search routines that return a single
157 ;; tag which, in turn, may have come from a deep scope. The scope
158 ;; will be attached to the tag. Thus, in future scope based calls, a
159 ;; tag can be passed in and a scope derived from it.
161 (defun semantic-scope-tag-clone-with-scope (tag scopetags
)
162 "Clone TAG, and return it. Add SCOPETAGS as a tag-local scope.
163 Stores the SCOPETAGS as a set of tag properties on the cloned tag."
164 (let ((clone (semantic-tag-clone tag
))
166 (semantic--tag-put-property clone
'scope scopetags
)
169 (defun semantic-scope-tag-get-scope (tag)
170 "Get from TAG the list of tags comprising the scope from TAG."
171 (semantic--tag-get-property tag
'scope
))
175 ;; Functions that do the main scope calculations
178 (define-overloadable-function semantic-analyze-scoped-types
(position)
179 "Return a list of types currently in scope at POSITION.
180 This is based on what tags exist at POSITION, and any associated
183 (defun semantic-analyze-scoped-types-default (position)
184 "Return a list of types currently in scope at POSITION.
185 Use `semantic-ctxt-scoped-types' to find types."
186 (require 'semantic
/db-typecache
)
189 (let ((code-scoped-types nil
))
190 ;; Let's ask if any types are currently scoped. Scoped
191 ;; classes and types provide their public methods and types
192 ;; in source code, but are unrelated hierarchically.
193 (let ((sp (semantic-ctxt-scoped-types)))
195 ;; Get this thing as a tag
198 (or (semanticdb-typecache-find (car sp
))
199 ;; If we did not find it in the typecache,
200 ;; look in the tags we found so far
201 (car (semantic-deep-find-tags-by-name
203 code-scoped-types
))))
204 ((semantic-tag-p (car sp
))
205 (if (semantic-tag-prototype-p (car sp
))
206 (or (semanticdb-typecache-find (semantic-tag-name (car sp
)))
207 (car (semantic-deep-find-tags-by-name
208 (semantic-tag-name (car sp
))
213 (setq code-scoped-types
214 (cons tmp code-scoped-types
))))
216 (setq code-scoped-types
(nreverse code-scoped-types
))
218 (when code-scoped-types
219 (semanticdb-typecache-merge-streams code-scoped-types nil
))
223 ;;------------------------------------------------------------
224 (define-overloadable-function semantic-analyze-scope-nested-tags
(position scopedtypes
)
225 "Return a list of types in order of nesting for the context of POSITION.
226 If POSITION is in a method with a named parent, find that parent, and
227 identify it's scope via overlay instead.
228 Optional SCOPETYPES are additional scoped entities in which our parent might
231 (defun semantic-analyze-scope-nested-tags-default (position scopetypes
)
232 "Return a list of types in order of nesting for the context of POSITION.
233 If POSITION is in a method with a named parent, find that parent, and
234 identify it's scope via overlay instead.
235 Optional SCOPETYPES are additional scoped entities in which our parent might
237 This only finds ONE immediate parent by name. All other parents returned
238 are from nesting data types."
239 (require 'semantic
/analyze
)
241 (if position
(goto-char position
))
242 (let* ((stack (reverse (semantic-find-tag-by-overlay (point))))
244 (pparent (car (cdr stack
)))
247 ;; In case of arg lists or some-such, throw out non-types.
248 (while (and stack
(not (semantic-tag-of-class-p pparent
'type
)))
249 (setq stack
(cdr stack
) pparent
(car (cdr stack
))))
252 (while (member pparent scopetypes
)
253 (setq stack
(cdr stack
) pparent
(car (cdr stack
))))
256 ;; Analyze the stack of tags we are nested in as parents.
259 ;; If we have a pparent tag, let's go there
260 ;; an analyze that stack of tags.
261 (when (and pparent
(semantic-tag-with-position-p pparent
))
262 (semantic-go-to-tag pparent
)
263 (setq stack
(semantic-find-tag-by-overlay (point)))
264 ;; Step one, find the merged version of stack in the typecache.
265 (let* ((stacknames (reverse (mapcar 'semantic-tag-name stack
)))
268 ;; @todo - can we use the typecache ability to
269 ;; put a scope into a tag to do this?
270 (while (and stacknames
271 (setq tc
(semanticdb-typecache-find
272 (reverse stacknames
))))
273 (setq returnlist
(cons tc returnlist
)
274 stacknames
(cdr stacknames
)))
275 (when (not returnlist
)
276 ;; When there was nothing from the typecache, then just
277 ;; use what's right here.
278 (setq stack
(reverse stack
))
279 ;; Add things to STACK until we cease finding tags of class type.
280 (while (and stack
(eq (semantic-tag-class (car stack
)) 'type
))
281 ;; Otherwise, just add this to the returnlist, but make
282 ;; sure we didn't already have that tag in scopetypes
283 (unless (member (car stack
) scopetypes
)
284 (setq returnlist
(cons (car stack
) returnlist
)))
285 (setq stack
(cdr stack
)))
287 (setq returnlist
(nreverse returnlist
))
291 ;; Only do this level of analysis for functions.
292 (when (eq (semantic-tag-class tag
) 'function
)
294 ;; If the function tag itself has a "parent" by name, then that
295 ;; parent will exist in the scope we just calculated, so look it
298 (let ((p (semantic-tag-function-parent tag
)))
300 ;; We have a parent, search for it.
301 (let* ((searchnameraw (cond ((stringp p
) p
)
303 (semantic-tag-name p
))
304 ((and (listp p
) (stringp (car p
)))
306 (searchname (semantic-analyze-split-name searchnameraw
))
307 (snlist (if (consp searchname
)
312 (miniscope (semantic-scope-cache "mini"))
315 ;; Find the next entry in the referenced type for
316 ;; our function, and append to return list till our
317 ;; returnlist is empty.
320 (append (mapcar 'semantic-tag-name returnlist
)
321 (list (car snlist
)))) ;; Next one
323 (semanticdb-typecache-find fullsearchname
))
326 (not (semantic-tag-of-class-p ptag
'type
)))
329 (mapcar 'semantic-tag-type-members
330 (cons (car returnlist
) scopetypes
)
333 (oset miniscope parents returnlist
) ;; Not really accurate, but close
334 (oset miniscope scope rawscope
)
335 (oset miniscope fullscope rawscope
)
337 (semantic-analyze-find-tag searchnameraw
344 (when (and (not (semantic-tag-p ptag
))
345 (semantic-tag-p (car ptag
)))
346 (setq ptag
(car ptag
)))
347 (setq returnlist
(append returnlist
(list ptag
)))
350 (setq snlist
(cdr snlist
)))
351 (setq returnlist returnlist
)
357 (define-overloadable-function semantic-analyze-scope-lineage-tags
(parents scopedtypes
)
358 "Return the full lineage of tags from PARENTS.
359 The return list is of the form ( TAG . PROTECTION ), where TAG is a tag,
360 and PROTECTION is the level of protection offered by the relationship.
361 Optional SCOPETYPES are additional scoped entities in which our parent might
364 (defun semantic-analyze-scope-lineage-tags-default (parents scopetypes
)
365 "Return the full lineage of tags from PARENTS.
366 The return list is of the form ( TAG . PROTECTION ), where TAG is a tag,
367 and PROTECTION is the level of protection offered by the relationship.
368 Optional SCOPETYPES are additional scoped entities in which our parent might
371 (miniscope (semantic-scope-cache "mini"))
373 (oset miniscope parents parents
)
374 (oset miniscope scope scopetypes
)
375 (oset miniscope fullscope scopetypes
)
377 (dolist (slp parents
)
378 (semantic-analyze-scoped-inherited-tag-map
379 slp
(lambda (newparent)
380 (let* ((pname (semantic-tag-name newparent
))
381 (prot (semantic-tag-type-superclass-protection slp pname
))
382 (effectiveprot (cond ((eq prot
'public
)
383 ;; doesn't provide access to private slots?
386 (push (cons newparent effectiveprot
) lineage
)
393 ;;------------------------------------------------------------
395 (define-overloadable-function semantic-analyze-scoped-tags
(typelist parentlist
)
396 "Return accessible tags when TYPELIST and PARENTLIST is in scope.
397 Tags returned are not in the global name space, but are instead
398 scoped inside a class or namespace. Such items can be referenced
399 without use of \"object.function()\" style syntax due to an
400 implicit \"object\".")
402 (defun semantic-analyze-scoped-tags-default (typelist halfscope
)
403 "Return accessible tags when TYPELIST and HALFSCOPE is in scope.
404 HALFSCOPE is the current scope partially initialized.
405 Tags returned are not in the global name space, but are instead
406 scoped inside a class or namespace. Such items can be referenced
407 without use of \"object.function()\" style syntax due to an
408 implicit \"object\"."
409 (let ((typelist2 nil
)
411 (parentlist (oref halfscope parents
))
412 (miniscope halfscope
)
414 ;; Loop over typelist, and find and merge all namespaces matching
415 ;; the names in typelist.
417 (let ((tt (semantic-tag-type (car typelist
))))
418 (when (and (stringp tt
) (string= tt
"namespace"))
419 ;; By using the typecache, our namespaces are pre-merged.
420 (setq typelist2
(cons (car typelist
) typelist2
))
422 (setq typelist
(cdr typelist
)))
424 ;; Loop over the types (which should be sorted by position)
425 ;; adding to the scopelist as we go, and using the scopelist
426 ;; for additional searching!
428 (oset miniscope scope currentscope
)
429 (oset miniscope fullscope currentscope
)
430 (setq currentscope
(append
431 (semantic-analyze-scoped-type-parts (car typelist2
)
434 (setq typelist2
(cdr typelist2
)))
436 ;; Collect all the types (class, etc) that are in our heritage.
437 ;; These are types that we can extract members from, not those
438 ;; declared in using statements, or the like.
439 ;; Get the PARENTS including nesting scope for this location.
441 (oset miniscope scope currentscope
)
442 (oset miniscope fullscope currentscope
)
443 (setq currentscope
(append
444 (semantic-analyze-scoped-type-parts (car parentlist
)
447 (setq parentlist
(cdr parentlist
)))
449 ;; Loop over all the items, and collect any type constants.
450 (let ((constants nil
))
451 (dolist (T currentscope
)
452 (setq constants
(append constants
453 (semantic-analyze-type-constants T
)))
456 (setq currentscope
(append currentscope constants
)))
460 ;;------------------------------------------------------------
461 (define-overloadable-function semantic-analyze-scope-calculate-access
(type scope
)
462 "Calculate the access class for TYPE as defined by the current SCOPE.
463 Access is related to the :parents in SCOPE. If type is a member of SCOPE
464 then access would be 'private. If TYPE is inherited by a member of SCOPE,
465 the access would be 'protected. Otherwise, access is 'public")
467 (defun semantic-analyze-scope-calculate-access-default (type scope
)
468 "Calculate the access class for TYPE as defined by the current SCOPE."
469 (cond ((semantic-scope-cache-p scope
)
470 (let ((parents (oref scope parents
))
471 (parentsi (oref scope parentinheritance
))
474 ;; Investigate the parent, and see how it relates to type.
475 ;; If these tags are basically the same, then we have full access.
477 (when (semantic-tag-similar-p type p
)
478 (throw 'moose
'private
))
480 ;; Look to see if type is in our list of inherited parents.
481 (dolist (pi parentsi
)
482 ;; pi is a cons cell ( PARENT . protection)
485 (when (semantic-tag-similar-p type pip
)
487 ;; protection via inheritance means to pull out different
488 ;; bits based on protection labels in an opposite way.
491 (protected . protected
)
492 (private . public
))))
495 ;; Not in our parentage. Is type a FRIEND?
496 (let ((friends (semantic-find-tags-by-class 'friend
(semantic-tag-type-members type
))))
499 (if (string= (semantic-tag-name F
) (semantic-tag-name pi
))
500 (throw 'moose
'private
))
502 ;; Found nothing, return public
507 (defun semantic-completable-tags-from-type (type)
508 "Return a list of slots that are valid completions from the list of SLOTS.
509 If a tag in SLOTS has a named parent, then that implies that the
510 tag is not something you can complete from within TYPE."
511 (let ((allslots (semantic-tag-components type
))
515 ;; We have to specially deal with 'using' tags here, since those
516 ;; pull in namespaces or classes into the current scope.
517 ;; (Should this go into c.el? If so, into which override?)
518 (if (semantic-tag-of-class-p S
'using
)
519 (let* ((fullname (semantic-analyze-unsplit-name
520 (list (semantic-tag-name type
)
521 (semantic-tag-name S
))))
522 ;; Search the typecache, first for the unqualified name
524 (semanticdb-typecache-find (semantic-tag-name S
))
525 ;; If that didn't return anything, use
526 ;; fully qualified name
527 (semanticdb-typecache-find fullname
)))
528 (filename (when usingtype
(semantic-tag-file-name usingtype
))))
530 ;; Use recursion to examine that namespace or class
531 (let ((tags (semantic-completable-tags-from-type usingtype
)))
533 ;; If we have a filename, copy the tags with it
535 (setq leftover
(cons (semantic-tag-copy cur nil filename
)
537 ;; Otherwise just run with it
538 (setq leftover
(append tags leftover
))))))
539 (when (or (not (semantic-tag-of-class-p S
'function
))
540 (not (semantic-tag-function-parent S
)))
541 (setq leftover
(cons S leftover
)))))
542 (nreverse leftover
)))
544 (defun semantic-analyze-scoped-type-parts (type &optional scope noinherit protection
)
545 "Return all parts of TYPE, a tag representing a TYPE declaration.
546 SCOPE is the scope object.
547 NOINHERIT turns off searching of inherited tags.
548 PROTECTION specifies the type of access requested, such as 'public or 'private."
551 (let* ((access (semantic-analyze-scope-calculate-access type scope
))
552 ;; SLOTS are the slots directly a part of TYPE.
553 (allslots (semantic-completable-tags-from-type type
))
554 (slots (semantic-find-tags-by-scope-protection
557 (fname (semantic-tag-file-name type
))
558 ;; EXTMETH are externally defined methods that are still
559 ;; a part of this class.
561 ;; @TODO - is this line needed?? Try w/out for a while
562 ;; @note - I think C++ says no. elisp might, but methods
563 ;; look like defuns, so it makes no difference.
564 (extmeth nil
) ; (semantic-tag-external-member-children type t))
566 ;; INHERITED are tags found in classes that our TYPE tag
567 ;; inherits from. Do not do this if it was not requested.
568 (inherited (when (not noinherit
)
569 (semantic-analyze-scoped-inherited-tags type scope
572 (when (not (semantic-tag-in-buffer-p type
))
573 (let ((copyslots nil
))
575 ;;(semantic--tag-put-property TAG :filename fname)
576 (if (semantic-tag-file-name TAG
)
577 ;; If it has a filename, just go with it...
578 (setq copyslots
(cons TAG copyslots
))
579 ;; Otherwise, copy the tag w/ the guessed filename.
580 (setq copyslots
(cons (semantic-tag-copy TAG nil fname
)
583 (setq slots
(nreverse copyslots
))
585 ;; Flatten the database output.
586 (append slots extmeth inherited
)
589 (defun semantic-analyze-scoped-inherited-tags (type scope access
)
590 "Return all tags that TYPE inherits from.
591 Argument SCOPE specify additional tags that are in scope
592 whose tags can be searched when needed, OR it may be a scope object.
593 ACCESS is the level of access we filter on child supplied tags.
594 For languages with protection on specific methods or slots,
595 it should strip out those not accessible by methods of TYPE.
596 An ACCESS of 'public means not in a method of a subclass of type.
597 A value of 'private means we can access private parts of the originating
600 (semantic-analyze-scoped-inherited-tag-map
602 (let* ((pname (semantic-tag-name p
))
603 (protection (semantic-tag-type-superclass-protection
606 (if (and (eq access
'public
) (not (eq protection
'public
)))
609 ;; We can get some parts of this type.
611 ;; Do not pull in inherited parts here. Those
612 ;; will come via the inherited-tag-map fcn
613 (semantic-analyze-scoped-type-parts
614 p scope t protection
))
619 (defun semantic-analyze-scoped-inherited-tag-map (type fcn scope
)
620 "Map all parents of TYPE to FCN. Return tags of all the types.
621 Argument SCOPE specify additional tags that are in scope
622 whose tags can be searched when needed, OR it may be a scope object."
623 (require 'semantic
/analyze
)
624 (let* (;; PARENTS specifies only the superclasses and not
625 ;; interfaces. Inheriting from an interfaces implies
626 ;; you have a copy of all methods locally. I think.
627 (parents (semantic-tag-type-superclasses type
))
633 ;; Create a SCOPE just for looking up the parent based on where
634 ;; the parent came from.
636 ;; @TODO - Should we cache these mini-scopes around in Emacs
637 ;; for recycling later? Should this become a helpful
639 (when (and parents
(semantic-tag-with-position-p type
))
641 ;; If TYPE has a position, go there and get the scope.
642 (semantic-go-to-tag type
)
644 ;; We need to make a mini scope, and only include the misc bits
645 ;; that will help in finding the parent. We don't really need
646 ;; to do any of the stuff related to variables and what-not.
647 (setq tmpscope
(semantic-scope-cache "mini"))
649 (scopetypes (cons type
(semantic-analyze-scoped-types (point))))
650 (parents (semantic-analyze-scope-nested-tags (point) scopetypes
))
651 ;;(parentinherited (semantic-analyze-scope-lineage-tags parents scopetypes))
654 (oset tmpscope scopetypes scopetypes
)
655 (oset tmpscope parents parents
)
656 ;;(oset tmpscope parentinheritance parentinherited)
658 (when (or scopetypes parents
)
659 (setq lscope
(semantic-analyze-scoped-tags scopetypes tmpscope
))
660 (oset tmpscope scope lscope
))
661 (oset tmpscope fullscope
(append scopetypes lscope parents
))
663 ;; END creating tmpscope
665 ;; Look up each parent one at a time.
667 (setq ps
(cond ((stringp p
) p
)
668 ((and (semantic-tag-p p
) (semantic-tag-prototype-p p
))
669 (semantic-tag-name p
))
670 ((and (listp p
) (stringp (car p
)))
672 pt
(condition-case nil
673 (or (semantic-analyze-find-tag ps
'type tmpscope
)
675 (semantic-analyze-find-tag ps
'type scope
))
680 ;; Note that we pass the original SCOPE in while recursing.
681 ;; so that the correct inheritance model is passed along.
682 (semantic-analyze-scoped-inherited-tag-map pt fcn scope
)
688 ;; Create the scope structure for use in the Analyzer.
691 (defun semantic-calculate-scope (&optional point
)
692 "Calculate the scope at POINT.
693 If POINT is not provided, then use the current location of point.
694 The class returned from the scope calculation is variable
695 `semantic-scope-cache'."
697 (if (not (and (featurep 'semantic
/db
) semanticdb-current-database
))
698 nil
;; Don't do anything...
699 (require 'semantic
/db-typecache
)
700 (if (not point
) (setq point
(point)))
701 (when (called-interactively-p 'any
)
702 (semantic-fetch-tags)
703 (semantic-scope-reset-cache))
706 (let* ((TAG (semantic-current-tag))
708 (semanticdb-cache-get semanticdb-current-table
709 'semantic-scope-cache
))
711 (when (not (semantic-equivalent-tag-p TAG
(oref scopecache tag
)))
712 (semantic-reset scopecache
))
713 (if (oref scopecache tag
)
714 ;; Even though we can recycle most of the scope, we
715 ;; need to redo the local variables since those change
716 ;; as you move about the tag.
718 (oset scopecache localvar
(semantic-get-all-local-variables))
722 (scopetypes (semantic-analyze-scoped-types point
))
723 (parents (semantic-analyze-scope-nested-tags point scopetypes
))
724 (parentinherited (semantic-analyze-scope-lineage-tags
727 (oset scopecache tag TAG
)
728 (oset scopecache scopetypes scopetypes
)
729 (oset scopecache parents parents
)
730 (oset scopecache parentinheritance parentinherited
)
733 (scope (when (or scopetypes parents
)
734 (semantic-analyze-scoped-tags scopetypes scopecache
))
737 (localargs (semantic-get-local-arguments))
738 (localvar (condition-case nil
739 (semantic-get-all-local-variables)
743 ;; Try looking for parents again.
744 (when (not parentinherited
)
745 (setq parentinherited
(semantic-analyze-scope-lineage-tags
746 parents
(append scopetypes scope
)))
747 (when parentinherited
748 (oset scopecache parentinheritance parentinherited
)
749 ;; Try calculating the scope again with the new inherited parent list.
750 (setq scope
(when (or scopetypes parents
)
751 (semantic-analyze-scoped-tags scopetypes scopecache
))
754 ;; Fill out the scope.
755 (oset scopecache scope scope
)
756 (oset scopecache fullscope
(append scopetypes scope parents
))
757 (oset scopecache localargs localargs
)
758 (oset scopecache localvar localvar
)
760 ;; Make sure we become dependent on the typecache.
761 (semanticdb-typecache-add-dependant scopecache
)
762 ;; Handy debug output.
763 (when (called-interactively-p 'any
)
764 (require 'eieio-datadebug
)
765 (data-debug-show scopecache
))
766 ;; Return ourselves, but make a clone first so that the caller
767 ;; can reset the scope cache without affecting others.
768 (clone scopecache
)))))
770 (defun semantic-scope-find (name &optional class scope-in
)
771 "Find the tag with NAME, and optional CLASS in the current SCOPE-IN.
772 Searches various elements of the scope for NAME. Return ALL the
773 hits in order, with the first tag being in the closest scope."
774 (let ((scope (or scope-in
(semantic-calculate-scope)))
776 ;; Is the passed in scope really a scope? if so, look through
777 ;; the options in that scope.
778 (if (semantic-scope-cache-p scope
)
780 ;; This should be first, but bugs in the
781 ;; C parser will turn function calls into
782 ;; assumed int return function prototypes. Yuck!
783 (semantic-find-tags-by-name name
(oref scope localargs
)))
785 (semantic-find-tags-by-name name
(oref scope localvar
)))
786 (fullscoperaw (oref scope fullscope
))
787 (sc (semantic-find-tags-by-name name fullscoperaw
))
788 (typescoperaw (oref scope typescope
))
789 (tsc (semantic-find-tags-by-name name typescoperaw
))
793 ;; Scan out things not of the right class.
794 (semantic-find-tags-by-class class
(append la lv sc tsc
))
795 (append la lv sc tsc
))
798 (when (and (not ans
) (or typescoperaw fullscoperaw
))
799 (let ((namesplit (semantic-analyze-split-name name
)))
800 (when (consp namesplit
)
801 ;; It may be we need to hack our way through type typescope.
804 (semantic-find-tags-by-name (car namesplit
)
806 (semantic-find-tags-by-name (car namesplit
)
810 (setq typescoperaw nil
)
811 (when (cdr namesplit
)
812 (setq typescoperaw
(semantic-tag-type-members
815 (setq namesplit
(cdr namesplit
)))
816 ;; Once done, store the current typecache lookup
817 (oset scope typescope
818 (append typescoperaw
(oref scope typescope
)))
822 ;; Not a real scope. Our scope calculation analyze parts of
823 ;; what it finds, and needs to pass lists through to do it's work.
824 ;; Tread that list as a singly entry.
826 (semantic-find-tags-by-class class scope
)
832 (cl-defmethod semantic-analyze-show ((context semantic-scope-cache
))
833 "Insert CONTEXT into the current buffer in a nice way."
834 (require 'semantic
/analyze
)
835 (semantic-analyze-princ-sequence (oref context scopetypes
) "-> ScopeTypes: " )
836 (semantic-analyze-princ-sequence (oref context parents
) "-> Parents: " )
837 (semantic-analyze-princ-sequence (oref context scope
) "-> Scope: " )
838 ;;(semantic-analyze-princ-sequence (oref context fullscope) "Fullscope: " )
839 (semantic-analyze-princ-sequence (oref context localargs
) "-> Local Args: " )
840 (semantic-analyze-princ-sequence (oref context localvar
) "-> Local Vars: " )
843 (provide 'semantic
/scope
)
846 ;; generated-autoload-file: "loaddefs.el"
847 ;; generated-autoload-load-name: "semantic/scope"
850 ;;; semantic/scope.el ends here