1130803Smarcel/* Helper routines for C++ support in GDB. 2130803Smarcel Copyright 2002, 2003 Free Software Foundation, Inc. 3130803Smarcel 4130803Smarcel Contributed by MontaVista Software. 5130803Smarcel 6130803Smarcel This file is part of GDB. 7130803Smarcel 8130803Smarcel This program is free software; you can redistribute it and/or modify 9130803Smarcel it under the terms of the GNU General Public License as published by 10130803Smarcel the Free Software Foundation; either version 2 of the License, or 11130803Smarcel (at your option) any later version. 12130803Smarcel 13130803Smarcel This program is distributed in the hope that it will be useful, 14130803Smarcel but WITHOUT ANY WARRANTY; without even the implied warranty of 15130803Smarcel MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16130803Smarcel GNU General Public License for more details. 17130803Smarcel 18130803Smarcel You should have received a copy of the GNU General Public License 19130803Smarcel along with this program; if not, write to the Free Software 20130803Smarcel Foundation, Inc., 59 Temple Place - Suite 330, 21130803Smarcel Boston, MA 02111-1307, USA. */ 22130803Smarcel 23130803Smarcel#include "defs.h" 24130803Smarcel#include <ctype.h> 25130803Smarcel#include "cp-support.h" 26130803Smarcel#include "gdb_string.h" 27130803Smarcel#include "demangle.h" 28130803Smarcel#include "gdb_assert.h" 29130803Smarcel#include "gdbcmd.h" 30130803Smarcel#include "dictionary.h" 31130803Smarcel#include "objfiles.h" 32130803Smarcel#include "frame.h" 33130803Smarcel#include "symtab.h" 34130803Smarcel#include "block.h" 35130803Smarcel#include "complaints.h" 36130803Smarcel#include "gdbtypes.h" 37130803Smarcel 38130803Smarcel/* Functions related to demangled name parsing. */ 39130803Smarcel 40130803Smarcelstatic const char *find_last_component (const char *name); 41130803Smarcel 42130803Smarcelstatic unsigned int cp_find_first_component_aux (const char *name, 43130803Smarcel int permissive); 44130803Smarcel 45130803Smarcelstatic void demangled_name_complaint (const char *name); 46130803Smarcel 47130803Smarcel/* Functions/variables related to overload resolution. */ 48130803Smarcel 49130803Smarcelstatic int sym_return_val_size; 50130803Smarcelstatic int sym_return_val_index; 51130803Smarcelstatic struct symbol **sym_return_val; 52130803Smarcel 53130803Smarcelstatic char *remove_params (const char *demangled_name); 54130803Smarcel 55130803Smarcelstatic void overload_list_add_symbol (struct symbol *sym, 56130803Smarcel const char *oload_name); 57130803Smarcel 58130803Smarcelstatic void make_symbol_overload_list_using (const char *func_name, 59130803Smarcel const char *namespace); 60130803Smarcel 61130803Smarcelstatic void make_symbol_overload_list_qualified (const char *func_name); 62130803Smarcel 63130803Smarcelstatic void read_in_psymtabs (const char *oload_name); 64130803Smarcel 65130803Smarcel/* The list of "maint cplus" commands. */ 66130803Smarcel 67130803Smarcelstruct cmd_list_element *maint_cplus_cmd_list = NULL; 68130803Smarcel 69130803Smarcel/* The actual commands. */ 70130803Smarcel 71130803Smarcelstatic void maint_cplus_command (char *arg, int from_tty); 72130803Smarcelstatic void first_component_command (char *arg, int from_tty); 73130803Smarcel 74130803Smarcel/* Here are some random pieces of trivia to keep in mind while trying 75130803Smarcel to take apart demangled names: 76130803Smarcel 77130803Smarcel - Names can contain function arguments or templates, so the process 78130803Smarcel has to be, to some extent recursive: maybe keep track of your 79130803Smarcel depth based on encountering <> and (). 80130803Smarcel 81130803Smarcel - Parentheses don't just have to happen at the end of a name: they 82130803Smarcel can occur even if the name in question isn't a function, because 83130803Smarcel a template argument might be a type that's a function. 84130803Smarcel 85130803Smarcel - Conversely, even if you're trying to deal with a function, its 86130803Smarcel demangled name might not end with ')': it could be a const or 87130803Smarcel volatile class method, in which case it ends with "const" or 88130803Smarcel "volatile". 89130803Smarcel 90130803Smarcel - Parentheses are also used in anonymous namespaces: a variable 91130803Smarcel 'foo' in an anonymous namespace gets demangled as "(anonymous 92130803Smarcel namespace)::foo". 93130803Smarcel 94130803Smarcel - And operator names can contain parentheses or angle brackets. */ 95130803Smarcel 96130803Smarcel/* FIXME: carlton/2003-03-13: We have several functions here with 97130803Smarcel overlapping functionality; can we combine them? Also, do they 98130803Smarcel handle all the above considerations correctly? */ 99130803Smarcel 100130803Smarcel/* Find the last component of the demangled C++ name NAME. NAME 101130803Smarcel must be a method name including arguments, in order to correctly 102130803Smarcel locate the last component. 103130803Smarcel 104130803Smarcel This function return a pointer to the first colon before the 105130803Smarcel last component, or NULL if the name had only one component. */ 106130803Smarcel 107130803Smarcelstatic const char * 108130803Smarcelfind_last_component (const char *name) 109130803Smarcel{ 110130803Smarcel const char *p; 111130803Smarcel int depth; 112130803Smarcel 113130803Smarcel /* Functions can have local classes, so we need to find the 114130803Smarcel beginning of the last argument list, not the end of the first 115130803Smarcel one. */ 116130803Smarcel p = name + strlen (name) - 1; 117130803Smarcel while (p > name && *p != ')') 118130803Smarcel p--; 119130803Smarcel 120130803Smarcel if (p == name) 121130803Smarcel return NULL; 122130803Smarcel 123130803Smarcel /* P now points at the `)' at the end of the argument list. Walk 124130803Smarcel back to the beginning. */ 125130803Smarcel p--; 126130803Smarcel depth = 1; 127130803Smarcel while (p > name && depth > 0) 128130803Smarcel { 129130803Smarcel if (*p == '<' || *p == '(') 130130803Smarcel depth--; 131130803Smarcel else if (*p == '>' || *p == ')') 132130803Smarcel depth++; 133130803Smarcel p--; 134130803Smarcel } 135130803Smarcel 136130803Smarcel if (p == name) 137130803Smarcel return NULL; 138130803Smarcel 139130803Smarcel while (p > name && *p != ':') 140130803Smarcel p--; 141130803Smarcel 142130803Smarcel if (p == name || p == name + 1 || p[-1] != ':') 143130803Smarcel return NULL; 144130803Smarcel 145130803Smarcel return p - 1; 146130803Smarcel} 147130803Smarcel 148130803Smarcel/* Return the name of the class containing method PHYSNAME. */ 149130803Smarcel 150130803Smarcelchar * 151130803Smarcelclass_name_from_physname (const char *physname) 152130803Smarcel{ 153130803Smarcel char *ret = NULL; 154130803Smarcel const char *end; 155130803Smarcel int depth = 0; 156130803Smarcel char *demangled_name = cplus_demangle (physname, DMGL_ANSI | DMGL_PARAMS); 157130803Smarcel 158130803Smarcel if (demangled_name == NULL) 159130803Smarcel return NULL; 160130803Smarcel 161130803Smarcel end = find_last_component (demangled_name); 162130803Smarcel if (end != NULL) 163130803Smarcel { 164130803Smarcel ret = xmalloc (end - demangled_name + 1); 165130803Smarcel memcpy (ret, demangled_name, end - demangled_name); 166130803Smarcel ret[end - demangled_name] = '\0'; 167130803Smarcel } 168130803Smarcel 169130803Smarcel xfree (demangled_name); 170130803Smarcel return ret; 171130803Smarcel} 172130803Smarcel 173130803Smarcel/* Return the name of the method whose linkage name is PHYSNAME. */ 174130803Smarcel 175130803Smarcelchar * 176130803Smarcelmethod_name_from_physname (const char *physname) 177130803Smarcel{ 178130803Smarcel char *ret = NULL; 179130803Smarcel const char *end; 180130803Smarcel int depth = 0; 181130803Smarcel char *demangled_name = cplus_demangle (physname, DMGL_ANSI | DMGL_PARAMS); 182130803Smarcel 183130803Smarcel if (demangled_name == NULL) 184130803Smarcel return NULL; 185130803Smarcel 186130803Smarcel end = find_last_component (demangled_name); 187130803Smarcel if (end != NULL) 188130803Smarcel { 189130803Smarcel char *args; 190130803Smarcel int len; 191130803Smarcel 192130803Smarcel /* Skip "::". */ 193130803Smarcel end = end + 2; 194130803Smarcel 195130803Smarcel /* Find the argument list, if any. */ 196130803Smarcel args = strchr (end, '('); 197130803Smarcel if (args == NULL) 198130803Smarcel len = strlen (end + 2); 199130803Smarcel else 200130803Smarcel { 201130803Smarcel args --; 202130803Smarcel while (*args == ' ') 203130803Smarcel args --; 204130803Smarcel len = args - end + 1; 205130803Smarcel } 206130803Smarcel ret = xmalloc (len + 1); 207130803Smarcel memcpy (ret, end, len); 208130803Smarcel ret[len] = 0; 209130803Smarcel } 210130803Smarcel 211130803Smarcel xfree (demangled_name); 212130803Smarcel return ret; 213130803Smarcel} 214130803Smarcel 215130803Smarcel/* This returns the length of first component of NAME, which should be 216130803Smarcel the demangled name of a C++ variable/function/method/etc. 217130803Smarcel Specifically, it returns the index of the first colon forming the 218130803Smarcel boundary of the first component: so, given 'A::foo' or 'A::B::foo' 219130803Smarcel it returns the 1, and given 'foo', it returns 0. */ 220130803Smarcel 221130803Smarcel/* The character in NAME indexed by the return value is guaranteed to 222130803Smarcel always be either ':' or '\0'. */ 223130803Smarcel 224130803Smarcel/* NOTE: carlton/2003-03-13: This function is currently only intended 225130803Smarcel for internal use: it's probably not entirely safe when called on 226130803Smarcel user-generated input, because some of the 'index += 2' lines in 227130803Smarcel cp_find_first_component_aux might go past the end of malformed 228130803Smarcel input. */ 229130803Smarcel 230130803Smarcelunsigned int 231130803Smarcelcp_find_first_component (const char *name) 232130803Smarcel{ 233130803Smarcel return cp_find_first_component_aux (name, 0); 234130803Smarcel} 235130803Smarcel 236130803Smarcel/* Helper function for cp_find_first_component. Like that function, 237130803Smarcel it returns the length of the first component of NAME, but to make 238130803Smarcel the recursion easier, it also stops if it reaches an unexpected ')' 239130803Smarcel or '>' if the value of PERMISSIVE is nonzero. */ 240130803Smarcel 241130803Smarcel/* Let's optimize away calls to strlen("operator"). */ 242130803Smarcel 243130803Smarcel#define LENGTH_OF_OPERATOR 8 244130803Smarcel 245130803Smarcelstatic unsigned int 246130803Smarcelcp_find_first_component_aux (const char *name, int permissive) 247130803Smarcel{ 248130803Smarcel unsigned int index = 0; 249130803Smarcel /* Operator names can show up in unexpected places. Since these can 250130803Smarcel contain parentheses or angle brackets, they can screw up the 251130803Smarcel recursion. But not every string 'operator' is part of an 252130803Smarcel operater name: e.g. you could have a variable 'cooperator'. So 253130803Smarcel this variable tells us whether or not we should treat the string 254130803Smarcel 'operator' as starting an operator. */ 255130803Smarcel int operator_possible = 1; 256130803Smarcel 257130803Smarcel for (;; ++index) 258130803Smarcel { 259130803Smarcel switch (name[index]) 260130803Smarcel { 261130803Smarcel case '<': 262130803Smarcel /* Template; eat it up. The calls to cp_first_component 263130803Smarcel should only return (I hope!) when they reach the '>' 264130803Smarcel terminating the component or a '::' between two 265130803Smarcel components. (Hence the '+ 2'.) */ 266130803Smarcel index += 1; 267130803Smarcel for (index += cp_find_first_component_aux (name + index, 1); 268130803Smarcel name[index] != '>'; 269130803Smarcel index += cp_find_first_component_aux (name + index, 1)) 270130803Smarcel { 271130803Smarcel if (name[index] != ':') 272130803Smarcel { 273130803Smarcel demangled_name_complaint (name); 274130803Smarcel return strlen (name); 275130803Smarcel } 276130803Smarcel index += 2; 277130803Smarcel } 278130803Smarcel operator_possible = 1; 279130803Smarcel break; 280130803Smarcel case '(': 281130803Smarcel /* Similar comment as to '<'. */ 282130803Smarcel index += 1; 283130803Smarcel for (index += cp_find_first_component_aux (name + index, 1); 284130803Smarcel name[index] != ')'; 285130803Smarcel index += cp_find_first_component_aux (name + index, 1)) 286130803Smarcel { 287130803Smarcel if (name[index] != ':') 288130803Smarcel { 289130803Smarcel demangled_name_complaint (name); 290130803Smarcel return strlen (name); 291130803Smarcel } 292130803Smarcel index += 2; 293130803Smarcel } 294130803Smarcel operator_possible = 1; 295130803Smarcel break; 296130803Smarcel case '>': 297130803Smarcel case ')': 298130803Smarcel if (permissive) 299130803Smarcel return index; 300130803Smarcel else 301130803Smarcel { 302130803Smarcel demangled_name_complaint (name); 303130803Smarcel return strlen (name); 304130803Smarcel } 305130803Smarcel case '\0': 306130803Smarcel case ':': 307130803Smarcel return index; 308130803Smarcel case 'o': 309130803Smarcel /* Operator names can screw up the recursion. */ 310130803Smarcel if (operator_possible 311130803Smarcel && strncmp (name + index, "operator", LENGTH_OF_OPERATOR) == 0) 312130803Smarcel { 313130803Smarcel index += LENGTH_OF_OPERATOR; 314130803Smarcel while (isspace(name[index])) 315130803Smarcel ++index; 316130803Smarcel switch (name[index]) 317130803Smarcel { 318130803Smarcel /* Skip over one less than the appropriate number of 319130803Smarcel characters: the for loop will skip over the last 320130803Smarcel one. */ 321130803Smarcel case '<': 322130803Smarcel if (name[index + 1] == '<') 323130803Smarcel index += 1; 324130803Smarcel else 325130803Smarcel index += 0; 326130803Smarcel break; 327130803Smarcel case '>': 328130803Smarcel case '-': 329130803Smarcel if (name[index + 1] == '>') 330130803Smarcel index += 1; 331130803Smarcel else 332130803Smarcel index += 0; 333130803Smarcel break; 334130803Smarcel case '(': 335130803Smarcel index += 1; 336130803Smarcel break; 337130803Smarcel default: 338130803Smarcel index += 0; 339130803Smarcel break; 340130803Smarcel } 341130803Smarcel } 342130803Smarcel operator_possible = 0; 343130803Smarcel break; 344130803Smarcel case ' ': 345130803Smarcel case ',': 346130803Smarcel case '.': 347130803Smarcel case '&': 348130803Smarcel case '*': 349130803Smarcel /* NOTE: carlton/2003-04-18: I'm not sure what the precise 350130803Smarcel set of relevant characters are here: it's necessary to 351130803Smarcel include any character that can show up before 'operator' 352130803Smarcel in a demangled name, and it's safe to include any 353130803Smarcel character that can't be part of an identifier's name. */ 354130803Smarcel operator_possible = 1; 355130803Smarcel break; 356130803Smarcel default: 357130803Smarcel operator_possible = 0; 358130803Smarcel break; 359130803Smarcel } 360130803Smarcel } 361130803Smarcel} 362130803Smarcel 363130803Smarcel/* Complain about a demangled name that we don't know how to parse. 364130803Smarcel NAME is the demangled name in question. */ 365130803Smarcel 366130803Smarcelstatic void 367130803Smarceldemangled_name_complaint (const char *name) 368130803Smarcel{ 369130803Smarcel complaint (&symfile_complaints, 370130803Smarcel "unexpected demangled name '%s'", name); 371130803Smarcel} 372130803Smarcel 373130803Smarcel/* If NAME is the fully-qualified name of a C++ 374130803Smarcel function/variable/method/etc., this returns the length of its 375130803Smarcel entire prefix: all of the namespaces and classes that make up its 376130803Smarcel name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns 377130803Smarcel 4, given 'foo', it returns 0. */ 378130803Smarcel 379130803Smarcelunsigned int 380130803Smarcelcp_entire_prefix_len (const char *name) 381130803Smarcel{ 382130803Smarcel unsigned int current_len = cp_find_first_component (name); 383130803Smarcel unsigned int previous_len = 0; 384130803Smarcel 385130803Smarcel while (name[current_len] != '\0') 386130803Smarcel { 387130803Smarcel gdb_assert (name[current_len] == ':'); 388130803Smarcel previous_len = current_len; 389130803Smarcel /* Skip the '::'. */ 390130803Smarcel current_len += 2; 391130803Smarcel current_len += cp_find_first_component (name + current_len); 392130803Smarcel } 393130803Smarcel 394130803Smarcel return previous_len; 395130803Smarcel} 396130803Smarcel 397130803Smarcel/* If FULL_NAME is the demangled name of a C++ function (including an 398130803Smarcel arg list, possibly including namespace/class qualifications), 399130803Smarcel return a new string containing only the function name (without the 400130803Smarcel arg list/class qualifications). Otherwise, return NULL. The 401130803Smarcel caller is responsible for freeing the memory in question. */ 402130803Smarcel 403130803Smarcelchar * 404130803Smarcelcp_func_name (const char *full_name) 405130803Smarcel{ 406130803Smarcel const char *previous_component = full_name; 407130803Smarcel const char *next_component; 408130803Smarcel 409130803Smarcel if (!full_name) 410130803Smarcel return NULL; 411130803Smarcel 412130803Smarcel for (next_component = (previous_component 413130803Smarcel + cp_find_first_component (previous_component)); 414130803Smarcel *next_component == ':'; 415130803Smarcel next_component = (previous_component 416130803Smarcel + cp_find_first_component (previous_component))) 417130803Smarcel { 418130803Smarcel /* Skip '::'. */ 419130803Smarcel previous_component = next_component + 2; 420130803Smarcel } 421130803Smarcel 422130803Smarcel return remove_params (previous_component); 423130803Smarcel} 424130803Smarcel 425130803Smarcel/* Overload resolution functions. */ 426130803Smarcel 427130803Smarcelstatic char * 428130803Smarcelremove_params (const char *demangled_name) 429130803Smarcel{ 430130803Smarcel const char *argp; 431130803Smarcel char *new_name; 432130803Smarcel int depth; 433130803Smarcel 434130803Smarcel if (demangled_name == NULL) 435130803Smarcel return NULL; 436130803Smarcel 437130803Smarcel /* First find the end of the arg list. */ 438130803Smarcel argp = strrchr (demangled_name, ')'); 439130803Smarcel if (argp == NULL) 440130803Smarcel return NULL; 441130803Smarcel 442130803Smarcel /* Back up to the beginning. */ 443130803Smarcel depth = 1; 444130803Smarcel 445130803Smarcel while (argp-- > demangled_name) 446130803Smarcel { 447130803Smarcel if (*argp == ')') 448130803Smarcel depth ++; 449130803Smarcel else if (*argp == '(') 450130803Smarcel { 451130803Smarcel depth --; 452130803Smarcel 453130803Smarcel if (depth == 0) 454130803Smarcel break; 455130803Smarcel } 456130803Smarcel } 457130803Smarcel if (depth != 0) 458130803Smarcel internal_error (__FILE__, __LINE__, 459130803Smarcel "bad demangled name %s\n", demangled_name); 460130803Smarcel while (argp[-1] == ' ' && argp > demangled_name) 461130803Smarcel argp --; 462130803Smarcel 463130803Smarcel new_name = xmalloc (argp - demangled_name + 1); 464130803Smarcel memcpy (new_name, demangled_name, argp - demangled_name); 465130803Smarcel new_name[argp - demangled_name] = '\0'; 466130803Smarcel return new_name; 467130803Smarcel} 468130803Smarcel 469130803Smarcel/* Test to see if SYM is a symbol that we haven't seen corresponding 470130803Smarcel to a function named OLOAD_NAME. If so, add it to the current 471130803Smarcel completion list. */ 472130803Smarcel 473130803Smarcelstatic void 474130803Smarceloverload_list_add_symbol (struct symbol *sym, const char *oload_name) 475130803Smarcel{ 476130803Smarcel int newsize; 477130803Smarcel int i; 478130803Smarcel char *sym_name; 479130803Smarcel 480130803Smarcel /* If there is no type information, we can't do anything, so skip */ 481130803Smarcel if (SYMBOL_TYPE (sym) == NULL) 482130803Smarcel return; 483130803Smarcel 484130803Smarcel /* skip any symbols that we've already considered. */ 485130803Smarcel for (i = 0; i < sym_return_val_index; ++i) 486130803Smarcel if (strcmp (SYMBOL_LINKAGE_NAME (sym), 487130803Smarcel SYMBOL_LINKAGE_NAME (sym_return_val[i])) == 0) 488130803Smarcel return; 489130803Smarcel 490130803Smarcel /* Get the demangled name without parameters */ 491130803Smarcel sym_name = remove_params (SYMBOL_NATURAL_NAME (sym)); 492130803Smarcel if (!sym_name) 493130803Smarcel return; 494130803Smarcel 495130803Smarcel /* skip symbols that cannot match */ 496130803Smarcel if (strcmp (sym_name, oload_name) != 0) 497130803Smarcel { 498130803Smarcel xfree (sym_name); 499130803Smarcel return; 500130803Smarcel } 501130803Smarcel 502130803Smarcel xfree (sym_name); 503130803Smarcel 504130803Smarcel /* We have a match for an overload instance, so add SYM to the current list 505130803Smarcel * of overload instances */ 506130803Smarcel if (sym_return_val_index + 3 > sym_return_val_size) 507130803Smarcel { 508130803Smarcel newsize = (sym_return_val_size *= 2) * sizeof (struct symbol *); 509130803Smarcel sym_return_val = (struct symbol **) xrealloc ((char *) sym_return_val, newsize); 510130803Smarcel } 511130803Smarcel sym_return_val[sym_return_val_index++] = sym; 512130803Smarcel sym_return_val[sym_return_val_index] = NULL; 513130803Smarcel} 514130803Smarcel 515130803Smarcel/* Return a null-terminated list of pointers to function symbols that 516130803Smarcel are named FUNC_NAME and are visible within NAMESPACE. */ 517130803Smarcel 518130803Smarcelstruct symbol ** 519130803Smarcelmake_symbol_overload_list (const char *func_name, 520130803Smarcel const char *namespace) 521130803Smarcel{ 522130803Smarcel struct cleanup *old_cleanups; 523130803Smarcel 524130803Smarcel sym_return_val_size = 100; 525130803Smarcel sym_return_val_index = 0; 526130803Smarcel sym_return_val = xmalloc ((sym_return_val_size + 1) * 527130803Smarcel sizeof (struct symbol *)); 528130803Smarcel sym_return_val[0] = NULL; 529130803Smarcel 530130803Smarcel old_cleanups = make_cleanup (xfree, sym_return_val); 531130803Smarcel 532130803Smarcel make_symbol_overload_list_using (func_name, namespace); 533130803Smarcel 534130803Smarcel discard_cleanups (old_cleanups); 535130803Smarcel 536130803Smarcel return sym_return_val; 537130803Smarcel} 538130803Smarcel 539130803Smarcel/* This applies the using directives to add namespaces to search in, 540130803Smarcel and then searches for overloads in all of those namespaces. It 541130803Smarcel adds the symbols found to sym_return_val. Arguments are as in 542130803Smarcel make_symbol_overload_list. */ 543130803Smarcel 544130803Smarcelstatic void 545130803Smarcelmake_symbol_overload_list_using (const char *func_name, 546130803Smarcel const char *namespace) 547130803Smarcel{ 548130803Smarcel const struct using_direct *current; 549130803Smarcel 550130803Smarcel /* First, go through the using directives. If any of them apply, 551130803Smarcel look in the appropriate namespaces for new functions to match 552130803Smarcel on. */ 553130803Smarcel 554130803Smarcel for (current = block_using (get_selected_block (0)); 555130803Smarcel current != NULL; 556130803Smarcel current = current->next) 557130803Smarcel { 558130803Smarcel if (strcmp (namespace, current->outer) == 0) 559130803Smarcel { 560130803Smarcel make_symbol_overload_list_using (func_name, 561130803Smarcel current->inner); 562130803Smarcel } 563130803Smarcel } 564130803Smarcel 565130803Smarcel /* Now, add names for this namespace. */ 566130803Smarcel 567130803Smarcel if (namespace[0] == '\0') 568130803Smarcel { 569130803Smarcel make_symbol_overload_list_qualified (func_name); 570130803Smarcel } 571130803Smarcel else 572130803Smarcel { 573130803Smarcel char *concatenated_name 574130803Smarcel = alloca (strlen (namespace) + 2 + strlen (func_name) + 1); 575130803Smarcel strcpy (concatenated_name, namespace); 576130803Smarcel strcat (concatenated_name, "::"); 577130803Smarcel strcat (concatenated_name, func_name); 578130803Smarcel make_symbol_overload_list_qualified (concatenated_name); 579130803Smarcel } 580130803Smarcel} 581130803Smarcel 582130803Smarcel/* This does the bulk of the work of finding overloaded symbols. 583130803Smarcel FUNC_NAME is the name of the overloaded function we're looking for 584130803Smarcel (possibly including namespace info). */ 585130803Smarcel 586130803Smarcelstatic void 587130803Smarcelmake_symbol_overload_list_qualified (const char *func_name) 588130803Smarcel{ 589130803Smarcel struct symbol *sym; 590130803Smarcel struct symtab *s; 591130803Smarcel struct objfile *objfile; 592130803Smarcel const struct block *b, *surrounding_static_block = 0; 593130803Smarcel struct dict_iterator iter; 594130803Smarcel const struct dictionary *dict; 595130803Smarcel 596130803Smarcel /* Look through the partial symtabs for all symbols which begin 597130803Smarcel by matching FUNC_NAME. Make sure we read that symbol table in. */ 598130803Smarcel 599130803Smarcel read_in_psymtabs (func_name); 600130803Smarcel 601130803Smarcel /* Search upwards from currently selected frame (so that we can 602130803Smarcel complete on local vars. */ 603130803Smarcel 604130803Smarcel for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b)) 605130803Smarcel { 606130803Smarcel dict = BLOCK_DICT (b); 607130803Smarcel 608130803Smarcel for (sym = dict_iter_name_first (dict, func_name, &iter); 609130803Smarcel sym; 610130803Smarcel sym = dict_iter_name_next (func_name, &iter)) 611130803Smarcel { 612130803Smarcel overload_list_add_symbol (sym, func_name); 613130803Smarcel } 614130803Smarcel } 615130803Smarcel 616130803Smarcel surrounding_static_block = block_static_block (get_selected_block (0)); 617130803Smarcel 618130803Smarcel /* Go through the symtabs and check the externs and statics for 619130803Smarcel symbols which match. */ 620130803Smarcel 621130803Smarcel ALL_SYMTABS (objfile, s) 622130803Smarcel { 623130803Smarcel QUIT; 624130803Smarcel b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK); 625130803Smarcel dict = BLOCK_DICT (b); 626130803Smarcel 627130803Smarcel for (sym = dict_iter_name_first (dict, func_name, &iter); 628130803Smarcel sym; 629130803Smarcel sym = dict_iter_name_next (func_name, &iter)) 630130803Smarcel { 631130803Smarcel overload_list_add_symbol (sym, func_name); 632130803Smarcel } 633130803Smarcel } 634130803Smarcel 635130803Smarcel ALL_SYMTABS (objfile, s) 636130803Smarcel { 637130803Smarcel QUIT; 638130803Smarcel b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK); 639130803Smarcel /* Don't do this block twice. */ 640130803Smarcel if (b == surrounding_static_block) 641130803Smarcel continue; 642130803Smarcel dict = BLOCK_DICT (b); 643130803Smarcel 644130803Smarcel for (sym = dict_iter_name_first (dict, func_name, &iter); 645130803Smarcel sym; 646130803Smarcel sym = dict_iter_name_next (func_name, &iter)) 647130803Smarcel { 648130803Smarcel overload_list_add_symbol (sym, func_name); 649130803Smarcel } 650130803Smarcel } 651130803Smarcel} 652130803Smarcel 653130803Smarcel/* Look through the partial symtabs for all symbols which begin 654130803Smarcel by matching FUNC_NAME. Make sure we read that symbol table in. */ 655130803Smarcel 656130803Smarcelstatic void 657130803Smarcelread_in_psymtabs (const char *func_name) 658130803Smarcel{ 659130803Smarcel struct partial_symtab *ps; 660130803Smarcel struct objfile *objfile; 661130803Smarcel 662130803Smarcel ALL_PSYMTABS (objfile, ps) 663130803Smarcel { 664130803Smarcel if (ps->readin) 665130803Smarcel continue; 666130803Smarcel 667130803Smarcel if ((lookup_partial_symbol (ps, func_name, NULL, 1, VAR_DOMAIN) 668130803Smarcel != NULL) 669130803Smarcel || (lookup_partial_symbol (ps, func_name, NULL, 0, VAR_DOMAIN) 670130803Smarcel != NULL)) 671130803Smarcel psymtab_to_symtab (ps); 672130803Smarcel } 673130803Smarcel} 674130803Smarcel 675130803Smarcel/* Lookup the rtti type for a class name. */ 676130803Smarcel 677130803Smarcelstruct type * 678130803Smarcelcp_lookup_rtti_type (const char *name, struct block *block) 679130803Smarcel{ 680130803Smarcel struct symbol * rtti_sym; 681130803Smarcel struct type * rtti_type; 682130803Smarcel 683130803Smarcel rtti_sym = lookup_symbol (name, block, STRUCT_DOMAIN, NULL, NULL); 684130803Smarcel 685130803Smarcel if (rtti_sym == NULL) 686130803Smarcel { 687130803Smarcel warning ("RTTI symbol not found for class '%s'", name); 688130803Smarcel return NULL; 689130803Smarcel } 690130803Smarcel 691130803Smarcel if (SYMBOL_CLASS (rtti_sym) != LOC_TYPEDEF) 692130803Smarcel { 693130803Smarcel warning ("RTTI symbol for class '%s' is not a type", name); 694130803Smarcel return NULL; 695130803Smarcel } 696130803Smarcel 697130803Smarcel rtti_type = SYMBOL_TYPE (rtti_sym); 698130803Smarcel 699130803Smarcel switch (TYPE_CODE (rtti_type)) 700130803Smarcel { 701130803Smarcel case TYPE_CODE_CLASS: 702130803Smarcel break; 703130803Smarcel case TYPE_CODE_NAMESPACE: 704130803Smarcel /* chastain/2003-11-26: the symbol tables often contain fake 705130803Smarcel symbols for namespaces with the same name as the struct. 706130803Smarcel This warning is an indication of a bug in the lookup order 707130803Smarcel or a bug in the way that the symbol tables are populated. */ 708130803Smarcel warning ("RTTI symbol for class '%s' is a namespace", name); 709130803Smarcel return NULL; 710130803Smarcel default: 711130803Smarcel warning ("RTTI symbol for class '%s' has bad type", name); 712130803Smarcel return NULL; 713130803Smarcel } 714130803Smarcel 715130803Smarcel return rtti_type; 716130803Smarcel} 717130803Smarcel 718130803Smarcel/* Don't allow just "maintenance cplus". */ 719130803Smarcel 720130803Smarcelstatic void 721130803Smarcelmaint_cplus_command (char *arg, int from_tty) 722130803Smarcel{ 723130803Smarcel printf_unfiltered ("\"maintenance cplus\" must be followed by the name of a command.\n"); 724130803Smarcel help_list (maint_cplus_cmd_list, "maintenance cplus ", -1, gdb_stdout); 725130803Smarcel} 726130803Smarcel 727130803Smarcel/* This is a front end for cp_find_first_component, for unit testing. 728130803Smarcel Be careful when using it: see the NOTE above 729130803Smarcel cp_find_first_component. */ 730130803Smarcel 731130803Smarcelstatic void 732130803Smarcelfirst_component_command (char *arg, int from_tty) 733130803Smarcel{ 734130803Smarcel int len = cp_find_first_component (arg); 735130803Smarcel char *prefix = alloca (len + 1); 736130803Smarcel 737130803Smarcel memcpy (prefix, arg, len); 738130803Smarcel prefix[len] = '\0'; 739130803Smarcel 740130803Smarcel printf_unfiltered ("%s\n", prefix); 741130803Smarcel} 742130803Smarcel 743130803Smarcelextern initialize_file_ftype _initialize_cp_support; /* -Wmissing-prototypes */ 744130803Smarcel 745130803Smarcelvoid 746130803Smarcel_initialize_cp_support (void) 747130803Smarcel{ 748130803Smarcel add_prefix_cmd ("cplus", class_maintenance, maint_cplus_command, 749130803Smarcel "C++ maintenance commands.", &maint_cplus_cmd_list, 750130803Smarcel "maintenance cplus ", 0, &maintenancelist); 751130803Smarcel add_alias_cmd ("cp", "cplus", class_maintenance, 1, &maintenancelist); 752130803Smarcel 753130803Smarcel add_cmd ("first_component", class_maintenance, first_component_command, 754130803Smarcel "Print the first class/namespace component of NAME.", 755130803Smarcel &maint_cplus_cmd_list); 756130803Smarcel 757130803Smarcel} 758