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