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