| 1 | /* Helper routines for C++ support in GDB. |
| 2 | Copyright (C) 2002, 2003, 2004, 2005 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 2 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, write to the Free Software |
| 20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 21 | Boston, MA 02110-1301, USA. */ |
| 22 | |
| 23 | #include "defs.h" |
| 24 | #include <ctype.h> |
| 25 | #include "cp-support.h" |
| 26 | #include "gdb_string.h" |
| 27 | #include "demangle.h" |
| 28 | #include "gdb_assert.h" |
| 29 | #include "gdbcmd.h" |
| 30 | #include "dictionary.h" |
| 31 | #include "objfiles.h" |
| 32 | #include "frame.h" |
| 33 | #include "symtab.h" |
| 34 | #include "block.h" |
| 35 | #include "complaints.h" |
| 36 | #include "gdbtypes.h" |
| 37 | |
| 38 | #define d_left(dc) (dc)->u.s_binary.left |
| 39 | #define d_right(dc) (dc)->u.s_binary.right |
| 40 | |
| 41 | /* Functions related to demangled name parsing. */ |
| 42 | |
| 43 | static unsigned int cp_find_first_component_aux (const char *name, |
| 44 | int permissive); |
| 45 | |
| 46 | static void demangled_name_complaint (const char *name); |
| 47 | |
| 48 | /* Functions/variables related to overload resolution. */ |
| 49 | |
| 50 | static int sym_return_val_size; |
| 51 | static int sym_return_val_index; |
| 52 | static struct symbol **sym_return_val; |
| 53 | |
| 54 | static void overload_list_add_symbol (struct symbol *sym, |
| 55 | const char *oload_name); |
| 56 | |
| 57 | static void make_symbol_overload_list_using (const char *func_name, |
| 58 | const char *namespace); |
| 59 | |
| 60 | static void make_symbol_overload_list_qualified (const char *func_name); |
| 61 | |
| 62 | static void read_in_psymtabs (const char *oload_name); |
| 63 | |
| 64 | /* The list of "maint cplus" commands. */ |
| 65 | |
| 66 | struct cmd_list_element *maint_cplus_cmd_list = NULL; |
| 67 | |
| 68 | /* The actual commands. */ |
| 69 | |
| 70 | static void maint_cplus_command (char *arg, int from_tty); |
| 71 | static void first_component_command (char *arg, int from_tty); |
| 72 | |
| 73 | /* Return the canonicalized form of STRING, or NULL if STRING can not be |
| 74 | parsed. The return value is allocated via xmalloc. |
| 75 | |
| 76 | drow/2005-03-07: Should we also return NULL for things that trivially do |
| 77 | not require any change? e.g. simple identifiers. This could be more |
| 78 | efficient. */ |
| 79 | |
| 80 | char * |
| 81 | cp_canonicalize_string (const char *string) |
| 82 | { |
| 83 | void *storage; |
| 84 | struct demangle_component *ret_comp; |
| 85 | char *ret; |
| 86 | int len = strlen (string); |
| 87 | |
| 88 | len = len + len / 8; |
| 89 | |
| 90 | ret_comp = cp_demangled_name_to_comp (string, &storage, NULL); |
| 91 | if (ret_comp == NULL) |
| 92 | return NULL; |
| 93 | |
| 94 | ret = cp_comp_to_string (ret_comp, len); |
| 95 | |
| 96 | xfree (storage); |
| 97 | |
| 98 | return ret; |
| 99 | } |
| 100 | |
| 101 | /* Convert a mangled name to a demangle_component tree. *MEMORY is set to the |
| 102 | block of used memory that should be freed when finished with the tree. |
| 103 | DEMANGLED_P is set to the char * that should be freed when finished with |
| 104 | the tree, or NULL if none was needed. OPTIONS will be passed to the |
| 105 | demangler. */ |
| 106 | |
| 107 | static struct demangle_component * |
| 108 | mangled_name_to_comp (const char *mangled_name, int options, |
| 109 | void **memory, char **demangled_p) |
| 110 | { |
| 111 | struct demangle_component *ret; |
| 112 | char *demangled_name; |
| 113 | int len; |
| 114 | |
| 115 | /* If it looks like a v3 mangled name, then try to go directly |
| 116 | to trees. */ |
| 117 | if (mangled_name[0] == '_' && mangled_name[1] == 'Z') |
| 118 | { |
| 119 | ret = cplus_demangle_v3_components (mangled_name, options, memory); |
| 120 | if (ret) |
| 121 | { |
| 122 | *demangled_p = NULL; |
| 123 | return ret; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /* If it doesn't, or if that failed, then try to demangle the name. */ |
| 128 | demangled_name = cplus_demangle (mangled_name, options); |
| 129 | if (demangled_name == NULL) |
| 130 | return NULL; |
| 131 | |
| 132 | /* If we could demangle the name, parse it to build the component tree. */ |
| 133 | ret = cp_demangled_name_to_comp (demangled_name, memory, NULL); |
| 134 | |
| 135 | if (ret == NULL) |
| 136 | { |
| 137 | free (demangled_name); |
| 138 | return NULL; |
| 139 | } |
| 140 | |
| 141 | *demangled_p = demangled_name; |
| 142 | return ret; |
| 143 | } |
| 144 | |
| 145 | /* Return the name of the class containing method PHYSNAME. */ |
| 146 | |
| 147 | char * |
| 148 | cp_class_name_from_physname (const char *physname) |
| 149 | { |
| 150 | void *storage; |
| 151 | char *demangled_name = NULL, *ret; |
| 152 | struct demangle_component *ret_comp, *prev_comp, *cur_comp; |
| 153 | int done; |
| 154 | |
| 155 | ret_comp = mangled_name_to_comp (physname, DMGL_ANSI, &storage, |
| 156 | &demangled_name); |
| 157 | if (ret_comp == NULL) |
| 158 | return NULL; |
| 159 | |
| 160 | done = 0; |
| 161 | |
| 162 | /* First strip off any qualifiers, if we have a function or method. */ |
| 163 | while (!done) |
| 164 | switch (ret_comp->type) |
| 165 | { |
| 166 | case DEMANGLE_COMPONENT_CONST: |
| 167 | case DEMANGLE_COMPONENT_RESTRICT: |
| 168 | case DEMANGLE_COMPONENT_VOLATILE: |
| 169 | case DEMANGLE_COMPONENT_CONST_THIS: |
| 170 | case DEMANGLE_COMPONENT_RESTRICT_THIS: |
| 171 | case DEMANGLE_COMPONENT_VOLATILE_THIS: |
| 172 | case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: |
| 173 | ret_comp = d_left (ret_comp); |
| 174 | break; |
| 175 | default: |
| 176 | done = 1; |
| 177 | break; |
| 178 | } |
| 179 | |
| 180 | /* If what we have now is a function, discard the argument list. */ |
| 181 | if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME) |
| 182 | ret_comp = d_left (ret_comp); |
| 183 | |
| 184 | /* If what we have now is a template, strip off the template |
| 185 | arguments. The left subtree may be a qualified name. */ |
| 186 | if (ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE) |
| 187 | ret_comp = d_left (ret_comp); |
| 188 | |
| 189 | /* What we have now should be a name, possibly qualified. Additional |
| 190 | qualifiers could live in the left subtree or the right subtree. Find |
| 191 | the last piece. */ |
| 192 | done = 0; |
| 193 | prev_comp = NULL; |
| 194 | cur_comp = ret_comp; |
| 195 | while (!done) |
| 196 | switch (cur_comp->type) |
| 197 | { |
| 198 | case DEMANGLE_COMPONENT_QUAL_NAME: |
| 199 | case DEMANGLE_COMPONENT_LOCAL_NAME: |
| 200 | prev_comp = cur_comp; |
| 201 | cur_comp = d_right (cur_comp); |
| 202 | break; |
| 203 | case DEMANGLE_COMPONENT_TEMPLATE: |
| 204 | case DEMANGLE_COMPONENT_NAME: |
| 205 | case DEMANGLE_COMPONENT_CTOR: |
| 206 | case DEMANGLE_COMPONENT_DTOR: |
| 207 | case DEMANGLE_COMPONENT_OPERATOR: |
| 208 | case DEMANGLE_COMPONENT_EXTENDED_OPERATOR: |
| 209 | done = 1; |
| 210 | break; |
| 211 | default: |
| 212 | done = 1; |
| 213 | cur_comp = NULL; |
| 214 | break; |
| 215 | } |
| 216 | |
| 217 | ret = NULL; |
| 218 | if (cur_comp != NULL && prev_comp != NULL) |
| 219 | { |
| 220 | /* We want to discard the rightmost child of PREV_COMP. */ |
| 221 | *prev_comp = *d_left (prev_comp); |
| 222 | /* The ten is completely arbitrary; we don't have a good estimate. */ |
| 223 | ret = cp_comp_to_string (ret_comp, 10); |
| 224 | } |
| 225 | |
| 226 | xfree (storage); |
| 227 | if (demangled_name) |
| 228 | xfree (demangled_name); |
| 229 | return ret; |
| 230 | } |
| 231 | |
| 232 | /* Return the child of COMP which is the basename of a method, variable, |
| 233 | et cetera. All scope qualifiers are discarded, but template arguments |
| 234 | will be included. The component tree may be modified. */ |
| 235 | |
| 236 | static struct demangle_component * |
| 237 | unqualified_name_from_comp (struct demangle_component *comp) |
| 238 | { |
| 239 | struct demangle_component *ret_comp = comp, *last_template; |
| 240 | int done; |
| 241 | |
| 242 | done = 0; |
| 243 | last_template = NULL; |
| 244 | while (!done) |
| 245 | switch (ret_comp->type) |
| 246 | { |
| 247 | case DEMANGLE_COMPONENT_QUAL_NAME: |
| 248 | case DEMANGLE_COMPONENT_LOCAL_NAME: |
| 249 | ret_comp = d_right (ret_comp); |
| 250 | break; |
| 251 | case DEMANGLE_COMPONENT_TYPED_NAME: |
| 252 | ret_comp = d_left (ret_comp); |
| 253 | break; |
| 254 | case DEMANGLE_COMPONENT_TEMPLATE: |
| 255 | gdb_assert (last_template == NULL); |
| 256 | last_template = ret_comp; |
| 257 | ret_comp = d_left (ret_comp); |
| 258 | break; |
| 259 | case DEMANGLE_COMPONENT_CONST: |
| 260 | case DEMANGLE_COMPONENT_RESTRICT: |
| 261 | case DEMANGLE_COMPONENT_VOLATILE: |
| 262 | case DEMANGLE_COMPONENT_CONST_THIS: |
| 263 | case DEMANGLE_COMPONENT_RESTRICT_THIS: |
| 264 | case DEMANGLE_COMPONENT_VOLATILE_THIS: |
| 265 | case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: |
| 266 | ret_comp = d_left (ret_comp); |
| 267 | break; |
| 268 | case DEMANGLE_COMPONENT_NAME: |
| 269 | case DEMANGLE_COMPONENT_CTOR: |
| 270 | case DEMANGLE_COMPONENT_DTOR: |
| 271 | case DEMANGLE_COMPONENT_OPERATOR: |
| 272 | case DEMANGLE_COMPONENT_EXTENDED_OPERATOR: |
| 273 | done = 1; |
| 274 | break; |
| 275 | default: |
| 276 | return NULL; |
| 277 | break; |
| 278 | } |
| 279 | |
| 280 | if (last_template) |
| 281 | { |
| 282 | d_left (last_template) = ret_comp; |
| 283 | return last_template; |
| 284 | } |
| 285 | |
| 286 | return ret_comp; |
| 287 | } |
| 288 | |
| 289 | /* Return the name of the method whose linkage name is PHYSNAME. */ |
| 290 | |
| 291 | char * |
| 292 | method_name_from_physname (const char *physname) |
| 293 | { |
| 294 | void *storage; |
| 295 | char *demangled_name = NULL, *ret; |
| 296 | struct demangle_component *ret_comp; |
| 297 | int done; |
| 298 | |
| 299 | ret_comp = mangled_name_to_comp (physname, DMGL_ANSI, &storage, |
| 300 | &demangled_name); |
| 301 | if (ret_comp == NULL) |
| 302 | return NULL; |
| 303 | |
| 304 | ret_comp = unqualified_name_from_comp (ret_comp); |
| 305 | |
| 306 | ret = NULL; |
| 307 | if (ret_comp != NULL) |
| 308 | /* The ten is completely arbitrary; we don't have a good estimate. */ |
| 309 | ret = cp_comp_to_string (ret_comp, 10); |
| 310 | |
| 311 | xfree (storage); |
| 312 | if (demangled_name) |
| 313 | xfree (demangled_name); |
| 314 | return ret; |
| 315 | } |
| 316 | |
| 317 | /* If FULL_NAME is the demangled name of a C++ function (including an |
| 318 | arg list, possibly including namespace/class qualifications), |
| 319 | return a new string containing only the function name (without the |
| 320 | arg list/class qualifications). Otherwise, return NULL. The |
| 321 | caller is responsible for freeing the memory in question. */ |
| 322 | |
| 323 | char * |
| 324 | cp_func_name (const char *full_name) |
| 325 | { |
| 326 | void *storage; |
| 327 | char *ret; |
| 328 | struct demangle_component *ret_comp; |
| 329 | int done; |
| 330 | |
| 331 | ret_comp = cp_demangled_name_to_comp (full_name, &storage, NULL); |
| 332 | if (!ret_comp) |
| 333 | return NULL; |
| 334 | |
| 335 | ret_comp = unqualified_name_from_comp (ret_comp); |
| 336 | |
| 337 | ret = NULL; |
| 338 | if (ret_comp != NULL) |
| 339 | ret = cp_comp_to_string (ret_comp, 10); |
| 340 | |
| 341 | xfree (storage); |
| 342 | return ret; |
| 343 | } |
| 344 | |
| 345 | /* DEMANGLED_NAME is the name of a function, including parameters and |
| 346 | (optionally) a return type. Return the name of the function without |
| 347 | parameters or return type, or NULL if we can not parse the name. */ |
| 348 | |
| 349 | static char * |
| 350 | remove_params (const char *demangled_name) |
| 351 | { |
| 352 | int done = 0; |
| 353 | struct demangle_component *ret_comp; |
| 354 | void *storage; |
| 355 | char *ret = NULL; |
| 356 | |
| 357 | if (demangled_name == NULL) |
| 358 | return NULL; |
| 359 | |
| 360 | ret_comp = cp_demangled_name_to_comp (demangled_name, &storage, NULL); |
| 361 | if (ret_comp == NULL) |
| 362 | return NULL; |
| 363 | |
| 364 | /* First strip off any qualifiers, if we have a function or method. */ |
| 365 | while (!done) |
| 366 | switch (ret_comp->type) |
| 367 | { |
| 368 | case DEMANGLE_COMPONENT_CONST: |
| 369 | case DEMANGLE_COMPONENT_RESTRICT: |
| 370 | case DEMANGLE_COMPONENT_VOLATILE: |
| 371 | case DEMANGLE_COMPONENT_CONST_THIS: |
| 372 | case DEMANGLE_COMPONENT_RESTRICT_THIS: |
| 373 | case DEMANGLE_COMPONENT_VOLATILE_THIS: |
| 374 | case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: |
| 375 | ret_comp = d_left (ret_comp); |
| 376 | break; |
| 377 | default: |
| 378 | done = 1; |
| 379 | break; |
| 380 | } |
| 381 | |
| 382 | /* What we have now should be a function. Return its name. */ |
| 383 | if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME) |
| 384 | ret = cp_comp_to_string (d_left (ret_comp), 10); |
| 385 | |
| 386 | xfree (storage); |
| 387 | return ret; |
| 388 | } |
| 389 | |
| 390 | /* Here are some random pieces of trivia to keep in mind while trying |
| 391 | to take apart demangled names: |
| 392 | |
| 393 | - Names can contain function arguments or templates, so the process |
| 394 | has to be, to some extent recursive: maybe keep track of your |
| 395 | depth based on encountering <> and (). |
| 396 | |
| 397 | - Parentheses don't just have to happen at the end of a name: they |
| 398 | can occur even if the name in question isn't a function, because |
| 399 | a template argument might be a type that's a function. |
| 400 | |
| 401 | - Conversely, even if you're trying to deal with a function, its |
| 402 | demangled name might not end with ')': it could be a const or |
| 403 | volatile class method, in which case it ends with "const" or |
| 404 | "volatile". |
| 405 | |
| 406 | - Parentheses are also used in anonymous namespaces: a variable |
| 407 | 'foo' in an anonymous namespace gets demangled as "(anonymous |
| 408 | namespace)::foo". |
| 409 | |
| 410 | - And operator names can contain parentheses or angle brackets. */ |
| 411 | |
| 412 | /* FIXME: carlton/2003-03-13: We have several functions here with |
| 413 | overlapping functionality; can we combine them? Also, do they |
| 414 | handle all the above considerations correctly? */ |
| 415 | |
| 416 | |
| 417 | /* This returns the length of first component of NAME, which should be |
| 418 | the demangled name of a C++ variable/function/method/etc. |
| 419 | Specifically, it returns the index of the first colon forming the |
| 420 | boundary of the first component: so, given 'A::foo' or 'A::B::foo' |
| 421 | it returns the 1, and given 'foo', it returns 0. */ |
| 422 | |
| 423 | /* The character in NAME indexed by the return value is guaranteed to |
| 424 | always be either ':' or '\0'. */ |
| 425 | |
| 426 | /* NOTE: carlton/2003-03-13: This function is currently only intended |
| 427 | for internal use: it's probably not entirely safe when called on |
| 428 | user-generated input, because some of the 'index += 2' lines in |
| 429 | cp_find_first_component_aux might go past the end of malformed |
| 430 | input. */ |
| 431 | |
| 432 | unsigned int |
| 433 | cp_find_first_component (const char *name) |
| 434 | { |
| 435 | return cp_find_first_component_aux (name, 0); |
| 436 | } |
| 437 | |
| 438 | /* Helper function for cp_find_first_component. Like that function, |
| 439 | it returns the length of the first component of NAME, but to make |
| 440 | the recursion easier, it also stops if it reaches an unexpected ')' |
| 441 | or '>' if the value of PERMISSIVE is nonzero. */ |
| 442 | |
| 443 | /* Let's optimize away calls to strlen("operator"). */ |
| 444 | |
| 445 | #define LENGTH_OF_OPERATOR 8 |
| 446 | |
| 447 | static unsigned int |
| 448 | cp_find_first_component_aux (const char *name, int permissive) |
| 449 | { |
| 450 | unsigned int index = 0; |
| 451 | /* Operator names can show up in unexpected places. Since these can |
| 452 | contain parentheses or angle brackets, they can screw up the |
| 453 | recursion. But not every string 'operator' is part of an |
| 454 | operater name: e.g. you could have a variable 'cooperator'. So |
| 455 | this variable tells us whether or not we should treat the string |
| 456 | 'operator' as starting an operator. */ |
| 457 | int operator_possible = 1; |
| 458 | |
| 459 | for (;; ++index) |
| 460 | { |
| 461 | switch (name[index]) |
| 462 | { |
| 463 | case '<': |
| 464 | /* Template; eat it up. The calls to cp_first_component |
| 465 | should only return (I hope!) when they reach the '>' |
| 466 | terminating the component or a '::' between two |
| 467 | components. (Hence the '+ 2'.) */ |
| 468 | index += 1; |
| 469 | for (index += cp_find_first_component_aux (name + index, 1); |
| 470 | name[index] != '>'; |
| 471 | index += cp_find_first_component_aux (name + index, 1)) |
| 472 | { |
| 473 | if (name[index] != ':') |
| 474 | { |
| 475 | demangled_name_complaint (name); |
| 476 | return strlen (name); |
| 477 | } |
| 478 | index += 2; |
| 479 | } |
| 480 | operator_possible = 1; |
| 481 | break; |
| 482 | case '(': |
| 483 | /* Similar comment as to '<'. */ |
| 484 | index += 1; |
| 485 | for (index += cp_find_first_component_aux (name + index, 1); |
| 486 | name[index] != ')'; |
| 487 | index += cp_find_first_component_aux (name + index, 1)) |
| 488 | { |
| 489 | if (name[index] != ':') |
| 490 | { |
| 491 | demangled_name_complaint (name); |
| 492 | return strlen (name); |
| 493 | } |
| 494 | index += 2; |
| 495 | } |
| 496 | operator_possible = 1; |
| 497 | break; |
| 498 | case '>': |
| 499 | case ')': |
| 500 | if (permissive) |
| 501 | return index; |
| 502 | else |
| 503 | { |
| 504 | demangled_name_complaint (name); |
| 505 | return strlen (name); |
| 506 | } |
| 507 | case '\0': |
| 508 | case ':': |
| 509 | return index; |
| 510 | case 'o': |
| 511 | /* Operator names can screw up the recursion. */ |
| 512 | if (operator_possible |
| 513 | && strncmp (name + index, "operator", LENGTH_OF_OPERATOR) == 0) |
| 514 | { |
| 515 | index += LENGTH_OF_OPERATOR; |
| 516 | while (isspace(name[index])) |
| 517 | ++index; |
| 518 | switch (name[index]) |
| 519 | { |
| 520 | /* Skip over one less than the appropriate number of |
| 521 | characters: the for loop will skip over the last |
| 522 | one. */ |
| 523 | case '<': |
| 524 | if (name[index + 1] == '<') |
| 525 | index += 1; |
| 526 | else |
| 527 | index += 0; |
| 528 | break; |
| 529 | case '>': |
| 530 | case '-': |
| 531 | if (name[index + 1] == '>') |
| 532 | index += 1; |
| 533 | else |
| 534 | index += 0; |
| 535 | break; |
| 536 | case '(': |
| 537 | index += 1; |
| 538 | break; |
| 539 | default: |
| 540 | index += 0; |
| 541 | break; |
| 542 | } |
| 543 | } |
| 544 | operator_possible = 0; |
| 545 | break; |
| 546 | case ' ': |
| 547 | case ',': |
| 548 | case '.': |
| 549 | case '&': |
| 550 | case '*': |
| 551 | /* NOTE: carlton/2003-04-18: I'm not sure what the precise |
| 552 | set of relevant characters are here: it's necessary to |
| 553 | include any character that can show up before 'operator' |
| 554 | in a demangled name, and it's safe to include any |
| 555 | character that can't be part of an identifier's name. */ |
| 556 | operator_possible = 1; |
| 557 | break; |
| 558 | default: |
| 559 | operator_possible = 0; |
| 560 | break; |
| 561 | } |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | /* Complain about a demangled name that we don't know how to parse. |
| 566 | NAME is the demangled name in question. */ |
| 567 | |
| 568 | static void |
| 569 | demangled_name_complaint (const char *name) |
| 570 | { |
| 571 | complaint (&symfile_complaints, |
| 572 | "unexpected demangled name '%s'", name); |
| 573 | } |
| 574 | |
| 575 | /* If NAME is the fully-qualified name of a C++ |
| 576 | function/variable/method/etc., this returns the length of its |
| 577 | entire prefix: all of the namespaces and classes that make up its |
| 578 | name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns |
| 579 | 4, given 'foo', it returns 0. */ |
| 580 | |
| 581 | unsigned int |
| 582 | cp_entire_prefix_len (const char *name) |
| 583 | { |
| 584 | unsigned int current_len = cp_find_first_component (name); |
| 585 | unsigned int previous_len = 0; |
| 586 | |
| 587 | while (name[current_len] != '\0') |
| 588 | { |
| 589 | gdb_assert (name[current_len] == ':'); |
| 590 | previous_len = current_len; |
| 591 | /* Skip the '::'. */ |
| 592 | current_len += 2; |
| 593 | current_len += cp_find_first_component (name + current_len); |
| 594 | } |
| 595 | |
| 596 | return previous_len; |
| 597 | } |
| 598 | |
| 599 | /* Overload resolution functions. */ |
| 600 | |
| 601 | /* Test to see if SYM is a symbol that we haven't seen corresponding |
| 602 | to a function named OLOAD_NAME. If so, add it to the current |
| 603 | completion list. */ |
| 604 | |
| 605 | static void |
| 606 | overload_list_add_symbol (struct symbol *sym, const char *oload_name) |
| 607 | { |
| 608 | int newsize; |
| 609 | int i; |
| 610 | char *sym_name; |
| 611 | |
| 612 | /* If there is no type information, we can't do anything, so skip */ |
| 613 | if (SYMBOL_TYPE (sym) == NULL) |
| 614 | return; |
| 615 | |
| 616 | /* skip any symbols that we've already considered. */ |
| 617 | for (i = 0; i < sym_return_val_index; ++i) |
| 618 | if (strcmp (SYMBOL_LINKAGE_NAME (sym), |
| 619 | SYMBOL_LINKAGE_NAME (sym_return_val[i])) == 0) |
| 620 | return; |
| 621 | |
| 622 | /* Get the demangled name without parameters */ |
| 623 | sym_name = remove_params (SYMBOL_NATURAL_NAME (sym)); |
| 624 | if (!sym_name) |
| 625 | return; |
| 626 | |
| 627 | /* skip symbols that cannot match */ |
| 628 | if (strcmp (sym_name, oload_name) != 0) |
| 629 | { |
| 630 | xfree (sym_name); |
| 631 | return; |
| 632 | } |
| 633 | |
| 634 | xfree (sym_name); |
| 635 | |
| 636 | /* We have a match for an overload instance, so add SYM to the current list |
| 637 | * of overload instances */ |
| 638 | if (sym_return_val_index + 3 > sym_return_val_size) |
| 639 | { |
| 640 | newsize = (sym_return_val_size *= 2) * sizeof (struct symbol *); |
| 641 | sym_return_val = (struct symbol **) xrealloc ((char *) sym_return_val, newsize); |
| 642 | } |
| 643 | sym_return_val[sym_return_val_index++] = sym; |
| 644 | sym_return_val[sym_return_val_index] = NULL; |
| 645 | } |
| 646 | |
| 647 | /* Return a null-terminated list of pointers to function symbols that |
| 648 | are named FUNC_NAME and are visible within NAMESPACE. */ |
| 649 | |
| 650 | struct symbol ** |
| 651 | make_symbol_overload_list (const char *func_name, |
| 652 | const char *namespace) |
| 653 | { |
| 654 | struct cleanup *old_cleanups; |
| 655 | |
| 656 | sym_return_val_size = 100; |
| 657 | sym_return_val_index = 0; |
| 658 | sym_return_val = xmalloc ((sym_return_val_size + 1) * |
| 659 | sizeof (struct symbol *)); |
| 660 | sym_return_val[0] = NULL; |
| 661 | |
| 662 | old_cleanups = make_cleanup (xfree, sym_return_val); |
| 663 | |
| 664 | make_symbol_overload_list_using (func_name, namespace); |
| 665 | |
| 666 | discard_cleanups (old_cleanups); |
| 667 | |
| 668 | return sym_return_val; |
| 669 | } |
| 670 | |
| 671 | /* This applies the using directives to add namespaces to search in, |
| 672 | and then searches for overloads in all of those namespaces. It |
| 673 | adds the symbols found to sym_return_val. Arguments are as in |
| 674 | make_symbol_overload_list. */ |
| 675 | |
| 676 | static void |
| 677 | make_symbol_overload_list_using (const char *func_name, |
| 678 | const char *namespace) |
| 679 | { |
| 680 | const struct using_direct *current; |
| 681 | |
| 682 | /* First, go through the using directives. If any of them apply, |
| 683 | look in the appropriate namespaces for new functions to match |
| 684 | on. */ |
| 685 | |
| 686 | for (current = block_using (get_selected_block (0)); |
| 687 | current != NULL; |
| 688 | current = current->next) |
| 689 | { |
| 690 | if (strcmp (namespace, current->outer) == 0) |
| 691 | { |
| 692 | make_symbol_overload_list_using (func_name, |
| 693 | current->inner); |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | /* Now, add names for this namespace. */ |
| 698 | |
| 699 | if (namespace[0] == '\0') |
| 700 | { |
| 701 | make_symbol_overload_list_qualified (func_name); |
| 702 | } |
| 703 | else |
| 704 | { |
| 705 | char *concatenated_name |
| 706 | = alloca (strlen (namespace) + 2 + strlen (func_name) + 1); |
| 707 | strcpy (concatenated_name, namespace); |
| 708 | strcat (concatenated_name, "::"); |
| 709 | strcat (concatenated_name, func_name); |
| 710 | make_symbol_overload_list_qualified (concatenated_name); |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | /* This does the bulk of the work of finding overloaded symbols. |
| 715 | FUNC_NAME is the name of the overloaded function we're looking for |
| 716 | (possibly including namespace info). */ |
| 717 | |
| 718 | static void |
| 719 | make_symbol_overload_list_qualified (const char *func_name) |
| 720 | { |
| 721 | struct symbol *sym; |
| 722 | struct symtab *s; |
| 723 | struct objfile *objfile; |
| 724 | const struct block *b, *surrounding_static_block = 0; |
| 725 | struct dict_iterator iter; |
| 726 | const struct dictionary *dict; |
| 727 | |
| 728 | /* Look through the partial symtabs for all symbols which begin |
| 729 | by matching FUNC_NAME. Make sure we read that symbol table in. */ |
| 730 | |
| 731 | read_in_psymtabs (func_name); |
| 732 | |
| 733 | /* Search upwards from currently selected frame (so that we can |
| 734 | complete on local vars. */ |
| 735 | |
| 736 | for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b)) |
| 737 | { |
| 738 | dict = BLOCK_DICT (b); |
| 739 | |
| 740 | for (sym = dict_iter_name_first (dict, func_name, &iter); |
| 741 | sym; |
| 742 | sym = dict_iter_name_next (func_name, &iter)) |
| 743 | { |
| 744 | overload_list_add_symbol (sym, func_name); |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | surrounding_static_block = block_static_block (get_selected_block (0)); |
| 749 | |
| 750 | /* Go through the symtabs and check the externs and statics for |
| 751 | symbols which match. */ |
| 752 | |
| 753 | ALL_SYMTABS (objfile, s) |
| 754 | { |
| 755 | QUIT; |
| 756 | b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK); |
| 757 | dict = BLOCK_DICT (b); |
| 758 | |
| 759 | for (sym = dict_iter_name_first (dict, func_name, &iter); |
| 760 | sym; |
| 761 | sym = dict_iter_name_next (func_name, &iter)) |
| 762 | { |
| 763 | overload_list_add_symbol (sym, func_name); |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | ALL_SYMTABS (objfile, s) |
| 768 | { |
| 769 | QUIT; |
| 770 | b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK); |
| 771 | /* Don't do this block twice. */ |
| 772 | if (b == surrounding_static_block) |
| 773 | continue; |
| 774 | dict = BLOCK_DICT (b); |
| 775 | |
| 776 | for (sym = dict_iter_name_first (dict, func_name, &iter); |
| 777 | sym; |
| 778 | sym = dict_iter_name_next (func_name, &iter)) |
| 779 | { |
| 780 | overload_list_add_symbol (sym, func_name); |
| 781 | } |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | /* Look through the partial symtabs for all symbols which begin |
| 786 | by matching FUNC_NAME. Make sure we read that symbol table in. */ |
| 787 | |
| 788 | static void |
| 789 | read_in_psymtabs (const char *func_name) |
| 790 | { |
| 791 | struct partial_symtab *ps; |
| 792 | struct objfile *objfile; |
| 793 | |
| 794 | ALL_PSYMTABS (objfile, ps) |
| 795 | { |
| 796 | if (ps->readin) |
| 797 | continue; |
| 798 | |
| 799 | if ((lookup_partial_symbol (ps, func_name, NULL, 1, VAR_DOMAIN) |
| 800 | != NULL) |
| 801 | || (lookup_partial_symbol (ps, func_name, NULL, 0, VAR_DOMAIN) |
| 802 | != NULL)) |
| 803 | psymtab_to_symtab (ps); |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | /* Lookup the rtti type for a class name. */ |
| 808 | |
| 809 | struct type * |
| 810 | cp_lookup_rtti_type (const char *name, struct block *block) |
| 811 | { |
| 812 | struct symbol * rtti_sym; |
| 813 | struct type * rtti_type; |
| 814 | |
| 815 | rtti_sym = lookup_symbol (name, block, STRUCT_DOMAIN, NULL, NULL); |
| 816 | |
| 817 | if (rtti_sym == NULL) |
| 818 | { |
| 819 | warning (_("RTTI symbol not found for class '%s'"), name); |
| 820 | return NULL; |
| 821 | } |
| 822 | |
| 823 | if (SYMBOL_CLASS (rtti_sym) != LOC_TYPEDEF) |
| 824 | { |
| 825 | warning (_("RTTI symbol for class '%s' is not a type"), name); |
| 826 | return NULL; |
| 827 | } |
| 828 | |
| 829 | rtti_type = SYMBOL_TYPE (rtti_sym); |
| 830 | |
| 831 | switch (TYPE_CODE (rtti_type)) |
| 832 | { |
| 833 | case TYPE_CODE_CLASS: |
| 834 | break; |
| 835 | case TYPE_CODE_NAMESPACE: |
| 836 | /* chastain/2003-11-26: the symbol tables often contain fake |
| 837 | symbols for namespaces with the same name as the struct. |
| 838 | This warning is an indication of a bug in the lookup order |
| 839 | or a bug in the way that the symbol tables are populated. */ |
| 840 | warning (_("RTTI symbol for class '%s' is a namespace"), name); |
| 841 | return NULL; |
| 842 | default: |
| 843 | warning (_("RTTI symbol for class '%s' has bad type"), name); |
| 844 | return NULL; |
| 845 | } |
| 846 | |
| 847 | return rtti_type; |
| 848 | } |
| 849 | |
| 850 | /* Don't allow just "maintenance cplus". */ |
| 851 | |
| 852 | static void |
| 853 | maint_cplus_command (char *arg, int from_tty) |
| 854 | { |
| 855 | printf_unfiltered (_("\"maintenance cplus\" must be followed by the name of a command.\n")); |
| 856 | help_list (maint_cplus_cmd_list, "maintenance cplus ", -1, gdb_stdout); |
| 857 | } |
| 858 | |
| 859 | /* This is a front end for cp_find_first_component, for unit testing. |
| 860 | Be careful when using it: see the NOTE above |
| 861 | cp_find_first_component. */ |
| 862 | |
| 863 | static void |
| 864 | first_component_command (char *arg, int from_tty) |
| 865 | { |
| 866 | int len = cp_find_first_component (arg); |
| 867 | char *prefix = alloca (len + 1); |
| 868 | |
| 869 | memcpy (prefix, arg, len); |
| 870 | prefix[len] = '\0'; |
| 871 | |
| 872 | printf_unfiltered ("%s\n", prefix); |
| 873 | } |
| 874 | |
| 875 | extern initialize_file_ftype _initialize_cp_support; /* -Wmissing-prototypes */ |
| 876 | |
| 877 | void |
| 878 | _initialize_cp_support (void) |
| 879 | { |
| 880 | add_prefix_cmd ("cplus", class_maintenance, maint_cplus_command, |
| 881 | _("C++ maintenance commands."), &maint_cplus_cmd_list, |
| 882 | "maintenance cplus ", 0, &maintenancelist); |
| 883 | add_alias_cmd ("cp", "cplus", class_maintenance, 1, &maintenancelist); |
| 884 | |
| 885 | add_cmd ("first_component", class_maintenance, first_component_command, |
| 886 | _("Print the first class/namespace component of NAME."), |
| 887 | &maint_cplus_cmd_list); |
| 888 | } |