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