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