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