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