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