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