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