]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/cp-support.c
Remove TYPE_TAG_NAME
[thirdparty/binutils-gdb.git] / gdb / cp-support.c
CommitLineData
de17c821 1/* Helper routines for C++ support in GDB.
e2882c85 2 Copyright (C) 2002-2018 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 38#include "safe-ctype.h"
c62446b1 39#include "selftest.h"
f88e9fd3 40
fb4c6eba
DJ
41#define d_left(dc) (dc)->u.s_binary.left
42#define d_right(dc) (dc)->u.s_binary.right
b2a7f303 43
fb4c6eba 44/* Functions related to demangled name parsing. */
b2a7f303
DC
45
46static unsigned int cp_find_first_component_aux (const char *name,
47 int permissive);
48
49static void demangled_name_complaint (const char *name);
b6429628
DC
50
51/* Functions/variables related to overload resolution. */
52
7322dca9 53static int sym_return_val_size = -1;
b6429628
DC
54static int sym_return_val_index;
55static struct symbol **sym_return_val;
56
8d577d32
DC
57static void overload_list_add_symbol (struct symbol *sym,
58 const char *oload_name);
59
60static void make_symbol_overload_list_using (const char *func_name,
fe978cb0 61 const char *the_namespace);
8d577d32
DC
62
63static void make_symbol_overload_list_qualified (const char *func_name);
64
9219021c
DC
65/* The list of "maint cplus" commands. */
66
5c4e30ca 67struct cmd_list_element *maint_cplus_cmd_list = NULL;
9219021c 68
3a93a0c2
KS
69/* A list of typedefs which should not be substituted by replace_typedefs. */
70static const char * const ignore_typedefs[] =
71 {
72 "std::istream", "std::iostream", "std::ostream", "std::string"
73 };
74
75static void
76 replace_typedefs (struct demangle_parse_info *info,
2621e0fd
TT
77 struct demangle_component *ret_comp,
78 canonicalization_ftype *finder,
79 void *data);
3a93a0c2
KS
80
81/* A convenience function to copy STRING into OBSTACK, returning a pointer
82 to the newly allocated string and saving the number of bytes saved in LEN.
83
84 It does not copy the terminating '\0' byte! */
85
86static char *
87copy_string_to_obstack (struct obstack *obstack, const char *string,
88 long *len)
89{
90 *len = strlen (string);
224c3ddb 91 return (char *) obstack_copy (obstack, string, *len);
3a93a0c2
KS
92}
93
f88e9fd3
DJ
94/* Return 1 if STRING is clearly already in canonical form. This
95 function is conservative; things which it does not recognize are
96 assumed to be non-canonical, and the parser will sort them out
97 afterwards. This speeds up the critical path for alphanumeric
98 identifiers. */
99
100static int
101cp_already_canonical (const char *string)
102{
103 /* Identifier start character [a-zA-Z_]. */
104 if (!ISIDST (string[0]))
105 return 0;
106
107 /* These are the only two identifiers which canonicalize to other
108 than themselves or an error: unsigned -> unsigned int and
109 signed -> int. */
110 if (string[0] == 'u' && strcmp (&string[1], "nsigned") == 0)
111 return 0;
112 else if (string[0] == 's' && strcmp (&string[1], "igned") == 0)
113 return 0;
114
115 /* Identifier character [a-zA-Z0-9_]. */
116 while (ISIDNUM (string[1]))
117 string++;
118
119 if (string[1] == '\0')
120 return 1;
121 else
122 return 0;
123}
9219021c 124
3a93a0c2
KS
125/* Inspect the given RET_COMP for its type. If it is a typedef,
126 replace the node with the typedef's tree.
127
128 Returns 1 if any typedef substitutions were made, 0 otherwise. */
129
130static int
131inspect_type (struct demangle_parse_info *info,
2621e0fd
TT
132 struct demangle_component *ret_comp,
133 canonicalization_ftype *finder,
134 void *data)
3a93a0c2
KS
135{
136 int i;
137 char *name;
138 struct symbol *sym;
3a93a0c2
KS
139
140 /* Copy the symbol's name from RET_COMP and look it up
141 in the symbol table. */
142 name = (char *) alloca (ret_comp->u.s_name.len + 1);
143 memcpy (name, ret_comp->u.s_name.s, ret_comp->u.s_name.len);
144 name[ret_comp->u.s_name.len] = '\0';
145
146 /* Ignore any typedefs that should not be substituted. */
147 for (i = 0; i < ARRAY_SIZE (ignore_typedefs); ++i)
148 {
149 if (strcmp (name, ignore_typedefs[i]) == 0)
150 return 0;
151 }
152
153 sym = NULL;
3a93a0c2 154
492d29ea
PA
155 TRY
156 {
d12307c1 157 sym = lookup_symbol (name, 0, VAR_DOMAIN, 0).symbol;
492d29ea
PA
158 }
159 CATCH (except, RETURN_MASK_ALL)
160 {
161 return 0;
162 }
163 END_CATCH
164
165 if (sym != NULL)
3a93a0c2
KS
166 {
167 struct type *otype = SYMBOL_TYPE (sym);
168
2621e0fd
TT
169 if (finder != NULL)
170 {
171 const char *new_name = (*finder) (otype, data);
172
173 if (new_name != NULL)
174 {
175 ret_comp->u.s_name.s = new_name;
176 ret_comp->u.s_name.len = strlen (new_name);
177 return 1;
178 }
179
180 return 0;
181 }
182
74921315
KS
183 /* If the type is a typedef or namespace alias, replace it. */
184 if (TYPE_CODE (otype) == TYPE_CODE_TYPEDEF
185 || TYPE_CODE (otype) == TYPE_CODE_NAMESPACE)
3a93a0c2
KS
186 {
187 long len;
188 int is_anon;
189 struct type *type;
c8b23b3f 190 std::unique_ptr<demangle_parse_info> i;
3a93a0c2
KS
191
192 /* Get the real type of the typedef. */
193 type = check_typedef (otype);
194
74921315
KS
195 /* If the symbol is a namespace and its type name is no different
196 than the name we looked up, this symbol is not a namespace
197 alias and does not need to be substituted. */
198 if (TYPE_CODE (otype) == TYPE_CODE_NAMESPACE
199 && strcmp (TYPE_NAME (type), name) == 0)
200 return 0;
201
e86ca25f 202 is_anon = (TYPE_NAME (type) == NULL
3a93a0c2
KS
203 && (TYPE_CODE (type) == TYPE_CODE_ENUM
204 || TYPE_CODE (type) == TYPE_CODE_STRUCT
205 || TYPE_CODE (type) == TYPE_CODE_UNION));
206 if (is_anon)
207 {
208 struct type *last = otype;
209
210 /* Find the last typedef for the type. */
211 while (TYPE_TARGET_TYPE (last) != NULL
212 && (TYPE_CODE (TYPE_TARGET_TYPE (last))
213 == TYPE_CODE_TYPEDEF))
214 last = TYPE_TARGET_TYPE (last);
215
216 /* If there is only one typedef for this anonymous type,
217 do not substitute it. */
218 if (type == otype)
219 return 0;
220 else
221 /* Use the last typedef seen as the type for this
222 anonymous type. */
223 type = last;
224 }
225
d7e74731 226 string_file buf;
492d29ea 227 TRY
d7e74731
PA
228 {
229 type_print (type, "", &buf, -1);
230 }
3a93a0c2
KS
231 /* If type_print threw an exception, there is little point
232 in continuing, so just bow out gracefully. */
492d29ea 233 CATCH (except, RETURN_MASK_ERROR)
3a93a0c2 234 {
3a93a0c2
KS
235 return 0;
236 }
492d29ea 237 END_CATCH
3a93a0c2 238
d7e74731
PA
239 len = buf.size ();
240 name = (char *) obstack_copy0 (&info->obstack, buf.c_str (), len);
3a93a0c2
KS
241
242 /* Turn the result into a new tree. Note that this
243 tree will contain pointers into NAME, so NAME cannot
244 be free'd until all typedef conversion is done and
245 the final result is converted into a string. */
246 i = cp_demangled_name_to_comp (name, NULL);
247 if (i != NULL)
248 {
249 /* Merge the two trees. */
c8b23b3f 250 cp_merge_demangle_parse_infos (info, ret_comp, i.get ());
3a93a0c2
KS
251
252 /* Replace any newly introduced typedefs -- but not
253 if the type is anonymous (that would lead to infinite
254 looping). */
255 if (!is_anon)
2621e0fd 256 replace_typedefs (info, ret_comp, finder, data);
3a93a0c2
KS
257 }
258 else
259 {
260 /* This shouldn't happen unless the type printer has
261 output something that the name parser cannot grok.
262 Nonetheless, an ounce of prevention...
263
264 Canonicalize the name again, and store it in the
265 current node (RET_COMP). */
2f408ecb 266 std::string canon = cp_canonicalize_string_no_typedefs (name);
3a93a0c2 267
2f408ecb 268 if (!canon.empty ())
3a93a0c2 269 {
2f408ecb
PA
270 /* Copy the canonicalization into the obstack. */
271 name = copy_string_to_obstack (&info->obstack, canon.c_str (), &len);
3a93a0c2
KS
272 }
273
274 ret_comp->u.s_name.s = name;
275 ret_comp->u.s_name.len = len;
276 }
277
278 return 1;
279 }
280 }
281
282 return 0;
283}
284
285/* Replace any typedefs appearing in the qualified name
286 (DEMANGLE_COMPONENT_QUAL_NAME) represented in RET_COMP for the name parse
287 given in INFO. */
288
289static void
290replace_typedefs_qualified_name (struct demangle_parse_info *info,
2621e0fd
TT
291 struct demangle_component *ret_comp,
292 canonicalization_ftype *finder,
293 void *data)
3a93a0c2 294{
d7e74731 295 string_file buf;
3a93a0c2
KS
296 struct demangle_component *comp = ret_comp;
297
298 /* Walk each node of the qualified name, reconstructing the name of
299 this element. With every node, check for any typedef substitutions.
300 If a substitution has occurred, replace the qualified name node
301 with a DEMANGLE_COMPONENT_NAME node representing the new, typedef-
302 substituted name. */
303 while (comp->type == DEMANGLE_COMPONENT_QUAL_NAME)
304 {
305 if (d_left (comp)->type == DEMANGLE_COMPONENT_NAME)
306 {
fe978cb0 307 struct demangle_component newobj;
3a93a0c2 308
d7e74731 309 buf.write (d_left (comp)->u.s_name.s, d_left (comp)->u.s_name.len);
fe978cb0 310 newobj.type = DEMANGLE_COMPONENT_NAME;
29592bde
PA
311 newobj.u.s_name.s
312 = (char *) obstack_copy0 (&info->obstack,
313 buf.c_str (), buf.size ());
314 newobj.u.s_name.len = buf.size ();
fe978cb0 315 if (inspect_type (info, &newobj, finder, data))
3a93a0c2 316 {
29592bde 317 char *s;
3a93a0c2
KS
318 long slen;
319
320 /* A typedef was substituted in NEW. Convert it to a
321 string and replace the top DEMANGLE_COMPONENT_QUAL_NAME
322 node. */
323
d7e74731 324 buf.clear ();
29592bde
PA
325 gdb::unique_xmalloc_ptr<char> n
326 = cp_comp_to_string (&newobj, 100);
3a93a0c2
KS
327 if (n == NULL)
328 {
329 /* If something went astray, abort typedef substitutions. */
3a93a0c2
KS
330 return;
331 }
332
29592bde 333 s = copy_string_to_obstack (&info->obstack, n.get (), &slen);
3a93a0c2
KS
334
335 d_left (ret_comp)->type = DEMANGLE_COMPONENT_NAME;
336 d_left (ret_comp)->u.s_name.s = s;
337 d_left (ret_comp)->u.s_name.len = slen;
338 d_right (ret_comp) = d_right (comp);
339 comp = ret_comp;
340 continue;
341 }
342 }
343 else
344 {
345 /* The current node is not a name, so simply replace any
346 typedefs in it. Then print it to the stream to continue
347 checking for more typedefs in the tree. */
2621e0fd 348 replace_typedefs (info, d_left (comp), finder, data);
29592bde
PA
349 gdb::unique_xmalloc_ptr<char> name
350 = cp_comp_to_string (d_left (comp), 100);
3a93a0c2
KS
351 if (name == NULL)
352 {
353 /* If something went astray, abort typedef substitutions. */
3a93a0c2
KS
354 return;
355 }
29592bde 356 buf.puts (name.get ());
3a93a0c2 357 }
2621e0fd 358
d7e74731 359 buf.write ("::", 2);
3a93a0c2
KS
360 comp = d_right (comp);
361 }
362
363 /* If the next component is DEMANGLE_COMPONENT_NAME, save the qualified
364 name assembled above and append the name given by COMP. Then use this
365 reassembled name to check for a typedef. */
366
367 if (comp->type == DEMANGLE_COMPONENT_NAME)
368 {
d7e74731 369 buf.write (comp->u.s_name.s, comp->u.s_name.len);
3a93a0c2
KS
370
371 /* Replace the top (DEMANGLE_COMPONENT_QUAL_NAME) node
372 with a DEMANGLE_COMPONENT_NAME node containing the whole
373 name. */
374 ret_comp->type = DEMANGLE_COMPONENT_NAME;
29592bde
PA
375 ret_comp->u.s_name.s
376 = (char *) obstack_copy0 (&info->obstack,
377 buf.c_str (), buf.size ());
378 ret_comp->u.s_name.len = buf.size ();
2621e0fd 379 inspect_type (info, ret_comp, finder, data);
3a93a0c2
KS
380 }
381 else
2621e0fd 382 replace_typedefs (info, comp, finder, data);
3a93a0c2
KS
383}
384
385
386/* A function to check const and volatile qualifiers for argument types.
387
388 "Parameter declarations that differ only in the presence
389 or absence of `const' and/or `volatile' are equivalent."
390 C++ Standard N3290, clause 13.1.3 #4. */
391
392static void
393check_cv_qualifiers (struct demangle_component *ret_comp)
394{
395 while (d_left (ret_comp) != NULL
396 && (d_left (ret_comp)->type == DEMANGLE_COMPONENT_CONST
397 || d_left (ret_comp)->type == DEMANGLE_COMPONENT_VOLATILE))
398 {
399 d_left (ret_comp) = d_left (d_left (ret_comp));
400 }
401}
402
403/* Walk the parse tree given by RET_COMP, replacing any typedefs with
404 their basic types. */
405
406static void
407replace_typedefs (struct demangle_parse_info *info,
2621e0fd
TT
408 struct demangle_component *ret_comp,
409 canonicalization_ftype *finder,
410 void *data)
3a93a0c2
KS
411{
412 if (ret_comp)
413 {
2621e0fd
TT
414 if (finder != NULL
415 && (ret_comp->type == DEMANGLE_COMPONENT_NAME
416 || ret_comp->type == DEMANGLE_COMPONENT_QUAL_NAME
417 || ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE
418 || ret_comp->type == DEMANGLE_COMPONENT_BUILTIN_TYPE))
419 {
29592bde
PA
420 gdb::unique_xmalloc_ptr<char> local_name
421 = cp_comp_to_string (ret_comp, 10);
2621e0fd
TT
422
423 if (local_name != NULL)
424 {
492d29ea 425 struct symbol *sym = NULL;
2621e0fd
TT
426
427 sym = NULL;
492d29ea 428 TRY
2621e0fd 429 {
29592bde
PA
430 sym = lookup_symbol (local_name.get (), 0,
431 VAR_DOMAIN, 0).symbol;
2621e0fd 432 }
492d29ea
PA
433 CATCH (except, RETURN_MASK_ALL)
434 {
435 }
436 END_CATCH
437
492d29ea 438 if (sym != NULL)
2621e0fd
TT
439 {
440 struct type *otype = SYMBOL_TYPE (sym);
441 const char *new_name = (*finder) (otype, data);
442
443 if (new_name != NULL)
444 {
445 ret_comp->type = DEMANGLE_COMPONENT_NAME;
446 ret_comp->u.s_name.s = new_name;
447 ret_comp->u.s_name.len = strlen (new_name);
448 return;
449 }
450 }
451 }
452 }
453
3a93a0c2
KS
454 switch (ret_comp->type)
455 {
456 case DEMANGLE_COMPONENT_ARGLIST:
457 check_cv_qualifiers (ret_comp);
458 /* Fall through */
459
460 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
461 case DEMANGLE_COMPONENT_TEMPLATE:
462 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
463 case DEMANGLE_COMPONENT_TYPED_NAME:
2621e0fd
TT
464 replace_typedefs (info, d_left (ret_comp), finder, data);
465 replace_typedefs (info, d_right (ret_comp), finder, data);
3a93a0c2
KS
466 break;
467
468 case DEMANGLE_COMPONENT_NAME:
2621e0fd 469 inspect_type (info, ret_comp, finder, data);
3a93a0c2
KS
470 break;
471
472 case DEMANGLE_COMPONENT_QUAL_NAME:
2621e0fd 473 replace_typedefs_qualified_name (info, ret_comp, finder, data);
3a93a0c2
KS
474 break;
475
476 case DEMANGLE_COMPONENT_LOCAL_NAME:
477 case DEMANGLE_COMPONENT_CTOR:
478 case DEMANGLE_COMPONENT_ARRAY_TYPE:
479 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
2621e0fd 480 replace_typedefs (info, d_right (ret_comp), finder, data);
3a93a0c2
KS
481 break;
482
483 case DEMANGLE_COMPONENT_CONST:
484 case DEMANGLE_COMPONENT_RESTRICT:
485 case DEMANGLE_COMPONENT_VOLATILE:
486 case DEMANGLE_COMPONENT_VOLATILE_THIS:
487 case DEMANGLE_COMPONENT_CONST_THIS:
488 case DEMANGLE_COMPONENT_RESTRICT_THIS:
489 case DEMANGLE_COMPONENT_POINTER:
490 case DEMANGLE_COMPONENT_REFERENCE:
e4347c89 491 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
2621e0fd 492 replace_typedefs (info, d_left (ret_comp), finder, data);
3a93a0c2
KS
493 break;
494
495 default:
496 break;
497 }
498 }
499}
500
2f408ecb
PA
501/* Parse STRING and convert it to canonical form, resolving any
502 typedefs. If parsing fails, or if STRING is already canonical,
503 return the empty string. Otherwise return the canonical form. If
504 FINDER is not NULL, then type components are passed to FINDER to be
505 looked up. DATA is passed verbatim to FINDER. */
3a93a0c2 506
2f408ecb 507std::string
2621e0fd
TT
508cp_canonicalize_string_full (const char *string,
509 canonicalization_ftype *finder,
510 void *data)
3a93a0c2 511{
2f408ecb 512 std::string ret;
3a93a0c2 513 unsigned int estimated_len;
c8b23b3f 514 std::unique_ptr<demangle_parse_info> info;
3a93a0c2 515
3a93a0c2
KS
516 estimated_len = strlen (string) * 2;
517 info = cp_demangled_name_to_comp (string, NULL);
518 if (info != NULL)
519 {
520 /* Replace all the typedefs in the tree. */
c8b23b3f 521 replace_typedefs (info.get (), info->tree, finder, data);
3a93a0c2
KS
522
523 /* Convert the tree back into a string. */
29592bde
PA
524 gdb::unique_xmalloc_ptr<char> us = cp_comp_to_string (info->tree,
525 estimated_len);
e88e8651 526 gdb_assert (us);
3a93a0c2 527
e88e8651 528 ret = us.get ();
3a93a0c2
KS
529 /* Finally, compare the original string with the computed
530 name, returning NULL if they are the same. */
2f408ecb
PA
531 if (ret == string)
532 return std::string ();
3a93a0c2
KS
533 }
534
535 return ret;
536}
537
2621e0fd
TT
538/* Like cp_canonicalize_string_full, but always passes NULL for
539 FINDER. */
540
2f408ecb 541std::string
2621e0fd
TT
542cp_canonicalize_string_no_typedefs (const char *string)
543{
544 return cp_canonicalize_string_full (string, NULL, NULL);
545}
546
f88e9fd3 547/* Parse STRING and convert it to canonical form. If parsing fails,
2f408ecb
PA
548 or if STRING is already canonical, return the empty string.
549 Otherwise return the canonical form. */
9219021c 550
2f408ecb 551std::string
fb4c6eba
DJ
552cp_canonicalize_string (const char *string)
553{
c8b23b3f 554 std::unique_ptr<demangle_parse_info> info;
f88e9fd3 555 unsigned int estimated_len;
9219021c 556
f88e9fd3 557 if (cp_already_canonical (string))
2f408ecb 558 return std::string ();
9219021c 559
3a93a0c2
KS
560 info = cp_demangled_name_to_comp (string, NULL);
561 if (info == NULL)
2f408ecb 562 return std::string ();
9219021c 563
f88e9fd3 564 estimated_len = strlen (string) * 2;
e88e8651
YQ
565 gdb::unique_xmalloc_ptr<char> us (cp_comp_to_string (info->tree,
566 estimated_len));
9219021c 567
e88e8651 568 if (!us)
9934703b
JK
569 {
570 warning (_("internal error: string \"%s\" failed to be canonicalized"),
571 string);
2f408ecb 572 return std::string ();
9934703b
JK
573 }
574
e88e8651
YQ
575 std::string ret (us.get ());
576
2f408ecb
PA
577 if (ret == string)
578 return std::string ();
de17c821 579
fb4c6eba
DJ
580 return ret;
581}
de17c821 582
aff410f1
MS
583/* Convert a mangled name to a demangle_component tree. *MEMORY is
584 set to the block of used memory that should be freed when finished
585 with the tree. DEMANGLED_P is set to the char * that should be
586 freed when finished with the tree, or NULL if none was needed.
587 OPTIONS will be passed to the demangler. */
de17c821 588
c8b23b3f 589static std::unique_ptr<demangle_parse_info>
fb4c6eba
DJ
590mangled_name_to_comp (const char *mangled_name, int options,
591 void **memory, char **demangled_p)
de17c821 592{
fb4c6eba 593 char *demangled_name;
de17c821 594
fb4c6eba
DJ
595 /* If it looks like a v3 mangled name, then try to go directly
596 to trees. */
597 if (mangled_name[0] == '_' && mangled_name[1] == 'Z')
de17c821 598 {
3a93a0c2
KS
599 struct demangle_component *ret;
600
aff410f1
MS
601 ret = cplus_demangle_v3_components (mangled_name,
602 options, memory);
fb4c6eba
DJ
603 if (ret)
604 {
c8b23b3f 605 std::unique_ptr<demangle_parse_info> info (new demangle_parse_info);
3a93a0c2 606 info->tree = ret;
fb4c6eba 607 *demangled_p = NULL;
3a93a0c2 608 return info;
fb4c6eba 609 }
de17c821
DJ
610 }
611
aff410f1
MS
612 /* If it doesn't, or if that failed, then try to demangle the
613 name. */
8de20a37 614 demangled_name = gdb_demangle (mangled_name, options);
fb4c6eba
DJ
615 if (demangled_name == NULL)
616 return NULL;
617
aff410f1
MS
618 /* If we could demangle the name, parse it to build the component
619 tree. */
c8b23b3f
TT
620 std::unique_ptr<demangle_parse_info> info
621 = cp_demangled_name_to_comp (demangled_name, NULL);
de17c821 622
3a93a0c2 623 if (info == NULL)
fb4c6eba 624 {
6c761d9c 625 xfree (demangled_name);
fb4c6eba
DJ
626 return NULL;
627 }
de17c821 628
fb4c6eba 629 *demangled_p = demangled_name;
3a93a0c2 630 return info;
de17c821
DJ
631}
632
633/* Return the name of the class containing method PHYSNAME. */
634
635char *
31c27f77 636cp_class_name_from_physname (const char *physname)
de17c821 637{
de237128 638 void *storage = NULL;
29592bde
PA
639 char *demangled_name = NULL;
640 gdb::unique_xmalloc_ptr<char> ret;
5e5100cb 641 struct demangle_component *ret_comp, *prev_comp, *cur_comp;
c8b23b3f 642 std::unique_ptr<demangle_parse_info> info;
fb4c6eba
DJ
643 int done;
644
3a93a0c2
KS
645 info = mangled_name_to_comp (physname, DMGL_ANSI,
646 &storage, &demangled_name);
647 if (info == NULL)
de17c821
DJ
648 return NULL;
649
fb4c6eba 650 done = 0;
3a93a0c2 651 ret_comp = info->tree;
5e5100cb 652
aff410f1
MS
653 /* First strip off any qualifiers, if we have a function or
654 method. */
fb4c6eba
DJ
655 while (!done)
656 switch (ret_comp->type)
657 {
fb4c6eba
DJ
658 case DEMANGLE_COMPONENT_CONST:
659 case DEMANGLE_COMPONENT_RESTRICT:
660 case DEMANGLE_COMPONENT_VOLATILE:
661 case DEMANGLE_COMPONENT_CONST_THIS:
662 case DEMANGLE_COMPONENT_RESTRICT_THIS:
663 case DEMANGLE_COMPONENT_VOLATILE_THIS:
664 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
fb4c6eba
DJ
665 ret_comp = d_left (ret_comp);
666 break;
5e5100cb
DJ
667 default:
668 done = 1;
669 break;
670 }
671
672 /* If what we have now is a function, discard the argument list. */
673 if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
674 ret_comp = d_left (ret_comp);
675
676 /* If what we have now is a template, strip off the template
677 arguments. The left subtree may be a qualified name. */
678 if (ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE)
679 ret_comp = d_left (ret_comp);
680
aff410f1
MS
681 /* What we have now should be a name, possibly qualified.
682 Additional qualifiers could live in the left subtree or the right
683 subtree. Find the last piece. */
5e5100cb
DJ
684 done = 0;
685 prev_comp = NULL;
686 cur_comp = ret_comp;
687 while (!done)
688 switch (cur_comp->type)
689 {
690 case DEMANGLE_COMPONENT_QUAL_NAME:
691 case DEMANGLE_COMPONENT_LOCAL_NAME:
692 prev_comp = cur_comp;
693 cur_comp = d_right (cur_comp);
694 break;
fb4c6eba 695 case DEMANGLE_COMPONENT_TEMPLATE:
5e5100cb 696 case DEMANGLE_COMPONENT_NAME:
fb4c6eba
DJ
697 case DEMANGLE_COMPONENT_CTOR:
698 case DEMANGLE_COMPONENT_DTOR:
699 case DEMANGLE_COMPONENT_OPERATOR:
700 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
701 done = 1;
702 break;
703 default:
704 done = 1;
5e5100cb 705 cur_comp = NULL;
fb4c6eba
DJ
706 break;
707 }
708
5e5100cb 709 if (cur_comp != NULL && prev_comp != NULL)
de17c821 710 {
5e5100cb 711 /* We want to discard the rightmost child of PREV_COMP. */
fb4c6eba 712 *prev_comp = *d_left (prev_comp);
aff410f1
MS
713 /* The ten is completely arbitrary; we don't have a good
714 estimate. */
5e5100cb 715 ret = cp_comp_to_string (ret_comp, 10);
de17c821
DJ
716 }
717
fb4c6eba 718 xfree (storage);
3a93a0c2 719 xfree (demangled_name);
29592bde 720 return ret.release ();
de17c821
DJ
721}
722
aff410f1
MS
723/* Return the child of COMP which is the basename of a method,
724 variable, et cetera. All scope qualifiers are discarded, but
725 template arguments will be included. The component tree may be
726 modified. */
de17c821 727
5e5100cb
DJ
728static struct demangle_component *
729unqualified_name_from_comp (struct demangle_component *comp)
de17c821 730{
5e5100cb 731 struct demangle_component *ret_comp = comp, *last_template;
fb4c6eba
DJ
732 int done;
733
fb4c6eba 734 done = 0;
5e5100cb 735 last_template = NULL;
fb4c6eba
DJ
736 while (!done)
737 switch (ret_comp->type)
738 {
739 case DEMANGLE_COMPONENT_QUAL_NAME:
740 case DEMANGLE_COMPONENT_LOCAL_NAME:
fb4c6eba
DJ
741 ret_comp = d_right (ret_comp);
742 break;
5e5100cb
DJ
743 case DEMANGLE_COMPONENT_TYPED_NAME:
744 ret_comp = d_left (ret_comp);
745 break;
746 case DEMANGLE_COMPONENT_TEMPLATE:
747 gdb_assert (last_template == NULL);
748 last_template = ret_comp;
749 ret_comp = d_left (ret_comp);
750 break;
fb4c6eba
DJ
751 case DEMANGLE_COMPONENT_CONST:
752 case DEMANGLE_COMPONENT_RESTRICT:
753 case DEMANGLE_COMPONENT_VOLATILE:
754 case DEMANGLE_COMPONENT_CONST_THIS:
755 case DEMANGLE_COMPONENT_RESTRICT_THIS:
756 case DEMANGLE_COMPONENT_VOLATILE_THIS:
757 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
758 ret_comp = d_left (ret_comp);
759 break;
760 case DEMANGLE_COMPONENT_NAME:
fb4c6eba
DJ
761 case DEMANGLE_COMPONENT_CTOR:
762 case DEMANGLE_COMPONENT_DTOR:
763 case DEMANGLE_COMPONENT_OPERATOR:
764 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
765 done = 1;
766 break;
767 default:
5e5100cb 768 return NULL;
fb4c6eba
DJ
769 break;
770 }
771
5e5100cb
DJ
772 if (last_template)
773 {
774 d_left (last_template) = ret_comp;
775 return last_template;
776 }
777
778 return ret_comp;
779}
780
781/* Return the name of the method whose linkage name is PHYSNAME. */
782
783char *
784method_name_from_physname (const char *physname)
785{
de237128 786 void *storage = NULL;
29592bde
PA
787 char *demangled_name = NULL;
788 gdb::unique_xmalloc_ptr<char> ret;
5e5100cb 789 struct demangle_component *ret_comp;
c8b23b3f 790 std::unique_ptr<demangle_parse_info> info;
5e5100cb 791
3a93a0c2
KS
792 info = mangled_name_to_comp (physname, DMGL_ANSI,
793 &storage, &demangled_name);
794 if (info == NULL)
5e5100cb
DJ
795 return NULL;
796
3a93a0c2 797 ret_comp = unqualified_name_from_comp (info->tree);
5e5100cb 798
fb4c6eba 799 if (ret_comp != NULL)
aff410f1
MS
800 /* The ten is completely arbitrary; we don't have a good
801 estimate. */
fb4c6eba
DJ
802 ret = cp_comp_to_string (ret_comp, 10);
803
804 xfree (storage);
3a93a0c2 805 xfree (demangled_name);
29592bde 806 return ret.release ();
fb4c6eba 807}
de17c821 808
5e5100cb
DJ
809/* If FULL_NAME is the demangled name of a C++ function (including an
810 arg list, possibly including namespace/class qualifications),
811 return a new string containing only the function name (without the
812 arg list/class qualifications). Otherwise, return NULL. The
813 caller is responsible for freeing the memory in question. */
814
815char *
816cp_func_name (const char *full_name)
817{
29592bde 818 gdb::unique_xmalloc_ptr<char> ret;
5e5100cb 819 struct demangle_component *ret_comp;
c8b23b3f 820 std::unique_ptr<demangle_parse_info> info;
5e5100cb 821
3a93a0c2
KS
822 info = cp_demangled_name_to_comp (full_name, NULL);
823 if (!info)
5e5100cb
DJ
824 return NULL;
825
3a93a0c2 826 ret_comp = unqualified_name_from_comp (info->tree);
5e5100cb 827
5e5100cb
DJ
828 if (ret_comp != NULL)
829 ret = cp_comp_to_string (ret_comp, 10);
830
29592bde 831 return ret.release ();
5e5100cb
DJ
832}
833
c62446b1
PA
834/* Helper for cp_remove_params. DEMANGLED_NAME is the name of a
835 function, including parameters and (optionally) a return type.
836 Return the name of the function without parameters or return type,
837 or NULL if we can not parse the name. If REQUIRE_PARAMS is false,
838 then tolerate a non-existing or unbalanced parameter list. */
5e5100cb 839
c62446b1
PA
840static gdb::unique_xmalloc_ptr<char>
841cp_remove_params_1 (const char *demangled_name, bool require_params)
5e5100cb 842{
109483d9 843 bool done = false;
5e5100cb 844 struct demangle_component *ret_comp;
c8b23b3f 845 std::unique_ptr<demangle_parse_info> info;
29592bde 846 gdb::unique_xmalloc_ptr<char> ret;
5e5100cb
DJ
847
848 if (demangled_name == NULL)
849 return NULL;
850
3a93a0c2
KS
851 info = cp_demangled_name_to_comp (demangled_name, NULL);
852 if (info == NULL)
5e5100cb
DJ
853 return NULL;
854
855 /* First strip off any qualifiers, if we have a function or method. */
3a93a0c2 856 ret_comp = info->tree;
5e5100cb
DJ
857 while (!done)
858 switch (ret_comp->type)
859 {
860 case DEMANGLE_COMPONENT_CONST:
861 case DEMANGLE_COMPONENT_RESTRICT:
862 case DEMANGLE_COMPONENT_VOLATILE:
863 case DEMANGLE_COMPONENT_CONST_THIS:
864 case DEMANGLE_COMPONENT_RESTRICT_THIS:
865 case DEMANGLE_COMPONENT_VOLATILE_THIS:
866 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
867 ret_comp = d_left (ret_comp);
868 break;
869 default:
109483d9 870 done = true;
5e5100cb
DJ
871 break;
872 }
873
874 /* What we have now should be a function. Return its name. */
875 if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
876 ret = cp_comp_to_string (d_left (ret_comp), 10);
c62446b1
PA
877 else if (!require_params
878 && (ret_comp->type == DEMANGLE_COMPONENT_NAME
879 || ret_comp->type == DEMANGLE_COMPONENT_QUAL_NAME
880 || ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE))
881 ret = cp_comp_to_string (ret_comp, 10);
5e5100cb 882
109483d9 883 return ret;
5e5100cb
DJ
884}
885
c62446b1
PA
886/* DEMANGLED_NAME is the name of a function, including parameters and
887 (optionally) a return type. Return the name of the function
888 without parameters or return type, or NULL if we can not parse the
889 name. */
890
891gdb::unique_xmalloc_ptr<char>
892cp_remove_params (const char *demangled_name)
893{
894 return cp_remove_params_1 (demangled_name, true);
895}
896
897/* See cp-support.h. */
898
899gdb::unique_xmalloc_ptr<char>
900cp_remove_params_if_any (const char *demangled_name, bool completion_mode)
901{
902 /* Trying to remove parameters from the empty string fails. If
903 we're completing / matching everything, avoid returning NULL
904 which would make callers interpret the result as an error. */
905 if (demangled_name[0] == '\0' && completion_mode)
906 return gdb::unique_xmalloc_ptr<char> (xstrdup (""));
907
908 gdb::unique_xmalloc_ptr<char> without_params
909 = cp_remove_params_1 (demangled_name, false);
910
911 if (without_params == NULL && completion_mode)
912 {
913 std::string copy = demangled_name;
914
915 while (!copy.empty ())
916 {
917 copy.pop_back ();
918 without_params = cp_remove_params_1 (copy.c_str (), false);
919 if (without_params != NULL)
920 break;
921 }
922 }
923
924 return without_params;
925}
926
fb4c6eba
DJ
927/* Here are some random pieces of trivia to keep in mind while trying
928 to take apart demangled names:
de17c821 929
fb4c6eba
DJ
930 - Names can contain function arguments or templates, so the process
931 has to be, to some extent recursive: maybe keep track of your
932 depth based on encountering <> and ().
933
934 - Parentheses don't just have to happen at the end of a name: they
935 can occur even if the name in question isn't a function, because
936 a template argument might be a type that's a function.
937
938 - Conversely, even if you're trying to deal with a function, its
939 demangled name might not end with ')': it could be a const or
940 volatile class method, in which case it ends with "const" or
941 "volatile".
942
943 - Parentheses are also used in anonymous namespaces: a variable
944 'foo' in an anonymous namespace gets demangled as "(anonymous
945 namespace)::foo".
946
947 - And operator names can contain parentheses or angle brackets. */
948
949/* FIXME: carlton/2003-03-13: We have several functions here with
950 overlapping functionality; can we combine them? Also, do they
951 handle all the above considerations correctly? */
de17c821 952
9219021c
DC
953
954/* This returns the length of first component of NAME, which should be
955 the demangled name of a C++ variable/function/method/etc.
956 Specifically, it returns the index of the first colon forming the
957 boundary of the first component: so, given 'A::foo' or 'A::B::foo'
958 it returns the 1, and given 'foo', it returns 0. */
959
b2a7f303
DC
960/* The character in NAME indexed by the return value is guaranteed to
961 always be either ':' or '\0'. */
9219021c
DC
962
963/* NOTE: carlton/2003-03-13: This function is currently only intended
964 for internal use: it's probably not entirely safe when called on
b2a7f303
DC
965 user-generated input, because some of the 'index += 2' lines in
966 cp_find_first_component_aux might go past the end of malformed
967 input. */
968
969unsigned int
970cp_find_first_component (const char *name)
971{
972 return cp_find_first_component_aux (name, 0);
973}
974
975/* Helper function for cp_find_first_component. Like that function,
976 it returns the length of the first component of NAME, but to make
977 the recursion easier, it also stops if it reaches an unexpected ')'
978 or '>' if the value of PERMISSIVE is nonzero. */
9219021c 979
b2a7f303
DC
980static unsigned int
981cp_find_first_component_aux (const char *name, int permissive)
9219021c 982{
9219021c 983 unsigned int index = 0;
0f20eeea
DC
984 /* Operator names can show up in unexpected places. Since these can
985 contain parentheses or angle brackets, they can screw up the
986 recursion. But not every string 'operator' is part of an
987 operater name: e.g. you could have a variable 'cooperator'. So
988 this variable tells us whether or not we should treat the string
989 'operator' as starting an operator. */
990 int operator_possible = 1;
9219021c
DC
991
992 for (;; ++index)
993 {
994 switch (name[index])
995 {
996 case '<':
997 /* Template; eat it up. The calls to cp_first_component
998 should only return (I hope!) when they reach the '>'
999 terminating the component or a '::' between two
1000 components. (Hence the '+ 2'.) */
1001 index += 1;
b2a7f303 1002 for (index += cp_find_first_component_aux (name + index, 1);
9219021c 1003 name[index] != '>';
b2a7f303 1004 index += cp_find_first_component_aux (name + index, 1))
9219021c 1005 {
b2a7f303
DC
1006 if (name[index] != ':')
1007 {
1008 demangled_name_complaint (name);
1009 return strlen (name);
1010 }
9219021c
DC
1011 index += 2;
1012 }
0f20eeea 1013 operator_possible = 1;
9219021c
DC
1014 break;
1015 case '(':
1016 /* Similar comment as to '<'. */
1017 index += 1;
b2a7f303 1018 for (index += cp_find_first_component_aux (name + index, 1);
9219021c 1019 name[index] != ')';
b2a7f303 1020 index += cp_find_first_component_aux (name + index, 1))
9219021c 1021 {
b2a7f303
DC
1022 if (name[index] != ':')
1023 {
1024 demangled_name_complaint (name);
1025 return strlen (name);
1026 }
9219021c
DC
1027 index += 2;
1028 }
0f20eeea 1029 operator_possible = 1;
9219021c
DC
1030 break;
1031 case '>':
1032 case ')':
b2a7f303 1033 if (permissive)
7a20f2c2 1034 return index;
b2a7f303
DC
1035 else
1036 {
1037 demangled_name_complaint (name);
1038 return strlen (name);
1039 }
9219021c 1040 case '\0':
9219021c 1041 return index;
1cafadb4
DB
1042 case ':':
1043 /* ':' marks a component iff the next character is also a ':'.
1044 Otherwise it is probably malformed input. */
1045 if (name[index + 1] == ':')
1046 return index;
1047 break;
0f20eeea
DC
1048 case 'o':
1049 /* Operator names can screw up the recursion. */
1050 if (operator_possible
8090b426 1051 && startswith (name + index, CP_OPERATOR_STR))
0f20eeea 1052 {
8090b426 1053 index += CP_OPERATOR_LEN;
f88e9fd3 1054 while (ISSPACE(name[index]))
0f20eeea
DC
1055 ++index;
1056 switch (name[index])
1057 {
cf325299
PA
1058 case '\0':
1059 return index;
0f20eeea
DC
1060 /* Skip over one less than the appropriate number of
1061 characters: the for loop will skip over the last
1062 one. */
1063 case '<':
1064 if (name[index + 1] == '<')
1065 index += 1;
1066 else
1067 index += 0;
1068 break;
1069 case '>':
1070 case '-':
1071 if (name[index + 1] == '>')
1072 index += 1;
1073 else
1074 index += 0;
1075 break;
1076 case '(':
1077 index += 1;
1078 break;
1079 default:
1080 index += 0;
1081 break;
1082 }
1083 }
1084 operator_possible = 0;
1085 break;
1086 case ' ':
1087 case ',':
1088 case '.':
1089 case '&':
1090 case '*':
1091 /* NOTE: carlton/2003-04-18: I'm not sure what the precise
1092 set of relevant characters are here: it's necessary to
1093 include any character that can show up before 'operator'
1094 in a demangled name, and it's safe to include any
1095 character that can't be part of an identifier's name. */
1096 operator_possible = 1;
1097 break;
9219021c 1098 default:
0f20eeea 1099 operator_possible = 0;
9219021c
DC
1100 break;
1101 }
1102 }
1103}
1104
b2a7f303
DC
1105/* Complain about a demangled name that we don't know how to parse.
1106 NAME is the demangled name in question. */
1107
1108static void
1109demangled_name_complaint (const char *name)
1110{
b98664d3 1111 complaint ("unexpected demangled name '%s'", name);
b2a7f303
DC
1112}
1113
9219021c
DC
1114/* If NAME is the fully-qualified name of a C++
1115 function/variable/method/etc., this returns the length of its
1116 entire prefix: all of the namespaces and classes that make up its
1117 name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns
1118 4, given 'foo', it returns 0. */
1119
1120unsigned int
1121cp_entire_prefix_len (const char *name)
1122{
1123 unsigned int current_len = cp_find_first_component (name);
1124 unsigned int previous_len = 0;
1125
1126 while (name[current_len] != '\0')
1127 {
1128 gdb_assert (name[current_len] == ':');
1129 previous_len = current_len;
1130 /* Skip the '::'. */
1131 current_len += 2;
1132 current_len += cp_find_first_component (name + current_len);
1133 }
1134
1135 return previous_len;
1136}
1137
b6429628
DC
1138/* Overload resolution functions. */
1139
8d577d32
DC
1140/* Test to see if SYM is a symbol that we haven't seen corresponding
1141 to a function named OLOAD_NAME. If so, add it to the current
aff410f1 1142 completion list. */
b6429628
DC
1143
1144static void
aff410f1
MS
1145overload_list_add_symbol (struct symbol *sym,
1146 const char *oload_name)
b6429628
DC
1147{
1148 int newsize;
1149 int i;
109483d9 1150 gdb::unique_xmalloc_ptr<char> sym_name;
b6429628 1151
aff410f1
MS
1152 /* If there is no type information, we can't do anything, so
1153 skip. */
b6429628
DC
1154 if (SYMBOL_TYPE (sym) == NULL)
1155 return;
1156
aff410f1 1157 /* skip any symbols that we've already considered. */
b6429628 1158 for (i = 0; i < sym_return_val_index; ++i)
8d577d32
DC
1159 if (strcmp (SYMBOL_LINKAGE_NAME (sym),
1160 SYMBOL_LINKAGE_NAME (sym_return_val[i])) == 0)
b6429628
DC
1161 return;
1162
1163 /* Get the demangled name without parameters */
3567439c 1164 sym_name = cp_remove_params (SYMBOL_NATURAL_NAME (sym));
b6429628
DC
1165 if (!sym_name)
1166 return;
1167
1168 /* skip symbols that cannot match */
109483d9
PA
1169 if (strcmp (sym_name.get (), oload_name) != 0)
1170 return;
b6429628 1171
aff410f1
MS
1172 /* We have a match for an overload instance, so add SYM to the
1173 current list of overload instances */
b6429628
DC
1174 if (sym_return_val_index + 3 > sym_return_val_size)
1175 {
1176 newsize = (sym_return_val_size *= 2) * sizeof (struct symbol *);
aff410f1
MS
1177 sym_return_val = (struct symbol **)
1178 xrealloc ((char *) sym_return_val, newsize);
b6429628
DC
1179 }
1180 sym_return_val[sym_return_val_index++] = sym;
1181 sym_return_val[sym_return_val_index] = NULL;
1182}
1183
1184/* Return a null-terminated list of pointers to function symbols that
8d577d32 1185 are named FUNC_NAME and are visible within NAMESPACE. */
b6429628
DC
1186
1187struct symbol **
8d577d32 1188make_symbol_overload_list (const char *func_name,
fe978cb0 1189 const char *the_namespace)
b6429628 1190{
8d577d32 1191 struct cleanup *old_cleanups;
245040d7 1192 const char *name;
b6429628 1193
8d577d32
DC
1194 sym_return_val_size = 100;
1195 sym_return_val_index = 0;
8d749320 1196 sym_return_val = XNEWVEC (struct symbol *, sym_return_val_size + 1);
8d577d32 1197 sym_return_val[0] = NULL;
b6429628 1198
8d577d32
DC
1199 old_cleanups = make_cleanup (xfree, sym_return_val);
1200
fe978cb0 1201 make_symbol_overload_list_using (func_name, the_namespace);
8d577d32 1202
fe978cb0 1203 if (the_namespace[0] == '\0')
245040d7
SW
1204 name = func_name;
1205 else
1206 {
1207 char *concatenated_name
224c3ddb 1208 = (char *) alloca (strlen (the_namespace) + 2 + strlen (func_name) + 1);
fe978cb0 1209 strcpy (concatenated_name, the_namespace);
245040d7
SW
1210 strcat (concatenated_name, "::");
1211 strcat (concatenated_name, func_name);
1212 name = concatenated_name;
1213 }
1214
1215 make_symbol_overload_list_qualified (name);
1216
8d577d32
DC
1217 discard_cleanups (old_cleanups);
1218
1219 return sym_return_val;
1220}
1221
245040d7
SW
1222/* Add all symbols with a name matching NAME in BLOCK to the overload
1223 list. */
1224
1225static void
1226make_symbol_overload_list_block (const char *name,
1227 const struct block *block)
1228{
8157b174 1229 struct block_iterator iter;
245040d7
SW
1230 struct symbol *sym;
1231
b5ec771e
PA
1232 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
1233
1234 ALL_BLOCK_SYMBOLS_WITH_NAME (block, lookup_name, iter, sym)
245040d7
SW
1235 overload_list_add_symbol (sym, name);
1236}
1237
7322dca9
SW
1238/* Adds the function FUNC_NAME from NAMESPACE to the overload set. */
1239
1240static void
1241make_symbol_overload_list_namespace (const char *func_name,
fe978cb0 1242 const char *the_namespace)
7322dca9 1243{
245040d7
SW
1244 const char *name;
1245 const struct block *block = NULL;
1246
fe978cb0 1247 if (the_namespace[0] == '\0')
245040d7 1248 name = func_name;
7322dca9
SW
1249 else
1250 {
1251 char *concatenated_name
224c3ddb 1252 = (char *) alloca (strlen (the_namespace) + 2 + strlen (func_name) + 1);
c5504eaf 1253
fe978cb0 1254 strcpy (concatenated_name, the_namespace);
7322dca9
SW
1255 strcat (concatenated_name, "::");
1256 strcat (concatenated_name, func_name);
245040d7 1257 name = concatenated_name;
7322dca9 1258 }
245040d7
SW
1259
1260 /* Look in the static block. */
1261 block = block_static_block (get_selected_block (0));
eeaafae2
JK
1262 if (block)
1263 make_symbol_overload_list_block (name, block);
245040d7
SW
1264
1265 /* Look in the global block. */
1266 block = block_global_block (block);
eeaafae2
JK
1267 if (block)
1268 make_symbol_overload_list_block (name, block);
245040d7 1269
7322dca9
SW
1270}
1271
aff410f1
MS
1272/* Search the namespace of the given type and namespace of and public
1273 base types. */
7322dca9
SW
1274
1275static void
1276make_symbol_overload_list_adl_namespace (struct type *type,
1277 const char *func_name)
1278{
fe978cb0 1279 char *the_namespace;
0d5cff50 1280 const char *type_name;
7322dca9
SW
1281 int i, prefix_len;
1282
aff410f1 1283 while (TYPE_CODE (type) == TYPE_CODE_PTR
aa006118 1284 || TYPE_IS_REFERENCE (type)
7322dca9
SW
1285 || TYPE_CODE (type) == TYPE_CODE_ARRAY
1286 || TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1287 {
1288 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1289 type = check_typedef(type);
1290 else
1291 type = TYPE_TARGET_TYPE (type);
1292 }
1293
1294 type_name = TYPE_NAME (type);
1295
7d3fe98e
SW
1296 if (type_name == NULL)
1297 return;
1298
7322dca9
SW
1299 prefix_len = cp_entire_prefix_len (type_name);
1300
1301 if (prefix_len != 0)
1302 {
224c3ddb 1303 the_namespace = (char *) alloca (prefix_len + 1);
fe978cb0
PA
1304 strncpy (the_namespace, type_name, prefix_len);
1305 the_namespace[prefix_len] = '\0';
7322dca9 1306
fe978cb0 1307 make_symbol_overload_list_namespace (func_name, the_namespace);
7322dca9
SW
1308 }
1309
1310 /* Check public base type */
4753d33b 1311 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
7322dca9
SW
1312 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
1313 {
1314 if (BASETYPE_VIA_PUBLIC (type, i))
aff410f1
MS
1315 make_symbol_overload_list_adl_namespace (TYPE_BASECLASS (type,
1316 i),
7322dca9
SW
1317 func_name);
1318 }
1319}
1320
b021a221 1321/* Adds the overload list overload candidates for FUNC_NAME found
aff410f1 1322 through argument dependent lookup. */
7322dca9
SW
1323
1324struct symbol **
1325make_symbol_overload_list_adl (struct type **arg_types, int nargs,
1326 const char *func_name)
1327{
1328 int i;
1329
1330 gdb_assert (sym_return_val_size != -1);
1331
1332 for (i = 1; i <= nargs; i++)
aff410f1
MS
1333 make_symbol_overload_list_adl_namespace (arg_types[i - 1],
1334 func_name);
7322dca9
SW
1335
1336 return sym_return_val;
1337}
1338
8d577d32
DC
1339/* This applies the using directives to add namespaces to search in,
1340 and then searches for overloads in all of those namespaces. It
1341 adds the symbols found to sym_return_val. Arguments are as in
1342 make_symbol_overload_list. */
1343
1344static void
1345make_symbol_overload_list_using (const char *func_name,
fe978cb0 1346 const char *the_namespace)
8d577d32 1347{
19c0c0f8 1348 struct using_direct *current;
4c3376c8 1349 const struct block *block;
8d577d32
DC
1350
1351 /* First, go through the using directives. If any of them apply,
1352 look in the appropriate namespaces for new functions to match
1353 on. */
b6429628 1354
4c3376c8
SW
1355 for (block = get_selected_block (0);
1356 block != NULL;
1357 block = BLOCK_SUPERBLOCK (block))
1358 for (current = block_using (block);
1359 current != NULL;
1360 current = current->next)
1361 {
19c0c0f8
UW
1362 /* Prevent recursive calls. */
1363 if (current->searched)
1364 continue;
1365
aff410f1
MS
1366 /* If this is a namespace alias or imported declaration ignore
1367 it. */
4c3376c8
SW
1368 if (current->alias != NULL || current->declaration != NULL)
1369 continue;
1370
fe978cb0 1371 if (strcmp (the_namespace, current->import_dest) == 0)
19c0c0f8 1372 {
aff410f1
MS
1373 /* Mark this import as searched so that the recursive call
1374 does not search it again. */
167b0be1
TT
1375 scoped_restore reset_directive_searched
1376 = make_scoped_restore (&current->searched, 1);
19c0c0f8 1377
aff410f1
MS
1378 make_symbol_overload_list_using (func_name,
1379 current->import_src);
19c0c0f8 1380 }
4c3376c8 1381 }
b6429628 1382
8d577d32 1383 /* Now, add names for this namespace. */
fe978cb0 1384 make_symbol_overload_list_namespace (func_name, the_namespace);
8d577d32 1385}
b6429628 1386
8d577d32
DC
1387/* This does the bulk of the work of finding overloaded symbols.
1388 FUNC_NAME is the name of the overloaded function we're looking for
1389 (possibly including namespace info). */
b6429628 1390
8d577d32
DC
1391static void
1392make_symbol_overload_list_qualified (const char *func_name)
1393{
43f3e411 1394 struct compunit_symtab *cust;
8d577d32
DC
1395 struct objfile *objfile;
1396 const struct block *b, *surrounding_static_block = 0;
b6429628 1397
aff410f1
MS
1398 /* Look through the partial symtabs for all symbols which begin by
1399 matching FUNC_NAME. Make sure we read that symbol table in. */
b6429628 1400
ccefe4c4
TT
1401 ALL_OBJFILES (objfile)
1402 {
1403 if (objfile->sf)
1404 objfile->sf->qf->expand_symtabs_for_function (objfile, func_name);
1405 }
b6429628
DC
1406
1407 /* Search upwards from currently selected frame (so that we can
1408 complete on local vars. */
1409
1410 for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
245040d7 1411 make_symbol_overload_list_block (func_name, b);
b6429628 1412
8d577d32
DC
1413 surrounding_static_block = block_static_block (get_selected_block (0));
1414
b6429628
DC
1415 /* Go through the symtabs and check the externs and statics for
1416 symbols which match. */
1417
43f3e411 1418 ALL_COMPUNITS (objfile, cust)
b6429628
DC
1419 {
1420 QUIT;
43f3e411 1421 b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), GLOBAL_BLOCK);
245040d7 1422 make_symbol_overload_list_block (func_name, b);
b6429628
DC
1423 }
1424
43f3e411 1425 ALL_COMPUNITS (objfile, cust)
b6429628
DC
1426 {
1427 QUIT;
43f3e411 1428 b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), STATIC_BLOCK);
b6429628
DC
1429 /* Don't do this block twice. */
1430 if (b == surrounding_static_block)
1431 continue;
245040d7 1432 make_symbol_overload_list_block (func_name, b);
b6429628 1433 }
8d577d32
DC
1434}
1435
aff410f1 1436/* Lookup the rtti type for a class name. */
362ff856
MC
1437
1438struct type *
1439cp_lookup_rtti_type (const char *name, struct block *block)
1440{
1441 struct symbol * rtti_sym;
1442 struct type * rtti_type;
1443
82c7be31
DE
1444 /* Use VAR_DOMAIN here as NAME may be a typedef. PR 18141, 18417.
1445 Classes "live" in both STRUCT_DOMAIN and VAR_DOMAIN. */
d12307c1 1446 rtti_sym = lookup_symbol (name, block, VAR_DOMAIN, NULL).symbol;
362ff856
MC
1447
1448 if (rtti_sym == NULL)
1449 {
8a3fe4f8 1450 warning (_("RTTI symbol not found for class '%s'"), name);
362ff856
MC
1451 return NULL;
1452 }
1453
1454 if (SYMBOL_CLASS (rtti_sym) != LOC_TYPEDEF)
1455 {
8a3fe4f8 1456 warning (_("RTTI symbol for class '%s' is not a type"), name);
362ff856
MC
1457 return NULL;
1458 }
1459
82c7be31 1460 rtti_type = check_typedef (SYMBOL_TYPE (rtti_sym));
362ff856
MC
1461
1462 switch (TYPE_CODE (rtti_type))
1463 {
4753d33b 1464 case TYPE_CODE_STRUCT:
362ff856
MC
1465 break;
1466 case TYPE_CODE_NAMESPACE:
1467 /* chastain/2003-11-26: the symbol tables often contain fake
1468 symbols for namespaces with the same name as the struct.
1469 This warning is an indication of a bug in the lookup order
1470 or a bug in the way that the symbol tables are populated. */
8a3fe4f8 1471 warning (_("RTTI symbol for class '%s' is a namespace"), name);
362ff856
MC
1472 return NULL;
1473 default:
8a3fe4f8 1474 warning (_("RTTI symbol for class '%s' has bad type"), name);
362ff856
MC
1475 return NULL;
1476 }
1477
1478 return rtti_type;
1479}
b6429628 1480
992c7d70
GB
1481#ifdef HAVE_WORKING_FORK
1482
1483/* If nonzero, attempt to catch crashes in the demangler and print
1484 useful debugging information. */
1485
1486static int catch_demangler_crashes = 1;
1487
992c7d70
GB
1488/* Stack context and environment for demangler crash recovery. */
1489
1490static SIGJMP_BUF gdb_demangle_jmp_buf;
1491
1492/* If nonzero, attempt to dump core from the signal handler. */
1493
1494static int gdb_demangle_attempt_core_dump = 1;
1495
1496/* Signal handler for gdb_demangle. */
1497
1498static void
1499gdb_demangle_signal_handler (int signo)
1500{
1501 if (gdb_demangle_attempt_core_dump)
1502 {
1503 if (fork () == 0)
1504 dump_core ();
1505
1506 gdb_demangle_attempt_core_dump = 0;
1507 }
1508
1509 SIGLONGJMP (gdb_demangle_jmp_buf, signo);
1510}
1511
1512#endif
1513
8de20a37
TT
1514/* A wrapper for bfd_demangle. */
1515
1516char *
1517gdb_demangle (const char *name, int options)
1518{
992c7d70
GB
1519 char *result = NULL;
1520 int crash_signal = 0;
1521
1522#ifdef HAVE_WORKING_FORK
1523#if defined (HAVE_SIGACTION) && defined (SA_RESTART)
1524 struct sigaction sa, old_sa;
1525#else
a40805d4 1526 sighandler_t ofunc;
992c7d70
GB
1527#endif
1528 static int core_dump_allowed = -1;
1529
1530 if (core_dump_allowed == -1)
1531 {
1532 core_dump_allowed = can_dump_core (LIMIT_CUR);
1533
1534 if (!core_dump_allowed)
1535 gdb_demangle_attempt_core_dump = 0;
1536 }
1537
1538 if (catch_demangler_crashes)
1539 {
1540#if defined (HAVE_SIGACTION) && defined (SA_RESTART)
1541 sa.sa_handler = gdb_demangle_signal_handler;
1542 sigemptyset (&sa.sa_mask);
91b52240 1543#ifdef HAVE_SIGALTSTACK
992c7d70 1544 sa.sa_flags = SA_ONSTACK;
91b52240
GB
1545#else
1546 sa.sa_flags = 0;
1547#endif
992c7d70
GB
1548 sigaction (SIGSEGV, &sa, &old_sa);
1549#else
a40805d4 1550 ofunc = signal (SIGSEGV, gdb_demangle_signal_handler);
992c7d70
GB
1551#endif
1552
1553 crash_signal = SIGSETJMP (gdb_demangle_jmp_buf);
1554 }
1555#endif
1556
1557 if (crash_signal == 0)
1558 result = bfd_demangle (NULL, name, options);
1559
1560#ifdef HAVE_WORKING_FORK
1561 if (catch_demangler_crashes)
1562 {
1563#if defined (HAVE_SIGACTION) && defined (SA_RESTART)
1564 sigaction (SIGSEGV, &old_sa, NULL);
1565#else
1566 signal (SIGSEGV, ofunc);
1567#endif
1568
1569 if (crash_signal != 0)
1570 {
1571 static int error_reported = 0;
1572
1573 if (!error_reported)
1574 {
6ad94bc7
TT
1575 std::string short_msg
1576 = string_printf (_("unable to demangle '%s' "
1577 "(demangler failed with signal %d)"),
1578 name, crash_signal);
992c7d70 1579
6ad94bc7
TT
1580 std::string long_msg
1581 = string_printf ("%s:%d: %s: %s", __FILE__, __LINE__,
1582 "demangler-warning", short_msg.c_str ());
992c7d70 1583
223ffa71
TT
1584 target_terminal::scoped_restore_terminal_state term_state;
1585 target_terminal::ours_for_output ();
c509f1e1 1586
992c7d70
GB
1587 begin_line ();
1588 if (core_dump_allowed)
1589 fprintf_unfiltered (gdb_stderr,
1590 _("%s\nAttempting to dump core.\n"),
6ad94bc7 1591 long_msg.c_str ());
992c7d70 1592 else
6ad94bc7 1593 warn_cant_dump_core (long_msg.c_str ());
992c7d70 1594
6ad94bc7 1595 demangler_warning (__FILE__, __LINE__, "%s", short_msg.c_str ());
992c7d70
GB
1596
1597 error_reported = 1;
1598 }
1599
1600 result = NULL;
1601 }
1602 }
1603#endif
1604
1605 return result;
8de20a37
TT
1606}
1607
8b302db8
TT
1608/* See cp-support.h. */
1609
1610int
1611gdb_sniff_from_mangled_name (const char *mangled, char **demangled)
1612{
1613 *demangled = gdb_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
1614 return *demangled != NULL;
1615}
1616
a20714ff
PA
1617/* See cp-support.h. */
1618
1619unsigned int
1620cp_search_name_hash (const char *search_name)
1621{
1622 /* cp_entire_prefix_len assumes a fully-qualified name with no
1623 leading "::". */
1624 if (startswith (search_name, "::"))
1625 search_name += 2;
1626
1627 unsigned int prefix_len = cp_entire_prefix_len (search_name);
1628 if (prefix_len != 0)
1629 search_name += prefix_len + 2;
1630
bd69330d
PA
1631 unsigned int hash = 0;
1632 for (const char *string = search_name; *string != '\0'; ++string)
1633 {
1634 string = skip_spaces (string);
1635
1636 if (*string == '(')
1637 break;
1638
1639 /* Ignore ABI tags such as "[abi:cxx11]. */
1640 if (*string == '['
1641 && startswith (string + 1, "abi:")
1642 && string[5] != ':')
1643 break;
1644
1645 hash = SYMBOL_HASH_NEXT (hash, *string);
1646 }
1647 return hash;
a20714ff
PA
1648}
1649
1650/* Helper for cp_symbol_name_matches (i.e., symbol_name_matcher_ftype
1651 implementation for symbol_name_match_type::WILD matching). Split
1652 to a separate function for unit-testing convenience.
1653
1654 If SYMBOL_SEARCH_NAME has more scopes than LOOKUP_NAME, we try to
1655 match ignoring the extra leading scopes of SYMBOL_SEARCH_NAME.
1656 This allows conveniently setting breakpoints on functions/methods
1657 inside any namespace/class without specifying the fully-qualified
1658 name.
1659
1660 E.g., these match:
b5ec771e 1661
a20714ff
PA
1662 [symbol search name] [lookup name]
1663 foo::bar::func foo::bar::func
1664 foo::bar::func bar::func
1665 foo::bar::func func
1666
1667 While these don't:
1668
1669 [symbol search name] [lookup name]
1670 foo::zbar::func bar::func
1671 foo::bar::func foo::func
1672
1673 See more examples in the test_cp_symbol_name_matches selftest
1674 function below.
0662b6a7
PA
1675
1676 See symbol_name_matcher_ftype for description of SYMBOL_SEARCH_NAME
1677 and COMP_MATCH_RES.
1678
1679 LOOKUP_NAME/LOOKUP_NAME_LEN is the name we're looking up.
1680
1681 See strncmp_iw_with_mode for description of MODE.
1682*/
1683
1684static bool
1685cp_symbol_name_matches_1 (const char *symbol_search_name,
1686 const char *lookup_name,
1687 size_t lookup_name_len,
1688 strncmp_iw_mode mode,
a207cff2 1689 completion_match_result *comp_match_res)
0662b6a7 1690{
a20714ff 1691 const char *sname = symbol_search_name;
bd69330d
PA
1692 completion_match_for_lcd *match_for_lcd
1693 = (comp_match_res != NULL ? &comp_match_res->match_for_lcd : NULL);
a20714ff
PA
1694
1695 while (true)
1696 {
1697 if (strncmp_iw_with_mode (sname, lookup_name, lookup_name_len,
bd69330d 1698 mode, language_cplus, match_for_lcd) == 0)
a20714ff
PA
1699 {
1700 if (comp_match_res != NULL)
1701 {
1702 /* Note here we set different MATCH and MATCH_FOR_LCD
1703 strings. This is because with
1704
1705 (gdb) b push_bac[TAB]
1706
1707 we want the completion matches to list
1708
1709 std::vector<int>::push_back(...)
1710 std::vector<char>::push_back(...)
1711
1712 etc., which are SYMBOL_SEARCH_NAMEs, while we want
1713 the input line to auto-complete to
1714
1715 (gdb) push_back(...)
1716
1717 which is SNAME, not to
1718
1719 (gdb) std::vector<
1720
1721 which would be the regular common prefix between all
1722 the matches otherwise. */
1723 comp_match_res->set_match (symbol_search_name, sname);
1724 }
1725 return true;
1726 }
1727
1728 unsigned int len = cp_find_first_component (sname);
1729
1730 if (sname[len] == '\0')
1731 return false;
1732
1733 gdb_assert (sname[len] == ':');
1734 /* Skip the '::'. */
1735 sname += len + 2;
1736 }
1737}
1738
1739/* C++ symbol_name_matcher_ftype implementation. */
1740
1741static bool
1742cp_fq_symbol_name_matches (const char *symbol_search_name,
1743 const lookup_name_info &lookup_name,
1744 completion_match_result *comp_match_res)
1745{
1746 /* Get the demangled name. */
1747 const std::string &name = lookup_name.cplus ().lookup_name ();
bd69330d
PA
1748 completion_match_for_lcd *match_for_lcd
1749 = (comp_match_res != NULL ? &comp_match_res->match_for_lcd : NULL);
a20714ff
PA
1750 strncmp_iw_mode mode = (lookup_name.completion_mode ()
1751 ? strncmp_iw_mode::NORMAL
1752 : strncmp_iw_mode::MATCH_PARAMS);
1753
0662b6a7 1754 if (strncmp_iw_with_mode (symbol_search_name,
a20714ff 1755 name.c_str (), name.size (),
bd69330d 1756 mode, language_cplus, match_for_lcd) == 0)
0662b6a7 1757 {
a207cff2
PA
1758 if (comp_match_res != NULL)
1759 comp_match_res->set_match (symbol_search_name);
0662b6a7
PA
1760 return true;
1761 }
1762
1763 return false;
1764}
1765
a20714ff
PA
1766/* C++ symbol_name_matcher_ftype implementation for wild matches.
1767 Defers work to cp_symbol_name_matches_1. */
0662b6a7 1768
b5ec771e 1769static bool
a20714ff
PA
1770cp_symbol_name_matches (const char *symbol_search_name,
1771 const lookup_name_info &lookup_name,
1772 completion_match_result *comp_match_res)
b5ec771e
PA
1773{
1774 /* Get the demangled name. */
1775 const std::string &name = lookup_name.cplus ().lookup_name ();
1776
1777 strncmp_iw_mode mode = (lookup_name.completion_mode ()
1778 ? strncmp_iw_mode::NORMAL
1779 : strncmp_iw_mode::MATCH_PARAMS);
1780
0662b6a7
PA
1781 return cp_symbol_name_matches_1 (symbol_search_name,
1782 name.c_str (), name.size (),
a207cff2 1783 mode, comp_match_res);
b5ec771e
PA
1784}
1785
1786/* See cp-support.h. */
1787
1788symbol_name_matcher_ftype *
1789cp_get_symbol_name_matcher (const lookup_name_info &lookup_name)
1790{
a20714ff
PA
1791 switch (lookup_name.match_type ())
1792 {
1793 case symbol_name_match_type::FULL:
1794 case symbol_name_match_type::EXPRESSION:
de63c46b 1795 case symbol_name_match_type::SEARCH_NAME:
a20714ff
PA
1796 return cp_fq_symbol_name_matches;
1797 case symbol_name_match_type::WILD:
1798 return cp_symbol_name_matches;
1799 }
1800
1801 gdb_assert_not_reached ("");
b5ec771e
PA
1802}
1803
c62446b1
PA
1804#if GDB_SELF_TEST
1805
1806namespace selftests {
1807
0662b6a7
PA
1808void
1809test_cp_symbol_name_matches ()
1810{
1811#define CHECK_MATCH(SYMBOL, INPUT) \
1812 SELF_CHECK (cp_symbol_name_matches_1 (SYMBOL, \
1813 INPUT, sizeof (INPUT) - 1, \
1814 strncmp_iw_mode::MATCH_PARAMS, \
1815 NULL))
1816
1817#define CHECK_NOT_MATCH(SYMBOL, INPUT) \
1818 SELF_CHECK (!cp_symbol_name_matches_1 (SYMBOL, \
1819 INPUT, sizeof (INPUT) - 1, \
1820 strncmp_iw_mode::MATCH_PARAMS, \
1821 NULL))
1822
1823 /* Like CHECK_MATCH, and also check that INPUT (and all substrings
1824 that start at index 0) completes to SYMBOL. */
1825#define CHECK_MATCH_C(SYMBOL, INPUT) \
1826 do \
1827 { \
1828 CHECK_MATCH (SYMBOL, INPUT); \
1829 for (size_t i = 0; i < sizeof (INPUT) - 1; i++) \
1830 SELF_CHECK (cp_symbol_name_matches_1 (SYMBOL, INPUT, i, \
1831 strncmp_iw_mode::NORMAL, \
1832 NULL)); \
1833 } while (0)
1834
1835 /* Like CHECK_NOT_MATCH, and also check that INPUT does NOT complete
1836 to SYMBOL. */
1837#define CHECK_NOT_MATCH_C(SYMBOL, INPUT) \
1838 do \
1839 { \
1840 CHECK_NOT_MATCH (SYMBOL, INPUT); \
1841 SELF_CHECK (!cp_symbol_name_matches_1 (SYMBOL, INPUT, \
1842 sizeof (INPUT) - 1, \
1843 strncmp_iw_mode::NORMAL, \
1844 NULL)); \
1845 } while (0)
1846
1847 /* Lookup name without parens matches all overloads. */
1848 CHECK_MATCH_C ("function()", "function");
1849 CHECK_MATCH_C ("function(int)", "function");
1850
1851 /* Check whitespace around parameters is ignored. */
1852 CHECK_MATCH_C ("function()", "function ()");
1853 CHECK_MATCH_C ("function ( )", "function()");
1854 CHECK_MATCH_C ("function ()", "function( )");
1855 CHECK_MATCH_C ("func(int)", "func( int )");
1856 CHECK_MATCH_C ("func(int)", "func ( int ) ");
1857 CHECK_MATCH_C ("func ( int )", "func( int )");
1858 CHECK_MATCH_C ("func ( int )", "func ( int ) ");
1859
1860 /* Check symbol name prefixes aren't incorrectly matched. */
1861 CHECK_NOT_MATCH ("func", "function");
1862 CHECK_NOT_MATCH ("function", "func");
1863 CHECK_NOT_MATCH ("function()", "func");
1864
1865 /* Check that if the lookup name includes parameters, only the right
1866 overload matches. */
1867 CHECK_MATCH_C ("function(int)", "function(int)");
1868 CHECK_NOT_MATCH_C ("function(int)", "function()");
1869
1870 /* Check that whitespace within symbol names is not ignored. */
1871 CHECK_NOT_MATCH_C ("function", "func tion");
1872 CHECK_NOT_MATCH_C ("func__tion", "func_ _tion");
1873 CHECK_NOT_MATCH_C ("func11tion", "func1 1tion");
1874
1875 /* Check the converse, which can happen with template function,
1876 where the return type is part of the demangled name. */
1877 CHECK_NOT_MATCH_C ("func tion", "function");
1878 CHECK_NOT_MATCH_C ("func1 1tion", "func11tion");
1879 CHECK_NOT_MATCH_C ("func_ _tion", "func__tion");
1880
1881 /* Within parameters too. */
1882 CHECK_NOT_MATCH_C ("func(param)", "func(par am)");
1883
1884 /* Check handling of whitespace around C++ operators. */
1885 CHECK_NOT_MATCH_C ("operator<<", "opera tor<<");
1886 CHECK_NOT_MATCH_C ("operator<<", "operator< <");
1887 CHECK_NOT_MATCH_C ("operator<<", "operator < <");
1888 CHECK_NOT_MATCH_C ("operator==", "operator= =");
1889 CHECK_NOT_MATCH_C ("operator==", "operator = =");
1890 CHECK_MATCH_C ("operator<<", "operator <<");
1891 CHECK_MATCH_C ("operator<<()", "operator <<");
1892 CHECK_NOT_MATCH_C ("operator<<()", "operator<<(int)");
1893 CHECK_NOT_MATCH_C ("operator<<(int)", "operator<<()");
1894 CHECK_MATCH_C ("operator==", "operator ==");
1895 CHECK_MATCH_C ("operator==()", "operator ==");
1896 CHECK_MATCH_C ("operator <<", "operator<<");
1897 CHECK_MATCH_C ("operator ==", "operator==");
1898 CHECK_MATCH_C ("operator bool", "operator bool");
1899 CHECK_MATCH_C ("operator bool ()", "operator bool");
1900 CHECK_MATCH_C ("operatorX<<", "operatorX < <");
1901 CHECK_MATCH_C ("Xoperator<<", "Xoperator < <");
1902
1903 CHECK_MATCH_C ("operator()(int)", "operator()(int)");
1904 CHECK_MATCH_C ("operator()(int)", "operator ( ) ( int )");
1905 CHECK_MATCH_C ("operator()<long>(int)", "operator ( ) < long > ( int )");
1906 /* The first "()" is not the parameter list. */
1907 CHECK_NOT_MATCH ("operator()(int)", "operator");
1908
1909 /* Misc user-defined operator tests. */
1910
1911 CHECK_NOT_MATCH_C ("operator/=()", "operator ^=");
1912 /* Same length at end of input. */
1913 CHECK_NOT_MATCH_C ("operator>>", "operator[]");
1914 /* Same length but not at end of input. */
1915 CHECK_NOT_MATCH_C ("operator>>()", "operator[]()");
1916
1917 CHECK_MATCH_C ("base::operator char*()", "base::operator char*()");
1918 CHECK_MATCH_C ("base::operator char*()", "base::operator char * ()");
1919 CHECK_MATCH_C ("base::operator char**()", "base::operator char * * ()");
1920 CHECK_MATCH ("base::operator char**()", "base::operator char * *");
1921 CHECK_MATCH_C ("base::operator*()", "base::operator*()");
1922 CHECK_NOT_MATCH_C ("base::operator char*()", "base::operatorc");
1923 CHECK_NOT_MATCH ("base::operator char*()", "base::operator char");
1924 CHECK_NOT_MATCH ("base::operator char*()", "base::operat");
1925
1926 /* Check handling of whitespace around C++ scope operators. */
1927 CHECK_NOT_MATCH_C ("foo::bar", "foo: :bar");
1928 CHECK_MATCH_C ("foo::bar", "foo :: bar");
1929 CHECK_MATCH_C ("foo :: bar", "foo::bar");
1930
1931 CHECK_MATCH_C ("abc::def::ghi()", "abc::def::ghi()");
1932 CHECK_MATCH_C ("abc::def::ghi ( )", "abc::def::ghi()");
1933 CHECK_MATCH_C ("abc::def::ghi()", "abc::def::ghi ( )");
1934 CHECK_MATCH_C ("function()", "function()");
1935 CHECK_MATCH_C ("bar::function()", "bar::function()");
a20714ff
PA
1936
1937 /* Wild matching tests follow. */
1938
1939 /* Tests matching symbols in some scope. */
1940 CHECK_MATCH_C ("foo::function()", "function");
1941 CHECK_MATCH_C ("foo::function(int)", "function");
1942 CHECK_MATCH_C ("foo::bar::function()", "function");
1943 CHECK_MATCH_C ("bar::function()", "bar::function");
1944 CHECK_MATCH_C ("foo::bar::function()", "bar::function");
1945 CHECK_MATCH_C ("foo::bar::function(int)", "bar::function");
1946
1947 /* Same, with parameters in the lookup name. */
1948 CHECK_MATCH_C ("foo::function()", "function()");
1949 CHECK_MATCH_C ("foo::bar::function()", "function()");
1950 CHECK_MATCH_C ("foo::function(int)", "function(int)");
1951 CHECK_MATCH_C ("foo::function()", "foo::function()");
1952 CHECK_MATCH_C ("foo::bar::function()", "bar::function()");
1953 CHECK_MATCH_C ("foo::bar::function(int)", "bar::function(int)");
1954 CHECK_MATCH_C ("bar::function()", "bar::function()");
1955
1956 CHECK_NOT_MATCH_C ("foo::bar::function(int)", "bar::function()");
1957
1958 CHECK_MATCH_C ("(anonymous namespace)::bar::function(int)",
1959 "bar::function(int)");
1960 CHECK_MATCH_C ("foo::(anonymous namespace)::bar::function(int)",
1961 "function(int)");
1962
1963 /* Lookup scope wider than symbol scope, should not match. */
1964 CHECK_NOT_MATCH_C ("function()", "bar::function");
1965 CHECK_NOT_MATCH_C ("function()", "bar::function()");
1966
1967 /* Explicit global scope doesn't match. */
1968 CHECK_NOT_MATCH_C ("foo::function()", "::function");
1969 CHECK_NOT_MATCH_C ("foo::function()", "::function()");
1970 CHECK_NOT_MATCH_C ("foo::function(int)", "::function()");
1971 CHECK_NOT_MATCH_C ("foo::function(int)", "::function(int)");
bd69330d
PA
1972
1973 /* Test ABI tag matching/ignoring. */
1974
1975 /* If the symbol name has an ABI tag, but the lookup name doesn't,
1976 then the ABI tag in the symbol name is ignored. */
1977 CHECK_MATCH_C ("function[abi:foo]()", "function");
1978 CHECK_MATCH_C ("function[abi:foo](int)", "function");
1979 CHECK_MATCH_C ("function[abi:foo]()", "function ()");
1980 CHECK_NOT_MATCH_C ("function[abi:foo]()", "function (int)");
1981
1982 CHECK_MATCH_C ("function[abi:foo]()", "function[abi:foo]");
1983 CHECK_MATCH_C ("function[abi:foo](int)", "function[abi:foo]");
1984 CHECK_MATCH_C ("function[abi:foo]()", "function[abi:foo] ()");
1985 CHECK_MATCH_C ("function[abi:foo][abi:bar]()", "function");
1986 CHECK_MATCH_C ("function[abi:foo][abi:bar](int)", "function");
1987 CHECK_MATCH_C ("function[abi:foo][abi:bar]()", "function[abi:foo]");
1988 CHECK_MATCH_C ("function[abi:foo][abi:bar](int)", "function[abi:foo]");
1989 CHECK_MATCH_C ("function[abi:foo][abi:bar]()", "function[abi:foo] ()");
1990 CHECK_NOT_MATCH_C ("function[abi:foo][abi:bar]()", "function[abi:foo] (int)");
1991
1992 CHECK_MATCH_C ("function [abi:foo][abi:bar] ( )", "function [abi:foo]");
1993
1994 /* If the symbol name does not have an ABI tag, while the lookup
1995 name has one, then there's no match. */
1996 CHECK_NOT_MATCH_C ("function()", "function[abi:foo]()");
1997 CHECK_NOT_MATCH_C ("function()", "function[abi:foo]");
0662b6a7
PA
1998}
1999
c62446b1
PA
2000/* If non-NULL, return STR wrapped in quotes. Otherwise, return a
2001 "<null>" string (with no quotes). */
2002
2003static std::string
2004quote (const char *str)
2005{
2006 if (str != NULL)
2007 return std::string (1, '\"') + str + '\"';
2008 else
2009 return "<null>";
2010}
2011
2012/* Check that removing parameter info out of NAME produces EXPECTED.
2013 COMPLETION_MODE indicates whether we're testing normal and
2014 completion mode. FILE and LINE are used to provide better test
2015 location information in case ithe check fails. */
2016
2017static void
2018check_remove_params (const char *file, int line,
2019 const char *name, const char *expected,
2020 bool completion_mode)
2021{
2022 gdb::unique_xmalloc_ptr<char> result
2023 = cp_remove_params_if_any (name, completion_mode);
2024
2025 if ((expected == NULL) != (result == NULL)
2026 || (expected != NULL
2027 && strcmp (result.get (), expected) != 0))
2028 {
2029 error (_("%s:%d: make-paramless self-test failed: (completion=%d) "
2030 "\"%s\" -> %s, expected %s"),
2031 file, line, completion_mode, name,
2032 quote (result.get ()).c_str (), quote (expected).c_str ());
2033 }
2034}
2035
2036/* Entry point for cp_remove_params unit tests. */
2037
2038static void
2039test_cp_remove_params ()
2040{
2041 /* Check that removing parameter info out of NAME produces EXPECTED.
2042 Checks both normal and completion modes. */
2043#define CHECK(NAME, EXPECTED) \
2044 do \
2045 { \
2046 check_remove_params (__FILE__, __LINE__, NAME, EXPECTED, false); \
2047 check_remove_params (__FILE__, __LINE__, NAME, EXPECTED, true); \
2048 } \
2049 while (0)
2050
2051 /* Similar, but used when NAME is incomplete -- i.e., is has
2052 unbalanced parentheses. In this case, looking for the exact name
2053 should fail / return empty. */
2054#define CHECK_INCOMPL(NAME, EXPECTED) \
2055 do \
2056 { \
2057 check_remove_params (__FILE__, __LINE__, NAME, NULL, false); \
2058 check_remove_params (__FILE__, __LINE__, NAME, EXPECTED, true); \
2059 } \
2060 while (0)
2061
2062 CHECK ("function()", "function");
2063 CHECK_INCOMPL ("function(", "function");
2064 CHECK ("function() const", "function");
2065
2066 CHECK ("(anonymous namespace)::A::B::C",
2067 "(anonymous namespace)::A::B::C");
2068
2069 CHECK ("A::(anonymous namespace)",
2070 "A::(anonymous namespace)");
2071
2072 CHECK_INCOMPL ("A::(anonymou", "A");
2073
2074 CHECK ("A::foo<int>()",
2075 "A::foo<int>");
2076
2077 CHECK_INCOMPL ("A::foo<int>(",
2078 "A::foo<int>");
2079
2080 CHECK ("A::foo<(anonymous namespace)::B>::func(int)",
2081 "A::foo<(anonymous namespace)::B>::func");
2082
2083 CHECK_INCOMPL ("A::foo<(anonymous namespace)::B>::func(in",
2084 "A::foo<(anonymous namespace)::B>::func");
2085
2086 CHECK_INCOMPL ("A::foo<(anonymous namespace)::B>::",
2087 "A::foo<(anonymous namespace)::B>");
2088
2089 CHECK_INCOMPL ("A::foo<(anonymous namespace)::B>:",
2090 "A::foo<(anonymous namespace)::B>");
2091
2092 CHECK ("A::foo<(anonymous namespace)::B>",
2093 "A::foo<(anonymous namespace)::B>");
2094
2095 CHECK_INCOMPL ("A::foo<(anonymous namespace)::B",
2096 "A::foo");
2097
2098 /* Shouldn't this parse? Looks like a bug in
2099 cp_demangled_name_to_comp. See PR c++/22411. */
2100#if 0
2101 CHECK ("A::foo<void(int)>::func(int)",
2102 "A::foo<void(int)>::func");
2103#else
2104 CHECK_INCOMPL ("A::foo<void(int)>::func(int)",
2105 "A::foo");
2106#endif
2107
2108 CHECK_INCOMPL ("A::foo<void(int",
2109 "A::foo");
2110
2111#undef CHECK
2112#undef CHECK_INCOMPL
2113}
2114
2115} // namespace selftests
2116
2117#endif /* GDB_SELF_CHECK */
2118
9219021c
DC
2119/* Don't allow just "maintenance cplus". */
2120
2121static void
981a3fb3 2122maint_cplus_command (const char *arg, int from_tty)
9219021c 2123{
3e43a32a
MS
2124 printf_unfiltered (_("\"maintenance cplus\" must be followed "
2125 "by the name of a command.\n"));
aff410f1
MS
2126 help_list (maint_cplus_cmd_list,
2127 "maintenance cplus ",
635c7e8a 2128 all_commands, gdb_stdout);
9219021c
DC
2129}
2130
2131/* This is a front end for cp_find_first_component, for unit testing.
2132 Be careful when using it: see the NOTE above
2133 cp_find_first_component. */
2134
2135static void
4a475551 2136first_component_command (const char *arg, int from_tty)
9219021c 2137{
c836824f
AR
2138 int len;
2139 char *prefix;
2140
2141 if (!arg)
2142 return;
2143
2144 len = cp_find_first_component (arg);
224c3ddb 2145 prefix = (char *) alloca (len + 1);
9219021c
DC
2146
2147 memcpy (prefix, arg, len);
2148 prefix[len] = '\0';
2149
2150 printf_unfiltered ("%s\n", prefix);
2151}
2152
57651221 2153/* Implement "info vtbl". */
c4aeac85
TT
2154
2155static void
1d12d88f 2156info_vtbl_command (const char *arg, int from_tty)
c4aeac85
TT
2157{
2158 struct value *value;
2159
2160 value = parse_and_eval (arg);
2161 cplus_print_vtable (value);
2162}
2163
9219021c
DC
2164void
2165_initialize_cp_support (void)
2166{
aff410f1
MS
2167 add_prefix_cmd ("cplus", class_maintenance,
2168 maint_cplus_command,
2169 _("C++ maintenance commands."),
2170 &maint_cplus_cmd_list,
2171 "maintenance cplus ",
2172 0, &maintenancelist);
2173 add_alias_cmd ("cp", "cplus",
2174 class_maintenance, 1,
2175 &maintenancelist);
2176
2177 add_cmd ("first_component",
2178 class_maintenance,
2179 first_component_command,
1a966eab 2180 _("Print the first class/namespace component of NAME."),
9219021c 2181 &maint_cplus_cmd_list);
c4aeac85
TT
2182
2183 add_info ("vtbl", info_vtbl_command,
57651221 2184 _("Show the virtual function table for a C++ object.\n\
c4aeac85
TT
2185Usage: info vtbl EXPRESSION\n\
2186Evaluate EXPRESSION and display the virtual function table for the\n\
2187resulting object."));
992c7d70
GB
2188
2189#ifdef HAVE_WORKING_FORK
2190 add_setshow_boolean_cmd ("catch-demangler-crashes", class_maintenance,
2191 &catch_demangler_crashes, _("\
2192Set whether to attempt to catch demangler crashes."), _("\
2193Show whether to attempt to catch demangler crashes."), _("\
2194If enabled GDB will attempt to catch demangler crashes and\n\
2195display the offending symbol."),
2196 NULL,
2197 NULL,
2198 &maintenance_set_cmdlist,
2199 &maintenance_show_cmdlist);
2200#endif
c62446b1
PA
2201
2202#if GDB_SELF_TEST
0662b6a7
PA
2203 selftests::register_test ("cp_symbol_name_matches",
2204 selftests::test_cp_symbol_name_matches);
c62446b1
PA
2205 selftests::register_test ("cp_remove_params",
2206 selftests::test_cp_remove_params);
2207#endif
9219021c 2208}