Home | History | Annotate | Line # | Download | only in gdb
cp-support.c revision 1.1.1.4
      1      1.1  christos /* Helper routines for C++ support in GDB.
      2  1.1.1.4  christos    Copyright (C) 2002-2016 Free Software Foundation, Inc.
      3      1.1  christos 
      4      1.1  christos    Contributed by MontaVista Software.
      5      1.1  christos 
      6      1.1  christos    This file is part of GDB.
      7      1.1  christos 
      8      1.1  christos    This program is free software; you can redistribute it and/or modify
      9      1.1  christos    it under the terms of the GNU General Public License as published by
     10      1.1  christos    the Free Software Foundation; either version 3 of the License, or
     11      1.1  christos    (at your option) any later version.
     12      1.1  christos 
     13      1.1  christos    This program is distributed in the hope that it will be useful,
     14      1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     15      1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16      1.1  christos    GNU General Public License for more details.
     17      1.1  christos 
     18      1.1  christos    You should have received a copy of the GNU General Public License
     19      1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     20      1.1  christos 
     21      1.1  christos #include "defs.h"
     22      1.1  christos #include "cp-support.h"
     23      1.1  christos #include "demangle.h"
     24      1.1  christos #include "gdbcmd.h"
     25      1.1  christos #include "dictionary.h"
     26      1.1  christos #include "objfiles.h"
     27      1.1  christos #include "frame.h"
     28      1.1  christos #include "symtab.h"
     29      1.1  christos #include "block.h"
     30      1.1  christos #include "complaints.h"
     31      1.1  christos #include "gdbtypes.h"
     32      1.1  christos #include "expression.h"
     33      1.1  christos #include "value.h"
     34      1.1  christos #include "cp-abi.h"
     35  1.1.1.4  christos #include "namespace.h"
     36  1.1.1.2  christos #include <signal.h>
     37  1.1.1.4  christos #include "gdb_setjmp.h"
     38      1.1  christos #include "safe-ctype.h"
     39      1.1  christos 
     40      1.1  christos #define d_left(dc) (dc)->u.s_binary.left
     41      1.1  christos #define d_right(dc) (dc)->u.s_binary.right
     42      1.1  christos 
     43      1.1  christos /* Functions related to demangled name parsing.  */
     44      1.1  christos 
     45      1.1  christos static unsigned int cp_find_first_component_aux (const char *name,
     46      1.1  christos 						 int permissive);
     47      1.1  christos 
     48      1.1  christos static void demangled_name_complaint (const char *name);
     49      1.1  christos 
     50      1.1  christos /* Functions/variables related to overload resolution.  */
     51      1.1  christos 
     52      1.1  christos static int sym_return_val_size = -1;
     53      1.1  christos static int sym_return_val_index;
     54      1.1  christos static struct symbol **sym_return_val;
     55      1.1  christos 
     56      1.1  christos static void overload_list_add_symbol (struct symbol *sym,
     57      1.1  christos 				      const char *oload_name);
     58      1.1  christos 
     59      1.1  christos static void make_symbol_overload_list_using (const char *func_name,
     60  1.1.1.3  christos 					     const char *the_namespace);
     61      1.1  christos 
     62      1.1  christos static void make_symbol_overload_list_qualified (const char *func_name);
     63      1.1  christos 
     64      1.1  christos /* The list of "maint cplus" commands.  */
     65      1.1  christos 
     66      1.1  christos struct cmd_list_element *maint_cplus_cmd_list = NULL;
     67      1.1  christos 
     68      1.1  christos /* The actual commands.  */
     69      1.1  christos 
     70      1.1  christos static void maint_cplus_command (char *arg, int from_tty);
     71      1.1  christos static void first_component_command (char *arg, int from_tty);
     72      1.1  christos 
     73      1.1  christos /* A list of typedefs which should not be substituted by replace_typedefs.  */
     74      1.1  christos static const char * const ignore_typedefs[] =
     75      1.1  christos   {
     76      1.1  christos     "std::istream", "std::iostream", "std::ostream", "std::string"
     77      1.1  christos   };
     78      1.1  christos 
     79      1.1  christos static void
     80      1.1  christos   replace_typedefs (struct demangle_parse_info *info,
     81      1.1  christos 		    struct demangle_component *ret_comp,
     82      1.1  christos 		    canonicalization_ftype *finder,
     83      1.1  christos 		    void *data);
     84      1.1  christos 
     85      1.1  christos /* A convenience function to copy STRING into OBSTACK, returning a pointer
     86      1.1  christos    to the newly allocated string and saving the number of bytes saved in LEN.
     87      1.1  christos 
     88      1.1  christos    It does not copy the terminating '\0' byte!  */
     89      1.1  christos 
     90      1.1  christos static char *
     91      1.1  christos copy_string_to_obstack (struct obstack *obstack, const char *string,
     92      1.1  christos 			long *len)
     93      1.1  christos {
     94      1.1  christos   *len = strlen (string);
     95  1.1.1.4  christos   return (char *) obstack_copy (obstack, string, *len);
     96      1.1  christos }
     97      1.1  christos 
     98      1.1  christos /* A cleanup wrapper for cp_demangled_name_parse_free.  */
     99      1.1  christos 
    100      1.1  christos static void
    101      1.1  christos do_demangled_name_parse_free_cleanup (void *data)
    102      1.1  christos {
    103      1.1  christos   struct demangle_parse_info *info = (struct demangle_parse_info *) data;
    104      1.1  christos 
    105      1.1  christos   cp_demangled_name_parse_free (info);
    106      1.1  christos }
    107      1.1  christos 
    108      1.1  christos /* Create a cleanup for C++ name parsing.  */
    109      1.1  christos 
    110      1.1  christos struct cleanup *
    111      1.1  christos make_cleanup_cp_demangled_name_parse_free (struct demangle_parse_info *info)
    112      1.1  christos {
    113      1.1  christos   return make_cleanup (do_demangled_name_parse_free_cleanup, info);
    114      1.1  christos }
    115      1.1  christos 
    116      1.1  christos /* Return 1 if STRING is clearly already in canonical form.  This
    117      1.1  christos    function is conservative; things which it does not recognize are
    118      1.1  christos    assumed to be non-canonical, and the parser will sort them out
    119      1.1  christos    afterwards.  This speeds up the critical path for alphanumeric
    120      1.1  christos    identifiers.  */
    121      1.1  christos 
    122      1.1  christos static int
    123      1.1  christos cp_already_canonical (const char *string)
    124      1.1  christos {
    125      1.1  christos   /* Identifier start character [a-zA-Z_].  */
    126      1.1  christos   if (!ISIDST (string[0]))
    127      1.1  christos     return 0;
    128      1.1  christos 
    129      1.1  christos   /* These are the only two identifiers which canonicalize to other
    130      1.1  christos      than themselves or an error: unsigned -> unsigned int and
    131      1.1  christos      signed -> int.  */
    132      1.1  christos   if (string[0] == 'u' && strcmp (&string[1], "nsigned") == 0)
    133      1.1  christos     return 0;
    134      1.1  christos   else if (string[0] == 's' && strcmp (&string[1], "igned") == 0)
    135      1.1  christos     return 0;
    136      1.1  christos 
    137      1.1  christos   /* Identifier character [a-zA-Z0-9_].  */
    138      1.1  christos   while (ISIDNUM (string[1]))
    139      1.1  christos     string++;
    140      1.1  christos 
    141      1.1  christos   if (string[1] == '\0')
    142      1.1  christos     return 1;
    143      1.1  christos   else
    144      1.1  christos     return 0;
    145      1.1  christos }
    146      1.1  christos 
    147      1.1  christos /* Inspect the given RET_COMP for its type.  If it is a typedef,
    148      1.1  christos    replace the node with the typedef's tree.
    149      1.1  christos 
    150      1.1  christos    Returns 1 if any typedef substitutions were made, 0 otherwise.  */
    151      1.1  christos 
    152      1.1  christos static int
    153      1.1  christos inspect_type (struct demangle_parse_info *info,
    154      1.1  christos 	      struct demangle_component *ret_comp,
    155      1.1  christos 	      canonicalization_ftype *finder,
    156      1.1  christos 	      void *data)
    157      1.1  christos {
    158      1.1  christos   int i;
    159      1.1  christos   char *name;
    160      1.1  christos   struct symbol *sym;
    161      1.1  christos 
    162      1.1  christos   /* Copy the symbol's name from RET_COMP and look it up
    163      1.1  christos      in the symbol table.  */
    164      1.1  christos   name = (char *) alloca (ret_comp->u.s_name.len + 1);
    165      1.1  christos   memcpy (name, ret_comp->u.s_name.s, ret_comp->u.s_name.len);
    166      1.1  christos   name[ret_comp->u.s_name.len] = '\0';
    167      1.1  christos 
    168      1.1  christos   /* Ignore any typedefs that should not be substituted.  */
    169      1.1  christos   for (i = 0; i < ARRAY_SIZE (ignore_typedefs); ++i)
    170      1.1  christos     {
    171      1.1  christos       if (strcmp (name, ignore_typedefs[i]) == 0)
    172      1.1  christos 	return 0;
    173      1.1  christos     }
    174      1.1  christos 
    175      1.1  christos   sym = NULL;
    176      1.1  christos 
    177  1.1.1.3  christos   TRY
    178  1.1.1.3  christos     {
    179  1.1.1.4  christos       sym = lookup_symbol (name, 0, VAR_DOMAIN, 0).symbol;
    180  1.1.1.3  christos     }
    181  1.1.1.3  christos   CATCH (except, RETURN_MASK_ALL)
    182  1.1.1.3  christos     {
    183  1.1.1.3  christos       return 0;
    184  1.1.1.3  christos     }
    185  1.1.1.3  christos   END_CATCH
    186  1.1.1.3  christos 
    187  1.1.1.3  christos   if (sym != NULL)
    188      1.1  christos     {
    189      1.1  christos       struct type *otype = SYMBOL_TYPE (sym);
    190      1.1  christos 
    191      1.1  christos       if (finder != NULL)
    192      1.1  christos 	{
    193      1.1  christos 	  const char *new_name = (*finder) (otype, data);
    194      1.1  christos 
    195      1.1  christos 	  if (new_name != NULL)
    196      1.1  christos 	    {
    197      1.1  christos 	      ret_comp->u.s_name.s = new_name;
    198      1.1  christos 	      ret_comp->u.s_name.len = strlen (new_name);
    199      1.1  christos 	      return 1;
    200      1.1  christos 	    }
    201      1.1  christos 
    202      1.1  christos 	  return 0;
    203      1.1  christos 	}
    204      1.1  christos 
    205      1.1  christos       /* If the type is a typedef or namespace alias, replace it.  */
    206      1.1  christos       if (TYPE_CODE (otype) == TYPE_CODE_TYPEDEF
    207      1.1  christos 	  || TYPE_CODE (otype) == TYPE_CODE_NAMESPACE)
    208      1.1  christos 	{
    209      1.1  christos 	  long len;
    210      1.1  christos 	  int is_anon;
    211      1.1  christos 	  struct type *type;
    212      1.1  christos 	  struct demangle_parse_info *i;
    213      1.1  christos 	  struct ui_file *buf;
    214      1.1  christos 
    215      1.1  christos 	  /* Get the real type of the typedef.  */
    216      1.1  christos 	  type = check_typedef (otype);
    217      1.1  christos 
    218      1.1  christos 	  /* If the symbol is a namespace and its type name is no different
    219      1.1  christos 	     than the name we looked up, this symbol is not a namespace
    220      1.1  christos 	     alias and does not need to be substituted.  */
    221      1.1  christos 	  if (TYPE_CODE (otype) == TYPE_CODE_NAMESPACE
    222      1.1  christos 	      && strcmp (TYPE_NAME (type), name) == 0)
    223      1.1  christos 	    return 0;
    224      1.1  christos 
    225      1.1  christos 	  is_anon = (TYPE_TAG_NAME (type) == NULL
    226      1.1  christos 		     && (TYPE_CODE (type) == TYPE_CODE_ENUM
    227      1.1  christos 			 || TYPE_CODE (type) == TYPE_CODE_STRUCT
    228      1.1  christos 			 || TYPE_CODE (type) == TYPE_CODE_UNION));
    229      1.1  christos 	  if (is_anon)
    230      1.1  christos 	    {
    231      1.1  christos 	      struct type *last = otype;
    232      1.1  christos 
    233      1.1  christos 	      /* Find the last typedef for the type.  */
    234      1.1  christos 	      while (TYPE_TARGET_TYPE (last) != NULL
    235      1.1  christos 		     && (TYPE_CODE (TYPE_TARGET_TYPE (last))
    236      1.1  christos 			 == TYPE_CODE_TYPEDEF))
    237      1.1  christos 		last = TYPE_TARGET_TYPE (last);
    238      1.1  christos 
    239      1.1  christos 	      /* If there is only one typedef for this anonymous type,
    240      1.1  christos 		 do not substitute it.  */
    241      1.1  christos 	      if (type == otype)
    242      1.1  christos 		return 0;
    243      1.1  christos 	      else
    244      1.1  christos 		/* Use the last typedef seen as the type for this
    245      1.1  christos 		   anonymous type.  */
    246      1.1  christos 		type = last;
    247      1.1  christos 	    }
    248      1.1  christos 
    249      1.1  christos 	  buf = mem_fileopen ();
    250  1.1.1.3  christos 	  TRY
    251      1.1  christos 	  {
    252      1.1  christos 	    type_print (type, "", buf, -1);
    253      1.1  christos 	  }
    254      1.1  christos 
    255      1.1  christos 	  /* If type_print threw an exception, there is little point
    256      1.1  christos 	     in continuing, so just bow out gracefully.  */
    257  1.1.1.3  christos 	  CATCH (except, RETURN_MASK_ERROR)
    258      1.1  christos 	    {
    259      1.1  christos 	      ui_file_delete (buf);
    260      1.1  christos 	      return 0;
    261      1.1  christos 	    }
    262  1.1.1.3  christos 	  END_CATCH
    263      1.1  christos 
    264      1.1  christos 	  name = ui_file_obsavestring (buf, &info->obstack, &len);
    265      1.1  christos 	  ui_file_delete (buf);
    266      1.1  christos 
    267      1.1  christos 	  /* Turn the result into a new tree.  Note that this
    268      1.1  christos 	     tree will contain pointers into NAME, so NAME cannot
    269      1.1  christos 	     be free'd until all typedef conversion is done and
    270      1.1  christos 	     the final result is converted into a string.  */
    271      1.1  christos 	  i = cp_demangled_name_to_comp (name, NULL);
    272      1.1  christos 	  if (i != NULL)
    273      1.1  christos 	    {
    274      1.1  christos 	      /* Merge the two trees.  */
    275      1.1  christos 	      cp_merge_demangle_parse_infos (info, ret_comp, i);
    276      1.1  christos 
    277      1.1  christos 	      /* Replace any newly introduced typedefs -- but not
    278      1.1  christos 		 if the type is anonymous (that would lead to infinite
    279      1.1  christos 		 looping).  */
    280      1.1  christos 	      if (!is_anon)
    281      1.1  christos 		replace_typedefs (info, ret_comp, finder, data);
    282      1.1  christos 	    }
    283      1.1  christos 	  else
    284      1.1  christos 	    {
    285      1.1  christos 	      /* This shouldn't happen unless the type printer has
    286      1.1  christos 		 output something that the name parser cannot grok.
    287      1.1  christos 		 Nonetheless, an ounce of prevention...
    288      1.1  christos 
    289      1.1  christos 		 Canonicalize the name again, and store it in the
    290      1.1  christos 		 current node (RET_COMP).  */
    291      1.1  christos 	      char *canon = cp_canonicalize_string_no_typedefs (name);
    292      1.1  christos 
    293      1.1  christos 	      if (canon != NULL)
    294      1.1  christos 		{
    295      1.1  christos 		  /* Copy the canonicalization into the obstack and
    296      1.1  christos 		     free CANON.  */
    297      1.1  christos 		  name = copy_string_to_obstack (&info->obstack, canon, &len);
    298      1.1  christos 		  xfree (canon);
    299      1.1  christos 		}
    300      1.1  christos 
    301      1.1  christos 	      ret_comp->u.s_name.s = name;
    302      1.1  christos 	      ret_comp->u.s_name.len = len;
    303      1.1  christos 	    }
    304      1.1  christos 
    305      1.1  christos 	  return 1;
    306      1.1  christos 	}
    307      1.1  christos     }
    308      1.1  christos 
    309      1.1  christos   return 0;
    310      1.1  christos }
    311      1.1  christos 
    312      1.1  christos /* Replace any typedefs appearing in the qualified name
    313      1.1  christos    (DEMANGLE_COMPONENT_QUAL_NAME) represented in RET_COMP for the name parse
    314      1.1  christos    given in INFO.  */
    315      1.1  christos 
    316      1.1  christos static void
    317      1.1  christos replace_typedefs_qualified_name (struct demangle_parse_info *info,
    318      1.1  christos 				 struct demangle_component *ret_comp,
    319      1.1  christos 				 canonicalization_ftype *finder,
    320      1.1  christos 				 void *data)
    321      1.1  christos {
    322      1.1  christos   long len;
    323      1.1  christos   char *name;
    324      1.1  christos   struct ui_file *buf = mem_fileopen ();
    325      1.1  christos   struct demangle_component *comp = ret_comp;
    326      1.1  christos 
    327      1.1  christos   /* Walk each node of the qualified name, reconstructing the name of
    328      1.1  christos      this element.  With every node, check for any typedef substitutions.
    329      1.1  christos      If a substitution has occurred, replace the qualified name node
    330      1.1  christos      with a DEMANGLE_COMPONENT_NAME node representing the new, typedef-
    331      1.1  christos      substituted name.  */
    332      1.1  christos   while (comp->type == DEMANGLE_COMPONENT_QUAL_NAME)
    333      1.1  christos     {
    334      1.1  christos       if (d_left (comp)->type == DEMANGLE_COMPONENT_NAME)
    335      1.1  christos 	{
    336  1.1.1.3  christos 	  struct demangle_component newobj;
    337      1.1  christos 
    338      1.1  christos 	  ui_file_write (buf, d_left (comp)->u.s_name.s,
    339      1.1  christos 			 d_left (comp)->u.s_name.len);
    340      1.1  christos 	  name = ui_file_obsavestring (buf, &info->obstack, &len);
    341  1.1.1.3  christos 	  newobj.type = DEMANGLE_COMPONENT_NAME;
    342  1.1.1.3  christos 	  newobj.u.s_name.s = name;
    343  1.1.1.3  christos 	  newobj.u.s_name.len = len;
    344  1.1.1.3  christos 	  if (inspect_type (info, &newobj, finder, data))
    345      1.1  christos 	    {
    346      1.1  christos 	      char *n, *s;
    347      1.1  christos 	      long slen;
    348      1.1  christos 
    349      1.1  christos 	      /* A typedef was substituted in NEW.  Convert it to a
    350      1.1  christos 		 string and replace the top DEMANGLE_COMPONENT_QUAL_NAME
    351      1.1  christos 		 node.  */
    352      1.1  christos 
    353      1.1  christos 	      ui_file_rewind (buf);
    354  1.1.1.3  christos 	      n = cp_comp_to_string (&newobj, 100);
    355      1.1  christos 	      if (n == NULL)
    356      1.1  christos 		{
    357      1.1  christos 		  /* If something went astray, abort typedef substitutions.  */
    358      1.1  christos 		  ui_file_delete (buf);
    359      1.1  christos 		  return;
    360      1.1  christos 		}
    361      1.1  christos 
    362      1.1  christos 	      s = copy_string_to_obstack (&info->obstack, n, &slen);
    363      1.1  christos 	      xfree (n);
    364      1.1  christos 
    365      1.1  christos 	      d_left (ret_comp)->type = DEMANGLE_COMPONENT_NAME;
    366      1.1  christos 	      d_left (ret_comp)->u.s_name.s = s;
    367      1.1  christos 	      d_left (ret_comp)->u.s_name.len = slen;
    368      1.1  christos 	      d_right (ret_comp) = d_right (comp);
    369      1.1  christos 	      comp = ret_comp;
    370      1.1  christos 	      continue;
    371      1.1  christos 	    }
    372      1.1  christos 	}
    373      1.1  christos       else
    374      1.1  christos 	{
    375      1.1  christos 	  /* The current node is not a name, so simply replace any
    376      1.1  christos 	     typedefs in it.  Then print it to the stream to continue
    377      1.1  christos 	     checking for more typedefs in the tree.  */
    378      1.1  christos 	  replace_typedefs (info, d_left (comp), finder, data);
    379      1.1  christos 	  name = cp_comp_to_string (d_left (comp), 100);
    380      1.1  christos 	  if (name == NULL)
    381      1.1  christos 	    {
    382      1.1  christos 	      /* If something went astray, abort typedef substitutions.  */
    383      1.1  christos 	      ui_file_delete (buf);
    384      1.1  christos 	      return;
    385      1.1  christos 	    }
    386      1.1  christos 	  fputs_unfiltered (name, buf);
    387      1.1  christos 	  xfree (name);
    388      1.1  christos 	}
    389      1.1  christos 
    390      1.1  christos       ui_file_write (buf, "::", 2);
    391      1.1  christos       comp = d_right (comp);
    392      1.1  christos     }
    393      1.1  christos 
    394      1.1  christos   /* If the next component is DEMANGLE_COMPONENT_NAME, save the qualified
    395      1.1  christos      name assembled above and append the name given by COMP.  Then use this
    396      1.1  christos      reassembled name to check for a typedef.  */
    397      1.1  christos 
    398      1.1  christos   if (comp->type == DEMANGLE_COMPONENT_NAME)
    399      1.1  christos     {
    400      1.1  christos       ui_file_write (buf, comp->u.s_name.s, comp->u.s_name.len);
    401      1.1  christos       name = ui_file_obsavestring (buf, &info->obstack, &len);
    402      1.1  christos 
    403      1.1  christos       /* Replace the top (DEMANGLE_COMPONENT_QUAL_NAME) node
    404      1.1  christos 	 with a DEMANGLE_COMPONENT_NAME node containing the whole
    405      1.1  christos 	 name.  */
    406      1.1  christos       ret_comp->type = DEMANGLE_COMPONENT_NAME;
    407      1.1  christos       ret_comp->u.s_name.s = name;
    408      1.1  christos       ret_comp->u.s_name.len = len;
    409      1.1  christos       inspect_type (info, ret_comp, finder, data);
    410      1.1  christos     }
    411      1.1  christos   else
    412      1.1  christos     replace_typedefs (info, comp, finder, data);
    413      1.1  christos 
    414      1.1  christos   ui_file_delete (buf);
    415      1.1  christos }
    416      1.1  christos 
    417      1.1  christos 
    418      1.1  christos /* A function to check const and volatile qualifiers for argument types.
    419      1.1  christos 
    420      1.1  christos    "Parameter declarations that differ only in the presence
    421      1.1  christos    or absence of `const' and/or `volatile' are equivalent."
    422      1.1  christos    C++ Standard N3290, clause 13.1.3 #4.  */
    423      1.1  christos 
    424      1.1  christos static void
    425      1.1  christos check_cv_qualifiers (struct demangle_component *ret_comp)
    426      1.1  christos {
    427      1.1  christos   while (d_left (ret_comp) != NULL
    428      1.1  christos 	 && (d_left (ret_comp)->type == DEMANGLE_COMPONENT_CONST
    429      1.1  christos 	     || d_left (ret_comp)->type == DEMANGLE_COMPONENT_VOLATILE))
    430      1.1  christos     {
    431      1.1  christos       d_left (ret_comp) = d_left (d_left (ret_comp));
    432      1.1  christos     }
    433      1.1  christos }
    434      1.1  christos 
    435      1.1  christos /* Walk the parse tree given by RET_COMP, replacing any typedefs with
    436      1.1  christos    their basic types.  */
    437      1.1  christos 
    438      1.1  christos static void
    439      1.1  christos replace_typedefs (struct demangle_parse_info *info,
    440      1.1  christos 		  struct demangle_component *ret_comp,
    441      1.1  christos 		  canonicalization_ftype *finder,
    442      1.1  christos 		  void *data)
    443      1.1  christos {
    444      1.1  christos   if (ret_comp)
    445      1.1  christos     {
    446      1.1  christos       if (finder != NULL
    447      1.1  christos 	  && (ret_comp->type == DEMANGLE_COMPONENT_NAME
    448      1.1  christos 	      || ret_comp->type == DEMANGLE_COMPONENT_QUAL_NAME
    449      1.1  christos 	      || ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE
    450      1.1  christos 	      || ret_comp->type == DEMANGLE_COMPONENT_BUILTIN_TYPE))
    451      1.1  christos 	{
    452      1.1  christos 	  char *local_name = cp_comp_to_string (ret_comp, 10);
    453      1.1  christos 
    454      1.1  christos 	  if (local_name != NULL)
    455      1.1  christos 	    {
    456  1.1.1.3  christos 	      struct symbol *sym = NULL;
    457      1.1  christos 
    458      1.1  christos 	      sym = NULL;
    459  1.1.1.3  christos 	      TRY
    460      1.1  christos 		{
    461  1.1.1.4  christos 		  sym = lookup_symbol (local_name, 0, VAR_DOMAIN, 0).symbol;
    462      1.1  christos 		}
    463  1.1.1.3  christos 	      CATCH (except, RETURN_MASK_ALL)
    464  1.1.1.3  christos 		{
    465  1.1.1.3  christos 		}
    466  1.1.1.3  christos 	      END_CATCH
    467  1.1.1.3  christos 
    468      1.1  christos 	      xfree (local_name);
    469      1.1  christos 
    470  1.1.1.3  christos 	      if (sym != NULL)
    471      1.1  christos 		{
    472      1.1  christos 		  struct type *otype = SYMBOL_TYPE (sym);
    473      1.1  christos 		  const char *new_name = (*finder) (otype, data);
    474      1.1  christos 
    475      1.1  christos 		  if (new_name != NULL)
    476      1.1  christos 		    {
    477      1.1  christos 		      ret_comp->type = DEMANGLE_COMPONENT_NAME;
    478      1.1  christos 		      ret_comp->u.s_name.s = new_name;
    479      1.1  christos 		      ret_comp->u.s_name.len = strlen (new_name);
    480      1.1  christos 		      return;
    481      1.1  christos 		    }
    482      1.1  christos 		}
    483      1.1  christos 	    }
    484      1.1  christos 	}
    485      1.1  christos 
    486      1.1  christos       switch (ret_comp->type)
    487      1.1  christos 	{
    488      1.1  christos 	case DEMANGLE_COMPONENT_ARGLIST:
    489      1.1  christos 	  check_cv_qualifiers (ret_comp);
    490      1.1  christos 	  /* Fall through */
    491      1.1  christos 
    492      1.1  christos 	case DEMANGLE_COMPONENT_FUNCTION_TYPE:
    493      1.1  christos 	case DEMANGLE_COMPONENT_TEMPLATE:
    494      1.1  christos 	case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
    495      1.1  christos 	case DEMANGLE_COMPONENT_TYPED_NAME:
    496      1.1  christos 	  replace_typedefs (info, d_left (ret_comp), finder, data);
    497      1.1  christos 	  replace_typedefs (info, d_right (ret_comp), finder, data);
    498      1.1  christos 	  break;
    499      1.1  christos 
    500      1.1  christos 	case DEMANGLE_COMPONENT_NAME:
    501      1.1  christos 	  inspect_type (info, ret_comp, finder, data);
    502      1.1  christos 	  break;
    503      1.1  christos 
    504      1.1  christos 	case DEMANGLE_COMPONENT_QUAL_NAME:
    505      1.1  christos 	  replace_typedefs_qualified_name (info, ret_comp, finder, data);
    506      1.1  christos 	  break;
    507      1.1  christos 
    508      1.1  christos 	case DEMANGLE_COMPONENT_LOCAL_NAME:
    509      1.1  christos 	case DEMANGLE_COMPONENT_CTOR:
    510      1.1  christos 	case DEMANGLE_COMPONENT_ARRAY_TYPE:
    511      1.1  christos 	case DEMANGLE_COMPONENT_PTRMEM_TYPE:
    512      1.1  christos 	  replace_typedefs (info, d_right (ret_comp), finder, data);
    513      1.1  christos 	  break;
    514      1.1  christos 
    515      1.1  christos 	case DEMANGLE_COMPONENT_CONST:
    516      1.1  christos 	case DEMANGLE_COMPONENT_RESTRICT:
    517      1.1  christos 	case DEMANGLE_COMPONENT_VOLATILE:
    518      1.1  christos 	case DEMANGLE_COMPONENT_VOLATILE_THIS:
    519      1.1  christos 	case DEMANGLE_COMPONENT_CONST_THIS:
    520      1.1  christos 	case DEMANGLE_COMPONENT_RESTRICT_THIS:
    521      1.1  christos 	case DEMANGLE_COMPONENT_POINTER:
    522      1.1  christos 	case DEMANGLE_COMPONENT_REFERENCE:
    523      1.1  christos 	  replace_typedefs (info, d_left (ret_comp), finder, data);
    524      1.1  christos 	  break;
    525      1.1  christos 
    526      1.1  christos 	default:
    527      1.1  christos 	  break;
    528      1.1  christos 	}
    529      1.1  christos     }
    530      1.1  christos }
    531      1.1  christos 
    532      1.1  christos /* Parse STRING and convert it to canonical form, resolving any typedefs.
    533      1.1  christos    If parsing fails, or if STRING is already canonical, return NULL.
    534      1.1  christos    Otherwise return the canonical form.  The return value is allocated via
    535      1.1  christos    xmalloc.  If FINDER is not NULL, then type components are passed to
    536      1.1  christos    FINDER to be looked up.  DATA is passed verbatim to FINDER.  */
    537      1.1  christos 
    538      1.1  christos char *
    539      1.1  christos cp_canonicalize_string_full (const char *string,
    540      1.1  christos 			     canonicalization_ftype *finder,
    541      1.1  christos 			     void *data)
    542      1.1  christos {
    543      1.1  christos   char *ret;
    544      1.1  christos   unsigned int estimated_len;
    545      1.1  christos   struct demangle_parse_info *info;
    546      1.1  christos 
    547      1.1  christos   ret = NULL;
    548      1.1  christos   estimated_len = strlen (string) * 2;
    549      1.1  christos   info = cp_demangled_name_to_comp (string, NULL);
    550      1.1  christos   if (info != NULL)
    551      1.1  christos     {
    552      1.1  christos       /* Replace all the typedefs in the tree.  */
    553      1.1  christos       replace_typedefs (info, info->tree, finder, data);
    554      1.1  christos 
    555      1.1  christos       /* Convert the tree back into a string.  */
    556      1.1  christos       ret = cp_comp_to_string (info->tree, estimated_len);
    557      1.1  christos       gdb_assert (ret != NULL);
    558      1.1  christos 
    559      1.1  christos       /* Free the parse information.  */
    560      1.1  christos       cp_demangled_name_parse_free (info);
    561      1.1  christos 
    562      1.1  christos       /* Finally, compare the original string with the computed
    563      1.1  christos 	 name, returning NULL if they are the same.  */
    564      1.1  christos       if (strcmp (string, ret) == 0)
    565      1.1  christos 	{
    566      1.1  christos 	  xfree (ret);
    567      1.1  christos 	  return NULL;
    568      1.1  christos 	}
    569      1.1  christos     }
    570      1.1  christos 
    571      1.1  christos   return ret;
    572      1.1  christos }
    573      1.1  christos 
    574      1.1  christos /* Like cp_canonicalize_string_full, but always passes NULL for
    575      1.1  christos    FINDER.  */
    576      1.1  christos 
    577      1.1  christos char *
    578      1.1  christos cp_canonicalize_string_no_typedefs (const char *string)
    579      1.1  christos {
    580      1.1  christos   return cp_canonicalize_string_full (string, NULL, NULL);
    581      1.1  christos }
    582      1.1  christos 
    583      1.1  christos /* Parse STRING and convert it to canonical form.  If parsing fails,
    584      1.1  christos    or if STRING is already canonical, return NULL.  Otherwise return
    585      1.1  christos    the canonical form.  The return value is allocated via xmalloc.  */
    586      1.1  christos 
    587      1.1  christos char *
    588      1.1  christos cp_canonicalize_string (const char *string)
    589      1.1  christos {
    590      1.1  christos   struct demangle_parse_info *info;
    591      1.1  christos   unsigned int estimated_len;
    592      1.1  christos   char *ret;
    593      1.1  christos 
    594      1.1  christos   if (cp_already_canonical (string))
    595      1.1  christos     return NULL;
    596      1.1  christos 
    597      1.1  christos   info = cp_demangled_name_to_comp (string, NULL);
    598      1.1  christos   if (info == NULL)
    599      1.1  christos     return NULL;
    600      1.1  christos 
    601      1.1  christos   estimated_len = strlen (string) * 2;
    602      1.1  christos   ret = cp_comp_to_string (info->tree, estimated_len);
    603      1.1  christos   cp_demangled_name_parse_free (info);
    604      1.1  christos 
    605      1.1  christos   if (ret == NULL)
    606      1.1  christos     {
    607      1.1  christos       warning (_("internal error: string \"%s\" failed to be canonicalized"),
    608      1.1  christos 	       string);
    609      1.1  christos       return NULL;
    610      1.1  christos     }
    611      1.1  christos 
    612      1.1  christos   if (strcmp (string, ret) == 0)
    613      1.1  christos     {
    614      1.1  christos       xfree (ret);
    615      1.1  christos       return NULL;
    616      1.1  christos     }
    617      1.1  christos 
    618      1.1  christos   return ret;
    619      1.1  christos }
    620      1.1  christos 
    621      1.1  christos /* Convert a mangled name to a demangle_component tree.  *MEMORY is
    622      1.1  christos    set to the block of used memory that should be freed when finished
    623      1.1  christos    with the tree.  DEMANGLED_P is set to the char * that should be
    624      1.1  christos    freed when finished with the tree, or NULL if none was needed.
    625      1.1  christos    OPTIONS will be passed to the demangler.  */
    626      1.1  christos 
    627      1.1  christos static struct demangle_parse_info *
    628      1.1  christos mangled_name_to_comp (const char *mangled_name, int options,
    629      1.1  christos 		      void **memory, char **demangled_p)
    630      1.1  christos {
    631      1.1  christos   char *demangled_name;
    632      1.1  christos   struct demangle_parse_info *info;
    633      1.1  christos 
    634      1.1  christos   /* If it looks like a v3 mangled name, then try to go directly
    635      1.1  christos      to trees.  */
    636      1.1  christos   if (mangled_name[0] == '_' && mangled_name[1] == 'Z')
    637      1.1  christos     {
    638      1.1  christos       struct demangle_component *ret;
    639      1.1  christos 
    640      1.1  christos       ret = cplus_demangle_v3_components (mangled_name,
    641      1.1  christos 					  options, memory);
    642      1.1  christos       if (ret)
    643      1.1  christos 	{
    644      1.1  christos 	  info = cp_new_demangle_parse_info ();
    645      1.1  christos 	  info->tree = ret;
    646      1.1  christos 	  *demangled_p = NULL;
    647      1.1  christos 	  return info;
    648      1.1  christos 	}
    649      1.1  christos     }
    650      1.1  christos 
    651      1.1  christos   /* If it doesn't, or if that failed, then try to demangle the
    652      1.1  christos      name.  */
    653      1.1  christos   demangled_name = gdb_demangle (mangled_name, options);
    654      1.1  christos   if (demangled_name == NULL)
    655      1.1  christos    return NULL;
    656      1.1  christos 
    657      1.1  christos   /* If we could demangle the name, parse it to build the component
    658      1.1  christos      tree.  */
    659      1.1  christos   info = cp_demangled_name_to_comp (demangled_name, NULL);
    660      1.1  christos 
    661      1.1  christos   if (info == NULL)
    662      1.1  christos     {
    663      1.1  christos       xfree (demangled_name);
    664      1.1  christos       return NULL;
    665      1.1  christos     }
    666      1.1  christos 
    667      1.1  christos   *demangled_p = demangled_name;
    668      1.1  christos   return info;
    669      1.1  christos }
    670      1.1  christos 
    671      1.1  christos /* Return the name of the class containing method PHYSNAME.  */
    672      1.1  christos 
    673      1.1  christos char *
    674      1.1  christos cp_class_name_from_physname (const char *physname)
    675      1.1  christos {
    676      1.1  christos   void *storage = NULL;
    677      1.1  christos   char *demangled_name = NULL, *ret;
    678      1.1  christos   struct demangle_component *ret_comp, *prev_comp, *cur_comp;
    679      1.1  christos   struct demangle_parse_info *info;
    680      1.1  christos   int done;
    681      1.1  christos 
    682      1.1  christos   info = mangled_name_to_comp (physname, DMGL_ANSI,
    683      1.1  christos 			       &storage, &demangled_name);
    684      1.1  christos   if (info == NULL)
    685      1.1  christos     return NULL;
    686      1.1  christos 
    687      1.1  christos   done = 0;
    688      1.1  christos   ret_comp = info->tree;
    689      1.1  christos 
    690      1.1  christos   /* First strip off any qualifiers, if we have a function or
    691      1.1  christos      method.  */
    692      1.1  christos   while (!done)
    693      1.1  christos     switch (ret_comp->type)
    694      1.1  christos       {
    695      1.1  christos       case DEMANGLE_COMPONENT_CONST:
    696      1.1  christos       case DEMANGLE_COMPONENT_RESTRICT:
    697      1.1  christos       case DEMANGLE_COMPONENT_VOLATILE:
    698      1.1  christos       case DEMANGLE_COMPONENT_CONST_THIS:
    699      1.1  christos       case DEMANGLE_COMPONENT_RESTRICT_THIS:
    700      1.1  christos       case DEMANGLE_COMPONENT_VOLATILE_THIS:
    701      1.1  christos       case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
    702      1.1  christos         ret_comp = d_left (ret_comp);
    703      1.1  christos         break;
    704      1.1  christos       default:
    705      1.1  christos 	done = 1;
    706      1.1  christos 	break;
    707      1.1  christos       }
    708      1.1  christos 
    709      1.1  christos   /* If what we have now is a function, discard the argument list.  */
    710      1.1  christos   if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
    711      1.1  christos     ret_comp = d_left (ret_comp);
    712      1.1  christos 
    713      1.1  christos   /* If what we have now is a template, strip off the template
    714      1.1  christos      arguments.  The left subtree may be a qualified name.  */
    715      1.1  christos   if (ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE)
    716      1.1  christos     ret_comp = d_left (ret_comp);
    717      1.1  christos 
    718      1.1  christos   /* What we have now should be a name, possibly qualified.
    719      1.1  christos      Additional qualifiers could live in the left subtree or the right
    720      1.1  christos      subtree.  Find the last piece.  */
    721      1.1  christos   done = 0;
    722      1.1  christos   prev_comp = NULL;
    723      1.1  christos   cur_comp = ret_comp;
    724      1.1  christos   while (!done)
    725      1.1  christos     switch (cur_comp->type)
    726      1.1  christos       {
    727      1.1  christos       case DEMANGLE_COMPONENT_QUAL_NAME:
    728      1.1  christos       case DEMANGLE_COMPONENT_LOCAL_NAME:
    729      1.1  christos 	prev_comp = cur_comp;
    730      1.1  christos         cur_comp = d_right (cur_comp);
    731      1.1  christos         break;
    732      1.1  christos       case DEMANGLE_COMPONENT_TEMPLATE:
    733      1.1  christos       case DEMANGLE_COMPONENT_NAME:
    734      1.1  christos       case DEMANGLE_COMPONENT_CTOR:
    735      1.1  christos       case DEMANGLE_COMPONENT_DTOR:
    736      1.1  christos       case DEMANGLE_COMPONENT_OPERATOR:
    737      1.1  christos       case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
    738      1.1  christos 	done = 1;
    739      1.1  christos 	break;
    740      1.1  christos       default:
    741      1.1  christos 	done = 1;
    742      1.1  christos 	cur_comp = NULL;
    743      1.1  christos 	break;
    744      1.1  christos       }
    745      1.1  christos 
    746      1.1  christos   ret = NULL;
    747      1.1  christos   if (cur_comp != NULL && prev_comp != NULL)
    748      1.1  christos     {
    749      1.1  christos       /* We want to discard the rightmost child of PREV_COMP.  */
    750      1.1  christos       *prev_comp = *d_left (prev_comp);
    751      1.1  christos       /* The ten is completely arbitrary; we don't have a good
    752      1.1  christos 	 estimate.  */
    753      1.1  christos       ret = cp_comp_to_string (ret_comp, 10);
    754      1.1  christos     }
    755      1.1  christos 
    756      1.1  christos   xfree (storage);
    757      1.1  christos   xfree (demangled_name);
    758      1.1  christos   cp_demangled_name_parse_free (info);
    759      1.1  christos   return ret;
    760      1.1  christos }
    761      1.1  christos 
    762      1.1  christos /* Return the child of COMP which is the basename of a method,
    763      1.1  christos    variable, et cetera.  All scope qualifiers are discarded, but
    764      1.1  christos    template arguments will be included.  The component tree may be
    765      1.1  christos    modified.  */
    766      1.1  christos 
    767      1.1  christos static struct demangle_component *
    768      1.1  christos unqualified_name_from_comp (struct demangle_component *comp)
    769      1.1  christos {
    770      1.1  christos   struct demangle_component *ret_comp = comp, *last_template;
    771      1.1  christos   int done;
    772      1.1  christos 
    773      1.1  christos   done = 0;
    774      1.1  christos   last_template = NULL;
    775      1.1  christos   while (!done)
    776      1.1  christos     switch (ret_comp->type)
    777      1.1  christos       {
    778      1.1  christos       case DEMANGLE_COMPONENT_QUAL_NAME:
    779      1.1  christos       case DEMANGLE_COMPONENT_LOCAL_NAME:
    780      1.1  christos         ret_comp = d_right (ret_comp);
    781      1.1  christos         break;
    782      1.1  christos       case DEMANGLE_COMPONENT_TYPED_NAME:
    783      1.1  christos         ret_comp = d_left (ret_comp);
    784      1.1  christos         break;
    785      1.1  christos       case DEMANGLE_COMPONENT_TEMPLATE:
    786      1.1  christos 	gdb_assert (last_template == NULL);
    787      1.1  christos 	last_template = ret_comp;
    788      1.1  christos 	ret_comp = d_left (ret_comp);
    789      1.1  christos 	break;
    790      1.1  christos       case DEMANGLE_COMPONENT_CONST:
    791      1.1  christos       case DEMANGLE_COMPONENT_RESTRICT:
    792      1.1  christos       case DEMANGLE_COMPONENT_VOLATILE:
    793      1.1  christos       case DEMANGLE_COMPONENT_CONST_THIS:
    794      1.1  christos       case DEMANGLE_COMPONENT_RESTRICT_THIS:
    795      1.1  christos       case DEMANGLE_COMPONENT_VOLATILE_THIS:
    796      1.1  christos       case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
    797      1.1  christos         ret_comp = d_left (ret_comp);
    798      1.1  christos         break;
    799      1.1  christos       case DEMANGLE_COMPONENT_NAME:
    800      1.1  christos       case DEMANGLE_COMPONENT_CTOR:
    801      1.1  christos       case DEMANGLE_COMPONENT_DTOR:
    802      1.1  christos       case DEMANGLE_COMPONENT_OPERATOR:
    803      1.1  christos       case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
    804      1.1  christos 	done = 1;
    805      1.1  christos 	break;
    806      1.1  christos       default:
    807      1.1  christos 	return NULL;
    808      1.1  christos 	break;
    809      1.1  christos       }
    810      1.1  christos 
    811      1.1  christos   if (last_template)
    812      1.1  christos     {
    813      1.1  christos       d_left (last_template) = ret_comp;
    814      1.1  christos       return last_template;
    815      1.1  christos     }
    816      1.1  christos 
    817      1.1  christos   return ret_comp;
    818      1.1  christos }
    819      1.1  christos 
    820      1.1  christos /* Return the name of the method whose linkage name is PHYSNAME.  */
    821      1.1  christos 
    822      1.1  christos char *
    823      1.1  christos method_name_from_physname (const char *physname)
    824      1.1  christos {
    825      1.1  christos   void *storage = NULL;
    826      1.1  christos   char *demangled_name = NULL, *ret;
    827      1.1  christos   struct demangle_component *ret_comp;
    828      1.1  christos   struct demangle_parse_info *info;
    829      1.1  christos 
    830      1.1  christos   info = mangled_name_to_comp (physname, DMGL_ANSI,
    831      1.1  christos 			       &storage, &demangled_name);
    832      1.1  christos   if (info == NULL)
    833      1.1  christos     return NULL;
    834      1.1  christos 
    835      1.1  christos   ret_comp = unqualified_name_from_comp (info->tree);
    836      1.1  christos 
    837      1.1  christos   ret = NULL;
    838      1.1  christos   if (ret_comp != NULL)
    839      1.1  christos     /* The ten is completely arbitrary; we don't have a good
    840      1.1  christos        estimate.  */
    841      1.1  christos     ret = cp_comp_to_string (ret_comp, 10);
    842      1.1  christos 
    843      1.1  christos   xfree (storage);
    844      1.1  christos   xfree (demangled_name);
    845      1.1  christos   cp_demangled_name_parse_free (info);
    846      1.1  christos   return ret;
    847      1.1  christos }
    848      1.1  christos 
    849      1.1  christos /* If FULL_NAME is the demangled name of a C++ function (including an
    850      1.1  christos    arg list, possibly including namespace/class qualifications),
    851      1.1  christos    return a new string containing only the function name (without the
    852      1.1  christos    arg list/class qualifications).  Otherwise, return NULL.  The
    853      1.1  christos    caller is responsible for freeing the memory in question.  */
    854      1.1  christos 
    855      1.1  christos char *
    856      1.1  christos cp_func_name (const char *full_name)
    857      1.1  christos {
    858      1.1  christos   char *ret;
    859      1.1  christos   struct demangle_component *ret_comp;
    860      1.1  christos   struct demangle_parse_info *info;
    861      1.1  christos 
    862      1.1  christos   info = cp_demangled_name_to_comp (full_name, NULL);
    863      1.1  christos   if (!info)
    864      1.1  christos     return NULL;
    865      1.1  christos 
    866      1.1  christos   ret_comp = unqualified_name_from_comp (info->tree);
    867      1.1  christos 
    868      1.1  christos   ret = NULL;
    869      1.1  christos   if (ret_comp != NULL)
    870      1.1  christos     ret = cp_comp_to_string (ret_comp, 10);
    871      1.1  christos 
    872      1.1  christos   cp_demangled_name_parse_free (info);
    873      1.1  christos   return ret;
    874      1.1  christos }
    875      1.1  christos 
    876      1.1  christos /* DEMANGLED_NAME is the name of a function, including parameters and
    877      1.1  christos    (optionally) a return type.  Return the name of the function without
    878      1.1  christos    parameters or return type, or NULL if we can not parse the name.  */
    879      1.1  christos 
    880      1.1  christos char *
    881      1.1  christos cp_remove_params (const char *demangled_name)
    882      1.1  christos {
    883      1.1  christos   int done = 0;
    884      1.1  christos   struct demangle_component *ret_comp;
    885      1.1  christos   struct demangle_parse_info *info;
    886      1.1  christos   char *ret = NULL;
    887      1.1  christos 
    888      1.1  christos   if (demangled_name == NULL)
    889      1.1  christos     return NULL;
    890      1.1  christos 
    891      1.1  christos   info = cp_demangled_name_to_comp (demangled_name, NULL);
    892      1.1  christos   if (info == NULL)
    893      1.1  christos     return NULL;
    894      1.1  christos 
    895      1.1  christos   /* First strip off any qualifiers, if we have a function or method.  */
    896      1.1  christos   ret_comp = info->tree;
    897      1.1  christos   while (!done)
    898      1.1  christos     switch (ret_comp->type)
    899      1.1  christos       {
    900      1.1  christos       case DEMANGLE_COMPONENT_CONST:
    901      1.1  christos       case DEMANGLE_COMPONENT_RESTRICT:
    902      1.1  christos       case DEMANGLE_COMPONENT_VOLATILE:
    903      1.1  christos       case DEMANGLE_COMPONENT_CONST_THIS:
    904      1.1  christos       case DEMANGLE_COMPONENT_RESTRICT_THIS:
    905      1.1  christos       case DEMANGLE_COMPONENT_VOLATILE_THIS:
    906      1.1  christos       case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
    907      1.1  christos         ret_comp = d_left (ret_comp);
    908      1.1  christos         break;
    909      1.1  christos       default:
    910      1.1  christos 	done = 1;
    911      1.1  christos 	break;
    912      1.1  christos       }
    913      1.1  christos 
    914      1.1  christos   /* What we have now should be a function.  Return its name.  */
    915      1.1  christos   if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
    916      1.1  christos     ret = cp_comp_to_string (d_left (ret_comp), 10);
    917      1.1  christos 
    918      1.1  christos   cp_demangled_name_parse_free (info);
    919      1.1  christos   return ret;
    920      1.1  christos }
    921      1.1  christos 
    922      1.1  christos /* Here are some random pieces of trivia to keep in mind while trying
    923      1.1  christos    to take apart demangled names:
    924      1.1  christos 
    925      1.1  christos    - Names can contain function arguments or templates, so the process
    926      1.1  christos      has to be, to some extent recursive: maybe keep track of your
    927      1.1  christos      depth based on encountering <> and ().
    928      1.1  christos 
    929      1.1  christos    - Parentheses don't just have to happen at the end of a name: they
    930      1.1  christos      can occur even if the name in question isn't a function, because
    931      1.1  christos      a template argument might be a type that's a function.
    932      1.1  christos 
    933      1.1  christos    - Conversely, even if you're trying to deal with a function, its
    934      1.1  christos      demangled name might not end with ')': it could be a const or
    935      1.1  christos      volatile class method, in which case it ends with "const" or
    936      1.1  christos      "volatile".
    937      1.1  christos 
    938      1.1  christos    - Parentheses are also used in anonymous namespaces: a variable
    939      1.1  christos      'foo' in an anonymous namespace gets demangled as "(anonymous
    940      1.1  christos      namespace)::foo".
    941      1.1  christos 
    942      1.1  christos    - And operator names can contain parentheses or angle brackets.  */
    943      1.1  christos 
    944      1.1  christos /* FIXME: carlton/2003-03-13: We have several functions here with
    945      1.1  christos    overlapping functionality; can we combine them?  Also, do they
    946      1.1  christos    handle all the above considerations correctly?  */
    947      1.1  christos 
    948      1.1  christos 
    949      1.1  christos /* This returns the length of first component of NAME, which should be
    950      1.1  christos    the demangled name of a C++ variable/function/method/etc.
    951      1.1  christos    Specifically, it returns the index of the first colon forming the
    952      1.1  christos    boundary of the first component: so, given 'A::foo' or 'A::B::foo'
    953      1.1  christos    it returns the 1, and given 'foo', it returns 0.  */
    954      1.1  christos 
    955      1.1  christos /* The character in NAME indexed by the return value is guaranteed to
    956      1.1  christos    always be either ':' or '\0'.  */
    957      1.1  christos 
    958      1.1  christos /* NOTE: carlton/2003-03-13: This function is currently only intended
    959      1.1  christos    for internal use: it's probably not entirely safe when called on
    960      1.1  christos    user-generated input, because some of the 'index += 2' lines in
    961      1.1  christos    cp_find_first_component_aux might go past the end of malformed
    962      1.1  christos    input.  */
    963      1.1  christos 
    964      1.1  christos unsigned int
    965      1.1  christos cp_find_first_component (const char *name)
    966      1.1  christos {
    967      1.1  christos   return cp_find_first_component_aux (name, 0);
    968      1.1  christos }
    969      1.1  christos 
    970      1.1  christos /* Helper function for cp_find_first_component.  Like that function,
    971      1.1  christos    it returns the length of the first component of NAME, but to make
    972      1.1  christos    the recursion easier, it also stops if it reaches an unexpected ')'
    973      1.1  christos    or '>' if the value of PERMISSIVE is nonzero.  */
    974      1.1  christos 
    975      1.1  christos /* Let's optimize away calls to strlen("operator").  */
    976      1.1  christos 
    977      1.1  christos #define LENGTH_OF_OPERATOR 8
    978      1.1  christos 
    979      1.1  christos static unsigned int
    980      1.1  christos cp_find_first_component_aux (const char *name, int permissive)
    981      1.1  christos {
    982      1.1  christos   unsigned int index = 0;
    983      1.1  christos   /* Operator names can show up in unexpected places.  Since these can
    984      1.1  christos      contain parentheses or angle brackets, they can screw up the
    985      1.1  christos      recursion.  But not every string 'operator' is part of an
    986      1.1  christos      operater name: e.g. you could have a variable 'cooperator'.  So
    987      1.1  christos      this variable tells us whether or not we should treat the string
    988      1.1  christos      'operator' as starting an operator.  */
    989      1.1  christos   int operator_possible = 1;
    990      1.1  christos 
    991      1.1  christos   for (;; ++index)
    992      1.1  christos     {
    993      1.1  christos       switch (name[index])
    994      1.1  christos 	{
    995      1.1  christos 	case '<':
    996      1.1  christos 	  /* Template; eat it up.  The calls to cp_first_component
    997      1.1  christos 	     should only return (I hope!) when they reach the '>'
    998      1.1  christos 	     terminating the component or a '::' between two
    999      1.1  christos 	     components.  (Hence the '+ 2'.)  */
   1000      1.1  christos 	  index += 1;
   1001      1.1  christos 	  for (index += cp_find_first_component_aux (name + index, 1);
   1002      1.1  christos 	       name[index] != '>';
   1003      1.1  christos 	       index += cp_find_first_component_aux (name + index, 1))
   1004      1.1  christos 	    {
   1005      1.1  christos 	      if (name[index] != ':')
   1006      1.1  christos 		{
   1007      1.1  christos 		  demangled_name_complaint (name);
   1008      1.1  christos 		  return strlen (name);
   1009      1.1  christos 		}
   1010      1.1  christos 	      index += 2;
   1011      1.1  christos 	    }
   1012      1.1  christos 	  operator_possible = 1;
   1013      1.1  christos 	  break;
   1014      1.1  christos 	case '(':
   1015      1.1  christos 	  /* Similar comment as to '<'.  */
   1016      1.1  christos 	  index += 1;
   1017      1.1  christos 	  for (index += cp_find_first_component_aux (name + index, 1);
   1018      1.1  christos 	       name[index] != ')';
   1019      1.1  christos 	       index += cp_find_first_component_aux (name + index, 1))
   1020      1.1  christos 	    {
   1021      1.1  christos 	      if (name[index] != ':')
   1022      1.1  christos 		{
   1023      1.1  christos 		  demangled_name_complaint (name);
   1024      1.1  christos 		  return strlen (name);
   1025      1.1  christos 		}
   1026      1.1  christos 	      index += 2;
   1027      1.1  christos 	    }
   1028      1.1  christos 	  operator_possible = 1;
   1029      1.1  christos 	  break;
   1030      1.1  christos 	case '>':
   1031      1.1  christos 	case ')':
   1032      1.1  christos 	  if (permissive)
   1033      1.1  christos 	    return index;
   1034      1.1  christos 	  else
   1035      1.1  christos 	    {
   1036      1.1  christos 	      demangled_name_complaint (name);
   1037      1.1  christos 	      return strlen (name);
   1038      1.1  christos 	    }
   1039      1.1  christos 	case '\0':
   1040      1.1  christos 	  return index;
   1041  1.1.1.4  christos 	case ':':
   1042  1.1.1.4  christos 	  /* ':' marks a component iff the next character is also a ':'.
   1043  1.1.1.4  christos 	     Otherwise it is probably malformed input.  */
   1044  1.1.1.4  christos 	  if (name[index + 1] == ':')
   1045  1.1.1.4  christos 	    return index;
   1046  1.1.1.4  christos 	  break;
   1047      1.1  christos 	case 'o':
   1048      1.1  christos 	  /* Operator names can screw up the recursion.  */
   1049      1.1  christos 	  if (operator_possible
   1050      1.1  christos 	      && strncmp (name + index, "operator",
   1051      1.1  christos 			  LENGTH_OF_OPERATOR) == 0)
   1052      1.1  christos 	    {
   1053      1.1  christos 	      index += LENGTH_OF_OPERATOR;
   1054      1.1  christos 	      while (ISSPACE(name[index]))
   1055      1.1  christos 		++index;
   1056      1.1  christos 	      switch (name[index])
   1057      1.1  christos 		{
   1058      1.1  christos 		  /* Skip over one less than the appropriate number of
   1059      1.1  christos 		     characters: the for loop will skip over the last
   1060      1.1  christos 		     one.  */
   1061      1.1  christos 		case '<':
   1062      1.1  christos 		  if (name[index + 1] == '<')
   1063      1.1  christos 		    index += 1;
   1064      1.1  christos 		  else
   1065      1.1  christos 		    index += 0;
   1066      1.1  christos 		  break;
   1067      1.1  christos 		case '>':
   1068      1.1  christos 		case '-':
   1069      1.1  christos 		  if (name[index + 1] == '>')
   1070      1.1  christos 		    index += 1;
   1071      1.1  christos 		  else
   1072      1.1  christos 		    index += 0;
   1073      1.1  christos 		  break;
   1074      1.1  christos 		case '(':
   1075      1.1  christos 		  index += 1;
   1076      1.1  christos 		  break;
   1077      1.1  christos 		default:
   1078      1.1  christos 		  index += 0;
   1079      1.1  christos 		  break;
   1080      1.1  christos 		}
   1081      1.1  christos 	    }
   1082      1.1  christos 	  operator_possible = 0;
   1083      1.1  christos 	  break;
   1084      1.1  christos 	case ' ':
   1085      1.1  christos 	case ',':
   1086      1.1  christos 	case '.':
   1087      1.1  christos 	case '&':
   1088      1.1  christos 	case '*':
   1089      1.1  christos 	  /* NOTE: carlton/2003-04-18: I'm not sure what the precise
   1090      1.1  christos 	     set of relevant characters are here: it's necessary to
   1091      1.1  christos 	     include any character that can show up before 'operator'
   1092      1.1  christos 	     in a demangled name, and it's safe to include any
   1093      1.1  christos 	     character that can't be part of an identifier's name.  */
   1094      1.1  christos 	  operator_possible = 1;
   1095      1.1  christos 	  break;
   1096      1.1  christos 	default:
   1097      1.1  christos 	  operator_possible = 0;
   1098      1.1  christos 	  break;
   1099      1.1  christos 	}
   1100      1.1  christos     }
   1101      1.1  christos }
   1102      1.1  christos 
   1103      1.1  christos /* Complain about a demangled name that we don't know how to parse.
   1104      1.1  christos    NAME is the demangled name in question.  */
   1105      1.1  christos 
   1106      1.1  christos static void
   1107      1.1  christos demangled_name_complaint (const char *name)
   1108      1.1  christos {
   1109      1.1  christos   complaint (&symfile_complaints,
   1110      1.1  christos 	     "unexpected demangled name '%s'", name);
   1111      1.1  christos }
   1112      1.1  christos 
   1113      1.1  christos /* If NAME is the fully-qualified name of a C++
   1114      1.1  christos    function/variable/method/etc., this returns the length of its
   1115      1.1  christos    entire prefix: all of the namespaces and classes that make up its
   1116      1.1  christos    name.  Given 'A::foo', it returns 1, given 'A::B::foo', it returns
   1117      1.1  christos    4, given 'foo', it returns 0.  */
   1118      1.1  christos 
   1119      1.1  christos unsigned int
   1120      1.1  christos cp_entire_prefix_len (const char *name)
   1121      1.1  christos {
   1122      1.1  christos   unsigned int current_len = cp_find_first_component (name);
   1123      1.1  christos   unsigned int previous_len = 0;
   1124      1.1  christos 
   1125      1.1  christos   while (name[current_len] != '\0')
   1126      1.1  christos     {
   1127      1.1  christos       gdb_assert (name[current_len] == ':');
   1128      1.1  christos       previous_len = current_len;
   1129      1.1  christos       /* Skip the '::'.  */
   1130      1.1  christos       current_len += 2;
   1131      1.1  christos       current_len += cp_find_first_component (name + current_len);
   1132      1.1  christos     }
   1133      1.1  christos 
   1134      1.1  christos   return previous_len;
   1135      1.1  christos }
   1136      1.1  christos 
   1137      1.1  christos /* Overload resolution functions.  */
   1138      1.1  christos 
   1139      1.1  christos /* Test to see if SYM is a symbol that we haven't seen corresponding
   1140      1.1  christos    to a function named OLOAD_NAME.  If so, add it to the current
   1141      1.1  christos    completion list.  */
   1142      1.1  christos 
   1143      1.1  christos static void
   1144      1.1  christos overload_list_add_symbol (struct symbol *sym,
   1145      1.1  christos 			  const char *oload_name)
   1146      1.1  christos {
   1147      1.1  christos   int newsize;
   1148      1.1  christos   int i;
   1149      1.1  christos   char *sym_name;
   1150      1.1  christos 
   1151      1.1  christos   /* If there is no type information, we can't do anything, so
   1152      1.1  christos      skip.  */
   1153      1.1  christos   if (SYMBOL_TYPE (sym) == NULL)
   1154      1.1  christos     return;
   1155      1.1  christos 
   1156      1.1  christos   /* skip any symbols that we've already considered.  */
   1157      1.1  christos   for (i = 0; i < sym_return_val_index; ++i)
   1158      1.1  christos     if (strcmp (SYMBOL_LINKAGE_NAME (sym),
   1159      1.1  christos 		SYMBOL_LINKAGE_NAME (sym_return_val[i])) == 0)
   1160      1.1  christos       return;
   1161      1.1  christos 
   1162      1.1  christos   /* Get the demangled name without parameters */
   1163      1.1  christos   sym_name = cp_remove_params (SYMBOL_NATURAL_NAME (sym));
   1164      1.1  christos   if (!sym_name)
   1165      1.1  christos     return;
   1166      1.1  christos 
   1167      1.1  christos   /* skip symbols that cannot match */
   1168      1.1  christos   if (strcmp (sym_name, oload_name) != 0)
   1169      1.1  christos     {
   1170      1.1  christos       xfree (sym_name);
   1171      1.1  christos       return;
   1172      1.1  christos     }
   1173      1.1  christos 
   1174      1.1  christos   xfree (sym_name);
   1175      1.1  christos 
   1176      1.1  christos   /* We have a match for an overload instance, so add SYM to the
   1177      1.1  christos      current list of overload instances */
   1178      1.1  christos   if (sym_return_val_index + 3 > sym_return_val_size)
   1179      1.1  christos     {
   1180      1.1  christos       newsize = (sym_return_val_size *= 2) * sizeof (struct symbol *);
   1181      1.1  christos       sym_return_val = (struct symbol **)
   1182      1.1  christos 	xrealloc ((char *) sym_return_val, newsize);
   1183      1.1  christos     }
   1184      1.1  christos   sym_return_val[sym_return_val_index++] = sym;
   1185      1.1  christos   sym_return_val[sym_return_val_index] = NULL;
   1186      1.1  christos }
   1187      1.1  christos 
   1188      1.1  christos /* Return a null-terminated list of pointers to function symbols that
   1189      1.1  christos    are named FUNC_NAME and are visible within NAMESPACE.  */
   1190      1.1  christos 
   1191      1.1  christos struct symbol **
   1192      1.1  christos make_symbol_overload_list (const char *func_name,
   1193  1.1.1.3  christos 			   const char *the_namespace)
   1194      1.1  christos {
   1195      1.1  christos   struct cleanup *old_cleanups;
   1196      1.1  christos   const char *name;
   1197      1.1  christos 
   1198      1.1  christos   sym_return_val_size = 100;
   1199      1.1  christos   sym_return_val_index = 0;
   1200  1.1.1.4  christos   sym_return_val = XNEWVEC (struct symbol *, sym_return_val_size + 1);
   1201      1.1  christos   sym_return_val[0] = NULL;
   1202      1.1  christos 
   1203      1.1  christos   old_cleanups = make_cleanup (xfree, sym_return_val);
   1204      1.1  christos 
   1205  1.1.1.3  christos   make_symbol_overload_list_using (func_name, the_namespace);
   1206      1.1  christos 
   1207  1.1.1.3  christos   if (the_namespace[0] == '\0')
   1208      1.1  christos     name = func_name;
   1209      1.1  christos   else
   1210      1.1  christos     {
   1211      1.1  christos       char *concatenated_name
   1212  1.1.1.4  christos 	= (char *) alloca (strlen (the_namespace) + 2 + strlen (func_name) + 1);
   1213  1.1.1.3  christos       strcpy (concatenated_name, the_namespace);
   1214      1.1  christos       strcat (concatenated_name, "::");
   1215      1.1  christos       strcat (concatenated_name, func_name);
   1216      1.1  christos       name = concatenated_name;
   1217      1.1  christos     }
   1218      1.1  christos 
   1219      1.1  christos   make_symbol_overload_list_qualified (name);
   1220      1.1  christos 
   1221      1.1  christos   discard_cleanups (old_cleanups);
   1222      1.1  christos 
   1223      1.1  christos   return sym_return_val;
   1224      1.1  christos }
   1225      1.1  christos 
   1226      1.1  christos /* Add all symbols with a name matching NAME in BLOCK to the overload
   1227      1.1  christos    list.  */
   1228      1.1  christos 
   1229      1.1  christos static void
   1230      1.1  christos make_symbol_overload_list_block (const char *name,
   1231      1.1  christos                                  const struct block *block)
   1232      1.1  christos {
   1233      1.1  christos   struct block_iterator iter;
   1234      1.1  christos   struct symbol *sym;
   1235      1.1  christos 
   1236  1.1.1.2  christos   ALL_BLOCK_SYMBOLS_WITH_NAME (block, name, iter, sym)
   1237      1.1  christos     overload_list_add_symbol (sym, name);
   1238      1.1  christos }
   1239      1.1  christos 
   1240      1.1  christos /* Adds the function FUNC_NAME from NAMESPACE to the overload set.  */
   1241      1.1  christos 
   1242      1.1  christos static void
   1243      1.1  christos make_symbol_overload_list_namespace (const char *func_name,
   1244  1.1.1.3  christos                                      const char *the_namespace)
   1245      1.1  christos {
   1246      1.1  christos   const char *name;
   1247      1.1  christos   const struct block *block = NULL;
   1248      1.1  christos 
   1249  1.1.1.3  christos   if (the_namespace[0] == '\0')
   1250      1.1  christos     name = func_name;
   1251      1.1  christos   else
   1252      1.1  christos     {
   1253      1.1  christos       char *concatenated_name
   1254  1.1.1.4  christos 	= (char *) alloca (strlen (the_namespace) + 2 + strlen (func_name) + 1);
   1255      1.1  christos 
   1256  1.1.1.3  christos       strcpy (concatenated_name, the_namespace);
   1257      1.1  christos       strcat (concatenated_name, "::");
   1258      1.1  christos       strcat (concatenated_name, func_name);
   1259      1.1  christos       name = concatenated_name;
   1260      1.1  christos     }
   1261      1.1  christos 
   1262      1.1  christos   /* Look in the static block.  */
   1263      1.1  christos   block = block_static_block (get_selected_block (0));
   1264      1.1  christos   if (block)
   1265      1.1  christos     make_symbol_overload_list_block (name, block);
   1266      1.1  christos 
   1267      1.1  christos   /* Look in the global block.  */
   1268      1.1  christos   block = block_global_block (block);
   1269      1.1  christos   if (block)
   1270      1.1  christos     make_symbol_overload_list_block (name, block);
   1271      1.1  christos 
   1272      1.1  christos }
   1273      1.1  christos 
   1274      1.1  christos /* Search the namespace of the given type and namespace of and public
   1275      1.1  christos    base types.  */
   1276      1.1  christos 
   1277      1.1  christos static void
   1278      1.1  christos make_symbol_overload_list_adl_namespace (struct type *type,
   1279      1.1  christos                                          const char *func_name)
   1280      1.1  christos {
   1281  1.1.1.3  christos   char *the_namespace;
   1282      1.1  christos   const char *type_name;
   1283      1.1  christos   int i, prefix_len;
   1284      1.1  christos 
   1285      1.1  christos   while (TYPE_CODE (type) == TYPE_CODE_PTR
   1286      1.1  christos 	 || TYPE_CODE (type) == TYPE_CODE_REF
   1287      1.1  christos          || TYPE_CODE (type) == TYPE_CODE_ARRAY
   1288      1.1  christos          || TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
   1289      1.1  christos     {
   1290      1.1  christos       if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
   1291      1.1  christos 	type = check_typedef(type);
   1292      1.1  christos       else
   1293      1.1  christos 	type = TYPE_TARGET_TYPE (type);
   1294      1.1  christos     }
   1295      1.1  christos 
   1296      1.1  christos   type_name = TYPE_NAME (type);
   1297      1.1  christos 
   1298      1.1  christos   if (type_name == NULL)
   1299      1.1  christos     return;
   1300      1.1  christos 
   1301      1.1  christos   prefix_len = cp_entire_prefix_len (type_name);
   1302      1.1  christos 
   1303      1.1  christos   if (prefix_len != 0)
   1304      1.1  christos     {
   1305  1.1.1.4  christos       the_namespace = (char *) alloca (prefix_len + 1);
   1306  1.1.1.3  christos       strncpy (the_namespace, type_name, prefix_len);
   1307  1.1.1.3  christos       the_namespace[prefix_len] = '\0';
   1308      1.1  christos 
   1309  1.1.1.3  christos       make_symbol_overload_list_namespace (func_name, the_namespace);
   1310      1.1  christos     }
   1311      1.1  christos 
   1312      1.1  christos   /* Check public base type */
   1313  1.1.1.2  christos   if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
   1314      1.1  christos     for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
   1315      1.1  christos       {
   1316      1.1  christos 	if (BASETYPE_VIA_PUBLIC (type, i))
   1317      1.1  christos 	  make_symbol_overload_list_adl_namespace (TYPE_BASECLASS (type,
   1318      1.1  christos 								   i),
   1319      1.1  christos 						   func_name);
   1320      1.1  christos       }
   1321      1.1  christos }
   1322      1.1  christos 
   1323      1.1  christos /* Adds the overload list overload candidates for FUNC_NAME found
   1324      1.1  christos    through argument dependent lookup.  */
   1325      1.1  christos 
   1326      1.1  christos struct symbol **
   1327      1.1  christos make_symbol_overload_list_adl (struct type **arg_types, int nargs,
   1328      1.1  christos                                const char *func_name)
   1329      1.1  christos {
   1330      1.1  christos   int i;
   1331      1.1  christos 
   1332      1.1  christos   gdb_assert (sym_return_val_size != -1);
   1333      1.1  christos 
   1334      1.1  christos   for (i = 1; i <= nargs; i++)
   1335      1.1  christos     make_symbol_overload_list_adl_namespace (arg_types[i - 1],
   1336      1.1  christos 					     func_name);
   1337      1.1  christos 
   1338      1.1  christos   return sym_return_val;
   1339      1.1  christos }
   1340      1.1  christos 
   1341      1.1  christos /* Used for cleanups to reset the "searched" flag in case of an
   1342      1.1  christos    error.  */
   1343      1.1  christos 
   1344      1.1  christos static void
   1345      1.1  christos reset_directive_searched (void *data)
   1346      1.1  christos {
   1347  1.1.1.4  christos   struct using_direct *direct = (struct using_direct *) data;
   1348      1.1  christos   direct->searched = 0;
   1349      1.1  christos }
   1350      1.1  christos 
   1351      1.1  christos /* This applies the using directives to add namespaces to search in,
   1352      1.1  christos    and then searches for overloads in all of those namespaces.  It
   1353      1.1  christos    adds the symbols found to sym_return_val.  Arguments are as in
   1354      1.1  christos    make_symbol_overload_list.  */
   1355      1.1  christos 
   1356      1.1  christos static void
   1357      1.1  christos make_symbol_overload_list_using (const char *func_name,
   1358  1.1.1.3  christos 				 const char *the_namespace)
   1359      1.1  christos {
   1360      1.1  christos   struct using_direct *current;
   1361      1.1  christos   const struct block *block;
   1362      1.1  christos 
   1363      1.1  christos   /* First, go through the using directives.  If any of them apply,
   1364      1.1  christos      look in the appropriate namespaces for new functions to match
   1365      1.1  christos      on.  */
   1366      1.1  christos 
   1367      1.1  christos   for (block = get_selected_block (0);
   1368      1.1  christos        block != NULL;
   1369      1.1  christos        block = BLOCK_SUPERBLOCK (block))
   1370      1.1  christos     for (current = block_using (block);
   1371      1.1  christos 	current != NULL;
   1372      1.1  christos 	current = current->next)
   1373      1.1  christos       {
   1374      1.1  christos 	/* Prevent recursive calls.  */
   1375      1.1  christos 	if (current->searched)
   1376      1.1  christos 	  continue;
   1377      1.1  christos 
   1378      1.1  christos         /* If this is a namespace alias or imported declaration ignore
   1379      1.1  christos 	   it.  */
   1380      1.1  christos         if (current->alias != NULL || current->declaration != NULL)
   1381      1.1  christos           continue;
   1382      1.1  christos 
   1383  1.1.1.3  christos         if (strcmp (the_namespace, current->import_dest) == 0)
   1384      1.1  christos 	  {
   1385      1.1  christos 	    /* Mark this import as searched so that the recursive call
   1386      1.1  christos 	       does not search it again.  */
   1387      1.1  christos 	    struct cleanup *old_chain;
   1388      1.1  christos 	    current->searched = 1;
   1389      1.1  christos 	    old_chain = make_cleanup (reset_directive_searched,
   1390      1.1  christos 				      current);
   1391      1.1  christos 
   1392      1.1  christos 	    make_symbol_overload_list_using (func_name,
   1393      1.1  christos 					     current->import_src);
   1394      1.1  christos 
   1395      1.1  christos 	    current->searched = 0;
   1396      1.1  christos 	    discard_cleanups (old_chain);
   1397      1.1  christos 	  }
   1398      1.1  christos       }
   1399      1.1  christos 
   1400      1.1  christos   /* Now, add names for this namespace.  */
   1401  1.1.1.3  christos   make_symbol_overload_list_namespace (func_name, the_namespace);
   1402      1.1  christos }
   1403      1.1  christos 
   1404      1.1  christos /* This does the bulk of the work of finding overloaded symbols.
   1405      1.1  christos    FUNC_NAME is the name of the overloaded function we're looking for
   1406      1.1  christos    (possibly including namespace info).  */
   1407      1.1  christos 
   1408      1.1  christos static void
   1409      1.1  christos make_symbol_overload_list_qualified (const char *func_name)
   1410      1.1  christos {
   1411  1.1.1.2  christos   struct compunit_symtab *cust;
   1412      1.1  christos   struct objfile *objfile;
   1413      1.1  christos   const struct block *b, *surrounding_static_block = 0;
   1414      1.1  christos 
   1415      1.1  christos   /* Look through the partial symtabs for all symbols which begin by
   1416      1.1  christos      matching FUNC_NAME.  Make sure we read that symbol table in.  */
   1417      1.1  christos 
   1418      1.1  christos   ALL_OBJFILES (objfile)
   1419      1.1  christos   {
   1420      1.1  christos     if (objfile->sf)
   1421      1.1  christos       objfile->sf->qf->expand_symtabs_for_function (objfile, func_name);
   1422      1.1  christos   }
   1423      1.1  christos 
   1424      1.1  christos   /* Search upwards from currently selected frame (so that we can
   1425      1.1  christos      complete on local vars.  */
   1426      1.1  christos 
   1427      1.1  christos   for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
   1428      1.1  christos     make_symbol_overload_list_block (func_name, b);
   1429      1.1  christos 
   1430      1.1  christos   surrounding_static_block = block_static_block (get_selected_block (0));
   1431      1.1  christos 
   1432      1.1  christos   /* Go through the symtabs and check the externs and statics for
   1433      1.1  christos      symbols which match.  */
   1434      1.1  christos 
   1435  1.1.1.2  christos   ALL_COMPUNITS (objfile, cust)
   1436      1.1  christos   {
   1437      1.1  christos     QUIT;
   1438  1.1.1.2  christos     b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), GLOBAL_BLOCK);
   1439      1.1  christos     make_symbol_overload_list_block (func_name, b);
   1440      1.1  christos   }
   1441      1.1  christos 
   1442  1.1.1.2  christos   ALL_COMPUNITS (objfile, cust)
   1443      1.1  christos   {
   1444      1.1  christos     QUIT;
   1445  1.1.1.2  christos     b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), STATIC_BLOCK);
   1446      1.1  christos     /* Don't do this block twice.  */
   1447      1.1  christos     if (b == surrounding_static_block)
   1448      1.1  christos       continue;
   1449      1.1  christos     make_symbol_overload_list_block (func_name, b);
   1450      1.1  christos   }
   1451      1.1  christos }
   1452      1.1  christos 
   1453      1.1  christos /* Lookup the rtti type for a class name.  */
   1454      1.1  christos 
   1455      1.1  christos struct type *
   1456      1.1  christos cp_lookup_rtti_type (const char *name, struct block *block)
   1457      1.1  christos {
   1458      1.1  christos   struct symbol * rtti_sym;
   1459      1.1  christos   struct type * rtti_type;
   1460      1.1  christos 
   1461  1.1.1.3  christos   /* Use VAR_DOMAIN here as NAME may be a typedef.  PR 18141, 18417.
   1462  1.1.1.3  christos      Classes "live" in both STRUCT_DOMAIN and VAR_DOMAIN.  */
   1463  1.1.1.4  christos   rtti_sym = lookup_symbol (name, block, VAR_DOMAIN, NULL).symbol;
   1464      1.1  christos 
   1465      1.1  christos   if (rtti_sym == NULL)
   1466      1.1  christos     {
   1467      1.1  christos       warning (_("RTTI symbol not found for class '%s'"), name);
   1468      1.1  christos       return NULL;
   1469      1.1  christos     }
   1470      1.1  christos 
   1471      1.1  christos   if (SYMBOL_CLASS (rtti_sym) != LOC_TYPEDEF)
   1472      1.1  christos     {
   1473      1.1  christos       warning (_("RTTI symbol for class '%s' is not a type"), name);
   1474      1.1  christos       return NULL;
   1475      1.1  christos     }
   1476      1.1  christos 
   1477  1.1.1.3  christos   rtti_type = check_typedef (SYMBOL_TYPE (rtti_sym));
   1478      1.1  christos 
   1479      1.1  christos   switch (TYPE_CODE (rtti_type))
   1480      1.1  christos     {
   1481  1.1.1.2  christos     case TYPE_CODE_STRUCT:
   1482      1.1  christos       break;
   1483      1.1  christos     case TYPE_CODE_NAMESPACE:
   1484      1.1  christos       /* chastain/2003-11-26: the symbol tables often contain fake
   1485      1.1  christos 	 symbols for namespaces with the same name as the struct.
   1486      1.1  christos 	 This warning is an indication of a bug in the lookup order
   1487      1.1  christos 	 or a bug in the way that the symbol tables are populated.  */
   1488      1.1  christos       warning (_("RTTI symbol for class '%s' is a namespace"), name);
   1489      1.1  christos       return NULL;
   1490      1.1  christos     default:
   1491      1.1  christos       warning (_("RTTI symbol for class '%s' has bad type"), name);
   1492      1.1  christos       return NULL;
   1493      1.1  christos     }
   1494      1.1  christos 
   1495      1.1  christos   return rtti_type;
   1496      1.1  christos }
   1497      1.1  christos 
   1498  1.1.1.2  christos #ifdef HAVE_WORKING_FORK
   1499  1.1.1.2  christos 
   1500  1.1.1.2  christos /* If nonzero, attempt to catch crashes in the demangler and print
   1501  1.1.1.2  christos    useful debugging information.  */
   1502  1.1.1.2  christos 
   1503  1.1.1.2  christos static int catch_demangler_crashes = 1;
   1504  1.1.1.2  christos 
   1505  1.1.1.2  christos /* Stack context and environment for demangler crash recovery.  */
   1506  1.1.1.2  christos 
   1507  1.1.1.2  christos static SIGJMP_BUF gdb_demangle_jmp_buf;
   1508  1.1.1.2  christos 
   1509  1.1.1.2  christos /* If nonzero, attempt to dump core from the signal handler.  */
   1510  1.1.1.2  christos 
   1511  1.1.1.2  christos static int gdb_demangle_attempt_core_dump = 1;
   1512  1.1.1.2  christos 
   1513  1.1.1.2  christos /* Signal handler for gdb_demangle.  */
   1514  1.1.1.2  christos 
   1515  1.1.1.2  christos static void
   1516  1.1.1.2  christos gdb_demangle_signal_handler (int signo)
   1517  1.1.1.2  christos {
   1518  1.1.1.2  christos   if (gdb_demangle_attempt_core_dump)
   1519  1.1.1.2  christos     {
   1520  1.1.1.2  christos       if (fork () == 0)
   1521  1.1.1.2  christos 	dump_core ();
   1522  1.1.1.2  christos 
   1523  1.1.1.2  christos       gdb_demangle_attempt_core_dump = 0;
   1524  1.1.1.2  christos     }
   1525  1.1.1.2  christos 
   1526  1.1.1.2  christos   SIGLONGJMP (gdb_demangle_jmp_buf, signo);
   1527  1.1.1.2  christos }
   1528  1.1.1.2  christos 
   1529  1.1.1.2  christos #endif
   1530  1.1.1.2  christos 
   1531      1.1  christos /* A wrapper for bfd_demangle.  */
   1532      1.1  christos 
   1533      1.1  christos char *
   1534      1.1  christos gdb_demangle (const char *name, int options)
   1535      1.1  christos {
   1536  1.1.1.2  christos   char *result = NULL;
   1537  1.1.1.2  christos   int crash_signal = 0;
   1538  1.1.1.2  christos 
   1539  1.1.1.2  christos #ifdef HAVE_WORKING_FORK
   1540  1.1.1.2  christos #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
   1541  1.1.1.2  christos   struct sigaction sa, old_sa;
   1542  1.1.1.2  christos #else
   1543  1.1.1.4  christos   sighandler_t ofunc;
   1544  1.1.1.2  christos #endif
   1545  1.1.1.2  christos   static int core_dump_allowed = -1;
   1546  1.1.1.2  christos 
   1547  1.1.1.2  christos   if (core_dump_allowed == -1)
   1548  1.1.1.2  christos     {
   1549  1.1.1.2  christos       core_dump_allowed = can_dump_core (LIMIT_CUR);
   1550  1.1.1.2  christos 
   1551  1.1.1.2  christos       if (!core_dump_allowed)
   1552  1.1.1.2  christos 	gdb_demangle_attempt_core_dump = 0;
   1553  1.1.1.2  christos     }
   1554  1.1.1.2  christos 
   1555  1.1.1.2  christos   if (catch_demangler_crashes)
   1556  1.1.1.2  christos     {
   1557  1.1.1.2  christos #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
   1558  1.1.1.2  christos       sa.sa_handler = gdb_demangle_signal_handler;
   1559  1.1.1.2  christos       sigemptyset (&sa.sa_mask);
   1560  1.1.1.2  christos #ifdef HAVE_SIGALTSTACK
   1561  1.1.1.2  christos       sa.sa_flags = SA_ONSTACK;
   1562  1.1.1.2  christos #else
   1563  1.1.1.2  christos       sa.sa_flags = 0;
   1564  1.1.1.2  christos #endif
   1565  1.1.1.2  christos       sigaction (SIGSEGV, &sa, &old_sa);
   1566  1.1.1.2  christos #else
   1567  1.1.1.4  christos       ofunc = signal (SIGSEGV, gdb_demangle_signal_handler);
   1568  1.1.1.2  christos #endif
   1569  1.1.1.2  christos 
   1570  1.1.1.2  christos       crash_signal = SIGSETJMP (gdb_demangle_jmp_buf);
   1571  1.1.1.2  christos     }
   1572  1.1.1.2  christos #endif
   1573  1.1.1.2  christos 
   1574  1.1.1.2  christos   if (crash_signal == 0)
   1575  1.1.1.2  christos     result = bfd_demangle (NULL, name, options);
   1576  1.1.1.2  christos 
   1577  1.1.1.2  christos #ifdef HAVE_WORKING_FORK
   1578  1.1.1.2  christos   if (catch_demangler_crashes)
   1579  1.1.1.2  christos     {
   1580  1.1.1.2  christos #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
   1581  1.1.1.2  christos       sigaction (SIGSEGV, &old_sa, NULL);
   1582  1.1.1.2  christos #else
   1583  1.1.1.2  christos       signal (SIGSEGV, ofunc);
   1584  1.1.1.2  christos #endif
   1585  1.1.1.2  christos 
   1586  1.1.1.2  christos       if (crash_signal != 0)
   1587  1.1.1.2  christos 	{
   1588  1.1.1.2  christos 	  static int error_reported = 0;
   1589  1.1.1.2  christos 
   1590  1.1.1.2  christos 	  if (!error_reported)
   1591  1.1.1.2  christos 	    {
   1592  1.1.1.2  christos 	      char *short_msg, *long_msg;
   1593  1.1.1.2  christos 	      struct cleanup *back_to;
   1594  1.1.1.2  christos 
   1595  1.1.1.2  christos 	      short_msg = xstrprintf (_("unable to demangle '%s' "
   1596  1.1.1.2  christos 				      "(demangler failed with signal %d)"),
   1597  1.1.1.2  christos 				    name, crash_signal);
   1598  1.1.1.2  christos 	      back_to = make_cleanup (xfree, short_msg);
   1599  1.1.1.2  christos 
   1600  1.1.1.2  christos 	      long_msg = xstrprintf ("%s:%d: %s: %s", __FILE__, __LINE__,
   1601  1.1.1.2  christos 				    "demangler-warning", short_msg);
   1602  1.1.1.2  christos 	      make_cleanup (xfree, long_msg);
   1603  1.1.1.2  christos 
   1604  1.1.1.4  christos 	      make_cleanup_restore_target_terminal ();
   1605  1.1.1.4  christos 	      target_terminal_ours_for_output ();
   1606  1.1.1.4  christos 
   1607  1.1.1.2  christos 	      begin_line ();
   1608  1.1.1.2  christos 	      if (core_dump_allowed)
   1609  1.1.1.2  christos 		fprintf_unfiltered (gdb_stderr,
   1610  1.1.1.2  christos 				    _("%s\nAttempting to dump core.\n"),
   1611  1.1.1.2  christos 				    long_msg);
   1612  1.1.1.2  christos 	      else
   1613  1.1.1.2  christos 		warn_cant_dump_core (long_msg);
   1614  1.1.1.2  christos 
   1615  1.1.1.2  christos 	      demangler_warning (__FILE__, __LINE__, "%s", short_msg);
   1616  1.1.1.2  christos 
   1617  1.1.1.2  christos 	      do_cleanups (back_to);
   1618  1.1.1.2  christos 
   1619  1.1.1.2  christos 	      error_reported = 1;
   1620  1.1.1.2  christos 	    }
   1621  1.1.1.2  christos 
   1622  1.1.1.2  christos 	  result = NULL;
   1623  1.1.1.2  christos 	}
   1624  1.1.1.2  christos     }
   1625  1.1.1.2  christos #endif
   1626  1.1.1.2  christos 
   1627  1.1.1.2  christos   return result;
   1628      1.1  christos }
   1629      1.1  christos 
   1630  1.1.1.4  christos /* See cp-support.h.  */
   1631  1.1.1.4  christos 
   1632  1.1.1.4  christos int
   1633  1.1.1.4  christos gdb_sniff_from_mangled_name (const char *mangled, char **demangled)
   1634  1.1.1.4  christos {
   1635  1.1.1.4  christos   *demangled = gdb_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
   1636  1.1.1.4  christos   return *demangled != NULL;
   1637  1.1.1.4  christos }
   1638  1.1.1.4  christos 
   1639      1.1  christos /* Don't allow just "maintenance cplus".  */
   1640      1.1  christos 
   1641      1.1  christos static  void
   1642      1.1  christos maint_cplus_command (char *arg, int from_tty)
   1643      1.1  christos {
   1644      1.1  christos   printf_unfiltered (_("\"maintenance cplus\" must be followed "
   1645      1.1  christos 		       "by the name of a command.\n"));
   1646      1.1  christos   help_list (maint_cplus_cmd_list,
   1647      1.1  christos 	     "maintenance cplus ",
   1648  1.1.1.2  christos 	     all_commands, gdb_stdout);
   1649      1.1  christos }
   1650      1.1  christos 
   1651      1.1  christos /* This is a front end for cp_find_first_component, for unit testing.
   1652      1.1  christos    Be careful when using it: see the NOTE above
   1653      1.1  christos    cp_find_first_component.  */
   1654      1.1  christos 
   1655      1.1  christos static void
   1656      1.1  christos first_component_command (char *arg, int from_tty)
   1657      1.1  christos {
   1658      1.1  christos   int len;
   1659      1.1  christos   char *prefix;
   1660      1.1  christos 
   1661      1.1  christos   if (!arg)
   1662      1.1  christos     return;
   1663      1.1  christos 
   1664      1.1  christos   len = cp_find_first_component (arg);
   1665  1.1.1.4  christos   prefix = (char *) alloca (len + 1);
   1666      1.1  christos 
   1667      1.1  christos   memcpy (prefix, arg, len);
   1668      1.1  christos   prefix[len] = '\0';
   1669      1.1  christos 
   1670      1.1  christos   printf_unfiltered ("%s\n", prefix);
   1671      1.1  christos }
   1672      1.1  christos 
   1673      1.1  christos extern initialize_file_ftype _initialize_cp_support; /* -Wmissing-prototypes */
   1674      1.1  christos 
   1675      1.1  christos 
   1676      1.1  christos /* Implement "info vtbl".  */
   1677      1.1  christos 
   1678      1.1  christos static void
   1679      1.1  christos info_vtbl_command (char *arg, int from_tty)
   1680      1.1  christos {
   1681      1.1  christos   struct value *value;
   1682      1.1  christos 
   1683      1.1  christos   value = parse_and_eval (arg);
   1684      1.1  christos   cplus_print_vtable (value);
   1685      1.1  christos }
   1686      1.1  christos 
   1687      1.1  christos void
   1688      1.1  christos _initialize_cp_support (void)
   1689      1.1  christos {
   1690      1.1  christos   add_prefix_cmd ("cplus", class_maintenance,
   1691      1.1  christos 		  maint_cplus_command,
   1692      1.1  christos 		  _("C++ maintenance commands."),
   1693      1.1  christos 		  &maint_cplus_cmd_list,
   1694      1.1  christos 		  "maintenance cplus ",
   1695      1.1  christos 		  0, &maintenancelist);
   1696      1.1  christos   add_alias_cmd ("cp", "cplus",
   1697      1.1  christos 		 class_maintenance, 1,
   1698      1.1  christos 		 &maintenancelist);
   1699      1.1  christos 
   1700      1.1  christos   add_cmd ("first_component",
   1701      1.1  christos 	   class_maintenance,
   1702      1.1  christos 	   first_component_command,
   1703      1.1  christos 	   _("Print the first class/namespace component of NAME."),
   1704      1.1  christos 	   &maint_cplus_cmd_list);
   1705      1.1  christos 
   1706      1.1  christos   add_info ("vtbl", info_vtbl_command,
   1707      1.1  christos 	    _("Show the virtual function table for a C++ object.\n\
   1708      1.1  christos Usage: info vtbl EXPRESSION\n\
   1709      1.1  christos Evaluate EXPRESSION and display the virtual function table for the\n\
   1710      1.1  christos resulting object."));
   1711  1.1.1.2  christos 
   1712  1.1.1.2  christos #ifdef HAVE_WORKING_FORK
   1713  1.1.1.2  christos   add_setshow_boolean_cmd ("catch-demangler-crashes", class_maintenance,
   1714  1.1.1.2  christos 			   &catch_demangler_crashes, _("\
   1715  1.1.1.2  christos Set whether to attempt to catch demangler crashes."), _("\
   1716  1.1.1.2  christos Show whether to attempt to catch demangler crashes."), _("\
   1717  1.1.1.2  christos If enabled GDB will attempt to catch demangler crashes and\n\
   1718  1.1.1.2  christos display the offending symbol."),
   1719  1.1.1.2  christos 			   NULL,
   1720  1.1.1.2  christos 			   NULL,
   1721  1.1.1.2  christos 			   &maintenance_set_cmdlist,
   1722  1.1.1.2  christos 			   &maintenance_show_cmdlist);
   1723  1.1.1.2  christos #endif
   1724      1.1  christos }
   1725