15796c8dcSSimon Schubert /* Helper routines for C++ support in GDB. 2*c50c785cSJohn Marino Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011 35796c8dcSSimon Schubert Free Software Foundation, Inc. 45796c8dcSSimon Schubert 55796c8dcSSimon Schubert Contributed by MontaVista Software. 65796c8dcSSimon Schubert 75796c8dcSSimon Schubert This file is part of GDB. 85796c8dcSSimon Schubert 95796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify 105796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by 115796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or 125796c8dcSSimon Schubert (at your option) any later version. 135796c8dcSSimon Schubert 145796c8dcSSimon Schubert This program is distributed in the hope that it will be useful, 155796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of 165796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 175796c8dcSSimon Schubert GNU General Public License for more details. 185796c8dcSSimon Schubert 195796c8dcSSimon Schubert You should have received a copy of the GNU General Public License 205796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */ 215796c8dcSSimon Schubert 225796c8dcSSimon Schubert #include "defs.h" 235796c8dcSSimon Schubert #include "cp-support.h" 245796c8dcSSimon Schubert #include "gdb_string.h" 255796c8dcSSimon Schubert #include "demangle.h" 265796c8dcSSimon Schubert #include "gdb_assert.h" 275796c8dcSSimon Schubert #include "gdbcmd.h" 285796c8dcSSimon Schubert #include "dictionary.h" 295796c8dcSSimon Schubert #include "objfiles.h" 305796c8dcSSimon Schubert #include "frame.h" 315796c8dcSSimon Schubert #include "symtab.h" 325796c8dcSSimon Schubert #include "block.h" 335796c8dcSSimon Schubert #include "complaints.h" 345796c8dcSSimon Schubert #include "gdbtypes.h" 35cf7f2e2dSJohn Marino #include "exceptions.h" 36cf7f2e2dSJohn Marino #include "expression.h" 37cf7f2e2dSJohn Marino #include "value.h" 385796c8dcSSimon Schubert 395796c8dcSSimon Schubert #include "safe-ctype.h" 405796c8dcSSimon Schubert 41cf7f2e2dSJohn Marino #include "psymtab.h" 42cf7f2e2dSJohn Marino 435796c8dcSSimon Schubert #define d_left(dc) (dc)->u.s_binary.left 445796c8dcSSimon Schubert #define d_right(dc) (dc)->u.s_binary.right 455796c8dcSSimon Schubert 465796c8dcSSimon Schubert /* Functions related to demangled name parsing. */ 475796c8dcSSimon Schubert 485796c8dcSSimon Schubert static unsigned int cp_find_first_component_aux (const char *name, 495796c8dcSSimon Schubert int permissive); 505796c8dcSSimon Schubert 515796c8dcSSimon Schubert static void demangled_name_complaint (const char *name); 525796c8dcSSimon Schubert 535796c8dcSSimon Schubert /* Functions/variables related to overload resolution. */ 545796c8dcSSimon Schubert 55cf7f2e2dSJohn Marino static int sym_return_val_size = -1; 565796c8dcSSimon Schubert static int sym_return_val_index; 575796c8dcSSimon Schubert static struct symbol **sym_return_val; 585796c8dcSSimon Schubert 595796c8dcSSimon Schubert static void overload_list_add_symbol (struct symbol *sym, 605796c8dcSSimon Schubert const char *oload_name); 615796c8dcSSimon Schubert 625796c8dcSSimon Schubert static void make_symbol_overload_list_using (const char *func_name, 635796c8dcSSimon Schubert const char *namespace); 645796c8dcSSimon Schubert 655796c8dcSSimon Schubert static void make_symbol_overload_list_qualified (const char *func_name); 665796c8dcSSimon Schubert 675796c8dcSSimon Schubert /* The list of "maint cplus" commands. */ 685796c8dcSSimon Schubert 695796c8dcSSimon Schubert struct cmd_list_element *maint_cplus_cmd_list = NULL; 705796c8dcSSimon Schubert 715796c8dcSSimon Schubert /* The actual commands. */ 725796c8dcSSimon Schubert 735796c8dcSSimon Schubert static void maint_cplus_command (char *arg, int from_tty); 745796c8dcSSimon Schubert static void first_component_command (char *arg, int from_tty); 755796c8dcSSimon Schubert 76cf7f2e2dSJohn Marino /* Operator validation. 77*c50c785cSJohn Marino NOTE: Multi-byte operators (usually the assignment variety 78*c50c785cSJohn Marino operator) must appear before the single byte version, i.e., "+=" 79*c50c785cSJohn Marino before "+". */ 80cf7f2e2dSJohn Marino static const char *operator_tokens[] = 81cf7f2e2dSJohn Marino { 82*c50c785cSJohn Marino "++", "+=", "+", "->*", "->", "--", "-=", "-", "*=", "*", 83*c50c785cSJohn Marino "/=", "/", "%=", "%", "!=", "==", "!", "&&", "<<=", "<<", 84*c50c785cSJohn Marino ">>=", ">>", "<=", "<", ">=", ">", "~", "&=", "&", "|=", 85*c50c785cSJohn Marino "||", "|", "^=", "^", "=", "()", "[]", ",", "new", "delete" 86cf7f2e2dSJohn Marino /* new[] and delete[] require special whitespace handling */ 87cf7f2e2dSJohn Marino }; 88cf7f2e2dSJohn Marino 895796c8dcSSimon Schubert /* Return 1 if STRING is clearly already in canonical form. This 905796c8dcSSimon Schubert function is conservative; things which it does not recognize are 915796c8dcSSimon Schubert assumed to be non-canonical, and the parser will sort them out 925796c8dcSSimon Schubert afterwards. This speeds up the critical path for alphanumeric 935796c8dcSSimon Schubert identifiers. */ 945796c8dcSSimon Schubert 955796c8dcSSimon Schubert static int 965796c8dcSSimon Schubert cp_already_canonical (const char *string) 975796c8dcSSimon Schubert { 985796c8dcSSimon Schubert /* Identifier start character [a-zA-Z_]. */ 995796c8dcSSimon Schubert if (!ISIDST (string[0])) 1005796c8dcSSimon Schubert return 0; 1015796c8dcSSimon Schubert 1025796c8dcSSimon Schubert /* These are the only two identifiers which canonicalize to other 1035796c8dcSSimon Schubert than themselves or an error: unsigned -> unsigned int and 1045796c8dcSSimon Schubert signed -> int. */ 1055796c8dcSSimon Schubert if (string[0] == 'u' && strcmp (&string[1], "nsigned") == 0) 1065796c8dcSSimon Schubert return 0; 1075796c8dcSSimon Schubert else if (string[0] == 's' && strcmp (&string[1], "igned") == 0) 1085796c8dcSSimon Schubert return 0; 1095796c8dcSSimon Schubert 1105796c8dcSSimon Schubert /* Identifier character [a-zA-Z0-9_]. */ 1115796c8dcSSimon Schubert while (ISIDNUM (string[1])) 1125796c8dcSSimon Schubert string++; 1135796c8dcSSimon Schubert 1145796c8dcSSimon Schubert if (string[1] == '\0') 1155796c8dcSSimon Schubert return 1; 1165796c8dcSSimon Schubert else 1175796c8dcSSimon Schubert return 0; 1185796c8dcSSimon Schubert } 1195796c8dcSSimon Schubert 1205796c8dcSSimon Schubert /* Parse STRING and convert it to canonical form. If parsing fails, 1215796c8dcSSimon Schubert or if STRING is already canonical, return NULL. Otherwise return 1225796c8dcSSimon Schubert the canonical form. The return value is allocated via xmalloc. */ 1235796c8dcSSimon Schubert 1245796c8dcSSimon Schubert char * 1255796c8dcSSimon Schubert cp_canonicalize_string (const char *string) 1265796c8dcSSimon Schubert { 1275796c8dcSSimon Schubert struct demangle_component *ret_comp; 1285796c8dcSSimon Schubert unsigned int estimated_len; 1295796c8dcSSimon Schubert char *ret; 1305796c8dcSSimon Schubert 1315796c8dcSSimon Schubert if (cp_already_canonical (string)) 1325796c8dcSSimon Schubert return NULL; 1335796c8dcSSimon Schubert 1345796c8dcSSimon Schubert ret_comp = cp_demangled_name_to_comp (string, NULL); 1355796c8dcSSimon Schubert if (ret_comp == NULL) 1365796c8dcSSimon Schubert return NULL; 1375796c8dcSSimon Schubert 1385796c8dcSSimon Schubert estimated_len = strlen (string) * 2; 1395796c8dcSSimon Schubert ret = cp_comp_to_string (ret_comp, estimated_len); 1405796c8dcSSimon Schubert 1415796c8dcSSimon Schubert if (strcmp (string, ret) == 0) 1425796c8dcSSimon Schubert { 1435796c8dcSSimon Schubert xfree (ret); 1445796c8dcSSimon Schubert return NULL; 1455796c8dcSSimon Schubert } 1465796c8dcSSimon Schubert 1475796c8dcSSimon Schubert return ret; 1485796c8dcSSimon Schubert } 1495796c8dcSSimon Schubert 150*c50c785cSJohn Marino /* Convert a mangled name to a demangle_component tree. *MEMORY is 151*c50c785cSJohn Marino set to the block of used memory that should be freed when finished 152*c50c785cSJohn Marino with the tree. DEMANGLED_P is set to the char * that should be 153*c50c785cSJohn Marino freed when finished with the tree, or NULL if none was needed. 154*c50c785cSJohn Marino OPTIONS will be passed to the demangler. */ 1555796c8dcSSimon Schubert 1565796c8dcSSimon Schubert static struct demangle_component * 1575796c8dcSSimon Schubert mangled_name_to_comp (const char *mangled_name, int options, 1585796c8dcSSimon Schubert void **memory, char **demangled_p) 1595796c8dcSSimon Schubert { 1605796c8dcSSimon Schubert struct demangle_component *ret; 1615796c8dcSSimon Schubert char *demangled_name; 1625796c8dcSSimon Schubert 1635796c8dcSSimon Schubert /* If it looks like a v3 mangled name, then try to go directly 1645796c8dcSSimon Schubert to trees. */ 1655796c8dcSSimon Schubert if (mangled_name[0] == '_' && mangled_name[1] == 'Z') 1665796c8dcSSimon Schubert { 167*c50c785cSJohn Marino ret = cplus_demangle_v3_components (mangled_name, 168*c50c785cSJohn Marino options, memory); 1695796c8dcSSimon Schubert if (ret) 1705796c8dcSSimon Schubert { 1715796c8dcSSimon Schubert *demangled_p = NULL; 1725796c8dcSSimon Schubert return ret; 1735796c8dcSSimon Schubert } 1745796c8dcSSimon Schubert } 1755796c8dcSSimon Schubert 176*c50c785cSJohn Marino /* If it doesn't, or if that failed, then try to demangle the 177*c50c785cSJohn Marino name. */ 1785796c8dcSSimon Schubert demangled_name = cplus_demangle (mangled_name, options); 1795796c8dcSSimon Schubert if (demangled_name == NULL) 1805796c8dcSSimon Schubert return NULL; 1815796c8dcSSimon Schubert 182*c50c785cSJohn Marino /* If we could demangle the name, parse it to build the component 183*c50c785cSJohn Marino tree. */ 1845796c8dcSSimon Schubert ret = cp_demangled_name_to_comp (demangled_name, NULL); 1855796c8dcSSimon Schubert 1865796c8dcSSimon Schubert if (ret == NULL) 1875796c8dcSSimon Schubert { 1885796c8dcSSimon Schubert xfree (demangled_name); 1895796c8dcSSimon Schubert return NULL; 1905796c8dcSSimon Schubert } 1915796c8dcSSimon Schubert 1925796c8dcSSimon Schubert *demangled_p = demangled_name; 1935796c8dcSSimon Schubert return ret; 1945796c8dcSSimon Schubert } 1955796c8dcSSimon Schubert 1965796c8dcSSimon Schubert /* Return the name of the class containing method PHYSNAME. */ 1975796c8dcSSimon Schubert 1985796c8dcSSimon Schubert char * 1995796c8dcSSimon Schubert cp_class_name_from_physname (const char *physname) 2005796c8dcSSimon Schubert { 2015796c8dcSSimon Schubert void *storage = NULL; 2025796c8dcSSimon Schubert char *demangled_name = NULL, *ret; 2035796c8dcSSimon Schubert struct demangle_component *ret_comp, *prev_comp, *cur_comp; 2045796c8dcSSimon Schubert int done; 2055796c8dcSSimon Schubert 206*c50c785cSJohn Marino ret_comp = mangled_name_to_comp (physname, DMGL_ANSI, 207*c50c785cSJohn Marino &storage, &demangled_name); 2085796c8dcSSimon Schubert if (ret_comp == NULL) 2095796c8dcSSimon Schubert return NULL; 2105796c8dcSSimon Schubert 2115796c8dcSSimon Schubert done = 0; 2125796c8dcSSimon Schubert 213*c50c785cSJohn Marino /* First strip off any qualifiers, if we have a function or 214*c50c785cSJohn Marino method. */ 2155796c8dcSSimon Schubert while (!done) 2165796c8dcSSimon Schubert switch (ret_comp->type) 2175796c8dcSSimon Schubert { 2185796c8dcSSimon Schubert case DEMANGLE_COMPONENT_CONST: 2195796c8dcSSimon Schubert case DEMANGLE_COMPONENT_RESTRICT: 2205796c8dcSSimon Schubert case DEMANGLE_COMPONENT_VOLATILE: 2215796c8dcSSimon Schubert case DEMANGLE_COMPONENT_CONST_THIS: 2225796c8dcSSimon Schubert case DEMANGLE_COMPONENT_RESTRICT_THIS: 2235796c8dcSSimon Schubert case DEMANGLE_COMPONENT_VOLATILE_THIS: 2245796c8dcSSimon Schubert case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: 2255796c8dcSSimon Schubert ret_comp = d_left (ret_comp); 2265796c8dcSSimon Schubert break; 2275796c8dcSSimon Schubert default: 2285796c8dcSSimon Schubert done = 1; 2295796c8dcSSimon Schubert break; 2305796c8dcSSimon Schubert } 2315796c8dcSSimon Schubert 2325796c8dcSSimon Schubert /* If what we have now is a function, discard the argument list. */ 2335796c8dcSSimon Schubert if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME) 2345796c8dcSSimon Schubert ret_comp = d_left (ret_comp); 2355796c8dcSSimon Schubert 2365796c8dcSSimon Schubert /* If what we have now is a template, strip off the template 2375796c8dcSSimon Schubert arguments. The left subtree may be a qualified name. */ 2385796c8dcSSimon Schubert if (ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE) 2395796c8dcSSimon Schubert ret_comp = d_left (ret_comp); 2405796c8dcSSimon Schubert 241*c50c785cSJohn Marino /* What we have now should be a name, possibly qualified. 242*c50c785cSJohn Marino Additional qualifiers could live in the left subtree or the right 243*c50c785cSJohn Marino subtree. Find the last piece. */ 2445796c8dcSSimon Schubert done = 0; 2455796c8dcSSimon Schubert prev_comp = NULL; 2465796c8dcSSimon Schubert cur_comp = ret_comp; 2475796c8dcSSimon Schubert while (!done) 2485796c8dcSSimon Schubert switch (cur_comp->type) 2495796c8dcSSimon Schubert { 2505796c8dcSSimon Schubert case DEMANGLE_COMPONENT_QUAL_NAME: 2515796c8dcSSimon Schubert case DEMANGLE_COMPONENT_LOCAL_NAME: 2525796c8dcSSimon Schubert prev_comp = cur_comp; 2535796c8dcSSimon Schubert cur_comp = d_right (cur_comp); 2545796c8dcSSimon Schubert break; 2555796c8dcSSimon Schubert case DEMANGLE_COMPONENT_TEMPLATE: 2565796c8dcSSimon Schubert case DEMANGLE_COMPONENT_NAME: 2575796c8dcSSimon Schubert case DEMANGLE_COMPONENT_CTOR: 2585796c8dcSSimon Schubert case DEMANGLE_COMPONENT_DTOR: 2595796c8dcSSimon Schubert case DEMANGLE_COMPONENT_OPERATOR: 2605796c8dcSSimon Schubert case DEMANGLE_COMPONENT_EXTENDED_OPERATOR: 2615796c8dcSSimon Schubert done = 1; 2625796c8dcSSimon Schubert break; 2635796c8dcSSimon Schubert default: 2645796c8dcSSimon Schubert done = 1; 2655796c8dcSSimon Schubert cur_comp = NULL; 2665796c8dcSSimon Schubert break; 2675796c8dcSSimon Schubert } 2685796c8dcSSimon Schubert 2695796c8dcSSimon Schubert ret = NULL; 2705796c8dcSSimon Schubert if (cur_comp != NULL && prev_comp != NULL) 2715796c8dcSSimon Schubert { 2725796c8dcSSimon Schubert /* We want to discard the rightmost child of PREV_COMP. */ 2735796c8dcSSimon Schubert *prev_comp = *d_left (prev_comp); 274*c50c785cSJohn Marino /* The ten is completely arbitrary; we don't have a good 275*c50c785cSJohn Marino estimate. */ 2765796c8dcSSimon Schubert ret = cp_comp_to_string (ret_comp, 10); 2775796c8dcSSimon Schubert } 2785796c8dcSSimon Schubert 2795796c8dcSSimon Schubert xfree (storage); 2805796c8dcSSimon Schubert if (demangled_name) 2815796c8dcSSimon Schubert xfree (demangled_name); 2825796c8dcSSimon Schubert return ret; 2835796c8dcSSimon Schubert } 2845796c8dcSSimon Schubert 285*c50c785cSJohn Marino /* Return the child of COMP which is the basename of a method, 286*c50c785cSJohn Marino variable, et cetera. All scope qualifiers are discarded, but 287*c50c785cSJohn Marino template arguments will be included. The component tree may be 288*c50c785cSJohn Marino modified. */ 2895796c8dcSSimon Schubert 2905796c8dcSSimon Schubert static struct demangle_component * 2915796c8dcSSimon Schubert unqualified_name_from_comp (struct demangle_component *comp) 2925796c8dcSSimon Schubert { 2935796c8dcSSimon Schubert struct demangle_component *ret_comp = comp, *last_template; 2945796c8dcSSimon Schubert int done; 2955796c8dcSSimon Schubert 2965796c8dcSSimon Schubert done = 0; 2975796c8dcSSimon Schubert last_template = NULL; 2985796c8dcSSimon Schubert while (!done) 2995796c8dcSSimon Schubert switch (ret_comp->type) 3005796c8dcSSimon Schubert { 3015796c8dcSSimon Schubert case DEMANGLE_COMPONENT_QUAL_NAME: 3025796c8dcSSimon Schubert case DEMANGLE_COMPONENT_LOCAL_NAME: 3035796c8dcSSimon Schubert ret_comp = d_right (ret_comp); 3045796c8dcSSimon Schubert break; 3055796c8dcSSimon Schubert case DEMANGLE_COMPONENT_TYPED_NAME: 3065796c8dcSSimon Schubert ret_comp = d_left (ret_comp); 3075796c8dcSSimon Schubert break; 3085796c8dcSSimon Schubert case DEMANGLE_COMPONENT_TEMPLATE: 3095796c8dcSSimon Schubert gdb_assert (last_template == NULL); 3105796c8dcSSimon Schubert last_template = ret_comp; 3115796c8dcSSimon Schubert ret_comp = d_left (ret_comp); 3125796c8dcSSimon Schubert break; 3135796c8dcSSimon Schubert case DEMANGLE_COMPONENT_CONST: 3145796c8dcSSimon Schubert case DEMANGLE_COMPONENT_RESTRICT: 3155796c8dcSSimon Schubert case DEMANGLE_COMPONENT_VOLATILE: 3165796c8dcSSimon Schubert case DEMANGLE_COMPONENT_CONST_THIS: 3175796c8dcSSimon Schubert case DEMANGLE_COMPONENT_RESTRICT_THIS: 3185796c8dcSSimon Schubert case DEMANGLE_COMPONENT_VOLATILE_THIS: 3195796c8dcSSimon Schubert case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: 3205796c8dcSSimon Schubert ret_comp = d_left (ret_comp); 3215796c8dcSSimon Schubert break; 3225796c8dcSSimon Schubert case DEMANGLE_COMPONENT_NAME: 3235796c8dcSSimon Schubert case DEMANGLE_COMPONENT_CTOR: 3245796c8dcSSimon Schubert case DEMANGLE_COMPONENT_DTOR: 3255796c8dcSSimon Schubert case DEMANGLE_COMPONENT_OPERATOR: 3265796c8dcSSimon Schubert case DEMANGLE_COMPONENT_EXTENDED_OPERATOR: 3275796c8dcSSimon Schubert done = 1; 3285796c8dcSSimon Schubert break; 3295796c8dcSSimon Schubert default: 3305796c8dcSSimon Schubert return NULL; 3315796c8dcSSimon Schubert break; 3325796c8dcSSimon Schubert } 3335796c8dcSSimon Schubert 3345796c8dcSSimon Schubert if (last_template) 3355796c8dcSSimon Schubert { 3365796c8dcSSimon Schubert d_left (last_template) = ret_comp; 3375796c8dcSSimon Schubert return last_template; 3385796c8dcSSimon Schubert } 3395796c8dcSSimon Schubert 3405796c8dcSSimon Schubert return ret_comp; 3415796c8dcSSimon Schubert } 3425796c8dcSSimon Schubert 3435796c8dcSSimon Schubert /* Return the name of the method whose linkage name is PHYSNAME. */ 3445796c8dcSSimon Schubert 3455796c8dcSSimon Schubert char * 3465796c8dcSSimon Schubert method_name_from_physname (const char *physname) 3475796c8dcSSimon Schubert { 3485796c8dcSSimon Schubert void *storage = NULL; 3495796c8dcSSimon Schubert char *demangled_name = NULL, *ret; 3505796c8dcSSimon Schubert struct demangle_component *ret_comp; 3515796c8dcSSimon Schubert 352*c50c785cSJohn Marino ret_comp = mangled_name_to_comp (physname, DMGL_ANSI, 353*c50c785cSJohn Marino &storage, &demangled_name); 3545796c8dcSSimon Schubert if (ret_comp == NULL) 3555796c8dcSSimon Schubert return NULL; 3565796c8dcSSimon Schubert 3575796c8dcSSimon Schubert ret_comp = unqualified_name_from_comp (ret_comp); 3585796c8dcSSimon Schubert 3595796c8dcSSimon Schubert ret = NULL; 3605796c8dcSSimon Schubert if (ret_comp != NULL) 361*c50c785cSJohn Marino /* The ten is completely arbitrary; we don't have a good 362*c50c785cSJohn Marino estimate. */ 3635796c8dcSSimon Schubert ret = cp_comp_to_string (ret_comp, 10); 3645796c8dcSSimon Schubert 3655796c8dcSSimon Schubert xfree (storage); 3665796c8dcSSimon Schubert if (demangled_name) 3675796c8dcSSimon Schubert xfree (demangled_name); 3685796c8dcSSimon Schubert return ret; 3695796c8dcSSimon Schubert } 3705796c8dcSSimon Schubert 3715796c8dcSSimon Schubert /* If FULL_NAME is the demangled name of a C++ function (including an 3725796c8dcSSimon Schubert arg list, possibly including namespace/class qualifications), 3735796c8dcSSimon Schubert return a new string containing only the function name (without the 3745796c8dcSSimon Schubert arg list/class qualifications). Otherwise, return NULL. The 3755796c8dcSSimon Schubert caller is responsible for freeing the memory in question. */ 3765796c8dcSSimon Schubert 3775796c8dcSSimon Schubert char * 3785796c8dcSSimon Schubert cp_func_name (const char *full_name) 3795796c8dcSSimon Schubert { 3805796c8dcSSimon Schubert char *ret; 3815796c8dcSSimon Schubert struct demangle_component *ret_comp; 3825796c8dcSSimon Schubert 3835796c8dcSSimon Schubert ret_comp = cp_demangled_name_to_comp (full_name, NULL); 3845796c8dcSSimon Schubert if (!ret_comp) 3855796c8dcSSimon Schubert return NULL; 3865796c8dcSSimon Schubert 3875796c8dcSSimon Schubert ret_comp = unqualified_name_from_comp (ret_comp); 3885796c8dcSSimon Schubert 3895796c8dcSSimon Schubert ret = NULL; 3905796c8dcSSimon Schubert if (ret_comp != NULL) 3915796c8dcSSimon Schubert ret = cp_comp_to_string (ret_comp, 10); 3925796c8dcSSimon Schubert 3935796c8dcSSimon Schubert return ret; 3945796c8dcSSimon Schubert } 3955796c8dcSSimon Schubert 3965796c8dcSSimon Schubert /* DEMANGLED_NAME is the name of a function, including parameters and 3975796c8dcSSimon Schubert (optionally) a return type. Return the name of the function without 3985796c8dcSSimon Schubert parameters or return type, or NULL if we can not parse the name. */ 3995796c8dcSSimon Schubert 4005796c8dcSSimon Schubert char * 4015796c8dcSSimon Schubert cp_remove_params (const char *demangled_name) 4025796c8dcSSimon Schubert { 4035796c8dcSSimon Schubert int done = 0; 4045796c8dcSSimon Schubert struct demangle_component *ret_comp; 4055796c8dcSSimon Schubert char *ret = NULL; 4065796c8dcSSimon Schubert 4075796c8dcSSimon Schubert if (demangled_name == NULL) 4085796c8dcSSimon Schubert return NULL; 4095796c8dcSSimon Schubert 4105796c8dcSSimon Schubert ret_comp = cp_demangled_name_to_comp (demangled_name, NULL); 4115796c8dcSSimon Schubert if (ret_comp == NULL) 4125796c8dcSSimon Schubert return NULL; 4135796c8dcSSimon Schubert 4145796c8dcSSimon Schubert /* First strip off any qualifiers, if we have a function or method. */ 4155796c8dcSSimon Schubert while (!done) 4165796c8dcSSimon Schubert switch (ret_comp->type) 4175796c8dcSSimon Schubert { 4185796c8dcSSimon Schubert case DEMANGLE_COMPONENT_CONST: 4195796c8dcSSimon Schubert case DEMANGLE_COMPONENT_RESTRICT: 4205796c8dcSSimon Schubert case DEMANGLE_COMPONENT_VOLATILE: 4215796c8dcSSimon Schubert case DEMANGLE_COMPONENT_CONST_THIS: 4225796c8dcSSimon Schubert case DEMANGLE_COMPONENT_RESTRICT_THIS: 4235796c8dcSSimon Schubert case DEMANGLE_COMPONENT_VOLATILE_THIS: 4245796c8dcSSimon Schubert case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: 4255796c8dcSSimon Schubert ret_comp = d_left (ret_comp); 4265796c8dcSSimon Schubert break; 4275796c8dcSSimon Schubert default: 4285796c8dcSSimon Schubert done = 1; 4295796c8dcSSimon Schubert break; 4305796c8dcSSimon Schubert } 4315796c8dcSSimon Schubert 4325796c8dcSSimon Schubert /* What we have now should be a function. Return its name. */ 4335796c8dcSSimon Schubert if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME) 4345796c8dcSSimon Schubert ret = cp_comp_to_string (d_left (ret_comp), 10); 4355796c8dcSSimon Schubert 4365796c8dcSSimon Schubert return ret; 4375796c8dcSSimon Schubert } 4385796c8dcSSimon Schubert 4395796c8dcSSimon Schubert /* Here are some random pieces of trivia to keep in mind while trying 4405796c8dcSSimon Schubert to take apart demangled names: 4415796c8dcSSimon Schubert 4425796c8dcSSimon Schubert - Names can contain function arguments or templates, so the process 4435796c8dcSSimon Schubert has to be, to some extent recursive: maybe keep track of your 4445796c8dcSSimon Schubert depth based on encountering <> and (). 4455796c8dcSSimon Schubert 4465796c8dcSSimon Schubert - Parentheses don't just have to happen at the end of a name: they 4475796c8dcSSimon Schubert can occur even if the name in question isn't a function, because 4485796c8dcSSimon Schubert a template argument might be a type that's a function. 4495796c8dcSSimon Schubert 4505796c8dcSSimon Schubert - Conversely, even if you're trying to deal with a function, its 4515796c8dcSSimon Schubert demangled name might not end with ')': it could be a const or 4525796c8dcSSimon Schubert volatile class method, in which case it ends with "const" or 4535796c8dcSSimon Schubert "volatile". 4545796c8dcSSimon Schubert 4555796c8dcSSimon Schubert - Parentheses are also used in anonymous namespaces: a variable 4565796c8dcSSimon Schubert 'foo' in an anonymous namespace gets demangled as "(anonymous 4575796c8dcSSimon Schubert namespace)::foo". 4585796c8dcSSimon Schubert 4595796c8dcSSimon Schubert - And operator names can contain parentheses or angle brackets. */ 4605796c8dcSSimon Schubert 4615796c8dcSSimon Schubert /* FIXME: carlton/2003-03-13: We have several functions here with 4625796c8dcSSimon Schubert overlapping functionality; can we combine them? Also, do they 4635796c8dcSSimon Schubert handle all the above considerations correctly? */ 4645796c8dcSSimon Schubert 4655796c8dcSSimon Schubert 4665796c8dcSSimon Schubert /* This returns the length of first component of NAME, which should be 4675796c8dcSSimon Schubert the demangled name of a C++ variable/function/method/etc. 4685796c8dcSSimon Schubert Specifically, it returns the index of the first colon forming the 4695796c8dcSSimon Schubert boundary of the first component: so, given 'A::foo' or 'A::B::foo' 4705796c8dcSSimon Schubert it returns the 1, and given 'foo', it returns 0. */ 4715796c8dcSSimon Schubert 4725796c8dcSSimon Schubert /* The character in NAME indexed by the return value is guaranteed to 4735796c8dcSSimon Schubert always be either ':' or '\0'. */ 4745796c8dcSSimon Schubert 4755796c8dcSSimon Schubert /* NOTE: carlton/2003-03-13: This function is currently only intended 4765796c8dcSSimon Schubert for internal use: it's probably not entirely safe when called on 4775796c8dcSSimon Schubert user-generated input, because some of the 'index += 2' lines in 4785796c8dcSSimon Schubert cp_find_first_component_aux might go past the end of malformed 4795796c8dcSSimon Schubert input. */ 4805796c8dcSSimon Schubert 4815796c8dcSSimon Schubert unsigned int 4825796c8dcSSimon Schubert cp_find_first_component (const char *name) 4835796c8dcSSimon Schubert { 4845796c8dcSSimon Schubert return cp_find_first_component_aux (name, 0); 4855796c8dcSSimon Schubert } 4865796c8dcSSimon Schubert 4875796c8dcSSimon Schubert /* Helper function for cp_find_first_component. Like that function, 4885796c8dcSSimon Schubert it returns the length of the first component of NAME, but to make 4895796c8dcSSimon Schubert the recursion easier, it also stops if it reaches an unexpected ')' 4905796c8dcSSimon Schubert or '>' if the value of PERMISSIVE is nonzero. */ 4915796c8dcSSimon Schubert 4925796c8dcSSimon Schubert /* Let's optimize away calls to strlen("operator"). */ 4935796c8dcSSimon Schubert 4945796c8dcSSimon Schubert #define LENGTH_OF_OPERATOR 8 4955796c8dcSSimon Schubert 4965796c8dcSSimon Schubert static unsigned int 4975796c8dcSSimon Schubert cp_find_first_component_aux (const char *name, int permissive) 4985796c8dcSSimon Schubert { 4995796c8dcSSimon Schubert unsigned int index = 0; 5005796c8dcSSimon Schubert /* Operator names can show up in unexpected places. Since these can 5015796c8dcSSimon Schubert contain parentheses or angle brackets, they can screw up the 5025796c8dcSSimon Schubert recursion. But not every string 'operator' is part of an 5035796c8dcSSimon Schubert operater name: e.g. you could have a variable 'cooperator'. So 5045796c8dcSSimon Schubert this variable tells us whether or not we should treat the string 5055796c8dcSSimon Schubert 'operator' as starting an operator. */ 5065796c8dcSSimon Schubert int operator_possible = 1; 5075796c8dcSSimon Schubert 5085796c8dcSSimon Schubert for (;; ++index) 5095796c8dcSSimon Schubert { 5105796c8dcSSimon Schubert switch (name[index]) 5115796c8dcSSimon Schubert { 5125796c8dcSSimon Schubert case '<': 5135796c8dcSSimon Schubert /* Template; eat it up. The calls to cp_first_component 5145796c8dcSSimon Schubert should only return (I hope!) when they reach the '>' 5155796c8dcSSimon Schubert terminating the component or a '::' between two 5165796c8dcSSimon Schubert components. (Hence the '+ 2'.) */ 5175796c8dcSSimon Schubert index += 1; 5185796c8dcSSimon Schubert for (index += cp_find_first_component_aux (name + index, 1); 5195796c8dcSSimon Schubert name[index] != '>'; 5205796c8dcSSimon Schubert index += cp_find_first_component_aux (name + index, 1)) 5215796c8dcSSimon Schubert { 5225796c8dcSSimon Schubert if (name[index] != ':') 5235796c8dcSSimon Schubert { 5245796c8dcSSimon Schubert demangled_name_complaint (name); 5255796c8dcSSimon Schubert return strlen (name); 5265796c8dcSSimon Schubert } 5275796c8dcSSimon Schubert index += 2; 5285796c8dcSSimon Schubert } 5295796c8dcSSimon Schubert operator_possible = 1; 5305796c8dcSSimon Schubert break; 5315796c8dcSSimon Schubert case '(': 5325796c8dcSSimon Schubert /* Similar comment as to '<'. */ 5335796c8dcSSimon Schubert index += 1; 5345796c8dcSSimon Schubert for (index += cp_find_first_component_aux (name + index, 1); 5355796c8dcSSimon Schubert name[index] != ')'; 5365796c8dcSSimon Schubert index += cp_find_first_component_aux (name + index, 1)) 5375796c8dcSSimon Schubert { 5385796c8dcSSimon Schubert if (name[index] != ':') 5395796c8dcSSimon Schubert { 5405796c8dcSSimon Schubert demangled_name_complaint (name); 5415796c8dcSSimon Schubert return strlen (name); 5425796c8dcSSimon Schubert } 5435796c8dcSSimon Schubert index += 2; 5445796c8dcSSimon Schubert } 5455796c8dcSSimon Schubert operator_possible = 1; 5465796c8dcSSimon Schubert break; 5475796c8dcSSimon Schubert case '>': 5485796c8dcSSimon Schubert case ')': 5495796c8dcSSimon Schubert if (permissive) 5505796c8dcSSimon Schubert return index; 5515796c8dcSSimon Schubert else 5525796c8dcSSimon Schubert { 5535796c8dcSSimon Schubert demangled_name_complaint (name); 5545796c8dcSSimon Schubert return strlen (name); 5555796c8dcSSimon Schubert } 5565796c8dcSSimon Schubert case '\0': 5575796c8dcSSimon Schubert case ':': 5585796c8dcSSimon Schubert return index; 5595796c8dcSSimon Schubert case 'o': 5605796c8dcSSimon Schubert /* Operator names can screw up the recursion. */ 5615796c8dcSSimon Schubert if (operator_possible 562*c50c785cSJohn Marino && strncmp (name + index, "operator", 563*c50c785cSJohn Marino LENGTH_OF_OPERATOR) == 0) 5645796c8dcSSimon Schubert { 5655796c8dcSSimon Schubert index += LENGTH_OF_OPERATOR; 5665796c8dcSSimon Schubert while (ISSPACE(name[index])) 5675796c8dcSSimon Schubert ++index; 5685796c8dcSSimon Schubert switch (name[index]) 5695796c8dcSSimon Schubert { 5705796c8dcSSimon Schubert /* Skip over one less than the appropriate number of 5715796c8dcSSimon Schubert characters: the for loop will skip over the last 5725796c8dcSSimon Schubert one. */ 5735796c8dcSSimon Schubert case '<': 5745796c8dcSSimon Schubert if (name[index + 1] == '<') 5755796c8dcSSimon Schubert index += 1; 5765796c8dcSSimon Schubert else 5775796c8dcSSimon Schubert index += 0; 5785796c8dcSSimon Schubert break; 5795796c8dcSSimon Schubert case '>': 5805796c8dcSSimon Schubert case '-': 5815796c8dcSSimon Schubert if (name[index + 1] == '>') 5825796c8dcSSimon Schubert index += 1; 5835796c8dcSSimon Schubert else 5845796c8dcSSimon Schubert index += 0; 5855796c8dcSSimon Schubert break; 5865796c8dcSSimon Schubert case '(': 5875796c8dcSSimon Schubert index += 1; 5885796c8dcSSimon Schubert break; 5895796c8dcSSimon Schubert default: 5905796c8dcSSimon Schubert index += 0; 5915796c8dcSSimon Schubert break; 5925796c8dcSSimon Schubert } 5935796c8dcSSimon Schubert } 5945796c8dcSSimon Schubert operator_possible = 0; 5955796c8dcSSimon Schubert break; 5965796c8dcSSimon Schubert case ' ': 5975796c8dcSSimon Schubert case ',': 5985796c8dcSSimon Schubert case '.': 5995796c8dcSSimon Schubert case '&': 6005796c8dcSSimon Schubert case '*': 6015796c8dcSSimon Schubert /* NOTE: carlton/2003-04-18: I'm not sure what the precise 6025796c8dcSSimon Schubert set of relevant characters are here: it's necessary to 6035796c8dcSSimon Schubert include any character that can show up before 'operator' 6045796c8dcSSimon Schubert in a demangled name, and it's safe to include any 6055796c8dcSSimon Schubert character that can't be part of an identifier's name. */ 6065796c8dcSSimon Schubert operator_possible = 1; 6075796c8dcSSimon Schubert break; 6085796c8dcSSimon Schubert default: 6095796c8dcSSimon Schubert operator_possible = 0; 6105796c8dcSSimon Schubert break; 6115796c8dcSSimon Schubert } 6125796c8dcSSimon Schubert } 6135796c8dcSSimon Schubert } 6145796c8dcSSimon Schubert 6155796c8dcSSimon Schubert /* Complain about a demangled name that we don't know how to parse. 6165796c8dcSSimon Schubert NAME is the demangled name in question. */ 6175796c8dcSSimon Schubert 6185796c8dcSSimon Schubert static void 6195796c8dcSSimon Schubert demangled_name_complaint (const char *name) 6205796c8dcSSimon Schubert { 6215796c8dcSSimon Schubert complaint (&symfile_complaints, 6225796c8dcSSimon Schubert "unexpected demangled name '%s'", name); 6235796c8dcSSimon Schubert } 6245796c8dcSSimon Schubert 6255796c8dcSSimon Schubert /* If NAME is the fully-qualified name of a C++ 6265796c8dcSSimon Schubert function/variable/method/etc., this returns the length of its 6275796c8dcSSimon Schubert entire prefix: all of the namespaces and classes that make up its 6285796c8dcSSimon Schubert name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns 6295796c8dcSSimon Schubert 4, given 'foo', it returns 0. */ 6305796c8dcSSimon Schubert 6315796c8dcSSimon Schubert unsigned int 6325796c8dcSSimon Schubert cp_entire_prefix_len (const char *name) 6335796c8dcSSimon Schubert { 6345796c8dcSSimon Schubert unsigned int current_len = cp_find_first_component (name); 6355796c8dcSSimon Schubert unsigned int previous_len = 0; 6365796c8dcSSimon Schubert 6375796c8dcSSimon Schubert while (name[current_len] != '\0') 6385796c8dcSSimon Schubert { 6395796c8dcSSimon Schubert gdb_assert (name[current_len] == ':'); 6405796c8dcSSimon Schubert previous_len = current_len; 6415796c8dcSSimon Schubert /* Skip the '::'. */ 6425796c8dcSSimon Schubert current_len += 2; 6435796c8dcSSimon Schubert current_len += cp_find_first_component (name + current_len); 6445796c8dcSSimon Schubert } 6455796c8dcSSimon Schubert 6465796c8dcSSimon Schubert return previous_len; 6475796c8dcSSimon Schubert } 6485796c8dcSSimon Schubert 6495796c8dcSSimon Schubert /* Overload resolution functions. */ 6505796c8dcSSimon Schubert 6515796c8dcSSimon Schubert /* Test to see if SYM is a symbol that we haven't seen corresponding 6525796c8dcSSimon Schubert to a function named OLOAD_NAME. If so, add it to the current 6535796c8dcSSimon Schubert completion list. */ 6545796c8dcSSimon Schubert 6555796c8dcSSimon Schubert static void 656*c50c785cSJohn Marino overload_list_add_symbol (struct symbol *sym, 657*c50c785cSJohn Marino const char *oload_name) 6585796c8dcSSimon Schubert { 6595796c8dcSSimon Schubert int newsize; 6605796c8dcSSimon Schubert int i; 6615796c8dcSSimon Schubert char *sym_name; 6625796c8dcSSimon Schubert 663*c50c785cSJohn Marino /* If there is no type information, we can't do anything, so 664*c50c785cSJohn Marino skip. */ 6655796c8dcSSimon Schubert if (SYMBOL_TYPE (sym) == NULL) 6665796c8dcSSimon Schubert return; 6675796c8dcSSimon Schubert 6685796c8dcSSimon Schubert /* skip any symbols that we've already considered. */ 6695796c8dcSSimon Schubert for (i = 0; i < sym_return_val_index; ++i) 6705796c8dcSSimon Schubert if (strcmp (SYMBOL_LINKAGE_NAME (sym), 6715796c8dcSSimon Schubert SYMBOL_LINKAGE_NAME (sym_return_val[i])) == 0) 6725796c8dcSSimon Schubert return; 6735796c8dcSSimon Schubert 6745796c8dcSSimon Schubert /* Get the demangled name without parameters */ 6755796c8dcSSimon Schubert sym_name = cp_remove_params (SYMBOL_NATURAL_NAME (sym)); 6765796c8dcSSimon Schubert if (!sym_name) 6775796c8dcSSimon Schubert return; 6785796c8dcSSimon Schubert 6795796c8dcSSimon Schubert /* skip symbols that cannot match */ 6805796c8dcSSimon Schubert if (strcmp (sym_name, oload_name) != 0) 6815796c8dcSSimon Schubert { 6825796c8dcSSimon Schubert xfree (sym_name); 6835796c8dcSSimon Schubert return; 6845796c8dcSSimon Schubert } 6855796c8dcSSimon Schubert 6865796c8dcSSimon Schubert xfree (sym_name); 6875796c8dcSSimon Schubert 688*c50c785cSJohn Marino /* We have a match for an overload instance, so add SYM to the 689*c50c785cSJohn Marino current list of overload instances */ 6905796c8dcSSimon Schubert if (sym_return_val_index + 3 > sym_return_val_size) 6915796c8dcSSimon Schubert { 6925796c8dcSSimon Schubert newsize = (sym_return_val_size *= 2) * sizeof (struct symbol *); 693*c50c785cSJohn Marino sym_return_val = (struct symbol **) 694*c50c785cSJohn Marino xrealloc ((char *) sym_return_val, newsize); 6955796c8dcSSimon Schubert } 6965796c8dcSSimon Schubert sym_return_val[sym_return_val_index++] = sym; 6975796c8dcSSimon Schubert sym_return_val[sym_return_val_index] = NULL; 6985796c8dcSSimon Schubert } 6995796c8dcSSimon Schubert 7005796c8dcSSimon Schubert /* Return a null-terminated list of pointers to function symbols that 7015796c8dcSSimon Schubert are named FUNC_NAME and are visible within NAMESPACE. */ 7025796c8dcSSimon Schubert 7035796c8dcSSimon Schubert struct symbol ** 7045796c8dcSSimon Schubert make_symbol_overload_list (const char *func_name, 7055796c8dcSSimon Schubert const char *namespace) 7065796c8dcSSimon Schubert { 7075796c8dcSSimon Schubert struct cleanup *old_cleanups; 708cf7f2e2dSJohn Marino const char *name; 7095796c8dcSSimon Schubert 7105796c8dcSSimon Schubert sym_return_val_size = 100; 7115796c8dcSSimon Schubert sym_return_val_index = 0; 7125796c8dcSSimon Schubert sym_return_val = xmalloc ((sym_return_val_size + 1) * 7135796c8dcSSimon Schubert sizeof (struct symbol *)); 7145796c8dcSSimon Schubert sym_return_val[0] = NULL; 7155796c8dcSSimon Schubert 7165796c8dcSSimon Schubert old_cleanups = make_cleanup (xfree, sym_return_val); 7175796c8dcSSimon Schubert 7185796c8dcSSimon Schubert make_symbol_overload_list_using (func_name, namespace); 7195796c8dcSSimon Schubert 720cf7f2e2dSJohn Marino if (namespace[0] == '\0') 721cf7f2e2dSJohn Marino name = func_name; 722cf7f2e2dSJohn Marino else 723cf7f2e2dSJohn Marino { 724cf7f2e2dSJohn Marino char *concatenated_name 725cf7f2e2dSJohn Marino = alloca (strlen (namespace) + 2 + strlen (func_name) + 1); 726cf7f2e2dSJohn Marino strcpy (concatenated_name, namespace); 727cf7f2e2dSJohn Marino strcat (concatenated_name, "::"); 728cf7f2e2dSJohn Marino strcat (concatenated_name, func_name); 729cf7f2e2dSJohn Marino name = concatenated_name; 730cf7f2e2dSJohn Marino } 731cf7f2e2dSJohn Marino 732cf7f2e2dSJohn Marino make_symbol_overload_list_qualified (name); 733cf7f2e2dSJohn Marino 7345796c8dcSSimon Schubert discard_cleanups (old_cleanups); 7355796c8dcSSimon Schubert 7365796c8dcSSimon Schubert return sym_return_val; 7375796c8dcSSimon Schubert } 7385796c8dcSSimon Schubert 739cf7f2e2dSJohn Marino /* Add all symbols with a name matching NAME in BLOCK to the overload 740cf7f2e2dSJohn Marino list. */ 741cf7f2e2dSJohn Marino 742cf7f2e2dSJohn Marino static void 743cf7f2e2dSJohn Marino make_symbol_overload_list_block (const char *name, 744cf7f2e2dSJohn Marino const struct block *block) 745cf7f2e2dSJohn Marino { 746cf7f2e2dSJohn Marino struct dict_iterator iter; 747cf7f2e2dSJohn Marino struct symbol *sym; 748cf7f2e2dSJohn Marino 749cf7f2e2dSJohn Marino const struct dictionary *dict = BLOCK_DICT (block); 750cf7f2e2dSJohn Marino 751cf7f2e2dSJohn Marino for (sym = dict_iter_name_first (dict, name, &iter); 752cf7f2e2dSJohn Marino sym != NULL; 753cf7f2e2dSJohn Marino sym = dict_iter_name_next (name, &iter)) 754cf7f2e2dSJohn Marino overload_list_add_symbol (sym, name); 755cf7f2e2dSJohn Marino } 756cf7f2e2dSJohn Marino 757cf7f2e2dSJohn Marino /* Adds the function FUNC_NAME from NAMESPACE to the overload set. */ 758cf7f2e2dSJohn Marino 759cf7f2e2dSJohn Marino static void 760cf7f2e2dSJohn Marino make_symbol_overload_list_namespace (const char *func_name, 761cf7f2e2dSJohn Marino const char *namespace) 762cf7f2e2dSJohn Marino { 763cf7f2e2dSJohn Marino const char *name; 764cf7f2e2dSJohn Marino const struct block *block = NULL; 765cf7f2e2dSJohn Marino 766cf7f2e2dSJohn Marino if (namespace[0] == '\0') 767cf7f2e2dSJohn Marino name = func_name; 768cf7f2e2dSJohn Marino else 769cf7f2e2dSJohn Marino { 770cf7f2e2dSJohn Marino char *concatenated_name 771cf7f2e2dSJohn Marino = alloca (strlen (namespace) + 2 + strlen (func_name) + 1); 772cf7f2e2dSJohn Marino 773cf7f2e2dSJohn Marino strcpy (concatenated_name, namespace); 774cf7f2e2dSJohn Marino strcat (concatenated_name, "::"); 775cf7f2e2dSJohn Marino strcat (concatenated_name, func_name); 776cf7f2e2dSJohn Marino name = concatenated_name; 777cf7f2e2dSJohn Marino } 778cf7f2e2dSJohn Marino 779cf7f2e2dSJohn Marino /* Look in the static block. */ 780cf7f2e2dSJohn Marino block = block_static_block (get_selected_block (0)); 781*c50c785cSJohn Marino if (block) 782cf7f2e2dSJohn Marino make_symbol_overload_list_block (name, block); 783cf7f2e2dSJohn Marino 784cf7f2e2dSJohn Marino /* Look in the global block. */ 785cf7f2e2dSJohn Marino block = block_global_block (block); 786*c50c785cSJohn Marino if (block) 787cf7f2e2dSJohn Marino make_symbol_overload_list_block (name, block); 788cf7f2e2dSJohn Marino 789cf7f2e2dSJohn Marino } 790cf7f2e2dSJohn Marino 791*c50c785cSJohn Marino /* Search the namespace of the given type and namespace of and public 792*c50c785cSJohn Marino base types. */ 793cf7f2e2dSJohn Marino 794cf7f2e2dSJohn Marino static void 795cf7f2e2dSJohn Marino make_symbol_overload_list_adl_namespace (struct type *type, 796cf7f2e2dSJohn Marino const char *func_name) 797cf7f2e2dSJohn Marino { 798cf7f2e2dSJohn Marino char *namespace; 799cf7f2e2dSJohn Marino char *type_name; 800cf7f2e2dSJohn Marino int i, prefix_len; 801cf7f2e2dSJohn Marino 802*c50c785cSJohn Marino while (TYPE_CODE (type) == TYPE_CODE_PTR 803*c50c785cSJohn Marino || TYPE_CODE (type) == TYPE_CODE_REF 804cf7f2e2dSJohn Marino || TYPE_CODE (type) == TYPE_CODE_ARRAY 805cf7f2e2dSJohn Marino || TYPE_CODE (type) == TYPE_CODE_TYPEDEF) 806cf7f2e2dSJohn Marino { 807cf7f2e2dSJohn Marino if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF) 808cf7f2e2dSJohn Marino type = check_typedef(type); 809cf7f2e2dSJohn Marino else 810cf7f2e2dSJohn Marino type = TYPE_TARGET_TYPE (type); 811cf7f2e2dSJohn Marino } 812cf7f2e2dSJohn Marino 813cf7f2e2dSJohn Marino type_name = TYPE_NAME (type); 814cf7f2e2dSJohn Marino 815cf7f2e2dSJohn Marino if (type_name == NULL) 816cf7f2e2dSJohn Marino return; 817cf7f2e2dSJohn Marino 818cf7f2e2dSJohn Marino prefix_len = cp_entire_prefix_len (type_name); 819cf7f2e2dSJohn Marino 820cf7f2e2dSJohn Marino if (prefix_len != 0) 821cf7f2e2dSJohn Marino { 822cf7f2e2dSJohn Marino namespace = alloca (prefix_len + 1); 823cf7f2e2dSJohn Marino strncpy (namespace, type_name, prefix_len); 824cf7f2e2dSJohn Marino namespace[prefix_len] = '\0'; 825cf7f2e2dSJohn Marino 826cf7f2e2dSJohn Marino make_symbol_overload_list_namespace (func_name, namespace); 827cf7f2e2dSJohn Marino } 828cf7f2e2dSJohn Marino 829cf7f2e2dSJohn Marino /* Check public base type */ 830cf7f2e2dSJohn Marino if (TYPE_CODE (type) == TYPE_CODE_CLASS) 831cf7f2e2dSJohn Marino for (i = 0; i < TYPE_N_BASECLASSES (type); i++) 832cf7f2e2dSJohn Marino { 833cf7f2e2dSJohn Marino if (BASETYPE_VIA_PUBLIC (type, i)) 834*c50c785cSJohn Marino make_symbol_overload_list_adl_namespace (TYPE_BASECLASS (type, 835*c50c785cSJohn Marino i), 836cf7f2e2dSJohn Marino func_name); 837cf7f2e2dSJohn Marino } 838cf7f2e2dSJohn Marino } 839cf7f2e2dSJohn Marino 840*c50c785cSJohn Marino /* Adds the overload list overload candidates for FUNC_NAME found 841*c50c785cSJohn Marino through argument dependent lookup. */ 842cf7f2e2dSJohn Marino 843cf7f2e2dSJohn Marino struct symbol ** 844cf7f2e2dSJohn Marino make_symbol_overload_list_adl (struct type **arg_types, int nargs, 845cf7f2e2dSJohn Marino const char *func_name) 846cf7f2e2dSJohn Marino { 847cf7f2e2dSJohn Marino int i; 848cf7f2e2dSJohn Marino 849cf7f2e2dSJohn Marino gdb_assert (sym_return_val_size != -1); 850cf7f2e2dSJohn Marino 851cf7f2e2dSJohn Marino for (i = 1; i <= nargs; i++) 852*c50c785cSJohn Marino make_symbol_overload_list_adl_namespace (arg_types[i - 1], 853*c50c785cSJohn Marino func_name); 854cf7f2e2dSJohn Marino 855cf7f2e2dSJohn Marino return sym_return_val; 856cf7f2e2dSJohn Marino } 857cf7f2e2dSJohn Marino 858*c50c785cSJohn Marino /* Used for cleanups to reset the "searched" flag in case of an 859*c50c785cSJohn Marino error. */ 860cf7f2e2dSJohn Marino 861cf7f2e2dSJohn Marino static void 862cf7f2e2dSJohn Marino reset_directive_searched (void *data) 863cf7f2e2dSJohn Marino { 864cf7f2e2dSJohn Marino struct using_direct *direct = data; 865cf7f2e2dSJohn Marino direct->searched = 0; 866cf7f2e2dSJohn Marino } 867cf7f2e2dSJohn Marino 8685796c8dcSSimon Schubert /* This applies the using directives to add namespaces to search in, 8695796c8dcSSimon Schubert and then searches for overloads in all of those namespaces. It 8705796c8dcSSimon Schubert adds the symbols found to sym_return_val. Arguments are as in 8715796c8dcSSimon Schubert make_symbol_overload_list. */ 8725796c8dcSSimon Schubert 8735796c8dcSSimon Schubert static void 8745796c8dcSSimon Schubert make_symbol_overload_list_using (const char *func_name, 8755796c8dcSSimon Schubert const char *namespace) 8765796c8dcSSimon Schubert { 877cf7f2e2dSJohn Marino struct using_direct *current; 878cf7f2e2dSJohn Marino const struct block *block; 8795796c8dcSSimon Schubert 8805796c8dcSSimon Schubert /* First, go through the using directives. If any of them apply, 8815796c8dcSSimon Schubert look in the appropriate namespaces for new functions to match 8825796c8dcSSimon Schubert on. */ 8835796c8dcSSimon Schubert 884cf7f2e2dSJohn Marino for (block = get_selected_block (0); 885cf7f2e2dSJohn Marino block != NULL; 886cf7f2e2dSJohn Marino block = BLOCK_SUPERBLOCK (block)) 887cf7f2e2dSJohn Marino for (current = block_using (block); 8885796c8dcSSimon Schubert current != NULL; 8895796c8dcSSimon Schubert current = current->next) 8905796c8dcSSimon Schubert { 891cf7f2e2dSJohn Marino /* Prevent recursive calls. */ 892cf7f2e2dSJohn Marino if (current->searched) 893cf7f2e2dSJohn Marino continue; 894cf7f2e2dSJohn Marino 895*c50c785cSJohn Marino /* If this is a namespace alias or imported declaration ignore 896*c50c785cSJohn Marino it. */ 897cf7f2e2dSJohn Marino if (current->alias != NULL || current->declaration != NULL) 898cf7f2e2dSJohn Marino continue; 899cf7f2e2dSJohn Marino 9005796c8dcSSimon Schubert if (strcmp (namespace, current->import_dest) == 0) 9015796c8dcSSimon Schubert { 902*c50c785cSJohn Marino /* Mark this import as searched so that the recursive call 903*c50c785cSJohn Marino does not search it again. */ 904cf7f2e2dSJohn Marino struct cleanup *old_chain; 905cf7f2e2dSJohn Marino current->searched = 1; 906*c50c785cSJohn Marino old_chain = make_cleanup (reset_directive_searched, 907*c50c785cSJohn Marino current); 908cf7f2e2dSJohn Marino 909*c50c785cSJohn Marino make_symbol_overload_list_using (func_name, 910*c50c785cSJohn Marino current->import_src); 911cf7f2e2dSJohn Marino 912cf7f2e2dSJohn Marino current->searched = 0; 913cf7f2e2dSJohn Marino discard_cleanups (old_chain); 9145796c8dcSSimon Schubert } 9155796c8dcSSimon Schubert } 9165796c8dcSSimon Schubert 9175796c8dcSSimon Schubert /* Now, add names for this namespace. */ 918cf7f2e2dSJohn Marino make_symbol_overload_list_namespace (func_name, namespace); 9195796c8dcSSimon Schubert } 9205796c8dcSSimon Schubert 9215796c8dcSSimon Schubert /* This does the bulk of the work of finding overloaded symbols. 9225796c8dcSSimon Schubert FUNC_NAME is the name of the overloaded function we're looking for 9235796c8dcSSimon Schubert (possibly including namespace info). */ 9245796c8dcSSimon Schubert 9255796c8dcSSimon Schubert static void 9265796c8dcSSimon Schubert make_symbol_overload_list_qualified (const char *func_name) 9275796c8dcSSimon Schubert { 9285796c8dcSSimon Schubert struct symbol *sym; 9295796c8dcSSimon Schubert struct symtab *s; 9305796c8dcSSimon Schubert struct objfile *objfile; 9315796c8dcSSimon Schubert const struct block *b, *surrounding_static_block = 0; 9325796c8dcSSimon Schubert struct dict_iterator iter; 9335796c8dcSSimon Schubert const struct dictionary *dict; 9345796c8dcSSimon Schubert 935*c50c785cSJohn Marino /* Look through the partial symtabs for all symbols which begin by 936*c50c785cSJohn Marino matching FUNC_NAME. Make sure we read that symbol table in. */ 9375796c8dcSSimon Schubert 938cf7f2e2dSJohn Marino ALL_OBJFILES (objfile) 939cf7f2e2dSJohn Marino { 940cf7f2e2dSJohn Marino if (objfile->sf) 941cf7f2e2dSJohn Marino objfile->sf->qf->expand_symtabs_for_function (objfile, func_name); 942cf7f2e2dSJohn Marino } 9435796c8dcSSimon Schubert 9445796c8dcSSimon Schubert /* Search upwards from currently selected frame (so that we can 9455796c8dcSSimon Schubert complete on local vars. */ 9465796c8dcSSimon Schubert 9475796c8dcSSimon Schubert for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b)) 948cf7f2e2dSJohn Marino make_symbol_overload_list_block (func_name, b); 9495796c8dcSSimon Schubert 9505796c8dcSSimon Schubert surrounding_static_block = block_static_block (get_selected_block (0)); 9515796c8dcSSimon Schubert 9525796c8dcSSimon Schubert /* Go through the symtabs and check the externs and statics for 9535796c8dcSSimon Schubert symbols which match. */ 9545796c8dcSSimon Schubert 9555796c8dcSSimon Schubert ALL_PRIMARY_SYMTABS (objfile, s) 9565796c8dcSSimon Schubert { 9575796c8dcSSimon Schubert QUIT; 9585796c8dcSSimon Schubert b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK); 959cf7f2e2dSJohn Marino make_symbol_overload_list_block (func_name, b); 9605796c8dcSSimon Schubert } 9615796c8dcSSimon Schubert 9625796c8dcSSimon Schubert ALL_PRIMARY_SYMTABS (objfile, s) 9635796c8dcSSimon Schubert { 9645796c8dcSSimon Schubert QUIT; 9655796c8dcSSimon Schubert b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK); 9665796c8dcSSimon Schubert /* Don't do this block twice. */ 9675796c8dcSSimon Schubert if (b == surrounding_static_block) 9685796c8dcSSimon Schubert continue; 969cf7f2e2dSJohn Marino make_symbol_overload_list_block (func_name, b); 9705796c8dcSSimon Schubert } 9715796c8dcSSimon Schubert } 9725796c8dcSSimon Schubert 9735796c8dcSSimon Schubert /* Lookup the rtti type for a class name. */ 9745796c8dcSSimon Schubert 9755796c8dcSSimon Schubert struct type * 9765796c8dcSSimon Schubert cp_lookup_rtti_type (const char *name, struct block *block) 9775796c8dcSSimon Schubert { 9785796c8dcSSimon Schubert struct symbol * rtti_sym; 9795796c8dcSSimon Schubert struct type * rtti_type; 9805796c8dcSSimon Schubert 9815796c8dcSSimon Schubert rtti_sym = lookup_symbol (name, block, STRUCT_DOMAIN, NULL); 9825796c8dcSSimon Schubert 9835796c8dcSSimon Schubert if (rtti_sym == NULL) 9845796c8dcSSimon Schubert { 9855796c8dcSSimon Schubert warning (_("RTTI symbol not found for class '%s'"), name); 9865796c8dcSSimon Schubert return NULL; 9875796c8dcSSimon Schubert } 9885796c8dcSSimon Schubert 9895796c8dcSSimon Schubert if (SYMBOL_CLASS (rtti_sym) != LOC_TYPEDEF) 9905796c8dcSSimon Schubert { 9915796c8dcSSimon Schubert warning (_("RTTI symbol for class '%s' is not a type"), name); 9925796c8dcSSimon Schubert return NULL; 9935796c8dcSSimon Schubert } 9945796c8dcSSimon Schubert 9955796c8dcSSimon Schubert rtti_type = SYMBOL_TYPE (rtti_sym); 9965796c8dcSSimon Schubert 9975796c8dcSSimon Schubert switch (TYPE_CODE (rtti_type)) 9985796c8dcSSimon Schubert { 9995796c8dcSSimon Schubert case TYPE_CODE_CLASS: 10005796c8dcSSimon Schubert break; 10015796c8dcSSimon Schubert case TYPE_CODE_NAMESPACE: 10025796c8dcSSimon Schubert /* chastain/2003-11-26: the symbol tables often contain fake 10035796c8dcSSimon Schubert symbols for namespaces with the same name as the struct. 10045796c8dcSSimon Schubert This warning is an indication of a bug in the lookup order 10055796c8dcSSimon Schubert or a bug in the way that the symbol tables are populated. */ 10065796c8dcSSimon Schubert warning (_("RTTI symbol for class '%s' is a namespace"), name); 10075796c8dcSSimon Schubert return NULL; 10085796c8dcSSimon Schubert default: 10095796c8dcSSimon Schubert warning (_("RTTI symbol for class '%s' has bad type"), name); 10105796c8dcSSimon Schubert return NULL; 10115796c8dcSSimon Schubert } 10125796c8dcSSimon Schubert 10135796c8dcSSimon Schubert return rtti_type; 10145796c8dcSSimon Schubert } 10155796c8dcSSimon Schubert 10165796c8dcSSimon Schubert /* Don't allow just "maintenance cplus". */ 10175796c8dcSSimon Schubert 10185796c8dcSSimon Schubert static void 10195796c8dcSSimon Schubert maint_cplus_command (char *arg, int from_tty) 10205796c8dcSSimon Schubert { 1021*c50c785cSJohn Marino printf_unfiltered (_("\"maintenance cplus\" must be followed " 1022*c50c785cSJohn Marino "by the name of a command.\n")); 1023*c50c785cSJohn Marino help_list (maint_cplus_cmd_list, 1024*c50c785cSJohn Marino "maintenance cplus ", 1025*c50c785cSJohn Marino -1, gdb_stdout); 10265796c8dcSSimon Schubert } 10275796c8dcSSimon Schubert 10285796c8dcSSimon Schubert /* This is a front end for cp_find_first_component, for unit testing. 10295796c8dcSSimon Schubert Be careful when using it: see the NOTE above 10305796c8dcSSimon Schubert cp_find_first_component. */ 10315796c8dcSSimon Schubert 10325796c8dcSSimon Schubert static void 10335796c8dcSSimon Schubert first_component_command (char *arg, int from_tty) 10345796c8dcSSimon Schubert { 10355796c8dcSSimon Schubert int len; 10365796c8dcSSimon Schubert char *prefix; 10375796c8dcSSimon Schubert 10385796c8dcSSimon Schubert if (!arg) 10395796c8dcSSimon Schubert return; 10405796c8dcSSimon Schubert 10415796c8dcSSimon Schubert len = cp_find_first_component (arg); 10425796c8dcSSimon Schubert prefix = alloca (len + 1); 10435796c8dcSSimon Schubert 10445796c8dcSSimon Schubert memcpy (prefix, arg, len); 10455796c8dcSSimon Schubert prefix[len] = '\0'; 10465796c8dcSSimon Schubert 10475796c8dcSSimon Schubert printf_unfiltered ("%s\n", prefix); 10485796c8dcSSimon Schubert } 10495796c8dcSSimon Schubert 10505796c8dcSSimon Schubert extern initialize_file_ftype _initialize_cp_support; /* -Wmissing-prototypes */ 10515796c8dcSSimon Schubert 1052cf7f2e2dSJohn Marino #define SKIP_SPACE(P) \ 1053cf7f2e2dSJohn Marino do \ 1054cf7f2e2dSJohn Marino { \ 1055cf7f2e2dSJohn Marino while (*(P) == ' ' || *(P) == '\t') \ 1056cf7f2e2dSJohn Marino ++(P); \ 1057cf7f2e2dSJohn Marino } \ 1058cf7f2e2dSJohn Marino while (0) 1059cf7f2e2dSJohn Marino 1060cf7f2e2dSJohn Marino /* Returns the length of the operator name or 0 if INPUT does not 1061*c50c785cSJohn Marino point to a valid C++ operator. INPUT should start with 1062*c50c785cSJohn Marino "operator". */ 1063cf7f2e2dSJohn Marino int 1064cf7f2e2dSJohn Marino cp_validate_operator (const char *input) 1065cf7f2e2dSJohn Marino { 1066cf7f2e2dSJohn Marino int i; 1067cf7f2e2dSJohn Marino char *copy; 1068cf7f2e2dSJohn Marino const char *p; 1069cf7f2e2dSJohn Marino struct expression *expr; 1070cf7f2e2dSJohn Marino struct value *val; 1071cf7f2e2dSJohn Marino struct gdb_exception except; 1072cf7f2e2dSJohn Marino 1073cf7f2e2dSJohn Marino p = input; 1074cf7f2e2dSJohn Marino 1075cf7f2e2dSJohn Marino if (strncmp (p, "operator", 8) == 0) 1076cf7f2e2dSJohn Marino { 1077cf7f2e2dSJohn Marino int valid = 0; 1078cf7f2e2dSJohn Marino 1079cf7f2e2dSJohn Marino p += 8; 1080cf7f2e2dSJohn Marino SKIP_SPACE (p); 1081*c50c785cSJohn Marino for (i = 0; 1082*c50c785cSJohn Marino i < sizeof (operator_tokens) / sizeof (operator_tokens[0]); 1083cf7f2e2dSJohn Marino ++i) 1084cf7f2e2dSJohn Marino { 1085cf7f2e2dSJohn Marino int length = strlen (operator_tokens[i]); 1086cf7f2e2dSJohn Marino 1087*c50c785cSJohn Marino /* By using strncmp here, we MUST have operator_tokens 1088*c50c785cSJohn Marino ordered! See additional notes where operator_tokens is 1089*c50c785cSJohn Marino defined above. */ 1090cf7f2e2dSJohn Marino if (strncmp (p, operator_tokens[i], length) == 0) 1091cf7f2e2dSJohn Marino { 1092cf7f2e2dSJohn Marino const char *op = p; 1093cf7f2e2dSJohn Marino 1094cf7f2e2dSJohn Marino valid = 1; 1095cf7f2e2dSJohn Marino p += length; 1096cf7f2e2dSJohn Marino 1097cf7f2e2dSJohn Marino if (strncmp (op, "new", 3) == 0 1098cf7f2e2dSJohn Marino || strncmp (op, "delete", 6) == 0) 1099cf7f2e2dSJohn Marino { 1100cf7f2e2dSJohn Marino 1101*c50c785cSJohn Marino /* Special case: new[] and delete[]. We must be 1102*c50c785cSJohn Marino careful to swallow whitespace before/in "[]". */ 1103cf7f2e2dSJohn Marino SKIP_SPACE (p); 1104cf7f2e2dSJohn Marino 1105cf7f2e2dSJohn Marino if (*p == '[') 1106cf7f2e2dSJohn Marino { 1107cf7f2e2dSJohn Marino ++p; 1108cf7f2e2dSJohn Marino SKIP_SPACE (p); 1109cf7f2e2dSJohn Marino if (*p == ']') 1110cf7f2e2dSJohn Marino ++p; 1111cf7f2e2dSJohn Marino else 1112cf7f2e2dSJohn Marino valid = 0; 1113cf7f2e2dSJohn Marino } 1114cf7f2e2dSJohn Marino } 1115cf7f2e2dSJohn Marino 1116cf7f2e2dSJohn Marino if (valid) 1117cf7f2e2dSJohn Marino return (p - input); 1118cf7f2e2dSJohn Marino } 1119cf7f2e2dSJohn Marino } 1120cf7f2e2dSJohn Marino 1121cf7f2e2dSJohn Marino /* Check input for a conversion operator. */ 1122cf7f2e2dSJohn Marino 1123*c50c785cSJohn Marino /* Skip past base typename. */ 1124cf7f2e2dSJohn Marino while (*p != '*' && *p != '&' && *p != 0 && *p != ' ') 1125cf7f2e2dSJohn Marino ++p; 1126cf7f2e2dSJohn Marino SKIP_SPACE (p); 1127cf7f2e2dSJohn Marino 1128*c50c785cSJohn Marino /* Add modifiers '*' / '&'. */ 1129cf7f2e2dSJohn Marino while (*p == '*' || *p == '&') 1130cf7f2e2dSJohn Marino { 1131cf7f2e2dSJohn Marino ++p; 1132cf7f2e2dSJohn Marino SKIP_SPACE (p); 1133cf7f2e2dSJohn Marino } 1134cf7f2e2dSJohn Marino 1135cf7f2e2dSJohn Marino /* Check for valid type. [Remember: input starts with 1136cf7f2e2dSJohn Marino "operator".] */ 1137cf7f2e2dSJohn Marino copy = savestring (input + 8, p - input - 8); 1138cf7f2e2dSJohn Marino expr = NULL; 1139cf7f2e2dSJohn Marino val = NULL; 1140cf7f2e2dSJohn Marino TRY_CATCH (except, RETURN_MASK_ALL) 1141cf7f2e2dSJohn Marino { 1142cf7f2e2dSJohn Marino expr = parse_expression (copy); 1143cf7f2e2dSJohn Marino val = evaluate_type (expr); 1144cf7f2e2dSJohn Marino } 1145cf7f2e2dSJohn Marino 1146cf7f2e2dSJohn Marino xfree (copy); 1147cf7f2e2dSJohn Marino if (expr) 1148cf7f2e2dSJohn Marino xfree (expr); 1149cf7f2e2dSJohn Marino 1150cf7f2e2dSJohn Marino if (val != NULL && value_type (val) != NULL) 1151cf7f2e2dSJohn Marino return (p - input); 1152cf7f2e2dSJohn Marino } 1153cf7f2e2dSJohn Marino 1154cf7f2e2dSJohn Marino return 0; 1155cf7f2e2dSJohn Marino } 1156cf7f2e2dSJohn Marino 11575796c8dcSSimon Schubert void 11585796c8dcSSimon Schubert _initialize_cp_support (void) 11595796c8dcSSimon Schubert { 1160*c50c785cSJohn Marino add_prefix_cmd ("cplus", class_maintenance, 1161*c50c785cSJohn Marino maint_cplus_command, 1162*c50c785cSJohn Marino _("C++ maintenance commands."), 1163*c50c785cSJohn Marino &maint_cplus_cmd_list, 1164*c50c785cSJohn Marino "maintenance cplus ", 1165*c50c785cSJohn Marino 0, &maintenancelist); 1166*c50c785cSJohn Marino add_alias_cmd ("cp", "cplus", 1167*c50c785cSJohn Marino class_maintenance, 1, 1168*c50c785cSJohn Marino &maintenancelist); 11695796c8dcSSimon Schubert 1170*c50c785cSJohn Marino add_cmd ("first_component", 1171*c50c785cSJohn Marino class_maintenance, 1172*c50c785cSJohn Marino first_component_command, 11735796c8dcSSimon Schubert _("Print the first class/namespace component of NAME."), 11745796c8dcSSimon Schubert &maint_cplus_cmd_list); 11755796c8dcSSimon Schubert } 1176