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