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