xref: /dragonfly/contrib/gdb-7/gdb/cp-support.c (revision ef5ccd6c)
15796c8dcSSimon Schubert /* Helper routines for C++ support in GDB.
2*ef5ccd6cSJohn Marino    Copyright (C) 2002-2013 Free Software Foundation, Inc.
35796c8dcSSimon Schubert 
45796c8dcSSimon Schubert    Contributed by MontaVista Software.
55796c8dcSSimon Schubert 
65796c8dcSSimon Schubert    This file is part of GDB.
75796c8dcSSimon Schubert 
85796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
95796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
105796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
115796c8dcSSimon Schubert    (at your option) any later version.
125796c8dcSSimon Schubert 
135796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
145796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
155796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
165796c8dcSSimon Schubert    GNU General Public License for more details.
175796c8dcSSimon Schubert 
185796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
195796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
205796c8dcSSimon Schubert 
215796c8dcSSimon Schubert #include "defs.h"
225796c8dcSSimon Schubert #include "cp-support.h"
235796c8dcSSimon Schubert #include "gdb_string.h"
245796c8dcSSimon Schubert #include "demangle.h"
255796c8dcSSimon Schubert #include "gdb_assert.h"
265796c8dcSSimon Schubert #include "gdbcmd.h"
275796c8dcSSimon Schubert #include "dictionary.h"
285796c8dcSSimon Schubert #include "objfiles.h"
295796c8dcSSimon Schubert #include "frame.h"
305796c8dcSSimon Schubert #include "symtab.h"
315796c8dcSSimon Schubert #include "block.h"
325796c8dcSSimon Schubert #include "complaints.h"
335796c8dcSSimon Schubert #include "gdbtypes.h"
34cf7f2e2dSJohn Marino #include "exceptions.h"
35cf7f2e2dSJohn Marino #include "expression.h"
36cf7f2e2dSJohn Marino #include "value.h"
37*ef5ccd6cSJohn Marino #include "cp-abi.h"
385796c8dcSSimon Schubert 
395796c8dcSSimon Schubert #include "safe-ctype.h"
405796c8dcSSimon Schubert 
41cf7f2e2dSJohn Marino #include "psymtab.h"
42cf7f2e2dSJohn Marino 
435796c8dcSSimon Schubert #define d_left(dc) (dc)->u.s_binary.left
445796c8dcSSimon Schubert #define d_right(dc) (dc)->u.s_binary.right
455796c8dcSSimon Schubert 
465796c8dcSSimon Schubert /* Functions related to demangled name parsing.  */
475796c8dcSSimon Schubert 
485796c8dcSSimon Schubert static unsigned int cp_find_first_component_aux (const char *name,
495796c8dcSSimon Schubert 						 int permissive);
505796c8dcSSimon Schubert 
515796c8dcSSimon Schubert static void demangled_name_complaint (const char *name);
525796c8dcSSimon Schubert 
535796c8dcSSimon Schubert /* Functions/variables related to overload resolution.  */
545796c8dcSSimon Schubert 
55cf7f2e2dSJohn Marino static int sym_return_val_size = -1;
565796c8dcSSimon Schubert static int sym_return_val_index;
575796c8dcSSimon Schubert static struct symbol **sym_return_val;
585796c8dcSSimon Schubert 
595796c8dcSSimon Schubert static void overload_list_add_symbol (struct symbol *sym,
605796c8dcSSimon Schubert 				      const char *oload_name);
615796c8dcSSimon Schubert 
625796c8dcSSimon Schubert static void make_symbol_overload_list_using (const char *func_name,
635796c8dcSSimon Schubert 					     const char *namespace);
645796c8dcSSimon Schubert 
655796c8dcSSimon Schubert static void make_symbol_overload_list_qualified (const char *func_name);
665796c8dcSSimon Schubert 
675796c8dcSSimon Schubert /* The list of "maint cplus" commands.  */
685796c8dcSSimon Schubert 
695796c8dcSSimon Schubert struct cmd_list_element *maint_cplus_cmd_list = NULL;
705796c8dcSSimon Schubert 
715796c8dcSSimon Schubert /* The actual commands.  */
725796c8dcSSimon Schubert 
735796c8dcSSimon Schubert static void maint_cplus_command (char *arg, int from_tty);
745796c8dcSSimon Schubert static void first_component_command (char *arg, int from_tty);
755796c8dcSSimon Schubert 
76a45ae5f8SJohn Marino /* A list of typedefs which should not be substituted by replace_typedefs.  */
77a45ae5f8SJohn Marino static const char * const ignore_typedefs[] =
78a45ae5f8SJohn Marino   {
79a45ae5f8SJohn Marino     "std::istream", "std::iostream", "std::ostream", "std::string"
80a45ae5f8SJohn Marino   };
81a45ae5f8SJohn Marino 
82a45ae5f8SJohn Marino static void
83a45ae5f8SJohn Marino   replace_typedefs (struct demangle_parse_info *info,
84*ef5ccd6cSJohn Marino 		    struct demangle_component *ret_comp,
85*ef5ccd6cSJohn Marino 		    canonicalization_ftype *finder,
86*ef5ccd6cSJohn Marino 		    void *data);
87a45ae5f8SJohn Marino 
88a45ae5f8SJohn Marino /* A convenience function to copy STRING into OBSTACK, returning a pointer
89a45ae5f8SJohn Marino    to the newly allocated string and saving the number of bytes saved in LEN.
90a45ae5f8SJohn Marino 
91a45ae5f8SJohn Marino    It does not copy the terminating '\0' byte!  */
92a45ae5f8SJohn Marino 
93a45ae5f8SJohn Marino static char *
copy_string_to_obstack(struct obstack * obstack,const char * string,long * len)94a45ae5f8SJohn Marino copy_string_to_obstack (struct obstack *obstack, const char *string,
95a45ae5f8SJohn Marino 			long *len)
96a45ae5f8SJohn Marino {
97a45ae5f8SJohn Marino   *len = strlen (string);
98a45ae5f8SJohn Marino   return obstack_copy (obstack, string, *len);
99a45ae5f8SJohn Marino }
100a45ae5f8SJohn Marino 
101a45ae5f8SJohn Marino /* A cleanup wrapper for cp_demangled_name_parse_free.  */
102a45ae5f8SJohn Marino 
103a45ae5f8SJohn Marino static void
do_demangled_name_parse_free_cleanup(void * data)104a45ae5f8SJohn Marino do_demangled_name_parse_free_cleanup (void *data)
105a45ae5f8SJohn Marino {
106a45ae5f8SJohn Marino   struct demangle_parse_info *info = (struct demangle_parse_info *) data;
107a45ae5f8SJohn Marino 
108a45ae5f8SJohn Marino   cp_demangled_name_parse_free (info);
109a45ae5f8SJohn Marino }
110a45ae5f8SJohn Marino 
111a45ae5f8SJohn Marino /* Create a cleanup for C++ name parsing.  */
112a45ae5f8SJohn Marino 
113a45ae5f8SJohn Marino struct cleanup *
make_cleanup_cp_demangled_name_parse_free(struct demangle_parse_info * info)114a45ae5f8SJohn Marino make_cleanup_cp_demangled_name_parse_free (struct demangle_parse_info *info)
115a45ae5f8SJohn Marino {
116a45ae5f8SJohn Marino   return make_cleanup (do_demangled_name_parse_free_cleanup, info);
117a45ae5f8SJohn Marino }
118a45ae5f8SJohn Marino 
1195796c8dcSSimon Schubert /* Return 1 if STRING is clearly already in canonical form.  This
1205796c8dcSSimon Schubert    function is conservative; things which it does not recognize are
1215796c8dcSSimon Schubert    assumed to be non-canonical, and the parser will sort them out
1225796c8dcSSimon Schubert    afterwards.  This speeds up the critical path for alphanumeric
1235796c8dcSSimon Schubert    identifiers.  */
1245796c8dcSSimon Schubert 
1255796c8dcSSimon Schubert static int
cp_already_canonical(const char * string)1265796c8dcSSimon Schubert cp_already_canonical (const char *string)
1275796c8dcSSimon Schubert {
1285796c8dcSSimon Schubert   /* Identifier start character [a-zA-Z_].  */
1295796c8dcSSimon Schubert   if (!ISIDST (string[0]))
1305796c8dcSSimon Schubert     return 0;
1315796c8dcSSimon Schubert 
1325796c8dcSSimon Schubert   /* These are the only two identifiers which canonicalize to other
1335796c8dcSSimon Schubert      than themselves or an error: unsigned -> unsigned int and
1345796c8dcSSimon Schubert      signed -> int.  */
1355796c8dcSSimon Schubert   if (string[0] == 'u' && strcmp (&string[1], "nsigned") == 0)
1365796c8dcSSimon Schubert     return 0;
1375796c8dcSSimon Schubert   else if (string[0] == 's' && strcmp (&string[1], "igned") == 0)
1385796c8dcSSimon Schubert     return 0;
1395796c8dcSSimon Schubert 
1405796c8dcSSimon Schubert   /* Identifier character [a-zA-Z0-9_].  */
1415796c8dcSSimon Schubert   while (ISIDNUM (string[1]))
1425796c8dcSSimon Schubert     string++;
1435796c8dcSSimon Schubert 
1445796c8dcSSimon Schubert   if (string[1] == '\0')
1455796c8dcSSimon Schubert     return 1;
1465796c8dcSSimon Schubert   else
1475796c8dcSSimon Schubert     return 0;
1485796c8dcSSimon Schubert }
1495796c8dcSSimon Schubert 
150a45ae5f8SJohn Marino /* Inspect the given RET_COMP for its type.  If it is a typedef,
151a45ae5f8SJohn Marino    replace the node with the typedef's tree.
152a45ae5f8SJohn Marino 
153a45ae5f8SJohn Marino    Returns 1 if any typedef substitutions were made, 0 otherwise.  */
154a45ae5f8SJohn Marino 
155a45ae5f8SJohn Marino static int
inspect_type(struct demangle_parse_info * info,struct demangle_component * ret_comp,canonicalization_ftype * finder,void * data)156a45ae5f8SJohn Marino inspect_type (struct demangle_parse_info *info,
157*ef5ccd6cSJohn Marino 	      struct demangle_component *ret_comp,
158*ef5ccd6cSJohn Marino 	      canonicalization_ftype *finder,
159*ef5ccd6cSJohn Marino 	      void *data)
160a45ae5f8SJohn Marino {
161a45ae5f8SJohn Marino   int i;
162a45ae5f8SJohn Marino   char *name;
163a45ae5f8SJohn Marino   struct symbol *sym;
164a45ae5f8SJohn Marino   volatile struct gdb_exception except;
165a45ae5f8SJohn Marino 
166a45ae5f8SJohn Marino   /* Copy the symbol's name from RET_COMP and look it up
167a45ae5f8SJohn Marino      in the symbol table.  */
168a45ae5f8SJohn Marino   name = (char *) alloca (ret_comp->u.s_name.len + 1);
169a45ae5f8SJohn Marino   memcpy (name, ret_comp->u.s_name.s, ret_comp->u.s_name.len);
170a45ae5f8SJohn Marino   name[ret_comp->u.s_name.len] = '\0';
171a45ae5f8SJohn Marino 
172a45ae5f8SJohn Marino   /* Ignore any typedefs that should not be substituted.  */
173a45ae5f8SJohn Marino   for (i = 0; i < ARRAY_SIZE (ignore_typedefs); ++i)
174a45ae5f8SJohn Marino     {
175a45ae5f8SJohn Marino       if (strcmp (name, ignore_typedefs[i]) == 0)
176a45ae5f8SJohn Marino 	return 0;
177a45ae5f8SJohn Marino     }
178a45ae5f8SJohn Marino 
179a45ae5f8SJohn Marino   sym = NULL;
180a45ae5f8SJohn Marino   TRY_CATCH (except, RETURN_MASK_ALL)
181a45ae5f8SJohn Marino   {
182a45ae5f8SJohn Marino     sym = lookup_symbol (name, 0, VAR_DOMAIN, 0);
183a45ae5f8SJohn Marino   }
184a45ae5f8SJohn Marino 
185a45ae5f8SJohn Marino   if (except.reason >= 0 && sym != NULL)
186a45ae5f8SJohn Marino     {
187a45ae5f8SJohn Marino       struct type *otype = SYMBOL_TYPE (sym);
188a45ae5f8SJohn Marino 
189*ef5ccd6cSJohn Marino       if (finder != NULL)
190*ef5ccd6cSJohn Marino 	{
191*ef5ccd6cSJohn Marino 	  const char *new_name = (*finder) (otype, data);
192*ef5ccd6cSJohn Marino 
193*ef5ccd6cSJohn Marino 	  if (new_name != NULL)
194*ef5ccd6cSJohn Marino 	    {
195*ef5ccd6cSJohn Marino 	      ret_comp->u.s_name.s = new_name;
196*ef5ccd6cSJohn Marino 	      ret_comp->u.s_name.len = strlen (new_name);
197*ef5ccd6cSJohn Marino 	      return 1;
198*ef5ccd6cSJohn Marino 	    }
199*ef5ccd6cSJohn Marino 
200*ef5ccd6cSJohn Marino 	  return 0;
201*ef5ccd6cSJohn Marino 	}
202*ef5ccd6cSJohn Marino 
203a45ae5f8SJohn Marino       /* If the type is a typedef, replace it.  */
204a45ae5f8SJohn Marino       if (TYPE_CODE (otype) == TYPE_CODE_TYPEDEF)
205a45ae5f8SJohn Marino 	{
206a45ae5f8SJohn Marino 	  long len;
207a45ae5f8SJohn Marino 	  int is_anon;
208a45ae5f8SJohn Marino 	  struct type *type;
209a45ae5f8SJohn Marino 	  struct demangle_parse_info *i;
210a45ae5f8SJohn Marino 	  struct ui_file *buf;
211a45ae5f8SJohn Marino 
212a45ae5f8SJohn Marino 	  /* Get the real type of the typedef.  */
213a45ae5f8SJohn Marino 	  type = check_typedef (otype);
214a45ae5f8SJohn Marino 
215a45ae5f8SJohn Marino 	  is_anon = (TYPE_TAG_NAME (type) == NULL
216a45ae5f8SJohn Marino 		     && (TYPE_CODE (type) == TYPE_CODE_ENUM
217a45ae5f8SJohn Marino 			 || TYPE_CODE (type) == TYPE_CODE_STRUCT
218a45ae5f8SJohn Marino 			 || TYPE_CODE (type) == TYPE_CODE_UNION));
219a45ae5f8SJohn Marino 	  if (is_anon)
220a45ae5f8SJohn Marino 	    {
221a45ae5f8SJohn Marino 	      struct type *last = otype;
222a45ae5f8SJohn Marino 
223a45ae5f8SJohn Marino 	      /* Find the last typedef for the type.  */
224a45ae5f8SJohn Marino 	      while (TYPE_TARGET_TYPE (last) != NULL
225a45ae5f8SJohn Marino 		     && (TYPE_CODE (TYPE_TARGET_TYPE (last))
226a45ae5f8SJohn Marino 			 == TYPE_CODE_TYPEDEF))
227a45ae5f8SJohn Marino 		last = TYPE_TARGET_TYPE (last);
228a45ae5f8SJohn Marino 
229a45ae5f8SJohn Marino 	      /* If there is only one typedef for this anonymous type,
230a45ae5f8SJohn Marino 		 do not substitute it.  */
231a45ae5f8SJohn Marino 	      if (type == otype)
232a45ae5f8SJohn Marino 		return 0;
233a45ae5f8SJohn Marino 	      else
234a45ae5f8SJohn Marino 		/* Use the last typedef seen as the type for this
235a45ae5f8SJohn Marino 		   anonymous type.  */
236a45ae5f8SJohn Marino 		type = last;
237a45ae5f8SJohn Marino 	    }
238a45ae5f8SJohn Marino 
239a45ae5f8SJohn Marino 	  buf = mem_fileopen ();
240a45ae5f8SJohn Marino 	  TRY_CATCH (except, RETURN_MASK_ERROR)
241a45ae5f8SJohn Marino 	  {
242a45ae5f8SJohn Marino 	    type_print (type, "", buf, -1);
243a45ae5f8SJohn Marino 	  }
244a45ae5f8SJohn Marino 
245a45ae5f8SJohn Marino 	  /* If type_print threw an exception, there is little point
246a45ae5f8SJohn Marino 	     in continuing, so just bow out gracefully.  */
247a45ae5f8SJohn Marino 	  if (except.reason < 0)
248a45ae5f8SJohn Marino 	    {
249a45ae5f8SJohn Marino 	      ui_file_delete (buf);
250a45ae5f8SJohn Marino 	      return 0;
251a45ae5f8SJohn Marino 	    }
252a45ae5f8SJohn Marino 
253a45ae5f8SJohn Marino 	  name = ui_file_obsavestring (buf, &info->obstack, &len);
254a45ae5f8SJohn Marino 	  ui_file_delete (buf);
255a45ae5f8SJohn Marino 
256a45ae5f8SJohn Marino 	  /* Turn the result into a new tree.  Note that this
257a45ae5f8SJohn Marino 	     tree will contain pointers into NAME, so NAME cannot
258a45ae5f8SJohn Marino 	     be free'd until all typedef conversion is done and
259a45ae5f8SJohn Marino 	     the final result is converted into a string.  */
260a45ae5f8SJohn Marino 	  i = cp_demangled_name_to_comp (name, NULL);
261a45ae5f8SJohn Marino 	  if (i != NULL)
262a45ae5f8SJohn Marino 	    {
263a45ae5f8SJohn Marino 	      /* Merge the two trees.  */
264a45ae5f8SJohn Marino 	      cp_merge_demangle_parse_infos (info, ret_comp, i);
265a45ae5f8SJohn Marino 
266a45ae5f8SJohn Marino 	      /* Replace any newly introduced typedefs -- but not
267a45ae5f8SJohn Marino 		 if the type is anonymous (that would lead to infinite
268a45ae5f8SJohn Marino 		 looping).  */
269a45ae5f8SJohn Marino 	      if (!is_anon)
270*ef5ccd6cSJohn Marino 		replace_typedefs (info, ret_comp, finder, data);
271a45ae5f8SJohn Marino 	    }
272a45ae5f8SJohn Marino 	  else
273a45ae5f8SJohn Marino 	    {
274a45ae5f8SJohn Marino 	      /* This shouldn't happen unless the type printer has
275a45ae5f8SJohn Marino 		 output something that the name parser cannot grok.
276a45ae5f8SJohn Marino 		 Nonetheless, an ounce of prevention...
277a45ae5f8SJohn Marino 
278a45ae5f8SJohn Marino 		 Canonicalize the name again, and store it in the
279a45ae5f8SJohn Marino 		 current node (RET_COMP).  */
280a45ae5f8SJohn Marino 	      char *canon = cp_canonicalize_string_no_typedefs (name);
281a45ae5f8SJohn Marino 
282a45ae5f8SJohn Marino 	      if (canon != NULL)
283a45ae5f8SJohn Marino 		{
284a45ae5f8SJohn Marino 		  /* Copy the canonicalization into the obstack and
285a45ae5f8SJohn Marino 		     free CANON.  */
286a45ae5f8SJohn Marino 		  name = copy_string_to_obstack (&info->obstack, canon, &len);
287a45ae5f8SJohn Marino 		  xfree (canon);
288a45ae5f8SJohn Marino 		}
289a45ae5f8SJohn Marino 
290a45ae5f8SJohn Marino 	      ret_comp->u.s_name.s = name;
291a45ae5f8SJohn Marino 	      ret_comp->u.s_name.len = len;
292a45ae5f8SJohn Marino 	    }
293a45ae5f8SJohn Marino 
294a45ae5f8SJohn Marino 	  return 1;
295a45ae5f8SJohn Marino 	}
296a45ae5f8SJohn Marino     }
297a45ae5f8SJohn Marino 
298a45ae5f8SJohn Marino   return 0;
299a45ae5f8SJohn Marino }
300a45ae5f8SJohn Marino 
301a45ae5f8SJohn Marino /* Replace any typedefs appearing in the qualified name
302a45ae5f8SJohn Marino    (DEMANGLE_COMPONENT_QUAL_NAME) represented in RET_COMP for the name parse
303a45ae5f8SJohn Marino    given in INFO.  */
304a45ae5f8SJohn Marino 
305a45ae5f8SJohn Marino static void
replace_typedefs_qualified_name(struct demangle_parse_info * info,struct demangle_component * ret_comp,canonicalization_ftype * finder,void * data)306a45ae5f8SJohn Marino replace_typedefs_qualified_name (struct demangle_parse_info *info,
307*ef5ccd6cSJohn Marino 				 struct demangle_component *ret_comp,
308*ef5ccd6cSJohn Marino 				 canonicalization_ftype *finder,
309*ef5ccd6cSJohn Marino 				 void *data)
310a45ae5f8SJohn Marino {
311a45ae5f8SJohn Marino   long len;
312a45ae5f8SJohn Marino   char *name;
313a45ae5f8SJohn Marino   struct ui_file *buf = mem_fileopen ();
314a45ae5f8SJohn Marino   struct demangle_component *comp = ret_comp;
315a45ae5f8SJohn Marino 
316a45ae5f8SJohn Marino   /* Walk each node of the qualified name, reconstructing the name of
317a45ae5f8SJohn Marino      this element.  With every node, check for any typedef substitutions.
318a45ae5f8SJohn Marino      If a substitution has occurred, replace the qualified name node
319a45ae5f8SJohn Marino      with a DEMANGLE_COMPONENT_NAME node representing the new, typedef-
320a45ae5f8SJohn Marino      substituted name.  */
321a45ae5f8SJohn Marino   while (comp->type == DEMANGLE_COMPONENT_QUAL_NAME)
322a45ae5f8SJohn Marino     {
323a45ae5f8SJohn Marino       if (d_left (comp)->type == DEMANGLE_COMPONENT_NAME)
324a45ae5f8SJohn Marino 	{
325a45ae5f8SJohn Marino 	  struct demangle_component new;
326a45ae5f8SJohn Marino 
327a45ae5f8SJohn Marino 	  ui_file_write (buf, d_left (comp)->u.s_name.s,
328a45ae5f8SJohn Marino 			 d_left (comp)->u.s_name.len);
329a45ae5f8SJohn Marino 	  name = ui_file_obsavestring (buf, &info->obstack, &len);
330a45ae5f8SJohn Marino 	  new.type = DEMANGLE_COMPONENT_NAME;
331a45ae5f8SJohn Marino 	  new.u.s_name.s = name;
332a45ae5f8SJohn Marino 	  new.u.s_name.len = len;
333*ef5ccd6cSJohn Marino 	  if (inspect_type (info, &new, finder, data))
334a45ae5f8SJohn Marino 	    {
335a45ae5f8SJohn Marino 	      char *n, *s;
336a45ae5f8SJohn Marino 	      long slen;
337a45ae5f8SJohn Marino 
338a45ae5f8SJohn Marino 	      /* A typedef was substituted in NEW.  Convert it to a
339a45ae5f8SJohn Marino 		 string and replace the top DEMANGLE_COMPONENT_QUAL_NAME
340a45ae5f8SJohn Marino 		 node.  */
341a45ae5f8SJohn Marino 
342a45ae5f8SJohn Marino 	      ui_file_rewind (buf);
343a45ae5f8SJohn Marino 	      n = cp_comp_to_string (&new, 100);
344a45ae5f8SJohn Marino 	      if (n == NULL)
345a45ae5f8SJohn Marino 		{
346a45ae5f8SJohn Marino 		  /* If something went astray, abort typedef substitutions.  */
347a45ae5f8SJohn Marino 		  ui_file_delete (buf);
348a45ae5f8SJohn Marino 		  return;
349a45ae5f8SJohn Marino 		}
350a45ae5f8SJohn Marino 
351a45ae5f8SJohn Marino 	      s = copy_string_to_obstack (&info->obstack, n, &slen);
352a45ae5f8SJohn Marino 	      xfree (n);
353a45ae5f8SJohn Marino 
354a45ae5f8SJohn Marino 	      d_left (ret_comp)->type = DEMANGLE_COMPONENT_NAME;
355a45ae5f8SJohn Marino 	      d_left (ret_comp)->u.s_name.s = s;
356a45ae5f8SJohn Marino 	      d_left (ret_comp)->u.s_name.len = slen;
357a45ae5f8SJohn Marino 	      d_right (ret_comp) = d_right (comp);
358a45ae5f8SJohn Marino 	      comp = ret_comp;
359a45ae5f8SJohn Marino 	      continue;
360a45ae5f8SJohn Marino 	    }
361a45ae5f8SJohn Marino 	}
362a45ae5f8SJohn Marino       else
363a45ae5f8SJohn Marino 	{
364a45ae5f8SJohn Marino 	  /* The current node is not a name, so simply replace any
365a45ae5f8SJohn Marino 	     typedefs in it.  Then print it to the stream to continue
366a45ae5f8SJohn Marino 	     checking for more typedefs in the tree.  */
367*ef5ccd6cSJohn Marino 	  replace_typedefs (info, d_left (comp), finder, data);
368a45ae5f8SJohn Marino 	  name = cp_comp_to_string (d_left (comp), 100);
369a45ae5f8SJohn Marino 	  if (name == NULL)
370a45ae5f8SJohn Marino 	    {
371a45ae5f8SJohn Marino 	      /* If something went astray, abort typedef substitutions.  */
372a45ae5f8SJohn Marino 	      ui_file_delete (buf);
373a45ae5f8SJohn Marino 	      return;
374a45ae5f8SJohn Marino 	    }
375a45ae5f8SJohn Marino 	  fputs_unfiltered (name, buf);
376a45ae5f8SJohn Marino 	  xfree (name);
377a45ae5f8SJohn Marino 	}
378*ef5ccd6cSJohn Marino 
379a45ae5f8SJohn Marino       ui_file_write (buf, "::", 2);
380a45ae5f8SJohn Marino       comp = d_right (comp);
381a45ae5f8SJohn Marino     }
382a45ae5f8SJohn Marino 
383a45ae5f8SJohn Marino   /* If the next component is DEMANGLE_COMPONENT_NAME, save the qualified
384a45ae5f8SJohn Marino      name assembled above and append the name given by COMP.  Then use this
385a45ae5f8SJohn Marino      reassembled name to check for a typedef.  */
386a45ae5f8SJohn Marino 
387a45ae5f8SJohn Marino   if (comp->type == DEMANGLE_COMPONENT_NAME)
388a45ae5f8SJohn Marino     {
389a45ae5f8SJohn Marino       ui_file_write (buf, comp->u.s_name.s, comp->u.s_name.len);
390a45ae5f8SJohn Marino       name = ui_file_obsavestring (buf, &info->obstack, &len);
391a45ae5f8SJohn Marino 
392a45ae5f8SJohn Marino       /* Replace the top (DEMANGLE_COMPONENT_QUAL_NAME) node
393a45ae5f8SJohn Marino 	 with a DEMANGLE_COMPONENT_NAME node containing the whole
394a45ae5f8SJohn Marino 	 name.  */
395a45ae5f8SJohn Marino       ret_comp->type = DEMANGLE_COMPONENT_NAME;
396a45ae5f8SJohn Marino       ret_comp->u.s_name.s = name;
397a45ae5f8SJohn Marino       ret_comp->u.s_name.len = len;
398*ef5ccd6cSJohn Marino       inspect_type (info, ret_comp, finder, data);
399a45ae5f8SJohn Marino     }
400a45ae5f8SJohn Marino   else
401*ef5ccd6cSJohn Marino     replace_typedefs (info, comp, finder, data);
402a45ae5f8SJohn Marino 
403a45ae5f8SJohn Marino   ui_file_delete (buf);
404a45ae5f8SJohn Marino }
405a45ae5f8SJohn Marino 
406a45ae5f8SJohn Marino 
407a45ae5f8SJohn Marino /* A function to check const and volatile qualifiers for argument types.
408a45ae5f8SJohn Marino 
409a45ae5f8SJohn Marino    "Parameter declarations that differ only in the presence
410a45ae5f8SJohn Marino    or absence of `const' and/or `volatile' are equivalent."
411a45ae5f8SJohn Marino    C++ Standard N3290, clause 13.1.3 #4.  */
412a45ae5f8SJohn Marino 
413a45ae5f8SJohn Marino static void
check_cv_qualifiers(struct demangle_component * ret_comp)414a45ae5f8SJohn Marino check_cv_qualifiers (struct demangle_component *ret_comp)
415a45ae5f8SJohn Marino {
416a45ae5f8SJohn Marino   while (d_left (ret_comp) != NULL
417a45ae5f8SJohn Marino 	 && (d_left (ret_comp)->type == DEMANGLE_COMPONENT_CONST
418a45ae5f8SJohn Marino 	     || d_left (ret_comp)->type == DEMANGLE_COMPONENT_VOLATILE))
419a45ae5f8SJohn Marino     {
420a45ae5f8SJohn Marino       d_left (ret_comp) = d_left (d_left (ret_comp));
421a45ae5f8SJohn Marino     }
422a45ae5f8SJohn Marino }
423a45ae5f8SJohn Marino 
424a45ae5f8SJohn Marino /* Walk the parse tree given by RET_COMP, replacing any typedefs with
425a45ae5f8SJohn Marino    their basic types.  */
426a45ae5f8SJohn Marino 
427a45ae5f8SJohn Marino static void
replace_typedefs(struct demangle_parse_info * info,struct demangle_component * ret_comp,canonicalization_ftype * finder,void * data)428a45ae5f8SJohn Marino replace_typedefs (struct demangle_parse_info *info,
429*ef5ccd6cSJohn Marino 		  struct demangle_component *ret_comp,
430*ef5ccd6cSJohn Marino 		  canonicalization_ftype *finder,
431*ef5ccd6cSJohn Marino 		  void *data)
432a45ae5f8SJohn Marino {
433a45ae5f8SJohn Marino   if (ret_comp)
434a45ae5f8SJohn Marino     {
435*ef5ccd6cSJohn Marino       if (finder != NULL
436*ef5ccd6cSJohn Marino 	  && (ret_comp->type == DEMANGLE_COMPONENT_NAME
437*ef5ccd6cSJohn Marino 	      || ret_comp->type == DEMANGLE_COMPONENT_QUAL_NAME
438*ef5ccd6cSJohn Marino 	      || ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE
439*ef5ccd6cSJohn Marino 	      || ret_comp->type == DEMANGLE_COMPONENT_BUILTIN_TYPE))
440*ef5ccd6cSJohn Marino 	{
441*ef5ccd6cSJohn Marino 	  char *local_name = cp_comp_to_string (ret_comp, 10);
442*ef5ccd6cSJohn Marino 
443*ef5ccd6cSJohn Marino 	  if (local_name != NULL)
444*ef5ccd6cSJohn Marino 	    {
445*ef5ccd6cSJohn Marino 	      struct symbol *sym;
446*ef5ccd6cSJohn Marino 	      volatile struct gdb_exception except;
447*ef5ccd6cSJohn Marino 
448*ef5ccd6cSJohn Marino 	      sym = NULL;
449*ef5ccd6cSJohn Marino 	      TRY_CATCH (except, RETURN_MASK_ALL)
450*ef5ccd6cSJohn Marino 		{
451*ef5ccd6cSJohn Marino 		  sym = lookup_symbol (local_name, 0, VAR_DOMAIN, 0);
452*ef5ccd6cSJohn Marino 		}
453*ef5ccd6cSJohn Marino 	      xfree (local_name);
454*ef5ccd6cSJohn Marino 
455*ef5ccd6cSJohn Marino 	      if (except.reason >= 0 && sym != NULL)
456*ef5ccd6cSJohn Marino 		{
457*ef5ccd6cSJohn Marino 		  struct type *otype = SYMBOL_TYPE (sym);
458*ef5ccd6cSJohn Marino 		  const char *new_name = (*finder) (otype, data);
459*ef5ccd6cSJohn Marino 
460*ef5ccd6cSJohn Marino 		  if (new_name != NULL)
461*ef5ccd6cSJohn Marino 		    {
462*ef5ccd6cSJohn Marino 		      ret_comp->type = DEMANGLE_COMPONENT_NAME;
463*ef5ccd6cSJohn Marino 		      ret_comp->u.s_name.s = new_name;
464*ef5ccd6cSJohn Marino 		      ret_comp->u.s_name.len = strlen (new_name);
465*ef5ccd6cSJohn Marino 		      return;
466*ef5ccd6cSJohn Marino 		    }
467*ef5ccd6cSJohn Marino 		}
468*ef5ccd6cSJohn Marino 	    }
469*ef5ccd6cSJohn Marino 	}
470*ef5ccd6cSJohn Marino 
471a45ae5f8SJohn Marino       switch (ret_comp->type)
472a45ae5f8SJohn Marino 	{
473a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_ARGLIST:
474a45ae5f8SJohn Marino 	  check_cv_qualifiers (ret_comp);
475a45ae5f8SJohn Marino 	  /* Fall through */
476a45ae5f8SJohn Marino 
477a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_FUNCTION_TYPE:
478a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_TEMPLATE:
479a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
480a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_TYPED_NAME:
481*ef5ccd6cSJohn Marino 	  replace_typedefs (info, d_left (ret_comp), finder, data);
482*ef5ccd6cSJohn Marino 	  replace_typedefs (info, d_right (ret_comp), finder, data);
483a45ae5f8SJohn Marino 	  break;
484a45ae5f8SJohn Marino 
485a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_NAME:
486*ef5ccd6cSJohn Marino 	  inspect_type (info, ret_comp, finder, data);
487a45ae5f8SJohn Marino 	  break;
488a45ae5f8SJohn Marino 
489a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_QUAL_NAME:
490*ef5ccd6cSJohn Marino 	  replace_typedefs_qualified_name (info, ret_comp, finder, data);
491a45ae5f8SJohn Marino 	  break;
492a45ae5f8SJohn Marino 
493a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_LOCAL_NAME:
494a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_CTOR:
495a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_ARRAY_TYPE:
496a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_PTRMEM_TYPE:
497*ef5ccd6cSJohn Marino 	  replace_typedefs (info, d_right (ret_comp), finder, data);
498a45ae5f8SJohn Marino 	  break;
499a45ae5f8SJohn Marino 
500a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_CONST:
501a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_RESTRICT:
502a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_VOLATILE:
503a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_VOLATILE_THIS:
504a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_CONST_THIS:
505a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_RESTRICT_THIS:
506a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_POINTER:
507a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_REFERENCE:
508*ef5ccd6cSJohn Marino 	  replace_typedefs (info, d_left (ret_comp), finder, data);
509a45ae5f8SJohn Marino 	  break;
510a45ae5f8SJohn Marino 
511a45ae5f8SJohn Marino 	default:
512a45ae5f8SJohn Marino 	  break;
513a45ae5f8SJohn Marino 	}
514a45ae5f8SJohn Marino     }
515a45ae5f8SJohn Marino }
516a45ae5f8SJohn Marino 
517a45ae5f8SJohn Marino /* Parse STRING and convert it to canonical form, resolving any typedefs.
518a45ae5f8SJohn Marino    If parsing fails, or if STRING is already canonical, return NULL.
519a45ae5f8SJohn Marino    Otherwise return the canonical form.  The return value is allocated via
520*ef5ccd6cSJohn Marino    xmalloc.  If FINDER is not NULL, then type components are passed to
521*ef5ccd6cSJohn Marino    FINDER to be looked up.  DATA is passed verbatim to FINDER.  */
522a45ae5f8SJohn Marino 
523a45ae5f8SJohn Marino char *
cp_canonicalize_string_full(const char * string,canonicalization_ftype * finder,void * data)524*ef5ccd6cSJohn Marino cp_canonicalize_string_full (const char *string,
525*ef5ccd6cSJohn Marino 			     canonicalization_ftype *finder,
526*ef5ccd6cSJohn Marino 			     void *data)
527a45ae5f8SJohn Marino {
528a45ae5f8SJohn Marino   char *ret;
529a45ae5f8SJohn Marino   unsigned int estimated_len;
530a45ae5f8SJohn Marino   struct demangle_parse_info *info;
531a45ae5f8SJohn Marino 
532a45ae5f8SJohn Marino   ret = NULL;
533a45ae5f8SJohn Marino   estimated_len = strlen (string) * 2;
534a45ae5f8SJohn Marino   info = cp_demangled_name_to_comp (string, NULL);
535a45ae5f8SJohn Marino   if (info != NULL)
536a45ae5f8SJohn Marino     {
537a45ae5f8SJohn Marino       /* Replace all the typedefs in the tree.  */
538*ef5ccd6cSJohn Marino       replace_typedefs (info, info->tree, finder, data);
539a45ae5f8SJohn Marino 
540a45ae5f8SJohn Marino       /* Convert the tree back into a string.  */
541a45ae5f8SJohn Marino       ret = cp_comp_to_string (info->tree, estimated_len);
542a45ae5f8SJohn Marino       gdb_assert (ret != NULL);
543a45ae5f8SJohn Marino 
544a45ae5f8SJohn Marino       /* Free the parse information.  */
545a45ae5f8SJohn Marino       cp_demangled_name_parse_free (info);
546a45ae5f8SJohn Marino 
547a45ae5f8SJohn Marino       /* Finally, compare the original string with the computed
548a45ae5f8SJohn Marino 	 name, returning NULL if they are the same.  */
549a45ae5f8SJohn Marino       if (strcmp (string, ret) == 0)
550a45ae5f8SJohn Marino 	{
551a45ae5f8SJohn Marino 	  xfree (ret);
552a45ae5f8SJohn Marino 	  return NULL;
553a45ae5f8SJohn Marino 	}
554a45ae5f8SJohn Marino     }
555a45ae5f8SJohn Marino 
556a45ae5f8SJohn Marino   return ret;
557a45ae5f8SJohn Marino }
558a45ae5f8SJohn Marino 
559*ef5ccd6cSJohn Marino /* Like cp_canonicalize_string_full, but always passes NULL for
560*ef5ccd6cSJohn Marino    FINDER.  */
561*ef5ccd6cSJohn Marino 
562*ef5ccd6cSJohn Marino char *
cp_canonicalize_string_no_typedefs(const char * string)563*ef5ccd6cSJohn Marino cp_canonicalize_string_no_typedefs (const char *string)
564*ef5ccd6cSJohn Marino {
565*ef5ccd6cSJohn Marino   return cp_canonicalize_string_full (string, NULL, NULL);
566*ef5ccd6cSJohn Marino }
567*ef5ccd6cSJohn Marino 
5685796c8dcSSimon Schubert /* Parse STRING and convert it to canonical form.  If parsing fails,
5695796c8dcSSimon Schubert    or if STRING is already canonical, return NULL.  Otherwise return
5705796c8dcSSimon Schubert    the canonical form.  The return value is allocated via xmalloc.  */
5715796c8dcSSimon Schubert 
5725796c8dcSSimon Schubert char *
cp_canonicalize_string(const char * string)5735796c8dcSSimon Schubert cp_canonicalize_string (const char *string)
5745796c8dcSSimon Schubert {
575a45ae5f8SJohn Marino   struct demangle_parse_info *info;
5765796c8dcSSimon Schubert   unsigned int estimated_len;
5775796c8dcSSimon Schubert   char *ret;
5785796c8dcSSimon Schubert 
5795796c8dcSSimon Schubert   if (cp_already_canonical (string))
5805796c8dcSSimon Schubert     return NULL;
5815796c8dcSSimon Schubert 
582a45ae5f8SJohn Marino   info = cp_demangled_name_to_comp (string, NULL);
583a45ae5f8SJohn Marino   if (info == NULL)
5845796c8dcSSimon Schubert     return NULL;
5855796c8dcSSimon Schubert 
5865796c8dcSSimon Schubert   estimated_len = strlen (string) * 2;
587a45ae5f8SJohn Marino   ret = cp_comp_to_string (info->tree, estimated_len);
588a45ae5f8SJohn Marino   cp_demangled_name_parse_free (info);
5895796c8dcSSimon Schubert 
590*ef5ccd6cSJohn Marino   if (ret == NULL)
591*ef5ccd6cSJohn Marino     {
592*ef5ccd6cSJohn Marino       warning (_("internal error: string \"%s\" failed to be canonicalized"),
593*ef5ccd6cSJohn Marino 	       string);
594*ef5ccd6cSJohn Marino       return NULL;
595*ef5ccd6cSJohn Marino     }
596*ef5ccd6cSJohn Marino 
5975796c8dcSSimon Schubert   if (strcmp (string, ret) == 0)
5985796c8dcSSimon Schubert     {
5995796c8dcSSimon Schubert       xfree (ret);
6005796c8dcSSimon Schubert       return NULL;
6015796c8dcSSimon Schubert     }
6025796c8dcSSimon Schubert 
6035796c8dcSSimon Schubert   return ret;
6045796c8dcSSimon Schubert }
6055796c8dcSSimon Schubert 
606c50c785cSJohn Marino /* Convert a mangled name to a demangle_component tree.  *MEMORY is
607c50c785cSJohn Marino    set to the block of used memory that should be freed when finished
608c50c785cSJohn Marino    with the tree.  DEMANGLED_P is set to the char * that should be
609c50c785cSJohn Marino    freed when finished with the tree, or NULL if none was needed.
610c50c785cSJohn Marino    OPTIONS will be passed to the demangler.  */
6115796c8dcSSimon Schubert 
612a45ae5f8SJohn Marino static struct demangle_parse_info *
mangled_name_to_comp(const char * mangled_name,int options,void ** memory,char ** demangled_p)6135796c8dcSSimon Schubert mangled_name_to_comp (const char *mangled_name, int options,
6145796c8dcSSimon Schubert 		      void **memory, char **demangled_p)
6155796c8dcSSimon Schubert {
6165796c8dcSSimon Schubert   char *demangled_name;
617a45ae5f8SJohn Marino   struct demangle_parse_info *info;
6185796c8dcSSimon Schubert 
6195796c8dcSSimon Schubert   /* If it looks like a v3 mangled name, then try to go directly
6205796c8dcSSimon Schubert      to trees.  */
6215796c8dcSSimon Schubert   if (mangled_name[0] == '_' && mangled_name[1] == 'Z')
6225796c8dcSSimon Schubert     {
623a45ae5f8SJohn Marino       struct demangle_component *ret;
624a45ae5f8SJohn Marino 
625c50c785cSJohn Marino       ret = cplus_demangle_v3_components (mangled_name,
626c50c785cSJohn Marino 					  options, memory);
6275796c8dcSSimon Schubert       if (ret)
6285796c8dcSSimon Schubert 	{
629a45ae5f8SJohn Marino 	  info = cp_new_demangle_parse_info ();
630a45ae5f8SJohn Marino 	  info->tree = ret;
6315796c8dcSSimon Schubert 	  *demangled_p = NULL;
632a45ae5f8SJohn Marino 	  return info;
6335796c8dcSSimon Schubert 	}
6345796c8dcSSimon Schubert     }
6355796c8dcSSimon Schubert 
636c50c785cSJohn Marino   /* If it doesn't, or if that failed, then try to demangle the
637c50c785cSJohn Marino      name.  */
6385796c8dcSSimon Schubert   demangled_name = cplus_demangle (mangled_name, options);
6395796c8dcSSimon Schubert   if (demangled_name == NULL)
6405796c8dcSSimon Schubert    return NULL;
6415796c8dcSSimon Schubert 
642c50c785cSJohn Marino   /* If we could demangle the name, parse it to build the component
643c50c785cSJohn Marino      tree.  */
644a45ae5f8SJohn Marino   info = cp_demangled_name_to_comp (demangled_name, NULL);
6455796c8dcSSimon Schubert 
646a45ae5f8SJohn Marino   if (info == NULL)
6475796c8dcSSimon Schubert     {
6485796c8dcSSimon Schubert       xfree (demangled_name);
6495796c8dcSSimon Schubert       return NULL;
6505796c8dcSSimon Schubert     }
6515796c8dcSSimon Schubert 
6525796c8dcSSimon Schubert   *demangled_p = demangled_name;
653a45ae5f8SJohn Marino   return info;
6545796c8dcSSimon Schubert }
6555796c8dcSSimon Schubert 
6565796c8dcSSimon Schubert /* Return the name of the class containing method PHYSNAME.  */
6575796c8dcSSimon Schubert 
6585796c8dcSSimon Schubert char *
cp_class_name_from_physname(const char * physname)6595796c8dcSSimon Schubert cp_class_name_from_physname (const char *physname)
6605796c8dcSSimon Schubert {
6615796c8dcSSimon Schubert   void *storage = NULL;
6625796c8dcSSimon Schubert   char *demangled_name = NULL, *ret;
6635796c8dcSSimon Schubert   struct demangle_component *ret_comp, *prev_comp, *cur_comp;
664a45ae5f8SJohn Marino   struct demangle_parse_info *info;
6655796c8dcSSimon Schubert   int done;
6665796c8dcSSimon Schubert 
667a45ae5f8SJohn Marino   info = mangled_name_to_comp (physname, DMGL_ANSI,
668c50c785cSJohn Marino 			       &storage, &demangled_name);
669a45ae5f8SJohn Marino   if (info == NULL)
6705796c8dcSSimon Schubert     return NULL;
6715796c8dcSSimon Schubert 
6725796c8dcSSimon Schubert   done = 0;
673a45ae5f8SJohn Marino   ret_comp = info->tree;
6745796c8dcSSimon Schubert 
675c50c785cSJohn Marino   /* First strip off any qualifiers, if we have a function or
676c50c785cSJohn Marino      method.  */
6775796c8dcSSimon Schubert   while (!done)
6785796c8dcSSimon Schubert     switch (ret_comp->type)
6795796c8dcSSimon Schubert       {
6805796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_CONST:
6815796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_RESTRICT:
6825796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_VOLATILE:
6835796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_CONST_THIS:
6845796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_RESTRICT_THIS:
6855796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_VOLATILE_THIS:
6865796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
6875796c8dcSSimon Schubert         ret_comp = d_left (ret_comp);
6885796c8dcSSimon Schubert         break;
6895796c8dcSSimon Schubert       default:
6905796c8dcSSimon Schubert 	done = 1;
6915796c8dcSSimon Schubert 	break;
6925796c8dcSSimon Schubert       }
6935796c8dcSSimon Schubert 
6945796c8dcSSimon Schubert   /* If what we have now is a function, discard the argument list.  */
6955796c8dcSSimon Schubert   if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
6965796c8dcSSimon Schubert     ret_comp = d_left (ret_comp);
6975796c8dcSSimon Schubert 
6985796c8dcSSimon Schubert   /* If what we have now is a template, strip off the template
6995796c8dcSSimon Schubert      arguments.  The left subtree may be a qualified name.  */
7005796c8dcSSimon Schubert   if (ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE)
7015796c8dcSSimon Schubert     ret_comp = d_left (ret_comp);
7025796c8dcSSimon Schubert 
703c50c785cSJohn Marino   /* What we have now should be a name, possibly qualified.
704c50c785cSJohn Marino      Additional qualifiers could live in the left subtree or the right
705c50c785cSJohn Marino      subtree.  Find the last piece.  */
7065796c8dcSSimon Schubert   done = 0;
7075796c8dcSSimon Schubert   prev_comp = NULL;
7085796c8dcSSimon Schubert   cur_comp = ret_comp;
7095796c8dcSSimon Schubert   while (!done)
7105796c8dcSSimon Schubert     switch (cur_comp->type)
7115796c8dcSSimon Schubert       {
7125796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_QUAL_NAME:
7135796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_LOCAL_NAME:
7145796c8dcSSimon Schubert 	prev_comp = cur_comp;
7155796c8dcSSimon Schubert         cur_comp = d_right (cur_comp);
7165796c8dcSSimon Schubert         break;
7175796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_TEMPLATE:
7185796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_NAME:
7195796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_CTOR:
7205796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_DTOR:
7215796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_OPERATOR:
7225796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
7235796c8dcSSimon Schubert 	done = 1;
7245796c8dcSSimon Schubert 	break;
7255796c8dcSSimon Schubert       default:
7265796c8dcSSimon Schubert 	done = 1;
7275796c8dcSSimon Schubert 	cur_comp = NULL;
7285796c8dcSSimon Schubert 	break;
7295796c8dcSSimon Schubert       }
7305796c8dcSSimon Schubert 
7315796c8dcSSimon Schubert   ret = NULL;
7325796c8dcSSimon Schubert   if (cur_comp != NULL && prev_comp != NULL)
7335796c8dcSSimon Schubert     {
7345796c8dcSSimon Schubert       /* We want to discard the rightmost child of PREV_COMP.  */
7355796c8dcSSimon Schubert       *prev_comp = *d_left (prev_comp);
736c50c785cSJohn Marino       /* The ten is completely arbitrary; we don't have a good
737c50c785cSJohn Marino 	 estimate.  */
7385796c8dcSSimon Schubert       ret = cp_comp_to_string (ret_comp, 10);
7395796c8dcSSimon Schubert     }
7405796c8dcSSimon Schubert 
7415796c8dcSSimon Schubert   xfree (storage);
7425796c8dcSSimon Schubert   xfree (demangled_name);
743a45ae5f8SJohn Marino   cp_demangled_name_parse_free (info);
7445796c8dcSSimon Schubert   return ret;
7455796c8dcSSimon Schubert }
7465796c8dcSSimon Schubert 
747c50c785cSJohn Marino /* Return the child of COMP which is the basename of a method,
748c50c785cSJohn Marino    variable, et cetera.  All scope qualifiers are discarded, but
749c50c785cSJohn Marino    template arguments will be included.  The component tree may be
750c50c785cSJohn Marino    modified.  */
7515796c8dcSSimon Schubert 
7525796c8dcSSimon Schubert static struct demangle_component *
unqualified_name_from_comp(struct demangle_component * comp)7535796c8dcSSimon Schubert unqualified_name_from_comp (struct demangle_component *comp)
7545796c8dcSSimon Schubert {
7555796c8dcSSimon Schubert   struct demangle_component *ret_comp = comp, *last_template;
7565796c8dcSSimon Schubert   int done;
7575796c8dcSSimon Schubert 
7585796c8dcSSimon Schubert   done = 0;
7595796c8dcSSimon Schubert   last_template = NULL;
7605796c8dcSSimon Schubert   while (!done)
7615796c8dcSSimon Schubert     switch (ret_comp->type)
7625796c8dcSSimon Schubert       {
7635796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_QUAL_NAME:
7645796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_LOCAL_NAME:
7655796c8dcSSimon Schubert         ret_comp = d_right (ret_comp);
7665796c8dcSSimon Schubert         break;
7675796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_TYPED_NAME:
7685796c8dcSSimon Schubert         ret_comp = d_left (ret_comp);
7695796c8dcSSimon Schubert         break;
7705796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_TEMPLATE:
7715796c8dcSSimon Schubert 	gdb_assert (last_template == NULL);
7725796c8dcSSimon Schubert 	last_template = ret_comp;
7735796c8dcSSimon Schubert 	ret_comp = d_left (ret_comp);
7745796c8dcSSimon Schubert 	break;
7755796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_CONST:
7765796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_RESTRICT:
7775796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_VOLATILE:
7785796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_CONST_THIS:
7795796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_RESTRICT_THIS:
7805796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_VOLATILE_THIS:
7815796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
7825796c8dcSSimon Schubert         ret_comp = d_left (ret_comp);
7835796c8dcSSimon Schubert         break;
7845796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_NAME:
7855796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_CTOR:
7865796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_DTOR:
7875796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_OPERATOR:
7885796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
7895796c8dcSSimon Schubert 	done = 1;
7905796c8dcSSimon Schubert 	break;
7915796c8dcSSimon Schubert       default:
7925796c8dcSSimon Schubert 	return NULL;
7935796c8dcSSimon Schubert 	break;
7945796c8dcSSimon Schubert       }
7955796c8dcSSimon Schubert 
7965796c8dcSSimon Schubert   if (last_template)
7975796c8dcSSimon Schubert     {
7985796c8dcSSimon Schubert       d_left (last_template) = ret_comp;
7995796c8dcSSimon Schubert       return last_template;
8005796c8dcSSimon Schubert     }
8015796c8dcSSimon Schubert 
8025796c8dcSSimon Schubert   return ret_comp;
8035796c8dcSSimon Schubert }
8045796c8dcSSimon Schubert 
8055796c8dcSSimon Schubert /* Return the name of the method whose linkage name is PHYSNAME.  */
8065796c8dcSSimon Schubert 
8075796c8dcSSimon Schubert char *
method_name_from_physname(const char * physname)8085796c8dcSSimon Schubert method_name_from_physname (const char *physname)
8095796c8dcSSimon Schubert {
8105796c8dcSSimon Schubert   void *storage = NULL;
8115796c8dcSSimon Schubert   char *demangled_name = NULL, *ret;
8125796c8dcSSimon Schubert   struct demangle_component *ret_comp;
813a45ae5f8SJohn Marino   struct demangle_parse_info *info;
8145796c8dcSSimon Schubert 
815a45ae5f8SJohn Marino   info = mangled_name_to_comp (physname, DMGL_ANSI,
816c50c785cSJohn Marino 			       &storage, &demangled_name);
817a45ae5f8SJohn Marino   if (info == NULL)
8185796c8dcSSimon Schubert     return NULL;
8195796c8dcSSimon Schubert 
820a45ae5f8SJohn Marino   ret_comp = unqualified_name_from_comp (info->tree);
8215796c8dcSSimon Schubert 
8225796c8dcSSimon Schubert   ret = NULL;
8235796c8dcSSimon Schubert   if (ret_comp != NULL)
824c50c785cSJohn Marino     /* The ten is completely arbitrary; we don't have a good
825c50c785cSJohn Marino        estimate.  */
8265796c8dcSSimon Schubert     ret = cp_comp_to_string (ret_comp, 10);
8275796c8dcSSimon Schubert 
8285796c8dcSSimon Schubert   xfree (storage);
8295796c8dcSSimon Schubert   xfree (demangled_name);
830a45ae5f8SJohn Marino   cp_demangled_name_parse_free (info);
8315796c8dcSSimon Schubert   return ret;
8325796c8dcSSimon Schubert }
8335796c8dcSSimon Schubert 
8345796c8dcSSimon Schubert /* If FULL_NAME is the demangled name of a C++ function (including an
8355796c8dcSSimon Schubert    arg list, possibly including namespace/class qualifications),
8365796c8dcSSimon Schubert    return a new string containing only the function name (without the
8375796c8dcSSimon Schubert    arg list/class qualifications).  Otherwise, return NULL.  The
8385796c8dcSSimon Schubert    caller is responsible for freeing the memory in question.  */
8395796c8dcSSimon Schubert 
8405796c8dcSSimon Schubert char *
cp_func_name(const char * full_name)8415796c8dcSSimon Schubert cp_func_name (const char *full_name)
8425796c8dcSSimon Schubert {
8435796c8dcSSimon Schubert   char *ret;
8445796c8dcSSimon Schubert   struct demangle_component *ret_comp;
845a45ae5f8SJohn Marino   struct demangle_parse_info *info;
8465796c8dcSSimon Schubert 
847a45ae5f8SJohn Marino   info = cp_demangled_name_to_comp (full_name, NULL);
848a45ae5f8SJohn Marino   if (!info)
8495796c8dcSSimon Schubert     return NULL;
8505796c8dcSSimon Schubert 
851a45ae5f8SJohn Marino   ret_comp = unqualified_name_from_comp (info->tree);
8525796c8dcSSimon Schubert 
8535796c8dcSSimon Schubert   ret = NULL;
8545796c8dcSSimon Schubert   if (ret_comp != NULL)
8555796c8dcSSimon Schubert     ret = cp_comp_to_string (ret_comp, 10);
8565796c8dcSSimon Schubert 
857a45ae5f8SJohn Marino   cp_demangled_name_parse_free (info);
8585796c8dcSSimon Schubert   return ret;
8595796c8dcSSimon Schubert }
8605796c8dcSSimon Schubert 
8615796c8dcSSimon Schubert /* DEMANGLED_NAME is the name of a function, including parameters and
8625796c8dcSSimon Schubert    (optionally) a return type.  Return the name of the function without
8635796c8dcSSimon Schubert    parameters or return type, or NULL if we can not parse the name.  */
8645796c8dcSSimon Schubert 
8655796c8dcSSimon Schubert char *
cp_remove_params(const char * demangled_name)8665796c8dcSSimon Schubert cp_remove_params (const char *demangled_name)
8675796c8dcSSimon Schubert {
8685796c8dcSSimon Schubert   int done = 0;
8695796c8dcSSimon Schubert   struct demangle_component *ret_comp;
870a45ae5f8SJohn Marino   struct demangle_parse_info *info;
8715796c8dcSSimon Schubert   char *ret = NULL;
8725796c8dcSSimon Schubert 
8735796c8dcSSimon Schubert   if (demangled_name == NULL)
8745796c8dcSSimon Schubert     return NULL;
8755796c8dcSSimon Schubert 
876a45ae5f8SJohn Marino   info = cp_demangled_name_to_comp (demangled_name, NULL);
877a45ae5f8SJohn Marino   if (info == NULL)
8785796c8dcSSimon Schubert     return NULL;
8795796c8dcSSimon Schubert 
8805796c8dcSSimon Schubert   /* First strip off any qualifiers, if we have a function or method.  */
881a45ae5f8SJohn Marino   ret_comp = info->tree;
8825796c8dcSSimon Schubert   while (!done)
8835796c8dcSSimon Schubert     switch (ret_comp->type)
8845796c8dcSSimon Schubert       {
8855796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_CONST:
8865796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_RESTRICT:
8875796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_VOLATILE:
8885796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_CONST_THIS:
8895796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_RESTRICT_THIS:
8905796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_VOLATILE_THIS:
8915796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
8925796c8dcSSimon Schubert         ret_comp = d_left (ret_comp);
8935796c8dcSSimon Schubert         break;
8945796c8dcSSimon Schubert       default:
8955796c8dcSSimon Schubert 	done = 1;
8965796c8dcSSimon Schubert 	break;
8975796c8dcSSimon Schubert       }
8985796c8dcSSimon Schubert 
8995796c8dcSSimon Schubert   /* What we have now should be a function.  Return its name.  */
9005796c8dcSSimon Schubert   if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
9015796c8dcSSimon Schubert     ret = cp_comp_to_string (d_left (ret_comp), 10);
9025796c8dcSSimon Schubert 
903a45ae5f8SJohn Marino   cp_demangled_name_parse_free (info);
9045796c8dcSSimon Schubert   return ret;
9055796c8dcSSimon Schubert }
9065796c8dcSSimon Schubert 
9075796c8dcSSimon Schubert /* Here are some random pieces of trivia to keep in mind while trying
9085796c8dcSSimon Schubert    to take apart demangled names:
9095796c8dcSSimon Schubert 
9105796c8dcSSimon Schubert    - Names can contain function arguments or templates, so the process
9115796c8dcSSimon Schubert      has to be, to some extent recursive: maybe keep track of your
9125796c8dcSSimon Schubert      depth based on encountering <> and ().
9135796c8dcSSimon Schubert 
9145796c8dcSSimon Schubert    - Parentheses don't just have to happen at the end of a name: they
9155796c8dcSSimon Schubert      can occur even if the name in question isn't a function, because
9165796c8dcSSimon Schubert      a template argument might be a type that's a function.
9175796c8dcSSimon Schubert 
9185796c8dcSSimon Schubert    - Conversely, even if you're trying to deal with a function, its
9195796c8dcSSimon Schubert      demangled name might not end with ')': it could be a const or
9205796c8dcSSimon Schubert      volatile class method, in which case it ends with "const" or
9215796c8dcSSimon Schubert      "volatile".
9225796c8dcSSimon Schubert 
9235796c8dcSSimon Schubert    - Parentheses are also used in anonymous namespaces: a variable
9245796c8dcSSimon Schubert      'foo' in an anonymous namespace gets demangled as "(anonymous
9255796c8dcSSimon Schubert      namespace)::foo".
9265796c8dcSSimon Schubert 
9275796c8dcSSimon Schubert    - And operator names can contain parentheses or angle brackets.  */
9285796c8dcSSimon Schubert 
9295796c8dcSSimon Schubert /* FIXME: carlton/2003-03-13: We have several functions here with
9305796c8dcSSimon Schubert    overlapping functionality; can we combine them?  Also, do they
9315796c8dcSSimon Schubert    handle all the above considerations correctly?  */
9325796c8dcSSimon Schubert 
9335796c8dcSSimon Schubert 
9345796c8dcSSimon Schubert /* This returns the length of first component of NAME, which should be
9355796c8dcSSimon Schubert    the demangled name of a C++ variable/function/method/etc.
9365796c8dcSSimon Schubert    Specifically, it returns the index of the first colon forming the
9375796c8dcSSimon Schubert    boundary of the first component: so, given 'A::foo' or 'A::B::foo'
9385796c8dcSSimon Schubert    it returns the 1, and given 'foo', it returns 0.  */
9395796c8dcSSimon Schubert 
9405796c8dcSSimon Schubert /* The character in NAME indexed by the return value is guaranteed to
9415796c8dcSSimon Schubert    always be either ':' or '\0'.  */
9425796c8dcSSimon Schubert 
9435796c8dcSSimon Schubert /* NOTE: carlton/2003-03-13: This function is currently only intended
9445796c8dcSSimon Schubert    for internal use: it's probably not entirely safe when called on
9455796c8dcSSimon Schubert    user-generated input, because some of the 'index += 2' lines in
9465796c8dcSSimon Schubert    cp_find_first_component_aux might go past the end of malformed
9475796c8dcSSimon Schubert    input.  */
9485796c8dcSSimon Schubert 
9495796c8dcSSimon Schubert unsigned int
cp_find_first_component(const char * name)9505796c8dcSSimon Schubert cp_find_first_component (const char *name)
9515796c8dcSSimon Schubert {
9525796c8dcSSimon Schubert   return cp_find_first_component_aux (name, 0);
9535796c8dcSSimon Schubert }
9545796c8dcSSimon Schubert 
9555796c8dcSSimon Schubert /* Helper function for cp_find_first_component.  Like that function,
9565796c8dcSSimon Schubert    it returns the length of the first component of NAME, but to make
9575796c8dcSSimon Schubert    the recursion easier, it also stops if it reaches an unexpected ')'
9585796c8dcSSimon Schubert    or '>' if the value of PERMISSIVE is nonzero.  */
9595796c8dcSSimon Schubert 
9605796c8dcSSimon Schubert /* Let's optimize away calls to strlen("operator").  */
9615796c8dcSSimon Schubert 
9625796c8dcSSimon Schubert #define LENGTH_OF_OPERATOR 8
9635796c8dcSSimon Schubert 
9645796c8dcSSimon Schubert static unsigned int
cp_find_first_component_aux(const char * name,int permissive)9655796c8dcSSimon Schubert cp_find_first_component_aux (const char *name, int permissive)
9665796c8dcSSimon Schubert {
9675796c8dcSSimon Schubert   unsigned int index = 0;
9685796c8dcSSimon Schubert   /* Operator names can show up in unexpected places.  Since these can
9695796c8dcSSimon Schubert      contain parentheses or angle brackets, they can screw up the
9705796c8dcSSimon Schubert      recursion.  But not every string 'operator' is part of an
9715796c8dcSSimon Schubert      operater name: e.g. you could have a variable 'cooperator'.  So
9725796c8dcSSimon Schubert      this variable tells us whether or not we should treat the string
9735796c8dcSSimon Schubert      'operator' as starting an operator.  */
9745796c8dcSSimon Schubert   int operator_possible = 1;
9755796c8dcSSimon Schubert 
9765796c8dcSSimon Schubert   for (;; ++index)
9775796c8dcSSimon Schubert     {
9785796c8dcSSimon Schubert       switch (name[index])
9795796c8dcSSimon Schubert 	{
9805796c8dcSSimon Schubert 	case '<':
9815796c8dcSSimon Schubert 	  /* Template; eat it up.  The calls to cp_first_component
9825796c8dcSSimon Schubert 	     should only return (I hope!) when they reach the '>'
9835796c8dcSSimon Schubert 	     terminating the component or a '::' between two
9845796c8dcSSimon Schubert 	     components.  (Hence the '+ 2'.)  */
9855796c8dcSSimon Schubert 	  index += 1;
9865796c8dcSSimon Schubert 	  for (index += cp_find_first_component_aux (name + index, 1);
9875796c8dcSSimon Schubert 	       name[index] != '>';
9885796c8dcSSimon Schubert 	       index += cp_find_first_component_aux (name + index, 1))
9895796c8dcSSimon Schubert 	    {
9905796c8dcSSimon Schubert 	      if (name[index] != ':')
9915796c8dcSSimon Schubert 		{
9925796c8dcSSimon Schubert 		  demangled_name_complaint (name);
9935796c8dcSSimon Schubert 		  return strlen (name);
9945796c8dcSSimon Schubert 		}
9955796c8dcSSimon Schubert 	      index += 2;
9965796c8dcSSimon Schubert 	    }
9975796c8dcSSimon Schubert 	  operator_possible = 1;
9985796c8dcSSimon Schubert 	  break;
9995796c8dcSSimon Schubert 	case '(':
10005796c8dcSSimon Schubert 	  /* Similar comment as to '<'.  */
10015796c8dcSSimon Schubert 	  index += 1;
10025796c8dcSSimon Schubert 	  for (index += cp_find_first_component_aux (name + index, 1);
10035796c8dcSSimon Schubert 	       name[index] != ')';
10045796c8dcSSimon Schubert 	       index += cp_find_first_component_aux (name + index, 1))
10055796c8dcSSimon Schubert 	    {
10065796c8dcSSimon Schubert 	      if (name[index] != ':')
10075796c8dcSSimon Schubert 		{
10085796c8dcSSimon Schubert 		  demangled_name_complaint (name);
10095796c8dcSSimon Schubert 		  return strlen (name);
10105796c8dcSSimon Schubert 		}
10115796c8dcSSimon Schubert 	      index += 2;
10125796c8dcSSimon Schubert 	    }
10135796c8dcSSimon Schubert 	  operator_possible = 1;
10145796c8dcSSimon Schubert 	  break;
10155796c8dcSSimon Schubert 	case '>':
10165796c8dcSSimon Schubert 	case ')':
10175796c8dcSSimon Schubert 	  if (permissive)
10185796c8dcSSimon Schubert 	    return index;
10195796c8dcSSimon Schubert 	  else
10205796c8dcSSimon Schubert 	    {
10215796c8dcSSimon Schubert 	      demangled_name_complaint (name);
10225796c8dcSSimon Schubert 	      return strlen (name);
10235796c8dcSSimon Schubert 	    }
10245796c8dcSSimon Schubert 	case '\0':
10255796c8dcSSimon Schubert 	case ':':
10265796c8dcSSimon Schubert 	  return index;
10275796c8dcSSimon Schubert 	case 'o':
10285796c8dcSSimon Schubert 	  /* Operator names can screw up the recursion.  */
10295796c8dcSSimon Schubert 	  if (operator_possible
1030c50c785cSJohn Marino 	      && strncmp (name + index, "operator",
1031c50c785cSJohn Marino 			  LENGTH_OF_OPERATOR) == 0)
10325796c8dcSSimon Schubert 	    {
10335796c8dcSSimon Schubert 	      index += LENGTH_OF_OPERATOR;
10345796c8dcSSimon Schubert 	      while (ISSPACE(name[index]))
10355796c8dcSSimon Schubert 		++index;
10365796c8dcSSimon Schubert 	      switch (name[index])
10375796c8dcSSimon Schubert 		{
10385796c8dcSSimon Schubert 		  /* Skip over one less than the appropriate number of
10395796c8dcSSimon Schubert 		     characters: the for loop will skip over the last
10405796c8dcSSimon Schubert 		     one.  */
10415796c8dcSSimon Schubert 		case '<':
10425796c8dcSSimon Schubert 		  if (name[index + 1] == '<')
10435796c8dcSSimon Schubert 		    index += 1;
10445796c8dcSSimon Schubert 		  else
10455796c8dcSSimon Schubert 		    index += 0;
10465796c8dcSSimon Schubert 		  break;
10475796c8dcSSimon Schubert 		case '>':
10485796c8dcSSimon Schubert 		case '-':
10495796c8dcSSimon Schubert 		  if (name[index + 1] == '>')
10505796c8dcSSimon Schubert 		    index += 1;
10515796c8dcSSimon Schubert 		  else
10525796c8dcSSimon Schubert 		    index += 0;
10535796c8dcSSimon Schubert 		  break;
10545796c8dcSSimon Schubert 		case '(':
10555796c8dcSSimon Schubert 		  index += 1;
10565796c8dcSSimon Schubert 		  break;
10575796c8dcSSimon Schubert 		default:
10585796c8dcSSimon Schubert 		  index += 0;
10595796c8dcSSimon Schubert 		  break;
10605796c8dcSSimon Schubert 		}
10615796c8dcSSimon Schubert 	    }
10625796c8dcSSimon Schubert 	  operator_possible = 0;
10635796c8dcSSimon Schubert 	  break;
10645796c8dcSSimon Schubert 	case ' ':
10655796c8dcSSimon Schubert 	case ',':
10665796c8dcSSimon Schubert 	case '.':
10675796c8dcSSimon Schubert 	case '&':
10685796c8dcSSimon Schubert 	case '*':
10695796c8dcSSimon Schubert 	  /* NOTE: carlton/2003-04-18: I'm not sure what the precise
10705796c8dcSSimon Schubert 	     set of relevant characters are here: it's necessary to
10715796c8dcSSimon Schubert 	     include any character that can show up before 'operator'
10725796c8dcSSimon Schubert 	     in a demangled name, and it's safe to include any
10735796c8dcSSimon Schubert 	     character that can't be part of an identifier's name.  */
10745796c8dcSSimon Schubert 	  operator_possible = 1;
10755796c8dcSSimon Schubert 	  break;
10765796c8dcSSimon Schubert 	default:
10775796c8dcSSimon Schubert 	  operator_possible = 0;
10785796c8dcSSimon Schubert 	  break;
10795796c8dcSSimon Schubert 	}
10805796c8dcSSimon Schubert     }
10815796c8dcSSimon Schubert }
10825796c8dcSSimon Schubert 
10835796c8dcSSimon Schubert /* Complain about a demangled name that we don't know how to parse.
10845796c8dcSSimon Schubert    NAME is the demangled name in question.  */
10855796c8dcSSimon Schubert 
10865796c8dcSSimon Schubert static void
demangled_name_complaint(const char * name)10875796c8dcSSimon Schubert demangled_name_complaint (const char *name)
10885796c8dcSSimon Schubert {
10895796c8dcSSimon Schubert   complaint (&symfile_complaints,
10905796c8dcSSimon Schubert 	     "unexpected demangled name '%s'", name);
10915796c8dcSSimon Schubert }
10925796c8dcSSimon Schubert 
10935796c8dcSSimon Schubert /* If NAME is the fully-qualified name of a C++
10945796c8dcSSimon Schubert    function/variable/method/etc., this returns the length of its
10955796c8dcSSimon Schubert    entire prefix: all of the namespaces and classes that make up its
10965796c8dcSSimon Schubert    name.  Given 'A::foo', it returns 1, given 'A::B::foo', it returns
10975796c8dcSSimon Schubert    4, given 'foo', it returns 0.  */
10985796c8dcSSimon Schubert 
10995796c8dcSSimon Schubert unsigned int
cp_entire_prefix_len(const char * name)11005796c8dcSSimon Schubert cp_entire_prefix_len (const char *name)
11015796c8dcSSimon Schubert {
11025796c8dcSSimon Schubert   unsigned int current_len = cp_find_first_component (name);
11035796c8dcSSimon Schubert   unsigned int previous_len = 0;
11045796c8dcSSimon Schubert 
11055796c8dcSSimon Schubert   while (name[current_len] != '\0')
11065796c8dcSSimon Schubert     {
11075796c8dcSSimon Schubert       gdb_assert (name[current_len] == ':');
11085796c8dcSSimon Schubert       previous_len = current_len;
11095796c8dcSSimon Schubert       /* Skip the '::'.  */
11105796c8dcSSimon Schubert       current_len += 2;
11115796c8dcSSimon Schubert       current_len += cp_find_first_component (name + current_len);
11125796c8dcSSimon Schubert     }
11135796c8dcSSimon Schubert 
11145796c8dcSSimon Schubert   return previous_len;
11155796c8dcSSimon Schubert }
11165796c8dcSSimon Schubert 
11175796c8dcSSimon Schubert /* Overload resolution functions.  */
11185796c8dcSSimon Schubert 
11195796c8dcSSimon Schubert /* Test to see if SYM is a symbol that we haven't seen corresponding
11205796c8dcSSimon Schubert    to a function named OLOAD_NAME.  If so, add it to the current
11215796c8dcSSimon Schubert    completion list.  */
11225796c8dcSSimon Schubert 
11235796c8dcSSimon Schubert static void
overload_list_add_symbol(struct symbol * sym,const char * oload_name)1124c50c785cSJohn Marino overload_list_add_symbol (struct symbol *sym,
1125c50c785cSJohn Marino 			  const char *oload_name)
11265796c8dcSSimon Schubert {
11275796c8dcSSimon Schubert   int newsize;
11285796c8dcSSimon Schubert   int i;
11295796c8dcSSimon Schubert   char *sym_name;
11305796c8dcSSimon Schubert 
1131c50c785cSJohn Marino   /* If there is no type information, we can't do anything, so
1132c50c785cSJohn Marino      skip.  */
11335796c8dcSSimon Schubert   if (SYMBOL_TYPE (sym) == NULL)
11345796c8dcSSimon Schubert     return;
11355796c8dcSSimon Schubert 
11365796c8dcSSimon Schubert   /* skip any symbols that we've already considered.  */
11375796c8dcSSimon Schubert   for (i = 0; i < sym_return_val_index; ++i)
11385796c8dcSSimon Schubert     if (strcmp (SYMBOL_LINKAGE_NAME (sym),
11395796c8dcSSimon Schubert 		SYMBOL_LINKAGE_NAME (sym_return_val[i])) == 0)
11405796c8dcSSimon Schubert       return;
11415796c8dcSSimon Schubert 
11425796c8dcSSimon Schubert   /* Get the demangled name without parameters */
11435796c8dcSSimon Schubert   sym_name = cp_remove_params (SYMBOL_NATURAL_NAME (sym));
11445796c8dcSSimon Schubert   if (!sym_name)
11455796c8dcSSimon Schubert     return;
11465796c8dcSSimon Schubert 
11475796c8dcSSimon Schubert   /* skip symbols that cannot match */
11485796c8dcSSimon Schubert   if (strcmp (sym_name, oload_name) != 0)
11495796c8dcSSimon Schubert     {
11505796c8dcSSimon Schubert       xfree (sym_name);
11515796c8dcSSimon Schubert       return;
11525796c8dcSSimon Schubert     }
11535796c8dcSSimon Schubert 
11545796c8dcSSimon Schubert   xfree (sym_name);
11555796c8dcSSimon Schubert 
1156c50c785cSJohn Marino   /* We have a match for an overload instance, so add SYM to the
1157c50c785cSJohn Marino      current list of overload instances */
11585796c8dcSSimon Schubert   if (sym_return_val_index + 3 > sym_return_val_size)
11595796c8dcSSimon Schubert     {
11605796c8dcSSimon Schubert       newsize = (sym_return_val_size *= 2) * sizeof (struct symbol *);
1161c50c785cSJohn Marino       sym_return_val = (struct symbol **)
1162c50c785cSJohn Marino 	xrealloc ((char *) sym_return_val, newsize);
11635796c8dcSSimon Schubert     }
11645796c8dcSSimon Schubert   sym_return_val[sym_return_val_index++] = sym;
11655796c8dcSSimon Schubert   sym_return_val[sym_return_val_index] = NULL;
11665796c8dcSSimon Schubert }
11675796c8dcSSimon Schubert 
11685796c8dcSSimon Schubert /* Return a null-terminated list of pointers to function symbols that
11695796c8dcSSimon Schubert    are named FUNC_NAME and are visible within NAMESPACE.  */
11705796c8dcSSimon Schubert 
11715796c8dcSSimon Schubert struct symbol **
make_symbol_overload_list(const char * func_name,const char * namespace)11725796c8dcSSimon Schubert make_symbol_overload_list (const char *func_name,
11735796c8dcSSimon Schubert 			   const char *namespace)
11745796c8dcSSimon Schubert {
11755796c8dcSSimon Schubert   struct cleanup *old_cleanups;
1176cf7f2e2dSJohn Marino   const char *name;
11775796c8dcSSimon Schubert 
11785796c8dcSSimon Schubert   sym_return_val_size = 100;
11795796c8dcSSimon Schubert   sym_return_val_index = 0;
11805796c8dcSSimon Schubert   sym_return_val = xmalloc ((sym_return_val_size + 1) *
11815796c8dcSSimon Schubert 			    sizeof (struct symbol *));
11825796c8dcSSimon Schubert   sym_return_val[0] = NULL;
11835796c8dcSSimon Schubert 
11845796c8dcSSimon Schubert   old_cleanups = make_cleanup (xfree, sym_return_val);
11855796c8dcSSimon Schubert 
11865796c8dcSSimon Schubert   make_symbol_overload_list_using (func_name, namespace);
11875796c8dcSSimon Schubert 
1188cf7f2e2dSJohn Marino   if (namespace[0] == '\0')
1189cf7f2e2dSJohn Marino     name = func_name;
1190cf7f2e2dSJohn Marino   else
1191cf7f2e2dSJohn Marino     {
1192cf7f2e2dSJohn Marino       char *concatenated_name
1193cf7f2e2dSJohn Marino 	= alloca (strlen (namespace) + 2 + strlen (func_name) + 1);
1194cf7f2e2dSJohn Marino       strcpy (concatenated_name, namespace);
1195cf7f2e2dSJohn Marino       strcat (concatenated_name, "::");
1196cf7f2e2dSJohn Marino       strcat (concatenated_name, func_name);
1197cf7f2e2dSJohn Marino       name = concatenated_name;
1198cf7f2e2dSJohn Marino     }
1199cf7f2e2dSJohn Marino 
1200cf7f2e2dSJohn Marino   make_symbol_overload_list_qualified (name);
1201cf7f2e2dSJohn Marino 
12025796c8dcSSimon Schubert   discard_cleanups (old_cleanups);
12035796c8dcSSimon Schubert 
12045796c8dcSSimon Schubert   return sym_return_val;
12055796c8dcSSimon Schubert }
12065796c8dcSSimon Schubert 
1207cf7f2e2dSJohn Marino /* Add all symbols with a name matching NAME in BLOCK to the overload
1208cf7f2e2dSJohn Marino    list.  */
1209cf7f2e2dSJohn Marino 
1210cf7f2e2dSJohn Marino static void
make_symbol_overload_list_block(const char * name,const struct block * block)1211cf7f2e2dSJohn Marino make_symbol_overload_list_block (const char *name,
1212cf7f2e2dSJohn Marino                                  const struct block *block)
1213cf7f2e2dSJohn Marino {
1214*ef5ccd6cSJohn Marino   struct block_iterator iter;
1215cf7f2e2dSJohn Marino   struct symbol *sym;
1216cf7f2e2dSJohn Marino 
1217*ef5ccd6cSJohn Marino   for (sym = block_iter_name_first (block, name, &iter);
1218cf7f2e2dSJohn Marino        sym != NULL;
1219*ef5ccd6cSJohn Marino        sym = block_iter_name_next (name, &iter))
1220cf7f2e2dSJohn Marino     overload_list_add_symbol (sym, name);
1221cf7f2e2dSJohn Marino }
1222cf7f2e2dSJohn Marino 
1223cf7f2e2dSJohn Marino /* Adds the function FUNC_NAME from NAMESPACE to the overload set.  */
1224cf7f2e2dSJohn Marino 
1225cf7f2e2dSJohn Marino static void
make_symbol_overload_list_namespace(const char * func_name,const char * namespace)1226cf7f2e2dSJohn Marino make_symbol_overload_list_namespace (const char *func_name,
1227cf7f2e2dSJohn Marino                                      const char *namespace)
1228cf7f2e2dSJohn Marino {
1229cf7f2e2dSJohn Marino   const char *name;
1230cf7f2e2dSJohn Marino   const struct block *block = NULL;
1231cf7f2e2dSJohn Marino 
1232cf7f2e2dSJohn Marino   if (namespace[0] == '\0')
1233cf7f2e2dSJohn Marino     name = func_name;
1234cf7f2e2dSJohn Marino   else
1235cf7f2e2dSJohn Marino     {
1236cf7f2e2dSJohn Marino       char *concatenated_name
1237cf7f2e2dSJohn Marino 	= alloca (strlen (namespace) + 2 + strlen (func_name) + 1);
1238cf7f2e2dSJohn Marino 
1239cf7f2e2dSJohn Marino       strcpy (concatenated_name, namespace);
1240cf7f2e2dSJohn Marino       strcat (concatenated_name, "::");
1241cf7f2e2dSJohn Marino       strcat (concatenated_name, func_name);
1242cf7f2e2dSJohn Marino       name = concatenated_name;
1243cf7f2e2dSJohn Marino     }
1244cf7f2e2dSJohn Marino 
1245cf7f2e2dSJohn Marino   /* Look in the static block.  */
1246cf7f2e2dSJohn Marino   block = block_static_block (get_selected_block (0));
1247c50c785cSJohn Marino   if (block)
1248cf7f2e2dSJohn Marino     make_symbol_overload_list_block (name, block);
1249cf7f2e2dSJohn Marino 
1250cf7f2e2dSJohn Marino   /* Look in the global block.  */
1251cf7f2e2dSJohn Marino   block = block_global_block (block);
1252c50c785cSJohn Marino   if (block)
1253cf7f2e2dSJohn Marino     make_symbol_overload_list_block (name, block);
1254cf7f2e2dSJohn Marino 
1255cf7f2e2dSJohn Marino }
1256cf7f2e2dSJohn Marino 
1257c50c785cSJohn Marino /* Search the namespace of the given type and namespace of and public
1258c50c785cSJohn Marino    base types.  */
1259cf7f2e2dSJohn Marino 
1260cf7f2e2dSJohn Marino static void
make_symbol_overload_list_adl_namespace(struct type * type,const char * func_name)1261cf7f2e2dSJohn Marino make_symbol_overload_list_adl_namespace (struct type *type,
1262cf7f2e2dSJohn Marino                                          const char *func_name)
1263cf7f2e2dSJohn Marino {
1264cf7f2e2dSJohn Marino   char *namespace;
1265*ef5ccd6cSJohn Marino   const char *type_name;
1266cf7f2e2dSJohn Marino   int i, prefix_len;
1267cf7f2e2dSJohn Marino 
1268c50c785cSJohn Marino   while (TYPE_CODE (type) == TYPE_CODE_PTR
1269c50c785cSJohn Marino 	 || TYPE_CODE (type) == TYPE_CODE_REF
1270cf7f2e2dSJohn Marino          || TYPE_CODE (type) == TYPE_CODE_ARRAY
1271cf7f2e2dSJohn Marino          || TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1272cf7f2e2dSJohn Marino     {
1273cf7f2e2dSJohn Marino       if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1274cf7f2e2dSJohn Marino 	type = check_typedef(type);
1275cf7f2e2dSJohn Marino       else
1276cf7f2e2dSJohn Marino 	type = TYPE_TARGET_TYPE (type);
1277cf7f2e2dSJohn Marino     }
1278cf7f2e2dSJohn Marino 
1279cf7f2e2dSJohn Marino   type_name = TYPE_NAME (type);
1280cf7f2e2dSJohn Marino 
1281cf7f2e2dSJohn Marino   if (type_name == NULL)
1282cf7f2e2dSJohn Marino     return;
1283cf7f2e2dSJohn Marino 
1284cf7f2e2dSJohn Marino   prefix_len = cp_entire_prefix_len (type_name);
1285cf7f2e2dSJohn Marino 
1286cf7f2e2dSJohn Marino   if (prefix_len != 0)
1287cf7f2e2dSJohn Marino     {
1288cf7f2e2dSJohn Marino       namespace = alloca (prefix_len + 1);
1289cf7f2e2dSJohn Marino       strncpy (namespace, type_name, prefix_len);
1290cf7f2e2dSJohn Marino       namespace[prefix_len] = '\0';
1291cf7f2e2dSJohn Marino 
1292cf7f2e2dSJohn Marino       make_symbol_overload_list_namespace (func_name, namespace);
1293cf7f2e2dSJohn Marino     }
1294cf7f2e2dSJohn Marino 
1295cf7f2e2dSJohn Marino   /* Check public base type */
1296cf7f2e2dSJohn Marino   if (TYPE_CODE (type) == TYPE_CODE_CLASS)
1297cf7f2e2dSJohn Marino     for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
1298cf7f2e2dSJohn Marino       {
1299cf7f2e2dSJohn Marino 	if (BASETYPE_VIA_PUBLIC (type, i))
1300c50c785cSJohn Marino 	  make_symbol_overload_list_adl_namespace (TYPE_BASECLASS (type,
1301c50c785cSJohn Marino 								   i),
1302cf7f2e2dSJohn Marino 						   func_name);
1303cf7f2e2dSJohn Marino       }
1304cf7f2e2dSJohn Marino }
1305cf7f2e2dSJohn Marino 
1306c50c785cSJohn Marino /* Adds the overload list overload candidates for FUNC_NAME found
1307c50c785cSJohn Marino    through argument dependent lookup.  */
1308cf7f2e2dSJohn Marino 
1309cf7f2e2dSJohn Marino struct symbol **
make_symbol_overload_list_adl(struct type ** arg_types,int nargs,const char * func_name)1310cf7f2e2dSJohn Marino make_symbol_overload_list_adl (struct type **arg_types, int nargs,
1311cf7f2e2dSJohn Marino                                const char *func_name)
1312cf7f2e2dSJohn Marino {
1313cf7f2e2dSJohn Marino   int i;
1314cf7f2e2dSJohn Marino 
1315cf7f2e2dSJohn Marino   gdb_assert (sym_return_val_size != -1);
1316cf7f2e2dSJohn Marino 
1317cf7f2e2dSJohn Marino   for (i = 1; i <= nargs; i++)
1318c50c785cSJohn Marino     make_symbol_overload_list_adl_namespace (arg_types[i - 1],
1319c50c785cSJohn Marino 					     func_name);
1320cf7f2e2dSJohn Marino 
1321cf7f2e2dSJohn Marino   return sym_return_val;
1322cf7f2e2dSJohn Marino }
1323cf7f2e2dSJohn Marino 
1324c50c785cSJohn Marino /* Used for cleanups to reset the "searched" flag in case of an
1325c50c785cSJohn Marino    error.  */
1326cf7f2e2dSJohn Marino 
1327cf7f2e2dSJohn Marino static void
reset_directive_searched(void * data)1328cf7f2e2dSJohn Marino reset_directive_searched (void *data)
1329cf7f2e2dSJohn Marino {
1330cf7f2e2dSJohn Marino   struct using_direct *direct = data;
1331cf7f2e2dSJohn Marino   direct->searched = 0;
1332cf7f2e2dSJohn Marino }
1333cf7f2e2dSJohn Marino 
13345796c8dcSSimon Schubert /* This applies the using directives to add namespaces to search in,
13355796c8dcSSimon Schubert    and then searches for overloads in all of those namespaces.  It
13365796c8dcSSimon Schubert    adds the symbols found to sym_return_val.  Arguments are as in
13375796c8dcSSimon Schubert    make_symbol_overload_list.  */
13385796c8dcSSimon Schubert 
13395796c8dcSSimon Schubert static void
make_symbol_overload_list_using(const char * func_name,const char * namespace)13405796c8dcSSimon Schubert make_symbol_overload_list_using (const char *func_name,
13415796c8dcSSimon Schubert 				 const char *namespace)
13425796c8dcSSimon Schubert {
1343cf7f2e2dSJohn Marino   struct using_direct *current;
1344cf7f2e2dSJohn Marino   const struct block *block;
13455796c8dcSSimon Schubert 
13465796c8dcSSimon Schubert   /* First, go through the using directives.  If any of them apply,
13475796c8dcSSimon Schubert      look in the appropriate namespaces for new functions to match
13485796c8dcSSimon Schubert      on.  */
13495796c8dcSSimon Schubert 
1350cf7f2e2dSJohn Marino   for (block = get_selected_block (0);
1351cf7f2e2dSJohn Marino        block != NULL;
1352cf7f2e2dSJohn Marino        block = BLOCK_SUPERBLOCK (block))
1353cf7f2e2dSJohn Marino     for (current = block_using (block);
13545796c8dcSSimon Schubert 	current != NULL;
13555796c8dcSSimon Schubert 	current = current->next)
13565796c8dcSSimon Schubert       {
1357cf7f2e2dSJohn Marino 	/* Prevent recursive calls.  */
1358cf7f2e2dSJohn Marino 	if (current->searched)
1359cf7f2e2dSJohn Marino 	  continue;
1360cf7f2e2dSJohn Marino 
1361c50c785cSJohn Marino         /* If this is a namespace alias or imported declaration ignore
1362c50c785cSJohn Marino 	   it.  */
1363cf7f2e2dSJohn Marino         if (current->alias != NULL || current->declaration != NULL)
1364cf7f2e2dSJohn Marino           continue;
1365cf7f2e2dSJohn Marino 
13665796c8dcSSimon Schubert         if (strcmp (namespace, current->import_dest) == 0)
13675796c8dcSSimon Schubert 	  {
1368c50c785cSJohn Marino 	    /* Mark this import as searched so that the recursive call
1369c50c785cSJohn Marino 	       does not search it again.  */
1370cf7f2e2dSJohn Marino 	    struct cleanup *old_chain;
1371cf7f2e2dSJohn Marino 	    current->searched = 1;
1372c50c785cSJohn Marino 	    old_chain = make_cleanup (reset_directive_searched,
1373c50c785cSJohn Marino 				      current);
1374cf7f2e2dSJohn Marino 
1375c50c785cSJohn Marino 	    make_symbol_overload_list_using (func_name,
1376c50c785cSJohn Marino 					     current->import_src);
1377cf7f2e2dSJohn Marino 
1378cf7f2e2dSJohn Marino 	    current->searched = 0;
1379cf7f2e2dSJohn Marino 	    discard_cleanups (old_chain);
13805796c8dcSSimon Schubert 	  }
13815796c8dcSSimon Schubert       }
13825796c8dcSSimon Schubert 
13835796c8dcSSimon Schubert   /* Now, add names for this namespace.  */
1384cf7f2e2dSJohn Marino   make_symbol_overload_list_namespace (func_name, namespace);
13855796c8dcSSimon Schubert }
13865796c8dcSSimon Schubert 
13875796c8dcSSimon Schubert /* This does the bulk of the work of finding overloaded symbols.
13885796c8dcSSimon Schubert    FUNC_NAME is the name of the overloaded function we're looking for
13895796c8dcSSimon Schubert    (possibly including namespace info).  */
13905796c8dcSSimon Schubert 
13915796c8dcSSimon Schubert static void
make_symbol_overload_list_qualified(const char * func_name)13925796c8dcSSimon Schubert make_symbol_overload_list_qualified (const char *func_name)
13935796c8dcSSimon Schubert {
13945796c8dcSSimon Schubert   struct symtab *s;
13955796c8dcSSimon Schubert   struct objfile *objfile;
13965796c8dcSSimon Schubert   const struct block *b, *surrounding_static_block = 0;
13975796c8dcSSimon Schubert 
1398c50c785cSJohn Marino   /* Look through the partial symtabs for all symbols which begin by
1399c50c785cSJohn Marino      matching FUNC_NAME.  Make sure we read that symbol table in.  */
14005796c8dcSSimon Schubert 
1401cf7f2e2dSJohn Marino   ALL_OBJFILES (objfile)
1402cf7f2e2dSJohn Marino   {
1403cf7f2e2dSJohn Marino     if (objfile->sf)
1404cf7f2e2dSJohn Marino       objfile->sf->qf->expand_symtabs_for_function (objfile, func_name);
1405cf7f2e2dSJohn Marino   }
14065796c8dcSSimon Schubert 
14075796c8dcSSimon Schubert   /* Search upwards from currently selected frame (so that we can
14085796c8dcSSimon Schubert      complete on local vars.  */
14095796c8dcSSimon Schubert 
14105796c8dcSSimon Schubert   for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
1411cf7f2e2dSJohn Marino     make_symbol_overload_list_block (func_name, b);
14125796c8dcSSimon Schubert 
14135796c8dcSSimon Schubert   surrounding_static_block = block_static_block (get_selected_block (0));
14145796c8dcSSimon Schubert 
14155796c8dcSSimon Schubert   /* Go through the symtabs and check the externs and statics for
14165796c8dcSSimon Schubert      symbols which match.  */
14175796c8dcSSimon Schubert 
14185796c8dcSSimon Schubert   ALL_PRIMARY_SYMTABS (objfile, s)
14195796c8dcSSimon Schubert   {
14205796c8dcSSimon Schubert     QUIT;
14215796c8dcSSimon Schubert     b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
1422cf7f2e2dSJohn Marino     make_symbol_overload_list_block (func_name, b);
14235796c8dcSSimon Schubert   }
14245796c8dcSSimon Schubert 
14255796c8dcSSimon Schubert   ALL_PRIMARY_SYMTABS (objfile, s)
14265796c8dcSSimon Schubert   {
14275796c8dcSSimon Schubert     QUIT;
14285796c8dcSSimon Schubert     b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
14295796c8dcSSimon Schubert     /* Don't do this block twice.  */
14305796c8dcSSimon Schubert     if (b == surrounding_static_block)
14315796c8dcSSimon Schubert       continue;
1432cf7f2e2dSJohn Marino     make_symbol_overload_list_block (func_name, b);
14335796c8dcSSimon Schubert   }
14345796c8dcSSimon Schubert }
14355796c8dcSSimon Schubert 
14365796c8dcSSimon Schubert /* Lookup the rtti type for a class name.  */
14375796c8dcSSimon Schubert 
14385796c8dcSSimon Schubert struct type *
cp_lookup_rtti_type(const char * name,struct block * block)14395796c8dcSSimon Schubert cp_lookup_rtti_type (const char *name, struct block *block)
14405796c8dcSSimon Schubert {
14415796c8dcSSimon Schubert   struct symbol * rtti_sym;
14425796c8dcSSimon Schubert   struct type * rtti_type;
14435796c8dcSSimon Schubert 
14445796c8dcSSimon Schubert   rtti_sym = lookup_symbol (name, block, STRUCT_DOMAIN, NULL);
14455796c8dcSSimon Schubert 
14465796c8dcSSimon Schubert   if (rtti_sym == NULL)
14475796c8dcSSimon Schubert     {
14485796c8dcSSimon Schubert       warning (_("RTTI symbol not found for class '%s'"), name);
14495796c8dcSSimon Schubert       return NULL;
14505796c8dcSSimon Schubert     }
14515796c8dcSSimon Schubert 
14525796c8dcSSimon Schubert   if (SYMBOL_CLASS (rtti_sym) != LOC_TYPEDEF)
14535796c8dcSSimon Schubert     {
14545796c8dcSSimon Schubert       warning (_("RTTI symbol for class '%s' is not a type"), name);
14555796c8dcSSimon Schubert       return NULL;
14565796c8dcSSimon Schubert     }
14575796c8dcSSimon Schubert 
14585796c8dcSSimon Schubert   rtti_type = SYMBOL_TYPE (rtti_sym);
14595796c8dcSSimon Schubert 
14605796c8dcSSimon Schubert   switch (TYPE_CODE (rtti_type))
14615796c8dcSSimon Schubert     {
14625796c8dcSSimon Schubert     case TYPE_CODE_CLASS:
14635796c8dcSSimon Schubert       break;
14645796c8dcSSimon Schubert     case TYPE_CODE_NAMESPACE:
14655796c8dcSSimon Schubert       /* chastain/2003-11-26: the symbol tables often contain fake
14665796c8dcSSimon Schubert 	 symbols for namespaces with the same name as the struct.
14675796c8dcSSimon Schubert 	 This warning is an indication of a bug in the lookup order
14685796c8dcSSimon Schubert 	 or a bug in the way that the symbol tables are populated.  */
14695796c8dcSSimon Schubert       warning (_("RTTI symbol for class '%s' is a namespace"), name);
14705796c8dcSSimon Schubert       return NULL;
14715796c8dcSSimon Schubert     default:
14725796c8dcSSimon Schubert       warning (_("RTTI symbol for class '%s' has bad type"), name);
14735796c8dcSSimon Schubert       return NULL;
14745796c8dcSSimon Schubert     }
14755796c8dcSSimon Schubert 
14765796c8dcSSimon Schubert   return rtti_type;
14775796c8dcSSimon Schubert }
14785796c8dcSSimon Schubert 
14795796c8dcSSimon Schubert /* Don't allow just "maintenance cplus".  */
14805796c8dcSSimon Schubert 
14815796c8dcSSimon Schubert static  void
maint_cplus_command(char * arg,int from_tty)14825796c8dcSSimon Schubert maint_cplus_command (char *arg, int from_tty)
14835796c8dcSSimon Schubert {
1484c50c785cSJohn Marino   printf_unfiltered (_("\"maintenance cplus\" must be followed "
1485c50c785cSJohn Marino 		       "by the name of a command.\n"));
1486c50c785cSJohn Marino   help_list (maint_cplus_cmd_list,
1487c50c785cSJohn Marino 	     "maintenance cplus ",
1488c50c785cSJohn Marino 	     -1, gdb_stdout);
14895796c8dcSSimon Schubert }
14905796c8dcSSimon Schubert 
14915796c8dcSSimon Schubert /* This is a front end for cp_find_first_component, for unit testing.
14925796c8dcSSimon Schubert    Be careful when using it: see the NOTE above
14935796c8dcSSimon Schubert    cp_find_first_component.  */
14945796c8dcSSimon Schubert 
14955796c8dcSSimon Schubert static void
first_component_command(char * arg,int from_tty)14965796c8dcSSimon Schubert first_component_command (char *arg, int from_tty)
14975796c8dcSSimon Schubert {
14985796c8dcSSimon Schubert   int len;
14995796c8dcSSimon Schubert   char *prefix;
15005796c8dcSSimon Schubert 
15015796c8dcSSimon Schubert   if (!arg)
15025796c8dcSSimon Schubert     return;
15035796c8dcSSimon Schubert 
15045796c8dcSSimon Schubert   len = cp_find_first_component (arg);
15055796c8dcSSimon Schubert   prefix = alloca (len + 1);
15065796c8dcSSimon Schubert 
15075796c8dcSSimon Schubert   memcpy (prefix, arg, len);
15085796c8dcSSimon Schubert   prefix[len] = '\0';
15095796c8dcSSimon Schubert 
15105796c8dcSSimon Schubert   printf_unfiltered ("%s\n", prefix);
15115796c8dcSSimon Schubert }
15125796c8dcSSimon Schubert 
15135796c8dcSSimon Schubert extern initialize_file_ftype _initialize_cp_support; /* -Wmissing-prototypes */
15145796c8dcSSimon Schubert 
1515cf7f2e2dSJohn Marino 
1516*ef5ccd6cSJohn Marino /* Implement "info vtbl".  */
1517*ef5ccd6cSJohn Marino 
1518*ef5ccd6cSJohn Marino static void
info_vtbl_command(char * arg,int from_tty)1519*ef5ccd6cSJohn Marino info_vtbl_command (char *arg, int from_tty)
1520cf7f2e2dSJohn Marino {
1521*ef5ccd6cSJohn Marino   struct value *value;
1522cf7f2e2dSJohn Marino 
1523*ef5ccd6cSJohn Marino   value = parse_and_eval (arg);
1524*ef5ccd6cSJohn Marino   cplus_print_vtable (value);
1525cf7f2e2dSJohn Marino }
1526cf7f2e2dSJohn Marino 
15275796c8dcSSimon Schubert void
_initialize_cp_support(void)15285796c8dcSSimon Schubert _initialize_cp_support (void)
15295796c8dcSSimon Schubert {
1530c50c785cSJohn Marino   add_prefix_cmd ("cplus", class_maintenance,
1531c50c785cSJohn Marino 		  maint_cplus_command,
1532c50c785cSJohn Marino 		  _("C++ maintenance commands."),
1533c50c785cSJohn Marino 		  &maint_cplus_cmd_list,
1534c50c785cSJohn Marino 		  "maintenance cplus ",
1535c50c785cSJohn Marino 		  0, &maintenancelist);
1536c50c785cSJohn Marino   add_alias_cmd ("cp", "cplus",
1537c50c785cSJohn Marino 		 class_maintenance, 1,
1538c50c785cSJohn Marino 		 &maintenancelist);
15395796c8dcSSimon Schubert 
1540c50c785cSJohn Marino   add_cmd ("first_component",
1541c50c785cSJohn Marino 	   class_maintenance,
1542c50c785cSJohn Marino 	   first_component_command,
15435796c8dcSSimon Schubert 	   _("Print the first class/namespace component of NAME."),
15445796c8dcSSimon Schubert 	   &maint_cplus_cmd_list);
1545*ef5ccd6cSJohn Marino 
1546*ef5ccd6cSJohn Marino   add_info ("vtbl", info_vtbl_command,
1547*ef5ccd6cSJohn Marino 	    _("Show the virtual function table for a C++ object.\n\
1548*ef5ccd6cSJohn Marino Usage: info vtbl EXPRESSION\n\
1549*ef5ccd6cSJohn Marino Evaluate EXPRESSION and display the virtual function table for the\n\
1550*ef5ccd6cSJohn Marino resulting object."));
15515796c8dcSSimon Schubert }
1552