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