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 if (block) 782 make_symbol_overload_list_block (name, block); 783 784 /* Look in the global block. */ 785 block = block_global_block (block); 786 if (block) 787 make_symbol_overload_list_block (name, block); 788 789 } 790 791 /* Search the namespace of the given type and namespace of and public 792 base types. */ 793 794 static void 795 make_symbol_overload_list_adl_namespace (struct type *type, 796 const char *func_name) 797 { 798 char *namespace; 799 char *type_name; 800 int i, prefix_len; 801 802 while (TYPE_CODE (type) == TYPE_CODE_PTR 803 || TYPE_CODE (type) == TYPE_CODE_REF 804 || TYPE_CODE (type) == TYPE_CODE_ARRAY 805 || TYPE_CODE (type) == TYPE_CODE_TYPEDEF) 806 { 807 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF) 808 type = check_typedef(type); 809 else 810 type = TYPE_TARGET_TYPE (type); 811 } 812 813 type_name = TYPE_NAME (type); 814 815 if (type_name == NULL) 816 return; 817 818 prefix_len = cp_entire_prefix_len (type_name); 819 820 if (prefix_len != 0) 821 { 822 namespace = alloca (prefix_len + 1); 823 strncpy (namespace, type_name, prefix_len); 824 namespace[prefix_len] = '\0'; 825 826 make_symbol_overload_list_namespace (func_name, namespace); 827 } 828 829 /* Check public base type */ 830 if (TYPE_CODE (type) == TYPE_CODE_CLASS) 831 for (i = 0; i < TYPE_N_BASECLASSES (type); i++) 832 { 833 if (BASETYPE_VIA_PUBLIC (type, i)) 834 make_symbol_overload_list_adl_namespace (TYPE_BASECLASS (type, 835 i), 836 func_name); 837 } 838 } 839 840 /* Adds the overload list overload candidates for FUNC_NAME found 841 through argument dependent lookup. */ 842 843 struct symbol ** 844 make_symbol_overload_list_adl (struct type **arg_types, int nargs, 845 const char *func_name) 846 { 847 int i; 848 849 gdb_assert (sym_return_val_size != -1); 850 851 for (i = 1; i <= nargs; i++) 852 make_symbol_overload_list_adl_namespace (arg_types[i - 1], 853 func_name); 854 855 return sym_return_val; 856 } 857 858 /* Used for cleanups to reset the "searched" flag in case of an 859 error. */ 860 861 static void 862 reset_directive_searched (void *data) 863 { 864 struct using_direct *direct = data; 865 direct->searched = 0; 866 } 867 868 /* This applies the using directives to add namespaces to search in, 869 and then searches for overloads in all of those namespaces. It 870 adds the symbols found to sym_return_val. Arguments are as in 871 make_symbol_overload_list. */ 872 873 static void 874 make_symbol_overload_list_using (const char *func_name, 875 const char *namespace) 876 { 877 struct using_direct *current; 878 const struct block *block; 879 880 /* First, go through the using directives. If any of them apply, 881 look in the appropriate namespaces for new functions to match 882 on. */ 883 884 for (block = get_selected_block (0); 885 block != NULL; 886 block = BLOCK_SUPERBLOCK (block)) 887 for (current = block_using (block); 888 current != NULL; 889 current = current->next) 890 { 891 /* Prevent recursive calls. */ 892 if (current->searched) 893 continue; 894 895 /* If this is a namespace alias or imported declaration ignore 896 it. */ 897 if (current->alias != NULL || current->declaration != NULL) 898 continue; 899 900 if (strcmp (namespace, current->import_dest) == 0) 901 { 902 /* Mark this import as searched so that the recursive call 903 does not search it again. */ 904 struct cleanup *old_chain; 905 current->searched = 1; 906 old_chain = make_cleanup (reset_directive_searched, 907 current); 908 909 make_symbol_overload_list_using (func_name, 910 current->import_src); 911 912 current->searched = 0; 913 discard_cleanups (old_chain); 914 } 915 } 916 917 /* Now, add names for this namespace. */ 918 make_symbol_overload_list_namespace (func_name, namespace); 919 } 920 921 /* This does the bulk of the work of finding overloaded symbols. 922 FUNC_NAME is the name of the overloaded function we're looking for 923 (possibly including namespace info). */ 924 925 static void 926 make_symbol_overload_list_qualified (const char *func_name) 927 { 928 struct symbol *sym; 929 struct symtab *s; 930 struct objfile *objfile; 931 const struct block *b, *surrounding_static_block = 0; 932 struct dict_iterator iter; 933 const struct dictionary *dict; 934 935 /* Look through the partial symtabs for all symbols which begin by 936 matching FUNC_NAME. Make sure we read that symbol table in. */ 937 938 ALL_OBJFILES (objfile) 939 { 940 if (objfile->sf) 941 objfile->sf->qf->expand_symtabs_for_function (objfile, func_name); 942 } 943 944 /* Search upwards from currently selected frame (so that we can 945 complete on local vars. */ 946 947 for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b)) 948 make_symbol_overload_list_block (func_name, b); 949 950 surrounding_static_block = block_static_block (get_selected_block (0)); 951 952 /* Go through the symtabs and check the externs and statics for 953 symbols which match. */ 954 955 ALL_PRIMARY_SYMTABS (objfile, s) 956 { 957 QUIT; 958 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK); 959 make_symbol_overload_list_block (func_name, b); 960 } 961 962 ALL_PRIMARY_SYMTABS (objfile, s) 963 { 964 QUIT; 965 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK); 966 /* Don't do this block twice. */ 967 if (b == surrounding_static_block) 968 continue; 969 make_symbol_overload_list_block (func_name, b); 970 } 971 } 972 973 /* Lookup the rtti type for a class name. */ 974 975 struct type * 976 cp_lookup_rtti_type (const char *name, struct block *block) 977 { 978 struct symbol * rtti_sym; 979 struct type * rtti_type; 980 981 rtti_sym = lookup_symbol (name, block, STRUCT_DOMAIN, NULL); 982 983 if (rtti_sym == NULL) 984 { 985 warning (_("RTTI symbol not found for class '%s'"), name); 986 return NULL; 987 } 988 989 if (SYMBOL_CLASS (rtti_sym) != LOC_TYPEDEF) 990 { 991 warning (_("RTTI symbol for class '%s' is not a type"), name); 992 return NULL; 993 } 994 995 rtti_type = SYMBOL_TYPE (rtti_sym); 996 997 switch (TYPE_CODE (rtti_type)) 998 { 999 case TYPE_CODE_CLASS: 1000 break; 1001 case TYPE_CODE_NAMESPACE: 1002 /* chastain/2003-11-26: the symbol tables often contain fake 1003 symbols for namespaces with the same name as the struct. 1004 This warning is an indication of a bug in the lookup order 1005 or a bug in the way that the symbol tables are populated. */ 1006 warning (_("RTTI symbol for class '%s' is a namespace"), name); 1007 return NULL; 1008 default: 1009 warning (_("RTTI symbol for class '%s' has bad type"), name); 1010 return NULL; 1011 } 1012 1013 return rtti_type; 1014 } 1015 1016 /* Don't allow just "maintenance cplus". */ 1017 1018 static void 1019 maint_cplus_command (char *arg, int from_tty) 1020 { 1021 printf_unfiltered (_("\"maintenance cplus\" must be followed " 1022 "by the name of a command.\n")); 1023 help_list (maint_cplus_cmd_list, 1024 "maintenance cplus ", 1025 -1, gdb_stdout); 1026 } 1027 1028 /* This is a front end for cp_find_first_component, for unit testing. 1029 Be careful when using it: see the NOTE above 1030 cp_find_first_component. */ 1031 1032 static void 1033 first_component_command (char *arg, int from_tty) 1034 { 1035 int len; 1036 char *prefix; 1037 1038 if (!arg) 1039 return; 1040 1041 len = cp_find_first_component (arg); 1042 prefix = alloca (len + 1); 1043 1044 memcpy (prefix, arg, len); 1045 prefix[len] = '\0'; 1046 1047 printf_unfiltered ("%s\n", prefix); 1048 } 1049 1050 extern initialize_file_ftype _initialize_cp_support; /* -Wmissing-prototypes */ 1051 1052 #define SKIP_SPACE(P) \ 1053 do \ 1054 { \ 1055 while (*(P) == ' ' || *(P) == '\t') \ 1056 ++(P); \ 1057 } \ 1058 while (0) 1059 1060 /* Returns the length of the operator name or 0 if INPUT does not 1061 point to a valid C++ operator. INPUT should start with 1062 "operator". */ 1063 int 1064 cp_validate_operator (const char *input) 1065 { 1066 int i; 1067 char *copy; 1068 const char *p; 1069 struct expression *expr; 1070 struct value *val; 1071 struct gdb_exception except; 1072 1073 p = input; 1074 1075 if (strncmp (p, "operator", 8) == 0) 1076 { 1077 int valid = 0; 1078 1079 p += 8; 1080 SKIP_SPACE (p); 1081 for (i = 0; 1082 i < sizeof (operator_tokens) / sizeof (operator_tokens[0]); 1083 ++i) 1084 { 1085 int length = strlen (operator_tokens[i]); 1086 1087 /* By using strncmp here, we MUST have operator_tokens 1088 ordered! See additional notes where operator_tokens is 1089 defined above. */ 1090 if (strncmp (p, operator_tokens[i], length) == 0) 1091 { 1092 const char *op = p; 1093 1094 valid = 1; 1095 p += length; 1096 1097 if (strncmp (op, "new", 3) == 0 1098 || strncmp (op, "delete", 6) == 0) 1099 { 1100 1101 /* Special case: new[] and delete[]. We must be 1102 careful to swallow whitespace before/in "[]". */ 1103 SKIP_SPACE (p); 1104 1105 if (*p == '[') 1106 { 1107 ++p; 1108 SKIP_SPACE (p); 1109 if (*p == ']') 1110 ++p; 1111 else 1112 valid = 0; 1113 } 1114 } 1115 1116 if (valid) 1117 return (p - input); 1118 } 1119 } 1120 1121 /* Check input for a conversion operator. */ 1122 1123 /* Skip past base typename. */ 1124 while (*p != '*' && *p != '&' && *p != 0 && *p != ' ') 1125 ++p; 1126 SKIP_SPACE (p); 1127 1128 /* Add modifiers '*' / '&'. */ 1129 while (*p == '*' || *p == '&') 1130 { 1131 ++p; 1132 SKIP_SPACE (p); 1133 } 1134 1135 /* Check for valid type. [Remember: input starts with 1136 "operator".] */ 1137 copy = savestring (input + 8, p - input - 8); 1138 expr = NULL; 1139 val = NULL; 1140 TRY_CATCH (except, RETURN_MASK_ALL) 1141 { 1142 expr = parse_expression (copy); 1143 val = evaluate_type (expr); 1144 } 1145 1146 xfree (copy); 1147 if (expr) 1148 xfree (expr); 1149 1150 if (val != NULL && value_type (val) != NULL) 1151 return (p - input); 1152 } 1153 1154 return 0; 1155 } 1156 1157 void 1158 _initialize_cp_support (void) 1159 { 1160 add_prefix_cmd ("cplus", class_maintenance, 1161 maint_cplus_command, 1162 _("C++ maintenance commands."), 1163 &maint_cplus_cmd_list, 1164 "maintenance cplus ", 1165 0, &maintenancelist); 1166 add_alias_cmd ("cp", "cplus", 1167 class_maintenance, 1, 1168 &maintenancelist); 1169 1170 add_cmd ("first_component", 1171 class_maintenance, 1172 first_component_command, 1173 _("Print the first class/namespace component of NAME."), 1174 &maint_cplus_cmd_list); 1175 } 1176