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