xref: /dflybsd-src/contrib/gdb-7/gdb/cp-support.c (revision a45ae5f869d9cfcb3e41dbab486e10bfa9e336bf)
15796c8dcSSimon Schubert /* Helper routines for C++ support in GDB.
2*a45ae5f8SJohn Marino    Copyright (C) 2002-2005, 2007-2012 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"
375796c8dcSSimon Schubert 
385796c8dcSSimon Schubert #include "safe-ctype.h"
395796c8dcSSimon Schubert 
40cf7f2e2dSJohn Marino #include "psymtab.h"
41cf7f2e2dSJohn Marino 
425796c8dcSSimon Schubert #define d_left(dc) (dc)->u.s_binary.left
435796c8dcSSimon Schubert #define d_right(dc) (dc)->u.s_binary.right
445796c8dcSSimon Schubert 
455796c8dcSSimon Schubert /* Functions related to demangled name parsing.  */
465796c8dcSSimon Schubert 
475796c8dcSSimon Schubert static unsigned int cp_find_first_component_aux (const char *name,
485796c8dcSSimon Schubert 						 int permissive);
495796c8dcSSimon Schubert 
505796c8dcSSimon Schubert static void demangled_name_complaint (const char *name);
515796c8dcSSimon Schubert 
525796c8dcSSimon Schubert /* Functions/variables related to overload resolution.  */
535796c8dcSSimon Schubert 
54cf7f2e2dSJohn Marino static int sym_return_val_size = -1;
555796c8dcSSimon Schubert static int sym_return_val_index;
565796c8dcSSimon Schubert static struct symbol **sym_return_val;
575796c8dcSSimon Schubert 
585796c8dcSSimon Schubert static void overload_list_add_symbol (struct symbol *sym,
595796c8dcSSimon Schubert 				      const char *oload_name);
605796c8dcSSimon Schubert 
615796c8dcSSimon Schubert static void make_symbol_overload_list_using (const char *func_name,
625796c8dcSSimon Schubert 					     const char *namespace);
635796c8dcSSimon Schubert 
645796c8dcSSimon Schubert static void make_symbol_overload_list_qualified (const char *func_name);
655796c8dcSSimon Schubert 
665796c8dcSSimon Schubert /* The list of "maint cplus" commands.  */
675796c8dcSSimon Schubert 
685796c8dcSSimon Schubert struct cmd_list_element *maint_cplus_cmd_list = NULL;
695796c8dcSSimon Schubert 
705796c8dcSSimon Schubert /* The actual commands.  */
715796c8dcSSimon Schubert 
725796c8dcSSimon Schubert static void maint_cplus_command (char *arg, int from_tty);
735796c8dcSSimon Schubert static void first_component_command (char *arg, int from_tty);
745796c8dcSSimon Schubert 
75cf7f2e2dSJohn Marino /* Operator validation.
76c50c785cSJohn Marino    NOTE: Multi-byte operators (usually the assignment variety
77c50c785cSJohn Marino    operator) must appear before the single byte version, i.e., "+="
78c50c785cSJohn Marino    before "+".  */
79cf7f2e2dSJohn Marino static const char *operator_tokens[] =
80cf7f2e2dSJohn Marino   {
81c50c785cSJohn Marino     "++", "+=", "+", "->*", "->", "--", "-=", "-", "*=", "*",
82c50c785cSJohn Marino     "/=", "/", "%=", "%", "!=", "==", "!", "&&", "<<=", "<<",
83c50c785cSJohn Marino     ">>=", ">>", "<=", "<", ">=", ">", "~", "&=", "&", "|=",
84c50c785cSJohn Marino     "||", "|", "^=", "^", "=", "()", "[]", ",", "new", "delete"
85cf7f2e2dSJohn Marino     /* new[] and delete[] require special whitespace handling */
86cf7f2e2dSJohn Marino   };
87cf7f2e2dSJohn Marino 
88*a45ae5f8SJohn Marino /* A list of typedefs which should not be substituted by replace_typedefs.  */
89*a45ae5f8SJohn Marino static const char * const ignore_typedefs[] =
90*a45ae5f8SJohn Marino   {
91*a45ae5f8SJohn Marino     "std::istream", "std::iostream", "std::ostream", "std::string"
92*a45ae5f8SJohn Marino   };
93*a45ae5f8SJohn Marino 
94*a45ae5f8SJohn Marino static void
95*a45ae5f8SJohn Marino   replace_typedefs (struct demangle_parse_info *info,
96*a45ae5f8SJohn Marino 		    struct demangle_component *ret_comp);
97*a45ae5f8SJohn Marino 
98*a45ae5f8SJohn Marino /* A convenience function to copy STRING into OBSTACK, returning a pointer
99*a45ae5f8SJohn Marino    to the newly allocated string and saving the number of bytes saved in LEN.
100*a45ae5f8SJohn Marino 
101*a45ae5f8SJohn Marino    It does not copy the terminating '\0' byte!  */
102*a45ae5f8SJohn Marino 
103*a45ae5f8SJohn Marino static char *
104*a45ae5f8SJohn Marino copy_string_to_obstack (struct obstack *obstack, const char *string,
105*a45ae5f8SJohn Marino 			long *len)
106*a45ae5f8SJohn Marino {
107*a45ae5f8SJohn Marino   *len = strlen (string);
108*a45ae5f8SJohn Marino   return obstack_copy (obstack, string, *len);
109*a45ae5f8SJohn Marino }
110*a45ae5f8SJohn Marino 
111*a45ae5f8SJohn Marino /* A cleanup wrapper for cp_demangled_name_parse_free.  */
112*a45ae5f8SJohn Marino 
113*a45ae5f8SJohn Marino static void
114*a45ae5f8SJohn Marino do_demangled_name_parse_free_cleanup (void *data)
115*a45ae5f8SJohn Marino {
116*a45ae5f8SJohn Marino   struct demangle_parse_info *info = (struct demangle_parse_info *) data;
117*a45ae5f8SJohn Marino 
118*a45ae5f8SJohn Marino   cp_demangled_name_parse_free (info);
119*a45ae5f8SJohn Marino }
120*a45ae5f8SJohn Marino 
121*a45ae5f8SJohn Marino /* Create a cleanup for C++ name parsing.  */
122*a45ae5f8SJohn Marino 
123*a45ae5f8SJohn Marino struct cleanup *
124*a45ae5f8SJohn Marino make_cleanup_cp_demangled_name_parse_free (struct demangle_parse_info *info)
125*a45ae5f8SJohn Marino {
126*a45ae5f8SJohn Marino   return make_cleanup (do_demangled_name_parse_free_cleanup, info);
127*a45ae5f8SJohn Marino }
128*a45ae5f8SJohn Marino 
1295796c8dcSSimon Schubert /* Return 1 if STRING is clearly already in canonical form.  This
1305796c8dcSSimon Schubert    function is conservative; things which it does not recognize are
1315796c8dcSSimon Schubert    assumed to be non-canonical, and the parser will sort them out
1325796c8dcSSimon Schubert    afterwards.  This speeds up the critical path for alphanumeric
1335796c8dcSSimon Schubert    identifiers.  */
1345796c8dcSSimon Schubert 
1355796c8dcSSimon Schubert static int
1365796c8dcSSimon Schubert cp_already_canonical (const char *string)
1375796c8dcSSimon Schubert {
1385796c8dcSSimon Schubert   /* Identifier start character [a-zA-Z_].  */
1395796c8dcSSimon Schubert   if (!ISIDST (string[0]))
1405796c8dcSSimon Schubert     return 0;
1415796c8dcSSimon Schubert 
1425796c8dcSSimon Schubert   /* These are the only two identifiers which canonicalize to other
1435796c8dcSSimon Schubert      than themselves or an error: unsigned -> unsigned int and
1445796c8dcSSimon Schubert      signed -> int.  */
1455796c8dcSSimon Schubert   if (string[0] == 'u' && strcmp (&string[1], "nsigned") == 0)
1465796c8dcSSimon Schubert     return 0;
1475796c8dcSSimon Schubert   else if (string[0] == 's' && strcmp (&string[1], "igned") == 0)
1485796c8dcSSimon Schubert     return 0;
1495796c8dcSSimon Schubert 
1505796c8dcSSimon Schubert   /* Identifier character [a-zA-Z0-9_].  */
1515796c8dcSSimon Schubert   while (ISIDNUM (string[1]))
1525796c8dcSSimon Schubert     string++;
1535796c8dcSSimon Schubert 
1545796c8dcSSimon Schubert   if (string[1] == '\0')
1555796c8dcSSimon Schubert     return 1;
1565796c8dcSSimon Schubert   else
1575796c8dcSSimon Schubert     return 0;
1585796c8dcSSimon Schubert }
1595796c8dcSSimon Schubert 
160*a45ae5f8SJohn Marino /* Inspect the given RET_COMP for its type.  If it is a typedef,
161*a45ae5f8SJohn Marino    replace the node with the typedef's tree.
162*a45ae5f8SJohn Marino 
163*a45ae5f8SJohn Marino    Returns 1 if any typedef substitutions were made, 0 otherwise.  */
164*a45ae5f8SJohn Marino 
165*a45ae5f8SJohn Marino static int
166*a45ae5f8SJohn Marino inspect_type (struct demangle_parse_info *info,
167*a45ae5f8SJohn Marino 	      struct demangle_component *ret_comp)
168*a45ae5f8SJohn Marino {
169*a45ae5f8SJohn Marino   int i;
170*a45ae5f8SJohn Marino   char *name;
171*a45ae5f8SJohn Marino   struct symbol *sym;
172*a45ae5f8SJohn Marino   volatile struct gdb_exception except;
173*a45ae5f8SJohn Marino 
174*a45ae5f8SJohn Marino   /* Copy the symbol's name from RET_COMP and look it up
175*a45ae5f8SJohn Marino      in the symbol table.  */
176*a45ae5f8SJohn Marino   name = (char *) alloca (ret_comp->u.s_name.len + 1);
177*a45ae5f8SJohn Marino   memcpy (name, ret_comp->u.s_name.s, ret_comp->u.s_name.len);
178*a45ae5f8SJohn Marino   name[ret_comp->u.s_name.len] = '\0';
179*a45ae5f8SJohn Marino 
180*a45ae5f8SJohn Marino   /* Ignore any typedefs that should not be substituted.  */
181*a45ae5f8SJohn Marino   for (i = 0; i < ARRAY_SIZE (ignore_typedefs); ++i)
182*a45ae5f8SJohn Marino     {
183*a45ae5f8SJohn Marino       if (strcmp (name, ignore_typedefs[i]) == 0)
184*a45ae5f8SJohn Marino 	return 0;
185*a45ae5f8SJohn Marino     }
186*a45ae5f8SJohn Marino 
187*a45ae5f8SJohn Marino   sym = NULL;
188*a45ae5f8SJohn Marino   TRY_CATCH (except, RETURN_MASK_ALL)
189*a45ae5f8SJohn Marino   {
190*a45ae5f8SJohn Marino     sym = lookup_symbol (name, 0, VAR_DOMAIN, 0);
191*a45ae5f8SJohn Marino   }
192*a45ae5f8SJohn Marino 
193*a45ae5f8SJohn Marino   if (except.reason >= 0 && sym != NULL)
194*a45ae5f8SJohn Marino     {
195*a45ae5f8SJohn Marino       struct type *otype = SYMBOL_TYPE (sym);
196*a45ae5f8SJohn Marino 
197*a45ae5f8SJohn Marino       /* If the type is a typedef, replace it.  */
198*a45ae5f8SJohn Marino       if (TYPE_CODE (otype) == TYPE_CODE_TYPEDEF)
199*a45ae5f8SJohn Marino 	{
200*a45ae5f8SJohn Marino 	  long len;
201*a45ae5f8SJohn Marino 	  int is_anon;
202*a45ae5f8SJohn Marino 	  struct type *type;
203*a45ae5f8SJohn Marino 	  struct demangle_parse_info *i;
204*a45ae5f8SJohn Marino 	  struct ui_file *buf;
205*a45ae5f8SJohn Marino 
206*a45ae5f8SJohn Marino 	  /* Get the real type of the typedef.  */
207*a45ae5f8SJohn Marino 	  type = check_typedef (otype);
208*a45ae5f8SJohn Marino 
209*a45ae5f8SJohn Marino 	  is_anon = (TYPE_TAG_NAME (type) == NULL
210*a45ae5f8SJohn Marino 		     && (TYPE_CODE (type) == TYPE_CODE_ENUM
211*a45ae5f8SJohn Marino 			 || TYPE_CODE (type) == TYPE_CODE_STRUCT
212*a45ae5f8SJohn Marino 			 || TYPE_CODE (type) == TYPE_CODE_UNION));
213*a45ae5f8SJohn Marino 	  if (is_anon)
214*a45ae5f8SJohn Marino 	    {
215*a45ae5f8SJohn Marino 	      struct type *last = otype;
216*a45ae5f8SJohn Marino 
217*a45ae5f8SJohn Marino 	      /* Find the last typedef for the type.  */
218*a45ae5f8SJohn Marino 	      while (TYPE_TARGET_TYPE (last) != NULL
219*a45ae5f8SJohn Marino 		     && (TYPE_CODE (TYPE_TARGET_TYPE (last))
220*a45ae5f8SJohn Marino 			 == TYPE_CODE_TYPEDEF))
221*a45ae5f8SJohn Marino 		last = TYPE_TARGET_TYPE (last);
222*a45ae5f8SJohn Marino 
223*a45ae5f8SJohn Marino 	      /* If there is only one typedef for this anonymous type,
224*a45ae5f8SJohn Marino 		 do not substitute it.  */
225*a45ae5f8SJohn Marino 	      if (type == otype)
226*a45ae5f8SJohn Marino 		return 0;
227*a45ae5f8SJohn Marino 	      else
228*a45ae5f8SJohn Marino 		/* Use the last typedef seen as the type for this
229*a45ae5f8SJohn Marino 		   anonymous type.  */
230*a45ae5f8SJohn Marino 		type = last;
231*a45ae5f8SJohn Marino 	    }
232*a45ae5f8SJohn Marino 
233*a45ae5f8SJohn Marino 	  buf = mem_fileopen ();
234*a45ae5f8SJohn Marino 	  TRY_CATCH (except, RETURN_MASK_ERROR)
235*a45ae5f8SJohn Marino 	  {
236*a45ae5f8SJohn Marino 	    type_print (type, "", buf, -1);
237*a45ae5f8SJohn Marino 	  }
238*a45ae5f8SJohn Marino 
239*a45ae5f8SJohn Marino 	  /* If type_print threw an exception, there is little point
240*a45ae5f8SJohn Marino 	     in continuing, so just bow out gracefully.  */
241*a45ae5f8SJohn Marino 	  if (except.reason < 0)
242*a45ae5f8SJohn Marino 	    {
243*a45ae5f8SJohn Marino 	      ui_file_delete (buf);
244*a45ae5f8SJohn Marino 	      return 0;
245*a45ae5f8SJohn Marino 	    }
246*a45ae5f8SJohn Marino 
247*a45ae5f8SJohn Marino 	  name = ui_file_obsavestring (buf, &info->obstack, &len);
248*a45ae5f8SJohn Marino 	  ui_file_delete (buf);
249*a45ae5f8SJohn Marino 
250*a45ae5f8SJohn Marino 	  /* Turn the result into a new tree.  Note that this
251*a45ae5f8SJohn Marino 	     tree will contain pointers into NAME, so NAME cannot
252*a45ae5f8SJohn Marino 	     be free'd until all typedef conversion is done and
253*a45ae5f8SJohn Marino 	     the final result is converted into a string.  */
254*a45ae5f8SJohn Marino 	  i = cp_demangled_name_to_comp (name, NULL);
255*a45ae5f8SJohn Marino 	  if (i != NULL)
256*a45ae5f8SJohn Marino 	    {
257*a45ae5f8SJohn Marino 	      /* Merge the two trees.  */
258*a45ae5f8SJohn Marino 	      cp_merge_demangle_parse_infos (info, ret_comp, i);
259*a45ae5f8SJohn Marino 
260*a45ae5f8SJohn Marino 	      /* Replace any newly introduced typedefs -- but not
261*a45ae5f8SJohn Marino 		 if the type is anonymous (that would lead to infinite
262*a45ae5f8SJohn Marino 		 looping).  */
263*a45ae5f8SJohn Marino 	      if (!is_anon)
264*a45ae5f8SJohn Marino 		replace_typedefs (info, ret_comp);
265*a45ae5f8SJohn Marino 	    }
266*a45ae5f8SJohn Marino 	  else
267*a45ae5f8SJohn Marino 	    {
268*a45ae5f8SJohn Marino 	      /* This shouldn't happen unless the type printer has
269*a45ae5f8SJohn Marino 		 output something that the name parser cannot grok.
270*a45ae5f8SJohn Marino 		 Nonetheless, an ounce of prevention...
271*a45ae5f8SJohn Marino 
272*a45ae5f8SJohn Marino 		 Canonicalize the name again, and store it in the
273*a45ae5f8SJohn Marino 		 current node (RET_COMP).  */
274*a45ae5f8SJohn Marino 	      char *canon = cp_canonicalize_string_no_typedefs (name);
275*a45ae5f8SJohn Marino 
276*a45ae5f8SJohn Marino 	      if (canon != NULL)
277*a45ae5f8SJohn Marino 		{
278*a45ae5f8SJohn Marino 		  /* Copy the canonicalization into the obstack and
279*a45ae5f8SJohn Marino 		     free CANON.  */
280*a45ae5f8SJohn Marino 		  name = copy_string_to_obstack (&info->obstack, canon, &len);
281*a45ae5f8SJohn Marino 		  xfree (canon);
282*a45ae5f8SJohn Marino 		}
283*a45ae5f8SJohn Marino 
284*a45ae5f8SJohn Marino 	      ret_comp->u.s_name.s = name;
285*a45ae5f8SJohn Marino 	      ret_comp->u.s_name.len = len;
286*a45ae5f8SJohn Marino 	    }
287*a45ae5f8SJohn Marino 
288*a45ae5f8SJohn Marino 	  return 1;
289*a45ae5f8SJohn Marino 	}
290*a45ae5f8SJohn Marino     }
291*a45ae5f8SJohn Marino 
292*a45ae5f8SJohn Marino   return 0;
293*a45ae5f8SJohn Marino }
294*a45ae5f8SJohn Marino 
295*a45ae5f8SJohn Marino /* Replace any typedefs appearing in the qualified name
296*a45ae5f8SJohn Marino    (DEMANGLE_COMPONENT_QUAL_NAME) represented in RET_COMP for the name parse
297*a45ae5f8SJohn Marino    given in INFO.  */
298*a45ae5f8SJohn Marino 
299*a45ae5f8SJohn Marino static void
300*a45ae5f8SJohn Marino replace_typedefs_qualified_name (struct demangle_parse_info *info,
301*a45ae5f8SJohn Marino 				 struct demangle_component *ret_comp)
302*a45ae5f8SJohn Marino {
303*a45ae5f8SJohn Marino   long len;
304*a45ae5f8SJohn Marino   char *name;
305*a45ae5f8SJohn Marino   struct ui_file *buf = mem_fileopen ();
306*a45ae5f8SJohn Marino   struct demangle_component *comp = ret_comp;
307*a45ae5f8SJohn Marino 
308*a45ae5f8SJohn Marino   /* Walk each node of the qualified name, reconstructing the name of
309*a45ae5f8SJohn Marino      this element.  With every node, check for any typedef substitutions.
310*a45ae5f8SJohn Marino      If a substitution has occurred, replace the qualified name node
311*a45ae5f8SJohn Marino      with a DEMANGLE_COMPONENT_NAME node representing the new, typedef-
312*a45ae5f8SJohn Marino      substituted name.  */
313*a45ae5f8SJohn Marino   while (comp->type == DEMANGLE_COMPONENT_QUAL_NAME)
314*a45ae5f8SJohn Marino     {
315*a45ae5f8SJohn Marino       if (d_left (comp)->type == DEMANGLE_COMPONENT_NAME)
316*a45ae5f8SJohn Marino 	{
317*a45ae5f8SJohn Marino 	  struct demangle_component new;
318*a45ae5f8SJohn Marino 
319*a45ae5f8SJohn Marino 	  ui_file_write (buf, d_left (comp)->u.s_name.s,
320*a45ae5f8SJohn Marino 			 d_left (comp)->u.s_name.len);
321*a45ae5f8SJohn Marino 	  name = ui_file_obsavestring (buf, &info->obstack, &len);
322*a45ae5f8SJohn Marino 	  new.type = DEMANGLE_COMPONENT_NAME;
323*a45ae5f8SJohn Marino 	  new.u.s_name.s = name;
324*a45ae5f8SJohn Marino 	  new.u.s_name.len = len;
325*a45ae5f8SJohn Marino 	  if (inspect_type (info, &new))
326*a45ae5f8SJohn Marino 	    {
327*a45ae5f8SJohn Marino 	      char *n, *s;
328*a45ae5f8SJohn Marino 	      long slen;
329*a45ae5f8SJohn Marino 
330*a45ae5f8SJohn Marino 	      /* A typedef was substituted in NEW.  Convert it to a
331*a45ae5f8SJohn Marino 		 string and replace the top DEMANGLE_COMPONENT_QUAL_NAME
332*a45ae5f8SJohn Marino 		 node.  */
333*a45ae5f8SJohn Marino 
334*a45ae5f8SJohn Marino 	      ui_file_rewind (buf);
335*a45ae5f8SJohn Marino 	      n = cp_comp_to_string (&new, 100);
336*a45ae5f8SJohn Marino 	      if (n == NULL)
337*a45ae5f8SJohn Marino 		{
338*a45ae5f8SJohn Marino 		  /* If something went astray, abort typedef substitutions.  */
339*a45ae5f8SJohn Marino 		  ui_file_delete (buf);
340*a45ae5f8SJohn Marino 		  return;
341*a45ae5f8SJohn Marino 		}
342*a45ae5f8SJohn Marino 
343*a45ae5f8SJohn Marino 	      s = copy_string_to_obstack (&info->obstack, n, &slen);
344*a45ae5f8SJohn Marino 	      xfree (n);
345*a45ae5f8SJohn Marino 
346*a45ae5f8SJohn Marino 	      d_left (ret_comp)->type = DEMANGLE_COMPONENT_NAME;
347*a45ae5f8SJohn Marino 	      d_left (ret_comp)->u.s_name.s = s;
348*a45ae5f8SJohn Marino 	      d_left (ret_comp)->u.s_name.len = slen;
349*a45ae5f8SJohn Marino 	      d_right (ret_comp) = d_right (comp);
350*a45ae5f8SJohn Marino 	      comp = ret_comp;
351*a45ae5f8SJohn Marino 	      continue;
352*a45ae5f8SJohn Marino 	    }
353*a45ae5f8SJohn Marino 	}
354*a45ae5f8SJohn Marino       else
355*a45ae5f8SJohn Marino 	{
356*a45ae5f8SJohn Marino 	  /* The current node is not a name, so simply replace any
357*a45ae5f8SJohn Marino 	     typedefs in it.  Then print it to the stream to continue
358*a45ae5f8SJohn Marino 	     checking for more typedefs in the tree.  */
359*a45ae5f8SJohn Marino 	  replace_typedefs (info, d_left (comp));
360*a45ae5f8SJohn Marino 	  name = cp_comp_to_string (d_left (comp), 100);
361*a45ae5f8SJohn Marino 	  if (name == NULL)
362*a45ae5f8SJohn Marino 	    {
363*a45ae5f8SJohn Marino 	      /* If something went astray, abort typedef substitutions.  */
364*a45ae5f8SJohn Marino 	      ui_file_delete (buf);
365*a45ae5f8SJohn Marino 	      return;
366*a45ae5f8SJohn Marino 	    }
367*a45ae5f8SJohn Marino 	  fputs_unfiltered (name, buf);
368*a45ae5f8SJohn Marino 	  xfree (name);
369*a45ae5f8SJohn Marino 	}
370*a45ae5f8SJohn Marino       ui_file_write (buf, "::", 2);
371*a45ae5f8SJohn Marino       comp = d_right (comp);
372*a45ae5f8SJohn Marino     }
373*a45ae5f8SJohn Marino 
374*a45ae5f8SJohn Marino   /* If the next component is DEMANGLE_COMPONENT_NAME, save the qualified
375*a45ae5f8SJohn Marino      name assembled above and append the name given by COMP.  Then use this
376*a45ae5f8SJohn Marino      reassembled name to check for a typedef.  */
377*a45ae5f8SJohn Marino 
378*a45ae5f8SJohn Marino   if (comp->type == DEMANGLE_COMPONENT_NAME)
379*a45ae5f8SJohn Marino     {
380*a45ae5f8SJohn Marino       ui_file_write (buf, comp->u.s_name.s, comp->u.s_name.len);
381*a45ae5f8SJohn Marino       name = ui_file_obsavestring (buf, &info->obstack, &len);
382*a45ae5f8SJohn Marino 
383*a45ae5f8SJohn Marino       /* Replace the top (DEMANGLE_COMPONENT_QUAL_NAME) node
384*a45ae5f8SJohn Marino 	 with a DEMANGLE_COMPONENT_NAME node containing the whole
385*a45ae5f8SJohn Marino 	 name.  */
386*a45ae5f8SJohn Marino       ret_comp->type = DEMANGLE_COMPONENT_NAME;
387*a45ae5f8SJohn Marino       ret_comp->u.s_name.s = name;
388*a45ae5f8SJohn Marino       ret_comp->u.s_name.len = len;
389*a45ae5f8SJohn Marino       inspect_type (info, ret_comp);
390*a45ae5f8SJohn Marino     }
391*a45ae5f8SJohn Marino   else
392*a45ae5f8SJohn Marino     replace_typedefs (info, comp);
393*a45ae5f8SJohn Marino 
394*a45ae5f8SJohn Marino   ui_file_delete (buf);
395*a45ae5f8SJohn Marino }
396*a45ae5f8SJohn Marino 
397*a45ae5f8SJohn Marino 
398*a45ae5f8SJohn Marino /* A function to check const and volatile qualifiers for argument types.
399*a45ae5f8SJohn Marino 
400*a45ae5f8SJohn Marino    "Parameter declarations that differ only in the presence
401*a45ae5f8SJohn Marino    or absence of `const' and/or `volatile' are equivalent."
402*a45ae5f8SJohn Marino    C++ Standard N3290, clause 13.1.3 #4.  */
403*a45ae5f8SJohn Marino 
404*a45ae5f8SJohn Marino static void
405*a45ae5f8SJohn Marino check_cv_qualifiers (struct demangle_component *ret_comp)
406*a45ae5f8SJohn Marino {
407*a45ae5f8SJohn Marino   while (d_left (ret_comp) != NULL
408*a45ae5f8SJohn Marino 	 && (d_left (ret_comp)->type == DEMANGLE_COMPONENT_CONST
409*a45ae5f8SJohn Marino 	     || d_left (ret_comp)->type == DEMANGLE_COMPONENT_VOLATILE))
410*a45ae5f8SJohn Marino     {
411*a45ae5f8SJohn Marino       d_left (ret_comp) = d_left (d_left (ret_comp));
412*a45ae5f8SJohn Marino     }
413*a45ae5f8SJohn Marino }
414*a45ae5f8SJohn Marino 
415*a45ae5f8SJohn Marino /* Walk the parse tree given by RET_COMP, replacing any typedefs with
416*a45ae5f8SJohn Marino    their basic types.  */
417*a45ae5f8SJohn Marino 
418*a45ae5f8SJohn Marino static void
419*a45ae5f8SJohn Marino replace_typedefs (struct demangle_parse_info *info,
420*a45ae5f8SJohn Marino 		  struct demangle_component *ret_comp)
421*a45ae5f8SJohn Marino {
422*a45ae5f8SJohn Marino   if (ret_comp)
423*a45ae5f8SJohn Marino     {
424*a45ae5f8SJohn Marino       switch (ret_comp->type)
425*a45ae5f8SJohn Marino 	{
426*a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_ARGLIST:
427*a45ae5f8SJohn Marino 	  check_cv_qualifiers (ret_comp);
428*a45ae5f8SJohn Marino 	  /* Fall through */
429*a45ae5f8SJohn Marino 
430*a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_FUNCTION_TYPE:
431*a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_TEMPLATE:
432*a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
433*a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_TYPED_NAME:
434*a45ae5f8SJohn Marino 	  replace_typedefs (info, d_left (ret_comp));
435*a45ae5f8SJohn Marino 	  replace_typedefs (info, d_right (ret_comp));
436*a45ae5f8SJohn Marino 	  break;
437*a45ae5f8SJohn Marino 
438*a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_NAME:
439*a45ae5f8SJohn Marino 	  inspect_type (info, ret_comp);
440*a45ae5f8SJohn Marino 	  break;
441*a45ae5f8SJohn Marino 
442*a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_QUAL_NAME:
443*a45ae5f8SJohn Marino 	  replace_typedefs_qualified_name (info, ret_comp);
444*a45ae5f8SJohn Marino 	  break;
445*a45ae5f8SJohn Marino 
446*a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_LOCAL_NAME:
447*a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_CTOR:
448*a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_ARRAY_TYPE:
449*a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_PTRMEM_TYPE:
450*a45ae5f8SJohn Marino 	  replace_typedefs (info, d_right (ret_comp));
451*a45ae5f8SJohn Marino 	  break;
452*a45ae5f8SJohn Marino 
453*a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_CONST:
454*a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_RESTRICT:
455*a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_VOLATILE:
456*a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_VOLATILE_THIS:
457*a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_CONST_THIS:
458*a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_RESTRICT_THIS:
459*a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_POINTER:
460*a45ae5f8SJohn Marino 	case DEMANGLE_COMPONENT_REFERENCE:
461*a45ae5f8SJohn Marino 	  replace_typedefs (info, d_left (ret_comp));
462*a45ae5f8SJohn Marino 	  break;
463*a45ae5f8SJohn Marino 
464*a45ae5f8SJohn Marino 	default:
465*a45ae5f8SJohn Marino 	  break;
466*a45ae5f8SJohn Marino 	}
467*a45ae5f8SJohn Marino     }
468*a45ae5f8SJohn Marino }
469*a45ae5f8SJohn Marino 
470*a45ae5f8SJohn Marino /* Parse STRING and convert it to canonical form, resolving any typedefs.
471*a45ae5f8SJohn Marino    If parsing fails, or if STRING is already canonical, return NULL.
472*a45ae5f8SJohn Marino    Otherwise return the canonical form.  The return value is allocated via
473*a45ae5f8SJohn Marino    xmalloc.  */
474*a45ae5f8SJohn Marino 
475*a45ae5f8SJohn Marino char *
476*a45ae5f8SJohn Marino cp_canonicalize_string_no_typedefs (const char *string)
477*a45ae5f8SJohn Marino {
478*a45ae5f8SJohn Marino   char *ret;
479*a45ae5f8SJohn Marino   unsigned int estimated_len;
480*a45ae5f8SJohn Marino   struct demangle_parse_info *info;
481*a45ae5f8SJohn Marino 
482*a45ae5f8SJohn Marino   ret = NULL;
483*a45ae5f8SJohn Marino   estimated_len = strlen (string) * 2;
484*a45ae5f8SJohn Marino   info = cp_demangled_name_to_comp (string, NULL);
485*a45ae5f8SJohn Marino   if (info != NULL)
486*a45ae5f8SJohn Marino     {
487*a45ae5f8SJohn Marino       /* Replace all the typedefs in the tree.  */
488*a45ae5f8SJohn Marino       replace_typedefs (info, info->tree);
489*a45ae5f8SJohn Marino 
490*a45ae5f8SJohn Marino       /* Convert the tree back into a string.  */
491*a45ae5f8SJohn Marino       ret = cp_comp_to_string (info->tree, estimated_len);
492*a45ae5f8SJohn Marino       gdb_assert (ret != NULL);
493*a45ae5f8SJohn Marino 
494*a45ae5f8SJohn Marino       /* Free the parse information.  */
495*a45ae5f8SJohn Marino       cp_demangled_name_parse_free (info);
496*a45ae5f8SJohn Marino 
497*a45ae5f8SJohn Marino       /* Finally, compare the original string with the computed
498*a45ae5f8SJohn Marino 	 name, returning NULL if they are the same.  */
499*a45ae5f8SJohn Marino       if (strcmp (string, ret) == 0)
500*a45ae5f8SJohn Marino 	{
501*a45ae5f8SJohn Marino 	  xfree (ret);
502*a45ae5f8SJohn Marino 	  return NULL;
503*a45ae5f8SJohn Marino 	}
504*a45ae5f8SJohn Marino     }
505*a45ae5f8SJohn Marino 
506*a45ae5f8SJohn Marino   return ret;
507*a45ae5f8SJohn Marino }
508*a45ae5f8SJohn Marino 
5095796c8dcSSimon Schubert /* Parse STRING and convert it to canonical form.  If parsing fails,
5105796c8dcSSimon Schubert    or if STRING is already canonical, return NULL.  Otherwise return
5115796c8dcSSimon Schubert    the canonical form.  The return value is allocated via xmalloc.  */
5125796c8dcSSimon Schubert 
5135796c8dcSSimon Schubert char *
5145796c8dcSSimon Schubert cp_canonicalize_string (const char *string)
5155796c8dcSSimon Schubert {
516*a45ae5f8SJohn Marino   struct demangle_parse_info *info;
5175796c8dcSSimon Schubert   unsigned int estimated_len;
5185796c8dcSSimon Schubert   char *ret;
5195796c8dcSSimon Schubert 
5205796c8dcSSimon Schubert   if (cp_already_canonical (string))
5215796c8dcSSimon Schubert     return NULL;
5225796c8dcSSimon Schubert 
523*a45ae5f8SJohn Marino   info = cp_demangled_name_to_comp (string, NULL);
524*a45ae5f8SJohn Marino   if (info == NULL)
5255796c8dcSSimon Schubert     return NULL;
5265796c8dcSSimon Schubert 
5275796c8dcSSimon Schubert   estimated_len = strlen (string) * 2;
528*a45ae5f8SJohn Marino   ret = cp_comp_to_string (info->tree, estimated_len);
529*a45ae5f8SJohn Marino   cp_demangled_name_parse_free (info);
5305796c8dcSSimon Schubert 
5315796c8dcSSimon Schubert   if (strcmp (string, ret) == 0)
5325796c8dcSSimon Schubert     {
5335796c8dcSSimon Schubert       xfree (ret);
5345796c8dcSSimon Schubert       return NULL;
5355796c8dcSSimon Schubert     }
5365796c8dcSSimon Schubert 
5375796c8dcSSimon Schubert   return ret;
5385796c8dcSSimon Schubert }
5395796c8dcSSimon Schubert 
540c50c785cSJohn Marino /* Convert a mangled name to a demangle_component tree.  *MEMORY is
541c50c785cSJohn Marino    set to the block of used memory that should be freed when finished
542c50c785cSJohn Marino    with the tree.  DEMANGLED_P is set to the char * that should be
543c50c785cSJohn Marino    freed when finished with the tree, or NULL if none was needed.
544c50c785cSJohn Marino    OPTIONS will be passed to the demangler.  */
5455796c8dcSSimon Schubert 
546*a45ae5f8SJohn Marino static struct demangle_parse_info *
5475796c8dcSSimon Schubert mangled_name_to_comp (const char *mangled_name, int options,
5485796c8dcSSimon Schubert 		      void **memory, char **demangled_p)
5495796c8dcSSimon Schubert {
5505796c8dcSSimon Schubert   char *demangled_name;
551*a45ae5f8SJohn Marino   struct demangle_parse_info *info;
5525796c8dcSSimon Schubert 
5535796c8dcSSimon Schubert   /* If it looks like a v3 mangled name, then try to go directly
5545796c8dcSSimon Schubert      to trees.  */
5555796c8dcSSimon Schubert   if (mangled_name[0] == '_' && mangled_name[1] == 'Z')
5565796c8dcSSimon Schubert     {
557*a45ae5f8SJohn Marino       struct demangle_component *ret;
558*a45ae5f8SJohn Marino 
559c50c785cSJohn Marino       ret = cplus_demangle_v3_components (mangled_name,
560c50c785cSJohn Marino 					  options, memory);
5615796c8dcSSimon Schubert       if (ret)
5625796c8dcSSimon Schubert 	{
563*a45ae5f8SJohn Marino 	  info = cp_new_demangle_parse_info ();
564*a45ae5f8SJohn Marino 	  info->tree = ret;
5655796c8dcSSimon Schubert 	  *demangled_p = NULL;
566*a45ae5f8SJohn Marino 	  return info;
5675796c8dcSSimon Schubert 	}
5685796c8dcSSimon Schubert     }
5695796c8dcSSimon Schubert 
570c50c785cSJohn Marino   /* If it doesn't, or if that failed, then try to demangle the
571c50c785cSJohn Marino      name.  */
5725796c8dcSSimon Schubert   demangled_name = cplus_demangle (mangled_name, options);
5735796c8dcSSimon Schubert   if (demangled_name == NULL)
5745796c8dcSSimon Schubert    return NULL;
5755796c8dcSSimon Schubert 
576c50c785cSJohn Marino   /* If we could demangle the name, parse it to build the component
577c50c785cSJohn Marino      tree.  */
578*a45ae5f8SJohn Marino   info = cp_demangled_name_to_comp (demangled_name, NULL);
5795796c8dcSSimon Schubert 
580*a45ae5f8SJohn Marino   if (info == NULL)
5815796c8dcSSimon Schubert     {
5825796c8dcSSimon Schubert       xfree (demangled_name);
5835796c8dcSSimon Schubert       return NULL;
5845796c8dcSSimon Schubert     }
5855796c8dcSSimon Schubert 
5865796c8dcSSimon Schubert   *demangled_p = demangled_name;
587*a45ae5f8SJohn Marino   return info;
5885796c8dcSSimon Schubert }
5895796c8dcSSimon Schubert 
5905796c8dcSSimon Schubert /* Return the name of the class containing method PHYSNAME.  */
5915796c8dcSSimon Schubert 
5925796c8dcSSimon Schubert char *
5935796c8dcSSimon Schubert cp_class_name_from_physname (const char *physname)
5945796c8dcSSimon Schubert {
5955796c8dcSSimon Schubert   void *storage = NULL;
5965796c8dcSSimon Schubert   char *demangled_name = NULL, *ret;
5975796c8dcSSimon Schubert   struct demangle_component *ret_comp, *prev_comp, *cur_comp;
598*a45ae5f8SJohn Marino   struct demangle_parse_info *info;
5995796c8dcSSimon Schubert   int done;
6005796c8dcSSimon Schubert 
601*a45ae5f8SJohn Marino   info = mangled_name_to_comp (physname, DMGL_ANSI,
602c50c785cSJohn Marino 			       &storage, &demangled_name);
603*a45ae5f8SJohn Marino   if (info == NULL)
6045796c8dcSSimon Schubert     return NULL;
6055796c8dcSSimon Schubert 
6065796c8dcSSimon Schubert   done = 0;
607*a45ae5f8SJohn Marino   ret_comp = info->tree;
6085796c8dcSSimon Schubert 
609c50c785cSJohn Marino   /* First strip off any qualifiers, if we have a function or
610c50c785cSJohn Marino      method.  */
6115796c8dcSSimon Schubert   while (!done)
6125796c8dcSSimon Schubert     switch (ret_comp->type)
6135796c8dcSSimon Schubert       {
6145796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_CONST:
6155796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_RESTRICT:
6165796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_VOLATILE:
6175796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_CONST_THIS:
6185796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_RESTRICT_THIS:
6195796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_VOLATILE_THIS:
6205796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
6215796c8dcSSimon Schubert         ret_comp = d_left (ret_comp);
6225796c8dcSSimon Schubert         break;
6235796c8dcSSimon Schubert       default:
6245796c8dcSSimon Schubert 	done = 1;
6255796c8dcSSimon Schubert 	break;
6265796c8dcSSimon Schubert       }
6275796c8dcSSimon Schubert 
6285796c8dcSSimon Schubert   /* If what we have now is a function, discard the argument list.  */
6295796c8dcSSimon Schubert   if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
6305796c8dcSSimon Schubert     ret_comp = d_left (ret_comp);
6315796c8dcSSimon Schubert 
6325796c8dcSSimon Schubert   /* If what we have now is a template, strip off the template
6335796c8dcSSimon Schubert      arguments.  The left subtree may be a qualified name.  */
6345796c8dcSSimon Schubert   if (ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE)
6355796c8dcSSimon Schubert     ret_comp = d_left (ret_comp);
6365796c8dcSSimon Schubert 
637c50c785cSJohn Marino   /* What we have now should be a name, possibly qualified.
638c50c785cSJohn Marino      Additional qualifiers could live in the left subtree or the right
639c50c785cSJohn Marino      subtree.  Find the last piece.  */
6405796c8dcSSimon Schubert   done = 0;
6415796c8dcSSimon Schubert   prev_comp = NULL;
6425796c8dcSSimon Schubert   cur_comp = ret_comp;
6435796c8dcSSimon Schubert   while (!done)
6445796c8dcSSimon Schubert     switch (cur_comp->type)
6455796c8dcSSimon Schubert       {
6465796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_QUAL_NAME:
6475796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_LOCAL_NAME:
6485796c8dcSSimon Schubert 	prev_comp = cur_comp;
6495796c8dcSSimon Schubert         cur_comp = d_right (cur_comp);
6505796c8dcSSimon Schubert         break;
6515796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_TEMPLATE:
6525796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_NAME:
6535796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_CTOR:
6545796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_DTOR:
6555796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_OPERATOR:
6565796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
6575796c8dcSSimon Schubert 	done = 1;
6585796c8dcSSimon Schubert 	break;
6595796c8dcSSimon Schubert       default:
6605796c8dcSSimon Schubert 	done = 1;
6615796c8dcSSimon Schubert 	cur_comp = NULL;
6625796c8dcSSimon Schubert 	break;
6635796c8dcSSimon Schubert       }
6645796c8dcSSimon Schubert 
6655796c8dcSSimon Schubert   ret = NULL;
6665796c8dcSSimon Schubert   if (cur_comp != NULL && prev_comp != NULL)
6675796c8dcSSimon Schubert     {
6685796c8dcSSimon Schubert       /* We want to discard the rightmost child of PREV_COMP.  */
6695796c8dcSSimon Schubert       *prev_comp = *d_left (prev_comp);
670c50c785cSJohn Marino       /* The ten is completely arbitrary; we don't have a good
671c50c785cSJohn Marino 	 estimate.  */
6725796c8dcSSimon Schubert       ret = cp_comp_to_string (ret_comp, 10);
6735796c8dcSSimon Schubert     }
6745796c8dcSSimon Schubert 
6755796c8dcSSimon Schubert   xfree (storage);
6765796c8dcSSimon Schubert   xfree (demangled_name);
677*a45ae5f8SJohn Marino   cp_demangled_name_parse_free (info);
6785796c8dcSSimon Schubert   return ret;
6795796c8dcSSimon Schubert }
6805796c8dcSSimon Schubert 
681c50c785cSJohn Marino /* Return the child of COMP which is the basename of a method,
682c50c785cSJohn Marino    variable, et cetera.  All scope qualifiers are discarded, but
683c50c785cSJohn Marino    template arguments will be included.  The component tree may be
684c50c785cSJohn Marino    modified.  */
6855796c8dcSSimon Schubert 
6865796c8dcSSimon Schubert static struct demangle_component *
6875796c8dcSSimon Schubert unqualified_name_from_comp (struct demangle_component *comp)
6885796c8dcSSimon Schubert {
6895796c8dcSSimon Schubert   struct demangle_component *ret_comp = comp, *last_template;
6905796c8dcSSimon Schubert   int done;
6915796c8dcSSimon Schubert 
6925796c8dcSSimon Schubert   done = 0;
6935796c8dcSSimon Schubert   last_template = NULL;
6945796c8dcSSimon Schubert   while (!done)
6955796c8dcSSimon Schubert     switch (ret_comp->type)
6965796c8dcSSimon Schubert       {
6975796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_QUAL_NAME:
6985796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_LOCAL_NAME:
6995796c8dcSSimon Schubert         ret_comp = d_right (ret_comp);
7005796c8dcSSimon Schubert         break;
7015796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_TYPED_NAME:
7025796c8dcSSimon Schubert         ret_comp = d_left (ret_comp);
7035796c8dcSSimon Schubert         break;
7045796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_TEMPLATE:
7055796c8dcSSimon Schubert 	gdb_assert (last_template == NULL);
7065796c8dcSSimon Schubert 	last_template = ret_comp;
7075796c8dcSSimon Schubert 	ret_comp = d_left (ret_comp);
7085796c8dcSSimon Schubert 	break;
7095796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_CONST:
7105796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_RESTRICT:
7115796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_VOLATILE:
7125796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_CONST_THIS:
7135796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_RESTRICT_THIS:
7145796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_VOLATILE_THIS:
7155796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
7165796c8dcSSimon Schubert         ret_comp = d_left (ret_comp);
7175796c8dcSSimon Schubert         break;
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 	return NULL;
7275796c8dcSSimon Schubert 	break;
7285796c8dcSSimon Schubert       }
7295796c8dcSSimon Schubert 
7305796c8dcSSimon Schubert   if (last_template)
7315796c8dcSSimon Schubert     {
7325796c8dcSSimon Schubert       d_left (last_template) = ret_comp;
7335796c8dcSSimon Schubert       return last_template;
7345796c8dcSSimon Schubert     }
7355796c8dcSSimon Schubert 
7365796c8dcSSimon Schubert   return ret_comp;
7375796c8dcSSimon Schubert }
7385796c8dcSSimon Schubert 
7395796c8dcSSimon Schubert /* Return the name of the method whose linkage name is PHYSNAME.  */
7405796c8dcSSimon Schubert 
7415796c8dcSSimon Schubert char *
7425796c8dcSSimon Schubert method_name_from_physname (const char *physname)
7435796c8dcSSimon Schubert {
7445796c8dcSSimon Schubert   void *storage = NULL;
7455796c8dcSSimon Schubert   char *demangled_name = NULL, *ret;
7465796c8dcSSimon Schubert   struct demangle_component *ret_comp;
747*a45ae5f8SJohn Marino   struct demangle_parse_info *info;
7485796c8dcSSimon Schubert 
749*a45ae5f8SJohn Marino   info = mangled_name_to_comp (physname, DMGL_ANSI,
750c50c785cSJohn Marino 			       &storage, &demangled_name);
751*a45ae5f8SJohn Marino   if (info == NULL)
7525796c8dcSSimon Schubert     return NULL;
7535796c8dcSSimon Schubert 
754*a45ae5f8SJohn Marino   ret_comp = unqualified_name_from_comp (info->tree);
7555796c8dcSSimon Schubert 
7565796c8dcSSimon Schubert   ret = NULL;
7575796c8dcSSimon Schubert   if (ret_comp != NULL)
758c50c785cSJohn Marino     /* The ten is completely arbitrary; we don't have a good
759c50c785cSJohn Marino        estimate.  */
7605796c8dcSSimon Schubert     ret = cp_comp_to_string (ret_comp, 10);
7615796c8dcSSimon Schubert 
7625796c8dcSSimon Schubert   xfree (storage);
7635796c8dcSSimon Schubert   xfree (demangled_name);
764*a45ae5f8SJohn Marino   cp_demangled_name_parse_free (info);
7655796c8dcSSimon Schubert   return ret;
7665796c8dcSSimon Schubert }
7675796c8dcSSimon Schubert 
7685796c8dcSSimon Schubert /* If FULL_NAME is the demangled name of a C++ function (including an
7695796c8dcSSimon Schubert    arg list, possibly including namespace/class qualifications),
7705796c8dcSSimon Schubert    return a new string containing only the function name (without the
7715796c8dcSSimon Schubert    arg list/class qualifications).  Otherwise, return NULL.  The
7725796c8dcSSimon Schubert    caller is responsible for freeing the memory in question.  */
7735796c8dcSSimon Schubert 
7745796c8dcSSimon Schubert char *
7755796c8dcSSimon Schubert cp_func_name (const char *full_name)
7765796c8dcSSimon Schubert {
7775796c8dcSSimon Schubert   char *ret;
7785796c8dcSSimon Schubert   struct demangle_component *ret_comp;
779*a45ae5f8SJohn Marino   struct demangle_parse_info *info;
7805796c8dcSSimon Schubert 
781*a45ae5f8SJohn Marino   info = cp_demangled_name_to_comp (full_name, NULL);
782*a45ae5f8SJohn Marino   if (!info)
7835796c8dcSSimon Schubert     return NULL;
7845796c8dcSSimon Schubert 
785*a45ae5f8SJohn Marino   ret_comp = unqualified_name_from_comp (info->tree);
7865796c8dcSSimon Schubert 
7875796c8dcSSimon Schubert   ret = NULL;
7885796c8dcSSimon Schubert   if (ret_comp != NULL)
7895796c8dcSSimon Schubert     ret = cp_comp_to_string (ret_comp, 10);
7905796c8dcSSimon Schubert 
791*a45ae5f8SJohn Marino   cp_demangled_name_parse_free (info);
7925796c8dcSSimon Schubert   return ret;
7935796c8dcSSimon Schubert }
7945796c8dcSSimon Schubert 
7955796c8dcSSimon Schubert /* DEMANGLED_NAME is the name of a function, including parameters and
7965796c8dcSSimon Schubert    (optionally) a return type.  Return the name of the function without
7975796c8dcSSimon Schubert    parameters or return type, or NULL if we can not parse the name.  */
7985796c8dcSSimon Schubert 
7995796c8dcSSimon Schubert char *
8005796c8dcSSimon Schubert cp_remove_params (const char *demangled_name)
8015796c8dcSSimon Schubert {
8025796c8dcSSimon Schubert   int done = 0;
8035796c8dcSSimon Schubert   struct demangle_component *ret_comp;
804*a45ae5f8SJohn Marino   struct demangle_parse_info *info;
8055796c8dcSSimon Schubert   char *ret = NULL;
8065796c8dcSSimon Schubert 
8075796c8dcSSimon Schubert   if (demangled_name == NULL)
8085796c8dcSSimon Schubert     return NULL;
8095796c8dcSSimon Schubert 
810*a45ae5f8SJohn Marino   info = cp_demangled_name_to_comp (demangled_name, NULL);
811*a45ae5f8SJohn Marino   if (info == NULL)
8125796c8dcSSimon Schubert     return NULL;
8135796c8dcSSimon Schubert 
8145796c8dcSSimon Schubert   /* First strip off any qualifiers, if we have a function or method.  */
815*a45ae5f8SJohn Marino   ret_comp = info->tree;
8165796c8dcSSimon Schubert   while (!done)
8175796c8dcSSimon Schubert     switch (ret_comp->type)
8185796c8dcSSimon Schubert       {
8195796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_CONST:
8205796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_RESTRICT:
8215796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_VOLATILE:
8225796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_CONST_THIS:
8235796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_RESTRICT_THIS:
8245796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_VOLATILE_THIS:
8255796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
8265796c8dcSSimon Schubert         ret_comp = d_left (ret_comp);
8275796c8dcSSimon Schubert         break;
8285796c8dcSSimon Schubert       default:
8295796c8dcSSimon Schubert 	done = 1;
8305796c8dcSSimon Schubert 	break;
8315796c8dcSSimon Schubert       }
8325796c8dcSSimon Schubert 
8335796c8dcSSimon Schubert   /* What we have now should be a function.  Return its name.  */
8345796c8dcSSimon Schubert   if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
8355796c8dcSSimon Schubert     ret = cp_comp_to_string (d_left (ret_comp), 10);
8365796c8dcSSimon Schubert 
837*a45ae5f8SJohn Marino   cp_demangled_name_parse_free (info);
8385796c8dcSSimon Schubert   return ret;
8395796c8dcSSimon Schubert }
8405796c8dcSSimon Schubert 
8415796c8dcSSimon Schubert /* Here are some random pieces of trivia to keep in mind while trying
8425796c8dcSSimon Schubert    to take apart demangled names:
8435796c8dcSSimon Schubert 
8445796c8dcSSimon Schubert    - Names can contain function arguments or templates, so the process
8455796c8dcSSimon Schubert      has to be, to some extent recursive: maybe keep track of your
8465796c8dcSSimon Schubert      depth based on encountering <> and ().
8475796c8dcSSimon Schubert 
8485796c8dcSSimon Schubert    - Parentheses don't just have to happen at the end of a name: they
8495796c8dcSSimon Schubert      can occur even if the name in question isn't a function, because
8505796c8dcSSimon Schubert      a template argument might be a type that's a function.
8515796c8dcSSimon Schubert 
8525796c8dcSSimon Schubert    - Conversely, even if you're trying to deal with a function, its
8535796c8dcSSimon Schubert      demangled name might not end with ')': it could be a const or
8545796c8dcSSimon Schubert      volatile class method, in which case it ends with "const" or
8555796c8dcSSimon Schubert      "volatile".
8565796c8dcSSimon Schubert 
8575796c8dcSSimon Schubert    - Parentheses are also used in anonymous namespaces: a variable
8585796c8dcSSimon Schubert      'foo' in an anonymous namespace gets demangled as "(anonymous
8595796c8dcSSimon Schubert      namespace)::foo".
8605796c8dcSSimon Schubert 
8615796c8dcSSimon Schubert    - And operator names can contain parentheses or angle brackets.  */
8625796c8dcSSimon Schubert 
8635796c8dcSSimon Schubert /* FIXME: carlton/2003-03-13: We have several functions here with
8645796c8dcSSimon Schubert    overlapping functionality; can we combine them?  Also, do they
8655796c8dcSSimon Schubert    handle all the above considerations correctly?  */
8665796c8dcSSimon Schubert 
8675796c8dcSSimon Schubert 
8685796c8dcSSimon Schubert /* This returns the length of first component of NAME, which should be
8695796c8dcSSimon Schubert    the demangled name of a C++ variable/function/method/etc.
8705796c8dcSSimon Schubert    Specifically, it returns the index of the first colon forming the
8715796c8dcSSimon Schubert    boundary of the first component: so, given 'A::foo' or 'A::B::foo'
8725796c8dcSSimon Schubert    it returns the 1, and given 'foo', it returns 0.  */
8735796c8dcSSimon Schubert 
8745796c8dcSSimon Schubert /* The character in NAME indexed by the return value is guaranteed to
8755796c8dcSSimon Schubert    always be either ':' or '\0'.  */
8765796c8dcSSimon Schubert 
8775796c8dcSSimon Schubert /* NOTE: carlton/2003-03-13: This function is currently only intended
8785796c8dcSSimon Schubert    for internal use: it's probably not entirely safe when called on
8795796c8dcSSimon Schubert    user-generated input, because some of the 'index += 2' lines in
8805796c8dcSSimon Schubert    cp_find_first_component_aux might go past the end of malformed
8815796c8dcSSimon Schubert    input.  */
8825796c8dcSSimon Schubert 
8835796c8dcSSimon Schubert unsigned int
8845796c8dcSSimon Schubert cp_find_first_component (const char *name)
8855796c8dcSSimon Schubert {
8865796c8dcSSimon Schubert   return cp_find_first_component_aux (name, 0);
8875796c8dcSSimon Schubert }
8885796c8dcSSimon Schubert 
8895796c8dcSSimon Schubert /* Helper function for cp_find_first_component.  Like that function,
8905796c8dcSSimon Schubert    it returns the length of the first component of NAME, but to make
8915796c8dcSSimon Schubert    the recursion easier, it also stops if it reaches an unexpected ')'
8925796c8dcSSimon Schubert    or '>' if the value of PERMISSIVE is nonzero.  */
8935796c8dcSSimon Schubert 
8945796c8dcSSimon Schubert /* Let's optimize away calls to strlen("operator").  */
8955796c8dcSSimon Schubert 
8965796c8dcSSimon Schubert #define LENGTH_OF_OPERATOR 8
8975796c8dcSSimon Schubert 
8985796c8dcSSimon Schubert static unsigned int
8995796c8dcSSimon Schubert cp_find_first_component_aux (const char *name, int permissive)
9005796c8dcSSimon Schubert {
9015796c8dcSSimon Schubert   unsigned int index = 0;
9025796c8dcSSimon Schubert   /* Operator names can show up in unexpected places.  Since these can
9035796c8dcSSimon Schubert      contain parentheses or angle brackets, they can screw up the
9045796c8dcSSimon Schubert      recursion.  But not every string 'operator' is part of an
9055796c8dcSSimon Schubert      operater name: e.g. you could have a variable 'cooperator'.  So
9065796c8dcSSimon Schubert      this variable tells us whether or not we should treat the string
9075796c8dcSSimon Schubert      'operator' as starting an operator.  */
9085796c8dcSSimon Schubert   int operator_possible = 1;
9095796c8dcSSimon Schubert 
9105796c8dcSSimon Schubert   for (;; ++index)
9115796c8dcSSimon Schubert     {
9125796c8dcSSimon Schubert       switch (name[index])
9135796c8dcSSimon Schubert 	{
9145796c8dcSSimon Schubert 	case '<':
9155796c8dcSSimon Schubert 	  /* Template; eat it up.  The calls to cp_first_component
9165796c8dcSSimon Schubert 	     should only return (I hope!) when they reach the '>'
9175796c8dcSSimon Schubert 	     terminating the component or a '::' between two
9185796c8dcSSimon Schubert 	     components.  (Hence the '+ 2'.)  */
9195796c8dcSSimon Schubert 	  index += 1;
9205796c8dcSSimon Schubert 	  for (index += cp_find_first_component_aux (name + index, 1);
9215796c8dcSSimon Schubert 	       name[index] != '>';
9225796c8dcSSimon Schubert 	       index += cp_find_first_component_aux (name + index, 1))
9235796c8dcSSimon Schubert 	    {
9245796c8dcSSimon Schubert 	      if (name[index] != ':')
9255796c8dcSSimon Schubert 		{
9265796c8dcSSimon Schubert 		  demangled_name_complaint (name);
9275796c8dcSSimon Schubert 		  return strlen (name);
9285796c8dcSSimon Schubert 		}
9295796c8dcSSimon Schubert 	      index += 2;
9305796c8dcSSimon Schubert 	    }
9315796c8dcSSimon Schubert 	  operator_possible = 1;
9325796c8dcSSimon Schubert 	  break;
9335796c8dcSSimon Schubert 	case '(':
9345796c8dcSSimon Schubert 	  /* Similar comment as to '<'.  */
9355796c8dcSSimon Schubert 	  index += 1;
9365796c8dcSSimon Schubert 	  for (index += cp_find_first_component_aux (name + index, 1);
9375796c8dcSSimon Schubert 	       name[index] != ')';
9385796c8dcSSimon Schubert 	       index += cp_find_first_component_aux (name + index, 1))
9395796c8dcSSimon Schubert 	    {
9405796c8dcSSimon Schubert 	      if (name[index] != ':')
9415796c8dcSSimon Schubert 		{
9425796c8dcSSimon Schubert 		  demangled_name_complaint (name);
9435796c8dcSSimon Schubert 		  return strlen (name);
9445796c8dcSSimon Schubert 		}
9455796c8dcSSimon Schubert 	      index += 2;
9465796c8dcSSimon Schubert 	    }
9475796c8dcSSimon Schubert 	  operator_possible = 1;
9485796c8dcSSimon Schubert 	  break;
9495796c8dcSSimon Schubert 	case '>':
9505796c8dcSSimon Schubert 	case ')':
9515796c8dcSSimon Schubert 	  if (permissive)
9525796c8dcSSimon Schubert 	    return index;
9535796c8dcSSimon Schubert 	  else
9545796c8dcSSimon Schubert 	    {
9555796c8dcSSimon Schubert 	      demangled_name_complaint (name);
9565796c8dcSSimon Schubert 	      return strlen (name);
9575796c8dcSSimon Schubert 	    }
9585796c8dcSSimon Schubert 	case '\0':
9595796c8dcSSimon Schubert 	case ':':
9605796c8dcSSimon Schubert 	  return index;
9615796c8dcSSimon Schubert 	case 'o':
9625796c8dcSSimon Schubert 	  /* Operator names can screw up the recursion.  */
9635796c8dcSSimon Schubert 	  if (operator_possible
964c50c785cSJohn Marino 	      && strncmp (name + index, "operator",
965c50c785cSJohn Marino 			  LENGTH_OF_OPERATOR) == 0)
9665796c8dcSSimon Schubert 	    {
9675796c8dcSSimon Schubert 	      index += LENGTH_OF_OPERATOR;
9685796c8dcSSimon Schubert 	      while (ISSPACE(name[index]))
9695796c8dcSSimon Schubert 		++index;
9705796c8dcSSimon Schubert 	      switch (name[index])
9715796c8dcSSimon Schubert 		{
9725796c8dcSSimon Schubert 		  /* Skip over one less than the appropriate number of
9735796c8dcSSimon Schubert 		     characters: the for loop will skip over the last
9745796c8dcSSimon Schubert 		     one.  */
9755796c8dcSSimon Schubert 		case '<':
9765796c8dcSSimon Schubert 		  if (name[index + 1] == '<')
9775796c8dcSSimon Schubert 		    index += 1;
9785796c8dcSSimon Schubert 		  else
9795796c8dcSSimon Schubert 		    index += 0;
9805796c8dcSSimon Schubert 		  break;
9815796c8dcSSimon Schubert 		case '>':
9825796c8dcSSimon Schubert 		case '-':
9835796c8dcSSimon Schubert 		  if (name[index + 1] == '>')
9845796c8dcSSimon Schubert 		    index += 1;
9855796c8dcSSimon Schubert 		  else
9865796c8dcSSimon Schubert 		    index += 0;
9875796c8dcSSimon Schubert 		  break;
9885796c8dcSSimon Schubert 		case '(':
9895796c8dcSSimon Schubert 		  index += 1;
9905796c8dcSSimon Schubert 		  break;
9915796c8dcSSimon Schubert 		default:
9925796c8dcSSimon Schubert 		  index += 0;
9935796c8dcSSimon Schubert 		  break;
9945796c8dcSSimon Schubert 		}
9955796c8dcSSimon Schubert 	    }
9965796c8dcSSimon Schubert 	  operator_possible = 0;
9975796c8dcSSimon Schubert 	  break;
9985796c8dcSSimon Schubert 	case ' ':
9995796c8dcSSimon Schubert 	case ',':
10005796c8dcSSimon Schubert 	case '.':
10015796c8dcSSimon Schubert 	case '&':
10025796c8dcSSimon Schubert 	case '*':
10035796c8dcSSimon Schubert 	  /* NOTE: carlton/2003-04-18: I'm not sure what the precise
10045796c8dcSSimon Schubert 	     set of relevant characters are here: it's necessary to
10055796c8dcSSimon Schubert 	     include any character that can show up before 'operator'
10065796c8dcSSimon Schubert 	     in a demangled name, and it's safe to include any
10075796c8dcSSimon Schubert 	     character that can't be part of an identifier's name.  */
10085796c8dcSSimon Schubert 	  operator_possible = 1;
10095796c8dcSSimon Schubert 	  break;
10105796c8dcSSimon Schubert 	default:
10115796c8dcSSimon Schubert 	  operator_possible = 0;
10125796c8dcSSimon Schubert 	  break;
10135796c8dcSSimon Schubert 	}
10145796c8dcSSimon Schubert     }
10155796c8dcSSimon Schubert }
10165796c8dcSSimon Schubert 
10175796c8dcSSimon Schubert /* Complain about a demangled name that we don't know how to parse.
10185796c8dcSSimon Schubert    NAME is the demangled name in question.  */
10195796c8dcSSimon Schubert 
10205796c8dcSSimon Schubert static void
10215796c8dcSSimon Schubert demangled_name_complaint (const char *name)
10225796c8dcSSimon Schubert {
10235796c8dcSSimon Schubert   complaint (&symfile_complaints,
10245796c8dcSSimon Schubert 	     "unexpected demangled name '%s'", name);
10255796c8dcSSimon Schubert }
10265796c8dcSSimon Schubert 
10275796c8dcSSimon Schubert /* If NAME is the fully-qualified name of a C++
10285796c8dcSSimon Schubert    function/variable/method/etc., this returns the length of its
10295796c8dcSSimon Schubert    entire prefix: all of the namespaces and classes that make up its
10305796c8dcSSimon Schubert    name.  Given 'A::foo', it returns 1, given 'A::B::foo', it returns
10315796c8dcSSimon Schubert    4, given 'foo', it returns 0.  */
10325796c8dcSSimon Schubert 
10335796c8dcSSimon Schubert unsigned int
10345796c8dcSSimon Schubert cp_entire_prefix_len (const char *name)
10355796c8dcSSimon Schubert {
10365796c8dcSSimon Schubert   unsigned int current_len = cp_find_first_component (name);
10375796c8dcSSimon Schubert   unsigned int previous_len = 0;
10385796c8dcSSimon Schubert 
10395796c8dcSSimon Schubert   while (name[current_len] != '\0')
10405796c8dcSSimon Schubert     {
10415796c8dcSSimon Schubert       gdb_assert (name[current_len] == ':');
10425796c8dcSSimon Schubert       previous_len = current_len;
10435796c8dcSSimon Schubert       /* Skip the '::'.  */
10445796c8dcSSimon Schubert       current_len += 2;
10455796c8dcSSimon Schubert       current_len += cp_find_first_component (name + current_len);
10465796c8dcSSimon Schubert     }
10475796c8dcSSimon Schubert 
10485796c8dcSSimon Schubert   return previous_len;
10495796c8dcSSimon Schubert }
10505796c8dcSSimon Schubert 
10515796c8dcSSimon Schubert /* Overload resolution functions.  */
10525796c8dcSSimon Schubert 
10535796c8dcSSimon Schubert /* Test to see if SYM is a symbol that we haven't seen corresponding
10545796c8dcSSimon Schubert    to a function named OLOAD_NAME.  If so, add it to the current
10555796c8dcSSimon Schubert    completion list.  */
10565796c8dcSSimon Schubert 
10575796c8dcSSimon Schubert static void
1058c50c785cSJohn Marino overload_list_add_symbol (struct symbol *sym,
1059c50c785cSJohn Marino 			  const char *oload_name)
10605796c8dcSSimon Schubert {
10615796c8dcSSimon Schubert   int newsize;
10625796c8dcSSimon Schubert   int i;
10635796c8dcSSimon Schubert   char *sym_name;
10645796c8dcSSimon Schubert 
1065c50c785cSJohn Marino   /* If there is no type information, we can't do anything, so
1066c50c785cSJohn Marino      skip.  */
10675796c8dcSSimon Schubert   if (SYMBOL_TYPE (sym) == NULL)
10685796c8dcSSimon Schubert     return;
10695796c8dcSSimon Schubert 
10705796c8dcSSimon Schubert   /* skip any symbols that we've already considered.  */
10715796c8dcSSimon Schubert   for (i = 0; i < sym_return_val_index; ++i)
10725796c8dcSSimon Schubert     if (strcmp (SYMBOL_LINKAGE_NAME (sym),
10735796c8dcSSimon Schubert 		SYMBOL_LINKAGE_NAME (sym_return_val[i])) == 0)
10745796c8dcSSimon Schubert       return;
10755796c8dcSSimon Schubert 
10765796c8dcSSimon Schubert   /* Get the demangled name without parameters */
10775796c8dcSSimon Schubert   sym_name = cp_remove_params (SYMBOL_NATURAL_NAME (sym));
10785796c8dcSSimon Schubert   if (!sym_name)
10795796c8dcSSimon Schubert     return;
10805796c8dcSSimon Schubert 
10815796c8dcSSimon Schubert   /* skip symbols that cannot match */
10825796c8dcSSimon Schubert   if (strcmp (sym_name, oload_name) != 0)
10835796c8dcSSimon Schubert     {
10845796c8dcSSimon Schubert       xfree (sym_name);
10855796c8dcSSimon Schubert       return;
10865796c8dcSSimon Schubert     }
10875796c8dcSSimon Schubert 
10885796c8dcSSimon Schubert   xfree (sym_name);
10895796c8dcSSimon Schubert 
1090c50c785cSJohn Marino   /* We have a match for an overload instance, so add SYM to the
1091c50c785cSJohn Marino      current list of overload instances */
10925796c8dcSSimon Schubert   if (sym_return_val_index + 3 > sym_return_val_size)
10935796c8dcSSimon Schubert     {
10945796c8dcSSimon Schubert       newsize = (sym_return_val_size *= 2) * sizeof (struct symbol *);
1095c50c785cSJohn Marino       sym_return_val = (struct symbol **)
1096c50c785cSJohn Marino 	xrealloc ((char *) sym_return_val, newsize);
10975796c8dcSSimon Schubert     }
10985796c8dcSSimon Schubert   sym_return_val[sym_return_val_index++] = sym;
10995796c8dcSSimon Schubert   sym_return_val[sym_return_val_index] = NULL;
11005796c8dcSSimon Schubert }
11015796c8dcSSimon Schubert 
11025796c8dcSSimon Schubert /* Return a null-terminated list of pointers to function symbols that
11035796c8dcSSimon Schubert    are named FUNC_NAME and are visible within NAMESPACE.  */
11045796c8dcSSimon Schubert 
11055796c8dcSSimon Schubert struct symbol **
11065796c8dcSSimon Schubert make_symbol_overload_list (const char *func_name,
11075796c8dcSSimon Schubert 			   const char *namespace)
11085796c8dcSSimon Schubert {
11095796c8dcSSimon Schubert   struct cleanup *old_cleanups;
1110cf7f2e2dSJohn Marino   const char *name;
11115796c8dcSSimon Schubert 
11125796c8dcSSimon Schubert   sym_return_val_size = 100;
11135796c8dcSSimon Schubert   sym_return_val_index = 0;
11145796c8dcSSimon Schubert   sym_return_val = xmalloc ((sym_return_val_size + 1) *
11155796c8dcSSimon Schubert 			    sizeof (struct symbol *));
11165796c8dcSSimon Schubert   sym_return_val[0] = NULL;
11175796c8dcSSimon Schubert 
11185796c8dcSSimon Schubert   old_cleanups = make_cleanup (xfree, sym_return_val);
11195796c8dcSSimon Schubert 
11205796c8dcSSimon Schubert   make_symbol_overload_list_using (func_name, namespace);
11215796c8dcSSimon Schubert 
1122cf7f2e2dSJohn Marino   if (namespace[0] == '\0')
1123cf7f2e2dSJohn Marino     name = func_name;
1124cf7f2e2dSJohn Marino   else
1125cf7f2e2dSJohn Marino     {
1126cf7f2e2dSJohn Marino       char *concatenated_name
1127cf7f2e2dSJohn Marino 	= alloca (strlen (namespace) + 2 + strlen (func_name) + 1);
1128cf7f2e2dSJohn Marino       strcpy (concatenated_name, namespace);
1129cf7f2e2dSJohn Marino       strcat (concatenated_name, "::");
1130cf7f2e2dSJohn Marino       strcat (concatenated_name, func_name);
1131cf7f2e2dSJohn Marino       name = concatenated_name;
1132cf7f2e2dSJohn Marino     }
1133cf7f2e2dSJohn Marino 
1134cf7f2e2dSJohn Marino   make_symbol_overload_list_qualified (name);
1135cf7f2e2dSJohn Marino 
11365796c8dcSSimon Schubert   discard_cleanups (old_cleanups);
11375796c8dcSSimon Schubert 
11385796c8dcSSimon Schubert   return sym_return_val;
11395796c8dcSSimon Schubert }
11405796c8dcSSimon Schubert 
1141cf7f2e2dSJohn Marino /* Add all symbols with a name matching NAME in BLOCK to the overload
1142cf7f2e2dSJohn Marino    list.  */
1143cf7f2e2dSJohn Marino 
1144cf7f2e2dSJohn Marino static void
1145cf7f2e2dSJohn Marino make_symbol_overload_list_block (const char *name,
1146cf7f2e2dSJohn Marino                                  const struct block *block)
1147cf7f2e2dSJohn Marino {
1148cf7f2e2dSJohn Marino   struct dict_iterator iter;
1149cf7f2e2dSJohn Marino   struct symbol *sym;
1150cf7f2e2dSJohn Marino 
1151cf7f2e2dSJohn Marino   const struct dictionary *dict = BLOCK_DICT (block);
1152cf7f2e2dSJohn Marino 
1153cf7f2e2dSJohn Marino   for (sym = dict_iter_name_first (dict, name, &iter);
1154cf7f2e2dSJohn Marino        sym != NULL;
1155cf7f2e2dSJohn Marino        sym = dict_iter_name_next (name, &iter))
1156cf7f2e2dSJohn Marino     overload_list_add_symbol (sym, name);
1157cf7f2e2dSJohn Marino }
1158cf7f2e2dSJohn Marino 
1159cf7f2e2dSJohn Marino /* Adds the function FUNC_NAME from NAMESPACE to the overload set.  */
1160cf7f2e2dSJohn Marino 
1161cf7f2e2dSJohn Marino static void
1162cf7f2e2dSJohn Marino make_symbol_overload_list_namespace (const char *func_name,
1163cf7f2e2dSJohn Marino                                      const char *namespace)
1164cf7f2e2dSJohn Marino {
1165cf7f2e2dSJohn Marino   const char *name;
1166cf7f2e2dSJohn Marino   const struct block *block = NULL;
1167cf7f2e2dSJohn Marino 
1168cf7f2e2dSJohn Marino   if (namespace[0] == '\0')
1169cf7f2e2dSJohn Marino     name = func_name;
1170cf7f2e2dSJohn Marino   else
1171cf7f2e2dSJohn Marino     {
1172cf7f2e2dSJohn Marino       char *concatenated_name
1173cf7f2e2dSJohn Marino 	= alloca (strlen (namespace) + 2 + strlen (func_name) + 1);
1174cf7f2e2dSJohn Marino 
1175cf7f2e2dSJohn Marino       strcpy (concatenated_name, namespace);
1176cf7f2e2dSJohn Marino       strcat (concatenated_name, "::");
1177cf7f2e2dSJohn Marino       strcat (concatenated_name, func_name);
1178cf7f2e2dSJohn Marino       name = concatenated_name;
1179cf7f2e2dSJohn Marino     }
1180cf7f2e2dSJohn Marino 
1181cf7f2e2dSJohn Marino   /* Look in the static block.  */
1182cf7f2e2dSJohn Marino   block = block_static_block (get_selected_block (0));
1183c50c785cSJohn Marino   if (block)
1184cf7f2e2dSJohn Marino     make_symbol_overload_list_block (name, block);
1185cf7f2e2dSJohn Marino 
1186cf7f2e2dSJohn Marino   /* Look in the global block.  */
1187cf7f2e2dSJohn Marino   block = block_global_block (block);
1188c50c785cSJohn Marino   if (block)
1189cf7f2e2dSJohn Marino     make_symbol_overload_list_block (name, block);
1190cf7f2e2dSJohn Marino 
1191cf7f2e2dSJohn Marino }
1192cf7f2e2dSJohn Marino 
1193c50c785cSJohn Marino /* Search the namespace of the given type and namespace of and public
1194c50c785cSJohn Marino    base types.  */
1195cf7f2e2dSJohn Marino 
1196cf7f2e2dSJohn Marino static void
1197cf7f2e2dSJohn Marino make_symbol_overload_list_adl_namespace (struct type *type,
1198cf7f2e2dSJohn Marino                                          const char *func_name)
1199cf7f2e2dSJohn Marino {
1200cf7f2e2dSJohn Marino   char *namespace;
1201cf7f2e2dSJohn Marino   char *type_name;
1202cf7f2e2dSJohn Marino   int i, prefix_len;
1203cf7f2e2dSJohn Marino 
1204c50c785cSJohn Marino   while (TYPE_CODE (type) == TYPE_CODE_PTR
1205c50c785cSJohn Marino 	 || TYPE_CODE (type) == TYPE_CODE_REF
1206cf7f2e2dSJohn Marino          || TYPE_CODE (type) == TYPE_CODE_ARRAY
1207cf7f2e2dSJohn Marino          || TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1208cf7f2e2dSJohn Marino     {
1209cf7f2e2dSJohn Marino       if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1210cf7f2e2dSJohn Marino 	type = check_typedef(type);
1211cf7f2e2dSJohn Marino       else
1212cf7f2e2dSJohn Marino 	type = TYPE_TARGET_TYPE (type);
1213cf7f2e2dSJohn Marino     }
1214cf7f2e2dSJohn Marino 
1215cf7f2e2dSJohn Marino   type_name = TYPE_NAME (type);
1216cf7f2e2dSJohn Marino 
1217cf7f2e2dSJohn Marino   if (type_name == NULL)
1218cf7f2e2dSJohn Marino     return;
1219cf7f2e2dSJohn Marino 
1220cf7f2e2dSJohn Marino   prefix_len = cp_entire_prefix_len (type_name);
1221cf7f2e2dSJohn Marino 
1222cf7f2e2dSJohn Marino   if (prefix_len != 0)
1223cf7f2e2dSJohn Marino     {
1224cf7f2e2dSJohn Marino       namespace = alloca (prefix_len + 1);
1225cf7f2e2dSJohn Marino       strncpy (namespace, type_name, prefix_len);
1226cf7f2e2dSJohn Marino       namespace[prefix_len] = '\0';
1227cf7f2e2dSJohn Marino 
1228cf7f2e2dSJohn Marino       make_symbol_overload_list_namespace (func_name, namespace);
1229cf7f2e2dSJohn Marino     }
1230cf7f2e2dSJohn Marino 
1231cf7f2e2dSJohn Marino   /* Check public base type */
1232cf7f2e2dSJohn Marino   if (TYPE_CODE (type) == TYPE_CODE_CLASS)
1233cf7f2e2dSJohn Marino     for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
1234cf7f2e2dSJohn Marino       {
1235cf7f2e2dSJohn Marino 	if (BASETYPE_VIA_PUBLIC (type, i))
1236c50c785cSJohn Marino 	  make_symbol_overload_list_adl_namespace (TYPE_BASECLASS (type,
1237c50c785cSJohn Marino 								   i),
1238cf7f2e2dSJohn Marino 						   func_name);
1239cf7f2e2dSJohn Marino       }
1240cf7f2e2dSJohn Marino }
1241cf7f2e2dSJohn Marino 
1242c50c785cSJohn Marino /* Adds the overload list overload candidates for FUNC_NAME found
1243c50c785cSJohn Marino    through argument dependent lookup.  */
1244cf7f2e2dSJohn Marino 
1245cf7f2e2dSJohn Marino struct symbol **
1246cf7f2e2dSJohn Marino make_symbol_overload_list_adl (struct type **arg_types, int nargs,
1247cf7f2e2dSJohn Marino                                const char *func_name)
1248cf7f2e2dSJohn Marino {
1249cf7f2e2dSJohn Marino   int i;
1250cf7f2e2dSJohn Marino 
1251cf7f2e2dSJohn Marino   gdb_assert (sym_return_val_size != -1);
1252cf7f2e2dSJohn Marino 
1253cf7f2e2dSJohn Marino   for (i = 1; i <= nargs; i++)
1254c50c785cSJohn Marino     make_symbol_overload_list_adl_namespace (arg_types[i - 1],
1255c50c785cSJohn Marino 					     func_name);
1256cf7f2e2dSJohn Marino 
1257cf7f2e2dSJohn Marino   return sym_return_val;
1258cf7f2e2dSJohn Marino }
1259cf7f2e2dSJohn Marino 
1260c50c785cSJohn Marino /* Used for cleanups to reset the "searched" flag in case of an
1261c50c785cSJohn Marino    error.  */
1262cf7f2e2dSJohn Marino 
1263cf7f2e2dSJohn Marino static void
1264cf7f2e2dSJohn Marino reset_directive_searched (void *data)
1265cf7f2e2dSJohn Marino {
1266cf7f2e2dSJohn Marino   struct using_direct *direct = data;
1267cf7f2e2dSJohn Marino   direct->searched = 0;
1268cf7f2e2dSJohn Marino }
1269cf7f2e2dSJohn Marino 
12705796c8dcSSimon Schubert /* This applies the using directives to add namespaces to search in,
12715796c8dcSSimon Schubert    and then searches for overloads in all of those namespaces.  It
12725796c8dcSSimon Schubert    adds the symbols found to sym_return_val.  Arguments are as in
12735796c8dcSSimon Schubert    make_symbol_overload_list.  */
12745796c8dcSSimon Schubert 
12755796c8dcSSimon Schubert static void
12765796c8dcSSimon Schubert make_symbol_overload_list_using (const char *func_name,
12775796c8dcSSimon Schubert 				 const char *namespace)
12785796c8dcSSimon Schubert {
1279cf7f2e2dSJohn Marino   struct using_direct *current;
1280cf7f2e2dSJohn Marino   const struct block *block;
12815796c8dcSSimon Schubert 
12825796c8dcSSimon Schubert   /* First, go through the using directives.  If any of them apply,
12835796c8dcSSimon Schubert      look in the appropriate namespaces for new functions to match
12845796c8dcSSimon Schubert      on.  */
12855796c8dcSSimon Schubert 
1286cf7f2e2dSJohn Marino   for (block = get_selected_block (0);
1287cf7f2e2dSJohn Marino        block != NULL;
1288cf7f2e2dSJohn Marino        block = BLOCK_SUPERBLOCK (block))
1289cf7f2e2dSJohn Marino     for (current = block_using (block);
12905796c8dcSSimon Schubert 	current != NULL;
12915796c8dcSSimon Schubert 	current = current->next)
12925796c8dcSSimon Schubert       {
1293cf7f2e2dSJohn Marino 	/* Prevent recursive calls.  */
1294cf7f2e2dSJohn Marino 	if (current->searched)
1295cf7f2e2dSJohn Marino 	  continue;
1296cf7f2e2dSJohn Marino 
1297c50c785cSJohn Marino         /* If this is a namespace alias or imported declaration ignore
1298c50c785cSJohn Marino 	   it.  */
1299cf7f2e2dSJohn Marino         if (current->alias != NULL || current->declaration != NULL)
1300cf7f2e2dSJohn Marino           continue;
1301cf7f2e2dSJohn Marino 
13025796c8dcSSimon Schubert         if (strcmp (namespace, current->import_dest) == 0)
13035796c8dcSSimon Schubert 	  {
1304c50c785cSJohn Marino 	    /* Mark this import as searched so that the recursive call
1305c50c785cSJohn Marino 	       does not search it again.  */
1306cf7f2e2dSJohn Marino 	    struct cleanup *old_chain;
1307cf7f2e2dSJohn Marino 	    current->searched = 1;
1308c50c785cSJohn Marino 	    old_chain = make_cleanup (reset_directive_searched,
1309c50c785cSJohn Marino 				      current);
1310cf7f2e2dSJohn Marino 
1311c50c785cSJohn Marino 	    make_symbol_overload_list_using (func_name,
1312c50c785cSJohn Marino 					     current->import_src);
1313cf7f2e2dSJohn Marino 
1314cf7f2e2dSJohn Marino 	    current->searched = 0;
1315cf7f2e2dSJohn Marino 	    discard_cleanups (old_chain);
13165796c8dcSSimon Schubert 	  }
13175796c8dcSSimon Schubert       }
13185796c8dcSSimon Schubert 
13195796c8dcSSimon Schubert   /* Now, add names for this namespace.  */
1320cf7f2e2dSJohn Marino   make_symbol_overload_list_namespace (func_name, namespace);
13215796c8dcSSimon Schubert }
13225796c8dcSSimon Schubert 
13235796c8dcSSimon Schubert /* This does the bulk of the work of finding overloaded symbols.
13245796c8dcSSimon Schubert    FUNC_NAME is the name of the overloaded function we're looking for
13255796c8dcSSimon Schubert    (possibly including namespace info).  */
13265796c8dcSSimon Schubert 
13275796c8dcSSimon Schubert static void
13285796c8dcSSimon Schubert make_symbol_overload_list_qualified (const char *func_name)
13295796c8dcSSimon Schubert {
13305796c8dcSSimon Schubert   struct symbol *sym;
13315796c8dcSSimon Schubert   struct symtab *s;
13325796c8dcSSimon Schubert   struct objfile *objfile;
13335796c8dcSSimon Schubert   const struct block *b, *surrounding_static_block = 0;
13345796c8dcSSimon Schubert   struct dict_iterator iter;
13355796c8dcSSimon Schubert   const struct dictionary *dict;
13365796c8dcSSimon Schubert 
1337c50c785cSJohn Marino   /* Look through the partial symtabs for all symbols which begin by
1338c50c785cSJohn Marino      matching FUNC_NAME.  Make sure we read that symbol table in.  */
13395796c8dcSSimon Schubert 
1340cf7f2e2dSJohn Marino   ALL_OBJFILES (objfile)
1341cf7f2e2dSJohn Marino   {
1342cf7f2e2dSJohn Marino     if (objfile->sf)
1343cf7f2e2dSJohn Marino       objfile->sf->qf->expand_symtabs_for_function (objfile, func_name);
1344cf7f2e2dSJohn Marino   }
13455796c8dcSSimon Schubert 
13465796c8dcSSimon Schubert   /* Search upwards from currently selected frame (so that we can
13475796c8dcSSimon Schubert      complete on local vars.  */
13485796c8dcSSimon Schubert 
13495796c8dcSSimon Schubert   for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
1350cf7f2e2dSJohn Marino     make_symbol_overload_list_block (func_name, b);
13515796c8dcSSimon Schubert 
13525796c8dcSSimon Schubert   surrounding_static_block = block_static_block (get_selected_block (0));
13535796c8dcSSimon Schubert 
13545796c8dcSSimon Schubert   /* Go through the symtabs and check the externs and statics for
13555796c8dcSSimon Schubert      symbols which match.  */
13565796c8dcSSimon Schubert 
13575796c8dcSSimon Schubert   ALL_PRIMARY_SYMTABS (objfile, s)
13585796c8dcSSimon Schubert   {
13595796c8dcSSimon Schubert     QUIT;
13605796c8dcSSimon Schubert     b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
1361cf7f2e2dSJohn Marino     make_symbol_overload_list_block (func_name, b);
13625796c8dcSSimon Schubert   }
13635796c8dcSSimon Schubert 
13645796c8dcSSimon Schubert   ALL_PRIMARY_SYMTABS (objfile, s)
13655796c8dcSSimon Schubert   {
13665796c8dcSSimon Schubert     QUIT;
13675796c8dcSSimon Schubert     b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
13685796c8dcSSimon Schubert     /* Don't do this block twice.  */
13695796c8dcSSimon Schubert     if (b == surrounding_static_block)
13705796c8dcSSimon Schubert       continue;
1371cf7f2e2dSJohn Marino     make_symbol_overload_list_block (func_name, b);
13725796c8dcSSimon Schubert   }
13735796c8dcSSimon Schubert }
13745796c8dcSSimon Schubert 
13755796c8dcSSimon Schubert /* Lookup the rtti type for a class name.  */
13765796c8dcSSimon Schubert 
13775796c8dcSSimon Schubert struct type *
13785796c8dcSSimon Schubert cp_lookup_rtti_type (const char *name, struct block *block)
13795796c8dcSSimon Schubert {
13805796c8dcSSimon Schubert   struct symbol * rtti_sym;
13815796c8dcSSimon Schubert   struct type * rtti_type;
13825796c8dcSSimon Schubert 
13835796c8dcSSimon Schubert   rtti_sym = lookup_symbol (name, block, STRUCT_DOMAIN, NULL);
13845796c8dcSSimon Schubert 
13855796c8dcSSimon Schubert   if (rtti_sym == NULL)
13865796c8dcSSimon Schubert     {
13875796c8dcSSimon Schubert       warning (_("RTTI symbol not found for class '%s'"), name);
13885796c8dcSSimon Schubert       return NULL;
13895796c8dcSSimon Schubert     }
13905796c8dcSSimon Schubert 
13915796c8dcSSimon Schubert   if (SYMBOL_CLASS (rtti_sym) != LOC_TYPEDEF)
13925796c8dcSSimon Schubert     {
13935796c8dcSSimon Schubert       warning (_("RTTI symbol for class '%s' is not a type"), name);
13945796c8dcSSimon Schubert       return NULL;
13955796c8dcSSimon Schubert     }
13965796c8dcSSimon Schubert 
13975796c8dcSSimon Schubert   rtti_type = SYMBOL_TYPE (rtti_sym);
13985796c8dcSSimon Schubert 
13995796c8dcSSimon Schubert   switch (TYPE_CODE (rtti_type))
14005796c8dcSSimon Schubert     {
14015796c8dcSSimon Schubert     case TYPE_CODE_CLASS:
14025796c8dcSSimon Schubert       break;
14035796c8dcSSimon Schubert     case TYPE_CODE_NAMESPACE:
14045796c8dcSSimon Schubert       /* chastain/2003-11-26: the symbol tables often contain fake
14055796c8dcSSimon Schubert 	 symbols for namespaces with the same name as the struct.
14065796c8dcSSimon Schubert 	 This warning is an indication of a bug in the lookup order
14075796c8dcSSimon Schubert 	 or a bug in the way that the symbol tables are populated.  */
14085796c8dcSSimon Schubert       warning (_("RTTI symbol for class '%s' is a namespace"), name);
14095796c8dcSSimon Schubert       return NULL;
14105796c8dcSSimon Schubert     default:
14115796c8dcSSimon Schubert       warning (_("RTTI symbol for class '%s' has bad type"), name);
14125796c8dcSSimon Schubert       return NULL;
14135796c8dcSSimon Schubert     }
14145796c8dcSSimon Schubert 
14155796c8dcSSimon Schubert   return rtti_type;
14165796c8dcSSimon Schubert }
14175796c8dcSSimon Schubert 
14185796c8dcSSimon Schubert /* Don't allow just "maintenance cplus".  */
14195796c8dcSSimon Schubert 
14205796c8dcSSimon Schubert static  void
14215796c8dcSSimon Schubert maint_cplus_command (char *arg, int from_tty)
14225796c8dcSSimon Schubert {
1423c50c785cSJohn Marino   printf_unfiltered (_("\"maintenance cplus\" must be followed "
1424c50c785cSJohn Marino 		       "by the name of a command.\n"));
1425c50c785cSJohn Marino   help_list (maint_cplus_cmd_list,
1426c50c785cSJohn Marino 	     "maintenance cplus ",
1427c50c785cSJohn Marino 	     -1, gdb_stdout);
14285796c8dcSSimon Schubert }
14295796c8dcSSimon Schubert 
14305796c8dcSSimon Schubert /* This is a front end for cp_find_first_component, for unit testing.
14315796c8dcSSimon Schubert    Be careful when using it: see the NOTE above
14325796c8dcSSimon Schubert    cp_find_first_component.  */
14335796c8dcSSimon Schubert 
14345796c8dcSSimon Schubert static void
14355796c8dcSSimon Schubert first_component_command (char *arg, int from_tty)
14365796c8dcSSimon Schubert {
14375796c8dcSSimon Schubert   int len;
14385796c8dcSSimon Schubert   char *prefix;
14395796c8dcSSimon Schubert 
14405796c8dcSSimon Schubert   if (!arg)
14415796c8dcSSimon Schubert     return;
14425796c8dcSSimon Schubert 
14435796c8dcSSimon Schubert   len = cp_find_first_component (arg);
14445796c8dcSSimon Schubert   prefix = alloca (len + 1);
14455796c8dcSSimon Schubert 
14465796c8dcSSimon Schubert   memcpy (prefix, arg, len);
14475796c8dcSSimon Schubert   prefix[len] = '\0';
14485796c8dcSSimon Schubert 
14495796c8dcSSimon Schubert   printf_unfiltered ("%s\n", prefix);
14505796c8dcSSimon Schubert }
14515796c8dcSSimon Schubert 
14525796c8dcSSimon Schubert extern initialize_file_ftype _initialize_cp_support; /* -Wmissing-prototypes */
14535796c8dcSSimon Schubert 
1454cf7f2e2dSJohn Marino #define SKIP_SPACE(P)				\
1455cf7f2e2dSJohn Marino   do						\
1456cf7f2e2dSJohn Marino   {						\
1457cf7f2e2dSJohn Marino     while (*(P) == ' ' || *(P) == '\t')		\
1458cf7f2e2dSJohn Marino       ++(P);					\
1459cf7f2e2dSJohn Marino   }						\
1460cf7f2e2dSJohn Marino   while (0)
1461cf7f2e2dSJohn Marino 
1462cf7f2e2dSJohn Marino /* Returns the length of the operator name or 0 if INPUT does not
1463c50c785cSJohn Marino    point to a valid C++ operator.  INPUT should start with
1464c50c785cSJohn Marino    "operator".  */
1465cf7f2e2dSJohn Marino int
1466cf7f2e2dSJohn Marino cp_validate_operator (const char *input)
1467cf7f2e2dSJohn Marino {
1468cf7f2e2dSJohn Marino   int i;
1469cf7f2e2dSJohn Marino   char *copy;
1470cf7f2e2dSJohn Marino   const char *p;
1471cf7f2e2dSJohn Marino   struct expression *expr;
1472cf7f2e2dSJohn Marino   struct value *val;
1473cf7f2e2dSJohn Marino   struct gdb_exception except;
1474cf7f2e2dSJohn Marino 
1475cf7f2e2dSJohn Marino   p = input;
1476cf7f2e2dSJohn Marino 
1477cf7f2e2dSJohn Marino   if (strncmp (p, "operator", 8) == 0)
1478cf7f2e2dSJohn Marino     {
1479cf7f2e2dSJohn Marino       int valid = 0;
1480cf7f2e2dSJohn Marino 
1481cf7f2e2dSJohn Marino       p += 8;
1482cf7f2e2dSJohn Marino       SKIP_SPACE (p);
1483c50c785cSJohn Marino       for (i = 0;
1484c50c785cSJohn Marino 	   i < sizeof (operator_tokens) / sizeof (operator_tokens[0]);
1485cf7f2e2dSJohn Marino 	   ++i)
1486cf7f2e2dSJohn Marino 	{
1487cf7f2e2dSJohn Marino 	  int length = strlen (operator_tokens[i]);
1488cf7f2e2dSJohn Marino 
1489c50c785cSJohn Marino 	  /* By using strncmp here, we MUST have operator_tokens
1490c50c785cSJohn Marino 	     ordered!  See additional notes where operator_tokens is
1491c50c785cSJohn Marino 	     defined above.  */
1492cf7f2e2dSJohn Marino 	  if (strncmp (p, operator_tokens[i], length) == 0)
1493cf7f2e2dSJohn Marino 	    {
1494cf7f2e2dSJohn Marino 	      const char *op = p;
1495cf7f2e2dSJohn Marino 
1496cf7f2e2dSJohn Marino 	      valid = 1;
1497cf7f2e2dSJohn Marino 	      p += length;
1498cf7f2e2dSJohn Marino 
1499cf7f2e2dSJohn Marino 	      if (strncmp (op, "new", 3) == 0
1500cf7f2e2dSJohn Marino 		  || strncmp (op, "delete", 6) == 0)
1501cf7f2e2dSJohn Marino 		{
1502cf7f2e2dSJohn Marino 
1503c50c785cSJohn Marino 		  /* Special case: new[] and delete[].  We must be
1504c50c785cSJohn Marino 		     careful to swallow whitespace before/in "[]".  */
1505cf7f2e2dSJohn Marino 		  SKIP_SPACE (p);
1506cf7f2e2dSJohn Marino 
1507cf7f2e2dSJohn Marino 		  if (*p == '[')
1508cf7f2e2dSJohn Marino 		    {
1509cf7f2e2dSJohn Marino 		      ++p;
1510cf7f2e2dSJohn Marino 		      SKIP_SPACE (p);
1511cf7f2e2dSJohn Marino 		      if (*p == ']')
1512cf7f2e2dSJohn Marino 			++p;
1513cf7f2e2dSJohn Marino 		      else
1514cf7f2e2dSJohn Marino 			valid = 0;
1515cf7f2e2dSJohn Marino 		    }
1516cf7f2e2dSJohn Marino 		}
1517cf7f2e2dSJohn Marino 
1518cf7f2e2dSJohn Marino 	      if (valid)
1519cf7f2e2dSJohn Marino 		return (p - input);
1520cf7f2e2dSJohn Marino 	    }
1521cf7f2e2dSJohn Marino 	}
1522cf7f2e2dSJohn Marino 
1523cf7f2e2dSJohn Marino       /* Check input for a conversion operator.  */
1524cf7f2e2dSJohn Marino 
1525c50c785cSJohn Marino       /* Skip past base typename.  */
1526cf7f2e2dSJohn Marino       while (*p != '*' && *p != '&' && *p != 0 && *p != ' ')
1527cf7f2e2dSJohn Marino 	++p;
1528cf7f2e2dSJohn Marino       SKIP_SPACE (p);
1529cf7f2e2dSJohn Marino 
1530c50c785cSJohn Marino       /* Add modifiers '*' / '&'.  */
1531cf7f2e2dSJohn Marino       while (*p == '*' || *p == '&')
1532cf7f2e2dSJohn Marino 	{
1533cf7f2e2dSJohn Marino 	  ++p;
1534cf7f2e2dSJohn Marino 	  SKIP_SPACE (p);
1535cf7f2e2dSJohn Marino 	}
1536cf7f2e2dSJohn Marino 
1537cf7f2e2dSJohn Marino       /* Check for valid type.  [Remember: input starts with
1538cf7f2e2dSJohn Marino 	 "operator".]  */
1539cf7f2e2dSJohn Marino       copy = savestring (input + 8, p - input - 8);
1540cf7f2e2dSJohn Marino       expr = NULL;
1541cf7f2e2dSJohn Marino       val = NULL;
1542cf7f2e2dSJohn Marino       TRY_CATCH (except, RETURN_MASK_ALL)
1543cf7f2e2dSJohn Marino 	{
1544cf7f2e2dSJohn Marino 	  expr = parse_expression (copy);
1545cf7f2e2dSJohn Marino 	  val = evaluate_type (expr);
1546cf7f2e2dSJohn Marino 	}
1547cf7f2e2dSJohn Marino 
1548cf7f2e2dSJohn Marino       xfree (copy);
1549cf7f2e2dSJohn Marino       if (expr)
1550cf7f2e2dSJohn Marino 	xfree (expr);
1551cf7f2e2dSJohn Marino 
1552cf7f2e2dSJohn Marino       if (val != NULL && value_type (val) != NULL)
1553cf7f2e2dSJohn Marino 	return (p - input);
1554cf7f2e2dSJohn Marino     }
1555cf7f2e2dSJohn Marino 
1556cf7f2e2dSJohn Marino   return 0;
1557cf7f2e2dSJohn Marino }
1558cf7f2e2dSJohn Marino 
15595796c8dcSSimon Schubert void
15605796c8dcSSimon Schubert _initialize_cp_support (void)
15615796c8dcSSimon Schubert {
1562c50c785cSJohn Marino   add_prefix_cmd ("cplus", class_maintenance,
1563c50c785cSJohn Marino 		  maint_cplus_command,
1564c50c785cSJohn Marino 		  _("C++ maintenance commands."),
1565c50c785cSJohn Marino 		  &maint_cplus_cmd_list,
1566c50c785cSJohn Marino 		  "maintenance cplus ",
1567c50c785cSJohn Marino 		  0, &maintenancelist);
1568c50c785cSJohn Marino   add_alias_cmd ("cp", "cplus",
1569c50c785cSJohn Marino 		 class_maintenance, 1,
1570c50c785cSJohn Marino 		 &maintenancelist);
15715796c8dcSSimon Schubert 
1572c50c785cSJohn Marino   add_cmd ("first_component",
1573c50c785cSJohn Marino 	   class_maintenance,
1574c50c785cSJohn Marino 	   first_component_command,
15755796c8dcSSimon Schubert 	   _("Print the first class/namespace component of NAME."),
15765796c8dcSSimon Schubert 	   &maint_cplus_cmd_list);
15775796c8dcSSimon Schubert }
1578