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