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