| 1 | /* Helper routines for C++ support in GDB. |
| 2 | Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011 |
| 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 | #include "exceptions.h" |
| 36 | #include "expression.h" |
| 37 | #include "value.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 | /* Operator validation. |
| 77 | NOTE: Multi-byte operators (usually the assignment variety |
| 78 | operator) must appear before the single byte version, i.e., "+=" |
| 79 | before "+". */ |
| 80 | static const char *operator_tokens[] = |
| 81 | { |
| 82 | "++", "+=", "+", "->*", "->", "--", "-=", "-", "*=", "*", |
| 83 | "/=", "/", "%=", "%", "!=", "==", "!", "&&", "<<=", "<<", |
| 84 | ">>=", ">>", "<=", "<", ">=", ">", "~", "&=", "&", "|=", |
| 85 | "||", "|", "^=", "^", "=", "()", "[]", ",", "new", "delete" |
| 86 | /* new[] and delete[] require special whitespace handling */ |
| 87 | }; |
| 88 | |
| 89 | /* Return 1 if STRING is clearly already in canonical form. This |
| 90 | function is conservative; things which it does not recognize are |
| 91 | assumed to be non-canonical, and the parser will sort them out |
| 92 | afterwards. This speeds up the critical path for alphanumeric |
| 93 | identifiers. */ |
| 94 | |
| 95 | static int |
| 96 | cp_already_canonical (const char *string) |
| 97 | { |
| 98 | /* Identifier start character [a-zA-Z_]. */ |
| 99 | if (!ISIDST (string[0])) |
| 100 | return 0; |
| 101 | |
| 102 | /* These are the only two identifiers which canonicalize to other |
| 103 | than themselves or an error: unsigned -> unsigned int and |
| 104 | signed -> int. */ |
| 105 | if (string[0] == 'u' && strcmp (&string[1], "nsigned") == 0) |
| 106 | return 0; |
| 107 | else if (string[0] == 's' && strcmp (&string[1], "igned") == 0) |
| 108 | return 0; |
| 109 | |
| 110 | /* Identifier character [a-zA-Z0-9_]. */ |
| 111 | while (ISIDNUM (string[1])) |
| 112 | string++; |
| 113 | |
| 114 | if (string[1] == '\0') |
| 115 | return 1; |
| 116 | else |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | /* Parse STRING and convert it to canonical form. If parsing fails, |
| 121 | or if STRING is already canonical, return NULL. Otherwise return |
| 122 | the canonical form. The return value is allocated via xmalloc. */ |
| 123 | |
| 124 | char * |
| 125 | cp_canonicalize_string (const char *string) |
| 126 | { |
| 127 | struct demangle_component *ret_comp; |
| 128 | unsigned int estimated_len; |
| 129 | char *ret; |
| 130 | |
| 131 | if (cp_already_canonical (string)) |
| 132 | return NULL; |
| 133 | |
| 134 | ret_comp = cp_demangled_name_to_comp (string, NULL); |
| 135 | if (ret_comp == NULL) |
| 136 | return NULL; |
| 137 | |
| 138 | estimated_len = strlen (string) * 2; |
| 139 | ret = cp_comp_to_string (ret_comp, estimated_len); |
| 140 | |
| 141 | if (strcmp (string, ret) == 0) |
| 142 | { |
| 143 | xfree (ret); |
| 144 | return NULL; |
| 145 | } |
| 146 | |
| 147 | return ret; |
| 148 | } |
| 149 | |
| 150 | /* Convert a mangled name to a demangle_component tree. *MEMORY is |
| 151 | set to the block of used memory that should be freed when finished |
| 152 | with the tree. DEMANGLED_P is set to the char * that should be |
| 153 | freed when finished with the tree, or NULL if none was needed. |
| 154 | OPTIONS will be passed to the demangler. */ |
| 155 | |
| 156 | static struct demangle_component * |
| 157 | mangled_name_to_comp (const char *mangled_name, int options, |
| 158 | void **memory, char **demangled_p) |
| 159 | { |
| 160 | struct demangle_component *ret; |
| 161 | char *demangled_name; |
| 162 | |
| 163 | /* If it looks like a v3 mangled name, then try to go directly |
| 164 | to trees. */ |
| 165 | if (mangled_name[0] == '_' && mangled_name[1] == 'Z') |
| 166 | { |
| 167 | ret = cplus_demangle_v3_components (mangled_name, |
| 168 | options, memory); |
| 169 | if (ret) |
| 170 | { |
| 171 | *demangled_p = NULL; |
| 172 | return ret; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /* If it doesn't, or if that failed, then try to demangle the |
| 177 | name. */ |
| 178 | demangled_name = cplus_demangle (mangled_name, options); |
| 179 | if (demangled_name == NULL) |
| 180 | return NULL; |
| 181 | |
| 182 | /* If we could demangle the name, parse it to build the component |
| 183 | tree. */ |
| 184 | ret = cp_demangled_name_to_comp (demangled_name, NULL); |
| 185 | |
| 186 | if (ret == NULL) |
| 187 | { |
| 188 | xfree (demangled_name); |
| 189 | return NULL; |
| 190 | } |
| 191 | |
| 192 | *demangled_p = demangled_name; |
| 193 | return ret; |
| 194 | } |
| 195 | |
| 196 | /* Return the name of the class containing method PHYSNAME. */ |
| 197 | |
| 198 | char * |
| 199 | cp_class_name_from_physname (const char *physname) |
| 200 | { |
| 201 | void *storage = NULL; |
| 202 | char *demangled_name = NULL, *ret; |
| 203 | struct demangle_component *ret_comp, *prev_comp, *cur_comp; |
| 204 | int done; |
| 205 | |
| 206 | ret_comp = mangled_name_to_comp (physname, DMGL_ANSI, |
| 207 | &storage, &demangled_name); |
| 208 | if (ret_comp == NULL) |
| 209 | return NULL; |
| 210 | |
| 211 | done = 0; |
| 212 | |
| 213 | /* First strip off any qualifiers, if we have a function or |
| 214 | method. */ |
| 215 | while (!done) |
| 216 | switch (ret_comp->type) |
| 217 | { |
| 218 | case DEMANGLE_COMPONENT_CONST: |
| 219 | case DEMANGLE_COMPONENT_RESTRICT: |
| 220 | case DEMANGLE_COMPONENT_VOLATILE: |
| 221 | case DEMANGLE_COMPONENT_CONST_THIS: |
| 222 | case DEMANGLE_COMPONENT_RESTRICT_THIS: |
| 223 | case DEMANGLE_COMPONENT_VOLATILE_THIS: |
| 224 | case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: |
| 225 | ret_comp = d_left (ret_comp); |
| 226 | break; |
| 227 | default: |
| 228 | done = 1; |
| 229 | break; |
| 230 | } |
| 231 | |
| 232 | /* If what we have now is a function, discard the argument list. */ |
| 233 | if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME) |
| 234 | ret_comp = d_left (ret_comp); |
| 235 | |
| 236 | /* If what we have now is a template, strip off the template |
| 237 | arguments. The left subtree may be a qualified name. */ |
| 238 | if (ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE) |
| 239 | ret_comp = d_left (ret_comp); |
| 240 | |
| 241 | /* What we have now should be a name, possibly qualified. |
| 242 | Additional qualifiers could live in the left subtree or the right |
| 243 | subtree. Find the last piece. */ |
| 244 | done = 0; |
| 245 | prev_comp = NULL; |
| 246 | cur_comp = ret_comp; |
| 247 | while (!done) |
| 248 | switch (cur_comp->type) |
| 249 | { |
| 250 | case DEMANGLE_COMPONENT_QUAL_NAME: |
| 251 | case DEMANGLE_COMPONENT_LOCAL_NAME: |
| 252 | prev_comp = cur_comp; |
| 253 | cur_comp = d_right (cur_comp); |
| 254 | break; |
| 255 | case DEMANGLE_COMPONENT_TEMPLATE: |
| 256 | case DEMANGLE_COMPONENT_NAME: |
| 257 | case DEMANGLE_COMPONENT_CTOR: |
| 258 | case DEMANGLE_COMPONENT_DTOR: |
| 259 | case DEMANGLE_COMPONENT_OPERATOR: |
| 260 | case DEMANGLE_COMPONENT_EXTENDED_OPERATOR: |
| 261 | done = 1; |
| 262 | break; |
| 263 | default: |
| 264 | done = 1; |
| 265 | cur_comp = NULL; |
| 266 | break; |
| 267 | } |
| 268 | |
| 269 | ret = NULL; |
| 270 | if (cur_comp != NULL && prev_comp != NULL) |
| 271 | { |
| 272 | /* We want to discard the rightmost child of PREV_COMP. */ |
| 273 | *prev_comp = *d_left (prev_comp); |
| 274 | /* The ten is completely arbitrary; we don't have a good |
| 275 | estimate. */ |
| 276 | ret = cp_comp_to_string (ret_comp, 10); |
| 277 | } |
| 278 | |
| 279 | xfree (storage); |
| 280 | if (demangled_name) |
| 281 | xfree (demangled_name); |
| 282 | return ret; |
| 283 | } |
| 284 | |
| 285 | /* Return the child of COMP which is the basename of a method, |
| 286 | variable, et cetera. All scope qualifiers are discarded, but |
| 287 | template arguments will be included. The component tree may be |
| 288 | modified. */ |
| 289 | |
| 290 | static struct demangle_component * |
| 291 | unqualified_name_from_comp (struct demangle_component *comp) |
| 292 | { |
| 293 | struct demangle_component *ret_comp = comp, *last_template; |
| 294 | int done; |
| 295 | |
| 296 | done = 0; |
| 297 | last_template = NULL; |
| 298 | while (!done) |
| 299 | switch (ret_comp->type) |
| 300 | { |
| 301 | case DEMANGLE_COMPONENT_QUAL_NAME: |
| 302 | case DEMANGLE_COMPONENT_LOCAL_NAME: |
| 303 | ret_comp = d_right (ret_comp); |
| 304 | break; |
| 305 | case DEMANGLE_COMPONENT_TYPED_NAME: |
| 306 | ret_comp = d_left (ret_comp); |
| 307 | break; |
| 308 | case DEMANGLE_COMPONENT_TEMPLATE: |
| 309 | gdb_assert (last_template == NULL); |
| 310 | last_template = ret_comp; |
| 311 | ret_comp = d_left (ret_comp); |
| 312 | break; |
| 313 | case DEMANGLE_COMPONENT_CONST: |
| 314 | case DEMANGLE_COMPONENT_RESTRICT: |
| 315 | case DEMANGLE_COMPONENT_VOLATILE: |
| 316 | case DEMANGLE_COMPONENT_CONST_THIS: |
| 317 | case DEMANGLE_COMPONENT_RESTRICT_THIS: |
| 318 | case DEMANGLE_COMPONENT_VOLATILE_THIS: |
| 319 | case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: |
| 320 | ret_comp = d_left (ret_comp); |
| 321 | break; |
| 322 | case DEMANGLE_COMPONENT_NAME: |
| 323 | case DEMANGLE_COMPONENT_CTOR: |
| 324 | case DEMANGLE_COMPONENT_DTOR: |
| 325 | case DEMANGLE_COMPONENT_OPERATOR: |
| 326 | case DEMANGLE_COMPONENT_EXTENDED_OPERATOR: |
| 327 | done = 1; |
| 328 | break; |
| 329 | default: |
| 330 | return NULL; |
| 331 | break; |
| 332 | } |
| 333 | |
| 334 | if (last_template) |
| 335 | { |
| 336 | d_left (last_template) = ret_comp; |
| 337 | return last_template; |
| 338 | } |
| 339 | |
| 340 | return ret_comp; |
| 341 | } |
| 342 | |
| 343 | /* Return the name of the method whose linkage name is PHYSNAME. */ |
| 344 | |
| 345 | char * |
| 346 | method_name_from_physname (const char *physname) |
| 347 | { |
| 348 | void *storage = NULL; |
| 349 | char *demangled_name = NULL, *ret; |
| 350 | struct demangle_component *ret_comp; |
| 351 | |
| 352 | ret_comp = mangled_name_to_comp (physname, DMGL_ANSI, |
| 353 | &storage, &demangled_name); |
| 354 | if (ret_comp == NULL) |
| 355 | return NULL; |
| 356 | |
| 357 | ret_comp = unqualified_name_from_comp (ret_comp); |
| 358 | |
| 359 | ret = NULL; |
| 360 | if (ret_comp != NULL) |
| 361 | /* The ten is completely arbitrary; we don't have a good |
| 362 | estimate. */ |
| 363 | ret = cp_comp_to_string (ret_comp, 10); |
| 364 | |
| 365 | xfree (storage); |
| 366 | if (demangled_name) |
| 367 | xfree (demangled_name); |
| 368 | return ret; |
| 369 | } |
| 370 | |
| 371 | /* If FULL_NAME is the demangled name of a C++ function (including an |
| 372 | arg list, possibly including namespace/class qualifications), |
| 373 | return a new string containing only the function name (without the |
| 374 | arg list/class qualifications). Otherwise, return NULL. The |
| 375 | caller is responsible for freeing the memory in question. */ |
| 376 | |
| 377 | char * |
| 378 | cp_func_name (const char *full_name) |
| 379 | { |
| 380 | char *ret; |
| 381 | struct demangle_component *ret_comp; |
| 382 | |
| 383 | ret_comp = cp_demangled_name_to_comp (full_name, NULL); |
| 384 | if (!ret_comp) |
| 385 | return NULL; |
| 386 | |
| 387 | ret_comp = unqualified_name_from_comp (ret_comp); |
| 388 | |
| 389 | ret = NULL; |
| 390 | if (ret_comp != NULL) |
| 391 | ret = cp_comp_to_string (ret_comp, 10); |
| 392 | |
| 393 | return ret; |
| 394 | } |
| 395 | |
| 396 | /* DEMANGLED_NAME is the name of a function, including parameters and |
| 397 | (optionally) a return type. Return the name of the function without |
| 398 | parameters or return type, or NULL if we can not parse the name. */ |
| 399 | |
| 400 | char * |
| 401 | cp_remove_params (const char *demangled_name) |
| 402 | { |
| 403 | int done = 0; |
| 404 | struct demangle_component *ret_comp; |
| 405 | char *ret = NULL; |
| 406 | |
| 407 | if (demangled_name == NULL) |
| 408 | return NULL; |
| 409 | |
| 410 | ret_comp = cp_demangled_name_to_comp (demangled_name, NULL); |
| 411 | if (ret_comp == NULL) |
| 412 | return NULL; |
| 413 | |
| 414 | /* First strip off any qualifiers, if we have a function or method. */ |
| 415 | while (!done) |
| 416 | switch (ret_comp->type) |
| 417 | { |
| 418 | case DEMANGLE_COMPONENT_CONST: |
| 419 | case DEMANGLE_COMPONENT_RESTRICT: |
| 420 | case DEMANGLE_COMPONENT_VOLATILE: |
| 421 | case DEMANGLE_COMPONENT_CONST_THIS: |
| 422 | case DEMANGLE_COMPONENT_RESTRICT_THIS: |
| 423 | case DEMANGLE_COMPONENT_VOLATILE_THIS: |
| 424 | case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: |
| 425 | ret_comp = d_left (ret_comp); |
| 426 | break; |
| 427 | default: |
| 428 | done = 1; |
| 429 | break; |
| 430 | } |
| 431 | |
| 432 | /* What we have now should be a function. Return its name. */ |
| 433 | if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME) |
| 434 | ret = cp_comp_to_string (d_left (ret_comp), 10); |
| 435 | |
| 436 | return ret; |
| 437 | } |
| 438 | |
| 439 | /* Here are some random pieces of trivia to keep in mind while trying |
| 440 | to take apart demangled names: |
| 441 | |
| 442 | - Names can contain function arguments or templates, so the process |
| 443 | has to be, to some extent recursive: maybe keep track of your |
| 444 | depth based on encountering <> and (). |
| 445 | |
| 446 | - Parentheses don't just have to happen at the end of a name: they |
| 447 | can occur even if the name in question isn't a function, because |
| 448 | a template argument might be a type that's a function. |
| 449 | |
| 450 | - Conversely, even if you're trying to deal with a function, its |
| 451 | demangled name might not end with ')': it could be a const or |
| 452 | volatile class method, in which case it ends with "const" or |
| 453 | "volatile". |
| 454 | |
| 455 | - Parentheses are also used in anonymous namespaces: a variable |
| 456 | 'foo' in an anonymous namespace gets demangled as "(anonymous |
| 457 | namespace)::foo". |
| 458 | |
| 459 | - And operator names can contain parentheses or angle brackets. */ |
| 460 | |
| 461 | /* FIXME: carlton/2003-03-13: We have several functions here with |
| 462 | overlapping functionality; can we combine them? Also, do they |
| 463 | handle all the above considerations correctly? */ |
| 464 | |
| 465 | |
| 466 | /* This returns the length of first component of NAME, which should be |
| 467 | the demangled name of a C++ variable/function/method/etc. |
| 468 | Specifically, it returns the index of the first colon forming the |
| 469 | boundary of the first component: so, given 'A::foo' or 'A::B::foo' |
| 470 | it returns the 1, and given 'foo', it returns 0. */ |
| 471 | |
| 472 | /* The character in NAME indexed by the return value is guaranteed to |
| 473 | always be either ':' or '\0'. */ |
| 474 | |
| 475 | /* NOTE: carlton/2003-03-13: This function is currently only intended |
| 476 | for internal use: it's probably not entirely safe when called on |
| 477 | user-generated input, because some of the 'index += 2' lines in |
| 478 | cp_find_first_component_aux might go past the end of malformed |
| 479 | input. */ |
| 480 | |
| 481 | unsigned int |
| 482 | cp_find_first_component (const char *name) |
| 483 | { |
| 484 | return cp_find_first_component_aux (name, 0); |
| 485 | } |
| 486 | |
| 487 | /* Helper function for cp_find_first_component. Like that function, |
| 488 | it returns the length of the first component of NAME, but to make |
| 489 | the recursion easier, it also stops if it reaches an unexpected ')' |
| 490 | or '>' if the value of PERMISSIVE is nonzero. */ |
| 491 | |
| 492 | /* Let's optimize away calls to strlen("operator"). */ |
| 493 | |
| 494 | #define LENGTH_OF_OPERATOR 8 |
| 495 | |
| 496 | static unsigned int |
| 497 | cp_find_first_component_aux (const char *name, int permissive) |
| 498 | { |
| 499 | unsigned int index = 0; |
| 500 | /* Operator names can show up in unexpected places. Since these can |
| 501 | contain parentheses or angle brackets, they can screw up the |
| 502 | recursion. But not every string 'operator' is part of an |
| 503 | operater name: e.g. you could have a variable 'cooperator'. So |
| 504 | this variable tells us whether or not we should treat the string |
| 505 | 'operator' as starting an operator. */ |
| 506 | int operator_possible = 1; |
| 507 | |
| 508 | for (;; ++index) |
| 509 | { |
| 510 | switch (name[index]) |
| 511 | { |
| 512 | case '<': |
| 513 | /* Template; eat it up. The calls to cp_first_component |
| 514 | should only return (I hope!) when they reach the '>' |
| 515 | terminating the component or a '::' between two |
| 516 | components. (Hence the '+ 2'.) */ |
| 517 | index += 1; |
| 518 | for (index += cp_find_first_component_aux (name + index, 1); |
| 519 | name[index] != '>'; |
| 520 | index += cp_find_first_component_aux (name + index, 1)) |
| 521 | { |
| 522 | if (name[index] != ':') |
| 523 | { |
| 524 | demangled_name_complaint (name); |
| 525 | return strlen (name); |
| 526 | } |
| 527 | index += 2; |
| 528 | } |
| 529 | operator_possible = 1; |
| 530 | break; |
| 531 | case '(': |
| 532 | /* Similar comment as to '<'. */ |
| 533 | index += 1; |
| 534 | for (index += cp_find_first_component_aux (name + index, 1); |
| 535 | name[index] != ')'; |
| 536 | index += cp_find_first_component_aux (name + index, 1)) |
| 537 | { |
| 538 | if (name[index] != ':') |
| 539 | { |
| 540 | demangled_name_complaint (name); |
| 541 | return strlen (name); |
| 542 | } |
| 543 | index += 2; |
| 544 | } |
| 545 | operator_possible = 1; |
| 546 | break; |
| 547 | case '>': |
| 548 | case ')': |
| 549 | if (permissive) |
| 550 | return index; |
| 551 | else |
| 552 | { |
| 553 | demangled_name_complaint (name); |
| 554 | return strlen (name); |
| 555 | } |
| 556 | case '\0': |
| 557 | case ':': |
| 558 | return index; |
| 559 | case 'o': |
| 560 | /* Operator names can screw up the recursion. */ |
| 561 | if (operator_possible |
| 562 | && strncmp (name + index, "operator", |
| 563 | LENGTH_OF_OPERATOR) == 0) |
| 564 | { |
| 565 | index += LENGTH_OF_OPERATOR; |
| 566 | while (ISSPACE(name[index])) |
| 567 | ++index; |
| 568 | switch (name[index]) |
| 569 | { |
| 570 | /* Skip over one less than the appropriate number of |
| 571 | characters: the for loop will skip over the last |
| 572 | one. */ |
| 573 | case '<': |
| 574 | if (name[index + 1] == '<') |
| 575 | index += 1; |
| 576 | else |
| 577 | index += 0; |
| 578 | break; |
| 579 | case '>': |
| 580 | case '-': |
| 581 | if (name[index + 1] == '>') |
| 582 | index += 1; |
| 583 | else |
| 584 | index += 0; |
| 585 | break; |
| 586 | case '(': |
| 587 | index += 1; |
| 588 | break; |
| 589 | default: |
| 590 | index += 0; |
| 591 | break; |
| 592 | } |
| 593 | } |
| 594 | operator_possible = 0; |
| 595 | break; |
| 596 | case ' ': |
| 597 | case ',': |
| 598 | case '.': |
| 599 | case '&': |
| 600 | case '*': |
| 601 | /* NOTE: carlton/2003-04-18: I'm not sure what the precise |
| 602 | set of relevant characters are here: it's necessary to |
| 603 | include any character that can show up before 'operator' |
| 604 | in a demangled name, and it's safe to include any |
| 605 | character that can't be part of an identifier's name. */ |
| 606 | operator_possible = 1; |
| 607 | break; |
| 608 | default: |
| 609 | operator_possible = 0; |
| 610 | break; |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | /* Complain about a demangled name that we don't know how to parse. |
| 616 | NAME is the demangled name in question. */ |
| 617 | |
| 618 | static void |
| 619 | demangled_name_complaint (const char *name) |
| 620 | { |
| 621 | complaint (&symfile_complaints, |
| 622 | "unexpected demangled name '%s'", name); |
| 623 | } |
| 624 | |
| 625 | /* If NAME is the fully-qualified name of a C++ |
| 626 | function/variable/method/etc., this returns the length of its |
| 627 | entire prefix: all of the namespaces and classes that make up its |
| 628 | name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns |
| 629 | 4, given 'foo', it returns 0. */ |
| 630 | |
| 631 | unsigned int |
| 632 | cp_entire_prefix_len (const char *name) |
| 633 | { |
| 634 | unsigned int current_len = cp_find_first_component (name); |
| 635 | unsigned int previous_len = 0; |
| 636 | |
| 637 | while (name[current_len] != '\0') |
| 638 | { |
| 639 | gdb_assert (name[current_len] == ':'); |
| 640 | previous_len = current_len; |
| 641 | /* Skip the '::'. */ |
| 642 | current_len += 2; |
| 643 | current_len += cp_find_first_component (name + current_len); |
| 644 | } |
| 645 | |
| 646 | return previous_len; |
| 647 | } |
| 648 | |
| 649 | /* Overload resolution functions. */ |
| 650 | |
| 651 | /* Test to see if SYM is a symbol that we haven't seen corresponding |
| 652 | to a function named OLOAD_NAME. If so, add it to the current |
| 653 | completion list. */ |
| 654 | |
| 655 | static void |
| 656 | overload_list_add_symbol (struct symbol *sym, |
| 657 | const char *oload_name) |
| 658 | { |
| 659 | int newsize; |
| 660 | int i; |
| 661 | char *sym_name; |
| 662 | |
| 663 | /* If there is no type information, we can't do anything, so |
| 664 | skip. */ |
| 665 | if (SYMBOL_TYPE (sym) == NULL) |
| 666 | return; |
| 667 | |
| 668 | /* skip any symbols that we've already considered. */ |
| 669 | for (i = 0; i < sym_return_val_index; ++i) |
| 670 | if (strcmp (SYMBOL_LINKAGE_NAME (sym), |
| 671 | SYMBOL_LINKAGE_NAME (sym_return_val[i])) == 0) |
| 672 | return; |
| 673 | |
| 674 | /* Get the demangled name without parameters */ |
| 675 | sym_name = cp_remove_params (SYMBOL_NATURAL_NAME (sym)); |
| 676 | if (!sym_name) |
| 677 | return; |
| 678 | |
| 679 | /* skip symbols that cannot match */ |
| 680 | if (strcmp (sym_name, oload_name) != 0) |
| 681 | { |
| 682 | xfree (sym_name); |
| 683 | return; |
| 684 | } |
| 685 | |
| 686 | xfree (sym_name); |
| 687 | |
| 688 | /* We have a match for an overload instance, so add SYM to the |
| 689 | current list of overload instances */ |
| 690 | if (sym_return_val_index + 3 > sym_return_val_size) |
| 691 | { |
| 692 | newsize = (sym_return_val_size *= 2) * sizeof (struct symbol *); |
| 693 | sym_return_val = (struct symbol **) |
| 694 | xrealloc ((char *) sym_return_val, newsize); |
| 695 | } |
| 696 | sym_return_val[sym_return_val_index++] = sym; |
| 697 | sym_return_val[sym_return_val_index] = NULL; |
| 698 | } |
| 699 | |
| 700 | /* Return a null-terminated list of pointers to function symbols that |
| 701 | are named FUNC_NAME and are visible within NAMESPACE. */ |
| 702 | |
| 703 | struct symbol ** |
| 704 | make_symbol_overload_list (const char *func_name, |
| 705 | const char *namespace) |
| 706 | { |
| 707 | struct cleanup *old_cleanups; |
| 708 | const char *name; |
| 709 | |
| 710 | sym_return_val_size = 100; |
| 711 | sym_return_val_index = 0; |
| 712 | sym_return_val = xmalloc ((sym_return_val_size + 1) * |
| 713 | sizeof (struct symbol *)); |
| 714 | sym_return_val[0] = NULL; |
| 715 | |
| 716 | old_cleanups = make_cleanup (xfree, sym_return_val); |
| 717 | |
| 718 | make_symbol_overload_list_using (func_name, namespace); |
| 719 | |
| 720 | if (namespace[0] == '\0') |
| 721 | name = func_name; |
| 722 | else |
| 723 | { |
| 724 | char *concatenated_name |
| 725 | = alloca (strlen (namespace) + 2 + strlen (func_name) + 1); |
| 726 | strcpy (concatenated_name, namespace); |
| 727 | strcat (concatenated_name, "::"); |
| 728 | strcat (concatenated_name, func_name); |
| 729 | name = concatenated_name; |
| 730 | } |
| 731 | |
| 732 | make_symbol_overload_list_qualified (name); |
| 733 | |
| 734 | discard_cleanups (old_cleanups); |
| 735 | |
| 736 | return sym_return_val; |
| 737 | } |
| 738 | |
| 739 | /* Add all symbols with a name matching NAME in BLOCK to the overload |
| 740 | list. */ |
| 741 | |
| 742 | static void |
| 743 | make_symbol_overload_list_block (const char *name, |
| 744 | const struct block *block) |
| 745 | { |
| 746 | struct dict_iterator iter; |
| 747 | struct symbol *sym; |
| 748 | |
| 749 | const struct dictionary *dict = BLOCK_DICT (block); |
| 750 | |
| 751 | for (sym = dict_iter_name_first (dict, name, &iter); |
| 752 | sym != NULL; |
| 753 | sym = dict_iter_name_next (name, &iter)) |
| 754 | overload_list_add_symbol (sym, name); |
| 755 | } |
| 756 | |
| 757 | /* Adds the function FUNC_NAME from NAMESPACE to the overload set. */ |
| 758 | |
| 759 | static void |
| 760 | make_symbol_overload_list_namespace (const char *func_name, |
| 761 | const char *namespace) |
| 762 | { |
| 763 | const char *name; |
| 764 | const struct block *block = NULL; |
| 765 | |
| 766 | if (namespace[0] == '\0') |
| 767 | name = func_name; |
| 768 | else |
| 769 | { |
| 770 | char *concatenated_name |
| 771 | = alloca (strlen (namespace) + 2 + strlen (func_name) + 1); |
| 772 | |
| 773 | strcpy (concatenated_name, namespace); |
| 774 | strcat (concatenated_name, "::"); |
| 775 | strcat (concatenated_name, func_name); |
| 776 | name = concatenated_name; |
| 777 | } |
| 778 | |
| 779 | /* Look in the static block. */ |
| 780 | block = block_static_block (get_selected_block (0)); |
| 781 | make_symbol_overload_list_block (name, block); |
| 782 | |
| 783 | /* Look in the global block. */ |
| 784 | block = block_global_block (block); |
| 785 | make_symbol_overload_list_block (name, block); |
| 786 | |
| 787 | } |
| 788 | |
| 789 | /* Search the namespace of the given type and namespace of and public |
| 790 | base types. */ |
| 791 | |
| 792 | static void |
| 793 | make_symbol_overload_list_adl_namespace (struct type *type, |
| 794 | const char *func_name) |
| 795 | { |
| 796 | char *namespace; |
| 797 | char *type_name; |
| 798 | int i, prefix_len; |
| 799 | |
| 800 | while (TYPE_CODE (type) == TYPE_CODE_PTR |
| 801 | || TYPE_CODE (type) == TYPE_CODE_REF |
| 802 | || TYPE_CODE (type) == TYPE_CODE_ARRAY |
| 803 | || TYPE_CODE (type) == TYPE_CODE_TYPEDEF) |
| 804 | { |
| 805 | if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF) |
| 806 | type = check_typedef(type); |
| 807 | else |
| 808 | type = TYPE_TARGET_TYPE (type); |
| 809 | } |
| 810 | |
| 811 | type_name = TYPE_NAME (type); |
| 812 | |
| 813 | if (type_name == NULL) |
| 814 | return; |
| 815 | |
| 816 | prefix_len = cp_entire_prefix_len (type_name); |
| 817 | |
| 818 | if (prefix_len != 0) |
| 819 | { |
| 820 | namespace = alloca (prefix_len + 1); |
| 821 | strncpy (namespace, type_name, prefix_len); |
| 822 | namespace[prefix_len] = '\0'; |
| 823 | |
| 824 | make_symbol_overload_list_namespace (func_name, namespace); |
| 825 | } |
| 826 | |
| 827 | /* Check public base type */ |
| 828 | if (TYPE_CODE (type) == TYPE_CODE_CLASS) |
| 829 | for (i = 0; i < TYPE_N_BASECLASSES (type); i++) |
| 830 | { |
| 831 | if (BASETYPE_VIA_PUBLIC (type, i)) |
| 832 | make_symbol_overload_list_adl_namespace (TYPE_BASECLASS (type, |
| 833 | i), |
| 834 | func_name); |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | /* Adds the the overload list overload candidates for FUNC_NAME found |
| 839 | through argument dependent lookup. */ |
| 840 | |
| 841 | struct symbol ** |
| 842 | make_symbol_overload_list_adl (struct type **arg_types, int nargs, |
| 843 | const char *func_name) |
| 844 | { |
| 845 | int i; |
| 846 | |
| 847 | gdb_assert (sym_return_val_size != -1); |
| 848 | |
| 849 | for (i = 1; i <= nargs; i++) |
| 850 | make_symbol_overload_list_adl_namespace (arg_types[i - 1], |
| 851 | func_name); |
| 852 | |
| 853 | return sym_return_val; |
| 854 | } |
| 855 | |
| 856 | /* Used for cleanups to reset the "searched" flag in case of an |
| 857 | error. */ |
| 858 | |
| 859 | static void |
| 860 | reset_directive_searched (void *data) |
| 861 | { |
| 862 | struct using_direct *direct = data; |
| 863 | direct->searched = 0; |
| 864 | } |
| 865 | |
| 866 | /* This applies the using directives to add namespaces to search in, |
| 867 | and then searches for overloads in all of those namespaces. It |
| 868 | adds the symbols found to sym_return_val. Arguments are as in |
| 869 | make_symbol_overload_list. */ |
| 870 | |
| 871 | static void |
| 872 | make_symbol_overload_list_using (const char *func_name, |
| 873 | const char *namespace) |
| 874 | { |
| 875 | struct using_direct *current; |
| 876 | const struct block *block; |
| 877 | |
| 878 | /* First, go through the using directives. If any of them apply, |
| 879 | look in the appropriate namespaces for new functions to match |
| 880 | on. */ |
| 881 | |
| 882 | for (block = get_selected_block (0); |
| 883 | block != NULL; |
| 884 | block = BLOCK_SUPERBLOCK (block)) |
| 885 | for (current = block_using (block); |
| 886 | current != NULL; |
| 887 | current = current->next) |
| 888 | { |
| 889 | /* Prevent recursive calls. */ |
| 890 | if (current->searched) |
| 891 | continue; |
| 892 | |
| 893 | /* If this is a namespace alias or imported declaration ignore |
| 894 | it. */ |
| 895 | if (current->alias != NULL || current->declaration != NULL) |
| 896 | continue; |
| 897 | |
| 898 | if (strcmp (namespace, current->import_dest) == 0) |
| 899 | { |
| 900 | /* Mark this import as searched so that the recursive call |
| 901 | does not search it again. */ |
| 902 | struct cleanup *old_chain; |
| 903 | current->searched = 1; |
| 904 | old_chain = make_cleanup (reset_directive_searched, |
| 905 | current); |
| 906 | |
| 907 | make_symbol_overload_list_using (func_name, |
| 908 | current->import_src); |
| 909 | |
| 910 | current->searched = 0; |
| 911 | discard_cleanups (old_chain); |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | /* Now, add names for this namespace. */ |
| 916 | make_symbol_overload_list_namespace (func_name, namespace); |
| 917 | } |
| 918 | |
| 919 | /* This does the bulk of the work of finding overloaded symbols. |
| 920 | FUNC_NAME is the name of the overloaded function we're looking for |
| 921 | (possibly including namespace info). */ |
| 922 | |
| 923 | static void |
| 924 | make_symbol_overload_list_qualified (const char *func_name) |
| 925 | { |
| 926 | struct symbol *sym; |
| 927 | struct symtab *s; |
| 928 | struct objfile *objfile; |
| 929 | const struct block *b, *surrounding_static_block = 0; |
| 930 | struct dict_iterator iter; |
| 931 | const struct dictionary *dict; |
| 932 | |
| 933 | /* Look through the partial symtabs for all symbols which begin by |
| 934 | matching FUNC_NAME. Make sure we read that symbol table in. */ |
| 935 | |
| 936 | ALL_OBJFILES (objfile) |
| 937 | { |
| 938 | if (objfile->sf) |
| 939 | objfile->sf->qf->expand_symtabs_for_function (objfile, func_name); |
| 940 | } |
| 941 | |
| 942 | /* Search upwards from currently selected frame (so that we can |
| 943 | complete on local vars. */ |
| 944 | |
| 945 | for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b)) |
| 946 | make_symbol_overload_list_block (func_name, b); |
| 947 | |
| 948 | surrounding_static_block = block_static_block (get_selected_block (0)); |
| 949 | |
| 950 | /* Go through the symtabs and check the externs and statics for |
| 951 | symbols which match. */ |
| 952 | |
| 953 | ALL_PRIMARY_SYMTABS (objfile, s) |
| 954 | { |
| 955 | QUIT; |
| 956 | b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK); |
| 957 | make_symbol_overload_list_block (func_name, b); |
| 958 | } |
| 959 | |
| 960 | ALL_PRIMARY_SYMTABS (objfile, s) |
| 961 | { |
| 962 | QUIT; |
| 963 | b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK); |
| 964 | /* Don't do this block twice. */ |
| 965 | if (b == surrounding_static_block) |
| 966 | continue; |
| 967 | make_symbol_overload_list_block (func_name, b); |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | /* Lookup the rtti type for a class name. */ |
| 972 | |
| 973 | struct type * |
| 974 | cp_lookup_rtti_type (const char *name, struct block *block) |
| 975 | { |
| 976 | struct symbol * rtti_sym; |
| 977 | struct type * rtti_type; |
| 978 | |
| 979 | rtti_sym = lookup_symbol (name, block, STRUCT_DOMAIN, NULL); |
| 980 | |
| 981 | if (rtti_sym == NULL) |
| 982 | { |
| 983 | warning (_("RTTI symbol not found for class '%s'"), name); |
| 984 | return NULL; |
| 985 | } |
| 986 | |
| 987 | if (SYMBOL_CLASS (rtti_sym) != LOC_TYPEDEF) |
| 988 | { |
| 989 | warning (_("RTTI symbol for class '%s' is not a type"), name); |
| 990 | return NULL; |
| 991 | } |
| 992 | |
| 993 | rtti_type = SYMBOL_TYPE (rtti_sym); |
| 994 | |
| 995 | switch (TYPE_CODE (rtti_type)) |
| 996 | { |
| 997 | case TYPE_CODE_CLASS: |
| 998 | break; |
| 999 | case TYPE_CODE_NAMESPACE: |
| 1000 | /* chastain/2003-11-26: the symbol tables often contain fake |
| 1001 | symbols for namespaces with the same name as the struct. |
| 1002 | This warning is an indication of a bug in the lookup order |
| 1003 | or a bug in the way that the symbol tables are populated. */ |
| 1004 | warning (_("RTTI symbol for class '%s' is a namespace"), name); |
| 1005 | return NULL; |
| 1006 | default: |
| 1007 | warning (_("RTTI symbol for class '%s' has bad type"), name); |
| 1008 | return NULL; |
| 1009 | } |
| 1010 | |
| 1011 | return rtti_type; |
| 1012 | } |
| 1013 | |
| 1014 | /* Don't allow just "maintenance cplus". */ |
| 1015 | |
| 1016 | static void |
| 1017 | maint_cplus_command (char *arg, int from_tty) |
| 1018 | { |
| 1019 | printf_unfiltered (_("\"maintenance cplus\" must be followed " |
| 1020 | "by the name of a command.\n")); |
| 1021 | help_list (maint_cplus_cmd_list, |
| 1022 | "maintenance cplus ", |
| 1023 | -1, gdb_stdout); |
| 1024 | } |
| 1025 | |
| 1026 | /* This is a front end for cp_find_first_component, for unit testing. |
| 1027 | Be careful when using it: see the NOTE above |
| 1028 | cp_find_first_component. */ |
| 1029 | |
| 1030 | static void |
| 1031 | first_component_command (char *arg, int from_tty) |
| 1032 | { |
| 1033 | int len; |
| 1034 | char *prefix; |
| 1035 | |
| 1036 | if (!arg) |
| 1037 | return; |
| 1038 | |
| 1039 | len = cp_find_first_component (arg); |
| 1040 | prefix = alloca (len + 1); |
| 1041 | |
| 1042 | memcpy (prefix, arg, len); |
| 1043 | prefix[len] = '\0'; |
| 1044 | |
| 1045 | printf_unfiltered ("%s\n", prefix); |
| 1046 | } |
| 1047 | |
| 1048 | extern initialize_file_ftype _initialize_cp_support; /* -Wmissing-prototypes */ |
| 1049 | |
| 1050 | #define SKIP_SPACE(P) \ |
| 1051 | do \ |
| 1052 | { \ |
| 1053 | while (*(P) == ' ' || *(P) == '\t') \ |
| 1054 | ++(P); \ |
| 1055 | } \ |
| 1056 | while (0) |
| 1057 | |
| 1058 | /* Returns the length of the operator name or 0 if INPUT does not |
| 1059 | point to a valid C++ operator. INPUT should start with |
| 1060 | "operator". */ |
| 1061 | int |
| 1062 | cp_validate_operator (const char *input) |
| 1063 | { |
| 1064 | int i; |
| 1065 | char *copy; |
| 1066 | const char *p; |
| 1067 | struct expression *expr; |
| 1068 | struct value *val; |
| 1069 | struct gdb_exception except; |
| 1070 | |
| 1071 | p = input; |
| 1072 | |
| 1073 | if (strncmp (p, "operator", 8) == 0) |
| 1074 | { |
| 1075 | int valid = 0; |
| 1076 | |
| 1077 | p += 8; |
| 1078 | SKIP_SPACE (p); |
| 1079 | for (i = 0; |
| 1080 | i < sizeof (operator_tokens) / sizeof (operator_tokens[0]); |
| 1081 | ++i) |
| 1082 | { |
| 1083 | int length = strlen (operator_tokens[i]); |
| 1084 | |
| 1085 | /* By using strncmp here, we MUST have operator_tokens |
| 1086 | ordered! See additional notes where operator_tokens is |
| 1087 | defined above. */ |
| 1088 | if (strncmp (p, operator_tokens[i], length) == 0) |
| 1089 | { |
| 1090 | const char *op = p; |
| 1091 | |
| 1092 | valid = 1; |
| 1093 | p += length; |
| 1094 | |
| 1095 | if (strncmp (op, "new", 3) == 0 |
| 1096 | || strncmp (op, "delete", 6) == 0) |
| 1097 | { |
| 1098 | |
| 1099 | /* Special case: new[] and delete[]. We must be |
| 1100 | careful to swallow whitespace before/in "[]". */ |
| 1101 | SKIP_SPACE (p); |
| 1102 | |
| 1103 | if (*p == '[') |
| 1104 | { |
| 1105 | ++p; |
| 1106 | SKIP_SPACE (p); |
| 1107 | if (*p == ']') |
| 1108 | ++p; |
| 1109 | else |
| 1110 | valid = 0; |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | if (valid) |
| 1115 | return (p - input); |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | /* Check input for a conversion operator. */ |
| 1120 | |
| 1121 | /* Skip past base typename. */ |
| 1122 | while (*p != '*' && *p != '&' && *p != 0 && *p != ' ') |
| 1123 | ++p; |
| 1124 | SKIP_SPACE (p); |
| 1125 | |
| 1126 | /* Add modifiers '*' / '&'. */ |
| 1127 | while (*p == '*' || *p == '&') |
| 1128 | { |
| 1129 | ++p; |
| 1130 | SKIP_SPACE (p); |
| 1131 | } |
| 1132 | |
| 1133 | /* Check for valid type. [Remember: input starts with |
| 1134 | "operator".] */ |
| 1135 | copy = savestring (input + 8, p - input - 8); |
| 1136 | expr = NULL; |
| 1137 | val = NULL; |
| 1138 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 1139 | { |
| 1140 | expr = parse_expression (copy); |
| 1141 | val = evaluate_type (expr); |
| 1142 | } |
| 1143 | |
| 1144 | xfree (copy); |
| 1145 | if (expr) |
| 1146 | xfree (expr); |
| 1147 | |
| 1148 | if (val != NULL && value_type (val) != NULL) |
| 1149 | return (p - input); |
| 1150 | } |
| 1151 | |
| 1152 | return 0; |
| 1153 | } |
| 1154 | |
| 1155 | void |
| 1156 | _initialize_cp_support (void) |
| 1157 | { |
| 1158 | add_prefix_cmd ("cplus", class_maintenance, |
| 1159 | maint_cplus_command, |
| 1160 | _("C++ maintenance commands."), |
| 1161 | &maint_cplus_cmd_list, |
| 1162 | "maintenance cplus ", |
| 1163 | 0, &maintenancelist); |
| 1164 | add_alias_cmd ("cp", "cplus", |
| 1165 | class_maintenance, 1, |
| 1166 | &maintenancelist); |
| 1167 | |
| 1168 | add_cmd ("first_component", |
| 1169 | class_maintenance, |
| 1170 | first_component_command, |
| 1171 | _("Print the first class/namespace component of NAME."), |
| 1172 | &maint_cplus_cmd_list); |
| 1173 | } |