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