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