Add 2010 to copyright years.
[bpt/emacs.git] / lisp / cedet / semantic / scope.el
CommitLineData
20bfd709
CY
1;;; semantic/scope.el --- Analyzer Scope Calculations
2
114f9c96 3;; Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
20bfd709
CY
4
5;; Author: Eric M. Ludlam <eric@siege-engine.com>
6
7;; This file is part of GNU Emacs.
8
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.
13
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.
18
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/>.
21
22;;; Commentary:
23;;
24;; Calculate information about the current scope.
25;;
26;; Manages the current scope as a structure that can be cached on a
9bf6c65c 27;; per-file basis and recycled between different occurrences of
20bfd709
CY
28;; analysis on different parts of a file.
29;;
30;; Pattern for Scope Calculation
31;;
32;; Step 1: Calculate DataTypes in Scope:
33;;
34;; a) What is in scope via using statements or local namespaces
35;; b) Lineage of current context. Some names drawn from step 1.
36;;
37;; Step 2: Convert type names into lists of concrete tags
38;;
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.
42;;
43;; Step 3: Local variables
44;;
45;; a) Local variables are in the master search list.
46;;
47
48(require 'semantic/db)
49(require 'semantic/analyze/fcn)
50(require 'semantic/ctxt)
51
a60f2e7b 52(eval-when-compile (require 'semantic/find))
55b522b2
CY
53
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")
20bfd709
CY
59
60;;; Code:
61
62(defclass semantic-scope-cache (semanticdb-abstract-cache)
63 ((tag :initform nil
64 :documentation
65 "The tag this scope was calculated for.")
66 (scopetypes :initform nil
67 :documentation
68 "The list of types currently in scope.
69For C++, this would contain anonymous namespaces known, and
70anything labled by a `using' statement.")
71 (parents :initform nil
72 :documentation
73 "List of parents in scope w/in the body of this function.
74Presumably, the members of these parent classes are available for access
75based on private:, or public: style statements.")
76 (parentinheritance :initform nil
77 :documentation "Alist of parents by inheritance.
78Each entry is ( PARENT . PROTECTION ), where PARENT is a type, and
79PROTECTION is a symbol representing the level of inheritance, such as 'private, or 'protected.")
80 (scope :initform nil
81 :documentation
82 "Items in scope due to the scopetypes or parents.")
83 (fullscope :initform nil
84 :documentation
85 "All the other stuff on one master list you can search.")
86 (localargs :initform nil
87 :documentation
88 "The arguments to the function tag.")
89 (localvar :initform nil
90 :documentation
91 "The local variables.")
92 (typescope :initform nil
93 :documentation
94 "Slot to save intermediate scope while metatypes are dereferenced.")
95 )
96 "Cache used for storage of the current scope by the Semantic Analyzer.
97Saves scoping information between runs of the analyzer.")
98
99;;; METHODS
100;;
101;; Methods for basic management of the structure in semanticdb.
102;;
103(defmethod semantic-reset ((obj semantic-scope-cache))
104 "Reset OBJ back to it's empty settings."
105 (oset obj tag nil)
106 (oset obj scopetypes nil)
107 (oset obj parents nil)
108 (oset obj parentinheritance nil)
109 (oset obj scope nil)
110 (oset obj fullscope nil)
111 (oset obj localargs nil)
112 (oset obj localvar nil)
113 (oset obj typescope nil)
114 )
115
116(defmethod semanticdb-synchronize ((cache semantic-scope-cache)
117 new-tags)
118 "Synchronize a CACHE with some NEW-TAGS."
119 (semantic-reset cache))
120
121
122(defmethod semanticdb-partial-synchronize ((cache semantic-scope-cache)
123 new-tags)
124 "Synchronize a CACHE with some changed NEW-TAGS."
125 ;; If there are any includes or datatypes changed, then clear.
126 (if (or (semantic-find-tags-by-class 'include new-tags)
127 (semantic-find-tags-by-class 'type new-tags)
128 (semantic-find-tags-by-class 'using new-tags))
129 (semantic-reset cache))
130 )
131
132(defun semantic-scope-reset-cache ()
133 "Get the current cached scope, and reset it."
134 (when semanticdb-current-table
135 (let ((co (semanticdb-cache-get semanticdb-current-table
136 semantic-scope-cache)))
137 (semantic-reset co))))
138
139(defmethod semantic-scope-set-typecache ((cache semantic-scope-cache)
140 types-in-scope)
141 "Set the :typescope property on CACHE to some types.
142TYPES-IN-SCOPE is a list of type tags whos members are
143currently in scope. For each type in TYPES-IN-SCOPE,
144add those members to the types list.
145If nil, then the typescope is reset."
146 (let ((newts nil)) ;; New Type Scope
147 (dolist (onetype types-in-scope)
148 (setq newts (append (semantic-tag-type-members onetype)
149 newts))
150 )
151 (oset cache typescope newts)))
152
153;;; TAG SCOPES
154;;
155;; These fcns should be used by search routines that return a single
156;; tag which, in turn, may have come from a deep scope. The scope
157;; will be attached to the tag. Thus, in future scope based calls, a
158;; tag can be passed in and a scope derived from it.
159
160(defun semantic-scope-tag-clone-with-scope (tag scopetags)
161 "Close TAG, and return it. Add SCOPETAGS as a tag-local scope.
162Stores the SCOPETAGS as a set of tag properties on the cloned tag."
163 (let ((clone (semantic-tag-clone tag))
164 )
165 (semantic--tag-put-property clone 'scope scopetags)
166 ))
167
168(defun semantic-scope-tag-get-scope (tag)
169 "Get from TAG the list of tags comprising the scope from TAG."
170 (semantic--tag-get-property tag 'scope))
171
172;;; SCOPE UTILITIES
173;;
174;; Functions that do the main scope calculations
175
176
177(define-overloadable-function semantic-analyze-scoped-types (position)
178 "Return a list of types currently in scope at POSITION.
179This is based on what tags exist at POSITION, and any associated
180types available.")
181
182(defun semantic-analyze-scoped-types-default (position)
183 "Return a list of types currently in scope at POSITION.
184Use `semantic-ctxt-scoped-types' to find types."
55b522b2 185 (require 'semantic/db-typecache)
20bfd709
CY
186 (save-excursion
187 (goto-char position)
188 (let ((code-scoped-types nil))
189 ;; Lets ask if any types are currently scoped. Scoped
190 ;; classes and types provide their public methods and types
191 ;; in source code, but are unrelated hierarchically.
192 (let ((sp (semantic-ctxt-scoped-types)))
193 (while sp
194 ;; Get this thing as a tag
195 (let ((tmp (cond
196 ((stringp (car sp))
197 (semanticdb-typecache-find (car sp)))
198 ;(semantic-analyze-find-tag (car sp) 'type))
199 ((semantic-tag-p (car sp))
200 (if (semantic-analyze-tag-prototype-p (car sp))
201 (semanticdb-typecache-find (semantic-tag-name (car sp)))
202 ;;(semantic-analyze-find-tag (semantic-tag-name (car sp)) 'type)
203 (car sp)))
204 (t nil))))
205 (when tmp
206 (setq code-scoped-types
207 (cons tmp code-scoped-types))))
208 (setq sp (cdr sp))))
209 (setq code-scoped-types (nreverse code-scoped-types))
210
211 (when code-scoped-types
212 (semanticdb-typecache-merge-streams code-scoped-types nil))
213
214 )))
215
216;;------------------------------------------------------------
217(define-overloadable-function semantic-analyze-scope-nested-tags (position scopedtypes)
218 "Return a list of types in order of nesting for the context of POSITION.
219If POSITION is in a method with a named parent, find that parent, and
220identify it's scope via overlay instead.
221Optional SCOPETYPES are additional scoped entities in which our parent might
222be found.")
223
224(defun semantic-analyze-scope-nested-tags-default (position scopetypes)
225 "Return a list of types in order of nesting for the context of POSITION.
226If POSITION is in a method with a named parent, find that parent, and
227identify it's scope via overlay instead.
228Optional SCOPETYPES are additional scoped entities in which our parent might
229be found.
230This only finds ONE immediate parent by name. All other parents returned
231are from nesting data types."
55b522b2 232 (require 'semantic/analyze)
20bfd709
CY
233 (save-excursion
234 (if position (goto-char position))
235 (let* ((stack (reverse (semantic-find-tag-by-overlay (point))))
236 (tag (car stack))
237 (pparent (car (cdr stack)))
238 (returnlist nil)
239 )
240 ;; In case of arg lists or some-such, throw out non-types.
241 (while (and stack (not (semantic-tag-of-class-p pparent 'type)))
242 (setq stack (cdr stack)
243 pparent (car (cdr stack))))
244
245 ;; Step 1:
246 ;; Analyze the stack of tags we are nested in as parents.
247 ;;
248
249 ;; If we have a pparent tag, lets go there
250 ;; an analyze that stack of tags.
251 (when (and pparent (semantic-tag-with-position-p pparent))
252 (semantic-go-to-tag pparent)
253 (setq stack (semantic-find-tag-by-overlay (point)))
254 ;; Step one, find the merged version of stack in the typecache.
255 (let* ((stacknames (reverse (mapcar 'semantic-tag-name stack)))
256 (tc nil)
257 )
258 ;; @todo - can we use the typecache ability to
259 ;; put a scope into a tag to do this?
260 (while (and stacknames
261 (setq tc (semanticdb-typecache-find
262 (reverse stacknames))))
263 (setq returnlist (cons tc returnlist)
264 stacknames (cdr stacknames)))
265 (when (not returnlist)
266 ;; When there was nothing from the typecache, then just
267 ;; use what's right here.
268 (setq stack (reverse stack))
269 ;; Add things to STACK until we cease finding tags of class type.
270 (while (and stack (eq (semantic-tag-class (car stack)) 'type))
271 ;; Otherwise, just add this to the returnlist.
272 (setq returnlist (cons (car stack) returnlist))
273 (setq stack (cdr stack)))
274
275 (setq returnlist (nreverse returnlist))
276 ))
277 )
278
279 ;; Only do this level of analysis for functions.
280 (when (eq (semantic-tag-class tag) 'function)
281 ;; Step 2:
282 ;; If the function tag itself has a "parent" by name, then that
283 ;; parent will exist in the scope we just calculated, so look it
284 ;; up now.
285 ;;
286 (let ((p (semantic-tag-function-parent tag)))
287 (when p
288 ;; We have a parent, search for it.
289 (let* ((searchnameraw (cond ((stringp p) p)
290 ((semantic-tag-p p)
291 (semantic-tag-name p))
292 ((and (listp p) (stringp (car p)))
293 (car p))))
294 (searchname (semantic-analyze-split-name searchnameraw))
295 (snlist (if (consp searchname)
296 searchname
297 (list searchname)))
298 (fullsearchname nil)
299
300 (miniscope (semantic-scope-cache "mini"))
301 ptag)
302
303 ;; Find the next entry in the refereneced type for
304 ;; our function, and append to return list till our
305 ;; returnlist is empty.
306 (while snlist
307 (setq fullsearchname
308 (append (mapcar 'semantic-tag-name returnlist)
309 (list (car snlist)))) ;; Next one
310 (setq ptag
311 (semanticdb-typecache-find fullsearchname))
312
313 (when (or (not ptag)
314 (not (semantic-tag-of-class-p ptag 'type)))
315 (let ((rawscope
316 (apply 'append
317 (mapcar 'semantic-tag-type-members
318 (cons (car returnlist) scopetypes)
319 )))
320 )
321 (oset miniscope parents returnlist) ;; Not really accurate, but close
322 (oset miniscope scope rawscope)
323 (oset miniscope fullscope rawscope)
324 (setq ptag
325 (semantic-analyze-find-tag searchnameraw
326 'type
327 miniscope
328 ))
329 ))
330
331 (when ptag
332 (when (and (not (semantic-tag-p ptag))
333 (semantic-tag-p (car ptag)))
334 (setq ptag (car ptag)))
335 (setq returnlist (append returnlist (list ptag)))
336 )
337
338 (setq snlist (cdr snlist)))
339 (setq returnlist returnlist)
340 )))
341 )
342 returnlist
343 )))
344
345(define-overloadable-function semantic-analyze-scope-lineage-tags (parents scopedtypes)
346 "Return the full lineage of tags from PARENTS.
347The return list is of the form ( TAG . PROTECTION ), where TAG is a tag,
348and PROTECTION is the level of protection offered by the relationship.
349Optional SCOPETYPES are additional scoped entities in which our parent might
350be found.")
351
352(defun semantic-analyze-scope-lineage-tags-default (parents scopetypes)
353 "Return the full lineage of tags from PARENTS.
354The return list is of the form ( TAG . PROTECTION ), where TAG is a tag,
355and PROTECTION is the level of protection offered by the relationship.
356Optional SCOPETYPES are additional scoped entities in which our parent might
357be found."
358 (let ((lineage nil)
359 (miniscope (semantic-scope-cache "mini"))
360 )
361 (oset miniscope parents parents)
362 (oset miniscope scope scopetypes)
363 (oset miniscope fullscope scopetypes)
364
365 (dolist (slp parents)
366 (semantic-analyze-scoped-inherited-tag-map
367 slp (lambda (newparent)
368 (let* ((pname (semantic-tag-name newparent))
369 (prot (semantic-tag-type-superclass-protection slp pname))
370 (effectiveprot (cond ((eq prot 'public)
371 ;; doesn't provide access to private slots?
372 'protected)
373 (t prot))))
374 (push (cons newparent effectiveprot) lineage)
375 ))
376 miniscope))
377
378 lineage))
379
380
381;;------------------------------------------------------------
382
383(define-overloadable-function semantic-analyze-scoped-tags (typelist parentlist)
384 "Return accessable tags when TYPELIST and PARENTLIST is in scope.
385Tags returned are not in the global name space, but are instead
386scoped inside a class or namespace. Such items can be referenced
387without use of \"object.function()\" style syntax due to an
388implicit \"object\".")
389
390(defun semantic-analyze-scoped-tags-default (typelist halfscope)
9bf6c65c 391 "Return accessible tags when TYPELIST and HALFSCOPE is in scope.
20bfd709
CY
392HALFSCOPE is the current scope partially initialized.
393Tags returned are not in the global name space, but are instead
394scoped inside a class or namespace. Such items can be referenced
395without use of \"object.function()\" style syntax due to an
396implicit \"object\"."
397 (let ((typelist2 nil)
398 (currentscope nil)
399 (parentlist (oref halfscope parents))
400 (miniscope halfscope)
401 )
402 ;; Loop over typelist, and find and merge all namespaces matching
403 ;; the names in typelist.
404 (while typelist
405 (let ((tt (semantic-tag-type (car typelist))))
406 (when (and (stringp tt) (string= tt "namespace"))
407 ;; By using the typecache, our namespaces are pre-merged.
408 (setq typelist2 (cons (car typelist) typelist2))
409 ))
410 (setq typelist (cdr typelist)))
411
412 ;; Loop over the types (which should be sorted by postion
413 ;; adding to the scopelist as we go, and using the scopelist
414 ;; for additional searching!
415 (while typelist2
416 (oset miniscope scope currentscope)
417 (oset miniscope fullscope currentscope)
418 (setq currentscope (append
419 (semantic-analyze-scoped-type-parts (car typelist2)
420 miniscope)
421 currentscope))
422 (setq typelist2 (cdr typelist2)))
423
424 ;; Collect all the types (class, etc) that are in our heratage.
425 ;; These are types that we can extract members from, not those
426 ;; delclared in using statements, or the like.
427 ;; Get the PARENTS including nesting scope for this location.
428 (while parentlist
429 (oset miniscope scope currentscope)
430 (oset miniscope fullscope currentscope)
431 (setq currentscope (append
432 (semantic-analyze-scoped-type-parts (car parentlist)
433 miniscope)
434 currentscope))
435 (setq parentlist (cdr parentlist)))
436
437 ;; Loop over all the items, and collect any type constants.
438 (let ((constants nil))
439 (dolist (T currentscope)
440 (setq constants (append constants
441 (semantic-analyze-type-constants T)))
442 )
443
444 (setq currentscope (append currentscope constants)))
445
446 currentscope))
447
448;;------------------------------------------------------------
449(define-overloadable-function semantic-analyze-scope-calculate-access (type scope)
450 "Calculate the access class for TYPE as defined by the current SCOPE.
451Access is related to the :parents in SCOPE. If type is a member of SCOPE
452then access would be 'private. If TYPE is inherited by a member of SCOPE,
453the access would be 'protected. Otherwise, access is 'public")
454
455(defun semantic-analyze-scope-calculate-access-default (type scope)
456 "Calculate the access class for TYPE as defined by the current SCOPE."
457 (cond ((semantic-scope-cache-p scope)
458 (let ((parents (oref scope parents))
459 (parentsi (oref scope parentinheritance))
460 )
461 (catch 'moose
462 ;; Investigate the parent, and see how it relates to type.
463 ;; If these tags are basically the same, then we have full access.
464 (dolist (p parents)
465 (when (semantic-tag-similar-p type p)
466 (throw 'moose 'private))
467 )
468 ;; Look to see if type is in our list of inherited parents.
469 (dolist (pi parentsi)
470 ;; pi is a cons cell ( PARENT . protection)
471 (let ((pip (car pi))
472 (piprot (cdr pi)))
473 (when (semantic-tag-similar-p type pip)
474 (throw 'moose
475 ;; protection via inheritance means to pull out different
476 ;; bits based on protection labels in an opposite way.
477 (cdr (assoc piprot
478 '((public . private)
479 (protected . protected)
480 (private . public))))
481 )))
482 )
483 ;; Not in our parentage. Is type a FRIEND?
484 (let ((friends (semantic-find-tags-by-class 'friend (semantic-tag-type-members type))))
485 (dolist (F friends)
486 (dolist (pi parents)
487 (if (string= (semantic-tag-name F) (semantic-tag-name pi))
488 (throw 'moose 'private))
489 )))
490 ;; Found nothing, return public
491 'public)
492 ))
493 (t 'public)))
494
495(defun semantic-completable-tags-from-type (type)
496 "Return a list of slots that are valid completions from the list of SLOTS.
497If a tag in SLOTS has a named parent, then that implies that the
498tag is not something you can complete from within TYPE."
499 (let ((allslots (semantic-tag-components type))
500 (leftover nil)
501 )
502 (dolist (S allslots)
503 (when (or (not (semantic-tag-of-class-p S 'function))
504 (not (semantic-tag-function-parent S)))
505 (setq leftover (cons S leftover)))
506 )
507 (nreverse leftover)))
508
509(defun semantic-analyze-scoped-type-parts (type &optional scope noinherit protection)
510 "Return all parts of TYPE, a tag representing a TYPE declaration.
511SCOPE is the scope object.
512NOINHERIT turns off searching of inherited tags.
513PROTECTION specifies the type of access requested, such as 'public or 'private."
514 (if (not type)
515 nil
516 (let* ((access (semantic-analyze-scope-calculate-access type scope))
517 ;; SLOTS are the slots directly a part of TYPE.
518 (allslots (semantic-completable-tags-from-type type))
519 (slots (semantic-find-tags-by-scope-protection
520 access
521 type allslots))
522 (fname (semantic-tag-file-name type))
523 ;; EXTMETH are externally defined methods that are still
524 ;; a part of this class.
525
526 ;; @TODO - is this line needed?? Try w/out for a while
527 ;; @note - I think C++ says no. elisp might, but methods
528 ;; look like defuns, so it makes no difference.
529 (extmeth nil) ; (semantic-tag-external-member-children type t))
530
531 ;; INHERITED are tags found in classes that our TYPE tag
532 ;; inherits from. Do not do this if it was not requested.
533 (inherited (when (not noinherit)
534 (semantic-analyze-scoped-inherited-tags type scope
535 access)))
536 )
537 (when (not (semantic-tag-in-buffer-p type))
538 (let ((copyslots nil))
539 (dolist (TAG slots)
540 ;;(semantic--tag-put-property TAG :filename fname)
541 (if (semantic-tag-file-name TAG)
542 ;; If it has a filename, just go with it...
543 (setq copyslots (cons TAG copyslots))
544 ;; Otherwise, copy the tag w/ the guessed filename.
545 (setq copyslots (cons (semantic-tag-copy TAG nil fname)
546 copyslots)))
547 )
548 (setq slots (nreverse copyslots))
549 ))
550 ;; Flatten the database output.
551 (append slots extmeth inherited)
552 )))
553
554(defun semantic-analyze-scoped-inherited-tags (type scope access)
555 "Return all tags that TYPE inherits from.
556Argument SCOPE specify additional tags that are in scope
557whose tags can be searched when needed, OR it may be a scope object.
558ACCESS is the level of access we filter on child supplied tags.
9bf6c65c
GM
559For languages with protection on specific methods or slots,
560it should strip out those not accessible by methods of TYPE.
20bfd709
CY
561An ACCESS of 'public means not in a method of a subclass of type.
562A value of 'private means we can access private parts of the originating
563type."
564 (let ((ret nil))
565 (semantic-analyze-scoped-inherited-tag-map
566 type (lambda (p)
567 (let* ((pname (semantic-tag-name p))
568 (protection (semantic-tag-type-superclass-protection
569 type pname))
570 )
571 (if (and (eq access 'public) (not (eq protection 'public)))
572 nil ;; Don't do it.
573
574 ;; We can get some parts of this type.
575 (setq ret (nconc ret
576 ;; Do not pull in inherited parts here. Those
577 ;; will come via the inherited-tag-map fcn
578 (semantic-analyze-scoped-type-parts
579 p scope t protection))
580 ))))
581 scope)
582 ret))
583
584(defun semantic-analyze-scoped-inherited-tag-map (type fcn scope)
585 "Map all parents of TYPE to FCN. Return tags of all the types.
586Argument SCOPE specify additional tags that are in scope
587whose tags can be searched when needed, OR it may be a scope object."
55b522b2 588 (require 'semantic/analyze)
20bfd709
CY
589 (let* (;; PARENTS specifies only the superclasses and not
590 ;; interfaces. Inheriting from an interfaces implies
591 ;; you have a copy of all methods locally. I think.
592 (parents (semantic-tag-type-superclasses type))
593 ps pt
594 (tmpscope scope)
595 )
596 (save-excursion
597
598 ;; Create a SCOPE just for looking up the parent based on where
599 ;; the parent came from.
600 ;;
601 ;; @TODO - Should we cache these mini-scopes around in Emacs
602 ;; for recycling later? Should this become a helpful
603 ;; extra routine?
604 (when (and parents (semantic-tag-with-position-p type))
67d3ffe4
CY
605 (save-excursion
606 ;; If TYPE has a position, go there and get the scope.
607 (semantic-go-to-tag type)
608
609 ;; We need to make a mini scope, and only include the misc bits
610 ;; that will help in finding the parent. We don't really need
611 ;; to do any of the stuff related to variables and what-not.
612 (setq tmpscope (semantic-scope-cache "mini"))
613 (let* ( ;; Step 1:
614 (scopetypes (semantic-analyze-scoped-types (point)))
615 (parents (semantic-analyze-scope-nested-tags (point) scopetypes))
616 ;;(parentinherited (semantic-analyze-scope-lineage-tags parents scopetypes))
617 (lscope nil)
618 )
619 (oset tmpscope scopetypes scopetypes)
620 (oset tmpscope parents parents)
621 ;;(oset tmpscope parentinheritance parentinherited)
622
623 (when (or scopetypes parents)
624 (setq lscope (semantic-analyze-scoped-tags scopetypes tmpscope))
625 (oset tmpscope scope lscope))
626 (oset tmpscope fullscope (append scopetypes lscope parents))
627 )))
20bfd709
CY
628 ;; END creating tmpscope
629
630 ;; Look up each parent one at a time.
631 (dolist (p parents)
632 (setq ps (cond ((stringp p) p)
633 ((and (semantic-tag-p p) (semantic-tag-prototype-p p))
634 (semantic-tag-name p))
635 ((and (listp p) (stringp (car p)))
636 p))
637 pt (condition-case nil
638 (or (semantic-analyze-find-tag ps 'type tmpscope)
639 ;; A backup hack.
640 (semantic-analyze-find-tag ps 'type scope))
641 (error nil)))
642
643 (when pt
644 (funcall fcn pt)
645 ;; Note that we pass the original SCOPE in while recursing.
646 ;; so that the correct inheritance model is passed along.
647 (semantic-analyze-scoped-inherited-tag-map pt fcn scope)
648 )))
649 nil))
650
651;;; ANALYZER
652;;
653;; Create the scope structure for use in the Analyzer.
654;;
638e74d1 655;;;###autoload
20bfd709
CY
656(defun semantic-calculate-scope (&optional point)
657 "Calculate the scope at POINT.
658If POINT is not provided, then use the current location of point.
659The class returned from the scope calculation is variable
660`semantic-scope-cache'."
661 (interactive)
55b522b2 662 (if (not (and (featurep 'semantic/db) semanticdb-current-database))
20bfd709 663 nil ;; Don't do anything...
55b522b2 664 (require 'semantic/db-typecache)
20bfd709 665 (if (not point) (setq point (point)))
2054a44c 666 (when (called-interactively-p 'any)
20bfd709 667 (semantic-fetch-tags)
2054a44c 668 (semantic-scope-reset-cache))
20bfd709
CY
669 (save-excursion
670 (goto-char point)
671 (let* ((TAG (semantic-current-tag))
672 (scopecache
673 (semanticdb-cache-get semanticdb-current-table
674 semantic-scope-cache))
675 )
676 (when (not (semantic-equivalent-tag-p TAG (oref scopecache tag)))
677 (semantic-reset scopecache))
678 (if (oref scopecache tag)
679 ;; Even though we can recycle most of the scope, we
680 ;; need to redo the local variables since those change
681 ;; as you move about the tag.
682 (condition-case nil
683 (oset scopecache localvar (semantic-get-all-local-variables))
684 (error nil))
685
686 (let* (;; Step 1:
687 (scopetypes (semantic-analyze-scoped-types point))
688 (parents (semantic-analyze-scope-nested-tags point scopetypes))
689 (parentinherited (semantic-analyze-scope-lineage-tags
690 parents scopetypes))
691 )
692 (oset scopecache tag TAG)
693 (oset scopecache scopetypes scopetypes)
694 (oset scopecache parents parents)
695 (oset scopecache parentinheritance parentinherited)
696
697 (let* (;; Step 2:
698 (scope (when (or scopetypes parents)
699 (semantic-analyze-scoped-tags scopetypes scopecache))
700 )
701 ;; Step 3:
702 (localargs (semantic-get-local-arguments))
703 (localvar (condition-case nil
704 (semantic-get-all-local-variables)
705 (error nil)))
706 )
707
708 ;; Try looking for parents again.
709 (when (not parentinherited)
710 (setq parentinherited (semantic-analyze-scope-lineage-tags
711 parents (append scopetypes scope)))
712 (when parentinherited
713 (oset scopecache parentinheritance parentinherited)
714 ;; Try calculating the scope again with the new inherited parent list.
715 (setq scope (when (or scopetypes parents)
716 (semantic-analyze-scoped-tags scopetypes scopecache))
717 )))
718
719 ;; Fill out the scope.
720 (oset scopecache scope scope)
721 (oset scopecache fullscope (append scopetypes scope parents))
722 (oset scopecache localargs localargs)
723 (oset scopecache localvar localvar)
724 )))
725 ;; Make sure we become dependant on the typecache.
726 (semanticdb-typecache-add-dependant scopecache)
727 ;; Handy debug output.
2054a44c 728 (when (called-interactively-p 'any)
55b522b2 729 (require 'eieio-datadebug)
2054a44c 730 (data-debug-show scopecache))
20bfd709
CY
731 ;; Return ourselves
732 scopecache))))
733
734(defun semantic-scope-find (name &optional class scope-in)
9bf6c65c 735 "Find the tag with NAME, and optional CLASS in the current SCOPE-IN.
20bfd709
CY
736Searches various elements of the scope for NAME. Return ALL the
737hits in order, with the first tag being in the closest scope."
738 (let ((scope (or scope-in (semantic-calculate-scope)))
739 (ans nil))
740 ;; Is the passed in scope really a scope? if so, look through
741 ;; the options in that scope.
742 (if (semantic-scope-cache-p scope)
743 (let* ((la
744 ;; This should be first, but bugs in the
745 ;; C parser will turn function calls into
746 ;; assumed int return function prototypes. Yuck!
747 (semantic-find-tags-by-name name (oref scope localargs)))
748 (lv
749 (semantic-find-tags-by-name name (oref scope localvar)))
750 (fullscoperaw (oref scope fullscope))
751 (sc (semantic-find-tags-by-name name fullscoperaw))
752 (typescoperaw (oref scope typescope))
753 (tsc (semantic-find-tags-by-name name typescoperaw))
754 )
755 (setq ans
756 (if class
757 ;; Scan out things not of the right class.
758 (semantic-find-tags-by-class class (append la lv sc tsc))
759 (append la lv sc tsc))
760 )
761
762 (when (and (not ans) (or typescoperaw fullscoperaw))
763 (let ((namesplit (semantic-analyze-split-name name)))
764 (when (consp namesplit)
765 ;; It may be we need to hack our way through type typescope.
766 (while namesplit
767 (setq ans (append
768 (semantic-find-tags-by-name (car namesplit)
769 typescoperaw)
770 (semantic-find-tags-by-name (car namesplit)
771 fullscoperaw)
772 ))
773 (if (not ans)
774 (setq typescoperaw nil)
775 (when (cdr namesplit)
776 (setq typescoperaw (semantic-tag-type-members
777 (car ans)))))
778
779 (setq namesplit (cdr namesplit)))
780 ;; Once done, store the current typecache lookup
781 (oset scope typescope
782 (append typescoperaw (oref scope typescope)))
783 )))
784 ;; Return it.
785 ans)
786 ;; Not a real scope. Our scope calculation analyze parts of
787 ;; what it finds, and needs to pass lists through to do it's work.
788 ;; Tread that list as a singly entry.
789 (if class
790 (semantic-find-tags-by-class class scope)
791 scope)
792 )))
793
794;;; DUMP
795;;
796(defmethod semantic-analyze-show ((context semantic-scope-cache))
797 "Insert CONTEXT into the current buffer in a nice way."
0a3b3f9e 798 (require 'semantic/analyze)
20bfd709
CY
799 (semantic-analyze-princ-sequence (oref context scopetypes) "-> ScopeTypes: " )
800 (semantic-analyze-princ-sequence (oref context parents) "-> Parents: " )
801 (semantic-analyze-princ-sequence (oref context scope) "-> Scope: " )
802 ;;(semantic-analyze-princ-sequence (oref context fullscope) "Fullscope: " )
803 (semantic-analyze-princ-sequence (oref context localargs) "-> Local Args: " )
804 (semantic-analyze-princ-sequence (oref context localvar) "-> Local Vars: " )
805 )
806
807(provide 'semantic/scope)
808
638e74d1
CY
809;; Local variables:
810;; generated-autoload-file: "loaddefs.el"
638e74d1
CY
811;; generated-autoload-load-name: "semantic/scope"
812;; End:
813
3999968a 814;; arch-tag: 056ab514-3e28-4d6e-84ed-9283dce5a01e
20bfd709 815;;; semantic/scope.el ends here