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