Commit | Line | Data |
---|---|---|
4feec2f5 CY |
1 | ;;; semantic/bovine/c.el --- Semantic details for C |
2 | ||
9bf6c65c | 3 | ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, |
49f70d46 | 4 | ;; 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc. |
4feec2f5 CY |
5 | |
6 | ;; Author: Eric M. Ludlam <zappo@gnu.org> | |
7 | ||
8 | ;; This file is part of GNU Emacs. | |
9 | ||
10 | ;; GNU Emacs is free software: you can redistribute it and/or modify | |
11 | ;; it under the terms of the GNU General Public License as published by | |
12 | ;; the Free Software Foundation, either version 3 of the License, or | |
13 | ;; (at your option) any later version. | |
14 | ||
15 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | ;; GNU General Public License for more details. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. | |
22 | ||
23 | ;;; Commentary: | |
24 | ;; | |
25 | ;; Support for the C/C++ bovine parser for Semantic. | |
26 | ;; | |
27 | ;; @todo - can I support c++-font-lock-extra-types ? | |
28 | ||
29 | (require 'semantic) | |
30 | (require 'semantic/analyze) | |
31 | (require 'semantic/bovine/gcc) | |
4feec2f5 CY |
32 | (require 'semantic/idle) |
33 | (require 'semantic/lex-spp) | |
4feec2f5 CY |
34 | (require 'semantic/bovine/c-by) |
35 | ||
36 | (eval-when-compile | |
4feec2f5 CY |
37 | (require 'semantic/find)) |
38 | ||
39 | (declare-function semantic-brute-find-tag-by-attribute "semantic/find") | |
40 | (declare-function semanticdb-minor-mode-p "semantic/db-mode") | |
4feec2f5 | 41 | (declare-function semanticdb-needs-refresh-p "semantic/db") |
dd9af436 | 42 | (declare-function semanticdb-typecache-faux-namespace "semantic/db-typecache") |
4feec2f5 | 43 | (declare-function c-forward-conditional "cc-cmds") |
1fe1547a | 44 | (declare-function ede-system-include-path "ede") |
4feec2f5 CY |
45 | |
46 | ;;; Compatibility | |
47 | ;; | |
48 | (eval-when-compile (require 'cc-mode)) | |
49 | ||
50 | (if (fboundp 'c-end-of-macro) | |
51 | (eval-and-compile | |
52 | (defalias 'semantic-c-end-of-macro 'c-end-of-macro)) | |
53 | ;; From cc-mode 5.30 | |
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. | |
58 | ||
59 | This function does not do any hidden buffer changes." | |
60 | (while (progn | |
61 | (end-of-line) | |
62 | (when (and (eq (char-before) ?\\) | |
63 | (not (eobp))) | |
64 | (forward-char) | |
65 | t)))) | |
66 | ) | |
67 | ||
68 | ;;; Code: | |
69 | (define-child-mode c++-mode c-mode | |
70 | "`c++-mode' uses the same parser as `c-mode'.") | |
71 | ||
72 | \f | |
73 | ;;; Include Paths | |
74 | ;; | |
75 | (defcustom-mode-local-semantic-dependency-system-include-path | |
76 | c-mode semantic-c-dependency-system-include-path | |
77 | '("/usr/include") | |
91abaf51 | 78 | "The system include path used by the C language.") |
4feec2f5 CY |
79 | |
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." | |
84 | :group 'c | |
85 | :group 'semantic | |
86 | :type '(repeat (string :tag "Path"))) | |
87 | ||
88 | (defvar-mode-local c-mode semantic-dependency-include-path | |
89 | semantic-default-c-path | |
90 | "System path to search for include files.") | |
91 | ||
92 | ;;; Compile Options | |
93 | ;; | |
94 | ;; Compiler options need to show up after path setup, but before | |
95 | ;; the preprocessor section. | |
96 | ||
97 | (when (member system-type '(gnu gnu/linux darwin cygwin)) | |
98 | (semantic-gcc-setup)) | |
99 | ||
100 | ;;; Pre-processor maps | |
101 | ;; | |
102 | ;;; Lexical analysis | |
103 | (defvar semantic-lex-c-preprocessor-symbol-map-builtin | |
104 | '( ("__THROW" . "") | |
105 | ("__const" . "const") | |
106 | ("__restrict" . "") | |
107 | ("__declspec" . ((spp-arg-list ("foo") 1 . 2))) | |
108 | ("__attribute__" . ((spp-arg-list ("foo") 1 . 2))) | |
109 | ) | |
110 | "List of symbols to include by default.") | |
111 | ||
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.") | |
116 | ||
117 | (defvar semantic-lex-c-preprocessor-symbol-file) | |
118 | (defvar semantic-lex-c-preprocessor-symbol-map) | |
119 | ||
120 | (defun semantic-c-reset-preprocessor-symbol-map () | |
121 | "Reset the C preprocessor symbol map based on all input variables." | |
a60f2e7b | 122 | (when (featurep 'semantic/bovine/c) |
4feec2f5 CY |
123 | (let ((filemap nil) |
124 | ) | |
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) | |
132 | ) | |
133 | (dolist (sf semantic-lex-c-preprocessor-symbol-file) | |
134 | ;; Global map entries | |
135 | (let* ((table (semanticdb-file-table-object sf t))) | |
136 | (when table | |
137 | (when (semanticdb-needs-refresh-p table) | |
138 | (condition-case nil | |
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))) | |
145 | ) | |
146 | )))) | |
147 | ||
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 | |
153 | filemap)) | |
154 | ) | |
155 | ))) | |
156 | ||
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, | |
dd9af436 | 162 | and REPLACEMENT is a string that is inserted in its place. Empty string |
4feec2f5 CY |
163 | implies that the lexical analyzer will discard KEYWORD when it is encountered. |
164 | ||
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 : | |
9bf6c65c | 172 | is a separate lexical symbol. |
4feec2f5 CY |
173 | |
174 | A quick way to see what you would need to insert is to place a | |
175 | definition such as: | |
176 | ||
177 | #define MYSYM foo::bar | |
178 | ||
179 | into a C file, and do this: | |
180 | \\[semantic-lex-spp-describe] | |
181 | ||
182 | The output table will describe the symbols needed." | |
183 | :group 'c | |
184 | :type '(repeat (cons (string :tag "Keyword") | |
185 | (sexp :tag "Replacement"))) | |
186 | :set (lambda (sym value) | |
187 | (set-default sym value) | |
188 | (condition-case nil | |
189 | (semantic-c-reset-preprocessor-symbol-map) | |
190 | (error nil)) | |
191 | ) | |
192 | ) | |
193 | ||
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." | |
200 | :group 'c | |
201 | :type '(repeat (file :tag "File")) | |
202 | :set (lambda (sym value) | |
203 | (set-default sym value) | |
204 | (condition-case nil | |
205 | (semantic-c-reset-preprocessor-symbol-map) | |
206 | (error nil)) | |
207 | ) | |
208 | ) | |
209 | ||
210 | (defcustom semantic-c-member-of-autocast 't | |
91abaf51 | 211 | "Non-nil means classes with a '->' operator will cast to its return type. |
4feec2f5 CY |
212 | |
213 | For Examples: | |
214 | ||
215 | class Foo { | |
216 | Bar *operator->(); | |
217 | } | |
218 | ||
219 | Foo foo; | |
220 | ||
221 | if `semantic-c-member-of-autocast' is non-nil : | |
222 | foo->[here completion will list method of Bar] | |
223 | ||
224 | if `semantic-c-member-of-autocast' is nil : | |
225 | foo->[here completion will list method of Foo]" | |
226 | :group 'c | |
227 | :type 'boolean) | |
228 | ||
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. | |
a30e71ae | 232 | Return the defined symbol as a special spp lex token." |
4feec2f5 CY |
233 | "^\\s-*#\\s-*define\\s-+\\(\\(\\sw\\|\\s_\\)+\\)" 1 |
234 | (goto-char (match-end 0)) | |
235 | (skip-chars-forward " \t") | |
236 | (if (eolp) | |
237 | nil | |
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)) | |
242 | (looking-at "("))) | |
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) | |
247 | (raw-stream | |
248 | (semantic-lex-spp-stream-for-macro (save-excursion | |
249 | (semantic-c-end-of-macro) | |
250 | (point)))) | |
251 | ) | |
252 | ||
253 | ;; Only do argument checking if the paren was immediatly after | |
254 | ;; the macro name. | |
255 | (if with-args | |
256 | (semantic-lex-spp-first-token-arg-list (car raw-stream))) | |
257 | ||
258 | ;; Magical spp variable for end point. | |
259 | (setq semantic-lex-end-point (point)) | |
260 | ||
261 | ;; Handled nested macro streams. | |
262 | (semantic-lex-spp-merge-streams raw-stream) | |
263 | ))) | |
264 | ||
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. | |
a30e71ae | 268 | Return the defined symbol as a special spp lex token." |
4feec2f5 CY |
269 | "^\\s-*#\\s-*undef\\s-+\\(\\(\\sw\\|\\s_\\)+\\)" 1) |
270 | ||
271 | \f | |
272 | ;;; Conditional Skipping | |
273 | ;; | |
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." | |
278 | :group 'c | |
279 | :type 'boolean) | |
280 | ||
281 | (defun semantic-c-skip-conditional-section () | |
282 | "Skip one section of a conditional. | |
283 | Moves forward to a matching #elif, #else, or #endif. | |
29e1a603 | 284 | Moves completely over balanced #if blocks." |
4feec2f5 CY |
285 | (require 'cc-cmds) |
286 | (let ((done nil)) | |
287 | ;; (if (looking-at "^\\s-*#if") | |
288 | ;; (semantic-lex-spp-push-if (point)) | |
289 | (end-of-line) | |
290 | (while (and semantic-c-obey-conditional-section-parsing-flag | |
291 | (and (not done) | |
292 | (re-search-forward | |
293 | "^\\s-*#\\s-*\\(if\\(n?def\\)?\\|el\\(if\\|se\\)\\|endif\\)\\>" | |
294 | nil t))) | |
295 | (goto-char (match-beginning 0)) | |
296 | (cond | |
297 | ((looking-at "^\\s-*#\\s-*if") | |
298 | ;; We found a nested if. Skip it. | |
dd9af436 | 299 | ;; @TODO - can we use the new c-scan-conditionals |
4feec2f5 CY |
300 | (c-forward-conditional 1)) |
301 | ((looking-at "^\\s-*#\\s-*elif") | |
302 | ;; We need to let the preprocessor analize this one. | |
303 | (beginning-of-line) | |
304 | (setq done t) | |
305 | ) | |
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 | |
311 | ;; like the endif. | |
312 | (end-of-line) | |
313 | (forward-char 1) | |
314 | (setq done t)) | |
315 | (t | |
316 | ;; We found an elif. Stop here. | |
317 | (setq done t)))))) | |
318 | ||
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)) | |
324 | ||
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) | |
338 | )) | |
339 | (ifndef (or (string= ift "ifndef") | |
340 | (and (string= ift "if") notdefined) | |
341 | (and (string= ift "elif") notdefined) | |
342 | )) | |
343 | ) | |
344 | (if (or (and (or (string= ift "if") (string= ift "elif")) | |
345 | (string= sym "0")) | |
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 | |
349 | (let ((pt nil)) | |
350 | ;; (message "%s %s yes" ift sym) | |
351 | (beginning-of-line) | |
352 | (setq pt (point)) | |
4feec2f5 CY |
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) | |
359 | pt (point)) | |
dd9af436 CY |
360 | ;; (semantic-lex-push-token |
361 | ;; (semantic-lex-token 'c-preprocessor-skip pt (point))) | |
4feec2f5 CY |
362 | nil) |
363 | ;; Else, don't ignore it, but do handle the internals. | |
364 | ;;(message "%s %s no" ift sym) | |
365 | (end-of-line) | |
366 | (setq semantic-lex-end-point (point)) | |
367 | nil))) | |
368 | ||
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\\)" | |
375 | (let ((pt (point))) | |
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))) | |
381 | nil)) | |
382 | ||
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)) | |
388 | nil) | |
389 | ||
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) | |
397 | (match-end 1)) | |
398 | 'system)) | |
399 | ||
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) | |
407 | (match-end 1)) | |
408 | nil)) | |
409 | ||
410 | ||
411 | (define-lex-regex-analyzer semantic-lex-c-ignore-ending-backslash | |
412 | "Skip backslash ending a line. | |
413 | Go to the next line." | |
414 | "\\\\\\s-*\n" | |
415 | (setq semantic-lex-end-point (match-end 0))) | |
416 | ||
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")) | |
427 | ;; Push the name. | |
428 | (semantic-lex-push-token | |
429 | (semantic-lex-token 'symbol sym-start sym-end ms)) | |
430 | ) | |
431 | (goto-char (match-end 0)) | |
432 | (let ((start (point)) | |
433 | (end 0)) | |
434 | ;; If we can't find a matching end, then create the fake list. | |
435 | (when (re-search-forward "_GLIBCXX_END_NAMESPACE" nil t) | |
436 | (setq end (point)) | |
437 | (semantic-lex-push-token | |
438 | (semantic-lex-token 'semantic-list start end | |
439 | (list 'prefix-fake))))) | |
440 | (setq semantic-lex-end-point (point))) | |
441 | ||
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. | |
446 | ||
447 | If you are having problems with smart completion and STL templates, | |
91abaf51 | 448 | it may be that this is set incorrectly. After changing the value |
4feec2f5 CY |
449 | of this flag, you will need to delete any semanticdb cache files |
450 | that may have been incorrectly parsed." | |
451 | :group 'semantic | |
452 | :type 'boolean) | |
453 | ||
454 | (define-lex-regex-analyzer semantic-lex-c-VC++-begin-std-namespace | |
455 | "Handle VC++'s definition of the std namespace." | |
456 | "\\(_STD_BEGIN\\)" | |
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)) | |
463 | (end 0)) | |
464 | (when (re-search-forward "_STD_END" nil t) | |
465 | (setq end (point)) | |
466 | (semantic-lex-push-token | |
467 | (semantic-lex-token 'semantic-list start end | |
468 | (list 'prefix-fake))))) | |
469 | (setq semantic-lex-end-point (point))) | |
470 | ||
471 | (define-lex-regex-analyzer semantic-lex-c-VC++-end-std-namespace | |
472 | "Handle VC++'s definition of the std namespace." | |
473 | "\\(_STD_END\\)" | |
474 | (goto-char (match-end 0)) | |
475 | (setq semantic-lex-end-point (point))) | |
476 | ||
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")) | |
491 | ;; Push the name. | |
492 | (semantic-lex-push-token | |
493 | (semantic-lex-token 'symbol sym-start sym-end ms)) | |
494 | ||
495 | (goto-char (match-end 0)) | |
496 | (let ((start (point)) | |
497 | (end 0)) | |
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) | |
500 | (setq end (point)) | |
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 | |
512 | sym-end sym2-start | |
513 | "namespace") | |
514 | (semantic-lex-token 'symbol | |
515 | sym2-start sym2-end | |
516 | ms2) | |
517 | (semantic-lex-token 'semantic-list start end | |
518 | (list 'prefix-fake))) | |
519 | ))) | |
520 | ))) | |
521 | (setq semantic-lex-end-point (point))) | |
522 | ||
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))) | |
528 | ||
529 | (define-lex-regex-analyzer semantic-lex-c-string | |
530 | "Detect and create a C string token." | |
531 | "L?\\(\\s\"\\)" | |
532 | ;; Zing to the end of this string. | |
533 | (semantic-lex-push-token | |
534 | (semantic-lex-token | |
535 | 'string (point) | |
536 | (save-excursion | |
537 | ;; Skip L prefix if present. | |
538 | (goto-char (match-beginning 1)) | |
539 | (semantic-lex-unterminated-syntax-protection 'string | |
540 | (forward-sexp 1) | |
541 | (point)) | |
542 | )))) | |
543 | ||
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 | |
549 | ;; trailing \. | |
550 | "\\s-*\\\\?\\s-*\\(\n\\|\\s>\\)" | |
551 | (setq semantic-lex-end-point (match-end 0))) | |
552 | ||
553 | ||
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 | |
560 | semantic-lex-c-if | |
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 | |
570 | semantic-lex-number | |
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) | |
587 | ||
588 | (define-lex-simple-regex-analyzer semantic-lex-cpp-hashhash | |
589 | "Match ## inside a CPP macro as special." | |
590 | "##" 'spp-concat) | |
591 | ||
592 | (define-lex semantic-cpp-lexer | |
593 | "Lexical Analyzer for CPP macros in C code." | |
594 | ;; CPP special | |
595 | semantic-lex-cpp-hashhash | |
596 | ;; C preprocessor features | |
597 | semantic-lex-cpp-define | |
598 | semantic-lex-cpp-undef | |
599 | semantic-lex-c-if | |
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 | |
609 | semantic-lex-number | |
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) | |
621 | ||
622 | (define-mode-local-override semantic-parse-region c-mode | |
623 | (start end &optional nonterminal depth returnonerror) | |
91abaf51 | 624 | "Calls `semantic-parse-region-default', except in a macro expansion. |
4feec2f5 CY |
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 | |
628 | as for the parent." | |
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))))) | |
634 | (if macroexpand | |
635 | (progn | |
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) | |
640 | ) | |
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)) | |
647 | (consp llt-fakebits) | |
648 | (symbolp (car llt-fakebits)) | |
649 | ) | |
650 | (progn | |
651 | (setq depth 0) | |
652 | ||
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 | |
656 | ;; removed. | |
657 | (let ((tokstream | |
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 | |
661 | ;; fake tokens. | |
662 | (cdr llt-fakebits) | |
663 | ;; Lex up the region with a depth of 0 | |
664 | (semantic-lex start end 0)))) | |
665 | ||
666 | ;; Do the parse | |
667 | (nreverse | |
668 | (semantic-repeat-parse-whole-stream tokstream | |
669 | nonterminal | |
670 | returnonerror)) | |
671 | ||
672 | )) | |
673 | ||
674 | ;; It was not a macro expansion, nor a special semantic-list. | |
675 | ;; Do old thing. | |
676 | (semantic-parse-region-default start end | |
677 | nonterminal depth | |
678 | returnonerror) | |
679 | ))) | |
680 | ;; Do the parse | |
681 | (semantic-parse-region-default start end nonterminal | |
682 | depth returnonerror) | |
683 | )) | |
684 | ||
29e1a603 | 685 | (defvar semantic-c-parse-token-hack-depth 0 |
9bf6c65c | 686 | "Current depth of recursive calls to `semantic-c-parse-lexical-token'.") |
29e1a603 | 687 | |
4feec2f5 CY |
688 | (defun semantic-c-parse-lexical-token (lexicaltoken nonterminal depth |
689 | returnonerror) | |
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 | |
693 | parsed there. | |
694 | Argument NONTERMINAL, DEPTH, and RETURNONERROR are passed into | |
695 | the regular parser." | |
29e1a603 CY |
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))) | |
4feec2f5 CY |
699 | (mode major-mode) |
700 | (spp-syms semantic-lex-spp-dynamic-macro-symbol-obarray) | |
701 | (stream nil) | |
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)) | |
706 | ) | |
dd9af436 CY |
707 | (if (> semantic-c-parse-token-hack-depth 5) |
708 | nil | |
709 | (with-current-buffer buf | |
710 | (erase-buffer) | |
711 | (when (not (eq major-mode mode)) | |
712 | (save-match-data | |
713 | ||
714 | ;; Protect against user hooks throwing errors. | |
715 | (condition-case nil | |
716 | (funcall mode) | |
717 | (error | |
718 | (if (y-or-n-p | |
719 | (format "There was an error initializing %s in buffer \"%s\". Debug your hooks? " | |
720 | mode (buffer-name))) | |
721 | (semantic-c-debug-mode-init mode) | |
722 | (message "Macro parsing state may be broken...") | |
723 | (sit-for 1)))) | |
724 | ) ; save match data | |
725 | ||
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) | |
732 | (semantic-lex-init) | |
733 | (semantic-clear-toplevel-cache) | |
734 | (remove-hook 'semantic-lex-reset-hooks 'semantic-lex-spp-reset-hook | |
735 | t) | |
736 | ) | |
737 | ;; Get the macro symbol table right. | |
738 | (setq semantic-lex-spp-dynamic-macro-symbol-obarray spp-syms) | |
739 | ;; (message "%S" macros) | |
740 | (dolist (sym macros) | |
741 | (semantic-lex-spp-symbol-set (car sym) (cdr sym))) | |
742 | ||
743 | (insert symtext) | |
744 | ||
745 | (setq stream | |
746 | (semantic-parse-region-default | |
747 | (point-min) (point-max) nonterminal depth returnonerror)) | |
748 | ||
749 | ;; Clean up macro symbols | |
750 | (dolist (sym macros) | |
751 | (semantic-lex-spp-symbol-remove (car sym))) | |
752 | ||
753 | ;; Convert the text of the stream. | |
754 | (dolist (tag 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)) | |
759 | )) | |
760 | )) | |
4feec2f5 CY |
761 | stream)) |
762 | ||
8d106ea0 CY |
763 | (defvar semantic-c-debug-mode-init-last-mode nil |
764 | "The most recent mode needing debugging.") | |
765 | ||
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)) | |
2054a44c | 769 | (if (called-interactively-p 'interactive) |
8d106ea0 CY |
770 | ;; Do the debug. |
771 | (progn | |
772 | (switch-to-buffer (get-buffer-create "*MODE HACK TEST*")) | |
773 | (let ((debug-on-error t)) | |
774 | (funcall mm))) | |
775 | ||
776 | ;; Notify about the debug | |
777 | (setq semantic-c-debug-mode-init-last-mode mm) | |
778 | ||
779 | (add-hook 'post-command-hook 'semantic-c-debug-mode-init-pch))) | |
780 | ||
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*")) | |
786 | (erase-buffer) | |
91abaf51 | 787 | (insert "A failure occurred while parsing your buffers. |
8d106ea0 | 788 | |
91abaf51 | 789 | The failure occurred while attempting to initialize " (symbol-name mm) " in a |
8d106ea0 CY |
790 | buffer not associated with a file. To debug this problem, type |
791 | ||
792 | M-x semantic-c-debug-mode-init | |
793 | ||
794 | now. | |
795 | ") | |
796 | (remove-hook 'post-command-hook 'semantic-c-debug-mode-init-pch))) | |
797 | ||
4feec2f5 CY |
798 | (defun semantic-expand-c-tag (tag) |
799 | "Expand TAG into a list of equivalent tags, or nil." | |
800 | (let ((return-list nil) | |
801 | ) | |
802 | ;; Expand an EXTERN C first. | |
803 | (when (eq (semantic-tag-class tag) 'extern) | |
804 | (let* ((mb (semantic-tag-get-attribute tag :members)) | |
805 | (ret mb)) | |
806 | (while mb | |
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)) | |
810 | (setq mb (cdr mb))) | |
811 | (setq return-list ret))) | |
812 | ||
813 | ;; Function or variables that have a :type that is some complex | |
814 | ;; thing, extract it, and replace it with a reference. | |
815 | ;; | |
816 | ;; Thus, struct A { int a; } B; | |
817 | ;; | |
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)) | |
824 | (typeref nil) | |
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)) | |
832 | tname | |
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) | |
838 | nil nil | |
839 | :prototype t)) | |
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))))) | |
849 | ||
850 | ;; Name of the tag is a list, so expand it. Tag lists occur | |
851 | ;; for variables like this: int var1, var2, var3; | |
852 | ;; | |
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))) | |
857 | (cond | |
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 | |
862 | ;; of returns. | |
863 | ((consp rl) | |
864 | (setq return-list (append rl return-list))) | |
865 | )) | |
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))) | |
871 | ) | |
872 | ||
873 | ;; Default, don't change the tag means returning nil. | |
874 | return-list)) | |
875 | ||
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 ) | |
881 | (let ((vl nil) | |
882 | (basety (semantic-tag-type tag)) | |
883 | (ty "") | |
884 | (mods (semantic-tag-get-attribute tag :typemodifiers)) | |
885 | (suffix "") | |
886 | (lst (semantic-tag-name tag)) | |
887 | (default nil) | |
888 | (cur nil)) | |
889 | ;; Open up each name in the name list. | |
890 | (while lst | |
891 | (setq suffix "" ty "") | |
892 | (setq cur (car lst)) | |
893 | (if (nth 2 cur) | |
894 | (setq suffix (concat ":" (nth 2 cur)))) | |
895 | (if (= (length basety) 1) | |
896 | (setq ty (car basety)) | |
897 | (setq ty basety)) | |
898 | (setq default (nth 4 cur)) | |
899 | (setq vl (cons | |
900 | (semantic-tag-new-variable | |
901 | (car cur) ;name | |
902 | ty ;type | |
903 | (if default | |
904 | (buffer-substring-no-properties | |
905 | (car default) (car (cdr default)))) | |
906 | :constant-flag (semantic-tag-variable-constant-p tag) | |
907 | :suffix suffix | |
908 | :typemodifiers mods | |
909 | :dereference (length (nth 3 cur)) | |
910 | :pointer (nth 1 cur) | |
911 | :reference (semantic-tag-get-attribute tag :reference) | |
912 | :documentation (semantic-tag-docstring tag) ;doc | |
913 | ) | |
914 | vl)) | |
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))) | |
919 | ;; Return the list | |
920 | (nreverse vl))) | |
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 ) | |
925 | (let ((vl nil) | |
dd9af436 CY |
926 | (names (semantic-tag-name tag)) |
927 | (super (semantic-tag-get-attribute tag :superclasses)) | |
928 | (addlast nil)) | |
929 | ||
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. | |
935 | (setq addlast super) | |
936 | ||
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) | |
944 | (setq addlast nil)) | |
945 | ||
946 | ;; Add in props to the full superclass. | |
947 | (when addlast | |
948 | (semantic--tag-copy-properties tag addlast) | |
949 | (semantic--tag-set-overlay addlast (semantic-tag-overlay tag))) | |
950 | ) | |
951 | ||
4feec2f5 | 952 | (while names |
dd9af436 | 953 | |
4feec2f5 CY |
954 | (setq vl (cons (semantic-tag-new-type |
955 | (nth 1 (car names)) ; name | |
956 | "typedef" | |
957 | (semantic-tag-type-members tag) | |
958 | ;; parent is just tbe name of what | |
959 | ;; is passed down as a tag. | |
960 | (list | |
961 | (semantic-tag-name | |
962 | (semantic-tag-type-superclasses tag))) | |
963 | :pointer | |
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 | |
969 | ;; typedef. | |
dd9af436 | 970 | :typedef super |
4feec2f5 CY |
971 | ;;(semantic-tag-type-superclasses tag) |
972 | :documentation | |
973 | (semantic-tag-docstring tag)) | |
974 | vl)) | |
975 | (semantic--tag-copy-properties tag (car vl)) | |
dd9af436 | 976 | (semantic--tag-set-overlay (car vl) (semantic-tag-overlay tag)) |
4feec2f5 | 977 | (setq names (cdr names))) |
dd9af436 CY |
978 | |
979 | ;; Add typedef superclass last. | |
980 | (when addlast (setq vl (cons addlast vl))) | |
981 | ||
4feec2f5 CY |
982 | 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! | |
986 | (let ((vl nil)) | |
987 | (while tag | |
988 | (setq vl (append (semantic-tag-components (car vl)) | |
989 | vl) | |
990 | tag (cdr tag))) | |
991 | vl)) | |
992 | (t nil))) | |
993 | ||
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.") | |
996 | ||
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: | |
91abaf51 | 1001 | (TYPENAME . TYPEOFTYPE) |
4feec2f5 CY |
1002 | where typename is the name of the type, and typeoftype is \"class\" |
1003 | or \"struct\".") | |
1004 | ||
1005 | (defun semantic-c-reconstitute-token (tokenpart declmods typedecl) | |
1006 | "Reconstitute a token TOKENPART with DECLMODS and TYPEDECL. | |
1007 | This is so we don't have to match the same starting text several times. | |
1008 | Optional argument STAR and REF indicate the number of * and & in the typedef." | |
1009 | (when (and (listp typedecl) | |
1010 | (= 1 (length typedecl)) | |
1011 | (stringp (car typedecl))) | |
1012 | (setq typedecl (car typedecl))) | |
1013 | (cond ((eq (nth 1 tokenpart) 'variable) | |
1014 | (semantic-tag-new-variable | |
1015 | (car tokenpart) | |
1016 | (or typedecl "int") ;type | |
1017 | nil ;default value (filled with expand) | |
1018 | :constant-flag (if (member "const" declmods) t nil) | |
1019 | :typemodifiers (delete "const" declmods) | |
1020 | ) | |
1021 | ) | |
1022 | ((eq (nth 1 tokenpart) 'function) | |
1023 | ;; We should look at part 4 (the arglist) here, and throw an | |
1024 | ;; error of some sort if it contains parser errors so that we | |
1025 | ;; don't parser function calls, but that is a little beyond what | |
1026 | ;; is available for data here. | |
1027 | (let* ((constructor | |
1028 | (and (or (and semantic-c-classname | |
1029 | (string= (car semantic-c-classname) | |
1030 | (car tokenpart))) | |
1031 | (and (stringp (car (nth 2 tokenpart))) | |
1032 | (string= (car (nth 2 tokenpart)) (car tokenpart))) | |
dd9af436 | 1033 | (nth 10 tokenpart) ; initializers |
4feec2f5 CY |
1034 | ) |
1035 | (not (car (nth 3 tokenpart))))) | |
1036 | (fcnpointer (string-match "^\\*" (car tokenpart))) | |
1037 | (fnname (if fcnpointer | |
1038 | (substring (car tokenpart) 1) | |
1039 | (car tokenpart))) | |
1040 | (operator (if (string-match "[a-zA-Z]" fnname) | |
1041 | nil | |
1042 | t)) | |
1043 | ) | |
1044 | (if fcnpointer | |
1045 | ;; Function pointers are really variables. | |
1046 | (semantic-tag-new-variable | |
1047 | fnname | |
1048 | typedecl | |
1049 | nil | |
1050 | ;; It is a function pointer | |
1051 | :functionpointer-flag t | |
1052 | ) | |
1053 | ;; The function | |
1054 | (semantic-tag-new-function | |
1055 | fnname | |
1056 | (or typedecl ;type | |
1057 | (cond ((car (nth 3 tokenpart) ) | |
1058 | "void") ; Destructors have no return? | |
1059 | (constructor | |
1060 | ;; Constructors return an object. | |
1061 | (semantic-tag-new-type | |
1062 | ;; name | |
1063 | (or (car semantic-c-classname) | |
dd9af436 CY |
1064 | (let ((split (semantic-analyze-split-name-c-mode |
1065 | (car (nth 2 tokenpart))))) | |
1066 | (if (stringp split) split | |
1067 | (car (last split))))) | |
4feec2f5 CY |
1068 | ;; type |
1069 | (or (cdr semantic-c-classname) | |
1070 | "class") | |
1071 | ;; members | |
1072 | nil | |
1073 | ;; parents | |
1074 | nil | |
1075 | )) | |
1076 | (t "int"))) | |
1077 | (nth 4 tokenpart) ;arglist | |
1078 | :constant-flag (if (member "const" declmods) t nil) | |
1079 | :typemodifiers (delete "const" declmods) | |
1080 | :parent (car (nth 2 tokenpart)) | |
1081 | :destructor-flag (if (car (nth 3 tokenpart) ) t) | |
1082 | :constructor-flag (if constructor t) | |
1083 | :pointer (nth 7 tokenpart) | |
1084 | :operator-flag operator | |
1085 | ;; Even though it is "throw" in C++, we use | |
1086 | ;; `throws' as a common name for things that toss | |
1087 | ;; exceptions about. | |
1088 | :throws (nth 5 tokenpart) | |
1089 | ;; Reemtrant is a C++ thingy. Add it here | |
1090 | :reentrant-flag (if (member "reentrant" (nth 6 tokenpart)) t) | |
1091 | ;; A function post-const is funky. Try stuff | |
1092 | :methodconst-flag (if (member "const" (nth 6 tokenpart)) t) | |
1093 | ;; prototypes are functions w/ no body | |
1094 | :prototype-flag (if (nth 8 tokenpart) t) | |
1095 | ;; Pure virtual | |
1096 | :pure-virtual-flag (if (eq (nth 8 tokenpart) :pure-virtual-flag) t) | |
1097 | ;; Template specifier. | |
1098 | :template-specifier (nth 9 tokenpart) | |
1099 | ))) | |
1100 | ) | |
1101 | )) | |
1102 | ||
1103 | (defun semantic-c-reconstitute-template (tag specifier) | |
1104 | "Reconstitute the token TAG with the template SPECIFIER." | |
1105 | (semantic-tag-put-attribute tag :template (or specifier "")) | |
1106 | tag) | |
1107 | ||
1108 | \f | |
1109 | ;;; Override methods & Variables | |
1110 | ;; | |
1111 | (define-mode-local-override semantic-format-tag-name | |
1112 | c-mode (tag &optional parent color) | |
1113 | "Convert TAG to a string that is the print name for TAG. | |
1114 | Optional PARENT and COLOR are ignored." | |
1115 | (let ((name (semantic-format-tag-name-default tag parent color)) | |
1116 | (fnptr (semantic-tag-get-attribute tag :functionpointer-flag)) | |
1117 | ) | |
1118 | (if (not fnptr) | |
1119 | name | |
1120 | (concat "(*" name ")")) | |
1121 | )) | |
1122 | ||
1123 | (define-mode-local-override semantic-format-tag-canonical-name | |
1124 | c-mode (tag &optional parent color) | |
1125 | "Create a cannonical name for TAG. | |
1126 | PARENT specifies a parent class. | |
1127 | COLOR indicates that the text should be type colorized. | |
1128 | Enhances the base class to search for the entire parent | |
1129 | tree to make the name accurate." | |
1130 | (semantic-format-tag-canonical-name-default tag parent color) | |
1131 | ) | |
1132 | ||
1133 | (define-mode-local-override semantic-format-tag-type c-mode (tag color) | |
1134 | "Convert the data type of TAG to a string usable in tag formatting. | |
1135 | Adds pointer and reference symbols to the default. | |
1136 | Argument COLOR adds color to the text." | |
1137 | (let* ((type (semantic-tag-type tag)) | |
1138 | (defaulttype nil) | |
1139 | (point (semantic-tag-get-attribute tag :pointer)) | |
1140 | (ref (semantic-tag-get-attribute tag :reference)) | |
1141 | ) | |
1142 | (if (semantic-tag-p type) | |
1143 | (let ((typetype (semantic-tag-type type)) | |
1144 | (typename (semantic-tag-name type))) | |
1145 | ;; Create the string that expresses the type | |
1146 | (if (string= typetype "class") | |
1147 | (setq defaulttype typename) | |
1148 | (setq defaulttype (concat typetype " " typename)))) | |
1149 | (setq defaulttype (semantic-format-tag-type-default tag color))) | |
1150 | ||
1151 | ;; Colorize | |
1152 | (when color | |
1153 | (setq defaulttype (semantic--format-colorize-text defaulttype 'type))) | |
1154 | ||
1155 | ;; Add refs, ptrs, etc | |
1156 | (if ref (setq ref "&")) | |
1157 | (if point (setq point (make-string point ?*)) "") | |
1158 | (when type | |
1159 | (concat defaulttype ref point)) | |
1160 | )) | |
1161 | ||
1162 | (define-mode-local-override semantic-find-tags-by-scope-protection | |
1163 | c-mode (scopeprotection parent &optional table) | |
1164 | "Override the usual search for protection. | |
1165 | We can be more effective than the default by scanning through once, | |
1166 | and collecting tags based on the labels we see along the way." | |
1167 | (if (not table) (setq table (semantic-tag-type-members parent))) | |
1168 | (if (null scopeprotection) | |
1169 | table | |
1170 | (let ((ans nil) | |
1171 | (curprot 1) | |
1172 | (targetprot (cond ((eq scopeprotection 'public) | |
1173 | 1) | |
1174 | ((eq scopeprotection 'protected) | |
1175 | 2) | |
1176 | (t 3) | |
1177 | )) | |
1178 | (alist '(("public" . 1) | |
1179 | ("protected" . 2) | |
1180 | ("private" . 3))) | |
1181 | ) | |
1182 | (dolist (tag table) | |
1183 | (cond | |
1184 | ((semantic-tag-of-class-p tag 'label) | |
1185 | (setq curprot (cdr (assoc (semantic-tag-name tag) alist))) | |
1186 | ) | |
1187 | ((>= targetprot curprot) | |
1188 | (setq ans (cons tag ans))) | |
1189 | )) | |
1190 | ans))) | |
1191 | ||
1192 | (define-mode-local-override semantic-tag-protection | |
1193 | c-mode (tag &optional parent) | |
1194 | "Return the protection of TAG in PARENT. | |
1195 | Override function for `semantic-tag-protection'." | |
1196 | (let ((mods (semantic-tag-modifiers tag)) | |
1197 | (prot nil)) | |
1198 | ;; Check the modifiers for protection if we are not a child | |
1199 | ;; of some class type. | |
1200 | (when (or (not parent) (not (eq (semantic-tag-class parent) 'type))) | |
1201 | (while (and (not prot) mods) | |
1202 | (if (stringp (car mods)) | |
1203 | (let ((s (car mods))) | |
1204 | ;; A few silly defaults to get things started. | |
1205 | (cond ((or (string= s "extern") | |
1206 | (string= s "export")) | |
1207 | 'public) | |
1208 | ((string= s "static") | |
1209 | 'private)))) | |
1210 | (setq mods (cdr mods)))) | |
1211 | ;; If we have a typed parent, look for :public style labels. | |
1212 | (when (and parent (eq (semantic-tag-class parent) 'type)) | |
1213 | (let ((pp (semantic-tag-type-members parent))) | |
1214 | (while (and pp (not (semantic-equivalent-tag-p (car pp) tag))) | |
1215 | (when (eq (semantic-tag-class (car pp)) 'label) | |
1216 | (setq prot | |
1217 | (cond ((string= (semantic-tag-name (car pp)) "public") | |
1218 | 'public) | |
1219 | ((string= (semantic-tag-name (car pp)) "private") | |
1220 | 'private) | |
1221 | ((string= (semantic-tag-name (car pp)) "protected") | |
1222 | 'protected))) | |
1223 | ) | |
1224 | (setq pp (cdr pp))))) | |
1225 | (when (and (not prot) (eq (semantic-tag-class parent) 'type)) | |
1226 | (setq prot | |
1227 | (cond ((string= (semantic-tag-type parent) "class") 'private) | |
1228 | ((string= (semantic-tag-type parent) "struct") 'public) | |
1229 | (t 'unknown)))) | |
1230 | (or prot | |
1231 | (if (and parent (semantic-tag-of-class-p parent 'type)) | |
1232 | 'public | |
1233 | nil)))) | |
1234 | ||
1235 | (define-mode-local-override semantic-tag-components c-mode (tag) | |
1236 | "Return components for TAG." | |
1237 | (if (and (eq (semantic-tag-class tag) 'type) | |
1238 | (string= (semantic-tag-type tag) "typedef")) | |
1239 | ;; A typedef can contain a parent who has positional children, | |
1240 | ;; but that parent will not have a position. Do this funny hack | |
1241 | ;; to make sure we can apply overlays properly. | |
1242 | (let ((sc (semantic-tag-get-attribute tag :typedef))) | |
1243 | (when (semantic-tag-p sc) (semantic-tag-components sc))) | |
1244 | (semantic-tag-components-default tag))) | |
1245 | ||
1246 | (defun semantic-c-tag-template (tag) | |
1247 | "Return the template specification for TAG, or nil." | |
1248 | (semantic-tag-get-attribute tag :template)) | |
1249 | ||
1250 | (defun semantic-c-tag-template-specifier (tag) | |
1251 | "Return the template specifier specification for TAG, or nil." | |
1252 | (semantic-tag-get-attribute tag :template-specifier)) | |
1253 | ||
1254 | (defun semantic-c-template-string-body (templatespec) | |
1255 | "Convert TEMPLATESPEC into a string. | |
1256 | This might be a string, or a list of tokens." | |
1257 | (cond ((stringp templatespec) | |
1258 | templatespec) | |
1259 | ((semantic-tag-p templatespec) | |
1260 | (semantic-format-tag-abbreviate templatespec)) | |
1261 | ((listp templatespec) | |
1262 | (mapconcat 'semantic-format-tag-abbreviate templatespec ", ")))) | |
1263 | ||
1264 | (defun semantic-c-template-string (token &optional parent color) | |
1265 | "Return a string representing the TEMPLATE attribute of TOKEN. | |
1266 | This string is prefixed with a space, or is the empty string. | |
1267 | Argument PARENT specifies a parent type. | |
1268 | Argument COLOR specifies that the string should be colorized." | |
1269 | (let ((t2 (semantic-c-tag-template-specifier token)) | |
1270 | (t1 (semantic-c-tag-template token)) | |
1271 | ;; @todo - Need to account for a parent that is a template | |
1272 | (pt1 (if parent (semantic-c-tag-template parent))) | |
1273 | (pt2 (if parent (semantic-c-tag-template-specifier parent))) | |
1274 | ) | |
1275 | (cond (t2 ;; we have a template with specifier | |
1276 | (concat " <" | |
1277 | ;; Fill in the parts here | |
1278 | (semantic-c-template-string-body t2) | |
1279 | ">")) | |
1280 | (t1 ;; we have a template without specifier | |
1281 | " <>") | |
1282 | (t | |
1283 | "")))) | |
1284 | ||
1285 | (define-mode-local-override semantic-format-tag-concise-prototype | |
1286 | c-mode (token &optional parent color) | |
1287 | "Return an abbreviated string describing TOKEN for C and C++. | |
1288 | Optional PARENT and COLOR as specified with | |
1289 | `semantic-format-tag-abbreviate-default'." | |
1290 | ;; If we have special template things, append. | |
1291 | (concat (semantic-format-tag-concise-prototype-default token parent color) | |
1292 | (semantic-c-template-string token parent color))) | |
1293 | ||
1294 | (define-mode-local-override semantic-format-tag-uml-prototype | |
1295 | c-mode (token &optional parent color) | |
91abaf51 | 1296 | "Return an UML string describing TOKEN for C and C++. |
4feec2f5 CY |
1297 | Optional PARENT and COLOR as specified with |
1298 | `semantic-abbreviate-tag-default'." | |
1299 | ;; If we have special template things, append. | |
1300 | (concat (semantic-format-tag-uml-prototype-default token parent color) | |
1301 | (semantic-c-template-string token parent color))) | |
1302 | ||
1303 | (define-mode-local-override semantic-tag-abstract-p | |
1304 | c-mode (tag &optional parent) | |
1305 | "Return non-nil if TAG is considered abstract. | |
1306 | PARENT is tag's parent. | |
1307 | In C, a method is abstract if it is `virtual', which is already | |
1308 | handled. A class is abstract iff it's destructor is virtual." | |
1309 | (cond | |
1310 | ((eq (semantic-tag-class tag) 'type) | |
1311 | (require 'semantic/find) | |
1312 | (or (semantic-brute-find-tag-by-attribute :pure-virtual-flag | |
1313 | (semantic-tag-components tag) | |
1314 | ) | |
1315 | (let* ((ds (semantic-brute-find-tag-by-attribute | |
1316 | :destructor-flag | |
1317 | (semantic-tag-components tag) | |
1318 | )) | |
1319 | (cs (semantic-brute-find-tag-by-attribute | |
1320 | :constructor-flag | |
1321 | (semantic-tag-components tag) | |
1322 | ))) | |
1323 | (and ds (member "virtual" (semantic-tag-modifiers (car ds))) | |
1324 | cs (eq 'protected (semantic-tag-protection (car cs) tag)) | |
1325 | ) | |
1326 | ))) | |
1327 | ((eq (semantic-tag-class tag) 'function) | |
1328 | (or (semantic-tag-get-attribute tag :pure-virtual-flag) | |
1329 | (member "virtual" (semantic-tag-modifiers tag)))) | |
1330 | (t (semantic-tag-abstract-p-default tag parent)))) | |
1331 | ||
1332 | (defun semantic-c-dereference-typedef (type scope &optional type-declaration) | |
1333 | "If TYPE is a typedef, get TYPE's type by name or tag, and return. | |
1334 | SCOPE is not used, and TYPE-DECLARATION is used only if TYPE is not a typedef." | |
1335 | (if (and (eq (semantic-tag-class type) 'type) | |
1336 | (string= (semantic-tag-type type) "typedef")) | |
1337 | (let ((dt (semantic-tag-get-attribute type :typedef))) | |
1338 | (cond ((and (semantic-tag-p dt) | |
1339 | (not (semantic-analyze-tag-prototype-p dt))) | |
1340 | ;; In this case, DT was declared directly. We need | |
1341 | ;; to clone DT and apply a filename to it. | |
1342 | (let* ((fname (semantic-tag-file-name type)) | |
1343 | (def (semantic-tag-copy dt nil fname))) | |
1344 | (list def def))) | |
1345 | ((stringp dt) (list dt (semantic-tag dt 'type))) | |
1346 | ((consp dt) (list (car dt) dt)))) | |
1347 | ||
1348 | (list type type-declaration))) | |
1349 | ||
1350 | (defun semantic-c--instantiate-template (tag def-list spec-list) | |
1351 | "Replace TAG name according to template specification. | |
1352 | DEF-LIST is the template information. | |
1353 | SPEC-LIST is the template specifier of the datatype instantiated." | |
1354 | (when (and (car def-list) (car spec-list)) | |
1355 | ||
1356 | (when (and (string= (semantic-tag-type (car def-list)) "class") | |
1357 | (string= (semantic-tag-name tag) (semantic-tag-name (car def-list)))) | |
1358 | (semantic-tag-set-name tag (semantic-tag-name (car spec-list)))) | |
1359 | ||
1360 | (semantic-c--instantiate-template tag (cdr def-list) (cdr spec-list)))) | |
1361 | ||
1362 | (defun semantic-c--template-name-1 (spec-list) | |
9bf6c65c GM |
1363 | "Return a string used to compute template class name. |
1364 | Based on SPEC-LIST, for ref<Foo,Bar> it will return 'Foo,Bar'." | |
4feec2f5 CY |
1365 | (when (car spec-list) |
1366 | (let* ((endpart (semantic-c--template-name-1 (cdr spec-list))) | |
1367 | (separator (and endpart ","))) | |
1368 | (concat (semantic-tag-name (car spec-list)) separator endpart)))) | |
1369 | ||
1370 | (defun semantic-c--template-name (type spec-list) | |
1371 | "Return a template class name for TYPE based on SPEC-LIST. | |
1372 | For a type `ref' with a template specifier of (Foo Bar) it will | |
1373 | return 'ref<Foo,Bar>'." | |
1374 | (concat (semantic-tag-name type) | |
1375 | "<" (semantic-c--template-name-1 (cdr spec-list)) ">")) | |
1376 | ||
1377 | (defun semantic-c-dereference-template (type scope &optional type-declaration) | |
9bf6c65c | 1378 | "Dereference any template specifiers in TYPE within SCOPE. |
4feec2f5 CY |
1379 | If TYPE is a template, return a TYPE copy with the templates types |
1380 | instantiated as specified in TYPE-DECLARATION." | |
1381 | (when (semantic-tag-p type-declaration) | |
1382 | (let ((def-list (semantic-tag-get-attribute type :template)) | |
1383 | (spec-list (semantic-tag-get-attribute type-declaration :template-specifier))) | |
1384 | (when (and def-list spec-list) | |
1385 | (setq type (semantic-tag-deep-copy-one-tag | |
1386 | type | |
1387 | (lambda (tag) | |
1388 | (when (semantic-tag-of-class-p tag 'type) | |
1389 | (semantic-c--instantiate-template | |
1390 | tag def-list spec-list)) | |
1391 | tag) | |
1392 | )) | |
1393 | (semantic-tag-set-name type (semantic-c--template-name type spec-list)) | |
1394 | (semantic-tag-put-attribute type :template nil) | |
1395 | (semantic-tag-set-faux type)))) | |
1396 | (list type type-declaration)) | |
1397 | ||
1398 | ;;; Patch here by "Raf" for instantiating templates. | |
1399 | (defun semantic-c-dereference-member-of (type scope &optional type-declaration) | |
1400 | "Dereference through the `->' operator of TYPE. | |
1401 | Uses the return type of the '->' operator if it is contained in TYPE. | |
1402 | SCOPE is the current local scope to perform searches in. | |
1403 | TYPE-DECLARATION is passed through." | |
1404 | (if semantic-c-member-of-autocast | |
1405 | (let ((operator (car (semantic-find-tags-by-name "->" (semantic-analyze-scoped-type-parts type))))) | |
1406 | (if operator | |
1407 | (list (semantic-tag-get-attribute operator :type) (semantic-tag-get-attribute operator :type)) | |
1408 | (list type type-declaration))) | |
1409 | (list type type-declaration))) | |
1410 | ||
1411 | ;; David Engster: The following three functions deal with namespace | |
1412 | ;; aliases and types which are member of a namespace through a using | |
1413 | ;; statement. For examples, see the file semantic/tests/testusing.cpp, | |
1414 | ;; tests 5 and following. | |
1415 | ||
1416 | (defun semantic-c-dereference-namespace (type scope &optional type-declaration) | |
1417 | "Dereference namespace which might hold an 'alias' for TYPE. | |
1418 | Such an alias can be created through 'using' statements in a | |
91abaf51 | 1419 | namespace declaration. This function checks the namespaces in |
4feec2f5 CY |
1420 | SCOPE for such statements." |
1421 | (let ((scopetypes (oref scope scopetypes)) | |
1422 | typename currentns tmp usingname result namespaces) | |
1423 | (when (and (semantic-tag-p type-declaration) | |
1424 | (or (null type) (semantic-tag-prototype-p type))) | |
1425 | (setq typename (semantic-analyze-split-name (semantic-tag-name type-declaration))) | |
1426 | ;; If we already have that TYPE in SCOPE, we do nothing | |
1427 | (unless (semantic-deep-find-tags-by-name (or (car-safe typename) typename) scopetypes) | |
1428 | (if (stringp typename) | |
1429 | ;; The type isn't fully qualified, so we have to search in all namespaces in SCOPE. | |
1430 | (setq namespaces (semantic-find-tags-by-type "namespace" scopetypes)) | |
1431 | ;; This is a fully qualified name, so we only have to search one namespace. | |
1432 | (setq namespaces (semanticdb-typecache-find (car typename))) | |
1433 | ;; Make sure it's really a namespace. | |
1434 | (if (string= (semantic-tag-type namespaces) "namespace") | |
1435 | (setq namespaces (list namespaces)) | |
1436 | (setq namespaces nil))) | |
1437 | (setq result nil) | |
1438 | ;; Iterate over all the namespaces we have to check. | |
1439 | (while (and namespaces | |
1440 | (null result)) | |
1441 | (setq currentns (car namespaces)) | |
1442 | ;; Check if this is namespace is an alias and dereference it if necessary. | |
1443 | (setq result (semantic-c-dereference-namespace-alias type-declaration currentns)) | |
1444 | (unless result | |
1445 | ;; Otherwise, check if we can reach the type through 'using' statements. | |
1446 | (setq result | |
1447 | (semantic-c-check-type-namespace-using type-declaration currentns))) | |
1448 | (setq namespaces (cdr namespaces))))) | |
1449 | (if result | |
1450 | ;; we have found the original type | |
1451 | (list result result) | |
1452 | (list type type-declaration)))) | |
1453 | ||
1454 | (defun semantic-c-dereference-namespace-alias (type namespace) | |
1455 | "Dereference TYPE in NAMESPACE, given that NAMESPACE is an alias. | |
1456 | Checks if NAMESPACE is an alias and if so, returns a new type | |
1457 | with a fully qualified name in the original namespace. Returns | |
1458 | nil if NAMESPACE is not an alias." | |
1459 | (when (eq (semantic-tag-get-attribute namespace :kind) 'alias) | |
1460 | (let ((typename (semantic-analyze-split-name (semantic-tag-name type))) | |
a964f5e5 CY |
1461 | ns nstype originaltype newtype) |
1462 | ;; Make typename unqualified | |
1463 | (if (listp typename) | |
1464 | (setq typename (last typename)) | |
1465 | (setq typename (list typename))) | |
4feec2f5 | 1466 | (when |
a964f5e5 CY |
1467 | (and |
1468 | ;; Get original namespace and make sure TYPE exists there. | |
1469 | (setq ns (semantic-tag-name | |
1470 | (car (semantic-tag-get-attribute namespace :members)))) | |
1471 | (setq nstype (semanticdb-typecache-find ns)) | |
1472 | (setq originaltype (semantic-find-tags-by-name | |
1473 | (car typename) | |
1474 | (semantic-tag-get-attribute nstype :members)))) | |
4feec2f5 | 1475 | ;; Construct new type with name in original namespace. |
a964f5e5 | 1476 | (setq ns (semantic-analyze-split-name ns)) |
4feec2f5 CY |
1477 | (setq newtype |
1478 | (semantic-tag-clone | |
a964f5e5 | 1479 | (car originaltype) |
4feec2f5 CY |
1480 | (semantic-analyze-unsplit-name |
1481 | (if (listp ns) | |
a964f5e5 CY |
1482 | (append ns typename) |
1483 | (append (list ns) typename))))))))) | |
4feec2f5 CY |
1484 | |
1485 | ;; This searches a type in a namespace, following through all using | |
1486 | ;; statements. | |
1487 | (defun semantic-c-check-type-namespace-using (type namespace) | |
1488 | "Check if TYPE is accessible in NAMESPACE through a using statement. | |
1489 | Returns the original type from the namespace where it is defined, | |
1490 | or nil if it cannot be found." | |
1491 | (let (usings result usingname usingtype unqualifiedname members shortname tmp) | |
1492 | ;; Get all using statements from NAMESPACE. | |
1493 | (when (and (setq usings (semantic-tag-get-attribute namespace :members)) | |
1494 | (setq usings (semantic-find-tags-by-class 'using usings))) | |
1495 | ;; Get unqualified typename. | |
1496 | (when (listp (setq unqualifiedname (semantic-analyze-split-name | |
1497 | (semantic-tag-name type)))) | |
1498 | (setq unqualifiedname (car (last unqualifiedname)))) | |
1499 | ;; Iterate over all using statements in NAMESPACE. | |
1500 | (while (and usings | |
1501 | (null result)) | |
1502 | (setq usingname (semantic-analyze-split-name | |
1503 | (semantic-tag-name (car usings))) | |
1504 | usingtype (semantic-tag-type (semantic-tag-type (car usings)))) | |
1505 | (cond | |
1506 | ((or (string= usingtype "namespace") | |
1507 | (stringp usingname)) | |
1508 | ;; We are dealing with a 'using [namespace] NAMESPACE;' | |
1509 | ;; Search for TYPE in that namespace | |
1510 | (setq result | |
1511 | (semanticdb-typecache-find usingname)) | |
1512 | (if (and result | |
1513 | (setq members (semantic-tag-get-attribute result :members)) | |
1514 | (setq members (semantic-find-tags-by-name unqualifiedname members))) | |
1515 | ;; TYPE is member of that namespace, so we are finished | |
1516 | (setq result (car members)) | |
1517 | ;; otherwise recursively search in that namespace for an alias | |
1518 | (setq result (semantic-c-check-type-namespace-using type result)) | |
1519 | (when result | |
1520 | (setq result (semantic-tag-type result))))) | |
1521 | ((and (string= usingtype "class") | |
1522 | (listp usingname)) | |
1523 | ;; We are dealing with a 'using TYPE;' | |
1524 | (when (string= unqualifiedname (car (last usingname))) | |
1525 | ;; We have found the correct tag. | |
1526 | (setq result (semantic-tag-type (car usings)))))) | |
1527 | (setq usings (cdr usings)))) | |
1528 | result)) | |
1529 | ||
1530 | ||
1531 | (define-mode-local-override semantic-analyze-dereference-metatype | |
1532 | c-mode (type scope &optional type-declaration) | |
1533 | "Dereference TYPE as described in `semantic-analyze-dereference-metatype'. | |
1534 | Handle typedef, template instantiation, and '->' operator." | |
1535 | (let* ((dereferencer-list '(semantic-c-dereference-typedef | |
1536 | semantic-c-dereference-template | |
1537 | semantic-c-dereference-member-of | |
1538 | semantic-c-dereference-namespace)) | |
1539 | (dereferencer (pop dereferencer-list)) | |
1540 | (type-tuple) | |
1541 | (original-type type)) | |
1542 | (while dereferencer | |
1543 | (setq type-tuple (funcall dereferencer type scope type-declaration) | |
1544 | type (car type-tuple) | |
1545 | type-declaration (cadr type-tuple)) | |
1546 | (if (not (eq type original-type)) | |
1547 | ;; we found a new type so break the dereferencer loop now ! | |
1548 | ;; (we will be recalled with the new type expanded by | |
1549 | ;; semantic-analyze-dereference-metatype-stack). | |
1550 | (setq dereferencer nil) | |
1551 | ;; no new type found try the next dereferencer : | |
1552 | (setq dereferencer (pop dereferencer-list))))) | |
1553 | (list type type-declaration)) | |
1554 | ||
1555 | (define-mode-local-override semantic-analyze-type-constants c-mode (type) | |
91abaf51 | 1556 | "When TYPE is a tag for an enum, return its parts. |
4feec2f5 CY |
1557 | These are constants which are of type TYPE." |
1558 | (if (and (eq (semantic-tag-class type) 'type) | |
1559 | (string= (semantic-tag-type type) "enum")) | |
1560 | (semantic-tag-type-members type))) | |
1561 | ||
1562 | (define-mode-local-override semantic-analyze-split-name c-mode (name) | |
1563 | "Split up tag names on colon (:) boundaries." | |
1564 | (let ((ans (split-string name ":"))) | |
1565 | (if (= (length ans) 1) | |
1566 | name | |
1567 | (delete "" ans)))) | |
1568 | ||
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 "::")) | |
1572 | ||
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)) | |
1578 | (tagreturn nil) | |
1579 | (tmp nil)) | |
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)) | |
1589 | (let ((idx 0) | |
1590 | (len (semanticdb-find-result-length tmp))) | |
1591 | (while (< idx len) | |
1592 | (setq tagreturn (cons (semantic-tag-type (car (semanticdb-find-result-nth tmp idx))) tagreturn)) | |
1593 | (setq idx (1+ idx))) | |
1594 | ) | |
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)))) | |
1599 | (dolist (T tmp) | |
1600 | (setq tagreturn (cons (semantic-tag-type T) tagreturn)) | |
1601 | ) | |
1602 | (setq tagsaroundpoint (cdr tagsaroundpoint)) | |
1603 | ) | |
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 | |
1608 | 'using | |
1609 | (semantic-get-local-variables)))) | |
1610 | ;; ... and add them. | |
1611 | (setq tagreturn | |
1612 | (append tagreturn | |
1613 | (mapcar 'semantic-tag-type tmp)))) | |
1614 | ;; Return the stuff | |
1615 | tagreturn | |
1616 | )) | |
1617 | ||
dd9af436 CY |
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)) | |
1622 | (namereturn nil) | |
1623 | (tmp nil) | |
1624 | ) | |
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)) | |
1633 | ) | |
1634 | namereturn)) | |
1635 | ||
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)) | |
1641 | ;; Expand the tag | |
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 | |
1649 | ;; namespace. | |
1650 | (require 'semantic/db-typecache) | |
1651 | (dolist (namespace s) | |
1652 | (setq newtag (semanticdb-typecache-faux-namespace | |
1653 | namespace (list newtag))) | |
1654 | ) | |
1655 | ;; Return the last created namespace. | |
1656 | newtag) | |
1657 | ;; Else, return tag unmodified. | |
1658 | tag))) | |
1659 | ||
4feec2f5 CY |
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) | |
1669 | origvar) | |
1670 | ;; No parent, just return the usual | |
1671 | origvar) | |
1672 | )) | |
1673 | ||
1674 | (define-mode-local-override semantic-idle-summary-current-symbol-info | |
1675 | c-mode () | |
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))) | |
1679 | (if spp-sym | |
1680 | (let* ((txt (concat "Macro: " sym)) | |
1681 | (sv (symbol-value spp-sym)) | |
1682 | (arg (semantic-lex-spp-macro-with-args sv)) | |
1683 | ) | |
1684 | (when arg | |
1685 | (setq txt (concat txt (format "%S" arg))) | |
1686 | (setq sv (cdr sv))) | |
1687 | ||
1688 | ;; This is optional, and potentially fraught w/ errors. | |
1689 | (condition-case nil | |
1690 | (dolist (lt sv) | |
1691 | (setq txt (concat txt " " (semantic-lex-token-text lt)))) | |
1692 | (error (setq txt (concat txt " #error in summary fcn")))) | |
1693 | ||
1694 | txt) | |
1695 | (semantic-idle-summary-current-symbol-info-default)))) | |
1696 | ||
1697 | (defvar-mode-local c-mode semantic-orphaned-member-metaparent-type "struct" | |
91abaf51 | 1698 | "When lost members are found in the class hierarchy generator, use a struct.") |
4feec2f5 CY |
1699 | |
1700 | (defvar-mode-local c-mode semantic-symbol->name-assoc-list | |
1701 | '((type . "Types") | |
1702 | (variable . "Variables") | |
1703 | (function . "Functions") | |
1704 | (include . "Includes") | |
1705 | ) | |
1706 | "List of tag classes, and strings to describe them.") | |
1707 | ||
1708 | (defvar-mode-local c-mode semantic-symbol->name-assoc-list-for-type-parts | |
1709 | '((type . "Types") | |
1710 | (variable . "Attributes") | |
1711 | (function . "Methods") | |
1712 | (label . "Labels") | |
1713 | ) | |
1714 | "List of tag classes in a datatype decl, and strings to describe them.") | |
1715 | ||
1716 | (defvar-mode-local c-mode imenu-create-index-function 'semantic-create-imenu-index | |
1717 | "Imenu index function for C.") | |
1718 | ||
1719 | (defvar-mode-local c-mode semantic-type-relation-separator-character | |
1720 | '("." "->" "::") | |
1721 | "Separator characters between something of a given type, and a field.") | |
1722 | ||
1723 | (defvar-mode-local c-mode semantic-command-separation-character ";" | |
91abaf51 | 1724 | "Command separation character for C.") |
4feec2f5 CY |
1725 | |
1726 | (defvar-mode-local c-mode senator-step-at-tag-classes '(function variable) | |
1727 | "Tag classes where senator will stop at the end.") | |
1728 | ||
a60f2e7b | 1729 | ;;;###autoload |
4feec2f5 CY |
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 '((?> ".") | |
1734 | (?< ".") | |
1735 | ) | |
1736 | ) | |
1737 | ||
1738 | (setq semantic-lex-analyzer #'semantic-c-lexer) | |
1739 | (add-hook 'semantic-lex-reset-hooks 'semantic-lex-spp-reset-hook nil t) | |
1740 | ) | |
1741 | ||
a60f2e7b | 1742 | ;;;###autoload |
4feec2f5 CY |
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))) | |
1747 | (if SA | |
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)))) | |
1754 | ||
1755 | (semantic-c-reset-preprocessor-symbol-map) | |
1756 | ) | |
1757 | ||
4feec2f5 CY |
1758 | ;;; SETUP QUERY |
1759 | ;; | |
1760 | (defun semantic-c-describe-environment () | |
1761 | "Describe the Semantic features of the current C environment." | |
1762 | (interactive) | |
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)) | |
1767 | ) | |
1768 | (semantic-fetch-tags) | |
1769 | ||
1770 | (with-output-to-temp-buffer "*Semantic C Environment*" | |
1771 | (when gcc | |
1772 | (princ "Calculated GCC Parameters:") | |
1773 | (dolist (P gcc) | |
1774 | (princ "\n ") | |
1775 | (princ (car P)) | |
1776 | (princ " = ") | |
1777 | (princ (cdr P)) | |
1778 | ) | |
1779 | ) | |
1780 | ||
1781 | (princ "\n\nInclude Path Summary:\n") | |
1fe1547a | 1782 | (when (and (boundp 'ede-object) ede-object) |
4feec2f5 CY |
1783 | (princ "\n This file's project include is handled by:\n") |
1784 | (princ " ") | |
1785 | (princ (object-print ede-object)) | |
1786 | (princ "\n with the system path:\n") | |
1787 | (dolist (dir (ede-system-include-path ede-object)) | |
1788 | (princ " ") | |
1789 | (princ dir) | |
1790 | (princ "\n")) | |
1791 | ) | |
1792 | ||
1793 | (when semantic-dependency-include-path | |
1794 | (princ "\n This file's generic include path is:\n") | |
1795 | (dolist (dir semantic-dependency-include-path) | |
1796 | (princ " ") | |
1797 | (princ dir) | |
1798 | (princ "\n"))) | |
1799 | ||
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) | |
1803 | (princ " ") | |
1804 | (princ dir) | |
1805 | (princ "\n"))) | |
1806 | ||
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) | |
1811 | (princ " ") | |
1812 | (princ file) | |
1813 | (princ "\n") | |
1814 | (princ " in table: ") | |
1815 | (princ (object-print (semanticdb-file-table-object file))) | |
1816 | (princ "\n") | |
1817 | )) | |
1818 | ||
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) | |
1822 | (princ " ") | |
1823 | (princ (car S)) | |
1824 | (princ " = ") | |
1825 | (princ (cdr S)) | |
1826 | (princ "\n") | |
1827 | )) | |
1828 | ||
1829 | (when semantic-lex-c-preprocessor-symbol-map | |
1830 | (princ "\n User symbol map:\n") | |
1831 | (dolist (S semantic-lex-c-preprocessor-symbol-map) | |
1832 | (princ " ") | |
1833 | (princ (car S)) | |
1834 | (princ " = ") | |
1835 | (princ (cdr S)) | |
1836 | (princ "\n") | |
1837 | )) | |
1838 | ||
dd9af436 CY |
1839 | (when (and (boundp 'ede-object) |
1840 | ede-object | |
1841 | (arrayp semantic-lex-spp-project-macro-symbol-obarray)) | |
1dc5c6f3 CY |
1842 | (princ "\n Project symbol map:\n") |
1843 | (princ " Your project symbol map is derived from the EDE object:\n ") | |
1844 | (princ (object-print ede-object)) | |
1845 | (princ "\n\n") | |
1846 | (let ((macros nil)) | |
1847 | (mapatoms | |
1848 | #'(lambda (symbol) | |
1849 | (setq macros (cons symbol macros))) | |
1850 | semantic-lex-spp-project-macro-symbol-obarray) | |
1851 | (dolist (S macros) | |
1852 | (princ " ") | |
1853 | (princ (symbol-name S)) | |
1854 | (princ " = ") | |
1855 | (princ (symbol-value S)) | |
1856 | (princ "\n") | |
1857 | ))) | |
1858 | ||
4feec2f5 CY |
1859 | (princ "\n\n Use: M-x semantic-lex-spp-describe RET\n") |
1860 | (princ "\n to see the complete macro table.\n") | |
1861 | ||
1862 | ))) | |
1863 | ||
1864 | (provide 'semantic/bovine/c) | |
1865 | ||
1866 | (semantic-c-reset-preprocessor-symbol-map) | |
1867 | ||
a60f2e7b CY |
1868 | ;; Local variables: |
1869 | ;; generated-autoload-file: "../loaddefs.el" | |
a60f2e7b CY |
1870 | ;; generated-autoload-load-name: "semantic/bovine/c" |
1871 | ;; End: | |
1872 | ||
3999968a | 1873 | ;; arch-tag: 263951a8-0f18-445d-8e73-eb8f9ac8e2a3 |
4feec2f5 | 1874 | ;;; semantic/bovine/c.el ends here |