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