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