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