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