OSDN Git Service

* cp-support.c: Include "safe-ctype.h".
[pf3gnuchains/pf3gnuchains3x.git] / gdb / cp-support.c
1 /* Helper routines for C++ support in GDB.
2    Copyright (C) 2002, 2003, 2004, 2005, 2007 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
35 #include "safe-ctype.h"
36
37 #define d_left(dc) (dc)->u.s_binary.left
38 #define d_right(dc) (dc)->u.s_binary.right
39
40 /* Functions related to demangled name parsing.  */
41
42 static unsigned int cp_find_first_component_aux (const char *name,
43                                                  int permissive);
44
45 static void demangled_name_complaint (const char *name);
46
47 /* Functions/variables related to overload resolution.  */
48
49 static int sym_return_val_size;
50 static int sym_return_val_index;
51 static struct symbol **sym_return_val;
52
53 static void overload_list_add_symbol (struct symbol *sym,
54                                       const char *oload_name);
55
56 static void make_symbol_overload_list_using (const char *func_name,
57                                              const char *namespace);
58
59 static void make_symbol_overload_list_qualified (const char *func_name);
60
61 static void read_in_psymtabs (const char *oload_name);
62
63 /* The list of "maint cplus" commands.  */
64
65 struct cmd_list_element *maint_cplus_cmd_list = NULL;
66
67 /* The actual commands.  */
68
69 static void maint_cplus_command (char *arg, int from_tty);
70 static void first_component_command (char *arg, int from_tty);
71
72 /* Return 1 if STRING is clearly already in canonical form.  This
73    function is conservative; things which it does not recognize are
74    assumed to be non-canonical, and the parser will sort them out
75    afterwards.  This speeds up the critical path for alphanumeric
76    identifiers.  */
77
78 static int
79 cp_already_canonical (const char *string)
80 {
81   /* Identifier start character [a-zA-Z_].  */
82   if (!ISIDST (string[0]))
83     return 0;
84
85   /* These are the only two identifiers which canonicalize to other
86      than themselves or an error: unsigned -> unsigned int and
87      signed -> int.  */
88   if (string[0] == 'u' && strcmp (&string[1], "nsigned") == 0)
89     return 0;
90   else if (string[0] == 's' && strcmp (&string[1], "igned") == 0)
91     return 0;
92
93   /* Identifier character [a-zA-Z0-9_].  */
94   while (ISIDNUM (string[1]))
95     string++;
96
97   if (string[1] == '\0')
98     return 1;
99   else
100     return 0;
101 }
102
103 /* Parse STRING and convert it to canonical form.  If parsing fails,
104    or if STRING is already canonical, return NULL.  Otherwise return
105    the canonical form.  The return value is allocated via xmalloc.  */
106
107 char *
108 cp_canonicalize_string (const char *string)
109 {
110   struct demangle_component *ret_comp;
111   unsigned int estimated_len;
112   char *ret;
113
114   if (cp_already_canonical (string))
115     return NULL;
116
117   ret_comp = cp_demangled_name_to_comp (string, NULL);
118   if (ret_comp == NULL)
119     return NULL;
120
121   estimated_len = strlen (string) * 2;
122   ret = cp_comp_to_string (ret_comp, estimated_len);
123
124   if (strcmp (string, ret) == 0)
125     {
126       xfree (ret);
127       return NULL;
128     }
129
130   return ret;
131 }
132
133 /* Convert a mangled name to a demangle_component tree.  *MEMORY is set to the
134    block of used memory that should be freed when finished with the tree. 
135    DEMANGLED_P is set to the char * that should be freed when finished with
136    the tree, or NULL if none was needed.  OPTIONS will be passed to the
137    demangler.  */
138
139 static struct demangle_component *
140 mangled_name_to_comp (const char *mangled_name, int options,
141                       void **memory, char **demangled_p)
142 {
143   struct demangle_component *ret;
144   char *demangled_name;
145   int len;
146
147   /* If it looks like a v3 mangled name, then try to go directly
148      to trees.  */
149   if (mangled_name[0] == '_' && mangled_name[1] == 'Z')
150     {
151       ret = cplus_demangle_v3_components (mangled_name, options, memory);
152       if (ret)
153         {
154           *demangled_p = NULL;
155           return ret;
156         }
157     }
158
159   /* If it doesn't, or if that failed, then try to demangle the name.  */
160   demangled_name = cplus_demangle (mangled_name, options);
161   if (demangled_name == NULL)
162    return NULL;
163   
164   /* If we could demangle the name, parse it to build the component tree.  */
165   ret = cp_demangled_name_to_comp (demangled_name, NULL);
166
167   if (ret == NULL)
168     {
169       free (demangled_name);
170       return NULL;
171     }
172
173   *demangled_p = demangled_name;
174   return ret;
175 }
176
177 /* Return the name of the class containing method PHYSNAME.  */
178
179 char *
180 cp_class_name_from_physname (const char *physname)
181 {
182   void *storage;
183   char *demangled_name = NULL, *ret;
184   struct demangle_component *ret_comp, *prev_comp, *cur_comp;
185   int done;
186
187   ret_comp = mangled_name_to_comp (physname, DMGL_ANSI, &storage,
188                                    &demangled_name);
189   if (ret_comp == NULL)
190     return NULL;
191
192   done = 0;
193
194   /* First strip off any qualifiers, if we have a function or method.  */
195   while (!done)
196     switch (ret_comp->type)
197       {
198       case DEMANGLE_COMPONENT_CONST:
199       case DEMANGLE_COMPONENT_RESTRICT:
200       case DEMANGLE_COMPONENT_VOLATILE:
201       case DEMANGLE_COMPONENT_CONST_THIS:
202       case DEMANGLE_COMPONENT_RESTRICT_THIS:
203       case DEMANGLE_COMPONENT_VOLATILE_THIS:
204       case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
205         ret_comp = d_left (ret_comp);
206         break;
207       default:
208         done = 1;
209         break;
210       }
211
212   /* If what we have now is a function, discard the argument list.  */
213   if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
214     ret_comp = d_left (ret_comp);
215
216   /* If what we have now is a template, strip off the template
217      arguments.  The left subtree may be a qualified name.  */
218   if (ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE)
219     ret_comp = d_left (ret_comp);
220
221   /* What we have now should be a name, possibly qualified.  Additional
222      qualifiers could live in the left subtree or the right subtree.  Find
223      the last piece.  */
224   done = 0;
225   prev_comp = NULL;
226   cur_comp = ret_comp;
227   while (!done)
228     switch (cur_comp->type)
229       {
230       case DEMANGLE_COMPONENT_QUAL_NAME:
231       case DEMANGLE_COMPONENT_LOCAL_NAME:
232         prev_comp = cur_comp;
233         cur_comp = d_right (cur_comp);
234         break;
235       case DEMANGLE_COMPONENT_TEMPLATE:
236       case DEMANGLE_COMPONENT_NAME:
237       case DEMANGLE_COMPONENT_CTOR:
238       case DEMANGLE_COMPONENT_DTOR:
239       case DEMANGLE_COMPONENT_OPERATOR:
240       case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
241         done = 1;
242         break;
243       default:
244         done = 1;
245         cur_comp = NULL;
246         break;
247       }
248
249   ret = NULL;
250   if (cur_comp != NULL && prev_comp != NULL)
251     {
252       /* We want to discard the rightmost child of PREV_COMP.  */
253       *prev_comp = *d_left (prev_comp);
254       /* The ten is completely arbitrary; we don't have a good estimate.  */
255       ret = cp_comp_to_string (ret_comp, 10);
256     }
257
258   xfree (storage);
259   if (demangled_name)
260     xfree (demangled_name);
261   return ret;
262 }
263
264 /* Return the child of COMP which is the basename of a method, variable,
265    et cetera.  All scope qualifiers are discarded, but template arguments
266    will be included.  The component tree may be modified.  */
267
268 static struct demangle_component *
269 unqualified_name_from_comp (struct demangle_component *comp)
270 {
271   struct demangle_component *ret_comp = comp, *last_template;
272   int done;
273
274   done = 0;
275   last_template = NULL;
276   while (!done)
277     switch (ret_comp->type)
278       {
279       case DEMANGLE_COMPONENT_QUAL_NAME:
280       case DEMANGLE_COMPONENT_LOCAL_NAME:
281         ret_comp = d_right (ret_comp);
282         break;
283       case DEMANGLE_COMPONENT_TYPED_NAME:
284         ret_comp = d_left (ret_comp);
285         break;
286       case DEMANGLE_COMPONENT_TEMPLATE:
287         gdb_assert (last_template == NULL);
288         last_template = ret_comp;
289         ret_comp = d_left (ret_comp);
290         break;
291       case DEMANGLE_COMPONENT_CONST:
292       case DEMANGLE_COMPONENT_RESTRICT:
293       case DEMANGLE_COMPONENT_VOLATILE:
294       case DEMANGLE_COMPONENT_CONST_THIS:
295       case DEMANGLE_COMPONENT_RESTRICT_THIS:
296       case DEMANGLE_COMPONENT_VOLATILE_THIS:
297       case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
298         ret_comp = d_left (ret_comp);
299         break;
300       case DEMANGLE_COMPONENT_NAME:
301       case DEMANGLE_COMPONENT_CTOR:
302       case DEMANGLE_COMPONENT_DTOR:
303       case DEMANGLE_COMPONENT_OPERATOR:
304       case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
305         done = 1;
306         break;
307       default:
308         return NULL;
309         break;
310       }
311
312   if (last_template)
313     {
314       d_left (last_template) = ret_comp;
315       return last_template;
316     }
317
318   return ret_comp;
319 }
320
321 /* Return the name of the method whose linkage name is PHYSNAME.  */
322
323 char *
324 method_name_from_physname (const char *physname)
325 {
326   void *storage;
327   char *demangled_name = NULL, *ret;
328   struct demangle_component *ret_comp;
329   int done;
330
331   ret_comp = mangled_name_to_comp (physname, DMGL_ANSI, &storage,
332                                    &demangled_name);
333   if (ret_comp == NULL)
334     return NULL;
335
336   ret_comp = unqualified_name_from_comp (ret_comp);
337
338   ret = NULL;
339   if (ret_comp != NULL)
340     /* The ten is completely arbitrary; we don't have a good estimate.  */
341     ret = cp_comp_to_string (ret_comp, 10);
342
343   xfree (storage);
344   if (demangled_name)
345     xfree (demangled_name);
346   return ret;
347 }
348
349 /* If FULL_NAME is the demangled name of a C++ function (including an
350    arg list, possibly including namespace/class qualifications),
351    return a new string containing only the function name (without the
352    arg list/class qualifications).  Otherwise, return NULL.  The
353    caller is responsible for freeing the memory in question.  */
354
355 char *
356 cp_func_name (const char *full_name)
357 {
358   char *ret;
359   struct demangle_component *ret_comp;
360   int done;
361
362   ret_comp = cp_demangled_name_to_comp (full_name, NULL);
363   if (!ret_comp)
364     return NULL;
365
366   ret_comp = unqualified_name_from_comp (ret_comp);
367
368   ret = NULL;
369   if (ret_comp != NULL)
370     ret = cp_comp_to_string (ret_comp, 10);
371
372   return ret;
373 }
374
375 /* DEMANGLED_NAME is the name of a function, including parameters and
376    (optionally) a return type.  Return the name of the function without
377    parameters or return type, or NULL if we can not parse the name.  */
378
379 static char *
380 remove_params (const char *demangled_name)
381 {
382   int done = 0;
383   struct demangle_component *ret_comp;
384   char *ret = NULL;
385
386   if (demangled_name == NULL)
387     return NULL;
388
389   ret_comp = cp_demangled_name_to_comp (demangled_name, NULL);
390   if (ret_comp == NULL)
391     return NULL;
392
393   /* First strip off any qualifiers, if we have a function or method.  */
394   while (!done)
395     switch (ret_comp->type)
396       {
397       case DEMANGLE_COMPONENT_CONST:
398       case DEMANGLE_COMPONENT_RESTRICT:
399       case DEMANGLE_COMPONENT_VOLATILE:
400       case DEMANGLE_COMPONENT_CONST_THIS:
401       case DEMANGLE_COMPONENT_RESTRICT_THIS:
402       case DEMANGLE_COMPONENT_VOLATILE_THIS:
403       case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
404         ret_comp = d_left (ret_comp);
405         break;
406       default:
407         done = 1;
408         break;
409       }
410
411   /* What we have now should be a function.  Return its name.  */
412   if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
413     ret = cp_comp_to_string (d_left (ret_comp), 10);
414
415   return ret;
416 }
417
418 /* Here are some random pieces of trivia to keep in mind while trying
419    to take apart demangled names:
420
421    - Names can contain function arguments or templates, so the process
422      has to be, to some extent recursive: maybe keep track of your
423      depth based on encountering <> and ().
424
425    - Parentheses don't just have to happen at the end of a name: they
426      can occur even if the name in question isn't a function, because
427      a template argument might be a type that's a function.
428
429    - Conversely, even if you're trying to deal with a function, its
430      demangled name might not end with ')': it could be a const or
431      volatile class method, in which case it ends with "const" or
432      "volatile".
433
434    - Parentheses are also used in anonymous namespaces: a variable
435      'foo' in an anonymous namespace gets demangled as "(anonymous
436      namespace)::foo".
437
438    - And operator names can contain parentheses or angle brackets.  */
439
440 /* FIXME: carlton/2003-03-13: We have several functions here with
441    overlapping functionality; can we combine them?  Also, do they
442    handle all the above considerations correctly?  */
443
444
445 /* This returns the length of first component of NAME, which should be
446    the demangled name of a C++ variable/function/method/etc.
447    Specifically, it returns the index of the first colon forming the
448    boundary of the first component: so, given 'A::foo' or 'A::B::foo'
449    it returns the 1, and given 'foo', it returns 0.  */
450
451 /* The character in NAME indexed by the return value is guaranteed to
452    always be either ':' or '\0'.  */
453
454 /* NOTE: carlton/2003-03-13: This function is currently only intended
455    for internal use: it's probably not entirely safe when called on
456    user-generated input, because some of the 'index += 2' lines in
457    cp_find_first_component_aux might go past the end of malformed
458    input.  */
459
460 unsigned int
461 cp_find_first_component (const char *name)
462 {
463   return cp_find_first_component_aux (name, 0);
464 }
465
466 /* Helper function for cp_find_first_component.  Like that function,
467    it returns the length of the first component of NAME, but to make
468    the recursion easier, it also stops if it reaches an unexpected ')'
469    or '>' if the value of PERMISSIVE is nonzero.  */
470
471 /* Let's optimize away calls to strlen("operator").  */
472
473 #define LENGTH_OF_OPERATOR 8
474
475 static unsigned int
476 cp_find_first_component_aux (const char *name, int permissive)
477 {
478   unsigned int index = 0;
479   /* Operator names can show up in unexpected places.  Since these can
480      contain parentheses or angle brackets, they can screw up the
481      recursion.  But not every string 'operator' is part of an
482      operater name: e.g. you could have a variable 'cooperator'.  So
483      this variable tells us whether or not we should treat the string
484      'operator' as starting an operator.  */
485   int operator_possible = 1;
486
487   for (;; ++index)
488     {
489       switch (name[index])
490         {
491         case '<':
492           /* Template; eat it up.  The calls to cp_first_component
493              should only return (I hope!) when they reach the '>'
494              terminating the component or a '::' between two
495              components.  (Hence the '+ 2'.)  */
496           index += 1;
497           for (index += cp_find_first_component_aux (name + index, 1);
498                name[index] != '>';
499                index += cp_find_first_component_aux (name + index, 1))
500             {
501               if (name[index] != ':')
502                 {
503                   demangled_name_complaint (name);
504                   return strlen (name);
505                 }
506               index += 2;
507             }
508           operator_possible = 1;
509           break;
510         case '(':
511           /* Similar comment as to '<'.  */
512           index += 1;
513           for (index += cp_find_first_component_aux (name + index, 1);
514                name[index] != ')';
515                index += cp_find_first_component_aux (name + index, 1))
516             {
517               if (name[index] != ':')
518                 {
519                   demangled_name_complaint (name);
520                   return strlen (name);
521                 }
522               index += 2;
523             }
524           operator_possible = 1;
525           break;
526         case '>':
527         case ')':
528           if (permissive)
529             return index;
530           else
531             {
532               demangled_name_complaint (name);
533               return strlen (name);
534             }
535         case '\0':
536         case ':':
537           return index;
538         case 'o':
539           /* Operator names can screw up the recursion.  */
540           if (operator_possible
541               && strncmp (name + index, "operator", LENGTH_OF_OPERATOR) == 0)
542             {
543               index += LENGTH_OF_OPERATOR;
544               while (ISSPACE(name[index]))
545                 ++index;
546               switch (name[index])
547                 {
548                   /* Skip over one less than the appropriate number of
549                      characters: the for loop will skip over the last
550                      one.  */
551                 case '<':
552                   if (name[index + 1] == '<')
553                     index += 1;
554                   else
555                     index += 0;
556                   break;
557                 case '>':
558                 case '-':
559                   if (name[index + 1] == '>')
560                     index += 1;
561                   else
562                     index += 0;
563                   break;
564                 case '(':
565                   index += 1;
566                   break;
567                 default:
568                   index += 0;
569                   break;
570                 }
571             }
572           operator_possible = 0;
573           break;
574         case ' ':
575         case ',':
576         case '.':
577         case '&':
578         case '*':
579           /* NOTE: carlton/2003-04-18: I'm not sure what the precise
580              set of relevant characters are here: it's necessary to
581              include any character that can show up before 'operator'
582              in a demangled name, and it's safe to include any
583              character that can't be part of an identifier's name.  */
584           operator_possible = 1;
585           break;
586         default:
587           operator_possible = 0;
588           break;
589         }
590     }
591 }
592
593 /* Complain about a demangled name that we don't know how to parse.
594    NAME is the demangled name in question.  */
595
596 static void
597 demangled_name_complaint (const char *name)
598 {
599   complaint (&symfile_complaints,
600              "unexpected demangled name '%s'", name);
601 }
602
603 /* If NAME is the fully-qualified name of a C++
604    function/variable/method/etc., this returns the length of its
605    entire prefix: all of the namespaces and classes that make up its
606    name.  Given 'A::foo', it returns 1, given 'A::B::foo', it returns
607    4, given 'foo', it returns 0.  */
608
609 unsigned int
610 cp_entire_prefix_len (const char *name)
611 {
612   unsigned int current_len = cp_find_first_component (name);
613   unsigned int previous_len = 0;
614
615   while (name[current_len] != '\0')
616     {
617       gdb_assert (name[current_len] == ':');
618       previous_len = current_len;
619       /* Skip the '::'.  */
620       current_len += 2;
621       current_len += cp_find_first_component (name + current_len);
622     }
623
624   return previous_len;
625 }
626
627 /* Overload resolution functions.  */
628
629 /* Test to see if SYM is a symbol that we haven't seen corresponding
630    to a function named OLOAD_NAME.  If so, add it to the current
631    completion list. */
632
633 static void
634 overload_list_add_symbol (struct symbol *sym, const char *oload_name)
635 {
636   int newsize;
637   int i;
638   char *sym_name;
639
640   /* If there is no type information, we can't do anything, so skip */
641   if (SYMBOL_TYPE (sym) == NULL)
642     return;
643
644   /* skip any symbols that we've already considered. */
645   for (i = 0; i < sym_return_val_index; ++i)
646     if (strcmp (SYMBOL_LINKAGE_NAME (sym),
647                 SYMBOL_LINKAGE_NAME (sym_return_val[i])) == 0)
648       return;
649
650   /* Get the demangled name without parameters */
651   sym_name = remove_params (SYMBOL_NATURAL_NAME (sym));
652   if (!sym_name)
653     return;
654
655   /* skip symbols that cannot match */
656   if (strcmp (sym_name, oload_name) != 0)
657     {
658       xfree (sym_name);
659       return;
660     }
661
662   xfree (sym_name);
663
664   /* We have a match for an overload instance, so add SYM to the current list
665    * of overload instances */
666   if (sym_return_val_index + 3 > sym_return_val_size)
667     {
668       newsize = (sym_return_val_size *= 2) * sizeof (struct symbol *);
669       sym_return_val = (struct symbol **) xrealloc ((char *) sym_return_val, newsize);
670     }
671   sym_return_val[sym_return_val_index++] = sym;
672   sym_return_val[sym_return_val_index] = NULL;
673 }
674
675 /* Return a null-terminated list of pointers to function symbols that
676    are named FUNC_NAME and are visible within NAMESPACE.  */
677
678 struct symbol **
679 make_symbol_overload_list (const char *func_name,
680                            const char *namespace)
681 {
682   struct cleanup *old_cleanups;
683
684   sym_return_val_size = 100;
685   sym_return_val_index = 0;
686   sym_return_val = xmalloc ((sym_return_val_size + 1) *
687                             sizeof (struct symbol *));
688   sym_return_val[0] = NULL;
689
690   old_cleanups = make_cleanup (xfree, sym_return_val);
691
692   make_symbol_overload_list_using (func_name, namespace);
693
694   discard_cleanups (old_cleanups);
695
696   return sym_return_val;
697 }
698
699 /* This applies the using directives to add namespaces to search in,
700    and then searches for overloads in all of those namespaces.  It
701    adds the symbols found to sym_return_val.  Arguments are as in
702    make_symbol_overload_list.  */
703
704 static void
705 make_symbol_overload_list_using (const char *func_name,
706                                  const char *namespace)
707 {
708   const struct using_direct *current;
709
710   /* First, go through the using directives.  If any of them apply,
711      look in the appropriate namespaces for new functions to match
712      on.  */
713
714   for (current = block_using (get_selected_block (0));
715        current != NULL;
716        current = current->next)
717     {
718       if (strcmp (namespace, current->outer) == 0)
719         {
720           make_symbol_overload_list_using (func_name,
721                                            current->inner);
722         }
723     }
724
725   /* Now, add names for this namespace.  */
726   
727   if (namespace[0] == '\0')
728     {
729       make_symbol_overload_list_qualified (func_name);
730     }
731   else
732     {
733       char *concatenated_name
734         = alloca (strlen (namespace) + 2 + strlen (func_name) + 1);
735       strcpy (concatenated_name, namespace);
736       strcat (concatenated_name, "::");
737       strcat (concatenated_name, func_name);
738       make_symbol_overload_list_qualified (concatenated_name);
739     }
740 }
741
742 /* This does the bulk of the work of finding overloaded symbols.
743    FUNC_NAME is the name of the overloaded function we're looking for
744    (possibly including namespace info).  */
745
746 static void
747 make_symbol_overload_list_qualified (const char *func_name)
748 {
749   struct symbol *sym;
750   struct symtab *s;
751   struct objfile *objfile;
752   const struct block *b, *surrounding_static_block = 0;
753   struct dict_iterator iter;
754   const struct dictionary *dict;
755
756   /* Look through the partial symtabs for all symbols which begin
757      by matching FUNC_NAME.  Make sure we read that symbol table in. */
758
759   read_in_psymtabs (func_name);
760
761   /* Search upwards from currently selected frame (so that we can
762      complete on local vars.  */
763
764   for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
765     {
766       dict = BLOCK_DICT (b);
767
768       for (sym = dict_iter_name_first (dict, func_name, &iter);
769            sym;
770            sym = dict_iter_name_next (func_name, &iter))
771         {
772           overload_list_add_symbol (sym, func_name);
773         }
774     }
775
776   surrounding_static_block = block_static_block (get_selected_block (0));
777
778   /* Go through the symtabs and check the externs and statics for
779      symbols which match.  */
780
781   ALL_PRIMARY_SYMTABS (objfile, s)
782   {
783     QUIT;
784     b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
785     dict = BLOCK_DICT (b);
786
787     for (sym = dict_iter_name_first (dict, func_name, &iter);
788          sym;
789          sym = dict_iter_name_next (func_name, &iter))
790     {
791       overload_list_add_symbol (sym, func_name);
792     }
793   }
794
795   ALL_PRIMARY_SYMTABS (objfile, s)
796   {
797     QUIT;
798     b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
799     /* Don't do this block twice.  */
800     if (b == surrounding_static_block)
801       continue;
802     dict = BLOCK_DICT (b);
803
804     for (sym = dict_iter_name_first (dict, func_name, &iter);
805          sym;
806          sym = dict_iter_name_next (func_name, &iter))
807     {
808       overload_list_add_symbol (sym, func_name);
809     }
810   }
811 }
812
813 /* Look through the partial symtabs for all symbols which begin
814    by matching FUNC_NAME.  Make sure we read that symbol table in. */
815
816 static void
817 read_in_psymtabs (const char *func_name)
818 {
819   struct partial_symtab *ps;
820   struct objfile *objfile;
821
822   ALL_PSYMTABS (objfile, ps)
823   {
824     if (ps->readin)
825       continue;
826
827     if ((lookup_partial_symbol (ps, func_name, NULL, 1, VAR_DOMAIN)
828          != NULL)
829         || (lookup_partial_symbol (ps, func_name, NULL, 0, VAR_DOMAIN)
830             != NULL))
831       psymtab_to_symtab (ps);
832   }
833 }
834
835 /* Lookup the rtti type for a class name. */
836
837 struct type *
838 cp_lookup_rtti_type (const char *name, struct block *block)
839 {
840   struct symbol * rtti_sym;
841   struct type * rtti_type;
842
843   rtti_sym = lookup_symbol (name, block, STRUCT_DOMAIN, NULL, NULL);
844
845   if (rtti_sym == NULL)
846     {
847       warning (_("RTTI symbol not found for class '%s'"), name);
848       return NULL;
849     }
850
851   if (SYMBOL_CLASS (rtti_sym) != LOC_TYPEDEF)
852     {
853       warning (_("RTTI symbol for class '%s' is not a type"), name);
854       return NULL;
855     }
856
857   rtti_type = SYMBOL_TYPE (rtti_sym);
858
859   switch (TYPE_CODE (rtti_type))
860     {
861     case TYPE_CODE_CLASS:
862       break;
863     case TYPE_CODE_NAMESPACE:
864       /* chastain/2003-11-26: the symbol tables often contain fake
865          symbols for namespaces with the same name as the struct.
866          This warning is an indication of a bug in the lookup order
867          or a bug in the way that the symbol tables are populated.  */
868       warning (_("RTTI symbol for class '%s' is a namespace"), name);
869       return NULL;
870     default:
871       warning (_("RTTI symbol for class '%s' has bad type"), name);
872       return NULL;
873     }
874
875   return rtti_type;
876 }
877
878 /* Don't allow just "maintenance cplus".  */
879
880 static  void
881 maint_cplus_command (char *arg, int from_tty)
882 {
883   printf_unfiltered (_("\"maintenance cplus\" must be followed by the name of a command.\n"));
884   help_list (maint_cplus_cmd_list, "maintenance cplus ", -1, gdb_stdout);
885 }
886
887 /* This is a front end for cp_find_first_component, for unit testing.
888    Be careful when using it: see the NOTE above
889    cp_find_first_component.  */
890
891 static void
892 first_component_command (char *arg, int from_tty)
893 {
894   int len = cp_find_first_component (arg);
895   char *prefix = alloca (len + 1);
896
897   memcpy (prefix, arg, len);
898   prefix[len] = '\0';
899
900   printf_unfiltered ("%s\n", prefix);
901 }
902
903 extern initialize_file_ftype _initialize_cp_support; /* -Wmissing-prototypes */
904
905 void
906 _initialize_cp_support (void)
907 {
908   add_prefix_cmd ("cplus", class_maintenance, maint_cplus_command,
909                   _("C++ maintenance commands."), &maint_cplus_cmd_list,
910                   "maintenance cplus ", 0, &maintenancelist);
911   add_alias_cmd ("cp", "cplus", class_maintenance, 1, &maintenancelist);
912
913   add_cmd ("first_component", class_maintenance, first_component_command,
914            _("Print the first class/namespace component of NAME."),
915            &maint_cplus_cmd_list);
916 }