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