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