1 ;;; semantic/bovine/c.el --- Semantic details for C
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
8 ;; This file is part of GNU Emacs.
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.
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.
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/>.
25 ;; Support for the C/C++ bovine parser for Semantic.
27 ;; @todo - can I support c++-font-lock-extra-types ?
30 (require 'semantic/analyze)
31 (require 'semantic/bovine/gcc)
32 (require 'semantic/idle)
33 (require 'semantic/lex-spp)
34 (require 'semantic/bovine/c-by)
37 (require 'semantic/find))
39 (declare-function semantic-brute-find-tag-by-attribute "semantic/find")
40 (declare-function semanticdb-minor-mode-p "semantic/db-mode")
41 (declare-function semanticdb-needs-refresh-p "semantic/db")
42 (declare-function semanticdb-typecache-faux-namespace "semantic/db-typecache")
43 (declare-function c-forward-conditional "cc-cmds")
44 (declare-function ede-system-include-path "ede")
48 (eval-when-compile (require 'cc-mode))
50 (if (fboundp 'c-end-of-macro)
52 (defalias 'semantic-c-end-of-macro 'c-end-of-macro))
54 (defun semantic-c-end-of-macro ()
55 "Go to the end of a preprocessor directive.
56 More accurately, move point to the end of the closest following line
57 that doesn't end with a line continuation backslash.
59 This function does not do any hidden buffer changes."
62 (when (and (eq (char-before) ?\\)
69 (define-child-mode c++-mode c-mode
70 "`c++-mode' uses the same parser as `c-mode'.")
75 (defcustom-mode-local-semantic-dependency-system-include-path
76 c-mode semantic-c-dependency-system-include-path
78 "The system include path used by the C language.")
80 (defcustom semantic-default-c-path nil
81 "Default set of include paths for C code.
82 Used by `semantic-dep' to define an include path.
83 NOTE: In process of obsoleting this."
86 :type '(repeat (string :tag "Path")))
88 (defvar-mode-local c-mode semantic-dependency-include-path
89 semantic-default-c-path
90 "System path to search for include files.")
94 ;; Compiler options need to show up after path setup, but before
95 ;; the preprocessor section.
97 (if (memq system-type '(gnu gnu/linux darwin cygwin))
100 ;;; Pre-processor maps
103 (defvar semantic-lex-c-preprocessor-symbol-map-builtin
105 ("__const" . "const")
107 ("__declspec" . ((spp-arg-list ("foo") 1 . 2)))
108 ("__attribute__" . ((spp-arg-list ("foo") 1 . 2)))
110 "List of symbols to include by default.")
112 (defvar semantic-c-in-reset-preprocessor-table nil
113 "Non-nil while resetting the preprocessor symbol map.
114 Used to prevent a reset while trying to parse files that are
115 part of the preprocessor map.")
117 (defvar semantic-lex-c-preprocessor-symbol-file)
118 (defvar semantic-lex-c-preprocessor-symbol-map)
120 (defun semantic-c-reset-preprocessor-symbol-map ()
121 "Reset the C preprocessor symbol map based on all input variables."
122 (when (featurep 'semantic/bovine/c)
125 (when (and (not semantic-c-in-reset-preprocessor-table)
126 (featurep 'semantic/db-mode)
127 (semanticdb-minor-mode-p))
128 (let ( ;; Don't use external parsers. We need the internal one.
129 (semanticdb-out-of-buffer-create-table-fcn nil)
130 ;; Don't recurse while parsing these files the first time.
131 (semantic-c-in-reset-preprocessor-table t)
133 (dolist (sf semantic-lex-c-preprocessor-symbol-file)
134 ;; Global map entries
135 (let* ((table (semanticdb-file-table-object sf t)))
137 (when (semanticdb-needs-refresh-p table)
139 ;; Call with FORCE, as the file is very likely to
140 ;; not be in a buffer.
141 (semanticdb-refresh-table table t)
142 (error (message "Error updating tables for %S"
143 (object-name table)))))
144 (setq filemap (append filemap (oref table lexical-table)))
148 (setq-mode-local c-mode
149 semantic-lex-spp-macro-symbol-obarray
150 (semantic-lex-make-spp-table
151 (append semantic-lex-c-preprocessor-symbol-map-builtin
152 semantic-lex-c-preprocessor-symbol-map
157 (defcustom semantic-lex-c-preprocessor-symbol-map nil
158 "Table of C Preprocessor keywords used by the Semantic C lexer.
159 Each entry is a cons cell like this:
160 ( \"KEYWORD\" . \"REPLACEMENT\" )
161 Where KEYWORD is the macro that gets replaced in the lexical phase,
162 and REPLACEMENT is a string that is inserted in its place. Empty string
163 implies that the lexical analyzer will discard KEYWORD when it is encountered.
165 Alternately, it can be of the form:
166 ( \"KEYWORD\" ( LEXSYM1 \"str\" 1 1 ) ... ( LEXSYMN \"str\" 1 1 ) )
167 where LEXSYM is a symbol that would normally be produced by the
168 lexical analyzer, such as `symbol' or `string'. The string in the
169 second position is the text that makes up the replacement. This is
170 the way to have multiple lexical symbols in a replacement. Using the
171 first way to specify text like \"foo::bar\" would not work, because :
172 is a separate lexical symbol.
174 A quick way to see what you would need to insert is to place a
177 #define MYSYM foo::bar
179 into a C file, and do this:
180 \\[semantic-lex-spp-describe]
182 The output table will describe the symbols needed."
184 :type '(repeat (cons (string :tag "Keyword")
185 (sexp :tag "Replacement")))
186 :set (lambda (sym value)
187 (set-default sym value)
189 (semantic-c-reset-preprocessor-symbol-map)
194 (defcustom semantic-lex-c-preprocessor-symbol-file nil
195 "List of C/C++ files that contain preprocessor macros for the C lexer.
196 Each entry is a filename and each file is parsed, and those macros
197 are included in every C/C++ file parsed by semantic.
198 You can use this variable instead of `semantic-lex-c-preprocessor-symbol-map'
199 to store your global macros in a more natural way."
201 :type '(repeat (file :tag "File"))
202 :set (lambda (sym value)
203 (set-default sym value)
205 (semantic-c-reset-preprocessor-symbol-map)
210 (defcustom semantic-c-member-of-autocast 't
211 "Non-nil means classes with a '->' operator will cast to its return type.
221 if `semantic-c-member-of-autocast' is non-nil :
222 foo->[here completion will list method of Bar]
224 if `semantic-c-member-of-autocast' is nil :
225 foo->[here completion will list method of Foo]"
229 (define-lex-spp-macro-declaration-analyzer semantic-lex-cpp-define
230 "A #define of a symbol with some value.
231 Record the symbol in the semantic preprocessor.
232 Return the defined symbol as a special spp lex token."
233 "^\\s-*#\\s-*define\\s-+\\(\\(\\sw\\|\\s_\\)+\\)" 1
234 (goto-char (match-end 0))
235 (skip-chars-forward " \t")
238 (let* ((name (buffer-substring-no-properties
239 (match-beginning 1) (match-end 1)))
240 (with-args (save-excursion
241 (goto-char (match-end 0))
243 (semantic-lex-spp-replacements-enabled nil)
244 ;; Temporarilly override the lexer to include
245 ;; special items needed inside a macro
246 (semantic-lex-analyzer #'semantic-cpp-lexer)
248 (semantic-lex-spp-stream-for-macro (save-excursion
249 (semantic-c-end-of-macro)
253 ;; Only do argument checking if the paren was immediatly after
256 (semantic-lex-spp-first-token-arg-list (car raw-stream)))
258 ;; Magical spp variable for end point.
259 (setq semantic-lex-end-point (point))
261 ;; Handled nested macro streams.
262 (semantic-lex-spp-merge-streams raw-stream)
265 (define-lex-spp-macro-undeclaration-analyzer semantic-lex-cpp-undef
266 "A #undef of a symbol.
267 Remove the symbol from the semantic preprocessor.
268 Return the defined symbol as a special spp lex token."
269 "^\\s-*#\\s-*undef\\s-+\\(\\(\\sw\\|\\s_\\)+\\)" 1)
272 ;;; Conditional Skipping
274 (defcustom semantic-c-obey-conditional-section-parsing-flag t
275 "*Non-nil means to interpret preprocessor #if sections.
276 This implies that some blocks of code will not be parsed based on the
277 values of the conditions in the #if blocks."
281 (defun semantic-c-skip-conditional-section ()
282 "Skip one section of a conditional.
283 Moves forward to a matching #elif, #else, or #endif.
284 Moves completely over balanced #if blocks."
287 ;; (if (looking-at "^\\s-*#if")
288 ;; (semantic-lex-spp-push-if (point))
290 (while (and semantic-c-obey-conditional-section-parsing-flag
293 "^\\s-*#\\s-*\\(if\\(n?def\\)?\\|el\\(if\\|se\\)\\|endif\\)\\>"
295 (goto-char (match-beginning 0))
297 ((looking-at "^\\s-*#\\s-*if")
298 ;; We found a nested if. Skip it.
299 ;; @TODO - can we use the new c-scan-conditionals
300 (c-forward-conditional 1))
301 ((looking-at "^\\s-*#\\s-*elif")
302 ;; We need to let the preprocessor analize this one.
306 ((looking-at "^\\s-*#\\s-*\\(endif\\|else\\)\\>")
307 ;; We are at the end. Pop our state.
308 ;; (semantic-lex-spp-pop-if)
309 ;; Note: We include ELSE and ENDIF the same. If skip some previous
310 ;; section, then we should do the else by default, making it much
316 ;; We found an elif. Stop here.
319 (define-lex-regex-analyzer semantic-lex-c-if
320 "Code blocks wrapped up in #if, or #ifdef.
321 Uses known macro tables in SPP to determine what block to skip."
322 "^\\s-*#\\s-*\\(if\\|ifndef\\|ifdef\\|elif\\)\\s-+\\(!?defined(\\|\\)\\s-*\\(\\(\\sw\\|\\s_\\)+\\)\\(\\s-*)\\)?\\s-*$"
323 (semantic-c-do-lex-if))
325 (defun semantic-c-do-lex-if ()
326 "Handle lexical CPP if statements."
327 (let* ((sym (buffer-substring-no-properties
328 (match-beginning 3) (match-end 3)))
329 (defstr (buffer-substring-no-properties
330 (match-beginning 2) (match-end 2)))
331 (defined (string= defstr "defined("))
332 (notdefined (string= defstr "!defined("))
333 (ift (buffer-substring-no-properties
334 (match-beginning 1) (match-end 1)))
335 (ifdef (or (string= ift "ifdef")
336 (and (string= ift "if") defined)
337 (and (string= ift "elif") defined)
339 (ifndef (or (string= ift "ifndef")
340 (and (string= ift "if") notdefined)
341 (and (string= ift "elif") notdefined)
344 (if (or (and (or (string= ift "if") (string= ift "elif"))
346 (and ifdef (not (semantic-lex-spp-symbol-p sym)))
347 (and ifndef (semantic-lex-spp-symbol-p sym)))
348 ;; The if indecates to skip this preprocessor section
350 ;; (message "%s %s yes" ift sym)
353 ;; This skips only a section of a conditional. Once that section
354 ;; is opened, encountering any new #else or related conditional
355 ;; should be skipped.
356 (semantic-c-skip-conditional-section)
357 (setq semantic-lex-end-point (point))
358 (semantic-push-parser-warning (format "Skip #%s %s" ift sym)
360 ;; (semantic-lex-push-token
361 ;; (semantic-lex-token 'c-preprocessor-skip pt (point)))
363 ;; Else, don't ignore it, but do handle the internals.
364 ;;(message "%s %s no" ift sym)
366 (setq semantic-lex-end-point (point))
369 (define-lex-regex-analyzer semantic-lex-c-macro-else
370 "Ignore an #else block.
371 We won't see the #else due to the macro skip section block
372 unless we are actively parsing an open #if statement. In that
373 case, we must skip it since it is the ELSE part."
374 "^\\s-*#\\s-*\\(else\\)"
376 (semantic-c-skip-conditional-section)
377 (setq semantic-lex-end-point (point))
378 (semantic-push-parser-warning "Skip #else" pt (point))
379 ;; (semantic-lex-push-token
380 ;; (semantic-lex-token 'c-preprocessor-skip pt (point)))
383 (define-lex-regex-analyzer semantic-lex-c-macrobits
384 "Ignore various forms of #if/#else/#endif conditionals."
385 "^\\s-*#\\s-*\\(if\\(n?def\\)?\\|endif\\|elif\\|else\\)"
386 (semantic-c-end-of-macro)
387 (setq semantic-lex-end-point (point))
390 (define-lex-spp-include-analyzer semantic-lex-c-include-system
391 "Identify include strings, and return special tokens."
392 "^\\s-*#\\s-*include\\s-*<\\([^ \t\n>]+\\)>" 0
393 ;; Hit 1 is the name of the include.
394 (goto-char (match-end 0))
395 (setq semantic-lex-end-point (point))
396 (cons (buffer-substring-no-properties (match-beginning 1)
400 (define-lex-spp-include-analyzer semantic-lex-c-include
401 "Identify include strings, and return special tokens."
402 "^\\s-*#\\s-*include\\s-*\"\\([^ \t\n>]+\\)\"" 0
403 ;; Hit 1 is the name of the include.
404 (goto-char (match-end 0))
405 (setq semantic-lex-end-point (point))
406 (cons (buffer-substring-no-properties (match-beginning 1)
411 (define-lex-regex-analyzer semantic-lex-c-ignore-ending-backslash
412 "Skip backslash ending a line.
413 Go to the next line."
415 (setq semantic-lex-end-point (match-end 0)))
417 (define-lex-regex-analyzer semantic-lex-c-namespace-begin-macro
418 "Handle G++'s namespace macros which the pre-processor can't handle."
419 "\\(_GLIBCXX_BEGIN_NAMESPACE\\)(\\s-*\\(\\(?:\\w\\|\\s_\\)+\\)\\s-*)"
420 (let* ((nsend (match-end 1))
421 (sym-start (match-beginning 2))
422 (sym-end (match-end 2))
423 (ms (buffer-substring-no-properties sym-start sym-end)))
424 ;; Push the namespace keyword.
425 (semantic-lex-push-token
426 (semantic-lex-token 'NAMESPACE (match-beginning 0) nsend "namespace"))
428 (semantic-lex-push-token
429 (semantic-lex-token 'symbol sym-start sym-end ms))
431 (goto-char (match-end 0))
432 (let ((start (point))
434 ;; If we can't find a matching end, then create the fake list.
435 (when (re-search-forward "_GLIBCXX_END_NAMESPACE" nil t)
437 (semantic-lex-push-token
438 (semantic-lex-token 'semantic-list start end
439 (list 'prefix-fake)))))
440 (setq semantic-lex-end-point (point)))
442 (defcustom semantic-lex-c-nested-namespace-ignore-second t
443 "Should _GLIBCXX_BEGIN_NESTED_NAMESPACE ignore the second namespace?
444 It is really there, but if a majority of uses is to squeeze out
445 the second namespace in use, then it should not be included.
447 If you are having problems with smart completion and STL templates,
448 it may be that this is set incorrectly. After changing the value
449 of this flag, you will need to delete any semanticdb cache files
450 that may have been incorrectly parsed."
454 (define-lex-regex-analyzer semantic-lex-c-VC++-begin-std-namespace
455 "Handle VC++'s definition of the std namespace."
457 (semantic-lex-push-token
458 (semantic-lex-token 'NAMESPACE (match-beginning 0) (match-end 0) "namespace"))
459 (semantic-lex-push-token
460 (semantic-lex-token 'symbol (match-beginning 0) (match-end 0) "std"))
461 (goto-char (match-end 0))
462 (let ((start (point))
464 (when (re-search-forward "_STD_END" nil t)
466 (semantic-lex-push-token
467 (semantic-lex-token 'semantic-list start end
468 (list 'prefix-fake)))))
469 (setq semantic-lex-end-point (point)))
471 (define-lex-regex-analyzer semantic-lex-c-VC++-end-std-namespace
472 "Handle VC++'s definition of the std namespace."
474 (goto-char (match-end 0))
475 (setq semantic-lex-end-point (point)))
477 (define-lex-regex-analyzer semantic-lex-c-namespace-begin-nested-macro
478 "Handle G++'s namespace macros which the pre-processor can't handle."
479 "\\(_GLIBCXX_BEGIN_NESTED_NAMESPACE\\)(\\s-*\\(\\(?:\\w\\|\\s_\\)+\\)\\s-*,\\s-*\\(\\(?:\\w\\|\\s_\\)+\\)\\s-*)"
480 (goto-char (match-end 0))
481 (let* ((nsend (match-end 1))
482 (sym-start (match-beginning 2))
483 (sym-end (match-end 2))
484 (ms (buffer-substring-no-properties sym-start sym-end))
485 (sym2-start (match-beginning 3))
486 (sym2-end (match-end 3))
487 (ms2 (buffer-substring-no-properties sym2-start sym2-end)))
488 ;; Push the namespace keyword.
489 (semantic-lex-push-token
490 (semantic-lex-token 'NAMESPACE (match-beginning 0) nsend "namespace"))
492 (semantic-lex-push-token
493 (semantic-lex-token 'symbol sym-start sym-end ms))
495 (goto-char (match-end 0))
496 (let ((start (point))
498 ;; If we can't find a matching end, then create the fake list.
499 (when (re-search-forward "_GLIBCXX_END_NESTED_NAMESPACE" nil t)
501 (if semantic-lex-c-nested-namespace-ignore-second
502 ;; The same as _GLIBCXX_BEGIN_NAMESPACE
503 (semantic-lex-push-token
504 (semantic-lex-token 'semantic-list start end
505 (list 'prefix-fake)))
506 ;; Do both the top and second level namespace
507 (semantic-lex-push-token
508 (semantic-lex-token 'semantic-list start end
509 ;; We'll depend on a quick hack
510 (list 'prefix-fake-plus
511 (semantic-lex-token 'NAMESPACE
514 (semantic-lex-token 'symbol
517 (semantic-lex-token 'semantic-list start end
518 (list 'prefix-fake)))
521 (setq semantic-lex-end-point (point)))
523 (define-lex-regex-analyzer semantic-lex-c-namespace-end-macro
524 "Handle G++'s namespace macros which the pre-processor can't handle."
525 "_GLIBCXX_END_\\(NESTED_\\)?NAMESPACE"
526 (goto-char (match-end 0))
527 (setq semantic-lex-end-point (point)))
529 (define-lex-regex-analyzer semantic-lex-c-string
530 "Detect and create a C string token."
532 ;; Zing to the end of this string.
533 (semantic-lex-push-token
537 ;; Skip L prefix if present.
538 (goto-char (match-beginning 1))
539 (semantic-lex-unterminated-syntax-protection 'string
544 (define-lex-regex-analyzer semantic-c-lex-ignore-newline
545 "Detect and ignore newline tokens.
546 Use this ONLY if newlines are not whitespace characters (such as when
547 they are comment end characters)."
548 ;; Just like semantic-lex-ignore-newline, but also ignores
550 "\\s-*\\\\?\\s-*\\(\n\\|\\s>\\)"
551 (setq semantic-lex-end-point (match-end 0)))
554 (define-lex semantic-c-lexer
555 "Lexical Analyzer for C code.
556 Use semantic-cpp-lexer for parsing text inside a CPP macro."
557 ;; C preprocessor features
558 semantic-lex-cpp-define
559 semantic-lex-cpp-undef
561 semantic-lex-c-macro-else
562 semantic-lex-c-macrobits
563 semantic-lex-c-include
564 semantic-lex-c-include-system
565 semantic-lex-c-ignore-ending-backslash
566 ;; Whitespace handling
567 semantic-lex-ignore-whitespace
568 semantic-c-lex-ignore-newline
569 ;; Non-preprocessor features
571 ;; Must detect C strings before symbols because of possible L prefix!
572 semantic-lex-c-string
573 ;; Custom handlers for some macros come before the macro replacement analyzer.
574 semantic-lex-c-namespace-begin-macro
575 semantic-lex-c-namespace-begin-nested-macro
576 semantic-lex-c-namespace-end-macro
577 semantic-lex-c-VC++-begin-std-namespace
578 semantic-lex-c-VC++-end-std-namespace
579 ;; Handle macros, symbols, and keywords
580 semantic-lex-spp-replace-or-symbol-or-keyword
581 semantic-lex-charquote
582 semantic-lex-paren-or-list
583 semantic-lex-close-paren
584 semantic-lex-ignore-comments
585 semantic-lex-punctuation
586 semantic-lex-default-action)
588 (define-lex-simple-regex-analyzer semantic-lex-cpp-hashhash
589 "Match ## inside a CPP macro as special."
592 (define-lex semantic-cpp-lexer
593 "Lexical Analyzer for CPP macros in C code."
595 semantic-lex-cpp-hashhash
596 ;; C preprocessor features
597 semantic-lex-cpp-define
598 semantic-lex-cpp-undef
600 semantic-lex-c-macro-else
601 semantic-lex-c-macrobits
602 semantic-lex-c-include
603 semantic-lex-c-include-system
604 semantic-lex-c-ignore-ending-backslash
605 ;; Whitespace handling
606 semantic-lex-ignore-whitespace
607 semantic-c-lex-ignore-newline
608 ;; Non-preprocessor features
610 ;; Must detect C strings before symbols because of possible L prefix!
611 semantic-lex-c-string
612 ;; Parsing inside a macro means that we don't do macro replacement.
613 ;; semantic-lex-spp-replace-or-symbol-or-keyword
614 semantic-lex-symbol-or-keyword
615 semantic-lex-charquote
616 semantic-lex-paren-or-list
617 semantic-lex-close-paren
618 semantic-lex-ignore-comments
619 semantic-lex-punctuation
620 semantic-lex-default-action)
622 (define-mode-local-override semantic-parse-region c-mode
623 (start end &optional nonterminal depth returnonerror)
624 "Calls `semantic-parse-region-default', except in a macro expansion.
625 MACRO expansion mode is handled through the nature of Emacs's non-lexical
626 binding of variables.
627 START, END, NONTERMINAL, DEPTH, and RETURNONERRORS are the same
629 (if (and (boundp 'lse) (or (/= start 1) (/= end (point-max))))
630 (let* ((last-lexical-token lse)
631 (llt-class (semantic-lex-token-class last-lexical-token))
632 (llt-fakebits (car (cdr last-lexical-token)))
633 (macroexpand (stringp (car (cdr last-lexical-token)))))
636 ;; It is a macro expansion. Do something special.
637 ;;(message "MOOSE %S %S, %S : %S" start end nonterminal lse)
638 (semantic-c-parse-lexical-token
639 lse nonterminal depth returnonerror)
641 ;; Not a macro expansion, but perhaps a funny semantic-list
642 ;; is at the start? Remove the depth if our semantic list is not
643 ;; made of list tokens.
644 (if (and depth (= depth 1)
645 (eq llt-class 'semantic-list)
646 (not (null llt-fakebits))
648 (symbolp (car llt-fakebits))
653 ;; This is a copy of semantic-parse-region-default where we
654 ;; are doing something special with the lexication of the
655 ;; contents of the semantic-list token. Stuff not used by C
658 (if (and (consp llt-fakebits)
659 (eq (car llt-fakebits) 'prefix-fake-plus))
660 ;; If our semantic-list is special, then only stick in the
663 ;; Lex up the region with a depth of 0
664 (semantic-lex start end 0))))
668 (semantic-repeat-parse-whole-stream tokstream
674 ;; It was not a macro expansion, nor a special semantic-list.
676 (semantic-parse-region-default start end
681 (semantic-parse-region-default start end nonterminal
685 (defvar semantic-c-parse-token-hack-depth 0
686 "Current depth of recursive calls to `semantic-c-parse-lexical-token'.")
688 (defun semantic-c-parse-lexical-token (lexicaltoken nonterminal depth
690 "Do a region parse on the contents of LEXICALTOKEN.
691 Presumably, this token has a string in it from a macro.
692 The text of the token is inserted into a different buffer, and
694 Argument NONTERMINAL, DEPTH, and RETURNONERROR are passed into
696 (let* ((semantic-c-parse-token-hack-depth (1+ semantic-c-parse-token-hack-depth))
697 (buf (get-buffer-create (format " *C parse hack %d*"
698 semantic-c-parse-token-hack-depth)))
700 (spp-syms semantic-lex-spp-dynamic-macro-symbol-obarray)
702 (start (semantic-lex-token-start lexicaltoken))
703 (end (semantic-lex-token-end lexicaltoken))
704 (symtext (semantic-lex-token-text lexicaltoken))
705 (macros (get-text-property 0 'macros symtext))
707 (if (> semantic-c-parse-token-hack-depth 5)
709 (with-current-buffer buf
711 (when (not (eq major-mode mode))
714 ;; Protect against user hooks throwing errors.
719 (format "There was an error initializing %s in buffer \"%s\". Debug your hooks? "
721 (semantic-c-debug-mode-init mode)
722 (message "Macro parsing state may be broken...")
726 ;; Hack in mode-local
727 (activate-mode-local-bindings)
728 ;; CHEATER! The following 3 lines are from
729 ;; `semantic-new-buffer-fcn', but we don't want to turn
730 ;; on all the other annoying modes for this little task.
731 (setq semantic-new-buffer-fcn-was-run t)
733 (semantic-clear-toplevel-cache)
734 (remove-hook 'semantic-lex-reset-hooks 'semantic-lex-spp-reset-hook
737 ;; Get the macro symbol table right.
738 (setq semantic-lex-spp-dynamic-macro-symbol-obarray spp-syms)
739 ;; (message "%S" macros)
741 (semantic-lex-spp-symbol-set (car sym) (cdr sym)))
746 (semantic-parse-region-default
747 (point-min) (point-max) nonterminal depth returnonerror))
749 ;; Clean up macro symbols
751 (semantic-lex-spp-symbol-remove (car sym)))
753 ;; Convert the text of the stream.
755 ;; Only do two levels here 'cause I'm lazy.
756 (semantic--tag-set-overlay tag (list start end))
757 (dolist (stag (semantic-tag-components-with-overlays tag))
758 (semantic--tag-set-overlay stag (list start end))
763 (defvar semantic-c-debug-mode-init-last-mode nil
764 "The most recent mode needing debugging.")
766 (defun semantic-c-debug-mode-init (mm)
767 "Debug mode init for major mode MM after we're done parsing now."
768 (interactive (list semantic-c-debug-mode-init-last-mode))
769 (if (called-interactively-p 'interactive)
772 (switch-to-buffer (get-buffer-create "*MODE HACK TEST*"))
773 (let ((debug-on-error t))
776 ;; Notify about the debug
777 (setq semantic-c-debug-mode-init-last-mode mm)
779 (add-hook 'post-command-hook 'semantic-c-debug-mode-init-pch)))
781 (defun semantic-c-debug-mode-init-pch ()
782 "Notify user about needing to debug their major mode hooks."
783 (let ((mm semantic-c-debug-mode-init-last-mode))
784 (switch-to-buffer-other-window
785 (get-buffer-create "*MODE HACK TEST*"))
787 (insert "A failure occurred while parsing your buffers.
789 The failure occurred while attempting to initialize " (symbol-name mm) " in a
790 buffer not associated with a file. To debug this problem, type
792 M-x semantic-c-debug-mode-init
796 (remove-hook 'post-command-hook 'semantic-c-debug-mode-init-pch)))
798 (defun semantic-expand-c-tag (tag)
799 "Expand TAG into a list of equivalent tags, or nil."
800 (let ((return-list nil)
802 ;; Expand an EXTERN C first.
803 (when (eq (semantic-tag-class tag) 'extern)
804 (let* ((mb (semantic-tag-get-attribute tag :members))
807 (let ((mods (semantic-tag-get-attribute (car mb) :typemodifiers)))
808 (setq mods (cons "extern" (cons "\"C\"" mods)))
809 (semantic-tag-put-attribute (car mb) :typemodifiers mods))
811 (setq return-list ret)))
813 ;; Function or variables that have a :type that is some complex
814 ;; thing, extract it, and replace it with a reference.
816 ;; Thus, struct A { int a; } B;
818 ;; will create 2 toplevel tags, one is type A, and the other variable B
819 ;; where the :type of B is just a type tag A that is a prototype, and
820 ;; the actual struct info of A is it's own toplevel tag.
821 (when (or (semantic-tag-of-class-p tag 'function)
822 (semantic-tag-of-class-p tag 'variable))
823 (let* ((basetype (semantic-tag-type tag))
825 (tname (when (consp basetype)
826 (semantic-tag-name basetype))))
827 ;; Make tname be a string.
828 (when (consp tname) (setq tname (car (car tname))))
829 ;; Is the basetype a full type with a name of its own?
830 (when (and basetype (semantic-tag-p basetype)
831 (not (semantic-tag-prototype-p basetype))
833 (not (string= tname "")))
834 ;; a type tag referencing the type we are extracting.
835 (setq typeref (semantic-tag-new-type
836 (semantic-tag-name basetype)
837 (semantic-tag-type basetype)
840 ;; Convert original tag to only have a reference.
841 (setq tag (semantic-tag-copy tag))
842 (semantic-tag-put-attribute tag :type typeref)
843 ;; Convert basetype to have the location information.
844 (semantic--tag-copy-properties tag basetype)
845 (semantic--tag-set-overlay basetype
846 (semantic-tag-overlay tag))
847 ;; Store the base tag as part of the return list.
848 (setq return-list (cons basetype return-list)))))
850 ;; Name of the tag is a list, so expand it. Tag lists occur
851 ;; for variables like this: int var1, var2, var3;
853 ;; This will expand that to 3 tags that happen to share the
854 ;; same overlay information.
855 (if (consp (semantic-tag-name tag))
856 (let ((rl (semantic-expand-c-tag-namelist tag)))
858 ;; If this returns nothing, then return nil overall
859 ;; because that will restore the old TAG input.
860 ((not rl) (setq return-list nil))
861 ;; If we have a return, append it to the existing list
864 (setq return-list (append rl return-list)))
866 ;; If we didn't have a list, but the return-list is non-empty,
867 ;; that means we still need to take our existing tag, and glom
868 ;; it onto our extracted type.
869 (if (consp return-list)
870 (setq return-list (cons tag return-list)))
873 ;; Default, don't change the tag means returning nil.
876 (defun semantic-expand-c-tag-namelist (tag)
877 "Expand TAG whose name is a list into a list of tags, or nil."
878 (cond ((semantic-tag-of-class-p tag 'variable)
879 ;; The name part comes back in the form of:
880 ;; ( NAME NUMSTARS BITS ARRAY ASSIGN )
882 (basety (semantic-tag-type tag))
884 (mods (semantic-tag-get-attribute tag :typemodifiers))
886 (lst (semantic-tag-name tag))
889 ;; Open up each name in the name list.
891 (setq suffix "" ty "")
894 (setq suffix (concat ":" (nth 2 cur))))
895 (if (= (length basety) 1)
896 (setq ty (car basety))
898 (setq default (nth 4 cur))
900 (semantic-tag-new-variable
904 (buffer-substring-no-properties
905 (car default) (car (cdr default))))
906 :constant-flag (semantic-tag-variable-constant-p tag)
909 :dereference (length (nth 3 cur))
911 :reference (semantic-tag-get-attribute tag :reference)
912 :documentation (semantic-tag-docstring tag) ;doc
915 (semantic--tag-copy-properties tag (car vl))
916 (semantic--tag-set-overlay (car vl)
917 (semantic-tag-overlay tag))
918 (setq lst (cdr lst)))
921 ((semantic-tag-of-class-p tag 'type)
922 ;; We may someday want to add an extra check for a type
923 ;; of type "typedef".
924 ;; Each elt of NAME is ( STARS NAME )
926 (names (semantic-tag-name tag))
927 (super (semantic-tag-get-attribute tag :superclasses))
930 (when (and (semantic-tag-of-type-p tag "typedef")
931 (semantic-tag-of-class-p super 'type)
932 (semantic-tag-type-members super))
933 ;; This is a typedef of a real type. Extract
934 ;; the super class, and stick it into the tags list.
937 ;; Clone super and remove the members IFF super has a name.
938 ;; Note: anonymous struct/enums that are typedef'd shouldn't
939 ;; exist in the top level type list, so they will appear only
940 ;; in the :typedef slot of the typedef.
941 (setq super (semantic-tag-clone super))
942 (if (not (string= (semantic-tag-name super) ""))
943 (semantic-tag-put-attribute super :members nil)
946 ;; Add in props to the full superclass.
948 (semantic--tag-copy-properties tag addlast)
949 (semantic--tag-set-overlay addlast (semantic-tag-overlay tag)))
954 (setq vl (cons (semantic-tag-new-type
955 (nth 1 (car names)) ; name
957 (semantic-tag-type-members tag)
958 ;; parent is just tbe name of what
959 ;; is passed down as a tag.
962 (semantic-tag-type-superclasses tag)))
964 (let ((stars (car (car (car names)))))
965 (if (= stars 0) nil stars))
966 ;; This specifies what the typedef
967 ;; is expanded out as. Just the
968 ;; name shows up as a parent of this
971 ;;(semantic-tag-type-superclasses tag)
973 (semantic-tag-docstring tag))
975 (semantic--tag-copy-properties tag (car vl))
976 (semantic--tag-set-overlay (car vl) (semantic-tag-overlay tag))
977 (setq names (cdr names)))
979 ;; Add typedef superclass last.
980 (when addlast (setq vl (cons addlast vl)))
983 ((and (listp (car tag))
984 (semantic-tag-of-class-p (car tag) 'variable))
985 ;; Argument lists come in this way. Append all the expansions!
988 (setq vl (append (semantic-tag-components (car vl))
994 (defvar-mode-local c-mode semantic-tag-expand-function 'semantic-expand-c-tag
995 "Function used to expand tags generated in the C bovine parser.")
997 (defvar semantic-c-classname nil
998 "At parse time, assign a class or struct name text here.
999 It is picked up by `semantic-c-reconstitute-token' to determine
1000 if something is a constructor. Value should be:
1001 (TYPENAME . TYPEOFTYPE)
1002 where typename is the name of the type, and typeoftype is \"class\"
1005 (define-mode-local-override semantic-analyze-split-name c-mode (name)
1006 "Split up tag names on colon (:) boundaries."
1007 (let ((ans (split-string name ":")))
1008 (if (= (length ans) 1)
1012 (defun semantic-c-reconstitute-token (tokenpart declmods typedecl)
1013 "Reconstitute a token TOKENPART with DECLMODS and TYPEDECL.
1014 This is so we don't have to match the same starting text several times.
1015 Optional argument STAR and REF indicate the number of * and & in the typedef."
1016 (when (and (listp typedecl)
1017 (= 1 (length typedecl))
1018 (stringp (car typedecl)))
1019 (setq typedecl (car typedecl)))
1020 (cond ((eq (nth 1 tokenpart) 'variable)
1021 (semantic-tag-new-variable
1023 (or typedecl "int") ;type
1024 nil ;default value (filled with expand)
1025 :constant-flag (if (member "const" declmods) t nil)
1026 :typemodifiers (delete "const" declmods)
1029 ((eq (nth 1 tokenpart) 'function)
1030 ;; We should look at part 4 (the arglist) here, and throw an
1031 ;; error of some sort if it contains parser errors so that we
1032 ;; don't parser function calls, but that is a little beyond what
1033 ;; is available for data here.
1035 (and (or (and semantic-c-classname
1036 (string= (car semantic-c-classname)
1038 (and (stringp (car (nth 2 tokenpart)))
1039 (string= (car (nth 2 tokenpart)) (car tokenpart)))
1040 (nth 10 tokenpart) ; initializers
1042 (not (car (nth 3 tokenpart)))))
1043 (fcnpointer (string-match "^\\*" (car tokenpart)))
1044 (fnname (if fcnpointer
1045 (substring (car tokenpart) 1)
1047 (operator (if (string-match "[a-zA-Z]" fnname)
1052 ;; Function pointers are really variables.
1053 (semantic-tag-new-variable
1057 ;; It is a function pointer
1058 :functionpointer-flag t
1061 (semantic-tag-new-function
1064 (cond ((car (nth 3 tokenpart) )
1065 "void") ; Destructors have no return?
1067 ;; Constructors return an object.
1068 (semantic-tag-new-type
1070 (or (car semantic-c-classname)
1071 (let ((split (semantic-analyze-split-name-c-mode
1072 (car (nth 2 tokenpart)))))
1073 (if (stringp split) split
1074 (car (last split)))))
1076 (or (cdr semantic-c-classname)
1084 (nth 4 tokenpart) ;arglist
1085 :constant-flag (if (member "const" declmods) t nil)
1086 :typemodifiers (delete "const" declmods)
1087 :parent (car (nth 2 tokenpart))
1088 :destructor-flag (if (car (nth 3 tokenpart) ) t)
1089 :constructor-flag (if constructor t)
1090 :pointer (nth 7 tokenpart)
1091 :operator-flag operator
1092 ;; Even though it is "throw" in C++, we use
1093 ;; `throws' as a common name for things that toss
1094 ;; exceptions about.
1095 :throws (nth 5 tokenpart)
1096 ;; Reemtrant is a C++ thingy. Add it here
1097 :reentrant-flag (if (member "reentrant" (nth 6 tokenpart)) t)
1098 ;; A function post-const is funky. Try stuff
1099 :methodconst-flag (if (member "const" (nth 6 tokenpart)) t)
1100 ;; prototypes are functions w/ no body
1101 :prototype-flag (if (nth 8 tokenpart) t)
1103 :pure-virtual-flag (if (eq (nth 8 tokenpart) :pure-virtual-flag) t)
1104 ;; Template specifier.
1105 :template-specifier (nth 9 tokenpart)
1110 (defun semantic-c-reconstitute-template (tag specifier)
1111 "Reconstitute the token TAG with the template SPECIFIER."
1112 (semantic-tag-put-attribute tag :template (or specifier ""))
1116 ;;; Override methods & Variables
1118 (define-mode-local-override semantic-format-tag-name
1119 c-mode (tag &optional parent color)
1120 "Convert TAG to a string that is the print name for TAG.
1121 Optional PARENT and COLOR are ignored."
1122 (let ((name (semantic-format-tag-name-default tag parent color))
1123 (fnptr (semantic-tag-get-attribute tag :functionpointer-flag))
1127 (concat "(*" name ")"))
1130 (define-mode-local-override semantic-format-tag-canonical-name
1131 c-mode (tag &optional parent color)
1132 "Create a cannonical name for TAG.
1133 PARENT specifies a parent class.
1134 COLOR indicates that the text should be type colorized.
1135 Enhances the base class to search for the entire parent
1136 tree to make the name accurate."
1137 (semantic-format-tag-canonical-name-default tag parent color)
1140 (define-mode-local-override semantic-format-tag-type c-mode (tag color)
1141 "Convert the data type of TAG to a string usable in tag formatting.
1142 Adds pointer and reference symbols to the default.
1143 Argument COLOR adds color to the text."
1144 (let* ((type (semantic-tag-type tag))
1146 (point (semantic-tag-get-attribute tag :pointer))
1147 (ref (semantic-tag-get-attribute tag :reference))
1149 (if (semantic-tag-p type)
1150 (let ((typetype (semantic-tag-type type))
1151 (typename (semantic-tag-name type)))
1152 ;; Create the string that expresses the type
1153 (if (string= typetype "class")
1154 (setq defaulttype typename)
1155 (setq defaulttype (concat typetype " " typename))))
1156 (setq defaulttype (semantic-format-tag-type-default tag color)))
1160 (setq defaulttype (semantic--format-colorize-text defaulttype 'type)))
1162 ;; Add refs, ptrs, etc
1163 (if ref (setq ref "&"))
1164 (if point (setq point (make-string point ?*)) "")
1166 (concat defaulttype ref point))
1169 (define-mode-local-override semantic-find-tags-by-scope-protection
1170 c-mode (scopeprotection parent &optional table)
1171 "Override the usual search for protection.
1172 We can be more effective than the default by scanning through once,
1173 and collecting tags based on the labels we see along the way."
1174 (if (not table) (setq table (semantic-tag-type-members parent)))
1175 (if (null scopeprotection)
1179 (targetprot (cond ((eq scopeprotection 'public)
1181 ((eq scopeprotection 'protected)
1185 (alist '(("public" . 1)
1191 ((semantic-tag-of-class-p tag 'label)
1192 (setq curprot (cdr (assoc (semantic-tag-name tag) alist)))
1194 ((>= targetprot curprot)
1195 (setq ans (cons tag ans)))
1199 (define-mode-local-override semantic-tag-protection
1200 c-mode (tag &optional parent)
1201 "Return the protection of TAG in PARENT.
1202 Override function for `semantic-tag-protection'."
1203 (let ((mods (semantic-tag-modifiers tag))
1205 ;; Check the modifiers for protection if we are not a child
1206 ;; of some class type.
1207 (when (or (not parent) (not (eq (semantic-tag-class parent) 'type)))
1208 (while (and (not prot) mods)
1209 (if (stringp (car mods))
1210 (let ((s (car mods)))
1211 ;; A few silly defaults to get things started.
1212 (cond ((or (string= s "extern")
1213 (string= s "export"))
1215 ((string= s "static")
1217 (setq mods (cdr mods))))
1218 ;; If we have a typed parent, look for :public style labels.
1219 (when (and parent (eq (semantic-tag-class parent) 'type))
1220 (let ((pp (semantic-tag-type-members parent)))
1221 (while (and pp (not (semantic-equivalent-tag-p (car pp) tag)))
1222 (when (eq (semantic-tag-class (car pp)) 'label)
1224 (cond ((string= (semantic-tag-name (car pp)) "public")
1226 ((string= (semantic-tag-name (car pp)) "private")
1228 ((string= (semantic-tag-name (car pp)) "protected")
1231 (setq pp (cdr pp)))))
1232 (when (and (not prot) (eq (semantic-tag-class parent) 'type))
1234 (cond ((string= (semantic-tag-type parent) "class") 'private)
1235 ((string= (semantic-tag-type parent) "struct") 'public)
1238 (if (and parent (semantic-tag-of-class-p parent 'type))
1242 (define-mode-local-override semantic-tag-components c-mode (tag)
1243 "Return components for TAG."
1244 (if (and (eq (semantic-tag-class tag) 'type)
1245 (string= (semantic-tag-type tag) "typedef"))
1246 ;; A typedef can contain a parent who has positional children,
1247 ;; but that parent will not have a position. Do this funny hack
1248 ;; to make sure we can apply overlays properly.
1249 (let ((sc (semantic-tag-get-attribute tag :typedef)))
1250 (when (semantic-tag-p sc) (semantic-tag-components sc)))
1251 (semantic-tag-components-default tag)))
1253 (defun semantic-c-tag-template (tag)
1254 "Return the template specification for TAG, or nil."
1255 (semantic-tag-get-attribute tag :template))
1257 (defun semantic-c-tag-template-specifier (tag)
1258 "Return the template specifier specification for TAG, or nil."
1259 (semantic-tag-get-attribute tag :template-specifier))
1261 (defun semantic-c-template-string-body (templatespec)
1262 "Convert TEMPLATESPEC into a string.
1263 This might be a string, or a list of tokens."
1264 (cond ((stringp templatespec)
1266 ((semantic-tag-p templatespec)
1267 (semantic-format-tag-abbreviate templatespec))
1268 ((listp templatespec)
1269 (mapconcat 'semantic-format-tag-abbreviate templatespec ", "))))
1271 (defun semantic-c-template-string (token &optional parent color)
1272 "Return a string representing the TEMPLATE attribute of TOKEN.
1273 This string is prefixed with a space, or is the empty string.
1274 Argument PARENT specifies a parent type.
1275 Argument COLOR specifies that the string should be colorized."
1276 (let ((t2 (semantic-c-tag-template-specifier token))
1277 (t1 (semantic-c-tag-template token))
1278 ;; @todo - Need to account for a parent that is a template
1279 (pt1 (if parent (semantic-c-tag-template parent)))
1280 (pt2 (if parent (semantic-c-tag-template-specifier parent)))
1282 (cond (t2 ;; we have a template with specifier
1284 ;; Fill in the parts here
1285 (semantic-c-template-string-body t2)
1287 (t1 ;; we have a template without specifier
1292 (define-mode-local-override semantic-format-tag-concise-prototype
1293 c-mode (token &optional parent color)
1294 "Return an abbreviated string describing TOKEN for C and C++.
1295 Optional PARENT and COLOR as specified with
1296 `semantic-format-tag-abbreviate-default'."
1297 ;; If we have special template things, append.
1298 (concat (semantic-format-tag-concise-prototype-default token parent color)
1299 (semantic-c-template-string token parent color)))
1301 (define-mode-local-override semantic-format-tag-uml-prototype
1302 c-mode (token &optional parent color)
1303 "Return an UML string describing TOKEN for C and C++.
1304 Optional PARENT and COLOR as specified with
1305 `semantic-abbreviate-tag-default'."
1306 ;; If we have special template things, append.
1307 (concat (semantic-format-tag-uml-prototype-default token parent color)
1308 (semantic-c-template-string token parent color)))
1310 (define-mode-local-override semantic-tag-abstract-p
1311 c-mode (tag &optional parent)
1312 "Return non-nil if TAG is considered abstract.
1313 PARENT is tag's parent.
1314 In C, a method is abstract if it is `virtual', which is already
1315 handled. A class is abstract iff it's destructor is virtual."
1317 ((eq (semantic-tag-class tag) 'type)
1318 (require 'semantic/find)
1319 (or (semantic-brute-find-tag-by-attribute :pure-virtual-flag
1320 (semantic-tag-components tag)
1322 (let* ((ds (semantic-brute-find-tag-by-attribute
1324 (semantic-tag-components tag)
1326 (cs (semantic-brute-find-tag-by-attribute
1328 (semantic-tag-components tag)
1330 (and ds (member "virtual" (semantic-tag-modifiers (car ds)))
1331 cs (eq 'protected (semantic-tag-protection (car cs) tag))
1334 ((eq (semantic-tag-class tag) 'function)
1335 (or (semantic-tag-get-attribute tag :pure-virtual-flag)
1336 (member "virtual" (semantic-tag-modifiers tag))))
1337 (t (semantic-tag-abstract-p-default tag parent))))
1339 (defun semantic-c-dereference-typedef (type scope &optional type-declaration)
1340 "If TYPE is a typedef, get TYPE's type by name or tag, and return.
1341 SCOPE is not used, and TYPE-DECLARATION is used only if TYPE is not a typedef."
1342 (if (and (eq (semantic-tag-class type) 'type)
1343 (string= (semantic-tag-type type) "typedef"))
1344 (let ((dt (semantic-tag-get-attribute type :typedef)))
1345 (cond ((and (semantic-tag-p dt)
1346 (not (semantic-analyze-tag-prototype-p dt)))
1347 ;; In this case, DT was declared directly. We need
1348 ;; to clone DT and apply a filename to it.
1349 (let* ((fname (semantic-tag-file-name type))
1350 (def (semantic-tag-copy dt nil fname)))
1352 ((stringp dt) (list dt (semantic-tag dt 'type)))
1353 ((consp dt) (list (car dt) dt))))
1355 (list type type-declaration)))
1357 (defun semantic-c--instantiate-template (tag def-list spec-list)
1358 "Replace TAG name according to template specification.
1359 DEF-LIST is the template information.
1360 SPEC-LIST is the template specifier of the datatype instantiated."
1361 (when (and (car def-list) (car spec-list))
1363 (when (and (string= (semantic-tag-type (car def-list)) "class")
1364 (string= (semantic-tag-name tag) (semantic-tag-name (car def-list))))
1365 (semantic-tag-set-name tag (semantic-tag-name (car spec-list))))
1367 (semantic-c--instantiate-template tag (cdr def-list) (cdr spec-list))))
1369 (defun semantic-c--template-name-1 (spec-list)
1370 "Return a string used to compute template class name.
1371 Based on SPEC-LIST, for ref<Foo,Bar> it will return 'Foo,Bar'."
1372 (when (car spec-list)
1373 (let* ((endpart (semantic-c--template-name-1 (cdr spec-list)))
1374 (separator (and endpart ",")))
1375 (concat (semantic-tag-name (car spec-list)) separator endpart))))
1377 (defun semantic-c--template-name (type spec-list)
1378 "Return a template class name for TYPE based on SPEC-LIST.
1379 For a type `ref' with a template specifier of (Foo Bar) it will
1380 return 'ref<Foo,Bar>'."
1381 (concat (semantic-tag-name type)
1382 "<" (semantic-c--template-name-1 (cdr spec-list)) ">"))
1384 (defun semantic-c-dereference-template (type scope &optional type-declaration)
1385 "Dereference any template specifiers in TYPE within SCOPE.
1386 If TYPE is a template, return a TYPE copy with the templates types
1387 instantiated as specified in TYPE-DECLARATION."
1388 (when (semantic-tag-p type-declaration)
1389 (let ((def-list (semantic-tag-get-attribute type :template))
1390 (spec-list (semantic-tag-get-attribute type-declaration :template-specifier)))
1391 (when (and def-list spec-list)
1392 (setq type (semantic-tag-deep-copy-one-tag
1395 (when (semantic-tag-of-class-p tag 'type)
1396 (semantic-c--instantiate-template
1397 tag def-list spec-list))
1400 (semantic-tag-set-name type (semantic-c--template-name type spec-list))
1401 (semantic-tag-put-attribute type :template nil)
1402 (semantic-tag-set-faux type))))
1403 (list type type-declaration))
1405 ;;; Patch here by "Raf" for instantiating templates.
1406 (defun semantic-c-dereference-member-of (type scope &optional type-declaration)
1407 "Dereference through the `->' operator of TYPE.
1408 Uses the return type of the '->' operator if it is contained in TYPE.
1409 SCOPE is the current local scope to perform searches in.
1410 TYPE-DECLARATION is passed through."
1411 (if semantic-c-member-of-autocast
1412 (let ((operator (car (semantic-find-tags-by-name "->" (semantic-analyze-scoped-type-parts type)))))
1414 (list (semantic-tag-get-attribute operator :type) (semantic-tag-get-attribute operator :type))
1415 (list type type-declaration)))
1416 (list type type-declaration)))
1418 ;; David Engster: The following three functions deal with namespace
1419 ;; aliases and types which are member of a namespace through a using
1420 ;; statement. For examples, see the file semantic/tests/testusing.cpp,
1421 ;; tests 5 and following.
1423 (defun semantic-c-dereference-namespace (type scope &optional type-declaration)
1424 "Dereference namespace which might hold an 'alias' for TYPE.
1425 Such an alias can be created through 'using' statements in a
1426 namespace declaration. This function checks the namespaces in
1427 SCOPE for such statements."
1428 (let ((scopetypes (oref scope scopetypes))
1429 typename currentns tmp usingname result namespaces)
1430 (when (and (semantic-tag-p type-declaration)
1431 (or (null type) (semantic-tag-prototype-p type)))
1432 (setq typename (semantic-analyze-split-name (semantic-tag-name type-declaration)))
1433 ;; If we already have that TYPE in SCOPE, we do nothing
1434 (unless (semantic-deep-find-tags-by-name (or (car-safe typename) typename) scopetypes)
1435 (if (stringp typename)
1436 ;; The type isn't fully qualified, so we have to search in all namespaces in SCOPE.
1437 (setq namespaces (semantic-find-tags-by-type "namespace" scopetypes))
1438 ;; This is a fully qualified name, so we only have to search one namespace.
1439 (setq namespaces (semanticdb-typecache-find (car typename)))
1440 ;; Make sure it's really a namespace.
1441 (if (string= (semantic-tag-type namespaces) "namespace")
1442 (setq namespaces (list namespaces))
1443 (setq namespaces nil)))
1445 ;; Iterate over all the namespaces we have to check.
1446 (while (and namespaces
1448 (setq currentns (car namespaces))
1449 ;; Check if this is namespace is an alias and dereference it if necessary.
1450 (setq result (semantic-c-dereference-namespace-alias type-declaration currentns))
1452 ;; Otherwise, check if we can reach the type through 'using' statements.
1454 (semantic-c-check-type-namespace-using type-declaration currentns)))
1455 (setq namespaces (cdr namespaces)))))
1457 ;; we have found the original type
1458 (list result result)
1459 (list type type-declaration))))
1461 (defun semantic-c-dereference-namespace-alias (type namespace)
1462 "Dereference TYPE in NAMESPACE, given that NAMESPACE is an alias.
1463 Checks if NAMESPACE is an alias and if so, returns a new type
1464 with a fully qualified name in the original namespace. Returns
1465 nil if NAMESPACE is not an alias."
1466 (when (eq (semantic-tag-get-attribute namespace :kind) 'alias)
1467 (let ((typename (semantic-analyze-split-name (semantic-tag-name type)))
1468 ns nstype originaltype newtype)
1469 ;; Make typename unqualified
1470 (if (listp typename)
1471 (setq typename (last typename))
1472 (setq typename (list typename)))
1475 ;; Get original namespace and make sure TYPE exists there.
1476 (setq ns (semantic-tag-name
1477 (car (semantic-tag-get-attribute namespace :members))))
1478 (setq nstype (semanticdb-typecache-find ns))
1479 (setq originaltype (semantic-find-tags-by-name
1481 (semantic-tag-get-attribute nstype :members))))
1482 ;; Construct new type with name in original namespace.
1483 (setq ns (semantic-analyze-split-name ns))
1487 (semantic-analyze-unsplit-name
1489 (append ns typename)
1490 (append (list ns) typename)))))))))
1492 ;; This searches a type in a namespace, following through all using
1494 (defun semantic-c-check-type-namespace-using (type namespace)
1495 "Check if TYPE is accessible in NAMESPACE through a using statement.
1496 Returns the original type from the namespace where it is defined,
1497 or nil if it cannot be found."
1498 (let (usings result usingname usingtype unqualifiedname members shortname tmp)
1499 ;; Get all using statements from NAMESPACE.
1500 (when (and (setq usings (semantic-tag-get-attribute namespace :members))
1501 (setq usings (semantic-find-tags-by-class 'using usings)))
1502 ;; Get unqualified typename.
1503 (when (listp (setq unqualifiedname (semantic-analyze-split-name
1504 (semantic-tag-name type))))
1505 (setq unqualifiedname (car (last unqualifiedname))))
1506 ;; Iterate over all using statements in NAMESPACE.
1509 (setq usingname (semantic-analyze-split-name
1510 (semantic-tag-name (car usings)))
1511 usingtype (semantic-tag-type (semantic-tag-type (car usings))))
1513 ((or (string= usingtype "namespace")
1514 (stringp usingname))
1515 ;; We are dealing with a 'using [namespace] NAMESPACE;'
1516 ;; Search for TYPE in that namespace
1518 (semanticdb-typecache-find usingname))
1520 (setq members (semantic-tag-get-attribute result :members))
1521 (setq members (semantic-find-tags-by-name unqualifiedname members)))
1522 ;; TYPE is member of that namespace, so we are finished
1523 (setq result (car members))
1524 ;; otherwise recursively search in that namespace for an alias
1525 (setq result (semantic-c-check-type-namespace-using type result))
1527 (setq result (semantic-tag-type result)))))
1528 ((and (string= usingtype "class")
1530 ;; We are dealing with a 'using TYPE;'
1531 (when (string= unqualifiedname (car (last usingname)))
1532 ;; We have found the correct tag.
1533 (setq result (semantic-tag-type (car usings))))))
1534 (setq usings (cdr usings))))
1538 (define-mode-local-override semantic-analyze-dereference-metatype
1539 c-mode (type scope &optional type-declaration)
1540 "Dereference TYPE as described in `semantic-analyze-dereference-metatype'.
1541 Handle typedef, template instantiation, and '->' operator."
1542 (let* ((dereferencer-list '(semantic-c-dereference-typedef
1543 semantic-c-dereference-template
1544 semantic-c-dereference-member-of
1545 semantic-c-dereference-namespace))
1546 (dereferencer (pop dereferencer-list))
1548 (original-type type))
1550 (setq type-tuple (funcall dereferencer type scope type-declaration)
1551 type (car type-tuple)
1552 type-declaration (cadr type-tuple))
1553 (if (not (eq type original-type))
1554 ;; we found a new type so break the dereferencer loop now !
1555 ;; (we will be recalled with the new type expanded by
1556 ;; semantic-analyze-dereference-metatype-stack).
1557 (setq dereferencer nil)
1558 ;; no new type found try the next dereferencer :
1559 (setq dereferencer (pop dereferencer-list)))))
1560 (list type type-declaration))
1562 (define-mode-local-override semantic-analyze-type-constants c-mode (type)
1563 "When TYPE is a tag for an enum, return its parts.
1564 These are constants which are of type TYPE."
1565 (if (and (eq (semantic-tag-class type) 'type)
1566 (string= (semantic-tag-type type) "enum"))
1567 (semantic-tag-type-members type)))
1569 (define-mode-local-override semantic-analyze-unsplit-name c-mode (namelist)
1570 "Assemble the list of names NAMELIST into a namespace name."
1571 (mapconcat 'identity namelist "::"))
1573 (define-mode-local-override semantic-ctxt-scoped-types c++-mode (&optional point)
1574 "Return a list of tags of CLASS type based on POINT.
1575 DO NOT return the list of tags encompassing point."
1576 (when point (goto-char (point)))
1577 (let ((tagsaroundpoint (semantic-find-tag-by-overlay))
1580 ;; In C++, we want to find all the namespaces declared
1581 ;; locally and add them to the list.
1582 (setq tmp (semantic-find-tags-by-class 'type (current-buffer)))
1583 (setq tmp (semantic-find-tags-by-type "namespace" tmp))
1584 (setq tmp (semantic-find-tags-by-name "unnamed" tmp))
1585 (setq tagreturn tmp)
1586 ;; We should also find all "using" type statements and
1587 ;; accept those entities in as well.
1588 (setq tmp (semanticdb-find-tags-by-class 'using))
1590 (len (semanticdb-find-result-length tmp)))
1592 (setq tagreturn (cons (semantic-tag-type (car (semanticdb-find-result-nth tmp idx))) tagreturn))
1593 (setq idx (1+ idx)))
1595 ;; Use the encompased types around point to also look for using statements.
1596 ;;(setq tagreturn (cons "bread_name" tagreturn))
1597 (while (cdr tagsaroundpoint) ; don't search the last one
1598 (setq tmp (semantic-find-tags-by-class 'using (semantic-tag-components (car tagsaroundpoint))))
1600 (setq tagreturn (cons (semantic-tag-type T) tagreturn))
1602 (setq tagsaroundpoint (cdr tagsaroundpoint))
1604 ;; If in a function...
1605 (when (and (semantic-tag-of-class-p (car tagsaroundpoint) 'function)
1606 ;; ...search for using statements in the local scope...
1607 (setq tmp (semantic-find-tags-by-class
1609 (semantic-get-local-variables))))
1610 ;; ... and add them.
1613 (mapcar 'semantic-tag-type tmp))))
1618 (define-mode-local-override semantic-ctxt-imported-packages c++-mode (&optional point)
1619 "Return the list of using tag types in scope of POINT."
1620 (when point (goto-char (point)))
1621 (let ((tagsaroundpoint (semantic-find-tag-by-overlay))
1625 ;; Collect using statements from the top level.
1626 (setq tmp (semantic-find-tags-by-class 'using (current-buffer)))
1627 (dolist (T tmp) (setq namereturn (cons (semantic-tag-type T) namereturn)))
1628 ;; Move through the tags around point looking for more using statements
1629 (while (cdr tagsaroundpoint) ; don't search the last one
1630 (setq tmp (semantic-find-tags-by-class 'using (semantic-tag-components (car tagsaroundpoint))))
1631 (dolist (T tmp) (setq namereturn (cons (semantic-tag-type T) namereturn)))
1632 (setq tagsaroundpoint (cdr tagsaroundpoint))
1636 (define-mode-local-override semanticdb-expand-nested-tag c++-mode (tag)
1637 "Expand TAG if it has a fully qualified name.
1638 For types with a :parent, create faux namespaces to put TAG into."
1639 (let ((p (semantic-tag-get-attribute tag :parent)))
1640 (if (and p (semantic-tag-of-class-p tag 'type))
1642 (let ((s (semantic-analyze-split-name p))
1643 (newtag (semantic-tag-copy tag nil t)))
1644 ;; Erase the qualified name.
1645 (semantic-tag-put-attribute newtag :parent nil)
1646 ;; Fixup the namespace name
1647 (setq s (if (stringp s) (list s) (nreverse s)))
1648 ;; Loop over all the parents, creating the nested
1650 (require 'semantic/db-typecache)
1651 (dolist (namespace s)
1652 (setq newtag (semanticdb-typecache-faux-namespace
1653 namespace (list newtag)))
1655 ;; Return the last created namespace.
1657 ;; Else, return tag unmodified.
1660 (define-mode-local-override semantic-get-local-variables c++-mode ()
1661 "Do what `semantic-get-local-variables' does, plus add `this' if needed."
1662 (let* ((origvar (semantic-get-local-variables-default))
1663 (ct (semantic-current-tag))
1664 (p (semantic-tag-function-parent ct)))
1665 ;; If we have a function parent, then that implies we can
1666 (if (and p (semantic-tag-of-class-p ct 'function))
1667 ;; Append a new tag THIS into our space.
1668 (cons (semantic-tag-new-variable "this" p nil)
1670 ;; No parent, just return the usual
1674 (define-mode-local-override semantic-idle-summary-current-symbol-info
1676 "Handle the SPP keywords, then use the default mechanism."
1677 (let* ((sym (car (semantic-ctxt-current-thing)))
1678 (spp-sym (semantic-lex-spp-symbol sym)))
1680 (let* ((txt (concat "Macro: " sym))
1681 (sv (symbol-value spp-sym))
1682 (arg (semantic-lex-spp-macro-with-args sv))
1685 (setq txt (concat txt (format "%S" arg)))
1688 ;; This is optional, and potentially fraught w/ errors.
1691 (setq txt (concat txt " " (semantic-lex-token-text lt))))
1692 (error (setq txt (concat txt " #error in summary fcn"))))
1695 (semantic-idle-summary-current-symbol-info-default))))
1697 (defvar-mode-local c-mode semantic-orphaned-member-metaparent-type "struct"
1698 "When lost members are found in the class hierarchy generator, use a struct.")
1700 (defvar-mode-local c-mode semantic-symbol->name-assoc-list
1702 (variable . "Variables")
1703 (function . "Functions")
1704 (include . "Includes")
1706 "List of tag classes, and strings to describe them.")
1708 (defvar-mode-local c-mode semantic-symbol->name-assoc-list-for-type-parts
1710 (variable . "Attributes")
1711 (function . "Methods")
1714 "List of tag classes in a datatype decl, and strings to describe them.")
1716 (defvar-mode-local c-mode imenu-create-index-function 'semantic-create-imenu-index
1717 "Imenu index function for C.")
1719 (defvar-mode-local c-mode semantic-type-relation-separator-character
1721 "Separator characters between something of a given type, and a field.")
1723 (defvar-mode-local c-mode semantic-command-separation-character ";"
1724 "Command separation character for C.")
1726 (defvar-mode-local c-mode senator-step-at-tag-classes '(function variable)
1727 "Tag classes where senator will stop at the end.")
1730 (defun semantic-default-c-setup ()
1731 "Set up a buffer for semantic parsing of the C language."
1732 (semantic-c-by--install-parser)
1733 (setq semantic-lex-syntax-modifications '((?> ".")
1738 (setq semantic-lex-analyzer #'semantic-c-lexer)
1739 (add-hook 'semantic-lex-reset-hooks 'semantic-lex-spp-reset-hook nil t)
1743 (defun semantic-c-add-preprocessor-symbol (sym replacement)
1744 "Add a preprocessor symbol SYM with a REPLACEMENT value."
1745 (interactive "sSymbol: \nsReplacement: ")
1746 (let ((SA (assoc sym semantic-lex-c-preprocessor-symbol-map)))
1748 ;; Replace if there is one.
1749 (setcdr SA replacement)
1750 ;; Otherwise, append
1751 (setq semantic-lex-c-preprocessor-symbol-map
1752 (cons (cons sym replacement)
1753 semantic-lex-c-preprocessor-symbol-map))))
1755 (semantic-c-reset-preprocessor-symbol-map)
1760 (defun semantic-c-describe-environment ()
1761 "Describe the Semantic features of the current C environment."
1763 (if (not (or (eq major-mode 'c-mode) (eq major-mode 'c++-mode)))
1764 (error "Not useful to query C mode in %s mode" major-mode))
1765 (let ((gcc (when (boundp 'semantic-gcc-setup-data)
1766 semantic-gcc-setup-data))
1768 (semantic-fetch-tags)
1770 (with-output-to-temp-buffer "*Semantic C Environment*"
1772 (princ "Calculated GCC Parameters:")
1781 (princ "\n\nInclude Path Summary:\n")
1782 (when (and (boundp 'ede-object) ede-object)
1783 (princ "\n This file's project include is handled by:\n")
1785 (princ (object-print ede-object))
1786 (princ "\n with the system path:\n")
1787 (dolist (dir (ede-system-include-path ede-object))
1793 (when semantic-dependency-include-path
1794 (princ "\n This file's generic include path is:\n")
1795 (dolist (dir semantic-dependency-include-path)
1800 (when semantic-dependency-system-include-path
1801 (princ "\n This file's system include path is:\n")
1802 (dolist (dir semantic-dependency-system-include-path)
1807 (princ "\n\nMacro Summary:\n")
1808 (when semantic-lex-c-preprocessor-symbol-file
1809 (princ "\n Your CPP table is primed from these files:\n")
1810 (dolist (file semantic-lex-c-preprocessor-symbol-file)
1814 (princ " in table: ")
1815 (princ (object-print (semanticdb-file-table-object file)))
1819 (when semantic-lex-c-preprocessor-symbol-map-builtin
1820 (princ "\n Built-in symbol map:\n")
1821 (dolist (S semantic-lex-c-preprocessor-symbol-map-builtin)
1829 (when semantic-lex-c-preprocessor-symbol-map
1830 (princ "\n User symbol map:\n")
1831 (dolist (S semantic-lex-c-preprocessor-symbol-map)
1839 (when (and (boundp 'ede-object)
1841 (arrayp semantic-lex-spp-project-macro-symbol-obarray))
1842 (princ "\n Project symbol map:\n")
1843 (when (and (boundp 'ede-object) ede-object)
1844 (princ " Your project symbol map is derived from the EDE object:\n ")
1845 (princ (object-print ede-object)))
1850 (setq macros (cons symbol macros)))
1851 semantic-lex-spp-project-macro-symbol-obarray)
1854 (princ (symbol-name S))
1856 (princ (symbol-value S))
1860 (princ "\n\n Use: M-x semantic-lex-spp-describe RET\n")
1861 (princ "\n to see the complete macro table.\n")
1865 (provide 'semantic/bovine/c)
1867 (semantic-c-reset-preprocessor-symbol-map)
1870 ;; generated-autoload-file: "../loaddefs.el"
1871 ;; generated-autoload-load-name: "semantic/bovine/c"
1874 ;;; semantic/bovine/c.el ends here