scope.el 29 KB

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