MIPS: Use 64-bit a ABI by default for `mipsisa64*-*-linux*' targets
[binutils-gdb.git] / gdb / cp-namespace.c
blobd4a47eaac1f4f7b71ec0f9a7e0ad43d30d88cdf4
1 /* Helper routines for C++ support in GDB.
2 Copyright (C) 2003-2023 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 "defs.h"
22 #include "cp-support.h"
23 #include "gdbsupport/gdb_obstack.h"
24 #include "symtab.h"
25 #include "symfile.h"
26 #include "block.h"
27 #include "objfiles.h"
28 #include "gdbtypes.h"
29 #include "dictionary.h"
30 #include "command.h"
31 #include "frame.h"
32 #include "buildsym.h"
33 #include "language.h"
34 #include "namespace.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_enum 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 dest, src, NULL, NULL, excludes, 0,
104 1, &objfile->objfile_obstack);
106 /* The "+ 2" is for the "::". */
107 previous_component = next_component + 2;
108 next_component = (previous_component
109 + cp_find_first_component (name
110 + previous_component));
115 /* Test whether or not NAMESPACE looks like it mentions an anonymous
116 namespace; return nonzero if so. */
119 cp_is_in_anonymous (const char *symbol_name)
121 return (strstr (symbol_name, CP_ANONYMOUS_NAMESPACE_STR)
122 != NULL);
125 /* Look up NAME in DOMAIN in BLOCK's static block and in global blocks.
126 If IS_IN_ANONYMOUS is nonzero, the symbol in question is located
127 within an anonymous namespace. */
129 static struct block_symbol
130 cp_basic_lookup_symbol (const char *name, const struct block *block,
131 const domain_enum domain, int is_in_anonymous)
133 struct block_symbol sym;
135 sym = lookup_symbol_in_static_block (name, block, domain);
136 if (sym.symbol != NULL)
137 return sym;
139 if (is_in_anonymous)
141 /* Symbols defined in anonymous namespaces have external linkage
142 but should be treated as local to a single file nonetheless.
143 So we only search the current file's global block. */
145 const struct block *global_block = block->global_block ();
147 if (global_block != NULL)
149 sym.symbol = lookup_symbol_in_block (name,
150 symbol_name_match_type::FULL,
151 global_block, domain);
152 sym.block = global_block;
155 else
156 sym = lookup_global_symbol (name, block, domain);
158 return sym;
161 /* Search bare symbol NAME in DOMAIN in BLOCK.
162 NAME is guaranteed to not have any scope (no "::") in its name, though
163 if for example NAME is a template spec then "::" may appear in the
164 argument list.
165 If LANGDEF is non-NULL then try to lookup NAME as a primitive type in
166 that language. Normally we wouldn't need LANGDEF but fortran also uses
167 this code.
168 If SEARCH is non-zero then see if we can determine "this" from BLOCK, and
169 if so then also search for NAME in that class. */
171 static struct block_symbol
172 cp_lookup_bare_symbol (const struct language_defn *langdef,
173 const char *name, const struct block *block,
174 const domain_enum domain, int search)
176 struct block_symbol sym;
178 /* Note: We can't do a simple assert for ':' not being in NAME because
179 ':' may be in the args of a template spec. This isn't intended to be
180 a complete test, just cheap and documentary. */
181 gdb_assert (strpbrk ("<>()", name) != nullptr
182 || strstr (name, "::") == nullptr);
184 sym = lookup_symbol_in_static_block (name, block, domain);
185 if (sym.symbol != NULL)
186 return sym;
188 /* If we didn't find a definition for a builtin type in the static block,
189 search for it now. This is actually the right thing to do and can be
190 a massive performance win. E.g., when debugging a program with lots of
191 shared libraries we could search all of them only to find out the
192 builtin type isn't defined in any of them. This is common for types
193 like "void". */
194 if (langdef != NULL && domain == VAR_DOMAIN)
196 struct gdbarch *gdbarch;
198 if (block == NULL)
199 gdbarch = target_gdbarch ();
200 else
201 gdbarch = block->gdbarch ();
202 sym.symbol
203 = language_lookup_primitive_type_as_symbol (langdef, gdbarch, name);
204 sym.block = NULL;
205 if (sym.symbol != NULL)
206 return sym;
209 sym = lookup_global_symbol (name, block, domain);
210 if (sym.symbol != NULL)
211 return sym;
213 if (search)
215 struct block_symbol lang_this;
216 struct type *type;
218 lang_this.symbol = NULL;
220 if (langdef != NULL)
221 lang_this = lookup_language_this (langdef, block);
223 if (lang_this.symbol == NULL)
224 return {};
227 type = check_typedef (lang_this.symbol->type ()->target_type ());
228 /* If TYPE_NAME is NULL, abandon trying to find this symbol.
229 This can happen for lambda functions compiled with clang++,
230 which outputs no name for the container class. */
231 if (type->name () == NULL)
232 return {};
234 /* Look for symbol NAME in this class. */
235 sym = cp_lookup_nested_symbol (type, name, block, domain);
238 return sym;
241 /* Search NAME in DOMAIN in all static blocks, and then in all baseclasses.
242 BLOCK specifies the context in which to perform the search.
243 NAME is guaranteed to have scope (contain "::") and PREFIX_LEN specifies
244 the length of the entire scope of NAME (up to, but not including, the last
245 "::".
247 Note: At least in the case of Fortran, which also uses this code, there
248 may be no text after the last "::". */
250 static struct block_symbol
251 cp_search_static_and_baseclasses (const char *name,
252 const struct block *block,
253 const domain_enum domain,
254 unsigned int prefix_len,
255 int is_in_anonymous)
257 /* Check for malformed input. */
258 if (prefix_len + 2 > strlen (name) || name[prefix_len + 1] != ':')
259 return {};
261 /* The class, namespace or function name is everything up to and
262 including PREFIX_LEN. */
263 std::string scope (name, prefix_len);
265 /* The rest of the name is everything else past the initial scope
266 operator. */
267 const char *nested = name + prefix_len + 2;
269 /* Lookup the scope symbol. If none is found, there is nothing more
270 that can be done. SCOPE could be a namespace, so always look in
271 VAR_DOMAIN. This works for classes too because of
272 symbol_matches_domain (which should be replaced with something
273 else, but it's what we have today). */
274 block_symbol scope_sym = lookup_symbol_in_static_block (scope.c_str (),
275 block, VAR_DOMAIN);
276 if (scope_sym.symbol == NULL)
277 scope_sym = lookup_global_symbol (scope.c_str (), block, VAR_DOMAIN);
278 if (scope_sym.symbol == NULL)
279 return {};
281 struct type *scope_type = scope_sym.symbol->type ();
283 /* If the scope is a function/method, then look up NESTED as a local
284 static variable. E.g., "print 'function()::static_var'". */
285 if ((scope_type->code () == TYPE_CODE_FUNC
286 || scope_type->code () == TYPE_CODE_METHOD)
287 && domain == VAR_DOMAIN)
288 return lookup_symbol (nested, scope_sym.symbol->value_block (),
289 VAR_DOMAIN, NULL);
291 /* Look for a symbol named NESTED in this class/namespace.
292 The caller is assumed to have already have done a basic lookup of NAME.
293 So we pass zero for BASIC_LOOKUP to cp_lookup_nested_symbol_1 here. */
294 return cp_lookup_nested_symbol_1 (scope_type, nested, name,
295 block, domain, 0, is_in_anonymous);
298 /* Look up NAME in the C++ namespace NAMESPACE. Other arguments are
299 as in cp_lookup_symbol_nonlocal. If SEARCH is non-zero, search
300 through base classes for a matching symbol.
302 Note: Part of the complexity is because NAME may itself specify scope.
303 Part of the complexity is also because this handles the case where
304 there is no scoping in which case we also try looking in the class of
305 "this" if we can compute it. */
307 static struct block_symbol
308 cp_lookup_symbol_in_namespace (const char *the_namespace, const char *name,
309 const struct block *block,
310 const domain_enum domain, int search)
312 char *concatenated_name = NULL;
313 int is_in_anonymous;
314 unsigned int prefix_len;
315 struct block_symbol sym;
317 if (the_namespace[0] != '\0')
319 concatenated_name
320 = (char *) alloca (strlen (the_namespace) + 2 + strlen (name) + 1);
321 strcpy (concatenated_name, the_namespace);
322 strcat (concatenated_name, "::");
323 strcat (concatenated_name, name);
324 name = concatenated_name;
327 prefix_len = cp_entire_prefix_len (name);
328 if (prefix_len == 0)
329 return cp_lookup_bare_symbol (NULL, name, block, domain, search);
331 /* This would be simpler if we just called cp_lookup_nested_symbol
332 at this point. But that would require first looking up the containing
333 class/namespace. Since we're only searching static and global blocks
334 there's often no need to first do that lookup. */
336 is_in_anonymous
337 = the_namespace[0] != '\0' && cp_is_in_anonymous (the_namespace);
338 sym = cp_basic_lookup_symbol (name, block, domain, is_in_anonymous);
339 if (sym.symbol != NULL)
340 return sym;
342 if (search)
343 sym = cp_search_static_and_baseclasses (name, block, domain, prefix_len,
344 is_in_anonymous);
346 return sym;
349 /* This version of the function is internal, use the wrapper unless
350 the list of ambiguous symbols is needed.
352 Search for NAME by applying all import statements belonging to
353 BLOCK which are applicable in SCOPE. If DECLARATION_ONLY the
354 search is restricted to using declarations.
355 Example:
357 namespace A {
358 int x;
360 using A::x;
362 If SEARCH_PARENTS the search will include imports which are
363 applicable in parents of SCOPE.
364 Example:
366 namespace A {
367 using namespace X;
368 namespace B {
369 using namespace Y;
373 If SCOPE is "A::B" and SEARCH_PARENTS is true the imports of
374 namespaces X and Y will be considered. If SEARCH_PARENTS is false
375 only the import of Y is considered.
377 SEARCH_SCOPE_FIRST is an internal implementation detail: Callers must
378 pass 0 for it. Internally we pass 1 when recursing. */
380 static void
381 cp_lookup_symbol_via_imports (const char *scope,
382 const char *name,
383 const struct block *block,
384 const domain_enum domain,
385 const int search_scope_first,
386 const int declaration_only,
387 const int search_parents,
388 std::map<std::string,
389 struct block_symbol>& found_symbols)
391 struct using_direct *current;
392 struct block_symbol sym = {};
393 int len;
394 int directive_match;
396 /* All the symbols we found will be kept in this relational map between
397 the mangled name and the block_symbol found. We do this so that GDB
398 won't incorrectly report an ambiguous symbol for finding the same
399 thing twice. */
401 /* First, try to find the symbol in the given namespace if requested. */
402 if (search_scope_first)
404 sym = cp_lookup_symbol_in_namespace (scope, name,
405 block, domain, 1);
406 if (sym.symbol != nullptr)
407 found_symbols[sym.symbol->m_name] = sym;
410 /* Due to a GCC bug, we need to know the boundaries of the current block
411 to know if a certain using directive is valid. */
412 symtab_and_line boundary_sal = find_pc_line (block->end () - 1, 0);
414 /* Go through the using directives. If any of them add new names to
415 the namespace we're searching in, see if we can find a match by
416 applying them. */
417 for (current = block->get_using ();
418 current != NULL;
419 current = current->next)
421 const char **excludep;
423 /* If the using directive was below the place we are stopped at,
424 do not use this directive. */
425 if (!current->valid_line (boundary_sal.line))
426 continue;
427 len = strlen (current->import_dest);
428 directive_match = (search_parents
429 ? (startswith (scope, current->import_dest)
430 && (len == 0
431 || scope[len] == ':'
432 || scope[len] == '\0'))
433 : strcmp (scope, current->import_dest) == 0);
435 /* If the import destination is the current scope or one of its
436 ancestors then it is applicable. */
437 if (directive_match && !current->searched)
439 /* Mark this import as searched so that the recursive call
440 does not search it again. */
441 scoped_restore reset_directive_searched
442 = make_scoped_restore (&current->searched, 1);
444 /* If there is an import of a single declaration, compare the
445 imported declaration (after optional renaming by its alias)
446 with the sought out name. If there is a match pass
447 current->import_src as NAMESPACE to direct the search
448 towards the imported namespace. */
449 if (current->declaration
450 && strcmp (name, current->alias
451 ? current->alias : current->declaration) == 0)
452 sym = cp_lookup_symbol_in_namespace (current->import_src,
453 current->declaration,
454 block, domain, 1);
456 /* If this is a DECLARATION_ONLY search or a symbol was found
457 or this import statement was an import declaration, the
458 search of this import is complete. */
459 if (declaration_only || sym.symbol != NULL || current->declaration)
461 if (sym.symbol != NULL)
462 found_symbols[sym.symbol->m_name] = sym;
464 continue;
467 /* Do not follow CURRENT if NAME matches its EXCLUDES. */
468 for (excludep = current->excludes; *excludep; excludep++)
469 if (strcmp (name, *excludep) == 0)
470 break;
471 if (*excludep)
472 continue;
474 if (current->alias != NULL
475 && strcmp (name, current->alias) == 0)
476 /* If the import is creating an alias and the alias matches
477 the sought name. Pass current->import_src as the NAME to
478 direct the search towards the aliased namespace. */
480 sym = cp_lookup_symbol_in_namespace (scope,
481 current->import_src,
482 block, domain, 1);
483 found_symbols[sym.symbol->m_name] = sym;
485 else if (current->alias == NULL)
487 /* If this import statement creates no alias, pass
488 current->inner as NAMESPACE to direct the search
489 towards the imported namespace. */
490 cp_lookup_symbol_via_imports (current->import_src, name,
491 block, domain, 1, 0, 0,
492 found_symbols);
499 /* Wrapper for the actual cp_lookup_symbol_via_imports. This wrapper sets
500 search_scope_first correctly and handles errors if needed. */
501 static struct block_symbol
502 cp_lookup_symbol_via_imports (const char *scope,
503 const char *name,
504 const struct block *block,
505 const domain_enum domain,
506 const int declaration_only,
507 const int search_parents)
509 std::map<std::string, struct block_symbol> found_symbols;
511 cp_lookup_symbol_via_imports(scope, name, block, domain, 0,
512 declaration_only, search_parents,
513 found_symbols);
515 if (found_symbols.size () > 1)
517 auto itr = found_symbols.cbegin ();
518 std::string error_str = "Reference to \"";
519 error_str += name;
520 error_str += "\" is ambiguous, possibilities are: ";
521 error_str += itr->second.symbol->print_name ();
522 for (itr++; itr != found_symbols.end (); itr++)
524 error_str += " and ";
525 error_str += itr->second.symbol->print_name ();
527 error (_("%s"), error_str.c_str ());
530 if (found_symbols.size() == 1)
531 return found_symbols.cbegin ()->second;
532 else
533 return {};
536 /* Helper function that searches an array of symbols for one named NAME. */
538 static struct symbol *
539 search_symbol_list (const char *name, int num,
540 struct symbol **syms)
542 int i;
544 /* Maybe we should store a dictionary in here instead. */
545 for (i = 0; i < num; ++i)
547 if (strcmp (name, syms[i]->natural_name ()) == 0)
548 return syms[i];
550 return NULL;
553 /* Search for symbols whose name match NAME in the given SCOPE.
554 if BLOCK is a function, we'll search first through the template
555 parameters and function type. Afterwards (or if BLOCK is not a function)
556 search through imported directives using cp_lookup_symbol_via_imports. */
558 struct block_symbol
559 cp_lookup_symbol_imports_or_template (const char *scope,
560 const char *name,
561 const struct block *block,
562 const domain_enum domain)
564 struct symbol *function = block->function ();
566 symbol_lookup_debug_printf
567 ("cp_lookup_symbol_imports_or_template (%s, %s, %s, %s)",
568 scope, name, host_address_to_string (block), domain_name (domain));
570 if (function != NULL && function->language () == language_cplus)
572 /* Search the function's template parameters. */
573 if (function->is_cplus_template_function ())
575 struct template_symbol *templ
576 = (struct template_symbol *) function;
577 struct symbol *sym = search_symbol_list (name,
578 templ->n_template_arguments,
579 templ->template_arguments);
581 if (sym != NULL)
583 symbol_lookup_debug_printf
584 ("cp_lookup_symbol_imports_or_template (...) = %s",
585 host_address_to_string (sym));
586 return (struct block_symbol) {sym, block};
590 /* Search the template parameters of the function's defining
591 context. */
592 if (function->natural_name ())
594 struct type *context;
595 std::string name_copy (function->natural_name ());
596 const struct language_defn *lang = language_def (language_cplus);
597 const struct block *parent = block->superblock ();
598 struct symbol *sym;
600 while (1)
602 unsigned int prefix_len
603 = cp_entire_prefix_len (name_copy.c_str ());
605 if (prefix_len == 0)
606 context = NULL;
607 else
609 name_copy.erase (prefix_len);
610 context = lookup_typename (lang,
611 name_copy.c_str (),
612 parent, 1);
615 if (context == NULL)
616 break;
619 = search_symbol_list (name,
620 TYPE_N_TEMPLATE_ARGUMENTS (context),
621 TYPE_TEMPLATE_ARGUMENTS (context));
622 if (sym != NULL)
624 symbol_lookup_debug_printf
625 ("cp_lookup_symbol_imports_or_template (...) = %s",
626 host_address_to_string (sym));
627 return (struct block_symbol) {sym, parent};
633 struct block_symbol result
634 = cp_lookup_symbol_via_imports (scope, name, block, domain, 1, 1);
635 symbol_lookup_debug_printf ("cp_lookup_symbol_imports_or_template (...) = %s\n",
636 result.symbol != nullptr
637 ? host_address_to_string (result.symbol) : "NULL");
638 return result;
641 /* Search for NAME by applying relevant import statements belonging to BLOCK
642 and its parents. SCOPE is the namespace scope of the context in which the
643 search is being evaluated. */
645 static struct block_symbol
646 cp_lookup_symbol_via_all_imports (const char *scope, const char *name,
647 const struct block *block,
648 const domain_enum domain)
650 struct block_symbol sym;
652 while (block != NULL)
654 sym = cp_lookup_symbol_via_imports (scope, name, block, domain, 0, 1);
655 if (sym.symbol != nullptr)
656 return sym;
658 block = block->superblock ();
661 return {};
664 /* Searches for NAME in the current namespace, and by applying
665 relevant import statements belonging to BLOCK and its parents.
666 SCOPE is the namespace scope of the context in which the search is
667 being evaluated. */
669 struct block_symbol
670 cp_lookup_symbol_namespace (const char *scope,
671 const char *name,
672 const struct block *block,
673 const domain_enum domain)
675 struct block_symbol sym;
677 symbol_lookup_debug_printf ("cp_lookup_symbol_namespace (%s, %s, %s, %s)",
678 scope, name, host_address_to_string (block),
679 domain_name (domain));
681 /* First, try to find the symbol in the given namespace. */
682 sym = cp_lookup_symbol_in_namespace (scope, name, block, domain, 1);
684 /* Search for name in namespaces imported to this and parent blocks. */
685 if (sym.symbol == NULL)
686 sym = cp_lookup_symbol_via_all_imports (scope, name, block, domain);
688 symbol_lookup_debug_printf ("cp_lookup_symbol_namespace (...) = %s",
689 sym.symbol != NULL
690 ? host_address_to_string (sym.symbol) : "NULL");
691 return sym;
694 /* Lookup NAME at namespace scope (or, in C terms, in static and
695 global variables). SCOPE is the namespace that the current
696 function is defined within; only consider namespaces whose length
697 is at least SCOPE_LEN. Other arguments are as in
698 cp_lookup_symbol_nonlocal.
700 For example, if we're within a function A::B::f and looking for a
701 symbol x, this will get called with NAME = "x", SCOPE = "A::B", and
702 SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same,
703 but with SCOPE_LEN = 1. And then it calls itself with NAME and
704 SCOPE the same, but with SCOPE_LEN = 4. This third call looks for
705 "A::B::x"; if it doesn't find it, then the second call looks for
706 "A::x", and if that call fails, then the first call looks for
707 "x". */
709 static struct block_symbol
710 lookup_namespace_scope (const struct language_defn *langdef,
711 const char *name,
712 const struct block *block,
713 const domain_enum domain,
714 const char *scope,
715 int scope_len)
717 char *the_namespace;
719 if (scope[scope_len] != '\0')
721 /* Recursively search for names in child namespaces first. */
723 struct block_symbol sym;
724 int new_scope_len = scope_len;
726 /* If the current scope is followed by "::", skip past that. */
727 if (new_scope_len != 0)
729 gdb_assert (scope[new_scope_len] == ':');
730 new_scope_len += 2;
732 new_scope_len += cp_find_first_component (scope + new_scope_len);
733 sym = lookup_namespace_scope (langdef, name, block, domain,
734 scope, new_scope_len);
735 if (sym.symbol != NULL)
736 return sym;
739 /* Okay, we didn't find a match in our children, so look for the
740 name in the current namespace.
742 If we there is no scope and we know we have a bare symbol, then short
743 circuit everything and call cp_lookup_bare_symbol directly.
744 This isn't an optimization, rather it allows us to pass LANGDEF which
745 is needed for primitive type lookup. The test doesn't have to be
746 perfect: if NAME is a bare symbol that our test doesn't catch (e.g., a
747 template symbol with "::" in the argument list) then
748 cp_lookup_symbol_in_namespace will catch it. */
750 if (scope_len == 0 && strchr (name, ':') == NULL)
751 return cp_lookup_bare_symbol (langdef, name, block, domain, 1);
753 the_namespace = (char *) alloca (scope_len + 1);
754 strncpy (the_namespace, scope, scope_len);
755 the_namespace[scope_len] = '\0';
756 return cp_lookup_symbol_in_namespace (the_namespace, name,
757 block, domain, 1);
760 /* The C++-specific version of name lookup for static and global
761 names. This makes sure that names get looked for in all namespaces
762 that are in scope. NAME is the natural name of the symbol that
763 we're looking for, BLOCK is the block that we're searching within,
764 DOMAIN says what kind of symbols we're looking for. */
766 struct block_symbol
767 cp_lookup_symbol_nonlocal (const struct language_defn *langdef,
768 const char *name,
769 const struct block *block,
770 const domain_enum domain)
772 struct block_symbol sym;
773 const char *scope = block == nullptr ? "" : block->scope ();
775 symbol_lookup_debug_printf
776 ("cp_lookup_symbol_non_local (%s, %s (scope %s), %s)",
777 name, host_address_to_string (block), scope, domain_name (domain));
779 /* First, try to find the symbol in the given namespace, and all
780 containing namespaces. */
781 sym = lookup_namespace_scope (langdef, name, block, domain, scope, 0);
783 /* Search for name in namespaces imported to this and parent blocks. */
784 if (sym.symbol == NULL)
785 sym = cp_lookup_symbol_via_all_imports (scope, name, block, domain);
787 symbol_lookup_debug_printf ("cp_lookup_symbol_nonlocal (...) = %s",
788 (sym.symbol != NULL
789 ? host_address_to_string (sym.symbol)
790 : "NULL"));
791 return sym;
794 /* Search through the base classes of PARENT_TYPE for a base class
795 named NAME and return its type. If not found, return NULL. */
797 struct type *
798 cp_find_type_baseclass_by_name (struct type *parent_type, const char *name)
800 int i;
802 parent_type = check_typedef (parent_type);
803 for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i)
805 struct type *type = check_typedef (TYPE_BASECLASS (parent_type, i));
806 const char *tdef_name = TYPE_BASECLASS_NAME (parent_type, i);
807 const char *base_name = type->name ();
809 if (base_name == NULL)
810 continue;
812 if (streq (tdef_name, name) || streq (base_name, name))
813 return type;
815 type = cp_find_type_baseclass_by_name (type, name);
816 if (type != NULL)
817 return type;
820 return NULL;
823 /* Search through the base classes of PARENT_TYPE for a symbol named
824 NAME in block BLOCK. */
826 static struct block_symbol
827 find_symbol_in_baseclass (struct type *parent_type, const char *name,
828 const struct block *block, const domain_enum domain,
829 int is_in_anonymous)
831 int i;
832 struct block_symbol sym = {};
834 for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i)
836 struct type *base_type = TYPE_BASECLASS (parent_type, i);
837 const char *base_name = TYPE_BASECLASS_NAME (parent_type, i);
839 if (base_name == NULL)
840 continue;
842 std::string concatenated_name = std::string (base_name) + "::" + name;
844 sym = cp_lookup_nested_symbol_1 (base_type, name,
845 concatenated_name.c_str (),
846 block, domain, 1, is_in_anonymous);
847 if (sym.symbol != NULL)
848 break;
851 return sym;
854 /* Helper function to look up NESTED_NAME in CONTAINER_TYPE and in DOMAIN
855 and within the context of BLOCK.
856 NESTED_NAME may have scope ("::").
857 CONTAINER_TYPE needn't have been "check_typedef'd" yet.
858 CONCATENATED_NAME is the fully scoped spelling of NESTED_NAME, it is
859 passed as an argument so that callers can control how space for it is
860 allocated.
861 If BASIC_LOOKUP is non-zero then perform a basic lookup of
862 CONCATENATED_NAME. See cp_basic_lookup_symbol for details.
863 If IS_IN_ANONYMOUS is non-zero then CONCATENATED_NAME is in an anonymous
864 namespace. */
866 static struct block_symbol
867 cp_lookup_nested_symbol_1 (struct type *container_type,
868 const char *nested_name,
869 const char *concatenated_name,
870 const struct block *block,
871 const domain_enum domain,
872 int basic_lookup, int is_in_anonymous)
874 struct block_symbol sym;
876 /* NOTE: carlton/2003-11-10: We don't treat C++ class members
877 of classes like, say, data or function members. Instead,
878 they're just represented by symbols whose names are
879 qualified by the name of the surrounding class. This is
880 just like members of namespaces; in particular,
881 cp_basic_lookup_symbol works when looking them up. */
883 if (basic_lookup)
885 sym = cp_basic_lookup_symbol (concatenated_name, block, domain,
886 is_in_anonymous);
887 if (sym.symbol != NULL)
888 return sym;
891 /* Now search all static file-level symbols. We have to do this for things
892 like typedefs in the class. We do not try to guess any imported
893 namespace as even the fully specified namespace search is already not
894 C++ compliant and more assumptions could make it too magic. */
896 /* First search in this symtab, what we want is possibly there. */
897 sym = lookup_symbol_in_static_block (concatenated_name, block, domain);
898 if (sym.symbol != NULL)
899 return sym;
901 /* Nope. We now have to search all static blocks in all objfiles,
902 even if block != NULL, because there's no guarantees as to which
903 symtab the symbol we want is in. Except for symbols defined in
904 anonymous namespaces should be treated as local to a single file,
905 which we just searched. */
906 if (!is_in_anonymous)
908 sym = lookup_static_symbol (concatenated_name, domain);
909 if (sym.symbol != NULL)
910 return sym;
913 /* If this is a class with baseclasses, search them next. */
914 container_type = check_typedef (container_type);
915 if (TYPE_N_BASECLASSES (container_type) > 0)
917 sym = find_symbol_in_baseclass (container_type, nested_name, block,
918 domain, is_in_anonymous);
919 if (sym.symbol != NULL)
920 return sym;
923 return {};
926 /* Look up a symbol named NESTED_NAME that is nested inside the C++
927 class or namespace given by PARENT_TYPE, from within the context
928 given by BLOCK, and in DOMAIN.
929 Return NULL if there is no such nested symbol. */
931 struct block_symbol
932 cp_lookup_nested_symbol (struct type *parent_type,
933 const char *nested_name,
934 const struct block *block,
935 const domain_enum domain)
937 /* type_name_or_error provides better error reporting using the
938 original type. */
939 struct type *saved_parent_type = parent_type;
941 parent_type = check_typedef (parent_type);
943 if (symbol_lookup_debug)
945 const char *type_name = saved_parent_type->name ();
947 symbol_lookup_debug_printf ("cp_lookup_nested_symbol (%s, %s, %s, %s)",
948 type_name != NULL ? type_name : "unnamed",
949 nested_name, host_address_to_string (block),
950 domain_name (domain));
953 switch (parent_type->code ())
955 case TYPE_CODE_STRUCT:
956 case TYPE_CODE_NAMESPACE:
957 case TYPE_CODE_UNION:
958 case TYPE_CODE_ENUM:
959 /* NOTE: Handle modules here as well, because Fortran is re-using the C++
960 specific code to lookup nested symbols in modules, by calling the
961 method lookup_symbol_nonlocal, which ends up here. */
962 case TYPE_CODE_MODULE:
964 int size;
965 const char *parent_name = type_name_or_error (saved_parent_type);
966 struct block_symbol sym;
967 char *concatenated_name;
968 int is_in_anonymous;
970 size = strlen (parent_name) + 2 + strlen (nested_name) + 1;
971 concatenated_name = (char *) alloca (size);
972 xsnprintf (concatenated_name, size, "%s::%s",
973 parent_name, nested_name);
974 is_in_anonymous = cp_is_in_anonymous (concatenated_name);
976 sym = cp_lookup_nested_symbol_1 (parent_type, nested_name,
977 concatenated_name, block, domain,
978 1, is_in_anonymous);
980 symbol_lookup_debug_printf ("cp_lookup_nested_symbol (...) = %s",
981 (sym.symbol != NULL
982 ? host_address_to_string (sym.symbol)
983 : "NULL"));
984 return sym;
987 case TYPE_CODE_FUNC:
988 case TYPE_CODE_METHOD:
989 symbol_lookup_debug_printf
990 ("cp_lookup_nested_symbol (...) = NULL (func/method)");
991 return {};
993 default:
994 internal_error (_("cp_lookup_nested_symbol called "
995 "on a non-aggregate type."));
999 /* The C++-version of lookup_transparent_type. */
1001 /* FIXME: carlton/2004-01-16: The problem that this is trying to
1002 address is that, unfortunately, sometimes NAME is wrong: it may not
1003 include the name of namespaces enclosing the type in question.
1004 lookup_transparent_type gets called when the type in question
1005 is a declaration, and we're trying to find its definition; but, for
1006 declarations, our type name deduction mechanism doesn't work.
1007 There's nothing we can do to fix this in general, I think, in the
1008 absence of debug information about namespaces (I've filed PR
1009 gdb/1511 about this); until such debug information becomes more
1010 prevalent, one heuristic which sometimes looks is to search for the
1011 definition in namespaces containing the current namespace.
1013 We should delete this functions once the appropriate debug
1014 information becomes more widespread. (GCC 3.4 will be the first
1015 released version of GCC with such information.) */
1017 struct type *
1018 cp_lookup_transparent_type (const char *name)
1020 /* First, try the honest way of looking up the definition. */
1021 struct type *t = basic_lookup_transparent_type (name);
1022 const char *scope;
1024 if (t != NULL)
1025 return t;
1027 /* If that doesn't work and we're within a namespace, look there
1028 instead. */
1029 scope = get_selected_block (0)->scope ();
1031 if (scope[0] == '\0')
1032 return NULL;
1034 return cp_lookup_transparent_type_loop (name, scope, 0);
1037 /* Lookup the type definition associated to NAME in namespaces/classes
1038 containing SCOPE whose name is strictly longer than LENGTH. LENGTH
1039 must be the index of the start of a component of SCOPE. */
1041 static struct type *
1042 cp_lookup_transparent_type_loop (const char *name,
1043 const char *scope,
1044 int length)
1046 int scope_length = length + cp_find_first_component (scope + length);
1047 char *full_name;
1049 /* If the current scope is followed by "::", look in the next
1050 component. */
1051 if (scope[scope_length] == ':')
1053 struct type *retval
1054 = cp_lookup_transparent_type_loop (name, scope,
1055 scope_length + 2);
1057 if (retval != NULL)
1058 return retval;
1061 full_name = (char *) alloca (scope_length + 2 + strlen (name) + 1);
1062 strncpy (full_name, scope, scope_length);
1063 memcpy (full_name + scope_length, "::", 2);
1064 strcpy (full_name + scope_length + 2, name);
1066 return basic_lookup_transparent_type (full_name);
1069 /* This used to do something but was removed when it became
1070 obsolete. */
1072 static void
1073 maintenance_cplus_namespace (const char *args, int from_tty)
1075 gdb_printf (_("The `maint namespace' command was removed.\n"));
1078 void _initialize_cp_namespace ();
1079 void
1080 _initialize_cp_namespace ()
1082 struct cmd_list_element *cmd;
1084 cmd = add_cmd ("namespace", class_maintenance,
1085 maintenance_cplus_namespace,
1086 _("Deprecated placeholder for removed functionality."),
1087 &maint_cplus_cmd_list);
1088 deprecate_cmd (cmd, NULL);