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