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