2011-02-28 Michael Snyder <msnyder@vmware.com>
[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
f88e9fd3
DJ
89/* Return 1 if STRING is clearly already in canonical form. This
90 function is conservative; things which it does not recognize are
91 assumed to be non-canonical, and the parser will sort them out
92 afterwards. This speeds up the critical path for alphanumeric
93 identifiers. */
94
95static int
96cp_already_canonical (const char *string)
97{
98 /* Identifier start character [a-zA-Z_]. */
99 if (!ISIDST (string[0]))
100 return 0;
101
102 /* These are the only two identifiers which canonicalize to other
103 than themselves or an error: unsigned -> unsigned int and
104 signed -> int. */
105 if (string[0] == 'u' && strcmp (&string[1], "nsigned") == 0)
106 return 0;
107 else if (string[0] == 's' && strcmp (&string[1], "igned") == 0)
108 return 0;
109
110 /* Identifier character [a-zA-Z0-9_]. */
111 while (ISIDNUM (string[1]))
112 string++;
113
114 if (string[1] == '\0')
115 return 1;
116 else
117 return 0;
118}
9219021c 119
f88e9fd3
DJ
120/* Parse STRING and convert it to canonical form. If parsing fails,
121 or if STRING is already canonical, return NULL. Otherwise return
122 the canonical form. The return value is allocated via xmalloc. */
9219021c 123
fb4c6eba
DJ
124char *
125cp_canonicalize_string (const char *string)
126{
fb4c6eba 127 struct demangle_component *ret_comp;
f88e9fd3 128 unsigned int estimated_len;
fb4c6eba 129 char *ret;
9219021c 130
f88e9fd3
DJ
131 if (cp_already_canonical (string))
132 return NULL;
9219021c 133
f88e9fd3 134 ret_comp = cp_demangled_name_to_comp (string, NULL);
fb4c6eba
DJ
135 if (ret_comp == NULL)
136 return NULL;
9219021c 137
f88e9fd3
DJ
138 estimated_len = strlen (string) * 2;
139 ret = cp_comp_to_string (ret_comp, estimated_len);
9219021c 140
f88e9fd3
DJ
141 if (strcmp (string, ret) == 0)
142 {
143 xfree (ret);
144 return NULL;
145 }
de17c821 146
fb4c6eba
DJ
147 return ret;
148}
de17c821 149
aff410f1
MS
150/* Convert a mangled name to a demangle_component tree. *MEMORY is
151 set to the block of used memory that should be freed when finished
152 with the tree. DEMANGLED_P is set to the char * that should be
153 freed when finished with the tree, or NULL if none was needed.
154 OPTIONS will be passed to the demangler. */
de17c821 155
fb4c6eba
DJ
156static struct demangle_component *
157mangled_name_to_comp (const char *mangled_name, int options,
158 void **memory, char **demangled_p)
de17c821 159{
fb4c6eba
DJ
160 struct demangle_component *ret;
161 char *demangled_name;
de17c821 162
fb4c6eba
DJ
163 /* If it looks like a v3 mangled name, then try to go directly
164 to trees. */
165 if (mangled_name[0] == '_' && mangled_name[1] == 'Z')
de17c821 166 {
aff410f1
MS
167 ret = cplus_demangle_v3_components (mangled_name,
168 options, memory);
fb4c6eba
DJ
169 if (ret)
170 {
171 *demangled_p = NULL;
172 return ret;
173 }
de17c821
DJ
174 }
175
aff410f1
MS
176 /* If it doesn't, or if that failed, then try to demangle the
177 name. */
fb4c6eba
DJ
178 demangled_name = cplus_demangle (mangled_name, options);
179 if (demangled_name == NULL)
180 return NULL;
181
aff410f1
MS
182 /* If we could demangle the name, parse it to build the component
183 tree. */
f88e9fd3 184 ret = cp_demangled_name_to_comp (demangled_name, NULL);
de17c821 185
fb4c6eba
DJ
186 if (ret == NULL)
187 {
6c761d9c 188 xfree (demangled_name);
fb4c6eba
DJ
189 return NULL;
190 }
de17c821 191
fb4c6eba
DJ
192 *demangled_p = demangled_name;
193 return ret;
de17c821
DJ
194}
195
196/* Return the name of the class containing method PHYSNAME. */
197
198char *
31c27f77 199cp_class_name_from_physname (const char *physname)
de17c821 200{
de237128 201 void *storage = NULL;
fb4c6eba 202 char *demangled_name = NULL, *ret;
5e5100cb 203 struct demangle_component *ret_comp, *prev_comp, *cur_comp;
fb4c6eba
DJ
204 int done;
205
aff410f1
MS
206 ret_comp = mangled_name_to_comp (physname, DMGL_ANSI,
207 &storage, &demangled_name);
fb4c6eba 208 if (ret_comp == NULL)
de17c821
DJ
209 return NULL;
210
fb4c6eba 211 done = 0;
5e5100cb 212
aff410f1
MS
213 /* First strip off any qualifiers, if we have a function or
214 method. */
fb4c6eba
DJ
215 while (!done)
216 switch (ret_comp->type)
217 {
fb4c6eba
DJ
218 case DEMANGLE_COMPONENT_CONST:
219 case DEMANGLE_COMPONENT_RESTRICT:
220 case DEMANGLE_COMPONENT_VOLATILE:
221 case DEMANGLE_COMPONENT_CONST_THIS:
222 case DEMANGLE_COMPONENT_RESTRICT_THIS:
223 case DEMANGLE_COMPONENT_VOLATILE_THIS:
224 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
fb4c6eba
DJ
225 ret_comp = d_left (ret_comp);
226 break;
5e5100cb
DJ
227 default:
228 done = 1;
229 break;
230 }
231
232 /* If what we have now is a function, discard the argument list. */
233 if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
234 ret_comp = d_left (ret_comp);
235
236 /* If what we have now is a template, strip off the template
237 arguments. The left subtree may be a qualified name. */
238 if (ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE)
239 ret_comp = d_left (ret_comp);
240
aff410f1
MS
241 /* What we have now should be a name, possibly qualified.
242 Additional qualifiers could live in the left subtree or the right
243 subtree. Find the last piece. */
5e5100cb
DJ
244 done = 0;
245 prev_comp = NULL;
246 cur_comp = ret_comp;
247 while (!done)
248 switch (cur_comp->type)
249 {
250 case DEMANGLE_COMPONENT_QUAL_NAME:
251 case DEMANGLE_COMPONENT_LOCAL_NAME:
252 prev_comp = cur_comp;
253 cur_comp = d_right (cur_comp);
254 break;
fb4c6eba 255 case DEMANGLE_COMPONENT_TEMPLATE:
5e5100cb 256 case DEMANGLE_COMPONENT_NAME:
fb4c6eba
DJ
257 case DEMANGLE_COMPONENT_CTOR:
258 case DEMANGLE_COMPONENT_DTOR:
259 case DEMANGLE_COMPONENT_OPERATOR:
260 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
261 done = 1;
262 break;
263 default:
264 done = 1;
5e5100cb 265 cur_comp = NULL;
fb4c6eba
DJ
266 break;
267 }
268
269 ret = NULL;
5e5100cb 270 if (cur_comp != NULL && prev_comp != NULL)
de17c821 271 {
5e5100cb 272 /* We want to discard the rightmost child of PREV_COMP. */
fb4c6eba 273 *prev_comp = *d_left (prev_comp);
aff410f1
MS
274 /* The ten is completely arbitrary; we don't have a good
275 estimate. */
5e5100cb 276 ret = cp_comp_to_string (ret_comp, 10);
de17c821
DJ
277 }
278
fb4c6eba
DJ
279 xfree (storage);
280 if (demangled_name)
281 xfree (demangled_name);
de17c821
DJ
282 return ret;
283}
284
aff410f1
MS
285/* Return the child of COMP which is the basename of a method,
286 variable, et cetera. All scope qualifiers are discarded, but
287 template arguments will be included. The component tree may be
288 modified. */
de17c821 289
5e5100cb
DJ
290static struct demangle_component *
291unqualified_name_from_comp (struct demangle_component *comp)
de17c821 292{
5e5100cb 293 struct demangle_component *ret_comp = comp, *last_template;
fb4c6eba
DJ
294 int done;
295
fb4c6eba 296 done = 0;
5e5100cb 297 last_template = NULL;
fb4c6eba
DJ
298 while (!done)
299 switch (ret_comp->type)
300 {
301 case DEMANGLE_COMPONENT_QUAL_NAME:
302 case DEMANGLE_COMPONENT_LOCAL_NAME:
fb4c6eba
DJ
303 ret_comp = d_right (ret_comp);
304 break;
5e5100cb
DJ
305 case DEMANGLE_COMPONENT_TYPED_NAME:
306 ret_comp = d_left (ret_comp);
307 break;
308 case DEMANGLE_COMPONENT_TEMPLATE:
309 gdb_assert (last_template == NULL);
310 last_template = ret_comp;
311 ret_comp = d_left (ret_comp);
312 break;
fb4c6eba
DJ
313 case DEMANGLE_COMPONENT_CONST:
314 case DEMANGLE_COMPONENT_RESTRICT:
315 case DEMANGLE_COMPONENT_VOLATILE:
316 case DEMANGLE_COMPONENT_CONST_THIS:
317 case DEMANGLE_COMPONENT_RESTRICT_THIS:
318 case DEMANGLE_COMPONENT_VOLATILE_THIS:
319 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
320 ret_comp = d_left (ret_comp);
321 break;
322 case DEMANGLE_COMPONENT_NAME:
fb4c6eba
DJ
323 case DEMANGLE_COMPONENT_CTOR:
324 case DEMANGLE_COMPONENT_DTOR:
325 case DEMANGLE_COMPONENT_OPERATOR:
326 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
327 done = 1;
328 break;
329 default:
5e5100cb 330 return NULL;
fb4c6eba
DJ
331 break;
332 }
333
5e5100cb
DJ
334 if (last_template)
335 {
336 d_left (last_template) = ret_comp;
337 return last_template;
338 }
339
340 return ret_comp;
341}
342
343/* Return the name of the method whose linkage name is PHYSNAME. */
344
345char *
346method_name_from_physname (const char *physname)
347{
de237128 348 void *storage = NULL;
5e5100cb
DJ
349 char *demangled_name = NULL, *ret;
350 struct demangle_component *ret_comp;
5e5100cb 351
aff410f1
MS
352 ret_comp = mangled_name_to_comp (physname, DMGL_ANSI,
353 &storage, &demangled_name);
5e5100cb
DJ
354 if (ret_comp == NULL)
355 return NULL;
356
357 ret_comp = unqualified_name_from_comp (ret_comp);
358
fb4c6eba
DJ
359 ret = NULL;
360 if (ret_comp != NULL)
aff410f1
MS
361 /* The ten is completely arbitrary; we don't have a good
362 estimate. */
fb4c6eba
DJ
363 ret = cp_comp_to_string (ret_comp, 10);
364
365 xfree (storage);
366 if (demangled_name)
367 xfree (demangled_name);
368 return ret;
369}
de17c821 370
5e5100cb
DJ
371/* If FULL_NAME is the demangled name of a C++ function (including an
372 arg list, possibly including namespace/class qualifications),
373 return a new string containing only the function name (without the
374 arg list/class qualifications). Otherwise, return NULL. The
375 caller is responsible for freeing the memory in question. */
376
377char *
378cp_func_name (const char *full_name)
379{
5e5100cb
DJ
380 char *ret;
381 struct demangle_component *ret_comp;
5e5100cb 382
f88e9fd3 383 ret_comp = cp_demangled_name_to_comp (full_name, NULL);
5e5100cb
DJ
384 if (!ret_comp)
385 return NULL;
386
387 ret_comp = unqualified_name_from_comp (ret_comp);
388
389 ret = NULL;
390 if (ret_comp != NULL)
391 ret = cp_comp_to_string (ret_comp, 10);
392
5e5100cb
DJ
393 return ret;
394}
395
396/* DEMANGLED_NAME is the name of a function, including parameters and
397 (optionally) a return type. Return the name of the function without
398 parameters or return type, or NULL if we can not parse the name. */
399
3567439c
DJ
400char *
401cp_remove_params (const char *demangled_name)
5e5100cb
DJ
402{
403 int done = 0;
404 struct demangle_component *ret_comp;
5e5100cb
DJ
405 char *ret = NULL;
406
407 if (demangled_name == NULL)
408 return NULL;
409
f88e9fd3 410 ret_comp = cp_demangled_name_to_comp (demangled_name, NULL);
5e5100cb
DJ
411 if (ret_comp == NULL)
412 return NULL;
413
414 /* First strip off any qualifiers, if we have a function or method. */
415 while (!done)
416 switch (ret_comp->type)
417 {
418 case DEMANGLE_COMPONENT_CONST:
419 case DEMANGLE_COMPONENT_RESTRICT:
420 case DEMANGLE_COMPONENT_VOLATILE:
421 case DEMANGLE_COMPONENT_CONST_THIS:
422 case DEMANGLE_COMPONENT_RESTRICT_THIS:
423 case DEMANGLE_COMPONENT_VOLATILE_THIS:
424 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
425 ret_comp = d_left (ret_comp);
426 break;
427 default:
428 done = 1;
429 break;
430 }
431
432 /* What we have now should be a function. Return its name. */
433 if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
434 ret = cp_comp_to_string (d_left (ret_comp), 10);
435
5e5100cb
DJ
436 return ret;
437}
438
fb4c6eba
DJ
439/* Here are some random pieces of trivia to keep in mind while trying
440 to take apart demangled names:
de17c821 441
fb4c6eba
DJ
442 - Names can contain function arguments or templates, so the process
443 has to be, to some extent recursive: maybe keep track of your
444 depth based on encountering <> and ().
445
446 - Parentheses don't just have to happen at the end of a name: they
447 can occur even if the name in question isn't a function, because
448 a template argument might be a type that's a function.
449
450 - Conversely, even if you're trying to deal with a function, its
451 demangled name might not end with ')': it could be a const or
452 volatile class method, in which case it ends with "const" or
453 "volatile".
454
455 - Parentheses are also used in anonymous namespaces: a variable
456 'foo' in an anonymous namespace gets demangled as "(anonymous
457 namespace)::foo".
458
459 - And operator names can contain parentheses or angle brackets. */
460
461/* FIXME: carlton/2003-03-13: We have several functions here with
462 overlapping functionality; can we combine them? Also, do they
463 handle all the above considerations correctly? */
de17c821 464
9219021c
DC
465
466/* This returns the length of first component of NAME, which should be
467 the demangled name of a C++ variable/function/method/etc.
468 Specifically, it returns the index of the first colon forming the
469 boundary of the first component: so, given 'A::foo' or 'A::B::foo'
470 it returns the 1, and given 'foo', it returns 0. */
471
b2a7f303
DC
472/* The character in NAME indexed by the return value is guaranteed to
473 always be either ':' or '\0'. */
9219021c
DC
474
475/* NOTE: carlton/2003-03-13: This function is currently only intended
476 for internal use: it's probably not entirely safe when called on
b2a7f303
DC
477 user-generated input, because some of the 'index += 2' lines in
478 cp_find_first_component_aux might go past the end of malformed
479 input. */
480
481unsigned int
482cp_find_first_component (const char *name)
483{
484 return cp_find_first_component_aux (name, 0);
485}
486
487/* Helper function for cp_find_first_component. Like that function,
488 it returns the length of the first component of NAME, but to make
489 the recursion easier, it also stops if it reaches an unexpected ')'
490 or '>' if the value of PERMISSIVE is nonzero. */
9219021c
DC
491
492/* Let's optimize away calls to strlen("operator"). */
493
494#define LENGTH_OF_OPERATOR 8
495
b2a7f303
DC
496static unsigned int
497cp_find_first_component_aux (const char *name, int permissive)
9219021c 498{
9219021c 499 unsigned int index = 0;
0f20eeea
DC
500 /* Operator names can show up in unexpected places. Since these can
501 contain parentheses or angle brackets, they can screw up the
502 recursion. But not every string 'operator' is part of an
503 operater name: e.g. you could have a variable 'cooperator'. So
504 this variable tells us whether or not we should treat the string
505 'operator' as starting an operator. */
506 int operator_possible = 1;
9219021c
DC
507
508 for (;; ++index)
509 {
510 switch (name[index])
511 {
512 case '<':
513 /* Template; eat it up. The calls to cp_first_component
514 should only return (I hope!) when they reach the '>'
515 terminating the component or a '::' between two
516 components. (Hence the '+ 2'.) */
517 index += 1;
b2a7f303 518 for (index += cp_find_first_component_aux (name + index, 1);
9219021c 519 name[index] != '>';
b2a7f303 520 index += cp_find_first_component_aux (name + index, 1))
9219021c 521 {
b2a7f303
DC
522 if (name[index] != ':')
523 {
524 demangled_name_complaint (name);
525 return strlen (name);
526 }
9219021c
DC
527 index += 2;
528 }
0f20eeea 529 operator_possible = 1;
9219021c
DC
530 break;
531 case '(':
532 /* Similar comment as to '<'. */
533 index += 1;
b2a7f303 534 for (index += cp_find_first_component_aux (name + index, 1);
9219021c 535 name[index] != ')';
b2a7f303 536 index += cp_find_first_component_aux (name + index, 1))
9219021c 537 {
b2a7f303
DC
538 if (name[index] != ':')
539 {
540 demangled_name_complaint (name);
541 return strlen (name);
542 }
9219021c
DC
543 index += 2;
544 }
0f20eeea 545 operator_possible = 1;
9219021c
DC
546 break;
547 case '>':
548 case ')':
b2a7f303 549 if (permissive)
7a20f2c2 550 return index;
b2a7f303
DC
551 else
552 {
553 demangled_name_complaint (name);
554 return strlen (name);
555 }
9219021c
DC
556 case '\0':
557 case ':':
558 return index;
0f20eeea
DC
559 case 'o':
560 /* Operator names can screw up the recursion. */
561 if (operator_possible
aff410f1
MS
562 && strncmp (name + index, "operator",
563 LENGTH_OF_OPERATOR) == 0)
0f20eeea
DC
564 {
565 index += LENGTH_OF_OPERATOR;
f88e9fd3 566 while (ISSPACE(name[index]))
0f20eeea
DC
567 ++index;
568 switch (name[index])
569 {
570 /* Skip over one less than the appropriate number of
571 characters: the for loop will skip over the last
572 one. */
573 case '<':
574 if (name[index + 1] == '<')
575 index += 1;
576 else
577 index += 0;
578 break;
579 case '>':
580 case '-':
581 if (name[index + 1] == '>')
582 index += 1;
583 else
584 index += 0;
585 break;
586 case '(':
587 index += 1;
588 break;
589 default:
590 index += 0;
591 break;
592 }
593 }
594 operator_possible = 0;
595 break;
596 case ' ':
597 case ',':
598 case '.':
599 case '&':
600 case '*':
601 /* NOTE: carlton/2003-04-18: I'm not sure what the precise
602 set of relevant characters are here: it's necessary to
603 include any character that can show up before 'operator'
604 in a demangled name, and it's safe to include any
605 character that can't be part of an identifier's name. */
606 operator_possible = 1;
607 break;
9219021c 608 default:
0f20eeea 609 operator_possible = 0;
9219021c
DC
610 break;
611 }
612 }
613}
614
b2a7f303
DC
615/* Complain about a demangled name that we don't know how to parse.
616 NAME is the demangled name in question. */
617
618static void
619demangled_name_complaint (const char *name)
620{
621 complaint (&symfile_complaints,
622 "unexpected demangled name '%s'", name);
623}
624
9219021c
DC
625/* If NAME is the fully-qualified name of a C++
626 function/variable/method/etc., this returns the length of its
627 entire prefix: all of the namespaces and classes that make up its
628 name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns
629 4, given 'foo', it returns 0. */
630
631unsigned int
632cp_entire_prefix_len (const char *name)
633{
634 unsigned int current_len = cp_find_first_component (name);
635 unsigned int previous_len = 0;
636
637 while (name[current_len] != '\0')
638 {
639 gdb_assert (name[current_len] == ':');
640 previous_len = current_len;
641 /* Skip the '::'. */
642 current_len += 2;
643 current_len += cp_find_first_component (name + current_len);
644 }
645
646 return previous_len;
647}
648
b6429628
DC
649/* Overload resolution functions. */
650
8d577d32
DC
651/* Test to see if SYM is a symbol that we haven't seen corresponding
652 to a function named OLOAD_NAME. If so, add it to the current
aff410f1 653 completion list. */
b6429628
DC
654
655static void
aff410f1
MS
656overload_list_add_symbol (struct symbol *sym,
657 const char *oload_name)
b6429628
DC
658{
659 int newsize;
660 int i;
661 char *sym_name;
662
aff410f1
MS
663 /* If there is no type information, we can't do anything, so
664 skip. */
b6429628
DC
665 if (SYMBOL_TYPE (sym) == NULL)
666 return;
667
aff410f1 668 /* skip any symbols that we've already considered. */
b6429628 669 for (i = 0; i < sym_return_val_index; ++i)
8d577d32
DC
670 if (strcmp (SYMBOL_LINKAGE_NAME (sym),
671 SYMBOL_LINKAGE_NAME (sym_return_val[i])) == 0)
b6429628
DC
672 return;
673
674 /* Get the demangled name without parameters */
3567439c 675 sym_name = cp_remove_params (SYMBOL_NATURAL_NAME (sym));
b6429628
DC
676 if (!sym_name)
677 return;
678
679 /* skip symbols that cannot match */
680 if (strcmp (sym_name, oload_name) != 0)
681 {
682 xfree (sym_name);
683 return;
684 }
685
686 xfree (sym_name);
687
aff410f1
MS
688 /* We have a match for an overload instance, so add SYM to the
689 current list of overload instances */
b6429628
DC
690 if (sym_return_val_index + 3 > sym_return_val_size)
691 {
692 newsize = (sym_return_val_size *= 2) * sizeof (struct symbol *);
aff410f1
MS
693 sym_return_val = (struct symbol **)
694 xrealloc ((char *) sym_return_val, newsize);
b6429628
DC
695 }
696 sym_return_val[sym_return_val_index++] = sym;
697 sym_return_val[sym_return_val_index] = NULL;
698}
699
700/* Return a null-terminated list of pointers to function symbols that
8d577d32 701 are named FUNC_NAME and are visible within NAMESPACE. */
b6429628
DC
702
703struct symbol **
8d577d32
DC
704make_symbol_overload_list (const char *func_name,
705 const char *namespace)
b6429628 706{
8d577d32 707 struct cleanup *old_cleanups;
245040d7 708 const char *name;
b6429628 709
8d577d32
DC
710 sym_return_val_size = 100;
711 sym_return_val_index = 0;
712 sym_return_val = xmalloc ((sym_return_val_size + 1) *
713 sizeof (struct symbol *));
714 sym_return_val[0] = NULL;
b6429628 715
8d577d32
DC
716 old_cleanups = make_cleanup (xfree, sym_return_val);
717
718 make_symbol_overload_list_using (func_name, namespace);
719
245040d7
SW
720 if (namespace[0] == '\0')
721 name = func_name;
722 else
723 {
724 char *concatenated_name
725 = alloca (strlen (namespace) + 2 + strlen (func_name) + 1);
726 strcpy (concatenated_name, namespace);
727 strcat (concatenated_name, "::");
728 strcat (concatenated_name, func_name);
729 name = concatenated_name;
730 }
731
732 make_symbol_overload_list_qualified (name);
733
8d577d32
DC
734 discard_cleanups (old_cleanups);
735
736 return sym_return_val;
737}
738
245040d7
SW
739/* Add all symbols with a name matching NAME in BLOCK to the overload
740 list. */
741
742static void
743make_symbol_overload_list_block (const char *name,
744 const struct block *block)
745{
746 struct dict_iterator iter;
747 struct symbol *sym;
748
749 const struct dictionary *dict = BLOCK_DICT (block);
750
751 for (sym = dict_iter_name_first (dict, name, &iter);
752 sym != NULL;
753 sym = dict_iter_name_next (name, &iter))
754 overload_list_add_symbol (sym, name);
755}
756
7322dca9
SW
757/* Adds the function FUNC_NAME from NAMESPACE to the overload set. */
758
759static void
760make_symbol_overload_list_namespace (const char *func_name,
761 const char *namespace)
762{
245040d7
SW
763 const char *name;
764 const struct block *block = NULL;
765
7322dca9 766 if (namespace[0] == '\0')
245040d7 767 name = func_name;
7322dca9
SW
768 else
769 {
770 char *concatenated_name
771 = alloca (strlen (namespace) + 2 + strlen (func_name) + 1);
c5504eaf 772
7322dca9
SW
773 strcpy (concatenated_name, namespace);
774 strcat (concatenated_name, "::");
775 strcat (concatenated_name, func_name);
245040d7 776 name = concatenated_name;
7322dca9 777 }
245040d7
SW
778
779 /* Look in the static block. */
780 block = block_static_block (get_selected_block (0));
eeaafae2
JK
781 if (block)
782 make_symbol_overload_list_block (name, block);
245040d7
SW
783
784 /* Look in the global block. */
785 block = block_global_block (block);
eeaafae2
JK
786 if (block)
787 make_symbol_overload_list_block (name, block);
245040d7 788
7322dca9
SW
789}
790
aff410f1
MS
791/* Search the namespace of the given type and namespace of and public
792 base types. */
7322dca9
SW
793
794static void
795make_symbol_overload_list_adl_namespace (struct type *type,
796 const char *func_name)
797{
798 char *namespace;
799 char *type_name;
800 int i, prefix_len;
801
aff410f1
MS
802 while (TYPE_CODE (type) == TYPE_CODE_PTR
803 || TYPE_CODE (type) == TYPE_CODE_REF
7322dca9
SW
804 || TYPE_CODE (type) == TYPE_CODE_ARRAY
805 || TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
806 {
807 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
808 type = check_typedef(type);
809 else
810 type = TYPE_TARGET_TYPE (type);
811 }
812
813 type_name = TYPE_NAME (type);
814
7d3fe98e
SW
815 if (type_name == NULL)
816 return;
817
7322dca9
SW
818 prefix_len = cp_entire_prefix_len (type_name);
819
820 if (prefix_len != 0)
821 {
822 namespace = alloca (prefix_len + 1);
823 strncpy (namespace, type_name, prefix_len);
824 namespace[prefix_len] = '\0';
825
826 make_symbol_overload_list_namespace (func_name, namespace);
827 }
828
829 /* Check public base type */
830 if (TYPE_CODE (type) == TYPE_CODE_CLASS)
831 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
832 {
833 if (BASETYPE_VIA_PUBLIC (type, i))
aff410f1
MS
834 make_symbol_overload_list_adl_namespace (TYPE_BASECLASS (type,
835 i),
7322dca9
SW
836 func_name);
837 }
838}
839
b021a221 840/* Adds the overload list overload candidates for FUNC_NAME found
aff410f1 841 through argument dependent lookup. */
7322dca9
SW
842
843struct symbol **
844make_symbol_overload_list_adl (struct type **arg_types, int nargs,
845 const char *func_name)
846{
847 int i;
848
849 gdb_assert (sym_return_val_size != -1);
850
851 for (i = 1; i <= nargs; i++)
aff410f1
MS
852 make_symbol_overload_list_adl_namespace (arg_types[i - 1],
853 func_name);
7322dca9
SW
854
855 return sym_return_val;
856}
857
aff410f1
MS
858/* Used for cleanups to reset the "searched" flag in case of an
859 error. */
19c0c0f8
UW
860
861static void
862reset_directive_searched (void *data)
863{
864 struct using_direct *direct = data;
865 direct->searched = 0;
866}
867
8d577d32
DC
868/* This applies the using directives to add namespaces to search in,
869 and then searches for overloads in all of those namespaces. It
870 adds the symbols found to sym_return_val. Arguments are as in
871 make_symbol_overload_list. */
872
873static void
874make_symbol_overload_list_using (const char *func_name,
875 const char *namespace)
876{
19c0c0f8 877 struct using_direct *current;
4c3376c8 878 const struct block *block;
8d577d32
DC
879
880 /* First, go through the using directives. If any of them apply,
881 look in the appropriate namespaces for new functions to match
882 on. */
b6429628 883
4c3376c8
SW
884 for (block = get_selected_block (0);
885 block != NULL;
886 block = BLOCK_SUPERBLOCK (block))
887 for (current = block_using (block);
888 current != NULL;
889 current = current->next)
890 {
19c0c0f8
UW
891 /* Prevent recursive calls. */
892 if (current->searched)
893 continue;
894
aff410f1
MS
895 /* If this is a namespace alias or imported declaration ignore
896 it. */
4c3376c8
SW
897 if (current->alias != NULL || current->declaration != NULL)
898 continue;
899
900 if (strcmp (namespace, current->import_dest) == 0)
19c0c0f8 901 {
aff410f1
MS
902 /* Mark this import as searched so that the recursive call
903 does not search it again. */
19c0c0f8
UW
904 struct cleanup *old_chain;
905 current->searched = 1;
aff410f1
MS
906 old_chain = make_cleanup (reset_directive_searched,
907 current);
19c0c0f8 908
aff410f1
MS
909 make_symbol_overload_list_using (func_name,
910 current->import_src);
19c0c0f8
UW
911
912 current->searched = 0;
913 discard_cleanups (old_chain);
914 }
4c3376c8 915 }
b6429628 916
8d577d32 917 /* Now, add names for this namespace. */
7322dca9 918 make_symbol_overload_list_namespace (func_name, namespace);
8d577d32 919}
b6429628 920
8d577d32
DC
921/* This does the bulk of the work of finding overloaded symbols.
922 FUNC_NAME is the name of the overloaded function we're looking for
923 (possibly including namespace info). */
b6429628 924
8d577d32
DC
925static void
926make_symbol_overload_list_qualified (const char *func_name)
927{
928 struct symbol *sym;
929 struct symtab *s;
930 struct objfile *objfile;
931 const struct block *b, *surrounding_static_block = 0;
932 struct dict_iterator iter;
933 const struct dictionary *dict;
b6429628 934
aff410f1
MS
935 /* Look through the partial symtabs for all symbols which begin by
936 matching FUNC_NAME. Make sure we read that symbol table in. */
b6429628 937
ccefe4c4
TT
938 ALL_OBJFILES (objfile)
939 {
940 if (objfile->sf)
941 objfile->sf->qf->expand_symtabs_for_function (objfile, func_name);
942 }
b6429628
DC
943
944 /* Search upwards from currently selected frame (so that we can
945 complete on local vars. */
946
947 for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
245040d7 948 make_symbol_overload_list_block (func_name, b);
b6429628 949
8d577d32
DC
950 surrounding_static_block = block_static_block (get_selected_block (0));
951
b6429628
DC
952 /* Go through the symtabs and check the externs and statics for
953 symbols which match. */
954
11309657 955 ALL_PRIMARY_SYMTABS (objfile, s)
b6429628
DC
956 {
957 QUIT;
958 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
245040d7 959 make_symbol_overload_list_block (func_name, b);
b6429628
DC
960 }
961
11309657 962 ALL_PRIMARY_SYMTABS (objfile, s)
b6429628
DC
963 {
964 QUIT;
965 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
966 /* Don't do this block twice. */
967 if (b == surrounding_static_block)
968 continue;
245040d7 969 make_symbol_overload_list_block (func_name, b);
b6429628 970 }
8d577d32
DC
971}
972
aff410f1 973/* Lookup the rtti type for a class name. */
362ff856
MC
974
975struct type *
976cp_lookup_rtti_type (const char *name, struct block *block)
977{
978 struct symbol * rtti_sym;
979 struct type * rtti_type;
980
2570f2b7 981 rtti_sym = lookup_symbol (name, block, STRUCT_DOMAIN, NULL);
362ff856
MC
982
983 if (rtti_sym == NULL)
984 {
8a3fe4f8 985 warning (_("RTTI symbol not found for class '%s'"), name);
362ff856
MC
986 return NULL;
987 }
988
989 if (SYMBOL_CLASS (rtti_sym) != LOC_TYPEDEF)
990 {
8a3fe4f8 991 warning (_("RTTI symbol for class '%s' is not a type"), name);
362ff856
MC
992 return NULL;
993 }
994
995 rtti_type = SYMBOL_TYPE (rtti_sym);
996
997 switch (TYPE_CODE (rtti_type))
998 {
999 case TYPE_CODE_CLASS:
1000 break;
1001 case TYPE_CODE_NAMESPACE:
1002 /* chastain/2003-11-26: the symbol tables often contain fake
1003 symbols for namespaces with the same name as the struct.
1004 This warning is an indication of a bug in the lookup order
1005 or a bug in the way that the symbol tables are populated. */
8a3fe4f8 1006 warning (_("RTTI symbol for class '%s' is a namespace"), name);
362ff856
MC
1007 return NULL;
1008 default:
8a3fe4f8 1009 warning (_("RTTI symbol for class '%s' has bad type"), name);
362ff856
MC
1010 return NULL;
1011 }
1012
1013 return rtti_type;
1014}
b6429628 1015
9219021c
DC
1016/* Don't allow just "maintenance cplus". */
1017
1018static void
1019maint_cplus_command (char *arg, int from_tty)
1020{
3e43a32a
MS
1021 printf_unfiltered (_("\"maintenance cplus\" must be followed "
1022 "by the name of a command.\n"));
aff410f1
MS
1023 help_list (maint_cplus_cmd_list,
1024 "maintenance cplus ",
1025 -1, gdb_stdout);
9219021c
DC
1026}
1027
1028/* This is a front end for cp_find_first_component, for unit testing.
1029 Be careful when using it: see the NOTE above
1030 cp_find_first_component. */
1031
1032static void
1033first_component_command (char *arg, int from_tty)
1034{
c836824f
AR
1035 int len;
1036 char *prefix;
1037
1038 if (!arg)
1039 return;
1040
1041 len = cp_find_first_component (arg);
1042 prefix = alloca (len + 1);
9219021c
DC
1043
1044 memcpy (prefix, arg, len);
1045 prefix[len] = '\0';
1046
1047 printf_unfiltered ("%s\n", prefix);
1048}
1049
b9362cc7
AC
1050extern initialize_file_ftype _initialize_cp_support; /* -Wmissing-prototypes */
1051
12907978
KS
1052#define SKIP_SPACE(P) \
1053 do \
1054 { \
1055 while (*(P) == ' ' || *(P) == '\t') \
1056 ++(P); \
1057 } \
1058 while (0)
1059
1060/* Returns the length of the operator name or 0 if INPUT does not
aff410f1
MS
1061 point to a valid C++ operator. INPUT should start with
1062 "operator". */
12907978
KS
1063int
1064cp_validate_operator (const char *input)
1065{
1066 int i;
1067 char *copy;
1068 const char *p;
1069 struct expression *expr;
1070 struct value *val;
1071 struct gdb_exception except;
12907978
KS
1072
1073 p = input;
1074
1075 if (strncmp (p, "operator", 8) == 0)
1076 {
1077 int valid = 0;
12907978 1078
c5504eaf 1079 p += 8;
12907978 1080 SKIP_SPACE (p);
aff410f1
MS
1081 for (i = 0;
1082 i < sizeof (operator_tokens) / sizeof (operator_tokens[0]);
12907978
KS
1083 ++i)
1084 {
1085 int length = strlen (operator_tokens[i]);
c5504eaf 1086
aff410f1
MS
1087 /* By using strncmp here, we MUST have operator_tokens
1088 ordered! See additional notes where operator_tokens is
1089 defined above. */
12907978
KS
1090 if (strncmp (p, operator_tokens[i], length) == 0)
1091 {
1092 const char *op = p;
c5504eaf 1093
12907978
KS
1094 valid = 1;
1095 p += length;
1096
1097 if (strncmp (op, "new", 3) == 0
1098 || strncmp (op, "delete", 6) == 0)
1099 {
1100
aff410f1
MS
1101 /* Special case: new[] and delete[]. We must be
1102 careful to swallow whitespace before/in "[]". */
12907978
KS
1103 SKIP_SPACE (p);
1104
1105 if (*p == '[')
1106 {
1107 ++p;
1108 SKIP_SPACE (p);
1109 if (*p == ']')
1110 ++p;
1111 else
1112 valid = 0;
1113 }
1114 }
1115
1116 if (valid)
1117 return (p - input);
1118 }
1119 }
1120
1121 /* Check input for a conversion operator. */
1122
aff410f1 1123 /* Skip past base typename. */
12907978
KS
1124 while (*p != '*' && *p != '&' && *p != 0 && *p != ' ')
1125 ++p;
1126 SKIP_SPACE (p);
1127
aff410f1 1128 /* Add modifiers '*' / '&'. */
12907978
KS
1129 while (*p == '*' || *p == '&')
1130 {
1131 ++p;
1132 SKIP_SPACE (p);
1133 }
1134
1135 /* Check for valid type. [Remember: input starts with
1136 "operator".] */
1137 copy = savestring (input + 8, p - input - 8);
1138 expr = NULL;
1139 val = NULL;
1140 TRY_CATCH (except, RETURN_MASK_ALL)
1141 {
1142 expr = parse_expression (copy);
1143 val = evaluate_type (expr);
1144 }
1145
1146 xfree (copy);
1147 if (expr)
1148 xfree (expr);
1149
1150 if (val != NULL && value_type (val) != NULL)
1151 return (p - input);
1152 }
1153
1154 return 0;
1155}
1156
9219021c
DC
1157void
1158_initialize_cp_support (void)
1159{
aff410f1
MS
1160 add_prefix_cmd ("cplus", class_maintenance,
1161 maint_cplus_command,
1162 _("C++ maintenance commands."),
1163 &maint_cplus_cmd_list,
1164 "maintenance cplus ",
1165 0, &maintenancelist);
1166 add_alias_cmd ("cp", "cplus",
1167 class_maintenance, 1,
1168 &maintenancelist);
1169
1170 add_cmd ("first_component",
1171 class_maintenance,
1172 first_component_command,
1a966eab 1173 _("Print the first class/namespace component of NAME."),
9219021c 1174 &maint_cplus_cmd_list);
9219021c 1175}
This page took 0.60666 seconds and 4 git commands to generate.