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