Merge from vendor branch GDB:
[dragonfly.git] / contrib / gdb-6.2.1 / gdb / cp-support.c
1 /* Helper routines for C++ support in GDB.
2    Copyright 2002, 2003 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 2 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, write to the Free Software
20    Foundation, Inc., 59 Temple Place - Suite 330,
21    Boston, MA 02111-1307, USA.  */
22
23 #include "defs.h"
24 #include <ctype.h>
25 #include "cp-support.h"
26 #include "gdb_string.h"
27 #include "demangle.h"
28 #include "gdb_assert.h"
29 #include "gdbcmd.h"
30 #include "dictionary.h"
31 #include "objfiles.h"
32 #include "frame.h"
33 #include "symtab.h"
34 #include "block.h"
35 #include "complaints.h"
36 #include "gdbtypes.h"
37
38 /* Functions related to demangled name parsing.  */
39
40 static const char *find_last_component (const char *name);
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 char *remove_params (const char *demangled_name);
54
55 static void overload_list_add_symbol (struct symbol *sym,
56                                       const char *oload_name);
57
58 static void make_symbol_overload_list_using (const char *func_name,
59                                              const char *namespace);
60
61 static void make_symbol_overload_list_qualified (const char *func_name);
62
63 static void read_in_psymtabs (const char *oload_name);
64
65 /* The list of "maint cplus" commands.  */
66
67 struct cmd_list_element *maint_cplus_cmd_list = NULL;
68
69 /* The actual commands.  */
70
71 static void maint_cplus_command (char *arg, int from_tty);
72 static void first_component_command (char *arg, int from_tty);
73
74 /* Here are some random pieces of trivia to keep in mind while trying
75    to take apart demangled names:
76
77    - Names can contain function arguments or templates, so the process
78      has to be, to some extent recursive: maybe keep track of your
79      depth based on encountering <> and ().
80
81    - Parentheses don't just have to happen at the end of a name: they
82      can occur even if the name in question isn't a function, because
83      a template argument might be a type that's a function.
84
85    - Conversely, even if you're trying to deal with a function, its
86      demangled name might not end with ')': it could be a const or
87      volatile class method, in which case it ends with "const" or
88      "volatile".
89
90    - Parentheses are also used in anonymous namespaces: a variable
91      'foo' in an anonymous namespace gets demangled as "(anonymous
92      namespace)::foo".
93
94    - And operator names can contain parentheses or angle brackets.  */
95
96 /* FIXME: carlton/2003-03-13: We have several functions here with
97    overlapping functionality; can we combine them?  Also, do they
98    handle all the above considerations correctly?  */
99
100 /* Find the last component of the demangled C++ name NAME.  NAME
101    must be a method name including arguments, in order to correctly
102    locate the last component.
103
104    This function return a pointer to the first colon before the
105    last component, or NULL if the name had only one component.  */
106
107 static const char *
108 find_last_component (const char *name)
109 {
110   const char *p;
111   int depth;
112
113   /* Functions can have local classes, so we need to find the
114      beginning of the last argument list, not the end of the first
115      one.  */
116   p = name + strlen (name) - 1;
117   while (p > name && *p != ')')
118     p--;
119
120   if (p == name)
121     return NULL;
122
123   /* P now points at the `)' at the end of the argument list.  Walk
124      back to the beginning.  */
125   p--;
126   depth = 1;
127   while (p > name && depth > 0)
128     {
129       if (*p == '<' || *p == '(')
130         depth--;
131       else if (*p == '>' || *p == ')')
132         depth++;
133       p--;
134     }
135
136   if (p == name)
137     return NULL;
138
139   while (p > name && *p != ':')
140     p--;
141
142   if (p == name || p == name + 1 || p[-1] != ':')
143     return NULL;
144
145   return p - 1;
146 }
147
148 /* Return the name of the class containing method PHYSNAME.  */
149
150 char *
151 cp_class_name_from_physname (const char *physname)
152 {
153   char *ret = NULL;
154   const char *end;
155   int depth = 0;
156   char *demangled_name = cplus_demangle (physname, DMGL_ANSI | DMGL_PARAMS);
157
158   if (demangled_name == NULL)
159     return NULL;
160
161   end = find_last_component (demangled_name);
162   if (end != NULL)
163     {
164       ret = xmalloc (end - demangled_name + 1);
165       memcpy (ret, demangled_name, end - demangled_name);
166       ret[end - demangled_name] = '\0';
167     }
168
169   xfree (demangled_name);
170   return ret;
171 }
172
173 /* Return the name of the method whose linkage name is PHYSNAME.  */
174
175 char *
176 method_name_from_physname (const char *physname)
177 {
178   char *ret = NULL;
179   const char *end;
180   int depth = 0;
181   char *demangled_name = cplus_demangle (physname, DMGL_ANSI | DMGL_PARAMS);
182
183   if (demangled_name == NULL)
184     return NULL;
185
186   end = find_last_component (demangled_name);
187   if (end != NULL)
188     {
189       char *args;
190       int len;
191
192       /* Skip "::".  */
193       end = end + 2;
194
195       /* Find the argument list, if any.  */
196       args = strchr (end, '(');
197       if (args == NULL)
198         len = strlen (end + 2);
199       else
200         {
201           args --;
202           while (*args == ' ')
203             args --;
204           len = args - end + 1;
205         }
206       ret = xmalloc (len + 1);
207       memcpy (ret, end, len);
208       ret[len] = 0;
209     }
210
211   xfree (demangled_name);
212   return ret;
213 }
214
215 /* This returns the length of first component of NAME, which should be
216    the demangled name of a C++ variable/function/method/etc.
217    Specifically, it returns the index of the first colon forming the
218    boundary of the first component: so, given 'A::foo' or 'A::B::foo'
219    it returns the 1, and given 'foo', it returns 0.  */
220
221 /* The character in NAME indexed by the return value is guaranteed to
222    always be either ':' or '\0'.  */
223
224 /* NOTE: carlton/2003-03-13: This function is currently only intended
225    for internal use: it's probably not entirely safe when called on
226    user-generated input, because some of the 'index += 2' lines in
227    cp_find_first_component_aux might go past the end of malformed
228    input.  */
229
230 unsigned int
231 cp_find_first_component (const char *name)
232 {
233   return cp_find_first_component_aux (name, 0);
234 }
235
236 /* Helper function for cp_find_first_component.  Like that function,
237    it returns the length of the first component of NAME, but to make
238    the recursion easier, it also stops if it reaches an unexpected ')'
239    or '>' if the value of PERMISSIVE is nonzero.  */
240
241 /* Let's optimize away calls to strlen("operator").  */
242
243 #define LENGTH_OF_OPERATOR 8
244
245 static unsigned int
246 cp_find_first_component_aux (const char *name, int permissive)
247 {
248   unsigned int index = 0;
249   /* Operator names can show up in unexpected places.  Since these can
250      contain parentheses or angle brackets, they can screw up the
251      recursion.  But not every string 'operator' is part of an
252      operater name: e.g. you could have a variable 'cooperator'.  So
253      this variable tells us whether or not we should treat the string
254      'operator' as starting an operator.  */
255   int operator_possible = 1;
256
257   for (;; ++index)
258     {
259       switch (name[index])
260         {
261         case '<':
262           /* Template; eat it up.  The calls to cp_first_component
263              should only return (I hope!) when they reach the '>'
264              terminating the component or a '::' between two
265              components.  (Hence the '+ 2'.)  */
266           index += 1;
267           for (index += cp_find_first_component_aux (name + index, 1);
268                name[index] != '>';
269                index += cp_find_first_component_aux (name + index, 1))
270             {
271               if (name[index] != ':')
272                 {
273                   demangled_name_complaint (name);
274                   return strlen (name);
275                 }
276               index += 2;
277             }
278           operator_possible = 1;
279           break;
280         case '(':
281           /* Similar comment as to '<'.  */
282           index += 1;
283           for (index += cp_find_first_component_aux (name + index, 1);
284                name[index] != ')';
285                index += cp_find_first_component_aux (name + index, 1))
286             {
287               if (name[index] != ':')
288                 {
289                   demangled_name_complaint (name);
290                   return strlen (name);
291                 }
292               index += 2;
293             }
294           operator_possible = 1;
295           break;
296         case '>':
297         case ')':
298           if (permissive)
299             return index;
300           else
301             {
302               demangled_name_complaint (name);
303               return strlen (name);
304             }
305         case '\0':
306         case ':':
307           return index;
308         case 'o':
309           /* Operator names can screw up the recursion.  */
310           if (operator_possible
311               && strncmp (name + index, "operator", LENGTH_OF_OPERATOR) == 0)
312             {
313               index += LENGTH_OF_OPERATOR;
314               while (isspace(name[index]))
315                 ++index;
316               switch (name[index])
317                 {
318                   /* Skip over one less than the appropriate number of
319                      characters: the for loop will skip over the last
320                      one.  */
321                 case '<':
322                   if (name[index + 1] == '<')
323                     index += 1;
324                   else
325                     index += 0;
326                   break;
327                 case '>':
328                 case '-':
329                   if (name[index + 1] == '>')
330                     index += 1;
331                   else
332                     index += 0;
333                   break;
334                 case '(':
335                   index += 1;
336                   break;
337                 default:
338                   index += 0;
339                   break;
340                 }
341             }
342           operator_possible = 0;
343           break;
344         case ' ':
345         case ',':
346         case '.':
347         case '&':
348         case '*':
349           /* NOTE: carlton/2003-04-18: I'm not sure what the precise
350              set of relevant characters are here: it's necessary to
351              include any character that can show up before 'operator'
352              in a demangled name, and it's safe to include any
353              character that can't be part of an identifier's name.  */
354           operator_possible = 1;
355           break;
356         default:
357           operator_possible = 0;
358           break;
359         }
360     }
361 }
362
363 /* Complain about a demangled name that we don't know how to parse.
364    NAME is the demangled name in question.  */
365
366 static void
367 demangled_name_complaint (const char *name)
368 {
369   complaint (&symfile_complaints,
370              "unexpected demangled name '%s'", name);
371 }
372
373 /* If NAME is the fully-qualified name of a C++
374    function/variable/method/etc., this returns the length of its
375    entire prefix: all of the namespaces and classes that make up its
376    name.  Given 'A::foo', it returns 1, given 'A::B::foo', it returns
377    4, given 'foo', it returns 0.  */
378
379 unsigned int
380 cp_entire_prefix_len (const char *name)
381 {
382   unsigned int current_len = cp_find_first_component (name);
383   unsigned int previous_len = 0;
384
385   while (name[current_len] != '\0')
386     {
387       gdb_assert (name[current_len] == ':');
388       previous_len = current_len;
389       /* Skip the '::'.  */
390       current_len += 2;
391       current_len += cp_find_first_component (name + current_len);
392     }
393
394   return previous_len;
395 }
396
397 /* If FULL_NAME is the demangled name of a C++ function (including an
398    arg list, possibly including namespace/class qualifications),
399    return a new string containing only the function name (without the
400    arg list/class qualifications).  Otherwise, return NULL.  The
401    caller is responsible for freeing the memory in question.  */
402
403 char *
404 cp_func_name (const char *full_name)
405 {
406   const char *previous_component = full_name;
407   const char *next_component;
408
409   if (!full_name)
410     return NULL;
411
412   for (next_component = (previous_component
413                          + cp_find_first_component (previous_component));
414        *next_component == ':';
415        next_component = (previous_component
416                          + cp_find_first_component (previous_component)))
417     {
418       /* Skip '::'.  */
419       previous_component = next_component + 2;
420     }
421
422   return remove_params (previous_component);
423 }
424
425 /* Overload resolution functions.  */
426
427 static char *
428 remove_params (const char *demangled_name)
429 {
430   const char *argp;
431   char *new_name;
432   int depth;
433
434   if (demangled_name == NULL)
435     return NULL;
436
437   /* First find the end of the arg list.  */
438   argp = strrchr (demangled_name, ')');
439   if (argp == NULL)
440     return NULL;
441
442   /* Back up to the beginning.  */
443   depth = 1;
444
445   while (argp-- > demangled_name)
446     {
447       if (*argp == ')')
448         depth ++;
449       else if (*argp == '(')
450         {
451           depth --;
452
453           if (depth == 0)
454             break;
455         }
456     }
457   if (depth != 0)
458     internal_error (__FILE__, __LINE__,
459                     "bad demangled name %s\n", demangled_name);
460   while (argp[-1] == ' ' && argp > demangled_name)
461     argp --;
462
463   new_name = xmalloc (argp - demangled_name + 1);
464   memcpy (new_name, demangled_name, argp - demangled_name);
465   new_name[argp - demangled_name] = '\0';
466   return new_name;
467 }
468
469 /* Test to see if SYM is a symbol that we haven't seen corresponding
470    to a function named OLOAD_NAME.  If so, add it to the current
471    completion list. */
472
473 static void
474 overload_list_add_symbol (struct symbol *sym, const char *oload_name)
475 {
476   int newsize;
477   int i;
478   char *sym_name;
479
480   /* If there is no type information, we can't do anything, so skip */
481   if (SYMBOL_TYPE (sym) == NULL)
482     return;
483
484   /* skip any symbols that we've already considered. */
485   for (i = 0; i < sym_return_val_index; ++i)
486     if (strcmp (SYMBOL_LINKAGE_NAME (sym),
487                 SYMBOL_LINKAGE_NAME (sym_return_val[i])) == 0)
488       return;
489
490   /* Get the demangled name without parameters */
491   sym_name = remove_params (SYMBOL_NATURAL_NAME (sym));
492   if (!sym_name)
493     return;
494
495   /* skip symbols that cannot match */
496   if (strcmp (sym_name, oload_name) != 0)
497     {
498       xfree (sym_name);
499       return;
500     }
501
502   xfree (sym_name);
503
504   /* We have a match for an overload instance, so add SYM to the current list
505    * of overload instances */
506   if (sym_return_val_index + 3 > sym_return_val_size)
507     {
508       newsize = (sym_return_val_size *= 2) * sizeof (struct symbol *);
509       sym_return_val = (struct symbol **) xrealloc ((char *) sym_return_val, newsize);
510     }
511   sym_return_val[sym_return_val_index++] = sym;
512   sym_return_val[sym_return_val_index] = NULL;
513 }
514
515 /* Return a null-terminated list of pointers to function symbols that
516    are named FUNC_NAME and are visible within NAMESPACE.  */
517
518 struct symbol **
519 make_symbol_overload_list (const char *func_name,
520                            const char *namespace)
521 {
522   struct cleanup *old_cleanups;
523
524   sym_return_val_size = 100;
525   sym_return_val_index = 0;
526   sym_return_val = xmalloc ((sym_return_val_size + 1) *
527                             sizeof (struct symbol *));
528   sym_return_val[0] = NULL;
529
530   old_cleanups = make_cleanup (xfree, sym_return_val);
531
532   make_symbol_overload_list_using (func_name, namespace);
533
534   discard_cleanups (old_cleanups);
535
536   return sym_return_val;
537 }
538
539 /* This applies the using directives to add namespaces to search in,
540    and then searches for overloads in all of those namespaces.  It
541    adds the symbols found to sym_return_val.  Arguments are as in
542    make_symbol_overload_list.  */
543
544 static void
545 make_symbol_overload_list_using (const char *func_name,
546                                  const char *namespace)
547 {
548   const struct using_direct *current;
549
550   /* First, go through the using directives.  If any of them apply,
551      look in the appropriate namespaces for new functions to match
552      on.  */
553
554   for (current = block_using (get_selected_block (0));
555        current != NULL;
556        current = current->next)
557     {
558       if (strcmp (namespace, current->outer) == 0)
559         {
560           make_symbol_overload_list_using (func_name,
561                                            current->inner);
562         }
563     }
564
565   /* Now, add names for this namespace.  */
566   
567   if (namespace[0] == '\0')
568     {
569       make_symbol_overload_list_qualified (func_name);
570     }
571   else
572     {
573       char *concatenated_name
574         = alloca (strlen (namespace) + 2 + strlen (func_name) + 1);
575       strcpy (concatenated_name, namespace);
576       strcat (concatenated_name, "::");
577       strcat (concatenated_name, func_name);
578       make_symbol_overload_list_qualified (concatenated_name);
579     }
580 }
581
582 /* This does the bulk of the work of finding overloaded symbols.
583    FUNC_NAME is the name of the overloaded function we're looking for
584    (possibly including namespace info).  */
585
586 static void
587 make_symbol_overload_list_qualified (const char *func_name)
588 {
589   struct symbol *sym;
590   struct symtab *s;
591   struct objfile *objfile;
592   const struct block *b, *surrounding_static_block = 0;
593   struct dict_iterator iter;
594   const struct dictionary *dict;
595
596   /* Look through the partial symtabs for all symbols which begin
597      by matching FUNC_NAME.  Make sure we read that symbol table in. */
598
599   read_in_psymtabs (func_name);
600
601   /* Search upwards from currently selected frame (so that we can
602      complete on local vars.  */
603
604   for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
605     {
606       dict = BLOCK_DICT (b);
607
608       for (sym = dict_iter_name_first (dict, func_name, &iter);
609            sym;
610            sym = dict_iter_name_next (func_name, &iter))
611         {
612           overload_list_add_symbol (sym, func_name);
613         }
614     }
615
616   surrounding_static_block = block_static_block (get_selected_block (0));
617
618   /* Go through the symtabs and check the externs and statics for
619      symbols which match.  */
620
621   ALL_SYMTABS (objfile, s)
622   {
623     QUIT;
624     b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
625     dict = BLOCK_DICT (b);
626
627     for (sym = dict_iter_name_first (dict, func_name, &iter);
628          sym;
629          sym = dict_iter_name_next (func_name, &iter))
630     {
631       overload_list_add_symbol (sym, func_name);
632     }
633   }
634
635   ALL_SYMTABS (objfile, s)
636   {
637     QUIT;
638     b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
639     /* Don't do this block twice.  */
640     if (b == surrounding_static_block)
641       continue;
642     dict = BLOCK_DICT (b);
643
644     for (sym = dict_iter_name_first (dict, func_name, &iter);
645          sym;
646          sym = dict_iter_name_next (func_name, &iter))
647     {
648       overload_list_add_symbol (sym, func_name);
649     }
650   }
651 }
652
653 /* Look through the partial symtabs for all symbols which begin
654    by matching FUNC_NAME.  Make sure we read that symbol table in. */
655
656 static void
657 read_in_psymtabs (const char *func_name)
658 {
659   struct partial_symtab *ps;
660   struct objfile *objfile;
661
662   ALL_PSYMTABS (objfile, ps)
663   {
664     if (ps->readin)
665       continue;
666
667     if ((lookup_partial_symbol (ps, func_name, NULL, 1, VAR_DOMAIN)
668          != NULL)
669         || (lookup_partial_symbol (ps, func_name, NULL, 0, VAR_DOMAIN)
670             != NULL))
671       psymtab_to_symtab (ps);
672   }
673 }
674
675 /* Lookup the rtti type for a class name. */
676
677 struct type *
678 cp_lookup_rtti_type (const char *name, struct block *block)
679 {
680   struct symbol * rtti_sym;
681   struct type * rtti_type;
682
683   rtti_sym = lookup_symbol (name, block, STRUCT_DOMAIN, NULL, NULL);
684
685   if (rtti_sym == NULL)
686     {
687       warning ("RTTI symbol not found for class '%s'", name);
688       return NULL;
689     }
690
691   if (SYMBOL_CLASS (rtti_sym) != LOC_TYPEDEF)
692     {
693       warning ("RTTI symbol for class '%s' is not a type", name);
694       return NULL;
695     }
696
697   rtti_type = SYMBOL_TYPE (rtti_sym);
698
699   switch (TYPE_CODE (rtti_type))
700     {
701     case TYPE_CODE_CLASS:
702       break;
703     case TYPE_CODE_NAMESPACE:
704       /* chastain/2003-11-26: the symbol tables often contain fake
705          symbols for namespaces with the same name as the struct.
706          This warning is an indication of a bug in the lookup order
707          or a bug in the way that the symbol tables are populated.  */
708       warning ("RTTI symbol for class '%s' is a namespace", name);
709       return NULL;
710     default:
711       warning ("RTTI symbol for class '%s' has bad type", name);
712       return NULL;
713     }
714
715   return rtti_type;
716 }
717
718 /* Don't allow just "maintenance cplus".  */
719
720 static  void
721 maint_cplus_command (char *arg, int from_tty)
722 {
723   printf_unfiltered ("\"maintenance cplus\" must be followed by the name of a command.\n");
724   help_list (maint_cplus_cmd_list, "maintenance cplus ", -1, gdb_stdout);
725 }
726
727 /* This is a front end for cp_find_first_component, for unit testing.
728    Be careful when using it: see the NOTE above
729    cp_find_first_component.  */
730
731 static void
732 first_component_command (char *arg, int from_tty)
733 {
734   int len = cp_find_first_component (arg);
735   char *prefix = alloca (len + 1);
736
737   memcpy (prefix, arg, len);
738   prefix[len] = '\0';
739
740   printf_unfiltered ("%s\n", prefix);
741 }
742
743 extern initialize_file_ftype _initialize_cp_support; /* -Wmissing-prototypes */
744
745 void
746 _initialize_cp_support (void)
747 {
748   add_prefix_cmd ("cplus", class_maintenance, maint_cplus_command,
749                   "C++ maintenance commands.", &maint_cplus_cmd_list,
750                   "maintenance cplus ", 0, &maintenancelist);
751   add_alias_cmd ("cp", "cplus", class_maintenance, 1, &maintenancelist);
752
753   add_cmd ("first_component", class_maintenance, first_component_command,
754            "Print the first class/namespace component of NAME.",
755            &maint_cplus_cmd_list);
756                   
757 }