4902e50e3c6496a00cdb2db7cd6317c9d5f7f216
[platform/upstream/binutils.git] / gdb / cp-support.c
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 (ret == NULL)
532     {
533       warning (_("internal error: string \"%s\" failed to be canonicalized"),
534                string);
535       return NULL;
536     }
537
538   if (strcmp (string, ret) == 0)
539     {
540       xfree (ret);
541       return NULL;
542     }
543
544   return ret;
545 }
546
547 /* Convert a mangled name to a demangle_component tree.  *MEMORY is
548    set to the block of used memory that should be freed when finished
549    with the tree.  DEMANGLED_P is set to the char * that should be
550    freed when finished with the tree, or NULL if none was needed.
551    OPTIONS will be passed to the demangler.  */
552
553 static struct demangle_parse_info *
554 mangled_name_to_comp (const char *mangled_name, int options,
555                       void **memory, char **demangled_p)
556 {
557   char *demangled_name;
558   struct demangle_parse_info *info;
559
560   /* If it looks like a v3 mangled name, then try to go directly
561      to trees.  */
562   if (mangled_name[0] == '_' && mangled_name[1] == 'Z')
563     {
564       struct demangle_component *ret;
565
566       ret = cplus_demangle_v3_components (mangled_name,
567                                           options, memory);
568       if (ret)
569         {
570           info = cp_new_demangle_parse_info ();
571           info->tree = ret;
572           *demangled_p = NULL;
573           return info;
574         }
575     }
576
577   /* If it doesn't, or if that failed, then try to demangle the
578      name.  */
579   demangled_name = cplus_demangle (mangled_name, options);
580   if (demangled_name == NULL)
581    return NULL;
582   
583   /* If we could demangle the name, parse it to build the component
584      tree.  */
585   info = cp_demangled_name_to_comp (demangled_name, NULL);
586
587   if (info == NULL)
588     {
589       xfree (demangled_name);
590       return NULL;
591     }
592
593   *demangled_p = demangled_name;
594   return info;
595 }
596
597 /* Return the name of the class containing method PHYSNAME.  */
598
599 char *
600 cp_class_name_from_physname (const char *physname)
601 {
602   void *storage = NULL;
603   char *demangled_name = NULL, *ret;
604   struct demangle_component *ret_comp, *prev_comp, *cur_comp;
605   struct demangle_parse_info *info;
606   int done;
607
608   info = mangled_name_to_comp (physname, DMGL_ANSI,
609                                &storage, &demangled_name);
610   if (info == NULL)
611     return NULL;
612
613   done = 0;
614   ret_comp = info->tree;
615
616   /* First strip off any qualifiers, if we have a function or
617      method.  */
618   while (!done)
619     switch (ret_comp->type)
620       {
621       case DEMANGLE_COMPONENT_CONST:
622       case DEMANGLE_COMPONENT_RESTRICT:
623       case DEMANGLE_COMPONENT_VOLATILE:
624       case DEMANGLE_COMPONENT_CONST_THIS:
625       case DEMANGLE_COMPONENT_RESTRICT_THIS:
626       case DEMANGLE_COMPONENT_VOLATILE_THIS:
627       case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
628         ret_comp = d_left (ret_comp);
629         break;
630       default:
631         done = 1;
632         break;
633       }
634
635   /* If what we have now is a function, discard the argument list.  */
636   if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
637     ret_comp = d_left (ret_comp);
638
639   /* If what we have now is a template, strip off the template
640      arguments.  The left subtree may be a qualified name.  */
641   if (ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE)
642     ret_comp = d_left (ret_comp);
643
644   /* What we have now should be a name, possibly qualified.
645      Additional qualifiers could live in the left subtree or the right
646      subtree.  Find the last piece.  */
647   done = 0;
648   prev_comp = NULL;
649   cur_comp = ret_comp;
650   while (!done)
651     switch (cur_comp->type)
652       {
653       case DEMANGLE_COMPONENT_QUAL_NAME:
654       case DEMANGLE_COMPONENT_LOCAL_NAME:
655         prev_comp = cur_comp;
656         cur_comp = d_right (cur_comp);
657         break;
658       case DEMANGLE_COMPONENT_TEMPLATE:
659       case DEMANGLE_COMPONENT_NAME:
660       case DEMANGLE_COMPONENT_CTOR:
661       case DEMANGLE_COMPONENT_DTOR:
662       case DEMANGLE_COMPONENT_OPERATOR:
663       case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
664         done = 1;
665         break;
666       default:
667         done = 1;
668         cur_comp = NULL;
669         break;
670       }
671
672   ret = NULL;
673   if (cur_comp != NULL && prev_comp != NULL)
674     {
675       /* We want to discard the rightmost child of PREV_COMP.  */
676       *prev_comp = *d_left (prev_comp);
677       /* The ten is completely arbitrary; we don't have a good
678          estimate.  */
679       ret = cp_comp_to_string (ret_comp, 10);
680     }
681
682   xfree (storage);
683   xfree (demangled_name);
684   cp_demangled_name_parse_free (info);
685   return ret;
686 }
687
688 /* Return the child of COMP which is the basename of a method,
689    variable, et cetera.  All scope qualifiers are discarded, but
690    template arguments will be included.  The component tree may be
691    modified.  */
692
693 static struct demangle_component *
694 unqualified_name_from_comp (struct demangle_component *comp)
695 {
696   struct demangle_component *ret_comp = comp, *last_template;
697   int done;
698
699   done = 0;
700   last_template = NULL;
701   while (!done)
702     switch (ret_comp->type)
703       {
704       case DEMANGLE_COMPONENT_QUAL_NAME:
705       case DEMANGLE_COMPONENT_LOCAL_NAME:
706         ret_comp = d_right (ret_comp);
707         break;
708       case DEMANGLE_COMPONENT_TYPED_NAME:
709         ret_comp = d_left (ret_comp);
710         break;
711       case DEMANGLE_COMPONENT_TEMPLATE:
712         gdb_assert (last_template == NULL);
713         last_template = ret_comp;
714         ret_comp = d_left (ret_comp);
715         break;
716       case DEMANGLE_COMPONENT_CONST:
717       case DEMANGLE_COMPONENT_RESTRICT:
718       case DEMANGLE_COMPONENT_VOLATILE:
719       case DEMANGLE_COMPONENT_CONST_THIS:
720       case DEMANGLE_COMPONENT_RESTRICT_THIS:
721       case DEMANGLE_COMPONENT_VOLATILE_THIS:
722       case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
723         ret_comp = d_left (ret_comp);
724         break;
725       case DEMANGLE_COMPONENT_NAME:
726       case DEMANGLE_COMPONENT_CTOR:
727       case DEMANGLE_COMPONENT_DTOR:
728       case DEMANGLE_COMPONENT_OPERATOR:
729       case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
730         done = 1;
731         break;
732       default:
733         return NULL;
734         break;
735       }
736
737   if (last_template)
738     {
739       d_left (last_template) = ret_comp;
740       return last_template;
741     }
742
743   return ret_comp;
744 }
745
746 /* Return the name of the method whose linkage name is PHYSNAME.  */
747
748 char *
749 method_name_from_physname (const char *physname)
750 {
751   void *storage = NULL;
752   char *demangled_name = NULL, *ret;
753   struct demangle_component *ret_comp;
754   struct demangle_parse_info *info;
755
756   info = mangled_name_to_comp (physname, DMGL_ANSI,
757                                &storage, &demangled_name);
758   if (info == NULL)
759     return NULL;
760
761   ret_comp = unqualified_name_from_comp (info->tree);
762
763   ret = NULL;
764   if (ret_comp != NULL)
765     /* The ten is completely arbitrary; we don't have a good
766        estimate.  */
767     ret = cp_comp_to_string (ret_comp, 10);
768
769   xfree (storage);
770   xfree (demangled_name);
771   cp_demangled_name_parse_free (info);
772   return ret;
773 }
774
775 /* If FULL_NAME is the demangled name of a C++ function (including an
776    arg list, possibly including namespace/class qualifications),
777    return a new string containing only the function name (without the
778    arg list/class qualifications).  Otherwise, return NULL.  The
779    caller is responsible for freeing the memory in question.  */
780
781 char *
782 cp_func_name (const char *full_name)
783 {
784   char *ret;
785   struct demangle_component *ret_comp;
786   struct demangle_parse_info *info;
787
788   info = cp_demangled_name_to_comp (full_name, NULL);
789   if (!info)
790     return NULL;
791
792   ret_comp = unqualified_name_from_comp (info->tree);
793
794   ret = NULL;
795   if (ret_comp != NULL)
796     ret = cp_comp_to_string (ret_comp, 10);
797
798   cp_demangled_name_parse_free (info);
799   return ret;
800 }
801
802 /* DEMANGLED_NAME is the name of a function, including parameters and
803    (optionally) a return type.  Return the name of the function without
804    parameters or return type, or NULL if we can not parse the name.  */
805
806 char *
807 cp_remove_params (const char *demangled_name)
808 {
809   int done = 0;
810   struct demangle_component *ret_comp;
811   struct demangle_parse_info *info;
812   char *ret = NULL;
813
814   if (demangled_name == NULL)
815     return NULL;
816
817   info = cp_demangled_name_to_comp (demangled_name, NULL);
818   if (info == NULL)
819     return NULL;
820
821   /* First strip off any qualifiers, if we have a function or method.  */
822   ret_comp = info->tree;
823   while (!done)
824     switch (ret_comp->type)
825       {
826       case DEMANGLE_COMPONENT_CONST:
827       case DEMANGLE_COMPONENT_RESTRICT:
828       case DEMANGLE_COMPONENT_VOLATILE:
829       case DEMANGLE_COMPONENT_CONST_THIS:
830       case DEMANGLE_COMPONENT_RESTRICT_THIS:
831       case DEMANGLE_COMPONENT_VOLATILE_THIS:
832       case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
833         ret_comp = d_left (ret_comp);
834         break;
835       default:
836         done = 1;
837         break;
838       }
839
840   /* What we have now should be a function.  Return its name.  */
841   if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
842     ret = cp_comp_to_string (d_left (ret_comp), 10);
843
844   cp_demangled_name_parse_free (info);
845   return ret;
846 }
847
848 /* Here are some random pieces of trivia to keep in mind while trying
849    to take apart demangled names:
850
851    - Names can contain function arguments or templates, so the process
852      has to be, to some extent recursive: maybe keep track of your
853      depth based on encountering <> and ().
854
855    - Parentheses don't just have to happen at the end of a name: they
856      can occur even if the name in question isn't a function, because
857      a template argument might be a type that's a function.
858
859    - Conversely, even if you're trying to deal with a function, its
860      demangled name might not end with ')': it could be a const or
861      volatile class method, in which case it ends with "const" or
862      "volatile".
863
864    - Parentheses are also used in anonymous namespaces: a variable
865      'foo' in an anonymous namespace gets demangled as "(anonymous
866      namespace)::foo".
867
868    - And operator names can contain parentheses or angle brackets.  */
869
870 /* FIXME: carlton/2003-03-13: We have several functions here with
871    overlapping functionality; can we combine them?  Also, do they
872    handle all the above considerations correctly?  */
873
874
875 /* This returns the length of first component of NAME, which should be
876    the demangled name of a C++ variable/function/method/etc.
877    Specifically, it returns the index of the first colon forming the
878    boundary of the first component: so, given 'A::foo' or 'A::B::foo'
879    it returns the 1, and given 'foo', it returns 0.  */
880
881 /* The character in NAME indexed by the return value is guaranteed to
882    always be either ':' or '\0'.  */
883
884 /* NOTE: carlton/2003-03-13: This function is currently only intended
885    for internal use: it's probably not entirely safe when called on
886    user-generated input, because some of the 'index += 2' lines in
887    cp_find_first_component_aux might go past the end of malformed
888    input.  */
889
890 unsigned int
891 cp_find_first_component (const char *name)
892 {
893   return cp_find_first_component_aux (name, 0);
894 }
895
896 /* Helper function for cp_find_first_component.  Like that function,
897    it returns the length of the first component of NAME, but to make
898    the recursion easier, it also stops if it reaches an unexpected ')'
899    or '>' if the value of PERMISSIVE is nonzero.  */
900
901 /* Let's optimize away calls to strlen("operator").  */
902
903 #define LENGTH_OF_OPERATOR 8
904
905 static unsigned int
906 cp_find_first_component_aux (const char *name, int permissive)
907 {
908   unsigned int index = 0;
909   /* Operator names can show up in unexpected places.  Since these can
910      contain parentheses or angle brackets, they can screw up the
911      recursion.  But not every string 'operator' is part of an
912      operater name: e.g. you could have a variable 'cooperator'.  So
913      this variable tells us whether or not we should treat the string
914      'operator' as starting an operator.  */
915   int operator_possible = 1;
916
917   for (;; ++index)
918     {
919       switch (name[index])
920         {
921         case '<':
922           /* Template; eat it up.  The calls to cp_first_component
923              should only return (I hope!) when they reach the '>'
924              terminating the component or a '::' between two
925              components.  (Hence the '+ 2'.)  */
926           index += 1;
927           for (index += cp_find_first_component_aux (name + index, 1);
928                name[index] != '>';
929                index += cp_find_first_component_aux (name + index, 1))
930             {
931               if (name[index] != ':')
932                 {
933                   demangled_name_complaint (name);
934                   return strlen (name);
935                 }
936               index += 2;
937             }
938           operator_possible = 1;
939           break;
940         case '(':
941           /* Similar comment as to '<'.  */
942           index += 1;
943           for (index += cp_find_first_component_aux (name + index, 1);
944                name[index] != ')';
945                index += cp_find_first_component_aux (name + index, 1))
946             {
947               if (name[index] != ':')
948                 {
949                   demangled_name_complaint (name);
950                   return strlen (name);
951                 }
952               index += 2;
953             }
954           operator_possible = 1;
955           break;
956         case '>':
957         case ')':
958           if (permissive)
959             return index;
960           else
961             {
962               demangled_name_complaint (name);
963               return strlen (name);
964             }
965         case '\0':
966         case ':':
967           return index;
968         case 'o':
969           /* Operator names can screw up the recursion.  */
970           if (operator_possible
971               && strncmp (name + index, "operator",
972                           LENGTH_OF_OPERATOR) == 0)
973             {
974               index += LENGTH_OF_OPERATOR;
975               while (ISSPACE(name[index]))
976                 ++index;
977               switch (name[index])
978                 {
979                   /* Skip over one less than the appropriate number of
980                      characters: the for loop will skip over the last
981                      one.  */
982                 case '<':
983                   if (name[index + 1] == '<')
984                     index += 1;
985                   else
986                     index += 0;
987                   break;
988                 case '>':
989                 case '-':
990                   if (name[index + 1] == '>')
991                     index += 1;
992                   else
993                     index += 0;
994                   break;
995                 case '(':
996                   index += 1;
997                   break;
998                 default:
999                   index += 0;
1000                   break;
1001                 }
1002             }
1003           operator_possible = 0;
1004           break;
1005         case ' ':
1006         case ',':
1007         case '.':
1008         case '&':
1009         case '*':
1010           /* NOTE: carlton/2003-04-18: I'm not sure what the precise
1011              set of relevant characters are here: it's necessary to
1012              include any character that can show up before 'operator'
1013              in a demangled name, and it's safe to include any
1014              character that can't be part of an identifier's name.  */
1015           operator_possible = 1;
1016           break;
1017         default:
1018           operator_possible = 0;
1019           break;
1020         }
1021     }
1022 }
1023
1024 /* Complain about a demangled name that we don't know how to parse.
1025    NAME is the demangled name in question.  */
1026
1027 static void
1028 demangled_name_complaint (const char *name)
1029 {
1030   complaint (&symfile_complaints,
1031              "unexpected demangled name '%s'", name);
1032 }
1033
1034 /* If NAME is the fully-qualified name of a C++
1035    function/variable/method/etc., this returns the length of its
1036    entire prefix: all of the namespaces and classes that make up its
1037    name.  Given 'A::foo', it returns 1, given 'A::B::foo', it returns
1038    4, given 'foo', it returns 0.  */
1039
1040 unsigned int
1041 cp_entire_prefix_len (const char *name)
1042 {
1043   unsigned int current_len = cp_find_first_component (name);
1044   unsigned int previous_len = 0;
1045
1046   while (name[current_len] != '\0')
1047     {
1048       gdb_assert (name[current_len] == ':');
1049       previous_len = current_len;
1050       /* Skip the '::'.  */
1051       current_len += 2;
1052       current_len += cp_find_first_component (name + current_len);
1053     }
1054
1055   return previous_len;
1056 }
1057
1058 /* Overload resolution functions.  */
1059
1060 /* Test to see if SYM is a symbol that we haven't seen corresponding
1061    to a function named OLOAD_NAME.  If so, add it to the current
1062    completion list.  */
1063
1064 static void
1065 overload_list_add_symbol (struct symbol *sym,
1066                           const char *oload_name)
1067 {
1068   int newsize;
1069   int i;
1070   char *sym_name;
1071
1072   /* If there is no type information, we can't do anything, so
1073      skip.  */
1074   if (SYMBOL_TYPE (sym) == NULL)
1075     return;
1076
1077   /* skip any symbols that we've already considered.  */
1078   for (i = 0; i < sym_return_val_index; ++i)
1079     if (strcmp (SYMBOL_LINKAGE_NAME (sym),
1080                 SYMBOL_LINKAGE_NAME (sym_return_val[i])) == 0)
1081       return;
1082
1083   /* Get the demangled name without parameters */
1084   sym_name = cp_remove_params (SYMBOL_NATURAL_NAME (sym));
1085   if (!sym_name)
1086     return;
1087
1088   /* skip symbols that cannot match */
1089   if (strcmp (sym_name, oload_name) != 0)
1090     {
1091       xfree (sym_name);
1092       return;
1093     }
1094
1095   xfree (sym_name);
1096
1097   /* We have a match for an overload instance, so add SYM to the
1098      current list of overload instances */
1099   if (sym_return_val_index + 3 > sym_return_val_size)
1100     {
1101       newsize = (sym_return_val_size *= 2) * sizeof (struct symbol *);
1102       sym_return_val = (struct symbol **)
1103         xrealloc ((char *) sym_return_val, newsize);
1104     }
1105   sym_return_val[sym_return_val_index++] = sym;
1106   sym_return_val[sym_return_val_index] = NULL;
1107 }
1108
1109 /* Return a null-terminated list of pointers to function symbols that
1110    are named FUNC_NAME and are visible within NAMESPACE.  */
1111
1112 struct symbol **
1113 make_symbol_overload_list (const char *func_name,
1114                            const char *namespace)
1115 {
1116   struct cleanup *old_cleanups;
1117   const char *name;
1118
1119   sym_return_val_size = 100;
1120   sym_return_val_index = 0;
1121   sym_return_val = xmalloc ((sym_return_val_size + 1) *
1122                             sizeof (struct symbol *));
1123   sym_return_val[0] = NULL;
1124
1125   old_cleanups = make_cleanup (xfree, sym_return_val);
1126
1127   make_symbol_overload_list_using (func_name, namespace);
1128
1129   if (namespace[0] == '\0')
1130     name = func_name;
1131   else
1132     {
1133       char *concatenated_name
1134         = alloca (strlen (namespace) + 2 + strlen (func_name) + 1);
1135       strcpy (concatenated_name, namespace);
1136       strcat (concatenated_name, "::");
1137       strcat (concatenated_name, func_name);
1138       name = concatenated_name;
1139     }
1140
1141   make_symbol_overload_list_qualified (name);
1142
1143   discard_cleanups (old_cleanups);
1144
1145   return sym_return_val;
1146 }
1147
1148 /* Add all symbols with a name matching NAME in BLOCK to the overload
1149    list.  */
1150
1151 static void
1152 make_symbol_overload_list_block (const char *name,
1153                                  const struct block *block)
1154 {
1155   struct dict_iterator iter;
1156   struct symbol *sym;
1157
1158   const struct dictionary *dict = BLOCK_DICT (block);
1159
1160   for (sym = dict_iter_name_first (dict, name, &iter);
1161        sym != NULL;
1162        sym = dict_iter_name_next (name, &iter))
1163     overload_list_add_symbol (sym, name);
1164 }
1165
1166 /* Adds the function FUNC_NAME from NAMESPACE to the overload set.  */
1167
1168 static void
1169 make_symbol_overload_list_namespace (const char *func_name,
1170                                      const char *namespace)
1171 {
1172   const char *name;
1173   const struct block *block = NULL;
1174
1175   if (namespace[0] == '\0')
1176     name = func_name;
1177   else
1178     {
1179       char *concatenated_name
1180         = alloca (strlen (namespace) + 2 + strlen (func_name) + 1);
1181
1182       strcpy (concatenated_name, namespace);
1183       strcat (concatenated_name, "::");
1184       strcat (concatenated_name, func_name);
1185       name = concatenated_name;
1186     }
1187
1188   /* Look in the static block.  */
1189   block = block_static_block (get_selected_block (0));
1190   if (block)
1191     make_symbol_overload_list_block (name, block);
1192
1193   /* Look in the global block.  */
1194   block = block_global_block (block);
1195   if (block)
1196     make_symbol_overload_list_block (name, block);
1197
1198 }
1199
1200 /* Search the namespace of the given type and namespace of and public
1201    base types.  */
1202
1203 static void
1204 make_symbol_overload_list_adl_namespace (struct type *type,
1205                                          const char *func_name)
1206 {
1207   char *namespace;
1208   char *type_name;
1209   int i, prefix_len;
1210
1211   while (TYPE_CODE (type) == TYPE_CODE_PTR
1212          || TYPE_CODE (type) == TYPE_CODE_REF
1213          || TYPE_CODE (type) == TYPE_CODE_ARRAY
1214          || TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1215     {
1216       if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1217         type = check_typedef(type);
1218       else
1219         type = TYPE_TARGET_TYPE (type);
1220     }
1221
1222   type_name = TYPE_NAME (type);
1223
1224   if (type_name == NULL)
1225     return;
1226
1227   prefix_len = cp_entire_prefix_len (type_name);
1228
1229   if (prefix_len != 0)
1230     {
1231       namespace = alloca (prefix_len + 1);
1232       strncpy (namespace, type_name, prefix_len);
1233       namespace[prefix_len] = '\0';
1234
1235       make_symbol_overload_list_namespace (func_name, namespace);
1236     }
1237
1238   /* Check public base type */
1239   if (TYPE_CODE (type) == TYPE_CODE_CLASS)
1240     for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
1241       {
1242         if (BASETYPE_VIA_PUBLIC (type, i))
1243           make_symbol_overload_list_adl_namespace (TYPE_BASECLASS (type,
1244                                                                    i),
1245                                                    func_name);
1246       }
1247 }
1248
1249 /* Adds the overload list overload candidates for FUNC_NAME found
1250    through argument dependent lookup.  */
1251
1252 struct symbol **
1253 make_symbol_overload_list_adl (struct type **arg_types, int nargs,
1254                                const char *func_name)
1255 {
1256   int i;
1257
1258   gdb_assert (sym_return_val_size != -1);
1259
1260   for (i = 1; i <= nargs; i++)
1261     make_symbol_overload_list_adl_namespace (arg_types[i - 1],
1262                                              func_name);
1263
1264   return sym_return_val;
1265 }
1266
1267 /* Used for cleanups to reset the "searched" flag in case of an
1268    error.  */
1269
1270 static void
1271 reset_directive_searched (void *data)
1272 {
1273   struct using_direct *direct = data;
1274   direct->searched = 0;
1275 }
1276
1277 /* This applies the using directives to add namespaces to search in,
1278    and then searches for overloads in all of those namespaces.  It
1279    adds the symbols found to sym_return_val.  Arguments are as in
1280    make_symbol_overload_list.  */
1281
1282 static void
1283 make_symbol_overload_list_using (const char *func_name,
1284                                  const char *namespace)
1285 {
1286   struct using_direct *current;
1287   const struct block *block;
1288
1289   /* First, go through the using directives.  If any of them apply,
1290      look in the appropriate namespaces for new functions to match
1291      on.  */
1292
1293   for (block = get_selected_block (0);
1294        block != NULL;
1295        block = BLOCK_SUPERBLOCK (block))
1296     for (current = block_using (block);
1297         current != NULL;
1298         current = current->next)
1299       {
1300         /* Prevent recursive calls.  */
1301         if (current->searched)
1302           continue;
1303
1304         /* If this is a namespace alias or imported declaration ignore
1305            it.  */
1306         if (current->alias != NULL || current->declaration != NULL)
1307           continue;
1308
1309         if (strcmp (namespace, current->import_dest) == 0)
1310           {
1311             /* Mark this import as searched so that the recursive call
1312                does not search it again.  */
1313             struct cleanup *old_chain;
1314             current->searched = 1;
1315             old_chain = make_cleanup (reset_directive_searched,
1316                                       current);
1317
1318             make_symbol_overload_list_using (func_name,
1319                                              current->import_src);
1320
1321             current->searched = 0;
1322             discard_cleanups (old_chain);
1323           }
1324       }
1325
1326   /* Now, add names for this namespace.  */
1327   make_symbol_overload_list_namespace (func_name, namespace);
1328 }
1329
1330 /* This does the bulk of the work of finding overloaded symbols.
1331    FUNC_NAME is the name of the overloaded function we're looking for
1332    (possibly including namespace info).  */
1333
1334 static void
1335 make_symbol_overload_list_qualified (const char *func_name)
1336 {
1337   struct symbol *sym;
1338   struct symtab *s;
1339   struct objfile *objfile;
1340   const struct block *b, *surrounding_static_block = 0;
1341   struct dict_iterator iter;
1342   const struct dictionary *dict;
1343
1344   /* Look through the partial symtabs for all symbols which begin by
1345      matching FUNC_NAME.  Make sure we read that symbol table in.  */
1346
1347   ALL_OBJFILES (objfile)
1348   {
1349     if (objfile->sf)
1350       objfile->sf->qf->expand_symtabs_for_function (objfile, func_name);
1351   }
1352
1353   /* Search upwards from currently selected frame (so that we can
1354      complete on local vars.  */
1355
1356   for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
1357     make_symbol_overload_list_block (func_name, b);
1358
1359   surrounding_static_block = block_static_block (get_selected_block (0));
1360
1361   /* Go through the symtabs and check the externs and statics for
1362      symbols which match.  */
1363
1364   ALL_PRIMARY_SYMTABS (objfile, s)
1365   {
1366     QUIT;
1367     b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
1368     make_symbol_overload_list_block (func_name, b);
1369   }
1370
1371   ALL_PRIMARY_SYMTABS (objfile, s)
1372   {
1373     QUIT;
1374     b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
1375     /* Don't do this block twice.  */
1376     if (b == surrounding_static_block)
1377       continue;
1378     make_symbol_overload_list_block (func_name, b);
1379   }
1380 }
1381
1382 /* Lookup the rtti type for a class name.  */
1383
1384 struct type *
1385 cp_lookup_rtti_type (const char *name, struct block *block)
1386 {
1387   struct symbol * rtti_sym;
1388   struct type * rtti_type;
1389
1390   rtti_sym = lookup_symbol (name, block, STRUCT_DOMAIN, NULL);
1391
1392   if (rtti_sym == NULL)
1393     {
1394       warning (_("RTTI symbol not found for class '%s'"), name);
1395       return NULL;
1396     }
1397
1398   if (SYMBOL_CLASS (rtti_sym) != LOC_TYPEDEF)
1399     {
1400       warning (_("RTTI symbol for class '%s' is not a type"), name);
1401       return NULL;
1402     }
1403
1404   rtti_type = SYMBOL_TYPE (rtti_sym);
1405
1406   switch (TYPE_CODE (rtti_type))
1407     {
1408     case TYPE_CODE_CLASS:
1409       break;
1410     case TYPE_CODE_NAMESPACE:
1411       /* chastain/2003-11-26: the symbol tables often contain fake
1412          symbols for namespaces with the same name as the struct.
1413          This warning is an indication of a bug in the lookup order
1414          or a bug in the way that the symbol tables are populated.  */
1415       warning (_("RTTI symbol for class '%s' is a namespace"), name);
1416       return NULL;
1417     default:
1418       warning (_("RTTI symbol for class '%s' has bad type"), name);
1419       return NULL;
1420     }
1421
1422   return rtti_type;
1423 }
1424
1425 /* Don't allow just "maintenance cplus".  */
1426
1427 static  void
1428 maint_cplus_command (char *arg, int from_tty)
1429 {
1430   printf_unfiltered (_("\"maintenance cplus\" must be followed "
1431                        "by the name of a command.\n"));
1432   help_list (maint_cplus_cmd_list,
1433              "maintenance cplus ",
1434              -1, gdb_stdout);
1435 }
1436
1437 /* This is a front end for cp_find_first_component, for unit testing.
1438    Be careful when using it: see the NOTE above
1439    cp_find_first_component.  */
1440
1441 static void
1442 first_component_command (char *arg, int from_tty)
1443 {
1444   int len;  
1445   char *prefix; 
1446
1447   if (!arg)
1448     return;
1449
1450   len = cp_find_first_component (arg);
1451   prefix = alloca (len + 1);
1452
1453   memcpy (prefix, arg, len);
1454   prefix[len] = '\0';
1455
1456   printf_unfiltered ("%s\n", prefix);
1457 }
1458
1459 extern initialize_file_ftype _initialize_cp_support; /* -Wmissing-prototypes */
1460
1461 #define SKIP_SPACE(P)                           \
1462   do                                            \
1463   {                                             \
1464     while (*(P) == ' ' || *(P) == '\t')         \
1465       ++(P);                                    \
1466   }                                             \
1467   while (0)
1468
1469 /* Returns the length of the operator name or 0 if INPUT does not
1470    point to a valid C++ operator.  INPUT should start with
1471    "operator".  */
1472 int
1473 cp_validate_operator (const char *input)
1474 {
1475   int i;
1476   char *copy;
1477   const char *p;
1478   struct expression *expr;
1479   struct value *val;
1480   volatile struct gdb_exception except;
1481
1482   p = input;
1483
1484   if (strncmp (p, "operator", 8) == 0)
1485     {
1486       int valid = 0;
1487
1488       p += 8;
1489       SKIP_SPACE (p);
1490       for (i = 0;
1491            i < sizeof (operator_tokens) / sizeof (operator_tokens[0]);
1492            ++i)
1493         {
1494           int length = strlen (operator_tokens[i]);
1495
1496           /* By using strncmp here, we MUST have operator_tokens
1497              ordered!  See additional notes where operator_tokens is
1498              defined above.  */
1499           if (strncmp (p, operator_tokens[i], length) == 0)
1500             {
1501               const char *op = p;
1502
1503               valid = 1;
1504               p += length;
1505
1506               if (strncmp (op, "new", 3) == 0
1507                   || strncmp (op, "delete", 6) == 0)
1508                 {
1509
1510                   /* Special case: new[] and delete[].  We must be
1511                      careful to swallow whitespace before/in "[]".  */
1512                   SKIP_SPACE (p);
1513
1514                   if (*p == '[')
1515                     {
1516                       ++p;
1517                       SKIP_SPACE (p);
1518                       if (*p == ']')
1519                         ++p;
1520                       else
1521                         valid = 0;
1522                     }
1523                 }
1524
1525               if (valid)
1526                 return (p - input);
1527             }
1528         }
1529
1530       /* Check input for a conversion operator.  */
1531
1532       /* Skip past base typename.  */
1533       while (*p != '*' && *p != '&' && *p != 0 && *p != ' ')
1534         ++p;
1535       SKIP_SPACE (p);
1536
1537       /* Add modifiers '*' / '&'.  */
1538       while (*p == '*' || *p == '&')
1539         {
1540           ++p;
1541           SKIP_SPACE (p);
1542         }
1543
1544       /* Check for valid type.  [Remember: input starts with 
1545          "operator".]  */
1546       copy = savestring (input + 8, p - input - 8);
1547       expr = NULL;
1548       val = NULL;
1549       TRY_CATCH (except, RETURN_MASK_ALL)
1550         {
1551           expr = parse_expression (copy);
1552           val = evaluate_type (expr);
1553         }
1554
1555       xfree (copy);
1556       if (expr)
1557         xfree (expr);
1558
1559       if (val != NULL && value_type (val) != NULL)
1560         return (p - input);
1561     }
1562
1563   return 0;
1564 }
1565
1566 void
1567 _initialize_cp_support (void)
1568 {
1569   add_prefix_cmd ("cplus", class_maintenance,
1570                   maint_cplus_command,
1571                   _("C++ maintenance commands."),
1572                   &maint_cplus_cmd_list,
1573                   "maintenance cplus ",
1574                   0, &maintenancelist);
1575   add_alias_cmd ("cp", "cplus",
1576                  class_maintenance, 1,
1577                  &maintenancelist);
1578
1579   add_cmd ("first_component",
1580            class_maintenance,
1581            first_component_command,
1582            _("Print the first class/namespace component of NAME."),
1583            &maint_cplus_cmd_list);
1584 }