]> code.delx.au - gnu-emacs/blob - lisp/cedet/semantic/analyze.el
Add arch tagline
[gnu-emacs] / lisp / cedet / semantic / analyze.el
1 ;;; semantic/analyze.el --- Analyze semantic tags against local context
2
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009
4 ;; Free Software Foundation, Inc.
5
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24 ;;
25 ;; Semantic, as a tool, provides a nice list of searchable tags.
26 ;; That information can provide some very accurate answers if the current
27 ;; context of a position is known.
28 ;;
29 ;; Semantic-ctxt provides ways of analyzing, and manipulating the
30 ;; semantic context of a language in code.
31 ;;
32 ;; This library provides routines for finding intelligent answers to
33 ;; tough problems, such as if an argument to a function has the correct
34 ;; return type, or all possible tags that fit in a given local context.
35 ;;
36
37 ;;; Vocabulary:
38 ;;
39 ;; Here are some words used to describe different things in the analyzer:
40 ;;
41 ;; tag - A single entity
42 ;; prefix - The beginning of a symbol, usually used to look up something
43 ;; incomplete.
44 ;; type - The name of a datatype in the langauge.
45 ;; metatype - If a type is named in a declaration like:
46 ;; struct moose somevariable;
47 ;; that name "moose" can be turned into a concrete type.
48 ;; tag sequence - In C code, a list of dereferences, such as:
49 ;; this.that.theother();
50 ;; parent - For a datatype in an OO language, another datatype
51 ;; inherited from. This excludes interfaces.
52 ;; scope - A list of tags that can be dereferenced that cannot
53 ;; be found from the global namespace.
54 ;; scopetypes - A list of tags which are datatype that contain
55 ;; the scope. The scopetypes need to have the scope extracted
56 ;; in a way that honors the type of inheritance.
57 ;; nest/nested - When one tag is contained entirely in another.
58 ;;
59 ;; context - A semantic datatype representing a point in a buffer.
60 ;;
61 ;; constriant - If a context specifies a specific datatype is needed,
62 ;; that is a constraint.
63 ;; constants - Some datatypes define elements of themselves as a
64 ;; constant. These need to be returned as there would be no
65 ;; other possible completions.
66
67 (require 'semantic)
68 (require 'semantic/format)
69 (require 'semantic/ctxt)
70 (require 'semantic/scope)
71 (require 'semantic/sort)
72 (require 'semantic/analyze/fcn)
73
74 (eval-when-compile (require 'semantic/find))
75
76 (declare-function data-debug-new-buffer "data-debug")
77 (declare-function data-debug-insert-object-slots "eieio-datadebug")
78
79 ;;; Code:
80 (defvar semantic-analyze-error-stack nil
81 "Collection of any errors thrown during analysis.")
82
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))
87
88 ;;; Analysis Classes
89 ;;
90 ;; These classes represent what a context is. Different types
91 ;; of contexts provide differing amounts of information to help
92 ;; provide completions.
93 ;;
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. Preceeding 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 clases 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.")
125 )
126 "Base analysis data for a any context.")
127
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 derefernece to get to the
135 assignee."))
136 "Analysis class for a value in an assignment.")
137
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.")
158 )
159 "Analysis class for a value as a function argument.")
160
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 requred type by the return value
165 of the parent function.")
166
167 ;;; METHODS
168 ;;
169 ;; Simple methods against the context classes.
170 ;;
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))
189 )
190 desired-type))
191
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))))
196
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)))))
201
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)))
211
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))))
216
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))))
221
222 ;;; ANALYSIS
223 ;;
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.
228
229 (define-overloadable-function semantic-analyze-find-tag-sequence (sequence &optional
230 scope typereturn throwsym)
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 dereferneced.
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
242 (defun semantic-analyze-find-tag-sequence-default (sequence &optional
243 scope typereturn
244 throwsym)
245 "Attempt to find all tags in SEQUENCE.
246 SCOPE are extra tags which are in scope.
247 TYPERETURN is a symbol in which to place a list of tag classes that
248 are found in SEQUENCE.
249 Optional argument THROWSYM specifies a symbol the throw on non-recoverable error."
250 (let ((s sequence) ; copy of the sequence
251 (tmp nil) ; tmp find variable
252 (tag nil) ; tag return list
253 (tagtype nil) ; tag types return list
254 (fname nil)
255 (miniscope (clone scope))
256 )
257 ;; First order check. Is this wholely contained in the typecache?
258 (setq tmp (semanticdb-typecache-find sequence))
259
260 (if tmp
261 (progn
262 ;; We are effectively done...
263 (setq s nil)
264 (setq tag (list tmp)))
265
266 ;; For the first entry, it better be a variable, but it might
267 ;; be in the local context too.
268 ;; NOTE: Don't forget c++ namespace foo::bar.
269 (setq tmp (or
270 ;; Is this tag within our scope. Scopes can sometimes
271 ;; shadow other things, so it goes first.
272 (and scope (semantic-scope-find (car s) nil scope))
273 ;; Find the tag out there... somewhere, but not in scope
274 (semantic-analyze-find-tag (car s))
275 ))
276
277 (if (and (listp tmp) (semantic-tag-p (car tmp)))
278 (setq tmp (semantic-analyze-select-best-tag tmp)))
279 (if (not (semantic-tag-p tmp))
280 (if throwsym
281 (throw throwsym "Cannot find definition")
282 (error "Cannot find definition for \"%s\"" (car s))))
283 (setq s (cdr s))
284 (setq tag (cons tmp tag)) ; tag is nil here...
285 (setq fname (semantic-tag-file-name tmp))
286 )
287
288 ;; For the middle entries
289 (while s
290 ;; Using the tag found in TMP, lets find the tag
291 ;; representing the full typeographic information of its
292 ;; type, and use that to determine the search context for
293 ;; (car s)
294 (let* ((tmptype
295 ;; In some cases the found TMP is a type,
296 ;; and we can use it directly.
297 (cond ((semantic-tag-of-class-p tmp 'type)
298 ;; update the miniscope when we need to analyze types directly.
299 (let ((rawscope
300 (apply 'append
301 (mapcar 'semantic-tag-type-members
302 tagtype))))
303 (oset miniscope fullscope rawscope))
304 ;; Now analayze the type to remove metatypes.
305 (or (semantic-analyze-type tmp miniscope)
306 tmp))
307 (t
308 (semantic-analyze-tag-type tmp scope))))
309 (typefile
310 (when tmptype
311 (semantic-tag-file-name tmptype)))
312 (slots nil))
313
314 ;; Get the children
315 (setq slots (semantic-analyze-scoped-type-parts tmptype scope))
316
317 ;; find (car s) in the list o slots
318 (setq tmp (semantic-find-tags-by-name (car s) slots))
319
320 ;; If we have lots
321 (if (and (listp tmp) (semantic-tag-p (car tmp)))
322 (setq tmp (semantic-analyze-select-best-tag tmp)))
323
324 ;; Make sure we have a tag.
325 (if (not (semantic-tag-p tmp))
326 (if (cdr s)
327 ;; In the middle, we need to keep seeking our types out.
328 (error "Cannot find definition for \"%s\"" (car s))
329 ;; Else, it's ok to end with a non-tag
330 (setq tmp (car s))))
331
332 (setq fname (or typefile fname))
333 (when (and fname (semantic-tag-p tmp)
334 (not (semantic-tag-in-buffer-p tmp)))
335 (semantic--tag-put-property tmp :filename fname))
336 (setq tag (cons tmp tag))
337 (setq tagtype (cons tmptype tagtype))
338 )
339 (setq s (cdr s)))
340
341 (if typereturn (set typereturn (nreverse tagtype)))
342 ;; Return the mess
343 (nreverse tag)))
344
345 (defun semantic-analyze-find-tag (name &optional tagclass scope)
346 "Return the first tag found with NAME or nil if not found.
347 Optional argument TAGCLASS specifies the class of tag to return, such
348 as 'function or 'variable.
349 Optional argument SCOPE specifies a scope object which has
350 additional tags which are in SCOPE and do not need prefixing to
351 find.
352
353 This is a wrapper on top of semanticdb, semanticdb-typecache,
354 semantic-scope, and semantic search functions. Almost all
355 searches use the same arguments."
356 (let ((namelst (if (consp name) name ;; test if pre-split.
357 (semantic-analyze-split-name name))))
358 (cond
359 ;; If the splitter gives us a list, use the sequence finder
360 ;; to get the list. Since this routine is expected to return
361 ;; only one tag, return the LAST tag found from the sequence
362 ;; which is supposedly the nested reference.
363 ;;
364 ;; Of note, the SEQUENCE function below calls this function
365 ;; (recursively now) so the names that we get from the above
366 ;; fcn better not, in turn, be splittable.
367 ((listp namelst)
368 ;; If we had a split, then this is likely a c++ style namespace::name sequence,
369 ;; so take a short-cut through the typecache.
370 (or (semanticdb-typecache-find namelst)
371 ;; Ok, not there, try the usual...
372 (let ((seq (semantic-analyze-find-tag-sequence
373 namelst scope nil)))
374 (semantic-analyze-select-best-tag seq tagclass)
375 )))
376 ;; If NAME is solo, then do our searches for it here.
377 ((stringp namelst)
378 (let ((retlist (and scope (semantic-scope-find name tagclass scope))))
379 (if retlist
380 (semantic-analyze-select-best-tag
381 retlist tagclass)
382 (if (eq tagclass 'type)
383 (semanticdb-typecache-find name)
384 ;; Search in the typecache. First entries in a sequence are
385 ;; often there.
386 (setq retlist (semanticdb-typecache-find name))
387 (if retlist
388 retlist
389 (semantic-analyze-select-best-tag
390 (semanticdb-strip-find-results
391 (semanticdb-find-tags-by-name name)
392 'name)
393 tagclass)
394 )))))
395 )))
396
397 ;;; SHORT ANALYSIS
398 ;;
399 ;; Create a mini-analysis of just the symbol under point.
400 ;;
401 (define-overloadable-function semantic-analyze-current-symbol
402 (analyzehookfcn &optional position)
403 "Call ANALYZEHOOKFCN after analyzing the symbol under POSITION.
404 The ANALYZEHOOKFCN is called with the current symbol bounds, and the
405 analyzed prefix. It should take the arguments (START END PREFIX).
406 The ANALYZEHOOKFCN is only called if some sort of prefix with bounds was
407 found under POSITION.
408
409 The results of ANALYZEHOOKFCN is returned, or nil if there was nothing to
410 call it with.
411
412 For regular analysis, you should call `semantic-analyze-current-context'
413 to calculate the context information. The purpose for this function is
414 to provide a large number of non-cached analysis for filtering symbols."
415 ;; Only do this in a Semantic enabled buffer.
416 (when (not (semantic-active-p))
417 (error "Cannot analyze buffers not supported by Semantic"))
418 ;; Always refresh out tags in a safe way before doing the
419 ;; context.
420 (semantic-refresh-tags-safe)
421 ;; Do the rest of the analysis.
422 (save-match-data
423 (save-excursion
424 (:override)))
425 )
426
427 (defun semantic-analyze-current-symbol-default (analyzehookfcn position)
428 "Call ANALYZEHOOKFCN on the analyzed symbol at POSITION."
429 (let* ((semantic-analyze-error-stack nil)
430 (LLstart (current-time))
431 (prefixandbounds (semantic-ctxt-current-symbol-and-bounds (or position (point))))
432 (prefix (car prefixandbounds))
433 (bounds (nth 2 prefixandbounds))
434 (scope (semantic-calculate-scope position))
435 (end nil)
436 )
437 ;; Only do work if we have bounds (meaning a prefix to complete)
438 (when bounds
439
440 (if debug-on-error
441 (catch 'unfindable
442 ;; If debug on error is on, allow debugging in this fcn.
443 (setq prefix (semantic-analyze-find-tag-sequence
444 prefix scope 'prefixtypes 'unfindable)))
445 ;; Debug on error is off. Capture errors and move on
446 (condition-case err
447 ;; NOTE: This line is duplicated in
448 ;; semantic-analyzer-debug-global-symbol
449 ;; You will need to update both places.
450 (setq prefix (semantic-analyze-find-tag-sequence
451 prefix scope 'prefixtypes))
452 (error (semantic-analyze-push-error err))))
453
454 (setq end (current-time))
455 ;;(message "Analysis took %.2f sec" (semantic-elapsed-time LLstart end))
456
457 )
458 (when prefix
459 (prog1
460 (funcall analyzehookfcn (car bounds) (cdr bounds) prefix)
461 ;;(setq end (current-time))
462 ;;(message "hookfcn took %.5f sec" (semantic-elapsed-time LLstart end))
463 )
464
465 )))
466
467 ;;; MAIN ANALYSIS
468 ;;
469 ;; Create a full-up context analysis.
470 ;;
471 ;;;###autoload
472 (define-overloadable-function semantic-analyze-current-context (&optional position)
473 "Analyze the current context at optional POSITION.
474 If called interactively, display interesting information about POSITION
475 in a separate buffer.
476 Returns an object based on symbol `semantic-analyze-context'.
477
478 This function can be overriden with the symbol `analyze-context'.
479 When overriding this function, your override will be called while
480 cursor is at POSITION. In addition, your function will not be called
481 if a cached copy of the return object is found."
482 (interactive "d")
483 ;; Only do this in a Semantic enabled buffer.
484 (when (not (semantic-active-p))
485 (error "Cannot analyze buffers not supported by Semantic"))
486 ;; Always refresh out tags in a safe way before doing the
487 ;; context.
488 (semantic-refresh-tags-safe)
489 ;; Do the rest of the analysis.
490 (if (not position) (setq position (point)))
491 (save-excursion
492 (goto-char position)
493 (let* ((answer (semantic-get-cache-data 'current-context)))
494 (with-syntax-table semantic-lex-syntax-table
495 (when (not answer)
496 (setq answer (:override))
497 (when (and answer (oref answer bounds))
498 (with-slots (bounds) answer
499 (semantic-cache-data-to-buffer (current-buffer)
500 (car bounds)
501 (cdr bounds)
502 answer
503 'current-context
504 'exit-cache-zone)))
505 ;; Check for interactivity
506 (when (interactive-p)
507 (if answer
508 (semantic-analyze-pop-to-context answer)
509 (message "No Context."))
510 ))
511
512 answer))))
513
514 (defun semantic-analyze-current-context-default (position)
515 "Analyze the current context at POSITION.
516 Returns an object based on symbol `semantic-analyze-context'."
517 (let* ((semantic-analyze-error-stack nil)
518 (context-return nil)
519 (prefixandbounds (semantic-ctxt-current-symbol-and-bounds (or position (point))))
520 (prefix (car prefixandbounds))
521 (bounds (nth 2 prefixandbounds))
522 ;; @todo - vv too early to really know this answer! vv
523 (prefixclass (semantic-ctxt-current-class-list))
524 (prefixtypes nil)
525 (scope (semantic-calculate-scope position))
526 (function nil)
527 (fntag nil)
528 arg fntagend argtag
529 assign asstag
530 )
531
532 ;; Pattern for Analysis:
533 ;;
534 ;; Step 1: Calculate DataTypes in Scope:
535 ;;
536 ;; a) Calculate the scope (above)
537 ;;
538 ;; Step 2: Parse context
539 ;;
540 ;; a) Identify function being called, or variable assignment,
541 ;; and find source tags for those references
542 ;; b) Identify the prefix (text cursor is on) and find the source
543 ;; tags for those references.
544 ;;
545 ;; Step 3: Assemble an object
546 ;;
547
548 ;; Step 2 a:
549
550 (setq function (semantic-ctxt-current-function))
551
552 (when function
553 ;; Calculate the argument for the function if there is one.
554 (setq arg (semantic-ctxt-current-argument))
555
556 ;; Find a tag related to the function name.
557 (condition-case err
558 (setq fntag
559 (semantic-analyze-find-tag-sequence function scope))
560 (error (semantic-analyze-push-error err)))
561
562 ;; fntag can have the last entry as just a string, meaning we
563 ;; could not find the core datatype. In this case, the searches
564 ;; below will not work.
565 (when (stringp (car (last fntag)))
566 ;; Take a wild guess!
567 (setcar (last fntag) (semantic-tag (car (last fntag)) 'function))
568 )
569
570 (when fntag
571 (let ((fcn (semantic-find-tags-by-class 'function fntag)))
572 (when (not fcn)
573 (let ((ty (semantic-find-tags-by-class 'type fntag)))
574 (when ty
575 ;; We might have a constructor with the same name as
576 ;; the found datatype.
577 (setq fcn (semantic-find-tags-by-name
578 (semantic-tag-name (car ty))
579 (semantic-tag-type-members (car ty))))
580 (if fcn
581 (let ((lp fcn))
582 (while lp
583 (when (semantic-tag-get-attribute (car lp)
584 :constructor)
585 (setq fcn (cons (car lp) fcn)))
586 (setq lp (cdr lp))))
587 ;; Give up, go old school
588 (setq fcn fntag))
589 )))
590 (setq fntagend (car (reverse fcn))
591 argtag
592 (when (semantic-tag-p fntagend)
593 (nth (1- arg) (semantic-tag-function-arguments fntagend)))
594 fntag fcn))))
595
596 ;; Step 2 b:
597
598 ;; Only do work if we have bounds (meaning a prefix to complete)
599 (when bounds
600
601 (if debug-on-error
602 (catch 'unfindable
603 ;; If debug on error is on, allow debugging in this fcn.
604 (setq prefix (semantic-analyze-find-tag-sequence
605 prefix scope 'prefixtypes 'unfindable)))
606 ;; Debug on error is off. Capture errors and move on
607 (condition-case err
608 ;; NOTE: This line is duplicated in
609 ;; semantic-analyzer-debug-global-symbol
610 ;; You will need to update both places.
611 (setq prefix (semantic-analyze-find-tag-sequence
612 prefix scope 'prefixtypes))
613 (error (semantic-analyze-push-error err))))
614 )
615
616 ;; Step 3:
617
618 (cond
619 (fntag
620 ;; If we found a tag for our function, we can go into
621 ;; functional context analysis mode, meaning we have a type
622 ;; for the argument.
623 (setq context-return
624 (semantic-analyze-context-functionarg
625 "functionargument"
626 :buffer (current-buffer)
627 :function fntag
628 :index arg
629 :argument (list argtag)
630 :scope scope
631 :prefix prefix
632 :prefixclass prefixclass
633 :bounds bounds
634 :prefixtypes prefixtypes
635 :errors semantic-analyze-error-stack)))
636
637 ;; No function, try assignment
638 ((and (setq assign (semantic-ctxt-current-assignment))
639 ;; We have some sort of an assignment
640 (condition-case err
641 (setq asstag (semantic-analyze-find-tag-sequence
642 assign scope))
643 (error (semantic-analyze-push-error err)
644 nil)))
645
646 (setq context-return
647 (semantic-analyze-context-assignment
648 "assignment"
649 :buffer (current-buffer)
650 :assignee asstag
651 :scope scope
652 :bounds bounds
653 :prefix prefix
654 :prefixclass prefixclass
655 :prefixtypes prefixtypes
656 :errors semantic-analyze-error-stack)))
657
658 ;; TODO: Identify return value condition.
659 ;;((setq return .... what to do?)
660 ;; ...)
661
662 (bounds
663 ;; Nothing in particular
664 (setq context-return
665 (semantic-analyze-context
666 "context"
667 :buffer (current-buffer)
668 :scope scope
669 :bounds bounds
670 :prefix prefix
671 :prefixclass prefixclass
672 :prefixtypes prefixtypes
673 :errors semantic-analyze-error-stack)))
674
675 (t (setq context-return nil))
676 )
677
678 ;; Return our context.
679 context-return))
680
681 \f
682 (defun semantic-adebug-analyze (&optional ctxt)
683 "Perform `semantic-analyze-current-context'.
684 Display the results as a debug list.
685 Optional argument CTXT is the context to show."
686 (interactive)
687 (require 'data-debug)
688 (let ((start (current-time))
689 (ctxt (or ctxt (semantic-analyze-current-context)))
690 (end (current-time)))
691 (if (not ctxt)
692 (message "No Analyzer Results")
693 (message "Analysis took %.2f seconds."
694 (semantic-elapsed-time start end))
695 (semantic-analyze-pulse ctxt)
696 (if ctxt
697 (progn
698 (data-debug-new-buffer "*Analyzer ADEBUG*")
699 (data-debug-insert-object-slots ctxt "]"))
700 (message "No Context to analyze here.")))))
701
702 \f
703 ;;; DEBUG OUTPUT
704 ;;
705 ;; Friendly output of a context analysis.
706 ;;
707 (declare-function pulse-momentary-highlight-region "pulse")
708
709 (defmethod semantic-analyze-pulse ((context semantic-analyze-context))
710 "Pulse the region that CONTEXT affects."
711 (require 'pulse)
712 (save-excursion
713 (set-buffer (oref context :buffer))
714 (let ((bounds (oref context :bounds)))
715 (when bounds
716 (pulse-momentary-highlight-region (car bounds) (cdr bounds))))))
717
718 (defcustom semantic-analyze-summary-function 'semantic-format-tag-prototype
719 "Function to use when creating items in Imenu.
720 Some useful functions are found in `semantic-format-tag-functions'."
721 :group 'semantic
722 :type semantic-format-tag-custom-list)
723
724 (defun semantic-analyze-princ-sequence (sequence &optional prefix buff)
725 "Send the tag SEQUENCE to standard out.
726 Use PREFIX as a label.
727 Use BUFF as a source of override methods."
728 (while sequence
729 (princ prefix)
730 (cond
731 ((semantic-tag-p (car sequence))
732 (princ (funcall semantic-analyze-summary-function
733 (car sequence))))
734 ((stringp (car sequence))
735 (princ "\"")
736 (princ (semantic--format-colorize-text (car sequence) 'variable))
737 (princ "\""))
738 (t
739 (princ (format "'%S" (car sequence)))))
740 (princ "\n")
741 (setq sequence (cdr sequence))
742 (setq prefix (make-string (length prefix) ? ))
743 ))
744
745 (defmethod semantic-analyze-show ((context semantic-analyze-context))
746 "Insert CONTEXT into the current buffer in a nice way."
747 (semantic-analyze-princ-sequence (oref context prefix) "Prefix: " )
748 (semantic-analyze-princ-sequence (oref context prefixclass) "Prefix Classes: ")
749 (semantic-analyze-princ-sequence (oref context prefixtypes) "Prefix Types: ")
750 (semantic-analyze-princ-sequence (oref context errors) "Encountered Errors: ")
751 (princ "--------\n")
752 ;(semantic-analyze-princ-sequence (oref context scopetypes) "Scope Types: ")
753 ;(semantic-analyze-princ-sequence (oref context scope) "Scope: ")
754 ;(semantic-analyze-princ-sequence (oref context localvariables) "LocalVars: ")
755 (when (oref context scope)
756 (semantic-analyze-show (oref context scope)))
757 )
758
759 (defmethod semantic-analyze-show ((context semantic-analyze-context-assignment))
760 "Insert CONTEXT into the current buffer in a nice way."
761 (semantic-analyze-princ-sequence (oref context assignee) "Assignee: ")
762 (call-next-method))
763
764 (defmethod semantic-analyze-show ((context semantic-analyze-context-functionarg))
765 "Insert CONTEXT into the current buffer in a nice way."
766 (semantic-analyze-princ-sequence (oref context function) "Function: ")
767 (princ "Argument Index: ")
768 (princ (oref context index))
769 (princ "\n")
770 (semantic-analyze-princ-sequence (oref context argument) "Argument: ")
771 (call-next-method))
772
773 (defun semantic-analyze-pop-to-context (context)
774 "Display CONTEXT in a temporary buffer.
775 CONTEXT's content is described in `semantic-analyze-current-context'."
776 (semantic-analyze-pulse context)
777 (with-output-to-temp-buffer "*Semantic Context Analysis*"
778 (princ "Context Type: ")
779 (princ (object-name context))
780 (princ "\n")
781 (princ "Bounds: ")
782 (princ (oref context bounds))
783 (princ "\n")
784 (semantic-analyze-show context)
785 )
786 (shrink-window-if-larger-than-buffer
787 (get-buffer-window "*Semantic Context Analysis*"))
788 )
789
790 (provide 'semantic/analyze)
791
792 ;; Local variables:
793 ;; generated-autoload-file: "loaddefs.el"
794 ;; generated-autoload-feature: semantic/loaddefs
795 ;; generated-autoload-load-name: "semantic/analyze"
796 ;; End:
797
798 ;; arch-tag: 1102143a-1c05-4631-83e8-45aafc6b4a59
799 ;;; semantic/analyze.el ends here