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