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