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