summaryrefslogtreecommitdiff
path: root/lisp/cedet/semantic/scope.el
blob: 31576d29bc67d3bf235410056d9c391149a6b7ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
;;; semantic/scope.el --- Analyzer Scope Calculations

;; Copyright (C) 2007-2021 Free Software Foundation, Inc.

;; Author: Eric M. Ludlam <zappo@gnu.org>

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; Calculate information about the current scope.
;;
;; Manages the current scope as a structure that can be cached on a
;; per-file basis and recycled between different occurrences of
;; analysis on different parts of a file.
;;
;; Pattern for Scope Calculation
;;
;; Step 1: Calculate DataTypes in Scope:
;;
;; a) What is in scope via using statements or local namespaces
;; b) Lineage of current context.  Some names drawn from step 1.
;;
;; Step 2: Convert type names into lists of concrete tags
;;
;; a) Convert each datatype into the real datatype tag
;; b) Convert namespaces into the list of contents of the namespace.
;; c) Merge all existing scopes together into one search list.
;;
;; Step 3: Local variables
;;
;; a) Local variables are in the master search list.
;;

(require 'semantic/db)
(require 'semantic/analyze/fcn)
(require 'semantic/ctxt)

(eval-when-compile (require 'semantic/find))

(declare-function data-debug-show "eieio-datadebug")
(declare-function semantic-analyze-find-tag "semantic/analyze")
(declare-function semantic-analyze-princ-sequence "semantic/analyze")
(declare-function semanticdb-typecache-merge-streams "semantic/db-typecache")
(declare-function semanticdb-typecache-add-dependant "semantic/db-typecache")
(declare-function semantic-tag-similar-p "semantic/tag-ls")

;;; Code:

(defclass semantic-scope-cache (semanticdb-abstract-cache)
  ((tag :initform nil
	:documentation
	"The tag this scope was calculated for.")
   (scopetypes :initform nil
	       :documentation
	       "The list of types currently in scope.
For C++, this would contain anonymous namespaces known, and
anything labeled by a `using' statement.")
   (parents :initform nil
	    :documentation
	    "List of parents in scope w/in the body of this function.
Presumably, the members of these parent classes are available for access
based on private:, or public: style statements.")
   (parentinheritance :initform nil
		      :documentation "Alist of parents by inheritance.
Each entry is ( PARENT . PROTECTION ), where PARENT is a type, and
PROTECTION is a symbol representing the level of inheritance, such as 'private, or 'protected.")
   (scope :initform nil
	  :documentation
	  "Items in scope due to the scopetypes or parents.")
   (fullscope :initform nil
	      :documentation
	      "All the other stuff on one master list you can search.")
   (localargs :initform nil
	      :documentation
	      "The arguments to the function tag.")
   (localvar :initform nil
	     :documentation
	     "The local variables.")
   (typescope :initform nil
	      :documentation
	      "Slot to save intermediate scope while metatypes are dereferenced.")
   )
  "Cache used for storage of the current scope by the Semantic Analyzer.
Saves scoping information between runs of the analyzer.")

;;; METHODS
;;
;; Methods for basic management of the structure in semanticdb.
;;
(cl-defmethod semantic-reset ((obj semantic-scope-cache))
  "Reset OBJ back to it's empty settings."
  (oset obj tag nil)
  (oset obj scopetypes nil)
  (oset obj parents nil)
  (oset obj parentinheritance nil)
  (oset obj scope nil)
  (oset obj fullscope nil)
  (oset obj localargs nil)
  (oset obj localvar nil)
  (oset obj typescope nil)
  )

(cl-defmethod semanticdb-synchronize ((cache semantic-scope-cache)
				   new-tags)
  "Synchronize a CACHE with some NEW-TAGS."
  (semantic-reset cache))


(cl-defmethod semanticdb-partial-synchronize ((cache semantic-scope-cache)
					   new-tags)
  "Synchronize a CACHE with some changed NEW-TAGS."
  ;; If there are any includes or datatypes changed, then clear.
  (if (or (semantic-find-tags-by-class 'include new-tags)
	  (semantic-find-tags-by-class 'type new-tags)
	  (semantic-find-tags-by-class 'using new-tags))
      (semantic-reset cache))
  )

(defun semantic-scope-reset-cache ()
  "Get the current cached scope, and reset it."
  (when semanticdb-current-table
    (let ((co (semanticdb-cache-get semanticdb-current-table
				    'semantic-scope-cache)))
      (semantic-reset co))))

(cl-defmethod semantic-scope-set-typecache ((cache semantic-scope-cache)
					 types-in-scope)
  "Set the :typescope property on CACHE to some types.
TYPES-IN-SCOPE is a list of type tags whose members are
currently in scope.  For each type in TYPES-IN-SCOPE,
add those members to the types list.
If nil, then the typescope is reset."
  (let ((newts nil)) ;; New Type Scope
    (dolist (onetype types-in-scope)
      (setq newts (append (semantic-tag-type-members onetype)
			  newts))
      )
    (oset cache typescope newts)))

;;; TAG SCOPES
;;
;; These fcns should be used by search routines that return a single
;; tag which, in turn, may have come from a deep scope.  The scope
;; will be attached to the tag.  Thus, in future scope based calls, a
;; tag can be passed in and a scope derived from it.

(defun semantic-scope-tag-clone-with-scope (tag scopetags)
  "Clone TAG, and return it.  Add SCOPETAGS as a tag-local scope.
Stores the SCOPETAGS as a set of tag properties on the cloned tag."
  (let ((clone (semantic-tag-clone tag))
	)
    (semantic--tag-put-property clone 'scope scopetags)
    ))

(defun semantic-scope-tag-get-scope (tag)
  "Get from TAG the list of tags comprising the scope from TAG."
  (semantic--tag-get-property tag 'scope))

;;; SCOPE UTILITIES
;;
;; Functions that do the main scope calculations


(define-overloadable-function semantic-analyze-scoped-types (position)
  "Return a list of types currently in scope at POSITION.
This is based on what tags exist at POSITION, and any associated
types available.")

(defun semantic-analyze-scoped-types-default (position)
  "Return a list of types currently in scope at POSITION.
Use `semantic-ctxt-scoped-types' to find types."
  (require 'semantic/db-typecache)
  (save-excursion
    (goto-char position)
    (let ((code-scoped-types nil))
      ;; Let's ask if any types are currently scoped.  Scoped
      ;; classes and types provide their public methods and types
      ;; in source code, but are unrelated hierarchically.
      (let ((sp (semantic-ctxt-scoped-types)))
	(while sp
	  ;; Get this thing as a tag
	  (let ((tmp (cond
		      ((stringp (car sp))
		       (or (semanticdb-typecache-find (car sp))
			   ;; If we did not find it in the typecache,
			   ;; look in the tags we found so far
			   (car (semantic-deep-find-tags-by-name
				 (car sp)
				 code-scoped-types))))
		      ((semantic-tag-p (car sp))
		       (if (semantic-tag-prototype-p (car sp))
			   (or (semanticdb-typecache-find (semantic-tag-name (car sp)))
			       (car (semantic-deep-find-tags-by-name
				     (semantic-tag-name (car sp))
				     code-scoped-types)))
			 (car sp)))
		      (t nil))))
	    (when tmp
	      (setq code-scoped-types
		    (cons tmp code-scoped-types))))
	  (setq  sp (cdr sp))))
      (setq code-scoped-types (nreverse code-scoped-types))

      (when code-scoped-types
	(semanticdb-typecache-merge-streams code-scoped-types nil))

      )))

;;------------------------------------------------------------
(define-overloadable-function semantic-analyze-scope-nested-tags (position scopedtypes)
  "Return a list of types in order of nesting for the context of POSITION.
If POSITION is in a method with a named parent, find that parent, and
identify it's scope via overlay instead.
Optional SCOPETYPES are additional scoped entities in which our parent might
be found.")

(defun semantic-analyze-scope-nested-tags-default (position scopetypes)
  "Return a list of types in order of nesting for the context of POSITION.
If POSITION is in a method with a named parent, find that parent, and
identify it's scope via overlay instead.
Optional SCOPETYPES are additional scoped entities in which our parent might
be found.
This only finds ONE immediate parent by name.  All other parents returned
are from nesting data types."
  (require 'semantic/analyze)
  (save-excursion
    (if position (goto-char position))
    (let* ((stack (reverse (semantic-find-tag-by-overlay (point))))
	   (tag (car stack))
	   (pparent (car (cdr stack)))
	   (returnlist nil)
	   )
      ;; In case of arg lists or some-such, throw out non-types.
      (while (and stack (not (semantic-tag-of-class-p pparent 'type)))
	(setq stack (cdr stack) pparent (car (cdr stack))))

      ;; Remove duplicates
      (while (member pparent scopetypes)
	(setq stack (cdr stack) pparent (car (cdr stack))))

      ;; Step 1:
      ;;    Analyze the stack of tags we are nested in as parents.
      ;;

      ;; If we have a pparent tag, let's go there
      ;; an analyze that stack of tags.
      (when (and pparent (semantic-tag-with-position-p pparent))
	(semantic-go-to-tag pparent)
	(setq stack (semantic-find-tag-by-overlay (point)))
	;; Step one, find the merged version of stack in the typecache.
	(let* ((stacknames (reverse (mapcar 'semantic-tag-name stack)))
	       (tc nil)
	       )
	  ;; @todo - can we use the typecache ability to
	  ;;         put a scope into a tag to do this?
	  (while (and stacknames
		      (setq tc (semanticdb-typecache-find
				(reverse stacknames))))
	    (setq returnlist (cons tc returnlist)
		  stacknames (cdr stacknames)))
	  (when (not returnlist)
	    ;; When there was nothing from the typecache, then just
	    ;; use what's right here.
	    (setq stack (reverse stack))
	    ;; Add things to STACK until we cease finding tags of class type.
	    (while (and stack (eq (semantic-tag-class (car stack)) 'type))
	      ;; Otherwise, just add this to the returnlist, but make
	      ;; sure we didn't already have that tag in scopetypes
             (unless (member (car stack) scopetypes)
               (setq returnlist (cons (car stack) returnlist)))
	     (setq stack (cdr stack)))

	    (setq returnlist (nreverse returnlist))
	    ))
	)

      ;; Only do this level of analysis for functions.
      (when (eq (semantic-tag-class tag) 'function)
	;; Step 2:
	;;   If the function tag itself has a "parent" by name, then that
	;;   parent will exist in the scope we just calculated, so look it
	;;   up now.
	;;
	(let ((p (semantic-tag-function-parent tag)))
	  (when p
	    ;; We have a parent, search for it.
	    (let* ((searchnameraw (cond ((stringp p) p)
					((semantic-tag-p p)
					 (semantic-tag-name p))
					((and (listp p) (stringp (car p)))
					 (car p))))
		   (searchname (semantic-analyze-split-name searchnameraw))
		   (snlist (if (consp searchname)
			       searchname
			     (list searchname)))
		   (fullsearchname nil)

		   (miniscope (semantic-scope-cache))
		   ptag)

	      ;; Find the next entry in the referenced type for
	      ;; our function, and append to return list till our
	      ;; returnlist is empty.
	      (while snlist
		(setq fullsearchname
		      (append (mapcar 'semantic-tag-name returnlist)
			      (list (car snlist)))) ;; Next one
		(setq ptag
		      (semanticdb-typecache-find fullsearchname))

		(when (or (not ptag)
			  (not (semantic-tag-of-class-p ptag 'type)))
		  (let ((rawscope
			 (apply 'append
				(mapcar 'semantic-tag-type-members
					(cons (car returnlist) scopetypes)
					)))
			)
		    (oset miniscope parents returnlist) ;; Not really accurate, but close
		    (oset miniscope scope rawscope)
		    (oset miniscope fullscope rawscope)
		    (setq ptag
			  (semantic-analyze-find-tag searchnameraw
						     'type
						     miniscope
						     ))
		    ))

		(when ptag
		  (when (and (not (semantic-tag-p ptag))
			     (semantic-tag-p (car ptag)))
		    (setq ptag (car ptag)))
		  (setq returnlist (append returnlist (list ptag)))
		  )

		(setq snlist (cdr snlist)))
	      (setq returnlist returnlist)
	      )))
	)
      returnlist
      )))

(define-overloadable-function semantic-analyze-scope-lineage-tags (parents scopedtypes)
  "Return the full lineage of tags from PARENTS.
The return list is of the form ( TAG . PROTECTION ), where TAG is a tag,
and PROTECTION is the level of protection offered by the relationship.
Optional SCOPETYPES are additional scoped entities in which our parent might
be found.")

(defun semantic-analyze-scope-lineage-tags-default (parents scopetypes)
  "Return the full lineage of tags from PARENTS.
The return list is of the form ( TAG . PROTECTION ), where TAG is a tag,
and PROTECTION is the level of protection offered by the relationship.
Optional SCOPETYPES are additional scoped entities in which our parent might
be found."
  (let ((lineage nil)
	(miniscope (semantic-scope-cache))
	)
    (oset miniscope parents parents)
    (oset miniscope scope scopetypes)
    (oset miniscope fullscope scopetypes)

    (dolist (slp parents)
      (semantic-analyze-scoped-inherited-tag-map
       slp (lambda (newparent)
	     (let* ((pname (semantic-tag-name newparent))
		    (prot (semantic-tag-type-superclass-protection slp pname))
		    (effectiveprot (cond ((eq prot 'public)
					  ;; doesn't provide access to private slots?
					  'protected)
					 (t prot))))
	       (push (cons newparent effectiveprot) lineage)
	       ))
       miniscope))

    lineage))


;;------------------------------------------------------------

(define-overloadable-function semantic-analyze-scoped-tags (typelist parentlist)
  "Return accessible tags when TYPELIST and PARENTLIST is in scope.
Tags returned are not in the global name space, but are instead
scoped inside a class or namespace.  Such items can be referenced
without use of \"object.function()\" style syntax due to an
implicit \"object\".")

(defun semantic-analyze-scoped-tags-default (typelist halfscope)
  "Return accessible tags when TYPELIST and HALFSCOPE is in scope.
HALFSCOPE is the current scope partially initialized.
Tags returned are not in the global name space, but are instead
scoped inside a class or namespace.  Such items can be referenced
without use of \"object.function()\" style syntax due to an
implicit \"object\"."
  (let ((typelist2 nil)
	(currentscope nil)
	(parentlist (oref halfscope parents))
	(miniscope halfscope)
	)
    ;; Loop over typelist, and find and merge all namespaces matching
    ;; the names in typelist.
    (while typelist
      (let ((tt (semantic-tag-type (car typelist))))
	(when (and (stringp tt) (string= tt "namespace"))
	  ;; By using the typecache, our namespaces are pre-merged.
	  (setq typelist2 (cons (car typelist) typelist2))
	  ))
      (setq typelist (cdr typelist)))

    ;; Loop over the types (which should be sorted by position)
    ;; adding to the scopelist as we go, and using the scopelist
    ;; for additional searching!
    (while typelist2
      (oset miniscope scope currentscope)
      (oset miniscope fullscope currentscope)
      (setq currentscope (append
			  (semantic-analyze-scoped-type-parts (car typelist2)
							      miniscope)
			  currentscope))
      (setq typelist2 (cdr typelist2)))

    ;; Collect all the types (class, etc) that are in our heritage.
    ;; These are types that we can extract members from, not those
    ;; declared in using statements, or the like.
    ;; Get the PARENTS including nesting scope for this location.
    (while parentlist
      (oset miniscope scope currentscope)
      (oset miniscope fullscope currentscope)
      (setq currentscope (append
			  (semantic-analyze-scoped-type-parts (car parentlist)
							      miniscope)
			  currentscope))
      (setq parentlist (cdr parentlist)))

    ;; Loop over all the items, and collect any type constants.
    (let ((constants nil))
      (dolist (T currentscope)
	(setq constants (append constants
				(semantic-analyze-type-constants T)))
	)

      (setq currentscope (append currentscope constants)))

    currentscope))

;;------------------------------------------------------------
(define-overloadable-function  semantic-analyze-scope-calculate-access (type scope)
  "Calculate the access class for TYPE as defined by the current SCOPE.
Access is related to the :parents in SCOPE.  If type is a member of SCOPE
then access would be `private'.  If TYPE is inherited by a member of SCOPE,
the access would be `protected'.  Otherwise, access is `public'.")

(defun semantic-analyze-scope-calculate-access-default (type scope)
  "Calculate the access class for TYPE as defined by the current SCOPE."
  (cond ((semantic-scope-cache-p scope)
	 (let ((parents (oref scope parents))
	       (parentsi (oref scope parentinheritance))
	       )
	   (catch 'moose
	     ;; Investigate the parent, and see how it relates to type.
	     ;; If these tags are basically the same, then we have full access.
	     (dolist (p parents)
	       (when (semantic-tag-similar-p type p)
		 (throw 'moose 'private))
	       )
	     ;; Look to see if type is in our list of inherited parents.
	     (dolist (pi parentsi)
	       ;; pi is a cons cell ( PARENT . protection)
	       (let ((pip (car pi))
		     (piprot (cdr pi)))
		 (when (semantic-tag-similar-p type pip)
		   (throw 'moose
			  ;; protection via inheritance means to pull out different
			  ;; bits based on protection labels in an opposite way.
			  (cdr (assoc piprot
				      '((public . private)
					(protected . protected)
					(private . public))))
			  )))
	       )
	     ;; Not in our parentage.  Is type a FRIEND?
	     (let ((friends (semantic-find-tags-by-class 'friend (semantic-tag-type-members type))))
	       (dolist (F friends)
		 (dolist (pi parents)
		   (if (string= (semantic-tag-name F) (semantic-tag-name pi))
		       (throw 'moose 'private))
		   )))
	     ;; Found nothing, return public
	     'public)
	   ))
	(t 'public)))

(defun semantic-completable-tags-from-type (type)
  "Return a list of slots that are valid completions from the list of SLOTS.
If a tag in SLOTS has a named parent, then that implies that the
tag is not something you can complete from within TYPE."
  (let ((allslots (semantic-tag-components type))
	(leftover nil)
	)
    (dolist (S allslots)
      ;; We have to specially deal with 'using' tags here, since those
      ;; pull in namespaces or classes into the current scope.
      ;; (Should this go into c.el? If so, into which override?)
      (if (semantic-tag-of-class-p S 'using)
	  (let* ((fullname (semantic-analyze-unsplit-name
			    (list (semantic-tag-name type)
				  (semantic-tag-name S))))
		 ;; Search the typecache, first for the unqualified name
		 (usingtype (or
			      (semanticdb-typecache-find (semantic-tag-name S))
			      ;; If that didn't return anything, use
			      ;; fully qualified name
			      (semanticdb-typecache-find fullname)))
		 (filename (when usingtype (semantic-tag-file-name usingtype))))
	    (when usingtype
	      ;; Use recursion to examine that namespace or class
	      (let ((tags (semantic-completable-tags-from-type usingtype)))
		(if filename
		    ;; If we have a filename, copy the tags with it
		    (dolist (cur tags)
		      (setq leftover (cons (semantic-tag-copy cur nil filename)
					   leftover)))
		  ;; Otherwise just run with it
		  (setq leftover (append tags leftover))))))
	(when (or (not (semantic-tag-of-class-p S 'function))
		  (not (semantic-tag-function-parent S)))
	  (setq leftover (cons S leftover)))))
    (nreverse leftover)))

(defun semantic-analyze-scoped-type-parts (type &optional scope noinherit protection)
  "Return all parts of TYPE, a tag representing a TYPE declaration.
SCOPE is the scope object.
NOINHERIT turns off searching of inherited tags.
PROTECTION specifies the type of access requested,
such as `public' or `private'."
  (if (not type)
      nil
    (let* ((access (semantic-analyze-scope-calculate-access type scope))
	   ;; SLOTS are the slots directly a part of TYPE.
	   (allslots (semantic-completable-tags-from-type type))
	   (slots (semantic-find-tags-by-scope-protection
		   access
		   type allslots))
	   (fname (semantic-tag-file-name type))
	   ;; EXTMETH are externally defined methods that are still
	   ;; a part of this class.

	   ;; @TODO - is this line needed??  Try w/out for a while
	   ;; @note - I think C++ says no.  elisp might, but methods
	   ;;         look like defuns, so it makes no difference.
	   (extmeth nil) ; (semantic-tag-external-member-children type t))

	   ;; INHERITED are tags found in classes that our TYPE tag
	   ;; inherits from.  Do not do this if it was not requested.
	   (inherited (when (not noinherit)
			(semantic-analyze-scoped-inherited-tags type scope
								access)))
	   )
      (when (not (semantic-tag-in-buffer-p type))
	(let ((copyslots nil))
	  (dolist (TAG slots)
	    ;;(semantic--tag-put-property TAG :filename fname)
	    (if (semantic-tag-file-name TAG)
		;; If it has a filename, just go with it...
		(setq copyslots (cons TAG copyslots))
	      ;; Otherwise, copy the tag w/ the guessed filename.
	      (setq copyslots (cons (semantic-tag-copy TAG nil fname)
				    copyslots)))
	    )
	  (setq slots (nreverse copyslots))
	  ))
      ;; Flatten the database output.
      (append slots extmeth inherited)
      )))

(defun semantic-analyze-scoped-inherited-tags (type scope access)
  "Return all tags that TYPE inherits from.
Argument SCOPE specify additional tags that are in scope
whose tags can be searched when needed, OR it may be a scope object.
ACCESS is the level of access we filter on child supplied tags.
For languages with protection on specific methods or slots,
it should strip out those not accessible by methods of TYPE.
An ACCESS of `public' means not in a method of a subclass of type.
A value of `private' means we can access private parts of the originating
type."
  (let ((ret nil))
    (semantic-analyze-scoped-inherited-tag-map
     type (lambda (p)
	    (let* ((pname (semantic-tag-name p))
		   (protection (semantic-tag-type-superclass-protection
				type pname))
		   )
	      (if (and (eq access 'public) (not (eq protection 'public)))
		  nil ;; Don't do it.

		;; We can get some parts of this type.
		(setq ret (nconc ret
				 ;; Do not pull in inherited parts here.  Those
				 ;; will come via the inherited-tag-map fcn
				 (semantic-analyze-scoped-type-parts
				  p scope t protection))
		      ))))
     scope)
    ret))

(defun semantic-analyze-scoped-inherited-tag-map (type fcn scope)
  "Map all parents of TYPE to FCN.  Return tags of all the types.
Argument SCOPE specify additional tags that are in scope
whose tags can be searched when needed, OR it may be a scope object."
  (require 'semantic/analyze)
  (let* (;; PARENTS specifies only the superclasses and not
	 ;; interfaces.  Inheriting from an interfaces implies
	 ;; you have a copy of all methods locally.  I think.
	 (parents (semantic-tag-type-superclasses type))
	 ps pt
	 (tmpscope scope)
	 )
    (save-excursion

      ;; Create a SCOPE just for looking up the parent based on where
      ;; the parent came from.
      ;;
      ;; @TODO - Should we cache these mini-scopes around in Emacs
      ;;         for recycling later?  Should this become a helpful
      ;;         extra routine?
      (when (and parents (semantic-tag-with-position-p type))
	(save-excursion
	  ;; If TYPE has a position, go there and get the scope.
	  (semantic-go-to-tag type)

	  ;; We need to make a mini scope, and only include the misc bits
	  ;; that will help in finding the parent.  We don't really need
	  ;; to do any of the stuff related to variables and what-not.
	  (setq tmpscope (semantic-scope-cache))
	  (let* ( ;; Step 1:
		 (scopetypes (cons type (semantic-analyze-scoped-types (point))))
		 (parents (semantic-analyze-scope-nested-tags (point) scopetypes))
		 ;;(parentinherited (semantic-analyze-scope-lineage-tags parents scopetypes))
		 (lscope nil)
		 )
	    (oset tmpscope scopetypes scopetypes)
	    (oset tmpscope parents parents)
	    ;;(oset tmpscope parentinheritance parentinherited)

	    (when (or scopetypes parents)
	      (setq lscope (semantic-analyze-scoped-tags scopetypes tmpscope))
	      (oset tmpscope scope lscope))
	    (oset tmpscope fullscope (append scopetypes lscope parents))
	    )))
      ;; END creating tmpscope

      ;; Look up each parent one at a time.
      (dolist (p parents)
	(setq ps (cond ((stringp p) p)
		       ((and (semantic-tag-p p) (semantic-tag-prototype-p p))
			(semantic-tag-name p))
		       ((and (listp p) (stringp (car p)))
			p))
	      pt (condition-case nil
		     (or (semantic-analyze-find-tag ps 'type tmpscope)
			 ;; A backup hack.
			 (semantic-analyze-find-tag ps 'type scope))
		   (error nil)))

	(when pt
	  (funcall fcn pt)
	  ;; Note that we pass the original SCOPE in while recursing.
	  ;; so that the correct inheritance model is passed along.
	  (semantic-analyze-scoped-inherited-tag-map pt fcn scope)
	  )))
    nil))

;;; ANALYZER
;;
;; Create the scope structure for use in the Analyzer.
;;
;;;###autoload
(defun semantic-calculate-scope (&optional point)
  "Calculate the scope at POINT.
If POINT is not provided, then use the current location of point.
The class returned from the scope calculation is variable
`semantic-scope-cache'."
  (interactive)
  (if (not (and (featurep 'semantic/db) semanticdb-current-database))
      nil ;; Don't do anything...
    (require 'semantic/db-typecache)
    (if (not point) (setq point (point)))
    (when (called-interactively-p 'any)
      (semantic-fetch-tags)
      (semantic-scope-reset-cache))
    (save-excursion
      (goto-char point)
      (let* ((TAG  (semantic-current-tag))
	     (scopecache
	      (semanticdb-cache-get semanticdb-current-table
				    'semantic-scope-cache))
	     )
	(when (not (semantic-equivalent-tag-p TAG (oref scopecache tag)))
	  (semantic-reset scopecache))
	(if (oref scopecache tag)
	    ;; Even though we can recycle most of the scope, we
	    ;; need to redo the local variables since those change
	    ;; as you move about the tag.
	    (condition-case nil
		(oset scopecache localvar (semantic-get-all-local-variables))
	      (error nil))

	  (let* (;; Step 1:
		 (scopetypes (semantic-analyze-scoped-types point))
		 (parents (semantic-analyze-scope-nested-tags point scopetypes))
		 (parentinherited (semantic-analyze-scope-lineage-tags
				   parents scopetypes))
		 )
	    (oset scopecache tag TAG)
	    (oset scopecache scopetypes scopetypes)
	    (oset scopecache parents parents)
	    (oset scopecache parentinheritance parentinherited)

	    (let* (;; Step 2:
		   (scope (when (or scopetypes parents)
			    (semantic-analyze-scoped-tags scopetypes scopecache))
			  )
		   ;; Step 3:
		   (localargs (semantic-get-local-arguments))
		   (localvar (condition-case nil
				 (semantic-get-all-local-variables)
			       (error nil)))
		   )

	      ;; Try looking for parents again.
	      (when (not parentinherited)
		(setq parentinherited (semantic-analyze-scope-lineage-tags
				       parents (append scopetypes scope)))
		(when parentinherited
		  (oset scopecache parentinheritance parentinherited)
		  ;; Try calculating the scope again with the new inherited parent list.
		  (setq scope (when (or scopetypes parents)
				(semantic-analyze-scoped-tags scopetypes scopecache))
			)))

	      ;; Fill out the scope.
	      (oset scopecache scope scope)
	      (oset scopecache fullscope (append scopetypes scope parents))
	      (oset scopecache localargs localargs)
	      (oset scopecache localvar localvar)
	      )))
	;; Make sure we become dependent on the typecache.
	(semanticdb-typecache-add-dependant scopecache)
	;; Handy debug output.
	(when (called-interactively-p 'any)
	  (require 'eieio-datadebug)
	  (data-debug-show scopecache))
	;; Return ourselves, but make a clone first so that the caller
	;; can reset the scope cache without affecting others.
	(clone scopecache)))))

(defun semantic-scope-find (name &optional class scope-in)
  "Find the tag with NAME, and optional CLASS in the current SCOPE-IN.
Searches various elements of the scope for NAME.  Return ALL the
hits in order, with the first tag being in the closest scope."
  (let ((scope (or scope-in (semantic-calculate-scope)))
	(ans nil))
    ;; Is the passed in scope really a scope?  if so, look through
    ;; the options in that scope.
    (if (semantic-scope-cache-p scope)
	(let* ((la
		;; This should be first, but bugs in the
		;; C parser will turn function calls into
		;; assumed int return function prototypes.  Yuck!
		(semantic-find-tags-by-name name (oref scope localargs)))
	       (lv
		(semantic-find-tags-by-name name (oref scope localvar)))
	       (fullscoperaw (oref scope fullscope))
	       (sc (semantic-find-tags-by-name name fullscoperaw))
	       (typescoperaw  (oref scope typescope))
	       (tsc (semantic-find-tags-by-name name typescoperaw))
	       )
	  (setq ans
		(if class
		    ;; Scan out things not of the right class.
		    (semantic-find-tags-by-class class (append la lv sc tsc))
		  (append la lv sc tsc))
		)

	  (when (and (not ans) (or typescoperaw fullscoperaw))
	    (let ((namesplit (semantic-analyze-split-name name)))
	      (when (consp namesplit)
		;; It may be we need to hack our way through type typescope.
		(while namesplit
		  (setq ans (append
			     (semantic-find-tags-by-name (car namesplit)
							 typescoperaw)
			     (semantic-find-tags-by-name (car namesplit)
							 fullscoperaw)
			     ))
		  (if (not ans)
		      (setq typescoperaw nil)
		    (when (cdr namesplit)
		      (setq typescoperaw (semantic-tag-type-members
					  (car ans)))))

		  (setq namesplit (cdr namesplit)))
		;; Once done, store the current typecache lookup
		(oset scope typescope
		      (append typescoperaw (oref scope typescope)))
		)))
	  ;; Return it.
	  ans)
      ;; Not a real scope.  Our scope calculation analyze parts of
      ;; what it finds, and needs to pass lists through to do it's work.
      ;; Tread that list as a singly entry.
      (if class
	  (semantic-find-tags-by-class class scope)
	scope)
      )))

;;; DUMP
;;
(cl-defmethod semantic-analyze-show ((context semantic-scope-cache))
  "Insert CONTEXT into the current buffer in a nice way."
  (require 'semantic/analyze)
  (semantic-analyze-princ-sequence (oref context scopetypes) "-> ScopeTypes: " )
  (semantic-analyze-princ-sequence (oref context parents) "-> Parents: " )
  (semantic-analyze-princ-sequence (oref context scope) "-> Scope: " )
  ;;(semantic-analyze-princ-sequence (oref context fullscope) "Fullscope:  " )
  (semantic-analyze-princ-sequence (oref context localargs) "-> Local Args: " )
  (semantic-analyze-princ-sequence (oref context localvar) "-> Local Vars: " )
  )

(provide 'semantic/scope)

;; Local variables:
;; generated-autoload-file: "loaddefs.el"
;; generated-autoload-load-name: "semantic/scope"
;; End:

;;; semantic/scope.el ends here