15796c8dcSSimon Schubert /* Helper routines for C++ support in GDB. 2*cf7f2e2dSJohn Marino Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010 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" 35*cf7f2e2dSJohn Marino #include "exceptions.h" 36*cf7f2e2dSJohn Marino #include "expression.h" 37*cf7f2e2dSJohn Marino #include "value.h" 385796c8dcSSimon Schubert 395796c8dcSSimon Schubert #include "safe-ctype.h" 405796c8dcSSimon Schubert 41*cf7f2e2dSJohn Marino #include "psymtab.h" 42*cf7f2e2dSJohn 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 55*cf7f2e2dSJohn 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 76*cf7f2e2dSJohn Marino /* Operator validation. 77*cf7f2e2dSJohn Marino NOTE: Multi-byte operators (usually the assignment variety operator) 78*cf7f2e2dSJohn Marino must appear before the single byte version, i.e., "+=" before "+". */ 79*cf7f2e2dSJohn Marino static const char *operator_tokens[] = 80*cf7f2e2dSJohn Marino { 81*cf7f2e2dSJohn Marino "++", "+=", "+", "->*", "->", "--", "-=", "-", "*=", "*", "/=", "/", 82*cf7f2e2dSJohn Marino "%=", "%", "!=", "==", "!", "&&", "<<=", "<<", ">>=", ">>", 83*cf7f2e2dSJohn Marino "<=", "<", ">=", ">", "~", "&=", "&", "|=", "||", "|", "^=", "^", 84*cf7f2e2dSJohn Marino "=", "()", "[]", ",", "new", "delete" 85*cf7f2e2dSJohn Marino /* new[] and delete[] require special whitespace handling */ 86*cf7f2e2dSJohn Marino }; 87*cf7f2e2dSJohn Marino 885796c8dcSSimon Schubert /* Return 1 if STRING is clearly already in canonical form. This 895796c8dcSSimon Schubert function is conservative; things which it does not recognize are 905796c8dcSSimon Schubert assumed to be non-canonical, and the parser will sort them out 915796c8dcSSimon Schubert afterwards. This speeds up the critical path for alphanumeric 925796c8dcSSimon Schubert identifiers. */ 935796c8dcSSimon Schubert 945796c8dcSSimon Schubert static int 955796c8dcSSimon Schubert cp_already_canonical (const char *string) 965796c8dcSSimon Schubert { 975796c8dcSSimon Schubert /* Identifier start character [a-zA-Z_]. */ 985796c8dcSSimon Schubert if (!ISIDST (string[0])) 995796c8dcSSimon Schubert return 0; 1005796c8dcSSimon Schubert 1015796c8dcSSimon Schubert /* These are the only two identifiers which canonicalize to other 1025796c8dcSSimon Schubert than themselves or an error: unsigned -> unsigned int and 1035796c8dcSSimon Schubert signed -> int. */ 1045796c8dcSSimon Schubert if (string[0] == 'u' && strcmp (&string[1], "nsigned") == 0) 1055796c8dcSSimon Schubert return 0; 1065796c8dcSSimon Schubert else if (string[0] == 's' && strcmp (&string[1], "igned") == 0) 1075796c8dcSSimon Schubert return 0; 1085796c8dcSSimon Schubert 1095796c8dcSSimon Schubert /* Identifier character [a-zA-Z0-9_]. */ 1105796c8dcSSimon Schubert while (ISIDNUM (string[1])) 1115796c8dcSSimon Schubert string++; 1125796c8dcSSimon Schubert 1135796c8dcSSimon Schubert if (string[1] == '\0') 1145796c8dcSSimon Schubert return 1; 1155796c8dcSSimon Schubert else 1165796c8dcSSimon Schubert return 0; 1175796c8dcSSimon Schubert } 1185796c8dcSSimon Schubert 1195796c8dcSSimon Schubert /* Parse STRING and convert it to canonical form. If parsing fails, 1205796c8dcSSimon Schubert or if STRING is already canonical, return NULL. Otherwise return 1215796c8dcSSimon Schubert the canonical form. The return value is allocated via xmalloc. */ 1225796c8dcSSimon Schubert 1235796c8dcSSimon Schubert char * 1245796c8dcSSimon Schubert cp_canonicalize_string (const char *string) 1255796c8dcSSimon Schubert { 1265796c8dcSSimon Schubert struct demangle_component *ret_comp; 1275796c8dcSSimon Schubert unsigned int estimated_len; 1285796c8dcSSimon Schubert char *ret; 1295796c8dcSSimon Schubert 1305796c8dcSSimon Schubert if (cp_already_canonical (string)) 1315796c8dcSSimon Schubert return NULL; 1325796c8dcSSimon Schubert 1335796c8dcSSimon Schubert ret_comp = cp_demangled_name_to_comp (string, NULL); 1345796c8dcSSimon Schubert if (ret_comp == NULL) 1355796c8dcSSimon Schubert return NULL; 1365796c8dcSSimon Schubert 1375796c8dcSSimon Schubert estimated_len = strlen (string) * 2; 1385796c8dcSSimon Schubert ret = cp_comp_to_string (ret_comp, estimated_len); 1395796c8dcSSimon Schubert 1405796c8dcSSimon Schubert if (strcmp (string, ret) == 0) 1415796c8dcSSimon Schubert { 1425796c8dcSSimon Schubert xfree (ret); 1435796c8dcSSimon Schubert return NULL; 1445796c8dcSSimon Schubert } 1455796c8dcSSimon Schubert 1465796c8dcSSimon Schubert return ret; 1475796c8dcSSimon Schubert } 1485796c8dcSSimon Schubert 1495796c8dcSSimon Schubert /* Convert a mangled name to a demangle_component tree. *MEMORY is set to the 1505796c8dcSSimon Schubert block of used memory that should be freed when finished with the tree. 1515796c8dcSSimon Schubert DEMANGLED_P is set to the char * that should be freed when finished with 1525796c8dcSSimon Schubert the tree, or NULL if none was needed. OPTIONS will be passed to the 1535796c8dcSSimon Schubert demangler. */ 1545796c8dcSSimon Schubert 1555796c8dcSSimon Schubert static struct demangle_component * 1565796c8dcSSimon Schubert mangled_name_to_comp (const char *mangled_name, int options, 1575796c8dcSSimon Schubert void **memory, char **demangled_p) 1585796c8dcSSimon Schubert { 1595796c8dcSSimon Schubert struct demangle_component *ret; 1605796c8dcSSimon Schubert char *demangled_name; 1615796c8dcSSimon Schubert 1625796c8dcSSimon Schubert /* If it looks like a v3 mangled name, then try to go directly 1635796c8dcSSimon Schubert to trees. */ 1645796c8dcSSimon Schubert if (mangled_name[0] == '_' && mangled_name[1] == 'Z') 1655796c8dcSSimon Schubert { 1665796c8dcSSimon Schubert ret = cplus_demangle_v3_components (mangled_name, options, memory); 1675796c8dcSSimon Schubert if (ret) 1685796c8dcSSimon Schubert { 1695796c8dcSSimon Schubert *demangled_p = NULL; 1705796c8dcSSimon Schubert return ret; 1715796c8dcSSimon Schubert } 1725796c8dcSSimon Schubert } 1735796c8dcSSimon Schubert 1745796c8dcSSimon Schubert /* If it doesn't, or if that failed, then try to demangle the name. */ 1755796c8dcSSimon Schubert demangled_name = cplus_demangle (mangled_name, options); 1765796c8dcSSimon Schubert if (demangled_name == NULL) 1775796c8dcSSimon Schubert return NULL; 1785796c8dcSSimon Schubert 1795796c8dcSSimon Schubert /* If we could demangle the name, parse it to build the component tree. */ 1805796c8dcSSimon Schubert ret = cp_demangled_name_to_comp (demangled_name, NULL); 1815796c8dcSSimon Schubert 1825796c8dcSSimon Schubert if (ret == NULL) 1835796c8dcSSimon Schubert { 1845796c8dcSSimon Schubert xfree (demangled_name); 1855796c8dcSSimon Schubert return NULL; 1865796c8dcSSimon Schubert } 1875796c8dcSSimon Schubert 1885796c8dcSSimon Schubert *demangled_p = demangled_name; 1895796c8dcSSimon Schubert return ret; 1905796c8dcSSimon Schubert } 1915796c8dcSSimon Schubert 1925796c8dcSSimon Schubert /* Return the name of the class containing method PHYSNAME. */ 1935796c8dcSSimon Schubert 1945796c8dcSSimon Schubert char * 1955796c8dcSSimon Schubert cp_class_name_from_physname (const char *physname) 1965796c8dcSSimon Schubert { 1975796c8dcSSimon Schubert void *storage = NULL; 1985796c8dcSSimon Schubert char *demangled_name = NULL, *ret; 1995796c8dcSSimon Schubert struct demangle_component *ret_comp, *prev_comp, *cur_comp; 2005796c8dcSSimon Schubert int done; 2015796c8dcSSimon Schubert 2025796c8dcSSimon Schubert ret_comp = mangled_name_to_comp (physname, DMGL_ANSI, &storage, 2035796c8dcSSimon Schubert &demangled_name); 2045796c8dcSSimon Schubert if (ret_comp == NULL) 2055796c8dcSSimon Schubert return NULL; 2065796c8dcSSimon Schubert 2075796c8dcSSimon Schubert done = 0; 2085796c8dcSSimon Schubert 2095796c8dcSSimon Schubert /* First strip off any qualifiers, if we have a function or method. */ 2105796c8dcSSimon Schubert while (!done) 2115796c8dcSSimon Schubert switch (ret_comp->type) 2125796c8dcSSimon Schubert { 2135796c8dcSSimon Schubert case DEMANGLE_COMPONENT_CONST: 2145796c8dcSSimon Schubert case DEMANGLE_COMPONENT_RESTRICT: 2155796c8dcSSimon Schubert case DEMANGLE_COMPONENT_VOLATILE: 2165796c8dcSSimon Schubert case DEMANGLE_COMPONENT_CONST_THIS: 2175796c8dcSSimon Schubert case DEMANGLE_COMPONENT_RESTRICT_THIS: 2185796c8dcSSimon Schubert case DEMANGLE_COMPONENT_VOLATILE_THIS: 2195796c8dcSSimon Schubert case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: 2205796c8dcSSimon Schubert ret_comp = d_left (ret_comp); 2215796c8dcSSimon Schubert break; 2225796c8dcSSimon Schubert default: 2235796c8dcSSimon Schubert done = 1; 2245796c8dcSSimon Schubert break; 2255796c8dcSSimon Schubert } 2265796c8dcSSimon Schubert 2275796c8dcSSimon Schubert /* If what we have now is a function, discard the argument list. */ 2285796c8dcSSimon Schubert if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME) 2295796c8dcSSimon Schubert ret_comp = d_left (ret_comp); 2305796c8dcSSimon Schubert 2315796c8dcSSimon Schubert /* If what we have now is a template, strip off the template 2325796c8dcSSimon Schubert arguments. The left subtree may be a qualified name. */ 2335796c8dcSSimon Schubert if (ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE) 2345796c8dcSSimon Schubert ret_comp = d_left (ret_comp); 2355796c8dcSSimon Schubert 2365796c8dcSSimon Schubert /* What we have now should be a name, possibly qualified. Additional 2375796c8dcSSimon Schubert qualifiers could live in the left subtree or the right subtree. Find 2385796c8dcSSimon Schubert the last piece. */ 2395796c8dcSSimon Schubert done = 0; 2405796c8dcSSimon Schubert prev_comp = NULL; 2415796c8dcSSimon Schubert cur_comp = ret_comp; 2425796c8dcSSimon Schubert while (!done) 2435796c8dcSSimon Schubert switch (cur_comp->type) 2445796c8dcSSimon Schubert { 2455796c8dcSSimon Schubert case DEMANGLE_COMPONENT_QUAL_NAME: 2465796c8dcSSimon Schubert case DEMANGLE_COMPONENT_LOCAL_NAME: 2475796c8dcSSimon Schubert prev_comp = cur_comp; 2485796c8dcSSimon Schubert cur_comp = d_right (cur_comp); 2495796c8dcSSimon Schubert break; 2505796c8dcSSimon Schubert case DEMANGLE_COMPONENT_TEMPLATE: 2515796c8dcSSimon Schubert case DEMANGLE_COMPONENT_NAME: 2525796c8dcSSimon Schubert case DEMANGLE_COMPONENT_CTOR: 2535796c8dcSSimon Schubert case DEMANGLE_COMPONENT_DTOR: 2545796c8dcSSimon Schubert case DEMANGLE_COMPONENT_OPERATOR: 2555796c8dcSSimon Schubert case DEMANGLE_COMPONENT_EXTENDED_OPERATOR: 2565796c8dcSSimon Schubert done = 1; 2575796c8dcSSimon Schubert break; 2585796c8dcSSimon Schubert default: 2595796c8dcSSimon Schubert done = 1; 2605796c8dcSSimon Schubert cur_comp = NULL; 2615796c8dcSSimon Schubert break; 2625796c8dcSSimon Schubert } 2635796c8dcSSimon Schubert 2645796c8dcSSimon Schubert ret = NULL; 2655796c8dcSSimon Schubert if (cur_comp != NULL && prev_comp != NULL) 2665796c8dcSSimon Schubert { 2675796c8dcSSimon Schubert /* We want to discard the rightmost child of PREV_COMP. */ 2685796c8dcSSimon Schubert *prev_comp = *d_left (prev_comp); 2695796c8dcSSimon Schubert /* The ten is completely arbitrary; we don't have a good estimate. */ 2705796c8dcSSimon Schubert ret = cp_comp_to_string (ret_comp, 10); 2715796c8dcSSimon Schubert } 2725796c8dcSSimon Schubert 2735796c8dcSSimon Schubert xfree (storage); 2745796c8dcSSimon Schubert if (demangled_name) 2755796c8dcSSimon Schubert xfree (demangled_name); 2765796c8dcSSimon Schubert return ret; 2775796c8dcSSimon Schubert } 2785796c8dcSSimon Schubert 2795796c8dcSSimon Schubert /* Return the child of COMP which is the basename of a method, variable, 2805796c8dcSSimon Schubert et cetera. All scope qualifiers are discarded, but template arguments 2815796c8dcSSimon Schubert will be included. The component tree may be modified. */ 2825796c8dcSSimon Schubert 2835796c8dcSSimon Schubert static struct demangle_component * 2845796c8dcSSimon Schubert unqualified_name_from_comp (struct demangle_component *comp) 2855796c8dcSSimon Schubert { 2865796c8dcSSimon Schubert struct demangle_component *ret_comp = comp, *last_template; 2875796c8dcSSimon Schubert int done; 2885796c8dcSSimon Schubert 2895796c8dcSSimon Schubert done = 0; 2905796c8dcSSimon Schubert last_template = NULL; 2915796c8dcSSimon Schubert while (!done) 2925796c8dcSSimon Schubert switch (ret_comp->type) 2935796c8dcSSimon Schubert { 2945796c8dcSSimon Schubert case DEMANGLE_COMPONENT_QUAL_NAME: 2955796c8dcSSimon Schubert case DEMANGLE_COMPONENT_LOCAL_NAME: 2965796c8dcSSimon Schubert ret_comp = d_right (ret_comp); 2975796c8dcSSimon Schubert break; 2985796c8dcSSimon Schubert case DEMANGLE_COMPONENT_TYPED_NAME: 2995796c8dcSSimon Schubert ret_comp = d_left (ret_comp); 3005796c8dcSSimon Schubert break; 3015796c8dcSSimon Schubert case DEMANGLE_COMPONENT_TEMPLATE: 3025796c8dcSSimon Schubert gdb_assert (last_template == NULL); 3035796c8dcSSimon Schubert last_template = ret_comp; 3045796c8dcSSimon Schubert ret_comp = d_left (ret_comp); 3055796c8dcSSimon Schubert break; 3065796c8dcSSimon Schubert case DEMANGLE_COMPONENT_CONST: 3075796c8dcSSimon Schubert case DEMANGLE_COMPONENT_RESTRICT: 3085796c8dcSSimon Schubert case DEMANGLE_COMPONENT_VOLATILE: 3095796c8dcSSimon Schubert case DEMANGLE_COMPONENT_CONST_THIS: 3105796c8dcSSimon Schubert case DEMANGLE_COMPONENT_RESTRICT_THIS: 3115796c8dcSSimon Schubert case DEMANGLE_COMPONENT_VOLATILE_THIS: 3125796c8dcSSimon Schubert case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: 3135796c8dcSSimon Schubert ret_comp = d_left (ret_comp); 3145796c8dcSSimon Schubert break; 3155796c8dcSSimon Schubert case DEMANGLE_COMPONENT_NAME: 3165796c8dcSSimon Schubert case DEMANGLE_COMPONENT_CTOR: 3175796c8dcSSimon Schubert case DEMANGLE_COMPONENT_DTOR: 3185796c8dcSSimon Schubert case DEMANGLE_COMPONENT_OPERATOR: 3195796c8dcSSimon Schubert case DEMANGLE_COMPONENT_EXTENDED_OPERATOR: 3205796c8dcSSimon Schubert done = 1; 3215796c8dcSSimon Schubert break; 3225796c8dcSSimon Schubert default: 3235796c8dcSSimon Schubert return NULL; 3245796c8dcSSimon Schubert break; 3255796c8dcSSimon Schubert } 3265796c8dcSSimon Schubert 3275796c8dcSSimon Schubert if (last_template) 3285796c8dcSSimon Schubert { 3295796c8dcSSimon Schubert d_left (last_template) = ret_comp; 3305796c8dcSSimon Schubert return last_template; 3315796c8dcSSimon Schubert } 3325796c8dcSSimon Schubert 3335796c8dcSSimon Schubert return ret_comp; 3345796c8dcSSimon Schubert } 3355796c8dcSSimon Schubert 3365796c8dcSSimon Schubert /* Return the name of the method whose linkage name is PHYSNAME. */ 3375796c8dcSSimon Schubert 3385796c8dcSSimon Schubert char * 3395796c8dcSSimon Schubert method_name_from_physname (const char *physname) 3405796c8dcSSimon Schubert { 3415796c8dcSSimon Schubert void *storage = NULL; 3425796c8dcSSimon Schubert char *demangled_name = NULL, *ret; 3435796c8dcSSimon Schubert struct demangle_component *ret_comp; 3445796c8dcSSimon Schubert 3455796c8dcSSimon Schubert ret_comp = mangled_name_to_comp (physname, DMGL_ANSI, &storage, 3465796c8dcSSimon Schubert &demangled_name); 3475796c8dcSSimon Schubert if (ret_comp == NULL) 3485796c8dcSSimon Schubert return NULL; 3495796c8dcSSimon Schubert 3505796c8dcSSimon Schubert ret_comp = unqualified_name_from_comp (ret_comp); 3515796c8dcSSimon Schubert 3525796c8dcSSimon Schubert ret = NULL; 3535796c8dcSSimon Schubert if (ret_comp != NULL) 3545796c8dcSSimon Schubert /* The ten is completely arbitrary; we don't have a good estimate. */ 3555796c8dcSSimon Schubert ret = cp_comp_to_string (ret_comp, 10); 3565796c8dcSSimon Schubert 3575796c8dcSSimon Schubert xfree (storage); 3585796c8dcSSimon Schubert if (demangled_name) 3595796c8dcSSimon Schubert xfree (demangled_name); 3605796c8dcSSimon Schubert return ret; 3615796c8dcSSimon Schubert } 3625796c8dcSSimon Schubert 3635796c8dcSSimon Schubert /* If FULL_NAME is the demangled name of a C++ function (including an 3645796c8dcSSimon Schubert arg list, possibly including namespace/class qualifications), 3655796c8dcSSimon Schubert return a new string containing only the function name (without the 3665796c8dcSSimon Schubert arg list/class qualifications). Otherwise, return NULL. The 3675796c8dcSSimon Schubert caller is responsible for freeing the memory in question. */ 3685796c8dcSSimon Schubert 3695796c8dcSSimon Schubert char * 3705796c8dcSSimon Schubert cp_func_name (const char *full_name) 3715796c8dcSSimon Schubert { 3725796c8dcSSimon Schubert char *ret; 3735796c8dcSSimon Schubert struct demangle_component *ret_comp; 3745796c8dcSSimon Schubert 3755796c8dcSSimon Schubert ret_comp = cp_demangled_name_to_comp (full_name, NULL); 3765796c8dcSSimon Schubert if (!ret_comp) 3775796c8dcSSimon Schubert return NULL; 3785796c8dcSSimon Schubert 3795796c8dcSSimon Schubert ret_comp = unqualified_name_from_comp (ret_comp); 3805796c8dcSSimon Schubert 3815796c8dcSSimon Schubert ret = NULL; 3825796c8dcSSimon Schubert if (ret_comp != NULL) 3835796c8dcSSimon Schubert ret = cp_comp_to_string (ret_comp, 10); 3845796c8dcSSimon Schubert 3855796c8dcSSimon Schubert return ret; 3865796c8dcSSimon Schubert } 3875796c8dcSSimon Schubert 3885796c8dcSSimon Schubert /* DEMANGLED_NAME is the name of a function, including parameters and 3895796c8dcSSimon Schubert (optionally) a return type. Return the name of the function without 3905796c8dcSSimon Schubert parameters or return type, or NULL if we can not parse the name. */ 3915796c8dcSSimon Schubert 3925796c8dcSSimon Schubert char * 3935796c8dcSSimon Schubert cp_remove_params (const char *demangled_name) 3945796c8dcSSimon Schubert { 3955796c8dcSSimon Schubert int done = 0; 3965796c8dcSSimon Schubert struct demangle_component *ret_comp; 3975796c8dcSSimon Schubert char *ret = NULL; 3985796c8dcSSimon Schubert 3995796c8dcSSimon Schubert if (demangled_name == NULL) 4005796c8dcSSimon Schubert return NULL; 4015796c8dcSSimon Schubert 4025796c8dcSSimon Schubert ret_comp = cp_demangled_name_to_comp (demangled_name, NULL); 4035796c8dcSSimon Schubert if (ret_comp == NULL) 4045796c8dcSSimon Schubert return NULL; 4055796c8dcSSimon Schubert 4065796c8dcSSimon Schubert /* First strip off any qualifiers, if we have a function or method. */ 4075796c8dcSSimon Schubert while (!done) 4085796c8dcSSimon Schubert switch (ret_comp->type) 4095796c8dcSSimon Schubert { 4105796c8dcSSimon Schubert case DEMANGLE_COMPONENT_CONST: 4115796c8dcSSimon Schubert case DEMANGLE_COMPONENT_RESTRICT: 4125796c8dcSSimon Schubert case DEMANGLE_COMPONENT_VOLATILE: 4135796c8dcSSimon Schubert case DEMANGLE_COMPONENT_CONST_THIS: 4145796c8dcSSimon Schubert case DEMANGLE_COMPONENT_RESTRICT_THIS: 4155796c8dcSSimon Schubert case DEMANGLE_COMPONENT_VOLATILE_THIS: 4165796c8dcSSimon Schubert case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: 4175796c8dcSSimon Schubert ret_comp = d_left (ret_comp); 4185796c8dcSSimon Schubert break; 4195796c8dcSSimon Schubert default: 4205796c8dcSSimon Schubert done = 1; 4215796c8dcSSimon Schubert break; 4225796c8dcSSimon Schubert } 4235796c8dcSSimon Schubert 4245796c8dcSSimon Schubert /* What we have now should be a function. Return its name. */ 4255796c8dcSSimon Schubert if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME) 4265796c8dcSSimon Schubert ret = cp_comp_to_string (d_left (ret_comp), 10); 4275796c8dcSSimon Schubert 4285796c8dcSSimon Schubert return ret; 4295796c8dcSSimon Schubert } 4305796c8dcSSimon Schubert 4315796c8dcSSimon Schubert /* Here are some random pieces of trivia to keep in mind while trying 4325796c8dcSSimon Schubert to take apart demangled names: 4335796c8dcSSimon Schubert 4345796c8dcSSimon Schubert - Names can contain function arguments or templates, so the process 4355796c8dcSSimon Schubert has to be, to some extent recursive: maybe keep track of your 4365796c8dcSSimon Schubert depth based on encountering <> and (). 4375796c8dcSSimon Schubert 4385796c8dcSSimon Schubert - Parentheses don't just have to happen at the end of a name: they 4395796c8dcSSimon Schubert can occur even if the name in question isn't a function, because 4405796c8dcSSimon Schubert a template argument might be a type that's a function. 4415796c8dcSSimon Schubert 4425796c8dcSSimon Schubert - Conversely, even if you're trying to deal with a function, its 4435796c8dcSSimon Schubert demangled name might not end with ')': it could be a const or 4445796c8dcSSimon Schubert volatile class method, in which case it ends with "const" or 4455796c8dcSSimon Schubert "volatile". 4465796c8dcSSimon Schubert 4475796c8dcSSimon Schubert - Parentheses are also used in anonymous namespaces: a variable 4485796c8dcSSimon Schubert 'foo' in an anonymous namespace gets demangled as "(anonymous 4495796c8dcSSimon Schubert namespace)::foo". 4505796c8dcSSimon Schubert 4515796c8dcSSimon Schubert - And operator names can contain parentheses or angle brackets. */ 4525796c8dcSSimon Schubert 4535796c8dcSSimon Schubert /* FIXME: carlton/2003-03-13: We have several functions here with 4545796c8dcSSimon Schubert overlapping functionality; can we combine them? Also, do they 4555796c8dcSSimon Schubert handle all the above considerations correctly? */ 4565796c8dcSSimon Schubert 4575796c8dcSSimon Schubert 4585796c8dcSSimon Schubert /* This returns the length of first component of NAME, which should be 4595796c8dcSSimon Schubert the demangled name of a C++ variable/function/method/etc. 4605796c8dcSSimon Schubert Specifically, it returns the index of the first colon forming the 4615796c8dcSSimon Schubert boundary of the first component: so, given 'A::foo' or 'A::B::foo' 4625796c8dcSSimon Schubert it returns the 1, and given 'foo', it returns 0. */ 4635796c8dcSSimon Schubert 4645796c8dcSSimon Schubert /* The character in NAME indexed by the return value is guaranteed to 4655796c8dcSSimon Schubert always be either ':' or '\0'. */ 4665796c8dcSSimon Schubert 4675796c8dcSSimon Schubert /* NOTE: carlton/2003-03-13: This function is currently only intended 4685796c8dcSSimon Schubert for internal use: it's probably not entirely safe when called on 4695796c8dcSSimon Schubert user-generated input, because some of the 'index += 2' lines in 4705796c8dcSSimon Schubert cp_find_first_component_aux might go past the end of malformed 4715796c8dcSSimon Schubert input. */ 4725796c8dcSSimon Schubert 4735796c8dcSSimon Schubert unsigned int 4745796c8dcSSimon Schubert cp_find_first_component (const char *name) 4755796c8dcSSimon Schubert { 4765796c8dcSSimon Schubert return cp_find_first_component_aux (name, 0); 4775796c8dcSSimon Schubert } 4785796c8dcSSimon Schubert 4795796c8dcSSimon Schubert /* Helper function for cp_find_first_component. Like that function, 4805796c8dcSSimon Schubert it returns the length of the first component of NAME, but to make 4815796c8dcSSimon Schubert the recursion easier, it also stops if it reaches an unexpected ')' 4825796c8dcSSimon Schubert or '>' if the value of PERMISSIVE is nonzero. */ 4835796c8dcSSimon Schubert 4845796c8dcSSimon Schubert /* Let's optimize away calls to strlen("operator"). */ 4855796c8dcSSimon Schubert 4865796c8dcSSimon Schubert #define LENGTH_OF_OPERATOR 8 4875796c8dcSSimon Schubert 4885796c8dcSSimon Schubert static unsigned int 4895796c8dcSSimon Schubert cp_find_first_component_aux (const char *name, int permissive) 4905796c8dcSSimon Schubert { 4915796c8dcSSimon Schubert unsigned int index = 0; 4925796c8dcSSimon Schubert /* Operator names can show up in unexpected places. Since these can 4935796c8dcSSimon Schubert contain parentheses or angle brackets, they can screw up the 4945796c8dcSSimon Schubert recursion. But not every string 'operator' is part of an 4955796c8dcSSimon Schubert operater name: e.g. you could have a variable 'cooperator'. So 4965796c8dcSSimon Schubert this variable tells us whether or not we should treat the string 4975796c8dcSSimon Schubert 'operator' as starting an operator. */ 4985796c8dcSSimon Schubert int operator_possible = 1; 4995796c8dcSSimon Schubert 5005796c8dcSSimon Schubert for (;; ++index) 5015796c8dcSSimon Schubert { 5025796c8dcSSimon Schubert switch (name[index]) 5035796c8dcSSimon Schubert { 5045796c8dcSSimon Schubert case '<': 5055796c8dcSSimon Schubert /* Template; eat it up. The calls to cp_first_component 5065796c8dcSSimon Schubert should only return (I hope!) when they reach the '>' 5075796c8dcSSimon Schubert terminating the component or a '::' between two 5085796c8dcSSimon Schubert components. (Hence the '+ 2'.) */ 5095796c8dcSSimon Schubert index += 1; 5105796c8dcSSimon Schubert for (index += cp_find_first_component_aux (name + index, 1); 5115796c8dcSSimon Schubert name[index] != '>'; 5125796c8dcSSimon Schubert index += cp_find_first_component_aux (name + index, 1)) 5135796c8dcSSimon Schubert { 5145796c8dcSSimon Schubert if (name[index] != ':') 5155796c8dcSSimon Schubert { 5165796c8dcSSimon Schubert demangled_name_complaint (name); 5175796c8dcSSimon Schubert return strlen (name); 5185796c8dcSSimon Schubert } 5195796c8dcSSimon Schubert index += 2; 5205796c8dcSSimon Schubert } 5215796c8dcSSimon Schubert operator_possible = 1; 5225796c8dcSSimon Schubert break; 5235796c8dcSSimon Schubert case '(': 5245796c8dcSSimon Schubert /* Similar comment as to '<'. */ 5255796c8dcSSimon Schubert index += 1; 5265796c8dcSSimon Schubert for (index += cp_find_first_component_aux (name + index, 1); 5275796c8dcSSimon Schubert name[index] != ')'; 5285796c8dcSSimon Schubert index += cp_find_first_component_aux (name + index, 1)) 5295796c8dcSSimon Schubert { 5305796c8dcSSimon Schubert if (name[index] != ':') 5315796c8dcSSimon Schubert { 5325796c8dcSSimon Schubert demangled_name_complaint (name); 5335796c8dcSSimon Schubert return strlen (name); 5345796c8dcSSimon Schubert } 5355796c8dcSSimon Schubert index += 2; 5365796c8dcSSimon Schubert } 5375796c8dcSSimon Schubert operator_possible = 1; 5385796c8dcSSimon Schubert break; 5395796c8dcSSimon Schubert case '>': 5405796c8dcSSimon Schubert case ')': 5415796c8dcSSimon Schubert if (permissive) 5425796c8dcSSimon Schubert return index; 5435796c8dcSSimon Schubert else 5445796c8dcSSimon Schubert { 5455796c8dcSSimon Schubert demangled_name_complaint (name); 5465796c8dcSSimon Schubert return strlen (name); 5475796c8dcSSimon Schubert } 5485796c8dcSSimon Schubert case '\0': 5495796c8dcSSimon Schubert case ':': 5505796c8dcSSimon Schubert return index; 5515796c8dcSSimon Schubert case 'o': 5525796c8dcSSimon Schubert /* Operator names can screw up the recursion. */ 5535796c8dcSSimon Schubert if (operator_possible 5545796c8dcSSimon Schubert && strncmp (name + index, "operator", LENGTH_OF_OPERATOR) == 0) 5555796c8dcSSimon Schubert { 5565796c8dcSSimon Schubert index += LENGTH_OF_OPERATOR; 5575796c8dcSSimon Schubert while (ISSPACE(name[index])) 5585796c8dcSSimon Schubert ++index; 5595796c8dcSSimon Schubert switch (name[index]) 5605796c8dcSSimon Schubert { 5615796c8dcSSimon Schubert /* Skip over one less than the appropriate number of 5625796c8dcSSimon Schubert characters: the for loop will skip over the last 5635796c8dcSSimon Schubert one. */ 5645796c8dcSSimon Schubert case '<': 5655796c8dcSSimon Schubert if (name[index + 1] == '<') 5665796c8dcSSimon Schubert index += 1; 5675796c8dcSSimon Schubert else 5685796c8dcSSimon Schubert index += 0; 5695796c8dcSSimon Schubert break; 5705796c8dcSSimon Schubert case '>': 5715796c8dcSSimon Schubert case '-': 5725796c8dcSSimon Schubert if (name[index + 1] == '>') 5735796c8dcSSimon Schubert index += 1; 5745796c8dcSSimon Schubert else 5755796c8dcSSimon Schubert index += 0; 5765796c8dcSSimon Schubert break; 5775796c8dcSSimon Schubert case '(': 5785796c8dcSSimon Schubert index += 1; 5795796c8dcSSimon Schubert break; 5805796c8dcSSimon Schubert default: 5815796c8dcSSimon Schubert index += 0; 5825796c8dcSSimon Schubert break; 5835796c8dcSSimon Schubert } 5845796c8dcSSimon Schubert } 5855796c8dcSSimon Schubert operator_possible = 0; 5865796c8dcSSimon Schubert break; 5875796c8dcSSimon Schubert case ' ': 5885796c8dcSSimon Schubert case ',': 5895796c8dcSSimon Schubert case '.': 5905796c8dcSSimon Schubert case '&': 5915796c8dcSSimon Schubert case '*': 5925796c8dcSSimon Schubert /* NOTE: carlton/2003-04-18: I'm not sure what the precise 5935796c8dcSSimon Schubert set of relevant characters are here: it's necessary to 5945796c8dcSSimon Schubert include any character that can show up before 'operator' 5955796c8dcSSimon Schubert in a demangled name, and it's safe to include any 5965796c8dcSSimon Schubert character that can't be part of an identifier's name. */ 5975796c8dcSSimon Schubert operator_possible = 1; 5985796c8dcSSimon Schubert break; 5995796c8dcSSimon Schubert default: 6005796c8dcSSimon Schubert operator_possible = 0; 6015796c8dcSSimon Schubert break; 6025796c8dcSSimon Schubert } 6035796c8dcSSimon Schubert } 6045796c8dcSSimon Schubert } 6055796c8dcSSimon Schubert 6065796c8dcSSimon Schubert /* Complain about a demangled name that we don't know how to parse. 6075796c8dcSSimon Schubert NAME is the demangled name in question. */ 6085796c8dcSSimon Schubert 6095796c8dcSSimon Schubert static void 6105796c8dcSSimon Schubert demangled_name_complaint (const char *name) 6115796c8dcSSimon Schubert { 6125796c8dcSSimon Schubert complaint (&symfile_complaints, 6135796c8dcSSimon Schubert "unexpected demangled name '%s'", name); 6145796c8dcSSimon Schubert } 6155796c8dcSSimon Schubert 6165796c8dcSSimon Schubert /* If NAME is the fully-qualified name of a C++ 6175796c8dcSSimon Schubert function/variable/method/etc., this returns the length of its 6185796c8dcSSimon Schubert entire prefix: all of the namespaces and classes that make up its 6195796c8dcSSimon Schubert name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns 6205796c8dcSSimon Schubert 4, given 'foo', it returns 0. */ 6215796c8dcSSimon Schubert 6225796c8dcSSimon Schubert unsigned int 6235796c8dcSSimon Schubert cp_entire_prefix_len (const char *name) 6245796c8dcSSimon Schubert { 6255796c8dcSSimon Schubert unsigned int current_len = cp_find_first_component (name); 6265796c8dcSSimon Schubert unsigned int previous_len = 0; 6275796c8dcSSimon Schubert 6285796c8dcSSimon Schubert while (name[current_len] != '\0') 6295796c8dcSSimon Schubert { 6305796c8dcSSimon Schubert gdb_assert (name[current_len] == ':'); 6315796c8dcSSimon Schubert previous_len = current_len; 6325796c8dcSSimon Schubert /* Skip the '::'. */ 6335796c8dcSSimon Schubert current_len += 2; 6345796c8dcSSimon Schubert current_len += cp_find_first_component (name + current_len); 6355796c8dcSSimon Schubert } 6365796c8dcSSimon Schubert 6375796c8dcSSimon Schubert return previous_len; 6385796c8dcSSimon Schubert } 6395796c8dcSSimon Schubert 6405796c8dcSSimon Schubert /* Overload resolution functions. */ 6415796c8dcSSimon Schubert 6425796c8dcSSimon Schubert /* Test to see if SYM is a symbol that we haven't seen corresponding 6435796c8dcSSimon Schubert to a function named OLOAD_NAME. If so, add it to the current 6445796c8dcSSimon Schubert completion list. */ 6455796c8dcSSimon Schubert 6465796c8dcSSimon Schubert static void 6475796c8dcSSimon Schubert overload_list_add_symbol (struct symbol *sym, const char *oload_name) 6485796c8dcSSimon Schubert { 6495796c8dcSSimon Schubert int newsize; 6505796c8dcSSimon Schubert int i; 6515796c8dcSSimon Schubert char *sym_name; 6525796c8dcSSimon Schubert 6535796c8dcSSimon Schubert /* If there is no type information, we can't do anything, so skip */ 6545796c8dcSSimon Schubert if (SYMBOL_TYPE (sym) == NULL) 6555796c8dcSSimon Schubert return; 6565796c8dcSSimon Schubert 6575796c8dcSSimon Schubert /* skip any symbols that we've already considered. */ 6585796c8dcSSimon Schubert for (i = 0; i < sym_return_val_index; ++i) 6595796c8dcSSimon Schubert if (strcmp (SYMBOL_LINKAGE_NAME (sym), 6605796c8dcSSimon Schubert SYMBOL_LINKAGE_NAME (sym_return_val[i])) == 0) 6615796c8dcSSimon Schubert return; 6625796c8dcSSimon Schubert 6635796c8dcSSimon Schubert /* Get the demangled name without parameters */ 6645796c8dcSSimon Schubert sym_name = cp_remove_params (SYMBOL_NATURAL_NAME (sym)); 6655796c8dcSSimon Schubert if (!sym_name) 6665796c8dcSSimon Schubert return; 6675796c8dcSSimon Schubert 6685796c8dcSSimon Schubert /* skip symbols that cannot match */ 6695796c8dcSSimon Schubert if (strcmp (sym_name, oload_name) != 0) 6705796c8dcSSimon Schubert { 6715796c8dcSSimon Schubert xfree (sym_name); 6725796c8dcSSimon Schubert return; 6735796c8dcSSimon Schubert } 6745796c8dcSSimon Schubert 6755796c8dcSSimon Schubert xfree (sym_name); 6765796c8dcSSimon Schubert 6775796c8dcSSimon Schubert /* We have a match for an overload instance, so add SYM to the current list 6785796c8dcSSimon Schubert * of overload instances */ 6795796c8dcSSimon Schubert if (sym_return_val_index + 3 > sym_return_val_size) 6805796c8dcSSimon Schubert { 6815796c8dcSSimon Schubert newsize = (sym_return_val_size *= 2) * sizeof (struct symbol *); 6825796c8dcSSimon Schubert sym_return_val = (struct symbol **) xrealloc ((char *) sym_return_val, newsize); 6835796c8dcSSimon Schubert } 6845796c8dcSSimon Schubert sym_return_val[sym_return_val_index++] = sym; 6855796c8dcSSimon Schubert sym_return_val[sym_return_val_index] = NULL; 6865796c8dcSSimon Schubert } 6875796c8dcSSimon Schubert 6885796c8dcSSimon Schubert /* Return a null-terminated list of pointers to function symbols that 6895796c8dcSSimon Schubert are named FUNC_NAME and are visible within NAMESPACE. */ 6905796c8dcSSimon Schubert 6915796c8dcSSimon Schubert struct symbol ** 6925796c8dcSSimon Schubert make_symbol_overload_list (const char *func_name, 6935796c8dcSSimon Schubert const char *namespace) 6945796c8dcSSimon Schubert { 6955796c8dcSSimon Schubert struct cleanup *old_cleanups; 696*cf7f2e2dSJohn Marino const char *name; 6975796c8dcSSimon Schubert 6985796c8dcSSimon Schubert sym_return_val_size = 100; 6995796c8dcSSimon Schubert sym_return_val_index = 0; 7005796c8dcSSimon Schubert sym_return_val = xmalloc ((sym_return_val_size + 1) * 7015796c8dcSSimon Schubert sizeof (struct symbol *)); 7025796c8dcSSimon Schubert sym_return_val[0] = NULL; 7035796c8dcSSimon Schubert 7045796c8dcSSimon Schubert old_cleanups = make_cleanup (xfree, sym_return_val); 7055796c8dcSSimon Schubert 7065796c8dcSSimon Schubert make_symbol_overload_list_using (func_name, namespace); 7075796c8dcSSimon Schubert 708*cf7f2e2dSJohn Marino if (namespace[0] == '\0') 709*cf7f2e2dSJohn Marino name = func_name; 710*cf7f2e2dSJohn Marino else 711*cf7f2e2dSJohn Marino { 712*cf7f2e2dSJohn Marino char *concatenated_name 713*cf7f2e2dSJohn Marino = alloca (strlen (namespace) + 2 + strlen (func_name) + 1); 714*cf7f2e2dSJohn Marino strcpy (concatenated_name, namespace); 715*cf7f2e2dSJohn Marino strcat (concatenated_name, "::"); 716*cf7f2e2dSJohn Marino strcat (concatenated_name, func_name); 717*cf7f2e2dSJohn Marino name = concatenated_name; 718*cf7f2e2dSJohn Marino } 719*cf7f2e2dSJohn Marino 720*cf7f2e2dSJohn Marino make_symbol_overload_list_qualified (name); 721*cf7f2e2dSJohn Marino 7225796c8dcSSimon Schubert discard_cleanups (old_cleanups); 7235796c8dcSSimon Schubert 7245796c8dcSSimon Schubert return sym_return_val; 7255796c8dcSSimon Schubert } 7265796c8dcSSimon Schubert 727*cf7f2e2dSJohn Marino /* Add all symbols with a name matching NAME in BLOCK to the overload 728*cf7f2e2dSJohn Marino list. */ 729*cf7f2e2dSJohn Marino 730*cf7f2e2dSJohn Marino static void 731*cf7f2e2dSJohn Marino make_symbol_overload_list_block (const char *name, 732*cf7f2e2dSJohn Marino const struct block *block) 733*cf7f2e2dSJohn Marino { 734*cf7f2e2dSJohn Marino struct dict_iterator iter; 735*cf7f2e2dSJohn Marino struct symbol *sym; 736*cf7f2e2dSJohn Marino 737*cf7f2e2dSJohn Marino const struct dictionary *dict = BLOCK_DICT (block); 738*cf7f2e2dSJohn Marino 739*cf7f2e2dSJohn Marino for (sym = dict_iter_name_first (dict, name, &iter); 740*cf7f2e2dSJohn Marino sym != NULL; 741*cf7f2e2dSJohn Marino sym = dict_iter_name_next (name, &iter)) 742*cf7f2e2dSJohn Marino overload_list_add_symbol (sym, name); 743*cf7f2e2dSJohn Marino } 744*cf7f2e2dSJohn Marino 745*cf7f2e2dSJohn Marino /* Adds the function FUNC_NAME from NAMESPACE to the overload set. */ 746*cf7f2e2dSJohn Marino 747*cf7f2e2dSJohn Marino static void 748*cf7f2e2dSJohn Marino make_symbol_overload_list_namespace (const char *func_name, 749*cf7f2e2dSJohn Marino const char *namespace) 750*cf7f2e2dSJohn Marino { 751*cf7f2e2dSJohn Marino const char *name; 752*cf7f2e2dSJohn Marino const struct block *block = NULL; 753*cf7f2e2dSJohn Marino 754*cf7f2e2dSJohn Marino if (namespace[0] == '\0') 755*cf7f2e2dSJohn Marino name = func_name; 756*cf7f2e2dSJohn Marino else 757*cf7f2e2dSJohn Marino { 758*cf7f2e2dSJohn Marino char *concatenated_name 759*cf7f2e2dSJohn Marino = alloca (strlen (namespace) + 2 + strlen (func_name) + 1); 760*cf7f2e2dSJohn Marino 761*cf7f2e2dSJohn Marino strcpy (concatenated_name, namespace); 762*cf7f2e2dSJohn Marino strcat (concatenated_name, "::"); 763*cf7f2e2dSJohn Marino strcat (concatenated_name, func_name); 764*cf7f2e2dSJohn Marino name = concatenated_name; 765*cf7f2e2dSJohn Marino } 766*cf7f2e2dSJohn Marino 767*cf7f2e2dSJohn Marino /* Look in the static block. */ 768*cf7f2e2dSJohn Marino block = block_static_block (get_selected_block (0)); 769*cf7f2e2dSJohn Marino make_symbol_overload_list_block (name, block); 770*cf7f2e2dSJohn Marino 771*cf7f2e2dSJohn Marino /* Look in the global block. */ 772*cf7f2e2dSJohn Marino block = block_global_block (block); 773*cf7f2e2dSJohn Marino make_symbol_overload_list_block (name, block); 774*cf7f2e2dSJohn Marino 775*cf7f2e2dSJohn Marino } 776*cf7f2e2dSJohn Marino 777*cf7f2e2dSJohn Marino /* Search the namespace of the given type and namespace of and public base 778*cf7f2e2dSJohn Marino types. */ 779*cf7f2e2dSJohn Marino 780*cf7f2e2dSJohn Marino static void 781*cf7f2e2dSJohn Marino make_symbol_overload_list_adl_namespace (struct type *type, 782*cf7f2e2dSJohn Marino const char *func_name) 783*cf7f2e2dSJohn Marino { 784*cf7f2e2dSJohn Marino char *namespace; 785*cf7f2e2dSJohn Marino char *type_name; 786*cf7f2e2dSJohn Marino int i, prefix_len; 787*cf7f2e2dSJohn Marino 788*cf7f2e2dSJohn Marino while (TYPE_CODE (type) == TYPE_CODE_PTR || TYPE_CODE (type) == TYPE_CODE_REF 789*cf7f2e2dSJohn Marino || TYPE_CODE (type) == TYPE_CODE_ARRAY 790*cf7f2e2dSJohn Marino || TYPE_CODE (type) == TYPE_CODE_TYPEDEF) 791*cf7f2e2dSJohn Marino { 792*cf7f2e2dSJohn Marino if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF) 793*cf7f2e2dSJohn Marino type = check_typedef(type); 794*cf7f2e2dSJohn Marino else 795*cf7f2e2dSJohn Marino type = TYPE_TARGET_TYPE (type); 796*cf7f2e2dSJohn Marino } 797*cf7f2e2dSJohn Marino 798*cf7f2e2dSJohn Marino type_name = TYPE_NAME (type); 799*cf7f2e2dSJohn Marino 800*cf7f2e2dSJohn Marino if (type_name == NULL) 801*cf7f2e2dSJohn Marino return; 802*cf7f2e2dSJohn Marino 803*cf7f2e2dSJohn Marino prefix_len = cp_entire_prefix_len (type_name); 804*cf7f2e2dSJohn Marino 805*cf7f2e2dSJohn Marino if (prefix_len != 0) 806*cf7f2e2dSJohn Marino { 807*cf7f2e2dSJohn Marino namespace = alloca (prefix_len + 1); 808*cf7f2e2dSJohn Marino strncpy (namespace, type_name, prefix_len); 809*cf7f2e2dSJohn Marino namespace[prefix_len] = '\0'; 810*cf7f2e2dSJohn Marino 811*cf7f2e2dSJohn Marino make_symbol_overload_list_namespace (func_name, namespace); 812*cf7f2e2dSJohn Marino } 813*cf7f2e2dSJohn Marino 814*cf7f2e2dSJohn Marino /* Check public base type */ 815*cf7f2e2dSJohn Marino if (TYPE_CODE (type) == TYPE_CODE_CLASS) 816*cf7f2e2dSJohn Marino for (i = 0; i < TYPE_N_BASECLASSES (type); i++) 817*cf7f2e2dSJohn Marino { 818*cf7f2e2dSJohn Marino if (BASETYPE_VIA_PUBLIC (type, i)) 819*cf7f2e2dSJohn Marino make_symbol_overload_list_adl_namespace (TYPE_BASECLASS (type, i), 820*cf7f2e2dSJohn Marino func_name); 821*cf7f2e2dSJohn Marino } 822*cf7f2e2dSJohn Marino } 823*cf7f2e2dSJohn Marino 824*cf7f2e2dSJohn Marino /* Adds the the overload list overload candidates for FUNC_NAME found through 825*cf7f2e2dSJohn Marino argument dependent lookup. */ 826*cf7f2e2dSJohn Marino 827*cf7f2e2dSJohn Marino struct symbol ** 828*cf7f2e2dSJohn Marino make_symbol_overload_list_adl (struct type **arg_types, int nargs, 829*cf7f2e2dSJohn Marino const char *func_name) 830*cf7f2e2dSJohn Marino { 831*cf7f2e2dSJohn Marino int i; 832*cf7f2e2dSJohn Marino 833*cf7f2e2dSJohn Marino gdb_assert (sym_return_val_size != -1); 834*cf7f2e2dSJohn Marino 835*cf7f2e2dSJohn Marino for (i = 1; i <= nargs; i++) 836*cf7f2e2dSJohn Marino make_symbol_overload_list_adl_namespace (arg_types[i - 1], func_name); 837*cf7f2e2dSJohn Marino 838*cf7f2e2dSJohn Marino return sym_return_val; 839*cf7f2e2dSJohn Marino } 840*cf7f2e2dSJohn Marino 841*cf7f2e2dSJohn Marino /* Used for cleanups to reset the "searched" flag in case of an error. */ 842*cf7f2e2dSJohn Marino 843*cf7f2e2dSJohn Marino static void 844*cf7f2e2dSJohn Marino reset_directive_searched (void *data) 845*cf7f2e2dSJohn Marino { 846*cf7f2e2dSJohn Marino struct using_direct *direct = data; 847*cf7f2e2dSJohn Marino direct->searched = 0; 848*cf7f2e2dSJohn Marino } 849*cf7f2e2dSJohn Marino 8505796c8dcSSimon Schubert /* This applies the using directives to add namespaces to search in, 8515796c8dcSSimon Schubert and then searches for overloads in all of those namespaces. It 8525796c8dcSSimon Schubert adds the symbols found to sym_return_val. Arguments are as in 8535796c8dcSSimon Schubert make_symbol_overload_list. */ 8545796c8dcSSimon Schubert 8555796c8dcSSimon Schubert static void 8565796c8dcSSimon Schubert make_symbol_overload_list_using (const char *func_name, 8575796c8dcSSimon Schubert const char *namespace) 8585796c8dcSSimon Schubert { 859*cf7f2e2dSJohn Marino struct using_direct *current; 860*cf7f2e2dSJohn Marino const struct block *block; 8615796c8dcSSimon Schubert 8625796c8dcSSimon Schubert /* First, go through the using directives. If any of them apply, 8635796c8dcSSimon Schubert look in the appropriate namespaces for new functions to match 8645796c8dcSSimon Schubert on. */ 8655796c8dcSSimon Schubert 866*cf7f2e2dSJohn Marino for (block = get_selected_block (0); 867*cf7f2e2dSJohn Marino block != NULL; 868*cf7f2e2dSJohn Marino block = BLOCK_SUPERBLOCK (block)) 869*cf7f2e2dSJohn Marino for (current = block_using (block); 8705796c8dcSSimon Schubert current != NULL; 8715796c8dcSSimon Schubert current = current->next) 8725796c8dcSSimon Schubert { 873*cf7f2e2dSJohn Marino /* Prevent recursive calls. */ 874*cf7f2e2dSJohn Marino if (current->searched) 875*cf7f2e2dSJohn Marino continue; 876*cf7f2e2dSJohn Marino 877*cf7f2e2dSJohn Marino /* If this is a namespace alias or imported declaration ignore it. */ 878*cf7f2e2dSJohn Marino if (current->alias != NULL || current->declaration != NULL) 879*cf7f2e2dSJohn Marino continue; 880*cf7f2e2dSJohn Marino 8815796c8dcSSimon Schubert if (strcmp (namespace, current->import_dest) == 0) 8825796c8dcSSimon Schubert { 883*cf7f2e2dSJohn Marino /* Mark this import as searched so that the recursive call does 884*cf7f2e2dSJohn Marino not search it again. */ 885*cf7f2e2dSJohn Marino struct cleanup *old_chain; 886*cf7f2e2dSJohn Marino current->searched = 1; 887*cf7f2e2dSJohn Marino old_chain = make_cleanup (reset_directive_searched, current); 888*cf7f2e2dSJohn Marino 889*cf7f2e2dSJohn Marino make_symbol_overload_list_using (func_name, current->import_src); 890*cf7f2e2dSJohn Marino 891*cf7f2e2dSJohn Marino current->searched = 0; 892*cf7f2e2dSJohn Marino discard_cleanups (old_chain); 8935796c8dcSSimon Schubert } 8945796c8dcSSimon Schubert } 8955796c8dcSSimon Schubert 8965796c8dcSSimon Schubert /* Now, add names for this namespace. */ 897*cf7f2e2dSJohn Marino make_symbol_overload_list_namespace (func_name, namespace); 8985796c8dcSSimon Schubert } 8995796c8dcSSimon Schubert 9005796c8dcSSimon Schubert /* This does the bulk of the work of finding overloaded symbols. 9015796c8dcSSimon Schubert FUNC_NAME is the name of the overloaded function we're looking for 9025796c8dcSSimon Schubert (possibly including namespace info). */ 9035796c8dcSSimon Schubert 9045796c8dcSSimon Schubert static void 9055796c8dcSSimon Schubert make_symbol_overload_list_qualified (const char *func_name) 9065796c8dcSSimon Schubert { 9075796c8dcSSimon Schubert struct symbol *sym; 9085796c8dcSSimon Schubert struct symtab *s; 9095796c8dcSSimon Schubert struct objfile *objfile; 9105796c8dcSSimon Schubert const struct block *b, *surrounding_static_block = 0; 9115796c8dcSSimon Schubert struct dict_iterator iter; 9125796c8dcSSimon Schubert const struct dictionary *dict; 9135796c8dcSSimon Schubert 9145796c8dcSSimon Schubert /* Look through the partial symtabs for all symbols which begin 9155796c8dcSSimon Schubert by matching FUNC_NAME. Make sure we read that symbol table in. */ 9165796c8dcSSimon Schubert 917*cf7f2e2dSJohn Marino ALL_OBJFILES (objfile) 918*cf7f2e2dSJohn Marino { 919*cf7f2e2dSJohn Marino if (objfile->sf) 920*cf7f2e2dSJohn Marino objfile->sf->qf->expand_symtabs_for_function (objfile, func_name); 921*cf7f2e2dSJohn Marino } 9225796c8dcSSimon Schubert 9235796c8dcSSimon Schubert /* Search upwards from currently selected frame (so that we can 9245796c8dcSSimon Schubert complete on local vars. */ 9255796c8dcSSimon Schubert 9265796c8dcSSimon Schubert for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b)) 927*cf7f2e2dSJohn Marino make_symbol_overload_list_block (func_name, b); 9285796c8dcSSimon Schubert 9295796c8dcSSimon Schubert surrounding_static_block = block_static_block (get_selected_block (0)); 9305796c8dcSSimon Schubert 9315796c8dcSSimon Schubert /* Go through the symtabs and check the externs and statics for 9325796c8dcSSimon Schubert symbols which match. */ 9335796c8dcSSimon Schubert 9345796c8dcSSimon Schubert ALL_PRIMARY_SYMTABS (objfile, s) 9355796c8dcSSimon Schubert { 9365796c8dcSSimon Schubert QUIT; 9375796c8dcSSimon Schubert b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK); 938*cf7f2e2dSJohn Marino make_symbol_overload_list_block (func_name, b); 9395796c8dcSSimon Schubert } 9405796c8dcSSimon Schubert 9415796c8dcSSimon Schubert ALL_PRIMARY_SYMTABS (objfile, s) 9425796c8dcSSimon Schubert { 9435796c8dcSSimon Schubert QUIT; 9445796c8dcSSimon Schubert b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK); 9455796c8dcSSimon Schubert /* Don't do this block twice. */ 9465796c8dcSSimon Schubert if (b == surrounding_static_block) 9475796c8dcSSimon Schubert continue; 948*cf7f2e2dSJohn Marino make_symbol_overload_list_block (func_name, b); 9495796c8dcSSimon Schubert } 9505796c8dcSSimon Schubert } 9515796c8dcSSimon Schubert 9525796c8dcSSimon Schubert /* Lookup the rtti type for a class name. */ 9535796c8dcSSimon Schubert 9545796c8dcSSimon Schubert struct type * 9555796c8dcSSimon Schubert cp_lookup_rtti_type (const char *name, struct block *block) 9565796c8dcSSimon Schubert { 9575796c8dcSSimon Schubert struct symbol * rtti_sym; 9585796c8dcSSimon Schubert struct type * rtti_type; 9595796c8dcSSimon Schubert 9605796c8dcSSimon Schubert rtti_sym = lookup_symbol (name, block, STRUCT_DOMAIN, NULL); 9615796c8dcSSimon Schubert 9625796c8dcSSimon Schubert if (rtti_sym == NULL) 9635796c8dcSSimon Schubert { 9645796c8dcSSimon Schubert warning (_("RTTI symbol not found for class '%s'"), name); 9655796c8dcSSimon Schubert return NULL; 9665796c8dcSSimon Schubert } 9675796c8dcSSimon Schubert 9685796c8dcSSimon Schubert if (SYMBOL_CLASS (rtti_sym) != LOC_TYPEDEF) 9695796c8dcSSimon Schubert { 9705796c8dcSSimon Schubert warning (_("RTTI symbol for class '%s' is not a type"), name); 9715796c8dcSSimon Schubert return NULL; 9725796c8dcSSimon Schubert } 9735796c8dcSSimon Schubert 9745796c8dcSSimon Schubert rtti_type = SYMBOL_TYPE (rtti_sym); 9755796c8dcSSimon Schubert 9765796c8dcSSimon Schubert switch (TYPE_CODE (rtti_type)) 9775796c8dcSSimon Schubert { 9785796c8dcSSimon Schubert case TYPE_CODE_CLASS: 9795796c8dcSSimon Schubert break; 9805796c8dcSSimon Schubert case TYPE_CODE_NAMESPACE: 9815796c8dcSSimon Schubert /* chastain/2003-11-26: the symbol tables often contain fake 9825796c8dcSSimon Schubert symbols for namespaces with the same name as the struct. 9835796c8dcSSimon Schubert This warning is an indication of a bug in the lookup order 9845796c8dcSSimon Schubert or a bug in the way that the symbol tables are populated. */ 9855796c8dcSSimon Schubert warning (_("RTTI symbol for class '%s' is a namespace"), name); 9865796c8dcSSimon Schubert return NULL; 9875796c8dcSSimon Schubert default: 9885796c8dcSSimon Schubert warning (_("RTTI symbol for class '%s' has bad type"), name); 9895796c8dcSSimon Schubert return NULL; 9905796c8dcSSimon Schubert } 9915796c8dcSSimon Schubert 9925796c8dcSSimon Schubert return rtti_type; 9935796c8dcSSimon Schubert } 9945796c8dcSSimon Schubert 9955796c8dcSSimon Schubert /* Don't allow just "maintenance cplus". */ 9965796c8dcSSimon Schubert 9975796c8dcSSimon Schubert static void 9985796c8dcSSimon Schubert maint_cplus_command (char *arg, int from_tty) 9995796c8dcSSimon Schubert { 10005796c8dcSSimon Schubert printf_unfiltered (_("\"maintenance cplus\" must be followed by the name of a command.\n")); 10015796c8dcSSimon Schubert help_list (maint_cplus_cmd_list, "maintenance cplus ", -1, gdb_stdout); 10025796c8dcSSimon Schubert } 10035796c8dcSSimon Schubert 10045796c8dcSSimon Schubert /* This is a front end for cp_find_first_component, for unit testing. 10055796c8dcSSimon Schubert Be careful when using it: see the NOTE above 10065796c8dcSSimon Schubert cp_find_first_component. */ 10075796c8dcSSimon Schubert 10085796c8dcSSimon Schubert static void 10095796c8dcSSimon Schubert first_component_command (char *arg, int from_tty) 10105796c8dcSSimon Schubert { 10115796c8dcSSimon Schubert int len; 10125796c8dcSSimon Schubert char *prefix; 10135796c8dcSSimon Schubert 10145796c8dcSSimon Schubert if (!arg) 10155796c8dcSSimon Schubert return; 10165796c8dcSSimon Schubert 10175796c8dcSSimon Schubert len = cp_find_first_component (arg); 10185796c8dcSSimon Schubert prefix = alloca (len + 1); 10195796c8dcSSimon Schubert 10205796c8dcSSimon Schubert memcpy (prefix, arg, len); 10215796c8dcSSimon Schubert prefix[len] = '\0'; 10225796c8dcSSimon Schubert 10235796c8dcSSimon Schubert printf_unfiltered ("%s\n", prefix); 10245796c8dcSSimon Schubert } 10255796c8dcSSimon Schubert 10265796c8dcSSimon Schubert extern initialize_file_ftype _initialize_cp_support; /* -Wmissing-prototypes */ 10275796c8dcSSimon Schubert 1028*cf7f2e2dSJohn Marino #define SKIP_SPACE(P) \ 1029*cf7f2e2dSJohn Marino do \ 1030*cf7f2e2dSJohn Marino { \ 1031*cf7f2e2dSJohn Marino while (*(P) == ' ' || *(P) == '\t') \ 1032*cf7f2e2dSJohn Marino ++(P); \ 1033*cf7f2e2dSJohn Marino } \ 1034*cf7f2e2dSJohn Marino while (0) 1035*cf7f2e2dSJohn Marino 1036*cf7f2e2dSJohn Marino /* Returns the length of the operator name or 0 if INPUT does not 1037*cf7f2e2dSJohn Marino point to a valid C++ operator. INPUT should start with "operator". */ 1038*cf7f2e2dSJohn Marino int 1039*cf7f2e2dSJohn Marino cp_validate_operator (const char *input) 1040*cf7f2e2dSJohn Marino { 1041*cf7f2e2dSJohn Marino int i; 1042*cf7f2e2dSJohn Marino char *copy; 1043*cf7f2e2dSJohn Marino const char *p; 1044*cf7f2e2dSJohn Marino struct expression *expr; 1045*cf7f2e2dSJohn Marino struct value *val; 1046*cf7f2e2dSJohn Marino struct gdb_exception except; 1047*cf7f2e2dSJohn Marino 1048*cf7f2e2dSJohn Marino p = input; 1049*cf7f2e2dSJohn Marino 1050*cf7f2e2dSJohn Marino if (strncmp (p, "operator", 8) == 0) 1051*cf7f2e2dSJohn Marino { 1052*cf7f2e2dSJohn Marino int valid = 0; 1053*cf7f2e2dSJohn Marino 1054*cf7f2e2dSJohn Marino p += 8; 1055*cf7f2e2dSJohn Marino SKIP_SPACE (p); 1056*cf7f2e2dSJohn Marino for (i = 0; i < sizeof (operator_tokens) / sizeof (operator_tokens[0]); 1057*cf7f2e2dSJohn Marino ++i) 1058*cf7f2e2dSJohn Marino { 1059*cf7f2e2dSJohn Marino int length = strlen (operator_tokens[i]); 1060*cf7f2e2dSJohn Marino 1061*cf7f2e2dSJohn Marino /* By using strncmp here, we MUST have operator_tokens ordered! 1062*cf7f2e2dSJohn Marino See additional notes where operator_tokens is defined above. */ 1063*cf7f2e2dSJohn Marino if (strncmp (p, operator_tokens[i], length) == 0) 1064*cf7f2e2dSJohn Marino { 1065*cf7f2e2dSJohn Marino const char *op = p; 1066*cf7f2e2dSJohn Marino 1067*cf7f2e2dSJohn Marino valid = 1; 1068*cf7f2e2dSJohn Marino p += length; 1069*cf7f2e2dSJohn Marino 1070*cf7f2e2dSJohn Marino if (strncmp (op, "new", 3) == 0 1071*cf7f2e2dSJohn Marino || strncmp (op, "delete", 6) == 0) 1072*cf7f2e2dSJohn Marino { 1073*cf7f2e2dSJohn Marino 1074*cf7f2e2dSJohn Marino /* Special case: new[] and delete[]. We must be careful 1075*cf7f2e2dSJohn Marino to swallow whitespace before/in "[]". */ 1076*cf7f2e2dSJohn Marino SKIP_SPACE (p); 1077*cf7f2e2dSJohn Marino 1078*cf7f2e2dSJohn Marino if (*p == '[') 1079*cf7f2e2dSJohn Marino { 1080*cf7f2e2dSJohn Marino ++p; 1081*cf7f2e2dSJohn Marino SKIP_SPACE (p); 1082*cf7f2e2dSJohn Marino if (*p == ']') 1083*cf7f2e2dSJohn Marino ++p; 1084*cf7f2e2dSJohn Marino else 1085*cf7f2e2dSJohn Marino valid = 0; 1086*cf7f2e2dSJohn Marino } 1087*cf7f2e2dSJohn Marino } 1088*cf7f2e2dSJohn Marino 1089*cf7f2e2dSJohn Marino if (valid) 1090*cf7f2e2dSJohn Marino return (p - input); 1091*cf7f2e2dSJohn Marino } 1092*cf7f2e2dSJohn Marino } 1093*cf7f2e2dSJohn Marino 1094*cf7f2e2dSJohn Marino /* Check input for a conversion operator. */ 1095*cf7f2e2dSJohn Marino 1096*cf7f2e2dSJohn Marino /* Skip past base typename */ 1097*cf7f2e2dSJohn Marino while (*p != '*' && *p != '&' && *p != 0 && *p != ' ') 1098*cf7f2e2dSJohn Marino ++p; 1099*cf7f2e2dSJohn Marino SKIP_SPACE (p); 1100*cf7f2e2dSJohn Marino 1101*cf7f2e2dSJohn Marino /* Add modifiers '*'/'&' */ 1102*cf7f2e2dSJohn Marino while (*p == '*' || *p == '&') 1103*cf7f2e2dSJohn Marino { 1104*cf7f2e2dSJohn Marino ++p; 1105*cf7f2e2dSJohn Marino SKIP_SPACE (p); 1106*cf7f2e2dSJohn Marino } 1107*cf7f2e2dSJohn Marino 1108*cf7f2e2dSJohn Marino /* Check for valid type. [Remember: input starts with 1109*cf7f2e2dSJohn Marino "operator".] */ 1110*cf7f2e2dSJohn Marino copy = savestring (input + 8, p - input - 8); 1111*cf7f2e2dSJohn Marino expr = NULL; 1112*cf7f2e2dSJohn Marino val = NULL; 1113*cf7f2e2dSJohn Marino TRY_CATCH (except, RETURN_MASK_ALL) 1114*cf7f2e2dSJohn Marino { 1115*cf7f2e2dSJohn Marino expr = parse_expression (copy); 1116*cf7f2e2dSJohn Marino val = evaluate_type (expr); 1117*cf7f2e2dSJohn Marino } 1118*cf7f2e2dSJohn Marino 1119*cf7f2e2dSJohn Marino xfree (copy); 1120*cf7f2e2dSJohn Marino if (expr) 1121*cf7f2e2dSJohn Marino xfree (expr); 1122*cf7f2e2dSJohn Marino 1123*cf7f2e2dSJohn Marino if (val != NULL && value_type (val) != NULL) 1124*cf7f2e2dSJohn Marino return (p - input); 1125*cf7f2e2dSJohn Marino } 1126*cf7f2e2dSJohn Marino 1127*cf7f2e2dSJohn Marino return 0; 1128*cf7f2e2dSJohn Marino } 1129*cf7f2e2dSJohn Marino 11305796c8dcSSimon Schubert void 11315796c8dcSSimon Schubert _initialize_cp_support (void) 11325796c8dcSSimon Schubert { 11335796c8dcSSimon Schubert add_prefix_cmd ("cplus", class_maintenance, maint_cplus_command, 11345796c8dcSSimon Schubert _("C++ maintenance commands."), &maint_cplus_cmd_list, 11355796c8dcSSimon Schubert "maintenance cplus ", 0, &maintenancelist); 11365796c8dcSSimon Schubert add_alias_cmd ("cp", "cplus", class_maintenance, 1, &maintenancelist); 11375796c8dcSSimon Schubert 11385796c8dcSSimon Schubert add_cmd ("first_component", class_maintenance, first_component_command, 11395796c8dcSSimon Schubert _("Print the first class/namespace component of NAME."), 11405796c8dcSSimon Schubert &maint_cplus_cmd_list); 11415796c8dcSSimon Schubert } 1142