Home | History | Annotate | Line # | Download | only in gdb
cp-namespace.c revision 1.1
      1 /* Helper routines for C++ support in GDB.
      2    Copyright (C) 2003-2014 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 "gdb_assert.h"
     27 #include "block.h"
     28 #include "objfiles.h"
     29 #include "gdbtypes.h"
     30 #include "dictionary.h"
     31 #include "command.h"
     32 #include "frame.h"
     33 #include "buildsym.h"
     34 #include "language.h"
     35 
     36 static struct symbol *lookup_namespace_scope (const char *name,
     37 					      const struct block *block,
     38 					      const domain_enum domain,
     39 					      const char *scope,
     40 					      int scope_len);
     41 
     42 static struct symbol *lookup_symbol_file (const char *name,
     43 					  const struct block *block,
     44 					  const domain_enum domain,
     45 					  int anonymous_namespace,
     46 					  int search);
     47 
     48 static struct type *cp_lookup_transparent_type_loop (const char *name,
     49 						     const char *scope,
     50 						     int scope_len);
     51 
     52 /* Check to see if SYMBOL refers to an object contained within an
     53    anonymous namespace; if so, add an appropriate using directive.  */
     54 
     55 void
     56 cp_scan_for_anonymous_namespaces (const struct symbol *const symbol,
     57 				  struct objfile *const objfile)
     58 {
     59   if (SYMBOL_DEMANGLED_NAME (symbol) != NULL)
     60     {
     61       const char *name = SYMBOL_DEMANGLED_NAME (symbol);
     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_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 = alloca (dest_len + 1);
     87 	      char *src = 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 	      cp_add_using_directive (dest, src, NULL, NULL, NULL, 1,
    100 	                              &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 
    112 /* Add a using directive to using_directives.  If the using directive
    113    in question has already been added, don't add it twice.
    114 
    115    Create a new struct using_direct which imports the namespace SRC
    116    into the scope DEST.  ALIAS is the name of the imported namespace
    117    in the current scope.  If ALIAS is NULL then the namespace is known
    118    by its original name.  DECLARATION is the name if the imported
    119    varable if this is a declaration import (Eg. using A::x), otherwise
    120    it is NULL.  EXCLUDES is a list of names not to import from an
    121    imported module or NULL.  If COPY_NAMES is non-zero, then the
    122    arguments are copied into newly allocated memory so they can be
    123    temporaries.  For EXCLUDES the VEC pointers are copied but the
    124    pointed to characters are not copied.  */
    125 
    126 void
    127 cp_add_using_directive (const char *dest,
    128 			const char *src,
    129 			const char *alias,
    130 			const char *declaration,
    131 			VEC (const_char_ptr) *excludes,
    132 			int copy_names,
    133                         struct obstack *obstack)
    134 {
    135   struct using_direct *current;
    136   struct using_direct *new;
    137 
    138   /* Has it already been added?  */
    139 
    140   for (current = using_directives; current != NULL; current = current->next)
    141     {
    142       int ix;
    143       const char *param;
    144 
    145       if (strcmp (current->import_src, src) != 0)
    146 	continue;
    147       if (strcmp (current->import_dest, dest) != 0)
    148 	continue;
    149       if ((alias == NULL && current->alias != NULL)
    150 	  || (alias != NULL && current->alias == NULL)
    151 	  || (alias != NULL && current->alias != NULL
    152 	      && strcmp (alias, current->alias) != 0))
    153 	continue;
    154       if ((declaration == NULL && current->declaration != NULL)
    155 	  || (declaration != NULL && current->declaration == NULL)
    156 	  || (declaration != NULL && current->declaration != NULL
    157 	      && strcmp (declaration, current->declaration) != 0))
    158 	continue;
    159 
    160       /* Compare the contents of EXCLUDES.  */
    161       for (ix = 0; VEC_iterate (const_char_ptr, excludes, ix, param); ix++)
    162 	if (current->excludes[ix] == NULL
    163 	    || strcmp (param, current->excludes[ix]) != 0)
    164 	  break;
    165       if (ix < VEC_length (const_char_ptr, excludes)
    166 	  || current->excludes[ix] != NULL)
    167 	continue;
    168 
    169       /* Parameters exactly match CURRENT.  */
    170       return;
    171     }
    172 
    173   new = obstack_alloc (obstack, (sizeof (*new)
    174 				 + (VEC_length (const_char_ptr, excludes)
    175 				    * sizeof (*new->excludes))));
    176   memset (new, 0, sizeof (*new));
    177 
    178   if (copy_names)
    179     {
    180       new->import_src = obstack_copy0 (obstack, src, strlen (src));
    181       new->import_dest = obstack_copy0 (obstack, dest, strlen (dest));
    182     }
    183   else
    184     {
    185       new->import_src = src;
    186       new->import_dest = dest;
    187     }
    188 
    189   if (alias != NULL && copy_names)
    190     new->alias = obstack_copy0 (obstack, alias, strlen (alias));
    191   else
    192     new->alias = alias;
    193 
    194   if (declaration != NULL && copy_names)
    195     new->declaration = obstack_copy0 (obstack,
    196 				      declaration, strlen (declaration));
    197   else
    198     new->declaration = declaration;
    199 
    200   memcpy (new->excludes, VEC_address (const_char_ptr, excludes),
    201 	  VEC_length (const_char_ptr, excludes) * sizeof (*new->excludes));
    202   new->excludes[VEC_length (const_char_ptr, excludes)] = NULL;
    203 
    204   new->next = using_directives;
    205   using_directives = new;
    206 }
    207 
    208 /* Test whether or not NAMESPACE looks like it mentions an anonymous
    209    namespace; return nonzero if so.  */
    210 
    211 int
    212 cp_is_anonymous (const char *namespace)
    213 {
    214   return (strstr (namespace, CP_ANONYMOUS_NAMESPACE_STR)
    215 	  != NULL);
    216 }
    217 
    218 /* The C++-specific version of name lookup for static and global
    219    names.  This makes sure that names get looked for in all namespaces
    220    that are in scope.  NAME is the natural name of the symbol that
    221    we're looking for, BLOCK is the block that we're searching within,
    222    DOMAIN says what kind of symbols we're looking for, and if SYMTAB
    223    is non-NULL, we should store the symtab where we found the symbol
    224    in it.  */
    225 
    226 struct symbol *
    227 cp_lookup_symbol_nonlocal (const char *name,
    228 			   const struct block *block,
    229 			   const domain_enum domain)
    230 {
    231   struct symbol *sym;
    232   const char *scope = block_scope (block);
    233 
    234   sym = lookup_namespace_scope (name, block,
    235 				domain, scope, 0);
    236   if (sym != NULL)
    237     return sym;
    238 
    239   return cp_lookup_symbol_namespace (scope, name,
    240 				     block, domain);
    241 }
    242 
    243 /* Look up NAME in the C++ namespace NAMESPACE.  Other arguments are
    244    as in cp_lookup_symbol_nonlocal.  If SEARCH is non-zero, search
    245    through base classes for a matching symbol.  */
    246 
    247 static struct symbol *
    248 cp_lookup_symbol_in_namespace (const char *namespace,
    249                                const char *name,
    250                                const struct block *block,
    251                                const domain_enum domain, int search)
    252 {
    253   if (namespace[0] == '\0')
    254     {
    255       return lookup_symbol_file (name, block, domain, 0, search);
    256     }
    257   else
    258     {
    259       char *concatenated_name = alloca (strlen (namespace) + 2
    260 					+ strlen (name) + 1);
    261 
    262       strcpy (concatenated_name, namespace);
    263       strcat (concatenated_name, "::");
    264       strcat (concatenated_name, name);
    265       return lookup_symbol_file (concatenated_name, block, domain,
    266 				 cp_is_anonymous (namespace), search);
    267     }
    268 }
    269 
    270 /* Used for cleanups to reset the "searched" flag incase
    271    of an error.  */
    272 
    273 static void
    274 reset_directive_searched (void *data)
    275 {
    276   struct using_direct *direct = data;
    277   direct->searched = 0;
    278 }
    279 
    280 /* Search for NAME by applying all import statements belonging to
    281    BLOCK which are applicable in SCOPE.  If DECLARATION_ONLY the
    282    search is restricted to using declarations.
    283    Example:
    284 
    285      namespace A {
    286        int x;
    287      }
    288      using A::x;
    289 
    290    If SEARCH_PARENTS the search will include imports which are
    291    applicable in parents of SCOPE.
    292    Example:
    293 
    294      namespace A {
    295        using namespace X;
    296        namespace B {
    297          using namespace Y;
    298        }
    299      }
    300 
    301    If SCOPE is "A::B" and SEARCH_PARENTS is true the imports of
    302    namespaces X and Y will be considered.  If SEARCH_PARENTS is false
    303    only the import of Y is considered.  */
    304 
    305 struct symbol *
    306 cp_lookup_symbol_imports (const char *scope,
    307                           const char *name,
    308                           const struct block *block,
    309                           const domain_enum domain,
    310                           const int declaration_only,
    311                           const int search_parents)
    312 {
    313   struct using_direct *current;
    314   struct symbol *sym = NULL;
    315   int len;
    316   int directive_match;
    317   struct cleanup *searched_cleanup;
    318 
    319   /* First, try to find the symbol in the given namespace.  */
    320   if (!declaration_only)
    321     sym = cp_lookup_symbol_in_namespace (scope, name,
    322 					 block, domain, 1);
    323 
    324   if (sym != NULL)
    325     return sym;
    326 
    327   /* Go through the using directives.  If any of them add new names to
    328      the namespace we're searching in, see if we can find a match by
    329      applying them.  */
    330 
    331   for (current = block_using (block);
    332        current != NULL;
    333        current = current->next)
    334     {
    335       const char **excludep;
    336 
    337       len = strlen (current->import_dest);
    338       directive_match = (search_parents
    339                          ? (strncmp (scope, current->import_dest,
    340                                      strlen (current->import_dest)) == 0
    341                             && (len == 0
    342                                 || scope[len] == ':'
    343 				|| scope[len] == '\0'))
    344                          : strcmp (scope, current->import_dest) == 0);
    345 
    346       /* If the import destination is the current scope or one of its
    347          ancestors then it is applicable.  */
    348       if (directive_match && !current->searched)
    349 	{
    350 	  /* Mark this import as searched so that the recursive call
    351 	     does not search it again.  */
    352 	  current->searched = 1;
    353 	  searched_cleanup = make_cleanup (reset_directive_searched,
    354 					   current);
    355 
    356 	  /* If there is an import of a single declaration, compare the
    357 	     imported declaration (after optional renaming by its alias)
    358 	     with the sought out name.  If there is a match pass
    359 	     current->import_src as NAMESPACE to direct the search
    360 	     towards the imported namespace.  */
    361 	  if (current->declaration
    362 	      && strcmp (name, current->alias
    363 			 ? current->alias : current->declaration) == 0)
    364 	    sym = cp_lookup_symbol_in_namespace (current->import_src,
    365 						 current->declaration,
    366 						 block, domain, 1);
    367 
    368 	  /* If this is a DECLARATION_ONLY search or a symbol was found
    369 	     or this import statement was an import declaration, the
    370 	     search of this import is complete.  */
    371 	  if (declaration_only || sym != NULL || current->declaration)
    372 	    {
    373 	      current->searched = 0;
    374 	      discard_cleanups (searched_cleanup);
    375 
    376 	      if (sym != NULL)
    377 		return sym;
    378 
    379 	      continue;
    380 	    }
    381 
    382 	  /* Do not follow CURRENT if NAME matches its EXCLUDES.  */
    383 	  for (excludep = current->excludes; *excludep; excludep++)
    384 	    if (strcmp (name, *excludep) == 0)
    385 	      break;
    386 	  if (*excludep)
    387 	    {
    388 	      discard_cleanups (searched_cleanup);
    389 	      continue;
    390 	    }
    391 
    392 	  if (current->alias != NULL
    393 	      && strcmp (name, current->alias) == 0)
    394 	    /* If the import is creating an alias and the alias matches
    395 	       the sought name.  Pass current->import_src as the NAME to
    396 	       direct the search towards the aliased namespace.  */
    397 	    {
    398 	      sym = cp_lookup_symbol_in_namespace (scope,
    399 						   current->import_src,
    400 						   block, domain, 1);
    401 	    }
    402 	  else if (current->alias == NULL)
    403 	    {
    404 	      /* If this import statement creates no alias, pass
    405 		 current->inner as NAMESPACE to direct the search
    406 		 towards the imported namespace.  */
    407 	      sym = cp_lookup_symbol_imports (current->import_src,
    408 					      name, block,
    409 					      domain, 0, 0);
    410 	    }
    411 	  current->searched = 0;
    412 	  discard_cleanups (searched_cleanup);
    413 
    414 	  if (sym != NULL)
    415 	    return sym;
    416 	}
    417     }
    418 
    419   return NULL;
    420 }
    421 
    422 /* Helper function that searches an array of symbols for one named
    423    NAME.  */
    424 
    425 static struct symbol *
    426 search_symbol_list (const char *name, int num,
    427 		    struct symbol **syms)
    428 {
    429   int i;
    430 
    431   /* Maybe we should store a dictionary in here instead.  */
    432   for (i = 0; i < num; ++i)
    433     {
    434       if (strcmp (name, SYMBOL_NATURAL_NAME (syms[i])) == 0)
    435 	return syms[i];
    436     }
    437   return NULL;
    438 }
    439 
    440 /* Like cp_lookup_symbol_imports, but if BLOCK is a function, it
    441    searches through the template parameters of the function and the
    442    function's type.  */
    443 
    444 struct symbol *
    445 cp_lookup_symbol_imports_or_template (const char *scope,
    446 				      const char *name,
    447 				      const struct block *block,
    448 				      const domain_enum domain)
    449 {
    450   struct symbol *function = BLOCK_FUNCTION (block);
    451 
    452   if (function != NULL && SYMBOL_LANGUAGE (function) == language_cplus)
    453     {
    454       /* Search the function's template parameters.  */
    455       if (SYMBOL_IS_CPLUS_TEMPLATE_FUNCTION (function))
    456 	{
    457 	  struct template_symbol *templ
    458 	    = (struct template_symbol *) function;
    459 	  struct symbol *result;
    460 
    461 	  result = search_symbol_list (name,
    462 				       templ->n_template_arguments,
    463 				       templ->template_arguments);
    464 	  if (result != NULL)
    465 	    return result;
    466 	}
    467 
    468       /* Search the template parameters of the function's defining
    469 	 context.  */
    470       if (SYMBOL_NATURAL_NAME (function))
    471 	{
    472 	  struct type *context;
    473 	  char *name_copy = xstrdup (SYMBOL_NATURAL_NAME (function));
    474 	  struct cleanup *cleanups = make_cleanup (xfree, name_copy);
    475 	  const struct language_defn *lang = language_def (language_cplus);
    476 	  struct gdbarch *arch
    477 	    = get_objfile_arch (SYMBOL_SYMTAB (function)->objfile);
    478 	  const struct block *parent = BLOCK_SUPERBLOCK (block);
    479 
    480 	  while (1)
    481 	    {
    482 	      struct symbol *result;
    483 	      unsigned int prefix_len = cp_entire_prefix_len (name_copy);
    484 
    485 	      if (prefix_len == 0)
    486 		context = NULL;
    487 	      else
    488 		{
    489 		  name_copy[prefix_len] = '\0';
    490 		  context = lookup_typename (lang, arch,
    491 					     name_copy,
    492 					     parent, 1);
    493 		}
    494 
    495 	      if (context == NULL)
    496 		break;
    497 
    498 	      result
    499 		= search_symbol_list (name,
    500 				      TYPE_N_TEMPLATE_ARGUMENTS (context),
    501 				      TYPE_TEMPLATE_ARGUMENTS (context));
    502 	      if (result != NULL)
    503 		{
    504 		  do_cleanups (cleanups);
    505 		  return result;
    506 		}
    507 	    }
    508 
    509 	  do_cleanups (cleanups);
    510 	}
    511     }
    512 
    513   return cp_lookup_symbol_imports (scope, name, block, domain, 1, 1);
    514 }
    515 
    516  /* Searches for NAME in the current namespace, and by applying
    517     relevant import statements belonging to BLOCK and its parents.
    518     SCOPE is the namespace scope of the context in which the search is
    519     being evaluated.  */
    520 
    521 struct symbol*
    522 cp_lookup_symbol_namespace (const char *scope,
    523                             const char *name,
    524                             const struct block *block,
    525                             const domain_enum domain)
    526 {
    527   struct symbol *sym;
    528 
    529   /* First, try to find the symbol in the given namespace.  */
    530   sym = cp_lookup_symbol_in_namespace (scope, name,
    531 				       block, domain, 1);
    532   if (sym != NULL)
    533     return sym;
    534 
    535   /* Search for name in namespaces imported to this and parent
    536      blocks.  */
    537   while (block != NULL)
    538     {
    539       sym = cp_lookup_symbol_imports (scope, name, block,
    540 				      domain, 0, 1);
    541 
    542       if (sym)
    543 	return sym;
    544 
    545       block = BLOCK_SUPERBLOCK (block);
    546     }
    547 
    548   return NULL;
    549 }
    550 
    551 /* Lookup NAME at namespace scope (or, in C terms, in static and
    552    global variables).  SCOPE is the namespace that the current
    553    function is defined within; only consider namespaces whose length
    554    is at least SCOPE_LEN.  Other arguments are as in
    555    cp_lookup_symbol_nonlocal.
    556 
    557    For example, if we're within a function A::B::f and looking for a
    558    symbol x, this will get called with NAME = "x", SCOPE = "A::B", and
    559    SCOPE_LEN = 0.  It then calls itself with NAME and SCOPE the same,
    560    but with SCOPE_LEN = 1.  And then it calls itself with NAME and
    561    SCOPE the same, but with SCOPE_LEN = 4.  This third call looks for
    562    "A::B::x"; if it doesn't find it, then the second call looks for
    563    "A::x", and if that call fails, then the first call looks for
    564    "x".  */
    565 
    566 static struct symbol *
    567 lookup_namespace_scope (const char *name,
    568 			const struct block *block,
    569 			const domain_enum domain,
    570 			const char *scope,
    571 			int scope_len)
    572 {
    573   char *namespace;
    574 
    575   if (scope[scope_len] != '\0')
    576     {
    577       /* Recursively search for names in child namespaces first.  */
    578 
    579       struct symbol *sym;
    580       int new_scope_len = scope_len;
    581 
    582       /* If the current scope is followed by "::", skip past that.  */
    583       if (new_scope_len != 0)
    584 	{
    585 	  gdb_assert (scope[new_scope_len] == ':');
    586 	  new_scope_len += 2;
    587 	}
    588       new_scope_len += cp_find_first_component (scope + new_scope_len);
    589       sym = lookup_namespace_scope (name, block, domain,
    590 				    scope, new_scope_len);
    591       if (sym != NULL)
    592 	return sym;
    593     }
    594 
    595   /* Okay, we didn't find a match in our children, so look for the
    596      name in the current namespace.  */
    597 
    598   namespace = alloca (scope_len + 1);
    599   strncpy (namespace, scope, scope_len);
    600   namespace[scope_len] = '\0';
    601   return cp_lookup_symbol_in_namespace (namespace, name,
    602 					block, domain, 1);
    603 }
    604 
    605 /* Look up NAME in BLOCK's static block and in global blocks.  If
    606    ANONYMOUS_NAMESPACE is nonzero, the symbol in question is located
    607    within an anonymous namespace.  If SEARCH is non-zero, search through
    608    base classes for a matching symbol.  Other arguments are as in
    609    cp_lookup_symbol_nonlocal.  */
    610 
    611 static struct symbol *
    612 lookup_symbol_file (const char *name,
    613 		    const struct block *block,
    614 		    const domain_enum domain,
    615 		    int anonymous_namespace, int search)
    616 {
    617   struct symbol *sym = NULL;
    618 
    619   sym = lookup_symbol_static (name, block, domain);
    620   if (sym != NULL)
    621     return sym;
    622 
    623   if (anonymous_namespace)
    624     {
    625       /* Symbols defined in anonymous namespaces have external linkage
    626 	 but should be treated as local to a single file nonetheless.
    627 	 So we only search the current file's global block.  */
    628 
    629       const struct block *global_block = block_global_block (block);
    630 
    631       if (global_block != NULL)
    632 	sym = lookup_symbol_aux_block (name, global_block, domain);
    633     }
    634   else
    635     {
    636       sym = lookup_symbol_global (name, block, domain);
    637     }
    638 
    639   if (sym != NULL)
    640     return sym;
    641 
    642   if (search)
    643     {
    644       char *klass, *nested;
    645       unsigned int prefix_len;
    646       struct cleanup *cleanup;
    647       struct symbol *klass_sym;
    648 
    649       /* A simple lookup failed.  Check if the symbol was defined in
    650 	 a base class.  */
    651 
    652       cleanup = make_cleanup (null_cleanup, NULL);
    653 
    654       /* Find the name of the class and the name of the method,
    655 	 variable, etc.  */
    656       prefix_len = cp_entire_prefix_len (name);
    657 
    658       /* If no prefix was found, search "this".  */
    659       if (prefix_len == 0)
    660 	{
    661 	  struct type *type;
    662 	  struct symbol *this;
    663 
    664 	  this = lookup_language_this (language_def (language_cplus), block);
    665 	  if (this == NULL)
    666 	    {
    667 	      do_cleanups (cleanup);
    668 	      return NULL;
    669 	    }
    670 
    671 	  type = check_typedef (TYPE_TARGET_TYPE (SYMBOL_TYPE (this)));
    672 	  klass = xstrdup (TYPE_NAME (type));
    673 	  nested = xstrdup (name);
    674 	}
    675       else
    676 	{
    677 	  /* The class name is everything up to and including PREFIX_LEN.  */
    678 	  klass = savestring (name, prefix_len);
    679 
    680 	  /* The rest of the name is everything else past the initial scope
    681 	     operator.  */
    682 	  nested = xstrdup (name + prefix_len + 2);
    683 	}
    684 
    685       /* Add cleanups to free memory for these strings.  */
    686       make_cleanup (xfree, klass);
    687       make_cleanup (xfree, nested);
    688 
    689       /* Lookup a class named KLASS.  If none is found, there is nothing
    690 	 more that can be done.  */
    691       klass_sym = lookup_symbol_global (klass, block, domain);
    692       if (klass_sym == NULL)
    693 	{
    694 	  do_cleanups (cleanup);
    695 	  return NULL;
    696 	}
    697 
    698       /* Look for a symbol named NESTED in this class.  */
    699       sym = cp_lookup_nested_symbol (SYMBOL_TYPE (klass_sym), nested, block);
    700       do_cleanups (cleanup);
    701     }
    702 
    703   return sym;
    704 }
    705 
    706 /* Search through the base classes of PARENT_TYPE for a base class
    707    named NAME and return its type.  If not found, return NULL.  */
    708 
    709 struct type *
    710 find_type_baseclass_by_name (struct type *parent_type, const char *name)
    711 {
    712   int i;
    713 
    714   CHECK_TYPEDEF (parent_type);
    715   for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i)
    716     {
    717       struct type *type = check_typedef (TYPE_BASECLASS (parent_type, i));
    718       const char *base_name = TYPE_BASECLASS_NAME (parent_type, i);
    719 
    720       if (base_name == NULL)
    721 	continue;
    722 
    723       if (streq (base_name, name))
    724 	return type;
    725 
    726       type = find_type_baseclass_by_name (type, name);
    727       if (type != NULL)
    728 	return type;
    729     }
    730 
    731   return NULL;
    732 }
    733 
    734 /* Search through the base classes of PARENT_TYPE for a symbol named
    735    NAME in block BLOCK.  */
    736 
    737 static struct symbol *
    738 find_symbol_in_baseclass (struct type *parent_type, const char *name,
    739 			   const struct block *block)
    740 {
    741   int i;
    742   struct symbol *sym;
    743   struct cleanup *cleanup;
    744   char *concatenated_name;
    745 
    746   sym = NULL;
    747   concatenated_name = NULL;
    748   cleanup = make_cleanup (free_current_contents, &concatenated_name);
    749   for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i)
    750     {
    751       size_t len;
    752       struct type *base_type = TYPE_BASECLASS (parent_type, i);
    753       const char *base_name = TYPE_BASECLASS_NAME (parent_type, i);
    754 
    755       if (base_name == NULL)
    756 	continue;
    757 
    758       /* Search this particular base class.  */
    759       sym = cp_lookup_symbol_in_namespace (base_name, name, block,
    760 					   VAR_DOMAIN, 0);
    761       if (sym != NULL)
    762 	break;
    763 
    764       /* Now search all static file-level symbols.  We have to do this for
    765 	 things like typedefs in the class.  First search in this symtab,
    766 	 what we want is possibly there.  */
    767       len = strlen (base_name) + 2 + strlen (name) + 1;
    768       concatenated_name = xrealloc (concatenated_name, len);
    769       xsnprintf (concatenated_name, len, "%s::%s", base_name, name);
    770       sym = lookup_symbol_static (concatenated_name, block, VAR_DOMAIN);
    771       if (sym != NULL)
    772 	break;
    773 
    774       /* Nope.  We now have to search all static blocks in all objfiles,
    775 	 even if block != NULL, because there's no guarantees as to which
    776 	 symtab the symbol we want is in.  */
    777       sym = lookup_static_symbol_aux (concatenated_name, VAR_DOMAIN);
    778       if (sym != NULL)
    779 	break;
    780 
    781       /* If this class has base classes, search them next.  */
    782       CHECK_TYPEDEF (base_type);
    783       if (TYPE_N_BASECLASSES (base_type) > 0)
    784 	{
    785 	  sym = find_symbol_in_baseclass (base_type, name, block);
    786 	  if (sym != NULL)
    787 	    break;
    788 	}
    789     }
    790 
    791   do_cleanups (cleanup);
    792   return sym;
    793 }
    794 
    795 /* Look up a symbol named NESTED_NAME that is nested inside the C++
    796    class or namespace given by PARENT_TYPE, from within the context
    797    given by BLOCK.  Return NULL if there is no such nested type.  */
    798 
    799 struct symbol *
    800 cp_lookup_nested_symbol (struct type *parent_type,
    801 			 const char *nested_name,
    802 			 const struct block *block)
    803 {
    804   /* type_name_no_tag_required provides better error reporting using the
    805      original type.  */
    806   struct type *saved_parent_type = parent_type;
    807 
    808   CHECK_TYPEDEF (parent_type);
    809 
    810   switch (TYPE_CODE (parent_type))
    811     {
    812     case TYPE_CODE_STRUCT:
    813     case TYPE_CODE_NAMESPACE:
    814     case TYPE_CODE_UNION:
    815     /* NOTE: Handle modules here as well, because Fortran is re-using the C++
    816        specific code to lookup nested symbols in modules, by calling the
    817        function pointer la_lookup_symbol_nonlocal, which ends up here.  */
    818     case TYPE_CODE_MODULE:
    819       {
    820 	/* NOTE: carlton/2003-11-10: We don't treat C++ class members
    821 	   of classes like, say, data or function members.  Instead,
    822 	   they're just represented by symbols whose names are
    823 	   qualified by the name of the surrounding class.  This is
    824 	   just like members of namespaces; in particular,
    825 	   lookup_symbol_namespace works when looking them up.  */
    826 
    827 	int size;
    828 	const char *parent_name = type_name_no_tag_or_error (saved_parent_type);
    829 	struct symbol *sym
    830 	  = cp_lookup_symbol_in_namespace (parent_name, nested_name,
    831 					   block, VAR_DOMAIN, 0);
    832 	char *concatenated_name;
    833 
    834 	if (sym != NULL)
    835 	  return sym;
    836 
    837 	/* Now search all static file-level symbols.  We have to do this
    838 	   for things like typedefs in the class.  We do not try to
    839 	   guess any imported namespace as even the fully specified
    840 	   namespace search is already not C++ compliant and more
    841 	   assumptions could make it too magic.  */
    842 
    843 	size = strlen (parent_name) + 2 + strlen (nested_name) + 1;
    844 	concatenated_name = alloca (size);
    845 	xsnprintf (concatenated_name, size, "%s::%s",
    846 		 parent_name, nested_name);
    847 	sym = lookup_static_symbol_aux (concatenated_name, VAR_DOMAIN);
    848 	if (sym != NULL)
    849 	  return sym;
    850 
    851 	/* If no matching symbols were found, try searching any
    852 	   base classes.  */
    853 	return find_symbol_in_baseclass (parent_type, nested_name, block);
    854       }
    855 
    856     case TYPE_CODE_FUNC:
    857     case TYPE_CODE_METHOD:
    858       return NULL;
    859 
    860     default:
    861       internal_error (__FILE__, __LINE__,
    862 		      _("cp_lookup_nested_symbol called "
    863 			"on a non-aggregate type."));
    864     }
    865 }
    866 
    867 /* The C++-version of lookup_transparent_type.  */
    868 
    869 /* FIXME: carlton/2004-01-16: The problem that this is trying to
    870    address is that, unfortunately, sometimes NAME is wrong: it may not
    871    include the name of namespaces enclosing the type in question.
    872    lookup_transparent_type gets called when the type in question
    873    is a declaration, and we're trying to find its definition; but, for
    874    declarations, our type name deduction mechanism doesn't work.
    875    There's nothing we can do to fix this in general, I think, in the
    876    absence of debug information about namespaces (I've filed PR
    877    gdb/1511 about this); until such debug information becomes more
    878    prevalent, one heuristic which sometimes looks is to search for the
    879    definition in namespaces containing the current namespace.
    880 
    881    We should delete this functions once the appropriate debug
    882    information becomes more widespread.  (GCC 3.4 will be the first
    883    released version of GCC with such information.)  */
    884 
    885 struct type *
    886 cp_lookup_transparent_type (const char *name)
    887 {
    888   /* First, try the honest way of looking up the definition.  */
    889   struct type *t = basic_lookup_transparent_type (name);
    890   const char *scope;
    891 
    892   if (t != NULL)
    893     return t;
    894 
    895   /* If that doesn't work and we're within a namespace, look there
    896      instead.  */
    897   scope = block_scope (get_selected_block (0));
    898 
    899   if (scope[0] == '\0')
    900     return NULL;
    901 
    902   return cp_lookup_transparent_type_loop (name, scope, 0);
    903 }
    904 
    905 /* Lookup the type definition associated to NAME in namespaces/classes
    906    containing SCOPE whose name is strictly longer than LENGTH.  LENGTH
    907    must be the index of the start of a component of SCOPE.  */
    908 
    909 static struct type *
    910 cp_lookup_transparent_type_loop (const char *name,
    911 				 const char *scope,
    912 				 int length)
    913 {
    914   int scope_length = length + cp_find_first_component (scope + length);
    915   char *full_name;
    916 
    917   /* If the current scope is followed by "::", look in the next
    918      component.  */
    919   if (scope[scope_length] == ':')
    920     {
    921       struct type *retval
    922 	= cp_lookup_transparent_type_loop (name, scope,
    923 					   scope_length + 2);
    924 
    925       if (retval != NULL)
    926 	return retval;
    927     }
    928 
    929   full_name = alloca (scope_length + 2 + strlen (name) + 1);
    930   strncpy (full_name, scope, scope_length);
    931   strncpy (full_name + scope_length, "::", 2);
    932   strcpy (full_name + scope_length + 2, name);
    933 
    934   return basic_lookup_transparent_type (full_name);
    935 }
    936 
    937 /* This used to do something but was removed when it became
    938    obsolete.  */
    939 
    940 static void
    941 maintenance_cplus_namespace (char *args, int from_tty)
    942 {
    943   printf_unfiltered (_("The `maint namespace' command was removed.\n"));
    944 }
    945 
    946 /* Provide a prototype to silence -Wmissing-prototypes.  */
    947 extern initialize_file_ftype _initialize_cp_namespace;
    948 
    949 void
    950 _initialize_cp_namespace (void)
    951 {
    952   struct cmd_list_element *cmd;
    953 
    954   cmd = add_cmd ("namespace", class_maintenance,
    955 		 maintenance_cplus_namespace,
    956 		 _("Deprecated placeholder for removed functionality."),
    957 		 &maint_cplus_cmd_list);
    958   deprecate_cmd (cmd, NULL);
    959 }
    960