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