Add 2012 to FSF copyright years for Emacs files
[bpt/emacs.git] / lisp / cedet / semantic / scope.el
1 ;;; semantic/scope.el --- Analyzer Scope Calculations
2
3 ;; Copyright (C) 2007-2012 Free Software Foundation, Inc.
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
27 ;; per-file basis and recycled between different occurrences of
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
52 (eval-when-compile (require 'semantic/find))
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")
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.
69 For C++, this would contain anonymous namespaces known, and
70 anything labeled by a `using' statement.")
71 (parents :initform nil
72 :documentation
73 "List of parents in scope w/in the body of this function.
74 Presumably, the members of these parent classes are available for access
75 based on private:, or public: style statements.")
76 (parentinheritance :initform nil
77 :documentation "Alist of parents by inheritance.
78 Each entry is ( PARENT . PROTECTION ), where PARENT is a type, and
79 PROTECTION 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.
97 Saves 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.
142 TYPES-IN-SCOPE is a list of type tags whos members are
143 currently in scope. For each type in TYPES-IN-SCOPE,
144 add those members to the types list.
145 If 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.
162 Stores 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.
179 This is based on what tags exist at POSITION, and any associated
180 types available.")
181
182 (defun semantic-analyze-scoped-types-default (position)
183 "Return a list of types currently in scope at POSITION.
184 Use `semantic-ctxt-scoped-types' to find types."
185 (require 'semantic/db-typecache)
186 (save-excursion
187 (goto-char position)
188 (let ((code-scoped-types nil))
189 ;; Let's 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.
219 If POSITION is in a method with a named parent, find that parent, and
220 identify it's scope via overlay instead.
221 Optional SCOPETYPES are additional scoped entities in which our parent might
222 be 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.
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.
230 This only finds ONE immediate parent by name. All other parents returned
231 are from nesting data types."
232 (require 'semantic/analyze)
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) pparent (car (cdr stack))))
243
244 ;; Remove duplicates
245 (while (member pparent scopetypes)
246 (setq stack (cdr stack) pparent (car (cdr stack))))
247
248 ;; Step 1:
249 ;; Analyze the stack of tags we are nested in as parents.
250 ;;
251
252 ;; If we have a pparent tag, let's go there
253 ;; an analyze that stack of tags.
254 (when (and pparent (semantic-tag-with-position-p pparent))
255 (semantic-go-to-tag pparent)
256 (setq stack (semantic-find-tag-by-overlay (point)))
257 ;; Step one, find the merged version of stack in the typecache.
258 (let* ((stacknames (reverse (mapcar 'semantic-tag-name stack)))
259 (tc nil)
260 )
261 ;; @todo - can we use the typecache ability to
262 ;; put a scope into a tag to do this?
263 (while (and stacknames
264 (setq tc (semanticdb-typecache-find
265 (reverse stacknames))))
266 (setq returnlist (cons tc returnlist)
267 stacknames (cdr stacknames)))
268 (when (not returnlist)
269 ;; When there was nothing from the typecache, then just
270 ;; use what's right here.
271 (setq stack (reverse stack))
272 ;; Add things to STACK until we cease finding tags of class type.
273 (while (and stack (eq (semantic-tag-class (car stack)) 'type))
274 ;; Otherwise, just add this to the returnlist.
275 (setq returnlist (cons (car stack) returnlist))
276 (setq stack (cdr stack)))
277
278 (setq returnlist (nreverse returnlist))
279 ))
280 )
281
282 ;; Only do this level of analysis for functions.
283 (when (eq (semantic-tag-class tag) 'function)
284 ;; Step 2:
285 ;; If the function tag itself has a "parent" by name, then that
286 ;; parent will exist in the scope we just calculated, so look it
287 ;; up now.
288 ;;
289 (let ((p (semantic-tag-function-parent tag)))
290 (when p
291 ;; We have a parent, search for it.
292 (let* ((searchnameraw (cond ((stringp p) p)
293 ((semantic-tag-p p)
294 (semantic-tag-name p))
295 ((and (listp p) (stringp (car p)))
296 (car p))))
297 (searchname (semantic-analyze-split-name searchnameraw))
298 (snlist (if (consp searchname)
299 searchname
300 (list searchname)))
301 (fullsearchname nil)
302
303 (miniscope (semantic-scope-cache "mini"))
304 ptag)
305
306 ;; Find the next entry in the referenced type for
307 ;; our function, and append to return list till our
308 ;; returnlist is empty.
309 (while snlist
310 (setq fullsearchname
311 (append (mapcar 'semantic-tag-name returnlist)
312 (list (car snlist)))) ;; Next one
313 (setq ptag
314 (semanticdb-typecache-find fullsearchname))
315
316 (when (or (not ptag)
317 (not (semantic-tag-of-class-p ptag 'type)))
318 (let ((rawscope
319 (apply 'append
320 (mapcar 'semantic-tag-type-members
321 (cons (car returnlist) scopetypes)
322 )))
323 )
324 (oset miniscope parents returnlist) ;; Not really accurate, but close
325 (oset miniscope scope rawscope)
326 (oset miniscope fullscope rawscope)
327 (setq ptag
328 (semantic-analyze-find-tag searchnameraw
329 'type
330 miniscope
331 ))
332 ))
333
334 (when ptag
335 (when (and (not (semantic-tag-p ptag))
336 (semantic-tag-p (car ptag)))
337 (setq ptag (car ptag)))
338 (setq returnlist (append returnlist (list ptag)))
339 )
340
341 (setq snlist (cdr snlist)))
342 (setq returnlist returnlist)
343 )))
344 )
345 returnlist
346 )))
347
348 (define-overloadable-function semantic-analyze-scope-lineage-tags (parents scopedtypes)
349 "Return the full lineage of tags from PARENTS.
350 The return list is of the form ( TAG . PROTECTION ), where TAG is a tag,
351 and PROTECTION is the level of protection offered by the relationship.
352 Optional SCOPETYPES are additional scoped entities in which our parent might
353 be found.")
354
355 (defun semantic-analyze-scope-lineage-tags-default (parents scopetypes)
356 "Return the full lineage of tags from PARENTS.
357 The return list is of the form ( TAG . PROTECTION ), where TAG is a tag,
358 and PROTECTION is the level of protection offered by the relationship.
359 Optional SCOPETYPES are additional scoped entities in which our parent might
360 be found."
361 (let ((lineage nil)
362 (miniscope (semantic-scope-cache "mini"))
363 )
364 (oset miniscope parents parents)
365 (oset miniscope scope scopetypes)
366 (oset miniscope fullscope scopetypes)
367
368 (dolist (slp parents)
369 (semantic-analyze-scoped-inherited-tag-map
370 slp (lambda (newparent)
371 (let* ((pname (semantic-tag-name newparent))
372 (prot (semantic-tag-type-superclass-protection slp pname))
373 (effectiveprot (cond ((eq prot 'public)
374 ;; doesn't provide access to private slots?
375 'protected)
376 (t prot))))
377 (push (cons newparent effectiveprot) lineage)
378 ))
379 miniscope))
380
381 lineage))
382
383
384 ;;------------------------------------------------------------
385
386 (define-overloadable-function semantic-analyze-scoped-tags (typelist parentlist)
387 "Return accessible tags when TYPELIST and PARENTLIST is in scope.
388 Tags returned are not in the global name space, but are instead
389 scoped inside a class or namespace. Such items can be referenced
390 without use of \"object.function()\" style syntax due to an
391 implicit \"object\".")
392
393 (defun semantic-analyze-scoped-tags-default (typelist halfscope)
394 "Return accessible tags when TYPELIST and HALFSCOPE is in scope.
395 HALFSCOPE is the current scope partially initialized.
396 Tags returned are not in the global name space, but are instead
397 scoped inside a class or namespace. Such items can be referenced
398 without use of \"object.function()\" style syntax due to an
399 implicit \"object\"."
400 (let ((typelist2 nil)
401 (currentscope nil)
402 (parentlist (oref halfscope parents))
403 (miniscope halfscope)
404 )
405 ;; Loop over typelist, and find and merge all namespaces matching
406 ;; the names in typelist.
407 (while typelist
408 (let ((tt (semantic-tag-type (car typelist))))
409 (when (and (stringp tt) (string= tt "namespace"))
410 ;; By using the typecache, our namespaces are pre-merged.
411 (setq typelist2 (cons (car typelist) typelist2))
412 ))
413 (setq typelist (cdr typelist)))
414
415 ;; Loop over the types (which should be sorted by position)
416 ;; adding to the scopelist as we go, and using the scopelist
417 ;; for additional searching!
418 (while typelist2
419 (oset miniscope scope currentscope)
420 (oset miniscope fullscope currentscope)
421 (setq currentscope (append
422 (semantic-analyze-scoped-type-parts (car typelist2)
423 miniscope)
424 currentscope))
425 (setq typelist2 (cdr typelist2)))
426
427 ;; Collect all the types (class, etc) that are in our heritage.
428 ;; These are types that we can extract members from, not those
429 ;; declared in using statements, or the like.
430 ;; Get the PARENTS including nesting scope for this location.
431 (while parentlist
432 (oset miniscope scope currentscope)
433 (oset miniscope fullscope currentscope)
434 (setq currentscope (append
435 (semantic-analyze-scoped-type-parts (car parentlist)
436 miniscope)
437 currentscope))
438 (setq parentlist (cdr parentlist)))
439
440 ;; Loop over all the items, and collect any type constants.
441 (let ((constants nil))
442 (dolist (T currentscope)
443 (setq constants (append constants
444 (semantic-analyze-type-constants T)))
445 )
446
447 (setq currentscope (append currentscope constants)))
448
449 currentscope))
450
451 ;;------------------------------------------------------------
452 (define-overloadable-function semantic-analyze-scope-calculate-access (type scope)
453 "Calculate the access class for TYPE as defined by the current SCOPE.
454 Access is related to the :parents in SCOPE. If type is a member of SCOPE
455 then access would be 'private. If TYPE is inherited by a member of SCOPE,
456 the access would be 'protected. Otherwise, access is 'public")
457
458 (defun semantic-analyze-scope-calculate-access-default (type scope)
459 "Calculate the access class for TYPE as defined by the current SCOPE."
460 (cond ((semantic-scope-cache-p scope)
461 (let ((parents (oref scope parents))
462 (parentsi (oref scope parentinheritance))
463 )
464 (catch 'moose
465 ;; Investigate the parent, and see how it relates to type.
466 ;; If these tags are basically the same, then we have full access.
467 (dolist (p parents)
468 (when (semantic-tag-similar-p type p)
469 (throw 'moose 'private))
470 )
471 ;; Look to see if type is in our list of inherited parents.
472 (dolist (pi parentsi)
473 ;; pi is a cons cell ( PARENT . protection)
474 (let ((pip (car pi))
475 (piprot (cdr pi)))
476 (when (semantic-tag-similar-p type pip)
477 (throw 'moose
478 ;; protection via inheritance means to pull out different
479 ;; bits based on protection labels in an opposite way.
480 (cdr (assoc piprot
481 '((public . private)
482 (protected . protected)
483 (private . public))))
484 )))
485 )
486 ;; Not in our parentage. Is type a FRIEND?
487 (let ((friends (semantic-find-tags-by-class 'friend (semantic-tag-type-members type))))
488 (dolist (F friends)
489 (dolist (pi parents)
490 (if (string= (semantic-tag-name F) (semantic-tag-name pi))
491 (throw 'moose 'private))
492 )))
493 ;; Found nothing, return public
494 'public)
495 ))
496 (t 'public)))
497
498 (defun semantic-completable-tags-from-type (type)
499 "Return a list of slots that are valid completions from the list of SLOTS.
500 If a tag in SLOTS has a named parent, then that implies that the
501 tag is not something you can complete from within TYPE."
502 (let ((allslots (semantic-tag-components type))
503 (leftover nil)
504 )
505 (dolist (S allslots)
506 (when (or (not (semantic-tag-of-class-p S 'function))
507 (not (semantic-tag-function-parent S)))
508 (setq leftover (cons S leftover)))
509 )
510 (nreverse leftover)))
511
512 (defun semantic-analyze-scoped-type-parts (type &optional scope noinherit protection)
513 "Return all parts of TYPE, a tag representing a TYPE declaration.
514 SCOPE is the scope object.
515 NOINHERIT turns off searching of inherited tags.
516 PROTECTION specifies the type of access requested, such as 'public or 'private."
517 (if (not type)
518 nil
519 (let* ((access (semantic-analyze-scope-calculate-access type scope))
520 ;; SLOTS are the slots directly a part of TYPE.
521 (allslots (semantic-completable-tags-from-type type))
522 (slots (semantic-find-tags-by-scope-protection
523 access
524 type allslots))
525 (fname (semantic-tag-file-name type))
526 ;; EXTMETH are externally defined methods that are still
527 ;; a part of this class.
528
529 ;; @TODO - is this line needed?? Try w/out for a while
530 ;; @note - I think C++ says no. elisp might, but methods
531 ;; look like defuns, so it makes no difference.
532 (extmeth nil) ; (semantic-tag-external-member-children type t))
533
534 ;; INHERITED are tags found in classes that our TYPE tag
535 ;; inherits from. Do not do this if it was not requested.
536 (inherited (when (not noinherit)
537 (semantic-analyze-scoped-inherited-tags type scope
538 access)))
539 )
540 (when (not (semantic-tag-in-buffer-p type))
541 (let ((copyslots nil))
542 (dolist (TAG slots)
543 ;;(semantic--tag-put-property TAG :filename fname)
544 (if (semantic-tag-file-name TAG)
545 ;; If it has a filename, just go with it...
546 (setq copyslots (cons TAG copyslots))
547 ;; Otherwise, copy the tag w/ the guessed filename.
548 (setq copyslots (cons (semantic-tag-copy TAG nil fname)
549 copyslots)))
550 )
551 (setq slots (nreverse copyslots))
552 ))
553 ;; Flatten the database output.
554 (append slots extmeth inherited)
555 )))
556
557 (defun semantic-analyze-scoped-inherited-tags (type scope access)
558 "Return all tags that TYPE inherits from.
559 Argument SCOPE specify additional tags that are in scope
560 whose tags can be searched when needed, OR it may be a scope object.
561 ACCESS is the level of access we filter on child supplied tags.
562 For languages with protection on specific methods or slots,
563 it should strip out those not accessible by methods of TYPE.
564 An ACCESS of 'public means not in a method of a subclass of type.
565 A value of 'private means we can access private parts of the originating
566 type."
567 (let ((ret nil))
568 (semantic-analyze-scoped-inherited-tag-map
569 type (lambda (p)
570 (let* ((pname (semantic-tag-name p))
571 (protection (semantic-tag-type-superclass-protection
572 type pname))
573 )
574 (if (and (eq access 'public) (not (eq protection 'public)))
575 nil ;; Don't do it.
576
577 ;; We can get some parts of this type.
578 (setq ret (nconc ret
579 ;; Do not pull in inherited parts here. Those
580 ;; will come via the inherited-tag-map fcn
581 (semantic-analyze-scoped-type-parts
582 p scope t protection))
583 ))))
584 scope)
585 ret))
586
587 (defun semantic-analyze-scoped-inherited-tag-map (type fcn scope)
588 "Map all parents of TYPE to FCN. Return tags of all the types.
589 Argument SCOPE specify additional tags that are in scope
590 whose tags can be searched when needed, OR it may be a scope object."
591 (require 'semantic/analyze)
592 (let* (;; PARENTS specifies only the superclasses and not
593 ;; interfaces. Inheriting from an interfaces implies
594 ;; you have a copy of all methods locally. I think.
595 (parents (semantic-tag-type-superclasses type))
596 ps pt
597 (tmpscope scope)
598 )
599 (save-excursion
600
601 ;; Create a SCOPE just for looking up the parent based on where
602 ;; the parent came from.
603 ;;
604 ;; @TODO - Should we cache these mini-scopes around in Emacs
605 ;; for recycling later? Should this become a helpful
606 ;; extra routine?
607 (when (and parents (semantic-tag-with-position-p type))
608 (save-excursion
609 ;; If TYPE has a position, go there and get the scope.
610 (semantic-go-to-tag type)
611
612 ;; We need to make a mini scope, and only include the misc bits
613 ;; that will help in finding the parent. We don't really need
614 ;; to do any of the stuff related to variables and what-not.
615 (setq tmpscope (semantic-scope-cache "mini"))
616 (let* ( ;; Step 1:
617 (scopetypes (cons type (semantic-analyze-scoped-types (point))))
618 (parents (semantic-analyze-scope-nested-tags (point) scopetypes))
619 ;;(parentinherited (semantic-analyze-scope-lineage-tags parents scopetypes))
620 (lscope nil)
621 )
622 (oset tmpscope scopetypes scopetypes)
623 (oset tmpscope parents parents)
624 ;;(oset tmpscope parentinheritance parentinherited)
625
626 (when (or scopetypes parents)
627 (setq lscope (semantic-analyze-scoped-tags scopetypes tmpscope))
628 (oset tmpscope scope lscope))
629 (oset tmpscope fullscope (append scopetypes lscope parents))
630 )))
631 ;; END creating tmpscope
632
633 ;; Look up each parent one at a time.
634 (dolist (p parents)
635 (setq ps (cond ((stringp p) p)
636 ((and (semantic-tag-p p) (semantic-tag-prototype-p p))
637 (semantic-tag-name p))
638 ((and (listp p) (stringp (car p)))
639 p))
640 pt (condition-case nil
641 (or (semantic-analyze-find-tag ps 'type tmpscope)
642 ;; A backup hack.
643 (semantic-analyze-find-tag ps 'type scope))
644 (error nil)))
645
646 (when pt
647 (funcall fcn pt)
648 ;; Note that we pass the original SCOPE in while recursing.
649 ;; so that the correct inheritance model is passed along.
650 (semantic-analyze-scoped-inherited-tag-map pt fcn scope)
651 )))
652 nil))
653
654 ;;; ANALYZER
655 ;;
656 ;; Create the scope structure for use in the Analyzer.
657 ;;
658 ;;;###autoload
659 (defun semantic-calculate-scope (&optional point)
660 "Calculate the scope at POINT.
661 If POINT is not provided, then use the current location of point.
662 The class returned from the scope calculation is variable
663 `semantic-scope-cache'."
664 (interactive)
665 (if (not (and (featurep 'semantic/db) semanticdb-current-database))
666 nil ;; Don't do anything...
667 (require 'semantic/db-typecache)
668 (if (not point) (setq point (point)))
669 (when (called-interactively-p 'any)
670 (semantic-fetch-tags)
671 (semantic-scope-reset-cache))
672 (save-excursion
673 (goto-char point)
674 (let* ((TAG (semantic-current-tag))
675 (scopecache
676 (semanticdb-cache-get semanticdb-current-table
677 semantic-scope-cache))
678 )
679 (when (not (semantic-equivalent-tag-p TAG (oref scopecache tag)))
680 (semantic-reset scopecache))
681 (if (oref scopecache tag)
682 ;; Even though we can recycle most of the scope, we
683 ;; need to redo the local variables since those change
684 ;; as you move about the tag.
685 (condition-case nil
686 (oset scopecache localvar (semantic-get-all-local-variables))
687 (error nil))
688
689 (let* (;; Step 1:
690 (scopetypes (semantic-analyze-scoped-types point))
691 (parents (semantic-analyze-scope-nested-tags point scopetypes))
692 (parentinherited (semantic-analyze-scope-lineage-tags
693 parents scopetypes))
694 )
695 (oset scopecache tag TAG)
696 (oset scopecache scopetypes scopetypes)
697 (oset scopecache parents parents)
698 (oset scopecache parentinheritance parentinherited)
699
700 (let* (;; Step 2:
701 (scope (when (or scopetypes parents)
702 (semantic-analyze-scoped-tags scopetypes scopecache))
703 )
704 ;; Step 3:
705 (localargs (semantic-get-local-arguments))
706 (localvar (condition-case nil
707 (semantic-get-all-local-variables)
708 (error nil)))
709 )
710
711 ;; Try looking for parents again.
712 (when (not parentinherited)
713 (setq parentinherited (semantic-analyze-scope-lineage-tags
714 parents (append scopetypes scope)))
715 (when parentinherited
716 (oset scopecache parentinheritance parentinherited)
717 ;; Try calculating the scope again with the new inherited parent list.
718 (setq scope (when (or scopetypes parents)
719 (semantic-analyze-scoped-tags scopetypes scopecache))
720 )))
721
722 ;; Fill out the scope.
723 (oset scopecache scope scope)
724 (oset scopecache fullscope (append scopetypes scope parents))
725 (oset scopecache localargs localargs)
726 (oset scopecache localvar localvar)
727 )))
728 ;; Make sure we become dependent on the typecache.
729 (semanticdb-typecache-add-dependant scopecache)
730 ;; Handy debug output.
731 (when (called-interactively-p 'any)
732 (require 'eieio-datadebug)
733 (data-debug-show scopecache))
734 ;; Return ourselves
735 scopecache))))
736
737 (defun semantic-scope-find (name &optional class scope-in)
738 "Find the tag with NAME, and optional CLASS in the current SCOPE-IN.
739 Searches various elements of the scope for NAME. Return ALL the
740 hits in order, with the first tag being in the closest scope."
741 (let ((scope (or scope-in (semantic-calculate-scope)))
742 (ans nil))
743 ;; Is the passed in scope really a scope? if so, look through
744 ;; the options in that scope.
745 (if (semantic-scope-cache-p scope)
746 (let* ((la
747 ;; This should be first, but bugs in the
748 ;; C parser will turn function calls into
749 ;; assumed int return function prototypes. Yuck!
750 (semantic-find-tags-by-name name (oref scope localargs)))
751 (lv
752 (semantic-find-tags-by-name name (oref scope localvar)))
753 (fullscoperaw (oref scope fullscope))
754 (sc (semantic-find-tags-by-name name fullscoperaw))
755 (typescoperaw (oref scope typescope))
756 (tsc (semantic-find-tags-by-name name typescoperaw))
757 )
758 (setq ans
759 (if class
760 ;; Scan out things not of the right class.
761 (semantic-find-tags-by-class class (append la lv sc tsc))
762 (append la lv sc tsc))
763 )
764
765 (when (and (not ans) (or typescoperaw fullscoperaw))
766 (let ((namesplit (semantic-analyze-split-name name)))
767 (when (consp namesplit)
768 ;; It may be we need to hack our way through type typescope.
769 (while namesplit
770 (setq ans (append
771 (semantic-find-tags-by-name (car namesplit)
772 typescoperaw)
773 (semantic-find-tags-by-name (car namesplit)
774 fullscoperaw)
775 ))
776 (if (not ans)
777 (setq typescoperaw nil)
778 (when (cdr namesplit)
779 (setq typescoperaw (semantic-tag-type-members
780 (car ans)))))
781
782 (setq namesplit (cdr namesplit)))
783 ;; Once done, store the current typecache lookup
784 (oset scope typescope
785 (append typescoperaw (oref scope typescope)))
786 )))
787 ;; Return it.
788 ans)
789 ;; Not a real scope. Our scope calculation analyze parts of
790 ;; what it finds, and needs to pass lists through to do it's work.
791 ;; Tread that list as a singly entry.
792 (if class
793 (semantic-find-tags-by-class class scope)
794 scope)
795 )))
796
797 ;;; DUMP
798 ;;
799 (defmethod semantic-analyze-show ((context semantic-scope-cache))
800 "Insert CONTEXT into the current buffer in a nice way."
801 (require 'semantic/analyze)
802 (semantic-analyze-princ-sequence (oref context scopetypes) "-> ScopeTypes: " )
803 (semantic-analyze-princ-sequence (oref context parents) "-> Parents: " )
804 (semantic-analyze-princ-sequence (oref context scope) "-> Scope: " )
805 ;;(semantic-analyze-princ-sequence (oref context fullscope) "Fullscope: " )
806 (semantic-analyze-princ-sequence (oref context localargs) "-> Local Args: " )
807 (semantic-analyze-princ-sequence (oref context localvar) "-> Local Vars: " )
808 )
809
810 (provide 'semantic/scope)
811
812 ;; Local variables:
813 ;; generated-autoload-file: "loaddefs.el"
814 ;; generated-autoload-load-name: "semantic/scope"
815 ;; End:
816
817 ;;; semantic/scope.el ends here