xref: /dflybsd-src/contrib/gdb-7/gdb/cp-support.c (revision 5796c8dc12c637f18a1740c26afd8d40ffa9b719)
1*5796c8dcSSimon Schubert /* Helper routines for C++ support in GDB.
2*5796c8dcSSimon Schubert    Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008, 2009
3*5796c8dcSSimon Schubert    Free Software Foundation, Inc.
4*5796c8dcSSimon Schubert 
5*5796c8dcSSimon Schubert    Contributed by MontaVista Software.
6*5796c8dcSSimon Schubert 
7*5796c8dcSSimon Schubert    This file is part of GDB.
8*5796c8dcSSimon Schubert 
9*5796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
10*5796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
11*5796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
12*5796c8dcSSimon Schubert    (at your option) any later version.
13*5796c8dcSSimon Schubert 
14*5796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
15*5796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
16*5796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17*5796c8dcSSimon Schubert    GNU General Public License for more details.
18*5796c8dcSSimon Schubert 
19*5796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
20*5796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21*5796c8dcSSimon Schubert 
22*5796c8dcSSimon Schubert #include "defs.h"
23*5796c8dcSSimon Schubert #include "cp-support.h"
24*5796c8dcSSimon Schubert #include "gdb_string.h"
25*5796c8dcSSimon Schubert #include "demangle.h"
26*5796c8dcSSimon Schubert #include "gdb_assert.h"
27*5796c8dcSSimon Schubert #include "gdbcmd.h"
28*5796c8dcSSimon Schubert #include "dictionary.h"
29*5796c8dcSSimon Schubert #include "objfiles.h"
30*5796c8dcSSimon Schubert #include "frame.h"
31*5796c8dcSSimon Schubert #include "symtab.h"
32*5796c8dcSSimon Schubert #include "block.h"
33*5796c8dcSSimon Schubert #include "complaints.h"
34*5796c8dcSSimon Schubert #include "gdbtypes.h"
35*5796c8dcSSimon Schubert 
36*5796c8dcSSimon Schubert #include "safe-ctype.h"
37*5796c8dcSSimon Schubert 
38*5796c8dcSSimon Schubert #define d_left(dc) (dc)->u.s_binary.left
39*5796c8dcSSimon Schubert #define d_right(dc) (dc)->u.s_binary.right
40*5796c8dcSSimon Schubert 
41*5796c8dcSSimon Schubert /* Functions related to demangled name parsing.  */
42*5796c8dcSSimon Schubert 
43*5796c8dcSSimon Schubert static unsigned int cp_find_first_component_aux (const char *name,
44*5796c8dcSSimon Schubert 						 int permissive);
45*5796c8dcSSimon Schubert 
46*5796c8dcSSimon Schubert static void demangled_name_complaint (const char *name);
47*5796c8dcSSimon Schubert 
48*5796c8dcSSimon Schubert /* Functions/variables related to overload resolution.  */
49*5796c8dcSSimon Schubert 
50*5796c8dcSSimon Schubert static int sym_return_val_size;
51*5796c8dcSSimon Schubert static int sym_return_val_index;
52*5796c8dcSSimon Schubert static struct symbol **sym_return_val;
53*5796c8dcSSimon Schubert 
54*5796c8dcSSimon Schubert static void overload_list_add_symbol (struct symbol *sym,
55*5796c8dcSSimon Schubert 				      const char *oload_name);
56*5796c8dcSSimon Schubert 
57*5796c8dcSSimon Schubert static void make_symbol_overload_list_using (const char *func_name,
58*5796c8dcSSimon Schubert 					     const char *namespace);
59*5796c8dcSSimon Schubert 
60*5796c8dcSSimon Schubert static void make_symbol_overload_list_qualified (const char *func_name);
61*5796c8dcSSimon Schubert 
62*5796c8dcSSimon Schubert static void read_in_psymtabs (const char *oload_name);
63*5796c8dcSSimon Schubert 
64*5796c8dcSSimon Schubert /* The list of "maint cplus" commands.  */
65*5796c8dcSSimon Schubert 
66*5796c8dcSSimon Schubert struct cmd_list_element *maint_cplus_cmd_list = NULL;
67*5796c8dcSSimon Schubert 
68*5796c8dcSSimon Schubert /* The actual commands.  */
69*5796c8dcSSimon Schubert 
70*5796c8dcSSimon Schubert static void maint_cplus_command (char *arg, int from_tty);
71*5796c8dcSSimon Schubert static void first_component_command (char *arg, int from_tty);
72*5796c8dcSSimon Schubert 
73*5796c8dcSSimon Schubert /* Return 1 if STRING is clearly already in canonical form.  This
74*5796c8dcSSimon Schubert    function is conservative; things which it does not recognize are
75*5796c8dcSSimon Schubert    assumed to be non-canonical, and the parser will sort them out
76*5796c8dcSSimon Schubert    afterwards.  This speeds up the critical path for alphanumeric
77*5796c8dcSSimon Schubert    identifiers.  */
78*5796c8dcSSimon Schubert 
79*5796c8dcSSimon Schubert static int
80*5796c8dcSSimon Schubert cp_already_canonical (const char *string)
81*5796c8dcSSimon Schubert {
82*5796c8dcSSimon Schubert   /* Identifier start character [a-zA-Z_].  */
83*5796c8dcSSimon Schubert   if (!ISIDST (string[0]))
84*5796c8dcSSimon Schubert     return 0;
85*5796c8dcSSimon Schubert 
86*5796c8dcSSimon Schubert   /* These are the only two identifiers which canonicalize to other
87*5796c8dcSSimon Schubert      than themselves or an error: unsigned -> unsigned int and
88*5796c8dcSSimon Schubert      signed -> int.  */
89*5796c8dcSSimon Schubert   if (string[0] == 'u' && strcmp (&string[1], "nsigned") == 0)
90*5796c8dcSSimon Schubert     return 0;
91*5796c8dcSSimon Schubert   else if (string[0] == 's' && strcmp (&string[1], "igned") == 0)
92*5796c8dcSSimon Schubert     return 0;
93*5796c8dcSSimon Schubert 
94*5796c8dcSSimon Schubert   /* Identifier character [a-zA-Z0-9_].  */
95*5796c8dcSSimon Schubert   while (ISIDNUM (string[1]))
96*5796c8dcSSimon Schubert     string++;
97*5796c8dcSSimon Schubert 
98*5796c8dcSSimon Schubert   if (string[1] == '\0')
99*5796c8dcSSimon Schubert     return 1;
100*5796c8dcSSimon Schubert   else
101*5796c8dcSSimon Schubert     return 0;
102*5796c8dcSSimon Schubert }
103*5796c8dcSSimon Schubert 
104*5796c8dcSSimon Schubert /* Parse STRING and convert it to canonical form.  If parsing fails,
105*5796c8dcSSimon Schubert    or if STRING is already canonical, return NULL.  Otherwise return
106*5796c8dcSSimon Schubert    the canonical form.  The return value is allocated via xmalloc.  */
107*5796c8dcSSimon Schubert 
108*5796c8dcSSimon Schubert char *
109*5796c8dcSSimon Schubert cp_canonicalize_string (const char *string)
110*5796c8dcSSimon Schubert {
111*5796c8dcSSimon Schubert   struct demangle_component *ret_comp;
112*5796c8dcSSimon Schubert   unsigned int estimated_len;
113*5796c8dcSSimon Schubert   char *ret;
114*5796c8dcSSimon Schubert 
115*5796c8dcSSimon Schubert   if (cp_already_canonical (string))
116*5796c8dcSSimon Schubert     return NULL;
117*5796c8dcSSimon Schubert 
118*5796c8dcSSimon Schubert   ret_comp = cp_demangled_name_to_comp (string, NULL);
119*5796c8dcSSimon Schubert   if (ret_comp == NULL)
120*5796c8dcSSimon Schubert     return NULL;
121*5796c8dcSSimon Schubert 
122*5796c8dcSSimon Schubert   estimated_len = strlen (string) * 2;
123*5796c8dcSSimon Schubert   ret = cp_comp_to_string (ret_comp, estimated_len);
124*5796c8dcSSimon Schubert 
125*5796c8dcSSimon Schubert   if (strcmp (string, ret) == 0)
126*5796c8dcSSimon Schubert     {
127*5796c8dcSSimon Schubert       xfree (ret);
128*5796c8dcSSimon Schubert       return NULL;
129*5796c8dcSSimon Schubert     }
130*5796c8dcSSimon Schubert 
131*5796c8dcSSimon Schubert   return ret;
132*5796c8dcSSimon Schubert }
133*5796c8dcSSimon Schubert 
134*5796c8dcSSimon Schubert /* Convert a mangled name to a demangle_component tree.  *MEMORY is set to the
135*5796c8dcSSimon Schubert    block of used memory that should be freed when finished with the tree.
136*5796c8dcSSimon Schubert    DEMANGLED_P is set to the char * that should be freed when finished with
137*5796c8dcSSimon Schubert    the tree, or NULL if none was needed.  OPTIONS will be passed to the
138*5796c8dcSSimon Schubert    demangler.  */
139*5796c8dcSSimon Schubert 
140*5796c8dcSSimon Schubert static struct demangle_component *
141*5796c8dcSSimon Schubert mangled_name_to_comp (const char *mangled_name, int options,
142*5796c8dcSSimon Schubert 		      void **memory, char **demangled_p)
143*5796c8dcSSimon Schubert {
144*5796c8dcSSimon Schubert   struct demangle_component *ret;
145*5796c8dcSSimon Schubert   char *demangled_name;
146*5796c8dcSSimon Schubert   int len;
147*5796c8dcSSimon Schubert 
148*5796c8dcSSimon Schubert   /* If it looks like a v3 mangled name, then try to go directly
149*5796c8dcSSimon Schubert      to trees.  */
150*5796c8dcSSimon Schubert   if (mangled_name[0] == '_' && mangled_name[1] == 'Z')
151*5796c8dcSSimon Schubert     {
152*5796c8dcSSimon Schubert       ret = cplus_demangle_v3_components (mangled_name, options, memory);
153*5796c8dcSSimon Schubert       if (ret)
154*5796c8dcSSimon Schubert 	{
155*5796c8dcSSimon Schubert 	  *demangled_p = NULL;
156*5796c8dcSSimon Schubert 	  return ret;
157*5796c8dcSSimon Schubert 	}
158*5796c8dcSSimon Schubert     }
159*5796c8dcSSimon Schubert 
160*5796c8dcSSimon Schubert   /* If it doesn't, or if that failed, then try to demangle the name.  */
161*5796c8dcSSimon Schubert   demangled_name = cplus_demangle (mangled_name, options);
162*5796c8dcSSimon Schubert   if (demangled_name == NULL)
163*5796c8dcSSimon Schubert    return NULL;
164*5796c8dcSSimon Schubert 
165*5796c8dcSSimon Schubert   /* If we could demangle the name, parse it to build the component tree.  */
166*5796c8dcSSimon Schubert   ret = cp_demangled_name_to_comp (demangled_name, NULL);
167*5796c8dcSSimon Schubert 
168*5796c8dcSSimon Schubert   if (ret == NULL)
169*5796c8dcSSimon Schubert     {
170*5796c8dcSSimon Schubert       xfree (demangled_name);
171*5796c8dcSSimon Schubert       return NULL;
172*5796c8dcSSimon Schubert     }
173*5796c8dcSSimon Schubert 
174*5796c8dcSSimon Schubert   *demangled_p = demangled_name;
175*5796c8dcSSimon Schubert   return ret;
176*5796c8dcSSimon Schubert }
177*5796c8dcSSimon Schubert 
178*5796c8dcSSimon Schubert /* Return the name of the class containing method PHYSNAME.  */
179*5796c8dcSSimon Schubert 
180*5796c8dcSSimon Schubert char *
181*5796c8dcSSimon Schubert cp_class_name_from_physname (const char *physname)
182*5796c8dcSSimon Schubert {
183*5796c8dcSSimon Schubert   void *storage = NULL;
184*5796c8dcSSimon Schubert   char *demangled_name = NULL, *ret;
185*5796c8dcSSimon Schubert   struct demangle_component *ret_comp, *prev_comp, *cur_comp;
186*5796c8dcSSimon Schubert   int done;
187*5796c8dcSSimon Schubert 
188*5796c8dcSSimon Schubert   ret_comp = mangled_name_to_comp (physname, DMGL_ANSI, &storage,
189*5796c8dcSSimon Schubert 				   &demangled_name);
190*5796c8dcSSimon Schubert   if (ret_comp == NULL)
191*5796c8dcSSimon Schubert     return NULL;
192*5796c8dcSSimon Schubert 
193*5796c8dcSSimon Schubert   done = 0;
194*5796c8dcSSimon Schubert 
195*5796c8dcSSimon Schubert   /* First strip off any qualifiers, if we have a function or method.  */
196*5796c8dcSSimon Schubert   while (!done)
197*5796c8dcSSimon Schubert     switch (ret_comp->type)
198*5796c8dcSSimon Schubert       {
199*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_CONST:
200*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_RESTRICT:
201*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_VOLATILE:
202*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_CONST_THIS:
203*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_RESTRICT_THIS:
204*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_VOLATILE_THIS:
205*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
206*5796c8dcSSimon Schubert         ret_comp = d_left (ret_comp);
207*5796c8dcSSimon Schubert         break;
208*5796c8dcSSimon Schubert       default:
209*5796c8dcSSimon Schubert 	done = 1;
210*5796c8dcSSimon Schubert 	break;
211*5796c8dcSSimon Schubert       }
212*5796c8dcSSimon Schubert 
213*5796c8dcSSimon Schubert   /* If what we have now is a function, discard the argument list.  */
214*5796c8dcSSimon Schubert   if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
215*5796c8dcSSimon Schubert     ret_comp = d_left (ret_comp);
216*5796c8dcSSimon Schubert 
217*5796c8dcSSimon Schubert   /* If what we have now is a template, strip off the template
218*5796c8dcSSimon Schubert      arguments.  The left subtree may be a qualified name.  */
219*5796c8dcSSimon Schubert   if (ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE)
220*5796c8dcSSimon Schubert     ret_comp = d_left (ret_comp);
221*5796c8dcSSimon Schubert 
222*5796c8dcSSimon Schubert   /* What we have now should be a name, possibly qualified.  Additional
223*5796c8dcSSimon Schubert      qualifiers could live in the left subtree or the right subtree.  Find
224*5796c8dcSSimon Schubert      the last piece.  */
225*5796c8dcSSimon Schubert   done = 0;
226*5796c8dcSSimon Schubert   prev_comp = NULL;
227*5796c8dcSSimon Schubert   cur_comp = ret_comp;
228*5796c8dcSSimon Schubert   while (!done)
229*5796c8dcSSimon Schubert     switch (cur_comp->type)
230*5796c8dcSSimon Schubert       {
231*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_QUAL_NAME:
232*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_LOCAL_NAME:
233*5796c8dcSSimon Schubert 	prev_comp = cur_comp;
234*5796c8dcSSimon Schubert         cur_comp = d_right (cur_comp);
235*5796c8dcSSimon Schubert         break;
236*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_TEMPLATE:
237*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_NAME:
238*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_CTOR:
239*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_DTOR:
240*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_OPERATOR:
241*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
242*5796c8dcSSimon Schubert 	done = 1;
243*5796c8dcSSimon Schubert 	break;
244*5796c8dcSSimon Schubert       default:
245*5796c8dcSSimon Schubert 	done = 1;
246*5796c8dcSSimon Schubert 	cur_comp = NULL;
247*5796c8dcSSimon Schubert 	break;
248*5796c8dcSSimon Schubert       }
249*5796c8dcSSimon Schubert 
250*5796c8dcSSimon Schubert   ret = NULL;
251*5796c8dcSSimon Schubert   if (cur_comp != NULL && prev_comp != NULL)
252*5796c8dcSSimon Schubert     {
253*5796c8dcSSimon Schubert       /* We want to discard the rightmost child of PREV_COMP.  */
254*5796c8dcSSimon Schubert       *prev_comp = *d_left (prev_comp);
255*5796c8dcSSimon Schubert       /* The ten is completely arbitrary; we don't have a good estimate.  */
256*5796c8dcSSimon Schubert       ret = cp_comp_to_string (ret_comp, 10);
257*5796c8dcSSimon Schubert     }
258*5796c8dcSSimon Schubert 
259*5796c8dcSSimon Schubert   xfree (storage);
260*5796c8dcSSimon Schubert   if (demangled_name)
261*5796c8dcSSimon Schubert     xfree (demangled_name);
262*5796c8dcSSimon Schubert   return ret;
263*5796c8dcSSimon Schubert }
264*5796c8dcSSimon Schubert 
265*5796c8dcSSimon Schubert /* Return the child of COMP which is the basename of a method, variable,
266*5796c8dcSSimon Schubert    et cetera.  All scope qualifiers are discarded, but template arguments
267*5796c8dcSSimon Schubert    will be included.  The component tree may be modified.  */
268*5796c8dcSSimon Schubert 
269*5796c8dcSSimon Schubert static struct demangle_component *
270*5796c8dcSSimon Schubert unqualified_name_from_comp (struct demangle_component *comp)
271*5796c8dcSSimon Schubert {
272*5796c8dcSSimon Schubert   struct demangle_component *ret_comp = comp, *last_template;
273*5796c8dcSSimon Schubert   int done;
274*5796c8dcSSimon Schubert 
275*5796c8dcSSimon Schubert   done = 0;
276*5796c8dcSSimon Schubert   last_template = NULL;
277*5796c8dcSSimon Schubert   while (!done)
278*5796c8dcSSimon Schubert     switch (ret_comp->type)
279*5796c8dcSSimon Schubert       {
280*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_QUAL_NAME:
281*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_LOCAL_NAME:
282*5796c8dcSSimon Schubert         ret_comp = d_right (ret_comp);
283*5796c8dcSSimon Schubert         break;
284*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_TYPED_NAME:
285*5796c8dcSSimon Schubert         ret_comp = d_left (ret_comp);
286*5796c8dcSSimon Schubert         break;
287*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_TEMPLATE:
288*5796c8dcSSimon Schubert 	gdb_assert (last_template == NULL);
289*5796c8dcSSimon Schubert 	last_template = ret_comp;
290*5796c8dcSSimon Schubert 	ret_comp = d_left (ret_comp);
291*5796c8dcSSimon Schubert 	break;
292*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_CONST:
293*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_RESTRICT:
294*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_VOLATILE:
295*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_CONST_THIS:
296*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_RESTRICT_THIS:
297*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_VOLATILE_THIS:
298*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
299*5796c8dcSSimon Schubert         ret_comp = d_left (ret_comp);
300*5796c8dcSSimon Schubert         break;
301*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_NAME:
302*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_CTOR:
303*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_DTOR:
304*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_OPERATOR:
305*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
306*5796c8dcSSimon Schubert 	done = 1;
307*5796c8dcSSimon Schubert 	break;
308*5796c8dcSSimon Schubert       default:
309*5796c8dcSSimon Schubert 	return NULL;
310*5796c8dcSSimon Schubert 	break;
311*5796c8dcSSimon Schubert       }
312*5796c8dcSSimon Schubert 
313*5796c8dcSSimon Schubert   if (last_template)
314*5796c8dcSSimon Schubert     {
315*5796c8dcSSimon Schubert       d_left (last_template) = ret_comp;
316*5796c8dcSSimon Schubert       return last_template;
317*5796c8dcSSimon Schubert     }
318*5796c8dcSSimon Schubert 
319*5796c8dcSSimon Schubert   return ret_comp;
320*5796c8dcSSimon Schubert }
321*5796c8dcSSimon Schubert 
322*5796c8dcSSimon Schubert /* Return the name of the method whose linkage name is PHYSNAME.  */
323*5796c8dcSSimon Schubert 
324*5796c8dcSSimon Schubert char *
325*5796c8dcSSimon Schubert method_name_from_physname (const char *physname)
326*5796c8dcSSimon Schubert {
327*5796c8dcSSimon Schubert   void *storage = NULL;
328*5796c8dcSSimon Schubert   char *demangled_name = NULL, *ret;
329*5796c8dcSSimon Schubert   struct demangle_component *ret_comp;
330*5796c8dcSSimon Schubert   int done;
331*5796c8dcSSimon Schubert 
332*5796c8dcSSimon Schubert   ret_comp = mangled_name_to_comp (physname, DMGL_ANSI, &storage,
333*5796c8dcSSimon Schubert 				   &demangled_name);
334*5796c8dcSSimon Schubert   if (ret_comp == NULL)
335*5796c8dcSSimon Schubert     return NULL;
336*5796c8dcSSimon Schubert 
337*5796c8dcSSimon Schubert   ret_comp = unqualified_name_from_comp (ret_comp);
338*5796c8dcSSimon Schubert 
339*5796c8dcSSimon Schubert   ret = NULL;
340*5796c8dcSSimon Schubert   if (ret_comp != NULL)
341*5796c8dcSSimon Schubert     /* The ten is completely arbitrary; we don't have a good estimate.  */
342*5796c8dcSSimon Schubert     ret = cp_comp_to_string (ret_comp, 10);
343*5796c8dcSSimon Schubert 
344*5796c8dcSSimon Schubert   xfree (storage);
345*5796c8dcSSimon Schubert   if (demangled_name)
346*5796c8dcSSimon Schubert     xfree (demangled_name);
347*5796c8dcSSimon Schubert   return ret;
348*5796c8dcSSimon Schubert }
349*5796c8dcSSimon Schubert 
350*5796c8dcSSimon Schubert /* If FULL_NAME is the demangled name of a C++ function (including an
351*5796c8dcSSimon Schubert    arg list, possibly including namespace/class qualifications),
352*5796c8dcSSimon Schubert    return a new string containing only the function name (without the
353*5796c8dcSSimon Schubert    arg list/class qualifications).  Otherwise, return NULL.  The
354*5796c8dcSSimon Schubert    caller is responsible for freeing the memory in question.  */
355*5796c8dcSSimon Schubert 
356*5796c8dcSSimon Schubert char *
357*5796c8dcSSimon Schubert cp_func_name (const char *full_name)
358*5796c8dcSSimon Schubert {
359*5796c8dcSSimon Schubert   char *ret;
360*5796c8dcSSimon Schubert   struct demangle_component *ret_comp;
361*5796c8dcSSimon Schubert   int done;
362*5796c8dcSSimon Schubert 
363*5796c8dcSSimon Schubert   ret_comp = cp_demangled_name_to_comp (full_name, NULL);
364*5796c8dcSSimon Schubert   if (!ret_comp)
365*5796c8dcSSimon Schubert     return NULL;
366*5796c8dcSSimon Schubert 
367*5796c8dcSSimon Schubert   ret_comp = unqualified_name_from_comp (ret_comp);
368*5796c8dcSSimon Schubert 
369*5796c8dcSSimon Schubert   ret = NULL;
370*5796c8dcSSimon Schubert   if (ret_comp != NULL)
371*5796c8dcSSimon Schubert     ret = cp_comp_to_string (ret_comp, 10);
372*5796c8dcSSimon Schubert 
373*5796c8dcSSimon Schubert   return ret;
374*5796c8dcSSimon Schubert }
375*5796c8dcSSimon Schubert 
376*5796c8dcSSimon Schubert /* DEMANGLED_NAME is the name of a function, including parameters and
377*5796c8dcSSimon Schubert    (optionally) a return type.  Return the name of the function without
378*5796c8dcSSimon Schubert    parameters or return type, or NULL if we can not parse the name.  */
379*5796c8dcSSimon Schubert 
380*5796c8dcSSimon Schubert char *
381*5796c8dcSSimon Schubert cp_remove_params (const char *demangled_name)
382*5796c8dcSSimon Schubert {
383*5796c8dcSSimon Schubert   int done = 0;
384*5796c8dcSSimon Schubert   struct demangle_component *ret_comp;
385*5796c8dcSSimon Schubert   char *ret = NULL;
386*5796c8dcSSimon Schubert 
387*5796c8dcSSimon Schubert   if (demangled_name == NULL)
388*5796c8dcSSimon Schubert     return NULL;
389*5796c8dcSSimon Schubert 
390*5796c8dcSSimon Schubert   ret_comp = cp_demangled_name_to_comp (demangled_name, NULL);
391*5796c8dcSSimon Schubert   if (ret_comp == NULL)
392*5796c8dcSSimon Schubert     return NULL;
393*5796c8dcSSimon Schubert 
394*5796c8dcSSimon Schubert   /* First strip off any qualifiers, if we have a function or method.  */
395*5796c8dcSSimon Schubert   while (!done)
396*5796c8dcSSimon Schubert     switch (ret_comp->type)
397*5796c8dcSSimon Schubert       {
398*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_CONST:
399*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_RESTRICT:
400*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_VOLATILE:
401*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_CONST_THIS:
402*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_RESTRICT_THIS:
403*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_VOLATILE_THIS:
404*5796c8dcSSimon Schubert       case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
405*5796c8dcSSimon Schubert         ret_comp = d_left (ret_comp);
406*5796c8dcSSimon Schubert         break;
407*5796c8dcSSimon Schubert       default:
408*5796c8dcSSimon Schubert 	done = 1;
409*5796c8dcSSimon Schubert 	break;
410*5796c8dcSSimon Schubert       }
411*5796c8dcSSimon Schubert 
412*5796c8dcSSimon Schubert   /* What we have now should be a function.  Return its name.  */
413*5796c8dcSSimon Schubert   if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
414*5796c8dcSSimon Schubert     ret = cp_comp_to_string (d_left (ret_comp), 10);
415*5796c8dcSSimon Schubert 
416*5796c8dcSSimon Schubert   return ret;
417*5796c8dcSSimon Schubert }
418*5796c8dcSSimon Schubert 
419*5796c8dcSSimon Schubert /* Here are some random pieces of trivia to keep in mind while trying
420*5796c8dcSSimon Schubert    to take apart demangled names:
421*5796c8dcSSimon Schubert 
422*5796c8dcSSimon Schubert    - Names can contain function arguments or templates, so the process
423*5796c8dcSSimon Schubert      has to be, to some extent recursive: maybe keep track of your
424*5796c8dcSSimon Schubert      depth based on encountering <> and ().
425*5796c8dcSSimon Schubert 
426*5796c8dcSSimon Schubert    - Parentheses don't just have to happen at the end of a name: they
427*5796c8dcSSimon Schubert      can occur even if the name in question isn't a function, because
428*5796c8dcSSimon Schubert      a template argument might be a type that's a function.
429*5796c8dcSSimon Schubert 
430*5796c8dcSSimon Schubert    - Conversely, even if you're trying to deal with a function, its
431*5796c8dcSSimon Schubert      demangled name might not end with ')': it could be a const or
432*5796c8dcSSimon Schubert      volatile class method, in which case it ends with "const" or
433*5796c8dcSSimon Schubert      "volatile".
434*5796c8dcSSimon Schubert 
435*5796c8dcSSimon Schubert    - Parentheses are also used in anonymous namespaces: a variable
436*5796c8dcSSimon Schubert      'foo' in an anonymous namespace gets demangled as "(anonymous
437*5796c8dcSSimon Schubert      namespace)::foo".
438*5796c8dcSSimon Schubert 
439*5796c8dcSSimon Schubert    - And operator names can contain parentheses or angle brackets.  */
440*5796c8dcSSimon Schubert 
441*5796c8dcSSimon Schubert /* FIXME: carlton/2003-03-13: We have several functions here with
442*5796c8dcSSimon Schubert    overlapping functionality; can we combine them?  Also, do they
443*5796c8dcSSimon Schubert    handle all the above considerations correctly?  */
444*5796c8dcSSimon Schubert 
445*5796c8dcSSimon Schubert 
446*5796c8dcSSimon Schubert /* This returns the length of first component of NAME, which should be
447*5796c8dcSSimon Schubert    the demangled name of a C++ variable/function/method/etc.
448*5796c8dcSSimon Schubert    Specifically, it returns the index of the first colon forming the
449*5796c8dcSSimon Schubert    boundary of the first component: so, given 'A::foo' or 'A::B::foo'
450*5796c8dcSSimon Schubert    it returns the 1, and given 'foo', it returns 0.  */
451*5796c8dcSSimon Schubert 
452*5796c8dcSSimon Schubert /* The character in NAME indexed by the return value is guaranteed to
453*5796c8dcSSimon Schubert    always be either ':' or '\0'.  */
454*5796c8dcSSimon Schubert 
455*5796c8dcSSimon Schubert /* NOTE: carlton/2003-03-13: This function is currently only intended
456*5796c8dcSSimon Schubert    for internal use: it's probably not entirely safe when called on
457*5796c8dcSSimon Schubert    user-generated input, because some of the 'index += 2' lines in
458*5796c8dcSSimon Schubert    cp_find_first_component_aux might go past the end of malformed
459*5796c8dcSSimon Schubert    input.  */
460*5796c8dcSSimon Schubert 
461*5796c8dcSSimon Schubert unsigned int
462*5796c8dcSSimon Schubert cp_find_first_component (const char *name)
463*5796c8dcSSimon Schubert {
464*5796c8dcSSimon Schubert   return cp_find_first_component_aux (name, 0);
465*5796c8dcSSimon Schubert }
466*5796c8dcSSimon Schubert 
467*5796c8dcSSimon Schubert /* Helper function for cp_find_first_component.  Like that function,
468*5796c8dcSSimon Schubert    it returns the length of the first component of NAME, but to make
469*5796c8dcSSimon Schubert    the recursion easier, it also stops if it reaches an unexpected ')'
470*5796c8dcSSimon Schubert    or '>' if the value of PERMISSIVE is nonzero.  */
471*5796c8dcSSimon Schubert 
472*5796c8dcSSimon Schubert /* Let's optimize away calls to strlen("operator").  */
473*5796c8dcSSimon Schubert 
474*5796c8dcSSimon Schubert #define LENGTH_OF_OPERATOR 8
475*5796c8dcSSimon Schubert 
476*5796c8dcSSimon Schubert static unsigned int
477*5796c8dcSSimon Schubert cp_find_first_component_aux (const char *name, int permissive)
478*5796c8dcSSimon Schubert {
479*5796c8dcSSimon Schubert   unsigned int index = 0;
480*5796c8dcSSimon Schubert   /* Operator names can show up in unexpected places.  Since these can
481*5796c8dcSSimon Schubert      contain parentheses or angle brackets, they can screw up the
482*5796c8dcSSimon Schubert      recursion.  But not every string 'operator' is part of an
483*5796c8dcSSimon Schubert      operater name: e.g. you could have a variable 'cooperator'.  So
484*5796c8dcSSimon Schubert      this variable tells us whether or not we should treat the string
485*5796c8dcSSimon Schubert      'operator' as starting an operator.  */
486*5796c8dcSSimon Schubert   int operator_possible = 1;
487*5796c8dcSSimon Schubert 
488*5796c8dcSSimon Schubert   for (;; ++index)
489*5796c8dcSSimon Schubert     {
490*5796c8dcSSimon Schubert       switch (name[index])
491*5796c8dcSSimon Schubert 	{
492*5796c8dcSSimon Schubert 	case '<':
493*5796c8dcSSimon Schubert 	  /* Template; eat it up.  The calls to cp_first_component
494*5796c8dcSSimon Schubert 	     should only return (I hope!) when they reach the '>'
495*5796c8dcSSimon Schubert 	     terminating the component or a '::' between two
496*5796c8dcSSimon Schubert 	     components.  (Hence the '+ 2'.)  */
497*5796c8dcSSimon Schubert 	  index += 1;
498*5796c8dcSSimon Schubert 	  for (index += cp_find_first_component_aux (name + index, 1);
499*5796c8dcSSimon Schubert 	       name[index] != '>';
500*5796c8dcSSimon Schubert 	       index += cp_find_first_component_aux (name + index, 1))
501*5796c8dcSSimon Schubert 	    {
502*5796c8dcSSimon Schubert 	      if (name[index] != ':')
503*5796c8dcSSimon Schubert 		{
504*5796c8dcSSimon Schubert 		  demangled_name_complaint (name);
505*5796c8dcSSimon Schubert 		  return strlen (name);
506*5796c8dcSSimon Schubert 		}
507*5796c8dcSSimon Schubert 	      index += 2;
508*5796c8dcSSimon Schubert 	    }
509*5796c8dcSSimon Schubert 	  operator_possible = 1;
510*5796c8dcSSimon Schubert 	  break;
511*5796c8dcSSimon Schubert 	case '(':
512*5796c8dcSSimon Schubert 	  /* Similar comment as to '<'.  */
513*5796c8dcSSimon Schubert 	  index += 1;
514*5796c8dcSSimon Schubert 	  for (index += cp_find_first_component_aux (name + index, 1);
515*5796c8dcSSimon Schubert 	       name[index] != ')';
516*5796c8dcSSimon Schubert 	       index += cp_find_first_component_aux (name + index, 1))
517*5796c8dcSSimon Schubert 	    {
518*5796c8dcSSimon Schubert 	      if (name[index] != ':')
519*5796c8dcSSimon Schubert 		{
520*5796c8dcSSimon Schubert 		  demangled_name_complaint (name);
521*5796c8dcSSimon Schubert 		  return strlen (name);
522*5796c8dcSSimon Schubert 		}
523*5796c8dcSSimon Schubert 	      index += 2;
524*5796c8dcSSimon Schubert 	    }
525*5796c8dcSSimon Schubert 	  operator_possible = 1;
526*5796c8dcSSimon Schubert 	  break;
527*5796c8dcSSimon Schubert 	case '>':
528*5796c8dcSSimon Schubert 	case ')':
529*5796c8dcSSimon Schubert 	  if (permissive)
530*5796c8dcSSimon Schubert 	    return index;
531*5796c8dcSSimon Schubert 	  else
532*5796c8dcSSimon Schubert 	    {
533*5796c8dcSSimon Schubert 	      demangled_name_complaint (name);
534*5796c8dcSSimon Schubert 	      return strlen (name);
535*5796c8dcSSimon Schubert 	    }
536*5796c8dcSSimon Schubert 	case '\0':
537*5796c8dcSSimon Schubert 	case ':':
538*5796c8dcSSimon Schubert 	  return index;
539*5796c8dcSSimon Schubert 	case 'o':
540*5796c8dcSSimon Schubert 	  /* Operator names can screw up the recursion.  */
541*5796c8dcSSimon Schubert 	  if (operator_possible
542*5796c8dcSSimon Schubert 	      && strncmp (name + index, "operator", LENGTH_OF_OPERATOR) == 0)
543*5796c8dcSSimon Schubert 	    {
544*5796c8dcSSimon Schubert 	      index += LENGTH_OF_OPERATOR;
545*5796c8dcSSimon Schubert 	      while (ISSPACE(name[index]))
546*5796c8dcSSimon Schubert 		++index;
547*5796c8dcSSimon Schubert 	      switch (name[index])
548*5796c8dcSSimon Schubert 		{
549*5796c8dcSSimon Schubert 		  /* Skip over one less than the appropriate number of
550*5796c8dcSSimon Schubert 		     characters: the for loop will skip over the last
551*5796c8dcSSimon Schubert 		     one.  */
552*5796c8dcSSimon Schubert 		case '<':
553*5796c8dcSSimon Schubert 		  if (name[index + 1] == '<')
554*5796c8dcSSimon Schubert 		    index += 1;
555*5796c8dcSSimon Schubert 		  else
556*5796c8dcSSimon Schubert 		    index += 0;
557*5796c8dcSSimon Schubert 		  break;
558*5796c8dcSSimon Schubert 		case '>':
559*5796c8dcSSimon Schubert 		case '-':
560*5796c8dcSSimon Schubert 		  if (name[index + 1] == '>')
561*5796c8dcSSimon Schubert 		    index += 1;
562*5796c8dcSSimon Schubert 		  else
563*5796c8dcSSimon Schubert 		    index += 0;
564*5796c8dcSSimon Schubert 		  break;
565*5796c8dcSSimon Schubert 		case '(':
566*5796c8dcSSimon Schubert 		  index += 1;
567*5796c8dcSSimon Schubert 		  break;
568*5796c8dcSSimon Schubert 		default:
569*5796c8dcSSimon Schubert 		  index += 0;
570*5796c8dcSSimon Schubert 		  break;
571*5796c8dcSSimon Schubert 		}
572*5796c8dcSSimon Schubert 	    }
573*5796c8dcSSimon Schubert 	  operator_possible = 0;
574*5796c8dcSSimon Schubert 	  break;
575*5796c8dcSSimon Schubert 	case ' ':
576*5796c8dcSSimon Schubert 	case ',':
577*5796c8dcSSimon Schubert 	case '.':
578*5796c8dcSSimon Schubert 	case '&':
579*5796c8dcSSimon Schubert 	case '*':
580*5796c8dcSSimon Schubert 	  /* NOTE: carlton/2003-04-18: I'm not sure what the precise
581*5796c8dcSSimon Schubert 	     set of relevant characters are here: it's necessary to
582*5796c8dcSSimon Schubert 	     include any character that can show up before 'operator'
583*5796c8dcSSimon Schubert 	     in a demangled name, and it's safe to include any
584*5796c8dcSSimon Schubert 	     character that can't be part of an identifier's name.  */
585*5796c8dcSSimon Schubert 	  operator_possible = 1;
586*5796c8dcSSimon Schubert 	  break;
587*5796c8dcSSimon Schubert 	default:
588*5796c8dcSSimon Schubert 	  operator_possible = 0;
589*5796c8dcSSimon Schubert 	  break;
590*5796c8dcSSimon Schubert 	}
591*5796c8dcSSimon Schubert     }
592*5796c8dcSSimon Schubert }
593*5796c8dcSSimon Schubert 
594*5796c8dcSSimon Schubert /* Complain about a demangled name that we don't know how to parse.
595*5796c8dcSSimon Schubert    NAME is the demangled name in question.  */
596*5796c8dcSSimon Schubert 
597*5796c8dcSSimon Schubert static void
598*5796c8dcSSimon Schubert demangled_name_complaint (const char *name)
599*5796c8dcSSimon Schubert {
600*5796c8dcSSimon Schubert   complaint (&symfile_complaints,
601*5796c8dcSSimon Schubert 	     "unexpected demangled name '%s'", name);
602*5796c8dcSSimon Schubert }
603*5796c8dcSSimon Schubert 
604*5796c8dcSSimon Schubert /* If NAME is the fully-qualified name of a C++
605*5796c8dcSSimon Schubert    function/variable/method/etc., this returns the length of its
606*5796c8dcSSimon Schubert    entire prefix: all of the namespaces and classes that make up its
607*5796c8dcSSimon Schubert    name.  Given 'A::foo', it returns 1, given 'A::B::foo', it returns
608*5796c8dcSSimon Schubert    4, given 'foo', it returns 0.  */
609*5796c8dcSSimon Schubert 
610*5796c8dcSSimon Schubert unsigned int
611*5796c8dcSSimon Schubert cp_entire_prefix_len (const char *name)
612*5796c8dcSSimon Schubert {
613*5796c8dcSSimon Schubert   unsigned int current_len = cp_find_first_component (name);
614*5796c8dcSSimon Schubert   unsigned int previous_len = 0;
615*5796c8dcSSimon Schubert 
616*5796c8dcSSimon Schubert   while (name[current_len] != '\0')
617*5796c8dcSSimon Schubert     {
618*5796c8dcSSimon Schubert       gdb_assert (name[current_len] == ':');
619*5796c8dcSSimon Schubert       previous_len = current_len;
620*5796c8dcSSimon Schubert       /* Skip the '::'.  */
621*5796c8dcSSimon Schubert       current_len += 2;
622*5796c8dcSSimon Schubert       current_len += cp_find_first_component (name + current_len);
623*5796c8dcSSimon Schubert     }
624*5796c8dcSSimon Schubert 
625*5796c8dcSSimon Schubert   return previous_len;
626*5796c8dcSSimon Schubert }
627*5796c8dcSSimon Schubert 
628*5796c8dcSSimon Schubert /* Overload resolution functions.  */
629*5796c8dcSSimon Schubert 
630*5796c8dcSSimon Schubert /* Test to see if SYM is a symbol that we haven't seen corresponding
631*5796c8dcSSimon Schubert    to a function named OLOAD_NAME.  If so, add it to the current
632*5796c8dcSSimon Schubert    completion list. */
633*5796c8dcSSimon Schubert 
634*5796c8dcSSimon Schubert static void
635*5796c8dcSSimon Schubert overload_list_add_symbol (struct symbol *sym, const char *oload_name)
636*5796c8dcSSimon Schubert {
637*5796c8dcSSimon Schubert   int newsize;
638*5796c8dcSSimon Schubert   int i;
639*5796c8dcSSimon Schubert   char *sym_name;
640*5796c8dcSSimon Schubert 
641*5796c8dcSSimon Schubert   /* If there is no type information, we can't do anything, so skip */
642*5796c8dcSSimon Schubert   if (SYMBOL_TYPE (sym) == NULL)
643*5796c8dcSSimon Schubert     return;
644*5796c8dcSSimon Schubert 
645*5796c8dcSSimon Schubert   /* skip any symbols that we've already considered. */
646*5796c8dcSSimon Schubert   for (i = 0; i < sym_return_val_index; ++i)
647*5796c8dcSSimon Schubert     if (strcmp (SYMBOL_LINKAGE_NAME (sym),
648*5796c8dcSSimon Schubert 		SYMBOL_LINKAGE_NAME (sym_return_val[i])) == 0)
649*5796c8dcSSimon Schubert       return;
650*5796c8dcSSimon Schubert 
651*5796c8dcSSimon Schubert   /* Get the demangled name without parameters */
652*5796c8dcSSimon Schubert   sym_name = cp_remove_params (SYMBOL_NATURAL_NAME (sym));
653*5796c8dcSSimon Schubert   if (!sym_name)
654*5796c8dcSSimon Schubert     return;
655*5796c8dcSSimon Schubert 
656*5796c8dcSSimon Schubert   /* skip symbols that cannot match */
657*5796c8dcSSimon Schubert   if (strcmp (sym_name, oload_name) != 0)
658*5796c8dcSSimon Schubert     {
659*5796c8dcSSimon Schubert       xfree (sym_name);
660*5796c8dcSSimon Schubert       return;
661*5796c8dcSSimon Schubert     }
662*5796c8dcSSimon Schubert 
663*5796c8dcSSimon Schubert   xfree (sym_name);
664*5796c8dcSSimon Schubert 
665*5796c8dcSSimon Schubert   /* We have a match for an overload instance, so add SYM to the current list
666*5796c8dcSSimon Schubert    * of overload instances */
667*5796c8dcSSimon Schubert   if (sym_return_val_index + 3 > sym_return_val_size)
668*5796c8dcSSimon Schubert     {
669*5796c8dcSSimon Schubert       newsize = (sym_return_val_size *= 2) * sizeof (struct symbol *);
670*5796c8dcSSimon Schubert       sym_return_val = (struct symbol **) xrealloc ((char *) sym_return_val, newsize);
671*5796c8dcSSimon Schubert     }
672*5796c8dcSSimon Schubert   sym_return_val[sym_return_val_index++] = sym;
673*5796c8dcSSimon Schubert   sym_return_val[sym_return_val_index] = NULL;
674*5796c8dcSSimon Schubert }
675*5796c8dcSSimon Schubert 
676*5796c8dcSSimon Schubert /* Return a null-terminated list of pointers to function symbols that
677*5796c8dcSSimon Schubert    are named FUNC_NAME and are visible within NAMESPACE.  */
678*5796c8dcSSimon Schubert 
679*5796c8dcSSimon Schubert struct symbol **
680*5796c8dcSSimon Schubert make_symbol_overload_list (const char *func_name,
681*5796c8dcSSimon Schubert 			   const char *namespace)
682*5796c8dcSSimon Schubert {
683*5796c8dcSSimon Schubert   struct cleanup *old_cleanups;
684*5796c8dcSSimon Schubert 
685*5796c8dcSSimon Schubert   sym_return_val_size = 100;
686*5796c8dcSSimon Schubert   sym_return_val_index = 0;
687*5796c8dcSSimon Schubert   sym_return_val = xmalloc ((sym_return_val_size + 1) *
688*5796c8dcSSimon Schubert 			    sizeof (struct symbol *));
689*5796c8dcSSimon Schubert   sym_return_val[0] = NULL;
690*5796c8dcSSimon Schubert 
691*5796c8dcSSimon Schubert   old_cleanups = make_cleanup (xfree, sym_return_val);
692*5796c8dcSSimon Schubert 
693*5796c8dcSSimon Schubert   make_symbol_overload_list_using (func_name, namespace);
694*5796c8dcSSimon Schubert 
695*5796c8dcSSimon Schubert   discard_cleanups (old_cleanups);
696*5796c8dcSSimon Schubert 
697*5796c8dcSSimon Schubert   return sym_return_val;
698*5796c8dcSSimon Schubert }
699*5796c8dcSSimon Schubert 
700*5796c8dcSSimon Schubert /* This applies the using directives to add namespaces to search in,
701*5796c8dcSSimon Schubert    and then searches for overloads in all of those namespaces.  It
702*5796c8dcSSimon Schubert    adds the symbols found to sym_return_val.  Arguments are as in
703*5796c8dcSSimon Schubert    make_symbol_overload_list.  */
704*5796c8dcSSimon Schubert 
705*5796c8dcSSimon Schubert static void
706*5796c8dcSSimon Schubert make_symbol_overload_list_using (const char *func_name,
707*5796c8dcSSimon Schubert 				 const char *namespace)
708*5796c8dcSSimon Schubert {
709*5796c8dcSSimon Schubert   const struct using_direct *current;
710*5796c8dcSSimon Schubert 
711*5796c8dcSSimon Schubert   /* First, go through the using directives.  If any of them apply,
712*5796c8dcSSimon Schubert      look in the appropriate namespaces for new functions to match
713*5796c8dcSSimon Schubert      on.  */
714*5796c8dcSSimon Schubert 
715*5796c8dcSSimon Schubert   for (current = block_using (get_selected_block (0));
716*5796c8dcSSimon Schubert        current != NULL;
717*5796c8dcSSimon Schubert        current = current->next)
718*5796c8dcSSimon Schubert     {
719*5796c8dcSSimon Schubert       if (strcmp (namespace, current->import_dest) == 0)
720*5796c8dcSSimon Schubert 	{
721*5796c8dcSSimon Schubert 	  make_symbol_overload_list_using (func_name,
722*5796c8dcSSimon Schubert 					   current->import_src);
723*5796c8dcSSimon Schubert 	}
724*5796c8dcSSimon Schubert     }
725*5796c8dcSSimon Schubert 
726*5796c8dcSSimon Schubert   /* Now, add names for this namespace.  */
727*5796c8dcSSimon Schubert 
728*5796c8dcSSimon Schubert   if (namespace[0] == '\0')
729*5796c8dcSSimon Schubert     {
730*5796c8dcSSimon Schubert       make_symbol_overload_list_qualified (func_name);
731*5796c8dcSSimon Schubert     }
732*5796c8dcSSimon Schubert   else
733*5796c8dcSSimon Schubert     {
734*5796c8dcSSimon Schubert       char *concatenated_name
735*5796c8dcSSimon Schubert 	= alloca (strlen (namespace) + 2 + strlen (func_name) + 1);
736*5796c8dcSSimon Schubert       strcpy (concatenated_name, namespace);
737*5796c8dcSSimon Schubert       strcat (concatenated_name, "::");
738*5796c8dcSSimon Schubert       strcat (concatenated_name, func_name);
739*5796c8dcSSimon Schubert       make_symbol_overload_list_qualified (concatenated_name);
740*5796c8dcSSimon Schubert     }
741*5796c8dcSSimon Schubert }
742*5796c8dcSSimon Schubert 
743*5796c8dcSSimon Schubert /* This does the bulk of the work of finding overloaded symbols.
744*5796c8dcSSimon Schubert    FUNC_NAME is the name of the overloaded function we're looking for
745*5796c8dcSSimon Schubert    (possibly including namespace info).  */
746*5796c8dcSSimon Schubert 
747*5796c8dcSSimon Schubert static void
748*5796c8dcSSimon Schubert make_symbol_overload_list_qualified (const char *func_name)
749*5796c8dcSSimon Schubert {
750*5796c8dcSSimon Schubert   struct symbol *sym;
751*5796c8dcSSimon Schubert   struct symtab *s;
752*5796c8dcSSimon Schubert   struct objfile *objfile;
753*5796c8dcSSimon Schubert   const struct block *b, *surrounding_static_block = 0;
754*5796c8dcSSimon Schubert   struct dict_iterator iter;
755*5796c8dcSSimon Schubert   const struct dictionary *dict;
756*5796c8dcSSimon Schubert 
757*5796c8dcSSimon Schubert   /* Look through the partial symtabs for all symbols which begin
758*5796c8dcSSimon Schubert      by matching FUNC_NAME.  Make sure we read that symbol table in. */
759*5796c8dcSSimon Schubert 
760*5796c8dcSSimon Schubert   read_in_psymtabs (func_name);
761*5796c8dcSSimon Schubert 
762*5796c8dcSSimon Schubert   /* Search upwards from currently selected frame (so that we can
763*5796c8dcSSimon Schubert      complete on local vars.  */
764*5796c8dcSSimon Schubert 
765*5796c8dcSSimon Schubert   for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
766*5796c8dcSSimon Schubert     {
767*5796c8dcSSimon Schubert       dict = BLOCK_DICT (b);
768*5796c8dcSSimon Schubert 
769*5796c8dcSSimon Schubert       for (sym = dict_iter_name_first (dict, func_name, &iter);
770*5796c8dcSSimon Schubert 	   sym;
771*5796c8dcSSimon Schubert 	   sym = dict_iter_name_next (func_name, &iter))
772*5796c8dcSSimon Schubert 	{
773*5796c8dcSSimon Schubert 	  overload_list_add_symbol (sym, func_name);
774*5796c8dcSSimon Schubert 	}
775*5796c8dcSSimon Schubert     }
776*5796c8dcSSimon Schubert 
777*5796c8dcSSimon Schubert   surrounding_static_block = block_static_block (get_selected_block (0));
778*5796c8dcSSimon Schubert 
779*5796c8dcSSimon Schubert   /* Go through the symtabs and check the externs and statics for
780*5796c8dcSSimon Schubert      symbols which match.  */
781*5796c8dcSSimon Schubert 
782*5796c8dcSSimon Schubert   ALL_PRIMARY_SYMTABS (objfile, s)
783*5796c8dcSSimon Schubert   {
784*5796c8dcSSimon Schubert     QUIT;
785*5796c8dcSSimon Schubert     b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
786*5796c8dcSSimon Schubert     dict = BLOCK_DICT (b);
787*5796c8dcSSimon Schubert 
788*5796c8dcSSimon Schubert     for (sym = dict_iter_name_first (dict, func_name, &iter);
789*5796c8dcSSimon Schubert 	 sym;
790*5796c8dcSSimon Schubert 	 sym = dict_iter_name_next (func_name, &iter))
791*5796c8dcSSimon Schubert     {
792*5796c8dcSSimon Schubert       overload_list_add_symbol (sym, func_name);
793*5796c8dcSSimon Schubert     }
794*5796c8dcSSimon Schubert   }
795*5796c8dcSSimon Schubert 
796*5796c8dcSSimon Schubert   ALL_PRIMARY_SYMTABS (objfile, s)
797*5796c8dcSSimon Schubert   {
798*5796c8dcSSimon Schubert     QUIT;
799*5796c8dcSSimon Schubert     b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
800*5796c8dcSSimon Schubert     /* Don't do this block twice.  */
801*5796c8dcSSimon Schubert     if (b == surrounding_static_block)
802*5796c8dcSSimon Schubert       continue;
803*5796c8dcSSimon Schubert     dict = BLOCK_DICT (b);
804*5796c8dcSSimon Schubert 
805*5796c8dcSSimon Schubert     for (sym = dict_iter_name_first (dict, func_name, &iter);
806*5796c8dcSSimon Schubert 	 sym;
807*5796c8dcSSimon Schubert 	 sym = dict_iter_name_next (func_name, &iter))
808*5796c8dcSSimon Schubert     {
809*5796c8dcSSimon Schubert       overload_list_add_symbol (sym, func_name);
810*5796c8dcSSimon Schubert     }
811*5796c8dcSSimon Schubert   }
812*5796c8dcSSimon Schubert }
813*5796c8dcSSimon Schubert 
814*5796c8dcSSimon Schubert /* Look through the partial symtabs for all symbols which begin
815*5796c8dcSSimon Schubert    by matching FUNC_NAME.  Make sure we read that symbol table in. */
816*5796c8dcSSimon Schubert 
817*5796c8dcSSimon Schubert static void
818*5796c8dcSSimon Schubert read_in_psymtabs (const char *func_name)
819*5796c8dcSSimon Schubert {
820*5796c8dcSSimon Schubert   struct partial_symtab *ps;
821*5796c8dcSSimon Schubert   struct objfile *objfile;
822*5796c8dcSSimon Schubert 
823*5796c8dcSSimon Schubert   ALL_PSYMTABS (objfile, ps)
824*5796c8dcSSimon Schubert   {
825*5796c8dcSSimon Schubert     if (ps->readin)
826*5796c8dcSSimon Schubert       continue;
827*5796c8dcSSimon Schubert 
828*5796c8dcSSimon Schubert     if ((lookup_partial_symbol (ps, func_name, NULL, 1, VAR_DOMAIN)
829*5796c8dcSSimon Schubert 	 != NULL)
830*5796c8dcSSimon Schubert 	|| (lookup_partial_symbol (ps, func_name, NULL, 0, VAR_DOMAIN)
831*5796c8dcSSimon Schubert 	    != NULL))
832*5796c8dcSSimon Schubert       psymtab_to_symtab (ps);
833*5796c8dcSSimon Schubert   }
834*5796c8dcSSimon Schubert }
835*5796c8dcSSimon Schubert 
836*5796c8dcSSimon Schubert /* Lookup the rtti type for a class name. */
837*5796c8dcSSimon Schubert 
838*5796c8dcSSimon Schubert struct type *
839*5796c8dcSSimon Schubert cp_lookup_rtti_type (const char *name, struct block *block)
840*5796c8dcSSimon Schubert {
841*5796c8dcSSimon Schubert   struct symbol * rtti_sym;
842*5796c8dcSSimon Schubert   struct type * rtti_type;
843*5796c8dcSSimon Schubert 
844*5796c8dcSSimon Schubert   rtti_sym = lookup_symbol (name, block, STRUCT_DOMAIN, NULL);
845*5796c8dcSSimon Schubert 
846*5796c8dcSSimon Schubert   if (rtti_sym == NULL)
847*5796c8dcSSimon Schubert     {
848*5796c8dcSSimon Schubert       warning (_("RTTI symbol not found for class '%s'"), name);
849*5796c8dcSSimon Schubert       return NULL;
850*5796c8dcSSimon Schubert     }
851*5796c8dcSSimon Schubert 
852*5796c8dcSSimon Schubert   if (SYMBOL_CLASS (rtti_sym) != LOC_TYPEDEF)
853*5796c8dcSSimon Schubert     {
854*5796c8dcSSimon Schubert       warning (_("RTTI symbol for class '%s' is not a type"), name);
855*5796c8dcSSimon Schubert       return NULL;
856*5796c8dcSSimon Schubert     }
857*5796c8dcSSimon Schubert 
858*5796c8dcSSimon Schubert   rtti_type = SYMBOL_TYPE (rtti_sym);
859*5796c8dcSSimon Schubert 
860*5796c8dcSSimon Schubert   switch (TYPE_CODE (rtti_type))
861*5796c8dcSSimon Schubert     {
862*5796c8dcSSimon Schubert     case TYPE_CODE_CLASS:
863*5796c8dcSSimon Schubert       break;
864*5796c8dcSSimon Schubert     case TYPE_CODE_NAMESPACE:
865*5796c8dcSSimon Schubert       /* chastain/2003-11-26: the symbol tables often contain fake
866*5796c8dcSSimon Schubert 	 symbols for namespaces with the same name as the struct.
867*5796c8dcSSimon Schubert 	 This warning is an indication of a bug in the lookup order
868*5796c8dcSSimon Schubert 	 or a bug in the way that the symbol tables are populated.  */
869*5796c8dcSSimon Schubert       warning (_("RTTI symbol for class '%s' is a namespace"), name);
870*5796c8dcSSimon Schubert       return NULL;
871*5796c8dcSSimon Schubert     default:
872*5796c8dcSSimon Schubert       warning (_("RTTI symbol for class '%s' has bad type"), name);
873*5796c8dcSSimon Schubert       return NULL;
874*5796c8dcSSimon Schubert     }
875*5796c8dcSSimon Schubert 
876*5796c8dcSSimon Schubert   return rtti_type;
877*5796c8dcSSimon Schubert }
878*5796c8dcSSimon Schubert 
879*5796c8dcSSimon Schubert /* Don't allow just "maintenance cplus".  */
880*5796c8dcSSimon Schubert 
881*5796c8dcSSimon Schubert static  void
882*5796c8dcSSimon Schubert maint_cplus_command (char *arg, int from_tty)
883*5796c8dcSSimon Schubert {
884*5796c8dcSSimon Schubert   printf_unfiltered (_("\"maintenance cplus\" must be followed by the name of a command.\n"));
885*5796c8dcSSimon Schubert   help_list (maint_cplus_cmd_list, "maintenance cplus ", -1, gdb_stdout);
886*5796c8dcSSimon Schubert }
887*5796c8dcSSimon Schubert 
888*5796c8dcSSimon Schubert /* This is a front end for cp_find_first_component, for unit testing.
889*5796c8dcSSimon Schubert    Be careful when using it: see the NOTE above
890*5796c8dcSSimon Schubert    cp_find_first_component.  */
891*5796c8dcSSimon Schubert 
892*5796c8dcSSimon Schubert static void
893*5796c8dcSSimon Schubert first_component_command (char *arg, int from_tty)
894*5796c8dcSSimon Schubert {
895*5796c8dcSSimon Schubert   int len;
896*5796c8dcSSimon Schubert   char *prefix;
897*5796c8dcSSimon Schubert 
898*5796c8dcSSimon Schubert   if (!arg)
899*5796c8dcSSimon Schubert     return;
900*5796c8dcSSimon Schubert 
901*5796c8dcSSimon Schubert   len = cp_find_first_component (arg);
902*5796c8dcSSimon Schubert   prefix = alloca (len + 1);
903*5796c8dcSSimon Schubert 
904*5796c8dcSSimon Schubert   memcpy (prefix, arg, len);
905*5796c8dcSSimon Schubert   prefix[len] = '\0';
906*5796c8dcSSimon Schubert 
907*5796c8dcSSimon Schubert   printf_unfiltered ("%s\n", prefix);
908*5796c8dcSSimon Schubert }
909*5796c8dcSSimon Schubert 
910*5796c8dcSSimon Schubert extern initialize_file_ftype _initialize_cp_support; /* -Wmissing-prototypes */
911*5796c8dcSSimon Schubert 
912*5796c8dcSSimon Schubert void
913*5796c8dcSSimon Schubert _initialize_cp_support (void)
914*5796c8dcSSimon Schubert {
915*5796c8dcSSimon Schubert   add_prefix_cmd ("cplus", class_maintenance, maint_cplus_command,
916*5796c8dcSSimon Schubert 		  _("C++ maintenance commands."), &maint_cplus_cmd_list,
917*5796c8dcSSimon Schubert 		  "maintenance cplus ", 0, &maintenancelist);
918*5796c8dcSSimon Schubert   add_alias_cmd ("cp", "cplus", class_maintenance, 1, &maintenancelist);
919*5796c8dcSSimon Schubert 
920*5796c8dcSSimon Schubert   add_cmd ("first_component", class_maintenance, first_component_command,
921*5796c8dcSSimon Schubert 	   _("Print the first class/namespace component of NAME."),
922*5796c8dcSSimon Schubert 	   &maint_cplus_cmd_list);
923*5796c8dcSSimon Schubert }
924