gdb/cp-support.c - gdb

Global variables defined

Functions defined

Macros defined

Source code

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

  3.    Contributed by MontaVista Software.

  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 "demangle.h"
  18. #include "gdbcmd.h"
  19. #include "dictionary.h"
  20. #include "objfiles.h"
  21. #include "frame.h"
  22. #include "symtab.h"
  23. #include "block.h"
  24. #include "complaints.h"
  25. #include "gdbtypes.h"
  26. #include "expression.h"
  27. #include "value.h"
  28. #include "cp-abi.h"
  29. #include <signal.h>

  30. #include "safe-ctype.h"

  31. #define d_left(dc) (dc)->u.s_binary.left
  32. #define d_right(dc) (dc)->u.s_binary.right

  33. /* Functions related to demangled name parsing.  */

  34. static unsigned int cp_find_first_component_aux (const char *name,
  35.                                                  int permissive);

  36. static void demangled_name_complaint (const char *name);

  37. /* Functions/variables related to overload resolution.  */

  38. static int sym_return_val_size = -1;
  39. static int sym_return_val_index;
  40. static struct symbol **sym_return_val;

  41. static void overload_list_add_symbol (struct symbol *sym,
  42.                                       const char *oload_name);

  43. static void make_symbol_overload_list_using (const char *func_name,
  44.                                              const char *namespace);

  45. static void make_symbol_overload_list_qualified (const char *func_name);

  46. /* The list of "maint cplus" commands.  */

  47. struct cmd_list_element *maint_cplus_cmd_list = NULL;

  48. /* The actual commands.  */

  49. static void maint_cplus_command (char *arg, int from_tty);
  50. static void first_component_command (char *arg, int from_tty);

  51. /* A list of typedefs which should not be substituted by replace_typedefs.  */
  52. static const char * const ignore_typedefs[] =
  53.   {
  54.     "std::istream", "std::iostream", "std::ostream", "std::string"
  55.   };

  56. static void
  57.   replace_typedefs (struct demangle_parse_info *info,
  58.                     struct demangle_component *ret_comp,
  59.                     canonicalization_ftype *finder,
  60.                     void *data);

  61. /* A convenience function to copy STRING into OBSTACK, returning a pointer
  62.    to the newly allocated string and saving the number of bytes saved in LEN.

  63.    It does not copy the terminating '\0' byte!  */

  64. static char *
  65. copy_string_to_obstack (struct obstack *obstack, const char *string,
  66.                         long *len)
  67. {
  68.   *len = strlen (string);
  69.   return obstack_copy (obstack, string, *len);
  70. }

  71. /* A cleanup wrapper for cp_demangled_name_parse_free.  */

  72. static void
  73. do_demangled_name_parse_free_cleanup (void *data)
  74. {
  75.   struct demangle_parse_info *info = (struct demangle_parse_info *) data;

  76.   cp_demangled_name_parse_free (info);
  77. }

  78. /* Create a cleanup for C++ name parsing.  */

  79. struct cleanup *
  80. make_cleanup_cp_demangled_name_parse_free (struct demangle_parse_info *info)
  81. {
  82.   return make_cleanup (do_demangled_name_parse_free_cleanup, info);
  83. }

  84. /* Return 1 if STRING is clearly already in canonical form.  This
  85.    function is conservative; things which it does not recognize are
  86.    assumed to be non-canonical, and the parser will sort them out
  87.    afterwards.  This speeds up the critical path for alphanumeric
  88.    identifiers.  */

  89. static int
  90. cp_already_canonical (const char *string)
  91. {
  92.   /* Identifier start character [a-zA-Z_].  */
  93.   if (!ISIDST (string[0]))
  94.     return 0;

  95.   /* These are the only two identifiers which canonicalize to other
  96.      than themselves or an error: unsigned -> unsigned int and
  97.      signed -> int.  */
  98.   if (string[0] == 'u' && strcmp (&string[1], "nsigned") == 0)
  99.     return 0;
  100.   else if (string[0] == 's' && strcmp (&string[1], "igned") == 0)
  101.     return 0;

  102.   /* Identifier character [a-zA-Z0-9_].  */
  103.   while (ISIDNUM (string[1]))
  104.     string++;

  105.   if (string[1] == '\0')
  106.     return 1;
  107.   else
  108.     return 0;
  109. }

  110. /* Inspect the given RET_COMP for its type.  If it is a typedef,
  111.    replace the node with the typedef's tree.

  112.    Returns 1 if any typedef substitutions were made, 0 otherwise.  */

  113. static int
  114. inspect_type (struct demangle_parse_info *info,
  115.               struct demangle_component *ret_comp,
  116.               canonicalization_ftype *finder,
  117.               void *data)
  118. {
  119.   int i;
  120.   char *name;
  121.   struct symbol *sym;
  122.   volatile struct gdb_exception except;

  123.   /* Copy the symbol's name from RET_COMP and look it up
  124.      in the symbol table.  */
  125.   name = (char *) alloca (ret_comp->u.s_name.len + 1);
  126.   memcpy (name, ret_comp->u.s_name.s, ret_comp->u.s_name.len);
  127.   name[ret_comp->u.s_name.len] = '\0';

  128.   /* Ignore any typedefs that should not be substituted.  */
  129.   for (i = 0; i < ARRAY_SIZE (ignore_typedefs); ++i)
  130.     {
  131.       if (strcmp (name, ignore_typedefs[i]) == 0)
  132.         return 0;
  133.     }

  134.   sym = NULL;
  135.   TRY_CATCH (except, RETURN_MASK_ALL)
  136.   {
  137.     sym = lookup_symbol (name, 0, VAR_DOMAIN, 0);
  138.   }

  139.   if (except.reason >= 0 && sym != NULL)
  140.     {
  141.       struct type *otype = SYMBOL_TYPE (sym);

  142.       if (finder != NULL)
  143.         {
  144.           const char *new_name = (*finder) (otype, data);

  145.           if (new_name != NULL)
  146.             {
  147.               ret_comp->u.s_name.s = new_name;
  148.               ret_comp->u.s_name.len = strlen (new_name);
  149.               return 1;
  150.             }

  151.           return 0;
  152.         }

  153.       /* If the type is a typedef or namespace alias, replace it.  */
  154.       if (TYPE_CODE (otype) == TYPE_CODE_TYPEDEF
  155.           || TYPE_CODE (otype) == TYPE_CODE_NAMESPACE)
  156.         {
  157.           long len;
  158.           int is_anon;
  159.           struct type *type;
  160.           struct demangle_parse_info *i;
  161.           struct ui_file *buf;

  162.           /* Get the real type of the typedef.  */
  163.           type = check_typedef (otype);

  164.           /* If the symbol is a namespace and its type name is no different
  165.              than the name we looked up, this symbol is not a namespace
  166.              alias and does not need to be substituted.  */
  167.           if (TYPE_CODE (otype) == TYPE_CODE_NAMESPACE
  168.               && strcmp (TYPE_NAME (type), name) == 0)
  169.             return 0;

  170.           is_anon = (TYPE_TAG_NAME (type) == NULL
  171.                      && (TYPE_CODE (type) == TYPE_CODE_ENUM
  172.                          || TYPE_CODE (type) == TYPE_CODE_STRUCT
  173.                          || TYPE_CODE (type) == TYPE_CODE_UNION));
  174.           if (is_anon)
  175.             {
  176.               struct type *last = otype;

  177.               /* Find the last typedef for the type.  */
  178.               while (TYPE_TARGET_TYPE (last) != NULL
  179.                      && (TYPE_CODE (TYPE_TARGET_TYPE (last))
  180.                          == TYPE_CODE_TYPEDEF))
  181.                 last = TYPE_TARGET_TYPE (last);

  182.               /* If there is only one typedef for this anonymous type,
  183.                  do not substitute it.  */
  184.               if (type == otype)
  185.                 return 0;
  186.               else
  187.                 /* Use the last typedef seen as the type for this
  188.                    anonymous type.  */
  189.                 type = last;
  190.             }

  191.           buf = mem_fileopen ();
  192.           TRY_CATCH (except, RETURN_MASK_ERROR)
  193.           {
  194.             type_print (type, "", buf, -1);
  195.           }

  196.           /* If type_print threw an exception, there is little point
  197.              in continuing, so just bow out gracefully.  */
  198.           if (except.reason < 0)
  199.             {
  200.               ui_file_delete (buf);
  201.               return 0;
  202.             }

  203.           name = ui_file_obsavestring (buf, &info->obstack, &len);
  204.           ui_file_delete (buf);

  205.           /* Turn the result into a new tree.  Note that this
  206.              tree will contain pointers into NAME, so NAME cannot
  207.              be free'd until all typedef conversion is done and
  208.              the final result is converted into a string.  */
  209.           i = cp_demangled_name_to_comp (name, NULL);
  210.           if (i != NULL)
  211.             {
  212.               /* Merge the two trees.  */
  213.               cp_merge_demangle_parse_infos (info, ret_comp, i);

  214.               /* Replace any newly introduced typedefs -- but not
  215.                  if the type is anonymous (that would lead to infinite
  216.                  looping).  */
  217.               if (!is_anon)
  218.                 replace_typedefs (info, ret_comp, finder, data);
  219.             }
  220.           else
  221.             {
  222.               /* This shouldn't happen unless the type printer has
  223.                  output something that the name parser cannot grok.
  224.                  Nonetheless, an ounce of prevention...

  225.                  Canonicalize the name again, and store it in the
  226.                  current node (RET_COMP).  */
  227.               char *canon = cp_canonicalize_string_no_typedefs (name);

  228.               if (canon != NULL)
  229.                 {
  230.                   /* Copy the canonicalization into the obstack and
  231.                      free CANON.  */
  232.                   name = copy_string_to_obstack (&info->obstack, canon, &len);
  233.                   xfree (canon);
  234.                 }

  235.               ret_comp->u.s_name.s = name;
  236.               ret_comp->u.s_name.len = len;
  237.             }

  238.           return 1;
  239.         }
  240.     }

  241.   return 0;
  242. }

  243. /* Replace any typedefs appearing in the qualified name
  244.    (DEMANGLE_COMPONENT_QUAL_NAME) represented in RET_COMP for the name parse
  245.    given in INFO.  */

  246. static void
  247. replace_typedefs_qualified_name (struct demangle_parse_info *info,
  248.                                  struct demangle_component *ret_comp,
  249.                                  canonicalization_ftype *finder,
  250.                                  void *data)
  251. {
  252.   long len;
  253.   char *name;
  254.   struct ui_file *buf = mem_fileopen ();
  255.   struct demangle_component *comp = ret_comp;

  256.   /* Walk each node of the qualified name, reconstructing the name of
  257.      this element.  With every node, check for any typedef substitutions.
  258.      If a substitution has occurred, replace the qualified name node
  259.      with a DEMANGLE_COMPONENT_NAME node representing the new, typedef-
  260.      substituted name.  */
  261.   while (comp->type == DEMANGLE_COMPONENT_QUAL_NAME)
  262.     {
  263.       if (d_left (comp)->type == DEMANGLE_COMPONENT_NAME)
  264.         {
  265.           struct demangle_component new;

  266.           ui_file_write (buf, d_left (comp)->u.s_name.s,
  267.                          d_left (comp)->u.s_name.len);
  268.           name = ui_file_obsavestring (buf, &info->obstack, &len);
  269.           new.type = DEMANGLE_COMPONENT_NAME;
  270.           new.u.s_name.s = name;
  271.           new.u.s_name.len = len;
  272.           if (inspect_type (info, &new, finder, data))
  273.             {
  274.               char *n, *s;
  275.               long slen;

  276.               /* A typedef was substituted in NEW.  Convert it to a
  277.                  string and replace the top DEMANGLE_COMPONENT_QUAL_NAME
  278.                  node.  */

  279.               ui_file_rewind (buf);
  280.               n = cp_comp_to_string (&new, 100);
  281.               if (n == NULL)
  282.                 {
  283.                   /* If something went astray, abort typedef substitutions.  */
  284.                   ui_file_delete (buf);
  285.                   return;
  286.                 }

  287.               s = copy_string_to_obstack (&info->obstack, n, &slen);
  288.               xfree (n);

  289.               d_left (ret_comp)->type = DEMANGLE_COMPONENT_NAME;
  290.               d_left (ret_comp)->u.s_name.s = s;
  291.               d_left (ret_comp)->u.s_name.len = slen;
  292.               d_right (ret_comp) = d_right (comp);
  293.               comp = ret_comp;
  294.               continue;
  295.             }
  296.         }
  297.       else
  298.         {
  299.           /* The current node is not a name, so simply replace any
  300.              typedefs in it.  Then print it to the stream to continue
  301.              checking for more typedefs in the tree.  */
  302.           replace_typedefs (info, d_left (comp), finder, data);
  303.           name = cp_comp_to_string (d_left (comp), 100);
  304.           if (name == NULL)
  305.             {
  306.               /* If something went astray, abort typedef substitutions.  */
  307.               ui_file_delete (buf);
  308.               return;
  309.             }
  310.           fputs_unfiltered (name, buf);
  311.           xfree (name);
  312.         }

  313.       ui_file_write (buf, "::", 2);
  314.       comp = d_right (comp);
  315.     }

  316.   /* If the next component is DEMANGLE_COMPONENT_NAME, save the qualified
  317.      name assembled above and append the name given by COMP.  Then use this
  318.      reassembled name to check for a typedef.  */

  319.   if (comp->type == DEMANGLE_COMPONENT_NAME)
  320.     {
  321.       ui_file_write (buf, comp->u.s_name.s, comp->u.s_name.len);
  322.       name = ui_file_obsavestring (buf, &info->obstack, &len);

  323.       /* Replace the top (DEMANGLE_COMPONENT_QUAL_NAME) node
  324.          with a DEMANGLE_COMPONENT_NAME node containing the whole
  325.          name.  */
  326.       ret_comp->type = DEMANGLE_COMPONENT_NAME;
  327.       ret_comp->u.s_name.s = name;
  328.       ret_comp->u.s_name.len = len;
  329.       inspect_type (info, ret_comp, finder, data);
  330.     }
  331.   else
  332.     replace_typedefs (info, comp, finder, data);

  333.   ui_file_delete (buf);
  334. }


  335. /* A function to check const and volatile qualifiers for argument types.

  336.    "Parameter declarations that differ only in the presence
  337.    or absence of `const' and/or `volatile' are equivalent."
  338.    C++ Standard N3290, clause 13.1.3 #4.  */

  339. static void
  340. check_cv_qualifiers (struct demangle_component *ret_comp)
  341. {
  342.   while (d_left (ret_comp) != NULL
  343.          && (d_left (ret_comp)->type == DEMANGLE_COMPONENT_CONST
  344.              || d_left (ret_comp)->type == DEMANGLE_COMPONENT_VOLATILE))
  345.     {
  346.       d_left (ret_comp) = d_left (d_left (ret_comp));
  347.     }
  348. }

  349. /* Walk the parse tree given by RET_COMP, replacing any typedefs with
  350.    their basic types.  */

  351. static void
  352. replace_typedefs (struct demangle_parse_info *info,
  353.                   struct demangle_component *ret_comp,
  354.                   canonicalization_ftype *finder,
  355.                   void *data)
  356. {
  357.   if (ret_comp)
  358.     {
  359.       if (finder != NULL
  360.           && (ret_comp->type == DEMANGLE_COMPONENT_NAME
  361.               || ret_comp->type == DEMANGLE_COMPONENT_QUAL_NAME
  362.               || ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE
  363.               || ret_comp->type == DEMANGLE_COMPONENT_BUILTIN_TYPE))
  364.         {
  365.           char *local_name = cp_comp_to_string (ret_comp, 10);

  366.           if (local_name != NULL)
  367.             {
  368.               struct symbol *sym;
  369.               volatile struct gdb_exception except;

  370.               sym = NULL;
  371.               TRY_CATCH (except, RETURN_MASK_ALL)
  372.                 {
  373.                   sym = lookup_symbol (local_name, 0, VAR_DOMAIN, 0);
  374.                 }
  375.               xfree (local_name);

  376.               if (except.reason >= 0 && sym != NULL)
  377.                 {
  378.                   struct type *otype = SYMBOL_TYPE (sym);
  379.                   const char *new_name = (*finder) (otype, data);

  380.                   if (new_name != NULL)
  381.                     {
  382.                       ret_comp->type = DEMANGLE_COMPONENT_NAME;
  383.                       ret_comp->u.s_name.s = new_name;
  384.                       ret_comp->u.s_name.len = strlen (new_name);
  385.                       return;
  386.                     }
  387.                 }
  388.             }
  389.         }

  390.       switch (ret_comp->type)
  391.         {
  392.         case DEMANGLE_COMPONENT_ARGLIST:
  393.           check_cv_qualifiers (ret_comp);
  394.           /* Fall through */

  395.         case DEMANGLE_COMPONENT_FUNCTION_TYPE:
  396.         case DEMANGLE_COMPONENT_TEMPLATE:
  397.         case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
  398.         case DEMANGLE_COMPONENT_TYPED_NAME:
  399.           replace_typedefs (info, d_left (ret_comp), finder, data);
  400.           replace_typedefs (info, d_right (ret_comp), finder, data);
  401.           break;

  402.         case DEMANGLE_COMPONENT_NAME:
  403.           inspect_type (info, ret_comp, finder, data);
  404.           break;

  405.         case DEMANGLE_COMPONENT_QUAL_NAME:
  406.           replace_typedefs_qualified_name (info, ret_comp, finder, data);
  407.           break;

  408.         case DEMANGLE_COMPONENT_LOCAL_NAME:
  409.         case DEMANGLE_COMPONENT_CTOR:
  410.         case DEMANGLE_COMPONENT_ARRAY_TYPE:
  411.         case DEMANGLE_COMPONENT_PTRMEM_TYPE:
  412.           replace_typedefs (info, d_right (ret_comp), finder, data);
  413.           break;

  414.         case DEMANGLE_COMPONENT_CONST:
  415.         case DEMANGLE_COMPONENT_RESTRICT:
  416.         case DEMANGLE_COMPONENT_VOLATILE:
  417.         case DEMANGLE_COMPONENT_VOLATILE_THIS:
  418.         case DEMANGLE_COMPONENT_CONST_THIS:
  419.         case DEMANGLE_COMPONENT_RESTRICT_THIS:
  420.         case DEMANGLE_COMPONENT_POINTER:
  421.         case DEMANGLE_COMPONENT_REFERENCE:
  422.           replace_typedefs (info, d_left (ret_comp), finder, data);
  423.           break;

  424.         default:
  425.           break;
  426.         }
  427.     }
  428. }

  429. /* Parse STRING and convert it to canonical form, resolving any typedefs.
  430.    If parsing fails, or if STRING is already canonical, return NULL.
  431.    Otherwise return the canonical form.  The return value is allocated via
  432.    xmalloc.  If FINDER is not NULL, then type components are passed to
  433.    FINDER to be looked up.  DATA is passed verbatim to FINDER.  */

  434. char *
  435. cp_canonicalize_string_full (const char *string,
  436.                              canonicalization_ftype *finder,
  437.                              void *data)
  438. {
  439.   char *ret;
  440.   unsigned int estimated_len;
  441.   struct demangle_parse_info *info;

  442.   ret = NULL;
  443.   estimated_len = strlen (string) * 2;
  444.   info = cp_demangled_name_to_comp (string, NULL);
  445.   if (info != NULL)
  446.     {
  447.       /* Replace all the typedefs in the tree.  */
  448.       replace_typedefs (info, info->tree, finder, data);

  449.       /* Convert the tree back into a string.  */
  450.       ret = cp_comp_to_string (info->tree, estimated_len);
  451.       gdb_assert (ret != NULL);

  452.       /* Free the parse information.  */
  453.       cp_demangled_name_parse_free (info);

  454.       /* Finally, compare the original string with the computed
  455.          name, returning NULL if they are the same.  */
  456.       if (strcmp (string, ret) == 0)
  457.         {
  458.           xfree (ret);
  459.           return NULL;
  460.         }
  461.     }

  462.   return ret;
  463. }

  464. /* Like cp_canonicalize_string_full, but always passes NULL for
  465.    FINDER.  */

  466. char *
  467. cp_canonicalize_string_no_typedefs (const char *string)
  468. {
  469.   return cp_canonicalize_string_full (string, NULL, NULL);
  470. }

  471. /* Parse STRING and convert it to canonical form.  If parsing fails,
  472.    or if STRING is already canonical, return NULL.  Otherwise return
  473.    the canonical form.  The return value is allocated via xmalloc.  */

  474. char *
  475. cp_canonicalize_string (const char *string)
  476. {
  477.   struct demangle_parse_info *info;
  478.   unsigned int estimated_len;
  479.   char *ret;

  480.   if (cp_already_canonical (string))
  481.     return NULL;

  482.   info = cp_demangled_name_to_comp (string, NULL);
  483.   if (info == NULL)
  484.     return NULL;

  485.   estimated_len = strlen (string) * 2;
  486.   ret = cp_comp_to_string (info->tree, estimated_len);
  487.   cp_demangled_name_parse_free (info);

  488.   if (ret == NULL)
  489.     {
  490.       warning (_("internal error: string \"%s\" failed to be canonicalized"),
  491.                string);
  492.       return NULL;
  493.     }

  494.   if (strcmp (string, ret) == 0)
  495.     {
  496.       xfree (ret);
  497.       return NULL;
  498.     }

  499.   return ret;
  500. }

  501. /* Convert a mangled name to a demangle_component tree.  *MEMORY is
  502.    set to the block of used memory that should be freed when finished
  503.    with the tree.  DEMANGLED_P is set to the char * that should be
  504.    freed when finished with the tree, or NULL if none was needed.
  505.    OPTIONS will be passed to the demangler.  */

  506. static struct demangle_parse_info *
  507. mangled_name_to_comp (const char *mangled_name, int options,
  508.                       void **memory, char **demangled_p)
  509. {
  510.   char *demangled_name;
  511.   struct demangle_parse_info *info;

  512.   /* If it looks like a v3 mangled name, then try to go directly
  513.      to trees.  */
  514.   if (mangled_name[0] == '_' && mangled_name[1] == 'Z')
  515.     {
  516.       struct demangle_component *ret;

  517.       ret = cplus_demangle_v3_components (mangled_name,
  518.                                           options, memory);
  519.       if (ret)
  520.         {
  521.           info = cp_new_demangle_parse_info ();
  522.           info->tree = ret;
  523.           *demangled_p = NULL;
  524.           return info;
  525.         }
  526.     }

  527.   /* If it doesn't, or if that failed, then try to demangle the
  528.      name.  */
  529.   demangled_name = gdb_demangle (mangled_name, options);
  530.   if (demangled_name == NULL)
  531.    return NULL;

  532.   /* If we could demangle the name, parse it to build the component
  533.      tree.  */
  534.   info = cp_demangled_name_to_comp (demangled_name, NULL);

  535.   if (info == NULL)
  536.     {
  537.       xfree (demangled_name);
  538.       return NULL;
  539.     }

  540.   *demangled_p = demangled_name;
  541.   return info;
  542. }

  543. /* Return the name of the class containing method PHYSNAME.  */

  544. char *
  545. cp_class_name_from_physname (const char *physname)
  546. {
  547.   void *storage = NULL;
  548.   char *demangled_name = NULL, *ret;
  549.   struct demangle_component *ret_comp, *prev_comp, *cur_comp;
  550.   struct demangle_parse_info *info;
  551.   int done;

  552.   info = mangled_name_to_comp (physname, DMGL_ANSI,
  553.                                &storage, &demangled_name);
  554.   if (info == NULL)
  555.     return NULL;

  556.   done = 0;
  557.   ret_comp = info->tree;

  558.   /* First strip off any qualifiers, if we have a function or
  559.      method.  */
  560.   while (!done)
  561.     switch (ret_comp->type)
  562.       {
  563.       case DEMANGLE_COMPONENT_CONST:
  564.       case DEMANGLE_COMPONENT_RESTRICT:
  565.       case DEMANGLE_COMPONENT_VOLATILE:
  566.       case DEMANGLE_COMPONENT_CONST_THIS:
  567.       case DEMANGLE_COMPONENT_RESTRICT_THIS:
  568.       case DEMANGLE_COMPONENT_VOLATILE_THIS:
  569.       case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
  570.         ret_comp = d_left (ret_comp);
  571.         break;
  572.       default:
  573.         done = 1;
  574.         break;
  575.       }

  576.   /* If what we have now is a function, discard the argument list.  */
  577.   if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
  578.     ret_comp = d_left (ret_comp);

  579.   /* If what we have now is a template, strip off the template
  580.      arguments.  The left subtree may be a qualified name.  */
  581.   if (ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE)
  582.     ret_comp = d_left (ret_comp);

  583.   /* What we have now should be a name, possibly qualified.
  584.      Additional qualifiers could live in the left subtree or the right
  585.      subtree.  Find the last piece.  */
  586.   done = 0;
  587.   prev_comp = NULL;
  588.   cur_comp = ret_comp;
  589.   while (!done)
  590.     switch (cur_comp->type)
  591.       {
  592.       case DEMANGLE_COMPONENT_QUAL_NAME:
  593.       case DEMANGLE_COMPONENT_LOCAL_NAME:
  594.         prev_comp = cur_comp;
  595.         cur_comp = d_right (cur_comp);
  596.         break;
  597.       case DEMANGLE_COMPONENT_TEMPLATE:
  598.       case DEMANGLE_COMPONENT_NAME:
  599.       case DEMANGLE_COMPONENT_CTOR:
  600.       case DEMANGLE_COMPONENT_DTOR:
  601.       case DEMANGLE_COMPONENT_OPERATOR:
  602.       case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
  603.         done = 1;
  604.         break;
  605.       default:
  606.         done = 1;
  607.         cur_comp = NULL;
  608.         break;
  609.       }

  610.   ret = NULL;
  611.   if (cur_comp != NULL && prev_comp != NULL)
  612.     {
  613.       /* We want to discard the rightmost child of PREV_COMP.  */
  614.       *prev_comp = *d_left (prev_comp);
  615.       /* The ten is completely arbitrary; we don't have a good
  616.          estimate.  */
  617.       ret = cp_comp_to_string (ret_comp, 10);
  618.     }

  619.   xfree (storage);
  620.   xfree (demangled_name);
  621.   cp_demangled_name_parse_free (info);
  622.   return ret;
  623. }

  624. /* Return the child of COMP which is the basename of a method,
  625.    variable, et cetera.  All scope qualifiers are discarded, but
  626.    template arguments will be included.  The component tree may be
  627.    modified.  */

  628. static struct demangle_component *
  629. unqualified_name_from_comp (struct demangle_component *comp)
  630. {
  631.   struct demangle_component *ret_comp = comp, *last_template;
  632.   int done;

  633.   done = 0;
  634.   last_template = NULL;
  635.   while (!done)
  636.     switch (ret_comp->type)
  637.       {
  638.       case DEMANGLE_COMPONENT_QUAL_NAME:
  639.       case DEMANGLE_COMPONENT_LOCAL_NAME:
  640.         ret_comp = d_right (ret_comp);
  641.         break;
  642.       case DEMANGLE_COMPONENT_TYPED_NAME:
  643.         ret_comp = d_left (ret_comp);
  644.         break;
  645.       case DEMANGLE_COMPONENT_TEMPLATE:
  646.         gdb_assert (last_template == NULL);
  647.         last_template = ret_comp;
  648.         ret_comp = d_left (ret_comp);
  649.         break;
  650.       case DEMANGLE_COMPONENT_CONST:
  651.       case DEMANGLE_COMPONENT_RESTRICT:
  652.       case DEMANGLE_COMPONENT_VOLATILE:
  653.       case DEMANGLE_COMPONENT_CONST_THIS:
  654.       case DEMANGLE_COMPONENT_RESTRICT_THIS:
  655.       case DEMANGLE_COMPONENT_VOLATILE_THIS:
  656.       case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
  657.         ret_comp = d_left (ret_comp);
  658.         break;
  659.       case DEMANGLE_COMPONENT_NAME:
  660.       case DEMANGLE_COMPONENT_CTOR:
  661.       case DEMANGLE_COMPONENT_DTOR:
  662.       case DEMANGLE_COMPONENT_OPERATOR:
  663.       case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
  664.         done = 1;
  665.         break;
  666.       default:
  667.         return NULL;
  668.         break;
  669.       }

  670.   if (last_template)
  671.     {
  672.       d_left (last_template) = ret_comp;
  673.       return last_template;
  674.     }

  675.   return ret_comp;
  676. }

  677. /* Return the name of the method whose linkage name is PHYSNAME.  */

  678. char *
  679. method_name_from_physname (const char *physname)
  680. {
  681.   void *storage = NULL;
  682.   char *demangled_name = NULL, *ret;
  683.   struct demangle_component *ret_comp;
  684.   struct demangle_parse_info *info;

  685.   info = mangled_name_to_comp (physname, DMGL_ANSI,
  686.                                &storage, &demangled_name);
  687.   if (info == NULL)
  688.     return NULL;

  689.   ret_comp = unqualified_name_from_comp (info->tree);

  690.   ret = NULL;
  691.   if (ret_comp != NULL)
  692.     /* The ten is completely arbitrary; we don't have a good
  693.        estimate.  */
  694.     ret = cp_comp_to_string (ret_comp, 10);

  695.   xfree (storage);
  696.   xfree (demangled_name);
  697.   cp_demangled_name_parse_free (info);
  698.   return ret;
  699. }

  700. /* If FULL_NAME is the demangled name of a C++ function (including an
  701.    arg list, possibly including namespace/class qualifications),
  702.    return a new string containing only the function name (without the
  703.    arg list/class qualifications).  Otherwise, return NULL.  The
  704.    caller is responsible for freeing the memory in question.  */

  705. char *
  706. cp_func_name (const char *full_name)
  707. {
  708.   char *ret;
  709.   struct demangle_component *ret_comp;
  710.   struct demangle_parse_info *info;

  711.   info = cp_demangled_name_to_comp (full_name, NULL);
  712.   if (!info)
  713.     return NULL;

  714.   ret_comp = unqualified_name_from_comp (info->tree);

  715.   ret = NULL;
  716.   if (ret_comp != NULL)
  717.     ret = cp_comp_to_string (ret_comp, 10);

  718.   cp_demangled_name_parse_free (info);
  719.   return ret;
  720. }

  721. /* DEMANGLED_NAME is the name of a function, including parameters and
  722.    (optionally) a return type.  Return the name of the function without
  723.    parameters or return type, or NULL if we can not parse the name.  */

  724. char *
  725. cp_remove_params (const char *demangled_name)
  726. {
  727.   int done = 0;
  728.   struct demangle_component *ret_comp;
  729.   struct demangle_parse_info *info;
  730.   char *ret = NULL;

  731.   if (demangled_name == NULL)
  732.     return NULL;

  733.   info = cp_demangled_name_to_comp (demangled_name, NULL);
  734.   if (info == NULL)
  735.     return NULL;

  736.   /* First strip off any qualifiers, if we have a function or method.  */
  737.   ret_comp = info->tree;
  738.   while (!done)
  739.     switch (ret_comp->type)
  740.       {
  741.       case DEMANGLE_COMPONENT_CONST:
  742.       case DEMANGLE_COMPONENT_RESTRICT:
  743.       case DEMANGLE_COMPONENT_VOLATILE:
  744.       case DEMANGLE_COMPONENT_CONST_THIS:
  745.       case DEMANGLE_COMPONENT_RESTRICT_THIS:
  746.       case DEMANGLE_COMPONENT_VOLATILE_THIS:
  747.       case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
  748.         ret_comp = d_left (ret_comp);
  749.         break;
  750.       default:
  751.         done = 1;
  752.         break;
  753.       }

  754.   /* What we have now should be a function.  Return its name.  */
  755.   if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
  756.     ret = cp_comp_to_string (d_left (ret_comp), 10);

  757.   cp_demangled_name_parse_free (info);
  758.   return ret;
  759. }

  760. /* Here are some random pieces of trivia to keep in mind while trying
  761.    to take apart demangled names:

  762.    - Names can contain function arguments or templates, so the process
  763.      has to be, to some extent recursive: maybe keep track of your
  764.      depth based on encountering <> and ().

  765.    - Parentheses don't just have to happen at the end of a name: they
  766.      can occur even if the name in question isn't a function, because
  767.      a template argument might be a type that's a function.

  768.    - Conversely, even if you're trying to deal with a function, its
  769.      demangled name might not end with ')': it could be a const or
  770.      volatile class method, in which case it ends with "const" or
  771.      "volatile".

  772.    - Parentheses are also used in anonymous namespaces: a variable
  773.      'foo' in an anonymous namespace gets demangled as "(anonymous
  774.      namespace)::foo".

  775.    - And operator names can contain parentheses or angle brackets.  */

  776. /* FIXME: carlton/2003-03-13: We have several functions here with
  777.    overlapping functionality; can we combine them?  Also, do they
  778.    handle all the above considerations correctly?  */


  779. /* This returns the length of first component of NAME, which should be
  780.    the demangled name of a C++ variable/function/method/etc.
  781.    Specifically, it returns the index of the first colon forming the
  782.    boundary of the first component: so, given 'A::foo' or 'A::B::foo'
  783.    it returns the 1, and given 'foo', it returns 0.  */

  784. /* The character in NAME indexed by the return value is guaranteed to
  785.    always be either ':' or '\0'.  */

  786. /* NOTE: carlton/2003-03-13: This function is currently only intended
  787.    for internal use: it's probably not entirely safe when called on
  788.    user-generated input, because some of the 'index += 2' lines in
  789.    cp_find_first_component_aux might go past the end of malformed
  790.    input.  */

  791. unsigned int
  792. cp_find_first_component (const char *name)
  793. {
  794.   return cp_find_first_component_aux (name, 0);
  795. }

  796. /* Helper function for cp_find_first_component.  Like that function,
  797.    it returns the length of the first component of NAME, but to make
  798.    the recursion easier, it also stops if it reaches an unexpected ')'
  799.    or '>' if the value of PERMISSIVE is nonzero.  */

  800. /* Let's optimize away calls to strlen("operator").  */

  801. #define LENGTH_OF_OPERATOR 8

  802. static unsigned int
  803. cp_find_first_component_aux (const char *name, int permissive)
  804. {
  805.   unsigned int index = 0;
  806.   /* Operator names can show up in unexpected places.  Since these can
  807.      contain parentheses or angle brackets, they can screw up the
  808.      recursion.  But not every string 'operator' is part of an
  809.      operater name: e.g. you could have a variable 'cooperator'.  So
  810.      this variable tells us whether or not we should treat the string
  811.      'operator' as starting an operator.  */
  812.   int operator_possible = 1;

  813.   for (;; ++index)
  814.     {
  815.       switch (name[index])
  816.         {
  817.         case '<':
  818.           /* Template; eat it up.  The calls to cp_first_component
  819.              should only return (I hope!) when they reach the '>'
  820.              terminating the component or a '::' between two
  821.              components.  (Hence the '+ 2'.)  */
  822.           index += 1;
  823.           for (index += cp_find_first_component_aux (name + index, 1);
  824.                name[index] != '>';
  825.                index += cp_find_first_component_aux (name + index, 1))
  826.             {
  827.               if (name[index] != ':')
  828.                 {
  829.                   demangled_name_complaint (name);
  830.                   return strlen (name);
  831.                 }
  832.               index += 2;
  833.             }
  834.           operator_possible = 1;
  835.           break;
  836.         case '(':
  837.           /* Similar comment as to '<'.  */
  838.           index += 1;
  839.           for (index += cp_find_first_component_aux (name + index, 1);
  840.                name[index] != ')';
  841.                index += cp_find_first_component_aux (name + index, 1))
  842.             {
  843.               if (name[index] != ':')
  844.                 {
  845.                   demangled_name_complaint (name);
  846.                   return strlen (name);
  847.                 }
  848.               index += 2;
  849.             }
  850.           operator_possible = 1;
  851.           break;
  852.         case '>':
  853.         case ')':
  854.           if (permissive)
  855.             return index;
  856.           else
  857.             {
  858.               demangled_name_complaint (name);
  859.               return strlen (name);
  860.             }
  861.         case '\0':
  862.         case ':':
  863.           return index;
  864.         case 'o':
  865.           /* Operator names can screw up the recursion.  */
  866.           if (operator_possible
  867.               && strncmp (name + index, "operator",
  868.                           LENGTH_OF_OPERATOR) == 0)
  869.             {
  870.               index += LENGTH_OF_OPERATOR;
  871.               while (ISSPACE(name[index]))
  872.                 ++index;
  873.               switch (name[index])
  874.                 {
  875.                   /* Skip over one less than the appropriate number of
  876.                      characters: the for loop will skip over the last
  877.                      one.  */
  878.                 case '<':
  879.                   if (name[index + 1] == '<')
  880.                     index += 1;
  881.                   else
  882.                     index += 0;
  883.                   break;
  884.                 case '>':
  885.                 case '-':
  886.                   if (name[index + 1] == '>')
  887.                     index += 1;
  888.                   else
  889.                     index += 0;
  890.                   break;
  891.                 case '(':
  892.                   index += 1;
  893.                   break;
  894.                 default:
  895.                   index += 0;
  896.                   break;
  897.                 }
  898.             }
  899.           operator_possible = 0;
  900.           break;
  901.         case ' ':
  902.         case ',':
  903.         case '.':
  904.         case '&':
  905.         case '*':
  906.           /* NOTE: carlton/2003-04-18: I'm not sure what the precise
  907.              set of relevant characters are here: it's necessary to
  908.              include any character that can show up before 'operator'
  909.              in a demangled name, and it's safe to include any
  910.              character that can't be part of an identifier's name.  */
  911.           operator_possible = 1;
  912.           break;
  913.         default:
  914.           operator_possible = 0;
  915.           break;
  916.         }
  917.     }
  918. }

  919. /* Complain about a demangled name that we don't know how to parse.
  920.    NAME is the demangled name in question.  */

  921. static void
  922. demangled_name_complaint (const char *name)
  923. {
  924.   complaint (&symfile_complaints,
  925.              "unexpected demangled name '%s'", name);
  926. }

  927. /* If NAME is the fully-qualified name of a C++
  928.    function/variable/method/etc., this returns the length of its
  929.    entire prefix: all of the namespaces and classes that make up its
  930.    name.  Given 'A::foo', it returns 1, given 'A::B::foo', it returns
  931.    4, given 'foo', it returns 0.  */

  932. unsigned int
  933. cp_entire_prefix_len (const char *name)
  934. {
  935.   unsigned int current_len = cp_find_first_component (name);
  936.   unsigned int previous_len = 0;

  937.   while (name[current_len] != '\0')
  938.     {
  939.       gdb_assert (name[current_len] == ':');
  940.       previous_len = current_len;
  941.       /* Skip the '::'.  */
  942.       current_len += 2;
  943.       current_len += cp_find_first_component (name + current_len);
  944.     }

  945.   return previous_len;
  946. }

  947. /* Overload resolution functions.  */

  948. /* Test to see if SYM is a symbol that we haven't seen corresponding
  949.    to a function named OLOAD_NAME.  If so, add it to the current
  950.    completion list.  */

  951. static void
  952. overload_list_add_symbol (struct symbol *sym,
  953.                           const char *oload_name)
  954. {
  955.   int newsize;
  956.   int i;
  957.   char *sym_name;

  958.   /* If there is no type information, we can't do anything, so
  959.      skip.  */
  960.   if (SYMBOL_TYPE (sym) == NULL)
  961.     return;

  962.   /* skip any symbols that we've already considered.  */
  963.   for (i = 0; i < sym_return_val_index; ++i)
  964.     if (strcmp (SYMBOL_LINKAGE_NAME (sym),
  965.                 SYMBOL_LINKAGE_NAME (sym_return_val[i])) == 0)
  966.       return;

  967.   /* Get the demangled name without parameters */
  968.   sym_name = cp_remove_params (SYMBOL_NATURAL_NAME (sym));
  969.   if (!sym_name)
  970.     return;

  971.   /* skip symbols that cannot match */
  972.   if (strcmp (sym_name, oload_name) != 0)
  973.     {
  974.       xfree (sym_name);
  975.       return;
  976.     }

  977.   xfree (sym_name);

  978.   /* We have a match for an overload instance, so add SYM to the
  979.      current list of overload instances */
  980.   if (sym_return_val_index + 3 > sym_return_val_size)
  981.     {
  982.       newsize = (sym_return_val_size *= 2) * sizeof (struct symbol *);
  983.       sym_return_val = (struct symbol **)
  984.         xrealloc ((char *) sym_return_val, newsize);
  985.     }
  986.   sym_return_val[sym_return_val_index++] = sym;
  987.   sym_return_val[sym_return_val_index] = NULL;
  988. }

  989. /* Return a null-terminated list of pointers to function symbols that
  990.    are named FUNC_NAME and are visible within NAMESPACE.  */

  991. struct symbol **
  992. make_symbol_overload_list (const char *func_name,
  993.                            const char *namespace)
  994. {
  995.   struct cleanup *old_cleanups;
  996.   const char *name;

  997.   sym_return_val_size = 100;
  998.   sym_return_val_index = 0;
  999.   sym_return_val = xmalloc ((sym_return_val_size + 1) *
  1000.                             sizeof (struct symbol *));
  1001.   sym_return_val[0] = NULL;

  1002.   old_cleanups = make_cleanup (xfree, sym_return_val);

  1003.   make_symbol_overload_list_using (func_name, namespace);

  1004.   if (namespace[0] == '\0')
  1005.     name = func_name;
  1006.   else
  1007.     {
  1008.       char *concatenated_name
  1009.         = alloca (strlen (namespace) + 2 + strlen (func_name) + 1);
  1010.       strcpy (concatenated_name, namespace);
  1011.       strcat (concatenated_name, "::");
  1012.       strcat (concatenated_name, func_name);
  1013.       name = concatenated_name;
  1014.     }

  1015.   make_symbol_overload_list_qualified (name);

  1016.   discard_cleanups (old_cleanups);

  1017.   return sym_return_val;
  1018. }

  1019. /* Add all symbols with a name matching NAME in BLOCK to the overload
  1020.    list.  */

  1021. static void
  1022. make_symbol_overload_list_block (const char *name,
  1023.                                  const struct block *block)
  1024. {
  1025.   struct block_iterator iter;
  1026.   struct symbol *sym;

  1027.   ALL_BLOCK_SYMBOLS_WITH_NAME (block, name, iter, sym)
  1028.     overload_list_add_symbol (sym, name);
  1029. }

  1030. /* Adds the function FUNC_NAME from NAMESPACE to the overload set.  */

  1031. static void
  1032. make_symbol_overload_list_namespace (const char *func_name,
  1033.                                      const char *namespace)
  1034. {
  1035.   const char *name;
  1036.   const struct block *block = NULL;

  1037.   if (namespace[0] == '\0')
  1038.     name = func_name;
  1039.   else
  1040.     {
  1041.       char *concatenated_name
  1042.         = alloca (strlen (namespace) + 2 + strlen (func_name) + 1);

  1043.       strcpy (concatenated_name, namespace);
  1044.       strcat (concatenated_name, "::");
  1045.       strcat (concatenated_name, func_name);
  1046.       name = concatenated_name;
  1047.     }

  1048.   /* Look in the static block.  */
  1049.   block = block_static_block (get_selected_block (0));
  1050.   if (block)
  1051.     make_symbol_overload_list_block (name, block);

  1052.   /* Look in the global block.  */
  1053.   block = block_global_block (block);
  1054.   if (block)
  1055.     make_symbol_overload_list_block (name, block);

  1056. }

  1057. /* Search the namespace of the given type and namespace of and public
  1058.    base types.  */

  1059. static void
  1060. make_symbol_overload_list_adl_namespace (struct type *type,
  1061.                                          const char *func_name)
  1062. {
  1063.   char *namespace;
  1064.   const char *type_name;
  1065.   int i, prefix_len;

  1066.   while (TYPE_CODE (type) == TYPE_CODE_PTR
  1067.          || TYPE_CODE (type) == TYPE_CODE_REF
  1068.          || TYPE_CODE (type) == TYPE_CODE_ARRAY
  1069.          || TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
  1070.     {
  1071.       if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
  1072.         type = check_typedef(type);
  1073.       else
  1074.         type = TYPE_TARGET_TYPE (type);
  1075.     }

  1076.   type_name = TYPE_NAME (type);

  1077.   if (type_name == NULL)
  1078.     return;

  1079.   prefix_len = cp_entire_prefix_len (type_name);

  1080.   if (prefix_len != 0)
  1081.     {
  1082.       namespace = alloca (prefix_len + 1);
  1083.       strncpy (namespace, type_name, prefix_len);
  1084.       namespace[prefix_len] = '\0';

  1085.       make_symbol_overload_list_namespace (func_name, namespace);
  1086.     }

  1087.   /* Check public base type */
  1088.   if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
  1089.     for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
  1090.       {
  1091.         if (BASETYPE_VIA_PUBLIC (type, i))
  1092.           make_symbol_overload_list_adl_namespace (TYPE_BASECLASS (type,
  1093.                                                                    i),
  1094.                                                    func_name);
  1095.       }
  1096. }

  1097. /* Adds the overload list overload candidates for FUNC_NAME found
  1098.    through argument dependent lookup.  */

  1099. struct symbol **
  1100. make_symbol_overload_list_adl (struct type **arg_types, int nargs,
  1101.                                const char *func_name)
  1102. {
  1103.   int i;

  1104.   gdb_assert (sym_return_val_size != -1);

  1105.   for (i = 1; i <= nargs; i++)
  1106.     make_symbol_overload_list_adl_namespace (arg_types[i - 1],
  1107.                                              func_name);

  1108.   return sym_return_val;
  1109. }

  1110. /* Used for cleanups to reset the "searched" flag in case of an
  1111.    error.  */

  1112. static void
  1113. reset_directive_searched (void *data)
  1114. {
  1115.   struct using_direct *direct = data;
  1116.   direct->searched = 0;
  1117. }

  1118. /* This applies the using directives to add namespaces to search in,
  1119.    and then searches for overloads in all of those namespaces.  It
  1120.    adds the symbols found to sym_return_val.  Arguments are as in
  1121.    make_symbol_overload_list.  */

  1122. static void
  1123. make_symbol_overload_list_using (const char *func_name,
  1124.                                  const char *namespace)
  1125. {
  1126.   struct using_direct *current;
  1127.   const struct block *block;

  1128.   /* First, go through the using directives.  If any of them apply,
  1129.      look in the appropriate namespaces for new functions to match
  1130.      on.  */

  1131.   for (block = get_selected_block (0);
  1132.        block != NULL;
  1133.        block = BLOCK_SUPERBLOCK (block))
  1134.     for (current = block_using (block);
  1135.         current != NULL;
  1136.         current = current->next)
  1137.       {
  1138.         /* Prevent recursive calls.  */
  1139.         if (current->searched)
  1140.           continue;

  1141.         /* If this is a namespace alias or imported declaration ignore
  1142.            it.  */
  1143.         if (current->alias != NULL || current->declaration != NULL)
  1144.           continue;

  1145.         if (strcmp (namespace, current->import_dest) == 0)
  1146.           {
  1147.             /* Mark this import as searched so that the recursive call
  1148.                does not search it again.  */
  1149.             struct cleanup *old_chain;
  1150.             current->searched = 1;
  1151.             old_chain = make_cleanup (reset_directive_searched,
  1152.                                       current);

  1153.             make_symbol_overload_list_using (func_name,
  1154.                                              current->import_src);

  1155.             current->searched = 0;
  1156.             discard_cleanups (old_chain);
  1157.           }
  1158.       }

  1159.   /* Now, add names for this namespace.  */
  1160.   make_symbol_overload_list_namespace (func_name, namespace);
  1161. }

  1162. /* This does the bulk of the work of finding overloaded symbols.
  1163.    FUNC_NAME is the name of the overloaded function we're looking for
  1164.    (possibly including namespace info).  */

  1165. static void
  1166. make_symbol_overload_list_qualified (const char *func_name)
  1167. {
  1168.   struct compunit_symtab *cust;
  1169.   struct objfile *objfile;
  1170.   const struct block *b, *surrounding_static_block = 0;

  1171.   /* Look through the partial symtabs for all symbols which begin by
  1172.      matching FUNC_NAME.  Make sure we read that symbol table in.  */

  1173.   ALL_OBJFILES (objfile)
  1174.   {
  1175.     if (objfile->sf)
  1176.       objfile->sf->qf->expand_symtabs_for_function (objfile, func_name);
  1177.   }

  1178.   /* Search upwards from currently selected frame (so that we can
  1179.      complete on local vars.  */

  1180.   for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
  1181.     make_symbol_overload_list_block (func_name, b);

  1182.   surrounding_static_block = block_static_block (get_selected_block (0));

  1183.   /* Go through the symtabs and check the externs and statics for
  1184.      symbols which match.  */

  1185.   ALL_COMPUNITS (objfile, cust)
  1186.   {
  1187.     QUIT;
  1188.     b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), GLOBAL_BLOCK);
  1189.     make_symbol_overload_list_block (func_name, b);
  1190.   }

  1191.   ALL_COMPUNITS (objfile, cust)
  1192.   {
  1193.     QUIT;
  1194.     b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), STATIC_BLOCK);
  1195.     /* Don't do this block twice.  */
  1196.     if (b == surrounding_static_block)
  1197.       continue;
  1198.     make_symbol_overload_list_block (func_name, b);
  1199.   }
  1200. }

  1201. /* Lookup the rtti type for a class name.  */

  1202. struct type *
  1203. cp_lookup_rtti_type (const char *name, struct block *block)
  1204. {
  1205.   struct symbol * rtti_sym;
  1206.   struct type * rtti_type;

  1207.   rtti_sym = lookup_symbol (name, block, STRUCT_DOMAIN, NULL);

  1208.   if (rtti_sym == NULL)
  1209.     {
  1210.       warning (_("RTTI symbol not found for class '%s'"), name);
  1211.       return NULL;
  1212.     }

  1213.   if (SYMBOL_CLASS (rtti_sym) != LOC_TYPEDEF)
  1214.     {
  1215.       warning (_("RTTI symbol for class '%s' is not a type"), name);
  1216.       return NULL;
  1217.     }

  1218.   rtti_type = SYMBOL_TYPE (rtti_sym);

  1219.   switch (TYPE_CODE (rtti_type))
  1220.     {
  1221.     case TYPE_CODE_STRUCT:
  1222.       break;
  1223.     case TYPE_CODE_NAMESPACE:
  1224.       /* chastain/2003-11-26: the symbol tables often contain fake
  1225.          symbols for namespaces with the same name as the struct.
  1226.          This warning is an indication of a bug in the lookup order
  1227.          or a bug in the way that the symbol tables are populated.  */
  1228.       warning (_("RTTI symbol for class '%s' is a namespace"), name);
  1229.       return NULL;
  1230.     default:
  1231.       warning (_("RTTI symbol for class '%s' has bad type"), name);
  1232.       return NULL;
  1233.     }

  1234.   return rtti_type;
  1235. }

  1236. #ifdef HAVE_WORKING_FORK

  1237. /* If nonzero, attempt to catch crashes in the demangler and print
  1238.    useful debugging information.  */

  1239. static int catch_demangler_crashes = 1;

  1240. /* Stack context and environment for demangler crash recovery.  */

  1241. static SIGJMP_BUF gdb_demangle_jmp_buf;

  1242. /* If nonzero, attempt to dump core from the signal handler.  */

  1243. static int gdb_demangle_attempt_core_dump = 1;

  1244. /* Signal handler for gdb_demangle.  */

  1245. static void
  1246. gdb_demangle_signal_handler (int signo)
  1247. {
  1248.   if (gdb_demangle_attempt_core_dump)
  1249.     {
  1250.       if (fork () == 0)
  1251.         dump_core ();

  1252.       gdb_demangle_attempt_core_dump = 0;
  1253.     }

  1254.   SIGLONGJMP (gdb_demangle_jmp_buf, signo);
  1255. }

  1256. #endif

  1257. /* A wrapper for bfd_demangle.  */

  1258. char *
  1259. gdb_demangle (const char *name, int options)
  1260. {
  1261.   char *result = NULL;
  1262.   int crash_signal = 0;

  1263. #ifdef HAVE_WORKING_FORK
  1264. #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
  1265.   struct sigaction sa, old_sa;
  1266. #else
  1267.   void (*ofunc) ();
  1268. #endif
  1269.   static int core_dump_allowed = -1;

  1270.   if (core_dump_allowed == -1)
  1271.     {
  1272.       core_dump_allowed = can_dump_core (LIMIT_CUR);

  1273.       if (!core_dump_allowed)
  1274.         gdb_demangle_attempt_core_dump = 0;
  1275.     }

  1276.   if (catch_demangler_crashes)
  1277.     {
  1278. #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
  1279.       sa.sa_handler = gdb_demangle_signal_handler;
  1280.       sigemptyset (&sa.sa_mask);
  1281. #ifdef HAVE_SIGALTSTACK
  1282.       sa.sa_flags = SA_ONSTACK;
  1283. #else
  1284.       sa.sa_flags = 0;
  1285. #endif
  1286.       sigaction (SIGSEGV, &sa, &old_sa);
  1287. #else
  1288.       ofunc = (void (*)()) signal (SIGSEGV, gdb_demangle_signal_handler);
  1289. #endif

  1290.       crash_signal = SIGSETJMP (gdb_demangle_jmp_buf);
  1291.     }
  1292. #endif

  1293.   if (crash_signal == 0)
  1294.     result = bfd_demangle (NULL, name, options);

  1295. #ifdef HAVE_WORKING_FORK
  1296.   if (catch_demangler_crashes)
  1297.     {
  1298. #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
  1299.       sigaction (SIGSEGV, &old_sa, NULL);
  1300. #else
  1301.       signal (SIGSEGV, ofunc);
  1302. #endif

  1303.       if (crash_signal != 0)
  1304.         {
  1305.           static int error_reported = 0;

  1306.           if (!error_reported)
  1307.             {
  1308.               char *short_msg, *long_msg;
  1309.               struct cleanup *back_to;

  1310.               short_msg = xstrprintf (_("unable to demangle '%s' "
  1311.                                       "(demangler failed with signal %d)"),
  1312.                                     name, crash_signal);
  1313.               back_to = make_cleanup (xfree, short_msg);

  1314.               long_msg = xstrprintf ("%s:%d: %s: %s", __FILE__, __LINE__,
  1315.                                     "demangler-warning", short_msg);
  1316.               make_cleanup (xfree, long_msg);

  1317.               target_terminal_ours ();
  1318.               begin_line ();
  1319.               if (core_dump_allowed)
  1320.                 fprintf_unfiltered (gdb_stderr,
  1321.                                     _("%s\nAttempting to dump core.\n"),
  1322.                                     long_msg);
  1323.               else
  1324.                 warn_cant_dump_core (long_msg);

  1325.               demangler_warning (__FILE__, __LINE__, "%s", short_msg);

  1326.               do_cleanups (back_to);

  1327.               error_reported = 1;
  1328.             }

  1329.           result = NULL;
  1330.         }
  1331.     }
  1332. #endif

  1333.   return result;
  1334. }

  1335. /* Don't allow just "maintenance cplus".  */

  1336. static  void
  1337. maint_cplus_command (char *arg, int from_tty)
  1338. {
  1339.   printf_unfiltered (_("\"maintenance cplus\" must be followed "
  1340.                        "by the name of a command.\n"));
  1341.   help_list (maint_cplus_cmd_list,
  1342.              "maintenance cplus ",
  1343.              all_commands, gdb_stdout);
  1344. }

  1345. /* This is a front end for cp_find_first_component, for unit testing.
  1346.    Be careful when using it: see the NOTE above
  1347.    cp_find_first_component.  */

  1348. static void
  1349. first_component_command (char *arg, int from_tty)
  1350. {
  1351.   int len;
  1352.   char *prefix;

  1353.   if (!arg)
  1354.     return;

  1355.   len = cp_find_first_component (arg);
  1356.   prefix = alloca (len + 1);

  1357.   memcpy (prefix, arg, len);
  1358.   prefix[len] = '\0';

  1359.   printf_unfiltered ("%s\n", prefix);
  1360. }

  1361. extern initialize_file_ftype _initialize_cp_support; /* -Wmissing-prototypes */


  1362. /* Implement "info vtbl".  */

  1363. static void
  1364. info_vtbl_command (char *arg, int from_tty)
  1365. {
  1366.   struct value *value;

  1367.   value = parse_and_eval (arg);
  1368.   cplus_print_vtable (value);
  1369. }

  1370. void
  1371. _initialize_cp_support (void)
  1372. {
  1373.   add_prefix_cmd ("cplus", class_maintenance,
  1374.                   maint_cplus_command,
  1375.                   _("C++ maintenance commands."),
  1376.                   &maint_cplus_cmd_list,
  1377.                   "maintenance cplus ",
  1378.                   0, &maintenancelist);
  1379.   add_alias_cmd ("cp", "cplus",
  1380.                  class_maintenance, 1,
  1381.                  &maintenancelist);

  1382.   add_cmd ("first_component",
  1383.            class_maintenance,
  1384.            first_component_command,
  1385.            _("Print the first class/namespace component of NAME."),
  1386.            &maint_cplus_cmd_list);

  1387.   add_info ("vtbl", info_vtbl_command,
  1388.             _("Show the virtual function table for a C++ object.\n\
  1389. Usage: info vtbl EXPRESSION\n\
  1390. Evaluate EXPRESSION and display the virtual function table for the\n\
  1391. resulting object."));

  1392. #ifdef HAVE_WORKING_FORK
  1393.   add_setshow_boolean_cmd ("catch-demangler-crashes", class_maintenance,
  1394.                            &catch_demangler_crashes, _("\
  1395. Set whether to attempt to catch demangler crashes."), _("\
  1396. Show whether to attempt to catch demangler crashes."), _("\
  1397. If enabled GDB will attempt to catch demangler crashes and\n\
  1398. display the offending symbol."),
  1399.                            NULL,
  1400.                            NULL,
  1401.                            &maintenance_set_cmdlist,
  1402.                            &maintenance_show_cmdlist);
  1403. #endif
  1404. }