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 | ||
9219021c DC |
52 | /* Check to see if SYMBOL refers to an object contained within an |
53 | anonymous namespace; if so, add an appropriate using directive. */ | |
54 | ||
9219021c DC |
55 | void |
56 | cp_scan_for_anonymous_namespaces (const struct symbol *symbol) | |
57 | { | |
df8a16a1 | 58 | if (SYMBOL_DEMANGLED_NAME (symbol) != NULL) |
9219021c | 59 | { |
df8a16a1 | 60 | const char *name = SYMBOL_DEMANGLED_NAME (symbol); |
9219021c DC |
61 | unsigned int previous_component; |
62 | unsigned int next_component; | |
9219021c DC |
63 | |
64 | /* Start with a quick-and-dirty check for mention of "(anonymous | |
65 | namespace)". */ | |
66 | ||
67 | if (!cp_is_anonymous (name)) | |
68 | return; | |
69 | ||
70 | previous_component = 0; | |
71 | next_component = cp_find_first_component (name + previous_component); | |
72 | ||
73 | while (name[next_component] == ':') | |
74 | { | |
2b1dbab0 KS |
75 | if (((next_component - previous_component) |
76 | == CP_ANONYMOUS_NAMESPACE_LEN) | |
9219021c | 77 | && strncmp (name + previous_component, |
2b1dbab0 KS |
78 | CP_ANONYMOUS_NAMESPACE_STR, |
79 | CP_ANONYMOUS_NAMESPACE_LEN) == 0) | |
9219021c | 80 | { |
aff410f1 MS |
81 | int dest_len = (previous_component == 0 |
82 | ? 0 : previous_component - 2); | |
8c902bb1 | 83 | int src_len = next_component; |
794684b6 | 84 | |
8c902bb1 SW |
85 | char *dest = alloca (dest_len + 1); |
86 | char *src = alloca (src_len + 1); | |
794684b6 | 87 | |
8c902bb1 SW |
88 | memcpy (dest, name, dest_len); |
89 | memcpy (src, name, src_len); | |
794684b6 | 90 | |
8c902bb1 SW |
91 | dest[dest_len] = '\0'; |
92 | src[src_len] = '\0'; | |
794684b6 | 93 | |
9219021c DC |
94 | /* We've found a component of the name that's an |
95 | anonymous namespace. So add symbols in it to the | |
96 | namespace given by the previous component if there is | |
97 | one, or to the global namespace if there isn't. */ | |
13387711 | 98 | cp_add_using_directive (dest, src, NULL, NULL, |
c0cc3a76 | 99 | &SYMBOL_SYMTAB (symbol)->objfile->objfile_obstack); |
9219021c DC |
100 | } |
101 | /* The "+ 2" is for the "::". */ | |
102 | previous_component = next_component + 2; | |
103 | next_component = (previous_component | |
104 | + cp_find_first_component (name | |
105 | + previous_component)); | |
106 | } | |
107 | } | |
108 | } | |
109 | ||
c0cc3a76 | 110 | |
aff410f1 MS |
111 | /* Add a using directive to using_directives. If the using directive |
112 | in question has already been added, don't add it twice. | |
113 | ||
114 | Create a new struct using_direct which imports the namespace SRC | |
115 | into the scope DEST. ALIAS is the name of the imported namespace | |
116 | in the current scope. If ALIAS is NULL then the namespace is known | |
117 | by its original name. DECLARATION is the name if the imported | |
118 | varable if this is a declaration import (Eg. using A::x), otherwise | |
119 | it is NULL. The arguments are copied into newly allocated memory | |
120 | so they can be temporaries. */ | |
9219021c DC |
121 | |
122 | void | |
13387711 SW |
123 | cp_add_using_directive (const char *dest, |
124 | const char *src, | |
125 | const char *alias, | |
126 | const char *declaration, | |
c0cc3a76 | 127 | struct obstack *obstack) |
9219021c DC |
128 | { |
129 | struct using_direct *current; | |
130 | struct using_direct *new; | |
13387711 | 131 | |
9219021c DC |
132 | /* Has it already been added? */ |
133 | ||
27aa8d6a | 134 | for (current = using_directives; current != NULL; current = current->next) |
9219021c | 135 | { |
8c902bb1 | 136 | if (strcmp (current->import_src, src) == 0 |
c0cc3a76 SW |
137 | && strcmp (current->import_dest, dest) == 0 |
138 | && ((alias == NULL && current->alias == NULL) | |
139 | || (alias != NULL && current->alias != NULL | |
13387711 SW |
140 | && strcmp (alias, current->alias) == 0)) |
141 | && ((declaration == NULL && current->declaration == NULL) | |
142 | || (declaration != NULL && current->declaration != NULL | |
143 | && strcmp (declaration, current->declaration) == 0))) | |
9219021c DC |
144 | return; |
145 | } | |
146 | ||
c0cc3a76 SW |
147 | new = OBSTACK_ZALLOC (obstack, struct using_direct); |
148 | ||
149 | new->import_src = obsavestring (src, strlen (src), obstack); | |
150 | new->import_dest = obsavestring (dest, strlen (dest), obstack); | |
794684b6 | 151 | |
c0cc3a76 SW |
152 | if (alias != NULL) |
153 | new->alias = obsavestring (alias, strlen (alias), obstack); | |
154 | ||
13387711 SW |
155 | if (declaration != NULL) |
156 | new->declaration = obsavestring (declaration, strlen (declaration), | |
157 | obstack); | |
158 | ||
c0cc3a76 SW |
159 | new->next = using_directives; |
160 | using_directives = new; | |
9219021c DC |
161 | } |
162 | ||
163 | /* Record the namespace that the function defined by SYMBOL was | |
164 | defined in, if necessary. BLOCK is the associated block; use | |
165 | OBSTACK for allocation. */ | |
166 | ||
167 | void | |
168 | cp_set_block_scope (const struct symbol *symbol, | |
169 | struct block *block, | |
df8a16a1 DJ |
170 | struct obstack *obstack, |
171 | const char *processing_current_prefix, | |
172 | int processing_has_namespace_info) | |
9219021c | 173 | { |
df8a16a1 | 174 | if (processing_has_namespace_info) |
9219021c | 175 | { |
df8a16a1 DJ |
176 | block_set_scope |
177 | (block, obsavestring (processing_current_prefix, | |
178 | strlen (processing_current_prefix), | |
179 | obstack), | |
180 | obstack); | |
181 | } | |
182 | else if (SYMBOL_DEMANGLED_NAME (symbol) != NULL) | |
183 | { | |
184 | /* Try to figure out the appropriate namespace from the | |
185 | demangled name. */ | |
9219021c | 186 | |
df8a16a1 DJ |
187 | /* FIXME: carlton/2003-04-15: If the function in question is |
188 | a method of a class, the name will actually include the | |
189 | name of the class as well. This should be harmless, but | |
190 | is a little unfortunate. */ | |
9219021c | 191 | |
df8a16a1 DJ |
192 | const char *name = SYMBOL_DEMANGLED_NAME (symbol); |
193 | unsigned int prefix_len = cp_entire_prefix_len (name); | |
9219021c | 194 | |
df8a16a1 DJ |
195 | block_set_scope (block, |
196 | obsavestring (name, prefix_len, obstack), | |
197 | obstack); | |
9219021c DC |
198 | } |
199 | } | |
200 | ||
201 | /* Test whether or not NAMESPACE looks like it mentions an anonymous | |
202 | namespace; return nonzero if so. */ | |
203 | ||
204 | int | |
205 | cp_is_anonymous (const char *namespace) | |
206 | { | |
2b1dbab0 | 207 | return (strstr (namespace, CP_ANONYMOUS_NAMESPACE_STR) |
9219021c DC |
208 | != NULL); |
209 | } | |
210 | ||
1fcb5155 DC |
211 | /* The C++-specific version of name lookup for static and global |
212 | names. This makes sure that names get looked for in all namespaces | |
213 | that are in scope. NAME is the natural name of the symbol that | |
94af9270 | 214 | we're looking for, BLOCK is the block that we're searching within, |
aff410f1 MS |
215 | DOMAIN says what kind of symbols we're looking for, and if SYMTAB |
216 | is non-NULL, we should store the symtab where we found the symbol | |
217 | in it. */ | |
1fcb5155 DC |
218 | |
219 | struct symbol * | |
220 | cp_lookup_symbol_nonlocal (const char *name, | |
1fcb5155 | 221 | const struct block *block, |
21b556f4 | 222 | const domain_enum domain) |
1fcb5155 | 223 | { |
8540c487 SW |
224 | struct symbol *sym; |
225 | const char *scope = block_scope (block); | |
226 | ||
aff410f1 MS |
227 | sym = lookup_namespace_scope (name, block, |
228 | domain, scope, 0); | |
8540c487 SW |
229 | if (sym != NULL) |
230 | return sym; | |
231 | ||
aff410f1 MS |
232 | return cp_lookup_symbol_namespace (scope, name, |
233 | block, domain); | |
8540c487 SW |
234 | } |
235 | ||
aff410f1 MS |
236 | /* Look up NAME in the C++ namespace NAMESPACE. Other arguments are |
237 | as in cp_lookup_symbol_nonlocal. */ | |
8540c487 SW |
238 | |
239 | static struct symbol * | |
240 | cp_lookup_symbol_in_namespace (const char *namespace, | |
241 | const char *name, | |
8540c487 SW |
242 | const struct block *block, |
243 | const domain_enum domain) | |
244 | { | |
245 | if (namespace[0] == '\0') | |
246 | { | |
94af9270 | 247 | return lookup_symbol_file (name, block, domain, 0); |
8540c487 SW |
248 | } |
249 | else | |
250 | { | |
aff410f1 MS |
251 | char *concatenated_name = alloca (strlen (namespace) + 2 |
252 | + strlen (name) + 1); | |
c5504eaf | 253 | |
8540c487 SW |
254 | strcpy (concatenated_name, namespace); |
255 | strcat (concatenated_name, "::"); | |
256 | strcat (concatenated_name, name); | |
aff410f1 MS |
257 | return lookup_symbol_file (concatenated_name, block, domain, |
258 | cp_is_anonymous (namespace)); | |
8540c487 SW |
259 | } |
260 | } | |
261 | ||
b14e635e SW |
262 | /* Used for cleanups to reset the "searched" flag incase |
263 | of an error. */ | |
264 | ||
265 | static void | |
266 | reset_directive_searched (void *data) | |
267 | { | |
268 | struct using_direct *direct = data; | |
269 | direct->searched = 0; | |
270 | } | |
271 | ||
aff410f1 MS |
272 | /* Search for NAME by applying all import statements belonging to |
273 | BLOCK which are applicable in SCOPE. If DECLARATION_ONLY the | |
274 | search is restricted to using declarations. | |
13387711 SW |
275 | Example: |
276 | ||
aff410f1 | 277 | namespace A { |
13387711 SW |
278 | int x; |
279 | } | |
280 | using A::x; | |
281 | ||
aff410f1 MS |
282 | If SEARCH_PARENTS the search will include imports which are |
283 | applicable in parents of SCOPE. | |
b14e635e SW |
284 | Example: |
285 | ||
aff410f1 | 286 | namespace A { |
b14e635e | 287 | using namespace X; |
aff410f1 | 288 | namespace B { |
b14e635e SW |
289 | using namespace Y; |
290 | } | |
291 | } | |
292 | ||
aff410f1 MS |
293 | If SCOPE is "A::B" and SEARCH_PARENTS is true the imports of |
294 | namespaces X and Y will be considered. If SEARCH_PARENTS is false | |
295 | only the import of Y is considered. */ | |
8540c487 | 296 | |
13387711 | 297 | struct symbol * |
8540c487 SW |
298 | cp_lookup_symbol_imports (const char *scope, |
299 | const char *name, | |
8540c487 | 300 | const struct block *block, |
b14e635e | 301 | const domain_enum domain, |
13387711 | 302 | const int declaration_only, |
b14e635e | 303 | const int search_parents) |
8540c487 | 304 | { |
b14e635e | 305 | struct using_direct *current; |
13387711 | 306 | struct symbol *sym = NULL; |
8540c487 | 307 | int len; |
b14e635e SW |
308 | int directive_match; |
309 | struct cleanup *searched_cleanup; | |
8540c487 SW |
310 | |
311 | /* First, try to find the symbol in the given namespace. */ | |
13387711 | 312 | if (!declaration_only) |
aff410f1 MS |
313 | sym = cp_lookup_symbol_in_namespace (scope, name, |
314 | block, domain); | |
13387711 | 315 | |
8540c487 SW |
316 | if (sym != NULL) |
317 | return sym; | |
318 | ||
aff410f1 MS |
319 | /* Go through the using directives. If any of them add new names to |
320 | the namespace we're searching in, see if we can find a match by | |
321 | applying them. */ | |
8540c487 SW |
322 | |
323 | for (current = block_using (block); | |
324 | current != NULL; | |
325 | current = current->next) | |
326 | { | |
b14e635e SW |
327 | len = strlen (current->import_dest); |
328 | directive_match = (search_parents | |
329 | ? (strncmp (scope, current->import_dest, | |
330 | strlen (current->import_dest)) == 0 | |
331 | && (len == 0 | |
aff410f1 MS |
332 | || scope[len] == ':' |
333 | || scope[len] == '\0')) | |
b14e635e | 334 | : strcmp (scope, current->import_dest) == 0); |
8540c487 | 335 | |
aff410f1 MS |
336 | /* If the import destination is the current scope or one of its |
337 | ancestors then it is applicable. */ | |
b14e635e | 338 | if (directive_match && !current->searched) |
8540c487 | 339 | { |
aff410f1 MS |
340 | /* Mark this import as searched so that the recursive call |
341 | does not search it again. */ | |
b14e635e | 342 | current->searched = 1; |
aff410f1 MS |
343 | searched_cleanup = make_cleanup (reset_directive_searched, |
344 | current); | |
345 | ||
346 | /* If there is an import of a single declaration, compare the | |
347 | imported declaration (after optional renaming by its alias) | |
348 | with the sought out name. If there is a match pass | |
349 | current->import_src as NAMESPACE to direct the search | |
350 | towards the imported namespace. */ | |
1ac77ea1 | 351 | if (current->declaration |
aff410f1 MS |
352 | && strcmp (name, current->alias |
353 | ? current->alias : current->declaration) == 0) | |
13387711 | 354 | sym = cp_lookup_symbol_in_namespace (current->import_src, |
aff410f1 MS |
355 | current->declaration, |
356 | block, domain); | |
13387711 | 357 | |
aff410f1 MS |
358 | /* If this is a DECLARATION_ONLY search or a symbol was found |
359 | or this import statement was an import declaration, the | |
360 | search of this import is complete. */ | |
13387711 SW |
361 | if (declaration_only || sym != NULL || current->declaration) |
362 | { | |
363 | current->searched = 0; | |
364 | discard_cleanups (searched_cleanup); | |
365 | ||
366 | if (sym != NULL) | |
367 | return sym; | |
368 | ||
369 | continue; | |
370 | } | |
371 | ||
aff410f1 MS |
372 | if (current->alias != NULL |
373 | && strcmp (name, current->alias) == 0) | |
374 | /* If the import is creating an alias and the alias matches | |
375 | the sought name. Pass current->import_src as the NAME to | |
376 | direct the search towards the aliased namespace. */ | |
82856980 SW |
377 | { |
378 | sym = cp_lookup_symbol_in_namespace (scope, | |
aff410f1 MS |
379 | current->import_src, |
380 | block, domain); | |
82856980 SW |
381 | } |
382 | else if (current->alias == NULL) | |
383 | { | |
aff410f1 MS |
384 | /* If this import statement creates no alias, pass |
385 | current->inner as NAMESPACE to direct the search | |
386 | towards the imported namespace. */ | |
82856980 | 387 | sym = cp_lookup_symbol_imports (current->import_src, |
aff410f1 MS |
388 | name, block, |
389 | domain, 0, 0); | |
82856980 | 390 | } |
b14e635e SW |
391 | current->searched = 0; |
392 | discard_cleanups (searched_cleanup); | |
393 | ||
394 | if (sym != NULL) | |
395 | return sym; | |
8540c487 SW |
396 | } |
397 | } | |
398 | ||
399 | return NULL; | |
400 | } | |
401 | ||
34eaf542 TT |
402 | /* Helper function that searches an array of symbols for one named |
403 | NAME. */ | |
404 | ||
405 | static struct symbol * | |
aff410f1 MS |
406 | search_symbol_list (const char *name, int num, |
407 | struct symbol **syms) | |
34eaf542 TT |
408 | { |
409 | int i; | |
410 | ||
411 | /* Maybe we should store a dictionary in here instead. */ | |
412 | for (i = 0; i < num; ++i) | |
413 | { | |
414 | if (strcmp (name, SYMBOL_NATURAL_NAME (syms[i])) == 0) | |
415 | return syms[i]; | |
416 | } | |
417 | return NULL; | |
418 | } | |
419 | ||
420 | /* Like cp_lookup_symbol_imports, but if BLOCK is a function, it | |
421 | searches through the template parameters of the function and the | |
422 | function's type. */ | |
423 | ||
424 | struct symbol * | |
425 | cp_lookup_symbol_imports_or_template (const char *scope, | |
426 | const char *name, | |
427 | const struct block *block, | |
428 | const domain_enum domain) | |
429 | { | |
430 | struct symbol *function = BLOCK_FUNCTION (block); | |
431 | ||
432 | if (function != NULL && SYMBOL_LANGUAGE (function) == language_cplus) | |
433 | { | |
434 | int i; | |
435 | struct cplus_specific *cps | |
436 | = function->ginfo.language_specific.cplus_specific; | |
437 | ||
438 | /* Search the function's template parameters. */ | |
439 | if (SYMBOL_IS_CPLUS_TEMPLATE_FUNCTION (function)) | |
440 | { | |
aff410f1 MS |
441 | struct template_symbol *templ |
442 | = (struct template_symbol *) function; | |
34eaf542 TT |
443 | struct symbol *result; |
444 | ||
445 | result = search_symbol_list (name, | |
446 | templ->n_template_arguments, | |
447 | templ->template_arguments); | |
448 | if (result != NULL) | |
449 | return result; | |
450 | } | |
451 | ||
452 | /* Search the template parameters of the function's defining | |
453 | context. */ | |
454 | if (SYMBOL_NATURAL_NAME (function)) | |
455 | { | |
456 | struct type *context; | |
457 | char *name_copy = xstrdup (SYMBOL_NATURAL_NAME (function)); | |
458 | struct cleanup *cleanups = make_cleanup (xfree, name_copy); | |
459 | const struct language_defn *lang = language_def (language_cplus); | |
460 | struct gdbarch *arch = SYMBOL_SYMTAB (function)->objfile->gdbarch; | |
461 | const struct block *parent = BLOCK_SUPERBLOCK (block); | |
462 | ||
463 | while (1) | |
464 | { | |
465 | struct symbol *result; | |
466 | unsigned int prefix_len = cp_entire_prefix_len (name_copy); | |
467 | ||
468 | if (prefix_len == 0) | |
469 | context = NULL; | |
470 | else | |
471 | { | |
472 | name_copy[prefix_len] = '\0'; | |
aff410f1 MS |
473 | context = lookup_typename (lang, arch, |
474 | name_copy, | |
475 | parent, 1); | |
34eaf542 TT |
476 | } |
477 | ||
478 | if (context == NULL) | |
479 | break; | |
480 | ||
aff410f1 MS |
481 | result |
482 | = search_symbol_list (name, | |
483 | TYPE_N_TEMPLATE_ARGUMENTS (context), | |
484 | TYPE_TEMPLATE_ARGUMENTS (context)); | |
34eaf542 TT |
485 | if (result != NULL) |
486 | return result; | |
487 | } | |
488 | ||
489 | do_cleanups (cleanups); | |
490 | } | |
491 | } | |
492 | ||
493 | return cp_lookup_symbol_imports (scope, name, block, domain, 1, 1); | |
494 | } | |
495 | ||
aff410f1 MS |
496 | /* Searches for NAME in the current namespace, and by applying |
497 | relevant import statements belonging to BLOCK and its parents. | |
498 | SCOPE is the namespace scope of the context in which the search is | |
499 | being evaluated. */ | |
8540c487 SW |
500 | |
501 | struct symbol* | |
502 | cp_lookup_symbol_namespace (const char *scope, | |
503 | const char *name, | |
8540c487 | 504 | const struct block *block, |
13387711 | 505 | const domain_enum domain) |
8540c487 SW |
506 | { |
507 | struct symbol *sym; | |
13387711 SW |
508 | |
509 | /* First, try to find the symbol in the given namespace. */ | |
aff410f1 MS |
510 | sym = cp_lookup_symbol_in_namespace (scope, name, |
511 | block, domain); | |
13387711 SW |
512 | if (sym != NULL) |
513 | return sym; | |
8540c487 | 514 | |
aff410f1 MS |
515 | /* Search for name in namespaces imported to this and parent |
516 | blocks. */ | |
8540c487 SW |
517 | while (block != NULL) |
518 | { | |
aff410f1 MS |
519 | sym = cp_lookup_symbol_imports (scope, name, block, |
520 | domain, 0, 1); | |
8540c487 SW |
521 | |
522 | if (sym) | |
523 | return sym; | |
524 | ||
525 | block = BLOCK_SUPERBLOCK (block); | |
526 | } | |
527 | ||
528 | return NULL; | |
1fcb5155 DC |
529 | } |
530 | ||
531 | /* Lookup NAME at namespace scope (or, in C terms, in static and | |
532 | global variables). SCOPE is the namespace that the current | |
533 | function is defined within; only consider namespaces whose length | |
534 | is at least SCOPE_LEN. Other arguments are as in | |
535 | cp_lookup_symbol_nonlocal. | |
536 | ||
537 | For example, if we're within a function A::B::f and looking for a | |
3882f37a | 538 | symbol x, this will get called with NAME = "x", SCOPE = "A::B", and |
1fcb5155 DC |
539 | SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same, |
540 | but with SCOPE_LEN = 1. And then it calls itself with NAME and | |
541 | SCOPE the same, but with SCOPE_LEN = 4. This third call looks for | |
542 | "A::B::x"; if it doesn't find it, then the second call looks for | |
543 | "A::x", and if that call fails, then the first call looks for | |
544 | "x". */ | |
545 | ||
546 | static struct symbol * | |
547 | lookup_namespace_scope (const char *name, | |
1fcb5155 DC |
548 | const struct block *block, |
549 | const domain_enum domain, | |
1fcb5155 DC |
550 | const char *scope, |
551 | int scope_len) | |
552 | { | |
553 | char *namespace; | |
554 | ||
555 | if (scope[scope_len] != '\0') | |
556 | { | |
557 | /* Recursively search for names in child namespaces first. */ | |
558 | ||
559 | struct symbol *sym; | |
560 | int new_scope_len = scope_len; | |
561 | ||
562 | /* If the current scope is followed by "::", skip past that. */ | |
563 | if (new_scope_len != 0) | |
564 | { | |
565 | gdb_assert (scope[new_scope_len] == ':'); | |
566 | new_scope_len += 2; | |
567 | } | |
568 | new_scope_len += cp_find_first_component (scope + new_scope_len); | |
aff410f1 MS |
569 | sym = lookup_namespace_scope (name, block, domain, |
570 | scope, new_scope_len); | |
1fcb5155 DC |
571 | if (sym != NULL) |
572 | return sym; | |
573 | } | |
574 | ||
575 | /* Okay, we didn't find a match in our children, so look for the | |
576 | name in the current namespace. */ | |
577 | ||
578 | namespace = alloca (scope_len + 1); | |
579 | strncpy (namespace, scope, scope_len); | |
580 | namespace[scope_len] = '\0'; | |
aff410f1 MS |
581 | return cp_lookup_symbol_in_namespace (namespace, name, |
582 | block, domain); | |
1fcb5155 DC |
583 | } |
584 | ||
585 | /* Look up NAME in BLOCK's static block and in global blocks. If | |
586 | ANONYMOUS_NAMESPACE is nonzero, the symbol in question is located | |
587 | within an anonymous namespace. Other arguments are as in | |
588 | cp_lookup_symbol_nonlocal. */ | |
589 | ||
590 | static struct symbol * | |
591 | lookup_symbol_file (const char *name, | |
1fcb5155 DC |
592 | const struct block *block, |
593 | const domain_enum domain, | |
1fcb5155 DC |
594 | int anonymous_namespace) |
595 | { | |
596 | struct symbol *sym = NULL; | |
597 | ||
94af9270 | 598 | sym = lookup_symbol_static (name, block, domain); |
1fcb5155 DC |
599 | if (sym != NULL) |
600 | return sym; | |
601 | ||
602 | if (anonymous_namespace) | |
603 | { | |
604 | /* Symbols defined in anonymous namespaces have external linkage | |
605 | but should be treated as local to a single file nonetheless. | |
606 | So we only search the current file's global block. */ | |
607 | ||
608 | const struct block *global_block = block_global_block (block); | |
609 | ||
610 | if (global_block != NULL) | |
94af9270 | 611 | sym = lookup_symbol_aux_block (name, global_block, domain); |
1fcb5155 DC |
612 | } |
613 | else | |
614 | { | |
94af9270 | 615 | sym = lookup_symbol_global (name, block, domain); |
5c4e30ca DC |
616 | } |
617 | ||
0c2e6019 | 618 | return sym; |
5c4e30ca DC |
619 | } |
620 | ||
79c2c32d DC |
621 | /* Look up a type named NESTED_NAME that is nested inside the C++ |
622 | class or namespace given by PARENT_TYPE, from within the context | |
623 | given by BLOCK. Return NULL if there is no such nested type. */ | |
624 | ||
79c2c32d DC |
625 | struct type * |
626 | cp_lookup_nested_type (struct type *parent_type, | |
627 | const char *nested_name, | |
628 | const struct block *block) | |
629 | { | |
d8228535 JK |
630 | /* type_name_no_tag_required provides better error reporting using the |
631 | original type. */ | |
632 | struct type *saved_parent_type = parent_type; | |
633 | ||
634 | CHECK_TYPEDEF (parent_type); | |
635 | ||
79c2c32d DC |
636 | switch (TYPE_CODE (parent_type)) |
637 | { | |
63d06c5c | 638 | case TYPE_CODE_STRUCT: |
79c2c32d | 639 | case TYPE_CODE_NAMESPACE: |
48e32051 | 640 | case TYPE_CODE_UNION: |
79c2c32d | 641 | { |
63d06c5c DC |
642 | /* NOTE: carlton/2003-11-10: We don't treat C++ class members |
643 | of classes like, say, data or function members. Instead, | |
644 | they're just represented by symbols whose names are | |
645 | qualified by the name of the surrounding class. This is | |
646 | just like members of namespaces; in particular, | |
647 | lookup_symbol_namespace works when looking them up. */ | |
648 | ||
d8228535 | 649 | const char *parent_name = type_name_no_tag_or_error (saved_parent_type); |
aff410f1 MS |
650 | struct symbol *sym |
651 | = cp_lookup_symbol_in_namespace (parent_name, nested_name, | |
652 | block, VAR_DOMAIN); | |
41f62f39 | 653 | char *concatenated_name; |
c5504eaf | 654 | |
41f62f39 | 655 | if (sym != NULL && SYMBOL_CLASS (sym) == LOC_TYPEDEF) |
79c2c32d | 656 | return SYMBOL_TYPE (sym); |
41f62f39 | 657 | |
aff410f1 MS |
658 | /* Now search all static file-level symbols. Not strictly |
659 | correct, but more useful than an error. We do not try to | |
660 | guess any imported namespace as even the fully specified | |
661 | namespace seach is is already not C++ compliant and more | |
662 | assumptions could make it too magic. */ | |
41f62f39 JK |
663 | |
664 | concatenated_name = alloca (strlen (parent_name) + 2 | |
665 | + strlen (nested_name) + 1); | |
aff410f1 MS |
666 | sprintf (concatenated_name, "%s::%s", |
667 | parent_name, nested_name); | |
668 | sym = lookup_static_symbol_aux (concatenated_name, | |
669 | VAR_DOMAIN); | |
41f62f39 JK |
670 | if (sym != NULL && SYMBOL_CLASS (sym) == LOC_TYPEDEF) |
671 | return SYMBOL_TYPE (sym); | |
672 | ||
673 | return NULL; | |
79c2c32d DC |
674 | } |
675 | default: | |
676 | internal_error (__FILE__, __LINE__, | |
3e43a32a MS |
677 | _("cp_lookup_nested_type called " |
678 | "on a non-aggregate type.")); | |
79c2c32d DC |
679 | } |
680 | } | |
681 | ||
b368761e DC |
682 | /* The C++-version of lookup_transparent_type. */ |
683 | ||
684 | /* FIXME: carlton/2004-01-16: The problem that this is trying to | |
685 | address is that, unfortunately, sometimes NAME is wrong: it may not | |
686 | include the name of namespaces enclosing the type in question. | |
b021a221 | 687 | lookup_transparent_type gets called when the type in question |
b368761e DC |
688 | is a declaration, and we're trying to find its definition; but, for |
689 | declarations, our type name deduction mechanism doesn't work. | |
690 | There's nothing we can do to fix this in general, I think, in the | |
691 | absence of debug information about namespaces (I've filed PR | |
692 | gdb/1511 about this); until such debug information becomes more | |
693 | prevalent, one heuristic which sometimes looks is to search for the | |
694 | definition in namespaces containing the current namespace. | |
695 | ||
696 | We should delete this functions once the appropriate debug | |
697 | information becomes more widespread. (GCC 3.4 will be the first | |
698 | released version of GCC with such information.) */ | |
699 | ||
700 | struct type * | |
701 | cp_lookup_transparent_type (const char *name) | |
702 | { | |
703 | /* First, try the honest way of looking up the definition. */ | |
704 | struct type *t = basic_lookup_transparent_type (name); | |
705 | const char *scope; | |
706 | ||
707 | if (t != NULL) | |
708 | return t; | |
709 | ||
710 | /* If that doesn't work and we're within a namespace, look there | |
711 | instead. */ | |
712 | scope = block_scope (get_selected_block (0)); | |
713 | ||
714 | if (scope[0] == '\0') | |
715 | return NULL; | |
716 | ||
717 | return cp_lookup_transparent_type_loop (name, scope, 0); | |
718 | } | |
719 | ||
b021a221 MS |
720 | /* Lookup the type definition associated to NAME in namespaces/classes |
721 | containing SCOPE whose name is strictly longer than LENGTH. LENGTH | |
722 | must be the index of the start of a component of SCOPE. */ | |
b368761e DC |
723 | |
724 | static struct type * | |
aff410f1 MS |
725 | cp_lookup_transparent_type_loop (const char *name, |
726 | const char *scope, | |
b368761e DC |
727 | int length) |
728 | { | |
1198ecbe | 729 | int scope_length = length + cp_find_first_component (scope + length); |
b368761e DC |
730 | char *full_name; |
731 | ||
732 | /* If the current scope is followed by "::", look in the next | |
733 | component. */ | |
734 | if (scope[scope_length] == ':') | |
735 | { | |
736 | struct type *retval | |
aff410f1 MS |
737 | = cp_lookup_transparent_type_loop (name, scope, |
738 | scope_length + 2); | |
c5504eaf | 739 | |
b368761e DC |
740 | if (retval != NULL) |
741 | return retval; | |
742 | } | |
743 | ||
744 | full_name = alloca (scope_length + 2 + strlen (name) + 1); | |
745 | strncpy (full_name, scope, scope_length); | |
746 | strncpy (full_name + scope_length, "::", 2); | |
747 | strcpy (full_name + scope_length + 2, name); | |
748 | ||
749 | return basic_lookup_transparent_type (full_name); | |
750 | } | |
751 | ||
0c2e6019 TT |
752 | /* This used to do something but was removed when it became |
753 | obsolete. */ | |
5c4e30ca DC |
754 | |
755 | static void | |
756 | maintenance_cplus_namespace (char *args, int from_tty) | |
757 | { | |
0c2e6019 | 758 | printf_unfiltered (_("The `maint namespace' command was removed.\n")); |
5c4e30ca DC |
759 | } |
760 | ||
2c0b251b PA |
761 | /* Provide a prototype to silence -Wmissing-prototypes. */ |
762 | extern initialize_file_ftype _initialize_cp_namespace; | |
763 | ||
5c4e30ca DC |
764 | void |
765 | _initialize_cp_namespace (void) | |
766 | { | |
0c2e6019 TT |
767 | struct cmd_list_element *cmd; |
768 | ||
769 | cmd = add_cmd ("namespace", class_maintenance, | |
770 | maintenance_cplus_namespace, | |
771 | _("Deprecated placeholder for removed functionality."), | |
772 | &maint_cplus_cmd_list); | |
773 | deprecate_cmd (cmd, NULL); | |
1fcb5155 | 774 | } |