Fix null pointer dereference in process_debug_info()
[binutils-gdb.git] / gdb / cp-namespace.c
blobec72d72b5a787426007a8e05cb72ca7286a3d95c
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 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_search_flags 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_search_flags 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 != nullptr && (domain & SEARCH_TYPE_DOMAIN) != 0)
196 struct gdbarch *gdbarch;
198 if (block == NULL)
199 gdbarch = current_inferior ()->arch ();
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_search_flags 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, a class, or even a
271 function. This code is also used by Fortran, so modules are
272 included in the search as well. */
273 block_symbol scope_sym
274 = lookup_symbol_in_static_block (scope.c_str (), block,
275 SEARCH_TYPE_DOMAIN
276 | SEARCH_FUNCTION_DOMAIN
277 | SEARCH_MODULE_DOMAIN);
278 if (scope_sym.symbol == NULL)
279 scope_sym = lookup_global_symbol (scope.c_str (), block,
280 SEARCH_TYPE_DOMAIN
281 | SEARCH_FUNCTION_DOMAIN
282 | SEARCH_MODULE_DOMAIN);
283 if (scope_sym.symbol == NULL)
284 return {};
286 struct type *scope_type = scope_sym.symbol->type ();
288 /* If the scope is a function/method, then look up NESTED as a local
289 static variable. E.g., "print 'function()::static_var'". */
290 if ((scope_type->code () == TYPE_CODE_FUNC
291 || scope_type->code () == TYPE_CODE_METHOD)
292 && (domain & SEARCH_VAR_DOMAIN) != 0)
293 return lookup_symbol (nested, scope_sym.symbol->value_block (),
294 domain, NULL);
296 /* Look for a symbol named NESTED in this class/namespace.
297 The caller is assumed to have already have done a basic lookup of NAME.
298 So we pass zero for BASIC_LOOKUP to cp_lookup_nested_symbol_1 here. */
299 return cp_lookup_nested_symbol_1 (scope_type, nested, name,
300 block, domain, 0, is_in_anonymous);
303 /* Look up NAME in the C++ namespace NAMESPACE. Other arguments are
304 as in cp_lookup_symbol_nonlocal. If SEARCH is non-zero, search
305 through base classes for a matching symbol.
307 Note: Part of the complexity is because NAME may itself specify scope.
308 Part of the complexity is also because this handles the case where
309 there is no scoping in which case we also try looking in the class of
310 "this" if we can compute it. */
312 static struct block_symbol
313 cp_lookup_symbol_in_namespace (const char *the_namespace, const char *name,
314 const struct block *block,
315 const domain_search_flags domain, int search)
317 char *concatenated_name = NULL;
318 int is_in_anonymous;
319 unsigned int prefix_len;
320 struct block_symbol sym;
322 if (the_namespace[0] != '\0')
324 concatenated_name
325 = (char *) alloca (strlen (the_namespace) + 2 + strlen (name) + 1);
326 strcpy (concatenated_name, the_namespace);
327 strcat (concatenated_name, "::");
328 strcat (concatenated_name, name);
329 name = concatenated_name;
332 prefix_len = cp_entire_prefix_len (name);
333 if (prefix_len == 0)
334 return cp_lookup_bare_symbol (NULL, name, block, domain, search);
336 /* This would be simpler if we just called cp_lookup_nested_symbol
337 at this point. But that would require first looking up the containing
338 class/namespace. Since we're only searching static and global blocks
339 there's often no need to first do that lookup. */
341 is_in_anonymous
342 = the_namespace[0] != '\0' && cp_is_in_anonymous (the_namespace);
343 sym = cp_basic_lookup_symbol (name, block, domain, is_in_anonymous);
344 if (sym.symbol != NULL)
345 return sym;
347 if (search)
348 sym = cp_search_static_and_baseclasses (name, block, domain, prefix_len,
349 is_in_anonymous);
351 return sym;
354 /* This version of the function is internal, use the wrapper unless
355 the list of ambiguous symbols is needed.
357 Search for NAME by applying all import statements belonging to
358 BLOCK which are applicable in SCOPE. If DECLARATION_ONLY the
359 search is restricted to using declarations.
360 Example:
362 namespace A {
363 int x;
365 using A::x;
367 If SEARCH_PARENTS the search will include imports which are
368 applicable in parents of SCOPE.
369 Example:
371 namespace A {
372 using namespace X;
373 namespace B {
374 using namespace Y;
378 If SCOPE is "A::B" and SEARCH_PARENTS is true the imports of
379 namespaces X and Y will be considered. If SEARCH_PARENTS is false
380 only the import of Y is considered.
382 SEARCH_SCOPE_FIRST is an internal implementation detail: Callers must
383 pass 0 for it. Internally we pass 1 when recursing. */
385 static void
386 cp_lookup_symbol_via_imports (const char *scope,
387 const char *name,
388 const struct block *block,
389 const domain_search_flags domain,
390 const int search_scope_first,
391 const int declaration_only,
392 const int search_parents,
393 std::map<std::string,
394 struct block_symbol>& found_symbols)
396 struct using_direct *current;
397 struct block_symbol sym = {};
398 int len;
399 int directive_match;
401 /* All the symbols we found will be kept in this relational map between
402 the mangled name and the block_symbol found. We do this so that GDB
403 won't incorrectly report an ambiguous symbol for finding the same
404 thing twice. */
406 /* First, try to find the symbol in the given namespace if requested. */
407 if (search_scope_first)
409 sym = cp_lookup_symbol_in_namespace (scope, name,
410 block, domain, 1);
411 if (sym.symbol != nullptr)
412 found_symbols[sym.symbol->m_name] = sym;
415 /* Due to a GCC bug, we need to know the boundaries of the current block
416 to know if a certain using directive is valid. */
417 symtab_and_line boundary_sal = find_pc_line (block->end () - 1, 0);
419 /* Go through the using directives. If any of them add new names to
420 the namespace we're searching in, see if we can find a match by
421 applying them. */
422 for (current = block->get_using ();
423 current != NULL;
424 current = current->next)
426 const char **excludep;
428 /* If the using directive was below the place we are stopped at,
429 do not use this directive. */
430 if (!current->valid_line (boundary_sal.line))
431 continue;
432 len = strlen (current->import_dest);
433 directive_match = (search_parents
434 ? (startswith (scope, current->import_dest)
435 && (len == 0
436 || scope[len] == ':'
437 || scope[len] == '\0'))
438 : strcmp (scope, current->import_dest) == 0);
440 /* If the import destination is the current scope or one of its
441 ancestors then it is applicable. */
442 if (directive_match && !current->searched)
444 /* Mark this import as searched so that the recursive call
445 does not search it again. */
446 scoped_restore reset_directive_searched
447 = make_scoped_restore (&current->searched, 1);
449 /* If there is an import of a single declaration, compare the
450 imported declaration (after optional renaming by its alias)
451 with the sought out name. If there is a match pass
452 current->import_src as NAMESPACE to direct the search
453 towards the imported namespace. */
454 if (current->declaration
455 && strcmp (name, current->alias
456 ? current->alias : current->declaration) == 0)
457 sym = cp_lookup_symbol_in_namespace (current->import_src,
458 current->declaration,
459 block, domain, 1);
461 /* If this is a DECLARATION_ONLY search or a symbol was found
462 or this import statement was an import declaration, the
463 search of this import is complete. */
464 if (declaration_only || sym.symbol != NULL || current->declaration)
466 if (sym.symbol != NULL)
467 found_symbols[sym.symbol->m_name] = sym;
469 continue;
472 /* Do not follow CURRENT if NAME matches its EXCLUDES. */
473 for (excludep = current->excludes; *excludep; excludep++)
474 if (strcmp (name, *excludep) == 0)
475 break;
476 if (*excludep)
477 continue;
479 if (current->alias != NULL
480 && strcmp (name, current->alias) == 0)
481 /* If the import is creating an alias and the alias matches
482 the sought name. Pass current->import_src as the NAME to
483 direct the search towards the aliased namespace. */
485 sym = cp_lookup_symbol_in_namespace (scope,
486 current->import_src,
487 block, domain, 1);
488 found_symbols[sym.symbol->m_name] = sym;
490 else if (current->alias == NULL)
492 /* If this import statement creates no alias, pass
493 current->inner as NAMESPACE to direct the search
494 towards the imported namespace. */
495 cp_lookup_symbol_via_imports (current->import_src, name,
496 block, domain, 1, 0, 0,
497 found_symbols);
504 /* Wrapper for the actual cp_lookup_symbol_via_imports. This wrapper sets
505 search_scope_first correctly and handles errors if needed. */
506 static struct block_symbol
507 cp_lookup_symbol_via_imports (const char *scope,
508 const char *name,
509 const struct block *block,
510 const domain_search_flags domain,
511 const int declaration_only,
512 const int search_parents)
514 std::map<std::string, struct block_symbol> found_symbols;
516 cp_lookup_symbol_via_imports(scope, name, block, domain, 0,
517 declaration_only, search_parents,
518 found_symbols);
520 if (found_symbols.size () > 1)
522 auto itr = found_symbols.cbegin ();
523 std::string error_str = "Reference to \"";
524 error_str += name;
525 error_str += "\" is ambiguous, possibilities are: ";
526 error_str += itr->second.symbol->print_name ();
527 for (itr++; itr != found_symbols.end (); itr++)
529 error_str += " and ";
530 error_str += itr->second.symbol->print_name ();
532 error (_("%s"), error_str.c_str ());
535 if (found_symbols.size() == 1)
536 return found_symbols.cbegin ()->second;
537 else
538 return {};
541 /* Helper function that searches an array of symbols for one named NAME. */
543 static struct symbol *
544 search_symbol_list (const char *name, int num,
545 struct symbol **syms)
547 int i;
549 /* Maybe we should store a dictionary in here instead. */
550 for (i = 0; i < num; ++i)
552 if (strcmp (name, syms[i]->natural_name ()) == 0)
553 return syms[i];
555 return NULL;
558 /* Search for symbols whose name match NAME in the given SCOPE.
559 if BLOCK is a function, we'll search first through the template
560 parameters and function type. Afterwards (or if BLOCK is not a function)
561 search through imported directives using cp_lookup_symbol_via_imports. */
563 struct block_symbol
564 cp_lookup_symbol_imports_or_template (const char *scope,
565 const char *name,
566 const struct block *block,
567 const domain_search_flags domain)
569 struct symbol *function = block->function ();
571 symbol_lookup_debug_printf
572 ("cp_lookup_symbol_imports_or_template (%s, %s, %s, %s)",
573 scope, name, host_address_to_string (block),
574 domain_name (domain).c_str ());
576 if (function != NULL && function->language () == language_cplus)
578 /* Search the function's template parameters. */
579 if (function->is_cplus_template_function ())
581 struct template_symbol *templ
582 = (struct template_symbol *) function;
583 struct symbol *sym = search_symbol_list (name,
584 templ->n_template_arguments,
585 templ->template_arguments);
587 if (sym != NULL)
589 symbol_lookup_debug_printf
590 ("cp_lookup_symbol_imports_or_template (...) = %s",
591 host_address_to_string (sym));
592 return (struct block_symbol) {sym, block};
596 /* Search the template parameters of the function's defining
597 context. */
598 if (function->natural_name ())
600 struct type *context;
601 std::string name_copy (function->natural_name ());
602 const struct language_defn *lang = language_def (language_cplus);
603 const struct block *parent = block->superblock ();
604 struct symbol *sym;
606 while (1)
608 unsigned int prefix_len
609 = cp_entire_prefix_len (name_copy.c_str ());
611 if (prefix_len == 0)
612 context = NULL;
613 else
615 name_copy.erase (prefix_len);
616 context = lookup_typename (lang,
617 name_copy.c_str (),
618 parent, 1);
621 if (context == NULL)
622 break;
625 = search_symbol_list (name,
626 TYPE_N_TEMPLATE_ARGUMENTS (context),
627 TYPE_TEMPLATE_ARGUMENTS (context));
628 if (sym != NULL)
630 symbol_lookup_debug_printf
631 ("cp_lookup_symbol_imports_or_template (...) = %s",
632 host_address_to_string (sym));
633 return (struct block_symbol) {sym, parent};
639 struct block_symbol result
640 = cp_lookup_symbol_via_imports (scope, name, block, domain, 1, 1);
641 symbol_lookup_debug_printf ("cp_lookup_symbol_imports_or_template (...) = %s\n",
642 result.symbol != nullptr
643 ? host_address_to_string (result.symbol) : "NULL");
644 return result;
647 /* Search for NAME by applying relevant import statements belonging to BLOCK
648 and its parents. SCOPE is the namespace scope of the context in which the
649 search is being evaluated. */
651 static struct block_symbol
652 cp_lookup_symbol_via_all_imports (const char *scope, const char *name,
653 const struct block *block,
654 const domain_search_flags domain)
656 struct block_symbol sym;
658 while (block != NULL)
660 sym = cp_lookup_symbol_via_imports (scope, name, block, domain, 0, 1);
661 if (sym.symbol != nullptr)
662 return sym;
664 block = block->superblock ();
667 return {};
670 /* Searches for NAME in the current namespace, and by applying
671 relevant import statements belonging to BLOCK and its parents.
672 SCOPE is the namespace scope of the context in which the search is
673 being evaluated. */
675 struct block_symbol
676 cp_lookup_symbol_namespace (const char *scope,
677 const char *name,
678 const struct block *block,
679 const domain_search_flags domain)
681 struct block_symbol sym;
683 symbol_lookup_debug_printf ("cp_lookup_symbol_namespace (%s, %s, %s, %s)",
684 scope, name, host_address_to_string (block),
685 domain_name (domain).c_str ());
687 /* First, try to find the symbol in the given namespace. */
688 sym = cp_lookup_symbol_in_namespace (scope, name, block, domain, 1);
690 /* Search for name in namespaces imported to this and parent blocks. */
691 if (sym.symbol == NULL)
692 sym = cp_lookup_symbol_via_all_imports (scope, name, block, domain);
694 symbol_lookup_debug_printf ("cp_lookup_symbol_namespace (...) = %s",
695 sym.symbol != NULL
696 ? host_address_to_string (sym.symbol) : "NULL");
697 return sym;
700 /* Lookup NAME at namespace scope (or, in C terms, in static and
701 global variables). SCOPE is the namespace that the current
702 function is defined within; only consider namespaces whose length
703 is at least SCOPE_LEN. Other arguments are as in
704 cp_lookup_symbol_nonlocal.
706 For example, if we're within a function A::B::f and looking for a
707 symbol x, this will get called with NAME = "x", SCOPE = "A::B", and
708 SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same,
709 but with SCOPE_LEN = 1. And then it calls itself with NAME and
710 SCOPE the same, but with SCOPE_LEN = 4. This third call looks for
711 "A::B::x"; if it doesn't find it, then the second call looks for
712 "A::x", and if that call fails, then the first call looks for
713 "x". */
715 static struct block_symbol
716 lookup_namespace_scope (const struct language_defn *langdef,
717 const char *name,
718 const struct block *block,
719 const domain_search_flags domain,
720 const char *scope,
721 int scope_len)
723 char *the_namespace;
725 if (scope[scope_len] != '\0')
727 /* Recursively search for names in child namespaces first. */
729 struct block_symbol sym;
730 int new_scope_len = scope_len;
732 /* If the current scope is followed by "::", skip past that. */
733 if (new_scope_len != 0)
735 gdb_assert (scope[new_scope_len] == ':');
736 new_scope_len += 2;
738 new_scope_len += cp_find_first_component (scope + new_scope_len);
739 sym = lookup_namespace_scope (langdef, name, block, domain,
740 scope, new_scope_len);
741 if (sym.symbol != NULL)
742 return sym;
745 /* Okay, we didn't find a match in our children, so look for the
746 name in the current namespace.
748 If we there is no scope and we know we have a bare symbol, then short
749 circuit everything and call cp_lookup_bare_symbol directly.
750 This isn't an optimization, rather it allows us to pass LANGDEF which
751 is needed for primitive type lookup. The test doesn't have to be
752 perfect: if NAME is a bare symbol that our test doesn't catch (e.g., a
753 template symbol with "::" in the argument list) then
754 cp_lookup_symbol_in_namespace will catch it. */
756 if (scope_len == 0 && strchr (name, ':') == NULL)
757 return cp_lookup_bare_symbol (langdef, name, block, domain, 1);
759 the_namespace = (char *) alloca (scope_len + 1);
760 strncpy (the_namespace, scope, scope_len);
761 the_namespace[scope_len] = '\0';
762 return cp_lookup_symbol_in_namespace (the_namespace, name,
763 block, domain, 1);
766 /* The C++-specific version of name lookup for static and global
767 names. This makes sure that names get looked for in all namespaces
768 that are in scope. NAME is the natural name of the symbol that
769 we're looking for, BLOCK is the block that we're searching within,
770 DOMAIN says what kind of symbols we're looking for. */
772 struct block_symbol
773 cp_lookup_symbol_nonlocal (const struct language_defn *langdef,
774 const char *name,
775 const struct block *block,
776 const domain_search_flags domain)
778 struct block_symbol sym;
779 const char *scope = block == nullptr ? "" : block->scope ();
781 symbol_lookup_debug_printf
782 ("cp_lookup_symbol_non_local (%s, %s (scope %s), %s)",
783 name, host_address_to_string (block), scope,
784 domain_name (domain).c_str ());
786 /* First, try to find the symbol in the given namespace, and all
787 containing namespaces. */
788 sym = lookup_namespace_scope (langdef, name, block, domain, scope, 0);
790 /* Search for name in namespaces imported to this and parent blocks. */
791 if (sym.symbol == NULL)
792 sym = cp_lookup_symbol_via_all_imports (scope, name, block, domain);
794 symbol_lookup_debug_printf ("cp_lookup_symbol_nonlocal (...) = %s",
795 (sym.symbol != NULL
796 ? host_address_to_string (sym.symbol)
797 : "NULL"));
798 return sym;
801 /* Search through the base classes of PARENT_TYPE for a base class
802 named NAME and return its type. If not found, return NULL. */
804 struct type *
805 cp_find_type_baseclass_by_name (struct type *parent_type, const char *name)
807 int i;
809 parent_type = check_typedef (parent_type);
810 for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i)
812 struct type *type = check_typedef (TYPE_BASECLASS (parent_type, i));
813 const char *tdef_name = TYPE_BASECLASS_NAME (parent_type, i);
814 const char *base_name = type->name ();
816 if (base_name == NULL)
817 continue;
819 if (streq (tdef_name, name) || streq (base_name, name))
820 return type;
822 type = cp_find_type_baseclass_by_name (type, name);
823 if (type != NULL)
824 return type;
827 return NULL;
830 /* Search through the base classes of PARENT_TYPE for a symbol named
831 NAME in block BLOCK. */
833 static struct block_symbol
834 find_symbol_in_baseclass (struct type *parent_type, const char *name,
835 const struct block *block,
836 const domain_search_flags domain,
837 int is_in_anonymous)
839 int i;
840 struct block_symbol sym = {};
842 for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i)
844 struct type *base_type = TYPE_BASECLASS (parent_type, i);
845 const char *base_name = TYPE_BASECLASS_NAME (parent_type, i);
847 if (base_name == NULL)
848 continue;
850 std::string concatenated_name = std::string (base_name) + "::" + name;
852 sym = cp_lookup_nested_symbol_1 (base_type, name,
853 concatenated_name.c_str (),
854 block, domain, 1, is_in_anonymous);
855 if (sym.symbol != NULL)
856 break;
859 return sym;
862 /* Helper function to look up NESTED_NAME in CONTAINER_TYPE and in DOMAIN
863 and within the context of BLOCK.
864 NESTED_NAME may have scope ("::").
865 CONTAINER_TYPE needn't have been "check_typedef'd" yet.
866 CONCATENATED_NAME is the fully scoped spelling of NESTED_NAME, it is
867 passed as an argument so that callers can control how space for it is
868 allocated.
869 If BASIC_LOOKUP is non-zero then perform a basic lookup of
870 CONCATENATED_NAME. See cp_basic_lookup_symbol for details.
871 If IS_IN_ANONYMOUS is non-zero then CONCATENATED_NAME is in an anonymous
872 namespace. */
874 static struct block_symbol
875 cp_lookup_nested_symbol_1 (struct type *container_type,
876 const char *nested_name,
877 const char *concatenated_name,
878 const struct block *block,
879 const domain_search_flags domain,
880 int basic_lookup, int is_in_anonymous)
882 struct block_symbol sym;
884 /* NOTE: carlton/2003-11-10: We don't treat C++ class members
885 of classes like, say, data or function members. Instead,
886 they're just represented by symbols whose names are
887 qualified by the name of the surrounding class. This is
888 just like members of namespaces; in particular,
889 cp_basic_lookup_symbol works when looking them up. */
891 if (basic_lookup)
893 sym = cp_basic_lookup_symbol (concatenated_name, block, domain,
894 is_in_anonymous);
895 if (sym.symbol != NULL)
896 return sym;
899 /* Now search all static file-level symbols. We have to do this for things
900 like typedefs in the class. We do not try to guess any imported
901 namespace as even the fully specified namespace search is already not
902 C++ compliant and more assumptions could make it too magic. */
904 /* First search in this symtab, what we want is possibly there. */
905 sym = lookup_symbol_in_static_block (concatenated_name, block, domain);
906 if (sym.symbol != NULL)
907 return sym;
909 /* Nope. We now have to search all static blocks in all objfiles,
910 even if block != NULL, because there's no guarantees as to which
911 symtab the symbol we want is in. Except for symbols defined in
912 anonymous namespaces should be treated as local to a single file,
913 which we just searched. */
914 if (!is_in_anonymous)
916 sym = lookup_static_symbol (concatenated_name, domain);
917 if (sym.symbol != NULL)
918 return sym;
921 /* If this is a class with baseclasses, search them next. */
922 container_type = check_typedef (container_type);
923 if (TYPE_N_BASECLASSES (container_type) > 0)
925 sym = find_symbol_in_baseclass (container_type, nested_name, block,
926 domain, is_in_anonymous);
927 if (sym.symbol != NULL)
928 return sym;
931 return {};
934 /* Look up a symbol named NESTED_NAME that is nested inside the C++
935 class or namespace given by PARENT_TYPE, from within the context
936 given by BLOCK, and in DOMAIN.
937 Return NULL if there is no such nested symbol. */
939 struct block_symbol
940 cp_lookup_nested_symbol (struct type *parent_type,
941 const char *nested_name,
942 const struct block *block,
943 const domain_search_flags domain)
945 /* type_name_or_error provides better error reporting using the
946 original type. */
947 struct type *saved_parent_type = parent_type;
949 parent_type = check_typedef (parent_type);
951 if (symbol_lookup_debug)
953 const char *type_name = saved_parent_type->name ();
955 symbol_lookup_debug_printf ("cp_lookup_nested_symbol (%s, %s, %s, %s)",
956 type_name != NULL ? type_name : "unnamed",
957 nested_name, host_address_to_string (block),
958 domain_name (domain).c_str ());
961 switch (parent_type->code ())
963 case TYPE_CODE_STRUCT:
964 case TYPE_CODE_NAMESPACE:
965 case TYPE_CODE_UNION:
966 case TYPE_CODE_ENUM:
967 /* NOTE: Handle modules here as well, because Fortran is re-using the C++
968 specific code to lookup nested symbols in modules, by calling the
969 method lookup_symbol_nonlocal, which ends up here. */
970 case TYPE_CODE_MODULE:
972 int size;
973 const char *parent_name = type_name_or_error (saved_parent_type);
974 struct block_symbol sym;
975 char *concatenated_name;
976 int is_in_anonymous;
978 size = strlen (parent_name) + 2 + strlen (nested_name) + 1;
979 concatenated_name = (char *) alloca (size);
980 xsnprintf (concatenated_name, size, "%s::%s",
981 parent_name, nested_name);
982 is_in_anonymous = cp_is_in_anonymous (concatenated_name);
984 sym = cp_lookup_nested_symbol_1 (parent_type, nested_name,
985 concatenated_name, block, domain,
986 1, is_in_anonymous);
988 symbol_lookup_debug_printf ("cp_lookup_nested_symbol (...) = %s",
989 (sym.symbol != NULL
990 ? host_address_to_string (sym.symbol)
991 : "NULL"));
992 return sym;
995 case TYPE_CODE_FUNC:
996 case TYPE_CODE_METHOD:
997 symbol_lookup_debug_printf
998 ("cp_lookup_nested_symbol (...) = NULL (func/method)");
999 return {};
1001 default:
1002 internal_error (_("cp_lookup_nested_symbol called "
1003 "on a non-aggregate type."));
1007 /* The C++-version of lookup_transparent_type. */
1009 /* FIXME: carlton/2004-01-16: The problem that this is trying to
1010 address is that, unfortunately, sometimes NAME is wrong: it may not
1011 include the name of namespaces enclosing the type in question.
1012 lookup_transparent_type gets called when the type in question
1013 is a declaration, and we're trying to find its definition; but, for
1014 declarations, our type name deduction mechanism doesn't work.
1015 There's nothing we can do to fix this in general, I think, in the
1016 absence of debug information about namespaces (I've filed PR
1017 gdb/1511 about this); until such debug information becomes more
1018 prevalent, one heuristic which sometimes looks is to search for the
1019 definition in namespaces containing the current namespace.
1021 We should delete this functions once the appropriate debug
1022 information becomes more widespread. (GCC 3.4 will be the first
1023 released version of GCC with such information.) */
1025 struct type *
1026 cp_lookup_transparent_type (const char *name, domain_search_flags flags)
1028 /* First, try the honest way of looking up the definition. */
1029 struct type *t = basic_lookup_transparent_type (name, flags);
1030 const char *scope;
1032 if (t != NULL)
1033 return t;
1035 /* If that doesn't work and we're within a namespace, look there
1036 instead. */
1037 const block *block = get_selected_block (0);
1038 if (block == nullptr)
1039 return nullptr;
1041 scope = block->scope ();
1043 if (scope[0] == '\0')
1044 return NULL;
1046 return cp_lookup_transparent_type_loop (name, scope, 0);
1049 /* Lookup the type definition associated to NAME in namespaces/classes
1050 containing SCOPE whose name is strictly longer than LENGTH. LENGTH
1051 must be the index of the start of a component of SCOPE. */
1053 static struct type *
1054 cp_lookup_transparent_type_loop (const char *name,
1055 const char *scope,
1056 int length)
1058 int scope_length = length + cp_find_first_component (scope + length);
1059 char *full_name;
1061 /* If the current scope is followed by "::", look in the next
1062 component. */
1063 if (scope[scope_length] == ':')
1065 struct type *retval
1066 = cp_lookup_transparent_type_loop (name, scope,
1067 scope_length + 2);
1069 if (retval != NULL)
1070 return retval;
1073 full_name = (char *) alloca (scope_length + 2 + strlen (name) + 1);
1074 strncpy (full_name, scope, scope_length);
1075 memcpy (full_name + scope_length, "::", 2);
1076 strcpy (full_name + scope_length + 2, name);
1078 return basic_lookup_transparent_type (full_name);
1081 /* This used to do something but was removed when it became
1082 obsolete. */
1084 static void
1085 maintenance_cplus_namespace (const char *args, int from_tty)
1087 gdb_printf (_("The `maint namespace' command was removed.\n"));
1090 void _initialize_cp_namespace ();
1091 void
1092 _initialize_cp_namespace ()
1094 struct cmd_list_element *cmd;
1096 cmd = add_cmd ("namespace", class_maintenance,
1097 maintenance_cplus_namespace,
1098 _("Deprecated placeholder for removed functionality."),
1099 &maint_cplus_cmd_list);
1100 deprecate_cmd (cmd, NULL);