analyze.el 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. ;;; semantic/analyze.el --- Analyze semantic tags against local context
  2. ;; Copyright (C) 2000-2005, 2007-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric M. Ludlam <zappo@gnu.org>
  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. ;; Semantic, as a tool, provides a nice list of searchable tags.
  18. ;; That information can provide some very accurate answers if the current
  19. ;; context of a position is known.
  20. ;;
  21. ;; Semantic-ctxt provides ways of analyzing, and manipulating the
  22. ;; semantic context of a language in code.
  23. ;;
  24. ;; This library provides routines for finding intelligent answers to
  25. ;; tough problems, such as if an argument to a function has the correct
  26. ;; return type, or all possible tags that fit in a given local context.
  27. ;;
  28. ;;; Vocabulary:
  29. ;;
  30. ;; Here are some words used to describe different things in the analyzer:
  31. ;;
  32. ;; tag - A single entity
  33. ;; prefix - The beginning of a symbol, usually used to look up something
  34. ;; incomplete.
  35. ;; type - The name of a datatype in the language.
  36. ;; metatype - If a type is named in a declaration like:
  37. ;; struct moose somevariable;
  38. ;; that name "moose" can be turned into a concrete type.
  39. ;; tag sequence - In C code, a list of dereferences, such as:
  40. ;; this.that.theother();
  41. ;; parent - For a datatype in an OO language, another datatype
  42. ;; inherited from. This excludes interfaces.
  43. ;; scope - A list of tags that can be dereferenced that cannot
  44. ;; be found from the global namespace.
  45. ;; scopetypes - A list of tags which are datatype that contain
  46. ;; the scope. The scopetypes need to have the scope extracted
  47. ;; in a way that honors the type of inheritance.
  48. ;; nest/nested - When one tag is contained entirely in another.
  49. ;;
  50. ;; context - A semantic datatype representing a point in a buffer.
  51. ;;
  52. ;; constraint - If a context specifies a specific datatype is needed,
  53. ;; that is a constraint.
  54. ;; constants - Some datatypes define elements of themselves as a
  55. ;; constant. These need to be returned as there would be no
  56. ;; other possible completions.
  57. (eval-when-compile (require 'cl))
  58. (require 'semantic)
  59. (require 'semantic/format)
  60. (require 'semantic/ctxt)
  61. (require 'semantic/scope)
  62. (require 'semantic/sort)
  63. (require 'semantic/analyze/fcn)
  64. (eval-when-compile (require 'semantic/find))
  65. (declare-function data-debug-new-buffer "data-debug")
  66. (declare-function data-debug-insert-object-slots "eieio-datadebug")
  67. ;;; Code:
  68. (defvar semantic-analyze-error-stack nil
  69. "Collection of any errors thrown during analysis.")
  70. (defun semantic-analyze-push-error (err)
  71. "Push the error in ERR-DATA onto the error stack.
  72. Argument ERR."
  73. (push err semantic-analyze-error-stack))
  74. ;;; Analysis Classes
  75. ;;
  76. ;; These classes represent what a context is. Different types
  77. ;; of contexts provide differing amounts of information to help
  78. ;; provide completions.
  79. ;;
  80. (defclass semantic-analyze-context ()
  81. ((bounds :initarg :bounds
  82. :type list
  83. :documentation "The bounds of this context.
  84. Usually bound to the dimension of a single symbol or command.")
  85. (prefix :initarg :prefix
  86. :type list
  87. :documentation "List of tags defining local text.
  88. This can be nil, or a list where the last element can be a string
  89. representing text that may be incomplete. Preceding elements
  90. must be semantic tags representing variables or functions
  91. called in a dereference sequence.")
  92. (prefixclass :initarg :prefixclass
  93. :type list
  94. :documentation "Tag classes expected at this context.
  95. These are classes for tags, such as 'function, or 'variable.")
  96. (prefixtypes :initarg :prefixtypes
  97. :type list
  98. :documentation "List of tags defining types for :prefix.
  99. This list is one shorter than :prefix. Each element is a semantic
  100. tag representing a type matching the semantic tag in the same
  101. position in PREFIX.")
  102. (scope :initarg :scope
  103. :type (or null semantic-scope-cache)
  104. :documentation "List of tags available in scopetype.
  105. See `semantic-analyze-scoped-tags' for details.")
  106. (buffer :initarg :buffer
  107. :type buffer
  108. :documentation "The buffer this context is derived from.")
  109. (errors :initarg :errors
  110. :documentation "Any errors thrown an caught during analysis.")
  111. )
  112. "Base analysis data for any context.")
  113. (defclass semantic-analyze-context-assignment (semantic-analyze-context)
  114. ((assignee :initarg :assignee
  115. :type list
  116. :documentation "A sequence of tags for an assignee.
  117. This is a variable into which some value is being placed. The last
  118. item in the list is the variable accepting the value. Earlier
  119. tags represent the variables being dereferenced to get to the
  120. assignee."))
  121. "Analysis class for a value in an assignment.")
  122. (defclass semantic-analyze-context-functionarg (semantic-analyze-context)
  123. ((function :initarg :function
  124. :type list
  125. :documentation "A sequence of tags for a function.
  126. This is a function being called. The cursor will be in the position
  127. of an argument.
  128. The last tag in :function is the function being called. Earlier
  129. tags represent the variables being dereferenced to get to the
  130. function.")
  131. (index :initarg :index
  132. :type integer
  133. :documentation "The index of the argument for this context.
  134. If a function takes 4 arguments, this value should be bound to
  135. the values 1 through 4.")
  136. (argument :initarg :argument
  137. :type list
  138. :documentation "A sequence of tags for the :index argument.
  139. The argument can accept a value of some type, and this contains the
  140. tag for that definition. It should be a tag, but might
  141. be just a string in some circumstances.")
  142. )
  143. "Analysis class for a value as a function argument.")
  144. (defclass semantic-analyze-context-return (semantic-analyze-context)
  145. () ; No extra data.
  146. "Analysis class for return data.
  147. Return data methods identify the required type by the return value
  148. of the parent function.")
  149. ;;; METHODS
  150. ;;
  151. ;; Simple methods against the context classes.
  152. ;;
  153. (defmethod semantic-analyze-type-constraint
  154. ((context semantic-analyze-context) &optional desired-type)
  155. "Return a type constraint for completing :prefix in CONTEXT.
  156. Optional argument DESIRED-TYPE may be a non-type tag to analyze."
  157. (when (semantic-tag-p desired-type)
  158. ;; Convert the desired type if needed.
  159. (if (not (eq (semantic-tag-class desired-type) 'type))
  160. (setq desired-type (semantic-tag-type desired-type)))
  161. ;; Protect against plain strings
  162. (cond ((stringp desired-type)
  163. (setq desired-type (list desired-type 'type)))
  164. ((and (stringp (car desired-type))
  165. (not (semantic-tag-p desired-type)))
  166. (setq desired-type (list (car desired-type) 'type)))
  167. ((semantic-tag-p desired-type)
  168. ;; We have a tag of some sort. Yay!
  169. nil)
  170. (t (setq desired-type nil))
  171. )
  172. desired-type))
  173. (defmethod semantic-analyze-type-constraint
  174. ((context semantic-analyze-context-functionarg))
  175. "Return a type constraint for completing :prefix in CONTEXT."
  176. (call-next-method context (car (oref context argument))))
  177. (defmethod semantic-analyze-type-constraint
  178. ((context semantic-analyze-context-assignment))
  179. "Return a type constraint for completing :prefix in CONTEXT."
  180. (call-next-method context (car (reverse (oref context assignee)))))
  181. (defmethod semantic-analyze-interesting-tag
  182. ((context semantic-analyze-context))
  183. "Return a tag from CONTEXT that would be most interesting to a user."
  184. (let ((prefix (reverse (oref context :prefix))))
  185. ;; Go back through the prefix until we find a tag we can return.
  186. (while (and prefix (not (semantic-tag-p (car prefix))))
  187. (setq prefix (cdr prefix)))
  188. ;; Return the found tag, or nil.
  189. (car prefix)))
  190. (defmethod semantic-analyze-interesting-tag
  191. ((context semantic-analyze-context-functionarg))
  192. "Try the base, and if that fails, return what we are assigning into."
  193. (or (call-next-method) (car-safe (oref context :function))))
  194. (defmethod semantic-analyze-interesting-tag
  195. ((context semantic-analyze-context-assignment))
  196. "Try the base, and if that fails, return what we are assigning into."
  197. (or (call-next-method) (car-safe (oref context :assignee))))
  198. ;;; ANALYSIS
  199. ;;
  200. ;; Start out with routines that will calculate useful parts of
  201. ;; the general analyzer function. These could be used directly
  202. ;; by an application that doesn't need to calculate the full
  203. ;; context.
  204. (define-overloadable-function semantic-analyze-find-tag-sequence (sequence &optional
  205. scope typereturn throwsym)
  206. "Attempt to find all tags in SEQUENCE.
  207. Optional argument LOCALVAR is the list of local variables to use when
  208. finding the details on the first element of SEQUENCE in case
  209. it is not found in the global set of tables.
  210. Optional argument SCOPE are additional terminals to search which are currently
  211. scoped. These are not local variables, but symbols available in a structure
  212. which doesn't need to be dereferenced.
  213. Optional argument TYPERETURN is a symbol in which the types of all found
  214. will be stored. If nil, that data is thrown away.
  215. Optional argument THROWSYM specifies a symbol the throw on non-recoverable error.")
  216. (defun semantic-analyze-find-tag-sequence-default (sequence &optional
  217. scope typereturn
  218. throwsym)
  219. "Attempt to find all tags in SEQUENCE.
  220. SCOPE are extra tags which are in scope.
  221. TYPERETURN is a symbol in which to place a list of tag classes that
  222. are found in SEQUENCE.
  223. Optional argument THROWSYM specifies a symbol the throw on non-recoverable error."
  224. (let ((s sequence) ; copy of the sequence
  225. (tmp nil) ; tmp find variable
  226. (tag nil) ; tag return list
  227. (tagtype nil) ; tag types return list
  228. (fname nil)
  229. (miniscope (when scope (clone scope)))
  230. )
  231. ;; First order check. Is this wholly contained in the typecache?
  232. (setq tmp (semanticdb-typecache-find sequence))
  233. (if tmp
  234. (progn
  235. ;; We are effectively done...
  236. (setq s nil)
  237. (setq tag (list tmp)))
  238. ;; For the first entry, it better be a variable, but it might
  239. ;; be in the local context too.
  240. ;; NOTE: Don't forget c++ namespace foo::bar.
  241. (setq tmp (or
  242. ;; Is this tag within our scope. Scopes can sometimes
  243. ;; shadow other things, so it goes first.
  244. (and scope (semantic-scope-find (car s) nil scope))
  245. ;; Find the tag out there... somewhere, but not in scope
  246. (semantic-analyze-find-tag (car s))
  247. ))
  248. (if (and (listp tmp) (semantic-tag-p (car tmp)))
  249. (setq tmp (semantic-analyze-select-best-tag tmp)))
  250. (if (not (semantic-tag-p tmp))
  251. (if throwsym
  252. (throw throwsym "Cannot find definition")
  253. (error "Cannot find definition for \"%s\"" (car s))))
  254. (setq s (cdr s))
  255. (setq tag (cons tmp tag)) ; tag is nil here...
  256. (setq fname (semantic-tag-file-name tmp))
  257. )
  258. ;; For the middle entries
  259. (while s
  260. ;; Using the tag found in TMP, let's find the tag
  261. ;; representing the full typeographic information of its
  262. ;; type, and use that to determine the search context for
  263. ;; (car s)
  264. (let* ((tmptype
  265. ;; In some cases the found TMP is a type,
  266. ;; and we can use it directly.
  267. (cond ((semantic-tag-of-class-p tmp 'type)
  268. ;; update the miniscope when we need to analyze types directly.
  269. (when miniscope
  270. (let ((rawscope
  271. (apply 'append
  272. (mapcar 'semantic-tag-type-members
  273. tagtype))))
  274. (oset miniscope fullscope rawscope)))
  275. ;; Now analyze the type to remove metatypes.
  276. (or (semantic-analyze-type tmp miniscope)
  277. tmp))
  278. (t
  279. (semantic-analyze-tag-type tmp scope))))
  280. (typefile
  281. (when tmptype
  282. (semantic-tag-file-name tmptype)))
  283. (slots nil))
  284. ;; Get the children
  285. (setq slots (semantic-analyze-scoped-type-parts tmptype scope))
  286. ;; find (car s) in the list o slots
  287. (setq tmp (semantic-find-tags-by-name (car s) slots))
  288. ;; If we have lots
  289. (if (and (listp tmp) (semantic-tag-p (car tmp)))
  290. (setq tmp (semantic-analyze-select-best-tag tmp)))
  291. ;; Make sure we have a tag.
  292. (if (not (semantic-tag-p tmp))
  293. (if (cdr s)
  294. ;; In the middle, we need to keep seeking our types out.
  295. (error "Cannot find definition for \"%s\"" (car s))
  296. ;; Else, it's ok to end with a non-tag
  297. (setq tmp (car s))))
  298. (setq fname (or typefile fname))
  299. (when (and fname (semantic-tag-p tmp)
  300. (not (semantic-tag-in-buffer-p tmp)))
  301. (semantic--tag-put-property tmp :filename fname))
  302. (setq tag (cons tmp tag))
  303. (setq tagtype (cons tmptype tagtype))
  304. )
  305. (setq s (cdr s)))
  306. (if typereturn (set typereturn (nreverse tagtype)))
  307. ;; Return the mess
  308. (nreverse tag)))
  309. (defun semantic-analyze-find-tag (name &optional tagclass scope)
  310. "Return the first tag found with NAME or nil if not found.
  311. Optional argument TAGCLASS specifies the class of tag to return,
  312. such as 'function or 'variable.
  313. Optional argument SCOPE specifies a scope object which has
  314. additional tags which are in SCOPE and do not need prefixing to
  315. find.
  316. This is a wrapper on top of semanticdb, semanticdb typecache,
  317. semantic-scope, and semantic search functions. Almost all
  318. searches use the same arguments."
  319. (let ((namelst (if (consp name) name ;; test if pre-split.
  320. (semantic-analyze-split-name name))))
  321. (cond
  322. ;; If the splitter gives us a list, use the sequence finder
  323. ;; to get the list. Since this routine is expected to return
  324. ;; only one tag, return the LAST tag found from the sequence
  325. ;; which is supposedly the nested reference.
  326. ;;
  327. ;; Of note, the SEQUENCE function below calls this function
  328. ;; (recursively now) so the names that we get from the above
  329. ;; fcn better not, in turn, be splittable.
  330. ((listp namelst)
  331. ;; If we had a split, then this is likely a c++ style namespace::name sequence,
  332. ;; so take a short-cut through the typecache.
  333. (or (semanticdb-typecache-find namelst)
  334. ;; Ok, not there, try the usual...
  335. (let ((seq (semantic-analyze-find-tag-sequence
  336. namelst scope nil)))
  337. (semantic-analyze-select-best-tag seq tagclass)
  338. )))
  339. ;; If NAME is solo, then do our searches for it here.
  340. ((stringp namelst)
  341. (let ((retlist (and scope (semantic-scope-find name tagclass scope))))
  342. (if retlist
  343. (semantic-analyze-select-best-tag
  344. retlist tagclass)
  345. (if (eq tagclass 'type)
  346. (semanticdb-typecache-find name)
  347. ;; Search in the typecache. First entries in a sequence are
  348. ;; often there.
  349. (setq retlist (semanticdb-typecache-find name))
  350. (if retlist
  351. retlist
  352. (semantic-analyze-select-best-tag
  353. (semanticdb-strip-find-results
  354. (semanticdb-find-tags-by-name name)
  355. 'name)
  356. tagclass)
  357. )))))
  358. )))
  359. ;;; SHORT ANALYSIS
  360. ;;
  361. ;; Create a mini-analysis of just the symbol under point.
  362. ;;
  363. (define-overloadable-function semantic-analyze-current-symbol
  364. (analyzehookfcn &optional position)
  365. "Call ANALYZEHOOKFCN after analyzing the symbol under POSITION.
  366. The ANALYZEHOOKFCN is called with the current symbol bounds, and the
  367. analyzed prefix. It should take the arguments (START END PREFIX).
  368. The ANALYZEHOOKFCN is only called if some sort of prefix with bounds was
  369. found under POSITION.
  370. The results of ANALYZEHOOKFCN is returned, or nil if there was nothing to
  371. call it with.
  372. For regular analysis, you should call `semantic-analyze-current-context'
  373. to calculate the context information. The purpose for this function is
  374. to provide a large number of non-cached analysis for filtering symbols."
  375. ;; Only do this in a Semantic enabled buffer.
  376. (when (not (semantic-active-p))
  377. (error "Cannot analyze buffers not supported by Semantic"))
  378. ;; Always refresh out tags in a safe way before doing the
  379. ;; context.
  380. (semantic-refresh-tags-safe)
  381. ;; Do the rest of the analysis.
  382. (save-match-data
  383. (save-excursion
  384. (:override)))
  385. )
  386. (defun semantic-analyze-current-symbol-default (analyzehookfcn position)
  387. "Call ANALYZEHOOKFCN on the analyzed symbol at POSITION."
  388. (let* ((semantic-analyze-error-stack nil)
  389. (LLstart (current-time))
  390. (prefixandbounds (semantic-ctxt-current-symbol-and-bounds (or position (point))))
  391. (prefix (car prefixandbounds))
  392. (bounds (nth 2 prefixandbounds))
  393. (scope (semantic-calculate-scope position))
  394. (end nil)
  395. )
  396. ;; Only do work if we have bounds (meaning a prefix to complete)
  397. (when bounds
  398. (if debug-on-error
  399. (catch 'unfindable
  400. ;; If debug on error is on, allow debugging in this fcn.
  401. (setq prefix (semantic-analyze-find-tag-sequence
  402. prefix scope 'prefixtypes 'unfindable)))
  403. ;; Debug on error is off. Capture errors and move on
  404. (condition-case err
  405. ;; NOTE: This line is duplicated in
  406. ;; semantic-analyzer-debug-global-symbol
  407. ;; You will need to update both places.
  408. (setq prefix (semantic-analyze-find-tag-sequence
  409. prefix scope 'prefixtypes))
  410. (error (semantic-analyze-push-error err))))
  411. (setq end (current-time))
  412. ;;(message "Analysis took %.2f sec" (semantic-elapsed-time LLstart end))
  413. )
  414. (when prefix
  415. (prog1
  416. (funcall analyzehookfcn (car bounds) (cdr bounds) prefix)
  417. ;;(setq end (current-time))
  418. ;;(message "hookfcn took %.5f sec" (semantic-elapsed-time LLstart end))
  419. )
  420. )))
  421. ;;; MAIN ANALYSIS
  422. ;;
  423. ;; Create a full-up context analysis.
  424. ;;
  425. ;;;###autoload
  426. (define-overloadable-function semantic-analyze-current-context (&optional position)
  427. "Analyze the current context at optional POSITION.
  428. If called interactively, display interesting information about POSITION
  429. in a separate buffer.
  430. Returns an object based on symbol `semantic-analyze-context'.
  431. This function can be overridden with the symbol `analyze-context'.
  432. When overriding this function, your override will be called while
  433. cursor is at POSITION. In addition, your function will not be called
  434. if a cached copy of the return object is found."
  435. (interactive "d")
  436. ;; Only do this in a Semantic enabled buffer.
  437. (when (not (semantic-active-p))
  438. (error "Cannot analyze buffers not supported by Semantic"))
  439. ;; Always refresh out tags in a safe way before doing the
  440. ;; context.
  441. (semantic-refresh-tags-safe)
  442. ;; Do the rest of the analysis.
  443. (if (not position) (setq position (point)))
  444. (save-excursion
  445. (goto-char position)
  446. (let* ((answer (semantic-get-cache-data 'current-context)))
  447. (with-syntax-table semantic-lex-syntax-table
  448. (when (not answer)
  449. (setq answer (:override))
  450. (when (and answer (oref answer bounds))
  451. (with-slots (bounds) answer
  452. (semantic-cache-data-to-buffer (current-buffer)
  453. (car bounds)
  454. (cdr bounds)
  455. answer
  456. 'current-context
  457. 'exit-cache-zone)))
  458. ;; Check for interactivity
  459. (when (called-interactively-p 'any)
  460. (if answer
  461. (semantic-analyze-pop-to-context answer)
  462. (message "No Context."))
  463. ))
  464. answer))))
  465. (defun semantic-analyze-current-context-default (position)
  466. "Analyze the current context at POSITION.
  467. Returns an object based on symbol `semantic-analyze-context'."
  468. (let* ((semantic-analyze-error-stack nil)
  469. (context-return nil)
  470. (prefixandbounds (semantic-ctxt-current-symbol-and-bounds (or position (point))))
  471. (prefix (car prefixandbounds))
  472. (bounds (nth 2 prefixandbounds))
  473. ;; @todo - vv too early to really know this answer! vv
  474. (prefixclass (semantic-ctxt-current-class-list))
  475. (prefixtypes nil)
  476. (scope (semantic-calculate-scope position))
  477. (function nil)
  478. (fntag nil)
  479. arg fntagend argtag
  480. assign asstag
  481. )
  482. ;; Pattern for Analysis:
  483. ;;
  484. ;; Step 1: Calculate DataTypes in Scope:
  485. ;;
  486. ;; a) Calculate the scope (above)
  487. ;;
  488. ;; Step 2: Parse context
  489. ;;
  490. ;; a) Identify function being called, or variable assignment,
  491. ;; and find source tags for those references
  492. ;; b) Identify the prefix (text cursor is on) and find the source
  493. ;; tags for those references.
  494. ;;
  495. ;; Step 3: Assemble an object
  496. ;;
  497. ;; Step 2 a:
  498. (setq function (semantic-ctxt-current-function))
  499. (when function
  500. ;; Calculate the argument for the function if there is one.
  501. (setq arg (semantic-ctxt-current-argument))
  502. ;; Find a tag related to the function name.
  503. (condition-case err
  504. (setq fntag
  505. (semantic-analyze-find-tag-sequence function scope))
  506. (error (semantic-analyze-push-error err)))
  507. ;; fntag can have the last entry as just a string, meaning we
  508. ;; could not find the core datatype. In this case, the searches
  509. ;; below will not work.
  510. (when (stringp (car (last fntag)))
  511. ;; Take a wild guess!
  512. (setcar (last fntag) (semantic-tag (car (last fntag)) 'function))
  513. )
  514. (when fntag
  515. (let ((fcn (semantic-find-tags-by-class 'function fntag)))
  516. (when (not fcn)
  517. (let ((ty (semantic-find-tags-by-class 'type fntag)))
  518. (when ty
  519. ;; We might have a constructor with the same name as
  520. ;; the found datatype.
  521. (setq fcn (semantic-find-tags-by-name
  522. (semantic-tag-name (car ty))
  523. (semantic-tag-type-members (car ty))))
  524. (if fcn
  525. (let ((lp fcn))
  526. (while lp
  527. (when (semantic-tag-get-attribute (car lp)
  528. :constructor)
  529. (setq fcn (cons (car lp) fcn)))
  530. (setq lp (cdr lp))))
  531. ;; Give up, go old school
  532. (setq fcn fntag))
  533. )))
  534. (setq fntagend (car (reverse fcn))
  535. argtag
  536. (when (semantic-tag-p fntagend)
  537. (nth (1- arg) (semantic-tag-function-arguments fntagend)))
  538. fntag fcn))))
  539. ;; Step 2 b:
  540. ;; Only do work if we have bounds (meaning a prefix to complete)
  541. (when bounds
  542. (if debug-on-error
  543. (catch 'unfindable
  544. ;; If debug on error is on, allow debugging in this fcn.
  545. (setq prefix (semantic-analyze-find-tag-sequence
  546. prefix scope 'prefixtypes 'unfindable)))
  547. ;; Debug on error is off. Capture errors and move on
  548. (condition-case err
  549. ;; NOTE: This line is duplicated in
  550. ;; semantic-analyzer-debug-global-symbol
  551. ;; You will need to update both places.
  552. (setq prefix (semantic-analyze-find-tag-sequence
  553. prefix scope 'prefixtypes))
  554. (error (semantic-analyze-push-error err))))
  555. )
  556. ;; Step 3:
  557. (cond
  558. (fntag
  559. ;; If we found a tag for our function, we can go into
  560. ;; functional context analysis mode, meaning we have a type
  561. ;; for the argument.
  562. (setq context-return
  563. (semantic-analyze-context-functionarg
  564. "functionargument"
  565. :buffer (current-buffer)
  566. :function fntag
  567. :index arg
  568. :argument (list argtag)
  569. :scope scope
  570. :prefix prefix
  571. :prefixclass prefixclass
  572. :bounds bounds
  573. :prefixtypes prefixtypes
  574. :errors semantic-analyze-error-stack)))
  575. ;; No function, try assignment
  576. ((and (setq assign (semantic-ctxt-current-assignment))
  577. ;; We have some sort of an assignment
  578. (condition-case err
  579. (setq asstag (semantic-analyze-find-tag-sequence
  580. assign scope))
  581. (error (semantic-analyze-push-error err)
  582. nil)))
  583. (setq context-return
  584. (semantic-analyze-context-assignment
  585. "assignment"
  586. :buffer (current-buffer)
  587. :assignee asstag
  588. :scope scope
  589. :bounds bounds
  590. :prefix prefix
  591. :prefixclass prefixclass
  592. :prefixtypes prefixtypes
  593. :errors semantic-analyze-error-stack)))
  594. ;; TODO: Identify return value condition.
  595. ;;((setq return .... what to do?)
  596. ;; ...)
  597. (bounds
  598. ;; Nothing in particular
  599. (setq context-return
  600. (semantic-analyze-context
  601. "context"
  602. :buffer (current-buffer)
  603. :scope scope
  604. :bounds bounds
  605. :prefix prefix
  606. :prefixclass prefixclass
  607. :prefixtypes prefixtypes
  608. :errors semantic-analyze-error-stack)))
  609. (t (setq context-return nil))
  610. )
  611. ;; Return our context.
  612. context-return))
  613. (defun semantic-adebug-analyze (&optional ctxt)
  614. "Perform `semantic-analyze-current-context'.
  615. Display the results as a debug list.
  616. Optional argument CTXT is the context to show."
  617. (interactive)
  618. (require 'data-debug)
  619. (let ((start (current-time))
  620. (ctxt (or ctxt (semantic-analyze-current-context)))
  621. (end (current-time)))
  622. (if (not ctxt)
  623. (message "No Analyzer Results")
  624. (message "Analysis took %.2f seconds."
  625. (semantic-elapsed-time start end))
  626. (semantic-analyze-pulse ctxt)
  627. (if ctxt
  628. (progn
  629. (data-debug-new-buffer "*Analyzer ADEBUG*")
  630. (data-debug-insert-object-slots ctxt "]"))
  631. (message "No Context to analyze here.")))))
  632. ;;; DEBUG OUTPUT
  633. ;;
  634. ;; Friendly output of a context analysis.
  635. ;;
  636. (declare-function pulse-momentary-highlight-region "pulse")
  637. (defmethod semantic-analyze-pulse ((context semantic-analyze-context))
  638. "Pulse the region that CONTEXT affects."
  639. (require 'pulse)
  640. (with-current-buffer (oref context :buffer)
  641. (let ((bounds (oref context :bounds)))
  642. (when bounds
  643. (pulse-momentary-highlight-region (car bounds) (cdr bounds))))))
  644. (defcustom semantic-analyze-summary-function 'semantic-format-tag-prototype
  645. "Function to use when creating items in Imenu.
  646. Some useful functions are found in `semantic-format-tag-functions'."
  647. :group 'semantic
  648. :type semantic-format-tag-custom-list)
  649. (defun semantic-analyze-princ-sequence (sequence &optional prefix buff)
  650. "Send the tag SEQUENCE to standard out.
  651. Use PREFIX as a label.
  652. Use BUFF as a source of override methods."
  653. (while sequence
  654. (princ prefix)
  655. (cond
  656. ((semantic-tag-p (car sequence))
  657. (princ (funcall semantic-analyze-summary-function
  658. (car sequence))))
  659. ((stringp (car sequence))
  660. (princ "\"")
  661. (princ (semantic--format-colorize-text (car sequence) 'variable))
  662. (princ "\""))
  663. (t
  664. (princ (format "'%S" (car sequence)))))
  665. (princ "\n")
  666. (setq sequence (cdr sequence))
  667. (setq prefix (make-string (length prefix) ? ))
  668. ))
  669. (defmethod semantic-analyze-show ((context semantic-analyze-context))
  670. "Insert CONTEXT into the current buffer in a nice way."
  671. (semantic-analyze-princ-sequence (oref context prefix) "Prefix: " )
  672. (semantic-analyze-princ-sequence (oref context prefixclass) "Prefix Classes: ")
  673. (semantic-analyze-princ-sequence (oref context prefixtypes) "Prefix Types: ")
  674. (semantic-analyze-princ-sequence (oref context errors) "Encountered Errors: ")
  675. (princ "--------\n")
  676. ;(semantic-analyze-princ-sequence (oref context scopetypes) "Scope Types: ")
  677. ;(semantic-analyze-princ-sequence (oref context scope) "Scope: ")
  678. ;(semantic-analyze-princ-sequence (oref context localvariables) "LocalVars: ")
  679. (when (oref context scope)
  680. (semantic-analyze-show (oref context scope)))
  681. )
  682. (defmethod semantic-analyze-show ((context semantic-analyze-context-assignment))
  683. "Insert CONTEXT into the current buffer in a nice way."
  684. (semantic-analyze-princ-sequence (oref context assignee) "Assignee: ")
  685. (call-next-method))
  686. (defmethod semantic-analyze-show ((context semantic-analyze-context-functionarg))
  687. "Insert CONTEXT into the current buffer in a nice way."
  688. (semantic-analyze-princ-sequence (oref context function) "Function: ")
  689. (princ "Argument Index: ")
  690. (princ (oref context index))
  691. (princ "\n")
  692. (semantic-analyze-princ-sequence (oref context argument) "Argument: ")
  693. (call-next-method))
  694. (defun semantic-analyze-pop-to-context (context)
  695. "Display CONTEXT in a temporary buffer.
  696. CONTEXT's content is described in `semantic-analyze-current-context'."
  697. (semantic-analyze-pulse context)
  698. (with-output-to-temp-buffer "*Semantic Context Analysis*"
  699. (princ "Context Type: ")
  700. (princ (object-name context))
  701. (princ "\n")
  702. (princ "Bounds: ")
  703. (princ (oref context bounds))
  704. (princ "\n")
  705. (semantic-analyze-show context)
  706. )
  707. (shrink-window-if-larger-than-buffer
  708. (get-buffer-window "*Semantic Context Analysis*"))
  709. )
  710. (provide 'semantic/analyze)
  711. ;; Local variables:
  712. ;; generated-autoload-file: "loaddefs.el"
  713. ;; generated-autoload-load-name: "semantic/analyze"
  714. ;; End:
  715. ;;; semantic/analyze.el ends here