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