Merge branch 'master' into comment-cache
[emacs.git] / lisp / cedet / semantic / scope.el
blob9bade56965939549c59ae4efa3f476b34b84a3a0
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 <http://www.gnu.org/licenses/>.
22 ;;; Commentary:
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")
61 ;;; Code:
63 (defclass semantic-scope-cache (semanticdb-abstract-cache)
64 ((tag :initform nil
65 :documentation
66 "The tag this scope was calculated for.")
67 (scopetypes :initform nil
68 :documentation
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
73 :documentation
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.")
81 (scope :initform nil
82 :documentation
83 "Items in scope due to the scopetypes or parents.")
84 (fullscope :initform nil
85 :documentation
86 "All the other stuff on one master list you can search.")
87 (localargs :initform nil
88 :documentation
89 "The arguments to the function tag.")
90 (localvar :initform nil
91 :documentation
92 "The local variables.")
93 (typescope :initform nil
94 :documentation
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.")
100 ;;; METHODS
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."
106 (oset obj tag nil)
107 (oset obj scopetypes nil)
108 (oset obj parents nil)
109 (oset obj parentinheritance nil)
110 (oset obj scope 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)
118 new-tags)
119 "Synchronize a CACHE with some NEW-TAGS."
120 (semantic-reset cache))
123 (cl-defmethod semanticdb-partial-synchronize ((cache semantic-scope-cache)
124 new-tags)
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)
141 types-in-scope)
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)
150 newts))
152 (oset cache typescope newts)))
154 ;;; TAG SCOPES
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))
173 ;;; SCOPE UTILITIES
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
181 types available.")
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)
187 (save-excursion
188 (goto-char position)
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)))
194 (while sp
195 ;; Get this thing as a tag
196 (let ((tmp (cond
197 ((stringp (car sp))
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
202 (car sp)
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))
209 code-scoped-types)))
210 (car sp)))
211 (t nil))))
212 (when tmp
213 (setq code-scoped-types
214 (cons tmp code-scoped-types))))
215 (setq sp (cdr sp))))
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
229 be found.")
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
236 be found.
237 This only finds ONE immediate parent by name. All other parents returned
238 are from nesting data types."
239 (require 'semantic/analyze)
240 (save-excursion
241 (if position (goto-char position))
242 (let* ((stack (reverse (semantic-find-tag-by-overlay (point))))
243 (tag (car stack))
244 (pparent (car (cdr stack)))
245 (returnlist nil)
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))))
251 ;; Remove duplicates
252 (while (member pparent scopetypes)
253 (setq stack (cdr stack) pparent (car (cdr stack))))
255 ;; Step 1:
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)))
266 (tc nil)
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)
293 ;; Step 2:
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
296 ;; up now.
298 (let ((p (semantic-tag-function-parent tag)))
299 (when p
300 ;; We have a parent, search for it.
301 (let* ((searchnameraw (cond ((stringp p) p)
302 ((semantic-tag-p p)
303 (semantic-tag-name p))
304 ((and (listp p) (stringp (car p)))
305 (car p))))
306 (searchname (semantic-analyze-split-name searchnameraw))
307 (snlist (if (consp searchname)
308 searchname
309 (list searchname)))
310 (fullsearchname nil)
312 (miniscope (semantic-scope-cache "mini"))
313 ptag)
315 ;; Find the next entry in the referenced type for
316 ;; our function, and append to return list till our
317 ;; returnlist is empty.
318 (while snlist
319 (setq fullsearchname
320 (append (mapcar 'semantic-tag-name returnlist)
321 (list (car snlist)))) ;; Next one
322 (setq ptag
323 (semanticdb-typecache-find fullsearchname))
325 (when (or (not ptag)
326 (not (semantic-tag-of-class-p ptag 'type)))
327 (let ((rawscope
328 (apply 'append
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)
336 (setq ptag
337 (semantic-analyze-find-tag searchnameraw
338 'type
339 miniscope
343 (when ptag
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)
354 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
362 be found.")
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
369 be found."
370 (let ((lineage nil)
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?
384 'protected)
385 (t prot))))
386 (push (cons newparent effectiveprot) lineage)
388 miniscope))
390 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)
410 (currentscope 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.
416 (while 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!
427 (while typelist2
428 (oset miniscope scope currentscope)
429 (oset miniscope fullscope currentscope)
430 (setq currentscope (append
431 (semantic-analyze-scoped-type-parts (car typelist2)
432 miniscope)
433 currentscope))
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.
440 (while parentlist
441 (oset miniscope scope currentscope)
442 (oset miniscope fullscope currentscope)
443 (setq currentscope (append
444 (semantic-analyze-scoped-type-parts (car parentlist)
445 miniscope)
446 currentscope))
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)))
458 currentscope))
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))
473 (catch 'moose
474 ;; Investigate the parent, and see how it relates to type.
475 ;; If these tags are basically the same, then we have full access.
476 (dolist (p parents)
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)
483 (let ((pip (car pi))
484 (piprot (cdr pi)))
485 (when (semantic-tag-similar-p type pip)
486 (throw 'moose
487 ;; protection via inheritance means to pull out different
488 ;; bits based on protection labels in an opposite way.
489 (cdr (assoc piprot
490 '((public . private)
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))))
497 (dolist (F friends)
498 (dolist (pi parents)
499 (if (string= (semantic-tag-name F) (semantic-tag-name pi))
500 (throw 'moose 'private))
502 ;; Found nothing, return public
503 'public)
505 (t '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))
512 (leftover nil)
514 (dolist (S allslots)
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
523 (usingtype (or
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))))
529 (when usingtype
530 ;; Use recursion to examine that namespace or class
531 (let ((tags (semantic-completable-tags-from-type usingtype)))
532 (if filename
533 ;; If we have a filename, copy the tags with it
534 (dolist (cur tags)
535 (setq leftover (cons (semantic-tag-copy cur nil filename)
536 leftover)))
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."
549 (if (not type)
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
555 access
556 type allslots))
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
570 access)))
572 (when (not (semantic-tag-in-buffer-p type))
573 (let ((copyslots nil))
574 (dolist (TAG slots)
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)
581 copyslots)))
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
598 type."
599 (let ((ret nil))
600 (semantic-analyze-scoped-inherited-tag-map
601 type (lambda (p)
602 (let* ((pname (semantic-tag-name p))
603 (protection (semantic-tag-type-superclass-protection
604 type pname))
606 (if (and (eq access 'public) (not (eq protection 'public)))
607 nil ;; Don't do it.
609 ;; We can get some parts of this type.
610 (setq ret (nconc ret
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))
615 ))))
616 scope)
617 ret))
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))
628 ps pt
629 (tmpscope scope)
631 (save-excursion
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
638 ;; extra routine?
639 (when (and parents (semantic-tag-with-position-p type))
640 (save-excursion
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"))
648 (let* ( ;; Step 1:
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))
652 (lscope nil)
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.
666 (dolist (p parents)
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)
674 ;; A backup hack.
675 (semantic-analyze-find-tag ps 'type scope))
676 (error nil)))
678 (when pt
679 (funcall fcn pt)
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)
684 nil))
686 ;;; ANALYZER
688 ;; Create the scope structure for use in the Analyzer.
690 ;;;###autoload
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'."
696 (interactive)
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))
704 (save-excursion
705 (goto-char point)
706 (let* ((TAG (semantic-current-tag))
707 (scopecache
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.
717 (condition-case nil
718 (oset scopecache localvar (semantic-get-all-local-variables))
719 (error nil))
721 (let* (;; Step 1:
722 (scopetypes (semantic-analyze-scoped-types point))
723 (parents (semantic-analyze-scope-nested-tags point scopetypes))
724 (parentinherited (semantic-analyze-scope-lineage-tags
725 parents scopetypes))
727 (oset scopecache tag TAG)
728 (oset scopecache scopetypes scopetypes)
729 (oset scopecache parents parents)
730 (oset scopecache parentinheritance parentinherited)
732 (let* (;; Step 2:
733 (scope (when (or scopetypes parents)
734 (semantic-analyze-scoped-tags scopetypes scopecache))
736 ;; Step 3:
737 (localargs (semantic-get-local-arguments))
738 (localvar (condition-case nil
739 (semantic-get-all-local-variables)
740 (error nil)))
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)))
775 (ans nil))
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)
779 (let* ((la
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))
791 (setq ans
792 (if class
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.
802 (while namesplit
803 (setq ans (append
804 (semantic-find-tags-by-name (car namesplit)
805 typescoperaw)
806 (semantic-find-tags-by-name (car namesplit)
807 fullscoperaw)
809 (if (not ans)
810 (setq typescoperaw nil)
811 (when (cdr namesplit)
812 (setq typescoperaw (semantic-tag-type-members
813 (car ans)))))
815 (setq namesplit (cdr namesplit)))
816 ;; Once done, store the current typecache lookup
817 (oset scope typescope
818 (append typescoperaw (oref scope typescope)))
820 ;; Return it.
821 ans)
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.
825 (if class
826 (semantic-find-tags-by-class class scope)
827 scope)
830 ;;; DUMP
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)
845 ;; Local variables:
846 ;; generated-autoload-file: "loaddefs.el"
847 ;; generated-autoload-load-name: "semantic/scope"
848 ;; End:
850 ;;; semantic/scope.el ends here