]>
Commit | Line | Data |
---|---|---|
9219021c | 1 | /* Helper routines for C++ support in GDB. |
7b6bb8da | 2 | Copyright (C) 2003, 2004, 2007, 2008, 2009, 2010, 2011 |
4c38e0a4 | 3 | Free Software Foundation, Inc. |
9219021c | 4 | |
1fcb5155 | 5 | Contributed by David Carlton and by Kealia, Inc. |
9219021c DC |
6 | |
7 | This file is part of GDB. | |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 11 | the Free Software Foundation; either version 3 of the License, or |
9219021c DC |
12 | (at your option) any later version. |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
9219021c DC |
21 | |
22 | #include "defs.h" | |
23 | #include "cp-support.h" | |
24 | #include "gdb_obstack.h" | |
25 | #include "symtab.h" | |
26 | #include "symfile.h" | |
27 | #include "gdb_assert.h" | |
28 | #include "block.h" | |
5c4e30ca DC |
29 | #include "objfiles.h" |
30 | #include "gdbtypes.h" | |
31 | #include "dictionary.h" | |
32 | #include "command.h" | |
b368761e | 33 | #include "frame.h" |
27aa8d6a | 34 | #include "buildsym.h" |
34eaf542 | 35 | #include "language.h" |
9219021c | 36 | |
1fcb5155 | 37 | static struct symbol *lookup_namespace_scope (const char *name, |
1fcb5155 DC |
38 | const struct block *block, |
39 | const domain_enum domain, | |
1fcb5155 DC |
40 | const char *scope, |
41 | int scope_len); | |
42 | ||
43 | static struct symbol *lookup_symbol_file (const char *name, | |
1fcb5155 DC |
44 | const struct block *block, |
45 | const domain_enum domain, | |
1fcb5155 DC |
46 | int anonymous_namespace); |
47 | ||
b368761e DC |
48 | static struct type *cp_lookup_transparent_type_loop (const char *name, |
49 | const char *scope, | |
50 | int scope_len); | |
51 | ||
5c4e30ca DC |
52 | static void initialize_namespace_symtab (struct objfile *objfile); |
53 | ||
54 | static struct block *get_possible_namespace_block (struct objfile *objfile); | |
55 | ||
56 | static void free_namespace_block (struct symtab *symtab); | |
57 | ||
58 | static int check_possible_namespace_symbols_loop (const char *name, | |
59 | int len, | |
60 | struct objfile *objfile); | |
61 | ||
62 | static int check_one_possible_namespace_symbol (const char *name, | |
63 | int len, | |
64 | struct objfile *objfile); | |
65 | ||
21b556f4 | 66 | static struct symbol *lookup_possible_namespace_symbol (const char *name); |
5c4e30ca DC |
67 | |
68 | static void maintenance_cplus_namespace (char *args, int from_tty); | |
69 | ||
9219021c DC |
70 | /* Check to see if SYMBOL refers to an object contained within an |
71 | anonymous namespace; if so, add an appropriate using directive. */ | |
72 | ||
73 | /* Optimize away strlen ("(anonymous namespace)"). */ | |
74 | ||
75 | #define ANONYMOUS_NAMESPACE_LEN 21 | |
76 | ||
77 | void | |
78 | cp_scan_for_anonymous_namespaces (const struct symbol *symbol) | |
79 | { | |
df8a16a1 | 80 | if (SYMBOL_DEMANGLED_NAME (symbol) != NULL) |
9219021c | 81 | { |
df8a16a1 | 82 | const char *name = SYMBOL_DEMANGLED_NAME (symbol); |
9219021c DC |
83 | unsigned int previous_component; |
84 | unsigned int next_component; | |
9219021c DC |
85 | |
86 | /* Start with a quick-and-dirty check for mention of "(anonymous | |
87 | namespace)". */ | |
88 | ||
89 | if (!cp_is_anonymous (name)) | |
90 | return; | |
91 | ||
92 | previous_component = 0; | |
93 | next_component = cp_find_first_component (name + previous_component); | |
94 | ||
95 | while (name[next_component] == ':') | |
96 | { | |
97 | if ((next_component - previous_component) == ANONYMOUS_NAMESPACE_LEN | |
98 | && strncmp (name + previous_component, | |
99 | "(anonymous namespace)", | |
100 | ANONYMOUS_NAMESPACE_LEN) == 0) | |
101 | { | |
aff410f1 MS |
102 | int dest_len = (previous_component == 0 |
103 | ? 0 : previous_component - 2); | |
8c902bb1 | 104 | int src_len = next_component; |
794684b6 | 105 | |
8c902bb1 SW |
106 | char *dest = alloca (dest_len + 1); |
107 | char *src = alloca (src_len + 1); | |
794684b6 | 108 | |
8c902bb1 SW |
109 | memcpy (dest, name, dest_len); |
110 | memcpy (src, name, src_len); | |
794684b6 | 111 | |
8c902bb1 SW |
112 | dest[dest_len] = '\0'; |
113 | src[src_len] = '\0'; | |
794684b6 | 114 | |
9219021c DC |
115 | /* We've found a component of the name that's an |
116 | anonymous namespace. So add symbols in it to the | |
117 | namespace given by the previous component if there is | |
118 | one, or to the global namespace if there isn't. */ | |
13387711 | 119 | cp_add_using_directive (dest, src, NULL, NULL, |
c0cc3a76 | 120 | &SYMBOL_SYMTAB (symbol)->objfile->objfile_obstack); |
9219021c DC |
121 | } |
122 | /* The "+ 2" is for the "::". */ | |
123 | previous_component = next_component + 2; | |
124 | next_component = (previous_component | |
125 | + cp_find_first_component (name | |
126 | + previous_component)); | |
127 | } | |
128 | } | |
129 | } | |
130 | ||
c0cc3a76 | 131 | |
aff410f1 MS |
132 | /* Add a using directive to using_directives. If the using directive |
133 | in question has already been added, don't add it twice. | |
134 | ||
135 | Create a new struct using_direct which imports the namespace SRC | |
136 | into the scope DEST. ALIAS is the name of the imported namespace | |
137 | in the current scope. If ALIAS is NULL then the namespace is known | |
138 | by its original name. DECLARATION is the name if the imported | |
139 | varable if this is a declaration import (Eg. using A::x), otherwise | |
140 | it is NULL. The arguments are copied into newly allocated memory | |
141 | so they can be temporaries. */ | |
9219021c DC |
142 | |
143 | void | |
13387711 SW |
144 | cp_add_using_directive (const char *dest, |
145 | const char *src, | |
146 | const char *alias, | |
147 | const char *declaration, | |
c0cc3a76 | 148 | struct obstack *obstack) |
9219021c DC |
149 | { |
150 | struct using_direct *current; | |
151 | struct using_direct *new; | |
13387711 | 152 | |
9219021c DC |
153 | /* Has it already been added? */ |
154 | ||
27aa8d6a | 155 | for (current = using_directives; current != NULL; current = current->next) |
9219021c | 156 | { |
8c902bb1 | 157 | if (strcmp (current->import_src, src) == 0 |
c0cc3a76 SW |
158 | && strcmp (current->import_dest, dest) == 0 |
159 | && ((alias == NULL && current->alias == NULL) | |
160 | || (alias != NULL && current->alias != NULL | |
13387711 SW |
161 | && strcmp (alias, current->alias) == 0)) |
162 | && ((declaration == NULL && current->declaration == NULL) | |
163 | || (declaration != NULL && current->declaration != NULL | |
164 | && strcmp (declaration, current->declaration) == 0))) | |
9219021c DC |
165 | return; |
166 | } | |
167 | ||
c0cc3a76 SW |
168 | new = OBSTACK_ZALLOC (obstack, struct using_direct); |
169 | ||
170 | new->import_src = obsavestring (src, strlen (src), obstack); | |
171 | new->import_dest = obsavestring (dest, strlen (dest), obstack); | |
794684b6 | 172 | |
c0cc3a76 SW |
173 | if (alias != NULL) |
174 | new->alias = obsavestring (alias, strlen (alias), obstack); | |
175 | ||
13387711 SW |
176 | if (declaration != NULL) |
177 | new->declaration = obsavestring (declaration, strlen (declaration), | |
178 | obstack); | |
179 | ||
c0cc3a76 SW |
180 | new->next = using_directives; |
181 | using_directives = new; | |
9219021c DC |
182 | } |
183 | ||
184 | /* Record the namespace that the function defined by SYMBOL was | |
185 | defined in, if necessary. BLOCK is the associated block; use | |
186 | OBSTACK for allocation. */ | |
187 | ||
188 | void | |
189 | cp_set_block_scope (const struct symbol *symbol, | |
190 | struct block *block, | |
df8a16a1 DJ |
191 | struct obstack *obstack, |
192 | const char *processing_current_prefix, | |
193 | int processing_has_namespace_info) | |
9219021c | 194 | { |
df8a16a1 | 195 | if (processing_has_namespace_info) |
9219021c | 196 | { |
df8a16a1 DJ |
197 | block_set_scope |
198 | (block, obsavestring (processing_current_prefix, | |
199 | strlen (processing_current_prefix), | |
200 | obstack), | |
201 | obstack); | |
202 | } | |
203 | else if (SYMBOL_DEMANGLED_NAME (symbol) != NULL) | |
204 | { | |
205 | /* Try to figure out the appropriate namespace from the | |
206 | demangled name. */ | |
9219021c | 207 | |
df8a16a1 DJ |
208 | /* FIXME: carlton/2003-04-15: If the function in question is |
209 | a method of a class, the name will actually include the | |
210 | name of the class as well. This should be harmless, but | |
211 | is a little unfortunate. */ | |
9219021c | 212 | |
df8a16a1 DJ |
213 | const char *name = SYMBOL_DEMANGLED_NAME (symbol); |
214 | unsigned int prefix_len = cp_entire_prefix_len (name); | |
9219021c | 215 | |
df8a16a1 DJ |
216 | block_set_scope (block, |
217 | obsavestring (name, prefix_len, obstack), | |
218 | obstack); | |
9219021c DC |
219 | } |
220 | } | |
221 | ||
222 | /* Test whether or not NAMESPACE looks like it mentions an anonymous | |
223 | namespace; return nonzero if so. */ | |
224 | ||
225 | int | |
226 | cp_is_anonymous (const char *namespace) | |
227 | { | |
228 | return (strstr (namespace, "(anonymous namespace)") | |
229 | != NULL); | |
230 | } | |
231 | ||
1fcb5155 DC |
232 | /* The C++-specific version of name lookup for static and global |
233 | names. This makes sure that names get looked for in all namespaces | |
234 | that are in scope. NAME is the natural name of the symbol that | |
94af9270 | 235 | we're looking for, BLOCK is the block that we're searching within, |
aff410f1 MS |
236 | DOMAIN says what kind of symbols we're looking for, and if SYMTAB |
237 | is non-NULL, we should store the symtab where we found the symbol | |
238 | in it. */ | |
1fcb5155 DC |
239 | |
240 | struct symbol * | |
241 | cp_lookup_symbol_nonlocal (const char *name, | |
1fcb5155 | 242 | const struct block *block, |
21b556f4 | 243 | const domain_enum domain) |
1fcb5155 | 244 | { |
8540c487 SW |
245 | struct symbol *sym; |
246 | const char *scope = block_scope (block); | |
247 | ||
aff410f1 MS |
248 | sym = lookup_namespace_scope (name, block, |
249 | domain, scope, 0); | |
8540c487 SW |
250 | if (sym != NULL) |
251 | return sym; | |
252 | ||
aff410f1 MS |
253 | return cp_lookup_symbol_namespace (scope, name, |
254 | block, domain); | |
8540c487 SW |
255 | } |
256 | ||
aff410f1 MS |
257 | /* Look up NAME in the C++ namespace NAMESPACE. Other arguments are |
258 | as in cp_lookup_symbol_nonlocal. */ | |
8540c487 SW |
259 | |
260 | static struct symbol * | |
261 | cp_lookup_symbol_in_namespace (const char *namespace, | |
262 | const char *name, | |
8540c487 SW |
263 | const struct block *block, |
264 | const domain_enum domain) | |
265 | { | |
266 | if (namespace[0] == '\0') | |
267 | { | |
94af9270 | 268 | return lookup_symbol_file (name, block, domain, 0); |
8540c487 SW |
269 | } |
270 | else | |
271 | { | |
aff410f1 MS |
272 | char *concatenated_name = alloca (strlen (namespace) + 2 |
273 | + strlen (name) + 1); | |
c5504eaf | 274 | |
8540c487 SW |
275 | strcpy (concatenated_name, namespace); |
276 | strcat (concatenated_name, "::"); | |
277 | strcat (concatenated_name, name); | |
aff410f1 MS |
278 | return lookup_symbol_file (concatenated_name, block, domain, |
279 | cp_is_anonymous (namespace)); | |
8540c487 SW |
280 | } |
281 | } | |
282 | ||
b14e635e SW |
283 | /* Used for cleanups to reset the "searched" flag incase |
284 | of an error. */ | |
285 | ||
286 | static void | |
287 | reset_directive_searched (void *data) | |
288 | { | |
289 | struct using_direct *direct = data; | |
290 | direct->searched = 0; | |
291 | } | |
292 | ||
aff410f1 MS |
293 | /* Search for NAME by applying all import statements belonging to |
294 | BLOCK which are applicable in SCOPE. If DECLARATION_ONLY the | |
295 | search is restricted to using declarations. | |
13387711 SW |
296 | Example: |
297 | ||
aff410f1 | 298 | namespace A { |
13387711 SW |
299 | int x; |
300 | } | |
301 | using A::x; | |
302 | ||
aff410f1 MS |
303 | If SEARCH_PARENTS the search will include imports which are |
304 | applicable in parents of SCOPE. | |
b14e635e SW |
305 | Example: |
306 | ||
aff410f1 | 307 | namespace A { |
b14e635e | 308 | using namespace X; |
aff410f1 | 309 | namespace B { |
b14e635e SW |
310 | using namespace Y; |
311 | } | |
312 | } | |
313 | ||
aff410f1 MS |
314 | If SCOPE is "A::B" and SEARCH_PARENTS is true the imports of |
315 | namespaces X and Y will be considered. If SEARCH_PARENTS is false | |
316 | only the import of Y is considered. */ | |
8540c487 | 317 | |
13387711 | 318 | struct symbol * |
8540c487 SW |
319 | cp_lookup_symbol_imports (const char *scope, |
320 | const char *name, | |
8540c487 | 321 | const struct block *block, |
b14e635e | 322 | const domain_enum domain, |
13387711 | 323 | const int declaration_only, |
b14e635e | 324 | const int search_parents) |
8540c487 | 325 | { |
b14e635e | 326 | struct using_direct *current; |
13387711 | 327 | struct symbol *sym = NULL; |
8540c487 | 328 | int len; |
b14e635e SW |
329 | int directive_match; |
330 | struct cleanup *searched_cleanup; | |
8540c487 SW |
331 | |
332 | /* First, try to find the symbol in the given namespace. */ | |
13387711 | 333 | if (!declaration_only) |
aff410f1 MS |
334 | sym = cp_lookup_symbol_in_namespace (scope, name, |
335 | block, domain); | |
13387711 | 336 | |
8540c487 SW |
337 | if (sym != NULL) |
338 | return sym; | |
339 | ||
aff410f1 MS |
340 | /* Go through the using directives. If any of them add new names to |
341 | the namespace we're searching in, see if we can find a match by | |
342 | applying them. */ | |
8540c487 SW |
343 | |
344 | for (current = block_using (block); | |
345 | current != NULL; | |
346 | current = current->next) | |
347 | { | |
b14e635e SW |
348 | len = strlen (current->import_dest); |
349 | directive_match = (search_parents | |
350 | ? (strncmp (scope, current->import_dest, | |
351 | strlen (current->import_dest)) == 0 | |
352 | && (len == 0 | |
aff410f1 MS |
353 | || scope[len] == ':' |
354 | || scope[len] == '\0')) | |
b14e635e | 355 | : strcmp (scope, current->import_dest) == 0); |
8540c487 | 356 | |
aff410f1 MS |
357 | /* If the import destination is the current scope or one of its |
358 | ancestors then it is applicable. */ | |
b14e635e | 359 | if (directive_match && !current->searched) |
8540c487 | 360 | { |
aff410f1 MS |
361 | /* Mark this import as searched so that the recursive call |
362 | does not search it again. */ | |
b14e635e | 363 | current->searched = 1; |
aff410f1 MS |
364 | searched_cleanup = make_cleanup (reset_directive_searched, |
365 | current); | |
366 | ||
367 | /* If there is an import of a single declaration, compare the | |
368 | imported declaration (after optional renaming by its alias) | |
369 | with the sought out name. If there is a match pass | |
370 | current->import_src as NAMESPACE to direct the search | |
371 | towards the imported namespace. */ | |
1ac77ea1 | 372 | if (current->declaration |
aff410f1 MS |
373 | && strcmp (name, current->alias |
374 | ? current->alias : current->declaration) == 0) | |
13387711 | 375 | sym = cp_lookup_symbol_in_namespace (current->import_src, |
aff410f1 MS |
376 | current->declaration, |
377 | block, domain); | |
13387711 | 378 | |
aff410f1 MS |
379 | /* If this is a DECLARATION_ONLY search or a symbol was found |
380 | or this import statement was an import declaration, the | |
381 | search of this import is complete. */ | |
13387711 SW |
382 | if (declaration_only || sym != NULL || current->declaration) |
383 | { | |
384 | current->searched = 0; | |
385 | discard_cleanups (searched_cleanup); | |
386 | ||
387 | if (sym != NULL) | |
388 | return sym; | |
389 | ||
390 | continue; | |
391 | } | |
392 | ||
aff410f1 MS |
393 | if (current->alias != NULL |
394 | && strcmp (name, current->alias) == 0) | |
395 | /* If the import is creating an alias and the alias matches | |
396 | the sought name. Pass current->import_src as the NAME to | |
397 | direct the search towards the aliased namespace. */ | |
82856980 SW |
398 | { |
399 | sym = cp_lookup_symbol_in_namespace (scope, | |
aff410f1 MS |
400 | current->import_src, |
401 | block, domain); | |
82856980 SW |
402 | } |
403 | else if (current->alias == NULL) | |
404 | { | |
aff410f1 MS |
405 | /* If this import statement creates no alias, pass |
406 | current->inner as NAMESPACE to direct the search | |
407 | towards the imported namespace. */ | |
82856980 | 408 | sym = cp_lookup_symbol_imports (current->import_src, |
aff410f1 MS |
409 | name, block, |
410 | domain, 0, 0); | |
82856980 | 411 | } |
b14e635e SW |
412 | current->searched = 0; |
413 | discard_cleanups (searched_cleanup); | |
414 | ||
415 | if (sym != NULL) | |
416 | return sym; | |
8540c487 SW |
417 | } |
418 | } | |
419 | ||
420 | return NULL; | |
421 | } | |
422 | ||
34eaf542 TT |
423 | /* Helper function that searches an array of symbols for one named |
424 | NAME. */ | |
425 | ||
426 | static struct symbol * | |
aff410f1 MS |
427 | search_symbol_list (const char *name, int num, |
428 | struct symbol **syms) | |
34eaf542 TT |
429 | { |
430 | int i; | |
431 | ||
432 | /* Maybe we should store a dictionary in here instead. */ | |
433 | for (i = 0; i < num; ++i) | |
434 | { | |
435 | if (strcmp (name, SYMBOL_NATURAL_NAME (syms[i])) == 0) | |
436 | return syms[i]; | |
437 | } | |
438 | return NULL; | |
439 | } | |
440 | ||
441 | /* Like cp_lookup_symbol_imports, but if BLOCK is a function, it | |
442 | searches through the template parameters of the function and the | |
443 | function's type. */ | |
444 | ||
445 | struct symbol * | |
446 | cp_lookup_symbol_imports_or_template (const char *scope, | |
447 | const char *name, | |
448 | const struct block *block, | |
449 | const domain_enum domain) | |
450 | { | |
451 | struct symbol *function = BLOCK_FUNCTION (block); | |
452 | ||
453 | if (function != NULL && SYMBOL_LANGUAGE (function) == language_cplus) | |
454 | { | |
455 | int i; | |
456 | struct cplus_specific *cps | |
457 | = function->ginfo.language_specific.cplus_specific; | |
458 | ||
459 | /* Search the function's template parameters. */ | |
460 | if (SYMBOL_IS_CPLUS_TEMPLATE_FUNCTION (function)) | |
461 | { | |
aff410f1 MS |
462 | struct template_symbol *templ |
463 | = (struct template_symbol *) function; | |
34eaf542 TT |
464 | struct symbol *result; |
465 | ||
466 | result = search_symbol_list (name, | |
467 | templ->n_template_arguments, | |
468 | templ->template_arguments); | |
469 | if (result != NULL) | |
470 | return result; | |
471 | } | |
472 | ||
473 | /* Search the template parameters of the function's defining | |
474 | context. */ | |
475 | if (SYMBOL_NATURAL_NAME (function)) | |
476 | { | |
477 | struct type *context; | |
478 | char *name_copy = xstrdup (SYMBOL_NATURAL_NAME (function)); | |
479 | struct cleanup *cleanups = make_cleanup (xfree, name_copy); | |
480 | const struct language_defn *lang = language_def (language_cplus); | |
481 | struct gdbarch *arch = SYMBOL_SYMTAB (function)->objfile->gdbarch; | |
482 | const struct block *parent = BLOCK_SUPERBLOCK (block); | |
483 | ||
484 | while (1) | |
485 | { | |
486 | struct symbol *result; | |
487 | unsigned int prefix_len = cp_entire_prefix_len (name_copy); | |
488 | ||
489 | if (prefix_len == 0) | |
490 | context = NULL; | |
491 | else | |
492 | { | |
493 | name_copy[prefix_len] = '\0'; | |
aff410f1 MS |
494 | context = lookup_typename (lang, arch, |
495 | name_copy, | |
496 | parent, 1); | |
34eaf542 TT |
497 | } |
498 | ||
499 | if (context == NULL) | |
500 | break; | |
501 | ||
aff410f1 MS |
502 | result |
503 | = search_symbol_list (name, | |
504 | TYPE_N_TEMPLATE_ARGUMENTS (context), | |
505 | TYPE_TEMPLATE_ARGUMENTS (context)); | |
34eaf542 TT |
506 | if (result != NULL) |
507 | return result; | |
508 | } | |
509 | ||
510 | do_cleanups (cleanups); | |
511 | } | |
512 | } | |
513 | ||
514 | return cp_lookup_symbol_imports (scope, name, block, domain, 1, 1); | |
515 | } | |
516 | ||
aff410f1 MS |
517 | /* Searches for NAME in the current namespace, and by applying |
518 | relevant import statements belonging to BLOCK and its parents. | |
519 | SCOPE is the namespace scope of the context in which the search is | |
520 | being evaluated. */ | |
8540c487 SW |
521 | |
522 | struct symbol* | |
523 | cp_lookup_symbol_namespace (const char *scope, | |
524 | const char *name, | |
8540c487 | 525 | const struct block *block, |
13387711 | 526 | const domain_enum domain) |
8540c487 SW |
527 | { |
528 | struct symbol *sym; | |
13387711 SW |
529 | |
530 | /* First, try to find the symbol in the given namespace. */ | |
aff410f1 MS |
531 | sym = cp_lookup_symbol_in_namespace (scope, name, |
532 | block, domain); | |
13387711 SW |
533 | if (sym != NULL) |
534 | return sym; | |
8540c487 | 535 | |
aff410f1 MS |
536 | /* Search for name in namespaces imported to this and parent |
537 | blocks. */ | |
8540c487 SW |
538 | while (block != NULL) |
539 | { | |
aff410f1 MS |
540 | sym = cp_lookup_symbol_imports (scope, name, block, |
541 | domain, 0, 1); | |
8540c487 SW |
542 | |
543 | if (sym) | |
544 | return sym; | |
545 | ||
546 | block = BLOCK_SUPERBLOCK (block); | |
547 | } | |
548 | ||
549 | return NULL; | |
1fcb5155 DC |
550 | } |
551 | ||
552 | /* Lookup NAME at namespace scope (or, in C terms, in static and | |
553 | global variables). SCOPE is the namespace that the current | |
554 | function is defined within; only consider namespaces whose length | |
555 | is at least SCOPE_LEN. Other arguments are as in | |
556 | cp_lookup_symbol_nonlocal. | |
557 | ||
558 | For example, if we're within a function A::B::f and looking for a | |
3882f37a | 559 | symbol x, this will get called with NAME = "x", SCOPE = "A::B", and |
1fcb5155 DC |
560 | SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same, |
561 | but with SCOPE_LEN = 1. And then it calls itself with NAME and | |
562 | SCOPE the same, but with SCOPE_LEN = 4. This third call looks for | |
563 | "A::B::x"; if it doesn't find it, then the second call looks for | |
564 | "A::x", and if that call fails, then the first call looks for | |
565 | "x". */ | |
566 | ||
567 | static struct symbol * | |
568 | lookup_namespace_scope (const char *name, | |
1fcb5155 DC |
569 | const struct block *block, |
570 | const domain_enum domain, | |
1fcb5155 DC |
571 | const char *scope, |
572 | int scope_len) | |
573 | { | |
574 | char *namespace; | |
575 | ||
576 | if (scope[scope_len] != '\0') | |
577 | { | |
578 | /* Recursively search for names in child namespaces first. */ | |
579 | ||
580 | struct symbol *sym; | |
581 | int new_scope_len = scope_len; | |
582 | ||
583 | /* If the current scope is followed by "::", skip past that. */ | |
584 | if (new_scope_len != 0) | |
585 | { | |
586 | gdb_assert (scope[new_scope_len] == ':'); | |
587 | new_scope_len += 2; | |
588 | } | |
589 | new_scope_len += cp_find_first_component (scope + new_scope_len); | |
aff410f1 MS |
590 | sym = lookup_namespace_scope (name, block, domain, |
591 | scope, new_scope_len); | |
1fcb5155 DC |
592 | if (sym != NULL) |
593 | return sym; | |
594 | } | |
595 | ||
596 | /* Okay, we didn't find a match in our children, so look for the | |
597 | name in the current namespace. */ | |
598 | ||
599 | namespace = alloca (scope_len + 1); | |
600 | strncpy (namespace, scope, scope_len); | |
601 | namespace[scope_len] = '\0'; | |
aff410f1 MS |
602 | return cp_lookup_symbol_in_namespace (namespace, name, |
603 | block, domain); | |
1fcb5155 DC |
604 | } |
605 | ||
606 | /* Look up NAME in BLOCK's static block and in global blocks. If | |
607 | ANONYMOUS_NAMESPACE is nonzero, the symbol in question is located | |
608 | within an anonymous namespace. Other arguments are as in | |
609 | cp_lookup_symbol_nonlocal. */ | |
610 | ||
611 | static struct symbol * | |
612 | lookup_symbol_file (const char *name, | |
1fcb5155 DC |
613 | const struct block *block, |
614 | const domain_enum domain, | |
1fcb5155 DC |
615 | int anonymous_namespace) |
616 | { | |
617 | struct symbol *sym = NULL; | |
618 | ||
94af9270 | 619 | sym = lookup_symbol_static (name, block, domain); |
1fcb5155 DC |
620 | if (sym != NULL) |
621 | return sym; | |
622 | ||
623 | if (anonymous_namespace) | |
624 | { | |
625 | /* Symbols defined in anonymous namespaces have external linkage | |
626 | but should be treated as local to a single file nonetheless. | |
627 | So we only search the current file's global block. */ | |
628 | ||
629 | const struct block *global_block = block_global_block (block); | |
630 | ||
631 | if (global_block != NULL) | |
94af9270 | 632 | sym = lookup_symbol_aux_block (name, global_block, domain); |
1fcb5155 DC |
633 | } |
634 | else | |
635 | { | |
94af9270 | 636 | sym = lookup_symbol_global (name, block, domain); |
5c4e30ca DC |
637 | } |
638 | ||
639 | if (sym != NULL) | |
640 | return sym; | |
641 | ||
642 | /* Now call "lookup_possible_namespace_symbol". Symbols in here | |
643 | claim to be associated to namespaces, but this claim might be | |
644 | incorrect: the names in question might actually correspond to | |
645 | classes instead of namespaces. But if they correspond to | |
646 | classes, then we should have found a match for them above. So if | |
647 | we find them now, they should be genuine. */ | |
648 | ||
649 | /* FIXME: carlton/2003-06-12: This is a hack and should eventually | |
650 | be deleted: see comments below. */ | |
651 | ||
652 | if (domain == VAR_DOMAIN) | |
653 | { | |
21b556f4 | 654 | sym = lookup_possible_namespace_symbol (name); |
5c4e30ca DC |
655 | if (sym != NULL) |
656 | return sym; | |
657 | } | |
658 | ||
659 | return NULL; | |
660 | } | |
661 | ||
79c2c32d DC |
662 | /* Look up a type named NESTED_NAME that is nested inside the C++ |
663 | class or namespace given by PARENT_TYPE, from within the context | |
664 | given by BLOCK. Return NULL if there is no such nested type. */ | |
665 | ||
79c2c32d DC |
666 | struct type * |
667 | cp_lookup_nested_type (struct type *parent_type, | |
668 | const char *nested_name, | |
669 | const struct block *block) | |
670 | { | |
671 | switch (TYPE_CODE (parent_type)) | |
672 | { | |
63d06c5c | 673 | case TYPE_CODE_STRUCT: |
79c2c32d | 674 | case TYPE_CODE_NAMESPACE: |
48e32051 | 675 | case TYPE_CODE_UNION: |
79c2c32d | 676 | { |
63d06c5c DC |
677 | /* NOTE: carlton/2003-11-10: We don't treat C++ class members |
678 | of classes like, say, data or function members. Instead, | |
679 | they're just represented by symbols whose names are | |
680 | qualified by the name of the surrounding class. This is | |
681 | just like members of namespaces; in particular, | |
682 | lookup_symbol_namespace works when looking them up. */ | |
683 | ||
79c2c32d | 684 | const char *parent_name = TYPE_TAG_NAME (parent_type); |
aff410f1 MS |
685 | struct symbol *sym |
686 | = cp_lookup_symbol_in_namespace (parent_name, nested_name, | |
687 | block, VAR_DOMAIN); | |
41f62f39 | 688 | char *concatenated_name; |
c5504eaf | 689 | |
41f62f39 | 690 | if (sym != NULL && SYMBOL_CLASS (sym) == LOC_TYPEDEF) |
79c2c32d | 691 | return SYMBOL_TYPE (sym); |
41f62f39 | 692 | |
aff410f1 MS |
693 | /* Now search all static file-level symbols. Not strictly |
694 | correct, but more useful than an error. We do not try to | |
695 | guess any imported namespace as even the fully specified | |
696 | namespace seach is is already not C++ compliant and more | |
697 | assumptions could make it too magic. */ | |
41f62f39 JK |
698 | |
699 | concatenated_name = alloca (strlen (parent_name) + 2 | |
700 | + strlen (nested_name) + 1); | |
aff410f1 MS |
701 | sprintf (concatenated_name, "%s::%s", |
702 | parent_name, nested_name); | |
703 | sym = lookup_static_symbol_aux (concatenated_name, | |
704 | VAR_DOMAIN); | |
41f62f39 JK |
705 | if (sym != NULL && SYMBOL_CLASS (sym) == LOC_TYPEDEF) |
706 | return SYMBOL_TYPE (sym); | |
707 | ||
708 | return NULL; | |
79c2c32d DC |
709 | } |
710 | default: | |
711 | internal_error (__FILE__, __LINE__, | |
3e43a32a MS |
712 | _("cp_lookup_nested_type called " |
713 | "on a non-aggregate type.")); | |
79c2c32d DC |
714 | } |
715 | } | |
716 | ||
b368761e DC |
717 | /* The C++-version of lookup_transparent_type. */ |
718 | ||
719 | /* FIXME: carlton/2004-01-16: The problem that this is trying to | |
720 | address is that, unfortunately, sometimes NAME is wrong: it may not | |
721 | include the name of namespaces enclosing the type in question. | |
722 | lookup_transparent_type gets called when the the type in question | |
723 | is a declaration, and we're trying to find its definition; but, for | |
724 | declarations, our type name deduction mechanism doesn't work. | |
725 | There's nothing we can do to fix this in general, I think, in the | |
726 | absence of debug information about namespaces (I've filed PR | |
727 | gdb/1511 about this); until such debug information becomes more | |
728 | prevalent, one heuristic which sometimes looks is to search for the | |
729 | definition in namespaces containing the current namespace. | |
730 | ||
731 | We should delete this functions once the appropriate debug | |
732 | information becomes more widespread. (GCC 3.4 will be the first | |
733 | released version of GCC with such information.) */ | |
734 | ||
735 | struct type * | |
736 | cp_lookup_transparent_type (const char *name) | |
737 | { | |
738 | /* First, try the honest way of looking up the definition. */ | |
739 | struct type *t = basic_lookup_transparent_type (name); | |
740 | const char *scope; | |
741 | ||
742 | if (t != NULL) | |
743 | return t; | |
744 | ||
745 | /* If that doesn't work and we're within a namespace, look there | |
746 | instead. */ | |
747 | scope = block_scope (get_selected_block (0)); | |
748 | ||
749 | if (scope[0] == '\0') | |
750 | return NULL; | |
751 | ||
752 | return cp_lookup_transparent_type_loop (name, scope, 0); | |
753 | } | |
754 | ||
755 | /* Lookup the the type definition associated to NAME in | |
756 | namespaces/classes containing SCOPE whose name is strictly longer | |
aff410f1 MS |
757 | than LENGTH. LENGTH must be the index of the start of a component |
758 | of SCOPE. */ | |
b368761e DC |
759 | |
760 | static struct type * | |
aff410f1 MS |
761 | cp_lookup_transparent_type_loop (const char *name, |
762 | const char *scope, | |
b368761e DC |
763 | int length) |
764 | { | |
1198ecbe | 765 | int scope_length = length + cp_find_first_component (scope + length); |
b368761e DC |
766 | char *full_name; |
767 | ||
768 | /* If the current scope is followed by "::", look in the next | |
769 | component. */ | |
770 | if (scope[scope_length] == ':') | |
771 | { | |
772 | struct type *retval | |
aff410f1 MS |
773 | = cp_lookup_transparent_type_loop (name, scope, |
774 | scope_length + 2); | |
c5504eaf | 775 | |
b368761e DC |
776 | if (retval != NULL) |
777 | return retval; | |
778 | } | |
779 | ||
780 | full_name = alloca (scope_length + 2 + strlen (name) + 1); | |
781 | strncpy (full_name, scope, scope_length); | |
782 | strncpy (full_name + scope_length, "::", 2); | |
783 | strcpy (full_name + scope_length + 2, name); | |
784 | ||
785 | return basic_lookup_transparent_type (full_name); | |
786 | } | |
787 | ||
5c4e30ca DC |
788 | /* Now come functions for dealing with symbols associated to |
789 | namespaces. (They're used to store the namespaces themselves, not | |
790 | objects that live in the namespaces.) These symbols come in two | |
791 | varieties: if we run into a DW_TAG_namespace DIE, then we know that | |
792 | we have a namespace, so dwarf2read.c creates a symbol for it just | |
793 | like normal. But, unfortunately, versions of GCC through at least | |
794 | 3.3 don't generate those DIE's. Our solution is to try to guess | |
795 | their existence by looking at demangled names. This might cause us | |
796 | to misidentify classes as namespaces, however. So we put those | |
797 | symbols in a special block (one per objfile), and we only search | |
798 | that block as a last resort. */ | |
799 | ||
800 | /* FIXME: carlton/2003-06-12: Once versions of GCC that generate | |
801 | DW_TAG_namespace have been out for a year or two, we should get rid | |
802 | of all of this "possible namespace" nonsense. */ | |
803 | ||
804 | /* Allocate everything necessary for the possible namespace block | |
805 | associated to OBJFILE. */ | |
806 | ||
807 | static void | |
808 | initialize_namespace_symtab (struct objfile *objfile) | |
809 | { | |
810 | struct symtab *namespace_symtab; | |
811 | struct blockvector *bv; | |
812 | struct block *bl; | |
813 | ||
814 | namespace_symtab = allocate_symtab ("<<C++-namespaces>>", objfile); | |
815 | namespace_symtab->language = language_cplus; | |
816 | namespace_symtab->free_code = free_nothing; | |
817 | namespace_symtab->dirname = NULL; | |
818 | ||
4a146b47 | 819 | bv = obstack_alloc (&objfile->objfile_obstack, |
5c4e30ca DC |
820 | sizeof (struct blockvector) |
821 | + FIRST_LOCAL_BLOCK * sizeof (struct block *)); | |
822 | BLOCKVECTOR_NBLOCKS (bv) = FIRST_LOCAL_BLOCK + 1; | |
823 | BLOCKVECTOR (namespace_symtab) = bv; | |
824 | ||
aff410f1 | 825 | /* Allocate empty GLOBAL_BLOCK and STATIC_BLOCK. */ |
5c4e30ca | 826 | |
4a146b47 EZ |
827 | bl = allocate_block (&objfile->objfile_obstack); |
828 | BLOCK_DICT (bl) = dict_create_linear (&objfile->objfile_obstack, | |
5c4e30ca DC |
829 | NULL); |
830 | BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK) = bl; | |
4a146b47 EZ |
831 | bl = allocate_block (&objfile->objfile_obstack); |
832 | BLOCK_DICT (bl) = dict_create_linear (&objfile->objfile_obstack, | |
5c4e30ca DC |
833 | NULL); |
834 | BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK) = bl; | |
835 | ||
836 | /* Allocate the possible namespace block; we put it where the first | |
837 | local block will live, though I don't think there's any need to | |
838 | pretend that it's actually a local block (e.g. by setting | |
839 | BLOCK_SUPERBLOCK appropriately). We don't use the global or | |
840 | static block because we don't want it searched during the normal | |
841 | search of all global/static blocks in lookup_symbol: we only want | |
842 | it used as a last resort. */ | |
843 | ||
844 | /* NOTE: carlton/2003-09-11: I considered not associating the fake | |
845 | symbols to a block/symtab at all. But that would cause problems | |
846 | with lookup_symbol's SYMTAB argument and with block_found, so | |
847 | having a symtab/block for this purpose seems like the best | |
848 | solution for now. */ | |
849 | ||
4a146b47 | 850 | bl = allocate_block (&objfile->objfile_obstack); |
5c4e30ca DC |
851 | BLOCK_DICT (bl) = dict_create_hashed_expandable (); |
852 | BLOCKVECTOR_BLOCK (bv, FIRST_LOCAL_BLOCK) = bl; | |
853 | ||
854 | namespace_symtab->free_func = free_namespace_block; | |
855 | ||
856 | objfile->cp_namespace_symtab = namespace_symtab; | |
857 | } | |
858 | ||
859 | /* Locate the possible namespace block associated to OBJFILE, | |
860 | allocating it if necessary. */ | |
861 | ||
862 | static struct block * | |
863 | get_possible_namespace_block (struct objfile *objfile) | |
864 | { | |
865 | if (objfile->cp_namespace_symtab == NULL) | |
866 | initialize_namespace_symtab (objfile); | |
867 | ||
868 | return BLOCKVECTOR_BLOCK (BLOCKVECTOR (objfile->cp_namespace_symtab), | |
869 | FIRST_LOCAL_BLOCK); | |
870 | } | |
871 | ||
872 | /* Free the dictionary associated to the possible namespace block. */ | |
873 | ||
874 | static void | |
875 | free_namespace_block (struct symtab *symtab) | |
876 | { | |
877 | struct block *possible_namespace_block; | |
878 | ||
879 | possible_namespace_block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), | |
880 | FIRST_LOCAL_BLOCK); | |
881 | gdb_assert (possible_namespace_block != NULL); | |
882 | dict_free (BLOCK_DICT (possible_namespace_block)); | |
883 | } | |
884 | ||
885 | /* Ensure that there are symbols in the possible namespace block | |
886 | associated to OBJFILE for all initial substrings of NAME that look | |
887 | like namespaces or classes. NAME should end in a member variable: | |
888 | it shouldn't consist solely of namespaces. */ | |
889 | ||
890 | void | |
aff410f1 MS |
891 | cp_check_possible_namespace_symbols (const char *name, |
892 | struct objfile *objfile) | |
5c4e30ca DC |
893 | { |
894 | check_possible_namespace_symbols_loop (name, | |
895 | cp_find_first_component (name), | |
896 | objfile); | |
897 | } | |
898 | ||
899 | /* This is a helper loop for cp_check_possible_namespace_symbols; it | |
900 | ensures that there are symbols in the possible namespace block | |
901 | associated to OBJFILE for all namespaces that are initial | |
902 | substrings of NAME of length at least LEN. It returns 1 if a | |
903 | previous loop had already created the shortest such symbol and 0 | |
904 | otherwise. | |
905 | ||
906 | This function assumes that if there is already a symbol associated | |
907 | to a substring of NAME of a given length, then there are already | |
908 | symbols associated to all substrings of NAME whose length is less | |
909 | than that length. So if cp_check_possible_namespace_symbols has | |
910 | been called once with argument "A::B::C::member", then that will | |
911 | create symbols "A", "A::B", and "A::B::C". If it is then later | |
912 | called with argument "A::B::D::member", then the new call will | |
913 | generate a new symbol for "A::B::D", but once it sees that "A::B" | |
914 | has already been created, it doesn't bother checking to see if "A" | |
915 | has also been created. */ | |
916 | ||
917 | static int | |
918 | check_possible_namespace_symbols_loop (const char *name, int len, | |
919 | struct objfile *objfile) | |
920 | { | |
921 | if (name[len] == ':') | |
922 | { | |
923 | int done; | |
924 | int next_len = len + 2; | |
925 | ||
926 | next_len += cp_find_first_component (name + next_len); | |
927 | done = check_possible_namespace_symbols_loop (name, next_len, | |
928 | objfile); | |
929 | ||
930 | if (!done) | |
aff410f1 MS |
931 | done = check_one_possible_namespace_symbol (name, len, |
932 | objfile); | |
5c4e30ca DC |
933 | |
934 | return done; | |
1fcb5155 | 935 | } |
5c4e30ca DC |
936 | else |
937 | return 0; | |
938 | } | |
939 | ||
940 | /* Check to see if there's already a possible namespace symbol in | |
941 | OBJFILE whose name is the initial substring of NAME of length LEN. | |
942 | If not, create one and return 0; otherwise, return 1. */ | |
943 | ||
944 | static int | |
945 | check_one_possible_namespace_symbol (const char *name, int len, | |
946 | struct objfile *objfile) | |
947 | { | |
948 | struct block *block = get_possible_namespace_block (objfile); | |
ec5cdd75 DJ |
949 | char *name_copy = alloca (len + 1); |
950 | struct symbol *sym; | |
951 | ||
952 | memcpy (name_copy, name, len); | |
953 | name_copy[len] = '\0'; | |
94af9270 | 954 | sym = lookup_block_symbol (block, name_copy, VAR_DOMAIN); |
5c4e30ca DC |
955 | |
956 | if (sym == NULL) | |
957 | { | |
ec5cdd75 | 958 | struct type *type; |
ec5cdd75 | 959 | |
aff410f1 MS |
960 | type = init_type (TYPE_CODE_NAMESPACE, 0, 0, |
961 | name_copy, objfile); | |
ec5cdd75 | 962 | |
5c4e30ca DC |
963 | TYPE_TAG_NAME (type) = TYPE_NAME (type); |
964 | ||
aff410f1 MS |
965 | sym = obstack_alloc (&objfile->objfile_obstack, |
966 | sizeof (struct symbol)); | |
5c4e30ca | 967 | memset (sym, 0, sizeof (struct symbol)); |
33e5013e | 968 | SYMBOL_SET_LANGUAGE (sym, language_cplus); |
04a679b8 TT |
969 | /* Note that init_type copied the name to the objfile's |
970 | obstack. */ | |
971 | SYMBOL_SET_NAMES (sym, TYPE_NAME (type), len, 0, objfile); | |
5c4e30ca DC |
972 | SYMBOL_CLASS (sym) = LOC_TYPEDEF; |
973 | SYMBOL_TYPE (sym) = type; | |
974 | SYMBOL_DOMAIN (sym) = VAR_DOMAIN; | |
975 | ||
976 | dict_add_symbol (BLOCK_DICT (block), sym); | |
977 | ||
978 | return 0; | |
979 | } | |
980 | else | |
ec5cdd75 | 981 | return 1; |
5c4e30ca DC |
982 | } |
983 | ||
984 | /* Look for a symbol named NAME in all the possible namespace blocks. | |
21b556f4 | 985 | If one is found, return it. */ |
5c4e30ca DC |
986 | |
987 | static struct symbol * | |
21b556f4 | 988 | lookup_possible_namespace_symbol (const char *name) |
5c4e30ca DC |
989 | { |
990 | struct objfile *objfile; | |
991 | ||
992 | ALL_OBJFILES (objfile) | |
993 | { | |
994 | struct symbol *sym; | |
995 | ||
996 | sym = lookup_block_symbol (get_possible_namespace_block (objfile), | |
94af9270 | 997 | name, VAR_DOMAIN); |
5c4e30ca DC |
998 | |
999 | if (sym != NULL) | |
21b556f4 | 1000 | return sym; |
5c4e30ca DC |
1001 | } |
1002 | ||
1003 | return NULL; | |
1004 | } | |
1005 | ||
1006 | /* Print out all the possible namespace symbols. */ | |
1007 | ||
1008 | static void | |
1009 | maintenance_cplus_namespace (char *args, int from_tty) | |
1010 | { | |
1011 | struct objfile *objfile; | |
c5504eaf | 1012 | |
a3f17187 | 1013 | printf_unfiltered (_("Possible namespaces:\n")); |
5c4e30ca DC |
1014 | ALL_OBJFILES (objfile) |
1015 | { | |
1016 | struct dict_iterator iter; | |
1017 | struct symbol *sym; | |
1018 | ||
aff410f1 MS |
1019 | ALL_BLOCK_SYMBOLS (get_possible_namespace_block (objfile), |
1020 | iter, sym) | |
5c4e30ca DC |
1021 | { |
1022 | printf_unfiltered ("%s\n", SYMBOL_PRINT_NAME (sym)); | |
1023 | } | |
1024 | } | |
1025 | } | |
1026 | ||
2c0b251b PA |
1027 | /* Provide a prototype to silence -Wmissing-prototypes. */ |
1028 | extern initialize_file_ftype _initialize_cp_namespace; | |
1029 | ||
5c4e30ca DC |
1030 | void |
1031 | _initialize_cp_namespace (void) | |
1032 | { | |
aff410f1 MS |
1033 | add_cmd ("namespace", class_maintenance, |
1034 | maintenance_cplus_namespace, | |
1a966eab | 1035 | _("Print the list of possible C++ namespaces."), |
5c4e30ca | 1036 | &maint_cplus_cmd_list); |
1fcb5155 | 1037 | } |