gdb/cp-namespace.c - gdb

Functions defined

Source code

  1. /* Helper routines for C++ support in GDB.
  2.    Copyright (C) 2003-2015 Free Software Foundation, Inc.

  3.    Contributed by David Carlton and by Kealia, Inc.

  4.    This file is part of GDB.

  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 3 of the License, or
  8.    (at your option) any later version.

  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.

  13.    You should have received a copy of the GNU General Public License
  14.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  15. #include "defs.h"
  16. #include "cp-support.h"
  17. #include "gdb_obstack.h"
  18. #include "symtab.h"
  19. #include "symfile.h"
  20. #include "block.h"
  21. #include "objfiles.h"
  22. #include "gdbtypes.h"
  23. #include "dictionary.h"
  24. #include "command.h"
  25. #include "frame.h"
  26. #include "buildsym.h"
  27. #include "language.h"

  28. static struct symbol *
  29.   cp_lookup_nested_symbol_1 (struct type *container_type,
  30.                              const char *nested_name,
  31.                              const char *concatenated_name,
  32.                              const struct block *block,
  33.                              int basic_lookup);

  34. static struct type *cp_lookup_transparent_type_loop (const char *name,
  35.                                                      const char *scope,
  36.                                                      int scope_len);

  37. /* Check to see if SYMBOL refers to an object contained within an
  38.    anonymous namespace; if so, add an appropriate using directive.  */

  39. void
  40. cp_scan_for_anonymous_namespaces (const struct symbol *const symbol,
  41.                                   struct objfile *const objfile)
  42. {
  43.   if (SYMBOL_DEMANGLED_NAME (symbol) != NULL)
  44.     {
  45.       const char *name = SYMBOL_DEMANGLED_NAME (symbol);
  46.       unsigned int previous_component;
  47.       unsigned int next_component;

  48.       /* Start with a quick-and-dirty check for mention of "(anonymous
  49.          namespace)".  */

  50.       if (!cp_is_in_anonymous (name))
  51.         return;

  52.       previous_component = 0;
  53.       next_component = cp_find_first_component (name + previous_component);

  54.       while (name[next_component] == ':')
  55.         {
  56.           if (((next_component - previous_component)
  57.                == CP_ANONYMOUS_NAMESPACE_LEN)
  58.               && strncmp (name + previous_component,
  59.                           CP_ANONYMOUS_NAMESPACE_STR,
  60.                           CP_ANONYMOUS_NAMESPACE_LEN) == 0)
  61.             {
  62.               int dest_len = (previous_component == 0
  63.                               ? 0 : previous_component - 2);
  64.               int src_len = next_component;

  65.               char *dest = alloca (dest_len + 1);
  66.               char *src = alloca (src_len + 1);

  67.               memcpy (dest, name, dest_len);
  68.               memcpy (src, name, src_len);

  69.               dest[dest_len] = '\0';
  70.               src[src_len] = '\0';

  71.               /* We've found a component of the name that's an
  72.                  anonymous namespace.  So add symbols in it to the
  73.                  namespace given by the previous component if there is
  74.                  one, or to the global namespace if there isn't.  */
  75.               cp_add_using_directive (dest, src, NULL, NULL, NULL, 1,
  76.                                       &objfile->objfile_obstack);
  77.             }
  78.           /* The "+ 2" is for the "::".  */
  79.           previous_component = next_component + 2;
  80.           next_component = (previous_component
  81.                             + cp_find_first_component (name
  82.                                                        + previous_component));
  83.         }
  84.     }
  85. }

  86. /* Add a using directive to using_directives.  If the using directive
  87.    in question has already been added, don't add it twice.

  88.    Create a new struct using_direct which imports the namespace SRC
  89.    into the scope DEST.  ALIAS is the name of the imported namespace
  90.    in the current scope.  If ALIAS is NULL then the namespace is known
  91.    by its original name.  DECLARATION is the name if the imported
  92.    varable if this is a declaration import (Eg. using A::x), otherwise
  93.    it is NULL.  EXCLUDES is a list of names not to import from an
  94.    imported module or NULL.  If COPY_NAMES is non-zero, then the
  95.    arguments are copied into newly allocated memory so they can be
  96.    temporaries.  For EXCLUDES the VEC pointers are copied but the
  97.    pointed to characters are not copied.  */

  98. void
  99. cp_add_using_directive (const char *dest,
  100.                         const char *src,
  101.                         const char *alias,
  102.                         const char *declaration,
  103.                         VEC (const_char_ptr) *excludes,
  104.                         int copy_names,
  105.                         struct obstack *obstack)
  106. {
  107.   struct using_direct *current;
  108.   struct using_direct *new;

  109.   /* Has it already been added?  */

  110.   for (current = using_directives; current != NULL; current = current->next)
  111.     {
  112.       int ix;
  113.       const char *param;

  114.       if (strcmp (current->import_src, src) != 0)
  115.         continue;
  116.       if (strcmp (current->import_dest, dest) != 0)
  117.         continue;
  118.       if ((alias == NULL && current->alias != NULL)
  119.           || (alias != NULL && current->alias == NULL)
  120.           || (alias != NULL && current->alias != NULL
  121.               && strcmp (alias, current->alias) != 0))
  122.         continue;
  123.       if ((declaration == NULL && current->declaration != NULL)
  124.           || (declaration != NULL && current->declaration == NULL)
  125.           || (declaration != NULL && current->declaration != NULL
  126.               && strcmp (declaration, current->declaration) != 0))
  127.         continue;

  128.       /* Compare the contents of EXCLUDES.  */
  129.       for (ix = 0; VEC_iterate (const_char_ptr, excludes, ix, param); ix++)
  130.         if (current->excludes[ix] == NULL
  131.             || strcmp (param, current->excludes[ix]) != 0)
  132.           break;
  133.       if (ix < VEC_length (const_char_ptr, excludes)
  134.           || current->excludes[ix] != NULL)
  135.         continue;

  136.       /* Parameters exactly match CURRENT.  */
  137.       return;
  138.     }

  139.   new = obstack_alloc (obstack, (sizeof (*new)
  140.                                  + (VEC_length (const_char_ptr, excludes)
  141.                                     * sizeof (*new->excludes))));
  142.   memset (new, 0, sizeof (*new));

  143.   if (copy_names)
  144.     {
  145.       new->import_src = obstack_copy0 (obstack, src, strlen (src));
  146.       new->import_dest = obstack_copy0 (obstack, dest, strlen (dest));
  147.     }
  148.   else
  149.     {
  150.       new->import_src = src;
  151.       new->import_dest = dest;
  152.     }

  153.   if (alias != NULL && copy_names)
  154.     new->alias = obstack_copy0 (obstack, alias, strlen (alias));
  155.   else
  156.     new->alias = alias;

  157.   if (declaration != NULL && copy_names)
  158.     new->declaration = obstack_copy0 (obstack,
  159.                                       declaration, strlen (declaration));
  160.   else
  161.     new->declaration = declaration;

  162.   memcpy (new->excludes, VEC_address (const_char_ptr, excludes),
  163.           VEC_length (const_char_ptr, excludes) * sizeof (*new->excludes));
  164.   new->excludes[VEC_length (const_char_ptr, excludes)] = NULL;

  165.   new->next = using_directives;
  166.   using_directives = new;
  167. }

  168. /* Test whether or not NAMESPACE looks like it mentions an anonymous
  169.    namespace; return nonzero if so.  */

  170. int
  171. cp_is_in_anonymous (const char *symbol_name)
  172. {
  173.   return (strstr (symbol_name, CP_ANONYMOUS_NAMESPACE_STR)
  174.           != NULL);
  175. }

  176. /* Look up NAME in DOMAIN in BLOCK's static block and in global blocks.
  177.    If ANONYMOUS_NAMESPACE is nonzero, the symbol in question is located
  178.    within an anonymous namespace.  */

  179. static struct symbol *
  180. cp_basic_lookup_symbol (const char *name, const struct block *block,
  181.                         const domain_enum domain, int anonymous_namespace)
  182. {
  183.   struct symbol *sym;

  184.   sym = lookup_symbol_in_static_block (name, block, domain);
  185.   if (sym != NULL)
  186.     return sym;

  187.   if (anonymous_namespace)
  188.     {
  189.       /* Symbols defined in anonymous namespaces have external linkage
  190.          but should be treated as local to a single file nonetheless.
  191.          So we only search the current file's global block.  */

  192.       const struct block *global_block = block_global_block (block);

  193.       if (global_block != NULL)
  194.         sym = lookup_symbol_in_block (name, global_block, domain);
  195.     }
  196.   else
  197.     {
  198.       sym = lookup_global_symbol (name, block, domain);
  199.     }

  200.   return sym;
  201. }

  202. /* Search bare symbol NAME in DOMAIN in BLOCK.
  203.    NAME is guaranteed to not have any scope (no "::") in its name, though
  204.    if for example NAME is a template spec then "::" may appear in the
  205.    argument list.
  206.    If LANGDEF is non-NULL then try to lookup NAME as a primitive type in
  207.    that language.  Normally we wouldn't need LANGDEF but fortran also uses
  208.    this code.
  209.    If SEARCH is non-zero then see if we can determine "this" from BLOCK, and
  210.    if so then also search for NAME in that class.  */

  211. static struct symbol *
  212. cp_lookup_bare_symbol (const struct language_defn *langdef,
  213.                        const char *name, const struct block *block,
  214.                        const domain_enum domain, int search)
  215. {
  216.   struct symbol *sym;

  217.   /* Note: We can't do a simple assert for ':' not being in NAME because
  218.      ':' may be in the args of a template spec.  This isn't intended to be
  219.      a complete test, just cheap and documentary.  */
  220.   if (strchr (name, '<') == NULL && strchr (name, '(') == NULL)
  221.     gdb_assert (strchr (name, ':') == NULL);

  222.   sym = lookup_symbol_in_static_block (name, block, domain);
  223.   if (sym != NULL)
  224.     return sym;

  225.   /* If we didn't find a definition for a builtin type in the static block,
  226.      search for it now.  This is actually the right thing to do and can be
  227.      a massive performance win.  E.g., when debugging a program with lots of
  228.      shared libraries we could search all of them only to find out the
  229.      builtin type isn't defined in any of them.  This is common for types
  230.      like "void".  */
  231.   if (langdef != NULL && domain == VAR_DOMAIN)
  232.     {
  233.       struct gdbarch *gdbarch;

  234.       if (block == NULL)
  235.         gdbarch = target_gdbarch ();
  236.       else
  237.         gdbarch = block_gdbarch (block);
  238.       sym = language_lookup_primitive_type_as_symbol (langdef, gdbarch, name);
  239.       if (sym != NULL)
  240.         return sym;
  241.     }

  242.   sym = lookup_global_symbol (name, block, domain);
  243.   if (sym != NULL)
  244.     return sym;

  245.   if (search)
  246.     {
  247.       struct symbol *this;
  248.       struct type *type;

  249.       this = lookup_language_this (language_def (language_cplus), block);
  250.       if (this == NULL)
  251.         return NULL;

  252.       type = check_typedef (TYPE_TARGET_TYPE (SYMBOL_TYPE (this)));
  253.       /* If TYPE_NAME is NULL, abandon trying to find this symbol.
  254.          This can happen for lambda functions compiled with clang++,
  255.          which outputs no name for the container class.  */
  256.       if (TYPE_NAME (type) == NULL)
  257.         return NULL;

  258.       /* Look for a symbol named NESTED in this class.  */
  259.       sym = cp_lookup_nested_symbol (type, name, block);
  260.     }

  261.   return sym;
  262. }

  263. /* Search NAME in DOMAIN in all static blocks, and then in all baseclasses.
  264.    BLOCK specifies the context in which to perform the search.
  265.    NAME is guaranteed to have scope (contain "::") and PREFIX_LEN specifies
  266.    then length the entire scope of NAME (up to, but not including, the last
  267.    "::".

  268.    Note: At least in the case of Fortran, which also uses this code, there
  269.    may be no text after the last "::".  */

  270. static struct symbol *
  271. cp_search_static_and_baseclasses (const char *name,
  272.                                   const struct block *block,
  273.                                   const domain_enum domain,
  274.                                   unsigned int prefix_len)
  275. {
  276.   struct symbol *sym;
  277.   char *klass, *nested;
  278.   struct cleanup *cleanup;
  279.   struct symbol *klass_sym;
  280.   struct type *klass_type;

  281.   /* The test here uses <= instead of < because Fortran also uses this,
  282.      and the module.exp testcase will pass "modmany::" for NAME here.  */
  283.   gdb_assert (prefix_len + 2 <= strlen (name));
  284.   gdb_assert (name[prefix_len + 1] == ':');

  285.   /* Find the name of the class and the name of the method, variable, etc.  */

  286.   /* The class name is everything up to and including PREFIX_LEN.  */
  287.   klass = savestring (name, prefix_len);

  288.   /* The rest of the name is everything else past the initial scope
  289.      operator.  */
  290.   nested = xstrdup (name + prefix_len + 2);

  291.   /* Add cleanups to free memory for these strings.  */
  292.   cleanup = make_cleanup (xfree, klass);
  293.   make_cleanup (xfree, nested);

  294.   /* Lookup a class named KLASS.  If none is found, there is nothing
  295.      more that can be done.  */
  296.   klass_sym = lookup_global_symbol (klass, block, domain);
  297.   if (klass_sym == NULL)
  298.     {
  299.       do_cleanups (cleanup);
  300.       return NULL;
  301.     }
  302.   klass_type = SYMBOL_TYPE (klass_sym);

  303.   /* Look for a symbol named NESTED in this class.
  304.      The caller is assumed to have already have done a basic lookup of NAME.
  305.      So we pass zero for BASIC_LOOKUP to cp_lookup_nested_symbol_1 here.  */
  306.   sym = cp_lookup_nested_symbol_1 (klass_type, nested, name, block, 0);

  307.   do_cleanups (cleanup);
  308.   return sym;
  309. }

  310. /* Look up NAME in the C++ namespace NAMESPACE.  Other arguments are
  311.    as in cp_lookup_symbol_nonlocal.  If SEARCH is non-zero, search
  312.    through base classes for a matching symbol.

  313.    Note: Part of the complexity is because NAME may itself specify scope.
  314.    Part of the complexity is also because this handles the case where
  315.    there is no scoping in which case we also try looking in the class of
  316.    "this" if we can compute it.  */

  317. static struct symbol *
  318. cp_lookup_symbol_in_namespace (const char *namespace, const char *name,
  319.                                const struct block *block,
  320.                                const domain_enum domain, int search)
  321. {
  322.   char *concatenated_name = NULL;
  323.   int is_in_anonymous;
  324.   unsigned int prefix_len;
  325.   struct symbol *sym;

  326.   if (namespace[0] != '\0')
  327.     {
  328.       concatenated_name = alloca (strlen (namespace) + 2
  329.                                   + strlen (name) + 1);
  330.       strcpy (concatenated_name, namespace);
  331.       strcat (concatenated_name, "::");
  332.       strcat (concatenated_name, name);
  333.       name = concatenated_name;
  334.     }

  335.   prefix_len = cp_entire_prefix_len (name);
  336.   if (prefix_len == 0)
  337.     return cp_lookup_bare_symbol (NULL, name, block, domain, search);

  338.   /* This would be simpler if we just called cp_lookup_nested_symbol
  339.      at this point.  But that would require first looking up the containing
  340.      class/namespace.  Since we're only searching static and global blocks
  341.      there's often no need to first do that lookup.  */

  342.   is_in_anonymous = namespace[0] != '\0' && cp_is_in_anonymous (namespace);
  343.   sym = cp_basic_lookup_symbol (name, block, domain, is_in_anonymous);
  344.   if (sym != NULL)
  345.     return sym;

  346.   if (search)
  347.     sym = cp_search_static_and_baseclasses (name, block, domain, prefix_len);

  348.   return sym;
  349. }

  350. /* Used for cleanups to reset the "searched" flag incase
  351.    of an error.  */

  352. static void
  353. reset_directive_searched (void *data)
  354. {
  355.   struct using_direct *direct = data;
  356.   direct->searched = 0;
  357. }

  358. /* Search for NAME by applying all import statements belonging to
  359.    BLOCK which are applicable in SCOPE.  If DECLARATION_ONLY the
  360.    search is restricted to using declarations.
  361.    Example:

  362.      namespace A {
  363.        int x;
  364.      }
  365.      using A::x;

  366.    If SEARCH_PARENTS the search will include imports which are
  367.    applicable in parents of SCOPE.
  368.    Example:

  369.      namespace A {
  370.        using namespace X;
  371.        namespace B {
  372.          using namespace Y;
  373.        }
  374.      }

  375.    If SCOPE is "A::B" and SEARCH_PARENTS is true the imports of
  376.    namespaces X and Y will be considered.  If SEARCH_PARENTS is false
  377.    only the import of Y is considered.

  378.    SEARCH_SCOPE_FIRST is an internal implementation detail: Callers must
  379.    pass 0 for it.  Internally we pass 1 when recursing.  */

  380. static struct symbol *
  381. cp_lookup_symbol_via_imports (const char *scope,
  382.                               const char *name,
  383.                               const struct block *block,
  384.                               const domain_enum domain,
  385.                               const int search_scope_first,
  386.                               const int declaration_only,
  387.                               const int search_parents)
  388. {
  389.   struct using_direct *current;
  390.   struct symbol *sym = NULL;
  391.   int len;
  392.   int directive_match;
  393.   struct cleanup *searched_cleanup;

  394.   /* First, try to find the symbol in the given namespace if requested.  */
  395.   if (search_scope_first)
  396.     sym = cp_lookup_symbol_in_namespace (scope, name,
  397.                                          block, domain, 1);

  398.   if (sym != NULL)
  399.     return sym;

  400.   /* Go through the using directives.  If any of them add new names to
  401.      the namespace we're searching in, see if we can find a match by
  402.      applying them.  */

  403.   for (current = block_using (block);
  404.        current != NULL;
  405.        current = current->next)
  406.     {
  407.       const char **excludep;

  408.       len = strlen (current->import_dest);
  409.       directive_match = (search_parents
  410.                          ? (strncmp (scope, current->import_dest,
  411.                                      strlen (current->import_dest)) == 0
  412.                             && (len == 0
  413.                                 || scope[len] == ':'
  414.                                 || scope[len] == '\0'))
  415.                          : strcmp (scope, current->import_dest) == 0);

  416.       /* If the import destination is the current scope or one of its
  417.          ancestors then it is applicable.  */
  418.       if (directive_match && !current->searched)
  419.         {
  420.           /* Mark this import as searched so that the recursive call
  421.              does not search it again.  */
  422.           current->searched = 1;
  423.           searched_cleanup = make_cleanup (reset_directive_searched,
  424.                                            current);

  425.           /* If there is an import of a single declaration, compare the
  426.              imported declaration (after optional renaming by its alias)
  427.              with the sought out name.  If there is a match pass
  428.              current->import_src as NAMESPACE to direct the search
  429.              towards the imported namespace.  */
  430.           if (current->declaration
  431.               && strcmp (name, current->alias
  432.                          ? current->alias : current->declaration) == 0)
  433.             sym = cp_lookup_symbol_in_namespace (current->import_src,
  434.                                                  current->declaration,
  435.                                                  block, domain, 1);

  436.           /* If this is a DECLARATION_ONLY search or a symbol was found
  437.              or this import statement was an import declaration, the
  438.              search of this import is complete.  */
  439.           if (declaration_only || sym != NULL || current->declaration)
  440.             {
  441.               current->searched = 0;
  442.               discard_cleanups (searched_cleanup);

  443.               if (sym != NULL)
  444.                 return sym;

  445.               continue;
  446.             }

  447.           /* Do not follow CURRENT if NAME matches its EXCLUDES.  */
  448.           for (excludep = current->excludes; *excludep; excludep++)
  449.             if (strcmp (name, *excludep) == 0)
  450.               break;
  451.           if (*excludep)
  452.             {
  453.               discard_cleanups (searched_cleanup);
  454.               continue;
  455.             }

  456.           if (current->alias != NULL
  457.               && strcmp (name, current->alias) == 0)
  458.             /* If the import is creating an alias and the alias matches
  459.                the sought name.  Pass current->import_src as the NAME to
  460.                direct the search towards the aliased namespace.  */
  461.             {
  462.               sym = cp_lookup_symbol_in_namespace (scope,
  463.                                                    current->import_src,
  464.                                                    block, domain, 1);
  465.             }
  466.           else if (current->alias == NULL)
  467.             {
  468.               /* If this import statement creates no alias, pass
  469.                  current->inner as NAMESPACE to direct the search
  470.                  towards the imported namespace.  */
  471.               sym = cp_lookup_symbol_via_imports (current->import_src,
  472.                                                   name, block,
  473.                                                   domain, 1, 0, 0);
  474.             }
  475.           current->searched = 0;
  476.           discard_cleanups (searched_cleanup);

  477.           if (sym != NULL)
  478.             return sym;
  479.         }
  480.     }

  481.   return NULL;
  482. }

  483. /* Helper function that searches an array of symbols for one named
  484.    NAME.  */

  485. static struct symbol *
  486. search_symbol_list (const char *name, int num,
  487.                     struct symbol **syms)
  488. {
  489.   int i;

  490.   /* Maybe we should store a dictionary in here instead.  */
  491.   for (i = 0; i < num; ++i)
  492.     {
  493.       if (strcmp (name, SYMBOL_NATURAL_NAME (syms[i])) == 0)
  494.         return syms[i];
  495.     }
  496.   return NULL;
  497. }

  498. /* Like cp_lookup_symbol_via_imports, but if BLOCK is a function, it
  499.    searches through the template parameters of the function and the
  500.    function's type.  */

  501. struct symbol *
  502. cp_lookup_symbol_imports_or_template (const char *scope,
  503.                                       const char *name,
  504.                                       const struct block *block,
  505.                                       const domain_enum domain)
  506. {
  507.   struct symbol *function = BLOCK_FUNCTION (block);
  508.   struct symbol *result;

  509.   if (symbol_lookup_debug)
  510.     {
  511.       fprintf_unfiltered (gdb_stdlog,
  512.                           "cp_lookup_symbol_imports_or_template"
  513.                           " (%s, %s, %s, %s)\n",
  514.                           scope, name, host_address_to_string (block),
  515.                           domain_name (domain));
  516.     }

  517.   if (function != NULL && SYMBOL_LANGUAGE (function) == language_cplus)
  518.     {
  519.       /* Search the function's template parameters.  */
  520.       if (SYMBOL_IS_CPLUS_TEMPLATE_FUNCTION (function))
  521.         {
  522.           struct template_symbol *templ
  523.             = (struct template_symbol *) function;

  524.           result = search_symbol_list (name,
  525.                                        templ->n_template_arguments,
  526.                                        templ->template_arguments);
  527.           if (result != NULL)
  528.             {
  529.               if (symbol_lookup_debug)
  530.                 {
  531.                   fprintf_unfiltered (gdb_stdlog,
  532.                                       "cp_lookup_symbol_imports_or_template"
  533.                                       " (...) = %s\n",
  534.                                       host_address_to_string (result));
  535.                 }
  536.               return result;
  537.             }
  538.         }

  539.       /* Search the template parameters of the function's defining
  540.          context.  */
  541.       if (SYMBOL_NATURAL_NAME (function))
  542.         {
  543.           struct type *context;
  544.           char *name_copy = xstrdup (SYMBOL_NATURAL_NAME (function));
  545.           struct cleanup *cleanups = make_cleanup (xfree, name_copy);
  546.           const struct language_defn *lang = language_def (language_cplus);
  547.           struct gdbarch *arch = symbol_arch (function);
  548.           const struct block *parent = BLOCK_SUPERBLOCK (block);

  549.           while (1)
  550.             {
  551.               unsigned int prefix_len = cp_entire_prefix_len (name_copy);

  552.               if (prefix_len == 0)
  553.                 context = NULL;
  554.               else
  555.                 {
  556.                   name_copy[prefix_len] = '\0';
  557.                   context = lookup_typename (lang, arch,
  558.                                              name_copy,
  559.                                              parent, 1);
  560.                 }

  561.               if (context == NULL)
  562.                 break;

  563.               result
  564.                 = search_symbol_list (name,
  565.                                       TYPE_N_TEMPLATE_ARGUMENTS (context),
  566.                                       TYPE_TEMPLATE_ARGUMENTS (context));
  567.               if (result != NULL)
  568.                 {
  569.                   do_cleanups (cleanups);
  570.                   if (symbol_lookup_debug)
  571.                     {
  572.                       fprintf_unfiltered (gdb_stdlog,
  573.                                           "cp_lookup_symbol_imports_or_template"
  574.                                           " (...) = %s\n",
  575.                                           host_address_to_string (result));
  576.                     }
  577.                   return result;
  578.                 }
  579.             }

  580.           do_cleanups (cleanups);
  581.         }
  582.     }

  583.   result = cp_lookup_symbol_via_imports (scope, name, block, domain, 0, 1, 1);
  584.   if (symbol_lookup_debug)
  585.     {
  586.       fprintf_unfiltered (gdb_stdlog,
  587.                           "cp_lookup_symbol_imports_or_template (...) = %s\n",
  588.                           result != NULL
  589.                           ? host_address_to_string (result) : "NULL");
  590.     }
  591.   return result;
  592. }

  593. /* Search for NAME by applying relevant import statements belonging to BLOCK
  594.    and its parents.  SCOPE is the namespace scope of the context in which the
  595.    search is being evaluated.  */

  596. static struct symbol *
  597. cp_lookup_symbol_via_all_imports (const char *scope, const char *name,
  598.                                   const struct block *block,
  599.                                   const domain_enum domain)
  600. {
  601.   struct symbol *sym;

  602.   while (block != NULL)
  603.     {
  604.       sym = cp_lookup_symbol_via_imports (scope, name, block, domain, 0, 0, 1);
  605.       if (sym)
  606.         return sym;

  607.       block = BLOCK_SUPERBLOCK (block);
  608.     }

  609.   return NULL;
  610. }

  611. /* Searches for NAME in the current namespace, and by applying
  612.    relevant import statements belonging to BLOCK and its parents.
  613.    SCOPE is the namespace scope of the context in which the search is
  614.    being evaluated.  */

  615. struct symbol *
  616. cp_lookup_symbol_namespace (const char *scope,
  617.                             const char *name,
  618.                             const struct block *block,
  619.                             const domain_enum domain)
  620. {
  621.   struct symbol *sym;

  622.   if (symbol_lookup_debug)
  623.     {
  624.       fprintf_unfiltered (gdb_stdlog,
  625.                           "cp_lookup_symbol_namespace (%s, %s, %s, %s)\n",
  626.                           scope, name, host_address_to_string (block),
  627.                           domain_name (domain));
  628.     }

  629.   /* First, try to find the symbol in the given namespace.  */
  630.   sym = cp_lookup_symbol_in_namespace (scope, name, block, domain, 1);

  631.   /* Search for name in namespaces imported to this and parent blocks.  */
  632.   if (sym == NULL)
  633.     sym = cp_lookup_symbol_via_all_imports (scope, name, block, domain);

  634.   if (symbol_lookup_debug)
  635.     {
  636.       fprintf_unfiltered (gdb_stdlog,
  637.                           "cp_lookup_symbol_namespace (...) = %s\n",
  638.                           sym != NULL ? host_address_to_string (sym) : "NULL");
  639.     }
  640.   return sym;
  641. }

  642. /* Lookup NAME at namespace scope (or, in C terms, in static and
  643.    global variables).  SCOPE is the namespace that the current
  644.    function is defined within; only consider namespaces whose length
  645.    is at least SCOPE_LEN.  Other arguments are as in
  646.    cp_lookup_symbol_nonlocal.

  647.    For example, if we're within a function A::B::f and looking for a
  648.    symbol x, this will get called with NAME = "x", SCOPE = "A::B", and
  649.    SCOPE_LEN = 0.  It then calls itself with NAME and SCOPE the same,
  650.    but with SCOPE_LEN = 1.  And then it calls itself with NAME and
  651.    SCOPE the same, but with SCOPE_LEN = 4.  This third call looks for
  652.    "A::B::x"; if it doesn't find it, then the second call looks for
  653.    "A::x", and if that call fails, then the first call looks for
  654.    "x".  */

  655. static struct symbol *
  656. lookup_namespace_scope (const struct language_defn *langdef,
  657.                         const char *name,
  658.                         const struct block *block,
  659.                         const domain_enum domain,
  660.                         const char *scope,
  661.                         int scope_len)
  662. {
  663.   char *namespace;

  664.   if (scope[scope_len] != '\0')
  665.     {
  666.       /* Recursively search for names in child namespaces first.  */

  667.       struct symbol *sym;
  668.       int new_scope_len = scope_len;

  669.       /* If the current scope is followed by "::", skip past that.  */
  670.       if (new_scope_len != 0)
  671.         {
  672.           gdb_assert (scope[new_scope_len] == ':');
  673.           new_scope_len += 2;
  674.         }
  675.       new_scope_len += cp_find_first_component (scope + new_scope_len);
  676.       sym = lookup_namespace_scope (langdef, name, block, domain,
  677.                                     scope, new_scope_len);
  678.       if (sym != NULL)
  679.         return sym;
  680.     }

  681.   /* Okay, we didn't find a match in our children, so look for the
  682.      name in the current namespace.

  683.      If we there is no scope and we know we have a bare symbol, then short
  684.      circuit everything and call cp_lookup_bare_symbol directly.
  685.      This isn't an optimization, rather it allows us to pass LANGDEF which
  686.      is needed for primitive type lookup.  The test doesn't have to be
  687.      perfect: if NAME is a bare symbol that our test doesn't catch (e.g., a
  688.      template symbol with "::" in the argument list) then
  689.      cp_lookup_symbol_in_namespace will catch it.  */

  690.   if (scope_len == 0 && strchr (name, ':') == NULL)
  691.     return cp_lookup_bare_symbol (langdef, name, block, domain, 1);

  692.   namespace = alloca (scope_len + 1);
  693.   strncpy (namespace, scope, scope_len);
  694.   namespace[scope_len] = '\0';
  695.   return cp_lookup_symbol_in_namespace (namespace, name,
  696.                                         block, domain, 1);
  697. }

  698. /* The C++-specific version of name lookup for static and global
  699.    names.  This makes sure that names get looked for in all namespaces
  700.    that are in scope.  NAME is the natural name of the symbol that
  701.    we're looking for, BLOCK is the block that we're searching within,
  702.    DOMAIN says what kind of symbols we're looking for.  */

  703. struct symbol *
  704. cp_lookup_symbol_nonlocal (const struct language_defn *langdef,
  705.                            const char *name,
  706.                            const struct block *block,
  707.                            const domain_enum domain)
  708. {
  709.   struct symbol *sym;
  710.   const char *scope = block_scope (block);

  711.   if (symbol_lookup_debug)
  712.     {
  713.       fprintf_unfiltered (gdb_stdlog,
  714.                           "cp_lookup_symbol_non_local"
  715.                           " (%s, %s (scope %s), %s)\n",
  716.                           name, host_address_to_string (block), scope,
  717.                           domain_name (domain));
  718.     }

  719.   /* First, try to find the symbol in the given namespace, and all
  720.      containing namespaces.  */
  721.   sym = lookup_namespace_scope (langdef, name, block, domain, scope, 0);

  722.   /* Search for name in namespaces imported to this and parent blocks.  */
  723.   if (sym == NULL)
  724.     sym = cp_lookup_symbol_via_all_imports (scope, name, block, domain);

  725.   if (symbol_lookup_debug)
  726.     {
  727.       fprintf_unfiltered (gdb_stdlog,
  728.                           "cp_lookup_symbol_nonlocal (...) = %s\n",
  729.                           sym != NULL ? host_address_to_string (sym) : "NULL");
  730.     }
  731.   return sym;
  732. }

  733. /* Search through the base classes of PARENT_TYPE for a base class
  734.    named NAME and return its type.  If not found, return NULL.  */

  735. struct type *
  736. cp_find_type_baseclass_by_name (struct type *parent_type, const char *name)
  737. {
  738.   int i;

  739.   CHECK_TYPEDEF (parent_type);
  740.   for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i)
  741.     {
  742.       struct type *type = check_typedef (TYPE_BASECLASS (parent_type, i));
  743.       const char *base_name = TYPE_BASECLASS_NAME (parent_type, i);

  744.       if (base_name == NULL)
  745.         continue;

  746.       if (streq (base_name, name))
  747.         return type;

  748.       type = cp_find_type_baseclass_by_name (type, name);
  749.       if (type != NULL)
  750.         return type;
  751.     }

  752.   return NULL;
  753. }

  754. /* Search through the base classes of PARENT_TYPE for a symbol named
  755.    NAME in block BLOCK.  */

  756. static struct symbol *
  757. find_symbol_in_baseclass (struct type *parent_type, const char *name,
  758.                            const struct block *block)
  759. {
  760.   int i;
  761.   struct symbol *sym;
  762.   struct cleanup *cleanup;
  763.   char *concatenated_name;

  764.   sym = NULL;
  765.   concatenated_name = NULL;
  766.   cleanup = make_cleanup (free_current_contents, &concatenated_name);

  767.   for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i)
  768.     {
  769.       size_t len;
  770.       struct type *base_type = TYPE_BASECLASS (parent_type, i);
  771.       const char *base_name = TYPE_BASECLASS_NAME (parent_type, i);

  772.       if (base_name == NULL)
  773.         continue;

  774.       len = strlen (base_name) + 2 + strlen (name) + 1;
  775.       concatenated_name = xrealloc (concatenated_name, len);
  776.       xsnprintf (concatenated_name, len, "%s::%s", base_name, name);

  777.       sym = cp_lookup_nested_symbol_1 (base_type, name, concatenated_name,
  778.                                        block, 1);
  779.       if (sym != NULL)
  780.         break;
  781.     }

  782.   do_cleanups (cleanup);
  783.   return sym;
  784. }

  785. /* Helper function to look up NESTED_NAME in CONTAINER_TYPE within the
  786.    context of BLOCK.
  787.    CONTAINER_TYPE needn't have been "check_typedef'd" yet.
  788.    CONCATENATED_NAME is the fully scoped spelling of NESTED_NAME, it is
  789.    passed as an argument so that callers can control how space for it is
  790.    allocated.
  791.    If BASIC_LOOKUP is non-zero then perform a basic lookup of
  792.    CONCATENATED_NAME.  See cp_basic_lookup_symbol for details.  */

  793. static struct symbol *
  794. cp_lookup_nested_symbol_1 (struct type *container_type,
  795.                            const char *nested_name,
  796.                            const char *concatenated_name,
  797.                            const struct block *block,
  798.                            int basic_lookup)
  799. {
  800.   int is_in_anonymous = cp_is_in_anonymous (concatenated_name);
  801.   struct symbol *sym;

  802.   /* NOTE: carlton/2003-11-10: We don't treat C++ class members
  803.      of classes like, say, data or function members.  Instead,
  804.      they're just represented by symbols whose names are
  805.      qualified by the name of the surrounding class.  This is
  806.      just like members of namespaces; in particular,
  807.      cp_basic_lookup_symbol works when looking them up.  */

  808.   if (basic_lookup)
  809.     {
  810.       sym = cp_basic_lookup_symbol (concatenated_name, block, VAR_DOMAIN,
  811.                                     is_in_anonymous);
  812.       if (sym != NULL)
  813.         return sym;
  814.     }

  815.   /* Now search all static file-level symbols.  We have to do this for things
  816.      like typedefs in the class.  We do not try to guess any imported
  817.      namespace as even the fully specified namespace search is already not
  818.      C++ compliant and more assumptions could make it too magic.  */

  819.   /* First search in this symtab, what we want is possibly there.  */
  820.   sym = lookup_symbol_in_static_block (concatenated_name, block, VAR_DOMAIN);
  821.   if (sym != NULL)
  822.     return sym;

  823.   /* Nope.  We now have to search all static blocks in all objfiles,
  824.      even if block != NULL, because there's no guarantees as to which
  825.      symtab the symbol we want is in.  */
  826.   sym = lookup_static_symbol (concatenated_name, VAR_DOMAIN);
  827.   if (sym != NULL)
  828.     return sym;

  829.   /* If this is a class with baseclasses, search them next.  */
  830.   CHECK_TYPEDEF (container_type);
  831.   if (TYPE_N_BASECLASSES (container_type) > 0)
  832.     {
  833.       sym = find_symbol_in_baseclass (container_type, nested_name, block);
  834.       if (sym != NULL)
  835.         return sym;
  836.     }

  837.   return NULL;
  838. }

  839. /* Look up a symbol named NESTED_NAME that is nested inside the C++
  840.    class or namespace given by PARENT_TYPE, from within the context
  841.    given by BLOCK.  Return NULL if there is no such nested symbol.  */

  842. struct symbol *
  843. cp_lookup_nested_symbol (struct type *parent_type,
  844.                          const char *nested_name,
  845.                          const struct block *block)
  846. {
  847.   /* type_name_no_tag_or_error provides better error reporting using the
  848.      original type.  */
  849.   struct type *saved_parent_type = parent_type;

  850.   CHECK_TYPEDEF (parent_type);

  851.   if (symbol_lookup_debug)
  852.     {
  853.       const char *type_name = type_name_no_tag (saved_parent_type);

  854.       fprintf_unfiltered (gdb_stdlog,
  855.                           "cp_lookup_nested_symbol (%s, %s, %s)\n",
  856.                           type_name != NULL ? type_name : "unnamed",
  857.                           nested_name, host_address_to_string (block));
  858.     }

  859.   switch (TYPE_CODE (parent_type))
  860.     {
  861.     case TYPE_CODE_STRUCT:
  862.     case TYPE_CODE_NAMESPACE:
  863.     case TYPE_CODE_UNION:
  864.     case TYPE_CODE_ENUM:
  865.     /* NOTE: Handle modules here as well, because Fortran is re-using the C++
  866.        specific code to lookup nested symbols in modules, by calling the
  867.        function pointer la_lookup_symbol_nonlocal, which ends up here.  */
  868.     case TYPE_CODE_MODULE:
  869.       {
  870.         int size;
  871.         const char *parent_name = type_name_no_tag_or_error (saved_parent_type);
  872.         struct symbol *sym;
  873.         char *concatenated_name;

  874.         size = strlen (parent_name) + 2 + strlen (nested_name) + 1;
  875.         concatenated_name = alloca (size);
  876.         xsnprintf (concatenated_name, size, "%s::%s",
  877.                    parent_name, nested_name);

  878.         sym = cp_lookup_nested_symbol_1 (parent_type, nested_name,
  879.                                          concatenated_name, block, 1);

  880.         if (symbol_lookup_debug)
  881.           {
  882.             fprintf_unfiltered (gdb_stdlog,
  883.                                 "cp_lookup_nested_symbol (...) = %s\n",
  884.                                 sym != NULL
  885.                                 ? host_address_to_string (sym) : "NULL");
  886.           }
  887.         return sym;
  888.       }

  889.     case TYPE_CODE_FUNC:
  890.     case TYPE_CODE_METHOD:
  891.       if (symbol_lookup_debug)
  892.         {
  893.           fprintf_unfiltered (gdb_stdlog,
  894.                               "cp_lookup_nested_symbol (...) = NULL"
  895.                               " (func/method)\n");
  896.         }
  897.       return NULL;

  898.     default:
  899.       internal_error (__FILE__, __LINE__,
  900.                       _("cp_lookup_nested_symbol called "
  901.                         "on a non-aggregate type."));
  902.     }
  903. }

  904. /* The C++-version of lookup_transparent_type.  */

  905. /* FIXME: carlton/2004-01-16: The problem that this is trying to
  906.    address is that, unfortunately, sometimes NAME is wrong: it may not
  907.    include the name of namespaces enclosing the type in question.
  908.    lookup_transparent_type gets called when the type in question
  909.    is a declaration, and we're trying to find its definition; but, for
  910.    declarations, our type name deduction mechanism doesn't work.
  911.    There's nothing we can do to fix this in general, I think, in the
  912.    absence of debug information about namespaces (I've filed PR
  913.    gdb/1511 about this); until such debug information becomes more
  914.    prevalent, one heuristic which sometimes looks is to search for the
  915.    definition in namespaces containing the current namespace.

  916.    We should delete this functions once the appropriate debug
  917.    information becomes more widespread.  (GCC 3.4 will be the first
  918.    released version of GCC with such information.)  */

  919. struct type *
  920. cp_lookup_transparent_type (const char *name)
  921. {
  922.   /* First, try the honest way of looking up the definition.  */
  923.   struct type *t = basic_lookup_transparent_type (name);
  924.   const char *scope;

  925.   if (t != NULL)
  926.     return t;

  927.   /* If that doesn't work and we're within a namespace, look there
  928.      instead.  */
  929.   scope = block_scope (get_selected_block (0));

  930.   if (scope[0] == '\0')
  931.     return NULL;

  932.   return cp_lookup_transparent_type_loop (name, scope, 0);
  933. }

  934. /* Lookup the type definition associated to NAME in namespaces/classes
  935.    containing SCOPE whose name is strictly longer than LENGTH.  LENGTH
  936.    must be the index of the start of a component of SCOPE.  */

  937. static struct type *
  938. cp_lookup_transparent_type_loop (const char *name,
  939.                                  const char *scope,
  940.                                  int length)
  941. {
  942.   int scope_length = length + cp_find_first_component (scope + length);
  943.   char *full_name;

  944.   /* If the current scope is followed by "::", look in the next
  945.      component.  */
  946.   if (scope[scope_length] == ':')
  947.     {
  948.       struct type *retval
  949.         = cp_lookup_transparent_type_loop (name, scope,
  950.                                            scope_length + 2);

  951.       if (retval != NULL)
  952.         return retval;
  953.     }

  954.   full_name = alloca (scope_length + 2 + strlen (name) + 1);
  955.   strncpy (full_name, scope, scope_length);
  956.   strncpy (full_name + scope_length, "::", 2);
  957.   strcpy (full_name + scope_length + 2, name);

  958.   return basic_lookup_transparent_type (full_name);
  959. }

  960. /* This used to do something but was removed when it became
  961.    obsolete.  */

  962. static void
  963. maintenance_cplus_namespace (char *args, int from_tty)
  964. {
  965.   printf_unfiltered (_("The `maint namespace' command was removed.\n"));
  966. }

  967. /* Provide a prototype to silence -Wmissing-prototypes.  */
  968. extern initialize_file_ftype _initialize_cp_namespace;

  969. void
  970. _initialize_cp_namespace (void)
  971. {
  972.   struct cmd_list_element *cmd;

  973.   cmd = add_cmd ("namespace", class_maintenance,
  974.                  maintenance_cplus_namespace,
  975.                  _("Deprecated placeholder for removed functionality."),
  976.                  &maint_cplus_cmd_list);
  977.   deprecate_cmd (cmd, NULL);
  978. }