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