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