scope.el 31 KB

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