PR ld/12376
[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));
781 make_symbol_overload_list_block (name, block);
782
783 /* Look in the global block. */
784 block = block_global_block (block);
785 make_symbol_overload_list_block (name, block);
786
7322dca9
SW
787}
788
aff410f1
MS
789/* Search the namespace of the given type and namespace of and public
790 base types. */
7322dca9
SW
791
792static void
793make_symbol_overload_list_adl_namespace (struct type *type,
794 const char *func_name)
795{
796 char *namespace;
797 char *type_name;
798 int i, prefix_len;
799
aff410f1
MS
800 while (TYPE_CODE (type) == TYPE_CODE_PTR
801 || TYPE_CODE (type) == TYPE_CODE_REF
7322dca9
SW
802 || TYPE_CODE (type) == TYPE_CODE_ARRAY
803 || TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
804 {
805 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
806 type = check_typedef(type);
807 else
808 type = TYPE_TARGET_TYPE (type);
809 }
810
811 type_name = TYPE_NAME (type);
812
7d3fe98e
SW
813 if (type_name == NULL)
814 return;
815
7322dca9
SW
816 prefix_len = cp_entire_prefix_len (type_name);
817
818 if (prefix_len != 0)
819 {
820 namespace = alloca (prefix_len + 1);
821 strncpy (namespace, type_name, prefix_len);
822 namespace[prefix_len] = '\0';
823
824 make_symbol_overload_list_namespace (func_name, namespace);
825 }
826
827 /* Check public base type */
828 if (TYPE_CODE (type) == TYPE_CODE_CLASS)
829 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
830 {
831 if (BASETYPE_VIA_PUBLIC (type, i))
aff410f1
MS
832 make_symbol_overload_list_adl_namespace (TYPE_BASECLASS (type,
833 i),
7322dca9
SW
834 func_name);
835 }
836}
837
aff410f1
MS
838/* Adds the the overload list overload candidates for FUNC_NAME found
839 through argument dependent lookup. */
7322dca9
SW
840
841struct symbol **
842make_symbol_overload_list_adl (struct type **arg_types, int nargs,
843 const char *func_name)
844{
845 int i;
846
847 gdb_assert (sym_return_val_size != -1);
848
849 for (i = 1; i <= nargs; i++)
aff410f1
MS
850 make_symbol_overload_list_adl_namespace (arg_types[i - 1],
851 func_name);
7322dca9
SW
852
853 return sym_return_val;
854}
855
aff410f1
MS
856/* Used for cleanups to reset the "searched" flag in case of an
857 error. */
19c0c0f8
UW
858
859static void
860reset_directive_searched (void *data)
861{
862 struct using_direct *direct = data;
863 direct->searched = 0;
864}
865
8d577d32
DC
866/* This applies the using directives to add namespaces to search in,
867 and then searches for overloads in all of those namespaces. It
868 adds the symbols found to sym_return_val. Arguments are as in
869 make_symbol_overload_list. */
870
871static void
872make_symbol_overload_list_using (const char *func_name,
873 const char *namespace)
874{
19c0c0f8 875 struct using_direct *current;
4c3376c8 876 const struct block *block;
8d577d32
DC
877
878 /* First, go through the using directives. If any of them apply,
879 look in the appropriate namespaces for new functions to match
880 on. */
b6429628 881
4c3376c8
SW
882 for (block = get_selected_block (0);
883 block != NULL;
884 block = BLOCK_SUPERBLOCK (block))
885 for (current = block_using (block);
886 current != NULL;
887 current = current->next)
888 {
19c0c0f8
UW
889 /* Prevent recursive calls. */
890 if (current->searched)
891 continue;
892
aff410f1
MS
893 /* If this is a namespace alias or imported declaration ignore
894 it. */
4c3376c8
SW
895 if (current->alias != NULL || current->declaration != NULL)
896 continue;
897
898 if (strcmp (namespace, current->import_dest) == 0)
19c0c0f8 899 {
aff410f1
MS
900 /* Mark this import as searched so that the recursive call
901 does not search it again. */
19c0c0f8
UW
902 struct cleanup *old_chain;
903 current->searched = 1;
aff410f1
MS
904 old_chain = make_cleanup (reset_directive_searched,
905 current);
19c0c0f8 906
aff410f1
MS
907 make_symbol_overload_list_using (func_name,
908 current->import_src);
19c0c0f8
UW
909
910 current->searched = 0;
911 discard_cleanups (old_chain);
912 }
4c3376c8 913 }
b6429628 914
8d577d32 915 /* Now, add names for this namespace. */
7322dca9 916 make_symbol_overload_list_namespace (func_name, namespace);
8d577d32 917}
b6429628 918
8d577d32
DC
919/* This does the bulk of the work of finding overloaded symbols.
920 FUNC_NAME is the name of the overloaded function we're looking for
921 (possibly including namespace info). */
b6429628 922
8d577d32
DC
923static void
924make_symbol_overload_list_qualified (const char *func_name)
925{
926 struct symbol *sym;
927 struct symtab *s;
928 struct objfile *objfile;
929 const struct block *b, *surrounding_static_block = 0;
930 struct dict_iterator iter;
931 const struct dictionary *dict;
b6429628 932
aff410f1
MS
933 /* Look through the partial symtabs for all symbols which begin by
934 matching FUNC_NAME. Make sure we read that symbol table in. */
b6429628 935
ccefe4c4
TT
936 ALL_OBJFILES (objfile)
937 {
938 if (objfile->sf)
939 objfile->sf->qf->expand_symtabs_for_function (objfile, func_name);
940 }
b6429628
DC
941
942 /* Search upwards from currently selected frame (so that we can
943 complete on local vars. */
944
945 for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
245040d7 946 make_symbol_overload_list_block (func_name, b);
b6429628 947
8d577d32
DC
948 surrounding_static_block = block_static_block (get_selected_block (0));
949
b6429628
DC
950 /* Go through the symtabs and check the externs and statics for
951 symbols which match. */
952
11309657 953 ALL_PRIMARY_SYMTABS (objfile, s)
b6429628
DC
954 {
955 QUIT;
956 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
245040d7 957 make_symbol_overload_list_block (func_name, b);
b6429628
DC
958 }
959
11309657 960 ALL_PRIMARY_SYMTABS (objfile, s)
b6429628
DC
961 {
962 QUIT;
963 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
964 /* Don't do this block twice. */
965 if (b == surrounding_static_block)
966 continue;
245040d7 967 make_symbol_overload_list_block (func_name, b);
b6429628 968 }
8d577d32
DC
969}
970
aff410f1 971/* Lookup the rtti type for a class name. */
362ff856
MC
972
973struct type *
974cp_lookup_rtti_type (const char *name, struct block *block)
975{
976 struct symbol * rtti_sym;
977 struct type * rtti_type;
978
2570f2b7 979 rtti_sym = lookup_symbol (name, block, STRUCT_DOMAIN, NULL);
362ff856
MC
980
981 if (rtti_sym == NULL)
982 {
8a3fe4f8 983 warning (_("RTTI symbol not found for class '%s'"), name);
362ff856
MC
984 return NULL;
985 }
986
987 if (SYMBOL_CLASS (rtti_sym) != LOC_TYPEDEF)
988 {
8a3fe4f8 989 warning (_("RTTI symbol for class '%s' is not a type"), name);
362ff856
MC
990 return NULL;
991 }
992
993 rtti_type = SYMBOL_TYPE (rtti_sym);
994
995 switch (TYPE_CODE (rtti_type))
996 {
997 case TYPE_CODE_CLASS:
998 break;
999 case TYPE_CODE_NAMESPACE:
1000 /* chastain/2003-11-26: the symbol tables often contain fake
1001 symbols for namespaces with the same name as the struct.
1002 This warning is an indication of a bug in the lookup order
1003 or a bug in the way that the symbol tables are populated. */
8a3fe4f8 1004 warning (_("RTTI symbol for class '%s' is a namespace"), name);
362ff856
MC
1005 return NULL;
1006 default:
8a3fe4f8 1007 warning (_("RTTI symbol for class '%s' has bad type"), name);
362ff856
MC
1008 return NULL;
1009 }
1010
1011 return rtti_type;
1012}
b6429628 1013
9219021c
DC
1014/* Don't allow just "maintenance cplus". */
1015
1016static void
1017maint_cplus_command (char *arg, int from_tty)
1018{
3e43a32a
MS
1019 printf_unfiltered (_("\"maintenance cplus\" must be followed "
1020 "by the name of a command.\n"));
aff410f1
MS
1021 help_list (maint_cplus_cmd_list,
1022 "maintenance cplus ",
1023 -1, gdb_stdout);
9219021c
DC
1024}
1025
1026/* This is a front end for cp_find_first_component, for unit testing.
1027 Be careful when using it: see the NOTE above
1028 cp_find_first_component. */
1029
1030static void
1031first_component_command (char *arg, int from_tty)
1032{
c836824f
AR
1033 int len;
1034 char *prefix;
1035
1036 if (!arg)
1037 return;
1038
1039 len = cp_find_first_component (arg);
1040 prefix = alloca (len + 1);
9219021c
DC
1041
1042 memcpy (prefix, arg, len);
1043 prefix[len] = '\0';
1044
1045 printf_unfiltered ("%s\n", prefix);
1046}
1047
b9362cc7
AC
1048extern initialize_file_ftype _initialize_cp_support; /* -Wmissing-prototypes */
1049
12907978
KS
1050#define SKIP_SPACE(P) \
1051 do \
1052 { \
1053 while (*(P) == ' ' || *(P) == '\t') \
1054 ++(P); \
1055 } \
1056 while (0)
1057
1058/* Returns the length of the operator name or 0 if INPUT does not
aff410f1
MS
1059 point to a valid C++ operator. INPUT should start with
1060 "operator". */
12907978
KS
1061int
1062cp_validate_operator (const char *input)
1063{
1064 int i;
1065 char *copy;
1066 const char *p;
1067 struct expression *expr;
1068 struct value *val;
1069 struct gdb_exception except;
12907978
KS
1070
1071 p = input;
1072
1073 if (strncmp (p, "operator", 8) == 0)
1074 {
1075 int valid = 0;
12907978 1076
c5504eaf 1077 p += 8;
12907978 1078 SKIP_SPACE (p);
aff410f1
MS
1079 for (i = 0;
1080 i < sizeof (operator_tokens) / sizeof (operator_tokens[0]);
12907978
KS
1081 ++i)
1082 {
1083 int length = strlen (operator_tokens[i]);
c5504eaf 1084
aff410f1
MS
1085 /* By using strncmp here, we MUST have operator_tokens
1086 ordered! See additional notes where operator_tokens is
1087 defined above. */
12907978
KS
1088 if (strncmp (p, operator_tokens[i], length) == 0)
1089 {
1090 const char *op = p;
c5504eaf 1091
12907978
KS
1092 valid = 1;
1093 p += length;
1094
1095 if (strncmp (op, "new", 3) == 0
1096 || strncmp (op, "delete", 6) == 0)
1097 {
1098
aff410f1
MS
1099 /* Special case: new[] and delete[]. We must be
1100 careful to swallow whitespace before/in "[]". */
12907978
KS
1101 SKIP_SPACE (p);
1102
1103 if (*p == '[')
1104 {
1105 ++p;
1106 SKIP_SPACE (p);
1107 if (*p == ']')
1108 ++p;
1109 else
1110 valid = 0;
1111 }
1112 }
1113
1114 if (valid)
1115 return (p - input);
1116 }
1117 }
1118
1119 /* Check input for a conversion operator. */
1120
aff410f1 1121 /* Skip past base typename. */
12907978
KS
1122 while (*p != '*' && *p != '&' && *p != 0 && *p != ' ')
1123 ++p;
1124 SKIP_SPACE (p);
1125
aff410f1 1126 /* Add modifiers '*' / '&'. */
12907978
KS
1127 while (*p == '*' || *p == '&')
1128 {
1129 ++p;
1130 SKIP_SPACE (p);
1131 }
1132
1133 /* Check for valid type. [Remember: input starts with
1134 "operator".] */
1135 copy = savestring (input + 8, p - input - 8);
1136 expr = NULL;
1137 val = NULL;
1138 TRY_CATCH (except, RETURN_MASK_ALL)
1139 {
1140 expr = parse_expression (copy);
1141 val = evaluate_type (expr);
1142 }
1143
1144 xfree (copy);
1145 if (expr)
1146 xfree (expr);
1147
1148 if (val != NULL && value_type (val) != NULL)
1149 return (p - input);
1150 }
1151
1152 return 0;
1153}
1154
9219021c
DC
1155void
1156_initialize_cp_support (void)
1157{
aff410f1
MS
1158 add_prefix_cmd ("cplus", class_maintenance,
1159 maint_cplus_command,
1160 _("C++ maintenance commands."),
1161 &maint_cplus_cmd_list,
1162 "maintenance cplus ",
1163 0, &maintenancelist);
1164 add_alias_cmd ("cp", "cplus",
1165 class_maintenance, 1,
1166 &maintenancelist);
1167
1168 add_cmd ("first_component",
1169 class_maintenance,
1170 first_component_command,
1a966eab 1171 _("Print the first class/namespace component of NAME."),
9219021c 1172 &maint_cplus_cmd_list);
9219021c 1173}
This page took 0.700285 seconds and 4 git commands to generate.