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