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