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