testsuite, threads: fix LD_LIBRARY_PATH in 'tls-sepdebug.exp'
[binutils-gdb.git] / gdb / cp-namespace.c
blob9d86fe2722863e6dccc197b2dff4f3ba1d0852b1
1 /* Helper routines for C++ support in GDB.
2 Copyright (C) 2003-2024 Free Software Foundation, Inc.
4 Contributed by David Carlton and by Kealia, Inc.
6 This file is part of GDB.
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
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "cp-support.h"
22 #include "gdbsupport/gdb_obstack.h"
23 #include "symtab.h"
24 #include "symfile.h"
25 #include "block.h"
26 #include "objfiles.h"
27 #include "gdbtypes.h"
28 #include "dictionary.h"
29 #include "command.h"
30 #include "frame.h"
31 #include "buildsym.h"
32 #include "language.h"
33 #include "namespace.h"
34 #include "inferior.h"
35 #include <map>
36 #include <string>
37 #include <string.h>
39 static struct block_symbol
40 cp_lookup_nested_symbol_1 (struct type *container_type,
41 const char *nested_name,
42 const char *concatenated_name,
43 const struct block *block,
44 const domain_search_flags domain,
45 int basic_lookup, int is_in_anonymous);
47 static struct type *cp_lookup_transparent_type_loop (const char *name,
48 const char *scope,
49 int scope_len);
51 /* Check to see if SYMBOL refers to an object contained within an
52 anonymous namespace; if so, add an appropriate using directive. */
54 void
55 cp_scan_for_anonymous_namespaces (struct buildsym_compunit *compunit,
56 const struct symbol *const symbol,
57 struct objfile *const objfile)
59 if (symbol->demangled_name () != NULL)
61 const char *name = symbol->demangled_name ();
62 unsigned int previous_component;
63 unsigned int next_component;
65 /* Start with a quick-and-dirty check for mention of "(anonymous
66 namespace)". */
68 if (!cp_is_in_anonymous (name))
69 return;
71 previous_component = 0;
72 next_component = cp_find_first_component (name + previous_component);
74 while (name[next_component] == ':')
76 if (((next_component - previous_component)
77 == CP_ANONYMOUS_NAMESPACE_LEN)
78 && strncmp (name + previous_component,
79 CP_ANONYMOUS_NAMESPACE_STR,
80 CP_ANONYMOUS_NAMESPACE_LEN) == 0)
82 int dest_len = (previous_component == 0
83 ? 0 : previous_component - 2);
84 int src_len = next_component;
86 char *dest = (char *) alloca (dest_len + 1);
87 char *src = (char *) alloca (src_len + 1);
89 memcpy (dest, name, dest_len);
90 memcpy (src, name, src_len);
92 dest[dest_len] = '\0';
93 src[src_len] = '\0';
95 /* We've found a component of the name that's an
96 anonymous namespace. So add symbols in it to the
97 namespace given by the previous component if there is
98 one, or to the global namespace if there isn't.
99 The declared line of this using directive can be set
100 to 0, this way it is always considered valid. */
101 std::vector<const char *> excludes;
102 add_using_directive (compunit->get_local_using_directives (),
103 objfile->intern (dest), objfile->intern (src),
104 nullptr, nullptr, excludes, 0,
105 &objfile->objfile_obstack);
107 /* The "+ 2" is for the "::". */
108 previous_component = next_component + 2;
109 next_component = (previous_component
110 + cp_find_first_component (name
111 + previous_component));
116 /* Test whether or not NAMESPACE looks like it mentions an anonymous
117 namespace; return nonzero if so. */
120 cp_is_in_anonymous (const char *symbol_name)
122 return (strstr (symbol_name, CP_ANONYMOUS_NAMESPACE_STR)
123 != NULL);
126 /* Look up NAME in DOMAIN in BLOCK's static block and in global blocks.
127 If IS_IN_ANONYMOUS is nonzero, the symbol in question is located
128 within an anonymous namespace. */
130 static struct block_symbol
131 cp_basic_lookup_symbol (const char *name, const struct block *block,
132 const domain_search_flags domain, int is_in_anonymous)
134 struct block_symbol sym;
136 sym = lookup_symbol_in_static_block (name, block, domain);
137 if (sym.symbol != NULL)
138 return sym;
140 if (is_in_anonymous)
142 /* Symbols defined in anonymous namespaces have external linkage
143 but should be treated as local to a single file nonetheless.
144 So we only search the current file's global block. */
146 const struct block *global_block = block->global_block ();
148 if (global_block != NULL)
150 sym.symbol = lookup_symbol_in_block (name,
151 symbol_name_match_type::FULL,
152 global_block, domain);
153 sym.block = global_block;
156 else
157 sym = lookup_global_symbol (name, block, domain);
159 return sym;
162 /* Search bare symbol NAME in DOMAIN in BLOCK.
163 NAME is guaranteed to not have any scope (no "::") in its name, though
164 if for example NAME is a template spec then "::" may appear in the
165 argument list.
166 If LANGDEF is non-NULL then try to lookup NAME as a primitive type in
167 that language. Normally we wouldn't need LANGDEF but fortran also uses
168 this code.
169 If SEARCH is non-zero then see if we can determine "this" from BLOCK, and
170 if so then also search for NAME in that class. */
172 static struct block_symbol
173 cp_lookup_bare_symbol (const struct language_defn *langdef,
174 const char *name, const struct block *block,
175 const domain_search_flags domain, int search)
177 struct block_symbol sym;
179 /* Note: We can't do a simple assert for ':' not being in NAME because
180 ':' may be in the args of a template spec. This isn't intended to be
181 a complete test, just cheap and documentary. */
182 gdb_assert (strpbrk ("<>()", name) != nullptr
183 || strstr (name, "::") == nullptr);
185 sym = lookup_symbol_in_static_block (name, block, domain);
186 if (sym.symbol != NULL)
187 return sym;
189 /* If we didn't find a definition for a builtin type in the static block,
190 search for it now. This is actually the right thing to do and can be
191 a massive performance win. E.g., when debugging a program with lots of
192 shared libraries we could search all of them only to find out the
193 builtin type isn't defined in any of them. This is common for types
194 like "void". */
195 if (langdef != nullptr && (domain & SEARCH_TYPE_DOMAIN) != 0)
197 struct gdbarch *gdbarch;
199 if (block == NULL)
200 gdbarch = current_inferior ()->arch ();
201 else
202 gdbarch = block->gdbarch ();
203 sym.symbol
204 = language_lookup_primitive_type_as_symbol (langdef, gdbarch, name);
205 sym.block = NULL;
206 if (sym.symbol != NULL)
207 return sym;
210 sym = lookup_global_symbol (name, block, domain);
211 if (sym.symbol != NULL)
212 return sym;
214 if (search)
216 struct block_symbol lang_this;
217 struct type *type;
219 lang_this.symbol = NULL;
221 if (langdef != NULL)
222 lang_this = lookup_language_this (langdef, block);
224 if (lang_this.symbol == NULL)
225 return {};
228 type = check_typedef (lang_this.symbol->type ()->target_type ());
229 /* If TYPE_NAME is NULL, abandon trying to find this symbol.
230 This can happen for lambda functions compiled with clang++,
231 which outputs no name for the container class. */
232 if (type->name () == NULL)
233 return {};
235 /* Look for symbol NAME in this class. */
236 sym = cp_lookup_nested_symbol (type, name, block, domain);
239 return sym;
242 /* Search NAME in DOMAIN in all static blocks, and then in all baseclasses.
243 BLOCK specifies the context in which to perform the search.
244 NAME is guaranteed to have scope (contain "::") and PREFIX_LEN specifies
245 the length of the entire scope of NAME (up to, but not including, the last
246 "::".
248 Note: At least in the case of Fortran, which also uses this code, there
249 may be no text after the last "::". */
251 static struct block_symbol
252 cp_search_static_and_baseclasses (const char *name,
253 const struct block *block,
254 const domain_search_flags domain,
255 unsigned int prefix_len,
256 int is_in_anonymous)
258 /* Check for malformed input. */
259 if (prefix_len + 2 > strlen (name) || name[prefix_len + 1] != ':')
260 return {};
262 /* The class, namespace or function name is everything up to and
263 including PREFIX_LEN. */
264 std::string scope (name, prefix_len);
266 /* The rest of the name is everything else past the initial scope
267 operator. */
268 const char *nested = name + prefix_len + 2;
270 /* Lookup the scope symbol. If none is found, there is nothing more
271 that can be done. SCOPE could be a namespace, a class, or even a
272 function. This code is also used by Fortran, so modules are
273 included in the search as well. */
274 block_symbol scope_sym
275 = lookup_symbol_in_static_block (scope.c_str (), block,
276 SEARCH_TYPE_DOMAIN
277 | SEARCH_FUNCTION_DOMAIN
278 | SEARCH_MODULE_DOMAIN);
279 if (scope_sym.symbol == NULL)
280 scope_sym = lookup_global_symbol (scope.c_str (), block,
281 SEARCH_TYPE_DOMAIN
282 | SEARCH_FUNCTION_DOMAIN
283 | SEARCH_MODULE_DOMAIN);
284 if (scope_sym.symbol == NULL)
285 return {};
287 struct type *scope_type = scope_sym.symbol->type ();
289 /* If the scope is a function/method, then look up NESTED as a local
290 static variable or type. E.g., "print 'function()::static_var'". */
291 if ((scope_type->code () == TYPE_CODE_FUNC
292 || scope_type->code () == TYPE_CODE_METHOD)
293 && (domain & (SEARCH_VAR_DOMAIN | SEARCH_TYPE_DOMAIN)) != 0)
294 return lookup_symbol (nested, scope_sym.symbol->value_block (),
295 domain, NULL);
297 /* Look for a symbol named NESTED in this class/namespace.
298 The caller is assumed to have already have done a basic lookup of NAME.
299 So we pass zero for BASIC_LOOKUP to cp_lookup_nested_symbol_1 here. */
300 return cp_lookup_nested_symbol_1 (scope_type, nested, name,
301 block, domain, 0, is_in_anonymous);
304 /* Look up NAME in the C++ namespace NAMESPACE. Other arguments are
305 as in cp_lookup_symbol_nonlocal. If SEARCH is non-zero, search
306 through base classes for a matching symbol.
308 Note: Part of the complexity is because NAME may itself specify scope.
309 Part of the complexity is also because this handles the case where
310 there is no scoping in which case we also try looking in the class of
311 "this" if we can compute it. */
313 static struct block_symbol
314 cp_lookup_symbol_in_namespace (const char *the_namespace, const char *name,
315 const struct block *block,
316 const domain_search_flags domain, int search)
318 char *concatenated_name = NULL;
319 int is_in_anonymous;
320 unsigned int prefix_len;
321 struct block_symbol sym;
323 if (the_namespace[0] != '\0')
325 concatenated_name
326 = (char *) alloca (strlen (the_namespace) + 2 + strlen (name) + 1);
327 strcpy (concatenated_name, the_namespace);
328 strcat (concatenated_name, "::");
329 strcat (concatenated_name, name);
330 name = concatenated_name;
333 prefix_len = cp_entire_prefix_len (name);
334 if (prefix_len == 0)
335 return cp_lookup_bare_symbol (NULL, name, block, domain, search);
337 /* This would be simpler if we just called cp_lookup_nested_symbol
338 at this point. But that would require first looking up the containing
339 class/namespace. Since we're only searching static and global blocks
340 there's often no need to first do that lookup. */
342 is_in_anonymous
343 = the_namespace[0] != '\0' && cp_is_in_anonymous (the_namespace);
344 sym = cp_basic_lookup_symbol (name, block, domain, is_in_anonymous);
345 if (sym.symbol != NULL)
346 return sym;
348 if (search)
349 sym = cp_search_static_and_baseclasses (name, block, domain, prefix_len,
350 is_in_anonymous);
352 return sym;
355 /* This version of the function is internal, use the wrapper unless
356 the list of ambiguous symbols is needed.
358 Search for NAME by applying all import statements belonging to
359 BLOCK which are applicable in SCOPE. If DECLARATION_ONLY the
360 search is restricted to using declarations.
361 Example:
363 namespace A {
364 int x;
366 using A::x;
368 If SEARCH_PARENTS the search will include imports which are
369 applicable in parents of SCOPE.
370 Example:
372 namespace A {
373 using namespace X;
374 namespace B {
375 using namespace Y;
379 If SCOPE is "A::B" and SEARCH_PARENTS is true the imports of
380 namespaces X and Y will be considered. If SEARCH_PARENTS is false
381 only the import of Y is considered.
383 SEARCH_SCOPE_FIRST is an internal implementation detail: Callers must
384 pass 0 for it. Internally we pass 1 when recursing. */
386 static void
387 cp_lookup_symbol_via_imports (const char *scope,
388 const char *name,
389 const struct block *block,
390 const domain_search_flags domain,
391 const int search_scope_first,
392 const int declaration_only,
393 const int search_parents,
394 std::map<std::string,
395 struct block_symbol>& found_symbols)
397 struct using_direct *current;
398 struct block_symbol sym = {};
399 int len;
400 int directive_match;
402 /* All the symbols we found will be kept in this relational map between
403 the mangled name and the block_symbol found. We do this so that GDB
404 won't incorrectly report an ambiguous symbol for finding the same
405 thing twice. */
407 /* First, try to find the symbol in the given namespace if requested. */
408 if (search_scope_first)
410 sym = cp_lookup_symbol_in_namespace (scope, name,
411 block, domain, 1);
412 if (sym.symbol != nullptr)
413 found_symbols[sym.symbol->m_name] = sym;
416 /* Due to a GCC bug, we need to know the boundaries of the current block
417 to know if a certain using directive is valid. */
418 symtab_and_line boundary_sal = find_pc_line (block->end () - 1, 0);
420 /* Go through the using directives. If any of them add new names to
421 the namespace we're searching in, see if we can find a match by
422 applying them. */
423 for (current = block->get_using ();
424 current != NULL;
425 current = current->next)
427 const char **excludep;
429 /* If the using directive was below the place we are stopped at,
430 do not use this directive. */
431 if (!current->valid_line (boundary_sal.line))
432 continue;
433 len = strlen (current->import_dest);
434 directive_match = (search_parents
435 ? (startswith (scope, current->import_dest)
436 && (len == 0
437 || scope[len] == ':'
438 || scope[len] == '\0'))
439 : strcmp (scope, current->import_dest) == 0);
441 /* If the import destination is the current scope or one of its
442 ancestors then it is applicable. */
443 if (directive_match && !current->searched)
445 /* Mark this import as searched so that the recursive call
446 does not search it again. */
447 scoped_restore reset_directive_searched
448 = make_scoped_restore (&current->searched, 1);
450 /* If there is an import of a single declaration, compare the
451 imported declaration (after optional renaming by its alias)
452 with the sought out name. If there is a match pass
453 current->import_src as NAMESPACE to direct the search
454 towards the imported namespace. */
455 if (current->declaration
456 && strcmp (name, current->alias
457 ? current->alias : current->declaration) == 0)
458 sym = cp_lookup_symbol_in_namespace (current->import_src,
459 current->declaration,
460 block, domain, 1);
462 /* If this is a DECLARATION_ONLY search or a symbol was found
463 or this import statement was an import declaration, the
464 search of this import is complete. */
465 if (declaration_only || sym.symbol != NULL || current->declaration)
467 if (sym.symbol != NULL)
468 found_symbols[sym.symbol->m_name] = sym;
470 continue;
473 /* Do not follow CURRENT if NAME matches its EXCLUDES. */
474 for (excludep = current->excludes; *excludep; excludep++)
475 if (strcmp (name, *excludep) == 0)
476 break;
477 if (*excludep)
478 continue;
480 if (current->alias != NULL
481 && strcmp (name, current->alias) == 0)
482 /* If the import is creating an alias and the alias matches
483 the sought name. Pass current->import_src as the NAME to
484 direct the search towards the aliased namespace. */
486 sym = cp_lookup_symbol_in_namespace (scope,
487 current->import_src,
488 block, domain, 1);
489 found_symbols[sym.symbol->m_name] = sym;
491 else if (current->alias == NULL)
493 /* If this import statement creates no alias, pass
494 current->inner as NAMESPACE to direct the search
495 towards the imported namespace. */
496 cp_lookup_symbol_via_imports (current->import_src, name,
497 block, domain, 1, 0, 0,
498 found_symbols);
505 /* Wrapper for the actual cp_lookup_symbol_via_imports. This wrapper sets
506 search_scope_first correctly and handles errors if needed. */
507 static struct block_symbol
508 cp_lookup_symbol_via_imports (const char *scope,
509 const char *name,
510 const struct block *block,
511 const domain_search_flags domain,
512 const int declaration_only,
513 const int search_parents)
515 std::map<std::string, struct block_symbol> found_symbols;
517 cp_lookup_symbol_via_imports(scope, name, block, domain, 0,
518 declaration_only, search_parents,
519 found_symbols);
521 if (found_symbols.size () > 1)
523 auto itr = found_symbols.cbegin ();
524 std::string error_str = "Reference to \"";
525 error_str += name;
526 error_str += "\" is ambiguous, possibilities are: ";
527 error_str += itr->second.symbol->print_name ();
528 for (itr++; itr != found_symbols.end (); itr++)
530 error_str += " and ";
531 error_str += itr->second.symbol->print_name ();
533 error (_("%s"), error_str.c_str ());
536 if (found_symbols.size() == 1)
537 return found_symbols.cbegin ()->second;
538 else
539 return {};
542 /* Search for symbols whose name match NAME in the given SCOPE. */
544 struct block_symbol
545 cp_lookup_symbol_imports (const char *scope,
546 const char *name,
547 const struct block *block,
548 const domain_search_flags domain)
550 struct symbol *function = block->function ();
552 symbol_lookup_debug_printf
553 ("cp_lookup_symbol_imports (%s, %s, %s, %s)",
554 scope, name, host_address_to_string (block),
555 domain_name (domain).c_str ());
557 if (function != NULL && function->language () == language_cplus)
559 /* Search the template parameters of the function's defining
560 context. */
561 if (function->natural_name ())
563 struct type *context;
564 std::string name_copy (function->natural_name ());
565 const struct language_defn *lang = language_def (language_cplus);
566 const struct block *parent = block->superblock ();
567 struct symbol *sym;
569 while (1)
571 unsigned int prefix_len
572 = cp_entire_prefix_len (name_copy.c_str ());
574 if (prefix_len == 0)
575 context = NULL;
576 else
578 name_copy.erase (prefix_len);
579 context = lookup_typename (lang,
580 name_copy.c_str (),
581 parent, 1);
584 if (context == NULL)
585 break;
588 = search_symbol_list (name,
589 TYPE_N_TEMPLATE_ARGUMENTS (context),
590 TYPE_TEMPLATE_ARGUMENTS (context));
591 if (sym != NULL)
593 symbol_lookup_debug_printf
594 ("cp_lookup_symbol_imports (...) = %s",
595 host_address_to_string (sym));
596 return (struct block_symbol) {sym, parent};
602 struct block_symbol result
603 = cp_lookup_symbol_via_imports (scope, name, block, domain, 1, 1);
604 symbol_lookup_debug_printf ("cp_lookup_symbol_imports (...) = %s\n",
605 result.symbol != nullptr
606 ? host_address_to_string (result.symbol) : "NULL");
607 return result;
610 /* Search for NAME by applying relevant import statements belonging to BLOCK
611 and its parents. SCOPE is the namespace scope of the context in which the
612 search is being evaluated. */
614 static struct block_symbol
615 cp_lookup_symbol_via_all_imports (const char *scope, const char *name,
616 const struct block *block,
617 const domain_search_flags domain)
619 struct block_symbol sym;
621 while (block != NULL)
623 sym = cp_lookup_symbol_via_imports (scope, name, block, domain, 0, 1);
624 if (sym.symbol != nullptr)
625 return sym;
627 block = block->superblock ();
630 return {};
633 /* Searches for NAME in the current namespace, and by applying
634 relevant import statements belonging to BLOCK and its parents.
635 SCOPE is the namespace scope of the context in which the search is
636 being evaluated. */
638 struct block_symbol
639 cp_lookup_symbol_namespace (const char *scope,
640 const char *name,
641 const struct block *block,
642 const domain_search_flags domain)
644 struct block_symbol sym;
646 symbol_lookup_debug_printf ("cp_lookup_symbol_namespace (%s, %s, %s, %s)",
647 scope, name, host_address_to_string (block),
648 domain_name (domain).c_str ());
650 /* First, try to find the symbol in the given namespace. */
651 sym = cp_lookup_symbol_in_namespace (scope, name, block, domain, 1);
653 /* Search for name in namespaces imported to this and parent blocks. */
654 if (sym.symbol == NULL)
655 sym = cp_lookup_symbol_via_all_imports (scope, name, block, domain);
657 symbol_lookup_debug_printf ("cp_lookup_symbol_namespace (...) = %s",
658 sym.symbol != NULL
659 ? host_address_to_string (sym.symbol) : "NULL");
660 return sym;
663 /* Lookup NAME at namespace scope (or, in C terms, in static and
664 global variables). SCOPE is the namespace that the current
665 function is defined within; only consider namespaces whose length
666 is at least SCOPE_LEN. Other arguments are as in
667 cp_lookup_symbol_nonlocal.
669 For example, if we're within a function A::B::f and looking for a
670 symbol x, this will get called with NAME = "x", SCOPE = "A::B", and
671 SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same,
672 but with SCOPE_LEN = 1. And then it calls itself with NAME and
673 SCOPE the same, but with SCOPE_LEN = 4. This third call looks for
674 "A::B::x"; if it doesn't find it, then the second call looks for
675 "A::x", and if that call fails, then the first call looks for
676 "x". */
678 static struct block_symbol
679 lookup_namespace_scope (const struct language_defn *langdef,
680 const char *name,
681 const struct block *block,
682 const domain_search_flags domain,
683 const char *scope,
684 int scope_len)
686 char *the_namespace;
688 if (scope[scope_len] != '\0')
690 /* Recursively search for names in child namespaces first. */
692 struct block_symbol sym;
693 int new_scope_len = scope_len;
695 /* If the current scope is followed by "::", skip past that. */
696 if (new_scope_len != 0)
698 gdb_assert (scope[new_scope_len] == ':');
699 new_scope_len += 2;
701 new_scope_len += cp_find_first_component (scope + new_scope_len);
702 sym = lookup_namespace_scope (langdef, name, block, domain,
703 scope, new_scope_len);
704 if (sym.symbol != NULL)
705 return sym;
708 /* Okay, we didn't find a match in our children, so look for the
709 name in the current namespace.
711 If we there is no scope and we know we have a bare symbol, then short
712 circuit everything and call cp_lookup_bare_symbol directly.
713 This isn't an optimization, rather it allows us to pass LANGDEF which
714 is needed for primitive type lookup. The test doesn't have to be
715 perfect: if NAME is a bare symbol that our test doesn't catch (e.g., a
716 template symbol with "::" in the argument list) then
717 cp_lookup_symbol_in_namespace will catch it. */
719 if (scope_len == 0 && strchr (name, ':') == NULL)
720 return cp_lookup_bare_symbol (langdef, name, block, domain, 1);
722 the_namespace = (char *) alloca (scope_len + 1);
723 strncpy (the_namespace, scope, scope_len);
724 the_namespace[scope_len] = '\0';
725 return cp_lookup_symbol_in_namespace (the_namespace, name,
726 block, domain, 1);
729 /* The C++-specific version of name lookup for static and global
730 names. This makes sure that names get looked for in all namespaces
731 that are in scope. NAME is the natural name of the symbol that
732 we're looking for, BLOCK is the block that we're searching within,
733 DOMAIN says what kind of symbols we're looking for. */
735 struct block_symbol
736 cp_lookup_symbol_nonlocal (const struct language_defn *langdef,
737 const char *name,
738 const struct block *block,
739 const domain_search_flags domain)
741 struct block_symbol sym;
742 const char *scope = block == nullptr ? "" : block->scope ();
744 symbol_lookup_debug_printf
745 ("cp_lookup_symbol_non_local (%s, %s (scope %s), %s)",
746 name, host_address_to_string (block), scope,
747 domain_name (domain).c_str ());
749 /* First, try to find the symbol in the given namespace, and all
750 containing namespaces. */
751 sym = lookup_namespace_scope (langdef, name, block, domain, scope, 0);
753 /* Search for name in namespaces imported to this and parent blocks. */
754 if (sym.symbol == NULL)
755 sym = cp_lookup_symbol_via_all_imports (scope, name, block, domain);
757 symbol_lookup_debug_printf ("cp_lookup_symbol_nonlocal (...) = %s",
758 (sym.symbol != NULL
759 ? host_address_to_string (sym.symbol)
760 : "NULL"));
761 return sym;
764 /* Search through the base classes of PARENT_TYPE for a base class
765 named NAME and return its type. If not found, return NULL. */
767 struct type *
768 cp_find_type_baseclass_by_name (struct type *parent_type, const char *name)
770 int i;
772 parent_type = check_typedef (parent_type);
773 for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i)
775 struct type *type = check_typedef (TYPE_BASECLASS (parent_type, i));
776 const char *tdef_name = TYPE_BASECLASS_NAME (parent_type, i);
777 const char *base_name = type->name ();
779 if (base_name == NULL)
780 continue;
782 if (streq (tdef_name, name) || streq (base_name, name))
783 return type;
785 type = cp_find_type_baseclass_by_name (type, name);
786 if (type != NULL)
787 return type;
790 return NULL;
793 /* Search through the base classes of PARENT_TYPE for a symbol named
794 NAME in block BLOCK. */
796 static struct block_symbol
797 find_symbol_in_baseclass (struct type *parent_type, const char *name,
798 const struct block *block,
799 const domain_search_flags domain,
800 int is_in_anonymous)
802 int i;
803 struct block_symbol sym = {};
805 for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i)
807 struct type *base_type = TYPE_BASECLASS (parent_type, i);
808 const char *base_name = TYPE_BASECLASS_NAME (parent_type, i);
810 if (base_name == NULL)
811 continue;
813 std::string concatenated_name = std::string (base_name) + "::" + name;
815 sym = cp_lookup_nested_symbol_1 (base_type, name,
816 concatenated_name.c_str (),
817 block, domain, 1, is_in_anonymous);
818 if (sym.symbol != NULL)
819 break;
822 return sym;
825 /* Helper function to look up NESTED_NAME in CONTAINER_TYPE and in DOMAIN
826 and within the context of BLOCK.
827 NESTED_NAME may have scope ("::").
828 CONTAINER_TYPE needn't have been "check_typedef'd" yet.
829 CONCATENATED_NAME is the fully scoped spelling of NESTED_NAME, it is
830 passed as an argument so that callers can control how space for it is
831 allocated.
832 If BASIC_LOOKUP is non-zero then perform a basic lookup of
833 CONCATENATED_NAME. See cp_basic_lookup_symbol for details.
834 If IS_IN_ANONYMOUS is non-zero then CONCATENATED_NAME is in an anonymous
835 namespace. */
837 static struct block_symbol
838 cp_lookup_nested_symbol_1 (struct type *container_type,
839 const char *nested_name,
840 const char *concatenated_name,
841 const struct block *block,
842 const domain_search_flags domain,
843 int basic_lookup, int is_in_anonymous)
845 struct block_symbol sym;
847 /* NOTE: carlton/2003-11-10: We don't treat C++ class members
848 of classes like, say, data or function members. Instead,
849 they're just represented by symbols whose names are
850 qualified by the name of the surrounding class. This is
851 just like members of namespaces; in particular,
852 cp_basic_lookup_symbol works when looking them up. */
854 if (basic_lookup)
856 sym = cp_basic_lookup_symbol (concatenated_name, block, domain,
857 is_in_anonymous);
858 if (sym.symbol != NULL)
859 return sym;
862 /* Now search all static file-level symbols. We have to do this for things
863 like typedefs in the class. We do not try to guess any imported
864 namespace as even the fully specified namespace search is already not
865 C++ compliant and more assumptions could make it too magic. */
867 /* First search in this symtab, what we want is possibly there. */
868 sym = lookup_symbol_in_static_block (concatenated_name, block, domain);
869 if (sym.symbol != NULL)
870 return sym;
872 /* Nope. We now have to search all static blocks in all objfiles,
873 even if block != NULL, because there's no guarantees as to which
874 symtab the symbol we want is in. Except for symbols defined in
875 anonymous namespaces should be treated as local to a single file,
876 which we just searched. */
877 if (!is_in_anonymous)
879 sym = lookup_static_symbol (concatenated_name, domain);
880 if (sym.symbol != NULL)
881 return sym;
884 /* If this is a class with baseclasses, search them next. */
885 container_type = check_typedef (container_type);
886 if (TYPE_N_BASECLASSES (container_type) > 0)
888 sym = find_symbol_in_baseclass (container_type, nested_name, block,
889 domain, is_in_anonymous);
890 if (sym.symbol != NULL)
891 return sym;
894 return {};
897 /* Look up a symbol named NESTED_NAME that is nested inside the C++
898 class or namespace given by PARENT_TYPE, from within the context
899 given by BLOCK, and in DOMAIN.
900 Return NULL if there is no such nested symbol. */
902 struct block_symbol
903 cp_lookup_nested_symbol (struct type *parent_type,
904 const char *nested_name,
905 const struct block *block,
906 const domain_search_flags domain)
908 /* type_name_or_error provides better error reporting using the
909 original type. */
910 struct type *saved_parent_type = parent_type;
912 parent_type = check_typedef (parent_type);
914 if (symbol_lookup_debug)
916 const char *type_name = saved_parent_type->name ();
918 symbol_lookup_debug_printf ("cp_lookup_nested_symbol (%s, %s, %s, %s)",
919 type_name != NULL ? type_name : "unnamed",
920 nested_name, host_address_to_string (block),
921 domain_name (domain).c_str ());
924 switch (parent_type->code ())
926 case TYPE_CODE_STRUCT:
927 case TYPE_CODE_NAMESPACE:
928 case TYPE_CODE_UNION:
929 case TYPE_CODE_ENUM:
930 /* NOTE: Handle modules here as well, because Fortran is re-using the C++
931 specific code to lookup nested symbols in modules, by calling the
932 method lookup_symbol_nonlocal, which ends up here. */
933 case TYPE_CODE_MODULE:
935 int size;
936 const char *parent_name = type_name_or_error (saved_parent_type);
937 struct block_symbol sym;
938 char *concatenated_name;
939 int is_in_anonymous;
941 size = strlen (parent_name) + 2 + strlen (nested_name) + 1;
942 concatenated_name = (char *) alloca (size);
943 xsnprintf (concatenated_name, size, "%s::%s",
944 parent_name, nested_name);
945 is_in_anonymous = cp_is_in_anonymous (concatenated_name);
947 sym = cp_lookup_nested_symbol_1 (parent_type, nested_name,
948 concatenated_name, block, domain,
949 1, is_in_anonymous);
951 symbol_lookup_debug_printf ("cp_lookup_nested_symbol (...) = %s",
952 (sym.symbol != NULL
953 ? host_address_to_string (sym.symbol)
954 : "NULL"));
955 return sym;
958 case TYPE_CODE_FUNC:
959 case TYPE_CODE_METHOD:
960 symbol_lookup_debug_printf
961 ("cp_lookup_nested_symbol (...) = NULL (func/method)");
962 return {};
964 default:
965 internal_error (_("cp_lookup_nested_symbol called "
966 "on a non-aggregate type."));
970 /* The C++-version of lookup_transparent_type. */
972 /* FIXME: carlton/2004-01-16: The problem that this is trying to
973 address is that, unfortunately, sometimes NAME is wrong: it may not
974 include the name of namespaces enclosing the type in question.
975 lookup_transparent_type gets called when the type in question
976 is a declaration, and we're trying to find its definition; but, for
977 declarations, our type name deduction mechanism doesn't work.
978 There's nothing we can do to fix this in general, I think, in the
979 absence of debug information about namespaces (I've filed PR
980 gdb/1511 about this); until such debug information becomes more
981 prevalent, one heuristic which sometimes looks is to search for the
982 definition in namespaces containing the current namespace.
984 We should delete this functions once the appropriate debug
985 information becomes more widespread. (GCC 3.4 will be the first
986 released version of GCC with such information.) */
988 struct type *
989 cp_lookup_transparent_type (const char *name, domain_search_flags flags)
991 /* First, try the honest way of looking up the definition. */
992 struct type *t = basic_lookup_transparent_type (name, flags);
993 const char *scope;
995 if (t != NULL)
996 return t;
998 /* If that doesn't work and we're within a namespace, look there
999 instead. */
1000 const block *block = get_selected_block (0);
1001 if (block == nullptr)
1002 return nullptr;
1004 scope = block->scope ();
1006 if (scope[0] == '\0')
1007 return NULL;
1009 return cp_lookup_transparent_type_loop (name, scope, 0);
1012 /* Lookup the type definition associated to NAME in namespaces/classes
1013 containing SCOPE whose name is strictly longer than LENGTH. LENGTH
1014 must be the index of the start of a component of SCOPE. */
1016 static struct type *
1017 cp_lookup_transparent_type_loop (const char *name,
1018 const char *scope,
1019 int length)
1021 int scope_length = length + cp_find_first_component (scope + length);
1022 char *full_name;
1024 /* If the current scope is followed by "::", look in the next
1025 component. */
1026 if (scope[scope_length] == ':')
1028 struct type *retval
1029 = cp_lookup_transparent_type_loop (name, scope,
1030 scope_length + 2);
1032 if (retval != NULL)
1033 return retval;
1036 full_name = (char *) alloca (scope_length + 2 + strlen (name) + 1);
1037 strncpy (full_name, scope, scope_length);
1038 memcpy (full_name + scope_length, "::", 2);
1039 strcpy (full_name + scope_length + 2, name);
1041 return basic_lookup_transparent_type (full_name);
1044 /* This used to do something but was removed when it became
1045 obsolete. */
1047 static void
1048 maintenance_cplus_namespace (const char *args, int from_tty)
1050 gdb_printf (_("The `maint namespace' command was removed.\n"));
1053 void _initialize_cp_namespace ();
1054 void
1055 _initialize_cp_namespace ()
1057 struct cmd_list_element *cmd;
1059 cmd = add_cmd ("namespace", class_maintenance,
1060 maintenance_cplus_namespace,
1061 _("Deprecated placeholder for removed functionality."),
1062 &maint_cplus_cmd_list);
1063 deprecate_cmd (cmd, NULL);