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