Commit | Line | Data |
---|---|---|
9219021c | 1 | /* Helper routines for C++ support in GDB. |
28e7fd62 | 2 | Copyright (C) 2003-2013 Free Software Foundation, Inc. |
9219021c | 3 | |
1fcb5155 | 4 | Contributed by David Carlton and by Kealia, Inc. |
9219021c DC |
5 | |
6 | This file is part of GDB. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 10 | the Free Software Foundation; either version 3 of the License, or |
9219021c DC |
11 | (at your option) any later version. |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
9219021c DC |
20 | |
21 | #include "defs.h" | |
22 | #include "cp-support.h" | |
23 | #include "gdb_obstack.h" | |
24 | #include "symtab.h" | |
25 | #include "symfile.h" | |
26 | #include "gdb_assert.h" | |
27 | #include "block.h" | |
5c4e30ca DC |
28 | #include "objfiles.h" |
29 | #include "gdbtypes.h" | |
30 | #include "dictionary.h" | |
31 | #include "command.h" | |
b368761e | 32 | #include "frame.h" |
27aa8d6a | 33 | #include "buildsym.h" |
34eaf542 | 34 | #include "language.h" |
9219021c | 35 | |
1fcb5155 | 36 | static struct symbol *lookup_namespace_scope (const char *name, |
1fcb5155 DC |
37 | const struct block *block, |
38 | const domain_enum domain, | |
1fcb5155 DC |
39 | const char *scope, |
40 | int scope_len); | |
41 | ||
42 | static struct symbol *lookup_symbol_file (const char *name, | |
1fcb5155 DC |
43 | const struct block *block, |
44 | const domain_enum domain, | |
8dea366b KS |
45 | int anonymous_namespace, |
46 | int search); | |
1fcb5155 | 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 | 267 | /* Look up NAME in the C++ namespace NAMESPACE. Other arguments are |
8dea366b KS |
268 | as in cp_lookup_symbol_nonlocal. If SEARCH is non-zero, search |
269 | through base classes for a matching symbol. */ | |
8540c487 SW |
270 | |
271 | static struct symbol * | |
272 | cp_lookup_symbol_in_namespace (const char *namespace, | |
273 | const char *name, | |
8540c487 | 274 | const struct block *block, |
8dea366b | 275 | const domain_enum domain, int search) |
8540c487 SW |
276 | { |
277 | if (namespace[0] == '\0') | |
278 | { | |
8dea366b | 279 | return lookup_symbol_file (name, block, domain, 0, search); |
8540c487 SW |
280 | } |
281 | else | |
282 | { | |
aff410f1 MS |
283 | char *concatenated_name = alloca (strlen (namespace) + 2 |
284 | + strlen (name) + 1); | |
c5504eaf | 285 | |
8540c487 SW |
286 | strcpy (concatenated_name, namespace); |
287 | strcat (concatenated_name, "::"); | |
288 | strcat (concatenated_name, name); | |
aff410f1 | 289 | return lookup_symbol_file (concatenated_name, block, domain, |
8dea366b | 290 | cp_is_anonymous (namespace), search); |
8540c487 SW |
291 | } |
292 | } | |
293 | ||
b14e635e SW |
294 | /* Used for cleanups to reset the "searched" flag incase |
295 | of an error. */ | |
296 | ||
297 | static void | |
298 | reset_directive_searched (void *data) | |
299 | { | |
300 | struct using_direct *direct = data; | |
301 | direct->searched = 0; | |
302 | } | |
303 | ||
aff410f1 MS |
304 | /* Search for NAME by applying all import statements belonging to |
305 | BLOCK which are applicable in SCOPE. If DECLARATION_ONLY the | |
306 | search is restricted to using declarations. | |
13387711 SW |
307 | Example: |
308 | ||
aff410f1 | 309 | namespace A { |
13387711 SW |
310 | int x; |
311 | } | |
312 | using A::x; | |
313 | ||
aff410f1 MS |
314 | If SEARCH_PARENTS the search will include imports which are |
315 | applicable in parents of SCOPE. | |
b14e635e SW |
316 | Example: |
317 | ||
aff410f1 | 318 | namespace A { |
b14e635e | 319 | using namespace X; |
aff410f1 | 320 | namespace B { |
b14e635e SW |
321 | using namespace Y; |
322 | } | |
323 | } | |
324 | ||
aff410f1 MS |
325 | If SCOPE is "A::B" and SEARCH_PARENTS is true the imports of |
326 | namespaces X and Y will be considered. If SEARCH_PARENTS is false | |
327 | only the import of Y is considered. */ | |
8540c487 | 328 | |
13387711 | 329 | struct symbol * |
8540c487 SW |
330 | cp_lookup_symbol_imports (const char *scope, |
331 | const char *name, | |
8540c487 | 332 | const struct block *block, |
b14e635e | 333 | const domain_enum domain, |
13387711 | 334 | const int declaration_only, |
b14e635e | 335 | const int search_parents) |
8540c487 | 336 | { |
b14e635e | 337 | struct using_direct *current; |
13387711 | 338 | struct symbol *sym = NULL; |
8540c487 | 339 | int len; |
b14e635e SW |
340 | int directive_match; |
341 | struct cleanup *searched_cleanup; | |
8540c487 SW |
342 | |
343 | /* First, try to find the symbol in the given namespace. */ | |
13387711 | 344 | if (!declaration_only) |
aff410f1 | 345 | sym = cp_lookup_symbol_in_namespace (scope, name, |
8dea366b | 346 | block, domain, 1); |
13387711 | 347 | |
8540c487 SW |
348 | if (sym != NULL) |
349 | return sym; | |
350 | ||
aff410f1 MS |
351 | /* Go through the using directives. If any of them add new names to |
352 | the namespace we're searching in, see if we can find a match by | |
353 | applying them. */ | |
8540c487 SW |
354 | |
355 | for (current = block_using (block); | |
356 | current != NULL; | |
357 | current = current->next) | |
358 | { | |
32019081 JK |
359 | const char **excludep; |
360 | ||
b14e635e SW |
361 | len = strlen (current->import_dest); |
362 | directive_match = (search_parents | |
363 | ? (strncmp (scope, current->import_dest, | |
364 | strlen (current->import_dest)) == 0 | |
365 | && (len == 0 | |
aff410f1 MS |
366 | || scope[len] == ':' |
367 | || scope[len] == '\0')) | |
b14e635e | 368 | : strcmp (scope, current->import_dest) == 0); |
8540c487 | 369 | |
aff410f1 MS |
370 | /* If the import destination is the current scope or one of its |
371 | ancestors then it is applicable. */ | |
b14e635e | 372 | if (directive_match && !current->searched) |
8540c487 | 373 | { |
a1d705ee TT |
374 | /* Mark this import as searched so that the recursive call |
375 | does not search it again. */ | |
376 | current->searched = 1; | |
377 | searched_cleanup = make_cleanup (reset_directive_searched, | |
378 | current); | |
379 | ||
380 | /* If there is an import of a single declaration, compare the | |
381 | imported declaration (after optional renaming by its alias) | |
382 | with the sought out name. If there is a match pass | |
383 | current->import_src as NAMESPACE to direct the search | |
384 | towards the imported namespace. */ | |
385 | if (current->declaration | |
386 | && strcmp (name, current->alias | |
387 | ? current->alias : current->declaration) == 0) | |
388 | sym = cp_lookup_symbol_in_namespace (current->import_src, | |
389 | current->declaration, | |
8dea366b | 390 | block, domain, 1); |
a1d705ee TT |
391 | |
392 | /* If this is a DECLARATION_ONLY search or a symbol was found | |
393 | or this import statement was an import declaration, the | |
394 | search of this import is complete. */ | |
395 | if (declaration_only || sym != NULL || current->declaration) | |
396 | { | |
397 | current->searched = 0; | |
398 | discard_cleanups (searched_cleanup); | |
399 | ||
400 | if (sym != NULL) | |
401 | return sym; | |
402 | ||
403 | continue; | |
404 | } | |
405 | ||
406 | /* Do not follow CURRENT if NAME matches its EXCLUDES. */ | |
407 | for (excludep = current->excludes; *excludep; excludep++) | |
408 | if (strcmp (name, *excludep) == 0) | |
409 | break; | |
410 | if (*excludep) | |
411 | { | |
412 | discard_cleanups (searched_cleanup); | |
413 | continue; | |
414 | } | |
415 | ||
416 | if (current->alias != NULL | |
417 | && strcmp (name, current->alias) == 0) | |
418 | /* If the import is creating an alias and the alias matches | |
419 | the sought name. Pass current->import_src as the NAME to | |
420 | direct the search towards the aliased namespace. */ | |
421 | { | |
422 | sym = cp_lookup_symbol_in_namespace (scope, | |
423 | current->import_src, | |
8dea366b | 424 | block, domain, 1); |
a1d705ee TT |
425 | } |
426 | else if (current->alias == NULL) | |
427 | { | |
428 | /* If this import statement creates no alias, pass | |
429 | current->inner as NAMESPACE to direct the search | |
430 | towards the imported namespace. */ | |
431 | sym = cp_lookup_symbol_imports (current->import_src, | |
432 | name, block, | |
433 | domain, 0, 0); | |
434 | } | |
435 | current->searched = 0; | |
436 | discard_cleanups (searched_cleanup); | |
437 | ||
438 | if (sym != NULL) | |
439 | return sym; | |
8540c487 SW |
440 | } |
441 | } | |
442 | ||
443 | return NULL; | |
444 | } | |
445 | ||
34eaf542 TT |
446 | /* Helper function that searches an array of symbols for one named |
447 | NAME. */ | |
448 | ||
449 | static struct symbol * | |
aff410f1 MS |
450 | search_symbol_list (const char *name, int num, |
451 | struct symbol **syms) | |
34eaf542 TT |
452 | { |
453 | int i; | |
454 | ||
455 | /* Maybe we should store a dictionary in here instead. */ | |
456 | for (i = 0; i < num; ++i) | |
457 | { | |
458 | if (strcmp (name, SYMBOL_NATURAL_NAME (syms[i])) == 0) | |
459 | return syms[i]; | |
460 | } | |
461 | return NULL; | |
462 | } | |
463 | ||
464 | /* Like cp_lookup_symbol_imports, but if BLOCK is a function, it | |
465 | searches through the template parameters of the function and the | |
466 | function's type. */ | |
467 | ||
468 | struct symbol * | |
469 | cp_lookup_symbol_imports_or_template (const char *scope, | |
470 | const char *name, | |
471 | const struct block *block, | |
472 | const domain_enum domain) | |
473 | { | |
474 | struct symbol *function = BLOCK_FUNCTION (block); | |
475 | ||
476 | if (function != NULL && SYMBOL_LANGUAGE (function) == language_cplus) | |
477 | { | |
478 | int i; | |
479 | struct cplus_specific *cps | |
480 | = function->ginfo.language_specific.cplus_specific; | |
481 | ||
482 | /* Search the function's template parameters. */ | |
483 | if (SYMBOL_IS_CPLUS_TEMPLATE_FUNCTION (function)) | |
484 | { | |
aff410f1 MS |
485 | struct template_symbol *templ |
486 | = (struct template_symbol *) function; | |
34eaf542 TT |
487 | struct symbol *result; |
488 | ||
489 | result = search_symbol_list (name, | |
490 | templ->n_template_arguments, | |
491 | templ->template_arguments); | |
492 | if (result != NULL) | |
493 | return result; | |
494 | } | |
495 | ||
496 | /* Search the template parameters of the function's defining | |
497 | context. */ | |
498 | if (SYMBOL_NATURAL_NAME (function)) | |
499 | { | |
500 | struct type *context; | |
501 | char *name_copy = xstrdup (SYMBOL_NATURAL_NAME (function)); | |
502 | struct cleanup *cleanups = make_cleanup (xfree, name_copy); | |
503 | const struct language_defn *lang = language_def (language_cplus); | |
504 | struct gdbarch *arch = SYMBOL_SYMTAB (function)->objfile->gdbarch; | |
505 | const struct block *parent = BLOCK_SUPERBLOCK (block); | |
506 | ||
507 | while (1) | |
508 | { | |
509 | struct symbol *result; | |
510 | unsigned int prefix_len = cp_entire_prefix_len (name_copy); | |
511 | ||
512 | if (prefix_len == 0) | |
513 | context = NULL; | |
514 | else | |
515 | { | |
516 | name_copy[prefix_len] = '\0'; | |
aff410f1 MS |
517 | context = lookup_typename (lang, arch, |
518 | name_copy, | |
519 | parent, 1); | |
34eaf542 TT |
520 | } |
521 | ||
522 | if (context == NULL) | |
523 | break; | |
524 | ||
aff410f1 MS |
525 | result |
526 | = search_symbol_list (name, | |
527 | TYPE_N_TEMPLATE_ARGUMENTS (context), | |
528 | TYPE_TEMPLATE_ARGUMENTS (context)); | |
34eaf542 TT |
529 | if (result != NULL) |
530 | return result; | |
531 | } | |
532 | ||
533 | do_cleanups (cleanups); | |
534 | } | |
535 | } | |
536 | ||
537 | return cp_lookup_symbol_imports (scope, name, block, domain, 1, 1); | |
538 | } | |
539 | ||
aff410f1 MS |
540 | /* Searches for NAME in the current namespace, and by applying |
541 | relevant import statements belonging to BLOCK and its parents. | |
542 | SCOPE is the namespace scope of the context in which the search is | |
543 | being evaluated. */ | |
8540c487 SW |
544 | |
545 | struct symbol* | |
546 | cp_lookup_symbol_namespace (const char *scope, | |
547 | const char *name, | |
8540c487 | 548 | const struct block *block, |
13387711 | 549 | const domain_enum domain) |
8540c487 SW |
550 | { |
551 | struct symbol *sym; | |
13387711 SW |
552 | |
553 | /* First, try to find the symbol in the given namespace. */ | |
aff410f1 | 554 | sym = cp_lookup_symbol_in_namespace (scope, name, |
8dea366b | 555 | block, domain, 1); |
13387711 SW |
556 | if (sym != NULL) |
557 | return sym; | |
8540c487 | 558 | |
aff410f1 MS |
559 | /* Search for name in namespaces imported to this and parent |
560 | blocks. */ | |
8540c487 SW |
561 | while (block != NULL) |
562 | { | |
aff410f1 MS |
563 | sym = cp_lookup_symbol_imports (scope, name, block, |
564 | domain, 0, 1); | |
8540c487 SW |
565 | |
566 | if (sym) | |
567 | return sym; | |
568 | ||
569 | block = BLOCK_SUPERBLOCK (block); | |
570 | } | |
571 | ||
572 | return NULL; | |
1fcb5155 DC |
573 | } |
574 | ||
575 | /* Lookup NAME at namespace scope (or, in C terms, in static and | |
576 | global variables). SCOPE is the namespace that the current | |
577 | function is defined within; only consider namespaces whose length | |
578 | is at least SCOPE_LEN. Other arguments are as in | |
579 | cp_lookup_symbol_nonlocal. | |
580 | ||
581 | For example, if we're within a function A::B::f and looking for a | |
3882f37a | 582 | symbol x, this will get called with NAME = "x", SCOPE = "A::B", and |
1fcb5155 DC |
583 | SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same, |
584 | but with SCOPE_LEN = 1. And then it calls itself with NAME and | |
585 | SCOPE the same, but with SCOPE_LEN = 4. This third call looks for | |
586 | "A::B::x"; if it doesn't find it, then the second call looks for | |
587 | "A::x", and if that call fails, then the first call looks for | |
588 | "x". */ | |
589 | ||
590 | static struct symbol * | |
591 | lookup_namespace_scope (const char *name, | |
1fcb5155 DC |
592 | const struct block *block, |
593 | const domain_enum domain, | |
1fcb5155 DC |
594 | const char *scope, |
595 | int scope_len) | |
596 | { | |
597 | char *namespace; | |
598 | ||
599 | if (scope[scope_len] != '\0') | |
600 | { | |
601 | /* Recursively search for names in child namespaces first. */ | |
602 | ||
603 | struct symbol *sym; | |
604 | int new_scope_len = scope_len; | |
605 | ||
606 | /* If the current scope is followed by "::", skip past that. */ | |
607 | if (new_scope_len != 0) | |
608 | { | |
609 | gdb_assert (scope[new_scope_len] == ':'); | |
610 | new_scope_len += 2; | |
611 | } | |
612 | new_scope_len += cp_find_first_component (scope + new_scope_len); | |
aff410f1 MS |
613 | sym = lookup_namespace_scope (name, block, domain, |
614 | scope, new_scope_len); | |
1fcb5155 DC |
615 | if (sym != NULL) |
616 | return sym; | |
617 | } | |
618 | ||
619 | /* Okay, we didn't find a match in our children, so look for the | |
620 | name in the current namespace. */ | |
621 | ||
622 | namespace = alloca (scope_len + 1); | |
623 | strncpy (namespace, scope, scope_len); | |
624 | namespace[scope_len] = '\0'; | |
aff410f1 | 625 | return cp_lookup_symbol_in_namespace (namespace, name, |
8dea366b | 626 | block, domain, 1); |
1fcb5155 DC |
627 | } |
628 | ||
629 | /* Look up NAME in BLOCK's static block and in global blocks. If | |
630 | ANONYMOUS_NAMESPACE is nonzero, the symbol in question is located | |
8dea366b KS |
631 | within an anonymous namespace. If SEARCH is non-zero, search through |
632 | base classes for a matching symbol. Other arguments are as in | |
1fcb5155 DC |
633 | cp_lookup_symbol_nonlocal. */ |
634 | ||
635 | static struct symbol * | |
636 | lookup_symbol_file (const char *name, | |
1fcb5155 DC |
637 | const struct block *block, |
638 | const domain_enum domain, | |
8dea366b | 639 | int anonymous_namespace, int search) |
1fcb5155 DC |
640 | { |
641 | struct symbol *sym = NULL; | |
642 | ||
94af9270 | 643 | sym = lookup_symbol_static (name, block, domain); |
1fcb5155 DC |
644 | if (sym != NULL) |
645 | return sym; | |
646 | ||
647 | if (anonymous_namespace) | |
648 | { | |
649 | /* Symbols defined in anonymous namespaces have external linkage | |
650 | but should be treated as local to a single file nonetheless. | |
651 | So we only search the current file's global block. */ | |
652 | ||
653 | const struct block *global_block = block_global_block (block); | |
654 | ||
655 | if (global_block != NULL) | |
94af9270 | 656 | sym = lookup_symbol_aux_block (name, global_block, domain); |
1fcb5155 DC |
657 | } |
658 | else | |
659 | { | |
94af9270 | 660 | sym = lookup_symbol_global (name, block, domain); |
5c4e30ca DC |
661 | } |
662 | ||
8dea366b KS |
663 | if (sym != NULL) |
664 | return sym; | |
665 | ||
666 | if (search) | |
667 | { | |
668 | char *klass, *nested; | |
669 | unsigned int prefix_len; | |
670 | struct cleanup *cleanup; | |
671 | struct symbol *klass_sym; | |
672 | ||
673 | /* A simple lookup failed. Check if the symbol was defined in | |
674 | a base class. */ | |
675 | ||
676 | cleanup = make_cleanup (null_cleanup, NULL); | |
677 | ||
678 | /* Find the name of the class and the name of the method, | |
679 | variable, etc. */ | |
680 | prefix_len = cp_entire_prefix_len (name); | |
681 | ||
682 | /* If no prefix was found, search "this". */ | |
683 | if (prefix_len == 0) | |
684 | { | |
685 | struct type *type; | |
686 | struct symbol *this; | |
687 | ||
688 | this = lookup_language_this (language_def (language_cplus), block); | |
689 | if (this == NULL) | |
690 | { | |
691 | do_cleanups (cleanup); | |
692 | return NULL; | |
693 | } | |
694 | ||
695 | type = check_typedef (TYPE_TARGET_TYPE (SYMBOL_TYPE (this))); | |
696 | klass = xstrdup (TYPE_NAME (type)); | |
697 | nested = xstrdup (name); | |
698 | } | |
699 | else | |
700 | { | |
701 | /* The class name is everything up to and including PREFIX_LEN. */ | |
702 | klass = savestring (name, prefix_len); | |
703 | ||
704 | /* The rest of the name is everything else past the initial scope | |
705 | operator. */ | |
706 | nested = xstrdup (name + prefix_len + 2); | |
707 | } | |
708 | ||
709 | /* Add cleanups to free memory for these strings. */ | |
710 | make_cleanup (xfree, klass); | |
711 | make_cleanup (xfree, nested); | |
712 | ||
713 | /* Lookup a class named KLASS. If none is found, there is nothing | |
714 | more that can be done. */ | |
715 | klass_sym = lookup_symbol_global (klass, block, domain); | |
716 | if (klass_sym == NULL) | |
717 | { | |
718 | do_cleanups (cleanup); | |
719 | return NULL; | |
720 | } | |
721 | ||
722 | /* Look for a symbol named NESTED in this class. */ | |
723 | sym = cp_lookup_nested_symbol (SYMBOL_TYPE (klass_sym), nested, block); | |
724 | do_cleanups (cleanup); | |
725 | } | |
726 | ||
727 | return sym; | |
728 | } | |
729 | ||
730 | /* Search through the base classes of PARENT_TYPE for a symbol named | |
731 | NAME in block BLOCK. */ | |
732 | ||
733 | static struct symbol * | |
734 | find_symbol_in_baseclass (struct type *parent_type, const char *name, | |
735 | const struct block *block) | |
736 | { | |
737 | int i; | |
738 | struct symbol *sym; | |
739 | struct cleanup *cleanup; | |
740 | char *concatenated_name; | |
741 | ||
742 | sym = NULL; | |
743 | concatenated_name = NULL; | |
744 | cleanup = make_cleanup (free_current_contents, &concatenated_name); | |
745 | for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i) | |
746 | { | |
747 | size_t len; | |
748 | const char *base_name = TYPE_BASECLASS_NAME (parent_type, i); | |
749 | ||
750 | if (base_name == NULL) | |
751 | continue; | |
752 | ||
753 | /* Search this particular base class. */ | |
754 | sym = cp_lookup_symbol_namespace (base_name, name, block, VAR_DOMAIN); | |
755 | if (sym != NULL) | |
756 | break; | |
757 | ||
758 | len = strlen (base_name) + 2 + strlen (name) + 1; | |
759 | concatenated_name = xrealloc (concatenated_name, len); | |
760 | xsnprintf (concatenated_name, len, "%s::%s", base_name, name); | |
761 | sym = lookup_symbol_static (concatenated_name, block, VAR_DOMAIN); | |
762 | ||
763 | /* If there is currently no BLOCK, e.g., the inferior hasn't yet | |
764 | been started, then try searching all STATIC_BLOCK symbols in | |
765 | all objfiles. */ | |
766 | if (block == NULL) | |
767 | { | |
768 | sym = lookup_static_symbol_aux (concatenated_name, VAR_DOMAIN); | |
769 | if (sym != NULL) | |
770 | break; | |
771 | } | |
772 | ||
773 | /* If this class has base classes, search them next. */ | |
774 | if (TYPE_N_BASECLASSES (TYPE_BASECLASS (parent_type, i)) > 0) | |
775 | { | |
776 | sym = find_symbol_in_baseclass (TYPE_BASECLASS (parent_type, i), | |
777 | name, block); | |
778 | if (sym != NULL) | |
779 | break; | |
780 | } | |
781 | } | |
782 | ||
783 | do_cleanups (cleanup); | |
0c2e6019 | 784 | return sym; |
5c4e30ca DC |
785 | } |
786 | ||
50af5481 | 787 | /* Look up a symbol named NESTED_NAME that is nested inside the C++ |
79c2c32d DC |
788 | class or namespace given by PARENT_TYPE, from within the context |
789 | given by BLOCK. Return NULL if there is no such nested type. */ | |
790 | ||
50af5481 JK |
791 | struct symbol * |
792 | cp_lookup_nested_symbol (struct type *parent_type, | |
793 | const char *nested_name, | |
794 | const struct block *block) | |
79c2c32d | 795 | { |
d8228535 JK |
796 | /* type_name_no_tag_required provides better error reporting using the |
797 | original type. */ | |
798 | struct type *saved_parent_type = parent_type; | |
799 | ||
800 | CHECK_TYPEDEF (parent_type); | |
801 | ||
79c2c32d DC |
802 | switch (TYPE_CODE (parent_type)) |
803 | { | |
63d06c5c | 804 | case TYPE_CODE_STRUCT: |
79c2c32d | 805 | case TYPE_CODE_NAMESPACE: |
48e32051 | 806 | case TYPE_CODE_UNION: |
79c2c32d | 807 | { |
63d06c5c DC |
808 | /* NOTE: carlton/2003-11-10: We don't treat C++ class members |
809 | of classes like, say, data or function members. Instead, | |
810 | they're just represented by symbols whose names are | |
811 | qualified by the name of the surrounding class. This is | |
812 | just like members of namespaces; in particular, | |
813 | lookup_symbol_namespace works when looking them up. */ | |
814 | ||
08850b56 | 815 | int size; |
d8228535 | 816 | const char *parent_name = type_name_no_tag_or_error (saved_parent_type); |
aff410f1 MS |
817 | struct symbol *sym |
818 | = cp_lookup_symbol_in_namespace (parent_name, nested_name, | |
8dea366b | 819 | block, VAR_DOMAIN, 0); |
41f62f39 | 820 | char *concatenated_name; |
c5504eaf | 821 | |
50af5481 JK |
822 | if (sym != NULL) |
823 | return sym; | |
41f62f39 | 824 | |
aff410f1 MS |
825 | /* Now search all static file-level symbols. Not strictly |
826 | correct, but more useful than an error. We do not try to | |
827 | guess any imported namespace as even the fully specified | |
8dea366b | 828 | namespace search is already not C++ compliant and more |
aff410f1 | 829 | assumptions could make it too magic. */ |
41f62f39 | 830 | |
08850b56 PM |
831 | size = strlen (parent_name) + 2 + strlen (nested_name) + 1; |
832 | concatenated_name = alloca (size); | |
833 | xsnprintf (concatenated_name, size, "%s::%s", | |
aff410f1 | 834 | parent_name, nested_name); |
50af5481 JK |
835 | sym = lookup_static_symbol_aux (concatenated_name, VAR_DOMAIN); |
836 | if (sym != NULL) | |
837 | return sym; | |
41f62f39 | 838 | |
8dea366b KS |
839 | /* If no matching symbols were found, try searching any |
840 | base classes. */ | |
841 | return find_symbol_in_baseclass (parent_type, nested_name, block); | |
79c2c32d DC |
842 | } |
843 | default: | |
844 | internal_error (__FILE__, __LINE__, | |
50af5481 | 845 | _("cp_lookup_nested_symbol called " |
3e43a32a | 846 | "on a non-aggregate type.")); |
79c2c32d DC |
847 | } |
848 | } | |
849 | ||
b368761e DC |
850 | /* The C++-version of lookup_transparent_type. */ |
851 | ||
852 | /* FIXME: carlton/2004-01-16: The problem that this is trying to | |
853 | address is that, unfortunately, sometimes NAME is wrong: it may not | |
854 | include the name of namespaces enclosing the type in question. | |
b021a221 | 855 | lookup_transparent_type gets called when the type in question |
b368761e DC |
856 | is a declaration, and we're trying to find its definition; but, for |
857 | declarations, our type name deduction mechanism doesn't work. | |
858 | There's nothing we can do to fix this in general, I think, in the | |
859 | absence of debug information about namespaces (I've filed PR | |
860 | gdb/1511 about this); until such debug information becomes more | |
861 | prevalent, one heuristic which sometimes looks is to search for the | |
862 | definition in namespaces containing the current namespace. | |
863 | ||
864 | We should delete this functions once the appropriate debug | |
865 | information becomes more widespread. (GCC 3.4 will be the first | |
866 | released version of GCC with such information.) */ | |
867 | ||
868 | struct type * | |
869 | cp_lookup_transparent_type (const char *name) | |
870 | { | |
871 | /* First, try the honest way of looking up the definition. */ | |
872 | struct type *t = basic_lookup_transparent_type (name); | |
873 | const char *scope; | |
874 | ||
875 | if (t != NULL) | |
876 | return t; | |
877 | ||
878 | /* If that doesn't work and we're within a namespace, look there | |
879 | instead. */ | |
880 | scope = block_scope (get_selected_block (0)); | |
881 | ||
882 | if (scope[0] == '\0') | |
883 | return NULL; | |
884 | ||
885 | return cp_lookup_transparent_type_loop (name, scope, 0); | |
886 | } | |
887 | ||
b021a221 MS |
888 | /* Lookup the type definition associated to NAME in namespaces/classes |
889 | containing SCOPE whose name is strictly longer than LENGTH. LENGTH | |
890 | must be the index of the start of a component of SCOPE. */ | |
b368761e DC |
891 | |
892 | static struct type * | |
aff410f1 MS |
893 | cp_lookup_transparent_type_loop (const char *name, |
894 | const char *scope, | |
b368761e DC |
895 | int length) |
896 | { | |
1198ecbe | 897 | int scope_length = length + cp_find_first_component (scope + length); |
b368761e DC |
898 | char *full_name; |
899 | ||
900 | /* If the current scope is followed by "::", look in the next | |
901 | component. */ | |
902 | if (scope[scope_length] == ':') | |
903 | { | |
904 | struct type *retval | |
aff410f1 MS |
905 | = cp_lookup_transparent_type_loop (name, scope, |
906 | scope_length + 2); | |
c5504eaf | 907 | |
b368761e DC |
908 | if (retval != NULL) |
909 | return retval; | |
910 | } | |
911 | ||
912 | full_name = alloca (scope_length + 2 + strlen (name) + 1); | |
913 | strncpy (full_name, scope, scope_length); | |
914 | strncpy (full_name + scope_length, "::", 2); | |
915 | strcpy (full_name + scope_length + 2, name); | |
916 | ||
917 | return basic_lookup_transparent_type (full_name); | |
918 | } | |
919 | ||
0c2e6019 TT |
920 | /* This used to do something but was removed when it became |
921 | obsolete. */ | |
5c4e30ca DC |
922 | |
923 | static void | |
924 | maintenance_cplus_namespace (char *args, int from_tty) | |
925 | { | |
0c2e6019 | 926 | printf_unfiltered (_("The `maint namespace' command was removed.\n")); |
5c4e30ca DC |
927 | } |
928 | ||
2c0b251b PA |
929 | /* Provide a prototype to silence -Wmissing-prototypes. */ |
930 | extern initialize_file_ftype _initialize_cp_namespace; | |
931 | ||
5c4e30ca DC |
932 | void |
933 | _initialize_cp_namespace (void) | |
934 | { | |
0c2e6019 TT |
935 | struct cmd_list_element *cmd; |
936 | ||
937 | cmd = add_cmd ("namespace", class_maintenance, | |
938 | maintenance_cplus_namespace, | |
939 | _("Deprecated placeholder for removed functionality."), | |
940 | &maint_cplus_cmd_list); | |
941 | deprecate_cmd (cmd, NULL); | |
1fcb5155 | 942 | } |