]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/cp-support.c
PR c++/12824:
[thirdparty/binutils-gdb.git] / gdb / cp-support.c
CommitLineData
de17c821 1/* Helper routines for C++ support in GDB.
28e7fd62 2 Copyright (C) 2002-2013 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"
12907978
KS
34#include "exceptions.h"
35#include "expression.h"
36#include "value.h"
c4aeac85 37#include "cp-abi.h"
b2a7f303 38
f88e9fd3
DJ
39#include "safe-ctype.h"
40
ccefe4c4
TT
41#include "psymtab.h"
42
fb4c6eba
DJ
43#define d_left(dc) (dc)->u.s_binary.left
44#define d_right(dc) (dc)->u.s_binary.right
b2a7f303 45
fb4c6eba 46/* Functions related to demangled name parsing. */
b2a7f303
DC
47
48static unsigned int cp_find_first_component_aux (const char *name,
49 int permissive);
50
51static void demangled_name_complaint (const char *name);
b6429628
DC
52
53/* Functions/variables related to overload resolution. */
54
7322dca9 55static int sym_return_val_size = -1;
b6429628
DC
56static int sym_return_val_index;
57static struct symbol **sym_return_val;
58
8d577d32
DC
59static void overload_list_add_symbol (struct symbol *sym,
60 const char *oload_name);
61
62static void make_symbol_overload_list_using (const char *func_name,
63 const char *namespace);
64
65static void make_symbol_overload_list_qualified (const char *func_name);
66
9219021c
DC
67/* The list of "maint cplus" commands. */
68
5c4e30ca 69struct cmd_list_element *maint_cplus_cmd_list = NULL;
9219021c
DC
70
71/* The actual commands. */
72
73static void maint_cplus_command (char *arg, int from_tty);
74static void first_component_command (char *arg, int from_tty);
75
3a93a0c2
KS
76/* A list of typedefs which should not be substituted by replace_typedefs. */
77static const char * const ignore_typedefs[] =
78 {
79 "std::istream", "std::iostream", "std::ostream", "std::string"
80 };
81
82static void
83 replace_typedefs (struct demangle_parse_info *info,
2621e0fd
TT
84 struct demangle_component *ret_comp,
85 canonicalization_ftype *finder,
86 void *data);
3a93a0c2
KS
87
88/* A convenience function to copy STRING into OBSTACK, returning a pointer
89 to the newly allocated string and saving the number of bytes saved in LEN.
90
91 It does not copy the terminating '\0' byte! */
92
93static char *
94copy_string_to_obstack (struct obstack *obstack, const char *string,
95 long *len)
96{
97 *len = strlen (string);
98 return obstack_copy (obstack, string, *len);
99}
100
101/* A cleanup wrapper for cp_demangled_name_parse_free. */
102
103static void
104do_demangled_name_parse_free_cleanup (void *data)
105{
106 struct demangle_parse_info *info = (struct demangle_parse_info *) data;
107
108 cp_demangled_name_parse_free (info);
109}
110
111/* Create a cleanup for C++ name parsing. */
112
113struct cleanup *
114make_cleanup_cp_demangled_name_parse_free (struct demangle_parse_info *info)
115{
116 return make_cleanup (do_demangled_name_parse_free_cleanup, info);
117}
118
f88e9fd3
DJ
119/* Return 1 if STRING is clearly already in canonical form. This
120 function is conservative; things which it does not recognize are
121 assumed to be non-canonical, and the parser will sort them out
122 afterwards. This speeds up the critical path for alphanumeric
123 identifiers. */
124
125static int
126cp_already_canonical (const char *string)
127{
128 /* Identifier start character [a-zA-Z_]. */
129 if (!ISIDST (string[0]))
130 return 0;
131
132 /* These are the only two identifiers which canonicalize to other
133 than themselves or an error: unsigned -> unsigned int and
134 signed -> int. */
135 if (string[0] == 'u' && strcmp (&string[1], "nsigned") == 0)
136 return 0;
137 else if (string[0] == 's' && strcmp (&string[1], "igned") == 0)
138 return 0;
139
140 /* Identifier character [a-zA-Z0-9_]. */
141 while (ISIDNUM (string[1]))
142 string++;
143
144 if (string[1] == '\0')
145 return 1;
146 else
147 return 0;
148}
9219021c 149
3a93a0c2
KS
150/* Inspect the given RET_COMP for its type. If it is a typedef,
151 replace the node with the typedef's tree.
152
153 Returns 1 if any typedef substitutions were made, 0 otherwise. */
154
155static int
156inspect_type (struct demangle_parse_info *info,
2621e0fd
TT
157 struct demangle_component *ret_comp,
158 canonicalization_ftype *finder,
159 void *data)
3a93a0c2
KS
160{
161 int i;
162 char *name;
163 struct symbol *sym;
164 volatile struct gdb_exception except;
165
166 /* Copy the symbol's name from RET_COMP and look it up
167 in the symbol table. */
168 name = (char *) alloca (ret_comp->u.s_name.len + 1);
169 memcpy (name, ret_comp->u.s_name.s, ret_comp->u.s_name.len);
170 name[ret_comp->u.s_name.len] = '\0';
171
172 /* Ignore any typedefs that should not be substituted. */
173 for (i = 0; i < ARRAY_SIZE (ignore_typedefs); ++i)
174 {
175 if (strcmp (name, ignore_typedefs[i]) == 0)
176 return 0;
177 }
178
179 sym = NULL;
180 TRY_CATCH (except, RETURN_MASK_ALL)
181 {
182 sym = lookup_symbol (name, 0, VAR_DOMAIN, 0);
183 }
184
185 if (except.reason >= 0 && sym != NULL)
186 {
187 struct type *otype = SYMBOL_TYPE (sym);
188
2621e0fd
TT
189 if (finder != NULL)
190 {
191 const char *new_name = (*finder) (otype, data);
192
193 if (new_name != NULL)
194 {
195 ret_comp->u.s_name.s = new_name;
196 ret_comp->u.s_name.len = strlen (new_name);
197 return 1;
198 }
199
200 return 0;
201 }
202
3a93a0c2
KS
203 /* If the type is a typedef, replace it. */
204 if (TYPE_CODE (otype) == TYPE_CODE_TYPEDEF)
205 {
206 long len;
207 int is_anon;
208 struct type *type;
209 struct demangle_parse_info *i;
210 struct ui_file *buf;
211
212 /* Get the real type of the typedef. */
213 type = check_typedef (otype);
214
215 is_anon = (TYPE_TAG_NAME (type) == NULL
216 && (TYPE_CODE (type) == TYPE_CODE_ENUM
217 || TYPE_CODE (type) == TYPE_CODE_STRUCT
218 || TYPE_CODE (type) == TYPE_CODE_UNION));
219 if (is_anon)
220 {
221 struct type *last = otype;
222
223 /* Find the last typedef for the type. */
224 while (TYPE_TARGET_TYPE (last) != NULL
225 && (TYPE_CODE (TYPE_TARGET_TYPE (last))
226 == TYPE_CODE_TYPEDEF))
227 last = TYPE_TARGET_TYPE (last);
228
229 /* If there is only one typedef for this anonymous type,
230 do not substitute it. */
231 if (type == otype)
232 return 0;
233 else
234 /* Use the last typedef seen as the type for this
235 anonymous type. */
236 type = last;
237 }
238
239 buf = mem_fileopen ();
240 TRY_CATCH (except, RETURN_MASK_ERROR)
241 {
242 type_print (type, "", buf, -1);
243 }
244
245 /* If type_print threw an exception, there is little point
246 in continuing, so just bow out gracefully. */
247 if (except.reason < 0)
248 {
249 ui_file_delete (buf);
250 return 0;
251 }
252
253 name = ui_file_obsavestring (buf, &info->obstack, &len);
254 ui_file_delete (buf);
255
256 /* Turn the result into a new tree. Note that this
257 tree will contain pointers into NAME, so NAME cannot
258 be free'd until all typedef conversion is done and
259 the final result is converted into a string. */
260 i = cp_demangled_name_to_comp (name, NULL);
261 if (i != NULL)
262 {
263 /* Merge the two trees. */
264 cp_merge_demangle_parse_infos (info, ret_comp, i);
265
266 /* Replace any newly introduced typedefs -- but not
267 if the type is anonymous (that would lead to infinite
268 looping). */
269 if (!is_anon)
2621e0fd 270 replace_typedefs (info, ret_comp, finder, data);
3a93a0c2
KS
271 }
272 else
273 {
274 /* This shouldn't happen unless the type printer has
275 output something that the name parser cannot grok.
276 Nonetheless, an ounce of prevention...
277
278 Canonicalize the name again, and store it in the
279 current node (RET_COMP). */
280 char *canon = cp_canonicalize_string_no_typedefs (name);
281
282 if (canon != NULL)
283 {
284 /* Copy the canonicalization into the obstack and
285 free CANON. */
286 name = copy_string_to_obstack (&info->obstack, canon, &len);
287 xfree (canon);
288 }
289
290 ret_comp->u.s_name.s = name;
291 ret_comp->u.s_name.len = len;
292 }
293
294 return 1;
295 }
296 }
297
298 return 0;
299}
300
301/* Replace any typedefs appearing in the qualified name
302 (DEMANGLE_COMPONENT_QUAL_NAME) represented in RET_COMP for the name parse
303 given in INFO. */
304
305static void
306replace_typedefs_qualified_name (struct demangle_parse_info *info,
2621e0fd
TT
307 struct demangle_component *ret_comp,
308 canonicalization_ftype *finder,
309 void *data)
3a93a0c2
KS
310{
311 long len;
312 char *name;
313 struct ui_file *buf = mem_fileopen ();
314 struct demangle_component *comp = ret_comp;
315
316 /* Walk each node of the qualified name, reconstructing the name of
317 this element. With every node, check for any typedef substitutions.
318 If a substitution has occurred, replace the qualified name node
319 with a DEMANGLE_COMPONENT_NAME node representing the new, typedef-
320 substituted name. */
321 while (comp->type == DEMANGLE_COMPONENT_QUAL_NAME)
322 {
323 if (d_left (comp)->type == DEMANGLE_COMPONENT_NAME)
324 {
325 struct demangle_component new;
326
327 ui_file_write (buf, d_left (comp)->u.s_name.s,
328 d_left (comp)->u.s_name.len);
329 name = ui_file_obsavestring (buf, &info->obstack, &len);
330 new.type = DEMANGLE_COMPONENT_NAME;
331 new.u.s_name.s = name;
332 new.u.s_name.len = len;
2621e0fd 333 if (inspect_type (info, &new, finder, data))
3a93a0c2
KS
334 {
335 char *n, *s;
336 long slen;
337
338 /* A typedef was substituted in NEW. Convert it to a
339 string and replace the top DEMANGLE_COMPONENT_QUAL_NAME
340 node. */
341
342 ui_file_rewind (buf);
343 n = cp_comp_to_string (&new, 100);
344 if (n == NULL)
345 {
346 /* If something went astray, abort typedef substitutions. */
347 ui_file_delete (buf);
348 return;
349 }
350
351 s = copy_string_to_obstack (&info->obstack, n, &slen);
352 xfree (n);
353
354 d_left (ret_comp)->type = DEMANGLE_COMPONENT_NAME;
355 d_left (ret_comp)->u.s_name.s = s;
356 d_left (ret_comp)->u.s_name.len = slen;
357 d_right (ret_comp) = d_right (comp);
358 comp = ret_comp;
359 continue;
360 }
361 }
362 else
363 {
364 /* The current node is not a name, so simply replace any
365 typedefs in it. Then print it to the stream to continue
366 checking for more typedefs in the tree. */
2621e0fd 367 replace_typedefs (info, d_left (comp), finder, data);
3a93a0c2
KS
368 name = cp_comp_to_string (d_left (comp), 100);
369 if (name == NULL)
370 {
371 /* If something went astray, abort typedef substitutions. */
372 ui_file_delete (buf);
373 return;
374 }
375 fputs_unfiltered (name, buf);
376 xfree (name);
377 }
2621e0fd 378
3a93a0c2
KS
379 ui_file_write (buf, "::", 2);
380 comp = d_right (comp);
381 }
382
383 /* If the next component is DEMANGLE_COMPONENT_NAME, save the qualified
384 name assembled above and append the name given by COMP. Then use this
385 reassembled name to check for a typedef. */
386
387 if (comp->type == DEMANGLE_COMPONENT_NAME)
388 {
389 ui_file_write (buf, comp->u.s_name.s, comp->u.s_name.len);
390 name = ui_file_obsavestring (buf, &info->obstack, &len);
391
392 /* Replace the top (DEMANGLE_COMPONENT_QUAL_NAME) node
393 with a DEMANGLE_COMPONENT_NAME node containing the whole
394 name. */
395 ret_comp->type = DEMANGLE_COMPONENT_NAME;
396 ret_comp->u.s_name.s = name;
397 ret_comp->u.s_name.len = len;
2621e0fd 398 inspect_type (info, ret_comp, finder, data);
3a93a0c2
KS
399 }
400 else
2621e0fd 401 replace_typedefs (info, comp, finder, data);
3a93a0c2
KS
402
403 ui_file_delete (buf);
404}
405
406
407/* A function to check const and volatile qualifiers for argument types.
408
409 "Parameter declarations that differ only in the presence
410 or absence of `const' and/or `volatile' are equivalent."
411 C++ Standard N3290, clause 13.1.3 #4. */
412
413static void
414check_cv_qualifiers (struct demangle_component *ret_comp)
415{
416 while (d_left (ret_comp) != NULL
417 && (d_left (ret_comp)->type == DEMANGLE_COMPONENT_CONST
418 || d_left (ret_comp)->type == DEMANGLE_COMPONENT_VOLATILE))
419 {
420 d_left (ret_comp) = d_left (d_left (ret_comp));
421 }
422}
423
424/* Walk the parse tree given by RET_COMP, replacing any typedefs with
425 their basic types. */
426
427static void
428replace_typedefs (struct demangle_parse_info *info,
2621e0fd
TT
429 struct demangle_component *ret_comp,
430 canonicalization_ftype *finder,
431 void *data)
3a93a0c2
KS
432{
433 if (ret_comp)
434 {
2621e0fd
TT
435 if (finder != NULL
436 && (ret_comp->type == DEMANGLE_COMPONENT_NAME
437 || ret_comp->type == DEMANGLE_COMPONENT_QUAL_NAME
438 || ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE
439 || ret_comp->type == DEMANGLE_COMPONENT_BUILTIN_TYPE))
440 {
441 char *local_name = cp_comp_to_string (ret_comp, 10);
442
443 if (local_name != NULL)
444 {
445 struct symbol *sym;
446 volatile struct gdb_exception except;
447
448 sym = NULL;
449 TRY_CATCH (except, RETURN_MASK_ALL)
450 {
451 sym = lookup_symbol (local_name, 0, VAR_DOMAIN, 0);
452 }
453 xfree (local_name);
454
455 if (except.reason >= 0 && sym != NULL)
456 {
457 struct type *otype = SYMBOL_TYPE (sym);
458 const char *new_name = (*finder) (otype, data);
459
460 if (new_name != NULL)
461 {
462 ret_comp->type = DEMANGLE_COMPONENT_NAME;
463 ret_comp->u.s_name.s = new_name;
464 ret_comp->u.s_name.len = strlen (new_name);
465 return;
466 }
467 }
468 }
469 }
470
3a93a0c2
KS
471 switch (ret_comp->type)
472 {
473 case DEMANGLE_COMPONENT_ARGLIST:
474 check_cv_qualifiers (ret_comp);
475 /* Fall through */
476
477 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
478 case DEMANGLE_COMPONENT_TEMPLATE:
479 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
480 case DEMANGLE_COMPONENT_TYPED_NAME:
2621e0fd
TT
481 replace_typedefs (info, d_left (ret_comp), finder, data);
482 replace_typedefs (info, d_right (ret_comp), finder, data);
3a93a0c2
KS
483 break;
484
485 case DEMANGLE_COMPONENT_NAME:
2621e0fd 486 inspect_type (info, ret_comp, finder, data);
3a93a0c2
KS
487 break;
488
489 case DEMANGLE_COMPONENT_QUAL_NAME:
2621e0fd 490 replace_typedefs_qualified_name (info, ret_comp, finder, data);
3a93a0c2
KS
491 break;
492
493 case DEMANGLE_COMPONENT_LOCAL_NAME:
494 case DEMANGLE_COMPONENT_CTOR:
495 case DEMANGLE_COMPONENT_ARRAY_TYPE:
496 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
2621e0fd 497 replace_typedefs (info, d_right (ret_comp), finder, data);
3a93a0c2
KS
498 break;
499
500 case DEMANGLE_COMPONENT_CONST:
501 case DEMANGLE_COMPONENT_RESTRICT:
502 case DEMANGLE_COMPONENT_VOLATILE:
503 case DEMANGLE_COMPONENT_VOLATILE_THIS:
504 case DEMANGLE_COMPONENT_CONST_THIS:
505 case DEMANGLE_COMPONENT_RESTRICT_THIS:
506 case DEMANGLE_COMPONENT_POINTER:
507 case DEMANGLE_COMPONENT_REFERENCE:
2621e0fd 508 replace_typedefs (info, d_left (ret_comp), finder, data);
3a93a0c2
KS
509 break;
510
511 default:
512 break;
513 }
514 }
515}
516
517/* Parse STRING and convert it to canonical form, resolving any typedefs.
518 If parsing fails, or if STRING is already canonical, return NULL.
519 Otherwise return the canonical form. The return value is allocated via
2621e0fd
TT
520 xmalloc. If FINDER is not NULL, then type components are passed to
521 FINDER to be looked up. DATA is passed verbatim to FINDER. */
3a93a0c2
KS
522
523char *
2621e0fd
TT
524cp_canonicalize_string_full (const char *string,
525 canonicalization_ftype *finder,
526 void *data)
3a93a0c2
KS
527{
528 char *ret;
529 unsigned int estimated_len;
530 struct demangle_parse_info *info;
531
532 ret = NULL;
533 estimated_len = strlen (string) * 2;
534 info = cp_demangled_name_to_comp (string, NULL);
535 if (info != NULL)
536 {
537 /* Replace all the typedefs in the tree. */
2621e0fd 538 replace_typedefs (info, info->tree, finder, data);
3a93a0c2
KS
539
540 /* Convert the tree back into a string. */
541 ret = cp_comp_to_string (info->tree, estimated_len);
542 gdb_assert (ret != NULL);
543
544 /* Free the parse information. */
545 cp_demangled_name_parse_free (info);
546
547 /* Finally, compare the original string with the computed
548 name, returning NULL if they are the same. */
549 if (strcmp (string, ret) == 0)
550 {
551 xfree (ret);
552 return NULL;
553 }
554 }
555
556 return ret;
557}
558
2621e0fd
TT
559/* Like cp_canonicalize_string_full, but always passes NULL for
560 FINDER. */
561
562char *
563cp_canonicalize_string_no_typedefs (const char *string)
564{
565 return cp_canonicalize_string_full (string, NULL, NULL);
566}
567
f88e9fd3
DJ
568/* Parse STRING and convert it to canonical form. If parsing fails,
569 or if STRING is already canonical, return NULL. Otherwise return
570 the canonical form. The return value is allocated via xmalloc. */
9219021c 571
fb4c6eba
DJ
572char *
573cp_canonicalize_string (const char *string)
574{
3a93a0c2 575 struct demangle_parse_info *info;
f88e9fd3 576 unsigned int estimated_len;
fb4c6eba 577 char *ret;
9219021c 578
f88e9fd3
DJ
579 if (cp_already_canonical (string))
580 return NULL;
9219021c 581
3a93a0c2
KS
582 info = cp_demangled_name_to_comp (string, NULL);
583 if (info == NULL)
fb4c6eba 584 return NULL;
9219021c 585
f88e9fd3 586 estimated_len = strlen (string) * 2;
3a93a0c2
KS
587 ret = cp_comp_to_string (info->tree, estimated_len);
588 cp_demangled_name_parse_free (info);
9219021c 589
9934703b
JK
590 if (ret == NULL)
591 {
592 warning (_("internal error: string \"%s\" failed to be canonicalized"),
593 string);
594 return NULL;
595 }
596
f88e9fd3
DJ
597 if (strcmp (string, ret) == 0)
598 {
599 xfree (ret);
600 return NULL;
601 }
de17c821 602
fb4c6eba
DJ
603 return ret;
604}
de17c821 605
aff410f1
MS
606/* Convert a mangled name to a demangle_component tree. *MEMORY is
607 set to the block of used memory that should be freed when finished
608 with the tree. DEMANGLED_P is set to the char * that should be
609 freed when finished with the tree, or NULL if none was needed.
610 OPTIONS will be passed to the demangler. */
de17c821 611
3a93a0c2 612static struct demangle_parse_info *
fb4c6eba
DJ
613mangled_name_to_comp (const char *mangled_name, int options,
614 void **memory, char **demangled_p)
de17c821 615{
fb4c6eba 616 char *demangled_name;
3a93a0c2 617 struct demangle_parse_info *info;
de17c821 618
fb4c6eba
DJ
619 /* If it looks like a v3 mangled name, then try to go directly
620 to trees. */
621 if (mangled_name[0] == '_' && mangled_name[1] == 'Z')
de17c821 622 {
3a93a0c2
KS
623 struct demangle_component *ret;
624
aff410f1
MS
625 ret = cplus_demangle_v3_components (mangled_name,
626 options, memory);
fb4c6eba
DJ
627 if (ret)
628 {
3a93a0c2
KS
629 info = cp_new_demangle_parse_info ();
630 info->tree = ret;
fb4c6eba 631 *demangled_p = NULL;
3a93a0c2 632 return info;
fb4c6eba 633 }
de17c821
DJ
634 }
635
aff410f1
MS
636 /* If it doesn't, or if that failed, then try to demangle the
637 name. */
fb4c6eba
DJ
638 demangled_name = cplus_demangle (mangled_name, options);
639 if (demangled_name == NULL)
640 return NULL;
641
aff410f1
MS
642 /* If we could demangle the name, parse it to build the component
643 tree. */
3a93a0c2 644 info = cp_demangled_name_to_comp (demangled_name, NULL);
de17c821 645
3a93a0c2 646 if (info == NULL)
fb4c6eba 647 {
6c761d9c 648 xfree (demangled_name);
fb4c6eba
DJ
649 return NULL;
650 }
de17c821 651
fb4c6eba 652 *demangled_p = demangled_name;
3a93a0c2 653 return info;
de17c821
DJ
654}
655
656/* Return the name of the class containing method PHYSNAME. */
657
658char *
31c27f77 659cp_class_name_from_physname (const char *physname)
de17c821 660{
de237128 661 void *storage = NULL;
fb4c6eba 662 char *demangled_name = NULL, *ret;
5e5100cb 663 struct demangle_component *ret_comp, *prev_comp, *cur_comp;
3a93a0c2 664 struct demangle_parse_info *info;
fb4c6eba
DJ
665 int done;
666
3a93a0c2
KS
667 info = mangled_name_to_comp (physname, DMGL_ANSI,
668 &storage, &demangled_name);
669 if (info == NULL)
de17c821
DJ
670 return NULL;
671
fb4c6eba 672 done = 0;
3a93a0c2 673 ret_comp = info->tree;
5e5100cb 674
aff410f1
MS
675 /* First strip off any qualifiers, if we have a function or
676 method. */
fb4c6eba
DJ
677 while (!done)
678 switch (ret_comp->type)
679 {
fb4c6eba
DJ
680 case DEMANGLE_COMPONENT_CONST:
681 case DEMANGLE_COMPONENT_RESTRICT:
682 case DEMANGLE_COMPONENT_VOLATILE:
683 case DEMANGLE_COMPONENT_CONST_THIS:
684 case DEMANGLE_COMPONENT_RESTRICT_THIS:
685 case DEMANGLE_COMPONENT_VOLATILE_THIS:
686 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
fb4c6eba
DJ
687 ret_comp = d_left (ret_comp);
688 break;
5e5100cb
DJ
689 default:
690 done = 1;
691 break;
692 }
693
694 /* If what we have now is a function, discard the argument list. */
695 if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
696 ret_comp = d_left (ret_comp);
697
698 /* If what we have now is a template, strip off the template
699 arguments. The left subtree may be a qualified name. */
700 if (ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE)
701 ret_comp = d_left (ret_comp);
702
aff410f1
MS
703 /* What we have now should be a name, possibly qualified.
704 Additional qualifiers could live in the left subtree or the right
705 subtree. Find the last piece. */
5e5100cb
DJ
706 done = 0;
707 prev_comp = NULL;
708 cur_comp = ret_comp;
709 while (!done)
710 switch (cur_comp->type)
711 {
712 case DEMANGLE_COMPONENT_QUAL_NAME:
713 case DEMANGLE_COMPONENT_LOCAL_NAME:
714 prev_comp = cur_comp;
715 cur_comp = d_right (cur_comp);
716 break;
fb4c6eba 717 case DEMANGLE_COMPONENT_TEMPLATE:
5e5100cb 718 case DEMANGLE_COMPONENT_NAME:
fb4c6eba
DJ
719 case DEMANGLE_COMPONENT_CTOR:
720 case DEMANGLE_COMPONENT_DTOR:
721 case DEMANGLE_COMPONENT_OPERATOR:
722 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
723 done = 1;
724 break;
725 default:
726 done = 1;
5e5100cb 727 cur_comp = NULL;
fb4c6eba
DJ
728 break;
729 }
730
731 ret = NULL;
5e5100cb 732 if (cur_comp != NULL && prev_comp != NULL)
de17c821 733 {
5e5100cb 734 /* We want to discard the rightmost child of PREV_COMP. */
fb4c6eba 735 *prev_comp = *d_left (prev_comp);
aff410f1
MS
736 /* The ten is completely arbitrary; we don't have a good
737 estimate. */
5e5100cb 738 ret = cp_comp_to_string (ret_comp, 10);
de17c821
DJ
739 }
740
fb4c6eba 741 xfree (storage);
3a93a0c2
KS
742 xfree (demangled_name);
743 cp_demangled_name_parse_free (info);
de17c821
DJ
744 return ret;
745}
746
aff410f1
MS
747/* Return the child of COMP which is the basename of a method,
748 variable, et cetera. All scope qualifiers are discarded, but
749 template arguments will be included. The component tree may be
750 modified. */
de17c821 751
5e5100cb
DJ
752static struct demangle_component *
753unqualified_name_from_comp (struct demangle_component *comp)
de17c821 754{
5e5100cb 755 struct demangle_component *ret_comp = comp, *last_template;
fb4c6eba
DJ
756 int done;
757
fb4c6eba 758 done = 0;
5e5100cb 759 last_template = NULL;
fb4c6eba
DJ
760 while (!done)
761 switch (ret_comp->type)
762 {
763 case DEMANGLE_COMPONENT_QUAL_NAME:
764 case DEMANGLE_COMPONENT_LOCAL_NAME:
fb4c6eba
DJ
765 ret_comp = d_right (ret_comp);
766 break;
5e5100cb
DJ
767 case DEMANGLE_COMPONENT_TYPED_NAME:
768 ret_comp = d_left (ret_comp);
769 break;
770 case DEMANGLE_COMPONENT_TEMPLATE:
771 gdb_assert (last_template == NULL);
772 last_template = ret_comp;
773 ret_comp = d_left (ret_comp);
774 break;
fb4c6eba
DJ
775 case DEMANGLE_COMPONENT_CONST:
776 case DEMANGLE_COMPONENT_RESTRICT:
777 case DEMANGLE_COMPONENT_VOLATILE:
778 case DEMANGLE_COMPONENT_CONST_THIS:
779 case DEMANGLE_COMPONENT_RESTRICT_THIS:
780 case DEMANGLE_COMPONENT_VOLATILE_THIS:
781 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
782 ret_comp = d_left (ret_comp);
783 break;
784 case DEMANGLE_COMPONENT_NAME:
fb4c6eba
DJ
785 case DEMANGLE_COMPONENT_CTOR:
786 case DEMANGLE_COMPONENT_DTOR:
787 case DEMANGLE_COMPONENT_OPERATOR:
788 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
789 done = 1;
790 break;
791 default:
5e5100cb 792 return NULL;
fb4c6eba
DJ
793 break;
794 }
795
5e5100cb
DJ
796 if (last_template)
797 {
798 d_left (last_template) = ret_comp;
799 return last_template;
800 }
801
802 return ret_comp;
803}
804
805/* Return the name of the method whose linkage name is PHYSNAME. */
806
807char *
808method_name_from_physname (const char *physname)
809{
de237128 810 void *storage = NULL;
5e5100cb
DJ
811 char *demangled_name = NULL, *ret;
812 struct demangle_component *ret_comp;
3a93a0c2 813 struct demangle_parse_info *info;
5e5100cb 814
3a93a0c2
KS
815 info = mangled_name_to_comp (physname, DMGL_ANSI,
816 &storage, &demangled_name);
817 if (info == NULL)
5e5100cb
DJ
818 return NULL;
819
3a93a0c2 820 ret_comp = unqualified_name_from_comp (info->tree);
5e5100cb 821
fb4c6eba
DJ
822 ret = NULL;
823 if (ret_comp != NULL)
aff410f1
MS
824 /* The ten is completely arbitrary; we don't have a good
825 estimate. */
fb4c6eba
DJ
826 ret = cp_comp_to_string (ret_comp, 10);
827
828 xfree (storage);
3a93a0c2
KS
829 xfree (demangled_name);
830 cp_demangled_name_parse_free (info);
fb4c6eba
DJ
831 return ret;
832}
de17c821 833
5e5100cb
DJ
834/* If FULL_NAME is the demangled name of a C++ function (including an
835 arg list, possibly including namespace/class qualifications),
836 return a new string containing only the function name (without the
837 arg list/class qualifications). Otherwise, return NULL. The
838 caller is responsible for freeing the memory in question. */
839
840char *
841cp_func_name (const char *full_name)
842{
5e5100cb
DJ
843 char *ret;
844 struct demangle_component *ret_comp;
3a93a0c2 845 struct demangle_parse_info *info;
5e5100cb 846
3a93a0c2
KS
847 info = cp_demangled_name_to_comp (full_name, NULL);
848 if (!info)
5e5100cb
DJ
849 return NULL;
850
3a93a0c2 851 ret_comp = unqualified_name_from_comp (info->tree);
5e5100cb
DJ
852
853 ret = NULL;
854 if (ret_comp != NULL)
855 ret = cp_comp_to_string (ret_comp, 10);
856
3a93a0c2 857 cp_demangled_name_parse_free (info);
5e5100cb
DJ
858 return ret;
859}
860
861/* DEMANGLED_NAME is the name of a function, including parameters and
862 (optionally) a return type. Return the name of the function without
863 parameters or return type, or NULL if we can not parse the name. */
864
3567439c
DJ
865char *
866cp_remove_params (const char *demangled_name)
5e5100cb
DJ
867{
868 int done = 0;
869 struct demangle_component *ret_comp;
3a93a0c2 870 struct demangle_parse_info *info;
5e5100cb
DJ
871 char *ret = NULL;
872
873 if (demangled_name == NULL)
874 return NULL;
875
3a93a0c2
KS
876 info = cp_demangled_name_to_comp (demangled_name, NULL);
877 if (info == NULL)
5e5100cb
DJ
878 return NULL;
879
880 /* First strip off any qualifiers, if we have a function or method. */
3a93a0c2 881 ret_comp = info->tree;
5e5100cb
DJ
882 while (!done)
883 switch (ret_comp->type)
884 {
885 case DEMANGLE_COMPONENT_CONST:
886 case DEMANGLE_COMPONENT_RESTRICT:
887 case DEMANGLE_COMPONENT_VOLATILE:
888 case DEMANGLE_COMPONENT_CONST_THIS:
889 case DEMANGLE_COMPONENT_RESTRICT_THIS:
890 case DEMANGLE_COMPONENT_VOLATILE_THIS:
891 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
892 ret_comp = d_left (ret_comp);
893 break;
894 default:
895 done = 1;
896 break;
897 }
898
899 /* What we have now should be a function. Return its name. */
900 if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
901 ret = cp_comp_to_string (d_left (ret_comp), 10);
902
3a93a0c2 903 cp_demangled_name_parse_free (info);
5e5100cb
DJ
904 return ret;
905}
906
fb4c6eba
DJ
907/* Here are some random pieces of trivia to keep in mind while trying
908 to take apart demangled names:
de17c821 909
fb4c6eba
DJ
910 - Names can contain function arguments or templates, so the process
911 has to be, to some extent recursive: maybe keep track of your
912 depth based on encountering <> and ().
913
914 - Parentheses don't just have to happen at the end of a name: they
915 can occur even if the name in question isn't a function, because
916 a template argument might be a type that's a function.
917
918 - Conversely, even if you're trying to deal with a function, its
919 demangled name might not end with ')': it could be a const or
920 volatile class method, in which case it ends with "const" or
921 "volatile".
922
923 - Parentheses are also used in anonymous namespaces: a variable
924 'foo' in an anonymous namespace gets demangled as "(anonymous
925 namespace)::foo".
926
927 - And operator names can contain parentheses or angle brackets. */
928
929/* FIXME: carlton/2003-03-13: We have several functions here with
930 overlapping functionality; can we combine them? Also, do they
931 handle all the above considerations correctly? */
de17c821 932
9219021c
DC
933
934/* This returns the length of first component of NAME, which should be
935 the demangled name of a C++ variable/function/method/etc.
936 Specifically, it returns the index of the first colon forming the
937 boundary of the first component: so, given 'A::foo' or 'A::B::foo'
938 it returns the 1, and given 'foo', it returns 0. */
939
b2a7f303
DC
940/* The character in NAME indexed by the return value is guaranteed to
941 always be either ':' or '\0'. */
9219021c
DC
942
943/* NOTE: carlton/2003-03-13: This function is currently only intended
944 for internal use: it's probably not entirely safe when called on
b2a7f303
DC
945 user-generated input, because some of the 'index += 2' lines in
946 cp_find_first_component_aux might go past the end of malformed
947 input. */
948
949unsigned int
950cp_find_first_component (const char *name)
951{
952 return cp_find_first_component_aux (name, 0);
953}
954
955/* Helper function for cp_find_first_component. Like that function,
956 it returns the length of the first component of NAME, but to make
957 the recursion easier, it also stops if it reaches an unexpected ')'
958 or '>' if the value of PERMISSIVE is nonzero. */
9219021c
DC
959
960/* Let's optimize away calls to strlen("operator"). */
961
962#define LENGTH_OF_OPERATOR 8
963
b2a7f303
DC
964static unsigned int
965cp_find_first_component_aux (const char *name, int permissive)
9219021c 966{
9219021c 967 unsigned int index = 0;
0f20eeea
DC
968 /* Operator names can show up in unexpected places. Since these can
969 contain parentheses or angle brackets, they can screw up the
970 recursion. But not every string 'operator' is part of an
971 operater name: e.g. you could have a variable 'cooperator'. So
972 this variable tells us whether or not we should treat the string
973 'operator' as starting an operator. */
974 int operator_possible = 1;
9219021c
DC
975
976 for (;; ++index)
977 {
978 switch (name[index])
979 {
980 case '<':
981 /* Template; eat it up. The calls to cp_first_component
982 should only return (I hope!) when they reach the '>'
983 terminating the component or a '::' between two
984 components. (Hence the '+ 2'.) */
985 index += 1;
b2a7f303 986 for (index += cp_find_first_component_aux (name + index, 1);
9219021c 987 name[index] != '>';
b2a7f303 988 index += cp_find_first_component_aux (name + index, 1))
9219021c 989 {
b2a7f303
DC
990 if (name[index] != ':')
991 {
992 demangled_name_complaint (name);
993 return strlen (name);
994 }
9219021c
DC
995 index += 2;
996 }
0f20eeea 997 operator_possible = 1;
9219021c
DC
998 break;
999 case '(':
1000 /* Similar comment as to '<'. */
1001 index += 1;
b2a7f303 1002 for (index += cp_find_first_component_aux (name + index, 1);
9219021c 1003 name[index] != ')';
b2a7f303 1004 index += cp_find_first_component_aux (name + index, 1))
9219021c 1005 {
b2a7f303
DC
1006 if (name[index] != ':')
1007 {
1008 demangled_name_complaint (name);
1009 return strlen (name);
1010 }
9219021c
DC
1011 index += 2;
1012 }
0f20eeea 1013 operator_possible = 1;
9219021c
DC
1014 break;
1015 case '>':
1016 case ')':
b2a7f303 1017 if (permissive)
7a20f2c2 1018 return index;
b2a7f303
DC
1019 else
1020 {
1021 demangled_name_complaint (name);
1022 return strlen (name);
1023 }
9219021c
DC
1024 case '\0':
1025 case ':':
1026 return index;
0f20eeea
DC
1027 case 'o':
1028 /* Operator names can screw up the recursion. */
1029 if (operator_possible
aff410f1
MS
1030 && strncmp (name + index, "operator",
1031 LENGTH_OF_OPERATOR) == 0)
0f20eeea
DC
1032 {
1033 index += LENGTH_OF_OPERATOR;
f88e9fd3 1034 while (ISSPACE(name[index]))
0f20eeea
DC
1035 ++index;
1036 switch (name[index])
1037 {
1038 /* Skip over one less than the appropriate number of
1039 characters: the for loop will skip over the last
1040 one. */
1041 case '<':
1042 if (name[index + 1] == '<')
1043 index += 1;
1044 else
1045 index += 0;
1046 break;
1047 case '>':
1048 case '-':
1049 if (name[index + 1] == '>')
1050 index += 1;
1051 else
1052 index += 0;
1053 break;
1054 case '(':
1055 index += 1;
1056 break;
1057 default:
1058 index += 0;
1059 break;
1060 }
1061 }
1062 operator_possible = 0;
1063 break;
1064 case ' ':
1065 case ',':
1066 case '.':
1067 case '&':
1068 case '*':
1069 /* NOTE: carlton/2003-04-18: I'm not sure what the precise
1070 set of relevant characters are here: it's necessary to
1071 include any character that can show up before 'operator'
1072 in a demangled name, and it's safe to include any
1073 character that can't be part of an identifier's name. */
1074 operator_possible = 1;
1075 break;
9219021c 1076 default:
0f20eeea 1077 operator_possible = 0;
9219021c
DC
1078 break;
1079 }
1080 }
1081}
1082
b2a7f303
DC
1083/* Complain about a demangled name that we don't know how to parse.
1084 NAME is the demangled name in question. */
1085
1086static void
1087demangled_name_complaint (const char *name)
1088{
1089 complaint (&symfile_complaints,
1090 "unexpected demangled name '%s'", name);
1091}
1092
9219021c
DC
1093/* If NAME is the fully-qualified name of a C++
1094 function/variable/method/etc., this returns the length of its
1095 entire prefix: all of the namespaces and classes that make up its
1096 name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns
1097 4, given 'foo', it returns 0. */
1098
1099unsigned int
1100cp_entire_prefix_len (const char *name)
1101{
1102 unsigned int current_len = cp_find_first_component (name);
1103 unsigned int previous_len = 0;
1104
1105 while (name[current_len] != '\0')
1106 {
1107 gdb_assert (name[current_len] == ':');
1108 previous_len = current_len;
1109 /* Skip the '::'. */
1110 current_len += 2;
1111 current_len += cp_find_first_component (name + current_len);
1112 }
1113
1114 return previous_len;
1115}
1116
b6429628
DC
1117/* Overload resolution functions. */
1118
8d577d32
DC
1119/* Test to see if SYM is a symbol that we haven't seen corresponding
1120 to a function named OLOAD_NAME. If so, add it to the current
aff410f1 1121 completion list. */
b6429628
DC
1122
1123static void
aff410f1
MS
1124overload_list_add_symbol (struct symbol *sym,
1125 const char *oload_name)
b6429628
DC
1126{
1127 int newsize;
1128 int i;
1129 char *sym_name;
1130
aff410f1
MS
1131 /* If there is no type information, we can't do anything, so
1132 skip. */
b6429628
DC
1133 if (SYMBOL_TYPE (sym) == NULL)
1134 return;
1135
aff410f1 1136 /* skip any symbols that we've already considered. */
b6429628 1137 for (i = 0; i < sym_return_val_index; ++i)
8d577d32
DC
1138 if (strcmp (SYMBOL_LINKAGE_NAME (sym),
1139 SYMBOL_LINKAGE_NAME (sym_return_val[i])) == 0)
b6429628
DC
1140 return;
1141
1142 /* Get the demangled name without parameters */
3567439c 1143 sym_name = cp_remove_params (SYMBOL_NATURAL_NAME (sym));
b6429628
DC
1144 if (!sym_name)
1145 return;
1146
1147 /* skip symbols that cannot match */
1148 if (strcmp (sym_name, oload_name) != 0)
1149 {
1150 xfree (sym_name);
1151 return;
1152 }
1153
1154 xfree (sym_name);
1155
aff410f1
MS
1156 /* We have a match for an overload instance, so add SYM to the
1157 current list of overload instances */
b6429628
DC
1158 if (sym_return_val_index + 3 > sym_return_val_size)
1159 {
1160 newsize = (sym_return_val_size *= 2) * sizeof (struct symbol *);
aff410f1
MS
1161 sym_return_val = (struct symbol **)
1162 xrealloc ((char *) sym_return_val, newsize);
b6429628
DC
1163 }
1164 sym_return_val[sym_return_val_index++] = sym;
1165 sym_return_val[sym_return_val_index] = NULL;
1166}
1167
1168/* Return a null-terminated list of pointers to function symbols that
8d577d32 1169 are named FUNC_NAME and are visible within NAMESPACE. */
b6429628
DC
1170
1171struct symbol **
8d577d32
DC
1172make_symbol_overload_list (const char *func_name,
1173 const char *namespace)
b6429628 1174{
8d577d32 1175 struct cleanup *old_cleanups;
245040d7 1176 const char *name;
b6429628 1177
8d577d32
DC
1178 sym_return_val_size = 100;
1179 sym_return_val_index = 0;
1180 sym_return_val = xmalloc ((sym_return_val_size + 1) *
1181 sizeof (struct symbol *));
1182 sym_return_val[0] = NULL;
b6429628 1183
8d577d32
DC
1184 old_cleanups = make_cleanup (xfree, sym_return_val);
1185
1186 make_symbol_overload_list_using (func_name, namespace);
1187
245040d7
SW
1188 if (namespace[0] == '\0')
1189 name = func_name;
1190 else
1191 {
1192 char *concatenated_name
1193 = alloca (strlen (namespace) + 2 + strlen (func_name) + 1);
1194 strcpy (concatenated_name, namespace);
1195 strcat (concatenated_name, "::");
1196 strcat (concatenated_name, func_name);
1197 name = concatenated_name;
1198 }
1199
1200 make_symbol_overload_list_qualified (name);
1201
8d577d32
DC
1202 discard_cleanups (old_cleanups);
1203
1204 return sym_return_val;
1205}
1206
245040d7
SW
1207/* Add all symbols with a name matching NAME in BLOCK to the overload
1208 list. */
1209
1210static void
1211make_symbol_overload_list_block (const char *name,
1212 const struct block *block)
1213{
8157b174 1214 struct block_iterator iter;
245040d7
SW
1215 struct symbol *sym;
1216
8157b174 1217 for (sym = block_iter_name_first (block, name, &iter);
245040d7 1218 sym != NULL;
8157b174 1219 sym = block_iter_name_next (name, &iter))
245040d7
SW
1220 overload_list_add_symbol (sym, name);
1221}
1222
7322dca9
SW
1223/* Adds the function FUNC_NAME from NAMESPACE to the overload set. */
1224
1225static void
1226make_symbol_overload_list_namespace (const char *func_name,
1227 const char *namespace)
1228{
245040d7
SW
1229 const char *name;
1230 const struct block *block = NULL;
1231
7322dca9 1232 if (namespace[0] == '\0')
245040d7 1233 name = func_name;
7322dca9
SW
1234 else
1235 {
1236 char *concatenated_name
1237 = alloca (strlen (namespace) + 2 + strlen (func_name) + 1);
c5504eaf 1238
7322dca9
SW
1239 strcpy (concatenated_name, namespace);
1240 strcat (concatenated_name, "::");
1241 strcat (concatenated_name, func_name);
245040d7 1242 name = concatenated_name;
7322dca9 1243 }
245040d7
SW
1244
1245 /* Look in the static block. */
1246 block = block_static_block (get_selected_block (0));
eeaafae2
JK
1247 if (block)
1248 make_symbol_overload_list_block (name, block);
245040d7
SW
1249
1250 /* Look in the global block. */
1251 block = block_global_block (block);
eeaafae2
JK
1252 if (block)
1253 make_symbol_overload_list_block (name, block);
245040d7 1254
7322dca9
SW
1255}
1256
aff410f1
MS
1257/* Search the namespace of the given type and namespace of and public
1258 base types. */
7322dca9
SW
1259
1260static void
1261make_symbol_overload_list_adl_namespace (struct type *type,
1262 const char *func_name)
1263{
1264 char *namespace;
0d5cff50 1265 const char *type_name;
7322dca9
SW
1266 int i, prefix_len;
1267
aff410f1
MS
1268 while (TYPE_CODE (type) == TYPE_CODE_PTR
1269 || TYPE_CODE (type) == TYPE_CODE_REF
7322dca9
SW
1270 || TYPE_CODE (type) == TYPE_CODE_ARRAY
1271 || TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1272 {
1273 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1274 type = check_typedef(type);
1275 else
1276 type = TYPE_TARGET_TYPE (type);
1277 }
1278
1279 type_name = TYPE_NAME (type);
1280
7d3fe98e
SW
1281 if (type_name == NULL)
1282 return;
1283
7322dca9
SW
1284 prefix_len = cp_entire_prefix_len (type_name);
1285
1286 if (prefix_len != 0)
1287 {
1288 namespace = alloca (prefix_len + 1);
1289 strncpy (namespace, type_name, prefix_len);
1290 namespace[prefix_len] = '\0';
1291
1292 make_symbol_overload_list_namespace (func_name, namespace);
1293 }
1294
1295 /* Check public base type */
1296 if (TYPE_CODE (type) == TYPE_CODE_CLASS)
1297 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
1298 {
1299 if (BASETYPE_VIA_PUBLIC (type, i))
aff410f1
MS
1300 make_symbol_overload_list_adl_namespace (TYPE_BASECLASS (type,
1301 i),
7322dca9
SW
1302 func_name);
1303 }
1304}
1305
b021a221 1306/* Adds the overload list overload candidates for FUNC_NAME found
aff410f1 1307 through argument dependent lookup. */
7322dca9
SW
1308
1309struct symbol **
1310make_symbol_overload_list_adl (struct type **arg_types, int nargs,
1311 const char *func_name)
1312{
1313 int i;
1314
1315 gdb_assert (sym_return_val_size != -1);
1316
1317 for (i = 1; i <= nargs; i++)
aff410f1
MS
1318 make_symbol_overload_list_adl_namespace (arg_types[i - 1],
1319 func_name);
7322dca9
SW
1320
1321 return sym_return_val;
1322}
1323
aff410f1
MS
1324/* Used for cleanups to reset the "searched" flag in case of an
1325 error. */
19c0c0f8
UW
1326
1327static void
1328reset_directive_searched (void *data)
1329{
1330 struct using_direct *direct = data;
1331 direct->searched = 0;
1332}
1333
8d577d32
DC
1334/* This applies the using directives to add namespaces to search in,
1335 and then searches for overloads in all of those namespaces. It
1336 adds the symbols found to sym_return_val. Arguments are as in
1337 make_symbol_overload_list. */
1338
1339static void
1340make_symbol_overload_list_using (const char *func_name,
1341 const char *namespace)
1342{
19c0c0f8 1343 struct using_direct *current;
4c3376c8 1344 const struct block *block;
8d577d32
DC
1345
1346 /* First, go through the using directives. If any of them apply,
1347 look in the appropriate namespaces for new functions to match
1348 on. */
b6429628 1349
4c3376c8
SW
1350 for (block = get_selected_block (0);
1351 block != NULL;
1352 block = BLOCK_SUPERBLOCK (block))
1353 for (current = block_using (block);
1354 current != NULL;
1355 current = current->next)
1356 {
19c0c0f8
UW
1357 /* Prevent recursive calls. */
1358 if (current->searched)
1359 continue;
1360
aff410f1
MS
1361 /* If this is a namespace alias or imported declaration ignore
1362 it. */
4c3376c8
SW
1363 if (current->alias != NULL || current->declaration != NULL)
1364 continue;
1365
1366 if (strcmp (namespace, current->import_dest) == 0)
19c0c0f8 1367 {
aff410f1
MS
1368 /* Mark this import as searched so that the recursive call
1369 does not search it again. */
19c0c0f8
UW
1370 struct cleanup *old_chain;
1371 current->searched = 1;
aff410f1
MS
1372 old_chain = make_cleanup (reset_directive_searched,
1373 current);
19c0c0f8 1374
aff410f1
MS
1375 make_symbol_overload_list_using (func_name,
1376 current->import_src);
19c0c0f8
UW
1377
1378 current->searched = 0;
1379 discard_cleanups (old_chain);
1380 }
4c3376c8 1381 }
b6429628 1382
8d577d32 1383 /* Now, add names for this namespace. */
7322dca9 1384 make_symbol_overload_list_namespace (func_name, namespace);
8d577d32 1385}
b6429628 1386
8d577d32
DC
1387/* This does the bulk of the work of finding overloaded symbols.
1388 FUNC_NAME is the name of the overloaded function we're looking for
1389 (possibly including namespace info). */
b6429628 1390
8d577d32
DC
1391static void
1392make_symbol_overload_list_qualified (const char *func_name)
1393{
8d577d32
DC
1394 struct symtab *s;
1395 struct objfile *objfile;
1396 const struct block *b, *surrounding_static_block = 0;
b6429628 1397
aff410f1
MS
1398 /* Look through the partial symtabs for all symbols which begin by
1399 matching FUNC_NAME. Make sure we read that symbol table in. */
b6429628 1400
ccefe4c4
TT
1401 ALL_OBJFILES (objfile)
1402 {
1403 if (objfile->sf)
1404 objfile->sf->qf->expand_symtabs_for_function (objfile, func_name);
1405 }
b6429628
DC
1406
1407 /* Search upwards from currently selected frame (so that we can
1408 complete on local vars. */
1409
1410 for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
245040d7 1411 make_symbol_overload_list_block (func_name, b);
b6429628 1412
8d577d32
DC
1413 surrounding_static_block = block_static_block (get_selected_block (0));
1414
b6429628
DC
1415 /* Go through the symtabs and check the externs and statics for
1416 symbols which match. */
1417
11309657 1418 ALL_PRIMARY_SYMTABS (objfile, s)
b6429628
DC
1419 {
1420 QUIT;
1421 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
245040d7 1422 make_symbol_overload_list_block (func_name, b);
b6429628
DC
1423 }
1424
11309657 1425 ALL_PRIMARY_SYMTABS (objfile, s)
b6429628
DC
1426 {
1427 QUIT;
1428 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
1429 /* Don't do this block twice. */
1430 if (b == surrounding_static_block)
1431 continue;
245040d7 1432 make_symbol_overload_list_block (func_name, b);
b6429628 1433 }
8d577d32
DC
1434}
1435
aff410f1 1436/* Lookup the rtti type for a class name. */
362ff856
MC
1437
1438struct type *
1439cp_lookup_rtti_type (const char *name, struct block *block)
1440{
1441 struct symbol * rtti_sym;
1442 struct type * rtti_type;
1443
2570f2b7 1444 rtti_sym = lookup_symbol (name, block, STRUCT_DOMAIN, NULL);
362ff856
MC
1445
1446 if (rtti_sym == NULL)
1447 {
8a3fe4f8 1448 warning (_("RTTI symbol not found for class '%s'"), name);
362ff856
MC
1449 return NULL;
1450 }
1451
1452 if (SYMBOL_CLASS (rtti_sym) != LOC_TYPEDEF)
1453 {
8a3fe4f8 1454 warning (_("RTTI symbol for class '%s' is not a type"), name);
362ff856
MC
1455 return NULL;
1456 }
1457
1458 rtti_type = SYMBOL_TYPE (rtti_sym);
1459
1460 switch (TYPE_CODE (rtti_type))
1461 {
1462 case TYPE_CODE_CLASS:
1463 break;
1464 case TYPE_CODE_NAMESPACE:
1465 /* chastain/2003-11-26: the symbol tables often contain fake
1466 symbols for namespaces with the same name as the struct.
1467 This warning is an indication of a bug in the lookup order
1468 or a bug in the way that the symbol tables are populated. */
8a3fe4f8 1469 warning (_("RTTI symbol for class '%s' is a namespace"), name);
362ff856
MC
1470 return NULL;
1471 default:
8a3fe4f8 1472 warning (_("RTTI symbol for class '%s' has bad type"), name);
362ff856
MC
1473 return NULL;
1474 }
1475
1476 return rtti_type;
1477}
b6429628 1478
9219021c
DC
1479/* Don't allow just "maintenance cplus". */
1480
1481static void
1482maint_cplus_command (char *arg, int from_tty)
1483{
3e43a32a
MS
1484 printf_unfiltered (_("\"maintenance cplus\" must be followed "
1485 "by the name of a command.\n"));
aff410f1
MS
1486 help_list (maint_cplus_cmd_list,
1487 "maintenance cplus ",
1488 -1, gdb_stdout);
9219021c
DC
1489}
1490
1491/* This is a front end for cp_find_first_component, for unit testing.
1492 Be careful when using it: see the NOTE above
1493 cp_find_first_component. */
1494
1495static void
1496first_component_command (char *arg, int from_tty)
1497{
c836824f
AR
1498 int len;
1499 char *prefix;
1500
1501 if (!arg)
1502 return;
1503
1504 len = cp_find_first_component (arg);
1505 prefix = alloca (len + 1);
9219021c
DC
1506
1507 memcpy (prefix, arg, len);
1508 prefix[len] = '\0';
1509
1510 printf_unfiltered ("%s\n", prefix);
1511}
1512
b9362cc7
AC
1513extern initialize_file_ftype _initialize_cp_support; /* -Wmissing-prototypes */
1514
12907978 1515
57651221 1516/* Implement "info vtbl". */
c4aeac85
TT
1517
1518static void
1519info_vtbl_command (char *arg, int from_tty)
1520{
1521 struct value *value;
1522
1523 value = parse_and_eval (arg);
1524 cplus_print_vtable (value);
1525}
1526
9219021c
DC
1527void
1528_initialize_cp_support (void)
1529{
aff410f1
MS
1530 add_prefix_cmd ("cplus", class_maintenance,
1531 maint_cplus_command,
1532 _("C++ maintenance commands."),
1533 &maint_cplus_cmd_list,
1534 "maintenance cplus ",
1535 0, &maintenancelist);
1536 add_alias_cmd ("cp", "cplus",
1537 class_maintenance, 1,
1538 &maintenancelist);
1539
1540 add_cmd ("first_component",
1541 class_maintenance,
1542 first_component_command,
1a966eab 1543 _("Print the first class/namespace component of NAME."),
9219021c 1544 &maint_cplus_cmd_list);
c4aeac85
TT
1545
1546 add_info ("vtbl", info_vtbl_command,
57651221 1547 _("Show the virtual function table for a C++ object.\n\
c4aeac85
TT
1548Usage: info vtbl EXPRESSION\n\
1549Evaluate EXPRESSION and display the virtual function table for the\n\
1550resulting object."));
9219021c 1551}