Commit | Line | Data |
---|---|---|
9219021c | 1 | /* Helper routines for C++ support in GDB. |
32d0add0 | 2 | Copyright (C) 2003-2015 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" | |
9219021c | 26 | #include "block.h" |
5c4e30ca DC |
27 | #include "objfiles.h" |
28 | #include "gdbtypes.h" | |
29 | #include "dictionary.h" | |
30 | #include "command.h" | |
b368761e | 31 | #include "frame.h" |
27aa8d6a | 32 | #include "buildsym.h" |
34eaf542 | 33 | #include "language.h" |
9219021c | 34 | |
d12307c1 | 35 | static struct block_symbol |
6f27419a DE |
36 | cp_lookup_nested_symbol_1 (struct type *container_type, |
37 | const char *nested_name, | |
38 | const char *concatenated_name, | |
39 | const struct block *block, | |
4dcabcc2 | 40 | const domain_enum domain, |
96553a0c | 41 | int basic_lookup, int is_in_anonymous); |
6f27419a | 42 | |
b368761e DC |
43 | static struct type *cp_lookup_transparent_type_loop (const char *name, |
44 | const char *scope, | |
45 | int scope_len); | |
46 | ||
9219021c DC |
47 | /* Check to see if SYMBOL refers to an object contained within an |
48 | anonymous namespace; if so, add an appropriate using directive. */ | |
49 | ||
9219021c | 50 | void |
a10964d1 AR |
51 | cp_scan_for_anonymous_namespaces (const struct symbol *const symbol, |
52 | struct objfile *const objfile) | |
9219021c | 53 | { |
df8a16a1 | 54 | if (SYMBOL_DEMANGLED_NAME (symbol) != NULL) |
9219021c | 55 | { |
df8a16a1 | 56 | const char *name = SYMBOL_DEMANGLED_NAME (symbol); |
9219021c DC |
57 | unsigned int previous_component; |
58 | unsigned int next_component; | |
9219021c DC |
59 | |
60 | /* Start with a quick-and-dirty check for mention of "(anonymous | |
61 | namespace)". */ | |
62 | ||
59da4d04 | 63 | if (!cp_is_in_anonymous (name)) |
9219021c DC |
64 | return; |
65 | ||
66 | previous_component = 0; | |
67 | next_component = cp_find_first_component (name + previous_component); | |
68 | ||
69 | while (name[next_component] == ':') | |
70 | { | |
2b1dbab0 KS |
71 | if (((next_component - previous_component) |
72 | == CP_ANONYMOUS_NAMESPACE_LEN) | |
9219021c | 73 | && strncmp (name + previous_component, |
2b1dbab0 KS |
74 | CP_ANONYMOUS_NAMESPACE_STR, |
75 | CP_ANONYMOUS_NAMESPACE_LEN) == 0) | |
9219021c | 76 | { |
aff410f1 MS |
77 | int dest_len = (previous_component == 0 |
78 | ? 0 : previous_component - 2); | |
8c902bb1 | 79 | int src_len = next_component; |
794684b6 | 80 | |
8c902bb1 SW |
81 | char *dest = alloca (dest_len + 1); |
82 | char *src = alloca (src_len + 1); | |
794684b6 | 83 | |
8c902bb1 SW |
84 | memcpy (dest, name, dest_len); |
85 | memcpy (src, name, src_len); | |
794684b6 | 86 | |
8c902bb1 SW |
87 | dest[dest_len] = '\0'; |
88 | src[src_len] = '\0'; | |
794684b6 | 89 | |
9219021c DC |
90 | /* We've found a component of the name that's an |
91 | anonymous namespace. So add symbols in it to the | |
92 | namespace given by the previous component if there is | |
93 | one, or to the global namespace if there isn't. */ | |
12aaed36 | 94 | cp_add_using_directive (dest, src, NULL, NULL, NULL, 1, |
a10964d1 | 95 | &objfile->objfile_obstack); |
9219021c DC |
96 | } |
97 | /* The "+ 2" is for the "::". */ | |
98 | previous_component = next_component + 2; | |
99 | next_component = (previous_component | |
100 | + cp_find_first_component (name | |
101 | + previous_component)); | |
102 | } | |
103 | } | |
104 | } | |
105 | ||
aff410f1 MS |
106 | /* Add a using directive to using_directives. If the using directive |
107 | in question has already been added, don't add it twice. | |
108 | ||
109 | Create a new struct using_direct which imports the namespace SRC | |
110 | into the scope DEST. ALIAS is the name of the imported namespace | |
111 | in the current scope. If ALIAS is NULL then the namespace is known | |
112 | by its original name. DECLARATION is the name if the imported | |
113 | varable if this is a declaration import (Eg. using A::x), otherwise | |
12aaed36 TT |
114 | it is NULL. EXCLUDES is a list of names not to import from an |
115 | imported module or NULL. If COPY_NAMES is non-zero, then the | |
116 | arguments are copied into newly allocated memory so they can be | |
117 | temporaries. For EXCLUDES the VEC pointers are copied but the | |
32019081 | 118 | pointed to characters are not copied. */ |
9219021c DC |
119 | |
120 | void | |
13387711 SW |
121 | cp_add_using_directive (const char *dest, |
122 | const char *src, | |
123 | const char *alias, | |
124 | const char *declaration, | |
32019081 | 125 | VEC (const_char_ptr) *excludes, |
12aaed36 | 126 | int copy_names, |
c0cc3a76 | 127 | struct obstack *obstack) |
9219021c DC |
128 | { |
129 | struct using_direct *current; | |
fe978cb0 | 130 | struct using_direct *newobj; |
791244be | 131 | |
9219021c DC |
132 | /* Has it already been added? */ |
133 | ||
27aa8d6a | 134 | for (current = using_directives; current != NULL; current = current->next) |
9219021c | 135 | { |
32019081 JK |
136 | int ix; |
137 | const char *param; | |
138 | ||
70c622a3 JK |
139 | if (strcmp (current->import_src, src) != 0) |
140 | continue; | |
141 | if (strcmp (current->import_dest, dest) != 0) | |
142 | continue; | |
143 | if ((alias == NULL && current->alias != NULL) | |
144 | || (alias != NULL && current->alias == NULL) | |
145 | || (alias != NULL && current->alias != NULL | |
146 | && strcmp (alias, current->alias) != 0)) | |
147 | continue; | |
148 | if ((declaration == NULL && current->declaration != NULL) | |
149 | || (declaration != NULL && current->declaration == NULL) | |
150 | || (declaration != NULL && current->declaration != NULL | |
151 | && strcmp (declaration, current->declaration) != 0)) | |
152 | continue; | |
153 | ||
32019081 JK |
154 | /* Compare the contents of EXCLUDES. */ |
155 | for (ix = 0; VEC_iterate (const_char_ptr, excludes, ix, param); ix++) | |
156 | if (current->excludes[ix] == NULL | |
157 | || strcmp (param, current->excludes[ix]) != 0) | |
158 | break; | |
159 | if (ix < VEC_length (const_char_ptr, excludes) | |
160 | || current->excludes[ix] != NULL) | |
161 | continue; | |
162 | ||
70c622a3 JK |
163 | /* Parameters exactly match CURRENT. */ |
164 | return; | |
9219021c DC |
165 | } |
166 | ||
fe978cb0 | 167 | newobj = obstack_alloc (obstack, (sizeof (*newobj) |
32019081 | 168 | + (VEC_length (const_char_ptr, excludes) |
fe978cb0 PA |
169 | * sizeof (*newobj->excludes)))); |
170 | memset (newobj, 0, sizeof (*newobj)); | |
c0cc3a76 | 171 | |
12aaed36 TT |
172 | if (copy_names) |
173 | { | |
fe978cb0 PA |
174 | newobj->import_src = obstack_copy0 (obstack, src, strlen (src)); |
175 | newobj->import_dest = obstack_copy0 (obstack, dest, strlen (dest)); | |
12aaed36 TT |
176 | } |
177 | else | |
178 | { | |
fe978cb0 PA |
179 | newobj->import_src = src; |
180 | newobj->import_dest = dest; | |
12aaed36 | 181 | } |
794684b6 | 182 | |
12aaed36 | 183 | if (alias != NULL && copy_names) |
fe978cb0 | 184 | newobj->alias = obstack_copy0 (obstack, alias, strlen (alias)); |
12aaed36 | 185 | else |
fe978cb0 | 186 | newobj->alias = alias; |
c0cc3a76 | 187 | |
12aaed36 | 188 | if (declaration != NULL && copy_names) |
fe978cb0 | 189 | newobj->declaration = obstack_copy0 (obstack, |
10f0c4bb | 190 | declaration, strlen (declaration)); |
12aaed36 | 191 | else |
fe978cb0 | 192 | newobj->declaration = declaration; |
13387711 | 193 | |
fe978cb0 PA |
194 | memcpy (newobj->excludes, VEC_address (const_char_ptr, excludes), |
195 | VEC_length (const_char_ptr, excludes) * sizeof (*newobj->excludes)); | |
196 | newobj->excludes[VEC_length (const_char_ptr, excludes)] = NULL; | |
32019081 | 197 | |
fe978cb0 PA |
198 | newobj->next = using_directives; |
199 | using_directives = newobj; | |
9219021c DC |
200 | } |
201 | ||
9219021c DC |
202 | /* Test whether or not NAMESPACE looks like it mentions an anonymous |
203 | namespace; return nonzero if so. */ | |
204 | ||
205 | int | |
59da4d04 | 206 | cp_is_in_anonymous (const char *symbol_name) |
9219021c | 207 | { |
59da4d04 | 208 | return (strstr (symbol_name, CP_ANONYMOUS_NAMESPACE_STR) |
9219021c DC |
209 | != NULL); |
210 | } | |
211 | ||
6f27419a | 212 | /* Look up NAME in DOMAIN in BLOCK's static block and in global blocks. |
d5ff0482 | 213 | If IS_IN_ANONYMOUS is nonzero, the symbol in question is located |
6f27419a | 214 | within an anonymous namespace. */ |
34ef8452 | 215 | |
d12307c1 | 216 | static struct block_symbol |
6f27419a | 217 | cp_basic_lookup_symbol (const char *name, const struct block *block, |
d5ff0482 | 218 | const domain_enum domain, int is_in_anonymous) |
34ef8452 | 219 | { |
d12307c1 | 220 | struct block_symbol sym; |
34ef8452 DE |
221 | |
222 | sym = lookup_symbol_in_static_block (name, block, domain); | |
d12307c1 | 223 | if (sym.symbol != NULL) |
34ef8452 DE |
224 | return sym; |
225 | ||
d5ff0482 | 226 | if (is_in_anonymous) |
34ef8452 DE |
227 | { |
228 | /* Symbols defined in anonymous namespaces have external linkage | |
229 | but should be treated as local to a single file nonetheless. | |
230 | So we only search the current file's global block. */ | |
231 | ||
232 | const struct block *global_block = block_global_block (block); | |
791244be | 233 | |
34ef8452 | 234 | if (global_block != NULL) |
d12307c1 PMR |
235 | { |
236 | sym.symbol = lookup_symbol_in_block (name, global_block, domain); | |
237 | sym.block = global_block; | |
238 | } | |
34ef8452 DE |
239 | } |
240 | else | |
d12307c1 | 241 | sym = lookup_global_symbol (name, block, domain); |
34ef8452 | 242 | |
6f27419a DE |
243 | return sym; |
244 | } | |
245 | ||
246 | /* Search bare symbol NAME in DOMAIN in BLOCK. | |
1994afbf DE |
247 | NAME is guaranteed to not have any scope (no "::") in its name, though |
248 | if for example NAME is a template spec then "::" may appear in the | |
249 | argument list. | |
250 | If LANGDEF is non-NULL then try to lookup NAME as a primitive type in | |
251 | that language. Normally we wouldn't need LANGDEF but fortran also uses | |
252 | this code. | |
6f27419a DE |
253 | If SEARCH is non-zero then see if we can determine "this" from BLOCK, and |
254 | if so then also search for NAME in that class. */ | |
255 | ||
d12307c1 | 256 | static struct block_symbol |
1994afbf DE |
257 | cp_lookup_bare_symbol (const struct language_defn *langdef, |
258 | const char *name, const struct block *block, | |
6f27419a DE |
259 | const domain_enum domain, int search) |
260 | { | |
d12307c1 | 261 | struct block_symbol sym; |
6f27419a DE |
262 | |
263 | /* Note: We can't do a simple assert for ':' not being in NAME because | |
264 | ':' may be in the args of a template spec. This isn't intended to be | |
265 | a complete test, just cheap and documentary. */ | |
266 | if (strchr (name, '<') == NULL && strchr (name, '(') == NULL) | |
267 | gdb_assert (strchr (name, ':') == NULL); | |
268 | ||
269 | sym = lookup_symbol_in_static_block (name, block, domain); | |
d12307c1 | 270 | if (sym.symbol != NULL) |
6f27419a DE |
271 | return sym; |
272 | ||
1994afbf DE |
273 | /* If we didn't find a definition for a builtin type in the static block, |
274 | search for it now. This is actually the right thing to do and can be | |
275 | a massive performance win. E.g., when debugging a program with lots of | |
276 | shared libraries we could search all of them only to find out the | |
277 | builtin type isn't defined in any of them. This is common for types | |
278 | like "void". */ | |
279 | if (langdef != NULL && domain == VAR_DOMAIN) | |
280 | { | |
281 | struct gdbarch *gdbarch; | |
282 | ||
283 | if (block == NULL) | |
284 | gdbarch = target_gdbarch (); | |
285 | else | |
286 | gdbarch = block_gdbarch (block); | |
d12307c1 PMR |
287 | sym.symbol |
288 | = language_lookup_primitive_type_as_symbol (langdef, gdbarch, name); | |
289 | sym.block = NULL; | |
290 | if (sym.symbol != NULL) | |
1994afbf DE |
291 | return sym; |
292 | } | |
293 | ||
6f27419a | 294 | sym = lookup_global_symbol (name, block, domain); |
d12307c1 | 295 | if (sym.symbol != NULL) |
34ef8452 DE |
296 | return sym; |
297 | ||
298 | if (search) | |
299 | { | |
d12307c1 | 300 | struct block_symbol lang_this; |
6f27419a | 301 | struct type *type; |
34ef8452 | 302 | |
fe978cb0 | 303 | lang_this = lookup_language_this (language_def (language_cplus), block); |
d12307c1 PMR |
304 | if (lang_this.symbol == NULL) |
305 | return (struct block_symbol) {NULL, NULL}; | |
34ef8452 | 306 | |
d12307c1 | 307 | type = check_typedef (TYPE_TARGET_TYPE (SYMBOL_TYPE (lang_this.symbol))); |
6f27419a DE |
308 | /* If TYPE_NAME is NULL, abandon trying to find this symbol. |
309 | This can happen for lambda functions compiled with clang++, | |
310 | which outputs no name for the container class. */ | |
311 | if (TYPE_NAME (type) == NULL) | |
d12307c1 | 312 | return (struct block_symbol) {NULL, NULL}; |
34ef8452 | 313 | |
d5ff0482 | 314 | /* Look for symbol NAME in this class. */ |
4dcabcc2 | 315 | sym = cp_lookup_nested_symbol (type, name, block, domain); |
6f27419a | 316 | } |
34ef8452 | 317 | |
6f27419a DE |
318 | return sym; |
319 | } | |
34ef8452 | 320 | |
6f27419a DE |
321 | /* Search NAME in DOMAIN in all static blocks, and then in all baseclasses. |
322 | BLOCK specifies the context in which to perform the search. | |
323 | NAME is guaranteed to have scope (contain "::") and PREFIX_LEN specifies | |
d5ff0482 | 324 | the length of the entire scope of NAME (up to, but not including, the last |
6f27419a | 325 | "::". |
34ef8452 | 326 | |
6f27419a DE |
327 | Note: At least in the case of Fortran, which also uses this code, there |
328 | may be no text after the last "::". */ | |
34ef8452 | 329 | |
d12307c1 | 330 | static struct block_symbol |
6f27419a DE |
331 | cp_search_static_and_baseclasses (const char *name, |
332 | const struct block *block, | |
333 | const domain_enum domain, | |
96553a0c DE |
334 | unsigned int prefix_len, |
335 | int is_in_anonymous) | |
6f27419a | 336 | { |
d12307c1 | 337 | struct block_symbol sym; |
6f27419a DE |
338 | char *klass, *nested; |
339 | struct cleanup *cleanup; | |
d12307c1 | 340 | struct block_symbol klass_sym; |
6f27419a | 341 | struct type *klass_type; |
34ef8452 | 342 | |
6f27419a DE |
343 | /* The test here uses <= instead of < because Fortran also uses this, |
344 | and the module.exp testcase will pass "modmany::" for NAME here. */ | |
345 | gdb_assert (prefix_len + 2 <= strlen (name)); | |
346 | gdb_assert (name[prefix_len + 1] == ':'); | |
34ef8452 | 347 | |
6f27419a | 348 | /* Find the name of the class and the name of the method, variable, etc. */ |
34ef8452 | 349 | |
6f27419a DE |
350 | /* The class name is everything up to and including PREFIX_LEN. */ |
351 | klass = savestring (name, prefix_len); | |
352 | ||
353 | /* The rest of the name is everything else past the initial scope | |
354 | operator. */ | |
355 | nested = xstrdup (name + prefix_len + 2); | |
356 | ||
357 | /* Add cleanups to free memory for these strings. */ | |
358 | cleanup = make_cleanup (xfree, klass); | |
359 | make_cleanup (xfree, nested); | |
360 | ||
361 | /* Lookup a class named KLASS. If none is found, there is nothing | |
13ce9222 DE |
362 | more that can be done. KLASS could be a namespace, so always look |
363 | in VAR_DOMAIN. This works for classes too because of | |
364 | symbol_matches_domain (which should be replaced with something else, | |
365 | but it's what we have today). */ | |
366 | klass_sym = lookup_global_symbol (klass, block, VAR_DOMAIN); | |
d12307c1 | 367 | if (klass_sym.symbol == NULL) |
6f27419a | 368 | { |
34ef8452 | 369 | do_cleanups (cleanup); |
d12307c1 | 370 | return (struct block_symbol) {NULL, NULL}; |
34ef8452 | 371 | } |
d12307c1 | 372 | klass_type = SYMBOL_TYPE (klass_sym.symbol); |
6f27419a DE |
373 | |
374 | /* Look for a symbol named NESTED in this class. | |
375 | The caller is assumed to have already have done a basic lookup of NAME. | |
376 | So we pass zero for BASIC_LOOKUP to cp_lookup_nested_symbol_1 here. */ | |
4dcabcc2 DE |
377 | sym = cp_lookup_nested_symbol_1 (klass_type, nested, name, block, domain, |
378 | 0, is_in_anonymous); | |
34ef8452 | 379 | |
6f27419a | 380 | do_cleanups (cleanup); |
34ef8452 DE |
381 | return sym; |
382 | } | |
383 | ||
aff410f1 | 384 | /* Look up NAME in the C++ namespace NAMESPACE. Other arguments are |
8dea366b | 385 | as in cp_lookup_symbol_nonlocal. If SEARCH is non-zero, search |
6f27419a DE |
386 | through base classes for a matching symbol. |
387 | ||
388 | Note: Part of the complexity is because NAME may itself specify scope. | |
389 | Part of the complexity is also because this handles the case where | |
390 | there is no scoping in which case we also try looking in the class of | |
391 | "this" if we can compute it. */ | |
8540c487 | 392 | |
d12307c1 | 393 | static struct block_symbol |
fe978cb0 | 394 | cp_lookup_symbol_in_namespace (const char *the_namespace, const char *name, |
6f27419a DE |
395 | const struct block *block, |
396 | const domain_enum domain, int search) | |
8540c487 | 397 | { |
d2763117 | 398 | char *concatenated_name = NULL; |
6f27419a DE |
399 | int is_in_anonymous; |
400 | unsigned int prefix_len; | |
d12307c1 | 401 | struct block_symbol sym; |
c5504eaf | 402 | |
fe978cb0 | 403 | if (the_namespace[0] != '\0') |
d2763117 | 404 | { |
fe978cb0 | 405 | concatenated_name = alloca (strlen (the_namespace) + 2 |
d2763117 | 406 | + strlen (name) + 1); |
fe978cb0 | 407 | strcpy (concatenated_name, the_namespace); |
8540c487 SW |
408 | strcat (concatenated_name, "::"); |
409 | strcat (concatenated_name, name); | |
d2763117 | 410 | name = concatenated_name; |
8540c487 | 411 | } |
d2763117 | 412 | |
6f27419a DE |
413 | prefix_len = cp_entire_prefix_len (name); |
414 | if (prefix_len == 0) | |
1994afbf | 415 | return cp_lookup_bare_symbol (NULL, name, block, domain, search); |
6f27419a DE |
416 | |
417 | /* This would be simpler if we just called cp_lookup_nested_symbol | |
418 | at this point. But that would require first looking up the containing | |
419 | class/namespace. Since we're only searching static and global blocks | |
420 | there's often no need to first do that lookup. */ | |
421 | ||
fe978cb0 PA |
422 | is_in_anonymous |
423 | = the_namespace[0] != '\0' && cp_is_in_anonymous (the_namespace); | |
6f27419a | 424 | sym = cp_basic_lookup_symbol (name, block, domain, is_in_anonymous); |
d12307c1 | 425 | if (sym.symbol != NULL) |
6f27419a DE |
426 | return sym; |
427 | ||
428 | if (search) | |
96553a0c DE |
429 | sym = cp_search_static_and_baseclasses (name, block, domain, prefix_len, |
430 | is_in_anonymous); | |
6f27419a DE |
431 | |
432 | return sym; | |
8540c487 SW |
433 | } |
434 | ||
d5ff0482 | 435 | /* Used for cleanups to reset the "searched" flag in case of an error. */ |
b14e635e SW |
436 | |
437 | static void | |
438 | reset_directive_searched (void *data) | |
439 | { | |
440 | struct using_direct *direct = data; | |
441 | direct->searched = 0; | |
442 | } | |
443 | ||
aff410f1 MS |
444 | /* Search for NAME by applying all import statements belonging to |
445 | BLOCK which are applicable in SCOPE. If DECLARATION_ONLY the | |
446 | search is restricted to using declarations. | |
13387711 SW |
447 | Example: |
448 | ||
aff410f1 | 449 | namespace A { |
13387711 SW |
450 | int x; |
451 | } | |
452 | using A::x; | |
453 | ||
aff410f1 MS |
454 | If SEARCH_PARENTS the search will include imports which are |
455 | applicable in parents of SCOPE. | |
b14e635e SW |
456 | Example: |
457 | ||
aff410f1 | 458 | namespace A { |
b14e635e | 459 | using namespace X; |
aff410f1 | 460 | namespace B { |
b14e635e SW |
461 | using namespace Y; |
462 | } | |
463 | } | |
464 | ||
aff410f1 MS |
465 | If SCOPE is "A::B" and SEARCH_PARENTS is true the imports of |
466 | namespaces X and Y will be considered. If SEARCH_PARENTS is false | |
4bd0864e DE |
467 | only the import of Y is considered. |
468 | ||
469 | SEARCH_SCOPE_FIRST is an internal implementation detail: Callers must | |
470 | pass 0 for it. Internally we pass 1 when recursing. */ | |
8540c487 | 471 | |
d12307c1 | 472 | static struct block_symbol |
55accf4a DE |
473 | cp_lookup_symbol_via_imports (const char *scope, |
474 | const char *name, | |
475 | const struct block *block, | |
476 | const domain_enum domain, | |
4bd0864e | 477 | const int search_scope_first, |
55accf4a DE |
478 | const int declaration_only, |
479 | const int search_parents) | |
8540c487 | 480 | { |
b14e635e | 481 | struct using_direct *current; |
d12307c1 | 482 | struct block_symbol sym; |
8540c487 | 483 | int len; |
b14e635e SW |
484 | int directive_match; |
485 | struct cleanup *searched_cleanup; | |
8540c487 | 486 | |
d12307c1 | 487 | sym.symbol = NULL; |
1da03605 | 488 | sym.block = NULL; |
d12307c1 | 489 | |
4bd0864e DE |
490 | /* First, try to find the symbol in the given namespace if requested. */ |
491 | if (search_scope_first) | |
aff410f1 | 492 | sym = cp_lookup_symbol_in_namespace (scope, name, |
8dea366b | 493 | block, domain, 1); |
791244be | 494 | |
d12307c1 | 495 | if (sym.symbol != NULL) |
8540c487 SW |
496 | return sym; |
497 | ||
aff410f1 MS |
498 | /* Go through the using directives. If any of them add new names to |
499 | the namespace we're searching in, see if we can find a match by | |
500 | applying them. */ | |
8540c487 SW |
501 | |
502 | for (current = block_using (block); | |
503 | current != NULL; | |
504 | current = current->next) | |
505 | { | |
32019081 JK |
506 | const char **excludep; |
507 | ||
b14e635e SW |
508 | len = strlen (current->import_dest); |
509 | directive_match = (search_parents | |
61012eef | 510 | ? (startswith (scope, current->import_dest) |
b14e635e | 511 | && (len == 0 |
aff410f1 MS |
512 | || scope[len] == ':' |
513 | || scope[len] == '\0')) | |
b14e635e | 514 | : strcmp (scope, current->import_dest) == 0); |
8540c487 | 515 | |
aff410f1 MS |
516 | /* If the import destination is the current scope or one of its |
517 | ancestors then it is applicable. */ | |
b14e635e | 518 | if (directive_match && !current->searched) |
8540c487 | 519 | { |
a1d705ee TT |
520 | /* Mark this import as searched so that the recursive call |
521 | does not search it again. */ | |
522 | current->searched = 1; | |
523 | searched_cleanup = make_cleanup (reset_directive_searched, | |
524 | current); | |
525 | ||
526 | /* If there is an import of a single declaration, compare the | |
527 | imported declaration (after optional renaming by its alias) | |
528 | with the sought out name. If there is a match pass | |
529 | current->import_src as NAMESPACE to direct the search | |
530 | towards the imported namespace. */ | |
531 | if (current->declaration | |
532 | && strcmp (name, current->alias | |
533 | ? current->alias : current->declaration) == 0) | |
534 | sym = cp_lookup_symbol_in_namespace (current->import_src, | |
535 | current->declaration, | |
8dea366b | 536 | block, domain, 1); |
a1d705ee TT |
537 | |
538 | /* If this is a DECLARATION_ONLY search or a symbol was found | |
539 | or this import statement was an import declaration, the | |
540 | search of this import is complete. */ | |
d12307c1 | 541 | if (declaration_only || sym.symbol != NULL || current->declaration) |
a1d705ee TT |
542 | { |
543 | current->searched = 0; | |
544 | discard_cleanups (searched_cleanup); | |
545 | ||
d12307c1 | 546 | if (sym.symbol != NULL) |
a1d705ee TT |
547 | return sym; |
548 | ||
549 | continue; | |
550 | } | |
551 | ||
552 | /* Do not follow CURRENT if NAME matches its EXCLUDES. */ | |
553 | for (excludep = current->excludes; *excludep; excludep++) | |
554 | if (strcmp (name, *excludep) == 0) | |
555 | break; | |
556 | if (*excludep) | |
557 | { | |
558 | discard_cleanups (searched_cleanup); | |
559 | continue; | |
560 | } | |
561 | ||
562 | if (current->alias != NULL | |
563 | && strcmp (name, current->alias) == 0) | |
564 | /* If the import is creating an alias and the alias matches | |
565 | the sought name. Pass current->import_src as the NAME to | |
566 | direct the search towards the aliased namespace. */ | |
567 | { | |
568 | sym = cp_lookup_symbol_in_namespace (scope, | |
569 | current->import_src, | |
8dea366b | 570 | block, domain, 1); |
a1d705ee TT |
571 | } |
572 | else if (current->alias == NULL) | |
573 | { | |
574 | /* If this import statement creates no alias, pass | |
575 | current->inner as NAMESPACE to direct the search | |
576 | towards the imported namespace. */ | |
55accf4a DE |
577 | sym = cp_lookup_symbol_via_imports (current->import_src, |
578 | name, block, | |
4bd0864e | 579 | domain, 1, 0, 0); |
a1d705ee TT |
580 | } |
581 | current->searched = 0; | |
582 | discard_cleanups (searched_cleanup); | |
583 | ||
d12307c1 | 584 | if (sym.symbol != NULL) |
a1d705ee | 585 | return sym; |
8540c487 SW |
586 | } |
587 | } | |
588 | ||
d12307c1 | 589 | return (struct block_symbol) {NULL, NULL}; |
8540c487 SW |
590 | } |
591 | ||
d5ff0482 | 592 | /* Helper function that searches an array of symbols for one named NAME. */ |
34eaf542 TT |
593 | |
594 | static struct symbol * | |
aff410f1 MS |
595 | search_symbol_list (const char *name, int num, |
596 | struct symbol **syms) | |
34eaf542 TT |
597 | { |
598 | int i; | |
599 | ||
600 | /* Maybe we should store a dictionary in here instead. */ | |
601 | for (i = 0; i < num; ++i) | |
602 | { | |
603 | if (strcmp (name, SYMBOL_NATURAL_NAME (syms[i])) == 0) | |
604 | return syms[i]; | |
605 | } | |
606 | return NULL; | |
607 | } | |
608 | ||
55accf4a | 609 | /* Like cp_lookup_symbol_via_imports, but if BLOCK is a function, it |
34eaf542 TT |
610 | searches through the template parameters of the function and the |
611 | function's type. */ | |
612 | ||
d12307c1 | 613 | struct block_symbol |
34eaf542 TT |
614 | cp_lookup_symbol_imports_or_template (const char *scope, |
615 | const char *name, | |
616 | const struct block *block, | |
617 | const domain_enum domain) | |
618 | { | |
619 | struct symbol *function = BLOCK_FUNCTION (block); | |
d12307c1 | 620 | struct block_symbol result; |
cc485e62 DE |
621 | |
622 | if (symbol_lookup_debug) | |
623 | { | |
624 | fprintf_unfiltered (gdb_stdlog, | |
625 | "cp_lookup_symbol_imports_or_template" | |
626 | " (%s, %s, %s, %s)\n", | |
627 | scope, name, host_address_to_string (block), | |
628 | domain_name (domain)); | |
629 | } | |
34eaf542 TT |
630 | |
631 | if (function != NULL && SYMBOL_LANGUAGE (function) == language_cplus) | |
632 | { | |
34eaf542 TT |
633 | /* Search the function's template parameters. */ |
634 | if (SYMBOL_IS_CPLUS_TEMPLATE_FUNCTION (function)) | |
635 | { | |
791244be | 636 | struct template_symbol *templ |
aff410f1 | 637 | = (struct template_symbol *) function; |
d12307c1 PMR |
638 | struct symbol *sym = search_symbol_list (name, |
639 | templ->n_template_arguments, | |
640 | templ->template_arguments); | |
34eaf542 | 641 | |
d12307c1 | 642 | if (sym != NULL) |
cc485e62 DE |
643 | { |
644 | if (symbol_lookup_debug) | |
645 | { | |
646 | fprintf_unfiltered (gdb_stdlog, | |
647 | "cp_lookup_symbol_imports_or_template" | |
648 | " (...) = %s\n", | |
d12307c1 | 649 | host_address_to_string (sym)); |
cc485e62 | 650 | } |
d12307c1 | 651 | return (struct block_symbol) {sym, block}; |
cc485e62 | 652 | } |
34eaf542 TT |
653 | } |
654 | ||
655 | /* Search the template parameters of the function's defining | |
656 | context. */ | |
657 | if (SYMBOL_NATURAL_NAME (function)) | |
658 | { | |
659 | struct type *context; | |
660 | char *name_copy = xstrdup (SYMBOL_NATURAL_NAME (function)); | |
661 | struct cleanup *cleanups = make_cleanup (xfree, name_copy); | |
662 | const struct language_defn *lang = language_def (language_cplus); | |
08be3fe3 | 663 | struct gdbarch *arch = symbol_arch (function); |
34eaf542 | 664 | const struct block *parent = BLOCK_SUPERBLOCK (block); |
d12307c1 | 665 | struct symbol *sym; |
34eaf542 TT |
666 | |
667 | while (1) | |
668 | { | |
34eaf542 TT |
669 | unsigned int prefix_len = cp_entire_prefix_len (name_copy); |
670 | ||
671 | if (prefix_len == 0) | |
672 | context = NULL; | |
673 | else | |
674 | { | |
675 | name_copy[prefix_len] = '\0'; | |
aff410f1 MS |
676 | context = lookup_typename (lang, arch, |
677 | name_copy, | |
678 | parent, 1); | |
34eaf542 TT |
679 | } |
680 | ||
681 | if (context == NULL) | |
682 | break; | |
683 | ||
d12307c1 | 684 | sym |
aff410f1 MS |
685 | = search_symbol_list (name, |
686 | TYPE_N_TEMPLATE_ARGUMENTS (context), | |
687 | TYPE_TEMPLATE_ARGUMENTS (context)); | |
d12307c1 | 688 | if (sym != NULL) |
c27e16e3 TT |
689 | { |
690 | do_cleanups (cleanups); | |
cc485e62 DE |
691 | if (symbol_lookup_debug) |
692 | { | |
d12307c1 PMR |
693 | fprintf_unfiltered |
694 | (gdb_stdlog, | |
695 | "cp_lookup_symbol_imports_or_template (...) = %s\n", | |
696 | host_address_to_string (sym)); | |
cc485e62 | 697 | } |
d12307c1 | 698 | return (struct block_symbol) {sym, parent}; |
c27e16e3 | 699 | } |
34eaf542 TT |
700 | } |
701 | ||
702 | do_cleanups (cleanups); | |
703 | } | |
704 | } | |
705 | ||
4bd0864e | 706 | result = cp_lookup_symbol_via_imports (scope, name, block, domain, 0, 1, 1); |
cc485e62 DE |
707 | if (symbol_lookup_debug) |
708 | { | |
709 | fprintf_unfiltered (gdb_stdlog, | |
710 | "cp_lookup_symbol_imports_or_template (...) = %s\n", | |
d12307c1 PMR |
711 | result.symbol != NULL |
712 | ? host_address_to_string (result.symbol) : "NULL"); | |
cc485e62 DE |
713 | } |
714 | return result; | |
34eaf542 TT |
715 | } |
716 | ||
f953163f DE |
717 | /* Search for NAME by applying relevant import statements belonging to BLOCK |
718 | and its parents. SCOPE is the namespace scope of the context in which the | |
719 | search is being evaluated. */ | |
720 | ||
d12307c1 | 721 | static struct block_symbol |
f953163f DE |
722 | cp_lookup_symbol_via_all_imports (const char *scope, const char *name, |
723 | const struct block *block, | |
724 | const domain_enum domain) | |
725 | { | |
d12307c1 | 726 | struct block_symbol sym; |
f953163f DE |
727 | |
728 | while (block != NULL) | |
729 | { | |
730 | sym = cp_lookup_symbol_via_imports (scope, name, block, domain, 0, 0, 1); | |
d12307c1 | 731 | if (sym.symbol) |
f953163f DE |
732 | return sym; |
733 | ||
734 | block = BLOCK_SUPERBLOCK (block); | |
735 | } | |
736 | ||
d12307c1 | 737 | return (struct block_symbol) {NULL, NULL}; |
f953163f DE |
738 | } |
739 | ||
791244be DE |
740 | /* Searches for NAME in the current namespace, and by applying |
741 | relevant import statements belonging to BLOCK and its parents. | |
742 | SCOPE is the namespace scope of the context in which the search is | |
743 | being evaluated. */ | |
8540c487 | 744 | |
d12307c1 | 745 | struct block_symbol |
8540c487 SW |
746 | cp_lookup_symbol_namespace (const char *scope, |
747 | const char *name, | |
8540c487 | 748 | const struct block *block, |
13387711 | 749 | const domain_enum domain) |
8540c487 | 750 | { |
d12307c1 | 751 | struct block_symbol sym; |
cc485e62 DE |
752 | |
753 | if (symbol_lookup_debug) | |
754 | { | |
755 | fprintf_unfiltered (gdb_stdlog, | |
756 | "cp_lookup_symbol_namespace (%s, %s, %s, %s)\n", | |
757 | scope, name, host_address_to_string (block), | |
758 | domain_name (domain)); | |
759 | } | |
760 | ||
13387711 | 761 | /* First, try to find the symbol in the given namespace. */ |
f953163f | 762 | sym = cp_lookup_symbol_in_namespace (scope, name, block, domain, 1); |
8540c487 | 763 | |
f953163f | 764 | /* Search for name in namespaces imported to this and parent blocks. */ |
d12307c1 | 765 | if (sym.symbol == NULL) |
f953163f | 766 | sym = cp_lookup_symbol_via_all_imports (scope, name, block, domain); |
8540c487 | 767 | |
cc485e62 DE |
768 | if (symbol_lookup_debug) |
769 | { | |
770 | fprintf_unfiltered (gdb_stdlog, | |
f953163f | 771 | "cp_lookup_symbol_namespace (...) = %s\n", |
d12307c1 PMR |
772 | sym.symbol != NULL |
773 | ? host_address_to_string (sym.symbol) : "NULL"); | |
cc485e62 | 774 | } |
f953163f | 775 | return sym; |
1fcb5155 DC |
776 | } |
777 | ||
778 | /* Lookup NAME at namespace scope (or, in C terms, in static and | |
779 | global variables). SCOPE is the namespace that the current | |
780 | function is defined within; only consider namespaces whose length | |
781 | is at least SCOPE_LEN. Other arguments are as in | |
782 | cp_lookup_symbol_nonlocal. | |
783 | ||
784 | For example, if we're within a function A::B::f and looking for a | |
3882f37a | 785 | symbol x, this will get called with NAME = "x", SCOPE = "A::B", and |
1fcb5155 DC |
786 | SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same, |
787 | but with SCOPE_LEN = 1. And then it calls itself with NAME and | |
788 | SCOPE the same, but with SCOPE_LEN = 4. This third call looks for | |
789 | "A::B::x"; if it doesn't find it, then the second call looks for | |
790 | "A::x", and if that call fails, then the first call looks for | |
791 | "x". */ | |
792 | ||
d12307c1 | 793 | static struct block_symbol |
1994afbf DE |
794 | lookup_namespace_scope (const struct language_defn *langdef, |
795 | const char *name, | |
1fcb5155 DC |
796 | const struct block *block, |
797 | const domain_enum domain, | |
1fcb5155 DC |
798 | const char *scope, |
799 | int scope_len) | |
800 | { | |
fe978cb0 | 801 | char *the_namespace; |
1fcb5155 DC |
802 | |
803 | if (scope[scope_len] != '\0') | |
804 | { | |
805 | /* Recursively search for names in child namespaces first. */ | |
806 | ||
d12307c1 | 807 | struct block_symbol sym; |
1fcb5155 DC |
808 | int new_scope_len = scope_len; |
809 | ||
810 | /* If the current scope is followed by "::", skip past that. */ | |
811 | if (new_scope_len != 0) | |
812 | { | |
813 | gdb_assert (scope[new_scope_len] == ':'); | |
814 | new_scope_len += 2; | |
815 | } | |
816 | new_scope_len += cp_find_first_component (scope + new_scope_len); | |
1994afbf | 817 | sym = lookup_namespace_scope (langdef, name, block, domain, |
aff410f1 | 818 | scope, new_scope_len); |
d12307c1 | 819 | if (sym.symbol != NULL) |
1fcb5155 DC |
820 | return sym; |
821 | } | |
822 | ||
823 | /* Okay, we didn't find a match in our children, so look for the | |
1994afbf DE |
824 | name in the current namespace. |
825 | ||
826 | If we there is no scope and we know we have a bare symbol, then short | |
827 | circuit everything and call cp_lookup_bare_symbol directly. | |
828 | This isn't an optimization, rather it allows us to pass LANGDEF which | |
829 | is needed for primitive type lookup. The test doesn't have to be | |
830 | perfect: if NAME is a bare symbol that our test doesn't catch (e.g., a | |
831 | template symbol with "::" in the argument list) then | |
832 | cp_lookup_symbol_in_namespace will catch it. */ | |
833 | ||
834 | if (scope_len == 0 && strchr (name, ':') == NULL) | |
835 | return cp_lookup_bare_symbol (langdef, name, block, domain, 1); | |
1fcb5155 | 836 | |
fe978cb0 PA |
837 | the_namespace = alloca (scope_len + 1); |
838 | strncpy (the_namespace, scope, scope_len); | |
839 | the_namespace[scope_len] = '\0'; | |
840 | return cp_lookup_symbol_in_namespace (the_namespace, name, | |
8dea366b | 841 | block, domain, 1); |
1fcb5155 DC |
842 | } |
843 | ||
56286edf DE |
844 | /* The C++-specific version of name lookup for static and global |
845 | names. This makes sure that names get looked for in all namespaces | |
846 | that are in scope. NAME is the natural name of the symbol that | |
847 | we're looking for, BLOCK is the block that we're searching within, | |
848 | DOMAIN says what kind of symbols we're looking for. */ | |
849 | ||
d12307c1 | 850 | struct block_symbol |
f606139a DE |
851 | cp_lookup_symbol_nonlocal (const struct language_defn *langdef, |
852 | const char *name, | |
56286edf DE |
853 | const struct block *block, |
854 | const domain_enum domain) | |
855 | { | |
d12307c1 | 856 | struct block_symbol sym; |
56286edf DE |
857 | const char *scope = block_scope (block); |
858 | ||
cc485e62 DE |
859 | if (symbol_lookup_debug) |
860 | { | |
861 | fprintf_unfiltered (gdb_stdlog, | |
862 | "cp_lookup_symbol_non_local" | |
863 | " (%s, %s (scope %s), %s)\n", | |
864 | name, host_address_to_string (block), scope, | |
865 | domain_name (domain)); | |
866 | } | |
867 | ||
f953163f DE |
868 | /* First, try to find the symbol in the given namespace, and all |
869 | containing namespaces. */ | |
1994afbf | 870 | sym = lookup_namespace_scope (langdef, name, block, domain, scope, 0); |
56286edf | 871 | |
f953163f | 872 | /* Search for name in namespaces imported to this and parent blocks. */ |
d12307c1 | 873 | if (sym.symbol == NULL) |
f953163f DE |
874 | sym = cp_lookup_symbol_via_all_imports (scope, name, block, domain); |
875 | ||
cc485e62 DE |
876 | if (symbol_lookup_debug) |
877 | { | |
878 | fprintf_unfiltered (gdb_stdlog, | |
879 | "cp_lookup_symbol_nonlocal (...) = %s\n", | |
d12307c1 PMR |
880 | (sym.symbol != NULL |
881 | ? host_address_to_string (sym.symbol) | |
882 | : "NULL")); | |
cc485e62 DE |
883 | } |
884 | return sym; | |
56286edf DE |
885 | } |
886 | ||
f7e3ecae KS |
887 | /* Search through the base classes of PARENT_TYPE for a base class |
888 | named NAME and return its type. If not found, return NULL. */ | |
889 | ||
890 | struct type * | |
a07e3e18 | 891 | cp_find_type_baseclass_by_name (struct type *parent_type, const char *name) |
f7e3ecae KS |
892 | { |
893 | int i; | |
894 | ||
f168693b | 895 | parent_type = check_typedef (parent_type); |
f7e3ecae KS |
896 | for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i) |
897 | { | |
898 | struct type *type = check_typedef (TYPE_BASECLASS (parent_type, i)); | |
899 | const char *base_name = TYPE_BASECLASS_NAME (parent_type, i); | |
900 | ||
901 | if (base_name == NULL) | |
902 | continue; | |
903 | ||
904 | if (streq (base_name, name)) | |
905 | return type; | |
906 | ||
a07e3e18 | 907 | type = cp_find_type_baseclass_by_name (type, name); |
f7e3ecae KS |
908 | if (type != NULL) |
909 | return type; | |
910 | } | |
911 | ||
912 | return NULL; | |
913 | } | |
914 | ||
8dea366b KS |
915 | /* Search through the base classes of PARENT_TYPE for a symbol named |
916 | NAME in block BLOCK. */ | |
917 | ||
d12307c1 | 918 | static struct block_symbol |
8dea366b | 919 | find_symbol_in_baseclass (struct type *parent_type, const char *name, |
4dcabcc2 DE |
920 | const struct block *block, const domain_enum domain, |
921 | int is_in_anonymous) | |
8dea366b KS |
922 | { |
923 | int i; | |
d12307c1 | 924 | struct block_symbol sym; |
8dea366b KS |
925 | struct cleanup *cleanup; |
926 | char *concatenated_name; | |
927 | ||
d12307c1 PMR |
928 | sym.symbol = NULL; |
929 | sym.block = NULL; | |
8dea366b KS |
930 | concatenated_name = NULL; |
931 | cleanup = make_cleanup (free_current_contents, &concatenated_name); | |
791244be | 932 | |
8dea366b KS |
933 | for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i) |
934 | { | |
935 | size_t len; | |
69fc87c2 | 936 | struct type *base_type = TYPE_BASECLASS (parent_type, i); |
8dea366b KS |
937 | const char *base_name = TYPE_BASECLASS_NAME (parent_type, i); |
938 | ||
939 | if (base_name == NULL) | |
940 | continue; | |
941 | ||
8dea366b KS |
942 | len = strlen (base_name) + 2 + strlen (name) + 1; |
943 | concatenated_name = xrealloc (concatenated_name, len); | |
944 | xsnprintf (concatenated_name, len, "%s::%s", base_name, name); | |
8dea366b | 945 | |
6f27419a | 946 | sym = cp_lookup_nested_symbol_1 (base_type, name, concatenated_name, |
4dcabcc2 | 947 | block, domain, 1, is_in_anonymous); |
d12307c1 | 948 | if (sym.symbol != NULL) |
69fc87c2 | 949 | break; |
8dea366b KS |
950 | } |
951 | ||
952 | do_cleanups (cleanup); | |
0c2e6019 | 953 | return sym; |
5c4e30ca DC |
954 | } |
955 | ||
4dcabcc2 DE |
956 | /* Helper function to look up NESTED_NAME in CONTAINER_TYPE and in DOMAIN |
957 | and within the context of BLOCK. | |
d5ff0482 | 958 | NESTED_NAME may have scope ("::"). |
6f27419a DE |
959 | CONTAINER_TYPE needn't have been "check_typedef'd" yet. |
960 | CONCATENATED_NAME is the fully scoped spelling of NESTED_NAME, it is | |
961 | passed as an argument so that callers can control how space for it is | |
962 | allocated. | |
963 | If BASIC_LOOKUP is non-zero then perform a basic lookup of | |
96553a0c DE |
964 | CONCATENATED_NAME. See cp_basic_lookup_symbol for details. |
965 | If IS_IN_ANONYMOUS is non-zero then CONCATENATED_NAME is in an anonymous | |
966 | namespace. */ | |
6f27419a | 967 | |
d12307c1 | 968 | static struct block_symbol |
6f27419a DE |
969 | cp_lookup_nested_symbol_1 (struct type *container_type, |
970 | const char *nested_name, | |
971 | const char *concatenated_name, | |
972 | const struct block *block, | |
4dcabcc2 | 973 | const domain_enum domain, |
96553a0c | 974 | int basic_lookup, int is_in_anonymous) |
6f27419a | 975 | { |
d12307c1 | 976 | struct block_symbol sym; |
6f27419a DE |
977 | |
978 | /* NOTE: carlton/2003-11-10: We don't treat C++ class members | |
979 | of classes like, say, data or function members. Instead, | |
980 | they're just represented by symbols whose names are | |
981 | qualified by the name of the surrounding class. This is | |
982 | just like members of namespaces; in particular, | |
983 | cp_basic_lookup_symbol works when looking them up. */ | |
984 | ||
985 | if (basic_lookup) | |
986 | { | |
4dcabcc2 | 987 | sym = cp_basic_lookup_symbol (concatenated_name, block, domain, |
6f27419a | 988 | is_in_anonymous); |
d12307c1 | 989 | if (sym.symbol != NULL) |
6f27419a DE |
990 | return sym; |
991 | } | |
992 | ||
993 | /* Now search all static file-level symbols. We have to do this for things | |
994 | like typedefs in the class. We do not try to guess any imported | |
995 | namespace as even the fully specified namespace search is already not | |
996 | C++ compliant and more assumptions could make it too magic. */ | |
997 | ||
998 | /* First search in this symtab, what we want is possibly there. */ | |
4dcabcc2 | 999 | sym = lookup_symbol_in_static_block (concatenated_name, block, domain); |
d12307c1 | 1000 | if (sym.symbol != NULL) |
6f27419a DE |
1001 | return sym; |
1002 | ||
1003 | /* Nope. We now have to search all static blocks in all objfiles, | |
1004 | even if block != NULL, because there's no guarantees as to which | |
96553a0c DE |
1005 | symtab the symbol we want is in. Except for symbols defined in |
1006 | anonymous namespaces should be treated as local to a single file, | |
1007 | which we just searched. */ | |
1008 | if (!is_in_anonymous) | |
1009 | { | |
4dcabcc2 | 1010 | sym = lookup_static_symbol (concatenated_name, domain); |
d12307c1 | 1011 | if (sym.symbol != NULL) |
96553a0c DE |
1012 | return sym; |
1013 | } | |
6f27419a DE |
1014 | |
1015 | /* If this is a class with baseclasses, search them next. */ | |
f168693b | 1016 | container_type = check_typedef (container_type); |
6f27419a DE |
1017 | if (TYPE_N_BASECLASSES (container_type) > 0) |
1018 | { | |
96553a0c | 1019 | sym = find_symbol_in_baseclass (container_type, nested_name, block, |
4dcabcc2 | 1020 | domain, is_in_anonymous); |
d12307c1 | 1021 | if (sym.symbol != NULL) |
6f27419a DE |
1022 | return sym; |
1023 | } | |
1024 | ||
d12307c1 | 1025 | return (struct block_symbol) {NULL, NULL}; |
6f27419a DE |
1026 | } |
1027 | ||
50af5481 | 1028 | /* Look up a symbol named NESTED_NAME that is nested inside the C++ |
79c2c32d | 1029 | class or namespace given by PARENT_TYPE, from within the context |
4dcabcc2 DE |
1030 | given by BLOCK, and in DOMAIN. |
1031 | Return NULL if there is no such nested symbol. */ | |
79c2c32d | 1032 | |
d12307c1 | 1033 | struct block_symbol |
50af5481 JK |
1034 | cp_lookup_nested_symbol (struct type *parent_type, |
1035 | const char *nested_name, | |
4dcabcc2 DE |
1036 | const struct block *block, |
1037 | const domain_enum domain) | |
79c2c32d | 1038 | { |
05a6c3c8 | 1039 | /* type_name_no_tag_or_error provides better error reporting using the |
d8228535 JK |
1040 | original type. */ |
1041 | struct type *saved_parent_type = parent_type; | |
1042 | ||
f168693b | 1043 | parent_type = check_typedef (parent_type); |
d8228535 | 1044 | |
cc485e62 DE |
1045 | if (symbol_lookup_debug) |
1046 | { | |
1047 | const char *type_name = type_name_no_tag (saved_parent_type); | |
1048 | ||
1049 | fprintf_unfiltered (gdb_stdlog, | |
4dcabcc2 | 1050 | "cp_lookup_nested_symbol (%s, %s, %s, %s)\n", |
cc485e62 | 1051 | type_name != NULL ? type_name : "unnamed", |
4dcabcc2 DE |
1052 | nested_name, host_address_to_string (block), |
1053 | domain_name (domain)); | |
cc485e62 DE |
1054 | } |
1055 | ||
79c2c32d DC |
1056 | switch (TYPE_CODE (parent_type)) |
1057 | { | |
63d06c5c | 1058 | case TYPE_CODE_STRUCT: |
79c2c32d | 1059 | case TYPE_CODE_NAMESPACE: |
48e32051 | 1060 | case TYPE_CODE_UNION: |
3d567982 | 1061 | case TYPE_CODE_ENUM: |
530e8392 KB |
1062 | /* NOTE: Handle modules here as well, because Fortran is re-using the C++ |
1063 | specific code to lookup nested symbols in modules, by calling the | |
1064 | function pointer la_lookup_symbol_nonlocal, which ends up here. */ | |
1065 | case TYPE_CODE_MODULE: | |
79c2c32d | 1066 | { |
08850b56 | 1067 | int size; |
d8228535 | 1068 | const char *parent_name = type_name_no_tag_or_error (saved_parent_type); |
d12307c1 | 1069 | struct block_symbol sym; |
41f62f39 | 1070 | char *concatenated_name; |
96553a0c | 1071 | int is_in_anonymous; |
c5504eaf | 1072 | |
08850b56 PM |
1073 | size = strlen (parent_name) + 2 + strlen (nested_name) + 1; |
1074 | concatenated_name = alloca (size); | |
1075 | xsnprintf (concatenated_name, size, "%s::%s", | |
6f27419a | 1076 | parent_name, nested_name); |
96553a0c | 1077 | is_in_anonymous = cp_is_in_anonymous (concatenated_name); |
6f27419a DE |
1078 | |
1079 | sym = cp_lookup_nested_symbol_1 (parent_type, nested_name, | |
4dcabcc2 DE |
1080 | concatenated_name, block, domain, |
1081 | 1, is_in_anonymous); | |
41f62f39 | 1082 | |
cc485e62 DE |
1083 | if (symbol_lookup_debug) |
1084 | { | |
1085 | fprintf_unfiltered (gdb_stdlog, | |
1086 | "cp_lookup_nested_symbol (...) = %s\n", | |
d12307c1 PMR |
1087 | (sym.symbol != NULL |
1088 | ? host_address_to_string (sym.symbol) | |
1089 | : "NULL")); | |
cc485e62 DE |
1090 | } |
1091 | return sym; | |
79c2c32d | 1092 | } |
bb869963 SDJ |
1093 | |
1094 | case TYPE_CODE_FUNC: | |
1095 | case TYPE_CODE_METHOD: | |
cc485e62 DE |
1096 | if (symbol_lookup_debug) |
1097 | { | |
1098 | fprintf_unfiltered (gdb_stdlog, | |
1099 | "cp_lookup_nested_symbol (...) = NULL" | |
1100 | " (func/method)\n"); | |
1101 | } | |
d12307c1 | 1102 | return (struct block_symbol) {NULL, NULL}; |
bb869963 | 1103 | |
79c2c32d DC |
1104 | default: |
1105 | internal_error (__FILE__, __LINE__, | |
50af5481 | 1106 | _("cp_lookup_nested_symbol called " |
3e43a32a | 1107 | "on a non-aggregate type.")); |
79c2c32d DC |
1108 | } |
1109 | } | |
1110 | ||
b368761e DC |
1111 | /* The C++-version of lookup_transparent_type. */ |
1112 | ||
1113 | /* FIXME: carlton/2004-01-16: The problem that this is trying to | |
1114 | address is that, unfortunately, sometimes NAME is wrong: it may not | |
1115 | include the name of namespaces enclosing the type in question. | |
b021a221 | 1116 | lookup_transparent_type gets called when the type in question |
b368761e DC |
1117 | is a declaration, and we're trying to find its definition; but, for |
1118 | declarations, our type name deduction mechanism doesn't work. | |
1119 | There's nothing we can do to fix this in general, I think, in the | |
1120 | absence of debug information about namespaces (I've filed PR | |
1121 | gdb/1511 about this); until such debug information becomes more | |
1122 | prevalent, one heuristic which sometimes looks is to search for the | |
1123 | definition in namespaces containing the current namespace. | |
1124 | ||
1125 | We should delete this functions once the appropriate debug | |
1126 | information becomes more widespread. (GCC 3.4 will be the first | |
1127 | released version of GCC with such information.) */ | |
1128 | ||
1129 | struct type * | |
1130 | cp_lookup_transparent_type (const char *name) | |
1131 | { | |
1132 | /* First, try the honest way of looking up the definition. */ | |
1133 | struct type *t = basic_lookup_transparent_type (name); | |
1134 | const char *scope; | |
1135 | ||
1136 | if (t != NULL) | |
1137 | return t; | |
1138 | ||
1139 | /* If that doesn't work and we're within a namespace, look there | |
1140 | instead. */ | |
1141 | scope = block_scope (get_selected_block (0)); | |
1142 | ||
1143 | if (scope[0] == '\0') | |
1144 | return NULL; | |
1145 | ||
1146 | return cp_lookup_transparent_type_loop (name, scope, 0); | |
1147 | } | |
1148 | ||
b021a221 MS |
1149 | /* Lookup the type definition associated to NAME in namespaces/classes |
1150 | containing SCOPE whose name is strictly longer than LENGTH. LENGTH | |
1151 | must be the index of the start of a component of SCOPE. */ | |
b368761e DC |
1152 | |
1153 | static struct type * | |
aff410f1 MS |
1154 | cp_lookup_transparent_type_loop (const char *name, |
1155 | const char *scope, | |
b368761e DC |
1156 | int length) |
1157 | { | |
1198ecbe | 1158 | int scope_length = length + cp_find_first_component (scope + length); |
b368761e DC |
1159 | char *full_name; |
1160 | ||
1161 | /* If the current scope is followed by "::", look in the next | |
1162 | component. */ | |
1163 | if (scope[scope_length] == ':') | |
1164 | { | |
1165 | struct type *retval | |
aff410f1 MS |
1166 | = cp_lookup_transparent_type_loop (name, scope, |
1167 | scope_length + 2); | |
c5504eaf | 1168 | |
b368761e DC |
1169 | if (retval != NULL) |
1170 | return retval; | |
1171 | } | |
1172 | ||
1173 | full_name = alloca (scope_length + 2 + strlen (name) + 1); | |
1174 | strncpy (full_name, scope, scope_length); | |
1175 | strncpy (full_name + scope_length, "::", 2); | |
1176 | strcpy (full_name + scope_length + 2, name); | |
1177 | ||
1178 | return basic_lookup_transparent_type (full_name); | |
1179 | } | |
1180 | ||
0c2e6019 TT |
1181 | /* This used to do something but was removed when it became |
1182 | obsolete. */ | |
5c4e30ca DC |
1183 | |
1184 | static void | |
1185 | maintenance_cplus_namespace (char *args, int from_tty) | |
1186 | { | |
0c2e6019 | 1187 | printf_unfiltered (_("The `maint namespace' command was removed.\n")); |
5c4e30ca DC |
1188 | } |
1189 | ||
2c0b251b PA |
1190 | /* Provide a prototype to silence -Wmissing-prototypes. */ |
1191 | extern initialize_file_ftype _initialize_cp_namespace; | |
1192 | ||
5c4e30ca DC |
1193 | void |
1194 | _initialize_cp_namespace (void) | |
1195 | { | |
0c2e6019 TT |
1196 | struct cmd_list_element *cmd; |
1197 | ||
1198 | cmd = add_cmd ("namespace", class_maintenance, | |
1199 | maintenance_cplus_namespace, | |
1200 | _("Deprecated placeholder for removed functionality."), | |
1201 | &maint_cplus_cmd_list); | |
1202 | deprecate_cmd (cmd, NULL); | |
1fcb5155 | 1203 | } |