Add generated source files and fix thinko in aarch64-asm.c
[binutils-gdb.git] / gdb / cp-namespace.c
bloba573b1410d3a724d5f55fc3a0e16180755ef05fa
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 "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 "inferior.h"
36 #include <map>
37 #include <string>
38 #include <string.h>
40 static struct block_symbol
41 cp_lookup_nested_symbol_1 (struct type *container_type,
42 const char *nested_name,
43 const char *concatenated_name,
44 const struct block *block,
45 const domain_enum domain,
46 int basic_lookup, int is_in_anonymous);
48 static struct type *cp_lookup_transparent_type_loop (const char *name,
49 const char *scope,
50 int scope_len);
52 /* Check to see if SYMBOL refers to an object contained within an
53 anonymous namespace; if so, add an appropriate using directive. */
55 void
56 cp_scan_for_anonymous_namespaces (struct buildsym_compunit *compunit,
57 const struct symbol *const symbol,
58 struct objfile *const objfile)
60 if (symbol->demangled_name () != NULL)
62 const char *name = symbol->demangled_name ();
63 unsigned int previous_component;
64 unsigned int next_component;
66 /* Start with a quick-and-dirty check for mention of "(anonymous
67 namespace)". */
69 if (!cp_is_in_anonymous (name))
70 return;
72 previous_component = 0;
73 next_component = cp_find_first_component (name + previous_component);
75 while (name[next_component] == ':')
77 if (((next_component - previous_component)
78 == CP_ANONYMOUS_NAMESPACE_LEN)
79 && strncmp (name + previous_component,
80 CP_ANONYMOUS_NAMESPACE_STR,
81 CP_ANONYMOUS_NAMESPACE_LEN) == 0)
83 int dest_len = (previous_component == 0
84 ? 0 : previous_component - 2);
85 int src_len = next_component;
87 char *dest = (char *) alloca (dest_len + 1);
88 char *src = (char *) alloca (src_len + 1);
90 memcpy (dest, name, dest_len);
91 memcpy (src, name, src_len);
93 dest[dest_len] = '\0';
94 src[src_len] = '\0';
96 /* We've found a component of the name that's an
97 anonymous namespace. So add symbols in it to the
98 namespace given by the previous component if there is
99 one, or to the global namespace if there isn't.
100 The declared line of this using directive can be set
101 to 0, this way it is always considered valid. */
102 std::vector<const char *> excludes;
103 add_using_directive (compunit->get_local_using_directives (),
104 dest, src, NULL, NULL, excludes, 0,
105 1, &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_enum 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_enum 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 != NULL && domain == VAR_DOMAIN)
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_enum 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, so always look in
272 VAR_DOMAIN. This works for classes too because of
273 symbol_matches_domain (which should be replaced with something
274 else, but it's what we have today). */
275 block_symbol scope_sym = lookup_symbol_in_static_block (scope.c_str (),
276 block, VAR_DOMAIN);
277 if (scope_sym.symbol == NULL)
278 scope_sym = lookup_global_symbol (scope.c_str (), block, VAR_DOMAIN);
279 if (scope_sym.symbol == NULL)
280 return {};
282 struct type *scope_type = scope_sym.symbol->type ();
284 /* If the scope is a function/method, then look up NESTED as a local
285 static variable. E.g., "print 'function()::static_var'". */
286 if ((scope_type->code () == TYPE_CODE_FUNC
287 || scope_type->code () == TYPE_CODE_METHOD)
288 && domain == VAR_DOMAIN)
289 return lookup_symbol (nested, scope_sym.symbol->value_block (),
290 VAR_DOMAIN, NULL);
292 /* Look for a symbol named NESTED in this class/namespace.
293 The caller is assumed to have already have done a basic lookup of NAME.
294 So we pass zero for BASIC_LOOKUP to cp_lookup_nested_symbol_1 here. */
295 return cp_lookup_nested_symbol_1 (scope_type, nested, name,
296 block, domain, 0, is_in_anonymous);
299 /* Look up NAME in the C++ namespace NAMESPACE. Other arguments are
300 as in cp_lookup_symbol_nonlocal. If SEARCH is non-zero, search
301 through base classes for a matching symbol.
303 Note: Part of the complexity is because NAME may itself specify scope.
304 Part of the complexity is also because this handles the case where
305 there is no scoping in which case we also try looking in the class of
306 "this" if we can compute it. */
308 static struct block_symbol
309 cp_lookup_symbol_in_namespace (const char *the_namespace, const char *name,
310 const struct block *block,
311 const domain_enum domain, int search)
313 char *concatenated_name = NULL;
314 int is_in_anonymous;
315 unsigned int prefix_len;
316 struct block_symbol sym;
318 if (the_namespace[0] != '\0')
320 concatenated_name
321 = (char *) alloca (strlen (the_namespace) + 2 + strlen (name) + 1);
322 strcpy (concatenated_name, the_namespace);
323 strcat (concatenated_name, "::");
324 strcat (concatenated_name, name);
325 name = concatenated_name;
328 prefix_len = cp_entire_prefix_len (name);
329 if (prefix_len == 0)
330 return cp_lookup_bare_symbol (NULL, name, block, domain, search);
332 /* This would be simpler if we just called cp_lookup_nested_symbol
333 at this point. But that would require first looking up the containing
334 class/namespace. Since we're only searching static and global blocks
335 there's often no need to first do that lookup. */
337 is_in_anonymous
338 = the_namespace[0] != '\0' && cp_is_in_anonymous (the_namespace);
339 sym = cp_basic_lookup_symbol (name, block, domain, is_in_anonymous);
340 if (sym.symbol != NULL)
341 return sym;
343 if (search)
344 sym = cp_search_static_and_baseclasses (name, block, domain, prefix_len,
345 is_in_anonymous);
347 return sym;
350 /* This version of the function is internal, use the wrapper unless
351 the list of ambiguous symbols is needed.
353 Search for NAME by applying all import statements belonging to
354 BLOCK which are applicable in SCOPE. If DECLARATION_ONLY the
355 search is restricted to using declarations.
356 Example:
358 namespace A {
359 int x;
361 using A::x;
363 If SEARCH_PARENTS the search will include imports which are
364 applicable in parents of SCOPE.
365 Example:
367 namespace A {
368 using namespace X;
369 namespace B {
370 using namespace Y;
374 If SCOPE is "A::B" and SEARCH_PARENTS is true the imports of
375 namespaces X and Y will be considered. If SEARCH_PARENTS is false
376 only the import of Y is considered.
378 SEARCH_SCOPE_FIRST is an internal implementation detail: Callers must
379 pass 0 for it. Internally we pass 1 when recursing. */
381 static void
382 cp_lookup_symbol_via_imports (const char *scope,
383 const char *name,
384 const struct block *block,
385 const domain_enum domain,
386 const int search_scope_first,
387 const int declaration_only,
388 const int search_parents,
389 std::map<std::string,
390 struct block_symbol>& found_symbols)
392 struct using_direct *current;
393 struct block_symbol sym = {};
394 int len;
395 int directive_match;
397 /* All the symbols we found will be kept in this relational map between
398 the mangled name and the block_symbol found. We do this so that GDB
399 won't incorrectly report an ambiguous symbol for finding the same
400 thing twice. */
402 /* First, try to find the symbol in the given namespace if requested. */
403 if (search_scope_first)
405 sym = cp_lookup_symbol_in_namespace (scope, name,
406 block, domain, 1);
407 if (sym.symbol != nullptr)
408 found_symbols[sym.symbol->m_name] = sym;
411 /* Due to a GCC bug, we need to know the boundaries of the current block
412 to know if a certain using directive is valid. */
413 symtab_and_line boundary_sal = find_pc_line (block->end () - 1, 0);
415 /* Go through the using directives. If any of them add new names to
416 the namespace we're searching in, see if we can find a match by
417 applying them. */
418 for (current = block->get_using ();
419 current != NULL;
420 current = current->next)
422 const char **excludep;
424 /* If the using directive was below the place we are stopped at,
425 do not use this directive. */
426 if (!current->valid_line (boundary_sal.line))
427 continue;
428 len = strlen (current->import_dest);
429 directive_match = (search_parents
430 ? (startswith (scope, current->import_dest)
431 && (len == 0
432 || scope[len] == ':'
433 || scope[len] == '\0'))
434 : strcmp (scope, current->import_dest) == 0);
436 /* If the import destination is the current scope or one of its
437 ancestors then it is applicable. */
438 if (directive_match && !current->searched)
440 /* Mark this import as searched so that the recursive call
441 does not search it again. */
442 scoped_restore reset_directive_searched
443 = make_scoped_restore (&current->searched, 1);
445 /* If there is an import of a single declaration, compare the
446 imported declaration (after optional renaming by its alias)
447 with the sought out name. If there is a match pass
448 current->import_src as NAMESPACE to direct the search
449 towards the imported namespace. */
450 if (current->declaration
451 && strcmp (name, current->alias
452 ? current->alias : current->declaration) == 0)
453 sym = cp_lookup_symbol_in_namespace (current->import_src,
454 current->declaration,
455 block, domain, 1);
457 /* If this is a DECLARATION_ONLY search or a symbol was found
458 or this import statement was an import declaration, the
459 search of this import is complete. */
460 if (declaration_only || sym.symbol != NULL || current->declaration)
462 if (sym.symbol != NULL)
463 found_symbols[sym.symbol->m_name] = sym;
465 continue;
468 /* Do not follow CURRENT if NAME matches its EXCLUDES. */
469 for (excludep = current->excludes; *excludep; excludep++)
470 if (strcmp (name, *excludep) == 0)
471 break;
472 if (*excludep)
473 continue;
475 if (current->alias != NULL
476 && strcmp (name, current->alias) == 0)
477 /* If the import is creating an alias and the alias matches
478 the sought name. Pass current->import_src as the NAME to
479 direct the search towards the aliased namespace. */
481 sym = cp_lookup_symbol_in_namespace (scope,
482 current->import_src,
483 block, domain, 1);
484 found_symbols[sym.symbol->m_name] = sym;
486 else if (current->alias == NULL)
488 /* If this import statement creates no alias, pass
489 current->inner as NAMESPACE to direct the search
490 towards the imported namespace. */
491 cp_lookup_symbol_via_imports (current->import_src, name,
492 block, domain, 1, 0, 0,
493 found_symbols);
500 /* Wrapper for the actual cp_lookup_symbol_via_imports. This wrapper sets
501 search_scope_first correctly and handles errors if needed. */
502 static struct block_symbol
503 cp_lookup_symbol_via_imports (const char *scope,
504 const char *name,
505 const struct block *block,
506 const domain_enum domain,
507 const int declaration_only,
508 const int search_parents)
510 std::map<std::string, struct block_symbol> found_symbols;
512 cp_lookup_symbol_via_imports(scope, name, block, domain, 0,
513 declaration_only, search_parents,
514 found_symbols);
516 if (found_symbols.size () > 1)
518 auto itr = found_symbols.cbegin ();
519 std::string error_str = "Reference to \"";
520 error_str += name;
521 error_str += "\" is ambiguous, possibilities are: ";
522 error_str += itr->second.symbol->print_name ();
523 for (itr++; itr != found_symbols.end (); itr++)
525 error_str += " and ";
526 error_str += itr->second.symbol->print_name ();
528 error (_("%s"), error_str.c_str ());
531 if (found_symbols.size() == 1)
532 return found_symbols.cbegin ()->second;
533 else
534 return {};
537 /* Helper function that searches an array of symbols for one named NAME. */
539 static struct symbol *
540 search_symbol_list (const char *name, int num,
541 struct symbol **syms)
543 int i;
545 /* Maybe we should store a dictionary in here instead. */
546 for (i = 0; i < num; ++i)
548 if (strcmp (name, syms[i]->natural_name ()) == 0)
549 return syms[i];
551 return NULL;
554 /* Search for symbols whose name match NAME in the given SCOPE.
555 if BLOCK is a function, we'll search first through the template
556 parameters and function type. Afterwards (or if BLOCK is not a function)
557 search through imported directives using cp_lookup_symbol_via_imports. */
559 struct block_symbol
560 cp_lookup_symbol_imports_or_template (const char *scope,
561 const char *name,
562 const struct block *block,
563 const domain_enum domain)
565 struct symbol *function = block->function ();
567 symbol_lookup_debug_printf
568 ("cp_lookup_symbol_imports_or_template (%s, %s, %s, %s)",
569 scope, name, host_address_to_string (block), domain_name (domain));
571 if (function != NULL && function->language () == language_cplus)
573 /* Search the function's template parameters. */
574 if (function->is_cplus_template_function ())
576 struct template_symbol *templ
577 = (struct template_symbol *) function;
578 struct symbol *sym = search_symbol_list (name,
579 templ->n_template_arguments,
580 templ->template_arguments);
582 if (sym != NULL)
584 symbol_lookup_debug_printf
585 ("cp_lookup_symbol_imports_or_template (...) = %s",
586 host_address_to_string (sym));
587 return (struct block_symbol) {sym, block};
591 /* Search the template parameters of the function's defining
592 context. */
593 if (function->natural_name ())
595 struct type *context;
596 std::string name_copy (function->natural_name ());
597 const struct language_defn *lang = language_def (language_cplus);
598 const struct block *parent = block->superblock ();
599 struct symbol *sym;
601 while (1)
603 unsigned int prefix_len
604 = cp_entire_prefix_len (name_copy.c_str ());
606 if (prefix_len == 0)
607 context = NULL;
608 else
610 name_copy.erase (prefix_len);
611 context = lookup_typename (lang,
612 name_copy.c_str (),
613 parent, 1);
616 if (context == NULL)
617 break;
620 = search_symbol_list (name,
621 TYPE_N_TEMPLATE_ARGUMENTS (context),
622 TYPE_TEMPLATE_ARGUMENTS (context));
623 if (sym != NULL)
625 symbol_lookup_debug_printf
626 ("cp_lookup_symbol_imports_or_template (...) = %s",
627 host_address_to_string (sym));
628 return (struct block_symbol) {sym, parent};
634 struct block_symbol result
635 = cp_lookup_symbol_via_imports (scope, name, block, domain, 1, 1);
636 symbol_lookup_debug_printf ("cp_lookup_symbol_imports_or_template (...) = %s\n",
637 result.symbol != nullptr
638 ? host_address_to_string (result.symbol) : "NULL");
639 return result;
642 /* Search for NAME by applying relevant import statements belonging to BLOCK
643 and its parents. SCOPE is the namespace scope of the context in which the
644 search is being evaluated. */
646 static struct block_symbol
647 cp_lookup_symbol_via_all_imports (const char *scope, const char *name,
648 const struct block *block,
649 const domain_enum domain)
651 struct block_symbol sym;
653 while (block != NULL)
655 sym = cp_lookup_symbol_via_imports (scope, name, block, domain, 0, 1);
656 if (sym.symbol != nullptr)
657 return sym;
659 block = block->superblock ();
662 return {};
665 /* Searches for NAME in the current namespace, and by applying
666 relevant import statements belonging to BLOCK and its parents.
667 SCOPE is the namespace scope of the context in which the search is
668 being evaluated. */
670 struct block_symbol
671 cp_lookup_symbol_namespace (const char *scope,
672 const char *name,
673 const struct block *block,
674 const domain_enum domain)
676 struct block_symbol sym;
678 symbol_lookup_debug_printf ("cp_lookup_symbol_namespace (%s, %s, %s, %s)",
679 scope, name, host_address_to_string (block),
680 domain_name (domain));
682 /* First, try to find the symbol in the given namespace. */
683 sym = cp_lookup_symbol_in_namespace (scope, name, block, domain, 1);
685 /* Search for name in namespaces imported to this and parent blocks. */
686 if (sym.symbol == NULL)
687 sym = cp_lookup_symbol_via_all_imports (scope, name, block, domain);
689 symbol_lookup_debug_printf ("cp_lookup_symbol_namespace (...) = %s",
690 sym.symbol != NULL
691 ? host_address_to_string (sym.symbol) : "NULL");
692 return sym;
695 /* Lookup NAME at namespace scope (or, in C terms, in static and
696 global variables). SCOPE is the namespace that the current
697 function is defined within; only consider namespaces whose length
698 is at least SCOPE_LEN. Other arguments are as in
699 cp_lookup_symbol_nonlocal.
701 For example, if we're within a function A::B::f and looking for a
702 symbol x, this will get called with NAME = "x", SCOPE = "A::B", and
703 SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same,
704 but with SCOPE_LEN = 1. And then it calls itself with NAME and
705 SCOPE the same, but with SCOPE_LEN = 4. This third call looks for
706 "A::B::x"; if it doesn't find it, then the second call looks for
707 "A::x", and if that call fails, then the first call looks for
708 "x". */
710 static struct block_symbol
711 lookup_namespace_scope (const struct language_defn *langdef,
712 const char *name,
713 const struct block *block,
714 const domain_enum domain,
715 const char *scope,
716 int scope_len)
718 char *the_namespace;
720 if (scope[scope_len] != '\0')
722 /* Recursively search for names in child namespaces first. */
724 struct block_symbol sym;
725 int new_scope_len = scope_len;
727 /* If the current scope is followed by "::", skip past that. */
728 if (new_scope_len != 0)
730 gdb_assert (scope[new_scope_len] == ':');
731 new_scope_len += 2;
733 new_scope_len += cp_find_first_component (scope + new_scope_len);
734 sym = lookup_namespace_scope (langdef, name, block, domain,
735 scope, new_scope_len);
736 if (sym.symbol != NULL)
737 return sym;
740 /* Okay, we didn't find a match in our children, so look for the
741 name in the current namespace.
743 If we there is no scope and we know we have a bare symbol, then short
744 circuit everything and call cp_lookup_bare_symbol directly.
745 This isn't an optimization, rather it allows us to pass LANGDEF which
746 is needed for primitive type lookup. The test doesn't have to be
747 perfect: if NAME is a bare symbol that our test doesn't catch (e.g., a
748 template symbol with "::" in the argument list) then
749 cp_lookup_symbol_in_namespace will catch it. */
751 if (scope_len == 0 && strchr (name, ':') == NULL)
752 return cp_lookup_bare_symbol (langdef, name, block, domain, 1);
754 the_namespace = (char *) alloca (scope_len + 1);
755 strncpy (the_namespace, scope, scope_len);
756 the_namespace[scope_len] = '\0';
757 return cp_lookup_symbol_in_namespace (the_namespace, name,
758 block, domain, 1);
761 /* The C++-specific version of name lookup for static and global
762 names. This makes sure that names get looked for in all namespaces
763 that are in scope. NAME is the natural name of the symbol that
764 we're looking for, BLOCK is the block that we're searching within,
765 DOMAIN says what kind of symbols we're looking for. */
767 struct block_symbol
768 cp_lookup_symbol_nonlocal (const struct language_defn *langdef,
769 const char *name,
770 const struct block *block,
771 const domain_enum domain)
773 struct block_symbol sym;
774 const char *scope = block == nullptr ? "" : block->scope ();
776 symbol_lookup_debug_printf
777 ("cp_lookup_symbol_non_local (%s, %s (scope %s), %s)",
778 name, host_address_to_string (block), scope, domain_name (domain));
780 /* First, try to find the symbol in the given namespace, and all
781 containing namespaces. */
782 sym = lookup_namespace_scope (langdef, name, block, domain, scope, 0);
784 /* Search for name in namespaces imported to this and parent blocks. */
785 if (sym.symbol == NULL)
786 sym = cp_lookup_symbol_via_all_imports (scope, name, block, domain);
788 symbol_lookup_debug_printf ("cp_lookup_symbol_nonlocal (...) = %s",
789 (sym.symbol != NULL
790 ? host_address_to_string (sym.symbol)
791 : "NULL"));
792 return sym;
795 /* Search through the base classes of PARENT_TYPE for a base class
796 named NAME and return its type. If not found, return NULL. */
798 struct type *
799 cp_find_type_baseclass_by_name (struct type *parent_type, const char *name)
801 int i;
803 parent_type = check_typedef (parent_type);
804 for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i)
806 struct type *type = check_typedef (TYPE_BASECLASS (parent_type, i));
807 const char *tdef_name = TYPE_BASECLASS_NAME (parent_type, i);
808 const char *base_name = type->name ();
810 if (base_name == NULL)
811 continue;
813 if (streq (tdef_name, name) || streq (base_name, name))
814 return type;
816 type = cp_find_type_baseclass_by_name (type, name);
817 if (type != NULL)
818 return type;
821 return NULL;
824 /* Search through the base classes of PARENT_TYPE for a symbol named
825 NAME in block BLOCK. */
827 static struct block_symbol
828 find_symbol_in_baseclass (struct type *parent_type, const char *name,
829 const struct block *block, const domain_enum domain,
830 int is_in_anonymous)
832 int i;
833 struct block_symbol sym = {};
835 for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i)
837 struct type *base_type = TYPE_BASECLASS (parent_type, i);
838 const char *base_name = TYPE_BASECLASS_NAME (parent_type, i);
840 if (base_name == NULL)
841 continue;
843 std::string concatenated_name = std::string (base_name) + "::" + name;
845 sym = cp_lookup_nested_symbol_1 (base_type, name,
846 concatenated_name.c_str (),
847 block, domain, 1, is_in_anonymous);
848 if (sym.symbol != NULL)
849 break;
852 return sym;
855 /* Helper function to look up NESTED_NAME in CONTAINER_TYPE and in DOMAIN
856 and within the context of BLOCK.
857 NESTED_NAME may have scope ("::").
858 CONTAINER_TYPE needn't have been "check_typedef'd" yet.
859 CONCATENATED_NAME is the fully scoped spelling of NESTED_NAME, it is
860 passed as an argument so that callers can control how space for it is
861 allocated.
862 If BASIC_LOOKUP is non-zero then perform a basic lookup of
863 CONCATENATED_NAME. See cp_basic_lookup_symbol for details.
864 If IS_IN_ANONYMOUS is non-zero then CONCATENATED_NAME is in an anonymous
865 namespace. */
867 static struct block_symbol
868 cp_lookup_nested_symbol_1 (struct type *container_type,
869 const char *nested_name,
870 const char *concatenated_name,
871 const struct block *block,
872 const domain_enum domain,
873 int basic_lookup, int is_in_anonymous)
875 struct block_symbol sym;
877 /* NOTE: carlton/2003-11-10: We don't treat C++ class members
878 of classes like, say, data or function members. Instead,
879 they're just represented by symbols whose names are
880 qualified by the name of the surrounding class. This is
881 just like members of namespaces; in particular,
882 cp_basic_lookup_symbol works when looking them up. */
884 if (basic_lookup)
886 sym = cp_basic_lookup_symbol (concatenated_name, block, domain,
887 is_in_anonymous);
888 if (sym.symbol != NULL)
889 return sym;
892 /* Now search all static file-level symbols. We have to do this for things
893 like typedefs in the class. We do not try to guess any imported
894 namespace as even the fully specified namespace search is already not
895 C++ compliant and more assumptions could make it too magic. */
897 /* First search in this symtab, what we want is possibly there. */
898 sym = lookup_symbol_in_static_block (concatenated_name, block, domain);
899 if (sym.symbol != NULL)
900 return sym;
902 /* Nope. We now have to search all static blocks in all objfiles,
903 even if block != NULL, because there's no guarantees as to which
904 symtab the symbol we want is in. Except for symbols defined in
905 anonymous namespaces should be treated as local to a single file,
906 which we just searched. */
907 if (!is_in_anonymous)
909 sym = lookup_static_symbol (concatenated_name, domain);
910 if (sym.symbol != NULL)
911 return sym;
914 /* If this is a class with baseclasses, search them next. */
915 container_type = check_typedef (container_type);
916 if (TYPE_N_BASECLASSES (container_type) > 0)
918 sym = find_symbol_in_baseclass (container_type, nested_name, block,
919 domain, is_in_anonymous);
920 if (sym.symbol != NULL)
921 return sym;
924 return {};
927 /* Look up a symbol named NESTED_NAME that is nested inside the C++
928 class or namespace given by PARENT_TYPE, from within the context
929 given by BLOCK, and in DOMAIN.
930 Return NULL if there is no such nested symbol. */
932 struct block_symbol
933 cp_lookup_nested_symbol (struct type *parent_type,
934 const char *nested_name,
935 const struct block *block,
936 const domain_enum domain)
938 /* type_name_or_error provides better error reporting using the
939 original type. */
940 struct type *saved_parent_type = parent_type;
942 parent_type = check_typedef (parent_type);
944 if (symbol_lookup_debug)
946 const char *type_name = saved_parent_type->name ();
948 symbol_lookup_debug_printf ("cp_lookup_nested_symbol (%s, %s, %s, %s)",
949 type_name != NULL ? type_name : "unnamed",
950 nested_name, host_address_to_string (block),
951 domain_name (domain));
954 switch (parent_type->code ())
956 case TYPE_CODE_STRUCT:
957 case TYPE_CODE_NAMESPACE:
958 case TYPE_CODE_UNION:
959 case TYPE_CODE_ENUM:
960 /* NOTE: Handle modules here as well, because Fortran is re-using the C++
961 specific code to lookup nested symbols in modules, by calling the
962 method lookup_symbol_nonlocal, which ends up here. */
963 case TYPE_CODE_MODULE:
965 int size;
966 const char *parent_name = type_name_or_error (saved_parent_type);
967 struct block_symbol sym;
968 char *concatenated_name;
969 int is_in_anonymous;
971 size = strlen (parent_name) + 2 + strlen (nested_name) + 1;
972 concatenated_name = (char *) alloca (size);
973 xsnprintf (concatenated_name, size, "%s::%s",
974 parent_name, nested_name);
975 is_in_anonymous = cp_is_in_anonymous (concatenated_name);
977 sym = cp_lookup_nested_symbol_1 (parent_type, nested_name,
978 concatenated_name, block, domain,
979 1, is_in_anonymous);
981 symbol_lookup_debug_printf ("cp_lookup_nested_symbol (...) = %s",
982 (sym.symbol != NULL
983 ? host_address_to_string (sym.symbol)
984 : "NULL"));
985 return sym;
988 case TYPE_CODE_FUNC:
989 case TYPE_CODE_METHOD:
990 symbol_lookup_debug_printf
991 ("cp_lookup_nested_symbol (...) = NULL (func/method)");
992 return {};
994 default:
995 internal_error (_("cp_lookup_nested_symbol called "
996 "on a non-aggregate type."));
1000 /* The C++-version of lookup_transparent_type. */
1002 /* FIXME: carlton/2004-01-16: The problem that this is trying to
1003 address is that, unfortunately, sometimes NAME is wrong: it may not
1004 include the name of namespaces enclosing the type in question.
1005 lookup_transparent_type gets called when the type in question
1006 is a declaration, and we're trying to find its definition; but, for
1007 declarations, our type name deduction mechanism doesn't work.
1008 There's nothing we can do to fix this in general, I think, in the
1009 absence of debug information about namespaces (I've filed PR
1010 gdb/1511 about this); until such debug information becomes more
1011 prevalent, one heuristic which sometimes looks is to search for the
1012 definition in namespaces containing the current namespace.
1014 We should delete this functions once the appropriate debug
1015 information becomes more widespread. (GCC 3.4 will be the first
1016 released version of GCC with such information.) */
1018 struct type *
1019 cp_lookup_transparent_type (const char *name)
1021 /* First, try the honest way of looking up the definition. */
1022 struct type *t = basic_lookup_transparent_type (name);
1023 const char *scope;
1025 if (t != NULL)
1026 return t;
1028 /* If that doesn't work and we're within a namespace, look there
1029 instead. */
1030 const block *block = get_selected_block (0);
1031 if (block == nullptr)
1032 return nullptr;
1034 scope = block->scope ();
1036 if (scope[0] == '\0')
1037 return NULL;
1039 return cp_lookup_transparent_type_loop (name, scope, 0);
1042 /* Lookup the type definition associated to NAME in namespaces/classes
1043 containing SCOPE whose name is strictly longer than LENGTH. LENGTH
1044 must be the index of the start of a component of SCOPE. */
1046 static struct type *
1047 cp_lookup_transparent_type_loop (const char *name,
1048 const char *scope,
1049 int length)
1051 int scope_length = length + cp_find_first_component (scope + length);
1052 char *full_name;
1054 /* If the current scope is followed by "::", look in the next
1055 component. */
1056 if (scope[scope_length] == ':')
1058 struct type *retval
1059 = cp_lookup_transparent_type_loop (name, scope,
1060 scope_length + 2);
1062 if (retval != NULL)
1063 return retval;
1066 full_name = (char *) alloca (scope_length + 2 + strlen (name) + 1);
1067 strncpy (full_name, scope, scope_length);
1068 memcpy (full_name + scope_length, "::", 2);
1069 strcpy (full_name + scope_length + 2, name);
1071 return basic_lookup_transparent_type (full_name);
1074 /* This used to do something but was removed when it became
1075 obsolete. */
1077 static void
1078 maintenance_cplus_namespace (const char *args, int from_tty)
1080 gdb_printf (_("The `maint namespace' command was removed.\n"));
1083 void _initialize_cp_namespace ();
1084 void
1085 _initialize_cp_namespace ()
1087 struct cmd_list_element *cmd;
1089 cmd = add_cmd ("namespace", class_maintenance,
1090 maintenance_cplus_namespace,
1091 _("Deprecated placeholder for removed functionality."),
1092 &maint_cplus_cmd_list);
1093 deprecate_cmd (cmd, NULL);