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