Add missing files to previous commit (Allow Python notification of new object-file...
[deliverable/binutils-gdb.git] / gdb / cp-support.c
CommitLineData
de17c821 1/* Helper routines for C++ support in GDB.
7b6bb8da 2 Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
9b254dd1 3 Free Software Foundation, Inc.
de17c821
DJ
4
5 Contributed by MontaVista Software.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
de17c821
DJ
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
de17c821
DJ
21
22#include "defs.h"
23#include "cp-support.h"
24#include "gdb_string.h"
25#include "demangle.h"
9219021c
DC
26#include "gdb_assert.h"
27#include "gdbcmd.h"
b6429628
DC
28#include "dictionary.h"
29#include "objfiles.h"
30#include "frame.h"
31#include "symtab.h"
32#include "block.h"
b2a7f303 33#include "complaints.h"
362ff856 34#include "gdbtypes.h"
12907978
KS
35#include "exceptions.h"
36#include "expression.h"
37#include "value.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
f88e9fd3
DJ
532 if (strcmp (string, ret) == 0)
533 {
534 xfree (ret);
535 return NULL;
536 }
de17c821 537
fb4c6eba
DJ
538 return ret;
539}
de17c821 540
aff410f1
MS
541/* Convert a mangled name to a demangle_component tree. *MEMORY is
542 set to the block of used memory that should be freed when finished
543 with the tree. DEMANGLED_P is set to the char * that should be
544 freed when finished with the tree, or NULL if none was needed.
545 OPTIONS will be passed to the demangler. */
de17c821 546
3a93a0c2 547static struct demangle_parse_info *
fb4c6eba
DJ
548mangled_name_to_comp (const char *mangled_name, int options,
549 void **memory, char **demangled_p)
de17c821 550{
fb4c6eba 551 char *demangled_name;
3a93a0c2 552 struct demangle_parse_info *info;
de17c821 553
fb4c6eba
DJ
554 /* If it looks like a v3 mangled name, then try to go directly
555 to trees. */
556 if (mangled_name[0] == '_' && mangled_name[1] == 'Z')
de17c821 557 {
3a93a0c2
KS
558 struct demangle_component *ret;
559
aff410f1
MS
560 ret = cplus_demangle_v3_components (mangled_name,
561 options, memory);
fb4c6eba
DJ
562 if (ret)
563 {
3a93a0c2
KS
564 info = cp_new_demangle_parse_info ();
565 info->tree = ret;
fb4c6eba 566 *demangled_p = NULL;
3a93a0c2 567 return info;
fb4c6eba 568 }
de17c821
DJ
569 }
570
aff410f1
MS
571 /* If it doesn't, or if that failed, then try to demangle the
572 name. */
fb4c6eba
DJ
573 demangled_name = cplus_demangle (mangled_name, options);
574 if (demangled_name == NULL)
575 return NULL;
576
aff410f1
MS
577 /* If we could demangle the name, parse it to build the component
578 tree. */
3a93a0c2 579 info = cp_demangled_name_to_comp (demangled_name, NULL);
de17c821 580
3a93a0c2 581 if (info == NULL)
fb4c6eba 582 {
6c761d9c 583 xfree (demangled_name);
fb4c6eba
DJ
584 return NULL;
585 }
de17c821 586
fb4c6eba 587 *demangled_p = demangled_name;
3a93a0c2 588 return info;
de17c821
DJ
589}
590
591/* Return the name of the class containing method PHYSNAME. */
592
593char *
31c27f77 594cp_class_name_from_physname (const char *physname)
de17c821 595{
de237128 596 void *storage = NULL;
fb4c6eba 597 char *demangled_name = NULL, *ret;
5e5100cb 598 struct demangle_component *ret_comp, *prev_comp, *cur_comp;
3a93a0c2 599 struct demangle_parse_info *info;
fb4c6eba
DJ
600 int done;
601
3a93a0c2
KS
602 info = mangled_name_to_comp (physname, DMGL_ANSI,
603 &storage, &demangled_name);
604 if (info == NULL)
de17c821
DJ
605 return NULL;
606
fb4c6eba 607 done = 0;
3a93a0c2 608 ret_comp = info->tree;
5e5100cb 609
aff410f1
MS
610 /* First strip off any qualifiers, if we have a function or
611 method. */
fb4c6eba
DJ
612 while (!done)
613 switch (ret_comp->type)
614 {
fb4c6eba
DJ
615 case DEMANGLE_COMPONENT_CONST:
616 case DEMANGLE_COMPONENT_RESTRICT:
617 case DEMANGLE_COMPONENT_VOLATILE:
618 case DEMANGLE_COMPONENT_CONST_THIS:
619 case DEMANGLE_COMPONENT_RESTRICT_THIS:
620 case DEMANGLE_COMPONENT_VOLATILE_THIS:
621 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
fb4c6eba
DJ
622 ret_comp = d_left (ret_comp);
623 break;
5e5100cb
DJ
624 default:
625 done = 1;
626 break;
627 }
628
629 /* If what we have now is a function, discard the argument list. */
630 if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
631 ret_comp = d_left (ret_comp);
632
633 /* If what we have now is a template, strip off the template
634 arguments. The left subtree may be a qualified name. */
635 if (ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE)
636 ret_comp = d_left (ret_comp);
637
aff410f1
MS
638 /* What we have now should be a name, possibly qualified.
639 Additional qualifiers could live in the left subtree or the right
640 subtree. Find the last piece. */
5e5100cb
DJ
641 done = 0;
642 prev_comp = NULL;
643 cur_comp = ret_comp;
644 while (!done)
645 switch (cur_comp->type)
646 {
647 case DEMANGLE_COMPONENT_QUAL_NAME:
648 case DEMANGLE_COMPONENT_LOCAL_NAME:
649 prev_comp = cur_comp;
650 cur_comp = d_right (cur_comp);
651 break;
fb4c6eba 652 case DEMANGLE_COMPONENT_TEMPLATE:
5e5100cb 653 case DEMANGLE_COMPONENT_NAME:
fb4c6eba
DJ
654 case DEMANGLE_COMPONENT_CTOR:
655 case DEMANGLE_COMPONENT_DTOR:
656 case DEMANGLE_COMPONENT_OPERATOR:
657 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
658 done = 1;
659 break;
660 default:
661 done = 1;
5e5100cb 662 cur_comp = NULL;
fb4c6eba
DJ
663 break;
664 }
665
666 ret = NULL;
5e5100cb 667 if (cur_comp != NULL && prev_comp != NULL)
de17c821 668 {
5e5100cb 669 /* We want to discard the rightmost child of PREV_COMP. */
fb4c6eba 670 *prev_comp = *d_left (prev_comp);
aff410f1
MS
671 /* The ten is completely arbitrary; we don't have a good
672 estimate. */
5e5100cb 673 ret = cp_comp_to_string (ret_comp, 10);
de17c821
DJ
674 }
675
fb4c6eba 676 xfree (storage);
3a93a0c2
KS
677 xfree (demangled_name);
678 cp_demangled_name_parse_free (info);
de17c821
DJ
679 return ret;
680}
681
aff410f1
MS
682/* Return the child of COMP which is the basename of a method,
683 variable, et cetera. All scope qualifiers are discarded, but
684 template arguments will be included. The component tree may be
685 modified. */
de17c821 686
5e5100cb
DJ
687static struct demangle_component *
688unqualified_name_from_comp (struct demangle_component *comp)
de17c821 689{
5e5100cb 690 struct demangle_component *ret_comp = comp, *last_template;
fb4c6eba
DJ
691 int done;
692
fb4c6eba 693 done = 0;
5e5100cb 694 last_template = NULL;
fb4c6eba
DJ
695 while (!done)
696 switch (ret_comp->type)
697 {
698 case DEMANGLE_COMPONENT_QUAL_NAME:
699 case DEMANGLE_COMPONENT_LOCAL_NAME:
fb4c6eba
DJ
700 ret_comp = d_right (ret_comp);
701 break;
5e5100cb
DJ
702 case DEMANGLE_COMPONENT_TYPED_NAME:
703 ret_comp = d_left (ret_comp);
704 break;
705 case DEMANGLE_COMPONENT_TEMPLATE:
706 gdb_assert (last_template == NULL);
707 last_template = ret_comp;
708 ret_comp = d_left (ret_comp);
709 break;
fb4c6eba
DJ
710 case DEMANGLE_COMPONENT_CONST:
711 case DEMANGLE_COMPONENT_RESTRICT:
712 case DEMANGLE_COMPONENT_VOLATILE:
713 case DEMANGLE_COMPONENT_CONST_THIS:
714 case DEMANGLE_COMPONENT_RESTRICT_THIS:
715 case DEMANGLE_COMPONENT_VOLATILE_THIS:
716 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
717 ret_comp = d_left (ret_comp);
718 break;
719 case DEMANGLE_COMPONENT_NAME:
fb4c6eba
DJ
720 case DEMANGLE_COMPONENT_CTOR:
721 case DEMANGLE_COMPONENT_DTOR:
722 case DEMANGLE_COMPONENT_OPERATOR:
723 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
724 done = 1;
725 break;
726 default:
5e5100cb 727 return NULL;
fb4c6eba
DJ
728 break;
729 }
730
5e5100cb
DJ
731 if (last_template)
732 {
733 d_left (last_template) = ret_comp;
734 return last_template;
735 }
736
737 return ret_comp;
738}
739
740/* Return the name of the method whose linkage name is PHYSNAME. */
741
742char *
743method_name_from_physname (const char *physname)
744{
de237128 745 void *storage = NULL;
5e5100cb
DJ
746 char *demangled_name = NULL, *ret;
747 struct demangle_component *ret_comp;
3a93a0c2 748 struct demangle_parse_info *info;
5e5100cb 749
3a93a0c2
KS
750 info = mangled_name_to_comp (physname, DMGL_ANSI,
751 &storage, &demangled_name);
752 if (info == NULL)
5e5100cb
DJ
753 return NULL;
754
3a93a0c2 755 ret_comp = unqualified_name_from_comp (info->tree);
5e5100cb 756
fb4c6eba
DJ
757 ret = NULL;
758 if (ret_comp != NULL)
aff410f1
MS
759 /* The ten is completely arbitrary; we don't have a good
760 estimate. */
fb4c6eba
DJ
761 ret = cp_comp_to_string (ret_comp, 10);
762
763 xfree (storage);
3a93a0c2
KS
764 xfree (demangled_name);
765 cp_demangled_name_parse_free (info);
fb4c6eba
DJ
766 return ret;
767}
de17c821 768
5e5100cb
DJ
769/* If FULL_NAME is the demangled name of a C++ function (including an
770 arg list, possibly including namespace/class qualifications),
771 return a new string containing only the function name (without the
772 arg list/class qualifications). Otherwise, return NULL. The
773 caller is responsible for freeing the memory in question. */
774
775char *
776cp_func_name (const char *full_name)
777{
5e5100cb
DJ
778 char *ret;
779 struct demangle_component *ret_comp;
3a93a0c2 780 struct demangle_parse_info *info;
5e5100cb 781
3a93a0c2
KS
782 info = cp_demangled_name_to_comp (full_name, NULL);
783 if (!info)
5e5100cb
DJ
784 return NULL;
785
3a93a0c2 786 ret_comp = unqualified_name_from_comp (info->tree);
5e5100cb
DJ
787
788 ret = NULL;
789 if (ret_comp != NULL)
790 ret = cp_comp_to_string (ret_comp, 10);
791
3a93a0c2 792 cp_demangled_name_parse_free (info);
5e5100cb
DJ
793 return ret;
794}
795
796/* DEMANGLED_NAME is the name of a function, including parameters and
797 (optionally) a return type. Return the name of the function without
798 parameters or return type, or NULL if we can not parse the name. */
799
3567439c
DJ
800char *
801cp_remove_params (const char *demangled_name)
5e5100cb
DJ
802{
803 int done = 0;
804 struct demangle_component *ret_comp;
3a93a0c2 805 struct demangle_parse_info *info;
5e5100cb
DJ
806 char *ret = NULL;
807
808 if (demangled_name == NULL)
809 return NULL;
810
3a93a0c2
KS
811 info = cp_demangled_name_to_comp (demangled_name, NULL);
812 if (info == NULL)
5e5100cb
DJ
813 return NULL;
814
815 /* First strip off any qualifiers, if we have a function or method. */
3a93a0c2 816 ret_comp = info->tree;
5e5100cb
DJ
817 while (!done)
818 switch (ret_comp->type)
819 {
820 case DEMANGLE_COMPONENT_CONST:
821 case DEMANGLE_COMPONENT_RESTRICT:
822 case DEMANGLE_COMPONENT_VOLATILE:
823 case DEMANGLE_COMPONENT_CONST_THIS:
824 case DEMANGLE_COMPONENT_RESTRICT_THIS:
825 case DEMANGLE_COMPONENT_VOLATILE_THIS:
826 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
827 ret_comp = d_left (ret_comp);
828 break;
829 default:
830 done = 1;
831 break;
832 }
833
834 /* What we have now should be a function. Return its name. */
835 if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
836 ret = cp_comp_to_string (d_left (ret_comp), 10);
837
3a93a0c2 838 cp_demangled_name_parse_free (info);
5e5100cb
DJ
839 return ret;
840}
841
fb4c6eba
DJ
842/* Here are some random pieces of trivia to keep in mind while trying
843 to take apart demangled names:
de17c821 844
fb4c6eba
DJ
845 - Names can contain function arguments or templates, so the process
846 has to be, to some extent recursive: maybe keep track of your
847 depth based on encountering <> and ().
848
849 - Parentheses don't just have to happen at the end of a name: they
850 can occur even if the name in question isn't a function, because
851 a template argument might be a type that's a function.
852
853 - Conversely, even if you're trying to deal with a function, its
854 demangled name might not end with ')': it could be a const or
855 volatile class method, in which case it ends with "const" or
856 "volatile".
857
858 - Parentheses are also used in anonymous namespaces: a variable
859 'foo' in an anonymous namespace gets demangled as "(anonymous
860 namespace)::foo".
861
862 - And operator names can contain parentheses or angle brackets. */
863
864/* FIXME: carlton/2003-03-13: We have several functions here with
865 overlapping functionality; can we combine them? Also, do they
866 handle all the above considerations correctly? */
de17c821 867
9219021c
DC
868
869/* This returns the length of first component of NAME, which should be
870 the demangled name of a C++ variable/function/method/etc.
871 Specifically, it returns the index of the first colon forming the
872 boundary of the first component: so, given 'A::foo' or 'A::B::foo'
873 it returns the 1, and given 'foo', it returns 0. */
874
b2a7f303
DC
875/* The character in NAME indexed by the return value is guaranteed to
876 always be either ':' or '\0'. */
9219021c
DC
877
878/* NOTE: carlton/2003-03-13: This function is currently only intended
879 for internal use: it's probably not entirely safe when called on
b2a7f303
DC
880 user-generated input, because some of the 'index += 2' lines in
881 cp_find_first_component_aux might go past the end of malformed
882 input. */
883
884unsigned int
885cp_find_first_component (const char *name)
886{
887 return cp_find_first_component_aux (name, 0);
888}
889
890/* Helper function for cp_find_first_component. Like that function,
891 it returns the length of the first component of NAME, but to make
892 the recursion easier, it also stops if it reaches an unexpected ')'
893 or '>' if the value of PERMISSIVE is nonzero. */
9219021c
DC
894
895/* Let's optimize away calls to strlen("operator"). */
896
897#define LENGTH_OF_OPERATOR 8
898
b2a7f303
DC
899static unsigned int
900cp_find_first_component_aux (const char *name, int permissive)
9219021c 901{
9219021c 902 unsigned int index = 0;
0f20eeea
DC
903 /* Operator names can show up in unexpected places. Since these can
904 contain parentheses or angle brackets, they can screw up the
905 recursion. But not every string 'operator' is part of an
906 operater name: e.g. you could have a variable 'cooperator'. So
907 this variable tells us whether or not we should treat the string
908 'operator' as starting an operator. */
909 int operator_possible = 1;
9219021c
DC
910
911 for (;; ++index)
912 {
913 switch (name[index])
914 {
915 case '<':
916 /* Template; eat it up. The calls to cp_first_component
917 should only return (I hope!) when they reach the '>'
918 terminating the component or a '::' between two
919 components. (Hence the '+ 2'.) */
920 index += 1;
b2a7f303 921 for (index += cp_find_first_component_aux (name + index, 1);
9219021c 922 name[index] != '>';
b2a7f303 923 index += cp_find_first_component_aux (name + index, 1))
9219021c 924 {
b2a7f303
DC
925 if (name[index] != ':')
926 {
927 demangled_name_complaint (name);
928 return strlen (name);
929 }
9219021c
DC
930 index += 2;
931 }
0f20eeea 932 operator_possible = 1;
9219021c
DC
933 break;
934 case '(':
935 /* Similar comment as to '<'. */
936 index += 1;
b2a7f303 937 for (index += cp_find_first_component_aux (name + index, 1);
9219021c 938 name[index] != ')';
b2a7f303 939 index += cp_find_first_component_aux (name + index, 1))
9219021c 940 {
b2a7f303
DC
941 if (name[index] != ':')
942 {
943 demangled_name_complaint (name);
944 return strlen (name);
945 }
9219021c
DC
946 index += 2;
947 }
0f20eeea 948 operator_possible = 1;
9219021c
DC
949 break;
950 case '>':
951 case ')':
b2a7f303 952 if (permissive)
7a20f2c2 953 return index;
b2a7f303
DC
954 else
955 {
956 demangled_name_complaint (name);
957 return strlen (name);
958 }
9219021c
DC
959 case '\0':
960 case ':':
961 return index;
0f20eeea
DC
962 case 'o':
963 /* Operator names can screw up the recursion. */
964 if (operator_possible
aff410f1
MS
965 && strncmp (name + index, "operator",
966 LENGTH_OF_OPERATOR) == 0)
0f20eeea
DC
967 {
968 index += LENGTH_OF_OPERATOR;
f88e9fd3 969 while (ISSPACE(name[index]))
0f20eeea
DC
970 ++index;
971 switch (name[index])
972 {
973 /* Skip over one less than the appropriate number of
974 characters: the for loop will skip over the last
975 one. */
976 case '<':
977 if (name[index + 1] == '<')
978 index += 1;
979 else
980 index += 0;
981 break;
982 case '>':
983 case '-':
984 if (name[index + 1] == '>')
985 index += 1;
986 else
987 index += 0;
988 break;
989 case '(':
990 index += 1;
991 break;
992 default:
993 index += 0;
994 break;
995 }
996 }
997 operator_possible = 0;
998 break;
999 case ' ':
1000 case ',':
1001 case '.':
1002 case '&':
1003 case '*':
1004 /* NOTE: carlton/2003-04-18: I'm not sure what the precise
1005 set of relevant characters are here: it's necessary to
1006 include any character that can show up before 'operator'
1007 in a demangled name, and it's safe to include any
1008 character that can't be part of an identifier's name. */
1009 operator_possible = 1;
1010 break;
9219021c 1011 default:
0f20eeea 1012 operator_possible = 0;
9219021c
DC
1013 break;
1014 }
1015 }
1016}
1017
b2a7f303
DC
1018/* Complain about a demangled name that we don't know how to parse.
1019 NAME is the demangled name in question. */
1020
1021static void
1022demangled_name_complaint (const char *name)
1023{
1024 complaint (&symfile_complaints,
1025 "unexpected demangled name '%s'", name);
1026}
1027
9219021c
DC
1028/* If NAME is the fully-qualified name of a C++
1029 function/variable/method/etc., this returns the length of its
1030 entire prefix: all of the namespaces and classes that make up its
1031 name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns
1032 4, given 'foo', it returns 0. */
1033
1034unsigned int
1035cp_entire_prefix_len (const char *name)
1036{
1037 unsigned int current_len = cp_find_first_component (name);
1038 unsigned int previous_len = 0;
1039
1040 while (name[current_len] != '\0')
1041 {
1042 gdb_assert (name[current_len] == ':');
1043 previous_len = current_len;
1044 /* Skip the '::'. */
1045 current_len += 2;
1046 current_len += cp_find_first_component (name + current_len);
1047 }
1048
1049 return previous_len;
1050}
1051
b6429628
DC
1052/* Overload resolution functions. */
1053
8d577d32
DC
1054/* Test to see if SYM is a symbol that we haven't seen corresponding
1055 to a function named OLOAD_NAME. If so, add it to the current
aff410f1 1056 completion list. */
b6429628
DC
1057
1058static void
aff410f1
MS
1059overload_list_add_symbol (struct symbol *sym,
1060 const char *oload_name)
b6429628
DC
1061{
1062 int newsize;
1063 int i;
1064 char *sym_name;
1065
aff410f1
MS
1066 /* If there is no type information, we can't do anything, so
1067 skip. */
b6429628
DC
1068 if (SYMBOL_TYPE (sym) == NULL)
1069 return;
1070
aff410f1 1071 /* skip any symbols that we've already considered. */
b6429628 1072 for (i = 0; i < sym_return_val_index; ++i)
8d577d32
DC
1073 if (strcmp (SYMBOL_LINKAGE_NAME (sym),
1074 SYMBOL_LINKAGE_NAME (sym_return_val[i])) == 0)
b6429628
DC
1075 return;
1076
1077 /* Get the demangled name without parameters */
3567439c 1078 sym_name = cp_remove_params (SYMBOL_NATURAL_NAME (sym));
b6429628
DC
1079 if (!sym_name)
1080 return;
1081
1082 /* skip symbols that cannot match */
1083 if (strcmp (sym_name, oload_name) != 0)
1084 {
1085 xfree (sym_name);
1086 return;
1087 }
1088
1089 xfree (sym_name);
1090
aff410f1
MS
1091 /* We have a match for an overload instance, so add SYM to the
1092 current list of overload instances */
b6429628
DC
1093 if (sym_return_val_index + 3 > sym_return_val_size)
1094 {
1095 newsize = (sym_return_val_size *= 2) * sizeof (struct symbol *);
aff410f1
MS
1096 sym_return_val = (struct symbol **)
1097 xrealloc ((char *) sym_return_val, newsize);
b6429628
DC
1098 }
1099 sym_return_val[sym_return_val_index++] = sym;
1100 sym_return_val[sym_return_val_index] = NULL;
1101}
1102
1103/* Return a null-terminated list of pointers to function symbols that
8d577d32 1104 are named FUNC_NAME and are visible within NAMESPACE. */
b6429628
DC
1105
1106struct symbol **
8d577d32
DC
1107make_symbol_overload_list (const char *func_name,
1108 const char *namespace)
b6429628 1109{
8d577d32 1110 struct cleanup *old_cleanups;
245040d7 1111 const char *name;
b6429628 1112
8d577d32
DC
1113 sym_return_val_size = 100;
1114 sym_return_val_index = 0;
1115 sym_return_val = xmalloc ((sym_return_val_size + 1) *
1116 sizeof (struct symbol *));
1117 sym_return_val[0] = NULL;
b6429628 1118
8d577d32
DC
1119 old_cleanups = make_cleanup (xfree, sym_return_val);
1120
1121 make_symbol_overload_list_using (func_name, namespace);
1122
245040d7
SW
1123 if (namespace[0] == '\0')
1124 name = func_name;
1125 else
1126 {
1127 char *concatenated_name
1128 = alloca (strlen (namespace) + 2 + strlen (func_name) + 1);
1129 strcpy (concatenated_name, namespace);
1130 strcat (concatenated_name, "::");
1131 strcat (concatenated_name, func_name);
1132 name = concatenated_name;
1133 }
1134
1135 make_symbol_overload_list_qualified (name);
1136
8d577d32
DC
1137 discard_cleanups (old_cleanups);
1138
1139 return sym_return_val;
1140}
1141
245040d7
SW
1142/* Add all symbols with a name matching NAME in BLOCK to the overload
1143 list. */
1144
1145static void
1146make_symbol_overload_list_block (const char *name,
1147 const struct block *block)
1148{
1149 struct dict_iterator iter;
1150 struct symbol *sym;
1151
1152 const struct dictionary *dict = BLOCK_DICT (block);
1153
1154 for (sym = dict_iter_name_first (dict, name, &iter);
1155 sym != NULL;
1156 sym = dict_iter_name_next (name, &iter))
1157 overload_list_add_symbol (sym, name);
1158}
1159
7322dca9
SW
1160/* Adds the function FUNC_NAME from NAMESPACE to the overload set. */
1161
1162static void
1163make_symbol_overload_list_namespace (const char *func_name,
1164 const char *namespace)
1165{
245040d7
SW
1166 const char *name;
1167 const struct block *block = NULL;
1168
7322dca9 1169 if (namespace[0] == '\0')
245040d7 1170 name = func_name;
7322dca9
SW
1171 else
1172 {
1173 char *concatenated_name
1174 = alloca (strlen (namespace) + 2 + strlen (func_name) + 1);
c5504eaf 1175
7322dca9
SW
1176 strcpy (concatenated_name, namespace);
1177 strcat (concatenated_name, "::");
1178 strcat (concatenated_name, func_name);
245040d7 1179 name = concatenated_name;
7322dca9 1180 }
245040d7
SW
1181
1182 /* Look in the static block. */
1183 block = block_static_block (get_selected_block (0));
eeaafae2
JK
1184 if (block)
1185 make_symbol_overload_list_block (name, block);
245040d7
SW
1186
1187 /* Look in the global block. */
1188 block = block_global_block (block);
eeaafae2
JK
1189 if (block)
1190 make_symbol_overload_list_block (name, block);
245040d7 1191
7322dca9
SW
1192}
1193
aff410f1
MS
1194/* Search the namespace of the given type and namespace of and public
1195 base types. */
7322dca9
SW
1196
1197static void
1198make_symbol_overload_list_adl_namespace (struct type *type,
1199 const char *func_name)
1200{
1201 char *namespace;
1202 char *type_name;
1203 int i, prefix_len;
1204
aff410f1
MS
1205 while (TYPE_CODE (type) == TYPE_CODE_PTR
1206 || TYPE_CODE (type) == TYPE_CODE_REF
7322dca9
SW
1207 || TYPE_CODE (type) == TYPE_CODE_ARRAY
1208 || TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1209 {
1210 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1211 type = check_typedef(type);
1212 else
1213 type = TYPE_TARGET_TYPE (type);
1214 }
1215
1216 type_name = TYPE_NAME (type);
1217
7d3fe98e
SW
1218 if (type_name == NULL)
1219 return;
1220
7322dca9
SW
1221 prefix_len = cp_entire_prefix_len (type_name);
1222
1223 if (prefix_len != 0)
1224 {
1225 namespace = alloca (prefix_len + 1);
1226 strncpy (namespace, type_name, prefix_len);
1227 namespace[prefix_len] = '\0';
1228
1229 make_symbol_overload_list_namespace (func_name, namespace);
1230 }
1231
1232 /* Check public base type */
1233 if (TYPE_CODE (type) == TYPE_CODE_CLASS)
1234 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
1235 {
1236 if (BASETYPE_VIA_PUBLIC (type, i))
aff410f1
MS
1237 make_symbol_overload_list_adl_namespace (TYPE_BASECLASS (type,
1238 i),
7322dca9
SW
1239 func_name);
1240 }
1241}
1242
b021a221 1243/* Adds the overload list overload candidates for FUNC_NAME found
aff410f1 1244 through argument dependent lookup. */
7322dca9
SW
1245
1246struct symbol **
1247make_symbol_overload_list_adl (struct type **arg_types, int nargs,
1248 const char *func_name)
1249{
1250 int i;
1251
1252 gdb_assert (sym_return_val_size != -1);
1253
1254 for (i = 1; i <= nargs; i++)
aff410f1
MS
1255 make_symbol_overload_list_adl_namespace (arg_types[i - 1],
1256 func_name);
7322dca9
SW
1257
1258 return sym_return_val;
1259}
1260
aff410f1
MS
1261/* Used for cleanups to reset the "searched" flag in case of an
1262 error. */
19c0c0f8
UW
1263
1264static void
1265reset_directive_searched (void *data)
1266{
1267 struct using_direct *direct = data;
1268 direct->searched = 0;
1269}
1270
8d577d32
DC
1271/* This applies the using directives to add namespaces to search in,
1272 and then searches for overloads in all of those namespaces. It
1273 adds the symbols found to sym_return_val. Arguments are as in
1274 make_symbol_overload_list. */
1275
1276static void
1277make_symbol_overload_list_using (const char *func_name,
1278 const char *namespace)
1279{
19c0c0f8 1280 struct using_direct *current;
4c3376c8 1281 const struct block *block;
8d577d32
DC
1282
1283 /* First, go through the using directives. If any of them apply,
1284 look in the appropriate namespaces for new functions to match
1285 on. */
b6429628 1286
4c3376c8
SW
1287 for (block = get_selected_block (0);
1288 block != NULL;
1289 block = BLOCK_SUPERBLOCK (block))
1290 for (current = block_using (block);
1291 current != NULL;
1292 current = current->next)
1293 {
19c0c0f8
UW
1294 /* Prevent recursive calls. */
1295 if (current->searched)
1296 continue;
1297
aff410f1
MS
1298 /* If this is a namespace alias or imported declaration ignore
1299 it. */
4c3376c8
SW
1300 if (current->alias != NULL || current->declaration != NULL)
1301 continue;
1302
1303 if (strcmp (namespace, current->import_dest) == 0)
19c0c0f8 1304 {
aff410f1
MS
1305 /* Mark this import as searched so that the recursive call
1306 does not search it again. */
19c0c0f8
UW
1307 struct cleanup *old_chain;
1308 current->searched = 1;
aff410f1
MS
1309 old_chain = make_cleanup (reset_directive_searched,
1310 current);
19c0c0f8 1311
aff410f1
MS
1312 make_symbol_overload_list_using (func_name,
1313 current->import_src);
19c0c0f8
UW
1314
1315 current->searched = 0;
1316 discard_cleanups (old_chain);
1317 }
4c3376c8 1318 }
b6429628 1319
8d577d32 1320 /* Now, add names for this namespace. */
7322dca9 1321 make_symbol_overload_list_namespace (func_name, namespace);
8d577d32 1322}
b6429628 1323
8d577d32
DC
1324/* This does the bulk of the work of finding overloaded symbols.
1325 FUNC_NAME is the name of the overloaded function we're looking for
1326 (possibly including namespace info). */
b6429628 1327
8d577d32
DC
1328static void
1329make_symbol_overload_list_qualified (const char *func_name)
1330{
1331 struct symbol *sym;
1332 struct symtab *s;
1333 struct objfile *objfile;
1334 const struct block *b, *surrounding_static_block = 0;
1335 struct dict_iterator iter;
1336 const struct dictionary *dict;
b6429628 1337
aff410f1
MS
1338 /* Look through the partial symtabs for all symbols which begin by
1339 matching FUNC_NAME. Make sure we read that symbol table in. */
b6429628 1340
ccefe4c4
TT
1341 ALL_OBJFILES (objfile)
1342 {
1343 if (objfile->sf)
1344 objfile->sf->qf->expand_symtabs_for_function (objfile, func_name);
1345 }
b6429628
DC
1346
1347 /* Search upwards from currently selected frame (so that we can
1348 complete on local vars. */
1349
1350 for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
245040d7 1351 make_symbol_overload_list_block (func_name, b);
b6429628 1352
8d577d32
DC
1353 surrounding_static_block = block_static_block (get_selected_block (0));
1354
b6429628
DC
1355 /* Go through the symtabs and check the externs and statics for
1356 symbols which match. */
1357
11309657 1358 ALL_PRIMARY_SYMTABS (objfile, s)
b6429628
DC
1359 {
1360 QUIT;
1361 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
245040d7 1362 make_symbol_overload_list_block (func_name, b);
b6429628
DC
1363 }
1364
11309657 1365 ALL_PRIMARY_SYMTABS (objfile, s)
b6429628
DC
1366 {
1367 QUIT;
1368 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
1369 /* Don't do this block twice. */
1370 if (b == surrounding_static_block)
1371 continue;
245040d7 1372 make_symbol_overload_list_block (func_name, b);
b6429628 1373 }
8d577d32
DC
1374}
1375
aff410f1 1376/* Lookup the rtti type for a class name. */
362ff856
MC
1377
1378struct type *
1379cp_lookup_rtti_type (const char *name, struct block *block)
1380{
1381 struct symbol * rtti_sym;
1382 struct type * rtti_type;
1383
2570f2b7 1384 rtti_sym = lookup_symbol (name, block, STRUCT_DOMAIN, NULL);
362ff856
MC
1385
1386 if (rtti_sym == NULL)
1387 {
8a3fe4f8 1388 warning (_("RTTI symbol not found for class '%s'"), name);
362ff856
MC
1389 return NULL;
1390 }
1391
1392 if (SYMBOL_CLASS (rtti_sym) != LOC_TYPEDEF)
1393 {
8a3fe4f8 1394 warning (_("RTTI symbol for class '%s' is not a type"), name);
362ff856
MC
1395 return NULL;
1396 }
1397
1398 rtti_type = SYMBOL_TYPE (rtti_sym);
1399
1400 switch (TYPE_CODE (rtti_type))
1401 {
1402 case TYPE_CODE_CLASS:
1403 break;
1404 case TYPE_CODE_NAMESPACE:
1405 /* chastain/2003-11-26: the symbol tables often contain fake
1406 symbols for namespaces with the same name as the struct.
1407 This warning is an indication of a bug in the lookup order
1408 or a bug in the way that the symbol tables are populated. */
8a3fe4f8 1409 warning (_("RTTI symbol for class '%s' is a namespace"), name);
362ff856
MC
1410 return NULL;
1411 default:
8a3fe4f8 1412 warning (_("RTTI symbol for class '%s' has bad type"), name);
362ff856
MC
1413 return NULL;
1414 }
1415
1416 return rtti_type;
1417}
b6429628 1418
9219021c
DC
1419/* Don't allow just "maintenance cplus". */
1420
1421static void
1422maint_cplus_command (char *arg, int from_tty)
1423{
3e43a32a
MS
1424 printf_unfiltered (_("\"maintenance cplus\" must be followed "
1425 "by the name of a command.\n"));
aff410f1
MS
1426 help_list (maint_cplus_cmd_list,
1427 "maintenance cplus ",
1428 -1, gdb_stdout);
9219021c
DC
1429}
1430
1431/* This is a front end for cp_find_first_component, for unit testing.
1432 Be careful when using it: see the NOTE above
1433 cp_find_first_component. */
1434
1435static void
1436first_component_command (char *arg, int from_tty)
1437{
c836824f
AR
1438 int len;
1439 char *prefix;
1440
1441 if (!arg)
1442 return;
1443
1444 len = cp_find_first_component (arg);
1445 prefix = alloca (len + 1);
9219021c
DC
1446
1447 memcpy (prefix, arg, len);
1448 prefix[len] = '\0';
1449
1450 printf_unfiltered ("%s\n", prefix);
1451}
1452
b9362cc7
AC
1453extern initialize_file_ftype _initialize_cp_support; /* -Wmissing-prototypes */
1454
12907978
KS
1455#define SKIP_SPACE(P) \
1456 do \
1457 { \
1458 while (*(P) == ' ' || *(P) == '\t') \
1459 ++(P); \
1460 } \
1461 while (0)
1462
1463/* Returns the length of the operator name or 0 if INPUT does not
aff410f1
MS
1464 point to a valid C++ operator. INPUT should start with
1465 "operator". */
12907978
KS
1466int
1467cp_validate_operator (const char *input)
1468{
1469 int i;
1470 char *copy;
1471 const char *p;
1472 struct expression *expr;
1473 struct value *val;
1474 struct gdb_exception except;
12907978
KS
1475
1476 p = input;
1477
1478 if (strncmp (p, "operator", 8) == 0)
1479 {
1480 int valid = 0;
12907978 1481
c5504eaf 1482 p += 8;
12907978 1483 SKIP_SPACE (p);
aff410f1
MS
1484 for (i = 0;
1485 i < sizeof (operator_tokens) / sizeof (operator_tokens[0]);
12907978
KS
1486 ++i)
1487 {
1488 int length = strlen (operator_tokens[i]);
c5504eaf 1489
aff410f1
MS
1490 /* By using strncmp here, we MUST have operator_tokens
1491 ordered! See additional notes where operator_tokens is
1492 defined above. */
12907978
KS
1493 if (strncmp (p, operator_tokens[i], length) == 0)
1494 {
1495 const char *op = p;
c5504eaf 1496
12907978
KS
1497 valid = 1;
1498 p += length;
1499
1500 if (strncmp (op, "new", 3) == 0
1501 || strncmp (op, "delete", 6) == 0)
1502 {
1503
aff410f1
MS
1504 /* Special case: new[] and delete[]. We must be
1505 careful to swallow whitespace before/in "[]". */
12907978
KS
1506 SKIP_SPACE (p);
1507
1508 if (*p == '[')
1509 {
1510 ++p;
1511 SKIP_SPACE (p);
1512 if (*p == ']')
1513 ++p;
1514 else
1515 valid = 0;
1516 }
1517 }
1518
1519 if (valid)
1520 return (p - input);
1521 }
1522 }
1523
1524 /* Check input for a conversion operator. */
1525
aff410f1 1526 /* Skip past base typename. */
12907978
KS
1527 while (*p != '*' && *p != '&' && *p != 0 && *p != ' ')
1528 ++p;
1529 SKIP_SPACE (p);
1530
aff410f1 1531 /* Add modifiers '*' / '&'. */
12907978
KS
1532 while (*p == '*' || *p == '&')
1533 {
1534 ++p;
1535 SKIP_SPACE (p);
1536 }
1537
1538 /* Check for valid type. [Remember: input starts with
1539 "operator".] */
1540 copy = savestring (input + 8, p - input - 8);
1541 expr = NULL;
1542 val = NULL;
1543 TRY_CATCH (except, RETURN_MASK_ALL)
1544 {
1545 expr = parse_expression (copy);
1546 val = evaluate_type (expr);
1547 }
1548
1549 xfree (copy);
1550 if (expr)
1551 xfree (expr);
1552
1553 if (val != NULL && value_type (val) != NULL)
1554 return (p - input);
1555 }
1556
1557 return 0;
1558}
1559
9219021c
DC
1560void
1561_initialize_cp_support (void)
1562{
aff410f1
MS
1563 add_prefix_cmd ("cplus", class_maintenance,
1564 maint_cplus_command,
1565 _("C++ maintenance commands."),
1566 &maint_cplus_cmd_list,
1567 "maintenance cplus ",
1568 0, &maintenancelist);
1569 add_alias_cmd ("cp", "cplus",
1570 class_maintenance, 1,
1571 &maintenancelist);
1572
1573 add_cmd ("first_component",
1574 class_maintenance,
1575 first_component_command,
1a966eab 1576 _("Print the first class/namespace component of NAME."),
9219021c 1577 &maint_cplus_cmd_list);
9219021c 1578}
This page took 0.776327 seconds and 4 git commands to generate.