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