c.el 81.2 KB
Newer Older
1 2
;;; semantic/bovine/c.el --- Semantic details for C

3
;; Copyright (C) 1999-2013 Free Software Foundation, Inc.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

;; Author: Eric M. Ludlam <zappo@gnu.org>

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; Support for the C/C++ bovine parser for Semantic.
;;
;; @todo - can I support c++-font-lock-extra-types ?

(require 'semantic)
(require 'semantic/analyze)
David Engster's avatar
David Engster committed
30
(require 'semantic/analyze/refs)
Chong Yidong's avatar
Chong Yidong committed
31
(require 'semantic/bovine)
32 33 34 35
(require 'semantic/bovine/gcc)
(require 'semantic/idle)
(require 'semantic/lex-spp)
(require 'semantic/bovine/c-by)
Chong Yidong's avatar
Chong Yidong committed
36 37
(require 'semantic/db-find)
(require 'hideif)
38 39 40 41 42 43 44

(eval-when-compile
  (require 'semantic/find))

(declare-function semantic-brute-find-tag-by-attribute "semantic/find")
(declare-function semanticdb-minor-mode-p "semantic/db-mode")
(declare-function semanticdb-needs-refresh-p "semantic/db")
Chong Yidong's avatar
Chong Yidong committed
45
(declare-function semanticdb-typecache-faux-namespace "semantic/db-typecache")
46
(declare-function c-forward-conditional "cc-cmds")
47
(declare-function ede-system-include-path "ede")
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

;;; Compatibility
;;
(eval-when-compile (require 'cc-mode))

(if (fboundp 'c-end-of-macro)
    (eval-and-compile
      (defalias 'semantic-c-end-of-macro 'c-end-of-macro))
  ;; From cc-mode 5.30
  (defun semantic-c-end-of-macro ()
    "Go to the end of a preprocessor directive.
More accurately, move point to the end of the closest following line
that doesn't end with a line continuation backslash.

This function does not do any hidden buffer changes."
    (while (progn
             (end-of-line)
             (when (and (eq (char-before) ?\\)
                        (not (eobp)))
               (forward-char)
               t))))
  )

;;; Code:
(define-child-mode c++-mode c-mode
  "`c++-mode' uses the same parser as `c-mode'.")


;;; Include Paths
;;
(defcustom-mode-local-semantic-dependency-system-include-path
  c-mode semantic-c-dependency-system-include-path
  '("/usr/include")
81
  "The system include path used by the C language.")
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

(defcustom semantic-default-c-path nil
  "Default set of include paths for C code.
Used by `semantic-dep' to define an include path.
NOTE: In process of obsoleting this."
  :group 'c
  :group 'semantic
  :type '(repeat (string :tag "Path")))

(defvar-mode-local c-mode semantic-dependency-include-path
  semantic-default-c-path
  "System path to search for include files.")

;;; Compile Options
;;
;; Compiler options need to show up after path setup, but before
;; the preprocessor section.

Glenn Morris's avatar
Glenn Morris committed
100 101
(if (memq system-type '(gnu gnu/linux darwin cygwin))
    (semantic-gcc-setup))
102 103 104 105 106 107 108 109

;;; Pre-processor maps
;;
;;; Lexical analysis
(defvar semantic-lex-c-preprocessor-symbol-map-builtin
  '( ("__THROW" . "")
     ("__const" . "const")
     ("__restrict" . "")
Chong Yidong's avatar
Chong Yidong committed
110 111 112 113
     ("__attribute_pure__" . "")
     ("__attribute_malloc__" . "")
     ("__nonnull" . "")
     ("__wur" . "")
114 115
     ("__declspec" . ((spp-arg-list ("foo") 1 . 2)))
     ("__attribute__" . ((spp-arg-list ("foo") 1 . 2)))
Chong Yidong's avatar
Chong Yidong committed
116
     ("__asm" . ((spp-arg-list ("foo") 1 . 2)))
117 118 119 120 121 122 123 124 125 126 127 128 129
     )
  "List of symbols to include by default.")

(defvar semantic-c-in-reset-preprocessor-table nil
  "Non-nil while resetting the preprocessor symbol map.
Used to prevent a reset while trying to parse files that are
part of the preprocessor map.")

(defvar semantic-lex-c-preprocessor-symbol-file)
(defvar semantic-lex-c-preprocessor-symbol-map)

(defun semantic-c-reset-preprocessor-symbol-map ()
  "Reset the C preprocessor symbol map based on all input variables."
Chong Yidong's avatar
Chong Yidong committed
130 131 132 133 134 135 136 137 138
  (when (and semantic-mode
	     (featurep 'semantic/bovine/c))
    (remove-hook 'mode-local-init-hook 'semantic-c-reset-preprocessor-symbol-map)
    ;; Initialize semantic-lex-spp-macro-symbol-obarray with symbols.
    (setq-mode-local c-mode
		     semantic-lex-spp-macro-symbol-obarray
		     (semantic-lex-make-spp-table
		      (append semantic-lex-c-preprocessor-symbol-map-builtin
			      semantic-lex-c-preprocessor-symbol-map)))
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    (let ((filemap nil)
	  )
      (when (and (not semantic-c-in-reset-preprocessor-table)
		 (featurep 'semantic/db-mode)
		 (semanticdb-minor-mode-p))
	(let ( ;; Don't use external parsers.  We need the internal one.
	      (semanticdb-out-of-buffer-create-table-fcn nil)
	      ;; Don't recurse while parsing these files the first time.
	      (semantic-c-in-reset-preprocessor-table t)
	      )
	  (dolist (sf semantic-lex-c-preprocessor-symbol-file)
	    ;; Global map entries
	    (let* ((table (semanticdb-file-table-object sf t)))
	      (when table
		(when (semanticdb-needs-refresh-p table)
		  (condition-case nil
		      ;; Call with FORCE, as the file is very likely to
		      ;; not be in a buffer.
		      (semanticdb-refresh-table table t)
		    (error (message "Error updating tables for %S"
159
				    (eieio-object-name table)))))
160
		(setq filemap (append filemap (oref table lexical-table)))
161 162 163 164 165 166 167 168
		)))))
      ;; Update symbol obarray
      (setq-mode-local c-mode
		       semantic-lex-spp-macro-symbol-obarray
		       (semantic-lex-make-spp-table
			(append semantic-lex-c-preprocessor-symbol-map-builtin
				semantic-lex-c-preprocessor-symbol-map
				filemap))))))
Chong Yidong's avatar
Chong Yidong committed
169 170 171 172

;; Make sure the preprocessor symbols are set up when mode-local kicks
;; in.
(add-hook 'mode-local-init-hook 'semantic-c-reset-preprocessor-symbol-map)
173 174 175 176 177 178

(defcustom semantic-lex-c-preprocessor-symbol-map nil
  "Table of C Preprocessor keywords used by the Semantic C lexer.
Each entry is a cons cell like this:
  ( \"KEYWORD\" . \"REPLACEMENT\" )
Where KEYWORD is the macro that gets replaced in the lexical phase,
Chong Yidong's avatar
Chong Yidong committed
179
and REPLACEMENT is a string that is inserted in its place.  Empty string
180 181 182 183 184 185 186 187 188
implies that the lexical analyzer will discard KEYWORD when it is encountered.

Alternately, it can be of the form:
  ( \"KEYWORD\" ( LEXSYM1 \"str\" 1 1 ) ... ( LEXSYMN \"str\" 1 1 ) )
where LEXSYM is a symbol that would normally be produced by the
lexical analyzer, such as `symbol' or `string'.  The string in the
second position is the text that makes up the replacement.  This is
the way to have multiple lexical symbols in a replacement.  Using the
first way to specify text like \"foo::bar\" would not work, because :
Glenn Morris's avatar
Glenn Morris committed
189
is a separate lexical symbol.
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227

A quick way to see what you would need to insert is to place a
definition such as:

#define MYSYM foo::bar

into a C file, and do this:
  \\[semantic-lex-spp-describe]

The output table will describe the symbols needed."
  :group 'c
  :type '(repeat (cons (string :tag "Keyword")
		       (sexp :tag "Replacement")))
  :set (lambda (sym value)
	 (set-default sym value)
	 (condition-case nil
	     (semantic-c-reset-preprocessor-symbol-map)
	   (error nil))
	 )
  )

(defcustom semantic-lex-c-preprocessor-symbol-file nil
  "List of C/C++ files that contain preprocessor macros for the C lexer.
Each entry is a filename and each file is parsed, and those macros
are included in every C/C++ file parsed by semantic.
You can use this variable instead of `semantic-lex-c-preprocessor-symbol-map'
to store your global macros in a more natural way."
  :group 'c
  :type '(repeat (file :tag "File"))
  :set (lambda (sym value)
	 (set-default sym value)
	 (condition-case nil
	     (semantic-c-reset-preprocessor-symbol-map)
	   (error nil))
	 )
  )

(defcustom semantic-c-member-of-autocast 't
228
  "Non-nil means classes with a '->' operator will cast to its return type.
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248

For Examples:

  class Foo {
    Bar *operator->();
  }

  Foo foo;

if `semantic-c-member-of-autocast' is non-nil :
  foo->[here completion will list method of Bar]

if `semantic-c-member-of-autocast' is nil :
  foo->[here completion will list method of Foo]"
  :group 'c
  :type 'boolean)

(define-lex-spp-macro-declaration-analyzer semantic-lex-cpp-define
  "A #define of a symbol with some value.
Record the symbol in the semantic preprocessor.
249
Return the defined symbol as a special spp lex token."
250 251 252 253 254 255 256
  "^\\s-*#\\s-*define\\s-+\\(\\(\\sw\\|\\s_\\)+\\)" 1
  (goto-char (match-end 0))
  (skip-chars-forward " \t")
  (if (eolp)
      nil
    (let* ((name (buffer-substring-no-properties
		  (match-beginning 1) (match-end 1)))
Chong Yidong's avatar
Chong Yidong committed
257
	   (beginning-of-define (match-end 1))
258 259 260 261
	   (with-args (save-excursion
			(goto-char (match-end 0))
			(looking-at "(")))
	   (semantic-lex-spp-replacements-enabled nil)
Paul Eggert's avatar
Paul Eggert committed
262
	   ;; Temporarily override the lexer to include
263 264 265 266 267
	   ;; special items needed inside a macro
	   (semantic-lex-analyzer #'semantic-cpp-lexer)
	   (raw-stream
	    (semantic-lex-spp-stream-for-macro (save-excursion
						 (semantic-c-end-of-macro)
Chong Yidong's avatar
Chong Yidong committed
268 269 270 271 272 273 274
						 ;; HACK - If there's a C comment after
						 ;; the macro, do not parse it.
						 (if (looking-back "/\\*.*" beginning-of-define)
						     (progn
						       (goto-char (match-beginning 0))
						       (1- (point)))
						   (point)))))
275 276
	   )

Juanma Barranquero's avatar
Juanma Barranquero committed
277
      ;; Only do argument checking if the paren was immediately after
278 279 280 281 282 283 284 285 286 287 288 289 290 291
      ;; the macro name.
      (if with-args
	  (semantic-lex-spp-first-token-arg-list (car raw-stream)))

      ;; Magical spp variable for end point.
      (setq semantic-lex-end-point (point))

      ;; Handled nested macro streams.
      (semantic-lex-spp-merge-streams raw-stream)
      )))

(define-lex-spp-macro-undeclaration-analyzer semantic-lex-cpp-undef
  "A #undef of a symbol.
Remove the symbol from the semantic preprocessor.
292
Return the defined symbol as a special spp lex token."
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
  "^\\s-*#\\s-*undef\\s-+\\(\\(\\sw\\|\\s_\\)+\\)" 1)


;;; Conditional Skipping
;;
(defcustom semantic-c-obey-conditional-section-parsing-flag t
  "*Non-nil means to interpret preprocessor #if sections.
This implies that some blocks of code will not be parsed based on the
values of the conditions in the #if blocks."
  :group 'c
  :type 'boolean)

(defun semantic-c-skip-conditional-section ()
  "Skip one section of a conditional.
Moves forward to a matching #elif, #else, or #endif.
308
Moves completely over balanced #if blocks."
309 310 311 312 313 314 315 316 317 318 319 320 321 322
  (require 'cc-cmds)
  (let ((done nil))
    ;; (if (looking-at "^\\s-*#if")
    ;; (semantic-lex-spp-push-if (point))
    (end-of-line)
    (while (and semantic-c-obey-conditional-section-parsing-flag
		(and (not done)
		     (re-search-forward
		      "^\\s-*#\\s-*\\(if\\(n?def\\)?\\|el\\(if\\|se\\)\\|endif\\)\\>"
		      nil t)))
      (goto-char (match-beginning 0))
      (cond
       ((looking-at "^\\s-*#\\s-*if")
	;; We found a nested if.  Skip it.
Chong Yidong's avatar
Chong Yidong committed
323 324 325 326
	(if (fboundp 'c-scan-conditionals)
	    (goto-char (c-scan-conditionals 1))
	  ;; For older Emacsen, but this will set the mark.
	  (c-forward-conditional 1)))
327
       ((looking-at "^\\s-*#\\s-*elif")
Juanma Barranquero's avatar
Juanma Barranquero committed
328
	;; We need to let the preprocessor analyze this one.
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
	(beginning-of-line)
	(setq done t)
	)
       ((looking-at "^\\s-*#\\s-*\\(endif\\|else\\)\\>")
	;; We are at the end.  Pop our state.
	;; (semantic-lex-spp-pop-if)
	;; Note: We include ELSE and ENDIF the same. If skip some previous
	;; section, then we should do the else by default, making it much
	;; like the endif.
	(end-of-line)
	(forward-char 1)
	(setq done t))
       (t
	;; We found an elif.  Stop here.
	(setq done t))))))

Chong Yidong's avatar
Chong Yidong committed
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
;;; HIDEIF USAGE:
;; NOTE: All hideif using code was contributed by Brian Carlson as
;;       copies from hideif plus modifications and additions.
;;       Eric then converted things to use hideif functions directly,
;;       deleting most of that code, and added the advice.

;;; SPP SYM EVAL
;;
;; Convert SPP symbols into values usable by hideif.
;;
;; @TODO - can these conversion fcns be a part of semantic-lex-spp.el?
;;       -- TRY semantic-lex-spp-one-token-to-txt
(defun semantic-c-convert-spp-value-to-hideif-value (symbol macrovalue)
  "Convert an spp macro SYMBOL MACROVALUE, to something that hideif can use.
Take the first interesting thing and convert it."
  ;; Just warn for complex macros.
  (when (> (length macrovalue) 1)
    (semantic-push-parser-warning
     (format "Complex macro value (%s) may be improperly evaluated. "
	     symbol) 0 0))

  (let* ((lextoken (car macrovalue))
	 (key (semantic-lex-token-class lextoken))
	 (value (semantic-lex-token-text lextoken)))
    (cond
     ((eq key 'number) (string-to-number value))
     ((eq key 'symbol) (semantic-c-evaluate-symbol-for-hideif value))
     ((eq key 'string)
      (if (string-match "^[0-9]+L?$" value)
	  ;; If it matches a number expression, then
	  ;; convert to a number.
	  (string-to-number value)
	value))
     (t (semantic-push-parser-warning
	 (format "Unknown macro value. Token class = %s value = %s. " key value)
	 0 0)
      nil)
     )))

(defun semantic-c-evaluate-symbol-for-hideif (spp-symbol)
  "Lookup the symbol SPP-SYMBOL (a string) to something hideif can use.
Pulls out the symbol list, and call `semantic-c-convert-spp-value-to-hideif-value'."
  (interactive "sSymbol name: ")
  (when (symbolp spp-symbol) (setq spp-symbol (symbol-name spp-symbol)))

  (if (semantic-lex-spp-symbol-p spp-symbol )
      ;; Convert the symbol into a stream of tokens from the macro which we
      ;; can then interpret.
      (let ((stream (semantic-lex-spp-symbol-stream spp-symbol)))
	(cond
Paul Eggert's avatar
Paul Eggert committed
395
	 ;; Empty string means defined, so t.
Chong Yidong's avatar
Chong Yidong committed
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
	 ((null stream) t)
	 ;; A list means a parsed macro stream.
	 ((listp stream)
	  ;; Convert the macro to something we can return.
	  (semantic-c-convert-spp-value-to-hideif-value spp-symbol stream))

	 ;; Strings might need to be turned into numbers
	 ((stringp stream)
	  (if (string-match "^[0-9]+L?$" stream)
	      ;; If it matches a number expression, then convert to a
	      ;; number.
	      (string-to-number stream)
	    stream))

	 ;; Just return the stream.  A user might have just stuck some
	 ;; value in it directly.
	 (t stream)
	 ))
    ;; Else, store an error, return nil.
    (progn
      (semantic-push-parser-warning
       (format "SPP Symbol %s not available" spp-symbol)
       (point) (point))
      nil)))

;;; HIDEIF HACK support fcns
;;
;; These fcns can replace the impl of some hideif features.
;;
;; @TODO - Should hideif and semantic-c merge?
;;       I picture a grammar just for CPP that expands into
;;       a second token stream for the parser.
(defun semantic-c-hideif-lookup (var)
  "Replacement for `hif-lookup'.
I think it just gets the value for some CPP variable VAR."
  (let ((val (semantic-c-evaluate-symbol-for-hideif
              (cond
               ((stringp var) var)
               ((symbolp var) (symbol-name var))
               (t "Unable to determine var")))))
    (if val
	val
      ;; Real hideif will return the right undefined symbol.
      nil)))

(defun semantic-c-hideif-defined (var)
  "Replacement for `hif-defined'.
I think it just returns t/nil dependent on if VAR has been defined."
  (let ((var-symbol-name
          (cond
           ((symbolp var) (symbol-name var))
           ((stringp var) var)
           (t "Not A Symbol"))))
    (if (not (semantic-lex-spp-symbol-p var-symbol-name))
        (progn
          (semantic-push-parser-warning
	   (format "Skip %s" (buffer-substring-no-properties
			      (point-at-bol) (point-at-eol)))
	   (point-at-bol) (point-at-eol))
          nil)
      t)))

;;; HIDEIF ADVICE
;;
;; Advise hideif functions to use our lexical tables instead.
(defvar semantic-c-takeover-hideif nil
  "Non-nil when Semantic is taking over hideif features.")

;; (defadvice hif-defined (around semantic-c activate)
;;   "Is the variable defined?"
;;   (if semantic-c-takeover-hideif
;;       (setq ad-return-value
;; 	    (semantic-c-hideif-defined (ad-get-arg 0)))
;;     ad-do-it))

;; (defadvice hif-lookup (around semantic-c activate)
;;   "Is the argument defined?  Return true or false."
;;   (let ((ans nil))
;;     (when semantic-c-takeover-hideif
;;       (setq ans (semantic-c-hideif-lookup (ad-get-arg 0))))
;;     (if (null ans)
;; 	ad-do-it
;;       (setq ad-return-value ans))))

;;; #if macros
;;
;; Support #if macros by evaluating the values via use of hideif
;; logic.  See above for hacks to make this work.
484 485 486
(define-lex-regex-analyzer semantic-lex-c-if
  "Code blocks wrapped up in #if, or #ifdef.
Uses known macro tables in SPP to determine what block to skip."
Chong Yidong's avatar
Chong Yidong committed
487
  "^\\s-*#\\s-*\\(if\\|elif\\).*$"
488 489 490
  (semantic-c-do-lex-if))

(defun semantic-c-do-lex-if ()
Chong Yidong's avatar
Chong Yidong committed
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
  "Handle lexical CPP if statements.
Enables a takeover of some hideif functions, then uses hideif to
evaluate the #if expression and enables us to make decisions on which
code to parse."
  ;; Enable our advice, and use hideif to parse.
  (let* ((semantic-c-takeover-hideif t)
	 (hif-ifx-regexp (concat hif-cpp-prefix "\\(elif\\|if\\(n?def\\)?\\)[ \t]+"))
	 (parsedtokelist
	  (condition-case nil
	      ;; This is imperfect, so always assume on error.
	      (hif-canonicalize)
	    (error nil))))

    (let ((eval-form (eval parsedtokelist)))
      (if (or (not eval-form)
              (and (numberp eval-form)
                   (equal eval-form 0)));; ifdefline resulted in false

	;; The if indicates to skip this preprocessor section
	(let ((pt nil))
	  (semantic-push-parser-warning (format "Skip %s" (buffer-substring-no-properties (point-at-bol) (point-at-eol)))
					(point-at-bol) (point-at-eol))
	  (beginning-of-line)
	  (setq pt (point))
	  ;; This skips only a section of a conditional.  Once that section
	  ;; is opened, encountering any new #else or related conditional
	  ;; should be skipped.
	  (semantic-c-skip-conditional-section)
	  (setq semantic-lex-end-point (point))
Paul Eggert's avatar
Paul Eggert committed
520

Chong Yidong's avatar
Chong Yidong committed
521 522 523 524 525 526 527 528 529 530 531 532
	  ;; @TODO -somewhere around here, we also need to skip
	  ;; other sections of the conditional.

	  nil)
      ;; Else, don't ignore it, but do handle the internals.
      (end-of-line)
      (setq semantic-lex-end-point (point))
      nil))))

(define-lex-regex-analyzer semantic-lex-c-ifdef
  "Code blocks wrapped up in #ifdef.
Uses known macro tables in SPP to determine what block to skip."
533
  "^\\s-*#\\s-*\\(ifndef\\|ifdef\\)\\s-+\\(\\(\\sw\\|\\s_\\)+\\)\\([ \t\C-m].*\\)?$"
Chong Yidong's avatar
Chong Yidong committed
534 535 536
  (semantic-c-do-lex-ifdef))

(defun semantic-c-do-lex-ifdef ()
537 538
  "Handle lexical CPP if statements."
  (let* ((sym (buffer-substring-no-properties
Chong Yidong's avatar
Chong Yidong committed
539
	       (match-beginning 2) (match-end 2)))
540 541
	 (ift (buffer-substring-no-properties
	       (match-beginning 1) (match-end 1)))
Chong Yidong's avatar
Chong Yidong committed
542 543
	 (ifdef (string= ift "ifdef"))
	 (ifndef (string= ift "ifndef"))
544
	 )
Chong Yidong's avatar
Chong Yidong committed
545
    (if (or (and ifdef (not (semantic-lex-spp-symbol-p sym)))
546
	    (and ifndef (semantic-lex-spp-symbol-p sym)))
Paul Eggert's avatar
Paul Eggert committed
547
	;; The if indicates to skip this preprocessor section.
548 549 550 551 552 553 554 555 556 557 558
	(let ((pt nil))
	  ;; (message "%s %s yes" ift sym)
	  (beginning-of-line)
	  (setq pt (point))
	  ;; This skips only a section of a conditional.  Once that section
	  ;; is opened, encountering any new #else or related conditional
	  ;; should be skipped.
	  (semantic-c-skip-conditional-section)
	  (setq semantic-lex-end-point (point))
	  (semantic-push-parser-warning (format "Skip #%s %s" ift sym)
					pt (point))
Chong Yidong's avatar
Chong Yidong committed
559 560
	  ;;	  (semantic-lex-push-token
	  ;;	   (semantic-lex-token 'c-preprocessor-skip pt (point)))
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
	  nil)
      ;; Else, don't ignore it, but do handle the internals.
      ;;(message "%s %s no" ift sym)
      (end-of-line)
      (setq semantic-lex-end-point (point))
      nil)))

(define-lex-regex-analyzer semantic-lex-c-macro-else
  "Ignore an #else block.
We won't see the #else due to the macro skip section block
unless we are actively parsing an open #if statement.  In that
case, we must skip it since it is the ELSE part."
  "^\\s-*#\\s-*\\(else\\)"
  (let ((pt (point)))
    (semantic-c-skip-conditional-section)
    (setq semantic-lex-end-point (point))
    (semantic-push-parser-warning "Skip #else" pt (point))
;;    (semantic-lex-push-token
;;     (semantic-lex-token 'c-preprocessor-skip pt (point)))
    nil))

(define-lex-regex-analyzer semantic-lex-c-macrobits
  "Ignore various forms of #if/#else/#endif conditionals."
  "^\\s-*#\\s-*\\(if\\(n?def\\)?\\|endif\\|elif\\|else\\)"
  (semantic-c-end-of-macro)
  (setq semantic-lex-end-point (point))
  nil)

(define-lex-spp-include-analyzer semantic-lex-c-include-system
  "Identify include strings, and return special tokens."
    "^\\s-*#\\s-*include\\s-*<\\([^ \t\n>]+\\)>" 0
    ;; Hit 1 is the name of the include.
    (goto-char (match-end 0))
    (setq semantic-lex-end-point (point))
    (cons (buffer-substring-no-properties (match-beginning 1)
					  (match-end 1))
	  'system))

(define-lex-spp-include-analyzer semantic-lex-c-include
  "Identify include strings, and return special tokens."
    "^\\s-*#\\s-*include\\s-*\"\\([^ \t\n>]+\\)\"" 0
    ;; Hit 1 is the name of the include.
    (goto-char (match-end 0))
    (setq semantic-lex-end-point (point))
    (cons (buffer-substring-no-properties (match-beginning 1)
					  (match-end 1))
	  nil))


(define-lex-regex-analyzer semantic-lex-c-ignore-ending-backslash
  "Skip backslash ending a line.
Go to the next line."
  "\\\\\\s-*\n"
  (setq semantic-lex-end-point (match-end 0)))

(define-lex-regex-analyzer semantic-lex-c-namespace-begin-macro
  "Handle G++'s namespace macros which the pre-processor can't handle."
  "\\(_GLIBCXX_BEGIN_NAMESPACE\\)(\\s-*\\(\\(?:\\w\\|\\s_\\)+\\)\\s-*)"
  (let* ((nsend (match-end 1))
	 (sym-start (match-beginning 2))
	 (sym-end (match-end 2))
	 (ms (buffer-substring-no-properties sym-start sym-end)))
    ;; Push the namespace keyword.
    (semantic-lex-push-token
     (semantic-lex-token 'NAMESPACE (match-beginning 0) nsend "namespace"))
    ;; Push the name.
    (semantic-lex-push-token
     (semantic-lex-token 'symbol sym-start sym-end ms))
    )
  (goto-char (match-end 0))
  (let ((start (point))
	(end 0))
    ;; If we can't find a matching end, then create the fake list.
    (when (re-search-forward "_GLIBCXX_END_NAMESPACE" nil t)
      (setq end (point))
      (semantic-lex-push-token
       (semantic-lex-token 'semantic-list start end
			   (list 'prefix-fake)))))
  (setq semantic-lex-end-point (point)))

(defcustom semantic-lex-c-nested-namespace-ignore-second t
  "Should _GLIBCXX_BEGIN_NESTED_NAMESPACE ignore the second namespace?
It is really there, but if a majority of uses is to squeeze out
the second namespace in use, then it should not be included.

If you are having problems with smart completion and STL templates,
647
it may be that this is set incorrectly.  After changing the value
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
of this flag, you will need to delete any semanticdb cache files
that may have been incorrectly parsed."
  :group 'semantic
  :type 'boolean)

(define-lex-regex-analyzer semantic-lex-c-VC++-begin-std-namespace
  "Handle VC++'s definition of the std namespace."
  "\\(_STD_BEGIN\\)"
  (semantic-lex-push-token
   (semantic-lex-token 'NAMESPACE (match-beginning 0) (match-end 0) "namespace"))
  (semantic-lex-push-token
   (semantic-lex-token 'symbol (match-beginning 0) (match-end 0) "std"))
  (goto-char (match-end 0))
  (let ((start (point))
	(end 0))
    (when (re-search-forward "_STD_END" nil t)
      (setq end (point))
      (semantic-lex-push-token
       (semantic-lex-token 'semantic-list start end
			   (list 'prefix-fake)))))
  (setq semantic-lex-end-point (point)))

(define-lex-regex-analyzer semantic-lex-c-VC++-end-std-namespace
  "Handle VC++'s definition of the std namespace."
  "\\(_STD_END\\)"
  (goto-char (match-end 0))
  (setq semantic-lex-end-point (point)))

(define-lex-regex-analyzer semantic-lex-c-namespace-begin-nested-macro
  "Handle G++'s namespace macros which the pre-processor can't handle."
  "\\(_GLIBCXX_BEGIN_NESTED_NAMESPACE\\)(\\s-*\\(\\(?:\\w\\|\\s_\\)+\\)\\s-*,\\s-*\\(\\(?:\\w\\|\\s_\\)+\\)\\s-*)"
  (goto-char (match-end 0))
  (let* ((nsend (match-end 1))
	 (sym-start (match-beginning 2))
	 (sym-end (match-end 2))
	 (ms (buffer-substring-no-properties sym-start sym-end))
	 (sym2-start (match-beginning 3))
	 (sym2-end (match-end 3))
	 (ms2 (buffer-substring-no-properties sym2-start sym2-end)))
    ;; Push the namespace keyword.
    (semantic-lex-push-token
     (semantic-lex-token 'NAMESPACE (match-beginning 0) nsend "namespace"))
    ;; Push the name.
    (semantic-lex-push-token
     (semantic-lex-token 'symbol sym-start sym-end ms))

    (goto-char (match-end 0))
    (let ((start (point))
	  (end 0))
      ;; If we can't find a matching end, then create the fake list.
      (when (re-search-forward "_GLIBCXX_END_NESTED_NAMESPACE" nil t)
	(setq end (point))
	(if semantic-lex-c-nested-namespace-ignore-second
	    ;; The same as _GLIBCXX_BEGIN_NAMESPACE
	    (semantic-lex-push-token
	     (semantic-lex-token 'semantic-list start end
				 (list 'prefix-fake)))
	  ;; Do both the top and second level namespace
	  (semantic-lex-push-token
	   (semantic-lex-token 'semantic-list start end
			       ;; We'll depend on a quick hack
			       (list 'prefix-fake-plus
				     (semantic-lex-token 'NAMESPACE
							 sym-end sym2-start
							 "namespace")
				     (semantic-lex-token 'symbol
							 sym2-start sym2-end
							 ms2)
				     (semantic-lex-token 'semantic-list start end
							 (list 'prefix-fake)))
			       )))
	)))
  (setq semantic-lex-end-point (point)))

(define-lex-regex-analyzer semantic-lex-c-namespace-end-macro
  "Handle G++'s namespace macros which the pre-processor can't handle."
  "_GLIBCXX_END_\\(NESTED_\\)?NAMESPACE"
  (goto-char (match-end 0))
  (setq semantic-lex-end-point (point)))

(define-lex-regex-analyzer semantic-lex-c-string
  "Detect and create a C string token."
  "L?\\(\\s\"\\)"
  ;; Zing to the end of this string.
  (semantic-lex-push-token
   (semantic-lex-token
    'string (point)
    (save-excursion
      ;; Skip L prefix if present.
      (goto-char (match-beginning 1))
      (semantic-lex-unterminated-syntax-protection 'string
	(forward-sexp 1)
	(point))
      ))))

(define-lex-regex-analyzer semantic-c-lex-ignore-newline
  "Detect and ignore newline tokens.
Use this ONLY if newlines are not whitespace characters (such as when
they are comment end characters)."
  ;; Just like semantic-lex-ignore-newline, but also ignores
  ;; trailing \.
  "\\s-*\\\\?\\s-*\\(\n\\|\\s>\\)"
  (setq semantic-lex-end-point (match-end 0)))


(define-lex semantic-c-lexer
  "Lexical Analyzer for C code.
Use semantic-cpp-lexer for parsing text inside a CPP macro."
  ;; C preprocessor features
  semantic-lex-cpp-define
  semantic-lex-cpp-undef
Chong Yidong's avatar
Chong Yidong committed
759
  semantic-lex-c-ifdef
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
  semantic-lex-c-if
  semantic-lex-c-macro-else
  semantic-lex-c-macrobits
  semantic-lex-c-include
  semantic-lex-c-include-system
  semantic-lex-c-ignore-ending-backslash
  ;; Whitespace handling
  semantic-lex-ignore-whitespace
  semantic-c-lex-ignore-newline
  ;; Non-preprocessor features
  semantic-lex-number
  ;; Must detect C strings before symbols because of possible L prefix!
  semantic-lex-c-string
  ;; Custom handlers for some macros come before the macro replacement analyzer.
  semantic-lex-c-namespace-begin-macro
  semantic-lex-c-namespace-begin-nested-macro
  semantic-lex-c-namespace-end-macro
  semantic-lex-c-VC++-begin-std-namespace
  semantic-lex-c-VC++-end-std-namespace
  ;; Handle macros, symbols, and keywords
  semantic-lex-spp-replace-or-symbol-or-keyword
  semantic-lex-charquote
  semantic-lex-paren-or-list
  semantic-lex-close-paren
  semantic-lex-ignore-comments
  semantic-lex-punctuation
  semantic-lex-default-action)

(define-lex-simple-regex-analyzer semantic-lex-cpp-hashhash
  "Match ## inside a CPP macro as special."
  "##" 'spp-concat)

(define-lex semantic-cpp-lexer
  "Lexical Analyzer for CPP macros in C code."
  ;; CPP special
  semantic-lex-cpp-hashhash
  ;; C preprocessor features
  semantic-lex-cpp-define
  semantic-lex-cpp-undef
  semantic-lex-c-if
  semantic-lex-c-macro-else
  semantic-lex-c-macrobits
  semantic-lex-c-include
  semantic-lex-c-include-system
  semantic-lex-c-ignore-ending-backslash
  ;; Whitespace handling
  semantic-lex-ignore-whitespace
  semantic-c-lex-ignore-newline
  ;; Non-preprocessor features
  semantic-lex-number
  ;; Must detect C strings before symbols because of possible L prefix!
  semantic-lex-c-string
  ;; Parsing inside a macro means that we don't do macro replacement.
  ;; semantic-lex-spp-replace-or-symbol-or-keyword
  semantic-lex-symbol-or-keyword
  semantic-lex-charquote
David Engster's avatar
David Engster committed
816
  semantic-lex-spp-paren-or-list
817 818 819 820 821 822 823
  semantic-lex-close-paren
  semantic-lex-ignore-comments
  semantic-lex-punctuation
  semantic-lex-default-action)

(define-mode-local-override semantic-parse-region c-mode
  (start end &optional nonterminal depth returnonerror)
824
  "Calls `semantic-parse-region-default', except in a macro expansion.
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
MACRO expansion mode is handled through the nature of Emacs's non-lexical
binding of variables.
START, END, NONTERMINAL, DEPTH, and RETURNONERRORS are the same
as for the parent."
  (if (and (boundp 'lse) (or (/= start 1) (/= end (point-max))))
      (let* ((last-lexical-token lse)
	     (llt-class (semantic-lex-token-class last-lexical-token))
	     (llt-fakebits (car (cdr last-lexical-token)))
	     (macroexpand (stringp (car (cdr last-lexical-token)))))
	(if macroexpand
  	    (progn
	      ;; It is a macro expansion.  Do something special.
	      ;;(message "MOOSE %S %S, %S : %S" start end nonterminal lse)
	      (semantic-c-parse-lexical-token
	       lse nonterminal depth returnonerror)
	      )
	  ;; Not a macro expansion, but perhaps a funny semantic-list
	  ;; is at the start?  Remove the depth if our semantic list is not
	  ;; made of list tokens.
	  (if (and depth (= depth 1)
		   (eq llt-class 'semantic-list)
		   (not (null llt-fakebits))
		   (consp llt-fakebits)
		   (symbolp (car llt-fakebits))
		   )
	      (progn
		(setq depth 0)

		;; This is a copy of semantic-parse-region-default where we
Paul Eggert's avatar
Paul Eggert committed
854
		;; are doing something special with the lexing of the
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
		;; contents of the semantic-list token.  Stuff not used by C
		;; removed.
		(let ((tokstream
		       (if (and (consp llt-fakebits)
				(eq (car llt-fakebits) 'prefix-fake-plus))
			   ;; If our semantic-list is special, then only stick in the
			   ;; fake tokens.
			   (cdr llt-fakebits)
			 ;; Lex up the region with a depth of 0
			 (semantic-lex start end 0))))

		  ;; Do the parse
		  (nreverse
		   (semantic-repeat-parse-whole-stream tokstream
						       nonterminal
						       returnonerror))

		  ))

	    ;; It was not a macro expansion, nor a special semantic-list.
	    ;; Do old thing.
	    (semantic-parse-region-default start end
					   nonterminal depth
					   returnonerror)
	    )))
    ;; Do the parse
    (semantic-parse-region-default start end nonterminal
				   depth returnonerror)
    ))

885
(defvar semantic-c-parse-token-hack-depth 0
Glenn Morris's avatar
Glenn Morris committed
886
  "Current depth of recursive calls to `semantic-c-parse-lexical-token'.")
887

888 889 890 891 892 893 894 895
(defun semantic-c-parse-lexical-token (lexicaltoken nonterminal depth
						    returnonerror)
  "Do a region parse on the contents of LEXICALTOKEN.
Presumably, this token has a string in it from a macro.
The text of the token is inserted into a different buffer, and
parsed there.
Argument NONTERMINAL, DEPTH, and RETURNONERROR are passed into
the regular parser."
896 897 898
  (let* ((semantic-c-parse-token-hack-depth (1+ semantic-c-parse-token-hack-depth))
	 (buf (get-buffer-create (format " *C parse hack %d*"
					 semantic-c-parse-token-hack-depth)))
899 900 901 902 903 904 905 906
	 (mode major-mode)
	 (spp-syms semantic-lex-spp-dynamic-macro-symbol-obarray)
	 (stream nil)
	 (start (semantic-lex-token-start lexicaltoken))
	 (end (semantic-lex-token-end lexicaltoken))
	 (symtext (semantic-lex-token-text lexicaltoken))
	 (macros (get-text-property 0 'macros symtext))
	 )
Chong Yidong's avatar
Chong Yidong committed
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
    (if (> semantic-c-parse-token-hack-depth 5)
	nil
      (with-current-buffer buf
	(erase-buffer)
	(when (not (eq major-mode mode))
	  (save-match-data

	    ;; Protect against user hooks throwing errors.
	    (condition-case nil
		(funcall mode)
	      (error
	       (if (y-or-n-p
		    (format "There was an error initializing %s in buffer \"%s\".  Debug your hooks? "
			    mode (buffer-name)))
		   (semantic-c-debug-mode-init mode)
		 (message "Macro parsing state may be broken...")
		 (sit-for 1))))
	    )				; save match data

	  ;; Hack in mode-local
	  (activate-mode-local-bindings)
Chong Yidong's avatar
Chong Yidong committed
928 929
	  ;; Setup C parser
	  (semantic-default-c-setup)
Chong Yidong's avatar
Chong Yidong committed
930 931 932 933 934 935
	  ;; CHEATER!  The following 3 lines are from
	  ;; `semantic-new-buffer-fcn', but we don't want to turn
	  ;; on all the other annoying modes for this little task.
	  (setq semantic-new-buffer-fcn-was-run t)
	  (semantic-lex-init)
	  (semantic-clear-toplevel-cache)
936 937
	  (remove-hook 'semantic-lex-reset-functions
		       'semantic-lex-spp-reset-hook t)
Chong Yidong's avatar
Chong Yidong committed
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
	  )
	;; Get the macro symbol table right.
	(setq semantic-lex-spp-dynamic-macro-symbol-obarray spp-syms)
	;; (message "%S" macros)
	(dolist (sym macros)
	  (semantic-lex-spp-symbol-set (car sym) (cdr sym)))

	(insert symtext)

	(setq stream
	      (semantic-parse-region-default
	       (point-min) (point-max) nonterminal depth returnonerror))

	;; Clean up macro symbols
	(dolist (sym macros)
	  (semantic-lex-spp-symbol-remove (car sym)))

	;; Convert the text of the stream.
	(dolist (tag stream)
	  ;; Only do two levels here 'cause I'm lazy.
	  (semantic--tag-set-overlay tag (list start end))
	  (dolist (stag (semantic-tag-components-with-overlays tag))
	    (semantic--tag-set-overlay stag (list start end))
	    ))
	))
963 964
    stream))

965 966 967 968 969 970
(defvar semantic-c-debug-mode-init-last-mode nil
  "The most recent mode needing debugging.")

(defun semantic-c-debug-mode-init (mm)
  "Debug mode init for major mode MM after we're done parsing now."
  (interactive (list semantic-c-debug-mode-init-last-mode))
971
  (if (called-interactively-p 'interactive)
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
      ;; Do the debug.
      (progn
	(switch-to-buffer (get-buffer-create "*MODE HACK TEST*"))
	(let ((debug-on-error t))
	  (funcall mm)))

    ;; Notify about the debug
    (setq semantic-c-debug-mode-init-last-mode mm)

    (add-hook 'post-command-hook 'semantic-c-debug-mode-init-pch)))

(defun semantic-c-debug-mode-init-pch ()
  "Notify user about needing to debug their major mode hooks."
  (let ((mm semantic-c-debug-mode-init-last-mode))
    (switch-to-buffer-other-window
     (get-buffer-create "*MODE HACK TEST*"))
    (erase-buffer)
989
    (insert "A failure occurred while parsing your buffers.
990

991
The failure occurred while attempting to initialize " (symbol-name mm) " in a
992 993 994 995 996 997 998 999
buffer not associated with a file.  To debug this problem, type

M-x semantic-c-debug-mode-init

now.
")
    (remove-hook 'post-command-hook 'semantic-c-debug-mode-init-pch)))

1000 1001 1002 1003 1004 1005
(defun semantic-expand-c-tag (tag)
  "Expand TAG into a list of equivalent tags, or nil."
  (let ((return-list nil)
	)
    ;; Expand an EXTERN C first.
    (when (eq (semantic-tag-class tag) 'extern)
Chong Yidong's avatar
Chong Yidong committed
1006 1007 1008 1009
      (setq return-list (semantic-expand-c-extern-C tag))
      ;; The members will be expanded in the next iteration. The
      ;; 'extern' tag itself isn't needed anymore.
      (setq tag nil))
1010

Chong Yidong's avatar
Chong Yidong committed
1011
    ;; Check if we have a complex type
1012 1013
    (when (or (semantic-tag-of-class-p tag 'function)
	      (semantic-tag-of-class-p tag 'variable))
Chong Yidong's avatar
Chong Yidong committed
1014 1015 1016 1017
      (setq tag (semantic-expand-c-complex-type tag))
      ;; Extract new basetag
      (setq return-list (car tag))
      (setq tag (cdr tag)))
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037

    ;; Name of the tag is a list, so expand it.  Tag lists occur
    ;; for variables like this: int var1, var2, var3;
    ;;
    ;; This will expand that to 3 tags that happen to share the
    ;; same overlay information.
    (if (consp (semantic-tag-name tag))
	(let ((rl (semantic-expand-c-tag-namelist tag)))
	  (cond
	   ;; If this returns nothing, then return nil overall
	   ;; because that will restore the old TAG input.
	   ((not rl) (setq return-list nil))
	   ;; If we have a return, append it to the existing list
	   ;; of returns.
	   ((consp rl)
	    (setq return-list (append rl return-list)))
	   ))
      ;; If we didn't have a list, but the return-list is non-empty,
      ;; that means we still need to take our existing tag, and glom
      ;; it onto our extracted type.
Chong Yidong's avatar
Chong Yidong committed
1038
      (if (and tag (consp return-list))
1039 1040 1041 1042 1043 1044
	  (setq return-list (cons tag return-list)))
      )

    ;; Default, don't change the tag means returning nil.
    return-list))

Chong Yidong's avatar
Chong Yidong committed
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
(defun semantic-expand-c-extern-C (tag)
  "Expand TAG containing an 'extern \"C\"' statement.
This will return all members of TAG with 'extern \"C\"' added to
the typemodifiers attribute."
    (when (eq (semantic-tag-class tag) 'extern)
      (let* ((mb (semantic-tag-get-attribute tag :members))
	     (ret mb))
	(while mb
	  (let ((mods (semantic-tag-get-attribute (car mb) :typemodifiers)))
	    (setq mods (cons "extern" (cons "\"C\"" mods)))
	    (semantic-tag-put-attribute (car mb) :typemodifiers mods))
	  (setq mb (cdr mb)))
	(nreverse ret))))

(defun semantic-expand-c-complex-type (tag)
  "Check if TAG has a full :type with a name on its own.
If so, extract it, and replace it with a reference to that type.
Thus, 'struct A { int a; } B;' will create 2 toplevel tags, one
is type A, and the other variable B where the :type of B is just
a type tag A that is a prototype, and the actual struct info of A
is its own toplevel tag.  This function will return (cons A B)."
  (let* ((basetype (semantic-tag-type tag))
	 (typeref nil)
	 (ret nil)
	 (tname (when (consp basetype)
		  (semantic-tag-name basetype))))
    ;; Make tname be a string.
    (when (consp tname) (setq tname (car (car tname))))
    ;; Is the basetype a full type with a name of its own?
    (when (and basetype (semantic-tag-p basetype)
	       (not (semantic-tag-prototype-p basetype))
	       tname
	       (not (string= tname "")))
      ;; a type tag referencing the type we are extracting.
      (setq typeref (semantic-tag-new-type
		     (semantic-tag-name basetype)
		     (semantic-tag-type basetype)
		     nil nil
		     :prototype t))
      ;; Convert original tag to only have a reference.
      (setq tag (semantic-tag-copy tag))
      (semantic-tag-put-attribute tag :type typeref)
      ;; Convert basetype to have the location information.
      (semantic--tag-copy-properties tag basetype)
      (semantic--tag-set-overlay basetype
				 (semantic-tag-overlay tag))
      ;; Store the base tag as part of the return list.
      (setq ret (cons basetype ret)))
    (cons ret tag)))

1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
(defun semantic-expand-c-tag-namelist (tag)
  "Expand TAG whose name is a list into a list of tags, or nil."
  (cond ((semantic-tag-of-class-p tag 'variable)
	 ;; The name part comes back in the form of:
	 ;; ( NAME NUMSTARS BITS ARRAY ASSIGN )
	 (let ((vl nil)
	       (basety (semantic-tag-type tag))
	       (ty "")
	       (mods (semantic-tag-get-attribute tag :typemodifiers))
	       (suffix "")
	       (lst (semantic-tag-name tag))
	       (default nil)
	       (cur nil))
	   ;; Open up each name in the name list.
	   (while lst
	     (setq suffix "" ty "")
	     (setq cur (car lst))
	     (if (nth 2 cur)
		 (setq suffix (concat ":" (nth 2 cur))))
	     (if (= (length basety) 1)
		 (setq ty (car basety))
	       (setq ty basety))
	     (setq default (nth 4 cur))
	     (setq vl (cons
		       (semantic-tag-new-variable
			(car cur)	;name
			ty		;type
David Engster's avatar
David Engster committed
1122 1123
			(if (and default
				 (listp (cdr default)))
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
			    (buffer-substring-no-properties
			     (car default) (car (cdr default))))
			:constant-flag (semantic-tag-variable-constant-p tag)
			:suffix suffix
			:typemodifiers mods
			:dereference (length (nth 3 cur))
			:pointer (nth 1 cur)
			:reference (semantic-tag-get-attribute tag :reference)
			:documentation (semantic-tag-docstring tag) ;doc
			)
		       vl))
	     (semantic--tag-copy-properties tag (car vl))
	     (semantic--tag-set-overlay (car vl)
					(semantic-tag-overlay tag))
	     (setq lst (cdr lst)))
	   ;; Return the list
	   (nreverse vl)))
	((semantic-tag-of-class-p tag 'type)
	 ;; We may someday want to add an extra check for a type
	 ;; of type "typedef".
	 ;; Each elt of NAME is ( STARS NAME )
	 (let ((vl nil)
Chong Yidong's avatar
Chong Yidong committed
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
	       (names (semantic-tag-name tag))
	       (super (semantic-tag-get-attribute tag :superclasses))
	       (addlast nil))

	   (when (and (semantic-tag-of-type-p tag "typedef")
		      (semantic-tag-of-class-p super 'type)
		      (semantic-tag-type-members super))
	     ;; This is a typedef of a real type.  Extract
	     ;; the super class, and stick it into the tags list.
	     (setq addlast super)

	     ;; Clone super and remove the members IFF super has a name.
	     ;; Note: anonymous struct/enums that are typedef'd shouldn't
	     ;; exist in the top level type list, so they will appear only
	     ;; in the :typedef slot of the typedef.
	     (setq super (semantic-tag-clone super))
	     (if (not (string= (semantic-tag-name super) ""))
		 (semantic-tag-put-attribute super :members nil)
	       (setq addlast nil))

	     ;; Add in props to the full superclass.
	     (when addlast
	       (semantic--tag-copy-properties tag addlast)
	       (semantic--tag-set-overlay addlast (semantic-tag-overlay tag)))
	     )

1172
	   (while names
Chong Yidong's avatar
Chong Yidong committed
1173

1174 1175 1176 1177
	     (setq vl (cons (semantic-tag-new-type
			     (nth 1 (car names)) ; name
			     "typedef"
			     (semantic-tag-type-members tag)
David Engster's avatar
David Engster committed
1178
			     nil
1179 1180 1181 1182 1183 1184 1185
			     :pointer
			     (let ((stars (car (car (car names)))))
			       (if (= stars 0) nil stars))
			     ;; This specifies what the typedef
			     ;; is expanded out as.  Just the
			     ;; name shows up as a parent of this
			     ;; typedef.
Chong Yidong's avatar
Chong Yidong committed
1186
			     :typedef super
1187 1188 1189 1190 1191
			     ;;(semantic-tag-type-superclasses tag)
			     :documentation
			     (semantic-tag-docstring tag))
			    vl))
	     (semantic--tag-copy-properties tag (car vl))
Chong Yidong's avatar
Chong Yidong committed
1192
	     (semantic--tag-set-overlay (car vl) (semantic-tag-overlay tag))
1193
	     (setq names (cdr names)))
Chong Yidong's avatar
Chong Yidong committed
1194 1195 1196 1197

	   ;; Add typedef superclass last.
	   (when addlast (setq vl (cons addlast vl)))

1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
	   vl))
	((and (listp (car tag))
	      (semantic-tag-of-class-p (car tag) 'variable))
	 ;; Argument lists come in this way.  Append all the expansions!
	 (let ((vl nil))
	   (while tag
	     (setq vl (append (semantic-tag-components (car vl))
			      vl)
		   tag (cdr tag)))
	   vl))
	(t nil)))

(defvar-mode-local c-mode semantic-tag-expand-function 'semantic-expand-c-tag
  "Function used to expand tags generated in the C bovine parser.")

(defvar semantic-c-classname nil
  "At parse time, assign a class or struct name text here.
It is picked up by `semantic-c-reconstitute-token' to determine
if something is a constructor.  Value should be:
1217
  (TYPENAME .  TYPEOFTYPE)
1218 1219 1220
where typename is the name of the type, and typeoftype is \"class\"
or \"struct\".")

1221 1222 1223 1224 1225 1226 1227
(define-mode-local-override semantic-analyze-split-name c-mode (name)
  "Split up tag names on colon (:) boundaries."
  (let ((ans (split-string name ":")))
    (if (= (length ans) 1)
	name
      (delete "" ans))))

David Engster's avatar
David Engster committed
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
(define-mode-local-override semantic-analyze-tag-references c-mode (tag &optional db)
  "Analyze the references for TAG.
Returns a class with information about TAG.

Optional argument DB is a database.  It will be used to help
locate TAG.

Use `semantic-analyze-current-tag' to debug this fcn."
  (when (not (semantic-tag-p tag))  (signal 'wrong-type-argument (list 'semantic-tag-p tag)))
  (let ((allhits nil)
	(scope nil)
	(refs nil))
    (save-excursion
      (semantic-go-to-tag tag db)
      (setq scope (semantic-calculate-scope))

      (setq allhits (semantic--analyze-refs-full-lookup tag scope t))
      
      (when (or (zerop (semanticdb-find-result-length allhits))
		(and (= (semanticdb-find-result-length allhits) 1)
		     (eq (car (semanticdb-find-result-nth allhits 0)) tag)))
	;; It found nothing or only itself - not good enough.  As a
	;; last resort, let's remove all namespaces from the scope and
	;; search again.
	(oset scope parents
	      (let ((parents (oref scope parents))
		    newparents)
		(dolist (cur parents)
		  (unless (string= (semantic-tag-type cur) "namespace")
		    (push cur newparents)))
		(reverse newparents)))
	(setq allhits (semantic--analyze-refs-full-lookup tag scope t)))

      (setq refs (semantic-analyze-references (semantic-tag-name tag)
				    :tag tag
				    :tagdb db
				    :scope scope
				    :rawsearchdata allhits)))))

1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
(defun semantic-c-reconstitute-token (tokenpart declmods typedecl)
  "Reconstitute a token TOKENPART with DECLMODS and TYPEDECL.
This is so we don't have to match the same starting text several times.
Optional argument STAR and REF indicate the number of * and & in the typedef."
  (when (and (listp typedecl)
	     (= 1 (length typedecl))
	     (stringp (car typedecl)))
    (setq typedecl (car typedecl)))
  (cond ((eq (nth 1 tokenpart) 'variable)
	 (semantic-tag-new-variable
	  (car tokenpart)
	  (or typedecl "int")	;type
	  nil			;default value (filled with expand)
	  :constant-flag (if (member "const" declmods) t nil)
	  :typemodifiers (delete "const" declmods)
	  )
	 )
	((eq (nth 1 tokenpart) 'function)
	 ;; We should look at part 4 (the arglist) here, and throw an
	 ;; error of some sort if it contains parser errors so that we
	 ;; don't parser function calls, but that is a little beyond what
	 ;; is available for data here.
	 (let* ((constructor
		 (and (or (and semantic-c-classname
			       (string= (car semantic-c-classname)
					(car tokenpart)))
			  (and (stringp (car (nth 2 tokenpart)))
			       (string= (car (nth 2 tokenpart)) (car tokenpart)))
Chong Yidong's avatar
Chong Yidong committed
1295
			  (nth 10 tokenpart) ; initializers
1296 1297
			  )
		      (not (car (nth 3 tokenpart)))))
David Engster's avatar
David Engster committed
1298 1299
		(fcnpointer (and (> (length (car tokenpart)) 0)
				 (= (aref (car tokenpart) 0) ?*)))
1300 1301 1302 1303 1304 1305 1306
		(fnname (if fcnpointer
			    (substring (car tokenpart) 1)
			  (car tokenpart)))
		(operator (if (string-match "[a-zA-Z]" fnname)
			      nil
			    t))
		)
David Engster's avatar
David Engster committed
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
	   ;; The function
	   (semantic-tag-new-function
	    fnname
	    (or typedecl		;type
		(cond ((car (nth 3 tokenpart) )
		       "void")	; Destructors have no return?
		      (constructor
		       ;; Constructors return an object.
		       (semantic-tag-new-type
			;; name
			(or (car semantic-c-classname)
			    (let ((split (semantic-analyze-split-name-c-mode
					  (car (nth 2 tokenpart)))))
			      (if (stringp split) split
				(car (last split)))))
			;; type
			(or (cdr semantic-c-classname)
			    "class")
			;; members
			nil
			;; parents
			nil
			))
		      (t "int")))
	    ;; Argument list can contain things like function pointers
	    (semantic-c-reconstitute-function-arglist (nth 4 tokenpart))
	    :constant-flag (if (member "const" declmods) t nil)
	    :typemodifiers (delete "const" declmods)
	    :parent (car (nth 2 tokenpart))
	    :destructor-flag (if (car (nth 3 tokenpart) ) t)
	    :constructor-flag (if constructor t)
	    :function-pointer fcnpointer
	    :pointer (nth 7 tokenpart)
	    :operator-flag operator
	    ;; Even though it is "throw" in C++, we use
	    ;; `throws' as a common name for things that toss
	    ;; exceptions about.
	    :throws (nth 5 tokenpart)
	    ;; Reentrant is a C++ thingy.  Add it here
	    :reentrant-flag (if (member "reentrant" (nth 6 tokenpart)) t)
	    ;; A function post-const is funky.  Try stuff
	    :methodconst-flag (if (member "const" (nth 6 tokenpart)) t)
	    ;; prototypes are functions w/ no body
	    :prototype-flag (if (nth 8 tokenpart) t)
	    ;; Pure virtual
	    :pure-virtual-flag (if (eq (nth 8 tokenpart) :pure-virtual-flag) t)
	    ;; Template specifier.
	    :template-specifier (nth 9 tokenpart))))))
1355 1356 1357 1358 1359 1360

(defun semantic-c-reconstitute-template (tag specifier)
  "Reconstitute the token TAG with the template SPECIFIER."
  (semantic-tag-put-attribute tag :template (or specifier ""))
  tag)

David Engster's avatar
David Engster committed
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
(defun semantic-c-reconstitute-function-arglist (arglist)
  "Reconstitute the argument list of a function.
This currently only checks if the function expects a function
pointer as argument."
  (let (result)
    (dolist (arg arglist)
      ;; Names starting with a '*' denote a function pointer
      (if (and (> (length (semantic-tag-name arg)) 0)
	       (= (aref (semantic-tag-name arg) 0) ?*))
	  (setq result
		(append result
			(list
			 (semantic-tag-new-function
			  (substring (semantic-tag-name arg) 1)
			  (semantic-tag-type arg)
			  (cadr (semantic-tag-attributes arg))
			  :function-pointer t))))
	(setq result (append result (list arg)))))
    result))

1381 1382 1383 1384 1385 1386 1387 1388

;;; Override methods & Variables
;;
(define-mode-local-override semantic-format-tag-name
  c-mode (tag &optional parent color)
  "Convert TAG to a string that is the print name for TAG.
Optional PARENT and COLOR are ignored."
  (let ((name (semantic-format-tag-name-default tag parent color))
David Engster's avatar
David Engster committed
1389
	(fnptr (semantic-tag-get-attribute tag :function-pointer))
1390 1391 1392 1393 1394 1395 1396 1397
	)
    (if (not fnptr)
	name
      (concat "(*" name ")"))
    ))

(define-mode-local-override semantic-format-tag-canonical-name
  c-mode (tag &optional parent color)
Paul Eggert's avatar
Paul Eggert committed
1398
  "Create a canonical name for TAG.
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
PARENT specifies a parent class.
COLOR indicates that the text should be type colorized.
Enhances the base class to search for the entire parent
tree to make the name accurate."
  (semantic-format-tag-canonical-name-default tag parent color)
  )

(define-mode-local-override semantic-format-tag-type c-mode (tag color)
  "Convert the data type of TAG to a string usable in tag formatting.
Adds pointer and reference symbols to the default.
Argument COLOR adds color to the text."
  (let* ((type (semantic-tag-type tag))
	 (defaulttype nil)
	 (point (semantic-tag-get-attribute tag :pointer))
	 (ref (semantic-tag-get-attribute tag :reference))
	 )
    (if (semantic-tag-p type)
	(let ((typetype (semantic-tag-type type))
	      (typename (semantic-tag-name type)))
	  ;; Create the string that expresses the type
	  (if (string= typetype "class")
	      (setq defaulttype typename)
	    (setq defaulttype (concat typetype " " typename))))
      (setq defaulttype (semantic-format-tag-type-default tag color)))

    ;; Colorize
    (when color
      (setq defaulttype (semantic--format-colorize-text defaulttype 'type)))

    ;; Add refs, ptrs, etc
    (if ref (setq ref "&"))
    (if point (setq point (make-string point ?*)) "")
    (when type
      (concat defaulttype ref point))
    ))

(define-mode-local-override semantic-find-tags-by-scope-protection
  c-mode (scopeprotection parent &optional table)
  "Override the usual search for protection.
We can be more effective than the default by scanning through once,
and collecting tags based on the labels we see along the way."
  (if (not table) (setq table (semantic-tag-type-members parent)))
  (if (null scopeprotection)
      table
    (let ((ans nil)
	  (curprot 1)
	  (targetprot (cond ((eq scopeprotection 'public)
			     1)
			    ((eq scopeprotection 'protected)
			     2)
			    (t 3)
			    ))
	  (alist '(("public" . 1)
		   ("protected" . 2)
		   ("private" . 3)))
	  )
      (dolist (tag table)
	(cond
	 ((semantic-tag-of-class-p tag 'label)
	  (setq curprot (cdr (assoc (semantic-tag-name tag) alist)))
	  )
	 ((>= targetprot curprot)
	  (setq ans (cons tag ans)))
	 ))
      ans)))

(define-mode-local-override semantic-tag-protection
  c-mode (tag &optional parent)
  "Return the protection of TAG in PARENT.
Override function for `semantic-tag-protection'."
  (let ((mods (semantic-tag-modifiers tag))
	(prot nil))
    ;; Check the modifiers for protection if we are not a child
    ;; of some class type.
    (when (or (not parent) (not (eq (semantic-tag-class parent) 'type)))
      (while (and (not prot) mods)
	(if (stringp (car mods))
	    (let ((s (car mods)))
	      ;; A few silly defaults to get things started.
	      (cond ((or (string= s "extern")
			 (string= s "export"))
		     'public)
		    ((string= s "static")
		     'private))))
	(setq mods (cdr mods))))
    ;; If we have a typed parent, look for :public style labels.
    (when (and parent (eq (semantic-tag-class parent) 'type))
      (let ((pp (semantic-tag-type-members parent)))
	(while (and pp (not (semantic-equivalent-tag-p (car pp) tag)))
	  (when (eq (semantic-tag-class (car pp)) 'label)
	    (setq prot
		  (cond ((string= (semantic-tag-name (car pp)) "public")
			 'public)
			((string= (semantic-tag-name (car pp)) "private")
			 'private)
			((string= (semantic-tag-name (car pp)) "protected")
			 'protected)))
	    )
	  (setq pp (cdr pp)))))
    (when (and (not prot) (eq (semantic-tag-class parent) 'type))
      (setq prot
	    (cond ((string= (semantic-tag-type parent) "class") 'private)
		  ((string= (semantic-tag-type parent) "struct") 'public)
		  (t 'unknown))))
    (or prot
	(if (and parent (semantic-tag-of-class-p parent 'type))
	    'public
	  nil))))

Chong Yidong's avatar
Chong Yidong committed
1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
(define-mode-local-override semantic-find-tags-included c-mode
  (&optional table)
  "Find all tags in TABLE that are of the 'include class.
TABLE is a tag table.  See `semantic-something-to-tag-table'.
For C++, we also have to search namespaces for include tags."
  (let ((tags (semantic-find-tags-by-class 'include table))
	(namespaces (semantic-find-tags-by-type "namespace" table)))
    (dolist (cur namespaces)
      (setq tags
	    (append tags
		    (semantic-find-tags-by-class
		     'include
		     (semantic-tag-get-attribute cur :members)))))
    tags))


1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
(define-mode-local-override semantic-tag-components c-mode (tag)
  "Return components for TAG."
  (if (and (eq (semantic-tag-class tag) 'type)
	   (string= (semantic-tag-type tag) "typedef"))
      ;; A typedef can contain a parent who has positional children,
      ;; but that parent will not have a position.  Do this funny hack
      ;; to make sure we can apply overlays properly.
      (let ((sc (semantic-tag-get-attribute tag :typedef)))
	(when (semantic-tag-p sc) (semantic-tag-components sc)))
    (semantic-tag-components-default tag)))

(defun semantic-c-tag-template (tag)
  "Return the template specification for TAG, or nil."
  (semantic-tag-get-attribute tag :template))

(defun semantic-c-tag-template-specifier (tag)
  "Return the template specifier specification for TAG, or nil."
  (semantic-tag-get-attribute tag :template-specifier))

(defun semantic-c-template-string-body (templatespec)
  "Convert TEMPLATESPEC into a string.
This might be a string, or a list of tokens."
  (cond ((stringp templatespec)
	 templatespec)
	((semantic-tag-p templatespec)
	 (semantic-format-tag-abbreviate templatespec))
	((listp templatespec)
	 (mapconcat 'semantic-format-tag-abbreviate templatespec ", "))))

(defun semantic-c-template-string (token &optional parent color)
  "Return a string representing the TEMPLATE attribute of TOKEN.
This string is prefixed with a space, or is the empty string.
Argument PARENT specifies a parent type.
Argument COLOR specifies that the string should be colorized."
  (let ((t2 (semantic-c-tag-template-specifier token))
	(t1 (semantic-c-tag-template token))
	;; @todo - Need to account for a parent that is a template
	(pt1 (if parent (semantic-c-tag-template parent)))
	(pt2 (if parent (semantic-c-tag-template-specifier parent)))
	)
    (cond (t2 ;; we have a template with specifier
	   (concat " <"
		   ;; Fill in the parts here
		   (semantic-c-template-string-body t2)
		   ">"))
	  (t1 ;; we have a template without specifier
	   " <>")
	  (t
	   ""))))

(define-mode-local-override semantic-format-tag-concise-prototype
  c-mode (token &optional parent color)
  "Return an abbreviated string describing TOKEN for C and C++.
Optional PARENT and COLOR as specified with
`semantic-format-tag-abbreviate-default'."
  ;; If we have special template things, append.
  (concat  (semantic-format-tag-concise-prototype-default token parent color)
	   (semantic-c-template-string token parent color)))

(define-mode-local-override semantic-format-tag-uml-prototype
  c-mode (token &optional parent color)
1585
  "Return an UML string describing TOKEN for C and C++.
1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596
Optional PARENT and COLOR as specified with
`semantic-abbreviate-tag-default'."
  ;; If we have special template things, append.
  (concat  (semantic-format-tag-uml-prototype-default token parent color)
	   (semantic-c-template-string token parent color)))

(define-mode-local-override semantic-tag-abstract-p
  c-mode (tag &optional parent)
  "Return non-nil if TAG is considered abstract.
PARENT is tag's parent.
In C, a method is abstract if it is `virtual', which is already
Juanma Barranquero's avatar
Juanma Barranquero committed
1597
handled.  A class is abstract iff its destructor is virtual."
1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
  (cond
   ((eq (semantic-tag-class tag) 'type)
    (require 'semantic/find)
    (or (semantic-brute-find-tag-by-attribute :pure-virtual-flag
					      (semantic-tag-components tag)
					      )
	(let* ((ds (semantic-brute-find-tag-by-attribute
		    :destructor-flag
		    (semantic-tag-components tag)
		    ))
	       (cs (semantic-brute-find-tag-by-attribute
		    :constructor-flag
		    (semantic-tag-components tag)
		    )))
	  (and ds (member "virtual" (semantic-tag-modifiers (car ds)))
	       cs (eq 'protected (semantic-tag-protection (car cs) tag))
	       )
	  )))
   ((eq (semantic-tag-class tag) 'function)
    (or (semantic-tag-get-attribute tag :pure-virtual-flag)
        (member "virtual" (semantic-tag-modifiers tag))))
   (t (semantic-tag-abstract-p-default tag parent))))

(defun semantic-c-dereference-typedef (type scope &optional type-declaration)
  "If TYPE is a typedef, get TYPE's type by name or tag, and return.
SCOPE is not used, and TYPE-DECLARATION is used only if TYPE is not a typedef."
  (if (and (eq (semantic-tag-class type) 'type)
           (string= (semantic-tag-type type) "typedef"))
      (let ((dt (semantic-tag-get-attribute type :typedef)))
        (cond ((and (semantic-tag-p dt)
Chong Yidong's avatar
Chong Yidong committed
1628
                    (not (semantic-tag-prototype-p dt)))
1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651
	       ;; In this case, DT was declared directly.  We need
	       ;; to clone DT and apply a filename to it.
	       (let* ((fname (semantic-tag-file-name type))
		      (def (semantic-tag-copy dt nil fname)))
		 (list def def)))
              ((stringp dt) (list dt (semantic-tag dt 'type)))
              ((consp dt) (list (car dt) dt))))

    (list type type-declaration)))

(defun semantic-c--instantiate-template (tag def-list spec-list)
  "Replace TAG name according to template specification.
DEF-LIST is the template information.
SPEC-LIST is the template specifier of the datatype instantiated."
  (when (and (car def-list) (car spec-list))

    (when (and (string= (semantic-tag-type (car def-list)) "class")
               (string= (semantic-tag-name tag) (semantic-tag-name (car def-list))))
      (semantic-tag-set-name tag (semantic-tag-name (car spec-list))))

    (semantic-c--instantiate-template tag (cdr def-list) (cdr spec-list))))

(defun semantic-c--template-name-1 (spec-list)
Glenn Morris's avatar
Glenn Morris committed
1652 1653
  "Return a string used to compute template class name.
Based on SPEC-LIST, for ref<Foo,Bar> it will return 'Foo,Bar'."
1654 1655 1656 1657 1658 1659 1660 1661 1662